Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [diff] [blame] | 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. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <assert.h> |
| 13 | #include <limits.h> |
| 14 | #include <stdio.h> |
| 15 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 16 | #include "aom/aom_encoder.h" |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 17 | #include "aom_dsp/aom_dsp_common.h" |
Debargha Mukherjee | 47748b5 | 2017-03-24 12:20:49 -0700 | [diff] [blame] | 18 | #include "aom_dsp/binary_codes_writer.h" |
Cheng Chen | c7855b1 | 2017-09-05 10:49:08 -0700 | [diff] [blame] | 19 | #include "aom_dsp/bitwriter_buffer.h" |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 20 | #include "aom_mem/aom_mem.h" |
Wan-Teh Chang | ccdd87f | 2018-08-08 18:40:29 -0700 | [diff] [blame] | 21 | #include "aom_ports/bitops.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 22 | #include "aom_ports/mem_ops.h" |
Angie Chiang | 6062a8b | 2016-09-21 16:01:04 -0700 | [diff] [blame] | 23 | #if CONFIG_BITSTREAM_DEBUG |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 24 | #include "aom_util/debug_util.h" |
Angie Chiang | 6062a8b | 2016-09-21 16:01:04 -0700 | [diff] [blame] | 25 | #endif // CONFIG_BITSTREAM_DEBUG |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 26 | |
Steinar Midtskogen | a9d41e8 | 2017-03-17 12:48:15 +0100 | [diff] [blame] | 27 | #include "av1/common/cdef.h" |
Luc Trudeau | d183b64 | 2017-11-28 11:42:37 -0500 | [diff] [blame] | 28 | #include "av1/common/cfl.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 29 | #include "av1/common/entropy.h" |
| 30 | #include "av1/common/entropymode.h" |
| 31 | #include "av1/common/entropymv.h" |
| 32 | #include "av1/common/mvref_common.h" |
| 33 | #include "av1/common/pred_common.h" |
| 34 | #include "av1/common/reconinter.h" |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 35 | #include "av1/common/reconintra.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 36 | #include "av1/common/seg_common.h" |
| 37 | #include "av1/common/tile_common.h" |
| 38 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 39 | #include "av1/encoder/bitstream.h" |
| 40 | #include "av1/encoder/cost.h" |
| 41 | #include "av1/encoder/encodemv.h" |
Hui Su | ec73b44 | 2018-01-04 12:47:53 -0800 | [diff] [blame] | 42 | #include "av1/encoder/encodetxb.h" |
Cherma Rajan A | ce0c423 | 2021-04-23 21:29:51 +0530 | [diff] [blame] | 43 | #include "av1/encoder/ethread.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 44 | #include "av1/encoder/mcomp.h" |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 45 | #include "av1/encoder/palette.h" |
Rachel Barker | fe93d33 | 2023-07-10 10:01:15 +0000 | [diff] [blame] | 46 | #include "av1/encoder/pickrst.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 47 | #include "av1/encoder/segmentation.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 48 | #include "av1/encoder/tokenize.h" |
| 49 | |
Di Chen | 5658662 | 2017-06-09 13:49:44 -0700 | [diff] [blame] | 50 | #define ENC_MISMATCH_DEBUG 0 |
Cherma Rajan A | 248e8d3 | 2021-06-15 09:06:38 +0530 | [diff] [blame] | 51 | #define SETUP_TIME_OH_CONST 5 // Setup time overhead constant per worker |
| 52 | #define JOB_DISP_TIME_OH_CONST 1 // Job dispatch time overhead per tile |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 53 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 54 | static INLINE void write_uniform(aom_writer *w, int n, int v) { |
hui su | 3749929 | 2017-04-26 09:49:53 -0700 | [diff] [blame] | 55 | const int l = get_unsigned_bits(n); |
| 56 | const int m = (1 << l) - n; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 57 | if (l == 0) return; |
| 58 | if (v < m) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 59 | aom_write_literal(w, v, l - 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 60 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 61 | aom_write_literal(w, m + ((v - m) >> 1), l - 1); |
| 62 | aom_write_literal(w, (v - m) & 1, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Jerome Jiang | 3dd9df9 | 2020-10-29 16:42:33 -0700 | [diff] [blame] | 66 | #if !CONFIG_REALTIME_ONLY |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 67 | static AOM_INLINE void loop_restoration_write_sb_coeffs( |
Rachel Barker | fe93d33 | 2023-07-10 10:01:15 +0000 | [diff] [blame] | 68 | const AV1_COMMON *const cm, MACROBLOCKD *xd, int runit_idx, |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 69 | aom_writer *const w, int plane, FRAME_COUNTS *counts); |
Jerome Jiang | 3dd9df9 | 2020-10-29 16:42:33 -0700 | [diff] [blame] | 70 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 71 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 72 | static AOM_INLINE void write_intra_y_mode_kf(FRAME_CONTEXT *frame_ctx, |
| 73 | const MB_MODE_INFO *mi, |
| 74 | const MB_MODE_INFO *above_mi, |
| 75 | const MB_MODE_INFO *left_mi, |
| 76 | PREDICTION_MODE mode, |
| 77 | aom_writer *w) { |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 78 | assert(!is_intrabc_block(mi)); |
Jingning Han | 9010e20 | 2017-12-14 14:48:09 -0800 | [diff] [blame] | 79 | (void)mi; |
Jingning Han | 9010e20 | 2017-12-14 14:48:09 -0800 | [diff] [blame] | 80 | aom_write_symbol(w, mode, get_y_mode_cdf(frame_ctx, above_mi, left_mi), |
Jingning Han | f04254f | 2017-03-08 10:51:35 -0800 | [diff] [blame] | 81 | INTRA_MODES); |
Jingning Han | f04254f | 2017-03-08 10:51:35 -0800 | [diff] [blame] | 82 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 83 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 84 | static AOM_INLINE void write_inter_mode(aom_writer *w, PREDICTION_MODE mode, |
| 85 | FRAME_CONTEXT *ec_ctx, |
| 86 | const int16_t mode_ctx) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 87 | const int16_t newmv_ctx = mode_ctx & NEWMV_CTX_MASK; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 88 | |
Thomas Davies | 149eda5 | 2017-06-12 18:11:55 +0100 | [diff] [blame] | 89 | aom_write_symbol(w, mode != NEWMV, ec_ctx->newmv_cdf[newmv_ctx], 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 90 | |
Jingning Han | f2b87bd | 2017-05-18 16:27:30 -0700 | [diff] [blame] | 91 | if (mode != NEWMV) { |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 92 | const int16_t zeromv_ctx = |
| 93 | (mode_ctx >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK; |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 94 | aom_write_symbol(w, mode != GLOBALMV, ec_ctx->zeromv_cdf[zeromv_ctx], 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 95 | |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 96 | if (mode != GLOBALMV) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 97 | int16_t refmv_ctx = (mode_ctx >> REFMV_OFFSET) & REFMV_CTX_MASK; |
Thomas Davies | 149eda5 | 2017-06-12 18:11:55 +0100 | [diff] [blame] | 98 | aom_write_symbol(w, mode != NEARESTMV, ec_ctx->refmv_cdf[refmv_ctx], 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 103 | static AOM_INLINE void write_drl_idx( |
| 104 | FRAME_CONTEXT *ec_ctx, const MB_MODE_INFO *mbmi, |
| 105 | const MB_MODE_INFO_EXT_FRAME *mbmi_ext_frame, aom_writer *w) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 106 | assert(mbmi->ref_mv_idx < 3); |
| 107 | |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 108 | const int new_mv = mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV; |
Rupert Swarbrick | cf77276 | 2017-11-03 16:41:07 +0000 | [diff] [blame] | 109 | if (new_mv) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 110 | int idx; |
| 111 | for (idx = 0; idx < 2; ++idx) { |
Remya | 0cce44c | 2019-08-16 11:57:24 +0530 | [diff] [blame] | 112 | if (mbmi_ext_frame->ref_mv_count > idx + 1) { |
| 113 | uint8_t drl_ctx = av1_drl_ctx(mbmi_ext_frame->weight, idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 114 | |
Thomas Davies | 149eda5 | 2017-06-12 18:11:55 +0100 | [diff] [blame] | 115 | aom_write_symbol(w, mbmi->ref_mv_idx != idx, ec_ctx->drl_cdf[drl_ctx], |
| 116 | 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 117 | if (mbmi->ref_mv_idx == idx) return; |
| 118 | } |
| 119 | } |
| 120 | return; |
| 121 | } |
| 122 | |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 123 | if (have_nearmv_in_inter_mode(mbmi->mode)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 124 | int idx; |
| 125 | // TODO(jingning): Temporary solution to compensate the NEARESTMV offset. |
| 126 | for (idx = 1; idx < 3; ++idx) { |
Remya | 0cce44c | 2019-08-16 11:57:24 +0530 | [diff] [blame] | 127 | if (mbmi_ext_frame->ref_mv_count > idx + 1) { |
| 128 | uint8_t drl_ctx = av1_drl_ctx(mbmi_ext_frame->weight, idx); |
Thomas Davies | 149eda5 | 2017-06-12 18:11:55 +0100 | [diff] [blame] | 129 | aom_write_symbol(w, mbmi->ref_mv_idx != (idx - 1), |
| 130 | ec_ctx->drl_cdf[drl_ctx], 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 131 | if (mbmi->ref_mv_idx == (idx - 1)) return; |
| 132 | } |
| 133 | } |
| 134 | return; |
| 135 | } |
| 136 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 137 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 138 | static AOM_INLINE void write_inter_compound_mode(MACROBLOCKD *xd, aom_writer *w, |
| 139 | PREDICTION_MODE mode, |
| 140 | const int16_t mode_ctx) { |
Thomas Davies | 8c08a33 | 2017-06-26 17:30:34 +0100 | [diff] [blame] | 141 | assert(is_inter_compound_mode(mode)); |
Thomas Davies | 8c08a33 | 2017-06-26 17:30:34 +0100 | [diff] [blame] | 142 | aom_write_symbol(w, INTER_COMPOUND_OFFSET(mode), |
| 143 | xd->tile_ctx->inter_compound_mode_cdf[mode_ctx], |
| 144 | INTER_COMPOUND_MODES); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 145 | } |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 146 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 147 | static AOM_INLINE void write_tx_size_vartx(MACROBLOCKD *xd, |
| 148 | const MB_MODE_INFO *mbmi, |
| 149 | TX_SIZE tx_size, int depth, |
| 150 | int blk_row, int blk_col, |
| 151 | aom_writer *w) { |
David Turner | 760a2f4 | 2018-12-07 15:25:36 +0000 | [diff] [blame] | 152 | FRAME_CONTEXT *const ec_ctx = xd->tile_ctx; |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 153 | const int max_blocks_high = max_block_high(xd, mbmi->bsize, 0); |
| 154 | const int max_blocks_wide = max_block_wide(xd, mbmi->bsize, 0); |
Jingning Han | f65b870 | 2016-10-31 12:13:20 -0700 | [diff] [blame] | 155 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 156 | if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return; |
| 157 | |
Jingning Han | 571189c | 2016-10-24 10:38:43 -0700 | [diff] [blame] | 158 | if (depth == MAX_VARTX_DEPTH) { |
Jingning Han | 331662e | 2017-05-30 17:03:32 -0700 | [diff] [blame] | 159 | txfm_partition_update(xd->above_txfm_context + blk_col, |
| 160 | xd->left_txfm_context + blk_row, tx_size, tx_size); |
Jingning Han | 94d5bfc | 2016-10-21 10:14:36 -0700 | [diff] [blame] | 161 | return; |
| 162 | } |
| 163 | |
Hui Su | 7167d95 | 2018-02-01 16:33:12 -0800 | [diff] [blame] | 164 | const int ctx = txfm_partition_context(xd->above_txfm_context + blk_col, |
| 165 | xd->left_txfm_context + blk_row, |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 166 | mbmi->bsize, tx_size); |
Hui Su | 7167d95 | 2018-02-01 16:33:12 -0800 | [diff] [blame] | 167 | const int txb_size_index = |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 168 | av1_get_txb_size_index(mbmi->bsize, blk_row, blk_col); |
Rupert Swarbrick | cf77276 | 2017-11-03 16:41:07 +0000 | [diff] [blame] | 169 | const int write_txfm_partition = |
Hui Su | 7167d95 | 2018-02-01 16:33:12 -0800 | [diff] [blame] | 170 | tx_size == mbmi->inter_tx_size[txb_size_index]; |
Rupert Swarbrick | cf77276 | 2017-11-03 16:41:07 +0000 | [diff] [blame] | 171 | if (write_txfm_partition) { |
Thomas Davies | 985bfc3 | 2017-06-27 16:51:26 +0100 | [diff] [blame] | 172 | aom_write_symbol(w, 0, ec_ctx->txfm_partition_cdf[ctx], 2); |
Thomas Davies | 985bfc3 | 2017-06-27 16:51:26 +0100 | [diff] [blame] | 173 | |
Jingning Han | 331662e | 2017-05-30 17:03:32 -0700 | [diff] [blame] | 174 | txfm_partition_update(xd->above_txfm_context + blk_col, |
| 175 | xd->left_txfm_context + blk_row, tx_size, tx_size); |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 176 | // TODO(yuec): set correct txfm partition update for qttx |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 177 | } else { |
Frederic Barbier | 4b56b10 | 2018-03-30 16:09:34 +0200 | [diff] [blame] | 178 | const TX_SIZE sub_txs = sub_tx_size_map[tx_size]; |
Yue Chen | 0797a20 | 2017-10-27 17:24:56 -0700 | [diff] [blame] | 179 | const int bsw = tx_size_wide_unit[sub_txs]; |
| 180 | const int bsh = tx_size_high_unit[sub_txs]; |
Jingning Han | f64062f | 2016-11-02 16:22:18 -0700 | [diff] [blame] | 181 | |
Thomas Davies | 985bfc3 | 2017-06-27 16:51:26 +0100 | [diff] [blame] | 182 | aom_write_symbol(w, 1, ec_ctx->txfm_partition_cdf[ctx], 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 183 | |
David Barker | 16c64e3 | 2017-08-23 16:54:59 +0100 | [diff] [blame] | 184 | if (sub_txs == TX_4X4) { |
Jingning Han | 331662e | 2017-05-30 17:03:32 -0700 | [diff] [blame] | 185 | txfm_partition_update(xd->above_txfm_context + blk_col, |
| 186 | xd->left_txfm_context + blk_row, sub_txs, tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 187 | return; |
| 188 | } |
| 189 | |
Yue Chen | 0797a20 | 2017-10-27 17:24:56 -0700 | [diff] [blame] | 190 | assert(bsw > 0 && bsh > 0); |
Wan-Teh Chang | 0cb03d9 | 2021-03-05 08:27:21 -0800 | [diff] [blame] | 191 | for (int row = 0; row < tx_size_high_unit[tx_size]; row += bsh) { |
| 192 | const int offsetr = blk_row + row; |
Yue Chen | 0797a20 | 2017-10-27 17:24:56 -0700 | [diff] [blame] | 193 | for (int col = 0; col < tx_size_wide_unit[tx_size]; col += bsw) { |
Wan-Teh Chang | 0cb03d9 | 2021-03-05 08:27:21 -0800 | [diff] [blame] | 194 | const int offsetc = blk_col + col; |
Yaowu Xu | 6567c4a | 2018-03-19 21:39:31 -0700 | [diff] [blame] | 195 | write_tx_size_vartx(xd, mbmi, sub_txs, depth + 1, offsetr, offsetc, w); |
Yue Chen | 0797a20 | 2017-10-27 17:24:56 -0700 | [diff] [blame] | 196 | } |
Wan-Teh Chang | 0cb03d9 | 2021-03-05 08:27:21 -0800 | [diff] [blame] | 197 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 201 | static AOM_INLINE void write_selected_tx_size(const MACROBLOCKD *xd, |
| 202 | aom_writer *w) { |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 203 | const MB_MODE_INFO *const mbmi = xd->mi[0]; |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 204 | const BLOCK_SIZE bsize = mbmi->bsize; |
Thomas Davies | 15580c5 | 2017-03-09 13:53:42 +0000 | [diff] [blame] | 205 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
Rupert Swarbrick | fcff0b2 | 2017-10-05 09:26:04 +0100 | [diff] [blame] | 206 | if (block_signals_txsize(bsize)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 207 | const TX_SIZE tx_size = mbmi->tx_size; |
Yaowu Xu | 25ff26a | 2018-02-26 11:20:10 -0800 | [diff] [blame] | 208 | const int tx_size_ctx = get_tx_size_context(xd); |
Frederic Barbier | 4b56b10 | 2018-03-30 16:09:34 +0200 | [diff] [blame] | 209 | const int depth = tx_size_to_depth(tx_size, bsize); |
| 210 | const int max_depths = bsize_to_max_depth(bsize); |
| 211 | const int32_t tx_size_cat = bsize_to_tx_size_cat(bsize); |
Debargha Mukherjee | 6147b1b | 2017-11-08 08:31:09 -0800 | [diff] [blame] | 212 | |
Debargha Mukherjee | 6147b1b | 2017-11-08 08:31:09 -0800 | [diff] [blame] | 213 | assert(depth >= 0 && depth <= max_depths); |
Debargha Mukherjee | 6147b1b | 2017-11-08 08:31:09 -0800 | [diff] [blame] | 214 | assert(!is_inter_block(mbmi)); |
Yue Chen | 49587a7 | 2016-09-28 17:09:47 -0700 | [diff] [blame] | 215 | assert(IMPLIES(is_rect_tx(tx_size), is_rect_tx_allowed(xd, mbmi))); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 216 | |
Thomas Davies | 15580c5 | 2017-03-09 13:53:42 +0000 | [diff] [blame] | 217 | aom_write_symbol(w, depth, ec_ctx->tx_size_cdf[tx_size_cat][tx_size_ctx], |
Debargha Mukherjee | 6147b1b | 2017-11-08 08:31:09 -0800 | [diff] [blame] | 218 | max_depths + 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 222 | static int write_skip(const AV1_COMMON *cm, const MACROBLOCKD *xd, |
chiyotsai | d730cef | 2022-10-26 13:58:20 -0700 | [diff] [blame] | 223 | uint8_t segment_id, const MB_MODE_INFO *mi, |
| 224 | aom_writer *w) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 225 | if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) { |
| 226 | return 1; |
| 227 | } else { |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 228 | const int skip_txfm = mi->skip_txfm; |
| 229 | const int ctx = av1_get_skip_txfm_context(xd); |
Thomas Davies | 61e3e37 | 2017-04-04 16:10:23 +0100 | [diff] [blame] | 230 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 231 | aom_write_symbol(w, skip_txfm, ec_ctx->skip_txfm_cdfs[ctx], 2); |
| 232 | return skip_txfm; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 236 | static int write_skip_mode(const AV1_COMMON *cm, const MACROBLOCKD *xd, |
chiyotsai | d730cef | 2022-10-26 13:58:20 -0700 | [diff] [blame] | 237 | uint8_t segment_id, const MB_MODE_INFO *mi, |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 238 | aom_writer *w) { |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 239 | if (!cm->current_frame.skip_mode_info.skip_mode_flag) return 0; |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 240 | if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) { |
| 241 | return 0; |
| 242 | } |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 243 | const int skip_mode = mi->skip_mode; |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 244 | if (!is_comp_ref_allowed(mi->bsize)) { |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 245 | assert(!skip_mode); |
| 246 | return 0; |
| 247 | } |
Urvang Joshi | 6cd95d2 | 2018-05-15 14:14:58 -0400 | [diff] [blame] | 248 | if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME) || |
| 249 | segfeature_active(&cm->seg, segment_id, SEG_LVL_GLOBALMV)) { |
| 250 | // These features imply single-reference mode, while skip mode implies |
| 251 | // compound reference. Hence, the two are mutually exclusive. |
| 252 | // In other words, skip_mode is implicitly 0 here. |
Urvang Joshi | e5ae283 | 2018-05-11 13:47:00 -0700 | [diff] [blame] | 253 | assert(!skip_mode); |
| 254 | return 0; |
| 255 | } |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 256 | const int ctx = av1_get_skip_mode_context(xd); |
| 257 | aom_write_symbol(w, skip_mode, xd->tile_ctx->skip_mode_cdfs[ctx], 2); |
| 258 | return skip_mode; |
| 259 | } |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 260 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 261 | static AOM_INLINE void write_is_inter(const AV1_COMMON *cm, |
chiyotsai | d730cef | 2022-10-26 13:58:20 -0700 | [diff] [blame] | 262 | const MACROBLOCKD *xd, uint8_t segment_id, |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 263 | aom_writer *w, const int is_inter) { |
Thomas Davies | f6ad935 | 2017-04-19 11:38:06 +0100 | [diff] [blame] | 264 | if (!segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) { |
Debargha Mukherjee | 27e55a6 | 2018-04-17 07:43:04 -0700 | [diff] [blame] | 265 | if (segfeature_active(&cm->seg, segment_id, SEG_LVL_GLOBALMV)) { |
| 266 | assert(is_inter); |
| 267 | return; |
| 268 | } |
Yue Chen | 170678a | 2017-10-17 13:43:10 -0700 | [diff] [blame] | 269 | const int ctx = av1_get_intra_inter_context(xd); |
Thomas Davies | f6ad935 | 2017-04-19 11:38:06 +0100 | [diff] [blame] | 270 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
Thomas Davies | f6ad935 | 2017-04-19 11:38:06 +0100 | [diff] [blame] | 271 | aom_write_symbol(w, is_inter, ec_ctx->intra_inter_cdf[ctx], 2); |
Thomas Davies | f6ad935 | 2017-04-19 11:38:06 +0100 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 275 | static AOM_INLINE void write_motion_mode(const AV1_COMMON *cm, MACROBLOCKD *xd, |
| 276 | const MB_MODE_INFO *mbmi, |
| 277 | aom_writer *w) { |
Sebastien Alaiwan | 4879580 | 2017-10-30 12:07:13 +0100 | [diff] [blame] | 278 | MOTION_MODE last_motion_mode_allowed = |
Urvang Joshi | 6237b88 | 2020-03-26 15:02:26 -0700 | [diff] [blame] | 279 | cm->features.switchable_motion_mode |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 280 | ? motion_mode_allowed(cm->global_motion, xd, mbmi, |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 281 | cm->features.allow_warped_motion) |
Yue Chen | 5380cb5 | 2018-02-23 15:33:21 -0800 | [diff] [blame] | 282 | : SIMPLE_TRANSLATION; |
| 283 | assert(mbmi->motion_mode <= last_motion_mode_allowed); |
Rupert Swarbrick | cf77276 | 2017-11-03 16:41:07 +0000 | [diff] [blame] | 284 | switch (last_motion_mode_allowed) { |
| 285 | case SIMPLE_TRANSLATION: break; |
Rupert Swarbrick | cf77276 | 2017-11-03 16:41:07 +0000 | [diff] [blame] | 286 | case OBMC_CAUSAL: |
Rupert Swarbrick | cf77276 | 2017-11-03 16:41:07 +0000 | [diff] [blame] | 287 | aom_write_symbol(w, mbmi->motion_mode == OBMC_CAUSAL, |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 288 | xd->tile_ctx->obmc_cdf[mbmi->bsize], 2); |
Rupert Swarbrick | cf77276 | 2017-11-03 16:41:07 +0000 | [diff] [blame] | 289 | break; |
Rupert Swarbrick | cf77276 | 2017-11-03 16:41:07 +0000 | [diff] [blame] | 290 | default: |
| 291 | aom_write_symbol(w, mbmi->motion_mode, |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 292 | xd->tile_ctx->motion_mode_cdf[mbmi->bsize], |
Rupert Swarbrick | cf77276 | 2017-11-03 16:41:07 +0000 | [diff] [blame] | 293 | MOTION_MODES); |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 294 | } |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 295 | } |
Wei-Ting Lin | 85a8f70 | 2017-06-22 13:55:15 -0700 | [diff] [blame] | 296 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 297 | static AOM_INLINE void write_delta_qindex(const MACROBLOCKD *xd, |
| 298 | int delta_qindex, aom_writer *w) { |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 299 | int sign = delta_qindex < 0; |
| 300 | int abs = sign ? -delta_qindex : delta_qindex; |
Thomas Davies | d6ee8a8 | 2017-03-02 14:42:50 +0000 | [diff] [blame] | 301 | int rem_bits, thr; |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 302 | int smallval = abs < DELTA_Q_SMALL ? 1 : 0; |
Thomas Davies | d6ee8a8 | 2017-03-02 14:42:50 +0000 | [diff] [blame] | 303 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 304 | |
Thomas Davies | d6ee8a8 | 2017-03-02 14:42:50 +0000 | [diff] [blame] | 305 | aom_write_symbol(w, AOMMIN(abs, DELTA_Q_SMALL), ec_ctx->delta_q_cdf, |
| 306 | DELTA_Q_PROBS + 1); |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 307 | |
| 308 | if (!smallval) { |
Wan-Teh Chang | ccdd87f | 2018-08-08 18:40:29 -0700 | [diff] [blame] | 309 | rem_bits = get_msb(abs - 1); |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 310 | thr = (1 << rem_bits) + 1; |
Thomas Davies | 3b93e8e | 2017-09-20 09:59:07 +0100 | [diff] [blame] | 311 | aom_write_literal(w, rem_bits - 1, 3); |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 312 | aom_write_literal(w, abs - thr, rem_bits); |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 313 | } |
| 314 | if (abs > 0) { |
| 315 | aom_write_bit(w, sign); |
| 316 | } |
| 317 | } |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 318 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 319 | static AOM_INLINE void write_delta_lflevel(const AV1_COMMON *cm, |
| 320 | const MACROBLOCKD *xd, int lf_id, |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 321 | int delta_lflevel, |
| 322 | int delta_lf_multi, aom_writer *w) { |
Fangwen Fu | 231fe42 | 2017-04-24 17:52:29 -0700 | [diff] [blame] | 323 | int sign = delta_lflevel < 0; |
| 324 | int abs = sign ? -delta_lflevel : delta_lflevel; |
| 325 | int rem_bits, thr; |
| 326 | int smallval = abs < DELTA_LF_SMALL ? 1 : 0; |
Fangwen Fu | 231fe42 | 2017-04-24 17:52:29 -0700 | [diff] [blame] | 327 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 328 | (void)cm; |
Fangwen Fu | 231fe42 | 2017-04-24 17:52:29 -0700 | [diff] [blame] | 329 | |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 330 | if (delta_lf_multi) { |
Imdad Sardharwalla | f74b4ab | 2018-02-20 17:22:42 +0000 | [diff] [blame] | 331 | assert(lf_id >= 0 && lf_id < (av1_num_planes(cm) > 1 ? FRAME_LF_COUNT |
| 332 | : FRAME_LF_COUNT - 2)); |
Cheng Chen | 880166a | 2017-10-02 17:48:48 -0700 | [diff] [blame] | 333 | aom_write_symbol(w, AOMMIN(abs, DELTA_LF_SMALL), |
| 334 | ec_ctx->delta_lf_multi_cdf[lf_id], DELTA_LF_PROBS + 1); |
| 335 | } else { |
| 336 | aom_write_symbol(w, AOMMIN(abs, DELTA_LF_SMALL), ec_ctx->delta_lf_cdf, |
| 337 | DELTA_LF_PROBS + 1); |
| 338 | } |
Fangwen Fu | 231fe42 | 2017-04-24 17:52:29 -0700 | [diff] [blame] | 339 | |
| 340 | if (!smallval) { |
Wan-Teh Chang | ccdd87f | 2018-08-08 18:40:29 -0700 | [diff] [blame] | 341 | rem_bits = get_msb(abs - 1); |
Fangwen Fu | 231fe42 | 2017-04-24 17:52:29 -0700 | [diff] [blame] | 342 | thr = (1 << rem_bits) + 1; |
Thomas Davies | 3b93e8e | 2017-09-20 09:59:07 +0100 | [diff] [blame] | 343 | aom_write_literal(w, rem_bits - 1, 3); |
Fangwen Fu | 231fe42 | 2017-04-24 17:52:29 -0700 | [diff] [blame] | 344 | aom_write_literal(w, abs - thr, rem_bits); |
| 345 | } |
| 346 | if (abs > 0) { |
| 347 | aom_write_bit(w, sign); |
| 348 | } |
| 349 | } |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 350 | |
Vishesh | 686aa77 | 2020-04-13 14:40:12 +0530 | [diff] [blame] | 351 | static AOM_INLINE void pack_map_tokens(aom_writer *w, const TokenExtra **tp, |
Jayasanker J | 4969e72 | 2020-09-18 20:36:15 +0530 | [diff] [blame] | 352 | int n, int num, MapCdf map_pb_cdf) { |
Vishesh | 686aa77 | 2020-04-13 14:40:12 +0530 | [diff] [blame] | 353 | const TokenExtra *p = *tp; |
Jayasanker J | 4969e72 | 2020-09-18 20:36:15 +0530 | [diff] [blame] | 354 | const int palette_size_idx = n - PALETTE_MIN_SIZE; |
hui su | 40b9e7f | 2017-07-13 18:15:56 -0700 | [diff] [blame] | 355 | write_uniform(w, n, p->token); // The first color index. |
| 356 | ++p; |
| 357 | --num; |
| 358 | for (int i = 0; i < num; ++i) { |
Aniket Wanare | 8268a65 | 2021-02-22 14:38:08 +0530 | [diff] [blame] | 359 | assert((p->color_ctx >= 0) && |
| 360 | (p->color_ctx < PALETTE_COLOR_INDEX_CONTEXTS)); |
Jayasanker J | 4969e72 | 2020-09-18 20:36:15 +0530 | [diff] [blame] | 361 | aom_cdf_prob *color_map_cdf = map_pb_cdf[palette_size_idx][p->color_ctx]; |
| 362 | aom_write_symbol(w, p->token, color_map_cdf, n); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 363 | ++p; |
| 364 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 365 | *tp = p; |
| 366 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 367 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 368 | static AOM_INLINE void pack_txb_tokens( |
Vishesh | 686aa77 | 2020-04-13 14:40:12 +0530 | [diff] [blame] | 369 | aom_writer *w, AV1_COMMON *cm, MACROBLOCK *const x, const TokenExtra **tp, |
| 370 | const TokenExtra *const tok_end, MACROBLOCKD *xd, MB_MODE_INFO *mbmi, |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 371 | int plane, BLOCK_SIZE plane_bsize, aom_bit_depth_t bit_depth, int block, |
| 372 | int blk_row, int blk_col, TX_SIZE tx_size, TOKEN_STATS *token_stats) { |
Jingning Han | 4fe5f67 | 2017-05-19 15:46:07 -0700 | [diff] [blame] | 373 | const int max_blocks_high = max_block_high(xd, plane_bsize, plane); |
| 374 | const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane); |
| 375 | |
| 376 | if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return; |
| 377 | |
Hui Su | 43d0994 | 2018-04-03 12:40:17 -0700 | [diff] [blame] | 378 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
Debargha Mukherjee | 8aec7f3 | 2017-12-20 15:48:49 -0800 | [diff] [blame] | 379 | const TX_SIZE plane_tx_size = |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 380 | plane ? av1_get_max_uv_txsize(mbmi->bsize, pd->subsampling_x, |
Cheng Chen | 8ab1f44 | 2018-04-27 18:01:52 -0700 | [diff] [blame] | 381 | pd->subsampling_y) |
Hui Su | 7167d95 | 2018-02-01 16:33:12 -0800 | [diff] [blame] | 382 | : mbmi->inter_tx_size[av1_get_txb_size_index(plane_bsize, blk_row, |
| 383 | blk_col)]; |
Jingning Han | 4fe5f67 | 2017-05-19 15:46:07 -0700 | [diff] [blame] | 384 | |
Debargha Mukherjee | 6396d3a | 2017-12-21 00:12:38 -0800 | [diff] [blame] | 385 | if (tx_size == plane_tx_size || plane) { |
Hui Su | 179fbfa | 2019-10-07 15:13:06 -0700 | [diff] [blame] | 386 | av1_write_coeffs_txb(cm, x, w, blk_row, blk_col, plane, block, tx_size); |
Jingning Han | 4fe5f67 | 2017-05-19 15:46:07 -0700 | [diff] [blame] | 387 | #if CONFIG_RD_DEBUG |
Hui Su | 43d0994 | 2018-04-03 12:40:17 -0700 | [diff] [blame] | 388 | TOKEN_STATS tmp_token_stats; |
| 389 | init_token_stats(&tmp_token_stats); |
Jingning Han | 4fe5f67 | 2017-05-19 15:46:07 -0700 | [diff] [blame] | 390 | token_stats->cost += tmp_token_stats.cost; |
| 391 | #endif |
| 392 | } else { |
Frederic Barbier | 4b56b10 | 2018-03-30 16:09:34 +0200 | [diff] [blame] | 393 | const TX_SIZE sub_txs = sub_tx_size_map[tx_size]; |
Yue Chen | 0797a20 | 2017-10-27 17:24:56 -0700 | [diff] [blame] | 394 | const int bsw = tx_size_wide_unit[sub_txs]; |
| 395 | const int bsh = tx_size_high_unit[sub_txs]; |
Hui Su | 43d0994 | 2018-04-03 12:40:17 -0700 | [diff] [blame] | 396 | const int step = bsh * bsw; |
Wan-Teh Chang | 0cb03d9 | 2021-03-05 08:27:21 -0800 | [diff] [blame] | 397 | const int row_end = |
| 398 | AOMMIN(tx_size_high_unit[tx_size], max_blocks_high - blk_row); |
| 399 | const int col_end = |
| 400 | AOMMIN(tx_size_wide_unit[tx_size], max_blocks_wide - blk_col); |
Jingning Han | 4fe5f67 | 2017-05-19 15:46:07 -0700 | [diff] [blame] | 401 | |
Yue Chen | 0797a20 | 2017-10-27 17:24:56 -0700 | [diff] [blame] | 402 | assert(bsw > 0 && bsh > 0); |
Jingning Han | 4fe5f67 | 2017-05-19 15:46:07 -0700 | [diff] [blame] | 403 | |
Wan-Teh Chang | 0cb03d9 | 2021-03-05 08:27:21 -0800 | [diff] [blame] | 404 | for (int r = 0; r < row_end; r += bsh) { |
| 405 | const int offsetr = blk_row + r; |
| 406 | for (int c = 0; c < col_end; c += bsw) { |
Yue Chen | 0797a20 | 2017-10-27 17:24:56 -0700 | [diff] [blame] | 407 | const int offsetc = blk_col + c; |
Yue Chen | 0797a20 | 2017-10-27 17:24:56 -0700 | [diff] [blame] | 408 | pack_txb_tokens(w, cm, x, tp, tok_end, xd, mbmi, plane, plane_bsize, |
| 409 | bit_depth, block, offsetr, offsetc, sub_txs, |
| 410 | token_stats); |
| 411 | block += step; |
| 412 | } |
Jingning Han | 4fe5f67 | 2017-05-19 15:46:07 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 416 | |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 417 | static INLINE void set_spatial_segment_id( |
| 418 | const CommonModeInfoParams *const mi_params, uint8_t *segment_ids, |
chiyotsai | d730cef | 2022-10-26 13:58:20 -0700 | [diff] [blame] | 419 | BLOCK_SIZE bsize, int mi_row, int mi_col, uint8_t segment_id) { |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 420 | const int mi_offset = mi_row * mi_params->mi_cols + mi_col; |
Frederic Barbier | 0c4a632 | 2018-02-22 10:14:00 +0100 | [diff] [blame] | 421 | const int bw = mi_size_wide[bsize]; |
| 422 | const int bh = mi_size_high[bsize]; |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 423 | const int xmis = AOMMIN(mi_params->mi_cols - mi_col, bw); |
| 424 | const int ymis = AOMMIN(mi_params->mi_rows - mi_row, bh); |
Frederic Barbier | 0c4a632 | 2018-02-22 10:14:00 +0100 | [diff] [blame] | 425 | |
chiyotsai | d730cef | 2022-10-26 13:58:20 -0700 | [diff] [blame] | 426 | const int mi_stride = mi_params->mi_cols; |
| 427 | |
chiyotsai | ae4339f | 2022-11-16 10:44:11 -0800 | [diff] [blame] | 428 | set_segment_id(segment_ids, mi_offset, xmis, ymis, mi_stride, segment_id); |
Frederic Barbier | 0c4a632 | 2018-02-22 10:14:00 +0100 | [diff] [blame] | 429 | } |
| 430 | |
Frederic Barbier | 19f0a23 | 2018-03-07 12:28:20 +0100 | [diff] [blame] | 431 | int av1_neg_interleave(int x, int ref, int max) { |
Hui Su | 6bf4ec8 | 2018-03-28 21:18:10 -0700 | [diff] [blame] | 432 | assert(x < max); |
Rostislav Pehlivanov | f624dd5 | 2017-10-24 16:46:09 +0100 | [diff] [blame] | 433 | const int diff = x - ref; |
| 434 | if (!ref) return x; |
Frederic Barbier | 541429a | 2018-03-07 14:33:42 +0100 | [diff] [blame] | 435 | if (ref >= (max - 1)) return -x + max - 1; |
Rostislav Pehlivanov | f624dd5 | 2017-10-24 16:46:09 +0100 | [diff] [blame] | 436 | if (2 * ref < max) { |
| 437 | if (abs(diff) <= ref) { |
| 438 | if (diff > 0) |
| 439 | return (diff << 1) - 1; |
| 440 | else |
| 441 | return ((-diff) << 1); |
| 442 | } |
| 443 | return x; |
| 444 | } else { |
| 445 | if (abs(diff) < (max - ref)) { |
| 446 | if (diff > 0) |
| 447 | return (diff << 1) - 1; |
| 448 | else |
| 449 | return ((-diff) << 1); |
| 450 | } |
| 451 | return (max - x) - 1; |
| 452 | } |
| 453 | } |
| 454 | |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 455 | static AOM_INLINE void write_segment_id(AV1_COMP *cpi, MACROBLOCKD *const xd, |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 456 | const MB_MODE_INFO *const mbmi, |
| 457 | aom_writer *w, |
| 458 | const struct segmentation *seg, |
| 459 | struct segmentation_probs *segp, |
| 460 | int skip_txfm) { |
Rostislav Pehlivanov | 938710c | 2017-11-28 02:26:21 +0000 | [diff] [blame] | 461 | if (!seg->enabled || !seg->update_map) return; |
Rostislav Pehlivanov | f624dd5 | 2017-10-24 16:46:09 +0100 | [diff] [blame] | 462 | |
Hui Su | cff20b9 | 2018-03-13 15:35:52 -0700 | [diff] [blame] | 463 | AV1_COMMON *const cm = &cpi->common; |
Hui Su | cff20b9 | 2018-03-13 15:35:52 -0700 | [diff] [blame] | 464 | int cdf_num; |
chiyotsai | d730cef | 2022-10-26 13:58:20 -0700 | [diff] [blame] | 465 | const uint8_t pred = av1_get_spatial_seg_pred( |
| 466 | cm, xd, &cdf_num, cpi->cyclic_refresh->skip_over4x4); |
Hui Su | d62a63a | 2020-02-27 16:59:54 -0800 | [diff] [blame] | 467 | const int mi_row = xd->mi_row; |
| 468 | const int mi_col = xd->mi_col; |
Rostislav Pehlivanov | f624dd5 | 2017-10-24 16:46:09 +0100 | [diff] [blame] | 469 | |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 470 | if (skip_txfm) { |
| 471 | // Still need to transmit tx size for intra blocks even if skip_txfm is |
Hui Su | ad7551e | 2018-03-14 11:13:31 -0700 | [diff] [blame] | 472 | // true. Changing segment_id may make the tx size become invalid, e.g |
| 473 | // changing from lossless to lossy. |
Vishesh | 8c90fff | 2020-04-13 11:32:56 +0530 | [diff] [blame] | 474 | assert(is_inter_block(mbmi) || !cpi->enc_seg.has_lossless_segment); |
Hui Su | ad7551e | 2018-03-14 11:13:31 -0700 | [diff] [blame] | 475 | |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 476 | set_spatial_segment_id(&cm->mi_params, cm->cur_frame->seg_map, mbmi->bsize, |
| 477 | mi_row, mi_col, pred); |
| 478 | set_spatial_segment_id(&cm->mi_params, cpi->enc_seg.map, mbmi->bsize, |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 479 | mi_row, mi_col, pred); |
Rostislav Pehlivanov | 938710c | 2017-11-28 02:26:21 +0000 | [diff] [blame] | 480 | /* mbmi is read only but we need to update segment_id */ |
| 481 | ((MB_MODE_INFO *)mbmi)->segment_id = pred; |
Rostislav Pehlivanov | f624dd5 | 2017-10-24 16:46:09 +0100 | [diff] [blame] | 482 | return; |
| 483 | } |
| 484 | |
Hui Su | cff20b9 | 2018-03-13 15:35:52 -0700 | [diff] [blame] | 485 | const int coded_id = |
Frederic Barbier | 19f0a23 | 2018-03-07 12:28:20 +0100 | [diff] [blame] | 486 | av1_neg_interleave(mbmi->segment_id, pred, seg->last_active_segid + 1); |
Rostislav Pehlivanov | 938710c | 2017-11-28 02:26:21 +0000 | [diff] [blame] | 487 | aom_cdf_prob *pred_cdf = segp->spatial_pred_seg_cdf[cdf_num]; |
Luc Trudeau | bb6ca12 | 2018-04-10 16:59:37 -0400 | [diff] [blame] | 488 | aom_write_symbol(w, coded_id, pred_cdf, MAX_SEGMENTS); |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 489 | set_spatial_segment_id(&cm->mi_params, cm->cur_frame->seg_map, mbmi->bsize, |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 490 | mi_row, mi_col, mbmi->segment_id); |
Rostislav Pehlivanov | f624dd5 | 2017-10-24 16:46:09 +0100 | [diff] [blame] | 491 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 492 | |
Thomas Davies | 315f578 | 2017-06-14 15:14:55 +0100 | [diff] [blame] | 493 | #define WRITE_REF_BIT(bname, pname) \ |
Thomas Davies | 0fbd2b7 | 2017-09-12 10:49:45 +0100 | [diff] [blame] | 494 | aom_write_symbol(w, bname, av1_get_pred_cdf_##pname(xd), 2) |
Thomas Davies | 315f578 | 2017-06-14 15:14:55 +0100 | [diff] [blame] | 495 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 496 | // This function encodes the reference frame |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 497 | static AOM_INLINE void write_ref_frames(const AV1_COMMON *cm, |
| 498 | const MACROBLOCKD *xd, aom_writer *w) { |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 499 | const MB_MODE_INFO *const mbmi = xd->mi[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 500 | const int is_compound = has_second_ref(mbmi); |
chiyotsai | d730cef | 2022-10-26 13:58:20 -0700 | [diff] [blame] | 501 | const uint8_t segment_id = mbmi->segment_id; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 502 | |
| 503 | // If segment level coding of this signal is disabled... |
| 504 | // or the segment allows multiple reference frame options |
| 505 | if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) { |
| 506 | assert(!is_compound); |
| 507 | assert(mbmi->ref_frame[0] == |
| 508 | get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME)); |
Yaowu Xu | 564a522 | 2018-03-20 09:12:16 -0700 | [diff] [blame] | 509 | } else if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP) || |
| 510 | segfeature_active(&cm->seg, segment_id, SEG_LVL_GLOBALMV)) { |
David Barker | d92f356 | 2017-10-09 17:46:23 +0100 | [diff] [blame] | 511 | assert(!is_compound); |
| 512 | assert(mbmi->ref_frame[0] == LAST_FRAME); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 513 | } else { |
| 514 | // does the feature use compound prediction or not |
| 515 | // (if not specified at the frame/segment level) |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 516 | if (cm->current_frame.reference_mode == REFERENCE_MODE_SELECT) { |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 517 | if (is_comp_ref_allowed(mbmi->bsize)) |
Yaowu Xu | 6567c4a | 2018-03-19 21:39:31 -0700 | [diff] [blame] | 518 | aom_write_symbol(w, is_compound, av1_get_reference_mode_cdf(xd), 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 519 | } else { |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 520 | assert((!is_compound) == |
| 521 | (cm->current_frame.reference_mode == SINGLE_REFERENCE)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | if (is_compound) { |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 525 | const COMP_REFERENCE_TYPE comp_ref_type = has_uni_comp_refs(mbmi) |
| 526 | ? UNIDIR_COMP_REFERENCE |
| 527 | : BIDIR_COMP_REFERENCE; |
Sebastien Alaiwan | 4be6cb3 | 2017-11-02 18:04:11 +0100 | [diff] [blame] | 528 | aom_write_symbol(w, comp_ref_type, av1_get_comp_reference_type_cdf(xd), |
| 529 | 2); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 530 | |
| 531 | if (comp_ref_type == UNIDIR_COMP_REFERENCE) { |
| 532 | const int bit = mbmi->ref_frame[0] == BWDREF_FRAME; |
Zoe Liu | 4917295 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 533 | WRITE_REF_BIT(bit, uni_comp_ref_p); |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 534 | |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 535 | if (!bit) { |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 536 | assert(mbmi->ref_frame[0] == LAST_FRAME); |
Sebastien Alaiwan | 4be6cb3 | 2017-11-02 18:04:11 +0100 | [diff] [blame] | 537 | const int bit1 = mbmi->ref_frame[1] == LAST3_FRAME || |
| 538 | mbmi->ref_frame[1] == GOLDEN_FRAME; |
Zoe Liu | 4917295 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 539 | WRITE_REF_BIT(bit1, uni_comp_ref_p1); |
Sebastien Alaiwan | 4be6cb3 | 2017-11-02 18:04:11 +0100 | [diff] [blame] | 540 | if (bit1) { |
| 541 | const int bit2 = mbmi->ref_frame[1] == GOLDEN_FRAME; |
Zoe Liu | 4917295 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 542 | WRITE_REF_BIT(bit2, uni_comp_ref_p2); |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 543 | } |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 544 | } else { |
| 545 | assert(mbmi->ref_frame[1] == ALTREF_FRAME); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | return; |
| 549 | } |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 550 | |
| 551 | assert(comp_ref_type == BIDIR_COMP_REFERENCE); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 552 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 553 | const int bit = (mbmi->ref_frame[0] == GOLDEN_FRAME || |
| 554 | mbmi->ref_frame[0] == LAST3_FRAME); |
Sebastien Alaiwan | 4be6cb3 | 2017-11-02 18:04:11 +0100 | [diff] [blame] | 555 | WRITE_REF_BIT(bit, comp_ref_p); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 556 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 557 | if (!bit) { |
Zoe Liu | 8781828 | 2017-11-26 17:09:59 -0800 | [diff] [blame] | 558 | const int bit1 = mbmi->ref_frame[0] == LAST2_FRAME; |
Sebastien Alaiwan | 4be6cb3 | 2017-11-02 18:04:11 +0100 | [diff] [blame] | 559 | WRITE_REF_BIT(bit1, comp_ref_p1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 560 | } else { |
Sebastien Alaiwan | 4be6cb3 | 2017-11-02 18:04:11 +0100 | [diff] [blame] | 561 | const int bit2 = mbmi->ref_frame[0] == GOLDEN_FRAME; |
| 562 | WRITE_REF_BIT(bit2, comp_ref_p2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 563 | } |
Zoe Liu | 7b1ec7a | 2017-05-24 22:28:24 -0700 | [diff] [blame] | 564 | |
Sebastien Alaiwan | 4be6cb3 | 2017-11-02 18:04:11 +0100 | [diff] [blame] | 565 | const int bit_bwd = mbmi->ref_frame[1] == ALTREF_FRAME; |
Zoe Liu | 4917295 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 566 | WRITE_REF_BIT(bit_bwd, comp_bwdref_p); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 567 | |
Sebastien Alaiwan | 4be6cb3 | 2017-11-02 18:04:11 +0100 | [diff] [blame] | 568 | if (!bit_bwd) { |
Zoe Liu | 4917295 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 569 | WRITE_REF_BIT(mbmi->ref_frame[1] == ALTREF2_FRAME, comp_bwdref_p1); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 570 | } |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 571 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 572 | } else { |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 573 | const int bit0 = (mbmi->ref_frame[0] <= ALTREF_FRAME && |
| 574 | mbmi->ref_frame[0] >= BWDREF_FRAME); |
Zoe Liu | 4917295 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 575 | WRITE_REF_BIT(bit0, single_ref_p1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 576 | |
| 577 | if (bit0) { |
Sebastien Alaiwan | 4be6cb3 | 2017-11-02 18:04:11 +0100 | [diff] [blame] | 578 | const int bit1 = mbmi->ref_frame[0] == ALTREF_FRAME; |
Zoe Liu | 4917295 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 579 | WRITE_REF_BIT(bit1, single_ref_p2); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 580 | |
Sebastien Alaiwan | 4be6cb3 | 2017-11-02 18:04:11 +0100 | [diff] [blame] | 581 | if (!bit1) { |
Zoe Liu | 4917295 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 582 | WRITE_REF_BIT(mbmi->ref_frame[0] == ALTREF2_FRAME, single_ref_p6); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 583 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 584 | } else { |
| 585 | const int bit2 = (mbmi->ref_frame[0] == LAST3_FRAME || |
| 586 | mbmi->ref_frame[0] == GOLDEN_FRAME); |
Zoe Liu | 4917295 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 587 | WRITE_REF_BIT(bit2, single_ref_p3); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 588 | |
| 589 | if (!bit2) { |
Sebastien Alaiwan | 4be6cb3 | 2017-11-02 18:04:11 +0100 | [diff] [blame] | 590 | const int bit3 = mbmi->ref_frame[0] != LAST_FRAME; |
Zoe Liu | 4917295 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 591 | WRITE_REF_BIT(bit3, single_ref_p4); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 592 | } else { |
Sebastien Alaiwan | 4be6cb3 | 2017-11-02 18:04:11 +0100 | [diff] [blame] | 593 | const int bit4 = mbmi->ref_frame[0] != LAST3_FRAME; |
Zoe Liu | 4917295 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 594 | WRITE_REF_BIT(bit4, single_ref_p5); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 595 | } |
| 596 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 601 | static AOM_INLINE void write_filter_intra_mode_info( |
| 602 | const AV1_COMMON *cm, const MACROBLOCKD *xd, const MB_MODE_INFO *const mbmi, |
| 603 | aom_writer *w) { |
Yue Chen | a938362 | 2018-03-08 14:37:09 -0800 | [diff] [blame] | 604 | if (av1_filter_intra_allowed(cm, mbmi)) { |
Yue Chen | b057187 | 2017-12-18 18:12:59 -0800 | [diff] [blame] | 605 | aom_write_symbol(w, mbmi->filter_intra_mode_info.use_filter_intra, |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 606 | xd->tile_ctx->filter_intra_cdfs[mbmi->bsize], 2); |
Yue Chen | b057187 | 2017-12-18 18:12:59 -0800 | [diff] [blame] | 607 | if (mbmi->filter_intra_mode_info.use_filter_intra) { |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 608 | const FILTER_INTRA_MODE mode = |
Yue Chen | b057187 | 2017-12-18 18:12:59 -0800 | [diff] [blame] | 609 | mbmi->filter_intra_mode_info.filter_intra_mode; |
Yue Chen | 994dba2 | 2017-12-19 15:27:26 -0800 | [diff] [blame] | 610 | aom_write_symbol(w, mode, xd->tile_ctx->filter_intra_mode_cdf, |
Yue Chen | 63ce36f | 2017-10-10 23:37:31 -0700 | [diff] [blame] | 611 | FILTER_INTRA_MODES); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 612 | } |
| 613 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 616 | static AOM_INLINE void write_angle_delta(aom_writer *w, int angle_delta, |
| 617 | aom_cdf_prob *cdf) { |
Luc Trudeau | 866da79 | 2018-02-12 11:13:34 -0500 | [diff] [blame] | 618 | aom_write_symbol(w, angle_delta + MAX_ANGLE_DELTA, cdf, |
| 619 | 2 * MAX_ANGLE_DELTA + 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 620 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 621 | |
Urvang Joshi | 6237b88 | 2020-03-26 15:02:26 -0700 | [diff] [blame] | 622 | static AOM_INLINE void write_mb_interp_filter(AV1_COMMON *const cm, |
Cherma Rajan A | d0f59a7 | 2021-04-13 12:43:54 +0530 | [diff] [blame] | 623 | ThreadData *td, aom_writer *w) { |
| 624 | const MACROBLOCKD *xd = &td->mb.e_mbd; |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 625 | const MB_MODE_INFO *const mbmi = xd->mi[0]; |
Thomas Davies | 77c7c40 | 2017-01-11 17:58:54 +0000 | [diff] [blame] | 626 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
Jingning Han | 203b1d3 | 2017-01-12 16:00:13 -0800 | [diff] [blame] | 627 | |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 628 | if (!av1_is_interp_needed(xd)) { |
Urvang Joshi | 6237b88 | 2020-03-26 15:02:26 -0700 | [diff] [blame] | 629 | int_interpfilters filters = av1_broadcast_interp_filter( |
| 630 | av1_unswitchable_filter(cm->features.interp_filter)); |
Ravi Chaudhary | 1e4f94b | 2019-06-20 16:19:49 +0530 | [diff] [blame] | 631 | assert(mbmi->interp_filters.as_int == filters.as_int); |
| 632 | (void)filters; |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 633 | return; |
| 634 | } |
Urvang Joshi | 6237b88 | 2020-03-26 15:02:26 -0700 | [diff] [blame] | 635 | if (cm->features.interp_filter == SWITCHABLE) { |
Jingning Han | 203b1d3 | 2017-01-12 16:00:13 -0800 | [diff] [blame] | 636 | int dir; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 637 | for (dir = 0; dir < 2; ++dir) { |
Jingning Han | 4a17335 | 2018-03-01 17:54:07 -0800 | [diff] [blame] | 638 | const int ctx = av1_get_pred_context_switchable_interp(xd, dir); |
| 639 | InterpFilter filter = |
| 640 | av1_extract_interp_filter(mbmi->interp_filters, dir); |
| 641 | aom_write_symbol(w, filter, ec_ctx->switchable_interp_cdf[ctx], |
| 642 | SWITCHABLE_FILTERS); |
Cherma Rajan A | d0f59a7 | 2021-04-13 12:43:54 +0530 | [diff] [blame] | 643 | ++td->interp_filter_selected[filter]; |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 644 | if (cm->seq_params->enable_dual_filter == 0) return; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 645 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 649 | // Transmit color values with delta encoding. Write the first value as |
| 650 | // literal, and the deltas between each value and the previous one. "min_val" is |
| 651 | // the smallest possible value of the deltas. |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 652 | static AOM_INLINE void delta_encode_palette_colors(const int *colors, int num, |
| 653 | int bit_depth, int min_val, |
| 654 | aom_writer *w) { |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 655 | if (num <= 0) return; |
hui su | fa4ff85 | 2017-05-15 12:20:50 -0700 | [diff] [blame] | 656 | assert(colors[0] < (1 << bit_depth)); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 657 | aom_write_literal(w, colors[0], bit_depth); |
| 658 | if (num == 1) return; |
| 659 | int max_delta = 0; |
| 660 | int deltas[PALETTE_MAX_SIZE]; |
| 661 | memset(deltas, 0, sizeof(deltas)); |
| 662 | for (int i = 1; i < num; ++i) { |
hui su | fa4ff85 | 2017-05-15 12:20:50 -0700 | [diff] [blame] | 663 | assert(colors[i] < (1 << bit_depth)); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 664 | const int delta = colors[i] - colors[i - 1]; |
| 665 | deltas[i - 1] = delta; |
| 666 | assert(delta >= min_val); |
| 667 | if (delta > max_delta) max_delta = delta; |
| 668 | } |
| 669 | const int min_bits = bit_depth - 3; |
| 670 | int bits = AOMMAX(av1_ceil_log2(max_delta + 1 - min_val), min_bits); |
hui su | fa4ff85 | 2017-05-15 12:20:50 -0700 | [diff] [blame] | 671 | assert(bits <= bit_depth); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 672 | int range = (1 << bit_depth) - colors[0] - min_val; |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 673 | aom_write_literal(w, bits - min_bits, 2); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 674 | for (int i = 0; i < num - 1; ++i) { |
| 675 | aom_write_literal(w, deltas[i] - min_val, bits); |
| 676 | range -= deltas[i]; |
| 677 | bits = AOMMIN(bits, av1_ceil_log2(range)); |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 681 | // Transmit luma palette color values. First signal if each color in the color |
| 682 | // cache is used. Those colors that are not in the cache are transmitted with |
| 683 | // delta encoding. |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 684 | static AOM_INLINE void write_palette_colors_y( |
| 685 | const MACROBLOCKD *const xd, const PALETTE_MODE_INFO *const pmi, |
| 686 | int bit_depth, aom_writer *w) { |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 687 | const int n = pmi->palette_size[0]; |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 688 | uint16_t color_cache[2 * PALETTE_MAX_SIZE]; |
Hui Su | 3748bc2 | 2017-08-23 11:30:41 -0700 | [diff] [blame] | 689 | const int n_cache = av1_get_palette_cache(xd, 0, color_cache); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 690 | int out_cache_colors[PALETTE_MAX_SIZE]; |
| 691 | uint8_t cache_color_found[2 * PALETTE_MAX_SIZE]; |
| 692 | const int n_out_cache = |
| 693 | av1_index_color_cache(color_cache, n_cache, pmi->palette_colors, n, |
| 694 | cache_color_found, out_cache_colors); |
| 695 | int n_in_cache = 0; |
| 696 | for (int i = 0; i < n_cache && n_in_cache < n; ++i) { |
| 697 | const int found = cache_color_found[i]; |
| 698 | aom_write_bit(w, found); |
| 699 | n_in_cache += found; |
| 700 | } |
| 701 | assert(n_in_cache + n_out_cache == n); |
| 702 | delta_encode_palette_colors(out_cache_colors, n_out_cache, bit_depth, 1, w); |
| 703 | } |
| 704 | |
| 705 | // Write chroma palette color values. U channel is handled similarly to the luma |
| 706 | // channel. For v channel, either use delta encoding or transmit raw values |
| 707 | // directly, whichever costs less. |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 708 | static AOM_INLINE void write_palette_colors_uv( |
| 709 | const MACROBLOCKD *const xd, const PALETTE_MODE_INFO *const pmi, |
| 710 | int bit_depth, aom_writer *w) { |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 711 | const int n = pmi->palette_size[1]; |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 712 | const uint16_t *colors_u = pmi->palette_colors + PALETTE_MAX_SIZE; |
| 713 | const uint16_t *colors_v = pmi->palette_colors + 2 * PALETTE_MAX_SIZE; |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 714 | // U channel colors. |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 715 | uint16_t color_cache[2 * PALETTE_MAX_SIZE]; |
Hui Su | 3748bc2 | 2017-08-23 11:30:41 -0700 | [diff] [blame] | 716 | const int n_cache = av1_get_palette_cache(xd, 1, color_cache); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 717 | int out_cache_colors[PALETTE_MAX_SIZE]; |
| 718 | uint8_t cache_color_found[2 * PALETTE_MAX_SIZE]; |
| 719 | const int n_out_cache = av1_index_color_cache( |
| 720 | color_cache, n_cache, colors_u, n, cache_color_found, out_cache_colors); |
| 721 | int n_in_cache = 0; |
| 722 | for (int i = 0; i < n_cache && n_in_cache < n; ++i) { |
| 723 | const int found = cache_color_found[i]; |
| 724 | aom_write_bit(w, found); |
| 725 | n_in_cache += found; |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 726 | } |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 727 | delta_encode_palette_colors(out_cache_colors, n_out_cache, bit_depth, 0, w); |
| 728 | |
| 729 | // V channel colors. Don't use color cache as the colors are not sorted. |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 730 | const int max_val = 1 << bit_depth; |
| 731 | int zero_count = 0, min_bits_v = 0; |
| 732 | int bits_v = |
| 733 | av1_get_palette_delta_bits_v(pmi, bit_depth, &zero_count, &min_bits_v); |
| 734 | const int rate_using_delta = |
| 735 | 2 + bit_depth + (bits_v + 1) * (n - 1) - zero_count; |
| 736 | const int rate_using_raw = bit_depth * n; |
| 737 | if (rate_using_delta < rate_using_raw) { // delta encoding |
hui su | fa4ff85 | 2017-05-15 12:20:50 -0700 | [diff] [blame] | 738 | assert(colors_v[0] < (1 << bit_depth)); |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 739 | aom_write_bit(w, 1); |
| 740 | aom_write_literal(w, bits_v - min_bits_v, 2); |
| 741 | aom_write_literal(w, colors_v[0], bit_depth); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 742 | for (int i = 1; i < n; ++i) { |
hui su | fa4ff85 | 2017-05-15 12:20:50 -0700 | [diff] [blame] | 743 | assert(colors_v[i] < (1 << bit_depth)); |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 744 | if (colors_v[i] == colors_v[i - 1]) { // No need to signal sign bit. |
| 745 | aom_write_literal(w, 0, bits_v); |
| 746 | continue; |
| 747 | } |
| 748 | const int delta = abs((int)colors_v[i] - colors_v[i - 1]); |
| 749 | const int sign_bit = colors_v[i] < colors_v[i - 1]; |
| 750 | if (delta <= max_val - delta) { |
| 751 | aom_write_literal(w, delta, bits_v); |
| 752 | aom_write_bit(w, sign_bit); |
| 753 | } else { |
| 754 | aom_write_literal(w, max_val - delta, bits_v); |
| 755 | aom_write_bit(w, !sign_bit); |
| 756 | } |
| 757 | } |
| 758 | } else { // Transmit raw values. |
| 759 | aom_write_bit(w, 0); |
hui su | fa4ff85 | 2017-05-15 12:20:50 -0700 | [diff] [blame] | 760 | for (int i = 0; i < n; ++i) { |
| 761 | assert(colors_v[i] < (1 << bit_depth)); |
| 762 | aom_write_literal(w, colors_v[i], bit_depth); |
| 763 | } |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 764 | } |
| 765 | } |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 766 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 767 | static AOM_INLINE void write_palette_mode_info(const AV1_COMMON *cm, |
| 768 | const MACROBLOCKD *xd, |
| 769 | const MB_MODE_INFO *const mbmi, |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 770 | aom_writer *w) { |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 771 | const int num_planes = av1_num_planes(cm); |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 772 | const BLOCK_SIZE bsize = mbmi->bsize; |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 773 | assert(av1_allow_palette(cm->features.allow_screen_content_tools, bsize)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 774 | const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
Hui Su | c1f411b | 2017-12-19 15:58:28 -0800 | [diff] [blame] | 775 | const int bsize_ctx = av1_get_palette_bsize_ctx(bsize); |
Rupert Swarbrick | 6f9cd94 | 2017-08-02 15:57:18 +0100 | [diff] [blame] | 776 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 777 | if (mbmi->mode == DC_PRED) { |
Urvang Joshi | 23a6111 | 2017-01-30 14:59:27 -0800 | [diff] [blame] | 778 | const int n = pmi->palette_size[0]; |
Hui Su | db68555 | 2018-01-12 16:38:33 -0800 | [diff] [blame] | 779 | const int palette_y_mode_ctx = av1_get_palette_mode_ctx(xd); |
Thomas Davies | 59f9231 | 2017-08-23 00:33:12 +0100 | [diff] [blame] | 780 | aom_write_symbol( |
| 781 | w, n > 0, |
Hui Su | c1f411b | 2017-12-19 15:58:28 -0800 | [diff] [blame] | 782 | xd->tile_ctx->palette_y_mode_cdf[bsize_ctx][palette_y_mode_ctx], 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 783 | if (n > 0) { |
Thomas Davies | ce7272d | 2017-07-04 16:11:08 +0100 | [diff] [blame] | 784 | aom_write_symbol(w, n - PALETTE_MIN_SIZE, |
Hui Su | c1f411b | 2017-12-19 15:58:28 -0800 | [diff] [blame] | 785 | xd->tile_ctx->palette_y_size_cdf[bsize_ctx], |
Thomas Davies | ce7272d | 2017-07-04 16:11:08 +0100 | [diff] [blame] | 786 | PALETTE_SIZES); |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 787 | write_palette_colors_y(xd, pmi, cm->seq_params->bit_depth, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | |
Hui Su | 474e1e1 | 2020-02-27 15:46:36 -0800 | [diff] [blame] | 791 | const int uv_dc_pred = |
| 792 | num_planes > 1 && mbmi->uv_mode == UV_DC_PRED && xd->is_chroma_ref; |
Rupert Swarbrick | dcb3cff | 2017-11-09 15:58:33 +0000 | [diff] [blame] | 793 | if (uv_dc_pred) { |
Urvang Joshi | 23a6111 | 2017-01-30 14:59:27 -0800 | [diff] [blame] | 794 | const int n = pmi->palette_size[1]; |
| 795 | const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0); |
Thomas Davies | 59f9231 | 2017-08-23 00:33:12 +0100 | [diff] [blame] | 796 | aom_write_symbol(w, n > 0, |
| 797 | xd->tile_ctx->palette_uv_mode_cdf[palette_uv_mode_ctx], 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 798 | if (n > 0) { |
Thomas Davies | ce7272d | 2017-07-04 16:11:08 +0100 | [diff] [blame] | 799 | aom_write_symbol(w, n - PALETTE_MIN_SIZE, |
Hui Su | c1f411b | 2017-12-19 15:58:28 -0800 | [diff] [blame] | 800 | xd->tile_ctx->palette_uv_size_cdf[bsize_ctx], |
Thomas Davies | ce7272d | 2017-07-04 16:11:08 +0100 | [diff] [blame] | 801 | PALETTE_SIZES); |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 802 | write_palette_colors_uv(xd, pmi, cm->seq_params->bit_depth, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | } |
| 806 | |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 807 | void av1_write_tx_type(const AV1_COMMON *const cm, const MACROBLOCKD *xd, |
Hui Su | 179fbfa | 2019-10-07 15:13:06 -0700 | [diff] [blame] | 808 | TX_TYPE tx_type, TX_SIZE tx_size, aom_writer *w) { |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 809 | MB_MODE_INFO *mbmi = xd->mi[0]; |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 810 | const FeatureFlags *const features = &cm->features; |
Jingning Han | 2a4da94 | 2016-11-03 18:31:30 -0700 | [diff] [blame] | 811 | const int is_inter = is_inter_block(mbmi); |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 812 | if (get_ext_tx_types(tx_size, is_inter, features->reduced_tx_set_used) > 1 && |
Urvang Joshi | 1781462 | 2020-03-27 17:26:17 -0700 | [diff] [blame] | 813 | ((!cm->seg.enabled && cm->quant_params.base_qindex > 0) || |
Hui Su | 99350a6 | 2018-01-11 16:41:09 -0800 | [diff] [blame] | 814 | (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) && |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 815 | !mbmi->skip_txfm && |
Hui Su | 99350a6 | 2018-01-11 16:41:09 -0800 | [diff] [blame] | 816 | !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) { |
Hui Su | 179fbfa | 2019-10-07 15:13:06 -0700 | [diff] [blame] | 817 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
| 818 | const TX_SIZE square_tx_size = txsize_sqr_map[tx_size]; |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 819 | const TxSetType tx_set_type = av1_get_ext_tx_set_type( |
| 820 | tx_size, is_inter, features->reduced_tx_set_used); |
| 821 | const int eset = |
| 822 | get_ext_tx_set(tx_size, is_inter, features->reduced_tx_set_used); |
Hui Su | 99350a6 | 2018-01-11 16:41:09 -0800 | [diff] [blame] | 823 | // eset == 0 should correspond to a set with only DCT_DCT and there |
| 824 | // is no need to send the tx_type |
| 825 | assert(eset > 0); |
| 826 | assert(av1_ext_tx_used[tx_set_type][tx_type]); |
| 827 | if (is_inter) { |
| 828 | aom_write_symbol(w, av1_ext_tx_ind[tx_set_type][tx_type], |
| 829 | ec_ctx->inter_ext_tx_cdf[eset][square_tx_size], |
| 830 | av1_num_ext_tx_set[tx_set_type]); |
| 831 | } else { |
Hui Su | 99350a6 | 2018-01-11 16:41:09 -0800 | [diff] [blame] | 832 | PREDICTION_MODE intra_dir; |
| 833 | if (mbmi->filter_intra_mode_info.use_filter_intra) |
| 834 | intra_dir = |
| 835 | fimode_to_intradir[mbmi->filter_intra_mode_info.filter_intra_mode]; |
| 836 | else |
| 837 | intra_dir = mbmi->mode; |
| 838 | aom_write_symbol( |
| 839 | w, av1_ext_tx_ind[tx_set_type][tx_type], |
| 840 | ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][intra_dir], |
| 841 | av1_num_ext_tx_set[tx_set_type]); |
Lester Lu | 432012f | 2017-08-17 14:39:29 -0700 | [diff] [blame] | 842 | } |
Jingning Han | 2a4da94 | 2016-11-03 18:31:30 -0700 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 846 | static AOM_INLINE void write_intra_y_mode_nonkf(FRAME_CONTEXT *frame_ctx, |
| 847 | BLOCK_SIZE bsize, |
| 848 | PREDICTION_MODE mode, |
| 849 | aom_writer *w) { |
Hui Su | 814f41e | 2017-10-02 12:21:24 -0700 | [diff] [blame] | 850 | aom_write_symbol(w, mode, frame_ctx->y_mode_cdf[size_group_lookup[bsize]], |
Jingning Han | f04254f | 2017-03-08 10:51:35 -0800 | [diff] [blame] | 851 | INTRA_MODES); |
Jingning Han | f04254f | 2017-03-08 10:51:35 -0800 | [diff] [blame] | 852 | } |
| 853 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 854 | static AOM_INLINE void write_intra_uv_mode(FRAME_CONTEXT *frame_ctx, |
| 855 | UV_PREDICTION_MODE uv_mode, |
| 856 | PREDICTION_MODE y_mode, |
| 857 | CFL_ALLOWED_TYPE cfl_allowed, |
| 858 | aom_writer *w) { |
David Michael Barr | cb3a8ef | 2018-01-06 15:48:49 +0900 | [diff] [blame] | 859 | aom_write_symbol(w, uv_mode, frame_ctx->uv_mode_cdf[cfl_allowed][y_mode], |
| 860 | UV_INTRA_MODES - !cfl_allowed); |
Jingning Han | f04254f | 2017-03-08 10:51:35 -0800 | [diff] [blame] | 861 | } |
| 862 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 863 | static AOM_INLINE void write_cfl_alphas(FRAME_CONTEXT *const ec_ctx, |
| 864 | uint8_t idx, int8_t joint_sign, |
| 865 | aom_writer *w) { |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 866 | aom_write_symbol(w, joint_sign, ec_ctx->cfl_sign_cdf, CFL_JOINT_SIGNS); |
| 867 | // Magnitudes are only signaled for nonzero codes. |
| 868 | if (CFL_SIGN_U(joint_sign) != CFL_SIGN_ZERO) { |
| 869 | aom_cdf_prob *cdf_u = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)]; |
| 870 | aom_write_symbol(w, CFL_IDX_U(idx), cdf_u, CFL_ALPHABET_SIZE); |
| 871 | } |
| 872 | if (CFL_SIGN_V(joint_sign) != CFL_SIGN_ZERO) { |
| 873 | aom_cdf_prob *cdf_v = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_V(joint_sign)]; |
| 874 | aom_write_symbol(w, CFL_IDX_V(idx), cdf_v, CFL_ALPHABET_SIZE); |
| 875 | } |
Luc Trudeau | f533400 | 2017-04-25 12:21:26 -0400 | [diff] [blame] | 876 | } |
Luc Trudeau | f533400 | 2017-04-25 12:21:26 -0400 | [diff] [blame] | 877 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 878 | static AOM_INLINE void write_cdef(AV1_COMMON *cm, MACROBLOCKD *const xd, |
Hui Su | d62a63a | 2020-02-27 16:59:54 -0800 | [diff] [blame] | 879 | aom_writer *w, int skip) { |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 880 | if (cm->features.coded_lossless || cm->features.allow_intrabc) return; |
Steinar Midtskogen | 6c24b02 | 2017-09-15 09:46:39 +0200 | [diff] [blame] | 881 | |
Urvang Joshi | 791fc3f | 2020-04-07 00:19:46 -0700 | [diff] [blame] | 882 | // At the start of a superblock, mark that we haven't yet written CDEF |
| 883 | // strengths for any of the CDEF units contained in this superblock. |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 884 | const int sb_mask = (cm->seq_params->mib_size - 1); |
Urvang Joshi | 791fc3f | 2020-04-07 00:19:46 -0700 | [diff] [blame] | 885 | const int mi_row_in_sb = (xd->mi_row & sb_mask); |
| 886 | const int mi_col_in_sb = (xd->mi_col & sb_mask); |
| 887 | if (mi_row_in_sb == 0 && mi_col_in_sb == 0) { |
| 888 | xd->cdef_transmitted[0] = xd->cdef_transmitted[1] = |
| 889 | xd->cdef_transmitted[2] = xd->cdef_transmitted[3] = false; |
Steinar Midtskogen | 6c24b02 | 2017-09-15 09:46:39 +0200 | [diff] [blame] | 890 | } |
| 891 | |
Urvang Joshi | 791fc3f | 2020-04-07 00:19:46 -0700 | [diff] [blame] | 892 | // CDEF unit size is 64x64 irrespective of the superblock size. |
| 893 | const int cdef_size = 1 << (6 - MI_SIZE_LOG2); |
| 894 | |
| 895 | // Find index of this CDEF unit in this superblock. |
| 896 | const int index_mask = cdef_size; |
| 897 | const int cdef_unit_row_in_sb = ((xd->mi_row & index_mask) != 0); |
| 898 | const int cdef_unit_col_in_sb = ((xd->mi_col & index_mask) != 0); |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 899 | const int index = (cm->seq_params->sb_size == BLOCK_128X128) |
Urvang Joshi | 791fc3f | 2020-04-07 00:19:46 -0700 | [diff] [blame] | 900 | ? cdef_unit_col_in_sb + 2 * cdef_unit_row_in_sb |
Steinar Midtskogen | 6c24b02 | 2017-09-15 09:46:39 +0200 | [diff] [blame] | 901 | : 0; |
Urvang Joshi | 791fc3f | 2020-04-07 00:19:46 -0700 | [diff] [blame] | 902 | |
| 903 | // Write CDEF strength to the first non-skip coding block in this CDEF unit. |
| 904 | if (!xd->cdef_transmitted[index] && !skip) { |
| 905 | // CDEF strength for this CDEF unit needs to be stored in the MB_MODE_INFO |
| 906 | // of the 1st block in this CDEF unit. |
| 907 | const int first_block_mask = ~(cdef_size - 1); |
| 908 | const CommonModeInfoParams *const mi_params = &cm->mi_params; |
| 909 | const int grid_idx = |
| 910 | get_mi_grid_idx(mi_params, xd->mi_row & first_block_mask, |
| 911 | xd->mi_col & first_block_mask); |
| 912 | const MB_MODE_INFO *const mbmi = mi_params->mi_grid_base[grid_idx]; |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 913 | aom_write_literal(w, mbmi->cdef_strength, cm->cdef_info.cdef_bits); |
Urvang Joshi | 791fc3f | 2020-04-07 00:19:46 -0700 | [diff] [blame] | 914 | xd->cdef_transmitted[index] = true; |
Steinar Midtskogen | 6c24b02 | 2017-09-15 09:46:39 +0200 | [diff] [blame] | 915 | } |
Steinar Midtskogen | 6c24b02 | 2017-09-15 09:46:39 +0200 | [diff] [blame] | 916 | } |
| 917 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 918 | static AOM_INLINE void write_inter_segment_id( |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 919 | AV1_COMP *cpi, MACROBLOCKD *const xd, aom_writer *w, |
| 920 | const struct segmentation *const seg, struct segmentation_probs *const segp, |
| 921 | int skip, int preskip) { |
David Turner | 760a2f4 | 2018-12-07 15:25:36 +0000 | [diff] [blame] | 922 | MB_MODE_INFO *const mbmi = xd->mi[0]; |
Rostislav Pehlivanov | 938710c | 2017-11-28 02:26:21 +0000 | [diff] [blame] | 923 | AV1_COMMON *const cm = &cpi->common; |
Hui Su | d62a63a | 2020-02-27 16:59:54 -0800 | [diff] [blame] | 924 | const int mi_row = xd->mi_row; |
| 925 | const int mi_col = xd->mi_col; |
Rostislav Pehlivanov | 938710c | 2017-11-28 02:26:21 +0000 | [diff] [blame] | 926 | |
| 927 | if (seg->update_map) { |
Rostislav Pehlivanov | 938710c | 2017-11-28 02:26:21 +0000 | [diff] [blame] | 928 | if (preskip) { |
Wan-Teh Chang | d1b9dcf | 2018-06-06 10:45:18 -0700 | [diff] [blame] | 929 | if (!seg->segid_preskip) return; |
Rostislav Pehlivanov | 938710c | 2017-11-28 02:26:21 +0000 | [diff] [blame] | 930 | } else { |
Wan-Teh Chang | d1b9dcf | 2018-06-06 10:45:18 -0700 | [diff] [blame] | 931 | if (seg->segid_preskip) return; |
Rostislav Pehlivanov | 938710c | 2017-11-28 02:26:21 +0000 | [diff] [blame] | 932 | if (skip) { |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 933 | write_segment_id(cpi, xd, mbmi, w, seg, segp, 1); |
David Turner | 760a2f4 | 2018-12-07 15:25:36 +0000 | [diff] [blame] | 934 | if (seg->temporal_update) mbmi->seg_id_predicted = 0; |
Rostislav Pehlivanov | 938710c | 2017-11-28 02:26:21 +0000 | [diff] [blame] | 935 | return; |
| 936 | } |
| 937 | } |
Rostislav Pehlivanov | 938710c | 2017-11-28 02:26:21 +0000 | [diff] [blame] | 938 | if (seg->temporal_update) { |
| 939 | const int pred_flag = mbmi->seg_id_predicted; |
| 940 | aom_cdf_prob *pred_cdf = av1_get_pred_cdf_seg_id(segp, xd); |
| 941 | aom_write_symbol(w, pred_flag, pred_cdf, 2); |
| 942 | if (!pred_flag) { |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 943 | write_segment_id(cpi, xd, mbmi, w, seg, segp, 0); |
Rostislav Pehlivanov | 938710c | 2017-11-28 02:26:21 +0000 | [diff] [blame] | 944 | } |
Rostislav Pehlivanov | 938710c | 2017-11-28 02:26:21 +0000 | [diff] [blame] | 945 | if (pred_flag) { |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 946 | set_spatial_segment_id(&cm->mi_params, cm->cur_frame->seg_map, |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 947 | mbmi->bsize, mi_row, mi_col, mbmi->segment_id); |
Rostislav Pehlivanov | 938710c | 2017-11-28 02:26:21 +0000 | [diff] [blame] | 948 | } |
Rostislav Pehlivanov | 938710c | 2017-11-28 02:26:21 +0000 | [diff] [blame] | 949 | } else { |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 950 | write_segment_id(cpi, xd, mbmi, w, seg, segp, 0); |
Rostislav Pehlivanov | 938710c | 2017-11-28 02:26:21 +0000 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | } |
| 954 | |
Urvang Joshi | 381ed66 | 2018-08-03 12:57:11 -0700 | [diff] [blame] | 955 | // If delta q is present, writes delta_q index. |
| 956 | // Also writes delta_q loop filter levels, if present. |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 957 | static AOM_INLINE void write_delta_q_params(AV1_COMMON *const cm, |
| 958 | MACROBLOCKD *const xd, int skip, |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 959 | aom_writer *w) { |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 960 | const DeltaQInfo *const delta_q_info = &cm->delta_q_info; |
| 961 | |
| 962 | if (delta_q_info->delta_q_present_flag) { |
Urvang Joshi | 381ed66 | 2018-08-03 12:57:11 -0700 | [diff] [blame] | 963 | const MB_MODE_INFO *const mbmi = xd->mi[0]; |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 964 | const BLOCK_SIZE bsize = mbmi->bsize; |
Urvang Joshi | 381ed66 | 2018-08-03 12:57:11 -0700 | [diff] [blame] | 965 | const int super_block_upper_left = |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 966 | ((xd->mi_row & (cm->seq_params->mib_size - 1)) == 0) && |
| 967 | ((xd->mi_col & (cm->seq_params->mib_size - 1)) == 0); |
Urvang Joshi | 381ed66 | 2018-08-03 12:57:11 -0700 | [diff] [blame] | 968 | |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 969 | if ((bsize != cm->seq_params->sb_size || skip == 0) && |
Imdad Sardharwalla | 4ec84ab | 2018-02-06 12:20:18 +0000 | [diff] [blame] | 970 | super_block_upper_left) { |
Wan-Teh Chang | 413f5ef | 2018-06-05 17:36:03 -0700 | [diff] [blame] | 971 | assert(mbmi->current_qindex > 0); |
Urvang Joshi | 381ed66 | 2018-08-03 12:57:11 -0700 | [diff] [blame] | 972 | const int reduced_delta_qindex = |
Urvang Joshi | acad1ca | 2020-04-27 17:03:25 -0700 | [diff] [blame] | 973 | (mbmi->current_qindex - xd->current_base_qindex) / |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 974 | delta_q_info->delta_q_res; |
Yaowu Xu | 6567c4a | 2018-03-19 21:39:31 -0700 | [diff] [blame] | 975 | write_delta_qindex(xd, reduced_delta_qindex, w); |
Urvang Joshi | acad1ca | 2020-04-27 17:03:25 -0700 | [diff] [blame] | 976 | xd->current_base_qindex = mbmi->current_qindex; |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 977 | if (delta_q_info->delta_lf_present_flag) { |
| 978 | if (delta_q_info->delta_lf_multi) { |
Imdad Sardharwalla | f74b4ab | 2018-02-20 17:22:42 +0000 | [diff] [blame] | 979 | const int frame_lf_count = |
| 980 | av1_num_planes(cm) > 1 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2; |
| 981 | for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id) { |
Cheng Chen | 880166a | 2017-10-02 17:48:48 -0700 | [diff] [blame] | 982 | int reduced_delta_lflevel = |
Wan-Teh Chang | 413f5ef | 2018-06-05 17:36:03 -0700 | [diff] [blame] | 983 | (mbmi->delta_lf[lf_id] - xd->delta_lf[lf_id]) / |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 984 | delta_q_info->delta_lf_res; |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 985 | write_delta_lflevel(cm, xd, lf_id, reduced_delta_lflevel, 1, w); |
Wan-Teh Chang | 413f5ef | 2018-06-05 17:36:03 -0700 | [diff] [blame] | 986 | xd->delta_lf[lf_id] = mbmi->delta_lf[lf_id]; |
Cheng Chen | 880166a | 2017-10-02 17:48:48 -0700 | [diff] [blame] | 987 | } |
| 988 | } else { |
Cheng Chen | a97394f | 2017-09-27 15:05:14 -0700 | [diff] [blame] | 989 | int reduced_delta_lflevel = |
Wan-Teh Chang | 413f5ef | 2018-06-05 17:36:03 -0700 | [diff] [blame] | 990 | (mbmi->delta_lf_from_base - xd->delta_lf_from_base) / |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 991 | delta_q_info->delta_lf_res; |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 992 | write_delta_lflevel(cm, xd, -1, reduced_delta_lflevel, 0, w); |
Wan-Teh Chang | 413f5ef | 2018-06-05 17:36:03 -0700 | [diff] [blame] | 993 | xd->delta_lf_from_base = mbmi->delta_lf_from_base; |
Cheng Chen | a97394f | 2017-09-27 15:05:14 -0700 | [diff] [blame] | 994 | } |
| 995 | } |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 996 | } |
| 997 | } |
Urvang Joshi | 381ed66 | 2018-08-03 12:57:11 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1000 | static AOM_INLINE void write_intra_prediction_modes(const AV1_COMMON *cm, |
| 1001 | MACROBLOCKD *const xd, |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 1002 | int is_keyframe, |
| 1003 | aom_writer *w) { |
Urvang Joshi | 76e8093 | 2018-08-03 14:36:11 -0700 | [diff] [blame] | 1004 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
| 1005 | const MB_MODE_INFO *const mbmi = xd->mi[0]; |
| 1006 | const PREDICTION_MODE mode = mbmi->mode; |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 1007 | const BLOCK_SIZE bsize = mbmi->bsize; |
Urvang Joshi | 76e8093 | 2018-08-03 14:36:11 -0700 | [diff] [blame] | 1008 | |
| 1009 | // Y mode. |
| 1010 | if (is_keyframe) { |
| 1011 | const MB_MODE_INFO *const above_mi = xd->above_mbmi; |
| 1012 | const MB_MODE_INFO *const left_mi = xd->left_mbmi; |
Urvang Joshi | c1437cd | 2018-08-03 14:42:49 -0700 | [diff] [blame] | 1013 | write_intra_y_mode_kf(ec_ctx, mbmi, above_mi, left_mi, mode, w); |
Urvang Joshi | 76e8093 | 2018-08-03 14:36:11 -0700 | [diff] [blame] | 1014 | } else { |
Urvang Joshi | c1437cd | 2018-08-03 14:42:49 -0700 | [diff] [blame] | 1015 | write_intra_y_mode_nonkf(ec_ctx, bsize, mode, w); |
Urvang Joshi | 76e8093 | 2018-08-03 14:36:11 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | // Y angle delta. |
| 1019 | const int use_angle_delta = av1_use_angle_delta(bsize); |
| 1020 | if (use_angle_delta && av1_is_directional_mode(mode)) { |
| 1021 | write_angle_delta(w, mbmi->angle_delta[PLANE_TYPE_Y], |
| 1022 | ec_ctx->angle_delta_cdf[mode - V_PRED]); |
| 1023 | } |
| 1024 | |
| 1025 | // UV mode and UV angle delta. |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 1026 | if (!cm->seq_params->monochrome && xd->is_chroma_ref) { |
Urvang Joshi | 76e8093 | 2018-08-03 14:36:11 -0700 | [diff] [blame] | 1027 | const UV_PREDICTION_MODE uv_mode = mbmi->uv_mode; |
| 1028 | write_intra_uv_mode(ec_ctx, uv_mode, mode, is_cfl_allowed(xd), w); |
| 1029 | if (uv_mode == UV_CFL_PRED) |
| 1030 | write_cfl_alphas(ec_ctx, mbmi->cfl_alpha_idx, mbmi->cfl_alpha_signs, w); |
Wan-Teh Chang | 7dd4cb6 | 2023-10-16 11:38:23 -0700 | [diff] [blame] | 1031 | const PREDICTION_MODE intra_mode = get_uv_mode(uv_mode); |
| 1032 | if (use_angle_delta && av1_is_directional_mode(intra_mode)) { |
Urvang Joshi | 76e8093 | 2018-08-03 14:36:11 -0700 | [diff] [blame] | 1033 | write_angle_delta(w, mbmi->angle_delta[PLANE_TYPE_UV], |
Wan-Teh Chang | 7dd4cb6 | 2023-10-16 11:38:23 -0700 | [diff] [blame] | 1034 | ec_ctx->angle_delta_cdf[intra_mode - V_PRED]); |
Urvang Joshi | 76e8093 | 2018-08-03 14:36:11 -0700 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | // Palette. |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 1039 | if (av1_allow_palette(cm->features.allow_screen_content_tools, bsize)) { |
Hui Su | d62a63a | 2020-02-27 16:59:54 -0800 | [diff] [blame] | 1040 | write_palette_mode_info(cm, xd, mbmi, w); |
Urvang Joshi | 76e8093 | 2018-08-03 14:36:11 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | // Filter intra. |
| 1044 | write_filter_intra_mode_info(cm, xd, mbmi, w); |
| 1045 | } |
| 1046 | |
Remya | 73ae2ba | 2019-08-12 19:19:40 +0530 | [diff] [blame] | 1047 | static INLINE int16_t mode_context_analyzer( |
Remya | 0cce44c | 2019-08-16 11:57:24 +0530 | [diff] [blame] | 1048 | const int16_t mode_context, const MV_REFERENCE_FRAME *const rf) { |
| 1049 | if (rf[1] <= INTRA_FRAME) return mode_context; |
Remya | 73ae2ba | 2019-08-12 19:19:40 +0530 | [diff] [blame] | 1050 | |
Remya | 0cce44c | 2019-08-16 11:57:24 +0530 | [diff] [blame] | 1051 | const int16_t newmv_ctx = mode_context & NEWMV_CTX_MASK; |
| 1052 | const int16_t refmv_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK; |
Remya | 73ae2ba | 2019-08-12 19:19:40 +0530 | [diff] [blame] | 1053 | |
| 1054 | const int16_t comp_ctx = compound_mode_ctx_map[refmv_ctx >> 1][AOMMIN( |
| 1055 | newmv_ctx, COMP_NEWMV_CTXS - 1)]; |
| 1056 | return comp_ctx; |
| 1057 | } |
| 1058 | |
Remya | 0cce44c | 2019-08-16 11:57:24 +0530 | [diff] [blame] | 1059 | static INLINE int_mv get_ref_mv_from_stack( |
| 1060 | int ref_idx, const MV_REFERENCE_FRAME *ref_frame, int ref_mv_idx, |
| 1061 | const MB_MODE_INFO_EXT_FRAME *mbmi_ext_frame) { |
Remya | 73ae2ba | 2019-08-12 19:19:40 +0530 | [diff] [blame] | 1062 | const int8_t ref_frame_type = av1_ref_frame_type(ref_frame); |
Remya | 0cce44c | 2019-08-16 11:57:24 +0530 | [diff] [blame] | 1063 | const CANDIDATE_MV *curr_ref_mv_stack = mbmi_ext_frame->ref_mv_stack; |
Remya | 73ae2ba | 2019-08-12 19:19:40 +0530 | [diff] [blame] | 1064 | |
| 1065 | if (ref_frame[1] > INTRA_FRAME) { |
| 1066 | assert(ref_idx == 0 || ref_idx == 1); |
| 1067 | return ref_idx ? curr_ref_mv_stack[ref_mv_idx].comp_mv |
| 1068 | : curr_ref_mv_stack[ref_mv_idx].this_mv; |
| 1069 | } |
| 1070 | |
| 1071 | assert(ref_idx == 0); |
Remya | 0cce44c | 2019-08-16 11:57:24 +0530 | [diff] [blame] | 1072 | return ref_mv_idx < mbmi_ext_frame->ref_mv_count |
Remya | 73ae2ba | 2019-08-12 19:19:40 +0530 | [diff] [blame] | 1073 | ? curr_ref_mv_stack[ref_mv_idx].this_mv |
Remya | 0cce44c | 2019-08-16 11:57:24 +0530 | [diff] [blame] | 1074 | : mbmi_ext_frame->global_mvs[ref_frame_type]; |
Remya | 73ae2ba | 2019-08-12 19:19:40 +0530 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | static INLINE int_mv get_ref_mv(const MACROBLOCK *x, int ref_idx) { |
| 1078 | const MACROBLOCKD *xd = &x->e_mbd; |
| 1079 | const MB_MODE_INFO *mbmi = xd->mi[0]; |
| 1080 | int ref_mv_idx = mbmi->ref_mv_idx; |
| 1081 | if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV) { |
| 1082 | assert(has_second_ref(mbmi)); |
| 1083 | ref_mv_idx += 1; |
| 1084 | } |
| 1085 | return get_ref_mv_from_stack(ref_idx, mbmi->ref_frame, ref_mv_idx, |
Remya | 0cce44c | 2019-08-16 11:57:24 +0530 | [diff] [blame] | 1086 | x->mbmi_ext_frame); |
Remya | 73ae2ba | 2019-08-12 19:19:40 +0530 | [diff] [blame] | 1087 | } |
| 1088 | |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1089 | static AOM_INLINE void pack_inter_mode_mvs(AV1_COMP *cpi, ThreadData *const td, |
| 1090 | aom_writer *w) { |
Urvang Joshi | 381ed66 | 2018-08-03 12:57:11 -0700 | [diff] [blame] | 1091 | AV1_COMMON *const cm = &cpi->common; |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1092 | MACROBLOCK *const x = &td->mb; |
Urvang Joshi | 381ed66 | 2018-08-03 12:57:11 -0700 | [diff] [blame] | 1093 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1094 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
| 1095 | const struct segmentation *const seg = &cm->seg; |
| 1096 | struct segmentation_probs *const segp = &ec_ctx->seg; |
| 1097 | const MB_MODE_INFO *const mbmi = xd->mi[0]; |
Remya | 0cce44c | 2019-08-16 11:57:24 +0530 | [diff] [blame] | 1098 | const MB_MODE_INFO_EXT_FRAME *const mbmi_ext_frame = x->mbmi_ext_frame; |
Urvang Joshi | 381ed66 | 2018-08-03 12:57:11 -0700 | [diff] [blame] | 1099 | const PREDICTION_MODE mode = mbmi->mode; |
chiyotsai | d730cef | 2022-10-26 13:58:20 -0700 | [diff] [blame] | 1100 | const uint8_t segment_id = mbmi->segment_id; |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 1101 | const BLOCK_SIZE bsize = mbmi->bsize; |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 1102 | const int allow_hp = cm->features.allow_high_precision_mv; |
Urvang Joshi | 381ed66 | 2018-08-03 12:57:11 -0700 | [diff] [blame] | 1103 | const int is_inter = is_inter_block(mbmi); |
| 1104 | const int is_compound = has_second_ref(mbmi); |
| 1105 | int ref; |
| 1106 | |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1107 | write_inter_segment_id(cpi, xd, w, seg, segp, 0, 1); |
Urvang Joshi | 381ed66 | 2018-08-03 12:57:11 -0700 | [diff] [blame] | 1108 | |
| 1109 | write_skip_mode(cm, xd, segment_id, mbmi, w); |
| 1110 | |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 1111 | assert(IMPLIES(mbmi->skip_mode, mbmi->skip_txfm)); |
Urvang Joshi | 381ed66 | 2018-08-03 12:57:11 -0700 | [diff] [blame] | 1112 | const int skip = |
| 1113 | mbmi->skip_mode ? 1 : write_skip(cm, xd, segment_id, mbmi, w); |
| 1114 | |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1115 | write_inter_segment_id(cpi, xd, w, seg, segp, skip, 0); |
Urvang Joshi | 381ed66 | 2018-08-03 12:57:11 -0700 | [diff] [blame] | 1116 | |
Hui Su | d62a63a | 2020-02-27 16:59:54 -0800 | [diff] [blame] | 1117 | write_cdef(cm, xd, w, skip); |
Urvang Joshi | 381ed66 | 2018-08-03 12:57:11 -0700 | [diff] [blame] | 1118 | |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1119 | write_delta_q_params(cm, xd, skip, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1120 | |
Zoe Liu | 93c3502 | 2018-02-27 17:15:13 -0800 | [diff] [blame] | 1121 | if (!mbmi->skip_mode) write_is_inter(cm, xd, mbmi->segment_id, w, is_inter); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1122 | |
Zoe Liu | 5664419 | 2017-12-19 13:16:18 -0800 | [diff] [blame] | 1123 | if (mbmi->skip_mode) return; |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 1124 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1125 | if (!is_inter) { |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1126 | write_intra_prediction_modes(cm, xd, 0, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1127 | } else { |
Yaowu Xu | b0d0d00 | 2016-11-22 09:26:43 -0800 | [diff] [blame] | 1128 | int16_t mode_ctx; |
Zoe Liu | fa8bad1 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 1129 | |
| 1130 | av1_collect_neighbors_ref_counts(xd); |
| 1131 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1132 | write_ref_frames(cm, xd, w); |
| 1133 | |
Remya | 0cce44c | 2019-08-16 11:57:24 +0530 | [diff] [blame] | 1134 | mode_ctx = |
| 1135 | mode_context_analyzer(mbmi_ext_frame->mode_context, mbmi->ref_frame); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1136 | |
| 1137 | // If segment skip is not enabled code the mode. |
| 1138 | if (!segfeature_active(seg, segment_id, SEG_LVL_SKIP)) { |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1139 | if (is_inter_compound_mode(mode)) |
Yaowu Xu | 6567c4a | 2018-03-19 21:39:31 -0700 | [diff] [blame] | 1140 | write_inter_compound_mode(xd, w, mode, mode_ctx); |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1141 | else if (is_inter_singleref_mode(mode)) |
| 1142 | write_inter_mode(w, mode, ec_ctx, mode_ctx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1143 | |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 1144 | if (mode == NEWMV || mode == NEW_NEWMV || have_nearmv_in_inter_mode(mode)) |
Remya | 0cce44c | 2019-08-16 11:57:24 +0530 | [diff] [blame] | 1145 | write_drl_idx(ec_ctx, mbmi, mbmi_ext_frame, w); |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1146 | else |
| 1147 | assert(mbmi->ref_mv_idx == 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1150 | if (mode == NEWMV || mode == NEW_NEWMV) { |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1151 | for (ref = 0; ref < 1 + is_compound; ++ref) { |
Jingning Han | f050fc1 | 2018-03-09 14:53:33 -0800 | [diff] [blame] | 1152 | nmv_context *nmvc = &ec_ctx->nmvc; |
Remya | 73ae2ba | 2019-08-12 19:19:40 +0530 | [diff] [blame] | 1153 | const int_mv ref_mv = get_ref_mv(x, ref); |
Cherma Rajan A | d0f59a7 | 2021-04-13 12:43:54 +0530 | [diff] [blame] | 1154 | av1_encode_mv(cpi, w, td, &mbmi->mv[ref].as_mv, &ref_mv.as_mv, nmvc, |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1155 | allow_hp); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1156 | } |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1157 | } else if (mode == NEAREST_NEWMV || mode == NEAR_NEWMV) { |
Jingning Han | f050fc1 | 2018-03-09 14:53:33 -0800 | [diff] [blame] | 1158 | nmv_context *nmvc = &ec_ctx->nmvc; |
Remya | 73ae2ba | 2019-08-12 19:19:40 +0530 | [diff] [blame] | 1159 | const int_mv ref_mv = get_ref_mv(x, 1); |
Cherma Rajan A | d0f59a7 | 2021-04-13 12:43:54 +0530 | [diff] [blame] | 1160 | av1_encode_mv(cpi, w, td, &mbmi->mv[1].as_mv, &ref_mv.as_mv, nmvc, |
| 1161 | allow_hp); |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1162 | } else if (mode == NEW_NEARESTMV || mode == NEW_NEARMV) { |
Jingning Han | f050fc1 | 2018-03-09 14:53:33 -0800 | [diff] [blame] | 1163 | nmv_context *nmvc = &ec_ctx->nmvc; |
Remya | 73ae2ba | 2019-08-12 19:19:40 +0530 | [diff] [blame] | 1164 | const int_mv ref_mv = get_ref_mv(x, 0); |
Cherma Rajan A | d0f59a7 | 2021-04-13 12:43:54 +0530 | [diff] [blame] | 1165 | av1_encode_mv(cpi, w, td, &mbmi->mv[0].as_mv, &ref_mv.as_mv, nmvc, |
| 1166 | allow_hp); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 1169 | if (cpi->common.current_frame.reference_mode != COMPOUND_REFERENCE && |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 1170 | cpi->common.seq_params->enable_interintra_compound && |
Debargha Mukherjee | 97095fb | 2018-03-26 07:51:48 -0700 | [diff] [blame] | 1171 | is_interintra_allowed(mbmi)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1172 | const int interintra = mbmi->ref_frame[1] == INTRA_FRAME; |
| 1173 | const int bsize_group = size_group_lookup[bsize]; |
Thomas Davies | cff9171 | 2017-07-07 11:49:55 +0100 | [diff] [blame] | 1174 | aom_write_symbol(w, interintra, ec_ctx->interintra_cdf[bsize_group], 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1175 | if (interintra) { |
Thomas Davies | 299ff04 | 2017-06-27 13:41:59 +0100 | [diff] [blame] | 1176 | aom_write_symbol(w, mbmi->interintra_mode, |
| 1177 | ec_ctx->interintra_mode_cdf[bsize_group], |
| 1178 | INTERINTRA_MODES); |
Hui Su | c88f974 | 2019-12-23 13:46:54 -0800 | [diff] [blame] | 1179 | if (av1_is_wedge_used(bsize)) { |
Thomas Davies | cff9171 | 2017-07-07 11:49:55 +0100 | [diff] [blame] | 1180 | aom_write_symbol(w, mbmi->use_wedge_interintra, |
| 1181 | ec_ctx->wedge_interintra_cdf[bsize], 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1182 | if (mbmi->use_wedge_interintra) { |
Yue Chen | 73335fa | 2017-12-20 23:33:41 -0800 | [diff] [blame] | 1183 | aom_write_symbol(w, mbmi->interintra_wedge_index, |
Hui Su | c88f974 | 2019-12-23 13:46:54 -0800 | [diff] [blame] | 1184 | ec_ctx->wedge_idx_cdf[bsize], MAX_WEDGE_TYPES); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | } |
| 1188 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1189 | |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 1190 | if (mbmi->ref_frame[1] != INTRA_FRAME) write_motion_mode(cm, xd, mbmi, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1191 | |
Cheng Chen | 33a13d9 | 2017-11-28 16:49:59 -0800 | [diff] [blame] | 1192 | // First write idx to indicate current compound inter prediction mode group |
Debargha Mukherjee | f90004a | 2018-12-20 13:35:06 -0800 | [diff] [blame] | 1193 | // Group A (0): dist_wtd_comp, compound_average |
Sarah Parker | 5b4df2b | 2018-04-02 14:48:25 -0700 | [diff] [blame] | 1194 | // Group B (1): interintra, compound_diffwtd, wedge |
Cheng Chen | 33a13d9 | 2017-11-28 16:49:59 -0800 | [diff] [blame] | 1195 | if (has_second_ref(mbmi)) { |
Debargha Mukherjee | 97095fb | 2018-03-26 07:51:48 -0700 | [diff] [blame] | 1196 | const int masked_compound_used = is_any_masked_compound_used(bsize) && |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 1197 | cm->seq_params->enable_masked_compound; |
Cheng Chen | 5a88172 | 2017-11-30 17:05:10 -0800 | [diff] [blame] | 1198 | |
Zoe Liu | 5f11e91 | 2017-12-05 23:23:56 -0800 | [diff] [blame] | 1199 | if (masked_compound_used) { |
Cheng Chen | 5a88172 | 2017-11-30 17:05:10 -0800 | [diff] [blame] | 1200 | const int ctx_comp_group_idx = get_comp_group_idx_context(xd); |
| 1201 | aom_write_symbol(w, mbmi->comp_group_idx, |
| 1202 | ec_ctx->comp_group_idx_cdf[ctx_comp_group_idx], 2); |
Zoe Liu | 5f11e91 | 2017-12-05 23:23:56 -0800 | [diff] [blame] | 1203 | } else { |
| 1204 | assert(mbmi->comp_group_idx == 0); |
Cheng Chen | 5a88172 | 2017-11-30 17:05:10 -0800 | [diff] [blame] | 1205 | } |
Cheng Chen | 33a13d9 | 2017-11-28 16:49:59 -0800 | [diff] [blame] | 1206 | |
| 1207 | if (mbmi->comp_group_idx == 0) { |
| 1208 | if (mbmi->compound_idx) |
Peng Bin | 42b7770 | 2018-06-01 11:09:54 +0800 | [diff] [blame] | 1209 | assert(mbmi->interinter_comp.type == COMPOUND_AVERAGE); |
Cheng Chen | 33a13d9 | 2017-11-28 16:49:59 -0800 | [diff] [blame] | 1210 | |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 1211 | if (cm->seq_params->order_hint_info.enable_dist_wtd_comp) { |
David Barker | e21f4d9 | 2018-02-26 16:37:24 +0000 | [diff] [blame] | 1212 | const int comp_index_ctx = get_comp_index_context(cm, xd); |
| 1213 | aom_write_symbol(w, mbmi->compound_idx, |
| 1214 | ec_ctx->compound_index_cdf[comp_index_ctx], 2); |
| 1215 | } else { |
| 1216 | assert(mbmi->compound_idx == 1); |
| 1217 | } |
Cheng Chen | 33a13d9 | 2017-11-28 16:49:59 -0800 | [diff] [blame] | 1218 | } else { |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 1219 | assert(cpi->common.current_frame.reference_mode != SINGLE_REFERENCE && |
Zoe Liu | 5f11e91 | 2017-12-05 23:23:56 -0800 | [diff] [blame] | 1220 | is_inter_compound_mode(mbmi->mode) && |
| 1221 | mbmi->motion_mode == SIMPLE_TRANSLATION); |
| 1222 | assert(masked_compound_used); |
Sarah Parker | 5b4df2b | 2018-04-02 14:48:25 -0700 | [diff] [blame] | 1223 | // compound_diffwtd, wedge |
Peng Bin | 42b7770 | 2018-06-01 11:09:54 +0800 | [diff] [blame] | 1224 | assert(mbmi->interinter_comp.type == COMPOUND_WEDGE || |
| 1225 | mbmi->interinter_comp.type == COMPOUND_DIFFWTD); |
Cheng Chen | 33a13d9 | 2017-11-28 16:49:59 -0800 | [diff] [blame] | 1226 | |
Zoe Liu | 5f11e91 | 2017-12-05 23:23:56 -0800 | [diff] [blame] | 1227 | if (is_interinter_compound_used(COMPOUND_WEDGE, bsize)) |
Debargha Mukherjee | 54eabb5 | 2019-02-01 16:54:33 -0800 | [diff] [blame] | 1228 | aom_write_symbol(w, mbmi->interinter_comp.type - COMPOUND_WEDGE, |
Zoe Liu | 5f11e91 | 2017-12-05 23:23:56 -0800 | [diff] [blame] | 1229 | ec_ctx->compound_type_cdf[bsize], |
Debargha Mukherjee | 54eabb5 | 2019-02-01 16:54:33 -0800 | [diff] [blame] | 1230 | MASKED_COMPOUND_TYPES); |
Zoe Liu | 5f11e91 | 2017-12-05 23:23:56 -0800 | [diff] [blame] | 1231 | |
Peng Bin | 42b7770 | 2018-06-01 11:09:54 +0800 | [diff] [blame] | 1232 | if (mbmi->interinter_comp.type == COMPOUND_WEDGE) { |
Zoe Liu | 5f11e91 | 2017-12-05 23:23:56 -0800 | [diff] [blame] | 1233 | assert(is_interinter_compound_used(COMPOUND_WEDGE, bsize)); |
Peng Bin | 42b7770 | 2018-06-01 11:09:54 +0800 | [diff] [blame] | 1234 | aom_write_symbol(w, mbmi->interinter_comp.wedge_index, |
Hui Su | c88f974 | 2019-12-23 13:46:54 -0800 | [diff] [blame] | 1235 | ec_ctx->wedge_idx_cdf[bsize], MAX_WEDGE_TYPES); |
Peng Bin | 42b7770 | 2018-06-01 11:09:54 +0800 | [diff] [blame] | 1236 | aom_write_bit(w, mbmi->interinter_comp.wedge_sign); |
Zoe Liu | 5f11e91 | 2017-12-05 23:23:56 -0800 | [diff] [blame] | 1237 | } else { |
Peng Bin | 42b7770 | 2018-06-01 11:09:54 +0800 | [diff] [blame] | 1238 | assert(mbmi->interinter_comp.type == COMPOUND_DIFFWTD); |
| 1239 | aom_write_literal(w, mbmi->interinter_comp.mask_type, |
| 1240 | MAX_DIFFWTD_MASK_BITS); |
Cheng Chen | 33a13d9 | 2017-11-28 16:49:59 -0800 | [diff] [blame] | 1241 | } |
| 1242 | } |
| 1243 | } |
Cherma Rajan A | d0f59a7 | 2021-04-13 12:43:54 +0530 | [diff] [blame] | 1244 | write_mb_interp_filter(cm, td, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1245 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1246 | } |
| 1247 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 1248 | static AOM_INLINE void write_intrabc_info( |
| 1249 | MACROBLOCKD *xd, const MB_MODE_INFO_EXT_FRAME *mbmi_ext_frame, |
| 1250 | aom_writer *w) { |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 1251 | const MB_MODE_INFO *const mbmi = xd->mi[0]; |
Hui Su | c2232cf | 2017-10-11 17:32:56 -0700 | [diff] [blame] | 1252 | int use_intrabc = is_intrabc_block(mbmi); |
| 1253 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
| 1254 | aom_write_symbol(w, use_intrabc, ec_ctx->intrabc_cdf, 2); |
| 1255 | if (use_intrabc) { |
| 1256 | assert(mbmi->mode == DC_PRED); |
| 1257 | assert(mbmi->uv_mode == UV_DC_PRED); |
Hui Su | 1fbe32a | 2018-02-26 21:44:54 -0800 | [diff] [blame] | 1258 | assert(mbmi->motion_mode == SIMPLE_TRANSLATION); |
Remya | 0cce44c | 2019-08-16 11:57:24 +0530 | [diff] [blame] | 1259 | int_mv dv_ref = mbmi_ext_frame->ref_mv_stack[0].this_mv; |
Hui Su | c2232cf | 2017-10-11 17:32:56 -0700 | [diff] [blame] | 1260 | av1_encode_dv(w, &mbmi->mv[0].as_mv, &dv_ref.as_mv, &ec_ctx->ndvc); |
Hui Su | c2232cf | 2017-10-11 17:32:56 -0700 | [diff] [blame] | 1261 | } |
| 1262 | } |
Hui Su | c2232cf | 2017-10-11 17:32:56 -0700 | [diff] [blame] | 1263 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 1264 | static AOM_INLINE void write_mb_modes_kf( |
| 1265 | AV1_COMP *cpi, MACROBLOCKD *xd, |
Hui Su | d62a63a | 2020-02-27 16:59:54 -0800 | [diff] [blame] | 1266 | const MB_MODE_INFO_EXT_FRAME *mbmi_ext_frame, aom_writer *w) { |
Rostislav Pehlivanov | 938710c | 2017-11-28 02:26:21 +0000 | [diff] [blame] | 1267 | AV1_COMMON *const cm = &cpi->common; |
Thomas Davies | 9f5cedd | 2017-07-10 09:20:32 +0100 | [diff] [blame] | 1268 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1269 | const struct segmentation *const seg = &cm->seg; |
Thomas Davies | 9f5cedd | 2017-07-10 09:20:32 +0100 | [diff] [blame] | 1270 | struct segmentation_probs *const segp = &ec_ctx->seg; |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 1271 | const MB_MODE_INFO *const mbmi = xd->mi[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1272 | |
Wan-Teh Chang | d1b9dcf | 2018-06-06 10:45:18 -0700 | [diff] [blame] | 1273 | if (seg->segid_preskip && seg->update_map) |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1274 | write_segment_id(cpi, xd, mbmi, w, seg, segp, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1275 | |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 1276 | const int skip = write_skip(cm, xd, mbmi->segment_id, mbmi, w); |
Steinar Midtskogen | 6c24b02 | 2017-09-15 09:46:39 +0200 | [diff] [blame] | 1277 | |
Wan-Teh Chang | d1b9dcf | 2018-06-06 10:45:18 -0700 | [diff] [blame] | 1278 | if (!seg->segid_preskip && seg->update_map) |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1279 | write_segment_id(cpi, xd, mbmi, w, seg, segp, skip); |
Steinar Midtskogen | 6c24b02 | 2017-09-15 09:46:39 +0200 | [diff] [blame] | 1280 | |
Hui Su | d62a63a | 2020-02-27 16:59:54 -0800 | [diff] [blame] | 1281 | write_cdef(cm, xd, w, skip); |
Steinar Midtskogen | 6c24b02 | 2017-09-15 09:46:39 +0200 | [diff] [blame] | 1282 | |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1283 | write_delta_q_params(cm, xd, skip, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1284 | |
Hui Su | eb2fd5c | 2017-12-15 14:38:01 -0800 | [diff] [blame] | 1285 | if (av1_allow_intrabc(cm)) { |
Remya | 0cce44c | 2019-08-16 11:57:24 +0530 | [diff] [blame] | 1286 | write_intrabc_info(xd, mbmi_ext_frame, w); |
Hui Su | c2232cf | 2017-10-11 17:32:56 -0700 | [diff] [blame] | 1287 | if (is_intrabc_block(mbmi)) return; |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 1288 | } |
Hui Su | c2232cf | 2017-10-11 17:32:56 -0700 | [diff] [blame] | 1289 | |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1290 | write_intra_prediction_modes(cm, xd, 1, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
Angie Chiang | d402282 | 2016-11-02 18:30:25 -0700 | [diff] [blame] | 1293 | #if CONFIG_RD_DEBUG |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 1294 | static AOM_INLINE void dump_mode_info(MB_MODE_INFO *mi) { |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 1295 | printf("\nmi->mi_row == %d\n", mi->mi_row); |
| 1296 | printf("&& mi->mi_col == %d\n", mi->mi_col); |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 1297 | printf("&& mi->bsize == %d\n", mi->bsize); |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 1298 | printf("&& mi->tx_size == %d\n", mi->tx_size); |
| 1299 | printf("&& mi->mode == %d\n", mi->mode); |
Angie Chiang | d402282 | 2016-11-02 18:30:25 -0700 | [diff] [blame] | 1300 | } |
Debargha Mukherjee | 514b52b | 2019-01-02 16:34:28 -0800 | [diff] [blame] | 1301 | |
Angie Chiang | d02001d | 2016-11-06 15:31:49 -0800 | [diff] [blame] | 1302 | static int rd_token_stats_mismatch(RD_STATS *rd_stats, TOKEN_STATS *token_stats, |
| 1303 | int plane) { |
| 1304 | if (rd_stats->txb_coeff_cost[plane] != token_stats->cost) { |
Angie Chiang | d02001d | 2016-11-06 15:31:49 -0800 | [diff] [blame] | 1305 | printf("\nplane %d rd_stats->txb_coeff_cost %d token_stats->cost %d\n", |
| 1306 | plane, rd_stats->txb_coeff_cost[plane], token_stats->cost); |
Angie Chiang | d02001d | 2016-11-06 15:31:49 -0800 | [diff] [blame] | 1307 | return 1; |
| 1308 | } |
| 1309 | return 0; |
| 1310 | } |
Angie Chiang | d402282 | 2016-11-02 18:30:25 -0700 | [diff] [blame] | 1311 | #endif |
| 1312 | |
Di Chen | 5658662 | 2017-06-09 13:49:44 -0700 | [diff] [blame] | 1313 | #if ENC_MISMATCH_DEBUG |
Vishesh | d131791 | 2020-04-07 14:39:44 +0530 | [diff] [blame] | 1314 | static AOM_INLINE void enc_dump_logs( |
| 1315 | const AV1_COMMON *const cm, |
| 1316 | const MBMIExtFrameBufferInfo *const mbmi_ext_info, int mi_row, int mi_col) { |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 1317 | const MB_MODE_INFO *const mbmi = *( |
| 1318 | cm->mi_params.mi_grid_base + (mi_row * cm->mi_params.mi_stride + mi_col)); |
Vishesh | d131791 | 2020-04-07 14:39:44 +0530 | [diff] [blame] | 1319 | const MB_MODE_INFO_EXT_FRAME *const mbmi_ext_frame = |
| 1320 | mbmi_ext_info->frame_base + get_mi_ext_idx(mi_row, mi_col, |
| 1321 | cm->mi_params.mi_alloc_bsize, |
| 1322 | mbmi_ext_info->stride); |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 1323 | if (is_inter_block(mbmi)) { |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 1324 | #define FRAME_TO_CHECK 11 |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 1325 | if (cm->current_frame.frame_number == FRAME_TO_CHECK && |
| 1326 | cm->show_frame == 1) { |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 1327 | const BLOCK_SIZE bsize = mbmi->bsize; |
Di Chen | 5658662 | 2017-06-09 13:49:44 -0700 | [diff] [blame] | 1328 | |
David Turner | 760a2f4 | 2018-12-07 15:25:36 +0000 | [diff] [blame] | 1329 | int_mv mv[2] = { 0 }; |
| 1330 | const int is_comp_ref = has_second_ref(mbmi); |
Di Chen | 5658662 | 2017-06-09 13:49:44 -0700 | [diff] [blame] | 1331 | |
David Turner | 760a2f4 | 2018-12-07 15:25:36 +0000 | [diff] [blame] | 1332 | for (int ref = 0; ref < 1 + is_comp_ref; ++ref) |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 1333 | mv[ref].as_mv = mbmi->mv[ref].as_mv; |
Di Chen | 5658662 | 2017-06-09 13:49:44 -0700 | [diff] [blame] | 1334 | |
| 1335 | if (!is_comp_ref) { |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 1336 | mv[1].as_int = 0; |
Di Chen | 5658662 | 2017-06-09 13:49:44 -0700 | [diff] [blame] | 1337 | } |
Di Chen | 5658662 | 2017-06-09 13:49:44 -0700 | [diff] [blame] | 1338 | |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 1339 | const int16_t mode_ctx = |
Remya | 0cce44c | 2019-08-16 11:57:24 +0530 | [diff] [blame] | 1340 | is_comp_ref ? 0 |
| 1341 | : mode_context_analyzer(mbmi_ext_frame->mode_context, |
| 1342 | mbmi->ref_frame); |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 1343 | |
Di Chen | 5658662 | 2017-06-09 13:49:44 -0700 | [diff] [blame] | 1344 | const int16_t newmv_ctx = mode_ctx & NEWMV_CTX_MASK; |
| 1345 | int16_t zeromv_ctx = -1; |
| 1346 | int16_t refmv_ctx = -1; |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 1347 | |
Di Chen | 5658662 | 2017-06-09 13:49:44 -0700 | [diff] [blame] | 1348 | if (mbmi->mode != NEWMV) { |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 1349 | zeromv_ctx = (mode_ctx >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK; |
Jingning Han | 59b1263 | 2018-02-12 10:44:52 -0800 | [diff] [blame] | 1350 | if (mbmi->mode != GLOBALMV) |
Di Chen | 5658662 | 2017-06-09 13:49:44 -0700 | [diff] [blame] | 1351 | refmv_ctx = (mode_ctx >> REFMV_OFFSET) & REFMV_CTX_MASK; |
Di Chen | 5658662 | 2017-06-09 13:49:44 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 1354 | printf( |
| 1355 | "=== ENCODER ===: " |
| 1356 | "Frame=%d, (mi_row,mi_col)=(%d,%d), skip_mode=%d, mode=%d, bsize=%d, " |
| 1357 | "show_frame=%d, mv[0]=(%d,%d), mv[1]=(%d,%d), ref[0]=%d, " |
| 1358 | "ref[1]=%d, motion_mode=%d, mode_ctx=%d, " |
| 1359 | "newmv_ctx=%d, zeromv_ctx=%d, refmv_ctx=%d, tx_size=%d\n", |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 1360 | cm->current_frame.frame_number, mi_row, mi_col, mbmi->skip_mode, |
| 1361 | mbmi->mode, bsize, cm->show_frame, mv[0].as_mv.row, mv[0].as_mv.col, |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 1362 | mv[1].as_mv.row, mv[1].as_mv.col, mbmi->ref_frame[0], |
| 1363 | mbmi->ref_frame[1], mbmi->motion_mode, mode_ctx, newmv_ctx, |
| 1364 | zeromv_ctx, refmv_ctx, mbmi->tx_size); |
Di Chen | 5658662 | 2017-06-09 13:49:44 -0700 | [diff] [blame] | 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | #endif // ENC_MISMATCH_DEBUG |
| 1369 | |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1370 | static AOM_INLINE void write_mbmi_b(AV1_COMP *cpi, ThreadData *const td, |
| 1371 | aom_writer *w) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1372 | AV1_COMMON *const cm = &cpi->common; |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1373 | MACROBLOCKD *const xd = &td->mb.e_mbd; |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 1374 | MB_MODE_INFO *m = xd->mi[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1375 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1376 | if (frame_is_intra_only(cm)) { |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1377 | write_mb_modes_kf(cpi, xd, td->mb.mbmi_ext_frame, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1378 | } else { |
Angie Chiang | 38edf68 | 2017-02-21 15:13:09 -0800 | [diff] [blame] | 1379 | // has_subpel_mv_component needs the ref frame buffers set up to look |
| 1380 | // up if they are scaled. has_subpel_mv_component is in turn needed by |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1381 | // write_switchable_interp_filter, which is called by pack_inter_mode_mvs. |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 1382 | set_ref_ptrs(cm, xd, m->ref_frame[0], m->ref_frame[1]); |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 1383 | |
Di Chen | 5658662 | 2017-06-09 13:49:44 -0700 | [diff] [blame] | 1384 | #if ENC_MISMATCH_DEBUG |
Vishesh | d131791 | 2020-04-07 14:39:44 +0530 | [diff] [blame] | 1385 | enc_dump_logs(cm, &cpi->mbmi_ext_info, xd->mi_row, xd->mi_col); |
Di Chen | 5658662 | 2017-06-09 13:49:44 -0700 | [diff] [blame] | 1386 | #endif // ENC_MISMATCH_DEBUG |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1387 | |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1388 | pack_inter_mode_mvs(cpi, td, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1389 | } |
Yue Chen | 64550b6 | 2017-01-12 12:18:22 -0800 | [diff] [blame] | 1390 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1391 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 1392 | static AOM_INLINE void write_inter_txb_coeff( |
| 1393 | AV1_COMMON *const cm, MACROBLOCK *const x, MB_MODE_INFO *const mbmi, |
Vishesh | 686aa77 | 2020-04-13 14:40:12 +0530 | [diff] [blame] | 1394 | aom_writer *w, const TokenExtra **tok, const TokenExtra *const tok_end, |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 1395 | TOKEN_STATS *token_stats, const int row, const int col, int *block, |
| 1396 | const int plane) { |
Jingning Han | e5e8f4d | 2017-11-20 20:11:04 -0800 | [diff] [blame] | 1397 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1398 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 1399 | const BLOCK_SIZE bsize = mbmi->bsize; |
kyslov | e7ff3b6 | 2019-04-05 14:15:03 -0700 | [diff] [blame] | 1400 | assert(bsize < BLOCK_SIZES_ALL); |
Hui Su | 56e838f | 2019-12-04 14:48:30 -0800 | [diff] [blame] | 1401 | const int ss_x = pd->subsampling_x; |
| 1402 | const int ss_y = pd->subsampling_y; |
Hui Su | aadb0b4 | 2019-12-06 15:56:47 -0800 | [diff] [blame] | 1403 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y); |
Hui Su | 56e838f | 2019-12-04 14:48:30 -0800 | [diff] [blame] | 1404 | assert(plane_bsize < BLOCK_SIZES_ALL); |
Urvang Joshi | 49c57d6 | 2018-05-03 11:37:38 -0700 | [diff] [blame] | 1405 | const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, plane_bsize, plane); |
Jingning Han | e5e8f4d | 2017-11-20 20:11:04 -0800 | [diff] [blame] | 1406 | const int step = |
| 1407 | tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size]; |
| 1408 | const int bkw = tx_size_wide_unit[max_tx_size]; |
| 1409 | const int bkh = tx_size_high_unit[max_tx_size]; |
Cheng Chen | 8ab1f44 | 2018-04-27 18:01:52 -0700 | [diff] [blame] | 1410 | const BLOCK_SIZE max_unit_bsize = |
Hui Su | 56e838f | 2019-12-04 14:48:30 -0800 | [diff] [blame] | 1411 | get_plane_block_size(BLOCK_64X64, ss_x, ss_y); |
Hui Su | adda587 | 2019-12-09 10:08:49 -0800 | [diff] [blame] | 1412 | const int num_4x4_w = mi_size_wide[plane_bsize]; |
| 1413 | const int num_4x4_h = mi_size_high[plane_bsize]; |
| 1414 | const int mu_blocks_wide = mi_size_wide[max_unit_bsize]; |
| 1415 | const int mu_blocks_high = mi_size_high[max_unit_bsize]; |
Hui Su | 56e838f | 2019-12-04 14:48:30 -0800 | [diff] [blame] | 1416 | const int unit_height = AOMMIN(mu_blocks_high + (row >> ss_y), num_4x4_h); |
| 1417 | const int unit_width = AOMMIN(mu_blocks_wide + (col >> ss_x), num_4x4_w); |
| 1418 | for (int blk_row = row >> ss_y; blk_row < unit_height; blk_row += bkh) { |
| 1419 | for (int blk_col = col >> ss_x; blk_col < unit_width; blk_col += bkw) { |
Sebastien Alaiwan | cad5ebc | 2018-02-20 16:18:20 +0100 | [diff] [blame] | 1420 | pack_txb_tokens(w, cm, x, tok, tok_end, xd, mbmi, plane, plane_bsize, |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 1421 | cm->seq_params->bit_depth, *block, blk_row, blk_col, |
Urvang Joshi | 20cf30e | 2018-07-19 02:33:58 -0700 | [diff] [blame] | 1422 | max_tx_size, token_stats); |
Jingning Han | e5e8f4d | 2017-11-20 20:11:04 -0800 | [diff] [blame] | 1423 | *block += step; |
| 1424 | } |
| 1425 | } |
| 1426 | } |
| 1427 | |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1428 | static AOM_INLINE void write_tokens_b(AV1_COMP *cpi, MACROBLOCK *const x, |
| 1429 | aom_writer *w, const TokenExtra **tok, |
Vishesh | 686aa77 | 2020-04-13 14:40:12 +0530 | [diff] [blame] | 1430 | const TokenExtra *const tok_end) { |
Yue Chen | 64550b6 | 2017-01-12 12:18:22 -0800 | [diff] [blame] | 1431 | AV1_COMMON *const cm = &cpi->common; |
Hui Su | c6b79f7 | 2019-04-17 16:44:33 -0700 | [diff] [blame] | 1432 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1433 | MB_MODE_INFO *const mbmi = xd->mi[0]; |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 1434 | const BLOCK_SIZE bsize = mbmi->bsize; |
Yue Chen | 64550b6 | 2017-01-12 12:18:22 -0800 | [diff] [blame] | 1435 | |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 1436 | assert(!mbmi->skip_txfm); |
Yue Chen | 64550b6 | 2017-01-12 12:18:22 -0800 | [diff] [blame] | 1437 | |
Hui Su | c6b79f7 | 2019-04-17 16:44:33 -0700 | [diff] [blame] | 1438 | const int is_inter = is_inter_block(mbmi); |
| 1439 | if (!is_inter) { |
angiebird | 320e228 | 2020-06-29 10:34:43 -0700 | [diff] [blame] | 1440 | av1_write_intra_coeffs_mb(cm, x, w, bsize); |
Hui Su | c6b79f7 | 2019-04-17 16:44:33 -0700 | [diff] [blame] | 1441 | } else { |
| 1442 | int block[MAX_MB_PLANE] = { 0 }; |
| 1443 | assert(bsize == get_plane_block_size(bsize, xd->plane[0].subsampling_x, |
| 1444 | xd->plane[0].subsampling_y)); |
Hui Su | adda587 | 2019-12-09 10:08:49 -0800 | [diff] [blame] | 1445 | const int num_4x4_w = mi_size_wide[bsize]; |
| 1446 | const int num_4x4_h = mi_size_high[bsize]; |
Hui Su | c6b79f7 | 2019-04-17 16:44:33 -0700 | [diff] [blame] | 1447 | TOKEN_STATS token_stats; |
| 1448 | init_token_stats(&token_stats); |
Yue Chen | 64550b6 | 2017-01-12 12:18:22 -0800 | [diff] [blame] | 1449 | |
Hui Su | c6b79f7 | 2019-04-17 16:44:33 -0700 | [diff] [blame] | 1450 | const BLOCK_SIZE max_unit_bsize = BLOCK_64X64; |
| 1451 | assert(max_unit_bsize == get_plane_block_size(BLOCK_64X64, |
| 1452 | xd->plane[0].subsampling_x, |
| 1453 | xd->plane[0].subsampling_y)); |
Hui Su | adda587 | 2019-12-09 10:08:49 -0800 | [diff] [blame] | 1454 | int mu_blocks_wide = mi_size_wide[max_unit_bsize]; |
| 1455 | int mu_blocks_high = mi_size_high[max_unit_bsize]; |
Hui Su | c6b79f7 | 2019-04-17 16:44:33 -0700 | [diff] [blame] | 1456 | mu_blocks_wide = AOMMIN(num_4x4_w, mu_blocks_wide); |
| 1457 | mu_blocks_high = AOMMIN(num_4x4_h, mu_blocks_high); |
Jingning Han | ad54a98 | 2018-01-12 14:40:29 -0800 | [diff] [blame] | 1458 | |
Hui Su | c6b79f7 | 2019-04-17 16:44:33 -0700 | [diff] [blame] | 1459 | const int num_planes = av1_num_planes(cm); |
| 1460 | for (int row = 0; row < num_4x4_h; row += mu_blocks_high) { |
| 1461 | for (int col = 0; col < num_4x4_w; col += mu_blocks_wide) { |
| 1462 | for (int plane = 0; plane < num_planes; ++plane) { |
Hui Su | 474e1e1 | 2020-02-27 15:46:36 -0800 | [diff] [blame] | 1463 | if (plane && !xd->is_chroma_ref) break; |
Hui Su | c6b79f7 | 2019-04-17 16:44:33 -0700 | [diff] [blame] | 1464 | write_inter_txb_coeff(cm, x, mbmi, w, tok, tok_end, &token_stats, row, |
| 1465 | col, &block[plane], plane); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1466 | } |
Debargha Mukherjee | 514b52b | 2019-01-02 16:34:28 -0800 | [diff] [blame] | 1467 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1468 | } |
Hui Su | c6b79f7 | 2019-04-17 16:44:33 -0700 | [diff] [blame] | 1469 | #if CONFIG_RD_DEBUG |
| 1470 | for (int plane = 0; plane < num_planes; ++plane) { |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 1471 | if (mbmi->bsize >= BLOCK_8X8 && |
Hui Su | c6b79f7 | 2019-04-17 16:44:33 -0700 | [diff] [blame] | 1472 | rd_token_stats_mismatch(&mbmi->rd_stats, &token_stats, plane)) { |
| 1473 | dump_mode_info(mbmi); |
| 1474 | assert(0); |
| 1475 | } |
| 1476 | } |
| 1477 | #endif // CONFIG_RD_DEBUG |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1478 | } |
| 1479 | } |
| 1480 | |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1481 | static AOM_INLINE void write_modes_b(AV1_COMP *cpi, ThreadData *const td, |
| 1482 | const TileInfo *const tile, aom_writer *w, |
| 1483 | const TokenExtra **tok, |
Vishesh | 686aa77 | 2020-04-13 14:40:12 +0530 | [diff] [blame] | 1484 | const TokenExtra *const tok_end, |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 1485 | int mi_row, int mi_col) { |
Hui Su | 0e48db9 | 2019-04-17 15:55:57 -0700 | [diff] [blame] | 1486 | const AV1_COMMON *cm = &cpi->common; |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 1487 | const CommonModeInfoParams *const mi_params = &cm->mi_params; |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1488 | MACROBLOCKD *xd = &td->mb.e_mbd; |
Jayasanker J | 4969e72 | 2020-09-18 20:36:15 +0530 | [diff] [blame] | 1489 | FRAME_CONTEXT *tile_ctx = xd->tile_ctx; |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 1490 | const int grid_idx = mi_row * mi_params->mi_stride + mi_col; |
| 1491 | xd->mi = mi_params->mi_grid_base + grid_idx; |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1492 | td->mb.mbmi_ext_frame = |
Vishesh | d131791 | 2020-04-07 14:39:44 +0530 | [diff] [blame] | 1493 | cpi->mbmi_ext_info.frame_base + |
| 1494 | get_mi_ext_idx(mi_row, mi_col, cm->mi_params.mi_alloc_bsize, |
| 1495 | cpi->mbmi_ext_info.stride); |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 1496 | xd->tx_type_map = mi_params->tx_type_map + grid_idx; |
| 1497 | xd->tx_type_map_stride = mi_params->mi_stride; |
Hui Su | c6b79f7 | 2019-04-17 16:44:33 -0700 | [diff] [blame] | 1498 | |
Hui Su | 0e48db9 | 2019-04-17 15:55:57 -0700 | [diff] [blame] | 1499 | const MB_MODE_INFO *mbmi = xd->mi[0]; |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 1500 | const BLOCK_SIZE bsize = mbmi->bsize; |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 1501 | assert(bsize <= cm->seq_params->sb_size || |
Hui Su | c6b79f7 | 2019-04-17 16:44:33 -0700 | [diff] [blame] | 1502 | (bsize >= BLOCK_SIZES && bsize < BLOCK_SIZES_ALL)); |
| 1503 | |
| 1504 | const int bh = mi_size_high[bsize]; |
| 1505 | const int bw = mi_size_wide[bsize]; |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 1506 | set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, mi_params->mi_rows, |
| 1507 | mi_params->mi_cols); |
Hui Su | c6b79f7 | 2019-04-17 16:44:33 -0700 | [diff] [blame] | 1508 | |
Urvang Joshi | 5c8625a | 2020-03-30 13:16:37 -0700 | [diff] [blame] | 1509 | xd->above_txfm_context = cm->above_contexts.txfm[tile->tile_row] + mi_col; |
Hui Su | c6b79f7 | 2019-04-17 16:44:33 -0700 | [diff] [blame] | 1510 | xd->left_txfm_context = |
| 1511 | xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK); |
| 1512 | |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1513 | write_mbmi_b(cpi, td, w); |
Hui Su | c6b79f7 | 2019-04-17 16:44:33 -0700 | [diff] [blame] | 1514 | |
Jingning Han | 088217b | 2018-02-23 21:55:21 -0800 | [diff] [blame] | 1515 | for (int plane = 0; plane < AOMMIN(2, av1_num_planes(cm)); ++plane) { |
| 1516 | const uint8_t palette_size_plane = |
| 1517 | mbmi->palette_mode_info.palette_size[plane]; |
Jingning Han | 088217b | 2018-02-23 21:55:21 -0800 | [diff] [blame] | 1518 | assert(!mbmi->skip_mode || !palette_size_plane); |
Jingning Han | 088217b | 2018-02-23 21:55:21 -0800 | [diff] [blame] | 1519 | if (palette_size_plane > 0) { |
| 1520 | assert(mbmi->use_intrabc == 0); |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 1521 | assert(av1_allow_palette(cm->features.allow_screen_content_tools, |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 1522 | mbmi->bsize)); |
Hui Su | a0ea2b8 | 2020-03-02 14:47:31 -0800 | [diff] [blame] | 1523 | assert(!plane || xd->is_chroma_ref); |
Jingning Han | 088217b | 2018-02-23 21:55:21 -0800 | [diff] [blame] | 1524 | int rows, cols; |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 1525 | av1_get_block_dimensions(mbmi->bsize, plane, xd, NULL, NULL, &rows, |
Jingning Han | 088217b | 2018-02-23 21:55:21 -0800 | [diff] [blame] | 1526 | &cols); |
| 1527 | assert(*tok < tok_end); |
Jayasanker J | 4969e72 | 2020-09-18 20:36:15 +0530 | [diff] [blame] | 1528 | MapCdf map_pb_cdf = plane ? tile_ctx->palette_uv_color_index_cdf |
| 1529 | : tile_ctx->palette_y_color_index_cdf; |
| 1530 | pack_map_tokens(w, tok, palette_size_plane, rows * cols, map_pb_cdf); |
Jingning Han | 088217b | 2018-02-23 21:55:21 -0800 | [diff] [blame] | 1531 | } |
| 1532 | } |
| 1533 | |
Hui Su | 0e48db9 | 2019-04-17 15:55:57 -0700 | [diff] [blame] | 1534 | const int is_inter_tx = is_inter_block(mbmi); |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 1535 | const int skip_txfm = mbmi->skip_txfm; |
chiyotsai | d730cef | 2022-10-26 13:58:20 -0700 | [diff] [blame] | 1536 | const uint8_t segment_id = mbmi->segment_id; |
Urvang Joshi | 6237b88 | 2020-03-26 15:02:26 -0700 | [diff] [blame] | 1537 | if (cm->features.tx_mode == TX_MODE_SELECT && block_signals_txsize(bsize) && |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 1538 | !(is_inter_tx && skip_txfm) && !xd->lossless[segment_id]) { |
Jingning Han | 088217b | 2018-02-23 21:55:21 -0800 | [diff] [blame] | 1539 | if (is_inter_tx) { // This implies skip flag is 0. |
| 1540 | const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, bsize, 0); |
| 1541 | const int txbh = tx_size_high_unit[max_tx_size]; |
| 1542 | const int txbw = tx_size_wide_unit[max_tx_size]; |
Hui Su | adda587 | 2019-12-09 10:08:49 -0800 | [diff] [blame] | 1543 | const int width = mi_size_wide[bsize]; |
| 1544 | const int height = mi_size_high[bsize]; |
Hui Su | 0e48db9 | 2019-04-17 15:55:57 -0700 | [diff] [blame] | 1545 | for (int idy = 0; idy < height; idy += txbh) { |
| 1546 | for (int idx = 0; idx < width; idx += txbw) { |
Yaowu Xu | 6567c4a | 2018-03-19 21:39:31 -0700 | [diff] [blame] | 1547 | write_tx_size_vartx(xd, mbmi, max_tx_size, 0, idy, idx, w); |
Hui Su | 0e48db9 | 2019-04-17 15:55:57 -0700 | [diff] [blame] | 1548 | } |
| 1549 | } |
Jingning Han | 088217b | 2018-02-23 21:55:21 -0800 | [diff] [blame] | 1550 | } else { |
Yaowu Xu | 6567c4a | 2018-03-19 21:39:31 -0700 | [diff] [blame] | 1551 | write_selected_tx_size(xd, w); |
Urvang Joshi | 33930b8 | 2020-04-06 12:41:27 -0700 | [diff] [blame] | 1552 | set_txfm_ctxs(mbmi->tx_size, xd->width, xd->height, 0, xd); |
Jingning Han | 088217b | 2018-02-23 21:55:21 -0800 | [diff] [blame] | 1553 | } |
| 1554 | } else { |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 1555 | set_txfm_ctxs(mbmi->tx_size, xd->width, xd->height, |
| 1556 | skip_txfm && is_inter_tx, xd); |
Jingning Han | 088217b | 2018-02-23 21:55:21 -0800 | [diff] [blame] | 1557 | } |
| 1558 | |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 1559 | if (!mbmi->skip_txfm) { |
Cheng Chen | 8e74aaf | 2020-08-24 21:07:12 -0700 | [diff] [blame] | 1560 | int start = aom_tell_size(w); |
| 1561 | |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1562 | write_tokens_b(cpi, &td->mb, w, tok, tok_end); |
Cheng Chen | 8e74aaf | 2020-08-24 21:07:12 -0700 | [diff] [blame] | 1563 | |
| 1564 | const int end = aom_tell_size(w); |
Cherma Rajan A | d0f59a7 | 2021-04-13 12:43:54 +0530 | [diff] [blame] | 1565 | td->coefficient_size += end - start; |
Hui Su | c6b79f7 | 2019-04-17 16:44:33 -0700 | [diff] [blame] | 1566 | } |
Yue Chen | 64550b6 | 2017-01-12 12:18:22 -0800 | [diff] [blame] | 1567 | } |
| 1568 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 1569 | static AOM_INLINE void write_partition(const AV1_COMMON *const cm, |
| 1570 | const MACROBLOCKD *const xd, int hbs, |
| 1571 | int mi_row, int mi_col, PARTITION_TYPE p, |
| 1572 | BLOCK_SIZE bsize, aom_writer *w) { |
Alex Converse | 55c6bde | 2017-01-12 15:55:31 -0800 | [diff] [blame] | 1573 | const int is_partition_point = bsize >= BLOCK_8X8; |
Thomas Davies | c2ec0e4 | 2017-01-11 16:27:27 +0000 | [diff] [blame] | 1574 | |
Jingning Han | bf9c6b7 | 2016-12-14 14:50:45 -0800 | [diff] [blame] | 1575 | if (!is_partition_point) return; |
| 1576 | |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 1577 | const int has_rows = (mi_row + hbs) < cm->mi_params.mi_rows; |
| 1578 | const int has_cols = (mi_col + hbs) < cm->mi_params.mi_cols; |
Rupert Swarbrick | eb12393 | 2017-11-22 15:20:47 +0000 | [diff] [blame] | 1579 | const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize); |
| 1580 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
| 1581 | |
| 1582 | if (!has_rows && !has_cols) { |
| 1583 | assert(p == PARTITION_SPLIT); |
| 1584 | return; |
| 1585 | } |
| 1586 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1587 | if (has_rows && has_cols) { |
Rupert Swarbrick | eb12393 | 2017-11-22 15:20:47 +0000 | [diff] [blame] | 1588 | aom_write_symbol(w, p, ec_ctx->partition_cdf[ctx], |
| 1589 | partition_cdf_length(bsize)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1590 | } else if (!has_rows && has_cols) { |
| 1591 | assert(p == PARTITION_SPLIT || p == PARTITION_HORZ); |
Stanislav Vitvitskyy | 8711cf5 | 2017-08-18 15:17:57 -0700 | [diff] [blame] | 1592 | assert(bsize > BLOCK_8X8); |
| 1593 | aom_cdf_prob cdf[2]; |
Rupert Swarbrick | eb12393 | 2017-11-22 15:20:47 +0000 | [diff] [blame] | 1594 | partition_gather_vert_alike(cdf, ec_ctx->partition_cdf[ctx], bsize); |
Stanislav Vitvitskyy | 8711cf5 | 2017-08-18 15:17:57 -0700 | [diff] [blame] | 1595 | aom_write_cdf(w, p == PARTITION_SPLIT, cdf, 2); |
Rupert Swarbrick | eb12393 | 2017-11-22 15:20:47 +0000 | [diff] [blame] | 1596 | } else { |
| 1597 | assert(has_rows && !has_cols); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1598 | assert(p == PARTITION_SPLIT || p == PARTITION_VERT); |
Stanislav Vitvitskyy | 8711cf5 | 2017-08-18 15:17:57 -0700 | [diff] [blame] | 1599 | assert(bsize > BLOCK_8X8); |
| 1600 | aom_cdf_prob cdf[2]; |
Rupert Swarbrick | eb12393 | 2017-11-22 15:20:47 +0000 | [diff] [blame] | 1601 | partition_gather_horz_alike(cdf, ec_ctx->partition_cdf[ctx], bsize); |
Stanislav Vitvitskyy | 8711cf5 | 2017-08-18 15:17:57 -0700 | [diff] [blame] | 1602 | aom_write_cdf(w, p == PARTITION_SPLIT, cdf, 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1603 | } |
| 1604 | } |
| 1605 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 1606 | static AOM_INLINE void write_modes_sb( |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1607 | AV1_COMP *const cpi, ThreadData *const td, const TileInfo *const tile, |
| 1608 | aom_writer *const w, const TokenExtra **tok, |
| 1609 | const TokenExtra *const tok_end, int mi_row, int mi_col, BLOCK_SIZE bsize) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1610 | const AV1_COMMON *const cm = &cpi->common; |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 1611 | const CommonModeInfoParams *const mi_params = &cm->mi_params; |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1612 | MACROBLOCKD *const xd = &td->mb.e_mbd; |
kyslov | 5859dca | 2019-04-08 12:13:11 -0700 | [diff] [blame] | 1613 | assert(bsize < BLOCK_SIZES_ALL); |
Jingning Han | c709e1f | 2016-12-06 14:48:09 -0800 | [diff] [blame] | 1614 | const int hbs = mi_size_wide[bsize] / 2; |
Rupert Swarbrick | 93c39e9 | 2017-07-12 11:11:02 +0100 | [diff] [blame] | 1615 | const int quarter_step = mi_size_wide[bsize] / 4; |
| 1616 | int i; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1617 | const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize); |
Cheng Chen | 82b4fa1 | 2018-05-02 18:43:17 -0700 | [diff] [blame] | 1618 | const BLOCK_SIZE subsize = get_partition_subsize(bsize, partition); |
Jingning Han | 5226184 | 2016-12-14 12:17:49 -0800 | [diff] [blame] | 1619 | |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 1620 | if (mi_row >= mi_params->mi_rows || mi_col >= mi_params->mi_cols) return; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1621 | |
Jerome Jiang | 3dd9df9 | 2020-10-29 16:42:33 -0700 | [diff] [blame] | 1622 | #if !CONFIG_REALTIME_ONLY |
Debargha Mukherjee | a78c8f5 | 2018-01-31 11:14:38 -0800 | [diff] [blame] | 1623 | const int num_planes = av1_num_planes(cm); |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 1624 | for (int plane = 0; plane < num_planes; ++plane) { |
Cheng Chen | 55c44ce | 2018-06-26 12:34:24 -0700 | [diff] [blame] | 1625 | int rcol0, rcol1, rrow0, rrow1; |
Rachel Barker | 2b98f1b | 2023-07-06 19:39:45 +0000 | [diff] [blame] | 1626 | |
| 1627 | // Skip some unnecessary work if loop restoration is disabled |
| 1628 | if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue; |
| 1629 | |
Imdad Sardharwalla | 7d2e5c9 | 2018-01-05 18:41:00 +0000 | [diff] [blame] | 1630 | if (av1_loop_restoration_corners_in_sb(cm, plane, mi_row, mi_col, bsize, |
Cheng Chen | 55c44ce | 2018-06-26 12:34:24 -0700 | [diff] [blame] | 1631 | &rcol0, &rcol1, &rrow0, &rrow1)) { |
Rachel Barker | 0483cbe | 2023-07-05 23:31:38 +0000 | [diff] [blame] | 1632 | const int rstride = cm->rst_info[plane].horz_units; |
Imdad Sardharwalla | 7d2e5c9 | 2018-01-05 18:41:00 +0000 | [diff] [blame] | 1633 | for (int rrow = rrow0; rrow < rrow1; ++rrow) { |
| 1634 | for (int rcol = rcol0; rcol < rcol1; ++rcol) { |
Cheng Chen | 55c44ce | 2018-06-26 12:34:24 -0700 | [diff] [blame] | 1635 | const int runit_idx = rcol + rrow * rstride; |
Rachel Barker | fe93d33 | 2023-07-10 10:01:15 +0000 | [diff] [blame] | 1636 | loop_restoration_write_sb_coeffs(cm, xd, runit_idx, w, plane, |
| 1637 | td->counts); |
Imdad Sardharwalla | 7d2e5c9 | 2018-01-05 18:41:00 +0000 | [diff] [blame] | 1638 | } |
| 1639 | } |
| 1640 | } |
| 1641 | } |
Jerome Jiang | 3dd9df9 | 2020-10-29 16:42:33 -0700 | [diff] [blame] | 1642 | #endif |
Imdad Sardharwalla | 7d2e5c9 | 2018-01-05 18:41:00 +0000 | [diff] [blame] | 1643 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1644 | write_partition(cm, xd, hbs, mi_row, mi_col, partition, bsize, w); |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1645 | switch (partition) { |
| 1646 | case PARTITION_NONE: |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1647 | write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col); |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1648 | break; |
| 1649 | case PARTITION_HORZ: |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1650 | write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col); |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 1651 | if (mi_row + hbs < mi_params->mi_rows) |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1652 | write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row + hbs, mi_col); |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1653 | break; |
| 1654 | case PARTITION_VERT: |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1655 | write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col); |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 1656 | if (mi_col + hbs < mi_params->mi_cols) |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1657 | write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col + hbs); |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1658 | break; |
| 1659 | case PARTITION_SPLIT: |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1660 | write_modes_sb(cpi, td, tile, w, tok, tok_end, mi_row, mi_col, subsize); |
| 1661 | write_modes_sb(cpi, td, tile, w, tok, tok_end, mi_row, mi_col + hbs, |
| 1662 | subsize); |
| 1663 | write_modes_sb(cpi, td, tile, w, tok, tok_end, mi_row + hbs, mi_col, |
| 1664 | subsize); |
| 1665 | write_modes_sb(cpi, td, tile, w, tok, tok_end, mi_row + hbs, mi_col + hbs, |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1666 | subsize); |
| 1667 | break; |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1668 | case PARTITION_HORZ_A: |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1669 | write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col); |
| 1670 | write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col + hbs); |
| 1671 | write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row + hbs, mi_col); |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1672 | break; |
| 1673 | case PARTITION_HORZ_B: |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1674 | write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col); |
| 1675 | write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row + hbs, mi_col); |
| 1676 | write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row + hbs, mi_col + hbs); |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1677 | break; |
| 1678 | case PARTITION_VERT_A: |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1679 | write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col); |
| 1680 | write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row + hbs, mi_col); |
| 1681 | write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col + hbs); |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1682 | break; |
| 1683 | case PARTITION_VERT_B: |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1684 | write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col); |
| 1685 | write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col + hbs); |
| 1686 | write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row + hbs, mi_col + hbs); |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1687 | break; |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1688 | case PARTITION_HORZ_4: |
| 1689 | for (i = 0; i < 4; ++i) { |
| 1690 | int this_mi_row = mi_row + i * quarter_step; |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 1691 | if (i > 0 && this_mi_row >= mi_params->mi_rows) break; |
Rupert Swarbrick | 93c39e9 | 2017-07-12 11:11:02 +0100 | [diff] [blame] | 1692 | |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1693 | write_modes_b(cpi, td, tile, w, tok, tok_end, this_mi_row, mi_col); |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1694 | } |
| 1695 | break; |
| 1696 | case PARTITION_VERT_4: |
| 1697 | for (i = 0; i < 4; ++i) { |
| 1698 | int this_mi_col = mi_col + i * quarter_step; |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 1699 | if (i > 0 && this_mi_col >= mi_params->mi_cols) break; |
Rupert Swarbrick | 93c39e9 | 2017-07-12 11:11:02 +0100 | [diff] [blame] | 1700 | |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1701 | write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, this_mi_col); |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1702 | } |
| 1703 | break; |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 1704 | default: assert(0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1705 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1706 | |
Debargha Mukherjee | 1687085 | 2018-02-28 10:00:17 -0800 | [diff] [blame] | 1707 | // update partition context |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1708 | update_ext_partition_context(xd, mi_row, mi_col, subsize, bsize, partition); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1709 | } |
| 1710 | |
Jayasanker J | 92cca7a | 2021-10-27 15:05:52 +0530 | [diff] [blame] | 1711 | // Populate token pointers appropriately based on token_info. |
| 1712 | static AOM_INLINE void get_token_pointers(const TokenInfo *token_info, |
| 1713 | const int tile_row, int tile_col, |
| 1714 | const int sb_row_in_tile, |
| 1715 | const TokenExtra **tok, |
| 1716 | const TokenExtra **tok_end) { |
| 1717 | if (!is_token_info_allocated(token_info)) { |
| 1718 | *tok = NULL; |
| 1719 | *tok_end = NULL; |
| 1720 | return; |
| 1721 | } |
| 1722 | *tok = token_info->tplist[tile_row][tile_col][sb_row_in_tile].start; |
| 1723 | *tok_end = |
| 1724 | *tok + token_info->tplist[tile_row][tile_col][sb_row_in_tile].count; |
| 1725 | } |
| 1726 | |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1727 | static AOM_INLINE void write_modes(AV1_COMP *const cpi, ThreadData *const td, |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 1728 | const TileInfo *const tile, |
| 1729 | aom_writer *const w, int tile_row, |
| 1730 | int tile_col) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1731 | AV1_COMMON *const cm = &cpi->common; |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1732 | MACROBLOCKD *const xd = &td->mb.e_mbd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1733 | const int mi_row_start = tile->mi_row_start; |
| 1734 | const int mi_row_end = tile->mi_row_end; |
| 1735 | const int mi_col_start = tile->mi_col_start; |
| 1736 | const int mi_col_end = tile->mi_col_end; |
Urvang Joshi | 5c8625a | 2020-03-30 13:16:37 -0700 | [diff] [blame] | 1737 | const int num_planes = av1_num_planes(cm); |
Fangwen Fu | 7b9f2b3 | 2017-01-17 14:01:52 -0800 | [diff] [blame] | 1738 | |
Yunqing Wang | 6c50963 | 2018-07-03 12:38:33 -0700 | [diff] [blame] | 1739 | av1_zero_above_context(cm, xd, mi_col_start, mi_col_end, tile->tile_row); |
Urvang Joshi | 5c8625a | 2020-03-30 13:16:37 -0700 | [diff] [blame] | 1740 | av1_init_above_context(&cm->above_contexts, num_planes, tile->tile_row, xd); |
Cherma Rajan A | 71d20db | 2018-04-27 11:15:32 +0530 | [diff] [blame] | 1741 | |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 1742 | if (cpi->common.delta_q_info.delta_q_present_flag) { |
Urvang Joshi | acad1ca | 2020-04-27 17:03:25 -0700 | [diff] [blame] | 1743 | xd->current_base_qindex = cpi->common.quant_params.base_qindex; |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 1744 | if (cpi->common.delta_q_info.delta_lf_present_flag) { |
Urvang Joshi | 5c8625a | 2020-03-30 13:16:37 -0700 | [diff] [blame] | 1745 | av1_reset_loop_filter_delta(xd, num_planes); |
Fangwen Fu | 231fe42 | 2017-04-24 17:52:29 -0700 | [diff] [blame] | 1746 | } |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 1747 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1748 | |
Urvang Joshi | 5c8625a | 2020-03-30 13:16:37 -0700 | [diff] [blame] | 1749 | for (int mi_row = mi_row_start; mi_row < mi_row_end; |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 1750 | mi_row += cm->seq_params->mib_size) { |
Urvang Joshi | 5c8625a | 2020-03-30 13:16:37 -0700 | [diff] [blame] | 1751 | const int sb_row_in_tile = |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 1752 | (mi_row - tile->mi_row_start) >> cm->seq_params->mib_size_log2; |
Jayasanker J | 92cca7a | 2021-10-27 15:05:52 +0530 | [diff] [blame] | 1753 | const TokenInfo *token_info = &cpi->token_info; |
| 1754 | const TokenExtra *tok; |
| 1755 | const TokenExtra *tok_end; |
| 1756 | get_token_pointers(token_info, tile_row, tile_col, sb_row_in_tile, &tok, |
| 1757 | &tok_end); |
Ravi Chaudhary | 73cf15b | 2018-08-30 10:52:51 +0530 | [diff] [blame] | 1758 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1759 | av1_zero_left_context(xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1760 | |
Urvang Joshi | 5c8625a | 2020-03-30 13:16:37 -0700 | [diff] [blame] | 1761 | for (int mi_col = mi_col_start; mi_col < mi_col_end; |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 1762 | mi_col += cm->seq_params->mib_size) { |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 1763 | td->mb.cb_coef_buff = av1_get_cb_coeff_buffer(cpi, mi_row, mi_col); |
| 1764 | write_modes_sb(cpi, td, tile, w, &tok, tok_end, mi_row, mi_col, |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 1765 | cm->seq_params->sb_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1766 | } |
Vishesh | 686aa77 | 2020-04-13 14:40:12 +0530 | [diff] [blame] | 1767 | assert(tok == tok_end); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1768 | } |
| 1769 | } |
| 1770 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 1771 | static AOM_INLINE void encode_restoration_mode( |
| 1772 | AV1_COMMON *cm, struct aom_write_bit_buffer *wb) { |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 1773 | assert(!cm->features.all_lossless); |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 1774 | if (!cm->seq_params->enable_restoration) return; |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 1775 | if (cm->features.allow_intrabc) return; |
Hui Su | 293f281 | 2018-02-26 14:41:18 -0800 | [diff] [blame] | 1776 | const int num_planes = av1_num_planes(cm); |
Rupert Swarbrick | dcb3cff | 2017-11-09 15:58:33 +0000 | [diff] [blame] | 1777 | int all_none = 1, chroma_none = 1; |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 1778 | for (int p = 0; p < num_planes; ++p) { |
Rupert Swarbrick | 4596deb | 2017-11-07 18:06:38 +0000 | [diff] [blame] | 1779 | RestorationInfo *rsi = &cm->rst_info[p]; |
Rupert Swarbrick | dcb3cff | 2017-11-09 15:58:33 +0000 | [diff] [blame] | 1780 | if (rsi->frame_restoration_type != RESTORE_NONE) { |
| 1781 | all_none = 0; |
| 1782 | chroma_none &= p == 0; |
| 1783 | } |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 1784 | switch (rsi->frame_restoration_type) { |
Debargha Mukherjee | a3d4fe5 | 2017-05-19 16:22:54 -0700 | [diff] [blame] | 1785 | case RESTORE_NONE: |
| 1786 | aom_wb_write_bit(wb, 0); |
| 1787 | aom_wb_write_bit(wb, 0); |
| 1788 | break; |
Debargha Mukherjee | d23ceea | 2017-05-18 20:33:52 -0700 | [diff] [blame] | 1789 | case RESTORE_WIENER: |
| 1790 | aom_wb_write_bit(wb, 1); |
| 1791 | aom_wb_write_bit(wb, 0); |
| 1792 | break; |
| 1793 | case RESTORE_SGRPROJ: |
| 1794 | aom_wb_write_bit(wb, 1); |
| 1795 | aom_wb_write_bit(wb, 1); |
| 1796 | break; |
Debargha Mukherjee | a3d4fe5 | 2017-05-19 16:22:54 -0700 | [diff] [blame] | 1797 | case RESTORE_SWITCHABLE: |
| 1798 | aom_wb_write_bit(wb, 0); |
| 1799 | aom_wb_write_bit(wb, 1); |
| 1800 | break; |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 1801 | default: assert(0); |
| 1802 | } |
| 1803 | } |
Rupert Swarbrick | dcb3cff | 2017-11-09 15:58:33 +0000 | [diff] [blame] | 1804 | if (!all_none) { |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 1805 | assert(cm->seq_params->sb_size == BLOCK_64X64 || |
| 1806 | cm->seq_params->sb_size == BLOCK_128X128); |
| 1807 | const int sb_size = cm->seq_params->sb_size == BLOCK_128X128 ? 128 : 64; |
Imdad Sardharwalla | b1dce0a | 2018-02-12 16:43:59 +0000 | [diff] [blame] | 1808 | |
Rupert Swarbrick | 4596deb | 2017-11-07 18:06:38 +0000 | [diff] [blame] | 1809 | RestorationInfo *rsi = &cm->rst_info[0]; |
Imdad Sardharwalla | b1dce0a | 2018-02-12 16:43:59 +0000 | [diff] [blame] | 1810 | |
| 1811 | assert(rsi->restoration_unit_size >= sb_size); |
Urvang Joshi | 813186b | 2018-03-08 15:38:46 -0800 | [diff] [blame] | 1812 | assert(RESTORATION_UNITSIZE_MAX == 256); |
Imdad Sardharwalla | b1dce0a | 2018-02-12 16:43:59 +0000 | [diff] [blame] | 1813 | |
| 1814 | if (sb_size == 64) { |
| 1815 | aom_wb_write_bit(wb, rsi->restoration_unit_size > 64); |
| 1816 | } |
| 1817 | if (rsi->restoration_unit_size > 64) { |
| 1818 | aom_wb_write_bit(wb, rsi->restoration_unit_size > 128); |
Debargha Mukherjee | 1008c1e | 2017-03-06 19:18:43 -0800 | [diff] [blame] | 1819 | } |
| 1820 | } |
Rupert Swarbrick | dcb3cff | 2017-11-09 15:58:33 +0000 | [diff] [blame] | 1821 | |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 1822 | if (num_planes > 1) { |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 1823 | int s = |
| 1824 | AOMMIN(cm->seq_params->subsampling_x, cm->seq_params->subsampling_y); |
Rupert Swarbrick | dcb3cff | 2017-11-09 15:58:33 +0000 | [diff] [blame] | 1825 | if (s && !chroma_none) { |
Johann | b0ef6ff | 2018-02-08 14:32:21 -0800 | [diff] [blame] | 1826 | aom_wb_write_bit(wb, cm->rst_info[1].restoration_unit_size != |
| 1827 | cm->rst_info[0].restoration_unit_size); |
Rupert Swarbrick | dcb3cff | 2017-11-09 15:58:33 +0000 | [diff] [blame] | 1828 | assert(cm->rst_info[1].restoration_unit_size == |
| 1829 | cm->rst_info[0].restoration_unit_size || |
| 1830 | cm->rst_info[1].restoration_unit_size == |
| 1831 | (cm->rst_info[0].restoration_unit_size >> s)); |
| 1832 | assert(cm->rst_info[2].restoration_unit_size == |
| 1833 | cm->rst_info[1].restoration_unit_size); |
| 1834 | } else if (!s) { |
| 1835 | assert(cm->rst_info[1].restoration_unit_size == |
| 1836 | cm->rst_info[0].restoration_unit_size); |
| 1837 | assert(cm->rst_info[2].restoration_unit_size == |
| 1838 | cm->rst_info[1].restoration_unit_size); |
| 1839 | } |
Debargha Mukherjee | 84f567c | 2017-06-21 10:53:59 -0700 | [diff] [blame] | 1840 | } |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 1841 | } |
| 1842 | |
Jerome Jiang | 3dd9df9 | 2020-10-29 16:42:33 -0700 | [diff] [blame] | 1843 | #if !CONFIG_REALTIME_ONLY |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 1844 | static AOM_INLINE void write_wiener_filter(int wiener_win, |
| 1845 | const WienerInfo *wiener_info, |
| 1846 | WienerInfo *ref_wiener_info, |
| 1847 | aom_writer *wb) { |
Debargha Mukherjee | 1cb757c | 2017-08-21 02:46:31 -0700 | [diff] [blame] | 1848 | if (wiener_win == WIENER_WIN) |
| 1849 | aom_write_primitive_refsubexpfin( |
| 1850 | wb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1, |
| 1851 | WIENER_FILT_TAP0_SUBEXP_K, |
| 1852 | ref_wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV, |
| 1853 | wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV); |
| 1854 | else |
| 1855 | assert(wiener_info->vfilter[0] == 0 && |
| 1856 | wiener_info->vfilter[WIENER_WIN - 1] == 0); |
Debargha Mukherjee | cfc12f3 | 2017-04-18 07:03:32 -0700 | [diff] [blame] | 1857 | aom_write_primitive_refsubexpfin( |
| 1858 | wb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1, |
| 1859 | WIENER_FILT_TAP1_SUBEXP_K, |
| 1860 | ref_wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV, |
| 1861 | wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV); |
| 1862 | aom_write_primitive_refsubexpfin( |
| 1863 | wb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1, |
| 1864 | WIENER_FILT_TAP2_SUBEXP_K, |
| 1865 | ref_wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV, |
| 1866 | wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV); |
Debargha Mukherjee | 1cb757c | 2017-08-21 02:46:31 -0700 | [diff] [blame] | 1867 | if (wiener_win == WIENER_WIN) |
| 1868 | aom_write_primitive_refsubexpfin( |
| 1869 | wb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1, |
| 1870 | WIENER_FILT_TAP0_SUBEXP_K, |
| 1871 | ref_wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV, |
| 1872 | wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV); |
| 1873 | else |
| 1874 | assert(wiener_info->hfilter[0] == 0 && |
| 1875 | wiener_info->hfilter[WIENER_WIN - 1] == 0); |
Debargha Mukherjee | cfc12f3 | 2017-04-18 07:03:32 -0700 | [diff] [blame] | 1876 | aom_write_primitive_refsubexpfin( |
| 1877 | wb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1, |
| 1878 | WIENER_FILT_TAP1_SUBEXP_K, |
| 1879 | ref_wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV, |
| 1880 | wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV); |
| 1881 | aom_write_primitive_refsubexpfin( |
| 1882 | wb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1, |
| 1883 | WIENER_FILT_TAP2_SUBEXP_K, |
| 1884 | ref_wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV, |
| 1885 | wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV); |
| 1886 | memcpy(ref_wiener_info, wiener_info, sizeof(*wiener_info)); |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 1887 | } |
| 1888 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 1889 | static AOM_INLINE void write_sgrproj_filter(const SgrprojInfo *sgrproj_info, |
| 1890 | SgrprojInfo *ref_sgrproj_info, |
| 1891 | aom_writer *wb) { |
Imdad Sardharwalla | fdeb116 | 2018-02-21 17:38:20 +0000 | [diff] [blame] | 1892 | aom_write_literal(wb, sgrproj_info->ep, SGRPROJ_PARAMS_BITS); |
Yaowu Xu | bf732b8 | 2019-04-30 15:21:52 -0700 | [diff] [blame] | 1893 | const sgr_params_type *params = &av1_sgr_params[sgrproj_info->ep]; |
Imdad Sardharwalla | fdeb116 | 2018-02-21 17:38:20 +0000 | [diff] [blame] | 1894 | |
Urvang Joshi | 3715b88 | 2018-05-14 20:05:25 -0400 | [diff] [blame] | 1895 | if (params->r[0] == 0) { |
Imdad Sardharwalla | fdeb116 | 2018-02-21 17:38:20 +0000 | [diff] [blame] | 1896 | assert(sgrproj_info->xqd[0] == 0); |
| 1897 | aom_write_primitive_refsubexpfin( |
| 1898 | wb, SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K, |
| 1899 | ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1, |
| 1900 | sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1); |
Urvang Joshi | 3715b88 | 2018-05-14 20:05:25 -0400 | [diff] [blame] | 1901 | } else if (params->r[1] == 0) { |
Imdad Sardharwalla | fdeb116 | 2018-02-21 17:38:20 +0000 | [diff] [blame] | 1902 | aom_write_primitive_refsubexpfin( |
| 1903 | wb, SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K, |
| 1904 | ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0, |
| 1905 | sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0); |
Imdad Sardharwalla | fdeb116 | 2018-02-21 17:38:20 +0000 | [diff] [blame] | 1906 | } else { |
| 1907 | aom_write_primitive_refsubexpfin( |
| 1908 | wb, SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K, |
| 1909 | ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0, |
| 1910 | sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0); |
| 1911 | aom_write_primitive_refsubexpfin( |
| 1912 | wb, SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K, |
| 1913 | ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1, |
| 1914 | sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1); |
| 1915 | } |
| 1916 | |
| 1917 | memcpy(ref_sgrproj_info, sgrproj_info, sizeof(*sgrproj_info)); |
| 1918 | } |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 1919 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 1920 | static AOM_INLINE void loop_restoration_write_sb_coeffs( |
Rachel Barker | fe93d33 | 2023-07-10 10:01:15 +0000 | [diff] [blame] | 1921 | const AV1_COMMON *const cm, MACROBLOCKD *xd, int runit_idx, |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 1922 | aom_writer *const w, int plane, FRAME_COUNTS *counts) { |
Rachel Barker | fe93d33 | 2023-07-10 10:01:15 +0000 | [diff] [blame] | 1923 | const RestorationUnitInfo *rui = &cm->rst_info[plane].unit_info[runit_idx]; |
Rupert Swarbrick | 6c54521 | 2017-09-01 17:17:25 +0100 | [diff] [blame] | 1924 | const RestorationInfo *rsi = cm->rst_info + plane; |
Rupert Swarbrick | dd6f09a | 2017-10-19 16:10:23 +0100 | [diff] [blame] | 1925 | RestorationType frame_rtype = rsi->frame_restoration_type; |
susannad | edc8a3a | 2020-11-06 16:39:21 +0000 | [diff] [blame] | 1926 | assert(frame_rtype != RESTORE_NONE); |
Rupert Swarbrick | 09b5b16 | 2017-08-31 16:32:29 +0100 | [diff] [blame] | 1927 | |
Yue Chen | 3dd9a12 | 2018-04-12 15:54:17 -0700 | [diff] [blame] | 1928 | (void)counts; |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 1929 | assert(!cm->features.all_lossless); |
Urvang Joshi | 5ec7b81 | 2018-02-28 14:37:06 -0800 | [diff] [blame] | 1930 | |
Rupert Swarbrick | 6c54521 | 2017-09-01 17:17:25 +0100 | [diff] [blame] | 1931 | const int wiener_win = (plane > 0) ? WIENER_WIN_CHROMA : WIENER_WIN; |
David Turner | 760a2f4 | 2018-12-07 15:25:36 +0000 | [diff] [blame] | 1932 | WienerInfo *ref_wiener_info = &xd->wiener_info[plane]; |
| 1933 | SgrprojInfo *ref_sgrproj_info = &xd->sgrproj_info[plane]; |
Rupert Swarbrick | dd6f09a | 2017-10-19 16:10:23 +0100 | [diff] [blame] | 1934 | RestorationType unit_rtype = rui->restoration_type; |
Rupert Swarbrick | 09b5b16 | 2017-08-31 16:32:29 +0100 | [diff] [blame] | 1935 | |
Rupert Swarbrick | dd6f09a | 2017-10-19 16:10:23 +0100 | [diff] [blame] | 1936 | if (frame_rtype == RESTORE_SWITCHABLE) { |
| 1937 | aom_write_symbol(w, unit_rtype, xd->tile_ctx->switchable_restore_cdf, |
Debargha Mukherjee | bc732ef | 2017-10-12 12:40:25 -0700 | [diff] [blame] | 1938 | RESTORE_SWITCHABLE_TYPES); |
Yue Chen | 3dd9a12 | 2018-04-12 15:54:17 -0700 | [diff] [blame] | 1939 | #if CONFIG_ENTROPY_STATS |
Yue Chen | 4439151 | 2018-03-13 15:37:26 -0700 | [diff] [blame] | 1940 | ++counts->switchable_restore[unit_rtype]; |
Yue Chen | 3dd9a12 | 2018-04-12 15:54:17 -0700 | [diff] [blame] | 1941 | #endif |
Rupert Swarbrick | dd6f09a | 2017-10-19 16:10:23 +0100 | [diff] [blame] | 1942 | switch (unit_rtype) { |
| 1943 | case RESTORE_WIENER: |
Rachel Barker | fe93d33 | 2023-07-10 10:01:15 +0000 | [diff] [blame] | 1944 | #if DEBUG_LR_COSTING |
| 1945 | assert(!memcmp( |
| 1946 | ref_wiener_info, |
| 1947 | &lr_ref_params[RESTORE_SWITCHABLE][plane][runit_idx].wiener_info, |
| 1948 | sizeof(*ref_wiener_info))); |
| 1949 | #endif |
David Turner | 760a2f4 | 2018-12-07 15:25:36 +0000 | [diff] [blame] | 1950 | write_wiener_filter(wiener_win, &rui->wiener_info, ref_wiener_info, w); |
Rupert Swarbrick | dd6f09a | 2017-10-19 16:10:23 +0100 | [diff] [blame] | 1951 | break; |
| 1952 | case RESTORE_SGRPROJ: |
Rachel Barker | fe93d33 | 2023-07-10 10:01:15 +0000 | [diff] [blame] | 1953 | #if DEBUG_LR_COSTING |
| 1954 | assert(!memcmp(&ref_sgrproj_info->xqd, |
| 1955 | &lr_ref_params[RESTORE_SWITCHABLE][plane][runit_idx] |
| 1956 | .sgrproj_info.xqd, |
| 1957 | sizeof(ref_sgrproj_info->xqd))); |
| 1958 | #endif |
David Turner | 760a2f4 | 2018-12-07 15:25:36 +0000 | [diff] [blame] | 1959 | write_sgrproj_filter(&rui->sgrproj_info, ref_sgrproj_info, w); |
Rupert Swarbrick | dd6f09a | 2017-10-19 16:10:23 +0100 | [diff] [blame] | 1960 | break; |
| 1961 | default: assert(unit_rtype == RESTORE_NONE); break; |
Rupert Swarbrick | 6c54521 | 2017-09-01 17:17:25 +0100 | [diff] [blame] | 1962 | } |
Rupert Swarbrick | dd6f09a | 2017-10-19 16:10:23 +0100 | [diff] [blame] | 1963 | } else if (frame_rtype == RESTORE_WIENER) { |
Rupert Swarbrick | dd6f09a | 2017-10-19 16:10:23 +0100 | [diff] [blame] | 1964 | aom_write_symbol(w, unit_rtype != RESTORE_NONE, |
Debargha Mukherjee | bc732ef | 2017-10-12 12:40:25 -0700 | [diff] [blame] | 1965 | xd->tile_ctx->wiener_restore_cdf, 2); |
Yue Chen | 3dd9a12 | 2018-04-12 15:54:17 -0700 | [diff] [blame] | 1966 | #if CONFIG_ENTROPY_STATS |
Yue Chen | 4439151 | 2018-03-13 15:37:26 -0700 | [diff] [blame] | 1967 | ++counts->wiener_restore[unit_rtype != RESTORE_NONE]; |
Yue Chen | 3dd9a12 | 2018-04-12 15:54:17 -0700 | [diff] [blame] | 1968 | #endif |
Rupert Swarbrick | dd6f09a | 2017-10-19 16:10:23 +0100 | [diff] [blame] | 1969 | if (unit_rtype != RESTORE_NONE) { |
Rachel Barker | fe93d33 | 2023-07-10 10:01:15 +0000 | [diff] [blame] | 1970 | #if DEBUG_LR_COSTING |
| 1971 | assert( |
| 1972 | !memcmp(ref_wiener_info, |
| 1973 | &lr_ref_params[RESTORE_WIENER][plane][runit_idx].wiener_info, |
| 1974 | sizeof(*ref_wiener_info))); |
| 1975 | #endif |
David Turner | 760a2f4 | 2018-12-07 15:25:36 +0000 | [diff] [blame] | 1976 | write_wiener_filter(wiener_win, &rui->wiener_info, ref_wiener_info, w); |
Rupert Swarbrick | 6c54521 | 2017-09-01 17:17:25 +0100 | [diff] [blame] | 1977 | } |
Rupert Swarbrick | dd6f09a | 2017-10-19 16:10:23 +0100 | [diff] [blame] | 1978 | } else if (frame_rtype == RESTORE_SGRPROJ) { |
Rupert Swarbrick | dd6f09a | 2017-10-19 16:10:23 +0100 | [diff] [blame] | 1979 | aom_write_symbol(w, unit_rtype != RESTORE_NONE, |
Debargha Mukherjee | bc732ef | 2017-10-12 12:40:25 -0700 | [diff] [blame] | 1980 | xd->tile_ctx->sgrproj_restore_cdf, 2); |
Yue Chen | 3dd9a12 | 2018-04-12 15:54:17 -0700 | [diff] [blame] | 1981 | #if CONFIG_ENTROPY_STATS |
Yue Chen | 4439151 | 2018-03-13 15:37:26 -0700 | [diff] [blame] | 1982 | ++counts->sgrproj_restore[unit_rtype != RESTORE_NONE]; |
Yue Chen | 3dd9a12 | 2018-04-12 15:54:17 -0700 | [diff] [blame] | 1983 | #endif |
Rupert Swarbrick | dd6f09a | 2017-10-19 16:10:23 +0100 | [diff] [blame] | 1984 | if (unit_rtype != RESTORE_NONE) { |
Rachel Barker | fe93d33 | 2023-07-10 10:01:15 +0000 | [diff] [blame] | 1985 | #if DEBUG_LR_COSTING |
| 1986 | assert(!memcmp( |
| 1987 | &ref_sgrproj_info->xqd, |
| 1988 | &lr_ref_params[RESTORE_SGRPROJ][plane][runit_idx].sgrproj_info.xqd, |
| 1989 | sizeof(ref_sgrproj_info->xqd))); |
| 1990 | #endif |
David Turner | 760a2f4 | 2018-12-07 15:25:36 +0000 | [diff] [blame] | 1991 | write_sgrproj_filter(&rui->sgrproj_info, ref_sgrproj_info, w); |
Rupert Swarbrick | 09b5b16 | 2017-08-31 16:32:29 +0100 | [diff] [blame] | 1992 | } |
| 1993 | } |
| 1994 | } |
Jerome Jiang | 3dd9df9 | 2020-10-29 16:42:33 -0700 | [diff] [blame] | 1995 | #endif // !CONFIG_REALTIME_ONLY |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1996 | |
Elliott Karpilovsky | 00ca4a3 | 2019-10-31 17:06:50 -0700 | [diff] [blame] | 1997 | // Only write out the ref delta section if any of the elements |
| 1998 | // will signal a delta. |
| 1999 | static bool is_mode_ref_delta_meaningful(AV1_COMMON *cm) { |
| 2000 | struct loopfilter *lf = &cm->lf; |
| 2001 | if (!lf->mode_ref_delta_update) { |
| 2002 | return 0; |
| 2003 | } |
| 2004 | const RefCntBuffer *buf = get_primary_ref_frame_buf(cm); |
| 2005 | int8_t last_ref_deltas[REF_FRAMES]; |
| 2006 | int8_t last_mode_deltas[MAX_MODE_LF_DELTAS]; |
| 2007 | if (buf == NULL) { |
| 2008 | av1_set_default_ref_deltas(last_ref_deltas); |
| 2009 | av1_set_default_mode_deltas(last_mode_deltas); |
| 2010 | } else { |
| 2011 | memcpy(last_ref_deltas, buf->ref_deltas, REF_FRAMES); |
| 2012 | memcpy(last_mode_deltas, buf->mode_deltas, MAX_MODE_LF_DELTAS); |
| 2013 | } |
| 2014 | for (int i = 0; i < REF_FRAMES; i++) { |
| 2015 | if (lf->ref_deltas[i] != last_ref_deltas[i]) { |
| 2016 | return true; |
| 2017 | } |
| 2018 | } |
| 2019 | for (int i = 0; i < MAX_MODE_LF_DELTAS; i++) { |
| 2020 | if (lf->mode_deltas[i] != last_mode_deltas[i]) { |
| 2021 | return true; |
| 2022 | } |
| 2023 | } |
| 2024 | return false; |
| 2025 | } |
| 2026 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2027 | static AOM_INLINE void encode_loopfilter(AV1_COMMON *cm, |
| 2028 | struct aom_write_bit_buffer *wb) { |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 2029 | assert(!cm->features.coded_lossless); |
| 2030 | if (cm->features.allow_intrabc) return; |
Hui Su | 293f281 | 2018-02-26 14:41:18 -0800 | [diff] [blame] | 2031 | const int num_planes = av1_num_planes(cm); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2032 | struct loopfilter *lf = &cm->lf; |
| 2033 | |
Debargha Mukherjee | 2382b14 | 2018-02-26 14:31:32 -0800 | [diff] [blame] | 2034 | // Encode the loop filter level and type |
Cheng Chen | 76224b0 | 2017-12-15 12:21:01 -0800 | [diff] [blame] | 2035 | aom_wb_write_literal(wb, lf->filter_level[0], 6); |
| 2036 | aom_wb_write_literal(wb, lf->filter_level[1], 6); |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 2037 | if (num_planes > 1) { |
Cheng Chen | 76224b0 | 2017-12-15 12:21:01 -0800 | [diff] [blame] | 2038 | if (lf->filter_level[0] || lf->filter_level[1]) { |
| 2039 | aom_wb_write_literal(wb, lf->filter_level_u, 6); |
| 2040 | aom_wb_write_literal(wb, lf->filter_level_v, 6); |
Cheng Chen | 765e34e | 2017-12-11 11:43:35 -0800 | [diff] [blame] | 2041 | } |
Cheng Chen | e94df5c | 2017-07-19 17:25:33 -0700 | [diff] [blame] | 2042 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2043 | aom_wb_write_literal(wb, lf->sharpness_level, 3); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2044 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2045 | aom_wb_write_bit(wb, lf->mode_ref_delta_enabled); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2046 | |
Elliott Karpilovsky | 00ca4a3 | 2019-10-31 17:06:50 -0700 | [diff] [blame] | 2047 | // Write out loop filter deltas applied at the MB level based on mode or |
| 2048 | // ref frame (if they are enabled), only if there is information to write. |
| 2049 | int meaningful = is_mode_ref_delta_meaningful(cm); |
| 2050 | aom_wb_write_bit(wb, meaningful); |
| 2051 | if (!meaningful) { |
| 2052 | return; |
| 2053 | } |
Cheng Chen | 7d089ae | 2018-03-15 19:11:03 -0700 | [diff] [blame] | 2054 | |
Elliott Karpilovsky | 00ca4a3 | 2019-10-31 17:06:50 -0700 | [diff] [blame] | 2055 | const RefCntBuffer *buf = get_primary_ref_frame_buf(cm); |
| 2056 | int8_t last_ref_deltas[REF_FRAMES]; |
| 2057 | int8_t last_mode_deltas[MAX_MODE_LF_DELTAS]; |
| 2058 | if (buf == NULL) { |
| 2059 | av1_set_default_ref_deltas(last_ref_deltas); |
| 2060 | av1_set_default_mode_deltas(last_mode_deltas); |
| 2061 | } else { |
| 2062 | memcpy(last_ref_deltas, buf->ref_deltas, REF_FRAMES); |
| 2063 | memcpy(last_mode_deltas, buf->mode_deltas, MAX_MODE_LF_DELTAS); |
| 2064 | } |
| 2065 | for (int i = 0; i < REF_FRAMES; i++) { |
| 2066 | const int delta = lf->ref_deltas[i]; |
| 2067 | const int changed = delta != last_ref_deltas[i]; |
| 2068 | aom_wb_write_bit(wb, changed); |
| 2069 | if (changed) aom_wb_write_inv_signed_literal(wb, delta, 6); |
| 2070 | } |
| 2071 | for (int i = 0; i < MAX_MODE_LF_DELTAS; i++) { |
| 2072 | const int delta = lf->mode_deltas[i]; |
| 2073 | const int changed = delta != last_mode_deltas[i]; |
| 2074 | aom_wb_write_bit(wb, changed); |
| 2075 | if (changed) aom_wb_write_inv_signed_literal(wb, delta, 6); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2076 | } |
| 2077 | } |
| 2078 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2079 | static AOM_INLINE void encode_cdef(const AV1_COMMON *cm, |
| 2080 | struct aom_write_bit_buffer *wb) { |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 2081 | assert(!cm->features.coded_lossless); |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 2082 | if (!cm->seq_params->enable_cdef) return; |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 2083 | if (cm->features.allow_intrabc) return; |
Hui Su | 293f281 | 2018-02-26 14:41:18 -0800 | [diff] [blame] | 2084 | const int num_planes = av1_num_planes(cm); |
Jean-Marc Valin | 5f5c132 | 2017-03-21 16:20:21 -0400 | [diff] [blame] | 2085 | int i; |
Hui Su | 584ba48 | 2019-06-19 11:48:05 -0700 | [diff] [blame] | 2086 | aom_wb_write_literal(wb, cm->cdef_info.cdef_damping - 3, 2); |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 2087 | aom_wb_write_literal(wb, cm->cdef_info.cdef_bits, 2); |
| 2088 | for (i = 0; i < cm->cdef_info.nb_cdef_strengths; i++) { |
| 2089 | aom_wb_write_literal(wb, cm->cdef_info.cdef_strengths[i], |
| 2090 | CDEF_STRENGTH_BITS); |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 2091 | if (num_planes > 1) |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 2092 | aom_wb_write_literal(wb, cm->cdef_info.cdef_uv_strengths[i], |
| 2093 | CDEF_STRENGTH_BITS); |
Jean-Marc Valin | 5f5c132 | 2017-03-21 16:20:21 -0400 | [diff] [blame] | 2094 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2095 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2096 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2097 | static AOM_INLINE void write_delta_q(struct aom_write_bit_buffer *wb, |
| 2098 | int delta_q) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2099 | if (delta_q != 0) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2100 | aom_wb_write_bit(wb, 1); |
| 2101 | aom_wb_write_inv_signed_literal(wb, delta_q, 6); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2102 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2103 | aom_wb_write_bit(wb, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2104 | } |
| 2105 | } |
| 2106 | |
Urvang Joshi | 1781462 | 2020-03-27 17:26:17 -0700 | [diff] [blame] | 2107 | static AOM_INLINE void encode_quantization( |
| 2108 | const CommonQuantParams *const quant_params, int num_planes, |
| 2109 | bool separate_uv_delta_q, struct aom_write_bit_buffer *wb) { |
| 2110 | aom_wb_write_literal(wb, quant_params->base_qindex, QINDEX_BITS); |
| 2111 | write_delta_q(wb, quant_params->y_dc_delta_q); |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 2112 | if (num_planes > 1) { |
Urvang Joshi | 1781462 | 2020-03-27 17:26:17 -0700 | [diff] [blame] | 2113 | int diff_uv_delta = |
| 2114 | (quant_params->u_dc_delta_q != quant_params->v_dc_delta_q) || |
| 2115 | (quant_params->u_ac_delta_q != quant_params->v_ac_delta_q); |
| 2116 | if (separate_uv_delta_q) aom_wb_write_bit(wb, diff_uv_delta); |
| 2117 | write_delta_q(wb, quant_params->u_dc_delta_q); |
| 2118 | write_delta_q(wb, quant_params->u_ac_delta_q); |
Debargha Mukherjee | 18f4fb2 | 2017-12-14 14:26:27 -0800 | [diff] [blame] | 2119 | if (diff_uv_delta) { |
Urvang Joshi | 1781462 | 2020-03-27 17:26:17 -0700 | [diff] [blame] | 2120 | write_delta_q(wb, quant_params->v_dc_delta_q); |
| 2121 | write_delta_q(wb, quant_params->v_ac_delta_q); |
Debargha Mukherjee | 18f4fb2 | 2017-12-14 14:26:27 -0800 | [diff] [blame] | 2122 | } |
Yaowu Xu | 6fc47e5 | 2017-12-04 15:07:48 -0800 | [diff] [blame] | 2123 | } |
Urvang Joshi | 1781462 | 2020-03-27 17:26:17 -0700 | [diff] [blame] | 2124 | aom_wb_write_bit(wb, quant_params->using_qmatrix); |
| 2125 | if (quant_params->using_qmatrix) { |
Urvang Joshi | 7fdbd05 | 2020-03-30 17:50:22 -0700 | [diff] [blame] | 2126 | aom_wb_write_literal(wb, quant_params->qmatrix_level_y, QM_LEVEL_BITS); |
| 2127 | aom_wb_write_literal(wb, quant_params->qmatrix_level_u, QM_LEVEL_BITS); |
Urvang Joshi | 1781462 | 2020-03-27 17:26:17 -0700 | [diff] [blame] | 2128 | if (!separate_uv_delta_q) |
Urvang Joshi | 7fdbd05 | 2020-03-30 17:50:22 -0700 | [diff] [blame] | 2129 | assert(quant_params->qmatrix_level_u == quant_params->qmatrix_level_v); |
Yaowu Xu | f7a1242 | 2018-01-31 15:29:20 -0800 | [diff] [blame] | 2130 | else |
Urvang Joshi | 7fdbd05 | 2020-03-30 17:50:22 -0700 | [diff] [blame] | 2131 | aom_wb_write_literal(wb, quant_params->qmatrix_level_v, QM_LEVEL_BITS); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2132 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2133 | } |
| 2134 | |
Cherma Rajan A | b4be7e5 | 2021-04-14 19:28:53 +0530 | [diff] [blame] | 2135 | static AOM_INLINE void encode_segmentation(AV1_COMMON *cm, |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2136 | struct aom_write_bit_buffer *wb) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2137 | int i, j; |
Rostislav Pehlivanov | dd1a80c | 2018-03-05 21:26:45 +0000 | [diff] [blame] | 2138 | struct segmentation *seg = &cm->seg; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2139 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2140 | aom_wb_write_bit(wb, seg->enabled); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2141 | if (!seg->enabled) return; |
| 2142 | |
David Barker | 190b77a | 2018-03-16 14:29:46 +0000 | [diff] [blame] | 2143 | // Write update flags |
Cherma Rajan A | b4be7e5 | 2021-04-14 19:28:53 +0530 | [diff] [blame] | 2144 | if (cm->features.primary_ref_frame != PRIMARY_REF_NONE) { |
David Barker | 190b77a | 2018-03-16 14:29:46 +0000 | [diff] [blame] | 2145 | aom_wb_write_bit(wb, seg->update_map); |
Cherma Rajan A | b4be7e5 | 2021-04-14 19:28:53 +0530 | [diff] [blame] | 2146 | if (seg->update_map) aom_wb_write_bit(wb, seg->temporal_update); |
David Barker | 190b77a | 2018-03-16 14:29:46 +0000 | [diff] [blame] | 2147 | aom_wb_write_bit(wb, seg->update_data); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | // Segmentation data |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2151 | if (seg->update_data) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2152 | for (i = 0; i < MAX_SEGMENTS; i++) { |
| 2153 | for (j = 0; j < SEG_LVL_MAX; j++) { |
| 2154 | const int active = segfeature_active(seg, i, j); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2155 | aom_wb_write_bit(wb, active); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2156 | if (active) { |
Sebastien Alaiwan | ca14b47 | 2017-12-11 11:46:00 +0100 | [diff] [blame] | 2157 | const int data_max = av1_seg_feature_data_max(j); |
| 2158 | const int data_min = -data_max; |
| 2159 | const int ubits = get_unsigned_bits(data_max); |
| 2160 | const int data = clamp(get_segdata(seg, i, j), data_min, data_max); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2161 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2162 | if (av1_is_segfeature_signed(j)) { |
Sebastien Alaiwan | ca14b47 | 2017-12-11 11:46:00 +0100 | [diff] [blame] | 2163 | aom_wb_write_inv_signed_literal(wb, data, ubits); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2164 | } else { |
Sebastien Alaiwan | ca14b47 | 2017-12-11 11:46:00 +0100 | [diff] [blame] | 2165 | aom_wb_write_literal(wb, data, ubits); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2166 | } |
| 2167 | } |
| 2168 | } |
| 2169 | } |
| 2170 | } |
| 2171 | } |
| 2172 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2173 | static AOM_INLINE void write_frame_interp_filter( |
| 2174 | InterpFilter filter, struct aom_write_bit_buffer *wb) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2175 | aom_wb_write_bit(wb, filter == SWITCHABLE); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2176 | if (filter != SWITCHABLE) |
Angie Chiang | 6305abe | 2016-10-24 12:24:44 -0700 | [diff] [blame] | 2177 | aom_wb_write_literal(wb, filter, LOG_SWITCHABLE_FILTERS); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2178 | } |
| 2179 | |
Dominic Symes | db5d66f | 2017-08-18 18:11:34 +0200 | [diff] [blame] | 2180 | // Same function as write_uniform but writing to uncompresses header wb |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2181 | static AOM_INLINE void wb_write_uniform(struct aom_write_bit_buffer *wb, int n, |
| 2182 | int v) { |
Dominic Symes | db5d66f | 2017-08-18 18:11:34 +0200 | [diff] [blame] | 2183 | const int l = get_unsigned_bits(n); |
| 2184 | const int m = (1 << l) - n; |
| 2185 | if (l == 0) return; |
| 2186 | if (v < m) { |
| 2187 | aom_wb_write_literal(wb, v, l - 1); |
| 2188 | } else { |
| 2189 | aom_wb_write_literal(wb, m + ((v - m) >> 1), l - 1); |
| 2190 | aom_wb_write_literal(wb, (v - m) & 1, 1); |
| 2191 | } |
| 2192 | } |
| 2193 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2194 | static AOM_INLINE void write_tile_info_max_tile( |
| 2195 | const AV1_COMMON *const cm, struct aom_write_bit_buffer *wb) { |
Mudassir Galagnath | 77f3160 | 2022-04-19 16:28:25 +0530 | [diff] [blame] | 2196 | int width_sb = |
| 2197 | CEIL_POWER_OF_TWO(cm->mi_params.mi_cols, cm->seq_params->mib_size_log2); |
| 2198 | int height_sb = |
| 2199 | CEIL_POWER_OF_TWO(cm->mi_params.mi_rows, cm->seq_params->mib_size_log2); |
Dominic Symes | db5d66f | 2017-08-18 18:11:34 +0200 | [diff] [blame] | 2200 | int size_sb, i; |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 2201 | const CommonTileParams *const tiles = &cm->tiles; |
Dominic Symes | db5d66f | 2017-08-18 18:11:34 +0200 | [diff] [blame] | 2202 | |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 2203 | aom_wb_write_bit(wb, tiles->uniform_spacing); |
Dominic Symes | db5d66f | 2017-08-18 18:11:34 +0200 | [diff] [blame] | 2204 | |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 2205 | if (tiles->uniform_spacing) { |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 2206 | int ones = tiles->log2_cols - tiles->min_log2_cols; |
Dominic Symes | db5d66f | 2017-08-18 18:11:34 +0200 | [diff] [blame] | 2207 | while (ones--) { |
| 2208 | aom_wb_write_bit(wb, 1); |
| 2209 | } |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 2210 | if (tiles->log2_cols < tiles->max_log2_cols) { |
Dominic Symes | db5d66f | 2017-08-18 18:11:34 +0200 | [diff] [blame] | 2211 | aom_wb_write_bit(wb, 0); |
| 2212 | } |
| 2213 | |
| 2214 | // rows |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 2215 | ones = tiles->log2_rows - tiles->min_log2_rows; |
Dominic Symes | db5d66f | 2017-08-18 18:11:34 +0200 | [diff] [blame] | 2216 | while (ones--) { |
| 2217 | aom_wb_write_bit(wb, 1); |
| 2218 | } |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 2219 | if (tiles->log2_rows < tiles->max_log2_rows) { |
Dominic Symes | db5d66f | 2017-08-18 18:11:34 +0200 | [diff] [blame] | 2220 | aom_wb_write_bit(wb, 0); |
| 2221 | } |
| 2222 | } else { |
| 2223 | // Explicit tiles with configurable tile widths and heights |
| 2224 | // columns |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 2225 | for (i = 0; i < tiles->cols; i++) { |
| 2226 | size_sb = tiles->col_start_sb[i + 1] - tiles->col_start_sb[i]; |
| 2227 | wb_write_uniform(wb, AOMMIN(width_sb, tiles->max_width_sb), size_sb - 1); |
Dominic Symes | db5d66f | 2017-08-18 18:11:34 +0200 | [diff] [blame] | 2228 | width_sb -= size_sb; |
| 2229 | } |
| 2230 | assert(width_sb == 0); |
| 2231 | |
| 2232 | // rows |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 2233 | for (i = 0; i < tiles->rows; i++) { |
| 2234 | size_sb = tiles->row_start_sb[i + 1] - tiles->row_start_sb[i]; |
| 2235 | wb_write_uniform(wb, AOMMIN(height_sb, tiles->max_height_sb), |
Dominic Symes | db5d66f | 2017-08-18 18:11:34 +0200 | [diff] [blame] | 2236 | size_sb - 1); |
| 2237 | height_sb -= size_sb; |
| 2238 | } |
| 2239 | assert(height_sb == 0); |
| 2240 | } |
| 2241 | } |
Dominic Symes | db5d66f | 2017-08-18 18:11:34 +0200 | [diff] [blame] | 2242 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2243 | static AOM_INLINE void write_tile_info(const AV1_COMMON *const cm, |
| 2244 | struct aom_write_bit_buffer *saved_wb, |
| 2245 | struct aom_write_bit_buffer *wb) { |
Yunqing Wang | 445739a | 2018-06-10 12:27:34 -0700 | [diff] [blame] | 2246 | write_tile_info_max_tile(cm, wb); |
Fangwen Fu | 7b9f2b3 | 2017-01-17 14:01:52 -0800 | [diff] [blame] | 2247 | |
James Zern | 8b007ff | 2018-02-26 22:49:13 -0800 | [diff] [blame] | 2248 | *saved_wb = *wb; |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 2249 | if (cm->tiles.rows * cm->tiles.cols > 1) { |
Dominic Symes | a48289a | 2018-04-03 21:07:59 +0200 | [diff] [blame] | 2250 | // tile id used for cdf update |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 2251 | aom_wb_write_literal(wb, 0, cm->tiles.log2_cols + cm->tiles.log2_rows); |
James Zern | 9e9f7ad | 2018-03-02 17:38:53 -0800 | [diff] [blame] | 2252 | // Number of bytes in tile size - 1 |
| 2253 | aom_wb_write_literal(wb, 3, 2); |
| 2254 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2255 | } |
| 2256 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2257 | static AOM_INLINE void write_ext_tile_info( |
| 2258 | const AV1_COMMON *const cm, struct aom_write_bit_buffer *saved_wb, |
| 2259 | struct aom_write_bit_buffer *wb) { |
Yunqing Wang | 3f9c1e2 | 2018-06-16 16:48:15 -0700 | [diff] [blame] | 2260 | // This information is stored as a separate byte. |
| 2261 | int mod = wb->bit_offset % CHAR_BIT; |
| 2262 | if (mod > 0) aom_wb_write_literal(wb, 0, CHAR_BIT - mod); |
| 2263 | assert(aom_wb_is_byte_aligned(wb)); |
| 2264 | |
Yunqing Wang | 445739a | 2018-06-10 12:27:34 -0700 | [diff] [blame] | 2265 | *saved_wb = *wb; |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 2266 | if (cm->tiles.rows * cm->tiles.cols > 1) { |
Yunqing Wang | 445739a | 2018-06-10 12:27:34 -0700 | [diff] [blame] | 2267 | // Note that the last item in the uncompressed header is the data |
| 2268 | // describing tile configuration. |
| 2269 | // Number of bytes in tile column size - 1 |
| 2270 | aom_wb_write_literal(wb, 0, 2); |
| 2271 | // Number of bytes in tile size - 1 |
| 2272 | aom_wb_write_literal(wb, 0, 2); |
| 2273 | } |
| 2274 | } |
| 2275 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2276 | static INLINE int find_identical_tile( |
| 2277 | const int tile_row, const int tile_col, |
Wan-Teh Chang | 04891e5 | 2018-05-24 17:31:10 -0700 | [diff] [blame] | 2278 | TileBufferEnc (*const tile_buffers)[MAX_TILE_COLS]) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2279 | const MV32 candidate_offset[1] = { { 1, 0 } }; |
| 2280 | const uint8_t *const cur_tile_data = |
| 2281 | tile_buffers[tile_row][tile_col].data + 4; |
Jingning Han | 99ffce6 | 2017-04-25 15:48:41 -0700 | [diff] [blame] | 2282 | const size_t cur_tile_size = tile_buffers[tile_row][tile_col].size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2283 | |
| 2284 | int i; |
| 2285 | |
| 2286 | if (tile_row == 0) return 0; |
| 2287 | |
| 2288 | // (TODO: yunqingwang) For now, only above tile is checked and used. |
| 2289 | // More candidates such as left tile can be added later. |
| 2290 | for (i = 0; i < 1; i++) { |
| 2291 | int row_offset = candidate_offset[0].row; |
| 2292 | int col_offset = candidate_offset[0].col; |
| 2293 | int row = tile_row - row_offset; |
| 2294 | int col = tile_col - col_offset; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2295 | const uint8_t *tile_data; |
| 2296 | TileBufferEnc *candidate; |
| 2297 | |
| 2298 | if (row < 0 || col < 0) continue; |
| 2299 | |
David Turner | e7fea8e | 2018-12-10 15:57:05 +0000 | [diff] [blame] | 2300 | const uint32_t tile_hdr = mem_get_le32(tile_buffers[row][col].data); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2301 | |
David Turner | e7fea8e | 2018-12-10 15:57:05 +0000 | [diff] [blame] | 2302 | // Read out tile-copy-mode bit: |
| 2303 | if ((tile_hdr >> 31) == 1) { |
| 2304 | // The candidate is a copy tile itself: the offset is stored in bits |
| 2305 | // 30 through 24 inclusive. |
| 2306 | row_offset += (tile_hdr >> 24) & 0x7f; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2307 | row = tile_row - row_offset; |
| 2308 | } |
| 2309 | |
| 2310 | candidate = &tile_buffers[row][col]; |
| 2311 | |
| 2312 | if (row_offset >= 128 || candidate->size != cur_tile_size) continue; |
| 2313 | |
| 2314 | tile_data = candidate->data + 4; |
| 2315 | |
| 2316 | if (memcmp(tile_data, cur_tile_data, cur_tile_size) != 0) continue; |
| 2317 | |
| 2318 | // Identical tile found |
| 2319 | assert(row_offset > 0); |
| 2320 | return row_offset; |
| 2321 | } |
| 2322 | |
| 2323 | // No identical tile found |
| 2324 | return 0; |
| 2325 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2326 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2327 | static AOM_INLINE void write_render_size(const AV1_COMMON *cm, |
| 2328 | struct aom_write_bit_buffer *wb) { |
Cheng Chen | 09c83a5 | 2018-06-05 12:27:36 -0700 | [diff] [blame] | 2329 | const int scaling_active = av1_resize_scaled(cm); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2330 | aom_wb_write_bit(wb, scaling_active); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2331 | if (scaling_active) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2332 | aom_wb_write_literal(wb, cm->render_width - 1, 16); |
| 2333 | aom_wb_write_literal(wb, cm->render_height - 1, 16); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2334 | } |
| 2335 | } |
| 2336 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2337 | static AOM_INLINE void write_superres_scale(const AV1_COMMON *const cm, |
| 2338 | struct aom_write_bit_buffer *wb) { |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 2339 | const SequenceHeader *const seq_params = cm->seq_params; |
Urvang Joshi | 2c92b07 | 2018-03-19 17:23:31 -0700 | [diff] [blame] | 2340 | if (!seq_params->enable_superres) { |
| 2341 | assert(cm->superres_scale_denominator == SCALE_NUMERATOR); |
| 2342 | return; |
| 2343 | } |
| 2344 | |
Fergus Simpson | e750841 | 2017-03-14 18:14:09 -0700 | [diff] [blame] | 2345 | // First bit is whether to to scale or not |
Urvang Joshi | de71d14 | 2017-10-05 12:12:15 -0700 | [diff] [blame] | 2346 | if (cm->superres_scale_denominator == SCALE_NUMERATOR) { |
Fergus Simpson | e750841 | 2017-03-14 18:14:09 -0700 | [diff] [blame] | 2347 | aom_wb_write_bit(wb, 0); // no scaling |
| 2348 | } else { |
| 2349 | aom_wb_write_bit(wb, 1); // scaling, write scale factor |
Urvang Joshi | 8301018 | 2017-10-27 12:36:02 -0700 | [diff] [blame] | 2350 | assert(cm->superres_scale_denominator >= SUPERRES_SCALE_DENOMINATOR_MIN); |
| 2351 | assert(cm->superres_scale_denominator < |
| 2352 | SUPERRES_SCALE_DENOMINATOR_MIN + (1 << SUPERRES_SCALE_BITS)); |
Fergus Simpson | e750841 | 2017-03-14 18:14:09 -0700 | [diff] [blame] | 2353 | aom_wb_write_literal( |
Urvang Joshi | de71d14 | 2017-10-05 12:12:15 -0700 | [diff] [blame] | 2354 | wb, cm->superres_scale_denominator - SUPERRES_SCALE_DENOMINATOR_MIN, |
Fergus Simpson | e750841 | 2017-03-14 18:14:09 -0700 | [diff] [blame] | 2355 | SUPERRES_SCALE_BITS); |
| 2356 | } |
| 2357 | } |
Fergus Simpson | e750841 | 2017-03-14 18:14:09 -0700 | [diff] [blame] | 2358 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2359 | static AOM_INLINE void write_frame_size(const AV1_COMMON *cm, |
| 2360 | int frame_size_override, |
| 2361 | struct aom_write_bit_buffer *wb) { |
David Barker | 2217131 | 2017-11-20 11:26:04 +0000 | [diff] [blame] | 2362 | const int coded_width = cm->superres_upscaled_width - 1; |
| 2363 | const int coded_height = cm->superres_upscaled_height - 1; |
David Barker | 2217131 | 2017-11-20 11:26:04 +0000 | [diff] [blame] | 2364 | |
Arild Fuldseth (arilfuld) | 7193f02 | 2017-10-30 12:24:57 +0100 | [diff] [blame] | 2365 | if (frame_size_override) { |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 2366 | const SequenceHeader *seq_params = cm->seq_params; |
Arild Fuldseth (arilfuld) | 7193f02 | 2017-10-30 12:24:57 +0100 | [diff] [blame] | 2367 | int num_bits_width = seq_params->num_bits_width; |
| 2368 | int num_bits_height = seq_params->num_bits_height; |
David Barker | 2217131 | 2017-11-20 11:26:04 +0000 | [diff] [blame] | 2369 | aom_wb_write_literal(wb, coded_width, num_bits_width); |
| 2370 | aom_wb_write_literal(wb, coded_height, num_bits_height); |
Arild Fuldseth (arilfuld) | 7193f02 | 2017-10-30 12:24:57 +0100 | [diff] [blame] | 2371 | } |
David Barker | 2217131 | 2017-11-20 11:26:04 +0000 | [diff] [blame] | 2372 | |
David Barker | 2217131 | 2017-11-20 11:26:04 +0000 | [diff] [blame] | 2373 | write_superres_scale(cm, wb); |
Fergus Simpson | d2bcbb5 | 2017-05-22 23:15:05 -0700 | [diff] [blame] | 2374 | write_render_size(cm, wb); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2375 | } |
| 2376 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2377 | static AOM_INLINE void write_frame_size_with_refs( |
| 2378 | const AV1_COMMON *const cm, struct aom_write_bit_buffer *wb) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2379 | int found = 0; |
| 2380 | |
| 2381 | MV_REFERENCE_FRAME ref_frame; |
| 2382 | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
David Turner | a21966b | 2018-12-05 14:48:49 +0000 | [diff] [blame] | 2383 | const YV12_BUFFER_CONFIG *cfg = get_ref_frame_yv12_buf(cm, ref_frame); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2384 | |
| 2385 | if (cfg != NULL) { |
Fergus Simpson | d2bcbb5 | 2017-05-22 23:15:05 -0700 | [diff] [blame] | 2386 | found = cm->superres_upscaled_width == cfg->y_crop_width && |
| 2387 | cm->superres_upscaled_height == cfg->y_crop_height; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2388 | found &= cm->render_width == cfg->render_width && |
| 2389 | cm->render_height == cfg->render_height; |
| 2390 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2391 | aom_wb_write_bit(wb, found); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2392 | if (found) { |
Fergus Simpson | d2bcbb5 | 2017-05-22 23:15:05 -0700 | [diff] [blame] | 2393 | write_superres_scale(cm, wb); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2394 | break; |
| 2395 | } |
| 2396 | } |
| 2397 | |
Arild Fuldseth (arilfuld) | 7193f02 | 2017-10-30 12:24:57 +0100 | [diff] [blame] | 2398 | if (!found) { |
Tom Finegan | 8ab2bba | 2018-02-28 07:36:28 -0800 | [diff] [blame] | 2399 | int frame_size_override = 1; // Always equal to 1 in this function |
Arild Fuldseth (arilfuld) | 7193f02 | 2017-10-30 12:24:57 +0100 | [diff] [blame] | 2400 | write_frame_size(cm, frame_size_override, wb); |
| 2401 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2402 | } |
| 2403 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2404 | static AOM_INLINE void write_profile(BITSTREAM_PROFILE profile, |
| 2405 | struct aom_write_bit_buffer *wb) { |
Debargha Mukherjee | f9a50ea | 2018-01-09 22:28:20 -0800 | [diff] [blame] | 2406 | assert(profile >= PROFILE_0 && profile < MAX_PROFILES); |
Debargha Mukherjee | 53396fb | 2018-03-30 12:19:38 -0700 | [diff] [blame] | 2407 | aom_wb_write_literal(wb, profile, PROFILE_BITS); |
Debargha Mukherjee | f9a50ea | 2018-01-09 22:28:20 -0800 | [diff] [blame] | 2408 | } |
| 2409 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2410 | static AOM_INLINE void write_bitdepth(const SequenceHeader *const seq_params, |
| 2411 | struct aom_write_bit_buffer *wb) { |
Debargha Mukherjee | f9a50ea | 2018-01-09 22:28:20 -0800 | [diff] [blame] | 2412 | // Profile 0/1: [0] for 8 bit, [1] 10-bit |
| 2413 | // Profile 2: [0] for 8 bit, [10] 10-bit, [11] - 12-bit |
Urvang Joshi | 20cf30e | 2018-07-19 02:33:58 -0700 | [diff] [blame] | 2414 | aom_wb_write_bit(wb, seq_params->bit_depth == AOM_BITS_8 ? 0 : 1); |
| 2415 | if (seq_params->profile == PROFILE_2 && seq_params->bit_depth != AOM_BITS_8) { |
| 2416 | aom_wb_write_bit(wb, seq_params->bit_depth == AOM_BITS_10 ? 0 : 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2417 | } |
| 2418 | } |
| 2419 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2420 | static AOM_INLINE void write_color_config( |
| 2421 | const SequenceHeader *const seq_params, struct aom_write_bit_buffer *wb) { |
Urvang Joshi | 20cf30e | 2018-07-19 02:33:58 -0700 | [diff] [blame] | 2422 | write_bitdepth(seq_params, wb); |
| 2423 | const int is_monochrome = seq_params->monochrome; |
Debargha Mukherjee | f9a50ea | 2018-01-09 22:28:20 -0800 | [diff] [blame] | 2424 | // monochrome bit |
Urvang Joshi | 20cf30e | 2018-07-19 02:33:58 -0700 | [diff] [blame] | 2425 | if (seq_params->profile != PROFILE_1) |
Debargha Mukherjee | f9a50ea | 2018-01-09 22:28:20 -0800 | [diff] [blame] | 2426 | aom_wb_write_bit(wb, is_monochrome); |
| 2427 | else |
| 2428 | assert(!is_monochrome); |
Urvang Joshi | 20cf30e | 2018-07-19 02:33:58 -0700 | [diff] [blame] | 2429 | if (seq_params->color_primaries == AOM_CICP_CP_UNSPECIFIED && |
| 2430 | seq_params->transfer_characteristics == AOM_CICP_TC_UNSPECIFIED && |
| 2431 | seq_params->matrix_coefficients == AOM_CICP_MC_UNSPECIFIED) { |
Andrey Norkin | 9e69463 | 2017-12-21 18:50:57 -0800 | [diff] [blame] | 2432 | aom_wb_write_bit(wb, 0); // No color description present |
| 2433 | } else { |
| 2434 | aom_wb_write_bit(wb, 1); // Color description present |
Urvang Joshi | 20cf30e | 2018-07-19 02:33:58 -0700 | [diff] [blame] | 2435 | aom_wb_write_literal(wb, seq_params->color_primaries, 8); |
| 2436 | aom_wb_write_literal(wb, seq_params->transfer_characteristics, 8); |
| 2437 | aom_wb_write_literal(wb, seq_params->matrix_coefficients, 8); |
Andrey Norkin | 9e69463 | 2017-12-21 18:50:57 -0800 | [diff] [blame] | 2438 | } |
Debargha Mukherjee | 085095d | 2018-03-30 12:39:56 -0700 | [diff] [blame] | 2439 | if (is_monochrome) { |
| 2440 | // 0: [16, 235] (i.e. xvYCC), 1: [0, 255] |
Urvang Joshi | 20cf30e | 2018-07-19 02:33:58 -0700 | [diff] [blame] | 2441 | aom_wb_write_bit(wb, seq_params->color_range); |
Debargha Mukherjee | 085095d | 2018-03-30 12:39:56 -0700 | [diff] [blame] | 2442 | return; |
| 2443 | } |
Urvang Joshi | 20cf30e | 2018-07-19 02:33:58 -0700 | [diff] [blame] | 2444 | if (seq_params->color_primaries == AOM_CICP_CP_BT_709 && |
| 2445 | seq_params->transfer_characteristics == AOM_CICP_TC_SRGB && |
Debargha Mukherjee | ffffc56 | 2018-10-02 09:02:25 -0700 | [diff] [blame] | 2446 | seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY) { |
Urvang Joshi | 20cf30e | 2018-07-19 02:33:58 -0700 | [diff] [blame] | 2447 | assert(seq_params->subsampling_x == 0 && seq_params->subsampling_y == 0); |
| 2448 | assert(seq_params->profile == PROFILE_1 || |
| 2449 | (seq_params->profile == PROFILE_2 && |
| 2450 | seq_params->bit_depth == AOM_BITS_12)); |
Imdad Sardharwalla | 317002f | 2017-12-05 16:24:56 +0000 | [diff] [blame] | 2451 | } else { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2452 | // 0: [16, 235] (i.e. xvYCC), 1: [0, 255] |
Urvang Joshi | 20cf30e | 2018-07-19 02:33:58 -0700 | [diff] [blame] | 2453 | aom_wb_write_bit(wb, seq_params->color_range); |
| 2454 | if (seq_params->profile == PROFILE_0) { |
Debargha Mukherjee | f9a50ea | 2018-01-09 22:28:20 -0800 | [diff] [blame] | 2455 | // 420 only |
Urvang Joshi | 20cf30e | 2018-07-19 02:33:58 -0700 | [diff] [blame] | 2456 | assert(seq_params->subsampling_x == 1 && seq_params->subsampling_y == 1); |
| 2457 | } else if (seq_params->profile == PROFILE_1) { |
Debargha Mukherjee | f9a50ea | 2018-01-09 22:28:20 -0800 | [diff] [blame] | 2458 | // 444 only |
Urvang Joshi | 20cf30e | 2018-07-19 02:33:58 -0700 | [diff] [blame] | 2459 | assert(seq_params->subsampling_x == 0 && seq_params->subsampling_y == 0); |
| 2460 | } else if (seq_params->profile == PROFILE_2) { |
| 2461 | if (seq_params->bit_depth == AOM_BITS_12) { |
Debargha Mukherjee | f9a50ea | 2018-01-09 22:28:20 -0800 | [diff] [blame] | 2462 | // 420, 444 or 422 |
Urvang Joshi | 20cf30e | 2018-07-19 02:33:58 -0700 | [diff] [blame] | 2463 | aom_wb_write_bit(wb, seq_params->subsampling_x); |
| 2464 | if (seq_params->subsampling_x == 0) { |
| 2465 | assert(seq_params->subsampling_y == 0 && |
David Barker | 0c3545b | 2018-01-16 17:32:23 +0000 | [diff] [blame] | 2466 | "4:4:0 subsampling not allowed in AV1"); |
| 2467 | } else { |
Urvang Joshi | 20cf30e | 2018-07-19 02:33:58 -0700 | [diff] [blame] | 2468 | aom_wb_write_bit(wb, seq_params->subsampling_y); |
David Barker | 0c3545b | 2018-01-16 17:32:23 +0000 | [diff] [blame] | 2469 | } |
Debargha Mukherjee | f9a50ea | 2018-01-09 22:28:20 -0800 | [diff] [blame] | 2470 | } else { |
| 2471 | // 422 only |
Urvang Joshi | 20cf30e | 2018-07-19 02:33:58 -0700 | [diff] [blame] | 2472 | assert(seq_params->subsampling_x == 1 && |
| 2473 | seq_params->subsampling_y == 0); |
Debargha Mukherjee | f9a50ea | 2018-01-09 22:28:20 -0800 | [diff] [blame] | 2474 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2475 | } |
Urvang Joshi | 20cf30e | 2018-07-19 02:33:58 -0700 | [diff] [blame] | 2476 | if (seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY) { |
| 2477 | assert(seq_params->subsampling_x == 0 && seq_params->subsampling_y == 0); |
Debargha Mukherjee | f61c0d1 | 2018-03-30 19:29:59 -0700 | [diff] [blame] | 2478 | } |
Urvang Joshi | 20cf30e | 2018-07-19 02:33:58 -0700 | [diff] [blame] | 2479 | if (seq_params->subsampling_x == 1 && seq_params->subsampling_y == 1) { |
| 2480 | aom_wb_write_literal(wb, seq_params->chroma_sample_position, 2); |
anorkin | 76fb126 | 2017-03-22 15:12:12 -0700 | [diff] [blame] | 2481 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2482 | } |
Urvang Joshi | 20cf30e | 2018-07-19 02:33:58 -0700 | [diff] [blame] | 2483 | aom_wb_write_bit(wb, seq_params->separate_uv_delta_q); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2484 | } |
| 2485 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2486 | static AOM_INLINE void write_timing_info_header( |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 2487 | const aom_timing_info_t *const timing_info, |
| 2488 | struct aom_write_bit_buffer *wb) { |
| 2489 | aom_wb_write_unsigned_literal(wb, timing_info->num_units_in_display_tick, 32); |
| 2490 | aom_wb_write_unsigned_literal(wb, timing_info->time_scale, 32); |
| 2491 | aom_wb_write_bit(wb, timing_info->equal_picture_interval); |
| 2492 | if (timing_info->equal_picture_interval) { |
| 2493 | aom_wb_write_uvlc(wb, timing_info->num_ticks_per_picture - 1); |
Andrey Norkin | 28e9ce2 | 2018-01-08 10:11:21 -0800 | [diff] [blame] | 2494 | } |
| 2495 | } |
Andrey Norkin | 28e9ce2 | 2018-01-08 10:11:21 -0800 | [diff] [blame] | 2496 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2497 | static AOM_INLINE void write_decoder_model_info( |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 2498 | const aom_dec_model_info_t *const decoder_model_info, |
| 2499 | struct aom_write_bit_buffer *wb) { |
Andrey Norkin | 795ba87 | 2018-03-06 13:24:14 -0800 | [diff] [blame] | 2500 | aom_wb_write_literal( |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 2501 | wb, decoder_model_info->encoder_decoder_buffer_delay_length - 1, 5); |
| 2502 | aom_wb_write_unsigned_literal( |
| 2503 | wb, decoder_model_info->num_units_in_decoding_tick, 32); |
| 2504 | aom_wb_write_literal(wb, decoder_model_info->buffer_removal_time_length - 1, |
Andrey Norkin | 795ba87 | 2018-03-06 13:24:14 -0800 | [diff] [blame] | 2505 | 5); |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 2506 | aom_wb_write_literal( |
| 2507 | wb, decoder_model_info->frame_presentation_time_length - 1, 5); |
Andrey Norkin | 795ba87 | 2018-03-06 13:24:14 -0800 | [diff] [blame] | 2508 | } |
| 2509 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2510 | static AOM_INLINE void write_dec_model_op_parameters( |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 2511 | const aom_dec_model_op_parameters_t *op_params, int buffer_delay_length, |
| 2512 | struct aom_write_bit_buffer *wb) { |
| 2513 | aom_wb_write_unsigned_literal(wb, op_params->decoder_buffer_delay, |
| 2514 | buffer_delay_length); |
| 2515 | aom_wb_write_unsigned_literal(wb, op_params->encoder_buffer_delay, |
| 2516 | buffer_delay_length); |
| 2517 | aom_wb_write_bit(wb, op_params->low_delay_mode_flag); |
Andrey Norkin | 795ba87 | 2018-03-06 13:24:14 -0800 | [diff] [blame] | 2518 | } |
| 2519 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2520 | static AOM_INLINE void write_tu_pts_info(AV1_COMMON *const cm, |
| 2521 | struct aom_write_bit_buffer *wb) { |
Andrey Norkin | 795ba87 | 2018-03-06 13:24:14 -0800 | [diff] [blame] | 2522 | aom_wb_write_unsigned_literal( |
Wan-Teh Chang | f64b3bc | 2018-07-02 09:42:39 -0700 | [diff] [blame] | 2523 | wb, cm->frame_presentation_time, |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 2524 | cm->seq_params->decoder_model_info.frame_presentation_time_length); |
Andrey Norkin | 795ba87 | 2018-03-06 13:24:14 -0800 | [diff] [blame] | 2525 | } |
Andrey Norkin | 795ba87 | 2018-03-06 13:24:14 -0800 | [diff] [blame] | 2526 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2527 | static AOM_INLINE void write_film_grain_params( |
| 2528 | const AV1_COMP *const cpi, struct aom_write_bit_buffer *wb) { |
David Turner | 08f909c | 2018-12-18 13:29:14 +0000 | [diff] [blame] | 2529 | const AV1_COMMON *const cm = &cpi->common; |
| 2530 | const aom_film_grain_t *const pars = &cm->cur_frame->film_grain_params; |
Andrey Norkin | 6f1c2f7 | 2018-01-15 20:08:52 -0800 | [diff] [blame] | 2531 | aom_wb_write_bit(wb, pars->apply_grain); |
| 2532 | if (!pars->apply_grain) return; |
| 2533 | |
| 2534 | aom_wb_write_literal(wb, pars->random_seed, 16); |
| 2535 | |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 2536 | if (cm->current_frame.frame_type == INTER_FRAME) |
Andrey Norkin | 879488f | 2018-02-28 15:30:26 -0800 | [diff] [blame] | 2537 | aom_wb_write_bit(wb, pars->update_parameters); |
David Turner | 08f909c | 2018-12-18 13:29:14 +0000 | [diff] [blame] | 2538 | |
Dominic Symes | d492901 | 2018-01-31 17:32:01 +0100 | [diff] [blame] | 2539 | if (!pars->update_parameters) { |
David Turner | e7ebf90 | 2018-12-04 14:04:55 +0000 | [diff] [blame] | 2540 | int ref_frame, ref_idx; |
Zoe Liu | 27deb38 | 2018-03-27 15:13:56 -0700 | [diff] [blame] | 2541 | for (ref_frame = LAST_FRAME; ref_frame < REF_FRAMES; ref_frame++) { |
David Turner | a21966b | 2018-12-05 14:48:49 +0000 | [diff] [blame] | 2542 | ref_idx = get_ref_frame_map_idx(cm, ref_frame); |
Dominic Symes | 4d37568 | 2018-02-28 17:26:04 +0100 | [diff] [blame] | 2543 | assert(ref_idx != INVALID_IDX); |
David Turner | e7ebf90 | 2018-12-04 14:04:55 +0000 | [diff] [blame] | 2544 | const RefCntBuffer *const buf = cm->ref_frame_map[ref_idx]; |
| 2545 | if (buf->film_grain_params_present && |
Yaowu Xu | 754f646 | 2021-07-12 14:00:23 -0700 | [diff] [blame] | 2546 | aom_check_grain_params_equiv(pars, &buf->film_grain_params)) { |
Dominic Symes | d492901 | 2018-01-31 17:32:01 +0100 | [diff] [blame] | 2547 | break; |
| 2548 | } |
| 2549 | } |
Zoe Liu | 27deb38 | 2018-03-27 15:13:56 -0700 | [diff] [blame] | 2550 | assert(ref_frame < REF_FRAMES); |
Dominic Symes | d492901 | 2018-01-31 17:32:01 +0100 | [diff] [blame] | 2551 | aom_wb_write_literal(wb, ref_idx, 3); |
| 2552 | return; |
| 2553 | } |
Andrey Norkin | 6f1c2f7 | 2018-01-15 20:08:52 -0800 | [diff] [blame] | 2554 | |
| 2555 | // Scaling functions parameters |
Andrey Norkin | 6f1c2f7 | 2018-01-15 20:08:52 -0800 | [diff] [blame] | 2556 | aom_wb_write_literal(wb, pars->num_y_points, 4); // max 14 |
| 2557 | for (int i = 0; i < pars->num_y_points; i++) { |
| 2558 | aom_wb_write_literal(wb, pars->scaling_points_y[i][0], 8); |
| 2559 | aom_wb_write_literal(wb, pars->scaling_points_y[i][1], 8); |
| 2560 | } |
| 2561 | |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 2562 | if (!cm->seq_params->monochrome) { |
Andrey Norkin | 20be545 | 2018-02-20 17:46:13 -0800 | [diff] [blame] | 2563 | aom_wb_write_bit(wb, pars->chroma_scaling_from_luma); |
David Turner | 08f909c | 2018-12-18 13:29:14 +0000 | [diff] [blame] | 2564 | } else { |
| 2565 | assert(!pars->chroma_scaling_from_luma); |
| 2566 | } |
Andrey Norkin | 6f1c2f7 | 2018-01-15 20:08:52 -0800 | [diff] [blame] | 2567 | |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 2568 | if (cm->seq_params->monochrome || pars->chroma_scaling_from_luma || |
| 2569 | ((cm->seq_params->subsampling_x == 1) && |
| 2570 | (cm->seq_params->subsampling_y == 1) && (pars->num_y_points == 0))) { |
David Turner | 08f909c | 2018-12-18 13:29:14 +0000 | [diff] [blame] | 2571 | assert(pars->num_cb_points == 0 && pars->num_cr_points == 0); |
Andrey Norkin | 0c294fa | 2018-02-16 18:32:12 -0800 | [diff] [blame] | 2572 | } else { |
Andrey Norkin | 6f1c2f7 | 2018-01-15 20:08:52 -0800 | [diff] [blame] | 2573 | aom_wb_write_literal(wb, pars->num_cb_points, 4); // max 10 |
| 2574 | for (int i = 0; i < pars->num_cb_points; i++) { |
| 2575 | aom_wb_write_literal(wb, pars->scaling_points_cb[i][0], 8); |
| 2576 | aom_wb_write_literal(wb, pars->scaling_points_cb[i][1], 8); |
| 2577 | } |
| 2578 | |
| 2579 | aom_wb_write_literal(wb, pars->num_cr_points, 4); // max 10 |
| 2580 | for (int i = 0; i < pars->num_cr_points; i++) { |
| 2581 | aom_wb_write_literal(wb, pars->scaling_points_cr[i][0], 8); |
| 2582 | aom_wb_write_literal(wb, pars->scaling_points_cr[i][1], 8); |
| 2583 | } |
| 2584 | } |
| 2585 | |
| 2586 | aom_wb_write_literal(wb, pars->scaling_shift - 8, 2); // 8 + value |
| 2587 | |
| 2588 | // AR coefficients |
| 2589 | // Only sent if the corresponsing scaling function has |
| 2590 | // more than 0 points |
| 2591 | |
| 2592 | aom_wb_write_literal(wb, pars->ar_coeff_lag, 2); |
| 2593 | |
| 2594 | int num_pos_luma = 2 * pars->ar_coeff_lag * (pars->ar_coeff_lag + 1); |
Andrey Norkin | 20be545 | 2018-02-20 17:46:13 -0800 | [diff] [blame] | 2595 | int num_pos_chroma = num_pos_luma; |
| 2596 | if (pars->num_y_points > 0) ++num_pos_chroma; |
Andrey Norkin | 6f1c2f7 | 2018-01-15 20:08:52 -0800 | [diff] [blame] | 2597 | |
| 2598 | if (pars->num_y_points) |
| 2599 | for (int i = 0; i < num_pos_luma; i++) |
| 2600 | aom_wb_write_literal(wb, pars->ar_coeffs_y[i] + 128, 8); |
| 2601 | |
| 2602 | if (pars->num_cb_points || pars->chroma_scaling_from_luma) |
| 2603 | for (int i = 0; i < num_pos_chroma; i++) |
| 2604 | aom_wb_write_literal(wb, pars->ar_coeffs_cb[i] + 128, 8); |
| 2605 | |
| 2606 | if (pars->num_cr_points || pars->chroma_scaling_from_luma) |
| 2607 | for (int i = 0; i < num_pos_chroma; i++) |
| 2608 | aom_wb_write_literal(wb, pars->ar_coeffs_cr[i] + 128, 8); |
| 2609 | |
| 2610 | aom_wb_write_literal(wb, pars->ar_coeff_shift - 6, 2); // 8 + value |
| 2611 | |
Andrey Norkin | a840cde | 2018-02-16 15:39:50 -0800 | [diff] [blame] | 2612 | aom_wb_write_literal(wb, pars->grain_scale_shift, 2); |
| 2613 | |
Andrey Norkin | 6f1c2f7 | 2018-01-15 20:08:52 -0800 | [diff] [blame] | 2614 | if (pars->num_cb_points) { |
| 2615 | aom_wb_write_literal(wb, pars->cb_mult, 8); |
| 2616 | aom_wb_write_literal(wb, pars->cb_luma_mult, 8); |
| 2617 | aom_wb_write_literal(wb, pars->cb_offset, 9); |
| 2618 | } |
| 2619 | |
| 2620 | if (pars->num_cr_points) { |
| 2621 | aom_wb_write_literal(wb, pars->cr_mult, 8); |
| 2622 | aom_wb_write_literal(wb, pars->cr_luma_mult, 8); |
| 2623 | aom_wb_write_literal(wb, pars->cr_offset, 9); |
| 2624 | } |
| 2625 | |
| 2626 | aom_wb_write_bit(wb, pars->overlap_flag); |
| 2627 | |
| 2628 | aom_wb_write_bit(wb, pars->clip_to_restricted_range); |
| 2629 | } |
Andrey Norkin | 6f1c2f7 | 2018-01-15 20:08:52 -0800 | [diff] [blame] | 2630 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2631 | static AOM_INLINE void write_sb_size(const SequenceHeader *const seq_params, |
| 2632 | struct aom_write_bit_buffer *wb) { |
Imdad Sardharwalla | 4ec84ab | 2018-02-06 12:20:18 +0000 | [diff] [blame] | 2633 | (void)seq_params; |
| 2634 | (void)wb; |
| 2635 | assert(seq_params->mib_size == mi_size_wide[seq_params->sb_size]); |
| 2636 | assert(seq_params->mib_size == 1 << seq_params->mib_size_log2); |
Imdad Sardharwalla | 4ec84ab | 2018-02-06 12:20:18 +0000 | [diff] [blame] | 2637 | assert(seq_params->sb_size == BLOCK_128X128 || |
| 2638 | seq_params->sb_size == BLOCK_64X64); |
| 2639 | aom_wb_write_bit(wb, seq_params->sb_size == BLOCK_128X128 ? 1 : 0); |
Imdad Sardharwalla | 4ec84ab | 2018-02-06 12:20:18 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2642 | static AOM_INLINE void write_sequence_header( |
| 2643 | const SequenceHeader *const seq_params, struct aom_write_bit_buffer *wb) { |
David Turner | 760a2f4 | 2018-12-07 15:25:36 +0000 | [diff] [blame] | 2644 | aom_wb_write_literal(wb, seq_params->num_bits_width - 1, 4); |
| 2645 | aom_wb_write_literal(wb, seq_params->num_bits_height - 1, 4); |
| 2646 | aom_wb_write_literal(wb, seq_params->max_frame_width - 1, |
| 2647 | seq_params->num_bits_width); |
| 2648 | aom_wb_write_literal(wb, seq_params->max_frame_height - 1, |
| 2649 | seq_params->num_bits_height); |
Arild Fuldseth (arilfuld) | 7193f02 | 2017-10-30 12:24:57 +0100 | [diff] [blame] | 2650 | |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2651 | if (!seq_params->reduced_still_picture_hdr) { |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2652 | aom_wb_write_bit(wb, seq_params->frame_id_numbers_present_flag); |
| 2653 | if (seq_params->frame_id_numbers_present_flag) { |
| 2654 | // We must always have delta_frame_id_length < frame_id_length, |
| 2655 | // in order for a frame to be referenced with a unique delta. |
| 2656 | // Avoid wasting bits by using a coding that enforces this restriction. |
| 2657 | aom_wb_write_literal(wb, seq_params->delta_frame_id_length - 2, 4); |
| 2658 | aom_wb_write_literal( |
| 2659 | wb, |
| 2660 | seq_params->frame_id_length - seq_params->delta_frame_id_length - 1, |
| 2661 | 3); |
| 2662 | } |
David Barker | 5e70a11 | 2017-10-03 14:28:17 +0100 | [diff] [blame] | 2663 | } |
Imdad Sardharwalla | 4ec84ab | 2018-02-06 12:20:18 +0000 | [diff] [blame] | 2664 | |
| 2665 | write_sb_size(seq_params, wb); |
Imdad Sardharwalla | bf2cc01 | 2018-02-09 17:32:10 +0000 | [diff] [blame] | 2666 | |
Debargha Mukherjee | 365eae8 | 2018-03-26 19:19:55 -0700 | [diff] [blame] | 2667 | aom_wb_write_bit(wb, seq_params->enable_filter_intra); |
| 2668 | aom_wb_write_bit(wb, seq_params->enable_intra_edge_filter); |
| 2669 | |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2670 | if (!seq_params->reduced_still_picture_hdr) { |
| 2671 | aom_wb_write_bit(wb, seq_params->enable_interintra_compound); |
| 2672 | aom_wb_write_bit(wb, seq_params->enable_masked_compound); |
| 2673 | aom_wb_write_bit(wb, seq_params->enable_warped_motion); |
| 2674 | aom_wb_write_bit(wb, seq_params->enable_dual_filter); |
Jingning Han | 127c823 | 2018-02-22 16:54:13 -0800 | [diff] [blame] | 2675 | |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 2676 | aom_wb_write_bit(wb, seq_params->order_hint_info.enable_order_hint); |
Cheng Chen | fecd9a7 | 2018-03-08 15:23:51 -0800 | [diff] [blame] | 2677 | |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 2678 | if (seq_params->order_hint_info.enable_order_hint) { |
Debargha Mukherjee | 7ac3eb1 | 2018-12-12 10:26:50 -0800 | [diff] [blame] | 2679 | aom_wb_write_bit(wb, seq_params->order_hint_info.enable_dist_wtd_comp); |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 2680 | aom_wb_write_bit(wb, seq_params->order_hint_info.enable_ref_frame_mvs); |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2681 | } |
| 2682 | if (seq_params->force_screen_content_tools == 2) { |
Imdad Sardharwalla | bf2cc01 | 2018-02-09 17:32:10 +0000 | [diff] [blame] | 2683 | aom_wb_write_bit(wb, 1); |
| 2684 | } else { |
| 2685 | aom_wb_write_bit(wb, 0); |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2686 | aom_wb_write_bit(wb, seq_params->force_screen_content_tools); |
Imdad Sardharwalla | bf2cc01 | 2018-02-09 17:32:10 +0000 | [diff] [blame] | 2687 | } |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2688 | if (seq_params->force_screen_content_tools > 0) { |
| 2689 | if (seq_params->force_integer_mv == 2) { |
| 2690 | aom_wb_write_bit(wb, 1); |
| 2691 | } else { |
| 2692 | aom_wb_write_bit(wb, 0); |
| 2693 | aom_wb_write_bit(wb, seq_params->force_integer_mv); |
| 2694 | } |
| 2695 | } else { |
| 2696 | assert(seq_params->force_integer_mv == 2); |
| 2697 | } |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 2698 | if (seq_params->order_hint_info.enable_order_hint) |
| 2699 | aom_wb_write_literal( |
| 2700 | wb, seq_params->order_hint_info.order_hint_bits_minus_1, 3); |
Imdad Sardharwalla | bf2cc01 | 2018-02-09 17:32:10 +0000 | [diff] [blame] | 2701 | } |
Imdad Sardharwalla | e68aa8a | 2018-03-07 18:52:54 +0000 | [diff] [blame] | 2702 | |
Urvang Joshi | 2c92b07 | 2018-03-19 17:23:31 -0700 | [diff] [blame] | 2703 | aom_wb_write_bit(wb, seq_params->enable_superres); |
Debargha Mukherjee | 98a311c | 2018-03-25 16:33:11 -0700 | [diff] [blame] | 2704 | aom_wb_write_bit(wb, seq_params->enable_cdef); |
| 2705 | aom_wb_write_bit(wb, seq_params->enable_restoration); |
Arild Fuldseth (arilfuld) | 5114b7b | 2016-11-09 13:32:54 +0100 | [diff] [blame] | 2706 | } |
Arild Fuldseth (arilfuld) | 5114b7b | 2016-11-09 13:32:54 +0100 | [diff] [blame] | 2707 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2708 | static AOM_INLINE void write_global_motion_params( |
| 2709 | const WarpedMotionParams *params, const WarpedMotionParams *ref_params, |
| 2710 | struct aom_write_bit_buffer *wb, int allow_hp) { |
Sebastien Alaiwan | e4984ff | 2017-10-31 15:27:44 +0100 | [diff] [blame] | 2711 | const TransformationType type = params->wmtype; |
Sarah Parker | 3e579a6 | 2017-08-23 16:53:20 -0700 | [diff] [blame] | 2712 | |
Rachel Barker | 6496e8d | 2022-09-23 09:40:02 +0000 | [diff] [blame] | 2713 | // As a workaround for an AV1 spec bug, we avoid choosing TRANSLATION |
| 2714 | // type models. Check here that we don't accidentally pick one somehow. |
| 2715 | // See comments in gm_get_motion_vector() for details on the bug we're |
| 2716 | // working around here |
| 2717 | assert(type != TRANSLATION); |
| 2718 | |
Sarah Parker | 3e579a6 | 2017-08-23 16:53:20 -0700 | [diff] [blame] | 2719 | aom_wb_write_bit(wb, type != IDENTITY); |
| 2720 | if (type != IDENTITY) { |
Sarah Parker | 3e579a6 | 2017-08-23 16:53:20 -0700 | [diff] [blame] | 2721 | aom_wb_write_bit(wb, type == ROTZOOM); |
| 2722 | if (type != ROTZOOM) aom_wb_write_bit(wb, type == TRANSLATION); |
Sarah Parker | 3e579a6 | 2017-08-23 16:53:20 -0700 | [diff] [blame] | 2723 | } |
| 2724 | |
Sebastien Alaiwan | e4984ff | 2017-10-31 15:27:44 +0100 | [diff] [blame] | 2725 | if (type >= ROTZOOM) { |
| 2726 | aom_wb_write_signed_primitive_refsubexpfin( |
| 2727 | wb, GM_ALPHA_MAX + 1, SUBEXPFIN_K, |
| 2728 | (ref_params->wmmat[2] >> GM_ALPHA_PREC_DIFF) - |
| 2729 | (1 << GM_ALPHA_PREC_BITS), |
| 2730 | (params->wmmat[2] >> GM_ALPHA_PREC_DIFF) - (1 << GM_ALPHA_PREC_BITS)); |
| 2731 | aom_wb_write_signed_primitive_refsubexpfin( |
| 2732 | wb, GM_ALPHA_MAX + 1, SUBEXPFIN_K, |
| 2733 | (ref_params->wmmat[3] >> GM_ALPHA_PREC_DIFF), |
| 2734 | (params->wmmat[3] >> GM_ALPHA_PREC_DIFF)); |
| 2735 | } |
| 2736 | |
| 2737 | if (type >= AFFINE) { |
| 2738 | aom_wb_write_signed_primitive_refsubexpfin( |
| 2739 | wb, GM_ALPHA_MAX + 1, SUBEXPFIN_K, |
| 2740 | (ref_params->wmmat[4] >> GM_ALPHA_PREC_DIFF), |
| 2741 | (params->wmmat[4] >> GM_ALPHA_PREC_DIFF)); |
| 2742 | aom_wb_write_signed_primitive_refsubexpfin( |
| 2743 | wb, GM_ALPHA_MAX + 1, SUBEXPFIN_K, |
| 2744 | (ref_params->wmmat[5] >> GM_ALPHA_PREC_DIFF) - |
| 2745 | (1 << GM_ALPHA_PREC_BITS), |
| 2746 | (params->wmmat[5] >> GM_ALPHA_PREC_DIFF) - (1 << GM_ALPHA_PREC_BITS)); |
| 2747 | } |
| 2748 | |
| 2749 | if (type >= TRANSLATION) { |
| 2750 | const int trans_bits = (type == TRANSLATION) |
| 2751 | ? GM_ABS_TRANS_ONLY_BITS - !allow_hp |
| 2752 | : GM_ABS_TRANS_BITS; |
| 2753 | const int trans_prec_diff = (type == TRANSLATION) |
| 2754 | ? GM_TRANS_ONLY_PREC_DIFF + !allow_hp |
| 2755 | : GM_TRANS_PREC_DIFF; |
| 2756 | aom_wb_write_signed_primitive_refsubexpfin( |
| 2757 | wb, (1 << trans_bits) + 1, SUBEXPFIN_K, |
| 2758 | (ref_params->wmmat[0] >> trans_prec_diff), |
| 2759 | (params->wmmat[0] >> trans_prec_diff)); |
| 2760 | aom_wb_write_signed_primitive_refsubexpfin( |
| 2761 | wb, (1 << trans_bits) + 1, SUBEXPFIN_K, |
| 2762 | (ref_params->wmmat[1] >> trans_prec_diff), |
| 2763 | (params->wmmat[1] >> trans_prec_diff)); |
Sarah Parker | 3e579a6 | 2017-08-23 16:53:20 -0700 | [diff] [blame] | 2764 | } |
| 2765 | } |
| 2766 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2767 | static AOM_INLINE void write_global_motion(AV1_COMP *cpi, |
| 2768 | struct aom_write_bit_buffer *wb) { |
Sarah Parker | 3e579a6 | 2017-08-23 16:53:20 -0700 | [diff] [blame] | 2769 | AV1_COMMON *const cm = &cpi->common; |
| 2770 | int frame; |
| 2771 | for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) { |
David Barker | d7c8bd5 | 2017-09-25 14:47:29 +0100 | [diff] [blame] | 2772 | const WarpedMotionParams *ref_params = |
David Barker | cc615a8 | 2018-03-19 14:38:51 +0000 | [diff] [blame] | 2773 | cm->prev_frame ? &cm->prev_frame->global_motion[frame] |
| 2774 | : &default_warp_params; |
David Barker | d7c8bd5 | 2017-09-25 14:47:29 +0100 | [diff] [blame] | 2775 | write_global_motion_params(&cm->global_motion[frame], ref_params, wb, |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 2776 | cm->features.allow_high_precision_mv); |
Sarah Parker | 3e579a6 | 2017-08-23 16:53:20 -0700 | [diff] [blame] | 2777 | // TODO(sarahparker, debargha): The logic in the commented out code below |
| 2778 | // does not work currently and causes mismatches when resize is on. |
| 2779 | // Fix it before turning the optimization back on. |
| 2780 | /* |
David Turner | a21966b | 2018-12-05 14:48:49 +0000 | [diff] [blame] | 2781 | YV12_BUFFER_CONFIG *ref_buf = get_ref_frame_yv12_buf(cpi, frame); |
Sarah Parker | 3e579a6 | 2017-08-23 16:53:20 -0700 | [diff] [blame] | 2782 | if (cpi->source->y_crop_width == ref_buf->y_crop_width && |
| 2783 | cpi->source->y_crop_height == ref_buf->y_crop_height) { |
| 2784 | write_global_motion_params(&cm->global_motion[frame], |
| 2785 | &cm->prev_frame->global_motion[frame], wb, |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 2786 | cm->features.allow_high_precision_mv); |
Sarah Parker | 3e579a6 | 2017-08-23 16:53:20 -0700 | [diff] [blame] | 2787 | } else { |
| 2788 | assert(cm->global_motion[frame].wmtype == IDENTITY && |
| 2789 | "Invalid warp type for frames of different resolutions"); |
| 2790 | } |
| 2791 | */ |
| 2792 | /* |
| 2793 | printf("Frame %d/%d: Enc Ref %d: %d %d %d %d\n", |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 2794 | cm->current_frame.frame_number, cm->show_frame, frame, |
Sarah Parker | 3e579a6 | 2017-08-23 16:53:20 -0700 | [diff] [blame] | 2795 | cm->global_motion[frame].wmmat[0], |
| 2796 | cm->global_motion[frame].wmmat[1], cm->global_motion[frame].wmmat[2], |
| 2797 | cm->global_motion[frame].wmmat[3]); |
| 2798 | */ |
| 2799 | } |
| 2800 | } |
Sarah Parker | 3e579a6 | 2017-08-23 16:53:20 -0700 | [diff] [blame] | 2801 | |
linzhen | b465128 | 2022-09-13 18:13:31 +0000 | [diff] [blame] | 2802 | static int check_frame_refs_short_signaling(AV1_COMMON *const cm, |
linzhen | 0737492 | 2022-09-19 23:09:28 +0000 | [diff] [blame] | 2803 | bool enable_ref_short_signaling) { |
linzhen | b465128 | 2022-09-13 18:13:31 +0000 | [diff] [blame] | 2804 | // In rtc case when res < 360p and speed >= 9, we turn on |
| 2805 | // frame_refs_short_signaling if it won't break the decoder. |
| 2806 | if (enable_ref_short_signaling) { |
| 2807 | const int gld_map_idx = get_ref_frame_map_idx(cm, GOLDEN_FRAME); |
| 2808 | const int base = |
| 2809 | 1 << (cm->seq_params->order_hint_info.order_hint_bits_minus_1 + 1); |
| 2810 | |
| 2811 | const int order_hint_group_cur = |
| 2812 | cm->current_frame.display_order_hint / base; |
| 2813 | const int order_hint_group_gld = |
| 2814 | cm->ref_frame_map[gld_map_idx]->display_order_hint / base; |
| 2815 | const int relative_dist = cm->current_frame.order_hint - |
| 2816 | cm->ref_frame_map[gld_map_idx]->order_hint; |
| 2817 | |
| 2818 | // If current frame and GOLDEN frame are in the same order_hint group, and |
| 2819 | // they are not far apart (i.e., > 64 frames), then return 1. |
| 2820 | if (order_hint_group_cur == order_hint_group_gld && relative_dist >= 0 && |
| 2821 | relative_dist <= 64) { |
| 2822 | return 1; |
| 2823 | } |
| 2824 | return 0; |
| 2825 | } |
| 2826 | |
Zoe Liu | 1d90ceb | 2018-04-16 16:53:37 -0700 | [diff] [blame] | 2827 | // Check whether all references are distinct frames. |
Wan-Teh Chang | 2621bad | 2022-12-28 16:01:46 -0800 | [diff] [blame] | 2828 | const RefCntBuffer *seen_bufs[INTER_REFS_PER_FRAME] = { NULL }; |
Zoe Liu | 1d90ceb | 2018-04-16 16:53:37 -0700 | [diff] [blame] | 2829 | int num_refs = 0; |
David Turner | e7ebf90 | 2018-12-04 14:04:55 +0000 | [diff] [blame] | 2830 | for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
David Turner | a21966b | 2018-12-05 14:48:49 +0000 | [diff] [blame] | 2831 | const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame); |
David Turner | e7ebf90 | 2018-12-04 14:04:55 +0000 | [diff] [blame] | 2832 | if (buf != NULL) { |
| 2833 | int seen = 0; |
| 2834 | for (int i = 0; i < num_refs; i++) { |
| 2835 | if (seen_bufs[i] == buf) { |
| 2836 | seen = 1; |
| 2837 | break; |
| 2838 | } |
| 2839 | } |
| 2840 | if (!seen) seen_bufs[num_refs++] = buf; |
| 2841 | } |
Zoe Liu | 1d90ceb | 2018-04-16 16:53:37 -0700 | [diff] [blame] | 2842 | } |
| 2843 | |
| 2844 | // We only turn on frame_refs_short_signaling when all references are |
| 2845 | // distinct. |
| 2846 | if (num_refs < INTER_REFS_PER_FRAME) { |
| 2847 | // It indicates that there exist more than one reference frame pointing to |
| 2848 | // the same reference buffer, i.e. two or more references are duplicate. |
David Turner | 45f416c | 2018-11-15 11:34:30 +0000 | [diff] [blame] | 2849 | return 0; |
Zoe Liu | 1d90ceb | 2018-04-16 16:53:37 -0700 | [diff] [blame] | 2850 | } |
| 2851 | |
| 2852 | // Check whether the encoder side ref frame choices are aligned with that to |
| 2853 | // be derived at the decoder side. |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 2854 | int remapped_ref_idx_decoder[REF_FRAMES]; |
Zoe Liu | 1d90ceb | 2018-04-16 16:53:37 -0700 | [diff] [blame] | 2855 | |
David Turner | a21966b | 2018-12-05 14:48:49 +0000 | [diff] [blame] | 2856 | const int lst_map_idx = get_ref_frame_map_idx(cm, LAST_FRAME); |
| 2857 | const int gld_map_idx = get_ref_frame_map_idx(cm, GOLDEN_FRAME); |
Zoe Liu | a5cf724 | 2018-05-14 14:26:18 -0700 | [diff] [blame] | 2858 | |
Zoe Liu | 1d90ceb | 2018-04-16 16:53:37 -0700 | [diff] [blame] | 2859 | // Set up the frame refs mapping indexes according to the |
| 2860 | // frame_refs_short_signaling policy. |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 2861 | av1_set_frame_refs(cm, remapped_ref_idx_decoder, lst_map_idx, gld_map_idx); |
Zoe Liu | 1d90ceb | 2018-04-16 16:53:37 -0700 | [diff] [blame] | 2862 | |
| 2863 | // We only turn on frame_refs_short_signaling when the encoder side decision |
| 2864 | // on ref frames is identical to that at the decoder side. |
David Turner | 45f416c | 2018-11-15 11:34:30 +0000 | [diff] [blame] | 2865 | int frame_refs_short_signaling = 1; |
Zoe Liu | 1d90ceb | 2018-04-16 16:53:37 -0700 | [diff] [blame] | 2866 | for (int ref_idx = 0; ref_idx < INTER_REFS_PER_FRAME; ++ref_idx) { |
| 2867 | // Compare the buffer index between two reference frames indexed |
| 2868 | // respectively by the encoder and the decoder side decisions. |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 2869 | RefCntBuffer *ref_frame_buf_new = NULL; |
| 2870 | if (remapped_ref_idx_decoder[ref_idx] != INVALID_IDX) { |
| 2871 | ref_frame_buf_new = cm->ref_frame_map[remapped_ref_idx_decoder[ref_idx]]; |
| 2872 | } |
| 2873 | if (get_ref_frame_buf(cm, LAST_FRAME + ref_idx) != ref_frame_buf_new) { |
David Turner | 45f416c | 2018-11-15 11:34:30 +0000 | [diff] [blame] | 2874 | frame_refs_short_signaling = 0; |
Zoe Liu | 1d90ceb | 2018-04-16 16:53:37 -0700 | [diff] [blame] | 2875 | break; |
| 2876 | } |
| 2877 | } |
| 2878 | |
| 2879 | #if 0 // For debug |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 2880 | printf("\nFrame=%d: \n", cm->current_frame.frame_number); |
David Turner | 45f416c | 2018-11-15 11:34:30 +0000 | [diff] [blame] | 2881 | printf("***frame_refs_short_signaling=%d\n", frame_refs_short_signaling); |
Zoe Liu | a5cf724 | 2018-05-14 14:26:18 -0700 | [diff] [blame] | 2882 | for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
David Turner | e7ebf90 | 2018-12-04 14:04:55 +0000 | [diff] [blame] | 2883 | printf("enc_ref(map_idx=%d)=%d, vs. " |
David Turner | 1bcefb3 | 2018-11-19 17:54:00 +0000 | [diff] [blame] | 2884 | "dec_ref(map_idx=%d)=%d\n", |
David Turner | a21966b | 2018-12-05 14:48:49 +0000 | [diff] [blame] | 2885 | get_ref_frame_map_idx(cm, ref_frame), ref_frame, |
| 2886 | cm->remapped_ref_idx[ref_frame - LAST_FRAME], |
David Turner | acbe83b | 2018-11-21 13:22:20 +0000 | [diff] [blame] | 2887 | ref_frame); |
Zoe Liu | 1d90ceb | 2018-04-16 16:53:37 -0700 | [diff] [blame] | 2888 | } |
| 2889 | #endif // 0 |
| 2890 | |
David Turner | 45f416c | 2018-11-15 11:34:30 +0000 | [diff] [blame] | 2891 | return frame_refs_short_signaling; |
Zoe Liu | 1d90ceb | 2018-04-16 16:53:37 -0700 | [diff] [blame] | 2892 | } |
| 2893 | |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 2894 | // New function based on HLS R18 |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2895 | static AOM_INLINE void write_uncompressed_header_obu( |
Vishesh | 8bd59d9 | 2021-04-27 11:34:17 +0530 | [diff] [blame] | 2896 | AV1_COMP *cpi, MACROBLOCKD *const xd, struct aom_write_bit_buffer *saved_wb, |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 2897 | struct aom_write_bit_buffer *wb) { |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 2898 | AV1_COMMON *const cm = &cpi->common; |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 2899 | const SequenceHeader *const seq_params = cm->seq_params; |
Urvang Joshi | 1781462 | 2020-03-27 17:26:17 -0700 | [diff] [blame] | 2900 | const CommonQuantParams *quant_params = &cm->quant_params; |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 2901 | CurrentFrame *const current_frame = &cm->current_frame; |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 2902 | FeatureFlags *const features = &cm->features; |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 2903 | |
linzhen | b465128 | 2022-09-13 18:13:31 +0000 | [diff] [blame] | 2904 | if (!cpi->sf.rt_sf.enable_ref_short_signaling || |
| 2905 | !seq_params->order_hint_info.enable_order_hint || |
| 2906 | seq_params->order_hint_info.enable_ref_frame_mvs) { |
| 2907 | current_frame->frame_refs_short_signaling = 0; |
| 2908 | } else { |
| 2909 | current_frame->frame_refs_short_signaling = 1; |
| 2910 | } |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 2911 | |
Urvang Joshi | 8d5a4ba | 2018-07-19 16:26:34 -0700 | [diff] [blame] | 2912 | if (seq_params->still_picture) { |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2913 | assert(cm->show_existing_frame == 0); |
| 2914 | assert(cm->show_frame == 1); |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 2915 | assert(current_frame->frame_type == KEY_FRAME); |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2916 | } |
Urvang Joshi | 8d5a4ba | 2018-07-19 16:26:34 -0700 | [diff] [blame] | 2917 | if (!seq_params->reduced_still_picture_hdr) { |
Sarah Parker | 3300552 | 2018-07-27 14:46:25 -0700 | [diff] [blame] | 2918 | if (encode_show_existing_frame(cm)) { |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2919 | aom_wb_write_bit(wb, 1); // show_existing_frame |
| 2920 | aom_wb_write_literal(wb, cpi->existing_fb_idx_to_show, 3); |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 2921 | |
Urvang Joshi | 8d5a4ba | 2018-07-19 16:26:34 -0700 | [diff] [blame] | 2922 | if (seq_params->decoder_model_info_present_flag && |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 2923 | seq_params->timing_info.equal_picture_interval == 0) { |
Andrey Norkin | 795ba87 | 2018-03-06 13:24:14 -0800 | [diff] [blame] | 2924 | write_tu_pts_info(cm, wb); |
| 2925 | } |
Urvang Joshi | 8d5a4ba | 2018-07-19 16:26:34 -0700 | [diff] [blame] | 2926 | if (seq_params->frame_id_numbers_present_flag) { |
| 2927 | int frame_id_len = seq_params->frame_id_length; |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2928 | int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show]; |
| 2929 | aom_wb_write_literal(wb, display_frame_id, frame_id_len); |
| 2930 | } |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2931 | return; |
| 2932 | } else { |
| 2933 | aom_wb_write_bit(wb, 0); // show_existing_frame |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 2934 | } |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 2935 | |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 2936 | aom_wb_write_literal(wb, current_frame->frame_type, 2); |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2937 | |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2938 | aom_wb_write_bit(wb, cm->show_frame); |
Andrey Norkin | 795ba87 | 2018-03-06 13:24:14 -0800 | [diff] [blame] | 2939 | if (cm->show_frame) { |
Urvang Joshi | 8d5a4ba | 2018-07-19 16:26:34 -0700 | [diff] [blame] | 2940 | if (seq_params->decoder_model_info_present_flag && |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 2941 | seq_params->timing_info.equal_picture_interval == 0) |
Andrey Norkin | 795ba87 | 2018-03-06 13:24:14 -0800 | [diff] [blame] | 2942 | write_tu_pts_info(cm, wb); |
| 2943 | } else { |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2944 | aom_wb_write_bit(wb, cm->showable_frame); |
Zoe Liu | b499120 | 2017-12-21 15:31:06 -0800 | [diff] [blame] | 2945 | } |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2946 | if (frame_is_sframe(cm)) { |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 2947 | assert(features->error_resilient_mode); |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 2948 | } else if (!(current_frame->frame_type == KEY_FRAME && cm->show_frame)) { |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 2949 | aom_wb_write_bit(wb, features->error_resilient_mode); |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2950 | } |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 2951 | } |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 2952 | aom_wb_write_bit(wb, features->disable_cdf_update); |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 2953 | |
Urvang Joshi | 8d5a4ba | 2018-07-19 16:26:34 -0700 | [diff] [blame] | 2954 | if (seq_params->force_screen_content_tools == 2) { |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 2955 | aom_wb_write_bit(wb, features->allow_screen_content_tools); |
Imdad Sardharwalla | bf2cc01 | 2018-02-09 17:32:10 +0000 | [diff] [blame] | 2956 | } else { |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 2957 | assert(features->allow_screen_content_tools == |
Urvang Joshi | 8d5a4ba | 2018-07-19 16:26:34 -0700 | [diff] [blame] | 2958 | seq_params->force_screen_content_tools); |
Imdad Sardharwalla | bf2cc01 | 2018-02-09 17:32:10 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 2961 | if (features->allow_screen_content_tools) { |
Urvang Joshi | 8d5a4ba | 2018-07-19 16:26:34 -0700 | [diff] [blame] | 2962 | if (seq_params->force_integer_mv == 2) { |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 2963 | aom_wb_write_bit(wb, features->cur_frame_force_integer_mv); |
Imdad Sardharwalla | bf2cc01 | 2018-02-09 17:32:10 +0000 | [diff] [blame] | 2964 | } else { |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 2965 | assert(features->cur_frame_force_integer_mv == |
| 2966 | seq_params->force_integer_mv); |
Imdad Sardharwalla | bf2cc01 | 2018-02-09 17:32:10 +0000 | [diff] [blame] | 2967 | } |
| 2968 | } else { |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 2969 | assert(features->cur_frame_force_integer_mv == 0); |
Imdad Sardharwalla | bf2cc01 | 2018-02-09 17:32:10 +0000 | [diff] [blame] | 2970 | } |
Imdad Sardharwalla | bf2cc01 | 2018-02-09 17:32:10 +0000 | [diff] [blame] | 2971 | |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2972 | int frame_size_override_flag = 0; |
Zoe Liu | d4a67a8 | 2018-02-21 12:35:33 -0800 | [diff] [blame] | 2973 | |
Urvang Joshi | 8d5a4ba | 2018-07-19 16:26:34 -0700 | [diff] [blame] | 2974 | if (seq_params->reduced_still_picture_hdr) { |
Urvang Joshi | 39e6dd5 | 2019-02-01 12:46:09 -0800 | [diff] [blame] | 2975 | assert(cm->superres_upscaled_width == seq_params->max_frame_width && |
| 2976 | cm->superres_upscaled_height == seq_params->max_frame_height); |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2977 | } else { |
Urvang Joshi | 8d5a4ba | 2018-07-19 16:26:34 -0700 | [diff] [blame] | 2978 | if (seq_params->frame_id_numbers_present_flag) { |
| 2979 | int frame_id_len = seq_params->frame_id_length; |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2980 | aom_wb_write_literal(wb, cm->current_frame_id, frame_id_len); |
| 2981 | } |
Zoe Liu | 45a8fed | 2018-03-02 16:51:02 -0800 | [diff] [blame] | 2982 | |
Urvang Joshi | 39e6dd5 | 2019-02-01 12:46:09 -0800 | [diff] [blame] | 2983 | if (cm->superres_upscaled_width > seq_params->max_frame_width || |
| 2984 | cm->superres_upscaled_height > seq_params->max_frame_height) { |
Vishesh | 8bd59d9 | 2021-04-27 11:34:17 +0530 | [diff] [blame] | 2985 | aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM, |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2986 | "Frame dimensions are larger than the maximum values"); |
| 2987 | } |
| 2988 | |
| 2989 | frame_size_override_flag = |
Urvang Joshi | 39e6dd5 | 2019-02-01 12:46:09 -0800 | [diff] [blame] | 2990 | frame_is_sframe(cm) |
| 2991 | ? 1 |
| 2992 | : (cm->superres_upscaled_width != seq_params->max_frame_width || |
| 2993 | cm->superres_upscaled_height != seq_params->max_frame_height); |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 2994 | if (!frame_is_sframe(cm)) aom_wb_write_bit(wb, frame_size_override_flag); |
| 2995 | |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 2996 | if (seq_params->order_hint_info.enable_order_hint) |
| 2997 | aom_wb_write_literal( |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 2998 | wb, current_frame->order_hint, |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 2999 | seq_params->order_hint_info.order_hint_bits_minus_1 + 1); |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 3000 | |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 3001 | if (!features->error_resilient_mode && !frame_is_intra_only(cm)) { |
Urvang Joshi | 6237b88 | 2020-03-26 15:02:26 -0700 | [diff] [blame] | 3002 | aom_wb_write_literal(wb, features->primary_ref_frame, PRIMARY_REF_BITS); |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 3003 | } |
Yue Chen | d90d343 | 2018-03-16 11:28:42 -0700 | [diff] [blame] | 3004 | } |
| 3005 | |
Urvang Joshi | 8d5a4ba | 2018-07-19 16:26:34 -0700 | [diff] [blame] | 3006 | if (seq_params->decoder_model_info_present_flag) { |
Tarundeep Singh | d00b98f | 2021-04-21 16:13:51 +0530 | [diff] [blame] | 3007 | aom_wb_write_bit(wb, cpi->ppi->buffer_removal_time_present); |
| 3008 | if (cpi->ppi->buffer_removal_time_present) { |
Adrian Grange | c56f6ec | 2018-05-31 14:19:32 -0700 | [diff] [blame] | 3009 | for (int op_num = 0; |
Urvang Joshi | 8d5a4ba | 2018-07-19 16:26:34 -0700 | [diff] [blame] | 3010 | op_num < seq_params->operating_points_cnt_minus_1 + 1; op_num++) { |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 3011 | if (seq_params->op_params[op_num].decoder_model_param_present_flag) { |
Wan-Teh Chang | 62e8d9c | 2021-05-28 12:30:23 -0700 | [diff] [blame] | 3012 | if (seq_params->operating_point_idc[op_num] == 0 || |
| 3013 | ((seq_params->operating_point_idc[op_num] >> |
Adrian Grange | c56f6ec | 2018-05-31 14:19:32 -0700 | [diff] [blame] | 3014 | cm->temporal_layer_id) & |
| 3015 | 0x1 && |
Urvang Joshi | 8d5a4ba | 2018-07-19 16:26:34 -0700 | [diff] [blame] | 3016 | (seq_params->operating_point_idc[op_num] >> |
Adrian Grange | c56f6ec | 2018-05-31 14:19:32 -0700 | [diff] [blame] | 3017 | (cm->spatial_layer_id + 8)) & |
Wan-Teh Chang | 62e8d9c | 2021-05-28 12:30:23 -0700 | [diff] [blame] | 3018 | 0x1)) { |
Wan-Teh Chang | f64b3bc | 2018-07-02 09:42:39 -0700 | [diff] [blame] | 3019 | aom_wb_write_unsigned_literal( |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 3020 | wb, cm->buffer_removal_times[op_num], |
| 3021 | seq_params->decoder_model_info.buffer_removal_time_length); |
| 3022 | cm->buffer_removal_times[op_num]++; |
| 3023 | if (cm->buffer_removal_times[op_num] == 0) { |
Vishesh | 8bd59d9 | 2021-04-27 11:34:17 +0530 | [diff] [blame] | 3024 | aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM, |
Wan-Teh Chang | f64b3bc | 2018-07-02 09:42:39 -0700 | [diff] [blame] | 3025 | "buffer_removal_time overflowed"); |
| 3026 | } |
Adrian Grange | c56f6ec | 2018-05-31 14:19:32 -0700 | [diff] [blame] | 3027 | } |
Andrey Norkin | 795ba87 | 2018-03-06 13:24:14 -0800 | [diff] [blame] | 3028 | } |
| 3029 | } |
| 3030 | } |
| 3031 | } |
David Turner | 996b2c1 | 2018-12-07 15:52:30 +0000 | [diff] [blame] | 3032 | |
| 3033 | // Shown keyframes and switch-frames automatically refreshes all reference |
| 3034 | // frames. For all other frame types, we need to write refresh_frame_flags. |
bohanli | 9985250 | 2020-07-14 16:22:45 -0700 | [diff] [blame] | 3035 | if ((current_frame->frame_type == KEY_FRAME && !cm->show_frame) || |
David Turner | 996b2c1 | 2018-12-07 15:52:30 +0000 | [diff] [blame] | 3036 | current_frame->frame_type == INTER_FRAME || |
| 3037 | current_frame->frame_type == INTRA_ONLY_FRAME) |
| 3038 | aom_wb_write_literal(wb, current_frame->refresh_frame_flags, REF_FRAMES); |
| 3039 | |
David Turner | 996b2c1 | 2018-12-07 15:52:30 +0000 | [diff] [blame] | 3040 | if (!frame_is_intra_only(cm) || current_frame->refresh_frame_flags != 0xff) { |
Debargha Mukherjee | 33f4265 | 2018-04-12 14:27:32 -0700 | [diff] [blame] | 3041 | // Write all ref frame order hints if error_resilient_mode == 1 |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 3042 | if (features->error_resilient_mode && |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 3043 | seq_params->order_hint_info.enable_order_hint) { |
Debargha Mukherjee | 33f4265 | 2018-04-12 14:27:32 -0700 | [diff] [blame] | 3044 | for (int ref_idx = 0; ref_idx < REF_FRAMES; ref_idx++) { |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 3045 | aom_wb_write_literal( |
David Turner | 996b2c1 | 2018-12-07 15:52:30 +0000 | [diff] [blame] | 3046 | wb, cm->ref_frame_map[ref_idx]->order_hint, |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 3047 | seq_params->order_hint_info.order_hint_bits_minus_1 + 1); |
Debargha Mukherjee | 33f4265 | 2018-04-12 14:27:32 -0700 | [diff] [blame] | 3048 | } |
| 3049 | } |
| 3050 | } |
| 3051 | |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 3052 | if (current_frame->frame_type == KEY_FRAME) { |
Debargha Mukherjee | 33f4265 | 2018-04-12 14:27:32 -0700 | [diff] [blame] | 3053 | write_frame_size(cm, frame_size_override_flag, wb); |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 3054 | assert(!av1_superres_scaled(cm) || !features->allow_intrabc); |
| 3055 | if (features->allow_screen_content_tools && !av1_superres_scaled(cm)) |
| 3056 | aom_wb_write_bit(wb, features->allow_intrabc); |
Debargha Mukherjee | 33f4265 | 2018-04-12 14:27:32 -0700 | [diff] [blame] | 3057 | } else { |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 3058 | if (current_frame->frame_type == INTRA_ONLY_FRAME) { |
Debargha Mukherjee | 33f4265 | 2018-04-12 14:27:32 -0700 | [diff] [blame] | 3059 | write_frame_size(cm, frame_size_override_flag, wb); |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 3060 | assert(!av1_superres_scaled(cm) || !features->allow_intrabc); |
| 3061 | if (features->allow_screen_content_tools && !av1_superres_scaled(cm)) |
| 3062 | aom_wb_write_bit(wb, features->allow_intrabc); |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 3063 | } else if (current_frame->frame_type == INTER_FRAME || |
| 3064 | frame_is_sframe(cm)) { |
Debargha Mukherjee | 33f4265 | 2018-04-12 14:27:32 -0700 | [diff] [blame] | 3065 | MV_REFERENCE_FRAME ref_frame; |
| 3066 | |
Imdad Sardharwalla | 21cc90b | 2018-03-12 16:39:38 +0000 | [diff] [blame] | 3067 | // NOTE: Error resilient mode turns off frame_refs_short_signaling |
| 3068 | // automatically. |
Zoe Liu | a5cf724 | 2018-05-14 14:26:18 -0700 | [diff] [blame] | 3069 | #define FRAME_REFS_SHORT_SIGNALING 0 |
| 3070 | #if FRAME_REFS_SHORT_SIGNALING |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 3071 | current_frame->frame_refs_short_signaling = |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 3072 | seq_params->order_hint_info.enable_order_hint; |
Zoe Liu | a5cf724 | 2018-05-14 14:26:18 -0700 | [diff] [blame] | 3073 | #endif // FRAME_REFS_SHORT_SIGNALING |
Zoe Liu | d4a67a8 | 2018-02-21 12:35:33 -0800 | [diff] [blame] | 3074 | |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 3075 | if (current_frame->frame_refs_short_signaling) { |
linzhen | 0737492 | 2022-09-19 23:09:28 +0000 | [diff] [blame] | 3076 | // In rtc case when cpi->sf.rt_sf.enable_ref_short_signaling is true, |
| 3077 | // we turn on frame_refs_short_signaling when the current frame and |
| 3078 | // golden frame are in the same order_hint group, and their relative |
| 3079 | // distance is <= 64 (in order to be decodable). |
linzhen | b465128 | 2022-09-13 18:13:31 +0000 | [diff] [blame] | 3080 | |
| 3081 | // For other cases, an example solution for encoder-side |
| 3082 | // implementation on frame_refs_short_signaling is also provided in |
| 3083 | // this function, where frame_refs_short_signaling is only turned on |
| 3084 | // when the encoder side decision on ref frames is identical to that |
| 3085 | // at the decoder side. |
| 3086 | |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 3087 | current_frame->frame_refs_short_signaling = |
linzhen | b465128 | 2022-09-13 18:13:31 +0000 | [diff] [blame] | 3088 | check_frame_refs_short_signaling( |
| 3089 | cm, cpi->sf.rt_sf.enable_ref_short_signaling); |
Zoe Liu | a5cf724 | 2018-05-14 14:26:18 -0700 | [diff] [blame] | 3090 | } |
Zoe Liu | 1d90ceb | 2018-04-16 16:53:37 -0700 | [diff] [blame] | 3091 | |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 3092 | if (seq_params->order_hint_info.enable_order_hint) |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 3093 | aom_wb_write_bit(wb, current_frame->frame_refs_short_signaling); |
Zoe Liu | a5cf724 | 2018-05-14 14:26:18 -0700 | [diff] [blame] | 3094 | |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 3095 | if (current_frame->frame_refs_short_signaling) { |
David Turner | a21966b | 2018-12-05 14:48:49 +0000 | [diff] [blame] | 3096 | const int lst_ref = get_ref_frame_map_idx(cm, LAST_FRAME); |
Zoe Liu | a5cf724 | 2018-05-14 14:26:18 -0700 | [diff] [blame] | 3097 | aom_wb_write_literal(wb, lst_ref, REF_FRAMES_LOG2); |
| 3098 | |
David Turner | a21966b | 2018-12-05 14:48:49 +0000 | [diff] [blame] | 3099 | const int gld_ref = get_ref_frame_map_idx(cm, GOLDEN_FRAME); |
Zoe Liu | a5cf724 | 2018-05-14 14:26:18 -0700 | [diff] [blame] | 3100 | aom_wb_write_literal(wb, gld_ref, REF_FRAMES_LOG2); |
Imdad Sardharwalla | 21cc90b | 2018-03-12 16:39:38 +0000 | [diff] [blame] | 3101 | } |
Imdad Sardharwalla | 21cc90b | 2018-03-12 16:39:38 +0000 | [diff] [blame] | 3102 | |
| 3103 | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
David Turner | a21966b | 2018-12-05 14:48:49 +0000 | [diff] [blame] | 3104 | assert(get_ref_frame_map_idx(cm, ref_frame) != INVALID_IDX); |
David Turner | 7324576 | 2019-02-11 16:42:34 +0000 | [diff] [blame] | 3105 | if (!current_frame->frame_refs_short_signaling) |
David Turner | a21966b | 2018-12-05 14:48:49 +0000 | [diff] [blame] | 3106 | aom_wb_write_literal(wb, get_ref_frame_map_idx(cm, ref_frame), |
Imdad Sardharwalla | 21cc90b | 2018-03-12 16:39:38 +0000 | [diff] [blame] | 3107 | REF_FRAMES_LOG2); |
Urvang Joshi | 8d5a4ba | 2018-07-19 16:26:34 -0700 | [diff] [blame] | 3108 | if (seq_params->frame_id_numbers_present_flag) { |
David Turner | a21966b | 2018-12-05 14:48:49 +0000 | [diff] [blame] | 3109 | int i = get_ref_frame_map_idx(cm, ref_frame); |
Urvang Joshi | 8d5a4ba | 2018-07-19 16:26:34 -0700 | [diff] [blame] | 3110 | int frame_id_len = seq_params->frame_id_length; |
| 3111 | int diff_len = seq_params->delta_frame_id_length; |
Wan-Teh Chang | 6958297 | 2018-05-15 13:18:52 -0700 | [diff] [blame] | 3112 | int delta_frame_id_minus_1 = |
Imdad Sardharwalla | 21cc90b | 2018-03-12 16:39:38 +0000 | [diff] [blame] | 3113 | ((cm->current_frame_id - cm->ref_frame_id[i] + |
| 3114 | (1 << frame_id_len)) % |
| 3115 | (1 << frame_id_len)) - |
| 3116 | 1; |
Wan-Teh Chang | 6958297 | 2018-05-15 13:18:52 -0700 | [diff] [blame] | 3117 | if (delta_frame_id_minus_1 < 0 || |
David Turner | 45f416c | 2018-11-15 11:34:30 +0000 | [diff] [blame] | 3118 | delta_frame_id_minus_1 >= (1 << diff_len)) { |
Vishesh | 8bd59d9 | 2021-04-27 11:34:17 +0530 | [diff] [blame] | 3119 | aom_internal_error(cm->error, AOM_CODEC_ERROR, |
David Turner | 45f416c | 2018-11-15 11:34:30 +0000 | [diff] [blame] | 3120 | "Invalid delta_frame_id_minus_1"); |
| 3121 | } |
Wan-Teh Chang | 6958297 | 2018-05-15 13:18:52 -0700 | [diff] [blame] | 3122 | aom_wb_write_literal(wb, delta_frame_id_minus_1, diff_len); |
Imdad Sardharwalla | 21cc90b | 2018-03-12 16:39:38 +0000 | [diff] [blame] | 3123 | } |
Zoe Liu | 48acf88 | 2018-02-21 12:16:50 -0800 | [diff] [blame] | 3124 | } |
Zoe Liu | ca0cd3f | 2018-02-26 15:07:50 -0800 | [diff] [blame] | 3125 | |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 3126 | if (!features->error_resilient_mode && frame_size_override_flag) { |
David Turner | a21966b | 2018-12-05 14:48:49 +0000 | [diff] [blame] | 3127 | write_frame_size_with_refs(cm, wb); |
Imdad Sardharwalla | 21cc90b | 2018-03-12 16:39:38 +0000 | [diff] [blame] | 3128 | } else { |
| 3129 | write_frame_size(cm, frame_size_override_flag, wb); |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3130 | } |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3131 | |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 3132 | if (!features->cur_frame_force_integer_mv) |
| 3133 | aom_wb_write_bit(wb, features->allow_high_precision_mv); |
Urvang Joshi | 6237b88 | 2020-03-26 15:02:26 -0700 | [diff] [blame] | 3134 | write_frame_interp_filter(features->interp_filter, wb); |
| 3135 | aom_wb_write_bit(wb, features->switchable_motion_mode); |
Debargha Mukherjee | a5b810a | 2018-03-26 19:19:55 -0700 | [diff] [blame] | 3136 | if (frame_might_allow_ref_frame_mvs(cm)) { |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 3137 | aom_wb_write_bit(wb, features->allow_ref_frame_mvs); |
Debargha Mukherjee | daa8d7e | 2018-04-07 13:07:29 -0700 | [diff] [blame] | 3138 | } else { |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 3139 | assert(features->allow_ref_frame_mvs == 0); |
Imdad Sardharwalla | 21cc90b | 2018-03-12 16:39:38 +0000 | [diff] [blame] | 3140 | } |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3141 | } |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3142 | } |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3143 | |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 3144 | const int might_bwd_adapt = !(seq_params->reduced_still_picture_hdr) && |
| 3145 | !(features->disable_cdf_update); |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 3146 | if (cm->tiles.large_scale) |
Urvang Joshi | 6237b88 | 2020-03-26 15:02:26 -0700 | [diff] [blame] | 3147 | assert(features->refresh_frame_context == REFRESH_FRAME_CONTEXT_DISABLED); |
Yaowu Xu | 66663f7 | 2018-03-27 08:22:23 -0700 | [diff] [blame] | 3148 | |
Jingning Han | da11e69 | 2017-12-19 08:45:08 -0800 | [diff] [blame] | 3149 | if (might_bwd_adapt) { |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3150 | aom_wb_write_bit( |
Urvang Joshi | 6237b88 | 2020-03-26 15:02:26 -0700 | [diff] [blame] | 3151 | wb, features->refresh_frame_context == REFRESH_FRAME_CONTEXT_DISABLED); |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3152 | } |
Yue Chen | d90d343 | 2018-03-16 11:28:42 -0700 | [diff] [blame] | 3153 | |
James Zern | 8b007ff | 2018-02-26 22:49:13 -0800 | [diff] [blame] | 3154 | write_tile_info(cm, saved_wb, wb); |
Urvang Joshi | 1781462 | 2020-03-27 17:26:17 -0700 | [diff] [blame] | 3155 | encode_quantization(quant_params, av1_num_planes(cm), |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 3156 | cm->seq_params->separate_uv_delta_q, wb); |
Cherma Rajan A | b4be7e5 | 2021-04-14 19:28:53 +0530 | [diff] [blame] | 3157 | encode_segmentation(cm, wb); |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3158 | |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 3159 | const DeltaQInfo *const delta_q_info = &cm->delta_q_info; |
Urvang Joshi | 1781462 | 2020-03-27 17:26:17 -0700 | [diff] [blame] | 3160 | if (delta_q_info->delta_q_present_flag) assert(quant_params->base_qindex > 0); |
| 3161 | if (quant_params->base_qindex > 0) { |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 3162 | aom_wb_write_bit(wb, delta_q_info->delta_q_present_flag); |
| 3163 | if (delta_q_info->delta_q_present_flag) { |
| 3164 | aom_wb_write_literal(wb, get_msb(delta_q_info->delta_q_res), 2); |
Urvang Joshi | acad1ca | 2020-04-27 17:03:25 -0700 | [diff] [blame] | 3165 | xd->current_base_qindex = quant_params->base_qindex; |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 3166 | if (features->allow_intrabc) |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 3167 | assert(delta_q_info->delta_lf_present_flag == 0); |
Yue Chen | 6bec057 | 2018-05-15 09:53:35 -0700 | [diff] [blame] | 3168 | else |
David Turner | ebf96f4 | 2018-11-14 16:57:57 +0000 | [diff] [blame] | 3169 | aom_wb_write_bit(wb, delta_q_info->delta_lf_present_flag); |
| 3170 | if (delta_q_info->delta_lf_present_flag) { |
| 3171 | aom_wb_write_literal(wb, get_msb(delta_q_info->delta_lf_res), 2); |
| 3172 | aom_wb_write_bit(wb, delta_q_info->delta_lf_multi); |
Wan-Teh Chang | 69ac7eb | 2018-06-08 09:41:37 -0700 | [diff] [blame] | 3173 | av1_reset_loop_filter_delta(xd, av1_num_planes(cm)); |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3174 | } |
| 3175 | } |
| 3176 | } |
Yue Chen | 6bec057 | 2018-05-15 09:53:35 -0700 | [diff] [blame] | 3177 | |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 3178 | if (features->all_lossless) { |
Cheng Chen | 09c83a5 | 2018-06-05 12:27:36 -0700 | [diff] [blame] | 3179 | assert(!av1_superres_scaled(cm)); |
Urvang Joshi | d6b5d51 | 2018-03-20 13:34:38 -0700 | [diff] [blame] | 3180 | } else { |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 3181 | if (!features->coded_lossless) { |
Urvang Joshi | c8b52d5 | 2018-03-23 13:16:51 -0700 | [diff] [blame] | 3182 | encode_loopfilter(cm, wb); |
| 3183 | encode_cdef(cm, wb); |
| 3184 | } |
Urvang Joshi | 5ec7b81 | 2018-02-28 14:37:06 -0800 | [diff] [blame] | 3185 | encode_restoration_mode(cm, wb); |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3186 | } |
Urvang Joshi | 5ec7b81 | 2018-02-28 14:37:06 -0800 | [diff] [blame] | 3187 | |
David Turner | 45f416c | 2018-11-15 11:34:30 +0000 | [diff] [blame] | 3188 | // Write TX mode |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 3189 | if (features->coded_lossless) |
Urvang Joshi | 6237b88 | 2020-03-26 15:02:26 -0700 | [diff] [blame] | 3190 | assert(features->tx_mode == ONLY_4X4); |
David Turner | 45f416c | 2018-11-15 11:34:30 +0000 | [diff] [blame] | 3191 | else |
Urvang Joshi | 6237b88 | 2020-03-26 15:02:26 -0700 | [diff] [blame] | 3192 | aom_wb_write_bit(wb, features->tx_mode == TX_MODE_SELECT); |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3193 | |
David Turner | b757ce0 | 2018-11-12 15:01:28 +0000 | [diff] [blame] | 3194 | if (!frame_is_intra_only(cm)) { |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 3195 | const int use_hybrid_pred = |
| 3196 | current_frame->reference_mode == REFERENCE_MODE_SELECT; |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3197 | |
| 3198 | aom_wb_write_bit(wb, use_hybrid_pred); |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3199 | } |
Zoe Liu | 4b847e1 | 2017-12-07 12:44:45 -0800 | [diff] [blame] | 3200 | |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 3201 | if (current_frame->skip_mode_info.skip_mode_allowed) |
| 3202 | aom_wb_write_bit(wb, current_frame->skip_mode_info.skip_mode_flag); |
Zoe Liu | 4b847e1 | 2017-12-07 12:44:45 -0800 | [diff] [blame] | 3203 | |
Debargha Mukherjee | a5b810a | 2018-03-26 19:19:55 -0700 | [diff] [blame] | 3204 | if (frame_might_allow_warped_motion(cm)) |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 3205 | aom_wb_write_bit(wb, features->allow_warped_motion); |
Debargha Mukherjee | 07a7c1f | 2018-03-21 17:39:13 -0700 | [diff] [blame] | 3206 | else |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 3207 | assert(!features->allow_warped_motion); |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3208 | |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 3209 | aom_wb_write_bit(wb, features->reduced_tx_set_used); |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3210 | |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3211 | if (!frame_is_intra_only(cm)) write_global_motion(cpi, wb); |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3212 | |
Urvang Joshi | 8d5a4ba | 2018-07-19 16:26:34 -0700 | [diff] [blame] | 3213 | if (seq_params->film_grain_params_present && |
David Turner | 08f909c | 2018-12-18 13:29:14 +0000 | [diff] [blame] | 3214 | (cm->show_frame || cm->showable_frame)) |
Dominic Symes | d492901 | 2018-01-31 17:32:01 +0100 | [diff] [blame] | 3215 | write_film_grain_params(cpi, wb); |
Andrey Norkin | 6f1c2f7 | 2018-01-15 20:08:52 -0800 | [diff] [blame] | 3216 | |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 3217 | if (cm->tiles.large_scale) write_ext_tile_info(cm, saved_wb, wb); |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3218 | } |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3219 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3220 | static int choose_size_bytes(uint32_t size, int spare_msbs) { |
| 3221 | // Choose the number of bytes required to represent size, without |
| 3222 | // using the 'spare_msbs' number of most significant bits. |
| 3223 | |
| 3224 | // Make sure we will fit in 4 bytes to start with.. |
| 3225 | if (spare_msbs > 0 && size >> (32 - spare_msbs) != 0) return -1; |
| 3226 | |
| 3227 | // Normalise to 32 bits |
| 3228 | size <<= spare_msbs; |
| 3229 | |
| 3230 | if (size >> 24 != 0) |
| 3231 | return 4; |
| 3232 | else if (size >> 16 != 0) |
| 3233 | return 3; |
| 3234 | else if (size >> 8 != 0) |
| 3235 | return 2; |
| 3236 | else |
| 3237 | return 1; |
| 3238 | } |
| 3239 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 3240 | static AOM_INLINE void mem_put_varsize(uint8_t *const dst, const int sz, |
| 3241 | const int val) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3242 | switch (sz) { |
| 3243 | case 1: dst[0] = (uint8_t)(val & 0xff); break; |
| 3244 | case 2: mem_put_le16(dst, val); break; |
| 3245 | case 3: mem_put_le24(dst, val); break; |
| 3246 | case 4: mem_put_le32(dst, val); break; |
James Zern | 06c372d | 2017-04-20 16:08:29 -0700 | [diff] [blame] | 3247 | default: assert(0 && "Invalid size"); break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3248 | } |
| 3249 | } |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3250 | |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 3251 | static int remux_tiles(const CommonTileParams *const tiles, uint8_t *dst, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3252 | const uint32_t data_size, const uint32_t max_tile_size, |
| 3253 | const uint32_t max_tile_col_size, |
| 3254 | int *const tile_size_bytes, |
| 3255 | int *const tile_col_size_bytes) { |
Yunqing Wang | eeb08a9 | 2017-07-07 21:25:18 -0700 | [diff] [blame] | 3256 | // Choose the tile size bytes (tsb) and tile column size bytes (tcsb) |
| 3257 | int tsb; |
| 3258 | int tcsb; |
| 3259 | |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 3260 | if (tiles->large_scale) { |
Yunqing Wang | eeb08a9 | 2017-07-07 21:25:18 -0700 | [diff] [blame] | 3261 | // The top bit in the tile size field indicates tile copy mode, so we |
| 3262 | // have 1 less bit to code the tile size |
| 3263 | tsb = choose_size_bytes(max_tile_size, 1); |
| 3264 | tcsb = choose_size_bytes(max_tile_col_size, 0); |
| 3265 | } else { |
Yunqing Wang | eeb08a9 | 2017-07-07 21:25:18 -0700 | [diff] [blame] | 3266 | tsb = choose_size_bytes(max_tile_size, 0); |
| 3267 | tcsb = 4; // This is ignored |
| 3268 | (void)max_tile_col_size; |
Yunqing Wang | eeb08a9 | 2017-07-07 21:25:18 -0700 | [diff] [blame] | 3269 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3270 | |
| 3271 | assert(tsb > 0); |
| 3272 | assert(tcsb > 0); |
| 3273 | |
| 3274 | *tile_size_bytes = tsb; |
| 3275 | *tile_col_size_bytes = tcsb; |
James Zern | a60ff58 | 2018-02-24 14:02:12 -0800 | [diff] [blame] | 3276 | if (tsb == 4 && tcsb == 4) return data_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3277 | |
James Zern | a60ff58 | 2018-02-24 14:02:12 -0800 | [diff] [blame] | 3278 | uint32_t wpos = 0; |
| 3279 | uint32_t rpos = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3280 | |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 3281 | if (tiles->large_scale) { |
James Zern | a60ff58 | 2018-02-24 14:02:12 -0800 | [diff] [blame] | 3282 | int tile_row; |
| 3283 | int tile_col; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3284 | |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 3285 | for (tile_col = 0; tile_col < tiles->cols; tile_col++) { |
James Zern | a60ff58 | 2018-02-24 14:02:12 -0800 | [diff] [blame] | 3286 | // All but the last column has a column header |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 3287 | if (tile_col < tiles->cols - 1) { |
James Zern | a60ff58 | 2018-02-24 14:02:12 -0800 | [diff] [blame] | 3288 | uint32_t tile_col_size = mem_get_le32(dst + rpos); |
| 3289 | rpos += 4; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3290 | |
James Zern | a60ff58 | 2018-02-24 14:02:12 -0800 | [diff] [blame] | 3291 | // Adjust the tile column size by the number of bytes removed |
| 3292 | // from the tile size fields. |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 3293 | tile_col_size -= (4 - tsb) * tiles->rows; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3294 | |
James Zern | a60ff58 | 2018-02-24 14:02:12 -0800 | [diff] [blame] | 3295 | mem_put_varsize(dst + wpos, tcsb, tile_col_size); |
| 3296 | wpos += tcsb; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3297 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3298 | |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 3299 | for (tile_row = 0; tile_row < tiles->rows; tile_row++) { |
James Zern | a60ff58 | 2018-02-24 14:02:12 -0800 | [diff] [blame] | 3300 | // All, including the last row has a header |
| 3301 | uint32_t tile_header = mem_get_le32(dst + rpos); |
| 3302 | rpos += 4; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3303 | |
James Zern | a60ff58 | 2018-02-24 14:02:12 -0800 | [diff] [blame] | 3304 | // If this is a copy tile, we need to shift the MSB to the |
| 3305 | // top bit of the new width, and there is no data to copy. |
| 3306 | if (tile_header >> 31 != 0) { |
| 3307 | if (tsb < 4) tile_header >>= 32 - 8 * tsb; |
| 3308 | mem_put_varsize(dst + wpos, tsb, tile_header); |
Yunqing Wang | eeb08a9 | 2017-07-07 21:25:18 -0700 | [diff] [blame] | 3309 | wpos += tsb; |
James Zern | a60ff58 | 2018-02-24 14:02:12 -0800 | [diff] [blame] | 3310 | } else { |
| 3311 | mem_put_varsize(dst + wpos, tsb, tile_header); |
| 3312 | wpos += tsb; |
| 3313 | |
James Zern | 2f22bfd | 2018-03-10 12:58:22 -0800 | [diff] [blame] | 3314 | tile_header += AV1_MIN_TILE_SIZE_BYTES; |
James Zern | a60ff58 | 2018-02-24 14:02:12 -0800 | [diff] [blame] | 3315 | memmove(dst + wpos, dst + rpos, tile_header); |
| 3316 | rpos += tile_header; |
| 3317 | wpos += tile_header; |
Yunqing Wang | eeb08a9 | 2017-07-07 21:25:18 -0700 | [diff] [blame] | 3318 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3319 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3320 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3321 | |
| 3322 | assert(rpos > wpos); |
| 3323 | assert(rpos == data_size); |
| 3324 | |
| 3325 | return wpos; |
| 3326 | } |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 3327 | const int n_tiles = tiles->cols * tiles->rows; |
James Zern | a60ff58 | 2018-02-24 14:02:12 -0800 | [diff] [blame] | 3328 | int n; |
| 3329 | |
| 3330 | for (n = 0; n < n_tiles; n++) { |
| 3331 | int tile_size; |
| 3332 | |
| 3333 | if (n == n_tiles - 1) { |
| 3334 | tile_size = data_size - rpos; |
| 3335 | } else { |
| 3336 | tile_size = mem_get_le32(dst + rpos); |
| 3337 | rpos += 4; |
| 3338 | mem_put_varsize(dst + wpos, tsb, tile_size); |
James Zern | 2f22bfd | 2018-03-10 12:58:22 -0800 | [diff] [blame] | 3339 | tile_size += AV1_MIN_TILE_SIZE_BYTES; |
James Zern | a60ff58 | 2018-02-24 14:02:12 -0800 | [diff] [blame] | 3340 | wpos += tsb; |
| 3341 | } |
| 3342 | |
| 3343 | memmove(dst + wpos, dst + rpos, tile_size); |
| 3344 | |
| 3345 | rpos += tile_size; |
| 3346 | wpos += tile_size; |
| 3347 | } |
| 3348 | |
| 3349 | assert(rpos > wpos); |
| 3350 | assert(rpos == data_size); |
| 3351 | |
| 3352 | return wpos; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3353 | } |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3354 | |
Vishesh | 8ac928b | 2020-04-01 02:36:35 +0530 | [diff] [blame] | 3355 | uint32_t av1_write_obu_header(AV1LevelParams *const level_params, |
Tarundeep Singh | d37807f | 2021-03-31 17:30:55 +0530 | [diff] [blame] | 3356 | int *frame_header_count, OBU_TYPE obu_type, |
| 3357 | int obu_extension, uint8_t *const dst) { |
Vishesh | 8ac928b | 2020-04-01 02:36:35 +0530 | [diff] [blame] | 3358 | if (level_params->keep_level_stats && |
Hui Su | 4fd1176 | 2019-03-26 16:05:07 -0700 | [diff] [blame] | 3359 | (obu_type == OBU_FRAME || obu_type == OBU_FRAME_HEADER)) |
Tarundeep Singh | d37807f | 2021-03-31 17:30:55 +0530 | [diff] [blame] | 3360 | ++(*frame_header_count); |
Hui Su | 4fd1176 | 2019-03-26 16:05:07 -0700 | [diff] [blame] | 3361 | |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3362 | struct aom_write_bit_buffer wb = { dst, 0 }; |
| 3363 | uint32_t size = 0; |
| 3364 | |
Tom Finegan | f927381 | 2018-03-14 09:49:45 -0700 | [diff] [blame] | 3365 | aom_wb_write_literal(&wb, 0, 1); // forbidden bit. |
Soo-Chul Han | 38427e8 | 2017-09-27 15:06:13 -0400 | [diff] [blame] | 3366 | aom_wb_write_literal(&wb, (int)obu_type, 4); |
Vignesh Venkatasubramanian | 726f795 | 2018-03-08 15:03:35 -0800 | [diff] [blame] | 3367 | aom_wb_write_literal(&wb, obu_extension ? 1 : 0, 1); |
Wan-Teh Chang | 99ed4b7 | 2023-08-08 12:55:51 -0700 | [diff] [blame] | 3368 | aom_wb_write_literal(&wb, 1, 1); // obu_has_size_field |
Vignesh Venkatasubramanian | 726f795 | 2018-03-08 15:03:35 -0800 | [diff] [blame] | 3369 | aom_wb_write_literal(&wb, 0, 1); // reserved |
Tom Finegan | 5427be1 | 2018-03-14 18:45:39 -0700 | [diff] [blame] | 3370 | |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3371 | if (obu_extension) { |
| 3372 | aom_wb_write_literal(&wb, obu_extension & 0xFF, 8); |
| 3373 | } |
| 3374 | |
| 3375 | size = aom_wb_bytes_written(&wb); |
| 3376 | return size; |
| 3377 | } |
| 3378 | |
Tom Finegan | da8da7f | 2019-11-14 13:02:55 -0800 | [diff] [blame] | 3379 | int av1_write_uleb_obu_size(size_t obu_header_size, size_t obu_payload_size, |
Yaowu Xu | 797674b | 2019-05-01 17:38:11 -0700 | [diff] [blame] | 3380 | uint8_t *dest) { |
Tom Finegan | da8da7f | 2019-11-14 13:02:55 -0800 | [diff] [blame] | 3381 | const size_t offset = obu_header_size; |
Tom Finegan | 41150ad | 2018-01-23 11:42:55 -0800 | [diff] [blame] | 3382 | size_t coded_obu_size = 0; |
Tom Finegan | 9a1a958 | 2019-11-14 14:28:09 -0800 | [diff] [blame] | 3383 | const uint32_t obu_size = (uint32_t)obu_payload_size; |
| 3384 | assert(obu_size == obu_payload_size); |
Tom Finegan | 41150ad | 2018-01-23 11:42:55 -0800 | [diff] [blame] | 3385 | |
Vignesh Venkatasubramanian | ea0257d | 2018-02-28 14:43:34 -0800 | [diff] [blame] | 3386 | if (aom_uleb_encode(obu_size, sizeof(obu_size), dest + offset, |
| 3387 | &coded_obu_size) != 0) { |
Tom Finegan | 41150ad | 2018-01-23 11:42:55 -0800 | [diff] [blame] | 3388 | return AOM_CODEC_ERROR; |
Vignesh Venkatasubramanian | ea0257d | 2018-02-28 14:43:34 -0800 | [diff] [blame] | 3389 | } |
Tom Finegan | 41150ad | 2018-01-23 11:42:55 -0800 | [diff] [blame] | 3390 | |
| 3391 | return AOM_CODEC_OK; |
| 3392 | } |
Vignesh Venkatasubramanian | ea0257d | 2018-02-28 14:43:34 -0800 | [diff] [blame] | 3393 | |
Vishesh | 5849e6d | 2021-04-23 19:15:29 +0530 | [diff] [blame] | 3394 | size_t av1_obu_memmove(size_t obu_header_size, size_t obu_payload_size, |
| 3395 | uint8_t *data) { |
Tom Finegan | 5427be1 | 2018-03-14 18:45:39 -0700 | [diff] [blame] | 3396 | const size_t length_field_size = aom_uleb_size_in_bytes(obu_payload_size); |
Tom Finegan | da8da7f | 2019-11-14 13:02:55 -0800 | [diff] [blame] | 3397 | const size_t move_dst_offset = length_field_size + obu_header_size; |
| 3398 | const size_t move_src_offset = obu_header_size; |
| 3399 | const size_t move_size = obu_payload_size; |
Vignesh Venkatasubramanian | ea0257d | 2018-02-28 14:43:34 -0800 | [diff] [blame] | 3400 | memmove(data + move_dst_offset, data + move_src_offset, move_size); |
| 3401 | return length_field_size; |
| 3402 | } |
Tom Finegan | 41150ad | 2018-01-23 11:42:55 -0800 | [diff] [blame] | 3403 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 3404 | static AOM_INLINE void add_trailing_bits(struct aom_write_bit_buffer *wb) { |
Cyril Concolato | 2dab275 | 2018-02-26 14:25:47 -0800 | [diff] [blame] | 3405 | if (aom_wb_is_byte_aligned(wb)) { |
| 3406 | aom_wb_write_literal(wb, 0x80, 8); |
| 3407 | } else { |
| 3408 | // assumes that the other bits are already 0s |
| 3409 | aom_wb_write_bit(wb, 1); |
| 3410 | } |
| 3411 | } |
Cyril Concolato | 2dab275 | 2018-02-26 14:25:47 -0800 | [diff] [blame] | 3412 | |
Elliott Karpilovsky | 18fcd6a | 2019-09-16 15:15:06 -0700 | [diff] [blame] | 3413 | static AOM_INLINE void write_bitstream_level(AV1_LEVEL seq_level_idx, |
| 3414 | struct aom_write_bit_buffer *wb) { |
Debargha Mukherjee | 5749869 | 2018-05-11 13:29:31 -0700 | [diff] [blame] | 3415 | assert(is_valid_seq_level_idx(seq_level_idx)); |
| 3416 | aom_wb_write_literal(wb, seq_level_idx, LEVEL_BITS); |
Debargha Mukherjee | ea67540 | 2018-05-10 16:10:41 -0700 | [diff] [blame] | 3417 | } |
| 3418 | |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 3419 | uint32_t av1_write_sequence_header_obu(const SequenceHeader *seq_params, |
| 3420 | uint8_t *const dst) { |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3421 | struct aom_write_bit_buffer wb = { dst, 0 }; |
| 3422 | uint32_t size = 0; |
| 3423 | |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 3424 | write_profile(seq_params->profile, &wb); |
Soo-Chul Han | 79a501a | 2018-03-19 15:24:40 -0400 | [diff] [blame] | 3425 | |
Debargha Mukherjee | 5d6e3fb | 2018-04-03 13:05:54 -0700 | [diff] [blame] | 3426 | // Still picture or not |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 3427 | aom_wb_write_bit(&wb, seq_params->still_picture); |
| 3428 | assert(IMPLIES(!seq_params->still_picture, |
| 3429 | !seq_params->reduced_still_picture_hdr)); |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 3430 | // whether to use reduced still picture header |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 3431 | aom_wb_write_bit(&wb, seq_params->reduced_still_picture_hdr); |
Debargha Mukherjee | 5d6e3fb | 2018-04-03 13:05:54 -0700 | [diff] [blame] | 3432 | |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 3433 | if (seq_params->reduced_still_picture_hdr) { |
| 3434 | assert(seq_params->timing_info_present == 0); |
| 3435 | assert(seq_params->decoder_model_info_present_flag == 0); |
| 3436 | assert(seq_params->display_model_info_present_flag == 0); |
| 3437 | write_bitstream_level(seq_params->seq_level_idx[0], &wb); |
Debargha Mukherjee | acd41f9 | 2018-04-11 07:58:34 -0700 | [diff] [blame] | 3438 | } else { |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 3439 | aom_wb_write_bit( |
| 3440 | &wb, seq_params->timing_info_present); // timing info present flag |
Andrey Norkin | c14f845 | 2018-06-08 18:15:31 -0700 | [diff] [blame] | 3441 | |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 3442 | if (seq_params->timing_info_present) { |
Andrey Norkin | c14f845 | 2018-06-08 18:15:31 -0700 | [diff] [blame] | 3443 | // timing_info |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 3444 | write_timing_info_header(&seq_params->timing_info, &wb); |
| 3445 | aom_wb_write_bit(&wb, seq_params->decoder_model_info_present_flag); |
| 3446 | if (seq_params->decoder_model_info_present_flag) { |
| 3447 | write_decoder_model_info(&seq_params->decoder_model_info, &wb); |
Andrey Norkin | c14f845 | 2018-06-08 18:15:31 -0700 | [diff] [blame] | 3448 | } |
Adrian Grange | c56f6ec | 2018-05-31 14:19:32 -0700 | [diff] [blame] | 3449 | } |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 3450 | aom_wb_write_bit(&wb, seq_params->display_model_info_present_flag); |
| 3451 | aom_wb_write_literal(&wb, seq_params->operating_points_cnt_minus_1, |
Wan-Teh Chang | 6958297 | 2018-05-15 13:18:52 -0700 | [diff] [blame] | 3452 | OP_POINTS_CNT_MINUS_1_BITS); |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 3453 | int i; |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 3454 | for (i = 0; i < seq_params->operating_points_cnt_minus_1 + 1; i++) { |
| 3455 | aom_wb_write_literal(&wb, seq_params->operating_point_idc[i], |
Frank Bossen | baf6666 | 2018-04-30 12:21:04 -0400 | [diff] [blame] | 3456 | OP_POINTS_IDC_BITS); |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 3457 | write_bitstream_level(seq_params->seq_level_idx[i], &wb); |
| 3458 | if (seq_params->seq_level_idx[i] >= SEQ_LEVEL_4_0) |
| 3459 | aom_wb_write_bit(&wb, seq_params->tier[i]); |
| 3460 | if (seq_params->decoder_model_info_present_flag) { |
| 3461 | aom_wb_write_bit( |
| 3462 | &wb, seq_params->op_params[i].decoder_model_param_present_flag); |
| 3463 | if (seq_params->op_params[i].decoder_model_param_present_flag) { |
| 3464 | write_dec_model_op_parameters( |
| 3465 | &seq_params->op_params[i], |
| 3466 | seq_params->decoder_model_info |
| 3467 | .encoder_decoder_buffer_delay_length, |
| 3468 | &wb); |
| 3469 | } |
Adrian Grange | c56f6ec | 2018-05-31 14:19:32 -0700 | [diff] [blame] | 3470 | } |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 3471 | if (seq_params->display_model_info_present_flag) { |
| 3472 | aom_wb_write_bit( |
| 3473 | &wb, seq_params->op_params[i].display_model_param_present_flag); |
| 3474 | if (seq_params->op_params[i].display_model_param_present_flag) { |
Wan-Teh Chang | d3c97c1 | 2022-09-29 14:34:35 -0700 | [diff] [blame] | 3475 | assert(seq_params->op_params[i].initial_display_delay >= 1); |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 3476 | assert(seq_params->op_params[i].initial_display_delay <= 10); |
| 3477 | aom_wb_write_literal( |
| 3478 | &wb, seq_params->op_params[i].initial_display_delay - 1, 4); |
Adrian Grange | c56f6ec | 2018-05-31 14:19:32 -0700 | [diff] [blame] | 3479 | } |
| 3480 | } |
Debargha Mukherjee | c6f24c2 | 2018-04-07 08:43:08 -0700 | [diff] [blame] | 3481 | } |
Soo-Chul Han | 79a501a | 2018-03-19 15:24:40 -0400 | [diff] [blame] | 3482 | } |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 3483 | write_sequence_header(seq_params, &wb); |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3484 | |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 3485 | write_color_config(seq_params, &wb); |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3486 | |
Urvang Joshi | 450a9a2 | 2020-03-31 16:00:22 -0700 | [diff] [blame] | 3487 | aom_wb_write_bit(&wb, seq_params->film_grain_params_present); |
Andrey Norkin | 6f1c2f7 | 2018-01-15 20:08:52 -0800 | [diff] [blame] | 3488 | |
Cyril Concolato | 2dab275 | 2018-02-26 14:25:47 -0800 | [diff] [blame] | 3489 | add_trailing_bits(&wb); |
Cyril Concolato | 2dab275 | 2018-02-26 14:25:47 -0800 | [diff] [blame] | 3490 | |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3491 | size = aom_wb_bytes_written(&wb); |
| 3492 | return size; |
| 3493 | } |
| 3494 | |
Vishesh | 8bd59d9 | 2021-04-27 11:34:17 +0530 | [diff] [blame] | 3495 | static uint32_t write_frame_header_obu(AV1_COMP *cpi, MACROBLOCKD *const xd, |
Jingning Han | da11e69 | 2017-12-19 08:45:08 -0800 | [diff] [blame] | 3496 | struct aom_write_bit_buffer *saved_wb, |
Vignesh Venkatasubramanian | bd7b0e3 | 2018-04-10 11:31:59 -0700 | [diff] [blame] | 3497 | uint8_t *const dst, |
| 3498 | int append_trailing_bits) { |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3499 | struct aom_write_bit_buffer wb = { dst, 0 }; |
Vishesh | 8bd59d9 | 2021-04-27 11:34:17 +0530 | [diff] [blame] | 3500 | write_uncompressed_header_obu(cpi, xd, saved_wb, &wb); |
Vignesh Venkatasubramanian | bd7b0e3 | 2018-04-10 11:31:59 -0700 | [diff] [blame] | 3501 | if (append_trailing_bits) add_trailing_bits(&wb); |
| 3502 | return aom_wb_bytes_written(&wb); |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3503 | } |
| 3504 | |
Johann | a7f959f | 2018-11-16 12:51:05 -0500 | [diff] [blame] | 3505 | static uint32_t write_tile_group_header(uint8_t *const dst, int start_tile, |
| 3506 | int end_tile, int tiles_log2, |
Vignesh Venkatasubramanian | 2a06b41 | 2018-03-01 15:18:06 -0800 | [diff] [blame] | 3507 | int tile_start_and_end_present_flag) { |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3508 | struct aom_write_bit_buffer wb = { dst, 0 }; |
| 3509 | uint32_t size = 0; |
| 3510 | |
Vignesh Venkatasubramanian | 2a06b41 | 2018-03-01 15:18:06 -0800 | [diff] [blame] | 3511 | if (!tiles_log2) return size; |
| 3512 | |
| 3513 | aom_wb_write_bit(&wb, tile_start_and_end_present_flag); |
| 3514 | |
| 3515 | if (tile_start_and_end_present_flag) { |
Johann | a7f959f | 2018-11-16 12:51:05 -0500 | [diff] [blame] | 3516 | aom_wb_write_literal(&wb, start_tile, tiles_log2); |
| 3517 | aom_wb_write_literal(&wb, end_tile, tiles_log2); |
Vignesh Venkatasubramanian | 2a06b41 | 2018-03-01 15:18:06 -0800 | [diff] [blame] | 3518 | } |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3519 | |
| 3520 | size = aom_wb_bytes_written(&wb); |
| 3521 | return size; |
| 3522 | } |
| 3523 | |
Yunqing Wang | d8fd9e7 | 2019-12-26 15:36:31 -0800 | [diff] [blame] | 3524 | extern void av1_print_uncompressed_frame_header(const uint8_t *data, int size, |
| 3525 | const char *filename); |
| 3526 | |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3527 | typedef struct { |
| 3528 | uint32_t tg_hdr_size; |
| 3529 | uint32_t frame_header_size; |
| 3530 | } LargeTileFrameOBU; |
| 3531 | |
| 3532 | // Initialize OBU header for large scale tile case. |
| 3533 | static uint32_t init_large_scale_tile_obu_header( |
| 3534 | AV1_COMP *const cpi, uint8_t **data, struct aom_write_bit_buffer *saved_wb, |
| 3535 | LargeTileFrameOBU *lst_obu) { |
Tarundeep Singh | 3b3e9e1 | 2021-03-16 14:58:27 +0530 | [diff] [blame] | 3536 | AV1LevelParams *const level_params = &cpi->ppi->level_params; |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3537 | CurrentFrame *const current_frame = &cpi->common.current_frame; |
| 3538 | // For large_scale_tile case, we always have only one tile group, so it can |
| 3539 | // be written as an OBU_FRAME. |
| 3540 | const OBU_TYPE obu_type = OBU_FRAME; |
Tarundeep Singh | d37807f | 2021-03-31 17:30:55 +0530 | [diff] [blame] | 3541 | lst_obu->tg_hdr_size = av1_write_obu_header( |
| 3542 | level_params, &cpi->frame_header_count, obu_type, 0, *data); |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3543 | *data += lst_obu->tg_hdr_size; |
| 3544 | |
| 3545 | const uint32_t frame_header_size = |
Vishesh | 8bd59d9 | 2021-04-27 11:34:17 +0530 | [diff] [blame] | 3546 | write_frame_header_obu(cpi, &cpi->td.mb.e_mbd, saved_wb, *data, 0); |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3547 | *data += frame_header_size; |
| 3548 | lst_obu->frame_header_size = frame_header_size; |
| 3549 | // (yunqing) This test ensures the correctness of large scale tile coding. |
| 3550 | if (cpi->oxcf.tile_cfg.enable_ext_tile_debug) { |
| 3551 | char fn[20] = "./fh"; |
| 3552 | fn[4] = current_frame->frame_number / 100 + '0'; |
| 3553 | fn[5] = (current_frame->frame_number % 100) / 10 + '0'; |
| 3554 | fn[6] = (current_frame->frame_number % 10) + '0'; |
| 3555 | fn[7] = '\0'; |
| 3556 | av1_print_uncompressed_frame_header(*data - frame_header_size, |
| 3557 | frame_header_size, fn); |
| 3558 | } |
| 3559 | return frame_header_size; |
| 3560 | } |
| 3561 | |
| 3562 | // Write total buffer size and related information into the OBU header for large |
| 3563 | // scale tile case. |
| 3564 | static void write_large_scale_tile_obu_size( |
| 3565 | const CommonTileParams *const tiles, uint8_t *const dst, uint8_t *data, |
| 3566 | struct aom_write_bit_buffer *saved_wb, LargeTileFrameOBU *const lst_obu, |
| 3567 | int have_tiles, uint32_t *total_size, int max_tile_size, |
| 3568 | int max_tile_col_size) { |
| 3569 | int tile_size_bytes = 0; |
| 3570 | int tile_col_size_bytes = 0; |
| 3571 | if (have_tiles) { |
| 3572 | *total_size = remux_tiles( |
| 3573 | tiles, data, *total_size - lst_obu->frame_header_size, max_tile_size, |
| 3574 | max_tile_col_size, &tile_size_bytes, &tile_col_size_bytes); |
| 3575 | *total_size += lst_obu->frame_header_size; |
| 3576 | } |
| 3577 | |
| 3578 | // In EXT_TILE case, only use 1 tile group. Follow the obu syntax, write |
| 3579 | // current tile group size before tile data(include tile column header). |
| 3580 | // Tile group size doesn't include the bytes storing tg size. |
| 3581 | *total_size += lst_obu->tg_hdr_size; |
| 3582 | const uint32_t obu_payload_size = *total_size - lst_obu->tg_hdr_size; |
| 3583 | const size_t length_field_size = |
Vishesh | 5849e6d | 2021-04-23 19:15:29 +0530 | [diff] [blame] | 3584 | av1_obu_memmove(lst_obu->tg_hdr_size, obu_payload_size, dst); |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3585 | if (av1_write_uleb_obu_size(lst_obu->tg_hdr_size, obu_payload_size, dst) != |
| 3586 | AOM_CODEC_OK) |
| 3587 | assert(0); |
| 3588 | |
| 3589 | *total_size += (uint32_t)length_field_size; |
| 3590 | saved_wb->bit_buffer += length_field_size; |
| 3591 | |
| 3592 | // Now fill in the gaps in the uncompressed header. |
| 3593 | if (have_tiles) { |
| 3594 | assert(tile_col_size_bytes >= 1 && tile_col_size_bytes <= 4); |
| 3595 | aom_wb_overwrite_literal(saved_wb, tile_col_size_bytes - 1, 2); |
| 3596 | |
| 3597 | assert(tile_size_bytes >= 1 && tile_size_bytes <= 4); |
| 3598 | aom_wb_overwrite_literal(saved_wb, tile_size_bytes - 1, 2); |
| 3599 | } |
| 3600 | } |
| 3601 | |
| 3602 | // Store information on each large scale tile in the OBU header. |
| 3603 | static void write_large_scale_tile_obu( |
| 3604 | AV1_COMP *const cpi, uint8_t *const dst, LargeTileFrameOBU *const lst_obu, |
| 3605 | int *const largest_tile_id, uint32_t *total_size, const int have_tiles, |
| 3606 | unsigned int *const max_tile_size, unsigned int *const max_tile_col_size) { |
Thomas Davies | 4822e14 | 2017-10-10 11:30:36 +0100 | [diff] [blame] | 3607 | AV1_COMMON *const cm = &cpi->common; |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 3608 | const CommonTileParams *const tiles = &cm->tiles; |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3609 | |
David Turner | e7fea8e | 2018-12-10 15:57:05 +0000 | [diff] [blame] | 3610 | TileBufferEnc tile_buffers[MAX_TILE_ROWS][MAX_TILE_COLS]; |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 3611 | const int tile_cols = tiles->cols; |
| 3612 | const int tile_rows = tiles->rows; |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3613 | unsigned int tile_size = 0; |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3614 | |
Vishesh | 5849e6d | 2021-04-23 19:15:29 +0530 | [diff] [blame] | 3615 | av1_reset_pack_bs_thread_data(&cpi->td); |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3616 | for (int tile_col = 0; tile_col < tile_cols; tile_col++) { |
| 3617 | TileInfo tile_info; |
| 3618 | const int is_last_col = (tile_col == tile_cols - 1); |
| 3619 | const uint32_t col_offset = *total_size; |
| 3620 | |
| 3621 | av1_tile_set_col(&tile_info, cm, tile_col); |
| 3622 | |
| 3623 | // The last column does not have a column header |
| 3624 | if (!is_last_col) *total_size += 4; |
| 3625 | |
| 3626 | for (int tile_row = 0; tile_row < tile_rows; tile_row++) { |
| 3627 | TileBufferEnc *const buf = &tile_buffers[tile_row][tile_col]; |
| 3628 | const int data_offset = have_tiles ? 4 : 0; |
| 3629 | const int tile_idx = tile_row * tile_cols + tile_col; |
| 3630 | TileDataEnc *this_tile = &cpi->tile_data[tile_idx]; |
| 3631 | av1_tile_set_row(&tile_info, cm, tile_row); |
| 3632 | aom_writer mode_bc; |
| 3633 | |
| 3634 | buf->data = dst + *total_size + lst_obu->tg_hdr_size; |
| 3635 | |
| 3636 | // Is CONFIG_EXT_TILE = 1, every tile in the row has a header, |
| 3637 | // even for the last one, unless no tiling is used at all. |
| 3638 | *total_size += data_offset; |
| 3639 | cpi->td.mb.e_mbd.tile_ctx = &this_tile->tctx; |
| 3640 | mode_bc.allow_update_cdf = !tiles->large_scale; |
| 3641 | mode_bc.allow_update_cdf = |
| 3642 | mode_bc.allow_update_cdf && !cm->features.disable_cdf_update; |
| 3643 | aom_start_encode(&mode_bc, buf->data + data_offset); |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 3644 | write_modes(cpi, &cpi->td, &tile_info, &mode_bc, tile_row, tile_col); |
Satheesh Kumar | 173c140 | 2023-12-11 15:44:04 +0530 | [diff] [blame] | 3645 | if (aom_stop_encode(&mode_bc) < 0) { |
| 3646 | aom_internal_error(cm->error, AOM_CODEC_ERROR, "Error writing modes"); |
| 3647 | } |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3648 | tile_size = mode_bc.pos; |
| 3649 | buf->size = tile_size; |
| 3650 | |
| 3651 | // Record the maximum tile size we see, so we can compact headers later. |
| 3652 | if (tile_size > *max_tile_size) { |
| 3653 | *max_tile_size = tile_size; |
| 3654 | *largest_tile_id = tile_cols * tile_row + tile_col; |
| 3655 | } |
| 3656 | |
| 3657 | if (have_tiles) { |
| 3658 | // tile header: size of this tile, or copy offset |
| 3659 | uint32_t tile_header = tile_size - AV1_MIN_TILE_SIZE_BYTES; |
| 3660 | const int tile_copy_mode = |
| 3661 | ((AOMMAX(tiles->width, tiles->height) << MI_SIZE_LOG2) <= 256) ? 1 |
| 3662 | : 0; |
| 3663 | |
| 3664 | // If tile_copy_mode = 1, check if this tile is a copy tile. |
| 3665 | // Very low chances to have copy tiles on the key frames, so don't |
| 3666 | // search on key frames to reduce unnecessary search. |
| 3667 | if (cm->current_frame.frame_type != KEY_FRAME && tile_copy_mode) { |
| 3668 | const int identical_tile_offset = |
| 3669 | find_identical_tile(tile_row, tile_col, tile_buffers); |
| 3670 | |
| 3671 | // Indicate a copy-tile by setting the most significant bit. |
| 3672 | // The row-offset to copy from is stored in the highest byte. |
| 3673 | // remux_tiles will move these around later |
| 3674 | if (identical_tile_offset > 0) { |
| 3675 | tile_size = 0; |
| 3676 | tile_header = identical_tile_offset | 0x80; |
| 3677 | tile_header <<= 24; |
| 3678 | } |
| 3679 | } |
| 3680 | |
James Zern | 8c3db28 | 2022-08-30 17:25:02 -0700 | [diff] [blame] | 3681 | mem_put_le32(buf->data, (MEM_VALUE_T)tile_header); |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3682 | } |
| 3683 | |
| 3684 | *total_size += tile_size; |
| 3685 | } |
| 3686 | if (!is_last_col) { |
| 3687 | uint32_t col_size = *total_size - col_offset - 4; |
| 3688 | mem_put_le32(dst + col_offset + lst_obu->tg_hdr_size, col_size); |
| 3689 | |
| 3690 | // Record the maximum tile column size we see. |
| 3691 | *max_tile_col_size = AOMMAX(*max_tile_col_size, col_size); |
| 3692 | } |
| 3693 | } |
Vishesh | 5849e6d | 2021-04-23 19:15:29 +0530 | [diff] [blame] | 3694 | av1_accumulate_pack_bs_thread_data(cpi, &cpi->td); |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3695 | } |
| 3696 | |
| 3697 | // Packs information in the obu header for large scale tiles. |
| 3698 | static INLINE uint32_t pack_large_scale_tiles_in_tg_obus( |
| 3699 | AV1_COMP *const cpi, uint8_t *const dst, |
| 3700 | struct aom_write_bit_buffer *saved_wb, int *const largest_tile_id) { |
| 3701 | AV1_COMMON *const cm = &cpi->common; |
| 3702 | const CommonTileParams *const tiles = &cm->tiles; |
| 3703 | uint32_t total_size = 0; |
James Zern | 2a7629e | 2018-03-23 00:06:45 -0700 | [diff] [blame] | 3704 | unsigned int max_tile_size = 0; |
| 3705 | unsigned int max_tile_col_size = 0; |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3706 | const int have_tiles = tiles->cols * tiles->rows > 1; |
| 3707 | uint8_t *data = dst; |
| 3708 | |
| 3709 | LargeTileFrameOBU lst_obu; |
| 3710 | |
| 3711 | total_size += |
| 3712 | init_large_scale_tile_obu_header(cpi, &data, saved_wb, &lst_obu); |
| 3713 | |
| 3714 | write_large_scale_tile_obu(cpi, dst, &lst_obu, largest_tile_id, &total_size, |
| 3715 | have_tiles, &max_tile_size, &max_tile_col_size); |
| 3716 | |
| 3717 | write_large_scale_tile_obu_size(tiles, dst, data, saved_wb, &lst_obu, |
| 3718 | have_tiles, &total_size, max_tile_size, |
| 3719 | max_tile_col_size); |
| 3720 | |
| 3721 | return total_size; |
| 3722 | } |
| 3723 | |
Vishesh | 268a08f | 2021-04-22 17:28:44 +0530 | [diff] [blame] | 3724 | // Writes obu, tile group and uncompressed headers to bitstream. |
Vishesh | 8bd59d9 | 2021-04-27 11:34:17 +0530 | [diff] [blame] | 3725 | void av1_write_obu_tg_tile_headers(AV1_COMP *const cpi, MACROBLOCKD *const xd, |
Vishesh | 268a08f | 2021-04-22 17:28:44 +0530 | [diff] [blame] | 3726 | PackBSParams *const pack_bs_params, |
| 3727 | const int tile_idx) { |
| 3728 | AV1_COMMON *const cm = &cpi->common; |
| 3729 | const CommonTileParams *const tiles = &cm->tiles; |
| 3730 | int *const curr_tg_hdr_size = &pack_bs_params->curr_tg_hdr_size; |
| 3731 | const int tg_size = |
| 3732 | (tiles->rows * tiles->cols + cpi->num_tg - 1) / cpi->num_tg; |
| 3733 | |
| 3734 | // Write Tile group, frame and OBU header |
| 3735 | // A new tile group begins at this tile. Write the obu header and |
| 3736 | // tile group header |
| 3737 | const OBU_TYPE obu_type = (cpi->num_tg == 1) ? OBU_FRAME : OBU_TILE_GROUP; |
| 3738 | *curr_tg_hdr_size = av1_write_obu_header( |
| 3739 | &cpi->ppi->level_params, &cpi->frame_header_count, obu_type, |
| 3740 | pack_bs_params->obu_extn_header, pack_bs_params->tile_data_curr); |
| 3741 | pack_bs_params->obu_header_size = *curr_tg_hdr_size; |
| 3742 | |
| 3743 | if (cpi->num_tg == 1) |
| 3744 | *curr_tg_hdr_size += write_frame_header_obu( |
Vishesh | 8bd59d9 | 2021-04-27 11:34:17 +0530 | [diff] [blame] | 3745 | cpi, xd, pack_bs_params->saved_wb, |
Vishesh | 268a08f | 2021-04-22 17:28:44 +0530 | [diff] [blame] | 3746 | pack_bs_params->tile_data_curr + *curr_tg_hdr_size, 0); |
| 3747 | *curr_tg_hdr_size += write_tile_group_header( |
| 3748 | pack_bs_params->tile_data_curr + *curr_tg_hdr_size, tile_idx, |
| 3749 | AOMMIN(tile_idx + tg_size - 1, tiles->cols * tiles->rows - 1), |
| 3750 | (tiles->log2_rows + tiles->log2_cols), cpi->num_tg > 1); |
| 3751 | *pack_bs_params->total_size += *curr_tg_hdr_size; |
| 3752 | } |
| 3753 | |
Vishesh | b2c65b9 | 2021-03-12 11:46:45 +0530 | [diff] [blame] | 3754 | // Pack tile data in the bitstream with tile_group, frame |
| 3755 | // and OBU header. |
Vishesh | 5849e6d | 2021-04-23 19:15:29 +0530 | [diff] [blame] | 3756 | void av1_pack_tile_info(AV1_COMP *const cpi, ThreadData *const td, |
| 3757 | PackBSParams *const pack_bs_params) { |
Vishesh | b2c65b9 | 2021-03-12 11:46:45 +0530 | [diff] [blame] | 3758 | aom_writer mode_bc; |
| 3759 | AV1_COMMON *const cm = &cpi->common; |
Vishesh | b2c65b9 | 2021-03-12 11:46:45 +0530 | [diff] [blame] | 3760 | int tile_row = pack_bs_params->tile_row; |
| 3761 | int tile_col = pack_bs_params->tile_col; |
Vishesh | b2c65b9 | 2021-03-12 11:46:45 +0530 | [diff] [blame] | 3762 | uint32_t *const total_size = pack_bs_params->total_size; |
Vishesh | b2c65b9 | 2021-03-12 11:46:45 +0530 | [diff] [blame] | 3763 | TileInfo tile_info; |
| 3764 | av1_tile_set_col(&tile_info, cm, tile_col); |
| 3765 | av1_tile_set_row(&tile_info, cm, tile_row); |
| 3766 | mode_bc.allow_update_cdf = 1; |
| 3767 | mode_bc.allow_update_cdf = |
| 3768 | mode_bc.allow_update_cdf && !cm->features.disable_cdf_update; |
| 3769 | |
Vishesh | b2c65b9 | 2021-03-12 11:46:45 +0530 | [diff] [blame] | 3770 | unsigned int tile_size; |
| 3771 | |
Vishesh | 268a08f | 2021-04-22 17:28:44 +0530 | [diff] [blame] | 3772 | const int num_planes = av1_num_planes(cm); |
| 3773 | av1_reset_loop_restoration(&td->mb.e_mbd, num_planes); |
Vishesh | b2c65b9 | 2021-03-12 11:46:45 +0530 | [diff] [blame] | 3774 | |
| 3775 | pack_bs_params->buf.data = pack_bs_params->dst + *total_size; |
| 3776 | |
| 3777 | // The last tile of the tile group does not have a header. |
| 3778 | if (!pack_bs_params->is_last_tile_in_tg) *total_size += 4; |
| 3779 | |
| 3780 | // Pack tile data |
| 3781 | aom_start_encode(&mode_bc, pack_bs_params->dst + *total_size); |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 3782 | write_modes(cpi, td, &tile_info, &mode_bc, tile_row, tile_col); |
Satheesh Kumar | 173c140 | 2023-12-11 15:44:04 +0530 | [diff] [blame] | 3783 | if (aom_stop_encode(&mode_bc) < 0) { |
| 3784 | aom_internal_error(td->mb.e_mbd.error_info, AOM_CODEC_ERROR, |
| 3785 | "Error writing modes"); |
| 3786 | } |
Vishesh | b2c65b9 | 2021-03-12 11:46:45 +0530 | [diff] [blame] | 3787 | tile_size = mode_bc.pos; |
| 3788 | assert(tile_size >= AV1_MIN_TILE_SIZE_BYTES); |
| 3789 | |
| 3790 | pack_bs_params->buf.size = tile_size; |
| 3791 | |
| 3792 | // Write tile size |
| 3793 | if (!pack_bs_params->is_last_tile_in_tg) { |
| 3794 | // size of this tile |
| 3795 | mem_put_le32(pack_bs_params->buf.data, tile_size - AV1_MIN_TILE_SIZE_BYTES); |
| 3796 | } |
| 3797 | } |
| 3798 | |
Vishesh | 5849e6d | 2021-04-23 19:15:29 +0530 | [diff] [blame] | 3799 | void av1_write_last_tile_info( |
| 3800 | AV1_COMP *const cpi, const FrameHeaderInfo *fh_info, |
| 3801 | struct aom_write_bit_buffer *saved_wb, size_t *curr_tg_data_size, |
| 3802 | uint8_t *curr_tg_start, uint32_t *const total_size, |
| 3803 | uint8_t **tile_data_start, int *const largest_tile_id, |
| 3804 | int *const is_first_tg, uint32_t obu_header_size, uint8_t obu_extn_header) { |
Vishesh | c1516b9 | 2021-04-05 15:33:23 +0530 | [diff] [blame] | 3805 | // write current tile group size |
| 3806 | const uint32_t obu_payload_size = |
| 3807 | (uint32_t)(*curr_tg_data_size) - obu_header_size; |
| 3808 | const size_t length_field_size = |
Vishesh | 5849e6d | 2021-04-23 19:15:29 +0530 | [diff] [blame] | 3809 | av1_obu_memmove(obu_header_size, obu_payload_size, curr_tg_start); |
Vishesh | c1516b9 | 2021-04-05 15:33:23 +0530 | [diff] [blame] | 3810 | if (av1_write_uleb_obu_size(obu_header_size, obu_payload_size, |
| 3811 | curr_tg_start) != AOM_CODEC_OK) { |
| 3812 | assert(0); |
| 3813 | } |
| 3814 | *curr_tg_data_size += (int)length_field_size; |
| 3815 | *total_size += (uint32_t)length_field_size; |
| 3816 | *tile_data_start += length_field_size; |
| 3817 | if (cpi->num_tg == 1) { |
| 3818 | // if this tg is combined with the frame header then update saved |
chiyotsai | 7881d8f | 2021-04-19 18:11:51 -0700 | [diff] [blame] | 3819 | // frame header base offset according to length field size |
Vishesh | c1516b9 | 2021-04-05 15:33:23 +0530 | [diff] [blame] | 3820 | saved_wb->bit_buffer += length_field_size; |
| 3821 | } |
| 3822 | |
| 3823 | if (!(*is_first_tg) && cpi->common.features.error_resilient_mode) { |
| 3824 | // Make room for a duplicate Frame Header OBU. |
| 3825 | memmove(curr_tg_start + fh_info->total_length, curr_tg_start, |
| 3826 | *curr_tg_data_size); |
| 3827 | |
| 3828 | // Insert a copy of the Frame Header OBU. |
| 3829 | memcpy(curr_tg_start, fh_info->frame_header, fh_info->total_length); |
| 3830 | |
| 3831 | // Force context update tile to be the first tile in error |
chiyotsai | 7881d8f | 2021-04-19 18:11:51 -0700 | [diff] [blame] | 3832 | // resilient mode as the duplicate frame headers will have |
Vishesh | c1516b9 | 2021-04-05 15:33:23 +0530 | [diff] [blame] | 3833 | // context_update_tile_id set to 0 |
| 3834 | *largest_tile_id = 0; |
| 3835 | |
| 3836 | // Rewrite the OBU header to change the OBU type to Redundant Frame |
| 3837 | // Header. |
| 3838 | av1_write_obu_header(&cpi->ppi->level_params, &cpi->frame_header_count, |
| 3839 | OBU_REDUNDANT_FRAME_HEADER, obu_extn_header, |
| 3840 | &curr_tg_start[fh_info->obu_header_byte_offset]); |
| 3841 | |
| 3842 | *curr_tg_data_size += (int)(fh_info->total_length); |
| 3843 | *total_size += (uint32_t)(fh_info->total_length); |
| 3844 | } |
| 3845 | *is_first_tg = 0; |
| 3846 | } |
| 3847 | |
Vishesh | 5849e6d | 2021-04-23 19:15:29 +0530 | [diff] [blame] | 3848 | void av1_reset_pack_bs_thread_data(ThreadData *const td) { |
Cherma Rajan A | d0f59a7 | 2021-04-13 12:43:54 +0530 | [diff] [blame] | 3849 | td->coefficient_size = 0; |
| 3850 | td->max_mv_magnitude = 0; |
| 3851 | av1_zero(td->interp_filter_selected); |
| 3852 | } |
| 3853 | |
Vishesh | 5849e6d | 2021-04-23 19:15:29 +0530 | [diff] [blame] | 3854 | void av1_accumulate_pack_bs_thread_data(AV1_COMP *const cpi, |
| 3855 | ThreadData const *td) { |
Aasaipriya Chandran | ad28124 | 2021-05-25 20:25:39 +0530 | [diff] [blame] | 3856 | int do_max_mv_magnitude_update = 1; |
Cherma Rajan A | d0f59a7 | 2021-04-13 12:43:54 +0530 | [diff] [blame] | 3857 | cpi->rc.coefficient_size += td->coefficient_size; |
| 3858 | |
Aasaipriya Chandran | ad28124 | 2021-05-25 20:25:39 +0530 | [diff] [blame] | 3859 | // Disable max_mv_magnitude update for parallel frames based on update flag. |
| 3860 | if (!cpi->do_frame_data_update) do_max_mv_magnitude_update = 0; |
Aasaipriya Chandran | ad28124 | 2021-05-25 20:25:39 +0530 | [diff] [blame] | 3861 | |
| 3862 | if (cpi->sf.mv_sf.auto_mv_step_size && do_max_mv_magnitude_update) |
Cherma Rajan A | d0f59a7 | 2021-04-13 12:43:54 +0530 | [diff] [blame] | 3863 | cpi->mv_search_params.max_mv_magnitude = |
| 3864 | AOMMAX(cpi->mv_search_params.max_mv_magnitude, td->max_mv_magnitude); |
| 3865 | |
| 3866 | for (InterpFilter filter = EIGHTTAP_REGULAR; filter < SWITCHABLE; filter++) |
| 3867 | cpi->common.cur_frame->interp_filter_selected[filter] += |
| 3868 | td->interp_filter_selected[filter]; |
| 3869 | } |
| 3870 | |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3871 | // Store information related to each default tile in the OBU header. |
| 3872 | static void write_tile_obu( |
| 3873 | AV1_COMP *const cpi, uint8_t *const dst, uint32_t *total_size, |
Vishesh | b2c65b9 | 2021-03-12 11:46:45 +0530 | [diff] [blame] | 3874 | struct aom_write_bit_buffer *saved_wb, uint8_t obu_extn_header, |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3875 | const FrameHeaderInfo *fh_info, int *const largest_tile_id, |
| 3876 | unsigned int *max_tile_size, uint32_t *const obu_header_size, |
| 3877 | uint8_t **tile_data_start) { |
| 3878 | AV1_COMMON *const cm = &cpi->common; |
Vishesh | 8bd59d9 | 2021-04-27 11:34:17 +0530 | [diff] [blame] | 3879 | MACROBLOCKD *const xd = &cpi->td.mb.e_mbd; |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3880 | const CommonTileParams *const tiles = &cm->tiles; |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3881 | const int tile_cols = tiles->cols; |
| 3882 | const int tile_rows = tiles->rows; |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3883 | // Fixed size tile groups for the moment |
Urvang Joshi | 1de67aa | 2020-03-20 11:21:57 -0700 | [diff] [blame] | 3884 | const int num_tg_hdrs = cpi->num_tg; |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3885 | const int tg_size = (tile_rows * tile_cols + num_tg_hdrs - 1) / num_tg_hdrs; |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3886 | int tile_count = 0; |
Vishesh | b2c65b9 | 2021-03-12 11:46:45 +0530 | [diff] [blame] | 3887 | size_t curr_tg_data_size = 0; |
| 3888 | uint8_t *tile_data_curr = dst; |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3889 | int new_tg = 1; |
Vishesh | b2c65b9 | 2021-03-12 11:46:45 +0530 | [diff] [blame] | 3890 | int is_first_tg = 1; |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3891 | |
Vishesh | 5849e6d | 2021-04-23 19:15:29 +0530 | [diff] [blame] | 3892 | av1_reset_pack_bs_thread_data(&cpi->td); |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3893 | for (int tile_row = 0; tile_row < tile_rows; tile_row++) { |
| 3894 | for (int tile_col = 0; tile_col < tile_cols; tile_col++) { |
James Zern | a60ff58 | 2018-02-24 14:02:12 -0800 | [diff] [blame] | 3895 | const int tile_idx = tile_row * tile_cols + tile_col; |
James Zern | a60ff58 | 2018-02-24 14:02:12 -0800 | [diff] [blame] | 3896 | TileDataEnc *this_tile = &cpi->tile_data[tile_idx]; |
Vishesh | b2c65b9 | 2021-03-12 11:46:45 +0530 | [diff] [blame] | 3897 | |
James Zern | a60ff58 | 2018-02-24 14:02:12 -0800 | [diff] [blame] | 3898 | int is_last_tile_in_tg = 0; |
James Zern | a60ff58 | 2018-02-24 14:02:12 -0800 | [diff] [blame] | 3899 | if (new_tg) { |
Vishesh | b2c65b9 | 2021-03-12 11:46:45 +0530 | [diff] [blame] | 3900 | tile_data_curr = dst + *total_size; |
James Zern | a60ff58 | 2018-02-24 14:02:12 -0800 | [diff] [blame] | 3901 | tile_count = 0; |
| 3902 | } |
| 3903 | tile_count++; |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3904 | |
Vishesh | b2c65b9 | 2021-03-12 11:46:45 +0530 | [diff] [blame] | 3905 | if (tile_count == tg_size || tile_idx == (tile_cols * tile_rows - 1)) |
James Zern | a60ff58 | 2018-02-24 14:02:12 -0800 | [diff] [blame] | 3906 | is_last_tile_in_tg = 1; |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3907 | |
Vishesh | 8bd59d9 | 2021-04-27 11:34:17 +0530 | [diff] [blame] | 3908 | xd->tile_ctx = &this_tile->tctx; |
Soo-Chul Han | 13f0d9c | 2017-10-22 21:55:52 -0400 | [diff] [blame] | 3909 | |
Vishesh | b2c65b9 | 2021-03-12 11:46:45 +0530 | [diff] [blame] | 3910 | // PackBSParams stores all parameters required to pack tile and header |
| 3911 | // info. |
| 3912 | PackBSParams pack_bs_params; |
| 3913 | pack_bs_params.dst = dst; |
| 3914 | pack_bs_params.curr_tg_hdr_size = 0; |
| 3915 | pack_bs_params.is_last_tile_in_tg = is_last_tile_in_tg; |
| 3916 | pack_bs_params.new_tg = new_tg; |
| 3917 | pack_bs_params.obu_extn_header = obu_extn_header; |
Vishesh | c1516b9 | 2021-04-05 15:33:23 +0530 | [diff] [blame] | 3918 | pack_bs_params.obu_header_size = 0; |
Vishesh | b2c65b9 | 2021-03-12 11:46:45 +0530 | [diff] [blame] | 3919 | pack_bs_params.saved_wb = saved_wb; |
| 3920 | pack_bs_params.tile_col = tile_col; |
| 3921 | pack_bs_params.tile_row = tile_row; |
| 3922 | pack_bs_params.tile_data_curr = tile_data_curr; |
| 3923 | pack_bs_params.total_size = total_size; |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3924 | |
Vishesh | 268a08f | 2021-04-22 17:28:44 +0530 | [diff] [blame] | 3925 | if (new_tg) |
Vishesh | 8bd59d9 | 2021-04-27 11:34:17 +0530 | [diff] [blame] | 3926 | av1_write_obu_tg_tile_headers(cpi, xd, &pack_bs_params, tile_idx); |
Vishesh | 268a08f | 2021-04-22 17:28:44 +0530 | [diff] [blame] | 3927 | |
Vishesh | 5849e6d | 2021-04-23 19:15:29 +0530 | [diff] [blame] | 3928 | av1_pack_tile_info(cpi, &cpi->td, &pack_bs_params); |
Vishesh | b2c65b9 | 2021-03-12 11:46:45 +0530 | [diff] [blame] | 3929 | |
| 3930 | if (new_tg) { |
| 3931 | curr_tg_data_size = pack_bs_params.curr_tg_hdr_size; |
| 3932 | *tile_data_start += pack_bs_params.curr_tg_hdr_size; |
Vishesh | c1516b9 | 2021-04-05 15:33:23 +0530 | [diff] [blame] | 3933 | *obu_header_size = pack_bs_params.obu_header_size; |
Vishesh | b2c65b9 | 2021-03-12 11:46:45 +0530 | [diff] [blame] | 3934 | new_tg = 0; |
| 3935 | } |
| 3936 | if (is_last_tile_in_tg) new_tg = 1; |
| 3937 | |
| 3938 | curr_tg_data_size += |
| 3939 | (pack_bs_params.buf.size + (is_last_tile_in_tg ? 0 : 4)); |
| 3940 | |
| 3941 | if (pack_bs_params.buf.size > *max_tile_size) { |
| 3942 | *largest_tile_id = tile_idx; |
| 3943 | *max_tile_size = (unsigned int)pack_bs_params.buf.size; |
James Zern | a60ff58 | 2018-02-24 14:02:12 -0800 | [diff] [blame] | 3944 | } |
Thomas Davies | 4822e14 | 2017-10-10 11:30:36 +0100 | [diff] [blame] | 3945 | |
Vishesh | c1516b9 | 2021-04-05 15:33:23 +0530 | [diff] [blame] | 3946 | if (is_last_tile_in_tg) |
Vishesh | 5849e6d | 2021-04-23 19:15:29 +0530 | [diff] [blame] | 3947 | av1_write_last_tile_info(cpi, fh_info, saved_wb, &curr_tg_data_size, |
| 3948 | tile_data_curr, total_size, tile_data_start, |
| 3949 | largest_tile_id, &is_first_tg, |
| 3950 | *obu_header_size, obu_extn_header); |
Vishesh | b2c65b9 | 2021-03-12 11:46:45 +0530 | [diff] [blame] | 3951 | *total_size += (uint32_t)pack_bs_params.buf.size; |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3952 | } |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 3953 | } |
Vishesh | 5849e6d | 2021-04-23 19:15:29 +0530 | [diff] [blame] | 3954 | av1_accumulate_pack_bs_thread_data(cpi, &cpi->td); |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3955 | } |
James Zern | e7c8237 | 2018-03-18 20:43:55 -0700 | [diff] [blame] | 3956 | |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3957 | // Write total buffer size and related information into the OBU header for |
| 3958 | // default tile case. |
| 3959 | static void write_tile_obu_size(AV1_COMP *const cpi, uint8_t *const dst, |
| 3960 | struct aom_write_bit_buffer *saved_wb, |
| 3961 | int largest_tile_id, uint32_t *const total_size, |
| 3962 | unsigned int max_tile_size, |
| 3963 | uint32_t obu_header_size, |
| 3964 | uint8_t *tile_data_start) { |
| 3965 | const CommonTileParams *const tiles = &cpi->common.tiles; |
James Zern | e7c8237 | 2018-03-18 20:43:55 -0700 | [diff] [blame] | 3966 | |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3967 | // Fill in context_update_tile_id indicating the tile to use for the |
| 3968 | // cdf update. The encoder currently sets it to the largest tile |
| 3969 | // (but is up to the encoder) |
| 3970 | aom_wb_overwrite_literal(saved_wb, largest_tile_id, |
| 3971 | (tiles->log2_cols + tiles->log2_rows)); |
| 3972 | // If more than one tile group. tile_size_bytes takes the default value 4 |
| 3973 | // and does not need to be set. For a single tile group it is set in the |
| 3974 | // section below. |
| 3975 | if (cpi->num_tg != 1) return; |
| 3976 | int tile_size_bytes = 4, unused; |
| 3977 | const uint32_t tile_data_offset = (uint32_t)(tile_data_start - dst); |
| 3978 | const uint32_t tile_data_size = *total_size - tile_data_offset; |
James Zern | e7c8237 | 2018-03-18 20:43:55 -0700 | [diff] [blame] | 3979 | |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3980 | *total_size = remux_tiles(tiles, tile_data_start, tile_data_size, |
| 3981 | max_tile_size, 0, &tile_size_bytes, &unused); |
| 3982 | *total_size += tile_data_offset; |
| 3983 | assert(tile_size_bytes >= 1 && tile_size_bytes <= 4); |
Dominic Symes | ccc3878 | 2018-05-04 18:24:00 +0200 | [diff] [blame] | 3984 | |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3985 | aom_wb_overwrite_literal(saved_wb, tile_size_bytes - 1, 2); |
Dominic Symes | ccc3878 | 2018-05-04 18:24:00 +0200 | [diff] [blame] | 3986 | |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 3987 | // Update the OBU length if remux_tiles() reduced the size. |
| 3988 | uint64_t payload_size; |
| 3989 | size_t length_field_size; |
| 3990 | int res = |
| 3991 | aom_uleb_decode(dst + obu_header_size, *total_size - obu_header_size, |
| 3992 | &payload_size, &length_field_size); |
| 3993 | assert(res == 0); |
| 3994 | (void)res; |
| 3995 | |
| 3996 | const uint64_t new_payload_size = |
| 3997 | *total_size - obu_header_size - length_field_size; |
| 3998 | if (new_payload_size != payload_size) { |
| 3999 | size_t new_length_field_size; |
| 4000 | res = aom_uleb_encode(new_payload_size, length_field_size, |
| 4001 | dst + obu_header_size, &new_length_field_size); |
| 4002 | assert(res == 0); |
| 4003 | if (new_length_field_size < length_field_size) { |
| 4004 | const size_t src_offset = obu_header_size + length_field_size; |
| 4005 | const size_t dst_offset = obu_header_size + new_length_field_size; |
| 4006 | memmove(dst + dst_offset, dst + src_offset, (size_t)payload_size); |
| 4007 | *total_size -= (int)(length_field_size - new_length_field_size); |
James Zern | e7c8237 | 2018-03-18 20:43:55 -0700 | [diff] [blame] | 4008 | } |
| 4009 | } |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 4010 | } |
| 4011 | |
Vishesh | 6465ae6 | 2021-05-26 15:12:09 +0530 | [diff] [blame] | 4012 | // As per the experiments, single-thread bitstream packing is better for |
| 4013 | // frames with a smaller bitstream size. This behavior is due to setup time |
| 4014 | // overhead of multithread function would be more than that of time required |
Cherma Rajan A | 248e8d3 | 2021-06-15 09:06:38 +0530 | [diff] [blame] | 4015 | // to pack the smaller bitstream of such frames. This function computes the |
| 4016 | // number of required number of workers based on setup time overhead and job |
| 4017 | // dispatch time overhead for given tiles and available workers. |
| 4018 | int calc_pack_bs_mt_workers(const TileDataEnc *tile_data, int num_tiles, |
Cherma Rajan A | a92f3c8 | 2022-02-17 20:14:47 +0530 | [diff] [blame] | 4019 | int avail_workers, bool pack_bs_mt_enabled) { |
| 4020 | if (!pack_bs_mt_enabled) return 1; |
Vishesh | 6465ae6 | 2021-05-26 15:12:09 +0530 | [diff] [blame] | 4021 | |
Vishesh | 6465ae6 | 2021-05-26 15:12:09 +0530 | [diff] [blame] | 4022 | uint64_t frame_abs_sum_level = 0; |
Cherma Rajan A | 248e8d3 | 2021-06-15 09:06:38 +0530 | [diff] [blame] | 4023 | |
Vishesh | 6465ae6 | 2021-05-26 15:12:09 +0530 | [diff] [blame] | 4024 | for (int idx = 0; idx < num_tiles; idx++) |
| 4025 | frame_abs_sum_level += tile_data[idx].abs_sum_level; |
Cherma Rajan A | 248e8d3 | 2021-06-15 09:06:38 +0530 | [diff] [blame] | 4026 | |
Cherma Rajan A | 248e8d3 | 2021-06-15 09:06:38 +0530 | [diff] [blame] | 4027 | int ideal_num_workers = 1; |
| 4028 | const float job_disp_time_const = (float)num_tiles * JOB_DISP_TIME_OH_CONST; |
| 4029 | float max_sum = 0.0; |
| 4030 | |
| 4031 | for (int num_workers = avail_workers; num_workers > 1; num_workers--) { |
| 4032 | const float fas_per_worker_const = |
| 4033 | ((float)(num_workers - 1) / num_workers) * frame_abs_sum_level; |
| 4034 | const float setup_time_const = (float)num_workers * SETUP_TIME_OH_CONST; |
| 4035 | const float this_sum = fas_per_worker_const - setup_time_const - |
| 4036 | job_disp_time_const / num_workers; |
| 4037 | |
| 4038 | if (this_sum > max_sum) { |
| 4039 | max_sum = this_sum; |
| 4040 | ideal_num_workers = num_workers; |
| 4041 | } |
| 4042 | } |
| 4043 | return ideal_num_workers; |
Vishesh | 6465ae6 | 2021-05-26 15:12:09 +0530 | [diff] [blame] | 4044 | } |
| 4045 | |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 4046 | static INLINE uint32_t pack_tiles_in_tg_obus( |
| 4047 | AV1_COMP *const cpi, uint8_t *const dst, |
| 4048 | struct aom_write_bit_buffer *saved_wb, uint8_t obu_extension_header, |
| 4049 | const FrameHeaderInfo *fh_info, int *const largest_tile_id) { |
| 4050 | const CommonTileParams *const tiles = &cpi->common.tiles; |
| 4051 | uint32_t total_size = 0; |
| 4052 | unsigned int max_tile_size = 0; |
| 4053 | uint32_t obu_header_size = 0; |
| 4054 | uint8_t *tile_data_start = dst; |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 4055 | const int tile_cols = tiles->cols; |
| 4056 | const int tile_rows = tiles->rows; |
Cherma Rajan A | ce0c423 | 2021-04-23 21:29:51 +0530 | [diff] [blame] | 4057 | const int num_tiles = tile_rows * tile_cols; |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 4058 | |
Cherma Rajan A | 248e8d3 | 2021-06-15 09:06:38 +0530 | [diff] [blame] | 4059 | const int num_workers = calc_pack_bs_mt_workers( |
Cherma Rajan A | a92f3c8 | 2022-02-17 20:14:47 +0530 | [diff] [blame] | 4060 | cpi->tile_data, num_tiles, cpi->mt_info.num_mod_workers[MOD_PACK_BS], |
| 4061 | cpi->mt_info.pack_bs_mt_enabled); |
Cherma Rajan A | ce0c423 | 2021-04-23 21:29:51 +0530 | [diff] [blame] | 4062 | |
Cherma Rajan A | 248e8d3 | 2021-06-15 09:06:38 +0530 | [diff] [blame] | 4063 | if (num_workers > 1) { |
Cherma Rajan A | ce0c423 | 2021-04-23 21:29:51 +0530 | [diff] [blame] | 4064 | av1_write_tile_obu_mt(cpi, dst, &total_size, saved_wb, obu_extension_header, |
| 4065 | fh_info, largest_tile_id, &max_tile_size, |
Cherma Rajan A | 248e8d3 | 2021-06-15 09:06:38 +0530 | [diff] [blame] | 4066 | &obu_header_size, &tile_data_start, num_workers); |
Cherma Rajan A | ce0c423 | 2021-04-23 21:29:51 +0530 | [diff] [blame] | 4067 | } else { |
| 4068 | write_tile_obu(cpi, dst, &total_size, saved_wb, obu_extension_header, |
| 4069 | fh_info, largest_tile_id, &max_tile_size, &obu_header_size, |
| 4070 | &tile_data_start); |
| 4071 | } |
| 4072 | |
| 4073 | if (num_tiles > 1) |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 4074 | write_tile_obu_size(cpi, dst, saved_wb, *largest_tile_id, &total_size, |
| 4075 | max_tile_size, obu_header_size, tile_data_start); |
Yaowu Xu | 8cabab8 | 2018-03-24 21:49:43 -0700 | [diff] [blame] | 4076 | return total_size; |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 4077 | } |
| 4078 | |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 4079 | static uint32_t write_tiles_in_tg_obus(AV1_COMP *const cpi, uint8_t *const dst, |
| 4080 | struct aom_write_bit_buffer *saved_wb, |
| 4081 | uint8_t obu_extension_header, |
| 4082 | const FrameHeaderInfo *fh_info, |
| 4083 | int *const largest_tile_id) { |
| 4084 | AV1_COMMON *const cm = &cpi->common; |
| 4085 | const CommonTileParams *const tiles = &cm->tiles; |
| 4086 | *largest_tile_id = 0; |
| 4087 | |
Cherma Rajan A | b4be7e5 | 2021-04-14 19:28:53 +0530 | [diff] [blame] | 4088 | // Select the coding strategy (temporal or spatial) |
Jingning Han | 564fe2c | 2022-03-08 23:35:01 -0800 | [diff] [blame] | 4089 | if (cm->seg.enabled && cm->seg.update_map) { |
| 4090 | if (cm->features.primary_ref_frame == PRIMARY_REF_NONE) { |
| 4091 | cm->seg.temporal_update = 0; |
| 4092 | } else { |
| 4093 | cm->seg.temporal_update = 1; |
| 4094 | if (cpi->td.rd_counts.seg_tmp_pred_cost[0] < |
| 4095 | cpi->td.rd_counts.seg_tmp_pred_cost[1]) |
| 4096 | cm->seg.temporal_update = 0; |
| 4097 | } |
| 4098 | } |
Cherma Rajan A | b4be7e5 | 2021-04-14 19:28:53 +0530 | [diff] [blame] | 4099 | |
Vishesh | 1c320df | 2021-03-01 09:46:30 +0530 | [diff] [blame] | 4100 | if (tiles->large_scale) |
| 4101 | return pack_large_scale_tiles_in_tg_obus(cpi, dst, saved_wb, |
| 4102 | largest_tile_id); |
| 4103 | |
| 4104 | return pack_tiles_in_tg_obus(cpi, dst, saved_wb, obu_extension_header, |
| 4105 | fh_info, largest_tile_id); |
| 4106 | } |
| 4107 | |
Daniel Max Valenzuela | 62eab67 | 2020-01-08 12:12:47 -0800 | [diff] [blame] | 4108 | static size_t av1_write_metadata_obu(const aom_metadata_t *metadata, |
| 4109 | uint8_t *const dst) { |
Daniel Max Valenzuela | 0a6c808 | 2019-11-04 10:52:56 -0800 | [diff] [blame] | 4110 | size_t coded_metadata_size = 0; |
| 4111 | const uint64_t metadata_type = (uint64_t)metadata->type; |
| 4112 | if (aom_uleb_encode(metadata_type, sizeof(metadata_type), dst, |
| 4113 | &coded_metadata_size) != 0) { |
| 4114 | return 0; |
| 4115 | } |
| 4116 | memcpy(dst + coded_metadata_size, metadata->payload, metadata->sz); |
| 4117 | // Add trailing bits. |
| 4118 | dst[coded_metadata_size + metadata->sz] = 0x80; |
| 4119 | return (uint32_t)(coded_metadata_size + metadata->sz + 1); |
| 4120 | } |
| 4121 | |
Tom Finegan | da8da7f | 2019-11-14 13:02:55 -0800 | [diff] [blame] | 4122 | static size_t av1_write_metadata_array(AV1_COMP *const cpi, uint8_t *dst) { |
Daniel Max Valenzuela | 0a6c808 | 2019-11-04 10:52:56 -0800 | [diff] [blame] | 4123 | if (!cpi->source) return 0; |
Daniel Max Valenzuela | 62eab67 | 2020-01-08 12:12:47 -0800 | [diff] [blame] | 4124 | AV1_COMMON *const cm = &cpi->common; |
Daniel Max Valenzuela | 0a6c808 | 2019-11-04 10:52:56 -0800 | [diff] [blame] | 4125 | aom_metadata_array_t *arr = cpi->source->metadata; |
| 4126 | if (!arr) return 0; |
Tom Finegan | da8da7f | 2019-11-14 13:02:55 -0800 | [diff] [blame] | 4127 | size_t obu_header_size = 0; |
| 4128 | size_t obu_payload_size = 0; |
| 4129 | size_t total_bytes_written = 0; |
Daniel Max Valenzuela | 0a6c808 | 2019-11-04 10:52:56 -0800 | [diff] [blame] | 4130 | size_t length_field_size = 0; |
| 4131 | for (size_t i = 0; i < arr->sz; i++) { |
| 4132 | aom_metadata_t *current_metadata = arr->metadata_array[i]; |
| 4133 | if (current_metadata && current_metadata->payload) { |
Daniel Max Valenzuela | 62eab67 | 2020-01-08 12:12:47 -0800 | [diff] [blame] | 4134 | if ((cm->current_frame.frame_type == KEY_FRAME && |
| 4135 | current_metadata->insert_flag == AOM_MIF_KEY_FRAME) || |
| 4136 | (cm->current_frame.frame_type != KEY_FRAME && |
| 4137 | current_metadata->insert_flag == AOM_MIF_NON_KEY_FRAME) || |
| 4138 | current_metadata->insert_flag == AOM_MIF_ANY_FRAME) { |
Tarundeep Singh | 3b3e9e1 | 2021-03-16 14:58:27 +0530 | [diff] [blame] | 4139 | obu_header_size = av1_write_obu_header(&cpi->ppi->level_params, |
| 4140 | &cpi->frame_header_count, |
| 4141 | OBU_METADATA, 0, dst); |
Daniel Max Valenzuela | 62eab67 | 2020-01-08 12:12:47 -0800 | [diff] [blame] | 4142 | obu_payload_size = |
| 4143 | av1_write_metadata_obu(current_metadata, dst + obu_header_size); |
Vishesh | 5849e6d | 2021-04-23 19:15:29 +0530 | [diff] [blame] | 4144 | length_field_size = |
| 4145 | av1_obu_memmove(obu_header_size, obu_payload_size, dst); |
Daniel Max Valenzuela | 62eab67 | 2020-01-08 12:12:47 -0800 | [diff] [blame] | 4146 | if (av1_write_uleb_obu_size(obu_header_size, obu_payload_size, dst) == |
| 4147 | AOM_CODEC_OK) { |
| 4148 | const size_t obu_size = obu_header_size + obu_payload_size; |
| 4149 | dst += obu_size + length_field_size; |
| 4150 | total_bytes_written += obu_size + length_field_size; |
| 4151 | } else { |
Tarundeep Singh | cfa5fb4 | 2021-04-23 18:25:32 +0530 | [diff] [blame] | 4152 | aom_internal_error(cpi->common.error, AOM_CODEC_ERROR, |
Daniel Max Valenzuela | 62eab67 | 2020-01-08 12:12:47 -0800 | [diff] [blame] | 4153 | "Error writing metadata OBU size"); |
| 4154 | } |
Daniel Max Valenzuela | 0a6c808 | 2019-11-04 10:52:56 -0800 | [diff] [blame] | 4155 | } |
| 4156 | } |
| 4157 | } |
| 4158 | return total_bytes_written; |
| 4159 | } |
| 4160 | |
David Turner | 35cba13 | 2018-12-10 15:48:15 +0000 | [diff] [blame] | 4161 | int av1_pack_bitstream(AV1_COMP *const cpi, uint8_t *dst, size_t *size, |
| 4162 | int *const largest_tile_id) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4163 | uint8_t *data = dst; |
Yunqing Wang | eeb08a9 | 2017-07-07 21:25:18 -0700 | [diff] [blame] | 4164 | uint32_t data_size; |
Soo-Chul Han | f858986 | 2018-01-24 03:13:14 +0000 | [diff] [blame] | 4165 | AV1_COMMON *const cm = &cpi->common; |
Tarundeep Singh | 3b3e9e1 | 2021-03-16 14:58:27 +0530 | [diff] [blame] | 4166 | AV1LevelParams *const level_params = &cpi->ppi->level_params; |
Vignesh Venkatasubramanian | ea0257d | 2018-02-28 14:43:34 -0800 | [diff] [blame] | 4167 | uint32_t obu_header_size = 0; |
| 4168 | uint32_t obu_payload_size = 0; |
Tom Finegan | f927381 | 2018-03-14 09:49:45 -0700 | [diff] [blame] | 4169 | FrameHeaderInfo fh_info = { NULL, 0, 0 }; |
Soo-Chul Han | f858986 | 2018-01-24 03:13:14 +0000 | [diff] [blame] | 4170 | const uint8_t obu_extension_header = |
Soo-Chul Han | d2f317c | 2018-05-08 14:21:24 -0400 | [diff] [blame] | 4171 | cm->temporal_layer_id << 5 | cm->spatial_layer_id << 3 | 0; |
Soo-Chul Han | f858986 | 2018-01-24 03:13:14 +0000 | [diff] [blame] | 4172 | |
Debargha Mukherjee | 472df41 | 2019-04-29 15:00:02 -0700 | [diff] [blame] | 4173 | // If no non-zero delta_q has been used, reset delta_q_present_flag |
Yue Chen | c87d749 | 2019-05-30 17:22:49 -0700 | [diff] [blame] | 4174 | if (cm->delta_q_info.delta_q_present_flag && cpi->deltaq_used == 0) { |
Debargha Mukherjee | 472df41 | 2019-04-29 15:00:02 -0700 | [diff] [blame] | 4175 | cm->delta_q_info.delta_q_present_flag = 0; |
| 4176 | } |
| 4177 | |
Angie Chiang | b11aedf | 2017-03-10 17:31:46 -0800 | [diff] [blame] | 4178 | #if CONFIG_BITSTREAM_DEBUG |
| 4179 | bitstream_queue_reset_write(); |
| 4180 | #endif |
| 4181 | |
Tarundeep Singh | d37807f | 2021-03-31 17:30:55 +0530 | [diff] [blame] | 4182 | cpi->frame_header_count = 0; |
Hui Su | 4fd1176 | 2019-03-26 16:05:07 -0700 | [diff] [blame] | 4183 | |
Soo-Chul Han | 38427e8 | 2017-09-27 15:06:13 -0400 | [diff] [blame] | 4184 | // The TD is now written outside the frame encode loop |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 4185 | |
Marco Paniconi | dfcdc7b | 2022-02-11 08:42:58 -0800 | [diff] [blame] | 4186 | // write sequence header obu at each key frame or intra_only frame, |
| 4187 | // preceded by 4-byte size |
| 4188 | if (cm->current_frame.frame_type == INTRA_ONLY_FRAME || |
Jingning Han | 2b922a5 | 2022-11-15 14:36:29 -0800 | [diff] [blame] | 4189 | cm->current_frame.frame_type == KEY_FRAME) { |
Tarundeep Singh | d37807f | 2021-03-31 17:30:55 +0530 | [diff] [blame] | 4190 | obu_header_size = av1_write_obu_header( |
| 4191 | level_params, &cpi->frame_header_count, OBU_SEQUENCE_HEADER, 0, data); |
Yaowu Xu | 797674b | 2019-05-01 17:38:11 -0700 | [diff] [blame] | 4192 | obu_payload_size = |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 4193 | av1_write_sequence_header_obu(cm->seq_params, data + obu_header_size); |
Vignesh Venkatasubramanian | ea0257d | 2018-02-28 14:43:34 -0800 | [diff] [blame] | 4194 | const size_t length_field_size = |
Vishesh | 5849e6d | 2021-04-23 19:15:29 +0530 | [diff] [blame] | 4195 | av1_obu_memmove(obu_header_size, obu_payload_size, data); |
Yaowu Xu | 797674b | 2019-05-01 17:38:11 -0700 | [diff] [blame] | 4196 | if (av1_write_uleb_obu_size(obu_header_size, obu_payload_size, data) != |
Tom Finegan | 9d9ec1f | 2018-03-14 17:55:18 -0700 | [diff] [blame] | 4197 | AOM_CODEC_OK) { |
Tom Finegan | 41150ad | 2018-01-23 11:42:55 -0800 | [diff] [blame] | 4198 | return AOM_CODEC_ERROR; |
Tom Finegan | 9d9ec1f | 2018-03-14 17:55:18 -0700 | [diff] [blame] | 4199 | } |
Tom Finegan | 41150ad | 2018-01-23 11:42:55 -0800 | [diff] [blame] | 4200 | |
Vignesh Venkatasubramanian | ea0257d | 2018-02-28 14:43:34 -0800 | [diff] [blame] | 4201 | data += obu_header_size + obu_payload_size + length_field_size; |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 4202 | } |
| 4203 | |
Daniel Max Valenzuela | 0a6c808 | 2019-11-04 10:52:56 -0800 | [diff] [blame] | 4204 | // write metadata obus before the frame obu that has the show_frame flag set |
| 4205 | if (cm->show_frame) data += av1_write_metadata_array(cpi, data); |
| 4206 | |
Sarah Parker | 3300552 | 2018-07-27 14:46:25 -0700 | [diff] [blame] | 4207 | const int write_frame_header = |
Urvang Joshi | 1de67aa | 2020-03-20 11:21:57 -0700 | [diff] [blame] | 4208 | (cpi->num_tg > 1 || encode_show_existing_frame(cm)); |
Jayasanker J | 4d1dfa3 | 2020-07-10 21:25:24 +0530 | [diff] [blame] | 4209 | struct aom_write_bit_buffer saved_wb = { NULL, 0 }; |
Wan-Teh Chang | 6e9ea9d | 2022-04-08 18:18:09 +0000 | [diff] [blame] | 4210 | size_t length_field = 0; |
Vignesh Venkatasubramanian | b2ce34e | 2018-03-05 16:57:40 -0800 | [diff] [blame] | 4211 | if (write_frame_header) { |
| 4212 | // Write Frame Header OBU. |
| 4213 | fh_info.frame_header = data; |
Tarundeep Singh | d37807f | 2021-03-31 17:30:55 +0530 | [diff] [blame] | 4214 | obu_header_size = |
| 4215 | av1_write_obu_header(level_params, &cpi->frame_header_count, |
| 4216 | OBU_FRAME_HEADER, obu_extension_header, data); |
Vishesh | 8bd59d9 | 2021-04-27 11:34:17 +0530 | [diff] [blame] | 4217 | obu_payload_size = write_frame_header_obu(cpi, &cpi->td.mb.e_mbd, &saved_wb, |
Cherma Rajan A | 8ba4824 | 2021-04-12 18:13:01 +0530 | [diff] [blame] | 4218 | data + obu_header_size, 1); |
Tom Finegan | 41150ad | 2018-01-23 11:42:55 -0800 | [diff] [blame] | 4219 | |
Wan-Teh Chang | 6e9ea9d | 2022-04-08 18:18:09 +0000 | [diff] [blame] | 4220 | length_field = av1_obu_memmove(obu_header_size, obu_payload_size, data); |
Yaowu Xu | 797674b | 2019-05-01 17:38:11 -0700 | [diff] [blame] | 4221 | if (av1_write_uleb_obu_size(obu_header_size, obu_payload_size, data) != |
Vignesh Venkatasubramanian | b2ce34e | 2018-03-05 16:57:40 -0800 | [diff] [blame] | 4222 | AOM_CODEC_OK) { |
| 4223 | return AOM_CODEC_ERROR; |
| 4224 | } |
Tom Finegan | 41150ad | 2018-01-23 11:42:55 -0800 | [diff] [blame] | 4225 | |
Tom Finegan | f927381 | 2018-03-14 09:49:45 -0700 | [diff] [blame] | 4226 | fh_info.obu_header_byte_offset = 0; |
Jayasanker J | 4d1dfa3 | 2020-07-10 21:25:24 +0530 | [diff] [blame] | 4227 | fh_info.total_length = obu_header_size + obu_payload_size + length_field; |
Vignesh Venkatasubramanian | b2ce34e | 2018-03-05 16:57:40 -0800 | [diff] [blame] | 4228 | data += fh_info.total_length; |
Vignesh Venkatasubramanian | b2ce34e | 2018-03-05 16:57:40 -0800 | [diff] [blame] | 4229 | } |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 4230 | |
Sarah Parker | 3300552 | 2018-07-27 14:46:25 -0700 | [diff] [blame] | 4231 | if (encode_show_existing_frame(cm)) { |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 4232 | data_size = 0; |
| 4233 | } else { |
Wan-Teh Chang | 6e9ea9d | 2022-04-08 18:18:09 +0000 | [diff] [blame] | 4234 | // Since length_field is determined adaptively after frame header |
| 4235 | // encoding, saved_wb must be adjusted accordingly. |
| 4236 | if (saved_wb.bit_buffer != NULL) { |
| 4237 | saved_wb.bit_buffer += length_field; |
| 4238 | } |
| 4239 | |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 4240 | // Each tile group obu will be preceded by 4-byte size of the tile group |
| 4241 | // obu |
David Turner | 35cba13 | 2018-12-10 15:48:15 +0000 | [diff] [blame] | 4242 | data_size = write_tiles_in_tg_obus( |
| 4243 | cpi, data, &saved_wb, obu_extension_header, &fh_info, largest_tile_id); |
Soo-Chul Han | 65c00ae1 | 2017-09-07 13:12:35 -0400 | [diff] [blame] | 4244 | } |
Yaowu Xu | d29ea97 | 2018-02-22 09:50:58 -0800 | [diff] [blame] | 4245 | data += data_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4246 | *size = data - dst; |
Tom Finegan | e4099e3 | 2018-01-23 12:01:51 -0800 | [diff] [blame] | 4247 | return AOM_CODEC_OK; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4248 | } |