2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
12 #include "vpx_ports/config.h"
15 #include "vp8/common/common.h"
17 #include "vp8/common/extend.h"
18 #include "vp8/common/entropymode.h"
19 #include "vp8/common/quant_common.h"
20 #include "segmentation.h"
21 #include "vp8/common/setupintrarecon.h"
22 #include "encodeintra.h"
23 #include "vp8/common/reconinter.h"
25 #include "pickinter.h"
26 #include "vp8/common/findnearmv.h"
27 #include "vp8/common/reconintra.h"
30 #include "vp8/common/subpixel.h"
31 #include "vpx_ports/vpx_timer.h"
33 #if CONFIG_RUNTIME_CPU_DETECT
34 #define RTCD(x) &cpi->common.rtcd.x
35 #define IF_RTCD(x) (x)
38 #define IF_RTCD(x) NULL
40 extern void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t) ;
42 extern void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex);
43 extern void vp8_auto_select_speed(VP8_COMP *cpi);
44 extern void vp8cx_init_mbrthread_data(VP8_COMP *cpi,
49 void vp8_build_block_offsets(MACROBLOCK *x);
50 void vp8_setup_block_ptrs(MACROBLOCK *x);
51 int vp8cx_encode_inter_macroblock(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t, int recon_yoffset, int recon_uvoffset);
52 int vp8cx_encode_intra_macro_block(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t);
53 static void adjust_act_zbin( VP8_COMP *cpi, MACROBLOCK *x );
56 unsigned int inter_y_modes[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
57 unsigned int inter_uv_modes[4] = {0, 0, 0, 0};
58 unsigned int inter_b_modes[15] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
59 unsigned int y_modes[5] = {0, 0, 0, 0, 0};
60 unsigned int uv_modes[4] = {0, 0, 0, 0};
61 unsigned int b_modes[14] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
65 /* activity_avg must be positive, or flat regions could get a zero weight
66 * (infinite lambda), which confounds analysis.
67 * This also avoids the need for divide by zero checks in
68 * vp8_activity_masking().
70 #define VP8_ACTIVITY_AVG_MIN (64)
72 /* This is used as a reference when computing the source variance for the
73 * purposes of activity masking.
74 * Eventually this should be replaced by custom no-reference routines,
75 * which will be faster.
77 static const unsigned char VP8_VAR_OFFS[16]=
79 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128
83 // Original activity measure from Tim T's code.
84 static unsigned int tt_activity_measure( VP8_COMP *cpi, MACROBLOCK *x )
88 /* TODO: This could also be done over smaller areas (8x8), but that would
89 * require extensive changes elsewhere, as lambda is assumed to be fixed
90 * over an entire MB in most of the code.
91 * Another option is to compute four 8x8 variances, and pick a single
92 * lambda using a non-linear combination (e.g., the smallest, or second
95 act = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x16)(x->src.y_buffer,
96 x->src.y_stride, VP8_VAR_OFFS, 0, &sse);
99 /* If the region is flat, lower the activity some more. */
101 act = act < 5<<12 ? act : 5<<12;
106 // Stub for alternative experimental activity measures.
107 static unsigned int alt_activity_measure( VP8_COMP *cpi,
108 MACROBLOCK *x, int use_dc_pred )
110 return vp8_encode_intra(cpi,x, use_dc_pred);
114 // Measure the activity of the current macroblock
115 // What we measure here is TBD so abstracted to this function
116 #define ALT_ACT_MEASURE 1
117 static unsigned int mb_activity_measure( VP8_COMP *cpi, MACROBLOCK *x,
118 int mb_row, int mb_col)
120 unsigned int mb_activity;
122 if ( ALT_ACT_MEASURE )
124 int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
126 // Or use and alternative.
127 mb_activity = alt_activity_measure( cpi, x, use_dc_pred );
131 // Original activity measure from Tim T's code.
132 mb_activity = tt_activity_measure( cpi, x );
135 if ( mb_activity < VP8_ACTIVITY_AVG_MIN )
136 mb_activity = VP8_ACTIVITY_AVG_MIN;
141 // Calculate an "average" mb activity value for the frame
143 static void calc_av_activity( VP8_COMP *cpi, INT64 activity_sum )
146 // Find median: Simple n^2 algorithm for experimentation
150 unsigned int * sortlist;
153 // Create a list to sort to
154 CHECK_MEM_ERROR(sortlist,
155 vpx_calloc(sizeof(unsigned int),
158 // Copy map to sort list
159 vpx_memcpy( sortlist, cpi->mb_activity_map,
160 sizeof(unsigned int) * cpi->common.MBs );
163 // Ripple each value down to its correct position
164 for ( i = 1; i < cpi->common.MBs; i ++ )
166 for ( j = i; j > 0; j -- )
168 if ( sortlist[j] < sortlist[j-1] )
172 sortlist[j-1] = sortlist[j];
180 // Even number MBs so estimate median as mean of two either side.
181 median = ( 1 + sortlist[cpi->common.MBs >> 1] +
182 sortlist[(cpi->common.MBs >> 1) + 1] ) >> 1;
184 cpi->activity_avg = median;
189 // Simple mean for now
190 cpi->activity_avg = (unsigned int)(activity_sum/cpi->common.MBs);
193 if (cpi->activity_avg < VP8_ACTIVITY_AVG_MIN)
194 cpi->activity_avg = VP8_ACTIVITY_AVG_MIN;
196 // Experimental code: return fixed value normalized for several clips
197 if ( ALT_ACT_MEASURE )
198 cpi->activity_avg = 100000;
201 #define USE_ACT_INDEX 0
202 #define OUTPUT_NORM_ACT_STATS 0
205 // Calculate and activity index for each mb
206 static void calc_activity_index( VP8_COMP *cpi, MACROBLOCK *x )
208 VP8_COMMON *const cm = & cpi->common;
215 #if OUTPUT_NORM_ACT_STATS
216 FILE *f = fopen("norm_act.stt", "a");
217 fprintf(f, "\n%12d\n", cpi->activity_avg );
220 // Reset pointers to start of activity map
221 x->mb_activity_ptr = cpi->mb_activity_map;
223 // Calculate normalized mb activity number.
224 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
226 // for each macroblock col in image
227 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
229 // Read activity from the map
230 act = *(x->mb_activity_ptr);
232 // Calculate a normalized activity number
233 a = act + 4*cpi->activity_avg;
234 b = 4*act + cpi->activity_avg;
237 *(x->activity_ptr) = (int)((b + (a>>1))/a) - 1;
239 *(x->activity_ptr) = 1 - (int)((a + (b>>1))/b);
241 #if OUTPUT_NORM_ACT_STATS
242 fprintf(f, " %6d", *(x->mb_activity_ptr));
244 // Increment activity map pointers
245 x->mb_activity_ptr++;
248 #if OUTPUT_NORM_ACT_STATS
254 #if OUTPUT_NORM_ACT_STATS
261 // Loop through all MBs. Note activity of each, average activity and
262 // calculate a normalized activity for each
263 static void build_activity_map( VP8_COMP *cpi )
265 MACROBLOCK *const x = & cpi->mb;
266 MACROBLOCKD *xd = &x->e_mbd;
267 VP8_COMMON *const cm = & cpi->common;
270 YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
272 int recon_y_stride = new_yv12->y_stride;
276 unsigned int mb_activity;
277 INT64 activity_sum = 0;
279 // for each macroblock row in image
280 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
283 // reset above block coeffs
284 xd->up_available = (mb_row != 0);
285 recon_yoffset = (mb_row * recon_y_stride * 16);
287 // for each macroblock col in image
288 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
291 xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset;
292 xd->left_available = (mb_col != 0);
295 //Copy current mb to a buffer
296 RECON_INVOKE(&xd->rtcd->recon, copy16x16)(x->src.y_buffer, x->src.y_stride, x->thismb, 16);
299 mb_activity = mb_activity_measure( cpi, x, mb_row, mb_col );
302 activity_sum += mb_activity;
304 // Store MB level activity details.
305 *x->mb_activity_ptr = mb_activity;
307 // Increment activity map pointer
308 x->mb_activity_ptr++;
310 // adjust to the next column of source macroblocks
311 x->src.y_buffer += 16;
315 // adjust to the next row of mbs
316 x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
319 //extend the recon for intra prediction
320 vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16,
321 xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
326 // Calculate an "average" MB activity
327 calc_av_activity(cpi, activity_sum);
330 // Calculate an activity index number of each mb
331 calc_activity_index( cpi, x );
336 // Macroblock activity masking
337 void vp8_activity_masking(VP8_COMP *cpi, MACROBLOCK *x)
340 x->rdmult += *(x->mb_activity_ptr) * (x->rdmult >> 2);
341 x->errorperbit = x->rdmult/x->rddiv;
345 INT64 act = *(x->mb_activity_ptr);
347 // Apply the masking to the RD multiplier.
348 a = act + (2*cpi->activity_avg);
349 b = (2*act) + cpi->activity_avg;
351 x->rdmult = (unsigned int)(((INT64)x->rdmult*b + (a>>1))/a);
352 x->errorperbit = x->rdmult/x->rddiv;
356 // Activity based Zbin adjustment
357 adjust_act_zbin(cpi, x);
361 void encode_mb_row(VP8_COMP *cpi,
371 int recon_yoffset, recon_uvoffset;
373 int ref_fb_idx = cm->lst_fb_idx;
374 int dst_fb_idx = cm->new_fb_idx;
375 int recon_y_stride = cm->yv12_fb[ref_fb_idx].y_stride;
376 int recon_uv_stride = cm->yv12_fb[ref_fb_idx].uv_stride;
377 int map_index = (mb_row * cpi->common.mb_cols);
379 #if CONFIG_MULTITHREAD
380 const int nsync = cpi->mt_sync_range;
381 const int rightmost_col = cm->mb_cols - 1;
382 volatile const int *last_row_current_mb_col;
384 if ((cpi->b_multi_threaded != 0) && (mb_row != 0))
385 last_row_current_mb_col = &cpi->mt_current_mb_col[mb_row - 1];
387 last_row_current_mb_col = &rightmost_col;
390 // reset above block coeffs
391 xd->above_context = cm->above_context;
393 xd->up_available = (mb_row != 0);
394 recon_yoffset = (mb_row * recon_y_stride * 16);
395 recon_uvoffset = (mb_row * recon_uv_stride * 8);
397 cpi->tplist[mb_row].start = *tp;
398 //printf("Main mb_row = %d\n", mb_row);
400 // Distance of Mb to the top & bottom edges, specified in 1/8th pel
401 // units as they are always compared to values that are in 1/8th pel units
402 xd->mb_to_top_edge = -((mb_row * 16) << 3);
403 xd->mb_to_bottom_edge = ((cm->mb_rows - 1 - mb_row) * 16) << 3;
405 // Set up limit values for vertical motion vector components
406 // to prevent them extending beyond the UMV borders
407 x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
408 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16)
409 + (VP8BORDERINPIXELS - 16);
411 // Set the mb activity pointer to the start of the row.
412 x->mb_activity_ptr = &cpi->mb_activity_map[map_index];
414 // for each macroblock col in image
415 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
417 // Distance of Mb to the left & right edges, specified in
418 // 1/8th pel units as they are always compared to values
419 // that are in 1/8th pel units
420 xd->mb_to_left_edge = -((mb_col * 16) << 3);
421 xd->mb_to_right_edge = ((cm->mb_cols - 1 - mb_col) * 16) << 3;
423 // Set up limit values for horizontal motion vector components
424 // to prevent them extending beyond the UMV borders
425 x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
426 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16)
427 + (VP8BORDERINPIXELS - 16);
429 xd->dst.y_buffer = cm->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset;
430 xd->dst.u_buffer = cm->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset;
431 xd->dst.v_buffer = cm->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset;
432 xd->left_available = (mb_col != 0);
434 x->rddiv = cpi->RDDIV;
435 x->rdmult = cpi->RDMULT;
437 //Copy current mb to a buffer
438 RECON_INVOKE(&xd->rtcd->recon, copy16x16)(x->src.y_buffer, x->src.y_stride, x->thismb, 16);
440 #if CONFIG_MULTITHREAD
441 if ((cpi->b_multi_threaded != 0) && (mb_row != 0))
443 if ((mb_col & (nsync - 1)) == 0)
445 while (mb_col > (*last_row_current_mb_col - nsync)
446 && (*last_row_current_mb_col) != (cm->mb_cols - 1))
455 if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
456 vp8_activity_masking(cpi, x);
458 // Is segmentation enabled
459 // MB level adjutment to quantizer
460 if (xd->segmentation_enabled)
462 // Code to set segment id in xd->mbmi.segment_id for current MB (with range checking)
463 if (cpi->segmentation_map[map_index+mb_col] <= 3)
464 xd->mode_info_context->mbmi.segment_id = cpi->segmentation_map[map_index+mb_col];
466 xd->mode_info_context->mbmi.segment_id = 0;
468 vp8cx_mb_init_quantizer(cpi, x);
471 xd->mode_info_context->mbmi.segment_id = 0; // Set to Segment 0 by default
473 x->active_ptr = cpi->active_map + map_index + mb_col;
475 if (cm->frame_type == KEY_FRAME)
477 *totalrate += vp8cx_encode_intra_macro_block(cpi, x, tp);
479 y_modes[xd->mbmi.mode] ++;
484 *totalrate += vp8cx_encode_inter_macroblock(cpi, x, tp, recon_yoffset, recon_uvoffset);
487 inter_y_modes[xd->mbmi.mode] ++;
489 if (xd->mbmi.mode == SPLITMV)
493 for (b = 0; b < xd->mbmi.partition_count; b++)
495 inter_b_modes[x->partition->bmi[b].mode] ++;
501 // Count of last ref frame 0,0 useage
502 if ((xd->mode_info_context->mbmi.mode == ZEROMV) && (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME))
503 cpi->inter_zz_count ++;
505 // Special case code for cyclic refresh
506 // If cyclic update enabled then copy xd->mbmi.segment_id; (which may have been updated based on mode
507 // during vp8cx_encode_inter_macroblock()) back into the global sgmentation map
508 if (cpi->cyclic_refresh_mode_enabled && xd->segmentation_enabled)
510 cpi->segmentation_map[map_index+mb_col] = xd->mode_info_context->mbmi.segment_id;
512 // If the block has been refreshed mark it as clean (the magnitude of the -ve influences how long it will be before we consider another refresh):
513 // Else if it was coded (last frame 0,0) and has not already been refreshed then mark it as a candidate for cleanup next time (marked 0)
514 // else mark it as dirty (1).
515 if (xd->mode_info_context->mbmi.segment_id)
516 cpi->cyclic_refresh_map[map_index+mb_col] = -1;
517 else if ((xd->mode_info_context->mbmi.mode == ZEROMV) && (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME))
519 if (cpi->cyclic_refresh_map[map_index+mb_col] == 1)
520 cpi->cyclic_refresh_map[map_index+mb_col] = 0;
523 cpi->cyclic_refresh_map[map_index+mb_col] = 1;
528 cpi->tplist[mb_row].stop = *tp;
530 // Increment pointer into gf useage flags structure.
533 // Increment the activity mask pointers.
534 x->mb_activity_ptr++;
536 /* save the block info */
537 for (i = 0; i < 16; i++)
538 xd->mode_info_context->bmi[i] = xd->block[i].bmi;
540 // adjust to the next column of macroblocks
541 x->src.y_buffer += 16;
542 x->src.u_buffer += 8;
543 x->src.v_buffer += 8;
548 // Keep track of segment useage
549 segment_counts[xd->mode_info_context->mbmi.segment_id] ++;
552 xd->mode_info_context++;
556 #if CONFIG_MULTITHREAD
557 if (cpi->b_multi_threaded != 0)
559 cpi->mt_current_mb_col[mb_row] = mb_col;
564 //extend the recon for intra prediction
566 &cm->yv12_fb[dst_fb_idx],
567 xd->dst.y_buffer + 16,
568 xd->dst.u_buffer + 8,
569 xd->dst.v_buffer + 8);
571 // this is to account for the border
572 xd->mode_info_context++;
575 #if CONFIG_MULTITHREAD
576 if ((cpi->b_multi_threaded != 0) && (mb_row == cm->mb_rows - 1))
578 sem_post(&cpi->h_event_end_encoding); /* signal frame encoding end */
583 void init_encode_frame_mb_context(VP8_COMP *cpi)
585 MACROBLOCK *const x = & cpi->mb;
586 VP8_COMMON *const cm = & cpi->common;
587 MACROBLOCKD *const xd = & x->e_mbd;
589 // GF active flags data structure
590 x->gf_active_ptr = (signed char *)cpi->gf_active_flags;
592 // Activity map pointer
593 x->mb_activity_ptr = cpi->mb_activity_map;
595 x->vector_range = 32;
599 x->partition_info = x->pi;
601 xd->mode_info_context = cm->mi;
602 xd->mode_info_stride = cm->mode_info_stride;
604 xd->frame_type = cm->frame_type;
606 xd->frames_since_golden = cm->frames_since_golden;
607 xd->frames_till_alt_ref_frame = cm->frames_till_alt_ref_frame;
609 // reset intra mode contexts
610 if (cm->frame_type == KEY_FRAME)
611 vp8_init_mbmode_probs(cm);
613 // Copy data over into macro block data sturctures.
614 x->src = * cpi->Source;
615 xd->pre = cm->yv12_fb[cm->lst_fb_idx];
616 xd->dst = cm->yv12_fb[cm->new_fb_idx];
618 // set up frame for intra coded blocks
619 vp8_setup_intra_recon(&cm->yv12_fb[cm->new_fb_idx]);
621 vp8_build_block_offsets(x);
623 vp8_setup_block_dptrs(&x->e_mbd);
625 vp8_setup_block_ptrs(x);
627 xd->mode_info_context->mbmi.mode = DC_PRED;
628 xd->mode_info_context->mbmi.uv_mode = DC_PRED;
630 xd->left_context = &cm->left_context;
632 vp8_zero(cpi->count_mb_ref_frame_usage)
633 vp8_zero(cpi->ymode_count)
634 vp8_zero(cpi->uv_mode_count)
638 vpx_memset(cm->above_context, 0,
639 sizeof(ENTROPY_CONTEXT_PLANES) * cm->mb_cols);
641 xd->ref_frame_cost[INTRA_FRAME] = vp8_cost_zero(cpi->prob_intra_coded);
643 // Special case treatment when GF and ARF are not sensible options for reference
644 if (cpi->ref_frame_flags == VP8_LAST_FLAG)
646 xd->ref_frame_cost[LAST_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
647 + vp8_cost_zero(255);
648 xd->ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
650 + vp8_cost_zero(128);
651 xd->ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
657 xd->ref_frame_cost[LAST_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
658 + vp8_cost_zero(cpi->prob_last_coded);
659 xd->ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
660 + vp8_cost_one(cpi->prob_last_coded)
661 + vp8_cost_zero(cpi->prob_gf_coded);
662 xd->ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
663 + vp8_cost_one(cpi->prob_last_coded)
664 + vp8_cost_one(cpi->prob_gf_coded);
669 void vp8_encode_frame(VP8_COMP *cpi)
672 MACROBLOCK *const x = & cpi->mb;
673 VP8_COMMON *const cm = & cpi->common;
674 MACROBLOCKD *const xd = & x->e_mbd;
676 TOKENEXTRA *tp = cpi->tok;
677 int segment_counts[MAX_MB_SEGMENTS];
680 vpx_memset(segment_counts, 0, sizeof(segment_counts));
683 if (cpi->compressor_speed == 2)
685 if (cpi->oxcf.cpu_used < 0)
686 cpi->Speed = -(cpi->oxcf.cpu_used);
688 vp8_auto_select_speed(cpi);
691 // Functions setup for all frame types so we can use MC in AltRef
692 if (cm->mcomp_filter_type == SIXTAP)
694 xd->subpixel_predict = SUBPIX_INVOKE(
695 &cpi->common.rtcd.subpix, sixtap4x4);
696 xd->subpixel_predict8x4 = SUBPIX_INVOKE(
697 &cpi->common.rtcd.subpix, sixtap8x4);
698 xd->subpixel_predict8x8 = SUBPIX_INVOKE(
699 &cpi->common.rtcd.subpix, sixtap8x8);
700 xd->subpixel_predict16x16 = SUBPIX_INVOKE(
701 &cpi->common.rtcd.subpix, sixtap16x16);
705 xd->subpixel_predict = SUBPIX_INVOKE(
706 &cpi->common.rtcd.subpix, bilinear4x4);
707 xd->subpixel_predict8x4 = SUBPIX_INVOKE(
708 &cpi->common.rtcd.subpix, bilinear8x4);
709 xd->subpixel_predict8x8 = SUBPIX_INVOKE(
710 &cpi->common.rtcd.subpix, bilinear8x8);
711 xd->subpixel_predict16x16 = SUBPIX_INVOKE(
712 &cpi->common.rtcd.subpix, bilinear16x16);
715 // Reset frame count of inter 0,0 motion vector useage.
716 cpi->inter_zz_count = 0;
718 vpx_memset(segment_counts, 0, sizeof(segment_counts));
720 cpi->prediction_error = 0;
721 cpi->intra_error = 0;
722 cpi->skip_true_count = 0;
723 cpi->skip_false_count = 0;
727 cpi->frame_distortion = 0;
728 cpi->last_mb_distortion = 0;
731 xd->mode_info_context = cm->mi;
733 vp8_zero(cpi->MVcount);
734 vp8_zero(cpi->coef_counts);
736 vp8cx_frame_init_quantizer(cpi);
738 vp8_initialize_rd_consts(cpi,
739 vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q));
741 vp8cx_initialize_me_consts(cpi, cm->base_qindex);
743 if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
745 // Initialize encode frame context.
746 init_encode_frame_mb_context(cpi);
748 // Build a frame level activity map
749 build_activity_map(cpi);
752 // re-initencode frame context.
753 init_encode_frame_mb_context(cpi);
756 struct vpx_usec_timer emr_timer;
757 vpx_usec_timer_start(&emr_timer);
759 #if CONFIG_MULTITHREAD
760 if (cpi->b_multi_threaded)
764 vp8cx_init_mbrthread_data(cpi, x, cpi->mb_row_ei, 1, cpi->encoding_thread_count);
766 for (i = 0; i < cm->mb_rows; i++)
767 cpi->mt_current_mb_col[i] = -1;
769 for (i = 0; i < cpi->encoding_thread_count; i++)
771 sem_post(&cpi->h_event_start_encoding[i]);
774 for (mb_row = 0; mb_row < cm->mb_rows; mb_row += (cpi->encoding_thread_count + 1))
776 vp8_zero(cm->left_context)
778 tp = cpi->tok + mb_row * (cm->mb_cols * 16 * 24);
780 encode_mb_row(cpi, cm, mb_row, x, xd, &tp, segment_counts, &totalrate);
782 // adjust to the next row of mbs
783 x->src.y_buffer += 16 * x->src.y_stride * (cpi->encoding_thread_count + 1) - 16 * cm->mb_cols;
784 x->src.u_buffer += 8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - 8 * cm->mb_cols;
785 x->src.v_buffer += 8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - 8 * cm->mb_cols;
787 xd->mode_info_context += xd->mode_info_stride * cpi->encoding_thread_count;
788 x->partition_info += xd->mode_info_stride * cpi->encoding_thread_count;
789 x->gf_active_ptr += cm->mb_cols * cpi->encoding_thread_count;
793 sem_wait(&cpi->h_event_end_encoding); /* wait for other threads to finish */
797 for (mb_row = 0; mb_row < cm->mb_rows; mb_row ++)
799 cpi->tok_count += cpi->tplist[mb_row].stop - cpi->tplist[mb_row].start;
802 if (xd->segmentation_enabled)
806 if (xd->segmentation_enabled)
809 for (i = 0; i < cpi->encoding_thread_count; i++)
811 for (j = 0; j < 4; j++)
812 segment_counts[j] += cpi->mb_row_ei[i].segment_counts[j];
817 for (i = 0; i < cpi->encoding_thread_count; i++)
819 totalrate += cpi->mb_row_ei[i].totalrate;
826 // for each macroblock row in image
827 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
830 vp8_zero(cm->left_context)
832 encode_mb_row(cpi, cm, mb_row, x, xd, &tp, segment_counts, &totalrate);
834 // adjust to the next row of mbs
835 x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
836 x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
837 x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
840 cpi->tok_count = tp - cpi->tok;
844 vpx_usec_timer_mark(&emr_timer);
845 cpi->time_encode_mb_row += vpx_usec_timer_elapsed(&emr_timer);
850 // Work out the segment probabilites if segmentation is enabled
851 if (xd->segmentation_enabled)
857 vpx_memset(xd->mb_segment_tree_probs, 255 , sizeof(xd->mb_segment_tree_probs));
859 tot_count = segment_counts[0] + segment_counts[1] + segment_counts[2] + segment_counts[3];
863 xd->mb_segment_tree_probs[0] = ((segment_counts[0] + segment_counts[1]) * 255) / tot_count;
865 tot_count = segment_counts[0] + segment_counts[1];
869 xd->mb_segment_tree_probs[1] = (segment_counts[0] * 255) / tot_count;
872 tot_count = segment_counts[2] + segment_counts[3];
875 xd->mb_segment_tree_probs[2] = (segment_counts[2] * 255) / tot_count;
877 // Zero probabilities not allowed
878 for (i = 0; i < MB_FEATURE_TREE_PROBS; i ++)
880 if (xd->mb_segment_tree_probs[i] == 0)
881 xd->mb_segment_tree_probs[i] = 1;
886 // 256 rate units to the bit
887 cpi->projected_frame_size = totalrate >> 8; // projected_frame_size in units of BYTES
889 // Make a note of the percentage MBs coded Intra.
890 if (cm->frame_type == KEY_FRAME)
892 cpi->this_frame_percent_intra = 100;
898 tot_modes = cpi->count_mb_ref_frame_usage[INTRA_FRAME]
899 + cpi->count_mb_ref_frame_usage[LAST_FRAME]
900 + cpi->count_mb_ref_frame_usage[GOLDEN_FRAME]
901 + cpi->count_mb_ref_frame_usage[ALTREF_FRAME];
904 cpi->this_frame_percent_intra = cpi->count_mb_ref_frame_usage[INTRA_FRAME] * 100 / tot_modes;
911 int flag[2] = {0, 0};
913 for (cnt = 0; cnt < MVPcount; cnt++)
915 if (cm->fc.pre_mvc[0][cnt] != cm->fc.mvc[0][cnt])
918 vpx_memcpy(cm->fc.pre_mvc[0], cm->fc.mvc[0], MVPcount);
923 for (cnt = 0; cnt < MVPcount; cnt++)
925 if (cm->fc.pre_mvc[1][cnt] != cm->fc.mvc[1][cnt])
928 vpx_memcpy(cm->fc.pre_mvc[1], cm->fc.mvc[1], MVPcount);
933 if (flag[0] || flag[1])
934 vp8_build_component_cost_table(cpi->mb.mvcost, (const MV_CONTEXT *) cm->fc.mvc, flag);
938 // Adjust the projected reference frame useage probability numbers to reflect
939 // what we have just seen. This may be usefull when we make multiple itterations
940 // of the recode loop rather than continuing to use values from the previous frame.
941 if ((cm->frame_type != KEY_FRAME) && !cm->refresh_alt_ref_frame && !cm->refresh_golden_frame)
943 const int *const rfct = cpi->count_mb_ref_frame_usage;
944 const int rf_intra = rfct[INTRA_FRAME];
945 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
947 if ((rf_intra + rf_inter) > 0)
949 cpi->prob_intra_coded = (rf_intra * 255) / (rf_intra + rf_inter);
951 if (cpi->prob_intra_coded < 1)
952 cpi->prob_intra_coded = 1;
954 if ((cm->frames_since_golden > 0) || cpi->source_alt_ref_active)
956 cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
958 if (cpi->prob_last_coded < 1)
959 cpi->prob_last_coded = 1;
961 cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
962 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
964 if (cpi->prob_gf_coded < 1)
965 cpi->prob_gf_coded = 1;
971 // Keep record of the total distortion this time around for future use
972 cpi->last_frame_distortion = cpi->frame_distortion;
976 void vp8_setup_block_ptrs(MACROBLOCK *x)
981 for (r = 0; r < 4; r++)
983 for (c = 0; c < 4; c++)
985 x->block[r*4+c].src_diff = x->src_diff + r * 4 * 16 + c * 4;
989 for (r = 0; r < 2; r++)
991 for (c = 0; c < 2; c++)
993 x->block[16 + r*2+c].src_diff = x->src_diff + 256 + r * 4 * 8 + c * 4;
998 for (r = 0; r < 2; r++)
1000 for (c = 0; c < 2; c++)
1002 x->block[20 + r*2+c].src_diff = x->src_diff + 320 + r * 4 * 8 + c * 4;
1006 x->block[24].src_diff = x->src_diff + 384;
1009 for (i = 0; i < 25; i++)
1011 x->block[i].coeff = x->coeff + i * 16;
1015 void vp8_build_block_offsets(MACROBLOCK *x)
1020 vp8_build_block_doffsets(&x->e_mbd);
1023 x->thismb_ptr = &x->thismb[0];
1024 for (br = 0; br < 4; br++)
1026 for (bc = 0; bc < 4; bc++)
1028 BLOCK *this_block = &x->block[block];
1029 //this_block->base_src = &x->src.y_buffer;
1030 //this_block->src_stride = x->src.y_stride;
1031 //this_block->src = 4 * br * this_block->src_stride + 4 * bc;
1032 this_block->base_src = &x->thismb_ptr;
1033 this_block->src_stride = 16;
1034 this_block->src = 4 * br * 16 + 4 * bc;
1040 for (br = 0; br < 2; br++)
1042 for (bc = 0; bc < 2; bc++)
1044 BLOCK *this_block = &x->block[block];
1045 this_block->base_src = &x->src.u_buffer;
1046 this_block->src_stride = x->src.uv_stride;
1047 this_block->src = 4 * br * this_block->src_stride + 4 * bc;
1053 for (br = 0; br < 2; br++)
1055 for (bc = 0; bc < 2; bc++)
1057 BLOCK *this_block = &x->block[block];
1058 this_block->base_src = &x->src.v_buffer;
1059 this_block->src_stride = x->src.uv_stride;
1060 this_block->src = 4 * br * this_block->src_stride + 4 * bc;
1066 static void sum_intra_stats(VP8_COMP *cpi, MACROBLOCK *x)
1068 const MACROBLOCKD *xd = & x->e_mbd;
1069 const MB_PREDICTION_MODE m = xd->mode_info_context->mbmi.mode;
1070 const MB_PREDICTION_MODE uvm = xd->mode_info_context->mbmi.uv_mode;
1073 const int is_key = cpi->common.frame_type == KEY_FRAME;
1075 ++ (is_key ? uv_modes : inter_uv_modes)[uvm];
1079 unsigned int *const bct = is_key ? b_modes : inter_b_modes;
1085 ++ bct[xd->block[b].bmi.mode];
1092 ++cpi->ymode_count[m];
1093 ++cpi->uv_mode_count[uvm];
1097 // Experimental stub function to create a per MB zbin adjustment based on
1098 // some previously calculated measure of MB activity.
1099 static void adjust_act_zbin( VP8_COMP *cpi, MACROBLOCK *x )
1102 x->act_zbin_adj = *(x->mb_activity_ptr);
1106 INT64 act = *(x->mb_activity_ptr);
1108 // Apply the masking to the RD multiplier.
1109 a = act + 4*cpi->activity_avg;
1110 b = 4*act + cpi->activity_avg;
1112 if ( act > cpi->activity_avg )
1113 x->act_zbin_adj = (int)(((INT64)b + (a>>1))/a) - 1;
1115 x->act_zbin_adj = 1 - (int)(((INT64)a + (b>>1))/b);
1119 int vp8cx_encode_intra_macro_block(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t)
1123 if (cpi->sf.RD && cpi->compressor_speed != 2)
1124 vp8_rd_pick_intra_mode(cpi, x, &rate);
1126 vp8_pick_intra_mode(cpi, x, &rate);
1128 if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
1130 adjust_act_zbin( cpi, x );
1131 vp8_update_zbin_extra(cpi, x);
1134 if (x->e_mbd.mode_info_context->mbmi.mode == B_PRED)
1135 vp8_encode_intra4x4mby(IF_RTCD(&cpi->rtcd), x);
1137 vp8_encode_intra16x16mby(IF_RTCD(&cpi->rtcd), x);
1139 vp8_encode_intra16x16mbuv(IF_RTCD(&cpi->rtcd), x);
1140 sum_intra_stats(cpi, x);
1141 vp8_tokenize_mb(cpi, &x->e_mbd, t);
1149 extern void vp8_fix_contexts(MACROBLOCKD *x);
1151 int vp8cx_encode_inter_macroblock
1153 VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t,
1154 int recon_yoffset, int recon_uvoffset
1157 MACROBLOCKD *const xd = &x->e_mbd;
1158 int intra_error = 0;
1164 if (xd->segmentation_enabled)
1165 x->encode_breakout = cpi->segment_encode_breakout[xd->mode_info_context->mbmi.segment_id];
1167 x->encode_breakout = cpi->oxcf.encode_breakout;
1171 int zbin_mode_boost_enabled = cpi->zbin_mode_boost_enabled;
1173 /* Are we using the fast quantizer for the mode selection? */
1174 if(cpi->sf.use_fastquant_for_pick)
1176 cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
1178 cpi->mb.quantize_b_pair = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
1181 /* the fast quantizer does not use zbin_extra, so
1182 * do not recalculate */
1183 cpi->zbin_mode_boost_enabled = 0;
1185 vp8_rd_pick_inter_mode(cpi, x, recon_yoffset, recon_uvoffset, &rate,
1186 &distortion, &intra_error);
1188 /* switch back to the regular quantizer for the encode */
1189 if (cpi->sf.improved_quant)
1191 cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
1193 cpi->mb.quantize_b_pair = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
1197 /* restore cpi->zbin_mode_boost_enabled */
1198 cpi->zbin_mode_boost_enabled = zbin_mode_boost_enabled;
1202 vp8_pick_inter_mode(cpi, x, recon_yoffset, recon_uvoffset, &rate,
1203 &distortion, &intra_error);
1205 cpi->prediction_error += distortion;
1206 cpi->intra_error += intra_error;
1208 if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
1210 // Adjust the zbin based on this MB rate.
1211 adjust_act_zbin( cpi, x );
1215 // Experimental RD code
1216 cpi->frame_distortion += distortion;
1217 cpi->last_mb_distortion = distortion;
1220 // MB level adjutment to quantizer setup
1221 if (xd->segmentation_enabled)
1223 // If cyclic update enabled
1224 if (cpi->cyclic_refresh_mode_enabled)
1226 // Clear segment_id back to 0 if not coded (last frame 0,0)
1227 if ((xd->mode_info_context->mbmi.segment_id == 1) &&
1228 ((xd->mode_info_context->mbmi.ref_frame != LAST_FRAME) || (xd->mode_info_context->mbmi.mode != ZEROMV)))
1230 xd->mode_info_context->mbmi.segment_id = 0;
1232 /* segment_id changed, so update */
1233 vp8cx_mb_init_quantizer(cpi, x);
1239 // Experimental code. Special case for gf and arf zeromv modes.
1240 // Increase zbin size to supress noise
1241 cpi->zbin_mode_boost = 0;
1242 if (cpi->zbin_mode_boost_enabled)
1244 if ( xd->mode_info_context->mbmi.ref_frame != INTRA_FRAME )
1246 if (xd->mode_info_context->mbmi.mode == ZEROMV)
1248 if (xd->mode_info_context->mbmi.ref_frame != LAST_FRAME)
1249 cpi->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST;
1251 cpi->zbin_mode_boost = LF_ZEROMV_ZBIN_BOOST;
1253 else if (xd->mode_info_context->mbmi.mode == SPLITMV)
1254 cpi->zbin_mode_boost = 0;
1256 cpi->zbin_mode_boost = MV_ZBIN_BOOST;
1259 vp8_update_zbin_extra(cpi, x);
1262 cpi->count_mb_ref_frame_usage[xd->mode_info_context->mbmi.ref_frame] ++;
1264 if (xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME)
1266 vp8_encode_intra16x16mbuv(IF_RTCD(&cpi->rtcd), x);
1268 if (xd->mode_info_context->mbmi.mode == B_PRED)
1270 vp8_encode_intra4x4mby(IF_RTCD(&cpi->rtcd), x);
1274 vp8_encode_intra16x16mby(IF_RTCD(&cpi->rtcd), x);
1277 sum_intra_stats(cpi, x);
1283 vp8_build_uvmvs(xd, cpi->common.full_pixel);
1285 if (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME)
1286 ref_fb_idx = cpi->common.lst_fb_idx;
1287 else if (xd->mode_info_context->mbmi.ref_frame == GOLDEN_FRAME)
1288 ref_fb_idx = cpi->common.gld_fb_idx;
1290 ref_fb_idx = cpi->common.alt_fb_idx;
1292 xd->pre.y_buffer = cpi->common.yv12_fb[ref_fb_idx].y_buffer + recon_yoffset;
1293 xd->pre.u_buffer = cpi->common.yv12_fb[ref_fb_idx].u_buffer + recon_uvoffset;
1294 xd->pre.v_buffer = cpi->common.yv12_fb[ref_fb_idx].v_buffer + recon_uvoffset;
1298 vp8_encode_inter16x16(IF_RTCD(&cpi->rtcd), x);
1300 // Clear mb_skip_coeff if mb_no_coeff_skip is not set
1301 if (!cpi->common.mb_no_coeff_skip)
1302 xd->mode_info_context->mbmi.mb_skip_coeff = 0;
1306 vp8_build_inter16x16_predictors_mb(xd, xd->dst.y_buffer,
1307 xd->dst.u_buffer, xd->dst.v_buffer,
1308 xd->dst.y_stride, xd->dst.uv_stride);
1313 vp8_tokenize_mb(cpi, xd, t);
1316 if (cpi->common.mb_no_coeff_skip)
1318 xd->mode_info_context->mbmi.mb_skip_coeff = 1;
1319 cpi->skip_true_count ++;
1320 vp8_fix_contexts(xd);
1324 vp8_stuff_mb(cpi, xd, t);
1325 xd->mode_info_context->mbmi.mb_skip_coeff = 0;
1326 cpi->skip_false_count ++;