Line data Source code
1 : /*
2 : * Copyright(c) 2019 Intel Corporation
3 : * SPDX - License - Identifier: BSD - 2 - Clause - Patent
4 : */
5 :
6 : /*
7 : * Copyright (c) 2016, Alliance for Open Media. All rights reserved
8 : *
9 : * This source code is subject to the terms of the BSD 2 Clause License and
10 : * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
11 : * was not distributed with this source code in the LICENSE file, you can
12 : * obtain it at www.aomedia.org/license/software. If the Alliance for Open
13 : * Media Patent License 1.0 was not distributed with this source code in the
14 : * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
15 : */
16 :
17 : #ifndef EbEntropyCoding_h
18 : #define EbEntropyCoding_h
19 :
20 : #include "EbDefinitions.h"
21 : #include "EbEntropyCodingObject.h"
22 : #include "EbEntropyCodingUtil.h"
23 : #include "EbEntropyCodingProcess.h"
24 :
25 : #include "EbCodingUnit.h"
26 : #include "EbPredictionUnit.h"
27 : #include "EbPictureBufferDesc.h"
28 : #include "EbSequenceControlSet.h"
29 : #include "EbPictureControlSet.h"
30 : #include "EbCabacContextModel.h"
31 : #include "EbModeDecision.h"
32 : #include "EbIntraPrediction.h"
33 : #include "EbBitstreamUnit.h"
34 : #include "EbPacketizationProcess.h"
35 : #include "EbModeDecisionProcess.h"
36 : #ifdef __cplusplus
37 : extern "C" {
38 : #endif
39 :
40 : #define MAX_TILE_WIDTH (4096) // Max Tile width in pixels
41 : #define MAX_TILE_AREA (4096 * 2304) // Maximum tile area in pixels
42 :
43 : #define CHECK_BACKWARD_REFS(ref_frame) \
44 : (((ref_frame) >= BWDREF_FRAME) && ((ref_frame) <= ALTREF_FRAME))
45 : #define IS_BACKWARD_REF_FRAME(ref_frame) CHECK_BACKWARD_REFS(ref_frame)
46 :
47 : /**************************************
48 : * Extern Function Declarations
49 : **************************************/
50 : struct EntropyCodingContext;
51 : extern EbErrorType write_sb(
52 : struct EntropyCodingContext *context_ptr,
53 : LargestCodingUnit *tb_ptr,
54 : PictureControlSet *picture_control_set_ptr,
55 : EntropyCoder *entropy_coder_ptr,
56 : EbPictureBufferDesc *coeff_ptr);
57 :
58 : extern EbErrorType encode_slice_finish(
59 : EntropyCoder *entropy_coder_ptr);
60 :
61 : extern EbErrorType reset_bitstream(
62 : EbPtr bitstream_ptr);
63 :
64 : extern EbErrorType reset_entropy_coder(
65 : EncodeContext *encode_context_ptr,
66 : EntropyCoder *entropy_coder_ptr,
67 : uint32_t qp,
68 : EB_SLICE slice_type);
69 :
70 : extern EbErrorType av1_tu_estimate_coeff_bits(
71 : struct ModeDecisionContext *md_context,
72 : uint8_t allow_update_cdf,
73 : FRAME_CONTEXT *ec_ctx,
74 : PictureControlSet *picture_control_set_ptr,
75 : struct ModeDecisionCandidateBuffer *candidate_buffer_ptr,
76 : uint32_t tu_origin_index,
77 : uint32_t tu_chroma_origin_index,
78 : EntropyCoder *entropy_coder_ptr,
79 : EbPictureBufferDesc *coeff_buffer_sb,
80 : uint32_t y_eob,
81 : uint32_t cb_eob,
82 : uint32_t cr_eob,
83 : uint64_t *y_tu_coeff_bits,
84 : uint64_t *cb_tu_coeff_bits,
85 : uint64_t *cr_tu_coeff_bits,
86 : TxSize txsize,
87 : TxSize txsize_uv,
88 : TxType tx_type,
89 : TxType tx_type_uv,
90 : COMPONENT_TYPE component_type);
91 :
92 : extern EbErrorType copy_rbsp_bitstream_to_payload(
93 : Bitstream *bitstream_ptr,
94 : EbByte output_buffer,
95 : uint32_t *output_buffer_index,
96 : uint32_t *output_buffer_size,
97 : EncodeContext *encode_context_ptr);
98 :
99 : //**********************************************************************************************************//
100 : //onyxc_int.h
101 900 : static INLINE int32_t frame_is_intra_only(const PictureParentControlSet *const pcs_ptr) {
102 900 : return pcs_ptr->frm_hdr.frame_type == KEY_FRAME || pcs_ptr->frm_hdr.frame_type == INTRA_ONLY_FRAME;
103 : }
104 :
105 0 : static INLINE int32_t frame_is_sframe(const PictureParentControlSet *pcs_ptr) {
106 0 : return pcs_ptr->frm_hdr.frame_type == S_FRAME;
107 : }
108 :
109 : // Returns 1 if this frame might allow mvs from some reference frame.
110 :
111 116 : static INLINE int32_t frame_might_allow_ref_frame_mvs(const PictureParentControlSet *pcs_ptr,
112 : SequenceControlSet *scs_ptr) {
113 232 : return !pcs_ptr->frm_hdr.error_resilient_mode &&
114 116 : scs_ptr->seq_header.order_hint_info.enable_ref_frame_mvs &&
115 232 : scs_ptr->seq_header.order_hint_info.enable_order_hint && !frame_is_intra_only(pcs_ptr);
116 : }
117 :
118 : // Returns 1 if this frame might use warped_motion
119 120 : static INLINE int32_t frame_might_allow_warped_motion(const PictureParentControlSet *pcs_ptr,
120 : SequenceControlSet *scs_ptr) {
121 236 : return !pcs_ptr->frm_hdr.error_resilient_mode && !frame_is_intra_only(pcs_ptr) &&
122 116 : scs_ptr->static_config.enable_warped_motion;
123 : }
124 :
125 2 : static INLINE uint8_t major_minor_to_seq_level_idx(BitstreamLevel bl) {
126 2 : assert(bl.major >= LEVEL_MAJOR_MIN && bl.major <= LEVEL_MAJOR_MAX);
127 2 : return ((bl.major - LEVEL_MAJOR_MIN) << LEVEL_MINOR_BITS) + bl.minor;
128 : }
129 :
130 : //**********************************************************************************************************//
131 : //encoder.h
132 1624 : static INLINE int32_t get_ref_frame_map_idx(const PictureParentControlSet *pcs_ptr,
133 : MvReferenceFrame ref_frame) {
134 1624 : return pcs_ptr->av1_ref_signal.ref_dpb_index[ref_frame - LAST_FRAME];//LAST-LAST2-LAST3-GOLDEN-BWD-ALT2-ALT
135 : }
136 :
137 99715 : static INLINE int is_intrabc_block(const BlockModeInfo *block_mi) {
138 99715 : return block_mi->use_intrabc;
139 : }
140 :
141 6698 : static INLINE int bsize_to_max_depth(BlockSize bsize) {
142 6698 : TxSize tx_size = max_txsize_rect_lookup[bsize];
143 6698 : int depth = 0;
144 16750 : while (depth < MAX_TX_DEPTH && tx_size != TX_4X4) {
145 10052 : depth++;
146 10052 : tx_size = sub_tx_size_map[tx_size];
147 : }
148 6698 : return depth;
149 : }
150 :
151 29376 : static AomCdfProb cdf_element_prob(const AomCdfProb *const cdf, size_t element) {
152 29376 : assert(cdf != NULL);
153 29376 : return (element > 0 ? cdf[element - 1] : CDF_PROB_TOP) - cdf[element];
154 : }
155 :
156 6698 : static INLINE int bsize_to_tx_size_cat(BlockSize bsize) {
157 6698 : TxSize tx_size = max_txsize_rect_lookup[bsize];
158 6698 : assert(tx_size != TX_4X4);
159 6698 : int depth = 0;
160 17368 : while (tx_size != TX_4X4) {
161 10670 : depth++;
162 10670 : tx_size = sub_tx_size_map[tx_size];
163 10670 : assert(depth < 10);
164 : }
165 6698 : assert(depth <= MAX_TX_CATS);
166 6698 : return depth - 1;
167 : }
168 :
169 0 : static INLINE void partition_gather_horz_alike(AomCdfProb *out,
170 : const AomCdfProb *const in, BlockSize bsize)
171 : {
172 0 : out[0] = CDF_PROB_TOP;
173 0 : out[0] -= cdf_element_prob(in, PARTITION_HORZ);
174 0 : out[0] -= cdf_element_prob(in, PARTITION_SPLIT);
175 0 : out[0] -= cdf_element_prob(in, PARTITION_HORZ_A);
176 0 : out[0] -= cdf_element_prob(in, PARTITION_HORZ_B);
177 0 : out[0] -= cdf_element_prob(in, PARTITION_VERT_A);
178 0 : if (bsize != BLOCK_128X128) out[0] -= cdf_element_prob(in, PARTITION_HORZ_4);
179 0 : out[0] = AOM_ICDF(out[0]);
180 0 : out[1] = AOM_ICDF(CDF_PROB_TOP);
181 0 : out[2] = 0;
182 0 : }
183 :
184 4896 : static INLINE void partition_gather_vert_alike(AomCdfProb *out,
185 : const AomCdfProb *const in, BlockSize bsize)
186 : {
187 4896 : out[0] = CDF_PROB_TOP;
188 4896 : out[0] -= cdf_element_prob(in, PARTITION_VERT);
189 4896 : out[0] -= cdf_element_prob(in, PARTITION_SPLIT);
190 4896 : out[0] -= cdf_element_prob(in, PARTITION_HORZ_A);
191 4896 : out[0] -= cdf_element_prob(in, PARTITION_VERT_A);
192 4896 : out[0] -= cdf_element_prob(in, PARTITION_VERT_B);
193 4896 : if (bsize != BLOCK_128X128) out[0] -= cdf_element_prob(in, PARTITION_VERT_4);
194 4896 : out[0] = AOM_ICDF(out[0]);
195 4896 : out[1] = AOM_ICDF(CDF_PROB_TOP);
196 4896 : out[2] = 0;
197 4896 : }
198 :
199 0 : static INLINE int get_palette_bsize_ctx(BlockSize bsize) {
200 0 : return num_pels_log2_lookup[bsize] - num_pels_log2_lookup[BLOCK_8X8];
201 : }
202 :
203 : //*******************************************************************************************//
204 : // bitwriter_buffer.h
205 : struct AomWriteBitBuffer
206 : {
207 : uint8_t *bit_buffer;
208 : uint32_t bit_offset;
209 : };
210 :
211 : int32_t eb_aom_wb_is_byte_aligned(const struct AomWriteBitBuffer *wb);
212 : uint32_t eb_aom_wb_bytes_written(const struct AomWriteBitBuffer *wb);
213 :
214 : void eb_aom_wb_write_bit(struct AomWriteBitBuffer *wb, int32_t bit);
215 :
216 : void eb_aom_wb_overwrite_bit(struct AomWriteBitBuffer *wb, int32_t bit);
217 :
218 : void eb_aom_wb_write_literal(struct AomWriteBitBuffer *wb, int32_t data, int32_t bits);
219 :
220 : void eb_aom_wb_write_inv_signed_literal(struct AomWriteBitBuffer *wb, int32_t data,
221 : int32_t bits);
222 : //*******************************************************************************************//
223 : // bitstream.h
224 : struct AomWriteBitBuffer;
225 :
226 : void write_sequence_header(SequenceControlSet *scs_ptr/*Av1Comp *cpi*/, struct AomWriteBitBuffer *wb);
227 :
228 : uint32_t WriteObuHeader(obuType obuType, int32_t obuExtension,
229 : uint8_t *const dst);
230 :
231 : int32_t WriteUlebObuSize(uint32_t obuHeaderSize, uint32_t obuPayloadSize,
232 : uint8_t *dest);
233 :
234 : //*******************************************************************************************//
235 : // blockd.h
236 151647406 : static INLINE uint32_t have_nearmv_in_inter_mode(PredictionMode mode) {
237 151647406 : return (mode == NEARMV || mode == NEAR_NEARMV || mode == NEAR_NEWMV ||
238 : mode == NEW_NEARMV);
239 : }
240 :
241 : void get_txb_ctx(
242 : SequenceControlSet *sequence_control_set_ptr,
243 : const int32_t plane,
244 : NeighborArrayUnit *dc_sign_level_coeff_neighbor_array,
245 : uint32_t cu_origin_x,
246 : uint32_t cu_origin_y,
247 : const BlockSize plane_bsize,
248 : const TxSize tx_size,
249 : int16_t *const txb_skip_ctx,
250 : int16_t *const dc_sign_ctx);
251 :
252 : extern int32_t eb_av1_get_reference_mode_context(
253 : uint32_t cu_origin_x,
254 : uint32_t cu_origin_y,
255 : NeighborArrayUnit *mode_type_neighbor_array,
256 : NeighborArrayUnit *inter_pred_dir_neighbor_array);
257 :
258 : extern int32_t eb_av1_get_comp_reference_type_context(
259 : uint32_t cu_origin_x,
260 : uint32_t cu_origin_y,
261 : NeighborArrayUnit *mode_type_neighbor_array,
262 : NeighborArrayUnit *inter_pred_dir_neighbor_array);
263 :
264 : extern void av1_collect_neighbors_ref_counts(
265 : CodingUnit *cu_ptr,
266 : uint32_t cu_origin_x,
267 : uint32_t cu_origin_y,
268 : NeighborArrayUnit *mode_type_neighbor_array,
269 : NeighborArrayUnit *inter_pred_dir_neighbor_array,
270 : NeighborArrayUnit *ref_frame_type_neighbor_array);
271 : extern void av1_collect_neighbors_ref_counts_new(MacroBlockD *const xd);
272 : // Obtain contexts to signal a reference frame be either BWDREF/ALTREF2, or
273 : // ALTREF.
274 : //extern int32_t get_pred_context_brfarf2_or_arf(const MacroBlockD *xd);
275 : // Obtain contexts to signal a reference frame be either BWDREF or ALTREF2.
276 : //extern int32_t get_pred_context_brf_or_arf2(const MacroBlockD *xd);
277 : // == Context functions for comp ref ==
278 : //
279 : // Returns a context number for the given MB prediction signal
280 : // Signal the first reference frame for a compound mode be either
281 : // GOLDEN/LAST3, or LAST/LAST2.
282 : extern int32_t eb_av1_get_pred_context_comp_ref_p(const MacroBlockD *xd);
283 :
284 : // Returns a context number for the given MB prediction signal
285 : // Signal the first reference frame for a compound mode be LAST,
286 : // conditioning on that it is known either LAST/LAST2.
287 : extern int32_t eb_av1_get_pred_context_comp_ref_p1(const MacroBlockD *xd);
288 :
289 : // Returns a context number for the given MB prediction signal
290 : // Signal the first reference frame for a compound mode be GOLDEN,
291 : // conditioning on that it is known either GOLDEN or LAST3.
292 : extern int32_t eb_av1_get_pred_context_comp_ref_p2(const MacroBlockD *xd);
293 :
294 : // Signal the 2nd reference frame for a compound mode be either
295 : // ALTREF, or ALTREF2/BWDREF.
296 : extern int32_t eb_av1_get_pred_context_comp_bwdref_p(const MacroBlockD *xd);
297 :
298 : // Signal the 2nd reference frame for a compound mode be either
299 : // ALTREF2 or BWDREF.
300 : extern int32_t eb_av1_get_pred_context_comp_bwdref_p1(const MacroBlockD *xd);
301 : // == Context functions for single ref ==
302 : //
303 : // For the bit to signal whether the single reference is a forward reference
304 : // frame or a backward reference frame.
305 : extern int32_t eb_av1_get_pred_context_single_ref_p1(const MacroBlockD *xd);
306 :
307 : // For the bit to signal whether the single reference is ALTREF_FRAME or
308 : // non-ALTREF backward reference frame, knowing that it shall be either of
309 : // these 2 choices.
310 : extern int32_t eb_av1_get_pred_context_single_ref_p2(const MacroBlockD *xd);
311 :
312 : // For the bit to signal whether the single reference is LAST3/GOLDEN or
313 : // LAST2/LAST, knowing that it shall be either of these 2 choices.
314 : extern int32_t eb_av1_get_pred_context_single_ref_p3(const MacroBlockD *xd);
315 :
316 : // For the bit to signal whether the single reference is LAST2_FRAME or
317 : // LAST_FRAME, knowing that it shall be either of these 2 choices.
318 : extern int32_t eb_av1_get_pred_context_single_ref_p4(const MacroBlockD *xd);
319 :
320 : // For the bit to signal whether the single reference is GOLDEN_FRAME or
321 : // LAST3_FRAME, knowing that it shall be either of these 2 choices.
322 : extern int32_t eb_av1_get_pred_context_single_ref_p5(const MacroBlockD *xd);
323 :
324 : // For the bit to signal whether the single reference is ALTREF2_FRAME or
325 : // BWDREF_FRAME, knowing that it shall be either of these 2 choices.
326 : extern int32_t eb_av1_get_pred_context_single_ref_p6(const MacroBlockD *xd);
327 :
328 : extern EbErrorType write_frame_header_av1(
329 : Bitstream *bitstream_ptr,
330 : SequenceControlSet *scs_ptr,
331 : PictureControlSet *pcs_ptr,
332 : uint8_t showExisting);
333 : extern EbErrorType encode_td_av1(
334 : uint8_t *bitstream_ptr);
335 : extern EbErrorType encode_sps_av1(
336 : Bitstream *bitstream_ptr,
337 : SequenceControlSet *scs_ptr);
338 :
339 : //*******************************************************************************************//
340 :
341 : MotionMode motion_mode_allowed(
342 : const PictureControlSet *picture_control_set_ptr,
343 : const CodingUnit *cu_ptr,
344 : const BlockSize bsize,
345 : MvReferenceFrame rf0,
346 : MvReferenceFrame rf1,
347 : PredictionMode mode);
348 :
349 : int is_masked_compound_type(COMPOUND_TYPE type);
350 :
351 204425725 : static INLINE int32_t is_comp_ref_allowed(BlockSize bsize) {
352 204425725 : return AOMMIN(block_size_wide[bsize], block_size_high[bsize]) >= 8;
353 : }
354 :
355 132860838 : static INLINE int is_interinter_compound_used(CompoundType type, BlockSize sb_type) {
356 132860838 : const int comp_allowed = is_comp_ref_allowed(sb_type);
357 132878638 : switch (type) {
358 6923536 : case COMPOUND_AVERAGE:
359 : case COMPOUND_DISTWTD:
360 6923536 : case COMPOUND_DIFFWTD: return comp_allowed;
361 126109592 : case COMPOUND_WEDGE:
362 126109592 : return comp_allowed && wedge_params_lookup[sb_type].bits > 0;
363 0 : default: assert(0); return 0;
364 : }
365 : }
366 :
367 71870194 : static INLINE int is_any_masked_compound_used(BlockSize sb_type) {
368 : CompoundType comp_type;
369 : int i;
370 71870194 : if (!is_comp_ref_allowed(sb_type)) return 0;
371 222276628 : for (i = 0; i < COMPOUND_TYPES; i++) {
372 221979628 : comp_type = (CompoundType)i;
373 300642668 : if (is_masked_compound_type(comp_type) &&
374 78524940 : is_interinter_compound_used(comp_type, sb_type))
375 71776694 : return 1;
376 : }
377 297071 : return 0;
378 : }
379 :
380 1268 : static INLINE int32_t tile_log2(int32_t blk_size, int32_t target) {
381 : int32_t k;
382 3382 : for (k = 0; (blk_size << k) < target; k++) {
383 : }
384 1268 : return k;
385 : }
386 : void eb_av1_tile_set_col(TileInfo *tile, const TilesInfo *tiles_info,
387 : int32_t mi_cols, int col);
388 : void eb_av1_tile_set_row(TileInfo *tile, TilesInfo *tiles_info,
389 : int32_t mi_rows, int row);
390 :
391 : int32_t eb_aom_count_primitive_subexpfin(uint16_t n, uint16_t k, uint16_t v);
392 : int32_t eb_aom_count_primitive_refsubexpfin(uint16_t n, uint16_t k, uint16_t ref, uint16_t v);
393 :
394 : #ifdef __cplusplus
395 : }
396 : #endif
397 : #endif //EbEntropyCoding_h
|