2
0

vp9_segmentation.c 9.7 KB

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