Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH 01/18] fftools/ffmpeg: stop explicitly closing output streams on input EOF
@ 2023-08-26 15:11 Anton Khirnov
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 02/18] fftools/ffmpeg: simplify handling input -t for streamcopy Anton Khirnov
                   ` (16 more replies)
  0 siblings, 17 replies; 21+ messages in thread
From: Anton Khirnov @ 2023-08-26 15:11 UTC (permalink / raw)
  To: ffmpeg-devel

Sending an empty packet already does that implicitly.
---
 fftools/ffmpeg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 6130fd06fc..c7822f8045 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1124,7 +1124,7 @@ static int process_input(int file_index)
             for (int oidx = 0; oidx < ist->nb_outputs; oidx++) {
                 OutputStream *ost = ist->outputs[oidx];
                 OutputFile    *of = output_files[ost->file_index];
-                close_output_stream(ost);
+
                 ret = of_output_packet(of, ost, NULL);
                 if (ret < 0)
                     return ret;
-- 
2.40.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [FFmpeg-devel] [PATCH 02/18] fftools/ffmpeg: simplify handling input -t for streamcopy
  2023-08-26 15:11 [FFmpeg-devel] [PATCH 01/18] fftools/ffmpeg: stop explicitly closing output streams on input EOF Anton Khirnov
@ 2023-08-26 15:11 ` Anton Khirnov
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 03/18] fftools/ffmpeg_mux: use correct timebases for bitstream filtering Anton Khirnov
                   ` (15 subsequent siblings)
  16 siblings, 0 replies; 21+ messages in thread
From: Anton Khirnov @ 2023-08-26 15:11 UTC (permalink / raw)
  To: ffmpeg-devel

Output stream will be closed implicitly after a NULL packet is sent to
it, there is no need to explicitly call close_output_stream().
---
 fftools/ffmpeg.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index c7822f8045..8b0f31aac6 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -814,7 +814,6 @@ static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eo
     int64_t dts_est = AV_NOPTS_VALUE;
     int ret = 0;
     int eof_reached = 0;
-    int duration_exceeded;
 
     if (ist->decoding_needed) {
         ret = dec_packet(ist, pkt, no_eof);
@@ -829,7 +828,6 @@ static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eo
         dts_est = pd->dts_est;
     }
 
-    duration_exceeded = 0;
     if (f->recording_time != INT64_MAX) {
         int64_t start_time = 0;
         if (copy_ts) {
@@ -837,7 +835,7 @@ static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eo
             start_time += start_at_zero ? 0 : f->start_time_effective;
         }
         if (dts_est >= f->recording_time + start_time)
-            duration_exceeded = 1;
+            pkt = NULL;
     }
 
     for (int oidx = 0; oidx < ist->nb_outputs; oidx++) {
@@ -845,11 +843,6 @@ static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eo
         if (ost->enc || (!pkt && no_eof))
             continue;
 
-        if (duration_exceeded) {
-            close_output_stream(ost);
-            continue;
-        }
-
         ret = of_streamcopy(ost, pkt, dts_est);
         if (ret < 0)
             return ret;
-- 
2.40.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [FFmpeg-devel] [PATCH 03/18] fftools/ffmpeg_mux: use correct timebases for bitstream filtering
  2023-08-26 15:11 [FFmpeg-devel] [PATCH 01/18] fftools/ffmpeg: stop explicitly closing output streams on input EOF Anton Khirnov
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 02/18] fftools/ffmpeg: simplify handling input -t for streamcopy Anton Khirnov
@ 2023-08-26 15:11 ` Anton Khirnov
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 04/18] fftools/ffmpeg_mux: stop rescaling timestamps in of_streamcopy() Anton Khirnov
                   ` (14 subsequent siblings)
  16 siblings, 0 replies; 21+ messages in thread
From: Anton Khirnov @ 2023-08-26 15:11 UTC (permalink / raw)
  To: ffmpeg-devel

Bitstream filtering input timebase is not always necessarily equal to
OutputStream.mux_timebase. Also, set AVPacket.time_base correctly for
packets output by bitstream filters

Do not rescale at all in of_output_packet() when not doing bitstream
filtering, as it's unnecessary - write_packet() will rescale to the
actual muxer timebase.
---
 fftools/ffmpeg_mux.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 24cdf00469..ab9bb398c1 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -340,16 +340,13 @@ int of_output_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt)
     if (pkt && pkt->dts != AV_NOPTS_VALUE)
         ost->last_mux_dts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
 
-    /* rescale timestamps to the muxing timebase */
-    if (pkt) {
-        av_packet_rescale_ts(pkt, pkt->time_base, ost->mux_timebase);
-        pkt->time_base = ost->mux_timebase;
-    }
-
     /* apply the output bitstream filters */
     if (ms->bsf_ctx) {
         int bsf_eof = 0;
 
+        if (pkt)
+            av_packet_rescale_ts(pkt, pkt->time_base, ms->bsf_ctx->time_base_in);
+
         ret = av_bsf_send_packet(ms->bsf_ctx, pkt);
         if (ret < 0) {
             err_msg = "submitting a packet for bitstream filtering";
@@ -367,6 +364,9 @@ int of_output_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt)
                 goto fail;
             }
 
+            if (!bsf_eof)
+                ms->bsf_pkt->time_base = ms->bsf_ctx->time_base_out;
+
             ret = submit_packet(mux, bsf_eof ? NULL : ms->bsf_pkt, ost);
             if (ret < 0)
                 goto mux_fail;
-- 
2.40.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [FFmpeg-devel] [PATCH 04/18] fftools/ffmpeg_mux: stop rescaling timestamps in of_streamcopy()
  2023-08-26 15:11 [FFmpeg-devel] [PATCH 01/18] fftools/ffmpeg: stop explicitly closing output streams on input EOF Anton Khirnov
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 02/18] fftools/ffmpeg: simplify handling input -t for streamcopy Anton Khirnov
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 03/18] fftools/ffmpeg_mux: use correct timebases for bitstream filtering Anton Khirnov
@ 2023-08-26 15:11 ` Anton Khirnov
  2023-08-26 20:20   ` Michael Niedermayer
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 05/18] fftools/ffmpeg_enc: factor out setting encoder timebase Anton Khirnov
                   ` (13 subsequent siblings)
  16 siblings, 1 reply; 21+ messages in thread
From: Anton Khirnov @ 2023-08-26 15:11 UTC (permalink / raw)
  To: ffmpeg-devel

This function converts packet timestamps from the input stream timebase
to OutputStream.mux_timebase, which may or may not be equal to the
actual output AVStream timebase (and even when it is, this may not
always be the optimal choice due to bitstream filtering).

Just keep the timestamps in input stream timebase, they will be rescaled
as needed before bitstream filtering and/or sending the packet to the
muxer.

Drop now-unused OutputStream.mux_timebase.
---
 fftools/ffmpeg.h          |  2 --
 fftools/ffmpeg_enc.c      |  2 --
 fftools/ffmpeg_mux.c      | 21 +++++++--------------
 fftools/ffmpeg_mux_init.c |  2 --
 4 files changed, 7 insertions(+), 20 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index d53181e427..ef5bb13908 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -523,8 +523,6 @@ typedef struct OutputStream {
     /* dts of the last packet sent to the muxing queue, in AV_TIME_BASE_Q */
     int64_t last_mux_dts;
 
-    // the timebase of the packets sent to the muxer
-    AVRational mux_timebase;
     AVRational enc_timebase;
 
     Encoder *enc;
diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index 96424272bf..3c9fdd3237 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -463,8 +463,6 @@ int enc_open(OutputStream *ost, AVFrame *frame)
     if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0)
         ost->st->time_base = av_add_q(ost->enc_ctx->time_base, (AVRational){0, 1});
 
-    ost->mux_timebase = enc_ctx->time_base;
-
     ret = of_stream_init(of, ost);
     if (ret < 0)
         return ret;
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index ab9bb398c1..076c6afabd 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -392,7 +392,7 @@ int of_streamcopy(OutputStream *ost, const AVPacket *pkt, int64_t dts)
     OutputFile *of = output_files[ost->file_index];
     MuxStream  *ms = ms_from_ost(ost);
     int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
-    int64_t ost_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ost->mux_timebase);
+    int64_t ts_offset;
     AVPacket *opkt = ms->pkt;
     int ret;
 
@@ -425,10 +425,10 @@ int of_streamcopy(OutputStream *ost, const AVPacket *pkt, int64_t dts)
     if (ret < 0)
         return ret;
 
-    opkt->time_base = ost->mux_timebase;
+    ts_offset = av_rescale_q(start_time, AV_TIME_BASE_Q, opkt->time_base);
 
     if (pkt->pts != AV_NOPTS_VALUE)
-        opkt->pts = av_rescale_q(pkt->pts, pkt->time_base, opkt->time_base) - ost_tb_start_time;
+        opkt->pts -= ts_offset;
 
     if (pkt->dts == AV_NOPTS_VALUE) {
         opkt->dts = av_rescale_q(dts, AV_TIME_BASE_Q, opkt->time_base);
@@ -436,16 +436,13 @@ int of_streamcopy(OutputStream *ost, const AVPacket *pkt, int64_t dts)
         int duration = av_get_audio_frame_duration2(ost->par_in, pkt->size);
         if(!duration)
             duration = ost->par_in->frame_size;
-        opkt->dts = av_rescale_delta(pkt->time_base, pkt->dts,
+        opkt->dts = av_rescale_delta(opkt->time_base, pkt->dts,
                                     (AVRational){1, ost->par_in->sample_rate}, duration,
                                     &ms->ts_rescale_delta_last, opkt->time_base);
         /* dts will be set immediately afterwards to what pts is now */
-        opkt->pts = opkt->dts - ost_tb_start_time;
-    } else
-        opkt->dts = av_rescale_q(pkt->dts, pkt->time_base, opkt->time_base);
-    opkt->dts -= ost_tb_start_time;
-
-    opkt->duration = av_rescale_q(pkt->duration, pkt->time_base, opkt->time_base);
+        opkt->pts = opkt->dts - ts_offset;
+    }
+    opkt->dts -= ts_offset;
 
     {
         int ret = trigger_fix_sub_duration_heartbeat(ost, pkt);
@@ -511,10 +508,6 @@ static int thread_start(Muxer *mux)
         MuxStream     *ms = ms_from_ost(ost);
         AVPacket *pkt;
 
-        /* try to improve muxing time_base (only possible if nothing has been written yet) */
-        if (!av_fifo_can_read(ms->muxing_queue))
-            ost->mux_timebase = ost->st->time_base;
-
         while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0) {
             ret = thread_submit_packet(mux, ost, pkt);
             if (pkt) {
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index 0289cdabad..cf4cd2d5b7 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -1090,8 +1090,6 @@ static int streamcopy_init(const Muxer *mux, OutputStream *ost)
         }
     }
 
-    ost->mux_timebase = ist->st->time_base;
-
 fail:
     avcodec_free_context(&codec_ctx);
     av_dict_free(&codec_opts);
