Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 */
11 #include <limits.h>
13 #include "vpx_mem/vpx_mem.h"
14 #include "vp9/encoder/vp9_encodeintra.h"
15 #include "vp9/encoder/vp9_rdopt.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 #include "vp9/common/vp9_systemdependent.h"
25 static unsigned int do_16x16_motion_iteration(VP9_COMP *cpi,
26 int_mv *ref_mv,
27 int_mv *dst_mv,
28 int mb_row,
29 int mb_col) {
30 MACROBLOCK *const x = &cpi->mb;
31 MACROBLOCKD *const xd = &x->e_mbd;
32 vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
33 unsigned int best_err;
35 const int tmp_col_min = x->mv_col_min;
36 const int tmp_col_max = x->mv_col_max;
37 const int tmp_row_min = x->mv_row_min;
38 const int tmp_row_max = x->mv_row_max;
39 int_mv ref_full;
41 // Further step/diamond searches as necessary
42 int step_param = cpi->sf.reduce_first_step_size +
43 (cpi->speed < 8 ? (cpi->speed > 5 ? 1 : 0) : 2);
44 step_param = MIN(step_param, (cpi->sf.max_step_search_steps - 2));
46 vp9_clamp_mv_min_max(x, &ref_mv->as_mv);
48 ref_full.as_mv.col = ref_mv->as_mv.col >> 3;
49 ref_full.as_mv.row = ref_mv->as_mv.row >> 3;
51 /*cpi->sf.search_method == HEX*/
52 best_err = vp9_hex_search(x, &ref_full.as_mv, step_param, x->errorperbit,
53 0, &v_fn_ptr,
54 0, &ref_mv->as_mv, &dst_mv->as_mv);
56 // Try sub-pixel MC
57 // if (bestsme > error_thresh && bestsme < INT_MAX)
58 {
59 int distortion;
60 unsigned int sse;
61 best_err = cpi->find_fractional_mv_step(
62 x,
63 &dst_mv->as_mv, &ref_mv->as_mv,
64 cpi->common.allow_high_precision_mv,
65 x->errorperbit, &v_fn_ptr,
66 0, cpi->sf.subpel_iters_per_step, NULL, NULL,
67 & distortion, &sse);
68 }
70 vp9_set_mbmode_and_mvs(x, NEWMV, dst_mv);
71 vp9_build_inter_predictors_sby(xd, mb_row, mb_col, BLOCK_16X16);
72 best_err = vp9_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
73 xd->plane[0].dst.buf, xd->plane[0].dst.stride,
74 INT_MAX);
76 /* restore UMV window */
77 x->mv_col_min = tmp_col_min;
78 x->mv_col_max = tmp_col_max;
79 x->mv_row_min = tmp_row_min;
80 x->mv_row_max = tmp_row_max;
82 return best_err;
83 }
85 static int do_16x16_motion_search(VP9_COMP *cpi, int_mv *ref_mv, int_mv *dst_mv,
86 int mb_row, int mb_col) {
87 MACROBLOCK *const x = &cpi->mb;
88 MACROBLOCKD *const xd = &x->e_mbd;
89 unsigned int err, tmp_err;
90 int_mv tmp_mv;
92 // Try zero MV first
93 // FIXME should really use something like near/nearest MV and/or MV prediction
94 err = vp9_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
95 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride,
96 INT_MAX);
97 dst_mv->as_int = 0;
99 // Test last reference frame using the previous best mv as the
100 // starting point (best reference) for the search
101 tmp_err = do_16x16_motion_iteration(cpi, ref_mv, &tmp_mv, mb_row, mb_col);
102 if (tmp_err < err) {
103 err = tmp_err;
104 dst_mv->as_int = tmp_mv.as_int;
105 }
107 // If the current best reference mv is not centered on 0,0 then do a 0,0
108 // based search as well.
109 if (ref_mv->as_int) {
110 unsigned int tmp_err;
111 int_mv zero_ref_mv, tmp_mv;
113 zero_ref_mv.as_int = 0;
114 tmp_err = do_16x16_motion_iteration(cpi, &zero_ref_mv, &tmp_mv,
115 mb_row, mb_col);
116 if (tmp_err < err) {
117 dst_mv->as_int = tmp_mv.as_int;
118 err = tmp_err;
119 }
120 }
122 return err;
123 }
125 static int do_16x16_zerozero_search(VP9_COMP *cpi, int_mv *dst_mv) {
126 MACROBLOCK *const x = &cpi->mb;
127 MACROBLOCKD *const xd = &x->e_mbd;
128 unsigned int err;
130 // Try zero MV first
131 // FIXME should really use something like near/nearest MV and/or MV prediction
132 err = vp9_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
133 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride,
134 INT_MAX);
136 dst_mv->as_int = 0;
138 return err;
139 }
140 static int find_best_16x16_intra(VP9_COMP *cpi,
141 int mb_y_offset,
142 MB_PREDICTION_MODE *pbest_mode) {
143 MACROBLOCK *const x = &cpi->mb;
144 MACROBLOCKD *const xd = &x->e_mbd;
145 MB_PREDICTION_MODE best_mode = -1, mode;
146 unsigned int best_err = INT_MAX;
148 // calculate SATD for each intra prediction mode;
149 // we're intentionally not doing 4x4, we just want a rough estimate
150 for (mode = DC_PRED; mode <= TM_PRED; mode++) {
151 unsigned int err;
153 xd->mi_8x8[0]->mbmi.mode = mode;
154 vp9_predict_intra_block(xd, 0, 2, TX_16X16, mode,
155 x->plane[0].src.buf, x->plane[0].src.stride,
156 xd->plane[0].dst.buf, xd->plane[0].dst.stride);
157 err = vp9_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
158 xd->plane[0].dst.buf, xd->plane[0].dst.stride, best_err);
160 // find best
161 if (err < best_err) {
162 best_err = err;
163 best_mode = mode;
164 }
165 }
167 if (pbest_mode)
168 *pbest_mode = best_mode;
170 return best_err;
171 }
173 static void update_mbgraph_mb_stats
174 (
175 VP9_COMP *cpi,
176 MBGRAPH_MB_STATS *stats,
177 YV12_BUFFER_CONFIG *buf,
178 int mb_y_offset,
179 YV12_BUFFER_CONFIG *golden_ref,
180 int_mv *prev_golden_ref_mv,
181 int gld_y_offset,
182 YV12_BUFFER_CONFIG *alt_ref,
183 int_mv *prev_alt_ref_mv,
184 int arf_y_offset,
185 int mb_row,
186 int mb_col
187 ) {
188 MACROBLOCK *const x = &cpi->mb;
189 MACROBLOCKD *const xd = &x->e_mbd;
190 int intra_error;
191 VP9_COMMON *cm = &cpi->common;
193 // FIXME in practice we're completely ignoring chroma here
194 x->plane[0].src.buf = buf->y_buffer + mb_y_offset;
195 x->plane[0].src.stride = buf->y_stride;
197 xd->plane[0].dst.buf = get_frame_new_buffer(cm)->y_buffer + mb_y_offset;
198 xd->plane[0].dst.stride = get_frame_new_buffer(cm)->y_stride;
200 // do intra 16x16 prediction
201 intra_error = find_best_16x16_intra(cpi, mb_y_offset,
202 &stats->ref[INTRA_FRAME].m.mode);
203 if (intra_error <= 0)
204 intra_error = 1;
205 stats->ref[INTRA_FRAME].err = intra_error;
207 // Golden frame MV search, if it exists and is different than last frame
208 if (golden_ref) {
209 int g_motion_error;
210 xd->plane[0].pre[0].buf = golden_ref->y_buffer + mb_y_offset;
211 xd->plane[0].pre[0].stride = golden_ref->y_stride;
212 g_motion_error = do_16x16_motion_search(cpi,
213 prev_golden_ref_mv,
214 &stats->ref[GOLDEN_FRAME].m.mv,
215 mb_row, mb_col);
216 stats->ref[GOLDEN_FRAME].err = g_motion_error;
217 } else {
218 stats->ref[GOLDEN_FRAME].err = INT_MAX;
219 stats->ref[GOLDEN_FRAME].m.mv.as_int = 0;
220 }
222 // Do an Alt-ref frame MV search, if it exists and is different than
223 // last/golden frame.
224 if (alt_ref) {
225 int a_motion_error;
226 xd->plane[0].pre[0].buf = alt_ref->y_buffer + mb_y_offset;
227 xd->plane[0].pre[0].stride = alt_ref->y_stride;
228 a_motion_error = do_16x16_zerozero_search(cpi,
229 &stats->ref[ALTREF_FRAME].m.mv);
231 stats->ref[ALTREF_FRAME].err = a_motion_error;
232 } else {
233 stats->ref[ALTREF_FRAME].err = INT_MAX;
234 stats->ref[ALTREF_FRAME].m.mv.as_int = 0;
235 }
236 }
238 static void update_mbgraph_frame_stats(VP9_COMP *cpi,
239 MBGRAPH_FRAME_STATS *stats,
240 YV12_BUFFER_CONFIG *buf,
241 YV12_BUFFER_CONFIG *golden_ref,
242 YV12_BUFFER_CONFIG *alt_ref) {
243 MACROBLOCK *const x = &cpi->mb;
244 MACROBLOCKD *const xd = &x->e_mbd;
245 VP9_COMMON *const cm = &cpi->common;
247 int mb_col, mb_row, offset = 0;
248 int mb_y_offset = 0, arf_y_offset = 0, gld_y_offset = 0;
249 int_mv arf_top_mv, gld_top_mv;
250 MODE_INFO mi_local = { { 0 } };
252 // Set up limit values for motion vectors to prevent them extending outside
253 // the UMV borders.
254 arf_top_mv.as_int = 0;
255 gld_top_mv.as_int = 0;
256 x->mv_row_min = -BORDER_MV_PIXELS_B16;
257 x->mv_row_max = (cm->mb_rows - 1) * 8 + BORDER_MV_PIXELS_B16;
258 xd->up_available = 0;
259 xd->plane[0].dst.stride = buf->y_stride;
260 xd->plane[0].pre[0].stride = buf->y_stride;
261 xd->plane[1].dst.stride = buf->uv_stride;
262 xd->mi_8x8[0] = &mi_local;
263 mi_local.mbmi.sb_type = BLOCK_16X16;
264 mi_local.mbmi.ref_frame[0] = LAST_FRAME;
265 mi_local.mbmi.ref_frame[1] = NONE;
267 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
268 int_mv arf_left_mv, gld_left_mv;
269 int mb_y_in_offset = mb_y_offset;
270 int arf_y_in_offset = arf_y_offset;
271 int gld_y_in_offset = gld_y_offset;
273 // Set up limit values for motion vectors to prevent them extending outside
274 // the UMV borders.
275 arf_left_mv.as_int = arf_top_mv.as_int;
276 gld_left_mv.as_int = gld_top_mv.as_int;
277 x->mv_col_min = -BORDER_MV_PIXELS_B16;
278 x->mv_col_max = (cm->mb_cols - 1) * 8 + BORDER_MV_PIXELS_B16;
279 xd->left_available = 0;
281 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
282 MBGRAPH_MB_STATS *mb_stats = &stats->mb_stats[offset + mb_col];
284 update_mbgraph_mb_stats(cpi, mb_stats, buf, mb_y_in_offset,
285 golden_ref, &gld_left_mv, gld_y_in_offset,
286 alt_ref, &arf_left_mv, arf_y_in_offset,
287 mb_row, mb_col);
288 arf_left_mv.as_int = mb_stats->ref[ALTREF_FRAME].m.mv.as_int;
289 gld_left_mv.as_int = mb_stats->ref[GOLDEN_FRAME].m.mv.as_int;
290 if (mb_col == 0) {
291 arf_top_mv.as_int = arf_left_mv.as_int;
292 gld_top_mv.as_int = gld_left_mv.as_int;
293 }
294 xd->left_available = 1;
295 mb_y_in_offset += 16;
296 gld_y_in_offset += 16;
297 arf_y_in_offset += 16;
298 x->mv_col_min -= 16;
299 x->mv_col_max -= 16;
300 }
301 xd->up_available = 1;
302 mb_y_offset += buf->y_stride * 16;
303 gld_y_offset += golden_ref->y_stride * 16;
304 if (alt_ref)
305 arf_y_offset += alt_ref->y_stride * 16;
306 x->mv_row_min -= 16;
307 x->mv_row_max -= 16;
308 offset += cm->mb_cols;
309 }
310 }
312 // void separate_arf_mbs_byzz
313 static void separate_arf_mbs(VP9_COMP *cpi) {
314 VP9_COMMON *const cm = &cpi->common;
315 int mb_col, mb_row, offset, i;
316 int mi_row, mi_col;
317 int ncnt[4] = { 0 };
318 int n_frames = cpi->mbgraph_n_frames;
320 int *arf_not_zz;
322 CHECK_MEM_ERROR(cm, arf_not_zz,
323 vpx_calloc(cm->mb_rows * cm->mb_cols * sizeof(*arf_not_zz),
324 1));
326 // We are not interested in results beyond the alt ref itself.
327 if (n_frames > cpi->frames_till_gf_update_due)
328 n_frames = cpi->frames_till_gf_update_due;
330 // defer cost to reference frames
331 for (i = n_frames - 1; i >= 0; i--) {
332 MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
334 for (offset = 0, mb_row = 0; mb_row < cm->mb_rows;
335 offset += cm->mb_cols, mb_row++) {
336 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
337 MBGRAPH_MB_STATS *mb_stats = &frame_stats->mb_stats[offset + mb_col];
339 int altref_err = mb_stats->ref[ALTREF_FRAME].err;
340 int intra_err = mb_stats->ref[INTRA_FRAME ].err;
341 int golden_err = mb_stats->ref[GOLDEN_FRAME].err;
343 // Test for altref vs intra and gf and that its mv was 0,0.
344 if (altref_err > 1000 ||
345 altref_err > intra_err ||
346 altref_err > golden_err) {
347 arf_not_zz[offset + mb_col]++;
348 }
349 }
350 }
351 }
353 // arf_not_zz is indexed by MB, but this loop is indexed by MI to avoid out
354 // of bound access in segmentation_map
355 for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
356 for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
357 // If any of the blocks in the sequence failed then the MB
358 // goes in segment 0
359 if (arf_not_zz[mi_row/2*cm->mb_cols + mi_col/2]) {
360 ncnt[0]++;
361 cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 0;
362 } else {
363 cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 1;
364 ncnt[1]++;
365 }
366 }
367 }
369 // Only bother with segmentation if over 10% of the MBs in static segment
370 // if ( ncnt[1] && (ncnt[0] / ncnt[1] < 10) )
371 if (1) {
372 // Note % of blocks that are marked as static
373 if (cm->MBs)
374 cpi->static_mb_pct = (ncnt[1] * 100) / (cm->mi_rows * cm->mi_cols);
376 // This error case should not be reachable as this function should
377 // never be called with the common data structure uninitialized.
378 else
379 cpi->static_mb_pct = 0;
381 cpi->seg0_cnt = ncnt[0];
382 vp9_enable_segmentation((VP9_PTR)cpi);
383 } else {
384 cpi->static_mb_pct = 0;
385 vp9_disable_segmentation((VP9_PTR)cpi);
386 }
388 // Free localy allocated storage
389 vpx_free(arf_not_zz);
390 }
392 void vp9_update_mbgraph_stats(VP9_COMP *cpi) {
393 VP9_COMMON *const cm = &cpi->common;
394 int i, n_frames = vp9_lookahead_depth(cpi->lookahead);
395 YV12_BUFFER_CONFIG *golden_ref =
396 &cm->yv12_fb[cm->ref_frame_map[cpi->gld_fb_idx]];
398 // we need to look ahead beyond where the ARF transitions into
399 // being a GF - so exit if we don't look ahead beyond that
400 if (n_frames <= cpi->frames_till_gf_update_due)
401 return;
402 if (n_frames > (int)cpi->frames_till_alt_ref_frame)
403 n_frames = cpi->frames_till_alt_ref_frame;
404 if (n_frames > MAX_LAG_BUFFERS)
405 n_frames = MAX_LAG_BUFFERS;
407 cpi->mbgraph_n_frames = n_frames;
408 for (i = 0; i < n_frames; i++) {
409 MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
410 vpx_memset(frame_stats->mb_stats, 0,
411 cm->mb_rows * cm->mb_cols *
412 sizeof(*cpi->mbgraph_stats[i].mb_stats));
413 }
415 // do motion search to find contribution of each reference to data
416 // later on in this GF group
417 // FIXME really, the GF/last MC search should be done forward, and
418 // the ARF MC search backwards, to get optimal results for MV caching
419 for (i = 0; i < n_frames; i++) {
420 MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
421 struct lookahead_entry *q_cur = vp9_lookahead_peek(cpi->lookahead, i);
423 assert(q_cur != NULL);
425 update_mbgraph_frame_stats(cpi, frame_stats, &q_cur->img,
426 golden_ref, cpi->Source);
427 }
429 vp9_clear_system_state(); // __asm emms;
431 separate_arf_mbs(cpi);
432 }