diff mbox series

[FFmpeg-devel,1/2] doc/examples/transcode: fix timestamps scaling

Message ID 20230319181745.121124-1-stefasab@gmail.com
State New
Headers show
Series [FFmpeg-devel,1/2] doc/examples/transcode: fix timestamps scaling | expand

Commit Message

Stefano Sabatini March 19, 2023, 6:17 p.m. UTC
Set pkt_timebase in the decoder and in the decoder frame, use it for
the filterchain source, and rescale the filtered frame to the target
encoder.
---
 doc/examples/transcode.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/doc/examples/transcode.c b/doc/examples/transcode.c
index cec4ad00e8..3bc85dce85 100644
--- a/doc/examples/transcode.c
+++ b/doc/examples/transcode.c
@@ -103,6 +103,7 @@  static int open_input_file(const char *filename)
             return ret;
         }
 
+        codec_ctx->pkt_timebase = stream->time_base;
         codec_type = codec_ctx->codec_type;
 
         /* Reencode video & audio and remux subtitles etc. */
@@ -272,7 +273,7 @@  static int init_filter(FilteringContext *fctx, AVCodecContext *dec_ctx,
         snprintf(args, sizeof(args),
                  "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
                  dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
-                 dec_ctx->time_base.num, dec_ctx->time_base.den,
+                 dec_ctx->pkt_timebase.num, dec_ctx->pkt_timebase.den,
                  dec_ctx->sample_aspect_ratio.num,
                  dec_ctx->sample_aspect_ratio.den);
 
@@ -443,6 +444,14 @@  static int encode_write_frame(unsigned int stream_index, int flush)
     /* encode filtered frame */
     av_packet_unref(enc_pkt);
 
+    if (filt_frame) {
+        /* rescale frame PTS, to match the expected encoder time base */
+        filt_frame->pts = av_rescale_q(filt_frame->pts, filt_frame->time_base,
+                                       stream->enc_ctx->time_base);
+        filt_frame->pkt_dts = av_rescale_q(filt_frame->pkt_dts, filt_frame->time_base,
+                                           stream->enc_ctx->time_base);
+        filt_frame->time_base = stream->enc_ctx->time_base;
+    }
     ret = avcodec_send_frame(stream->enc_ctx, filt_frame);
 
     if (ret < 0)
@@ -549,7 +558,7 @@  int main(int argc, char **argv)
 
             av_packet_rescale_ts(packet,
                                  ifmt_ctx->streams[stream_index]->time_base,
-                                 stream->dec_ctx->time_base);
+                                 stream->dec_ctx->pkt_timebase);
             ret = avcodec_send_packet(stream->dec_ctx, packet);
             if (ret < 0) {
                 av_log(NULL, AV_LOG_ERROR, "Decoding failed\n");
@@ -564,6 +573,7 @@  int main(int argc, char **argv)
                     goto end;
 
                 stream->dec_frame->pts = stream->dec_frame->best_effort_timestamp;
+                stream->dec_frame->time_base = ifmt_ctx->streams[stream_index]->time_base;
                 ret = filter_encode_write_frame(stream->dec_frame, stream_index);
                 if (ret < 0)
                     goto end;