xref: /aosp_15_r20/external/libaom/av1/decoder/detokenize.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
13*77c1e3ccSAndroid Build Coastguard Worker 
14*77c1e3ccSAndroid Build Coastguard Worker #include "aom_mem/aom_mem.h"
15*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/mem.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/blockd.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "av1/decoder/detokenize.h"
18*77c1e3ccSAndroid Build Coastguard Worker 
19*77c1e3ccSAndroid Build Coastguard Worker #define ACCT_STR __func__
20*77c1e3ccSAndroid Build Coastguard Worker 
21*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/common.h"
22*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/entropy.h"
23*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/idct.h"
24*77c1e3ccSAndroid Build Coastguard Worker 
decode_color_map_tokens(Av1ColorMapParam * param,aom_reader * r)25*77c1e3ccSAndroid Build Coastguard Worker static void decode_color_map_tokens(Av1ColorMapParam *param, aom_reader *r) {
26*77c1e3ccSAndroid Build Coastguard Worker   uint8_t color_order[PALETTE_MAX_SIZE];
27*77c1e3ccSAndroid Build Coastguard Worker   const int n = param->n_colors;
28*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *const color_map = param->color_map;
29*77c1e3ccSAndroid Build Coastguard Worker   MapCdf color_map_cdf = param->map_cdf;
30*77c1e3ccSAndroid Build Coastguard Worker   int plane_block_width = param->plane_width;
31*77c1e3ccSAndroid Build Coastguard Worker   int plane_block_height = param->plane_height;
32*77c1e3ccSAndroid Build Coastguard Worker   int rows = param->rows;
33*77c1e3ccSAndroid Build Coastguard Worker   int cols = param->cols;
34*77c1e3ccSAndroid Build Coastguard Worker 
35*77c1e3ccSAndroid Build Coastguard Worker   // The first color index.
36*77c1e3ccSAndroid Build Coastguard Worker   color_map[0] = av1_read_uniform(r, n);
37*77c1e3ccSAndroid Build Coastguard Worker   assert(color_map[0] < n);
38*77c1e3ccSAndroid Build Coastguard Worker 
39*77c1e3ccSAndroid Build Coastguard Worker   // Run wavefront on the palette map index decoding.
40*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 1; i < rows + cols - 1; ++i) {
41*77c1e3ccSAndroid Build Coastguard Worker     for (int j = AOMMIN(i, cols - 1); j >= AOMMAX(0, i - rows + 1); --j) {
42*77c1e3ccSAndroid Build Coastguard Worker       const int color_ctx = av1_get_palette_color_index_context(
43*77c1e3ccSAndroid Build Coastguard Worker           color_map, plane_block_width, (i - j), j, n, color_order, NULL);
44*77c1e3ccSAndroid Build Coastguard Worker       const int color_idx = aom_read_symbol(
45*77c1e3ccSAndroid Build Coastguard Worker           r, color_map_cdf[n - PALETTE_MIN_SIZE][color_ctx], n, ACCT_STR);
46*77c1e3ccSAndroid Build Coastguard Worker       assert(color_idx >= 0 && color_idx < n);
47*77c1e3ccSAndroid Build Coastguard Worker       color_map[(i - j) * plane_block_width + j] = color_order[color_idx];
48*77c1e3ccSAndroid Build Coastguard Worker     }
49*77c1e3ccSAndroid Build Coastguard Worker   }
50*77c1e3ccSAndroid Build Coastguard Worker   // Copy last column to extra columns.
51*77c1e3ccSAndroid Build Coastguard Worker   if (cols < plane_block_width) {
52*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < rows; ++i) {
53*77c1e3ccSAndroid Build Coastguard Worker       memset(color_map + i * plane_block_width + cols,
54*77c1e3ccSAndroid Build Coastguard Worker              color_map[i * plane_block_width + cols - 1],
55*77c1e3ccSAndroid Build Coastguard Worker              (plane_block_width - cols));
56*77c1e3ccSAndroid Build Coastguard Worker     }
57*77c1e3ccSAndroid Build Coastguard Worker   }
58*77c1e3ccSAndroid Build Coastguard Worker   // Copy last row to extra rows.
59*77c1e3ccSAndroid Build Coastguard Worker   for (int i = rows; i < plane_block_height; ++i) {
60*77c1e3ccSAndroid Build Coastguard Worker     memcpy(color_map + i * plane_block_width,
61*77c1e3ccSAndroid Build Coastguard Worker            color_map + (rows - 1) * plane_block_width, plane_block_width);
62*77c1e3ccSAndroid Build Coastguard Worker   }
63*77c1e3ccSAndroid Build Coastguard Worker }
64*77c1e3ccSAndroid Build Coastguard Worker 
av1_decode_palette_tokens(MACROBLOCKD * const xd,int plane,aom_reader * r)65*77c1e3ccSAndroid Build Coastguard Worker void av1_decode_palette_tokens(MACROBLOCKD *const xd, int plane,
66*77c1e3ccSAndroid Build Coastguard Worker                                aom_reader *r) {
67*77c1e3ccSAndroid Build Coastguard Worker   assert(plane == 0 || plane == 1);
68*77c1e3ccSAndroid Build Coastguard Worker   Av1ColorMapParam params;
69*77c1e3ccSAndroid Build Coastguard Worker   params.color_map =
70*77c1e3ccSAndroid Build Coastguard Worker       xd->plane[plane].color_index_map + xd->color_index_map_offset[plane];
71*77c1e3ccSAndroid Build Coastguard Worker   params.map_cdf = plane ? xd->tile_ctx->palette_uv_color_index_cdf
72*77c1e3ccSAndroid Build Coastguard Worker                          : xd->tile_ctx->palette_y_color_index_cdf;
73*77c1e3ccSAndroid Build Coastguard Worker   const MB_MODE_INFO *const mbmi = xd->mi[0];
74*77c1e3ccSAndroid Build Coastguard Worker   params.n_colors = mbmi->palette_mode_info.palette_size[plane];
75*77c1e3ccSAndroid Build Coastguard Worker   av1_get_block_dimensions(mbmi->bsize, plane, xd, &params.plane_width,
76*77c1e3ccSAndroid Build Coastguard Worker                            &params.plane_height, &params.rows, &params.cols);
77*77c1e3ccSAndroid Build Coastguard Worker   decode_color_map_tokens(&params, r);
78*77c1e3ccSAndroid Build Coastguard Worker }
79