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/23] fftools/ffmpeg: drop InputStream.processing_needed
@ 2023-03-25 19:15 Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 02/23] fftools/ffmpeg: move initializing next_[pd]ts to add_input_streams() Anton Khirnov
                   ` (21 more replies)
  0 siblings, 22 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

It is equivalent to !InputStream.discard.
---
 fftools/ffmpeg.c          | 4 ++--
 fftools/ffmpeg.h          | 1 -
 fftools/ffmpeg_filter.c   | 1 -
 fftools/ffmpeg_mux_init.c | 3 ---
 4 files changed, 2 insertions(+), 7 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 438bee8fef..aa9284ecd5 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -3648,7 +3648,7 @@ static void decode_flush(InputFile *ifile)
         InputStream *ist = ifile->streams[i];
         int ret;
 
-        if (!ist->processing_needed)
+        if (ist->discard)
             continue;
 
         do {
@@ -3793,7 +3793,7 @@ static int process_input(int file_index)
 
         for (i = 0; i < ifile->nb_streams; i++) {
             ist = ifile->streams[i];
-            if (ist->processing_needed) {
+            if (!ist->discard) {
                 ret = process_input_packet(ist, NULL, 0);
                 if (ret>0)
                     return 0;
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 823218e214..791deedc07 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -342,7 +342,6 @@ typedef struct InputStream {
     int decoding_needed;     /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
 #define DECODING_FOR_OST    1
 #define DECODING_FOR_FILTER 2
-    int processing_needed;   /* non zero if the packets must be processed */
     // should attach FrameData as opaque_ref after decoding
     int want_frame_data;
 
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 4fde120067..314b89b585 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -296,7 +296,6 @@ static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
 
     ist->discard         = 0;
     ist->decoding_needed |= DECODING_FOR_FILTER;
-    ist->processing_needed = 1;
     ist->st->discard = AVDISCARD_NONE;
 
     ifilter = ALLOC_ARRAY_ELEM(fg->inputs, fg->nb_inputs);
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index 09d24ba8e5..ebc17059f9 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -2283,7 +2283,6 @@ int of_open(const OptionsContext *o, const char *filename)
         if (ost->enc_ctx && ost->ist) {
             InputStream *ist = ost->ist;
             ist->decoding_needed |= DECODING_FOR_OST;
-            ist->processing_needed = 1;
 
             if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
                 ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
@@ -2294,8 +2293,6 @@ int of_open(const OptionsContext *o, const char *filename)
                     exit_program(1);
                 }
             }
-        } else if (ost->ist) {
-            ost->ist->processing_needed = 1;
         }
 
         /* set the filter output constraints */
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 02/23] fftools/ffmpeg: move initializing next_[pd]ts to add_input_streams()
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 03/23] fftools/sync_queue: use timebase from input frames/packets Anton Khirnov
                   ` (20 subsequent siblings)
  21 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

They are initialized to constants, so it makes most sense to do it as
soon as possible.
---
 fftools/ffmpeg.c       | 3 ---
 fftools/ffmpeg_demux.c | 3 +++
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index aa9284ecd5..abda713ddb 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -2848,9 +2848,6 @@ static int init_input_stream(InputStream *ist, char *error, int error_len)
         assert_avoptions(ist->decoder_opts);
     }
 
-    ist->next_pts = AV_NOPTS_VALUE;
-    ist->next_dts = AV_NOPTS_VALUE;
-
     return 0;
 }
 
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index db05ddb8e9..354d3165c9 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -675,6 +675,9 @@ static void add_input_streams(const OptionsContext *o, Demuxer *d)
         st->discard  = AVDISCARD_ALL;
         ist->nb_samples = 0;
         ist->first_dts = AV_NOPTS_VALUE;
+        ist->next_pts  = AV_NOPTS_VALUE;
+        ist->next_dts  = AV_NOPTS_VALUE;
+
         ds->min_pts = INT64_MAX;
         ds->max_pts = INT64_MIN;
 
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 03/23] fftools/sync_queue: use timebase from input frames/packets
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 02/23] fftools/ffmpeg: move initializing next_[pd]ts to add_input_streams() Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 04/23] fftools/sync_queue: document overall design Anton Khirnov
                   ` (19 subsequent siblings)
  21 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

They are always properly set now. Avoid a separate timebase-setting
call, which duplicates the knowledge of the timebase being used.
---
 fftools/ffmpeg.c     |  3 ---
 fftools/ffmpeg_mux.c |  3 ---
 fftools/sync_queue.c | 39 ++++++++++++++++++++++-----------------
 fftools/sync_queue.h |  6 ------
 4 files changed, 22 insertions(+), 29 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index abda713ddb..3a205a3b01 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -3194,9 +3194,6 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame)
     if (ost->bitexact)
         enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
 
-    if (ost->sq_idx_encode >= 0)
-        sq_set_tb(of->sq_encode, ost->sq_idx_encode, enc_ctx->time_base);
-
     ost->mux_timebase = enc_ctx->time_base;
 
     return 0;
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index cf58051949..1937bc2aa7 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -586,9 +586,6 @@ int of_stream_init(OutputFile *of, OutputStream *ost)
     MuxStream *ms = ms_from_ost(ost);
     int ret;
 
-    if (ost->sq_idx_mux >= 0)
-        sq_set_tb(mux->sq_mux, ost->sq_idx_mux, ost->mux_timebase);
-
     /* initialize bitstream filters for the output stream
      * needs to be done here, because the codec id for streamcopy is not
      * known until now */
diff --git a/fftools/sync_queue.c b/fftools/sync_queue.c
index c2b23ee4f5..7c348af300 100644
--- a/fftools/sync_queue.c
+++ b/fftools/sync_queue.c
@@ -84,6 +84,26 @@ static int frame_null(const SyncQueue *sq, SyncQueueFrame frame)
     return (sq->type == SYNC_QUEUE_PACKETS) ? (frame.p == NULL) : (frame.f == NULL);
 }
 
+static void tb_update(const SyncQueue *sq, SyncQueueStream *st,
+                      const SyncQueueFrame frame)
+{
+    AVRational tb = (sq->type == SYNC_QUEUE_PACKETS) ?
+                    frame.p->time_base : frame.f->time_base;
+
+    av_assert0(tb.num > 0 && tb.den > 0);
+
+    if (tb.num == st->tb.num && tb.den == st->tb.den)
+        return;
+
+    // timebase should not change after the first frame
+    av_assert0(!av_fifo_can_read(st->fifo));
+
+    if (st->head_ts != AV_NOPTS_VALUE)
+        st->head_ts = av_rescale_q(st->head_ts, st->tb, tb);
+
+    st->tb = tb;
+}
+
 static void finish_stream(SyncQueue *sq, unsigned int stream_idx)
 {
     SyncQueueStream *st = &sq->streams[stream_idx];
@@ -241,8 +261,6 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame)
     av_assert0(stream_idx < sq->nb_streams);
     st = &sq->streams[stream_idx];
 
-    av_assert0(st->tb.num > 0 && st->tb.den > 0);
-
     if (frame_null(sq, frame)) {
         finish_stream(sq, stream_idx);
         return 0;
@@ -250,6 +268,8 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame)
     if (st->finished)
         return AVERROR_EOF;
 
+    tb_update(sq, st, frame);
+
     ret = objpool_get(sq->pool, (void**)&dst);
     if (ret < 0)
         return ret;
@@ -375,21 +395,6 @@ int sq_add_stream(SyncQueue *sq, int limiting)
     return sq->nb_streams++;
 }
 
-void sq_set_tb(SyncQueue *sq, unsigned int stream_idx, AVRational tb)
-{
-    SyncQueueStream *st;
-
-    av_assert0(stream_idx < sq->nb_streams);
-    st = &sq->streams[stream_idx];
-
-    av_assert0(!av_fifo_can_read(st->fifo));
-
-    if (st->head_ts != AV_NOPTS_VALUE)
-        st->head_ts = av_rescale_q(st->head_ts, st->tb, tb);
-
-    st->tb = tb;
-}
-
 void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx, uint64_t frames)
 {
     SyncQueueStream *st;
diff --git a/fftools/sync_queue.h b/fftools/sync_queue.h
index 3f823ff0d9..17d282c38c 100644
--- a/fftools/sync_queue.h
+++ b/fftools/sync_queue.h
@@ -59,12 +59,6 @@ void       sq_free(SyncQueue **sq);
  */
 int sq_add_stream(SyncQueue *sq, int limiting);
 
-/**
- * Set the timebase for the stream with index stream_idx. Should be called
- * before sending any frames for this stream.
- */
-void sq_set_tb(SyncQueue *sq, unsigned int stream_idx, AVRational tb);
-
 /**
  * Limit the number of output frames for stream with index stream_idx
  * to max_frames.
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 04/23] fftools/sync_queue: document overall design
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 02/23] fftools/ffmpeg: move initializing next_[pd]ts to add_input_streams() Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 03/23] fftools/sync_queue: use timebase from input frames/packets Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 05/23] fftools/sync_queue: support operation with no limiting streams Anton Khirnov
                   ` (18 subsequent siblings)
  21 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

---
 fftools/sync_queue.c | 35 +++++++++++++++++++++++++++++++++++
 fftools/sync_queue.h |  5 +++++
 2 files changed, 40 insertions(+)

diff --git a/fftools/sync_queue.c b/fftools/sync_queue.c
index 7c348af300..0aee4ef5ff 100644
--- a/fftools/sync_queue.c
+++ b/fftools/sync_queue.c
@@ -28,6 +28,41 @@
 #include "objpool.h"
 #include "sync_queue.h"
 
+/*
+ * How this works:
+ * --------------
+ * time:   0    1    2    3    4    5    6    7    8    9    10   11   12   13
+ *         -------------------------------------------------------------------
+ *         |    |    |    |    |    |    |    |    |    |    |    |    |    |
+ *         |    ┌───┐┌────────┐┌───┐┌─────────────┐
+ * stream 0|    │d=1││  d=2   ││d=1││    d=3      │
+ *         |    └───┘└────────┘└───┘└─────────────┘
+ *         ┌───┐               ┌───────────────────────┐
+ * stream 1│d=1│               │         d=5           │
+ *         └───┘               └───────────────────────┘
+ *         |    ┌───┐┌───┐┌───┐┌───┐
+ * stream 2|    │d=1││d=1││d=1││d=1│ <- stream 2 is the head stream of the queue
+ *         |    └───┘└───┘└───┘└───┘
+ *                  ^              ^
+ *          [stream 2 tail] [stream 2 head]
+ *
+ * We have N streams (N=3 in the diagram), each stream is a FIFO. The *tail* of
+ * each FIFO is the frame with smallest end time, the *head* is the frame with
+ * the largest end time. Frames submitted to the queue with sq_send() are placed
+ * after the head, frames returned to the caller with sq_receive() are taken
+ * from the tail.
+ *
+ * The head stream of the whole queue (SyncQueue.head_stream) is the limiting
+ * stream with the *smallest* head timestamp, i.e. the stream whose source lags
+ * furthest behind all other streams. It determines which frames can be output
+ * from the queue.
+ *
+ * In the diagram, the head stream is 2, because it head time is t=5, while
+ * streams 0 and 1 end at t=8 and t=9 respectively. All frames that _end_ at
+ * or before t=5 can be output, i.e. the first 3 frames from stream 0, first
+ * frame from stream 1, and all 4 frames from stream 2.
+ */
+
 typedef struct SyncQueueStream {
     AVFifo          *fifo;
     AVRational       tb;
diff --git a/fftools/sync_queue.h b/fftools/sync_queue.h
index 17d282c38c..9659ee5d50 100644
--- a/fftools/sync_queue.h
+++ b/fftools/sync_queue.h
@@ -38,6 +38,11 @@ typedef union SyncQueueFrame {
 #define SQFRAME(frame) ((SyncQueueFrame){ .f = (frame) })
 #define SQPKT(pkt)     ((SyncQueueFrame){ .p = (pkt) })
 
+/**
+ * A sync queue provides timestamp synchronization between multiple streams.
+ * Some of these streams are marked as "limiting", then the queue ensures no
+ * stream gets ahead of any of the limiting streams.
+ */
 typedef struct SyncQueue SyncQueue;
 
 /**
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 05/23] fftools/sync_queue: support operation with no limiting streams
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
                   ` (2 preceding siblings ...)
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 04/23] fftools/sync_queue: document overall design Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 06/23] fftools/sync_queue: make sure audio duration matches sample count Anton Khirnov
                   ` (17 subsequent siblings)
  21 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

ffmpeg CLI will not create such queues currently, but this will become
useful in following commits.
---
 fftools/sync_queue.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/fftools/sync_queue.c b/fftools/sync_queue.c
index 0aee4ef5ff..4204a821c1 100644
--- a/fftools/sync_queue.c
+++ b/fftools/sync_queue.c
@@ -96,6 +96,8 @@ struct SyncQueue {
 
     // pool of preallocated frames to avoid constant allocations
     ObjPool *pool;
+
+    int have_limiting;
 };
 
 static void frame_move(const SyncQueue *sq, SyncQueueFrame dst,
@@ -354,8 +356,9 @@ static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx,
 
         /* We can release frames that do not end after the queue head.
          * Frames with no timestamps are just passed through with no conditions.
+         * Frames are also passed through when there are no limiting streams.
          */
-        if (cmp <= 0 || ts == AV_NOPTS_VALUE) {
+        if (cmp <= 0 || ts == AV_NOPTS_VALUE || !sq->have_limiting) {
             frame_move(sq, frame, peek);
             objpool_release(sq->pool, (void**)&peek);
             av_fifo_drain2(st->fifo, 1);
@@ -427,6 +430,8 @@ int sq_add_stream(SyncQueue *sq, int limiting)
     st->frames_max = UINT64_MAX;
     st->limiting   = limiting;
 
+    sq->have_limiting |= limiting;
+
     return sq->nb_streams++;
 }
 
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 06/23] fftools/sync_queue: make sure audio duration matches sample count
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
                   ` (3 preceding siblings ...)
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 05/23] fftools/sync_queue: support operation with no limiting streams Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 07/23] fftools/sync_queue: allow requesting a specific number of audio samples Anton Khirnov
                   ` (16 subsequent siblings)
  21 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

For audio AVFrames, nb_samples is typically more trustworthy than
duration. Since sync queues look at durations, make sure they match the
sample count.

The last audio frame in the fate-shortest test is now gone. This is more
correct, since it outlasts the last video frame.
---
 fftools/sync_queue.c    | 15 ++++++++++++++-
 tests/ref/fate/shortest |  1 -
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/fftools/sync_queue.c b/fftools/sync_queue.c
index 4204a821c1..5b98253a4a 100644
--- a/fftools/sync_queue.c
+++ b/fftools/sync_queue.c
@@ -116,6 +116,11 @@ static int64_t frame_ts(const SyncQueue *sq, SyncQueueFrame frame)
            frame.f->pts + frame.f->duration;
 }
 
+static int frame_samples(const SyncQueue *sq, SyncQueueFrame frame)
+{
+    return (sq->type == SYNC_QUEUE_PACKETS) ? 0 : frame.f->nb_samples;
+}
+
 static int frame_null(const SyncQueue *sq, SyncQueueFrame frame)
 {
     return (sq->type == SYNC_QUEUE_PACKETS) ? (frame.p == NULL) : (frame.f == NULL);
@@ -293,7 +298,7 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame)
     SyncQueueStream *st;
     SyncQueueFrame dst;
     int64_t ts;
-    int ret;
+    int ret, nb_samples;
 
     av_assert0(stream_idx < sq->nb_streams);
     st = &sq->streams[stream_idx];
@@ -313,6 +318,14 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame)
 
     frame_move(sq, dst, frame);
 
+    nb_samples = frame_samples(sq, dst);
+    // make sure frame duration is consistent with sample count
+    if (nb_samples) {
+        av_assert0(dst.f->sample_rate > 0);
+        dst.f->duration = av_rescale_q(nb_samples, (AVRational){ 1, dst.f->sample_rate },
+                                       dst.f->time_base);
+    }
+
     ts = frame_ts(sq, dst);
 
     ret = av_fifo_write(st->fifo, &dst, 1);
diff --git a/tests/ref/fate/shortest b/tests/ref/fate/shortest
index be93ff0da1..b5845508cf 100644
--- a/tests/ref/fate/shortest
+++ b/tests/ref/fate/shortest
@@ -115,4 +115,3 @@
 0,         48,         48,        1,    11212, 0xc61a3f0a, S=1,        8
 1,      85760,      85760,     1536,      418, 0xae06ca91
 0,         49,         49,        1,     1423, 0x45fba9e4, F=0x0, S=1,        8
-1,      87296,      87296,     1536,      418, 0x7bdcc3c7
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 07/23] fftools/sync_queue: allow requesting a specific number of audio samples
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
                   ` (4 preceding siblings ...)
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 06/23] fftools/sync_queue: make sure audio duration matches sample count Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-29 23:41   ` James Almer
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 08/23] fftools/ffmpeg: use sync queues for enforcing audio frame size Anton Khirnov
                   ` (15 subsequent siblings)
  21 siblings, 1 reply; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

This will be made useful in following commits.
---
 fftools/sync_queue.c | 164 ++++++++++++++++++++++++++++++++++++++++---
 fftools/sync_queue.h |  10 +++
 2 files changed, 165 insertions(+), 9 deletions(-)

diff --git a/fftools/sync_queue.c b/fftools/sync_queue.c
index 5b98253a4a..758357940f 100644
--- a/fftools/sync_queue.c
+++ b/fftools/sync_queue.c
@@ -20,10 +20,13 @@
 #include <string.h>
 
 #include "libavutil/avassert.h"
+#include "libavutil/channel_layout.h"
+#include "libavutil/cpu.h"
 #include "libavutil/error.h"
 #include "libavutil/fifo.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/mem.h"
+#include "libavutil/samplefmt.h"
 
 #include "objpool.h"
 #include "sync_queue.h"
@@ -67,6 +70,8 @@ typedef struct SyncQueueStream {
     AVFifo          *fifo;
     AVRational       tb;
 
+    /* number of audio samples in fifo */
+    uint64_t         samples_queued;
     /* stream head: largest timestamp seen */
     int64_t          head_ts;
     int              limiting;
@@ -74,7 +79,9 @@ typedef struct SyncQueueStream {
     int              finished;
 
     uint64_t         frames_sent;
+    uint64_t         samples_sent;
     uint64_t         frames_max;
+    int              frame_samples;
 } SyncQueueStream;
 
 struct SyncQueue {
@@ -109,8 +116,18 @@ static void frame_move(const SyncQueue *sq, SyncQueueFrame dst,
         av_frame_move_ref(dst.f, src.f);
 }
 
-static int64_t frame_ts(const SyncQueue *sq, SyncQueueFrame frame)
+/**
+ * Compute the end timestamp of a frame. If nb_samples is provided, consider
+ * the frame to have this number of audio samples, otherwise use frame duration.
+ */
+static int64_t frame_end(const SyncQueue *sq, SyncQueueFrame frame, int nb_samples)
 {
+    if (nb_samples) {
+        int64_t d = av_rescale_q(nb_samples, (AVRational){ 1, frame.f->sample_rate},
+                                 frame.f->time_base);
+        return frame.f->pts + d;
+    }
+
     return (sq->type == SYNC_QUEUE_PACKETS) ?
            frame.p->pts + frame.p->duration :
            frame.f->pts + frame.f->duration;
@@ -265,7 +282,7 @@ static int overflow_heartbeat(SyncQueue *sq, int stream_idx)
     /* get the chosen stream's tail timestamp */
     for (size_t i = 0; tail_ts == AV_NOPTS_VALUE &&
                        av_fifo_peek(st->fifo, &frame, 1, i) >= 0; i++)
-        tail_ts = frame_ts(sq, frame);
+        tail_ts = frame_end(sq, frame, 0);
 
     /* overflow triggers when the tail is over specified duration behind the head */
     if (tail_ts == AV_NOPTS_VALUE || tail_ts >= st->head_ts ||
@@ -326,7 +343,7 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame)
                                        dst.f->time_base);
     }
 
-    ts = frame_ts(sq, dst);
+    ts = frame_end(sq, dst, 0);
 
     ret = av_fifo_write(st->fifo, &dst, 1);
     if (ret < 0) {
@@ -337,13 +354,116 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame)
 
     stream_update_ts(sq, stream_idx, ts);
 
-    st->frames_sent++;
+    st->samples_queued += nb_samples;
+    st->samples_sent   += nb_samples;
+
+    if (st->frame_samples)
+        st->frames_sent = st->samples_sent / st->frame_samples;
+    else
+        st->frames_sent++;
+
     if (st->frames_sent >= st->frames_max)
         finish_stream(sq, stream_idx);
 
     return 0;
 }
 
+static void offset_audio(AVFrame *f, int nb_samples)
+{
+    const int planar = av_sample_fmt_is_planar(f->format);
+    const int planes = planar ? f->ch_layout.nb_channels : 1;
+    const int    bps = av_get_bytes_per_sample(f->format);
+    const int offset = nb_samples * bps * (planar ? 1 : f->ch_layout.nb_channels);
+
+    av_assert0(bps > 0);
+    av_assert0(nb_samples < f->nb_samples);
+
+    for (int i = 0; i < planes; i++) {
+        f->extended_data[i] += offset;
+        if (i < FF_ARRAY_ELEMS(f->data))
+            f->data[i] = f->extended_data[i];
+    }
+    f->linesize[0] -= offset;
+    f->nb_samples  -= nb_samples;
+    f->duration     = av_rescale_q(f->nb_samples, (AVRational){ 1, f->sample_rate },
+                                   f->time_base);
+    f->pts         += av_rescale_q(nb_samples,    (AVRational){ 1, f->sample_rate },
+                                   f->time_base);
+}
+
+static int receive_samples(SyncQueue *sq, SyncQueueStream *st,
+                           AVFrame *dst, int nb_samples)
+{
+    SyncQueueFrame src;
+    int ret;
+
+    av_assert0(st->samples_queued >= nb_samples);
+
+    ret = av_fifo_peek(st->fifo, &src, 1, 0);
+    av_assert0(ret >= 0);
+
+    // peeked frame has enough samples and its data is aligned
+    // -> we can just make a reference and limit its sample count
+    if (src.f->nb_samples > nb_samples &&
+        !((uintptr_t)src.f->data[0] & (av_cpu_max_align() - 1))) {
+        ret = av_frame_ref(dst, src.f);
+        if (ret < 0)
+            return ret;
+
+        dst->nb_samples = nb_samples;
+        offset_audio(src.f, nb_samples);
+        st->samples_queued -= nb_samples;
+
+        return 0;
+    }
+
+    // otherwise allocate a new frame and copy the data
+    ret = av_channel_layout_copy(&dst->ch_layout, &src.f->ch_layout);
+    if (ret < 0)
+        return ret;
+
+    dst->format     = src.f->format;
+    dst->nb_samples = nb_samples;
+
+    ret = av_frame_get_buffer(dst, 0);
+    if (ret < 0)
+        goto fail;
+
+    ret = av_frame_copy_props(dst, src.f);
+    if (ret < 0)
+        goto fail;
+
+    dst->nb_samples = 0;
+    while (dst->nb_samples < nb_samples) {
+        int to_copy;
+
+        ret = av_fifo_peek(st->fifo, &src, 1, 0);
+        av_assert0(ret >= 0);
+
+        to_copy = FFMIN(nb_samples - dst->nb_samples, src.f->nb_samples);
+
+        av_samples_copy(dst->extended_data, src.f->extended_data, dst->nb_samples,
+                        0, to_copy, dst->ch_layout.nb_channels, dst->format);
+
+        if (to_copy < src.f->nb_samples)
+            offset_audio(src.f, to_copy);
+        else {
+            av_frame_unref(src.f);
+            objpool_release(sq->pool, (void**)&src);
+            av_fifo_drain2(st->fifo, 1);
+        }
+        st->samples_queued -= to_copy;
+
+        dst->nb_samples += to_copy;
+    }
+
+    return 0;
+
+fail:
+    av_frame_unref(dst);
+    return ret;
+}
+
 static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx,
                               SyncQueueFrame frame)
 {
@@ -354,13 +474,18 @@ static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx,
     av_assert0(stream_idx < sq->nb_streams);
     st = &sq->streams[stream_idx];
 
-    if (av_fifo_can_read(st->fifo)) {
+    if (av_fifo_can_read(st->fifo) &&
+        (st->frame_samples <= st->samples_queued || st->finished)) {
+        int nb_samples = st->frame_samples;
         SyncQueueFrame peek;
         int64_t ts;
         int cmp = 1;
 
+        if (st->finished)
+            nb_samples = FFMIN(nb_samples, st->samples_queued);
+
         av_fifo_peek(st->fifo, &peek, 1, 0);
-        ts = frame_ts(sq, peek);
+        ts = frame_end(sq, peek, nb_samples);
 
         /* check if this stream's tail timestamp does not overtake
          * the overall queue head */
@@ -372,9 +497,18 @@ static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx,
          * Frames are also passed through when there are no limiting streams.
          */
         if (cmp <= 0 || ts == AV_NOPTS_VALUE || !sq->have_limiting) {
-            frame_move(sq, frame, peek);
-            objpool_release(sq->pool, (void**)&peek);
-            av_fifo_drain2(st->fifo, 1);
+            if (nb_samples && nb_samples != peek.f->nb_samples) {
+                int ret = receive_samples(sq, st, frame.f, nb_samples);
+                if (ret < 0)
+                    return ret;
+            } else {
+                frame_move(sq, frame, peek);
+                objpool_release(sq->pool, (void**)&peek);
+                av_fifo_drain2(st->fifo, 1);
+                av_assert0(st->samples_queued >= frame_samples(sq, frame));
+                st->samples_queued -= frame_samples(sq, frame);
+            }
+
             return 0;
         }
     }
@@ -460,6 +594,18 @@ void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx, uint64_t frames)
         finish_stream(sq, stream_idx);
 }
 
+void sq_frame_samples(SyncQueue *sq, unsigned int stream_idx,
+                      int frame_samples)
+{
+    SyncQueueStream *st;
+
+    av_assert0(sq->type == SYNC_QUEUE_FRAMES);
+    av_assert0(stream_idx < sq->nb_streams);
+    st = &sq->streams[stream_idx];
+
+    st->frame_samples = frame_samples;
+}
+
 SyncQueue *sq_alloc(enum SyncQueueType type, int64_t buf_size_us)
 {
     SyncQueue *sq = av_mallocz(sizeof(*sq));
diff --git a/fftools/sync_queue.h b/fftools/sync_queue.h
index 9659ee5d50..bc7cd42390 100644
--- a/fftools/sync_queue.h
+++ b/fftools/sync_queue.h
@@ -71,6 +71,16 @@ int sq_add_stream(SyncQueue *sq, int limiting);
 void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx,
                      uint64_t max_frames);
 
+/**
+ * Set a constant output audio frame size, in samples. Can only be used with
+ * SYNC_QUEUE_FRAMES queues and audio streams.
+ *
+ * All output frames will have exactly frame_samples audio samples, except
+ * possibly for the last one, which may have fewer.
+ */
+void sq_frame_samples(SyncQueue *sq, unsigned int stream_idx,
+                      int frame_samples);
+
 /**
  * Submit a frame for the stream with index stream_idx.
  *
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 08/23] fftools/ffmpeg: use sync queues for enforcing audio frame size
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
                   ` (5 preceding siblings ...)
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 07/23] fftools/sync_queue: allow requesting a specific number of audio samples Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-25 21:43   ` Michael Niedermayer
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 09/23] fftools/ffmpeg: stop handling AVMEDIA_TYPE_DATA in init_output_stream_encode() Anton Khirnov
                   ` (14 subsequent siblings)
  21 siblings, 1 reply; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

