diff --git a/mpp/codec/dec/h264/h264d_api.c b/mpp/codec/dec/h264/h264d_api.c index 4b850100..6c023557 100644 --- a/mpp/codec/dec/h264/h264d_api.c +++ b/mpp/codec/dec/h264/h264d_api.c @@ -167,11 +167,12 @@ static MPP_RET free_cur_ctx(H264dCurCtx_t *p_Cur) FunctionIn(p_Cur->p_Dec->logctx.parr[RUN_PARSE]); if (p_Cur) { recycle_slice(&p_Cur->slice); - for (i = 0; i < 2; i++) { + for (i = 0; i < MAX_NUM_DPB_LAYERS; i++) { MPP_FREE(p_Cur->listP[i]); MPP_FREE(p_Cur->listB[i]); } - MPP_FREE(p_Cur->strm.buf); + MPP_FREE(p_Cur->strm.nalu_buf); + MPP_FREE(p_Cur->strm.head_buf); } FunctionOut(p_Cur->p_Dec->logctx.parr[RUN_PARSE]); __RETURN: @@ -181,21 +182,28 @@ static MPP_RET init_cur_ctx(H264dCurCtx_t *p_Cur) { RK_U32 i = 0; MPP_RET ret = MPP_ERR_UNKNOW; + H264dCurStream_t *p_strm = NULL; INP_CHECK(ret, ctx, !p_Cur); FunctionIn(p_Cur->p_Dec->logctx.parr[RUN_PARSE]); - p_Cur->strm.buf = mpp_malloc_size(RK_U8, NALU_BUF_MAX_SIZE); - MEM_CHECK(ret, p_Cur->strm.buf); - p_Cur->strm.max_size = NALU_BUF_MAX_SIZE; - p_Cur->strm.prefixdata[0] = 0xff; - p_Cur->strm.prefixdata[1] = 0xff; - p_Cur->strm.prefixdata[2] = 0xff; - for (i = 0; i < 2; i++) { + + p_strm = &p_Cur->strm; + p_strm->nalu_max_size = NALU_BUF_MAX_SIZE; + p_strm->nalu_buf = mpp_malloc_size(RK_U8, p_strm->nalu_max_size); + p_strm->head_max_size = HEAD_BUF_MAX_SIZE; + p_strm->head_buf = mpp_malloc_size(RK_U8, p_strm->head_max_size); + MEM_CHECK(ret, p_strm->nalu_buf && p_strm->head_buf); + + p_strm->prefixdata[0] = 0xff; + p_strm->prefixdata[1] = 0xff; + p_strm->prefixdata[2] = 0xff; + for (i = 0; i < MAX_NUM_DPB_LAYERS; i++) { p_Cur->listP[i] = mpp_malloc_size(H264_StorePic_t*, MAX_LIST_SIZE * sizeof(H264_StorePic_t*)); p_Cur->listB[i] = mpp_malloc_size(H264_StorePic_t*, MAX_LIST_SIZE * sizeof(H264_StorePic_t*)); MEM_CHECK(ret, p_Cur->listP[i] && p_Cur->listB[i]); // +1 for reordering } FunctionOut(p_Cur->p_Dec->logctx.parr[RUN_PARSE]); + __RETURN: return ret = MPP_OK; __FAILED: @@ -354,7 +362,8 @@ static MPP_RET init_dec_ctx(H264_DecCtx_t *p_Dec) p_Dec->spt_decode_mtds = MPP_DEC_BY_FRAME | MPP_DEC_BY_SLICE; p_Dec->next_state = SliceSTATE_ResetSlice; p_Dec->nalu_ret = NALU_NULL; - p_Dec->first_frame_flag = 1; + p_Dec->is_first_frame = 1; + p_Dec->is_first_frame2 = 1; __RETURN: return ret = MPP_OK; @@ -450,12 +459,31 @@ __RETURN: MPP_RET h264d_reset(void *decoder) { MPP_RET ret = MPP_ERR_UNKNOW; + H264_DecCtx_t *p_Dec = (H264_DecCtx_t *)decoder; - INP_CHECK(ret, ctx, decoder); + INP_CHECK(ret, ctx, !decoder); + FunctionIn(p_Dec->logctx.parr[RUN_PARSE]); + //!< reset input parameter + p_Dec->p_Inp->in_buf = NULL; + p_Dec->p_Inp->in_size = 0; + p_Dec->p_Inp->is_eos = 0; + p_Dec->p_Inp->in_timestamp = 0; + p_Dec->p_Inp->out_buf = NULL; + p_Dec->p_Inp->out_length = 0; + //!< reset current stream + p_Dec->p_Cur->strm.prefixdata[0] = 0xff; + p_Dec->p_Cur->strm.prefixdata[1] = 0xff; + p_Dec->p_Cur->strm.prefixdata[2] = 0xff; + //!< reset dpb + FUN_CHECK(ret = init_dpb(p_Dec->p_Vid, p_Dec->p_Vid->p_Dpb_layer[0], 1)); + FUN_CHECK(ret = init_dpb(p_Dec->p_Vid, p_Dec->p_Vid->p_Dpb_layer[1], 2)); + + FunctionOut(p_Dec->logctx.parr[RUN_PARSE]); - (void)decoder; __RETURN: return ret = MPP_OK; +__FAILED: + return ret = MPP_NOK; } /*! @@ -467,12 +495,19 @@ __RETURN: MPP_RET h264d_flush(void *decoder) { MPP_RET ret = MPP_ERR_UNKNOW; + H264_DecCtx_t *p_Dec = (H264_DecCtx_t *)decoder; - INP_CHECK(ret, ctx, decoder); + INP_CHECK(ret, ctx, !decoder); + FunctionIn(p_Dec->logctx.parr[RUN_PARSE]); - (void)decoder; + FUN_CHECK(ret = flush_dpb(p_Dec->p_Vid->p_Dpb_layer[0])); + FUN_CHECK(ret = flush_dpb(p_Dec->p_Vid->p_Dpb_layer[1])); + + FunctionOut(p_Dec->logctx.parr[RUN_PARSE]); __RETURN: return ret = MPP_OK; +__FAILED: + return ret = MPP_NOK; } /*! @@ -484,15 +519,12 @@ __RETURN: MPP_RET h264d_control(void *decoder, RK_S32 cmd_type, void *param) { MPP_RET ret = MPP_ERR_UNKNOW; - - INP_CHECK(ret, ctx, decoder); - + INP_CHECK(ret, ctx, !decoder); (void)decoder; (void)cmd_type; (void)param; - __RETURN: return ret = MPP_OK; } @@ -504,28 +536,27 @@ __RETURN: * prepare *********************************************************************** */ -MPP_RET h264d_prepare(void *decoder, MppPacket in_pkt, HalDecTask *in_task) +MPP_RET h264d_prepare(void *decoder, MppPacket pkt, HalDecTask *task) { MPP_RET ret = MPP_ERR_UNKNOW; H264_DecCtx_t *p_Dec = (H264_DecCtx_t *)decoder; - MppPacketImpl *pkt = (MppPacketImpl *)in_pkt; - - INP_CHECK(ret, ctx, !decoder && !in_pkt && !in_task); + INP_CHECK(ret, ctx, !decoder && !pkt && !task); FunctionIn(p_Dec->logctx.parr[RUN_PARSE]); - p_Dec->p_Inp->in_buf = (RK_U8 *)pkt->pos; - p_Dec->p_Inp->in_size = &pkt->size; - p_Dec->p_Inp->is_eos = pkt->flag & MPP_PACKET_FLAG_EOS; - - - - + p_Dec->p_Inp->in_buf = (RK_U8 *)mpp_packet_get_pos(pkt); + p_Dec->p_Inp->in_length = mpp_packet_get_length(pkt); + p_Dec->p_Inp->is_eos = mpp_packet_get_eos(pkt); + p_Dec->p_Inp->in_size = &((MppPacketImpl *)pkt)->length; + do { + (ret = parse_prepare(p_Dec->p_Inp, p_Dec->p_Cur)); + task->valid = p_Dec->p_Inp->task_valid; // prepare valid flag + } while (mpp_packet_get_length(pkt) && !task->valid); FunctionOut(p_Dec->logctx.parr[RUN_PARSE]); __RETURN: return ret = MPP_OK; //__FAILED: -// return ret; +// return ret; } @@ -542,10 +573,10 @@ MPP_RET h264d_parse(void *decoder, HalDecTask *in_task) INP_CHECK(ret, ctx, !decoder && !in_task); FunctionIn(p_Dec->logctx.parr[RUN_PARSE]); - + in_task->valid = 0; // prepare end flag FUN_CHECK(ret = parse_loop(p_Dec)); - if (p_Dec->parser_end_flag) { - in_task->valid = 1; + if (p_Dec->is_parser_end) { + in_task->valid = 1; // register valid flag in_task->syntax.number = p_Dec->dxva_ctx->syn.num; in_task->syntax.data = (void *)p_Dec->dxva_ctx->syn.buf; FUN_CHECK(ret = update_dpb(p_Dec)); diff --git a/mpp/codec/dec/h264/h264d_dpb.c b/mpp/codec/dec/h264/h264d_dpb.c index 115f4c8d..2bf18cf4 100644 --- a/mpp/codec/dec/h264/h264d_dpb.c +++ b/mpp/codec/dec/h264/h264d_dpb.c @@ -21,11 +21,10 @@ #include #include "mpp_mem.h" +#include "mpp_common.h" #include "h264d_log.h" #include "h264d_scalist.h" - - #include "h264d_dpb.h" @@ -114,9 +113,9 @@ static RK_S32 getDpbSize(H264dVideoCtx_t *p_Vid, H264_SPS_t *active_sps) if (p_Vid->active_mvc_sps_flag && (p_Vid->profile_idc == MVC_HIGH || p_Vid->profile_idc == STEREO_HIGH)) { num_views = p_Vid->active_subsps->num_views_minus1 + 1; - size = min(2 * size, max(1, RoundLog2(num_views)) * 16) / num_views; + size = MPP_MIN(2 * size, MPP_MAX(1, RoundLog2(num_views)) * 16) / num_views; } else { - size = min(size, 16); + size = MPP_MIN(size, 16); } return size; @@ -552,7 +551,7 @@ static void sliding_window_memory_management(H264_DpbBuf_t *p_Dpb, H264_StorePic RK_U32 i = 0; // if this is a reference pic with sliding window, unmark first ref frame - if (p_Dpb->ref_frames_in_buffer == max(1, p_Dpb->num_ref_frames) - p_Dpb->ltref_frames_in_buffer) { + if (p_Dpb->ref_frames_in_buffer == MPP_MAX(1, p_Dpb->num_ref_frames) - p_Dpb->ltref_frames_in_buffer) { for (i = 0; i < p_Dpb->used_size; i++) { if (p_Dpb->fs[i]->is_reference && (!(p_Dpb->fs[i]->is_long_term))) { unmark_for_reference(p_Dpb->fs[i]); @@ -568,8 +567,8 @@ static void check_num_ref(H264_DpbBuf_t *p_Dpb) { LogCtx_t *runlog = p_Dpb->p_Vid->p_Dec->logctx.parr[RUN_PARSE]; - if ((RK_S32)(p_Dpb->ltref_frames_in_buffer + p_Dpb->ref_frames_in_buffer) > max(1, p_Dpb->num_ref_frames)) { - if ((RK_S32)(p_Dpb->ltref_frames_in_buffer + p_Dpb->ref_frames_in_buffer) > max(1, p_Dpb->num_ref_frames)) { + if ((RK_S32)(p_Dpb->ltref_frames_in_buffer + p_Dpb->ref_frames_in_buffer) > MPP_MAX(1, p_Dpb->num_ref_frames)) { + if ((RK_S32)(p_Dpb->ltref_frames_in_buffer + p_Dpb->ref_frames_in_buffer) > MPP_MAX(1, p_Dpb->num_ref_frames)) { sliding_window_memory_management(p_Dpb, NULL); LogWarnning(runlog, "Max number of reference frames exceeded"); } @@ -753,7 +752,7 @@ static MPP_RET dpb_combine_field_yuv(H264dVideoCtx_t *p_Vid, H264_FrameStore_t * ASSERT(fs->is_used == 0x03); } } - fs->poc = fs->frame->poc = fs->frame->frame_poc = min(fs->top_field->poc, fs->bottom_field->poc); + fs->poc = fs->frame->poc = fs->frame->frame_poc = MPP_MIN(fs->top_field->poc, fs->bottom_field->poc); fs->bottom_field->frame_poc = fs->top_field->frame_poc = fs->frame->poc; fs->bottom_field->top_poc = fs->frame->top_poc = fs->top_field->poc; fs->top_field->bottom_poc = fs->frame->bottom_poc = fs->bottom_field->poc; @@ -781,7 +780,7 @@ static MPP_RET dpb_combine_field_yuv(H264dVideoCtx_t *p_Vid, H264_FrameStore_t * fs->bottom_field->bottom_field = fs->bottom_field; fs->frame->is_mmco_5 = fs->top_field->is_mmco_5 || fs->bottom_field->is_mmco_5; - fs->frame->poc_mmco5 = min(fs->top_field->top_poc_mmco5, fs->bottom_field->bot_poc_mmco5); + fs->frame->poc_mmco5 = MPP_MIN(fs->top_field->top_poc_mmco5, fs->bottom_field->bot_poc_mmco5); fs->frame->top_poc_mmco5 = fs->top_field->top_poc_mmco5; fs->frame->bot_poc_mmco5 = fs->top_field->bot_poc_mmco5; @@ -967,11 +966,11 @@ static MPP_RET adaptive_memory_management(H264_DpbBuf_t *p_Dpb, H264_StorePic_t p->is_mmco_5 = 1; p->top_poc_mmco5 = p->top_poc; p->bot_poc_mmco5 = p->bottom_poc; - p->poc_mmco5 = min(p->top_poc, p->bottom_poc); + p->poc_mmco5 = MPP_MIN(p->top_poc, p->bottom_poc); p->top_poc -= p->poc; p->bottom_poc -= p->poc; - p->poc = min(p->top_poc, p->bottom_poc); + p->poc = MPP_MIN(p->top_poc, p->bottom_poc); p->frame_poc = p->poc; break; @@ -1646,12 +1645,10 @@ MPP_RET exit_picture(H264dVideoCtx_t *p_Vid, H264_StorePic_t **dec_picture) set_curframe_poc((*dec_picture), &p_Vid->p_Dec->regs); #endif FUN_CHECK(ret = store_picture_in_dpb(p_Vid->p_Dpb_layer[(*dec_picture)->layer_id], *dec_picture)); - FPRINT(g_debug_file1, "decoder ending, g_framecnt=%d \n", p_Vid->g_framecnt); - +// FPRINT(g_debug_file0, "decoder ending, g_framecnt=%d \n", p_Vid->g_framecnt); + //FPRINT(g_debug_file1, "decoder ending, g_framecnt=%d \n", p_Vid->g_framecnt); mpp_log("decoder ending, g_framecnt=%d \n", p_Vid->g_framecnt++); - - #if 0 update_all_logctx_framenum(&p_Vid->p_Dec->logctx, p_Vid->g_framecnt); #endif @@ -1876,7 +1873,7 @@ MPP_RET prepare_init_dpb_info(H264_SLICE_t *currSlice) voidx = p_Dec->dpb_info[i].voidx; is_used = p_Dec->dpb_info[i].is_used; if (currSlice->structure == FRAME && picbuf) { - if (poc == min(TOP_POC, BOT_POC) && (layer_id == voidx)) + if (poc == MPP_MIN(TOP_POC, BOT_POC) && (layer_id == voidx)) break; } else { if (is_used == 3) { @@ -1929,7 +1926,7 @@ MPP_RET prepare_init_dpb_info(H264_SLICE_t *currSlice) voidx = p_Dec->dpb_info[i].voidx; is_used = p_Dec->dpb_info[i].is_used; if (currSlice->structure == FRAME && picbuf) { - if (poc == min(TOP_POC, BOT_POC) && (layer_id == voidx)) + if (poc == MPP_MIN(TOP_POC, BOT_POC) && (layer_id == voidx)) break; } else { if (is_used == 3) { @@ -1975,7 +1972,7 @@ MPP_RET update_dpb(H264_DecCtx_t *p_Dec) { MPP_RET ret = MPP_ERR_UNKNOW; - p_Dec->parser_end_flag = 0; + p_Dec->is_parser_end = 0; p_Dec->p_Vid->exit_picture_flag = 1; p_Dec->p_Vid->have_outpicture_flag = 1; FUN_CHECK(ret = exit_picture(p_Dec->p_Vid, &p_Dec->p_Vid->dec_picture)); diff --git a/mpp/codec/dec/h264/h264d_dpb.h b/mpp/codec/dec/h264/h264d_dpb.h index 128c2b0e..11adaf95 100644 --- a/mpp/codec/dec/h264/h264d_dpb.h +++ b/mpp/codec/dec/h264/h264d_dpb.h @@ -35,10 +35,10 @@ void free_frame_store(H264_FrameStore_t *f); MPP_RET idr_memory_management(H264_DpbBuf_t *p_Dpb, H264_StorePic_t *p); MPP_RET insert_picture_in_dpb(H264dVideoCtx_t *p_Vid, H264_FrameStore_t *fs, H264_StorePic_t *p, RK_U8 combine_flag); -MPP_RET init_dpb (H264dVideoCtx_t *p_Vid, H264_DpbBuf_t *p_Dpb, RK_S32 type); -MPP_RET flush_dpb(H264_DpbBuf_t *p_Dpb); -MPP_RET update_dpb(H264_DecCtx_t *p_Dec); -void free_dpb (H264_DpbBuf_t *p_Dpb); +MPP_RET init_dpb (H264dVideoCtx_t *p_Vid, H264_DpbBuf_t *p_Dpb, RK_S32 type); +MPP_RET flush_dpb (H264_DpbBuf_t *p_Dpb); +MPP_RET update_dpb (H264_DecCtx_t *p_Dec); +void free_dpb (H264_DpbBuf_t *p_Dpb); MPP_RET exit_picture(H264dVideoCtx_t *p_Vid, H264_StorePic_t **dec_picture); MPP_RET prepare_init_dpb_info(H264_SLICE_t *currSlice); diff --git a/mpp/codec/dec/h264/h264d_fill.c b/mpp/codec/dec/h264/h264d_fill.c index 8f7716e6..b6c1bb49 100644 --- a/mpp/codec/dec/h264/h264d_fill.c +++ b/mpp/codec/dec/h264/h264d_fill.c @@ -46,23 +46,23 @@ __FAILED: } -static MPP_RET realloc_stream_buffer(H264dDxvaCtx_t *dxva_ctx, RK_U32 stream_add) -{ - MPP_RET ret = MPP_ERR_UNKNOW; - - if (stream_add > FRAME_BUF_ADD_SIZE) { - dxva_ctx->max_strm_size += ALIGN(stream_add, 128); - } else { - dxva_ctx->max_strm_size += FRAME_BUF_ADD_SIZE; - } - dxva_ctx->bitstream = mpp_realloc(dxva_ctx->bitstream, RK_U8, dxva_ctx->max_strm_size); - MEM_CHECK (ret, dxva_ctx->bitstream); - - return ret = MPP_OK; -__FAILED: - ASSERT(0); - return ret; -} +//static MPP_RET realloc_stream_buffer(H264dDxvaCtx_t *dxva_ctx, RK_U32 stream_add) +//{ +// MPP_RET ret = MPP_ERR_UNKNOW; +// +// if (stream_add > FRAME_BUF_ADD_SIZE) { +// dxva_ctx->max_strm_size += ALIGN(stream_add, 128); +// } else { +// dxva_ctx->max_strm_size += FRAME_BUF_ADD_SIZE; +// } +// dxva_ctx->bitstream = mpp_realloc(dxva_ctx->bitstream, RK_U8, dxva_ctx->max_strm_size); +// MEM_CHECK (ret, dxva_ctx->bitstream); +// +// return ret = MPP_OK; +//__FAILED: +// ASSERT(0); +// return ret; +//} static MPP_RET fill_stream_data(H264dDxvaCtx_t *dxva_ctx, H264_Nalu_t *p_nal) { @@ -74,20 +74,24 @@ static MPP_RET fill_stream_data(H264dDxvaCtx_t *dxva_ctx, H264_Nalu_t *p_nal) if (dxva_ctx->slice_count >= dxva_ctx->max_slice_size) { FUN_CHECK(ret = realloc_slice_list(dxva_ctx)); } - streamlen_add = p_nal->sodb_len + sizeof(start_code); - stream_offset = dxva_ctx->strm_offset + streamlen_add; - if (stream_offset > dxva_ctx->max_strm_size) { - FUN_CHECK (ret = realloc_stream_buffer(dxva_ctx, streamlen_add)); - } + //streamlen_add = p_nal->sodb_len + sizeof(start_code); + //stream_offset = dxva_ctx->strm_offset + streamlen_add; + //if (stream_offset > dxva_ctx->max_strm_size) { + // FUN_CHECK (ret = realloc_stream_buffer(dxva_ctx, streamlen_add)); + //} p_long = &dxva_ctx->slice_long[dxva_ctx->slice_count]; memset(p_long, 0, sizeof(DXVA_Slice_H264_Long)); p_long->BSNALunitDataLocation = dxva_ctx->strm_offset; p_long->wBadSliceChopping = 0; //!< set to 0 in Rock-Chip RKVDEC IP - memcpy(&dxva_ctx->bitstream[dxva_ctx->strm_offset], start_code, sizeof(start_code)); - dxva_ctx->strm_offset += sizeof(start_code); - memcpy(&dxva_ctx->bitstream[dxva_ctx->strm_offset], p_nal->sodb_buf, p_nal->sodb_len); - dxva_ctx->strm_offset += p_nal->sodb_len; - p_long->SliceBytesInBuffer = dxva_ctx->strm_offset - p_long->BSNALunitDataLocation; + //memcpy(&dxva_ctx->bitstream[dxva_ctx->strm_offset], start_code, sizeof(start_code)); + //dxva_ctx->strm_offset += sizeof(start_code); + //memcpy(&dxva_ctx->bitstream[dxva_ctx->strm_offset], p_nal->sodb_buf, p_nal->sodb_len); + //dxva_ctx->strm_offset += p_nal->sodb_len; + //p_long->SliceBytesInBuffer = dxva_ctx->strm_offset - p_long->BSNALunitDataLocation; + + (void)p_nal; + (void)stream_offset; + (void)streamlen_add; return ret = MPP_OK; __FAILED: diff --git a/mpp/codec/dec/h264/h264d_global.h b/mpp/codec/dec/h264/h264d_global.h index 8c581cd3..0532ad54 100644 --- a/mpp/codec/dec/h264/h264d_global.h +++ b/mpp/codec/dec/h264/h264d_global.h @@ -42,17 +42,15 @@ #define DPB_INFO_SIZE 16 #define REFPIC_INFO_SIZE 32 + #define MAX_TASK_SIZE 2 -#define NALU_BUF_MAX_SIZE 10*1024*1024 -#define NALU_BUF_ADD_SIZE 1024 +#define NALU_BUF_MAX_SIZE 10 +#define NALU_BUF_ADD_SIZE 10 +#define HEAD_BUF_MAX_SIZE 10*1024*1024 +#define HEAD_BUF_ADD_SIZE 1024 +#define SODB_BUF_MAX_SIZE 10*1024*1024 +#define SODB_BUF_ADD_SIZE 1024 -#ifndef min -#define min(a,b) (((a) < (b)) ? (a) : (b)) -#endif - -#ifndef max -#define max(a,b) (((a) > (b)) ? (a) : (b)) -#endif //!< AVC Profile IDC definitions typedef enum { @@ -732,7 +730,7 @@ typedef struct h264_slice_t { RK_S32 framepoc; //poc of this frame RK_U32 AbsFrameNum; RK_S32 PicOrderCntMsb; - RK_S32 is_new_picture_flag; + RK_S32 is_new_picture; struct h264_sps_t *active_sps; struct h264_subsps_t *active_subsps; struct h264_pps_t *active_pps; @@ -823,21 +821,31 @@ typedef struct h264d_input_ctx_t { //!< input data RK_U8 *in_buf; size_t *in_size; - RK_S64 *in_timestamp; + size_t in_length; + RK_S64 in_timestamp; //!< output data RK_U8 *out_buf; - RK_U32 *out_length; - + RK_U32 out_length; + RK_U8 task_valid; } H264dInputCtx_t; //!< current stream typedef struct h264d_curstrm_t { - RK_U32 offset; //!< The offset of the input stream - RK_U32 max_size; //!< Cur Unit Buffer size - RK_U8 *buf; //!< store read nalu data - RK_U8 prefixdata[START_PREFIX_3BYTE]; - RK_U8 startcode_found; - RK_U8 endcode_found; + RK_U32 nalu_offset; //!< The offset of the input stream + + RK_U32 nalu_max_size; //!< Cur Unit Buffer size + RK_U8 *curdata; + RK_S32 nal_unit_type; + RK_U32 nalu_len; + RK_U8 *nalu_buf; //!< store read nalu data + + RK_U32 head_offset; + RK_U32 head_max_size; + RK_U8 *head_buf; //!< store header data, sps/pps/slice header + + RK_U8 prefixdata[START_PREFIX_3BYTE]; + RK_U8 startcode_found; + RK_U8 endcode_found; } H264dCurStream_t; //!< current parameters @@ -937,11 +945,11 @@ typedef struct h264d_mem_t { typedef enum nalu_state_tpye { NALU_NULL = 0, - //StreamError, + StreamError, HaveNoStream, NaluNotSupport, - //ReadNaluError, - //StartofNalu, + ReadNaluError, + StartofNalu, EndofStream, //ReallocBufError, MidOfNalu, @@ -992,8 +1000,10 @@ typedef struct h264_dec_ctx_t { RK_U32 spt_decode_mtds; //!< support decoder methods NALU_STATUS nalu_ret; //!< current nalu state SLICE_STATUS next_state; //!< RKV_SLICE_STATUS - RK_U8 first_frame_flag; - RK_U8 parser_end_flag; + RK_U8 is_first_frame; + RK_U8 is_first_frame2; + RK_U8 is_new_frame; + RK_U8 is_parser_end; RK_U8 dxva_idx; struct h264d_logctx_t logctx; //!< debug log file struct log_ctx_t logctxbuf[LOG_MAX]; diff --git a/mpp/codec/dec/h264/h264d_init.c b/mpp/codec/dec/h264/h264d_init.c index 67ab885e..93ab24bf 100644 --- a/mpp/codec/dec/h264/h264d_init.c +++ b/mpp/codec/dec/h264/h264d_init.c @@ -1119,17 +1119,17 @@ static MPP_RET init_lists_b_slice_mvc(H264_SLICE_t *currSlice) for (i = currSlice->listXsizeB[1]; i < (MAX_LIST_SIZE); i++) { currSlice->listB[1][i] = p_Vid->no_reference_picture; } - MPP_FREE(currSlice->fs_listinterview0); - MPP_FREE(currSlice->fs_listinterview1); + MPP_FREE(currSlice->fs_listinterview0); + MPP_FREE(currSlice->fs_listinterview1); return ret = MPP_OK; __FAILED: ASSERT(0); - MPP_FREE(fs_list0); - MPP_FREE(fs_list1); - MPP_FREE(fs_listlt); - MPP_FREE(currSlice->fs_listinterview0); - MPP_FREE(currSlice->fs_listinterview1); + MPP_FREE(fs_list0); + MPP_FREE(fs_list1); + MPP_FREE(fs_listlt); + MPP_FREE(currSlice->fs_listinterview0); + MPP_FREE(currSlice->fs_listinterview1); return ret; } diff --git a/mpp/codec/dec/h264/h264d_log.c b/mpp/codec/dec/h264/h264d_log.c index d0565ff5..5b43de96 100644 --- a/mpp/codec/dec/h264/h264d_log.c +++ b/mpp/codec/dec/h264/h264d_log.c @@ -30,7 +30,8 @@ #define LOG_BUF_SIZE 512 -RK_U32 g_nalu_cnt = 0; +RK_U32 g_nalu_cnt0 = 0; +RK_U32 g_nalu_cnt1 = 0; RK_S32 g_max_bytes = 0; RK_U32 g_max_slice_data = 0; FILE *g_debug_file0 = NULL; @@ -83,19 +84,19 @@ const char *logctrl_name[LOG_MAX] = { */ static void log_info(void *ctx, ...) { - char *fname = NULL; - RK_U32 line = 0; - char argbuf[LOG_BUF_SIZE] = { 0 }; + char *fname = NULL; + RK_U32 line = 0; + char argbuf[LOG_BUF_SIZE] = { 0 }; - if(LogEnable(ctx, LOG_LEVEL_INFO)) { - va_list argptr; - va_start(argptr, ctx); - fname = va_arg(argptr, char*); - line = va_arg(argptr, RK_U32); - vsnprintf(argbuf, sizeof(argbuf), va_arg(argptr, char*), argptr); - writelog(ctx, "syntax", fname, line, argbuf); - va_end(argptr); - } + if (LogEnable(ctx, LOG_LEVEL_INFO)) { + va_list argptr; + va_start(argptr, ctx); + fname = va_arg(argptr, char*); + line = va_arg(argptr, RK_U32); + vsnprintf(argbuf, sizeof(argbuf), va_arg(argptr, char*), argptr); + writelog(ctx, "syntax", fname, line, argbuf); + va_end(argptr); + } } /*! *********************************************************************** @@ -209,7 +210,7 @@ void set_log_outpath(LogEnv_t *env) } } } - + /*! *********************************************************************** @@ -220,47 +221,47 @@ void set_log_outpath(LogEnv_t *env) void writelog(void *in_ctx, ...) { #if __DEBUG_EN - RK_U32 line = 0; - char *levelname = NULL; - char *filename = NULL; - char argmsg[LOG_BUF_SIZE] = { 0 }; - LogCtx_t *ctx = (LogCtx_t *)in_ctx; - char *pfn = NULL, *pfn0 = NULL , *pfn1 = NULL; + RK_U32 line = 0; + char *levelname = NULL; + char *filename = NULL; + char argmsg[LOG_BUF_SIZE] = { 0 }; + LogCtx_t *ctx = (LogCtx_t *)in_ctx; + char *pfn = NULL, *pfn0 = NULL , *pfn1 = NULL; va_list argptr; va_start(argptr, in_ctx); - levelname = va_arg(argptr, char*); - filename = va_arg(argptr, char*); - line = va_arg(argptr, RK_U32); - vsnprintf(argmsg, sizeof(argmsg), va_arg(argptr, char*), argptr); + levelname = va_arg(argptr, char*); + filename = va_arg(argptr, char*); + line = va_arg(argptr, RK_U32); + vsnprintf(argmsg, sizeof(argmsg), va_arg(argptr, char*), argptr); - pfn0 = strrchr(filename, '/'); - pfn1 = strrchr(filename, '\\'); - pfn = pfn0 ? (pfn0 + 1) : (pfn1 ? (pfn1 + 1) : filename); - if (ctx->flag->print_en) { - mpp_log("[TAG=%s] file: %s:%d, [ %s ], %s", ctx->tag, pfn, line, levelname, argmsg); - } - if (ctx->fp && ctx->flag->write_en) { - fprintf(ctx->fp, "%s \n", argmsg); - //fprintf(ctx->fp, "[ TAG = %s ], file: %s, line: %d, [ %s ], %s \n", ctx->tag, pfn, line, loglevel, argbuf); - fflush(ctx->fp); - } - va_end(argptr); + pfn0 = strrchr(filename, '/'); + pfn1 = strrchr(filename, '\\'); + pfn = pfn0 ? (pfn0 + 1) : (pfn1 ? (pfn1 + 1) : filename); + if (ctx->flag->print_en) { + mpp_log("[TAG=%s] file: %s:%d, [ %s ], %s", ctx->tag, pfn, line, levelname, argmsg); + } + if (ctx->fp && ctx->flag->write_en) { + fprintf(ctx->fp, "%s \n", argmsg); + //fprintf(ctx->fp, "[ TAG = %s ], file: %s, line: %d, [ %s ], %s \n", ctx->tag, pfn, line, loglevel, argbuf); + fflush(ctx->fp); + } + va_end(argptr); #endif } -/*! -*********************************************************************** -* \brief -* set bitread log context -*********************************************************************** -*/ -void set_bitread_logctx(BitReadCtx_t *bitctx, LogCtx_t *p_ctx) -{ - bitctx->ctx = p_ctx; - bitctx->wlog = log_info; +/*! +*********************************************************************** +* \brief +* set bitread log context +*********************************************************************** +*/ +void set_bitread_logctx(BitReadCtx_t *bitctx, LogCtx_t *p_ctx) +{ + bitctx->ctx = p_ctx; + bitctx->wlog = log_info; } - - + + diff --git a/mpp/codec/dec/h264/h264d_parse.c b/mpp/codec/dec/h264/h264d_parse.c index 6e22632d..46ab5273 100644 --- a/mpp/codec/dec/h264/h264d_parse.c +++ b/mpp/codec/dec/h264/h264d_parse.c @@ -33,6 +33,16 @@ #include "h264d_init.h" #include "h264d_fill.h" +#define HEAD_MAX_SIZE 12800 + +static const RK_U8 g_start_precode[3] = {0, 0, 1}; + +typedef struct h264d_nalu_head_t { + RK_U16 is_frame_end; + RK_U16 nal_unit_type; + RK_U32 sodb_len; +} H264dNaluHead_t; + static void reset_slice(H264dVideoCtx_t *p_Vid) { RK_U32 i = 0, j = 0; @@ -62,26 +72,26 @@ static void reset_slice(H264dVideoCtx_t *p_Vid) FunctionOut(p_Vid->p_Dec->logctx.parr[RUN_PARSE]); } -static MPP_RET realloc_curstrearm_buffer(H264dCurStream_t *p_strm) +static MPP_RET realloc_buffer(RK_U8 **buf, RK_U32 *max_size, RK_U32 add_size) { MPP_RET ret = MPP_ERR_UNKNOW; - p_strm->buf = mpp_realloc(p_strm->buf, RK_U8, p_strm->max_size + NALU_BUF_ADD_SIZE); - MEM_CHECK(ret, p_strm->buf); - p_strm->max_size += NALU_BUF_ADD_SIZE; + *buf = mpp_realloc(*buf, RK_U8, *max_size + add_size); + MEM_CHECK(ret, *buf); + *max_size += add_size; return ret = MPP_OK; __FAILED: return ret; } -static void reset_nalu(H264_Nalu_t*p_nal, H264dCurStream_t *p_strm) +static void reset_nalu(H264dCurStream_t *p_strm) { if (p_strm->endcode_found) { p_strm->startcode_found = p_strm->endcode_found; - memset(p_nal, 0, sizeof(H264_Nalu_t)); + p_strm->nalu_len = 0; + p_strm->nal_unit_type = NALU_TYPE_NULL; p_strm->endcode_found = 0; } - p_nal->sodb_buf = p_strm->buf; } static void find_prefix_code(RK_U8 *p_data, H264dCurStream_t *p_strm) @@ -101,42 +111,39 @@ static void find_prefix_code(RK_U8 *p_data, H264dCurStream_t *p_strm) } } -static MPP_RET read_nalu(H264_SLICE_t *currSlice) +static MPP_RET read_one_nalu(H264dInputCtx_t *p_Inp, H264dCurStream_t *p_strm) { MPP_RET ret = MPP_ERR_UNKNOW; - RK_U8 *p_curdata = NULL; - H264dLogCtx_t *logctx = currSlice->logctx; - H264_DecCtx_t *p_Dec = currSlice->p_Dec; - H264dCurCtx_t *p_Cur = currSlice->p_Cur; - H264dInputCtx_t *p_Inp = currSlice->p_Inp; + H264dLogCtx_t *logctx = &p_Inp->p_Dec->logctx; + H264_DecCtx_t *p_Dec = p_Inp->p_Dec; FunctionIn(logctx->parr[RUN_PARSE]); - reset_nalu(&p_Cur->nalu, &p_Cur->strm); - while ((*p_Inp->in_size) > 0) { - p_curdata = &p_Inp->in_buf[p_Cur->strm.offset++]; - (*p_Inp->in_size) -= 1; + reset_nalu(p_strm); - if (p_Cur->strm.startcode_found) { - if (p_Cur->nalu.sodb_len >= p_Cur->strm.max_size) { - FUN_CHECK(ret = realloc_curstrearm_buffer(&p_Cur->strm)); - p_Cur->nalu.sodb_buf = p_Cur->strm.buf; + while (p_Inp->in_length > 0) { + p_strm->curdata = &p_Inp->in_buf[p_strm->nalu_offset++]; + (*p_Inp->in_size) -= 1; + p_Inp->in_length--; + if (p_strm->startcode_found) { + + if (p_strm->nalu_len >= p_strm->nalu_max_size) { + FUN_CHECK(ret = realloc_buffer(&p_strm->nalu_buf, &p_strm->nalu_max_size, NALU_BUF_ADD_SIZE)); } - p_Cur->nalu.sodb_buf[p_Cur->nalu.sodb_len++] = *p_curdata; + p_strm->nalu_buf[p_strm->nalu_len++] = *p_strm->curdata; } - find_prefix_code(p_curdata, &p_Cur->strm); - if (p_Cur->strm.endcode_found) { - p_Cur->nalu.sodb_len -= START_PREFIX_3BYTE; - while (p_Cur->nalu.sodb_buf[p_Cur->nalu.sodb_len - 1] == 0x00) { //!< find non-zeros byte - p_Cur->nalu.sodb_len--; + find_prefix_code(p_strm->curdata, p_strm); + if (p_strm->endcode_found) { + p_strm->nalu_len -= START_PREFIX_3BYTE; + while (p_strm->nalu_buf[p_strm->nalu_len - 1] == 0x00) { //!< find non-zeros byte + p_strm->nalu_len--; } p_Dec->nalu_ret = EndOfNalu; break; } } - if (!(*p_Inp->in_size)) { //!< check input - p_Cur->strm.offset = 0; - p_Cur->strm.endcode_found = (p_Inp->is_eos && p_Cur->nalu.sodb_len) ? 1 : p_Cur->strm.endcode_found; - p_Dec->nalu_ret = p_Inp->is_eos ? (p_Cur->nalu.sodb_len ? EndOfNalu : EndofStream) : HaveNoStream; + if (!p_Inp->in_length) { //!< check input + p_strm->nalu_offset = 0; + p_Dec->nalu_ret = HaveNoStream; } FunctionIn(logctx->parr[RUN_PARSE]); @@ -146,7 +153,8 @@ __FAILED: return ret; } -RK_U32 g_strm_bytes = 0; + + static MPP_RET parser_nalu_header(H264_SLICE_t *currSlice) { MPP_RET ret = MPP_ERR_UNKNOW; @@ -158,23 +166,29 @@ static MPP_RET parser_nalu_header(H264_SLICE_t *currSlice) FunctionIn(logctx->parr[RUN_PARSE]); mpp_set_bitread_ctx(p_bitctx, cur_nal->sodb_buf, cur_nal->sodb_len); - g_strm_bytes += p_Cur->nalu.sodb_len; + set_bitread_logctx(p_bitctx, logctx->parr[LOG_READ_NALU]); LogInfo(p_bitctx->ctx, "================== NAL begin ==================="); READ_BITS(p_bitctx, 1, &cur_nal->forbidden_bit, "forbidden_bit"); ASSERT(cur_nal->forbidden_bit == 0); READ_BITS(p_bitctx, 2, (RK_S32 *)&cur_nal->nal_reference_idc, "nal_ref_idc"); READ_BITS(p_bitctx, 5, (RK_S32 *)&cur_nal->nal_unit_type, "nal_unit_type"); - if (g_nalu_cnt == 344) { - g_nalu_cnt = g_nalu_cnt; + if (g_nalu_cnt0 == 2384) { + g_nalu_cnt0 = g_nalu_cnt0; } //if (g_debug_file1 == NULL) //{ // g_debug_file1 = fopen("rk_debugfile_view1.txt", "wb"); //} //FPRINT(g_debug_file1, "g_nalu_cnt = %d, nal_unit_type = %d, nalu_size = %d\n", g_nalu_cnt++, cur_nal->nal_unit_type, cur_nal->sodb_len); - - + /* if((cur_nal->nal_unit_type == NALU_TYPE_SLICE) + || (cur_nal->nal_unit_type == NALU_TYPE_IDR) + || (cur_nal->nal_unit_type == NALU_TYPE_SPS) + || (cur_nal->nal_unit_type == NALU_TYPE_PPS) + || (cur_nal->nal_unit_type == NALU_TYPE_SUB_SPS) + || (cur_nal->nal_unit_type == NALU_TYPE_SEI))*/{ + FPRINT(g_debug_file0, "g_nalu_cnt=%d, nal_type=%d, sodb_len=%d \n", g_nalu_cnt0++, cur_nal->nal_unit_type, cur_nal->sodb_len); + } cur_nal->ualu_header_bytes = 1; currSlice->svc_extension_flag = -1; //!< initialize to -1 if ((cur_nal->nal_unit_type == NALU_TYPE_PREFIX) || (cur_nal->nal_unit_type == NALU_TYPE_SLC_EXT)) { @@ -207,16 +221,19 @@ static MPP_RET parser_nalu_header(H264_SLICE_t *currSlice) } mpp_set_bitread_ctx(p_bitctx, (cur_nal->sodb_buf + cur_nal->ualu_header_bytes), (cur_nal->sodb_len - cur_nal->ualu_header_bytes)); // reset + p_Cur->p_Dec->nalu_ret = StartofNalu; FunctionOut(logctx->parr[RUN_PARSE]); return ret = MPP_OK; __BITREAD_ERR: - ret = p_bitctx->ret; + p_Cur->p_Dec->nalu_ret = ReadNaluError; + return ret = p_bitctx->ret; __FAILED: + p_Cur->p_Dec->nalu_ret = StreamError; return ret; } -static MPP_RET parser_nalu(H264_SLICE_t *currSlice) +static MPP_RET parser_one_nalu(H264_SLICE_t *currSlice) { MPP_RET ret = MPP_ERR_UNKNOW; LogCtx_t *runlog = currSlice->logctx->parr[RUN_PARSE]; @@ -227,8 +244,9 @@ static MPP_RET parser_nalu(H264_SLICE_t *currSlice) case NALU_TYPE_SLICE: case NALU_TYPE_IDR: FUN_CHECK(ret = process_slice(currSlice)); - if (currSlice->is_new_picture_flag) { + if (currSlice->is_new_picture) { currSlice->p_Dec->nalu_ret = StartOfPicture; + FPRINT(g_debug_file0, "----- start of picture ---- \n"); } else { currSlice->p_Dec->nalu_ret = StartOfSlice; } @@ -300,20 +318,199 @@ __FAILED: return ret; } +static MPP_RET find_next_frame(H264dCurCtx_t *p_Cur) +{ + MPP_RET ret = MPP_ERR_UNKNOW; + RK_U32 nalu_header_bytes = 0; + RK_U32 first_mb_in_slice = 0; + H264dLogCtx_t *logctx = &p_Cur->p_Dec->logctx; + BitReadCtx_t *p_bitctx = &p_Cur->bitctx; + H264dCurStream_t *p_strm = &p_Cur->strm; + RK_U32 forbidden_bit = -1; + RK_U32 nal_reference_idc = -1; + RK_U32 svc_extension_flag = -1; + + FunctionIn(logctx->parr[RUN_PARSE]); + mpp_set_bitread_ctx(p_bitctx, p_strm->nalu_buf, p_strm->nalu_len); + set_bitread_logctx(p_bitctx, logctx->parr[LOG_READ_NALU]); + READ_BITS(p_bitctx, 1, &forbidden_bit); + ASSERT(forbidden_bit == 0); + READ_BITS(p_bitctx, 2, &nal_reference_idc); + READ_BITS(p_bitctx, 5, &p_strm->nal_unit_type); + if (g_nalu_cnt1 == 29) { + g_nalu_cnt1 = g_nalu_cnt1; + } + FPRINT(g_debug_file1, "g_nalu_cnt=%d, nal_type=%d, sodb_len=%d \n", g_nalu_cnt1++, p_strm->nal_unit_type, p_strm->nalu_len); + + nalu_header_bytes = 1; + if ((p_strm->nal_unit_type == NALU_TYPE_PREFIX) || (p_strm->nal_unit_type == NALU_TYPE_SLC_EXT)) { + READ_BITS(p_bitctx, 1, &svc_extension_flag); + if (svc_extension_flag) { + LogInfo(logctx->parr[RUN_PARSE], "svc_extension is not supported."); + goto __FAILED; + } else { + if (p_strm->nal_unit_type == NALU_TYPE_SLC_EXT) { + p_strm->nal_unit_type = NALU_TYPE_SLICE; + } + } + nalu_header_bytes += 3; + } + //-- parse slice + if ( p_strm->nal_unit_type == NALU_TYPE_SLICE || p_strm->nal_unit_type == NALU_TYPE_IDR) { + mpp_set_bitread_ctx(p_bitctx, (p_strm->nalu_buf + nalu_header_bytes), 4); // reset + READ_UE(p_bitctx, &first_mb_in_slice); + + if (!p_Cur->p_Dec->is_first_frame && (first_mb_in_slice == 0)) { + p_Cur->p_Dec->is_new_frame = 1; + } + p_Cur->p_Dec->is_first_frame = 0; + } + + FunctionOut(logctx->parr[RUN_PARSE]); + return ret = MPP_OK; +__BITREAD_ERR: + return ret = p_bitctx->ret; +__FAILED: + return ret; +} + +static MPP_RET add_empty_nalu(H264dCurCtx_t *p_Cur) +{ + MPP_RET ret = MPP_ERR_UNKNOW; + RK_U8 *p_des = NULL; + H264dCurStream_t *p_strm = &p_Cur->strm; + + if (p_strm->head_offset >= p_strm->head_max_size) { + + FUN_CHECK(ret = realloc_buffer(&p_strm->head_buf, &p_strm->head_max_size, HEAD_BUF_ADD_SIZE)); + } + p_des = &p_strm->head_buf[p_strm->head_offset]; + + ((H264dNaluHead_t *)p_des)->is_frame_end = 1; + ((H264dNaluHead_t *)p_des)->nal_unit_type = 0; + ((H264dNaluHead_t *)p_des)->sodb_len = 0; + p_strm->head_offset += sizeof(H264dNaluHead_t); + + return ret = MPP_OK; +__FAILED: + return ret; +} + + +static MPP_RET store_cur_nalu(H264dCurCtx_t *p_Cur) +{ + MPP_RET ret = MPP_ERR_UNKNOW; + RK_U8 *p_des = NULL; + RK_U32 add_size = 0; + H264dDxvaCtx_t *dxva_ctx = NULL; + H264dCurStream_t *p_strm = &p_Cur->strm; + + FunctionIn(p_Cur->p_Dec->logctx.parr[RUN_PARSE]); + //!< fill head buffer + if ((p_strm->nal_unit_type == NALU_TYPE_SLICE) + || (p_strm->nal_unit_type == NALU_TYPE_IDR) + || (p_strm->nal_unit_type == NALU_TYPE_SPS) + || (p_strm->nal_unit_type == NALU_TYPE_PPS) + || (p_strm->nal_unit_type == NALU_TYPE_SUB_SPS) + || (p_strm->nal_unit_type == NALU_TYPE_SEI) + || (p_strm->nal_unit_type == NALU_TYPE_PREFIX) + || (p_strm->nal_unit_type == NALU_TYPE_SLC_EXT)) { + if (p_strm->head_offset >= p_strm->head_max_size) { + add_size = MPP_MAX(HEAD_BUF_ADD_SIZE, p_strm->nalu_len); + FUN_CHECK(ret = realloc_buffer(&p_strm->head_buf, &p_strm->head_max_size, add_size)); + } + p_des = &p_strm->head_buf[p_strm->head_offset]; + add_size = MPP_MIN(HEAD_MAX_SIZE, p_strm->nalu_len); + ((H264dNaluHead_t *)p_des)->is_frame_end = 0; + ((H264dNaluHead_t *)p_des)->nal_unit_type = p_strm->nal_unit_type; + ((H264dNaluHead_t *)p_des)->sodb_len = add_size; + memcpy(p_des + sizeof(H264dNaluHead_t), p_strm->nalu_buf, add_size); + p_strm->head_offset += add_size + sizeof(H264dNaluHead_t); + } + //!< fill sodb buffer + if ((p_strm->nal_unit_type == NALU_TYPE_SLICE) + || (p_strm->nal_unit_type == NALU_TYPE_IDR)) { + dxva_ctx = p_Cur->p_Dec->dxva_ctx; + if (dxva_ctx->strm_offset >= dxva_ctx->max_strm_size) { + add_size = MPP_MAX(SODB_BUF_ADD_SIZE, p_strm->nalu_len); + realloc_buffer(&dxva_ctx->bitstream, &dxva_ctx->max_strm_size, add_size); + } + p_des = &dxva_ctx->bitstream[dxva_ctx->strm_offset]; + memcpy(p_des, g_start_precode, sizeof(g_start_precode)); + memcpy(p_des + sizeof(g_start_precode), p_strm->nalu_buf, p_strm->nalu_len); + dxva_ctx->strm_offset += p_strm->nalu_len + sizeof(g_start_precode); + } + FunctionIn(p_Cur->p_Dec->logctx.parr[RUN_PARSE]); + + return ret = MPP_OK; +__FAILED: + + return ret; +} + /*! *********************************************************************** * \brief -* loop function for parser +* prepare function for parser +*********************************************************************** +*/ +MPP_RET parse_prepare(H264dInputCtx_t *p_Inp, H264dCurCtx_t *p_Cur) +{ + MPP_RET ret = MPP_ERR_UNKNOW; + FunctionIn(p_Inp->p_Dec->logctx.parr[RUN_PARSE]); + + p_Inp->task_valid = 0; + if (p_Cur->p_Inp->is_eos) { + FUN_CHECK(ret = find_next_frame(p_Cur)); + FUN_CHECK(ret = store_cur_nalu(p_Cur)); + FUN_CHECK(ret = add_empty_nalu(p_Cur)); + p_Inp->task_valid = 1; + FPRINT(g_debug_file1, "----- end of stream ---- \n"); + goto __RETURN; + } else if (p_Cur->p_Dec->is_new_frame) { + FUN_CHECK(ret = store_cur_nalu(p_Cur)); + p_Cur->p_Dec->is_new_frame = 0; + } + + FUN_CHECK(ret = read_one_nalu(p_Inp, &p_Cur->strm)); + + if (p_Inp->p_Dec->nalu_ret == EndOfNalu) { + FUN_CHECK(ret = find_next_frame(p_Cur)); + if (p_Cur->p_Dec->is_new_frame) { + //!< add an empty nalu to tell frame end + FUN_CHECK(ret = add_empty_nalu(p_Cur)); + //!< reset curstream parameters + p_Cur->strm.head_offset = 0; + p_Inp->task_valid = 1; + FPRINT(g_debug_file1, "----- start of picture ---- \n"); + } else { + FUN_CHECK(ret = store_cur_nalu(p_Cur)); + } + } + +__RETURN: + FunctionOut(p_Inp->p_Dec->logctx.parr[RUN_PARSE]); + return ret = MPP_OK; +__FAILED: + return ret; +} + + +/*! +*********************************************************************** +* \brief +* main function for parser *********************************************************************** */ MPP_RET parse_loop(H264_DecCtx_t *p_Dec) { MPP_RET ret = MPP_ERR_UNKNOW; - RK_U32 while_loop_flag = 1; - - + RK_U8 *p_curdata = NULL; + RK_U8 while_loop_flag = 1; + H264dNaluHead_t *p_head = NULL; FunctionIn(p_Dec->logctx.parr[RUN_PARSE]); - //!< ==== parse loop ==== + //!< ==== loop ==== + p_curdata = p_Dec->p_Cur->strm.head_buf; while (while_loop_flag) { switch (p_Dec->next_state) { case SliceSTATE_ResetSlice: @@ -321,26 +518,26 @@ MPP_RET parse_loop(H264_DecCtx_t *p_Dec) p_Dec->next_state = SliceSTATE_ReadNalu; break; case SliceSTATE_ReadNalu: - (ret = read_nalu(&p_Dec->p_Cur->slice)); - if (p_Dec->nalu_ret == EndOfNalu) { - p_Dec->next_state = SliceSTATE_ParseNalu; - } else if (p_Dec->nalu_ret == EndofStream) { + p_head = (H264dNaluHead_t *)p_curdata; + if (p_head->is_frame_end) { p_Dec->next_state = SliceSTATE_RegisterOneFrame; - } else if (p_Dec->nalu_ret == HaveNoStream) { - while_loop_flag = 0; + p_Dec->nalu_ret = HaveNoStream; + } else { + p_curdata += sizeof(H264dNaluHead_t); + memset(&p_Dec->p_Cur->nalu, 0, sizeof(H264_Nalu_t)); + p_Dec->p_Cur->nalu.sodb_buf = p_curdata; + p_Dec->p_Cur->nalu.sodb_len = p_head->sodb_len; + p_curdata += p_head->sodb_len; + p_Dec->nalu_ret = EndOfNalu; + p_Dec->next_state = SliceSTATE_ParseNalu; } break; case SliceSTATE_ParseNalu: - (ret = parser_nalu(&p_Dec->p_Cur->slice)); + (ret = parser_one_nalu(&p_Dec->p_Cur->slice)); if (p_Dec->nalu_ret == StartOfSlice) { p_Dec->next_state = SliceSTATE_GetSliceData; } else if (p_Dec->nalu_ret == StartOfPicture) { - if (p_Dec->first_frame_flag) { - p_Dec->next_state = SliceSTATE_InitPicture; - p_Dec->first_frame_flag = 0; - } else { - p_Dec->next_state = SliceSTATE_RegisterOneFrame; - } + p_Dec->next_state = SliceSTATE_InitPicture; } else { p_Dec->next_state = SliceSTATE_ReadNalu; } @@ -357,8 +554,8 @@ MPP_RET parse_loop(H264_DecCtx_t *p_Dec) case SliceSTATE_RegisterOneFrame: commit_buffer(p_Dec->dxva_ctx); while_loop_flag = 0; - p_Dec->parser_end_flag = 1; - p_Dec->next_state = SliceSTATE_InitPicture; + p_Dec->is_parser_end = 1; + p_Dec->next_state = SliceSTATE_ReadNalu; break; default: ret = MPP_NOK; @@ -366,10 +563,9 @@ MPP_RET parse_loop(H264_DecCtx_t *p_Dec) } } FunctionIn(p_Dec->logctx.parr[RUN_PARSE]); -//__RETURN: + //__RETURN: return ret = MPP_OK; __FAILED: - return ret; } diff --git a/mpp/codec/dec/h264/h264d_parse.h b/mpp/codec/dec/h264/h264d_parse.h index 35734912..02d0640b 100644 --- a/mpp/codec/dec/h264/h264d_parse.h +++ b/mpp/codec/dec/h264/h264d_parse.h @@ -29,9 +29,8 @@ extern "C" { #endif -MPP_RET parse_loop(H264_DecCtx_t *p_Dec); - - +MPP_RET parse_prepare(H264dInputCtx_t *p_Inp, H264dCurCtx_t *p_Cur); +MPP_RET parse_loop (H264_DecCtx_t *p_Dec); #ifdef __cplusplus diff --git a/mpp/codec/dec/h264/h264d_pps.c b/mpp/codec/dec/h264/h264d_pps.c index d6465471..08e5c406 100644 --- a/mpp/codec/dec/h264/h264d_pps.c +++ b/mpp/codec/dec/h264/h264d_pps.c @@ -58,7 +58,7 @@ static MPP_RET parse_pps_calingLists(BitReadCtx_t *p_bitctx, H264_SPS_t *sps, H2 return ret = MPP_OK; __BITREAD_ERR: - ret = p_bitctx->ret; + ret = p_bitctx->ret; __FAILED: ASSERT(0); return ret; @@ -113,11 +113,11 @@ static MPP_RET parser_pps(BitReadCtx_t *p_bitctx, H264_SPS_t *cur_sps, H264_PPS_ return ret = MPP_OK; -__BITREAD_ERR: +__BITREAD_ERR: ret = p_bitctx->ret; __FAILED: - ASSERT(0); - return ret; + ASSERT(0); + return ret; } diff --git a/mpp/codec/dec/h264/h264d_scalist.c b/mpp/codec/dec/h264/h264d_scalist.c index fbb07ef4..e3c04ad3 100644 --- a/mpp/codec/dec/h264/h264d_scalist.c +++ b/mpp/codec/dec/h264/h264d_scalist.c @@ -196,7 +196,7 @@ MPP_RET parse_scalingList(BitReadCtx_t *p_bitctx, RK_S32 size, RK_S32 *scaling_l return ret = MPP_OK; __BITREAD_ERR: - ret = p_bitctx->ret; + ret = p_bitctx->ret; return ret; } @@ -279,7 +279,7 @@ MPP_RET get_max_dec_frame_buf_size(H264_SPS_t *sps) * (sps->pic_height_in_map_units_minus1 + 1) * (sps->frame_mbs_only_flag ? 1 : 2) * 384; size /= pic_size; - size = min(size, 16); + size = MPP_MIN(size, 16); sps->max_dec_frame_buffering = size; return ret = MPP_OK; @@ -316,7 +316,7 @@ MPP_RET parse_sps_scalinglists(BitReadCtx_t *p_bitctx, H264_SPS_t *sps) return ret = MPP_OK; __BITREAD_ERR: - ret = p_bitctx->ret; + ret = p_bitctx->ret; __FAILED: return ret; } diff --git a/mpp/codec/dec/h264/h264d_sei.c b/mpp/codec/dec/h264/h264d_sei.c index eadfc5fb..51a7255a 100644 --- a/mpp/codec/dec/h264/h264d_sei.c +++ b/mpp/codec/dec/h264/h264d_sei.c @@ -133,7 +133,7 @@ static MPP_RET interpret_recovery_point_info(RK_U8 *payload, RK_S32 size, BitRea return ret = MPP_OK; __BITREAD_ERR: - ret = p_bitctx->ret; + ret = p_bitctx->ret; return ret; } @@ -173,7 +173,7 @@ static MPP_RET interpret_mvc_scalable_nesting_info(RK_U8 *payload, RK_S32 size, sei_msg->mvc_scalable_nesting_flag = 1; return ret = MPP_OK; __BITREAD_ERR: - ret = p_bitctx->ret; + ret = p_bitctx->ret; return ret; } @@ -188,7 +188,7 @@ static MPP_RET interpret_buffering_period_info(RK_U8 *payload, RK_S32 size, BitR return ret = MPP_OK; __BITREAD_ERR: - ret = p_bitctx->ret; + ret = p_bitctx->ret; return ret; } @@ -389,7 +389,7 @@ MPP_RET process_sei(H264_SLICE_t *currSlice) return ret = MPP_OK; __BITREAD_ERR: - ret = p_bitctx->ret; + ret = p_bitctx->ret; __FAILED: ASSERT(0); return ret; diff --git a/mpp/codec/dec/h264/h264d_slice.c b/mpp/codec/dec/h264/h264d_slice.c index 66d2f623..344a56f2 100644 --- a/mpp/codec/dec/h264/h264d_slice.c +++ b/mpp/codec/dec/h264/h264d_slice.c @@ -213,7 +213,7 @@ static MPP_RET ref_pic_list_mvc_modification(H264_SLICE_t *currSlice) return ret = MPP_OK; __BITREAD_ERR: - ret = p_bitctx->ret; + ret = p_bitctx->ret; return ret; } @@ -537,13 +537,13 @@ MPP_RET process_slice(H264_SLICE_t *currSlice) if (g_max_bytes < (p_bitctx->used_bits >> 3)) { g_max_bytes = (p_bitctx->used_bits >> 3); } - currSlice->is_new_picture_flag = 1; + currSlice->is_new_picture = 1; } FunctionOut(logctx->parr[RUN_PARSE]); return ret = MPP_OK; __BITREAD_ERR: - ret = p_bitctx->ret; + ret = p_bitctx->ret; __FAILED: recycle_slice(currSlice); diff --git a/mpp/codec/dec/h264/h264d_sps.c b/mpp/codec/dec/h264/h264d_sps.c index 60550007..9fb2d773 100644 --- a/mpp/codec/dec/h264/h264d_sps.c +++ b/mpp/codec/dec/h264/h264d_sps.c @@ -60,7 +60,7 @@ static MPP_RET read_hrd_parameters(BitReadCtx_t *p_bitctx, H264_HRD_t *hrd) return ret = MPP_OK; __BITREAD_ERR: - return ret = p_bitctx->ret; + return ret = p_bitctx->ret; } static void init_VUI(H264_VUI_t *vui) @@ -135,7 +135,7 @@ static MPP_RET read_VUI(BitReadCtx_t *p_bitctx, H264_VUI_t *vui) __BITREAD_ERR: ret = p_bitctx->ret; __FAILED: - return ret; + return ret; } static MPP_RET parser_sps(BitReadCtx_t *p_bitctx, H264_SPS_t *cur_sps) @@ -244,7 +244,7 @@ static MPP_RET parser_sps(BitReadCtx_t *p_bitctx, H264_SPS_t *cur_sps) return ret = MPP_OK; __BITREAD_ERR: - ret = p_bitctx->ret; + ret = p_bitctx->ret; __FAILED: return ret; } @@ -315,7 +315,7 @@ static MPP_RET sps_mvc_extension(BitReadCtx_t *p_bitctx, H264_subSPS_t *subset_s } return ret = MPP_OK; __BITREAD_ERR: - ret = p_bitctx->ret; + ret = p_bitctx->ret; __FAILED: ASSERT(0); return ret; @@ -336,7 +336,7 @@ static MPP_RET parser_subsps_ext(BitReadCtx_t *p_bitctx, H264_subSPS_t *cur_subs return ret = MPP_OK; __BITREAD_ERR: - ret = p_bitctx->ret; + ret = p_bitctx->ret; __FAILED: return ret; } diff --git a/mpp/codec/dec/h265/h265d_parser.c b/mpp/codec/dec/h265/h265d_parser.c index 0a80d446..904f69ed 100644 --- a/mpp/codec/dec/h265/h265d_parser.c +++ b/mpp/codec/dec/h265/h265d_parser.c @@ -338,7 +338,7 @@ static RK_S32 pred_weight_table(HEVCContext *s, BitReadCtx_t *gb) } return 0; __BITREAD_ERR: - return MPP_ERR_STREAM; + return MPP_ERR_STREAM; } static RK_S32 decode_lt_rps(HEVCContext *s, LongTermRPS *rps, BitReadCtx_t *gb) @@ -402,7 +402,7 @@ static RK_S32 decode_lt_rps(HEVCContext *s, LongTermRPS *rps, BitReadCtx_t *gb) return 0; __BITREAD_ERR: - return MPP_ERR_STREAM; + return MPP_ERR_STREAM; } static RK_S32 set_sps(HEVCContext *s, const HEVCSPS *sps) @@ -1010,7 +1010,7 @@ static RK_S32 hls_slice_header(HEVCContext *s) return 0; __BITREAD_ERR: - return MPP_ERR_STREAM; + return MPP_ERR_STREAM; } /** @@ -1043,7 +1043,7 @@ static RK_S32 hls_nal_unit(HEVCContext *s) return (s->nuh_layer_id); __BITREAD_ERR: - return MPP_ERR_STREAM; + return MPP_ERR_STREAM; } static RK_S32 hevc_frame_start(HEVCContext *s) @@ -1468,7 +1468,7 @@ MPP_RET h265d_prepare(void *ctx, MppPacket pkt, HalDecTask *task) HEVCContext *s = (HEVCContext *)h265dctx->priv_data; RK_U8 *buf = NULL; void *pos = NULL; - size_t length = 0; + RK_S32 length = 0; s->eos = mpp_packet_get_eos(pkt); buf = (RK_U8 *)mpp_packet_get_pos(pkt); length = (RK_S32)mpp_packet_get_size(pkt); diff --git a/mpp/codec/dec/h265/h265d_parser2_syntax.c b/mpp/codec/dec/h265/h265d_parser2_syntax.c index 260d24ad..dd668d04 100644 --- a/mpp/codec/dec/h265/h265d_parser2_syntax.c +++ b/mpp/codec/dec/h265/h265d_parser2_syntax.c @@ -282,7 +282,7 @@ RK_S32 h265d_syntax_fill_slice(void *ctx, MppBuffer *streambuf) RK_U32 nal_type; mpp_set_bitread_ctx(&gb_cxt, (RK_U8 *)h->nals[i].data, - h->nals[i].size); + h->nals[i].size); gb = &gb_cxt; @@ -311,5 +311,5 @@ RK_S32 h265d_syntax_fill_slice(void *ctx, MppBuffer *streambuf) ctx_pic->bitstream = (RK_U8*)ptr; return MPP_OK; __BITREAD_ERR: - return MPP_ERR_STREAM; + return MPP_ERR_STREAM; } diff --git a/mpp/codec/dec/h265/h265d_ps.c b/mpp/codec/dec/h265/h265d_ps.c index 7f1efd0e..5d7b41d3 100644 --- a/mpp/codec/dec/h265/h265d_ps.c +++ b/mpp/codec/dec/h265/h265d_ps.c @@ -260,7 +260,7 @@ int mpp_hevc_decode_short_term_rps(HEVCContext *s, ShortTermRPS *rps, } return 0; __BITREAD_ERR: - return MPP_ERR_STREAM; + return MPP_ERR_STREAM; } @@ -295,7 +295,7 @@ static RK_S32 decode_profile_tier_level(HEVCContext *s, PTLCommon *ptl) SKIP_BITS(gb, 12); // XXX_reserved_zero_44bits[32..43] return 0; __BITREAD_ERR: - return MPP_ERR_STREAM; + return MPP_ERR_STREAM; } static RK_S32 parse_ptl(HEVCContext *s, PTL *ptl, int max_num_sub_layers) @@ -322,7 +322,7 @@ static RK_S32 parse_ptl(HEVCContext *s, PTL *ptl, int max_num_sub_layers) return 0; __BITREAD_ERR: - return MPP_ERR_STREAM; + return MPP_ERR_STREAM; } static RK_S32 decode_sublayer_hrd(HEVCContext *s, unsigned int nb_cpb, @@ -343,7 +343,7 @@ static RK_S32 decode_sublayer_hrd(HEVCContext *s, unsigned int nb_cpb, } return 0; __BITREAD_ERR: - return MPP_ERR_STREAM; + return MPP_ERR_STREAM; } static RK_S32 decode_hrd(HEVCContext *s, int common_inf_present, @@ -408,7 +408,7 @@ static RK_S32 decode_hrd(HEVCContext *s, int common_inf_present, } return 0; __BITREAD_ERR: - return MPP_ERR_STREAM; + return MPP_ERR_STREAM; } @@ -1276,7 +1276,7 @@ static RK_S32 decode_vui(HEVCContext *s, HEVCSPS *sps) } return 0; __BITREAD_ERR: - return MPP_ERR_STREAM; + return MPP_ERR_STREAM; } /*static */void set_default_scaling_list_data(ScalingList *sl)///<- zrh remove "static" @@ -1364,7 +1364,7 @@ static int scaling_list_data(HEVCContext *s, ScalingList *sl) return 0; __BITREAD_ERR: - return MPP_ERR_STREAM; + return MPP_ERR_STREAM; } RK_S32 mpp_hevc_decode_nal_sps(HEVCContext *s) diff --git a/mpp/codec/dec/h265/h265d_sei.c b/mpp/codec/dec/h265/h265d_sei.c index 2c59f7c5..80b397bf 100644 --- a/mpp/codec/dec/h265/h265d_sei.c +++ b/mpp/codec/dec/h265/h265d_sei.c @@ -52,7 +52,7 @@ static RK_S32 decode_nal_sei_decoded_picture_hash(HEVCContext *s) } return 0; __BITREAD_ERR: - return MPP_ERR_STREAM; + return MPP_ERR_STREAM; } static RK_S32 decode_nal_sei_frame_packing_arrangement(HEVCContext *s) @@ -82,7 +82,7 @@ static RK_S32 decode_nal_sei_frame_packing_arrangement(HEVCContext *s) SKIP_BITS(gb, 1); // upsampled_aspect_ratio_flag return 0; __BITREAD_ERR: - return MPP_ERR_STREAM; + return MPP_ERR_STREAM; } static RK_S32 decode_pic_timing(HEVCContext *s) @@ -116,7 +116,7 @@ static RK_S32 decode_pic_timing(HEVCContext *s) } return 1; __BITREAD_ERR: - return MPP_ERR_STREAM; + return MPP_ERR_STREAM; } static RK_S32 active_parameter_sets(HEVCContext *s) @@ -143,7 +143,7 @@ static RK_S32 active_parameter_sets(HEVCContext *s) return 0; __BITREAD_ERR: - return MPP_ERR_STREAM; + return MPP_ERR_STREAM; } static RK_S32 decode_nal_sei_message(HEVCContext *s) @@ -195,7 +195,7 @@ static RK_S32 decode_nal_sei_message(HEVCContext *s) return 1; } __BITREAD_ERR: - return MPP_ERR_STREAM; + return MPP_ERR_STREAM; } static RK_S32 more_rbsp_data(BitReadCtx_t *gb) diff --git a/mpp/codec/dec/h265/test/h265d_parser_test.c b/mpp/codec/dec/h265/test/h265d_parser_test.c index 570b3b4d..d1421f18 100644 --- a/mpp/codec/dec/h265/test/h265d_parser_test.c +++ b/mpp/codec/dec/h265/test/h265d_parser_test.c @@ -382,10 +382,10 @@ RK_S32 hevc_parser_test(ParserDemoCmdContext_t *cmd) } if (cutask->valid) { HalTaskInfo syn; + MppBuffer buffer = NULL; + syn.dec = *cutask; index = cutask->output; - - MppBuffer buffer = NULL; mpp_buf_slot_get_prop(slots, index, SLOT_BUFFER, &buffer); if (NULL == buffer) { RK_U32 size = mpp_buf_slot_get_size(slots); diff --git a/mpp/codec/inc/mpp_buf_slot.h b/mpp/codec/inc/mpp_buf_slot.h index c81f0eb0..e2473632 100644 --- a/mpp/codec/inc/mpp_buf_slot.h +++ b/mpp/codec/inc/mpp_buf_slot.h @@ -131,7 +131,7 @@ RK_U32 mpp_buf_slot_get_size(MppBufSlots slots); * called by parser * * mpp_buf_slot_get_unused - * - parser need a new slot ffor output, on field mode alloc one buffer for two field + * - parser need a new slot for output, on field mode alloc one buffer for two field * * mpp_buf_slot_set_dpb_ref * - mark a slot to be used as reference frame in dpb diff --git a/mpp/common/h264d_log.h b/mpp/common/h264d_log.h index 4934f181..e54ed5ea 100644 --- a/mpp/common/h264d_log.h +++ b/mpp/common/h264d_log.h @@ -126,26 +126,26 @@ typedef struct h264d_logctx_t { #define LogTrace(ctx, ...)\ do{ if(LogEnable(ctx, LOG_LEVEL_TRACE)) {\ - writelog(ctx, "TRACE", __FILE__, __LINE__, ##__VA_ARGS__);\ + writelog(ctx, "TRACE", __FILE__, __LINE__, ##__VA_ARGS__);\ } }while (0) #define LogInfo(ctx, ...)\ do{ if(LogEnable(ctx, LOG_LEVEL_INFO)) {\ - writelog(ctx, "INFO",__FILE__, __LINE__, ##__VA_ARGS__);\ + writelog(ctx, "INFO",__FILE__, __LINE__, ##__VA_ARGS__);\ } }while (0) #define LogWarnning(ctx, ...)\ do{ if(LogEnable(ctx, LOG_LEVEL_WARNNING)) {\ - writelog(ctx, "WARNNING",__FILE__, __LINE__, ##__VA_ARGS__);\ + writelog(ctx, "WARNNING",__FILE__, __LINE__, ##__VA_ARGS__);\ } }while (0) #define LogError(ctx, ...)\ do{ if(LogEnable(ctx, LOG_LEVEL_ERROR)) {\ - writelog(ctx, "ERROR",__FILE__, __LINE__, ##__VA_ARGS__);\ + writelog(ctx, "ERROR",__FILE__, __LINE__, ##__VA_ARGS__);\ ASSERT(0);\ } }while (0) #define LogFatal(ctx, ...)\ do{ if(LogEnable(ctx, LOG_LEVEL_ERROR)) {\ - writelog(ctx, "FATAL", __FILE__, __LINE__, ##__VA_ARGS__);\ + writelog(ctx, "FATAL", __FILE__, __LINE__, ##__VA_ARGS__);\ ASSERT(0);\ } }while (0) @@ -165,7 +165,7 @@ typedef struct h264d_logctx_t { ret = MPP_ERR_VALUE;\ mpp_log("ERROR: value error.\n");\ goto __FAILED;\ - } } while (0) + } } while (0) //!< memory malloc check #define MEM_CHECK(ret, val)\ do{ if(!(val)) {\ @@ -196,26 +196,27 @@ typedef struct h264d_logctx_t { #define CHECK_RANGE(bitctx, val, _min, _max)\ - do {\ - if ((val) < (_min) || (val) > (_max)) {\ - mpp_log("%d[%d,%d]", val, _min, _max);\ - goto __BITREAD_ERR;\ - }\ - } while (0) + do {\ + if ((val) < (_min) || (val) > (_max)) {\ + mpp_log("%d[%d,%d]", val, _min, _max);\ + goto __BITREAD_ERR;\ + }\ + } while (0) #define CHECK_ERROR(bitctx, val)\ - do {\ - if (!(val)) {\ - mpp_log("value false");\ - ASSERT(0);\ - goto __BITREAD_ERR;\ - }\ - } while (0) + do {\ + if (!(val)) {\ + mpp_log("value false");\ + ASSERT(0);\ + goto __BITREAD_ERR;\ + }\ + } while (0) #define FPRINT(fp, ...) { if (fp) { fprintf(fp, ##__VA_ARGS__); fflush(fp);} } -extern RK_U32 g_nalu_cnt; +extern RK_U32 g_nalu_cnt0; +extern RK_U32 g_nalu_cnt1; extern RK_S32 g_max_bytes; extern RK_U32 g_max_slice_data; extern FILE *g_debug_file0; @@ -235,7 +236,6 @@ MPP_RET get_logenv(LogEnv_t *env); MPP_RET explain_ctrl_flag(RK_U32 ctrl_val, LogFlag_t *pflag); void set_log_outpath(LogEnv_t *env); - void set_bitread_logctx(BitReadCtx_t *bitctx, LogCtx_t *p_ctx); void writelog(void *ctx, ...); diff --git a/mpp/hal/rkdec/h264d/hal_h264d_api.c b/mpp/hal/rkdec/h264d/hal_h264d_api.c index 90f1121b..e82092c8 100644 --- a/mpp/hal/rkdec/h264d/hal_h264d_api.c +++ b/mpp/hal/rkdec/h264d/hal_h264d_api.c @@ -215,7 +215,7 @@ MPP_RET hal_h264d_gen_regs(void *hal, HalTaskInfo *task) prepare_stream_packet(hal, &p_hal->pkts->strm); generate_regs(p_hal, &p_hal->pkts->reg); - //mpp_log("++++++++++ hal_h264_decoder, g_framecnt=%d \n", p_hal->g_framecnt++); + mpp_log("++++++++++ hal_h264_decoder, g_framecnt=%d \n", p_hal->g_framecnt++); ((HalDecTask*)&task->dec)->valid = 0; FunctionOut(p_hal->logctx.parr[RUN_HAL]); diff --git a/mpp/hal/rkdec/h265d/hal_h265d_reg.c b/mpp/hal/rkdec/h265d/hal_h265d_reg.c index 2308f1df..ad94839b 100644 --- a/mpp/hal/rkdec/h265d/hal_h265d_reg.c +++ b/mpp/hal/rkdec/h265d/hal_h265d_reg.c @@ -684,7 +684,7 @@ static RK_S32 hal_h265d_slice_output_rps(void *dxva, void *rps_buf) // mpp_err("data[%d]= 0x%x,size[%d] = %d \n", // k,dxva_cxt->slice_short[k].BSNALunitDataLocation, k,dxva_cxt->slice_short[k].SliceBytesInBuffer); mpp_set_bitread_ctx(&gb_cxt, (RK_U8*)(dxva_cxt->bitstream + dxva_cxt->slice_short[k].BSNALunitDataLocation), - dxva_cxt->slice_short[k].SliceBytesInBuffer); + dxva_cxt->slice_short[k].SliceBytesInBuffer); gb = &gb_cxt; @@ -983,7 +983,7 @@ static RK_S32 hal_h265d_slice_output_rps(void *dxva, void *rps_buf) return 0; __BITREAD_ERR: - return MPP_ERR_STREAM; + return MPP_ERR_STREAM; } static void hal_h265d_output_scalinglist_packet(void *ptr, void *dxva) diff --git a/mpp/mpi.cpp b/mpp/mpi.cpp index 832ec00e..2cc650e8 100644 --- a/mpp/mpi.cpp +++ b/mpp/mpi.cpp @@ -139,14 +139,14 @@ static MPP_RET mpi_control(MppCtx ctx, MpiCmd cmd, MppParam param) MPI_FUNCTION_ENTER(); MpiImpl *p = (MpiImpl *)ctx; switch (cmd) { - case MPP_DEC_SET_EXT_BUF_GROUP: { - mpp_log("mpi_control group %p", param); - p->ctx->mFrameGroup = (MppBufferGroup)param; - break; - } - default: { - break; - } + case MPP_DEC_SET_EXT_BUF_GROUP: { + mpp_log("mpi_control group %p", param); + p->ctx->mFrameGroup = (MppBufferGroup)param; + break; + } + default: { + break; + } } MPI_FUNCTION_LEAVE(); diff --git a/mpp/mpp.cpp b/mpp/mpp.cpp index c5e7e2c3..c4bde1d7 100644 --- a/mpp/mpp.cpp +++ b/mpp/mpp.cpp @@ -65,12 +65,12 @@ Mpp::Mpp(MppCtxType type, MppCodingType coding) mThreadCodec = new MppThread(mpp_dec_parser_thread, this); mThreadHal = new MppThread(mpp_dec_hal_thread, this); - mpp_buffer_group_normal_get(&mInternalGroup, MPP_BUFFER_TYPE_ION); + mpp_buffer_group_get_internal(&mInternalGroup, MPP_BUFFER_TYPE_ION); - mpp_buffer_group_normal_get(&mPacketGroup, MPP_BUFFER_TYPE_ION); + mpp_buffer_group_get_internal(&mPacketGroup, MPP_BUFFER_TYPE_ION); mpp_buffer_group_limit_config(mPacketGroup, MPP_TEST_PACKET_SIZE, 4); - mpp_buffer_group_normal_get(&mFrameGroup, MPP_BUFFER_TYPE_ION); + mpp_buffer_group_get_internal(&mFrameGroup, MPP_BUFFER_TYPE_ION); mpp_buffer_group_limit_config(mFrameGroup, MPP_TEST_FRAME_SIZE, 4); } break; case MPP_CTX_ENC : { @@ -82,9 +82,9 @@ Mpp::Mpp(MppCtxType type, MppCodingType coding) mThreadCodec = new MppThread(mpp_enc_control_thread, this); mThreadHal = new MppThread(mpp_dec_hal_thread, this); - mpp_buffer_group_normal_get(&mInternalGroup, MPP_BUFFER_TYPE_ION); - mpp_buffer_group_normal_get(&mPacketGroup, MPP_BUFFER_TYPE_NORMAL); - mpp_buffer_group_limited_get(&mFrameGroup, MPP_BUFFER_TYPE_ION); + mpp_buffer_group_get_internal(&mInternalGroup, MPP_BUFFER_TYPE_ION); + mpp_buffer_group_get_internal(&mPacketGroup, MPP_BUFFER_TYPE_NORMAL); + mpp_buffer_group_get_external(&mFrameGroup, MPP_BUFFER_TYPE_ION); } break; default : { mpp_err("Mpp error type %d\n", mType); diff --git a/mpp/mpp_bitread.c b/mpp/mpp_bitread.c index 70267b4f..b592f20a 100644 --- a/mpp/mpp_bitread.c +++ b/mpp/mpp_bitread.c @@ -1,18 +1,18 @@ -/* -* -* Copyright 2015 Rockchip Electronics Co. LTD -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. +/* +* +* Copyright 2015 Rockchip Electronics Co. LTD +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. */ #include @@ -45,20 +45,20 @@ static MPP_RET update_curbyte(BitReadCtx_t *bitctx) bitctx->num_remaining_bits_in_curr_byte_ = 8; bitctx->prev_two_bytes_ = (bitctx->prev_two_bytes_ << 8) | bitctx->curr_byte_; - return MPP_OK; + return MPP_OK; } static void log_info(void *ctx, ...) { - (void)ctx; + (void)ctx; } -/*! -*********************************************************************** -* \brief -* Read |num_bits| (1 to 31 inclusive) from the stream and return them -* in |out|, with first bit in the stream as MSB in |out| at position -* (|num_bits| - 1) -*********************************************************************** +/*! +*********************************************************************** +* \brief +* Read |num_bits| (1 to 31 inclusive) from the stream and return them +* in |out|, with first bit in the stream as MSB in |out| at position +* (|num_bits| - 1) +*********************************************************************** */ MPP_RET mpp_read_bits(BitReadCtx_t *bitctx, RK_S32 num_bits, RK_S32 *out) { @@ -82,11 +82,11 @@ MPP_RET mpp_read_bits(BitReadCtx_t *bitctx, RK_S32 num_bits, RK_S32 *out) return MPP_OK; } -/*! -*********************************************************************** -* \brief -* read more than 32 bits data -*********************************************************************** +/*! +*********************************************************************** +* \brief +* read more than 32 bits data +*********************************************************************** */ MPP_RET mpp_read_longbits(BitReadCtx_t *bitctx, RK_S32 num_bits, RK_U32 *out) { @@ -102,11 +102,11 @@ MPP_RET mpp_read_longbits(BitReadCtx_t *bitctx, RK_S32 num_bits, RK_U32 *out) return MPP_OK; } -/*! -*********************************************************************** -* \brief -* skip bits -*********************************************************************** +/*! +*********************************************************************** +* \brief +* skip bits +*********************************************************************** */ MPP_RET mpp_skip_bits(BitReadCtx_t *bitctx, RK_S32 num_bits) { @@ -124,11 +124,11 @@ MPP_RET mpp_skip_bits(BitReadCtx_t *bitctx, RK_S32 num_bits) return MPP_OK; } -/*! -*********************************************************************** -* \brief -* read unsigned data -*********************************************************************** +/*! +*********************************************************************** +* \brief +* read unsigned data +*********************************************************************** */ MPP_RET mpp_read_ue(BitReadCtx_t *bitctx, RK_U32 *val) @@ -157,11 +157,11 @@ MPP_RET mpp_read_ue(BitReadCtx_t *bitctx, RK_U32 *val) return MPP_OK; } -/*! -*********************************************************************** -* \brief -* read signed data -*********************************************************************** +/*! +*********************************************************************** +* \brief +* read signed data +*********************************************************************** */ MPP_RET mpp_read_se(BitReadCtx_t *bitctx, RK_S32 *val) { @@ -178,11 +178,11 @@ MPP_RET mpp_read_se(BitReadCtx_t *bitctx, RK_S32 *val) return MPP_OK; } -/*! -*********************************************************************** -* \brief -* check whether has more rbsp data -*********************************************************************** +/*! +*********************************************************************** +* \brief +* check whether has more rbsp data +*********************************************************************** */ RK_U32 mpp_has_more_rbsp_data(BitReadCtx_t *bitctx) { @@ -199,11 +199,11 @@ RK_U32 mpp_has_more_rbsp_data(BitReadCtx_t *bitctx) return (bitctx->curr_byte_ & ((1 << (bitctx->num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0; } -/*! -*********************************************************************** -* \brief -* initialize bit read contex -*********************************************************************** +/*! +*********************************************************************** +* \brief +* initialize bit read contex +*********************************************************************** */ void mpp_set_bitread_ctx(BitReadCtx_t *bitctx, RK_U8 *data, RK_S32 size) { @@ -218,14 +218,14 @@ void mpp_set_bitread_ctx(BitReadCtx_t *bitctx, RK_U8 *data, RK_S32 size) bitctx->buf = data; bitctx->buf_len = size; bitctx->used_bits = 0; - bitctx->wlog = log_info; + bitctx->wlog = log_info; } -/*! -*********************************************************************** -* \brief -* get current data value -*********************************************************************** +/*! +*********************************************************************** +* \brief +* get current data value +*********************************************************************** */ RK_U8 mpp_get_curdata_value(BitReadCtx_t *bitctx) { diff --git a/mpp/test/h264d_rwfile.c b/mpp/test/h264d_rwfile.c index ffa7e1c5..b5f98496 100644 --- a/mpp/test/h264d_rwfile.c +++ b/mpp/test/h264d_rwfile.c @@ -743,11 +743,10 @@ __FAILED: * read one frame *********************************************************************** */ -MPP_RET h264d_read_one_frame(InputParams *p_in, MppPacket pkt) +MPP_RET h264d_read_one_frame(InputParams *p_in) { p_in->strm.strmbytes = 0; p_in->is_new_frame = 0; - //-- copy first nalu if (!p_in->is_fist_frame) { write_nalu_prefix(p_in); @@ -763,15 +762,6 @@ MPP_RET h264d_read_one_frame(InputParams *p_in, MppPacket pkt) //FPRINT(g_debug_file0, "--- new frame ---- \n"); //} - //-- set code input context - ((MppPacketImpl *)pkt)->pos = p_in->strm.pbuf; - ((MppPacketImpl *)pkt)->size = p_in->strm.strmbytes; - if (g_max_slice_data < p_in->strm.strmbytes) { - g_max_slice_data = p_in->strm.strmbytes; - } - if (p_in->is_eof) { - mpp_packet_set_eos(pkt); - } return MPP_OK; } /*! diff --git a/mpp/test/h264d_rwfile.h b/mpp/test/h264d_rwfile.h index ede16df7..86d29e6f 100644 --- a/mpp/test/h264d_rwfile.h +++ b/mpp/test/h264d_rwfile.h @@ -18,7 +18,7 @@ #include #include "rk_type.h" #include "mpp_err.h" -#include "mpp_packet.h" + #ifndef __H264D_RWFILE_H__ #define __H264D_RWFILE_H__ @@ -70,7 +70,7 @@ MPP_RET h264d_configure (InputParams *in, RK_S32 ac, char *av[]); MPP_RET h264d_open_files (InputParams *in); MPP_RET h264d_close_files(InputParams *in); MPP_RET h264d_alloc_frame_buffer(InputParams *in); -MPP_RET h264d_read_one_frame (InputParams *in, MppPacket pkt); +MPP_RET h264d_read_one_frame (InputParams *in); MPP_RET h264d_free_frame_buffer (InputParams *in); MPP_RET h264d_write_fpga_data (InputParams *in); diff --git a/mpp/test/h264d_test.c b/mpp/test/h264d_test.c index cdee9323..09b59e1a 100644 --- a/mpp/test/h264d_test.c +++ b/mpp/test/h264d_test.c @@ -16,6 +16,7 @@ #define MODULE_TAG "h264d_test" + #if defined(_WIN32) #include "vld.h" #endif @@ -32,24 +33,22 @@ #include "h264d_log.h" #include "h264d_rwfile.h" #include "h264d_api.h" +#include "hal_task.h" #include "hal_h264d_api.h" -#define MODULE_TAG "h264d_test" - - static MPP_RET manual_set_env(void) { #if defined(_MSC_VER) mpp_env_set_u32("h264d_log_help", 1 ); mpp_env_set_u32("h264d_log_show", 1 ); - mpp_env_set_u32("h264d_log_ctrl", 0x040B ); + mpp_env_set_u32("h264d_log_ctrl", 0x100B ); mpp_env_set_u32("h264d_log_level", 5 ); mpp_env_set_u32("h264d_log_decframe", 0 ); mpp_env_set_u32("h264d_log_begframe", 0 ); mpp_env_set_u32("h264d_log_endframe", 0 ); mpp_env_set_str("h264d_log_outpath", "F:/h264_log" ); - mpp_env_set_str("h264d_log_cmppath", "F:/h264_log" ); + mpp_env_set_str("h264d_log_cmppath", "F:/h264_log_driver/trunk_dat" ); #endif return MPP_OK; } @@ -78,22 +77,25 @@ static MPP_RET decoder_init(MppDec *pApi) MPP_RET ret = MPP_ERR_UNKNOW; ParserCfg parser_cfg; MppHalCfg hal_cfg; - MppCodingType coding = MPP_VIDEO_CodingAVC; + pApi->coding = MPP_VIDEO_CodingAVC; // malloc slot FUN_CHECK(ret = mpp_buf_slot_init(&pApi->frame_slots)); MEM_CHECK(ret, pApi->frame_slots); - // init parser part memset(&parser_cfg, 0, sizeof(parser_cfg)); - parser_cfg.coding = coding; - parser_cfg.frame_slots = pApi->frame_slots; + parser_cfg.coding = pApi->coding; + parser_cfg.frame_slots = pApi->frame_slots; + parser_cfg.packet_slots = pApi->packet_slots; FUN_CHECK(ret = parser_init(&pApi->parser, &parser_cfg)); // init hal part memset(&hal_cfg, 0, sizeof(hal_cfg)); hal_cfg.type = MPP_CTX_DEC; - hal_cfg.coding = coding; + hal_cfg.coding = pApi->coding; + hal_cfg.mode = HAL_MODE_LIBVPU; + hal_cfg.frame_slots = pApi->frame_slots; + hal_cfg.packet_slots = pApi->packet_slots; hal_cfg.task_count = parser_cfg.task_count; FUN_CHECK(ret = mpp_hal_init(&pApi->hal, &hal_cfg)); pApi->tasks = hal_cfg.tasks; @@ -112,13 +114,24 @@ int main(int argc, char **argv) //FILE *g_debug_file = NULL; //RK_S32 m_max_bytes = 0; //RK_S32 m_max_slice_data = 0; - MPP_RET ret = MPP_ERR_UNKNOW; + MPP_RET ret = MPP_ERR_UNKNOW; + MppPacket pkt = NULL; InputParams *pIn = mpp_calloc(InputParams, 1); MppDec *pApi = mpp_calloc(MppDec, 1); - MppPacketImpl *pkt = mpp_calloc_size(MppPacketImpl, sizeof(MppPacketImpl)); HalTaskInfo *task = mpp_calloc_size(HalTaskInfo, sizeof(HalTaskInfo)); - MEM_CHECK(ret, pIn && pApi && pkt && task); + RK_U32 end_of_flag = 0; + + //if (g_debug_file0 == NULL) { + // g_debug_file0 = fopen("rk_debugfile_view0.txt", "wb"); + //} + + //if (g_debug_file1 == NULL) { + // g_debug_file1 = fopen("rk_debugfile_view1.txt", "wb"); + //} + + + MEM_CHECK(ret, pIn && pApi && task); mpp_log("== test start == \n"); // set debug mode FUN_CHECK(ret = manual_set_env()); @@ -127,43 +140,61 @@ int main(int argc, char **argv) // open files FUN_CHECK(ret = h264d_open_files(pIn)); FUN_CHECK(ret = h264d_alloc_frame_buffer(pIn)); - // init + + //!< init decoder FUN_CHECK(ret = decoder_init(pApi)); do { - if (!pkt->size) { - FUN_CHECK(ret = h264d_read_one_frame(pIn, (MppPacket)pkt)); - //if (g_debug_file1 == NULL) - //{ - // g_debug_file1 = fopen("rk_debugfile_view1.txt", "wb"); - //} - //FPRINT(g_debug_file1, "---- read_one_frame Frame_no = %d, size = %d \n", pIn->iFrmdecoded, pkt->size); + //!< get one packet + if (pkt == NULL) { + if (pIn->is_eof) { + mpp_packet_init(&pkt, NULL, 0); + mpp_packet_set_eos(pkt); + } else { + FUN_CHECK(ret = h264d_read_one_frame(pIn)); + mpp_packet_init(&pkt, pIn->strm.pbuf, pIn->strm.strmbytes); + } mpp_log("---- decoder, read_one_frame Frame_no = %d \n", pIn->iFrmdecoded++); } + //!< initial task + memset(task, 0, sizeof(HalTaskInfo)); + memset(task->dec.refer, -1, sizeof(task->dec.refer)); + //!< prepare FUN_CHECK(ret = parser_prepare(pApi->parser, pkt, &task->dec)); - FUN_CHECK(ret = parser_parse(pApi->parser, &task->dec)); - //FPRINT(g_debug_file1, "parse, frame_no = %d, size = %d \n", pIn->iFrmdecoded, pkt->size); - if (((HalDecTask*)&task->dec)->valid) { - FUN_CHECK(ret = mpp_hal_reg_gen(pApi->hal, task)); - //!< end of stream - if (!pkt->size && (pkt->flag & MPP_PACKET_FLAG_EOS)) { - break; + //!< parse + if (task->dec.valid) { + task->dec.valid = 0; + FUN_CHECK(ret = parser_parse(pApi->parser, &task->dec)); + } + //!< deinit packet + if (mpp_packet_get_length(pkt) == 0) { + if (mpp_packet_get_eos(pkt)) { + end_of_flag = 1; //!< end of stream } - //pIn->iFrmdecoded++; + mpp_packet_deinit(&pkt); + pkt = NULL; + } + //!< run hal module + if (task->dec.valid) { + FUN_CHECK(ret = mpp_hal_reg_gen(pApi->hal, task)); + FUN_CHECK(ret = mpp_hal_hw_start(pApi->hal, task)); + FUN_CHECK(ret = mpp_hal_hw_wait(pApi->hal, task)); + //!< write frame out + } + if (end_of_flag) { + break; } } while (!pIn->iDecFrmNum || (pIn->iFrmdecoded < pIn->iDecFrmNum)); - + //!< flush dpb and send to display + FUN_CHECK(ret = mpp_dec_flush(pApi)); mpp_log("+++++++ all test return +++++++ \n"); ret = MPP_OK; __FAILED: - - decoder_deinit(pApi); h264d_free_frame_buffer(pIn); h264d_write_fpga_data(pIn); //!< for fpga debug h264d_close_files(pIn); MPP_FREE(pIn); MPP_FREE(pApi); - MPP_FREE(pkt); MPP_FREE(task); //g_debug_file = fopen("rk_debugfile_view0.txt", "rb"); diff --git a/osal/mpp_allocator.cpp b/osal/mpp_allocator.cpp index ed93dab0..b5a47eca 100644 --- a/osal/mpp_allocator.cpp +++ b/osal/mpp_allocator.cpp @@ -30,7 +30,7 @@ MPP_RET mpp_allocator_alloc(MppAllocator allocator, MppBufferInfo *info) { if (NULL == allocator || NULL == info) { mpp_err_f("invalid input: allocator %p info %p\n", - allocator, info); + allocator, info); return MPP_ERR_UNKNOW; } @@ -48,7 +48,7 @@ MPP_RET mpp_allocator_free(MppAllocator allocator, MppBufferInfo *info) { if (NULL == allocator || NULL == info) { mpp_err_f("invalid input: allocator %p info %p\n", - allocator, info); + allocator, info); return MPP_ERR_UNKNOW; }