-- 
2.40.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [FFmpeg-devel] [PATCH 05/18] fftools/ffmpeg_enc: factor out setting encoder timebase
  2023-08-26 15:11 [FFmpeg-devel] [PATCH 01/18] fftools/ffmpeg: stop explicitly closing output streams on input EOF Anton Khirnov
                   ` (2 preceding siblings ...)
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 04/18] fftools/ffmpeg_mux: stop rescaling timestamps in of_streamcopy() Anton Khirnov
@ 2023-08-26 15:11 ` Anton Khirnov
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 06/18] fftools/ffmpeg_enc: reindent after previous commit Anton Khirnov
                   ` (12 subsequent siblings)
  16 siblings, 0 replies; 21+ messages in thread
From: Anton Khirnov @ 2023-08-26 15:11 UTC (permalink / raw)
  To: ffmpeg-devel

Mainly this fixes handling special values of -enc_time_base ('demux' or
'filter') for audio. It also prints a warning if -enc_time_base is
specified for subtitles, instead of ignoring it silently (current
subtitle encoding API only works with AV_TIME_BASE_Q).
---
 fftools/ffmpeg_enc.c | 154 ++++++++++++++++++++++++++-----------------
 1 file changed, 93 insertions(+), 61 deletions(-)

diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index 3c9fdd3237..c4874b4d78 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -190,6 +190,93 @@ static int set_encoder_id(OutputFile *of, OutputStream *ost)
     return 0;
 }
 
+static int enc_choose_timebase(OutputStream *ost, AVFrame *frame)
+{
+    const OutputFile *of = output_files[ost->file_index];
+    AVCodecContext  *enc = ost->enc_ctx;
+    AVRational        tb = (AVRational){ 0, 0 };
+    AVRational fr;
+    FrameData *fd;
+
+    if (ost->type == AVMEDIA_TYPE_SUBTITLE) {
+        if (ost->enc_timebase.num)
+            av_log(ost, AV_LOG_WARNING,
+                   "-enc_time_base not supported for subtitles, ignoring\n");
+        enc->time_base = AV_TIME_BASE_Q;
+        return 0;
+    }
+
+    fd = frame_data(frame);
+
+    // apply -enc_time_base
+    if (ost->enc_timebase.num == ENC_TIME_BASE_DEMUX &&
+        (fd->dec.tb.num <= 0 || fd->dec.tb.den <= 0)) {
+        av_log(ost, AV_LOG_ERROR,
+               "Demuxing timebase not available - cannot use it for encoding\n");
+        return AVERROR(EINVAL);
+    }
+
+    switch (ost->enc_timebase.num) {
+    case 0:                                            break;
+    case ENC_TIME_BASE_DEMUX:  tb = fd->dec.tb;        break;
+    case ENC_TIME_BASE_FILTER: tb = frame->time_base;  break;
+    default:                   tb = ost->enc_timebase; break;
+    }
+
+    if (ost->type == AVMEDIA_TYPE_AUDIO) {
+        enc->time_base = tb.num ? tb : (AVRational){ 1, frame->sample_rate };
+        return 0;
+    }
+
+        fr = ost->frame_rate;
+        if (!fr.num)
+            fr = fd->frame_rate_filter;
+        if (!fr.num && !ost->max_frame_rate.num) {
+            fr = (AVRational){25, 1};
+            av_log(ost, AV_LOG_WARNING,
+                   "No information "
+                   "about the input framerate is available. Falling "
+                   "back to a default value of 25fps. Use the -r option "
+                   "if you want a different framerate.\n");
+        }
+
+        if (ost->max_frame_rate.num &&
+            (av_q2d(fr) > av_q2d(ost->max_frame_rate) ||
+            !fr.den))
+            fr = ost->max_frame_rate;
+
+        if (enc->codec->supported_framerates && !ost->force_fps) {
+            int idx = av_find_nearest_q_idx(fr, enc->codec->supported_framerates);
+            fr = enc->codec->supported_framerates[idx];
+        }
+        // reduce frame rate for mpeg4 to be within the spec limits
+        if (enc->codec_id == AV_CODEC_ID_MPEG4) {
+            av_reduce(&fr.num, &fr.den,
+                      fr.num, fr.den, 65535);
+        }
+
+        if (av_q2d(fr) > 1e3 && ost->vsync_method != VSYNC_PASSTHROUGH &&
+            (ost->vsync_method == VSYNC_CFR || ost->vsync_method == VSYNC_VSCFR ||
+            (ost->vsync_method == VSYNC_AUTO && !(of->format->flags & AVFMT_VARIABLE_FPS)))){
+            av_log(ost, AV_LOG_WARNING, "Frame rate very high for a muxer not efficiently supporting it.\n"
+                                        "Please consider specifying a lower framerate, a different muxer or "
+                                        "setting vsync/fps_mode to vfr\n");
+        }
+
+        enc->framerate = fr;
+
+        ost->st->avg_frame_rate = fr;
+
+    if (!(tb.num > 0 && tb.den > 0))
+        tb = av_inv_q(fr);
+    if (!(tb.num > 0 && tb.den > 0))
+        tb = frame->time_base;
+
+    enc->time_base = tb;
+
+    return 0;
+}
+
 int enc_open(OutputStream *ost, AVFrame *frame)
 {
     InputStream *ist = ost->ist;
@@ -221,6 +308,12 @@ int enc_open(OutputStream *ost, AVFrame *frame)
         dec_ctx = ist->dec_ctx;
     }
 
+    ret = enc_choose_timebase(ost, frame);
+    if (ret < 0) {
+        av_log(ost, AV_LOG_ERROR, "Could not choose a time base for encoding\n");
+        return AVERROR(EINVAL);
+    }
+
     switch (enc_ctx->codec_type) {
     case AVMEDIA_TYPE_AUDIO:
         enc_ctx->sample_fmt     = frame->format;
@@ -234,65 +327,9 @@ int enc_open(OutputStream *ost, AVFrame *frame)
         else
             enc_ctx->bits_per_raw_sample = FFMIN(fd->bits_per_raw_sample,
                                                  av_get_bytes_per_sample(enc_ctx->sample_fmt) << 3);
-
-        enc_ctx->time_base = ost->enc_timebase.num > 0 ? ost->enc_timebase :
-                             av_make_q(1, enc_ctx->sample_rate);
         break;
 
     case AVMEDIA_TYPE_VIDEO: {
-        AVRational fr = ost->frame_rate;
-
-        if (!fr.num)
-            fr = fd->frame_rate_filter;
-        if (!fr.num && !ost->max_frame_rate.num) {
-            fr = (AVRational){25, 1};
-            av_log(ost, AV_LOG_WARNING,
-                   "No information "
-                   "about the input framerate is available. Falling "
-                   "back to a default value of 25fps. Use the -r option "
-                   "if you want a different framerate.\n");
-        }
-
-        if (ost->max_frame_rate.num &&
-            (av_q2d(fr) > av_q2d(ost->max_frame_rate) ||
-            !fr.den))
-            fr = ost->max_frame_rate;
-
-        if (enc->supported_framerates && !ost->force_fps) {
-            int idx = av_find_nearest_q_idx(fr, enc->supported_framerates);
-            fr = enc->supported_framerates[idx];
-        }
-        // reduce frame rate for mpeg4 to be within the spec limits
-        if (enc_ctx->codec_id == AV_CODEC_ID_MPEG4) {
-            av_reduce(&fr.num, &fr.den,
-                      fr.num, fr.den, 65535);
-        }
-
-        if (ost->enc_timebase.num == ENC_TIME_BASE_DEMUX) {
-            if (fd->dec.tb.num <= 0 || fd->dec.tb.den <= 0) {
-                av_log(ost, AV_LOG_ERROR,
-                       "Demuxing timebase not available - cannot use it for encoding\n");
-                return AVERROR(EINVAL);
-            }
-
-            enc_ctx->time_base = fd->dec.tb;
-        } else if (ost->enc_timebase.num == ENC_TIME_BASE_FILTER) {
-            enc_ctx->time_base = frame->time_base;
-        } else {
-            enc_ctx->time_base = ost->enc_timebase.num > 0 ? ost->enc_timebase :
-                                 av_inv_q(fr);
-        }
-
-        if (!(enc_ctx->time_base.num && enc_ctx->time_base.den))
-            enc_ctx->time_base = frame->time_base;
-        if (   av_q2d(enc_ctx->time_base) < 0.001 && ost->vsync_method != VSYNC_PASSTHROUGH
-           && (ost->vsync_method == VSYNC_CFR || ost->vsync_method == VSYNC_VSCFR ||
-               (ost->vsync_method == VSYNC_AUTO && !(of->format->flags & AVFMT_VARIABLE_FPS)))){
-            av_log(ost, AV_LOG_WARNING, "Frame rate very high for a muxer not efficiently supporting it.\n"
-                                        "Please consider specifying a lower framerate, a different muxer or "
-                                        "setting vsync/fps_mode to vfr\n");
-        }
-
         enc_ctx->width  = frame->width;
         enc_ctx->height = frame->height;
         enc_ctx->sample_aspect_ratio = ost->st->sample_aspect_ratio =
@@ -314,10 +351,6 @@ int enc_open(OutputStream *ost, AVFrame *frame)
         enc_ctx->colorspace             = frame->colorspace;
         enc_ctx->chroma_sample_location = frame->chroma_location;
 
-        enc_ctx->framerate = fr;
-
-        ost->st->avg_frame_rate = fr;
-
         // Field order: autodetection
         if (enc_ctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME) &&
             ost->top_field_first >= 0)
@@ -344,7 +377,6 @@ int enc_open(OutputStream *ost, AVFrame *frame)
         break;
         }
     case AVMEDIA_TYPE_SUBTITLE:
-        enc_ctx->time_base = AV_TIME_BASE_Q;
         if (!enc_ctx->width) {
             enc_ctx->width     = ost->ist->par->width;
             enc_ctx->height    = ost->ist->par->height;
-- 
2.40.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [FFmpeg-devel] [PATCH 06/18] fftools/ffmpeg_enc: reindent after previous commit
  2023-08-26 15:11 [FFmpeg-devel] [PATCH 01/18] fftools/ffmpeg: stop explicitly closing output streams on input EOF Anton Khirnov
                   ` (3 preceding siblings ...)
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 05/18] fftools/ffmpeg_enc: factor out setting encoder timebase Anton Khirnov
@ 2023-08-26 15:11 ` Anton Khirnov
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 07/18] fftools/ffmpeg_enc: only use fallback framerate when encoding CFR Anton Khirnov
                   ` (11 subsequent siblings)
  16 siblings, 0 replies; 21+ messages in thread
From: Anton Khirnov @ 2023-08-26 15:11 UTC (permalink / raw)
  To: ffmpeg-devel

---
 fftools/ffmpeg_enc.c | 66 ++++++++++++++++++++++----------------------
 1 file changed, 33 insertions(+), 33 deletions(-)

diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index c4874b4d78..4b5bd3d9b4 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -228,44 +228,44 @@ static int enc_choose_timebase(OutputStream *ost, AVFrame *frame)
         return 0;
     }
 
-        fr = ost->frame_rate;
-        if (!fr.num)
-            fr = fd->frame_rate_filter;
-        if (!fr.num && !ost->max_frame_rate.num) {
-            fr = (AVRational){25, 1};
-            av_log(ost, AV_LOG_WARNING,
-                   "No information "
-                   "about the input framerate is available. Falling "
-                   "back to a default value of 25fps. Use the -r option "
-                   "if you want a different framerate.\n");
-        }
+    fr = ost->frame_rate;
+    if (!fr.num)
+        fr = fd->frame_rate_filter;
+    if (!fr.num && !ost->max_frame_rate.num) {
+        fr = (AVRational){25, 1};
+        av_log(ost, AV_LOG_WARNING,
+               "No information "
+               "about the input framerate is available. Falling "
+               "back to a default value of 25fps. Use the -r option "
+               "if you want a different framerate.\n");
+    }
 
-        if (ost->max_frame_rate.num &&
-            (av_q2d(fr) > av_q2d(ost->max_frame_rate) ||
-            !fr.den))
-            fr = ost->max_frame_rate;
+    if (ost->max_frame_rate.num &&
+        (av_q2d(fr) > av_q2d(ost->max_frame_rate) ||
+        !fr.den))
+        fr = ost->max_frame_rate;
 
-        if (enc->codec->supported_framerates && !ost->force_fps) {
-            int idx = av_find_nearest_q_idx(fr, enc->codec->supported_framerates);
-            fr = enc->codec->supported_framerates[idx];
-        }
-        // reduce frame rate for mpeg4 to be within the spec limits
-        if (enc->codec_id == AV_CODEC_ID_MPEG4) {
-            av_reduce(&fr.num, &fr.den,
-                      fr.num, fr.den, 65535);
-        }
+    if (enc->codec->supported_framerates && !ost->force_fps) {
+        int idx = av_find_nearest_q_idx(fr, enc->codec->supported_framerates);
+        fr = enc->codec->supported_framerates[idx];
+    }
+    // reduce frame rate for mpeg4 to be within the spec limits
+    if (enc->codec_id == AV_CODEC_ID_MPEG4) {
+        av_reduce(&fr.num, &fr.den,
+                  fr.num, fr.den, 65535);
+    }
 
