Before this commit, the decoder erroneously assumes that the AVFrame passed to the receive_frame is the same one each time. Now it keeps an internal AVFrame to write into, and copies it over when it's done.
Showing the changes to libavcodec (1 of 1 file). Show all
Unified · Split
| @@ -58,6 +58,7 @@ typedef struct LibJxlDecodeContext { · 57 unmodified lines | ||
| 58 | 58 | int64_t frame_duration; |
| 59 | 59 | int prev_is_last; |
| 60 | 60 | AVRational timebase; |
| 61 | + AVFrame *frame; | |
| 61 | 62 | } LibJxlDecodeContext; |
| 62 | 63 | |
| 63 | 64 | static int libjxl_init_jxl_decoder(AVCodecContext *avctx) |
| @@ -104,6 +105,9 @@ static av_cold int libjxl_decode_init(AVCodecContext *avctx) · 40 unmodified lines | ||
| 104 | 105 | |
| 105 | 106 | ctx->avpkt = avctx->internal->in_pkt; |
| 106 | 107 | ctx->pts = 0; |
| 108 | + ctx->frame = av_frame_alloc(); | |
| 109 | + if (!ctx->frame) | |
| 110 | + return AVERROR(ENOMEM); | |
| 107 | 111 | |
| 108 | 112 | return libjxl_init_jxl_decoder(avctx); |
| 109 | 113 | } |
| @@ -406,10 +410,6 @@ static int libjxl_receive_frame(AVCodecContext *avctx, AVFrame *frame) · 296 unmodified lines | ||
| 406 | 410 | return AVERROR_INVALIDDATA; |
| 407 | 411 | case JXL_DEC_NEED_MORE_INPUT: |
| 408 | 412 | av_log(avctx, AV_LOG_DEBUG, "NEED_MORE_INPUT event emitted\n"); |
| 409 | − if (!pkt->size) { | |
| 410 | − av_packet_unref(pkt); | |
| 411 | − return AVERROR(EAGAIN); | |
| 412 | − } | |
| 413 | 413 | continue; |
| 414 | 414 | case JXL_DEC_BASIC_INFO: |
| 415 | 415 | av_log(avctx, AV_LOG_DEBUG, "BASIC_INFO event emitted\n"); |
| @@ -438,16 +438,19 @@ static int libjxl_receive_frame(AVCodecContext *avctx, AVFrame *frame) · 22 unmodified lines | ||
| 438 | 438 | continue; |
| 439 | 439 | case JXL_DEC_COLOR_ENCODING: |
| 440 | 440 | av_log(avctx, AV_LOG_DEBUG, "COLOR_ENCODING event emitted\n"); |
| 441 | − if ((ret = libjxl_color_encoding_event(avctx, frame)) < 0) | |
| 441 | + ret = libjxl_color_encoding_event(avctx, ctx->frame); | |
| 442 | + if (ret < 0) | |
| 442 | 443 | return ret; |
| 443 | 444 | continue; |
| 444 | 445 | case JXL_DEC_NEED_IMAGE_OUT_BUFFER: |
| 445 | 446 | av_log(avctx, AV_LOG_DEBUG, "NEED_IMAGE_OUT_BUFFER event emitted\n"); |
| 446 | − if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) | |
| 447 | + ret = ff_get_buffer(avctx, ctx->frame, 0); | |
| 448 | + if (ret < 0) | |
| 447 | 449 | return ret; |
| 448 | − ctx->jxl_pixfmt.align = frame->linesize[0]; | |
| 449 | − if (JxlDecoderSetImageOutBuffer(ctx->decoder, &ctx->jxl_pixfmt, frame->data[0], frame->buf[0]->size) | |
| 450 | − != JXL_DEC_SUCCESS) { | |
| 450 | + ctx->jxl_pixfmt.align = ctx->frame->linesize[0]; | |
| 451 | + if (JxlDecoderSetImageOutBuffer(ctx->decoder, &ctx->jxl_pixfmt, | |
| 452 | + ctx->frame->data[0], ctx->frame->buf[0]->size) | |
| 453 | + != JXL_DEC_SUCCESS) { | |
| 451 | 454 | av_log(avctx, AV_LOG_ERROR, "Bad libjxl dec need image out buffer event\n"); |
| 452 | 455 | return AVERROR_EXTERNAL; |
| 453 | 456 | } |
| @@ -461,8 +464,8 @@ static int libjxl_receive_frame(AVCodecContext *avctx, AVFrame *frame) · 7 unmodified lines | ||
| 461 | 464 | case JXL_DEC_FRAME: |
| 462 | 465 | av_log(avctx, AV_LOG_DEBUG, "FRAME event emitted\n"); |
| 463 | 466 | if (!ctx->basic_info.have_animation || ctx->prev_is_last) { |
| 464 | − frame->pict_type = AV_PICTURE_TYPE_I; | |
| 465 | − frame->flags |= AV_FRAME_FLAG_KEY; | |
| 467 | + ctx->frame->pict_type = AV_PICTURE_TYPE_I; | |
| 468 | + ctx->frame->flags |= AV_FRAME_FLAG_KEY; | |
| 466 | 469 | } |
| 467 | 470 | if (ctx->basic_info.have_animation) { |
| 468 | 471 | JxlFrameHeader header; |
| @@ -481,20 +484,21 @@ static int libjxl_receive_frame(AVCodecContext *avctx, AVFrame *frame) · 12 unmodified lines | ||
| 481 | 484 | /* full image is one frame, even if animated */ |
| 482 | 485 | av_log(avctx, AV_LOG_DEBUG, "FULL_IMAGE event emitted\n"); |
| 483 | 486 | if (ctx->iccp) { |
| 484 | − AVFrameSideData *sd = av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_ICC_PROFILE, ctx->iccp); | |
| 487 | + AVFrameSideData *sd = av_frame_new_side_data_from_buf(ctx->frame, AV_FRAME_DATA_ICC_PROFILE, ctx->iccp); | |
| 485 | 488 | if (!sd) |
| 486 | 489 | return AVERROR(ENOMEM); |
| 487 | 490 | /* ownership is transfered, and it is not ref-ed */ |
| 488 | 491 | ctx->iccp = NULL; |
| 489 | 492 | } |
| 490 | 493 | if (avctx->pkt_timebase.num) { |
| 491 | − frame->pts = av_rescale_q(ctx->pts, ctx->timebase, avctx->pkt_timebase); | |
| 492 | − frame->duration = av_rescale_q(ctx->frame_duration, ctx->timebase, avctx->pkt_timebase); | |
| 494 | + ctx->frame->pts = av_rescale_q(ctx->pts, ctx->timebase, avctx->pkt_timebase); | |
| 495 | + ctx->frame->duration = av_rescale_q(ctx->frame_duration, ctx->timebase, avctx->pkt_timebase); | |
| 493 | 496 | } else { |
| 494 | − frame->pts = ctx->pts; | |
| 495 | − frame->duration = ctx->frame_duration; | |
| 497 | + ctx->frame->pts = ctx->pts; | |
| 498 | + ctx->frame->duration = ctx->frame_duration; | |
| 496 | 499 | } |
| 497 | 500 | ctx->pts += ctx->frame_duration; |
| 501 | + av_frame_move_ref(frame, ctx->frame); | |
| 498 | 502 | return 0; |
| 499 | 503 | case JXL_DEC_SUCCESS: |
| 500 | 504 | av_log(avctx, AV_LOG_DEBUG, "SUCCESS event emitted\n"); |
| @@ -525,6 +529,7 @@ static av_cold int libjxl_decode_close(AVCodecContext *avctx) · 24 unmodified lines | ||
| 525 | 529 | JxlDecoderDestroy(ctx->decoder); |
| 526 | 530 | ctx->decoder = NULL; |
| 527 | 531 | av_buffer_unref(&ctx->iccp); |
| 532 | + av_frame_free(&ctx->frame); | |
| 528 | 533 | |
| 529 | 534 | return 0; |
| 530 | 535 | } |