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