History

avcodec/libjxldec: emit proper PTS to decoded AVFrame

If a sequence of JXL images is encapsulated in a container that has PTS information, we should use the PTS information from the container. At this time there is no container that does this, but if JPEG XL support is ever added to NUT, AVTransport, or some other container, this commit should allow the PTS information those containers provide to work as expected.

Leo Izen committed 2 years ago

Commit 5942cf41 parent 42f78921 file · +29 −28Browse files

Showing the changes to libavcodec (1 of 1 file). Show all

Unified · Split

libavcodec/libjxldec.c+29−28
@@ -54,10 +54,10 @@ typedef struct LibJxlDecodeContext { · 53 unmodified lines
5454 JxlDecoderStatus events;
5555 AVBufferRef *iccp;
5656 AVPacket *avpkt;
57− int64_t pts;
57+ int64_t accumulated_pts;
5858 int64_t frame_duration;
5959 int prev_is_last;
60− AVRational timebase;
60+ AVRational anim_timebase;
6161 AVFrame *frame;
6262 } LibJxlDecodeContext;
6363
@@ -80,7 +80,6 @@ static int libjxl_init_jxl_decoder(AVCodecContext *avctx) · 16 unmodified lines
8080 memset(&ctx->basic_info, 0, sizeof(JxlBasicInfo));
8181 memset(&ctx->jxl_pixfmt, 0, sizeof(JxlPixelFormat));
8282 ctx->prev_is_last = 1;
83− ctx->frame_duration = 1;
8483
8584 return 0;
8685 }
@@ -104,7 +103,6 @@ static av_cold int libjxl_decode_init(AVCodecContext *avctx) · 17 unmodified lines
104103 }
105104
106105 ctx->avpkt = avctx->internal->in_pkt;
107− ctx->pts = 0;
108106 ctx->frame = av_frame_alloc();
109107 if (!ctx->frame)
110108 return AVERROR(ENOMEM);
@@ -371,12 +369,15 @@ static int libjxl_receive_frame(AVCodecContext *avctx, AVFrame *frame) · 260 unmodified lines
371369
372370 while (1) {
373371 size_t remaining;
372+ JxlFrameHeader header;
374373
375374 if (!pkt->size) {
376375 av_packet_unref(pkt);
377376 ret = ff_decode_get_packet(avctx, pkt);
378377 if (ret < 0 && ret != AVERROR_EOF)
379378 return ret;
379+ ctx->accumulated_pts = 0;
380+ ctx->frame_duration = 0;
380381 if (!pkt->size) {
381382 /* jret set by the last iteration of the loop */
382383 if (jret == JXL_DEC_NEED_MORE_INPUT) {
@@ -430,12 +431,8 @@ static int libjxl_receive_frame(AVCodecContext *avctx, AVFrame *frame) · 47 unmodified lines
430431 if ((ret = ff_set_dimensions(avctx, ctx->basic_info.xsize, ctx->basic_info.ysize)) < 0)
431432 return ret;
432433 if (ctx->basic_info.have_animation)
433− ctx->timebase = av_make_q(ctx->basic_info.animation.tps_denominator,
434− ctx->basic_info.animation.tps_numerator);
435− else if (avctx->pkt_timebase.num)
436− ctx->timebase = avctx->pkt_timebase;
437− else
438− ctx->timebase = AV_TIME_BASE_Q;
434+ ctx->anim_timebase = av_make_q(ctx->basic_info.animation.tps_denominator,
435+ ctx->basic_info.animation.tps_numerator);
439436 continue;
440437 case JXL_DEC_COLOR_ENCODING:
441438 av_log(avctx, AV_LOG_DEBUG, "COLOR_ENCODING event emitted\n");
@@ -463,23 +460,24 @@ static int libjxl_receive_frame(AVCodecContext *avctx, AVFrame *frame) · 21 unmodified lines
463460 #endif
464461 continue;
465462 case JXL_DEC_FRAME:
463+ /* Frame here refers to the Frame bundle, not a decoded picture */
466464 av_log(avctx, AV_LOG_DEBUG, "FRAME event emitted\n");
467− if (!ctx->basic_info.have_animation || ctx->prev_is_last) {
465+ if (ctx->prev_is_last) {
466+ /*
467+ * The last frame sent was tagged as "is_last" which
468+ * means this is a new image file altogether.
469+ */
468470 ctx->frame->pict_type = AV_PICTURE_TYPE_I;
469471 ctx->frame->flags |= AV_FRAME_FLAG_KEY;
470472 }
471− if (ctx->basic_info.have_animation) {
472− JxlFrameHeader header;
473− if (JxlDecoderGetFrameHeader(ctx->decoder, &header) != JXL_DEC_SUCCESS) {
474− av_log(avctx, AV_LOG_ERROR, "Bad libjxl dec frame event\n");
475− return AVERROR_EXTERNAL;
476− }
477− ctx->prev_is_last = header.is_last;
478− ctx->frame_duration = header.duration;
479− } else {
480− ctx->prev_is_last = 1;
481− ctx->frame_duration = 1;
473+ if (JxlDecoderGetFrameHeader(ctx->decoder, &header) != JXL_DEC_SUCCESS) {
474+ av_log(avctx, AV_LOG_ERROR, "Bad libjxl dec frame event\n");
475+ return AVERROR_EXTERNAL;
482476 }
477+ ctx->prev_is_last = header.is_last;
478+ /* zero duration for animation means the frame is not presented */
479+ if (ctx->basic_info.have_animation && header.duration)
480+ ctx->frame_duration = header.duration;
483481 continue;
484482 case JXL_DEC_FULL_IMAGE:
485483 /* full image is one frame, even if animated */
@@ -491,14 +489,17 @@ static int libjxl_receive_frame(AVCodecContext *avctx, AVFrame *frame) · 5 unmodified lines
491489 /* ownership is transfered, and it is not ref-ed */
492490 ctx->iccp = NULL;
493491 }
494− if (avctx->pkt_timebase.num) {
495− ctx->frame->pts = av_rescale_q(ctx->pts, ctx->timebase, avctx->pkt_timebase);
496− ctx->frame->duration = av_rescale_q(ctx->frame_duration, ctx->timebase, avctx->pkt_timebase);
492+ if (ctx->basic_info.have_animation) {
493+ ctx->frame->pts = av_rescale_q(ctx->accumulated_pts, ctx->anim_timebase, avctx->pkt_timebase);
494+ ctx->frame->duration = av_rescale_q(ctx->frame_duration, ctx->anim_timebase, avctx->pkt_timebase);
497495 } else {
498− ctx->frame->pts = ctx->pts;
499− ctx->frame->duration = ctx->frame_duration;
496+ ctx->frame->pts = 0;
497+ ctx->frame->duration = pkt->duration;
500498 }
501− ctx->pts += ctx->frame_duration;
499+ if (pkt->pts != AV_NOPTS_VALUE)
500+ ctx->frame->pts += pkt->pts;
501+ ctx->accumulated_pts += ctx->frame_duration;
502+ ctx->frame->pkt_dts = pkt->dts;
502503 av_frame_move_ref(frame, ctx->frame);
503504 return 0;
504505 case JXL_DEC_SUCCESS:

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