vp9_segmentation.c 9.9 KB

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