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