The code currently uses lavfi for this, which creates a sort of
configuration dependency loop - the encoder should be ideally
initialized with information from the first audio frame, but to get this
frame one needs to first open the encoder to know the frame size. This
necessitates an awkward workaround, which causes audio handling to be
different from video.

With this change, audio encoder initialization is congruent with video.
---
 fftools/ffmpeg.c          | 58 ++++++++-------------------------------
 fftools/ffmpeg_filter.c   |  8 ------
 fftools/ffmpeg_mux_init.c | 19 +++++++++----
 3 files changed, 25 insertions(+), 60 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 3a205a3b01..f00b2d44e4 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1028,6 +1028,8 @@ static void do_audio_out(OutputFile *of, OutputStream *ost,
     AVCodecContext *enc = ost->enc_ctx;
     int ret;
 
+    init_output_stream_wrapper(ost, frame, 1);
+
     if (frame->pts == AV_NOPTS_VALUE)
         frame->pts = ost->next_pts;
     else {
@@ -1378,18 +1380,6 @@ static int reap_filters(int flush)
             continue;
         filter = ost->filter->filter;
 
-        /*
-         * Unlike video, with audio the audio frame size matters.
-         * Currently we are fully reliant on the lavfi filter chain to
-         * do the buffering deed for us, and thus the frame size parameter
-         * needs to be set accordingly. Where does one get the required
-         * frame size? From the initialized AVCodecContext of an audio
-         * encoder. Thus, if we have gotten to an audio stream, initialize
-         * the encoder earlier than receiving the first AVFrame.
-         */
-        if (av_buffersink_get_type(filter) == AVMEDIA_TYPE_AUDIO)
-            init_output_stream_wrapper(ost, NULL, 1);
-
         filtered_frame = ost->filtered_frame;
 
         while (1) {
@@ -1432,6 +1422,7 @@ static int reap_filters(int flush)
                 break;
             case AVMEDIA_TYPE_AUDIO:
                 if (!(enc->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE) &&
+                    avcodec_is_open(enc) &&
                     enc->ch_layout.nb_channels != filtered_frame->ch_layout.nb_channels) {
                     av_log(NULL, AV_LOG_ERROR,
                            "Audio filter graph output is not normalized and encoder does not support parameter changes\n");
@@ -3238,10 +3229,13 @@ static int init_output_stream(OutputStream *ost, AVFrame *frame,
                     ost->file_index, ost->index);
             return ret;
         }
-        if (codec->type == AVMEDIA_TYPE_AUDIO &&
-            !(codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
-            av_buffersink_set_frame_size(ost->filter->filter,
-                                            ost->enc_ctx->frame_size);
+
+        if (ost->enc_ctx->frame_size) {
+            av_assert0(ost->sq_idx_encode >= 0);
+            sq_frame_samples(output_files[ost->file_index]->sq_encode,
+                             ost->sq_idx_encode, ost->enc_ctx->frame_size);
+        }
+
         assert_avoptions(ost->encoder_opts);
         if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000 &&
             ost->enc_ctx->codec_id != AV_CODEC_ID_CODEC2 /* don't complain about 700 bit/s modes */)
@@ -3331,12 +3325,8 @@ static int transcode_init(void)
 
     /*
      * initialize stream copy and subtitle/data streams.
-     * Encoded AVFrame based streams will get initialized as follows:
-     * - when the first AVFrame is received in do_video_out
-     * - just before the first AVFrame is received in either transcode_step
-     *   or reap_filters due to us requiring the filter chain buffer sink
-     *   to be configured with the correct audio frame size, which is only
-     *   known after the encoder is initialized.
+     * Encoded AVFrame based streams will get initialized when the first AVFrame
+     * is received in do_video_out
      */
     for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
         if (ost->enc_ctx &&
@@ -3942,30 +3932,6 @@ static int transcode_step(void)
     }
 
     if (ost->filter && ost->filter->graph->graph) {
-        /*
-         * Similar case to the early audio initialization in reap_filters.
-         * Audio is special in ffmpeg.c currently as we depend on lavfi's
-         * audio frame buffering/creation to get the output audio frame size
-         * in samples correct. The audio frame size for the filter chain is
-         * configured during the output stream initialization.
-         *
-         * Apparently avfilter_graph_request_oldest (called in
-         * transcode_from_filter just down the line) peeks. Peeking already
-         * puts one frame "ready to be given out", which means that any
-         * update in filter buffer sink configuration afterwards will not
-         * help us. And yes, even if it would be utilized,
-         * av_buffersink_get_samples is affected, as it internally utilizes
-         * the same early exit for peeked frames.
-         *
-         * In other words, if avfilter_graph_request_oldest would not make
-         * further filter chain configuration or usage of
-         * av_buffersink_get_samples useless (by just causing the return
-         * of the peeked AVFrame as-is), we could get rid of this additional
-         * early encoder initialization.
-         */
-        if (av_buffersink_get_type(ost->filter->filter) == AVMEDIA_TYPE_AUDIO)
-            init_output_stream_wrapper(ost, NULL, 1);
-
         if ((ret = transcode_from_filter(ost->filter->graph, &ist)) < 0)
             return ret;
         if (!ist)
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 314b89b585..c9fd65e902 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -1242,14 +1242,6 @@ int configure_filtergraph(FilterGraph *fg)
 
     fg->reconfiguration = 1;
 
-    for (i = 0; i < fg->nb_outputs; i++) {
-        OutputStream *ost = fg->outputs[i]->ost;
-        if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_AUDIO &&
-            !(ost->enc_ctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
-            av_buffersink_set_frame_size(ost->filter->filter,
-                                         ost->enc_ctx->frame_size);
-    }
-
     for (i = 0; i < fg->nb_inputs; i++) {
         AVFrame *tmp;
         while (av_fifo_read(fg->inputs[i]->frame_queue, &tmp, 1) >= 0) {
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index ebc17059f9..385312d4fe 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -1451,7 +1451,7 @@ static void create_streams(Muxer *mux, const OptionsContext *o)
 static int setup_sync_queues(Muxer *mux, AVFormatContext *oc, int64_t buf_size_us)
 {
     OutputFile *of = &mux->of;
-    int nb_av_enc = 0, nb_interleaved = 0;
+    int nb_av_enc = 0, nb_audio_fs = 0, nb_interleaved = 0;
     int limit_frames = 0, limit_frames_av_enc = 0;
 
 #define IS_AV_ENC(ost, type)  \
@@ -1468,19 +1468,26 @@ static int setup_sync_queues(Muxer *mux, AVFormatContext *oc, int64_t buf_size_u
 
         nb_interleaved += IS_INTERLEAVED(type);
         nb_av_enc      += IS_AV_ENC(ost, type);
+        nb_audio_fs    += (ost->enc_ctx && type == AVMEDIA_TYPE_AUDIO &&
+                           !(ost->enc_ctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE));
 
         limit_frames        |=  ms->max_frames < INT64_MAX;
         limit_frames_av_enc |= (ms->max_frames < INT64_MAX) && IS_AV_ENC(ost, type);
     }
 
     if (!((nb_interleaved > 1 && of->shortest) ||
-          (nb_interleaved > 0 && limit_frames)))
+          (nb_interleaved > 0 && limit_frames) ||
+          nb_audio_fs))
         return 0;
 
-    /* if we have more than one encoded audio/video streams, or at least
-     * one encoded audio/video stream is frame-limited, then we
-     * synchronize them before encoding */
-    if ((of->shortest && nb_av_enc > 1) || limit_frames_av_enc) {
+    /* we use a sync queue before encoding when:
+     * - 'shortest' is in effect and we have two or more encoded audio/video
+     *   streams
+     * - at least one encoded audio/video stream is frame-limited, since
+     *   that has similar semantics to 'shortest'
+     * - at least one audio encoder requires constant frame sizes
+     */
+    if ((of->shortest && nb_av_enc > 1) || limit_frames_av_enc || nb_audio_fs) {
         of->sq_encode = sq_alloc(SYNC_QUEUE_FRAMES, buf_size_us);
         if (!of->sq_encode)
             return AVERROR(ENOMEM);
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 09/23] fftools/ffmpeg: stop handling AVMEDIA_TYPE_DATA in init_output_stream_encode()
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
                   ` (6 preceding siblings ...)
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 08/23] fftools/ffmpeg: use sync queues for enforcing audio frame size Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-25 19:43   ` James Almer
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 10/23] fftools/ffmpeg: drop unnecessarily indirection Anton Khirnov
                   ` (13 subsequent siblings)
  21 siblings, 1 reply; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

We do not support data encoders, so this should never be reached.
---
 fftools/ffmpeg.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index f00b2d44e4..a424c69725 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -3174,8 +3174,6 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame)
             }
         }
 
-        break;
-    case AVMEDIA_TYPE_DATA:
         break;
     default:
         abort();
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 10/23] fftools/ffmpeg: drop unnecessarily indirection
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
                   ` (7 preceding siblings ...)
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 09/23] fftools/ffmpeg: stop handling AVMEDIA_TYPE_DATA in init_output_stream_encode() Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 11/23] fftools/ffmpeg: use stack variables to shorten code Anton Khirnov
                   ` (12 subsequent siblings)
  21 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

init_output_stream() can print log messages directly, there is no need
to ship them to the caller.
---
 fftools/ffmpeg.c | 23 +++++++----------------
 1 file changed, 7 insertions(+), 16 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index a424c69725..77c41c7cc9 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -724,23 +724,18 @@ early_exit:
     return float_pts;
 }
 
-static int init_output_stream(OutputStream *ost, AVFrame *frame,
-                              char *error, int error_len);
+static int init_output_stream(OutputStream *ost, AVFrame *frame);
 
 static int init_output_stream_wrapper(OutputStream *ost, AVFrame *frame,
                                       unsigned int fatal)
 {
     int ret = AVERROR_BUG;
-    char error[1024] = {0};
 
     if (ost->initialized)
         return 0;
 
-    ret = init_output_stream(ost, frame, error, sizeof(error));
+    ret = init_output_stream(ost, frame);
     if (ret < 0) {
-        av_log(ost, AV_LOG_ERROR, "Error initializing output stream: %s\n",
-               error);
-
         if (fatal)
             exit_program(1);
     }
@@ -3188,8 +3183,7 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame)
     return 0;
 }
 
-static int init_output_stream(OutputStream *ost, AVFrame *frame,
-                              char *error, int error_len)
+static int init_output_stream(OutputStream *ost, AVFrame *frame)
 {
     int ret = 0;
 
@@ -3212,19 +3206,16 @@ static int init_output_stream(OutputStream *ost, AVFrame *frame,
 
         ret = hw_device_setup_for_encode(ost);
         if (ret < 0) {
-            snprintf(error, error_len, "Device setup failed for "
-                     "encoder on output stream #%d:%d : %s",
-                     ost->file_index, ost->index, av_err2str(ret));
+            av_log(ost, AV_LOG_ERROR,
+                   "Encoding hardware device setup failed: %s\n", av_err2str(ret));
             return ret;
         }
 
         if ((ret = avcodec_open2(ost->enc_ctx, codec, &ost->encoder_opts)) < 0) {
             if (ret == AVERROR_EXPERIMENTAL)
                 abort_codec_experimental(codec, 1);
-            snprintf(error, error_len,
-                     "Error while opening encoder for output stream #%d:%d - "
-                     "maybe incorrect parameters such as bit_rate, rate, width or height",
-                    ost->file_index, ost->index);
+            av_log(ost, AV_LOG_ERROR, "Error while opening encoder - maybe "
+                   "incorrect parameters such as bit_rate, rate, width or height.\n");
             return ret;
         }
 
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 11/23] fftools/ffmpeg: use stack variables to shorten code
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
                   ` (8 preceding siblings ...)
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 10/23] fftools/ffmpeg: drop unnecessarily indirection Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 12/23] fftools/ffmpeg: move encoder initialization to init_output_stream_encode Anton Khirnov
                   ` (11 subsequent siblings)
  21 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

---
 fftools/ffmpeg.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 77c41c7cc9..7b7db03bde 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -3018,6 +3018,7 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame)
     InputStream *ist = ost->ist;
     AVCodecContext *enc_ctx = ost->enc_ctx;
     AVCodecContext *dec_ctx = NULL;
+    const AVCodec      *enc = enc_ctx->codec;
     OutputFile      *of = output_files[ost->file_index];
     int ret;
 
@@ -3044,9 +3045,9 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame)
             !ost->frame_rate.den))
             ost->frame_rate = ost->max_frame_rate;
 
-        if (enc_ctx->codec->supported_framerates && !ost->force_fps) {
-            int idx = av_find_nearest_q_idx(ost->frame_rate, enc_ctx->codec->supported_framerates);
-            ost->frame_rate = enc_ctx->codec->supported_framerates[idx];
+        if (enc->supported_framerates && !ost->force_fps) {
+            int idx = av_find_nearest_q_idx(ost->frame_rate, enc->supported_framerates);
+            ost->frame_rate = enc->supported_framerates[idx];
         }
         // reduce frame rate for mpeg4 to be within the spec limits
         if (enc_ctx->codec_id == AV_CODEC_ID_MPEG4) {
@@ -3119,7 +3120,7 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame)
                 frame->top_field_first = !!ost->top_field_first;
 
             if (frame->interlaced_frame) {
-                if (enc_ctx->codec->id == AV_CODEC_ID_MJPEG)
+                if (enc->id == AV_CODEC_ID_MJPEG)
                     enc_ctx->field_order = frame->top_field_first ? AV_FIELD_TT:AV_FIELD_BB;
                 else
                     enc_ctx->field_order = frame->top_field_first ? AV_FIELD_TB:AV_FIELD_BT;
@@ -3143,12 +3144,12 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame)
         }
         if (dec_ctx && dec_ctx->subtitle_header) {
             /* ASS code assumes this buffer is null terminated so add extra byte. */
-            ost->enc_ctx->subtitle_header = av_mallocz(dec_ctx->subtitle_header_size + 1);
-            if (!ost->enc_ctx->subtitle_header)
+            enc_ctx->subtitle_header = av_mallocz(dec_ctx->subtitle_header_size + 1);
+            if (!enc_ctx->subtitle_header)
                 return AVERROR(ENOMEM);
-            memcpy(ost->enc_ctx->subtitle_header, dec_ctx->subtitle_header,
+            memcpy(enc_ctx->subtitle_header, dec_ctx->subtitle_header,
                    dec_ctx->subtitle_header_size);
-            ost->enc_ctx->subtitle_header_size = dec_ctx->subtitle_header_size;
+            enc_ctx->subtitle_header_size = dec_ctx->subtitle_header_size;
         }
         if (ist && ist->dec->type == AVMEDIA_TYPE_SUBTITLE &&
             enc_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
@@ -3156,7 +3157,7 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame)
             AVCodecDescriptor const *input_descriptor =
                 avcodec_descriptor_get(ist->dec->id);
             AVCodecDescriptor const *output_descriptor =
-                avcodec_descriptor_get(ost->enc_ctx->codec_id);
+                avcodec_descriptor_get(enc_ctx->codec_id);
             if (input_descriptor)
                 input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
             if (output_descriptor)
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 12/23] fftools/ffmpeg: move encoder initialization to init_output_stream_encode
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
                   ` (9 preceding siblings ...)
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 11/23] fftools/ffmpeg: use stack variables to shorten code Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 13/23] fftools/ffmpeg: reindent after previous commit Anton Khirnov
                   ` (10 subsequent siblings)
  21 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

Encoder initialization is currently split rather arbitrarily between
init_output_stream_encode() and init_output_stream(). Move all of it to
init_output_stream_encode().
---
 fftools/ffmpeg.c | 37 +++++++++++++++++--------------------
 1 file changed, 17 insertions(+), 20 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 7b7db03bde..9ae3d3891f 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -3179,27 +3179,10 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame)
     if (ost->bitexact)
         enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
 
-    ost->mux_timebase = enc_ctx->time_base;
-
-    return 0;
-}
-
-static int init_output_stream(OutputStream *ost, AVFrame *frame)
-{
-    int ret = 0;
-
-    if (ost->enc_ctx) {
-        const AVCodec *codec = ost->enc_ctx->codec;
-        InputStream *ist = ost->ist;
-
-        ret = init_output_stream_encode(ost, frame);
-        if (ret < 0)
-            return ret;
-
         if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
             av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
 
-        if (codec->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE) {
+        if (enc->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE) {
             ret = av_dict_set(&ost->encoder_opts, "flags", "+copy_opaque", AV_DICT_MULTIKEY);
             if (ret < 0)
                 return ret;
@@ -3212,9 +3195,9 @@ static int init_output_stream(OutputStream *ost, AVFrame *frame)
             return ret;
         }
 
-        if ((ret = avcodec_open2(ost->enc_ctx, codec, &ost->encoder_opts)) < 0) {
+        if ((ret = avcodec_open2(ost->enc_ctx, enc, &ost->encoder_opts)) < 0) {
             if (ret == AVERROR_EXPERIMENTAL)
-                abort_codec_experimental(codec, 1);
+                abort_codec_experimental(enc, 1);
             av_log(ost, AV_LOG_ERROR, "Error while opening encoder - maybe "
                    "incorrect parameters such as bit_rate, rate, width or height.\n");
             return ret;
@@ -3282,6 +3265,20 @@ static int init_output_stream(OutputStream *ost, AVFrame *frame)
         // copy estimated duration as a hint to the muxer
         if (ost->st->duration <= 0 && ist && ist->st->duration > 0)
             ost->st->duration = av_rescale_q(ist->st->duration, ist->st->time_base, ost->st->time_base);
+
+    ost->mux_timebase = enc_ctx->time_base;
+
+    return 0;
+}
+
+static int init_output_stream(OutputStream *ost, AVFrame *frame)
+{
+    int ret = 0;
+
+    if (ost->enc_ctx) {
+        ret = init_output_stream_encode(ost, frame);
+        if (ret < 0)
+            return ret;
     } else if (ost->ist) {
         ret = init_output_stream_streamcopy(ost);
         if (ret < 0)
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 13/23] fftools/ffmpeg: reindent after previous commit
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
                   ` (10 preceding siblings ...)
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 12/23] fftools/ffmpeg: move encoder initialization to init_output_stream_encode Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-28 22:42   ` Michael Niedermayer
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 14/23] fftools/ffmpeg: move initializing encoders to a new file Anton Khirnov
                   ` (9 subsequent siblings)
  21 siblings, 1 reply; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

---
 fftools/ffmpeg.c | 142 +++++++++++++++++++++++------------------------
 1 file changed, 71 insertions(+), 71 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 9ae3d3891f..5bfe465e97 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -3179,92 +3179,92 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame)
     if (ost->bitexact)
         enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
 
-        if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
-            av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
+    if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
+        av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
 
-        if (enc->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE) {
-            ret = av_dict_set(&ost->encoder_opts, "flags", "+copy_opaque", AV_DICT_MULTIKEY);
-            if (ret < 0)
-                return ret;
-        }
-
-        ret = hw_device_setup_for_encode(ost);
-        if (ret < 0) {
-            av_log(ost, AV_LOG_ERROR,
-                   "Encoding hardware device setup failed: %s\n", av_err2str(ret));
+    if (enc->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE) {
+        ret = av_dict_set(&ost->encoder_opts, "flags", "+copy_opaque", AV_DICT_MULTIKEY);
+        if (ret < 0)
             return ret;
-        }
+    }
 
-        if ((ret = avcodec_open2(ost->enc_ctx, enc, &ost->encoder_opts)) < 0) {
-            if (ret == AVERROR_EXPERIMENTAL)
-                abort_codec_experimental(enc, 1);
-            av_log(ost, AV_LOG_ERROR, "Error while opening encoder - maybe "
-                   "incorrect parameters such as bit_rate, rate, width or height.\n");
-            return ret;
-        }
+    ret = hw_device_setup_for_encode(ost);
+    if (ret < 0) {
+        av_log(ost, AV_LOG_ERROR,
+               "Encoding hardware device setup failed: %s\n", av_err2str(ret));
+        return ret;
+    }
 
