1 use nihav_core::codecs::*;
2 use nihav_codec_support::codecs::ZERO_MV;
3 use super::super::vp78::PredMode;
4 use super::super::vp78dsp::*;
5 use super::super::vp7data::HIGH_EDGE_VAR_THR;
6 use super::super::vp7dsp::*;
9 use super::mb_coding::*;
11 use super::motion_est::*;
14 const MBT_Q_OFFSET: usize = 3;
16 pub struct LoopParams {
17 pub loop_sharpness: u8,
18 pub loop_filter_level: u8,
22 pub struct FrameEncoder {
25 pub loop_params: LoopParams,
27 sblocks: Vec<SrcBlock>,
36 me_mode: MVSearchMode,
38 mc_buf1: NAVideoBufferRef<u8>,
39 mc_buf2: NAVideoBufferRef<u8>,
40 mv_search_last: Box<dyn MVSearch + Send>,
41 mv_search_gold: Box<dyn MVSearch + Send>,
45 pub fn new(mc_buf1: NAVideoBufferRef<u8>, mc_buf2: NAVideoBufferRef<u8>) -> Self {
46 let me_mode = MVSearchMode::default();
59 pctx: PredContext::new(),
61 loop_params: LoopParams {
68 mv_search_last: me_mode.create_search(),
69 mv_search_gold: me_mode.create_search(),
73 pub fn resize(&mut self, mb_w: usize, mb_h: usize) {
77 self.pctx.resize(mb_w, mb_h);
80 self.sblocks.reserve(mb_w * mb_h);
82 self.res.reserve(mb_w * mb_h);
84 self.mbtypes.reserve(mb_w * mb_h);
86 self.recon.reserve(mb_w * mb_h);
87 self.features.clear();
88 self.features.reserve(mb_w * mb_h);
90 pub fn set_me_params(&mut self, me_mode: MVSearchMode, me_range: i16, version: u8) {
91 self.me_range = me_range;
92 if self.me_mode != me_mode {
93 self.me_mode = me_mode;
94 self.mv_search_last = me_mode.create_search();
95 self.mv_search_gold = me_mode.create_search();
97 self.pctx.version = version;
99 pub fn load_frame(&mut self, vbuf: &NAVideoBuffer<u8>) {
100 load_blocks(vbuf, &mut self.sblocks);
103 pub fn mb_tree_search(&mut self, ref_frm: NAVideoBufferRef<u8>, mb_map: &[usize], new_mb_map: &mut [usize], mb_weights: &mut [usize]) {
104 let mut mv_est = MVEstimator::new(ref_frm, self.mc_buf1.clone(), self.me_range);
105 self.mv_search_last.preinit(&mv_est);
107 new_mb_map.copy_from_slice(mb_map);
108 for (mb_y, mb_row) in self.sblocks.chunks(self.mb_w).enumerate() {
109 for (mb_x, blk) in mb_row.iter().enumerate() {
110 let (mv, _) = self.mv_search_last.search_mb(&mut mv_est, blk, mb_x, mb_y);
113 let new_x = ((((mb_x as isize) * 64 + (mv.x as isize) + 32) >> 6).max(0) as usize).min(self.mb_w - 1);
114 let new_y = ((((mb_y as isize) * 64 + (mv.y as isize) + 32) >> 6).max(0) as usize).min(self.mb_h - 1);
115 let nidx = new_x + new_y * self.mb_w;
116 new_mb_map[mb_idx] = mb_map[nidx];
118 mb_weights[new_mb_map[mb_idx]] += 1;
124 pub fn intra_blocks(&mut self, base_q: usize, metric: &RateDistMetric, models: &VP7Models, mbt_map: Option<&[usize]>) {
125 self.mbtypes.clear();
127 self.pctx.reset_intra();
130 self.features.clear();
132 self.has_features = false;
133 if base_q > MBT_Q_OFFSET {
134 if let Some(map) = mbt_map {
135 let sum: usize = map.iter().sum();
136 let size = map.len();
137 let avg = (sum + size / 2) / size;
138 for &val in map.iter() {
140 self.features.push(1);
141 self.has_features = true;
143 self.features.push(0);
147 for _ in 0..(self.mb_w * self.mb_h) {
148 self.features.push(0);
152 for _ in 0..(self.mb_w * self.mb_h) {
153 self.features.push(0);
157 let mut imctx = IntraModePredCtx {
162 ipred_y: IPredContext::default(),
163 ipred_u: IPredContext::default(),
164 ipred_v: IPredContext::default(),
165 pctx: BlockPCtx::default(),
168 for (mb_y, mb_row) in self.sblocks.chunks_mut(self.mb_w).enumerate() {
169 imctx.ipred_y.has_top = mb_y != 0;
170 imctx.ipred_u.has_top = mb_y != 0;
171 imctx.ipred_v.has_top = mb_y != 0;
173 for (mb_x, sblk) in mb_row.iter().enumerate() {
174 self.pctx.fill_ipred(0, mb_x, &mut imctx.ipred_y);
175 self.pctx.fill_ipred(1, mb_x, &mut imctx.ipred_u);
176 self.pctx.fill_ipred(2, mb_x, &mut imctx.ipred_v);
177 self.pctx.fill_pctx(mb_x, &mut imctx.pctx);
178 if self.has_features {
179 imctx.q = if self.features[mb_x + mb_y * self.mb_w] != 0 {
180 base_q - MBT_Q_OFFSET
186 let mut res = Residue::new();
187 let mut newblk = SrcBlock::default();
189 imctx.tr = self.pctx.get_ipred_tr(mb_x);
190 let mut mb_type = select_intra_mode(sblk, &mut newblk, &mut res, &imctx, MAX_DIST, MBType::InterNoMV(false, [0;4]));
192 let use_i4 = match mb_type {
193 MBType::Intra(best_ymode, best_cmode) => {
194 sblk.apply_ipred_luma(best_ymode, &imctx.ipred_y, &mut res);
195 newblk.fill_ipred_luma(best_ymode, &imctx.ipred_y);
196 sblk.apply_ipred_chroma(best_cmode, &imctx.ipred_u, &imctx.ipred_v, &mut res);
197 newblk.fill_ipred_chroma(best_cmode, &imctx.ipred_u, &imctx.ipred_v);
201 self.pctx.ymodes.set_mode(mb_x, best_ymode);
205 MBType::Intra4x4(ref i4_modes, ref mut i4ctx, best_cmode) => {
206 sblk.apply_ipred_chroma(best_cmode, &imctx.ipred_u, &imctx.ipred_v, &mut res);
207 newblk.fill_ipred_chroma(best_cmode, &imctx.ipred_u, &imctx.ipred_v);
210 self.pctx.ymodes.set_modes4x4(mb_x, i4_modes, i4ctx);
218 self.pctx.set_nz(mb_x, &res);
219 let mut recon = res.clone();
221 self.mbtypes.push(mb_type);
224 recon.add_residue(&mut newblk);
226 recon.add_residue_chroma(&mut newblk);
229 self.pctx.update_mb(&newblk, mb_x);
230 self.recon.push(newblk);
232 self.pctx.update_mb_row();
235 pub fn inter_blocks(&mut self, q: usize, metric: &RateDistMetric, models: &VP7Models, last_frame: &NABufferType, gold_frame: &NABufferType) {
236 self.has_features = false;
238 let mut mv_est_last = MVEstimator::new(last_frame.get_vbuf().unwrap(), self.mc_buf1.clone(), self.me_range);
239 self.mv_search_last.preinit(&mv_est_last);
240 let mut mv_est_gold = if let Some(gbuf) = gold_frame.get_vbuf() {
241 let mv_est = MVEstimator::new(gbuf, self.mc_buf2.clone(), self.me_range);
242 self.mv_search_gold.preinit(&mv_est);
248 self.mbtypes.clear();
250 self.pctx.save_dc_pred();
253 self.features.clear();
255 let mut imctx = IntraModePredCtx {
260 ipred_y: IPredContext::default(),
261 ipred_u: IPredContext::default(),
262 ipred_v: IPredContext::default(),
263 pctx: BlockPCtx::default(),
266 for (mb_y, mb_row) in self.sblocks.chunks_mut(self.mb_w).enumerate() {
267 imctx.ipred_y.has_top = mb_y != 0;
268 imctx.ipred_u.has_top = mb_y != 0;
269 imctx.ipred_v.has_top = mb_y != 0;
271 for (mb_x, sblk) in mb_row.iter().enumerate() {
272 self.pctx.fill_ipred(0, mb_x, &mut imctx.ipred_y);
273 self.pctx.fill_ipred(1, mb_x, &mut imctx.ipred_u);
274 self.pctx.fill_ipred(2, mb_x, &mut imctx.ipred_v);
275 self.pctx.fill_pctx(mb_x, &mut imctx.pctx);
277 let mut res = Residue::new();
278 let mut newblk = SrcBlock::default();
280 let (mvprobs, nearest_mv, near_mv, pred_mv) = self.pctx.find_mv_pred(mb_x, mb_y);
282 let (mv, _dist) = self.mv_search_last.search_mb(&mut mv_est_last, sblk, mb_x, mb_y);
284 mv_est_last.get_mb(&mut newblk, mb_x, mb_y, mv);
285 let mv_nits_dist = metric.calc_metric(0, inter_mv_nits(mv, &mvprobs, nearest_mv, near_mv, pred_mv, models));
286 let last_dist = calc_inter_mb_dist(sblk, &newblk, &mut res, &imctx, self.pctx.get_y2_dc_pred(true)) + mv_nits_dist;
288 let (gmv, gold_dist) = if last_dist > SMALL_DIST {
289 if let Some(ref mut mv_est) = &mut mv_est_gold {
290 let (gmv, _gdist) = self.mv_search_gold.search_mb(mv_est, sblk, mb_x, mb_y);
291 mv_est.get_mb(&mut newblk, mb_x, mb_y, gmv);
292 let mv_nits_dist = metric.calc_metric(0, inter_mv_nits(gmv, &mvprobs, nearest_mv, near_mv, pred_mv, models));
293 let gdist = calc_inter_mb_dist(sblk, &newblk, &mut res, &imctx, self.pctx.get_y2_dc_pred(false)) + mv_nits_dist;
302 let (last, mut inter_dist, mv, mv_est) = if last_dist < gold_dist {
303 (true, last_dist, mv, &mut mv_est_last)
304 } else if let Some (ref mut mv_est) = &mut mv_est_gold {
305 (false, gold_dist, gmv, mv_est)
310 let mut mb_type = if mv == ZERO_MV {
311 MBType::InterNoMV(last, mvprobs)
312 } else if mv == nearest_mv {
313 MBType::InterNearest(last, mvprobs)
314 } else if mv == near_mv {
315 MBType::InterNear(last, mvprobs)
317 MBType::InterMV(last, mvprobs, mv - pred_mv)
319 if inter_dist > SMALL_DIST {
320 if let MBType::InterMV(_, _, _) = mb_type { // xxx: maybe do it for all types?
321 let mv_search = if last { &mut self.mv_search_last } else { &mut self.mv_search_gold };
322 if let Some((mbt, dist)) = try_inter_split(sblk, &mut newblk, &mut res, mvprobs, nearest_mv, near_mv, pred_mv, last, mb_x, mb_y, mv_search, mv_est, &mut self.pctx, &imctx, inter_dist) {
329 if inter_dist > SMALL_DIST {
330 imctx.tr = self.pctx.get_ipred_tr(mb_x);
331 mb_type = select_intra_mode(sblk, &mut newblk, &mut res, &imctx, inter_dist, mb_type);
334 self.mbtypes.push(mb_type);
337 MBType::Intra(ymode, cmode) => {
338 newblk.fill_ipred_luma(ymode, &imctx.ipred_y);
339 newblk.fill_ipred_chroma(cmode, &imctx.ipred_u, &imctx.ipred_v);
340 self.pctx.ymodes.set_mode(mb_x, ymode);
341 self.pctx.fill_mv(mb_x, mb_y, ZERO_MV);
343 MBType::Intra4x4(ref i4_modes, ref mut i4ctx, cmode) => {
344 newblk.fill_ipred_chroma(cmode, &imctx.ipred_u, &imctx.ipred_v);
345 self.pctx.ymodes.set_modes4x4(mb_x, i4_modes, i4ctx);
346 self.pctx.fill_mv(mb_x, mb_y, ZERO_MV);
348 MBType::InterNoMV(_, _) |
349 MBType::InterNearest(_, _) |
350 MBType::InterNear(_, _) |
351 MBType::InterMV(_, _, _) => {
352 mv_est.get_mb(&mut newblk, mb_x, mb_y, mv);
353 self.pctx.fill_mv(mb_x, mb_y, mv);
354 self.pctx.ymodes.set_mode(mb_x, PredMode::Inter);
356 MBType::InterSplitMV(_, _, _, _, _) => {
357 self.pctx.ymodes.set_mode(mb_x, PredMode::Inter);
358 recon_split_mb(&mut newblk, mb_x, mb_y, &self.pctx.mvs, self.pctx.mv_stride, mv_est);
361 if let MBType::Intra4x4(_, _, _) = mb_type {
362 res.set_chroma_from_diff(&sblk.chroma, &newblk.chroma);
365 res.set_luma_from_diff(&sblk.luma, &newblk.luma);
366 res.set_chroma_from_diff(&sblk.chroma, &newblk.chroma);
369 if !mb_type.is_intra() {
370 requant_y2_dc(&mut res.dcs[0], q);
371 self.pctx.predict_y2_dc(&mut res.dcs[0], last);
376 self.pctx.set_nz(mb_x, &res);
377 let mut recon = res.clone();
379 self.features.push(0);
380 if let MBType::Intra4x4(_, _, _) = mb_type {
381 recon.add_residue_chroma(&mut newblk);
383 recon.add_residue(&mut newblk);
385 self.pctx.update_mb(&newblk, mb_x);
386 self.recon.push(newblk);
388 self.pctx.update_mb_row();
391 pub fn encode_features(&self, bc: &mut BoolEncoder, q: usize, models: &VP7Models) -> EncoderResult<()> {
392 if self.has_features {
393 // first feature - quantiser
394 bc.put_bool(true, 128)?;
395 bc.put_byte(models.feature_present[0])?;
396 for &prob in models.feature_tree_probs[0].iter() {
397 bc.put_bool(prob != 255, 128)?;
402 bc.put_bool(true, 128)?;
403 bc.put_bits((q - MBT_Q_OFFSET) as u32, 7)?;
405 bc.put_bool(false, 128)?; // other quants
410 bc.put_bool(false, 128)?;
414 bc.put_bool(false, 128)?;
419 pub fn encode_mb_types(&self, bc: &mut BoolEncoder, is_intra: bool, models: &VP7Models) -> EncoderResult<()> {
420 for (mb_type, &feature) in self.mbtypes.iter().zip(self.features.iter()) {
421 if self.has_features {
422 bc.encode_feature(0, if feature == 0 { None } else { Some(0) }, models)?;
424 bc.encode_mb_type(is_intra, mb_type, models)?;
428 pub fn encode_residues(&mut self, bc: &mut BoolEncoder, models: &VP7Models) -> EncoderResult<()> {
430 //self.pctx.restore_dc_pred();
431 for (_mb_y, mb_row) in self.res.chunks(self.mb_w).enumerate() {
432 for (mb_x, blk) in mb_row.iter().enumerate() {
434 let pctx = (self.pctx.nz_y2_left as u8) + (self.pctx.nz_y2_top[mb_x] as u8);
435 bc.encode_subblock(&blk.dcs, 1, pctx, models)?;
436 let has_nz = blk.dcs.has_nz();
437 self.pctx.nz_y2_left = has_nz;
438 self.pctx.nz_y2_top[mb_x] = has_nz;
440 let ytype = if blk.has_dc { 0 } else { 3 };
441 for (y, blk_row) in blk.luma.chunks(4).enumerate() {
442 for (x, blk) in blk_row.iter().enumerate() {
443 let pctx = (self.pctx.nz_y_left[y] as u8) + (self.pctx.nz_y_top[mb_x * 4 + x] as u8);
444 bc.encode_subblock(blk, ytype, pctx, models)?;
445 let has_nz = blk.has_nz();
446 self.pctx.nz_y_left[y] = has_nz;
447 self.pctx.nz_y_top[mb_x * 4 + x] = has_nz;
451 for (c, chroma) in blk.chroma.iter().enumerate() {
452 for (y, blk_row) in chroma.chunks(2).enumerate() {
453 for (x, blk) in blk_row.iter().enumerate() {
454 let pctx = (self.pctx.nz_c_left[c][y] as u8) + (self.pctx.nz_c_top[c][mb_x * 2 + x] as u8);
455 bc.encode_subblock(blk, 2, pctx, models)?;
456 let has_nz = blk.has_nz();
457 self.pctx.nz_c_left[c][y] = has_nz;
458 self.pctx.nz_c_top[c][mb_x * 2 + x] = has_nz;
463 self.pctx.update_mb_row();
467 pub fn generate_models(&mut self, is_intra: bool, stats: &mut VP7ModelsStat) {
469 let est = Estimator::new();
471 if self.has_features {
472 for &feat in self.features.iter() {
473 est.estimate_feature(0, if feat == 0 { None } else { Some(0) }, stats);
476 for (mbt_row, mb_row) in self.mbtypes.chunks(self.mb_w).zip(self.res.chunks(self.mb_w)) {
477 for (mb_x, (mbtype, blk)) in mbt_row.iter().zip(mb_row.iter()).enumerate() {
478 est.estimate_mb_type(is_intra, mbtype, stats);
480 let pctx = (self.pctx.nz_y2_left as u8) + (self.pctx.nz_y2_top[mb_x] as u8);
481 est.estimate_subblock(&blk.dcs, 1, pctx, stats);
482 let has_nz = blk.dcs.has_nz();
483 self.pctx.nz_y2_left = has_nz;
484 self.pctx.nz_y2_top[mb_x] = has_nz;
486 let ytype = if blk.has_dc { 0 } else { 3 };
487 for (y, blk_row) in blk.luma.chunks(4).enumerate() {
488 for (x, blk) in blk_row.iter().enumerate() {
489 let pctx = (self.pctx.nz_y_left[y] as u8) + (self.pctx.nz_y_top[mb_x * 4 + x] as u8);
490 est.estimate_subblock(blk, ytype, pctx, stats);
491 let has_nz = blk.has_nz();
492 self.pctx.nz_y_left[y] = has_nz;
493 self.pctx.nz_y_top[mb_x * 4 + x] = has_nz;
497 for (c, chroma) in blk.chroma.iter().enumerate() {
498 for (y, blk_row) in chroma.chunks(2).enumerate() {
499 for (x, blk) in blk_row.iter().enumerate() {
500 let pctx = (self.pctx.nz_c_left[c][y] as u8) + (self.pctx.nz_c_top[c][mb_x * 2 + x] as u8);
501 est.estimate_subblock(blk, 2, pctx, stats);
502 let has_nz = blk.has_nz();
503 self.pctx.nz_c_left[c][y] = has_nz;
504 self.pctx.nz_c_top[c][mb_x * 2 + x] = has_nz;
509 self.pctx.update_mb_row();
512 pub fn reconstruct_frame(&mut self, frm: &mut NASimpleVideoFrame<u8>, is_intra: bool) {
513 let mut yidx = frm.offset[0];
514 let mut uidx = frm.offset[1];
515 let mut vidx = frm.offset[2];
516 let ystride = frm.stride[0];
517 let ustride = frm.stride[1];
518 let vstride = frm.stride[2];
520 for (mb_y, (f_row, mb_row)) in self.features.chunks(self.mb_w).zip(self.recon.chunks(self.mb_w)).enumerate() {
521 for (mb_x, (&feature, sblk)) in f_row.iter().zip(mb_row.iter()).enumerate() {
522 let dst = &mut frm.data[yidx + mb_x * 16..];
523 for (dst, src) in dst.chunks_mut(ystride).zip(sblk.luma.chunks(16)) {
524 dst[..16].copy_from_slice(src);
526 let dst = &mut frm.data[uidx + mb_x * 8..];
527 for (dst, src) in dst.chunks_mut(ustride).zip(sblk.chroma[0].chunks(8)) {
528 dst[..8].copy_from_slice(src);
530 let dst = &mut frm.data[vidx + mb_x * 8..];
531 for (dst, src) in dst.chunks_mut(vstride).zip(sblk.chroma[1].chunks(8)) {
532 dst[..8].copy_from_slice(src);
535 let loop_str = if feature != 2 {
536 self.loop_params.loop_filter_level
538 loop_filter_mb(frm, mb_x, mb_y, loop_str, &self.loop_params, is_intra);
540 yidx += ystride * 16;
547 fn loop_filter_mb(dframe: &mut NASimpleVideoFrame<u8>, mb_x: usize, mb_y: usize, loop_str: u8, loop_params: &LoopParams, is_intra: bool) {
548 let edge_thr = i16::from(loop_str) + 2;
549 let luma_thr = i16::from(loop_str);
550 let chroma_thr = i16::from(loop_str) * 2;
551 let inner_thr = if loop_params.loop_sharpness == 0 {
554 let bound1 = i16::from(9 - loop_params.loop_sharpness);
555 let shift = (loop_params.loop_sharpness + 3) >> 2;
556 (i16::from(loop_str) >> shift).min(bound1)
558 let hev_thr = i16::from(HIGH_EDGE_VAR_THR[if is_intra { 1 } else { 0 }][loop_str as usize]);
560 let ystride = dframe.stride[0];
561 let ustride = dframe.stride[1];
562 let vstride = dframe.stride[2];
563 let ypos = dframe.offset[0] + mb_x * 16 + mb_y * 16 * ystride;
564 let upos = dframe.offset[1] + mb_x * 8 + mb_y * 8 * ustride;
565 let vpos = dframe.offset[2] + mb_x * 8 + mb_y * 8 * vstride;
567 let (loop_edge, loop_inner) = if loop_params.lf_simple {
568 (simple_loop_filter as LoopFilterFunc, simple_loop_filter as LoopFilterFunc)
570 (normal_loop_filter_edge as LoopFilterFunc, normal_loop_filter_inner as LoopFilterFunc)
574 loop_edge(dframe.data, ypos, 1, ystride, 16, edge_thr, inner_thr, hev_thr);
575 loop_edge(dframe.data, upos, 1, ustride, 8, edge_thr, inner_thr, hev_thr);
576 loop_edge(dframe.data, vpos, 1, vstride, 8, edge_thr, inner_thr, hev_thr);
579 loop_edge(dframe.data, ypos, ystride, 1, 16, edge_thr, inner_thr, hev_thr);
580 loop_edge(dframe.data, upos, ustride, 1, 8, edge_thr, inner_thr, hev_thr);
581 loop_edge(dframe.data, vpos, vstride, 1, 8, edge_thr, inner_thr, hev_thr);
585 loop_inner(dframe.data, ypos + y * 4 * ystride, ystride, 1, 16, luma_thr, inner_thr, hev_thr);
587 loop_inner(dframe.data, upos + 4 * ustride, ustride, 1, 8, chroma_thr, inner_thr, hev_thr);
588 loop_inner(dframe.data, vpos + 4 * vstride, vstride, 1, 8, chroma_thr, inner_thr, hev_thr);
591 loop_inner(dframe.data, ypos + x * 4, 1, ystride, 16, luma_thr, inner_thr, hev_thr);
593 loop_inner(dframe.data, upos + 4, 1, ustride, 8, chroma_thr, inner_thr, hev_thr);
594 loop_inner(dframe.data, vpos + 4, 1, vstride, 8, chroma_thr, inner_thr, hev_thr);