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 "config/aom_config.h"
13 
14 #include "aom_mem/aom_mem.h"
15 #include "aom_ports/mem.h"
16 #include "av1/common/blockd.h"
17 #include "av1/decoder/detokenize.h"
18 
19 #define ACCT_STR __func__
20 
21 #include "av1/common/common.h"
22 #include "av1/common/entropy.h"
23 #include "av1/common/idct.h"
24 
decode_color_map_tokens(Av1ColorMapParam * param,aom_reader * r)25 static void decode_color_map_tokens(Av1ColorMapParam *param, aom_reader *r) {
26   uint8_t color_order[PALETTE_MAX_SIZE];
27   const int n = param->n_colors;
28   uint8_t *const color_map = param->color_map;
29   MapCdf color_map_cdf = param->map_cdf;
30   int plane_block_width = param->plane_width;
31   int plane_block_height = param->plane_height;
32   int rows = param->rows;
33   int cols = param->cols;
34 
35   // The first color index.
36   color_map[0] = av1_read_uniform(r, n);
37   assert(color_map[0] < n);
38 
39   // Run wavefront on the palette map index decoding.
40   for (int i = 1; i < rows + cols - 1; ++i) {
41     for (int j = AOMMIN(i, cols - 1); j >= AOMMAX(0, i - rows + 1); --j) {
42       const int color_ctx = av1_get_palette_color_index_context(
43           color_map, plane_block_width, (i - j), j, n, color_order, NULL);
44       const int color_idx = aom_read_symbol(
45           r, color_map_cdf[n - PALETTE_MIN_SIZE][color_ctx], n, ACCT_STR);
46       assert(color_idx >= 0 && color_idx < n);
47       color_map[(i - j) * plane_block_width + j] = color_order[color_idx];
48     }
49   }
50   // Copy last column to extra columns.
51   if (cols < plane_block_width) {
52     for (int i = 0; i < rows; ++i) {
53       memset(color_map + i * plane_block_width + cols,
54              color_map[i * plane_block_width + cols - 1],
55              (plane_block_width - cols));
56     }
57   }
58   // Copy last row to extra rows.
59   for (int i = rows; i < plane_block_height; ++i) {
60     memcpy(color_map + i * plane_block_width,
61            color_map + (rows - 1) * plane_block_width, plane_block_width);
62   }
63 }
64 
av1_decode_palette_tokens(MACROBLOCKD * const xd,int plane,aom_reader * r)65 void av1_decode_palette_tokens(MACROBLOCKD *const xd, int plane,
66                                aom_reader *r) {
67   assert(plane == 0 || plane == 1);
68   Av1ColorMapParam params;
69   params.color_map =
70       xd->plane[plane].color_index_map + xd->color_index_map_offset[plane];
71   params.map_cdf = plane ? xd->tile_ctx->palette_uv_color_index_cdf
72                          : xd->tile_ctx->palette_y_color_index_cdf;
73   const MB_MODE_INFO *const mbmi = xd->mi[0];
74   params.n_colors = mbmi->palette_mode_info.palette_size[plane];
75   av1_get_block_dimensions(mbmi->sb_type, plane, xd, &params.plane_width,
76                            &params.plane_height, &params.rows, &params.cols);
77   decode_color_map_tokens(&params, r);
78 }
79