vp9_aq_cyclicrefresh.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * Copyright (c) 2014 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 <limits.h>
  11. #include <math.h>
  12. #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
  13. #include "vp9/common/vp9_seg_common.h"
  14. #include "vp9/encoder/vp9_ratectrl.h"
  15. #include "vp9/encoder/vp9_segmentation.h"
  16. struct CYCLIC_REFRESH {
  17. // Percentage of blocks per frame that are targeted as candidates
  18. // for cyclic refresh.
  19. int percent_refresh;
  20. // Maximum q-delta as percentage of base q.
  21. int max_qdelta_perc;
  22. // Superblock starting index for cycling through the frame.
  23. int sb_index;
  24. // Controls how long block will need to wait to be refreshed again, in
  25. // excess of the cycle time, i.e., in the case of all zero motion, block
  26. // will be refreshed every (100/percent_refresh + time_for_refresh) frames.
  27. int time_for_refresh;
  28. // Target number of (8x8) blocks that are set for delta-q.
  29. int target_num_seg_blocks;
  30. // Actual number of (8x8) blocks that were applied delta-q.
  31. int actual_num_seg1_blocks;
  32. int actual_num_seg2_blocks;
  33. // RD mult. parameters for segment 1.
  34. int rdmult;
  35. // Cyclic refresh map.
  36. signed char *map;
  37. // Thresholds applied to the projected rate/distortion of the coding block,
  38. // when deciding whether block should be refreshed.
  39. int64_t thresh_rate_sb;
  40. int64_t thresh_dist_sb;
  41. // Threshold applied to the motion vector (in units of 1/8 pel) of the
  42. // coding block, when deciding whether block should be refreshed.
  43. int16_t motion_thresh;
  44. // Rate target ratio to set q delta.
  45. double rate_ratio_qdelta;
  46. double low_content_avg;
  47. int qindex_delta_seg1;
  48. int qindex_delta_seg2;
  49. };
  50. CYCLIC_REFRESH *vp9_cyclic_refresh_alloc(int mi_rows, int mi_cols) {
  51. CYCLIC_REFRESH *const cr = vpx_calloc(1, sizeof(*cr));
  52. if (cr == NULL)
  53. return NULL;
  54. cr->map = vpx_calloc(mi_rows * mi_cols, sizeof(*cr->map));
  55. if (cr->map == NULL) {
  56. vpx_free(cr);
  57. return NULL;
  58. }
  59. return cr;
  60. }
  61. void vp9_cyclic_refresh_free(CYCLIC_REFRESH *cr) {
  62. vpx_free(cr->map);
  63. vpx_free(cr);
  64. }
  65. // Check if we should turn off cyclic refresh based on bitrate condition.
  66. static int apply_cyclic_refresh_bitrate(const VP9_COMMON *cm,
  67. const RATE_CONTROL *rc) {
  68. // Turn off cyclic refresh if bits available per frame is not sufficiently
  69. // larger than bit cost of segmentation. Segment map bit cost should scale
  70. // with number of seg blocks, so compare available bits to number of blocks.
  71. // Average bits available per frame = avg_frame_bandwidth
  72. // Number of (8x8) blocks in frame = mi_rows * mi_cols;
  73. const float factor = 0.25;
  74. const int number_blocks = cm->mi_rows * cm->mi_cols;
  75. // The condition below corresponds to turning off at target bitrates:
  76. // (at 30fps), ~12kbps for CIF, 36kbps for VGA, 100kps for HD/720p.
  77. // Also turn off at very small frame sizes, to avoid too large fraction of
  78. // superblocks to be refreshed per frame. Threshold below is less than QCIF.
  79. if (rc->avg_frame_bandwidth < factor * number_blocks ||
  80. number_blocks / 64 < 5)
  81. return 0;
  82. else
  83. return 1;
  84. }
  85. // Check if this coding block, of size bsize, should be considered for refresh
  86. // (lower-qp coding). Decision can be based on various factors, such as
  87. // size of the coding block (i.e., below min_block size rejected), coding
  88. // mode, and rate/distortion.
  89. static int candidate_refresh_aq(const CYCLIC_REFRESH *cr,
  90. const MB_MODE_INFO *mbmi,
  91. int64_t rate,
  92. int64_t dist,
  93. int bsize) {
  94. MV mv = mbmi->mv[0].as_mv;
  95. // Reject the block for lower-qp coding if projected distortion
  96. // is above the threshold, and any of the following is true:
  97. // 1) mode uses large mv
  98. // 2) mode is an intra-mode
  99. // Otherwise accept for refresh.
  100. if (dist > cr->thresh_dist_sb &&
  101. (mv.row > cr->motion_thresh || mv.row < -cr->motion_thresh ||
  102. mv.col > cr->motion_thresh || mv.col < -cr->motion_thresh ||
  103. !is_inter_block(mbmi)))
  104. return CR_SEGMENT_ID_BASE;
  105. else if (bsize >= BLOCK_16X16 &&
  106. rate < cr->thresh_rate_sb &&
  107. is_inter_block(mbmi) &&
  108. mbmi->mv[0].as_int == 0)
  109. // More aggressive delta-q for bigger blocks with zero motion.
  110. return CR_SEGMENT_ID_BOOST2;
  111. else
  112. return CR_SEGMENT_ID_BOOST1;
  113. }
  114. // Compute delta-q for the segment.
  115. static int compute_deltaq(const VP9_COMP *cpi, int q, double rate_factor) {
  116. const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
  117. const RATE_CONTROL *const rc = &cpi->rc;
  118. int deltaq = vp9_compute_qdelta_by_rate(rc, cpi->common.frame_type,
  119. q, rate_factor,
  120. cpi->common.bit_depth);
  121. if ((-deltaq) > cr->max_qdelta_perc * q / 100) {
  122. deltaq = -cr->max_qdelta_perc * q / 100;
  123. }
  124. return deltaq;
  125. }
  126. // For the just encoded frame, estimate the bits, incorporating the delta-q
  127. // from non-base segment. For now ignore effect of multiple segments
  128. // (with different delta-q). Note this function is called in the postencode
  129. // (called from rc_update_rate_correction_factors()).
  130. int vp9_cyclic_refresh_estimate_bits_at_q(const VP9_COMP *cpi,
  131. double correction_factor) {
  132. const VP9_COMMON *const cm = &cpi->common;
  133. const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
  134. int estimated_bits;
  135. int mbs = cm->MBs;
  136. int num8x8bl = mbs << 2;
  137. // Weight for non-base segments: use actual number of blocks refreshed in
  138. // previous/just encoded frame. Note number of blocks here is in 8x8 units.
  139. double weight_segment1 = (double)cr->actual_num_seg1_blocks / num8x8bl;
  140. double weight_segment2 = (double)cr->actual_num_seg2_blocks / num8x8bl;
  141. // Take segment weighted average for estimated bits.
  142. estimated_bits = (int)((1.0 - weight_segment1 - weight_segment2) *
  143. vp9_estimate_bits_at_q(cm->frame_type, cm->base_qindex, mbs,
  144. correction_factor, cm->bit_depth) +
  145. weight_segment1 *
  146. vp9_estimate_bits_at_q(cm->frame_type,
  147. cm->base_qindex + cr->qindex_delta_seg1, mbs,
  148. correction_factor, cm->bit_depth) +
  149. weight_segment2 *
  150. vp9_estimate_bits_at_q(cm->frame_type,
  151. cm->base_qindex + cr->qindex_delta_seg2, mbs,
  152. correction_factor, cm->bit_depth));
  153. return estimated_bits;
  154. }
  155. // Prior to encoding the frame, estimate the bits per mb, for a given q = i and
  156. // a corresponding delta-q (for segment 1). This function is called in the
  157. // rc_regulate_q() to set the base qp index.
  158. // Note: the segment map is set to either 0/CR_SEGMENT_ID_BASE (no refresh) or
  159. // to 1/CR_SEGMENT_ID_BOOST1 (refresh) for each superblock, prior to encoding.
  160. int vp9_cyclic_refresh_rc_bits_per_mb(const VP9_COMP *cpi, int i,
  161. double correction_factor) {
  162. const VP9_COMMON *const cm = &cpi->common;
  163. CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
  164. int bits_per_mb;
  165. int num8x8bl = cm->MBs << 2;
  166. // Weight for segment prior to encoding: take the average of the target
  167. // number for the frame to be encoded and the actual from the previous frame.
  168. double weight_segment = (double)((cr->target_num_seg_blocks +
  169. cr->actual_num_seg1_blocks + cr->actual_num_seg2_blocks) >> 1) /
  170. num8x8bl;
  171. // Compute delta-q corresponding to qindex i.
  172. int deltaq = compute_deltaq(cpi, i, cr->rate_ratio_qdelta);
  173. // Take segment weighted average for bits per mb.
  174. bits_per_mb = (int)((1.0 - weight_segment) *
  175. vp9_rc_bits_per_mb(cm->frame_type, i, correction_factor, cm->bit_depth) +
  176. weight_segment *
  177. vp9_rc_bits_per_mb(cm->frame_type, i + deltaq, correction_factor,
  178. cm->bit_depth));
  179. return bits_per_mb;
  180. }
  181. // Prior to coding a given prediction block, of size bsize at (mi_row, mi_col),
  182. // check if we should reset the segment_id, and update the cyclic_refresh map
  183. // and segmentation map.
  184. void vp9_cyclic_refresh_update_segment(VP9_COMP *const cpi,
  185. MB_MODE_INFO *const mbmi,
  186. int mi_row, int mi_col,
  187. BLOCK_SIZE bsize,
  188. int64_t rate,
  189. int64_t dist,
  190. int skip) {
  191. const VP9_COMMON *const cm = &cpi->common;
  192. CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
  193. const int bw = num_8x8_blocks_wide_lookup[bsize];
  194. const int bh = num_8x8_blocks_high_lookup[bsize];
  195. const int xmis = MIN(cm->mi_cols - mi_col, bw);
  196. const int ymis = MIN(cm->mi_rows - mi_row, bh);
  197. const int block_index = mi_row * cm->mi_cols + mi_col;
  198. const int refresh_this_block = candidate_refresh_aq(cr, mbmi, rate, dist,
  199. bsize);
  200. // Default is to not update the refresh map.
  201. int new_map_value = cr->map[block_index];
  202. int x = 0; int y = 0;
  203. // If this block is labeled for refresh, check if we should reset the
  204. // segment_id.
  205. if (cyclic_refresh_segment_id_boosted(mbmi->segment_id)) {
  206. mbmi->segment_id = refresh_this_block;
  207. // Reset segment_id if will be skipped.
  208. if (skip)
  209. mbmi->segment_id = CR_SEGMENT_ID_BASE;
  210. }
  211. // Update the cyclic refresh map, to be used for setting segmentation map
  212. // for the next frame. If the block will be refreshed this frame, mark it
  213. // as clean. The magnitude of the -ve influences how long before we consider
  214. // it for refresh again.
  215. if (cyclic_refresh_segment_id_boosted(mbmi->segment_id)) {
  216. new_map_value = -cr->time_for_refresh;
  217. } else if (refresh_this_block) {
  218. // Else if it is accepted as candidate for refresh, and has not already
  219. // been refreshed (marked as 1) then mark it as a candidate for cleanup
  220. // for future time (marked as 0), otherwise don't update it.
  221. if (cr->map[block_index] == 1)
  222. new_map_value = 0;
  223. } else {
  224. // Leave it marked as block that is not candidate for refresh.
  225. new_map_value = 1;
  226. }
  227. // Update entries in the cyclic refresh map with new_map_value, and
  228. // copy mbmi->segment_id into global segmentation map.
  229. for (y = 0; y < ymis; y++)
  230. for (x = 0; x < xmis; x++) {
  231. cr->map[block_index + y * cm->mi_cols + x] = new_map_value;
  232. cpi->segmentation_map[block_index + y * cm->mi_cols + x] =
  233. mbmi->segment_id;
  234. }
  235. }
  236. // Update the actual number of blocks that were applied the segment delta q.
  237. void vp9_cyclic_refresh_postencode(VP9_COMP *const cpi) {
  238. VP9_COMMON *const cm = &cpi->common;
  239. CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
  240. unsigned char *const seg_map = cpi->segmentation_map;
  241. int mi_row, mi_col;
  242. cr->actual_num_seg1_blocks = 0;
  243. cr->actual_num_seg2_blocks = 0;
  244. for (mi_row = 0; mi_row < cm->mi_rows; mi_row++)
  245. for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
  246. if (cyclic_refresh_segment_id(
  247. seg_map[mi_row * cm->mi_cols + mi_col]) == CR_SEGMENT_ID_BOOST1)
  248. cr->actual_num_seg1_blocks++;
  249. else if (cyclic_refresh_segment_id(
  250. seg_map[mi_row * cm->mi_cols + mi_col]) == CR_SEGMENT_ID_BOOST2)
  251. cr->actual_num_seg2_blocks++;
  252. }
  253. }
  254. // Set golden frame update interval, for non-svc 1 pass CBR mode.
  255. void vp9_cyclic_refresh_set_golden_update(VP9_COMP *const cpi) {
  256. RATE_CONTROL *const rc = &cpi->rc;
  257. CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
  258. // Set minimum gf_interval for GF update to a multiple (== 2) of refresh
  259. // period. Depending on past encoding stats, GF flag may be reset and update
  260. // may not occur until next baseline_gf_interval.
  261. if (cr->percent_refresh > 0)
  262. rc->baseline_gf_interval = 4 * (100 / cr->percent_refresh);
  263. else
  264. rc->baseline_gf_interval = 40;
  265. }
  266. // Update some encoding stats (from the just encoded frame). If this frame's
  267. // background has high motion, refresh the golden frame. Otherwise, if the
  268. // golden reference is to be updated check if we should NOT update the golden
  269. // ref.
  270. void vp9_cyclic_refresh_check_golden_update(VP9_COMP *const cpi) {
  271. VP9_COMMON *const cm = &cpi->common;
  272. CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
  273. int mi_row, mi_col;
  274. double fraction_low = 0.0;
  275. int low_content_frame = 0;
  276. MODE_INFO **mi = cm->mi_grid_visible;
  277. RATE_CONTROL *const rc = &cpi->rc;
  278. const int rows = cm->mi_rows, cols = cm->mi_cols;
  279. int cnt1 = 0, cnt2 = 0;
  280. int force_gf_refresh = 0;
  281. for (mi_row = 0; mi_row < rows; mi_row++) {
  282. for (mi_col = 0; mi_col < cols; mi_col++) {
  283. int16_t abs_mvr = mi[0]->mbmi.mv[0].as_mv.row >= 0 ?
  284. mi[0]->mbmi.mv[0].as_mv.row : -1 * mi[0]->mbmi.mv[0].as_mv.row;
  285. int16_t abs_mvc = mi[0]->mbmi.mv[0].as_mv.col >= 0 ?
  286. mi[0]->mbmi.mv[0].as_mv.col : -1 * mi[0]->mbmi.mv[0].as_mv.col;
  287. // Calculate the motion of the background.
  288. if (abs_mvr <= 16 && abs_mvc <= 16) {
  289. cnt1++;
  290. if (abs_mvr == 0 && abs_mvc == 0)
  291. cnt2++;
  292. }
  293. mi++;
  294. // Accumulate low_content_frame.
  295. if (cr->map[mi_row * cols + mi_col] < 1)
  296. low_content_frame++;
  297. }
  298. mi += 8;
  299. }
  300. // For video conference clips, if the background has high motion in current
  301. // frame because of the camera movement, set this frame as the golden frame.
  302. // Use 70% and 5% as the thresholds for golden frame refreshing.
  303. if (cnt1 * 10 > (70 * rows * cols) && cnt2 * 20 < cnt1) {
  304. vp9_cyclic_refresh_set_golden_update(cpi);
  305. rc->frames_till_gf_update_due = rc->baseline_gf_interval;
  306. if (rc->frames_till_gf_update_due > rc->frames_to_key)
  307. rc->frames_till_gf_update_due = rc->frames_to_key;
  308. cpi->refresh_golden_frame = 1;
  309. force_gf_refresh = 1;
  310. }
  311. fraction_low =
  312. (double)low_content_frame / (rows * cols);
  313. // Update average.
  314. cr->low_content_avg = (fraction_low + 3 * cr->low_content_avg) / 4;
  315. if (!force_gf_refresh && cpi->refresh_golden_frame == 1) {
  316. // Don't update golden reference if the amount of low_content for the
  317. // current encoded frame is small, or if the recursive average of the
  318. // low_content over the update interval window falls below threshold.
  319. if (fraction_low < 0.8 || cr->low_content_avg < 0.7)
  320. cpi->refresh_golden_frame = 0;
  321. // Reset for next internal.
  322. cr->low_content_avg = fraction_low;
  323. }
  324. }
  325. // Update the segmentation map, and related quantities: cyclic refresh map,
  326. // refresh sb_index, and target number of blocks to be refreshed.
  327. // The map is set to either 0/CR_SEGMENT_ID_BASE (no refresh) or to
  328. // 1/CR_SEGMENT_ID_BOOST1 (refresh) for each superblock.
  329. // Blocks labeled as BOOST1 may later get set to BOOST2 (during the
  330. // encoding of the superblock).
  331. static void cyclic_refresh_update_map(VP9_COMP *const cpi) {
  332. VP9_COMMON *const cm = &cpi->common;
  333. CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
  334. unsigned char *const seg_map = cpi->segmentation_map;
  335. int i, block_count, bl_index, sb_rows, sb_cols, sbs_in_frame;
  336. int xmis, ymis, x, y;
  337. memset(seg_map, CR_SEGMENT_ID_BASE, cm->mi_rows * cm->mi_cols);
  338. sb_cols = (cm->mi_cols + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
  339. sb_rows = (cm->mi_rows + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
  340. sbs_in_frame = sb_cols * sb_rows;
  341. // Number of target blocks to get the q delta (segment 1).
  342. block_count = cr->percent_refresh * cm->mi_rows * cm->mi_cols / 100;
  343. // Set the segmentation map: cycle through the superblocks, starting at
  344. // cr->mb_index, and stopping when either block_count blocks have been found
  345. // to be refreshed, or we have passed through whole frame.
  346. assert(cr->sb_index < sbs_in_frame);
  347. i = cr->sb_index;
  348. cr->target_num_seg_blocks = 0;
  349. do {
  350. int sum_map = 0;
  351. // Get the mi_row/mi_col corresponding to superblock index i.
  352. int sb_row_index = (i / sb_cols);
  353. int sb_col_index = i - sb_row_index * sb_cols;
  354. int mi_row = sb_row_index * MI_BLOCK_SIZE;
  355. int mi_col = sb_col_index * MI_BLOCK_SIZE;
  356. assert(mi_row >= 0 && mi_row < cm->mi_rows);
  357. assert(mi_col >= 0 && mi_col < cm->mi_cols);
  358. bl_index = mi_row * cm->mi_cols + mi_col;
  359. // Loop through all 8x8 blocks in superblock and update map.
  360. xmis = MIN(cm->mi_cols - mi_col,
  361. num_8x8_blocks_wide_lookup[BLOCK_64X64]);
  362. ymis = MIN(cm->mi_rows - mi_row,
  363. num_8x8_blocks_high_lookup[BLOCK_64X64]);
  364. for (y = 0; y < ymis; y++) {
  365. for (x = 0; x < xmis; x++) {
  366. const int bl_index2 = bl_index + y * cm->mi_cols + x;
  367. // If the block is as a candidate for clean up then mark it
  368. // for possible boost/refresh (segment 1). The segment id may get
  369. // reset to 0 later if block gets coded anything other than ZEROMV.
  370. if (cr->map[bl_index2] == 0) {
  371. sum_map++;
  372. } else if (cr->map[bl_index2] < 0) {
  373. cr->map[bl_index2]++;
  374. }
  375. }
  376. }
  377. // Enforce constant segment over superblock.
  378. // If segment is at least half of superblock, set to 1.
  379. if (sum_map >= xmis * ymis / 2) {
  380. for (y = 0; y < ymis; y++)
  381. for (x = 0; x < xmis; x++) {
  382. seg_map[bl_index + y * cm->mi_cols + x] = CR_SEGMENT_ID_BOOST1;
  383. }
  384. cr->target_num_seg_blocks += xmis * ymis;
  385. }
  386. i++;
  387. if (i == sbs_in_frame) {
  388. i = 0;
  389. }
  390. } while (cr->target_num_seg_blocks < block_count && i != cr->sb_index);
  391. cr->sb_index = i;
  392. }
  393. // Set/update global/frame level cyclic refresh parameters.
  394. void vp9_cyclic_refresh_update_parameters(VP9_COMP *const cpi) {
  395. const RATE_CONTROL *const rc = &cpi->rc;
  396. CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
  397. cr->percent_refresh = 10;
  398. // Use larger delta-qp (increase rate_ratio_qdelta) for first few (~4)
  399. // periods of the refresh cycle, after a key frame. This corresponds to ~40
  400. // frames with cr->percent_refresh = 10.
  401. if (rc->frames_since_key < 40)
  402. cr->rate_ratio_qdelta = 3.0;
  403. else
  404. cr->rate_ratio_qdelta = 2.0;
  405. }
  406. // Setup cyclic background refresh: set delta q and segmentation map.
  407. void vp9_cyclic_refresh_setup(VP9_COMP *const cpi) {
  408. VP9_COMMON *const cm = &cpi->common;
  409. const RATE_CONTROL *const rc = &cpi->rc;
  410. CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
  411. struct segmentation *const seg = &cm->seg;
  412. const int apply_cyclic_refresh = apply_cyclic_refresh_bitrate(cm, rc);
  413. if (cm->current_video_frame == 0)
  414. cr->low_content_avg = 0.0;
  415. // Don't apply refresh on key frame or enhancement layer frames.
  416. if (!apply_cyclic_refresh ||
  417. (cm->frame_type == KEY_FRAME) ||
  418. (cpi->svc.temporal_layer_id > 0) ||
  419. (cpi->svc.spatial_layer_id > 0)) {
  420. // Set segmentation map to 0 and disable.
  421. unsigned char *const seg_map = cpi->segmentation_map;
  422. memset(seg_map, 0, cm->mi_rows * cm->mi_cols);
  423. vp9_disable_segmentation(&cm->seg);
  424. if (cm->frame_type == KEY_FRAME)
  425. cr->sb_index = 0;
  426. return;
  427. } else {
  428. int qindex_delta = 0;
  429. int qindex2;
  430. const double q = vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth);
  431. vp9_clear_system_state();
  432. cr->max_qdelta_perc = 50;
  433. cr->time_for_refresh = 0;
  434. // Set rate threshold to some multiple (set to 2 for now) of the target
  435. // rate (target is given by sb64_target_rate and scaled by 256).
  436. cr->thresh_rate_sb = ((int64_t)(rc->sb64_target_rate) << 8) << 2;
  437. // Distortion threshold, quadratic in Q, scale factor to be adjusted.
  438. // q will not exceed 457, so (q * q) is within 32bit; see:
  439. // vp9_convert_qindex_to_q(), vp9_ac_quant(), ac_qlookup*[].
  440. cr->thresh_dist_sb = ((int64_t)(q * q)) << 2;
  441. cr->motion_thresh = 32;
  442. // Set up segmentation.
  443. // Clear down the segment map.
  444. vp9_enable_segmentation(&cm->seg);
  445. vp9_clearall_segfeatures(seg);
  446. // Select delta coding method.
  447. seg->abs_delta = SEGMENT_DELTADATA;
  448. // Note: setting temporal_update has no effect, as the seg-map coding method
  449. // (temporal or spatial) is determined in vp9_choose_segmap_coding_method(),
  450. // based on the coding cost of each method. For error_resilient mode on the
  451. // last_frame_seg_map is set to 0, so if temporal coding is used, it is
  452. // relative to 0 previous map.
  453. // seg->temporal_update = 0;
  454. // Segment BASE "Q" feature is disabled so it defaults to the baseline Q.
  455. vp9_disable_segfeature(seg, CR_SEGMENT_ID_BASE, SEG_LVL_ALT_Q);
  456. // Use segment BOOST1 for in-frame Q adjustment.
  457. vp9_enable_segfeature(seg, CR_SEGMENT_ID_BOOST1, SEG_LVL_ALT_Q);
  458. // Use segment BOOST2 for more aggressive in-frame Q adjustment.
  459. vp9_enable_segfeature(seg, CR_SEGMENT_ID_BOOST2, SEG_LVL_ALT_Q);
  460. // Set the q delta for segment BOOST1.
  461. qindex_delta = compute_deltaq(cpi, cm->base_qindex, cr->rate_ratio_qdelta);
  462. cr->qindex_delta_seg1 = qindex_delta;
  463. // Compute rd-mult for segment BOOST1.
  464. qindex2 = clamp(cm->base_qindex + cm->y_dc_delta_q + qindex_delta, 0, MAXQ);
  465. cr->rdmult = vp9_compute_rd_mult(cpi, qindex2);
  466. vp9_set_segdata(seg, CR_SEGMENT_ID_BOOST1, SEG_LVL_ALT_Q, qindex_delta);
  467. // Set a more aggressive (higher) q delta for segment BOOST2.
  468. qindex_delta = compute_deltaq(cpi, cm->base_qindex,
  469. MIN(CR_MAX_RATE_TARGET_RATIO,
  470. CR_BOOST2_FAC * cr->rate_ratio_qdelta));
  471. cr->qindex_delta_seg2 = qindex_delta;
  472. vp9_set_segdata(seg, CR_SEGMENT_ID_BOOST2, SEG_LVL_ALT_Q, qindex_delta);
  473. // Update the segmentation and refresh map.
  474. cyclic_refresh_update_map(cpi);
  475. }
  476. }
  477. int vp9_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr) {
  478. return cr->rdmult;
  479. }