xref: /rockchip-linux_mpp/mpp/codec/dec/m2v/m2vd_parser.h (revision 437bfbeb9567cca9cd9080e3f6954aa9d6a94f18)
1 /*
2  * Copyright 2015 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 /*
18  * @file        m2vd_parser.h
19  * @brief
20  * @author      lks(lks@rock-chips.com)
21 
22  * @version     1.0.0
23  * @history
24  *   2015.7.15 : Create
25  */
26 
27 #ifndef __M2VD_PARSER_H__
28 #define __M2VD_PARSER_H__
29 
30 #include "mpp_mem.h"
31 #include "mpp_bitread.h"
32 
33 #include "parser_api.h"
34 #include "m2vd_syntax.h"
35 #include "m2vd_com.h"
36 
37 #define M2VD_SKIP_ERROR_FRAME_EN  1
38 #define M2VD_DEC_OK                   0
39 #define M2VD_IFNO_CHG                 0x10
40 #define M2VD_DEC_UNSURPORT            -1
41 #define M2VD_DEC_MEMORY_FAIL      -2
42 #define M2VD_DEC_FILE_END         -3
43 #define M2VD_HW_DEC_UNKNOW_ERROR     -4
44 #define M2VD_DEC_PICHEAD_OK       1
45 #define M2VD_DEC_STREAM_END       2
46 
47 #define TEMP_DBG_FM     0
48 
49 
50 #define PICTURE_START_CODE      0x100
51 #define SLICE_START_CODE_MIN    0x101
52 #define SLICE_START_CODE_MAX    0x1AF
53 #define USER_DATA_START_CODE    0x1B2
54 #define SEQUENCE_HEADER_CODE    0x1B3
55 #define SEQUENCE_ERROR_CODE     0x1B4
56 #define EXTENSION_START_CODE    0x1B5
57 #define SEQUENCE_END_CODE       0x1B7
58 #define GROUP_START_CODE        0x1B8
59 #define SYSTEM_START_CODE_MIN   0x1B9
60 #define SYSTEM_START_CODE_MAX   0x1FF
61 
62 #define ISO_END_CODE            0x1B9
63 #define PACK_START_CODE         0x1BA
64 #define SYSTEM_START_CODE       0x1BB
65 
66 #define VIDEO_ELEMENTARY_STREAM 0x1e0
67 #define NO_MORE_STREAM      0xffffffff
68 
69 #define SEQUENCE_EXTENSION_ID                    1
70 #define SEQUENCE_DISPLAY_EXTENSION_ID            2
71 #define QUANT_MATRIX_EXTENSION_ID                3
72 #define COPYRIGHT_EXTENSION_ID                   4
73 #define SEQUENCE_SCALABLE_EXTENSION_ID           5
74 #define PICTURE_DISPLAY_EXTENSION_ID             7
75 #define PICTURE_CODING_EXTENSION_ID              8
76 #define PICTURE_SPATIAL_SCALABLE_EXTENSION_ID    9
77 #define PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID  10
78 
79 
80 typedef enum M2VDPicCodingType_e {
81     M2VD_CODING_TYPE_I = 1,
82     M2VD_CODING_TYPE_P = 2,
83     M2VD_CODING_TYPE_B = 3,
84     M2VD_CODING_TYPE_D = 4
85 } M2VDPicCodingType;
86 
87 typedef enum M2VDPicStruct_e {
88     M2VD_PIC_STRUCT_TOP_FIELD    = 1,
89     M2VD_PIC_STRUCT_BOTTOM_FIELD = 2,
90     M2VD_PIC_STRUCT_FRAME        = 3
91 } M2VDPicStruct;
92 
93 #define     M2VD_DBG_FILE_NUM               1
94 #define     M2VD_DBG_FILE_W                 1
95 #define     M2VD_BUF_SIZE_BITMEM            (512 * 1024)
96 #define     M2VD_BUF_SIZE_QPTAB            (256)
97 #define     M2V_OUT_FLAG                   0x1;
98 
99 typedef enum M2VDBufGrpIdx_t {
100     M2VD_BUF_GRP_BITMEM,
101     M2VD_BUF_GRP_QPTAB,
102     M2VD_BUF_GRP_BUTT,
103 } M2VDBufGrpIdx;
104 
105 typedef struct M2VFrameHead_t {
106     RK_U32          frameNumber;
107     RK_U32          tr;
108     RK_U32          picCodingType;
109     RK_U32          totalMbInFrame;
110     RK_U32          frameWidth; /* in macro blocks */
111     RK_U32          frameHeight;    /* in macro blocks */
112     RK_U32          mb_width;
113     RK_U32          mb_height;
114     RK_U32          vlcSet;
115     RK_U32          qp;
116     RK_U32          frame_codelen;
117     // VPU_FRAME       *frame_space;
118     MppFrame        f;
119     RK_U32          flags;
120     RK_S32          slot_index;
121     //RK_U32          error_info;
122 } M2VDFrameHead;
123 
124 
125 /* ISO/IEC 13818-2 section 6.2.2.1:  sequence_header() */
126 typedef struct M2VDHeadSeq_t {
127     RK_U32          decode_width; //horizontal_size_value
128     RK_U32          decode_height; //vertical_size_value
129     RK_S32          aspect_ratio_information;
130     RK_S32          frame_rate_code;
131     RK_S32          bit_rate_value;
132     RK_S32          vbv_buffer_size;
133     RK_S32          constrained_parameters_flag;
134     RK_U32          load_intra_quantizer_matrix; //[TEMP]
135     RK_U32          load_non_intra_quantizer_matrix; //[TEMP]
136     RK_U8           *pIntra_table; //intra_quantiser_matrix[64]
137     RK_U8           *pInter_table; //non_intra_quantiser_matrix[64]
138 } M2VDHeadSeq;
139 
140 /* ISO/IEC 13818-2 section 6.2.2.3:  sequence_extension() */
141 typedef struct M2VDHeadSeqExt_t {
142     RK_U32             horizontal_size_extension; //[TEMP]
143     RK_U32             vertical_size_extension; //[TEMP]
144     RK_U32             bit_rate_extension; //[TEMP]
145     RK_U32             vbv_buffer_size_extension; //[TEMP]
146     RK_S32             profile_and_level_indication;
147     RK_S32             progressive_sequence;
148     RK_S32             chroma_format;
149     RK_S32             low_delay;
150     RK_S32             frame_rate_extension_n;
151     RK_S32             frame_rate_extension_d;
152 } M2VDHeadSeqExt;
153 
154 /* ISO/IEC 13818-2 section 6.2.2.6: group_of_pictures_header()  */
155 typedef struct M2VDHeadGop_t {
156     RK_S32             drop_flag;
157     RK_S32             hour;
158     RK_S32             minute;
159     RK_S32             sec;
160     RK_S32             frame;
161     RK_S32             closed_gop;
162     RK_S32             broken_link;
163 } M2VDHeadGop;
164 
165 
166 /* ISO/IEC 13818-2 section 6.2.3: picture_header() */
167 typedef struct M2VDHeadPic_t {
168     RK_S32             temporal_reference;
169     RK_S32             picture_coding_type;
170     RK_S32             pre_picture_coding_type;
171     RK_S32             vbv_delay;
172     RK_S32             full_pel_forward_vector;
173     RK_S32             forward_f_code;
174     RK_S32             full_pel_backward_vector;
175     RK_S32             backward_f_code;
176     RK_S32             pre_temporal_reference;
177 } M2VDHeadPic;
178 
179 
180 /* ISO/IEC 13818-2 section 6.2.2.4:  sequence_display_extension() */
181 typedef struct M2VDHeadSeqDispExt_t {
182     RK_S32             video_format;
183     RK_S32             color_description;
184     RK_S32             color_primaries;
185     RK_S32             transfer_characteristics;
186     RK_S32             matrix_coefficients;
187 } M2VDHeadSeqDispExt;
188 
189 /* ISO/IEC 13818-2 section 6.2.3.1: picture_coding_extension() header */
190 typedef struct M2VDHeadPicCodeExt_t {
191     RK_S32             f_code[2][2];
192     RK_S32             intra_dc_precision;
193     RK_S32             picture_structure;
194     RK_S32             top_field_first;
195     RK_S32             frame_pred_frame_dct;
196     RK_S32             concealment_motion_vectors;
197     RK_S32             q_scale_type;
198     RK_S32             intra_vlc_format;
199     RK_S32             alternate_scan;
200     RK_S32             repeat_first_field;
201     RK_S32             chroma_420_type;
202     RK_S32             progressive_frame;
203     RK_S32             composite_display_flag;
204     RK_S32             v_axis;
205     RK_S32             field_sequence;
206     RK_S32             sub_carrier;
207     RK_S32             burst_amplitude;
208     RK_S32             sub_carrier_phase;
209 } M2VDHeadPicCodeExt;
210 
211 
212 /* ISO/IEC 13818-2 section 6.2.3.3: picture_display_extension() header */
213 typedef struct M2VDHeadPicDispExt_t {
214     RK_S32             frame_center_horizontal_offset[3];
215     RK_S32             frame_center_vertical_offset[3];
216 } M2VDHeadPicDispExt;
217 
218 typedef struct M2VDCombMem_t {
219     MppBuffer hw_buf;
220     RK_U8*    sw_buf;
221     RK_U8     buf_size;
222     RK_U8*    sw_pos;
223     RK_U32    length;
224 } M2VDCombMem;
225 
226 typedef struct M2VDParserContext_t {
227     MppDecCfgSet    *cfg;
228     M2VDDxvaParam   *dxva_ctx;
229     BitReadCtx_t    *bitread_ctx;
230     RK_U8           *bitstream_sw_buf;
231     RK_U8           *qp_tab_sw_buf;
232     RK_U32          max_stream_size;
233     RK_U32          left_length;
234     RK_U32          state;
235     RK_U32          vop_header_found;
236 
237     RK_U32          frame_size;
238 
239     RK_U32          display_width;
240     RK_U32          display_height;
241     RK_U32          frame_width;
242     RK_U32          frame_height;
243     RK_U32          mb_width;
244     RK_U32          mb_height;
245     RK_U32          MPEG2_Flag;
246 
247     RK_U32          info_changed;
248     M2VDHeadSeq         seq_head;
249     M2VDHeadSeqExt      seq_ext_head;
250     M2VDHeadGop         gop_head;
251     M2VDHeadPic         pic_head;
252     M2VDHeadSeqDispExt  seq_disp_ext_head;
253     M2VDHeadPicCodeExt  pic_code_ext_head;
254     M2VDHeadPicDispExt  pic_disp_ext_head;
255 
256     RK_S32             resetFlag;
257 
258     RK_U64          pre_pts_27M;
259     RK_S64          group_start_time_27M;
260     RK_U32          group_frm_cnt;
261     RK_U32          pretemporal_reference;
262     RK_U32          pretime_temporal;
263     RK_U32          max_temporal_reference;
264     RK_U32          prechange_temporal_ref;
265     RK_S32          frame_period;
266     RK_S32          preframe_period;
267     RK_U32          maxFrame_inGOP;
268     RK_U32          pts_is_90K;
269     RK_U32          ref_frame_cnt;
270     long long       top_first_cnt;
271     long long       bottom_first_cnt;
272     RK_S32          mHeaderDecFlag;
273     RK_S32          mExtraHeaderDecFlag;
274     M2VDFrameHead   Framehead[3];
275     M2VDFrameHead   *frame_ref0;
276     M2VDFrameHead   *frame_ref1;
277     M2VDFrameHead   *frame_cur;
278 #if M2VD_SKIP_ERROR_FRAME_EN
279     RK_S32           mHwDecStatus;
280 #endif
281     RK_S32           flush_dpb_eos;
282 
283     MppPacket       input_packet;
284     RK_U32          eos;
285 
286     RK_S32          initFlag;
287     RK_S32          decoder_err;
288 
289     MppBufSlots     packet_slots;
290     MppBufSlots     frame_slots;
291     RK_U32          cur_slot_index;
292     MppDecCfgSet    *dec_cfg;
293 
294     RK_U64 pts;
295     RK_U64 dts;
296 
297     FILE *fp_dbg_file[M2VD_DBG_FILE_NUM];
298     FILE *fp_dbg_yuv;
299 } M2VDParserContext;
300 
301 MPP_RET  m2vd_parser_init   (void *ctx, ParserCfg *cfg);
302 MPP_RET  m2vd_parser_deinit (void *ctx);
303 MPP_RET  m2vd_parser_reset  (void *ctx);
304 MPP_RET  m2vd_parser_flush  (void *ctx);
305 MPP_RET  m2vd_parser_control(void *ctx, MpiCmd cmd_type, void *param);
306 MPP_RET  m2vd_parser_prepare(void *ctx, MppPacket pkt, HalDecTask *task);
307 MPP_RET  m2vd_parser_parse  (void *ctx, HalDecTask *task);
308 MPP_RET  m2vd_parser_callback(void *ctx, void *err_info);
309 
310 #endif
311 
312