1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-08-04 22:03:09 +02:00

avfilter/framequeue: add support for limiting and tracking buffered frames in the queues

Signed-off-by: Marton Balint <cus@passwd.hu>
This commit is contained in:
Marton Balint
2025-07-06 10:03:46 +02:00
parent 35a6de137a
commit 71468e85ae
2 changed files with 22 additions and 3 deletions

View File

@ -30,6 +30,8 @@ static inline FFFrameBucket *bucket(FFFrameQueue *fq, size_t idx)
void ff_framequeue_global_init(FFFrameQueueGlobal *fqg) void ff_framequeue_global_init(FFFrameQueueGlobal *fqg)
{ {
fqg->max_queued = SIZE_MAX;
fqg->queued = 0;
} }
static void check_consistency(FFFrameQueue *fq) static void check_consistency(FFFrameQueue *fq)
@ -49,6 +51,7 @@ void ff_framequeue_init(FFFrameQueue *fq, FFFrameQueueGlobal *fqg)
{ {
fq->queue = &fq->first_bucket; fq->queue = &fq->first_bucket;
fq->allocated = 1; fq->allocated = 1;
fq->global = fqg;
} }
void ff_framequeue_free(FFFrameQueue *fq) void ff_framequeue_free(FFFrameQueue *fq)
@ -66,6 +69,8 @@ int ff_framequeue_add(FFFrameQueue *fq, AVFrame *frame)
FFFrameBucket *b; FFFrameBucket *b;
check_consistency(fq); check_consistency(fq);
if (fq->global->queued >= fq->global->max_queued)
return AVERROR(ENOMEM);
if (fq->queued == fq->allocated) { if (fq->queued == fq->allocated) {
if (fq->allocated == 1) { if (fq->allocated == 1) {
size_t na = 8; size_t na = 8;
@ -89,6 +94,7 @@ int ff_framequeue_add(FFFrameQueue *fq, AVFrame *frame)
b = bucket(fq, fq->queued); b = bucket(fq, fq->queued);
b->frame = frame; b->frame = frame;
fq->queued++; fq->queued++;
fq->global->queued++;
fq->total_frames_head++; fq->total_frames_head++;
fq->total_samples_head += frame->nb_samples; fq->total_samples_head += frame->nb_samples;
check_consistency(fq); check_consistency(fq);
@ -103,6 +109,7 @@ AVFrame *ff_framequeue_take(FFFrameQueue *fq)
av_assert1(fq->queued); av_assert1(fq->queued);
b = bucket(fq, 0); b = bucket(fq, 0);
fq->queued--; fq->queued--;
fq->global->queued--;
fq->tail++; fq->tail++;
fq->tail &= fq->allocated - 1; fq->tail &= fq->allocated - 1;
fq->total_frames_tail++; fq->total_frames_tail++;

View File

@ -40,11 +40,18 @@ typedef struct FFFrameBucket {
* *
* This structure is intended to allow implementing global control of the * This structure is intended to allow implementing global control of the
* frame queues, including memory consumption caps. * frame queues, including memory consumption caps.
*
* It is currently empty.
*/ */
typedef struct FFFrameQueueGlobal { typedef struct FFFrameQueueGlobal {
char dummy; /* C does not allow empty structs */
/**
* Maximum number of allowed frames in the queues combined.
*/
size_t max_queued;
/**
* Total number of queued frames in the queues combined.
*/
size_t queued;
} FFFrameQueueGlobal; } FFFrameQueueGlobal;
/** /**
@ -52,6 +59,11 @@ typedef struct FFFrameQueueGlobal {
*/ */
typedef struct FFFrameQueue { typedef struct FFFrameQueue {
/**
* Pointer to the global frame queue struct holding statistics and limits
*/
FFFrameQueueGlobal *global;
/** /**
* Array of allocated buckets, used as a circular buffer. * Array of allocated buckets, used as a circular buffer.
*/ */