FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/framepool.c
Date: 2026-04-24 19:58:39
Exec Total Coverage
Lines: 102 131 77.9%
Functions: 6 6 100.0%
Branches: 62 87 71.3%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * Copyright (c) 2015 Matthieu Bouron <matthieu.bouron stupeflix.com>
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "framepool.h"
22 #include "libavutil/avassert.h"
23 #include "libavutil/avutil.h"
24 #include "libavutil/buffer.h"
25 #include "libavutil/frame.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/imgutils_internal.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/pixfmt.h"
30
31 32594 static av_cold int frame_pool_video_init(int width, int height,
32 enum AVPixelFormat format,
33 int align, FFFramePool *pool)
34 {
35 int ret;
36
37 32594 *pool = (FFFramePool) {
38 .type = AVMEDIA_TYPE_VIDEO,
39 .width = width,
40 .height = height,
41 .pix_fmt = format,
42 .align = align,
43 };
44
45
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32594 times.
32594 if ((ret = av_image_check_size2(width, height, INT64_MAX, format, 0, NULL)) < 0)
46 goto fail;
47
48 32594 ret = av_image_fill_linesizes(pool->linesize, pool->pix_fmt,
49 32594 FFALIGN(pool->width, align));
50
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32594 times.
32594 if (ret < 0)
51 goto fail;
52
53
4/4
✓ Branch 0 taken 99654 times.
✓ Branch 1 taken 4063 times.
✓ Branch 2 taken 71123 times.
✓ Branch 3 taken 28531 times.
103717 for (int i = 0; i < 4 && pool->linesize[i]; i++)
54 71123 pool->linesize[i] = FFALIGN(pool->linesize[i], pool->align);
55
56 ptrdiff_t linesizes[4];
57
2/2
✓ Branch 0 taken 130376 times.
✓ Branch 1 taken 32594 times.
162970 for (int i = 0; i < 4; i++)
58 130376 linesizes[i] = pool->linesize[i];
59
60 size_t sizes[4];
61 32594 ret = av_image_fill_plane_sizes(sizes, pool->pix_fmt,
62 32594 FFALIGN(pool->height, align),
63 linesizes);
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32594 times.
32594 if (ret < 0)
65 goto fail;
66
67
4/4
✓ Branch 0 taken 99677 times.
✓ Branch 1 taken 4063 times.
✓ Branch 2 taken 71146 times.
✓ Branch 3 taken 28531 times.
103740 for (int i = 0; i < 4 && sizes[i]; i++) {
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71146 times.
71146 if (sizes[i] > SIZE_MAX - align)
69 goto fail;
70 71146 pool->pools[i] = av_buffer_pool_init(sizes[i] + align,
71 CONFIG_MEMORY_POISONING
72 ? NULL
73 : av_buffer_allocz);
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71146 times.
71146 if (!pool->pools[i]) {
75 ret = AVERROR(ENOMEM);
76 goto fail;
77 }
78 }
79
80 32594 return 0;
81
82 fail:
83 ff_frame_pool_uninit(pool);
84 return ret;
85 }
86
87 13762 static av_cold int frame_pool_audio_init(int channels, int nb_samples,
88 enum AVSampleFormat format,
89 int align, FFFramePool *pool)
90 {
91 int ret;
92
93 13762 int planar = av_sample_fmt_is_planar(format);
94
95 13762 *pool = (FFFramePool) {
96 .type = AVMEDIA_TYPE_AUDIO,
97
2/2
✓ Branch 0 taken 2076 times.
✓ Branch 1 taken 11686 times.
13762 .planes = planar ? channels : 1,
98 .channels = channels,
99 .nb_samples = nb_samples,
100 .sample_fmt = format,
101 .align = align,
102 };
103
104 13762 ret = av_samples_get_buffer_size(&pool->linesize[0], channels,
105 nb_samples, format, 0);
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13762 times.
13762 if (ret < 0)
107 goto fail;
108
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13762 times.
13762 if (pool->linesize[0] > SIZE_MAX - align) {
110 ret = AVERROR(EINVAL);
111 goto fail;
112 }
113
114 13762 pool->pools[0] = av_buffer_pool_init(pool->linesize[0] + align,
115 av_buffer_allocz);
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13762 times.
13762 if (!pool->pools[0]) {
117 ret = AVERROR(ENOMEM);
118 goto fail;
119 }
120
121 13762 return 0;
122
123 fail:
124 ff_frame_pool_uninit(pool);
125 return ret;
126 }
127
128 403572 AVFrame *ff_frame_pool_get(FFFramePool *pool)
129 {
130 const AVPixFmtDescriptor *desc;
131
132 403572 AVFrame *frame = av_frame_alloc();
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 403572 times.
403572 if (!frame)
134 return NULL;
135
136
2/3
✓ Branch 0 taken 126152 times.
✓ Branch 1 taken 277420 times.
✗ Branch 2 not taken.
403572 switch(pool->type) {
137 126152 case AVMEDIA_TYPE_VIDEO:
138 126152 desc = av_pix_fmt_desc_get(pool->pix_fmt);
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126152 times.
126152 if (!desc)
140 goto fail;
141
142 126152 frame->width = pool->width;
143 126152 frame->height = pool->height;
144 126152 frame->format = pool->pix_fmt;
145
146
2/2
✓ Branch 0 taken 418089 times.
✓ Branch 1 taken 3459 times.
421548 for (int i = 0; i < 4; i++) {
147 418089 frame->linesize[i] = pool->linesize[i];
148
2/2
✓ Branch 0 taken 122693 times.
✓ Branch 1 taken 295396 times.
418089 if (!pool->pools[i])
149 122693 break;
150
151 295396 frame->buf[i] = av_buffer_pool_get(pool->pools[i]);
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 295396 times.
295396 if (!frame->buf[i])
153 goto fail;
154
155 295396 frame->data[i] = (uint8_t *)FFALIGN((uintptr_t)frame->buf[i]->data, pool->align);
156 }
157
158
2/2
✓ Branch 0 taken 877 times.
✓ Branch 1 taken 125275 times.
126152 if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
159 877 enum AVPixelFormat format = pool->pix_fmt;
160
1/2
✓ Branch 0 taken 877 times.
✗ Branch 1 not taken.
877 if (format == AV_PIX_FMT_PAL8)
161 877 format = AV_PIX_FMT_BGR8;
162
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 877 times.
877 av_assert0(frame->data[1] != NULL);
164
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 877 times.
877 if (avpriv_set_systematic_pal2((uint32_t *)frame->data[1], format) < 0)
165 goto fail;
166 }
167
168 126152 frame->extended_data = frame->data;
169 126152 break;
170 277420 case AVMEDIA_TYPE_AUDIO:
171 277420 frame->nb_samples = pool->nb_samples;
172 277420 frame->ch_layout.nb_channels = pool->channels;
173 277420 frame->format = pool->sample_fmt;
174 277420 frame->linesize[0] = pool->linesize[0];
175
176
2/2
✓ Branch 0 taken 516 times.
✓ Branch 1 taken 276904 times.
277420 if (pool->planes > AV_NUM_DATA_POINTERS) {
177 516 frame->extended_data = av_calloc(pool->planes,
178 sizeof(*frame->extended_data));
179 516 frame->nb_extended_buf = pool->planes - AV_NUM_DATA_POINTERS;
180 516 frame->extended_buf = av_calloc(frame->nb_extended_buf,
181 sizeof(*frame->extended_buf));
182
2/4
✓ Branch 0 taken 516 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 516 times.
516 if (!frame->extended_data || !frame->extended_buf)
183 goto fail;
184 } else {
185 276904 frame->extended_data = frame->data;
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 276904 times.
276904 av_assert0(frame->nb_extended_buf == 0);
187 }
188
189
2/2
✓ Branch 0 taken 286899 times.
✓ Branch 1 taken 277420 times.
564319 for (int i = 0; i < FFMIN(pool->planes, AV_NUM_DATA_POINTERS); i++) {
190 286899 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 286899 times.
286899 if (!frame->buf[i])
192 goto fail;
193 286899 frame->extended_data[i] = frame->data[i] =
194 286899 (uint8_t *)FFALIGN((uintptr_t)frame->buf[i]->data, pool->align);
195 }
196
2/2
✓ Branch 0 taken 1776 times.
✓ Branch 1 taken 277420 times.
279196 for (int i = 0; i < frame->nb_extended_buf; i++) {
197 1776 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1776 times.
1776 if (!frame->extended_buf[i])
199 goto fail;
200 1776 frame->extended_data[i + AV_NUM_DATA_POINTERS] =
201 1776 (uint8_t *)FFALIGN((uintptr_t)frame->extended_buf[i]->data, pool->align);
202 }
203
204 277420 break;
205 default:
206 av_unreachable("only audio and video frame pools exist");
207 }
208
209 403572 return frame;
210 fail:
211 av_frame_free(&frame);
212 return NULL;
213 }
214
215 170213 av_cold void ff_frame_pool_uninit(FFFramePool *pool)
216 {
217
2/2
✓ Branch 0 taken 680852 times.
✓ Branch 1 taken 170213 times.
851065 for (int i = 0; i < 4; i++)
218 680852 av_buffer_pool_uninit(&pool->pools[i]);
219
220 170213 memset(pool, 0, sizeof(*pool));
221 170213 }
222
223 282494 int ff_frame_pool_video_reinit(FFFramePool *pool,
224 int width,
225 int height,
226 enum AVPixelFormat format,
227 int align)
228 {
229
1/2
✓ Branch 0 taken 282494 times.
✗ Branch 1 not taken.
282494 if (pool->type == AVMEDIA_TYPE_VIDEO &&
230
2/2
✓ Branch 0 taken 251008 times.
✓ Branch 1 taken 31486 times.
282494 pool->pix_fmt == format &&
231
2/2
✓ Branch 0 taken 249903 times.
✓ Branch 1 taken 1105 times.
251008 FFALIGN(pool->width, pool->align) == FFALIGN(width, align) &&
232
2/2
✓ Branch 0 taken 249900 times.
✓ Branch 1 taken 3 times.
249903 FFALIGN(pool->height, pool->align) == FFALIGN(height, align) &&
233
1/2
✓ Branch 0 taken 249900 times.
✗ Branch 1 not taken.
249900 pool->align == align)
234 {
235 249900 pool->width = width;
236 249900 pool->height = height;
237 249900 return 0;
238 }
239
240 32594 ff_frame_pool_uninit(pool);
241 32594 return frame_pool_video_init(width, height, format, align, pool);
242 }
243
244 277420 int ff_frame_pool_audio_reinit(FFFramePool *pool,
245 int channels,
246 int nb_samples,
247 enum AVSampleFormat format,
248 int align)
249 {
250
2/2
✓ Branch 0 taken 275674 times.
✓ Branch 1 taken 1746 times.
277420 if (pool->type == AVMEDIA_TYPE_AUDIO &&
251
1/2
✓ Branch 0 taken 275674 times.
✗ Branch 1 not taken.
275674 pool->sample_fmt == format &&
252
1/2
✓ Branch 0 taken 275674 times.
✗ Branch 1 not taken.
275674 pool->channels == channels &&
253
2/2
✓ Branch 0 taken 263658 times.
✓ Branch 1 taken 12016 times.
275674 pool->nb_samples == nb_samples &&
254
1/2
✓ Branch 0 taken 263658 times.
✗ Branch 1 not taken.
263658 pool->align == align)
255 {
256 263658 return 0;
257 }
258
259 13762 ff_frame_pool_uninit(pool);
260 13762 return frame_pool_audio_init(channels, nb_samples, format, align, pool);
261 }
262