/src/libvpx/vp8/encoder/encodeframe.c
Line | Count | Source |
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 <stdio.h> |
12 | | |
13 | | #include "vpx_config.h" |
14 | | |
15 | | #include "vp8/common/common.h" |
16 | | #include "vp8/common/entropymode.h" |
17 | | #include "vp8/common/extend.h" |
18 | | #include "vp8/common/invtrans.h" |
19 | | #include "vp8/common/quant_common.h" |
20 | | #include "vp8/common/reconinter.h" |
21 | | #include "vp8/common/setupintrarecon.h" |
22 | | #include "vp8/common/threading.h" |
23 | | #include "vp8/encoder/bitstream.h" |
24 | | #include "vp8/encoder/encodeframe.h" |
25 | | #include "vp8/encoder/encodeintra.h" |
26 | | #include "vp8/encoder/encodemb.h" |
27 | | #include "vp8/encoder/onyx_int.h" |
28 | | #include "vp8/encoder/pickinter.h" |
29 | | #include "vp8/encoder/rdopt.h" |
30 | | #include "vp8_rtcd.h" |
31 | | #include "vpx/internal/vpx_codec_internal.h" |
32 | | #include "vpx_dsp_rtcd.h" |
33 | | #include "vpx_mem/vpx_mem.h" |
34 | | #include "vpx_ports/vpx_timer.h" |
35 | | |
36 | | #if CONFIG_MULTITHREAD |
37 | | #include "vp8/encoder/ethreading.h" |
38 | | #endif |
39 | | |
40 | | extern void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t); |
41 | | static void adjust_act_zbin(VP8_COMP *cpi, MACROBLOCK *x); |
42 | | |
43 | | #ifdef MODE_STATS |
44 | | unsigned int inter_y_modes[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
45 | | unsigned int inter_uv_modes[4] = { 0, 0, 0, 0 }; |
46 | | unsigned int inter_b_modes[15] = { |
47 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
48 | | }; |
49 | | unsigned int y_modes[5] = { 0, 0, 0, 0, 0 }; |
50 | | unsigned int uv_modes[4] = { 0, 0, 0, 0 }; |
51 | | unsigned int b_modes[14] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
52 | | #endif |
53 | | |
54 | | /* activity_avg must be positive, or flat regions could get a zero weight |
55 | | * (infinite lambda), which confounds analysis. |
56 | | * This also avoids the need for divide by zero checks in |
57 | | * vp8_activity_masking(). |
58 | | */ |
59 | 0 | #define VP8_ACTIVITY_AVG_MIN (64) |
60 | | |
61 | | /* This is used as a reference when computing the source variance for the |
62 | | * purposes of activity masking. |
63 | | * Eventually this should be replaced by custom no-reference routines, |
64 | | * which will be faster. |
65 | | */ |
66 | | static const unsigned char VP8_VAR_OFFS[16] = { 128, 128, 128, 128, 128, 128, |
67 | | 128, 128, 128, 128, 128, 128, |
68 | | 128, 128, 128, 128 }; |
69 | | |
70 | | /* Original activity measure from Tim T's code. */ |
71 | 0 | static unsigned int tt_activity_measure(MACROBLOCK *x) { |
72 | 0 | unsigned int act; |
73 | 0 | unsigned int sse; |
74 | 0 | /* TODO: This could also be done over smaller areas (8x8), but that would |
75 | 0 | * require extensive changes elsewhere, as lambda is assumed to be fixed |
76 | 0 | * over an entire MB in most of the code. |
77 | 0 | * Another option is to compute four 8x8 variances, and pick a single |
78 | 0 | * lambda using a non-linear combination (e.g., the smallest, or second |
79 | 0 | * smallest, etc.). |
80 | 0 | */ |
81 | 0 | act = vpx_variance16x16(x->src.y_buffer, x->src.y_stride, VP8_VAR_OFFS, 0, |
82 | 0 | &sse); |
83 | 0 | act = act << 4; |
84 | 0 |
|
85 | 0 | /* If the region is flat, lower the activity some more. */ |
86 | 0 | if (act < 8 << 12) act = act < 5 << 12 ? act : 5 << 12; |
87 | 0 |
|
88 | 0 | return act; |
89 | 0 | } |
90 | | |
91 | | /* Measure the activity of the current macroblock |
92 | | * What we measure here is TBD so abstracted to this function |
93 | | */ |
94 | 0 | #define ALT_ACT_MEASURE 1 |
95 | 0 | static unsigned int mb_activity_measure(MACROBLOCK *x, int mb_row, int mb_col) { |
96 | 0 | unsigned int mb_activity; |
97 | |
|
98 | 0 | if (ALT_ACT_MEASURE) { |
99 | 0 | int use_dc_pred = (mb_col > 1 && mb_row > 1); |
100 | | /* Or use an alternative. */ |
101 | 0 | mb_activity = vp8_encode_intra(x, use_dc_pred); |
102 | 0 | } else { |
103 | | /* Original activity measure from Tim T's code. */ |
104 | 0 | mb_activity = tt_activity_measure(x); |
105 | 0 | } |
106 | |
|
107 | 0 | if (mb_activity < VP8_ACTIVITY_AVG_MIN) mb_activity = VP8_ACTIVITY_AVG_MIN; |
108 | |
|
109 | 0 | return mb_activity; |
110 | 0 | } |
111 | | |
112 | | /* Calculate an "average" mb activity value for the frame */ |
113 | | #define ACT_MEDIAN 0 |
114 | 0 | static void calc_av_activity(VP8_COMP *cpi, int64_t activity_sum) { |
115 | | #if ACT_MEDIAN |
116 | | /* Find median: Simple n^2 algorithm for experimentation */ |
117 | | { |
118 | | unsigned int median; |
119 | | unsigned int i, j; |
120 | | unsigned int *sortlist; |
121 | | unsigned int tmp; |
122 | | |
123 | | /* Create a list to sort to */ |
124 | | CHECK_MEM_ERROR(&cpi->common.error, sortlist, |
125 | | vpx_calloc(sizeof(unsigned int), cpi->common.MBs)); |
126 | | |
127 | | /* Copy map to sort list */ |
128 | | memcpy(sortlist, cpi->mb_activity_map, |
129 | | sizeof(unsigned int) * cpi->common.MBs); |
130 | | |
131 | | /* Ripple each value down to its correct position */ |
132 | | for (i = 1; i < cpi->common.MBs; ++i) { |
133 | | for (j = i; j > 0; j--) { |
134 | | if (sortlist[j] < sortlist[j - 1]) { |
135 | | /* Swap values */ |
136 | | tmp = sortlist[j - 1]; |
137 | | sortlist[j - 1] = sortlist[j]; |
138 | | sortlist[j] = tmp; |
139 | | } else |
140 | | break; |
141 | | } |
142 | | } |
143 | | |
144 | | /* Even number MBs so estimate median as mean of two either side. */ |
145 | | median = (1 + sortlist[cpi->common.MBs >> 1] + |
146 | | sortlist[(cpi->common.MBs >> 1) + 1]) >> |
147 | | 1; |
148 | | |
149 | | cpi->activity_avg = median; |
150 | | |
151 | | vpx_free(sortlist); |
152 | | } |
153 | | #else |
154 | | /* Simple mean for now */ |
155 | 0 | cpi->activity_avg = (unsigned int)(activity_sum / cpi->common.MBs); |
156 | 0 | #endif |
157 | |
|
158 | 0 | if (cpi->activity_avg < VP8_ACTIVITY_AVG_MIN) { |
159 | 0 | cpi->activity_avg = VP8_ACTIVITY_AVG_MIN; |
160 | 0 | } |
161 | | |
162 | | /* Experimental code: return fixed value normalized for several clips */ |
163 | 0 | if (ALT_ACT_MEASURE) cpi->activity_avg = 100000; |
164 | 0 | } |
165 | | |
166 | | #define USE_ACT_INDEX 0 |
167 | | #define OUTPUT_NORM_ACT_STATS 0 |
168 | | |
169 | | #if USE_ACT_INDEX |
170 | | /* Calculate and activity index for each mb */ |
171 | | static void calc_activity_index(VP8_COMP *cpi, MACROBLOCK *x) { |
172 | | VP8_COMMON *const cm = &cpi->common; |
173 | | int mb_row, mb_col; |
174 | | |
175 | | int64_t act; |
176 | | int64_t a; |
177 | | int64_t b; |
178 | | |
179 | | #if OUTPUT_NORM_ACT_STATS |
180 | | FILE *f = fopen("norm_act.stt", "a"); |
181 | | fprintf(f, "\n%12d\n", cpi->activity_avg); |
182 | | #endif |
183 | | |
184 | | /* Reset pointers to start of activity map */ |
185 | | x->mb_activity_ptr = cpi->mb_activity_map; |
186 | | |
187 | | /* Calculate normalized mb activity number. */ |
188 | | for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) { |
189 | | /* for each macroblock col in image */ |
190 | | for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) { |
191 | | /* Read activity from the map */ |
192 | | act = *(x->mb_activity_ptr); |
193 | | |
194 | | /* Calculate a normalized activity number */ |
195 | | a = act + 4 * cpi->activity_avg; |
196 | | b = 4 * act + cpi->activity_avg; |
197 | | |
198 | | if (b >= a) |
199 | | *(x->activity_ptr) = (int)((b + (a >> 1)) / a) - 1; |
200 | | else |
201 | | *(x->activity_ptr) = 1 - (int)((a + (b >> 1)) / b); |
202 | | |
203 | | #if OUTPUT_NORM_ACT_STATS |
204 | | fprintf(f, " %6d", *(x->mb_activity_ptr)); |
205 | | #endif |
206 | | /* Increment activity map pointers */ |
207 | | x->mb_activity_ptr++; |
208 | | } |
209 | | |
210 | | #if OUTPUT_NORM_ACT_STATS |
211 | | fprintf(f, "\n"); |
212 | | #endif |
213 | | } |
214 | | |
215 | | #if OUTPUT_NORM_ACT_STATS |
216 | | fclose(f); |
217 | | #endif |
218 | | } |
219 | | #endif |
220 | | |
221 | | /* Loop through all MBs. Note activity of each, average activity and |
222 | | * calculate a normalized activity for each |
223 | | */ |
224 | 0 | static void build_activity_map(VP8_COMP *cpi) { |
225 | 0 | MACROBLOCK *const x = &cpi->mb; |
226 | 0 | MACROBLOCKD *xd = &x->e_mbd; |
227 | 0 | VP8_COMMON *const cm = &cpi->common; |
228 | |
|
229 | 0 | #if ALT_ACT_MEASURE |
230 | 0 | YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx]; |
231 | 0 | int recon_yoffset; |
232 | 0 | int recon_y_stride = new_yv12->y_stride; |
233 | 0 | #endif |
234 | |
|
235 | 0 | int mb_row, mb_col; |
236 | 0 | unsigned int mb_activity; |
237 | 0 | int64_t activity_sum = 0; |
238 | | |
239 | | /* for each macroblock row in image */ |
240 | 0 | for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) { |
241 | 0 | #if ALT_ACT_MEASURE |
242 | | /* reset above block coeffs */ |
243 | 0 | xd->up_available = (mb_row != 0); |
244 | 0 | recon_yoffset = (mb_row * recon_y_stride * 16); |
245 | 0 | #endif |
246 | | /* for each macroblock col in image */ |
247 | 0 | for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) { |
248 | 0 | #if ALT_ACT_MEASURE |
249 | 0 | xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset; |
250 | 0 | xd->left_available = (mb_col != 0); |
251 | 0 | recon_yoffset += 16; |
252 | 0 | #endif |
253 | | /* Copy current mb to a buffer */ |
254 | 0 | vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16); |
255 | | |
256 | | /* measure activity */ |
257 | 0 | mb_activity = mb_activity_measure(x, mb_row, mb_col); |
258 | | |
259 | | /* Keep frame sum */ |
260 | 0 | activity_sum += mb_activity; |
261 | | |
262 | | /* Store MB level activity details. */ |
263 | 0 | *x->mb_activity_ptr = mb_activity; |
264 | | |
265 | | /* Increment activity map pointer */ |
266 | 0 | x->mb_activity_ptr++; |
267 | | |
268 | | /* adjust to the next column of source macroblocks */ |
269 | 0 | x->src.y_buffer += 16; |
270 | 0 | } |
271 | | |
272 | | /* adjust to the next row of mbs */ |
273 | 0 | x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols; |
274 | |
|
275 | 0 | #if ALT_ACT_MEASURE |
276 | | /* extend the recon for intra prediction */ |
277 | 0 | vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16, xd->dst.u_buffer + 8, |
278 | 0 | xd->dst.v_buffer + 8); |
279 | 0 | #endif |
280 | 0 | } |
281 | | |
282 | | /* Calculate an "average" MB activity */ |
283 | 0 | calc_av_activity(cpi, activity_sum); |
284 | |
|
285 | | #if USE_ACT_INDEX |
286 | | /* Calculate an activity index number of each mb */ |
287 | | calc_activity_index(cpi, x); |
288 | | #endif |
289 | 0 | } |
290 | | |
291 | | /* Macroblock activity masking */ |
292 | 0 | void vp8_activity_masking(VP8_COMP *cpi, MACROBLOCK *x) { |
293 | | #if USE_ACT_INDEX |
294 | | x->rdmult += *(x->mb_activity_ptr) * (x->rdmult >> 2); |
295 | | x->errorperbit = x->rdmult * 100 / (110 * x->rddiv); |
296 | | x->errorperbit += (x->errorperbit == 0); |
297 | | #else |
298 | 0 | int64_t a; |
299 | 0 | int64_t b; |
300 | 0 | int64_t act = *(x->mb_activity_ptr); |
301 | | |
302 | | /* Apply the masking to the RD multiplier. */ |
303 | 0 | a = act + (2 * cpi->activity_avg); |
304 | 0 | b = (2 * act) + cpi->activity_avg; |
305 | |
|
306 | 0 | x->rdmult = (unsigned int)(((int64_t)x->rdmult * b + (a >> 1)) / a); |
307 | 0 | x->errorperbit = x->rdmult * 100 / (110 * x->rddiv); |
308 | 0 | x->errorperbit += (x->errorperbit == 0); |
309 | 0 | #endif |
310 | | |
311 | | /* Activity based Zbin adjustment */ |
312 | 0 | adjust_act_zbin(cpi, x); |
313 | 0 | } |
314 | | |
315 | | static void encode_mb_row(VP8_COMP *cpi, VP8_COMMON *cm, int mb_row, |
316 | | MACROBLOCK *x, MACROBLOCKD *xd, TOKENEXTRA **tp, |
317 | 549k | int *segment_counts, int *totalrate) { |
318 | 549k | int recon_yoffset, recon_uvoffset; |
319 | 549k | int mb_col; |
320 | 549k | int ref_fb_idx = cm->lst_fb_idx; |
321 | 549k | int dst_fb_idx = cm->new_fb_idx; |
322 | 549k | int recon_y_stride = cm->yv12_fb[ref_fb_idx].y_stride; |
323 | 549k | int recon_uv_stride = cm->yv12_fb[ref_fb_idx].uv_stride; |
324 | 549k | int map_index = (mb_row * cpi->common.mb_cols); |
325 | | |
326 | | #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING) |
327 | | const int num_part = (1 << cm->multi_token_partition); |
328 | | TOKENEXTRA *tp_start = cpi->tok; |
329 | | vp8_writer *w; |
330 | | #endif |
331 | | |
332 | 549k | #if CONFIG_MULTITHREAD |
333 | 549k | const int nsync = cpi->mt_sync_range; |
334 | 549k | vpx_atomic_int rightmost_col = VPX_ATOMIC_INIT(cm->mb_cols + nsync); |
335 | 549k | const vpx_atomic_int *last_row_current_mb_col; |
336 | 549k | vpx_atomic_int *current_mb_col = NULL; |
337 | | |
338 | 549k | if (vpx_atomic_load_acquire(&cpi->b_multi_threaded) != 0) { |
339 | 0 | current_mb_col = &cpi->mt_current_mb_col[mb_row]; |
340 | 0 | } |
341 | 549k | if (vpx_atomic_load_acquire(&cpi->b_multi_threaded) != 0 && mb_row != 0) { |
342 | 0 | last_row_current_mb_col = &cpi->mt_current_mb_col[mb_row - 1]; |
343 | 549k | } else { |
344 | 549k | last_row_current_mb_col = &rightmost_col; |
345 | 549k | } |
346 | 549k | #endif |
347 | | |
348 | | #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING) |
349 | | if (num_part > 1) |
350 | | w = &cpi->bc[1 + (mb_row % num_part)]; |
351 | | else |
352 | | w = &cpi->bc[1]; |
353 | | #endif |
354 | | |
355 | | /* reset above block coeffs */ |
356 | 549k | xd->above_context = cm->above_context; |
357 | | |
358 | 549k | xd->up_available = (mb_row != 0); |
359 | 549k | recon_yoffset = (mb_row * recon_y_stride * 16); |
360 | 549k | recon_uvoffset = (mb_row * recon_uv_stride * 8); |
361 | | |
362 | 549k | cpi->tplist[mb_row].start = *tp; |
363 | | /* printf("Main mb_row = %d\n", mb_row); */ |
364 | | |
365 | | /* Distance of Mb to the top & bottom edges, specified in 1/8th pel |
366 | | * units as they are always compared to values that are in 1/8th pel |
367 | | */ |
368 | 549k | xd->mb_to_top_edge = -((mb_row * 16) << 3); |
369 | 549k | xd->mb_to_bottom_edge = ((cm->mb_rows - 1 - mb_row) * 16) << 3; |
370 | | |
371 | | /* Set up limit values for vertical motion vector components |
372 | | * to prevent them extending beyond the UMV borders |
373 | | */ |
374 | 549k | x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16)); |
375 | 549k | x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16); |
376 | | |
377 | | /* Set the mb activity pointer to the start of the row. */ |
378 | 549k | x->mb_activity_ptr = &cpi->mb_activity_map[map_index]; |
379 | | |
380 | | /* for each macroblock col in image */ |
381 | 3.75M | for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) { |
382 | | #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING) |
383 | | *tp = cpi->tok; |
384 | | #endif |
385 | | /* Distance of Mb to the left & right edges, specified in |
386 | | * 1/8th pel units as they are always compared to values |
387 | | * that are in 1/8th pel units |
388 | | */ |
389 | 3.20M | xd->mb_to_left_edge = -((mb_col * 16) << 3); |
390 | 3.20M | xd->mb_to_right_edge = ((cm->mb_cols - 1 - mb_col) * 16) << 3; |
391 | | |
392 | | /* Set up limit values for horizontal motion vector components |
393 | | * to prevent them extending beyond the UMV borders |
394 | | */ |
395 | 3.20M | x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16)); |
396 | 3.20M | x->mv_col_max = |
397 | 3.20M | ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16); |
398 | | |
399 | 3.20M | xd->dst.y_buffer = cm->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset; |
400 | 3.20M | xd->dst.u_buffer = cm->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset; |
401 | 3.20M | xd->dst.v_buffer = cm->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset; |
402 | 3.20M | xd->left_available = (mb_col != 0); |
403 | | |
404 | 3.20M | x->rddiv = cpi->RDDIV; |
405 | 3.20M | x->rdmult = cpi->RDMULT; |
406 | | |
407 | | /* Copy current mb to a buffer */ |
408 | 3.20M | vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16); |
409 | | |
410 | 3.20M | #if CONFIG_MULTITHREAD |
411 | 3.20M | if (vpx_atomic_load_acquire(&cpi->b_multi_threaded) != 0) { |
412 | 0 | if (((mb_col - 1) % nsync) == 0) { |
413 | 0 | vpx_atomic_store_release(current_mb_col, mb_col - 1); |
414 | 0 | } |
415 | |
|
416 | 0 | if (mb_row && !(mb_col & (nsync - 1))) { |
417 | 0 | vp8_atomic_spin_wait(mb_col, last_row_current_mb_col, nsync); |
418 | 0 | } |
419 | 0 | } |
420 | 3.20M | #endif |
421 | | |
422 | 3.20M | if (cpi->oxcf.tuning == VP8_TUNE_SSIM) vp8_activity_masking(cpi, x); |
423 | | |
424 | | /* Is segmentation enabled */ |
425 | | /* MB level adjustment to quantizer */ |
426 | 3.20M | if (xd->segmentation_enabled) { |
427 | | /* Code to set segment id in xd->mbmi.segment_id for current MB |
428 | | * (with range checking) |
429 | | */ |
430 | 0 | if (cpi->segmentation_map[map_index + mb_col] <= 3) { |
431 | 0 | xd->mode_info_context->mbmi.segment_id = |
432 | 0 | cpi->segmentation_map[map_index + mb_col]; |
433 | 0 | } else { |
434 | 0 | xd->mode_info_context->mbmi.segment_id = 0; |
435 | 0 | } |
436 | |
|
437 | 0 | vp8cx_mb_init_quantizer(cpi, x, 1); |
438 | 3.20M | } else { |
439 | | /* Set to Segment 0 by default */ |
440 | 3.20M | xd->mode_info_context->mbmi.segment_id = 0; |
441 | 3.20M | } |
442 | | |
443 | 3.20M | x->active_ptr = cpi->active_map + map_index + mb_col; |
444 | | |
445 | 3.20M | if (cm->frame_type == KEY_FRAME) { |
446 | 1.47M | const int intra_rate_cost = vp8cx_encode_intra_macroblock(cpi, x, tp); |
447 | 1.47M | if (INT_MAX - *totalrate > intra_rate_cost) |
448 | 1.47M | *totalrate += intra_rate_cost; |
449 | 0 | else |
450 | 0 | *totalrate = INT_MAX; |
451 | | #ifdef MODE_STATS |
452 | | y_modes[xd->mbmi.mode]++; |
453 | | #endif |
454 | 1.73M | } else { |
455 | 1.73M | const int inter_rate_cost = vp8cx_encode_inter_macroblock( |
456 | 1.73M | cpi, x, tp, recon_yoffset, recon_uvoffset, mb_row, mb_col); |
457 | 1.73M | if (INT_MAX - *totalrate > inter_rate_cost) |
458 | 1.73M | *totalrate += inter_rate_cost; |
459 | 0 | else |
460 | 0 | *totalrate = INT_MAX; |
461 | | |
462 | | #ifdef MODE_STATS |
463 | | inter_y_modes[xd->mbmi.mode]++; |
464 | | |
465 | | if (xd->mbmi.mode == SPLITMV) { |
466 | | int b; |
467 | | |
468 | | for (b = 0; b < xd->mbmi.partition_count; ++b) { |
469 | | inter_b_modes[x->partition->bmi[b].mode]++; |
470 | | } |
471 | | } |
472 | | |
473 | | #endif |
474 | | |
475 | | // Keep track of how many (consecutive) times a block is coded |
476 | | // as ZEROMV_LASTREF, for base layer frames. |
477 | | // Reset to 0 if its coded as anything else. |
478 | 1.73M | if (cpi->current_layer == 0) { |
479 | 1.73M | if (xd->mode_info_context->mbmi.mode == ZEROMV && |
480 | 162k | xd->mode_info_context->mbmi.ref_frame == LAST_FRAME) { |
481 | | // Increment, check for wrap-around. |
482 | 149k | if (cpi->consec_zero_last[map_index + mb_col] < 255) { |
483 | 149k | cpi->consec_zero_last[map_index + mb_col] += 1; |
484 | 149k | } |
485 | 149k | if (cpi->consec_zero_last_mvbias[map_index + mb_col] < 255) { |
486 | 149k | cpi->consec_zero_last_mvbias[map_index + mb_col] += 1; |
487 | 149k | } |
488 | 1.58M | } else { |
489 | 1.58M | cpi->consec_zero_last[map_index + mb_col] = 0; |
490 | 1.58M | cpi->consec_zero_last_mvbias[map_index + mb_col] = 0; |
491 | 1.58M | } |
492 | 1.73M | if (x->zero_last_dot_suppress) { |
493 | 1.03k | cpi->consec_zero_last_mvbias[map_index + mb_col] = 0; |
494 | 1.03k | } |
495 | 1.73M | } |
496 | | |
497 | | /* Special case code for cyclic refresh |
498 | | * If cyclic update enabled then copy xd->mbmi.segment_id; (which |
499 | | * may have been updated based on mode during |
500 | | * vp8cx_encode_inter_macroblock()) back into the global |
501 | | * segmentation map |
502 | | */ |
503 | 1.73M | if ((cpi->current_layer == 0) && |
504 | 1.73M | (cpi->cyclic_refresh_mode_enabled && xd->segmentation_enabled)) { |
505 | 0 | cpi->segmentation_map[map_index + mb_col] = |
506 | 0 | xd->mode_info_context->mbmi.segment_id; |
507 | | |
508 | | /* If the block has been refreshed mark it as clean (the |
509 | | * magnitude of the -ve influences how long it will be before |
510 | | * we consider another refresh): |
511 | | * Else if it was coded (last frame 0,0) and has not already |
512 | | * been refreshed then mark it as a candidate for cleanup |
513 | | * next time (marked 0) else mark it as dirty (1). |
514 | | */ |
515 | 0 | if (xd->mode_info_context->mbmi.segment_id) { |
516 | 0 | cpi->cyclic_refresh_map[map_index + mb_col] = -1; |
517 | 0 | } else if ((xd->mode_info_context->mbmi.mode == ZEROMV) && |
518 | 0 | (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME)) { |
519 | 0 | if (cpi->cyclic_refresh_map[map_index + mb_col] == 1) { |
520 | 0 | cpi->cyclic_refresh_map[map_index + mb_col] = 0; |
521 | 0 | } |
522 | 0 | } else { |
523 | 0 | cpi->cyclic_refresh_map[map_index + mb_col] = 1; |
524 | 0 | } |
525 | 0 | } |
526 | 1.73M | } |
527 | | |
528 | 3.20M | cpi->tplist[mb_row].stop = *tp; |
529 | | |
530 | | #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING |
531 | | /* pack tokens for this MB */ |
532 | | { |
533 | | int tok_count = *tp - tp_start; |
534 | | vp8_pack_tokens(w, tp_start, tok_count); |
535 | | } |
536 | | #endif |
537 | | /* Increment pointer into gf usage flags structure. */ |
538 | 3.20M | x->gf_active_ptr++; |
539 | | |
540 | | /* Increment the activity mask pointers. */ |
541 | 3.20M | x->mb_activity_ptr++; |
542 | | |
543 | | /* adjust to the next column of macroblocks */ |
544 | 3.20M | x->src.y_buffer += 16; |
545 | 3.20M | x->src.u_buffer += 8; |
546 | 3.20M | x->src.v_buffer += 8; |
547 | | |
548 | 3.20M | recon_yoffset += 16; |
549 | 3.20M | recon_uvoffset += 8; |
550 | | |
551 | | /* Keep track of segment usage */ |
552 | 3.20M | segment_counts[xd->mode_info_context->mbmi.segment_id]++; |
553 | | |
554 | | /* skip to next mb */ |
555 | 3.20M | xd->mode_info_context++; |
556 | 3.20M | x->partition_info++; |
557 | 3.20M | xd->above_context++; |
558 | 3.20M | } |
559 | | |
560 | | /* extend the recon for intra prediction */ |
561 | 549k | vp8_extend_mb_row(&cm->yv12_fb[dst_fb_idx], xd->dst.y_buffer + 16, |
562 | 549k | xd->dst.u_buffer + 8, xd->dst.v_buffer + 8); |
563 | | |
564 | 549k | #if CONFIG_MULTITHREAD |
565 | 549k | if (vpx_atomic_load_acquire(&cpi->b_multi_threaded) != 0) { |
566 | 0 | vpx_atomic_store_release(current_mb_col, |
567 | 0 | vpx_atomic_load_acquire(&rightmost_col)); |
568 | 0 | } |
569 | 549k | #endif |
570 | | |
571 | | /* this is to account for the border */ |
572 | 549k | xd->mode_info_context++; |
573 | 549k | x->partition_info++; |
574 | 549k | } |
575 | | |
576 | 153k | static void init_encode_frame_mb_context(VP8_COMP *cpi) { |
577 | 153k | MACROBLOCK *const x = &cpi->mb; |
578 | 153k | VP8_COMMON *const cm = &cpi->common; |
579 | 153k | MACROBLOCKD *const xd = &x->e_mbd; |
580 | | |
581 | | /* GF active flags data structure */ |
582 | 153k | x->gf_active_ptr = (signed char *)cpi->gf_active_flags; |
583 | | |
584 | | /* Activity map pointer */ |
585 | 153k | x->mb_activity_ptr = cpi->mb_activity_map; |
586 | | |
587 | 153k | x->act_zbin_adj = 0; |
588 | | |
589 | 153k | x->partition_info = x->pi; |
590 | | |
591 | 153k | xd->mode_info_context = cm->mi; |
592 | 153k | xd->mode_info_stride = cm->mode_info_stride; |
593 | | |
594 | 153k | xd->frame_type = cm->frame_type; |
595 | | |
596 | | /* reset intra mode contexts */ |
597 | 153k | if (cm->frame_type == KEY_FRAME) vp8_init_mbmode_probs(cm); |
598 | | |
599 | | /* Copy data over into macro block data structures. */ |
600 | 153k | x->src = *cpi->Source; |
601 | 153k | xd->pre = cm->yv12_fb[cm->lst_fb_idx]; |
602 | 153k | xd->dst = cm->yv12_fb[cm->new_fb_idx]; |
603 | | |
604 | | /* set up frame for intra coded blocks */ |
605 | 153k | vp8_setup_intra_recon(&cm->yv12_fb[cm->new_fb_idx]); |
606 | | |
607 | 153k | vp8_build_block_offsets(x); |
608 | | |
609 | 153k | xd->mode_info_context->mbmi.mode = DC_PRED; |
610 | 153k | xd->mode_info_context->mbmi.uv_mode = DC_PRED; |
611 | | |
612 | 153k | xd->left_context = &cm->left_context; |
613 | | |
614 | 153k | x->mvc = cm->fc.mvc; |
615 | | |
616 | 153k | memset(cm->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES) * cm->mb_cols); |
617 | | |
618 | | /* Special case treatment when GF and ARF are not sensible options |
619 | | * for reference |
620 | | */ |
621 | 153k | if (cpi->ref_frame_flags == VP8_LAST_FRAME) { |
622 | 37.0k | vp8_calc_ref_frame_costs(x->ref_frame_cost, cpi->prob_intra_coded, 255, |
623 | 37.0k | 128); |
624 | 116k | } else if ((cpi->oxcf.number_of_layers > 1) && |
625 | 0 | (cpi->ref_frame_flags == VP8_GOLD_FRAME)) { |
626 | 0 | vp8_calc_ref_frame_costs(x->ref_frame_cost, cpi->prob_intra_coded, 1, 255); |
627 | 116k | } else if ((cpi->oxcf.number_of_layers > 1) && |
628 | 0 | (cpi->ref_frame_flags == VP8_ALTR_FRAME)) { |
629 | 0 | vp8_calc_ref_frame_costs(x->ref_frame_cost, cpi->prob_intra_coded, 1, 1); |
630 | 116k | } else { |
631 | 116k | vp8_calc_ref_frame_costs(x->ref_frame_cost, cpi->prob_intra_coded, |
632 | 116k | cpi->prob_last_coded, cpi->prob_gf_coded); |
633 | 116k | } |
634 | | |
635 | 153k | xd->fullpixel_mask = ~0; |
636 | 153k | if (cm->full_pixel) xd->fullpixel_mask = ~7; |
637 | | |
638 | 153k | vp8_zero(x->coef_counts); |
639 | 153k | vp8_zero(x->ymode_count); |
640 | 153k | vp8_zero(x->uv_mode_count); |
641 | 153k | x->prediction_error = 0; |
642 | 153k | x->intra_error = 0; |
643 | 153k | vp8_zero(x->count_mb_ref_frame_usage); |
644 | 153k | } |
645 | | |
646 | | #if CONFIG_MULTITHREAD |
647 | 0 | static void sum_coef_counts(MACROBLOCK *x, MACROBLOCK *x_thread) { |
648 | 0 | int i = 0; |
649 | 0 | do { |
650 | 0 | int j = 0; |
651 | 0 | do { |
652 | 0 | int k = 0; |
653 | 0 | do { |
654 | | /* at every context */ |
655 | | |
656 | | /* calc probs and branch cts for this frame only */ |
657 | 0 | int t = 0; /* token/prob index */ |
658 | |
|
659 | 0 | do { |
660 | 0 | x->coef_counts[i][j][k][t] += x_thread->coef_counts[i][j][k][t]; |
661 | 0 | } while (++t < ENTROPY_NODES); |
662 | 0 | } while (++k < PREV_COEF_CONTEXTS); |
663 | 0 | } while (++j < COEF_BANDS); |
664 | 0 | } while (++i < BLOCK_TYPES); |
665 | 0 | } |
666 | | #endif // CONFIG_MULTITHREAD |
667 | | |
668 | 153k | void vp8_encode_frame(VP8_COMP *cpi) { |
669 | 153k | int mb_row; |
670 | 153k | MACROBLOCK *const x = &cpi->mb; |
671 | 153k | VP8_COMMON *const cm = &cpi->common; |
672 | 153k | MACROBLOCKD *const xd = &x->e_mbd; |
673 | 153k | TOKENEXTRA *tp = cpi->tok; |
674 | 153k | int segment_counts[MAX_MB_SEGMENTS]; |
675 | 153k | int totalrate; |
676 | | #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING |
677 | | BOOL_CODER *bc = &cpi->bc[1]; /* bc[0] is for control partition */ |
678 | | const int num_part = (1 << cm->multi_token_partition); |
679 | | #endif |
680 | | |
681 | 153k | memset(segment_counts, 0, sizeof(segment_counts)); |
682 | 153k | totalrate = 0; |
683 | | |
684 | 153k | if (cpi->compressor_speed == 2) { |
685 | 59.1k | if (cpi->oxcf.cpu_used < 0) { |
686 | 0 | cpi->Speed = -(cpi->oxcf.cpu_used); |
687 | 59.1k | } else { |
688 | 59.1k | vp8_auto_select_speed(cpi); |
689 | 59.1k | } |
690 | 59.1k | } |
691 | | |
692 | | /* Functions setup for all frame types so we can use MC in AltRef */ |
693 | 153k | if (!cm->use_bilinear_mc_filter) { |
694 | 153k | xd->subpixel_predict = vp8_sixtap_predict4x4; |
695 | 153k | xd->subpixel_predict8x4 = vp8_sixtap_predict8x4; |
696 | 153k | xd->subpixel_predict8x8 = vp8_sixtap_predict8x8; |
697 | 153k | xd->subpixel_predict16x16 = vp8_sixtap_predict16x16; |
698 | 153k | } else { |
699 | 0 | xd->subpixel_predict = vp8_bilinear_predict4x4; |
700 | 0 | xd->subpixel_predict8x4 = vp8_bilinear_predict8x4; |
701 | 0 | xd->subpixel_predict8x8 = vp8_bilinear_predict8x8; |
702 | 0 | xd->subpixel_predict16x16 = vp8_bilinear_predict16x16; |
703 | 0 | } |
704 | | |
705 | 153k | cpi->mb.skip_true_count = 0; |
706 | 153k | cpi->tok_count = 0; |
707 | | |
708 | | #if 0 |
709 | | /* Experimental code */ |
710 | | cpi->frame_distortion = 0; |
711 | | cpi->last_mb_distortion = 0; |
712 | | #endif |
713 | | |
714 | 153k | xd->mode_info_context = cm->mi; |
715 | | |
716 | 153k | vp8_zero(cpi->mb.MVcount); |
717 | | |
718 | 153k | vp8cx_frame_init_quantizer(cpi); |
719 | | |
720 | 153k | vp8_initialize_rd_consts(cpi, x, |
721 | 153k | vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q)); |
722 | | |
723 | 153k | vp8cx_initialize_me_consts(cpi, cm->base_qindex); |
724 | | |
725 | 153k | if (cpi->oxcf.tuning == VP8_TUNE_SSIM) { |
726 | | /* Initialize encode frame context. */ |
727 | 0 | init_encode_frame_mb_context(cpi); |
728 | | |
729 | | /* Build a frame level activity map */ |
730 | 0 | build_activity_map(cpi); |
731 | 0 | } |
732 | | |
733 | | /* re-init encode frame context. */ |
734 | 153k | init_encode_frame_mb_context(cpi); |
735 | | |
736 | | #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING |
737 | | { |
738 | | int i; |
739 | | for (i = 0; i < num_part; ++i) { |
740 | | vp8_start_encode(&bc[i], cpi->partition_d[i + 1], |
741 | | cpi->partition_d_end[i + 1]); |
742 | | bc[i].error = &cm->error; |
743 | | } |
744 | | } |
745 | | |
746 | | #endif |
747 | | |
748 | 153k | { |
749 | | #if CONFIG_INTERNAL_STATS |
750 | | struct vpx_usec_timer emr_timer; |
751 | | vpx_usec_timer_start(&emr_timer); |
752 | | #endif |
753 | | |
754 | 153k | #if CONFIG_MULTITHREAD |
755 | 153k | if (vpx_atomic_load_acquire(&cpi->b_multi_threaded)) { |
756 | 0 | int i; |
757 | |
|
758 | 0 | vp8cx_init_mbrthread_data(cpi, x, cpi->mb_row_ei, |
759 | 0 | cpi->encoding_thread_count); |
760 | |
|
761 | 0 | if (cpi->mt_current_mb_col_size != cm->mb_rows) { |
762 | 0 | vpx_free(cpi->mt_current_mb_col); |
763 | 0 | cpi->mt_current_mb_col = NULL; |
764 | 0 | cpi->mt_current_mb_col_size = 0; |
765 | 0 | CHECK_MEM_ERROR( |
766 | 0 | &cpi->common.error, cpi->mt_current_mb_col, |
767 | 0 | vpx_malloc(sizeof(*cpi->mt_current_mb_col) * cm->mb_rows)); |
768 | 0 | cpi->mt_current_mb_col_size = cm->mb_rows; |
769 | 0 | } |
770 | 0 | for (i = 0; i < cm->mb_rows; ++i) |
771 | 0 | vpx_atomic_store_release(&cpi->mt_current_mb_col[i], -1); |
772 | |
|
773 | 0 | for (i = 0; i < cpi->encoding_thread_count; ++i) { |
774 | 0 | vp8_sem_post(&cpi->h_event_start_encoding[i]); |
775 | 0 | } |
776 | |
|
777 | 0 | for (mb_row = 0; mb_row < cm->mb_rows; |
778 | 0 | mb_row += (cpi->encoding_thread_count + 1)) { |
779 | 0 | vp8_zero(cm->left_context); |
780 | |
|
781 | | #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING |
782 | | tp = cpi->tok; |
783 | | #else |
784 | 0 | tp = cpi->tok + mb_row * (cm->mb_cols * 16 * 24); |
785 | 0 | #endif |
786 | |
|
787 | 0 | encode_mb_row(cpi, cm, mb_row, x, xd, &tp, segment_counts, &totalrate); |
788 | | |
789 | | /* adjust to the next row of mbs */ |
790 | 0 | x->src.y_buffer += |
791 | 0 | 16 * x->src.y_stride * (cpi->encoding_thread_count + 1) - |
792 | 0 | 16 * cm->mb_cols; |
793 | 0 | x->src.u_buffer += |
794 | 0 | 8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - |
795 | 0 | 8 * cm->mb_cols; |
796 | 0 | x->src.v_buffer += |
797 | 0 | 8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - |
798 | 0 | 8 * cm->mb_cols; |
799 | |
|
800 | 0 | xd->mode_info_context += |
801 | 0 | xd->mode_info_stride * cpi->encoding_thread_count; |
802 | 0 | x->partition_info += xd->mode_info_stride * cpi->encoding_thread_count; |
803 | 0 | x->gf_active_ptr += cm->mb_cols * cpi->encoding_thread_count; |
804 | 0 | } |
805 | | /* Wait for all the threads to finish. */ |
806 | 0 | for (i = 0; i < cpi->encoding_thread_count; ++i) { |
807 | 0 | vp8_sem_wait(&cpi->h_event_end_encoding[i]); |
808 | 0 | } |
809 | |
|
810 | 0 | for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) { |
811 | 0 | cpi->tok_count += (unsigned int)(cpi->tplist[mb_row].stop - |
812 | 0 | cpi->tplist[mb_row].start); |
813 | 0 | } |
814 | |
|
815 | 0 | if (xd->segmentation_enabled) { |
816 | 0 | int j; |
817 | |
|
818 | 0 | if (xd->segmentation_enabled) { |
819 | 0 | for (i = 0; i < cpi->encoding_thread_count; ++i) { |
820 | 0 | for (j = 0; j < 4; ++j) { |
821 | 0 | segment_counts[j] += cpi->mb_row_ei[i].segment_counts[j]; |
822 | 0 | } |
823 | 0 | } |
824 | 0 | } |
825 | 0 | } |
826 | |
|
827 | 0 | for (i = 0; i < cpi->encoding_thread_count; ++i) { |
828 | 0 | int mode_count; |
829 | 0 | int c_idx; |
830 | | /* Saturate like encode_mb_row(); per-thread totalrate is already |
831 | | * clamped to INT_MAX individually, so their sum can overflow. */ |
832 | 0 | const int thread_rate = cpi->mb_row_ei[i].totalrate; |
833 | 0 | if (INT_MAX - totalrate > thread_rate) |
834 | 0 | totalrate += thread_rate; |
835 | 0 | else |
836 | 0 | totalrate = INT_MAX; |
837 | |
|
838 | 0 | cpi->mb.skip_true_count += cpi->mb_row_ei[i].mb.skip_true_count; |
839 | |
|
840 | 0 | for (mode_count = 0; mode_count < VP8_YMODES; ++mode_count) { |
841 | 0 | cpi->mb.ymode_count[mode_count] += |
842 | 0 | cpi->mb_row_ei[i].mb.ymode_count[mode_count]; |
843 | 0 | } |
844 | |
|
845 | 0 | for (mode_count = 0; mode_count < VP8_UV_MODES; ++mode_count) { |
846 | 0 | cpi->mb.uv_mode_count[mode_count] += |
847 | 0 | cpi->mb_row_ei[i].mb.uv_mode_count[mode_count]; |
848 | 0 | } |
849 | |
|
850 | 0 | for (c_idx = 0; c_idx < MVvals; ++c_idx) { |
851 | 0 | cpi->mb.MVcount[0][c_idx] += cpi->mb_row_ei[i].mb.MVcount[0][c_idx]; |
852 | 0 | cpi->mb.MVcount[1][c_idx] += cpi->mb_row_ei[i].mb.MVcount[1][c_idx]; |
853 | 0 | } |
854 | |
|
855 | 0 | cpi->mb.prediction_error += cpi->mb_row_ei[i].mb.prediction_error; |
856 | 0 | cpi->mb.intra_error += cpi->mb_row_ei[i].mb.intra_error; |
857 | |
|
858 | 0 | for (c_idx = 0; c_idx < MAX_REF_FRAMES; ++c_idx) { |
859 | 0 | cpi->mb.count_mb_ref_frame_usage[c_idx] += |
860 | 0 | cpi->mb_row_ei[i].mb.count_mb_ref_frame_usage[c_idx]; |
861 | 0 | } |
862 | |
|
863 | 0 | for (c_idx = 0; c_idx < MAX_ERROR_BINS; ++c_idx) { |
864 | 0 | cpi->mb.error_bins[c_idx] += cpi->mb_row_ei[i].mb.error_bins[c_idx]; |
865 | 0 | } |
866 | | |
867 | | /* add up counts for each thread */ |
868 | 0 | sum_coef_counts(x, &cpi->mb_row_ei[i].mb); |
869 | 0 | } |
870 | |
|
871 | 0 | } else |
872 | 153k | #endif // CONFIG_MULTITHREAD |
873 | 153k | { |
874 | | |
875 | | /* for each macroblock row in image */ |
876 | 702k | for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) { |
877 | 549k | vp8_zero(cm->left_context); |
878 | | |
879 | | #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING |
880 | | tp = cpi->tok; |
881 | | #endif |
882 | | |
883 | 549k | encode_mb_row(cpi, cm, mb_row, x, xd, &tp, segment_counts, &totalrate); |
884 | | |
885 | | /* adjust to the next row of mbs */ |
886 | 549k | x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols; |
887 | 549k | x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols; |
888 | 549k | x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols; |
889 | 549k | } |
890 | | |
891 | 153k | cpi->tok_count = (unsigned int)(tp - cpi->tok); |
892 | 153k | } |
893 | | |
894 | | #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING |
895 | | { |
896 | | int i; |
897 | | for (i = 0; i < num_part; ++i) { |
898 | | vp8_stop_encode(&bc[i]); |
899 | | cpi->partition_sz[i + 1] = bc[i].pos; |
900 | | } |
901 | | } |
902 | | #endif |
903 | | |
904 | | #if CONFIG_INTERNAL_STATS |
905 | | vpx_usec_timer_mark(&emr_timer); |
906 | | cpi->time_encode_mb_row += vpx_usec_timer_elapsed(&emr_timer); |
907 | | #endif |
908 | 153k | } |
909 | | |
910 | | // Work out the segment probabilities if segmentation is enabled |
911 | | // and needs to be updated |
912 | 153k | if (xd->segmentation_enabled && xd->update_mb_segmentation_map) { |
913 | 0 | int tot_count; |
914 | 0 | int i; |
915 | | |
916 | | /* Set to defaults */ |
917 | 0 | memset(xd->mb_segment_tree_probs, 255, sizeof(xd->mb_segment_tree_probs)); |
918 | |
|
919 | 0 | tot_count = segment_counts[0] + segment_counts[1] + segment_counts[2] + |
920 | 0 | segment_counts[3]; |
921 | |
|
922 | 0 | if (tot_count) { |
923 | 0 | xd->mb_segment_tree_probs[0] = |
924 | 0 | ((segment_counts[0] + segment_counts[1]) * 255) / tot_count; |
925 | |
|
926 | 0 | tot_count = segment_counts[0] + segment_counts[1]; |
927 | |
|
928 | 0 | if (tot_count > 0) { |
929 | 0 | xd->mb_segment_tree_probs[1] = (segment_counts[0] * 255) / tot_count; |
930 | 0 | } |
931 | |
|
932 | 0 | tot_count = segment_counts[2] + segment_counts[3]; |
933 | |
|
934 | 0 | if (tot_count > 0) { |
935 | 0 | xd->mb_segment_tree_probs[2] = (segment_counts[2] * 255) / tot_count; |
936 | 0 | } |
937 | | |
938 | | /* Zero probabilities not allowed */ |
939 | 0 | for (i = 0; i < MB_FEATURE_TREE_PROBS; ++i) { |
940 | 0 | if (xd->mb_segment_tree_probs[i] == 0) xd->mb_segment_tree_probs[i] = 1; |
941 | 0 | } |
942 | 0 | } |
943 | 0 | } |
944 | | |
945 | | /* projected_frame_size in units of BYTES */ |
946 | 153k | cpi->projected_frame_size = totalrate >> 8; |
947 | | |
948 | | /* Make a note of the percentage MBs coded Intra. */ |
949 | 153k | if (cm->frame_type == KEY_FRAME) { |
950 | 36.0k | cpi->this_frame_percent_intra = 100; |
951 | 117k | } else { |
952 | 117k | int tot_modes; |
953 | | |
954 | 117k | tot_modes = cpi->mb.count_mb_ref_frame_usage[INTRA_FRAME] + |
955 | 117k | cpi->mb.count_mb_ref_frame_usage[LAST_FRAME] + |
956 | 117k | cpi->mb.count_mb_ref_frame_usage[GOLDEN_FRAME] + |
957 | 117k | cpi->mb.count_mb_ref_frame_usage[ALTREF_FRAME]; |
958 | | |
959 | 117k | if (tot_modes) { |
960 | 117k | cpi->this_frame_percent_intra = |
961 | 117k | cpi->mb.count_mb_ref_frame_usage[INTRA_FRAME] * 100 / tot_modes; |
962 | 117k | } |
963 | 117k | } |
964 | | |
965 | 153k | #if !CONFIG_REALTIME_ONLY |
966 | | /* Adjust the projected reference frame usage probability numbers to |
967 | | * reflect what we have just seen. This may be useful when we make |
968 | | * multiple iterations of the recode loop rather than continuing to use |
969 | | * values from the previous frame. |
970 | | */ |
971 | 153k | if ((cm->frame_type != KEY_FRAME) && |
972 | 117k | ((cpi->oxcf.number_of_layers > 1) || |
973 | 117k | (!cm->refresh_alt_ref_frame && !cm->refresh_golden_frame))) { |
974 | 105k | vp8_convert_rfct_to_prob(cpi); |
975 | 105k | } |
976 | 153k | #endif |
977 | 153k | } |
978 | 7.20k | void vp8_setup_block_ptrs(MACROBLOCK *x) { |
979 | 7.20k | int r, c; |
980 | 7.20k | int i; |
981 | | |
982 | 36.0k | for (r = 0; r < 4; ++r) { |
983 | 144k | for (c = 0; c < 4; ++c) { |
984 | 115k | x->block[r * 4 + c].src_diff = x->src_diff + r * 4 * 16 + c * 4; |
985 | 115k | } |
986 | 28.8k | } |
987 | | |
988 | 21.6k | for (r = 0; r < 2; ++r) { |
989 | 43.2k | for (c = 0; c < 2; ++c) { |
990 | 28.8k | x->block[16 + r * 2 + c].src_diff = x->src_diff + 256 + r * 4 * 8 + c * 4; |
991 | 28.8k | } |
992 | 14.4k | } |
993 | | |
994 | 21.6k | for (r = 0; r < 2; ++r) { |
995 | 43.2k | for (c = 0; c < 2; ++c) { |
996 | 28.8k | x->block[20 + r * 2 + c].src_diff = x->src_diff + 320 + r * 4 * 8 + c * 4; |
997 | 28.8k | } |
998 | 14.4k | } |
999 | | |
1000 | 7.20k | x->block[24].src_diff = x->src_diff + 384; |
1001 | | |
1002 | 187k | for (i = 0; i < 25; ++i) { |
1003 | 180k | x->block[i].coeff = x->coeff + i * 16; |
1004 | 180k | } |
1005 | 7.20k | } |
1006 | | |
1007 | 153k | void vp8_build_block_offsets(MACROBLOCK *x) { |
1008 | 153k | int block = 0; |
1009 | 153k | int br, bc; |
1010 | | |
1011 | 153k | vp8_build_block_doffsets(&x->e_mbd); |
1012 | | |
1013 | | /* y blocks */ |
1014 | 153k | x->thismb_ptr = &x->thismb[0]; |
1015 | 766k | for (br = 0; br < 4; ++br) { |
1016 | 3.06M | for (bc = 0; bc < 4; ++bc) { |
1017 | 2.45M | BLOCK *this_block = &x->block[block]; |
1018 | 2.45M | this_block->base_src = &x->thismb_ptr; |
1019 | 2.45M | this_block->src_stride = 16; |
1020 | 2.45M | this_block->src = 4 * br * 16 + 4 * bc; |
1021 | 2.45M | ++block; |
1022 | 2.45M | } |
1023 | 613k | } |
1024 | | |
1025 | | /* u blocks */ |
1026 | 460k | for (br = 0; br < 2; ++br) { |
1027 | 920k | for (bc = 0; bc < 2; ++bc) { |
1028 | 613k | BLOCK *this_block = &x->block[block]; |
1029 | 613k | this_block->base_src = &x->src.u_buffer; |
1030 | 613k | this_block->src_stride = x->src.uv_stride; |
1031 | 613k | this_block->src = 4 * br * this_block->src_stride + 4 * bc; |
1032 | 613k | ++block; |
1033 | 613k | } |
1034 | 306k | } |
1035 | | |
1036 | | /* v blocks */ |
1037 | 460k | for (br = 0; br < 2; ++br) { |
1038 | 920k | for (bc = 0; bc < 2; ++bc) { |
1039 | 613k | BLOCK *this_block = &x->block[block]; |
1040 | 613k | this_block->base_src = &x->src.v_buffer; |
1041 | 613k | this_block->src_stride = x->src.uv_stride; |
1042 | 613k | this_block->src = 4 * br * this_block->src_stride + 4 * bc; |
1043 | 613k | ++block; |
1044 | 613k | } |
1045 | 306k | } |
1046 | 153k | } |
1047 | | |
1048 | 2.42M | static void sum_intra_stats(VP8_COMP *cpi, MACROBLOCK *x) { |
1049 | 2.42M | const MACROBLOCKD *xd = &x->e_mbd; |
1050 | 2.42M | const MB_PREDICTION_MODE m = xd->mode_info_context->mbmi.mode; |
1051 | 2.42M | const MB_PREDICTION_MODE uvm = xd->mode_info_context->mbmi.uv_mode; |
1052 | | |
1053 | | #ifdef MODE_STATS |
1054 | | const int is_key = cpi->common.frame_type == KEY_FRAME; |
1055 | | |
1056 | | ++(is_key ? uv_modes : inter_uv_modes)[uvm]; |
1057 | | |
1058 | | if (m == B_PRED) { |
1059 | | unsigned int *const bct = is_key ? b_modes : inter_b_modes; |
1060 | | |
1061 | | int b = 0; |
1062 | | |
1063 | | do { |
1064 | | ++bct[xd->block[b].bmi.mode]; |
1065 | | } while (++b < 16); |
1066 | | } |
1067 | | |
1068 | | #else |
1069 | 2.42M | (void)cpi; |
1070 | 2.42M | #endif |
1071 | | |
1072 | 2.42M | ++x->ymode_count[m]; |
1073 | 2.42M | ++x->uv_mode_count[uvm]; |
1074 | 2.42M | } |
1075 | | |
1076 | | /* Experimental stub function to create a per MB zbin adjustment based on |
1077 | | * some previously calculated measure of MB activity. |
1078 | | */ |
1079 | 0 | static void adjust_act_zbin(VP8_COMP *cpi, MACROBLOCK *x) { |
1080 | | #if USE_ACT_INDEX |
1081 | | x->act_zbin_adj = *(x->mb_activity_ptr); |
1082 | | #else |
1083 | 0 | int64_t a; |
1084 | 0 | int64_t b; |
1085 | 0 | int64_t act = *(x->mb_activity_ptr); |
1086 | | |
1087 | | /* Apply the masking to the RD multiplier. */ |
1088 | 0 | a = act + 4 * cpi->activity_avg; |
1089 | 0 | b = 4 * act + cpi->activity_avg; |
1090 | |
|
1091 | 0 | if (act > cpi->activity_avg) { |
1092 | 0 | x->act_zbin_adj = (int)(((int64_t)b + (a >> 1)) / a) - 1; |
1093 | 0 | } else { |
1094 | 0 | x->act_zbin_adj = 1 - (int)(((int64_t)a + (b >> 1)) / b); |
1095 | 0 | } |
1096 | 0 | #endif |
1097 | 0 | } |
1098 | | |
1099 | | int vp8cx_encode_intra_macroblock(VP8_COMP *cpi, MACROBLOCK *x, |
1100 | 1.47M | TOKENEXTRA **t) { |
1101 | 1.47M | MACROBLOCKD *xd = &x->e_mbd; |
1102 | 1.47M | int rate; |
1103 | | |
1104 | 1.47M | if (cpi->sf.RD && cpi->compressor_speed != 2) { |
1105 | 770k | vp8_rd_pick_intra_mode(x, &rate); |
1106 | 770k | } else { |
1107 | 700k | vp8_pick_intra_mode(x, &rate); |
1108 | 700k | } |
1109 | | |
1110 | 1.47M | if (cpi->oxcf.tuning == VP8_TUNE_SSIM) { |
1111 | 0 | adjust_act_zbin(cpi, x); |
1112 | 0 | vp8_update_zbin_extra(cpi, x); |
1113 | 0 | } |
1114 | | |
1115 | 1.47M | if (x->e_mbd.mode_info_context->mbmi.mode == B_PRED) { |
1116 | 773k | vp8_encode_intra4x4mby(x); |
1117 | 773k | } else { |
1118 | 698k | vp8_encode_intra16x16mby(x); |
1119 | 698k | } |
1120 | | |
1121 | 1.47M | vp8_encode_intra16x16mbuv(x); |
1122 | | |
1123 | 1.47M | sum_intra_stats(cpi, x); |
1124 | | |
1125 | 1.47M | vp8_tokenize_mb(cpi, x, t); |
1126 | | |
1127 | 1.47M | if (xd->mode_info_context->mbmi.mode != B_PRED) vp8_inverse_transform_mby(xd); |
1128 | | |
1129 | 1.47M | vp8_dequant_idct_add_uv_block(xd->qcoeff + 16 * 16, xd->dequant_uv, |
1130 | 1.47M | xd->dst.u_buffer, xd->dst.v_buffer, |
1131 | 1.47M | xd->dst.uv_stride, xd->eobs + 16); |
1132 | 1.47M | return rate; |
1133 | 1.47M | } |
1134 | | #ifdef SPEEDSTATS |
1135 | | extern int cnt_pm; |
1136 | | #endif |
1137 | | |
1138 | | extern void vp8_fix_contexts(MACROBLOCKD *x); |
1139 | | |
1140 | | int vp8cx_encode_inter_macroblock(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t, |
1141 | | int recon_yoffset, int recon_uvoffset, |
1142 | 1.73M | int mb_row, int mb_col) { |
1143 | 1.73M | MACROBLOCKD *const xd = &x->e_mbd; |
1144 | 1.73M | int intra_error = 0; |
1145 | 1.73M | int rate; |
1146 | 1.73M | int distortion; |
1147 | | |
1148 | 1.73M | x->skip = 0; |
1149 | | |
1150 | 1.73M | if (xd->segmentation_enabled) { |
1151 | 0 | x->encode_breakout = |
1152 | 0 | cpi->segment_encode_breakout[xd->mode_info_context->mbmi.segment_id]; |
1153 | 1.73M | } else { |
1154 | 1.73M | x->encode_breakout = cpi->oxcf.encode_breakout; |
1155 | 1.73M | } |
1156 | | |
1157 | 1.73M | #if CONFIG_TEMPORAL_DENOISING |
1158 | | /* Reset the best sse mode/mv for each macroblock. */ |
1159 | 1.73M | x->best_reference_frame = INTRA_FRAME; |
1160 | 1.73M | x->best_zeromv_reference_frame = INTRA_FRAME; |
1161 | 1.73M | x->best_sse_inter_mode = 0; |
1162 | 1.73M | x->best_sse_mv.as_int = 0; |
1163 | 1.73M | x->need_to_clamp_best_mvs = 0; |
1164 | 1.73M | #endif |
1165 | | |
1166 | 1.73M | if (cpi->sf.RD) { |
1167 | 809k | int zbin_mode_boost_enabled = x->zbin_mode_boost_enabled; |
1168 | | |
1169 | | /* Are we using the fast quantizer for the mode selection? */ |
1170 | 809k | if (cpi->sf.use_fastquant_for_pick) { |
1171 | 809k | x->quantize_b = vp8_fast_quantize_b; |
1172 | | |
1173 | | /* the fast quantizer does not use zbin_extra, so |
1174 | | * do not recalculate */ |
1175 | 809k | x->zbin_mode_boost_enabled = 0; |
1176 | 809k | } |
1177 | 809k | vp8_rd_pick_inter_mode(cpi, x, recon_yoffset, recon_uvoffset, &rate, |
1178 | 809k | &distortion, &intra_error, mb_row, mb_col); |
1179 | | |
1180 | | /* switch back to the regular quantizer for the encode */ |
1181 | 809k | if (cpi->sf.improved_quant) { |
1182 | 809k | x->quantize_b = vp8_regular_quantize_b; |
1183 | 809k | } |
1184 | | |
1185 | | /* restore cpi->zbin_mode_boost_enabled */ |
1186 | 809k | x->zbin_mode_boost_enabled = zbin_mode_boost_enabled; |
1187 | | |
1188 | 928k | } else { |
1189 | 928k | vp8_pick_inter_mode(cpi, x, recon_yoffset, recon_uvoffset, &rate, |
1190 | 928k | &distortion, &intra_error, mb_row, mb_col); |
1191 | 928k | } |
1192 | | |
1193 | 1.73M | x->prediction_error += distortion; |
1194 | 1.73M | x->intra_error += intra_error; |
1195 | | |
1196 | 1.73M | if (cpi->oxcf.tuning == VP8_TUNE_SSIM) { |
1197 | | /* Adjust the zbin based on this MB rate. */ |
1198 | 0 | adjust_act_zbin(cpi, x); |
1199 | 0 | } |
1200 | | |
1201 | | #if 0 |
1202 | | /* Experimental RD code */ |
1203 | | cpi->frame_distortion += distortion; |
1204 | | cpi->last_mb_distortion = distortion; |
1205 | | #endif |
1206 | | |
1207 | | /* MB level adjutment to quantizer setup */ |
1208 | 1.73M | if (xd->segmentation_enabled) { |
1209 | | /* If cyclic update enabled */ |
1210 | 0 | if (cpi->current_layer == 0 && cpi->cyclic_refresh_mode_enabled) { |
1211 | | /* Clear segment_id back to 0 if not coded (last frame 0,0) */ |
1212 | 0 | if ((xd->mode_info_context->mbmi.segment_id == 1) && |
1213 | 0 | ((xd->mode_info_context->mbmi.ref_frame != LAST_FRAME) || |
1214 | 0 | (xd->mode_info_context->mbmi.mode != ZEROMV))) { |
1215 | 0 | xd->mode_info_context->mbmi.segment_id = 0; |
1216 | | |
1217 | | /* segment_id changed, so update */ |
1218 | 0 | vp8cx_mb_init_quantizer(cpi, x, 1); |
1219 | 0 | } |
1220 | 0 | } |
1221 | 0 | } |
1222 | | |
1223 | 1.73M | { |
1224 | | /* Experimental code. |
1225 | | * Special case for gf and arf zeromv modes, for 1 temporal layer. |
1226 | | * Increase zbin size to supress noise. |
1227 | | */ |
1228 | 1.73M | x->zbin_mode_boost = 0; |
1229 | 1.73M | if (x->zbin_mode_boost_enabled) { |
1230 | 1.73M | if (xd->mode_info_context->mbmi.ref_frame != INTRA_FRAME) { |
1231 | 786k | if (xd->mode_info_context->mbmi.mode == ZEROMV) { |
1232 | 162k | if (xd->mode_info_context->mbmi.ref_frame != LAST_FRAME && |
1233 | 12.7k | cpi->oxcf.number_of_layers == 1) { |
1234 | 12.7k | x->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST; |
1235 | 149k | } else { |
1236 | 149k | x->zbin_mode_boost = LF_ZEROMV_ZBIN_BOOST; |
1237 | 149k | } |
1238 | 624k | } else if (xd->mode_info_context->mbmi.mode == SPLITMV) { |
1239 | 186k | x->zbin_mode_boost = 0; |
1240 | 437k | } else { |
1241 | 437k | x->zbin_mode_boost = MV_ZBIN_BOOST; |
1242 | 437k | } |
1243 | 786k | } |
1244 | 1.73M | } |
1245 | | |
1246 | | /* The fast quantizer doesn't use zbin_extra, only do so with |
1247 | | * the regular quantizer. */ |
1248 | 1.73M | if (cpi->sf.improved_quant) vp8_update_zbin_extra(cpi, x); |
1249 | 1.73M | } |
1250 | | |
1251 | 1.73M | x->count_mb_ref_frame_usage[xd->mode_info_context->mbmi.ref_frame]++; |
1252 | | |
1253 | 1.73M | if (xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME) { |
1254 | 951k | vp8_encode_intra16x16mbuv(x); |
1255 | | |
1256 | 951k | if (xd->mode_info_context->mbmi.mode == B_PRED) { |
1257 | 380k | vp8_encode_intra4x4mby(x); |
1258 | 571k | } else { |
1259 | 571k | vp8_encode_intra16x16mby(x); |
1260 | 571k | } |
1261 | | |
1262 | 951k | sum_intra_stats(cpi, x); |
1263 | 951k | } else { |
1264 | 786k | int ref_fb_idx; |
1265 | | |
1266 | 786k | if (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME) { |
1267 | 701k | ref_fb_idx = cpi->common.lst_fb_idx; |
1268 | 701k | } else if (xd->mode_info_context->mbmi.ref_frame == GOLDEN_FRAME) { |
1269 | 65.1k | ref_fb_idx = cpi->common.gld_fb_idx; |
1270 | 65.1k | } else { |
1271 | 19.9k | ref_fb_idx = cpi->common.alt_fb_idx; |
1272 | 19.9k | } |
1273 | | |
1274 | 786k | xd->pre.y_buffer = cpi->common.yv12_fb[ref_fb_idx].y_buffer + recon_yoffset; |
1275 | 786k | xd->pre.u_buffer = |
1276 | 786k | cpi->common.yv12_fb[ref_fb_idx].u_buffer + recon_uvoffset; |
1277 | 786k | xd->pre.v_buffer = |
1278 | 786k | cpi->common.yv12_fb[ref_fb_idx].v_buffer + recon_uvoffset; |
1279 | | |
1280 | 786k | if (!x->skip) { |
1281 | 786k | vp8_encode_inter16x16(x); |
1282 | 786k | } else { |
1283 | 0 | vp8_build_inter16x16_predictors_mb(xd, xd->dst.y_buffer, xd->dst.u_buffer, |
1284 | 0 | xd->dst.v_buffer, xd->dst.y_stride, |
1285 | 0 | xd->dst.uv_stride); |
1286 | 0 | } |
1287 | 786k | } |
1288 | | |
1289 | 1.73M | if (!x->skip) { |
1290 | 1.73M | vp8_tokenize_mb(cpi, x, t); |
1291 | | |
1292 | 1.73M | if (xd->mode_info_context->mbmi.mode != B_PRED) { |
1293 | 1.35M | vp8_inverse_transform_mby(xd); |
1294 | 1.35M | } |
1295 | | |
1296 | 1.73M | vp8_dequant_idct_add_uv_block(xd->qcoeff + 16 * 16, xd->dequant_uv, |
1297 | 1.73M | xd->dst.u_buffer, xd->dst.v_buffer, |
1298 | 1.73M | xd->dst.uv_stride, xd->eobs + 16); |
1299 | 1.73M | } else { |
1300 | | /* always set mb_skip_coeff as it is needed by the loopfilter */ |
1301 | 0 | xd->mode_info_context->mbmi.mb_skip_coeff = 1; |
1302 | |
|
1303 | 0 | if (cpi->common.mb_no_coeff_skip) { |
1304 | 0 | x->skip_true_count++; |
1305 | 0 | vp8_fix_contexts(xd); |
1306 | 0 | } else { |
1307 | 0 | vp8_stuff_mb(cpi, x, t); |
1308 | 0 | } |
1309 | 0 | } |
1310 | | |
1311 | 1.73M | return rate; |
1312 | 1.73M | } |