-        if (av_q2d(fr) > 1e3 && ost->vsync_method != VSYNC_PASSTHROUGH &&
-            (ost->vsync_method == VSYNC_CFR || ost->vsync_method == VSYNC_VSCFR ||
-            (ost->vsync_method == VSYNC_AUTO && !(of->format->flags & AVFMT_VARIABLE_FPS)))){
-            av_log(ost, AV_LOG_WARNING, "Frame rate very high for a muxer not efficiently supporting it.\n"
-                                        "Please consider specifying a lower framerate, a different muxer or "
-                                        "setting vsync/fps_mode to vfr\n");
-        }
+    if (av_q2d(fr) > 1e3 && ost->vsync_method != VSYNC_PASSTHROUGH &&
+        (ost->vsync_method == VSYNC_CFR || ost->vsync_method == VSYNC_VSCFR ||
+        (ost->vsync_method == VSYNC_AUTO && !(of->format->flags & AVFMT_VARIABLE_FPS)))){
+        av_log(ost, AV_LOG_WARNING, "Frame rate very high for a muxer not efficiently supporting it.\n"
+                                    "Please consider specifying a lower framerate, a different muxer or "
+                                    "setting vsync/fps_mode to vfr\n");
+    }
 
-        enc->framerate = fr;
+    enc->framerate = fr;
 
-        ost->st->avg_frame_rate = fr;
+    ost->st->avg_frame_rate = fr;
 
     if (!(tb.num > 0 && tb.den > 0))
         tb = av_inv_q(fr);
-- 
2.40.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [FFmpeg-devel] [PATCH 07/18] fftools/ffmpeg_enc: only use fallback framerate when encoding CFR
  2023-08-26 15:11 [FFmpeg-devel] [PATCH 01/18] fftools/ffmpeg: stop explicitly closing output streams on input EOF Anton Khirnov
                   ` (4 preceding siblings ...)
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 06/18] fftools/ffmpeg_enc: reindent after previous commit Anton Khirnov
@ 2023-08-26 15:11 ` Anton Khirnov
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 08/18] fftools/ffmpeg_enc: reindent after previous commit Anton Khirnov
                   ` (10 subsequent siblings)
  16 siblings, 0 replies; 21+ messages in thread
From: Anton Khirnov @ 2023-08-26 15:11 UTC (permalink / raw)
  To: ffmpeg-devel

When no output video framerate is specified by the user with -r or can
be inferred from the filtergraph, encoder setup will arbitrarily decide
that the framerate is 25fps. However, making up any framerate value for
VFR encoding is at best unnecessary.

Changes the results of the sub2video tests, where the input timebase is
now used instead of 1/25.
---
 fftools/ffmpeg_enc.c                  |  29 ++--
 tests/ref/fate/sub2video_basic        | 182 +++++++++++++-------------
 tests/ref/fate/sub2video_time_limited |   8 +-
 3 files changed, 112 insertions(+), 107 deletions(-)

diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index 4b5bd3d9b4..80a49fe606 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -231,20 +231,24 @@ static int enc_choose_timebase(OutputStream *ost, AVFrame *frame)
     fr = ost->frame_rate;
     if (!fr.num)
         fr = fd->frame_rate_filter;
-    if (!fr.num && !ost->max_frame_rate.num) {
-        fr = (AVRational){25, 1};
-        av_log(ost, AV_LOG_WARNING,
-               "No information "
-               "about the input framerate is available. Falling "
-               "back to a default value of 25fps. Use the -r option "
-               "if you want a different framerate.\n");
+
+    if (ost->is_cfr) {
+        if (!fr.num && !ost->max_frame_rate.num) {
+            fr = (AVRational){25, 1};
+            av_log(ost, AV_LOG_WARNING,
+                   "No information "
+                   "about the input framerate is available. Falling "
+                   "back to a default value of 25fps. Use the -r option "
+                   "if you want a different framerate.\n");
+        }
+
+        if (ost->max_frame_rate.num &&
+            (av_q2d(fr) > av_q2d(ost->max_frame_rate) ||
+            !fr.den))
+            fr = ost->max_frame_rate;
     }
 
-    if (ost->max_frame_rate.num &&
-        (av_q2d(fr) > av_q2d(ost->max_frame_rate) ||
-        !fr.den))
-        fr = ost->max_frame_rate;
-
+    if (fr.num > 0) {
     if (enc->codec->supported_framerates && !ost->force_fps) {
         int idx = av_find_nearest_q_idx(fr, enc->codec->supported_framerates);
         fr = enc->codec->supported_framerates[idx];
@@ -254,6 +258,7 @@ static int enc_choose_timebase(OutputStream *ost, AVFrame *frame)
         av_reduce(&fr.num, &fr.den,
                   fr.num, fr.den, 65535);
     }
+    }
 
     if (av_q2d(fr) > 1e3 && ost->vsync_method != VSYNC_PASSTHROUGH &&
         (ost->vsync_method == VSYNC_CFR || ost->vsync_method == VSYNC_VSCFR ||
diff --git a/tests/ref/fate/sub2video_basic b/tests/ref/fate/sub2video_basic
index 5f72e292c9..a6eb1a34ea 100644
--- a/tests/ref/fate/sub2video_basic
+++ b/tests/ref/fate/sub2video_basic
@@ -1,95 +1,95 @@
-#tb 0: 1/25
+#tb 0: 1/1000
 #media_type 0: video
 #codec_id 0: rawvideo
 #dimensions 0: 720x480
 #sar 0: 0/1
-0,       3312,       3312,        1,  1382400, 0x00000000
-0,       3312,       3312,        1,  1382400, 0x8c93c2ba
-0,       3436,       3436,        1,  1382400, 0x00000000
-0,       3684,       3684,        1,  1382400, 0xb02e32ca
-0,       3802,       3802,        1,  1382400, 0x00000000
-0,       4520,       4520,        1,  1382400, 0x83b71116
-0,       4584,       4584,        1,  1382400, 0x00000000
-0,       4586,       4586,        1,  1382400, 0x85547fd1
-0,       4645,       4645,        1,  1382400, 0x00000000
-0,       4648,       4648,        1,  1382400, 0x00000000
-0,       4648,       4648,        1,  1382400, 0xb6a8f181
-0,       4715,       4715,        1,  1382400, 0x00000000
-0,       4717,       4717,        1,  1382400, 0xb64d1a2c
-0,       4748,       4748,        1,  1382400, 0x00000000
-0,       4750,       4750,        1,  1382400, 0x7b37ecf3
-0,       4792,       4792,        1,  1382400, 0x00000000
-0,       4993,       4993,        1,  1382400, 0xdc025bd1
-0,       5027,       5027,        1,  1382400, 0x00000000
-0,       5029,       5029,        1,  1382400, 0x688b294d
-0,       5068,       5068,        1,  1382400, 0x00000000
-0,       5070,       5070,        1,  1382400, 0xa2b33d1b
-0,       5117,       5117,        1,  1382400, 0x00000000
-0,       5119,       5119,        1,  1382400, 0xb3e525e3
-0,       5168,       5168,        1,  1382400, 0x00000000
-0,       5170,       5170,        1,  1382400, 0xaa8fbdd7
-0,       5216,       5216,        1,  1382400, 0x00000000
-0,       5218,       5218,        1,  1382400, 0x7b7f26dd
-0,       5249,       5249,        1,  1382400, 0x00000000
-0,       5251,       5251,        1,  1382400, 0x15e2f836
-0,       5289,       5289,        1,  1382400, 0x00000000
-0,       5291,       5291,        1,  1382400, 0x0fee9b0c
-0,       5358,       5358,        1,  1382400, 0x00000000
-0,       5360,       5360,        1,  1382400, 0x89d62791
-0,       5429,       5429,        1,  1382400, 0x00000000
-0,       5431,       5431,        1,  1382400, 0xa6a9fd74
-0,       5490,       5490,        1,  1382400, 0x00000000
-0,       5491,       5491,        1,  1382400, 0x7896178d
-0,       5537,       5537,        1,  1382400, 0x00000000
-0,       5588,       5588,        1,  1382400, 0x01751a52
-0,       5647,       5647,        1,  1382400, 0x00000000
-0,       5688,       5688,        1,  1382400, 0xa3959c6f
-0,       5770,       5770,        1,  1382400, 0x00000000
-0,       5772,       5772,        1,  1382400, 0x3d3ea47b
-0,       5826,       5826,        1,  1382400, 0x00000000
-0,       5828,       5828,        1,  1382400, 0x593f8b24
-0,       5931,       5931,        1,  1382400, 0x00000000
-0,       5933,       5933,        1,  1382400, 0x171f05ba
-0,       6001,       6001,        1,  1382400, 0x00000000
-0,       6003,       6003,        1,  1382400, 0xb014cdf1
-0,       6054,       6054,        1,  1382400, 0x00000000
-0,       6839,       6839,        1,  1382400, 0xd918e667
-0,       6880,       6880,        1,  1382400, 0x00000000
-0,       7386,       7386,        1,  1382400, 0xc9406331
-0,       7419,       7419,        1,  1382400, 0x00000000
-0,       7501,       7501,        1,  1382400, 0xaf08b10d
-0,       7549,       7549,        1,  1382400, 0x00000000
-0,       7551,       7551,        1,  1382400, 0x00000000
-0,       7551,       7551,        1,  1382400, 0x853a9d93
-0,       7589,       7589,        1,  1382400, 0x00000000
-0,       7605,       7605,        1,  1382400, 0x7491a87d
-0,       7647,       7647,        1,  1382400, 0x00000000
-0,       7649,       7649,        1,  1382400, 0xf7383c58
-0,       7697,       7697,        1,  1382400, 0x00000000
-0,       7699,       7699,        1,  1382400, 0xe66be411
-0,       7743,       7743,        1,  1382400, 0x00000000
-0,       8032,       8032,        1,  1382400, 0xd6850362
-0,       8082,       8082,        1,  1382400, 0x00000000
-0,       8084,       8084,        1,  1382400, 0x3e1ed109
-0,       8115,       8115,        1,  1382400, 0x00000000
-0,       8116,       8116,        1,  1382400, 0x39c1b7bd
-0,       8160,       8160,        1,  1382400, 0x00000000
-0,       8180,       8180,        1,  1382400, 0x35b85f2e
-0,       8207,       8207,        1,  1382400, 0x00000000
-0,       8209,       8209,        1,  1382400, 0x00000000
-0,       8209,       8209,        1,  1382400, 0x83f103e5
-0,       8247,       8247,        1,  1382400, 0x00000000
-0,       8249,       8249,        1,  1382400, 0xbc1ca9b3
-0,       8278,       8278,        1,  1382400, 0x00000000
-0,       8281,       8281,        1,  1382400, 0x94d4a51e
-0,       8321,       8321,        1,  1382400, 0x00000000
-0,       8323,       8323,        1,  1382400, 0xf88cdfde
-0,       8367,       8367,        1,  1382400, 0x00000000
-0,       8565,       8565,        1,  1382400, 0xdd51423b
-0,       8611,       8611,        1,  1382400, 0x00000000
-0,       8669,       8669,        1,  1382400, 0x08259fa4
-0,       8708,       8708,        1,  1382400, 0x00000000
-0,       8941,       8941,        1,  1382400, 0x1663fa34
-0,       8994,       8994,        1,  1382400, 0x00000000
-0,       8996,       8996,        1,  1382400, 0xda2ceb55
-0,       9027,       9027,        1,  1382400, 0x00000000
+0,     132499,     132499,        0,  1382400, 0x00000000
+0,     132499,     132499,        0,  1382400, 0x8c93c2ba
+0,     137459,     137459,        0,  1382400, 0x00000000
+0,     147355,     147355,        0,  1382400, 0xb02e32ca
+0,     152088,     152088,        0,  1382400, 0x00000000
+0,     180797,     180797,        0,  1382400, 0x83b71116
+0,     183357,     183357,        0,  1382400, 0x00000000
+0,     183433,     183433,        0,  1382400, 0x85547fd1
+0,     185799,     185799,        0,  1382400, 0x00000000
+0,     185909,     185909,        0,  1382400, 0x00000000
+0,     185910,     185910,        0,  1382400, 0xb6a8f181
+0,     188606,     188606,        0,  1382400, 0x00000000
+0,     188663,     188663,        0,  1382400, 0xb64d1a2c
+0,     189925,     189925,        0,  1382400, 0x00000000
+0,     190014,     190014,        0,  1382400, 0x7b37ecf3
+0,     191675,     191675,        0,  1382400, 0x00000000
+0,     199724,     199724,        0,  1382400, 0xdc025bd1
+0,     201089,     201089,        0,  1382400, 0x00000000
+0,     201175,     201175,        0,  1382400, 0x688b294d
+0,     202733,     202733,        0,  1382400, 0x00000000
+0,     202819,     202819,        0,  1382400, 0xa2b33d1b
+0,     204684,     204684,        0,  1382400, 0x00000000
+0,     204762,     204762,        0,  1382400, 0xb3e525e3
+0,     206730,     206730,        0,  1382400, 0x00000000
+0,     206806,     206806,        0,  1382400, 0xaa8fbdd7
+0,     208637,     208637,        0,  1382400, 0x00000000
+0,     208716,     208716,        0,  1382400, 0x7b7f26dd
+0,     209978,     209978,        0,  1382400, 0x00000000
+0,     210051,     210051,        0,  1382400, 0x15e2f836
+0,     211575,     211575,        0,  1382400, 0x00000000
+0,     211644,     211644,        0,  1382400, 0x0fee9b0c
+0,     214306,     214306,        0,  1382400, 0x00000000
+0,     214380,     214380,        0,  1382400, 0x89d62791
+0,     217144,     217144,        0,  1382400, 0x00000000
+0,     217225,     217225,        0,  1382400, 0xa6a9fd74
+0,     219591,     219591,        0,  1382400, 0x00000000
+0,     219652,     219652,        0,  1382400, 0x7896178d
+0,     221483,     221483,        0,  1382400, 0x00000000
+0,     223531,     223531,        0,  1382400, 0x01751a52
+0,     225863,     225863,        0,  1382400, 0x00000000
+0,     227510,     227510,        0,  1382400, 0xa3959c6f
+0,     230809,     230809,        0,  1382400, 0x00000000
+0,     230872,     230872,        0,  1382400, 0x3d3ea47b
+0,     233033,     233033,        0,  1382400, 0x00000000
+0,     233124,     233124,        0,  1382400, 0x593f8b24
+0,     237220,     237220,        0,  1382400, 0x00000000
+0,     237303,     237303,        0,  1382400, 0x171f05ba
+0,     240033,     240033,        0,  1382400, 0x00000000
+0,     240106,     240106,        0,  1382400, 0xb014cdf1
+0,     242165,     242165,        0,  1382400, 0x00000000
+0,     273556,     273556,        0,  1382400, 0xd918e667
+0,     275217,     275217,        0,  1382400, 0x00000000
+0,     295445,     295445,        0,  1382400, 0xc9406331
+0,     296776,     296776,        0,  1382400, 0x00000000
+0,     300049,     300049,        0,  1382400, 0xaf08b10d
+0,     301949,     301949,        0,  1382400, 0x00000000
+0,     302034,     302034,        0,  1382400, 0x00000000
+0,     302035,     302035,        0,  1382400, 0x853a9d93
+0,     303559,     303559,        0,  1382400, 0x00000000
+0,     304203,     304203,        0,  1382400, 0x7491a87d
+0,     305898,     305898,        0,  1382400, 0x00000000
+0,     305947,     305947,        0,  1382400, 0xf7383c58
+0,     307881,     307881,        0,  1382400, 0x00000000
+0,     307957,     307957,        0,  1382400, 0xe66be411
+0,     309720,     309720,        0,  1382400, 0x00000000
+0,     321295,     321295,        0,  1382400, 0xd6850362
+0,     323263,     323263,        0,  1382400, 0x00000000
+0,     323356,     323356,        0,  1382400, 0x3e1ed109
+0,     324584,     324584,        0,  1382400, 0x00000000
+0,     324640,     324640,        0,  1382400, 0x39c1b7bd
+0,     326403,     326403,        0,  1382400, 0x00000000
+0,     327193,     327193,        0,  1382400, 0x35b85f2e
+0,     328285,     328285,        0,  1382400, 0x00000000
+0,     328360,     328360,        0,  1382400, 0x00000000
+0,     328361,     328361,        0,  1382400, 0x83f103e5
+0,     329885,     329885,        0,  1382400, 0x00000000
+0,     329946,     329946,        0,  1382400, 0xbc1ca9b3
+0,     331106,     331106,        0,  1382400, 0x00000000
+0,     331230,     331230,        0,  1382400, 0x94d4a51e
+0,     332857,     332857,        0,  1382400, 0x00000000
+0,     332924,     332924,        0,  1382400, 0xf88cdfde
+0,     334687,     334687,        0,  1382400, 0x00000000
+0,     342600,     342600,        0,  1382400, 0xdd51423b
+0,     344431,     344431,        0,  1382400, 0x00000000
+0,     346771,     346771,        0,  1382400, 0x08259fa4
+0,     348329,     348329,        0,  1382400, 0x00000000
+0,     357640,     357640,        0,  1382400, 0x1663fa34
+0,     359767,     359767,        0,  1382400, 0x00000000
+0,     359834,     359834,        0,  1382400, 0xda2ceb55
+0,     361096,     361096,        0,  1382400, 0x00000000
diff --git a/tests/ref/fate/sub2video_time_limited b/tests/ref/fate/sub2video_time_limited
index 9fb6fb06f9..c7d48d639f 100644
--- a/tests/ref/fate/sub2video_time_limited
+++ b/tests/ref/fate/sub2video_time_limited
@@ -1,8 +1,8 @@
-#tb 0: 1/25
+#tb 0: 1/90000
 #media_type 0: video
 #codec_id 0: rawvideo
 #dimensions 0: 1920x1080
 #sar 0: 0/1
-0,          2,          2,        1,  8294400, 0x00000000
-0,          2,          2,        1,  8294400, 0xa87c518f
-0,         10,         10,        1,  8294400, 0xa87c518f
+0,       6072,       6072,        0,  8294400, 0x00000000
+0,       6072,       6072,        0,  8294400, 0xa87c518f
+0,      36101,      36101,        0,  8294400, 0xa87c518f
-- 
2.40.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [FFmpeg-devel] [PATCH 08/18] fftools/ffmpeg_enc: reindent after previous commit
  2023-08-26 15:11 [FFmpeg-devel] [PATCH 01/18] fftools/ffmpeg: stop explicitly closing output streams on input EOF Anton Khirnov
                   ` (5 preceding siblings ...)
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 07/18] fftools/ffmpeg_enc: only use fallback framerate when encoding CFR Anton Khirnov
@ 2023-08-26 15:11 ` Anton Khirnov
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 09/18] fftools/ffmpeg_filter: sanitize framerate retrieved from the filtergraph Anton Khirnov
                   ` (9 subsequent siblings)
  16 siblings, 0 replies; 21+ messages in thread
