blob: 1fb2975c3d075acef48a4f9f2346dc03f70d0927 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Krishna Rapaka7319db52021-09-28 20:35:29 -07002 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Vibhoothi41c6dd72021-10-12 18:48:26 +00004 * 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/.
Yaowu Xuc27fc142016-08-22 16:08:15 -070011 */
12
Tom Finegan60e653d2018-05-22 11:34:58 -070013#include "config/aom_config.h"
14
Yaowu Xuf883b422016-08-30 14:01:10 -070015#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070016#include "aom_ports/mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070017#include "av1/common/blockd.h"
hui su40b9e7f2017-07-13 18:15:56 -070018#include "av1/decoder/detokenize.h"
Yushin Choe5800922017-02-22 10:15:22 -080019
Thomas Daedee1227d52017-03-07 14:44:27 -080020#define ACCT_STR __func__
21
Yaowu Xuc27fc142016-08-22 16:08:15 -070022#include "av1/common/common.h"
23#include "av1/common/entropy.h"
24#include "av1/common/idct.h"
Angie Chiang3a769692017-10-01 18:26:48 -070025
Sarah Parkerefd8af22017-08-25 16:36:06 -070026static void decode_color_map_tokens(Av1ColorMapParam *param, aom_reader *r) {
Fangwen Fu33bcd112017-02-07 16:42:41 -080027 uint8_t color_order[PALETTE_MAX_SIZE];
Sarah Parkerefd8af22017-08-25 16:36:06 -070028 const int n = param->n_colors;
29 uint8_t *const color_map = param->color_map;
30 MapCdf color_map_cdf = param->map_cdf;
31 int plane_block_width = param->plane_width;
32 int plane_block_height = param->plane_height;
33 int rows = param->rows;
34 int cols = param->cols;
Fangwen Fu33bcd112017-02-07 16:42:41 -080035
Arild Fuldseth3de316f2022-03-18 16:41:45 +000036#if CONFIG_NEW_COLOR_MAP_CODING
37 IdentityRowCdf identity_row_cdf = param->identity_row_cdf;
38 int prev_identity_row_flag = 0;
39 for (int y = 0; y < rows; y++) {
40 const int ctx = y == 0 ? 2 : prev_identity_row_flag;
41 int identity_row_flag =
42 aom_read_symbol(r, identity_row_cdf[ctx], 2, ACCT_STR);
43 for (int x = 0; x < cols; x++) {
44 if (identity_row_flag && x > 0) {
45 color_map[y * plane_block_width + x] =
46 color_map[y * plane_block_width + x - 1];
47 } else if (y == 0 && x == 0) {
48 color_map[0] = av1_read_uniform(r, n);
49 } else {
50 const int color_ctx = av1_get_palette_color_index_context(
51 color_map, plane_block_width, y, x, n, color_order, NULL,
52 identity_row_flag, prev_identity_row_flag);
53 const int color_idx = aom_read_symbol(
54 r, color_map_cdf[n - PALETTE_MIN_SIZE][color_ctx], n, ACCT_STR);
55 assert(color_idx >= 0 && color_idx < n);
56 color_map[y * plane_block_width + x] = color_order[color_idx];
57 }
58 }
59 prev_identity_row_flag = identity_row_flag;
60 }
61#else
hui su40b9e7f2017-07-13 18:15:56 -070062 // The first color index.
63 color_map[0] = av1_read_uniform(r, n);
64 assert(color_map[0] < n);
65
Fangwen Fub3be9262017-03-06 15:34:28 -080066 // Run wavefront on the palette map index decoding.
hui su40b9e7f2017-07-13 18:15:56 -070067 for (int i = 1; i < rows + cols - 1; ++i) {
68 for (int j = AOMMIN(i, cols - 1); j >= AOMMAX(0, i - rows + 1); --j) {
Fangwen Fu33bcd112017-02-07 16:42:41 -080069 const int color_ctx = av1_get_palette_color_index_context(
Fangwen Fub3be9262017-03-06 15:34:28 -080070 color_map, plane_block_width, (i - j), j, n, color_order, NULL);
Thomas Daviesce7272d2017-07-04 16:11:08 +010071 const int color_idx = aom_read_symbol(
Sarah Parkerefd8af22017-08-25 16:36:06 -070072 r, color_map_cdf[n - PALETTE_MIN_SIZE][color_ctx], n, ACCT_STR);
Fangwen Fu33bcd112017-02-07 16:42:41 -080073 assert(color_idx >= 0 && color_idx < n);
Fangwen Fub3be9262017-03-06 15:34:28 -080074 color_map[(i - j) * plane_block_width + j] = color_order[color_idx];
Fangwen Fu33bcd112017-02-07 16:42:41 -080075 }
76 }
Arild Fuldseth3de316f2022-03-18 16:41:45 +000077#endif
Fangwen Fu33bcd112017-02-07 16:42:41 -080078 // Copy last column to extra columns.
Fangwen Fub3be9262017-03-06 15:34:28 -080079 if (cols < plane_block_width) {
Jonathan Matthewsbe984eb2017-08-18 14:42:11 +010080 for (int i = 0; i < rows; ++i) {
Fangwen Fub3be9262017-03-06 15:34:28 -080081 memset(color_map + i * plane_block_width + cols,
82 color_map[i * plane_block_width + cols - 1],
83 (plane_block_width - cols));
Fangwen Fu33bcd112017-02-07 16:42:41 -080084 }
85 }
Urvang Joshi56ba91b2017-01-10 13:22:09 -080086 // Copy last row to extra rows.
hui su40b9e7f2017-07-13 18:15:56 -070087 for (int i = rows; i < plane_block_height; ++i) {
Urvang Joshi56ba91b2017-01-10 13:22:09 -080088 memcpy(color_map + i * plane_block_width,
89 color_map + (rows - 1) * plane_block_width, plane_block_width);
Yaowu Xuc27fc142016-08-22 16:08:15 -070090 }
91}
92
Sarah Parkerefd8af22017-08-25 16:36:06 -070093void av1_decode_palette_tokens(MACROBLOCKD *const xd, int plane,
94 aom_reader *r) {
Sarah Parkerefd8af22017-08-25 16:36:06 -070095 assert(plane == 0 || plane == 1);
Hui Su059d6892018-04-16 16:53:55 -070096 Av1ColorMapParam params;
Deepa K G99f7ddb2018-06-20 11:21:21 +053097 params.color_map =
98 xd->plane[plane].color_index_map + xd->color_index_map_offset[plane];
Hui Su059d6892018-04-16 16:53:55 -070099 params.map_cdf = plane ? xd->tile_ctx->palette_uv_color_index_cdf
100 : xd->tile_ctx->palette_y_color_index_cdf;
Arild Fuldseth3de316f2022-03-18 16:41:45 +0000101#if CONFIG_NEW_COLOR_MAP_CODING
102 params.identity_row_cdf = plane ? xd->tile_ctx->identity_row_cdf_uv
103 : xd->tile_ctx->identity_row_cdf_y;
104#endif // CONFIG_NEW_COLOR_MAP_CODING
Hui Su059d6892018-04-16 16:53:55 -0700105 const MB_MODE_INFO *const mbmi = xd->mi[0];
106 params.n_colors = mbmi->palette_mode_info.palette_size[plane];
liang zhaoc6f775a2020-12-17 11:54:58 -0800107 av1_get_block_dimensions(mbmi->sb_type[plane > 0], plane, xd,
108 &params.plane_width, &params.plane_height,
109 &params.rows, &params.cols);
Hui Su059d6892018-04-16 16:53:55 -0700110 decode_color_map_tokens(&params, r);
Sarah Parkerefd8af22017-08-25 16:36:06 -0700111}