media/libvpx/vp8/encoder/encodeframe.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial