Coverage Report

Created: 2022-08-24 06:17

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