Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Anton Khirnov <anton@khirnov.net>
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH 07/23] fftools/sync_queue: allow requesting a specific number of audio samples
Date: Sat, 25 Mar 2023 20:15:13 +0100
Message-ID: <20230325191529.10578-7-anton@khirnov.net> (raw)
In-Reply-To: <20230325191529.10578-1-anton@khirnov.net>

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".

  parent reply	other threads:[~2023-03-25 19:17 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Anton Khirnov [this message]
2023-03-29 23:41   ` [FFmpeg-devel] [PATCH 07/23] fftools/sync_queue: allow requesting a specific number of audio samples 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230325191529.10578-7-anton@khirnov.net \
    --to=anton@khirnov.net \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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