/media/libvpx/vp8/encoder/encodeframe.c

http://github.com/zpao/v8monkey · C · 1332 lines · 931 code · 255 blank · 146 comment · 175 complexity · 175ad2814536cc68f2b1f328abcecc10 MD5 · raw file

  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "vpx_ports/config.h"
  11. #include "encodemb.h"
  12. #include "encodemv.h"
  13. #include "vp8/common/common.h"
  14. #include "onyx_int.h"
  15. #include "vp8/common/extend.h"
  16. #include "vp8/common/entropymode.h"
  17. #include "vp8/common/quant_common.h"
  18. #include "segmentation.h"
  19. #include "vp8/common/setupintrarecon.h"
  20. #include "encodeintra.h"
  21. #include "vp8/common/reconinter.h"
  22. #include "rdopt.h"
  23. #include "pickinter.h"
  24. #include "vp8/common/findnearmv.h"
  25. #include "vp8/common/reconintra.h"
  26. #include <stdio.h>
  27. #include <limits.h>
  28. #include "vp8/common/subpixel.h"
  29. #include "vpx_ports/vpx_timer.h"
  30. #if CONFIG_RUNTIME_CPU_DETECT
  31. #define RTCD(x) &cpi->common.rtcd.x
  32. #define IF_RTCD(x) (x)
  33. #else
  34. #define RTCD(x) NULL
  35. #define IF_RTCD(x) NULL
  36. #endif
  37. extern void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t) ;
  38. extern void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex);
  39. extern void vp8_auto_select_speed(VP8_COMP *cpi);
  40. extern void vp8cx_init_mbrthread_data(VP8_COMP *cpi,
  41. MACROBLOCK *x,
  42. MB_ROW_COMP *mbr_ei,
  43. int mb_row,
  44. int count);
  45. void vp8_build_block_offsets(MACROBLOCK *x);
  46. void vp8_setup_block_ptrs(MACROBLOCK *x);
  47. int vp8cx_encode_inter_macroblock(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t, int recon_yoffset, int recon_uvoffset);
  48. int vp8cx_encode_intra_macro_block(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t);
  49. static void adjust_act_zbin( VP8_COMP *cpi, MACROBLOCK *x );
  50. #ifdef MODE_STATS
  51. unsigned int inter_y_modes[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  52. unsigned int inter_uv_modes[4] = {0, 0, 0, 0};
  53. unsigned int inter_b_modes[15] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  54. unsigned int y_modes[5] = {0, 0, 0, 0, 0};
  55. unsigned int uv_modes[4] = {0, 0, 0, 0};
  56. unsigned int b_modes[14] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  57. #endif
  58. /* activity_avg must be positive, or flat regions could get a zero weight
  59. * (infinite lambda), which confounds analysis.
  60. * This also avoids the need for divide by zero checks in
  61. * vp8_activity_masking().
  62. */
  63. #define VP8_ACTIVITY_AVG_MIN (64)
  64. /* This is used as a reference when computing the source variance for the
  65. * purposes of activity masking.
  66. * Eventually this should be replaced by custom no-reference routines,
  67. * which will be faster.
  68. */
  69. static const unsigned char VP8_VAR_OFFS[16]=
  70. {
  71. 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128
  72. };
  73. // Original activity measure from Tim T's code.
  74. static unsigned int tt_activity_measure( VP8_COMP *cpi, MACROBLOCK *x )
  75. {
  76. unsigned int act;
  77. unsigned int sse;
  78. /* TODO: This could also be done over smaller areas (8x8), but that would
  79. * require extensive changes elsewhere, as lambda is assumed to be fixed
  80. * over an entire MB in most of the code.
  81. * Another option is to compute four 8x8 variances, and pick a single
  82. * lambda using a non-linear combination (e.g., the smallest, or second
  83. * smallest, etc.).
  84. */
  85. act = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x16)(x->src.y_buffer,
  86. x->src.y_stride, VP8_VAR_OFFS, 0, &sse);
  87. act = act<<4;
  88. /* If the region is flat, lower the activity some more. */
  89. if (act < 8<<12)
  90. act = act < 5<<12 ? act : 5<<12;
  91. return act;
  92. }
  93. // Stub for alternative experimental activity measures.
  94. static unsigned int alt_activity_measure( VP8_COMP *cpi,
  95. MACROBLOCK *x, int use_dc_pred )
  96. {
  97. return vp8_encode_intra(cpi,x, use_dc_pred);
  98. }
  99. // Measure the activity of the current macroblock
  100. // What we measure here is TBD so abstracted to this function
  101. #define ALT_ACT_MEASURE 1
  102. static unsigned int mb_activity_measure( VP8_COMP *cpi, MACROBLOCK *x,
  103. int mb_row, int mb_col)
  104. {
  105. unsigned int mb_activity;
  106. if ( ALT_ACT_MEASURE )
  107. {
  108. int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
  109. // Or use and alternative.
  110. mb_activity = alt_activity_measure( cpi, x, use_dc_pred );
  111. }
  112. else
  113. {
  114. // Original activity measure from Tim T's code.
  115. mb_activity = tt_activity_measure( cpi, x );
  116. }
  117. if ( mb_activity < VP8_ACTIVITY_AVG_MIN )
  118. mb_activity = VP8_ACTIVITY_AVG_MIN;
  119. return mb_activity;
  120. }
  121. // Calculate an "average" mb activity value for the frame
  122. #define ACT_MEDIAN 0
  123. static void calc_av_activity( VP8_COMP *cpi, int64_t activity_sum )
  124. {
  125. #if ACT_MEDIAN
  126. // Find median: Simple n^2 algorithm for experimentation
  127. {
  128. unsigned int median;
  129. unsigned int i,j;
  130. unsigned int * sortlist;
  131. unsigned int tmp;
  132. // Create a list to sort to
  133. CHECK_MEM_ERROR(sortlist,
  134. vpx_calloc(sizeof(unsigned int),
  135. cpi->common.MBs));
  136. // Copy map to sort list
  137. vpx_memcpy( sortlist, cpi->mb_activity_map,
  138. sizeof(unsigned int) * cpi->common.MBs );
  139. // Ripple each value down to its correct position
  140. for ( i = 1; i < cpi->common.MBs; i ++ )
  141. {
  142. for ( j = i; j > 0; j -- )
  143. {
  144. if ( sortlist[j] < sortlist[j-1] )
  145. {
  146. // Swap values
  147. tmp = sortlist[j-1];
  148. sortlist[j-1] = sortlist[j];
  149. sortlist[j] = tmp;
  150. }
  151. else
  152. break;
  153. }
  154. }
  155. // Even number MBs so estimate median as mean of two either side.
  156. median = ( 1 + sortlist[cpi->common.MBs >> 1] +
  157. sortlist[(cpi->common.MBs >> 1) + 1] ) >> 1;
  158. cpi->activity_avg = median;
  159. vpx_free(sortlist);
  160. }
  161. #else
  162. // Simple mean for now
  163. cpi->activity_avg = (unsigned int)(activity_sum/cpi->common.MBs);
  164. #endif
  165. if (cpi->activity_avg < VP8_ACTIVITY_AVG_MIN)
  166. cpi->activity_avg = VP8_ACTIVITY_AVG_MIN;
  167. // Experimental code: return fixed value normalized for several clips
  168. if ( ALT_ACT_MEASURE )
  169. cpi->activity_avg = 100000;
  170. }
  171. #define USE_ACT_INDEX 0
  172. #define OUTPUT_NORM_ACT_STATS 0
  173. #if USE_ACT_INDEX
  174. // Calculate and activity index for each mb
  175. static void calc_activity_index( VP8_COMP *cpi, MACROBLOCK *x )
  176. {
  177. VP8_COMMON *const cm = & cpi->common;
  178. int mb_row, mb_col;
  179. int64_t act;
  180. int64_t a;
  181. int64_t b;
  182. #if OUTPUT_NORM_ACT_STATS
  183. FILE *f = fopen("norm_act.stt", "a");
  184. fprintf(f, "\n%12d\n", cpi->activity_avg );
  185. #endif
  186. // Reset pointers to start of activity map
  187. x->mb_activity_ptr = cpi->mb_activity_map;
  188. // Calculate normalized mb activity number.
  189. for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
  190. {
  191. // for each macroblock col in image
  192. for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
  193. {
  194. // Read activity from the map
  195. act = *(x->mb_activity_ptr);
  196. // Calculate a normalized activity number
  197. a = act + 4*cpi->activity_avg;
  198. b = 4*act + cpi->activity_avg;
  199. if ( b >= a )
  200. *(x->activity_ptr) = (int)((b + (a>>1))/a) - 1;
  201. else
  202. *(x->activity_ptr) = 1 - (int)((a + (b>>1))/b);
  203. #if OUTPUT_NORM_ACT_STATS
  204. fprintf(f, " %6d", *(x->mb_activity_ptr));
  205. #endif
  206. // Increment activity map pointers
  207. x->mb_activity_ptr++;
  208. }
  209. #if OUTPUT_NORM_ACT_STATS
  210. fprintf(f, "\n");
  211. #endif
  212. }
  213. #if OUTPUT_NORM_ACT_STATS
  214. fclose(f);
  215. #endif
  216. }
  217. #endif
  218. // Loop through all MBs. Note activity of each, average activity and
  219. // calculate a normalized activity for each
  220. static void build_activity_map( VP8_COMP *cpi )
  221. {
  222. MACROBLOCK *const x = & cpi->mb;
  223. MACROBLOCKD *xd = &x->e_mbd;
  224. VP8_COMMON *const cm = & cpi->common;
  225. #if ALT_ACT_MEASURE
  226. YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
  227. int recon_yoffset;
  228. int recon_y_stride = new_yv12->y_stride;
  229. #endif
  230. int mb_row, mb_col;
  231. unsigned int mb_activity;
  232. int64_t activity_sum = 0;
  233. // for each macroblock row in image
  234. for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
  235. {
  236. #if ALT_ACT_MEASURE
  237. // reset above block coeffs
  238. xd->up_available = (mb_row != 0);
  239. recon_yoffset = (mb_row * recon_y_stride * 16);
  240. #endif
  241. // for each macroblock col in image
  242. for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
  243. {
  244. #if ALT_ACT_MEASURE
  245. xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset;
  246. xd->left_available = (mb_col != 0);
  247. recon_yoffset += 16;
  248. #endif
  249. //Copy current mb to a buffer
  250. RECON_INVOKE(&xd->rtcd->recon, copy16x16)(x->src.y_buffer, x->src.y_stride, x->thismb, 16);
  251. // measure activity
  252. mb_activity = mb_activity_measure( cpi, x, mb_row, mb_col );
  253. // Keep frame sum
  254. activity_sum += mb_activity;
  255. // Store MB level activity details.
  256. *x->mb_activity_ptr = mb_activity;
  257. // Increment activity map pointer
  258. x->mb_activity_ptr++;
  259. // adjust to the next column of source macroblocks
  260. x->src.y_buffer += 16;
  261. }
  262. // adjust to the next row of mbs
  263. x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
  264. #if ALT_ACT_MEASURE
  265. //extend the recon for intra prediction
  266. vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16,
  267. xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
  268. #endif
  269. }
  270. // Calculate an "average" MB activity
  271. calc_av_activity(cpi, activity_sum);
  272. #if USE_ACT_INDEX
  273. // Calculate an activity index number of each mb
  274. calc_activity_index( cpi, x );
  275. #endif
  276. }
  277. // Macroblock activity masking
  278. void vp8_activity_masking(VP8_COMP *cpi, MACROBLOCK *x)
  279. {
  280. #if USE_ACT_INDEX
  281. x->rdmult += *(x->mb_activity_ptr) * (x->rdmult >> 2);
  282. x->errorperbit = x->rdmult * 100 /(110 * x->rddiv);
  283. x->errorperbit += (x->errorperbit==0);
  284. #else
  285. int64_t a;
  286. int64_t b;
  287. int64_t act = *(x->mb_activity_ptr);
  288. // Apply the masking to the RD multiplier.
  289. a = act + (2*cpi->activity_avg);
  290. b = (2*act) + cpi->activity_avg;
  291. x->rdmult = (unsigned int)(((int64_t)x->rdmult*b + (a>>1))/a);
  292. x->errorperbit = x->rdmult * 100 /(110 * x->rddiv);
  293. x->errorperbit += (x->errorperbit==0);
  294. #endif
  295. // Activity based Zbin adjustment
  296. adjust_act_zbin(cpi, x);
  297. }
  298. static
  299. void encode_mb_row(VP8_COMP *cpi,
  300. VP8_COMMON *cm,
  301. int mb_row,
  302. MACROBLOCK *x,
  303. MACROBLOCKD *xd,
  304. TOKENEXTRA **tp,
  305. int *segment_counts,
  306. int *totalrate)
  307. {
  308. int i;
  309. int recon_yoffset, recon_uvoffset;
  310. int mb_col;
  311. int ref_fb_idx = cm->lst_fb_idx;
  312. int dst_fb_idx = cm->new_fb_idx;
  313. int recon_y_stride = cm->yv12_fb[ref_fb_idx].y_stride;
  314. int recon_uv_stride = cm->yv12_fb[ref_fb_idx].uv_stride;
  315. int map_index = (mb_row * cpi->common.mb_cols);
  316. #if CONFIG_MULTITHREAD
  317. const int nsync = cpi->mt_sync_range;
  318. const int rightmost_col = cm->mb_cols - 1;
  319. volatile const int *last_row_current_mb_col;
  320. if ((cpi->b_multi_threaded != 0) && (mb_row != 0))
  321. last_row_current_mb_col = &cpi->mt_current_mb_col[mb_row - 1];
  322. else
  323. last_row_current_mb_col = &rightmost_col;
  324. #endif
  325. // reset above block coeffs
  326. xd->above_context = cm->above_context;
  327. xd->up_available = (mb_row != 0);
  328. recon_yoffset = (mb_row * recon_y_stride * 16);
  329. recon_uvoffset = (mb_row * recon_uv_stride * 8);
  330. cpi->tplist[mb_row].start = *tp;
  331. //printf("Main mb_row = %d\n", mb_row);
  332. // Distance of Mb to the top & bottom edges, specified in 1/8th pel
  333. // units as they are always compared to values that are in 1/8th pel units
  334. xd->mb_to_top_edge = -((mb_row * 16) << 3);
  335. xd->mb_to_bottom_edge = ((cm->mb_rows - 1 - mb_row) * 16) << 3;
  336. // Set up limit values for vertical motion vector components
  337. // to prevent them extending beyond the UMV borders
  338. x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
  339. x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16)
  340. + (VP8BORDERINPIXELS - 16);
  341. // Set the mb activity pointer to the start of the row.
  342. x->mb_activity_ptr = &cpi->mb_activity_map[map_index];
  343. // for each macroblock col in image
  344. for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
  345. {
  346. // Distance of Mb to the left & right edges, specified in
  347. // 1/8th pel units as they are always compared to values
  348. // that are in 1/8th pel units
  349. xd->mb_to_left_edge = -((mb_col * 16) << 3);
  350. xd->mb_to_right_edge = ((cm->mb_cols - 1 - mb_col) * 16) << 3;
  351. // Set up limit values for horizontal motion vector components
  352. // to prevent them extending beyond the UMV borders
  353. x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
  354. x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16)
  355. + (VP8BORDERINPIXELS - 16);
  356. xd->dst.y_buffer = cm->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset;
  357. xd->dst.u_buffer = cm->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset;
  358. xd->dst.v_buffer = cm->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset;
  359. xd->left_available = (mb_col != 0);
  360. x->rddiv = cpi->RDDIV;
  361. x->rdmult = cpi->RDMULT;
  362. //Copy current mb to a buffer
  363. RECON_INVOKE(&xd->rtcd->recon, copy16x16)(x->src.y_buffer, x->src.y_stride, x->thismb, 16);
  364. #if CONFIG_MULTITHREAD
  365. if ((cpi->b_multi_threaded != 0) && (mb_row != 0))
  366. {
  367. if ((mb_col & (nsync - 1)) == 0)
  368. {
  369. while (mb_col > (*last_row_current_mb_col - nsync)
  370. && (*last_row_current_mb_col) != (cm->mb_cols - 1))
  371. {
  372. x86_pause_hint();
  373. thread_sleep(0);
  374. }
  375. }
  376. }
  377. #endif
  378. if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
  379. vp8_activity_masking(cpi, x);
  380. // Is segmentation enabled
  381. // MB level adjutment to quantizer
  382. if (xd->segmentation_enabled)
  383. {
  384. // Code to set segment id in xd->mbmi.segment_id for current MB (with range checking)
  385. if (cpi->segmentation_map[map_index+mb_col] <= 3)
  386. xd->mode_info_context->mbmi.segment_id = cpi->segmentation_map[map_index+mb_col];
  387. else
  388. xd->mode_info_context->mbmi.segment_id = 0;
  389. vp8cx_mb_init_quantizer(cpi, x);
  390. }
  391. else
  392. xd->mode_info_context->mbmi.segment_id = 0; // Set to Segment 0 by default
  393. x->active_ptr = cpi->active_map + map_index + mb_col;
  394. if (cm->frame_type == KEY_FRAME)
  395. {
  396. *totalrate += vp8cx_encode_intra_macro_block(cpi, x, tp);
  397. #ifdef MODE_STATS
  398. y_modes[xd->mbmi.mode] ++;
  399. #endif
  400. }
  401. else
  402. {
  403. *totalrate += vp8cx_encode_inter_macroblock(cpi, x, tp, recon_yoffset, recon_uvoffset);
  404. #ifdef MODE_STATS
  405. inter_y_modes[xd->mbmi.mode] ++;
  406. if (xd->mbmi.mode == SPLITMV)
  407. {
  408. int b;
  409. for (b = 0; b < xd->mbmi.partition_count; b++)
  410. {
  411. inter_b_modes[x->partition->bmi[b].mode] ++;
  412. }
  413. }
  414. #endif
  415. // Count of last ref frame 0,0 useage
  416. if ((xd->mode_info_context->mbmi.mode == ZEROMV) && (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME))
  417. cpi->inter_zz_count ++;
  418. // Special case code for cyclic refresh
  419. // If cyclic update enabled then copy xd->mbmi.segment_id; (which may have been updated based on mode
  420. // during vp8cx_encode_inter_macroblock()) back into the global sgmentation map
  421. if (cpi->cyclic_refresh_mode_enabled && xd->segmentation_enabled)
  422. {
  423. cpi->segmentation_map[map_index+mb_col] = xd->mode_info_context->mbmi.segment_id;
  424. // 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):
  425. // 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)
  426. // else mark it as dirty (1).
  427. if (xd->mode_info_context->mbmi.segment_id)
  428. cpi->cyclic_refresh_map[map_index+mb_col] = -1;
  429. else if ((xd->mode_info_context->mbmi.mode == ZEROMV) && (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME))
  430. {
  431. if (cpi->cyclic_refresh_map[map_index+mb_col] == 1)
  432. cpi->cyclic_refresh_map[map_index+mb_col] = 0;
  433. }
  434. else
  435. cpi->cyclic_refresh_map[map_index+mb_col] = 1;
  436. }
  437. }
  438. cpi->tplist[mb_row].stop = *tp;
  439. // Increment pointer into gf useage flags structure.
  440. x->gf_active_ptr++;
  441. // Increment the activity mask pointers.
  442. x->mb_activity_ptr++;
  443. /* save the block info */
  444. for (i = 0; i < 16; i++)
  445. xd->mode_info_context->bmi[i] = xd->block[i].bmi;
  446. // adjust to the next column of macroblocks
  447. x->src.y_buffer += 16;
  448. x->src.u_buffer += 8;
  449. x->src.v_buffer += 8;
  450. recon_yoffset += 16;
  451. recon_uvoffset += 8;
  452. // Keep track of segment useage
  453. segment_counts[xd->mode_info_context->mbmi.segment_id] ++;
  454. // skip to next mb
  455. xd->mode_info_context++;
  456. x->partition_info++;
  457. xd->above_context++;
  458. #if CONFIG_MULTITHREAD
  459. if (cpi->b_multi_threaded != 0)
  460. {
  461. cpi->mt_current_mb_col[mb_row] = mb_col;
  462. }
  463. #endif
  464. }
  465. //extend the recon for intra prediction
  466. vp8_extend_mb_row(
  467. &cm->yv12_fb[dst_fb_idx],
  468. xd->dst.y_buffer + 16,
  469. xd->dst.u_buffer + 8,
  470. xd->dst.v_buffer + 8);
  471. // this is to account for the border
  472. xd->mode_info_context++;
  473. x->partition_info++;
  474. #if CONFIG_MULTITHREAD
  475. if ((cpi->b_multi_threaded != 0) && (mb_row == cm->mb_rows - 1))
  476. {
  477. sem_post(&cpi->h_event_end_encoding); /* signal frame encoding end */
  478. }
  479. #endif
  480. }
  481. void init_encode_frame_mb_context(VP8_COMP *cpi)
  482. {
  483. MACROBLOCK *const x = & cpi->mb;
  484. VP8_COMMON *const cm = & cpi->common;
  485. MACROBLOCKD *const xd = & x->e_mbd;
  486. // GF active flags data structure
  487. x->gf_active_ptr = (signed char *)cpi->gf_active_flags;
  488. // Activity map pointer
  489. x->mb_activity_ptr = cpi->mb_activity_map;
  490. x->vector_range = 32;
  491. x->act_zbin_adj = 0;
  492. x->partition_info = x->pi;
  493. xd->mode_info_context = cm->mi;
  494. xd->mode_info_stride = cm->mode_info_stride;
  495. xd->frame_type = cm->frame_type;
  496. xd->frames_since_golden = cm->frames_since_golden;
  497. xd->frames_till_alt_ref_frame = cm->frames_till_alt_ref_frame;
  498. // reset intra mode contexts
  499. if (cm->frame_type == KEY_FRAME)
  500. vp8_init_mbmode_probs(cm);
  501. // Copy data over into macro block data sturctures.
  502. x->src = * cpi->Source;
  503. xd->pre = cm->yv12_fb[cm->lst_fb_idx];
  504. xd->dst = cm->yv12_fb[cm->new_fb_idx];
  505. // set up frame for intra coded blocks
  506. vp8_setup_intra_recon(&cm->yv12_fb[cm->new_fb_idx]);
  507. vp8_build_block_offsets(x);
  508. vp8_setup_block_dptrs(&x->e_mbd);
  509. vp8_setup_block_ptrs(x);
  510. xd->mode_info_context->mbmi.mode = DC_PRED;
  511. xd->mode_info_context->mbmi.uv_mode = DC_PRED;
  512. xd->left_context = &cm->left_context;
  513. vp8_zero(cpi->count_mb_ref_frame_usage)
  514. vp8_zero(cpi->ymode_count)
  515. vp8_zero(cpi->uv_mode_count)
  516. x->mvc = cm->fc.mvc;
  517. vpx_memset(cm->above_context, 0,
  518. sizeof(ENTROPY_CONTEXT_PLANES) * cm->mb_cols);
  519. xd->ref_frame_cost[INTRA_FRAME] = vp8_cost_zero(cpi->prob_intra_coded);
  520. // Special case treatment when GF and ARF are not sensible options for reference
  521. if (cpi->ref_frame_flags == VP8_LAST_FLAG)
  522. {
  523. xd->ref_frame_cost[LAST_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
  524. + vp8_cost_zero(255);
  525. xd->ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
  526. + vp8_cost_one(255)
  527. + vp8_cost_zero(128);
  528. xd->ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
  529. + vp8_cost_one(255)
  530. + vp8_cost_one(128);
  531. }
  532. else
  533. {
  534. xd->ref_frame_cost[LAST_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
  535. + vp8_cost_zero(cpi->prob_last_coded);
  536. xd->ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
  537. + vp8_cost_one(cpi->prob_last_coded)
  538. + vp8_cost_zero(cpi->prob_gf_coded);
  539. xd->ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
  540. + vp8_cost_one(cpi->prob_last_coded)
  541. + vp8_cost_one(cpi->prob_gf_coded);
  542. }
  543. }
  544. void vp8_encode_frame(VP8_COMP *cpi)
  545. {
  546. int mb_row;
  547. MACROBLOCK *const x = & cpi->mb;
  548. VP8_COMMON *const cm = & cpi->common;
  549. MACROBLOCKD *const xd = & x->e_mbd;
  550. TOKENEXTRA *tp = cpi->tok;
  551. int segment_counts[MAX_MB_SEGMENTS];
  552. int totalrate;
  553. vpx_memset(segment_counts, 0, sizeof(segment_counts));
  554. totalrate = 0;
  555. if (cpi->compressor_speed == 2)
  556. {
  557. if (cpi->oxcf.cpu_used < 0)
  558. cpi->Speed = -(cpi->oxcf.cpu_used);
  559. else
  560. vp8_auto_select_speed(cpi);
  561. }
  562. // Functions setup for all frame types so we can use MC in AltRef
  563. if (cm->mcomp_filter_type == SIXTAP)
  564. {
  565. xd->subpixel_predict = SUBPIX_INVOKE(
  566. &cpi->common.rtcd.subpix, sixtap4x4);
  567. xd->subpixel_predict8x4 = SUBPIX_INVOKE(
  568. &cpi->common.rtcd.subpix, sixtap8x4);
  569. xd->subpixel_predict8x8 = SUBPIX_INVOKE(
  570. &cpi->common.rtcd.subpix, sixtap8x8);
  571. xd->subpixel_predict16x16 = SUBPIX_INVOKE(
  572. &cpi->common.rtcd.subpix, sixtap16x16);
  573. }
  574. else
  575. {
  576. xd->subpixel_predict = SUBPIX_INVOKE(
  577. &cpi->common.rtcd.subpix, bilinear4x4);
  578. xd->subpixel_predict8x4 = SUBPIX_INVOKE(
  579. &cpi->common.rtcd.subpix, bilinear8x4);
  580. xd->subpixel_predict8x8 = SUBPIX_INVOKE(
  581. &cpi->common.rtcd.subpix, bilinear8x8);
  582. xd->subpixel_predict16x16 = SUBPIX_INVOKE(
  583. &cpi->common.rtcd.subpix, bilinear16x16);
  584. }
  585. // Reset frame count of inter 0,0 motion vector useage.
  586. cpi->inter_zz_count = 0;
  587. vpx_memset(segment_counts, 0, sizeof(segment_counts));
  588. cpi->prediction_error = 0;
  589. cpi->intra_error = 0;
  590. cpi->skip_true_count = 0;
  591. cpi->skip_false_count = 0;
  592. #if 0
  593. // Experimental code
  594. cpi->frame_distortion = 0;
  595. cpi->last_mb_distortion = 0;
  596. #endif
  597. xd->mode_info_context = cm->mi;
  598. vp8_zero(cpi->MVcount);
  599. vp8_zero(cpi->coef_counts);
  600. vp8cx_frame_init_quantizer(cpi);
  601. vp8_initialize_rd_consts(cpi,
  602. vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q));
  603. vp8cx_initialize_me_consts(cpi, cm->base_qindex);
  604. if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
  605. {
  606. // Initialize encode frame context.
  607. init_encode_frame_mb_context(cpi);
  608. // Build a frame level activity map
  609. build_activity_map(cpi);
  610. }
  611. // re-initencode frame context.
  612. init_encode_frame_mb_context(cpi);
  613. {
  614. struct vpx_usec_timer emr_timer;
  615. vpx_usec_timer_start(&emr_timer);
  616. #if CONFIG_MULTITHREAD
  617. if (cpi->b_multi_threaded)
  618. {
  619. int i;
  620. vp8cx_init_mbrthread_data(cpi, x, cpi->mb_row_ei, 1, cpi->encoding_thread_count);
  621. for (i = 0; i < cm->mb_rows; i++)
  622. cpi->mt_current_mb_col[i] = -1;
  623. for (i = 0; i < cpi->encoding_thread_count; i++)
  624. {
  625. sem_post(&cpi->h_event_start_encoding[i]);
  626. }
  627. for (mb_row = 0; mb_row < cm->mb_rows; mb_row += (cpi->encoding_thread_count + 1))
  628. {
  629. vp8_zero(cm->left_context)
  630. tp = cpi->tok + mb_row * (cm->mb_cols * 16 * 24);
  631. encode_mb_row(cpi, cm, mb_row, x, xd, &tp, segment_counts, &totalrate);
  632. // adjust to the next row of mbs
  633. x->src.y_buffer += 16 * x->src.y_stride * (cpi->encoding_thread_count + 1) - 16 * cm->mb_cols;
  634. x->src.u_buffer += 8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - 8 * cm->mb_cols;
  635. x->src.v_buffer += 8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - 8 * cm->mb_cols;
  636. xd->mode_info_context += xd->mode_info_stride * cpi->encoding_thread_count;
  637. x->partition_info += xd->mode_info_stride * cpi->encoding_thread_count;
  638. x->gf_active_ptr += cm->mb_cols * cpi->encoding_thread_count;
  639. }
  640. sem_wait(&cpi->h_event_end_encoding); /* wait for other threads to finish */
  641. cpi->tok_count = 0;
  642. for (mb_row = 0; mb_row < cm->mb_rows; mb_row ++)
  643. {
  644. cpi->tok_count += cpi->tplist[mb_row].stop - cpi->tplist[mb_row].start;
  645. }
  646. if (xd->segmentation_enabled)
  647. {
  648. int i, j;
  649. if (xd->segmentation_enabled)
  650. {
  651. for (i = 0; i < cpi->encoding_thread_count; i++)
  652. {
  653. for (j = 0; j < 4; j++)
  654. segment_counts[j] += cpi->mb_row_ei[i].segment_counts[j];
  655. }
  656. }
  657. }
  658. for (i = 0; i < cpi->encoding_thread_count; i++)
  659. {
  660. totalrate += cpi->mb_row_ei[i].totalrate;
  661. }
  662. }
  663. else
  664. #endif
  665. {
  666. // for each macroblock row in image
  667. for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
  668. {
  669. vp8_zero(cm->left_context)
  670. encode_mb_row(cpi, cm, mb_row, x, xd, &tp, segment_counts, &totalrate);
  671. // adjust to the next row of mbs
  672. x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
  673. x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
  674. x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
  675. }
  676. cpi->tok_count = tp - cpi->tok;
  677. }
  678. vpx_usec_timer_mark(&emr_timer);
  679. cpi->time_encode_mb_row += vpx_usec_timer_elapsed(&emr_timer);
  680. }
  681. // Work out the segment probabilites if segmentation is enabled
  682. if (xd->segmentation_enabled)
  683. {
  684. int tot_count;
  685. int i;
  686. // Set to defaults
  687. vpx_memset(xd->mb_segment_tree_probs, 255 , sizeof(xd->mb_segment_tree_probs));
  688. tot_count = segment_counts[0] + segment_counts[1] + segment_counts[2] + segment_counts[3];
  689. if (tot_count)
  690. {
  691. xd->mb_segment_tree_probs[0] = ((segment_counts[0] + segment_counts[1]) * 255) / tot_count;
  692. tot_count = segment_counts[0] + segment_counts[1];
  693. if (tot_count > 0)
  694. {
  695. xd->mb_segment_tree_probs[1] = (segment_counts[0] * 255) / tot_count;
  696. }
  697. tot_count = segment_counts[2] + segment_counts[3];
  698. if (tot_count > 0)
  699. xd->mb_segment_tree_probs[2] = (segment_counts[2] * 255) / tot_count;
  700. // Zero probabilities not allowed
  701. for (i = 0; i < MB_FEATURE_TREE_PROBS; i ++)
  702. {
  703. if (xd->mb_segment_tree_probs[i] == 0)
  704. xd->mb_segment_tree_probs[i] = 1;
  705. }
  706. }
  707. }
  708. // 256 rate units to the bit
  709. cpi->projected_frame_size = totalrate >> 8; // projected_frame_size in units of BYTES
  710. // Make a note of the percentage MBs coded Intra.
  711. if (cm->frame_type == KEY_FRAME)
  712. {
  713. cpi->this_frame_percent_intra = 100;
  714. }
  715. else
  716. {
  717. int tot_modes;
  718. tot_modes = cpi->count_mb_ref_frame_usage[INTRA_FRAME]
  719. + cpi->count_mb_ref_frame_usage[LAST_FRAME]
  720. + cpi->count_mb_ref_frame_usage[GOLDEN_FRAME]
  721. + cpi->count_mb_ref_frame_usage[ALTREF_FRAME];
  722. if (tot_modes)
  723. cpi->this_frame_percent_intra = cpi->count_mb_ref_frame_usage[INTRA_FRAME] * 100 / tot_modes;
  724. }
  725. #if 0
  726. {
  727. int cnt = 0;
  728. int flag[2] = {0, 0};
  729. for (cnt = 0; cnt < MVPcount; cnt++)
  730. {
  731. if (cm->fc.pre_mvc[0][cnt] != cm->fc.mvc[0][cnt])
  732. {
  733. flag[0] = 1;
  734. vpx_memcpy(cm->fc.pre_mvc[0], cm->fc.mvc[0], MVPcount);
  735. break;
  736. }
  737. }
  738. for (cnt = 0; cnt < MVPcount; cnt++)
  739. {
  740. if (cm->fc.pre_mvc[1][cnt] != cm->fc.mvc[1][cnt])
  741. {
  742. flag[1] = 1;
  743. vpx_memcpy(cm->fc.pre_mvc[1], cm->fc.mvc[1], MVPcount);
  744. break;
  745. }
  746. }
  747. if (flag[0] || flag[1])
  748. vp8_build_component_cost_table(cpi->mb.mvcost, (const MV_CONTEXT *) cm->fc.mvc, flag);
  749. }
  750. #endif
  751. // Adjust the projected reference frame useage probability numbers to reflect
  752. // what we have just seen. This may be usefull when we make multiple itterations
  753. // of the recode loop rather than continuing to use values from the previous frame.
  754. if ((cm->frame_type != KEY_FRAME) && !cm->refresh_alt_ref_frame && !cm->refresh_golden_frame)
  755. {
  756. const int *const rfct = cpi->count_mb_ref_frame_usage;
  757. const int rf_intra = rfct[INTRA_FRAME];
  758. const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
  759. if ((rf_intra + rf_inter) > 0)
  760. {
  761. cpi->prob_intra_coded = (rf_intra * 255) / (rf_intra + rf_inter);
  762. if (cpi->prob_intra_coded < 1)
  763. cpi->prob_intra_coded = 1;
  764. if ((cm->frames_since_golden > 0) || cpi->source_alt_ref_active)
  765. {
  766. cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
  767. if (cpi->prob_last_coded < 1)
  768. cpi->prob_last_coded = 1;
  769. cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
  770. ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
  771. if (cpi->prob_gf_coded < 1)
  772. cpi->prob_gf_coded = 1;
  773. }
  774. }
  775. }
  776. #if 0
  777. // Keep record of the total distortion this time around for future use
  778. cpi->last_frame_distortion = cpi->frame_distortion;
  779. #endif
  780. }
  781. void vp8_setup_block_ptrs(MACROBLOCK *x)
  782. {
  783. int r, c;
  784. int i;
  785. for (r = 0; r < 4; r++)
  786. {
  787. for (c = 0; c < 4; c++)
  788. {
  789. x->block[r*4+c].src_diff = x->src_diff + r * 4 * 16 + c * 4;
  790. }
  791. }
  792. for (r = 0; r < 2; r++)
  793. {
  794. for (c = 0; c < 2; c++)
  795. {
  796. x->block[16 + r*2+c].src_diff = x->src_diff + 256 + r * 4 * 8 + c * 4;
  797. }
  798. }
  799. for (r = 0; r < 2; r++)
  800. {
  801. for (c = 0; c < 2; c++)
  802. {
  803. x->block[20 + r*2+c].src_diff = x->src_diff + 320 + r * 4 * 8 + c * 4;
  804. }
  805. }
  806. x->block[24].src_diff = x->src_diff + 384;
  807. for (i = 0; i < 25; i++)
  808. {
  809. x->block[i].coeff = x->coeff + i * 16;
  810. }
  811. }
  812. void vp8_build_block_offsets(MACROBLOCK *x)
  813. {
  814. int block = 0;
  815. int br, bc;
  816. vp8_build_block_doffsets(&x->e_mbd);
  817. // y blocks
  818. x->thismb_ptr = &x->thismb[0];
  819. for (br = 0; br < 4; br++)
  820. {
  821. for (bc = 0; bc < 4; bc++)
  822. {
  823. BLOCK *this_block = &x->block[block];
  824. //this_block->base_src = &x->src.y_buffer;
  825. //this_block->src_stride = x->src.y_stride;
  826. //this_block->src = 4 * br * this_block->src_stride + 4 * bc;
  827. this_block->base_src = &x->thismb_ptr;
  828. this_block->src_stride = 16;
  829. this_block->src = 4 * br * 16 + 4 * bc;
  830. ++block;
  831. }
  832. }
  833. // u blocks
  834. for (br = 0; br < 2; br++)
  835. {
  836. for (bc = 0; bc < 2; bc++)
  837. {
  838. BLOCK *this_block = &x->block[block];
  839. this_block->base_src = &x->src.u_buffer;
  840. this_block->src_stride = x->src.uv_stride;
  841. this_block->src = 4 * br * this_block->src_stride + 4 * bc;
  842. ++block;
  843. }
  844. }
  845. // v blocks
  846. for (br = 0; br < 2; br++)
  847. {
  848. for (bc = 0; bc < 2; bc++)
  849. {
  850. BLOCK *this_block = &x->block[block];
  851. this_block->base_src = &x->src.v_buffer;
  852. this_block->src_stride = x->src.uv_stride;
  853. this_block->src = 4 * br * this_block->src_stride + 4 * bc;
  854. ++block;
  855. }
  856. }
  857. }
  858. static void sum_intra_stats(VP8_COMP *cpi, MACROBLOCK *x)
  859. {
  860. const MACROBLOCKD *xd = & x->e_mbd;
  861. const MB_PREDICTION_MODE m = xd->mode_info_context->mbmi.mode;
  862. const MB_PREDICTION_MODE uvm = xd->mode_info_context->mbmi.uv_mode;
  863. #ifdef MODE_STATS
  864. const int is_key = cpi->common.frame_type == KEY_FRAME;
  865. ++ (is_key ? uv_modes : inter_uv_modes)[uvm];
  866. if (m == B_PRED)
  867. {
  868. unsigned int *const bct = is_key ? b_modes : inter_b_modes;
  869. int b = 0;
  870. do
  871. {
  872. ++ bct[xd->block[b].bmi.mode];
  873. }
  874. while (++b < 16);
  875. }
  876. #endif
  877. ++cpi->ymode_count[m];
  878. ++cpi->uv_mode_count[uvm];
  879. }
  880. // Experimental stub function to create a per MB zbin adjustment based on
  881. // some previously calculated measure of MB activity.
  882. static void adjust_act_zbin( VP8_COMP *cpi, MACROBLOCK *x )
  883. {
  884. #if USE_ACT_INDEX
  885. x->act_zbin_adj = *(x->mb_activity_ptr);
  886. #else
  887. int64_t a;
  888. int64_t b;
  889. int64_t act = *(x->mb_activity_ptr);
  890. // Apply the masking to the RD multiplier.
  891. a = act + 4*cpi->activity_avg;
  892. b = 4*act + cpi->activity_avg;
  893. if ( act > cpi->activity_avg )
  894. x->act_zbin_adj = (int)(((int64_t)b + (a>>1))/a) - 1;
  895. else
  896. x->act_zbin_adj = 1 - (int)(((int64_t)a + (b>>1))/b);
  897. #endif
  898. }
  899. int vp8cx_encode_intra_macro_block(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t)
  900. {
  901. int rate;
  902. if (cpi->sf.RD && cpi->compressor_speed != 2)
  903. vp8_rd_pick_intra_mode(cpi, x, &rate);
  904. else
  905. vp8_pick_intra_mode(cpi, x, &rate);
  906. if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
  907. {
  908. adjust_act_zbin( cpi, x );
  909. vp8_update_zbin_extra(cpi, x);
  910. }
  911. if (x->e_mbd.mode_info_context->mbmi.mode == B_PRED)
  912. vp8_encode_intra4x4mby(IF_RTCD(&cpi->rtcd), x);
  913. else
  914. vp8_encode_intra16x16mby(IF_RTCD(&cpi->rtcd), x);
  915. vp8_encode_intra16x16mbuv(IF_RTCD(&cpi->rtcd), x);
  916. sum_intra_stats(cpi, x);
  917. vp8_tokenize_mb(cpi, &x->e_mbd, t);
  918. return rate;
  919. }
  920. #ifdef SPEEDSTATS
  921. extern int cnt_pm;
  922. #endif
  923. extern void vp8_fix_contexts(MACROBLOCKD *x);
  924. int vp8cx_encode_inter_macroblock
  925. (
  926. VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t,
  927. int recon_yoffset, int recon_uvoffset
  928. )
  929. {
  930. MACROBLOCKD *const xd = &x->e_mbd;
  931. int intra_error = 0;
  932. int rate;
  933. int distortion;
  934. x->skip = 0;
  935. if (xd->segmentation_enabled)
  936. x->encode_breakout = cpi->segment_encode_breakout[xd->mode_info_context->mbmi.segment_id];
  937. else
  938. x->encode_breakout = cpi->oxcf.encode_breakout;
  939. if (cpi->sf.RD)
  940. {
  941. int zbin_mode_boost_enabled = cpi->zbin_mode_boost_enabled;
  942. /* Are we using the fast quantizer for the mode selection? */
  943. if(cpi->sf.use_fastquant_for_pick)
  944. {
  945. cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
  946. fastquantb);
  947. cpi->mb.quantize_b_pair = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
  948. fastquantb_pair);
  949. /* the fast quantizer does not use zbin_extra, so
  950. * do not recalculate */
  951. cpi->zbin_mode_boost_enabled = 0;
  952. }
  953. vp8_rd_pick_inter_mode(cpi, x, recon_yoffset, recon_uvoffset, &rate,
  954. &distortion, &intra_error);
  955. /* switch back to the regular quantizer for the encode */
  956. if (cpi->sf.improved_quant)
  957. {
  958. cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
  959. quantb);
  960. cpi->mb.quantize_b_pair = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
  961. quantb_pair);
  962. }
  963. /* restore cpi->zbin_mode_boost_enabled */
  964. cpi->zbin_mode_boost_enabled = zbin_mode_boost_enabled;
  965. }
  966. else
  967. vp8_pick_inter_mode(cpi, x, recon_yoffset, recon_uvoffset, &rate,
  968. &distortion, &intra_error);
  969. cpi->prediction_error += distortion;
  970. cpi->intra_error += intra_error;
  971. if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
  972. {
  973. // Adjust the zbin based on this MB rate.
  974. adjust_act_zbin( cpi, x );
  975. }
  976. #if 0
  977. // Experimental RD code
  978. cpi->frame_distortion += distortion;
  979. cpi->last_mb_distortion = distortion;
  980. #endif
  981. // MB level adjutment to quantizer setup
  982. if (xd->segmentation_enabled)
  983. {
  984. // If cyclic update enabled
  985. if (cpi->cyclic_refresh_mode_enabled)
  986. {
  987. // Clear segment_id back to 0 if not coded (last frame 0,0)
  988. if ((xd->mode_info_context->mbmi.segment_id == 1) &&
  989. ((xd->mode_info_context->mbmi.ref_frame != LAST_FRAME) || (xd->mode_info_context->mbmi.mode != ZEROMV)))
  990. {
  991. xd->mode_info_context->mbmi.segment_id = 0;
  992. /* segment_id changed, so update */
  993. vp8cx_mb_init_quantizer(cpi, x);
  994. }
  995. }
  996. }
  997. {
  998. // Experimental code. Special case for gf and arf zeromv modes.
  999. // Increase zbin size to supress noise
  1000. cpi->zbin_mode_boost = 0;
  1001. if (cpi->zbin_mode_boost_enabled)
  1002. {
  1003. if ( xd->mode_info_context->mbmi.ref_frame != INTRA_FRAME )
  1004. {
  1005. if (xd->mode_info_context->mbmi.mode == ZEROMV)
  1006. {
  1007. if (xd->mode_info_context->mbmi.ref_frame != LAST_FRAME)
  1008. cpi->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST;
  1009. else
  1010. cpi->zbin_mode_boost = LF_ZEROMV_ZBIN_BOOST;
  1011. }
  1012. else if (xd->mode_info_context->mbmi.mode == SPLITMV)
  1013. cpi->zbin_mode_boost = 0;
  1014. else
  1015. cpi->zbin_mode_boost = MV_ZBIN_BOOST;
  1016. }
  1017. }
  1018. vp8_update_zbin_extra(cpi, x);
  1019. }
  1020. cpi->count_mb_ref_frame_usage[xd->mode_info_context->mbmi.ref_frame] ++;
  1021. if (xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME)
  1022. {
  1023. vp8_encode_intra16x16mbuv(IF_RTCD(&cpi->rtcd), x);
  1024. if (xd->mode_info_context->mbmi.mode == B_PRED)
  1025. {
  1026. vp8_encode_intra4x4mby(IF_RTCD(&cpi->rtcd), x);
  1027. }
  1028. else
  1029. {
  1030. vp8_encode_intra16x16mby(IF_RTCD(&cpi->rtcd), x);
  1031. }
  1032. sum_intra_stats(cpi, x);
  1033. }
  1034. else
  1035. {
  1036. int ref_fb_idx;
  1037. vp8_build_uvmvs(xd, cpi->common.full_pixel);
  1038. if (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME)
  1039. ref_fb_idx = cpi->common.lst_fb_idx;
  1040. else if (xd->mode_info_context->mbmi.ref_frame == GOLDEN_FRAME)
  1041. ref_fb_idx = cpi->common.gld_fb_idx;
  1042. else
  1043. ref_fb_idx = cpi->common.alt_fb_idx;
  1044. xd->pre.y_buffer = cpi->common.yv12_fb[ref_fb_idx].y_buffer + recon_yoffset;
  1045. xd->pre.u_buffer = cpi->common.yv12_fb[ref_fb_idx].u_buffer + recon_uvoffset;
  1046. xd->pre.v_buffer = cpi->common.yv12_fb[ref_fb_idx].v_buffer + recon_uvoffset;
  1047. if (!x->skip)
  1048. {
  1049. vp8_encode_inter16x16(IF_RTCD(&cpi->rtcd), x);
  1050. // Clear mb_skip_coeff if mb_no_coeff_skip is not set
  1051. if (!cpi->common.mb_no_coeff_skip)
  1052. xd->mode_info_context->mbmi.mb_skip_coeff = 0;
  1053. }
  1054. else
  1055. vp8_build_inter16x16_predictors_mb(xd, xd->dst.y_buffer,
  1056. xd->dst.u_buffer, xd->dst.v_buffer,
  1057. xd->dst.y_stride, xd->dst.uv_stride);
  1058. }
  1059. if (!x->skip)
  1060. vp8_tokenize_mb(cpi, xd, t);
  1061. else
  1062. {
  1063. if (cpi->common.mb_no_coeff_skip)
  1064. {
  1065. xd->mode_info_context->mbmi.mb_skip_coeff = 1;
  1066. cpi->skip_true_count ++;
  1067. vp8_fix_contexts(xd);
  1068. }
  1069. else
  1070. {
  1071. vp8_stuff_mb(cpi, xd, t);
  1072. xd->mode_info_context->mbmi.mb_skip_coeff = 0;
  1073. cpi->skip_false_count ++;
  1074. }
  1075. }
  1076. return rate;
  1077. }