1 /*
2 * Copyright(c) 2019 Intel Corporation
3 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
4 *
5 * This source code is subject to the terms of the BSD 2 Clause License and
6 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
7 * was not distributed with this source code in the LICENSE file, you can
8 * obtain it at https://www.aomedia.org/license/software-license. If the Alliance for Open
9 * Media Patent License 1.0 was not distributed with this source code in the
10 * PATENTS file, you can obtain it at https://www.aomedia.org/license/patent-license.
11 */
12 
13 #ifndef EbEntropyCoding_h
14 #define EbEntropyCoding_h
15 
16 #include "EbDefinitions.h"
17 #include "EbEntropyCodingObject.h"
18 #include "EbCodingUnit.h"
19 #include "EbPredictionUnit.h"
20 #include "EbPictureBufferDesc.h"
21 #include "EbSequenceControlSet.h"
22 #include "EbPictureControlSet.h"
23 #include "EbCabacContextModel.h"
24 #include "EbModeDecision.h"
25 #include "EbEncIntraPrediction.h"
26 #include "EbBitstreamUnit.h"
27 #include "EbPacketizationProcess.h"
28 #include "EbModeDecisionProcess.h"
29 #include "EbInterPrediction.h"
30 #include "EbSvtAv1Metadata.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 struct ModeDecisionCandidateBuffer;
37 struct ModeDecisionCandidate;
38 
39 /**************************************
40      * Extern Function Declarations
41      **************************************/
42 struct EntropyCodingContext;
43 extern EbErrorType write_sb(struct EntropyCodingContext *context_ptr, SuperBlock *tb_ptr,
44                             PictureControlSet *pcs_ptr, uint16_t tile_idx,
45                             EntropyCoder *entropy_coder_ptr, EbPictureBufferDesc *coeff_ptr);
46 
47 extern int get_wedge_params_bits(BlockSize sb_type);
48 
49 extern EbErrorType encode_slice_finish(EntropyCoder *entropy_coder_ptr);
50 
51 extern EbErrorType reset_entropy_coder(EncodeContext *encode_context_ptr,
52                                        EntropyCoder *entropy_coder_ptr, uint32_t qp,
53                                        EB_SLICE slice_type);
54 EbErrorType        av1_txb_estimate_coeff_bits(
55            struct ModeDecisionContext *md_context, uint8_t allow_update_cdf, FRAME_CONTEXT *ec_ctx,
56            PictureControlSet *pcs_ptr, struct ModeDecisionCandidateBuffer *candidate_buffer_ptr,
57            uint32_t txb_origin_index, uint32_t txb_chroma_origin_index,
58            EbPictureBufferDesc *coeff_buffer_sb, uint32_t y_eob, uint32_t cb_eob, uint32_t cr_eob,
59            uint64_t *y_txb_coeff_bits, uint64_t *cb_txb_coeff_bits, uint64_t *cr_txb_coeff_bits,
60            TxSize txsize, TxSize txsize_uv, TxType tx_type, TxType tx_type_uv,
61            COMPONENT_TYPE component_type);
62 
63 //**********************************************************************************************************//
64 //onyxc_int.h
frame_is_intra_only(const PictureParentControlSet * const pcs_ptr)65 static INLINE int32_t frame_is_intra_only(const PictureParentControlSet *const pcs_ptr) {
66     return pcs_ptr->frm_hdr.frame_type == KEY_FRAME ||
67         pcs_ptr->frm_hdr.frame_type == INTRA_ONLY_FRAME;
68 }
69 
frame_is_sframe(const PictureParentControlSet * pcs_ptr)70 static INLINE int32_t frame_is_sframe(const PictureParentControlSet *pcs_ptr) {
71     return pcs_ptr->frm_hdr.frame_type == S_FRAME;
72 }
73 
74 // Returns 1 if this frame might allow mvs from some reference frame.
75 
frame_might_allow_ref_frame_mvs(const PictureParentControlSet * pcs_ptr,SequenceControlSet * scs_ptr)76 static INLINE int32_t frame_might_allow_ref_frame_mvs(const PictureParentControlSet *pcs_ptr,
77                                                       SequenceControlSet *           scs_ptr) {
78     return !pcs_ptr->frm_hdr.error_resilient_mode &&
79         scs_ptr->seq_header.order_hint_info.enable_ref_frame_mvs &&
80         scs_ptr->seq_header.order_hint_info.enable_order_hint && !frame_is_intra_only(pcs_ptr);
81 }
82 
83 // Returns 1 if this frame might use warped_motion
frame_might_allow_warped_motion(const PictureParentControlSet * pcs_ptr,SequenceControlSet * scs_ptr)84 static INLINE int32_t frame_might_allow_warped_motion(const PictureParentControlSet *pcs_ptr,
85                                                       SequenceControlSet *           scs_ptr) {
86     return !pcs_ptr->frm_hdr.error_resilient_mode && !frame_is_intra_only(pcs_ptr) &&
87         scs_ptr->static_config.enable_warped_motion;
88 }
89 
major_minor_to_seq_level_idx(BitstreamLevel bl)90 static INLINE uint8_t major_minor_to_seq_level_idx(BitstreamLevel bl) {
91     assert(bl.major >= LEVEL_MAJOR_MIN && bl.major <= LEVEL_MAJOR_MAX);
92     return ((bl.major - LEVEL_MAJOR_MIN) << LEVEL_MINOR_BITS) + bl.minor;
93 }
94 
95 //**********************************************************************************************************//
96 //encoder.h
get_ref_frame_map_idx(const PictureParentControlSet * pcs_ptr,MvReferenceFrame ref_frame)97 static INLINE int32_t get_ref_frame_map_idx(const PictureParentControlSet *pcs_ptr,
98                                             MvReferenceFrame               ref_frame) {
99     return pcs_ptr->av1_ref_signal
100         .ref_dpb_index[ref_frame - LAST_FRAME]; //LAST-LAST2-LAST3-GOLDEN-BWD-ALT2-ALT
101 }
102 
103 //*******************************************************************************************//
104 // bitwriter_buffer.h
105 struct AomWriteBitBuffer {
106     uint8_t *bit_buffer;
107     uint32_t bit_offset;
108 };
109 
110 int32_t  svt_aom_wb_is_byte_aligned(const struct AomWriteBitBuffer *wb);
111 uint32_t svt_aom_wb_bytes_written(const struct AomWriteBitBuffer *wb);
112 
113 void svt_aom_wb_write_bit(struct AomWriteBitBuffer *wb, int32_t bit);
114 void svt_aom_wb_write_literal(struct AomWriteBitBuffer *wb, int32_t data, int32_t bits);
115 
116 void svt_aom_wb_write_inv_signed_literal(struct AomWriteBitBuffer *wb, int32_t data, int32_t bits);
117 //*******************************************************************************************//
118 // Bitstream.h
119 struct AomWriteBitBuffer;
120 
121 void write_sequence_header(SequenceControlSet *      scs_ptr /*Av1Comp *cpi*/,
122                            struct AomWriteBitBuffer *wb);
123 
124 uint32_t write_obu_header(ObuType ObuType, int32_t obuExtension, uint8_t *const dst);
125 
126 int32_t write_uleb_obu_size(uint32_t obu_header_size, uint32_t obu_payload_size, uint8_t *dest);
127 
128 //*******************************************************************************************//
129 // blockd.h
130 
131 void get_txb_ctx(PictureControlSet *pcs_ptr, const int32_t plane,
132                  NeighborArrayUnit *dc_sign_level_coeff_neighbor_array, uint32_t blk_origin_x,
133                  uint32_t blk_origin_y, const BlockSize plane_bsize, const TxSize tx_size,
134                  int16_t *const txb_skip_ctx, int16_t *const dc_sign_ctx);
135 
136 extern void av1_collect_neighbors_ref_counts_new(MacroBlockD *const xd);
137 // Obtain contexts to signal a reference frame be either BWDREF/ALTREF2, or
138 // ALTREF.
139 //extern int32_t get_pred_context_brfarf2_or_arf(const MacroBlockD *xd);
140 // Obtain contexts to signal a reference frame be either BWDREF or ALTREF2.
141 //extern int32_t get_pred_context_brf_or_arf2(const MacroBlockD *xd);
142 // == Context functions for comp ref ==
143 //
144 // Returns a context number for the given MB prediction signal
145 // Signal the first reference frame for a compound mode be either
146 // GOLDEN/LAST3, or LAST/LAST2.
147 extern int32_t svt_av1_get_pred_context_comp_ref_p(const MacroBlockD *xd);
148 
149 // Returns a context number for the given MB prediction signal
150 // Signal the first reference frame for a compound mode be LAST,
151 // conditioning on that it is known either LAST/LAST2.
152 extern int32_t svt_av1_get_pred_context_comp_ref_p1(const MacroBlockD *xd);
153 
154 // Returns a context number for the given MB prediction signal
155 // Signal the first reference frame for a compound mode be GOLDEN,
156 // conditioning on that it is known either GOLDEN or LAST3.
157 extern int32_t svt_av1_get_pred_context_comp_ref_p2(const MacroBlockD *xd);
158 
159 // Signal the 2nd reference frame for a compound mode be either
160 // ALTREF, or ALTREF2/BWDREF.
161 extern int32_t svt_av1_get_pred_context_comp_bwdref_p(const MacroBlockD *xd);
162 
163 // Signal the 2nd reference frame for a compound mode be either
164 // ALTREF2 or BWDREF.
165 extern int32_t svt_av1_get_pred_context_comp_bwdref_p1(const MacroBlockD *xd);
166 // == Context functions for single ref ==
167 //
168 // For the bit to signal whether the single reference is a forward reference
169 // frame or a backward reference frame.
170 extern int32_t svt_av1_get_pred_context_single_ref_p1(const MacroBlockD *xd);
171 
172 // For the bit to signal whether the single reference is ALTREF_FRAME or
173 // non-ALTREF backward reference frame, knowing that it shall be either of
174 // these 2 choices.
175 extern int32_t svt_av1_get_pred_context_single_ref_p2(const MacroBlockD *xd);
176 
177 // For the bit to signal whether the single reference is LAST3/GOLDEN or
178 // LAST2/LAST, knowing that it shall be either of these 2 choices.
179 extern int32_t svt_av1_get_pred_context_single_ref_p3(const MacroBlockD *xd);
180 
181 // For the bit to signal whether the single reference is LAST2_FRAME or
182 // LAST_FRAME, knowing that it shall be either of these 2 choices.
183 extern int32_t svt_av1_get_pred_context_single_ref_p4(const MacroBlockD *xd);
184 
185 // For the bit to signal whether the single reference is GOLDEN_FRAME or
186 // LAST3_FRAME, knowing that it shall be either of these 2 choices.
187 extern int32_t svt_av1_get_pred_context_single_ref_p5(const MacroBlockD *xd);
188 
189 // For the bit to signal whether the single reference is ALTREF2_FRAME or
190 // BWDREF_FRAME, knowing that it shall be either of these 2 choices.
191 extern int32_t svt_av1_get_pred_context_single_ref_p6(const MacroBlockD *xd);
192 
193 /*!\brief Writes a valid metadata object to the AV1 bitstream.
194  * \param[in]    bitstream_ptr       AV1 bitstream
195  * \param[in]    metadata            Metadata array object
196  * \param[in]    type                Metadata type descriptor
197  */
198 extern EbErrorType write_metadata_av1(Bitstream *bitstream_ptr, SvtMetadataArrayT *metadata,
199                                       const EbAv1MetadataType type);
200 extern EbErrorType write_frame_header_av1(Bitstream *bitstream_ptr, SequenceControlSet *scs_ptr,
201                                           PictureControlSet *pcs_ptr, uint8_t show_existing);
202 extern EbErrorType encode_td_av1(uint8_t *bitstream_ptr);
203 extern EbErrorType encode_sps_av1(Bitstream *bitstream_ptr, SequenceControlSet *scs_ptr);
204 
205 //*******************************************************************************************//
206 
207 MotionMode motion_mode_allowed(const PictureControlSet *pcs_ptr, const BlkStruct *blk_ptr,
208                                const BlockSize bsize, MvReferenceFrame rf0, MvReferenceFrame rf1,
209                                PredictionMode mode);
210 
211 int is_masked_compound_type(COMPOUND_TYPE type);
212 
213 int32_t svt_aom_count_primitive_subexpfin(uint16_t n, uint16_t k, uint16_t v);
214 int32_t svt_aom_count_primitive_refsubexpfin(uint16_t n, uint16_t k, uint16_t ref, uint16_t v);
215 
216 #ifdef __cplusplus
217 }
218 #endif
219 #endif //EbEntropyCoding_h
220