From: Anton Khirnov @ 2023-08-26 15:11 UTC (permalink / raw)
  To: ffmpeg-devel

---
 fftools/ffmpeg_enc.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index 80a49fe606..f28884e50c 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -249,15 +249,15 @@ static int enc_choose_timebase(OutputStream *ost, AVFrame *frame)
     }
 
     if (fr.num > 0) {
-    if (enc->codec->supported_framerates && !ost->force_fps) {
-        int idx = av_find_nearest_q_idx(fr, enc->codec->supported_framerates);
-        fr = enc->codec->supported_framerates[idx];
-    }
-    // reduce frame rate for mpeg4 to be within the spec limits
-    if (enc->codec_id == AV_CODEC_ID_MPEG4) {
-        av_reduce(&fr.num, &fr.den,
-                  fr.num, fr.den, 65535);
-    }
+        if (enc->codec->supported_framerates && !ost->force_fps) {
+            int idx = av_find_nearest_q_idx(fr, enc->codec->supported_framerates);
+            fr = enc->codec->supported_framerates[idx];
+        }
+        // reduce frame rate for mpeg4 to be within the spec limits
+        if (enc->codec_id == AV_CODEC_ID_MPEG4) {
+            av_reduce(&fr.num, &fr.den,
+                      fr.num, fr.den, 65535);
+        }
     }
 
     if (av_q2d(fr) > 1e3 && ost->vsync_method != VSYNC_PASSTHROUGH &&
-- 
2.40.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [FFmpeg-devel] [PATCH 09/18] fftools/ffmpeg_filter: sanitize framerate retrieved from the filtergraph
  2023-08-26 15:11 [FFmpeg-devel] [PATCH 01/18] fftools/ffmpeg: stop explicitly closing output streams on input EOF Anton Khirnov
                   ` (6 preceding siblings ...)
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 08/18] fftools/ffmpeg_enc: reindent after previous commit Anton Khirnov
@ 2023-08-26 15:11 ` Anton Khirnov
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 10/18] fftools/ffmpeg: move derivation of frame duration from filter framerate Anton Khirnov
                   ` (8 subsequent siblings)
  16 siblings, 0 replies; 21+ messages in thread
From: Anton Khirnov @ 2023-08-26 15:11 UTC (permalink / raw)
  To: ffmpeg-devel

Lavfi uses 1/0 to signal unknown/VFR, which should not be passed to
encoders.
---
 fftools/ffmpeg_filter.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 925b5116cc..177a6f7e5c 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -1805,8 +1805,11 @@ int reap_filters(FilterGraph *fg, int flush)
             if (!fgp->is_meta)
                 fd->bits_per_raw_sample = 0;
 
-            if (ost->type == AVMEDIA_TYPE_VIDEO)
-                fd->frame_rate_filter = av_buffersink_get_frame_rate(filter);
+            if (ost->type == AVMEDIA_TYPE_VIDEO) {
+                AVRational fr = av_buffersink_get_frame_rate(filter);
+                if (fr.num > 0 && fr.den > 0)
+                    fd->frame_rate_filter = fr;
+            }
 
             ret = enc_frame(ost, filtered_frame);
             av_frame_unref(filtered_frame);
-- 
2.40.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [FFmpeg-devel] [PATCH 10/18] fftools/ffmpeg: move derivation of frame duration from filter framerate
  2023-08-26 15:11 [FFmpeg-devel] [PATCH 01/18] fftools/ffmpeg: stop explicitly closing output streams on input EOF Anton Khirnov
                   ` (7 preceding siblings ...)
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 09/18] fftools/ffmpeg_filter: sanitize framerate retrieved from the filtergraph Anton Khirnov
@ 2023-08-26 15:11 ` Anton Khirnov
  2023-08-29  8:07   ` Anton Khirnov
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 11/18] fftools/ffmpeg_enc: move handling video frame duration to video_sync_process() Anton Khirnov
                   ` (7 subsequent siblings)
  16 siblings, 1 reply; 21+ messages in thread
From: Anton Khirnov @ 2023-08-26 15:11 UTC (permalink / raw)
  To: ffmpeg-devel

From ffmpeg_enc to ffmpeg_filter, which is a more appropriate
place for it.

fate-lavf-gxf* no longer spuriously duplicate the first video frame, due
to different rounding.
---
 fftools/ffmpeg_enc.c    | 9 +--------
 fftools/ffmpeg_filter.c | 8 +++++++-
 tests/ref/lavf/gxf      | 2 +-
 tests/ref/lavf/gxf_ntsc | 2 +-
 tests/ref/lavf/gxf_pal  | 2 +-
 5 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index f28884e50c..0586ba17c4 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -1112,16 +1112,9 @@ static int do_video_out(OutputFile *of, OutputStream *ost, AVFrame *frame)
     int64_t nb_frames, nb_frames_prev, i;
     double duration = 0;
 
-    if (frame) {
-        FrameData *fd = frame_data(frame);
-
+    if (frame)
         duration = lrintf(frame->duration * av_q2d(frame->time_base) / av_q2d(enc->time_base));
 
-        if (duration <= 0 &&
-            fd->frame_rate_filter.num > 0 && fd->frame_rate_filter.den > 0)
-            duration = 1 / (av_q2d(fd->frame_rate_filter) * av_q2d(enc->time_base));
-    }
-
     video_sync_process(of, ost, frame, duration,
                        &nb_frames, &nb_frames_prev);
 
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 177a6f7e5c..74b57191d1 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -1807,8 +1807,14 @@ int reap_filters(FilterGraph *fg, int flush)
 
             if (ost->type == AVMEDIA_TYPE_VIDEO) {
                 AVRational fr = av_buffersink_get_frame_rate(filter);
-                if (fr.num > 0 && fr.den > 0)
+                if (fr.num > 0 && fr.den > 0) {
                     fd->frame_rate_filter = fr;
+
+                    if (!filtered_frame->duration) {
+                        filtered_frame->duration = av_rescale_q(1, av_inv_q(fr),
+                                                                filtered_frame->time_base);
+                    }
+                }
             }
 
             ret = enc_frame(ost, filtered_frame);
diff --git a/tests/ref/lavf/gxf b/tests/ref/lavf/gxf
index e8351fab86..d5fd40c8ab 100644
--- a/tests/ref/lavf/gxf
+++ b/tests/ref/lavf/gxf
@@ -1,3 +1,3 @@
 0638c4d073ac224608baaba16732b68f *tests/data/lavf/lavf.gxf
 795876 tests/data/lavf/lavf.gxf
-tests/data/lavf/lavf.gxf CRC=0x5ade0285
+tests/data/lavf/lavf.gxf CRC=0xe032707a
diff --git a/tests/ref/lavf/gxf_ntsc b/tests/ref/lavf/gxf_ntsc
index 60efd80462..d375420ee6 100644
--- a/tests/ref/lavf/gxf_ntsc
+++ b/tests/ref/lavf/gxf_ntsc
@@ -1,3 +1,3 @@
 9a27673c85f1671ba9ff7cd33e5735de *tests/data/lavf/lavf.gxf_ntsc
 794660 tests/data/lavf/lavf.gxf_ntsc
-tests/data/lavf/lavf.gxf_ntsc CRC=0xdcd39443
+tests/data/lavf/lavf.gxf_ntsc CRC=0xaf956c57
diff --git a/tests/ref/lavf/gxf_pal b/tests/ref/lavf/gxf_pal
index aefcd0ccab..83b0482431 100644
--- a/tests/ref/lavf/gxf_pal
+++ b/tests/ref/lavf/gxf_pal
@@ -1,3 +1,3 @@
 4d1bd16c6d52468c05711d8301e4e302 *tests/data/lavf/lavf.gxf_pal
 795880 tests/data/lavf/lavf.gxf_pal
-tests/data/lavf/lavf.gxf_pal CRC=0x1dbfef76
+tests/data/lavf/lavf.gxf_pal CRC=0x34fe5d7a
-- 
2.40.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [FFmpeg-devel] [PATCH 11/18] fftools/ffmpeg_enc: move handling video frame duration to video_sync_process()
  2023-08-26 15:11 [FFmpeg-devel] [PATCH 01/18] fftools/ffmpeg: stop explicitly closing output streams on input EOF Anton Khirnov
                   ` (8 preceding siblings ...)
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 10/18] fftools/ffmpeg: move derivation of frame duration from filter framerate Anton Khirnov
@ 2023-08-26 15:11 ` Anton Khirnov
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 12/18] fftools/ffmpeg_filter: factor processing a single frame out of reap_filters() Anton Khirnov
                   ` (6 subsequent siblings)
  16 siblings, 0 replies; 21+ messages in thread
