From 781c812ce58f8ce82c68c40da86efc27b499101a Mon Sep 17 00:00:00 2001 From: ChenHengming Date: Tue, 19 Jul 2016 10:40:26 +0000 Subject: [PATCH] [frame/packet]: increase mpp_buffer reference count when mpp_buffer is set to mpp_frame or mpp_packet git-svn-id: https://10.10.10.66:8443/svn/MediaProcessPlatform/trunk/mpp@1045 6e48237b-75ef-9749-8fc9-41990f28c85a --- mpp/base/mpp_buf_slot.cpp | 13 ++++++------ mpp/base/mpp_frame.cpp | 27 ++++++++++++++++++++++++- mpp/base/mpp_packet.cpp | 14 +++++++------ mpp/codec/mpp_dec.cpp | 22 +++------------------ mpp/legacy/vpu_api_legacy.cpp | 37 ++++++++++++++++++++++------------- 5 files changed, 67 insertions(+), 46 deletions(-) diff --git a/mpp/base/mpp_buf_slot.cpp b/mpp/base/mpp_buf_slot.cpp index 22e12f8d..d36284e5 100644 --- a/mpp/base/mpp_buf_slot.cpp +++ b/mpp/base/mpp_buf_slot.cpp @@ -448,12 +448,13 @@ static void check_entry_unused(MppBufSlotsImpl *impl, MppBufSlotEntry *entry) if (entry->frame) { mpp_frame_deinit(&entry->frame); slot_ops_with_log(impl, entry, SLOT_CLR_FRAME); - } else { - if (entry->buffer) - mpp_buffer_put(entry->buffer); + } + if (entry->buffer) { + mpp_buffer_put(entry->buffer); + entry->buffer = NULL; + slot_ops_with_log(impl, entry, SLOT_CLR_BUFFER); } - slot_ops_with_log(impl, entry, SLOT_CLR_BUFFER); slot_ops_with_log(impl, entry, SLOT_CLR_ON_USE); } } @@ -831,11 +832,11 @@ MPP_RET mpp_buf_slot_set_prop(MppBufSlots slots, RK_S32 index, SlotPropType type slot_assert(impl, NULL == slot->frame); mpp_buffer_put(slot->buffer); } + mpp_buffer_inc_ref(buffer); slot->buffer = buffer; + if (slot->frame) mpp_frame_set_buffer(slot->frame, buffer); - - mpp_buffer_inc_ref(buffer); } break; default : { } break; diff --git a/mpp/base/mpp_frame.cpp b/mpp/base/mpp_frame.cpp index 8ee04ae7..baecc6cc 100644 --- a/mpp/base/mpp_frame.cpp +++ b/mpp/base/mpp_frame.cpp @@ -93,6 +93,32 @@ MPP_RET mpp_frame_set_next(MppFrame frame, MppFrame next) return MPP_OK; } +MppBuffer mpp_frame_get_buffer(MppFrame frame) +{ + if (check_is_mpp_frame(frame)) + return NULL; + + MppFrameImpl *p = (MppFrameImpl *)frame; + return (MppFrame)p->buffer; +} + +void mpp_frame_set_buffer(MppFrame frame, MppBuffer buffer) +{ + if (check_is_mpp_frame(frame)) + return ; + + MppFrameImpl *p = (MppFrameImpl *)frame; + if (p->buffer != buffer) { + if (buffer) + mpp_buffer_inc_ref(buffer); + + if (p->buffer) + mpp_buffer_put(p->buffer); + + p->buffer = buffer; + } +} + MPP_RET mpp_frame_copy(MppFrame dst, MppFrame src) { if (NULL == dst || check_is_mpp_frame(src)) { @@ -163,6 +189,5 @@ MPP_FRAME_ACCESSORS(MppFrameColorTransferCharacteristic, color_trc) MPP_FRAME_ACCESSORS(MppFrameColorSpace, colorspace) MPP_FRAME_ACCESSORS(MppFrameChromaLocation, chroma_location) MPP_FRAME_ACCESSORS(MppFrameFormat, fmt) -MPP_FRAME_ACCESSORS(MppBuffer, buffer) MPP_FRAME_ACCESSORS(size_t, buf_size) MPP_FRAME_ACCESSORS(RK_U32, errinfo) diff --git a/mpp/base/mpp_packet.cpp b/mpp/base/mpp_packet.cpp index cb8ac5e2..1e1750a7 100644 --- a/mpp/base/mpp_packet.cpp +++ b/mpp/base/mpp_packet.cpp @@ -233,12 +233,14 @@ void mpp_packet_set_buffer(MppPacket packet, MppBuffer buffer) return ; MppPacketImpl *p = (MppPacketImpl *)packet; - if (p->buffer) { - mpp_buffer_put(p->buffer); - } - p->buffer = buffer; - if (buffer) { - mpp_buffer_inc_ref(buffer); + if (p->buffer != buffer) { + if (buffer) + mpp_buffer_inc_ref(buffer); + + if (p->buffer) + mpp_buffer_put(p->buffer); + + p->buffer = buffer; } } diff --git a/mpp/codec/mpp_dec.cpp b/mpp/codec/mpp_dec.cpp index f22908a0..6284becf 100644 --- a/mpp/codec/mpp_dec.cpp +++ b/mpp/codec/mpp_dec.cpp @@ -205,11 +205,6 @@ static RK_U32 reset_dec_task(Mpp *mpp, DecTask *task) mpp_buf_slot_clr_flag(frame_slots, index, SLOT_QUEUE_USE); } if (task->status.dec_pkt_copy_rdy) { - mpp_buf_slot_get_prop(packet_slots, task_dec->input, SLOT_BUFFER, &task->hal_pkt_buf_in); - if (task->hal_pkt_buf_in) { - mpp_buffer_put(task->hal_pkt_buf_in); - task->hal_pkt_buf_in = NULL; - } mpp_buf_slot_clr_flag(packet_slots, task_dec->input, SLOT_HAL_INPUT); task->status.dec_pkt_copy_rdy = 0; task_dec->input = -1; @@ -400,8 +395,10 @@ static MPP_RET try_proc_dec_task(Mpp *mpp, DecTask *task) mpp_buf_slot_get_prop(packet_slots, task->hal_pkt_idx_in, SLOT_BUFFER, &hal_buf_in); if (NULL == hal_buf_in) { mpp_buffer_get(mpp->mPacketGroup, &hal_buf_in, stream_size); - if (hal_buf_in) + if (hal_buf_in) { mpp_buf_slot_set_prop(packet_slots, task->hal_pkt_idx_in, SLOT_BUFFER, hal_buf_in); + mpp_buffer_put(hal_buf_in); + } } else { MppBufferImpl *buf = (MppBufferImpl *)hal_buf_in; mpp_assert(buf->info.size >= stream_size); @@ -526,11 +523,6 @@ static MPP_RET try_proc_dec_task(Mpp *mpp, DecTask *task) mpp->mTaskPutCount++; task->hnd = NULL; if (task->status.dec_pkt_copy_rdy) { - mpp_buf_slot_get_prop(packet_slots, task_dec->input, SLOT_BUFFER, &task->hal_pkt_buf_in); - if (task->hal_pkt_buf_in) { - mpp_buffer_put(task->hal_pkt_buf_in); - task->hal_pkt_buf_in = NULL; - } mpp_buf_slot_clr_flag(packet_slots, task_dec->input, SLOT_HAL_INPUT); task->status.dec_pkt_copy_rdy = 0; } @@ -659,8 +651,6 @@ void *mpp_dec_parser_thread(void *data) mpp_buf_slot_set_flag(packet_slots, task_dec->input, SLOT_CODEC_READY); mpp_buf_slot_set_flag(packet_slots, task_dec->input, SLOT_HAL_INPUT); mpp_buf_slot_clr_flag(packet_slots, task_dec->input, SLOT_HAL_INPUT); - if (task.hal_pkt_buf_in) - mpp_buffer_put(task.hal_pkt_buf_in); } mpp_buffer_group_clear(mpp->mPacketGroup); mpp_log("mpp_dec_parser_thread exit ok"); @@ -673,7 +663,6 @@ void *mpp_dec_hal_thread(void *data) MppThread *hal = mpp->mThreadHal; MppDec *dec = mpp->mDec; HalTaskGroup tasks = dec->tasks; - MppBuffer buffer = NULL; MppBufSlots frame_slots = dec->frame_slots; MppBufSlots packet_slots = dec->packet_slots; @@ -751,11 +740,6 @@ void *mpp_dec_hal_thread(void *data) * 3. add frame to output list * repeat 2 and 3 until not frame can be output */ - mpp_buf_slot_get_prop(packet_slots, task_dec->input, SLOT_BUFFER, &buffer); - if (buffer) { - mpp_buffer_put(buffer); - buffer = NULL; - } mpp_buf_slot_clr_flag(packet_slots, task_dec->input, SLOT_HAL_INPUT); // TODO: may have risk here diff --git a/mpp/legacy/vpu_api_legacy.cpp b/mpp/legacy/vpu_api_legacy.cpp index 8d9e7605..a091d4c7 100644 --- a/mpp/legacy/vpu_api_legacy.cpp +++ b/mpp/legacy/vpu_api_legacy.cpp @@ -175,17 +175,16 @@ RK_S32 VpuApiLegacy:: decode_getoutframe(DecoderOut_t *aDecOut) if (ret || NULL == mframe) { aDecOut->size = 0; } else { - MppBuffer buf = NULL; - RK_U64 pts = 0; - RK_U32 fd = 0; - RK_U32 mode = 0; - void* ptr = NULL; + MppBuffer buf = mpp_frame_get_buffer(mframe); + RK_U64 pts = mpp_frame_get_pts(mframe); + RK_U32 mode = mpp_frame_get_mode(mframe); + RK_S32 fd = -1; + aDecOut->size = sizeof(VPU_FRAME); vframe->DisplayWidth = mpp_frame_get_width(mframe); vframe->DisplayHeight = mpp_frame_get_height(mframe); vframe->FrameWidth = mpp_frame_get_hor_stride(mframe); vframe->FrameHeight = mpp_frame_get_ver_stride(mframe); - mode = mpp_frame_get_mode(mframe); if (mode == MPP_FRAME_FLAG_FRAME) vframe->FrameType = 0; else { @@ -198,11 +197,9 @@ RK_S32 VpuApiLegacy:: decode_getoutframe(DecoderOut_t *aDecOut) vframe->FrameType = 4; } vframe->ErrorInfo = mpp_frame_get_errinfo(mframe) | mpp_frame_get_discard(mframe); - pts = mpp_frame_get_pts(mframe); aDecOut->timeUs = pts; vframe->ShowTime.TimeHigh = (RK_U32)(pts >> 32); vframe->ShowTime.TimeLow = (RK_U32)pts; - buf = mpp_frame_get_buffer(mframe); switch (mpp_frame_get_fmt(mframe)) { case MPP_FMT_YUV420SP: { vframe->ColorType = VPU_OUTPUT_FORMAT_YUV420_SEMIPLANAR; @@ -230,12 +227,16 @@ RK_S32 VpuApiLegacy:: decode_getoutframe(DecoderOut_t *aDecOut) break; } if (buf) { - ptr = mpp_buffer_get_ptr(buf); + void* ptr = mpp_buffer_get_ptr(buf); + + mpp_buffer_inc_ref(buf); fd = mpp_buffer_get_fd(buf); + vframe->FrameBusAddr[0] = fd; vframe->FrameBusAddr[1] = fd; vframe->vpumem.vir_addr = (RK_U32*)ptr; frame_count++; + //!< Dump yuv if (fp && !vframe->ErrorInfo) { if ((vframe->FrameWidth >= 1920) || (vframe->FrameHeight >= 1080)) { @@ -281,8 +282,11 @@ RK_S32 VpuApiLegacy:: decode_getoutframe(DecoderOut_t *aDecOut) vframe->vpumem.offset = (RK_U32*)buf; } if (vpu_api_debug & VPU_API_DBG_OUTPUT) { - mpp_log("get one frame timeUs %lld, fd=0x%x, poc=%d, errinfo=%d, discard=%d, eos=%d, verr=%d", aDecOut->timeUs, fd, - mpp_frame_get_poc(mframe), mpp_frame_get_errinfo(mframe), mpp_frame_get_discard(mframe), mpp_frame_get_eos(mframe), vframe->ErrorInfo); + mpp_log("get one frame pts %lld, fd 0x%x, poc %d, errinfo %x, discard %d, eos %d, verr %d", + aDecOut->timeUs, fd, mpp_frame_get_poc(mframe), + mpp_frame_get_errinfo(mframe), + mpp_frame_get_discard(mframe), + mpp_frame_get_eos(mframe), vframe->ErrorInfo); } if (mpp_frame_get_eos(mframe)) { set_eos = 1; @@ -292,12 +296,11 @@ RK_S32 VpuApiLegacy:: decode_getoutframe(DecoderOut_t *aDecOut) } /* - * IMPORTANT: mframe is malloced frome mpi->decode_get_frame + * IMPORTANT: mframe is malloced from mpi->decode_get_frame * So we need to deinit mframe here. But the buffer in the frame should not be free with mframe. * Because buffer need to be set to vframe->vpumem.offset and send to display. * The we have to clear the buffer pointer in mframe then release mframe. */ - mpp_frame_set_buffer(mframe, NULL); mpp_frame_deinit(&mframe); } @@ -307,11 +310,17 @@ RK_S32 VpuApiLegacy:: decode_getoutframe(DecoderOut_t *aDecOut) RK_S32 VpuApiLegacy::encode(VpuCodecContext *ctx, EncInputStream_t *aEncInStrm, EncoderOut_t *aEncOut) { mpp_log_f("in\n"); + RK_S32 ret = MPP_OK; + + if (!init_ok) { + return VPU_API_ERR_VPU_CODEC_INIT; + } + (void)ctx; (void)aEncInStrm; (void)aEncOut; mpp_log_f("ok\n"); - return 0; + return ret; } RK_S32 VpuApiLegacy::encoder_sendframe(VpuCodecContext *ctx, EncInputStream_t *aEncInStrm)