vp9_segmentation.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (c) 2012 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 "vpx_mem/vpx_mem.h"
  13. #include "vp9/common/vp9_pred_common.h"
  14. #include "vp9/common/vp9_tile_common.h"
  15. #include "vp9/encoder/vp9_cost.h"
  16. #include "vp9/encoder/vp9_segmentation.h"
  17. void vp9_enable_segmentation(struct segmentation *seg) {
  18. seg->enabled = 1;
  19. seg->update_map = 1;
  20. seg->update_data = 1;
  21. }
  22. void vp9_disable_segmentation(struct segmentation *seg) {
  23. seg->enabled = 0;
  24. seg->update_map = 0;
  25. seg->update_data = 0;
  26. }
  27. void vp9_set_segment_data(struct segmentation *seg, signed char *feature_data,
  28. unsigned char abs_delta) {
  29. seg->abs_delta = abs_delta;
  30. memcpy(seg->feature_data, feature_data, sizeof(seg->feature_data));
  31. }
  32. void vp9_disable_segfeature(struct segmentation *seg, int segment_id,
  33. SEG_LVL_FEATURES feature_id) {
  34. seg->feature_mask[segment_id] &= ~(1 << feature_id);
  35. }
  36. void vp9_clear_segdata(struct segmentation *seg, int segment_id,
  37. SEG_LVL_FEATURES feature_id) {
  38. seg->feature_data[segment_id][feature_id] = 0;
  39. }
  40. void vp9_psnr_aq_mode_setup(struct segmentation *seg) {
  41. int i;
  42. vp9_enable_segmentation(seg);
  43. vp9_clearall_segfeatures(seg);
  44. seg->abs_delta = SEGMENT_DELTADATA;
  45. for (i = 0; i < MAX_SEGMENTS; ++i) {
  46. vp9_set_segdata(seg, i, SEG_LVL_ALT_Q, 2 * (i - (MAX_SEGMENTS / 2)));
  47. vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q);
  48. }
  49. }
  50. void vp9_perceptual_aq_mode_setup(struct VP9_COMP *cpi,
  51. struct segmentation *seg) {
  52. const VP9_COMMON *cm = &cpi->common;
  53. const int seg_counts = cpi->kmeans_ctr_num;
  54. const int base_qindex = cm->base_qindex;
  55. const double base_qstep = vp9_convert_qindex_to_q(base_qindex, cm->bit_depth);
  56. const double mid_ctr = cpi->kmeans_ctr_ls[seg_counts / 2];
  57. const double var_diff_scale = 4.0;
  58. int i;
  59. assert(seg_counts <= MAX_SEGMENTS);
  60. vp9_enable_segmentation(seg);
  61. vp9_clearall_segfeatures(seg);
  62. seg->abs_delta = SEGMENT_DELTADATA;
  63. for (i = 0; i < seg_counts / 2; ++i) {
  64. double wiener_var_diff = mid_ctr - cpi->kmeans_ctr_ls[i];
  65. double target_qstep = base_qstep / (1.0 + wiener_var_diff / var_diff_scale);
  66. int target_qindex = vp9_convert_q_to_qindex(target_qstep, cm->bit_depth);
  67. assert(wiener_var_diff >= 0.0);
  68. vp9_set_segdata(seg, i, SEG_LVL_ALT_Q, target_qindex - base_qindex);
  69. vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q);
  70. }
  71. vp9_set_segdata(seg, i, SEG_LVL_ALT_Q, 0);
  72. vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q);
  73. for (; i < seg_counts; ++i) {
  74. double wiener_var_diff = cpi->kmeans_ctr_ls[i] - mid_ctr;
  75. double target_qstep = base_qstep * (1.0 + wiener_var_diff / var_diff_scale);
  76. int target_qindex = vp9_convert_q_to_qindex(target_qstep, cm->bit_depth);
  77. assert(wiener_var_diff >= 0.0);
  78. vp9_set_segdata(seg, i, SEG_LVL_ALT_Q, target_qindex - base_qindex);
  79. vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q);
  80. }
  81. }
  82. // Based on set of segment counts calculate a probability tree
  83. static void calc_segtree_probs(int *segcounts, vpx_prob *segment_tree_probs) {
  84. // Work out probabilities of each segment
  85. const int c01 = segcounts[0] + segcounts[1];
  86. const int c23 = segcounts[2] + segcounts[3];
  87. const int c45 = segcounts[4] + segcounts[5];
  88. const int c67 = segcounts[6] + segcounts[7];
  89. segment_tree_probs[0] = get_binary_prob(c01 + c23, c45 + c67);
  90. segment_tree_probs[1] = get_binary_prob(c01, c23);
  91. segment_tree_probs[2] = get_binary_prob(c45, c67);
  92. segment_tree_probs[3] = get_binary_prob(segcounts[0], segcounts[1]);
  93. segment_tree_probs[4] = get_binary_prob(segcounts[2], segcounts[3]);
  94. segment_tree_probs[5] = get_binary_prob(segcounts[4], segcounts[5]);
  95. segment_tree_probs[6] = get_binary_prob(segcounts[6], segcounts[7]);
  96. }
  97. // Based on set of segment counts and probabilities calculate a cost estimate
  98. static int cost_segmap(int *segcounts, vpx_prob *probs) {
  99. const int c01 = segcounts[0] + segcounts[1];
  100. const int c23 = segcounts[2] + segcounts[3];
  101. const int c45 = segcounts[4] + segcounts[5];
  102. const int c67 = segcounts[6] + segcounts[7];
  103. const int c0123 = c01 + c23;
  104. const int c4567 = c45 + c67;
  105. // Cost the top node of the tree
  106. int cost = c0123 * vp9_cost_zero(probs[0]) + c4567 * vp9_cost_one(probs[0]);
  107. // Cost subsequent levels
  108. if (c0123 > 0) {
  109. cost += c01 * vp9_cost_zero(probs[1]) + c23 * vp9_cost_one(probs[1]);
  110. if (c01 > 0)
  111. cost += segcounts[0] * vp9_cost_zero(probs[3]) +
  112. segcounts[1] * vp9_cost_one(probs[3]);
  113. if (c23 > 0)
  114. cost += segcounts[2] * vp9_cost_zero(probs[4]) +
  115. segcounts[3] * vp9_cost_one(probs[4]);
  116. }
  117. if (c4567 > 0) {
  118. cost += c45 * vp9_cost_zero(probs[2]) + c67 * vp9_cost_one(probs[2]);
  119. if (c45 > 0)
  120. cost += segcounts[4] * vp9_cost_zero(probs[5]) +
  121. segcounts[5] * vp9_cost_one(probs[5]);
  122. if (c67 > 0)
  123. cost += segcounts[6] * vp9_cost_zero(probs[6]) +
  124. segcounts[7] * vp9_cost_one(probs[6]);
  125. }
  126. return cost;
  127. }
  128. static void count_segs(const VP9_COMMON *cm, MACROBLOCKD *xd,
  129. const TileInfo *tile, MODE_INFO **mi,
  130. int *no_pred_segcounts,
  131. int (*temporal_predictor_count)[2],
  132. int *t_unpred_seg_counts, int bw, int bh, int mi_row,
  133. int mi_col) {
  134. int segment_id;
  135. if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
  136. xd->mi = mi;
  137. segment_id = xd->mi[0]->segment_id;
  138. set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
  139. // Count the number of hits on each segment with no prediction
  140. no_pred_segcounts[segment_id]++;
  141. // Temporal prediction not allowed on key frames
  142. if (cm->frame_type != KEY_FRAME) {
  143. const BLOCK_SIZE bsize = xd->mi[0]->sb_type;
  144. // Test to see if the segment id matches the predicted value.
  145. const int pred_segment_id =
  146. get_segment_id(cm, cm->last_frame_seg_map, bsize, mi_row, mi_col);
  147. const int pred_flag = pred_segment_id == segment_id;
  148. const int pred_context = vp9_get_pred_context_seg_id(xd);
  149. // Store the prediction status for this mb and update counts
  150. // as appropriate
  151. xd->mi[0]->seg_id_predicted = pred_flag;
  152. temporal_predictor_count[pred_context][pred_flag]++;
  153. // Update the "unpredicted" segment count
  154. if (!pred_flag) t_unpred_seg_counts[segment_id]++;
  155. }
  156. }
  157. static void count_segs_sb(const VP9_COMMON *cm, MACROBLOCKD *xd,
  158. const TileInfo *tile, MODE_INFO **mi,
  159. int *no_pred_segcounts,
  160. int (*temporal_predictor_count)[2],
  161. int *t_unpred_seg_counts, int mi_row, int mi_col,
  162. BLOCK_SIZE bsize) {
  163. const int mis = cm->mi_stride;
  164. int bw, bh;
  165. const int bs = num_8x8_blocks_wide_lookup[bsize], hbs = bs / 2;
  166. if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
  167. bw = num_8x8_blocks_wide_lookup[mi[0]->sb_type];
  168. bh = num_8x8_blocks_high_lookup[mi[0]->sb_type];
  169. if (bw == bs && bh == bs) {
  170. count_segs(cm, xd, tile, mi, no_pred_segcounts, temporal_predictor_count,
  171. t_unpred_seg_counts, bs, bs, mi_row, mi_col);
  172. } else if (bw == bs && bh < bs) {
  173. count_segs(cm, xd, tile, mi, no_pred_segcounts, temporal_predictor_count,
  174. t_unpred_seg_counts, bs, hbs, mi_row, mi_col);
  175. count_segs(cm, xd, tile, mi + hbs * mis, no_pred_segcounts,
  176. temporal_predictor_count, t_unpred_seg_counts, bs, hbs,
  177. mi_row + hbs, mi_col);
  178. } else if (bw < bs && bh == bs) {
  179. count_segs(cm, xd, tile, mi, no_pred_segcounts, temporal_predictor_count,
  180. t_unpred_seg_counts, hbs, bs, mi_row, mi_col);
  181. count_segs(cm, xd, tile, mi + hbs, no_pred_segcounts,
  182. temporal_predictor_count, t_unpred_seg_counts, hbs, bs, mi_row,
  183. mi_col + hbs);
  184. } else {
  185. const BLOCK_SIZE subsize = subsize_lookup[PARTITION_SPLIT][bsize];
  186. int n;
  187. assert(bw < bs && bh < bs);
  188. for (n = 0; n < 4; n++) {
  189. const int mi_dc = hbs * (n & 1);
  190. const int mi_dr = hbs * (n >> 1);
  191. count_segs_sb(cm, xd, tile, &mi[mi_dr * mis + mi_dc], no_pred_segcounts,
  192. temporal_predictor_count, t_unpred_seg_counts,
  193. mi_row + mi_dr, mi_col + mi_dc, subsize);
  194. }
  195. }
  196. }
  197. void vp9_choose_segmap_coding_method(VP9_COMMON *cm, MACROBLOCKD *xd) {
  198. struct segmentation *seg = &cm->seg;
  199. int no_pred_cost;
  200. int t_pred_cost = INT_MAX;
  201. int i, tile_col, mi_row, mi_col;
  202. int temporal_predictor_count[PREDICTION_PROBS][2] = { { 0 } };
  203. int no_pred_segcounts[MAX_SEGMENTS] = { 0 };
  204. int t_unpred_seg_counts[MAX_SEGMENTS] = { 0 };
  205. vpx_prob no_pred_tree[SEG_TREE_PROBS];
  206. vpx_prob t_pred_tree[SEG_TREE_PROBS];
  207. vpx_prob t_nopred_prob[PREDICTION_PROBS];
  208. // Set default state for the segment tree probabilities and the
  209. // temporal coding probabilities
  210. memset(seg->tree_probs, 255, sizeof(seg->tree_probs));
  211. memset(seg->pred_probs, 255, sizeof(seg->pred_probs));
  212. // First of all generate stats regarding how well the last segment map
  213. // predicts this one
  214. for (tile_col = 0; tile_col < 1 << cm->log2_tile_cols; tile_col++) {
  215. TileInfo tile;
  216. MODE_INFO **mi_ptr;
  217. vp9_tile_init(&tile, cm, 0, tile_col);
  218. mi_ptr = cm->mi_grid_visible + tile.mi_col_start;
  219. for (mi_row = 0; mi_row < cm->mi_rows;
  220. mi_row += 8, mi_ptr += 8 * cm->mi_stride) {
  221. MODE_INFO **mi = mi_ptr;
  222. for (mi_col = tile.mi_col_start; mi_col < tile.mi_col_end;
  223. mi_col += 8, mi += 8)
  224. count_segs_sb(cm, xd, &tile, mi, no_pred_segcounts,
  225. temporal_predictor_count, t_unpred_seg_counts, mi_row,
  226. mi_col, BLOCK_64X64);
  227. }
  228. }
  229. // Work out probability tree for coding segments without prediction
  230. // and the cost.
  231. calc_segtree_probs(no_pred_segcounts, no_pred_tree);
  232. no_pred_cost = cost_segmap(no_pred_segcounts, no_pred_tree);
  233. // Key frames cannot use temporal prediction
  234. if (!frame_is_intra_only(cm)) {
  235. // Work out probability tree for coding those segments not
  236. // predicted using the temporal method and the cost.
  237. calc_segtree_probs(t_unpred_seg_counts, t_pred_tree);
  238. t_pred_cost = cost_segmap(t_unpred_seg_counts, t_pred_tree);
  239. // Add in the cost of the signaling for each prediction context.
  240. for (i = 0; i < PREDICTION_PROBS; i++) {
  241. const int count0 = temporal_predictor_count[i][0];
  242. const int count1 = temporal_predictor_count[i][1];
  243. t_nopred_prob[i] = get_binary_prob(count0, count1);
  244. // Add in the predictor signaling cost
  245. t_pred_cost += count0 * vp9_cost_zero(t_nopred_prob[i]) +
  246. count1 * vp9_cost_one(t_nopred_prob[i]);
  247. }
  248. }
  249. // Now choose which coding method to use.
  250. if (t_pred_cost < no_pred_cost) {
  251. seg->temporal_update = 1;
  252. memcpy(seg->tree_probs, t_pred_tree, sizeof(t_pred_tree));
  253. memcpy(seg->pred_probs, t_nopred_prob, sizeof(t_nopred_prob));
  254. } else {
  255. seg->temporal_update = 0;
  256. memcpy(seg->tree_probs, no_pred_tree, sizeof(no_pred_tree));
  257. }
  258. }
  259. void vp9_reset_segment_features(struct segmentation *seg) {
  260. // Set up default state for MB feature flags
  261. seg->enabled = 0;
  262. seg->update_map = 0;
  263. seg->update_data = 0;
  264. memset(seg->tree_probs, 255, sizeof(seg->tree_probs));
  265. vp9_clearall_segfeatures(seg);
  266. }