xref: /rockchip-linux_mpp/mpp/codec/dec/avs2/avs2d_global.h (revision 437bfbeb9567cca9cd9080e3f6954aa9d6a94f18)
1 /*
2  * Copyright 2021 Rockchip Electronics Co. LTD
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __AVS2D_GLOBAL_H__
18 #define __AVS2D_GLOBAL_H__
19 
20 #include "mpp_list.h"
21 #include "parser_api.h"
22 #include "mpp_bitread.h"
23 
24 #include "avs2d_syntax.h"
25 #include "avs2d_api.h"
26 
27 #define AVS2D_DBG_ERROR             (0x00000001)
28 #define AVS2D_DBG_ASSERT            (0x00000002)
29 #define AVS2D_DBG_WARNNING          (0x00000004)
30 #define AVS2D_DBG_LOG               (0x00000008)
31 
32 #define AVS2D_DBG_INPUT             (0x00000010)   //!< input packet
33 #define AVS2D_DBG_TIME              (0x00000020)   //!< input packet
34 #define AVS2D_DBG_DPB               (0x00000040)
35 
36 #define AVS2D_DBG_CALLBACK          (0x00008000)
37 
38 extern RK_U32 avs2d_parse_debug;
39 
40 #define AVS2D_PARSE_TRACE(fmt, ...)\
41 do {\
42     if (AVS2D_DBG_LOG & avs2d_parse_debug)\
43         { mpp_log_f(fmt, ## __VA_ARGS__); }\
44 } while (0)
45 
46 #define avs2d_dbg_dpb(fmt, ...)\
47 do {\
48     if (AVS2D_DBG_DPB & avs2d_parse_debug)\
49         { mpp_log_f(fmt, ## __VA_ARGS__); }\
50 } while (0)
51 
52 
53 #define AVS2D_DBG(level, fmt, ...)\
54 do {\
55     if (level & avs2d_parse_debug)\
56         { mpp_log(fmt, ## __VA_ARGS__); }\
57 } while (0)
58 
59 //!< input check
60 #define INP_CHECK(ret, val, ...)\
61 do {\
62     if ((val)) {\
63         ret = MPP_ERR_INIT; \
64         AVS2D_DBG(AVS2D_DBG_WARNNING, "input empty(%d).\n", __LINE__); \
65         goto __RETURN; \
66 }} while (0)
67 
68 //!< memory malloc check
69 #define MEM_CHECK(ret, val, ...)\
70 do {\
71     if(!(val)) {\
72         ret = MPP_ERR_MALLOC;\
73         mpp_err_f("malloc buffer error(%d).\n", __LINE__); \
74         goto __FAILED; \
75 }} while (0)
76 
77 //!< function return check
78 #define FUN_CHECK(val)\
79 do {\
80     if ((val) < 0) {\
81         AVS2D_DBG(AVS2D_DBG_WARNNING, "Function error(%d).\n", __LINE__); \
82         goto __FAILED; \
83 }} while (0)
84 
85 //!< read & check marker bit
86 #define READ_MARKER_BIT(bitctx)\
87 do {\
88     RK_U8 bval;\
89     READ_ONEBIT(bitctx, &bval);\
90     if ((bval) != 1) {\
91         AVS2D_DBG(AVS2D_DBG_WARNNING, "expected marker_bit 1 while received 0(%d).\n", __LINE__); \
92 }} while (0)
93 
94 #define MAX_NALU_NUM                        (20)
95 #define MAX_HEADER_SIZE                     (2*1024)
96 #define MAX_STREAM_SIZE                     (2*1024*1024)
97 
98 #define AVS2_START_CODE                     (0x00000100)
99 
100 //!< NALU type
101 #define AVS2_SLICE_MIN_START_CODE           (AVS2_START_CODE | 0x00)
102 #define AVS2_SLICE_MAX_START_CODE           (AVS2_START_CODE | 0x8F)
103 #define AVS2_VIDEO_SEQUENCE_START_CODE      (AVS2_START_CODE | 0xb0)
104 #define AVS2_VIDEO_SEQUENCE_END_CODE        0x000001B1
105 #define AVS2_USER_DATA_START_CODE           0x000001B2
106 #define AVS2_I_PICTURE_START_CODE           0x000001B3
107 #define AVS2_EXTENSION_START_CODE           0x000001B5
108 #define AVS2_PB_PICTURE_START_CODE          0x000001B6
109 #define AVS2_VIDEO_EDIT_CODE                0x000001B7
110 
111 //!< SEQ/PIC EXTENSION 4bit
112 #define AVS2_SEQUENCE_DISPLAY_EXT_ID                        (0x2)
113 #define AVS2_TEMPORAL_SCALABLE_EXT_ID                       (0x3)
114 #define AVS2_COPYRIGHT_EXT_ID                               (0x4)
115 #define AVS2_HDR_DYNAMIC_METADATA_EXT_ID                    (0x5)
116 #define AVS2_PICTURE_DISPLAY_EXT_ID                         (0x7)
117 #define AVS2_MASTERING_DISPLAY_AND_CONTENT_METADATA_EXT_ID  (0xA)
118 #define AVS2_CAMERA_PARAMETERS_EXT_ID                       (0xB)
119 
120 //!< profile
121 #define MAIN_PICTURE_PROFILE            0x12
122 #define MAIN_PROFILE                    0x20
123 #define MAIN10_PROFILE                  0x22
124 
125 //!< max reference frame number
126 #define AVS2_MAX_REFS                   7
127 //!< max rps number
128 #define AVS2_MAX_RPS_NUM                32
129 #define AVS2_MAX_BUF_NUM                19
130 #define AVS2_MAX_DPB_SIZE               15
131 #define AVS2_DOI_CYCLE                  256
132 #define AVS2_MAX_POC_DISTANCE           128
133 
134 #define ALF_MAX_FILTERS                 16
135 #define ALF_MAX_COEFS                   9
136 
137 #define UNDETAILED                      0
138 #define DETAILED                        1
139 #define WQP_SIZE                        6
140 #define NO_VAL                         -1
141 
142 
143 //!< chroma format
144 enum avs2d_chroma_format_e {
145     CHROMA_400  = 0,
146     CHROMA_420  = 1,
147     CHROMA_422  = 2,
148     CHROMA_444  = 3
149 };
150 
151 //!< picture type
152 typedef enum avs2d_picture_type_e {
153     I_PICTURE   = 0,
154     P_PICTURE   = 1,
155     B_PICTURE   = 2,
156     F_PICTURE   = 3,
157     G_PICTURE   = 4,
158     GB_PICTURE  = 5,
159     S_PICTURE   = 6,
160 } Avs2dPicType;
161 
162 #define PICTURE_TYPE_TO_CHAR(x) ((x) == 0 ? 'I' : ((x) == 1 ? 'P' : ((x) == 2 ? 'B' : ((x) == 3 ? 'F' : ((x) == 4 ? 'G' : ((x) == 5 ? 'g' : ((x) == 6 ? 'S' : 'E')))))))
163 
164 typedef struct avs2d_nalu_t {
165     RK_U32      header;
166     RK_U32      length;
167     RK_U32      data_pos;
168     RK_U8       eof; //!< end of frame stream
169 } Avs2dNalu_t;
170 
171 //!< reference picture set (RPS)
172 typedef struct avs2d_rps_t {
173     RK_U8       ref_pic[AVS2_MAX_REFS]; //!< delta DOI of ref pic
174     RK_U8       remove_pic[8];          //!< delta DOI of removed pic
175     RK_U8       num_of_ref;             //!< number of reference picture
176     RK_U8       num_to_remove;          //!< number of removed picture
177     RK_U8       refered_by_others;      //!< referenced by others
178     RK_U32      reserved;               //!< reserved 4 bytes
179 } Avs2dRps_t;
180 
181 //!< sequence set information
182 typedef struct avs2d_sequence_header_t {
183     RK_U8       profile_id;
184     RK_U8       level_id;
185     RK_U8       progressive_sequence;
186     RK_U8       field_coded_sequence;
187     RK_U32      horizontal_size;
188     RK_U32      vertical_size;
189 
190     RK_U8       chroma_format;
191     RK_U8       sample_precision;
192     RK_U8       encoding_precision;
193     RK_U8       bit_depth;
194     RK_U8       aspect_ratio;
195     RK_U8       frame_rate_code;
196     RK_U32      bit_rate;
197     RK_U8       low_delay;
198     RK_U8       enable_temporal_id;
199     RK_U32      bbv_buffer_size;
200     RK_U8       lcu_size;
201 
202     RK_U8       enable_weighted_quant;    //!< weight quant enable?
203     RK_U8       enable_background_picture;//!< background picture enabled?
204     RK_U8       enable_mhp_skip;          //!< mhpskip enabled?
205     RK_U8       enable_dhp;               //!< dhp enabled?
206     RK_U8       enable_wsm;               //!< wsm enabled?
207     RK_U8       enable_amp;               //!< AMP(asymmetric motion partitions) enabled?
208     RK_U8       enable_nsqt;              //!< use NSQT?
209     RK_U8       enable_nsip;              //!< use NSIP?
210     RK_U8       enable_2nd_transform;     //!< secondary transform enabled?
211     RK_U8       enable_sao;               //!< SAO enabled?
212     RK_U8       enable_alf;               //!< ALF enabled?
213     RK_U8       enable_pmvr;              //!< PMVR enabled?
214     RK_U8       enable_clf;               //!< cross loop filter flag
215     RK_U8       picture_reorder_delay;    //!< picture reorder delay
216     RK_U8       num_of_rps;               //!< rps set number
217 
218     Avs2dRps_t  seq_rps[AVS2_MAX_RPS_NUM];//!< RPS at sequence level
219     RK_U32      seq_wq_matrix[2][64];     //!< sequence base weighting quantization matrix
220 
221 } Avs2dSeqHeader_t;
222 
223 //!< sequence display extension header
224 typedef struct avs2d_sequence_extension_header_t {
225     RK_U8       video_format;
226     RK_U8       sample_range;
227     RK_U8       color_description;
228     RK_U32      color_primaries;
229     RK_U32      transfer_characteristics;
230     RK_U32      matrix_coefficients;
231     RK_U32      display_horizontal_size;
232     RK_U32      display_vertical_size;
233     RK_U8       td_mode_flag;
234     RK_U32      td_packing_mode;
235     RK_U8       view_reverse_flag;
236 } Avs2dSeqExtHeader_t;
237 
238 //!< intra/inter picture header
239 typedef struct avs2d_picture_header {
240     Avs2dPicType picture_type;
241     RK_U8       picture_coding_type;
242     RK_U32      bbv_delay;
243     RK_U8       time_code_flag;
244     RK_U32      time_code;
245     RK_U8       background_picture_flag;
246     RK_U8       background_picture_output_flag;
247     RK_U8       background_pred_flag;
248     RK_U8       background_reference_flag;
249 
250     RK_S32      doi;
251     RK_S32      poi;
252     RK_U8       temporal_id;
253     RK_U8       picture_output_delay;
254     RK_U32      bbv_check_times;
255     RK_U8       progressive_frame;
256     RK_U8       picture_structure;
257     RK_U8       top_field_first;
258     RK_U8       repeat_first_field;
259     RK_U8       is_top_field;
260     RK_U8       fixed_picture_qp;
261     RK_U8       picture_qp;
262 
263     RK_U8       enable_random_decodable;
264     RK_U8       enable_loop_filter;
265     RK_U8       loop_filter_parameter_flag;
266     RK_S8       alpha_c_offset;
267     RK_S8       beta_offset;
268     RK_U8       enable_chroma_quant_param;
269     RK_S8       chroma_quant_param_delta_cb;
270     RK_S8       chroma_quant_param_delta_cr;
271     RK_U8       enable_pic_weight_quant;
272     RK_U8       pic_wq_data_index;
273     RK_U8       wq_param_index;
274     RK_U8       wq_model;
275     RK_S32      wq_param_delta1[WQP_SIZE];
276     RK_S32      wq_param_delta2[WQP_SIZE];
277 
278     RK_U8       enable_pic_alf_y;
279     RK_U8       enable_pic_alf_cb;
280     RK_U8       enable_pic_alf_cr;
281     RK_U8       alf_filter_num;
282     RK_U8       alf_filter_pattern[ALF_MAX_FILTERS];
283     RK_U8       alf_coeff_idx_tab[ALF_MAX_FILTERS];
284     RK_S32      alf_coeff_y[ALF_MAX_FILTERS][ALF_MAX_COEFS];
285     RK_S32      alf_coeff_cb[ALF_MAX_COEFS];
286     RK_S32      alf_coeff_cr[ALF_MAX_COEFS];
287 
288     RK_U32      pic_wq_matrix[2][64];     //!< picture weighting quantization matrix
289     RK_U32      pic_wq_param[WQP_SIZE];
290 } Avs2dPicHeader_t;
291 
292 typedef struct avs2d_frame_t {
293     MppFrame    frame;
294     RK_U32      frame_mode;         //!< mode used for display
295     RK_U32      frame_coding_mode;  //!< mode used for decoding, Field/Frame
296     Avs2dPicType picture_type;       //!< I/G/GB/P/F/B/S
297     RK_S32      slot_idx;
298     RK_S32      doi;
299     RK_S32      poi;
300     RK_S32      out_delay;
301     RK_U8       invisible;          //!< GB visible = 0
302     RK_U8       scene_frame_flag;   //!< G/GB
303     RK_U8       intra_frame_flag;   //!< I/G/GB
304     RK_U8       refered_bg_frame;   //!< ref G/GB
305     RK_U8       refered_by_others;
306     RK_U8       refered_by_scene;
307     RK_U8       error_flag;
308     RK_U8       is_output;      // mark for picture has been output to display queue
309 } Avs2dFrame_t;
310 
311 typedef enum avs2d_nal_type {
312 
313     NAL_SEQ_START   = 0xb0,
314     NAL_SEQ_END     = 0xb1,
315     NAL_USER_DATA   = 0xb2,
316     NAL_INTRA_PIC   = 0xb3,
317     NAL_EXTENSION   = 0xb5,
318     NAL_INTER_PIC   = 0xb6,
319     NAL_VIDEO_EDIT  = 0xb7
320 } Avs2dNalType;
321 
322 typedef struct avs2d_nal_t {
323     RK_U8 *data;
324     RK_U32 size;
325     RK_U32 nal_type;
326     RK_U32 start_found;
327     RK_U32 end_found;
328 } Avs2dNal;
329 
330 typedef struct avs2d_stream_buf_t {
331     RK_U8      *pbuf;
332     RK_U32      size;   //Total buffer size
333     RK_U32      len;    // Used length
334 } Avs2dStreamBuf_t;
335 
336 typedef struct avs2d_memory_t {
337     Avs2dStreamBuf_t        nals_buf;
338     Avs2dStreamBuf_t        headerbuf;
339     Avs2dStreamBuf_t        streambuf;
340 } Avs2dMemory_t;
341 
342 typedef struct avs2d_frame_mgr_t {
343     RK_U32                  dpb_size;
344     RK_U32                  dpb_specific_size;
345     RK_U32                  used_size;
346     Avs2dFrame_t          **dpb;
347     RK_U8                   num_of_ref;
348     Avs2dFrame_t           *refs[AVS2_MAX_REFS];
349     Avs2dFrame_t           *scene_ref;
350     Avs2dFrame_t           *cur_frm;
351     Avs2dRps_t              cur_rps;
352 
353     RK_S32                  prev_doi;
354     RK_S32                  output_poi;
355     RK_S32                  tr_wrap_cnt;
356     RK_U8                   initial_flag;
357 } Avs2dFrameMgr_t;
358 
359 #define END_NOT_FOUND (-100)
360 #define START_NOT_FOUND (-101)
361 #define MPP_INPUT_BUFFER_PADDING_SIZE 8
362 #define AVS2D_PACKET_SPLIT_LAST_KEPT_LENGTH     (3)
363 #define AVS2D_PACKET_SPLIT_CHECKER_BUFFER_SIZE  (7)
364 #define AVS2D_START_CODE_SIZE                   (4)
365 
366 //!< decoder parameters
367 typedef struct avs2_dec_ctx_t {
368     MppBufSlots             frame_slots;
369     MppBufSlots             packet_slots;
370 
371     MppPacket               task_pkt;
372     Avs2dMemory_t          *mem; //!< resotre slice data to decoder
373     Avs2dStreamBuf_t       *p_stream;
374     Avs2dStreamBuf_t       *p_header;
375 
376     //-------- input ---------------
377     ParserCfg               init;
378     RK_U64                  frame_no;
379     RK_U64                  pkt_no;
380     RK_U8                   has_get_eos;
381 
382     //-------- current --------------
383     Avs2dNalu_t             *p_nals;
384     RK_U32                  nal_cnt;
385     RK_U32                  nal_allocated;
386 
387     //--------  video  --------------
388     BitReadCtx_t            bitctx;
389     Avs2dSeqHeader_t        vsh;
390     Avs2dSeqExtHeader_t     exh;
391     Avs2dPicHeader_t        ph;
392     MppFrameMasteringDisplayMetadata    display_meta;
393     MppFrameContentLightMetadata    content_light;
394     MppFrameHdrDynamicMeta    *hdr_dynamic_meta;
395 
396     Avs2dSyntax_t           syntax;
397     Avs2dFrameMgr_t         frm_mgr;
398     RK_U32                  cur_wq_matrix[2][64];
399 
400     //!<------------------------------------
401     RK_U32                  got_vsh;
402     RK_U32                  got_exh;
403     RK_U32                  got_keyframe;
404     RK_U8                   enable_wq;//!< seq&pic weight quant enable
405     RK_U32                  prev_start_code;
406     RK_U32                  new_seq_flag;
407     RK_U8                   prev_tail_data[AVS2D_PACKET_SPLIT_CHECKER_BUFFER_SIZE]; // store the last 3 bytes at the lowest addr
408     RK_U32                  prev_state;
409     RK_U32                  new_frame_flag;
410     RK_U32                  is_hdr;
411     RK_U32                  hdr_dynamic;
412 } Avs2dCtx_t;
413 
414 #endif /*__AVS2D_GLOBAL_H__*/
415