-        if (ost->enc_ctx->frame_size) {
-            av_assert0(ost->sq_idx_encode >= 0);
-            sq_frame_samples(output_files[ost->file_index]->sq_encode,
-                             ost->sq_idx_encode, ost->enc_ctx->frame_size);
-        }
+    if ((ret = avcodec_open2(ost->enc_ctx, enc, &ost->encoder_opts)) < 0) {
+        if (ret == AVERROR_EXPERIMENTAL)
+            abort_codec_experimental(enc, 1);
+        av_log(ost, AV_LOG_ERROR, "Error while opening encoder - maybe "
+               "incorrect parameters such as bit_rate, rate, width or height.\n");
+        return ret;
+    }
 
-        assert_avoptions(ost->encoder_opts);
-        if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000 &&
-            ost->enc_ctx->codec_id != AV_CODEC_ID_CODEC2 /* don't complain about 700 bit/s modes */)
-            av_log(ost, AV_LOG_WARNING, "The bitrate parameter is set too low."
-                                        " It takes bits/s as argument, not kbits/s\n");
+    if (ost->enc_ctx->frame_size) {
+        av_assert0(ost->sq_idx_encode >= 0);
+        sq_frame_samples(output_files[ost->file_index]->sq_encode,
+                         ost->sq_idx_encode, ost->enc_ctx->frame_size);
+    }
 
-        ret = avcodec_parameters_from_context(ost->st->codecpar, ost->enc_ctx);
-        if (ret < 0) {
-            av_log(ost, AV_LOG_FATAL,
-                   "Error initializing the output stream codec context.\n");
-            exit_program(1);
-        }
+    assert_avoptions(ost->encoder_opts);
+    if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000 &&
+        ost->enc_ctx->codec_id != AV_CODEC_ID_CODEC2 /* don't complain about 700 bit/s modes */)
+        av_log(ost, AV_LOG_WARNING, "The bitrate parameter is set too low."
+                                    " It takes bits/s as argument, not kbits/s\n");
 
-        if (ost->enc_ctx->nb_coded_side_data) {
-            int i;
+    ret = avcodec_parameters_from_context(ost->st->codecpar, ost->enc_ctx);
+    if (ret < 0) {
+        av_log(ost, AV_LOG_FATAL,
+               "Error initializing the output stream codec context.\n");
+        exit_program(1);
+    }
 
-            for (i = 0; i < ost->enc_ctx->nb_coded_side_data; i++) {
-                const AVPacketSideData *sd_src = &ost->enc_ctx->coded_side_data[i];
-                uint8_t *dst_data;
+    if (ost->enc_ctx->nb_coded_side_data) {
+        int i;
 
-                dst_data = av_stream_new_side_data(ost->st, sd_src->type, sd_src->size);
-                if (!dst_data)
-                    return AVERROR(ENOMEM);
-                memcpy(dst_data, sd_src->data, sd_src->size);
-            }
+        for (i = 0; i < ost->enc_ctx->nb_coded_side_data; i++) {
+            const AVPacketSideData *sd_src = &ost->enc_ctx->coded_side_data[i];
+            uint8_t *dst_data;
+
+            dst_data = av_stream_new_side_data(ost->st, sd_src->type, sd_src->size);
+            if (!dst_data)
+                return AVERROR(ENOMEM);
+            memcpy(dst_data, sd_src->data, sd_src->size);
         }
+    }
 
-        /*
-         * Add global input side data. For now this is naive, and copies it
-         * from the input stream's global side data. All side data should
-         * really be funneled over AVFrame and libavfilter, then added back to
-         * packet side data, and then potentially using the first packet for
-         * global side data.
-         */
-        if (ist) {
-            int i;
-            for (i = 0; i < ist->st->nb_side_data; i++) {
-                AVPacketSideData *sd = &ist->st->side_data[i];
-                if (sd->type != AV_PKT_DATA_CPB_PROPERTIES) {
-                    uint8_t *dst = av_stream_new_side_data(ost->st, sd->type, sd->size);
-                    if (!dst)
-                        return AVERROR(ENOMEM);
-                    memcpy(dst, sd->data, sd->size);
-                    if (ist->autorotate && sd->type == AV_PKT_DATA_DISPLAYMATRIX)
-                        av_display_rotation_set((int32_t *)dst, 0);
-                }
+    /*
+     * Add global input side data. For now this is naive, and copies it
+     * from the input stream's global side data. All side data should
+     * really be funneled over AVFrame and libavfilter, then added back to
+     * packet side data, and then potentially using the first packet for
+     * global side data.
+     */
+    if (ist) {
+        int i;
+        for (i = 0; i < ist->st->nb_side_data; i++) {
+            AVPacketSideData *sd = &ist->st->side_data[i];
+            if (sd->type != AV_PKT_DATA_CPB_PROPERTIES) {
+                uint8_t *dst = av_stream_new_side_data(ost->st, sd->type, sd->size);
+                if (!dst)
+                    return AVERROR(ENOMEM);
+                memcpy(dst, sd->data, sd->size);
+                if (ist->autorotate && sd->type == AV_PKT_DATA_DISPLAYMATRIX)
+                    av_display_rotation_set((int32_t *)dst, 0);
             }
         }
+    }
 
-        // copy timebase while removing common factors
-        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});
+    // copy timebase while removing common factors
+    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});
 
-        // copy estimated duration as a hint to the muxer
-        if (ost->st->duration <= 0 && ist && ist->st->duration > 0)
-            ost->st->duration = av_rescale_q(ist->st->duration, ist->st->time_base, ost->st->time_base);
+    // copy estimated duration as a hint to the muxer
+    if (ost->st->duration <= 0 && ist && ist->st->duration > 0)
+        ost->st->duration = av_rescale_q(ist->st->duration, ist->st->time_base, ost->st->time_base);
 
     ost->mux_timebase = enc_ctx->time_base;
 
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 14/23] fftools/ffmpeg: move initializing encoders to a new file
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
                   ` (11 preceding siblings ...)
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 13/23] fftools/ffmpeg: reindent after previous commit Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 15/23] fftools/ffmpeg: simplify output stream initialization call graph Anton Khirnov
                   ` (8 subsequent siblings)
  21 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

This file will contain more encoding-related code in the future.
---
 fftools/Makefile     |   1 +
 fftools/ffmpeg.c     | 306 +-------------------------------------
 fftools/ffmpeg.h     |   2 +
 fftools/ffmpeg_enc.c | 342 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 346 insertions(+), 305 deletions(-)
 create mode 100644 fftools/ffmpeg_enc.c

diff --git a/fftools/Makefile b/fftools/Makefile
index 8ac38e75d2..9939c7095c 100644
--- a/fftools/Makefile
+++ b/fftools/Makefile
@@ -11,6 +11,7 @@ ALLAVPROGS_G = $(AVBASENAMES:%=%$(PROGSSUF)_g$(EXESUF))
 
 OBJS-ffmpeg +=                  \
     fftools/ffmpeg_demux.o      \
+    fftools/ffmpeg_enc.o        \
     fftools/ffmpeg_filter.o     \
     fftools/ffmpeg_hw.o         \
     fftools/ffmpeg_mux.o        \
diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 5bfe465e97..03e5391970 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -2967,316 +2967,12 @@ static int init_output_stream_streamcopy(OutputStream *ost)
     return 0;
 }
 
-static void set_encoder_id(OutputFile *of, OutputStream *ost)
-{
-    const char *cname = ost->enc_ctx->codec->name;
-    uint8_t *encoder_string;
-    int encoder_string_len;
-
-    if (av_dict_get(ost->st->metadata, "encoder",  NULL, 0))
-        return;
-
-    encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(cname) + 2;
-    encoder_string     = av_mallocz(encoder_string_len);
-    if (!encoder_string)
-        report_and_exit(AVERROR(ENOMEM));
-
-    if (!of->bitexact && !ost->bitexact)
-        av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len);
-    else
-        av_strlcpy(encoder_string, "Lavc ", encoder_string_len);
-    av_strlcat(encoder_string, cname, encoder_string_len);
-    av_dict_set(&ost->st->metadata, "encoder",  encoder_string,
-                AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
-}
-
-static void init_encoder_time_base(OutputStream *ost, AVRational default_time_base)
-{
-    InputStream *ist = ost->ist;
-    AVCodecContext *enc_ctx = ost->enc_ctx;
-
-    if (ost->enc_timebase.num > 0) {
-        enc_ctx->time_base = ost->enc_timebase;
-        return;
-    }
-
-    if (ost->enc_timebase.num < 0) {
-        if (ist) {
-            enc_ctx->time_base = ist->st->time_base;
-            return;
-        }
-
-        av_log(ost, AV_LOG_WARNING,
-               "Input stream data not available, using default time base\n");
-    }
-
-    enc_ctx->time_base = default_time_base;
-}
-
-static int init_output_stream_encode(OutputStream *ost, AVFrame *frame)
-{
-    InputStream *ist = ost->ist;
-    AVCodecContext *enc_ctx = ost->enc_ctx;
-    AVCodecContext *dec_ctx = NULL;
-    const AVCodec      *enc = enc_ctx->codec;
-    OutputFile      *of = output_files[ost->file_index];
-    int ret;
-
-    set_encoder_id(output_files[ost->file_index], ost);
-
-    if (ist) {
-        dec_ctx = ist->dec_ctx;
-    }
-
-    if (enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
-        if (!ost->frame_rate.num)
-            ost->frame_rate = av_buffersink_get_frame_rate(ost->filter->filter);
-        if (!ost->frame_rate.num && !ost->max_frame_rate.num) {
-            ost->frame_rate = (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(ost->frame_rate) > av_q2d(ost->max_frame_rate) ||
-            !ost->frame_rate.den))
-            ost->frame_rate = ost->max_frame_rate;
-
-        if (enc->supported_framerates && !ost->force_fps) {
-            int idx = av_find_nearest_q_idx(ost->frame_rate, enc->supported_framerates);
-            ost->frame_rate = 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(&ost->frame_rate.num, &ost->frame_rate.den,
-                      ost->frame_rate.num, ost->frame_rate.den, 65535);
-        }
-    }
-
-    switch (enc_ctx->codec_type) {
-    case AVMEDIA_TYPE_AUDIO:
-        enc_ctx->sample_fmt     = av_buffersink_get_format(ost->filter->filter);
-        enc_ctx->sample_rate    = av_buffersink_get_sample_rate(ost->filter->filter);
-        ret = av_buffersink_get_ch_layout(ost->filter->filter, &enc_ctx->ch_layout);
-        if (ret < 0)
-            return ret;
-
-        if (ost->bits_per_raw_sample)
-            enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
-        else if (dec_ctx && ost->filter->graph->is_meta)
-            enc_ctx->bits_per_raw_sample = FFMIN(dec_ctx->bits_per_raw_sample,
-                                                 av_get_bytes_per_sample(enc_ctx->sample_fmt) << 3);
-
-        init_encoder_time_base(ost, av_make_q(1, enc_ctx->sample_rate));
-        break;
-
-    case AVMEDIA_TYPE_VIDEO:
-        init_encoder_time_base(ost, av_inv_q(ost->frame_rate));
-
-        if (!(enc_ctx->time_base.num && enc_ctx->time_base.den))
-            enc_ctx->time_base = av_buffersink_get_time_base(ost->filter->filter);
-        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  = av_buffersink_get_w(ost->filter->filter);
-        enc_ctx->height = av_buffersink_get_h(ost->filter->filter);
-        enc_ctx->sample_aspect_ratio = ost->st->sample_aspect_ratio =
-            ost->frame_aspect_ratio.num ? // overridden by the -aspect cli option
-            av_mul_q(ost->frame_aspect_ratio, (AVRational){ enc_ctx->height, enc_ctx->width }) :
-            av_buffersink_get_sample_aspect_ratio(ost->filter->filter);
-
-        enc_ctx->pix_fmt = av_buffersink_get_format(ost->filter->filter);
-
-        if (ost->bits_per_raw_sample)
-            enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
-        else if (dec_ctx && ost->filter->graph->is_meta)
-            enc_ctx->bits_per_raw_sample = FFMIN(dec_ctx->bits_per_raw_sample,
-                                                 av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth);
-
-        if (frame) {
-            enc_ctx->color_range            = frame->color_range;
-            enc_ctx->color_primaries        = frame->color_primaries;
-            enc_ctx->color_trc              = frame->color_trc;
-            enc_ctx->colorspace             = frame->colorspace;
-            enc_ctx->chroma_sample_location = frame->chroma_location;
-        }
-
-        enc_ctx->framerate = ost->frame_rate;
-
-        ost->st->avg_frame_rate = ost->frame_rate;
-
-        // Field order: autodetection
-        if (frame) {
-            if (enc_ctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME) &&
-                ost->top_field_first >= 0)
-                frame->top_field_first = !!ost->top_field_first;
-
-            if (frame->interlaced_frame) {
-                if (enc->id == AV_CODEC_ID_MJPEG)
-                    enc_ctx->field_order = frame->top_field_first ? AV_FIELD_TT:AV_FIELD_BB;
-                else
-                    enc_ctx->field_order = frame->top_field_first ? AV_FIELD_TB:AV_FIELD_BT;
-            } else
-                enc_ctx->field_order = AV_FIELD_PROGRESSIVE;
-        }
-
-        // Field order: override
-        if (ost->top_field_first == 0) {
-            enc_ctx->field_order = AV_FIELD_BB;
-        } else if (ost->top_field_first == 1) {
-            enc_ctx->field_order = AV_FIELD_TT;
-        }
-
-        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;
-        }
-        if (dec_ctx && dec_ctx->subtitle_header) {
-            /* ASS code assumes this buffer is null terminated so add extra byte. */
-            enc_ctx->subtitle_header = av_mallocz(dec_ctx->subtitle_header_size + 1);
-            if (!enc_ctx->subtitle_header)
-                return AVERROR(ENOMEM);
-            memcpy(enc_ctx->subtitle_header, dec_ctx->subtitle_header,
-                   dec_ctx->subtitle_header_size);
-            enc_ctx->subtitle_header_size = dec_ctx->subtitle_header_size;
-        }
-        if (ist && ist->dec->type == AVMEDIA_TYPE_SUBTITLE &&
-            enc_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
-            int input_props = 0, output_props = 0;
-            AVCodecDescriptor const *input_descriptor =
-                avcodec_descriptor_get(ist->dec->id);
-            AVCodecDescriptor const *output_descriptor =
-                avcodec_descriptor_get(enc_ctx->codec_id);
-            if (input_descriptor)
-                input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
-            if (output_descriptor)
-                output_props = output_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
-            if (input_props && output_props && input_props != output_props) {
-                av_log(ost, AV_LOG_ERROR,
-                       "Subtitle encoding currently only possible from text to text "
-                       "or bitmap to bitmap");
-                return AVERROR_INVALIDDATA;
-            }
-        }
-
-        break;
-    default:
-        abort();
-        break;
-    }
-
-    if (ost->bitexact)
-        enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
-
-    if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
-        av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
-
-    if (enc->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE) {
-        ret = av_dict_set(&ost->encoder_opts, "flags", "+copy_opaque", AV_DICT_MULTIKEY);
-        if (ret < 0)
-            return ret;
-    }
-
-    ret = hw_device_setup_for_encode(ost);
-    if (ret < 0) {
-        av_log(ost, AV_LOG_ERROR,
-               "Encoding hardware device setup failed: %s\n", av_err2str(ret));
-        return ret;
-    }
-
-    if ((ret = avcodec_open2(ost->enc_ctx, enc, &ost->encoder_opts)) < 0) {
-        if (ret == AVERROR_EXPERIMENTAL)
-            abort_codec_experimental(enc, 1);
-        av_log(ost, AV_LOG_ERROR, "Error while opening encoder - maybe "
-               "incorrect parameters such as bit_rate, rate, width or height.\n");
-        return ret;
-    }
-
-    if (ost->enc_ctx->frame_size) {
-        av_assert0(ost->sq_idx_encode >= 0);
-        sq_frame_samples(output_files[ost->file_index]->sq_encode,
-                         ost->sq_idx_encode, ost->enc_ctx->frame_size);
-    }
-
-    assert_avoptions(ost->encoder_opts);
-    if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000 &&
-        ost->enc_ctx->codec_id != AV_CODEC_ID_CODEC2 /* don't complain about 700 bit/s modes */)
-        av_log(ost, AV_LOG_WARNING, "The bitrate parameter is set too low."
-                                    " It takes bits/s as argument, not kbits/s\n");
-
-    ret = avcodec_parameters_from_context(ost->st->codecpar, ost->enc_ctx);
-    if (ret < 0) {
-        av_log(ost, AV_LOG_FATAL,
-               "Error initializing the output stream codec context.\n");
-        exit_program(1);
-    }
-
-    if (ost->enc_ctx->nb_coded_side_data) {
-        int i;
-
-        for (i = 0; i < ost->enc_ctx->nb_coded_side_data; i++) {
-            const AVPacketSideData *sd_src = &ost->enc_ctx->coded_side_data[i];
-            uint8_t *dst_data;
-
-            dst_data = av_stream_new_side_data(ost->st, sd_src->type, sd_src->size);
-            if (!dst_data)
-                return AVERROR(ENOMEM);
-            memcpy(dst_data, sd_src->data, sd_src->size);
-        }
-    }
-
-    /*
-     * Add global input side data. For now this is naive, and copies it
-     * from the input stream's global side data. All side data should
-     * really be funneled over AVFrame and libavfilter, then added back to
-     * packet side data, and then potentially using the first packet for
-     * global side data.
-     */
-    if (ist) {
-        int i;
-        for (i = 0; i < ist->st->nb_side_data; i++) {
-            AVPacketSideData *sd = &ist->st->side_data[i];
-            if (sd->type != AV_PKT_DATA_CPB_PROPERTIES) {
-                uint8_t *dst = av_stream_new_side_data(ost->st, sd->type, sd->size);
-                if (!dst)
-                    return AVERROR(ENOMEM);
-                memcpy(dst, sd->data, sd->size);
-                if (ist->autorotate && sd->type == AV_PKT_DATA_DISPLAYMATRIX)
-                    av_display_rotation_set((int32_t *)dst, 0);
-            }
-        }
-    }
-
-    // copy timebase while removing common factors
-    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});
-
-    // copy estimated duration as a hint to the muxer
-    if (ost->st->duration <= 0 && ist && ist->st->duration > 0)
-        ost->st->duration = av_rescale_q(ist->st->duration, ist->st->time_base, ost->st->time_base);
-
-    ost->mux_timebase = enc_ctx->time_base;
-
-    return 0;
-}
-
 static int init_output_stream(OutputStream *ost, AVFrame *frame)
 {
     int ret = 0;
 
     if (ost->enc_ctx) {
-        ret = init_output_stream_encode(ost, frame);
+        ret = enc_open(ost, frame);
         if (ret < 0)
             return ret;
     } else if (ost->ist) {
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 791deedc07..c1e2bbc300 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -810,6 +810,8 @@ AVBufferRef *hw_device_for_filter(void);
 
 int hwaccel_decode_init(AVCodecContext *avctx);
 
+int enc_open(OutputStream *ost, AVFrame *frame);
+
 /*
  * Initialize muxing state for the given stream, should be called
  * after the codec/streamcopy setup has been done.
diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
new file mode 100644
index 0000000000..9db2ff5ef3
--- /dev/null
+++ b/fftools/ffmpeg_enc.c
@@ -0,0 +1,342 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <math.h>
+#include <stdint.h>
+
+#include "ffmpeg.h"
+
+#include "libavutil/avassert.h"
+#include "libavutil/avstring.h"
+#include "libavutil/avutil.h"
+#include "libavutil/dict.h"
+#include "libavutil/display.h"
+#include "libavutil/eval.h"
+#include "libavutil/frame.h"
+#include "libavutil/log.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/rational.h"
+
+#include "libavfilter/buffersink.h"
+
+#include "libavcodec/avcodec.h"
+
+#include "libavformat/avformat.h"
+
+static void set_encoder_id(OutputFile *of, OutputStream *ost)
+{
+    const char *cname = ost->enc_ctx->codec->name;
+    uint8_t *encoder_string;
+    int encoder_string_len;
+
+    if (av_dict_get(ost->st->metadata, "encoder",  NULL, 0))
+        return;
+
+    encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(cname) + 2;
+    encoder_string     = av_mallocz(encoder_string_len);
+    if (!encoder_string)
+        report_and_exit(AVERROR(ENOMEM));
+
+    if (!of->bitexact && !ost->bitexact)
+        av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len);
+    else
+        av_strlcpy(encoder_string, "Lavc ", encoder_string_len);
+    av_strlcat(encoder_string, cname, encoder_string_len);
+    av_dict_set(&ost->st->metadata, "encoder",  encoder_string,
+                AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
+}
+
+static void init_encoder_time_base(OutputStream *ost, AVRational default_time_base)
+{
+    InputStream *ist = ost->ist;
+    AVCodecContext *enc_ctx = ost->enc_ctx;
+
+    if (ost->enc_timebase.num > 0) {
+        enc_ctx->time_base = ost->enc_timebase;
+        return;
+    }
+
+    if (ost->enc_timebase.num < 0) {
+        if (ist) {
+            enc_ctx->time_base = ist->st->time_base;
+            return;
+        }
+
+        av_log(ost, AV_LOG_WARNING,
+               "Input stream data not available, using default time base\n");
+    }
+
+    enc_ctx->time_base = default_time_base;
+}
+
+int enc_open(OutputStream *ost, AVFrame *frame)
+{
+    InputStream *ist = ost->ist;
+    AVCodecContext *enc_ctx = ost->enc_ctx;
+    AVCodecContext *dec_ctx = NULL;
+    const AVCodec      *enc = enc_ctx->codec;
+    OutputFile      *of = output_files[ost->file_index];
+    int ret;
+
+    set_encoder_id(output_files[ost->file_index], ost);
+
+    if (ist) {
+        dec_ctx = ist->dec_ctx;
+    }
+
+    if (enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
+        if (!ost->frame_rate.num)
+            ost->frame_rate = av_buffersink_get_frame_rate(ost->filter->filter);
+        if (!ost->frame_rate.num && !ost->max_frame_rate.num) {
+            ost->frame_rate = (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(ost->frame_rate) > av_q2d(ost->max_frame_rate) ||
+            !ost->frame_rate.den))
+            ost->frame_rate = ost->max_frame_rate;
+
+        if (enc->supported_framerates && !ost->force_fps) {
+            int idx = av_find_nearest_q_idx(ost->frame_rate, enc->supported_framerates);
+            ost->frame_rate = 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(&ost->frame_rate.num, &ost->frame_rate.den,
+                      ost->frame_rate.num, ost->frame_rate.den, 65535);
+        }
+    }
+
+    switch (enc_ctx->codec_type) {
+    case AVMEDIA_TYPE_AUDIO:
+        enc_ctx->sample_fmt     = av_buffersink_get_format(ost->filter->filter);
+        enc_ctx->sample_rate    = av_buffersink_get_sample_rate(ost->filter->filter);
+        ret = av_buffersink_get_ch_layout(ost->filter->filter, &enc_ctx->ch_layout);
+        if (ret < 0)
+            return ret;
+
+        if (ost->bits_per_raw_sample)
+            enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
+        else if (dec_ctx && ost->filter->graph->is_meta)
+            enc_ctx->bits_per_raw_sample = FFMIN(dec_ctx->bits_per_raw_sample,
+                                                 av_get_bytes_per_sample(enc_ctx->sample_fmt) << 3);
+
+        init_encoder_time_base(ost, av_make_q(1, enc_ctx->sample_rate));
+        break;
+
+    case AVMEDIA_TYPE_VIDEO:
+        init_encoder_time_base(ost, av_inv_q(ost->frame_rate));
+
+        if (!(enc_ctx->time_base.num && enc_ctx->time_base.den))
+            enc_ctx->time_base = av_buffersink_get_time_base(ost->filter->filter);
+        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  = av_buffersink_get_w(ost->filter->filter);
+        enc_ctx->height = av_buffersink_get_h(ost->filter->filter);
+        enc_ctx->sample_aspect_ratio = ost->st->sample_aspect_ratio =
+            ost->frame_aspect_ratio.num ? // overridden by the -aspect cli option
+            av_mul_q(ost->frame_aspect_ratio, (AVRational){ enc_ctx->height, enc_ctx->width }) :
+            av_buffersink_get_sample_aspect_ratio(ost->filter->filter);
+
+        enc_ctx->pix_fmt = av_buffersink_get_format(ost->filter->filter);
+
+        if (ost->bits_per_raw_sample)
+            enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
+        else if (dec_ctx && ost->filter->graph->is_meta)
+            enc_ctx->bits_per_raw_sample = FFMIN(dec_ctx->bits_per_raw_sample,
+                                                 av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth);
+
+        if (frame) {
+            enc_ctx->color_range            = frame->color_range;
+            enc_ctx->color_primaries        = frame->color_primaries;
+            enc_ctx->color_trc              = frame->color_trc;
+            enc_ctx->colorspace             = frame->colorspace;
+            enc_ctx->chroma_sample_location = frame->chroma_location;
+        }
+
+        enc_ctx->framerate = ost->frame_rate;
+
+        ost->st->avg_frame_rate = ost->frame_rate;
+
+        // Field order: autodetection
+        if (frame) {
+            if (enc_ctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME) &&
+                ost->top_field_first >= 0)
+                frame->top_field_first = !!ost->top_field_first;
+
+            if (frame->interlaced_frame) {
+                if (enc->id == AV_CODEC_ID_MJPEG)
+                    enc_ctx->field_order = frame->top_field_first ? AV_FIELD_TT:AV_FIELD_BB;
+                else
+                    enc_ctx->field_order = frame->top_field_first ? AV_FIELD_TB:AV_FIELD_BT;
+            } else
+                enc_ctx->field_order = AV_FIELD_PROGRESSIVE;
+        }
+
+        // Field order: override
+        if (ost->top_field_first == 0) {
+            enc_ctx->field_order = AV_FIELD_BB;
+        } else if (ost->top_field_first == 1) {
+            enc_ctx->field_order = AV_FIELD_TT;
+        }
+
+        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;
+        }
+        if (dec_ctx && dec_ctx->subtitle_header) {
+            /* ASS code assumes this buffer is null terminated so add extra byte. */
+            enc_ctx->subtitle_header = av_mallocz(dec_ctx->subtitle_header_size + 1);
+            if (!enc_ctx->subtitle_header)
+                return AVERROR(ENOMEM);
+            memcpy(enc_ctx->subtitle_header, dec_ctx->subtitle_header,
+                   dec_ctx->subtitle_header_size);
+            enc_ctx->subtitle_header_size = dec_ctx->subtitle_header_size;
+        }
+        if (ist && ist->dec->type == AVMEDIA_TYPE_SUBTITLE &&
+            enc_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
+            int input_props = 0, output_props = 0;
+            AVCodecDescriptor const *input_descriptor =
+                avcodec_descriptor_get(ist->dec->id);
+            AVCodecDescriptor const *output_descriptor =
+                avcodec_descriptor_get(enc_ctx->codec_id);
+            if (input_descriptor)
+                input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
+            if (output_descriptor)
+                output_props = output_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
+            if (input_props && output_props && input_props != output_props) {
+                av_log(ost, AV_LOG_ERROR,
+                       "Subtitle encoding currently only possible from text to text "
+                       "or bitmap to bitmap");
+                return AVERROR_INVALIDDATA;
+            }
+        }
+
+        break;
+    default:
+        abort();
+        break;
+    }
+
+    if (ost->bitexact)
+        enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
+
+    if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
+        av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
+
+    if (enc->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE) {
+        ret = av_dict_set(&ost->encoder_opts, "flags", "+copy_opaque", AV_DICT_MULTIKEY);
+        if (ret < 0)
+            return ret;
+    }
+
+    ret = hw_device_setup_for_encode(ost);
+    if (ret < 0) {
+        av_log(ost, AV_LOG_ERROR,
+               "Encoding hardware device setup failed: %s\n", av_err2str(ret));
+        return ret;
+    }
+
+    if ((ret = avcodec_open2(ost->enc_ctx, enc, &ost->encoder_opts)) < 0) {
+        if (ret != AVERROR_EXPERIMENTAL)
+            av_log(ost, AV_LOG_ERROR, "Error while opening encoder - maybe "
+                   "incorrect parameters such as bit_rate, rate, width or height.\n");
+        return ret;
+    }
+
+    if (ost->enc_ctx->frame_size) {
+        av_assert0(ost->sq_idx_encode >= 0);
+        sq_frame_samples(output_files[ost->file_index]->sq_encode,
+                         ost->sq_idx_encode, ost->enc_ctx->frame_size);
+    }
+
+    assert_avoptions(ost->encoder_opts);
+    if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000 &&
+        ost->enc_ctx->codec_id != AV_CODEC_ID_CODEC2 /* don't complain about 700 bit/s modes */)
+        av_log(ost, AV_LOG_WARNING, "The bitrate parameter is set too low."
+                                    " It takes bits/s as argument, not kbits/s\n");
+
+    ret = avcodec_parameters_from_context(ost->st->codecpar, ost->enc_ctx);
+    if (ret < 0) {
+        av_log(ost, AV_LOG_FATAL,
+               "Error initializing the output stream codec context.\n");
+        exit_program(1);
+    }
+
+    if (ost->enc_ctx->nb_coded_side_data) {
+        int i;
+
+        for (i = 0; i < ost->enc_ctx->nb_coded_side_data; i++) {
+            const AVPacketSideData *sd_src = &ost->enc_ctx->coded_side_data[i];
+            uint8_t *dst_data;
+
+            dst_data = av_stream_new_side_data(ost->st, sd_src->type, sd_src->size);
+            if (!dst_data)
+                return AVERROR(ENOMEM);
+            memcpy(dst_data, sd_src->data, sd_src->size);
+        }
+    }
+
+    /*
+     * Add global input side data. For now this is naive, and copies it
+     * from the input stream's global side data. All side data should
+     * really be funneled over AVFrame and libavfilter, then added back to
+     * packet side data, and then potentially using the first packet for
+     * global side data.
+     */
+    if (ist) {
+        int i;
+        for (i = 0; i < ist->st->nb_side_data; i++) {
+            AVPacketSideData *sd = &ist->st->side_data[i];
+            if (sd->type != AV_PKT_DATA_CPB_PROPERTIES) {
+                uint8_t *dst = av_stream_new_side_data(ost->st, sd->type, sd->size);
+                if (!dst)
+                    return AVERROR(ENOMEM);
+                memcpy(dst, sd->data, sd->size);
+                if (ist->autorotate && sd->type == AV_PKT_DATA_DISPLAYMATRIX)
+                    av_display_rotation_set((int32_t *)dst, 0);
+            }
+        }
+    }
+
+    // copy timebase while removing common factors
+    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});
+
+    // copy estimated duration as a hint to the muxer
+    if (ost->st->duration <= 0 && ist && ist->st->duration > 0)
+        ost->st->duration = av_rescale_q(ist->st->duration, ist->st->time_base, ost->st->time_base);
+
+    ost->mux_timebase = enc_ctx->time_base;
+
+    return 0;
+}
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 15/23] fftools/ffmpeg: simplify output stream initialization call graph
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
                   ` (12 preceding siblings ...)
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 14/23] fftools/ffmpeg: move initializing encoders to a new file Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 16/23] fftools/ffmpeg: replace ff_dlog() with av_log() Anton Khirnov
                   ` (7 subsequent siblings)
  21 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

