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