History

fftools/sync_queue: switch from AVFifo+ObjPool to AVContainerFifo

Remove now-unused objpool.

Anton Khirnov committed 1 year ago

Commit 8ad34e91 parent 8e0ccef4 files · +29 −233Browse files

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

Unified · Split

fftools/sync_queue.c+29−64
@@ -20,16 +20,15 @@ · 19 unmodified lines
2020 #include <string.h>
2121
2222 #include "libavutil/avassert.h"
23+#include "libavutil/container_fifo.h"
2324 #include "libavutil/channel_layout.h"
2425 #include "libavutil/cpu.h"
2526 #include "libavutil/error.h"
26−#include "libavutil/fifo.h"
2727 #include "libavutil/mathematics.h"
2828 #include "libavutil/mem.h"
2929 #include "libavutil/samplefmt.h"
3030 #include "libavutil/timestamp.h"
3131
32−#include "objpool.h"
3332 #include "sync_queue.h"
3433
3534 /*
@@ -67,8 +66,11 @@ · 31 unmodified lines
6766 * frame from stream 1, and all 4 frames from stream 2.
6867 */
6968
69+#define SQPTR(sq, frame) ((sq->type == SYNC_QUEUE_FRAMES) ? \
70+ (void*)frame.f : (void*)frame.p)
71+
7072 typedef struct SyncQueueStream {
71− AVFifo *fifo;
73+ AVContainerFifo *fifo;
7274 AVRational tb;
7375
7476 /* number of audio samples in fifo */
@@ -104,23 +106,11 @@ struct SyncQueue { · 29 unmodified lines
104106 SyncQueueStream *streams;
105107 unsigned int nb_streams;
106108
107− // pool of preallocated frames to avoid constant allocations
108− ObjPool *pool;
109−
110109 int have_limiting;
111110
112111 uintptr_t align_mask;
113112 };
114113
115−static void frame_move(const SyncQueue *sq, SyncQueueFrame dst,
116− SyncQueueFrame src)
117−{
118− if (sq->type == SYNC_QUEUE_PACKETS)
119− av_packet_move_ref(dst.p, src.p);
120− else
121− av_frame_move_ref(dst.f, src.f);
122−}
123−
124114 /**
125115 * Compute the end timestamp of a frame. If nb_samples is provided, consider
126116 * the frame to have this number of audio samples, otherwise use frame duration.
@@ -160,7 +150,7 @@ static void tb_update(const SyncQueue *sq, SyncQueueStream *st, · 33 unmodified lines
160150 return;
161151
162152 // timebase should not change after the first frame
163− av_assert0(!av_fifo_can_read(st->fifo));
153+ av_assert0(!av_container_fifo_can_read(st->fifo));
164154
165155 if (st->head_ts != AV_NOPTS_VALUE)
166156 st->head_ts = av_rescale_q(st->head_ts, st->tb, tb);
@@ -308,7 +298,7 @@ static int overflow_heartbeat(SyncQueue *sq, int stream_idx) · 141 unmodified lines
308298
309299 /* get the chosen stream's tail timestamp */
310300 for (size_t i = 0; tail_ts == AV_NOPTS_VALUE &&
311− av_fifo_peek(st->fifo, &frame, 1, i) >= 0; i++)
301+ av_container_fifo_peek(st->fifo, (void**)&frame, i) >= 0; i++)
312302 tail_ts = frame_end(sq, frame, 0);
313303
314304 /* overflow triggers when the tail is over specified duration behind the head */
@@ -343,7 +333,6 @@ static int overflow_heartbeat(SyncQueue *sq, int stream_idx) · 28 unmodified lines
343333 int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame)
344334 {
345335 SyncQueueStream *st;
346− SyncQueueFrame dst;
347336 int64_t ts;
348337 int ret, nb_samples;
349338
@@ -360,31 +349,22 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame) · 10 unmodified lines
360349
361350 tb_update(sq, st, frame);
362351
363− ret = objpool_get(sq->pool, (void**)&dst);
364− if (ret < 0)
365− return ret;
366−
367− frame_move(sq, dst, frame);
368−
369− nb_samples = frame_samples(sq, dst);
352+ nb_samples = frame_samples(sq, frame);
370353 // make sure frame duration is consistent with sample count
371354 if (nb_samples) {
372− av_assert0(dst.f->sample_rate > 0);
373− dst.f->duration = av_rescale_q(nb_samples, (AVRational){ 1, dst.f->sample_rate },
374− dst.f->time_base);
355+ av_assert0(frame.f->sample_rate > 0);
356+ frame.f->duration = av_rescale_q(nb_samples, (AVRational){ 1, frame.f->sample_rate },
357+ frame.f->time_base);
375358 }
376359
377− ts = frame_end(sq, dst, 0);
360+ ts = frame_end(sq, frame, 0);
378361
379362 av_log(sq->logctx, AV_LOG_DEBUG, "sq: send %u ts %s\n", stream_idx,
380363 av_ts2timestr(ts, &st->tb));
381364
382− ret = av_fifo_write(st->fifo, &dst, 1);
383− if (ret < 0) {
384− frame_move(sq, frame, dst);
385− objpool_release(sq->pool, (void**)&dst);
365+ ret = av_container_fifo_write(st->fifo, SQPTR(sq, frame), 0);
366+ if (ret < 0)
386367 return ret;
387− }
388368
389369 stream_update_ts(sq, stream_idx, ts);
390370
@@ -453,7 +433,7 @@ static int receive_samples(SyncQueue *sq, SyncQueueStream *st, · 62 unmodified lines
453433
454434 av_assert0(st->samples_queued >= nb_samples);
455435
456− ret = av_fifo_peek(st->fifo, &src, 1, 0);
436+ ret = av_container_fifo_peek(st->fifo, (void**)&src, 0);
457437 av_assert0(ret >= 0);
458438
459439 // peeked frame has enough samples and its data is aligned
@@ -490,7 +470,7 @@ static int receive_samples(SyncQueue *sq, SyncQueueStream *st, · 30 unmodified lines
490470 while (dst->nb_samples < nb_samples) {
491471 int to_copy;
492472
493− ret = av_fifo_peek(st->fifo, &src, 1, 0);
473+ ret = av_container_fifo_peek(st->fifo, (void**)&src, 0);
494474 av_assert0(ret >= 0);
495475
496476 to_copy = FFMIN(nb_samples - dst->nb_samples, src.f->nb_samples);
@@ -500,11 +480,9 @@ static int receive_samples(SyncQueue *sq, SyncQueueStream *st, · 3 unmodified lines
500480
501481 if (to_copy < src.f->nb_samples)
502482 offset_audio(src.f, to_copy);
503− else {
504− av_frame_unref(src.f);
505− objpool_release(sq->pool, (void**)&src);
506− av_fifo_drain2(st->fifo, 1);
507− }
483+ else
484+ av_container_fifo_drain(st->fifo, 1);
485+
508486 st->samples_queued -= to_copy;
509487
510488 dst->nb_samples += to_copy;
@@ -531,7 +509,7 @@ static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx, · 20 unmodified lines
531509 av_assert0(stream_idx < sq->nb_streams);
532510 st = &sq->streams[stream_idx];
533511
534− if (av_fifo_can_read(st->fifo) &&
512+ if (av_container_fifo_can_read(st->fifo) &&
535513 (st->frame_samples <= st->samples_queued || st->finished)) {
536514 int nb_samples = st->frame_samples;
537515 SyncQueueFrame peek;
@@ -541,7 +519,7 @@ static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx, · 3 unmodified lines
541519 if (st->finished)
542520 nb_samples = FFMIN(nb_samples, st->samples_queued);
543521
544− av_fifo_peek(st->fifo, &peek, 1, 0);
522+ av_container_fifo_peek(st->fifo, (void**)&peek, 0);
545523 ts = frame_end(sq, peek, nb_samples);
546524
547525 /* check if this stream's tail timestamp does not overtake
@@ -560,9 +538,9 @@ static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx, · 12 unmodified lines
560538 if (ret < 0)
561539 return ret;
562540 } else {
563− frame_move(sq, frame, peek);
564− objpool_release(sq->pool, (void**)&peek);
565− av_fifo_drain2(st->fifo, 1);
541+ int ret = av_container_fifo_read(st->fifo, SQPTR(sq, frame), 0);
542+ av_assert0(ret >= 0);
543+
566544 av_assert0(st->samples_queued >= frame_samples(sq, frame));
567545 st->samples_queued -= frame_samples(sq, frame);
568546 }
@@ -577,7 +555,7 @@ static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx, · 8 unmodified lines
577555 }
578556 }
579557
580− return (sq->finished || (st->finished && !av_fifo_can_read(st->fifo))) ?
558+ return (sq->finished || (st->finished && !av_container_fifo_can_read(st->fifo))) ?
581559 AVERROR_EOF : AVERROR(EAGAIN);
582560 }
583561
@@ -629,7 +607,8 @@ int sq_add_stream(SyncQueue *sq, int limiting) · 45 unmodified lines
629607 st = &sq->streams[sq->nb_streams];
630608 memset(st, 0, sizeof(*st));
631609
632− st->fifo = av_fifo_alloc2(1, sizeof(SyncQueueFrame), AV_FIFO_FLAG_AUTO_GROW);
610+ st->fifo = (sq->type == SYNC_QUEUE_FRAMES) ?
611+ av_container_fifo_alloc_avframe(0) : av_container_fifo_alloc_avpacket(0);
633612 if (!st->fifo)
634613 return AVERROR(ENOMEM);
635614
@@ -686,13 +665,6 @@ SyncQueue *sq_alloc(enum SyncQueueType type, int64_t buf_size_us, void *logctx) · 50 unmodified lines
686665 sq->head_stream = -1;
687666 sq->head_finished_stream = -1;
688667
689− sq->pool = (type == SYNC_QUEUE_PACKETS) ? objpool_alloc_packets() :
690− objpool_alloc_frames();
691− if (!sq->pool) {
692− av_freep(&sq);
693− return NULL;
694− }
695−
696668 return sq;
697669 }
698670
@@ -703,17 +675,10 @@ void sq_free(SyncQueue **psq) · 4 unmodified lines
703675 if (!sq)
704676 return;
705677
706− for (unsigned int i = 0; i < sq->nb_streams; i++) {
707− SyncQueueFrame frame;
708− while (av_fifo_read(sq->streams[i].fifo, &frame, 1) >= 0)
709− objpool_release(sq->pool, (void**)&frame);
710−
711− av_fifo_freep2(&sq->streams[i].fifo);
712− }
678+ for (unsigned int i = 0; i < sq->nb_streams; i++)
679+ av_container_fifo_free(&sq->streams[i].fifo);
713680
714681 av_freep(&sq->streams);
715682
716− objpool_free(&sq->pool);
717−
718683 av_freep(psq);
719684 }

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