1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker *
4*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker */
11*77c1e3ccSAndroid Build Coastguard Worker
12*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
13*77c1e3ccSAndroid Build Coastguard Worker #include <limits.h>
14*77c1e3ccSAndroid Build Coastguard Worker #include <stdbool.h>
15*77c1e3ccSAndroid Build Coastguard Worker #include <stdint.h>
16*77c1e3ccSAndroid Build Coastguard Worker #include <stdio.h>
17*77c1e3ccSAndroid Build Coastguard Worker #include <string.h>
18*77c1e3ccSAndroid Build Coastguard Worker
19*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_encoder.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/aom_dsp_common.h"
21*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/binary_codes_writer.h"
22*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/bitwriter_buffer.h"
23*77c1e3ccSAndroid Build Coastguard Worker #include "aom_mem/aom_mem.h"
24*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/bitops.h"
25*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/mem_ops.h"
26*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITSTREAM_DEBUG
27*77c1e3ccSAndroid Build Coastguard Worker #include "aom_util/debug_util.h"
28*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_BITSTREAM_DEBUG
29*77c1e3ccSAndroid Build Coastguard Worker
30*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/cdef.h"
31*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/cfl.h"
32*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/debugmodes.h"
33*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/entropy.h"
34*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/entropymode.h"
35*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/entropymv.h"
36*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/mvref_common.h"
37*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/pred_common.h"
38*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/reconinter.h"
39*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/reconintra.h"
40*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/seg_common.h"
41*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/tile_common.h"
42*77c1e3ccSAndroid Build Coastguard Worker
43*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/bitstream.h"
44*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/cost.h"
45*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodemv.h"
46*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodetxb.h"
47*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/ethread.h"
48*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/mcomp.h"
49*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/palette.h"
50*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/pickrst.h"
51*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/segmentation.h"
52*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/tokenize.h"
53*77c1e3ccSAndroid Build Coastguard Worker
54*77c1e3ccSAndroid Build Coastguard Worker #define ENC_MISMATCH_DEBUG 0
55*77c1e3ccSAndroid Build Coastguard Worker #define SETUP_TIME_OH_CONST 5 // Setup time overhead constant per worker
56*77c1e3ccSAndroid Build Coastguard Worker #define JOB_DISP_TIME_OH_CONST 1 // Job dispatch time overhead per tile
57*77c1e3ccSAndroid Build Coastguard Worker
write_uniform(aom_writer * w,int n,int v)58*77c1e3ccSAndroid Build Coastguard Worker static inline void write_uniform(aom_writer *w, int n, int v) {
59*77c1e3ccSAndroid Build Coastguard Worker const int l = get_unsigned_bits(n);
60*77c1e3ccSAndroid Build Coastguard Worker const int m = (1 << l) - n;
61*77c1e3ccSAndroid Build Coastguard Worker if (l == 0) return;
62*77c1e3ccSAndroid Build Coastguard Worker if (v < m) {
63*77c1e3ccSAndroid Build Coastguard Worker aom_write_literal(w, v, l - 1);
64*77c1e3ccSAndroid Build Coastguard Worker } else {
65*77c1e3ccSAndroid Build Coastguard Worker aom_write_literal(w, m + ((v - m) >> 1), l - 1);
66*77c1e3ccSAndroid Build Coastguard Worker aom_write_literal(w, (v - m) & 1, 1);
67*77c1e3ccSAndroid Build Coastguard Worker }
68*77c1e3ccSAndroid Build Coastguard Worker }
69*77c1e3ccSAndroid Build Coastguard Worker
70*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
71*77c1e3ccSAndroid Build Coastguard Worker static inline void loop_restoration_write_sb_coeffs(
72*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm, MACROBLOCKD *xd, int runit_idx,
73*77c1e3ccSAndroid Build Coastguard Worker aom_writer *const w, int plane, FRAME_COUNTS *counts);
74*77c1e3ccSAndroid Build Coastguard Worker #endif
75*77c1e3ccSAndroid Build Coastguard Worker
write_intra_y_mode_kf(FRAME_CONTEXT * frame_ctx,const MB_MODE_INFO * mi,const MB_MODE_INFO * above_mi,const MB_MODE_INFO * left_mi,PREDICTION_MODE mode,aom_writer * w)76*77c1e3ccSAndroid Build Coastguard Worker static inline void write_intra_y_mode_kf(FRAME_CONTEXT *frame_ctx,
77*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *mi,
78*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *above_mi,
79*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *left_mi,
80*77c1e3ccSAndroid Build Coastguard Worker PREDICTION_MODE mode, aom_writer *w) {
81*77c1e3ccSAndroid Build Coastguard Worker assert(!is_intrabc_block(mi));
82*77c1e3ccSAndroid Build Coastguard Worker (void)mi;
83*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, mode, get_y_mode_cdf(frame_ctx, above_mi, left_mi),
84*77c1e3ccSAndroid Build Coastguard Worker INTRA_MODES);
85*77c1e3ccSAndroid Build Coastguard Worker }
86*77c1e3ccSAndroid Build Coastguard Worker
write_inter_mode(aom_writer * w,PREDICTION_MODE mode,FRAME_CONTEXT * ec_ctx,const int16_t mode_ctx)87*77c1e3ccSAndroid Build Coastguard Worker static inline void write_inter_mode(aom_writer *w, PREDICTION_MODE mode,
88*77c1e3ccSAndroid Build Coastguard Worker FRAME_CONTEXT *ec_ctx,
89*77c1e3ccSAndroid Build Coastguard Worker const int16_t mode_ctx) {
90*77c1e3ccSAndroid Build Coastguard Worker const int16_t newmv_ctx = mode_ctx & NEWMV_CTX_MASK;
91*77c1e3ccSAndroid Build Coastguard Worker
92*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, mode != NEWMV, ec_ctx->newmv_cdf[newmv_ctx], 2);
93*77c1e3ccSAndroid Build Coastguard Worker
94*77c1e3ccSAndroid Build Coastguard Worker if (mode != NEWMV) {
95*77c1e3ccSAndroid Build Coastguard Worker const int16_t zeromv_ctx =
96*77c1e3ccSAndroid Build Coastguard Worker (mode_ctx >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
97*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, mode != GLOBALMV, ec_ctx->zeromv_cdf[zeromv_ctx], 2);
98*77c1e3ccSAndroid Build Coastguard Worker
99*77c1e3ccSAndroid Build Coastguard Worker if (mode != GLOBALMV) {
100*77c1e3ccSAndroid Build Coastguard Worker int16_t refmv_ctx = (mode_ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
101*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, mode != NEARESTMV, ec_ctx->refmv_cdf[refmv_ctx], 2);
102*77c1e3ccSAndroid Build Coastguard Worker }
103*77c1e3ccSAndroid Build Coastguard Worker }
104*77c1e3ccSAndroid Build Coastguard Worker }
105*77c1e3ccSAndroid Build Coastguard Worker
write_drl_idx(FRAME_CONTEXT * ec_ctx,const MB_MODE_INFO * mbmi,const MB_MODE_INFO_EXT_FRAME * mbmi_ext_frame,aom_writer * w)106*77c1e3ccSAndroid Build Coastguard Worker static inline void write_drl_idx(FRAME_CONTEXT *ec_ctx,
107*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *mbmi,
108*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO_EXT_FRAME *mbmi_ext_frame,
109*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w) {
110*77c1e3ccSAndroid Build Coastguard Worker assert(mbmi->ref_mv_idx < 3);
111*77c1e3ccSAndroid Build Coastguard Worker
112*77c1e3ccSAndroid Build Coastguard Worker const int new_mv = mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV;
113*77c1e3ccSAndroid Build Coastguard Worker if (new_mv) {
114*77c1e3ccSAndroid Build Coastguard Worker int idx;
115*77c1e3ccSAndroid Build Coastguard Worker for (idx = 0; idx < 2; ++idx) {
116*77c1e3ccSAndroid Build Coastguard Worker if (mbmi_ext_frame->ref_mv_count > idx + 1) {
117*77c1e3ccSAndroid Build Coastguard Worker uint8_t drl_ctx = av1_drl_ctx(mbmi_ext_frame->weight, idx);
118*77c1e3ccSAndroid Build Coastguard Worker
119*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, mbmi->ref_mv_idx != idx, ec_ctx->drl_cdf[drl_ctx],
120*77c1e3ccSAndroid Build Coastguard Worker 2);
121*77c1e3ccSAndroid Build Coastguard Worker if (mbmi->ref_mv_idx == idx) return;
122*77c1e3ccSAndroid Build Coastguard Worker }
123*77c1e3ccSAndroid Build Coastguard Worker }
124*77c1e3ccSAndroid Build Coastguard Worker return;
125*77c1e3ccSAndroid Build Coastguard Worker }
126*77c1e3ccSAndroid Build Coastguard Worker
127*77c1e3ccSAndroid Build Coastguard Worker if (have_nearmv_in_inter_mode(mbmi->mode)) {
128*77c1e3ccSAndroid Build Coastguard Worker int idx;
129*77c1e3ccSAndroid Build Coastguard Worker // TODO(jingning): Temporary solution to compensate the NEARESTMV offset.
130*77c1e3ccSAndroid Build Coastguard Worker for (idx = 1; idx < 3; ++idx) {
131*77c1e3ccSAndroid Build Coastguard Worker if (mbmi_ext_frame->ref_mv_count > idx + 1) {
132*77c1e3ccSAndroid Build Coastguard Worker uint8_t drl_ctx = av1_drl_ctx(mbmi_ext_frame->weight, idx);
133*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, mbmi->ref_mv_idx != (idx - 1),
134*77c1e3ccSAndroid Build Coastguard Worker ec_ctx->drl_cdf[drl_ctx], 2);
135*77c1e3ccSAndroid Build Coastguard Worker if (mbmi->ref_mv_idx == (idx - 1)) return;
136*77c1e3ccSAndroid Build Coastguard Worker }
137*77c1e3ccSAndroid Build Coastguard Worker }
138*77c1e3ccSAndroid Build Coastguard Worker return;
139*77c1e3ccSAndroid Build Coastguard Worker }
140*77c1e3ccSAndroid Build Coastguard Worker }
141*77c1e3ccSAndroid Build Coastguard Worker
write_inter_compound_mode(MACROBLOCKD * xd,aom_writer * w,PREDICTION_MODE mode,const int16_t mode_ctx)142*77c1e3ccSAndroid Build Coastguard Worker static inline void write_inter_compound_mode(MACROBLOCKD *xd, aom_writer *w,
143*77c1e3ccSAndroid Build Coastguard Worker PREDICTION_MODE mode,
144*77c1e3ccSAndroid Build Coastguard Worker const int16_t mode_ctx) {
145*77c1e3ccSAndroid Build Coastguard Worker assert(is_inter_compound_mode(mode));
146*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, INTER_COMPOUND_OFFSET(mode),
147*77c1e3ccSAndroid Build Coastguard Worker xd->tile_ctx->inter_compound_mode_cdf[mode_ctx],
148*77c1e3ccSAndroid Build Coastguard Worker INTER_COMPOUND_MODES);
149*77c1e3ccSAndroid Build Coastguard Worker }
150*77c1e3ccSAndroid Build Coastguard Worker
write_tx_size_vartx(MACROBLOCKD * xd,const MB_MODE_INFO * mbmi,TX_SIZE tx_size,int depth,int blk_row,int blk_col,aom_writer * w)151*77c1e3ccSAndroid Build Coastguard Worker static inline void write_tx_size_vartx(MACROBLOCKD *xd,
152*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *mbmi,
153*77c1e3ccSAndroid Build Coastguard Worker TX_SIZE tx_size, int depth, int blk_row,
154*77c1e3ccSAndroid Build Coastguard Worker int blk_col, aom_writer *w) {
155*77c1e3ccSAndroid Build Coastguard Worker FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
156*77c1e3ccSAndroid Build Coastguard Worker const int max_blocks_high = max_block_high(xd, mbmi->bsize, 0);
157*77c1e3ccSAndroid Build Coastguard Worker const int max_blocks_wide = max_block_wide(xd, mbmi->bsize, 0);
158*77c1e3ccSAndroid Build Coastguard Worker
159*77c1e3ccSAndroid Build Coastguard Worker if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
160*77c1e3ccSAndroid Build Coastguard Worker
161*77c1e3ccSAndroid Build Coastguard Worker if (depth == MAX_VARTX_DEPTH) {
162*77c1e3ccSAndroid Build Coastguard Worker txfm_partition_update(xd->above_txfm_context + blk_col,
163*77c1e3ccSAndroid Build Coastguard Worker xd->left_txfm_context + blk_row, tx_size, tx_size);
164*77c1e3ccSAndroid Build Coastguard Worker return;
165*77c1e3ccSAndroid Build Coastguard Worker }
166*77c1e3ccSAndroid Build Coastguard Worker
167*77c1e3ccSAndroid Build Coastguard Worker const int ctx = txfm_partition_context(xd->above_txfm_context + blk_col,
168*77c1e3ccSAndroid Build Coastguard Worker xd->left_txfm_context + blk_row,
169*77c1e3ccSAndroid Build Coastguard Worker mbmi->bsize, tx_size);
170*77c1e3ccSAndroid Build Coastguard Worker const int txb_size_index =
171*77c1e3ccSAndroid Build Coastguard Worker av1_get_txb_size_index(mbmi->bsize, blk_row, blk_col);
172*77c1e3ccSAndroid Build Coastguard Worker const int write_txfm_partition =
173*77c1e3ccSAndroid Build Coastguard Worker tx_size == mbmi->inter_tx_size[txb_size_index];
174*77c1e3ccSAndroid Build Coastguard Worker if (write_txfm_partition) {
175*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, 0, ec_ctx->txfm_partition_cdf[ctx], 2);
176*77c1e3ccSAndroid Build Coastguard Worker
177*77c1e3ccSAndroid Build Coastguard Worker txfm_partition_update(xd->above_txfm_context + blk_col,
178*77c1e3ccSAndroid Build Coastguard Worker xd->left_txfm_context + blk_row, tx_size, tx_size);
179*77c1e3ccSAndroid Build Coastguard Worker // TODO(yuec): set correct txfm partition update for qttx
180*77c1e3ccSAndroid Build Coastguard Worker } else {
181*77c1e3ccSAndroid Build Coastguard Worker const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
182*77c1e3ccSAndroid Build Coastguard Worker const int bsw = tx_size_wide_unit[sub_txs];
183*77c1e3ccSAndroid Build Coastguard Worker const int bsh = tx_size_high_unit[sub_txs];
184*77c1e3ccSAndroid Build Coastguard Worker
185*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, 1, ec_ctx->txfm_partition_cdf[ctx], 2);
186*77c1e3ccSAndroid Build Coastguard Worker
187*77c1e3ccSAndroid Build Coastguard Worker if (sub_txs == TX_4X4) {
188*77c1e3ccSAndroid Build Coastguard Worker txfm_partition_update(xd->above_txfm_context + blk_col,
189*77c1e3ccSAndroid Build Coastguard Worker xd->left_txfm_context + blk_row, sub_txs, tx_size);
190*77c1e3ccSAndroid Build Coastguard Worker return;
191*77c1e3ccSAndroid Build Coastguard Worker }
192*77c1e3ccSAndroid Build Coastguard Worker
193*77c1e3ccSAndroid Build Coastguard Worker assert(bsw > 0 && bsh > 0);
194*77c1e3ccSAndroid Build Coastguard Worker for (int row = 0; row < tx_size_high_unit[tx_size]; row += bsh) {
195*77c1e3ccSAndroid Build Coastguard Worker const int offsetr = blk_row + row;
196*77c1e3ccSAndroid Build Coastguard Worker for (int col = 0; col < tx_size_wide_unit[tx_size]; col += bsw) {
197*77c1e3ccSAndroid Build Coastguard Worker const int offsetc = blk_col + col;
198*77c1e3ccSAndroid Build Coastguard Worker write_tx_size_vartx(xd, mbmi, sub_txs, depth + 1, offsetr, offsetc, w);
199*77c1e3ccSAndroid Build Coastguard Worker }
200*77c1e3ccSAndroid Build Coastguard Worker }
201*77c1e3ccSAndroid Build Coastguard Worker }
202*77c1e3ccSAndroid Build Coastguard Worker }
203*77c1e3ccSAndroid Build Coastguard Worker
write_selected_tx_size(const MACROBLOCKD * xd,aom_writer * w)204*77c1e3ccSAndroid Build Coastguard Worker static inline void write_selected_tx_size(const MACROBLOCKD *xd,
205*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w) {
206*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *const mbmi = xd->mi[0];
207*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE bsize = mbmi->bsize;
208*77c1e3ccSAndroid Build Coastguard Worker FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
209*77c1e3ccSAndroid Build Coastguard Worker if (block_signals_txsize(bsize)) {
210*77c1e3ccSAndroid Build Coastguard Worker const TX_SIZE tx_size = mbmi->tx_size;
211*77c1e3ccSAndroid Build Coastguard Worker const int tx_size_ctx = get_tx_size_context(xd);
212*77c1e3ccSAndroid Build Coastguard Worker const int depth = tx_size_to_depth(tx_size, bsize);
213*77c1e3ccSAndroid Build Coastguard Worker const int max_depths = bsize_to_max_depth(bsize);
214*77c1e3ccSAndroid Build Coastguard Worker const int32_t tx_size_cat = bsize_to_tx_size_cat(bsize);
215*77c1e3ccSAndroid Build Coastguard Worker
216*77c1e3ccSAndroid Build Coastguard Worker assert(depth >= 0 && depth <= max_depths);
217*77c1e3ccSAndroid Build Coastguard Worker assert(!is_inter_block(mbmi));
218*77c1e3ccSAndroid Build Coastguard Worker assert(IMPLIES(is_rect_tx(tx_size), is_rect_tx_allowed(xd, mbmi)));
219*77c1e3ccSAndroid Build Coastguard Worker
220*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, depth, ec_ctx->tx_size_cdf[tx_size_cat][tx_size_ctx],
221*77c1e3ccSAndroid Build Coastguard Worker max_depths + 1);
222*77c1e3ccSAndroid Build Coastguard Worker }
223*77c1e3ccSAndroid Build Coastguard Worker }
224*77c1e3ccSAndroid Build Coastguard Worker
write_skip(const AV1_COMMON * cm,const MACROBLOCKD * xd,uint8_t segment_id,const MB_MODE_INFO * mi,aom_writer * w)225*77c1e3ccSAndroid Build Coastguard Worker static int write_skip(const AV1_COMMON *cm, const MACROBLOCKD *xd,
226*77c1e3ccSAndroid Build Coastguard Worker uint8_t segment_id, const MB_MODE_INFO *mi,
227*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w) {
228*77c1e3ccSAndroid Build Coastguard Worker if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
229*77c1e3ccSAndroid Build Coastguard Worker return 1;
230*77c1e3ccSAndroid Build Coastguard Worker } else {
231*77c1e3ccSAndroid Build Coastguard Worker const int skip_txfm = mi->skip_txfm;
232*77c1e3ccSAndroid Build Coastguard Worker const int ctx = av1_get_skip_txfm_context(xd);
233*77c1e3ccSAndroid Build Coastguard Worker FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
234*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, skip_txfm, ec_ctx->skip_txfm_cdfs[ctx], 2);
235*77c1e3ccSAndroid Build Coastguard Worker return skip_txfm;
236*77c1e3ccSAndroid Build Coastguard Worker }
237*77c1e3ccSAndroid Build Coastguard Worker }
238*77c1e3ccSAndroid Build Coastguard Worker
write_skip_mode(const AV1_COMMON * cm,const MACROBLOCKD * xd,uint8_t segment_id,const MB_MODE_INFO * mi,aom_writer * w)239*77c1e3ccSAndroid Build Coastguard Worker static int write_skip_mode(const AV1_COMMON *cm, const MACROBLOCKD *xd,
240*77c1e3ccSAndroid Build Coastguard Worker uint8_t segment_id, const MB_MODE_INFO *mi,
241*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w) {
242*77c1e3ccSAndroid Build Coastguard Worker if (!cm->current_frame.skip_mode_info.skip_mode_flag) return 0;
243*77c1e3ccSAndroid Build Coastguard Worker if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
244*77c1e3ccSAndroid Build Coastguard Worker return 0;
245*77c1e3ccSAndroid Build Coastguard Worker }
246*77c1e3ccSAndroid Build Coastguard Worker const int skip_mode = mi->skip_mode;
247*77c1e3ccSAndroid Build Coastguard Worker if (!is_comp_ref_allowed(mi->bsize)) {
248*77c1e3ccSAndroid Build Coastguard Worker assert(!skip_mode);
249*77c1e3ccSAndroid Build Coastguard Worker return 0;
250*77c1e3ccSAndroid Build Coastguard Worker }
251*77c1e3ccSAndroid Build Coastguard Worker if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME) ||
252*77c1e3ccSAndroid Build Coastguard Worker segfeature_active(&cm->seg, segment_id, SEG_LVL_GLOBALMV)) {
253*77c1e3ccSAndroid Build Coastguard Worker // These features imply single-reference mode, while skip mode implies
254*77c1e3ccSAndroid Build Coastguard Worker // compound reference. Hence, the two are mutually exclusive.
255*77c1e3ccSAndroid Build Coastguard Worker // In other words, skip_mode is implicitly 0 here.
256*77c1e3ccSAndroid Build Coastguard Worker assert(!skip_mode);
257*77c1e3ccSAndroid Build Coastguard Worker return 0;
258*77c1e3ccSAndroid Build Coastguard Worker }
259*77c1e3ccSAndroid Build Coastguard Worker const int ctx = av1_get_skip_mode_context(xd);
260*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, skip_mode, xd->tile_ctx->skip_mode_cdfs[ctx], 2);
261*77c1e3ccSAndroid Build Coastguard Worker return skip_mode;
262*77c1e3ccSAndroid Build Coastguard Worker }
263*77c1e3ccSAndroid Build Coastguard Worker
write_is_inter(const AV1_COMMON * cm,const MACROBLOCKD * xd,uint8_t segment_id,aom_writer * w,const int is_inter)264*77c1e3ccSAndroid Build Coastguard Worker static inline void write_is_inter(const AV1_COMMON *cm, const MACROBLOCKD *xd,
265*77c1e3ccSAndroid Build Coastguard Worker uint8_t segment_id, aom_writer *w,
266*77c1e3ccSAndroid Build Coastguard Worker const int is_inter) {
267*77c1e3ccSAndroid Build Coastguard Worker if (!segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
268*77c1e3ccSAndroid Build Coastguard Worker if (segfeature_active(&cm->seg, segment_id, SEG_LVL_GLOBALMV)) {
269*77c1e3ccSAndroid Build Coastguard Worker assert(is_inter);
270*77c1e3ccSAndroid Build Coastguard Worker return;
271*77c1e3ccSAndroid Build Coastguard Worker }
272*77c1e3ccSAndroid Build Coastguard Worker const int ctx = av1_get_intra_inter_context(xd);
273*77c1e3ccSAndroid Build Coastguard Worker FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
274*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, is_inter, ec_ctx->intra_inter_cdf[ctx], 2);
275*77c1e3ccSAndroid Build Coastguard Worker }
276*77c1e3ccSAndroid Build Coastguard Worker }
277*77c1e3ccSAndroid Build Coastguard Worker
write_motion_mode(const AV1_COMMON * cm,MACROBLOCKD * xd,const MB_MODE_INFO * mbmi,aom_writer * w)278*77c1e3ccSAndroid Build Coastguard Worker static inline void write_motion_mode(const AV1_COMMON *cm, MACROBLOCKD *xd,
279*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *mbmi, aom_writer *w) {
280*77c1e3ccSAndroid Build Coastguard Worker MOTION_MODE last_motion_mode_allowed =
281*77c1e3ccSAndroid Build Coastguard Worker cm->features.switchable_motion_mode
282*77c1e3ccSAndroid Build Coastguard Worker ? motion_mode_allowed(cm->global_motion, xd, mbmi,
283*77c1e3ccSAndroid Build Coastguard Worker cm->features.allow_warped_motion)
284*77c1e3ccSAndroid Build Coastguard Worker : SIMPLE_TRANSLATION;
285*77c1e3ccSAndroid Build Coastguard Worker assert(mbmi->motion_mode <= last_motion_mode_allowed);
286*77c1e3ccSAndroid Build Coastguard Worker switch (last_motion_mode_allowed) {
287*77c1e3ccSAndroid Build Coastguard Worker case SIMPLE_TRANSLATION: break;
288*77c1e3ccSAndroid Build Coastguard Worker case OBMC_CAUSAL:
289*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, mbmi->motion_mode == OBMC_CAUSAL,
290*77c1e3ccSAndroid Build Coastguard Worker xd->tile_ctx->obmc_cdf[mbmi->bsize], 2);
291*77c1e3ccSAndroid Build Coastguard Worker break;
292*77c1e3ccSAndroid Build Coastguard Worker default:
293*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, mbmi->motion_mode,
294*77c1e3ccSAndroid Build Coastguard Worker xd->tile_ctx->motion_mode_cdf[mbmi->bsize],
295*77c1e3ccSAndroid Build Coastguard Worker MOTION_MODES);
296*77c1e3ccSAndroid Build Coastguard Worker }
297*77c1e3ccSAndroid Build Coastguard Worker }
298*77c1e3ccSAndroid Build Coastguard Worker
write_delta_qindex(const MACROBLOCKD * xd,int delta_qindex,aom_writer * w)299*77c1e3ccSAndroid Build Coastguard Worker static inline void write_delta_qindex(const MACROBLOCKD *xd, int delta_qindex,
300*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w) {
301*77c1e3ccSAndroid Build Coastguard Worker int sign = delta_qindex < 0;
302*77c1e3ccSAndroid Build Coastguard Worker int abs = sign ? -delta_qindex : delta_qindex;
303*77c1e3ccSAndroid Build Coastguard Worker int rem_bits, thr;
304*77c1e3ccSAndroid Build Coastguard Worker int smallval = abs < DELTA_Q_SMALL ? 1 : 0;
305*77c1e3ccSAndroid Build Coastguard Worker FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
306*77c1e3ccSAndroid Build Coastguard Worker
307*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, AOMMIN(abs, DELTA_Q_SMALL), ec_ctx->delta_q_cdf,
308*77c1e3ccSAndroid Build Coastguard Worker DELTA_Q_PROBS + 1);
309*77c1e3ccSAndroid Build Coastguard Worker
310*77c1e3ccSAndroid Build Coastguard Worker if (!smallval) {
311*77c1e3ccSAndroid Build Coastguard Worker rem_bits = get_msb(abs - 1);
312*77c1e3ccSAndroid Build Coastguard Worker thr = (1 << rem_bits) + 1;
313*77c1e3ccSAndroid Build Coastguard Worker aom_write_literal(w, rem_bits - 1, 3);
314*77c1e3ccSAndroid Build Coastguard Worker aom_write_literal(w, abs - thr, rem_bits);
315*77c1e3ccSAndroid Build Coastguard Worker }
316*77c1e3ccSAndroid Build Coastguard Worker if (abs > 0) {
317*77c1e3ccSAndroid Build Coastguard Worker aom_write_bit(w, sign);
318*77c1e3ccSAndroid Build Coastguard Worker }
319*77c1e3ccSAndroid Build Coastguard Worker }
320*77c1e3ccSAndroid Build Coastguard Worker
write_delta_lflevel(const AV1_COMMON * cm,const MACROBLOCKD * xd,int lf_id,int delta_lflevel,int delta_lf_multi,aom_writer * w)321*77c1e3ccSAndroid Build Coastguard Worker static inline void write_delta_lflevel(const AV1_COMMON *cm,
322*77c1e3ccSAndroid Build Coastguard Worker const MACROBLOCKD *xd, int lf_id,
323*77c1e3ccSAndroid Build Coastguard Worker int delta_lflevel, int delta_lf_multi,
324*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w) {
325*77c1e3ccSAndroid Build Coastguard Worker int sign = delta_lflevel < 0;
326*77c1e3ccSAndroid Build Coastguard Worker int abs = sign ? -delta_lflevel : delta_lflevel;
327*77c1e3ccSAndroid Build Coastguard Worker int rem_bits, thr;
328*77c1e3ccSAndroid Build Coastguard Worker int smallval = abs < DELTA_LF_SMALL ? 1 : 0;
329*77c1e3ccSAndroid Build Coastguard Worker FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
330*77c1e3ccSAndroid Build Coastguard Worker (void)cm;
331*77c1e3ccSAndroid Build Coastguard Worker
332*77c1e3ccSAndroid Build Coastguard Worker if (delta_lf_multi) {
333*77c1e3ccSAndroid Build Coastguard Worker assert(lf_id >= 0 && lf_id < (av1_num_planes(cm) > 1 ? FRAME_LF_COUNT
334*77c1e3ccSAndroid Build Coastguard Worker : FRAME_LF_COUNT - 2));
335*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, AOMMIN(abs, DELTA_LF_SMALL),
336*77c1e3ccSAndroid Build Coastguard Worker ec_ctx->delta_lf_multi_cdf[lf_id], DELTA_LF_PROBS + 1);
337*77c1e3ccSAndroid Build Coastguard Worker } else {
338*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, AOMMIN(abs, DELTA_LF_SMALL), ec_ctx->delta_lf_cdf,
339*77c1e3ccSAndroid Build Coastguard Worker DELTA_LF_PROBS + 1);
340*77c1e3ccSAndroid Build Coastguard Worker }
341*77c1e3ccSAndroid Build Coastguard Worker
342*77c1e3ccSAndroid Build Coastguard Worker if (!smallval) {
343*77c1e3ccSAndroid Build Coastguard Worker rem_bits = get_msb(abs - 1);
344*77c1e3ccSAndroid Build Coastguard Worker thr = (1 << rem_bits) + 1;
345*77c1e3ccSAndroid Build Coastguard Worker aom_write_literal(w, rem_bits - 1, 3);
346*77c1e3ccSAndroid Build Coastguard Worker aom_write_literal(w, abs - thr, rem_bits);
347*77c1e3ccSAndroid Build Coastguard Worker }
348*77c1e3ccSAndroid Build Coastguard Worker if (abs > 0) {
349*77c1e3ccSAndroid Build Coastguard Worker aom_write_bit(w, sign);
350*77c1e3ccSAndroid Build Coastguard Worker }
351*77c1e3ccSAndroid Build Coastguard Worker }
352*77c1e3ccSAndroid Build Coastguard Worker
pack_map_tokens(aom_writer * w,const TokenExtra ** tp,int n,int num,MapCdf map_pb_cdf)353*77c1e3ccSAndroid Build Coastguard Worker static inline void pack_map_tokens(aom_writer *w, const TokenExtra **tp, int n,
354*77c1e3ccSAndroid Build Coastguard Worker int num, MapCdf map_pb_cdf) {
355*77c1e3ccSAndroid Build Coastguard Worker const TokenExtra *p = *tp;
356*77c1e3ccSAndroid Build Coastguard Worker const int palette_size_idx = n - PALETTE_MIN_SIZE;
357*77c1e3ccSAndroid Build Coastguard Worker write_uniform(w, n, p->token); // The first color index.
358*77c1e3ccSAndroid Build Coastguard Worker ++p;
359*77c1e3ccSAndroid Build Coastguard Worker --num;
360*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < num; ++i) {
361*77c1e3ccSAndroid Build Coastguard Worker assert((p->color_ctx >= 0) &&
362*77c1e3ccSAndroid Build Coastguard Worker (p->color_ctx < PALETTE_COLOR_INDEX_CONTEXTS));
363*77c1e3ccSAndroid Build Coastguard Worker aom_cdf_prob *color_map_cdf = map_pb_cdf[palette_size_idx][p->color_ctx];
364*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, p->token, color_map_cdf, n);
365*77c1e3ccSAndroid Build Coastguard Worker ++p;
366*77c1e3ccSAndroid Build Coastguard Worker }
367*77c1e3ccSAndroid Build Coastguard Worker *tp = p;
368*77c1e3ccSAndroid Build Coastguard Worker }
369*77c1e3ccSAndroid Build Coastguard Worker
pack_txb_tokens(aom_writer * w,AV1_COMMON * cm,MACROBLOCK * const x,const TokenExtra ** tp,const TokenExtra * const tok_end,MACROBLOCKD * xd,MB_MODE_INFO * mbmi,int plane,BLOCK_SIZE plane_bsize,aom_bit_depth_t bit_depth,int block,int blk_row,int blk_col,TX_SIZE tx_size,TOKEN_STATS * token_stats)370*77c1e3ccSAndroid Build Coastguard Worker static inline void pack_txb_tokens(
371*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w, AV1_COMMON *cm, MACROBLOCK *const x, const TokenExtra **tp,
372*77c1e3ccSAndroid Build Coastguard Worker const TokenExtra *const tok_end, MACROBLOCKD *xd, MB_MODE_INFO *mbmi,
373*77c1e3ccSAndroid Build Coastguard Worker int plane, BLOCK_SIZE plane_bsize, aom_bit_depth_t bit_depth, int block,
374*77c1e3ccSAndroid Build Coastguard Worker int blk_row, int blk_col, TX_SIZE tx_size, TOKEN_STATS *token_stats) {
375*77c1e3ccSAndroid Build Coastguard Worker const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
376*77c1e3ccSAndroid Build Coastguard Worker const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
377*77c1e3ccSAndroid Build Coastguard Worker
378*77c1e3ccSAndroid Build Coastguard Worker if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
379*77c1e3ccSAndroid Build Coastguard Worker
380*77c1e3ccSAndroid Build Coastguard Worker const struct macroblockd_plane *const pd = &xd->plane[plane];
381*77c1e3ccSAndroid Build Coastguard Worker const TX_SIZE plane_tx_size =
382*77c1e3ccSAndroid Build Coastguard Worker plane ? av1_get_max_uv_txsize(mbmi->bsize, pd->subsampling_x,
383*77c1e3ccSAndroid Build Coastguard Worker pd->subsampling_y)
384*77c1e3ccSAndroid Build Coastguard Worker : mbmi->inter_tx_size[av1_get_txb_size_index(plane_bsize, blk_row,
385*77c1e3ccSAndroid Build Coastguard Worker blk_col)];
386*77c1e3ccSAndroid Build Coastguard Worker
387*77c1e3ccSAndroid Build Coastguard Worker if (tx_size == plane_tx_size || plane) {
388*77c1e3ccSAndroid Build Coastguard Worker av1_write_coeffs_txb(cm, x, w, blk_row, blk_col, plane, block, tx_size);
389*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_RD_DEBUG
390*77c1e3ccSAndroid Build Coastguard Worker TOKEN_STATS tmp_token_stats;
391*77c1e3ccSAndroid Build Coastguard Worker init_token_stats(&tmp_token_stats);
392*77c1e3ccSAndroid Build Coastguard Worker token_stats->cost += tmp_token_stats.cost;
393*77c1e3ccSAndroid Build Coastguard Worker #endif
394*77c1e3ccSAndroid Build Coastguard Worker } else {
395*77c1e3ccSAndroid Build Coastguard Worker const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
396*77c1e3ccSAndroid Build Coastguard Worker const int bsw = tx_size_wide_unit[sub_txs];
397*77c1e3ccSAndroid Build Coastguard Worker const int bsh = tx_size_high_unit[sub_txs];
398*77c1e3ccSAndroid Build Coastguard Worker const int step = bsh * bsw;
399*77c1e3ccSAndroid Build Coastguard Worker const int row_end =
400*77c1e3ccSAndroid Build Coastguard Worker AOMMIN(tx_size_high_unit[tx_size], max_blocks_high - blk_row);
401*77c1e3ccSAndroid Build Coastguard Worker const int col_end =
402*77c1e3ccSAndroid Build Coastguard Worker AOMMIN(tx_size_wide_unit[tx_size], max_blocks_wide - blk_col);
403*77c1e3ccSAndroid Build Coastguard Worker
404*77c1e3ccSAndroid Build Coastguard Worker assert(bsw > 0 && bsh > 0);
405*77c1e3ccSAndroid Build Coastguard Worker
406*77c1e3ccSAndroid Build Coastguard Worker for (int r = 0; r < row_end; r += bsh) {
407*77c1e3ccSAndroid Build Coastguard Worker const int offsetr = blk_row + r;
408*77c1e3ccSAndroid Build Coastguard Worker for (int c = 0; c < col_end; c += bsw) {
409*77c1e3ccSAndroid Build Coastguard Worker const int offsetc = blk_col + c;
410*77c1e3ccSAndroid Build Coastguard Worker pack_txb_tokens(w, cm, x, tp, tok_end, xd, mbmi, plane, plane_bsize,
411*77c1e3ccSAndroid Build Coastguard Worker bit_depth, block, offsetr, offsetc, sub_txs,
412*77c1e3ccSAndroid Build Coastguard Worker token_stats);
413*77c1e3ccSAndroid Build Coastguard Worker block += step;
414*77c1e3ccSAndroid Build Coastguard Worker }
415*77c1e3ccSAndroid Build Coastguard Worker }
416*77c1e3ccSAndroid Build Coastguard Worker }
417*77c1e3ccSAndroid Build Coastguard Worker }
418*77c1e3ccSAndroid Build Coastguard Worker
set_spatial_segment_id(const CommonModeInfoParams * const mi_params,uint8_t * segment_ids,BLOCK_SIZE bsize,int mi_row,int mi_col,uint8_t segment_id)419*77c1e3ccSAndroid Build Coastguard Worker static inline void set_spatial_segment_id(
420*77c1e3ccSAndroid Build Coastguard Worker const CommonModeInfoParams *const mi_params, uint8_t *segment_ids,
421*77c1e3ccSAndroid Build Coastguard Worker BLOCK_SIZE bsize, int mi_row, int mi_col, uint8_t segment_id) {
422*77c1e3ccSAndroid Build Coastguard Worker const int mi_offset = mi_row * mi_params->mi_cols + mi_col;
423*77c1e3ccSAndroid Build Coastguard Worker const int bw = mi_size_wide[bsize];
424*77c1e3ccSAndroid Build Coastguard Worker const int bh = mi_size_high[bsize];
425*77c1e3ccSAndroid Build Coastguard Worker const int xmis = AOMMIN(mi_params->mi_cols - mi_col, bw);
426*77c1e3ccSAndroid Build Coastguard Worker const int ymis = AOMMIN(mi_params->mi_rows - mi_row, bh);
427*77c1e3ccSAndroid Build Coastguard Worker
428*77c1e3ccSAndroid Build Coastguard Worker const int mi_stride = mi_params->mi_cols;
429*77c1e3ccSAndroid Build Coastguard Worker
430*77c1e3ccSAndroid Build Coastguard Worker set_segment_id(segment_ids, mi_offset, xmis, ymis, mi_stride, segment_id);
431*77c1e3ccSAndroid Build Coastguard Worker }
432*77c1e3ccSAndroid Build Coastguard Worker
av1_neg_interleave(int x,int ref,int max)433*77c1e3ccSAndroid Build Coastguard Worker int av1_neg_interleave(int x, int ref, int max) {
434*77c1e3ccSAndroid Build Coastguard Worker assert(x < max);
435*77c1e3ccSAndroid Build Coastguard Worker const int diff = x - ref;
436*77c1e3ccSAndroid Build Coastguard Worker if (!ref) return x;
437*77c1e3ccSAndroid Build Coastguard Worker if (ref >= (max - 1)) return -x + max - 1;
438*77c1e3ccSAndroid Build Coastguard Worker if (2 * ref < max) {
439*77c1e3ccSAndroid Build Coastguard Worker if (abs(diff) <= ref) {
440*77c1e3ccSAndroid Build Coastguard Worker if (diff > 0)
441*77c1e3ccSAndroid Build Coastguard Worker return (diff << 1) - 1;
442*77c1e3ccSAndroid Build Coastguard Worker else
443*77c1e3ccSAndroid Build Coastguard Worker return ((-diff) << 1);
444*77c1e3ccSAndroid Build Coastguard Worker }
445*77c1e3ccSAndroid Build Coastguard Worker return x;
446*77c1e3ccSAndroid Build Coastguard Worker } else {
447*77c1e3ccSAndroid Build Coastguard Worker if (abs(diff) < (max - ref)) {
448*77c1e3ccSAndroid Build Coastguard Worker if (diff > 0)
449*77c1e3ccSAndroid Build Coastguard Worker return (diff << 1) - 1;
450*77c1e3ccSAndroid Build Coastguard Worker else
451*77c1e3ccSAndroid Build Coastguard Worker return ((-diff) << 1);
452*77c1e3ccSAndroid Build Coastguard Worker }
453*77c1e3ccSAndroid Build Coastguard Worker return (max - x) - 1;
454*77c1e3ccSAndroid Build Coastguard Worker }
455*77c1e3ccSAndroid Build Coastguard Worker }
456*77c1e3ccSAndroid Build Coastguard Worker
write_segment_id(AV1_COMP * cpi,MACROBLOCKD * const xd,const MB_MODE_INFO * const mbmi,aom_writer * w,const struct segmentation * seg,struct segmentation_probs * segp,int skip_txfm)457*77c1e3ccSAndroid Build Coastguard Worker static inline void write_segment_id(AV1_COMP *cpi, MACROBLOCKD *const xd,
458*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *const mbmi,
459*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w,
460*77c1e3ccSAndroid Build Coastguard Worker const struct segmentation *seg,
461*77c1e3ccSAndroid Build Coastguard Worker struct segmentation_probs *segp,
462*77c1e3ccSAndroid Build Coastguard Worker int skip_txfm) {
463*77c1e3ccSAndroid Build Coastguard Worker if (!seg->enabled || !seg->update_map) return;
464*77c1e3ccSAndroid Build Coastguard Worker
465*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
466*77c1e3ccSAndroid Build Coastguard Worker int cdf_num;
467*77c1e3ccSAndroid Build Coastguard Worker const uint8_t pred = av1_get_spatial_seg_pred(
468*77c1e3ccSAndroid Build Coastguard Worker cm, xd, &cdf_num, cpi->cyclic_refresh->skip_over4x4);
469*77c1e3ccSAndroid Build Coastguard Worker const int mi_row = xd->mi_row;
470*77c1e3ccSAndroid Build Coastguard Worker const int mi_col = xd->mi_col;
471*77c1e3ccSAndroid Build Coastguard Worker
472*77c1e3ccSAndroid Build Coastguard Worker if (skip_txfm) {
473*77c1e3ccSAndroid Build Coastguard Worker // Still need to transmit tx size for intra blocks even if skip_txfm is
474*77c1e3ccSAndroid Build Coastguard Worker // true. Changing segment_id may make the tx size become invalid, e.g
475*77c1e3ccSAndroid Build Coastguard Worker // changing from lossless to lossy.
476*77c1e3ccSAndroid Build Coastguard Worker assert(is_inter_block(mbmi) || !cpi->enc_seg.has_lossless_segment);
477*77c1e3ccSAndroid Build Coastguard Worker
478*77c1e3ccSAndroid Build Coastguard Worker set_spatial_segment_id(&cm->mi_params, cm->cur_frame->seg_map, mbmi->bsize,
479*77c1e3ccSAndroid Build Coastguard Worker mi_row, mi_col, pred);
480*77c1e3ccSAndroid Build Coastguard Worker set_spatial_segment_id(&cm->mi_params, cpi->enc_seg.map, mbmi->bsize,
481*77c1e3ccSAndroid Build Coastguard Worker mi_row, mi_col, pred);
482*77c1e3ccSAndroid Build Coastguard Worker /* mbmi is read only but we need to update segment_id */
483*77c1e3ccSAndroid Build Coastguard Worker ((MB_MODE_INFO *)mbmi)->segment_id = pred;
484*77c1e3ccSAndroid Build Coastguard Worker return;
485*77c1e3ccSAndroid Build Coastguard Worker }
486*77c1e3ccSAndroid Build Coastguard Worker
487*77c1e3ccSAndroid Build Coastguard Worker const int coded_id =
488*77c1e3ccSAndroid Build Coastguard Worker av1_neg_interleave(mbmi->segment_id, pred, seg->last_active_segid + 1);
489*77c1e3ccSAndroid Build Coastguard Worker aom_cdf_prob *pred_cdf = segp->spatial_pred_seg_cdf[cdf_num];
490*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, coded_id, pred_cdf, MAX_SEGMENTS);
491*77c1e3ccSAndroid Build Coastguard Worker set_spatial_segment_id(&cm->mi_params, cm->cur_frame->seg_map, mbmi->bsize,
492*77c1e3ccSAndroid Build Coastguard Worker mi_row, mi_col, mbmi->segment_id);
493*77c1e3ccSAndroid Build Coastguard Worker }
494*77c1e3ccSAndroid Build Coastguard Worker
495*77c1e3ccSAndroid Build Coastguard Worker #define WRITE_REF_BIT(bname, pname) \
496*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, bname, av1_get_pred_cdf_##pname(xd), 2)
497*77c1e3ccSAndroid Build Coastguard Worker
498*77c1e3ccSAndroid Build Coastguard Worker // This function encodes the reference frame
write_ref_frames(const AV1_COMMON * cm,const MACROBLOCKD * xd,aom_writer * w)499*77c1e3ccSAndroid Build Coastguard Worker static inline void write_ref_frames(const AV1_COMMON *cm, const MACROBLOCKD *xd,
500*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w) {
501*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *const mbmi = xd->mi[0];
502*77c1e3ccSAndroid Build Coastguard Worker const int is_compound = has_second_ref(mbmi);
503*77c1e3ccSAndroid Build Coastguard Worker const uint8_t segment_id = mbmi->segment_id;
504*77c1e3ccSAndroid Build Coastguard Worker
505*77c1e3ccSAndroid Build Coastguard Worker // If segment level coding of this signal is disabled...
506*77c1e3ccSAndroid Build Coastguard Worker // or the segment allows multiple reference frame options
507*77c1e3ccSAndroid Build Coastguard Worker if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
508*77c1e3ccSAndroid Build Coastguard Worker assert(!is_compound);
509*77c1e3ccSAndroid Build Coastguard Worker assert(mbmi->ref_frame[0] ==
510*77c1e3ccSAndroid Build Coastguard Worker get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME));
511*77c1e3ccSAndroid Build Coastguard Worker } else if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP) ||
512*77c1e3ccSAndroid Build Coastguard Worker segfeature_active(&cm->seg, segment_id, SEG_LVL_GLOBALMV)) {
513*77c1e3ccSAndroid Build Coastguard Worker assert(!is_compound);
514*77c1e3ccSAndroid Build Coastguard Worker assert(mbmi->ref_frame[0] == LAST_FRAME);
515*77c1e3ccSAndroid Build Coastguard Worker } else {
516*77c1e3ccSAndroid Build Coastguard Worker // does the feature use compound prediction or not
517*77c1e3ccSAndroid Build Coastguard Worker // (if not specified at the frame/segment level)
518*77c1e3ccSAndroid Build Coastguard Worker if (cm->current_frame.reference_mode == REFERENCE_MODE_SELECT) {
519*77c1e3ccSAndroid Build Coastguard Worker if (is_comp_ref_allowed(mbmi->bsize))
520*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, is_compound, av1_get_reference_mode_cdf(xd), 2);
521*77c1e3ccSAndroid Build Coastguard Worker } else {
522*77c1e3ccSAndroid Build Coastguard Worker assert((!is_compound) ==
523*77c1e3ccSAndroid Build Coastguard Worker (cm->current_frame.reference_mode == SINGLE_REFERENCE));
524*77c1e3ccSAndroid Build Coastguard Worker }
525*77c1e3ccSAndroid Build Coastguard Worker
526*77c1e3ccSAndroid Build Coastguard Worker if (is_compound) {
527*77c1e3ccSAndroid Build Coastguard Worker const COMP_REFERENCE_TYPE comp_ref_type = has_uni_comp_refs(mbmi)
528*77c1e3ccSAndroid Build Coastguard Worker ? UNIDIR_COMP_REFERENCE
529*77c1e3ccSAndroid Build Coastguard Worker : BIDIR_COMP_REFERENCE;
530*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, comp_ref_type, av1_get_comp_reference_type_cdf(xd),
531*77c1e3ccSAndroid Build Coastguard Worker 2);
532*77c1e3ccSAndroid Build Coastguard Worker
533*77c1e3ccSAndroid Build Coastguard Worker if (comp_ref_type == UNIDIR_COMP_REFERENCE) {
534*77c1e3ccSAndroid Build Coastguard Worker const int bit = mbmi->ref_frame[0] == BWDREF_FRAME;
535*77c1e3ccSAndroid Build Coastguard Worker WRITE_REF_BIT(bit, uni_comp_ref_p);
536*77c1e3ccSAndroid Build Coastguard Worker
537*77c1e3ccSAndroid Build Coastguard Worker if (!bit) {
538*77c1e3ccSAndroid Build Coastguard Worker assert(mbmi->ref_frame[0] == LAST_FRAME);
539*77c1e3ccSAndroid Build Coastguard Worker const int bit1 = mbmi->ref_frame[1] == LAST3_FRAME ||
540*77c1e3ccSAndroid Build Coastguard Worker mbmi->ref_frame[1] == GOLDEN_FRAME;
541*77c1e3ccSAndroid Build Coastguard Worker WRITE_REF_BIT(bit1, uni_comp_ref_p1);
542*77c1e3ccSAndroid Build Coastguard Worker if (bit1) {
543*77c1e3ccSAndroid Build Coastguard Worker const int bit2 = mbmi->ref_frame[1] == GOLDEN_FRAME;
544*77c1e3ccSAndroid Build Coastguard Worker WRITE_REF_BIT(bit2, uni_comp_ref_p2);
545*77c1e3ccSAndroid Build Coastguard Worker }
546*77c1e3ccSAndroid Build Coastguard Worker } else {
547*77c1e3ccSAndroid Build Coastguard Worker assert(mbmi->ref_frame[1] == ALTREF_FRAME);
548*77c1e3ccSAndroid Build Coastguard Worker }
549*77c1e3ccSAndroid Build Coastguard Worker
550*77c1e3ccSAndroid Build Coastguard Worker return;
551*77c1e3ccSAndroid Build Coastguard Worker }
552*77c1e3ccSAndroid Build Coastguard Worker
553*77c1e3ccSAndroid Build Coastguard Worker assert(comp_ref_type == BIDIR_COMP_REFERENCE);
554*77c1e3ccSAndroid Build Coastguard Worker
555*77c1e3ccSAndroid Build Coastguard Worker const int bit = (mbmi->ref_frame[0] == GOLDEN_FRAME ||
556*77c1e3ccSAndroid Build Coastguard Worker mbmi->ref_frame[0] == LAST3_FRAME);
557*77c1e3ccSAndroid Build Coastguard Worker WRITE_REF_BIT(bit, comp_ref_p);
558*77c1e3ccSAndroid Build Coastguard Worker
559*77c1e3ccSAndroid Build Coastguard Worker if (!bit) {
560*77c1e3ccSAndroid Build Coastguard Worker const int bit1 = mbmi->ref_frame[0] == LAST2_FRAME;
561*77c1e3ccSAndroid Build Coastguard Worker WRITE_REF_BIT(bit1, comp_ref_p1);
562*77c1e3ccSAndroid Build Coastguard Worker } else {
563*77c1e3ccSAndroid Build Coastguard Worker const int bit2 = mbmi->ref_frame[0] == GOLDEN_FRAME;
564*77c1e3ccSAndroid Build Coastguard Worker WRITE_REF_BIT(bit2, comp_ref_p2);
565*77c1e3ccSAndroid Build Coastguard Worker }
566*77c1e3ccSAndroid Build Coastguard Worker
567*77c1e3ccSAndroid Build Coastguard Worker const int bit_bwd = mbmi->ref_frame[1] == ALTREF_FRAME;
568*77c1e3ccSAndroid Build Coastguard Worker WRITE_REF_BIT(bit_bwd, comp_bwdref_p);
569*77c1e3ccSAndroid Build Coastguard Worker
570*77c1e3ccSAndroid Build Coastguard Worker if (!bit_bwd) {
571*77c1e3ccSAndroid Build Coastguard Worker WRITE_REF_BIT(mbmi->ref_frame[1] == ALTREF2_FRAME, comp_bwdref_p1);
572*77c1e3ccSAndroid Build Coastguard Worker }
573*77c1e3ccSAndroid Build Coastguard Worker
574*77c1e3ccSAndroid Build Coastguard Worker } else {
575*77c1e3ccSAndroid Build Coastguard Worker const int bit0 = (mbmi->ref_frame[0] <= ALTREF_FRAME &&
576*77c1e3ccSAndroid Build Coastguard Worker mbmi->ref_frame[0] >= BWDREF_FRAME);
577*77c1e3ccSAndroid Build Coastguard Worker WRITE_REF_BIT(bit0, single_ref_p1);
578*77c1e3ccSAndroid Build Coastguard Worker
579*77c1e3ccSAndroid Build Coastguard Worker if (bit0) {
580*77c1e3ccSAndroid Build Coastguard Worker const int bit1 = mbmi->ref_frame[0] == ALTREF_FRAME;
581*77c1e3ccSAndroid Build Coastguard Worker WRITE_REF_BIT(bit1, single_ref_p2);
582*77c1e3ccSAndroid Build Coastguard Worker
583*77c1e3ccSAndroid Build Coastguard Worker if (!bit1) {
584*77c1e3ccSAndroid Build Coastguard Worker WRITE_REF_BIT(mbmi->ref_frame[0] == ALTREF2_FRAME, single_ref_p6);
585*77c1e3ccSAndroid Build Coastguard Worker }
586*77c1e3ccSAndroid Build Coastguard Worker } else {
587*77c1e3ccSAndroid Build Coastguard Worker const int bit2 = (mbmi->ref_frame[0] == LAST3_FRAME ||
588*77c1e3ccSAndroid Build Coastguard Worker mbmi->ref_frame[0] == GOLDEN_FRAME);
589*77c1e3ccSAndroid Build Coastguard Worker WRITE_REF_BIT(bit2, single_ref_p3);
590*77c1e3ccSAndroid Build Coastguard Worker
591*77c1e3ccSAndroid Build Coastguard Worker if (!bit2) {
592*77c1e3ccSAndroid Build Coastguard Worker const int bit3 = mbmi->ref_frame[0] != LAST_FRAME;
593*77c1e3ccSAndroid Build Coastguard Worker WRITE_REF_BIT(bit3, single_ref_p4);
594*77c1e3ccSAndroid Build Coastguard Worker } else {
595*77c1e3ccSAndroid Build Coastguard Worker const int bit4 = mbmi->ref_frame[0] != LAST3_FRAME;
596*77c1e3ccSAndroid Build Coastguard Worker WRITE_REF_BIT(bit4, single_ref_p5);
597*77c1e3ccSAndroid Build Coastguard Worker }
598*77c1e3ccSAndroid Build Coastguard Worker }
599*77c1e3ccSAndroid Build Coastguard Worker }
600*77c1e3ccSAndroid Build Coastguard Worker }
601*77c1e3ccSAndroid Build Coastguard Worker }
602*77c1e3ccSAndroid Build Coastguard Worker
write_filter_intra_mode_info(const AV1_COMMON * cm,const MACROBLOCKD * xd,const MB_MODE_INFO * const mbmi,aom_writer * w)603*77c1e3ccSAndroid Build Coastguard Worker static inline void write_filter_intra_mode_info(const AV1_COMMON *cm,
604*77c1e3ccSAndroid Build Coastguard Worker const MACROBLOCKD *xd,
605*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *const mbmi,
606*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w) {
607*77c1e3ccSAndroid Build Coastguard Worker if (av1_filter_intra_allowed(cm, mbmi)) {
608*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, mbmi->filter_intra_mode_info.use_filter_intra,
609*77c1e3ccSAndroid Build Coastguard Worker xd->tile_ctx->filter_intra_cdfs[mbmi->bsize], 2);
610*77c1e3ccSAndroid Build Coastguard Worker if (mbmi->filter_intra_mode_info.use_filter_intra) {
611*77c1e3ccSAndroid Build Coastguard Worker const FILTER_INTRA_MODE mode =
612*77c1e3ccSAndroid Build Coastguard Worker mbmi->filter_intra_mode_info.filter_intra_mode;
613*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, mode, xd->tile_ctx->filter_intra_mode_cdf,
614*77c1e3ccSAndroid Build Coastguard Worker FILTER_INTRA_MODES);
615*77c1e3ccSAndroid Build Coastguard Worker }
616*77c1e3ccSAndroid Build Coastguard Worker }
617*77c1e3ccSAndroid Build Coastguard Worker }
618*77c1e3ccSAndroid Build Coastguard Worker
write_angle_delta(aom_writer * w,int angle_delta,aom_cdf_prob * cdf)619*77c1e3ccSAndroid Build Coastguard Worker static inline void write_angle_delta(aom_writer *w, int angle_delta,
620*77c1e3ccSAndroid Build Coastguard Worker aom_cdf_prob *cdf) {
621*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, angle_delta + MAX_ANGLE_DELTA, cdf,
622*77c1e3ccSAndroid Build Coastguard Worker 2 * MAX_ANGLE_DELTA + 1);
623*77c1e3ccSAndroid Build Coastguard Worker }
624*77c1e3ccSAndroid Build Coastguard Worker
write_mb_interp_filter(AV1_COMMON * const cm,ThreadData * td,aom_writer * w)625*77c1e3ccSAndroid Build Coastguard Worker static inline void write_mb_interp_filter(AV1_COMMON *const cm, ThreadData *td,
626*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w) {
627*77c1e3ccSAndroid Build Coastguard Worker const MACROBLOCKD *xd = &td->mb.e_mbd;
628*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *const mbmi = xd->mi[0];
629*77c1e3ccSAndroid Build Coastguard Worker FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
630*77c1e3ccSAndroid Build Coastguard Worker
631*77c1e3ccSAndroid Build Coastguard Worker if (!av1_is_interp_needed(xd)) {
632*77c1e3ccSAndroid Build Coastguard Worker int_interpfilters filters = av1_broadcast_interp_filter(
633*77c1e3ccSAndroid Build Coastguard Worker av1_unswitchable_filter(cm->features.interp_filter));
634*77c1e3ccSAndroid Build Coastguard Worker assert(mbmi->interp_filters.as_int == filters.as_int);
635*77c1e3ccSAndroid Build Coastguard Worker (void)filters;
636*77c1e3ccSAndroid Build Coastguard Worker return;
637*77c1e3ccSAndroid Build Coastguard Worker }
638*77c1e3ccSAndroid Build Coastguard Worker if (cm->features.interp_filter == SWITCHABLE) {
639*77c1e3ccSAndroid Build Coastguard Worker int dir;
640*77c1e3ccSAndroid Build Coastguard Worker for (dir = 0; dir < 2; ++dir) {
641*77c1e3ccSAndroid Build Coastguard Worker const int ctx = av1_get_pred_context_switchable_interp(xd, dir);
642*77c1e3ccSAndroid Build Coastguard Worker InterpFilter filter =
643*77c1e3ccSAndroid Build Coastguard Worker av1_extract_interp_filter(mbmi->interp_filters, dir);
644*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, filter, ec_ctx->switchable_interp_cdf[ctx],
645*77c1e3ccSAndroid Build Coastguard Worker SWITCHABLE_FILTERS);
646*77c1e3ccSAndroid Build Coastguard Worker ++td->interp_filter_selected[filter];
647*77c1e3ccSAndroid Build Coastguard Worker if (cm->seq_params->enable_dual_filter == 0) return;
648*77c1e3ccSAndroid Build Coastguard Worker }
649*77c1e3ccSAndroid Build Coastguard Worker }
650*77c1e3ccSAndroid Build Coastguard Worker }
651*77c1e3ccSAndroid Build Coastguard Worker
652*77c1e3ccSAndroid Build Coastguard Worker // Transmit color values with delta encoding. Write the first value as
653*77c1e3ccSAndroid Build Coastguard Worker // literal, and the deltas between each value and the previous one. "min_val" is
654*77c1e3ccSAndroid Build Coastguard Worker // the smallest possible value of the deltas.
delta_encode_palette_colors(const int * colors,int num,int bit_depth,int min_val,aom_writer * w)655*77c1e3ccSAndroid Build Coastguard Worker static inline void delta_encode_palette_colors(const int *colors, int num,
656*77c1e3ccSAndroid Build Coastguard Worker int bit_depth, int min_val,
657*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w) {
658*77c1e3ccSAndroid Build Coastguard Worker if (num <= 0) return;
659*77c1e3ccSAndroid Build Coastguard Worker assert(colors[0] < (1 << bit_depth));
660*77c1e3ccSAndroid Build Coastguard Worker aom_write_literal(w, colors[0], bit_depth);
661*77c1e3ccSAndroid Build Coastguard Worker if (num == 1) return;
662*77c1e3ccSAndroid Build Coastguard Worker int max_delta = 0;
663*77c1e3ccSAndroid Build Coastguard Worker int deltas[PALETTE_MAX_SIZE];
664*77c1e3ccSAndroid Build Coastguard Worker memset(deltas, 0, sizeof(deltas));
665*77c1e3ccSAndroid Build Coastguard Worker for (int i = 1; i < num; ++i) {
666*77c1e3ccSAndroid Build Coastguard Worker assert(colors[i] < (1 << bit_depth));
667*77c1e3ccSAndroid Build Coastguard Worker const int delta = colors[i] - colors[i - 1];
668*77c1e3ccSAndroid Build Coastguard Worker deltas[i - 1] = delta;
669*77c1e3ccSAndroid Build Coastguard Worker assert(delta >= min_val);
670*77c1e3ccSAndroid Build Coastguard Worker if (delta > max_delta) max_delta = delta;
671*77c1e3ccSAndroid Build Coastguard Worker }
672*77c1e3ccSAndroid Build Coastguard Worker const int min_bits = bit_depth - 3;
673*77c1e3ccSAndroid Build Coastguard Worker int bits = AOMMAX(av1_ceil_log2(max_delta + 1 - min_val), min_bits);
674*77c1e3ccSAndroid Build Coastguard Worker assert(bits <= bit_depth);
675*77c1e3ccSAndroid Build Coastguard Worker int range = (1 << bit_depth) - colors[0] - min_val;
676*77c1e3ccSAndroid Build Coastguard Worker aom_write_literal(w, bits - min_bits, 2);
677*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < num - 1; ++i) {
678*77c1e3ccSAndroid Build Coastguard Worker aom_write_literal(w, deltas[i] - min_val, bits);
679*77c1e3ccSAndroid Build Coastguard Worker range -= deltas[i];
680*77c1e3ccSAndroid Build Coastguard Worker bits = AOMMIN(bits, av1_ceil_log2(range));
681*77c1e3ccSAndroid Build Coastguard Worker }
682*77c1e3ccSAndroid Build Coastguard Worker }
683*77c1e3ccSAndroid Build Coastguard Worker
684*77c1e3ccSAndroid Build Coastguard Worker // Transmit luma palette color values. First signal if each color in the color
685*77c1e3ccSAndroid Build Coastguard Worker // cache is used. Those colors that are not in the cache are transmitted with
686*77c1e3ccSAndroid Build Coastguard Worker // delta encoding.
write_palette_colors_y(const MACROBLOCKD * const xd,const PALETTE_MODE_INFO * const pmi,int bit_depth,aom_writer * w)687*77c1e3ccSAndroid Build Coastguard Worker static inline void write_palette_colors_y(const MACROBLOCKD *const xd,
688*77c1e3ccSAndroid Build Coastguard Worker const PALETTE_MODE_INFO *const pmi,
689*77c1e3ccSAndroid Build Coastguard Worker int bit_depth, aom_writer *w) {
690*77c1e3ccSAndroid Build Coastguard Worker const int n = pmi->palette_size[0];
691*77c1e3ccSAndroid Build Coastguard Worker uint16_t color_cache[2 * PALETTE_MAX_SIZE];
692*77c1e3ccSAndroid Build Coastguard Worker const int n_cache = av1_get_palette_cache(xd, 0, color_cache);
693*77c1e3ccSAndroid Build Coastguard Worker int out_cache_colors[PALETTE_MAX_SIZE];
694*77c1e3ccSAndroid Build Coastguard Worker uint8_t cache_color_found[2 * PALETTE_MAX_SIZE];
695*77c1e3ccSAndroid Build Coastguard Worker const int n_out_cache =
696*77c1e3ccSAndroid Build Coastguard Worker av1_index_color_cache(color_cache, n_cache, pmi->palette_colors, n,
697*77c1e3ccSAndroid Build Coastguard Worker cache_color_found, out_cache_colors);
698*77c1e3ccSAndroid Build Coastguard Worker int n_in_cache = 0;
699*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < n_cache && n_in_cache < n; ++i) {
700*77c1e3ccSAndroid Build Coastguard Worker const int found = cache_color_found[i];
701*77c1e3ccSAndroid Build Coastguard Worker aom_write_bit(w, found);
702*77c1e3ccSAndroid Build Coastguard Worker n_in_cache += found;
703*77c1e3ccSAndroid Build Coastguard Worker }
704*77c1e3ccSAndroid Build Coastguard Worker assert(n_in_cache + n_out_cache == n);
705*77c1e3ccSAndroid Build Coastguard Worker delta_encode_palette_colors(out_cache_colors, n_out_cache, bit_depth, 1, w);
706*77c1e3ccSAndroid Build Coastguard Worker }
707*77c1e3ccSAndroid Build Coastguard Worker
708*77c1e3ccSAndroid Build Coastguard Worker // Write chroma palette color values. U channel is handled similarly to the luma
709*77c1e3ccSAndroid Build Coastguard Worker // channel. For v channel, either use delta encoding or transmit raw values
710*77c1e3ccSAndroid Build Coastguard Worker // directly, whichever costs less.
write_palette_colors_uv(const MACROBLOCKD * const xd,const PALETTE_MODE_INFO * const pmi,int bit_depth,aom_writer * w)711*77c1e3ccSAndroid Build Coastguard Worker static inline void write_palette_colors_uv(const MACROBLOCKD *const xd,
712*77c1e3ccSAndroid Build Coastguard Worker const PALETTE_MODE_INFO *const pmi,
713*77c1e3ccSAndroid Build Coastguard Worker int bit_depth, aom_writer *w) {
714*77c1e3ccSAndroid Build Coastguard Worker const int n = pmi->palette_size[1];
715*77c1e3ccSAndroid Build Coastguard Worker const uint16_t *colors_u = pmi->palette_colors + PALETTE_MAX_SIZE;
716*77c1e3ccSAndroid Build Coastguard Worker const uint16_t *colors_v = pmi->palette_colors + 2 * PALETTE_MAX_SIZE;
717*77c1e3ccSAndroid Build Coastguard Worker // U channel colors.
718*77c1e3ccSAndroid Build Coastguard Worker uint16_t color_cache[2 * PALETTE_MAX_SIZE];
719*77c1e3ccSAndroid Build Coastguard Worker const int n_cache = av1_get_palette_cache(xd, 1, color_cache);
720*77c1e3ccSAndroid Build Coastguard Worker int out_cache_colors[PALETTE_MAX_SIZE];
721*77c1e3ccSAndroid Build Coastguard Worker uint8_t cache_color_found[2 * PALETTE_MAX_SIZE];
722*77c1e3ccSAndroid Build Coastguard Worker const int n_out_cache = av1_index_color_cache(
723*77c1e3ccSAndroid Build Coastguard Worker color_cache, n_cache, colors_u, n, cache_color_found, out_cache_colors);
724*77c1e3ccSAndroid Build Coastguard Worker int n_in_cache = 0;
725*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < n_cache && n_in_cache < n; ++i) {
726*77c1e3ccSAndroid Build Coastguard Worker const int found = cache_color_found[i];
727*77c1e3ccSAndroid Build Coastguard Worker aom_write_bit(w, found);
728*77c1e3ccSAndroid Build Coastguard Worker n_in_cache += found;
729*77c1e3ccSAndroid Build Coastguard Worker }
730*77c1e3ccSAndroid Build Coastguard Worker delta_encode_palette_colors(out_cache_colors, n_out_cache, bit_depth, 0, w);
731*77c1e3ccSAndroid Build Coastguard Worker
732*77c1e3ccSAndroid Build Coastguard Worker // V channel colors. Don't use color cache as the colors are not sorted.
733*77c1e3ccSAndroid Build Coastguard Worker const int max_val = 1 << bit_depth;
734*77c1e3ccSAndroid Build Coastguard Worker int zero_count = 0, min_bits_v = 0;
735*77c1e3ccSAndroid Build Coastguard Worker int bits_v =
736*77c1e3ccSAndroid Build Coastguard Worker av1_get_palette_delta_bits_v(pmi, bit_depth, &zero_count, &min_bits_v);
737*77c1e3ccSAndroid Build Coastguard Worker const int rate_using_delta =
738*77c1e3ccSAndroid Build Coastguard Worker 2 + bit_depth + (bits_v + 1) * (n - 1) - zero_count;
739*77c1e3ccSAndroid Build Coastguard Worker const int rate_using_raw = bit_depth * n;
740*77c1e3ccSAndroid Build Coastguard Worker if (rate_using_delta < rate_using_raw) { // delta encoding
741*77c1e3ccSAndroid Build Coastguard Worker assert(colors_v[0] < (1 << bit_depth));
742*77c1e3ccSAndroid Build Coastguard Worker aom_write_bit(w, 1);
743*77c1e3ccSAndroid Build Coastguard Worker aom_write_literal(w, bits_v - min_bits_v, 2);
744*77c1e3ccSAndroid Build Coastguard Worker aom_write_literal(w, colors_v[0], bit_depth);
745*77c1e3ccSAndroid Build Coastguard Worker for (int i = 1; i < n; ++i) {
746*77c1e3ccSAndroid Build Coastguard Worker assert(colors_v[i] < (1 << bit_depth));
747*77c1e3ccSAndroid Build Coastguard Worker if (colors_v[i] == colors_v[i - 1]) { // No need to signal sign bit.
748*77c1e3ccSAndroid Build Coastguard Worker aom_write_literal(w, 0, bits_v);
749*77c1e3ccSAndroid Build Coastguard Worker continue;
750*77c1e3ccSAndroid Build Coastguard Worker }
751*77c1e3ccSAndroid Build Coastguard Worker const int delta = abs((int)colors_v[i] - colors_v[i - 1]);
752*77c1e3ccSAndroid Build Coastguard Worker const int sign_bit = colors_v[i] < colors_v[i - 1];
753*77c1e3ccSAndroid Build Coastguard Worker if (delta <= max_val - delta) {
754*77c1e3ccSAndroid Build Coastguard Worker aom_write_literal(w, delta, bits_v);
755*77c1e3ccSAndroid Build Coastguard Worker aom_write_bit(w, sign_bit);
756*77c1e3ccSAndroid Build Coastguard Worker } else {
757*77c1e3ccSAndroid Build Coastguard Worker aom_write_literal(w, max_val - delta, bits_v);
758*77c1e3ccSAndroid Build Coastguard Worker aom_write_bit(w, !sign_bit);
759*77c1e3ccSAndroid Build Coastguard Worker }
760*77c1e3ccSAndroid Build Coastguard Worker }
761*77c1e3ccSAndroid Build Coastguard Worker } else { // Transmit raw values.
762*77c1e3ccSAndroid Build Coastguard Worker aom_write_bit(w, 0);
763*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < n; ++i) {
764*77c1e3ccSAndroid Build Coastguard Worker assert(colors_v[i] < (1 << bit_depth));
765*77c1e3ccSAndroid Build Coastguard Worker aom_write_literal(w, colors_v[i], bit_depth);
766*77c1e3ccSAndroid Build Coastguard Worker }
767*77c1e3ccSAndroid Build Coastguard Worker }
768*77c1e3ccSAndroid Build Coastguard Worker }
769*77c1e3ccSAndroid Build Coastguard Worker
write_palette_mode_info(const AV1_COMMON * cm,const MACROBLOCKD * xd,const MB_MODE_INFO * const mbmi,aom_writer * w)770*77c1e3ccSAndroid Build Coastguard Worker static inline void write_palette_mode_info(const AV1_COMMON *cm,
771*77c1e3ccSAndroid Build Coastguard Worker const MACROBLOCKD *xd,
772*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *const mbmi,
773*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w) {
774*77c1e3ccSAndroid Build Coastguard Worker const int num_planes = av1_num_planes(cm);
775*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE bsize = mbmi->bsize;
776*77c1e3ccSAndroid Build Coastguard Worker assert(av1_allow_palette(cm->features.allow_screen_content_tools, bsize));
777*77c1e3ccSAndroid Build Coastguard Worker const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
778*77c1e3ccSAndroid Build Coastguard Worker const int bsize_ctx = av1_get_palette_bsize_ctx(bsize);
779*77c1e3ccSAndroid Build Coastguard Worker
780*77c1e3ccSAndroid Build Coastguard Worker if (mbmi->mode == DC_PRED) {
781*77c1e3ccSAndroid Build Coastguard Worker const int n = pmi->palette_size[0];
782*77c1e3ccSAndroid Build Coastguard Worker const int palette_y_mode_ctx = av1_get_palette_mode_ctx(xd);
783*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(
784*77c1e3ccSAndroid Build Coastguard Worker w, n > 0,
785*77c1e3ccSAndroid Build Coastguard Worker xd->tile_ctx->palette_y_mode_cdf[bsize_ctx][palette_y_mode_ctx], 2);
786*77c1e3ccSAndroid Build Coastguard Worker if (n > 0) {
787*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, n - PALETTE_MIN_SIZE,
788*77c1e3ccSAndroid Build Coastguard Worker xd->tile_ctx->palette_y_size_cdf[bsize_ctx],
789*77c1e3ccSAndroid Build Coastguard Worker PALETTE_SIZES);
790*77c1e3ccSAndroid Build Coastguard Worker write_palette_colors_y(xd, pmi, cm->seq_params->bit_depth, w);
791*77c1e3ccSAndroid Build Coastguard Worker }
792*77c1e3ccSAndroid Build Coastguard Worker }
793*77c1e3ccSAndroid Build Coastguard Worker
794*77c1e3ccSAndroid Build Coastguard Worker const int uv_dc_pred =
795*77c1e3ccSAndroid Build Coastguard Worker num_planes > 1 && mbmi->uv_mode == UV_DC_PRED && xd->is_chroma_ref;
796*77c1e3ccSAndroid Build Coastguard Worker if (uv_dc_pred) {
797*77c1e3ccSAndroid Build Coastguard Worker const int n = pmi->palette_size[1];
798*77c1e3ccSAndroid Build Coastguard Worker const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0);
799*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, n > 0,
800*77c1e3ccSAndroid Build Coastguard Worker xd->tile_ctx->palette_uv_mode_cdf[palette_uv_mode_ctx], 2);
801*77c1e3ccSAndroid Build Coastguard Worker if (n > 0) {
802*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, n - PALETTE_MIN_SIZE,
803*77c1e3ccSAndroid Build Coastguard Worker xd->tile_ctx->palette_uv_size_cdf[bsize_ctx],
804*77c1e3ccSAndroid Build Coastguard Worker PALETTE_SIZES);
805*77c1e3ccSAndroid Build Coastguard Worker write_palette_colors_uv(xd, pmi, cm->seq_params->bit_depth, w);
806*77c1e3ccSAndroid Build Coastguard Worker }
807*77c1e3ccSAndroid Build Coastguard Worker }
808*77c1e3ccSAndroid Build Coastguard Worker }
809*77c1e3ccSAndroid Build Coastguard Worker
av1_write_tx_type(const AV1_COMMON * const cm,const MACROBLOCKD * xd,TX_TYPE tx_type,TX_SIZE tx_size,aom_writer * w)810*77c1e3ccSAndroid Build Coastguard Worker void av1_write_tx_type(const AV1_COMMON *const cm, const MACROBLOCKD *xd,
811*77c1e3ccSAndroid Build Coastguard Worker TX_TYPE tx_type, TX_SIZE tx_size, aom_writer *w) {
812*77c1e3ccSAndroid Build Coastguard Worker MB_MODE_INFO *mbmi = xd->mi[0];
813*77c1e3ccSAndroid Build Coastguard Worker const FeatureFlags *const features = &cm->features;
814*77c1e3ccSAndroid Build Coastguard Worker const int is_inter = is_inter_block(mbmi);
815*77c1e3ccSAndroid Build Coastguard Worker if (get_ext_tx_types(tx_size, is_inter, features->reduced_tx_set_used) > 1 &&
816*77c1e3ccSAndroid Build Coastguard Worker ((!cm->seg.enabled && cm->quant_params.base_qindex > 0) ||
817*77c1e3ccSAndroid Build Coastguard Worker (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) &&
818*77c1e3ccSAndroid Build Coastguard Worker !mbmi->skip_txfm &&
819*77c1e3ccSAndroid Build Coastguard Worker !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
820*77c1e3ccSAndroid Build Coastguard Worker FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
821*77c1e3ccSAndroid Build Coastguard Worker const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
822*77c1e3ccSAndroid Build Coastguard Worker const TxSetType tx_set_type = av1_get_ext_tx_set_type(
823*77c1e3ccSAndroid Build Coastguard Worker tx_size, is_inter, features->reduced_tx_set_used);
824*77c1e3ccSAndroid Build Coastguard Worker const int eset =
825*77c1e3ccSAndroid Build Coastguard Worker get_ext_tx_set(tx_size, is_inter, features->reduced_tx_set_used);
826*77c1e3ccSAndroid Build Coastguard Worker // eset == 0 should correspond to a set with only DCT_DCT and there
827*77c1e3ccSAndroid Build Coastguard Worker // is no need to send the tx_type
828*77c1e3ccSAndroid Build Coastguard Worker assert(eset > 0);
829*77c1e3ccSAndroid Build Coastguard Worker assert(av1_ext_tx_used[tx_set_type][tx_type]);
830*77c1e3ccSAndroid Build Coastguard Worker if (is_inter) {
831*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, av1_ext_tx_ind[tx_set_type][tx_type],
832*77c1e3ccSAndroid Build Coastguard Worker ec_ctx->inter_ext_tx_cdf[eset][square_tx_size],
833*77c1e3ccSAndroid Build Coastguard Worker av1_num_ext_tx_set[tx_set_type]);
834*77c1e3ccSAndroid Build Coastguard Worker } else {
835*77c1e3ccSAndroid Build Coastguard Worker PREDICTION_MODE intra_dir;
836*77c1e3ccSAndroid Build Coastguard Worker if (mbmi->filter_intra_mode_info.use_filter_intra)
837*77c1e3ccSAndroid Build Coastguard Worker intra_dir =
838*77c1e3ccSAndroid Build Coastguard Worker fimode_to_intradir[mbmi->filter_intra_mode_info.filter_intra_mode];
839*77c1e3ccSAndroid Build Coastguard Worker else
840*77c1e3ccSAndroid Build Coastguard Worker intra_dir = mbmi->mode;
841*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(
842*77c1e3ccSAndroid Build Coastguard Worker w, av1_ext_tx_ind[tx_set_type][tx_type],
843*77c1e3ccSAndroid Build Coastguard Worker ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][intra_dir],
844*77c1e3ccSAndroid Build Coastguard Worker av1_num_ext_tx_set[tx_set_type]);
845*77c1e3ccSAndroid Build Coastguard Worker }
846*77c1e3ccSAndroid Build Coastguard Worker }
847*77c1e3ccSAndroid Build Coastguard Worker }
848*77c1e3ccSAndroid Build Coastguard Worker
write_intra_y_mode_nonkf(FRAME_CONTEXT * frame_ctx,BLOCK_SIZE bsize,PREDICTION_MODE mode,aom_writer * w)849*77c1e3ccSAndroid Build Coastguard Worker static inline void write_intra_y_mode_nonkf(FRAME_CONTEXT *frame_ctx,
850*77c1e3ccSAndroid Build Coastguard Worker BLOCK_SIZE bsize,
851*77c1e3ccSAndroid Build Coastguard Worker PREDICTION_MODE mode,
852*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w) {
853*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, mode, frame_ctx->y_mode_cdf[size_group_lookup[bsize]],
854*77c1e3ccSAndroid Build Coastguard Worker INTRA_MODES);
855*77c1e3ccSAndroid Build Coastguard Worker }
856*77c1e3ccSAndroid Build Coastguard Worker
write_intra_uv_mode(FRAME_CONTEXT * frame_ctx,UV_PREDICTION_MODE uv_mode,PREDICTION_MODE y_mode,CFL_ALLOWED_TYPE cfl_allowed,aom_writer * w)857*77c1e3ccSAndroid Build Coastguard Worker static inline void write_intra_uv_mode(FRAME_CONTEXT *frame_ctx,
858*77c1e3ccSAndroid Build Coastguard Worker UV_PREDICTION_MODE uv_mode,
859*77c1e3ccSAndroid Build Coastguard Worker PREDICTION_MODE y_mode,
860*77c1e3ccSAndroid Build Coastguard Worker CFL_ALLOWED_TYPE cfl_allowed,
861*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w) {
862*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, uv_mode, frame_ctx->uv_mode_cdf[cfl_allowed][y_mode],
863*77c1e3ccSAndroid Build Coastguard Worker UV_INTRA_MODES - !cfl_allowed);
864*77c1e3ccSAndroid Build Coastguard Worker }
865*77c1e3ccSAndroid Build Coastguard Worker
write_cfl_alphas(FRAME_CONTEXT * const ec_ctx,uint8_t idx,int8_t joint_sign,aom_writer * w)866*77c1e3ccSAndroid Build Coastguard Worker static inline void write_cfl_alphas(FRAME_CONTEXT *const ec_ctx, uint8_t idx,
867*77c1e3ccSAndroid Build Coastguard Worker int8_t joint_sign, aom_writer *w) {
868*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, joint_sign, ec_ctx->cfl_sign_cdf, CFL_JOINT_SIGNS);
869*77c1e3ccSAndroid Build Coastguard Worker // Magnitudes are only signaled for nonzero codes.
870*77c1e3ccSAndroid Build Coastguard Worker if (CFL_SIGN_U(joint_sign) != CFL_SIGN_ZERO) {
871*77c1e3ccSAndroid Build Coastguard Worker aom_cdf_prob *cdf_u = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)];
872*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, CFL_IDX_U(idx), cdf_u, CFL_ALPHABET_SIZE);
873*77c1e3ccSAndroid Build Coastguard Worker }
874*77c1e3ccSAndroid Build Coastguard Worker if (CFL_SIGN_V(joint_sign) != CFL_SIGN_ZERO) {
875*77c1e3ccSAndroid Build Coastguard Worker aom_cdf_prob *cdf_v = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_V(joint_sign)];
876*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, CFL_IDX_V(idx), cdf_v, CFL_ALPHABET_SIZE);
877*77c1e3ccSAndroid Build Coastguard Worker }
878*77c1e3ccSAndroid Build Coastguard Worker }
879*77c1e3ccSAndroid Build Coastguard Worker
write_cdef(AV1_COMMON * cm,MACROBLOCKD * const xd,aom_writer * w,int skip)880*77c1e3ccSAndroid Build Coastguard Worker static inline void write_cdef(AV1_COMMON *cm, MACROBLOCKD *const xd,
881*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w, int skip) {
882*77c1e3ccSAndroid Build Coastguard Worker if (cm->features.coded_lossless || cm->features.allow_intrabc) return;
883*77c1e3ccSAndroid Build Coastguard Worker
884*77c1e3ccSAndroid Build Coastguard Worker // At the start of a superblock, mark that we haven't yet written CDEF
885*77c1e3ccSAndroid Build Coastguard Worker // strengths for any of the CDEF units contained in this superblock.
886*77c1e3ccSAndroid Build Coastguard Worker const int sb_mask = (cm->seq_params->mib_size - 1);
887*77c1e3ccSAndroid Build Coastguard Worker const int mi_row_in_sb = (xd->mi_row & sb_mask);
888*77c1e3ccSAndroid Build Coastguard Worker const int mi_col_in_sb = (xd->mi_col & sb_mask);
889*77c1e3ccSAndroid Build Coastguard Worker if (mi_row_in_sb == 0 && mi_col_in_sb == 0) {
890*77c1e3ccSAndroid Build Coastguard Worker xd->cdef_transmitted[0] = xd->cdef_transmitted[1] =
891*77c1e3ccSAndroid Build Coastguard Worker xd->cdef_transmitted[2] = xd->cdef_transmitted[3] = false;
892*77c1e3ccSAndroid Build Coastguard Worker }
893*77c1e3ccSAndroid Build Coastguard Worker
894*77c1e3ccSAndroid Build Coastguard Worker // CDEF unit size is 64x64 irrespective of the superblock size.
895*77c1e3ccSAndroid Build Coastguard Worker const int cdef_size = 1 << (6 - MI_SIZE_LOG2);
896*77c1e3ccSAndroid Build Coastguard Worker
897*77c1e3ccSAndroid Build Coastguard Worker // Find index of this CDEF unit in this superblock.
898*77c1e3ccSAndroid Build Coastguard Worker const int index_mask = cdef_size;
899*77c1e3ccSAndroid Build Coastguard Worker const int cdef_unit_row_in_sb = ((xd->mi_row & index_mask) != 0);
900*77c1e3ccSAndroid Build Coastguard Worker const int cdef_unit_col_in_sb = ((xd->mi_col & index_mask) != 0);
901*77c1e3ccSAndroid Build Coastguard Worker const int index = (cm->seq_params->sb_size == BLOCK_128X128)
902*77c1e3ccSAndroid Build Coastguard Worker ? cdef_unit_col_in_sb + 2 * cdef_unit_row_in_sb
903*77c1e3ccSAndroid Build Coastguard Worker : 0;
904*77c1e3ccSAndroid Build Coastguard Worker
905*77c1e3ccSAndroid Build Coastguard Worker // Write CDEF strength to the first non-skip coding block in this CDEF unit.
906*77c1e3ccSAndroid Build Coastguard Worker if (!xd->cdef_transmitted[index] && !skip) {
907*77c1e3ccSAndroid Build Coastguard Worker // CDEF strength for this CDEF unit needs to be stored in the MB_MODE_INFO
908*77c1e3ccSAndroid Build Coastguard Worker // of the 1st block in this CDEF unit.
909*77c1e3ccSAndroid Build Coastguard Worker const int first_block_mask = ~(cdef_size - 1);
910*77c1e3ccSAndroid Build Coastguard Worker const CommonModeInfoParams *const mi_params = &cm->mi_params;
911*77c1e3ccSAndroid Build Coastguard Worker const int grid_idx =
912*77c1e3ccSAndroid Build Coastguard Worker get_mi_grid_idx(mi_params, xd->mi_row & first_block_mask,
913*77c1e3ccSAndroid Build Coastguard Worker xd->mi_col & first_block_mask);
914*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *const mbmi = mi_params->mi_grid_base[grid_idx];
915*77c1e3ccSAndroid Build Coastguard Worker aom_write_literal(w, mbmi->cdef_strength, cm->cdef_info.cdef_bits);
916*77c1e3ccSAndroid Build Coastguard Worker xd->cdef_transmitted[index] = true;
917*77c1e3ccSAndroid Build Coastguard Worker }
918*77c1e3ccSAndroid Build Coastguard Worker }
919*77c1e3ccSAndroid Build Coastguard Worker
write_inter_segment_id(AV1_COMP * cpi,MACROBLOCKD * const xd,aom_writer * w,const struct segmentation * const seg,struct segmentation_probs * const segp,int skip,int preskip)920*77c1e3ccSAndroid Build Coastguard Worker static inline void write_inter_segment_id(AV1_COMP *cpi, MACROBLOCKD *const xd,
921*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w,
922*77c1e3ccSAndroid Build Coastguard Worker const struct segmentation *const seg,
923*77c1e3ccSAndroid Build Coastguard Worker struct segmentation_probs *const segp,
924*77c1e3ccSAndroid Build Coastguard Worker int skip, int preskip) {
925*77c1e3ccSAndroid Build Coastguard Worker MB_MODE_INFO *const mbmi = xd->mi[0];
926*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
927*77c1e3ccSAndroid Build Coastguard Worker const int mi_row = xd->mi_row;
928*77c1e3ccSAndroid Build Coastguard Worker const int mi_col = xd->mi_col;
929*77c1e3ccSAndroid Build Coastguard Worker
930*77c1e3ccSAndroid Build Coastguard Worker if (seg->update_map) {
931*77c1e3ccSAndroid Build Coastguard Worker if (preskip) {
932*77c1e3ccSAndroid Build Coastguard Worker if (!seg->segid_preskip) return;
933*77c1e3ccSAndroid Build Coastguard Worker } else {
934*77c1e3ccSAndroid Build Coastguard Worker if (seg->segid_preskip) return;
935*77c1e3ccSAndroid Build Coastguard Worker if (skip) {
936*77c1e3ccSAndroid Build Coastguard Worker write_segment_id(cpi, xd, mbmi, w, seg, segp, 1);
937*77c1e3ccSAndroid Build Coastguard Worker if (seg->temporal_update) mbmi->seg_id_predicted = 0;
938*77c1e3ccSAndroid Build Coastguard Worker return;
939*77c1e3ccSAndroid Build Coastguard Worker }
940*77c1e3ccSAndroid Build Coastguard Worker }
941*77c1e3ccSAndroid Build Coastguard Worker if (seg->temporal_update) {
942*77c1e3ccSAndroid Build Coastguard Worker const int pred_flag = mbmi->seg_id_predicted;
943*77c1e3ccSAndroid Build Coastguard Worker aom_cdf_prob *pred_cdf = av1_get_pred_cdf_seg_id(segp, xd);
944*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, pred_flag, pred_cdf, 2);
945*77c1e3ccSAndroid Build Coastguard Worker if (!pred_flag) {
946*77c1e3ccSAndroid Build Coastguard Worker write_segment_id(cpi, xd, mbmi, w, seg, segp, 0);
947*77c1e3ccSAndroid Build Coastguard Worker }
948*77c1e3ccSAndroid Build Coastguard Worker if (pred_flag) {
949*77c1e3ccSAndroid Build Coastguard Worker set_spatial_segment_id(&cm->mi_params, cm->cur_frame->seg_map,
950*77c1e3ccSAndroid Build Coastguard Worker mbmi->bsize, mi_row, mi_col, mbmi->segment_id);
951*77c1e3ccSAndroid Build Coastguard Worker }
952*77c1e3ccSAndroid Build Coastguard Worker } else {
953*77c1e3ccSAndroid Build Coastguard Worker write_segment_id(cpi, xd, mbmi, w, seg, segp, 0);
954*77c1e3ccSAndroid Build Coastguard Worker }
955*77c1e3ccSAndroid Build Coastguard Worker }
956*77c1e3ccSAndroid Build Coastguard Worker }
957*77c1e3ccSAndroid Build Coastguard Worker
958*77c1e3ccSAndroid Build Coastguard Worker // If delta q is present, writes delta_q index.
959*77c1e3ccSAndroid Build Coastguard Worker // Also writes delta_q loop filter levels, if present.
write_delta_q_params(AV1_COMMON * const cm,MACROBLOCKD * const xd,int skip,aom_writer * w)960*77c1e3ccSAndroid Build Coastguard Worker static inline void write_delta_q_params(AV1_COMMON *const cm,
961*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *const xd, int skip,
962*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w) {
963*77c1e3ccSAndroid Build Coastguard Worker const DeltaQInfo *const delta_q_info = &cm->delta_q_info;
964*77c1e3ccSAndroid Build Coastguard Worker
965*77c1e3ccSAndroid Build Coastguard Worker if (delta_q_info->delta_q_present_flag) {
966*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *const mbmi = xd->mi[0];
967*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE bsize = mbmi->bsize;
968*77c1e3ccSAndroid Build Coastguard Worker const int super_block_upper_left =
969*77c1e3ccSAndroid Build Coastguard Worker ((xd->mi_row & (cm->seq_params->mib_size - 1)) == 0) &&
970*77c1e3ccSAndroid Build Coastguard Worker ((xd->mi_col & (cm->seq_params->mib_size - 1)) == 0);
971*77c1e3ccSAndroid Build Coastguard Worker
972*77c1e3ccSAndroid Build Coastguard Worker if ((bsize != cm->seq_params->sb_size || skip == 0) &&
973*77c1e3ccSAndroid Build Coastguard Worker super_block_upper_left) {
974*77c1e3ccSAndroid Build Coastguard Worker assert(mbmi->current_qindex > 0);
975*77c1e3ccSAndroid Build Coastguard Worker const int reduced_delta_qindex =
976*77c1e3ccSAndroid Build Coastguard Worker (mbmi->current_qindex - xd->current_base_qindex) /
977*77c1e3ccSAndroid Build Coastguard Worker delta_q_info->delta_q_res;
978*77c1e3ccSAndroid Build Coastguard Worker write_delta_qindex(xd, reduced_delta_qindex, w);
979*77c1e3ccSAndroid Build Coastguard Worker xd->current_base_qindex = mbmi->current_qindex;
980*77c1e3ccSAndroid Build Coastguard Worker if (delta_q_info->delta_lf_present_flag) {
981*77c1e3ccSAndroid Build Coastguard Worker if (delta_q_info->delta_lf_multi) {
982*77c1e3ccSAndroid Build Coastguard Worker const int frame_lf_count =
983*77c1e3ccSAndroid Build Coastguard Worker av1_num_planes(cm) > 1 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2;
984*77c1e3ccSAndroid Build Coastguard Worker for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id) {
985*77c1e3ccSAndroid Build Coastguard Worker int reduced_delta_lflevel =
986*77c1e3ccSAndroid Build Coastguard Worker (mbmi->delta_lf[lf_id] - xd->delta_lf[lf_id]) /
987*77c1e3ccSAndroid Build Coastguard Worker delta_q_info->delta_lf_res;
988*77c1e3ccSAndroid Build Coastguard Worker write_delta_lflevel(cm, xd, lf_id, reduced_delta_lflevel, 1, w);
989*77c1e3ccSAndroid Build Coastguard Worker xd->delta_lf[lf_id] = mbmi->delta_lf[lf_id];
990*77c1e3ccSAndroid Build Coastguard Worker }
991*77c1e3ccSAndroid Build Coastguard Worker } else {
992*77c1e3ccSAndroid Build Coastguard Worker int reduced_delta_lflevel =
993*77c1e3ccSAndroid Build Coastguard Worker (mbmi->delta_lf_from_base - xd->delta_lf_from_base) /
994*77c1e3ccSAndroid Build Coastguard Worker delta_q_info->delta_lf_res;
995*77c1e3ccSAndroid Build Coastguard Worker write_delta_lflevel(cm, xd, -1, reduced_delta_lflevel, 0, w);
996*77c1e3ccSAndroid Build Coastguard Worker xd->delta_lf_from_base = mbmi->delta_lf_from_base;
997*77c1e3ccSAndroid Build Coastguard Worker }
998*77c1e3ccSAndroid Build Coastguard Worker }
999*77c1e3ccSAndroid Build Coastguard Worker }
1000*77c1e3ccSAndroid Build Coastguard Worker }
1001*77c1e3ccSAndroid Build Coastguard Worker }
1002*77c1e3ccSAndroid Build Coastguard Worker
write_intra_prediction_modes(const AV1_COMMON * cm,MACROBLOCKD * const xd,int is_keyframe,aom_writer * w)1003*77c1e3ccSAndroid Build Coastguard Worker static inline void write_intra_prediction_modes(const AV1_COMMON *cm,
1004*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *const xd,
1005*77c1e3ccSAndroid Build Coastguard Worker int is_keyframe,
1006*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w) {
1007*77c1e3ccSAndroid Build Coastguard Worker FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1008*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *const mbmi = xd->mi[0];
1009*77c1e3ccSAndroid Build Coastguard Worker const PREDICTION_MODE mode = mbmi->mode;
1010*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE bsize = mbmi->bsize;
1011*77c1e3ccSAndroid Build Coastguard Worker
1012*77c1e3ccSAndroid Build Coastguard Worker // Y mode.
1013*77c1e3ccSAndroid Build Coastguard Worker if (is_keyframe) {
1014*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *const above_mi = xd->above_mbmi;
1015*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *const left_mi = xd->left_mbmi;
1016*77c1e3ccSAndroid Build Coastguard Worker write_intra_y_mode_kf(ec_ctx, mbmi, above_mi, left_mi, mode, w);
1017*77c1e3ccSAndroid Build Coastguard Worker } else {
1018*77c1e3ccSAndroid Build Coastguard Worker write_intra_y_mode_nonkf(ec_ctx, bsize, mode, w);
1019*77c1e3ccSAndroid Build Coastguard Worker }
1020*77c1e3ccSAndroid Build Coastguard Worker
1021*77c1e3ccSAndroid Build Coastguard Worker // Y angle delta.
1022*77c1e3ccSAndroid Build Coastguard Worker const int use_angle_delta = av1_use_angle_delta(bsize);
1023*77c1e3ccSAndroid Build Coastguard Worker if (use_angle_delta && av1_is_directional_mode(mode)) {
1024*77c1e3ccSAndroid Build Coastguard Worker write_angle_delta(w, mbmi->angle_delta[PLANE_TYPE_Y],
1025*77c1e3ccSAndroid Build Coastguard Worker ec_ctx->angle_delta_cdf[mode - V_PRED]);
1026*77c1e3ccSAndroid Build Coastguard Worker }
1027*77c1e3ccSAndroid Build Coastguard Worker
1028*77c1e3ccSAndroid Build Coastguard Worker // UV mode and UV angle delta.
1029*77c1e3ccSAndroid Build Coastguard Worker if (!cm->seq_params->monochrome && xd->is_chroma_ref) {
1030*77c1e3ccSAndroid Build Coastguard Worker const UV_PREDICTION_MODE uv_mode = mbmi->uv_mode;
1031*77c1e3ccSAndroid Build Coastguard Worker write_intra_uv_mode(ec_ctx, uv_mode, mode, is_cfl_allowed(xd), w);
1032*77c1e3ccSAndroid Build Coastguard Worker if (uv_mode == UV_CFL_PRED)
1033*77c1e3ccSAndroid Build Coastguard Worker write_cfl_alphas(ec_ctx, mbmi->cfl_alpha_idx, mbmi->cfl_alpha_signs, w);
1034*77c1e3ccSAndroid Build Coastguard Worker const PREDICTION_MODE intra_mode = get_uv_mode(uv_mode);
1035*77c1e3ccSAndroid Build Coastguard Worker if (use_angle_delta && av1_is_directional_mode(intra_mode)) {
1036*77c1e3ccSAndroid Build Coastguard Worker write_angle_delta(w, mbmi->angle_delta[PLANE_TYPE_UV],
1037*77c1e3ccSAndroid Build Coastguard Worker ec_ctx->angle_delta_cdf[intra_mode - V_PRED]);
1038*77c1e3ccSAndroid Build Coastguard Worker }
1039*77c1e3ccSAndroid Build Coastguard Worker }
1040*77c1e3ccSAndroid Build Coastguard Worker
1041*77c1e3ccSAndroid Build Coastguard Worker // Palette.
1042*77c1e3ccSAndroid Build Coastguard Worker if (av1_allow_palette(cm->features.allow_screen_content_tools, bsize)) {
1043*77c1e3ccSAndroid Build Coastguard Worker write_palette_mode_info(cm, xd, mbmi, w);
1044*77c1e3ccSAndroid Build Coastguard Worker }
1045*77c1e3ccSAndroid Build Coastguard Worker
1046*77c1e3ccSAndroid Build Coastguard Worker // Filter intra.
1047*77c1e3ccSAndroid Build Coastguard Worker write_filter_intra_mode_info(cm, xd, mbmi, w);
1048*77c1e3ccSAndroid Build Coastguard Worker }
1049*77c1e3ccSAndroid Build Coastguard Worker
mode_context_analyzer(const int16_t mode_context,const MV_REFERENCE_FRAME * const rf)1050*77c1e3ccSAndroid Build Coastguard Worker static inline int16_t mode_context_analyzer(
1051*77c1e3ccSAndroid Build Coastguard Worker const int16_t mode_context, const MV_REFERENCE_FRAME *const rf) {
1052*77c1e3ccSAndroid Build Coastguard Worker if (rf[1] <= INTRA_FRAME) return mode_context;
1053*77c1e3ccSAndroid Build Coastguard Worker
1054*77c1e3ccSAndroid Build Coastguard Worker const int16_t newmv_ctx = mode_context & NEWMV_CTX_MASK;
1055*77c1e3ccSAndroid Build Coastguard Worker const int16_t refmv_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK;
1056*77c1e3ccSAndroid Build Coastguard Worker
1057*77c1e3ccSAndroid Build Coastguard Worker const int16_t comp_ctx = compound_mode_ctx_map[refmv_ctx >> 1][AOMMIN(
1058*77c1e3ccSAndroid Build Coastguard Worker newmv_ctx, COMP_NEWMV_CTXS - 1)];
1059*77c1e3ccSAndroid Build Coastguard Worker return comp_ctx;
1060*77c1e3ccSAndroid Build Coastguard Worker }
1061*77c1e3ccSAndroid Build Coastguard Worker
get_ref_mv_from_stack(int ref_idx,const MV_REFERENCE_FRAME * ref_frame,int ref_mv_idx,const MB_MODE_INFO_EXT_FRAME * mbmi_ext_frame)1062*77c1e3ccSAndroid Build Coastguard Worker static inline int_mv get_ref_mv_from_stack(
1063*77c1e3ccSAndroid Build Coastguard Worker int ref_idx, const MV_REFERENCE_FRAME *ref_frame, int ref_mv_idx,
1064*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO_EXT_FRAME *mbmi_ext_frame) {
1065*77c1e3ccSAndroid Build Coastguard Worker const int8_t ref_frame_type = av1_ref_frame_type(ref_frame);
1066*77c1e3ccSAndroid Build Coastguard Worker const CANDIDATE_MV *curr_ref_mv_stack = mbmi_ext_frame->ref_mv_stack;
1067*77c1e3ccSAndroid Build Coastguard Worker
1068*77c1e3ccSAndroid Build Coastguard Worker if (ref_frame[1] > INTRA_FRAME) {
1069*77c1e3ccSAndroid Build Coastguard Worker assert(ref_idx == 0 || ref_idx == 1);
1070*77c1e3ccSAndroid Build Coastguard Worker return ref_idx ? curr_ref_mv_stack[ref_mv_idx].comp_mv
1071*77c1e3ccSAndroid Build Coastguard Worker : curr_ref_mv_stack[ref_mv_idx].this_mv;
1072*77c1e3ccSAndroid Build Coastguard Worker }
1073*77c1e3ccSAndroid Build Coastguard Worker
1074*77c1e3ccSAndroid Build Coastguard Worker assert(ref_idx == 0);
1075*77c1e3ccSAndroid Build Coastguard Worker return ref_mv_idx < mbmi_ext_frame->ref_mv_count
1076*77c1e3ccSAndroid Build Coastguard Worker ? curr_ref_mv_stack[ref_mv_idx].this_mv
1077*77c1e3ccSAndroid Build Coastguard Worker : mbmi_ext_frame->global_mvs[ref_frame_type];
1078*77c1e3ccSAndroid Build Coastguard Worker }
1079*77c1e3ccSAndroid Build Coastguard Worker
get_ref_mv(const MACROBLOCK * x,int ref_idx)1080*77c1e3ccSAndroid Build Coastguard Worker static inline int_mv get_ref_mv(const MACROBLOCK *x, int ref_idx) {
1081*77c1e3ccSAndroid Build Coastguard Worker const MACROBLOCKD *xd = &x->e_mbd;
1082*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *mbmi = xd->mi[0];
1083*77c1e3ccSAndroid Build Coastguard Worker int ref_mv_idx = mbmi->ref_mv_idx;
1084*77c1e3ccSAndroid Build Coastguard Worker if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV) {
1085*77c1e3ccSAndroid Build Coastguard Worker assert(has_second_ref(mbmi));
1086*77c1e3ccSAndroid Build Coastguard Worker ref_mv_idx += 1;
1087*77c1e3ccSAndroid Build Coastguard Worker }
1088*77c1e3ccSAndroid Build Coastguard Worker return get_ref_mv_from_stack(ref_idx, mbmi->ref_frame, ref_mv_idx,
1089*77c1e3ccSAndroid Build Coastguard Worker x->mbmi_ext_frame);
1090*77c1e3ccSAndroid Build Coastguard Worker }
1091*77c1e3ccSAndroid Build Coastguard Worker
pack_inter_mode_mvs(AV1_COMP * cpi,ThreadData * const td,aom_writer * w)1092*77c1e3ccSAndroid Build Coastguard Worker static inline void pack_inter_mode_mvs(AV1_COMP *cpi, ThreadData *const td,
1093*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w) {
1094*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
1095*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCK *const x = &td->mb;
1096*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *const xd = &x->e_mbd;
1097*77c1e3ccSAndroid Build Coastguard Worker FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1098*77c1e3ccSAndroid Build Coastguard Worker const struct segmentation *const seg = &cm->seg;
1099*77c1e3ccSAndroid Build Coastguard Worker struct segmentation_probs *const segp = &ec_ctx->seg;
1100*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *const mbmi = xd->mi[0];
1101*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO_EXT_FRAME *const mbmi_ext_frame = x->mbmi_ext_frame;
1102*77c1e3ccSAndroid Build Coastguard Worker const PREDICTION_MODE mode = mbmi->mode;
1103*77c1e3ccSAndroid Build Coastguard Worker const uint8_t segment_id = mbmi->segment_id;
1104*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE bsize = mbmi->bsize;
1105*77c1e3ccSAndroid Build Coastguard Worker const int allow_hp = cm->features.allow_high_precision_mv;
1106*77c1e3ccSAndroid Build Coastguard Worker const int is_inter = is_inter_block(mbmi);
1107*77c1e3ccSAndroid Build Coastguard Worker const int is_compound = has_second_ref(mbmi);
1108*77c1e3ccSAndroid Build Coastguard Worker int ref;
1109*77c1e3ccSAndroid Build Coastguard Worker
1110*77c1e3ccSAndroid Build Coastguard Worker write_inter_segment_id(cpi, xd, w, seg, segp, 0, 1);
1111*77c1e3ccSAndroid Build Coastguard Worker
1112*77c1e3ccSAndroid Build Coastguard Worker write_skip_mode(cm, xd, segment_id, mbmi, w);
1113*77c1e3ccSAndroid Build Coastguard Worker
1114*77c1e3ccSAndroid Build Coastguard Worker assert(IMPLIES(mbmi->skip_mode, mbmi->skip_txfm));
1115*77c1e3ccSAndroid Build Coastguard Worker const int skip =
1116*77c1e3ccSAndroid Build Coastguard Worker mbmi->skip_mode ? 1 : write_skip(cm, xd, segment_id, mbmi, w);
1117*77c1e3ccSAndroid Build Coastguard Worker
1118*77c1e3ccSAndroid Build Coastguard Worker write_inter_segment_id(cpi, xd, w, seg, segp, skip, 0);
1119*77c1e3ccSAndroid Build Coastguard Worker
1120*77c1e3ccSAndroid Build Coastguard Worker write_cdef(cm, xd, w, skip);
1121*77c1e3ccSAndroid Build Coastguard Worker
1122*77c1e3ccSAndroid Build Coastguard Worker write_delta_q_params(cm, xd, skip, w);
1123*77c1e3ccSAndroid Build Coastguard Worker
1124*77c1e3ccSAndroid Build Coastguard Worker if (!mbmi->skip_mode) write_is_inter(cm, xd, mbmi->segment_id, w, is_inter);
1125*77c1e3ccSAndroid Build Coastguard Worker
1126*77c1e3ccSAndroid Build Coastguard Worker if (mbmi->skip_mode) return;
1127*77c1e3ccSAndroid Build Coastguard Worker
1128*77c1e3ccSAndroid Build Coastguard Worker if (!is_inter) {
1129*77c1e3ccSAndroid Build Coastguard Worker write_intra_prediction_modes(cm, xd, 0, w);
1130*77c1e3ccSAndroid Build Coastguard Worker } else {
1131*77c1e3ccSAndroid Build Coastguard Worker int16_t mode_ctx;
1132*77c1e3ccSAndroid Build Coastguard Worker
1133*77c1e3ccSAndroid Build Coastguard Worker av1_collect_neighbors_ref_counts(xd);
1134*77c1e3ccSAndroid Build Coastguard Worker
1135*77c1e3ccSAndroid Build Coastguard Worker write_ref_frames(cm, xd, w);
1136*77c1e3ccSAndroid Build Coastguard Worker
1137*77c1e3ccSAndroid Build Coastguard Worker mode_ctx =
1138*77c1e3ccSAndroid Build Coastguard Worker mode_context_analyzer(mbmi_ext_frame->mode_context, mbmi->ref_frame);
1139*77c1e3ccSAndroid Build Coastguard Worker
1140*77c1e3ccSAndroid Build Coastguard Worker // If segment skip is not enabled code the mode.
1141*77c1e3ccSAndroid Build Coastguard Worker if (!segfeature_active(seg, segment_id, SEG_LVL_SKIP)) {
1142*77c1e3ccSAndroid Build Coastguard Worker if (is_inter_compound_mode(mode))
1143*77c1e3ccSAndroid Build Coastguard Worker write_inter_compound_mode(xd, w, mode, mode_ctx);
1144*77c1e3ccSAndroid Build Coastguard Worker else if (is_inter_singleref_mode(mode))
1145*77c1e3ccSAndroid Build Coastguard Worker write_inter_mode(w, mode, ec_ctx, mode_ctx);
1146*77c1e3ccSAndroid Build Coastguard Worker
1147*77c1e3ccSAndroid Build Coastguard Worker if (mode == NEWMV || mode == NEW_NEWMV || have_nearmv_in_inter_mode(mode))
1148*77c1e3ccSAndroid Build Coastguard Worker write_drl_idx(ec_ctx, mbmi, mbmi_ext_frame, w);
1149*77c1e3ccSAndroid Build Coastguard Worker else
1150*77c1e3ccSAndroid Build Coastguard Worker assert(mbmi->ref_mv_idx == 0);
1151*77c1e3ccSAndroid Build Coastguard Worker }
1152*77c1e3ccSAndroid Build Coastguard Worker
1153*77c1e3ccSAndroid Build Coastguard Worker if (mode == NEWMV || mode == NEW_NEWMV) {
1154*77c1e3ccSAndroid Build Coastguard Worker for (ref = 0; ref < 1 + is_compound; ++ref) {
1155*77c1e3ccSAndroid Build Coastguard Worker nmv_context *nmvc = &ec_ctx->nmvc;
1156*77c1e3ccSAndroid Build Coastguard Worker const int_mv ref_mv = get_ref_mv(x, ref);
1157*77c1e3ccSAndroid Build Coastguard Worker av1_encode_mv(cpi, w, td, &mbmi->mv[ref].as_mv, &ref_mv.as_mv, nmvc,
1158*77c1e3ccSAndroid Build Coastguard Worker allow_hp);
1159*77c1e3ccSAndroid Build Coastguard Worker }
1160*77c1e3ccSAndroid Build Coastguard Worker } else if (mode == NEAREST_NEWMV || mode == NEAR_NEWMV) {
1161*77c1e3ccSAndroid Build Coastguard Worker nmv_context *nmvc = &ec_ctx->nmvc;
1162*77c1e3ccSAndroid Build Coastguard Worker const int_mv ref_mv = get_ref_mv(x, 1);
1163*77c1e3ccSAndroid Build Coastguard Worker av1_encode_mv(cpi, w, td, &mbmi->mv[1].as_mv, &ref_mv.as_mv, nmvc,
1164*77c1e3ccSAndroid Build Coastguard Worker allow_hp);
1165*77c1e3ccSAndroid Build Coastguard Worker } else if (mode == NEW_NEARESTMV || mode == NEW_NEARMV) {
1166*77c1e3ccSAndroid Build Coastguard Worker nmv_context *nmvc = &ec_ctx->nmvc;
1167*77c1e3ccSAndroid Build Coastguard Worker const int_mv ref_mv = get_ref_mv(x, 0);
1168*77c1e3ccSAndroid Build Coastguard Worker av1_encode_mv(cpi, w, td, &mbmi->mv[0].as_mv, &ref_mv.as_mv, nmvc,
1169*77c1e3ccSAndroid Build Coastguard Worker allow_hp);
1170*77c1e3ccSAndroid Build Coastguard Worker }
1171*77c1e3ccSAndroid Build Coastguard Worker
1172*77c1e3ccSAndroid Build Coastguard Worker if (cpi->common.current_frame.reference_mode != COMPOUND_REFERENCE &&
1173*77c1e3ccSAndroid Build Coastguard Worker cpi->common.seq_params->enable_interintra_compound &&
1174*77c1e3ccSAndroid Build Coastguard Worker is_interintra_allowed(mbmi)) {
1175*77c1e3ccSAndroid Build Coastguard Worker const int interintra = mbmi->ref_frame[1] == INTRA_FRAME;
1176*77c1e3ccSAndroid Build Coastguard Worker const int bsize_group = size_group_lookup[bsize];
1177*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, interintra, ec_ctx->interintra_cdf[bsize_group], 2);
1178*77c1e3ccSAndroid Build Coastguard Worker if (interintra) {
1179*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, mbmi->interintra_mode,
1180*77c1e3ccSAndroid Build Coastguard Worker ec_ctx->interintra_mode_cdf[bsize_group],
1181*77c1e3ccSAndroid Build Coastguard Worker INTERINTRA_MODES);
1182*77c1e3ccSAndroid Build Coastguard Worker if (av1_is_wedge_used(bsize)) {
1183*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, mbmi->use_wedge_interintra,
1184*77c1e3ccSAndroid Build Coastguard Worker ec_ctx->wedge_interintra_cdf[bsize], 2);
1185*77c1e3ccSAndroid Build Coastguard Worker if (mbmi->use_wedge_interintra) {
1186*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, mbmi->interintra_wedge_index,
1187*77c1e3ccSAndroid Build Coastguard Worker ec_ctx->wedge_idx_cdf[bsize], MAX_WEDGE_TYPES);
1188*77c1e3ccSAndroid Build Coastguard Worker }
1189*77c1e3ccSAndroid Build Coastguard Worker }
1190*77c1e3ccSAndroid Build Coastguard Worker }
1191*77c1e3ccSAndroid Build Coastguard Worker }
1192*77c1e3ccSAndroid Build Coastguard Worker
1193*77c1e3ccSAndroid Build Coastguard Worker if (mbmi->ref_frame[1] != INTRA_FRAME) write_motion_mode(cm, xd, mbmi, w);
1194*77c1e3ccSAndroid Build Coastguard Worker
1195*77c1e3ccSAndroid Build Coastguard Worker // First write idx to indicate current compound inter prediction mode group
1196*77c1e3ccSAndroid Build Coastguard Worker // Group A (0): dist_wtd_comp, compound_average
1197*77c1e3ccSAndroid Build Coastguard Worker // Group B (1): interintra, compound_diffwtd, wedge
1198*77c1e3ccSAndroid Build Coastguard Worker if (has_second_ref(mbmi)) {
1199*77c1e3ccSAndroid Build Coastguard Worker const int masked_compound_used = is_any_masked_compound_used(bsize) &&
1200*77c1e3ccSAndroid Build Coastguard Worker cm->seq_params->enable_masked_compound;
1201*77c1e3ccSAndroid Build Coastguard Worker
1202*77c1e3ccSAndroid Build Coastguard Worker if (masked_compound_used) {
1203*77c1e3ccSAndroid Build Coastguard Worker const int ctx_comp_group_idx = get_comp_group_idx_context(xd);
1204*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, mbmi->comp_group_idx,
1205*77c1e3ccSAndroid Build Coastguard Worker ec_ctx->comp_group_idx_cdf[ctx_comp_group_idx], 2);
1206*77c1e3ccSAndroid Build Coastguard Worker } else {
1207*77c1e3ccSAndroid Build Coastguard Worker assert(mbmi->comp_group_idx == 0);
1208*77c1e3ccSAndroid Build Coastguard Worker }
1209*77c1e3ccSAndroid Build Coastguard Worker
1210*77c1e3ccSAndroid Build Coastguard Worker if (mbmi->comp_group_idx == 0) {
1211*77c1e3ccSAndroid Build Coastguard Worker if (mbmi->compound_idx)
1212*77c1e3ccSAndroid Build Coastguard Worker assert(mbmi->interinter_comp.type == COMPOUND_AVERAGE);
1213*77c1e3ccSAndroid Build Coastguard Worker
1214*77c1e3ccSAndroid Build Coastguard Worker if (cm->seq_params->order_hint_info.enable_dist_wtd_comp) {
1215*77c1e3ccSAndroid Build Coastguard Worker const int comp_index_ctx = get_comp_index_context(cm, xd);
1216*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, mbmi->compound_idx,
1217*77c1e3ccSAndroid Build Coastguard Worker ec_ctx->compound_index_cdf[comp_index_ctx], 2);
1218*77c1e3ccSAndroid Build Coastguard Worker } else {
1219*77c1e3ccSAndroid Build Coastguard Worker assert(mbmi->compound_idx == 1);
1220*77c1e3ccSAndroid Build Coastguard Worker }
1221*77c1e3ccSAndroid Build Coastguard Worker } else {
1222*77c1e3ccSAndroid Build Coastguard Worker assert(cpi->common.current_frame.reference_mode != SINGLE_REFERENCE &&
1223*77c1e3ccSAndroid Build Coastguard Worker is_inter_compound_mode(mbmi->mode) &&
1224*77c1e3ccSAndroid Build Coastguard Worker mbmi->motion_mode == SIMPLE_TRANSLATION);
1225*77c1e3ccSAndroid Build Coastguard Worker assert(masked_compound_used);
1226*77c1e3ccSAndroid Build Coastguard Worker // compound_diffwtd, wedge
1227*77c1e3ccSAndroid Build Coastguard Worker assert(mbmi->interinter_comp.type == COMPOUND_WEDGE ||
1228*77c1e3ccSAndroid Build Coastguard Worker mbmi->interinter_comp.type == COMPOUND_DIFFWTD);
1229*77c1e3ccSAndroid Build Coastguard Worker
1230*77c1e3ccSAndroid Build Coastguard Worker if (is_interinter_compound_used(COMPOUND_WEDGE, bsize))
1231*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, mbmi->interinter_comp.type - COMPOUND_WEDGE,
1232*77c1e3ccSAndroid Build Coastguard Worker ec_ctx->compound_type_cdf[bsize],
1233*77c1e3ccSAndroid Build Coastguard Worker MASKED_COMPOUND_TYPES);
1234*77c1e3ccSAndroid Build Coastguard Worker
1235*77c1e3ccSAndroid Build Coastguard Worker if (mbmi->interinter_comp.type == COMPOUND_WEDGE) {
1236*77c1e3ccSAndroid Build Coastguard Worker assert(is_interinter_compound_used(COMPOUND_WEDGE, bsize));
1237*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, mbmi->interinter_comp.wedge_index,
1238*77c1e3ccSAndroid Build Coastguard Worker ec_ctx->wedge_idx_cdf[bsize], MAX_WEDGE_TYPES);
1239*77c1e3ccSAndroid Build Coastguard Worker aom_write_bit(w, mbmi->interinter_comp.wedge_sign);
1240*77c1e3ccSAndroid Build Coastguard Worker } else {
1241*77c1e3ccSAndroid Build Coastguard Worker assert(mbmi->interinter_comp.type == COMPOUND_DIFFWTD);
1242*77c1e3ccSAndroid Build Coastguard Worker aom_write_literal(w, mbmi->interinter_comp.mask_type,
1243*77c1e3ccSAndroid Build Coastguard Worker MAX_DIFFWTD_MASK_BITS);
1244*77c1e3ccSAndroid Build Coastguard Worker }
1245*77c1e3ccSAndroid Build Coastguard Worker }
1246*77c1e3ccSAndroid Build Coastguard Worker }
1247*77c1e3ccSAndroid Build Coastguard Worker write_mb_interp_filter(cm, td, w);
1248*77c1e3ccSAndroid Build Coastguard Worker }
1249*77c1e3ccSAndroid Build Coastguard Worker }
1250*77c1e3ccSAndroid Build Coastguard Worker
write_intrabc_info(MACROBLOCKD * xd,const MB_MODE_INFO_EXT_FRAME * mbmi_ext_frame,aom_writer * w)1251*77c1e3ccSAndroid Build Coastguard Worker static inline void write_intrabc_info(
1252*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *xd, const MB_MODE_INFO_EXT_FRAME *mbmi_ext_frame,
1253*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w) {
1254*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *const mbmi = xd->mi[0];
1255*77c1e3ccSAndroid Build Coastguard Worker int use_intrabc = is_intrabc_block(mbmi);
1256*77c1e3ccSAndroid Build Coastguard Worker FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1257*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, use_intrabc, ec_ctx->intrabc_cdf, 2);
1258*77c1e3ccSAndroid Build Coastguard Worker if (use_intrabc) {
1259*77c1e3ccSAndroid Build Coastguard Worker assert(mbmi->mode == DC_PRED);
1260*77c1e3ccSAndroid Build Coastguard Worker assert(mbmi->uv_mode == UV_DC_PRED);
1261*77c1e3ccSAndroid Build Coastguard Worker assert(mbmi->motion_mode == SIMPLE_TRANSLATION);
1262*77c1e3ccSAndroid Build Coastguard Worker int_mv dv_ref = mbmi_ext_frame->ref_mv_stack[0].this_mv;
1263*77c1e3ccSAndroid Build Coastguard Worker av1_encode_dv(w, &mbmi->mv[0].as_mv, &dv_ref.as_mv, &ec_ctx->ndvc);
1264*77c1e3ccSAndroid Build Coastguard Worker }
1265*77c1e3ccSAndroid Build Coastguard Worker }
1266*77c1e3ccSAndroid Build Coastguard Worker
write_mb_modes_kf(AV1_COMP * cpi,MACROBLOCKD * xd,const MB_MODE_INFO_EXT_FRAME * mbmi_ext_frame,aom_writer * w)1267*77c1e3ccSAndroid Build Coastguard Worker static inline void write_mb_modes_kf(
1268*77c1e3ccSAndroid Build Coastguard Worker AV1_COMP *cpi, MACROBLOCKD *xd,
1269*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO_EXT_FRAME *mbmi_ext_frame, aom_writer *w) {
1270*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
1271*77c1e3ccSAndroid Build Coastguard Worker FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1272*77c1e3ccSAndroid Build Coastguard Worker const struct segmentation *const seg = &cm->seg;
1273*77c1e3ccSAndroid Build Coastguard Worker struct segmentation_probs *const segp = &ec_ctx->seg;
1274*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *const mbmi = xd->mi[0];
1275*77c1e3ccSAndroid Build Coastguard Worker
1276*77c1e3ccSAndroid Build Coastguard Worker if (seg->segid_preskip && seg->update_map)
1277*77c1e3ccSAndroid Build Coastguard Worker write_segment_id(cpi, xd, mbmi, w, seg, segp, 0);
1278*77c1e3ccSAndroid Build Coastguard Worker
1279*77c1e3ccSAndroid Build Coastguard Worker const int skip = write_skip(cm, xd, mbmi->segment_id, mbmi, w);
1280*77c1e3ccSAndroid Build Coastguard Worker
1281*77c1e3ccSAndroid Build Coastguard Worker if (!seg->segid_preskip && seg->update_map)
1282*77c1e3ccSAndroid Build Coastguard Worker write_segment_id(cpi, xd, mbmi, w, seg, segp, skip);
1283*77c1e3ccSAndroid Build Coastguard Worker
1284*77c1e3ccSAndroid Build Coastguard Worker write_cdef(cm, xd, w, skip);
1285*77c1e3ccSAndroid Build Coastguard Worker
1286*77c1e3ccSAndroid Build Coastguard Worker write_delta_q_params(cm, xd, skip, w);
1287*77c1e3ccSAndroid Build Coastguard Worker
1288*77c1e3ccSAndroid Build Coastguard Worker if (av1_allow_intrabc(cm)) {
1289*77c1e3ccSAndroid Build Coastguard Worker write_intrabc_info(xd, mbmi_ext_frame, w);
1290*77c1e3ccSAndroid Build Coastguard Worker if (is_intrabc_block(mbmi)) return;
1291*77c1e3ccSAndroid Build Coastguard Worker }
1292*77c1e3ccSAndroid Build Coastguard Worker
1293*77c1e3ccSAndroid Build Coastguard Worker write_intra_prediction_modes(cm, xd, 1, w);
1294*77c1e3ccSAndroid Build Coastguard Worker }
1295*77c1e3ccSAndroid Build Coastguard Worker
1296*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_RD_DEBUG
dump_mode_info(MB_MODE_INFO * mi)1297*77c1e3ccSAndroid Build Coastguard Worker static inline void dump_mode_info(MB_MODE_INFO *mi) {
1298*77c1e3ccSAndroid Build Coastguard Worker printf("\nmi->mi_row == %d\n", mi->mi_row);
1299*77c1e3ccSAndroid Build Coastguard Worker printf("&& mi->mi_col == %d\n", mi->mi_col);
1300*77c1e3ccSAndroid Build Coastguard Worker printf("&& mi->bsize == %d\n", mi->bsize);
1301*77c1e3ccSAndroid Build Coastguard Worker printf("&& mi->tx_size == %d\n", mi->tx_size);
1302*77c1e3ccSAndroid Build Coastguard Worker printf("&& mi->mode == %d\n", mi->mode);
1303*77c1e3ccSAndroid Build Coastguard Worker }
1304*77c1e3ccSAndroid Build Coastguard Worker
rd_token_stats_mismatch(RD_STATS * rd_stats,TOKEN_STATS * token_stats,int plane)1305*77c1e3ccSAndroid Build Coastguard Worker static int rd_token_stats_mismatch(RD_STATS *rd_stats, TOKEN_STATS *token_stats,
1306*77c1e3ccSAndroid Build Coastguard Worker int plane) {
1307*77c1e3ccSAndroid Build Coastguard Worker if (rd_stats->txb_coeff_cost[plane] != token_stats->cost) {
1308*77c1e3ccSAndroid Build Coastguard Worker printf("\nplane %d rd_stats->txb_coeff_cost %d token_stats->cost %d\n",
1309*77c1e3ccSAndroid Build Coastguard Worker plane, rd_stats->txb_coeff_cost[plane], token_stats->cost);
1310*77c1e3ccSAndroid Build Coastguard Worker return 1;
1311*77c1e3ccSAndroid Build Coastguard Worker }
1312*77c1e3ccSAndroid Build Coastguard Worker return 0;
1313*77c1e3ccSAndroid Build Coastguard Worker }
1314*77c1e3ccSAndroid Build Coastguard Worker #endif
1315*77c1e3ccSAndroid Build Coastguard Worker
1316*77c1e3ccSAndroid Build Coastguard Worker #if ENC_MISMATCH_DEBUG
enc_dump_logs(const AV1_COMMON * const cm,const MBMIExtFrameBufferInfo * const mbmi_ext_info,int mi_row,int mi_col)1317*77c1e3ccSAndroid Build Coastguard Worker static inline void enc_dump_logs(
1318*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm,
1319*77c1e3ccSAndroid Build Coastguard Worker const MBMIExtFrameBufferInfo *const mbmi_ext_info, int mi_row, int mi_col) {
1320*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *const mbmi = *(
1321*77c1e3ccSAndroid Build Coastguard Worker cm->mi_params.mi_grid_base + (mi_row * cm->mi_params.mi_stride + mi_col));
1322*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO_EXT_FRAME *const mbmi_ext_frame =
1323*77c1e3ccSAndroid Build Coastguard Worker mbmi_ext_info->frame_base + get_mi_ext_idx(mi_row, mi_col,
1324*77c1e3ccSAndroid Build Coastguard Worker cm->mi_params.mi_alloc_bsize,
1325*77c1e3ccSAndroid Build Coastguard Worker mbmi_ext_info->stride);
1326*77c1e3ccSAndroid Build Coastguard Worker if (is_inter_block(mbmi)) {
1327*77c1e3ccSAndroid Build Coastguard Worker #define FRAME_TO_CHECK 11
1328*77c1e3ccSAndroid Build Coastguard Worker if (cm->current_frame.frame_number == FRAME_TO_CHECK &&
1329*77c1e3ccSAndroid Build Coastguard Worker cm->show_frame == 1) {
1330*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE bsize = mbmi->bsize;
1331*77c1e3ccSAndroid Build Coastguard Worker
1332*77c1e3ccSAndroid Build Coastguard Worker int_mv mv[2] = { 0 };
1333*77c1e3ccSAndroid Build Coastguard Worker const int is_comp_ref = has_second_ref(mbmi);
1334*77c1e3ccSAndroid Build Coastguard Worker
1335*77c1e3ccSAndroid Build Coastguard Worker for (int ref = 0; ref < 1 + is_comp_ref; ++ref)
1336*77c1e3ccSAndroid Build Coastguard Worker mv[ref].as_mv = mbmi->mv[ref].as_mv;
1337*77c1e3ccSAndroid Build Coastguard Worker
1338*77c1e3ccSAndroid Build Coastguard Worker if (!is_comp_ref) {
1339*77c1e3ccSAndroid Build Coastguard Worker mv[1].as_int = 0;
1340*77c1e3ccSAndroid Build Coastguard Worker }
1341*77c1e3ccSAndroid Build Coastguard Worker
1342*77c1e3ccSAndroid Build Coastguard Worker const int16_t mode_ctx =
1343*77c1e3ccSAndroid Build Coastguard Worker is_comp_ref ? 0
1344*77c1e3ccSAndroid Build Coastguard Worker : mode_context_analyzer(mbmi_ext_frame->mode_context,
1345*77c1e3ccSAndroid Build Coastguard Worker mbmi->ref_frame);
1346*77c1e3ccSAndroid Build Coastguard Worker
1347*77c1e3ccSAndroid Build Coastguard Worker const int16_t newmv_ctx = mode_ctx & NEWMV_CTX_MASK;
1348*77c1e3ccSAndroid Build Coastguard Worker int16_t zeromv_ctx = -1;
1349*77c1e3ccSAndroid Build Coastguard Worker int16_t refmv_ctx = -1;
1350*77c1e3ccSAndroid Build Coastguard Worker
1351*77c1e3ccSAndroid Build Coastguard Worker if (mbmi->mode != NEWMV) {
1352*77c1e3ccSAndroid Build Coastguard Worker zeromv_ctx = (mode_ctx >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
1353*77c1e3ccSAndroid Build Coastguard Worker if (mbmi->mode != GLOBALMV)
1354*77c1e3ccSAndroid Build Coastguard Worker refmv_ctx = (mode_ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
1355*77c1e3ccSAndroid Build Coastguard Worker }
1356*77c1e3ccSAndroid Build Coastguard Worker
1357*77c1e3ccSAndroid Build Coastguard Worker printf(
1358*77c1e3ccSAndroid Build Coastguard Worker "=== ENCODER ===: "
1359*77c1e3ccSAndroid Build Coastguard Worker "Frame=%d, (mi_row,mi_col)=(%d,%d), skip_mode=%d, mode=%d, bsize=%d, "
1360*77c1e3ccSAndroid Build Coastguard Worker "show_frame=%d, mv[0]=(%d,%d), mv[1]=(%d,%d), ref[0]=%d, "
1361*77c1e3ccSAndroid Build Coastguard Worker "ref[1]=%d, motion_mode=%d, mode_ctx=%d, "
1362*77c1e3ccSAndroid Build Coastguard Worker "newmv_ctx=%d, zeromv_ctx=%d, refmv_ctx=%d, tx_size=%d\n",
1363*77c1e3ccSAndroid Build Coastguard Worker cm->current_frame.frame_number, mi_row, mi_col, mbmi->skip_mode,
1364*77c1e3ccSAndroid Build Coastguard Worker mbmi->mode, bsize, cm->show_frame, mv[0].as_mv.row, mv[0].as_mv.col,
1365*77c1e3ccSAndroid Build Coastguard Worker mv[1].as_mv.row, mv[1].as_mv.col, mbmi->ref_frame[0],
1366*77c1e3ccSAndroid Build Coastguard Worker mbmi->ref_frame[1], mbmi->motion_mode, mode_ctx, newmv_ctx,
1367*77c1e3ccSAndroid Build Coastguard Worker zeromv_ctx, refmv_ctx, mbmi->tx_size);
1368*77c1e3ccSAndroid Build Coastguard Worker }
1369*77c1e3ccSAndroid Build Coastguard Worker }
1370*77c1e3ccSAndroid Build Coastguard Worker }
1371*77c1e3ccSAndroid Build Coastguard Worker #endif // ENC_MISMATCH_DEBUG
1372*77c1e3ccSAndroid Build Coastguard Worker
write_mbmi_b(AV1_COMP * cpi,ThreadData * const td,aom_writer * w)1373*77c1e3ccSAndroid Build Coastguard Worker static inline void write_mbmi_b(AV1_COMP *cpi, ThreadData *const td,
1374*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w) {
1375*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
1376*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *const xd = &td->mb.e_mbd;
1377*77c1e3ccSAndroid Build Coastguard Worker MB_MODE_INFO *m = xd->mi[0];
1378*77c1e3ccSAndroid Build Coastguard Worker
1379*77c1e3ccSAndroid Build Coastguard Worker if (frame_is_intra_only(cm)) {
1380*77c1e3ccSAndroid Build Coastguard Worker write_mb_modes_kf(cpi, xd, td->mb.mbmi_ext_frame, w);
1381*77c1e3ccSAndroid Build Coastguard Worker } else {
1382*77c1e3ccSAndroid Build Coastguard Worker // has_subpel_mv_component needs the ref frame buffers set up to look
1383*77c1e3ccSAndroid Build Coastguard Worker // up if they are scaled. has_subpel_mv_component is in turn needed by
1384*77c1e3ccSAndroid Build Coastguard Worker // write_switchable_interp_filter, which is called by pack_inter_mode_mvs.
1385*77c1e3ccSAndroid Build Coastguard Worker set_ref_ptrs(cm, xd, m->ref_frame[0], m->ref_frame[1]);
1386*77c1e3ccSAndroid Build Coastguard Worker
1387*77c1e3ccSAndroid Build Coastguard Worker #if ENC_MISMATCH_DEBUG
1388*77c1e3ccSAndroid Build Coastguard Worker enc_dump_logs(cm, &cpi->mbmi_ext_info, xd->mi_row, xd->mi_col);
1389*77c1e3ccSAndroid Build Coastguard Worker #endif // ENC_MISMATCH_DEBUG
1390*77c1e3ccSAndroid Build Coastguard Worker
1391*77c1e3ccSAndroid Build Coastguard Worker pack_inter_mode_mvs(cpi, td, w);
1392*77c1e3ccSAndroid Build Coastguard Worker }
1393*77c1e3ccSAndroid Build Coastguard Worker }
1394*77c1e3ccSAndroid Build Coastguard Worker
write_inter_txb_coeff(AV1_COMMON * const cm,MACROBLOCK * const x,MB_MODE_INFO * const mbmi,aom_writer * w,const TokenExtra ** tok,const TokenExtra * const tok_end,TOKEN_STATS * token_stats,const int row,const int col,int * block,const int plane)1395*77c1e3ccSAndroid Build Coastguard Worker static inline void write_inter_txb_coeff(
1396*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm, MACROBLOCK *const x, MB_MODE_INFO *const mbmi,
1397*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w, const TokenExtra **tok, const TokenExtra *const tok_end,
1398*77c1e3ccSAndroid Build Coastguard Worker TOKEN_STATS *token_stats, const int row, const int col, int *block,
1399*77c1e3ccSAndroid Build Coastguard Worker const int plane) {
1400*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *const xd = &x->e_mbd;
1401*77c1e3ccSAndroid Build Coastguard Worker const struct macroblockd_plane *const pd = &xd->plane[plane];
1402*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE bsize = mbmi->bsize;
1403*77c1e3ccSAndroid Build Coastguard Worker assert(bsize < BLOCK_SIZES_ALL);
1404*77c1e3ccSAndroid Build Coastguard Worker const int ss_x = pd->subsampling_x;
1405*77c1e3ccSAndroid Build Coastguard Worker const int ss_y = pd->subsampling_y;
1406*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y);
1407*77c1e3ccSAndroid Build Coastguard Worker assert(plane_bsize < BLOCK_SIZES_ALL);
1408*77c1e3ccSAndroid Build Coastguard Worker const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, plane_bsize, plane);
1409*77c1e3ccSAndroid Build Coastguard Worker const int step =
1410*77c1e3ccSAndroid Build Coastguard Worker tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
1411*77c1e3ccSAndroid Build Coastguard Worker const int bkw = tx_size_wide_unit[max_tx_size];
1412*77c1e3ccSAndroid Build Coastguard Worker const int bkh = tx_size_high_unit[max_tx_size];
1413*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE max_unit_bsize =
1414*77c1e3ccSAndroid Build Coastguard Worker get_plane_block_size(BLOCK_64X64, ss_x, ss_y);
1415*77c1e3ccSAndroid Build Coastguard Worker const int num_4x4_w = mi_size_wide[plane_bsize];
1416*77c1e3ccSAndroid Build Coastguard Worker const int num_4x4_h = mi_size_high[plane_bsize];
1417*77c1e3ccSAndroid Build Coastguard Worker const int mu_blocks_wide = mi_size_wide[max_unit_bsize];
1418*77c1e3ccSAndroid Build Coastguard Worker const int mu_blocks_high = mi_size_high[max_unit_bsize];
1419*77c1e3ccSAndroid Build Coastguard Worker const int unit_height = AOMMIN(mu_blocks_high + (row >> ss_y), num_4x4_h);
1420*77c1e3ccSAndroid Build Coastguard Worker const int unit_width = AOMMIN(mu_blocks_wide + (col >> ss_x), num_4x4_w);
1421*77c1e3ccSAndroid Build Coastguard Worker for (int blk_row = row >> ss_y; blk_row < unit_height; blk_row += bkh) {
1422*77c1e3ccSAndroid Build Coastguard Worker for (int blk_col = col >> ss_x; blk_col < unit_width; blk_col += bkw) {
1423*77c1e3ccSAndroid Build Coastguard Worker pack_txb_tokens(w, cm, x, tok, tok_end, xd, mbmi, plane, plane_bsize,
1424*77c1e3ccSAndroid Build Coastguard Worker cm->seq_params->bit_depth, *block, blk_row, blk_col,
1425*77c1e3ccSAndroid Build Coastguard Worker max_tx_size, token_stats);
1426*77c1e3ccSAndroid Build Coastguard Worker *block += step;
1427*77c1e3ccSAndroid Build Coastguard Worker }
1428*77c1e3ccSAndroid Build Coastguard Worker }
1429*77c1e3ccSAndroid Build Coastguard Worker }
1430*77c1e3ccSAndroid Build Coastguard Worker
write_tokens_b(AV1_COMP * cpi,MACROBLOCK * const x,aom_writer * w,const TokenExtra ** tok,const TokenExtra * const tok_end)1431*77c1e3ccSAndroid Build Coastguard Worker static inline void write_tokens_b(AV1_COMP *cpi, MACROBLOCK *const x,
1432*77c1e3ccSAndroid Build Coastguard Worker aom_writer *w, const TokenExtra **tok,
1433*77c1e3ccSAndroid Build Coastguard Worker const TokenExtra *const tok_end) {
1434*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
1435*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *const xd = &x->e_mbd;
1436*77c1e3ccSAndroid Build Coastguard Worker MB_MODE_INFO *const mbmi = xd->mi[0];
1437*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE bsize = mbmi->bsize;
1438*77c1e3ccSAndroid Build Coastguard Worker
1439*77c1e3ccSAndroid Build Coastguard Worker assert(!mbmi->skip_txfm);
1440*77c1e3ccSAndroid Build Coastguard Worker
1441*77c1e3ccSAndroid Build Coastguard Worker const int is_inter = is_inter_block(mbmi);
1442*77c1e3ccSAndroid Build Coastguard Worker if (!is_inter) {
1443*77c1e3ccSAndroid Build Coastguard Worker av1_write_intra_coeffs_mb(cm, x, w, bsize);
1444*77c1e3ccSAndroid Build Coastguard Worker } else {
1445*77c1e3ccSAndroid Build Coastguard Worker int block[MAX_MB_PLANE] = { 0 };
1446*77c1e3ccSAndroid Build Coastguard Worker assert(bsize == get_plane_block_size(bsize, xd->plane[0].subsampling_x,
1447*77c1e3ccSAndroid Build Coastguard Worker xd->plane[0].subsampling_y));
1448*77c1e3ccSAndroid Build Coastguard Worker const int num_4x4_w = mi_size_wide[bsize];
1449*77c1e3ccSAndroid Build Coastguard Worker const int num_4x4_h = mi_size_high[bsize];
1450*77c1e3ccSAndroid Build Coastguard Worker TOKEN_STATS token_stats;
1451*77c1e3ccSAndroid Build Coastguard Worker init_token_stats(&token_stats);
1452*77c1e3ccSAndroid Build Coastguard Worker
1453*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE max_unit_bsize = BLOCK_64X64;
1454*77c1e3ccSAndroid Build Coastguard Worker assert(max_unit_bsize == get_plane_block_size(BLOCK_64X64,
1455*77c1e3ccSAndroid Build Coastguard Worker xd->plane[0].subsampling_x,
1456*77c1e3ccSAndroid Build Coastguard Worker xd->plane[0].subsampling_y));
1457*77c1e3ccSAndroid Build Coastguard Worker int mu_blocks_wide = mi_size_wide[max_unit_bsize];
1458*77c1e3ccSAndroid Build Coastguard Worker int mu_blocks_high = mi_size_high[max_unit_bsize];
1459*77c1e3ccSAndroid Build Coastguard Worker mu_blocks_wide = AOMMIN(num_4x4_w, mu_blocks_wide);
1460*77c1e3ccSAndroid Build Coastguard Worker mu_blocks_high = AOMMIN(num_4x4_h, mu_blocks_high);
1461*77c1e3ccSAndroid Build Coastguard Worker
1462*77c1e3ccSAndroid Build Coastguard Worker const int num_planes = av1_num_planes(cm);
1463*77c1e3ccSAndroid Build Coastguard Worker for (int row = 0; row < num_4x4_h; row += mu_blocks_high) {
1464*77c1e3ccSAndroid Build Coastguard Worker for (int col = 0; col < num_4x4_w; col += mu_blocks_wide) {
1465*77c1e3ccSAndroid Build Coastguard Worker for (int plane = 0; plane < num_planes; ++plane) {
1466*77c1e3ccSAndroid Build Coastguard Worker if (plane && !xd->is_chroma_ref) break;
1467*77c1e3ccSAndroid Build Coastguard Worker write_inter_txb_coeff(cm, x, mbmi, w, tok, tok_end, &token_stats, row,
1468*77c1e3ccSAndroid Build Coastguard Worker col, &block[plane], plane);
1469*77c1e3ccSAndroid Build Coastguard Worker }
1470*77c1e3ccSAndroid Build Coastguard Worker }
1471*77c1e3ccSAndroid Build Coastguard Worker }
1472*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_RD_DEBUG
1473*77c1e3ccSAndroid Build Coastguard Worker for (int plane = 0; plane < num_planes; ++plane) {
1474*77c1e3ccSAndroid Build Coastguard Worker if (mbmi->bsize >= BLOCK_8X8 &&
1475*77c1e3ccSAndroid Build Coastguard Worker rd_token_stats_mismatch(&mbmi->rd_stats, &token_stats, plane)) {
1476*77c1e3ccSAndroid Build Coastguard Worker dump_mode_info(mbmi);
1477*77c1e3ccSAndroid Build Coastguard Worker assert(0);
1478*77c1e3ccSAndroid Build Coastguard Worker }
1479*77c1e3ccSAndroid Build Coastguard Worker }
1480*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_RD_DEBUG
1481*77c1e3ccSAndroid Build Coastguard Worker }
1482*77c1e3ccSAndroid Build Coastguard Worker }
1483*77c1e3ccSAndroid Build Coastguard Worker
write_modes_b(AV1_COMP * cpi,ThreadData * const td,const TileInfo * const tile,aom_writer * w,const TokenExtra ** tok,const TokenExtra * const tok_end,int mi_row,int mi_col)1484*77c1e3ccSAndroid Build Coastguard Worker static inline void write_modes_b(AV1_COMP *cpi, ThreadData *const td,
1485*77c1e3ccSAndroid Build Coastguard Worker const TileInfo *const tile, aom_writer *w,
1486*77c1e3ccSAndroid Build Coastguard Worker const TokenExtra **tok,
1487*77c1e3ccSAndroid Build Coastguard Worker const TokenExtra *const tok_end, int mi_row,
1488*77c1e3ccSAndroid Build Coastguard Worker int mi_col) {
1489*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *cm = &cpi->common;
1490*77c1e3ccSAndroid Build Coastguard Worker const CommonModeInfoParams *const mi_params = &cm->mi_params;
1491*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *xd = &td->mb.e_mbd;
1492*77c1e3ccSAndroid Build Coastguard Worker FRAME_CONTEXT *tile_ctx = xd->tile_ctx;
1493*77c1e3ccSAndroid Build Coastguard Worker const int grid_idx = mi_row * mi_params->mi_stride + mi_col;
1494*77c1e3ccSAndroid Build Coastguard Worker xd->mi = mi_params->mi_grid_base + grid_idx;
1495*77c1e3ccSAndroid Build Coastguard Worker td->mb.mbmi_ext_frame =
1496*77c1e3ccSAndroid Build Coastguard Worker cpi->mbmi_ext_info.frame_base +
1497*77c1e3ccSAndroid Build Coastguard Worker get_mi_ext_idx(mi_row, mi_col, cm->mi_params.mi_alloc_bsize,
1498*77c1e3ccSAndroid Build Coastguard Worker cpi->mbmi_ext_info.stride);
1499*77c1e3ccSAndroid Build Coastguard Worker xd->tx_type_map = mi_params->tx_type_map + grid_idx;
1500*77c1e3ccSAndroid Build Coastguard Worker xd->tx_type_map_stride = mi_params->mi_stride;
1501*77c1e3ccSAndroid Build Coastguard Worker
1502*77c1e3ccSAndroid Build Coastguard Worker const MB_MODE_INFO *mbmi = xd->mi[0];
1503*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE bsize = mbmi->bsize;
1504*77c1e3ccSAndroid Build Coastguard Worker assert(bsize <= cm->seq_params->sb_size ||
1505*77c1e3ccSAndroid Build Coastguard Worker (bsize >= BLOCK_SIZES && bsize < BLOCK_SIZES_ALL));
1506*77c1e3ccSAndroid Build Coastguard Worker
1507*77c1e3ccSAndroid Build Coastguard Worker const int bh = mi_size_high[bsize];
1508*77c1e3ccSAndroid Build Coastguard Worker const int bw = mi_size_wide[bsize];
1509*77c1e3ccSAndroid Build Coastguard Worker set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, mi_params->mi_rows,
1510*77c1e3ccSAndroid Build Coastguard Worker mi_params->mi_cols);
1511*77c1e3ccSAndroid Build Coastguard Worker
1512*77c1e3ccSAndroid Build Coastguard Worker xd->above_txfm_context = cm->above_contexts.txfm[tile->tile_row] + mi_col;
1513*77c1e3ccSAndroid Build Coastguard Worker xd->left_txfm_context =
1514*77c1e3ccSAndroid Build Coastguard Worker xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
1515*77c1e3ccSAndroid Build Coastguard Worker
1516*77c1e3ccSAndroid Build Coastguard Worker write_mbmi_b(cpi, td, w);
1517*77c1e3ccSAndroid Build Coastguard Worker
1518*77c1e3ccSAndroid Build Coastguard Worker for (int plane = 0; plane < AOMMIN(2, av1_num_planes(cm)); ++plane) {
1519*77c1e3ccSAndroid Build Coastguard Worker const uint8_t palette_size_plane =
1520*77c1e3ccSAndroid Build Coastguard Worker mbmi->palette_mode_info.palette_size[plane];
1521*77c1e3ccSAndroid Build Coastguard Worker assert(!mbmi->skip_mode || !palette_size_plane);
1522*77c1e3ccSAndroid Build Coastguard Worker if (palette_size_plane > 0) {
1523*77c1e3ccSAndroid Build Coastguard Worker assert(mbmi->use_intrabc == 0);
1524*77c1e3ccSAndroid Build Coastguard Worker assert(av1_allow_palette(cm->features.allow_screen_content_tools,
1525*77c1e3ccSAndroid Build Coastguard Worker mbmi->bsize));
1526*77c1e3ccSAndroid Build Coastguard Worker assert(!plane || xd->is_chroma_ref);
1527*77c1e3ccSAndroid Build Coastguard Worker int rows, cols;
1528*77c1e3ccSAndroid Build Coastguard Worker av1_get_block_dimensions(mbmi->bsize, plane, xd, NULL, NULL, &rows,
1529*77c1e3ccSAndroid Build Coastguard Worker &cols);
1530*77c1e3ccSAndroid Build Coastguard Worker assert(*tok < tok_end);
1531*77c1e3ccSAndroid Build Coastguard Worker MapCdf map_pb_cdf = plane ? tile_ctx->palette_uv_color_index_cdf
1532*77c1e3ccSAndroid Build Coastguard Worker : tile_ctx->palette_y_color_index_cdf;
1533*77c1e3ccSAndroid Build Coastguard Worker pack_map_tokens(w, tok, palette_size_plane, rows * cols, map_pb_cdf);
1534*77c1e3ccSAndroid Build Coastguard Worker }
1535*77c1e3ccSAndroid Build Coastguard Worker }
1536*77c1e3ccSAndroid Build Coastguard Worker
1537*77c1e3ccSAndroid Build Coastguard Worker const int is_inter_tx = is_inter_block(mbmi);
1538*77c1e3ccSAndroid Build Coastguard Worker const int skip_txfm = mbmi->skip_txfm;
1539*77c1e3ccSAndroid Build Coastguard Worker const uint8_t segment_id = mbmi->segment_id;
1540*77c1e3ccSAndroid Build Coastguard Worker if (cm->features.tx_mode == TX_MODE_SELECT && block_signals_txsize(bsize) &&
1541*77c1e3ccSAndroid Build Coastguard Worker !(is_inter_tx && skip_txfm) && !xd->lossless[segment_id]) {
1542*77c1e3ccSAndroid Build Coastguard Worker if (is_inter_tx) { // This implies skip flag is 0.
1543*77c1e3ccSAndroid Build Coastguard Worker const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, bsize, 0);
1544*77c1e3ccSAndroid Build Coastguard Worker const int txbh = tx_size_high_unit[max_tx_size];
1545*77c1e3ccSAndroid Build Coastguard Worker const int txbw = tx_size_wide_unit[max_tx_size];
1546*77c1e3ccSAndroid Build Coastguard Worker const int width = mi_size_wide[bsize];
1547*77c1e3ccSAndroid Build Coastguard Worker const int height = mi_size_high[bsize];
1548*77c1e3ccSAndroid Build Coastguard Worker for (int idy = 0; idy < height; idy += txbh) {
1549*77c1e3ccSAndroid Build Coastguard Worker for (int idx = 0; idx < width; idx += txbw) {
1550*77c1e3ccSAndroid Build Coastguard Worker write_tx_size_vartx(xd, mbmi, max_tx_size, 0, idy, idx, w);
1551*77c1e3ccSAndroid Build Coastguard Worker }
1552*77c1e3ccSAndroid Build Coastguard Worker }
1553*77c1e3ccSAndroid Build Coastguard Worker } else {
1554*77c1e3ccSAndroid Build Coastguard Worker write_selected_tx_size(xd, w);
1555*77c1e3ccSAndroid Build Coastguard Worker set_txfm_ctxs(mbmi->tx_size, xd->width, xd->height, 0, xd);
1556*77c1e3ccSAndroid Build Coastguard Worker }
1557*77c1e3ccSAndroid Build Coastguard Worker } else {
1558*77c1e3ccSAndroid Build Coastguard Worker set_txfm_ctxs(mbmi->tx_size, xd->width, xd->height,
1559*77c1e3ccSAndroid Build Coastguard Worker skip_txfm && is_inter_tx, xd);
1560*77c1e3ccSAndroid Build Coastguard Worker }
1561*77c1e3ccSAndroid Build Coastguard Worker
1562*77c1e3ccSAndroid Build Coastguard Worker if (!mbmi->skip_txfm) {
1563*77c1e3ccSAndroid Build Coastguard Worker int start = aom_tell_size(w);
1564*77c1e3ccSAndroid Build Coastguard Worker
1565*77c1e3ccSAndroid Build Coastguard Worker write_tokens_b(cpi, &td->mb, w, tok, tok_end);
1566*77c1e3ccSAndroid Build Coastguard Worker
1567*77c1e3ccSAndroid Build Coastguard Worker const int end = aom_tell_size(w);
1568*77c1e3ccSAndroid Build Coastguard Worker td->coefficient_size += end - start;
1569*77c1e3ccSAndroid Build Coastguard Worker }
1570*77c1e3ccSAndroid Build Coastguard Worker }
1571*77c1e3ccSAndroid Build Coastguard Worker
write_partition(const AV1_COMMON * const cm,const MACROBLOCKD * const xd,int hbs,int mi_row,int mi_col,PARTITION_TYPE p,BLOCK_SIZE bsize,aom_writer * w)1572*77c1e3ccSAndroid Build Coastguard Worker static inline void write_partition(const AV1_COMMON *const cm,
1573*77c1e3ccSAndroid Build Coastguard Worker const MACROBLOCKD *const xd, int hbs,
1574*77c1e3ccSAndroid Build Coastguard Worker int mi_row, int mi_col, PARTITION_TYPE p,
1575*77c1e3ccSAndroid Build Coastguard Worker BLOCK_SIZE bsize, aom_writer *w) {
1576*77c1e3ccSAndroid Build Coastguard Worker const int is_partition_point = bsize >= BLOCK_8X8;
1577*77c1e3ccSAndroid Build Coastguard Worker
1578*77c1e3ccSAndroid Build Coastguard Worker if (!is_partition_point) return;
1579*77c1e3ccSAndroid Build Coastguard Worker
1580*77c1e3ccSAndroid Build Coastguard Worker const int has_rows = (mi_row + hbs) < cm->mi_params.mi_rows;
1581*77c1e3ccSAndroid Build Coastguard Worker const int has_cols = (mi_col + hbs) < cm->mi_params.mi_cols;
1582*77c1e3ccSAndroid Build Coastguard Worker const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
1583*77c1e3ccSAndroid Build Coastguard Worker FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1584*77c1e3ccSAndroid Build Coastguard Worker
1585*77c1e3ccSAndroid Build Coastguard Worker if (!has_rows && !has_cols) {
1586*77c1e3ccSAndroid Build Coastguard Worker assert(p == PARTITION_SPLIT);
1587*77c1e3ccSAndroid Build Coastguard Worker return;
1588*77c1e3ccSAndroid Build Coastguard Worker }
1589*77c1e3ccSAndroid Build Coastguard Worker
1590*77c1e3ccSAndroid Build Coastguard Worker if (has_rows && has_cols) {
1591*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, p, ec_ctx->partition_cdf[ctx],
1592*77c1e3ccSAndroid Build Coastguard Worker partition_cdf_length(bsize));
1593*77c1e3ccSAndroid Build Coastguard Worker } else if (!has_rows && has_cols) {
1594*77c1e3ccSAndroid Build Coastguard Worker assert(p == PARTITION_SPLIT || p == PARTITION_HORZ);
1595*77c1e3ccSAndroid Build Coastguard Worker assert(bsize > BLOCK_8X8);
1596*77c1e3ccSAndroid Build Coastguard Worker aom_cdf_prob cdf[2];
1597*77c1e3ccSAndroid Build Coastguard Worker partition_gather_vert_alike(cdf, ec_ctx->partition_cdf[ctx], bsize);
1598*77c1e3ccSAndroid Build Coastguard Worker aom_write_cdf(w, p == PARTITION_SPLIT, cdf, 2);
1599*77c1e3ccSAndroid Build Coastguard Worker } else {
1600*77c1e3ccSAndroid Build Coastguard Worker assert(has_rows && !has_cols);
1601*77c1e3ccSAndroid Build Coastguard Worker assert(p == PARTITION_SPLIT || p == PARTITION_VERT);
1602*77c1e3ccSAndroid Build Coastguard Worker assert(bsize > BLOCK_8X8);
1603*77c1e3ccSAndroid Build Coastguard Worker aom_cdf_prob cdf[2];
1604*77c1e3ccSAndroid Build Coastguard Worker partition_gather_horz_alike(cdf, ec_ctx->partition_cdf[ctx], bsize);
1605*77c1e3ccSAndroid Build Coastguard Worker aom_write_cdf(w, p == PARTITION_SPLIT, cdf, 2);
1606*77c1e3ccSAndroid Build Coastguard Worker }
1607*77c1e3ccSAndroid Build Coastguard Worker }
1608*77c1e3ccSAndroid Build Coastguard Worker
write_modes_sb(AV1_COMP * const cpi,ThreadData * const td,const TileInfo * const tile,aom_writer * const w,const TokenExtra ** tok,const TokenExtra * const tok_end,int mi_row,int mi_col,BLOCK_SIZE bsize)1609*77c1e3ccSAndroid Build Coastguard Worker static inline void write_modes_sb(AV1_COMP *const cpi, ThreadData *const td,
1610*77c1e3ccSAndroid Build Coastguard Worker const TileInfo *const tile,
1611*77c1e3ccSAndroid Build Coastguard Worker aom_writer *const w, const TokenExtra **tok,
1612*77c1e3ccSAndroid Build Coastguard Worker const TokenExtra *const tok_end, int mi_row,
1613*77c1e3ccSAndroid Build Coastguard Worker int mi_col, BLOCK_SIZE bsize) {
1614*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
1615*77c1e3ccSAndroid Build Coastguard Worker const CommonModeInfoParams *const mi_params = &cm->mi_params;
1616*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *const xd = &td->mb.e_mbd;
1617*77c1e3ccSAndroid Build Coastguard Worker assert(bsize < BLOCK_SIZES_ALL);
1618*77c1e3ccSAndroid Build Coastguard Worker const int hbs = mi_size_wide[bsize] / 2;
1619*77c1e3ccSAndroid Build Coastguard Worker const int quarter_step = mi_size_wide[bsize] / 4;
1620*77c1e3ccSAndroid Build Coastguard Worker int i;
1621*77c1e3ccSAndroid Build Coastguard Worker const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize);
1622*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE subsize = get_partition_subsize(bsize, partition);
1623*77c1e3ccSAndroid Build Coastguard Worker
1624*77c1e3ccSAndroid Build Coastguard Worker if (mi_row >= mi_params->mi_rows || mi_col >= mi_params->mi_cols) return;
1625*77c1e3ccSAndroid Build Coastguard Worker
1626*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1627*77c1e3ccSAndroid Build Coastguard Worker const int num_planes = av1_num_planes(cm);
1628*77c1e3ccSAndroid Build Coastguard Worker for (int plane = 0; plane < num_planes; ++plane) {
1629*77c1e3ccSAndroid Build Coastguard Worker int rcol0, rcol1, rrow0, rrow1;
1630*77c1e3ccSAndroid Build Coastguard Worker
1631*77c1e3ccSAndroid Build Coastguard Worker // Skip some unnecessary work if loop restoration is disabled
1632*77c1e3ccSAndroid Build Coastguard Worker if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
1633*77c1e3ccSAndroid Build Coastguard Worker
1634*77c1e3ccSAndroid Build Coastguard Worker if (av1_loop_restoration_corners_in_sb(cm, plane, mi_row, mi_col, bsize,
1635*77c1e3ccSAndroid Build Coastguard Worker &rcol0, &rcol1, &rrow0, &rrow1)) {
1636*77c1e3ccSAndroid Build Coastguard Worker const int rstride = cm->rst_info[plane].horz_units;
1637*77c1e3ccSAndroid Build Coastguard Worker for (int rrow = rrow0; rrow < rrow1; ++rrow) {
1638*77c1e3ccSAndroid Build Coastguard Worker for (int rcol = rcol0; rcol < rcol1; ++rcol) {
1639*77c1e3ccSAndroid Build Coastguard Worker const int runit_idx = rcol + rrow * rstride;
1640*77c1e3ccSAndroid Build Coastguard Worker loop_restoration_write_sb_coeffs(cm, xd, runit_idx, w, plane,
1641*77c1e3ccSAndroid Build Coastguard Worker td->counts);
1642*77c1e3ccSAndroid Build Coastguard Worker }
1643*77c1e3ccSAndroid Build Coastguard Worker }
1644*77c1e3ccSAndroid Build Coastguard Worker }
1645*77c1e3ccSAndroid Build Coastguard Worker }
1646*77c1e3ccSAndroid Build Coastguard Worker #endif
1647*77c1e3ccSAndroid Build Coastguard Worker
1648*77c1e3ccSAndroid Build Coastguard Worker write_partition(cm, xd, hbs, mi_row, mi_col, partition, bsize, w);
1649*77c1e3ccSAndroid Build Coastguard Worker switch (partition) {
1650*77c1e3ccSAndroid Build Coastguard Worker case PARTITION_NONE:
1651*77c1e3ccSAndroid Build Coastguard Worker write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col);
1652*77c1e3ccSAndroid Build Coastguard Worker break;
1653*77c1e3ccSAndroid Build Coastguard Worker case PARTITION_HORZ:
1654*77c1e3ccSAndroid Build Coastguard Worker write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col);
1655*77c1e3ccSAndroid Build Coastguard Worker if (mi_row + hbs < mi_params->mi_rows)
1656*77c1e3ccSAndroid Build Coastguard Worker write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row + hbs, mi_col);
1657*77c1e3ccSAndroid Build Coastguard Worker break;
1658*77c1e3ccSAndroid Build Coastguard Worker case PARTITION_VERT:
1659*77c1e3ccSAndroid Build Coastguard Worker write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col);
1660*77c1e3ccSAndroid Build Coastguard Worker if (mi_col + hbs < mi_params->mi_cols)
1661*77c1e3ccSAndroid Build Coastguard Worker write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col + hbs);
1662*77c1e3ccSAndroid Build Coastguard Worker break;
1663*77c1e3ccSAndroid Build Coastguard Worker case PARTITION_SPLIT:
1664*77c1e3ccSAndroid Build Coastguard Worker write_modes_sb(cpi, td, tile, w, tok, tok_end, mi_row, mi_col, subsize);
1665*77c1e3ccSAndroid Build Coastguard Worker write_modes_sb(cpi, td, tile, w, tok, tok_end, mi_row, mi_col + hbs,
1666*77c1e3ccSAndroid Build Coastguard Worker subsize);
1667*77c1e3ccSAndroid Build Coastguard Worker write_modes_sb(cpi, td, tile, w, tok, tok_end, mi_row + hbs, mi_col,
1668*77c1e3ccSAndroid Build Coastguard Worker subsize);
1669*77c1e3ccSAndroid Build Coastguard Worker write_modes_sb(cpi, td, tile, w, tok, tok_end, mi_row + hbs, mi_col + hbs,
1670*77c1e3ccSAndroid Build Coastguard Worker subsize);
1671*77c1e3ccSAndroid Build Coastguard Worker break;
1672*77c1e3ccSAndroid Build Coastguard Worker case PARTITION_HORZ_A:
1673*77c1e3ccSAndroid Build Coastguard Worker write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col);
1674*77c1e3ccSAndroid Build Coastguard Worker write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col + hbs);
1675*77c1e3ccSAndroid Build Coastguard Worker write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row + hbs, mi_col);
1676*77c1e3ccSAndroid Build Coastguard Worker break;
1677*77c1e3ccSAndroid Build Coastguard Worker case PARTITION_HORZ_B:
1678*77c1e3ccSAndroid Build Coastguard Worker write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col);
1679*77c1e3ccSAndroid Build Coastguard Worker write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row + hbs, mi_col);
1680*77c1e3ccSAndroid Build Coastguard Worker write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row + hbs, mi_col + hbs);
1681*77c1e3ccSAndroid Build Coastguard Worker break;
1682*77c1e3ccSAndroid Build Coastguard Worker case PARTITION_VERT_A:
1683*77c1e3ccSAndroid Build Coastguard Worker write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col);
1684*77c1e3ccSAndroid Build Coastguard Worker write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row + hbs, mi_col);
1685*77c1e3ccSAndroid Build Coastguard Worker write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col + hbs);
1686*77c1e3ccSAndroid Build Coastguard Worker break;
1687*77c1e3ccSAndroid Build Coastguard Worker case PARTITION_VERT_B:
1688*77c1e3ccSAndroid Build Coastguard Worker write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col);
1689*77c1e3ccSAndroid Build Coastguard Worker write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, mi_col + hbs);
1690*77c1e3ccSAndroid Build Coastguard Worker write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row + hbs, mi_col + hbs);
1691*77c1e3ccSAndroid Build Coastguard Worker break;
1692*77c1e3ccSAndroid Build Coastguard Worker case PARTITION_HORZ_4:
1693*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < 4; ++i) {
1694*77c1e3ccSAndroid Build Coastguard Worker int this_mi_row = mi_row + i * quarter_step;
1695*77c1e3ccSAndroid Build Coastguard Worker if (i > 0 && this_mi_row >= mi_params->mi_rows) break;
1696*77c1e3ccSAndroid Build Coastguard Worker
1697*77c1e3ccSAndroid Build Coastguard Worker write_modes_b(cpi, td, tile, w, tok, tok_end, this_mi_row, mi_col);
1698*77c1e3ccSAndroid Build Coastguard Worker }
1699*77c1e3ccSAndroid Build Coastguard Worker break;
1700*77c1e3ccSAndroid Build Coastguard Worker case PARTITION_VERT_4:
1701*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < 4; ++i) {
1702*77c1e3ccSAndroid Build Coastguard Worker int this_mi_col = mi_col + i * quarter_step;
1703*77c1e3ccSAndroid Build Coastguard Worker if (i > 0 && this_mi_col >= mi_params->mi_cols) break;
1704*77c1e3ccSAndroid Build Coastguard Worker
1705*77c1e3ccSAndroid Build Coastguard Worker write_modes_b(cpi, td, tile, w, tok, tok_end, mi_row, this_mi_col);
1706*77c1e3ccSAndroid Build Coastguard Worker }
1707*77c1e3ccSAndroid Build Coastguard Worker break;
1708*77c1e3ccSAndroid Build Coastguard Worker default: assert(0);
1709*77c1e3ccSAndroid Build Coastguard Worker }
1710*77c1e3ccSAndroid Build Coastguard Worker
1711*77c1e3ccSAndroid Build Coastguard Worker // update partition context
1712*77c1e3ccSAndroid Build Coastguard Worker update_ext_partition_context(xd, mi_row, mi_col, subsize, bsize, partition);
1713*77c1e3ccSAndroid Build Coastguard Worker }
1714*77c1e3ccSAndroid Build Coastguard Worker
1715*77c1e3ccSAndroid Build Coastguard Worker // Populate token pointers appropriately based on token_info.
get_token_pointers(const TokenInfo * token_info,const int tile_row,int tile_col,const int sb_row_in_tile,const TokenExtra ** tok,const TokenExtra ** tok_end)1716*77c1e3ccSAndroid Build Coastguard Worker static inline void get_token_pointers(const TokenInfo *token_info,
1717*77c1e3ccSAndroid Build Coastguard Worker const int tile_row, int tile_col,
1718*77c1e3ccSAndroid Build Coastguard Worker const int sb_row_in_tile,
1719*77c1e3ccSAndroid Build Coastguard Worker const TokenExtra **tok,
1720*77c1e3ccSAndroid Build Coastguard Worker const TokenExtra **tok_end) {
1721*77c1e3ccSAndroid Build Coastguard Worker if (!is_token_info_allocated(token_info)) {
1722*77c1e3ccSAndroid Build Coastguard Worker *tok = NULL;
1723*77c1e3ccSAndroid Build Coastguard Worker *tok_end = NULL;
1724*77c1e3ccSAndroid Build Coastguard Worker return;
1725*77c1e3ccSAndroid Build Coastguard Worker }
1726*77c1e3ccSAndroid Build Coastguard Worker *tok = token_info->tplist[tile_row][tile_col][sb_row_in_tile].start;
1727*77c1e3ccSAndroid Build Coastguard Worker *tok_end =
1728*77c1e3ccSAndroid Build Coastguard Worker *tok + token_info->tplist[tile_row][tile_col][sb_row_in_tile].count;
1729*77c1e3ccSAndroid Build Coastguard Worker }
1730*77c1e3ccSAndroid Build Coastguard Worker
write_modes(AV1_COMP * const cpi,ThreadData * const td,const TileInfo * const tile,aom_writer * const w,int tile_row,int tile_col)1731*77c1e3ccSAndroid Build Coastguard Worker static inline void write_modes(AV1_COMP *const cpi, ThreadData *const td,
1732*77c1e3ccSAndroid Build Coastguard Worker const TileInfo *const tile, aom_writer *const w,
1733*77c1e3ccSAndroid Build Coastguard Worker int tile_row, int tile_col) {
1734*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
1735*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *const xd = &td->mb.e_mbd;
1736*77c1e3ccSAndroid Build Coastguard Worker const int mi_row_start = tile->mi_row_start;
1737*77c1e3ccSAndroid Build Coastguard Worker const int mi_row_end = tile->mi_row_end;
1738*77c1e3ccSAndroid Build Coastguard Worker const int mi_col_start = tile->mi_col_start;
1739*77c1e3ccSAndroid Build Coastguard Worker const int mi_col_end = tile->mi_col_end;
1740*77c1e3ccSAndroid Build Coastguard Worker const int num_planes = av1_num_planes(cm);
1741*77c1e3ccSAndroid Build Coastguard Worker
1742*77c1e3ccSAndroid Build Coastguard Worker av1_zero_above_context(cm, xd, mi_col_start, mi_col_end, tile->tile_row);
1743*77c1e3ccSAndroid Build Coastguard Worker av1_init_above_context(&cm->above_contexts, num_planes, tile->tile_row, xd);
1744*77c1e3ccSAndroid Build Coastguard Worker
1745*77c1e3ccSAndroid Build Coastguard Worker if (cpi->common.delta_q_info.delta_q_present_flag) {
1746*77c1e3ccSAndroid Build Coastguard Worker xd->current_base_qindex = cpi->common.quant_params.base_qindex;
1747*77c1e3ccSAndroid Build Coastguard Worker if (cpi->common.delta_q_info.delta_lf_present_flag) {
1748*77c1e3ccSAndroid Build Coastguard Worker av1_reset_loop_filter_delta(xd, num_planes);
1749*77c1e3ccSAndroid Build Coastguard Worker }
1750*77c1e3ccSAndroid Build Coastguard Worker }
1751*77c1e3ccSAndroid Build Coastguard Worker
1752*77c1e3ccSAndroid Build Coastguard Worker for (int mi_row = mi_row_start; mi_row < mi_row_end;
1753*77c1e3ccSAndroid Build Coastguard Worker mi_row += cm->seq_params->mib_size) {
1754*77c1e3ccSAndroid Build Coastguard Worker const int sb_row_in_tile =
1755*77c1e3ccSAndroid Build Coastguard Worker (mi_row - tile->mi_row_start) >> cm->seq_params->mib_size_log2;
1756*77c1e3ccSAndroid Build Coastguard Worker const TokenInfo *token_info = &cpi->token_info;
1757*77c1e3ccSAndroid Build Coastguard Worker const TokenExtra *tok;
1758*77c1e3ccSAndroid Build Coastguard Worker const TokenExtra *tok_end;
1759*77c1e3ccSAndroid Build Coastguard Worker get_token_pointers(token_info, tile_row, tile_col, sb_row_in_tile, &tok,
1760*77c1e3ccSAndroid Build Coastguard Worker &tok_end);
1761*77c1e3ccSAndroid Build Coastguard Worker
1762*77c1e3ccSAndroid Build Coastguard Worker av1_zero_left_context(xd);
1763*77c1e3ccSAndroid Build Coastguard Worker
1764*77c1e3ccSAndroid Build Coastguard Worker for (int mi_col = mi_col_start; mi_col < mi_col_end;
1765*77c1e3ccSAndroid Build Coastguard Worker mi_col += cm->seq_params->mib_size) {
1766*77c1e3ccSAndroid Build Coastguard Worker td->mb.cb_coef_buff = av1_get_cb_coeff_buffer(cpi, mi_row, mi_col);
1767*77c1e3ccSAndroid Build Coastguard Worker write_modes_sb(cpi, td, tile, w, &tok, tok_end, mi_row, mi_col,
1768*77c1e3ccSAndroid Build Coastguard Worker cm->seq_params->sb_size);
1769*77c1e3ccSAndroid Build Coastguard Worker }
1770*77c1e3ccSAndroid Build Coastguard Worker assert(tok == tok_end);
1771*77c1e3ccSAndroid Build Coastguard Worker }
1772*77c1e3ccSAndroid Build Coastguard Worker }
1773*77c1e3ccSAndroid Build Coastguard Worker
encode_restoration_mode(AV1_COMMON * cm,struct aom_write_bit_buffer * wb)1774*77c1e3ccSAndroid Build Coastguard Worker static inline void encode_restoration_mode(AV1_COMMON *cm,
1775*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
1776*77c1e3ccSAndroid Build Coastguard Worker assert(!cm->features.all_lossless);
1777*77c1e3ccSAndroid Build Coastguard Worker if (!cm->seq_params->enable_restoration) return;
1778*77c1e3ccSAndroid Build Coastguard Worker if (cm->features.allow_intrabc) return;
1779*77c1e3ccSAndroid Build Coastguard Worker const int num_planes = av1_num_planes(cm);
1780*77c1e3ccSAndroid Build Coastguard Worker int all_none = 1, chroma_none = 1;
1781*77c1e3ccSAndroid Build Coastguard Worker for (int p = 0; p < num_planes; ++p) {
1782*77c1e3ccSAndroid Build Coastguard Worker RestorationInfo *rsi = &cm->rst_info[p];
1783*77c1e3ccSAndroid Build Coastguard Worker if (rsi->frame_restoration_type != RESTORE_NONE) {
1784*77c1e3ccSAndroid Build Coastguard Worker all_none = 0;
1785*77c1e3ccSAndroid Build Coastguard Worker chroma_none &= p == 0;
1786*77c1e3ccSAndroid Build Coastguard Worker }
1787*77c1e3ccSAndroid Build Coastguard Worker switch (rsi->frame_restoration_type) {
1788*77c1e3ccSAndroid Build Coastguard Worker case RESTORE_NONE:
1789*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 0);
1790*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 0);
1791*77c1e3ccSAndroid Build Coastguard Worker break;
1792*77c1e3ccSAndroid Build Coastguard Worker case RESTORE_WIENER:
1793*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 1);
1794*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 0);
1795*77c1e3ccSAndroid Build Coastguard Worker break;
1796*77c1e3ccSAndroid Build Coastguard Worker case RESTORE_SGRPROJ:
1797*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 1);
1798*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 1);
1799*77c1e3ccSAndroid Build Coastguard Worker break;
1800*77c1e3ccSAndroid Build Coastguard Worker case RESTORE_SWITCHABLE:
1801*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 0);
1802*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 1);
1803*77c1e3ccSAndroid Build Coastguard Worker break;
1804*77c1e3ccSAndroid Build Coastguard Worker default: assert(0);
1805*77c1e3ccSAndroid Build Coastguard Worker }
1806*77c1e3ccSAndroid Build Coastguard Worker }
1807*77c1e3ccSAndroid Build Coastguard Worker if (!all_none) {
1808*77c1e3ccSAndroid Build Coastguard Worker assert(cm->seq_params->sb_size == BLOCK_64X64 ||
1809*77c1e3ccSAndroid Build Coastguard Worker cm->seq_params->sb_size == BLOCK_128X128);
1810*77c1e3ccSAndroid Build Coastguard Worker const int sb_size = cm->seq_params->sb_size == BLOCK_128X128 ? 128 : 64;
1811*77c1e3ccSAndroid Build Coastguard Worker
1812*77c1e3ccSAndroid Build Coastguard Worker RestorationInfo *rsi = &cm->rst_info[0];
1813*77c1e3ccSAndroid Build Coastguard Worker
1814*77c1e3ccSAndroid Build Coastguard Worker assert(rsi->restoration_unit_size >= sb_size);
1815*77c1e3ccSAndroid Build Coastguard Worker assert(RESTORATION_UNITSIZE_MAX == 256);
1816*77c1e3ccSAndroid Build Coastguard Worker
1817*77c1e3ccSAndroid Build Coastguard Worker if (sb_size == 64) {
1818*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, rsi->restoration_unit_size > 64);
1819*77c1e3ccSAndroid Build Coastguard Worker }
1820*77c1e3ccSAndroid Build Coastguard Worker if (rsi->restoration_unit_size > 64) {
1821*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, rsi->restoration_unit_size > 128);
1822*77c1e3ccSAndroid Build Coastguard Worker }
1823*77c1e3ccSAndroid Build Coastguard Worker }
1824*77c1e3ccSAndroid Build Coastguard Worker
1825*77c1e3ccSAndroid Build Coastguard Worker if (num_planes > 1) {
1826*77c1e3ccSAndroid Build Coastguard Worker int s =
1827*77c1e3ccSAndroid Build Coastguard Worker AOMMIN(cm->seq_params->subsampling_x, cm->seq_params->subsampling_y);
1828*77c1e3ccSAndroid Build Coastguard Worker if (s && !chroma_none) {
1829*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, cm->rst_info[1].restoration_unit_size !=
1830*77c1e3ccSAndroid Build Coastguard Worker cm->rst_info[0].restoration_unit_size);
1831*77c1e3ccSAndroid Build Coastguard Worker assert(cm->rst_info[1].restoration_unit_size ==
1832*77c1e3ccSAndroid Build Coastguard Worker cm->rst_info[0].restoration_unit_size ||
1833*77c1e3ccSAndroid Build Coastguard Worker cm->rst_info[1].restoration_unit_size ==
1834*77c1e3ccSAndroid Build Coastguard Worker (cm->rst_info[0].restoration_unit_size >> s));
1835*77c1e3ccSAndroid Build Coastguard Worker assert(cm->rst_info[2].restoration_unit_size ==
1836*77c1e3ccSAndroid Build Coastguard Worker cm->rst_info[1].restoration_unit_size);
1837*77c1e3ccSAndroid Build Coastguard Worker } else if (!s) {
1838*77c1e3ccSAndroid Build Coastguard Worker assert(cm->rst_info[1].restoration_unit_size ==
1839*77c1e3ccSAndroid Build Coastguard Worker cm->rst_info[0].restoration_unit_size);
1840*77c1e3ccSAndroid Build Coastguard Worker assert(cm->rst_info[2].restoration_unit_size ==
1841*77c1e3ccSAndroid Build Coastguard Worker cm->rst_info[1].restoration_unit_size);
1842*77c1e3ccSAndroid Build Coastguard Worker }
1843*77c1e3ccSAndroid Build Coastguard Worker }
1844*77c1e3ccSAndroid Build Coastguard Worker }
1845*77c1e3ccSAndroid Build Coastguard Worker
1846*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
write_wiener_filter(int wiener_win,const WienerInfo * wiener_info,WienerInfo * ref_wiener_info,aom_writer * wb)1847*77c1e3ccSAndroid Build Coastguard Worker static inline void write_wiener_filter(int wiener_win,
1848*77c1e3ccSAndroid Build Coastguard Worker const WienerInfo *wiener_info,
1849*77c1e3ccSAndroid Build Coastguard Worker WienerInfo *ref_wiener_info,
1850*77c1e3ccSAndroid Build Coastguard Worker aom_writer *wb) {
1851*77c1e3ccSAndroid Build Coastguard Worker if (wiener_win == WIENER_WIN)
1852*77c1e3ccSAndroid Build Coastguard Worker aom_write_primitive_refsubexpfin(
1853*77c1e3ccSAndroid Build Coastguard Worker wb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
1854*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP0_SUBEXP_K,
1855*77c1e3ccSAndroid Build Coastguard Worker ref_wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV,
1856*77c1e3ccSAndroid Build Coastguard Worker wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV);
1857*77c1e3ccSAndroid Build Coastguard Worker else
1858*77c1e3ccSAndroid Build Coastguard Worker assert(wiener_info->vfilter[0] == 0 &&
1859*77c1e3ccSAndroid Build Coastguard Worker wiener_info->vfilter[WIENER_WIN - 1] == 0);
1860*77c1e3ccSAndroid Build Coastguard Worker aom_write_primitive_refsubexpfin(
1861*77c1e3ccSAndroid Build Coastguard Worker wb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
1862*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP1_SUBEXP_K,
1863*77c1e3ccSAndroid Build Coastguard Worker ref_wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV,
1864*77c1e3ccSAndroid Build Coastguard Worker wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV);
1865*77c1e3ccSAndroid Build Coastguard Worker aom_write_primitive_refsubexpfin(
1866*77c1e3ccSAndroid Build Coastguard Worker wb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
1867*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP2_SUBEXP_K,
1868*77c1e3ccSAndroid Build Coastguard Worker ref_wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV,
1869*77c1e3ccSAndroid Build Coastguard Worker wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV);
1870*77c1e3ccSAndroid Build Coastguard Worker if (wiener_win == WIENER_WIN)
1871*77c1e3ccSAndroid Build Coastguard Worker aom_write_primitive_refsubexpfin(
1872*77c1e3ccSAndroid Build Coastguard Worker wb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
1873*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP0_SUBEXP_K,
1874*77c1e3ccSAndroid Build Coastguard Worker ref_wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV,
1875*77c1e3ccSAndroid Build Coastguard Worker wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV);
1876*77c1e3ccSAndroid Build Coastguard Worker else
1877*77c1e3ccSAndroid Build Coastguard Worker assert(wiener_info->hfilter[0] == 0 &&
1878*77c1e3ccSAndroid Build Coastguard Worker wiener_info->hfilter[WIENER_WIN - 1] == 0);
1879*77c1e3ccSAndroid Build Coastguard Worker aom_write_primitive_refsubexpfin(
1880*77c1e3ccSAndroid Build Coastguard Worker wb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
1881*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP1_SUBEXP_K,
1882*77c1e3ccSAndroid Build Coastguard Worker ref_wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV,
1883*77c1e3ccSAndroid Build Coastguard Worker wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV);
1884*77c1e3ccSAndroid Build Coastguard Worker aom_write_primitive_refsubexpfin(
1885*77c1e3ccSAndroid Build Coastguard Worker wb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
1886*77c1e3ccSAndroid Build Coastguard Worker WIENER_FILT_TAP2_SUBEXP_K,
1887*77c1e3ccSAndroid Build Coastguard Worker ref_wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV,
1888*77c1e3ccSAndroid Build Coastguard Worker wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV);
1889*77c1e3ccSAndroid Build Coastguard Worker memcpy(ref_wiener_info, wiener_info, sizeof(*wiener_info));
1890*77c1e3ccSAndroid Build Coastguard Worker }
1891*77c1e3ccSAndroid Build Coastguard Worker
write_sgrproj_filter(const SgrprojInfo * sgrproj_info,SgrprojInfo * ref_sgrproj_info,aom_writer * wb)1892*77c1e3ccSAndroid Build Coastguard Worker static inline void write_sgrproj_filter(const SgrprojInfo *sgrproj_info,
1893*77c1e3ccSAndroid Build Coastguard Worker SgrprojInfo *ref_sgrproj_info,
1894*77c1e3ccSAndroid Build Coastguard Worker aom_writer *wb) {
1895*77c1e3ccSAndroid Build Coastguard Worker aom_write_literal(wb, sgrproj_info->ep, SGRPROJ_PARAMS_BITS);
1896*77c1e3ccSAndroid Build Coastguard Worker const sgr_params_type *params = &av1_sgr_params[sgrproj_info->ep];
1897*77c1e3ccSAndroid Build Coastguard Worker
1898*77c1e3ccSAndroid Build Coastguard Worker if (params->r[0] == 0) {
1899*77c1e3ccSAndroid Build Coastguard Worker assert(sgrproj_info->xqd[0] == 0);
1900*77c1e3ccSAndroid Build Coastguard Worker aom_write_primitive_refsubexpfin(
1901*77c1e3ccSAndroid Build Coastguard Worker wb, SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
1902*77c1e3ccSAndroid Build Coastguard Worker ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1,
1903*77c1e3ccSAndroid Build Coastguard Worker sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1);
1904*77c1e3ccSAndroid Build Coastguard Worker } else if (params->r[1] == 0) {
1905*77c1e3ccSAndroid Build Coastguard Worker aom_write_primitive_refsubexpfin(
1906*77c1e3ccSAndroid Build Coastguard Worker wb, SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
1907*77c1e3ccSAndroid Build Coastguard Worker ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0,
1908*77c1e3ccSAndroid Build Coastguard Worker sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0);
1909*77c1e3ccSAndroid Build Coastguard Worker } else {
1910*77c1e3ccSAndroid Build Coastguard Worker aom_write_primitive_refsubexpfin(
1911*77c1e3ccSAndroid Build Coastguard Worker wb, SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
1912*77c1e3ccSAndroid Build Coastguard Worker ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0,
1913*77c1e3ccSAndroid Build Coastguard Worker sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0);
1914*77c1e3ccSAndroid Build Coastguard Worker aom_write_primitive_refsubexpfin(
1915*77c1e3ccSAndroid Build Coastguard Worker wb, SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
1916*77c1e3ccSAndroid Build Coastguard Worker ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1,
1917*77c1e3ccSAndroid Build Coastguard Worker sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1);
1918*77c1e3ccSAndroid Build Coastguard Worker }
1919*77c1e3ccSAndroid Build Coastguard Worker
1920*77c1e3ccSAndroid Build Coastguard Worker memcpy(ref_sgrproj_info, sgrproj_info, sizeof(*sgrproj_info));
1921*77c1e3ccSAndroid Build Coastguard Worker }
1922*77c1e3ccSAndroid Build Coastguard Worker
loop_restoration_write_sb_coeffs(const AV1_COMMON * const cm,MACROBLOCKD * xd,int runit_idx,aom_writer * const w,int plane,FRAME_COUNTS * counts)1923*77c1e3ccSAndroid Build Coastguard Worker static inline void loop_restoration_write_sb_coeffs(
1924*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm, MACROBLOCKD *xd, int runit_idx,
1925*77c1e3ccSAndroid Build Coastguard Worker aom_writer *const w, int plane, FRAME_COUNTS *counts) {
1926*77c1e3ccSAndroid Build Coastguard Worker const RestorationUnitInfo *rui = &cm->rst_info[plane].unit_info[runit_idx];
1927*77c1e3ccSAndroid Build Coastguard Worker const RestorationInfo *rsi = cm->rst_info + plane;
1928*77c1e3ccSAndroid Build Coastguard Worker RestorationType frame_rtype = rsi->frame_restoration_type;
1929*77c1e3ccSAndroid Build Coastguard Worker assert(frame_rtype != RESTORE_NONE);
1930*77c1e3ccSAndroid Build Coastguard Worker
1931*77c1e3ccSAndroid Build Coastguard Worker (void)counts;
1932*77c1e3ccSAndroid Build Coastguard Worker assert(!cm->features.all_lossless);
1933*77c1e3ccSAndroid Build Coastguard Worker
1934*77c1e3ccSAndroid Build Coastguard Worker const int wiener_win = (plane > 0) ? WIENER_WIN_CHROMA : WIENER_WIN;
1935*77c1e3ccSAndroid Build Coastguard Worker WienerInfo *ref_wiener_info = &xd->wiener_info[plane];
1936*77c1e3ccSAndroid Build Coastguard Worker SgrprojInfo *ref_sgrproj_info = &xd->sgrproj_info[plane];
1937*77c1e3ccSAndroid Build Coastguard Worker RestorationType unit_rtype = rui->restoration_type;
1938*77c1e3ccSAndroid Build Coastguard Worker
1939*77c1e3ccSAndroid Build Coastguard Worker if (frame_rtype == RESTORE_SWITCHABLE) {
1940*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, unit_rtype, xd->tile_ctx->switchable_restore_cdf,
1941*77c1e3ccSAndroid Build Coastguard Worker RESTORE_SWITCHABLE_TYPES);
1942*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1943*77c1e3ccSAndroid Build Coastguard Worker ++counts->switchable_restore[unit_rtype];
1944*77c1e3ccSAndroid Build Coastguard Worker #endif
1945*77c1e3ccSAndroid Build Coastguard Worker switch (unit_rtype) {
1946*77c1e3ccSAndroid Build Coastguard Worker case RESTORE_WIENER:
1947*77c1e3ccSAndroid Build Coastguard Worker #if DEBUG_LR_COSTING
1948*77c1e3ccSAndroid Build Coastguard Worker assert(!memcmp(
1949*77c1e3ccSAndroid Build Coastguard Worker ref_wiener_info,
1950*77c1e3ccSAndroid Build Coastguard Worker &lr_ref_params[RESTORE_SWITCHABLE][plane][runit_idx].wiener_info,
1951*77c1e3ccSAndroid Build Coastguard Worker sizeof(*ref_wiener_info)));
1952*77c1e3ccSAndroid Build Coastguard Worker #endif
1953*77c1e3ccSAndroid Build Coastguard Worker write_wiener_filter(wiener_win, &rui->wiener_info, ref_wiener_info, w);
1954*77c1e3ccSAndroid Build Coastguard Worker break;
1955*77c1e3ccSAndroid Build Coastguard Worker case RESTORE_SGRPROJ:
1956*77c1e3ccSAndroid Build Coastguard Worker #if DEBUG_LR_COSTING
1957*77c1e3ccSAndroid Build Coastguard Worker assert(!memcmp(&ref_sgrproj_info->xqd,
1958*77c1e3ccSAndroid Build Coastguard Worker &lr_ref_params[RESTORE_SWITCHABLE][plane][runit_idx]
1959*77c1e3ccSAndroid Build Coastguard Worker .sgrproj_info.xqd,
1960*77c1e3ccSAndroid Build Coastguard Worker sizeof(ref_sgrproj_info->xqd)));
1961*77c1e3ccSAndroid Build Coastguard Worker #endif
1962*77c1e3ccSAndroid Build Coastguard Worker write_sgrproj_filter(&rui->sgrproj_info, ref_sgrproj_info, w);
1963*77c1e3ccSAndroid Build Coastguard Worker break;
1964*77c1e3ccSAndroid Build Coastguard Worker default: assert(unit_rtype == RESTORE_NONE); break;
1965*77c1e3ccSAndroid Build Coastguard Worker }
1966*77c1e3ccSAndroid Build Coastguard Worker } else if (frame_rtype == RESTORE_WIENER) {
1967*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, unit_rtype != RESTORE_NONE,
1968*77c1e3ccSAndroid Build Coastguard Worker xd->tile_ctx->wiener_restore_cdf, 2);
1969*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1970*77c1e3ccSAndroid Build Coastguard Worker ++counts->wiener_restore[unit_rtype != RESTORE_NONE];
1971*77c1e3ccSAndroid Build Coastguard Worker #endif
1972*77c1e3ccSAndroid Build Coastguard Worker if (unit_rtype != RESTORE_NONE) {
1973*77c1e3ccSAndroid Build Coastguard Worker #if DEBUG_LR_COSTING
1974*77c1e3ccSAndroid Build Coastguard Worker assert(
1975*77c1e3ccSAndroid Build Coastguard Worker !memcmp(ref_wiener_info,
1976*77c1e3ccSAndroid Build Coastguard Worker &lr_ref_params[RESTORE_WIENER][plane][runit_idx].wiener_info,
1977*77c1e3ccSAndroid Build Coastguard Worker sizeof(*ref_wiener_info)));
1978*77c1e3ccSAndroid Build Coastguard Worker #endif
1979*77c1e3ccSAndroid Build Coastguard Worker write_wiener_filter(wiener_win, &rui->wiener_info, ref_wiener_info, w);
1980*77c1e3ccSAndroid Build Coastguard Worker }
1981*77c1e3ccSAndroid Build Coastguard Worker } else if (frame_rtype == RESTORE_SGRPROJ) {
1982*77c1e3ccSAndroid Build Coastguard Worker aom_write_symbol(w, unit_rtype != RESTORE_NONE,
1983*77c1e3ccSAndroid Build Coastguard Worker xd->tile_ctx->sgrproj_restore_cdf, 2);
1984*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ENTROPY_STATS
1985*77c1e3ccSAndroid Build Coastguard Worker ++counts->sgrproj_restore[unit_rtype != RESTORE_NONE];
1986*77c1e3ccSAndroid Build Coastguard Worker #endif
1987*77c1e3ccSAndroid Build Coastguard Worker if (unit_rtype != RESTORE_NONE) {
1988*77c1e3ccSAndroid Build Coastguard Worker #if DEBUG_LR_COSTING
1989*77c1e3ccSAndroid Build Coastguard Worker assert(!memcmp(
1990*77c1e3ccSAndroid Build Coastguard Worker &ref_sgrproj_info->xqd,
1991*77c1e3ccSAndroid Build Coastguard Worker &lr_ref_params[RESTORE_SGRPROJ][plane][runit_idx].sgrproj_info.xqd,
1992*77c1e3ccSAndroid Build Coastguard Worker sizeof(ref_sgrproj_info->xqd)));
1993*77c1e3ccSAndroid Build Coastguard Worker #endif
1994*77c1e3ccSAndroid Build Coastguard Worker write_sgrproj_filter(&rui->sgrproj_info, ref_sgrproj_info, w);
1995*77c1e3ccSAndroid Build Coastguard Worker }
1996*77c1e3ccSAndroid Build Coastguard Worker }
1997*77c1e3ccSAndroid Build Coastguard Worker }
1998*77c1e3ccSAndroid Build Coastguard Worker #endif // !CONFIG_REALTIME_ONLY
1999*77c1e3ccSAndroid Build Coastguard Worker
2000*77c1e3ccSAndroid Build Coastguard Worker // Only write out the ref delta section if any of the elements
2001*77c1e3ccSAndroid Build Coastguard Worker // will signal a delta.
is_mode_ref_delta_meaningful(AV1_COMMON * cm)2002*77c1e3ccSAndroid Build Coastguard Worker static bool is_mode_ref_delta_meaningful(AV1_COMMON *cm) {
2003*77c1e3ccSAndroid Build Coastguard Worker struct loopfilter *lf = &cm->lf;
2004*77c1e3ccSAndroid Build Coastguard Worker if (!lf->mode_ref_delta_update) {
2005*77c1e3ccSAndroid Build Coastguard Worker return 0;
2006*77c1e3ccSAndroid Build Coastguard Worker }
2007*77c1e3ccSAndroid Build Coastguard Worker const RefCntBuffer *buf = get_primary_ref_frame_buf(cm);
2008*77c1e3ccSAndroid Build Coastguard Worker int8_t last_ref_deltas[REF_FRAMES];
2009*77c1e3ccSAndroid Build Coastguard Worker int8_t last_mode_deltas[MAX_MODE_LF_DELTAS];
2010*77c1e3ccSAndroid Build Coastguard Worker if (buf == NULL) {
2011*77c1e3ccSAndroid Build Coastguard Worker av1_set_default_ref_deltas(last_ref_deltas);
2012*77c1e3ccSAndroid Build Coastguard Worker av1_set_default_mode_deltas(last_mode_deltas);
2013*77c1e3ccSAndroid Build Coastguard Worker } else {
2014*77c1e3ccSAndroid Build Coastguard Worker memcpy(last_ref_deltas, buf->ref_deltas, REF_FRAMES);
2015*77c1e3ccSAndroid Build Coastguard Worker memcpy(last_mode_deltas, buf->mode_deltas, MAX_MODE_LF_DELTAS);
2016*77c1e3ccSAndroid Build Coastguard Worker }
2017*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < REF_FRAMES; i++) {
2018*77c1e3ccSAndroid Build Coastguard Worker if (lf->ref_deltas[i] != last_ref_deltas[i]) {
2019*77c1e3ccSAndroid Build Coastguard Worker return true;
2020*77c1e3ccSAndroid Build Coastguard Worker }
2021*77c1e3ccSAndroid Build Coastguard Worker }
2022*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < MAX_MODE_LF_DELTAS; i++) {
2023*77c1e3ccSAndroid Build Coastguard Worker if (lf->mode_deltas[i] != last_mode_deltas[i]) {
2024*77c1e3ccSAndroid Build Coastguard Worker return true;
2025*77c1e3ccSAndroid Build Coastguard Worker }
2026*77c1e3ccSAndroid Build Coastguard Worker }
2027*77c1e3ccSAndroid Build Coastguard Worker return false;
2028*77c1e3ccSAndroid Build Coastguard Worker }
2029*77c1e3ccSAndroid Build Coastguard Worker
encode_loopfilter(AV1_COMMON * cm,struct aom_write_bit_buffer * wb)2030*77c1e3ccSAndroid Build Coastguard Worker static inline void encode_loopfilter(AV1_COMMON *cm,
2031*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2032*77c1e3ccSAndroid Build Coastguard Worker assert(!cm->features.coded_lossless);
2033*77c1e3ccSAndroid Build Coastguard Worker if (cm->features.allow_intrabc) return;
2034*77c1e3ccSAndroid Build Coastguard Worker const int num_planes = av1_num_planes(cm);
2035*77c1e3ccSAndroid Build Coastguard Worker struct loopfilter *lf = &cm->lf;
2036*77c1e3ccSAndroid Build Coastguard Worker
2037*77c1e3ccSAndroid Build Coastguard Worker // Encode the loop filter level and type
2038*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, lf->filter_level[0], 6);
2039*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, lf->filter_level[1], 6);
2040*77c1e3ccSAndroid Build Coastguard Worker if (num_planes > 1) {
2041*77c1e3ccSAndroid Build Coastguard Worker if (lf->filter_level[0] || lf->filter_level[1]) {
2042*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, lf->filter_level_u, 6);
2043*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, lf->filter_level_v, 6);
2044*77c1e3ccSAndroid Build Coastguard Worker }
2045*77c1e3ccSAndroid Build Coastguard Worker }
2046*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, lf->sharpness_level, 3);
2047*77c1e3ccSAndroid Build Coastguard Worker
2048*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, lf->mode_ref_delta_enabled);
2049*77c1e3ccSAndroid Build Coastguard Worker
2050*77c1e3ccSAndroid Build Coastguard Worker // Write out loop filter deltas applied at the MB level based on mode or
2051*77c1e3ccSAndroid Build Coastguard Worker // ref frame (if they are enabled), only if there is information to write.
2052*77c1e3ccSAndroid Build Coastguard Worker int meaningful = is_mode_ref_delta_meaningful(cm);
2053*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, meaningful);
2054*77c1e3ccSAndroid Build Coastguard Worker if (!meaningful) {
2055*77c1e3ccSAndroid Build Coastguard Worker return;
2056*77c1e3ccSAndroid Build Coastguard Worker }
2057*77c1e3ccSAndroid Build Coastguard Worker
2058*77c1e3ccSAndroid Build Coastguard Worker const RefCntBuffer *buf = get_primary_ref_frame_buf(cm);
2059*77c1e3ccSAndroid Build Coastguard Worker int8_t last_ref_deltas[REF_FRAMES];
2060*77c1e3ccSAndroid Build Coastguard Worker int8_t last_mode_deltas[MAX_MODE_LF_DELTAS];
2061*77c1e3ccSAndroid Build Coastguard Worker if (buf == NULL) {
2062*77c1e3ccSAndroid Build Coastguard Worker av1_set_default_ref_deltas(last_ref_deltas);
2063*77c1e3ccSAndroid Build Coastguard Worker av1_set_default_mode_deltas(last_mode_deltas);
2064*77c1e3ccSAndroid Build Coastguard Worker } else {
2065*77c1e3ccSAndroid Build Coastguard Worker memcpy(last_ref_deltas, buf->ref_deltas, REF_FRAMES);
2066*77c1e3ccSAndroid Build Coastguard Worker memcpy(last_mode_deltas, buf->mode_deltas, MAX_MODE_LF_DELTAS);
2067*77c1e3ccSAndroid Build Coastguard Worker }
2068*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < REF_FRAMES; i++) {
2069*77c1e3ccSAndroid Build Coastguard Worker const int delta = lf->ref_deltas[i];
2070*77c1e3ccSAndroid Build Coastguard Worker const int changed = delta != last_ref_deltas[i];
2071*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, changed);
2072*77c1e3ccSAndroid Build Coastguard Worker if (changed) aom_wb_write_inv_signed_literal(wb, delta, 6);
2073*77c1e3ccSAndroid Build Coastguard Worker }
2074*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < MAX_MODE_LF_DELTAS; i++) {
2075*77c1e3ccSAndroid Build Coastguard Worker const int delta = lf->mode_deltas[i];
2076*77c1e3ccSAndroid Build Coastguard Worker const int changed = delta != last_mode_deltas[i];
2077*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, changed);
2078*77c1e3ccSAndroid Build Coastguard Worker if (changed) aom_wb_write_inv_signed_literal(wb, delta, 6);
2079*77c1e3ccSAndroid Build Coastguard Worker }
2080*77c1e3ccSAndroid Build Coastguard Worker }
2081*77c1e3ccSAndroid Build Coastguard Worker
encode_cdef(const AV1_COMMON * cm,struct aom_write_bit_buffer * wb)2082*77c1e3ccSAndroid Build Coastguard Worker static inline void encode_cdef(const AV1_COMMON *cm,
2083*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2084*77c1e3ccSAndroid Build Coastguard Worker assert(!cm->features.coded_lossless);
2085*77c1e3ccSAndroid Build Coastguard Worker if (!cm->seq_params->enable_cdef) return;
2086*77c1e3ccSAndroid Build Coastguard Worker if (cm->features.allow_intrabc) return;
2087*77c1e3ccSAndroid Build Coastguard Worker const int num_planes = av1_num_planes(cm);
2088*77c1e3ccSAndroid Build Coastguard Worker int i;
2089*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, cm->cdef_info.cdef_damping - 3, 2);
2090*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, cm->cdef_info.cdef_bits, 2);
2091*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < cm->cdef_info.nb_cdef_strengths; i++) {
2092*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, cm->cdef_info.cdef_strengths[i],
2093*77c1e3ccSAndroid Build Coastguard Worker CDEF_STRENGTH_BITS);
2094*77c1e3ccSAndroid Build Coastguard Worker if (num_planes > 1)
2095*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, cm->cdef_info.cdef_uv_strengths[i],
2096*77c1e3ccSAndroid Build Coastguard Worker CDEF_STRENGTH_BITS);
2097*77c1e3ccSAndroid Build Coastguard Worker }
2098*77c1e3ccSAndroid Build Coastguard Worker }
2099*77c1e3ccSAndroid Build Coastguard Worker
write_delta_q(struct aom_write_bit_buffer * wb,int delta_q)2100*77c1e3ccSAndroid Build Coastguard Worker static inline void write_delta_q(struct aom_write_bit_buffer *wb, int delta_q) {
2101*77c1e3ccSAndroid Build Coastguard Worker if (delta_q != 0) {
2102*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 1);
2103*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_inv_signed_literal(wb, delta_q, 6);
2104*77c1e3ccSAndroid Build Coastguard Worker } else {
2105*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 0);
2106*77c1e3ccSAndroid Build Coastguard Worker }
2107*77c1e3ccSAndroid Build Coastguard Worker }
2108*77c1e3ccSAndroid Build Coastguard Worker
encode_quantization(const CommonQuantParams * const quant_params,int num_planes,bool separate_uv_delta_q,struct aom_write_bit_buffer * wb)2109*77c1e3ccSAndroid Build Coastguard Worker static inline void encode_quantization(
2110*77c1e3ccSAndroid Build Coastguard Worker const CommonQuantParams *const quant_params, int num_planes,
2111*77c1e3ccSAndroid Build Coastguard Worker bool separate_uv_delta_q, struct aom_write_bit_buffer *wb) {
2112*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, quant_params->base_qindex, QINDEX_BITS);
2113*77c1e3ccSAndroid Build Coastguard Worker write_delta_q(wb, quant_params->y_dc_delta_q);
2114*77c1e3ccSAndroid Build Coastguard Worker if (num_planes > 1) {
2115*77c1e3ccSAndroid Build Coastguard Worker int diff_uv_delta =
2116*77c1e3ccSAndroid Build Coastguard Worker (quant_params->u_dc_delta_q != quant_params->v_dc_delta_q) ||
2117*77c1e3ccSAndroid Build Coastguard Worker (quant_params->u_ac_delta_q != quant_params->v_ac_delta_q);
2118*77c1e3ccSAndroid Build Coastguard Worker if (separate_uv_delta_q) aom_wb_write_bit(wb, diff_uv_delta);
2119*77c1e3ccSAndroid Build Coastguard Worker write_delta_q(wb, quant_params->u_dc_delta_q);
2120*77c1e3ccSAndroid Build Coastguard Worker write_delta_q(wb, quant_params->u_ac_delta_q);
2121*77c1e3ccSAndroid Build Coastguard Worker if (diff_uv_delta) {
2122*77c1e3ccSAndroid Build Coastguard Worker write_delta_q(wb, quant_params->v_dc_delta_q);
2123*77c1e3ccSAndroid Build Coastguard Worker write_delta_q(wb, quant_params->v_ac_delta_q);
2124*77c1e3ccSAndroid Build Coastguard Worker }
2125*77c1e3ccSAndroid Build Coastguard Worker }
2126*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, quant_params->using_qmatrix);
2127*77c1e3ccSAndroid Build Coastguard Worker if (quant_params->using_qmatrix) {
2128*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, quant_params->qmatrix_level_y, QM_LEVEL_BITS);
2129*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, quant_params->qmatrix_level_u, QM_LEVEL_BITS);
2130*77c1e3ccSAndroid Build Coastguard Worker if (!separate_uv_delta_q)
2131*77c1e3ccSAndroid Build Coastguard Worker assert(quant_params->qmatrix_level_u == quant_params->qmatrix_level_v);
2132*77c1e3ccSAndroid Build Coastguard Worker else
2133*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, quant_params->qmatrix_level_v, QM_LEVEL_BITS);
2134*77c1e3ccSAndroid Build Coastguard Worker }
2135*77c1e3ccSAndroid Build Coastguard Worker }
2136*77c1e3ccSAndroid Build Coastguard Worker
encode_segmentation(AV1_COMMON * cm,struct aom_write_bit_buffer * wb)2137*77c1e3ccSAndroid Build Coastguard Worker static inline void encode_segmentation(AV1_COMMON *cm,
2138*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2139*77c1e3ccSAndroid Build Coastguard Worker int i, j;
2140*77c1e3ccSAndroid Build Coastguard Worker struct segmentation *seg = &cm->seg;
2141*77c1e3ccSAndroid Build Coastguard Worker
2142*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seg->enabled);
2143*77c1e3ccSAndroid Build Coastguard Worker if (!seg->enabled) return;
2144*77c1e3ccSAndroid Build Coastguard Worker
2145*77c1e3ccSAndroid Build Coastguard Worker // Write update flags
2146*77c1e3ccSAndroid Build Coastguard Worker if (cm->features.primary_ref_frame != PRIMARY_REF_NONE) {
2147*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seg->update_map);
2148*77c1e3ccSAndroid Build Coastguard Worker if (seg->update_map) aom_wb_write_bit(wb, seg->temporal_update);
2149*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seg->update_data);
2150*77c1e3ccSAndroid Build Coastguard Worker }
2151*77c1e3ccSAndroid Build Coastguard Worker
2152*77c1e3ccSAndroid Build Coastguard Worker // Segmentation data
2153*77c1e3ccSAndroid Build Coastguard Worker if (seg->update_data) {
2154*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < MAX_SEGMENTS; i++) {
2155*77c1e3ccSAndroid Build Coastguard Worker for (j = 0; j < SEG_LVL_MAX; j++) {
2156*77c1e3ccSAndroid Build Coastguard Worker const int active = segfeature_active(seg, i, j);
2157*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, active);
2158*77c1e3ccSAndroid Build Coastguard Worker if (active) {
2159*77c1e3ccSAndroid Build Coastguard Worker const int data_max = av1_seg_feature_data_max(j);
2160*77c1e3ccSAndroid Build Coastguard Worker const int data_min = -data_max;
2161*77c1e3ccSAndroid Build Coastguard Worker const int ubits = get_unsigned_bits(data_max);
2162*77c1e3ccSAndroid Build Coastguard Worker const int data = clamp(get_segdata(seg, i, j), data_min, data_max);
2163*77c1e3ccSAndroid Build Coastguard Worker
2164*77c1e3ccSAndroid Build Coastguard Worker if (av1_is_segfeature_signed(j)) {
2165*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_inv_signed_literal(wb, data, ubits);
2166*77c1e3ccSAndroid Build Coastguard Worker } else {
2167*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, data, ubits);
2168*77c1e3ccSAndroid Build Coastguard Worker }
2169*77c1e3ccSAndroid Build Coastguard Worker }
2170*77c1e3ccSAndroid Build Coastguard Worker }
2171*77c1e3ccSAndroid Build Coastguard Worker }
2172*77c1e3ccSAndroid Build Coastguard Worker }
2173*77c1e3ccSAndroid Build Coastguard Worker }
2174*77c1e3ccSAndroid Build Coastguard Worker
write_frame_interp_filter(InterpFilter filter,struct aom_write_bit_buffer * wb)2175*77c1e3ccSAndroid Build Coastguard Worker static inline void write_frame_interp_filter(InterpFilter filter,
2176*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2177*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, filter == SWITCHABLE);
2178*77c1e3ccSAndroid Build Coastguard Worker if (filter != SWITCHABLE)
2179*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, filter, LOG_SWITCHABLE_FILTERS);
2180*77c1e3ccSAndroid Build Coastguard Worker }
2181*77c1e3ccSAndroid Build Coastguard Worker
2182*77c1e3ccSAndroid Build Coastguard Worker // Same function as write_uniform but writing to uncompresses header wb
wb_write_uniform(struct aom_write_bit_buffer * wb,int n,int v)2183*77c1e3ccSAndroid Build Coastguard Worker static inline void wb_write_uniform(struct aom_write_bit_buffer *wb, int n,
2184*77c1e3ccSAndroid Build Coastguard Worker int v) {
2185*77c1e3ccSAndroid Build Coastguard Worker const int l = get_unsigned_bits(n);
2186*77c1e3ccSAndroid Build Coastguard Worker const int m = (1 << l) - n;
2187*77c1e3ccSAndroid Build Coastguard Worker if (l == 0) return;
2188*77c1e3ccSAndroid Build Coastguard Worker if (v < m) {
2189*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, v, l - 1);
2190*77c1e3ccSAndroid Build Coastguard Worker } else {
2191*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, m + ((v - m) >> 1), l - 1);
2192*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, (v - m) & 1, 1);
2193*77c1e3ccSAndroid Build Coastguard Worker }
2194*77c1e3ccSAndroid Build Coastguard Worker }
2195*77c1e3ccSAndroid Build Coastguard Worker
write_tile_info_max_tile(const AV1_COMMON * const cm,struct aom_write_bit_buffer * wb)2196*77c1e3ccSAndroid Build Coastguard Worker static inline void write_tile_info_max_tile(const AV1_COMMON *const cm,
2197*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2198*77c1e3ccSAndroid Build Coastguard Worker int width_sb =
2199*77c1e3ccSAndroid Build Coastguard Worker CEIL_POWER_OF_TWO(cm->mi_params.mi_cols, cm->seq_params->mib_size_log2);
2200*77c1e3ccSAndroid Build Coastguard Worker int height_sb =
2201*77c1e3ccSAndroid Build Coastguard Worker CEIL_POWER_OF_TWO(cm->mi_params.mi_rows, cm->seq_params->mib_size_log2);
2202*77c1e3ccSAndroid Build Coastguard Worker int size_sb, i;
2203*77c1e3ccSAndroid Build Coastguard Worker const CommonTileParams *const tiles = &cm->tiles;
2204*77c1e3ccSAndroid Build Coastguard Worker
2205*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, tiles->uniform_spacing);
2206*77c1e3ccSAndroid Build Coastguard Worker
2207*77c1e3ccSAndroid Build Coastguard Worker if (tiles->uniform_spacing) {
2208*77c1e3ccSAndroid Build Coastguard Worker int ones = tiles->log2_cols - tiles->min_log2_cols;
2209*77c1e3ccSAndroid Build Coastguard Worker while (ones--) {
2210*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 1);
2211*77c1e3ccSAndroid Build Coastguard Worker }
2212*77c1e3ccSAndroid Build Coastguard Worker if (tiles->log2_cols < tiles->max_log2_cols) {
2213*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 0);
2214*77c1e3ccSAndroid Build Coastguard Worker }
2215*77c1e3ccSAndroid Build Coastguard Worker
2216*77c1e3ccSAndroid Build Coastguard Worker // rows
2217*77c1e3ccSAndroid Build Coastguard Worker ones = tiles->log2_rows - tiles->min_log2_rows;
2218*77c1e3ccSAndroid Build Coastguard Worker while (ones--) {
2219*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 1);
2220*77c1e3ccSAndroid Build Coastguard Worker }
2221*77c1e3ccSAndroid Build Coastguard Worker if (tiles->log2_rows < tiles->max_log2_rows) {
2222*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 0);
2223*77c1e3ccSAndroid Build Coastguard Worker }
2224*77c1e3ccSAndroid Build Coastguard Worker } else {
2225*77c1e3ccSAndroid Build Coastguard Worker // Explicit tiles with configurable tile widths and heights
2226*77c1e3ccSAndroid Build Coastguard Worker // columns
2227*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < tiles->cols; i++) {
2228*77c1e3ccSAndroid Build Coastguard Worker size_sb = tiles->col_start_sb[i + 1] - tiles->col_start_sb[i];
2229*77c1e3ccSAndroid Build Coastguard Worker wb_write_uniform(wb, AOMMIN(width_sb, tiles->max_width_sb), size_sb - 1);
2230*77c1e3ccSAndroid Build Coastguard Worker width_sb -= size_sb;
2231*77c1e3ccSAndroid Build Coastguard Worker }
2232*77c1e3ccSAndroid Build Coastguard Worker assert(width_sb == 0);
2233*77c1e3ccSAndroid Build Coastguard Worker
2234*77c1e3ccSAndroid Build Coastguard Worker // rows
2235*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < tiles->rows; i++) {
2236*77c1e3ccSAndroid Build Coastguard Worker size_sb = tiles->row_start_sb[i + 1] - tiles->row_start_sb[i];
2237*77c1e3ccSAndroid Build Coastguard Worker wb_write_uniform(wb, AOMMIN(height_sb, tiles->max_height_sb),
2238*77c1e3ccSAndroid Build Coastguard Worker size_sb - 1);
2239*77c1e3ccSAndroid Build Coastguard Worker height_sb -= size_sb;
2240*77c1e3ccSAndroid Build Coastguard Worker }
2241*77c1e3ccSAndroid Build Coastguard Worker assert(height_sb == 0);
2242*77c1e3ccSAndroid Build Coastguard Worker }
2243*77c1e3ccSAndroid Build Coastguard Worker }
2244*77c1e3ccSAndroid Build Coastguard Worker
write_tile_info(const AV1_COMMON * const cm,struct aom_write_bit_buffer * saved_wb,struct aom_write_bit_buffer * wb)2245*77c1e3ccSAndroid Build Coastguard Worker static inline void write_tile_info(const AV1_COMMON *const cm,
2246*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *saved_wb,
2247*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2248*77c1e3ccSAndroid Build Coastguard Worker write_tile_info_max_tile(cm, wb);
2249*77c1e3ccSAndroid Build Coastguard Worker
2250*77c1e3ccSAndroid Build Coastguard Worker *saved_wb = *wb;
2251*77c1e3ccSAndroid Build Coastguard Worker if (cm->tiles.rows * cm->tiles.cols > 1) {
2252*77c1e3ccSAndroid Build Coastguard Worker // tile id used for cdf update
2253*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, 0, cm->tiles.log2_cols + cm->tiles.log2_rows);
2254*77c1e3ccSAndroid Build Coastguard Worker // Number of bytes in tile size - 1
2255*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, 3, 2);
2256*77c1e3ccSAndroid Build Coastguard Worker }
2257*77c1e3ccSAndroid Build Coastguard Worker }
2258*77c1e3ccSAndroid Build Coastguard Worker
write_ext_tile_info(const AV1_COMMON * const cm,struct aom_write_bit_buffer * saved_wb,struct aom_write_bit_buffer * wb)2259*77c1e3ccSAndroid Build Coastguard Worker static inline void write_ext_tile_info(const AV1_COMMON *const cm,
2260*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *saved_wb,
2261*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2262*77c1e3ccSAndroid Build Coastguard Worker // This information is stored as a separate byte.
2263*77c1e3ccSAndroid Build Coastguard Worker int mod = wb->bit_offset % CHAR_BIT;
2264*77c1e3ccSAndroid Build Coastguard Worker if (mod > 0) aom_wb_write_literal(wb, 0, CHAR_BIT - mod);
2265*77c1e3ccSAndroid Build Coastguard Worker assert(aom_wb_is_byte_aligned(wb));
2266*77c1e3ccSAndroid Build Coastguard Worker
2267*77c1e3ccSAndroid Build Coastguard Worker *saved_wb = *wb;
2268*77c1e3ccSAndroid Build Coastguard Worker if (cm->tiles.rows * cm->tiles.cols > 1) {
2269*77c1e3ccSAndroid Build Coastguard Worker // Note that the last item in the uncompressed header is the data
2270*77c1e3ccSAndroid Build Coastguard Worker // describing tile configuration.
2271*77c1e3ccSAndroid Build Coastguard Worker // Number of bytes in tile column size - 1
2272*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, 0, 2);
2273*77c1e3ccSAndroid Build Coastguard Worker // Number of bytes in tile size - 1
2274*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, 0, 2);
2275*77c1e3ccSAndroid Build Coastguard Worker }
2276*77c1e3ccSAndroid Build Coastguard Worker }
2277*77c1e3ccSAndroid Build Coastguard Worker
find_identical_tile(const int tile_row,const int tile_col,TileBufferEnc (* const tile_buffers)[MAX_TILE_COLS])2278*77c1e3ccSAndroid Build Coastguard Worker static inline int find_identical_tile(
2279*77c1e3ccSAndroid Build Coastguard Worker const int tile_row, const int tile_col,
2280*77c1e3ccSAndroid Build Coastguard Worker TileBufferEnc (*const tile_buffers)[MAX_TILE_COLS]) {
2281*77c1e3ccSAndroid Build Coastguard Worker const MV32 candidate_offset[1] = { { 1, 0 } };
2282*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *const cur_tile_data =
2283*77c1e3ccSAndroid Build Coastguard Worker tile_buffers[tile_row][tile_col].data + 4;
2284*77c1e3ccSAndroid Build Coastguard Worker const size_t cur_tile_size = tile_buffers[tile_row][tile_col].size;
2285*77c1e3ccSAndroid Build Coastguard Worker
2286*77c1e3ccSAndroid Build Coastguard Worker int i;
2287*77c1e3ccSAndroid Build Coastguard Worker
2288*77c1e3ccSAndroid Build Coastguard Worker if (tile_row == 0) return 0;
2289*77c1e3ccSAndroid Build Coastguard Worker
2290*77c1e3ccSAndroid Build Coastguard Worker // (TODO: yunqingwang) For now, only above tile is checked and used.
2291*77c1e3ccSAndroid Build Coastguard Worker // More candidates such as left tile can be added later.
2292*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < 1; i++) {
2293*77c1e3ccSAndroid Build Coastguard Worker int row_offset = candidate_offset[0].row;
2294*77c1e3ccSAndroid Build Coastguard Worker int col_offset = candidate_offset[0].col;
2295*77c1e3ccSAndroid Build Coastguard Worker int row = tile_row - row_offset;
2296*77c1e3ccSAndroid Build Coastguard Worker int col = tile_col - col_offset;
2297*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *tile_data;
2298*77c1e3ccSAndroid Build Coastguard Worker TileBufferEnc *candidate;
2299*77c1e3ccSAndroid Build Coastguard Worker
2300*77c1e3ccSAndroid Build Coastguard Worker if (row < 0 || col < 0) continue;
2301*77c1e3ccSAndroid Build Coastguard Worker
2302*77c1e3ccSAndroid Build Coastguard Worker const uint32_t tile_hdr = mem_get_le32(tile_buffers[row][col].data);
2303*77c1e3ccSAndroid Build Coastguard Worker
2304*77c1e3ccSAndroid Build Coastguard Worker // Read out tile-copy-mode bit:
2305*77c1e3ccSAndroid Build Coastguard Worker if ((tile_hdr >> 31) == 1) {
2306*77c1e3ccSAndroid Build Coastguard Worker // The candidate is a copy tile itself: the offset is stored in bits
2307*77c1e3ccSAndroid Build Coastguard Worker // 30 through 24 inclusive.
2308*77c1e3ccSAndroid Build Coastguard Worker row_offset += (tile_hdr >> 24) & 0x7f;
2309*77c1e3ccSAndroid Build Coastguard Worker row = tile_row - row_offset;
2310*77c1e3ccSAndroid Build Coastguard Worker }
2311*77c1e3ccSAndroid Build Coastguard Worker
2312*77c1e3ccSAndroid Build Coastguard Worker candidate = &tile_buffers[row][col];
2313*77c1e3ccSAndroid Build Coastguard Worker
2314*77c1e3ccSAndroid Build Coastguard Worker if (row_offset >= 128 || candidate->size != cur_tile_size) continue;
2315*77c1e3ccSAndroid Build Coastguard Worker
2316*77c1e3ccSAndroid Build Coastguard Worker tile_data = candidate->data + 4;
2317*77c1e3ccSAndroid Build Coastguard Worker
2318*77c1e3ccSAndroid Build Coastguard Worker if (memcmp(tile_data, cur_tile_data, cur_tile_size) != 0) continue;
2319*77c1e3ccSAndroid Build Coastguard Worker
2320*77c1e3ccSAndroid Build Coastguard Worker // Identical tile found
2321*77c1e3ccSAndroid Build Coastguard Worker assert(row_offset > 0);
2322*77c1e3ccSAndroid Build Coastguard Worker return row_offset;
2323*77c1e3ccSAndroid Build Coastguard Worker }
2324*77c1e3ccSAndroid Build Coastguard Worker
2325*77c1e3ccSAndroid Build Coastguard Worker // No identical tile found
2326*77c1e3ccSAndroid Build Coastguard Worker return 0;
2327*77c1e3ccSAndroid Build Coastguard Worker }
2328*77c1e3ccSAndroid Build Coastguard Worker
write_render_size(const AV1_COMMON * cm,struct aom_write_bit_buffer * wb)2329*77c1e3ccSAndroid Build Coastguard Worker static inline void write_render_size(const AV1_COMMON *cm,
2330*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2331*77c1e3ccSAndroid Build Coastguard Worker const int scaling_active = av1_resize_scaled(cm);
2332*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, scaling_active);
2333*77c1e3ccSAndroid Build Coastguard Worker if (scaling_active) {
2334*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, cm->render_width - 1, 16);
2335*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, cm->render_height - 1, 16);
2336*77c1e3ccSAndroid Build Coastguard Worker }
2337*77c1e3ccSAndroid Build Coastguard Worker }
2338*77c1e3ccSAndroid Build Coastguard Worker
write_superres_scale(const AV1_COMMON * const cm,struct aom_write_bit_buffer * wb)2339*77c1e3ccSAndroid Build Coastguard Worker static inline void write_superres_scale(const AV1_COMMON *const cm,
2340*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2341*77c1e3ccSAndroid Build Coastguard Worker const SequenceHeader *const seq_params = cm->seq_params;
2342*77c1e3ccSAndroid Build Coastguard Worker if (!seq_params->enable_superres) {
2343*77c1e3ccSAndroid Build Coastguard Worker assert(cm->superres_scale_denominator == SCALE_NUMERATOR);
2344*77c1e3ccSAndroid Build Coastguard Worker return;
2345*77c1e3ccSAndroid Build Coastguard Worker }
2346*77c1e3ccSAndroid Build Coastguard Worker
2347*77c1e3ccSAndroid Build Coastguard Worker // First bit is whether to to scale or not
2348*77c1e3ccSAndroid Build Coastguard Worker if (cm->superres_scale_denominator == SCALE_NUMERATOR) {
2349*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 0); // no scaling
2350*77c1e3ccSAndroid Build Coastguard Worker } else {
2351*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 1); // scaling, write scale factor
2352*77c1e3ccSAndroid Build Coastguard Worker assert(cm->superres_scale_denominator >= SUPERRES_SCALE_DENOMINATOR_MIN);
2353*77c1e3ccSAndroid Build Coastguard Worker assert(cm->superres_scale_denominator <
2354*77c1e3ccSAndroid Build Coastguard Worker SUPERRES_SCALE_DENOMINATOR_MIN + (1 << SUPERRES_SCALE_BITS));
2355*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(
2356*77c1e3ccSAndroid Build Coastguard Worker wb, cm->superres_scale_denominator - SUPERRES_SCALE_DENOMINATOR_MIN,
2357*77c1e3ccSAndroid Build Coastguard Worker SUPERRES_SCALE_BITS);
2358*77c1e3ccSAndroid Build Coastguard Worker }
2359*77c1e3ccSAndroid Build Coastguard Worker }
2360*77c1e3ccSAndroid Build Coastguard Worker
write_frame_size(const AV1_COMMON * cm,int frame_size_override,struct aom_write_bit_buffer * wb)2361*77c1e3ccSAndroid Build Coastguard Worker static inline void write_frame_size(const AV1_COMMON *cm,
2362*77c1e3ccSAndroid Build Coastguard Worker int frame_size_override,
2363*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2364*77c1e3ccSAndroid Build Coastguard Worker const int coded_width = cm->superres_upscaled_width - 1;
2365*77c1e3ccSAndroid Build Coastguard Worker const int coded_height = cm->superres_upscaled_height - 1;
2366*77c1e3ccSAndroid Build Coastguard Worker
2367*77c1e3ccSAndroid Build Coastguard Worker if (frame_size_override) {
2368*77c1e3ccSAndroid Build Coastguard Worker const SequenceHeader *seq_params = cm->seq_params;
2369*77c1e3ccSAndroid Build Coastguard Worker int num_bits_width = seq_params->num_bits_width;
2370*77c1e3ccSAndroid Build Coastguard Worker int num_bits_height = seq_params->num_bits_height;
2371*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, coded_width, num_bits_width);
2372*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, coded_height, num_bits_height);
2373*77c1e3ccSAndroid Build Coastguard Worker }
2374*77c1e3ccSAndroid Build Coastguard Worker
2375*77c1e3ccSAndroid Build Coastguard Worker write_superres_scale(cm, wb);
2376*77c1e3ccSAndroid Build Coastguard Worker write_render_size(cm, wb);
2377*77c1e3ccSAndroid Build Coastguard Worker }
2378*77c1e3ccSAndroid Build Coastguard Worker
write_frame_size_with_refs(const AV1_COMMON * const cm,struct aom_write_bit_buffer * wb)2379*77c1e3ccSAndroid Build Coastguard Worker static inline void write_frame_size_with_refs(const AV1_COMMON *const cm,
2380*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2381*77c1e3ccSAndroid Build Coastguard Worker int found = 0;
2382*77c1e3ccSAndroid Build Coastguard Worker
2383*77c1e3ccSAndroid Build Coastguard Worker MV_REFERENCE_FRAME ref_frame;
2384*77c1e3ccSAndroid Build Coastguard Worker for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2385*77c1e3ccSAndroid Build Coastguard Worker const YV12_BUFFER_CONFIG *cfg = get_ref_frame_yv12_buf(cm, ref_frame);
2386*77c1e3ccSAndroid Build Coastguard Worker
2387*77c1e3ccSAndroid Build Coastguard Worker if (cfg != NULL) {
2388*77c1e3ccSAndroid Build Coastguard Worker found = cm->superres_upscaled_width == cfg->y_crop_width &&
2389*77c1e3ccSAndroid Build Coastguard Worker cm->superres_upscaled_height == cfg->y_crop_height;
2390*77c1e3ccSAndroid Build Coastguard Worker found &= cm->render_width == cfg->render_width &&
2391*77c1e3ccSAndroid Build Coastguard Worker cm->render_height == cfg->render_height;
2392*77c1e3ccSAndroid Build Coastguard Worker }
2393*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, found);
2394*77c1e3ccSAndroid Build Coastguard Worker if (found) {
2395*77c1e3ccSAndroid Build Coastguard Worker write_superres_scale(cm, wb);
2396*77c1e3ccSAndroid Build Coastguard Worker break;
2397*77c1e3ccSAndroid Build Coastguard Worker }
2398*77c1e3ccSAndroid Build Coastguard Worker }
2399*77c1e3ccSAndroid Build Coastguard Worker
2400*77c1e3ccSAndroid Build Coastguard Worker if (!found) {
2401*77c1e3ccSAndroid Build Coastguard Worker int frame_size_override = 1; // Always equal to 1 in this function
2402*77c1e3ccSAndroid Build Coastguard Worker write_frame_size(cm, frame_size_override, wb);
2403*77c1e3ccSAndroid Build Coastguard Worker }
2404*77c1e3ccSAndroid Build Coastguard Worker }
2405*77c1e3ccSAndroid Build Coastguard Worker
write_profile(BITSTREAM_PROFILE profile,struct aom_write_bit_buffer * wb)2406*77c1e3ccSAndroid Build Coastguard Worker static inline void write_profile(BITSTREAM_PROFILE profile,
2407*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2408*77c1e3ccSAndroid Build Coastguard Worker assert(profile >= PROFILE_0 && profile < MAX_PROFILES);
2409*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, profile, PROFILE_BITS);
2410*77c1e3ccSAndroid Build Coastguard Worker }
2411*77c1e3ccSAndroid Build Coastguard Worker
write_bitdepth(const SequenceHeader * const seq_params,struct aom_write_bit_buffer * wb)2412*77c1e3ccSAndroid Build Coastguard Worker static inline void write_bitdepth(const SequenceHeader *const seq_params,
2413*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2414*77c1e3ccSAndroid Build Coastguard Worker // Profile 0/1: [0] for 8 bit, [1] 10-bit
2415*77c1e3ccSAndroid Build Coastguard Worker // Profile 2: [0] for 8 bit, [10] 10-bit, [11] - 12-bit
2416*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->bit_depth == AOM_BITS_8 ? 0 : 1);
2417*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->profile == PROFILE_2 && seq_params->bit_depth != AOM_BITS_8) {
2418*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->bit_depth == AOM_BITS_10 ? 0 : 1);
2419*77c1e3ccSAndroid Build Coastguard Worker }
2420*77c1e3ccSAndroid Build Coastguard Worker }
2421*77c1e3ccSAndroid Build Coastguard Worker
write_color_config(const SequenceHeader * const seq_params,struct aom_write_bit_buffer * wb)2422*77c1e3ccSAndroid Build Coastguard Worker static inline void write_color_config(const SequenceHeader *const seq_params,
2423*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2424*77c1e3ccSAndroid Build Coastguard Worker write_bitdepth(seq_params, wb);
2425*77c1e3ccSAndroid Build Coastguard Worker const int is_monochrome = seq_params->monochrome;
2426*77c1e3ccSAndroid Build Coastguard Worker // monochrome bit
2427*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->profile != PROFILE_1)
2428*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, is_monochrome);
2429*77c1e3ccSAndroid Build Coastguard Worker else
2430*77c1e3ccSAndroid Build Coastguard Worker assert(!is_monochrome);
2431*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->color_primaries == AOM_CICP_CP_UNSPECIFIED &&
2432*77c1e3ccSAndroid Build Coastguard Worker seq_params->transfer_characteristics == AOM_CICP_TC_UNSPECIFIED &&
2433*77c1e3ccSAndroid Build Coastguard Worker seq_params->matrix_coefficients == AOM_CICP_MC_UNSPECIFIED) {
2434*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 0); // No color description present
2435*77c1e3ccSAndroid Build Coastguard Worker } else {
2436*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 1); // Color description present
2437*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, seq_params->color_primaries, 8);
2438*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, seq_params->transfer_characteristics, 8);
2439*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, seq_params->matrix_coefficients, 8);
2440*77c1e3ccSAndroid Build Coastguard Worker }
2441*77c1e3ccSAndroid Build Coastguard Worker if (is_monochrome) {
2442*77c1e3ccSAndroid Build Coastguard Worker // 0: [16, 235] (i.e. xvYCC), 1: [0, 255]
2443*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->color_range);
2444*77c1e3ccSAndroid Build Coastguard Worker return;
2445*77c1e3ccSAndroid Build Coastguard Worker }
2446*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->color_primaries == AOM_CICP_CP_BT_709 &&
2447*77c1e3ccSAndroid Build Coastguard Worker seq_params->transfer_characteristics == AOM_CICP_TC_SRGB &&
2448*77c1e3ccSAndroid Build Coastguard Worker seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY) {
2449*77c1e3ccSAndroid Build Coastguard Worker assert(seq_params->subsampling_x == 0 && seq_params->subsampling_y == 0);
2450*77c1e3ccSAndroid Build Coastguard Worker assert(seq_params->profile == PROFILE_1 ||
2451*77c1e3ccSAndroid Build Coastguard Worker (seq_params->profile == PROFILE_2 &&
2452*77c1e3ccSAndroid Build Coastguard Worker seq_params->bit_depth == AOM_BITS_12));
2453*77c1e3ccSAndroid Build Coastguard Worker } else {
2454*77c1e3ccSAndroid Build Coastguard Worker // 0: [16, 235] (i.e. xvYCC), 1: [0, 255]
2455*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->color_range);
2456*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->profile == PROFILE_0) {
2457*77c1e3ccSAndroid Build Coastguard Worker // 420 only
2458*77c1e3ccSAndroid Build Coastguard Worker assert(seq_params->subsampling_x == 1 && seq_params->subsampling_y == 1);
2459*77c1e3ccSAndroid Build Coastguard Worker } else if (seq_params->profile == PROFILE_1) {
2460*77c1e3ccSAndroid Build Coastguard Worker // 444 only
2461*77c1e3ccSAndroid Build Coastguard Worker assert(seq_params->subsampling_x == 0 && seq_params->subsampling_y == 0);
2462*77c1e3ccSAndroid Build Coastguard Worker } else if (seq_params->profile == PROFILE_2) {
2463*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->bit_depth == AOM_BITS_12) {
2464*77c1e3ccSAndroid Build Coastguard Worker // 420, 444 or 422
2465*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->subsampling_x);
2466*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->subsampling_x == 0) {
2467*77c1e3ccSAndroid Build Coastguard Worker assert(seq_params->subsampling_y == 0 &&
2468*77c1e3ccSAndroid Build Coastguard Worker "4:4:0 subsampling not allowed in AV1");
2469*77c1e3ccSAndroid Build Coastguard Worker } else {
2470*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->subsampling_y);
2471*77c1e3ccSAndroid Build Coastguard Worker }
2472*77c1e3ccSAndroid Build Coastguard Worker } else {
2473*77c1e3ccSAndroid Build Coastguard Worker // 422 only
2474*77c1e3ccSAndroid Build Coastguard Worker assert(seq_params->subsampling_x == 1 &&
2475*77c1e3ccSAndroid Build Coastguard Worker seq_params->subsampling_y == 0);
2476*77c1e3ccSAndroid Build Coastguard Worker }
2477*77c1e3ccSAndroid Build Coastguard Worker }
2478*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY) {
2479*77c1e3ccSAndroid Build Coastguard Worker assert(seq_params->subsampling_x == 0 && seq_params->subsampling_y == 0);
2480*77c1e3ccSAndroid Build Coastguard Worker }
2481*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->subsampling_x == 1 && seq_params->subsampling_y == 1) {
2482*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, seq_params->chroma_sample_position, 2);
2483*77c1e3ccSAndroid Build Coastguard Worker }
2484*77c1e3ccSAndroid Build Coastguard Worker }
2485*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->separate_uv_delta_q);
2486*77c1e3ccSAndroid Build Coastguard Worker }
2487*77c1e3ccSAndroid Build Coastguard Worker
write_timing_info_header(const aom_timing_info_t * const timing_info,struct aom_write_bit_buffer * wb)2488*77c1e3ccSAndroid Build Coastguard Worker static inline void write_timing_info_header(
2489*77c1e3ccSAndroid Build Coastguard Worker const aom_timing_info_t *const timing_info,
2490*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2491*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_unsigned_literal(wb, timing_info->num_units_in_display_tick, 32);
2492*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_unsigned_literal(wb, timing_info->time_scale, 32);
2493*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, timing_info->equal_picture_interval);
2494*77c1e3ccSAndroid Build Coastguard Worker if (timing_info->equal_picture_interval) {
2495*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_uvlc(wb, timing_info->num_ticks_per_picture - 1);
2496*77c1e3ccSAndroid Build Coastguard Worker }
2497*77c1e3ccSAndroid Build Coastguard Worker }
2498*77c1e3ccSAndroid Build Coastguard Worker
write_decoder_model_info(const aom_dec_model_info_t * const decoder_model_info,struct aom_write_bit_buffer * wb)2499*77c1e3ccSAndroid Build Coastguard Worker static inline void write_decoder_model_info(
2500*77c1e3ccSAndroid Build Coastguard Worker const aom_dec_model_info_t *const decoder_model_info,
2501*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2502*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(
2503*77c1e3ccSAndroid Build Coastguard Worker wb, decoder_model_info->encoder_decoder_buffer_delay_length - 1, 5);
2504*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_unsigned_literal(
2505*77c1e3ccSAndroid Build Coastguard Worker wb, decoder_model_info->num_units_in_decoding_tick, 32);
2506*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, decoder_model_info->buffer_removal_time_length - 1,
2507*77c1e3ccSAndroid Build Coastguard Worker 5);
2508*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(
2509*77c1e3ccSAndroid Build Coastguard Worker wb, decoder_model_info->frame_presentation_time_length - 1, 5);
2510*77c1e3ccSAndroid Build Coastguard Worker }
2511*77c1e3ccSAndroid Build Coastguard Worker
write_dec_model_op_parameters(const aom_dec_model_op_parameters_t * op_params,int buffer_delay_length,struct aom_write_bit_buffer * wb)2512*77c1e3ccSAndroid Build Coastguard Worker static inline void write_dec_model_op_parameters(
2513*77c1e3ccSAndroid Build Coastguard Worker const aom_dec_model_op_parameters_t *op_params, int buffer_delay_length,
2514*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2515*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_unsigned_literal(wb, op_params->decoder_buffer_delay,
2516*77c1e3ccSAndroid Build Coastguard Worker buffer_delay_length);
2517*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_unsigned_literal(wb, op_params->encoder_buffer_delay,
2518*77c1e3ccSAndroid Build Coastguard Worker buffer_delay_length);
2519*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, op_params->low_delay_mode_flag);
2520*77c1e3ccSAndroid Build Coastguard Worker }
2521*77c1e3ccSAndroid Build Coastguard Worker
write_tu_pts_info(AV1_COMMON * const cm,struct aom_write_bit_buffer * wb)2522*77c1e3ccSAndroid Build Coastguard Worker static inline void write_tu_pts_info(AV1_COMMON *const cm,
2523*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2524*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_unsigned_literal(
2525*77c1e3ccSAndroid Build Coastguard Worker wb, cm->frame_presentation_time,
2526*77c1e3ccSAndroid Build Coastguard Worker cm->seq_params->decoder_model_info.frame_presentation_time_length);
2527*77c1e3ccSAndroid Build Coastguard Worker }
2528*77c1e3ccSAndroid Build Coastguard Worker
write_film_grain_params(const AV1_COMP * const cpi,struct aom_write_bit_buffer * wb)2529*77c1e3ccSAndroid Build Coastguard Worker static inline void write_film_grain_params(const AV1_COMP *const cpi,
2530*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2531*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
2532*77c1e3ccSAndroid Build Coastguard Worker const aom_film_grain_t *const pars = &cm->cur_frame->film_grain_params;
2533*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, pars->apply_grain);
2534*77c1e3ccSAndroid Build Coastguard Worker if (!pars->apply_grain) return;
2535*77c1e3ccSAndroid Build Coastguard Worker
2536*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->random_seed, 16);
2537*77c1e3ccSAndroid Build Coastguard Worker
2538*77c1e3ccSAndroid Build Coastguard Worker if (cm->current_frame.frame_type == INTER_FRAME)
2539*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, pars->update_parameters);
2540*77c1e3ccSAndroid Build Coastguard Worker
2541*77c1e3ccSAndroid Build Coastguard Worker if (!pars->update_parameters) {
2542*77c1e3ccSAndroid Build Coastguard Worker int ref_frame, ref_idx;
2543*77c1e3ccSAndroid Build Coastguard Worker for (ref_frame = LAST_FRAME; ref_frame < REF_FRAMES; ref_frame++) {
2544*77c1e3ccSAndroid Build Coastguard Worker ref_idx = get_ref_frame_map_idx(cm, ref_frame);
2545*77c1e3ccSAndroid Build Coastguard Worker assert(ref_idx != INVALID_IDX);
2546*77c1e3ccSAndroid Build Coastguard Worker const RefCntBuffer *const buf = cm->ref_frame_map[ref_idx];
2547*77c1e3ccSAndroid Build Coastguard Worker if (buf->film_grain_params_present &&
2548*77c1e3ccSAndroid Build Coastguard Worker aom_check_grain_params_equiv(pars, &buf->film_grain_params)) {
2549*77c1e3ccSAndroid Build Coastguard Worker break;
2550*77c1e3ccSAndroid Build Coastguard Worker }
2551*77c1e3ccSAndroid Build Coastguard Worker }
2552*77c1e3ccSAndroid Build Coastguard Worker assert(ref_frame < REF_FRAMES);
2553*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, ref_idx, 3);
2554*77c1e3ccSAndroid Build Coastguard Worker return;
2555*77c1e3ccSAndroid Build Coastguard Worker }
2556*77c1e3ccSAndroid Build Coastguard Worker
2557*77c1e3ccSAndroid Build Coastguard Worker // Scaling functions parameters
2558*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->num_y_points, 4); // max 14
2559*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < pars->num_y_points; i++) {
2560*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->scaling_points_y[i][0], 8);
2561*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->scaling_points_y[i][1], 8);
2562*77c1e3ccSAndroid Build Coastguard Worker }
2563*77c1e3ccSAndroid Build Coastguard Worker
2564*77c1e3ccSAndroid Build Coastguard Worker if (!cm->seq_params->monochrome) {
2565*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, pars->chroma_scaling_from_luma);
2566*77c1e3ccSAndroid Build Coastguard Worker } else {
2567*77c1e3ccSAndroid Build Coastguard Worker assert(!pars->chroma_scaling_from_luma);
2568*77c1e3ccSAndroid Build Coastguard Worker }
2569*77c1e3ccSAndroid Build Coastguard Worker
2570*77c1e3ccSAndroid Build Coastguard Worker if (cm->seq_params->monochrome || pars->chroma_scaling_from_luma ||
2571*77c1e3ccSAndroid Build Coastguard Worker ((cm->seq_params->subsampling_x == 1) &&
2572*77c1e3ccSAndroid Build Coastguard Worker (cm->seq_params->subsampling_y == 1) && (pars->num_y_points == 0))) {
2573*77c1e3ccSAndroid Build Coastguard Worker assert(pars->num_cb_points == 0 && pars->num_cr_points == 0);
2574*77c1e3ccSAndroid Build Coastguard Worker } else {
2575*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->num_cb_points, 4); // max 10
2576*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < pars->num_cb_points; i++) {
2577*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->scaling_points_cb[i][0], 8);
2578*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->scaling_points_cb[i][1], 8);
2579*77c1e3ccSAndroid Build Coastguard Worker }
2580*77c1e3ccSAndroid Build Coastguard Worker
2581*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->num_cr_points, 4); // max 10
2582*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < pars->num_cr_points; i++) {
2583*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->scaling_points_cr[i][0], 8);
2584*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->scaling_points_cr[i][1], 8);
2585*77c1e3ccSAndroid Build Coastguard Worker }
2586*77c1e3ccSAndroid Build Coastguard Worker }
2587*77c1e3ccSAndroid Build Coastguard Worker
2588*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->scaling_shift - 8, 2); // 8 + value
2589*77c1e3ccSAndroid Build Coastguard Worker
2590*77c1e3ccSAndroid Build Coastguard Worker // AR coefficients
2591*77c1e3ccSAndroid Build Coastguard Worker // Only sent if the corresponsing scaling function has
2592*77c1e3ccSAndroid Build Coastguard Worker // more than 0 points
2593*77c1e3ccSAndroid Build Coastguard Worker
2594*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->ar_coeff_lag, 2);
2595*77c1e3ccSAndroid Build Coastguard Worker
2596*77c1e3ccSAndroid Build Coastguard Worker int num_pos_luma = 2 * pars->ar_coeff_lag * (pars->ar_coeff_lag + 1);
2597*77c1e3ccSAndroid Build Coastguard Worker int num_pos_chroma = num_pos_luma;
2598*77c1e3ccSAndroid Build Coastguard Worker if (pars->num_y_points > 0) ++num_pos_chroma;
2599*77c1e3ccSAndroid Build Coastguard Worker
2600*77c1e3ccSAndroid Build Coastguard Worker if (pars->num_y_points)
2601*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < num_pos_luma; i++)
2602*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->ar_coeffs_y[i] + 128, 8);
2603*77c1e3ccSAndroid Build Coastguard Worker
2604*77c1e3ccSAndroid Build Coastguard Worker if (pars->num_cb_points || pars->chroma_scaling_from_luma)
2605*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < num_pos_chroma; i++)
2606*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->ar_coeffs_cb[i] + 128, 8);
2607*77c1e3ccSAndroid Build Coastguard Worker
2608*77c1e3ccSAndroid Build Coastguard Worker if (pars->num_cr_points || pars->chroma_scaling_from_luma)
2609*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < num_pos_chroma; i++)
2610*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->ar_coeffs_cr[i] + 128, 8);
2611*77c1e3ccSAndroid Build Coastguard Worker
2612*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->ar_coeff_shift - 6, 2); // 8 + value
2613*77c1e3ccSAndroid Build Coastguard Worker
2614*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->grain_scale_shift, 2);
2615*77c1e3ccSAndroid Build Coastguard Worker
2616*77c1e3ccSAndroid Build Coastguard Worker if (pars->num_cb_points) {
2617*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->cb_mult, 8);
2618*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->cb_luma_mult, 8);
2619*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->cb_offset, 9);
2620*77c1e3ccSAndroid Build Coastguard Worker }
2621*77c1e3ccSAndroid Build Coastguard Worker
2622*77c1e3ccSAndroid Build Coastguard Worker if (pars->num_cr_points) {
2623*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->cr_mult, 8);
2624*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->cr_luma_mult, 8);
2625*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, pars->cr_offset, 9);
2626*77c1e3ccSAndroid Build Coastguard Worker }
2627*77c1e3ccSAndroid Build Coastguard Worker
2628*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, pars->overlap_flag);
2629*77c1e3ccSAndroid Build Coastguard Worker
2630*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, pars->clip_to_restricted_range);
2631*77c1e3ccSAndroid Build Coastguard Worker }
2632*77c1e3ccSAndroid Build Coastguard Worker
write_sb_size(const SequenceHeader * const seq_params,struct aom_write_bit_buffer * wb)2633*77c1e3ccSAndroid Build Coastguard Worker static inline void write_sb_size(const SequenceHeader *const seq_params,
2634*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2635*77c1e3ccSAndroid Build Coastguard Worker (void)seq_params;
2636*77c1e3ccSAndroid Build Coastguard Worker (void)wb;
2637*77c1e3ccSAndroid Build Coastguard Worker assert(seq_params->mib_size == mi_size_wide[seq_params->sb_size]);
2638*77c1e3ccSAndroid Build Coastguard Worker assert(seq_params->mib_size == 1 << seq_params->mib_size_log2);
2639*77c1e3ccSAndroid Build Coastguard Worker assert(seq_params->sb_size == BLOCK_128X128 ||
2640*77c1e3ccSAndroid Build Coastguard Worker seq_params->sb_size == BLOCK_64X64);
2641*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->sb_size == BLOCK_128X128 ? 1 : 0);
2642*77c1e3ccSAndroid Build Coastguard Worker }
2643*77c1e3ccSAndroid Build Coastguard Worker
write_sequence_header(const SequenceHeader * const seq_params,struct aom_write_bit_buffer * wb)2644*77c1e3ccSAndroid Build Coastguard Worker static inline void write_sequence_header(const SequenceHeader *const seq_params,
2645*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2646*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, seq_params->num_bits_width - 1, 4);
2647*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, seq_params->num_bits_height - 1, 4);
2648*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, seq_params->max_frame_width - 1,
2649*77c1e3ccSAndroid Build Coastguard Worker seq_params->num_bits_width);
2650*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, seq_params->max_frame_height - 1,
2651*77c1e3ccSAndroid Build Coastguard Worker seq_params->num_bits_height);
2652*77c1e3ccSAndroid Build Coastguard Worker
2653*77c1e3ccSAndroid Build Coastguard Worker if (!seq_params->reduced_still_picture_hdr) {
2654*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->frame_id_numbers_present_flag);
2655*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->frame_id_numbers_present_flag) {
2656*77c1e3ccSAndroid Build Coastguard Worker // We must always have delta_frame_id_length < frame_id_length,
2657*77c1e3ccSAndroid Build Coastguard Worker // in order for a frame to be referenced with a unique delta.
2658*77c1e3ccSAndroid Build Coastguard Worker // Avoid wasting bits by using a coding that enforces this restriction.
2659*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, seq_params->delta_frame_id_length - 2, 4);
2660*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(
2661*77c1e3ccSAndroid Build Coastguard Worker wb,
2662*77c1e3ccSAndroid Build Coastguard Worker seq_params->frame_id_length - seq_params->delta_frame_id_length - 1,
2663*77c1e3ccSAndroid Build Coastguard Worker 3);
2664*77c1e3ccSAndroid Build Coastguard Worker }
2665*77c1e3ccSAndroid Build Coastguard Worker }
2666*77c1e3ccSAndroid Build Coastguard Worker
2667*77c1e3ccSAndroid Build Coastguard Worker write_sb_size(seq_params, wb);
2668*77c1e3ccSAndroid Build Coastguard Worker
2669*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->enable_filter_intra);
2670*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->enable_intra_edge_filter);
2671*77c1e3ccSAndroid Build Coastguard Worker
2672*77c1e3ccSAndroid Build Coastguard Worker if (!seq_params->reduced_still_picture_hdr) {
2673*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->enable_interintra_compound);
2674*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->enable_masked_compound);
2675*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->enable_warped_motion);
2676*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->enable_dual_filter);
2677*77c1e3ccSAndroid Build Coastguard Worker
2678*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->order_hint_info.enable_order_hint);
2679*77c1e3ccSAndroid Build Coastguard Worker
2680*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->order_hint_info.enable_order_hint) {
2681*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->order_hint_info.enable_dist_wtd_comp);
2682*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->order_hint_info.enable_ref_frame_mvs);
2683*77c1e3ccSAndroid Build Coastguard Worker }
2684*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->force_screen_content_tools == 2) {
2685*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 1);
2686*77c1e3ccSAndroid Build Coastguard Worker } else {
2687*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 0);
2688*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->force_screen_content_tools);
2689*77c1e3ccSAndroid Build Coastguard Worker }
2690*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->force_screen_content_tools > 0) {
2691*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->force_integer_mv == 2) {
2692*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 1);
2693*77c1e3ccSAndroid Build Coastguard Worker } else {
2694*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 0);
2695*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->force_integer_mv);
2696*77c1e3ccSAndroid Build Coastguard Worker }
2697*77c1e3ccSAndroid Build Coastguard Worker } else {
2698*77c1e3ccSAndroid Build Coastguard Worker assert(seq_params->force_integer_mv == 2);
2699*77c1e3ccSAndroid Build Coastguard Worker }
2700*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->order_hint_info.enable_order_hint)
2701*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(
2702*77c1e3ccSAndroid Build Coastguard Worker wb, seq_params->order_hint_info.order_hint_bits_minus_1, 3);
2703*77c1e3ccSAndroid Build Coastguard Worker }
2704*77c1e3ccSAndroid Build Coastguard Worker
2705*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->enable_superres);
2706*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->enable_cdef);
2707*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, seq_params->enable_restoration);
2708*77c1e3ccSAndroid Build Coastguard Worker }
2709*77c1e3ccSAndroid Build Coastguard Worker
write_global_motion_params(const WarpedMotionParams * params,const WarpedMotionParams * ref_params,struct aom_write_bit_buffer * wb,int allow_hp)2710*77c1e3ccSAndroid Build Coastguard Worker static inline void write_global_motion_params(
2711*77c1e3ccSAndroid Build Coastguard Worker const WarpedMotionParams *params, const WarpedMotionParams *ref_params,
2712*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb, int allow_hp) {
2713*77c1e3ccSAndroid Build Coastguard Worker const TransformationType type = params->wmtype;
2714*77c1e3ccSAndroid Build Coastguard Worker
2715*77c1e3ccSAndroid Build Coastguard Worker // As a workaround for an AV1 spec bug, we avoid choosing TRANSLATION
2716*77c1e3ccSAndroid Build Coastguard Worker // type models. Check here that we don't accidentally pick one somehow.
2717*77c1e3ccSAndroid Build Coastguard Worker // See comments in gm_get_motion_vector() for details on the bug we're
2718*77c1e3ccSAndroid Build Coastguard Worker // working around here
2719*77c1e3ccSAndroid Build Coastguard Worker assert(type != TRANSLATION);
2720*77c1e3ccSAndroid Build Coastguard Worker
2721*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, type != IDENTITY);
2722*77c1e3ccSAndroid Build Coastguard Worker if (type != IDENTITY) {
2723*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, type == ROTZOOM);
2724*77c1e3ccSAndroid Build Coastguard Worker if (type != ROTZOOM) aom_wb_write_bit(wb, type == TRANSLATION);
2725*77c1e3ccSAndroid Build Coastguard Worker }
2726*77c1e3ccSAndroid Build Coastguard Worker
2727*77c1e3ccSAndroid Build Coastguard Worker if (type >= ROTZOOM) {
2728*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_signed_primitive_refsubexpfin(
2729*77c1e3ccSAndroid Build Coastguard Worker wb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
2730*77c1e3ccSAndroid Build Coastguard Worker (ref_params->wmmat[2] >> GM_ALPHA_PREC_DIFF) -
2731*77c1e3ccSAndroid Build Coastguard Worker (1 << GM_ALPHA_PREC_BITS),
2732*77c1e3ccSAndroid Build Coastguard Worker (params->wmmat[2] >> GM_ALPHA_PREC_DIFF) - (1 << GM_ALPHA_PREC_BITS));
2733*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_signed_primitive_refsubexpfin(
2734*77c1e3ccSAndroid Build Coastguard Worker wb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
2735*77c1e3ccSAndroid Build Coastguard Worker (ref_params->wmmat[3] >> GM_ALPHA_PREC_DIFF),
2736*77c1e3ccSAndroid Build Coastguard Worker (params->wmmat[3] >> GM_ALPHA_PREC_DIFF));
2737*77c1e3ccSAndroid Build Coastguard Worker }
2738*77c1e3ccSAndroid Build Coastguard Worker
2739*77c1e3ccSAndroid Build Coastguard Worker if (type >= AFFINE) {
2740*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_signed_primitive_refsubexpfin(
2741*77c1e3ccSAndroid Build Coastguard Worker wb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
2742*77c1e3ccSAndroid Build Coastguard Worker (ref_params->wmmat[4] >> GM_ALPHA_PREC_DIFF),
2743*77c1e3ccSAndroid Build Coastguard Worker (params->wmmat[4] >> GM_ALPHA_PREC_DIFF));
2744*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_signed_primitive_refsubexpfin(
2745*77c1e3ccSAndroid Build Coastguard Worker wb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
2746*77c1e3ccSAndroid Build Coastguard Worker (ref_params->wmmat[5] >> GM_ALPHA_PREC_DIFF) -
2747*77c1e3ccSAndroid Build Coastguard Worker (1 << GM_ALPHA_PREC_BITS),
2748*77c1e3ccSAndroid Build Coastguard Worker (params->wmmat[5] >> GM_ALPHA_PREC_DIFF) - (1 << GM_ALPHA_PREC_BITS));
2749*77c1e3ccSAndroid Build Coastguard Worker }
2750*77c1e3ccSAndroid Build Coastguard Worker
2751*77c1e3ccSAndroid Build Coastguard Worker if (type >= TRANSLATION) {
2752*77c1e3ccSAndroid Build Coastguard Worker const int trans_bits = (type == TRANSLATION)
2753*77c1e3ccSAndroid Build Coastguard Worker ? GM_ABS_TRANS_ONLY_BITS - !allow_hp
2754*77c1e3ccSAndroid Build Coastguard Worker : GM_ABS_TRANS_BITS;
2755*77c1e3ccSAndroid Build Coastguard Worker const int trans_prec_diff = (type == TRANSLATION)
2756*77c1e3ccSAndroid Build Coastguard Worker ? GM_TRANS_ONLY_PREC_DIFF + !allow_hp
2757*77c1e3ccSAndroid Build Coastguard Worker : GM_TRANS_PREC_DIFF;
2758*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_signed_primitive_refsubexpfin(
2759*77c1e3ccSAndroid Build Coastguard Worker wb, (1 << trans_bits) + 1, SUBEXPFIN_K,
2760*77c1e3ccSAndroid Build Coastguard Worker (ref_params->wmmat[0] >> trans_prec_diff),
2761*77c1e3ccSAndroid Build Coastguard Worker (params->wmmat[0] >> trans_prec_diff));
2762*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_signed_primitive_refsubexpfin(
2763*77c1e3ccSAndroid Build Coastguard Worker wb, (1 << trans_bits) + 1, SUBEXPFIN_K,
2764*77c1e3ccSAndroid Build Coastguard Worker (ref_params->wmmat[1] >> trans_prec_diff),
2765*77c1e3ccSAndroid Build Coastguard Worker (params->wmmat[1] >> trans_prec_diff));
2766*77c1e3ccSAndroid Build Coastguard Worker }
2767*77c1e3ccSAndroid Build Coastguard Worker }
2768*77c1e3ccSAndroid Build Coastguard Worker
write_global_motion(AV1_COMP * cpi,struct aom_write_bit_buffer * wb)2769*77c1e3ccSAndroid Build Coastguard Worker static inline void write_global_motion(AV1_COMP *cpi,
2770*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2771*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
2772*77c1e3ccSAndroid Build Coastguard Worker int frame;
2773*77c1e3ccSAndroid Build Coastguard Worker for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
2774*77c1e3ccSAndroid Build Coastguard Worker const WarpedMotionParams *ref_params =
2775*77c1e3ccSAndroid Build Coastguard Worker cm->prev_frame ? &cm->prev_frame->global_motion[frame]
2776*77c1e3ccSAndroid Build Coastguard Worker : &default_warp_params;
2777*77c1e3ccSAndroid Build Coastguard Worker write_global_motion_params(&cm->global_motion[frame], ref_params, wb,
2778*77c1e3ccSAndroid Build Coastguard Worker cm->features.allow_high_precision_mv);
2779*77c1e3ccSAndroid Build Coastguard Worker // TODO(sarahparker, debargha): The logic in the commented out code below
2780*77c1e3ccSAndroid Build Coastguard Worker // does not work currently and causes mismatches when resize is on.
2781*77c1e3ccSAndroid Build Coastguard Worker // Fix it before turning the optimization back on.
2782*77c1e3ccSAndroid Build Coastguard Worker /*
2783*77c1e3ccSAndroid Build Coastguard Worker YV12_BUFFER_CONFIG *ref_buf = get_ref_frame_yv12_buf(cpi, frame);
2784*77c1e3ccSAndroid Build Coastguard Worker if (cpi->source->y_crop_width == ref_buf->y_crop_width &&
2785*77c1e3ccSAndroid Build Coastguard Worker cpi->source->y_crop_height == ref_buf->y_crop_height) {
2786*77c1e3ccSAndroid Build Coastguard Worker write_global_motion_params(&cm->global_motion[frame],
2787*77c1e3ccSAndroid Build Coastguard Worker &cm->prev_frame->global_motion[frame], wb,
2788*77c1e3ccSAndroid Build Coastguard Worker cm->features.allow_high_precision_mv);
2789*77c1e3ccSAndroid Build Coastguard Worker } else {
2790*77c1e3ccSAndroid Build Coastguard Worker assert(cm->global_motion[frame].wmtype == IDENTITY &&
2791*77c1e3ccSAndroid Build Coastguard Worker "Invalid warp type for frames of different resolutions");
2792*77c1e3ccSAndroid Build Coastguard Worker }
2793*77c1e3ccSAndroid Build Coastguard Worker */
2794*77c1e3ccSAndroid Build Coastguard Worker /*
2795*77c1e3ccSAndroid Build Coastguard Worker printf("Frame %d/%d: Enc Ref %d: %d %d %d %d\n",
2796*77c1e3ccSAndroid Build Coastguard Worker cm->current_frame.frame_number, cm->show_frame, frame,
2797*77c1e3ccSAndroid Build Coastguard Worker cm->global_motion[frame].wmmat[0],
2798*77c1e3ccSAndroid Build Coastguard Worker cm->global_motion[frame].wmmat[1], cm->global_motion[frame].wmmat[2],
2799*77c1e3ccSAndroid Build Coastguard Worker cm->global_motion[frame].wmmat[3]);
2800*77c1e3ccSAndroid Build Coastguard Worker */
2801*77c1e3ccSAndroid Build Coastguard Worker }
2802*77c1e3ccSAndroid Build Coastguard Worker }
2803*77c1e3ccSAndroid Build Coastguard Worker
check_frame_refs_short_signaling(AV1_COMMON * const cm,bool enable_ref_short_signaling)2804*77c1e3ccSAndroid Build Coastguard Worker static int check_frame_refs_short_signaling(AV1_COMMON *const cm,
2805*77c1e3ccSAndroid Build Coastguard Worker bool enable_ref_short_signaling) {
2806*77c1e3ccSAndroid Build Coastguard Worker // In rtc case when res < 360p and speed >= 9, we turn on
2807*77c1e3ccSAndroid Build Coastguard Worker // frame_refs_short_signaling if it won't break the decoder.
2808*77c1e3ccSAndroid Build Coastguard Worker if (enable_ref_short_signaling) {
2809*77c1e3ccSAndroid Build Coastguard Worker const int gld_map_idx = get_ref_frame_map_idx(cm, GOLDEN_FRAME);
2810*77c1e3ccSAndroid Build Coastguard Worker const int base =
2811*77c1e3ccSAndroid Build Coastguard Worker 1 << (cm->seq_params->order_hint_info.order_hint_bits_minus_1 + 1);
2812*77c1e3ccSAndroid Build Coastguard Worker
2813*77c1e3ccSAndroid Build Coastguard Worker const int order_hint_group_cur =
2814*77c1e3ccSAndroid Build Coastguard Worker cm->current_frame.display_order_hint / base;
2815*77c1e3ccSAndroid Build Coastguard Worker const int order_hint_group_gld =
2816*77c1e3ccSAndroid Build Coastguard Worker cm->ref_frame_map[gld_map_idx]->display_order_hint / base;
2817*77c1e3ccSAndroid Build Coastguard Worker const int relative_dist = cm->current_frame.order_hint -
2818*77c1e3ccSAndroid Build Coastguard Worker cm->ref_frame_map[gld_map_idx]->order_hint;
2819*77c1e3ccSAndroid Build Coastguard Worker
2820*77c1e3ccSAndroid Build Coastguard Worker // If current frame and GOLDEN frame are in the same order_hint group, and
2821*77c1e3ccSAndroid Build Coastguard Worker // they are not far apart (i.e., > 64 frames), then return 1.
2822*77c1e3ccSAndroid Build Coastguard Worker if (order_hint_group_cur == order_hint_group_gld && relative_dist >= 0 &&
2823*77c1e3ccSAndroid Build Coastguard Worker relative_dist <= 64) {
2824*77c1e3ccSAndroid Build Coastguard Worker return 1;
2825*77c1e3ccSAndroid Build Coastguard Worker }
2826*77c1e3ccSAndroid Build Coastguard Worker return 0;
2827*77c1e3ccSAndroid Build Coastguard Worker }
2828*77c1e3ccSAndroid Build Coastguard Worker
2829*77c1e3ccSAndroid Build Coastguard Worker // Check whether all references are distinct frames.
2830*77c1e3ccSAndroid Build Coastguard Worker const RefCntBuffer *seen_bufs[INTER_REFS_PER_FRAME] = { NULL };
2831*77c1e3ccSAndroid Build Coastguard Worker int num_refs = 0;
2832*77c1e3ccSAndroid Build Coastguard Worker for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2833*77c1e3ccSAndroid Build Coastguard Worker const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
2834*77c1e3ccSAndroid Build Coastguard Worker if (buf != NULL) {
2835*77c1e3ccSAndroid Build Coastguard Worker int seen = 0;
2836*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < num_refs; i++) {
2837*77c1e3ccSAndroid Build Coastguard Worker if (seen_bufs[i] == buf) {
2838*77c1e3ccSAndroid Build Coastguard Worker seen = 1;
2839*77c1e3ccSAndroid Build Coastguard Worker break;
2840*77c1e3ccSAndroid Build Coastguard Worker }
2841*77c1e3ccSAndroid Build Coastguard Worker }
2842*77c1e3ccSAndroid Build Coastguard Worker if (!seen) seen_bufs[num_refs++] = buf;
2843*77c1e3ccSAndroid Build Coastguard Worker }
2844*77c1e3ccSAndroid Build Coastguard Worker }
2845*77c1e3ccSAndroid Build Coastguard Worker
2846*77c1e3ccSAndroid Build Coastguard Worker // We only turn on frame_refs_short_signaling when all references are
2847*77c1e3ccSAndroid Build Coastguard Worker // distinct.
2848*77c1e3ccSAndroid Build Coastguard Worker if (num_refs < INTER_REFS_PER_FRAME) {
2849*77c1e3ccSAndroid Build Coastguard Worker // It indicates that there exist more than one reference frame pointing to
2850*77c1e3ccSAndroid Build Coastguard Worker // the same reference buffer, i.e. two or more references are duplicate.
2851*77c1e3ccSAndroid Build Coastguard Worker return 0;
2852*77c1e3ccSAndroid Build Coastguard Worker }
2853*77c1e3ccSAndroid Build Coastguard Worker
2854*77c1e3ccSAndroid Build Coastguard Worker // Check whether the encoder side ref frame choices are aligned with that to
2855*77c1e3ccSAndroid Build Coastguard Worker // be derived at the decoder side.
2856*77c1e3ccSAndroid Build Coastguard Worker int remapped_ref_idx_decoder[REF_FRAMES];
2857*77c1e3ccSAndroid Build Coastguard Worker
2858*77c1e3ccSAndroid Build Coastguard Worker const int lst_map_idx = get_ref_frame_map_idx(cm, LAST_FRAME);
2859*77c1e3ccSAndroid Build Coastguard Worker const int gld_map_idx = get_ref_frame_map_idx(cm, GOLDEN_FRAME);
2860*77c1e3ccSAndroid Build Coastguard Worker
2861*77c1e3ccSAndroid Build Coastguard Worker // Set up the frame refs mapping indexes according to the
2862*77c1e3ccSAndroid Build Coastguard Worker // frame_refs_short_signaling policy.
2863*77c1e3ccSAndroid Build Coastguard Worker av1_set_frame_refs(cm, remapped_ref_idx_decoder, lst_map_idx, gld_map_idx);
2864*77c1e3ccSAndroid Build Coastguard Worker
2865*77c1e3ccSAndroid Build Coastguard Worker // We only turn on frame_refs_short_signaling when the encoder side decision
2866*77c1e3ccSAndroid Build Coastguard Worker // on ref frames is identical to that at the decoder side.
2867*77c1e3ccSAndroid Build Coastguard Worker int frame_refs_short_signaling = 1;
2868*77c1e3ccSAndroid Build Coastguard Worker for (int ref_idx = 0; ref_idx < INTER_REFS_PER_FRAME; ++ref_idx) {
2869*77c1e3ccSAndroid Build Coastguard Worker // Compare the buffer index between two reference frames indexed
2870*77c1e3ccSAndroid Build Coastguard Worker // respectively by the encoder and the decoder side decisions.
2871*77c1e3ccSAndroid Build Coastguard Worker RefCntBuffer *ref_frame_buf_new = NULL;
2872*77c1e3ccSAndroid Build Coastguard Worker if (remapped_ref_idx_decoder[ref_idx] != INVALID_IDX) {
2873*77c1e3ccSAndroid Build Coastguard Worker ref_frame_buf_new = cm->ref_frame_map[remapped_ref_idx_decoder[ref_idx]];
2874*77c1e3ccSAndroid Build Coastguard Worker }
2875*77c1e3ccSAndroid Build Coastguard Worker if (get_ref_frame_buf(cm, LAST_FRAME + ref_idx) != ref_frame_buf_new) {
2876*77c1e3ccSAndroid Build Coastguard Worker frame_refs_short_signaling = 0;
2877*77c1e3ccSAndroid Build Coastguard Worker break;
2878*77c1e3ccSAndroid Build Coastguard Worker }
2879*77c1e3ccSAndroid Build Coastguard Worker }
2880*77c1e3ccSAndroid Build Coastguard Worker
2881*77c1e3ccSAndroid Build Coastguard Worker #if 0 // For debug
2882*77c1e3ccSAndroid Build Coastguard Worker printf("\nFrame=%d: \n", cm->current_frame.frame_number);
2883*77c1e3ccSAndroid Build Coastguard Worker printf("***frame_refs_short_signaling=%d\n", frame_refs_short_signaling);
2884*77c1e3ccSAndroid Build Coastguard Worker for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2885*77c1e3ccSAndroid Build Coastguard Worker printf("enc_ref(map_idx=%d)=%d, vs. "
2886*77c1e3ccSAndroid Build Coastguard Worker "dec_ref(map_idx=%d)=%d\n",
2887*77c1e3ccSAndroid Build Coastguard Worker get_ref_frame_map_idx(cm, ref_frame), ref_frame,
2888*77c1e3ccSAndroid Build Coastguard Worker cm->remapped_ref_idx[ref_frame - LAST_FRAME],
2889*77c1e3ccSAndroid Build Coastguard Worker ref_frame);
2890*77c1e3ccSAndroid Build Coastguard Worker }
2891*77c1e3ccSAndroid Build Coastguard Worker #endif // 0
2892*77c1e3ccSAndroid Build Coastguard Worker
2893*77c1e3ccSAndroid Build Coastguard Worker return frame_refs_short_signaling;
2894*77c1e3ccSAndroid Build Coastguard Worker }
2895*77c1e3ccSAndroid Build Coastguard Worker
2896*77c1e3ccSAndroid Build Coastguard Worker // New function based on HLS R18
write_uncompressed_header_obu(AV1_COMP * cpi,MACROBLOCKD * const xd,struct aom_write_bit_buffer * saved_wb,struct aom_write_bit_buffer * wb)2897*77c1e3ccSAndroid Build Coastguard Worker static inline void write_uncompressed_header_obu(
2898*77c1e3ccSAndroid Build Coastguard Worker AV1_COMP *cpi, MACROBLOCKD *const xd, struct aom_write_bit_buffer *saved_wb,
2899*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
2900*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
2901*77c1e3ccSAndroid Build Coastguard Worker const SequenceHeader *const seq_params = cm->seq_params;
2902*77c1e3ccSAndroid Build Coastguard Worker const CommonQuantParams *quant_params = &cm->quant_params;
2903*77c1e3ccSAndroid Build Coastguard Worker CurrentFrame *const current_frame = &cm->current_frame;
2904*77c1e3ccSAndroid Build Coastguard Worker FeatureFlags *const features = &cm->features;
2905*77c1e3ccSAndroid Build Coastguard Worker
2906*77c1e3ccSAndroid Build Coastguard Worker if (!cpi->sf.rt_sf.enable_ref_short_signaling ||
2907*77c1e3ccSAndroid Build Coastguard Worker !seq_params->order_hint_info.enable_order_hint ||
2908*77c1e3ccSAndroid Build Coastguard Worker seq_params->order_hint_info.enable_ref_frame_mvs) {
2909*77c1e3ccSAndroid Build Coastguard Worker current_frame->frame_refs_short_signaling = 0;
2910*77c1e3ccSAndroid Build Coastguard Worker } else {
2911*77c1e3ccSAndroid Build Coastguard Worker current_frame->frame_refs_short_signaling = 1;
2912*77c1e3ccSAndroid Build Coastguard Worker }
2913*77c1e3ccSAndroid Build Coastguard Worker
2914*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->still_picture) {
2915*77c1e3ccSAndroid Build Coastguard Worker assert(cm->show_existing_frame == 0);
2916*77c1e3ccSAndroid Build Coastguard Worker assert(cm->show_frame == 1);
2917*77c1e3ccSAndroid Build Coastguard Worker assert(current_frame->frame_type == KEY_FRAME);
2918*77c1e3ccSAndroid Build Coastguard Worker }
2919*77c1e3ccSAndroid Build Coastguard Worker if (!seq_params->reduced_still_picture_hdr) {
2920*77c1e3ccSAndroid Build Coastguard Worker if (encode_show_existing_frame(cm)) {
2921*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 1); // show_existing_frame
2922*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, cpi->existing_fb_idx_to_show, 3);
2923*77c1e3ccSAndroid Build Coastguard Worker
2924*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->decoder_model_info_present_flag &&
2925*77c1e3ccSAndroid Build Coastguard Worker seq_params->timing_info.equal_picture_interval == 0) {
2926*77c1e3ccSAndroid Build Coastguard Worker write_tu_pts_info(cm, wb);
2927*77c1e3ccSAndroid Build Coastguard Worker }
2928*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->frame_id_numbers_present_flag) {
2929*77c1e3ccSAndroid Build Coastguard Worker int frame_id_len = seq_params->frame_id_length;
2930*77c1e3ccSAndroid Build Coastguard Worker int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
2931*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, display_frame_id, frame_id_len);
2932*77c1e3ccSAndroid Build Coastguard Worker }
2933*77c1e3ccSAndroid Build Coastguard Worker return;
2934*77c1e3ccSAndroid Build Coastguard Worker } else {
2935*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 0); // show_existing_frame
2936*77c1e3ccSAndroid Build Coastguard Worker }
2937*77c1e3ccSAndroid Build Coastguard Worker
2938*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, current_frame->frame_type, 2);
2939*77c1e3ccSAndroid Build Coastguard Worker
2940*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, cm->show_frame);
2941*77c1e3ccSAndroid Build Coastguard Worker if (cm->show_frame) {
2942*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->decoder_model_info_present_flag &&
2943*77c1e3ccSAndroid Build Coastguard Worker seq_params->timing_info.equal_picture_interval == 0)
2944*77c1e3ccSAndroid Build Coastguard Worker write_tu_pts_info(cm, wb);
2945*77c1e3ccSAndroid Build Coastguard Worker } else {
2946*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, cm->showable_frame);
2947*77c1e3ccSAndroid Build Coastguard Worker }
2948*77c1e3ccSAndroid Build Coastguard Worker if (frame_is_sframe(cm)) {
2949*77c1e3ccSAndroid Build Coastguard Worker assert(features->error_resilient_mode);
2950*77c1e3ccSAndroid Build Coastguard Worker } else if (!(current_frame->frame_type == KEY_FRAME && cm->show_frame)) {
2951*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, features->error_resilient_mode);
2952*77c1e3ccSAndroid Build Coastguard Worker }
2953*77c1e3ccSAndroid Build Coastguard Worker }
2954*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, features->disable_cdf_update);
2955*77c1e3ccSAndroid Build Coastguard Worker
2956*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->force_screen_content_tools == 2) {
2957*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, features->allow_screen_content_tools);
2958*77c1e3ccSAndroid Build Coastguard Worker } else {
2959*77c1e3ccSAndroid Build Coastguard Worker assert(features->allow_screen_content_tools ==
2960*77c1e3ccSAndroid Build Coastguard Worker seq_params->force_screen_content_tools);
2961*77c1e3ccSAndroid Build Coastguard Worker }
2962*77c1e3ccSAndroid Build Coastguard Worker
2963*77c1e3ccSAndroid Build Coastguard Worker if (features->allow_screen_content_tools) {
2964*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->force_integer_mv == 2) {
2965*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, features->cur_frame_force_integer_mv);
2966*77c1e3ccSAndroid Build Coastguard Worker } else {
2967*77c1e3ccSAndroid Build Coastguard Worker assert(features->cur_frame_force_integer_mv ==
2968*77c1e3ccSAndroid Build Coastguard Worker seq_params->force_integer_mv);
2969*77c1e3ccSAndroid Build Coastguard Worker }
2970*77c1e3ccSAndroid Build Coastguard Worker } else {
2971*77c1e3ccSAndroid Build Coastguard Worker assert(features->cur_frame_force_integer_mv == 0);
2972*77c1e3ccSAndroid Build Coastguard Worker }
2973*77c1e3ccSAndroid Build Coastguard Worker
2974*77c1e3ccSAndroid Build Coastguard Worker int frame_size_override_flag = 0;
2975*77c1e3ccSAndroid Build Coastguard Worker
2976*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->reduced_still_picture_hdr) {
2977*77c1e3ccSAndroid Build Coastguard Worker assert(cm->superres_upscaled_width == seq_params->max_frame_width &&
2978*77c1e3ccSAndroid Build Coastguard Worker cm->superres_upscaled_height == seq_params->max_frame_height);
2979*77c1e3ccSAndroid Build Coastguard Worker } else {
2980*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->frame_id_numbers_present_flag) {
2981*77c1e3ccSAndroid Build Coastguard Worker int frame_id_len = seq_params->frame_id_length;
2982*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, cm->current_frame_id, frame_id_len);
2983*77c1e3ccSAndroid Build Coastguard Worker }
2984*77c1e3ccSAndroid Build Coastguard Worker
2985*77c1e3ccSAndroid Build Coastguard Worker if (cm->superres_upscaled_width > seq_params->max_frame_width ||
2986*77c1e3ccSAndroid Build Coastguard Worker cm->superres_upscaled_height > seq_params->max_frame_height) {
2987*77c1e3ccSAndroid Build Coastguard Worker aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
2988*77c1e3ccSAndroid Build Coastguard Worker "Frame dimensions are larger than the maximum values");
2989*77c1e3ccSAndroid Build Coastguard Worker }
2990*77c1e3ccSAndroid Build Coastguard Worker
2991*77c1e3ccSAndroid Build Coastguard Worker frame_size_override_flag =
2992*77c1e3ccSAndroid Build Coastguard Worker frame_is_sframe(cm)
2993*77c1e3ccSAndroid Build Coastguard Worker ? 1
2994*77c1e3ccSAndroid Build Coastguard Worker : (cm->superres_upscaled_width != seq_params->max_frame_width ||
2995*77c1e3ccSAndroid Build Coastguard Worker cm->superres_upscaled_height != seq_params->max_frame_height);
2996*77c1e3ccSAndroid Build Coastguard Worker if (!frame_is_sframe(cm)) aom_wb_write_bit(wb, frame_size_override_flag);
2997*77c1e3ccSAndroid Build Coastguard Worker
2998*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->order_hint_info.enable_order_hint)
2999*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(
3000*77c1e3ccSAndroid Build Coastguard Worker wb, current_frame->order_hint,
3001*77c1e3ccSAndroid Build Coastguard Worker seq_params->order_hint_info.order_hint_bits_minus_1 + 1);
3002*77c1e3ccSAndroid Build Coastguard Worker
3003*77c1e3ccSAndroid Build Coastguard Worker if (!features->error_resilient_mode && !frame_is_intra_only(cm)) {
3004*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, features->primary_ref_frame, PRIMARY_REF_BITS);
3005*77c1e3ccSAndroid Build Coastguard Worker }
3006*77c1e3ccSAndroid Build Coastguard Worker }
3007*77c1e3ccSAndroid Build Coastguard Worker
3008*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->decoder_model_info_present_flag) {
3009*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, cpi->ppi->buffer_removal_time_present);
3010*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->buffer_removal_time_present) {
3011*77c1e3ccSAndroid Build Coastguard Worker for (int op_num = 0;
3012*77c1e3ccSAndroid Build Coastguard Worker op_num < seq_params->operating_points_cnt_minus_1 + 1; op_num++) {
3013*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->op_params[op_num].decoder_model_param_present_flag) {
3014*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->operating_point_idc[op_num] == 0 ||
3015*77c1e3ccSAndroid Build Coastguard Worker ((seq_params->operating_point_idc[op_num] >>
3016*77c1e3ccSAndroid Build Coastguard Worker cm->temporal_layer_id) &
3017*77c1e3ccSAndroid Build Coastguard Worker 0x1 &&
3018*77c1e3ccSAndroid Build Coastguard Worker (seq_params->operating_point_idc[op_num] >>
3019*77c1e3ccSAndroid Build Coastguard Worker (cm->spatial_layer_id + 8)) &
3020*77c1e3ccSAndroid Build Coastguard Worker 0x1)) {
3021*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_unsigned_literal(
3022*77c1e3ccSAndroid Build Coastguard Worker wb, cm->buffer_removal_times[op_num],
3023*77c1e3ccSAndroid Build Coastguard Worker seq_params->decoder_model_info.buffer_removal_time_length);
3024*77c1e3ccSAndroid Build Coastguard Worker cm->buffer_removal_times[op_num]++;
3025*77c1e3ccSAndroid Build Coastguard Worker if (cm->buffer_removal_times[op_num] == 0) {
3026*77c1e3ccSAndroid Build Coastguard Worker aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3027*77c1e3ccSAndroid Build Coastguard Worker "buffer_removal_time overflowed");
3028*77c1e3ccSAndroid Build Coastguard Worker }
3029*77c1e3ccSAndroid Build Coastguard Worker }
3030*77c1e3ccSAndroid Build Coastguard Worker }
3031*77c1e3ccSAndroid Build Coastguard Worker }
3032*77c1e3ccSAndroid Build Coastguard Worker }
3033*77c1e3ccSAndroid Build Coastguard Worker }
3034*77c1e3ccSAndroid Build Coastguard Worker
3035*77c1e3ccSAndroid Build Coastguard Worker // Shown keyframes and switch-frames automatically refreshes all reference
3036*77c1e3ccSAndroid Build Coastguard Worker // frames. For all other frame types, we need to write refresh_frame_flags.
3037*77c1e3ccSAndroid Build Coastguard Worker if ((current_frame->frame_type == KEY_FRAME && !cm->show_frame) ||
3038*77c1e3ccSAndroid Build Coastguard Worker current_frame->frame_type == INTER_FRAME ||
3039*77c1e3ccSAndroid Build Coastguard Worker current_frame->frame_type == INTRA_ONLY_FRAME)
3040*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, current_frame->refresh_frame_flags, REF_FRAMES);
3041*77c1e3ccSAndroid Build Coastguard Worker
3042*77c1e3ccSAndroid Build Coastguard Worker if (!frame_is_intra_only(cm) || current_frame->refresh_frame_flags != 0xff) {
3043*77c1e3ccSAndroid Build Coastguard Worker // Write all ref frame order hints if error_resilient_mode == 1
3044*77c1e3ccSAndroid Build Coastguard Worker if (features->error_resilient_mode &&
3045*77c1e3ccSAndroid Build Coastguard Worker seq_params->order_hint_info.enable_order_hint) {
3046*77c1e3ccSAndroid Build Coastguard Worker for (int ref_idx = 0; ref_idx < REF_FRAMES; ref_idx++) {
3047*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(
3048*77c1e3ccSAndroid Build Coastguard Worker wb, cm->ref_frame_map[ref_idx]->order_hint,
3049*77c1e3ccSAndroid Build Coastguard Worker seq_params->order_hint_info.order_hint_bits_minus_1 + 1);
3050*77c1e3ccSAndroid Build Coastguard Worker }
3051*77c1e3ccSAndroid Build Coastguard Worker }
3052*77c1e3ccSAndroid Build Coastguard Worker }
3053*77c1e3ccSAndroid Build Coastguard Worker
3054*77c1e3ccSAndroid Build Coastguard Worker if (current_frame->frame_type == KEY_FRAME) {
3055*77c1e3ccSAndroid Build Coastguard Worker write_frame_size(cm, frame_size_override_flag, wb);
3056*77c1e3ccSAndroid Build Coastguard Worker assert(!av1_superres_scaled(cm) || !features->allow_intrabc);
3057*77c1e3ccSAndroid Build Coastguard Worker if (features->allow_screen_content_tools && !av1_superres_scaled(cm))
3058*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, features->allow_intrabc);
3059*77c1e3ccSAndroid Build Coastguard Worker } else {
3060*77c1e3ccSAndroid Build Coastguard Worker if (current_frame->frame_type == INTRA_ONLY_FRAME) {
3061*77c1e3ccSAndroid Build Coastguard Worker write_frame_size(cm, frame_size_override_flag, wb);
3062*77c1e3ccSAndroid Build Coastguard Worker assert(!av1_superres_scaled(cm) || !features->allow_intrabc);
3063*77c1e3ccSAndroid Build Coastguard Worker if (features->allow_screen_content_tools && !av1_superres_scaled(cm))
3064*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, features->allow_intrabc);
3065*77c1e3ccSAndroid Build Coastguard Worker } else if (current_frame->frame_type == INTER_FRAME ||
3066*77c1e3ccSAndroid Build Coastguard Worker frame_is_sframe(cm)) {
3067*77c1e3ccSAndroid Build Coastguard Worker MV_REFERENCE_FRAME ref_frame;
3068*77c1e3ccSAndroid Build Coastguard Worker
3069*77c1e3ccSAndroid Build Coastguard Worker // NOTE: Error resilient mode turns off frame_refs_short_signaling
3070*77c1e3ccSAndroid Build Coastguard Worker // automatically.
3071*77c1e3ccSAndroid Build Coastguard Worker #define FRAME_REFS_SHORT_SIGNALING 0
3072*77c1e3ccSAndroid Build Coastguard Worker #if FRAME_REFS_SHORT_SIGNALING
3073*77c1e3ccSAndroid Build Coastguard Worker current_frame->frame_refs_short_signaling =
3074*77c1e3ccSAndroid Build Coastguard Worker seq_params->order_hint_info.enable_order_hint;
3075*77c1e3ccSAndroid Build Coastguard Worker #endif // FRAME_REFS_SHORT_SIGNALING
3076*77c1e3ccSAndroid Build Coastguard Worker
3077*77c1e3ccSAndroid Build Coastguard Worker if (current_frame->frame_refs_short_signaling) {
3078*77c1e3ccSAndroid Build Coastguard Worker // In rtc case when cpi->sf.rt_sf.enable_ref_short_signaling is true,
3079*77c1e3ccSAndroid Build Coastguard Worker // we turn on frame_refs_short_signaling when the current frame and
3080*77c1e3ccSAndroid Build Coastguard Worker // golden frame are in the same order_hint group, and their relative
3081*77c1e3ccSAndroid Build Coastguard Worker // distance is <= 64 (in order to be decodable).
3082*77c1e3ccSAndroid Build Coastguard Worker
3083*77c1e3ccSAndroid Build Coastguard Worker // For other cases, an example solution for encoder-side
3084*77c1e3ccSAndroid Build Coastguard Worker // implementation on frame_refs_short_signaling is also provided in
3085*77c1e3ccSAndroid Build Coastguard Worker // this function, where frame_refs_short_signaling is only turned on
3086*77c1e3ccSAndroid Build Coastguard Worker // when the encoder side decision on ref frames is identical to that
3087*77c1e3ccSAndroid Build Coastguard Worker // at the decoder side.
3088*77c1e3ccSAndroid Build Coastguard Worker
3089*77c1e3ccSAndroid Build Coastguard Worker current_frame->frame_refs_short_signaling =
3090*77c1e3ccSAndroid Build Coastguard Worker check_frame_refs_short_signaling(
3091*77c1e3ccSAndroid Build Coastguard Worker cm, cpi->sf.rt_sf.enable_ref_short_signaling);
3092*77c1e3ccSAndroid Build Coastguard Worker }
3093*77c1e3ccSAndroid Build Coastguard Worker
3094*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->order_hint_info.enable_order_hint)
3095*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, current_frame->frame_refs_short_signaling);
3096*77c1e3ccSAndroid Build Coastguard Worker
3097*77c1e3ccSAndroid Build Coastguard Worker if (current_frame->frame_refs_short_signaling) {
3098*77c1e3ccSAndroid Build Coastguard Worker const int lst_ref = get_ref_frame_map_idx(cm, LAST_FRAME);
3099*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, lst_ref, REF_FRAMES_LOG2);
3100*77c1e3ccSAndroid Build Coastguard Worker
3101*77c1e3ccSAndroid Build Coastguard Worker const int gld_ref = get_ref_frame_map_idx(cm, GOLDEN_FRAME);
3102*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, gld_ref, REF_FRAMES_LOG2);
3103*77c1e3ccSAndroid Build Coastguard Worker }
3104*77c1e3ccSAndroid Build Coastguard Worker int first_ref_map_idx = INVALID_IDX;
3105*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->rtc_ref.set_ref_frame_config) {
3106*77c1e3ccSAndroid Build Coastguard Worker for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3107*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->rtc_ref.reference[ref_frame - 1] == 1) {
3108*77c1e3ccSAndroid Build Coastguard Worker first_ref_map_idx = cpi->ppi->rtc_ref.ref_idx[ref_frame - 1];
3109*77c1e3ccSAndroid Build Coastguard Worker break;
3110*77c1e3ccSAndroid Build Coastguard Worker }
3111*77c1e3ccSAndroid Build Coastguard Worker }
3112*77c1e3ccSAndroid Build Coastguard Worker }
3113*77c1e3ccSAndroid Build Coastguard Worker for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3114*77c1e3ccSAndroid Build Coastguard Worker assert(get_ref_frame_map_idx(cm, ref_frame) != INVALID_IDX);
3115*77c1e3ccSAndroid Build Coastguard Worker if (!current_frame->frame_refs_short_signaling) {
3116*77c1e3ccSAndroid Build Coastguard Worker if (cpi->ppi->rtc_ref.set_ref_frame_config &&
3117*77c1e3ccSAndroid Build Coastguard Worker first_ref_map_idx != INVALID_IDX &&
3118*77c1e3ccSAndroid Build Coastguard Worker cpi->svc.number_spatial_layers == 1 &&
3119*77c1e3ccSAndroid Build Coastguard Worker !seq_params->order_hint_info.enable_order_hint) {
3120*77c1e3ccSAndroid Build Coastguard Worker // For the usage of set_ref_frame_config:
3121*77c1e3ccSAndroid Build Coastguard Worker // for any reference not used set their ref_map_idx
3122*77c1e3ccSAndroid Build Coastguard Worker // to the first used reference.
3123*77c1e3ccSAndroid Build Coastguard Worker const int map_idx = cpi->ppi->rtc_ref.reference[ref_frame - 1]
3124*77c1e3ccSAndroid Build Coastguard Worker ? get_ref_frame_map_idx(cm, ref_frame)
3125*77c1e3ccSAndroid Build Coastguard Worker : first_ref_map_idx;
3126*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, map_idx, REF_FRAMES_LOG2);
3127*77c1e3ccSAndroid Build Coastguard Worker } else {
3128*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, get_ref_frame_map_idx(cm, ref_frame),
3129*77c1e3ccSAndroid Build Coastguard Worker REF_FRAMES_LOG2);
3130*77c1e3ccSAndroid Build Coastguard Worker }
3131*77c1e3ccSAndroid Build Coastguard Worker }
3132*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->frame_id_numbers_present_flag) {
3133*77c1e3ccSAndroid Build Coastguard Worker int i = get_ref_frame_map_idx(cm, ref_frame);
3134*77c1e3ccSAndroid Build Coastguard Worker int frame_id_len = seq_params->frame_id_length;
3135*77c1e3ccSAndroid Build Coastguard Worker int diff_len = seq_params->delta_frame_id_length;
3136*77c1e3ccSAndroid Build Coastguard Worker int delta_frame_id_minus_1 =
3137*77c1e3ccSAndroid Build Coastguard Worker ((cm->current_frame_id - cm->ref_frame_id[i] +
3138*77c1e3ccSAndroid Build Coastguard Worker (1 << frame_id_len)) %
3139*77c1e3ccSAndroid Build Coastguard Worker (1 << frame_id_len)) -
3140*77c1e3ccSAndroid Build Coastguard Worker 1;
3141*77c1e3ccSAndroid Build Coastguard Worker if (delta_frame_id_minus_1 < 0 ||
3142*77c1e3ccSAndroid Build Coastguard Worker delta_frame_id_minus_1 >= (1 << diff_len)) {
3143*77c1e3ccSAndroid Build Coastguard Worker aom_internal_error(cm->error, AOM_CODEC_ERROR,
3144*77c1e3ccSAndroid Build Coastguard Worker "Invalid delta_frame_id_minus_1");
3145*77c1e3ccSAndroid Build Coastguard Worker }
3146*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, delta_frame_id_minus_1, diff_len);
3147*77c1e3ccSAndroid Build Coastguard Worker }
3148*77c1e3ccSAndroid Build Coastguard Worker }
3149*77c1e3ccSAndroid Build Coastguard Worker
3150*77c1e3ccSAndroid Build Coastguard Worker if (!features->error_resilient_mode && frame_size_override_flag) {
3151*77c1e3ccSAndroid Build Coastguard Worker write_frame_size_with_refs(cm, wb);
3152*77c1e3ccSAndroid Build Coastguard Worker } else {
3153*77c1e3ccSAndroid Build Coastguard Worker write_frame_size(cm, frame_size_override_flag, wb);
3154*77c1e3ccSAndroid Build Coastguard Worker }
3155*77c1e3ccSAndroid Build Coastguard Worker
3156*77c1e3ccSAndroid Build Coastguard Worker if (!features->cur_frame_force_integer_mv)
3157*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, features->allow_high_precision_mv);
3158*77c1e3ccSAndroid Build Coastguard Worker write_frame_interp_filter(features->interp_filter, wb);
3159*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, features->switchable_motion_mode);
3160*77c1e3ccSAndroid Build Coastguard Worker if (frame_might_allow_ref_frame_mvs(cm)) {
3161*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, features->allow_ref_frame_mvs);
3162*77c1e3ccSAndroid Build Coastguard Worker } else {
3163*77c1e3ccSAndroid Build Coastguard Worker assert(features->allow_ref_frame_mvs == 0);
3164*77c1e3ccSAndroid Build Coastguard Worker }
3165*77c1e3ccSAndroid Build Coastguard Worker }
3166*77c1e3ccSAndroid Build Coastguard Worker }
3167*77c1e3ccSAndroid Build Coastguard Worker
3168*77c1e3ccSAndroid Build Coastguard Worker const int might_bwd_adapt = !(seq_params->reduced_still_picture_hdr) &&
3169*77c1e3ccSAndroid Build Coastguard Worker !(features->disable_cdf_update);
3170*77c1e3ccSAndroid Build Coastguard Worker if (cm->tiles.large_scale)
3171*77c1e3ccSAndroid Build Coastguard Worker assert(features->refresh_frame_context == REFRESH_FRAME_CONTEXT_DISABLED);
3172*77c1e3ccSAndroid Build Coastguard Worker
3173*77c1e3ccSAndroid Build Coastguard Worker if (might_bwd_adapt) {
3174*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(
3175*77c1e3ccSAndroid Build Coastguard Worker wb, features->refresh_frame_context == REFRESH_FRAME_CONTEXT_DISABLED);
3176*77c1e3ccSAndroid Build Coastguard Worker }
3177*77c1e3ccSAndroid Build Coastguard Worker
3178*77c1e3ccSAndroid Build Coastguard Worker write_tile_info(cm, saved_wb, wb);
3179*77c1e3ccSAndroid Build Coastguard Worker encode_quantization(quant_params, av1_num_planes(cm),
3180*77c1e3ccSAndroid Build Coastguard Worker cm->seq_params->separate_uv_delta_q, wb);
3181*77c1e3ccSAndroid Build Coastguard Worker encode_segmentation(cm, wb);
3182*77c1e3ccSAndroid Build Coastguard Worker
3183*77c1e3ccSAndroid Build Coastguard Worker const DeltaQInfo *const delta_q_info = &cm->delta_q_info;
3184*77c1e3ccSAndroid Build Coastguard Worker if (delta_q_info->delta_q_present_flag) assert(quant_params->base_qindex > 0);
3185*77c1e3ccSAndroid Build Coastguard Worker if (quant_params->base_qindex > 0) {
3186*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, delta_q_info->delta_q_present_flag);
3187*77c1e3ccSAndroid Build Coastguard Worker if (delta_q_info->delta_q_present_flag) {
3188*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, get_msb(delta_q_info->delta_q_res), 2);
3189*77c1e3ccSAndroid Build Coastguard Worker xd->current_base_qindex = quant_params->base_qindex;
3190*77c1e3ccSAndroid Build Coastguard Worker if (features->allow_intrabc)
3191*77c1e3ccSAndroid Build Coastguard Worker assert(delta_q_info->delta_lf_present_flag == 0);
3192*77c1e3ccSAndroid Build Coastguard Worker else
3193*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, delta_q_info->delta_lf_present_flag);
3194*77c1e3ccSAndroid Build Coastguard Worker if (delta_q_info->delta_lf_present_flag) {
3195*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, get_msb(delta_q_info->delta_lf_res), 2);
3196*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, delta_q_info->delta_lf_multi);
3197*77c1e3ccSAndroid Build Coastguard Worker av1_reset_loop_filter_delta(xd, av1_num_planes(cm));
3198*77c1e3ccSAndroid Build Coastguard Worker }
3199*77c1e3ccSAndroid Build Coastguard Worker }
3200*77c1e3ccSAndroid Build Coastguard Worker }
3201*77c1e3ccSAndroid Build Coastguard Worker
3202*77c1e3ccSAndroid Build Coastguard Worker if (features->all_lossless) {
3203*77c1e3ccSAndroid Build Coastguard Worker assert(!av1_superres_scaled(cm));
3204*77c1e3ccSAndroid Build Coastguard Worker } else {
3205*77c1e3ccSAndroid Build Coastguard Worker if (!features->coded_lossless) {
3206*77c1e3ccSAndroid Build Coastguard Worker encode_loopfilter(cm, wb);
3207*77c1e3ccSAndroid Build Coastguard Worker encode_cdef(cm, wb);
3208*77c1e3ccSAndroid Build Coastguard Worker }
3209*77c1e3ccSAndroid Build Coastguard Worker encode_restoration_mode(cm, wb);
3210*77c1e3ccSAndroid Build Coastguard Worker }
3211*77c1e3ccSAndroid Build Coastguard Worker
3212*77c1e3ccSAndroid Build Coastguard Worker // Write TX mode
3213*77c1e3ccSAndroid Build Coastguard Worker if (features->coded_lossless)
3214*77c1e3ccSAndroid Build Coastguard Worker assert(features->tx_mode == ONLY_4X4);
3215*77c1e3ccSAndroid Build Coastguard Worker else
3216*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, features->tx_mode == TX_MODE_SELECT);
3217*77c1e3ccSAndroid Build Coastguard Worker
3218*77c1e3ccSAndroid Build Coastguard Worker if (!frame_is_intra_only(cm)) {
3219*77c1e3ccSAndroid Build Coastguard Worker const int use_hybrid_pred =
3220*77c1e3ccSAndroid Build Coastguard Worker current_frame->reference_mode == REFERENCE_MODE_SELECT;
3221*77c1e3ccSAndroid Build Coastguard Worker
3222*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, use_hybrid_pred);
3223*77c1e3ccSAndroid Build Coastguard Worker }
3224*77c1e3ccSAndroid Build Coastguard Worker
3225*77c1e3ccSAndroid Build Coastguard Worker if (current_frame->skip_mode_info.skip_mode_allowed)
3226*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, current_frame->skip_mode_info.skip_mode_flag);
3227*77c1e3ccSAndroid Build Coastguard Worker
3228*77c1e3ccSAndroid Build Coastguard Worker if (frame_might_allow_warped_motion(cm))
3229*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, features->allow_warped_motion);
3230*77c1e3ccSAndroid Build Coastguard Worker else
3231*77c1e3ccSAndroid Build Coastguard Worker assert(!features->allow_warped_motion);
3232*77c1e3ccSAndroid Build Coastguard Worker
3233*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, features->reduced_tx_set_used);
3234*77c1e3ccSAndroid Build Coastguard Worker
3235*77c1e3ccSAndroid Build Coastguard Worker if (!frame_is_intra_only(cm)) write_global_motion(cpi, wb);
3236*77c1e3ccSAndroid Build Coastguard Worker
3237*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->film_grain_params_present &&
3238*77c1e3ccSAndroid Build Coastguard Worker (cm->show_frame || cm->showable_frame))
3239*77c1e3ccSAndroid Build Coastguard Worker write_film_grain_params(cpi, wb);
3240*77c1e3ccSAndroid Build Coastguard Worker
3241*77c1e3ccSAndroid Build Coastguard Worker if (cm->tiles.large_scale) write_ext_tile_info(cm, saved_wb, wb);
3242*77c1e3ccSAndroid Build Coastguard Worker }
3243*77c1e3ccSAndroid Build Coastguard Worker
choose_size_bytes(uint32_t size,int spare_msbs)3244*77c1e3ccSAndroid Build Coastguard Worker static int choose_size_bytes(uint32_t size, int spare_msbs) {
3245*77c1e3ccSAndroid Build Coastguard Worker // Choose the number of bytes required to represent size, without
3246*77c1e3ccSAndroid Build Coastguard Worker // using the 'spare_msbs' number of most significant bits.
3247*77c1e3ccSAndroid Build Coastguard Worker
3248*77c1e3ccSAndroid Build Coastguard Worker // Make sure we will fit in 4 bytes to start with..
3249*77c1e3ccSAndroid Build Coastguard Worker if (spare_msbs > 0 && size >> (32 - spare_msbs) != 0) return -1;
3250*77c1e3ccSAndroid Build Coastguard Worker
3251*77c1e3ccSAndroid Build Coastguard Worker // Normalise to 32 bits
3252*77c1e3ccSAndroid Build Coastguard Worker size <<= spare_msbs;
3253*77c1e3ccSAndroid Build Coastguard Worker
3254*77c1e3ccSAndroid Build Coastguard Worker if (size >> 24 != 0)
3255*77c1e3ccSAndroid Build Coastguard Worker return 4;
3256*77c1e3ccSAndroid Build Coastguard Worker else if (size >> 16 != 0)
3257*77c1e3ccSAndroid Build Coastguard Worker return 3;
3258*77c1e3ccSAndroid Build Coastguard Worker else if (size >> 8 != 0)
3259*77c1e3ccSAndroid Build Coastguard Worker return 2;
3260*77c1e3ccSAndroid Build Coastguard Worker else
3261*77c1e3ccSAndroid Build Coastguard Worker return 1;
3262*77c1e3ccSAndroid Build Coastguard Worker }
3263*77c1e3ccSAndroid Build Coastguard Worker
mem_put_varsize(uint8_t * const dst,const int sz,const int val)3264*77c1e3ccSAndroid Build Coastguard Worker static inline void mem_put_varsize(uint8_t *const dst, const int sz,
3265*77c1e3ccSAndroid Build Coastguard Worker const int val) {
3266*77c1e3ccSAndroid Build Coastguard Worker switch (sz) {
3267*77c1e3ccSAndroid Build Coastguard Worker case 1: dst[0] = (uint8_t)(val & 0xff); break;
3268*77c1e3ccSAndroid Build Coastguard Worker case 2: mem_put_le16(dst, val); break;
3269*77c1e3ccSAndroid Build Coastguard Worker case 3: mem_put_le24(dst, val); break;
3270*77c1e3ccSAndroid Build Coastguard Worker case 4: mem_put_le32(dst, val); break;
3271*77c1e3ccSAndroid Build Coastguard Worker default: assert(0 && "Invalid size"); break;
3272*77c1e3ccSAndroid Build Coastguard Worker }
3273*77c1e3ccSAndroid Build Coastguard Worker }
3274*77c1e3ccSAndroid Build Coastguard Worker
remux_tiles(const CommonTileParams * const tiles,uint8_t * dst,const uint32_t data_size,const uint32_t max_tile_size,const uint32_t max_tile_col_size,int * const tile_size_bytes,int * const tile_col_size_bytes)3275*77c1e3ccSAndroid Build Coastguard Worker static int remux_tiles(const CommonTileParams *const tiles, uint8_t *dst,
3276*77c1e3ccSAndroid Build Coastguard Worker const uint32_t data_size, const uint32_t max_tile_size,
3277*77c1e3ccSAndroid Build Coastguard Worker const uint32_t max_tile_col_size,
3278*77c1e3ccSAndroid Build Coastguard Worker int *const tile_size_bytes,
3279*77c1e3ccSAndroid Build Coastguard Worker int *const tile_col_size_bytes) {
3280*77c1e3ccSAndroid Build Coastguard Worker // Choose the tile size bytes (tsb) and tile column size bytes (tcsb)
3281*77c1e3ccSAndroid Build Coastguard Worker int tsb;
3282*77c1e3ccSAndroid Build Coastguard Worker int tcsb;
3283*77c1e3ccSAndroid Build Coastguard Worker
3284*77c1e3ccSAndroid Build Coastguard Worker if (tiles->large_scale) {
3285*77c1e3ccSAndroid Build Coastguard Worker // The top bit in the tile size field indicates tile copy mode, so we
3286*77c1e3ccSAndroid Build Coastguard Worker // have 1 less bit to code the tile size
3287*77c1e3ccSAndroid Build Coastguard Worker tsb = choose_size_bytes(max_tile_size, 1);
3288*77c1e3ccSAndroid Build Coastguard Worker tcsb = choose_size_bytes(max_tile_col_size, 0);
3289*77c1e3ccSAndroid Build Coastguard Worker } else {
3290*77c1e3ccSAndroid Build Coastguard Worker tsb = choose_size_bytes(max_tile_size, 0);
3291*77c1e3ccSAndroid Build Coastguard Worker tcsb = 4; // This is ignored
3292*77c1e3ccSAndroid Build Coastguard Worker (void)max_tile_col_size;
3293*77c1e3ccSAndroid Build Coastguard Worker }
3294*77c1e3ccSAndroid Build Coastguard Worker
3295*77c1e3ccSAndroid Build Coastguard Worker assert(tsb > 0);
3296*77c1e3ccSAndroid Build Coastguard Worker assert(tcsb > 0);
3297*77c1e3ccSAndroid Build Coastguard Worker
3298*77c1e3ccSAndroid Build Coastguard Worker *tile_size_bytes = tsb;
3299*77c1e3ccSAndroid Build Coastguard Worker *tile_col_size_bytes = tcsb;
3300*77c1e3ccSAndroid Build Coastguard Worker if (tsb == 4 && tcsb == 4) return data_size;
3301*77c1e3ccSAndroid Build Coastguard Worker
3302*77c1e3ccSAndroid Build Coastguard Worker uint32_t wpos = 0;
3303*77c1e3ccSAndroid Build Coastguard Worker uint32_t rpos = 0;
3304*77c1e3ccSAndroid Build Coastguard Worker
3305*77c1e3ccSAndroid Build Coastguard Worker if (tiles->large_scale) {
3306*77c1e3ccSAndroid Build Coastguard Worker int tile_row;
3307*77c1e3ccSAndroid Build Coastguard Worker int tile_col;
3308*77c1e3ccSAndroid Build Coastguard Worker
3309*77c1e3ccSAndroid Build Coastguard Worker for (tile_col = 0; tile_col < tiles->cols; tile_col++) {
3310*77c1e3ccSAndroid Build Coastguard Worker // All but the last column has a column header
3311*77c1e3ccSAndroid Build Coastguard Worker if (tile_col < tiles->cols - 1) {
3312*77c1e3ccSAndroid Build Coastguard Worker uint32_t tile_col_size = mem_get_le32(dst + rpos);
3313*77c1e3ccSAndroid Build Coastguard Worker rpos += 4;
3314*77c1e3ccSAndroid Build Coastguard Worker
3315*77c1e3ccSAndroid Build Coastguard Worker // Adjust the tile column size by the number of bytes removed
3316*77c1e3ccSAndroid Build Coastguard Worker // from the tile size fields.
3317*77c1e3ccSAndroid Build Coastguard Worker tile_col_size -= (4 - tsb) * tiles->rows;
3318*77c1e3ccSAndroid Build Coastguard Worker
3319*77c1e3ccSAndroid Build Coastguard Worker mem_put_varsize(dst + wpos, tcsb, tile_col_size);
3320*77c1e3ccSAndroid Build Coastguard Worker wpos += tcsb;
3321*77c1e3ccSAndroid Build Coastguard Worker }
3322*77c1e3ccSAndroid Build Coastguard Worker
3323*77c1e3ccSAndroid Build Coastguard Worker for (tile_row = 0; tile_row < tiles->rows; tile_row++) {
3324*77c1e3ccSAndroid Build Coastguard Worker // All, including the last row has a header
3325*77c1e3ccSAndroid Build Coastguard Worker uint32_t tile_header = mem_get_le32(dst + rpos);
3326*77c1e3ccSAndroid Build Coastguard Worker rpos += 4;
3327*77c1e3ccSAndroid Build Coastguard Worker
3328*77c1e3ccSAndroid Build Coastguard Worker // If this is a copy tile, we need to shift the MSB to the
3329*77c1e3ccSAndroid Build Coastguard Worker // top bit of the new width, and there is no data to copy.
3330*77c1e3ccSAndroid Build Coastguard Worker if (tile_header >> 31 != 0) {
3331*77c1e3ccSAndroid Build Coastguard Worker if (tsb < 4) tile_header >>= 32 - 8 * tsb;
3332*77c1e3ccSAndroid Build Coastguard Worker mem_put_varsize(dst + wpos, tsb, tile_header);
3333*77c1e3ccSAndroid Build Coastguard Worker wpos += tsb;
3334*77c1e3ccSAndroid Build Coastguard Worker } else {
3335*77c1e3ccSAndroid Build Coastguard Worker mem_put_varsize(dst + wpos, tsb, tile_header);
3336*77c1e3ccSAndroid Build Coastguard Worker wpos += tsb;
3337*77c1e3ccSAndroid Build Coastguard Worker
3338*77c1e3ccSAndroid Build Coastguard Worker tile_header += AV1_MIN_TILE_SIZE_BYTES;
3339*77c1e3ccSAndroid Build Coastguard Worker memmove(dst + wpos, dst + rpos, tile_header);
3340*77c1e3ccSAndroid Build Coastguard Worker rpos += tile_header;
3341*77c1e3ccSAndroid Build Coastguard Worker wpos += tile_header;
3342*77c1e3ccSAndroid Build Coastguard Worker }
3343*77c1e3ccSAndroid Build Coastguard Worker }
3344*77c1e3ccSAndroid Build Coastguard Worker }
3345*77c1e3ccSAndroid Build Coastguard Worker
3346*77c1e3ccSAndroid Build Coastguard Worker assert(rpos > wpos);
3347*77c1e3ccSAndroid Build Coastguard Worker assert(rpos == data_size);
3348*77c1e3ccSAndroid Build Coastguard Worker
3349*77c1e3ccSAndroid Build Coastguard Worker return wpos;
3350*77c1e3ccSAndroid Build Coastguard Worker }
3351*77c1e3ccSAndroid Build Coastguard Worker const int n_tiles = tiles->cols * tiles->rows;
3352*77c1e3ccSAndroid Build Coastguard Worker int n;
3353*77c1e3ccSAndroid Build Coastguard Worker
3354*77c1e3ccSAndroid Build Coastguard Worker for (n = 0; n < n_tiles; n++) {
3355*77c1e3ccSAndroid Build Coastguard Worker int tile_size;
3356*77c1e3ccSAndroid Build Coastguard Worker
3357*77c1e3ccSAndroid Build Coastguard Worker if (n == n_tiles - 1) {
3358*77c1e3ccSAndroid Build Coastguard Worker tile_size = data_size - rpos;
3359*77c1e3ccSAndroid Build Coastguard Worker } else {
3360*77c1e3ccSAndroid Build Coastguard Worker tile_size = mem_get_le32(dst + rpos);
3361*77c1e3ccSAndroid Build Coastguard Worker rpos += 4;
3362*77c1e3ccSAndroid Build Coastguard Worker mem_put_varsize(dst + wpos, tsb, tile_size);
3363*77c1e3ccSAndroid Build Coastguard Worker tile_size += AV1_MIN_TILE_SIZE_BYTES;
3364*77c1e3ccSAndroid Build Coastguard Worker wpos += tsb;
3365*77c1e3ccSAndroid Build Coastguard Worker }
3366*77c1e3ccSAndroid Build Coastguard Worker
3367*77c1e3ccSAndroid Build Coastguard Worker memmove(dst + wpos, dst + rpos, tile_size);
3368*77c1e3ccSAndroid Build Coastguard Worker
3369*77c1e3ccSAndroid Build Coastguard Worker rpos += tile_size;
3370*77c1e3ccSAndroid Build Coastguard Worker wpos += tile_size;
3371*77c1e3ccSAndroid Build Coastguard Worker }
3372*77c1e3ccSAndroid Build Coastguard Worker
3373*77c1e3ccSAndroid Build Coastguard Worker assert(rpos > wpos);
3374*77c1e3ccSAndroid Build Coastguard Worker assert(rpos == data_size);
3375*77c1e3ccSAndroid Build Coastguard Worker
3376*77c1e3ccSAndroid Build Coastguard Worker return wpos;
3377*77c1e3ccSAndroid Build Coastguard Worker }
3378*77c1e3ccSAndroid Build Coastguard Worker
av1_write_obu_header(AV1LevelParams * const level_params,int * frame_header_count,OBU_TYPE obu_type,bool has_nonzero_operating_point_idc,int obu_extension,uint8_t * const dst)3379*77c1e3ccSAndroid Build Coastguard Worker uint32_t av1_write_obu_header(AV1LevelParams *const level_params,
3380*77c1e3ccSAndroid Build Coastguard Worker int *frame_header_count, OBU_TYPE obu_type,
3381*77c1e3ccSAndroid Build Coastguard Worker bool has_nonzero_operating_point_idc,
3382*77c1e3ccSAndroid Build Coastguard Worker int obu_extension, uint8_t *const dst) {
3383*77c1e3ccSAndroid Build Coastguard Worker assert(IMPLIES(!has_nonzero_operating_point_idc, obu_extension == 0));
3384*77c1e3ccSAndroid Build Coastguard Worker
3385*77c1e3ccSAndroid Build Coastguard Worker if (level_params->keep_level_stats &&
3386*77c1e3ccSAndroid Build Coastguard Worker (obu_type == OBU_FRAME || obu_type == OBU_FRAME_HEADER))
3387*77c1e3ccSAndroid Build Coastguard Worker ++(*frame_header_count);
3388*77c1e3ccSAndroid Build Coastguard Worker
3389*77c1e3ccSAndroid Build Coastguard Worker uint32_t size = 0;
3390*77c1e3ccSAndroid Build Coastguard Worker
3391*77c1e3ccSAndroid Build Coastguard Worker // The AV1 spec Version 1.0.0 with Errata 1 has the following requirements on
3392*77c1e3ccSAndroid Build Coastguard Worker // the OBU extension header:
3393*77c1e3ccSAndroid Build Coastguard Worker //
3394*77c1e3ccSAndroid Build Coastguard Worker // 6.4.1. General sequence header OBU semantics:
3395*77c1e3ccSAndroid Build Coastguard Worker // It is a requirement of bitstream conformance that if OperatingPointIdc
3396*77c1e3ccSAndroid Build Coastguard Worker // is equal to 0, then obu_extension_flag is equal to 0 for all OBUs that
3397*77c1e3ccSAndroid Build Coastguard Worker // follow this sequence header until the next sequence header.
3398*77c1e3ccSAndroid Build Coastguard Worker //
3399*77c1e3ccSAndroid Build Coastguard Worker // 7.5. Ordering of OBUs:
3400*77c1e3ccSAndroid Build Coastguard Worker // If a coded video sequence contains at least one enhancement layer (OBUs
3401*77c1e3ccSAndroid Build Coastguard Worker // with spatial_id greater than 0 or temporal_id greater than 0) then all
3402*77c1e3ccSAndroid Build Coastguard Worker // frame headers and tile group OBUs associated with base (spatial_id
3403*77c1e3ccSAndroid Build Coastguard Worker // equals 0 and temporal_id equals 0) and enhancement layer (spatial_id
3404*77c1e3ccSAndroid Build Coastguard Worker // greater than 0 or temporal_id greater than 0) data must include the OBU
3405*77c1e3ccSAndroid Build Coastguard Worker // extension header.
3406*77c1e3ccSAndroid Build Coastguard Worker //
3407*77c1e3ccSAndroid Build Coastguard Worker // Set obu_extension_flag to satisfy these requirements.
3408*77c1e3ccSAndroid Build Coastguard Worker int obu_extension_flag = 0;
3409*77c1e3ccSAndroid Build Coastguard Worker if (has_nonzero_operating_point_idc) {
3410*77c1e3ccSAndroid Build Coastguard Worker obu_extension_flag =
3411*77c1e3ccSAndroid Build Coastguard Worker (obu_type == OBU_FRAME_HEADER || obu_type == OBU_TILE_GROUP ||
3412*77c1e3ccSAndroid Build Coastguard Worker obu_type == OBU_FRAME || obu_type == OBU_REDUNDANT_FRAME_HEADER);
3413*77c1e3ccSAndroid Build Coastguard Worker }
3414*77c1e3ccSAndroid Build Coastguard Worker const int obu_has_size_field = 1;
3415*77c1e3ccSAndroid Build Coastguard Worker
3416*77c1e3ccSAndroid Build Coastguard Worker dst[0] = ((int)obu_type << 3) | (obu_extension_flag << 2) |
3417*77c1e3ccSAndroid Build Coastguard Worker (obu_has_size_field << 1);
3418*77c1e3ccSAndroid Build Coastguard Worker size++;
3419*77c1e3ccSAndroid Build Coastguard Worker
3420*77c1e3ccSAndroid Build Coastguard Worker if (obu_extension_flag) {
3421*77c1e3ccSAndroid Build Coastguard Worker dst[1] = obu_extension & 0xFF;
3422*77c1e3ccSAndroid Build Coastguard Worker size++;
3423*77c1e3ccSAndroid Build Coastguard Worker }
3424*77c1e3ccSAndroid Build Coastguard Worker
3425*77c1e3ccSAndroid Build Coastguard Worker return size;
3426*77c1e3ccSAndroid Build Coastguard Worker }
3427*77c1e3ccSAndroid Build Coastguard Worker
av1_write_uleb_obu_size(size_t obu_payload_size,uint8_t * dest,size_t dest_size)3428*77c1e3ccSAndroid Build Coastguard Worker int av1_write_uleb_obu_size(size_t obu_payload_size, uint8_t *dest,
3429*77c1e3ccSAndroid Build Coastguard Worker size_t dest_size) {
3430*77c1e3ccSAndroid Build Coastguard Worker size_t coded_obu_size = 0;
3431*77c1e3ccSAndroid Build Coastguard Worker
3432*77c1e3ccSAndroid Build Coastguard Worker if (aom_uleb_encode(obu_payload_size, dest_size, dest, &coded_obu_size) !=
3433*77c1e3ccSAndroid Build Coastguard Worker 0) {
3434*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_ERROR;
3435*77c1e3ccSAndroid Build Coastguard Worker }
3436*77c1e3ccSAndroid Build Coastguard Worker if (coded_obu_size != dest_size) {
3437*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_ERROR;
3438*77c1e3ccSAndroid Build Coastguard Worker }
3439*77c1e3ccSAndroid Build Coastguard Worker
3440*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_OK;
3441*77c1e3ccSAndroid Build Coastguard Worker }
3442*77c1e3ccSAndroid Build Coastguard Worker
av1_write_uleb_obu_size_unsafe(size_t obu_payload_size,uint8_t * dest)3443*77c1e3ccSAndroid Build Coastguard Worker int av1_write_uleb_obu_size_unsafe(size_t obu_payload_size, uint8_t *dest) {
3444*77c1e3ccSAndroid Build Coastguard Worker size_t coded_obu_size = 0;
3445*77c1e3ccSAndroid Build Coastguard Worker
3446*77c1e3ccSAndroid Build Coastguard Worker if (aom_uleb_encode(obu_payload_size, sizeof(uint32_t), dest,
3447*77c1e3ccSAndroid Build Coastguard Worker &coded_obu_size) != 0) {
3448*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_ERROR;
3449*77c1e3ccSAndroid Build Coastguard Worker }
3450*77c1e3ccSAndroid Build Coastguard Worker
3451*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_OK;
3452*77c1e3ccSAndroid Build Coastguard Worker }
3453*77c1e3ccSAndroid Build Coastguard Worker
3454*77c1e3ccSAndroid Build Coastguard Worker // Returns 0 on failure.
obu_memmove(size_t obu_header_size,size_t obu_payload_size,uint8_t * data,size_t data_size)3455*77c1e3ccSAndroid Build Coastguard Worker static size_t obu_memmove(size_t obu_header_size, size_t obu_payload_size,
3456*77c1e3ccSAndroid Build Coastguard Worker uint8_t *data, size_t data_size) {
3457*77c1e3ccSAndroid Build Coastguard Worker const size_t length_field_size = aom_uleb_size_in_bytes(obu_payload_size);
3458*77c1e3ccSAndroid Build Coastguard Worker const size_t move_dst_offset = obu_header_size + length_field_size;
3459*77c1e3ccSAndroid Build Coastguard Worker const size_t move_src_offset = obu_header_size;
3460*77c1e3ccSAndroid Build Coastguard Worker const size_t move_size = obu_payload_size;
3461*77c1e3ccSAndroid Build Coastguard Worker if (move_size > data_size || move_src_offset > data_size - move_size) {
3462*77c1e3ccSAndroid Build Coastguard Worker assert(0 && "obu_memmove: output buffer overflow");
3463*77c1e3ccSAndroid Build Coastguard Worker return 0;
3464*77c1e3ccSAndroid Build Coastguard Worker }
3465*77c1e3ccSAndroid Build Coastguard Worker if (move_dst_offset > data_size - move_size) {
3466*77c1e3ccSAndroid Build Coastguard Worker // Buffer full.
3467*77c1e3ccSAndroid Build Coastguard Worker return 0;
3468*77c1e3ccSAndroid Build Coastguard Worker }
3469*77c1e3ccSAndroid Build Coastguard Worker memmove(data + move_dst_offset, data + move_src_offset, move_size);
3470*77c1e3ccSAndroid Build Coastguard Worker return length_field_size;
3471*77c1e3ccSAndroid Build Coastguard Worker }
3472*77c1e3ccSAndroid Build Coastguard Worker
3473*77c1e3ccSAndroid Build Coastguard Worker // Deprecated. Use obu_memmove() instead.
obu_memmove_unsafe(size_t obu_header_size,size_t obu_payload_size,uint8_t * data)3474*77c1e3ccSAndroid Build Coastguard Worker static size_t obu_memmove_unsafe(size_t obu_header_size,
3475*77c1e3ccSAndroid Build Coastguard Worker size_t obu_payload_size, uint8_t *data) {
3476*77c1e3ccSAndroid Build Coastguard Worker const size_t length_field_size = aom_uleb_size_in_bytes(obu_payload_size);
3477*77c1e3ccSAndroid Build Coastguard Worker const size_t move_dst_offset = obu_header_size + length_field_size;
3478*77c1e3ccSAndroid Build Coastguard Worker const size_t move_src_offset = obu_header_size;
3479*77c1e3ccSAndroid Build Coastguard Worker const size_t move_size = obu_payload_size;
3480*77c1e3ccSAndroid Build Coastguard Worker memmove(data + move_dst_offset, data + move_src_offset, move_size);
3481*77c1e3ccSAndroid Build Coastguard Worker return length_field_size;
3482*77c1e3ccSAndroid Build Coastguard Worker }
3483*77c1e3ccSAndroid Build Coastguard Worker
add_trailing_bits(struct aom_write_bit_buffer * wb)3484*77c1e3ccSAndroid Build Coastguard Worker static inline void add_trailing_bits(struct aom_write_bit_buffer *wb) {
3485*77c1e3ccSAndroid Build Coastguard Worker if (aom_wb_is_byte_aligned(wb)) {
3486*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, 0x80, 8);
3487*77c1e3ccSAndroid Build Coastguard Worker } else {
3488*77c1e3ccSAndroid Build Coastguard Worker // assumes that the other bits are already 0s
3489*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(wb, 1);
3490*77c1e3ccSAndroid Build Coastguard Worker }
3491*77c1e3ccSAndroid Build Coastguard Worker }
3492*77c1e3ccSAndroid Build Coastguard Worker
write_bitstream_level(AV1_LEVEL seq_level_idx,struct aom_write_bit_buffer * wb)3493*77c1e3ccSAndroid Build Coastguard Worker static inline void write_bitstream_level(AV1_LEVEL seq_level_idx,
3494*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *wb) {
3495*77c1e3ccSAndroid Build Coastguard Worker assert(is_valid_seq_level_idx(seq_level_idx));
3496*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(wb, seq_level_idx, LEVEL_BITS);
3497*77c1e3ccSAndroid Build Coastguard Worker }
3498*77c1e3ccSAndroid Build Coastguard Worker
av1_write_sequence_header_obu(const SequenceHeader * seq_params,uint8_t * const dst,size_t dst_size)3499*77c1e3ccSAndroid Build Coastguard Worker uint32_t av1_write_sequence_header_obu(const SequenceHeader *seq_params,
3500*77c1e3ccSAndroid Build Coastguard Worker uint8_t *const dst, size_t dst_size) {
3501*77c1e3ccSAndroid Build Coastguard Worker // TODO: bug 42302568 - Use dst_size.
3502*77c1e3ccSAndroid Build Coastguard Worker (void)dst_size;
3503*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer wb = { dst, 0 };
3504*77c1e3ccSAndroid Build Coastguard Worker uint32_t size = 0;
3505*77c1e3ccSAndroid Build Coastguard Worker
3506*77c1e3ccSAndroid Build Coastguard Worker write_profile(seq_params->profile, &wb);
3507*77c1e3ccSAndroid Build Coastguard Worker
3508*77c1e3ccSAndroid Build Coastguard Worker // Still picture or not
3509*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(&wb, seq_params->still_picture);
3510*77c1e3ccSAndroid Build Coastguard Worker assert(IMPLIES(!seq_params->still_picture,
3511*77c1e3ccSAndroid Build Coastguard Worker !seq_params->reduced_still_picture_hdr));
3512*77c1e3ccSAndroid Build Coastguard Worker // whether to use reduced still picture header
3513*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(&wb, seq_params->reduced_still_picture_hdr);
3514*77c1e3ccSAndroid Build Coastguard Worker
3515*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->reduced_still_picture_hdr) {
3516*77c1e3ccSAndroid Build Coastguard Worker assert(seq_params->timing_info_present == 0);
3517*77c1e3ccSAndroid Build Coastguard Worker assert(seq_params->decoder_model_info_present_flag == 0);
3518*77c1e3ccSAndroid Build Coastguard Worker assert(seq_params->display_model_info_present_flag == 0);
3519*77c1e3ccSAndroid Build Coastguard Worker write_bitstream_level(seq_params->seq_level_idx[0], &wb);
3520*77c1e3ccSAndroid Build Coastguard Worker } else {
3521*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(
3522*77c1e3ccSAndroid Build Coastguard Worker &wb, seq_params->timing_info_present); // timing info present flag
3523*77c1e3ccSAndroid Build Coastguard Worker
3524*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->timing_info_present) {
3525*77c1e3ccSAndroid Build Coastguard Worker // timing_info
3526*77c1e3ccSAndroid Build Coastguard Worker write_timing_info_header(&seq_params->timing_info, &wb);
3527*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(&wb, seq_params->decoder_model_info_present_flag);
3528*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->decoder_model_info_present_flag) {
3529*77c1e3ccSAndroid Build Coastguard Worker write_decoder_model_info(&seq_params->decoder_model_info, &wb);
3530*77c1e3ccSAndroid Build Coastguard Worker }
3531*77c1e3ccSAndroid Build Coastguard Worker }
3532*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(&wb, seq_params->display_model_info_present_flag);
3533*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(&wb, seq_params->operating_points_cnt_minus_1,
3534*77c1e3ccSAndroid Build Coastguard Worker OP_POINTS_CNT_MINUS_1_BITS);
3535*77c1e3ccSAndroid Build Coastguard Worker int i;
3536*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < seq_params->operating_points_cnt_minus_1 + 1; i++) {
3537*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(&wb, seq_params->operating_point_idc[i],
3538*77c1e3ccSAndroid Build Coastguard Worker OP_POINTS_IDC_BITS);
3539*77c1e3ccSAndroid Build Coastguard Worker write_bitstream_level(seq_params->seq_level_idx[i], &wb);
3540*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->seq_level_idx[i] >= SEQ_LEVEL_4_0)
3541*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(&wb, seq_params->tier[i]);
3542*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->decoder_model_info_present_flag) {
3543*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(
3544*77c1e3ccSAndroid Build Coastguard Worker &wb, seq_params->op_params[i].decoder_model_param_present_flag);
3545*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->op_params[i].decoder_model_param_present_flag) {
3546*77c1e3ccSAndroid Build Coastguard Worker write_dec_model_op_parameters(
3547*77c1e3ccSAndroid Build Coastguard Worker &seq_params->op_params[i],
3548*77c1e3ccSAndroid Build Coastguard Worker seq_params->decoder_model_info
3549*77c1e3ccSAndroid Build Coastguard Worker .encoder_decoder_buffer_delay_length,
3550*77c1e3ccSAndroid Build Coastguard Worker &wb);
3551*77c1e3ccSAndroid Build Coastguard Worker }
3552*77c1e3ccSAndroid Build Coastguard Worker }
3553*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->display_model_info_present_flag) {
3554*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(
3555*77c1e3ccSAndroid Build Coastguard Worker &wb, seq_params->op_params[i].display_model_param_present_flag);
3556*77c1e3ccSAndroid Build Coastguard Worker if (seq_params->op_params[i].display_model_param_present_flag) {
3557*77c1e3ccSAndroid Build Coastguard Worker assert(seq_params->op_params[i].initial_display_delay >= 1);
3558*77c1e3ccSAndroid Build Coastguard Worker assert(seq_params->op_params[i].initial_display_delay <= 10);
3559*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(
3560*77c1e3ccSAndroid Build Coastguard Worker &wb, seq_params->op_params[i].initial_display_delay - 1, 4);
3561*77c1e3ccSAndroid Build Coastguard Worker }
3562*77c1e3ccSAndroid Build Coastguard Worker }
3563*77c1e3ccSAndroid Build Coastguard Worker }
3564*77c1e3ccSAndroid Build Coastguard Worker }
3565*77c1e3ccSAndroid Build Coastguard Worker write_sequence_header(seq_params, &wb);
3566*77c1e3ccSAndroid Build Coastguard Worker
3567*77c1e3ccSAndroid Build Coastguard Worker write_color_config(seq_params, &wb);
3568*77c1e3ccSAndroid Build Coastguard Worker
3569*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(&wb, seq_params->film_grain_params_present);
3570*77c1e3ccSAndroid Build Coastguard Worker
3571*77c1e3ccSAndroid Build Coastguard Worker add_trailing_bits(&wb);
3572*77c1e3ccSAndroid Build Coastguard Worker
3573*77c1e3ccSAndroid Build Coastguard Worker size = aom_wb_bytes_written(&wb);
3574*77c1e3ccSAndroid Build Coastguard Worker return size;
3575*77c1e3ccSAndroid Build Coastguard Worker }
3576*77c1e3ccSAndroid Build Coastguard Worker
write_frame_header_obu(AV1_COMP * cpi,MACROBLOCKD * const xd,struct aom_write_bit_buffer * saved_wb,uint8_t * const dst,int append_trailing_bits)3577*77c1e3ccSAndroid Build Coastguard Worker static uint32_t write_frame_header_obu(AV1_COMP *cpi, MACROBLOCKD *const xd,
3578*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *saved_wb,
3579*77c1e3ccSAndroid Build Coastguard Worker uint8_t *const dst,
3580*77c1e3ccSAndroid Build Coastguard Worker int append_trailing_bits) {
3581*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer wb = { dst, 0 };
3582*77c1e3ccSAndroid Build Coastguard Worker write_uncompressed_header_obu(cpi, xd, saved_wb, &wb);
3583*77c1e3ccSAndroid Build Coastguard Worker if (append_trailing_bits) add_trailing_bits(&wb);
3584*77c1e3ccSAndroid Build Coastguard Worker return aom_wb_bytes_written(&wb);
3585*77c1e3ccSAndroid Build Coastguard Worker }
3586*77c1e3ccSAndroid Build Coastguard Worker
write_tile_group_header(uint8_t * const dst,int start_tile,int end_tile,int tiles_log2,int tile_start_and_end_present_flag)3587*77c1e3ccSAndroid Build Coastguard Worker static uint32_t write_tile_group_header(uint8_t *const dst, int start_tile,
3588*77c1e3ccSAndroid Build Coastguard Worker int end_tile, int tiles_log2,
3589*77c1e3ccSAndroid Build Coastguard Worker int tile_start_and_end_present_flag) {
3590*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer wb = { dst, 0 };
3591*77c1e3ccSAndroid Build Coastguard Worker uint32_t size = 0;
3592*77c1e3ccSAndroid Build Coastguard Worker
3593*77c1e3ccSAndroid Build Coastguard Worker if (!tiles_log2) return size;
3594*77c1e3ccSAndroid Build Coastguard Worker
3595*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_bit(&wb, tile_start_and_end_present_flag);
3596*77c1e3ccSAndroid Build Coastguard Worker
3597*77c1e3ccSAndroid Build Coastguard Worker if (tile_start_and_end_present_flag) {
3598*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(&wb, start_tile, tiles_log2);
3599*77c1e3ccSAndroid Build Coastguard Worker aom_wb_write_literal(&wb, end_tile, tiles_log2);
3600*77c1e3ccSAndroid Build Coastguard Worker }
3601*77c1e3ccSAndroid Build Coastguard Worker
3602*77c1e3ccSAndroid Build Coastguard Worker size = aom_wb_bytes_written(&wb);
3603*77c1e3ccSAndroid Build Coastguard Worker return size;
3604*77c1e3ccSAndroid Build Coastguard Worker }
3605*77c1e3ccSAndroid Build Coastguard Worker
3606*77c1e3ccSAndroid Build Coastguard Worker typedef struct {
3607*77c1e3ccSAndroid Build Coastguard Worker uint32_t tg_hdr_size;
3608*77c1e3ccSAndroid Build Coastguard Worker uint32_t frame_header_size;
3609*77c1e3ccSAndroid Build Coastguard Worker } LargeTileFrameOBU;
3610*77c1e3ccSAndroid Build Coastguard Worker
3611*77c1e3ccSAndroid Build Coastguard Worker // Initialize OBU header for large scale tile case.
init_large_scale_tile_obu_header(AV1_COMP * const cpi,uint8_t ** data,struct aom_write_bit_buffer * saved_wb,uint8_t obu_extension_header,LargeTileFrameOBU * lst_obu)3612*77c1e3ccSAndroid Build Coastguard Worker static uint32_t init_large_scale_tile_obu_header(
3613*77c1e3ccSAndroid Build Coastguard Worker AV1_COMP *const cpi, uint8_t **data, struct aom_write_bit_buffer *saved_wb,
3614*77c1e3ccSAndroid Build Coastguard Worker uint8_t obu_extension_header, LargeTileFrameOBU *lst_obu) {
3615*77c1e3ccSAndroid Build Coastguard Worker AV1LevelParams *const level_params = &cpi->ppi->level_params;
3616*77c1e3ccSAndroid Build Coastguard Worker CurrentFrame *const current_frame = &cpi->common.current_frame;
3617*77c1e3ccSAndroid Build Coastguard Worker // For large_scale_tile case, we always have only one tile group, so it can
3618*77c1e3ccSAndroid Build Coastguard Worker // be written as an OBU_FRAME.
3619*77c1e3ccSAndroid Build Coastguard Worker const OBU_TYPE obu_type = OBU_FRAME;
3620*77c1e3ccSAndroid Build Coastguard Worker lst_obu->tg_hdr_size = av1_write_obu_header(
3621*77c1e3ccSAndroid Build Coastguard Worker level_params, &cpi->frame_header_count, obu_type,
3622*77c1e3ccSAndroid Build Coastguard Worker cpi->common.seq_params->has_nonzero_operating_point_idc,
3623*77c1e3ccSAndroid Build Coastguard Worker obu_extension_header, *data);
3624*77c1e3ccSAndroid Build Coastguard Worker *data += lst_obu->tg_hdr_size;
3625*77c1e3ccSAndroid Build Coastguard Worker
3626*77c1e3ccSAndroid Build Coastguard Worker const uint32_t frame_header_size =
3627*77c1e3ccSAndroid Build Coastguard Worker write_frame_header_obu(cpi, &cpi->td.mb.e_mbd, saved_wb, *data, 0);
3628*77c1e3ccSAndroid Build Coastguard Worker *data += frame_header_size;
3629*77c1e3ccSAndroid Build Coastguard Worker lst_obu->frame_header_size = frame_header_size;
3630*77c1e3ccSAndroid Build Coastguard Worker // (yunqing) This test ensures the correctness of large scale tile coding.
3631*77c1e3ccSAndroid Build Coastguard Worker if (cpi->oxcf.tile_cfg.enable_ext_tile_debug) {
3632*77c1e3ccSAndroid Build Coastguard Worker char fn[20] = "./fh";
3633*77c1e3ccSAndroid Build Coastguard Worker fn[4] = current_frame->frame_number / 100 + '0';
3634*77c1e3ccSAndroid Build Coastguard Worker fn[5] = (current_frame->frame_number % 100) / 10 + '0';
3635*77c1e3ccSAndroid Build Coastguard Worker fn[6] = (current_frame->frame_number % 10) + '0';
3636*77c1e3ccSAndroid Build Coastguard Worker fn[7] = '\0';
3637*77c1e3ccSAndroid Build Coastguard Worker av1_print_uncompressed_frame_header(*data - frame_header_size,
3638*77c1e3ccSAndroid Build Coastguard Worker frame_header_size, fn);
3639*77c1e3ccSAndroid Build Coastguard Worker }
3640*77c1e3ccSAndroid Build Coastguard Worker return frame_header_size;
3641*77c1e3ccSAndroid Build Coastguard Worker }
3642*77c1e3ccSAndroid Build Coastguard Worker
3643*77c1e3ccSAndroid Build Coastguard Worker // Write total buffer size and related information into the OBU header for large
3644*77c1e3ccSAndroid Build Coastguard Worker // scale tile case.
write_large_scale_tile_obu_size(const CommonTileParams * const tiles,uint8_t * const dst,uint8_t * data,struct aom_write_bit_buffer * saved_wb,LargeTileFrameOBU * const lst_obu,int have_tiles,uint32_t * total_size,int max_tile_size,int max_tile_col_size)3645*77c1e3ccSAndroid Build Coastguard Worker static void write_large_scale_tile_obu_size(
3646*77c1e3ccSAndroid Build Coastguard Worker const CommonTileParams *const tiles, uint8_t *const dst, uint8_t *data,
3647*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *saved_wb, LargeTileFrameOBU *const lst_obu,
3648*77c1e3ccSAndroid Build Coastguard Worker int have_tiles, uint32_t *total_size, int max_tile_size,
3649*77c1e3ccSAndroid Build Coastguard Worker int max_tile_col_size) {
3650*77c1e3ccSAndroid Build Coastguard Worker int tile_size_bytes = 0;
3651*77c1e3ccSAndroid Build Coastguard Worker int tile_col_size_bytes = 0;
3652*77c1e3ccSAndroid Build Coastguard Worker if (have_tiles) {
3653*77c1e3ccSAndroid Build Coastguard Worker *total_size = remux_tiles(
3654*77c1e3ccSAndroid Build Coastguard Worker tiles, data, *total_size - lst_obu->frame_header_size, max_tile_size,
3655*77c1e3ccSAndroid Build Coastguard Worker max_tile_col_size, &tile_size_bytes, &tile_col_size_bytes);
3656*77c1e3ccSAndroid Build Coastguard Worker *total_size += lst_obu->frame_header_size;
3657*77c1e3ccSAndroid Build Coastguard Worker }
3658*77c1e3ccSAndroid Build Coastguard Worker
3659*77c1e3ccSAndroid Build Coastguard Worker // In EXT_TILE case, only use 1 tile group. Follow the obu syntax, write
3660*77c1e3ccSAndroid Build Coastguard Worker // current tile group size before tile data(include tile column header).
3661*77c1e3ccSAndroid Build Coastguard Worker // Tile group size doesn't include the bytes storing tg size.
3662*77c1e3ccSAndroid Build Coastguard Worker *total_size += lst_obu->tg_hdr_size;
3663*77c1e3ccSAndroid Build Coastguard Worker const uint32_t obu_payload_size = *total_size - lst_obu->tg_hdr_size;
3664*77c1e3ccSAndroid Build Coastguard Worker const size_t length_field_size =
3665*77c1e3ccSAndroid Build Coastguard Worker obu_memmove_unsafe(lst_obu->tg_hdr_size, obu_payload_size, dst);
3666*77c1e3ccSAndroid Build Coastguard Worker if (av1_write_uleb_obu_size_unsafe(
3667*77c1e3ccSAndroid Build Coastguard Worker obu_payload_size, dst + lst_obu->tg_hdr_size) != AOM_CODEC_OK)
3668*77c1e3ccSAndroid Build Coastguard Worker assert(0);
3669*77c1e3ccSAndroid Build Coastguard Worker
3670*77c1e3ccSAndroid Build Coastguard Worker *total_size += (uint32_t)length_field_size;
3671*77c1e3ccSAndroid Build Coastguard Worker saved_wb->bit_buffer += length_field_size;
3672*77c1e3ccSAndroid Build Coastguard Worker
3673*77c1e3ccSAndroid Build Coastguard Worker // Now fill in the gaps in the uncompressed header.
3674*77c1e3ccSAndroid Build Coastguard Worker if (have_tiles) {
3675*77c1e3ccSAndroid Build Coastguard Worker assert(tile_col_size_bytes >= 1 && tile_col_size_bytes <= 4);
3676*77c1e3ccSAndroid Build Coastguard Worker aom_wb_overwrite_literal(saved_wb, tile_col_size_bytes - 1, 2);
3677*77c1e3ccSAndroid Build Coastguard Worker
3678*77c1e3ccSAndroid Build Coastguard Worker assert(tile_size_bytes >= 1 && tile_size_bytes <= 4);
3679*77c1e3ccSAndroid Build Coastguard Worker aom_wb_overwrite_literal(saved_wb, tile_size_bytes - 1, 2);
3680*77c1e3ccSAndroid Build Coastguard Worker }
3681*77c1e3ccSAndroid Build Coastguard Worker }
3682*77c1e3ccSAndroid Build Coastguard Worker
3683*77c1e3ccSAndroid Build Coastguard Worker // Store information on each large scale tile in the OBU header.
write_large_scale_tile_obu(AV1_COMP * const cpi,uint8_t * const dst,LargeTileFrameOBU * const lst_obu,int * const largest_tile_id,uint32_t * total_size,const int have_tiles,unsigned int * const max_tile_size,unsigned int * const max_tile_col_size)3684*77c1e3ccSAndroid Build Coastguard Worker static void write_large_scale_tile_obu(
3685*77c1e3ccSAndroid Build Coastguard Worker AV1_COMP *const cpi, uint8_t *const dst, LargeTileFrameOBU *const lst_obu,
3686*77c1e3ccSAndroid Build Coastguard Worker int *const largest_tile_id, uint32_t *total_size, const int have_tiles,
3687*77c1e3ccSAndroid Build Coastguard Worker unsigned int *const max_tile_size, unsigned int *const max_tile_col_size) {
3688*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
3689*77c1e3ccSAndroid Build Coastguard Worker const CommonTileParams *const tiles = &cm->tiles;
3690*77c1e3ccSAndroid Build Coastguard Worker
3691*77c1e3ccSAndroid Build Coastguard Worker TileBufferEnc tile_buffers[MAX_TILE_ROWS][MAX_TILE_COLS];
3692*77c1e3ccSAndroid Build Coastguard Worker const int tile_cols = tiles->cols;
3693*77c1e3ccSAndroid Build Coastguard Worker const int tile_rows = tiles->rows;
3694*77c1e3ccSAndroid Build Coastguard Worker unsigned int tile_size = 0;
3695*77c1e3ccSAndroid Build Coastguard Worker
3696*77c1e3ccSAndroid Build Coastguard Worker av1_reset_pack_bs_thread_data(&cpi->td);
3697*77c1e3ccSAndroid Build Coastguard Worker for (int tile_col = 0; tile_col < tile_cols; tile_col++) {
3698*77c1e3ccSAndroid Build Coastguard Worker TileInfo tile_info;
3699*77c1e3ccSAndroid Build Coastguard Worker const int is_last_col = (tile_col == tile_cols - 1);
3700*77c1e3ccSAndroid Build Coastguard Worker const uint32_t col_offset = *total_size;
3701*77c1e3ccSAndroid Build Coastguard Worker
3702*77c1e3ccSAndroid Build Coastguard Worker av1_tile_set_col(&tile_info, cm, tile_col);
3703*77c1e3ccSAndroid Build Coastguard Worker
3704*77c1e3ccSAndroid Build Coastguard Worker // The last column does not have a column header
3705*77c1e3ccSAndroid Build Coastguard Worker if (!is_last_col) *total_size += 4;
3706*77c1e3ccSAndroid Build Coastguard Worker
3707*77c1e3ccSAndroid Build Coastguard Worker for (int tile_row = 0; tile_row < tile_rows; tile_row++) {
3708*77c1e3ccSAndroid Build Coastguard Worker TileBufferEnc *const buf = &tile_buffers[tile_row][tile_col];
3709*77c1e3ccSAndroid Build Coastguard Worker const int data_offset = have_tiles ? 4 : 0;
3710*77c1e3ccSAndroid Build Coastguard Worker const int tile_idx = tile_row * tile_cols + tile_col;
3711*77c1e3ccSAndroid Build Coastguard Worker TileDataEnc *this_tile = &cpi->tile_data[tile_idx];
3712*77c1e3ccSAndroid Build Coastguard Worker av1_tile_set_row(&tile_info, cm, tile_row);
3713*77c1e3ccSAndroid Build Coastguard Worker aom_writer mode_bc;
3714*77c1e3ccSAndroid Build Coastguard Worker
3715*77c1e3ccSAndroid Build Coastguard Worker buf->data = dst + *total_size + lst_obu->tg_hdr_size;
3716*77c1e3ccSAndroid Build Coastguard Worker
3717*77c1e3ccSAndroid Build Coastguard Worker // Is CONFIG_EXT_TILE = 1, every tile in the row has a header,
3718*77c1e3ccSAndroid Build Coastguard Worker // even for the last one, unless no tiling is used at all.
3719*77c1e3ccSAndroid Build Coastguard Worker *total_size += data_offset;
3720*77c1e3ccSAndroid Build Coastguard Worker cpi->td.mb.e_mbd.tile_ctx = &this_tile->tctx;
3721*77c1e3ccSAndroid Build Coastguard Worker mode_bc.allow_update_cdf = !tiles->large_scale;
3722*77c1e3ccSAndroid Build Coastguard Worker mode_bc.allow_update_cdf =
3723*77c1e3ccSAndroid Build Coastguard Worker mode_bc.allow_update_cdf && !cm->features.disable_cdf_update;
3724*77c1e3ccSAndroid Build Coastguard Worker aom_start_encode(&mode_bc, buf->data + data_offset);
3725*77c1e3ccSAndroid Build Coastguard Worker write_modes(cpi, &cpi->td, &tile_info, &mode_bc, tile_row, tile_col);
3726*77c1e3ccSAndroid Build Coastguard Worker if (aom_stop_encode(&mode_bc) < 0) {
3727*77c1e3ccSAndroid Build Coastguard Worker aom_internal_error(cm->error, AOM_CODEC_ERROR, "Error writing modes");
3728*77c1e3ccSAndroid Build Coastguard Worker }
3729*77c1e3ccSAndroid Build Coastguard Worker tile_size = mode_bc.pos;
3730*77c1e3ccSAndroid Build Coastguard Worker buf->size = tile_size;
3731*77c1e3ccSAndroid Build Coastguard Worker
3732*77c1e3ccSAndroid Build Coastguard Worker // Record the maximum tile size we see, so we can compact headers later.
3733*77c1e3ccSAndroid Build Coastguard Worker if (tile_size > *max_tile_size) {
3734*77c1e3ccSAndroid Build Coastguard Worker *max_tile_size = tile_size;
3735*77c1e3ccSAndroid Build Coastguard Worker *largest_tile_id = tile_cols * tile_row + tile_col;
3736*77c1e3ccSAndroid Build Coastguard Worker }
3737*77c1e3ccSAndroid Build Coastguard Worker
3738*77c1e3ccSAndroid Build Coastguard Worker if (have_tiles) {
3739*77c1e3ccSAndroid Build Coastguard Worker // tile header: size of this tile, or copy offset
3740*77c1e3ccSAndroid Build Coastguard Worker uint32_t tile_header = tile_size - AV1_MIN_TILE_SIZE_BYTES;
3741*77c1e3ccSAndroid Build Coastguard Worker const int tile_copy_mode =
3742*77c1e3ccSAndroid Build Coastguard Worker ((AOMMAX(tiles->width, tiles->height) << MI_SIZE_LOG2) <= 256) ? 1
3743*77c1e3ccSAndroid Build Coastguard Worker : 0;
3744*77c1e3ccSAndroid Build Coastguard Worker
3745*77c1e3ccSAndroid Build Coastguard Worker // If tile_copy_mode = 1, check if this tile is a copy tile.
3746*77c1e3ccSAndroid Build Coastguard Worker // Very low chances to have copy tiles on the key frames, so don't
3747*77c1e3ccSAndroid Build Coastguard Worker // search on key frames to reduce unnecessary search.
3748*77c1e3ccSAndroid Build Coastguard Worker if (cm->current_frame.frame_type != KEY_FRAME && tile_copy_mode) {
3749*77c1e3ccSAndroid Build Coastguard Worker const int identical_tile_offset =
3750*77c1e3ccSAndroid Build Coastguard Worker find_identical_tile(tile_row, tile_col, tile_buffers);
3751*77c1e3ccSAndroid Build Coastguard Worker
3752*77c1e3ccSAndroid Build Coastguard Worker // Indicate a copy-tile by setting the most significant bit.
3753*77c1e3ccSAndroid Build Coastguard Worker // The row-offset to copy from is stored in the highest byte.
3754*77c1e3ccSAndroid Build Coastguard Worker // remux_tiles will move these around later
3755*77c1e3ccSAndroid Build Coastguard Worker if (identical_tile_offset > 0) {
3756*77c1e3ccSAndroid Build Coastguard Worker tile_size = 0;
3757*77c1e3ccSAndroid Build Coastguard Worker tile_header = identical_tile_offset | 0x80;
3758*77c1e3ccSAndroid Build Coastguard Worker tile_header <<= 24;
3759*77c1e3ccSAndroid Build Coastguard Worker }
3760*77c1e3ccSAndroid Build Coastguard Worker }
3761*77c1e3ccSAndroid Build Coastguard Worker
3762*77c1e3ccSAndroid Build Coastguard Worker mem_put_le32(buf->data, (MEM_VALUE_T)tile_header);
3763*77c1e3ccSAndroid Build Coastguard Worker }
3764*77c1e3ccSAndroid Build Coastguard Worker
3765*77c1e3ccSAndroid Build Coastguard Worker *total_size += tile_size;
3766*77c1e3ccSAndroid Build Coastguard Worker }
3767*77c1e3ccSAndroid Build Coastguard Worker if (!is_last_col) {
3768*77c1e3ccSAndroid Build Coastguard Worker uint32_t col_size = *total_size - col_offset - 4;
3769*77c1e3ccSAndroid Build Coastguard Worker mem_put_le32(dst + col_offset + lst_obu->tg_hdr_size, col_size);
3770*77c1e3ccSAndroid Build Coastguard Worker
3771*77c1e3ccSAndroid Build Coastguard Worker // Record the maximum tile column size we see.
3772*77c1e3ccSAndroid Build Coastguard Worker *max_tile_col_size = AOMMAX(*max_tile_col_size, col_size);
3773*77c1e3ccSAndroid Build Coastguard Worker }
3774*77c1e3ccSAndroid Build Coastguard Worker }
3775*77c1e3ccSAndroid Build Coastguard Worker av1_accumulate_pack_bs_thread_data(cpi, &cpi->td);
3776*77c1e3ccSAndroid Build Coastguard Worker }
3777*77c1e3ccSAndroid Build Coastguard Worker
3778*77c1e3ccSAndroid Build Coastguard Worker // Packs information in the obu header for large scale tiles.
pack_large_scale_tiles_in_tg_obus(AV1_COMP * const cpi,uint8_t * const dst,struct aom_write_bit_buffer * saved_wb,uint8_t obu_extension_header,int * const largest_tile_id)3779*77c1e3ccSAndroid Build Coastguard Worker static inline uint32_t pack_large_scale_tiles_in_tg_obus(
3780*77c1e3ccSAndroid Build Coastguard Worker AV1_COMP *const cpi, uint8_t *const dst,
3781*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *saved_wb, uint8_t obu_extension_header,
3782*77c1e3ccSAndroid Build Coastguard Worker int *const largest_tile_id) {
3783*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
3784*77c1e3ccSAndroid Build Coastguard Worker const CommonTileParams *const tiles = &cm->tiles;
3785*77c1e3ccSAndroid Build Coastguard Worker uint32_t total_size = 0;
3786*77c1e3ccSAndroid Build Coastguard Worker unsigned int max_tile_size = 0;
3787*77c1e3ccSAndroid Build Coastguard Worker unsigned int max_tile_col_size = 0;
3788*77c1e3ccSAndroid Build Coastguard Worker const int have_tiles = tiles->cols * tiles->rows > 1;
3789*77c1e3ccSAndroid Build Coastguard Worker uint8_t *data = dst;
3790*77c1e3ccSAndroid Build Coastguard Worker
3791*77c1e3ccSAndroid Build Coastguard Worker LargeTileFrameOBU lst_obu;
3792*77c1e3ccSAndroid Build Coastguard Worker
3793*77c1e3ccSAndroid Build Coastguard Worker total_size += init_large_scale_tile_obu_header(
3794*77c1e3ccSAndroid Build Coastguard Worker cpi, &data, saved_wb, obu_extension_header, &lst_obu);
3795*77c1e3ccSAndroid Build Coastguard Worker
3796*77c1e3ccSAndroid Build Coastguard Worker write_large_scale_tile_obu(cpi, dst, &lst_obu, largest_tile_id, &total_size,
3797*77c1e3ccSAndroid Build Coastguard Worker have_tiles, &max_tile_size, &max_tile_col_size);
3798*77c1e3ccSAndroid Build Coastguard Worker
3799*77c1e3ccSAndroid Build Coastguard Worker write_large_scale_tile_obu_size(tiles, dst, data, saved_wb, &lst_obu,
3800*77c1e3ccSAndroid Build Coastguard Worker have_tiles, &total_size, max_tile_size,
3801*77c1e3ccSAndroid Build Coastguard Worker max_tile_col_size);
3802*77c1e3ccSAndroid Build Coastguard Worker
3803*77c1e3ccSAndroid Build Coastguard Worker return total_size;
3804*77c1e3ccSAndroid Build Coastguard Worker }
3805*77c1e3ccSAndroid Build Coastguard Worker
3806*77c1e3ccSAndroid Build Coastguard Worker // Writes obu, tile group and uncompressed headers to bitstream.
av1_write_obu_tg_tile_headers(AV1_COMP * const cpi,MACROBLOCKD * const xd,PackBSParams * const pack_bs_params,const int tile_idx)3807*77c1e3ccSAndroid Build Coastguard Worker void av1_write_obu_tg_tile_headers(AV1_COMP *const cpi, MACROBLOCKD *const xd,
3808*77c1e3ccSAndroid Build Coastguard Worker PackBSParams *const pack_bs_params,
3809*77c1e3ccSAndroid Build Coastguard Worker const int tile_idx) {
3810*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
3811*77c1e3ccSAndroid Build Coastguard Worker const CommonTileParams *const tiles = &cm->tiles;
3812*77c1e3ccSAndroid Build Coastguard Worker int *const curr_tg_hdr_size = &pack_bs_params->curr_tg_hdr_size;
3813*77c1e3ccSAndroid Build Coastguard Worker const int tg_size =
3814*77c1e3ccSAndroid Build Coastguard Worker (tiles->rows * tiles->cols + cpi->num_tg - 1) / cpi->num_tg;
3815*77c1e3ccSAndroid Build Coastguard Worker
3816*77c1e3ccSAndroid Build Coastguard Worker // Write Tile group, frame and OBU header
3817*77c1e3ccSAndroid Build Coastguard Worker // A new tile group begins at this tile. Write the obu header and
3818*77c1e3ccSAndroid Build Coastguard Worker // tile group header
3819*77c1e3ccSAndroid Build Coastguard Worker const OBU_TYPE obu_type = (cpi->num_tg == 1) ? OBU_FRAME : OBU_TILE_GROUP;
3820*77c1e3ccSAndroid Build Coastguard Worker *curr_tg_hdr_size = av1_write_obu_header(
3821*77c1e3ccSAndroid Build Coastguard Worker &cpi->ppi->level_params, &cpi->frame_header_count, obu_type,
3822*77c1e3ccSAndroid Build Coastguard Worker cm->seq_params->has_nonzero_operating_point_idc,
3823*77c1e3ccSAndroid Build Coastguard Worker pack_bs_params->obu_extn_header, pack_bs_params->tile_data_curr);
3824*77c1e3ccSAndroid Build Coastguard Worker pack_bs_params->obu_header_size = *curr_tg_hdr_size;
3825*77c1e3ccSAndroid Build Coastguard Worker
3826*77c1e3ccSAndroid Build Coastguard Worker if (cpi->num_tg == 1)
3827*77c1e3ccSAndroid Build Coastguard Worker *curr_tg_hdr_size += write_frame_header_obu(
3828*77c1e3ccSAndroid Build Coastguard Worker cpi, xd, pack_bs_params->saved_wb,
3829*77c1e3ccSAndroid Build Coastguard Worker pack_bs_params->tile_data_curr + *curr_tg_hdr_size, 0);
3830*77c1e3ccSAndroid Build Coastguard Worker *curr_tg_hdr_size += write_tile_group_header(
3831*77c1e3ccSAndroid Build Coastguard Worker pack_bs_params->tile_data_curr + *curr_tg_hdr_size, tile_idx,
3832*77c1e3ccSAndroid Build Coastguard Worker AOMMIN(tile_idx + tg_size - 1, tiles->cols * tiles->rows - 1),
3833*77c1e3ccSAndroid Build Coastguard Worker (tiles->log2_rows + tiles->log2_cols), cpi->num_tg > 1);
3834*77c1e3ccSAndroid Build Coastguard Worker *pack_bs_params->total_size += *curr_tg_hdr_size;
3835*77c1e3ccSAndroid Build Coastguard Worker }
3836*77c1e3ccSAndroid Build Coastguard Worker
3837*77c1e3ccSAndroid Build Coastguard Worker // Pack tile data in the bitstream with tile_group, frame
3838*77c1e3ccSAndroid Build Coastguard Worker // and OBU header.
av1_pack_tile_info(AV1_COMP * const cpi,ThreadData * const td,PackBSParams * const pack_bs_params)3839*77c1e3ccSAndroid Build Coastguard Worker void av1_pack_tile_info(AV1_COMP *const cpi, ThreadData *const td,
3840*77c1e3ccSAndroid Build Coastguard Worker PackBSParams *const pack_bs_params) {
3841*77c1e3ccSAndroid Build Coastguard Worker aom_writer mode_bc;
3842*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
3843*77c1e3ccSAndroid Build Coastguard Worker int tile_row = pack_bs_params->tile_row;
3844*77c1e3ccSAndroid Build Coastguard Worker int tile_col = pack_bs_params->tile_col;
3845*77c1e3ccSAndroid Build Coastguard Worker uint32_t *const total_size = pack_bs_params->total_size;
3846*77c1e3ccSAndroid Build Coastguard Worker TileInfo tile_info;
3847*77c1e3ccSAndroid Build Coastguard Worker av1_tile_set_col(&tile_info, cm, tile_col);
3848*77c1e3ccSAndroid Build Coastguard Worker av1_tile_set_row(&tile_info, cm, tile_row);
3849*77c1e3ccSAndroid Build Coastguard Worker mode_bc.allow_update_cdf = 1;
3850*77c1e3ccSAndroid Build Coastguard Worker mode_bc.allow_update_cdf =
3851*77c1e3ccSAndroid Build Coastguard Worker mode_bc.allow_update_cdf && !cm->features.disable_cdf_update;
3852*77c1e3ccSAndroid Build Coastguard Worker
3853*77c1e3ccSAndroid Build Coastguard Worker unsigned int tile_size;
3854*77c1e3ccSAndroid Build Coastguard Worker
3855*77c1e3ccSAndroid Build Coastguard Worker const int num_planes = av1_num_planes(cm);
3856*77c1e3ccSAndroid Build Coastguard Worker av1_reset_loop_restoration(&td->mb.e_mbd, num_planes);
3857*77c1e3ccSAndroid Build Coastguard Worker
3858*77c1e3ccSAndroid Build Coastguard Worker pack_bs_params->buf.data = pack_bs_params->dst + *total_size;
3859*77c1e3ccSAndroid Build Coastguard Worker
3860*77c1e3ccSAndroid Build Coastguard Worker // The last tile of the tile group does not have a header.
3861*77c1e3ccSAndroid Build Coastguard Worker if (!pack_bs_params->is_last_tile_in_tg) *total_size += 4;
3862*77c1e3ccSAndroid Build Coastguard Worker
3863*77c1e3ccSAndroid Build Coastguard Worker // Pack tile data
3864*77c1e3ccSAndroid Build Coastguard Worker aom_start_encode(&mode_bc, pack_bs_params->dst + *total_size);
3865*77c1e3ccSAndroid Build Coastguard Worker write_modes(cpi, td, &tile_info, &mode_bc, tile_row, tile_col);
3866*77c1e3ccSAndroid Build Coastguard Worker if (aom_stop_encode(&mode_bc) < 0) {
3867*77c1e3ccSAndroid Build Coastguard Worker aom_internal_error(td->mb.e_mbd.error_info, AOM_CODEC_ERROR,
3868*77c1e3ccSAndroid Build Coastguard Worker "Error writing modes");
3869*77c1e3ccSAndroid Build Coastguard Worker }
3870*77c1e3ccSAndroid Build Coastguard Worker tile_size = mode_bc.pos;
3871*77c1e3ccSAndroid Build Coastguard Worker assert(tile_size >= AV1_MIN_TILE_SIZE_BYTES);
3872*77c1e3ccSAndroid Build Coastguard Worker
3873*77c1e3ccSAndroid Build Coastguard Worker pack_bs_params->buf.size = tile_size;
3874*77c1e3ccSAndroid Build Coastguard Worker
3875*77c1e3ccSAndroid Build Coastguard Worker // Write tile size
3876*77c1e3ccSAndroid Build Coastguard Worker if (!pack_bs_params->is_last_tile_in_tg) {
3877*77c1e3ccSAndroid Build Coastguard Worker // size of this tile
3878*77c1e3ccSAndroid Build Coastguard Worker mem_put_le32(pack_bs_params->buf.data, tile_size - AV1_MIN_TILE_SIZE_BYTES);
3879*77c1e3ccSAndroid Build Coastguard Worker }
3880*77c1e3ccSAndroid Build Coastguard Worker }
3881*77c1e3ccSAndroid Build Coastguard Worker
av1_write_last_tile_info(AV1_COMP * const cpi,const FrameHeaderInfo * fh_info,struct aom_write_bit_buffer * saved_wb,size_t * curr_tg_data_size,uint8_t * curr_tg_start,uint32_t * const total_size,uint8_t ** tile_data_start,int * const largest_tile_id,int * const is_first_tg,uint32_t obu_header_size,uint8_t obu_extn_header)3882*77c1e3ccSAndroid Build Coastguard Worker void av1_write_last_tile_info(
3883*77c1e3ccSAndroid Build Coastguard Worker AV1_COMP *const cpi, const FrameHeaderInfo *fh_info,
3884*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *saved_wb, size_t *curr_tg_data_size,
3885*77c1e3ccSAndroid Build Coastguard Worker uint8_t *curr_tg_start, uint32_t *const total_size,
3886*77c1e3ccSAndroid Build Coastguard Worker uint8_t **tile_data_start, int *const largest_tile_id,
3887*77c1e3ccSAndroid Build Coastguard Worker int *const is_first_tg, uint32_t obu_header_size, uint8_t obu_extn_header) {
3888*77c1e3ccSAndroid Build Coastguard Worker // write current tile group size
3889*77c1e3ccSAndroid Build Coastguard Worker const size_t obu_payload_size = *curr_tg_data_size - obu_header_size;
3890*77c1e3ccSAndroid Build Coastguard Worker const size_t length_field_size =
3891*77c1e3ccSAndroid Build Coastguard Worker obu_memmove_unsafe(obu_header_size, obu_payload_size, curr_tg_start);
3892*77c1e3ccSAndroid Build Coastguard Worker if (av1_write_uleb_obu_size_unsafe(
3893*77c1e3ccSAndroid Build Coastguard Worker obu_payload_size, curr_tg_start + obu_header_size) != AOM_CODEC_OK) {
3894*77c1e3ccSAndroid Build Coastguard Worker aom_internal_error(cpi->common.error, AOM_CODEC_ERROR,
3895*77c1e3ccSAndroid Build Coastguard Worker "av1_write_last_tile_info: output buffer full");
3896*77c1e3ccSAndroid Build Coastguard Worker }
3897*77c1e3ccSAndroid Build Coastguard Worker *curr_tg_data_size += length_field_size;
3898*77c1e3ccSAndroid Build Coastguard Worker *total_size += (uint32_t)length_field_size;
3899*77c1e3ccSAndroid Build Coastguard Worker *tile_data_start += length_field_size;
3900*77c1e3ccSAndroid Build Coastguard Worker if (cpi->num_tg == 1) {
3901*77c1e3ccSAndroid Build Coastguard Worker // if this tg is combined with the frame header then update saved
3902*77c1e3ccSAndroid Build Coastguard Worker // frame header base offset according to length field size
3903*77c1e3ccSAndroid Build Coastguard Worker saved_wb->bit_buffer += length_field_size;
3904*77c1e3ccSAndroid Build Coastguard Worker }
3905*77c1e3ccSAndroid Build Coastguard Worker
3906*77c1e3ccSAndroid Build Coastguard Worker if (!(*is_first_tg) && cpi->common.features.error_resilient_mode) {
3907*77c1e3ccSAndroid Build Coastguard Worker // Make room for a duplicate Frame Header OBU.
3908*77c1e3ccSAndroid Build Coastguard Worker memmove(curr_tg_start + fh_info->total_length, curr_tg_start,
3909*77c1e3ccSAndroid Build Coastguard Worker *curr_tg_data_size);
3910*77c1e3ccSAndroid Build Coastguard Worker
3911*77c1e3ccSAndroid Build Coastguard Worker // Insert a copy of the Frame Header OBU.
3912*77c1e3ccSAndroid Build Coastguard Worker memcpy(curr_tg_start, fh_info->frame_header, fh_info->total_length);
3913*77c1e3ccSAndroid Build Coastguard Worker
3914*77c1e3ccSAndroid Build Coastguard Worker // Force context update tile to be the first tile in error
3915*77c1e3ccSAndroid Build Coastguard Worker // resilient mode as the duplicate frame headers will have
3916*77c1e3ccSAndroid Build Coastguard Worker // context_update_tile_id set to 0
3917*77c1e3ccSAndroid Build Coastguard Worker *largest_tile_id = 0;
3918*77c1e3ccSAndroid Build Coastguard Worker
3919*77c1e3ccSAndroid Build Coastguard Worker // Rewrite the OBU header to change the OBU type to Redundant Frame
3920*77c1e3ccSAndroid Build Coastguard Worker // Header.
3921*77c1e3ccSAndroid Build Coastguard Worker av1_write_obu_header(
3922*77c1e3ccSAndroid Build Coastguard Worker &cpi->ppi->level_params, &cpi->frame_header_count,
3923*77c1e3ccSAndroid Build Coastguard Worker OBU_REDUNDANT_FRAME_HEADER,
3924*77c1e3ccSAndroid Build Coastguard Worker cpi->common.seq_params->has_nonzero_operating_point_idc,
3925*77c1e3ccSAndroid Build Coastguard Worker obu_extn_header, &curr_tg_start[fh_info->obu_header_byte_offset]);
3926*77c1e3ccSAndroid Build Coastguard Worker
3927*77c1e3ccSAndroid Build Coastguard Worker *curr_tg_data_size += fh_info->total_length;
3928*77c1e3ccSAndroid Build Coastguard Worker *total_size += (uint32_t)fh_info->total_length;
3929*77c1e3ccSAndroid Build Coastguard Worker }
3930*77c1e3ccSAndroid Build Coastguard Worker *is_first_tg = 0;
3931*77c1e3ccSAndroid Build Coastguard Worker }
3932*77c1e3ccSAndroid Build Coastguard Worker
av1_reset_pack_bs_thread_data(ThreadData * const td)3933*77c1e3ccSAndroid Build Coastguard Worker void av1_reset_pack_bs_thread_data(ThreadData *const td) {
3934*77c1e3ccSAndroid Build Coastguard Worker td->coefficient_size = 0;
3935*77c1e3ccSAndroid Build Coastguard Worker td->max_mv_magnitude = 0;
3936*77c1e3ccSAndroid Build Coastguard Worker av1_zero(td->interp_filter_selected);
3937*77c1e3ccSAndroid Build Coastguard Worker }
3938*77c1e3ccSAndroid Build Coastguard Worker
av1_accumulate_pack_bs_thread_data(AV1_COMP * const cpi,ThreadData const * td)3939*77c1e3ccSAndroid Build Coastguard Worker void av1_accumulate_pack_bs_thread_data(AV1_COMP *const cpi,
3940*77c1e3ccSAndroid Build Coastguard Worker ThreadData const *td) {
3941*77c1e3ccSAndroid Build Coastguard Worker int do_max_mv_magnitude_update = 1;
3942*77c1e3ccSAndroid Build Coastguard Worker cpi->rc.coefficient_size += td->coefficient_size;
3943*77c1e3ccSAndroid Build Coastguard Worker
3944*77c1e3ccSAndroid Build Coastguard Worker // Disable max_mv_magnitude update for parallel frames based on update flag.
3945*77c1e3ccSAndroid Build Coastguard Worker if (!cpi->do_frame_data_update) do_max_mv_magnitude_update = 0;
3946*77c1e3ccSAndroid Build Coastguard Worker
3947*77c1e3ccSAndroid Build Coastguard Worker if (cpi->sf.mv_sf.auto_mv_step_size && do_max_mv_magnitude_update)
3948*77c1e3ccSAndroid Build Coastguard Worker cpi->mv_search_params.max_mv_magnitude =
3949*77c1e3ccSAndroid Build Coastguard Worker AOMMAX(cpi->mv_search_params.max_mv_magnitude, td->max_mv_magnitude);
3950*77c1e3ccSAndroid Build Coastguard Worker
3951*77c1e3ccSAndroid Build Coastguard Worker for (InterpFilter filter = EIGHTTAP_REGULAR; filter < SWITCHABLE; filter++)
3952*77c1e3ccSAndroid Build Coastguard Worker cpi->common.cur_frame->interp_filter_selected[filter] +=
3953*77c1e3ccSAndroid Build Coastguard Worker td->interp_filter_selected[filter];
3954*77c1e3ccSAndroid Build Coastguard Worker }
3955*77c1e3ccSAndroid Build Coastguard Worker
3956*77c1e3ccSAndroid Build Coastguard Worker // Store information related to each default tile in the OBU header.
write_tile_obu(AV1_COMP * const cpi,uint8_t * const dst,uint32_t * total_size,struct aom_write_bit_buffer * saved_wb,uint8_t obu_extn_header,const FrameHeaderInfo * fh_info,int * const largest_tile_id,unsigned int * max_tile_size,uint32_t * const obu_header_size,uint8_t ** tile_data_start)3957*77c1e3ccSAndroid Build Coastguard Worker static void write_tile_obu(
3958*77c1e3ccSAndroid Build Coastguard Worker AV1_COMP *const cpi, uint8_t *const dst, uint32_t *total_size,
3959*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *saved_wb, uint8_t obu_extn_header,
3960*77c1e3ccSAndroid Build Coastguard Worker const FrameHeaderInfo *fh_info, int *const largest_tile_id,
3961*77c1e3ccSAndroid Build Coastguard Worker unsigned int *max_tile_size, uint32_t *const obu_header_size,
3962*77c1e3ccSAndroid Build Coastguard Worker uint8_t **tile_data_start) {
3963*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
3964*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
3965*77c1e3ccSAndroid Build Coastguard Worker const CommonTileParams *const tiles = &cm->tiles;
3966*77c1e3ccSAndroid Build Coastguard Worker const int tile_cols = tiles->cols;
3967*77c1e3ccSAndroid Build Coastguard Worker const int tile_rows = tiles->rows;
3968*77c1e3ccSAndroid Build Coastguard Worker // Fixed size tile groups for the moment
3969*77c1e3ccSAndroid Build Coastguard Worker const int num_tg_hdrs = cpi->num_tg;
3970*77c1e3ccSAndroid Build Coastguard Worker const int tg_size = (tile_rows * tile_cols + num_tg_hdrs - 1) / num_tg_hdrs;
3971*77c1e3ccSAndroid Build Coastguard Worker int tile_count = 0;
3972*77c1e3ccSAndroid Build Coastguard Worker size_t curr_tg_data_size = 0;
3973*77c1e3ccSAndroid Build Coastguard Worker uint8_t *tile_data_curr = dst;
3974*77c1e3ccSAndroid Build Coastguard Worker int new_tg = 1;
3975*77c1e3ccSAndroid Build Coastguard Worker int is_first_tg = 1;
3976*77c1e3ccSAndroid Build Coastguard Worker
3977*77c1e3ccSAndroid Build Coastguard Worker av1_reset_pack_bs_thread_data(&cpi->td);
3978*77c1e3ccSAndroid Build Coastguard Worker for (int tile_row = 0; tile_row < tile_rows; tile_row++) {
3979*77c1e3ccSAndroid Build Coastguard Worker for (int tile_col = 0; tile_col < tile_cols; tile_col++) {
3980*77c1e3ccSAndroid Build Coastguard Worker const int tile_idx = tile_row * tile_cols + tile_col;
3981*77c1e3ccSAndroid Build Coastguard Worker TileDataEnc *this_tile = &cpi->tile_data[tile_idx];
3982*77c1e3ccSAndroid Build Coastguard Worker
3983*77c1e3ccSAndroid Build Coastguard Worker int is_last_tile_in_tg = 0;
3984*77c1e3ccSAndroid Build Coastguard Worker if (new_tg) {
3985*77c1e3ccSAndroid Build Coastguard Worker tile_data_curr = dst + *total_size;
3986*77c1e3ccSAndroid Build Coastguard Worker tile_count = 0;
3987*77c1e3ccSAndroid Build Coastguard Worker }
3988*77c1e3ccSAndroid Build Coastguard Worker tile_count++;
3989*77c1e3ccSAndroid Build Coastguard Worker
3990*77c1e3ccSAndroid Build Coastguard Worker if (tile_count == tg_size || tile_idx == (tile_cols * tile_rows - 1))
3991*77c1e3ccSAndroid Build Coastguard Worker is_last_tile_in_tg = 1;
3992*77c1e3ccSAndroid Build Coastguard Worker
3993*77c1e3ccSAndroid Build Coastguard Worker xd->tile_ctx = &this_tile->tctx;
3994*77c1e3ccSAndroid Build Coastguard Worker
3995*77c1e3ccSAndroid Build Coastguard Worker // PackBSParams stores all parameters required to pack tile and header
3996*77c1e3ccSAndroid Build Coastguard Worker // info.
3997*77c1e3ccSAndroid Build Coastguard Worker PackBSParams pack_bs_params;
3998*77c1e3ccSAndroid Build Coastguard Worker pack_bs_params.dst = dst;
3999*77c1e3ccSAndroid Build Coastguard Worker pack_bs_params.curr_tg_hdr_size = 0;
4000*77c1e3ccSAndroid Build Coastguard Worker pack_bs_params.is_last_tile_in_tg = is_last_tile_in_tg;
4001*77c1e3ccSAndroid Build Coastguard Worker pack_bs_params.new_tg = new_tg;
4002*77c1e3ccSAndroid Build Coastguard Worker pack_bs_params.obu_extn_header = obu_extn_header;
4003*77c1e3ccSAndroid Build Coastguard Worker pack_bs_params.obu_header_size = 0;
4004*77c1e3ccSAndroid Build Coastguard Worker pack_bs_params.saved_wb = saved_wb;
4005*77c1e3ccSAndroid Build Coastguard Worker pack_bs_params.tile_col = tile_col;
4006*77c1e3ccSAndroid Build Coastguard Worker pack_bs_params.tile_row = tile_row;
4007*77c1e3ccSAndroid Build Coastguard Worker pack_bs_params.tile_data_curr = tile_data_curr;
4008*77c1e3ccSAndroid Build Coastguard Worker pack_bs_params.total_size = total_size;
4009*77c1e3ccSAndroid Build Coastguard Worker
4010*77c1e3ccSAndroid Build Coastguard Worker if (new_tg)
4011*77c1e3ccSAndroid Build Coastguard Worker av1_write_obu_tg_tile_headers(cpi, xd, &pack_bs_params, tile_idx);
4012*77c1e3ccSAndroid Build Coastguard Worker
4013*77c1e3ccSAndroid Build Coastguard Worker av1_pack_tile_info(cpi, &cpi->td, &pack_bs_params);
4014*77c1e3ccSAndroid Build Coastguard Worker
4015*77c1e3ccSAndroid Build Coastguard Worker if (new_tg) {
4016*77c1e3ccSAndroid Build Coastguard Worker curr_tg_data_size = pack_bs_params.curr_tg_hdr_size;
4017*77c1e3ccSAndroid Build Coastguard Worker *tile_data_start += pack_bs_params.curr_tg_hdr_size;
4018*77c1e3ccSAndroid Build Coastguard Worker *obu_header_size = pack_bs_params.obu_header_size;
4019*77c1e3ccSAndroid Build Coastguard Worker new_tg = 0;
4020*77c1e3ccSAndroid Build Coastguard Worker }
4021*77c1e3ccSAndroid Build Coastguard Worker if (is_last_tile_in_tg) new_tg = 1;
4022*77c1e3ccSAndroid Build Coastguard Worker
4023*77c1e3ccSAndroid Build Coastguard Worker curr_tg_data_size +=
4024*77c1e3ccSAndroid Build Coastguard Worker (pack_bs_params.buf.size + (is_last_tile_in_tg ? 0 : 4));
4025*77c1e3ccSAndroid Build Coastguard Worker
4026*77c1e3ccSAndroid Build Coastguard Worker if (pack_bs_params.buf.size > *max_tile_size) {
4027*77c1e3ccSAndroid Build Coastguard Worker *largest_tile_id = tile_idx;
4028*77c1e3ccSAndroid Build Coastguard Worker *max_tile_size = (unsigned int)pack_bs_params.buf.size;
4029*77c1e3ccSAndroid Build Coastguard Worker }
4030*77c1e3ccSAndroid Build Coastguard Worker
4031*77c1e3ccSAndroid Build Coastguard Worker if (is_last_tile_in_tg)
4032*77c1e3ccSAndroid Build Coastguard Worker av1_write_last_tile_info(cpi, fh_info, saved_wb, &curr_tg_data_size,
4033*77c1e3ccSAndroid Build Coastguard Worker tile_data_curr, total_size, tile_data_start,
4034*77c1e3ccSAndroid Build Coastguard Worker largest_tile_id, &is_first_tg,
4035*77c1e3ccSAndroid Build Coastguard Worker *obu_header_size, obu_extn_header);
4036*77c1e3ccSAndroid Build Coastguard Worker *total_size += (uint32_t)pack_bs_params.buf.size;
4037*77c1e3ccSAndroid Build Coastguard Worker }
4038*77c1e3ccSAndroid Build Coastguard Worker }
4039*77c1e3ccSAndroid Build Coastguard Worker av1_accumulate_pack_bs_thread_data(cpi, &cpi->td);
4040*77c1e3ccSAndroid Build Coastguard Worker }
4041*77c1e3ccSAndroid Build Coastguard Worker
4042*77c1e3ccSAndroid Build Coastguard Worker // Write total buffer size and related information into the OBU header for
4043*77c1e3ccSAndroid Build Coastguard Worker // default tile case.
write_tile_obu_size(AV1_COMP * const cpi,uint8_t * const dst,struct aom_write_bit_buffer * saved_wb,int largest_tile_id,uint32_t * const total_size,unsigned int max_tile_size,uint32_t obu_header_size,uint8_t * tile_data_start)4044*77c1e3ccSAndroid Build Coastguard Worker static void write_tile_obu_size(AV1_COMP *const cpi, uint8_t *const dst,
4045*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *saved_wb,
4046*77c1e3ccSAndroid Build Coastguard Worker int largest_tile_id, uint32_t *const total_size,
4047*77c1e3ccSAndroid Build Coastguard Worker unsigned int max_tile_size,
4048*77c1e3ccSAndroid Build Coastguard Worker uint32_t obu_header_size,
4049*77c1e3ccSAndroid Build Coastguard Worker uint8_t *tile_data_start) {
4050*77c1e3ccSAndroid Build Coastguard Worker const CommonTileParams *const tiles = &cpi->common.tiles;
4051*77c1e3ccSAndroid Build Coastguard Worker
4052*77c1e3ccSAndroid Build Coastguard Worker // Fill in context_update_tile_id indicating the tile to use for the
4053*77c1e3ccSAndroid Build Coastguard Worker // cdf update. The encoder currently sets it to the largest tile
4054*77c1e3ccSAndroid Build Coastguard Worker // (but is up to the encoder)
4055*77c1e3ccSAndroid Build Coastguard Worker aom_wb_overwrite_literal(saved_wb, largest_tile_id,
4056*77c1e3ccSAndroid Build Coastguard Worker (tiles->log2_cols + tiles->log2_rows));
4057*77c1e3ccSAndroid Build Coastguard Worker // If more than one tile group. tile_size_bytes takes the default value 4
4058*77c1e3ccSAndroid Build Coastguard Worker // and does not need to be set. For a single tile group it is set in the
4059*77c1e3ccSAndroid Build Coastguard Worker // section below.
4060*77c1e3ccSAndroid Build Coastguard Worker if (cpi->num_tg != 1) return;
4061*77c1e3ccSAndroid Build Coastguard Worker int tile_size_bytes = 4, unused;
4062*77c1e3ccSAndroid Build Coastguard Worker const uint32_t tile_data_offset = (uint32_t)(tile_data_start - dst);
4063*77c1e3ccSAndroid Build Coastguard Worker const uint32_t tile_data_size = *total_size - tile_data_offset;
4064*77c1e3ccSAndroid Build Coastguard Worker
4065*77c1e3ccSAndroid Build Coastguard Worker *total_size = remux_tiles(tiles, tile_data_start, tile_data_size,
4066*77c1e3ccSAndroid Build Coastguard Worker max_tile_size, 0, &tile_size_bytes, &unused);
4067*77c1e3ccSAndroid Build Coastguard Worker *total_size += tile_data_offset;
4068*77c1e3ccSAndroid Build Coastguard Worker assert(tile_size_bytes >= 1 && tile_size_bytes <= 4);
4069*77c1e3ccSAndroid Build Coastguard Worker
4070*77c1e3ccSAndroid Build Coastguard Worker aom_wb_overwrite_literal(saved_wb, tile_size_bytes - 1, 2);
4071*77c1e3ccSAndroid Build Coastguard Worker
4072*77c1e3ccSAndroid Build Coastguard Worker // Update the OBU length if remux_tiles() reduced the size.
4073*77c1e3ccSAndroid Build Coastguard Worker uint64_t payload_size;
4074*77c1e3ccSAndroid Build Coastguard Worker size_t length_field_size;
4075*77c1e3ccSAndroid Build Coastguard Worker int res =
4076*77c1e3ccSAndroid Build Coastguard Worker aom_uleb_decode(dst + obu_header_size, *total_size - obu_header_size,
4077*77c1e3ccSAndroid Build Coastguard Worker &payload_size, &length_field_size);
4078*77c1e3ccSAndroid Build Coastguard Worker assert(res == 0);
4079*77c1e3ccSAndroid Build Coastguard Worker (void)res;
4080*77c1e3ccSAndroid Build Coastguard Worker
4081*77c1e3ccSAndroid Build Coastguard Worker const uint64_t new_payload_size =
4082*77c1e3ccSAndroid Build Coastguard Worker *total_size - obu_header_size - length_field_size;
4083*77c1e3ccSAndroid Build Coastguard Worker if (new_payload_size != payload_size) {
4084*77c1e3ccSAndroid Build Coastguard Worker size_t new_length_field_size;
4085*77c1e3ccSAndroid Build Coastguard Worker res = aom_uleb_encode(new_payload_size, length_field_size,
4086*77c1e3ccSAndroid Build Coastguard Worker dst + obu_header_size, &new_length_field_size);
4087*77c1e3ccSAndroid Build Coastguard Worker assert(res == 0);
4088*77c1e3ccSAndroid Build Coastguard Worker if (new_length_field_size < length_field_size) {
4089*77c1e3ccSAndroid Build Coastguard Worker const size_t src_offset = obu_header_size + length_field_size;
4090*77c1e3ccSAndroid Build Coastguard Worker const size_t dst_offset = obu_header_size + new_length_field_size;
4091*77c1e3ccSAndroid Build Coastguard Worker memmove(dst + dst_offset, dst + src_offset, (size_t)payload_size);
4092*77c1e3ccSAndroid Build Coastguard Worker *total_size -= (int)(length_field_size - new_length_field_size);
4093*77c1e3ccSAndroid Build Coastguard Worker }
4094*77c1e3ccSAndroid Build Coastguard Worker }
4095*77c1e3ccSAndroid Build Coastguard Worker }
4096*77c1e3ccSAndroid Build Coastguard Worker
4097*77c1e3ccSAndroid Build Coastguard Worker // As per the experiments, single-thread bitstream packing is better for
4098*77c1e3ccSAndroid Build Coastguard Worker // frames with a smaller bitstream size. This behavior is due to setup time
4099*77c1e3ccSAndroid Build Coastguard Worker // overhead of multithread function would be more than that of time required
4100*77c1e3ccSAndroid Build Coastguard Worker // to pack the smaller bitstream of such frames. This function computes the
4101*77c1e3ccSAndroid Build Coastguard Worker // number of required number of workers based on setup time overhead and job
4102*77c1e3ccSAndroid Build Coastguard Worker // dispatch time overhead for given tiles and available workers.
calc_pack_bs_mt_workers(const TileDataEnc * tile_data,int num_tiles,int avail_workers,bool pack_bs_mt_enabled)4103*77c1e3ccSAndroid Build Coastguard Worker static int calc_pack_bs_mt_workers(const TileDataEnc *tile_data, int num_tiles,
4104*77c1e3ccSAndroid Build Coastguard Worker int avail_workers, bool pack_bs_mt_enabled) {
4105*77c1e3ccSAndroid Build Coastguard Worker if (!pack_bs_mt_enabled) return 1;
4106*77c1e3ccSAndroid Build Coastguard Worker
4107*77c1e3ccSAndroid Build Coastguard Worker uint64_t frame_abs_sum_level = 0;
4108*77c1e3ccSAndroid Build Coastguard Worker
4109*77c1e3ccSAndroid Build Coastguard Worker for (int idx = 0; idx < num_tiles; idx++)
4110*77c1e3ccSAndroid Build Coastguard Worker frame_abs_sum_level += tile_data[idx].abs_sum_level;
4111*77c1e3ccSAndroid Build Coastguard Worker
4112*77c1e3ccSAndroid Build Coastguard Worker int ideal_num_workers = 1;
4113*77c1e3ccSAndroid Build Coastguard Worker const float job_disp_time_const = (float)num_tiles * JOB_DISP_TIME_OH_CONST;
4114*77c1e3ccSAndroid Build Coastguard Worker float max_sum = 0.0;
4115*77c1e3ccSAndroid Build Coastguard Worker
4116*77c1e3ccSAndroid Build Coastguard Worker for (int num_workers = avail_workers; num_workers > 1; num_workers--) {
4117*77c1e3ccSAndroid Build Coastguard Worker const float fas_per_worker_const =
4118*77c1e3ccSAndroid Build Coastguard Worker ((float)(num_workers - 1) / num_workers) * frame_abs_sum_level;
4119*77c1e3ccSAndroid Build Coastguard Worker const float setup_time_const = (float)num_workers * SETUP_TIME_OH_CONST;
4120*77c1e3ccSAndroid Build Coastguard Worker const float this_sum = fas_per_worker_const - setup_time_const -
4121*77c1e3ccSAndroid Build Coastguard Worker job_disp_time_const / num_workers;
4122*77c1e3ccSAndroid Build Coastguard Worker
4123*77c1e3ccSAndroid Build Coastguard Worker if (this_sum > max_sum) {
4124*77c1e3ccSAndroid Build Coastguard Worker max_sum = this_sum;
4125*77c1e3ccSAndroid Build Coastguard Worker ideal_num_workers = num_workers;
4126*77c1e3ccSAndroid Build Coastguard Worker }
4127*77c1e3ccSAndroid Build Coastguard Worker }
4128*77c1e3ccSAndroid Build Coastguard Worker return ideal_num_workers;
4129*77c1e3ccSAndroid Build Coastguard Worker }
4130*77c1e3ccSAndroid Build Coastguard Worker
pack_tiles_in_tg_obus(AV1_COMP * const cpi,uint8_t * const dst,struct aom_write_bit_buffer * saved_wb,uint8_t obu_extension_header,const FrameHeaderInfo * fh_info,int * const largest_tile_id)4131*77c1e3ccSAndroid Build Coastguard Worker static inline uint32_t pack_tiles_in_tg_obus(
4132*77c1e3ccSAndroid Build Coastguard Worker AV1_COMP *const cpi, uint8_t *const dst,
4133*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *saved_wb, uint8_t obu_extension_header,
4134*77c1e3ccSAndroid Build Coastguard Worker const FrameHeaderInfo *fh_info, int *const largest_tile_id) {
4135*77c1e3ccSAndroid Build Coastguard Worker const CommonTileParams *const tiles = &cpi->common.tiles;
4136*77c1e3ccSAndroid Build Coastguard Worker uint32_t total_size = 0;
4137*77c1e3ccSAndroid Build Coastguard Worker unsigned int max_tile_size = 0;
4138*77c1e3ccSAndroid Build Coastguard Worker uint32_t obu_header_size = 0;
4139*77c1e3ccSAndroid Build Coastguard Worker uint8_t *tile_data_start = dst;
4140*77c1e3ccSAndroid Build Coastguard Worker const int tile_cols = tiles->cols;
4141*77c1e3ccSAndroid Build Coastguard Worker const int tile_rows = tiles->rows;
4142*77c1e3ccSAndroid Build Coastguard Worker const int num_tiles = tile_rows * tile_cols;
4143*77c1e3ccSAndroid Build Coastguard Worker
4144*77c1e3ccSAndroid Build Coastguard Worker const int num_workers = calc_pack_bs_mt_workers(
4145*77c1e3ccSAndroid Build Coastguard Worker cpi->tile_data, num_tiles, cpi->mt_info.num_mod_workers[MOD_PACK_BS],
4146*77c1e3ccSAndroid Build Coastguard Worker cpi->mt_info.pack_bs_mt_enabled);
4147*77c1e3ccSAndroid Build Coastguard Worker
4148*77c1e3ccSAndroid Build Coastguard Worker if (num_workers > 1) {
4149*77c1e3ccSAndroid Build Coastguard Worker av1_write_tile_obu_mt(cpi, dst, &total_size, saved_wb, obu_extension_header,
4150*77c1e3ccSAndroid Build Coastguard Worker fh_info, largest_tile_id, &max_tile_size,
4151*77c1e3ccSAndroid Build Coastguard Worker &obu_header_size, &tile_data_start, num_workers);
4152*77c1e3ccSAndroid Build Coastguard Worker } else {
4153*77c1e3ccSAndroid Build Coastguard Worker write_tile_obu(cpi, dst, &total_size, saved_wb, obu_extension_header,
4154*77c1e3ccSAndroid Build Coastguard Worker fh_info, largest_tile_id, &max_tile_size, &obu_header_size,
4155*77c1e3ccSAndroid Build Coastguard Worker &tile_data_start);
4156*77c1e3ccSAndroid Build Coastguard Worker }
4157*77c1e3ccSAndroid Build Coastguard Worker
4158*77c1e3ccSAndroid Build Coastguard Worker if (num_tiles > 1)
4159*77c1e3ccSAndroid Build Coastguard Worker write_tile_obu_size(cpi, dst, saved_wb, *largest_tile_id, &total_size,
4160*77c1e3ccSAndroid Build Coastguard Worker max_tile_size, obu_header_size, tile_data_start);
4161*77c1e3ccSAndroid Build Coastguard Worker return total_size;
4162*77c1e3ccSAndroid Build Coastguard Worker }
4163*77c1e3ccSAndroid Build Coastguard Worker
write_tiles_in_tg_obus(AV1_COMP * const cpi,uint8_t * const dst,size_t dst_size,struct aom_write_bit_buffer * saved_wb,uint8_t obu_extension_header,const FrameHeaderInfo * fh_info,int * const largest_tile_id)4164*77c1e3ccSAndroid Build Coastguard Worker static uint32_t write_tiles_in_tg_obus(AV1_COMP *const cpi, uint8_t *const dst,
4165*77c1e3ccSAndroid Build Coastguard Worker size_t dst_size,
4166*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer *saved_wb,
4167*77c1e3ccSAndroid Build Coastguard Worker uint8_t obu_extension_header,
4168*77c1e3ccSAndroid Build Coastguard Worker const FrameHeaderInfo *fh_info,
4169*77c1e3ccSAndroid Build Coastguard Worker int *const largest_tile_id) {
4170*77c1e3ccSAndroid Build Coastguard Worker // TODO: bug 42302568 - Use dst_size.
4171*77c1e3ccSAndroid Build Coastguard Worker (void)dst_size;
4172*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
4173*77c1e3ccSAndroid Build Coastguard Worker const CommonTileParams *const tiles = &cm->tiles;
4174*77c1e3ccSAndroid Build Coastguard Worker *largest_tile_id = 0;
4175*77c1e3ccSAndroid Build Coastguard Worker
4176*77c1e3ccSAndroid Build Coastguard Worker // Select the coding strategy (temporal or spatial)
4177*77c1e3ccSAndroid Build Coastguard Worker if (cm->seg.enabled && cm->seg.update_map) {
4178*77c1e3ccSAndroid Build Coastguard Worker if (cm->features.primary_ref_frame == PRIMARY_REF_NONE) {
4179*77c1e3ccSAndroid Build Coastguard Worker cm->seg.temporal_update = 0;
4180*77c1e3ccSAndroid Build Coastguard Worker } else {
4181*77c1e3ccSAndroid Build Coastguard Worker cm->seg.temporal_update = 1;
4182*77c1e3ccSAndroid Build Coastguard Worker if (cpi->td.rd_counts.seg_tmp_pred_cost[0] <
4183*77c1e3ccSAndroid Build Coastguard Worker cpi->td.rd_counts.seg_tmp_pred_cost[1])
4184*77c1e3ccSAndroid Build Coastguard Worker cm->seg.temporal_update = 0;
4185*77c1e3ccSAndroid Build Coastguard Worker }
4186*77c1e3ccSAndroid Build Coastguard Worker }
4187*77c1e3ccSAndroid Build Coastguard Worker
4188*77c1e3ccSAndroid Build Coastguard Worker if (tiles->large_scale)
4189*77c1e3ccSAndroid Build Coastguard Worker return pack_large_scale_tiles_in_tg_obus(
4190*77c1e3ccSAndroid Build Coastguard Worker cpi, dst, saved_wb, obu_extension_header, largest_tile_id);
4191*77c1e3ccSAndroid Build Coastguard Worker
4192*77c1e3ccSAndroid Build Coastguard Worker return pack_tiles_in_tg_obus(cpi, dst, saved_wb, obu_extension_header,
4193*77c1e3ccSAndroid Build Coastguard Worker fh_info, largest_tile_id);
4194*77c1e3ccSAndroid Build Coastguard Worker }
4195*77c1e3ccSAndroid Build Coastguard Worker
4196*77c1e3ccSAndroid Build Coastguard Worker // Returns the number of bytes written on success. Returns 0 on failure.
av1_write_metadata_obu(const aom_metadata_t * metadata,uint8_t * const dst,size_t dst_size)4197*77c1e3ccSAndroid Build Coastguard Worker static size_t av1_write_metadata_obu(const aom_metadata_t *metadata,
4198*77c1e3ccSAndroid Build Coastguard Worker uint8_t *const dst, size_t dst_size) {
4199*77c1e3ccSAndroid Build Coastguard Worker size_t coded_metadata_size = 0;
4200*77c1e3ccSAndroid Build Coastguard Worker const uint64_t metadata_type = (uint64_t)metadata->type;
4201*77c1e3ccSAndroid Build Coastguard Worker if (aom_uleb_encode(metadata_type, dst_size, dst, &coded_metadata_size) !=
4202*77c1e3ccSAndroid Build Coastguard Worker 0) {
4203*77c1e3ccSAndroid Build Coastguard Worker return 0;
4204*77c1e3ccSAndroid Build Coastguard Worker }
4205*77c1e3ccSAndroid Build Coastguard Worker if (coded_metadata_size + metadata->sz + 1 > dst_size) {
4206*77c1e3ccSAndroid Build Coastguard Worker return 0;
4207*77c1e3ccSAndroid Build Coastguard Worker }
4208*77c1e3ccSAndroid Build Coastguard Worker memcpy(dst + coded_metadata_size, metadata->payload, metadata->sz);
4209*77c1e3ccSAndroid Build Coastguard Worker // Add trailing bits.
4210*77c1e3ccSAndroid Build Coastguard Worker dst[coded_metadata_size + metadata->sz] = 0x80;
4211*77c1e3ccSAndroid Build Coastguard Worker return coded_metadata_size + metadata->sz + 1;
4212*77c1e3ccSAndroid Build Coastguard Worker }
4213*77c1e3ccSAndroid Build Coastguard Worker
av1_write_metadata_array(AV1_COMP * const cpi,uint8_t * dst,size_t dst_size)4214*77c1e3ccSAndroid Build Coastguard Worker static size_t av1_write_metadata_array(AV1_COMP *const cpi, uint8_t *dst,
4215*77c1e3ccSAndroid Build Coastguard Worker size_t dst_size) {
4216*77c1e3ccSAndroid Build Coastguard Worker if (!cpi->source) return 0;
4217*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
4218*77c1e3ccSAndroid Build Coastguard Worker aom_metadata_array_t *arr = cpi->source->metadata;
4219*77c1e3ccSAndroid Build Coastguard Worker if (!arr) return 0;
4220*77c1e3ccSAndroid Build Coastguard Worker size_t obu_header_size = 0;
4221*77c1e3ccSAndroid Build Coastguard Worker size_t obu_payload_size = 0;
4222*77c1e3ccSAndroid Build Coastguard Worker size_t total_bytes_written = 0;
4223*77c1e3ccSAndroid Build Coastguard Worker size_t length_field_size = 0;
4224*77c1e3ccSAndroid Build Coastguard Worker for (size_t i = 0; i < arr->sz; i++) {
4225*77c1e3ccSAndroid Build Coastguard Worker aom_metadata_t *current_metadata = arr->metadata_array[i];
4226*77c1e3ccSAndroid Build Coastguard Worker if (current_metadata && current_metadata->payload) {
4227*77c1e3ccSAndroid Build Coastguard Worker if ((cm->current_frame.frame_type == KEY_FRAME &&
4228*77c1e3ccSAndroid Build Coastguard Worker current_metadata->insert_flag == AOM_MIF_KEY_FRAME) ||
4229*77c1e3ccSAndroid Build Coastguard Worker (cm->current_frame.frame_type != KEY_FRAME &&
4230*77c1e3ccSAndroid Build Coastguard Worker current_metadata->insert_flag == AOM_MIF_NON_KEY_FRAME) ||
4231*77c1e3ccSAndroid Build Coastguard Worker current_metadata->insert_flag == AOM_MIF_ANY_FRAME) {
4232*77c1e3ccSAndroid Build Coastguard Worker // OBU header is either one or two bytes.
4233*77c1e3ccSAndroid Build Coastguard Worker if (dst_size < 2) {
4234*77c1e3ccSAndroid Build Coastguard Worker aom_internal_error(cm->error, AOM_CODEC_ERROR,
4235*77c1e3ccSAndroid Build Coastguard Worker "av1_write_metadata_array: output buffer full");
4236*77c1e3ccSAndroid Build Coastguard Worker }
4237*77c1e3ccSAndroid Build Coastguard Worker obu_header_size = av1_write_obu_header(
4238*77c1e3ccSAndroid Build Coastguard Worker &cpi->ppi->level_params, &cpi->frame_header_count, OBU_METADATA,
4239*77c1e3ccSAndroid Build Coastguard Worker cm->seq_params->has_nonzero_operating_point_idc, 0, dst);
4240*77c1e3ccSAndroid Build Coastguard Worker assert(obu_header_size <= 2);
4241*77c1e3ccSAndroid Build Coastguard Worker obu_payload_size =
4242*77c1e3ccSAndroid Build Coastguard Worker av1_write_metadata_obu(current_metadata, dst + obu_header_size,
4243*77c1e3ccSAndroid Build Coastguard Worker dst_size - obu_header_size);
4244*77c1e3ccSAndroid Build Coastguard Worker if (obu_payload_size == 0) {
4245*77c1e3ccSAndroid Build Coastguard Worker aom_internal_error(cm->error, AOM_CODEC_ERROR,
4246*77c1e3ccSAndroid Build Coastguard Worker "av1_write_metadata_array: output buffer full");
4247*77c1e3ccSAndroid Build Coastguard Worker }
4248*77c1e3ccSAndroid Build Coastguard Worker length_field_size =
4249*77c1e3ccSAndroid Build Coastguard Worker obu_memmove(obu_header_size, obu_payload_size, dst, dst_size);
4250*77c1e3ccSAndroid Build Coastguard Worker if (length_field_size == 0) {
4251*77c1e3ccSAndroid Build Coastguard Worker aom_internal_error(cm->error, AOM_CODEC_ERROR,
4252*77c1e3ccSAndroid Build Coastguard Worker "av1_write_metadata_array: output buffer full");
4253*77c1e3ccSAndroid Build Coastguard Worker }
4254*77c1e3ccSAndroid Build Coastguard Worker if (av1_write_uleb_obu_size(obu_payload_size, dst + obu_header_size,
4255*77c1e3ccSAndroid Build Coastguard Worker length_field_size) == AOM_CODEC_OK) {
4256*77c1e3ccSAndroid Build Coastguard Worker const size_t obu_size =
4257*77c1e3ccSAndroid Build Coastguard Worker obu_header_size + length_field_size + obu_payload_size;
4258*77c1e3ccSAndroid Build Coastguard Worker dst += obu_size;
4259*77c1e3ccSAndroid Build Coastguard Worker dst_size -= obu_size;
4260*77c1e3ccSAndroid Build Coastguard Worker total_bytes_written += obu_size;
4261*77c1e3ccSAndroid Build Coastguard Worker } else {
4262*77c1e3ccSAndroid Build Coastguard Worker aom_internal_error(cpi->common.error, AOM_CODEC_ERROR,
4263*77c1e3ccSAndroid Build Coastguard Worker "av1_write_metadata_array: output buffer full");
4264*77c1e3ccSAndroid Build Coastguard Worker }
4265*77c1e3ccSAndroid Build Coastguard Worker }
4266*77c1e3ccSAndroid Build Coastguard Worker }
4267*77c1e3ccSAndroid Build Coastguard Worker }
4268*77c1e3ccSAndroid Build Coastguard Worker return total_bytes_written;
4269*77c1e3ccSAndroid Build Coastguard Worker }
4270*77c1e3ccSAndroid Build Coastguard Worker
av1_pack_bitstream(AV1_COMP * const cpi,uint8_t * dst,size_t dst_size,size_t * size,int * const largest_tile_id)4271*77c1e3ccSAndroid Build Coastguard Worker int av1_pack_bitstream(AV1_COMP *const cpi, uint8_t *dst, size_t dst_size,
4272*77c1e3ccSAndroid Build Coastguard Worker size_t *size, int *const largest_tile_id) {
4273*77c1e3ccSAndroid Build Coastguard Worker uint8_t *data = dst;
4274*77c1e3ccSAndroid Build Coastguard Worker size_t data_size = dst_size;
4275*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
4276*77c1e3ccSAndroid Build Coastguard Worker AV1LevelParams *const level_params = &cpi->ppi->level_params;
4277*77c1e3ccSAndroid Build Coastguard Worker uint32_t obu_header_size = 0;
4278*77c1e3ccSAndroid Build Coastguard Worker uint32_t obu_payload_size = 0;
4279*77c1e3ccSAndroid Build Coastguard Worker FrameHeaderInfo fh_info = { NULL, 0, 0 };
4280*77c1e3ccSAndroid Build Coastguard Worker const uint8_t obu_extension_header =
4281*77c1e3ccSAndroid Build Coastguard Worker cm->temporal_layer_id << 5 | cm->spatial_layer_id << 3 | 0;
4282*77c1e3ccSAndroid Build Coastguard Worker
4283*77c1e3ccSAndroid Build Coastguard Worker // If no non-zero delta_q has been used, reset delta_q_present_flag
4284*77c1e3ccSAndroid Build Coastguard Worker if (cm->delta_q_info.delta_q_present_flag && cpi->deltaq_used == 0) {
4285*77c1e3ccSAndroid Build Coastguard Worker cm->delta_q_info.delta_q_present_flag = 0;
4286*77c1e3ccSAndroid Build Coastguard Worker }
4287*77c1e3ccSAndroid Build Coastguard Worker
4288*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITSTREAM_DEBUG
4289*77c1e3ccSAndroid Build Coastguard Worker bitstream_queue_reset_write();
4290*77c1e3ccSAndroid Build Coastguard Worker #endif
4291*77c1e3ccSAndroid Build Coastguard Worker
4292*77c1e3ccSAndroid Build Coastguard Worker cpi->frame_header_count = 0;
4293*77c1e3ccSAndroid Build Coastguard Worker
4294*77c1e3ccSAndroid Build Coastguard Worker // The TD is now written outside the frame encode loop
4295*77c1e3ccSAndroid Build Coastguard Worker
4296*77c1e3ccSAndroid Build Coastguard Worker // write sequence header obu at each key frame or intra_only frame,
4297*77c1e3ccSAndroid Build Coastguard Worker // preceded by 4-byte size
4298*77c1e3ccSAndroid Build Coastguard Worker if (cm->current_frame.frame_type == INTRA_ONLY_FRAME ||
4299*77c1e3ccSAndroid Build Coastguard Worker cm->current_frame.frame_type == KEY_FRAME) {
4300*77c1e3ccSAndroid Build Coastguard Worker // OBU header is either one or two bytes.
4301*77c1e3ccSAndroid Build Coastguard Worker if (data_size < 2) {
4302*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_ERROR;
4303*77c1e3ccSAndroid Build Coastguard Worker }
4304*77c1e3ccSAndroid Build Coastguard Worker obu_header_size = av1_write_obu_header(
4305*77c1e3ccSAndroid Build Coastguard Worker level_params, &cpi->frame_header_count, OBU_SEQUENCE_HEADER,
4306*77c1e3ccSAndroid Build Coastguard Worker cm->seq_params->has_nonzero_operating_point_idc, 0, data);
4307*77c1e3ccSAndroid Build Coastguard Worker assert(obu_header_size <= 2);
4308*77c1e3ccSAndroid Build Coastguard Worker obu_payload_size = av1_write_sequence_header_obu(
4309*77c1e3ccSAndroid Build Coastguard Worker cm->seq_params, data + obu_header_size, data_size - obu_header_size);
4310*77c1e3ccSAndroid Build Coastguard Worker const size_t length_field_size =
4311*77c1e3ccSAndroid Build Coastguard Worker obu_memmove(obu_header_size, obu_payload_size, data, data_size);
4312*77c1e3ccSAndroid Build Coastguard Worker if (length_field_size == 0) {
4313*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_ERROR;
4314*77c1e3ccSAndroid Build Coastguard Worker }
4315*77c1e3ccSAndroid Build Coastguard Worker if (av1_write_uleb_obu_size(obu_payload_size, data + obu_header_size,
4316*77c1e3ccSAndroid Build Coastguard Worker length_field_size) != AOM_CODEC_OK) {
4317*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_ERROR;
4318*77c1e3ccSAndroid Build Coastguard Worker }
4319*77c1e3ccSAndroid Build Coastguard Worker
4320*77c1e3ccSAndroid Build Coastguard Worker const size_t bytes_written =
4321*77c1e3ccSAndroid Build Coastguard Worker obu_header_size + length_field_size + obu_payload_size;
4322*77c1e3ccSAndroid Build Coastguard Worker data += bytes_written;
4323*77c1e3ccSAndroid Build Coastguard Worker data_size -= bytes_written;
4324*77c1e3ccSAndroid Build Coastguard Worker }
4325*77c1e3ccSAndroid Build Coastguard Worker
4326*77c1e3ccSAndroid Build Coastguard Worker // write metadata obus before the frame obu that has the show_frame flag set
4327*77c1e3ccSAndroid Build Coastguard Worker if (cm->show_frame) {
4328*77c1e3ccSAndroid Build Coastguard Worker const size_t bytes_written = av1_write_metadata_array(cpi, data, data_size);
4329*77c1e3ccSAndroid Build Coastguard Worker data += bytes_written;
4330*77c1e3ccSAndroid Build Coastguard Worker data_size -= bytes_written;
4331*77c1e3ccSAndroid Build Coastguard Worker }
4332*77c1e3ccSAndroid Build Coastguard Worker
4333*77c1e3ccSAndroid Build Coastguard Worker const int write_frame_header =
4334*77c1e3ccSAndroid Build Coastguard Worker (cpi->num_tg > 1 || encode_show_existing_frame(cm));
4335*77c1e3ccSAndroid Build Coastguard Worker struct aom_write_bit_buffer saved_wb = { NULL, 0 };
4336*77c1e3ccSAndroid Build Coastguard Worker size_t length_field = 0;
4337*77c1e3ccSAndroid Build Coastguard Worker if (write_frame_header) {
4338*77c1e3ccSAndroid Build Coastguard Worker // Write Frame Header OBU.
4339*77c1e3ccSAndroid Build Coastguard Worker fh_info.frame_header = data;
4340*77c1e3ccSAndroid Build Coastguard Worker // OBU header is either one or two bytes.
4341*77c1e3ccSAndroid Build Coastguard Worker if (data_size < 2) {
4342*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_ERROR;
4343*77c1e3ccSAndroid Build Coastguard Worker }
4344*77c1e3ccSAndroid Build Coastguard Worker obu_header_size = av1_write_obu_header(
4345*77c1e3ccSAndroid Build Coastguard Worker level_params, &cpi->frame_header_count, OBU_FRAME_HEADER,
4346*77c1e3ccSAndroid Build Coastguard Worker cm->seq_params->has_nonzero_operating_point_idc, obu_extension_header,
4347*77c1e3ccSAndroid Build Coastguard Worker data);
4348*77c1e3ccSAndroid Build Coastguard Worker // TODO: bug 42302568 - Pass data_size - obu_header_size to
4349*77c1e3ccSAndroid Build Coastguard Worker // write_frame_header_obu().
4350*77c1e3ccSAndroid Build Coastguard Worker obu_payload_size = write_frame_header_obu(cpi, &cpi->td.mb.e_mbd, &saved_wb,
4351*77c1e3ccSAndroid Build Coastguard Worker data + obu_header_size, 1);
4352*77c1e3ccSAndroid Build Coastguard Worker
4353*77c1e3ccSAndroid Build Coastguard Worker length_field =
4354*77c1e3ccSAndroid Build Coastguard Worker obu_memmove(obu_header_size, obu_payload_size, data, data_size);
4355*77c1e3ccSAndroid Build Coastguard Worker if (length_field == 0) {
4356*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_ERROR;
4357*77c1e3ccSAndroid Build Coastguard Worker }
4358*77c1e3ccSAndroid Build Coastguard Worker if (av1_write_uleb_obu_size(obu_payload_size, data + obu_header_size,
4359*77c1e3ccSAndroid Build Coastguard Worker length_field) != AOM_CODEC_OK) {
4360*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_ERROR;
4361*77c1e3ccSAndroid Build Coastguard Worker }
4362*77c1e3ccSAndroid Build Coastguard Worker
4363*77c1e3ccSAndroid Build Coastguard Worker fh_info.obu_header_byte_offset = 0;
4364*77c1e3ccSAndroid Build Coastguard Worker fh_info.total_length = obu_header_size + length_field + obu_payload_size;
4365*77c1e3ccSAndroid Build Coastguard Worker // Make sure it is safe to cast fh_info.total_length to uint32_t.
4366*77c1e3ccSAndroid Build Coastguard Worker if (fh_info.total_length > UINT32_MAX) {
4367*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_ERROR;
4368*77c1e3ccSAndroid Build Coastguard Worker }
4369*77c1e3ccSAndroid Build Coastguard Worker data += fh_info.total_length;
4370*77c1e3ccSAndroid Build Coastguard Worker data_size -= fh_info.total_length;
4371*77c1e3ccSAndroid Build Coastguard Worker }
4372*77c1e3ccSAndroid Build Coastguard Worker
4373*77c1e3ccSAndroid Build Coastguard Worker if (!encode_show_existing_frame(cm)) {
4374*77c1e3ccSAndroid Build Coastguard Worker // Since length_field is determined adaptively after frame header
4375*77c1e3ccSAndroid Build Coastguard Worker // encoding, saved_wb must be adjusted accordingly.
4376*77c1e3ccSAndroid Build Coastguard Worker if (saved_wb.bit_buffer != NULL) {
4377*77c1e3ccSAndroid Build Coastguard Worker saved_wb.bit_buffer += length_field;
4378*77c1e3ccSAndroid Build Coastguard Worker }
4379*77c1e3ccSAndroid Build Coastguard Worker
4380*77c1e3ccSAndroid Build Coastguard Worker // Each tile group obu will be preceded by 4-byte size of the tile group
4381*77c1e3ccSAndroid Build Coastguard Worker // obu
4382*77c1e3ccSAndroid Build Coastguard Worker const size_t bytes_written =
4383*77c1e3ccSAndroid Build Coastguard Worker write_tiles_in_tg_obus(cpi, data, data_size, &saved_wb,
4384*77c1e3ccSAndroid Build Coastguard Worker obu_extension_header, &fh_info, largest_tile_id);
4385*77c1e3ccSAndroid Build Coastguard Worker data += bytes_written;
4386*77c1e3ccSAndroid Build Coastguard Worker data_size -= bytes_written;
4387*77c1e3ccSAndroid Build Coastguard Worker }
4388*77c1e3ccSAndroid Build Coastguard Worker *size = data - dst;
4389*77c1e3ccSAndroid Build Coastguard Worker (void)data_size;
4390*77c1e3ccSAndroid Build Coastguard Worker return AOM_CODEC_OK;
4391*77c1e3ccSAndroid Build Coastguard Worker }
4392