xref: /rockchip-linux_mpp/mpp/codec/dec/m2v/m2vd_parser.c (revision 437bfbeb9567cca9cd9080e3f6954aa9d6a94f18)
1 /*
2  * Copyright 2015 Rockchip Electronics Co. LTD
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define MODULE_TAG "m2vd_parser"
18 
19 #include <string.h>
20 #include <math.h>
21 
22 #include "mpp_env.h"
23 #include "mpp_debug.h"
24 #include "mpp_packet_impl.h"
25 
26 #include "m2vd_parser.h"
27 #include "m2vd_codec.h"
28 
29 #define VPU_BITSTREAM_START_CODE    (0x42564b52)  /* RKVB, rockchip video bitstream */
30 
31 /* ignore unofficial frame poriod */
32 #define MIN_FRAME_PERIOD_27M        449000  /* more than 60 fps a little*/
33 #define MAX_FRAME_PERIOD_27M        1130000  /* less than 23.976 fps a little */
34 #define MAX_FRAME_PERIOD_DIFF       13500   /* 500us */
35 
36 RK_U32 m2vd_debug = 0x0;
37 
38 static RK_U8 scanOrder[2][64] = {
39     {   /* zig-zag */
40         0, 1, 8, 16, 9, 2, 3, 10,
41         17, 24, 32, 25, 18, 11, 4, 5,
42         12, 19, 26, 33, 40, 48, 41, 34,
43         27, 20, 13, 6, 7, 14, 21, 28,
44         35, 42, 49, 56, 57, 50, 43, 36,
45         29, 22, 15, 23, 30, 37, 44, 51,
46         58, 59, 52, 45, 38, 31, 39, 46,
47         53, 60, 61, 54, 47, 55, 62, 63
48     },
49 
50     {   /* Alternate */
51         0, 8, 16, 24, 1, 9, 2, 10,
52         17, 25, 32, 40, 48, 56, 57, 49,
53         41, 33, 26, 18, 3, 11, 4, 12,
54         19, 27, 34, 42, 50, 58, 35, 43,
55         51, 59, 20, 28, 5, 13, 6, 14,
56         21, 29, 36, 44, 52, 60, 37, 45,
57         53, 61, 22, 30, 7, 15, 23, 31,
58         38, 46, 54, 62, 39, 47, 55, 63
59     }
60 };
61 
62 static RK_U8 intraDefaultQMatrix[64] = {
63     8, 16, 19, 22, 26, 27, 29, 34,
64     16, 16, 22, 24, 27, 29, 34, 37,
65     19, 22, 26, 27, 29, 34, 34, 38,
66     22, 22, 26, 27, 29, 34, 37, 40,
67     22, 26, 27, 29, 32, 35, 40, 48,
68     26, 27, 29, 32, 35, 40, 48, 58,
69     26, 27, 29, 34, 38, 46, 56, 69,
70     27, 29, 35, 38, 46, 56, 69, 83
71 };
72 
73 static int frame_period_Table_27M[16] = {
74     1,
75     1126125, /* 23.976 fps */
76     1125000, /* 24 fps */
77     1080000, /* 25 fps */
78     900900,  /* 29.97 fps */
79     900000,  /* 30 fps */
80     540000,  /* 50 fps */
81     450450,  /* 59.94 fps */
82     450000,  /* 60 fps */
83     1800000,  /* unofficial: xing 15 fps */
84     /* unofficial: libmpeg3 "Unofficial economy rates" 5/10/12/15 fps */
85     5400000,
86     2700000,
87     2250000,
88     1800000,
89     1,
90     1
91 };
92 
93 static const MppFrameRational mpeg2_aspect[16] = {
94     {0, 1},
95     {1, 1},
96     {4, 3},
97     {16, 9},
98     {221, 100},
99     {0, 1},
100     {0, 1},
101     {0, 1},
102     {0, 1},
103     {0, 1},
104     {0, 1},
105     {0, 1},
106     {0, 1},
107     {0, 1},
108     {0, 1},
109     {0, 1},
110 };
111 
m2vd_get_readbits(BitReadCtx_t * bx)112 static inline RK_S32 m2vd_get_readbits(BitReadCtx_t *bx)
113 {
114     return bx->used_bits;
115 }
116 
m2vd_get_leftbits(BitReadCtx_t * bx)117 static inline RK_S32 m2vd_get_leftbits(BitReadCtx_t *bx)
118 {
119     return  (bx->bytes_left_ * 8 + bx->num_remaining_bits_in_curr_byte_);
120 }
121 
m2vd_read_bits(BitReadCtx_t * bx,RK_U32 bits)122 static RK_S32 m2vd_read_bits(BitReadCtx_t *bx, RK_U32 bits)
123 {
124     RK_S32 ret = 0;
125     if (bits < 32)
126         mpp_read_bits(bx, bits, &ret);
127     else
128         mpp_read_longbits(bx, bits, (RK_U32 *)&ret);
129     return ret;
130 }
131 
m2vd_show_bits(BitReadCtx_t * bx,RK_U32 bits)132 static RK_S32 m2vd_show_bits(BitReadCtx_t *bx, RK_U32 bits)
133 {
134     RK_S32 ret = 0;
135     mpp_show_bits(bx, bits, &ret);
136     return ret;
137 }
138 
m2vd_parser_init_ctx(M2VDParserContext * ctx,ParserCfg * cfg)139 static MPP_RET m2vd_parser_init_ctx(M2VDParserContext *ctx, ParserCfg *cfg)
140 {
141     MPP_RET ret = MPP_OK;
142     RK_S32 i = 0;
143     m2vd_dbg_func("FUN_I");
144 
145     M2VD_CHK_I(ctx);
146     memset(ctx, 0, sizeof(*ctx));
147 
148     ctx->cfg = cfg->cfg;
149     ctx->dxva_ctx = mpp_calloc(M2VDDxvaParam, 1);
150     ctx->bitread_ctx = mpp_calloc(BitReadCtx_t, 1);
151 
152     ctx->packet_slots = cfg->packet_slots;
153     ctx->frame_slots = cfg->frame_slots;
154 
155     mpp_buf_slot_setup(ctx->frame_slots, 16);
156 
157     ctx->initFlag = 0;
158     ctx->info_changed = 0;
159 
160     /* copy from mpeg2decoder::mpeg2decoder */
161     memset(&ctx->Framehead, 0, 3 * sizeof(M2VDFrameHead));
162 
163     ctx->frame_ref0 = &ctx->Framehead[0];
164     ctx->frame_ref1 = &ctx->Framehead[1];
165     ctx->frame_cur = &ctx->Framehead[2];
166     for (i = 0; i < 3; i++) {
167         mpp_frame_init(&ctx->Framehead[i].f);
168         if (!ctx->Framehead[i].f) {
169             mpp_err("Failed to allocate frame buffer %d\n", i);
170             return MPP_ERR_NOMEM;
171         }
172         ctx->Framehead[i].picCodingType = 0xffffffff;
173         ctx->Framehead[i].slot_index = -1;
174     }
175 #if M2VD_SKIP_ERROR_FRAME_EN
176     ctx->mHwDecStatus = MPP_OK;
177 #endif
178     ctx->resetFlag = 0;
179     ctx->flush_dpb_eos = 0;
180     ctx->pic_head.pre_temporal_reference = 0;
181     ctx->pic_head.pre_picture_coding_type = 0;
182     ctx->pic_head.picture_coding_type = 0;
183 
184     /* copy form mpeg2decoder::decoder_init */
185     //----------------------------------------------------
186     ctx->bitstream_sw_buf = mpp_calloc(RK_U8, M2VD_BUF_SIZE_BITMEM);
187     mpp_packet_init(&ctx->input_packet, ctx->bitstream_sw_buf, M2VD_BUF_SIZE_BITMEM);
188 
189     ctx->qp_tab_sw_buf = mpp_calloc(RK_U8, M2VD_BUF_SIZE_QPTAB);
190     ctx->seq_head.pIntra_table = ctx->qp_tab_sw_buf;
191     ctx->seq_head.pInter_table = ctx->seq_head.pIntra_table + 64;
192 
193     ctx->MPEG2_Flag = 0;
194     ctx->seq_ext_head.progressive_sequence = 1;
195     ctx->pic_code_ext_head.progressive_frame = 1;
196     ctx->pic_code_ext_head.picture_structure = M2VD_PIC_STRUCT_FRAME;
197     ctx->pic_head.pre_picture_coding_type = M2VD_CODING_TYPE_D;
198     ctx->top_first_cnt = 0;
199     ctx->bottom_first_cnt = 0;
200     ctx->pic_code_ext_head.frame_pred_frame_dct = 1;
201     ctx->seq_ext_head.chroma_format = 1;
202     ctx->seq_disp_ext_head.matrix_coefficients = 5;
203     ctx->pre_pts_27M = 0;
204     ctx->maxFrame_inGOP = 0;
205     ctx->preframe_period = 0;
206     ctx->mHeaderDecFlag = 0;
207     ctx->mExtraHeaderDecFlag = 0;
208     ctx->max_stream_size = M2VD_BUF_SIZE_BITMEM;
209     ctx->ref_frame_cnt = 0;
210     ctx->left_length = 0;
211     ctx->vop_header_found = 0;
212 
213     if (M2VD_DBG_DUMP_REG & m2vd_debug) {
214         RK_S32 k = 0;
215         for (k = 0; k < M2VD_DBG_FILE_NUM; k++)
216             ctx->fp_dbg_file[k] = NULL;
217 
218         ctx->fp_dbg_file[0] = fopen("/sdcard/m2vd_dbg_stream.txt", "wb");
219         if (!ctx->fp_dbg_file[0])
220             mpp_log("open file failed: %s", "/sdcard/m2vd_dbg_stream.txt");
221 
222         ctx->fp_dbg_yuv = fopen("/sdcard/m2vd_dbg_yuv_out.txt", "wb");
223         if (!ctx->fp_dbg_yuv)
224             mpp_log("open file failed: %s", "/sdcard/m2vd_dbg_yuv_out.txt");
225     } else {
226         RK_S32 k = 0;
227         for (k = 0; k < M2VD_DBG_FILE_NUM; k++)
228             ctx->fp_dbg_file[k] = NULL;
229     }
230 
231 
232     m2vd_dbg_func("FUN_O");
233 __FAILED:
234 
235     return ret;
236 }
237 
m2vd_parser_init(void * ctx,ParserCfg * parser_cfg)238 MPP_RET m2vd_parser_init(void *ctx, ParserCfg *parser_cfg)
239 {
240     MPP_RET ret = MPP_OK;
241     M2VDContext *c = (M2VDContext *)ctx;
242     M2VDParserContext *p = (M2VDParserContext *)c->parse_ctx;
243     m2vd_dbg_func("FUN_I");
244     if (p == NULL) {
245         M2VD_CHK_M(p = (M2VDParserContext*)mpp_calloc(M2VDParserContext, 1));
246         c->parse_ctx = p;
247     }
248 
249     M2VD_CHK_F(m2vd_parser_init_ctx(p, parser_cfg));
250 
251     mpp_env_get_u32("m2vd_debug", &m2vd_debug, 0);
252 
253     m2vd_dbg_func("FUN_O");
254 __FAILED:
255     return ret;
256 }
257 
m2vd_parser_deinit(void * ctx)258 MPP_RET m2vd_parser_deinit(void *ctx)
259 {
260     RK_U32 k = 0;
261     MPP_RET ret = MPP_OK;
262     M2VDContext *c = (M2VDContext *)ctx;
263     M2VDParserContext *p = (M2VDParserContext *)c->parse_ctx;
264 
265     m2vd_dbg_func("FUN_I");
266 
267     for (k = 0; k < M2VD_DBG_FILE_NUM; k++) {
268         M2VD_FCLOSE(p->fp_dbg_file[k]);
269     }
270     M2VD_FCLOSE(p->fp_dbg_yuv);
271 
272     if (p->bitstream_sw_buf) {
273         mpp_free(p->bitstream_sw_buf);
274         p->bitstream_sw_buf = NULL;
275     }
276     if (p->qp_tab_sw_buf) {
277         mpp_free(p->qp_tab_sw_buf);
278         p->qp_tab_sw_buf = NULL;
279     }
280 
281     if (p->input_packet) {
282         mpp_packet_deinit(&p->input_packet);
283     }
284 
285     if (p->dxva_ctx) {
286         mpp_free(p->dxva_ctx);
287         p->dxva_ctx = NULL;
288     }
289     if (p->bitread_ctx) {
290         mpp_free(p->bitread_ctx);
291         p->bitread_ctx = NULL;
292     }
293 
294     for (k = 0; k < 3; k++) {
295         mpp_frame_deinit(&p->Framehead[k].f);
296     }
297 
298     if (p) {
299         mpp_free(p);
300     }
301 
302     m2vd_dbg_func("FUN_O");
303     return ret;
304 }
305 
306 /*!
307 ***********************************************************************
308 * \brief
309 *   reset
310 ***********************************************************************
311 */
m2vd_parser_reset(void * ctx)312 MPP_RET m2vd_parser_reset(void *ctx)
313 {
314     MPP_RET ret = MPP_OK;
315     M2VDContext *c = (M2VDContext *)ctx;
316     M2VDParserContext *p = (M2VDParserContext *)c->parse_ctx;
317     m2vd_dbg_func("FUN_I");
318     if (p->frame_cur->slot_index >= 0)
319         mpp_buf_slot_clr_flag(p->frame_slots, p->frame_cur->slot_index,
320                               SLOT_CODEC_USE);
321 
322     if (p->frame_ref0->slot_index >= 0) {
323         if (p->frame_ref0->flags) {
324             mpp_buf_slot_set_flag(p->frame_slots, p->frame_ref0->slot_index,
325                                   SLOT_QUEUE_USE);
326             mpp_buf_slot_enqueue(p->frame_slots, p->frame_ref0->slot_index,
327                                  QUEUE_DISPLAY);
328             p->frame_ref0->flags = 0;
329         }
330         mpp_buf_slot_clr_flag(p->frame_slots, p->frame_ref0->slot_index,
331                               SLOT_CODEC_USE);
332     }
333 
334     if (p->frame_ref1->slot_index >= 0)
335         mpp_buf_slot_clr_flag(p->frame_slots, p->frame_ref1->slot_index,
336                               SLOT_CODEC_USE);
337 
338     if (p->input_packet) {
339         mpp_packet_clr_eos(p->input_packet);
340     }
341 
342     p->frame_cur->slot_index = -1;
343     p->frame_ref0->slot_index = -1;
344     p->frame_ref1->slot_index = -1;
345     p->ref_frame_cnt = 0;
346     p->resetFlag = 1;
347     p->eos = 0;
348     p->left_length = 0;
349     p->vop_header_found = 0;
350     m2vd_dbg_func("FUN_O");
351     return ret;
352 }
353 
354 /*!
355 ***********************************************************************
356 * \brief
357 *   flush
358 ***********************************************************************
359 */
m2vd_parser_flush(void * ctx)360 MPP_RET m2vd_parser_flush(void *ctx)
361 {
362     MPP_RET ret = MPP_OK;
363     M2VDContext *c = (M2VDContext *)ctx;
364     M2VDParserContext *p = (M2VDParserContext *)c->parse_ctx;
365     m2vd_dbg_func("FUN_I");
366 
367     if ((p->frame_ref0->slot_index >= 0) && p->frame_ref0->flags) {
368         mpp_buf_slot_set_flag(p->frame_slots, p->frame_ref0->slot_index,
369                               SLOT_QUEUE_USE);
370         mpp_buf_slot_enqueue(p->frame_slots, p->frame_ref0->slot_index,
371                              QUEUE_DISPLAY);
372         p->frame_ref0->flags = 0;
373     }
374 
375     m2vd_dbg_func("FUN_O");
376     return ret;
377 }
378 
379 /*!
380 ***********************************************************************
381 * \brief
382 *   control/perform
383 ***********************************************************************
384 */
m2vd_parser_control(void * ctx,MpiCmd cmd_type,void * param)385 MPP_RET m2vd_parser_control(void *ctx, MpiCmd cmd_type, void *param)
386 {
387     MPP_RET ret = MPP_OK;
388     m2vd_dbg_func("FUN_I");
389     (void)ctx;
390     (void)cmd_type;
391     (void)param;
392     m2vd_dbg_func("FUN_O");
393     return ret;
394 }
395 
396 /*!
397 ***********************************************************************
398 * \brief
399 *   prepare
400 ***********************************************************************
401 */
mpp_m2vd_parser_split(M2VDParserContext * ctx,MppPacket dst,MppPacket src)402 MPP_RET mpp_m2vd_parser_split(M2VDParserContext *ctx, MppPacket dst, MppPacket src)
403 {
404     MPP_RET ret = MPP_NOK;
405     M2VDParserContext *p = ctx;
406     RK_U8 *src_buf = (RK_U8 *)mpp_packet_get_pos(src);
407     RK_U32 src_len = (RK_U32)mpp_packet_get_length(src);
408     RK_U32 src_eos = mpp_packet_get_eos(src);
409     RK_U8 *dst_buf = (RK_U8 *)mpp_packet_get_data(dst);
410     RK_U32 dst_len = (RK_U32)mpp_packet_get_length(dst);
411     RK_U32 src_pos = 0;
412 
413     if (!p->vop_header_found) {
414         if ((dst_len < sizeof(p->state)) &&
415             ((p->state & 0x00FFFFFF) == 0x000001)) {
416             dst_buf[0] = 0;
417             dst_buf[1] = 0;
418             dst_buf[2] = 1;
419             dst_len = 3;
420         }
421 
422         while (src_pos < src_len) {
423             p->state = (p->state << 8) | src_buf[src_pos];
424             dst_buf[dst_len++] = src_buf[src_pos++];
425 
426             /*
427              * 0x1b3 : sequence header
428              * 0x100 : frame header
429              * we see all 0x1b3 and 0x100 as boundary
430              */
431             if (p->state == SEQUENCE_HEADER_CODE || p->state == PICTURE_START_CODE) {
432                 p->pts = mpp_packet_get_pts(src);
433                 p->dts = mpp_packet_get_dts(src);
434                 p->vop_header_found = 1;
435                 break;
436             }
437         }
438     }
439 
440     if (p->vop_header_found) {
441         while (src_pos < src_len) {
442             p->state = (p->state << 8) | src_buf[src_pos];
443             dst_buf[dst_len++] = src_buf[src_pos++];
444 
445             if (((p->state & 0x00FFFFFF) == 0x000001) && (src_pos < src_len) &&
446                 (src_buf[src_pos] == (SEQUENCE_HEADER_CODE & 0xFF) ||
447                  src_buf[src_pos] == (PICTURE_START_CODE & 0xFF))) {
448                 dst_len -= 3;
449                 p->vop_header_found = 0;
450                 ret = MPP_OK;
451                 break;
452             }
453         }
454     }
455 
456     if (src_eos && src_pos >= src_len) {
457         mpp_packet_set_eos(dst);
458         ret = MPP_OK;
459     }
460 
461     mpp_packet_set_length(dst, dst_len);
462     mpp_packet_set_pos(src, src_buf + src_pos);
463 
464     return ret;
465 }
466 
m2vd_parser_prepare(void * ctx,MppPacket pkt,HalDecTask * task)467 MPP_RET m2vd_parser_prepare(void *ctx, MppPacket pkt, HalDecTask *task)
468 {
469     M2VDContext *c = (M2VDContext *)ctx;
470     M2VDParserContext *p = (M2VDParserContext *)c->parse_ctx;
471     RK_U8 *pos = NULL;
472     size_t length = 0;
473     RK_U32 eos = 0;
474 
475     if (ctx == NULL || pkt == NULL || task == NULL) {
476         mpp_err_f("found NULL input ctx %p pkt %p task %p\n", ctx, pkt, task);
477         return MPP_ERR_NULL_PTR;
478     }
479 
480     pos = mpp_packet_get_pos(pkt);
481     length = mpp_packet_get_length(pkt);
482     eos = mpp_packet_get_eos(pkt);
483 
484     if (eos && !length && !p->left_length) {
485         task->valid = 0;
486         task->flags.eos = 1;
487         m2vd_parser_flush(ctx);
488         return MPP_OK;
489     }
490 
491     if (p->bitstream_sw_buf == NULL) {
492         mpp_err("failed to malloc task buffer for hardware with size %d\n", length);
493         return MPP_ERR_UNKNOW;
494     }
495 
496     mpp_packet_set_length(p->input_packet, p->left_length);
497 
498     size_t total_length = MPP_ALIGN(p->left_length + length, 16) + 64;
499 
500     if (total_length > p->max_stream_size) {
501         RK_U8 *dst = NULL;
502 
503         do {
504             p->max_stream_size <<= 1;
505         } while (total_length > p->max_stream_size);
506 
507         dst = mpp_malloc_size(RK_U8, p->max_stream_size);
508         mpp_assert(dst);
509 
510         if (p->left_length > 0) {
511             memcpy(dst, p->bitstream_sw_buf, p->left_length);
512         }
513         mpp_free(p->bitstream_sw_buf);
514         p->bitstream_sw_buf = dst;
515 
516         mpp_packet_set_data(p->input_packet, p->bitstream_sw_buf);
517         mpp_packet_set_size(p->input_packet, p->max_stream_size);
518     }
519 
520     if (!p->cfg->base.split_parse) {
521         RK_U32 *val = (RK_U32 *)mpp_packet_get_pos(pkt);
522         /* if input data is rk format styl skip those 32 byte */
523         RK_S32 offset = (VPU_BITSTREAM_START_CODE == val[0]) ? 32 : 0;
524         memcpy(p->bitstream_sw_buf, pos + offset, length - offset);
525 
526         mpp_packet_set_length(p->input_packet, length - offset);
527         mpp_packet_set_data(p->input_packet, p->bitstream_sw_buf);
528         mpp_packet_set_size(p->input_packet, p->max_stream_size);
529 
530         if (mpp_packet_get_eos(pkt))
531             mpp_packet_set_eos(p->input_packet);
532 
533         p->pts = mpp_packet_get_pts(pkt);
534         p->dts = mpp_packet_get_dts(pkt);
535         task->valid = 1;
536         mpp_packet_set_length(pkt, 0);
537     } else {
538         if (MPP_OK == mpp_m2vd_parser_split(p, p->input_packet, pkt)) {
539             p->left_length = 0;
540             task->valid = 1;
541         } else {
542             task->valid = 0;
543             p->left_length = mpp_packet_get_length(p->input_packet);
544         }
545     }
546 
547     if (mpp_packet_get_flag(pkt) & MPP_PACKET_FLAG_EXTRA_DATA) {
548         mpp_packet_set_extra_data(p->input_packet);
549     }
550 
551     p->eos = mpp_packet_get_eos(p->input_packet);
552     mpp_packet_set_pts(p->input_packet, p->pts);
553     mpp_packet_set_dts(p->input_packet, p->dts);
554     task->input_packet = p->input_packet;
555     task->flags.eos = p->eos;
556 
557     return MPP_OK;
558 }
559 
560 /*!
561 ***********************************************************************
562 * \brief
563 *   parser
564 ***********************************************************************
565 */
566 
m2vd_search_header(BitReadCtx_t * bx)567 static RK_U32 m2vd_search_header(BitReadCtx_t *bx)
568 {
569     mpp_align_get_bits(bx);
570     while (m2vd_show_bits(bx, 24) != 0x01) {
571         mpp_skip_bits(bx, 8);
572         if (m2vd_get_leftbits(bx) < 32) {
573             if (M2VD_DBG_SEC_HEADER & m2vd_debug) {
574                 mpp_log("[m2v]: seach_header: str.leftbit()[%d] < 32", m2vd_get_leftbits(bx));
575             }
576             return NO_MORE_STREAM;
577         }
578     }
579     return m2vd_show_bits(bx, 32);
580 }
581 
m2vd_decode_seq_ext_header(M2VDParserContext * ctx)582 static int m2vd_decode_seq_ext_header(M2VDParserContext *ctx)
583 {
584     BitReadCtx_t *bx = ctx->bitread_ctx;
585     m2vd_dbg_func("FUN_I");
586     ctx->MPEG2_Flag = 1;
587     ctx->seq_ext_head.profile_and_level_indication = m2vd_read_bits(bx, 8);
588     ctx->seq_ext_head.progressive_sequence         = m2vd_read_bits(bx, 1);
589     ctx->seq_ext_head.chroma_format                = m2vd_read_bits(bx, 2);
590     if (ctx->seq_ext_head.chroma_format != 1)
591         return M2VD_DEC_UNSURPORT;
592     ctx->seq_ext_head.horizontal_size_extension    = m2vd_read_bits(bx, 2);
593     ctx->seq_ext_head.vertical_size_extension      = m2vd_read_bits(bx, 2);
594     ctx->seq_ext_head.bit_rate_extension           = m2vd_read_bits(bx, 12);
595     mpp_skip_bits(bx, 1);
596     ctx->seq_ext_head.vbv_buffer_size_extension    = m2vd_read_bits(bx, 8);
597     ctx->seq_ext_head.low_delay                    = m2vd_read_bits(bx, 1);
598     ctx->seq_ext_head.frame_rate_extension_n       = m2vd_read_bits(bx, 2);
599     ctx->seq_ext_head.frame_rate_extension_d       = m2vd_read_bits(bx, 5);
600 
601     ctx->seq_head.bit_rate_value |= (ctx->seq_ext_head.bit_rate_extension << 18);
602     ctx->seq_head.vbv_buffer_size += (ctx->seq_ext_head.vbv_buffer_size_extension << 10);
603 
604     m2vd_dbg_func("FUN_O");
605     return  M2VD_DEC_OK;
606 }
607 
m2vd_decode_seqdisp_ext_header(M2VDParserContext * ctx)608 static int m2vd_decode_seqdisp_ext_header(M2VDParserContext *ctx)
609 {
610     BitReadCtx_t *bx = ctx->bitread_ctx;
611     ctx->seq_disp_ext_head.video_format      = m2vd_read_bits(bx, 3);
612     ctx->seq_disp_ext_head.color_description = m2vd_read_bits(bx, 1);
613 
614     if (ctx->seq_disp_ext_head.color_description) {
615         ctx->seq_disp_ext_head.color_primaries          = m2vd_read_bits(bx, 8);
616         ctx->seq_disp_ext_head.transfer_characteristics = m2vd_read_bits(bx, 8);
617         ctx->seq_disp_ext_head.matrix_coefficients      = m2vd_read_bits(bx, 8);
618     }
619 
620     m2vd_read_bits(bx, 14);
621     mpp_skip_bits(bx, 1);
622     m2vd_read_bits(bx, 14);
623     return  M2VD_DEC_OK;
624 }
625 
m2vd_decode_matrix_ext_header(M2VDParserContext * ctx)626 static int m2vd_decode_matrix_ext_header(M2VDParserContext *ctx)
627 {
628     RK_U32  load_intra_quantizer_matrix;
629     RK_U32  load_non_intra_quantizer_matrix;
630     RK_U32  load_chroma_intra_quantizer_matrix;
631     RK_U32  load_chroma_non_intra_quantizer_matrix;
632     RK_U32  i;
633     BitReadCtx_t *bx = ctx->bitread_ctx;
634 
635     load_intra_quantizer_matrix = m2vd_read_bits(bx, 1);
636     if (load_intra_quantizer_matrix) {
637         for (i = 0; i < 64; i++)
638             ctx->seq_head.pIntra_table[scanOrder[0][i]] = (unsigned char)m2vd_read_bits(bx, 8);
639 
640     }
641     load_non_intra_quantizer_matrix = m2vd_read_bits(bx, 1);
642     if (load_non_intra_quantizer_matrix) {
643         for (i = 0; i < 64; i++)
644             ctx->seq_head.pInter_table[scanOrder[0][i]] = (unsigned char)m2vd_read_bits(bx, 8);
645     }
646     load_chroma_intra_quantizer_matrix = m2vd_read_bits(bx, 1);
647     if (load_chroma_intra_quantizer_matrix) {
648         return M2VD_DEC_UNSURPORT;
649     }
650     load_chroma_non_intra_quantizer_matrix = m2vd_read_bits(bx, 1);
651     if (load_chroma_non_intra_quantizer_matrix) {
652         return M2VD_DEC_UNSURPORT;
653     }
654     return  M2VD_DEC_OK;
655 }
656 
m2vd_decode_scalable_ext_header(M2VDParserContext * ctx)657 static int m2vd_decode_scalable_ext_header(M2VDParserContext *ctx)
658 {
659     /* ISO/IEC 13818-2 section 6.2.2.5: sequence_scalable_extension() header */
660     int layer_id;
661     int lower_layer_prediction_horizontal_size;
662     int lower_layer_prediction_vertical_size;
663     int horizontal_subsampling_factor_m;
664     int horizontal_subsampling_factor_n;
665     int vertical_subsampling_factor_m;
666     int vertical_subsampling_factor_n;
667     int scalable_mode;
668     BitReadCtx_t *bx = ctx->bitread_ctx;
669 
670     /* scalable_mode */
671 #define SC_NONE 0
672 #define SC_DP   1
673 #define SC_SPAT 2
674 #define SC_SNR  3
675 #define SC_TEMP 4
676 
677     scalable_mode = m2vd_read_bits(bx, 2) + 1; /* add 1 to make SC_DP != SC_NONE */
678 
679     layer_id = m2vd_read_bits(bx, 4);
680 
681     if (scalable_mode == SC_SPAT) {
682         lower_layer_prediction_horizontal_size = m2vd_read_bits(bx, 14);
683         mpp_skip_bits(bx, 1);
684         lower_layer_prediction_vertical_size   = m2vd_read_bits(bx, 14);
685         horizontal_subsampling_factor_m        = m2vd_read_bits(bx, 5);
686         horizontal_subsampling_factor_n        = m2vd_read_bits(bx, 5);
687         vertical_subsampling_factor_m          = m2vd_read_bits(bx, 5);
688         vertical_subsampling_factor_n          = m2vd_read_bits(bx, 5);
689     }
690 
691     (void)layer_id;
692     (void)lower_layer_prediction_horizontal_size;
693     (void)lower_layer_prediction_vertical_size;
694     (void)horizontal_subsampling_factor_m;
695     (void)horizontal_subsampling_factor_n;
696     (void)vertical_subsampling_factor_m;
697     (void)vertical_subsampling_factor_n;
698 
699 
700     if (scalable_mode == SC_TEMP)
701         return M2VD_DEC_UNSURPORT;
702     else
703         return  M2VD_DEC_OK;
704 }
705 
m2vd_decode_picdisp_ext_header(M2VDParserContext * ctx)706 static int m2vd_decode_picdisp_ext_header(M2VDParserContext *ctx)
707 {
708     int i;
709     int number_of_frame_center_offsets;
710     BitReadCtx_t *bx = ctx->bitread_ctx;
711     /* based on ISO/IEC 13818-2 section 6.3.12
712     (November 1994) Picture display extensions */
713 
714     /* derive number_of_frame_center_offsets */
715     if (ctx->seq_ext_head.progressive_sequence) {
716         if (ctx->pic_code_ext_head.repeat_first_field) {
717             if (ctx->pic_code_ext_head.top_field_first)
718                 number_of_frame_center_offsets = 3;
719             else
720                 number_of_frame_center_offsets = 2;
721         } else {
722             number_of_frame_center_offsets = 1;
723         }
724     } else {
725         if (ctx->pic_code_ext_head.picture_structure != M2VD_PIC_STRUCT_FRAME) {
726             number_of_frame_center_offsets = 1;
727         } else {
728             if (ctx->pic_code_ext_head.repeat_first_field)
729                 number_of_frame_center_offsets = 3;
730             else
731                 number_of_frame_center_offsets = 2;
732         }
733     }
734 
735     /* now parse */
736     for (i = 0; i < number_of_frame_center_offsets; i++) {
737         ctx->pic_disp_ext_head.frame_center_horizontal_offset[i] = m2vd_read_bits(bx, 16);
738         mpp_skip_bits(bx, 1);
739 
740         ctx->pic_disp_ext_head.frame_center_vertical_offset[i]   = m2vd_read_bits(bx, 16);
741         mpp_skip_bits(bx, 1);
742     }
743     return  M2VD_DEC_OK;
744 }
745 
m2vd_decode_spatial_ext_header(M2VDParserContext * ctx)746 static int m2vd_decode_spatial_ext_header(M2VDParserContext *ctx)
747 {
748     /* ISO/IEC 13818-2 section 6.2.3.5: picture_spatial_scalable_extension() header */
749     int lower_layer_temporal_reference;
750     int lower_layer_horizontal_offset;
751     int lower_layer_vertical_offset;
752     int spatial_temporal_weight_code_table_index;
753     int lower_layer_progressive_frame;
754     int lower_layer_deinterlaced_field_select;
755     BitReadCtx_t *bx = ctx->bitread_ctx;
756 
757     lower_layer_temporal_reference = m2vd_read_bits(bx, 10);
758     mpp_skip_bits(bx, 1);
759     lower_layer_horizontal_offset = m2vd_read_bits(bx, 15);
760     if (lower_layer_horizontal_offset >= 16384)
761         lower_layer_horizontal_offset -= 32768;
762     mpp_skip_bits(bx, 1);
763     lower_layer_vertical_offset = m2vd_read_bits(bx, 15);
764     if (lower_layer_vertical_offset >= 16384)
765         lower_layer_vertical_offset -= 32768;
766     spatial_temporal_weight_code_table_index = m2vd_read_bits(bx, 2);
767     lower_layer_progressive_frame = m2vd_read_bits(bx, 1);
768     lower_layer_deinterlaced_field_select = m2vd_read_bits(bx, 1);
769 
770     (void)lower_layer_temporal_reference;
771     (void)lower_layer_vertical_offset;
772     (void)spatial_temporal_weight_code_table_index;
773     (void)lower_layer_progressive_frame;
774     (void)lower_layer_deinterlaced_field_select;
775 
776     return  M2VD_DEC_OK;
777 }
778 
m2vd_decode_pic_ext_header(M2VDParserContext * ctx)779 static int m2vd_decode_pic_ext_header(M2VDParserContext *ctx)
780 {
781     BitReadCtx_t *bx = ctx->bitread_ctx;
782     ctx->pic_code_ext_head.f_code[0][0] = m2vd_read_bits(bx, 4);
783     ctx->pic_code_ext_head.f_code[0][1] = m2vd_read_bits(bx, 4);
784     ctx->pic_code_ext_head.f_code[1][0] = m2vd_read_bits(bx, 4);
785     ctx->pic_code_ext_head.f_code[1][1] = m2vd_read_bits(bx, 4);
786     if (ctx->MPEG2_Flag) {
787         ctx->pic_head.full_pel_forward_vector = ctx->pic_code_ext_head.f_code[0][0];
788         ctx->pic_head.forward_f_code = ctx->pic_code_ext_head.f_code[0][1];
789         ctx->pic_head.full_pel_backward_vector = ctx->pic_code_ext_head.f_code[1][0];
790         ctx->pic_head.backward_f_code = ctx->pic_code_ext_head.f_code[1][1];
791     }
792 
793     ctx->pic_code_ext_head.intra_dc_precision         = m2vd_read_bits(bx, 2);
794     ctx->pic_code_ext_head.picture_structure          = m2vd_read_bits(bx, 2);
795     if (ctx->pic_code_ext_head.picture_structure == M2VD_PIC_STRUCT_FRAME)
796         ctx->pic_code_ext_head.top_field_first            = m2vd_read_bits(bx, 1);
797     else {
798         m2vd_read_bits(bx, 1);
799         if ((ctx->pic_head.pre_picture_coding_type != ctx->pic_head.picture_coding_type) &&
800             (ctx->pic_head.pre_picture_coding_type != M2VD_CODING_TYPE_I)) {
801             if (ctx->pic_code_ext_head.picture_structure == M2VD_PIC_STRUCT_TOP_FIELD)
802                 ctx->top_first_cnt++;
803             else
804                 ctx->bottom_first_cnt++;
805         }
806         if (ctx->top_first_cnt >= ctx->bottom_first_cnt)
807             ctx->pic_code_ext_head.top_field_first = 1;
808         else
809             ctx->pic_code_ext_head.top_field_first = 0;
810     }
811     ctx->pic_code_ext_head.frame_pred_frame_dct       = m2vd_read_bits(bx, 1);
812     ctx->pic_code_ext_head.concealment_motion_vectors = m2vd_read_bits(bx, 1);
813     ctx->pic_code_ext_head.q_scale_type           = m2vd_read_bits(bx, 1);
814     ctx->pic_code_ext_head.intra_vlc_format           = m2vd_read_bits(bx, 1);
815     ctx->pic_code_ext_head.alternate_scan         = m2vd_read_bits(bx, 1);
816     ctx->pic_code_ext_head.repeat_first_field         = m2vd_read_bits(bx, 1);
817     ctx->pic_code_ext_head.chroma_420_type            = m2vd_read_bits(bx, 1);
818     ctx->pic_code_ext_head.progressive_frame          = m2vd_read_bits(bx, 1);
819     ctx->pic_code_ext_head.composite_display_flag     = m2vd_read_bits(bx, 1);
820     if (ctx->pic_code_ext_head.composite_display_flag) {
821         ctx->pic_code_ext_head.v_axis            = m2vd_read_bits(bx, 1);
822         ctx->pic_code_ext_head.field_sequence    = m2vd_read_bits(bx, 3);
823         ctx->pic_code_ext_head.sub_carrier       = m2vd_read_bits(bx, 1);
824         ctx->pic_code_ext_head.burst_amplitude   = m2vd_read_bits(bx, 7);
825         ctx->pic_code_ext_head.sub_carrier_phase = m2vd_read_bits(bx, 8);
826     }
827     return  M2VD_DEC_OK;
828 }
829 
m2vd_decode_temp_ext_header(void)830 static inline int m2vd_decode_temp_ext_header(void)
831 {
832     return  M2VD_DEC_UNSURPORT;
833 }
834 
m2vd_decode_copyright_ext_header(M2VDParserContext * ctx)835 static int m2vd_decode_copyright_ext_header(M2VDParserContext *ctx)
836 {
837     int copyright_flag;
838     int copyright_identifier;
839     int original_or_copy;
840     int copyright_number_1;
841     int copyright_number_2;
842     int copyright_number_3;
843     int reserved_data;
844     BitReadCtx_t *bx = ctx->bitread_ctx;
845 
846     copyright_flag =       m2vd_read_bits(bx, 1);
847     copyright_identifier = m2vd_read_bits(bx, 8);
848     original_or_copy =     m2vd_read_bits(bx, 1);
849 
850     /* reserved */
851     reserved_data = m2vd_read_bits(bx, 7);
852 
853     mpp_skip_bits(bx, 1);
854     copyright_number_1 =   m2vd_read_bits(bx, 20);
855     mpp_skip_bits(bx, 1);
856     copyright_number_2 =   m2vd_read_bits(bx, 22);
857     mpp_skip_bits(bx, 1);
858     copyright_number_3 =   m2vd_read_bits(bx, 22);
859 
860     (void)copyright_flag;
861     (void)copyright_identifier;
862     (void)original_or_copy;
863     (void)copyright_number_1;
864     (void)copyright_number_2;
865     (void)copyright_number_3;
866     (void)reserved_data;
867 
868     return  M2VD_DEC_OK;
869 }
870 
m2vd_decode_ext_header(M2VDParserContext * ctx)871 static int m2vd_decode_ext_header(M2VDParserContext *ctx)
872 {
873     RK_U32      code;
874     RK_U32      ext_ID;
875     int         result;
876     BitReadCtx_t *bx = ctx->bitread_ctx;
877 
878     code = m2vd_search_header(bx);
879 
880     if (M2VD_DBG_SEC_HEADER & m2vd_debug) {
881         mpp_log("[m2v]: decoder_ext_header : seach_header return code: %#03x", code);
882     }
883     while (code == EXTENSION_START_CODE || code == USER_DATA_START_CODE) {
884         if (code == EXTENSION_START_CODE) {
885             mpp_skip_bits(bx, 32);
886             ext_ID = m2vd_read_bits(bx, 4);
887             if (M2VD_DBG_SEC_HEADER & m2vd_debug) {
888                 mpp_log("[m2v]: decoder_ext_header : ext_ID: %d", ext_ID);
889             }
890             switch (ext_ID) {
891             case SEQUENCE_EXTENSION_ID:
892                 result = m2vd_decode_seq_ext_header(ctx);
893                 break;
894             case SEQUENCE_DISPLAY_EXTENSION_ID:
895                 result = m2vd_decode_seqdisp_ext_header(ctx);
896                 break;
897             case QUANT_MATRIX_EXTENSION_ID:
898                 result = m2vd_decode_matrix_ext_header(ctx);
899                 break;
900             case SEQUENCE_SCALABLE_EXTENSION_ID:
901                 result = m2vd_decode_scalable_ext_header(ctx);
902                 break;
903             case PICTURE_DISPLAY_EXTENSION_ID:
904                 result = m2vd_decode_picdisp_ext_header(ctx);
905                 break;
906             case PICTURE_CODING_EXTENSION_ID:
907                 result = m2vd_decode_pic_ext_header(ctx);
908                 break;
909             case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
910                 result = m2vd_decode_spatial_ext_header(ctx);
911                 break;
912             case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
913                 result = m2vd_decode_temp_ext_header();
914                 break;
915             case COPYRIGHT_EXTENSION_ID:
916                 result = m2vd_decode_copyright_ext_header(ctx);
917                 break;
918             case NO_MORE_STREAM:
919                 break;
920             default:
921                 break;
922             }
923 
924             if (M2VD_DBG_SEC_HEADER & m2vd_debug) {
925                 mpp_log("[m2v]: decoder_ext_header result: %d", result);
926             }
927             if (result)
928                 return result;
929         } else {
930             mpp_skip_bits(bx, 32);
931         }
932         code = m2vd_search_header(bx);
933     }
934 
935     return MPP_OK;
936 }
937 
m2vd_decode_gop_header(M2VDParserContext * ctx)938 static int m2vd_decode_gop_header(M2VDParserContext *ctx)
939 {
940     BitReadCtx_t *bx = ctx->bitread_ctx;
941 
942     ctx->gop_head.drop_flag   = m2vd_read_bits(bx, 1);
943     ctx->gop_head.hour        = m2vd_read_bits(bx, 5);
944     ctx->gop_head.minute      = m2vd_read_bits(bx, 6);
945 
946     mpp_skip_bits(bx, 1);
947 
948     ctx->gop_head.sec         = m2vd_read_bits(bx, 6);
949     ctx->gop_head.frame       = m2vd_read_bits(bx, 6);
950     ctx->gop_head.closed_gop  = m2vd_read_bits(bx, 1);
951     ctx->gop_head.broken_link = m2vd_read_bits(bx, 1);
952 
953     return m2vd_decode_ext_header(ctx);
954 }
955 
m2vd_decode_seq_header(M2VDParserContext * ctx)956 static int m2vd_decode_seq_header(M2VDParserContext *ctx)
957 {
958     RK_U32 i;
959     RK_S32 pre_frame_rate_code = ctx->seq_head.frame_rate_code;
960     BitReadCtx_t *bx = ctx->bitread_ctx;
961     RK_U32 width = m2vd_read_bits(bx, 12);
962     RK_U32 height = m2vd_read_bits(bx, 12);
963 
964     ctx->display_width = width;
965     ctx->display_height = height;
966 
967     height = MPP_ALIGN(height, 16);
968     width = MPP_ALIGN(width, 16);
969 
970     if (ctx->seq_head.decode_width && ctx->seq_head.decode_height) {
971         if (width != ctx->seq_head.decode_width ||
972             height != ctx->seq_head.decode_height)
973             ctx->info_changed = 1;
974     }
975 
976     ctx->seq_head.decode_width = width;
977     ctx->seq_head.decode_height = height;
978     ctx->seq_head.aspect_ratio_information = m2vd_read_bits(bx, 4);
979     ctx->seq_head.frame_rate_code = m2vd_read_bits(bx, 4);
980     if (!ctx->frame_period || pre_frame_rate_code != ctx->seq_head.frame_rate_code)
981         ctx->frame_period = frame_period_Table_27M[ctx->seq_head.frame_rate_code];
982     ctx->seq_head.bit_rate_value = m2vd_read_bits(bx, 18);
983     mpp_skip_bits(bx, 1);
984     ctx->seq_head.vbv_buffer_size = m2vd_read_bits(bx, 10);
985     ctx->seq_head.constrained_parameters_flag = m2vd_read_bits(bx, 1);
986     ctx->seq_head.load_intra_quantizer_matrix = m2vd_read_bits(bx, 1);
987     if (ctx->seq_head.load_intra_quantizer_matrix) {
988         for (i = 0; i < 64; i++)
989             ctx->seq_head.pIntra_table[scanOrder[0][i]] = (unsigned char)m2vd_read_bits(bx, 8);
990     } else {
991         for (i = 0; i < 64; i++)
992             ctx->seq_head.pIntra_table[i] = intraDefaultQMatrix[i];
993     }
994 
995     ctx->seq_head.load_non_intra_quantizer_matrix = m2vd_read_bits(bx, 1);
996     if (ctx->seq_head.load_non_intra_quantizer_matrix) {
997         for (i = 0; i < 64; i++)
998             ctx->seq_head.pInter_table[scanOrder[0][i]] = (unsigned char)m2vd_read_bits(bx, 8);
999     } else {
1000         for (i = 0; i < 64; i++)
1001             ctx->seq_head.pInter_table[i] = 16;
1002     }
1003 
1004     return m2vd_decode_ext_header(ctx);
1005 }
1006 
m2vd_extra_bit_information(M2VDParserContext * ctx)1007 static int m2vd_extra_bit_information(M2VDParserContext *ctx)
1008 {
1009     BitReadCtx_t *bx = ctx->bitread_ctx;
1010     while (m2vd_read_bits(bx, 1)) {
1011         mpp_skip_bits(bx, 8);
1012     }
1013     return M2VD_DEC_OK;
1014 }
1015 
m2vd_decode_pic_header(M2VDParserContext * ctx)1016 static int m2vd_decode_pic_header(M2VDParserContext *ctx)
1017 {
1018     BitReadCtx_t *bx = ctx->bitread_ctx;
1019     ctx->pic_head.temporal_reference  = m2vd_read_bits(bx, 10);
1020     ctx->pic_head.picture_coding_type = m2vd_read_bits(bx, 3);
1021     ctx->pic_head.vbv_delay           = m2vd_read_bits(bx, 16);
1022     if (ctx->pic_head.temporal_reference > 50) {
1023         ctx->pic_head.temporal_reference = ctx->pretemporal_reference;
1024     }
1025     //if ((maxFrame_inGOP < temporal_reference)&&(temporal_reference<60))
1026     //  maxFrame_inGOP = temporal_reference;
1027     if (((RK_S32)ctx->maxFrame_inGOP < ctx->pic_head.temporal_reference) && (ctx->pic_head.temporal_reference < 60))
1028         ctx->maxFrame_inGOP = ctx->pic_head.temporal_reference;
1029 
1030 
1031     if (ctx->pic_head.picture_coding_type == M2VD_CODING_TYPE_P ||
1032         ctx->pic_head.picture_coding_type == M2VD_CODING_TYPE_B) {
1033         ctx->pic_head.full_pel_forward_vector = m2vd_read_bits(bx, 1);
1034         ctx->pic_head.forward_f_code = m2vd_read_bits(bx, 3);
1035     }
1036     if (ctx->pic_head.picture_coding_type == M2VD_CODING_TYPE_B) {
1037         ctx->pic_head.full_pel_backward_vector = m2vd_read_bits(bx, 1);
1038         ctx->pic_head.backward_f_code = m2vd_read_bits(bx, 3);
1039     }
1040 
1041     m2vd_extra_bit_information(ctx);
1042 
1043     return m2vd_decode_ext_header(ctx);
1044 }
1045 
m2vd_decode_head(M2VDParserContext * ctx)1046 static MPP_RET m2vd_decode_head(M2VDParserContext *ctx)
1047 {
1048     RK_U32      code;
1049     int         ret = 0;
1050     BitReadCtx_t *bx = ctx->bitread_ctx;
1051 
1052     m2vd_dbg_func("FUN_I");
1053     while (1) {
1054         code = m2vd_search_header(bx);
1055         if (M2VD_DBG_SEC_HEADER & m2vd_debug) {
1056             mpp_log("[m2v]: decoder_header : seach_header return code: 0x%3x", code);
1057         }
1058         if (code == NO_MORE_STREAM)
1059             return M2VD_DEC_FILE_END;
1060         code = m2vd_read_bits(bx, 32);
1061 
1062         switch (code) {
1063         case SEQUENCE_HEADER_CODE:
1064             ret = m2vd_decode_seq_header(ctx);
1065             if (!ret) {
1066                 MppPacket pkt = ctx->input_packet;
1067                 if (mpp_packet_get_flag(pkt) & MPP_PACKET_FLAG_EXTRA_DATA) {
1068                     ctx->mExtraHeaderDecFlag = 1;
1069                 } else {
1070                     ctx->mHeaderDecFlag = 1;
1071                 }
1072             }
1073             break;
1074         case GROUP_START_CODE:
1075             ret = m2vd_decode_gop_header(ctx);
1076             break;
1077         case PICTURE_START_CODE:
1078             ret = m2vd_decode_pic_header(ctx);
1079             ret = M2VD_DEC_PICHEAD_OK;
1080             break;
1081         case SEQUENCE_END_CODE:
1082             ret = M2VD_DEC_STREAM_END;
1083             break;
1084         default:
1085             if (M2VD_DBG_SEC_HEADER & m2vd_debug) {
1086                 mpp_log("code=%x,leftbit=%d", code, m2vd_get_leftbits(bx));
1087             }
1088             break;
1089         }
1090         if (ret)
1091             break;
1092     }
1093     m2vd_dbg_func("FUN_O");
1094 
1095     return ret;
1096 }
1097 
m2v_update_ref_frame(M2VDParserContext * p,RK_S32 force)1098 static MPP_RET m2v_update_ref_frame(M2VDParserContext *p, RK_S32 force)
1099 {
1100     //push frame
1101     if (force || (p->pic_code_ext_head.picture_structure == M2VD_PIC_STRUCT_FRAME) ||
1102         ((p->pic_code_ext_head.picture_structure == M2VD_PIC_STRUCT_BOTTOM_FIELD) && p->pic_code_ext_head.top_field_first ) ||
1103         ((p->pic_code_ext_head.picture_structure == M2VD_PIC_STRUCT_TOP_FIELD) && (!p->pic_code_ext_head.top_field_first))) {
1104         if (p->frame_cur->picCodingType == M2VD_CODING_TYPE_B) {
1105             mpp_buf_slot_set_flag(p->frame_slots, p->frame_cur->slot_index, SLOT_QUEUE_USE);
1106             mpp_buf_slot_enqueue(p->frame_slots, p->frame_cur->slot_index, QUEUE_DISPLAY);
1107             mpp_buf_slot_clr_flag(p->frame_slots, p->frame_cur->slot_index, SLOT_CODEC_USE);
1108             p->frame_cur->slot_index = -1;
1109         } else if (p->frame_cur->picCodingType != 0xffffffff) {
1110             M2VDFrameHead *tmpHD = NULL;
1111             p->ref_frame_cnt++;
1112             if (p->frame_ref0->slot_index >= 0) {
1113                 if (p->frame_ref0->flags) {
1114                     mpp_buf_slot_set_flag(p->frame_slots, p->frame_ref0->slot_index, SLOT_QUEUE_USE);
1115                     mpp_buf_slot_enqueue(p->frame_slots, p->frame_ref0->slot_index, QUEUE_DISPLAY);
1116                     p->frame_ref0->flags = 0;
1117                 }
1118 
1119                 if (p->info_changed) {
1120                     mpp_buf_slot_clr_flag(p->frame_slots, p->frame_ref0->slot_index, SLOT_CODEC_USE);
1121                     p->frame_ref0->slot_index = -1;
1122                     p->info_changed = 0;
1123                 }
1124             }
1125 
1126             if (p->frame_ref1->slot_index >= 0) {
1127                 mpp_buf_slot_clr_flag(p->frame_slots, p->frame_ref1->slot_index, SLOT_CODEC_USE);
1128                 p->frame_ref1->slot_index = -1;
1129             }
1130             tmpHD = p->frame_ref1;
1131             p->frame_ref1 = p->frame_ref0;
1132             p->frame_ref0 = p->frame_cur;
1133             p->frame_cur = tmpHD;
1134         }
1135     }
1136 
1137     return MPP_OK;
1138 }
1139 
m2vd_alloc_frame(M2VDParserContext * ctx)1140 static MPP_RET m2vd_alloc_frame(M2VDParserContext *ctx)
1141 {
1142     M2VDHeadPic *pic_head = &ctx->pic_head;
1143     M2VDHeadPicCodeExt *pic_code_ext_head = &ctx->pic_code_ext_head;
1144     RK_U64 pts_27M = ctx->pts;
1145 
1146     if (ctx->resetFlag && pic_head->picture_coding_type != M2VD_CODING_TYPE_I) {
1147         mpp_log("[m2v]: resetFlag[%d] && picture_coding_type[%d] != I_TYPE", ctx->resetFlag, pic_head->picture_coding_type);
1148         return MPP_NOK;
1149     }
1150 
1151     ctx->resetFlag = 0;
1152 
1153     if ((ctx->ref_frame_cnt < 2) && (pic_head->picture_coding_type == M2VD_CODING_TYPE_B)) {
1154         mpp_log("[m2v]: (ref_frame_cnt[%d] < 2) && (frame_cur->picCodingType[%d] == B_TYPE)",
1155                 ctx->ref_frame_cnt, pic_head->picture_coding_type);
1156         return MPP_NOK;
1157     }
1158 
1159     if ((pic_code_ext_head->picture_structure == M2VD_PIC_STRUCT_FRAME ) ||
1160         ((pic_code_ext_head->picture_structure == M2VD_PIC_STRUCT_TOP_FIELD) && pic_code_ext_head->top_field_first) ||
1161         ((pic_code_ext_head->picture_structure == M2VD_PIC_STRUCT_BOTTOM_FIELD) && (!pic_code_ext_head->top_field_first))) {
1162         RK_S64 frm_pts = 0;
1163         RK_U32 frametype = 0;
1164 
1165         if (ctx->pts_is_90K) {
1166             pts_27M = ctx->pts * 300;
1167         } else {
1168             pts_27M = ctx->pts * 27;
1169         }
1170 
1171         if (ctx->frame_cur->slot_index >= 0) {
1172             MppFrame frame = NULL;
1173 
1174             mpp_buf_slot_get_prop(ctx->frame_slots, ctx->frame_cur->slot_index, SLOT_FRAME_PTR, &frame);
1175             if (ctx->cfg->base.disable_error)
1176                 mpp_frame_set_discard(frame, 1);
1177             else
1178                 mpp_frame_set_errinfo(frame, 1);
1179 
1180             mpp_log("[m2v]: miss match field order at ref_frame_cnt[%d] picture_structure %d top_field_first %d\n",
1181                     ctx->ref_frame_cnt, pic_head->picture_coding_type, pic_code_ext_head->top_field_first);
1182 
1183             m2v_update_ref_frame(ctx, 1);
1184         }
1185 
1186         if (ctx->pre_pts_27M != pts_27M) {
1187             RK_S64 tmp_frame_period;
1188 
1189             if (ctx->group_frm_cnt) {
1190                 ctx->group_frm_cnt = ctx->group_frm_cnt + pic_head->temporal_reference;
1191             } else if (pic_head->temporal_reference == (RK_S32)ctx->prechange_temporal_ref)
1192                 ctx->group_frm_cnt = ctx->max_temporal_reference + 1;
1193             else if (pic_head->temporal_reference)
1194                 ctx->group_frm_cnt = pic_head->temporal_reference - ctx->prechange_temporal_ref;
1195             else
1196                 ctx->group_frm_cnt = ctx->max_temporal_reference - ctx->prechange_temporal_ref + 1;
1197 
1198             tmp_frame_period = pts_27M - ctx->pre_pts_27M;
1199 
1200             if ((pts_27M > ctx->pre_pts_27M)
1201                 && (pic_head->temporal_reference > (RK_S32)ctx->prechange_temporal_ref)) {
1202                 RK_S32 theshold_frame_period = tmp_frame_period * 2;
1203                 RK_S32 last_frame_period = ctx->preframe_period ? ctx->preframe_period : ctx->frame_period;
1204                 RK_S32 predict_frame_period =
1205                     (pic_head->temporal_reference - ctx->prechange_temporal_ref) * last_frame_period;
1206                 if (theshold_frame_period < predict_frame_period) {
1207                     // check pts is 90KHZ raw pts
1208                     RK_S64 diff_90K_frame_period = tmp_frame_period * 1000 / 90 - predict_frame_period;
1209 
1210                     if (!ctx->pts_is_90K && llabs(diff_90K_frame_period) <= 300) {
1211                         mpp_log("judge result pts is base in 90KHZ.\n");
1212                         ctx->pts_is_90K = 1;
1213                         // pre pts is 1MHZ, so need convert to 90KHZ
1214                         pts_27M = ctx->pre_pts_27M * 1000 / 90 + predict_frame_period;
1215                     } else {
1216                         pts_27M = ctx->pre_pts_27M + predict_frame_period;
1217                     }
1218                     tmp_frame_period = predict_frame_period;
1219                 }
1220             }
1221 
1222             if ((pts_27M > ctx->pre_pts_27M) && (ctx->group_frm_cnt > 0)) {
1223                 tmp_frame_period = tmp_frame_period / ctx->group_frm_cnt;
1224                 // judge frame rate changed
1225                 if ((tmp_frame_period > MIN_FRAME_PERIOD_27M)
1226                     && (tmp_frame_period < MAX_FRAME_PERIOD_27M) &&
1227                     (llabs(ctx->frame_period - tmp_frame_period) > MAX_FRAME_PERIOD_DIFF)) {
1228                     if (llabs(ctx->preframe_period - tmp_frame_period) > MAX_FRAME_PERIOD_DIFF) {
1229                         ctx->preframe_period = tmp_frame_period;
1230                     } else {
1231                         ctx->frame_period = tmp_frame_period;
1232                         ctx->preframe_period = 0;
1233                     }
1234                 } else {
1235                     ctx->preframe_period = 0;
1236                 }
1237             }
1238 
1239             ctx->group_start_time_27M = pts_27M - pic_head->temporal_reference * ctx->frame_period;
1240             if (ctx->group_start_time_27M <= -ctx->frame_period)
1241                 ctx->group_start_time_27M = 0;
1242             ctx->pre_pts_27M = pts_27M;
1243             ctx->prechange_temporal_ref = pic_head->temporal_reference;
1244             ctx->group_frm_cnt = 0;
1245         } else if ((RK_S32)ctx->pretemporal_reference > pic_head->temporal_reference + 5) {
1246             ctx->group_start_time_27M += (ctx->max_temporal_reference + 1) * ctx->frame_period;
1247             ctx->group_frm_cnt = ctx->max_temporal_reference - ctx->prechange_temporal_ref + 1;
1248             ctx->max_temporal_reference = 0;
1249         }
1250         if ((RK_S32)ctx->pretemporal_reference > pic_head->temporal_reference + 5)
1251             ctx->max_temporal_reference = 0;
1252         if (pic_head->temporal_reference > (RK_S32)ctx->max_temporal_reference)
1253             ctx->max_temporal_reference = pic_head->temporal_reference;
1254         ctx->pretemporal_reference = pic_head->temporal_reference;
1255         frm_pts = ctx->group_start_time_27M;
1256         frm_pts += pic_head->temporal_reference * ctx->frame_period;
1257         if (frm_pts < 0) {
1258             frm_pts = 0;
1259         }
1260         if (ctx->pts_is_90K) {
1261             frm_pts = frm_pts / 300;
1262         } else {
1263             frm_pts = frm_pts / 27;
1264         }
1265 
1266         /*
1267         mpp_log("picture_coding_type %d picture_structure %d top_field_first %d frame slot %d ref_frame_cnt %d pts %lld\n",
1268                 pic_head->picture_coding_type, pic_code_ext_head->picture_structure,
1269                 pic_code_ext_head->top_field_first, ctx->frame_cur->slot_index, ctx->ref_frame_cnt, frm_pts);
1270         */
1271 
1272         //base.Current_frame->ShowTime = Time;//Video_Bitsream.Slice_Time.low_part;
1273         //!< mark current slot error
1274         if (pic_head->picture_coding_type != M2VD_CODING_TYPE_I
1275             && pic_head->pre_picture_coding_type == pic_head->picture_coding_type) {
1276             if ((pic_head->temporal_reference - pic_head->pre_temporal_reference > 3)
1277                 || (pic_head->temporal_reference - pic_head->pre_temporal_reference < -3)) {
1278                 mpp_frame_set_errinfo(ctx->frame_cur->f, 1);
1279             } else {
1280                 mpp_frame_set_errinfo(ctx->frame_cur->f, 0);
1281             }
1282         }
1283         pic_head->pre_temporal_reference = pic_head->temporal_reference;
1284         pic_head->pre_picture_coding_type = pic_head->picture_coding_type;
1285         ctx->frame_cur->picCodingType = pic_head->picture_coding_type;
1286 
1287         mpp_frame_set_width(ctx->frame_cur->f, ctx->display_width);
1288         mpp_frame_set_height(ctx->frame_cur->f, ctx->display_height);
1289         mpp_frame_set_hor_stride(ctx->frame_cur->f, 0);
1290         mpp_frame_set_ver_stride(ctx->frame_cur->f, 0);
1291         mpp_frame_set_errinfo(ctx->frame_cur->f, 0);
1292         mpp_frame_set_pts(ctx->frame_cur->f, frm_pts);
1293         mpp_frame_set_dts(ctx->frame_cur->f, ctx->dts);
1294         ctx->frame_cur->flags = M2V_OUT_FLAG;
1295 
1296         if (ctx->seq_ext_head.progressive_sequence) {
1297             frametype = MPP_FRAME_FLAG_FRAME;
1298         } else {
1299             frametype = MPP_FRAME_FLAG_PAIRED_FIELD;
1300             if (pic_code_ext_head->top_field_first)
1301                 frametype |= MPP_FRAME_FLAG_TOP_FIRST;
1302             else
1303                 frametype |= MPP_FRAME_FLAG_BOT_FIRST;
1304         }
1305         if ((ctx->cfg->base.enable_vproc & MPP_VPROC_MODE_DETECTION) &&
1306             frametype == MPP_FRAME_FLAG_FRAME)
1307             frametype = MPP_FRAME_FLAG_DEINTERLACED;
1308 
1309         mpp_frame_set_mode(ctx->frame_cur->f, frametype);
1310 
1311         if (ctx->seq_head.aspect_ratio_information >= 0 && ctx->seq_head.aspect_ratio_information < 16)
1312             mpp_frame_set_sar(ctx->frame_cur->f, mpeg2_aspect[ctx->seq_head.aspect_ratio_information]);
1313 
1314         //alloc frame space
1315         mpp_buf_slot_get_unused(ctx->frame_slots, &ctx->frame_cur->slot_index);
1316         mpp_buf_slot_set_prop(ctx->frame_slots, ctx->frame_cur->slot_index, SLOT_FRAME, ctx->frame_cur->f);
1317         mpp_buf_slot_set_flag(ctx->frame_slots, ctx->frame_cur->slot_index, SLOT_CODEC_USE);
1318         mpp_buf_slot_set_flag(ctx->frame_slots, ctx->frame_cur->slot_index, SLOT_HAL_OUTPUT);
1319     } else {
1320         if (ctx->frame_cur->slot_index >= 0)
1321             mpp_buf_slot_set_flag(ctx->frame_slots, ctx->frame_cur->slot_index, SLOT_HAL_OUTPUT);
1322         else {
1323             /*
1324              * frame alloc failed cause by stream error, such as:
1325              * receive top field while top_field_first = 0,
1326              * or bottom filed while top_field_first = 1;
1327              * mark resetFlag to wait for another I frame
1328              */
1329             mpp_log("frame alloc failed, need reset\n");
1330             ctx->resetFlag = 1;
1331             return MPP_NOK;
1332         }
1333     }
1334 
1335     return MPP_OK;
1336 }
1337 
m2vd_convert_to_dxva(M2VDParserContext * p)1338 static MPP_RET m2vd_convert_to_dxva(M2VDParserContext *p)
1339 {
1340     MPP_RET ret = MPP_OK;
1341     m2vd_dbg_func("FUN_I");
1342     M2VDDxvaParam *dst = p->dxva_ctx;
1343     M2VDFrameHead *pfw = p->frame_ref1;
1344     M2VDFrameHead *pbw = p->frame_ref0;
1345     BitReadCtx_t *bx = p->bitread_ctx;
1346     RK_U32 i = 0;
1347     RK_U32 error_info = 0;
1348 
1349     RK_S32 readbits = m2vd_get_readbits(bx);
1350 
1351     dst->seq.decode_width                       = p->seq_head.decode_width;
1352     dst->seq.decode_height                      = p->seq_head.decode_height;
1353     dst->seq.aspect_ratio_information           = p->seq_head.aspect_ratio_information;
1354     dst->seq.frame_rate_code                    = p->seq_head.frame_rate_code;
1355     dst->seq.bit_rate_value                     = p->seq_head.bit_rate_value;
1356     dst->seq.vbv_buffer_size                    = p->seq_head.vbv_buffer_size;
1357     dst->seq.constrained_parameters_flag        = p->seq_head.constrained_parameters_flag;
1358     dst->seq.load_intra_quantizer_matrix        = p->seq_head.load_intra_quantizer_matrix;
1359     dst->seq.load_non_intra_quantizer_matrix    = p->seq_head.load_non_intra_quantizer_matrix;
1360 
1361     dst->seq_ext.profile_and_level_indication   = p->seq_ext_head.profile_and_level_indication;
1362     dst->seq_ext.progressive_sequence           = p->seq_ext_head.progressive_sequence;
1363     dst->seq_ext.chroma_format                  = p->seq_ext_head.chroma_format;
1364     dst->seq_ext.low_delay                      = p->seq_ext_head.low_delay;
1365     dst->seq_ext.frame_rate_extension_n         = p->seq_ext_head.frame_rate_extension_n;
1366     dst->seq_ext.frame_rate_extension_d         = p->seq_ext_head.frame_rate_extension_d;
1367 
1368 
1369     dst->gop.drop_flag           = p->gop_head.drop_flag;
1370     dst->gop.hour                = p->gop_head.hour;
1371     dst->gop.minute              = p->gop_head.minute;
1372     dst->gop.sec                 = p->gop_head.sec;
1373     dst->gop.frame               = p->gop_head.frame;
1374     dst->gop.closed_gop          = p->gop_head.closed_gop;
1375     dst->gop.broken_link         = p->gop_head.broken_link;
1376 
1377 
1378     dst->pic.temporal_reference             = p->pic_head.temporal_reference;
1379     dst->pic.picture_coding_type            = p->pic_head.picture_coding_type;
1380     dst->pic.pre_picture_coding_type        = p->pic_head.pre_picture_coding_type;
1381     dst->pic.vbv_delay                      = p->pic_head.vbv_delay;
1382     dst->pic.full_pel_forward_vector        = p->pic_head.full_pel_forward_vector;
1383     dst->pic.forward_f_code                 = p->pic_head.forward_f_code;
1384     dst->pic.full_pel_backward_vector       = p->pic_head.full_pel_backward_vector;
1385     dst->pic.backward_f_code                = p->pic_head.backward_f_code;
1386     dst->pic.pre_temporal_reference         = p->pic_head.pre_temporal_reference;
1387 
1388     dst->seq_disp_ext.video_format              = p->seq_disp_ext_head.video_format;
1389     dst->seq_disp_ext.color_description         = p->seq_disp_ext_head.color_description;
1390     dst->seq_disp_ext.color_primaries           = p->seq_disp_ext_head.color_primaries;
1391     dst->seq_disp_ext.transfer_characteristics  = p->seq_disp_ext_head.transfer_characteristics;
1392     dst->seq_disp_ext.matrix_coefficients       = p->seq_disp_ext_head.matrix_coefficients;
1393 
1394     memcpy(dst->pic_code_ext.f_code, p->pic_code_ext_head.f_code, 2 * 2 * sizeof(RK_S32));
1395     dst->pic_code_ext.intra_dc_precision            = p->pic_code_ext_head.intra_dc_precision;
1396     dst->pic_code_ext.picture_structure             = p->pic_code_ext_head.picture_structure;
1397     dst->pic_code_ext.top_field_first               = p->pic_code_ext_head.top_field_first;
1398     dst->pic_code_ext.frame_pred_frame_dct          = p->pic_code_ext_head.frame_pred_frame_dct;
1399     dst->pic_code_ext.concealment_motion_vectors    = p->pic_code_ext_head.concealment_motion_vectors;
1400     dst->pic_code_ext.q_scale_type                  = p->pic_code_ext_head.q_scale_type;
1401     dst->pic_code_ext.intra_vlc_format              = p->pic_code_ext_head.intra_vlc_format;
1402     dst->pic_code_ext.alternate_scan                = p->pic_code_ext_head.alternate_scan;
1403     dst->pic_code_ext.repeat_first_field            = p->pic_code_ext_head.repeat_first_field;
1404     dst->pic_code_ext.chroma_420_type               = p->pic_code_ext_head.chroma_420_type;
1405     dst->pic_code_ext.progressive_frame             = p->pic_code_ext_head.progressive_frame;
1406     dst->pic_code_ext.composite_display_flag        = p->pic_code_ext_head.composite_display_flag;
1407     dst->pic_code_ext.v_axis                        = p->pic_code_ext_head.v_axis;
1408     dst->pic_code_ext.field_sequence                = p->pic_code_ext_head.field_sequence;
1409     dst->pic_code_ext.sub_carrier                   = p->pic_code_ext_head.sub_carrier;
1410     dst->pic_code_ext.burst_amplitude               = p->pic_code_ext_head.burst_amplitude;
1411     dst->pic_code_ext.sub_carrier_phase             = p->pic_code_ext_head.sub_carrier_phase;
1412 
1413     memcpy(dst->pic_disp_ext.frame_center_horizontal_offset, p->pic_disp_ext_head.frame_center_horizontal_offset, 3 * sizeof(RK_S32));
1414     memcpy(dst->pic_disp_ext.frame_center_vertical_offset, p->pic_disp_ext_head.frame_center_vertical_offset, 3 * sizeof(RK_S32));
1415 
1416     dst->bitstream_length = p->frame_size - ((readbits >> 3) & (~7));
1417     dst->bitstream_offset = ((readbits >> 3) & (~7));
1418     dst->bitstream_start_bit = readbits & 0x3f;
1419     dst->qp_tab = p->qp_tab_sw_buf;
1420     dst->CurrPic.Index7Bits = p->frame_cur->slot_index;
1421     p->cur_slot_index = p->frame_cur->slot_index;
1422 
1423     if (p->frame_ref0->slot_index < 0)
1424         pbw = p->frame_cur;
1425 
1426     if (p->frame_ref1->slot_index < 0)
1427         pfw = pbw;
1428 
1429     for (i = 0; i < 4; i++) {
1430         dst->frame_refs[i].bPicEntry = 0xff;
1431     }
1432     if (p->pic_head.picture_coding_type == M2VD_CODING_TYPE_B) {
1433         MppFrame frame0 = NULL;
1434         MppFrame frame1 = NULL;
1435         dst->frame_refs[0].Index7Bits = pfw->slot_index;
1436         dst->frame_refs[1].Index7Bits = pfw->slot_index;
1437         dst->frame_refs[2].Index7Bits = pbw->slot_index;
1438         dst->frame_refs[3].Index7Bits = pbw->slot_index;
1439         mpp_buf_slot_get_prop(p->frame_slots, pfw->slot_index, SLOT_FRAME_PTR, &frame0);
1440         mpp_buf_slot_get_prop(p->frame_slots, pbw->slot_index, SLOT_FRAME_PTR, &frame1);
1441         error_info = mpp_frame_get_errinfo(frame0) | mpp_frame_get_errinfo(frame1);
1442     } else {
1443         MppFrame frame = NULL;
1444         if ((p->pic_code_ext_head.picture_structure == M2VD_PIC_STRUCT_FRAME) ||
1445             ((p->pic_code_ext_head.picture_structure == M2VD_PIC_STRUCT_TOP_FIELD) && p->pic_code_ext_head.top_field_first) ||
1446             ((p->pic_code_ext_head.picture_structure == M2VD_PIC_STRUCT_BOTTOM_FIELD) && (!p->pic_code_ext_head.top_field_first))) {
1447             dst->frame_refs[0].Index7Bits = pbw->slot_index;
1448             dst->frame_refs[1].Index7Bits = pbw->slot_index;
1449         } else if (p->pic_code_ext_head.picture_structure == M2VD_PIC_STRUCT_TOP_FIELD) {
1450             dst->frame_refs[0].Index7Bits = pbw->slot_index;
1451             dst->frame_refs[1].Index7Bits = p->frame_cur->slot_index;
1452         } else if (p->pic_code_ext_head.picture_structure == M2VD_PIC_STRUCT_BOTTOM_FIELD) {
1453             dst->frame_refs[0].Index7Bits = p->frame_cur->slot_index;
1454             dst->frame_refs[1].Index7Bits = pbw->slot_index;
1455         }
1456         dst->frame_refs[2].Index7Bits = p->frame_cur->slot_index;
1457         dst->frame_refs[3].Index7Bits = p->frame_cur->slot_index;
1458         mpp_buf_slot_get_prop(p->frame_slots, pbw->slot_index, SLOT_FRAME_PTR, &frame);
1459         error_info = mpp_frame_get_errinfo(frame);
1460     }
1461     if (p->frame_cur->picCodingType == M2VD_CODING_TYPE_I) {
1462         error_info = 0;
1463     }
1464     dst->seq_ext_head_dec_flag = p->MPEG2_Flag;
1465     {
1466         MppFrame frame = NULL;
1467         mpp_buf_slot_get_prop(p->frame_slots, p->cur_slot_index, SLOT_FRAME_PTR, &frame);
1468         mpp_frame_set_errinfo(frame, error_info);
1469     }
1470     m2vd_dbg_func("FUN_O");
1471     return ret;
1472 }
1473 
m2vd_parser_parse(void * ctx,HalDecTask * in_task)1474 MPP_RET m2vd_parser_parse(void *ctx, HalDecTask *in_task)
1475 {
1476     MPP_RET ret = MPP_OK;
1477     int rev = 0;
1478     M2VDContext *c = (M2VDContext *)ctx;
1479     M2VDParserContext *p = (M2VDParserContext *)c->parse_ctx;
1480     m2vd_dbg_func("FUN_I");
1481 
1482     M2VD_CHK_I(in_task->valid);
1483 
1484     in_task->valid = 0;
1485 
1486     p->flush_dpb_eos = 0;
1487 
1488     p->frame_size = (RK_U32)mpp_packet_get_length(in_task->input_packet);
1489 
1490     mpp_set_bitread_ctx(p->bitread_ctx, p->bitstream_sw_buf, p->frame_size);
1491 
1492     rev = m2vd_decode_head(p);
1493 
1494     if (!p->mHeaderDecFlag) {
1495         if (p->mExtraHeaderDecFlag &&
1496             p->pic_head.picture_coding_type == M2VD_CODING_TYPE_I) {
1497             p->mHeaderDecFlag = 1;
1498             if (M2VD_DBG_SEC_HEADER & m2vd_debug)
1499                 mpp_log("[m2v]: use extra data sequence header");
1500         } else {
1501             if (M2VD_DBG_SEC_HEADER & m2vd_debug)
1502                 mpp_log("[m2v]: !mHeaderDecFlag");
1503             goto __FAILED;
1504         }
1505     }
1506 
1507     if (p->seq_head.decode_width > 1920 || p->seq_head.decode_height > 1088) {
1508         mpp_err("Warning: unsupport larger than 1920x1088\n");
1509         goto __FAILED;
1510     }
1511 
1512     p->mb_width = (p->seq_head.decode_width + 15) >> 4;
1513     p->mb_height = (p->seq_head.decode_height + 15) >> 4;
1514 
1515     if (rev < M2VD_DEC_OK) {
1516         if (M2VD_DBG_SEC_HEADER & m2vd_debug) {
1517             mpp_log("decoder_header error rev %d", rev);
1518         }
1519         goto __FAILED;
1520     }
1521 
1522     if (rev == M2VD_DEC_PICHEAD_OK) {
1523         if (m2vd_alloc_frame(p)) {
1524             goto __FAILED;
1525         }
1526         m2vd_convert_to_dxva(p);
1527         in_task->syntax.data = (void *)p->dxva_ctx;
1528         in_task->syntax.number = sizeof(M2VDDxvaParam);
1529         in_task->output = p->frame_cur->slot_index;
1530         p->pic_head.pre_picture_coding_type = p->pic_head.picture_coding_type;
1531 
1532         if (p->frame_ref0->slot_index >= 0 &&
1533             (p->frame_ref0->slot_index != p->frame_cur->slot_index)) {
1534             mpp_buf_slot_set_flag(p->frame_slots, p->frame_ref0->slot_index, SLOT_HAL_INPUT);
1535             in_task->refer[0] = p->frame_ref0->slot_index;
1536         }
1537 
1538         if (p->frame_ref1->slot_index >= 0 && (p->frame_ref1->slot_index != p->frame_cur->slot_index)) {
1539             mpp_buf_slot_set_flag(p->frame_slots, p->frame_ref1->slot_index, SLOT_HAL_INPUT);
1540             in_task->refer[1] = p->frame_ref1->slot_index;
1541         }
1542 
1543         MppFrame frame = NULL;
1544         mpp_buf_slot_get_prop(p->frame_slots, p->cur_slot_index, SLOT_FRAME_PTR, &frame);
1545         mpp_frame_set_poc(frame, p->pic_head.temporal_reference);
1546 
1547         if (p->dxva_ctx->seq_disp_ext.color_description) {
1548             mpp_frame_set_color_primaries(frame, p->dxva_ctx->seq_disp_ext.color_primaries);
1549             mpp_frame_set_color_trc(frame, p->dxva_ctx->seq_disp_ext.transfer_characteristics);
1550             mpp_frame_set_colorspace(frame, p->dxva_ctx->seq_disp_ext.matrix_coefficients);
1551         } else {
1552             mpp_frame_set_color_primaries(frame, MPP_FRAME_PRI_UNSPECIFIED);
1553             mpp_frame_set_color_trc(frame, MPP_FRAME_TRC_UNSPECIFIED);
1554             mpp_frame_set_colorspace(frame, MPP_FRAME_SPC_UNSPECIFIED);
1555         }
1556 
1557         in_task->valid = 1;
1558         m2v_update_ref_frame(p, 0);
1559     }
1560 
1561 __FAILED:
1562     if (p->eos)
1563         m2vd_parser_flush(ctx);
1564 
1565     m2vd_dbg_func("FUN_O");
1566 
1567     return ret;
1568 }
1569 
m2vd_parser_callback(void * ctx,void * errinfo)1570 MPP_RET m2vd_parser_callback(void *ctx, void *errinfo)
1571 {
1572     MPP_RET ret = MPP_OK;
1573     M2VDContext *c = (M2VDContext *)ctx;
1574     M2VDParserContext *p = (M2VDParserContext *)c->parse_ctx;
1575     MppFrame frame = NULL;
1576     (void)errinfo;
1577 
1578     m2vd_dbg_func("FUN_I");
1579     mpp_buf_slot_get_prop(p->frame_slots, p->cur_slot_index, SLOT_FRAME_PTR, &frame);
1580     mpp_frame_set_errinfo(frame, 1);
1581     m2vd_dbg_func("FUN_O");
1582 
1583     return ret;
1584 }
1585