xref: /rockchip-linux_mpp/mpp/codec/dec/mpg4/mpg4d_parser.c (revision 437bfbeb9567cca9cd9080e3f6954aa9d6a94f18)
1 /*
2  *
3  * Copyright 2010 Rockchip Electronics Co. LTD
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #define MODULE_TAG "mpg4d_parser"
19 
20 #include <string.h>
21 
22 #include "mpp_env.h"
23 #include "mpp_mem.h"
24 #include "mpp_debug.h"
25 #include "mpp_bitread.h"
26 
27 #include "mpg4d_parser.h"
28 #include "mpg4d_syntax.h"
29 
30 RK_U32 mpg4d_debug = 0;
31 
32 #define mpg4d_dbg(flag, fmt, ...)   _mpp_dbg(mpg4d_debug, flag, fmt, ## __VA_ARGS__)
33 #define mpg4d_dbg_f(flag, fmt, ...) _mpp_dbg_f(mpg4d_debug, flag, fmt, ## __VA_ARGS__)
34 
35 #define mpg4d_dbg_func(fmt, ...)    mpg4d_dbg_f(MPG4D_DBG_FUNCTION, fmt, ## __VA_ARGS__)
36 #define mpg4d_dbg_bit(fmt, ...)     mpg4d_dbg(MPG4D_DBG_BITS, fmt, ## __VA_ARGS__)
37 #define mpg4d_dbg_result(fmt, ...)  mpg4d_dbg(MPG4D_DBG_RESULT, fmt, ## __VA_ARGS__)
38 
39 #define MPEG4_VIDOBJ_START_CODE             0x00000100  /* ..0x0000011f */
40 #define MPEG4_VIDOBJLAY_START_CODE          0x00000120  /* ..0x0000012f */
41 #define MPEG4_VISOBJSEQ_START_CODE          0x000001b0
42 #define MPEG4_VISOBJSEQ_STOP_CODE           0x000001b1
43 #define MPEG4_USERDATA_START_CODE           0x000001b2
44 #define MPEG4_GRPOFVOP_START_CODE           0x000001b3
45 #define MPEG4_VISOBJ_START_CODE             0x000001b5
46 #define MPEG4_VOP_START_CODE                0x000001b6
47 
48 #define MPG4_VOL_STARTCODE                  0x120
49 #define MPG4_VOL_STOPCODE                   0x12F
50 #define MPG4_VOS_STARTCODE                  0x1B0
51 #define MPG4_VOS_STOPCODE                   0x1B1
52 #define MPG4_USER_DATA_STARTCODE            0x1B2
53 #define MPG4_GOP_STARTCODE                  0x1B3
54 #define MPG4_VISUAL_OBJ_STARTCODE           0x1B5
55 #define MPG4_VOP_STARTCODE                  0x1B6
56 
57 typedef struct {
58     RK_S32 method;
59 
60     RK_S32 opaque;
61     RK_S32 transparent;
62     RK_S32 intra_cae;
63     RK_S32 inter_cae;
64     RK_S32 no_update;
65     RK_S32 upsampling;
66 
67     RK_S32 intra_blocks;
68     RK_S32 inter_blocks;
69     RK_S32 inter4v_blocks;
70     RK_S32 gmc_blocks;
71     RK_S32 not_coded_blocks;
72 
73     RK_S32 dct_coefs;
74     RK_S32 dct_lines;
75     RK_S32 vlc_symbols;
76     RK_S32 vlc_bits;
77 
78     RK_S32 apm;
79     RK_S32 npm;
80     RK_S32 interpolate_mc_q;
81     RK_S32 forw_back_mc_q;
82     RK_S32 halfpel2;
83     RK_S32 halfpel4;
84 
85     RK_S32 sadct;
86     RK_S32 quarterpel;
87 } Mpeg4Estimation;
88 
89 typedef struct Mp4HdrVol_t {
90     RK_S32  vo_type;
91     RK_U32  low_delay;
92     RK_U32  shape;
93     RK_S32  time_inc_resolution;
94     RK_U32  time_inc_bits;
95     RK_S32  width;
96     RK_S32  height;
97     RK_U32  mb_width;
98     RK_U32  mb_height;
99     RK_S32  hor_stride;
100     RK_S32  ver_stride;
101     RK_U32  totalMbInVop;
102     RK_U32  interlacing;
103     RK_S32  sprite_enable;
104     RK_U32  quant_bits;
105     RK_U32  quant_type;
106     RK_S32  quarter_sample;
107     RK_S32  complexity_estimation_disable;
108     RK_U32  resync_marker_disable;
109     RK_S32  newpred_enable;
110     RK_S32  reduced_resolution_enable;
111     RK_S32  scalability;
112     RK_S32  ver_id;
113 } Mp4HdrVol;
114 
115 typedef struct Mp4HdrUserData_t {
116     RK_S32  packed_mode;                /* bframes packed bits? (1 = yes) */
117 } Mp4HdrUserData;
118 
119 typedef struct Mp4HdrVop_t {
120     RK_S32  coding_type;
121     RK_U32  frameNumber;
122     RK_U32  rounding;
123     RK_U32  intra_dc_vlc_threshold;
124     RK_U32  top_field_first;
125     RK_U32  alternate_vertical_scan;
126     RK_U32  fcode_forward;
127     RK_U32  fcode_backward;
128     RK_U32  quant;                      // OFFSET_OF_QUANT_IN_DEC
129     RK_U32  hdr_bits;
130 } Mp4HdrVop;
131 
132 typedef struct Mpg4Hdr_t {
133     // vol parameter
134     Mp4HdrVol       vol;
135 
136     // user data parameter
137     Mp4HdrUserData  usr;
138 
139     // vop header parameter
140     Mp4HdrVop       vop;
141 
142     // frame related parameter
143     RK_S64  pts;
144     RK_S32  slot_idx;
145     RK_U32  enqueued;
146 
147     RK_U32  last_time_base;
148     RK_U32  time_base;
149     RK_U32  time;
150     RK_U32  time_pp;
151     RK_U32  time_bp;
152     RK_U32  last_non_b_time;
153 } Mpg4Hdr;
154 
155 
156 typedef struct {
157     // global paramter
158     MppBufSlots     frame_slots;
159     RK_U32          found_vol;
160     RK_U32          found_vop;
161     RK_U32          found_i_vop;
162 
163     // frame size parameter
164     RK_S32          width;
165     RK_S32          height;
166     RK_S32          hor_stride;
167     RK_S32          ver_stride;
168     RK_U32          info_change;
169     RK_U32          eos;
170 
171     // spliter parameter
172     RK_U32          state;
173     RK_U32          vop_header_found;   // flag: visual object plane header found
174 
175     // bit read context
176     BitReadCtx_t    *bit_ctx;
177     // vos infomation
178     RK_U32          profile;
179     RK_U32          level;
180     RK_U32          custorm_version;
181     // commom buffer for header information
182     /*
183      * NOTE: We assume that quant matrix only used for current frame decoding
184      *       So we use only one quant matrix buffer and only support
185      */
186     RK_U32          new_qm[2];              // [0] - intra [1] - inter
187     RK_U8           quant_matrices[128];    // 0-63: intra 64-127: inter
188     Mpeg4Estimation estimation;
189     Mpg4Hdr         hdr_curr;               /* header for current decoding frame */
190     Mpg4Hdr         hdr_ref0;               /* header for reference frame 0 */
191     Mpg4Hdr         hdr_ref1;               /* header for reference frame 1 */
192 
193     // dpb/output information
194     RK_S32          output;
195     RK_S64          last_pts;
196     RK_S64          pts_inc;
197     RK_S64          pts;
198     RK_S64          dts;
199     RK_U32          frame_num;
200     MppDecCfgSet    *dec_cfg;
201 
202     // syntax for hal
203     mpeg4d_dxva2_picture_context_t *syntax;
204 } Mpg4dParserImpl;
205 
log2bin(RK_U32 value)206 static RK_S32 log2bin(RK_U32 value)
207 {
208     RK_S32 n = 0;
209 
210     while (value) {
211         value >>= 1;
212         n++;
213     }
214 
215     return n;
216 }
217 
mpg4d_parse_matrix(BitReadCtx_t * gb,RK_U8 * matrix)218 static MPP_RET mpg4d_parse_matrix(BitReadCtx_t *gb, RK_U8 * matrix)
219 {
220     RK_S32 i = 0;
221     RK_S32 last, value = 0;
222 
223     do {
224         last = value;
225         READ_BITS(gb, 8, &value);
226         matrix[i++] = value;
227     } while (value != 0 && i < 64);
228 
229     if (value != 0)
230         return MPP_ERR_STREAM;
231 
232     i--;
233 
234     while (i < 64) {
235         matrix[i++ ] = last;
236     }
237 
238     return MPP_OK;
239 
240 __BITREAD_ERR:
241     return MPP_ERR_STREAM;
242 }
243 
mpg4d_set_intra_matrix(RK_U8 * quant_matrices,RK_U8 * matrix)244 static void mpg4d_set_intra_matrix(RK_U8 * quant_matrices, RK_U8 * matrix)
245 {
246     RK_S32 i;
247     RK_U8 *intra_matrix = quant_matrices + 0 * 64;
248 
249     for (i = 0; i < 64; i++) {
250         intra_matrix[i] = (!i) ? (RK_U8)8 : (RK_U8)matrix[i];
251     }
252 }
253 
mpg4d_set_inter_matrix(RK_U8 * quant_matrices,RK_U8 * matrix)254 static void mpg4d_set_inter_matrix(RK_U8 * quant_matrices, RK_U8 * matrix)
255 {
256     RK_S32 i;
257     RK_U8 *inter_matrix = quant_matrices + 1 * 64;
258 
259     for (i = 0; i < 64; i++) {
260         inter_matrix[i] = (RK_U8) (matrix[i]);
261     }
262 }
263 
read_vol_complexity_estimation_header(Mpeg4Estimation * e,BitReadCtx_t * gb)264 static MPP_RET read_vol_complexity_estimation_header(Mpeg4Estimation *e, BitReadCtx_t *gb)
265 {
266     RK_U32 val;
267 
268     READ_BITS(gb, 2, &(e->method));                     /* estimation_method */
269 
270     if (e->method == 0 || e->method == 1) {
271         READ_BITS(gb, 1, &val);
272         if (!val) {                                     /* shape_complexity_estimation_disable */
273             READ_BITS(gb, 1, &(e->opaque));             /* opaque */
274             READ_BITS(gb, 1, &(e->transparent));        /* transparent */
275             READ_BITS(gb, 1, &(e->intra_cae));          /* intra_cae */
276             READ_BITS(gb, 1, &(e->inter_cae));          /* inter_cae */
277             READ_BITS(gb, 1, &(e->no_update));          /* no_update */
278             READ_BITS(gb, 1, &(e->upsampling));         /* upsampling */
279         }
280 
281         READ_BITS(gb, 1, &val);
282         if (!val) {                                     /* texture_complexity_estimation_set_1_disable */
283             READ_BITS(gb, 1, &(e->intra_blocks));       /* intra_blocks */
284             READ_BITS(gb, 1, &(e->inter_blocks));       /* inter_blocks */
285             READ_BITS(gb, 1, &(e->inter4v_blocks));     /* inter4v_blocks */
286             READ_BITS(gb, 1, &(e->not_coded_blocks));   /* not_coded_blocks */
287         }
288     }
289 
290     SKIP_BITS(gb, 1);
291 
292     READ_BITS(gb, 1, &val);
293     if (!val) {                                         /* texture_complexity_estimation_set_2_disable */
294         READ_BITS(gb, 1, &(e->dct_coefs));              /* dct_coefs */
295         READ_BITS(gb, 1, &(e->dct_lines));              /* dct_lines */
296         READ_BITS(gb, 1, &(e->vlc_symbols));            /* vlc_symbols */
297         READ_BITS(gb, 1, &(e->vlc_bits));               /* vlc_bits */
298     }
299 
300     READ_BITS(gb, 1, &val);
301     if (!val) {                                         /* motion_compensation_complexity_disable */
302         READ_BITS(gb, 1, &(e->apm));                    /* apm */
303         READ_BITS(gb, 1, &(e->npm));                    /* npm */
304         READ_BITS(gb, 1, &(e->interpolate_mc_q));       /* interpolate_mc_q */
305         READ_BITS(gb, 1, &(e->forw_back_mc_q));         /* forw_back_mc_q */
306         READ_BITS(gb, 1, &(e->halfpel2));               /* halfpel2 */
307         READ_BITS(gb, 1, &(e->halfpel4));               /* halfpel4 */
308     }
309 
310     SKIP_BITS(gb, 1);
311 
312     if (e->method == 1) {
313         READ_BITS(gb, 1, &val);
314         if (!val) {                                     /* version2_complexity_estimation_disable */
315             READ_BITS(gb, 1, &(e->sadct));              /* sadct */
316             READ_BITS(gb, 1, &(e->quarterpel));         /* quarterpel */
317         }
318     }
319 
320     return MPP_OK;
321 
322 __BITREAD_ERR:
323     return MPP_ERR_STREAM;
324 }
325 
326 /* vop estimation header */
read_vop_complexity_estimation_header(Mpeg4Estimation * e,BitReadCtx_t * gb,Mpg4Hdr * mp4Hdr,int coding_type)327 static MPP_RET read_vop_complexity_estimation_header(Mpeg4Estimation *e, BitReadCtx_t *gb, Mpg4Hdr *mp4Hdr, int coding_type)
328 {
329     if (e->method == 0 || e->method == 1) {
330         if (coding_type == MPEG4_I_VOP) {
331             if (e->opaque)  SKIP_BITS(gb, 8);           /* dcecs_opaque */
332             if (e->transparent) SKIP_BITS(gb, 8);       /* */
333             if (e->intra_cae) SKIP_BITS(gb, 8);         /* */
334             if (e->inter_cae) SKIP_BITS(gb, 8);         /* */
335             if (e->no_update) SKIP_BITS(gb, 8);         /* */
336             if (e->upsampling) SKIP_BITS(gb, 8);        /* */
337             if (e->intra_blocks) SKIP_BITS(gb, 8);      /* */
338             if (e->not_coded_blocks) SKIP_BITS(gb, 8);  /* */
339             if (e->dct_coefs) SKIP_BITS(gb, 8);         /* */
340             if (e->dct_lines) SKIP_BITS(gb, 8);         /* */
341             if (e->vlc_symbols) SKIP_BITS(gb, 8);       /* */
342             if (e->vlc_bits) SKIP_BITS(gb, 8);          /* */
343             if (e->sadct)  SKIP_BITS(gb, 8);            /* */
344         }
345 
346         if (coding_type == MPEG4_P_VOP) {
347             if (e->opaque) SKIP_BITS(gb, 8);            /* */
348             if (e->transparent) SKIP_BITS(gb, 8);       /* */
349             if (e->intra_cae) SKIP_BITS(gb, 8);         /* */
350             if (e->inter_cae) SKIP_BITS(gb, 8);         /* */
351             if (e->no_update) SKIP_BITS(gb, 8);         /* */
352             if (e->upsampling) SKIP_BITS(gb, 8);        /* */
353             if (e->intra_blocks) SKIP_BITS(gb, 8);      /* */
354             if (e->not_coded_blocks) SKIP_BITS(gb, 8);  /* */
355             if (e->dct_coefs) SKIP_BITS(gb, 8);         /* */
356             if (e->dct_lines) SKIP_BITS(gb, 8);         /* */
357             if (e->vlc_symbols) SKIP_BITS(gb, 8);       /* */
358             if (e->vlc_bits) SKIP_BITS(gb, 8);          /* */
359             if (e->inter_blocks) SKIP_BITS(gb, 8);      /* */
360             if (e->inter4v_blocks) SKIP_BITS(gb, 8);    /* */
361             if (e->apm) SKIP_BITS(gb, 8);               /* */
362             if (e->npm) SKIP_BITS(gb, 8);               /* */
363             if (e->forw_back_mc_q) SKIP_BITS(gb, 8);    /* */
364             if (e->halfpel2) SKIP_BITS(gb, 8);          /* */
365             if (e->halfpel4) SKIP_BITS(gb, 8);          /* */
366             if (e->sadct) SKIP_BITS(gb, 8);             /* */
367             if (e->quarterpel) SKIP_BITS(gb, 8);        /* */
368         }
369 
370         if (coding_type == MPEG4_B_VOP) {
371             if (e->opaque)  SKIP_BITS(gb, 8);           /* */
372             if (e->transparent) SKIP_BITS(gb, 8);       /* */
373             if (e->intra_cae) SKIP_BITS(gb, 8);         /* */
374             if (e->inter_cae) SKIP_BITS(gb, 8);         /* */
375             if (e->no_update) SKIP_BITS(gb, 8);         /* */
376             if (e->upsampling) SKIP_BITS(gb, 8);        /* */
377             if (e->intra_blocks) SKIP_BITS(gb, 8);      /* */
378             if (e->not_coded_blocks) SKIP_BITS(gb, 8);  /* */
379             if (e->dct_coefs) SKIP_BITS(gb, 8);         /* */
380             if (e->dct_lines) SKIP_BITS(gb, 8);         /* */
381             if (e->vlc_symbols) SKIP_BITS(gb, 8);       /* */
382             if (e->vlc_bits) SKIP_BITS(gb, 8);          /* */
383             if (e->inter_blocks) SKIP_BITS(gb, 8);      /* */
384             if (e->inter4v_blocks) SKIP_BITS(gb, 8);    /* */
385             if (e->apm) SKIP_BITS(gb, 8);               /* */
386             if (e->npm) SKIP_BITS(gb, 8);               /* */
387             if (e->forw_back_mc_q) SKIP_BITS(gb, 8);    /* */
388             if (e->halfpel2) SKIP_BITS(gb, 8);          /* */
389             if (e->halfpel4) SKIP_BITS(gb, 8);          /* */
390             if (e->interpolate_mc_q) SKIP_BITS(gb, 8);  /* */
391             if (e->sadct)  SKIP_BITS(gb, 8);            /* */
392             if (e->quarterpel) SKIP_BITS(gb, 8);        /* */
393         }
394 
395 #ifdef GMC_SUPPORT
396         if (coding_type == MPEG4_S_VOP && mp4Hdr->vol.sprite_enable == MPEG4_SPRITE_STATIC) {
397             if (e->intra_blocks) SKIP_BITS(gb, 8);      /* */
398             if (e->not_coded_blocks) SKIP_BITS(gb, 8);  /* */
399             if (e->dct_coefs) SKIP_BITS(gb, 8);         /* */
400             if (e->dct_lines) SKIP_BITS(gb, 8);         /* */
401             if (e->vlc_symbols) SKIP_BITS(gb, 8);       /* */
402             if (e->vlc_bits) SKIP_BITS(gb, 8);          /* */
403             if (e->inter_blocks) SKIP_BITS(gb, 8);      /* */
404             if (e->inter4v_blocks) SKIP_BITS(gb, 8);    /* */
405             if (e->apm) SKIP_BITS(gb, 8);               /* */
406             if (e->npm) SKIP_BITS(gb, 8);               /* */
407             if (e->forw_back_mc_q) SKIP_BITS(gb, 8);    /* */
408             if (e->halfpel2) SKIP_BITS(gb, 8);          /* */
409             if (e->halfpel4) SKIP_BITS(gb, 8);          /* */
410             if (e->interpolate_mc_q) SKIP_BITS(gb, 8);  /* */
411         }
412 #else
413         (void)mp4Hdr;
414 #endif
415     }
416 
417     return MPP_OK;
418 
419 __BITREAD_ERR:
420     return MPP_ERR_STREAM;
421 }
422 
init_mpg4_hdr_vol(Mpg4Hdr * header)423 static void init_mpg4_hdr_vol(Mpg4Hdr *header)
424 {
425     memset(&header->vol, 0, sizeof(header->vol));
426     header->vol.ver_id = 1;
427 }
428 
init_mpg4_hdr_vop(Mpg4Hdr * header)429 static void init_mpg4_hdr_vop(Mpg4Hdr *header)
430 {
431     memset(&header->vop, 0, sizeof(header->vop));
432     header->vop.coding_type = MPEG4_INVALID_VOP;
433 }
434 
init_mpg4_header(Mpg4Hdr * header)435 static void init_mpg4_header(Mpg4Hdr *header)
436 {
437     init_mpg4_hdr_vol(header);
438     header->usr.packed_mode = 0;
439     init_mpg4_hdr_vop(header);
440     header->pts         = 0;
441     header->slot_idx    = -1;
442     header->enqueued    = 0;
443 }
444 
mpg4d_parse_vol_header(Mpg4dParserImpl * p,BitReadCtx_t * cb)445 static MPP_RET mpg4d_parse_vol_header(Mpg4dParserImpl *p, BitReadCtx_t *cb)
446 {
447     RK_U32 val = 0;
448     Mpg4Hdr *mp4Hdr = &p->hdr_curr;
449     RK_S32 vol_ver_id;
450     RK_S32 aspect_ratio;
451     RK_S32 vol_control_parameters;
452 
453     SKIP_BITS(cb, 1);                                   /* random_accessible_vol */
454     READ_BITS(cb, 8, &(mp4Hdr->vol.vo_type));
455 
456     READ_BITS(cb, 1, &val); /* is_object_layer_identifier */
457     if (val) {
458         READ_BITS(cb, 4, &vol_ver_id);  /* video_object_layer_verid */
459         SKIP_BITS(cb, 3);                               /* video_object_layer_priority */
460     } else {
461         vol_ver_id = mp4Hdr->vol.ver_id;
462     }
463 
464     READ_BITS(cb, 4, &aspect_ratio);
465 
466     if (aspect_ratio == MPEG4_VIDOBJLAY_AR_EXTPAR) {    /* aspect_ratio_info */
467         RK_S32 par_width, par_height;
468 
469         READ_BITS(cb, 8, &par_width);                   /* par_width */
470         READ_BITS(cb, 8, &par_height);                  /* par_height */
471     }
472 
473     READ_BITS(cb, 1, &vol_control_parameters);
474 
475     if (vol_control_parameters) {                       /* vol_control_parameters */
476         SKIP_BITS(cb, 2);                               /* chroma_format */
477         READ_BITS(cb, 1, &(mp4Hdr->vol.low_delay));     /* low_delay flage (1 means no B_VOP) */
478 
479         READ_BITS(cb, 1, &val); /* vbv_parameters */
480         if (val) {
481             RK_U32 bitrate;
482             RK_U32 buffer_size;
483             RK_U32 occupancy;
484 
485             READ_BITS(cb, 15, &val); /* first_half_bit_rate */
486             bitrate = val << 15;
487             SKIP_BITS(cb, 1);
488             READ_BITS(cb, 15, &val); /* latter_half_bit_rate */
489             bitrate |= val;
490             SKIP_BITS(cb, 1);
491 
492             READ_BITS(cb, 15, &val); /* first_half_vbv_buffer_size */
493             buffer_size = val << 3;
494             SKIP_BITS(cb, 1);
495             READ_BITS(cb, 3, &val);
496             buffer_size |= val;                             /* latter_half_vbv_buffer_size */
497 
498             READ_BITS(cb, 11, &val);
499             occupancy = val << 15;                          /* first_half_vbv_occupancy */
500             SKIP_BITS(cb, 1);
501             READ_BITS(cb, 15, &val);
502             occupancy |= val;                               /* latter_half_vbv_occupancy */
503             SKIP_BITS(cb, 1);
504             mpg4d_dbg_bit("bitrate %d, buffer_size %d, occupancy %d", bitrate, buffer_size, occupancy);
505         }
506     } else {
507         mp4Hdr->vol.low_delay = 0;
508     }
509 
510     if (mp4Hdr->vol.vo_type == 0 && vol_control_parameters == 0 && mp4Hdr->vop.frameNumber == 0) {
511         mpp_log("looks like this file was encoded with (divx4/(old)xvid/opendivx)\n");
512         return MPP_NOK;
513     }
514 
515     READ_BITS(cb, 2, &(mp4Hdr->vol.shape));                 /* video_object_layer_shape */
516 
517     if (mp4Hdr->vol.shape != MPEG4_VIDOBJLAY_SHAPE_RECTANGULAR) {
518         mpp_log("unsupported shape %d\n", mp4Hdr->vol.shape);
519         return MPP_NOK;
520     }
521 
522     if (mp4Hdr->vol.shape == MPEG4_VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) {
523         SKIP_BITS(cb, 4);                                   /* video_object_layer_shape_extension */
524     }
525 
526     SKIP_BITS(cb, 1);
527 
528     READ_BITS(cb, 16, &(mp4Hdr->vol.time_inc_resolution));  /* vop_time_increment_resolution */
529 
530     if (mp4Hdr->vol.time_inc_resolution > 0) {
531         mp4Hdr->vol.time_inc_bits = MPP_MAX(log2bin(mp4Hdr->vol.time_inc_resolution - 1), 1);
532     } else {
533         /* for "old" xvid compatibility, set time_inc_bits = 1 */
534         mp4Hdr->vol.time_inc_bits = 1;
535     }
536 
537     SKIP_BITS(cb, 1);
538 
539     READ_BITS(cb, 1, &val);
540     if (val) {                                          /* fixed_vop_rate */
541         SKIP_BITS(cb, mp4Hdr->vol.time_inc_bits);       /* fixed_vop_time_increment */
542     }
543 
544     if (mp4Hdr->vol.shape != MPEG4_VIDOBJLAY_SHAPE_BINARY_ONLY) {
545         if (mp4Hdr->vol.shape == MPEG4_VIDOBJLAY_SHAPE_RECTANGULAR) {
546             RK_S32 width, height;
547 
548             SKIP_BITS(cb, 1);
549             READ_BITS(cb, 13, &width);                  /* video_object_layer_width */
550             SKIP_BITS(cb, 1);
551             READ_BITS(cb, 13, &height);                 /* video_object_layer_height */
552             SKIP_BITS(cb, 1);
553 
554             mpg4d_dbg_bit("width %4d height %4d\n", width, height);
555 
556             if (width > 1920 || height > 1088) {
557                 mpp_err("Warning: unsupport larger than 1920x1088\n");
558                 return MPP_NOK;
559             }
560 
561             mp4Hdr->vol.width  = width;
562             mp4Hdr->vol.height = height;
563             mp4Hdr->vol.mb_width  = (mp4Hdr->vol.width  + 15) >> 4;
564             mp4Hdr->vol.mb_height = (mp4Hdr->vol.height + 15) >> 4;
565             mp4Hdr->vol.totalMbInVop = mp4Hdr->vol.mb_width * mp4Hdr->vol.mb_height;
566             mp4Hdr->vol.hor_stride = 16 * mp4Hdr->vol.mb_width;
567             mp4Hdr->vol.ver_stride = 16 * mp4Hdr->vol.mb_height;
568         }
569 
570         READ_BITS(cb, 1, &(mp4Hdr->vol.interlacing));
571 
572         READ_BITS(cb, 1, &val);
573         if (!val) {                                     /* obmc_disable */
574             /* TODO */
575             /* fucking divx4.02 has this enabled */
576         }
577 
578         /* sprite_enable */
579         READ_BITS(cb, (vol_ver_id == 1 ? 1 : 2), &(mp4Hdr->vol.sprite_enable));
580 
581         if (mp4Hdr->vol.sprite_enable != MPEG4_SPRITE_NONE) {
582             mpp_err("GMC is not supported\n");
583             return MPP_ERR_PROTOL;
584         }
585 
586         if (vol_ver_id != 1 &&
587             mp4Hdr->vol.shape != MPEG4_VIDOBJLAY_SHAPE_RECTANGULAR) {
588             SKIP_BITS(cb, 1);                           /* sadct_disable */
589         }
590 
591         READ_BITS(cb, 1, &val);
592         if (val) {                                      /* not_8_bit */
593             READ_BITS(cb, 4, &(mp4Hdr->vol.quant_bits));/* quant_precision */
594             SKIP_BITS(cb, 4);                           /* bits_per_pixel */
595         } else {
596             mp4Hdr->vol.quant_bits = 5;
597         }
598 
599         if (mp4Hdr->vol.shape == MPEG4_VIDOBJLAY_SHAPE_GRAYSCALE) {
600             SKIP_BITS(cb, 1);                           /* no_gray_quant_update */
601             SKIP_BITS(cb, 1);                           /* composition_method */
602             SKIP_BITS(cb, 1);                           /* linear_composition */
603         }
604 
605         READ_BITS(cb, 1, &(mp4Hdr->vol.quant_type));    /* quant_type */
606 
607         if (mp4Hdr->vol.quant_type) {
608             RK_U8 matrix[64];
609 
610             READ_BITS(cb, 1, &val);
611             p->new_qm[0] = val;
612             if (val) {                                  /* load_intra_quant_mat */
613                 mpg4d_parse_matrix(cb, matrix);
614                 mpg4d_set_intra_matrix(p->quant_matrices, matrix);
615             }
616 
617             READ_BITS(cb, 1, &val);
618             p->new_qm[1] = val;
619             if (val) {                                  /* load_inter_quant_mat */
620                 mpg4d_parse_matrix(cb, matrix);
621                 mpg4d_set_inter_matrix(p->quant_matrices, matrix);
622             }
623 
624             if (mp4Hdr->vol.shape == MPEG4_VIDOBJLAY_SHAPE_GRAYSCALE) {
625                 mpp_err("SHAPE_GRAYSCALE is not supported\n");
626                 return MPP_NOK;
627             }
628         } else {
629             p->new_qm[0] = 0;
630             p->new_qm[1] = 0;
631         }
632 
633         if (vol_ver_id != 1) {
634             READ_BITS(cb, 1, &(mp4Hdr->vol.quarter_sample));
635         } else
636             mp4Hdr->vol.quarter_sample = 0;
637 
638         /* complexity estimation disable */
639         READ_BITS(cb, 1, &(mp4Hdr->vol.complexity_estimation_disable));
640 
641         if (!mp4Hdr->vol.complexity_estimation_disable) {
642             mpg4d_dbg_bit("read_vol_complexity_estimation_header\n");
643             if (read_vol_complexity_estimation_header(&p->estimation, cb))
644                 return MPP_ERR_STREAM;
645         }
646 
647         /* resync_marker_disable */
648         READ_BITS(cb, 1, &(mp4Hdr->vol.resync_marker_disable));
649         if (!mp4Hdr->vol.resync_marker_disable) {
650             mpp_log("resync marker enabled\n");
651             // return MPEG4_RESYNC_VOP;
652         }
653 
654         READ_BITS(cb, 1, &val);
655         if (val) {                                      /* data_partitioned */
656             SKIP_BITS(cb, 1);                           /* reversible_vlc */
657         }
658 
659         if (vol_ver_id != 1) {
660             READ_BITS(cb, 1, &(mp4Hdr->vol.newpred_enable));
661 
662             if (mp4Hdr->vol.newpred_enable) {               /* newpred_enable */
663                 SKIP_BITS(cb, 2);                       /* requested_upstream_message_type */
664                 SKIP_BITS(cb, 1);                       /* newpred_segment_type */
665             }
666 
667             /* reduced_resolution_vop_enable */
668             READ_BITS(cb, 1, &(mp4Hdr->vol.reduced_resolution_enable));
669         } else {
670             mp4Hdr->vol.newpred_enable = 0;
671             mp4Hdr->vol.reduced_resolution_enable = 0;
672         }
673 
674         READ_BITS(cb, 1, &mp4Hdr->vol.scalability);         /* scalability */
675 
676         if (mp4Hdr->vol.scalability) {
677             SKIP_BITS(cb, 1);                           /* hierarchy_type */
678             SKIP_BITS(cb, 4);                           /* ref_layer_id */
679             SKIP_BITS(cb, 1);                           /* ref_layer_sampling_direc */
680             SKIP_BITS(cb, 5);                           /* hor_sampling_factor_n */
681             SKIP_BITS(cb, 5);                           /* hor_sampling_factor_m */
682             SKIP_BITS(cb, 5);                           /* vert_sampling_factor_n */
683             SKIP_BITS(cb, 5);                           /* vert_sampling_factor_m */
684             SKIP_BITS(cb, 1);                           /* enhancement_type */
685 
686             if (mp4Hdr->vol.shape == MPEG4_VIDOBJLAY_SHAPE_BINARY /* && hierarchy_type==0 */) {
687                 /* use_ref_shape and so on*/
688                 SKIP_BITS(cb, ( 1 + 1 + 1 + 5 + 5 + 5 + 5));
689             }
690 
691             mpp_err("scalability is not supported\n");
692             return MPP_NOK;
693         }
694     } else { /* mp4Hdr->shape == BINARY_ONLY */
695         mpp_err("shape %d is not supported\n");
696         return MPP_NOK;
697     }
698 
699     return MPP_OK;
700 
701 __BITREAD_ERR:
702     return MPP_ERR_STREAM;
703 }
704 
mpg4d_parse_user_data(Mpg4dParserImpl * p,BitReadCtx_t * gb)705 static MPP_RET mpg4d_parse_user_data(Mpg4dParserImpl *p, BitReadCtx_t *gb)
706 {
707     RK_U8 tmp[256];
708     Mpg4Hdr *mp4Hdr = &p->hdr_curr;
709     RK_U32 remain_bit = gb->bytes_left_ * 8 + gb->num_remaining_bits_in_curr_byte_;
710     RK_S32 i;
711 
712     memset(tmp, 0, 256);
713 
714     for (i = 0; i < 255 && gb->bytes_left_; i++) {
715         RK_U32 show_bit = MPP_MIN(remain_bit, 23);
716         RK_U32 val;
717 
718         SHOW_BITS(gb, show_bit, &val);
719         if (val == 0)
720             break;
721 
722         READ_BITS(gb, 8, &val);
723         tmp[i] = val;
724         remain_bit -= 8;
725     }
726 
727     /*
728      * The value of i in the for loop above must be less than 255 otherwise it will cause
729      * tmp [256] = 0 which is overflow
730      */
731     tmp[i] = 0;
732 
733     if (!mp4Hdr->usr.packed_mode) {
734         RK_U32 packed = 0;
735 
736         if ((tmp[0] == 'D') &&
737             (tmp[1] == 'i') &&
738             (tmp[2] == 'v') &&
739             (tmp[3] == 'X')) {
740             RK_U32 j = 4;
741 
742             if (tmp[j] <= '4') {
743                 p->custorm_version = 4;
744             } else {
745                 p->custorm_version = 5;
746             }
747 
748             while ((tmp[j] >= '0') &&
749                    (tmp[j] <= '9'))
750                 j++;
751 
752             if (tmp[j] == 'b') {
753                 j++;
754 
755                 while ((tmp[j] >= '0') &&
756                        (tmp[j] <= '9'))
757                     j++;
758 
759                 packed = tmp[j];
760             } else if ((tmp[j + 0] == 'B') &&
761                        (tmp[j + 1] == 'u') &&
762                        (tmp[j + 2] == 'i') &&
763                        (tmp[j + 3] == 'l') &&
764                        (tmp[j + 4] == 'd')) {
765                 j += 5;
766 
767                 while ((tmp[j] >= '0') && (tmp[j] <= '9'))
768                     j++;
769 
770                 packed = tmp[j];
771             }
772 
773             mp4Hdr->usr.packed_mode = ((packed == 'p') ? (1) : (0));
774         } else {
775             mp4Hdr->usr.packed_mode = 0;
776         }
777     }
778 
779     return MPP_OK;
780 
781 __BITREAD_ERR:
782     return MPP_ERR_STREAM;
783 }
784 
mpeg4_parse_gop_header(Mpg4dParserImpl * p,BitReadCtx_t * gb)785 static MPP_RET mpeg4_parse_gop_header(Mpg4dParserImpl *p, BitReadCtx_t *gb)
786 {
787     (void) p;
788     RK_S32 hours, minutes, seconds;
789 
790     READ_BITS(gb, 5, &hours);
791     READ_BITS(gb, 6, &minutes);
792     SKIP_BITS(gb, 1);
793     READ_BITS(gb, 6, &seconds);
794 
795     SKIP_BITS(gb, 1); /* closed_gov */
796     SKIP_BITS(gb, 1); /* broken_link */
797 
798     return MPP_OK;
799 
800 __BITREAD_ERR:
801     return MPP_ERR_STREAM;
802 }
803 
mpeg4_parse_profile_level(Mpg4dParserImpl * p,BitReadCtx_t * bc)804 static MPP_RET mpeg4_parse_profile_level(Mpg4dParserImpl *p, BitReadCtx_t *bc)
805 {
806     READ_BITS(bc, 4, &p->profile);
807     READ_BITS(bc, 4, &p->level);
808     return MPP_OK;
809 
810 __BITREAD_ERR:
811     return MPP_ERR_STREAM;
812 }
813 
mpeg4_parse_vop_header(Mpg4dParserImpl * p,BitReadCtx_t * gb)814 static MPP_RET mpeg4_parse_vop_header(Mpg4dParserImpl *p, BitReadCtx_t *gb)
815 {
816     RK_U32 val;
817     RK_U32 time_incr = 0;
818     RK_S32 time_increment = 0;
819     Mpg4Hdr *mp4Hdr = &p->hdr_curr;
820 
821     READ_BITS(gb, 2, &(mp4Hdr->vop.coding_type));       /* vop_coding_type */
822 
823     READ_BITS(gb, 1, &val);
824     while (val != 0) {
825         time_incr++;                                    /* time_base */
826         READ_BITS(gb, 1, &val);
827     }
828 
829     SKIP_BITS(gb, 1);
830 
831     if (mp4Hdr->vol.time_inc_bits) {
832         /* vop_time_increment */
833         READ_BITS(gb, mp4Hdr->vol.time_inc_bits, &time_increment);
834     }
835 
836     if (mp4Hdr->vop.coding_type != MPEG4_B_VOP) {
837         mp4Hdr->last_time_base = mp4Hdr->time_base;
838         mp4Hdr->time_base += time_incr;
839         mp4Hdr->time = mp4Hdr->time_base * mp4Hdr->vol.time_inc_resolution + time_increment;
840         mp4Hdr->time_pp = (RK_S32)(mp4Hdr->time - mp4Hdr->last_non_b_time);
841         mp4Hdr->last_non_b_time = mp4Hdr->time;
842     } else {
843         mp4Hdr->time = (mp4Hdr->last_time_base + time_incr) * mp4Hdr->vol.time_inc_resolution + time_increment;
844         mp4Hdr->time_bp = mp4Hdr->time_pp - (RK_S32)(mp4Hdr->last_non_b_time - mp4Hdr->time);
845     }
846 
847     SKIP_BITS(gb, 1);
848 
849     READ_BITS(gb, 1, &val);
850     if (!val) {                                         /* vop_coded */
851         mp4Hdr->vop.coding_type = MPEG4_N_VOP;
852         mpg4d_dbg_result("found N frame\n");
853         return MPP_OK;
854     }
855     /* do coding_type detection here in order to save time_bp / time_pp */
856     if (mp4Hdr->vop.coding_type == MPEG4_B_VOP &&
857         (p->hdr_ref0.slot_idx == -1 || p->hdr_ref1.slot_idx == -1)) {
858         mpg4d_dbg_result("MPEG4 DIVX 5 PBBI case found!\n");
859         return MPP_NOK;
860     }
861     if (mp4Hdr->vop.coding_type == MPEG4_I_VOP)
862         p->found_i_vop = 1;
863 
864     if (mp4Hdr->vol.newpred_enable) {
865         RK_S32 vop_id;
866         RK_S32 vop_id_for_prediction;
867 
868         READ_BITS(gb, (MPP_MIN(mp4Hdr->vol.time_inc_bits + 3, 15)), &vop_id);
869 
870         READ_BITS(gb, 1, &val);
871         if (val) {
872             /* vop_id_for_prediction_indication */
873             READ_BITS(gb, MPP_MIN(mp4Hdr->vol.time_inc_bits + 3, 15), &vop_id_for_prediction);
874         }
875 
876         SKIP_BITS(gb, 1);
877     }
878 
879     if ((mp4Hdr->vol.shape != MPEG4_VIDOBJLAY_SHAPE_BINARY_ONLY) &&
880         ((mp4Hdr->vop.coding_type == MPEG4_P_VOP) ||
881          (mp4Hdr->vop.coding_type == MPEG4_S_VOP &&
882           mp4Hdr->vol.sprite_enable == MPEG4_SPRITE_GMC))) {
883         READ_BITS(gb, 1, &(mp4Hdr->vop.rounding));          /* rounding_type */
884     }
885 
886     if (mp4Hdr->vol.reduced_resolution_enable &&
887         mp4Hdr->vol.shape == MPEG4_VIDOBJLAY_SHAPE_RECTANGULAR &&
888         (mp4Hdr->vop.coding_type == MPEG4_P_VOP || mp4Hdr->vop.coding_type == MPEG4_I_VOP)) {
889 
890         READ_BITS(gb, 1, &val);
891     }
892 
893     mpp_assert(mp4Hdr->vol.shape == MPEG4_VIDOBJLAY_SHAPE_RECTANGULAR);
894 
895     if (mp4Hdr->vol.shape != MPEG4_VIDOBJLAY_SHAPE_BINARY_ONLY) {
896         if (!mp4Hdr->vol.complexity_estimation_disable) {
897             read_vop_complexity_estimation_header(&p->estimation, gb, mp4Hdr, mp4Hdr->vop.coding_type);
898         }
899 
900         READ_BITS(gb, 3, &val);
901         /* intra_dc_vlc_threshold */
902         mp4Hdr->vop.intra_dc_vlc_threshold = val;
903         mp4Hdr->vop.top_field_first = 0;
904         mp4Hdr->vop.alternate_vertical_scan = 0;
905 
906         if (mp4Hdr->vol.interlacing) {
907             READ_BITS(gb, 1, &(mp4Hdr->vop.top_field_first));
908             READ_BITS(gb, 1, &(mp4Hdr->vop.alternate_vertical_scan));
909         }
910     }
911 
912     if ((mp4Hdr->vol.sprite_enable == MPEG4_SPRITE_STATIC ||
913          mp4Hdr->vol.sprite_enable == MPEG4_SPRITE_GMC) &&
914         mp4Hdr->vop.coding_type == MPEG4_S_VOP) {
915         mpp_err("unsupport split mode %d coding type %d\n",
916                 mp4Hdr->vol.sprite_enable, mp4Hdr->vop.coding_type);
917         return MPP_ERR_STREAM;
918     }
919 
920     READ_BITS(gb, mp4Hdr->vol.quant_bits, &(mp4Hdr->vop.quant));
921     if (mp4Hdr->vop.quant < 1)                              /* vop_quant */
922         mp4Hdr->vop.quant = 1;
923 
924     if (mp4Hdr->vop.coding_type != MPEG4_I_VOP) {
925         READ_BITS(gb, 3, &(mp4Hdr->vop.fcode_forward));     /* fcode_forward */
926     }
927 
928     if (mp4Hdr->vop.coding_type == MPEG4_B_VOP) {
929         READ_BITS(gb, 3, &(mp4Hdr->vop.fcode_backward));    /* fcode_backward */
930     }
931 
932     if (!mp4Hdr->vol.scalability) {
933         if ((mp4Hdr->vol.shape != MPEG4_VIDOBJLAY_SHAPE_RECTANGULAR) &&
934             (mp4Hdr->vop.coding_type != MPEG4_I_VOP)) {
935             SKIP_BITS(gb, 1);                           /* vop_shape_coding_type */
936         }
937     }
938 
939     // NOTE: record header used bits here
940     mp4Hdr->vop.hdr_bits = gb->used_bits;
941 
942     return MPP_OK;
943 __BITREAD_ERR:
944     return MPP_ERR_STREAM;
945 }
946 
mpg4d_fill_picture_parameters(const Mpg4dParserImpl * p,DXVA_PicParams_MPEG4_PART2 * pp)947 static void mpg4d_fill_picture_parameters(const Mpg4dParserImpl *p,
948                                           DXVA_PicParams_MPEG4_PART2 *pp)
949 {
950     const Mpg4Hdr *hdr_curr = &p->hdr_curr;
951     const Mpg4Hdr *hdr_ref0 = &p->hdr_ref0;
952     const Mpg4Hdr *hdr_ref1 = &p->hdr_ref1;
953 
954     pp->short_video_header = 0;
955     pp->vop_coding_type = hdr_curr->vop.coding_type;
956     pp->vop_quant = hdr_curr->vop.quant;
957     pp->wDecodedPictureIndex = hdr_curr->slot_idx;
958     pp->wForwardRefPictureIndex = hdr_ref0->slot_idx;
959     pp->wBackwardRefPictureIndex = hdr_ref1->slot_idx;
960     pp->vop_time_increment_resolution = hdr_curr->vol.time_inc_resolution;
961     pp->TRB[0] = 0; /* FIXME: */
962     pp->TRD[0] = 0; /* FIXME: */
963     pp->unPicPostProc = 0; /* FIXME: */
964     pp->interlaced = hdr_curr->vol.interlacing;
965     pp->quant_type = hdr_curr->vol.quant_type;
966     pp->quarter_sample = hdr_curr->vol.quarter_sample;
967     pp->resync_marker_disable = 0; /* FIXME: */
968     pp->data_partitioned = 0; /* FIXME: */
969     pp->reversible_vlc = 0; /* FIXME: */
970     pp->reduced_resolution_vop_enable = hdr_curr->vol.reduced_resolution_enable;
971     pp->vop_coded = (hdr_curr->vop.coding_type != MPEG4_N_VOP);
972     pp->vop_rounding_type = hdr_curr->vop.rounding;
973     pp->intra_dc_vlc_thr = hdr_curr->vop.intra_dc_vlc_threshold;
974     pp->top_field_first = hdr_curr->vop.top_field_first;
975     pp->alternate_vertical_scan_flag = hdr_curr->vop.alternate_vertical_scan;
976     pp->profile_and_level_indication = (p->profile << 4) | p->level;
977     pp->video_object_layer_verid = hdr_curr->vol.ver_id;
978     pp->vop_width = hdr_curr->vol.width;
979     pp->vop_height = hdr_curr->vol.height;
980     pp->sprite_enable = hdr_curr->vol.sprite_enable;
981     pp->no_of_sprite_warping_points = 0; /* FIXME: */
982     pp->sprite_warping_accuracy = 0; /* FIXME: */
983     memset(pp->warping_mv, 0, sizeof(pp->warping_mv));
984     pp->vop_fcode_forward = hdr_curr->vop.fcode_forward;
985     pp->vop_fcode_backward = hdr_curr->vop.fcode_backward;
986     pp->StatusReportFeedbackNumber = 0; /* FIXME: */
987     pp->Reserved16BitsA = 0; /* FIXME: */
988     pp->Reserved16BitsB = 0; /* FIXME: */
989 
990     // Rockchip special data
991     pp->custorm_version = p->custorm_version;
992     pp->prev_coding_type = (hdr_ref0->vop.coding_type == MPEG4_INVALID_VOP) ? (0) : (hdr_ref0->vop.coding_type);
993     pp->time_bp = hdr_curr->time_bp;
994     pp->time_pp = hdr_curr->time_pp;
995     pp->header_bits = hdr_curr->vop.hdr_bits;
996 }
997 
mpg4d_fill_quantization_matrices(const Mpg4dParserImpl * p,DXVA_QmatrixData * qm)998 static void mpg4d_fill_quantization_matrices(const Mpg4dParserImpl *p,
999                                              DXVA_QmatrixData *qm)
1000 {
1001     RK_S32 i = 0;
1002 
1003     qm->bNewQmatrix[0] = p->new_qm[0];
1004     qm->bNewQmatrix[1] = p->new_qm[1];
1005     qm->bNewQmatrix[2] = 0;
1006     qm->bNewQmatrix[3] = 0;
1007 
1008     // intra Y
1009     if (p->new_qm[0]) {
1010         for (i = 0; i < 64; i++) {
1011             qm->Qmatrix[0][i] = p->quant_matrices[i];
1012         }
1013     } else {
1014         memset(qm->Qmatrix[0], 0, sizeof(qm->Qmatrix[0]));
1015     }
1016 
1017     // inter Y
1018     if (p->new_qm[1]) {
1019         for (i = 0; i < 64; i++) {
1020             qm->Qmatrix[1][i] = p->quant_matrices[64 + i];
1021         }
1022     } else {
1023         memset(qm->Qmatrix[1], 0, sizeof(qm->Qmatrix[1]));
1024     }
1025 
1026     memset(qm->Qmatrix[2], 0, sizeof(qm->Qmatrix[2]));
1027     memset(qm->Qmatrix[3], 0, sizeof(qm->Qmatrix[3]));
1028 }
1029 
mpg4_syntax_init(mpeg4d_dxva2_picture_context_t * syntax)1030 static void mpg4_syntax_init(mpeg4d_dxva2_picture_context_t *syntax)
1031 {
1032     DXVA2_DecodeBufferDesc *data = &syntax->desc[0];
1033 
1034     //!< commit picture paramters
1035     memset(data, 0, sizeof(*data));
1036     data->CompressedBufferType = DXVA2_PictureParametersBufferType;
1037     data->pvPVPState = (void *)&syntax->pp;
1038     data->DataSize = sizeof(syntax->pp);
1039     syntax->data[0] = data;
1040 
1041     //!< commit Qmatrix
1042     data = &syntax->desc[1];
1043     memset(data, 0, sizeof(*data));
1044     data->CompressedBufferType = DXVA2_InverseQuantizationMatrixBufferType;
1045     data->pvPVPState = (void *)&syntax->qm;
1046     data->DataSize = sizeof(syntax->qm);
1047     data->NumMBsInBuffer = 0;
1048     syntax->data[1] = data;
1049 
1050     //!< commit bitstream
1051     data = &syntax->desc[2];
1052     memset(data, 0, sizeof(*data));
1053     data->CompressedBufferType = DXVA2_BitStreamDateBufferType;
1054     syntax->data[2] = data;
1055 }
1056 
mpp_mpg4_parser_init(Mpg4dParser * ctx,ParserCfg * cfg)1057 MPP_RET mpp_mpg4_parser_init(Mpg4dParser *ctx, ParserCfg *cfg)
1058 {
1059     BitReadCtx_t *bit_ctx = mpp_calloc(BitReadCtx_t, 1);
1060     Mpg4dParserImpl *p = mpp_calloc(Mpg4dParserImpl, 1);
1061     mpeg4d_dxva2_picture_context_t *syntax = mpp_calloc(mpeg4d_dxva2_picture_context_t, 1);
1062     MppBufSlots frame_slots = cfg->frame_slots;
1063 
1064     p->dec_cfg = cfg->cfg;
1065 
1066     if (NULL == p || NULL == bit_ctx || NULL == syntax) {
1067         mpp_err_f("malloc context failed\n");
1068         if (p)
1069             mpp_free(p);
1070         if (bit_ctx)
1071             mpp_free(bit_ctx);
1072         if (syntax)
1073             mpp_free(syntax);
1074         return MPP_NOK;
1075     }
1076 
1077     mpg4d_dbg_func("in\n");
1078 
1079     mpp_buf_slot_setup(frame_slots, 8);
1080     p->frame_slots      = frame_slots;
1081     p->state            = -1;
1082     p->vop_header_found = 0;
1083     p->bit_ctx          = bit_ctx;
1084     init_mpg4_header(&p->hdr_curr);
1085     init_mpg4_header(&p->hdr_ref0);
1086     init_mpg4_header(&p->hdr_ref1);
1087     mpg4_syntax_init(syntax);
1088     p->syntax = syntax;
1089 
1090     mpp_env_get_u32("mpg4d_debug", &mpg4d_debug, 0);
1091 
1092     mpg4d_dbg_func("out\n");
1093 
1094     *ctx = p;
1095     return MPP_OK;
1096 }
1097 
mpp_mpg4_parser_deinit(Mpg4dParser ctx)1098 MPP_RET mpp_mpg4_parser_deinit(Mpg4dParser ctx)
1099 {
1100     Mpg4dParserImpl *p = (Mpg4dParserImpl *)ctx;
1101 
1102     mpg4d_dbg_func("in\n");
1103 
1104     if (p) {
1105         if (p->bit_ctx) {
1106             mpp_free(p->bit_ctx);
1107             p->bit_ctx = NULL;
1108         }
1109         if (p->syntax) {
1110             mpp_free(p->syntax);
1111             p->syntax = NULL;
1112         }
1113         mpp_free(p);
1114     }
1115 
1116     mpg4d_dbg_func("out\n");
1117 
1118     return MPP_OK;
1119 }
1120 
mpp_mpg4_parser_flush(Mpg4dParser ctx)1121 MPP_RET mpp_mpg4_parser_flush(Mpg4dParser ctx)
1122 {
1123     Mpg4dParserImpl *p = (Mpg4dParserImpl *)ctx;
1124     MppBufSlots slots = p->frame_slots;
1125     Mpg4Hdr *hdr_ref0 = &p->hdr_ref0;
1126     RK_S32 index = hdr_ref0->slot_idx;
1127 
1128     mpg4d_dbg_func("in\n");
1129 
1130     if (!hdr_ref0->enqueued && index >= 0) {
1131         mpp_buf_slot_set_flag(slots, index, SLOT_QUEUE_USE);
1132         mpp_buf_slot_enqueue(slots, index, QUEUE_DISPLAY);
1133         hdr_ref0->enqueued = 1;
1134     }
1135 
1136     mpg4d_dbg_func("out\n");
1137 
1138     return MPP_OK;
1139 }
1140 
mpp_mpg4_parser_reset(Mpg4dParser ctx)1141 MPP_RET mpp_mpg4_parser_reset(Mpg4dParser ctx)
1142 {
1143     Mpg4dParserImpl *p = (Mpg4dParserImpl *)ctx;
1144     MppBufSlots slots = p->frame_slots;
1145     Mpg4Hdr *hdr_ref0 = &p->hdr_ref0;
1146     Mpg4Hdr *hdr_ref1 = &p->hdr_ref1;
1147     RK_S32 index = hdr_ref0->slot_idx;
1148 
1149     mpg4d_dbg_func("in\n");
1150 
1151     if (index >= 0) {
1152         if (!hdr_ref0->enqueued) {
1153             mpp_buf_slot_set_flag(slots, index, SLOT_QUEUE_USE);
1154             mpp_buf_slot_enqueue(slots, index, QUEUE_DISPLAY);
1155             hdr_ref0->enqueued = 1;
1156         }
1157         mpp_buf_slot_clr_flag(slots, index, SLOT_CODEC_USE);
1158         hdr_ref0->slot_idx = -1;
1159     }
1160 
1161     index = hdr_ref1->slot_idx;
1162     if (index >= 0) {
1163         mpp_buf_slot_clr_flag(slots, index, SLOT_CODEC_USE);
1164         hdr_ref1->slot_idx = -1;
1165     }
1166 
1167     p->found_i_vop      = 0;
1168     p->found_vop        = 0;
1169     p->state            = -1;
1170     p->vop_header_found = 0;
1171 
1172     mpg4d_dbg_func("out\n");
1173 
1174     return MPP_OK;
1175 }
1176 
mpp_mpg4_parser_split(Mpg4dParser ctx,MppPacket dst,MppPacket src)1177 MPP_RET mpp_mpg4_parser_split(Mpg4dParser ctx, MppPacket dst, MppPacket src)
1178 {
1179     MPP_RET ret = MPP_NOK;
1180     Mpg4dParserImpl *p = (Mpg4dParserImpl *)ctx;
1181     RK_U8 *src_buf = (RK_U8 *)mpp_packet_get_pos(src);
1182     RK_U32 src_len = (RK_U32)mpp_packet_get_length(src);
1183     RK_U32 src_eos = mpp_packet_get_eos(src);
1184     RK_S64 src_pts = mpp_packet_get_pts(src);
1185     RK_U8 *dst_buf = (RK_U8 *)mpp_packet_get_data(dst);
1186     RK_U32 dst_len = (RK_U32)mpp_packet_get_length(dst);
1187     RK_U32 src_pos = 0;
1188 
1189     mpg4d_dbg_func("in\n");
1190 
1191     // find the began of the vop
1192     if (!p->vop_header_found) {
1193         // add last startcode to the new frame data
1194         if ((dst_len < sizeof(p->state))
1195             && ((p->state & 0x00FFFFFF) == 0x000001)) {
1196             dst_buf[0] = 0;
1197             dst_buf[1] = 0;
1198             dst_buf[2] = 1;
1199             dst_len = 3;
1200         }
1201         while (src_pos < src_len) {
1202             p->state = (p->state << 8) | src_buf[src_pos];
1203             dst_buf[dst_len++] = src_buf[src_pos++];
1204             if (p->state == MPG4_VOP_STARTCODE) {
1205                 p->vop_header_found = 1;
1206                 mpp_packet_set_pts(dst, src_pts);
1207                 break;
1208             }
1209         }
1210     }
1211     // find the end of the vop
1212     if (p->vop_header_found) {
1213         while (src_pos < src_len) {
1214             p->state = (p->state << 8) | src_buf[src_pos];
1215             dst_buf[dst_len++] = src_buf[src_pos++];
1216             if ((p->state & 0x00FFFFFF) == 0x000001) {
1217                 dst_len -= 3;
1218                 p->vop_header_found = 0;
1219                 ret = MPP_OK; // split complete
1220                 break;
1221             }
1222         }
1223     }
1224     // the last packet
1225     if (src_eos && src_pos >= src_len) {
1226         mpp_packet_set_eos(dst);
1227         ret = MPP_OK;
1228     }
1229     // reset the src and dst
1230     mpp_packet_set_length(dst, dst_len);
1231     mpp_packet_set_pos(src, src_buf + src_pos);
1232 
1233     mpg4d_dbg_func("out\n");
1234 
1235     return ret;
1236 }
1237 
mpp_mpg4_parser_decode(Mpg4dParser ctx,MppPacket pkt)1238 MPP_RET mpp_mpg4_parser_decode(Mpg4dParser ctx, MppPacket pkt)
1239 {
1240     MPP_RET ret = MPP_OK;
1241     Mpg4dParserImpl *p = (Mpg4dParserImpl *)ctx;
1242     BitReadCtx_t *gb = p->bit_ctx;
1243     RK_U8 *buf = mpp_packet_get_data(pkt);
1244     RK_S32 len = (RK_S32)mpp_packet_get_length(pkt);
1245     RK_U32 startcode = 0xff;
1246 
1247     mpg4d_dbg_func("in\n");
1248 
1249     // setup bit read context
1250     mpp_set_bitread_ctx(gb, buf, len);
1251     p->found_vop = 0;
1252 
1253     while (gb->bytes_left_) {
1254         RK_U32 val = 0;
1255 
1256         READ_BITS(gb, 8, &val);
1257         startcode = (startcode << 8) | val;
1258 
1259         if ((startcode & 0xFFFFFF00) != 0x100)
1260             continue;
1261 
1262         mpg4d_dbg_bit("found startcode at byte %d\n", gb->used_bits >> 3);
1263 
1264         if (mpg4d_debug & MPG4D_DBG_STARTCODE) {
1265             mpp_log_f("start code %03x\n", startcode);
1266             if (startcode <= 0x11F)
1267                 mpp_log_f("Video Object Start");
1268             else if (startcode <= 0x12F)
1269                 mpp_log_f("Video Object Layer Start");
1270             else if (startcode <= 0x13F)
1271                 mpp_log_f("Reserved");
1272             else if (startcode <= 0x15F)
1273                 mpp_log_f("FGS bp start");
1274             else if (startcode <= 0x1AF)
1275                 mpp_log_f("Reserved");
1276             else if (startcode == 0x1B0)
1277                 mpp_log_f("Visual Object Seq Start");
1278             else if (startcode == 0x1B1)
1279                 mpp_log_f("Visual Object Seq End");
1280             else if (startcode == 0x1B2)
1281                 mpp_log_f("User Data");
1282             else if (startcode == 0x1B3)
1283                 mpp_log_f("Group of VOP start");
1284             else if (startcode == 0x1B4)
1285                 mpp_log_f("Video Session Error");
1286             else if (startcode == 0x1B5)
1287                 mpp_log_f("Visual Object Start");
1288             else if (startcode == 0x1B6)
1289                 mpp_log_f("Video Object Plane start");
1290             else if (startcode == 0x1B7)
1291                 mpp_log_f("slice start");
1292             else if (startcode == 0x1B8)
1293                 mpp_log_f("extension start");
1294             else if (startcode == 0x1B9)
1295                 mpp_log_f("fgs start");
1296             else if (startcode == 0x1BA)
1297                 mpp_log_f("FBA Object start");
1298             else if (startcode == 0x1BB)
1299                 mpp_log_f("FBA Object Plane start");
1300             else if (startcode == 0x1BC)
1301                 mpp_log_f("Mesh Object start");
1302             else if (startcode == 0x1BD)
1303                 mpp_log_f("Mesh Object Plane start");
1304             else if (startcode == 0x1BE)
1305                 mpp_log_f("Still Texture Object start");
1306             else if (startcode == 0x1BF)
1307                 mpp_log_f("Texture Spatial Layer start");
1308             else if (startcode == 0x1C0)
1309                 mpp_log_f("Texture SNR Layer start");
1310             else if (startcode == 0x1C1)
1311                 mpp_log_f("Texture Tile start");
1312             else if (startcode == 0x1C2)
1313                 mpp_log_f("Texture Shape Layer start");
1314             else if (startcode == 0x1C3)
1315                 mpp_log_f("stuffing start");
1316             else if (startcode <= 0x1C5)
1317                 mpp_log_f("reserved");
1318             else if (startcode <= 0x1FF)
1319                 mpp_log_f("System start");
1320         }
1321 
1322         if (startcode >= MPG4_VOL_STARTCODE && startcode <= MPG4_VOL_STOPCODE) {
1323             ret = mpg4d_parse_vol_header(p, gb);
1324             if (!p->found_vol)
1325                 p->found_vol = (ret == MPP_OK);
1326         } else if (startcode == MPG4_USER_DATA_STARTCODE) {
1327             ret = mpg4d_parse_user_data(p, gb);
1328         } else if (startcode == MPG4_GOP_STARTCODE) {
1329             ret = mpeg4_parse_gop_header(p, gb);
1330         } else if (startcode == MPG4_VOS_STARTCODE) {
1331             ret = mpeg4_parse_profile_level(p, gb);
1332         } else if (startcode == MPG4_VOP_STARTCODE) {
1333             ret = mpeg4_parse_vop_header(p, gb);
1334             if (MPP_OK == ret) {
1335                 RK_S32 coding_type = p->hdr_curr.vop.coding_type;
1336                 mpg4d_dbg_bit("frame %d coding_type %d\n", p->frame_num, coding_type);
1337                 p->frame_num++;
1338 
1339                 if (coding_type == MPEG4_N_VOP) {
1340                     ret = MPP_OK;
1341                     mpp_align_get_bits(gb);
1342                     continue;
1343                 }
1344 
1345                 p->found_vop = p->found_i_vop;
1346             }
1347         }
1348 
1349         if (ret)
1350             goto __BITREAD_ERR;
1351 
1352         mpp_align_get_bits(gb);
1353 
1354         if (p->found_vol && p->found_vop)
1355             break;
1356     }
1357 
1358     if (p->found_vol) {
1359         mpg4d_dbg_result("found vol w %d h %d\n", p->hdr_curr.vol.width, p->hdr_curr.vol.height);
1360         p->width    = p->hdr_curr.vol.width;
1361         p->height   = p->hdr_curr.vol.height;
1362     }
1363 
1364     p->pts  = mpp_packet_get_pts(pkt);
1365     p->dts  = mpp_packet_get_dts(pkt);
1366 
1367     ret = (p->found_vol && p->found_vop) ? (MPP_OK) : (MPP_NOK);
1368 
1369 __BITREAD_ERR:
1370     mpg4d_dbg_result("found vol %d vop %d ret %d\n", p->found_vol, p->found_vop, ret);
1371 
1372     if (ret) {
1373         mpp_packet_set_pos(pkt, buf);
1374         mpp_packet_set_length(pkt, 0);
1375     } else {
1376         RK_U32 used_bytes = gb->used_bits >> 3;
1377         mpp_packet_set_pos(pkt, buf + used_bytes);
1378     }
1379 
1380     p->eos = mpp_packet_get_eos(pkt);
1381 
1382     mpg4d_dbg_func("out\n");
1383 
1384     return ret;
1385 }
1386 
mpp_mpg4_parser_setup_syntax(Mpg4dParser ctx,MppSyntax * syntax)1387 MPP_RET mpp_mpg4_parser_setup_syntax(Mpg4dParser ctx, MppSyntax *syntax)
1388 {
1389     Mpg4dParserImpl *p = (Mpg4dParserImpl *)ctx;
1390     mpeg4d_dxva2_picture_context_t *syn = p->syntax;
1391 
1392     mpg4d_dbg_func("in\n");
1393 
1394     mpg4d_fill_picture_parameters(p, &syn->pp);
1395     mpg4d_fill_quantization_matrices(p, &syn->qm);
1396 
1397     // fill bit stream parameter
1398     syn->data[2]->DataSize   = p->bit_ctx->buf_len;
1399     syn->data[2]->DataOffset = p->hdr_curr.vop.hdr_bits;
1400     syn->data[2]->pvPVPState = p->bit_ctx->buf;
1401 
1402     syntax->number = 3;
1403     syntax->data = syn->data;
1404 
1405     mpg4d_dbg_func("out\n");
1406 
1407     return MPP_OK;
1408 }
1409 
mpp_mpg4_parser_setup_hal_output(Mpg4dParser ctx,RK_S32 * output)1410 MPP_RET mpp_mpg4_parser_setup_hal_output(Mpg4dParser ctx, RK_S32 *output)
1411 {
1412     Mpg4dParserImpl *p = (Mpg4dParserImpl *)ctx;
1413     Mpg4Hdr *hdr_curr = &p->hdr_curr;
1414     RK_S32 index = -1;
1415 
1416     mpg4d_dbg_func("in\n");
1417 
1418     if (p->found_i_vop && hdr_curr->vop.coding_type != MPEG4_N_VOP) {
1419         MppBufSlots slots = p->frame_slots;
1420         RK_U32 frame_mode = MPP_FRAME_FLAG_FRAME;
1421         MppFrame frame = NULL;
1422 
1423         mpp_frame_init(&frame);
1424         mpp_frame_set_width(frame, p->width);
1425         mpp_frame_set_height(frame, p->height);
1426         mpp_frame_set_hor_stride(frame, MPP_ALIGN(p->width, 16));
1427         mpp_frame_set_ver_stride(frame, MPP_ALIGN(p->height, 16));
1428 
1429         /*
1430          * set slots information
1431          * 1. output index MUST be set
1432          * 2. get unused index for output if needed
1433          * 3. set output index as hal_input
1434          * 4. set frame information to output index
1435          * 5. if one frame can be display, it SHOULD be enqueued to display queue
1436          */
1437         mpp_buf_slot_get_unused(slots, &index);
1438         mpp_buf_slot_set_flag(slots, index, SLOT_HAL_OUTPUT);
1439         mpp_frame_set_pts(frame, p->pts);
1440         mpp_frame_set_dts(frame, p->dts);
1441 
1442         if (hdr_curr->vol.interlacing) {
1443             frame_mode = MPP_FRAME_FLAG_PAIRED_FIELD;
1444             if (hdr_curr->vop.top_field_first)
1445                 frame_mode |= MPP_FRAME_FLAG_TOP_FIRST;
1446             else
1447                 frame_mode |= MPP_FRAME_FLAG_BOT_FIRST;
1448         }
1449 
1450         if ((p->dec_cfg->base.enable_vproc & MPP_VPROC_MODE_DETECTION) &&
1451             frame_mode == MPP_FRAME_FLAG_FRAME)
1452             frame_mode = MPP_FRAME_FLAG_DEINTERLACED;
1453 
1454         mpp_frame_set_mode(frame, frame_mode);
1455 
1456         mpp_buf_slot_set_prop(slots, index, SLOT_FRAME, frame);
1457         mpp_frame_deinit(&frame);
1458         mpp_assert(NULL == frame);
1459 
1460         hdr_curr->slot_idx = index;
1461     }
1462 
1463     p->output = index;
1464     *output = index;
1465 
1466     mpg4d_dbg_func("out\n");
1467 
1468     return MPP_OK;
1469 }
1470 
mpp_mpg4_parser_setup_refer(Mpg4dParser ctx,RK_S32 * refer,RK_S32 max_ref)1471 MPP_RET mpp_mpg4_parser_setup_refer(Mpg4dParser ctx, RK_S32 *refer, RK_S32 max_ref)
1472 {
1473     Mpg4dParserImpl *p = (Mpg4dParserImpl *)ctx;
1474     Mpg4Hdr *hdr_curr = &p->hdr_curr;
1475     MppBufSlots slots = p->frame_slots;
1476     RK_S32 index;
1477 
1478     mpg4d_dbg_func("in\n");
1479 
1480     memset(refer, -1, sizeof(max_ref * sizeof(*refer)));
1481     if (hdr_curr->vop.coding_type != MPEG4_N_VOP) {
1482         index = p->hdr_ref0.slot_idx;
1483         if (index >= 0) {
1484             mpp_buf_slot_set_flag(slots, index, SLOT_HAL_INPUT);
1485             refer[0] = index;
1486         }
1487         index = p->hdr_ref1.slot_idx;
1488         if (index >= 0) {
1489             mpp_buf_slot_set_flag(slots, index, SLOT_HAL_INPUT);
1490             refer[1] = index;
1491         }
1492     }
1493 
1494     mpg4d_dbg_func("out\n");
1495 
1496     return MPP_OK;
1497 }
1498 
mpp_mpg4_parser_update_dpb(Mpg4dParser ctx)1499 MPP_RET mpp_mpg4_parser_update_dpb(Mpg4dParser ctx)
1500 {
1501     Mpg4dParserImpl *p = (Mpg4dParserImpl *)ctx;
1502     MppBufSlots slots = p->frame_slots;
1503     Mpg4Hdr *hdr_curr = &p->hdr_curr;
1504     Mpg4Hdr *hdr_ref0 = &p->hdr_ref0;
1505     Mpg4Hdr *hdr_ref1 = &p->hdr_ref1;
1506     RK_S32 coding_type = hdr_curr->vop.coding_type;
1507     RK_S32 index = p->output;
1508 
1509     mpg4d_dbg_func("in\n");
1510 
1511     // update pts increacement
1512     if (p->pts != p->last_pts)
1513         p->pts_inc = p->pts - p->last_pts;
1514 
1515     switch (coding_type) {
1516     case MPEG4_B_VOP : {
1517         mpp_assert((hdr_ref0->slot_idx >= 0) && (hdr_ref1->slot_idx >= 0));
1518         // B frame -> index current frame
1519         // output current frame and do not change ref0 and ref1
1520         index = hdr_curr->slot_idx;
1521         mpp_buf_slot_set_flag(slots, index, SLOT_QUEUE_USE);
1522         mpp_buf_slot_enqueue(slots, index, QUEUE_DISPLAY);
1523     }
1524     /*
1525      * NOTE: here fallback to N vop - do nothing
1526      */
1527     case MPEG4_INVALID_VOP :
1528     case MPEG4_N_VOP : {
1529     } break;
1530     case MPEG4_I_VOP :
1531     case MPEG4_P_VOP :
1532     case MPEG4_S_VOP :
1533         // the other case -> index reference 0
1534         index = hdr_ref0->slot_idx;
1535         if (!hdr_ref0->enqueued && index >= 0) {
1536             mpp_buf_slot_set_flag(slots, index, SLOT_QUEUE_USE);
1537             mpp_buf_slot_enqueue(slots, index, QUEUE_DISPLAY);
1538         }
1539         // non B frame send this frame to reference queue
1540         mpp_buf_slot_set_flag(slots, hdr_curr->slot_idx, SLOT_CODEC_USE);
1541 
1542         // release ref1
1543         index = hdr_ref1->slot_idx;
1544         if (index >= 0)
1545             mpp_buf_slot_clr_flag(slots, index, SLOT_CODEC_USE);
1546 
1547         // swap ref0 to ref1, current to ref0
1548         *hdr_ref1 = *hdr_ref0;
1549         *hdr_ref0 = *hdr_curr;
1550         hdr_curr->pts       = 0;
1551     }
1552 
1553     init_mpg4_hdr_vop(hdr_curr);
1554     hdr_curr->slot_idx = -1;
1555     p->last_pts = p->pts;
1556 
1557     mpg4d_dbg_func("out\n");
1558 
1559     return MPP_OK;
1560 }
1561 
1562 
1563 
1564