tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

bitstream.c (171177B)


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