From: Anton Khirnov @ 2023-08-26 15:11 UTC (permalink / raw)
  To: ffmpeg-devel

That is a more appropriate place for this.
---
 fftools/ffmpeg_enc.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index 0586ba17c4..590ce5d7be 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -968,12 +968,12 @@ early_exit:
  * should this (and possibly previous) frame be repeated in order to conform to
  * desired target framerate (if any).
  */
-static void video_sync_process(OutputFile *of, OutputStream *ost,
-                               AVFrame *frame, double duration,
+static void video_sync_process(OutputFile *of, OutputStream *ost, AVFrame *frame,
                                int64_t *nb_frames, int64_t *nb_frames_prev)
 {
     Encoder *e = ost->enc;
-    double delta0, delta, sync_ipts;
+    AVCodecContext *enc = ost->enc_ctx;
+    double delta0, delta, sync_ipts, duration;
 
     if (!frame) {
         *nb_frames_prev = *nb_frames = mid_pred(e->frames_prev_hist[0],
@@ -982,6 +982,8 @@ static void video_sync_process(OutputFile *of, OutputStream *ost,
         goto finish;
     }
 
+    duration = lrintf(frame->duration * av_q2d(frame->time_base) / av_q2d(enc->time_base));
+
     sync_ipts = adjust_frame_pts_to_encoder_tb(of, ost, frame);
     /* delta0 is the "drift" between the input frame and
      * where it would fall in the output. */
@@ -1110,12 +1112,8 @@ static int do_video_out(OutputFile *of, OutputStream *ost, AVFrame *frame)
     Encoder *e = ost->enc;
     AVCodecContext *enc = ost->enc_ctx;
     int64_t nb_frames, nb_frames_prev, i;
-    double duration = 0;
 
-    if (frame)
-        duration = lrintf(frame->duration * av_q2d(frame->time_base) / av_q2d(enc->time_base));
-
-    video_sync_process(of, ost, frame, duration,
+    video_sync_process(of, ost, frame,
                        &nb_frames, &nb_frames_prev);
 
     if (nb_frames_prev == 0 && ost->last_dropped) {
-- 
2.40.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [FFmpeg-devel] [PATCH 12/18] fftools/ffmpeg_filter: factor processing a single frame out of reap_filters()
  2023-08-26 15:11 [FFmpeg-devel] [PATCH 01/18] fftools/ffmpeg: stop explicitly closing output streams on input EOF Anton Khirnov
                   ` (9 preceding siblings ...)
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 11/18] fftools/ffmpeg_enc: move handling video frame duration to video_sync_process() Anton Khirnov
@ 2023-08-26 15:11 ` Anton Khirnov
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 13/18] fftools/ffmpeg_filter: reindent after previous commit Anton Khirnov
                   ` (5 subsequent siblings)
  16 siblings, 0 replies; 21+ messages in thread
From: Anton Khirnov @ 2023-08-26 15:11 UTC (permalink / raw)
  To: ffmpeg-devel

This is easier to read.
---
 fftools/ffmpeg_filter.c | 41 ++++++++++++++++++++++++-----------------
 1 file changed, 24 insertions(+), 17 deletions(-)

diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 74b57191d1..5bf2431fb9 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -1741,25 +1741,14 @@ int filtergraph_is_simple(const FilterGraph *fg)
     return fgp->is_simple;
 }
 
-int reap_filters(FilterGraph *fg, int flush)
+static int fg_output_step(OutputFilterPriv *ofp, int flush)
 {
-    FilterGraphPriv    *fgp = fgp_from_fg(fg);
+    FilterGraphPriv    *fgp = fgp_from_fg(ofp->ofilter.graph);
+    OutputStream       *ost = ofp->ofilter.ost;
     AVFrame *filtered_frame = fgp->frame;
-
-    if (!fg->graph)
-        return 0;
-
-    /* Reap all buffers present in the buffer sinks */
-    for (int i = 0; i < fg->nb_outputs; i++) {
-        OutputFilter   *ofilter = fg->outputs[i];
-        OutputStream       *ost = ofilter->ost;
-        OutputFilterPriv   *ofp = ofp_from_ofilter(ofilter);
         AVFilterContext *filter = ofp->filter;
-
-        int ret = 0;
-
-        while (1) {
             FrameData *fd;
+    int ret;
 
             ret = av_buffersink_get_frame_flags(filter, filtered_frame,
                                                AV_BUFFERSINK_FLAG_NO_REQUEST);
@@ -1774,11 +1763,11 @@ int reap_filters(FilterGraph *fg, int flush)
                         return ret;
                 }
 
-                break;
+                return 1;
             }
             if (ost->finished) {
                 av_frame_unref(filtered_frame);
-                continue;
+                return 0;
             }
 
             if (filtered_frame->pts != AV_NOPTS_VALUE) {
@@ -1823,6 +1812,24 @@ int reap_filters(FilterGraph *fg, int flush)
                 return ret;
 
             ofp->got_frame = 1;
+
+    return 0;
+}
+
+int reap_filters(FilterGraph *fg, int flush)
+{
+    if (!fg->graph)
+        return 0;
+
+    /* Reap all buffers present in the buffer sinks */
+    for (int i = 0; i < fg->nb_outputs; i++) {
+        OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[i]);
+        int ret = 0;
+
+        while (!ret) {
+            ret = fg_output_step(ofp, flush);
+            if (ret < 0)
+                return ret;
         }
     }
 
-- 
2.40.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [FFmpeg-devel] [PATCH 13/18] fftools/ffmpeg_filter: reindent after previous commit
  2023-08-26 15:11 [FFmpeg-devel] [PATCH 01/18] fftools/ffmpeg: stop explicitly closing output streams on input EOF Anton Khirnov
                   ` (10 preceding siblings ...)
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 12/18] fftools/ffmpeg_filter: factor processing a single frame out of reap_filters() Anton Khirnov
@ 2023-08-26 15:11 ` Anton Khirnov
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 14/18] fftools/ffmpeg_filter: shorten a variable name Anton Khirnov
                   ` (4 subsequent siblings)
  16 siblings, 0 replies; 21+ messages in thread
From: Anton Khirnov @ 2023-08-26 15:11 UTC (permalink / raw)
  To: ffmpeg-devel

---
 fftools/ffmpeg_filter.c | 122 ++++++++++++++++++++--------------------
 1 file changed, 61 insertions(+), 61 deletions(-)

diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 5bf2431fb9..1343e48fab 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -1746,72 +1746,72 @@ static int fg_output_step(OutputFilterPriv *ofp, int flush)
     FilterGraphPriv    *fgp = fgp_from_fg(ofp->ofilter.graph);
     OutputStream       *ost = ofp->ofilter.ost;
     AVFrame *filtered_frame = fgp->frame;
-        AVFilterContext *filter = ofp->filter;
-            FrameData *fd;
+    AVFilterContext *filter = ofp->filter;
+    FrameData *fd;
     int ret;
 
-            ret = av_buffersink_get_frame_flags(filter, filtered_frame,
-                                               AV_BUFFERSINK_FLAG_NO_REQUEST);
-            if (ret < 0) {
-                if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
-                    av_log(fgp, AV_LOG_WARNING,
-                           "Error in av_buffersink_get_frame_flags(): %s\n", av_err2str(ret));
-                } else if (flush && ret == AVERROR_EOF && ofp->got_frame &&
-                           av_buffersink_get_type(filter) == AVMEDIA_TYPE_VIDEO) {
-                    ret = enc_frame(ost, NULL);
-                    if (ret < 0)
-                        return ret;
-                }
-
-                return 1;
-            }
-            if (ost->finished) {
-                av_frame_unref(filtered_frame);
-                return 0;
-            }
-
-            if (filtered_frame->pts != AV_NOPTS_VALUE) {
-                AVRational tb = av_buffersink_get_time_base(filter);
-                ost->filter->last_pts = av_rescale_q(filtered_frame->pts, tb,
-                                                     AV_TIME_BASE_Q);
-                filtered_frame->time_base = tb;
-
-                if (debug_ts)
-                    av_log(fgp, AV_LOG_INFO, "filter_raw -> pts:%s pts_time:%s time_base:%d/%d\n",
-                           av_ts2str(filtered_frame->pts),
-                           av_ts2timestr(filtered_frame->pts, &tb),
-                           tb.num, tb.den);
-            }
-
-            fd = frame_data(filtered_frame);
-            if (!fd) {
-                av_frame_unref(filtered_frame);
-                return AVERROR(ENOMEM);
-            }
-
-            // only use bits_per_raw_sample passed through from the decoder
-            // if the filtergraph did not touch the frame data
-            if (!fgp->is_meta)
-                fd->bits_per_raw_sample = 0;
-
-            if (ost->type == AVMEDIA_TYPE_VIDEO) {
-                AVRational fr = av_buffersink_get_frame_rate(filter);
-                if (fr.num > 0 && fr.den > 0) {
-                    fd->frame_rate_filter = fr;
-
-                    if (!filtered_frame->duration) {
-                        filtered_frame->duration = av_rescale_q(1, av_inv_q(fr),
-                                                                filtered_frame->time_base);
-                    }
-                }
-            }
-
-            ret = enc_frame(ost, filtered_frame);
-            av_frame_unref(filtered_frame);
+    ret = av_buffersink_get_frame_flags(filter, filtered_frame,
+                                       AV_BUFFERSINK_FLAG_NO_REQUEST);
+    if (ret < 0) {
+        if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
+            av_log(fgp, AV_LOG_WARNING,
+                   "Error in av_buffersink_get_frame_flags(): %s\n", av_err2str(ret));
+        } else if (flush && ret == AVERROR_EOF && ofp->got_frame &&
+                   av_buffersink_get_type(filter) == AVMEDIA_TYPE_VIDEO) {
+            ret = enc_frame(ost, NULL);
             if (ret < 0)
                 return ret;
+        }
 
-            ofp->got_frame = 1;
+        return 1;
+    }
+    if (ost->finished) {
+        av_frame_unref(filtered_frame);
+        return 0;
+    }
+
+    if (filtered_frame->pts != AV_NOPTS_VALUE) {
+        AVRational tb = av_buffersink_get_time_base(filter);
+        ost->filter->last_pts = av_rescale_q(filtered_frame->pts, tb,
+                                             AV_TIME_BASE_Q);
+        filtered_frame->time_base = tb;
+
+        if (debug_ts)
+            av_log(fgp, AV_LOG_INFO, "filter_raw -> pts:%s pts_time:%s time_base:%d/%d\n",
+                   av_ts2str(filtered_frame->pts),
+                   av_ts2timestr(filtered_frame->pts, &tb),
+                   tb.num, tb.den);
+    }
+
+    fd = frame_data(filtered_frame);
+    if (!fd) {
+        av_frame_unref(filtered_frame);
+        return AVERROR(ENOMEM);
+    }
+
+    // only use bits_per_raw_sample passed through from the decoder
+    // if the filtergraph did not touch the frame data
+    if (!fgp->is_meta)
+        fd->bits_per_raw_sample = 0;
+
+    if (ost->type == AVMEDIA_TYPE_VIDEO) {
+        AVRational fr = av_buffersink_get_frame_rate(filter);
+        if (fr.num > 0 && fr.den > 0) {
+            fd->frame_rate_filter = fr;
+
+            if (!filtered_frame->duration) {
+                filtered_frame->duration = av_rescale_q(1, av_inv_q(fr),
+                                                        filtered_frame->time_base);
+            }
+        }
+    }
+
+    ret = enc_frame(ost, filtered_frame);
+    av_frame_unref(filtered_frame);
+    if (ret < 0)
+        return ret;
+
+    ofp->got_frame = 1;
 
     return 0;
 }
-- 
2.40.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [FFmpeg-devel] [PATCH 14/18] fftools/ffmpeg_filter: shorten a variable name
  2023-08-26 15:11 [FFmpeg-devel] [PATCH 01/18] fftools/ffmpeg: stop explicitly closing output streams on input EOF Anton Khirnov
                   ` (11 preceding siblings ...)
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 13/18] fftools/ffmpeg_filter: reindent after previous commit Anton Khirnov
@ 2023-08-26 15:11 ` Anton Khirnov
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 15/18] fftools/ffmpeg_enc: move remaining vsync-related code to video_sync_process() Anton Khirnov
                   ` (3 subsequent siblings)
  16 siblings, 0 replies; 21+ messages in thread
From: Anton Khirnov @ 2023-08-26 15:11 UTC (permalink / raw)
  To: ffmpeg-devel

---
 fftools/ffmpeg_filter.c | 32 ++++++++++++++------------------
 1 file changed, 14 insertions(+), 18 deletions(-)

diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 1343e48fab..e65e7cb64d 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -1745,13 +1745,13 @@ static int fg_output_step(OutputFilterPriv *ofp, int flush)
 {
     FilterGraphPriv    *fgp = fgp_from_fg(ofp->ofilter.graph);
     OutputStream       *ost = ofp->ofilter.ost;
-    AVFrame *filtered_frame = fgp->frame;
+    AVFrame          *frame = fgp->frame;
     AVFilterContext *filter = ofp->filter;
     FrameData *fd;
     int ret;
 
-    ret = av_buffersink_get_frame_flags(filter, filtered_frame,
-                                       AV_BUFFERSINK_FLAG_NO_REQUEST);
+    ret = av_buffersink_get_frame_flags(filter, frame,
+                                        AV_BUFFERSINK_FLAG_NO_REQUEST);
     if (ret < 0) {
         if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
             av_log(fgp, AV_LOG_WARNING,
@@ -1766,26 +1766,23 @@ static int fg_output_step(OutputFilterPriv *ofp, int flush)
         return 1;
     }
     if (ost->finished) {
-        av_frame_unref(filtered_frame);
+        av_frame_unref(frame);
         return 0;
     }
 
-    if (filtered_frame->pts != AV_NOPTS_VALUE) {
+    if (frame->pts != AV_NOPTS_VALUE) {
         AVRational tb = av_buffersink_get_time_base(filter);
-        ost->filter->last_pts = av_rescale_q(filtered_frame->pts, tb,
-                                             AV_TIME_BASE_Q);
-        filtered_frame->time_base = tb;
+        ost->filter->last_pts = av_rescale_q(frame->pts, tb, AV_TIME_BASE_Q);
+        frame->time_base = tb;
 
         if (debug_ts)
             av_log(fgp, AV_LOG_INFO, "filter_raw -> pts:%s pts_time:%s time_base:%d/%d\n",
-                   av_ts2str(filtered_frame->pts),
-                   av_ts2timestr(filtered_frame->pts, &tb),
-                   tb.num, tb.den);
+                   av_ts2str(frame->pts), av_ts2timestr(frame->pts, &tb), tb.num, tb.den);
     }
 
-    fd = frame_data(filtered_frame);
+    fd = frame_data(frame);
     if (!fd) {
-        av_frame_unref(filtered_frame);
+        av_frame_unref(frame);
         return AVERROR(ENOMEM);
     }
 
@@ -1799,15 +1796,14 @@ static int fg_output_step(OutputFilterPriv *ofp, int flush)
         if (fr.num > 0 && fr.den > 0) {
             fd->frame_rate_filter = fr;
 
-            if (!filtered_frame->duration) {
-                filtered_frame->duration = av_rescale_q(1, av_inv_q(fr),
-                                                        filtered_frame->time_base);
+            if (!frame->duration) {
+                frame->duration = av_rescale_q(1, av_inv_q(fr), frame->time_base);
             }
         }
     }
 
-    ret = enc_frame(ost, filtered_frame);
-    av_frame_unref(filtered_frame);
+    ret = enc_frame(ost, frame);
+    av_frame_unref(frame);
     if (ret < 0)
         return ret;
 
-- 
2.40.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [FFmpeg-devel] [PATCH 15/18] fftools/ffmpeg_enc: move remaining vsync-related code to video_sync_process()
  2023-08-26 15:11 [FFmpeg-devel] [PATCH 01/18] fftools/ffmpeg: stop explicitly closing output streams on input EOF Anton Khirnov
                   ` (12 preceding siblings ...)
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 14/18] fftools/ffmpeg_filter: shorten a variable name Anton Khirnov
@ 2023-08-26 15:11 ` Anton Khirnov
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 16/18] fftools/ffmpeg_enc: simplify adjust_frame_pts_to_encoder_tb() signature Anton Khirnov
                   ` (2 subsequent siblings)
  16 siblings, 0 replies; 21+ messages in thread