Several places in the code currently call init_output_stream_wrapper(),
which in turn calls init_output_stream(), which then calls either
enc_init() or init_output_stream_streamcopy(), followed by
of_stream_init(), which tells the muxer the stream is ready for muxing.

All except one of these callers are in the encoding code, which will be
moved to ffmpeg_enc.c. Keeping this structure would then necessitate
ffmpeg_enc.c calling back into the common code in ffmpeg.c, which would
then just call ffmpeg_mux, thus making the already convoluted call chain
even more so.

Simplify the situation by using separate paths for filter-fed output
streams (audio and video encoders) and others (subtitles, streamcopy,
data, attachments).
---
 fftools/ffmpeg.c     | 51 +++++++++++++++++---------------------------
 fftools/ffmpeg_enc.c |  7 ++++++
 2 files changed, 27 insertions(+), 31 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 03e5391970..1969ce9295 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -724,25 +724,6 @@ early_exit:
     return float_pts;
 }
 
-static int init_output_stream(OutputStream *ost, AVFrame *frame);
-
-static int init_output_stream_wrapper(OutputStream *ost, AVFrame *frame,
-                                      unsigned int fatal)
-{
-    int ret = AVERROR_BUG;
-
-    if (ost->initialized)
-        return 0;
-
-    ret = init_output_stream(ost, frame);
-    if (ret < 0) {
-        if (fatal)
-            exit_program(1);
-    }
-
-    return ret;
-}
-
 static double psnr(double d)
 {
     return -10.0 * log10(d);
@@ -1023,7 +1004,9 @@ static void do_audio_out(OutputFile *of, OutputStream *ost,
     AVCodecContext *enc = ost->enc_ctx;
     int ret;
 
-    init_output_stream_wrapper(ost, frame, 1);
+    ret = enc_open(ost, frame);
+    if (ret < 0)
+        exit_program(1);
 
     if (frame->pts == AV_NOPTS_VALUE)
         frame->pts = ost->next_pts;
@@ -1264,7 +1247,9 @@ static void do_video_out(OutputFile *of,
     InputStream *ist = ost->ist;
     AVFilterContext *filter = ost->filter->filter;
 
-    init_output_stream_wrapper(ost, next_picture, 1);
+    ret = enc_open(ost, next_picture);
+    if (ret < 0)
+        exit_program(1);
 
     frame_rate = av_buffersink_get_frame_rate(filter);
     if (frame_rate.num > 0 && frame_rate.den > 0)
@@ -1820,7 +1805,9 @@ static void flush_encoders(void)
                 of_output_packet(of, ost->pkt, ost, 1);
             }
 
-            init_output_stream_wrapper(ost, NULL, 1);
+            ret = enc_open(ost, NULL);
+            if (ret < 0)
+                exit_program(1);
         }
 
         if (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO)
@@ -2967,24 +2954,26 @@ static int init_output_stream_streamcopy(OutputStream *ost)
     return 0;
 }
 
-static int init_output_stream(OutputStream *ost, AVFrame *frame)
+static int init_output_stream_nofilter(OutputStream *ost)
 {
     int ret = 0;
 
     if (ost->enc_ctx) {
-        ret = enc_open(ost, frame);
+        ret = enc_open(ost, NULL);
         if (ret < 0)
             return ret;
-    } else if (ost->ist) {
-        ret = init_output_stream_streamcopy(ost);
+    } else {
+        if (ost->ist) {
+            ret = init_output_stream_streamcopy(ost);
+            if (ret < 0)
+                return ret;
+        }
+
+        ret = of_stream_init(output_files[ost->file_index], ost);
         if (ret < 0)
             return ret;
     }
 
-    ret = of_stream_init(output_files[ost->file_index], ost);
-    if (ret < 0)
-        return ret;
-
     return ret;
 }
 
@@ -3017,7 +3006,7 @@ static int transcode_init(void)
              ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))
             continue;
 
-        ret = init_output_stream_wrapper(ost, NULL, 0);
+        ret = init_output_stream_nofilter(ost);
         if (ret < 0)
             goto dump_format;
     }
diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index 9db2ff5ef3..a4660051a2 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -93,6 +93,9 @@ int enc_open(OutputStream *ost, AVFrame *frame)
     OutputFile      *of = output_files[ost->file_index];
     int ret;
 
+    if (ost->initialized)
+        return 0;
+
     set_encoder_id(output_files[ost->file_index], ost);
 
     if (ist) {
@@ -338,5 +341,9 @@ int enc_open(OutputStream *ost, AVFrame *frame)
 
     ost->mux_timebase = enc_ctx->time_base;
 
+    ret = of_stream_init(of, ost);
+    if (ret < 0)
+        return ret;
+
     return 0;
 }
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 16/23] fftools/ffmpeg: replace ff_dlog() with av_log()
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
                   ` (13 preceding siblings ...)
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 15/23] fftools/ffmpeg: simplify output stream initialization call graph Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 17/23] fftools/ffmpeg: move subtitle encoding to ffmpeg_enc.c Anton Khirnov
                   ` (6 subsequent siblings)
  21 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

ff_dlog() is a lavu internal macro, and av_log() at trace level is just
as good here.
---
 fftools/ffmpeg.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 1969ce9295..0324e45afa 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -49,7 +49,6 @@
 #include "libavutil/samplefmt.h"
 #include "libavutil/fifo.h"
 #include "libavutil/hwcontext.h"
-#include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/dict.h"
 #include "libavutil/display.h"
@@ -1202,13 +1201,14 @@ static enum AVPictureType forced_kf_apply(void *logctx, KeyframeForceCtx *kf,
         kf->expr_const_values[FKF_T] = pts_time;
         res = av_expr_eval(kf->pexpr,
                            kf->expr_const_values, NULL);
-        ff_dlog(NULL, "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
-                kf->expr_const_values[FKF_N],
-                kf->expr_const_values[FKF_N_FORCED],
-                kf->expr_const_values[FKF_PREV_FORCED_N],
-                kf->expr_const_values[FKF_T],
-                kf->expr_const_values[FKF_PREV_FORCED_T],
-                res);
+        av_log(logctx, AV_LOG_TRACE,
+               "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
+               kf->expr_const_values[FKF_N],
+               kf->expr_const_values[FKF_N_FORCED],
+               kf->expr_const_values[FKF_PREV_FORCED_N],
+               kf->expr_const_values[FKF_T],
+               kf->expr_const_values[FKF_PREV_FORCED_T],
+               res);
 
         kf->expr_const_values[FKF_N] += 1;
 
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 17/23] fftools/ffmpeg: move subtitle encoding to ffmpeg_enc.c
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
                   ` (14 preceding siblings ...)
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 16/23] fftools/ffmpeg: replace ff_dlog() with av_log() Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 18/23] fftools/ffmpeg: move audio/video encoding code " Anton Khirnov
                   ` (5 subsequent siblings)
  21 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

---
 fftools/ffmpeg.c     | 86 ++------------------------------------------
 fftools/ffmpeg.h     |  3 ++
 fftools/ffmpeg_enc.c | 80 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 85 insertions(+), 84 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 0324e45afa..bc2a9efbc1 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -669,7 +669,7 @@ static void close_output_stream(OutputStream *ost)
         sq_send(of->sq_encode, ost->sq_idx_encode, SQFRAME(NULL));
 }
 
-static int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb)
+int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb)
 {
     OutputFile *of = output_files[ost->file_index];
 
@@ -1027,88 +1027,6 @@ static void do_audio_out(OutputFile *of, OutputStream *ost,
         exit_program(1);
 }
 
-static void do_subtitle_out(OutputFile *of,
-                            OutputStream *ost,
-                            AVSubtitle *sub)
-{
-    int subtitle_out_max_size = 1024 * 1024;
-    int subtitle_out_size, nb, i, ret;
-    AVCodecContext *enc;
-    AVPacket *pkt = ost->pkt;
-    int64_t pts;
-
-    if (sub->pts == AV_NOPTS_VALUE) {
-        av_log(ost, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
-        if (exit_on_error)
-            exit_program(1);
-        return;
-    }
-
-    enc = ost->enc_ctx;
-
-    /* Note: DVB subtitle need one packet to draw them and one other
-       packet to clear them */
-    /* XXX: signal it in the codec context ? */
-    if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
-        nb = 2;
-    else if (enc->codec_id == AV_CODEC_ID_ASS)
-        nb = FFMAX(sub->num_rects, 1);
-    else
-        nb = 1;
-
-    /* shift timestamp to honor -ss and make check_recording_time() work with -t */
-    pts = sub->pts;
-    if (output_files[ost->file_index]->start_time != AV_NOPTS_VALUE)
-        pts -= output_files[ost->file_index]->start_time;
-    for (i = 0; i < nb; i++) {
-        AVSubtitle local_sub = *sub;
-
-        if (!check_recording_time(ost, pts, AV_TIME_BASE_Q))
-            return;
-
-        ret = av_new_packet(pkt, subtitle_out_max_size);
-        if (ret < 0)
-            report_and_exit(AVERROR(ENOMEM));
-
-        local_sub.pts = pts;
-        // start_display_time is required to be 0
-        local_sub.pts               += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
-        local_sub.end_display_time  -= sub->start_display_time;
-        local_sub.start_display_time = 0;
-
-        if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE && i == 1)
-            local_sub.num_rects = 0;
-        else if (enc->codec_id == AV_CODEC_ID_ASS && sub->num_rects > 0) {
-            local_sub.num_rects = 1;
-            local_sub.rects += i;
-        }
-
-        ost->frames_encoded++;
-
-        subtitle_out_size = avcodec_encode_subtitle(enc, pkt->data, pkt->size, &local_sub);
-        if (subtitle_out_size < 0) {
-            av_log(ost, AV_LOG_FATAL, "Subtitle encoding failed\n");
-            exit_program(1);
-        }
-
-        av_shrink_packet(pkt, subtitle_out_size);
-        pkt->time_base = ost->mux_timebase;
-        pkt->pts  = av_rescale_q(sub->pts, AV_TIME_BASE_Q, pkt->time_base);
-        pkt->duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
-        if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
-            /* XXX: the pts correction is handled here. Maybe handling
-               it in the codec would be better */
-            if (i == 0)
-                pkt->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
-            else
-                pkt->pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
-        }
-        pkt->dts = pkt->pts;
-
-        of_output_packet(of, pkt, ost, 0);
-    }
-}
-
 /* Convert frame timestamps to the encoder timebase and decide how many times
  * should this (and possibly previous) frame be repeated in order to conform to
  * desired target framerate (if any).
@@ -2351,7 +2269,7 @@ static int process_subtitle(InputStream *ist, AVSubtitle *subtitle, int *got_out
             || ost->enc_ctx->codec_type != AVMEDIA_TYPE_SUBTITLE)
             continue;
 
-        do_subtitle_out(output_files[ost->file_index], ost, subtitle);
+        enc_subtitle(output_files[ost->file_index], ost, subtitle);
     }
 
 out:
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index c1e2bbc300..ffb0ca33ac 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -811,6 +811,9 @@ AVBufferRef *hw_device_for_filter(void);
 int hwaccel_decode_init(AVCodecContext *avctx);
 
 int enc_open(OutputStream *ost, AVFrame *frame);
+void enc_subtitle(OutputFile *of, OutputStream *ost, AVSubtitle *sub);
+
+int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb);
 
 /*
  * Initialize muxing state for the given stream, should be called
diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index a4660051a2..bcc560b9b7 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -347,3 +347,83 @@ int enc_open(OutputStream *ost, AVFrame *frame)
 
     return 0;
 }
+
+void enc_subtitle(OutputFile *of, OutputStream *ost, AVSubtitle *sub)
+{
+    int subtitle_out_max_size = 1024 * 1024;
+    int subtitle_out_size, nb, i, ret;
+    AVCodecContext *enc;
+    AVPacket *pkt = ost->pkt;
+    int64_t pts;
+
+    if (sub->pts == AV_NOPTS_VALUE) {
+        av_log(ost, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
+        if (exit_on_error)
+            exit_program(1);
+        return;
+    }
+
+    enc = ost->enc_ctx;
+
+    /* Note: DVB subtitle need one packet to draw them and one other
+       packet to clear them */
+    /* XXX: signal it in the codec context ? */
+    if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
+        nb = 2;
+    else if (enc->codec_id == AV_CODEC_ID_ASS)
+        nb = FFMAX(sub->num_rects, 1);
+    else
+        nb = 1;
+
+    /* shift timestamp to honor -ss and make check_recording_time() work with -t */
+    pts = sub->pts;
+    if (output_files[ost->file_index]->start_time != AV_NOPTS_VALUE)
+        pts -= output_files[ost->file_index]->start_time;
+    for (i = 0; i < nb; i++) {
+        AVSubtitle local_sub = *sub;
+
+        if (!check_recording_time(ost, pts, AV_TIME_BASE_Q))
+            return;
+
+        ret = av_new_packet(pkt, subtitle_out_max_size);
+        if (ret < 0)
+            report_and_exit(AVERROR(ENOMEM));
+
+        local_sub.pts = pts;
+        // start_display_time is required to be 0
+        local_sub.pts               += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
+        local_sub.end_display_time  -= sub->start_display_time;
+        local_sub.start_display_time = 0;
+
+        if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE && i == 1)
+            local_sub.num_rects = 0;
+        else if (enc->codec_id == AV_CODEC_ID_ASS && sub->num_rects > 0) {
+            local_sub.num_rects = 1;
+            local_sub.rects += i;
+        }
+
+        ost->frames_encoded++;
+
+        subtitle_out_size = avcodec_encode_subtitle(enc, pkt->data, pkt->size, &local_sub);
+        if (subtitle_out_size < 0) {
+            av_log(ost, AV_LOG_FATAL, "Subtitle encoding failed\n");
+            exit_program(1);
+        }
+
+        av_shrink_packet(pkt, subtitle_out_size);
+        pkt->time_base = ost->mux_timebase;
+        pkt->pts  = av_rescale_q(sub->pts, AV_TIME_BASE_Q, pkt->time_base);
+        pkt->duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
+        if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
+            /* XXX: the pts correction is handled here. Maybe handling
+               it in the codec would be better */
+            if (i == 0)
+                pkt->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
+            else
+                pkt->pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
+        }
+        pkt->dts = pkt->pts;
+
+        of_output_packet(of, pkt, ost, 0);
+    }
+}
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 18/23] fftools/ffmpeg: move audio/video encoding code to ffmpeg_enc.c
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
                   ` (15 preceding siblings ...)
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 17/23] fftools/ffmpeg: move subtitle encoding to ffmpeg_enc.c Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 19/23] fftools/ffmpeg: add encoder private data Anton Khirnov
                   ` (4 subsequent siblings)
  21 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

---
 fftools/ffmpeg.c     | 690 +------------------------------------------
 fftools/ffmpeg.h     |  31 +-
 fftools/ffmpeg_enc.c | 660 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 702 insertions(+), 679 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index bc2a9efbc1..7a6b206d11 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -62,7 +62,6 @@
 #include "libavutil/time.h"
 #include "libavutil/thread.h"
 #include "libavutil/threadmessage.h"
-#include "libavcodec/mathops.h"
 #include "libavcodec/version.h"
 #include "libavformat/os_support.h"
 
@@ -110,14 +109,7 @@
 const char program_name[] = "ffmpeg";
 const int program_birth_year = 2000;
 
