FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/framepool.c
Date: 2026-08-15 14:54:27
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 54196 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 54196 *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 54196 times.
54196 if ((ret = av_image_check_size2(width, height, INT64_MAX, format, 0, NULL)) < 0)
46 goto fail;
47
48 54196 ret = av_image_fill_linesizes(pool->linesize, pool->pix_fmt,
49 54196 FFALIGN(pool->width, align));
50
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54196 times.
54196 if (ret < 0)
51 goto fail;
52
53
4/4
✓ Branch 0 taken 159118 times.
✓ Branch 1 taken 7316 times.
✓ Branch 2 taken 112238 times.
✓ Branch 3 taken 46880 times.
166434 for (int i = 0; i < 4 && pool->linesize[i]; i++)
54 112238 pool->linesize[i] = FFALIGN(pool->linesize[i], pool->align);
55
56 ptrdiff_t linesizes[4];
57
2/2
✓ Branch 0 taken 216784 times.
✓ Branch 1 taken 54196 times.
270980 for (int i = 0; i < 4; i++)
58 216784 linesizes[i] = pool->linesize[i];
59
60 size_t sizes[4];
61 54196 ret = av_image_fill_plane_sizes(sizes, pool->pix_fmt,
62 54196 FFALIGN(pool->height, align),
63 linesizes);
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54196 times.
54196 if (ret < 0)
65 goto fail;
66
67
4/4
✓ Branch 0 taken 159147 times.
✓ Branch 1 taken 7316 times.
✓ Branch 2 taken 112267 times.
✓ Branch 3 taken 46880 times.
166463 for (int i = 0; i < 4 && sizes[i]; i++) {
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112267 times.
112267 if (sizes[i] > SIZE_MAX - align)
69 goto fail;
70 112267 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 112267 times.
112267 if (!pool->pools[i]) {
75 ret = AVERROR(ENOMEM);
76 goto fail;
77 }
78 }
79
80 54196 return 0;
81
82 fail:
83 ff_frame_pool_uninit(pool);
84 return ret;
85 }
86
87 13915 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 13915 int planar = av_sample_fmt_is_planar(format);
94
95 13915 *pool = (FFFramePool) {
96 .type = AVMEDIA_TYPE_AUDIO,
97
2/2
✓ Branch 0 taken 2089 times.
✓ Branch 1 taken 11826 times.
13915 .planes = planar ? channels : 1,
98 .channels = channels,
99 .nb_samples = nb_samples,
100 .sample_fmt = format,
101 .align = align,
102 };
103
104 13915 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 13915 times.
13915 if (ret < 0)
107 goto fail;
108
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13915 times.
13915 if (pool->linesize[0] > SIZE_MAX - align) {
110 ret = AVERROR(EINVAL);
111 goto fail;
112 }
113
114 13915 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 13915 times.
13915 if (!pool->pools[0]) {
117 ret = AVERROR(ENOMEM);
118 goto fail;
119 }
120
121 13915 return 0;
122
123 fail:
124 ff_frame_pool_uninit(pool);
125 return ret;
126 }
127
128 411690 AVFrame *ff_frame_pool_get(FFFramePool *pool)
129 {
130 const AVPixFmtDescriptor *desc;
131
132 411690 AVFrame *frame = av_frame_alloc();
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 411690 times.
411690 if (!frame)
134 return NULL;
135
136
2/3
✓ Branch 0 taken 126596 times.
✓ Branch 1 taken 285094 times.
✗ Branch 2 not taken.
411690 switch(pool->type) {
137 126596 case AVMEDIA_TYPE_VIDEO:
138 126596 desc = av_pix_fmt_desc_get(pool->pix_fmt);
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126596 times.
126596 if (!desc)
140 goto fail;
141
142 126596 frame->width = pool->width;
143 126596 frame->height = pool->height;
144 126596 frame->format = pool->pix_fmt;
145
146
2/2
✓ Branch 0 taken 419606 times.
✓ Branch 1 taken 3461 times.
423067 for (int i = 0; i < 4; i++) {
147 419606 frame->linesize[i] = pool->linesize[i];
148
2/2
✓ Branch 0 taken 123135 times.
✓ Branch 1 taken 296471 times.
419606 if (!pool->pools[i])
149 123135 break;
150
151 296471 frame->buf[i] = av_buffer_pool_get(pool->pools[i]);
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 296471 times.
296471 if (!frame->buf[i])
153 goto fail;
154
155 296471 frame->data[i] = (uint8_t *)FFALIGN((uintptr_t)frame->buf[i]->data, pool->align);
156 }
157
158
2/2
✓ Branch 0 taken 913 times.
✓ Branch 1 taken 125683 times.
126596 if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
159 913 enum AVPixelFormat format = pool->pix_fmt;
160
1/2
✓ Branch 0 taken 913 times.
✗ Branch 1 not taken.
913 if (format == AV_PIX_FMT_PAL8)
161 913 format = AV_PIX_FMT_BGR8;
162
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 913 times.
913 av_assert0(frame->data[1] != NULL);
164
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 913 times.
913 if (avpriv_set_systematic_pal2((uint32_t *)frame->data[1], format) < 0)
165 goto fail;
166 }
167
168 126596 frame->extended_data = frame->data;
169 126596 break;
170 285094 case AVMEDIA_TYPE_AUDIO:
171 285094 frame->nb_samples = pool->nb_samples;
172 285094 frame->ch_layout.nb_channels = pool->channels;
173 285094 frame->format = pool->sample_fmt;
174 285094 frame->linesize[0] = pool->linesize[0];
175
176
2/2
✓ Branch 0 taken 516 times.
✓ Branch 1 taken 284578 times.
285094 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 284578 frame->extended_data = frame->data;
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 284578 times.
284578 av_assert0(frame->nb_extended_buf == 0);
187 }
188
189
2/2
✓ Branch 0 taken 294726 times.
✓ Branch 1 taken 285094 times.
579820 for (int i = 0; i < FFMIN(pool->planes, AV_NUM_DATA_POINTERS); i++) {
190 294726 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 294726 times.
294726 if (!frame->buf[i])
192 goto fail;
193 294726 frame->extended_data[i] = frame->data[i] =
194 294726 (uint8_t *)FFALIGN((uintptr_t)frame->buf[i]->data, pool->align);
195 }
196
2/2
✓ Branch 0 taken 1776 times.
✓ Branch 1 taken 285094 times.
286870 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 285094 break;
205 default:
206 av_unreachable("only audio and video frame pools exist");
207 }
208
209 411690 return frame;
210 fail:
211 av_frame_free(&frame);
212 return NULL;
213 }
214
215 205126 av_cold void ff_frame_pool_uninit(FFFramePool *pool)
216 {
217
2/2
✓ Branch 0 taken 820504 times.
✓ Branch 1 taken 205126 times.
1025630 for (int i = 0; i < 4; i++)
218 820504 av_buffer_pool_uninit(&pool->pools[i]);
219
220 205126 memset(pool, 0, sizeof(*pool));
221 205126 }
222
223 351894 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 351894 times.
✗ Branch 1 not taken.
351894 if (pool->type == AVMEDIA_TYPE_VIDEO &&
230
2/2
✓ Branch 0 taken 307107 times.
✓ Branch 1 taken 44787 times.
351894 pool->pix_fmt == format &&
231
2/2
✓ Branch 0 taken 299516 times.
✓ Branch 1 taken 7591 times.
307107 FFALIGN(pool->width, pool->align) == FFALIGN(width, align) &&
232
2/2
✓ Branch 0 taken 297698 times.
✓ Branch 1 taken 1818 times.
299516 FFALIGN(pool->height, pool->align) == FFALIGN(height, align) &&
233
1/2
✓ Branch 0 taken 297698 times.
✗ Branch 1 not taken.
297698 pool->align == align)
234 {
235 297698 pool->width = width;
236 297698 pool->height = height;
237 297698 return 0;
238 }
239
240 54196 ff_frame_pool_uninit(pool);
241 54196 return frame_pool_video_init(width, height, format, align, pool);
242 }
243
244 285094 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 283314 times.
✓ Branch 1 taken 1780 times.
285094 if (pool->type == AVMEDIA_TYPE_AUDIO &&
251
1/2
✓ Branch 0 taken 283314 times.
✗ Branch 1 not taken.
283314 pool->sample_fmt == format &&
252
1/2
✓ Branch 0 taken 283314 times.
✗ Branch 1 not taken.
283314 pool->channels == channels &&
253
2/2
✓ Branch 0 taken 271179 times.
✓ Branch 1 taken 12135 times.
283314 pool->nb_samples == nb_samples &&
254
1/2
✓ Branch 0 taken 271179 times.
✗ Branch 1 not taken.
271179 pool->align == align)
255 {
256 271179 return 0;
257 }
258
259 13915 ff_frame_pool_uninit(pool);
260 13915 return frame_pool_audio_init(channels, nb_samples, format, align, pool);
261 }
262