FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/framequeue.c
Date: 2025-09-17 22:08:16
Exec Total Coverage
Lines: 83 86 96.5%
Functions: 9 9 100.0%
Branches: 23 28 82.1%

Line Branch Exec Source
1 /*
2 * Generic frame queue
3 * Copyright (c) 2016 Nicolas George
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/avassert.h"
23 #include "libavutil/mem.h"
24 #include "framequeue.h"
25
26 3010630 static inline FFFrameBucket *bucket(FFFrameQueue *fq, size_t idx)
27 {
28 3010630 return &fq->queue[(fq->tail + idx) & (fq->allocated - 1)];
29 }
30
31 16159 void ff_framequeue_global_init(FFFrameQueueGlobal *fqg)
32 {
33 16159 fqg->max_queued = SIZE_MAX;
34 16159 fqg->queued = 0;
35 16159 }
36
37 6019745 static void check_consistency(FFFrameQueue *fq)
38 {
39 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
40 uint64_t nb_samples = 0;
41 size_t i;
42
43 av_assert0(fq->queued == fq->total_frames_head - fq->total_frames_tail);
44 for (i = 0; i < fq->queued; i++)
45 nb_samples += bucket(fq, i)->frame->nb_samples;
46 av_assert0(nb_samples == fq->total_samples_head - fq->total_samples_tail);
47 #endif
48 6019745 }
49
50 46649 void ff_framequeue_init(FFFrameQueue *fq, FFFrameQueueGlobal *fqg)
51 {
52 46649 fq->queue = &fq->first_bucket;
53 46649 fq->allocated = 1;
54 46649 fq->global = fqg;
55 46649 }
56
57 46781 void ff_framequeue_free(FFFrameQueue *fq)
58 {
59
2/2
✓ Branch 0 taken 2978 times.
✓ Branch 1 taken 46781 times.
49759 while (fq->queued) {
60 2978 AVFrame *frame = ff_framequeue_take(fq);
61 2978 av_frame_free(&frame);
62 }
63
2/2
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 46545 times.
46781 if (fq->queue != &fq->first_bucket)
64 236 av_freep(&fq->queue);
65 46781 }
66
67 1501925 int ff_framequeue_add(FFFrameQueue *fq, AVFrame *frame)
68 {
69 FFFrameBucket *b;
70
71 1501925 check_consistency(fq);
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1501925 times.
1501925 if (fq->global->queued >= fq->global->max_queued)
73 return AVERROR(ENOMEM);
74
2/2
✓ Branch 0 taken 156 times.
✓ Branch 1 taken 1501769 times.
1501925 if (fq->queued == fq->allocated) {
75
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 52 times.
156 if (fq->allocated == 1) {
76 104 size_t na = 8;
77 104 FFFrameBucket *nq = av_realloc_array(NULL, na, sizeof(*nq));
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 if (!nq)
79 return AVERROR(ENOMEM);
80 104 nq[0] = fq->queue[0];
81 104 fq->queue = nq;
82 104 fq->allocated = na;
83 } else {
84 52 size_t na = fq->allocated << 1;
85 52 FFFrameBucket *nq = av_realloc_array(fq->queue, na, sizeof(*nq));
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (!nq)
87 return AVERROR(ENOMEM);
88
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 48 times.
52 if (fq->tail)
89 4 memmove(nq + fq->allocated, nq, fq->tail * sizeof(*nq));
90 52 fq->queue = nq;
91 52 fq->allocated = na;
92 }
93 }
94 1501925 b = bucket(fq, fq->queued);
95 1501925 b->frame = frame;
96 1501925 fq->queued++;
97 1501925 fq->global->queued++;
98 1501925 fq->total_frames_head++;
99 1501925 fq->total_samples_head += frame->nb_samples;
100 1501925 check_consistency(fq);
101 1501925 return 0;
102 }
103
104 1501925 AVFrame *ff_framequeue_take(FFFrameQueue *fq)
105 {
106 FFFrameBucket *b;
107
108 1501925 check_consistency(fq);
109 av_assert1(fq->queued);
110 1501925 b = bucket(fq, 0);
111 1501925 fq->queued--;
112 1501925 fq->global->queued--;
113 1501925 fq->tail++;
114 1501925 fq->tail &= fq->allocated - 1;
115 1501925 fq->total_frames_tail++;
116 1501925 fq->total_samples_tail += b->frame->nb_samples;
117 1501925 fq->samples_skipped = 0;
118 1501925 check_consistency(fq);
119 1501925 return b->frame;
120 }
121
122 5265 AVFrame *ff_framequeue_peek(FFFrameQueue *fq, size_t idx)
123 {
124 FFFrameBucket *b;
125
126 5265 check_consistency(fq);
127 av_assert1(idx < fq->queued);
128 5265 b = bucket(fq, idx);
129 5265 check_consistency(fq);
130 5265 return b->frame;
131 }
132
133 1515 void ff_framequeue_skip_samples(FFFrameQueue *fq, size_t samples, AVRational time_base)
134 {
135 FFFrameBucket *b;
136 size_t bytes;
137 int planar, planes, i;
138
139 1515 check_consistency(fq);
140 av_assert1(fq->queued);
141 1515 b = bucket(fq, 0);
142 av_assert1(samples < b->frame->nb_samples);
143 1515 planar = av_sample_fmt_is_planar(b->frame->format);
144
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 1478 times.
1515 planes = planar ? b->frame->ch_layout.nb_channels : 1;
145 1515 bytes = samples * av_get_bytes_per_sample(b->frame->format);
146
2/2
✓ Branch 0 taken 1478 times.
✓ Branch 1 taken 37 times.
1515 if (!planar)
147 1478 bytes *= b->frame->ch_layout.nb_channels;
148
1/2
✓ Branch 0 taken 1515 times.
✗ Branch 1 not taken.
1515 if (b->frame->pts != AV_NOPTS_VALUE)
149 1515 b->frame->pts += av_rescale_q(samples, av_make_q(1, b->frame->sample_rate), time_base);
150 1515 b->frame->nb_samples -= samples;
151 1515 b->frame->linesize[0] -= bytes;
152
2/2
✓ Branch 0 taken 1550 times.
✓ Branch 1 taken 1515 times.
3065 for (i = 0; i < planes; i++)
153 1550 b->frame->extended_data[i] += bytes;
154
3/4
✓ Branch 0 taken 1550 times.
✓ Branch 1 taken 1515 times.
✓ Branch 2 taken 1550 times.
✗ Branch 3 not taken.
3065 for (i = 0; i < planes && i < AV_NUM_DATA_POINTERS; i++)
155 1550 b->frame->data[i] = b->frame->extended_data[i];
156 1515 fq->total_samples_tail += samples;
157 1515 fq->samples_skipped = 1;
158 1515 ff_framequeue_update_peeked(fq, 0);
159 1515 }
160