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

Unified · Split

4 files
  1. fftools/Makefile +0 −1
  2. fftools/objpool.c +0 −131
  3. fftools/objpool.h +0 −37
  4. fftools/sync_queue.c +29 −64
fftools/Makefile+0−1
@@ -19,7 +19,6 @@ OBJS-ffmpeg += \ · 18 unmodified lines
1919 fftools/ffmpeg_mux_init.o \
2020 fftools/ffmpeg_opt.o \
2121 fftools/ffmpeg_sched.o \
22− fftools/objpool.o \
2322 fftools/sync_queue.o \
2423 fftools/thread_queue.o \
2524
fftools/objpool.c+0−131
@@ -1,131 +0,0 @@
1−/*
2− * This file is part of FFmpeg.
3− *
4− * FFmpeg is free software; you can redistribute it and/or
5− * modify it under the terms of the GNU Lesser General Public
6− * License as published by the Free Software Foundation; either
7− * version 2.1 of the License, or (at your option) any later version.
8− *
9− * FFmpeg is distributed in the hope that it will be useful,
10− * but WITHOUT ANY WARRANTY; without even the implied warranty of
11− * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12− * Lesser General Public License for more details.
13− *
14− * You should have received a copy of the GNU Lesser General Public
15− * License along with FFmpeg; if not, write to the Free Software
16− * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17− */
18−
19−#include <stdint.h>
20−
21−#include "libavcodec/packet.h"
22−
23−#include "libavutil/common.h"
24−#include "libavutil/error.h"
25−#include "libavutil/frame.h"
26−#include "libavutil/mem.h"
27−
28−#include "objpool.h"
29−
30−struct ObjPool {
31− void *pool[32];
32− unsigned int pool_count;
33−
34− ObjPoolCBAlloc alloc;
35− ObjPoolCBReset reset;
36− ObjPoolCBFree free;
37−};
38−
39−ObjPool *objpool_alloc(ObjPoolCBAlloc cb_alloc, ObjPoolCBReset cb_reset,
40− ObjPoolCBFree cb_free)
41−{
42− ObjPool *op = av_mallocz(sizeof(*op));
43−
44− if (!op)
45− return NULL;
46−
47− op->alloc = cb_alloc;
48− op->reset = cb_reset;
49− op->free = cb_free;
50−
51− return op;
52−}
53−
54−void objpool_free(ObjPool **pop)
55−{
56− ObjPool *op = *pop;
57−
58− if (!op)
59− return;
60−
61− for (unsigned int i = 0; i < op->pool_count; i++)
62− op->free(&op->pool[i]);
63−
64− av_freep(pop);
65−}
66−
67−int objpool_get(ObjPool *op, void **obj)
68−{
69− if (op->pool_count) {
70− *obj = op->pool[--op->pool_count];
71− op->pool[op->pool_count] = NULL;
72− } else
73− *obj = op->alloc();
74−
75− return *obj ? 0 : AVERROR(ENOMEM);
76−}
77−
78−void objpool_release(ObjPool *op, void **obj)
79−{
80− if (!*obj)
81− return;
82−
83− op->reset(*obj);
84−
85− if (op->pool_count < FF_ARRAY_ELEMS(op->pool))
86− op->pool[op->pool_count++] = *obj;
87− else
88− op->free(obj);
89−
90− *obj = NULL;
91−}
92−
93−static void *alloc_packet(void)
94−{
95− return av_packet_alloc();
96−}
97−static void *alloc_frame(void)
98−{
99− return av_frame_alloc();
100−}
101−
102−static void reset_packet(void *obj)
103−{
104− av_packet_unref(obj);
105−}
106−static void reset_frame(void *obj)
107−{
108− av_frame_unref(obj);
109−}
110−
111−static void free_packet(void **obj)
112−{
113− AVPacket *pkt = *obj;
114− av_packet_free(&pkt);
115− *obj = NULL;
116−}
117−static void free_frame(void **obj)
118−{
119− AVFrame *frame = *obj;
120− av_frame_free(&frame);
121− *obj = NULL;
122−}
123−
124−ObjPool *objpool_alloc_packets(void)
125−{
126− return objpool_alloc(alloc_packet, reset_packet, free_packet);
127−}
128−ObjPool *objpool_alloc_frames(void)
129−{
130− return objpool_alloc(alloc_frame, reset_frame, free_frame);
131−}
fftools/objpool.h+0−37
@@ -1,37 +0,0 @@
1−/*
2− * This file is part of FFmpeg.
3− *
4− * FFmpeg is free software; you can redistribute it and/or
5− * modify it under the terms of the GNU Lesser General Public
6− * License as published by the Free Software Foundation; either
7− * version 2.1 of the License, or (at your option) any later version.
8− *
9− * FFmpeg is distributed in the hope that it will be useful,
10− * but WITHOUT ANY WARRANTY; without even the implied warranty of
11− * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12− * Lesser General Public License for more details.
13− *
14− * You should have received a copy of the GNU Lesser General Public
15− * License along with FFmpeg; if not, write to the Free Software
16− * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17− */
18−
19−#ifndef FFTOOLS_OBJPOOL_H
20−#define FFTOOLS_OBJPOOL_H
21−
22−typedef struct ObjPool ObjPool;
23−
24−typedef void* (*ObjPoolCBAlloc)(void);
25−typedef void (*ObjPoolCBReset)(void *);
26−typedef void (*ObjPoolCBFree)(void **);
27−
28−void objpool_free(ObjPool **op);
29−ObjPool *objpool_alloc(ObjPoolCBAlloc cb_alloc, ObjPoolCBReset cb_reset,
30− ObjPoolCBFree cb_free);
31−ObjPool *objpool_alloc_packets(void);
32−ObjPool *objpool_alloc_frames(void);
33−
34−int objpool_get(ObjPool *op, void **obj);
35−void objpool_release(ObjPool *op, void **obj);
36−
37−#endif // FFTOOLS_OBJPOOL_H
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