History

avcodec/rkmppdec: Allocate AVDRMFrameDescriptor and frame ctx jointly

Avoids an allocation and therefore one error path.

Andreas Rheinhardt committed 3 years ago

Commit fbd1b901 parent 1427e671 file · +15 −24Browse files

Unified · Split

libavcodec/rkmppdec.c+15−24
@@ -301,12 +301,10 @@ static int rkmpp_send_packet(AVCodecContext *avctx, const AVPacket *avpkt) · 300 unmodified lines
301static void rkmpp_release_frame(void *opaque, uint8_t *data)301static void rkmpp_release_frame(void *opaque, uint8_t *data)
302{302{
303 AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor *)data;303 AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor *)data;
304 AVBufferRef *framecontextref = (AVBufferRef *)opaque;304 RKMPPFrameContext *framecontext = opaque;
305 RKMPPFrameContext *framecontext = (RKMPPFrameContext *)framecontextref->data;
306 305
307 mpp_frame_deinit(&framecontext->frame);306 mpp_frame_deinit(&framecontext->frame);
308 ff_refstruct_unref(&framecontext->decoder_ref);307 ff_refstruct_unref(&framecontext->decoder_ref);
309 av_buffer_unref(&framecontextref);
310 308
311 av_free(desc);309 av_free(desc);
312}310}
@@ -315,12 +313,9 @@ static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame) · 2 unmodified lines
315{313{
316 RKMPPDecodeContext *rk_context = avctx->priv_data;314 RKMPPDecodeContext *rk_context = avctx->priv_data;
317 RKMPPDecoder *decoder = rk_context->decoder;315 RKMPPDecoder *decoder = rk_context->decoder;
318 RKMPPFrameContext *framecontext = NULL;
319 AVBufferRef *framecontextref = NULL;
320 int ret;316 int ret;
321 MppFrame mppframe = NULL;317 MppFrame mppframe = NULL;
322 MppBuffer buffer = NULL;318 MppBuffer buffer = NULL;
323 AVDRMFrameDescriptor *desc = NULL;
324 AVDRMLayerDescriptor *layer = NULL;319 AVDRMLayerDescriptor *layer = NULL;
325 int mode;320 int mode;
326 MppFrameFormat mppformat;321 MppFrameFormat mppformat;
@@ -409,11 +404,21 @@ static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame) · 82 unmodified lines
409 // now setup the frame buffer info404 // now setup the frame buffer info
410 buffer = mpp_frame_get_buffer(mppframe);405 buffer = mpp_frame_get_buffer(mppframe);
411 if (buffer) {406 if (buffer) {
412 desc = av_mallocz(sizeof(AVDRMFrameDescriptor));407 RKMPPFrameContext *framecontext;
413 if (!desc) {408 AVDRMFrameDescriptor *desc;
409 // We allocate the descriptor in buf[0] jointly with a structure
410 // that will allow to hold additional information
411 // for properly releasing MPP frames and decoder.
412 struct {
413 AVDRMFrameDescriptor desc;
414 RKMPPFrameContext framecontext;
415 } *combined_desc = av_mallocz(sizeof(*combined_desc));
416 if (!combined_desc) {
414 ret = AVERROR(ENOMEM);417 ret = AVERROR(ENOMEM);
415 goto fail;418 goto fail;
416 }419 }
420 desc = &combined_desc->desc;
421 framecontext = &combined_desc->framecontext;
417 422
418 desc->nb_objects = 1;423 desc->nb_objects = 1;
419 desc->objects[0].fd = mpp_buffer_get_fd(buffer);424 desc->objects[0].fd = mpp_buffer_get_fd(buffer);
@@ -432,23 +437,15 @@ static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame) · 12 unmodified lines
432 layer->planes[1].offset = layer->planes[0].pitch * mpp_frame_get_ver_stride(mppframe);437 layer->planes[1].offset = layer->planes[0].pitch * mpp_frame_get_ver_stride(mppframe);
433 layer->planes[1].pitch = layer->planes[0].pitch;438 layer->planes[1].pitch = layer->planes[0].pitch;
434 439
435 // we also allocate a struct in buf[0] that will allow to hold additionnal information
436 // for releasing properly MPP frames and decoder
437 framecontextref = av_buffer_allocz(sizeof(*framecontext));
438 if (!framecontextref) {
439 ret = AVERROR(ENOMEM);
440 goto fail;
441 }
442
443 // MPP decoder needs to be closed only when all frames have been released.440 // MPP decoder needs to be closed only when all frames have been released.
444 framecontext = (RKMPPFrameContext *)framecontextref->data;
445 framecontext->frame = mppframe;441 framecontext->frame = mppframe;
446 442
447 frame->data[0] = (uint8_t *)desc;443 frame->data[0] = (uint8_t *)desc;
448 frame->buf[0] = av_buffer_create((uint8_t *)desc, sizeof(*desc), rkmpp_release_frame,444 frame->buf[0] = av_buffer_create((uint8_t *)desc, sizeof(*desc), rkmpp_release_frame,
449 framecontextref, AV_BUFFER_FLAG_READONLY);445 framecontext, AV_BUFFER_FLAG_READONLY);
450 446
451 if (!frame->buf[0]) {447 if (!frame->buf[0]) {
448 av_free(combined_desc);
452 ret = AVERROR(ENOMEM);449 ret = AVERROR(ENOMEM);
453 goto fail;450 goto fail;
454 }451 }
@@ -477,12 +474,6 @@ fail: · 22 unmodified lines
477 if (mppframe)474 if (mppframe)
478 mpp_frame_deinit(&mppframe);475 mpp_frame_deinit(&mppframe);
479 476
480 if (framecontextref)
481 av_buffer_unref(&framecontextref);
482
483 if (desc)
484 av_free(desc);
485
486 return ret;477 return ret;
487}478}
488 479

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t