/media/libvpx/vp8/encoder/onyx_if.c

http://github.com/zpao/v8monkey · C · 5152 lines · 3740 code · 972 blank · 440 comment · 740 complexity · 724943c382813bdcad79d2b2804f0307 MD5 · raw file

Large files are truncated click here to view the full 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_config.h"
  11. #include "vp8/common/onyxc_int.h"
  12. #include "onyx_int.h"
  13. #include "vp8/common/systemdependent.h"
  14. #include "quantize.h"
  15. #include "vp8/common/alloccommon.h"
  16. #include "mcomp.h"
  17. #include "firstpass.h"
  18. #include "psnr.h"
  19. #include "vpx_scale/vpxscale.h"
  20. #include "vp8/common/extend.h"
  21. #include "ratectrl.h"
  22. #include "vp8/common/quant_common.h"
  23. #include "segmentation.h"
  24. #include "vp8/common/g_common.h"
  25. #include "vpx_scale/yv12extend.h"
  26. #if CONFIG_POSTPROC
  27. #include "vp8/common/postproc.h"
  28. #endif
  29. #include "vpx_mem/vpx_mem.h"
  30. #include "vp8/common/swapyv12buffer.h"
  31. #include "vp8/common/threading.h"
  32. #include "vpx_ports/vpx_timer.h"
  33. #include "temporal_filter.h"
  34. #if ARCH_ARM
  35. #include "vpx_ports/arm.h"
  36. #endif
  37. #include <math.h>
  38. #include <stdio.h>
  39. #include <limits.h>
  40. #if CONFIG_RUNTIME_CPU_DETECT
  41. #define IF_RTCD(x) (x)
  42. #define RTCD(x) &cpi->common.rtcd.x
  43. #else
  44. #define IF_RTCD(x) NULL
  45. #define RTCD(x) NULL
  46. #endif
  47. extern void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
  48. extern void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val);
  49. extern void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
  50. extern void vp8_dmachine_specific_config(VP8_COMP *cpi);
  51. extern void vp8_cmachine_specific_config(VP8_COMP *cpi);
  52. extern void vp8_deblock_frame(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *post, int filt_lvl, int low_var_thresh, int flag);
  53. extern void print_parms(VP8_CONFIG *ocf, char *filenam);
  54. extern unsigned int vp8_get_processor_freq();
  55. extern void print_tree_update_probs();
  56. extern void vp8cx_create_encoder_threads(VP8_COMP *cpi);
  57. extern void vp8cx_remove_encoder_threads(VP8_COMP *cpi);
  58. #if HAVE_ARMV7
  59. extern void vp8_yv12_copy_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
  60. extern void vp8_yv12_copy_src_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
  61. #endif
  62. int vp8_estimate_entropy_savings(VP8_COMP *cpi);
  63. int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd);
  64. extern void vp8_temporal_filter_prepare_c(VP8_COMP *cpi, int distance);
  65. static void set_default_lf_deltas(VP8_COMP *cpi);
  66. extern const int vp8_gf_interval_table[101];
  67. #if CONFIG_INTERNAL_STATS
  68. #include "math.h"
  69. extern double vp8_calc_ssim
  70. (
  71. YV12_BUFFER_CONFIG *source,
  72. YV12_BUFFER_CONFIG *dest,
  73. int lumamask,
  74. double *weight,
  75. const vp8_variance_rtcd_vtable_t *rtcd
  76. );
  77. extern double vp8_calc_ssimg
  78. (
  79. YV12_BUFFER_CONFIG *source,
  80. YV12_BUFFER_CONFIG *dest,
  81. double *ssim_y,
  82. double *ssim_u,
  83. double *ssim_v,
  84. const vp8_variance_rtcd_vtable_t *rtcd
  85. );
  86. #endif
  87. #ifdef OUTPUT_YUV_SRC
  88. FILE *yuv_file;
  89. #endif
  90. #if 0
  91. FILE *framepsnr;
  92. FILE *kf_list;
  93. FILE *keyfile;
  94. #endif
  95. #if 0
  96. extern int skip_true_count;
  97. extern int skip_false_count;
  98. #endif
  99. #ifdef ENTROPY_STATS
  100. extern int intra_mode_stats[10][10][10];
  101. #endif
  102. #ifdef SPEEDSTATS
  103. unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  104. unsigned int tot_pm = 0;
  105. unsigned int cnt_pm = 0;
  106. unsigned int tot_ef = 0;
  107. unsigned int cnt_ef = 0;
  108. #endif
  109. #ifdef MODE_STATS
  110. extern unsigned __int64 Sectionbits[50];
  111. extern int y_modes[5] ;
  112. extern int uv_modes[4] ;
  113. extern int b_modes[10] ;
  114. extern int inter_y_modes[10] ;
  115. extern int inter_uv_modes[4] ;
  116. extern unsigned int inter_b_modes[15];
  117. #endif
  118. extern void (*vp8_short_fdct4x4)(short *input, short *output, int pitch);
  119. extern void (*vp8_short_fdct8x4)(short *input, short *output, int pitch);
  120. extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
  121. extern const int qrounding_factors[129];
  122. extern const int qzbin_factors[129];
  123. extern void vp8cx_init_quantizer(VP8_COMP *cpi);
  124. extern const int vp8cx_base_skip_false_prob[128];
  125. // Tables relating active max Q to active min Q
  126. static const int kf_low_motion_minq[QINDEX_RANGE] =
  127. {
  128. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  129. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  130. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  131. 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
  132. 3,3,3,3,3,3,4,4,4,5,5,5,5,5,6,6,
  133. 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
  134. 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
  135. 16,16,17,17,18,18,18,18,19,20,20,21,21,22,23,23
  136. };
  137. static const int kf_high_motion_minq[QINDEX_RANGE] =
  138. {
  139. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  140. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  141. 1,1,1,1,1,1,1,1,2,2,2,2,3,3,3,3,
  142. 3,3,3,3,4,4,4,4,5,5,5,5,5,5,6,6,
  143. 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
  144. 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
  145. 16,16,17,17,18,18,18,18,19,19,20,20,20,20,21,21,
  146. 21,21,22,22,23,23,24,25,25,26,26,27,28,28,29,30
  147. };
  148. static const int gf_low_motion_minq[QINDEX_RANGE] =
  149. {
  150. 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
  151. 3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,
  152. 7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,
  153. 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
  154. 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
  155. 27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,
  156. 35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,
  157. 43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58
  158. };
  159. static const int gf_mid_motion_minq[QINDEX_RANGE] =
  160. {
  161. 0,0,0,0,1,1,1,1,1,1,2,2,3,3,3,4,
  162. 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
  163. 9,10,10,10,10,11,11,11,12,12,12,12,13,13,13,14,
  164. 14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,
  165. 22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,
  166. 30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,
  167. 38,39,39,40,40,41,41,42,42,43,43,44,45,46,47,48,
  168. 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64
  169. };
  170. static const int gf_high_motion_minq[QINDEX_RANGE] =
  171. {
  172. 0,0,0,0,1,1,1,1,1,2,2,2,3,3,3,4,
  173. 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
  174. 9,10,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
  175. 17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,
  176. 25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,
  177. 33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,
  178. 41,41,42,42,43,44,45,46,47,48,49,50,51,52,53,54,
  179. 55,56,57,58,59,60,62,64,66,68,70,72,74,76,78,80
  180. };
  181. static const int inter_minq[QINDEX_RANGE] =
  182. {
  183. 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
  184. 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
  185. 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
  186. 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
  187. 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
  188. 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
  189. 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
  190. 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
  191. };
  192. void vp8_initialize()
  193. {
  194. static int init_done = 0;
  195. if (!init_done)
  196. {
  197. vp8_scale_machine_specific_config();
  198. vp8_initialize_common();
  199. //vp8_dmachine_specific_config();
  200. vp8_tokenize_initialize();
  201. init_done = 1;
  202. }
  203. }
  204. #ifdef PACKET_TESTING
  205. extern FILE *vpxlogc;
  206. #endif
  207. static void setup_features(VP8_COMP *cpi)
  208. {
  209. // Set up default state for MB feature flags
  210. cpi->mb.e_mbd.segmentation_enabled = 0;
  211. cpi->mb.e_mbd.update_mb_segmentation_map = 0;
  212. cpi->mb.e_mbd.update_mb_segmentation_data = 0;
  213. vpx_memset(cpi->mb.e_mbd.mb_segment_tree_probs, 255, sizeof(cpi->mb.e_mbd.mb_segment_tree_probs));
  214. vpx_memset(cpi->mb.e_mbd.segment_feature_data, 0, sizeof(cpi->mb.e_mbd.segment_feature_data));
  215. cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0;
  216. cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
  217. vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
  218. vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
  219. vpx_memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
  220. vpx_memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
  221. set_default_lf_deltas(cpi);
  222. }
  223. static void dealloc_compressor_data(VP8_COMP *cpi)
  224. {
  225. vpx_free(cpi->tplist);
  226. cpi->tplist = NULL;
  227. // Delete last frame MV storage buffers
  228. vpx_free(cpi->lfmv);
  229. cpi->lfmv = 0;
  230. vpx_free(cpi->lf_ref_frame_sign_bias);
  231. cpi->lf_ref_frame_sign_bias = 0;
  232. vpx_free(cpi->lf_ref_frame);
  233. cpi->lf_ref_frame = 0;
  234. // Delete sementation map
  235. vpx_free(cpi->segmentation_map);
  236. cpi->segmentation_map = 0;
  237. vpx_free(cpi->active_map);
  238. cpi->active_map = 0;
  239. vp8_de_alloc_frame_buffers(&cpi->common);
  240. vp8_yv12_de_alloc_frame_buffer(&cpi->last_frame_uf);
  241. vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
  242. #if VP8_TEMPORAL_ALT_REF
  243. vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
  244. #endif
  245. vp8_lookahead_destroy(cpi->lookahead);
  246. vpx_free(cpi->tok);
  247. cpi->tok = 0;
  248. // Structure used to monitor GF usage
  249. vpx_free(cpi->gf_active_flags);
  250. cpi->gf_active_flags = 0;
  251. // Activity mask based per mb zbin adjustments
  252. vpx_free(cpi->mb_activity_map);
  253. cpi->mb_activity_map = 0;
  254. vpx_free(cpi->mb_norm_activity_map);
  255. cpi->mb_norm_activity_map = 0;
  256. vpx_free(cpi->mb.pip);
  257. cpi->mb.pip = 0;
  258. #if !(CONFIG_REALTIME_ONLY)
  259. vpx_free(cpi->twopass.total_stats);
  260. cpi->twopass.total_stats = 0;
  261. vpx_free(cpi->twopass.this_frame_stats);
  262. cpi->twopass.this_frame_stats = 0;
  263. #endif
  264. }
  265. static void enable_segmentation(VP8_PTR ptr)
  266. {
  267. VP8_COMP *cpi = (VP8_COMP *)(ptr);
  268. // Set the appropriate feature bit
  269. cpi->mb.e_mbd.segmentation_enabled = 1;
  270. cpi->mb.e_mbd.update_mb_segmentation_map = 1;
  271. cpi->mb.e_mbd.update_mb_segmentation_data = 1;
  272. }
  273. static void disable_segmentation(VP8_PTR ptr)
  274. {
  275. VP8_COMP *cpi = (VP8_COMP *)(ptr);
  276. // Clear the appropriate feature bit
  277. cpi->mb.e_mbd.segmentation_enabled = 0;
  278. }
  279. // Valid values for a segment are 0 to 3
  280. // Segmentation map is arrange as [Rows][Columns]
  281. static void set_segmentation_map(VP8_PTR ptr, unsigned char *segmentation_map)
  282. {
  283. VP8_COMP *cpi = (VP8_COMP *)(ptr);
  284. // Copy in the new segmentation map
  285. vpx_memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols));
  286. // Signal that the map should be updated.
  287. cpi->mb.e_mbd.update_mb_segmentation_map = 1;
  288. cpi->mb.e_mbd.update_mb_segmentation_data = 1;
  289. }
  290. // The values given for each segment can be either deltas (from the default value chosen for the frame) or absolute values.
  291. //
  292. // Valid range for abs values is (0-127 for MB_LVL_ALT_Q) , (0-63 for SEGMENT_ALT_LF)
  293. // Valid range for delta values are (+/-127 for MB_LVL_ALT_Q) , (+/-63 for SEGMENT_ALT_LF)
  294. //
  295. // abs_delta = SEGMENT_DELTADATA (deltas) abs_delta = SEGMENT_ABSDATA (use the absolute values given).
  296. //
  297. //
  298. static void set_segment_data(VP8_PTR ptr, signed char *feature_data, unsigned char abs_delta)
  299. {
  300. VP8_COMP *cpi = (VP8_COMP *)(ptr);
  301. cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
  302. vpx_memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data));
  303. }
  304. static void segmentation_test_function(VP8_PTR ptr)
  305. {
  306. VP8_COMP *cpi = (VP8_COMP *)(ptr);
  307. unsigned char *seg_map;
  308. signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
  309. // Create a temporary map for segmentation data.
  310. CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
  311. // MB loop to set local segmentation map
  312. /*for ( i = 0; i < cpi->common.mb_rows; i++ )
  313. {
  314. for ( j = 0; j < cpi->common.mb_cols; j++ )
  315. {
  316. //seg_map[(i*cpi->common.mb_cols) + j] = (j % 2) + ((i%2)* 2);
  317. //if ( j < cpi->common.mb_cols/2 )
  318. // Segment 1 around the edge else 0
  319. if ( (i == 0) || (j == 0) || (i == (cpi->common.mb_rows-1)) || (j == (cpi->common.mb_cols-1)) )
  320. seg_map[(i*cpi->common.mb_cols) + j] = 1;
  321. //else if ( (i < 2) || (j < 2) || (i > (cpi->common.mb_rows-3)) || (j > (cpi->common.mb_cols-3)) )
  322. // seg_map[(i*cpi->common.mb_cols) + j] = 2;
  323. //else if ( (i < 5) || (j < 5) || (i > (cpi->common.mb_rows-6)) || (j > (cpi->common.mb_cols-6)) )
  324. // seg_map[(i*cpi->common.mb_cols) + j] = 3;
  325. else
  326. seg_map[(i*cpi->common.mb_cols) + j] = 0;
  327. }
  328. }*/
  329. // Set the segmentation Map
  330. set_segmentation_map(ptr, seg_map);
  331. // Activate segmentation.
  332. enable_segmentation(ptr);
  333. // Set up the quant segment data
  334. feature_data[MB_LVL_ALT_Q][0] = 0;
  335. feature_data[MB_LVL_ALT_Q][1] = 4;
  336. feature_data[MB_LVL_ALT_Q][2] = 0;
  337. feature_data[MB_LVL_ALT_Q][3] = 0;
  338. // Set up the loop segment data
  339. feature_data[MB_LVL_ALT_LF][0] = 0;
  340. feature_data[MB_LVL_ALT_LF][1] = 0;
  341. feature_data[MB_LVL_ALT_LF][2] = 0;
  342. feature_data[MB_LVL_ALT_LF][3] = 0;
  343. // Initialise the feature data structure
  344. // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
  345. set_segment_data(ptr, &feature_data[0][0], SEGMENT_DELTADATA);
  346. // Delete sementation map
  347. vpx_free(seg_map);
  348. seg_map = 0;
  349. }
  350. // A simple function to cyclically refresh the background at a lower Q
  351. static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment)
  352. {
  353. unsigned char *seg_map;
  354. signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
  355. int i;
  356. int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
  357. int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;
  358. // Create a temporary map for segmentation data.
  359. CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
  360. cpi->cyclic_refresh_q = Q;
  361. for (i = Q; i > 0; i--)
  362. {
  363. if (vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*(Q + 128)) / 64))
  364. //if ( vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*((2*Q)+96))/64) )
  365. {
  366. break;
  367. }
  368. }
  369. cpi->cyclic_refresh_q = i;
  370. // Only update for inter frames
  371. if (cpi->common.frame_type != KEY_FRAME)
  372. {
  373. // Cycle through the macro_block rows
  374. // MB loop to set local segmentation map
  375. for (i = cpi->cyclic_refresh_mode_index; i < mbs_in_frame; i++)
  376. {
  377. // If the MB is as a candidate for clean up then mark it for possible boost/refresh (segment 1)
  378. // The segment id may get reset to 0 later if the MB gets coded anything other than last frame 0,0
  379. // as only (last frame 0,0) MBs are eligable for refresh : that is to say Mbs likely to be background blocks.
  380. if (cpi->cyclic_refresh_map[i] == 0)
  381. {
  382. seg_map[i] = 1;
  383. }
  384. else
  385. {
  386. seg_map[i] = 0;
  387. // Skip blocks that have been refreshed recently anyway.
  388. if (cpi->cyclic_refresh_map[i] < 0)
  389. //cpi->cyclic_refresh_map[i] = cpi->cyclic_refresh_map[i] / 16;
  390. cpi->cyclic_refresh_map[i]++;
  391. }
  392. if (block_count > 0)
  393. block_count--;
  394. else
  395. break;
  396. }
  397. // If we have gone through the frame reset to the start
  398. cpi->cyclic_refresh_mode_index = i;
  399. if (cpi->cyclic_refresh_mode_index >= mbs_in_frame)
  400. cpi->cyclic_refresh_mode_index = 0;
  401. }
  402. // Set the segmentation Map
  403. set_segmentation_map((VP8_PTR)cpi, seg_map);
  404. // Activate segmentation.
  405. enable_segmentation((VP8_PTR)cpi);
  406. // Set up the quant segment data
  407. feature_data[MB_LVL_ALT_Q][0] = 0;
  408. feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
  409. feature_data[MB_LVL_ALT_Q][2] = 0;
  410. feature_data[MB_LVL_ALT_Q][3] = 0;
  411. // Set up the loop segment data
  412. feature_data[MB_LVL_ALT_LF][0] = 0;
  413. feature_data[MB_LVL_ALT_LF][1] = lf_adjustment;
  414. feature_data[MB_LVL_ALT_LF][2] = 0;
  415. feature_data[MB_LVL_ALT_LF][3] = 0;
  416. // Initialise the feature data structure
  417. // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
  418. set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
  419. // Delete sementation map
  420. vpx_free(seg_map);
  421. seg_map = 0;
  422. }
  423. static void set_default_lf_deltas(VP8_COMP *cpi)
  424. {
  425. cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
  426. cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
  427. vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
  428. vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
  429. // Test of ref frame deltas
  430. cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
  431. cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
  432. cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
  433. cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
  434. cpi->mb.e_mbd.mode_lf_deltas[0] = 4; // BPRED
  435. cpi->mb.e_mbd.mode_lf_deltas[1] = -2; // Zero
  436. cpi->mb.e_mbd.mode_lf_deltas[2] = 2; // New mv
  437. cpi->mb.e_mbd.mode_lf_deltas[3] = 4; // Split mv
  438. }
  439. void vp8_set_speed_features(VP8_COMP *cpi)
  440. {
  441. SPEED_FEATURES *sf = &cpi->sf;
  442. int Mode = cpi->compressor_speed;
  443. int Speed = cpi->Speed;
  444. int i;
  445. VP8_COMMON *cm = &cpi->common;
  446. int last_improved_quant = sf->improved_quant;
  447. // Initialise default mode frequency sampling variables
  448. for (i = 0; i < MAX_MODES; i ++)
  449. {
  450. cpi->mode_check_freq[i] = 0;
  451. cpi->mode_test_hit_counts[i] = 0;
  452. cpi->mode_chosen_counts[i] = 0;
  453. }
  454. cpi->mbs_tested_so_far = 0;
  455. // best quality defaults
  456. sf->RD = 1;
  457. sf->search_method = NSTEP;
  458. sf->improved_quant = 1;
  459. sf->improved_dct = 1;
  460. sf->auto_filter = 1;
  461. sf->recode_loop = 1;
  462. sf->quarter_pixel_search = 1;
  463. sf->half_pixel_search = 1;
  464. sf->iterative_sub_pixel = 1;
  465. sf->optimize_coefficients = 1;
  466. sf->use_fastquant_for_pick = 0;
  467. sf->no_skip_block4x4_search = 1;
  468. sf->first_step = 0;
  469. sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
  470. sf->improved_mv_pred = 1;
  471. // default thresholds to 0
  472. for (i = 0; i < MAX_MODES; i++)
  473. sf->thresh_mult[i] = 0;
  474. switch (Mode)
  475. {
  476. #if !(CONFIG_REALTIME_ONLY)
  477. case 0: // best quality mode
  478. sf->thresh_mult[THR_ZEROMV ] = 0;
  479. sf->thresh_mult[THR_ZEROG ] = 0;
  480. sf->thresh_mult[THR_ZEROA ] = 0;
  481. sf->thresh_mult[THR_NEARESTMV] = 0;
  482. sf->thresh_mult[THR_NEARESTG ] = 0;
  483. sf->thresh_mult[THR_NEARESTA ] = 0;
  484. sf->thresh_mult[THR_NEARMV ] = 0;
  485. sf->thresh_mult[THR_NEARG ] = 0;
  486. sf->thresh_mult[THR_NEARA ] = 0;
  487. sf->thresh_mult[THR_DC ] = 0;
  488. sf->thresh_mult[THR_V_PRED ] = 1000;
  489. sf->thresh_mult[THR_H_PRED ] = 1000;
  490. sf->thresh_mult[THR_B_PRED ] = 2000;
  491. sf->thresh_mult[THR_TM ] = 1000;
  492. sf->thresh_mult[THR_NEWMV ] = 1000;
  493. sf->thresh_mult[THR_NEWG ] = 1000;
  494. sf->thresh_mult[THR_NEWA ] = 1000;
  495. sf->thresh_mult[THR_SPLITMV ] = 2500;
  496. sf->thresh_mult[THR_SPLITG ] = 5000;
  497. sf->thresh_mult[THR_SPLITA ] = 5000;
  498. sf->first_step = 0;
  499. sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
  500. break;
  501. case 1:
  502. case 3:
  503. sf->thresh_mult[THR_NEARESTMV] = 0;
  504. sf->thresh_mult[THR_ZEROMV ] = 0;
  505. sf->thresh_mult[THR_DC ] = 0;
  506. sf->thresh_mult[THR_NEARMV ] = 0;
  507. sf->thresh_mult[THR_V_PRED ] = 1000;
  508. sf->thresh_mult[THR_H_PRED ] = 1000;
  509. sf->thresh_mult[THR_B_PRED ] = 2500;
  510. sf->thresh_mult[THR_TM ] = 1000;
  511. sf->thresh_mult[THR_NEARESTG ] = 1000;
  512. sf->thresh_mult[THR_NEARESTA ] = 1000;
  513. sf->thresh_mult[THR_ZEROG ] = 1000;
  514. sf->thresh_mult[THR_ZEROA ] = 1000;
  515. sf->thresh_mult[THR_NEARG ] = 1000;
  516. sf->thresh_mult[THR_NEARA ] = 1000;
  517. #if 1
  518. sf->thresh_mult[THR_ZEROMV ] = 0;
  519. sf->thresh_mult[THR_ZEROG ] = 0;
  520. sf->thresh_mult[THR_ZEROA ] = 0;
  521. sf->thresh_mult[THR_NEARESTMV] = 0;
  522. sf->thresh_mult[THR_NEARESTG ] = 0;
  523. sf->thresh_mult[THR_NEARESTA ] = 0;
  524. sf->thresh_mult[THR_NEARMV ] = 0;
  525. sf->thresh_mult[THR_NEARG ] = 0;
  526. sf->thresh_mult[THR_NEARA ] = 0;
  527. // sf->thresh_mult[THR_DC ] = 0;
  528. // sf->thresh_mult[THR_V_PRED ] = 1000;
  529. // sf->thresh_mult[THR_H_PRED ] = 1000;
  530. // sf->thresh_mult[THR_B_PRED ] = 2000;
  531. // sf->thresh_mult[THR_TM ] = 1000;
  532. sf->thresh_mult[THR_NEWMV ] = 1000;
  533. sf->thresh_mult[THR_NEWG ] = 1000;
  534. sf->thresh_mult[THR_NEWA ] = 1000;
  535. sf->thresh_mult[THR_SPLITMV ] = 1700;
  536. sf->thresh_mult[THR_SPLITG ] = 4500;
  537. sf->thresh_mult[THR_SPLITA ] = 4500;
  538. #else
  539. sf->thresh_mult[THR_NEWMV ] = 1500;
  540. sf->thresh_mult[THR_NEWG ] = 1500;
  541. sf->thresh_mult[THR_NEWA ] = 1500;
  542. sf->thresh_mult[THR_SPLITMV ] = 5000;
  543. sf->thresh_mult[THR_SPLITG ] = 10000;
  544. sf->thresh_mult[THR_SPLITA ] = 10000;
  545. #endif
  546. if (Speed > 0)
  547. {
  548. /* Disable coefficient optimization above speed 0 */
  549. sf->optimize_coefficients = 0;
  550. sf->use_fastquant_for_pick = 1;
  551. sf->no_skip_block4x4_search = 0;
  552. sf->first_step = 1;
  553. cpi->mode_check_freq[THR_SPLITG] = 2;
  554. cpi->mode_check_freq[THR_SPLITA] = 2;
  555. cpi->mode_check_freq[THR_SPLITMV] = 0;
  556. }
  557. if (Speed > 1)
  558. {
  559. cpi->mode_check_freq[THR_SPLITG] = 4;
  560. cpi->mode_check_freq[THR_SPLITA] = 4;
  561. cpi->mode_check_freq[THR_SPLITMV] = 2;
  562. sf->thresh_mult[THR_TM ] = 1500;
  563. sf->thresh_mult[THR_V_PRED ] = 1500;
  564. sf->thresh_mult[THR_H_PRED ] = 1500;
  565. sf->thresh_mult[THR_B_PRED ] = 5000;
  566. if (cpi->ref_frame_flags & VP8_LAST_FLAG)
  567. {
  568. sf->thresh_mult[THR_NEWMV ] = 2000;
  569. sf->thresh_mult[THR_SPLITMV ] = 10000;
  570. }
  571. if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
  572. {
  573. sf->thresh_mult[THR_NEARESTG ] = 1500;
  574. sf->thresh_mult[THR_ZEROG ] = 1500;
  575. sf->thresh_mult[THR_NEARG ] = 1500;
  576. sf->thresh_mult[THR_NEWG ] = 2000;
  577. sf->thresh_mult[THR_SPLITG ] = 20000;
  578. }
  579. if (cpi->ref_frame_flags & VP8_ALT_FLAG)
  580. {
  581. sf->thresh_mult[THR_NEARESTA ] = 1500;
  582. sf->thresh_mult[THR_ZEROA ] = 1500;
  583. sf->thresh_mult[THR_NEARA ] = 1500;
  584. sf->thresh_mult[THR_NEWA ] = 2000;
  585. sf->thresh_mult[THR_SPLITA ] = 20000;
  586. }
  587. }
  588. if (Speed > 2)
  589. {
  590. cpi->mode_check_freq[THR_SPLITG] = 15;
  591. cpi->mode_check_freq[THR_SPLITA] = 15;
  592. cpi->mode_check_freq[THR_SPLITMV] = 7;
  593. sf->thresh_mult[THR_TM ] = 2000;
  594. sf->thresh_mult[THR_V_PRED ] = 2000;
  595. sf->thresh_mult[THR_H_PRED ] = 2000;
  596. sf->thresh_mult[THR_B_PRED ] = 7500;
  597. if (cpi->ref_frame_flags & VP8_LAST_FLAG)
  598. {
  599. sf->thresh_mult[THR_NEWMV ] = 2000;
  600. sf->thresh_mult[THR_SPLITMV ] = 25000;
  601. }
  602. if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
  603. {
  604. sf->thresh_mult[THR_NEARESTG ] = 2000;
  605. sf->thresh_mult[THR_ZEROG ] = 2000;
  606. sf->thresh_mult[THR_NEARG ] = 2000;
  607. sf->thresh_mult[THR_NEWG ] = 2500;
  608. sf->thresh_mult[THR_SPLITG ] = 50000;
  609. }
  610. if (cpi->ref_frame_flags & VP8_ALT_FLAG)
  611. {
  612. sf->thresh_mult[THR_NEARESTA ] = 2000;
  613. sf->thresh_mult[THR_ZEROA ] = 2000;
  614. sf->thresh_mult[THR_NEARA ] = 2000;
  615. sf->thresh_mult[THR_NEWA ] = 2500;
  616. sf->thresh_mult[THR_SPLITA ] = 50000;
  617. }
  618. sf->improved_quant = 0;
  619. sf->improved_dct = 0;
  620. // Only do recode loop on key frames, golden frames and
  621. // alt ref frames
  622. sf->recode_loop = 2;
  623. }
  624. if (Speed > 3)
  625. {
  626. sf->thresh_mult[THR_SPLITA ] = INT_MAX;
  627. sf->thresh_mult[THR_SPLITG ] = INT_MAX;
  628. sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
  629. cpi->mode_check_freq[THR_V_PRED] = 0;
  630. cpi->mode_check_freq[THR_H_PRED] = 0;
  631. cpi->mode_check_freq[THR_B_PRED] = 0;
  632. cpi->mode_check_freq[THR_NEARG] = 0;
  633. cpi->mode_check_freq[THR_NEWG] = 0;
  634. cpi->mode_check_freq[THR_NEARA] = 0;
  635. cpi->mode_check_freq[THR_NEWA] = 0;
  636. sf->auto_filter = 1;
  637. sf->recode_loop = 0; // recode loop off
  638. sf->RD = 0; // Turn rd off
  639. }
  640. if (Speed > 4)
  641. {
  642. sf->auto_filter = 0; // Faster selection of loop filter
  643. cpi->mode_check_freq[THR_V_PRED] = 2;
  644. cpi->mode_check_freq[THR_H_PRED] = 2;
  645. cpi->mode_check_freq[THR_B_PRED] = 2;
  646. if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
  647. {
  648. cpi->mode_check_freq[THR_NEARG] = 2;
  649. cpi->mode_check_freq[THR_NEWG] = 4;
  650. }
  651. if (cpi->ref_frame_flags & VP8_ALT_FLAG)
  652. {
  653. cpi->mode_check_freq[THR_NEARA] = 2;
  654. cpi->mode_check_freq[THR_NEWA] = 4;
  655. }
  656. if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
  657. {
  658. sf->thresh_mult[THR_NEARESTG ] = 2000;
  659. sf->thresh_mult[THR_ZEROG ] = 2000;
  660. sf->thresh_mult[THR_NEARG ] = 2000;
  661. sf->thresh_mult[THR_NEWG ] = 4000;
  662. }
  663. if (cpi->ref_frame_flags & VP8_ALT_FLAG)
  664. {
  665. sf->thresh_mult[THR_NEARESTA ] = 2000;
  666. sf->thresh_mult[THR_ZEROA ] = 2000;
  667. sf->thresh_mult[THR_NEARA ] = 2000;
  668. sf->thresh_mult[THR_NEWA ] = 4000;
  669. }
  670. }
  671. break;
  672. #endif
  673. case 2:
  674. sf->optimize_coefficients = 0;
  675. sf->recode_loop = 0;
  676. sf->auto_filter = 1;
  677. sf->iterative_sub_pixel = 1;
  678. sf->thresh_mult[THR_NEARESTMV] = 0;
  679. sf->thresh_mult[THR_ZEROMV ] = 0;
  680. sf->thresh_mult[THR_DC ] = 0;
  681. sf->thresh_mult[THR_TM ] = 0;
  682. sf->thresh_mult[THR_NEARMV ] = 0;
  683. sf->thresh_mult[THR_V_PRED ] = 1000;
  684. sf->thresh_mult[THR_H_PRED ] = 1000;
  685. sf->thresh_mult[THR_B_PRED ] = 2500;
  686. sf->thresh_mult[THR_NEARESTG ] = 1000;
  687. sf->thresh_mult[THR_ZEROG ] = 1000;
  688. sf->thresh_mult[THR_NEARG ] = 1000;
  689. sf->thresh_mult[THR_NEARESTA ] = 1000;
  690. sf->thresh_mult[THR_ZEROA ] = 1000;
  691. sf->thresh_mult[THR_NEARA ] = 1000;
  692. sf->thresh_mult[THR_NEWMV ] = 2000;
  693. sf->thresh_mult[THR_NEWG ] = 2000;
  694. sf->thresh_mult[THR_NEWA ] = 2000;
  695. sf->thresh_mult[THR_SPLITMV ] = 5000;
  696. sf->thresh_mult[THR_SPLITG ] = 10000;
  697. sf->thresh_mult[THR_SPLITA ] = 10000;
  698. sf->search_method = NSTEP;
  699. if (Speed > 0)
  700. {
  701. cpi->mode_check_freq[THR_SPLITG] = 4;
  702. cpi->mode_check_freq[THR_SPLITA] = 4;
  703. cpi->mode_check_freq[THR_SPLITMV] = 2;
  704. sf->thresh_mult[THR_DC ] = 0;
  705. sf->thresh_mult[THR_TM ] = 1000;
  706. sf->thresh_mult[THR_V_PRED ] = 2000;
  707. sf->thresh_mult[THR_H_PRED ] = 2000;
  708. sf->thresh_mult[THR_B_PRED ] = 5000;
  709. if (cpi->ref_frame_flags & VP8_LAST_FLAG)
  710. {
  711. sf->thresh_mult[THR_NEARESTMV] = 0;
  712. sf->thresh_mult[THR_ZEROMV ] = 0;
  713. sf->thresh_mult[THR_NEARMV ] = 0;
  714. sf->thresh_mult[THR_NEWMV ] = 2000;
  715. sf->thresh_mult[THR_SPLITMV ] = 10000;
  716. }
  717. if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
  718. {
  719. sf->thresh_mult[THR_NEARESTG ] = 1000;
  720. sf->thresh_mult[THR_ZEROG ] = 1000;
  721. sf->thresh_mult[THR_NEARG ] = 1000;
  722. sf->thresh_mult[THR_NEWG ] = 2000;
  723. sf->thresh_mult[THR_SPLITG ] = 20000;
  724. }
  725. if (cpi->ref_frame_flags & VP8_ALT_FLAG)
  726. {
  727. sf->thresh_mult[THR_NEARESTA ] = 1000;
  728. sf->thresh_mult[THR_ZEROA ] = 1000;
  729. sf->thresh_mult[THR_NEARA ] = 1000;
  730. sf->thresh_mult[THR_NEWA ] = 2000;
  731. sf->thresh_mult[THR_SPLITA ] = 20000;
  732. }
  733. sf->improved_quant = 0;
  734. sf->improved_dct = 0;
  735. sf->use_fastquant_for_pick = 1;
  736. sf->no_skip_block4x4_search = 0;
  737. sf->first_step = 1;
  738. }
  739. if (Speed > 1)
  740. {
  741. cpi->mode_check_freq[THR_SPLITMV] = 7;
  742. cpi->mode_check_freq[THR_SPLITG] = 15;
  743. cpi->mode_check_freq[THR_SPLITA] = 15;
  744. sf->thresh_mult[THR_TM ] = 2000;
  745. sf->thresh_mult[THR_V_PRED ] = 2000;
  746. sf->thresh_mult[THR_H_PRED ] = 2000;
  747. sf->thresh_mult[THR_B_PRED ] = 5000;
  748. if (cpi->ref_frame_flags & VP8_LAST_FLAG)
  749. {
  750. sf->thresh_mult[THR_NEWMV ] = 2000;
  751. sf->thresh_mult[THR_SPLITMV ] = 25000;
  752. }
  753. if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
  754. {
  755. sf->thresh_mult[THR_NEARESTG ] = 2000;
  756. sf->thresh_mult[THR_ZEROG ] = 2000;
  757. sf->thresh_mult[THR_NEARG ] = 2000;
  758. sf->thresh_mult[THR_NEWG ] = 2500;
  759. sf->thresh_mult[THR_SPLITG ] = 50000;
  760. }
  761. if (cpi->ref_frame_flags & VP8_ALT_FLAG)
  762. {
  763. sf->thresh_mult[THR_NEARESTA ] = 2000;
  764. sf->thresh_mult[THR_ZEROA ] = 2000;
  765. sf->thresh_mult[THR_NEARA ] = 2000;
  766. sf->thresh_mult[THR_NEWA ] = 2500;
  767. sf->thresh_mult[THR_SPLITA ] = 50000;
  768. }
  769. }
  770. if (Speed > 2)
  771. {
  772. sf->auto_filter = 0; // Faster selection of loop filter
  773. cpi->mode_check_freq[THR_V_PRED] = 2;
  774. cpi->mode_check_freq[THR_H_PRED] = 2;
  775. cpi->mode_check_freq[THR_B_PRED] = 2;
  776. if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
  777. {
  778. cpi->mode_check_freq[THR_NEARG] = 2;
  779. cpi->mode_check_freq[THR_NEWG] = 4;
  780. }
  781. if (cpi->ref_frame_flags & VP8_ALT_FLAG)
  782. {
  783. cpi->mode_check_freq[THR_NEARA] = 2;
  784. cpi->mode_check_freq[THR_NEWA] = 4;
  785. }
  786. sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
  787. sf->thresh_mult[THR_SPLITG ] = INT_MAX;
  788. sf->thresh_mult[THR_SPLITA ] = INT_MAX;
  789. }
  790. if (Speed > 3)
  791. {
  792. sf->RD = 0;
  793. sf->auto_filter = 1;
  794. }
  795. if (Speed > 4)
  796. {
  797. sf->auto_filter = 0; // Faster selection of loop filter
  798. sf->search_method = HEX;
  799. //sf->search_method = DIAMOND;
  800. sf->iterative_sub_pixel = 0;
  801. cpi->mode_check_freq[THR_V_PRED] = 4;
  802. cpi->mode_check_freq[THR_H_PRED] = 4;
  803. cpi->mode_check_freq[THR_B_PRED] = 4;
  804. if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
  805. {
  806. cpi->mode_check_freq[THR_NEARG] = 2;
  807. cpi->mode_check_freq[THR_NEWG] = 4;
  808. }
  809. if (cpi->ref_frame_flags & VP8_ALT_FLAG)
  810. {
  811. cpi->mode_check_freq[THR_NEARA] = 2;
  812. cpi->mode_check_freq[THR_NEWA] = 4;
  813. }
  814. sf->thresh_mult[THR_TM ] = 2000;
  815. sf->thresh_mult[THR_B_PRED ] = 5000;
  816. if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
  817. {
  818. sf->thresh_mult[THR_NEARESTG ] = 2000;
  819. sf->thresh_mult[THR_ZEROG ] = 2000;
  820. sf->thresh_mult[THR_NEARG ] = 2000;
  821. sf->thresh_mult[THR_NEWG ] = 4000;
  822. }
  823. if (cpi->ref_frame_flags & VP8_ALT_FLAG)
  824. {
  825. sf->thresh_mult[THR_NEARESTA ] = 2000;
  826. sf->thresh_mult[THR_ZEROA ] = 2000;
  827. sf->thresh_mult[THR_NEARA ] = 2000;
  828. sf->thresh_mult[THR_NEWA ] = 4000;
  829. }
  830. }
  831. if (Speed > 5)
  832. {
  833. // Disable split MB intra prediction mode
  834. sf->thresh_mult[THR_B_PRED] = INT_MAX;
  835. }
  836. if (Speed > 6)
  837. {
  838. unsigned int i, sum = 0;
  839. unsigned int total_mbs = cm->MBs;
  840. int thresh;
  841. int total_skip;
  842. int min = 2000;
  843. if (cpi->oxcf.encode_breakout > 2000)
  844. min = cpi->oxcf.encode_breakout;
  845. min >>= 7;
  846. for (i = 0; i < min; i++)
  847. {
  848. sum += cpi->error_bins[i];
  849. }
  850. total_skip = sum;
  851. sum = 0;
  852. // i starts from 2 to make sure thresh started from 2048
  853. for (; i < 1024; i++)
  854. {
  855. sum += cpi->error_bins[i];
  856. if (10 * sum >= (unsigned int)(cpi->Speed - 6)*(total_mbs - total_skip))
  857. break;
  858. }
  859. i--;
  860. thresh = (i << 7);
  861. if (thresh < 2000)
  862. thresh = 2000;
  863. if (cpi->ref_frame_flags & VP8_LAST_FLAG)
  864. {
  865. sf->thresh_mult[THR_NEWMV] = thresh;
  866. sf->thresh_mult[THR_NEARESTMV ] = thresh >> 1;
  867. sf->thresh_mult[THR_NEARMV ] = thresh >> 1;
  868. }
  869. if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
  870. {
  871. sf->thresh_mult[THR_NEWG] = thresh << 1;
  872. sf->thresh_mult[THR_NEARESTG ] = thresh;
  873. sf->thresh_mult[THR_NEARG ] = thresh;
  874. }
  875. if (cpi->ref_frame_flags & VP8_ALT_FLAG)
  876. {
  877. sf->thresh_mult[THR_NEWA] = thresh << 1;
  878. sf->thresh_mult[THR_NEARESTA ] = thresh;
  879. sf->thresh_mult[THR_NEARA ] = thresh;
  880. }
  881. // Disable other intra prediction modes
  882. sf->thresh_mult[THR_TM] = INT_MAX;
  883. sf->thresh_mult[THR_V_PRED] = INT_MAX;
  884. sf->thresh_mult[THR_H_PRED] = INT_MAX;
  885. sf->improved_mv_pred = 0;
  886. }
  887. if (Speed > 8)
  888. {
  889. sf->quarter_pixel_search = 0;
  890. }
  891. if (Speed > 9)
  892. {
  893. int Tmp = cpi->Speed - 8;
  894. if (Tmp > 4)
  895. Tmp = 4;
  896. if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
  897. {
  898. cpi->mode_check_freq[THR_ZEROG] = 1 << (Tmp - 1);
  899. cpi->mode_check_freq[THR_NEARESTG] = 1 << (Tmp - 1);
  900. cpi->mode_check_freq[THR_NEARG] = 1 << Tmp;
  901. cpi->mode_check_freq[THR_NEWG] = 1 << (Tmp + 1);
  902. }
  903. if (cpi->ref_frame_flags & VP8_ALT_FLAG)
  904. {
  905. cpi->mode_check_freq[THR_ZEROA] = 1 << (Tmp - 1);
  906. cpi->mode_check_freq[THR_NEARESTA] = 1 << (Tmp - 1);
  907. cpi->mode_check_freq[THR_NEARA] = 1 << Tmp;
  908. cpi->mode_check_freq[THR_NEWA] = 1 << (Tmp + 1);
  909. }
  910. cpi->mode_check_freq[THR_NEWMV] = 1 << (Tmp - 1);
  911. }
  912. cm->filter_type = NORMAL_LOOPFILTER;
  913. if (Speed >= 14)
  914. cm->filter_type = SIMPLE_LOOPFILTER;
  915. if (Speed >= 15)
  916. {
  917. sf->half_pixel_search = 0; // This has a big hit on quality. Last resort
  918. }
  919. vpx_memset(cpi->error_bins, 0, sizeof(cpi->error_bins));
  920. }; /* switch */
  921. /* disable frame modes if flags not set */
  922. if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
  923. {
  924. sf->thresh_mult[THR_NEWMV ] = INT_MAX;
  925. sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
  926. sf->thresh_mult[THR_ZEROMV ] = INT_MAX;
  927. sf->thresh_mult[THR_NEARMV ] = INT_MAX;
  928. sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
  929. }
  930. if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
  931. {
  932. sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
  933. sf->thresh_mult[THR_ZEROG ] = INT_MAX;
  934. sf->thresh_mult[THR_NEARG ] = INT_MAX;
  935. sf->thresh_mult[THR_NEWG ] = INT_MAX;
  936. sf->thresh_mult[THR_SPLITG ] = INT_MAX;
  937. }
  938. if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
  939. {
  940. sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
  941. sf->thresh_mult[THR_ZEROA ] = INT_MAX;
  942. sf->thresh_mult[THR_NEARA ] = INT_MAX;
  943. sf->thresh_mult[THR_NEWA ] = INT_MAX;
  944. sf->thresh_mult[THR_SPLITA ] = INT_MAX;
  945. }
  946. // Slow quant, dct and trellis not worthwhile for first pass
  947. // so make sure they are always turned off.
  948. if ( cpi->pass == 1 )
  949. {
  950. sf->improved_quant = 0;
  951. sf->optimize_coefficients = 0;
  952. sf->improved_dct = 0;
  953. }
  954. if (cpi->sf.search_method == NSTEP)
  955. {
  956. vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
  957. }
  958. else if (cpi->sf.search_method == DIAMOND)
  959. {
  960. vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
  961. }
  962. if (cpi->sf.improved_dct)
  963. {
  964. cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short8x4);
  965. cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short4x4);
  966. }
  967. else
  968. {
  969. cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast8x4);
  970. cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast4x4);
  971. }
  972. cpi->mb.short_walsh4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, walsh_short4x4);
  973. if (cpi->sf.improved_quant)
  974. {
  975. cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
  976. quantb);
  977. cpi->mb.quantize_b_pair = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
  978. quantb_pair);
  979. }
  980. else
  981. {
  982. cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
  983. fastquantb);
  984. cpi->mb.quantize_b_pair = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
  985. fastquantb_pair);
  986. }
  987. if (cpi->sf.improved_quant != last_improved_quant)
  988. vp8cx_init_quantizer(cpi);
  989. #if CONFIG_RUNTIME_CPU_DETECT
  990. cpi->mb.e_mbd.rtcd = &cpi->common.rtcd;
  991. #endif
  992. if (cpi->sf.iterative_sub_pixel == 1)
  993. {
  994. cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
  995. }
  996. else if (cpi->sf.quarter_pixel_search)
  997. {
  998. cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
  999. }
  1000. else if (cpi->sf.half_pixel_search)
  1001. {
  1002. cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step;
  1003. }
  1004. else
  1005. {
  1006. cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
  1007. }
  1008. if (cpi->sf.optimize_coefficients == 1 && cpi->pass!=1)
  1009. cpi->mb.optimize = 1;
  1010. else
  1011. cpi->mb.optimize = 0;
  1012. if (cpi->common.full_pixel)
  1013. cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
  1014. #ifdef SPEEDSTATS
  1015. frames_at_speed[cpi->Speed]++;
  1016. #endif
  1017. }
  1018. static void alloc_raw_frame_buffers(VP8_COMP *cpi)
  1019. {
  1020. int width = (cpi->oxcf.Width + 15) & ~15;
  1021. int height = (cpi->oxcf.Height + 15) & ~15;
  1022. cpi->lookahead = vp8_lookahead_init(cpi->oxcf.Width, cpi->oxcf.Height,
  1023. cpi->oxcf.lag_in_frames);
  1024. if(!cpi->lookahead)
  1025. vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
  1026. "Failed to allocate lag buffers");
  1027. #if VP8_TEMPORAL_ALT_REF
  1028. if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer,
  1029. width, height, VP8BORDERINPIXELS))
  1030. vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
  1031. "Failed to allocate altref buffer");
  1032. #endif
  1033. }
  1034. static int vp8_alloc_partition_data(VP8_COMP *cpi)
  1035. {
  1036. vpx_free(cpi->mb.pip);
  1037. cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
  1038. (cpi->common.mb_rows + 1),
  1039. sizeof(PARTITION_INFO));
  1040. if(!cpi->mb.pip)
  1041. return 1;
  1042. cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
  1043. return 0;
  1044. }
  1045. void vp8_alloc_compressor_data(VP8_COMP *cpi)
  1046. {
  1047. VP8_COMMON *cm = & cpi->common;
  1048. int width = cm->Width;
  1049. int height = cm->Height;
  1050. if (vp8_alloc_frame_buffers(cm, width, height))
  1051. vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
  1052. "Failed to allocate frame buffers");
  1053. if (vp8_alloc_partition_data(cpi))
  1054. vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
  1055. "Failed to allocate partition data");
  1056. if ((width & 0xf) != 0)
  1057. width += 16 - (width & 0xf);
  1058. if ((height & 0xf) != 0)
  1059. height += 16 - (height & 0xf);
  1060. if (vp8_yv12_alloc_frame_buffer(&cpi->last_frame_uf,
  1061. width, height, VP8BORDERINPIXELS))
  1062. vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
  1063. "Failed to allocate last frame buffer");
  1064. if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source,
  1065. width, height, VP8BORDERINPIXELS))
  1066. vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
  1067. "Failed to allocate scaled source buffer");
  1068. vpx_free(cpi->tok);
  1069. {
  1070. unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
  1071. CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
  1072. }
  1073. // Data used for real time vc mode to see if gf needs refreshing
  1074. cpi->inter_zz_count = 0;
  1075. cpi->gf_bad_count = 0;
  1076. cpi->gf_update_recommended = 0;
  1077. // Structures used to minitor GF usage
  1078. vpx_free(cpi->gf_active_flags);
  1079. CHECK_MEM_ERROR(cpi->gf_active_flags,
  1080. vpx_calloc(1, cm->mb_rows * cm->mb_cols));
  1081. cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
  1082. vpx_free(cpi->mb_activity_map);
  1083. CHECK_MEM_ERROR(cpi->mb_activity_map,
  1084. vpx_calloc(sizeof(unsigned int),
  1085. cm->mb_rows * cm->mb_cols));
  1086. vpx_free(cpi->mb_norm_activity_map);
  1087. CHECK_MEM_ERROR(cpi->mb_norm_activity_map,
  1088. vpx_calloc(sizeof(unsigned int),
  1089. cm->mb_rows * cm->mb_cols));
  1090. #if !(CONFIG_REALTIME_ONLY)
  1091. vpx_free(cpi->twopass.total_stats);
  1092. cpi->twopass.total_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));
  1093. vpx_free(cpi->twopass.this_frame_stats);
  1094. cpi->twopass.this_frame_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));
  1095. if(!cpi->twopass.total_stats || !cpi->twopass.this_frame_stats)
  1096. vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
  1097. "Failed to allocate firstpass stats");
  1098. #endif
  1099. #if CONFIG_MULTITHREAD
  1100. if (width < 640)
  1101. cpi->mt_sync_range = 1;
  1102. else if (width <= 1280)
  1103. cpi->mt_sync_range = 4;
  1104. else if (width <= 2560)
  1105. cpi->mt_sync_range = 8;
  1106. else
  1107. cpi->mt_sync_range = 16;
  1108. #endif
  1109. vpx_free(cpi->tplist);
  1110. CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cpi->common.mb_rows));
  1111. }
  1112. // Quant MOD
  1113. static const int q_trans[] =
  1114. {
  1115. 0, 1, 2, 3, 4, 5, 7, 8,
  1116. 9, 10, 12, 13, 15, 17, 18, 19,
  1117. 20, 21, 23, 24, 25, 26, 27, 28,
  1118. 29, 30, 31, 33, 35, 37, 39, 41,
  1119. 43, 45, 47, 49, 51, 53, 55, 57,
  1120. 59, 61, 64, 67, 70, 73, 76, 79,
  1121. 82, 85, 88, 91, 94, 97, 100, 103,
  1122. 106, 109, 112, 115, 118, 121, 124, 127,
  1123. };
  1124. int vp8_reverse_trans(int x)
  1125. {
  1126. int i;
  1127. for (i = 0; i < 64; i++)
  1128. if (q_trans[i] >= x)
  1129. return i;
  1130. return 63;
  1131. };
  1132. void vp8_new_frame_rate(VP8_COMP *cpi, double framerate)
  1133. {
  1134. if(framerate < .1)
  1135. framerate = 30;
  1136. cpi->oxcf.frame_rate = framerate;
  1137. cpi->output_frame_rate = cpi->oxcf.frame_rate;
  1138. cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
  1139. cpi->av_per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
  1140. cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
  1141. // Set Maximum gf/arf interval
  1142. cpi->max_gf_interval = ((int)(cpi->output_frame_rate / 2.0) + 2);
  1143. if(cpi->max_gf_interval < 12)
  1144. cpi->max_gf_interval = 12;
  1145. // Extended interval for genuinely static scenes
  1146. cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
  1147. // Special conditions when altr ref frame enabled in lagged compress mode
  1148. if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames)
  1149. {
  1150. if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
  1151. cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
  1152. if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
  1153. cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
  1154. }
  1155. if ( cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval )
  1156. cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
  1157. }
  1158. static int
  1159. rescale(int val, int num, int denom)
  1160. {
  1161. int64_t llnum = num;
  1162. int64_t llden = denom;
  1163. int64_t llval = val;
  1164. return llval * llnum / llden;
  1165. }
  1166. static void init_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
  1167. {
  1168. VP8_COMP *cpi = (VP8_COMP *)(ptr);
  1169. VP8_COMMON *cm = &cpi->common;
  1170. cpi->oxcf = *oxcf;
  1171. cpi->auto_gold = 1;
  1172. cpi->auto_adjust_gold_quantizer = 1;
  1173. cpi->goldfreq = 7;
  1174. cm->version = oxcf->Version;
  1175. vp8_setup_version(cm);
  1176. // change includes all joint functionality
  1177. vp8_change_config(ptr, oxcf);
  1178. // Initialize active best and worst q and average q values.
  1179. cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
  1180. cpi->active_best_quality = cpi->oxcf.best_allowed_q;
  1181. cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
  1182. // Initialise the starting buffer levels
  1183. cpi->buffer_level = cpi->oxcf.starting_buffer_level;
  1184. cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
  1185. cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
  1186. cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
  1187. cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
  1188. cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
  1189. cpi->total_actual_bits = 0;
  1190. cpi->total_target_vs_actual = 0;
  1191. #if VP8_TEMPORAL_ALT_REF
  1192. {
  1193. int i;
  1194. cpi->fixed_divide[0] = 0;
  1195. for (i = 1; i < 512; i++)
  1196. cpi->fixed_divide[i] = 0x80000 / i;
  1197. }
  1198. #endif
  1199. }
  1200. void vp8_change_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
  1201. {
  1202. VP8_COMP *cpi = (VP8_COMP *)(ptr);
  1203. VP8_COMMON *cm = &cpi->common;
  1204. if (!cpi)
  1205. return;
  1206. if (!oxcf)
  1207. return;
  1208. if (cm->version != oxcf->Version)
  1209. {
  1210. cm->version = oxcf->Version;
  1211. vp8_setup_version(cm);
  1212. }
  1213. cpi->oxcf = *oxcf;
  1214. switch (cpi->oxcf.Mode)
  1215. {
  1216. case MODE_REALTIME:
  1217. cpi->pass = 0;
  1218. cpi->compressor_speed = 2;
  1219. if (cpi->oxcf.cpu_used < -16)
  1220. {
  1221. cpi->oxcf.cpu_used = -16;
  1222. }
  1223. if (cpi->oxcf.cpu_used > 16)
  1224. cpi->oxcf.cpu_used = 16;
  1225. break;
  1226. case MODE_GOODQUALITY:
  1227. cpi->pass = 0;
  1228. cpi->compressor_speed = 1;
  1229. if (cpi->oxcf.cpu_used < -5)
  1230. {
  1231. cpi->oxcf.cpu_used = -5;
  1232. }
  1233. if (cpi->oxcf.cpu_used > 5)
  1234. cpi->oxcf.cpu_used = 5;
  1235. break;
  1236. case MODE_BESTQUALITY:
  1237. cpi->pass = 0;
  1238. cpi->compressor_speed = 0;
  1239. break;
  1240. case MODE_FIRSTPASS:
  1241. cpi->pass = 1;
  1242. cpi->compressor_speed = 1;
  1243. break;
  1244. case MODE_SECONDPASS:
  1245. cpi->pass = 2;
  1246. cpi->compressor_speed = 1;
  1247. if (cpi->oxcf.cpu_used < -5)
  1248. {
  1249. cpi->oxcf.cpu_used = -5;
  1250. }
  1251. if (cpi->oxcf.cpu_used > 5)
  1252. cpi->oxcf.cpu_used = 5;
  1253. break;
  1254. case MODE_SECONDPASS_BEST:
  1255. cpi->pass = 2;
  1256. cpi->compressor_speed = 0;
  1257. break;
  1258. }
  1259. if (cpi->pass == 0)
  1260. cpi->auto_worst_q = 1;
  1261. cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
  1262. cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
  1263. cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
  1264. if (oxcf->fixed_q >= 0)
  1265. {
  1266. if (oxcf->worst_allowed_q < 0)
  1267. cpi->oxcf.fixed_q = q_trans[0];
  1268. else
  1269. cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
  1270. if (oxcf->alt_q < 0)
  1271. cpi->oxcf.alt_q = q_trans[0];
  1272. else
  1273. cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
  1274. if (oxcf->key_q < 0)
  1275. cpi->oxcf.key_q = q_trans[0];
  1276. else
  1277. cpi->oxcf.key_q = q_trans[oxcf->key_q];
  1278. if (oxcf->gold_q < 0)
  1279. cpi->oxcf.gold_q = q_trans[0];
  1280. else
  1281. cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
  1282. }
  1283. cpi->baseline_gf_interval =
  1284. cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
  1285. cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
  1286. //cpi->use_golden_frame_only = 0;
  1287. //cpi->use_last_frame_only =…