vp9_mbgraph.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <limits.h>
  11. #include "./vp9_rtcd.h"
  12. #include "./vpx_dsp_rtcd.h"
  13. #include "vpx_dsp/vpx_dsp_common.h"
  14. #include "vpx_mem/vpx_mem.h"
  15. #include "vpx_ports/system_state.h"
  16. #include "vp9/encoder/vp9_segmentation.h"
  17. #include "vp9/encoder/vp9_mcomp.h"
  18. #include "vp9/common/vp9_blockd.h"
  19. #include "vp9/common/vp9_reconinter.h"
  20. #include "vp9/common/vp9_reconintra.h"
  21. static unsigned int do_16x16_motion_iteration(VP9_COMP *cpi, const MV *ref_mv,
  22. MV *dst_mv, int mb_row,
  23. int mb_col) {
  24. MACROBLOCK *const x = &cpi->td.mb;
  25. MACROBLOCKD *const xd = &x->e_mbd;
  26. MV_SPEED_FEATURES *const mv_sf = &cpi->sf.mv;
  27. const SEARCH_METHODS old_search_method = mv_sf->search_method;
  28. const vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
  29. const MvLimits tmp_mv_limits = x->mv_limits;
  30. MV ref_full;
  31. int cost_list[5];
  32. // Further step/diamond searches as necessary
  33. int step_param = mv_sf->reduce_first_step_size;
  34. step_param = VPXMIN(step_param, MAX_MVSEARCH_STEPS - 2);
  35. vp9_set_mv_search_range(&x->mv_limits, ref_mv);
  36. ref_full.col = ref_mv->col >> 3;
  37. ref_full.row = ref_mv->row >> 3;
  38. mv_sf->search_method = HEX;
  39. vp9_full_pixel_search(cpi, x, BLOCK_16X16, &ref_full, step_param,
  40. x->errorperbit, cond_cost_list(cpi, cost_list), ref_mv,
  41. dst_mv, 0, 0);
  42. mv_sf->search_method = old_search_method;
  43. // Try sub-pixel MC
  44. // if (bestsme > error_thresh && bestsme < INT_MAX)
  45. {
  46. uint32_t distortion;
  47. uint32_t sse;
  48. cpi->find_fractional_mv_step(
  49. x, dst_mv, ref_mv, cpi->common.allow_high_precision_mv, x->errorperbit,
  50. &v_fn_ptr, 0, mv_sf->subpel_iters_per_step,
  51. cond_cost_list(cpi, cost_list), NULL, NULL, &distortion, &sse, NULL, 0,
  52. 0);
  53. }
  54. xd->mi[0]->mode = NEWMV;
  55. xd->mi[0]->mv[0].as_mv = *dst_mv;
  56. vp9_build_inter_predictors_sby(xd, mb_row, mb_col, BLOCK_16X16);
  57. /* restore UMV window */
  58. x->mv_limits = tmp_mv_limits;
  59. return vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
  60. xd->plane[0].dst.buf, xd->plane[0].dst.stride);
  61. }
  62. static int do_16x16_motion_search(VP9_COMP *cpi, const MV *ref_mv,
  63. int_mv *dst_mv, int mb_row, int mb_col) {
  64. MACROBLOCK *const x = &cpi->td.mb;
  65. MACROBLOCKD *const xd = &x->e_mbd;
  66. unsigned int err, tmp_err;
  67. MV tmp_mv;
  68. // Try zero MV first
  69. // FIXME should really use something like near/nearest MV and/or MV prediction
  70. err = vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
  71. xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride);
  72. dst_mv->as_int = 0;
  73. // Test last reference frame using the previous best mv as the
  74. // starting point (best reference) for the search
  75. tmp_err = do_16x16_motion_iteration(cpi, ref_mv, &tmp_mv, mb_row, mb_col);
  76. if (tmp_err < err) {
  77. err = tmp_err;
  78. dst_mv->as_mv = tmp_mv;
  79. }
  80. // If the current best reference mv is not centered on 0,0 then do a 0,0
  81. // based search as well.
  82. if (ref_mv->row != 0 || ref_mv->col != 0) {
  83. unsigned int tmp_err;
  84. MV zero_ref_mv = { 0, 0 }, tmp_mv;
  85. tmp_err =
  86. do_16x16_motion_iteration(cpi, &zero_ref_mv, &tmp_mv, mb_row, mb_col);
  87. if (tmp_err < err) {
  88. dst_mv->as_mv = tmp_mv;
  89. err = tmp_err;
  90. }
  91. }
  92. return err;
  93. }
  94. static int do_16x16_zerozero_search(VP9_COMP *cpi, int_mv *dst_mv) {
  95. MACROBLOCK *const x = &cpi->td.mb;
  96. MACROBLOCKD *const xd = &x->e_mbd;
  97. unsigned int err;
  98. // Try zero MV first
  99. // FIXME should really use something like near/nearest MV and/or MV prediction
  100. err = vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
  101. xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride);
  102. dst_mv->as_int = 0;
  103. return err;
  104. }
  105. static int find_best_16x16_intra(VP9_COMP *cpi, PREDICTION_MODE *pbest_mode) {
  106. MACROBLOCK *const x = &cpi->td.mb;
  107. MACROBLOCKD *const xd = &x->e_mbd;
  108. PREDICTION_MODE best_mode = -1, mode;
  109. unsigned int best_err = INT_MAX;
  110. // calculate SATD for each intra prediction mode;
  111. // we're intentionally not doing 4x4, we just want a rough estimate
  112. for (mode = DC_PRED; mode <= TM_PRED; mode++) {
  113. unsigned int err;
  114. xd->mi[0]->mode = mode;
  115. vp9_predict_intra_block(xd, 2, TX_16X16, mode, x->plane[0].src.buf,
  116. x->plane[0].src.stride, xd->plane[0].dst.buf,
  117. xd->plane[0].dst.stride, 0, 0, 0);
  118. err = vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
  119. xd->plane[0].dst.buf, xd->plane[0].dst.stride);
  120. // find best
  121. if (err < best_err) {
  122. best_err = err;
  123. best_mode = mode;
  124. }
  125. }
  126. if (pbest_mode) *pbest_mode = best_mode;
  127. return best_err;
  128. }
  129. static void update_mbgraph_mb_stats(VP9_COMP *cpi, MBGRAPH_MB_STATS *stats,
  130. YV12_BUFFER_CONFIG *buf, int mb_y_offset,
  131. YV12_BUFFER_CONFIG *golden_ref,
  132. const MV *prev_golden_ref_mv,
  133. YV12_BUFFER_CONFIG *alt_ref, int mb_row,
  134. int mb_col) {
  135. MACROBLOCK *const x = &cpi->td.mb;
  136. MACROBLOCKD *const xd = &x->e_mbd;
  137. int intra_error;
  138. VP9_COMMON *cm = &cpi->common;
  139. // FIXME in practice we're completely ignoring chroma here
  140. x->plane[0].src.buf = buf->y_buffer + mb_y_offset;
  141. x->plane[0].src.stride = buf->y_stride;
  142. xd->plane[0].dst.buf = get_frame_new_buffer(cm)->y_buffer + mb_y_offset;
  143. xd->plane[0].dst.stride = get_frame_new_buffer(cm)->y_stride;
  144. // do intra 16x16 prediction
  145. intra_error = find_best_16x16_intra(cpi, &stats->ref[INTRA_FRAME].m.mode);
  146. if (intra_error <= 0) intra_error = 1;
  147. stats->ref[INTRA_FRAME].err = intra_error;
  148. // Golden frame MV search, if it exists and is different than last frame
  149. if (golden_ref) {
  150. int g_motion_error;
  151. xd->plane[0].pre[0].buf = golden_ref->y_buffer + mb_y_offset;
  152. xd->plane[0].pre[0].stride = golden_ref->y_stride;
  153. g_motion_error =
  154. do_16x16_motion_search(cpi, prev_golden_ref_mv,
  155. &stats->ref[GOLDEN_FRAME].m.mv, mb_row, mb_col);
  156. stats->ref[GOLDEN_FRAME].err = g_motion_error;
  157. } else {
  158. stats->ref[GOLDEN_FRAME].err = INT_MAX;
  159. stats->ref[GOLDEN_FRAME].m.mv.as_int = 0;
  160. }
  161. // Do an Alt-ref frame MV search, if it exists and is different than
  162. // last/golden frame.
  163. if (alt_ref) {
  164. int a_motion_error;
  165. xd->plane[0].pre[0].buf = alt_ref->y_buffer + mb_y_offset;
  166. xd->plane[0].pre[0].stride = alt_ref->y_stride;
  167. a_motion_error =
  168. do_16x16_zerozero_search(cpi, &stats->ref[ALTREF_FRAME].m.mv);
  169. stats->ref[ALTREF_FRAME].err = a_motion_error;
  170. } else {
  171. stats->ref[ALTREF_FRAME].err = INT_MAX;
  172. stats->ref[ALTREF_FRAME].m.mv.as_int = 0;
  173. }
  174. }
  175. static void update_mbgraph_frame_stats(VP9_COMP *cpi,
  176. MBGRAPH_FRAME_STATS *stats,
  177. YV12_BUFFER_CONFIG *buf,
  178. YV12_BUFFER_CONFIG *golden_ref,
  179. YV12_BUFFER_CONFIG *alt_ref) {
  180. MACROBLOCK *const x = &cpi->td.mb;
  181. MACROBLOCKD *const xd = &x->e_mbd;
  182. VP9_COMMON *const cm = &cpi->common;
  183. int mb_col, mb_row, offset = 0;
  184. int mb_y_offset = 0, arf_y_offset = 0, gld_y_offset = 0;
  185. MV gld_top_mv = { 0, 0 };
  186. MODE_INFO mi_local;
  187. MODE_INFO mi_above, mi_left;
  188. vp9_zero(mi_local);
  189. // Set up limit values for motion vectors to prevent them extending outside
  190. // the UMV borders.
  191. x->mv_limits.row_min = -BORDER_MV_PIXELS_B16;
  192. x->mv_limits.row_max = (cm->mb_rows - 1) * 8 + BORDER_MV_PIXELS_B16;
  193. // Signal to vp9_predict_intra_block() that above is not available
  194. xd->above_mi = NULL;
  195. xd->plane[0].dst.stride = buf->y_stride;
  196. xd->plane[0].pre[0].stride = buf->y_stride;
  197. xd->plane[1].dst.stride = buf->uv_stride;
  198. xd->mi[0] = &mi_local;
  199. mi_local.sb_type = BLOCK_16X16;
  200. mi_local.ref_frame[0] = LAST_FRAME;
  201. mi_local.ref_frame[1] = NONE;
  202. for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
  203. MV gld_left_mv = gld_top_mv;
  204. int mb_y_in_offset = mb_y_offset;
  205. int arf_y_in_offset = arf_y_offset;
  206. int gld_y_in_offset = gld_y_offset;
  207. // Set up limit values for motion vectors to prevent them extending outside
  208. // the UMV borders.
  209. x->mv_limits.col_min = -BORDER_MV_PIXELS_B16;
  210. x->mv_limits.col_max = (cm->mb_cols - 1) * 8 + BORDER_MV_PIXELS_B16;
  211. // Signal to vp9_predict_intra_block() that left is not available
  212. xd->left_mi = NULL;
  213. for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
  214. MBGRAPH_MB_STATS *mb_stats = &stats->mb_stats[offset + mb_col];
  215. update_mbgraph_mb_stats(cpi, mb_stats, buf, mb_y_in_offset, golden_ref,
  216. &gld_left_mv, alt_ref, mb_row, mb_col);
  217. gld_left_mv = mb_stats->ref[GOLDEN_FRAME].m.mv.as_mv;
  218. if (mb_col == 0) {
  219. gld_top_mv = gld_left_mv;
  220. }
  221. // Signal to vp9_predict_intra_block() that left is available
  222. xd->left_mi = &mi_left;
  223. mb_y_in_offset += 16;
  224. gld_y_in_offset += 16;
  225. arf_y_in_offset += 16;
  226. x->mv_limits.col_min -= 16;
  227. x->mv_limits.col_max -= 16;
  228. }
  229. // Signal to vp9_predict_intra_block() that above is available
  230. xd->above_mi = &mi_above;
  231. mb_y_offset += buf->y_stride * 16;
  232. gld_y_offset += golden_ref->y_stride * 16;
  233. if (alt_ref) arf_y_offset += alt_ref->y_stride * 16;
  234. x->mv_limits.row_min -= 16;
  235. x->mv_limits.row_max -= 16;
  236. offset += cm->mb_cols;
  237. }
  238. }
  239. // void separate_arf_mbs_byzz
  240. static void separate_arf_mbs(VP9_COMP *cpi) {
  241. VP9_COMMON *const cm = &cpi->common;
  242. int mb_col, mb_row, offset, i;
  243. int mi_row, mi_col;
  244. int ncnt[4] = { 0 };
  245. int n_frames = cpi->mbgraph_n_frames;
  246. int *arf_not_zz;
  247. CHECK_MEM_ERROR(
  248. cm, arf_not_zz,
  249. vpx_calloc(cm->mb_rows * cm->mb_cols * sizeof(*arf_not_zz), 1));
  250. // We are not interested in results beyond the alt ref itself.
  251. if (n_frames > cpi->rc.frames_till_gf_update_due)
  252. n_frames = cpi->rc.frames_till_gf_update_due;
  253. // defer cost to reference frames
  254. for (i = n_frames - 1; i >= 0; i--) {
  255. MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
  256. for (offset = 0, mb_row = 0; mb_row < cm->mb_rows;
  257. offset += cm->mb_cols, mb_row++) {
  258. for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
  259. MBGRAPH_MB_STATS *mb_stats = &frame_stats->mb_stats[offset + mb_col];
  260. int altref_err = mb_stats->ref[ALTREF_FRAME].err;
  261. int intra_err = mb_stats->ref[INTRA_FRAME].err;
  262. int golden_err = mb_stats->ref[GOLDEN_FRAME].err;
  263. // Test for altref vs intra and gf and that its mv was 0,0.
  264. if (altref_err > 1000 || altref_err > intra_err ||
  265. altref_err > golden_err) {
  266. arf_not_zz[offset + mb_col]++;
  267. }
  268. }
  269. }
  270. }
  271. // arf_not_zz is indexed by MB, but this loop is indexed by MI to avoid out
  272. // of bound access in segmentation_map
  273. for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
  274. for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
  275. // If any of the blocks in the sequence failed then the MB
  276. // goes in segment 0
  277. if (arf_not_zz[mi_row / 2 * cm->mb_cols + mi_col / 2]) {
  278. ncnt[0]++;
  279. cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 0;
  280. } else {
  281. cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 1;
  282. ncnt[1]++;
  283. }
  284. }
  285. }
  286. // Only bother with segmentation if over 10% of the MBs in static segment
  287. // if ( ncnt[1] && (ncnt[0] / ncnt[1] < 10) )
  288. if (1) {
  289. // Note % of blocks that are marked as static
  290. if (cm->MBs)
  291. cpi->static_mb_pct = (ncnt[1] * 100) / (cm->mi_rows * cm->mi_cols);
  292. // This error case should not be reachable as this function should
  293. // never be called with the common data structure uninitialized.
  294. else
  295. cpi->static_mb_pct = 0;
  296. vp9_enable_segmentation(&cm->seg);
  297. } else {
  298. cpi->static_mb_pct = 0;
  299. vp9_disable_segmentation(&cm->seg);
  300. }
  301. // Free localy allocated storage
  302. vpx_free(arf_not_zz);
  303. }
  304. void vp9_update_mbgraph_stats(VP9_COMP *cpi) {
  305. VP9_COMMON *const cm = &cpi->common;
  306. int i, n_frames = vp9_lookahead_depth(cpi->lookahead);
  307. YV12_BUFFER_CONFIG *golden_ref = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
  308. assert(golden_ref != NULL);
  309. // we need to look ahead beyond where the ARF transitions into
  310. // being a GF - so exit if we don't look ahead beyond that
  311. if (n_frames <= cpi->rc.frames_till_gf_update_due) return;
  312. if (n_frames > MAX_LAG_BUFFERS) n_frames = MAX_LAG_BUFFERS;
  313. cpi->mbgraph_n_frames = n_frames;
  314. for (i = 0; i < n_frames; i++) {
  315. MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
  316. memset(frame_stats->mb_stats, 0,
  317. cm->mb_rows * cm->mb_cols * sizeof(*cpi->mbgraph_stats[i].mb_stats));
  318. }
  319. // do motion search to find contribution of each reference to data
  320. // later on in this GF group
  321. // FIXME really, the GF/last MC search should be done forward, and
  322. // the ARF MC search backwards, to get optimal results for MV caching
  323. for (i = 0; i < n_frames; i++) {
  324. MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
  325. struct lookahead_entry *q_cur = vp9_lookahead_peek(cpi->lookahead, i);
  326. assert(q_cur != NULL);
  327. update_mbgraph_frame_stats(cpi, frame_stats, &q_cur->img, golden_ref,
  328. cpi->Source);
  329. }
  330. vpx_clear_system_state();
  331. separate_arf_mbs(cpi);
  332. }