Avoids an allocation and therefore one error path.
Unified · Split
| @@ -301,12 +301,10 @@ static int rkmpp_send_packet(AVCodecContext *avctx, const AVPacket *avpkt) · 300 unmodified lines | ||
| 301 | 301 | static void rkmpp_release_frame(void *opaque, uint8_t *data) |
| 302 | 302 | { |
| 303 | 303 | AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor *)data; |
| 304 | − AVBufferRef *framecontextref = (AVBufferRef *)opaque; | |
| 305 | − RKMPPFrameContext *framecontext = (RKMPPFrameContext *)framecontextref->data; | |
| 304 | + RKMPPFrameContext *framecontext = opaque; | |
| 306 | 305 | |
| 307 | 306 | mpp_frame_deinit(&framecontext->frame); |
| 308 | 307 | ff_refstruct_unref(&framecontext->decoder_ref); |
| 309 | − av_buffer_unref(&framecontextref); | |
| 310 | 308 | |
| 311 | 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 | 314 | RKMPPDecodeContext *rk_context = avctx->priv_data; |
| 317 | 315 | RKMPPDecoder *decoder = rk_context->decoder; |
| 318 | − RKMPPFrameContext *framecontext = NULL; | |
| 319 | − AVBufferRef *framecontextref = NULL; | |
| 320 | 316 | int ret; |
| 321 | 317 | MppFrame mppframe = NULL; |
| 322 | 318 | MppBuffer buffer = NULL; |
| 323 | − AVDRMFrameDescriptor *desc = NULL; | |
| 324 | 319 | AVDRMLayerDescriptor *layer = NULL; |
| 325 | 320 | int mode; |
| 326 | 321 | MppFrameFormat mppformat; |
| @@ -409,11 +404,21 @@ static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame) · 82 unmodified lines | ||
| 409 | 404 | // now setup the frame buffer info |
| 410 | 405 | buffer = mpp_frame_get_buffer(mppframe); |
| 411 | 406 | 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) { | |
| 414 | 417 | ret = AVERROR(ENOMEM); |
| 415 | 418 | goto fail; |
| 416 | 419 | } |
| 420 | + desc = &combined_desc->desc; | |
| 421 | + framecontext = &combined_desc->framecontext; | |
| 417 | 422 | |
| 418 | 423 | desc->nb_objects = 1; |
| 419 | 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 | 437 | layer->planes[1].offset = layer->planes[0].pitch * mpp_frame_get_ver_stride(mppframe); |
| 433 | 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 | 440 | // MPP decoder needs to be closed only when all frames have been released. |
| 444 | − framecontext = (RKMPPFrameContext *)framecontextref->data; | |
| 445 | 441 | framecontext->frame = mppframe; |
| 446 | 442 | |
| 447 | 443 | frame->data[0] = (uint8_t *)desc; |
| 448 | 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 | 447 | if (!frame->buf[0]) { |
| 448 | + av_free(combined_desc); | |
| 452 | 449 | ret = AVERROR(ENOMEM); |
| 453 | 450 | goto fail; |
| 454 | 451 | } |
| @@ -477,12 +474,6 @@ fail: · 22 unmodified lines | ||
| 477 | 474 | if (mppframe) |
| 478 | 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 | 477 | return ret; |
| 487 | 478 | } |
| 488 | 479 | |