Coverage Report

Created: 2026-12-01 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/avm/av2/decoder/detokenize.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
3
 *
4
 * This source code is subject to the terms of the BSD 3-Clause Clear License
5
 * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
6
 * License was not distributed with this source code in the LICENSE file, you
7
 * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/.  If the
8
 * Alliance for Open Media Patent License 1.0 was not distributed with this
9
 * source code in the PATENTS file, you can obtain it at
10
 * aomedia.org/license/patent-license/.
11
 */
12
13
#include "config/avm_config.h"
14
15
#include "avm_mem/avm_mem.h"
16
#include "avm_ports/mem.h"
17
#include "av2/common/blockd.h"
18
#include "av2/decoder/detokenize.h"
19
20
#include "av2/common/common.h"
21
#include "av2/common/entropy.h"
22
#include "av2/common/idct.h"
23
24
// Read direction flag, then read line flags, and palette tokens one line at at
25
// a time. Returns 1 for sucess.
26
11.6k
static int decode_color_map_tokens(Av2ColorMapParam *param, avm_reader *r) {
27
11.6k
  uint8_t color_order[PALETTE_MAX_SIZE];
28
11.6k
  const int num_colors = param->n_colors;
29
11.6k
  uint8_t *const color_map = param->color_map;
30
11.6k
  MapCdf color_map_cdf = param->map_cdf;
31
11.6k
  int plane_block_width = param->plane_width;
32
11.6k
  int plane_block_height = param->plane_height;
33
11.6k
  int rows = param->rows;
34
11.6k
  int cols = param->cols;
35
11.6k
  const bool transverse_allowed =
36
11.6k
      plane_block_width < 64 && plane_block_height < 64;
37
11.6k
  int direction;
38
11.6k
  if (transverse_allowed) {
39
9.25k
    direction = avm_read_bit(r, ACCT_INFO());
40
9.25k
  } else {
41
2.44k
    direction = 0;
42
2.44k
  }
43
11.6k
  const int axis1_limit = direction ? rows : cols;
44
11.6k
  const int axis2_limit = direction ? cols : rows;
45
46
11.6k
  IdentityRowCdf identity_row_cdf = param->identity_row_cdf;
47
11.6k
  int prev_identity_row_flag = 0;
48
277k
  for (int ax2 = 0; ax2 < axis2_limit; ax2++) {
49
265k
    const int ctx = ax2 == 0 ? 3 : prev_identity_row_flag;
50
265k
    int identity_row_flag =
51
265k
        avm_read_symbol(r, identity_row_cdf[ctx], 3, ACCT_INFO());
52
53
    // Copying previous line cannot be done on the first line of a block.
54
265k
    if (identity_row_flag == 2 && ax2 == 0) {
55
7
      return 0;
56
7
    }
57
58
8.31M
    for (int ax1 = 0; ax1 < axis1_limit; ax1++) {
59
8.05M
      const int y = direction ? ax1 : ax2;
60
8.05M
      const int x = direction ? ax2 : ax1;
61
8.05M
      if (identity_row_flag == 2) {
62
5.50M
        if (direction) {
63
          // Vertical
64
873k
          color_map[y * plane_block_width + x] =
65
873k
              color_map[y * plane_block_width + (x - 1)];
66
4.63M
        } else {
67
          // horizontal
68
4.63M
          color_map[y * plane_block_width + x] =
69
4.63M
              color_map[(y - 1) * plane_block_width + x];
70
4.63M
        }
71
5.50M
      } else if (identity_row_flag == 1 && ax1 > 0) {
72
445k
        if (direction) {
73
          // Vertical
74
90.8k
          color_map[y * plane_block_width + x] =
75
90.8k
              color_map[(y - 1) * plane_block_width + x];
76
354k
        } else {
77
          // horizontal
78
354k
          color_map[y * plane_block_width + x] =
79
354k
              color_map[y * plane_block_width + x - 1];
80
354k
        }
81
2.09M
      } else if (ax2 == 0 && ax1 == 0) {
82
11.6k
        color_map[0] = av2_read_uniform(r, num_colors);
83
2.08M
      } else {
84
2.08M
        const int color_ctx = av2_get_palette_color_index_context(
85
2.08M
            color_map, plane_block_width, y, x, color_order, NULL);
86
2.08M
        const int color_idx = avm_read_symbol(
87
2.08M
            r, color_map_cdf[num_colors - PALETTE_MIN_SIZE][color_ctx],
88
2.08M
            num_colors, ACCT_INFO());
89
2.08M
        assert(color_idx >= 0 && color_idx < num_colors);
90
2.08M
        color_map[y * plane_block_width + x] = color_order[color_idx];
91
        // color_map[ax2 * stride + ax1] = color_order[color_idx];
92
2.08M
      }
93
8.05M
    }
94
265k
    prev_identity_row_flag = identity_row_flag;
95
265k
  }
96
  // Copy last column to extra columns.
97
11.6k
  if (cols < plane_block_width) {
98
18.2k
    for (int i = 0; i < rows; ++i) {
99
17.9k
      memset(color_map + i * plane_block_width + cols,
100
17.9k
             color_map[i * plane_block_width + cols - 1],
101
17.9k
             (plane_block_width - cols));
102
17.9k
    }
103
281
  }
104
  // Copy last row to extra rows.
105
11.9k
  for (int i = rows; i < plane_block_height; ++i) {
106
264
    memcpy(color_map + i * plane_block_width,
107
264
           color_map + (rows - 1) * plane_block_width, plane_block_width);
108
264
  }
109
11.6k
  return 1;
110
11.6k
}
111
112
void av2_decode_palette_tokens(MACROBLOCKD *const xd, int plane,
113
11.6k
                               avm_reader *r) {
114
11.6k
  assert(plane == 0 || plane == 1);
115
11.6k
  Av2ColorMapParam params;
116
11.6k
  params.color_map =
117
11.6k
      xd->plane[plane].color_index_map + xd->color_index_map_offset[plane];
118
11.6k
  params.map_cdf = xd->tile_ctx->palette_y_color_index_cdf;
119
11.6k
  params.identity_row_cdf = plane ? xd->tile_ctx->identity_row_cdf_uv
120
11.6k
                                  : xd->tile_ctx->identity_row_cdf_y;
121
11.6k
  const MB_MODE_INFO *const mbmi = xd->mi[0];
122
11.6k
  params.n_colors = mbmi->palette_mode_info.palette_size[plane];
123
11.6k
  av2_get_block_dimensions(mbmi->sb_type[plane > 0], plane, xd,
124
11.6k
                           &params.plane_width, &params.plane_height,
125
11.6k
                           &params.rows, &params.cols);
126
11.6k
  if (!decode_color_map_tokens(&params, r)) {
127
7
    avm_internal_error(xd->error_info, AVM_CODEC_ERROR,
128
7
                       "Error decoding palette tokens");
129
7
  }
130
11.6k
}