History

fftools/sync_queue: allow requesting a specific number of audio samples

This will be made useful in following commits.

Anton Khirnov committed 3 years ago

Commit f9d3c061 parent 81cca3d2 files · +185 −9Browse files

Showing the changes to fftools/sync_queue.c (1 of 2 files). Show all

Unified · Split

fftools/sync_queue.c+175−9
@@ -20,10 +20,13 @@ · 19 unmodified lines
2020 #include <string.h>
2121
2222 #include "libavutil/avassert.h"
23+#include "libavutil/channel_layout.h"
24+#include "libavutil/cpu.h"
2325 #include "libavutil/error.h"
2426 #include "libavutil/fifo.h"
2527 #include "libavutil/mathematics.h"
2628 #include "libavutil/mem.h"
29+#include "libavutil/samplefmt.h"
2730
2831 #include "objpool.h"
2932 #include "sync_queue.h"
@@ -67,6 +70,8 @@ typedef struct SyncQueueStream { · 37 unmodified lines
6770 AVFifo *fifo;
6871 AVRational tb;
6972
73+ /* number of audio samples in fifo */
74+ uint64_t samples_queued;
7075 /* stream head: largest timestamp seen */
7176 int64_t head_ts;
7277 int limiting;
@@ -74,7 +79,9 @@ typedef struct SyncQueueStream { · 1 unmodified line
7479 int finished;
7580
7681 uint64_t frames_sent;
82+ uint64_t samples_sent;
7783 uint64_t frames_max;
84+ int frame_samples;
7885 } SyncQueueStream;
7986
8087 struct SyncQueue {
@@ -98,6 +105,8 @@ struct SyncQueue { · 17 unmodified lines
98105 ObjPool *pool;
99106
100107 int have_limiting;
108+
109+ uintptr_t align_mask;
101110 };
102111
103112 static void frame_move(const SyncQueue *sq, SyncQueueFrame dst,
@@ -109,8 +118,18 @@ static void frame_move(const SyncQueue *sq, SyncQueueFrame dst, · 5 unmodified lines
109118 av_frame_move_ref(dst.f, src.f);
110119 }
111120
112−static int64_t frame_ts(const SyncQueue *sq, SyncQueueFrame frame)
121+/**
122+ * Compute the end timestamp of a frame. If nb_samples is provided, consider
123+ * the frame to have this number of audio samples, otherwise use frame duration.
124+ */
125+static int64_t frame_end(const SyncQueue *sq, SyncQueueFrame frame, int nb_samples)
113126 {
127+ if (nb_samples) {
128+ int64_t d = av_rescale_q(nb_samples, (AVRational){ 1, frame.f->sample_rate},
129+ frame.f->time_base);
130+ return frame.f->pts + d;
131+ }
132+
114133 return (sq->type == SYNC_QUEUE_PACKETS) ?
115134 frame.p->pts + frame.p->duration :
116135 frame.f->pts + frame.f->duration;
@@ -265,7 +284,7 @@ static int overflow_heartbeat(SyncQueue *sq, int stream_idx) · 148 unmodified lines
265284 /* get the chosen stream's tail timestamp */
266285 for (size_t i = 0; tail_ts == AV_NOPTS_VALUE &&
267286 av_fifo_peek(st->fifo, &frame, 1, i) >= 0; i++)
268− tail_ts = frame_ts(sq, frame);
287+ tail_ts = frame_end(sq, frame, 0);
269288
270289 /* overflow triggers when the tail is over specified duration behind the head */
271290 if (tail_ts == AV_NOPTS_VALUE || tail_ts >= st->head_ts ||
@@ -326,7 +345,7 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame) · 54 unmodified lines
326345 dst.f->time_base);
327346 }
328347
329− ts = frame_ts(sq, dst);
348+ ts = frame_end(sq, dst, 0);
330349
331350 ret = av_fifo_write(st->fifo, &dst, 1);
332351 if (ret < 0) {
@@ -337,13 +356,131 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame) · 4 unmodified lines
337356
338357 stream_update_ts(sq, stream_idx, ts);
339358
340− st->frames_sent++;
359+ st->samples_queued += nb_samples;
360+ st->samples_sent += nb_samples;
361+
362+ if (st->frame_samples)
363+ st->frames_sent = st->samples_sent / st->frame_samples;
364+ else
365+ st->frames_sent++;
366+
341367 if (st->frames_sent >= st->frames_max)
342368 finish_stream(sq, stream_idx);
343369
344370 return 0;
345371 }
346372
373+static void offset_audio(AVFrame *f, int nb_samples)
374+{
375+ const int planar = av_sample_fmt_is_planar(f->format);
376+ const int planes = planar ? f->ch_layout.nb_channels : 1;
377+ const int bps = av_get_bytes_per_sample(f->format);
378+ const int offset = nb_samples * bps * (planar ? 1 : f->ch_layout.nb_channels);
379+
380+ av_assert0(bps > 0);
381+ av_assert0(nb_samples < f->nb_samples);
382+
383+ for (int i = 0; i < planes; i++) {
384+ f->extended_data[i] += offset;
385+ if (i < FF_ARRAY_ELEMS(f->data))
386+ f->data[i] = f->extended_data[i];
387+ }
388+ f->linesize[0] -= offset;
389+ f->nb_samples -= nb_samples;
390+ f->duration = av_rescale_q(f->nb_samples, (AVRational){ 1, f->sample_rate },
391+ f->time_base);
392+ f->pts += av_rescale_q(nb_samples, (AVRational){ 1, f->sample_rate },
393+ f->time_base);
394+}
395+
396+static int frame_is_aligned(const SyncQueue *sq, const AVFrame *frame)
397+{
398+ // only checks linesize[0], so only works for audio
399+ av_assert0(frame->nb_samples > 0);
400+ av_assert0(sq->align_mask);
401+
402+ // only check data[0], because we always offset all data pointers
403+ // by the same offset, so if one is aligned, all are
404+ if (!((uintptr_t)frame->data[0] & sq->align_mask) &&
405+ !(frame->linesize[0] & sq->align_mask) &&
406+ frame->linesize[0] > sq->align_mask)
407+ return 1;
408+
409+ return 0;
410+}
411+
412+static int receive_samples(SyncQueue *sq, SyncQueueStream *st,
413+ AVFrame *dst, int nb_samples)
414+{
415+ SyncQueueFrame src;
416+ int ret;
417+
418+ av_assert0(st->samples_queued >= nb_samples);
419+
420+ ret = av_fifo_peek(st->fifo, &src, 1, 0);
421+ av_assert0(ret >= 0);
422+
423+ // peeked frame has enough samples and its data is aligned
424+ // -> we can just make a reference and limit its sample count
425+ if (src.f->nb_samples > nb_samples && frame_is_aligned(sq, src.f)) {
426+ ret = av_frame_ref(dst, src.f);
427+ if (ret < 0)
428+ return ret;
429+
430+ dst->nb_samples = nb_samples;
431+ offset_audio(src.f, nb_samples);
432+ st->samples_queued -= nb_samples;
433+
434+ return 0;
435+ }
436+
437+ // otherwise allocate a new frame and copy the data
438+ ret = av_channel_layout_copy(&dst->ch_layout, &src.f->ch_layout);
439+ if (ret < 0)
440+ return ret;
441+
442+ dst->format = src.f->format;
443+ dst->nb_samples = nb_samples;
444+
445+ ret = av_frame_get_buffer(dst, 0);
446+ if (ret < 0)
447+ goto fail;
448+
449+ ret = av_frame_copy_props(dst, src.f);
450+ if (ret < 0)
451+ goto fail;
452+
453+ dst->nb_samples = 0;
454+ while (dst->nb_samples < nb_samples) {
455+ int to_copy;
456+
457+ ret = av_fifo_peek(st->fifo, &src, 1, 0);
458+ av_assert0(ret >= 0);
459+
460+ to_copy = FFMIN(nb_samples - dst->nb_samples, src.f->nb_samples);
461+
462+ av_samples_copy(dst->extended_data, src.f->extended_data, dst->nb_samples,
463+ 0, to_copy, dst->ch_layout.nb_channels, dst->format);
464+
465+ if (to_copy < src.f->nb_samples)
466+ offset_audio(src.f, to_copy);
467+ else {
468+ av_frame_unref(src.f);
469+ objpool_release(sq->pool, (void**)&src);
470+ av_fifo_drain2(st->fifo, 1);
471+ }
472+ st->samples_queued -= to_copy;
473+
474+ dst->nb_samples += to_copy;
475+ }
476+
477+ return 0;
478+
479+fail:
480+ av_frame_unref(dst);
481+ return ret;
482+}
483+
347484 static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx,
348485 SyncQueueFrame frame)
349486 {
@@ -354,13 +491,18 @@ static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx, · 4 unmodified lines
354491 av_assert0(stream_idx < sq->nb_streams);
355492 st = &sq->streams[stream_idx];
356493
357− if (av_fifo_can_read(st->fifo)) {
494+ if (av_fifo_can_read(st->fifo) &&
495+ (st->frame_samples <= st->samples_queued || st->finished)) {
496+ int nb_samples = st->frame_samples;
358497 SyncQueueFrame peek;
359498 int64_t ts;
360499 int cmp = 1;
361500
501+ if (st->finished)
502+ nb_samples = FFMIN(nb_samples, st->samples_queued);
503+
362504 av_fifo_peek(st->fifo, &peek, 1, 0);
363− ts = frame_ts(sq, peek);
505+ ts = frame_end(sq, peek, nb_samples);
364506
365507 /* check if this stream's tail timestamp does not overtake
366508 * the overall queue head */
@@ -372,9 +514,19 @@ static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx, · 5 unmodified lines
372514 * Frames are also passed through when there are no limiting streams.
373515 */
374516 if (cmp <= 0 || ts == AV_NOPTS_VALUE || !sq->have_limiting) {
375− frame_move(sq, frame, peek);
376− objpool_release(sq->pool, (void**)&peek);
377− av_fifo_drain2(st->fifo, 1);
517+ if (nb_samples &&
518+ (nb_samples != peek.f->nb_samples || !frame_is_aligned(sq, peek.f))) {
519+ int ret = receive_samples(sq, st, frame.f, nb_samples);
520+ if (ret < 0)
521+ return ret;
522+ } else {
523+ frame_move(sq, frame, peek);
524+ objpool_release(sq->pool, (void**)&peek);
525+ av_fifo_drain2(st->fifo, 1);
526+ av_assert0(st->samples_queued >= frame_samples(sq, frame));
527+ st->samples_queued -= frame_samples(sq, frame);
528+ }
529+
378530 return 0;
379531 }
380532 }
@@ -460,6 +612,20 @@ void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx, uint64_t frames) · 79 unmodified lines
460612 finish_stream(sq, stream_idx);
461613 }
462614
615+void sq_frame_samples(SyncQueue *sq, unsigned int stream_idx,
616+ int frame_samples)
617+{
618+ SyncQueueStream *st;
619+
620+ av_assert0(sq->type == SYNC_QUEUE_FRAMES);
621+ av_assert0(stream_idx < sq->nb_streams);
622+ st = &sq->streams[stream_idx];
623+
624+ st->frame_samples = frame_samples;
625+
626+ sq->align_mask = av_cpu_max_align() - 1;
627+}
628+
463629 SyncQueue *sq_alloc(enum SyncQueueType type, int64_t buf_size_us)
464630 {
465631 SyncQueue *sq = av_mallocz(sizeof(*sq));

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t