From: Anton Khirnov @ 2023-08-26 15:11 UTC (permalink / raw)
  To: ffmpeg-devel

---
 fftools/ffmpeg_enc.c | 46 +++++++++++++++++++++++---------------------
 1 file changed, 24 insertions(+), 22 deletions(-)

diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index 590ce5d7be..4f8c765256 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -1051,6 +1051,30 @@ finish:
             e->frames_prev_hist,
             sizeof(e->frames_prev_hist[0]) * (FF_ARRAY_ELEMS(e->frames_prev_hist) - 1));
     e->frames_prev_hist[0] = *nb_frames_prev;
+
+    if (*nb_frames_prev == 0 && ost->last_dropped) {
+        ost->nb_frames_drop++;
+        av_log(ost, AV_LOG_VERBOSE,
+               "*** dropping frame %"PRId64" at ts %"PRId64"\n",
+               e->vsync_frame_number, e->last_frame->pts);
+    }
+    if (*nb_frames > (*nb_frames_prev && ost->last_dropped) + (*nb_frames > *nb_frames_prev)) {
+        if (*nb_frames > dts_error_threshold * 30) {
+            av_log(ost, AV_LOG_ERROR, "%"PRId64" frame duplication too large, skipping\n", *nb_frames - 1);
+            ost->nb_frames_drop++;
+            *nb_frames = 0;
+            return;
+        }
+        ost->nb_frames_dup += *nb_frames - (*nb_frames_prev && ost->last_dropped) - (*nb_frames > *nb_frames_prev);
+        av_log(ost, AV_LOG_VERBOSE, "*** %"PRId64" dup!\n", *nb_frames - 1);
+        if (ost->nb_frames_dup > e->dup_warning) {
+            av_log(ost, AV_LOG_WARNING, "More than %"PRIu64" frames duplicated\n", e->dup_warning);
+            e->dup_warning *= 10;
+        }
+    }
+
+    ost->last_dropped = *nb_frames == *nb_frames_prev && frame;
+    ost->kf.dropped_keyframe = ost->last_dropped && frame && (frame->flags & AV_FRAME_FLAG_KEY);
 }
 
 static enum AVPictureType forced_kf_apply(void *logctx, KeyframeForceCtx *kf,
@@ -1116,28 +1140,6 @@ static int do_video_out(OutputFile *of, OutputStream *ost, AVFrame *frame)
     video_sync_process(of, ost, frame,
                        &nb_frames, &nb_frames_prev);
 
-    if (nb_frames_prev == 0 && ost->last_dropped) {
-        ost->nb_frames_drop++;
-        av_log(ost, AV_LOG_VERBOSE,
-               "*** dropping frame %"PRId64" at ts %"PRId64"\n",
-               e->vsync_frame_number, e->last_frame->pts);
-    }
-    if (nb_frames > (nb_frames_prev && ost->last_dropped) + (nb_frames > nb_frames_prev)) {
-        if (nb_frames > dts_error_threshold * 30) {
-            av_log(ost, AV_LOG_ERROR, "%"PRId64" frame duplication too large, skipping\n", nb_frames - 1);
-            ost->nb_frames_drop++;
-            return 0;
-        }
-        ost->nb_frames_dup += nb_frames - (nb_frames_prev && ost->last_dropped) - (nb_frames > nb_frames_prev);
-        av_log(ost, AV_LOG_VERBOSE, "*** %"PRId64" dup!\n", nb_frames - 1);
-        if (ost->nb_frames_dup > e->dup_warning) {
-            av_log(ost, AV_LOG_WARNING, "More than %"PRIu64" frames duplicated\n", e->dup_warning);
-            e->dup_warning *= 10;
-        }
-    }
-    ost->last_dropped = nb_frames == nb_frames_prev && frame;
-    ost->kf.dropped_keyframe = ost->last_dropped && frame && (frame->flags & AV_FRAME_FLAG_KEY);
-
     /* duplicates frame if needed */
     for (i = 0; i < nb_frames; i++) {
         AVFrame *in_picture;
-- 
2.40.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [FFmpeg-devel] [PATCH 16/18] fftools/ffmpeg_enc: simplify adjust_frame_pts_to_encoder_tb() signature
  2023-08-26 15:11 [FFmpeg-devel] [PATCH 01/18] fftools/ffmpeg: stop explicitly closing output streams on input EOF Anton Khirnov
                   ` (13 preceding siblings ...)
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 15/18] fftools/ffmpeg_enc: move remaining vsync-related code to video_sync_process() Anton Khirnov
@ 2023-08-26 15:11 ` Anton Khirnov
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 17/18] fftools/ffmpeg_mux: rename of_close() to of_free() Anton Khirnov
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 18/18] fftools/ffmpeg: move sending filtergraph commands to a separate function Anton Khirnov
  16 siblings, 0 replies; 21+ messages in thread
