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
54 JxlDecoderStatus events;54 JxlDecoderStatus events;
55 AVBufferRef *iccp;55 AVBufferRef *iccp;
56 AVPacket *avpkt;56 AVPacket *avpkt;
57 int64_t pts;57 int64_t accumulated_pts;
58 int64_t frame_duration;58 int64_t frame_duration;
59 int prev_is_last;59 int prev_is_last;
60 AVRational timebase;60 AVRational anim_timebase;
61 AVFrame *frame;61 AVFrame *frame;
62} LibJxlDecodeContext;62} LibJxlDecodeContext;
63 63
@@ -80,7 +80,6 @@ static int libjxl_init_jxl_decoder(AVCodecContext *avctx) · 16 unmodified lines
80 memset(&ctx->basic_info, 0, sizeof(JxlBasicInfo));80 memset(&ctx->basic_info, 0, sizeof(JxlBasicInfo));
81 memset(&ctx->jxl_pixfmt, 0, sizeof(JxlPixelFormat));81 memset(&ctx->jxl_pixfmt, 0, sizeof(JxlPixelFormat));
82 ctx->prev_is_last = 1;82 ctx->prev_is_last = 1;
83 ctx->frame_duration = 1;
84 83
85 return 0;84 return 0;
86}85}
@@ -104,7 +103,6 @@ static av_cold int libjxl_decode_init(AVCodecContext *avctx) · 17 unmodified lines
104 }103 }
105 104
106 ctx->avpkt = avctx->internal->in_pkt;105 ctx->avpkt = avctx->internal->in_pkt;
107 ctx->pts = 0;
108 ctx->frame = av_frame_alloc();106 ctx->frame = av_frame_alloc();
109 if (!ctx->frame)107 if (!ctx->frame)
110 return AVERROR(ENOMEM);108 return AVERROR(ENOMEM);
@@ -371,12 +369,15 @@ static int libjxl_receive_frame(AVCodecContext *avctx, AVFrame *frame) · 260 unmodified lines
371 369
372 while (1) {370 while (1) {
373 size_t remaining;371 size_t remaining;
372 JxlFrameHeader header;
374 373
375 if (!pkt->size) {374 if (!pkt->size) {
376 av_packet_unref(pkt);375 av_packet_unref(pkt);
377 ret = ff_decode_get_packet(avctx, pkt);376 ret = ff_decode_get_packet(avctx, pkt);
378 if (ret < 0 && ret != AVERROR_EOF)377 if (ret < 0 && ret != AVERROR_EOF)
379 return ret;378 return ret;
379 ctx->accumulated_pts = 0;
380 ctx->frame_duration = 0;
380 if (!pkt->size) {381 if (!pkt->size) {
381 /* jret set by the last iteration of the loop */382 /* jret set by the last iteration of the loop */
382 if (jret == JXL_DEC_NEED_MORE_INPUT) {383 if (jret == JXL_DEC_NEED_MORE_INPUT) {
@@ -430,12 +431,8 @@ static int libjxl_receive_frame(AVCodecContext *avctx, AVFrame *frame) · 47 unmodified lines
430 if ((ret = ff_set_dimensions(avctx, ctx->basic_info.xsize, ctx->basic_info.ysize)) < 0)431 if ((ret = ff_set_dimensions(avctx, ctx->basic_info.xsize, ctx->basic_info.ysize)) < 0)
431 return ret;432 return ret;
432 if (ctx->basic_info.have_animation)433 if (ctx->basic_info.have_animation)
433 ctx->timebase = av_make_q(ctx->basic_info.animation.tps_denominator,434 ctx->anim_timebase = av_make_q(ctx->basic_info.animation.tps_denominator,
434 ctx->basic_info.animation.tps_numerator);435 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;
439 continue;436 continue;
440 case JXL_DEC_COLOR_ENCODING:437 case JXL_DEC_COLOR_ENCODING:
441 av_log(avctx, AV_LOG_DEBUG, "COLOR_ENCODING event emitted\n");438 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
463#endif460#endif
464 continue;461 continue;
465 case JXL_DEC_FRAME:462 case JXL_DEC_FRAME:
463 /* Frame here refers to the Frame bundle, not a decoded picture */
466 av_log(avctx, AV_LOG_DEBUG, "FRAME event emitted\n");464 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 */
468 ctx->frame->pict_type = AV_PICTURE_TYPE_I;470 ctx->frame->pict_type = AV_PICTURE_TYPE_I;
469 ctx->frame->flags |= AV_FRAME_FLAG_KEY;471 ctx->frame->flags |= AV_FRAME_FLAG_KEY;
470 }472 }
471 if (ctx->basic_info.have_animation) {473 if (JxlDecoderGetFrameHeader(ctx->decoder, &header) != JXL_DEC_SUCCESS) {
472 JxlFrameHeader header;474 av_log(avctx, AV_LOG_ERROR, "Bad libjxl dec frame event\n");
473 if (JxlDecoderGetFrameHeader(ctx->decoder, &header) != JXL_DEC_SUCCESS) {475 return AVERROR_EXTERNAL;
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;
482 }476 }
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;
483 continue;481 continue;
484 case JXL_DEC_FULL_IMAGE:482 case JXL_DEC_FULL_IMAGE:
485 /* full image is one frame, even if animated */483 /* full image is one frame, even if animated */
@@ -491,14 +489,17 @@ static int libjxl_receive_frame(AVCodecContext *avctx, AVFrame *frame) · 5 unmodified lines
491 /* ownership is transfered, and it is not ref-ed */489 /* ownership is transfered, and it is not ref-ed */
492 ctx->iccp = NULL;490 ctx->iccp = NULL;
493 }491 }
494 if (avctx->pkt_timebase.num) {492 if (ctx->basic_info.have_animation) {
495 ctx->frame->pts = av_rescale_q(ctx->pts, ctx->timebase, avctx->pkt_timebase);493 ctx->frame->pts = av_rescale_q(ctx->accumulated_pts, ctx->anim_timebase, avctx->pkt_timebase);
496 ctx->frame->duration = av_rescale_q(ctx->frame_duration, ctx->timebase, avctx->pkt_timebase);494 ctx->frame->duration = av_rescale_q(ctx->frame_duration, ctx->anim_timebase, avctx->pkt_timebase);
497 } else {495 } else {
498 ctx->frame->pts = ctx->pts;496 ctx->frame->pts = 0;
499 ctx->frame->duration = ctx->frame_duration;497 ctx->frame->duration = pkt->duration;
500 }498 }
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;
502 av_frame_move_ref(frame, ctx->frame);503 av_frame_move_ref(frame, ctx->frame);
503 return 0;504 return 0;
504 case JXL_DEC_SUCCESS:505 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