-static FILE *vstats_file;
-
-// optionally attached as opaque_ref to decoded AVFrames
-typedef struct FrameData {
-    uint64_t   idx;
-    int64_t    pts;
-    AVRational tb;
-} FrameData;
+FILE *vstats_file;
 
 typedef struct BenchmarkTimeStamps {
     int64_t real_usec;
@@ -125,14 +117,11 @@ typedef struct BenchmarkTimeStamps {
     int64_t sys_usec;
 } BenchmarkTimeStamps;
 
-static int trigger_fix_sub_duration_heartbeat(OutputStream *ost, const AVPacket *pkt);
 static BenchmarkTimeStamps get_benchmark_time_stamps(void);
 static int64_t getmaxrss(void);
-static int ifilter_has_all_input_formats(FilterGraph *fg);
 
-static int64_t nb_frames_dup = 0;
-static uint64_t dup_warning = 1000;
-static int64_t nb_frames_drop = 0;
+int64_t nb_frames_dup = 0;
+int64_t nb_frames_drop = 0;
 static int64_t decode_error_stat[2];
 unsigned nb_output_dumped = 0;
 
@@ -582,9 +571,7 @@ static void ffmpeg_cleanup(int ret)
     ffmpeg_exited = 1;
 }
 
-/* iterate over all output streams in all output files;
- * pass NULL to start iteration */
-static OutputStream *ost_iter(OutputStream *prev)
+OutputStream *ost_iter(OutputStream *prev)
 {
     int of_idx  = prev ? prev->file_index : 0;
     int ost_idx = prev ? prev->index + 1  : 0;
@@ -639,7 +626,7 @@ static void abort_codec_experimental(const AVCodec *c, int encoder)
     exit_program(1);
 }
 
-static void update_benchmark(const char *fmt, ...)
+void update_benchmark(const char *fmt, ...)
 {
     if (do_benchmark_all) {
         BenchmarkTimeStamps t = get_benchmark_time_stamps();
@@ -660,7 +647,7 @@ static void update_benchmark(const char *fmt, ...)
     }
 }
 
-static void close_output_stream(OutputStream *ost)
+void close_output_stream(OutputStream *ost)
 {
     OutputFile *of = output_files[ost->file_index];
     ost->finished |= ENCODER_FINISHED;
@@ -669,594 +656,6 @@ static void close_output_stream(OutputStream *ost)
         sq_send(of->sq_encode, ost->sq_idx_encode, SQFRAME(NULL));
 }
 
-int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb)
-{
-    OutputFile *of = output_files[ost->file_index];
-
-    if (of->recording_time != INT64_MAX &&
-        av_compare_ts(ts, tb, of->recording_time, AV_TIME_BASE_Q) >= 0) {
-        close_output_stream(ost);
-        return 0;
-    }
-    return 1;
-}
-
-static double adjust_frame_pts_to_encoder_tb(OutputFile *of, OutputStream *ost,
-                                             AVFrame *frame)
-{
-    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 filter_tb = frame->time_base;
-    const int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16);
-
-    if (frame->pts == AV_NOPTS_VALUE)
-        goto early_exit;
-
-    tb.den <<= extra_bits;
-    float_pts = av_rescale_q(frame->pts, filter_tb, tb) -
-                av_rescale_q(start_time, AV_TIME_BASE_Q, tb);
-    float_pts /= 1 << extra_bits;
-    // avoid exact midoints to reduce the chance of rounding differences, this
-    // can be removed in case the fps code is changed to work with integers
-    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;
-
-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);
-    }
-
-    return float_pts;
-}
-
-static double psnr(double d)
-{
-    return -10.0 * log10(d);
-}
-
-static void update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats)
-{
-    const uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
-                                                NULL);
-    AVCodecContext *enc = ost->enc_ctx;
-    int64_t frame_number;
-    double ti1, bitrate, avg_bitrate;
-
-    ost->quality   = sd ? AV_RL32(sd) : -1;
-    ost->pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
-
-    for (int i = 0; i<FF_ARRAY_ELEMS(ost->error); i++) {
-        if (sd && i < sd[5])
-            ost->error[i] = AV_RL64(sd + 8 + 8*i);
-        else
-            ost->error[i] = -1;
-    }
-
-    if (!write_vstats)
-        return;
-
-    /* this is executed just the first time update_video_stats is called */
-    if (!vstats_file) {
-        vstats_file = fopen(vstats_filename, "w");
-        if (!vstats_file) {
-            perror("fopen");
-            exit_program(1);
-        }
-    }
-
-    frame_number = ost->packets_encoded;
-    if (vstats_version <= 1) {
-        fprintf(vstats_file, "frame= %5"PRId64" q= %2.1f ", frame_number,
-                ost->quality / (float)FF_QP2LAMBDA);
-    } else  {
-        fprintf(vstats_file, "out= %2d st= %2d frame= %5"PRId64" q= %2.1f ", ost->file_index, ost->index, frame_number,
-                ost->quality / (float)FF_QP2LAMBDA);
-    }
-
-    if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR))
-        fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
-
-    fprintf(vstats_file,"f_size= %6d ", pkt->size);
-    /* compute pts value */
-    ti1 = pkt->dts * av_q2d(pkt->time_base);
-    if (ti1 < 0.01)
-        ti1 = 0.01;
-
-    bitrate     = (pkt->size * 8) / av_q2d(enc->time_base) / 1000.0;
-    avg_bitrate = (double)(ost->data_size_enc * 8) / ti1 / 1000.0;
-    fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
-           (double)ost->data_size_enc / 1024, ti1, bitrate, avg_bitrate);
-    fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(ost->pict_type));
-}
-
-void enc_stats_write(OutputStream *ost, EncStats *es,
-                     const AVFrame *frame, const AVPacket *pkt,
-                     uint64_t frame_num)
-{
-    AVIOContext *io = es->io;
-    AVRational   tb = frame ? frame->time_base : pkt->time_base;
-    int64_t     pts = frame ? frame->pts : pkt->pts;
-
-    AVRational  tbi = (AVRational){ 0, 1};
-    int64_t    ptsi = INT64_MAX;
-
-    const FrameData *fd;
-
-    if ((frame && frame->opaque_ref) || (pkt && pkt->opaque_ref)) {
-        fd   = (const FrameData*)(frame ? frame->opaque_ref->data : pkt->opaque_ref->data);
-        tbi  = fd->tb;
-        ptsi = fd->pts;
-    }
-
-    for (size_t i = 0; i < es->nb_components; i++) {
-        const EncStatsComponent *c = &es->components[i];
-
-        switch (c->type) {
-        case ENC_STATS_LITERAL:         avio_write (io, c->str,     c->str_len);                    continue;
-        case ENC_STATS_FILE_IDX:        avio_printf(io, "%d",       ost->file_index);               continue;
-        case ENC_STATS_STREAM_IDX:      avio_printf(io, "%d",       ost->index);                    continue;
-        case ENC_STATS_TIMEBASE:        avio_printf(io, "%d/%d",    tb.num, tb.den);                continue;
-        case ENC_STATS_TIMEBASE_IN:     avio_printf(io, "%d/%d",    tbi.num, tbi.den);              continue;
-        case ENC_STATS_PTS:             avio_printf(io, "%"PRId64,  pts);                           continue;
-        case ENC_STATS_PTS_IN:          avio_printf(io, "%"PRId64,  ptsi);                          continue;
-        case ENC_STATS_PTS_TIME:        avio_printf(io, "%g",       pts * av_q2d(tb));              continue;
-        case ENC_STATS_PTS_TIME_IN:     avio_printf(io, "%g",       ptsi == INT64_MAX ?
-                                                                    INFINITY : ptsi * av_q2d(tbi)); continue;
-        case ENC_STATS_FRAME_NUM:       avio_printf(io, "%"PRIu64,  frame_num);                     continue;
-        case ENC_STATS_FRAME_NUM_IN:    avio_printf(io, "%"PRIu64,  fd ? fd->idx : -1);             continue;
-        }
-
-        if (frame) {
-            switch (c->type) {
-            case ENC_STATS_SAMPLE_NUM:  avio_printf(io, "%"PRIu64,  ost->samples_encoded);          continue;
-            case ENC_STATS_NB_SAMPLES:  avio_printf(io, "%d",       frame->nb_samples);             continue;
-            default: av_assert0(0);
-            }
-        } else {
-            switch (c->type) {
-            case ENC_STATS_DTS:         avio_printf(io, "%"PRId64,  pkt->dts);                      continue;
-            case ENC_STATS_DTS_TIME:    avio_printf(io, "%g",       pkt->dts * av_q2d(tb));         continue;
-            case ENC_STATS_PKT_SIZE:    avio_printf(io, "%d",       pkt->size);                     continue;
-            case ENC_STATS_BITRATE: {
-                double duration = FFMAX(pkt->duration, 1) * av_q2d(tb);
-                avio_printf(io, "%g",  8.0 * pkt->size / duration);
-                continue;
-            }
-            case ENC_STATS_AVG_BITRATE: {
-                double duration = pkt->dts * av_q2d(tb);
-                avio_printf(io, "%g",  duration > 0 ? 8.0 * ost->data_size_enc / duration : -1.);
-                continue;
-            }
-            default: av_assert0(0);
-            }
-        }
-    }
-    avio_w8(io, '\n');
-    avio_flush(io);
-}
-
-static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
-{
-    AVCodecContext   *enc = ost->enc_ctx;
-    AVPacket         *pkt = ost->pkt;
-    const char *type_desc = av_get_media_type_string(enc->codec_type);
-    const char    *action = frame ? "encode" : "flush";
-    int ret;
-
-    if (frame) {
-        if (ost->enc_stats_pre.io)
-            enc_stats_write(ost, &ost->enc_stats_pre, frame, NULL,
-                            ost->frames_encoded);
-
-        ost->frames_encoded++;
-        ost->samples_encoded += frame->nb_samples;
-
-        if (debug_ts) {
-            av_log(ost, AV_LOG_INFO, "encoder <- type:%s "
-                   "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
-                   type_desc,
-                   av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
-                   enc->time_base.num, enc->time_base.den);
-        }
-    }
-
-    update_benchmark(NULL);
-
-    ret = avcodec_send_frame(enc, frame);
-    if (ret < 0 && !(ret == AVERROR_EOF && !frame)) {
-        av_log(ost, AV_LOG_ERROR, "Error submitting %s frame to the encoder\n",
-               type_desc);
-        return ret;
-    }
-
-    while (1) {
-        ret = avcodec_receive_packet(enc, pkt);
-        update_benchmark("%s_%s %d.%d", action, type_desc,
-                         ost->file_index, ost->index);
-
-        pkt->time_base = enc->time_base;
-
-        /* if two pass, output log on success and EOF */
-        if ((ret >= 0 || ret == AVERROR_EOF) && ost->logfile && enc->stats_out)
-            fprintf(ost->logfile, "%s", enc->stats_out);
-
-        if (ret == AVERROR(EAGAIN)) {
-            av_assert0(frame); // should never happen during flushing
-            return 0;
-        } else if (ret == AVERROR_EOF) {
-            of_output_packet(of, pkt, ost, 1);
-            return ret;
-        } else if (ret < 0) {
-            av_log(ost, AV_LOG_ERROR, "%s encoding failed\n", type_desc);
-            return ret;
-        }
-
-        if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
-            update_video_stats(ost, pkt, !!vstats_filename);
-        if (ost->enc_stats_post.io)
-            enc_stats_write(ost, &ost->enc_stats_post, NULL, pkt,
-                            ost->packets_encoded);
-
-        if (debug_ts) {
-            av_log(ost, AV_LOG_INFO, "encoder -> type:%s "
-                   "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
-                   "duration:%s duration_time:%s\n",
-                   type_desc,
-                   av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
-                   av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base),
-                   av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base));
-        }
-
-        av_packet_rescale_ts(pkt, pkt->time_base, ost->mux_timebase);
-        pkt->time_base = ost->mux_timebase;
-
-        if (debug_ts) {
-            av_log(ost, AV_LOG_INFO, "encoder -> type:%s "
-                   "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
-                   "duration:%s duration_time:%s\n",
-                   type_desc,
-                   av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
-                   av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base),
-                   av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base));
-        }
-
-        if ((ret = trigger_fix_sub_duration_heartbeat(ost, pkt)) < 0) {
-            av_log(NULL, AV_LOG_ERROR,
-                   "Subtitle heartbeat logic failed in %s! (%s)\n",
-                   __func__, av_err2str(ret));
-            exit_program(1);
-        }
-
-        ost->data_size_enc += pkt->size;
-
-        ost->packets_encoded++;
-
-        of_output_packet(of, pkt, ost, 0);
-    }
-
-    av_assert0(0);
-}
-
-static int submit_encode_frame(OutputFile *of, OutputStream *ost,
-                               AVFrame *frame)
-{
-    int ret;
-
-    if (ost->sq_idx_encode < 0)
-        return encode_frame(of, ost, frame);
-
-    if (frame) {
-        ret = av_frame_ref(ost->sq_frame, frame);
-        if (ret < 0)
-            return ret;
-        frame = ost->sq_frame;
-    }
-
-    ret = sq_send(of->sq_encode, ost->sq_idx_encode,
-                  SQFRAME(frame));
-    if (ret < 0) {
-        if (frame)
-            av_frame_unref(frame);
-        if (ret != AVERROR_EOF)
-            return ret;
-    }
-
-    while (1) {
-        AVFrame *enc_frame = ost->sq_frame;
-
-        ret = sq_receive(of->sq_encode, ost->sq_idx_encode,
-                               SQFRAME(enc_frame));
-        if (ret == AVERROR_EOF) {
-            enc_frame = NULL;
-        } else if (ret < 0) {
-            return (ret == AVERROR(EAGAIN)) ? 0 : ret;
-        }
-
-        ret = encode_frame(of, ost, enc_frame);
-        if (enc_frame)
-            av_frame_unref(enc_frame);
-        if (ret < 0) {
-            if (ret == AVERROR_EOF)
-                close_output_stream(ost);
-            return ret;
-        }
-    }
-}
-
-static void do_audio_out(OutputFile *of, OutputStream *ost,
-                         AVFrame *frame)
-{
-    AVCodecContext *enc = ost->enc_ctx;
-    int ret;
-
-    ret = enc_open(ost, frame);
-    if (ret < 0)
-        exit_program(1);
-
-    if (frame->pts == AV_NOPTS_VALUE)
-        frame->pts = ost->next_pts;
-    else {
-        int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
-        frame->pts =
-            av_rescale_q(frame->pts, frame->time_base, enc->time_base) -
-            av_rescale_q(start_time, AV_TIME_BASE_Q,   enc->time_base);
-    }
-    frame->time_base = enc->time_base;
-
-    if (!check_recording_time(ost, frame->pts, frame->time_base))
-        return;
-
-    ost->next_pts = frame->pts + frame->nb_samples;
-
-    ret = submit_encode_frame(of, ost, frame);
-    if (ret < 0 && ret != AVERROR_EOF)
-        exit_program(1);
-}
-
-/* Convert frame timestamps to the encoder timebase and decide how many times
- * 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 *next_picture, double duration,
-                               int64_t *nb_frames, int64_t *nb_frames_prev)
-{
-    double delta0, delta;
-
-    double sync_ipts = adjust_frame_pts_to_encoder_tb(of, ost, next_picture);
-    /* delta0 is the "drift" between the input frame (next_picture) and
-     * where it would fall in the output. */
-    delta0 = sync_ipts - ost->next_pts;
-    delta  = delta0 + duration;
-
-    // tracks the number of times the PREVIOUS frame should be duplicated,
-    // mostly for variable framerate (VFR)
-    *nb_frames_prev = 0;
-    /* by default, we output a single frame */
-    *nb_frames = 1;
-
-    if (delta0 < 0 &&
-        delta > 0 &&
-        ost->vsync_method != VSYNC_PASSTHROUGH &&
-        ost->vsync_method != VSYNC_DROP) {
-        if (delta0 < -0.6) {
-            av_log(ost, AV_LOG_VERBOSE, "Past duration %f too large\n", -delta0);
-        } else
-            av_log(ost, AV_LOG_DEBUG, "Clipping frame in rate conversion by %f\n", -delta0);
-        sync_ipts = ost->next_pts;
-        duration += delta0;
-        delta0 = 0;
-    }
-
-    switch (ost->vsync_method) {
-    case VSYNC_VSCFR:
-        if (ost->vsync_frame_number == 0 && delta0 >= 0.5) {
-            av_log(ost, AV_LOG_DEBUG, "Not duplicating %d initial frames\n", (int)lrintf(delta0));
-            delta = duration;
-            delta0 = 0;
-            ost->next_pts = llrint(sync_ipts);
-        }
-    case VSYNC_CFR:
-        // FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c
-        if (frame_drop_threshold && delta < frame_drop_threshold && ost->vsync_frame_number) {
-            *nb_frames = 0;
-        } else if (delta < -1.1)
-            *nb_frames = 0;
-        else if (delta > 1.1) {
-            *nb_frames = llrintf(delta);
-            if (delta0 > 1.1)
-                *nb_frames_prev = llrintf(delta0 - 0.6);
-        }
-        next_picture->duration = 1;
-        break;
-    case VSYNC_VFR:
-        if (delta <= -0.6)
-            *nb_frames = 0;
-        else if (delta > 0.6)
-            ost->next_pts = llrint(sync_ipts);
-        next_picture->duration = duration;
-        break;
-    case VSYNC_DROP:
-    case VSYNC_PASSTHROUGH:
-        next_picture->duration = duration;
-        ost->next_pts = llrint(sync_ipts);
-        break;
-    default:
-        av_assert0(0);
-    }
-}
-
-static enum AVPictureType forced_kf_apply(void *logctx, KeyframeForceCtx *kf,
-                                          AVRational tb, const AVFrame *in_picture,
-                                          int dup_idx)
-{
-    double pts_time;
-
-    if (kf->ref_pts == AV_NOPTS_VALUE)
-        kf->ref_pts = in_picture->pts;
-
-    pts_time = (in_picture->pts - kf->ref_pts) * av_q2d(tb);
-    if (kf->index < kf->nb_pts &&
-        av_compare_ts(in_picture->pts, tb, kf->pts[kf->index], AV_TIME_BASE_Q) >= 0) {
-        kf->index++;
-        goto force_keyframe;
-    } else if (kf->pexpr) {
-        double res;
-        kf->expr_const_values[FKF_T] = pts_time;
-        res = av_expr_eval(kf->pexpr,
-                           kf->expr_const_values, NULL);
-        av_log(logctx, AV_LOG_TRACE,
-               "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
-               kf->expr_const_values[FKF_N],
-               kf->expr_const_values[FKF_N_FORCED],
-               kf->expr_const_values[FKF_PREV_FORCED_N],
-               kf->expr_const_values[FKF_T],
-               kf->expr_const_values[FKF_PREV_FORCED_T],
-               res);
-
-        kf->expr_const_values[FKF_N] += 1;
-
-        if (res) {
-            kf->expr_const_values[FKF_PREV_FORCED_N] = kf->expr_const_values[FKF_N] - 1;
-            kf->expr_const_values[FKF_PREV_FORCED_T] = kf->expr_const_values[FKF_T];
-            kf->expr_const_values[FKF_N_FORCED]     += 1;
-            goto force_keyframe;
-        }
-    } else if (kf->type == KF_FORCE_SOURCE &&
-               in_picture->key_frame == 1 && !dup_idx) {
-            goto force_keyframe;
-    } else if (kf->type == KF_FORCE_SOURCE_NO_DROP && !dup_idx) {
-        kf->dropped_keyframe = 0;
-        if ((in_picture->key_frame == 1) || kf->dropped_keyframe)
-            goto force_keyframe;
-    }
-
-    return AV_PICTURE_TYPE_NONE;
-
-force_keyframe:
-    av_log(logctx, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time);
-    return AV_PICTURE_TYPE_I;
-}
-
-/* May modify/reset next_picture */
-static void do_video_out(OutputFile *of,
-                         OutputStream *ost,
-                         AVFrame *next_picture)
-{
-    int ret;
-    AVCodecContext *enc = ost->enc_ctx;
-    AVRational frame_rate;
-    int64_t nb_frames, nb_frames_prev, i;
-    double duration = 0;
-    InputStream *ist = ost->ist;
-    AVFilterContext *filter = ost->filter->filter;
-
-    ret = enc_open(ost, next_picture);
-    if (ret < 0)
-        exit_program(1);
-
-    frame_rate = av_buffersink_get_frame_rate(filter);
-    if (frame_rate.num > 0 && frame_rate.den > 0)
-        duration = 1/(av_q2d(frame_rate) * av_q2d(enc->time_base));
-
-    if(ist && ist->st->start_time != AV_NOPTS_VALUE && ist->first_dts != AV_NOPTS_VALUE && ost->frame_rate.num)
-        duration = FFMIN(duration, 1/(av_q2d(ost->frame_rate) * av_q2d(enc->time_base)));
-
-    if (!ost->filters_script &&
-        !ost->filters &&
-        (nb_filtergraphs == 0 || !filtergraphs[0]->graph_desc) &&
-        next_picture &&
-        ist &&
-        lrintf(next_picture->duration * av_q2d(ist->st->time_base) / av_q2d(enc->time_base)) > 0) {
-        duration = lrintf(next_picture->duration * av_q2d(ist->st->time_base) / av_q2d(enc->time_base));
-    }
-
-    if (!next_picture) {
-        //end, flushing
-        nb_frames_prev = nb_frames = mid_pred(ost->last_nb0_frames[0],
-                                              ost->last_nb0_frames[1],
-                                              ost->last_nb0_frames[2]);
-    } else {
-        video_sync_process(of, ost, next_picture, duration,
-                           &nb_frames, &nb_frames_prev);
-    }
-
-    memmove(ost->last_nb0_frames + 1,
-            ost->last_nb0_frames,
-            sizeof(ost->last_nb0_frames[0]) * (FF_ARRAY_ELEMS(ost->last_nb0_frames) - 1));
-    ost->last_nb0_frames[0] = nb_frames_prev;
-
-    if (nb_frames_prev == 0 && ost->last_dropped) {
-        nb_frames_drop++;
-        av_log(ost, AV_LOG_VERBOSE,
-               "*** dropping frame %"PRId64" at ts %"PRId64"\n",
-               ost->vsync_frame_number, ost->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);
-            nb_frames_drop++;
-            return;
-        }
-        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 (nb_frames_dup > dup_warning) {
-            av_log(ost, AV_LOG_WARNING, "More than %"PRIu64" frames duplicated\n", dup_warning);
-            dup_warning *= 10;
-        }
-    }
-    ost->last_dropped = nb_frames == nb_frames_prev && next_picture;
-    ost->kf.dropped_keyframe = ost->last_dropped && next_picture && next_picture->key_frame;
-
-    /* duplicates frame if needed */
-    for (i = 0; i < nb_frames; i++) {
-        AVFrame *in_picture;
-
-        if (i < nb_frames_prev && ost->last_frame->buf[0]) {
-            in_picture = ost->last_frame;
-        } else
-            in_picture = next_picture;
-
-        if (!in_picture)
-            return;
-
-        in_picture->pts = ost->next_pts;
-
-        if (!check_recording_time(ost, in_picture->pts, ost->enc_ctx->time_base))
-            return;
-
-        in_picture->quality = enc->global_quality;
-        in_picture->pict_type = forced_kf_apply(ost, &ost->kf, enc->time_base, in_picture, i);
-
-        ret = submit_encode_frame(of, ost, in_picture);
-        if (ret == AVERROR_EOF)
-            break;
-        else if (ret < 0)
-            exit_program(1);
-
-        ost->next_pts++;
-        ost->vsync_frame_number++;
-    }
-
-    av_frame_unref(ost->last_frame);
-    if (next_picture)
-        av_frame_move_ref(ost->last_frame, next_picture);
-}
-
 /**
  * Get and encode new output from any of the filtergraphs, without causing
  * activity.
@@ -1269,7 +668,6 @@ static int reap_filters(int flush)
 
     /* Reap all buffers present in the buffer sinks */
     for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
-        OutputFile    *of = output_files[ost->file_index];
         AVFilterContext *filter;
         AVCodecContext *enc = ost->enc_ctx;
         int ret = 0;