From: Anton Khirnov @ 2023-08-26 15:11 UTC (permalink / raw)
  To: ffmpeg-devel

It does not need an OutputFile and an OutputStream, only the target
timebase and the timestamp offset.
---
 fftools/ffmpeg_enc.c | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)

diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index 4f8c765256..efa940fba9 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -920,16 +920,12 @@ static int do_audio_out(OutputFile *of, OutputStream *ost,
     return (ret < 0 && ret != AVERROR_EOF) ? ret : 0;
 }
 
-static double adjust_frame_pts_to_encoder_tb(OutputFile *of, OutputStream *ost,
-                                             AVFrame *frame)
+static double adjust_frame_pts_to_encoder_tb(AVFrame *frame, AVRational tb_dst,
+                                             int64_t start_time)
 {
     double float_pts = AV_NOPTS_VALUE; // this is identical to frame.pts but with higher precision
-    const int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ?
-                               0 : of->start_time;
 
-    AVCodecContext *const enc = ost->enc_ctx;
-
-    AVRational        tb = enc->time_base;
+    AVRational        tb = tb_dst;
     AVRational filter_tb = frame->time_base;
     const int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16);
 
@@ -946,19 +942,17 @@ static double adjust_frame_pts_to_encoder_tb(OutputFile *of, OutputStream *ost,
     if (float_pts != llrint(float_pts))
         float_pts += FFSIGN(float_pts) * 1.0 / (1<<17);
 
-    frame->pts = av_rescale_q(frame->pts, filter_tb, enc->time_base) -
-                 av_rescale_q(start_time, AV_TIME_BASE_Q, enc->time_base);
-    frame->time_base = enc->time_base;
+    frame->pts = av_rescale_q(frame->pts, filter_tb, tb_dst) -
+                 av_rescale_q(start_time, AV_TIME_BASE_Q, tb_dst);
+    frame->time_base = tb_dst;
 
 early_exit:
 
     if (debug_ts) {
         av_log(NULL, AV_LOG_INFO, "filter -> pts:%s pts_time:%s exact:%f time_base:%d/%d\n",
                frame ? av_ts2str(frame->pts) : "NULL",
-               (enc && frame) ? av_ts2timestr(frame->pts, &enc->time_base) : "NULL",
-               float_pts,
-               enc ? enc->time_base.num : -1,
-               enc ? enc->time_base.den : -1);
+               av_ts2timestr(frame->pts, &tb_dst),
+               float_pts, tb_dst.num, tb_dst.den);
     }
 
     return float_pts;
@@ -984,7 +978,8 @@ static void video_sync_process(OutputFile *of, OutputStream *ost, AVFrame *frame
 
     duration = lrintf(frame->duration * av_q2d(frame->time_base) / av_q2d(enc->time_base));
 
-    sync_ipts = adjust_frame_pts_to_encoder_tb(of, ost, frame);
+    sync_ipts = adjust_frame_pts_to_encoder_tb(frame, enc->time_base,
+                                               of->start_time == AV_NOPTS_VALUE ? 0 : of->start_time);
     /* delta0 is the "drift" between the input frame and
      * where it would fall in the output. */
     delta0 = sync_ipts - e->next_pts;
-- 
2.40.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [FFmpeg-devel] [PATCH 17/18] fftools/ffmpeg_mux: rename of_close() to of_free()
  2023-08-26 15:11 [FFmpeg-devel] [PATCH 01/18] fftools/ffmpeg: stop explicitly closing output streams on input EOF Anton Khirnov
                   ` (14 preceding siblings ...)
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 16/18] fftools/ffmpeg_enc: simplify adjust_frame_pts_to_encoder_tb() signature Anton Khirnov
@ 2023-08-26 15:11 ` Anton Khirnov
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 18/18] fftools/ffmpeg: move sending filtergraph commands to a separate function Anton Khirnov
  16 siblings, 0 replies; 21+ messages in thread
From: Anton Khirnov @ 2023-08-26 15:11 UTC (permalink / raw)
  To: ffmpeg-devel

This function is primarily a destructor for OutputFile, the underlying
AVIOContext is normally closed earlier (right after writing the
trailer).
---
 fftools/ffmpeg.c     | 3 +--
 fftools/ffmpeg.h     | 2 +-
 fftools/ffmpeg_mux.c | 2 +-
 3 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 8b0f31aac6..45844b489f 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -362,9 +362,8 @@ static void ffmpeg_cleanup(int ret)
         fg_free(&filtergraphs[i]);
     av_freep(&filtergraphs);
 
-    /* close files */
     for (i = 0; i < nb_output_files; i++)
-        of_close(&output_files[i]);
+        of_free(&output_files[i]);
 
     for (i = 0; i < nb_input_files; i++)
         ifile_close(&input_files[i]);
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index ef5bb13908..9c85df5bdf 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -821,7 +821,7 @@ int enc_flush(void);
 int of_stream_init(OutputFile *of, OutputStream *ost);
 int of_write_trailer(OutputFile *of);
 int of_open(const OptionsContext *o, const char *filename);
-void of_close(OutputFile **pof);
+void of_free(OutputFile **pof);
 
 void of_enc_stats_close(void);
 
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 076c6afabd..3d6309c3c0 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -908,7 +908,7 @@ static void fc_close(AVFormatContext **pfc)
     *pfc = NULL;
 }
 
