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