LCOV - code coverage report
Current view: top level - third_party/aom/av1/encoder - segmentation.c (source / functions) Hit Total Coverage
Test: output.info Lines: 0 145 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 11 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
       3             :  *
       4             :  * This source code is subject to the terms of the BSD 2 Clause License and
       5             :  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
       6             :  * was not distributed with this source code in the LICENSE file, you can
       7             :  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
       8             :  * Media Patent License 1.0 was not distributed with this source code in the
       9             :  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
      10             :  */
      11             : 
      12             : #include <limits.h>
      13             : 
      14             : #include "aom_mem/aom_mem.h"
      15             : 
      16             : #include "av1/common/pred_common.h"
      17             : #include "av1/common/tile_common.h"
      18             : 
      19             : #include "av1/encoder/cost.h"
      20             : #include "av1/encoder/segmentation.h"
      21             : #include "av1/encoder/subexp.h"
      22             : 
      23           0 : void av1_enable_segmentation(struct segmentation *seg) {
      24           0 :   seg->enabled = 1;
      25           0 :   seg->update_map = 1;
      26           0 :   seg->update_data = 1;
      27           0 : }
      28             : 
      29           0 : void av1_disable_segmentation(struct segmentation *seg) {
      30           0 :   seg->enabled = 0;
      31           0 :   seg->update_map = 0;
      32           0 :   seg->update_data = 0;
      33           0 : }
      34             : 
      35           0 : void av1_set_segment_data(struct segmentation *seg, signed char *feature_data,
      36             :                           unsigned char abs_delta) {
      37           0 :   seg->abs_delta = abs_delta;
      38             : 
      39           0 :   memcpy(seg->feature_data, feature_data, sizeof(seg->feature_data));
      40           0 : }
      41           0 : void av1_disable_segfeature(struct segmentation *seg, int segment_id,
      42             :                             SEG_LVL_FEATURES feature_id) {
      43           0 :   seg->feature_mask[segment_id] &= ~(1 << feature_id);
      44           0 : }
      45             : 
      46           0 : void av1_clear_segdata(struct segmentation *seg, int segment_id,
      47             :                        SEG_LVL_FEATURES feature_id) {
      48           0 :   seg->feature_data[segment_id][feature_id] = 0;
      49           0 : }
      50             : 
      51             : // Based on set of segment counts calculate a probability tree
      52           0 : static void calc_segtree_probs(unsigned *segcounts,
      53             :                                aom_prob *segment_tree_probs,
      54             :                                const aom_prob *cur_tree_probs,
      55             :                                const int probwt) {
      56             :   // Work out probabilities of each segment
      57           0 :   const unsigned cc[4] = { segcounts[0] + segcounts[1],
      58           0 :                            segcounts[2] + segcounts[3],
      59           0 :                            segcounts[4] + segcounts[5],
      60           0 :                            segcounts[6] + segcounts[7] };
      61           0 :   const unsigned ccc[2] = { cc[0] + cc[1], cc[2] + cc[3] };
      62             :   int i;
      63             : 
      64           0 :   segment_tree_probs[0] = get_binary_prob(ccc[0], ccc[1]);
      65           0 :   segment_tree_probs[1] = get_binary_prob(cc[0], cc[1]);
      66           0 :   segment_tree_probs[2] = get_binary_prob(cc[2], cc[3]);
      67           0 :   segment_tree_probs[3] = get_binary_prob(segcounts[0], segcounts[1]);
      68           0 :   segment_tree_probs[4] = get_binary_prob(segcounts[2], segcounts[3]);
      69           0 :   segment_tree_probs[5] = get_binary_prob(segcounts[4], segcounts[5]);
      70           0 :   segment_tree_probs[6] = get_binary_prob(segcounts[6], segcounts[7]);
      71             : 
      72           0 :   for (i = 0; i < 7; i++) {
      73           0 :     const unsigned *ct =
      74           0 :         i == 0 ? ccc : i < 3 ? cc + (i & 2) : segcounts + (i - 3) * 2;
      75           0 :     av1_prob_diff_update_savings_search(ct, cur_tree_probs[i],
      76             :                                         &segment_tree_probs[i],
      77             :                                         DIFF_UPDATE_PROB, probwt);
      78             :   }
      79           0 : }
      80             : 
      81             : // Based on set of segment counts and probabilities calculate a cost estimate
      82           0 : static int cost_segmap(unsigned *segcounts, aom_prob *probs) {
      83           0 :   const int c01 = segcounts[0] + segcounts[1];
      84           0 :   const int c23 = segcounts[2] + segcounts[3];
      85           0 :   const int c45 = segcounts[4] + segcounts[5];
      86           0 :   const int c67 = segcounts[6] + segcounts[7];
      87           0 :   const int c0123 = c01 + c23;
      88           0 :   const int c4567 = c45 + c67;
      89             : 
      90             :   // Cost the top node of the tree
      91           0 :   int cost = c0123 * av1_cost_zero(probs[0]) + c4567 * av1_cost_one(probs[0]);
      92             : 
      93             :   // Cost subsequent levels
      94           0 :   if (c0123 > 0) {
      95           0 :     cost += c01 * av1_cost_zero(probs[1]) + c23 * av1_cost_one(probs[1]);
      96             : 
      97           0 :     if (c01 > 0)
      98           0 :       cost += segcounts[0] * av1_cost_zero(probs[3]) +
      99           0 :               segcounts[1] * av1_cost_one(probs[3]);
     100           0 :     if (c23 > 0)
     101           0 :       cost += segcounts[2] * av1_cost_zero(probs[4]) +
     102           0 :               segcounts[3] * av1_cost_one(probs[4]);
     103             :   }
     104             : 
     105           0 :   if (c4567 > 0) {
     106           0 :     cost += c45 * av1_cost_zero(probs[2]) + c67 * av1_cost_one(probs[2]);
     107             : 
     108           0 :     if (c45 > 0)
     109           0 :       cost += segcounts[4] * av1_cost_zero(probs[5]) +
     110           0 :               segcounts[5] * av1_cost_one(probs[5]);
     111           0 :     if (c67 > 0)
     112           0 :       cost += segcounts[6] * av1_cost_zero(probs[6]) +
     113           0 :               segcounts[7] * av1_cost_one(probs[6]);
     114             :   }
     115             : 
     116           0 :   return cost;
     117             : }
     118             : 
     119           0 : static void count_segs(const AV1_COMMON *cm, MACROBLOCKD *xd,
     120             :                        const TileInfo *tile, MODE_INFO **mi,
     121             :                        unsigned *no_pred_segcounts,
     122             :                        unsigned (*temporal_predictor_count)[2],
     123             :                        unsigned *t_unpred_seg_counts, int bw, int bh,
     124             :                        int mi_row, int mi_col) {
     125             :   int segment_id;
     126             : 
     127           0 :   if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
     128             : 
     129           0 :   xd->mi = mi;
     130           0 :   segment_id = xd->mi[0]->mbmi.segment_id;
     131             : 
     132           0 :   set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw,
     133             : #if CONFIG_DEPENDENT_HORZTILES
     134             :                  cm->dependent_horz_tiles,
     135             : #endif  // CONFIG_DEPENDENT_HORZTILES
     136             :                  cm->mi_rows, cm->mi_cols);
     137             : 
     138             :   // Count the number of hits on each segment with no prediction
     139           0 :   no_pred_segcounts[segment_id]++;
     140             : 
     141             :   // Temporal prediction not allowed on key frames
     142           0 :   if (cm->frame_type != KEY_FRAME) {
     143           0 :     const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
     144             :     // Test to see if the segment id matches the predicted value.
     145           0 :     const int pred_segment_id =
     146           0 :         get_segment_id(cm, cm->last_frame_seg_map, bsize, mi_row, mi_col);
     147           0 :     const int pred_flag = pred_segment_id == segment_id;
     148           0 :     const int pred_context = av1_get_pred_context_seg_id(xd);
     149             : 
     150             :     // Store the prediction status for this mb and update counts
     151             :     // as appropriate
     152           0 :     xd->mi[0]->mbmi.seg_id_predicted = pred_flag;
     153           0 :     temporal_predictor_count[pred_context][pred_flag]++;
     154             : 
     155             :     // Update the "unpredicted" segment count
     156           0 :     if (!pred_flag) t_unpred_seg_counts[segment_id]++;
     157             :   }
     158             : }
     159             : 
     160           0 : static void count_segs_sb(const AV1_COMMON *cm, MACROBLOCKD *xd,
     161             :                           const TileInfo *tile, MODE_INFO **mi,
     162             :                           unsigned *no_pred_segcounts,
     163             :                           unsigned (*temporal_predictor_count)[2],
     164             :                           unsigned *t_unpred_seg_counts, int mi_row, int mi_col,
     165             :                           BLOCK_SIZE bsize) {
     166           0 :   const int mis = cm->mi_stride;
     167           0 :   const int bs = mi_size_wide[bsize], hbs = bs / 2;
     168             : #if CONFIG_EXT_PARTITION_TYPES
     169             :   PARTITION_TYPE partition;
     170             : #else
     171             :   int bw, bh;
     172             : #endif  // CONFIG_EXT_PARTITION_TYPES
     173             : 
     174           0 :   if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
     175             : 
     176             : #if CONFIG_EXT_PARTITION_TYPES
     177             :   if (bsize == BLOCK_8X8)
     178             :     partition = PARTITION_NONE;
     179             :   else
     180             :     partition = get_partition(cm, mi_row, mi_col, bsize);
     181             :   switch (partition) {
     182             :     case PARTITION_NONE:
     183             :       count_segs(cm, xd, tile, mi, no_pred_segcounts, temporal_predictor_count,
     184             :                  t_unpred_seg_counts, bs, bs, mi_row, mi_col);
     185             :       break;
     186             :     case PARTITION_HORZ:
     187             :       count_segs(cm, xd, tile, mi, no_pred_segcounts, temporal_predictor_count,
     188             :                  t_unpred_seg_counts, bs, hbs, mi_row, mi_col);
     189             :       count_segs(cm, xd, tile, mi + hbs * mis, no_pred_segcounts,
     190             :                  temporal_predictor_count, t_unpred_seg_counts, bs, hbs,
     191             :                  mi_row + hbs, mi_col);
     192             :       break;
     193             :     case PARTITION_VERT:
     194             :       count_segs(cm, xd, tile, mi, no_pred_segcounts, temporal_predictor_count,
     195             :                  t_unpred_seg_counts, hbs, bs, mi_row, mi_col);
     196             :       count_segs(cm, xd, tile, mi + hbs, no_pred_segcounts,
     197             :                  temporal_predictor_count, t_unpred_seg_counts, hbs, bs, mi_row,
     198             :                  mi_col + hbs);
     199             :       break;
     200             :     case PARTITION_HORZ_A:
     201             :       count_segs(cm, xd, tile, mi, no_pred_segcounts, temporal_predictor_count,
     202             :                  t_unpred_seg_counts, hbs, hbs, mi_row, mi_col);
     203             :       count_segs(cm, xd, tile, mi + hbs, no_pred_segcounts,
     204             :                  temporal_predictor_count, t_unpred_seg_counts, hbs, hbs,
     205             :                  mi_row, mi_col + hbs);
     206             :       count_segs(cm, xd, tile, mi + hbs * mis, no_pred_segcounts,
     207             :                  temporal_predictor_count, t_unpred_seg_counts, bs, hbs,
     208             :                  mi_row + hbs, mi_col);
     209             :       break;
     210             :     case PARTITION_HORZ_B:
     211             :       count_segs(cm, xd, tile, mi, no_pred_segcounts, temporal_predictor_count,
     212             :                  t_unpred_seg_counts, bs, hbs, mi_row, mi_col);
     213             :       count_segs(cm, xd, tile, mi + hbs * mis, no_pred_segcounts,
     214             :                  temporal_predictor_count, t_unpred_seg_counts, hbs, hbs,
     215             :                  mi_row + hbs, mi_col);
     216             :       count_segs(cm, xd, tile, mi + hbs + hbs * mis, no_pred_segcounts,
     217             :                  temporal_predictor_count, t_unpred_seg_counts, hbs, hbs,
     218             :                  mi_row + hbs, mi_col + hbs);
     219             :       break;
     220             :     case PARTITION_VERT_A:
     221             :       count_segs(cm, xd, tile, mi, no_pred_segcounts, temporal_predictor_count,
     222             :                  t_unpred_seg_counts, hbs, hbs, mi_row, mi_col);
     223             :       count_segs(cm, xd, tile, mi + hbs * mis, no_pred_segcounts,
     224             :                  temporal_predictor_count, t_unpred_seg_counts, hbs, hbs,
     225             :                  mi_row + hbs, mi_col);
     226             :       count_segs(cm, xd, tile, mi + hbs, no_pred_segcounts,
     227             :                  temporal_predictor_count, t_unpred_seg_counts, hbs, bs, mi_row,
     228             :                  mi_col + hbs);
     229             :       break;
     230             :     case PARTITION_VERT_B:
     231             :       count_segs(cm, xd, tile, mi, no_pred_segcounts, temporal_predictor_count,
     232             :                  t_unpred_seg_counts, hbs, bs, mi_row, mi_col);
     233             :       count_segs(cm, xd, tile, mi + hbs, no_pred_segcounts,
     234             :                  temporal_predictor_count, t_unpred_seg_counts, hbs, hbs,
     235             :                  mi_row, mi_col + hbs);
     236             :       count_segs(cm, xd, tile, mi + hbs + hbs * mis, no_pred_segcounts,
     237             :                  temporal_predictor_count, t_unpred_seg_counts, hbs, hbs,
     238             :                  mi_row + hbs, mi_col + hbs);
     239             :       break;
     240             :     case PARTITION_SPLIT: {
     241             :       const BLOCK_SIZE subsize = subsize_lookup[PARTITION_SPLIT][bsize];
     242             :       int n;
     243             : 
     244             :       assert(num_8x8_blocks_wide_lookup[mi[0]->mbmi.sb_type] < bs &&
     245             :              num_8x8_blocks_high_lookup[mi[0]->mbmi.sb_type] < bs);
     246             : 
     247             :       for (n = 0; n < 4; n++) {
     248             :         const int mi_dc = hbs * (n & 1);
     249             :         const int mi_dr = hbs * (n >> 1);
     250             : 
     251             :         count_segs_sb(cm, xd, tile, &mi[mi_dr * mis + mi_dc], no_pred_segcounts,
     252             :                       temporal_predictor_count, t_unpred_seg_counts,
     253             :                       mi_row + mi_dr, mi_col + mi_dc, subsize);
     254             :       }
     255             :     } break;
     256             :     default: assert(0);
     257             :   }
     258             : #else
     259           0 :   bw = mi_size_wide[mi[0]->mbmi.sb_type];
     260           0 :   bh = mi_size_high[mi[0]->mbmi.sb_type];
     261             : 
     262           0 :   if (bw == bs && bh == bs) {
     263           0 :     count_segs(cm, xd, tile, mi, no_pred_segcounts, temporal_predictor_count,
     264             :                t_unpred_seg_counts, bs, bs, mi_row, mi_col);
     265           0 :   } else if (bw == bs && bh < bs) {
     266           0 :     count_segs(cm, xd, tile, mi, no_pred_segcounts, temporal_predictor_count,
     267             :                t_unpred_seg_counts, bs, hbs, mi_row, mi_col);
     268           0 :     count_segs(cm, xd, tile, mi + hbs * mis, no_pred_segcounts,
     269             :                temporal_predictor_count, t_unpred_seg_counts, bs, hbs,
     270             :                mi_row + hbs, mi_col);
     271           0 :   } else if (bw < bs && bh == bs) {
     272           0 :     count_segs(cm, xd, tile, mi, no_pred_segcounts, temporal_predictor_count,
     273             :                t_unpred_seg_counts, hbs, bs, mi_row, mi_col);
     274           0 :     count_segs(cm, xd, tile, mi + hbs, no_pred_segcounts,
     275             :                temporal_predictor_count, t_unpred_seg_counts, hbs, bs, mi_row,
     276             :                mi_col + hbs);
     277             :   } else {
     278           0 :     const BLOCK_SIZE subsize = subsize_lookup[PARTITION_SPLIT][bsize];
     279             :     int n;
     280             : 
     281           0 :     assert(bw < bs && bh < bs);
     282             : 
     283           0 :     for (n = 0; n < 4; n++) {
     284           0 :       const int mi_dc = hbs * (n & 1);
     285           0 :       const int mi_dr = hbs * (n >> 1);
     286             : 
     287           0 :       count_segs_sb(cm, xd, tile, &mi[mi_dr * mis + mi_dc], no_pred_segcounts,
     288             :                     temporal_predictor_count, t_unpred_seg_counts,
     289             :                     mi_row + mi_dr, mi_col + mi_dc, subsize);
     290             :     }
     291             :   }
     292             : #endif  // CONFIG_EXT_PARTITION_TYPES
     293             : }
     294             : 
     295           0 : void av1_choose_segmap_coding_method(AV1_COMMON *cm, MACROBLOCKD *xd) {
     296           0 :   struct segmentation *seg = &cm->seg;
     297           0 :   struct segmentation_probs *segp = &cm->fc->seg;
     298             : 
     299             :   int no_pred_cost;
     300           0 :   int t_pred_cost = INT_MAX;
     301             : 
     302             :   int i, tile_col, tile_row, mi_row, mi_col;
     303             : #if CONFIG_TILE_GROUPS
     304           0 :   const int probwt = cm->num_tg;
     305             : #else
     306             :   const int probwt = 1;
     307             : #endif
     308             : 
     309           0 :   unsigned(*temporal_predictor_count)[2] = cm->counts.seg.pred;
     310           0 :   unsigned *no_pred_segcounts = cm->counts.seg.tree_total;
     311           0 :   unsigned *t_unpred_seg_counts = cm->counts.seg.tree_mispred;
     312             : 
     313             :   aom_prob no_pred_tree[SEG_TREE_PROBS];
     314             :   aom_prob t_pred_tree[SEG_TREE_PROBS];
     315             :   aom_prob t_nopred_prob[PREDICTION_PROBS];
     316             : 
     317             :   (void)xd;
     318             : 
     319             :   // We are about to recompute all the segment counts, so zero the accumulators.
     320           0 :   av1_zero(cm->counts.seg);
     321             : 
     322             :   // First of all generate stats regarding how well the last segment map
     323             :   // predicts this one
     324           0 :   for (tile_row = 0; tile_row < cm->tile_rows; tile_row++) {
     325             :     TileInfo tile_info;
     326           0 :     av1_tile_set_row(&tile_info, cm, tile_row);
     327           0 :     for (tile_col = 0; tile_col < cm->tile_cols; tile_col++) {
     328             :       MODE_INFO **mi_ptr;
     329           0 :       av1_tile_set_col(&tile_info, cm, tile_col);
     330             : #if CONFIG_TILE_GROUPS && CONFIG_DEPENDENT_HORZTILES
     331             :       av1_tile_set_tg_boundary(&tile_info, cm, tile_row, tile_col);
     332             : #endif
     333           0 :       mi_ptr = cm->mi_grid_visible + tile_info.mi_row_start * cm->mi_stride +
     334           0 :                tile_info.mi_col_start;
     335           0 :       for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
     336           0 :            mi_row += cm->mib_size, mi_ptr += cm->mib_size * cm->mi_stride) {
     337           0 :         MODE_INFO **mi = mi_ptr;
     338           0 :         for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
     339           0 :              mi_col += cm->mib_size, mi += cm->mib_size) {
     340           0 :           count_segs_sb(cm, xd, &tile_info, mi, no_pred_segcounts,
     341             :                         temporal_predictor_count, t_unpred_seg_counts, mi_row,
     342           0 :                         mi_col, cm->sb_size);
     343             :         }
     344             :       }
     345             :     }
     346             :   }
     347             : 
     348             :   // Work out probability tree for coding segments without prediction
     349             :   // and the cost.
     350           0 :   calc_segtree_probs(no_pred_segcounts, no_pred_tree, segp->tree_probs, probwt);
     351           0 :   no_pred_cost = cost_segmap(no_pred_segcounts, no_pred_tree);
     352             : 
     353             :   // Key frames cannot use temporal prediction
     354           0 :   if (!frame_is_intra_only(cm) && !cm->error_resilient_mode) {
     355             :     // Work out probability tree for coding those segments not
     356             :     // predicted using the temporal method and the cost.
     357           0 :     calc_segtree_probs(t_unpred_seg_counts, t_pred_tree, segp->tree_probs,
     358             :                        probwt);
     359           0 :     t_pred_cost = cost_segmap(t_unpred_seg_counts, t_pred_tree);
     360             : 
     361             :     // Add in the cost of the signaling for each prediction context.
     362           0 :     for (i = 0; i < PREDICTION_PROBS; i++) {
     363           0 :       const int count0 = temporal_predictor_count[i][0];
     364           0 :       const int count1 = temporal_predictor_count[i][1];
     365             : 
     366           0 :       t_nopred_prob[i] = get_binary_prob(count0, count1);
     367           0 :       av1_prob_diff_update_savings_search(
     368           0 :           temporal_predictor_count[i], segp->pred_probs[i], &t_nopred_prob[i],
     369             :           DIFF_UPDATE_PROB, probwt);
     370             : 
     371             :       // Add in the predictor signaling cost
     372           0 :       t_pred_cost += count0 * av1_cost_zero(t_nopred_prob[i]) +
     373           0 :                      count1 * av1_cost_one(t_nopred_prob[i]);
     374             :     }
     375             :   }
     376             : 
     377             :   // Now choose which coding method to use.
     378           0 :   if (t_pred_cost < no_pred_cost) {
     379           0 :     assert(!cm->error_resilient_mode);
     380           0 :     seg->temporal_update = 1;
     381             :   } else {
     382           0 :     seg->temporal_update = 0;
     383             :   }
     384           0 : }
     385             : 
     386           0 : void av1_reset_segment_features(AV1_COMMON *cm) {
     387           0 :   struct segmentation *seg = &cm->seg;
     388             : 
     389             :   // Set up default state for MB feature flags
     390           0 :   seg->enabled = 0;
     391           0 :   seg->update_map = 0;
     392           0 :   seg->update_data = 0;
     393           0 :   av1_clearall_segfeatures(seg);
     394           0 : }

Generated by: LCOV version 1.13