1 use nihav_core::codecs::*;
2 use nihav_core::io::byteio::*;
13 use motion_est::MVSearchMode;
17 #[derive(PartialEq,Debug)]
26 stream: Option<NAStreamRef>,
27 pkt: Option<NAPacket>,
36 metric: RateDistMetric,
39 br_ctl: BitRateControl,
41 last_frame: NABufferType,
42 gold_frame: NABufferType,
44 me_mode: MVSearchMode,
49 mb_weight: Vec<usize>,
52 qframes: Vec<NAFrame>,
53 frm_pool: NAVideoBufferPool<u8>,
54 i_frame: NAVideoBufferRef<u8>,
55 enc_state: EncodingState,
60 let vt = alloc_video_buffer(NAVideoInfo::new(24, 24, false, YUV420_FORMAT), 4).unwrap();
61 let mc_buf1 = vt.get_vbuf().unwrap();
62 let vt = alloc_video_buffer(NAVideoInfo::new(24, 24, false, YUV420_FORMAT), 4).unwrap();
63 let mc_buf2 = vt.get_vbuf().unwrap();
64 let vt = alloc_video_buffer(NAVideoInfo::new(24, 24, false, YUV420_FORMAT), 4).unwrap();
65 let i_frame = vt.get_vbuf().unwrap();
77 metric: RateDistMetric::new(),
78 fenc: FrameEncoder::new(mc_buf1, mc_buf2),
79 pmodels: VP7Models::new(),
80 br_ctl: BitRateControl::new(),
82 last_frame: NABufferType::None,
83 gold_frame: NABufferType::None,
85 me_mode: MVSearchMode::default(),
90 mb_weight: Vec::new(),
94 frm_pool: NAVideoBufferPool::new(0),
96 enc_state: EncodingState::JustEncode,
99 fn encode_frame(&mut self, frm: &NAFrame) -> EncoderResult<()> {
100 let buf = frm.get_buffer();
101 if let Some(ref vbuf) = buf.get_vbuf() {
102 self.fenc.set_me_params(self.me_mode, self.me_range, self.version);
103 self.fenc.load_frame(vbuf);
105 let mut dbuf = Vec::with_capacity(4);
106 let mut gw = GrowableMemoryWriter::new_write(&mut dbuf);
107 let mut bw = ByteWriter::new(&mut gw);
109 let is_intra = self.frmcount == 0;
110 let golden_frame = is_intra;
112 self.br_ctl.set_key_interval(self.key_int);
113 let cur_quant = self.br_ctl.get_frame_quant(is_intra);
115 if let Some(level) = self.lf_level {
116 self.fenc.loop_params.loop_filter_level = level;
118 self.fenc.loop_params.loop_filter_level = if cur_quant <= 16 { 0 } else { (cur_quant / 4) as u8 };
122 self.pmodels.reset();
123 let mbt_frames = self.mbt_depth.min(self.key_int as usize);
124 self.fenc.intra_blocks(cur_quant, &self.metric, &self.pmodels, if mbt_frames > 0 { Some(&self.mb_weight) } else { None });
126 let gold_ref = if !self.last_gold { &self.gold_frame } else { &NABufferType::None };
127 self.fenc.inter_blocks(cur_quant, &self.metric, &self.pmodels, &self.last_frame, gold_ref);
130 let mut stats = VP7ModelsStat::new();
131 let mut models = self.pmodels;
132 self.fenc.generate_models(is_intra, &mut stats);
133 stats.generate(&mut models, is_intra);
135 bw.write_u24le(0)?; // frame tag
136 if self.version == 0 {
137 bw.write_byte(0)?; // unused
140 let start = bw.tell();
142 let mut bc = BoolEncoder::new(&mut bw);
144 bc.put_bits(self.width as u32, 12)?;
145 bc.put_bits(self.height as u32, 12)?;
146 bc.put_bits(0, 2)?; // scale vertical
147 bc.put_bits(0, 2)?; // scale horizontal
150 self.fenc.encode_features(&mut bc, cur_quant, &models)?;
152 bc.put_bits(cur_quant as u32, 7)?; // y_ac_q
153 bc.put_bool(false, 128)?; // y_dc_q
154 bc.put_bool(false, 128)?; // y2_ac_q
155 bc.put_bool(false, 128)?; // y2_dc_q
156 bc.put_bool(false, 128)?; // uv_ac_q
157 bc.put_bool(false, 128)?; // uv_dc_q
160 bc.put_bool(false, 128)?; // update golden frame
163 let has_fading = self.version == 0 || is_intra;
164 if self.version != 0 {
165 bc.put_bool(true, 128)?; // keep probabilities
167 bc.put_bool(false, 128)?; // has fading feature
171 bc.put_bool(false, 128)?; // fading
174 if self.version == 0 {
175 bc.put_bool(self.fenc.loop_params.lf_simple, 128)?;
179 bc.put_bool(false, 128)?;
181 if self.version != 0 {
182 bc.put_bool(self.fenc.loop_params.lf_simple, 128)?;
185 bc.put_bits(u32::from(self.fenc.loop_params.loop_filter_level), 6)?;
186 bc.put_bits(u32::from(self.fenc.loop_params.loop_sharpness), 3)?;
188 encode_dct_coef_prob_upd(&mut bc, &models.coef_probs, &self.pmodels.coef_probs)?;
191 bc.put_byte(models.prob_intra_pred)?;
192 bc.put_byte(models.prob_last_pred)?;
194 let ymode_differs = models.kf_ymode_prob != self.pmodels.kf_ymode_prob;
195 bc.put_bool(ymode_differs, 128)?;
197 for &el in models.kf_ymode_prob.iter() {
202 let uvmode_differs = models.kf_uvmode_prob != self.pmodels.kf_uvmode_prob;
203 bc.put_bool(uvmode_differs, 128)?;
205 for &el in models.kf_uvmode_prob.iter() {
210 encode_mv_prob_upd(&mut bc, &models.mv_probs, &self.pmodels.mv_probs)?;
213 self.fenc.encode_mb_types(&mut bc, is_intra, &models)?;
218 let mut bc = BoolEncoder::new(&mut bw);
219 self.fenc.encode_residues(&mut bc, &models)?;
222 bw.seek(SeekFrom::Start(0))?;
223 bw.write_u24le((((end - start) as u32) << 4) |
224 (u32::from(self.version) << 1) |
225 if is_intra { 0 } else { 1 })?;
227 let cur_size = dbuf.len();
229 self.pkt = Some(NAPacket::new(self.stream.clone().unwrap(), frm.ts, is_intra, dbuf));
231 self.pmodels = models;
233 if self.key_int > 0 {
236 if self.frmcount == self.key_int {
240 if let Some(ref mut vbuf) = self.last_frame.get_vbuf() {
241 let mut frm = NASimpleVideoFrame::from_video_buf(vbuf).unwrap();
242 self.fenc.reconstruct_frame(&mut frm, is_intra);
244 self.last_gold = golden_frame;
246 let mut dfrm = self.gold_frame.get_vbuf().unwrap();
247 let src = self.last_frame.get_vbuf().unwrap();
249 let dst = dfrm.get_data_mut().unwrap();
250 dst.copy_from_slice(src.get_data());
253 self.br_ctl.update(cur_size);
254 if self.br_ctl.has_bitrate() {
255 let tgt_size = (self.br_ctl.get_target_size(is_intra) / 8) as usize;
256 self.metric.adjust_br(cur_size, tgt_size);
261 Err(EncoderError::InvalidParameters)
266 impl NAEncoder for VP7Encoder {
267 fn negotiate_format(&self, encinfo: &EncodeParameters) -> EncoderResult<EncodeParameters> {
268 match encinfo.format {
269 NACodecTypeInfo::None => {
270 Ok(EncodeParameters {
271 format: NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, false, YUV420_FORMAT)),
272 ..Default::default() })
274 NACodecTypeInfo::Audio(_) => Err(EncoderError::FormatError),
275 NACodecTypeInfo::Video(vinfo) => {
276 let outinfo = NAVideoInfo::new((vinfo.width + 15) & !15, (vinfo.height + 15) & !15, false, YUV420_FORMAT);
277 let mut ofmt = *encinfo;
278 ofmt.format = NACodecTypeInfo::Video(outinfo);
283 fn get_capabilities(&self) -> u64 { 0 }
284 fn init(&mut self, stream_id: u32, encinfo: EncodeParameters) -> EncoderResult<NAStreamRef> {
285 match encinfo.format {
286 NACodecTypeInfo::None => Err(EncoderError::FormatError),
287 NACodecTypeInfo::Audio(_) => Err(EncoderError::FormatError),
288 NACodecTypeInfo::Video(vinfo) => {
289 if vinfo.format != YUV420_FORMAT {
290 return Err(EncoderError::FormatError);
292 if ((vinfo.width | vinfo.height) & 15) != 0 {
293 return Err(EncoderError::FormatError);
295 if (vinfo.width | vinfo.height) >= (1 << 12) {
296 return Err(EncoderError::FormatError);
299 let out_info = NAVideoInfo::new(vinfo.width, vinfo.height, false, vinfo.format);
300 let info = NACodecInfo::new("vp7", NACodecTypeInfo::Video(out_info), None);
301 let mut stream = NAStream::new(StreamType::Video, stream_id, info, encinfo.tb_num, encinfo.tb_den, 0);
302 stream.set_num(stream_id as usize);
303 let stream = stream.into_ref();
305 self.last_frame = alloc_video_buffer(out_info, 4)?;
306 self.gold_frame = alloc_video_buffer(out_info, 4)?;
308 self.stream = Some(stream.clone());
310 self.width = vinfo.width;
311 self.height = vinfo.height;
312 self.mb_w = (vinfo.width + 15) >> 4;
313 self.mb_h = (vinfo.height + 15) >> 4;
314 self.fenc.resize(self.mb_w, self.mb_h);
316 self.br_ctl.set_params(encinfo.tb_num, encinfo.tb_den, encinfo.bitrate, self.key_int, self.mb_w * self.mb_h);
318 self.frm_pool.reset();
319 self.frm_pool.set_dec_bufs(self.mbt_depth + 1);
320 self.frm_pool.prealloc_video(out_info, 4)?;
321 self.i_frame = self.frm_pool.get_free().unwrap();
322 self.mb_weight.resize(self.mb_w * self.mb_h, 0);
323 self.mb_map.resize(self.mb_w * self.mb_h, 0);
324 self.mb_map2.resize(self.mb_w * self.mb_h, 0);
325 self.qframes.clear();
326 self.enc_state = if self.mbt_depth.min(self.key_int as usize) > 0 {
329 EncodingState::JustEncode
336 fn encode(&mut self, frm: &NAFrame) -> EncoderResult<()> {
337 if let Some(ref vbuf) = frm.get_buffer().get_vbuf() {
338 let mbt_frames = self.mbt_depth.min(self.key_int as usize);
339 if !self.qframes.is_empty() || (mbt_frames > 0 && self.enc_state != EncodingState::JustEncode) {
340 if let Some(dbuf) = self.frm_pool.get_copy(vbuf) {
341 let newfrm = NAFrame::new(frm.ts, frm.frame_type, frm.key, frm.get_info(), NABufferType::Video(dbuf));
342 if self.enc_state == EncodingState::Intra {
343 for (i, el) in self.mb_map.iter_mut().enumerate() {
346 for el in self.mb_weight.iter_mut() {
349 let frm = NASimpleVideoFrame::from_video_buf(&mut self.i_frame).unwrap();
350 let src = vbuf.get_data();
352 let soff = vbuf.get_offset(plane);
353 let sstride = vbuf.get_stride(plane);
354 let copy_len = sstride.min(frm.stride[plane]);
355 for (dst, src) in frm.data[frm.offset[plane]..].chunks_mut(frm.stride[plane]).zip(src[soff..].chunks(sstride)).take(frm.height[plane]) {
356 dst[..copy_len].copy_from_slice(&src[..copy_len]);
359 self.enc_state = EncodingState::Refinement;
361 self.fenc.set_me_params(self.me_mode, self.me_range, self.version);
362 self.fenc.load_frame(vbuf);
363 self.fenc.mb_tree_search(self.i_frame.clone(), &self.mb_map, &mut self.mb_map2, &mut self.mb_weight);
364 std::mem::swap(&mut self.mb_map, &mut self.mb_map2);
366 self.qframes.push(newfrm);
369 self.enc_state = EncodingState::JustEncode;
370 self.encode_frame(frm)
373 self.encode_frame(frm)
376 Err(EncoderError::FormatError)
379 fn get_packet(&mut self) -> EncoderResult<Option<NAPacket>> {
380 let mbt_frames = self.mbt_depth.min(self.key_int as usize);
381 if self.qframes.len() >= mbt_frames {
382 self.enc_state = EncodingState::JustEncode;
384 if self.pkt.is_none() && !self.qframes.is_empty() && self.enc_state == EncodingState::JustEncode {
385 let frm = self.qframes.remove(0);
386 self.encode_frame(&frm)?;
387 if self.qframes.is_empty() && self.mbt_depth > 0 && self.frmcount == 0 {
388 self.enc_state = EncodingState::Intra;
392 std::mem::swap(&mut self.pkt, &mut npkt);
395 fn flush(&mut self) -> EncoderResult<()> {
397 self.enc_state = EncodingState::JustEncode;
402 const VERSION_OPTION: &str = "version";
403 const LF_LEVEL_OPTION: &str = "lf_level";
404 const LF_SHARP_OPTION: &str = "lf_sharpness";
405 const LF_SIMPLE_OPTION: &str = "lf_simple";
406 const QUANT_OPTION: &str = "quant";
407 const MV_SEARCH_OPTION: &str = "mv_mode";
408 const MV_RANGE_OPTION: &str = "mv_range";
409 const MBTREE_DEPTH: &str = "mbtree_depth";
411 const ENCODER_OPTS: &[NAOptionDefinition] = &[
413 name: KEYFRAME_OPTION, description: KEYFRAME_OPTION_DESC,
414 opt_type: NAOptionDefinitionType::Int(Some(0), Some(128)) },
416 name: VERSION_OPTION, description: "internal codec version",
417 opt_type: NAOptionDefinitionType::Int(Some(0), Some(1)) },
419 name: LF_LEVEL_OPTION, description: "loop filter level (-1 = automatic)",
420 opt_type: NAOptionDefinitionType::Int(Some(-1), Some(63)) },
422 name: LF_SHARP_OPTION, description: "loop filter sharpness",
423 opt_type: NAOptionDefinitionType::Int(Some(0), Some(7)) },
425 name: LF_SIMPLE_OPTION, description: "use simple loop filter",
426 opt_type: NAOptionDefinitionType::Bool },
428 name: QUANT_OPTION, description: "force fixed quantiser for encoding",
429 opt_type: NAOptionDefinitionType::Int(Some(-1), Some(127)) },
431 name: MV_SEARCH_OPTION, description: "motion search mode",
432 opt_type: NAOptionDefinitionType::String(Some(&["sea", "dia", "hex", "epzs"])) },
434 name: MV_RANGE_OPTION, description: "motion search range (in pixels)",
435 opt_type: NAOptionDefinitionType::Int(Some(0), Some(30)) },
437 name: MBTREE_DEPTH, description: "number of frames in MB tree analysis buffer",
438 opt_type: NAOptionDefinitionType::Int(Some(0), Some(128)) },
441 impl NAOptionHandler for VP7Encoder {
442 fn get_supported_options(&self) -> &[NAOptionDefinition] { ENCODER_OPTS }
443 fn set_options(&mut self, options: &[NAOption]) {
444 for option in options.iter() {
445 for opt_def in ENCODER_OPTS.iter() {
446 if opt_def.check(option).is_ok() {
449 if let NAValue::Int(intval) = option.value {
450 self.key_int = intval as u8;
454 if let NAValue::Int(intval) = option.value {
455 self.version = intval as u8;
459 if let NAValue::Int(intval) = option.value {
460 self.lf_level = if intval < 0 { None } else { Some(intval as u8) };
464 if let NAValue::Int(intval) = option.value {
465 self.fenc.loop_params.loop_sharpness = intval as u8;
468 LF_SIMPLE_OPTION => {
469 if let NAValue::Bool(flag) = option.value {
470 self.fenc.loop_params.lf_simple = flag;
474 if let NAValue::Int(intval) = option.value {
475 self.br_ctl.set_quant(if intval < 0 { None } else { Some(intval as usize) });
478 MV_SEARCH_OPTION => {
479 if let NAValue::String(ref string) = option.value {
480 if let Ok(mv_mode) = string.parse::<MVSearchMode>() {
481 self.me_mode = mv_mode;
486 if let NAValue::Int(intval) = option.value {
487 self.me_range = intval as i16;
491 if let NAValue::Int(intval) = option.value {
492 self.mbt_depth = intval as usize;
501 fn query_option_value(&self, name: &str) -> Option<NAValue> {
503 KEYFRAME_OPTION => Some(NAValue::Int(i64::from(self.key_int))),
504 VERSION_OPTION => Some(NAValue::Int(i64::from(self.version))),
505 QUANT_OPTION => if let Some(q) = self.br_ctl.get_quant() {
506 Some(NAValue::Int(q as i64))
508 Some(NAValue::Int(-1))
510 LF_LEVEL_OPTION => if let Some(lev) = self.lf_level {
511 Some(NAValue::Int(i64::from(lev)))
513 Some(NAValue::Int(-1))
515 LF_SHARP_OPTION => Some(NAValue::Int(i64::from(self.fenc.loop_params.loop_sharpness))),
516 LF_SIMPLE_OPTION => Some(NAValue::Bool(self.fenc.loop_params.lf_simple)),
517 MV_SEARCH_OPTION => Some(NAValue::String(self.me_mode.to_string())),
518 MV_RANGE_OPTION => Some(NAValue::Int(i64::from(self.me_range))),
519 MBTREE_DEPTH => Some(NAValue::Int(self.mbt_depth as i64)),
525 pub fn get_encoder() -> Box<dyn NAEncoder + Send> {
526 Box::new(VP7Encoder::new())
531 use nihav_core::codecs::*;
532 use nihav_core::demuxers::*;
533 use nihav_core::muxers::*;
535 use nihav_commonfmt::*;
536 use nihav_codec_support::test::enc_video::*;
538 fn encode_test(out_name: &'static str, enc_options: &[NAOption], hash: &[u32; 4]) {
539 let mut dmx_reg = RegisteredDemuxers::new();
540 generic_register_all_demuxers(&mut dmx_reg);
541 let mut dec_reg = RegisteredDecoders::new();
542 duck_register_all_decoders(&mut dec_reg);
543 let mut mux_reg = RegisteredMuxers::new();
544 generic_register_all_muxers(&mut mux_reg);
545 let mut enc_reg = RegisteredEncoders::new();
546 duck_register_all_encoders(&mut enc_reg);
548 // sample: https://samples.mplayerhq.hu/V-codecs/VP4/ot171_vp40.avi
549 let dec_config = DecoderTestParams {
551 in_name: "assets/Duck/ot171_vp40.avi",
552 stream_type: StreamType::Video,
556 let enc_config = EncoderTestParams {
562 let dst_vinfo = NAVideoInfo {
565 format: YUV420_FORMAT,
569 let enc_params = EncodeParameters {
570 format: NACodecTypeInfo::Video(dst_vinfo),
577 //test_encoding_to_file(&dec_config, &enc_config, enc_params, enc_options);
578 test_encoding_md5(&dec_config, &enc_config, enc_params, enc_options,
582 fn test_vp7_encoder() {
584 NAOption { name: super::QUANT_OPTION, value: NAValue::Int(42) },
586 encode_test("vp7-q42.avi", enc_options, &[0xa5079e5b, 0x33dd8a63, 0xfc189e21, 0xee08332b]);
589 fn test_vp7_encoder_noloop() {
591 NAOption { name: super::QUANT_OPTION, value: NAValue::Int(42) },
592 NAOption { name: super::LF_LEVEL_OPTION, value: NAValue::Int(0) },
594 encode_test("vp7-noloop.avi", enc_options, &[0xc7d41732, 0x09b03059, 0x8550921c, 0xa99d4c29]);
597 fn test_vp7_encoder_mbtree() {
599 NAOption { name: super::QUANT_OPTION, value: NAValue::Int(24) },
600 NAOption { name: super::MBTREE_DEPTH, value: NAValue::Int(10) },
602 encode_test("vp7-mbt.avi", enc_options, &[0xd0d90d31, 0x0253275d, 0xbe502d3c, 0xacf2b6e7]);
605 fn test_vp7_encoder_ratectl() {
607 NAOption { name: super::QUANT_OPTION, value: NAValue::Int(-1) },
609 encode_test("vp7-br.avi", enc_options, &[0x47dcd4da, 0x04b06feb, 0x386163c1, 0x54899da3]);