FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/framequeue.c
Date: 2025-07-28 20:30:09
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 2996637 static inline FFFrameBucket *bucket(FFFrameQueue *fq, size_t idx)
27 {
28 2996637 return &fq->queue[(fq->tail + idx) & (fq->allocated - 1)];
29 }
30
31 16118 void ff_framequeue_global_init(FFFrameQueueGlobal *fqg)
32 {
33 16118 fqg->max_queued = SIZE_MAX;
34 16118 fqg->queued = 0;
35 16118 }
36
37 5991718 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 5991718 }
49
50 46591 void ff_framequeue_init(FFFrameQueue *fq, FFFrameQueueGlobal *fqg)
51 {
52 46591 fq->queue = &fq->first_bucket;
53 46591 fq->allocated = 1;
54 46591 fq->global = fqg;
55 46591 }
56
57 46723 void ff_framequeue_free(FFFrameQueue *fq)
58 {
59
2/2
✓ Branch 0 taken 2976 times.
✓ Branch 1 taken 46723 times.
49699 while (fq->queued) {
60 2976 AVFrame *frame = ff_framequeue_take(fq);
61 2976 av_frame_free(&frame);
62 }
63
2/2
✓ Branch 0 taken 235 times.
✓ Branch 1 taken 46488 times.
46723 if (fq->queue != &fq->first_bucket)
64 235 av_freep(&fq->queue);
65 46723 }
66
67 1494980 int ff_framequeue_add(FFFrameQueue *fq, AVFrame *frame)
68 {
69 FFFrameBucket *b;
70
71 1494980 check_consistency(fq);
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1494980 times.
1494980 if (fq->global->queued >= fq->global->max_queued)
73 return AVERROR(ENOMEM);
74
2/2
✓ Branch 0 taken 151 times.
✓ Branch 1 taken 1494829 times.
1494980 if (fq->queued == fq->allocated) {
75
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 48 times.
151 if (fq->allocated == 1) {
76 103 size_t na = 8;
77 103 FFFrameBucket *nq = av_realloc_array(NULL, na, sizeof(*nq));
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103 times.
103 if (!nq)
79 return AVERROR(ENOMEM);
80 103 nq[0] = fq->queue[0];
81 103 fq->queue = nq;
82 103 fq->allocated = na;
83 } else {
84 48 size_t na = fq->allocated << 1;
85 48 FFFrameBucket *nq = av_realloc_array(fq->queue, na, sizeof(*nq));
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (!nq)
87 return AVERROR(ENOMEM);
88
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 45 times.
48 if (fq->tail)
89 3 memmove(nq + fq->allocated, nq, fq->tail * sizeof(*nq));
90 48 fq->queue = nq;
91 48 fq->allocated = na;
92 }
93 }
94 1494980 b = bucket(fq, fq->queued);
95 1494980 b->frame = frame;
96 1494980 fq->queued++;
97 1494980 fq->global->queued++;
98 1494980 fq->total_frames_head++;
99 1494980 fq->total_samples_head += frame->nb_samples;
100 1494980 check_consistency(fq);
101 1494980 return 0;
102 }
103
104 1494980 AVFrame *ff_framequeue_take(FFFrameQueue *fq)
105 {
106 FFFrameBucket *b;
107
108 1494980 check_consistency(fq);
109 av_assert1(fq->queued);
110 1494980 b = bucket(fq, 0);
111 1494980 fq->queued--;
112 1494980 fq->global->queued--;
113 1494980 fq->tail++;
114 1494980 fq->tail &= fq->allocated - 1;
115 1494980 fq->total_frames_tail++;
116 1494980 fq->total_samples_tail += b->frame->nb_samples;
117 1494980 fq->samples_skipped = 0;
118 1494980 check_consistency(fq);
119 1494980 return b->frame;
120 }
121
122 5121 AVFrame *ff_framequeue_peek(FFFrameQueue *fq, size_t idx)
123 {
124 FFFrameBucket *b;
125
126 5121 check_consistency(fq);
127 av_assert1(idx < fq->queued);
128 5121 b = bucket(fq, idx);
129 5121 check_consistency(fq);
130 5121 return b->frame;
131 }
132
133 1556 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 1556 check_consistency(fq);
140 av_assert1(fq->queued);
141 1556 b = bucket(fq, 0);
142 av_assert1(samples < b->frame->nb_samples);
143 1556 planar = av_sample_fmt_is_planar(b->frame->format);
144
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 1519 times.
1556 planes = planar ? b->frame->ch_layout.nb_channels : 1;
145 1556 bytes = samples * av_get_bytes_per_sample(b->frame->format);
146
2/2
✓ Branch 0 taken 1519 times.
✓ Branch 1 taken 37 times.
1556 if (!planar)
147 1519 bytes *= b->frame->ch_layout.nb_channels;
148
1/2
✓ Branch 0 taken 1556 times.
✗ Branch 1 not taken.
1556 if (b->frame->pts != AV_NOPTS_VALUE)
149 1556 b->frame->pts += av_rescale_q(samples, av_make_q(1, b->frame->sample_rate), time_base);
150 1556 b->frame->nb_samples -= samples;
151 1556 b->frame->linesize[0] -= bytes;
152
2/2
✓ Branch 0 taken 1591 times.
✓ Branch 1 taken 1556 times.
3147 for (i = 0; i < planes; i++)
153 1591 b->frame->extended_data[i] += bytes;
154
3/4
✓ Branch 0 taken 1591 times.
✓ Branch 1 taken 1556 times.
✓ Branch 2 taken 1591 times.
✗ Branch 3 not taken.
3147 for (i = 0; i < planes && i < AV_NUM_DATA_POINTERS; i++)
155 1591 b->frame->data[i] = b->frame->extended_data[i];
156 1556 fq->total_samples_tail += samples;
157 1556 fq->samples_skipped = 1;
158 1556 ff_framequeue_update_peeked(fq, 0);
159 1556 }
160