@@ -1289,7 +687,7 @@ static int reap_filters(int flush)
                            "Error in av_buffersink_get_frame_flags(): %s\n", av_err2str(ret));
                 } else if (flush && ret == AVERROR_EOF) {
                     if (av_buffersink_get_type(filter) == AVMEDIA_TYPE_VIDEO)
-                        do_video_out(of, ost, NULL);
+                        enc_frame(ost, NULL);
                 }
                 break;
             }
@@ -1316,7 +714,7 @@ static int reap_filters(int flush)
                 if (!ost->frame_aspect_ratio.num)
                     enc->sample_aspect_ratio = filtered_frame->sample_aspect_ratio;
 
-                do_video_out(of, ost, filtered_frame);
+                enc_frame(ost, filtered_frame);
                 break;
             case AVMEDIA_TYPE_AUDIO:
                 if (!(enc->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE) &&
@@ -1326,7 +724,7 @@ static int reap_filters(int flush)
                            "Audio filter graph output is not normalized and encoder does not support parameter changes\n");
                     break;
                 }
-                do_audio_out(of, ost, filtered_frame);
+                enc_frame(ost, filtered_frame);
                 break;
             default:
                 // TODO support subtitle filters
@@ -1657,7 +1055,7 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
         print_final_stats(total_size);
 }
 
-static int ifilter_parameters_from_codecpar(InputFilter *ifilter, AVCodecParameters *par)
+int ifilter_parameters_from_codecpar(InputFilter *ifilter, AVCodecParameters *par)
 {
     int ret;
 
@@ -1675,68 +1073,6 @@ static int ifilter_parameters_from_codecpar(InputFilter *ifilter, AVCodecParamet
     return 0;
 }
 
-static void flush_encoders(void)
-{
-    int ret;
-
-    for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
-        OutputFile      *of = output_files[ost->file_index];
-        if (ost->sq_idx_encode >= 0)
-            sq_send(of->sq_encode, ost->sq_idx_encode, SQFRAME(NULL));
-    }
-
-    for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
-        AVCodecContext *enc = ost->enc_ctx;
-        OutputFile      *of = output_files[ost->file_index];
-
-        if (!enc)
-            continue;
-
-        // Try to enable encoding with no input frames.
-        // Maybe we should just let encoding fail instead.
-        if (!ost->initialized) {
-            FilterGraph *fg = ost->filter->graph;
-
-            av_log(ost, AV_LOG_WARNING,
-                   "Finishing stream without any data written to it.\n");
-
-            if (ost->filter && !fg->graph) {
-                int x;
-                for (x = 0; x < fg->nb_inputs; x++) {
-                    InputFilter *ifilter = fg->inputs[x];
-                    if (ifilter->format < 0 &&
-                        ifilter_parameters_from_codecpar(ifilter, ifilter->ist->par) < 0) {
-                        av_log(ost, AV_LOG_ERROR, "Error copying paramerets from input stream\n");
-                        exit_program(1);
-                    }
-                }
-
-                if (!ifilter_has_all_input_formats(fg))
-                    continue;
-
-                ret = configure_filtergraph(fg);
-                if (ret < 0) {
-                    av_log(ost, AV_LOG_ERROR, "Error configuring filter graph\n");
-                    exit_program(1);
-                }
-
-                of_output_packet(of, ost->pkt, ost, 1);
-            }
-
-            ret = enc_open(ost, NULL);
-            if (ret < 0)
-                exit_program(1);
-        }
-
-        if (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO)
-            continue;
-
-        ret = submit_encode_frame(of, ost, NULL);
-        if (ret != AVERROR_EOF)
-            exit_program(1);
-    }
-}
-
 /*
  * Check whether a packet from ist should be written into ost at this time
  */
@@ -1859,7 +1195,7 @@ static void check_decode_result(InputStream *ist, int *got_output, int ret)
 }
 
 // Filters can be configured only if the formats of all inputs are known.
-static int ifilter_has_all_input_formats(FilterGraph *fg)
+int ifilter_has_all_input_formats(FilterGraph *fg)
 {
     int i;
     for (i = 0; i < fg->nb_inputs; i++) {
@@ -2377,7 +1713,7 @@ static int fix_sub_duration_heartbeat(InputStream *ist, int64_t signal_pts)
     return process_subtitle(ist, &subtitle, &got_output);
 }
 
-static int trigger_fix_sub_duration_heartbeat(OutputStream *ost, const AVPacket *pkt)
+int trigger_fix_sub_duration_heartbeat(OutputStream *ost, const AVPacket *pkt)
 {
     OutputFile *of = output_files[ost->file_index];
     int64_t signal_pts = av_rescale_q(pkt->pts, pkt->time_base,
@@ -3607,7 +2943,7 @@ static int transcode(void)
             process_input_packet(ist, NULL, 0);
         }
     }
-    flush_encoders();
+    enc_flush();
 
     term_exit();
 
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index ffb0ca33ac..c30659176e 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -707,6 +707,13 @@ typedef struct OutputFile {
     int bitexact;
 } OutputFile;
 
+// optionally attached as opaque_ref to decoded AVFrames
+typedef struct FrameData {
+    uint64_t   idx;
+    int64_t    pts;
+    AVRational tb;
+} FrameData;
+
 extern InputFile   **input_files;
 extern int        nb_input_files;
 
@@ -760,6 +767,11 @@ extern int copy_unknown_streams;
 
 extern int recast_media;
 
+extern FILE *vstats_file;
+
+extern int64_t nb_frames_dup;
+extern int64_t nb_frames_drop;
+
 #if FFMPEG_OPT_PSNR
 extern int do_psnr;
 #endif
@@ -788,6 +800,8 @@ int init_complex_filtergraph(FilterGraph *fg);
 void sub2video_update(InputStream *ist, int64_t heartbeat_pts, AVSubtitle *sub);
 
 int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
+int ifilter_parameters_from_codecpar(InputFilter *ifilter, AVCodecParameters *par);
+int ifilter_has_all_input_formats(FilterGraph *fg);
 
 int ffmpeg_parse_options(int argc, char **argv);
 
@@ -812,8 +826,8 @@ int hwaccel_decode_init(AVCodecContext *avctx);
 
 int enc_open(OutputStream *ost, AVFrame *frame);
 void enc_subtitle(OutputFile *of, OutputStream *ost, AVSubtitle *sub);
-
-int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb);
+void enc_frame(OutputStream *ost, AVFrame *frame);
+void enc_flush(void);
 
 /*
  * Initialize muxing state for the given stream, should be called
@@ -861,6 +875,19 @@ int ifile_get_packet(InputFile *f, AVPacket **pkt);
  * pass NULL to start iteration */
 InputStream *ist_iter(InputStream *prev);
 
+/* iterate over all output streams in all output files;
+ * pass NULL to start iteration */
+OutputStream *ost_iter(OutputStream *prev);
+
+static inline double psnr(double d)
+{
+    return -10.0 * log10(d);
+}
+
+void close_output_stream(OutputStream *ost);
+int trigger_fix_sub_duration_heartbeat(OutputStream *ost, const AVPacket *pkt);
+void update_benchmark(const char *fmt, ...);
+
 #define SPECIFIER_OPT_FMT_str  "%s"
 #define SPECIFIER_OPT_FMT_i    "%i"
 #define SPECIFIER_OPT_FMT_i64  "%"PRId64
diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index bcc560b9b7..c0e3eaa1e8 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -28,16 +28,23 @@
 #include "libavutil/display.h"
 #include "libavutil/eval.h"
 #include "libavutil/frame.h"
+#include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/rational.h"
+#include "libavutil/timestamp.h"
 
 #include "libavfilter/buffersink.h"
 
 #include "libavcodec/avcodec.h"
 
+// FIXME private header, used for mid_pred()
+#include "libavcodec/mathops.h"
+
 #include "libavformat/avformat.h"
 
+static uint64_t dup_warning = 1000;
+
 static void set_encoder_id(OutputFile *of, OutputStream *ost)
 {
     const char *cname = ost->enc_ctx->codec->name;
@@ -348,6 +355,18 @@ int enc_open(OutputStream *ost, AVFrame *frame)
     return 0;
 }
 
+static int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb)
+{
+    OutputFile *of = output_files[ost->file_index];
+
+    if (of->recording_time != INT64_MAX &&
+        av_compare_ts(ts, tb, of->recording_time, AV_TIME_BASE_Q) >= 0) {
+        close_output_stream(ost);
+        return 0;
+    }
+    return 1;
+}
+
 void enc_subtitle(OutputFile *of, OutputStream *ost, AVSubtitle *sub)
 {
     int subtitle_out_max_size = 1024 * 1024;
@@ -427,3 +446,644 @@ void enc_subtitle(OutputFile *of, OutputStream *ost, AVSubtitle *sub)
         of_output_packet(of, pkt, ost, 0);
     }
 }
+
+void enc_stats_write(OutputStream *ost, EncStats *es,
+                     const AVFrame *frame, const AVPacket *pkt,
+                     uint64_t frame_num)
+{
+    AVIOContext *io = es->io;
+    AVRational   tb = frame ? frame->time_base : pkt->time_base;
+    int64_t     pts = frame ? frame->pts : pkt->pts;
+
+    AVRational  tbi = (AVRational){ 0, 1};
+    int64_t    ptsi = INT64_MAX;
+
+    const FrameData *fd;
+
+    if ((frame && frame->opaque_ref) || (pkt && pkt->opaque_ref)) {
+        fd   = (const FrameData*)(frame ? frame->opaque_ref->data : pkt->opaque_ref->data);
+        tbi  = fd->tb;
+        ptsi = fd->pts;
+    }
+
+    for (size_t i = 0; i < es->nb_components; i++) {
+        const EncStatsComponent *c = &es->components[i];
+
+        switch (c->type) {
+        case ENC_STATS_LITERAL:         avio_write (io, c->str,     c->str_len);                    continue;
+        case ENC_STATS_FILE_IDX:        avio_printf(io, "%d",       ost->file_index);               continue;
+        case ENC_STATS_STREAM_IDX:      avio_printf(io, "%d",       ost->index);                    continue;
+        case ENC_STATS_TIMEBASE:        avio_printf(io, "%d/%d",    tb.num, tb.den);                continue;
+        case ENC_STATS_TIMEBASE_IN:     avio_printf(io, "%d/%d",    tbi.num, tbi.den);              continue;
+        case ENC_STATS_PTS:             avio_printf(io, "%"PRId64,  pts);                           continue;
+        case ENC_STATS_PTS_IN:          avio_printf(io, "%"PRId64,  ptsi);                          continue;
+        case ENC_STATS_PTS_TIME:        avio_printf(io, "%g",       pts * av_q2d(tb));              continue;
+        case ENC_STATS_PTS_TIME_IN:     avio_printf(io, "%g",       ptsi == INT64_MAX ?
+                                                                    INFINITY : ptsi * av_q2d(tbi)); continue;
+        case ENC_STATS_FRAME_NUM:       avio_printf(io, "%"PRIu64,  frame_num);                     continue;
+        case ENC_STATS_FRAME_NUM_IN:    avio_printf(io, "%"PRIu64,  fd ? fd->idx : -1);             continue;
+        }
+
+        if (frame) {
+            switch (c->type) {
+            case ENC_STATS_SAMPLE_NUM:  avio_printf(io, "%"PRIu64,  ost->samples_encoded);          continue;
+            case ENC_STATS_NB_SAMPLES:  avio_printf(io, "%d",       frame->nb_samples);             continue;
+            default: av_assert0(0);
+            }
+        } else {
+            switch (c->type) {
+            case ENC_STATS_DTS:         avio_printf(io, "%"PRId64,  pkt->dts);                      continue;
+            case ENC_STATS_DTS_TIME:    avio_printf(io, "%g",       pkt->dts * av_q2d(tb));         continue;
+            case ENC_STATS_PKT_SIZE:    avio_printf(io, "%d",       pkt->size);                     continue;
+            case ENC_STATS_BITRATE: {
+                double duration = FFMAX(pkt->duration, 1) * av_q2d(tb);
+                avio_printf(io, "%g",  8.0 * pkt->size / duration);
+                continue;
+            }
+            case ENC_STATS_AVG_BITRATE: {
+                double duration = pkt->dts * av_q2d(tb);
+                avio_printf(io, "%g",  duration > 0 ? 8.0 * ost->data_size_enc / duration : -1.);
+                continue;
+            }
+            default: av_assert0(0);
+            }
+        }
+    }
+    avio_w8(io, '\n');
+    avio_flush(io);
+}
+
+static void update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats)
+{
+    const uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
+                                                NULL);
+    AVCodecContext *enc = ost->enc_ctx;
+    int64_t frame_number;
+    double ti1, bitrate, avg_bitrate;
+
+    ost->quality   = sd ? AV_RL32(sd) : -1;
+    ost->pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
+
+    for (int i = 0; i<FF_ARRAY_ELEMS(ost->error); i++) {
+        if (sd && i < sd[5])
+            ost->error[i] = AV_RL64(sd + 8 + 8*i);
+        else
+            ost->error[i] = -1;
+    }
+
+    if (!write_vstats)
+        return;
+
+    /* this is executed just the first time update_video_stats is called */
+    if (!vstats_file) {
+        vstats_file = fopen(vstats_filename, "w");
+        if (!vstats_file) {
+            perror("fopen");
+            exit_program(1);
+        }
+    }
+
+    frame_number = ost->packets_encoded;
+    if (vstats_version <= 1) {
+        fprintf(vstats_file, "frame= %5"PRId64" q= %2.1f ", frame_number,
+                ost->quality / (float)FF_QP2LAMBDA);
+    } else  {
+        fprintf(vstats_file, "out= %2d st= %2d frame= %5"PRId64" q= %2.1f ", ost->file_index, ost->index, frame_number,
+                ost->quality / (float)FF_QP2LAMBDA);
+    }
+
+    if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR))
+        fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
+
+    fprintf(vstats_file,"f_size= %6d ", pkt->size);
+    /* compute pts value */
+    ti1 = pkt->dts * av_q2d(pkt->time_base);
+    if (ti1 < 0.01)
+        ti1 = 0.01;
+
+    bitrate     = (pkt->size * 8) / av_q2d(enc->time_base) / 1000.0;
+    avg_bitrate = (double)(ost->data_size_enc * 8) / ti1 / 1000.0;
+    fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
+           (double)ost->data_size_enc / 1024, ti1, bitrate, avg_bitrate);
+    fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(ost->pict_type));
+}
+
+static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
+{
+    AVCodecContext   *enc = ost->enc_ctx;
+    AVPacket         *pkt = ost->pkt;
+    const char *type_desc = av_get_media_type_string(enc->codec_type);
+    const char    *action = frame ? "encode" : "flush";
+    int ret;
+
+    if (frame) {
+        if (ost->enc_stats_pre.io)
+            enc_stats_write(ost, &ost->enc_stats_pre, frame, NULL,
+                            ost->frames_encoded);
+
+        ost->frames_encoded++;
+        ost->samples_encoded += frame->nb_samples;
+
+        if (debug_ts) {
+            av_log(ost, AV_LOG_INFO, "encoder <- type:%s "
+                   "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
+                   type_desc,
+                   av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
+                   enc->time_base.num, enc->time_base.den);
+        }
+    }
+
+    update_benchmark(NULL);
+
+    ret = avcodec_send_frame(enc, frame);
+    if (ret < 0 && !(ret == AVERROR_EOF && !frame)) {
+        av_log(ost, AV_LOG_ERROR, "Error submitting %s frame to the encoder\n",
+               type_desc);
+        return ret;
+    }
+
+    while (1) {
+        ret = avcodec_receive_packet(enc, pkt);
+        update_benchmark("%s_%s %d.%d", action, type_desc,
+                         ost->file_index, ost->index);
+
+        pkt->time_base = enc->time_base;
+
+        /* if two pass, output log on success and EOF */
+        if ((ret >= 0 || ret == AVERROR_EOF) && ost->logfile && enc->stats_out)
+            fprintf(ost->logfile, "%s", enc->stats_out);
+
+        if (ret == AVERROR(EAGAIN)) {
+            av_assert0(frame); // should never happen during flushing
+            return 0;
+        } else if (ret == AVERROR_EOF) {
+            of_output_packet(of, pkt, ost, 1);
+            return ret;
+        } else if (ret < 0) {
+            av_log(ost, AV_LOG_ERROR, "%s encoding failed\n", type_desc);
+            return ret;
+        }
+
+        if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
+            update_video_stats(ost, pkt, !!vstats_filename);
+        if (ost->enc_stats_post.io)
+            enc_stats_write(ost, &ost->enc_stats_post, NULL, pkt,
+                            ost->packets_encoded);
+
+        if (debug_ts) {
+            av_log(ost, AV_LOG_INFO, "encoder -> type:%s "
+                   "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
+                   "duration:%s duration_time:%s\n",
+                   type_desc,
+                   av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
+                   av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base),
+                   av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base));
+        }
+
+        av_packet_rescale_ts(pkt, pkt->time_base, ost->mux_timebase);
+        pkt->time_base = ost->mux_timebase;
+
+        if (debug_ts) {
+            av_log(ost, AV_LOG_INFO, "encoder -> type:%s "
+                   "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
+                   "duration:%s duration_time:%s\n",
+                   type_desc,
+                   av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
+                   av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base),
+                   av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base));
+        }
+
+        if ((ret = trigger_fix_sub_duration_heartbeat(ost, pkt)) < 0) {
+            av_log(NULL, AV_LOG_ERROR,
+                   "Subtitle heartbeat logic failed in %s! (%s)\n",
+                   __func__, av_err2str(ret));
+            exit_program(1);
+        }
+
+        ost->data_size_enc += pkt->size;
+
+        ost->packets_encoded++;
+
+        of_output_packet(of, pkt, ost, 0);
+    }
+
+    av_assert0(0);
+}
+
+static int submit_encode_frame(OutputFile *of, OutputStream *ost,
+                               AVFrame *frame)
+{
+    int ret;
+
+    if (ost->sq_idx_encode < 0)
+        return encode_frame(of, ost, frame);
+
+    if (frame) {
+        ret = av_frame_ref(ost->sq_frame, frame);
+        if (ret < 0)
+            return ret;
+        frame = ost->sq_frame;
+    }
+
+    ret = sq_send(of->sq_encode, ost->sq_idx_encode,
+                  SQFRAME(frame));
+    if (ret < 0) {
+        if (frame)
+            av_frame_unref(frame);
+        if (ret != AVERROR_EOF)
+            return ret;
+    }
+
+    while (1) {
+        AVFrame *enc_frame = ost->sq_frame;
+
+        ret = sq_receive(of->sq_encode, ost->sq_idx_encode,
+                               SQFRAME(enc_frame));
+        if (ret == AVERROR_EOF) {
+            enc_frame = NULL;
+        } else if (ret < 0) {
+            return (ret == AVERROR(EAGAIN)) ? 0 : ret;
+        }
+
+        ret = encode_frame(of, ost, enc_frame);
+        if (enc_frame)
+            av_frame_unref(enc_frame);
+        if (ret < 0) {
+            if (ret == AVERROR_EOF)
+                close_output_stream(ost);
+            return ret;
+        }
+    }
+}
+
+static void do_audio_out(OutputFile *of, OutputStream *ost,
+                         AVFrame *frame)
+{
+    AVCodecContext *enc = ost->enc_ctx;
+    int ret;
+
+    ret = enc_open(ost, frame);
+    if (ret < 0)
+        exit_program(1);
+
+    if (frame->pts == AV_NOPTS_VALUE)
+        frame->pts = ost->next_pts;
+    else {
+        int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
+        frame->pts =
+            av_rescale_q(frame->pts, frame->time_base, enc->time_base) -
+            av_rescale_q(start_time, AV_TIME_BASE_Q,   enc->time_base);
+    }
+    frame->time_base = enc->time_base;
+
+    if (!check_recording_time(ost, frame->pts, frame->time_base))
+        return;
+
+    ost->next_pts = frame->pts + frame->nb_samples;
+
+    ret = submit_encode_frame(of, ost, frame);
+    if (ret < 0 && ret != AVERROR_EOF)
+        exit_program(1);
+}
+
+static double adjust_frame_pts_to_encoder_tb(OutputFile *of, OutputStream *ost,
+                                             AVFrame *frame)
+{
+    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 filter_tb = frame->time_base;
+    const int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16);
+
+    if (frame->pts == AV_NOPTS_VALUE)
+        goto early_exit;
+
+    tb.den <<= extra_bits;
+    float_pts = av_rescale_q(frame->pts, filter_tb, tb) -
+                av_rescale_q(start_time, AV_TIME_BASE_Q, tb);
+    float_pts /= 1 << extra_bits;
+    // avoid exact midoints to reduce the chance of rounding differences, this
+    // can be removed in case the fps code is changed to work with integers
+    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;
+
+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);
+    }
+
+    return float_pts;
+}
+
+/* Convert frame timestamps to the encoder timebase and decide how many times
+ * 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 *next_picture, double duration,
+                               int64_t *nb_frames, int64_t *nb_frames_prev)
+{
+    double delta0, delta;
+
+    double sync_ipts = adjust_frame_pts_to_encoder_tb(of, ost, next_picture);
+    /* delta0 is the "drift" between the input frame (next_picture) and
+     * where it would fall in the output. */
+    delta0 = sync_ipts - ost->next_pts;
+    delta  = delta0 + duration;
+
+    // tracks the number of times the PREVIOUS frame should be duplicated,
+    // mostly for variable framerate (VFR)
+    *nb_frames_prev = 0;
+    /* by default, we output a single frame */
+    *nb_frames = 1;
+
+    if (delta0 < 0 &&
+        delta > 0 &&
+        ost->vsync_method != VSYNC_PASSTHROUGH &&
+        ost->vsync_method != VSYNC_DROP) {
+        if (delta0 < -0.6) {
+            av_log(ost, AV_LOG_VERBOSE, "Past duration %f too large\n", -delta0);
+        } else
+            av_log(ost, AV_LOG_DEBUG, "Clipping frame in rate conversion by %f\n", -delta0);
+        sync_ipts = ost->next_pts;
+        duration += delta0;
+        delta0 = 0;
+    }
+
+    switch (ost->vsync_method) {
+    case VSYNC_VSCFR:
+        if (ost->vsync_frame_number == 0 && delta0 >= 0.5) {
+            av_log(ost, AV_LOG_DEBUG, "Not duplicating %d initial frames\n", (int)lrintf(delta0));
+            delta = duration;
+            delta0 = 0;
+            ost->next_pts = llrint(sync_ipts);
+        }
+    case VSYNC_CFR:
+        // FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c
+        if (frame_drop_threshold && delta < frame_drop_threshold && ost->vsync_frame_number) {
+            *nb_frames = 0;
+        } else if (delta < -1.1)
+            *nb_frames = 0;
+        else if (delta > 1.1) {
+            *nb_frames = llrintf(delta);
+            if (delta0 > 1.1)
+                *nb_frames_prev = llrintf(delta0 - 0.6);
+        }
+        next_picture->duration = 1;
+        break;
+    case VSYNC_VFR:
+        if (delta <= -0.6)
+            *nb_frames = 0;
+        else if (delta > 0.6)
+            ost->next_pts = llrint(sync_ipts);
+        next_picture->duration = duration;
+        break;
+    case VSYNC_DROP:
+    case VSYNC_PASSTHROUGH:
+        next_picture->duration = duration;
+        ost->next_pts = llrint(sync_ipts);
+        break;
+    default:
+        av_assert0(0);
+    }
+}
+
+static enum AVPictureType forced_kf_apply(void *logctx, KeyframeForceCtx *kf,
+                                          AVRational tb, const AVFrame *in_picture,
+                                          int dup_idx)
+{
+    double pts_time;
+
+    if (kf->ref_pts == AV_NOPTS_VALUE)
+        kf->ref_pts = in_picture->pts;
+
+    pts_time = (in_picture->pts - kf->ref_pts) * av_q2d(tb);
+    if (kf->index < kf->nb_pts &&
+        av_compare_ts(in_picture->pts, tb, kf->pts[kf->index], AV_TIME_BASE_Q) >= 0) {
+        kf->index++;
+        goto force_keyframe;
+    } else if (kf->pexpr) {
+        double res;
+        kf->expr_const_values[FKF_T] = pts_time;
+        res = av_expr_eval(kf->pexpr,
+                           kf->expr_const_values, NULL);
+        av_log(logctx, AV_LOG_TRACE,
+               "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
+               kf->expr_const_values[FKF_N],
+               kf->expr_const_values[FKF_N_FORCED],
+               kf->expr_const_values[FKF_PREV_FORCED_N],
+               kf->expr_const_values[FKF_T],
+               kf->expr_const_values[FKF_PREV_FORCED_T],
+               res);
+
+        kf->expr_const_values[FKF_N] += 1;
+
+        if (res) {
+            kf->expr_const_values[FKF_PREV_FORCED_N] = kf->expr_const_values[FKF_N] - 1;
+            kf->expr_const_values[FKF_PREV_FORCED_T] = kf->expr_const_values[FKF_T];
+            kf->expr_const_values[FKF_N_FORCED]     += 1;
+            goto force_keyframe;
+        }
+    } else if (kf->type == KF_FORCE_SOURCE &&
+               in_picture->key_frame == 1 && !dup_idx) {
+            goto force_keyframe;
+    } else if (kf->type == KF_FORCE_SOURCE_NO_DROP && !dup_idx) {
+        kf->dropped_keyframe = 0;
+        if ((in_picture->key_frame == 1) || kf->dropped_keyframe)
+            goto force_keyframe;
+    }
+
+    return AV_PICTURE_TYPE_NONE;
+
+force_keyframe:
+    av_log(logctx, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time);
+    return AV_PICTURE_TYPE_I;
+}
+
+/* May modify/reset next_picture */
+static void do_video_out(OutputFile *of,
+                         OutputStream *ost,
+                         AVFrame *next_picture)
+{
+    int ret;
+    AVCodecContext *enc = ost->enc_ctx;
+    AVRational frame_rate;
+    int64_t nb_frames, nb_frames_prev, i;
+    double duration = 0;
+    InputStream *ist = ost->ist;
+    AVFilterContext *filter = ost->filter->filter;
+
+    ret = enc_open(ost, next_picture);
+    if (ret < 0)
+        exit_program(1);
+
+    frame_rate = av_buffersink_get_frame_rate(filter);
+    if (frame_rate.num > 0 && frame_rate.den > 0)
+        duration = 1/(av_q2d(frame_rate) * av_q2d(enc->time_base));
+
+    if(ist && ist->st->start_time != AV_NOPTS_VALUE && ist->first_dts != AV_NOPTS_VALUE && ost->frame_rate.num)
+        duration = FFMIN(duration, 1/(av_q2d(ost->frame_rate) * av_q2d(enc->time_base)));
+
+    if (!ost->filters_script &&
+        !ost->filters &&
+        (nb_filtergraphs == 0 || !filtergraphs[0]->graph_desc) &&
+        next_picture &&
+        ist &&
+        lrintf(next_picture->duration * av_q2d(ist->st->time_base) / av_q2d(enc->time_base)) > 0) {
+        duration = lrintf(next_picture->duration * av_q2d(ist->st->time_base) / av_q2d(enc->time_base));
+    }
+
+    if (!next_picture) {
+        //end, flushing
+        nb_frames_prev = nb_frames = mid_pred(ost->last_nb0_frames[0],
+                                              ost->last_nb0_frames[1],
+                                              ost->last_nb0_frames[2]);
+    } else {
+        video_sync_process(of, ost, next_picture, duration,
+                           &nb_frames, &nb_frames_prev);
+    }
+
+    memmove(ost->last_nb0_frames + 1,
+            ost->last_nb0_frames,
+            sizeof(ost->last_nb0_frames[0]) * (FF_ARRAY_ELEMS(ost->last_nb0_frames) - 1));
+    ost->last_nb0_frames[0] = nb_frames_prev;
+
+    if (nb_frames_prev == 0 && ost->last_dropped) {
+        nb_frames_drop++;
+        av_log(ost, AV_LOG_VERBOSE,
+               "*** dropping frame %"PRId64" at ts %"PRId64"\n",
+               ost->vsync_frame_number, ost->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);
+            nb_frames_drop++;
+            return;
+        }
+        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 (nb_frames_dup > dup_warning) {
+            av_log(ost, AV_LOG_WARNING, "More than %"PRIu64" frames duplicated\n", dup_warning);
+            dup_warning *= 10;
+        }
+    }
+    ost->last_dropped = nb_frames == nb_frames_prev && next_picture;
+    ost->kf.dropped_keyframe = ost->last_dropped && next_picture && next_picture->key_frame;
+
+    /* duplicates frame if needed */
+    for (i = 0; i < nb_frames; i++) {
+        AVFrame *in_picture;
+
+        if (i < nb_frames_prev && ost->last_frame->buf[0]) {
+            in_picture = ost->last_frame;
+        } else
+            in_picture = next_picture;
+
+        if (!in_picture)
+            return;
+
+        in_picture->pts = ost->next_pts;
+
+        if (!check_recording_time(ost, in_picture->pts, ost->enc_ctx->time_base))
+            return;
+
+        in_picture->quality = enc->global_quality;
+        in_picture->pict_type = forced_kf_apply(ost, &ost->kf, enc->time_base, in_picture, i);
+
+        ret = submit_encode_frame(of, ost, in_picture);
+        if (ret == AVERROR_EOF)
+            break;
+        else if (ret < 0)
+            exit_program(1);
+
+        ost->next_pts++;
+        ost->vsync_frame_number++;
+    }
+
+    av_frame_unref(ost->last_frame);
+    if (next_picture)
+        av_frame_move_ref(ost->last_frame, next_picture);
+}
+
+void enc_frame(OutputStream *ost, AVFrame *frame)
+{
+    OutputFile *of = output_files[ost->file_index];
+
+    if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO) do_video_out(of, ost, frame);
+    else                                                do_audio_out(of, ost, frame);
+}
+
+void enc_flush(void)
+{
+    int ret;
+
+    for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
+        OutputFile      *of = output_files[ost->file_index];
+        if (ost->sq_idx_encode >= 0)
+            sq_send(of->sq_encode, ost->sq_idx_encode, SQFRAME(NULL));
+    }
+
+    for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
+        AVCodecContext *enc = ost->enc_ctx;
+        OutputFile      *of = output_files[ost->file_index];
+
+        if (!enc)
+            continue;
+
+        // Try to enable encoding with no input frames.
+        // Maybe we should just let encoding fail instead.
+        if (!ost->initialized) {
+            FilterGraph *fg = ost->filter->graph;
+
+            av_log(ost, AV_LOG_WARNING,
+                   "Finishing stream without any data written to it.\n");
+
+            if (ost->filter && !fg->graph) {
+                int x;
+                for (x = 0; x < fg->nb_inputs; x++) {
+                    InputFilter *ifilter = fg->inputs[x];
+                    if (ifilter->format < 0 &&
+                        ifilter_parameters_from_codecpar(ifilter, ifilter->ist->par) < 0) {
+                        av_log(ost, AV_LOG_ERROR, "Error copying paramerets from input stream\n");
+                        exit_program(1);
+                    }
+                }
+
+                if (!ifilter_has_all_input_formats(fg))
+                    continue;
+
+                ret = configure_filtergraph(fg);
+                if (ret < 0) {
+                    av_log(ost, AV_LOG_ERROR, "Error configuring filter graph\n");
+                    exit_program(1);
+                }
+
+                of_output_packet(of, ost->pkt, ost, 1);
+            }
+
+            ret = enc_open(ost, NULL);
+            if (ret < 0)
+                exit_program(1);
+        }
+
+        if (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO)
+            continue;
+
+        ret = submit_encode_frame(of, ost, NULL);
+        if (ret != AVERROR_EOF)
+            exit_program(1);
+    }
+}
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 19/23] fftools/ffmpeg: add encoder private data
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
                   ` (16 preceding siblings ...)
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 18/23] fftools/ffmpeg: move audio/video encoding code " Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 20/23] fftools/ffmpeg: stop including os_support.h Anton Khirnov
                   ` (3 subsequent siblings)
  21 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

