avcodec/libjxldec: use internal AVFrame as buffered space
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. Signed-off-by: Leo Izen <leo.izen@gmail.com>
This commit is contained in:
		
							parent
							
								
									245910d5c9
								
							
						
					
					
						commit
						7a69f7312e
					
				@ -58,6 +58,7 @@ typedef struct LibJxlDecodeContext {
 | 
				
			|||||||
    int64_t frame_duration;
 | 
					    int64_t frame_duration;
 | 
				
			||||||
    int prev_is_last;
 | 
					    int prev_is_last;
 | 
				
			||||||
    AVRational timebase;
 | 
					    AVRational timebase;
 | 
				
			||||||
 | 
					    AVFrame *frame;
 | 
				
			||||||
} LibJxlDecodeContext;
 | 
					} LibJxlDecodeContext;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int libjxl_init_jxl_decoder(AVCodecContext *avctx)
 | 
					static int libjxl_init_jxl_decoder(AVCodecContext *avctx)
 | 
				
			||||||
@ -104,6 +105,9 @@ static av_cold int libjxl_decode_init(AVCodecContext *avctx)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    ctx->avpkt = avctx->internal->in_pkt;
 | 
					    ctx->avpkt = avctx->internal->in_pkt;
 | 
				
			||||||
    ctx->pts = 0;
 | 
					    ctx->pts = 0;
 | 
				
			||||||
 | 
					    ctx->frame = av_frame_alloc();
 | 
				
			||||||
 | 
					    if (!ctx->frame)
 | 
				
			||||||
 | 
					        return AVERROR(ENOMEM);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return libjxl_init_jxl_decoder(avctx);
 | 
					    return libjxl_init_jxl_decoder(avctx);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -406,10 +410,6 @@ static int libjxl_receive_frame(AVCodecContext *avctx, AVFrame *frame)
 | 
				
			|||||||
            return AVERROR_INVALIDDATA;
 | 
					            return AVERROR_INVALIDDATA;
 | 
				
			||||||
        case JXL_DEC_NEED_MORE_INPUT:
 | 
					        case JXL_DEC_NEED_MORE_INPUT:
 | 
				
			||||||
            av_log(avctx, AV_LOG_DEBUG, "NEED_MORE_INPUT event emitted\n");
 | 
					            av_log(avctx, AV_LOG_DEBUG, "NEED_MORE_INPUT event emitted\n");
 | 
				
			||||||
            if (!pkt->size) {
 | 
					 | 
				
			||||||
                av_packet_unref(pkt);
 | 
					 | 
				
			||||||
                return AVERROR(EAGAIN);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            continue;
 | 
					            continue;
 | 
				
			||||||
        case JXL_DEC_BASIC_INFO:
 | 
					        case JXL_DEC_BASIC_INFO:
 | 
				
			||||||
            av_log(avctx, AV_LOG_DEBUG, "BASIC_INFO event emitted\n");
 | 
					            av_log(avctx, AV_LOG_DEBUG, "BASIC_INFO event emitted\n");
 | 
				
			||||||
@ -438,15 +438,18 @@ static int libjxl_receive_frame(AVCodecContext *avctx, AVFrame *frame)
 | 
				
			|||||||
            continue;
 | 
					            continue;
 | 
				
			||||||
        case JXL_DEC_COLOR_ENCODING:
 | 
					        case JXL_DEC_COLOR_ENCODING:
 | 
				
			||||||
            av_log(avctx, AV_LOG_DEBUG, "COLOR_ENCODING event emitted\n");
 | 
					            av_log(avctx, AV_LOG_DEBUG, "COLOR_ENCODING event emitted\n");
 | 
				
			||||||
            if ((ret = libjxl_color_encoding_event(avctx, frame)) < 0)
 | 
					            ret = libjxl_color_encoding_event(avctx, ctx->frame);
 | 
				
			||||||
 | 
					            if (ret < 0)
 | 
				
			||||||
                return ret;
 | 
					                return ret;
 | 
				
			||||||
            continue;
 | 
					            continue;
 | 
				
			||||||
        case JXL_DEC_NEED_IMAGE_OUT_BUFFER:
 | 
					        case JXL_DEC_NEED_IMAGE_OUT_BUFFER:
 | 
				
			||||||
            av_log(avctx, AV_LOG_DEBUG, "NEED_IMAGE_OUT_BUFFER event emitted\n");
 | 
					            av_log(avctx, AV_LOG_DEBUG, "NEED_IMAGE_OUT_BUFFER event emitted\n");
 | 
				
			||||||
            if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
 | 
					            ret = ff_get_buffer(avctx, ctx->frame, 0);
 | 
				
			||||||
 | 
					            if (ret < 0)
 | 
				
			||||||
                return ret;
 | 
					                return ret;
 | 
				
			||||||
            ctx->jxl_pixfmt.align = frame->linesize[0];
 | 
					            ctx->jxl_pixfmt.align = ctx->frame->linesize[0];
 | 
				
			||||||
            if (JxlDecoderSetImageOutBuffer(ctx->decoder, &ctx->jxl_pixfmt, frame->data[0], frame->buf[0]->size)
 | 
					            if (JxlDecoderSetImageOutBuffer(ctx->decoder, &ctx->jxl_pixfmt,
 | 
				
			||||||
 | 
					                    ctx->frame->data[0], ctx->frame->buf[0]->size)
 | 
				
			||||||
                    != JXL_DEC_SUCCESS) {
 | 
					                    != JXL_DEC_SUCCESS) {
 | 
				
			||||||
                av_log(avctx, AV_LOG_ERROR, "Bad libjxl dec need image out buffer event\n");
 | 
					                av_log(avctx, AV_LOG_ERROR, "Bad libjxl dec need image out buffer event\n");
 | 
				
			||||||
                return AVERROR_EXTERNAL;
 | 
					                return AVERROR_EXTERNAL;
 | 
				
			||||||
@ -461,8 +464,8 @@ static int libjxl_receive_frame(AVCodecContext *avctx, AVFrame *frame)
 | 
				
			|||||||
        case JXL_DEC_FRAME:
 | 
					        case JXL_DEC_FRAME:
 | 
				
			||||||
            av_log(avctx, AV_LOG_DEBUG, "FRAME event emitted\n");
 | 
					            av_log(avctx, AV_LOG_DEBUG, "FRAME event emitted\n");
 | 
				
			||||||
            if (!ctx->basic_info.have_animation || ctx->prev_is_last) {
 | 
					            if (!ctx->basic_info.have_animation || ctx->prev_is_last) {
 | 
				
			||||||
                frame->pict_type = AV_PICTURE_TYPE_I;
 | 
					                ctx->frame->pict_type = AV_PICTURE_TYPE_I;
 | 
				
			||||||
                frame->flags |= AV_FRAME_FLAG_KEY;
 | 
					                ctx->frame->flags |= AV_FRAME_FLAG_KEY;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            if (ctx->basic_info.have_animation) {
 | 
					            if (ctx->basic_info.have_animation) {
 | 
				
			||||||
                JxlFrameHeader header;
 | 
					                JxlFrameHeader header;
 | 
				
			||||||
@ -481,20 +484,21 @@ static int libjxl_receive_frame(AVCodecContext *avctx, AVFrame *frame)
 | 
				
			|||||||
            /* full image is one frame, even if animated */
 | 
					            /* full image is one frame, even if animated */
 | 
				
			||||||
            av_log(avctx, AV_LOG_DEBUG, "FULL_IMAGE event emitted\n");
 | 
					            av_log(avctx, AV_LOG_DEBUG, "FULL_IMAGE event emitted\n");
 | 
				
			||||||
            if (ctx->iccp) {
 | 
					            if (ctx->iccp) {
 | 
				
			||||||
                AVFrameSideData *sd = av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_ICC_PROFILE, ctx->iccp);
 | 
					                AVFrameSideData *sd = av_frame_new_side_data_from_buf(ctx->frame, AV_FRAME_DATA_ICC_PROFILE, ctx->iccp);
 | 
				
			||||||
                if (!sd)
 | 
					                if (!sd)
 | 
				
			||||||
                    return AVERROR(ENOMEM);
 | 
					                    return AVERROR(ENOMEM);
 | 
				
			||||||
                /* ownership is transfered, and it is not ref-ed */
 | 
					                /* ownership is transfered, and it is not ref-ed */
 | 
				
			||||||
                ctx->iccp = NULL;
 | 
					                ctx->iccp = NULL;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            if (avctx->pkt_timebase.num) {
 | 
					            if (avctx->pkt_timebase.num) {
 | 
				
			||||||
                frame->pts = av_rescale_q(ctx->pts, ctx->timebase, avctx->pkt_timebase);
 | 
					                ctx->frame->pts = av_rescale_q(ctx->pts, ctx->timebase, avctx->pkt_timebase);
 | 
				
			||||||
                frame->duration = av_rescale_q(ctx->frame_duration, ctx->timebase, avctx->pkt_timebase);
 | 
					                ctx->frame->duration = av_rescale_q(ctx->frame_duration, ctx->timebase, avctx->pkt_timebase);
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                frame->pts = ctx->pts;
 | 
					                ctx->frame->pts = ctx->pts;
 | 
				
			||||||
                frame->duration = ctx->frame_duration;
 | 
					                ctx->frame->duration = ctx->frame_duration;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            ctx->pts += ctx->frame_duration;
 | 
					            ctx->pts += ctx->frame_duration;
 | 
				
			||||||
 | 
					            av_frame_move_ref(frame, ctx->frame);
 | 
				
			||||||
            return 0;
 | 
					            return 0;
 | 
				
			||||||
        case JXL_DEC_SUCCESS:
 | 
					        case JXL_DEC_SUCCESS:
 | 
				
			||||||
            av_log(avctx, AV_LOG_DEBUG, "SUCCESS event emitted\n");
 | 
					            av_log(avctx, AV_LOG_DEBUG, "SUCCESS event emitted\n");
 | 
				
			||||||
@ -525,6 +529,7 @@ static av_cold int libjxl_decode_close(AVCodecContext *avctx)
 | 
				
			|||||||
        JxlDecoderDestroy(ctx->decoder);
 | 
					        JxlDecoderDestroy(ctx->decoder);
 | 
				
			||||||
    ctx->decoder = NULL;
 | 
					    ctx->decoder = NULL;
 | 
				
			||||||
    av_buffer_unref(&ctx->iccp);
 | 
					    av_buffer_unref(&ctx->iccp);
 | 
				
			||||||
 | 
					    av_frame_free(&ctx->frame);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user