Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Krishna Rapaka | 7319db5 | 2021-09-28 20:35:29 -0700 | [diff] [blame] | 2 | * Copyright (c) 2021, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Vibhoothi | 41c6dd7 | 2021-10-12 18:48:26 +0000 | [diff] [blame] | 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/. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
Tom Finegan | 60e653d | 2018-05-22 11:34:58 -0700 | [diff] [blame] | 13 | #include "config/aom_config.h" |
| 14 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 15 | #include "aom_mem/aom_mem.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 16 | #include "aom_ports/mem.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 17 | #include "av1/common/blockd.h" |
hui su | 40b9e7f | 2017-07-13 18:15:56 -0700 | [diff] [blame] | 18 | #include "av1/decoder/detokenize.h" |
Yushin Cho | e580092 | 2017-02-22 10:15:22 -0800 | [diff] [blame] | 19 | |
Thomas Daede | e1227d5 | 2017-03-07 14:44:27 -0800 | [diff] [blame] | 20 | #define ACCT_STR __func__ |
| 21 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 22 | #include "av1/common/common.h" |
| 23 | #include "av1/common/entropy.h" |
| 24 | #include "av1/common/idct.h" |
Angie Chiang | 3a76969 | 2017-10-01 18:26:48 -0700 | [diff] [blame] | 25 | |
Sarah Parker | efd8af2 | 2017-08-25 16:36:06 -0700 | [diff] [blame] | 26 | static void decode_color_map_tokens(Av1ColorMapParam *param, aom_reader *r) { |
Fangwen Fu | 33bcd11 | 2017-02-07 16:42:41 -0800 | [diff] [blame] | 27 | uint8_t color_order[PALETTE_MAX_SIZE]; |
Sarah Parker | efd8af2 | 2017-08-25 16:36:06 -0700 | [diff] [blame] | 28 | 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 Fu | 33bcd11 | 2017-02-07 16:42:41 -0800 | [diff] [blame] | 35 | |
Arild Fuldseth | 3de316f | 2022-03-18 16:41:45 +0000 | [diff] [blame] | 36 | #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 su | 40b9e7f | 2017-07-13 18:15:56 -0700 | [diff] [blame] | 62 | // The first color index. |
| 63 | color_map[0] = av1_read_uniform(r, n); |
| 64 | assert(color_map[0] < n); |
| 65 | |
Fangwen Fu | b3be926 | 2017-03-06 15:34:28 -0800 | [diff] [blame] | 66 | // Run wavefront on the palette map index decoding. |
hui su | 40b9e7f | 2017-07-13 18:15:56 -0700 | [diff] [blame] | 67 | 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 Fu | 33bcd11 | 2017-02-07 16:42:41 -0800 | [diff] [blame] | 69 | const int color_ctx = av1_get_palette_color_index_context( |
Fangwen Fu | b3be926 | 2017-03-06 15:34:28 -0800 | [diff] [blame] | 70 | color_map, plane_block_width, (i - j), j, n, color_order, NULL); |
Thomas Davies | ce7272d | 2017-07-04 16:11:08 +0100 | [diff] [blame] | 71 | const int color_idx = aom_read_symbol( |
Sarah Parker | efd8af2 | 2017-08-25 16:36:06 -0700 | [diff] [blame] | 72 | r, color_map_cdf[n - PALETTE_MIN_SIZE][color_ctx], n, ACCT_STR); |
Fangwen Fu | 33bcd11 | 2017-02-07 16:42:41 -0800 | [diff] [blame] | 73 | assert(color_idx >= 0 && color_idx < n); |
Fangwen Fu | b3be926 | 2017-03-06 15:34:28 -0800 | [diff] [blame] | 74 | color_map[(i - j) * plane_block_width + j] = color_order[color_idx]; |
Fangwen Fu | 33bcd11 | 2017-02-07 16:42:41 -0800 | [diff] [blame] | 75 | } |
| 76 | } |
Arild Fuldseth | 3de316f | 2022-03-18 16:41:45 +0000 | [diff] [blame] | 77 | #endif |
Fangwen Fu | 33bcd11 | 2017-02-07 16:42:41 -0800 | [diff] [blame] | 78 | // Copy last column to extra columns. |
Fangwen Fu | b3be926 | 2017-03-06 15:34:28 -0800 | [diff] [blame] | 79 | if (cols < plane_block_width) { |
Jonathan Matthews | be984eb | 2017-08-18 14:42:11 +0100 | [diff] [blame] | 80 | for (int i = 0; i < rows; ++i) { |
Fangwen Fu | b3be926 | 2017-03-06 15:34:28 -0800 | [diff] [blame] | 81 | memset(color_map + i * plane_block_width + cols, |
| 82 | color_map[i * plane_block_width + cols - 1], |
| 83 | (plane_block_width - cols)); |
Fangwen Fu | 33bcd11 | 2017-02-07 16:42:41 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 86 | // Copy last row to extra rows. |
hui su | 40b9e7f | 2017-07-13 18:15:56 -0700 | [diff] [blame] | 87 | for (int i = rows; i < plane_block_height; ++i) { |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 88 | memcpy(color_map + i * plane_block_width, |
| 89 | color_map + (rows - 1) * plane_block_width, plane_block_width); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Sarah Parker | efd8af2 | 2017-08-25 16:36:06 -0700 | [diff] [blame] | 93 | void av1_decode_palette_tokens(MACROBLOCKD *const xd, int plane, |
| 94 | aom_reader *r) { |
Sarah Parker | efd8af2 | 2017-08-25 16:36:06 -0700 | [diff] [blame] | 95 | assert(plane == 0 || plane == 1); |
Hui Su | 059d689 | 2018-04-16 16:53:55 -0700 | [diff] [blame] | 96 | Av1ColorMapParam params; |
Deepa K G | 99f7ddb | 2018-06-20 11:21:21 +0530 | [diff] [blame] | 97 | params.color_map = |
| 98 | xd->plane[plane].color_index_map + xd->color_index_map_offset[plane]; |
Hui Su | 059d689 | 2018-04-16 16:53:55 -0700 | [diff] [blame] | 99 | params.map_cdf = plane ? xd->tile_ctx->palette_uv_color_index_cdf |
| 100 | : xd->tile_ctx->palette_y_color_index_cdf; |
Arild Fuldseth | 3de316f | 2022-03-18 16:41:45 +0000 | [diff] [blame] | 101 | #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 Su | 059d689 | 2018-04-16 16:53:55 -0700 | [diff] [blame] | 105 | const MB_MODE_INFO *const mbmi = xd->mi[0]; |
| 106 | params.n_colors = mbmi->palette_mode_info.palette_size[plane]; |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 107 | av1_get_block_dimensions(mbmi->sb_type[plane > 0], plane, xd, |
| 108 | ¶ms.plane_width, ¶ms.plane_height, |
| 109 | ¶ms.rows, ¶ms.cols); |
Hui Su | 059d689 | 2018-04-16 16:53:55 -0700 | [diff] [blame] | 110 | decode_color_map_tokens(¶ms, r); |
Sarah Parker | efd8af2 | 2017-08-25 16:36:06 -0700 | [diff] [blame] | 111 | } |