Start by moving OutputStream.last_frame to it. In the future it will
hold other encoder-internal state.
---
 fftools/ffmpeg.h          |  7 +++++-
 fftools/ffmpeg_enc.c      | 51 +++++++++++++++++++++++++++++++++++----
 fftools/ffmpeg_mux.c      |  3 ++-
 fftools/ffmpeg_mux_init.c |  8 +++---
 4 files changed, 58 insertions(+), 11 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index c30659176e..8193dabb57 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -557,6 +557,8 @@ typedef struct KeyframeForceCtx {
     int          dropped_keyframe;
 } KeyframeForceCtx;
 
+typedef struct Encoder Encoder;
+
 typedef struct OutputStream {
     const AVClass *class;
 
@@ -588,9 +590,9 @@ typedef struct OutputStream {
     AVRational mux_timebase;
     AVRational enc_timebase;
 
+    Encoder *enc;
     AVCodecContext *enc_ctx;
     AVFrame *filtered_frame;
-    AVFrame *last_frame;
     AVFrame *sq_frame;
     AVPacket *pkt;
     int64_t last_dropped;
@@ -824,6 +826,9 @@ AVBufferRef *hw_device_for_filter(void);
 
 int hwaccel_decode_init(AVCodecContext *avctx);
 
+int enc_alloc(Encoder **penc, const AVCodec *codec);
+void enc_free(Encoder **penc);
+
 int enc_open(OutputStream *ost, AVFrame *frame);
 void enc_subtitle(OutputFile *of, OutputStream *ost, AVSubtitle *sub);
 void enc_frame(OutputStream *ost, AVFrame *frame);
diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index c0e3eaa1e8..1387e05cf6 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -43,8 +43,48 @@
 
 #include "libavformat/avformat.h"
 
+struct Encoder {
+    AVFrame *last_frame;
+};
+
 static uint64_t dup_warning = 1000;
 
+void enc_free(Encoder **penc)
+{
+    Encoder *enc = *penc;
+
+    if (!enc)
+        return;
+
+    av_frame_free(&enc->last_frame);
+
+    av_freep(penc);
+}
+
+int enc_alloc(Encoder **penc, const AVCodec *codec)
+{
+    Encoder *enc;
+
+    *penc = NULL;
+
+    enc = av_mallocz(sizeof(*enc));
+    if (!enc)
+        return AVERROR(ENOMEM);
+
+    if (codec->type == AVMEDIA_TYPE_VIDEO) {
+        enc->last_frame = av_frame_alloc();
+        if (!enc->last_frame)
+            goto fail;
+    }
+
+    *penc = enc;
+
+    return 0;
+fail:
+    enc_free(&enc);
+    return AVERROR(ENOMEM);
+}
+
 static void set_encoder_id(OutputFile *of, OutputStream *ost)
 {
     const char *cname = ost->enc_ctx->codec->name;
@@ -919,6 +959,7 @@ static void do_video_out(OutputFile *of,
                          AVFrame *next_picture)
 {
     int ret;
+    Encoder *e = ost->enc;
     AVCodecContext *enc = ost->enc_ctx;
     AVRational frame_rate;
     int64_t nb_frames, nb_frames_prev, i;
@@ -965,7 +1006,7 @@ static void do_video_out(OutputFile *of,
         nb_frames_drop++;
         av_log(ost, AV_LOG_VERBOSE,
                "*** dropping frame %"PRId64" at ts %"PRId64"\n",
-               ost->vsync_frame_number, ost->last_frame->pts);
+               ost->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) {
@@ -987,8 +1028,8 @@ static void do_video_out(OutputFile *of,
     for (i = 0; i < nb_frames; i++) {
         AVFrame *in_picture;
 
-        if (i < nb_frames_prev && ost->last_frame->buf[0]) {
-            in_picture = ost->last_frame;
+        if (i < nb_frames_prev && e->last_frame->buf[0]) {
+            in_picture = e->last_frame;
         } else
             in_picture = next_picture;
 
@@ -1013,9 +1054,9 @@ static void do_video_out(OutputFile *of,
         ost->vsync_frame_number++;
     }
 
-    av_frame_unref(ost->last_frame);
+    av_frame_unref(e->last_frame);
     if (next_picture)
-        av_frame_move_ref(ost->last_frame, next_picture);
+        av_frame_move_ref(e->last_frame, next_picture);
 }
 
 void enc_frame(OutputStream *ost, AVFrame *frame)
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 1937bc2aa7..527567831f 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -643,6 +643,8 @@ static void ost_free(OutputStream **post)
         return;
     ms = ms_from_ost(ost);
 
+    enc_free(&ost->enc);
+
     if (ost->logfile) {
         if (fclose(ost->logfile))
             av_log(ms, AV_LOG_ERROR,
@@ -662,7 +664,6 @@ static void ost_free(OutputStream **post)
 
     av_frame_free(&ost->filtered_frame);
     av_frame_free(&ost->sq_frame);
-    av_frame_free(&ost->last_frame);
     av_packet_free(&ost->pkt);
     av_dict_free(&ost->encoder_opts);
 
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index 385312d4fe..ff95ea9e02 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -462,6 +462,10 @@ static OutputStream *new_output_stream(Muxer *mux, const OptionsContext *o,
         if (!ost->enc_ctx)
             report_and_exit(AVERROR(ENOMEM));
 
+        ret = enc_alloc(&ost->enc, enc);
+        if (ret < 0)
+            report_and_exit(ret);
+
         av_strlcat(ms->log_name, "/",       sizeof(ms->log_name));
         av_strlcat(ms->log_name, enc->name, sizeof(ms->log_name));
     } else {
@@ -933,10 +937,6 @@ static OutputStream *new_video_stream(Muxer *mux, const OptionsContext *o, Input
         ost->avfilter = get_ost_filters(o, oc, ost);
         if (!ost->avfilter)
             exit_program(1);
-
-        ost->last_frame = av_frame_alloc();
-        if (!ost->last_frame)
-            report_and_exit(AVERROR(ENOMEM));
     } else
         check_streamcopy_filters(o, oc, ost, AVMEDIA_TYPE_VIDEO);
 
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 20/23] fftools/ffmpeg: stop including os_support.h
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
                   ` (17 preceding siblings ...)
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 19/23] fftools/ffmpeg: add encoder private data Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 21/23] fftools/ffmpeg: clean up system header includes Anton Khirnov
                   ` (2 subsequent siblings)
  21 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

It was added for usleep(), which
- is not used in ffmpeg
- is not handled in os_support.h anymore
---
 fftools/ffmpeg.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 7a6b206d11..63dd97a130 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -63,7 +63,6 @@
 #include "libavutil/thread.h"
 #include "libavutil/threadmessage.h"
 #include "libavcodec/version.h"
-#include "libavformat/os_support.h"
 
 # include "libavfilter/avfilter.h"
 # include "libavfilter/buffersrc.h"
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 21/23] fftools/ffmpeg: clean up system header includes
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
                   ` (18 preceding siblings ...)
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 20/23] fftools/ffmpeg: stop including os_support.h Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-27  5:35   ` Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 22/23] fftools/ffmpeg: clean up local includes Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 23/23] fftools/ffmpeg_enc: factorize calling enc_init() Anton Khirnov
  21 siblings, 1 reply; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

Drop unneeded ctype.h and math.h.
Group all system headers together.
Sort unconditional includes alphabetically.
---
 fftools/ffmpeg.c | 65 +++++++++++++++++++++++-------------------------
 1 file changed, 31 insertions(+), 34 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 63dd97a130..eedd51d407 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -24,14 +24,14 @@
  */
 
 #include "config.h"
-#include <ctype.h>
-#include <string.h>
-#include <math.h>
-#include <stdlib.h>
+
 #include <errno.h>
 #include <limits.h>
 #include <stdatomic.h>
 #include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
 
 #if HAVE_IO_H
 #include <io.h>
@@ -40,34 +40,6 @@
 #include <unistd.h>
 #endif
 
-#include "libavformat/avformat.h"
-#include "libavdevice/avdevice.h"
-#include "libswresample/swresample.h"
-#include "libavutil/opt.h"
-#include "libavutil/channel_layout.h"
-#include "libavutil/parseutils.h"
-#include "libavutil/samplefmt.h"
-#include "libavutil/fifo.h"
-#include "libavutil/hwcontext.h"
-#include "libavutil/intreadwrite.h"
-#include "libavutil/dict.h"
-#include "libavutil/display.h"
-#include "libavutil/mathematics.h"
-#include "libavutil/pixdesc.h"
-#include "libavutil/avstring.h"
-#include "libavutil/libm.h"
-#include "libavutil/imgutils.h"
-#include "libavutil/timestamp.h"
-#include "libavutil/bprint.h"
-#include "libavutil/time.h"
-#include "libavutil/thread.h"
-#include "libavutil/threadmessage.h"
-#include "libavcodec/version.h"
-
-# include "libavfilter/avfilter.h"
-# include "libavfilter/buffersrc.h"
-# include "libavfilter/buffersink.h"
-
 #if HAVE_SYS_RESOURCE_H
 #include <sys/time.h>
 #include <sys/types.h>
@@ -83,7 +55,6 @@
 #include <windows.h>
 #endif
 
-
 #if HAVE_SYS_SELECT_H
 #include <sys/select.h>
 #endif
@@ -97,7 +68,33 @@
 #include <conio.h>
 #endif
 
-#include <time.h>
+#include "libavformat/avformat.h"
+#include "libavdevice/avdevice.h"
+#include "libswresample/swresample.h"
+#include "libavutil/opt.h"
+#include "libavutil/channel_layout.h"
+#include "libavutil/parseutils.h"
+#include "libavutil/samplefmt.h"
+#include "libavutil/fifo.h"
+#include "libavutil/hwcontext.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/dict.h"
+#include "libavutil/display.h"
+#include "libavutil/mathematics.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/avstring.h"
+#include "libavutil/libm.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/timestamp.h"
+#include "libavutil/bprint.h"
+#include "libavutil/time.h"
+#include "libavutil/thread.h"
+#include "libavutil/threadmessage.h"
+#include "libavcodec/version.h"
+
+# include "libavfilter/avfilter.h"
+# include "libavfilter/buffersrc.h"
+# include "libavfilter/buffersink.h"
 
 #include "ffmpeg.h"
 #include "cmdutils.h"
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 22/23] fftools/ffmpeg: clean up local includes
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
                   ` (19 preceding siblings ...)
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 21/23] fftools/ffmpeg: clean up system header includes Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 23/23] fftools/ffmpeg_enc: factorize calling enc_init() Anton Khirnov
  21 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

Group them by the library, sort alphabetically.
---
 fftools/ffmpeg.c | 43 +++++++++++++++++++++++--------------------
 1 file changed, 23 insertions(+), 20 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index eedd51d407..ae69d29ab7 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -68,40 +68,43 @@
 #include <conio.h>
 #endif
 
-#include "libavformat/avformat.h"
-#include "libavdevice/avdevice.h"
-#include "libswresample/swresample.h"
-#include "libavutil/opt.h"
+#include "libavutil/avassert.h"
+#include "libavutil/avstring.h"
+#include "libavutil/bprint.h"
 #include "libavutil/channel_layout.h"
-#include "libavutil/parseutils.h"
-#include "libavutil/samplefmt.h"
+#include "libavutil/dict.h"
+#include "libavutil/display.h"
 #include "libavutil/fifo.h"
 #include "libavutil/hwcontext.h"
+#include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
-#include "libavutil/dict.h"
-#include "libavutil/display.h"
+#include "libavutil/libm.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/opt.h"
+#include "libavutil/parseutils.h"
 #include "libavutil/pixdesc.h"
-#include "libavutil/avstring.h"
-#include "libavutil/libm.h"
-#include "libavutil/imgutils.h"
-#include "libavutil/timestamp.h"
-#include "libavutil/bprint.h"
-#include "libavutil/time.h"
+#include "libavutil/samplefmt.h"
 #include "libavutil/thread.h"
 #include "libavutil/threadmessage.h"
+#include "libavutil/time.h"
+#include "libavutil/timestamp.h"
+
 #include "libavcodec/version.h"
 
-# include "libavfilter/avfilter.h"
-# include "libavfilter/buffersrc.h"
-# include "libavfilter/buffersink.h"
+#include "libavformat/avformat.h"
+
+#include "libavdevice/avdevice.h"
+
+#include "libswresample/swresample.h"
+
+#include "libavfilter/avfilter.h"
+#include "libavfilter/buffersrc.h"
+#include "libavfilter/buffersink.h"
 
-#include "ffmpeg.h"
 #include "cmdutils.h"
+#include "ffmpeg.h"
 #include "sync_queue.h"
 
-#include "libavutil/avassert.h"
-
 const char program_name[] = "ffmpeg";
 const int program_birth_year = 2000;
 
-- 
2.39.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] 38+ messages in thread

* [FFmpeg-devel] [PATCH 23/23] fftools/ffmpeg_enc: factorize calling enc_init()
  2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
                   ` (20 preceding siblings ...)
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 22/23] fftools/ffmpeg: clean up local includes Anton Khirnov
@ 2023-03-25 19:15 ` Anton Khirnov
  21 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-25 19:15 UTC (permalink / raw)
  To: ffmpeg-devel

It is done in the same way for both audio and video, so can be moved to
enc_frame().
---
 fftools/ffmpeg_enc.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index 1387e05cf6..2f1803a74c 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -762,10 +762,6 @@ static void do_audio_out(OutputFile *of, OutputStream *ost,
     AVCodecContext *enc = ost->enc_ctx;
     int ret;
 
-    ret = enc_open(ost, frame);
-    if (ret < 0)
-        exit_program(1);
-
     if (frame->pts == AV_NOPTS_VALUE)
         frame->pts = ost->next_pts;
     else {
@@ -967,10 +963,6 @@ static void do_video_out(OutputFile *of,
     InputStream *ist = ost->ist;
     AVFilterContext *filter = ost->filter->filter;
 
-    ret = enc_open(ost, next_picture);
-    if (ret < 0)
-        exit_program(1);
-
     frame_rate = av_buffersink_get_frame_rate(filter);
     if (frame_rate.num > 0 && frame_rate.den > 0)
         duration = 1/(av_q2d(frame_rate) * av_q2d(enc->time_base));
@@ -1062,6 +1054,11 @@ static void do_video_out(OutputFile *of,
 void enc_frame(OutputStream *ost, AVFrame *frame)
 {
     OutputFile *of = output_files[ost->file_index];
+    int ret;
+
+    ret = enc_open(ost, frame);
+    if (ret < 0)
+        exit_program(1);
 
     if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO) do_video_out(of, ost, frame);
     else                                                do_audio_out(of, ost, frame);
-- 
2.39.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] 38+ messages in thread

* Re: [FFmpeg-devel] [PATCH 09/23] fftools/ffmpeg: stop handling AVMEDIA_TYPE_DATA in init_output_stream_encode()
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 09/23] fftools/ffmpeg: stop handling AVMEDIA_TYPE_DATA in init_output_stream_encode() Anton Khirnov
@ 2023-03-25 19:43   ` James Almer
  2023-03-26  9:20     ` Anton Khirnov
  0 siblings, 1 reply; 38+ messages in thread
From: James Almer @ 2023-03-25 19:43 UTC (permalink / raw)
  To: ffmpeg-devel

On 3/25/2023 4:15 PM, Anton Khirnov wrote:
> We do not support data encoders, so this should never be reached.

nit: The abort() below could be an av_assert0(0) instead. It's the only 
abort() in the file, whereas av_assert0() is used a lot.

> ---
>   fftools/ffmpeg.c | 2 --
>   1 file changed, 2 deletions(-)
> 
> diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
> index f00b2d44e4..a424c69725 100644
> --- a/fftools/ffmpeg.c
> +++ b/fftools/ffmpeg.c
> @@ -3174,8 +3174,6 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame)
>               }
>           }
>   
> -        break;
> -    case AVMEDIA_TYPE_DATA:
>           break;
>       default:
>           abort();
_______________________________________________
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] 38+ messages in thread

* Re: [FFmpeg-devel] [PATCH 08/23] fftools/ffmpeg: use sync queues for enforcing audio frame size
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 08/23] fftools/ffmpeg: use sync queues for enforcing audio frame size Anton Khirnov
@ 2023-03-25 21:43   ` Michael Niedermayer
  2023-03-27  5:15     ` [FFmpeg-devel] [PATCH] fftools/ffmpeg: do not return finished streams from choose_output() Anton Khirnov
  2023-03-29 18:08     ` [FFmpeg-devel] [PATCH 08/23] fftools/ffmpeg: use sync queues for enforcing audio frame size Michael Niedermayer
  0 siblings, 2 replies; 38+ messages in thread
From: Michael Niedermayer @ 2023-03-25 21:43 UTC (permalink / raw)
  To: FFmpeg development discussions and patches


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

On Sat, Mar 25, 2023 at 08:15:14PM +0100, Anton Khirnov wrote:
> The code currently uses lavfi for this, which creates a sort of
> configuration dependency loop - the encoder should be ideally
> initialized with information from the first audio frame, but to get this
> frame one needs to first open the encoder to know the frame size. This
> necessitates an awkward workaround, which causes audio handling to be
> different from video.
> 
> With this change, audio encoder initialization is congruent with video.
> ---
>  fftools/ffmpeg.c          | 58 ++++++++-------------------------------
>  fftools/ffmpeg_filter.c   |  8 ------
>  fftools/ffmpeg_mux_init.c | 19 +++++++++----
>  3 files changed, 25 insertions(+), 60 deletions(-)

this results in the following to be apparently stuck

ffmpeg -y -i https://samples.ffmpeg.org/V-codecs/geov.avi -t 1 file.avi


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

Democracy is the form of government in which you can choose your dictator

[-- 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] 38+ messages in thread

* Re: [FFmpeg-devel] [PATCH 09/23] fftools/ffmpeg: stop handling AVMEDIA_TYPE_DATA in init_output_stream_encode()
  2023-03-25 19:43   ` James Almer
@ 2023-03-26  9:20     ` Anton Khirnov
  0 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-26  9:20 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

Quoting James Almer (2023-03-25 20:43:46)
> On 3/25/2023 4:15 PM, Anton Khirnov wrote:
> > We do not support data encoders, so this should never be reached.
> 
> nit: The abort() below could be an av_assert0(0) instead. It's the only 
> abort() in the file, whereas av_assert0() is used a lot.

I did notice that too and I agree it should be changed, but not in this
patch.

-- 
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] 38+ messages in thread

* [FFmpeg-devel] [PATCH] fftools/ffmpeg: do not return finished streams from choose_output()
  2023-03-25 21:43   ` Michael Niedermayer
@ 2023-03-27  5:15     ` Anton Khirnov
  2023-03-29 17:59       ` Michael Niedermayer
  2023-03-29 18:08     ` [FFmpeg-devel] [PATCH 08/23] fftools/ffmpeg: use sync queues for enforcing audio frame size Michael Niedermayer
  1 sibling, 1 reply; 38+ messages in thread
From: Anton Khirnov @ 2023-03-27  5:15 UTC (permalink / raw)
  To: ffmpeg-devel

---
Should be fixed by this patch.
---
 fftools/ffmpeg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index ae69d29ab7..f1fdbd47eb 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -2402,7 +2402,7 @@ static OutputStream *choose_output(void)
                     ost->initialized, ost->inputs_done, ost->finished);
         }
 
-        if (!ost->initialized && !ost->inputs_done)
+        if (!ost->initialized && !ost->inputs_done && !ost->finished)
             return ost->unavailable ? NULL : ost;
 
         if (!ost->finished && opts < opts_min) {
-- 
2.39.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] 38+ messages in thread

* Re: [FFmpeg-devel] [PATCH 21/23] fftools/ffmpeg: clean up system header includes
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 21/23] fftools/ffmpeg: clean up system header includes Anton Khirnov
@ 2023-03-27  5:35   ` Anton Khirnov
  0 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-27  5:35 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

Quoting Anton Khirnov (2023-03-25 20:15:27)
> Drop unneeded ctype.h and math.h.
> Group all system headers together.
> Sort unconditional includes alphabetically.
> ---
>  fftools/ffmpeg.c | 65 +++++++++++++++++++++++-------------------------
>  1 file changed, 31 insertions(+), 34 deletions(-)

As per discussion on IRC with Andreas and James, there seems to be
little advantage to splitting this patch and the next one, so unless
someone objects I will squash them before pushing.

-- 
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] 38+ messages in thread

* Re: [FFmpeg-devel] [PATCH 13/23] fftools/ffmpeg: reindent after previous commit
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 13/23] fftools/ffmpeg: reindent after previous commit Anton Khirnov
@ 2023-03-28 22:42   ` Michael Niedermayer
  2023-03-29  0:16     ` Anton Khirnov
  0 siblings, 1 reply; 38+ messages in thread
From: Michael Niedermayer @ 2023-03-28 22:42 UTC (permalink / raw)
  To: FFmpeg development discussions and patches


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

On Sat, Mar 25, 2023 at 08:15:19PM +0100, Anton Khirnov wrote:
> ---
>  fftools/ffmpeg.c | 142 +++++++++++++++++++++++------------------------
>  1 file changed, 71 insertions(+), 71 deletions(-)

with the new 8/23 this doesnt apply anymore and the next one also fails if i
fix this, so i cant test this updated patchset from the ML after this patch
and be sure what i test would match your code

thx

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

No great genius has ever existed without some touch of madness. -- Aristotle

[-- 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] 38+ messages in thread

* Re: [FFmpeg-devel] [PATCH 13/23] fftools/ffmpeg: reindent after previous commit
  2023-03-28 22:42   ` Michael Niedermayer
