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
301301 static void rkmpp_release_frame(void *opaque, uint8_t *data)
302302 {
303303 AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor *)data;
304− AVBufferRef *framecontextref = (AVBufferRef *)opaque;
305− RKMPPFrameContext *framecontext = (RKMPPFrameContext *)framecontextref->data;
304+ RKMPPFrameContext *framecontext = opaque;
306305
307306 mpp_frame_deinit(&framecontext->frame);
308307 ff_refstruct_unref(&framecontext->decoder_ref);
309− av_buffer_unref(&framecontextref);
310308
311309 av_free(desc);
312310 }
@@ -315,12 +313,9 @@ static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame) · 2 unmodified lines
315313 {
316314 RKMPPDecodeContext *rk_context = avctx->priv_data;
317315 RKMPPDecoder *decoder = rk_context->decoder;
318− RKMPPFrameContext *framecontext = NULL;
319− AVBufferRef *framecontextref = NULL;
320316 int ret;
321317 MppFrame mppframe = NULL;
322318 MppBuffer buffer = NULL;
323− AVDRMFrameDescriptor *desc = NULL;
324319 AVDRMLayerDescriptor *layer = NULL;
325320 int mode;
326321 MppFrameFormat mppformat;
@@ -409,11 +404,21 @@ static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame) · 82 unmodified lines
409404 // now setup the frame buffer info
410405 buffer = mpp_frame_get_buffer(mppframe);
411406 if (buffer) {
412− desc = av_mallocz(sizeof(AVDRMFrameDescriptor));
413− if (!desc) {
407+ RKMPPFrameContext *framecontext;
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) {
414417 ret = AVERROR(ENOMEM);
415418 goto fail;
416419 }
420+ desc = &combined_desc->desc;
421+ framecontext = &combined_desc->framecontext;
417422
418423 desc->nb_objects = 1;
419424 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
432437 layer->planes[1].offset = layer->planes[0].pitch * mpp_frame_get_ver_stride(mppframe);
433438 layer->planes[1].pitch = layer->planes[0].pitch;
434439
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−
443440 // MPP decoder needs to be closed only when all frames have been released.
444− framecontext = (RKMPPFrameContext *)framecontextref->data;
445441 framecontext->frame = mppframe;
446442
447443 frame->data[0] = (uint8_t *)desc;
448444 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);
450446
451447 if (!frame->buf[0]) {
448+ av_free(combined_desc);
452449 ret = AVERROR(ENOMEM);
453450 goto fail;
454451 }
@@ -477,12 +474,6 @@ fail: · 22 unmodified lines
477474 if (mppframe)
478475 mpp_frame_deinit(&mppframe);
479476
480− if (framecontextref)
481− av_buffer_unref(&framecontextref);
482−
483− if (desc)
484− av_free(desc);
485−
486477 return ret;
487478 }
488479

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