-void of_close(OutputFile **pof)
+void of_free(OutputFile **pof)
 {
     OutputFile *of = *pof;
     Muxer *mux;
-- 
2.40.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [FFmpeg-devel] [PATCH 18/18] fftools/ffmpeg: move sending filtergraph commands to a separate function
  2023-08-26 15:11 [FFmpeg-devel] [PATCH 01/18] fftools/ffmpeg: stop explicitly closing output streams on input EOF Anton Khirnov
                   ` (15 preceding siblings ...)
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 17/18] fftools/ffmpeg_mux: rename of_close() to of_free() Anton Khirnov
@ 2023-08-26 15:11 ` Anton Khirnov
  16 siblings, 0 replies; 21+ messages in thread
From: Anton Khirnov @ 2023-08-26 15:11 UTC (permalink / raw)
  To: ffmpeg-devel

Stop accessing filtergraph internals from keyboard reading code.
---
 fftools/ffmpeg.c        | 26 ++++----------------------
 fftools/ffmpeg.h        |  3 +++
 fftools/ffmpeg_filter.c | 24 ++++++++++++++++++++++++
 3 files changed, 31 insertions(+), 22 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 45844b489f..f1c5f40caf 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -97,10 +97,6 @@
 
 #include "libswresample/swresample.h"
 
-#include "libavfilter/avfilter.h"
-#include "libavfilter/buffersrc.h"
-#include "libavfilter/buffersink.h"
-
 #include "cmdutils.h"
 #include "ffmpeg.h"
 #include "sync_queue.h"
@@ -977,7 +973,7 @@ static void set_tty_echo(int on)
 
 static int check_keyboard_interaction(int64_t cur_time)
 {
-    int i, ret, key;
+    int i, key;
     static int64_t last_time;
     if (received_nb_signals)
         return AVERROR_EXIT;
@@ -1010,23 +1006,9 @@ static int check_keyboard_interaction(int64_t cur_time)
             (n = sscanf(buf, "%63[^ ] %lf %255[^ ] %255[^\n]", target, &time, command, arg)) >= 3) {
             av_log(NULL, AV_LOG_DEBUG, "Processing command target:%s time:%f command:%s arg:%s",
                    target, time, command, arg);
-            for (i = 0; i < nb_filtergraphs; i++) {
-                FilterGraph *fg = filtergraphs[i];
-                if (fg->graph) {
-                    if (time < 0) {
-                        ret = avfilter_graph_send_command(fg->graph, target, command, arg, buf, sizeof(buf),
-                                                          key == 'c' ? AVFILTER_CMD_FLAG_ONE : 0);
-                        fprintf(stderr, "Command reply for stream %d: ret:%d res:\n%s", i, ret, buf);
-                    } else if (key == 'c') {
-                        fprintf(stderr, "Queuing commands only on filters supporting the specific command is unsupported\n");
-                        ret = AVERROR_PATCHWELCOME;
-                    } else {
-                        ret = avfilter_graph_queue_command(fg->graph, target, command, arg, 0, time);
-                        if (ret < 0)
-                            fprintf(stderr, "Queuing command failed with error %s\n", av_err2str(ret));
-                    }
-                }
-            }
+            for (i = 0; i < nb_filtergraphs; i++)
+                fg_send_command(filtergraphs[i], time, target, command, arg,
+                                key == 'C');
         } else {
             av_log(NULL, AV_LOG_ERROR,
                    "Parse error, at least 3 arguments were expected, "
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 9c85df5bdf..b146f1f2dc 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -759,6 +759,9 @@ void fg_free(FilterGraph **pfg);
  */
 int fg_transcode_step(FilterGraph *graph, InputStream **best_ist);
 
+void fg_send_command(FilterGraph *fg, double time, const char *target,
+                     const char *command, const char *arg, int all_filters);
+
 /**
  * Get and encode new output from specified filtergraph, without causing
  * activity.
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index e65e7cb64d..4ba20342a9 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -1741,6 +1741,30 @@ int filtergraph_is_simple(const FilterGraph *fg)
     return fgp->is_simple;
 }
 
+void fg_send_command(FilterGraph *fg, double time, const char *target,
+                     const char *command, const char *arg, int all_filters)
+{
+    int ret;
+
+    if (!fg->graph)
+        return;
+
+    if (time < 0) {
+        char response[4096];
+        ret = avfilter_graph_send_command(fg->graph, target, command, arg,
+                                          response, sizeof(response),
+                                          all_filters ? 0 : AVFILTER_CMD_FLAG_ONE);
+        fprintf(stderr, "Command reply for stream %d: ret:%d res:\n%s",
+                fg->index, ret, response);
+    } else if (!all_filters) {
+        fprintf(stderr, "Queuing commands only on filters supporting the specific command is unsupported\n");
+    } else {
+        ret = avfilter_graph_queue_command(fg->graph, target, command, arg, 0, time);
+        if (ret < 0)
+            fprintf(stderr, "Queuing command failed with error %s\n", av_err2str(ret));
+    }
+}
+
 static int fg_output_step(OutputFilterPriv *ofp, int flush)
 {
     FilterGraphPriv    *fgp = fgp_from_fg(ofp->ofilter.graph);
-- 
2.40.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [FFmpeg-devel] [PATCH 04/18] fftools/ffmpeg_mux: stop rescaling timestamps in of_streamcopy()
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 04/18] fftools/ffmpeg_mux: stop rescaling timestamps in of_streamcopy() Anton Khirnov
@ 2023-08-26 20:20   ` Michael Niedermayer
  2023-08-27 10:50     ` [FFmpeg-devel] [PATCH 04/18 v2] " Anton Khirnov
  0 siblings, 1 reply; 21+ messages in thread
From: Michael Niedermayer @ 2023-08-26 20:20 UTC (permalink / raw)
  To: FFmpeg development discussions and patches


[-- Attachment #1.1: Type: text/plain, Size: 2504 bytes --]

On Sat, Aug 26, 2023 at 05:11:30PM +0200, Anton Khirnov wrote:
> This function converts packet timestamps from the input stream timebase
> to OutputStream.mux_timebase, which may or may not be equal to the
> actual output AVStream timebase (and even when it is, this may not
> always be the optimal choice due to bitstream filtering).
> 
> Just keep the timestamps in input stream timebase, they will be rescaled
> as needed before bitstream filtering and/or sending the packet to the
> muxer.
> 
> Drop now-unused OutputStream.mux_timebase.
> ---
>  fftools/ffmpeg.h          |  2 --
>  fftools/ffmpeg_enc.c      |  2 --
>  fftools/ffmpeg_mux.c      | 21 +++++++--------------
>  fftools/ffmpeg_mux_init.c |  2 --
>  4 files changed, 7 insertions(+), 20 deletions(-)

./ffmpeg -i in.flv  -t 0.2 -bitexact -acodec copy -y /tmp/copy.mov  (this ffmpeg version matters)

./ffmpeg -i /tmp/copy.mov -bitexact -f framecrc -                   (this ffmpeg version doesnt matter)

0,          0,          0,     1152,     4608, 0xa25d01fe
0,       1152,       1152,     1152,     4608, 0x3b9ff171
0,       2304,       2304,     1152,     4608, 0x10ad7d49
0,       3456,       3456,     1152,     4608, 0xadae4ea6
0,       4608,       4608,     1152,     4608, 0xaba106e7
0,       5760,       5760,     1152,     4608, 0x1beee538
0,       6912,       6912,     1152,     4608, 0x7dbe78ef
0,       8064,       8064,     1152,     4608, 0xaf41b247

after the commit:
0,          0,          0,     1152,     4608, 0xa25d01fe
0,       1147,       1147,     1152,     4608, 0x3b9ff171
0,       2293,       2293,     1152,     4608, 0x10ad7d49
0,       3440,       3440,     1152,     4608, 0xadae4ea6
0,       4586,       4586,     1152,     4608, 0xaba106e7
0,       5777,       5777,     1152,     4608, 0x1beee538
0,       6924,       6924,     1152,     4608, 0x7dbe78ef
0,       8070,       8070,     1152,     4608, 0xaf41b247

Here the timestamps stored in a newly muxed file seems bad
could be missing something but that seems bad, even if the
input is only 1ms precisse we need to generate valid timestamps
if we remux

I suspect every mp3 in flv will show this effect but if not i can share in.flv

thx

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

The bravest are surely those who have the clearest vision
of what is before them, glory and danger alike, and yet
notwithstanding go out to meet it. -- Thucydides

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

[-- Attachment #2: Type: text/plain, Size: 251 bytes --]

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [FFmpeg-devel] [PATCH 04/18 v2] fftools/ffmpeg_mux: stop rescaling timestamps in of_streamcopy()
  2023-08-26 20:20   ` Michael Niedermayer
@ 2023-08-27 10:50     ` Anton Khirnov
  0 siblings, 0 replies; 21+ messages in thread
From: Anton Khirnov @ 2023-08-27 10:50 UTC (permalink / raw)
  To: ffmpeg-devel

This function converts packet timestamps from the input stream timebase
to OutputStream.mux_timebase, which may or may not be equal to the
actual output AVStream timebase (and even when it is, this may not
always be the optimal choice due to bitstream filtering).

Just keep the timestamps in input stream timebase, they will be rescaled
as needed before bitstream filtering and/or sending the packet to the
muxer.

Move the av_rescale_delta() call for audio (needed to preserve accuracy
with coarse muxer timebases) to write_packet.

Drop now-unused OutputStream.mux_timebase.
---
 fftools/ffmpeg.h          |  2 --
 fftools/ffmpeg_enc.c      |  2 --
 fftools/ffmpeg_mux.c      | 44 ++++++++++++++++++++-------------------
 fftools/ffmpeg_mux.h      |  2 +-
 fftools/ffmpeg_mux_init.c |  2 --
 5 files changed, 24 insertions(+), 28 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index d53181e427..ef5bb13908 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -523,8 +523,6 @@ typedef struct OutputStream {
     /* dts of the last packet sent to the muxing queue, in AV_TIME_BASE_Q */
     int64_t last_mux_dts;
 
-    // the timebase of the packets sent to the muxer
-    AVRational mux_timebase;
     AVRational enc_timebase;
 
     Encoder *enc;
diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index 96424272bf..3c9fdd3237 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -463,8 +463,6 @@ int enc_open(OutputStream *ost, AVFrame *frame)
     if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0)
         ost->st->time_base = av_add_q(ost->enc_ctx->time_base, (AVRational){0, 1});
 
-    ost->mux_timebase = enc_ctx->time_base;
-
     ret = of_stream_init(of, ost);
     if (ret < 0)
         return ret;
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index ab9bb398c1..54410aac5c 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -76,7 +76,23 @@ static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
     if (ost->type == AVMEDIA_TYPE_VIDEO && ost->vsync_method == VSYNC_DROP)
         pkt->pts = pkt->dts = AV_NOPTS_VALUE;
 
-    av_packet_rescale_ts(pkt, pkt->time_base, ost->st->time_base);
+    // rescale timestamps to the stream timebase
+    if (ost->type == AVMEDIA_TYPE_AUDIO && !ost->enc) {
+        // use av_rescale_delta() for streamcopying audio, to preserve
+        // accuracy with coarse input timebases
+        int duration = av_get_audio_frame_duration2(ost->st->codecpar, pkt->size);
+
+        if (!duration)
+            duration = ost->st->codecpar->frame_size;
+
+        pkt->dts = av_rescale_delta(pkt->time_base, pkt->dts,
+                                    (AVRational){1, ost->st->codecpar->sample_rate}, duration,
+                                    &ms->ts_rescale_delta_last, ost->st->time_base);
+        pkt->pts = pkt->dts;
+
+        pkt->duration = av_rescale_q(pkt->duration, pkt->time_base, ost->st->time_base);
+    } else
+        av_packet_rescale_ts(pkt, pkt->time_base, ost->st->time_base);
     pkt->time_base = ost->st->time_base;
 
     if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
@@ -392,7 +408,7 @@ int of_streamcopy(OutputStream *ost, const AVPacket *pkt, int64_t dts)
     OutputFile *of = output_files[ost->file_index];
     MuxStream  *ms = ms_from_ost(ost);
     int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
-    int64_t ost_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ost->mux_timebase);
+    int64_t ts_offset;
     AVPacket *opkt = ms->pkt;
     int ret;
 
@@ -425,27 +441,17 @@ int of_streamcopy(OutputStream *ost, const AVPacket *pkt, int64_t dts)
     if (ret < 0)
         return ret;
 
-    opkt->time_base = ost->mux_timebase;
+    ts_offset = av_rescale_q(start_time, AV_TIME_BASE_Q, opkt->time_base);
 
     if (pkt->pts != AV_NOPTS_VALUE)
-        opkt->pts = av_rescale_q(pkt->pts, pkt->time_base, opkt->time_base) - ost_tb_start_time;
+        opkt->pts -= ts_offset;
 
     if (pkt->dts == AV_NOPTS_VALUE) {
         opkt->dts = av_rescale_q(dts, AV_TIME_BASE_Q, opkt->time_base);
     } else if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
-        int duration = av_get_audio_frame_duration2(ost->par_in, pkt->size);
-        if(!duration)
-            duration = ost->par_in->frame_size;
-        opkt->dts = av_rescale_delta(pkt->time_base, pkt->dts,
-                                    (AVRational){1, ost->par_in->sample_rate}, duration,
-                                    &ms->ts_rescale_delta_last, opkt->time_base);
-        /* dts will be set immediately afterwards to what pts is now */
-        opkt->pts = opkt->dts - ost_tb_start_time;
-    } else
-        opkt->dts = av_rescale_q(pkt->dts, pkt->time_base, opkt->time_base);
-    opkt->dts -= ost_tb_start_time;
-
-    opkt->duration = av_rescale_q(pkt->duration, pkt->time_base, opkt->time_base);
+        opkt->pts = opkt->dts - ts_offset;
+    }
+    opkt->dts -= ts_offset;
 
     {
         int ret = trigger_fix_sub_duration_heartbeat(ost, pkt);
@@ -511,10 +517,6 @@ static int thread_start(Muxer *mux)
         MuxStream     *ms = ms_from_ost(ost);
         AVPacket *pkt;
 
-        /* try to improve muxing time_base (only possible if nothing has been written yet) */
-        if (!av_fifo_can_read(ms->muxing_queue))
-            ost->mux_timebase = ost->st->time_base;
-
         while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0) {
             ret = thread_submit_packet(mux, ost, pkt);
             if (pkt) {
diff --git a/fftools/ffmpeg_mux.h b/fftools/ffmpeg_mux.h
index 7f34b86548..a2bb4dfc7d 100644
--- a/fftools/ffmpeg_mux.h
+++ b/fftools/ffmpeg_mux.h
@@ -75,7 +75,7 @@ typedef struct MuxStream {
     int64_t    stream_duration;
     AVRational stream_duration_tb;
 
-    // audio streamcopy - state for av_rescale_delta()
+    // state for av_rescale_delta() call for audio in write_packet()
     int64_t ts_rescale_delta_last;
 
     // combined size of all the packets sent to the muxer
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index 0289cdabad..cf4cd2d5b7 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -1090,8 +1090,6 @@ static int streamcopy_init(const Muxer *mux, OutputStream *ost)
         }
     }
 
-    ost->mux_timebase = ist->st->time_base;
-
 fail:
     avcodec_free_context(&codec_ctx);
     av_dict_free(&codec_opts);
-- 
2.40.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [FFmpeg-devel] [PATCH 10/18] fftools/ffmpeg: move derivation of frame duration from filter framerate
  2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 10/18] fftools/ffmpeg: move derivation of frame duration from filter framerate Anton Khirnov
@ 2023-08-29  8:07   ` Anton Khirnov
  0 siblings, 0 replies; 21+ messages in thread
From: Anton Khirnov @ 2023-08-29  8:07 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

I'm dropping this patch for now due to it causing issues in some cases.
I'll probably resend a version of it later.

-- 
Anton Khirnov
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2023-08-29  8:07 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-26 15:11 [FFmpeg-devel] [PATCH 01/18] fftools/ffmpeg: stop explicitly closing output streams on input EOF Anton Khirnov
2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 02/18] fftools/ffmpeg: simplify handling input -t for streamcopy Anton Khirnov
2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 03/18] fftools/ffmpeg_mux: use correct timebases for bitstream filtering Anton Khirnov
2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 04/18] fftools/ffmpeg_mux: stop rescaling timestamps in of_streamcopy() Anton Khirnov
2023-08-26 20:20   ` Michael Niedermayer
2023-08-27 10:50     ` [FFmpeg-devel] [PATCH 04/18 v2] " Anton Khirnov
2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 05/18] fftools/ffmpeg_enc: factor out setting encoder timebase Anton Khirnov
2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 06/18] fftools/ffmpeg_enc: reindent after previous commit Anton Khirnov
2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 07/18] fftools/ffmpeg_enc: only use fallback framerate when encoding CFR Anton Khirnov
2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 08/18] fftools/ffmpeg_enc: reindent after previous commit Anton Khirnov
2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 09/18] fftools/ffmpeg_filter: sanitize framerate retrieved from the filtergraph Anton Khirnov
2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 10/18] fftools/ffmpeg: move derivation of frame duration from filter framerate Anton Khirnov
2023-08-29  8:07   ` Anton Khirnov
2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 11/18] fftools/ffmpeg_enc: move handling video frame duration to video_sync_process() Anton Khirnov
2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 12/18] fftools/ffmpeg_filter: factor processing a single frame out of reap_filters() Anton Khirnov
2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 13/18] fftools/ffmpeg_filter: reindent after previous commit Anton Khirnov
2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 14/18] fftools/ffmpeg_filter: shorten a variable name Anton Khirnov
2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 15/18] fftools/ffmpeg_enc: move remaining vsync-related code to video_sync_process() Anton Khirnov
2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 16/18] fftools/ffmpeg_enc: simplify adjust_frame_pts_to_encoder_tb() signature Anton Khirnov
2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 17/18] fftools/ffmpeg_mux: rename of_close() to of_free() Anton Khirnov
2023-08-26 15:11 ` [FFmpeg-devel] [PATCH 18/18] fftools/ffmpeg: move sending filtergraph commands to a separate function Anton Khirnov

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git