@ 2023-03-29  0:16     ` Anton Khirnov
  0 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-03-29  0:16 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

Quoting Michael Niedermayer (2023-03-29 00:42:03)
> On Sat, Mar 25, 2023 at 08:15:19PM +0100, Anton Khirnov wrote:
> > ---
> >  fftools/ffmpeg.c | 142 +++++++++++++++++++++++------------------------
> >  1 file changed, 71 insertions(+), 71 deletions(-)
> 
> with the new 8/23 this doesnt apply anymore and the next one also fails if i
> fix this, so i cant test this updated patchset from the ML after this patch
> and be sure what i test would match your code

Oh, it's a new 8/23, it's a new patch that should be added to the
series (at any point, as it does not interact with any other patches).
All the old ones stay as they were. Sorry for the confusion.

I also pushed the branch to
https://git.khirnov.net/libav.git/log/?h=ffmpeg_frame_size

Thanks,
-- 
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] 38+ messages in thread

* Re: [FFmpeg-devel] [PATCH] fftools/ffmpeg: do not return finished streams from choose_output()
  2023-03-27  5:15     ` [FFmpeg-devel] [PATCH] fftools/ffmpeg: do not return finished streams from choose_output() Anton Khirnov
@ 2023-03-29 17:59       ` Michael Niedermayer
  2023-03-30  8:48         ` Anton Khirnov
  0 siblings, 1 reply; 38+ messages in thread
From: Michael Niedermayer @ 2023-03-29 17:59 UTC (permalink / raw)
  To: FFmpeg development discussions and patches


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

On Mon, Mar 27, 2023 at 07:15:56AM +0200, Anton Khirnov wrote:
> ---
> Should be fixed by this patch.
> ---
>  fftools/ffmpeg.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

This:

/home/michael/ffmpeg-git/ffmpeg/ffmpeg -y -threads:a 1 -i /home/michael/tickets//1208/702121h264-TTA.mkvtest82.mkv -bitexact -vn doom_tmp/regress/file1208.mkv
Error while decoding stream #0:1: Invalid data found when processing input
    Last message repeated 5 times
[matroska,webm @ 0x55e2e82919c0] File ended prematurely
Error while decoding stream #0:1: Invalid data found when processing input
    Last message repeated 1 times
[sost#0:1/ssa @ 0x55e2e82ca300] Error submitting a packet to the muxer
    Last message repeated 12465121 times
                          ^^^^^^^^
keeps counting until killed

(tested with khirnov/ffmpeg_frame_size
 it seems the new patch was in this)

thx

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

It is dangerous to be right in matters on which the established authorities
are wrong. -- Voltaire

[-- 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] 38+ messages in thread

* Re: [FFmpeg-devel] [PATCH 08/23] fftools/ffmpeg: use sync queues for enforcing audio frame size
  2023-03-25 21:43   ` Michael Niedermayer
  2023-03-27  5:15     ` [FFmpeg-devel] [PATCH] fftools/ffmpeg: do not return finished streams from choose_output() Anton Khirnov
@ 2023-03-29 18:08     ` Michael Niedermayer
  1 sibling, 0 replies; 38+ messages in thread
From: Michael Niedermayer @ 2023-03-29 18:08 UTC (permalink / raw)
  To: FFmpeg development discussions and patches


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

On Sat, Mar 25, 2023 at 10:43:49PM +0100, Michael Niedermayer wrote:
> On Sat, Mar 25, 2023 at 08:15:14PM +0100, Anton Khirnov wrote:
> > The code currently uses lavfi for this, which creates a sort of
> > configuration dependency loop - the encoder should be ideally
> > initialized with information from the first audio frame, but to get this
> > frame one needs to first open the encoder to know the frame size. This
> > necessitates an awkward workaround, which causes audio handling to be
> > different from video.
> > 
> > With this change, audio encoder initialization is congruent with video.
> > ---
> >  fftools/ffmpeg.c          | 58 ++++++++-------------------------------
> >  fftools/ffmpeg_filter.c   |  8 ------
> >  fftools/ffmpeg_mux_init.c | 19 +++++++++----
> >  3 files changed, 25 insertions(+), 60 deletions(-)
> 
> this results in the following to be apparently stuck
> 
> ffmpeg -y -i https://samples.ffmpeg.org/V-codecs/geov.avi -t 1 file.avi

This patch causes more issues it seems
the following:

./ffmpeg -i AnivisionLogo.bik -t 1 -y test.avi
see https://samples.ffmpeg.org/game-formats/bink/

produces:

==9868== Process terminating with default action of signal 11 (SIGSEGV)
==9868==  General Protection Fault
==9868==    at 0x11D3CAD: ??? (in ffmpeg/ffmpeg_g)
==9868==    by 0x9ACA31: mp3lame_encode_frame (in ffmpeg/ffmpeg_g)
==9868==    by 0x882EC4: ff_encode_encode_cb (in ffmpeg/ffmpeg_g)
==9868==    by 0x8832DD: encode_receive_packet_internal (in ffmpeg/ffmpeg_g)
==9868==    by 0x8834BF: avcodec_send_frame (in ffmpeg/ffmpeg_g)
==9868==    by 0x31A281: encode_frame (in ffmpeg/ffmpeg_g)
==9868==    by 0x31F901: transcode (in ffmpeg/ffmpeg_g)
==9868==    by 0x2F0843: main (in ffmpeg/ffmpeg_g)



[...]

-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

It is a danger to trust the dream we wish for rather than
the science we have, -- Dr. Kenneth Brown

[-- 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] 38+ messages in thread

* Re: [FFmpeg-devel] [PATCH 07/23] fftools/sync_queue: allow requesting a specific number of audio samples
  2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 07/23] fftools/sync_queue: allow requesting a specific number of audio samples Anton Khirnov
@ 2023-03-29 23:41   ` James Almer
  0 siblings, 0 replies; 38+ messages in thread
From: James Almer @ 2023-03-29 23:41 UTC (permalink / raw)
  To: ffmpeg-devel

On 3/25/2023 4:15 PM, Anton Khirnov wrote:
> +static void offset_audio(AVFrame *f, int nb_samples)
> +{
> +    const int planar = av_sample_fmt_is_planar(f->format);
> +    const int planes = planar ? f->ch_layout.nb_channels : 1;
> +    const int    bps = av_get_bytes_per_sample(f->format);
> +    const int offset = nb_samples * bps * (planar ? 1 : f->ch_layout.nb_channels);
> +
> +    av_assert0(bps > 0);
> +    av_assert0(nb_samples < f->nb_samples);
> +
> +    for (int i = 0; i < planes; i++) {
> +        f->extended_data[i] += offset;

This apparently can end up unaligned. It's (afaict) the source of the 
segmentation fault Michael reported. If you run his command with 
-cpuflags 0 it doesn't crash.
You may need to allocate a new frame and copy the data instead of making 
a reference in such scenarios.

> +        if (i < FF_ARRAY_ELEMS(f->data))
> +            f->data[i] = f->extended_data[i];
> +    }
> +    f->linesize[0] -= offset;
> +    f->nb_samples  -= nb_samples;
> +    f->duration     = av_rescale_q(f->nb_samples, (AVRational){ 1, f->sample_rate },
> +                                   f->time_base);
> +    f->pts         += av_rescale_q(nb_samples,    (AVRational){ 1, f->sample_rate },
> +                                   f->time_base);
> +}
_______________________________________________
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] 38+ messages in thread

* Re: [FFmpeg-devel] [PATCH] fftools/ffmpeg: do not return finished streams from choose_output()
  2023-03-29 17:59       ` Michael Niedermayer
@ 2023-03-30  8:48         ` Anton Khirnov
  2023-04-02 15:58           ` Michael Niedermayer
  0 siblings, 1 reply; 38+ messages in thread
From: Anton Khirnov @ 2023-03-30  8:48 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

Quoting Michael Niedermayer (2023-03-29 19:59:09)
> On Mon, Mar 27, 2023 at 07:15:56AM +0200, Anton Khirnov wrote:
> > ---
> > Should be fixed by this patch.
> > ---
> >  fftools/ffmpeg.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> This:
> 
> /home/michael/ffmpeg-git/ffmpeg/ffmpeg -y -threads:a 1 -i /home/michael/tickets//1208/702121h264-TTA.mkvtest82.mkv -bitexact -vn doom_tmp/regress/file1208.mkv
> Error while decoding stream #0:1: Invalid data found when processing input
>     Last message repeated 5 times
> [matroska,webm @ 0x55e2e82919c0] File ended prematurely
> Error while decoding stream #0:1: Invalid data found when processing input
>     Last message repeated 1 times
> [sost#0:1/ssa @ 0x55e2e82ca300] Error submitting a packet to the muxer
>     Last message repeated 12465121 times
>                           ^^^^^^^^
> keeps counting until killed
> 
> (tested with khirnov/ffmpeg_frame_size
>  it seems the new patch was in this)

Where can I get the sample? The link in ticket 1208 is apparently no
longer valid.

-- 
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] 38+ messages in thread

* Re: [FFmpeg-devel] [PATCH] fftools/ffmpeg: do not return finished streams from choose_output()
  2023-03-30  8:48         ` Anton Khirnov
@ 2023-04-02 15:58           ` Michael Niedermayer
  2023-04-03 10:09             ` [FFmpeg-devel] [PATCH] fftools/ffmpeg: make sure non-lavfi streams are closed on input EOF Anton Khirnov
  0 siblings, 1 reply; 38+ messages in thread
From: Michael Niedermayer @ 2023-04-02 15:58 UTC (permalink / raw)
  To: FFmpeg development discussions and patches


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

On Thu, Mar 30, 2023 at 10:48:21AM +0200, Anton Khirnov wrote:
> Quoting Michael Niedermayer (2023-03-29 19:59:09)
> > On Mon, Mar 27, 2023 at 07:15:56AM +0200, Anton Khirnov wrote:
> > > ---
> > > Should be fixed by this patch.
> > > ---
> > >  fftools/ffmpeg.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > This:
> > 
> > /home/michael/ffmpeg-git/ffmpeg/ffmpeg -y -threads:a 1 -i /home/michael/tickets//1208/702121h264-TTA.mkvtest82.mkv -bitexact -vn doom_tmp/regress/file1208.mkv
> > Error while decoding stream #0:1: Invalid data found when processing input
> >     Last message repeated 5 times
> > [matroska,webm @ 0x55e2e82919c0] File ended prematurely
> > Error while decoding stream #0:1: Invalid data found when processing input
> >     Last message repeated 1 times
> > [sost#0:1/ssa @ 0x55e2e82ca300] Error submitting a packet to the muxer
> >     Last message repeated 12465121 times
> >                           ^^^^^^^^
> > keeps counting until killed
> > 
> > (tested with khirnov/ffmpeg_frame_size
> >  it seems the new patch was in this)
> 
> Where can I get the sample? The link in ticket 1208 is apparently no
> longer valid.

sorry
its here:
https://samples.ffmpeg.org/ffmpeg-bugs/trac/ticket1208/702121h264-TTA.mkvtest82.mkv

thx

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

Many that live deserve death. And some that die deserve life. Can you give
it to them? Then do not be too eager to deal out death in judgement. For
even the very wise cannot see all ends. -- Gandalf

[-- 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] 38+ messages in thread

* [FFmpeg-devel] [PATCH] fftools/ffmpeg: make sure non-lavfi streams are closed on input EOF
  2023-04-02 15:58           ` Michael Niedermayer
@ 2023-04-03 10:09             ` Anton Khirnov
  2023-04-05 22:33               ` Michael Niedermayer
  0 siblings, 1 reply; 38+ messages in thread
From: Anton Khirnov @ 2023-04-03 10:09 UTC (permalink / raw)
  To: ffmpeg-devel

---
Should be fixed by adding this patch to the set, at any point.
Also updated my ffmpeg_frame_size branch.
---
 fftools/ffmpeg.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 438bee8fef..2f1f830507 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -3804,6 +3804,7 @@ static int process_input(int file_index)
                 if (ost->ist == ist &&
                     (!ost->enc_ctx || ost->enc_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
                     OutputFile *of = output_files[ost->file_index];
+                    close_output_stream(ost);
                     of_output_packet(of, ost->pkt, ost, 1);
                 }
             }
-- 
2.39.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] 38+ messages in thread

* Re: [FFmpeg-devel] [PATCH] fftools/ffmpeg: make sure non-lavfi streams are closed on input EOF
  2023-04-03 10:09             ` [FFmpeg-devel] [PATCH] fftools/ffmpeg: make sure non-lavfi streams are closed on input EOF Anton Khirnov
@ 2023-04-05 22:33               ` Michael Niedermayer
  2023-04-06  7:27                 ` Anton Khirnov
  0 siblings, 1 reply; 38+ messages in thread
From: Michael Niedermayer @ 2023-04-05 22:33 UTC (permalink / raw)
  To: FFmpeg development discussions and patches


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

On Mon, Apr 03, 2023 at 12:09:54PM +0200, Anton Khirnov wrote:
> ---
> Should be fixed by adding this patch to the set, at any point.
> Also updated my ffmpeg_frame_size branch.
> ---
>  fftools/ffmpeg.c | 1 +
>  1 file changed, 1 insertion(+)

Ive tested ffmpeg_frame_size and there are a few cases that change their
output, but i failed to find one that by watching the output looked different
I saw no crashes, infinite loops or other anomalies

Thx

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

If the United States is serious about tackling the national security threats 
related to an insecure 5G network, it needs to rethink the extent to which it
values corporate profits and government espionage over security.-Bruce Schneier

[-- 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] 38+ messages in thread

* Re: [FFmpeg-devel] [PATCH] fftools/ffmpeg: make sure non-lavfi streams are closed on input EOF
  2023-04-05 22:33               ` Michael Niedermayer
@ 2023-04-06  7:27                 ` Anton Khirnov
  0 siblings, 0 replies; 38+ messages in thread
From: Anton Khirnov @ 2023-04-06  7:27 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

Quoting Michael Niedermayer (2023-04-06 00:33:07)
> On Mon, Apr 03, 2023 at 12:09:54PM +0200, Anton Khirnov wrote:
> > ---
> > Should be fixed by adding this patch to the set, at any point.
> > Also updated my ffmpeg_frame_size branch.
> > ---
> >  fftools/ffmpeg.c | 1 +
> >  1 file changed, 1 insertion(+)
> 
> Ive tested ffmpeg_frame_size and there are a few cases that change their
> output, but i failed to find one that by watching the output looked different
> I saw no crashes, infinite loops or other anomalies

Thanks you for testing, I'd be interested in looking at some of those
changed cases, if the samples are available. While changes in some
corner cases are possible, it'd be nice to make sure that nothing
actually breaks.

Otherwise I'm going to push the set soonish.

-- 
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] 38+ messages in thread

end of thread, other threads:[~2023-04-06  7:27 UTC | newest]

Thread overview: 38+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 02/23] fftools/ffmpeg: move initializing next_[pd]ts to add_input_streams() Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 03/23] fftools/sync_queue: use timebase from input frames/packets Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 04/23] fftools/sync_queue: document overall design Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 05/23] fftools/sync_queue: support operation with no limiting streams Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 06/23] fftools/sync_queue: make sure audio duration matches sample count Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 07/23] fftools/sync_queue: allow requesting a specific number of audio samples Anton Khirnov
2023-03-29 23:41   ` James Almer
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 08/23] fftools/ffmpeg: use sync queues for enforcing audio frame size Anton Khirnov
2023-03-25 21:43   ` Michael Niedermayer
2023-03-27  5:15     ` [FFmpeg-devel] [PATCH] fftools/ffmpeg: do not return finished streams from choose_output() Anton Khirnov
2023-03-29 17:59       ` Michael Niedermayer
2023-03-30  8:48         ` Anton Khirnov
2023-04-02 15:58           ` Michael Niedermayer
2023-04-03 10:09             ` [FFmpeg-devel] [PATCH] fftools/ffmpeg: make sure non-lavfi streams are closed on input EOF Anton Khirnov
2023-04-05 22:33               ` Michael Niedermayer
2023-04-06  7:27                 ` Anton Khirnov
2023-03-29 18:08     ` [FFmpeg-devel] [PATCH 08/23] fftools/ffmpeg: use sync queues for enforcing audio frame size Michael Niedermayer
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 09/23] fftools/ffmpeg: stop handling AVMEDIA_TYPE_DATA in init_output_stream_encode() Anton Khirnov
2023-03-25 19:43   ` James Almer
2023-03-26  9:20     ` Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 10/23] fftools/ffmpeg: drop unnecessarily indirection Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 11/23] fftools/ffmpeg: use stack variables to shorten code Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 12/23] fftools/ffmpeg: move encoder initialization to init_output_stream_encode Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 13/23] fftools/ffmpeg: reindent after previous commit Anton Khirnov
2023-03-28 22:42   ` Michael Niedermayer
2023-03-29  0:16     ` Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 14/23] fftools/ffmpeg: move initializing encoders to a new file Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 15/23] fftools/ffmpeg: simplify output stream initialization call graph Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 16/23] fftools/ffmpeg: replace ff_dlog() with av_log() Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 17/23] fftools/ffmpeg: move subtitle encoding to ffmpeg_enc.c Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 18/23] fftools/ffmpeg: move audio/video encoding code " Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 19/23] fftools/ffmpeg: add encoder private data Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 20/23] fftools/ffmpeg: stop including os_support.h Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 21/23] fftools/ffmpeg: clean up system header includes Anton Khirnov
2023-03-27  5:35   ` Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 22/23] fftools/ffmpeg: clean up local includes Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 23/23] fftools/ffmpeg_enc: factorize calling enc_init() 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