FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/get_buffer.c
Date: 2024-07-26 21:54:09
Exec Total Coverage
Lines: 94 146 64.4%
Functions: 5 5 100.0%
Branches: 62 96 64.6%

Line Branch Exec Source
1 /*
2 * The default get_buffer2() implementation
3 *
4 * This file is part of FFmpeg.
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 <stdint.h>
22
23 #include "libavutil/avassert.h"
24 #include "libavutil/avutil.h"
25 #include "libavutil/buffer.h"
26 #include "libavutil/frame.h"
27 #include "libavutil/hwcontext.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/samplefmt.h"
31 #include "libavutil/version.h"
32
33 #include "avcodec.h"
34 #include "internal.h"
35 #include "refstruct.h"
36
37 typedef struct FramePool {
38 /**
39 * Pools for each data plane. For audio all the planes have the same size,
40 * so only pools[0] is used.
41 */
42 AVBufferPool *pools[4];
43
44 /*
45 * Pool parameters
46 */
47 int format;
48 int width, height;
49 int stride_align[AV_NUM_DATA_POINTERS];
50 int linesize[4];
51 int planes;
52 int channels;
53 int samples;
54 } FramePool;
55
56 14960 static void frame_pool_free(FFRefStructOpaque unused, void *obj)
57 {
58 14960 FramePool *pool = obj;
59 int i;
60
61
2/2
✓ Branch 0 taken 59840 times.
✓ Branch 1 taken 14960 times.
74800 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
62 59840 av_buffer_pool_uninit(&pool->pools[i]);
63 14960 }
64
65 382605 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
66 {
67 382605 FramePool *pool = avctx->internal->pool;
68 int i, ret;
69
70
4/4
✓ Branch 0 taken 374431 times.
✓ Branch 1 taken 8174 times.
✓ Branch 2 taken 374251 times.
✓ Branch 3 taken 180 times.
382605 if (pool && pool->format == frame->format) {
71
2/2
✓ Branch 0 taken 115052 times.
✓ Branch 1 taken 259199 times.
374251 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
72
4/4
✓ Branch 0 taken 114826 times.
✓ Branch 1 taken 226 times.
✓ Branch 2 taken 114813 times.
✓ Branch 3 taken 13 times.
115052 pool->width == frame->width && pool->height == frame->height)
73 114813 return 0;
74
2/2
✓ Branch 0 taken 259199 times.
✓ Branch 1 taken 239 times.
259438 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
75
2/2
✓ Branch 0 taken 259186 times.
✓ Branch 1 taken 13 times.
259199 pool->channels == frame->ch_layout.nb_channels &&
76
2/2
✓ Branch 0 taken 252832 times.
✓ Branch 1 taken 6354 times.
259186 frame->nb_samples == pool->samples)
77 252832 return 0;
78 }
79
80 14960 pool = ff_refstruct_alloc_ext(sizeof(*pool), 0, NULL, frame_pool_free);
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14960 times.
14960 if (!pool)
82 return AVERROR(ENOMEM);
83
84
2/3
✓ Branch 0 taken 6686 times.
✓ Branch 1 taken 8274 times.
✗ Branch 2 not taken.
14960 switch (avctx->codec_type) {
85 6686 case AVMEDIA_TYPE_VIDEO: {
86 int linesize[4];
87 6686 int w = frame->width;
88 6686 int h = frame->height;
89 int unaligned;
90 ptrdiff_t linesize1[4];
91 size_t size[4];
92
93 6686 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
94
95 do {
96 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
97 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
98 13329 ret = av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13329 times.
13329 if (ret < 0)
100 goto fail;
101 // increase alignment of w for next try (rhs gives the lowest bit set in w)
102 13329 w += w & ~(w - 1);
103
104 13329 unaligned = 0;
105
2/2
✓ Branch 0 taken 53316 times.
✓ Branch 1 taken 13329 times.
66645 for (i = 0; i < 4; i++)
106 53316 unaligned |= linesize[i] % pool->stride_align[i];
107
2/2
✓ Branch 0 taken 6643 times.
✓ Branch 1 taken 6686 times.
13329 } while (unaligned);
108
109
2/2
✓ Branch 0 taken 26744 times.
✓ Branch 1 taken 6686 times.
33430 for (i = 0; i < 4; i++)
110 26744 linesize1[i] = linesize[i];
111 6686 ret = av_image_fill_plane_sizes(size, avctx->pix_fmt, h, linesize1);
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6686 times.
6686 if (ret < 0)
113 goto fail;
114
115
2/2
✓ Branch 0 taken 26744 times.
✓ Branch 1 taken 6686 times.
33430 for (i = 0; i < 4; i++) {
116 26744 pool->linesize[i] = linesize[i];
117
2/2
✓ Branch 0 taken 18507 times.
✓ Branch 1 taken 8237 times.
26744 if (size[i]) {
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18507 times.
18507 if (size[i] > INT_MAX - (16 + STRIDE_ALIGN - 1)) {
119 ret = AVERROR(EINVAL);
120 goto fail;
121 }
122 18507 pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
123 CONFIG_MEMORY_POISONING ?
124 NULL :
125 av_buffer_allocz);
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18507 times.
18507 if (!pool->pools[i]) {
127 ret = AVERROR(ENOMEM);
128 goto fail;
129 }
130 }
131 }
132 6686 pool->format = frame->format;
133 6686 pool->width = frame->width;
134 6686 pool->height = frame->height;
135
136 6686 break;
137 }
138 8274 case AVMEDIA_TYPE_AUDIO: {
139 8274 ret = av_samples_get_buffer_size(&pool->linesize[0],
140 frame->ch_layout.nb_channels,
141 8274 frame->nb_samples, frame->format, 0);
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8274 times.
8274 if (ret < 0)
143 goto fail;
144
145 8274 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8274 times.
8274 if (!pool->pools[0]) {
147 ret = AVERROR(ENOMEM);
148 goto fail;
149 }
150
151 8274 pool->format = frame->format;
152 8274 pool->channels = frame->ch_layout.nb_channels;
153 8274 pool->samples = frame->nb_samples;
154
2/2
✓ Branch 1 taken 5476 times.
✓ Branch 2 taken 2798 times.
8274 pool->planes = av_sample_fmt_is_planar(pool->format) ? pool->channels : 1;
155 8274 break;
156 }
157 default: av_assert0(0);
158 }
159
160 14960 ff_refstruct_unref(&avctx->internal->pool);
161 14960 avctx->internal->pool = pool;
162
163 14960 return 0;
164 fail:
165 ff_refstruct_unref(&pool);
166 return ret;
167 }
168
169 261106 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
170 {
171 261106 FramePool *pool = avctx->internal->pool;
172 261106 int planes = pool->planes;
173 int i;
174
175 261106 frame->linesize[0] = pool->linesize[0];
176
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 261106 times.
261106 if (planes > AV_NUM_DATA_POINTERS) {
178 frame->extended_data = av_calloc(planes, sizeof(*frame->extended_data));
179 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
180 frame->extended_buf = av_calloc(frame->nb_extended_buf,
181 sizeof(*frame->extended_buf));
182 if (!frame->extended_data || !frame->extended_buf) {
183 av_freep(&frame->extended_data);
184 av_freep(&frame->extended_buf);
185 return AVERROR(ENOMEM);
186 }
187 } else {
188 261106 frame->extended_data = frame->data;
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 261106 times.
261106 av_assert0(frame->nb_extended_buf == 0);
190 }
191
192
2/2
✓ Branch 0 taken 408245 times.
✓ Branch 1 taken 261106 times.
669351 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
193 408245 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 408245 times.
408245 if (!frame->buf[i])
195 goto fail;
196 408245 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
197 }
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 261106 times.
261106 for (i = 0; i < frame->nb_extended_buf; i++) {
199 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
200 if (!frame->extended_buf[i])
201 goto fail;
202 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
203 }
204
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 261106 times.
261106 if (avctx->debug & FF_DEBUG_BUFFERS)
206 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
207
208 261106 return 0;
209 fail:
210 av_frame_unref(frame);
211 return AVERROR(ENOMEM);
212 }
213
214 121499 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
215 {
216 121499 FramePool *pool = s->internal->pool;
217 int i;
218
219
4/8
✓ Branch 0 taken 121499 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 121499 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 121499 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 121499 times.
121499 if (pic->data[0] || pic->data[1] || pic->data[2] || pic->data[3]) {
220 av_log(s, AV_LOG_ERROR, "pic->data[*]!=NULL in avcodec_default_get_buffer\n");
221 return -1;
222 }
223
224 121499 memset(pic->data, 0, sizeof(pic->data));
225 121499 pic->extended_data = pic->data;
226
227
4/4
✓ Branch 0 taken 463597 times.
✓ Branch 1 taken 891 times.
✓ Branch 2 taken 342989 times.
✓ Branch 3 taken 120608 times.
464488 for (i = 0; i < 4 && pool->pools[i]; i++) {
228 342989 pic->linesize[i] = pool->linesize[i];
229
230 342989 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 342989 times.
342989 if (!pic->buf[i])
232 goto fail;
233
234 342989 pic->data[i] = pic->buf[i]->data;
235 }
236
2/2
✓ Branch 0 taken 629003 times.
✓ Branch 1 taken 121499 times.
750502 for (; i < AV_NUM_DATA_POINTERS; i++) {
237 629003 pic->data[i] = NULL;
238 629003 pic->linesize[i] = 0;
239 }
240
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 121499 times.
121499 if (s->debug & FF_DEBUG_BUFFERS)
242 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
243
244 121499 return 0;
245 fail:
246 av_frame_unref(pic);
247 return AVERROR(ENOMEM);
248 }
249
250 382605 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
251 {
252 int ret;
253
254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 382605 times.
382605 if (avctx->hw_frames_ctx) {
255 ret = av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
256 if (ret == AVERROR(ENOMEM)) {
257 AVHWFramesContext *frames_ctx =
258 (AVHWFramesContext*)avctx->hw_frames_ctx->data;
259 if (frames_ctx->initial_pool_size > 0 &&
260 !avctx->internal->warned_on_failed_allocation_from_fixed_pool) {
261 av_log(avctx, AV_LOG_WARNING, "Failed to allocate a %s/%s "
262 "frame from a fixed pool of hardware frames.\n",
263 av_get_pix_fmt_name(frames_ctx->format),
264 av_get_pix_fmt_name(frames_ctx->sw_format));
265 av_log(avctx, AV_LOG_WARNING, "Consider setting "
266 "extra_hw_frames to a larger value "
267 "(currently set to %d, giving a pool size of %d).\n",
268 avctx->extra_hw_frames, frames_ctx->initial_pool_size);
269 avctx->internal->warned_on_failed_allocation_from_fixed_pool = 1;
270 }
271 }
272 frame->width = avctx->coded_width;
273 frame->height = avctx->coded_height;
274 return ret;
275 }
276
277
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 382605 times.
382605 if ((ret = update_frame_pool(avctx, frame)) < 0)
278 return ret;
279
280
2/3
✓ Branch 0 taken 121499 times.
✓ Branch 1 taken 261106 times.
✗ Branch 2 not taken.
382605 switch (avctx->codec_type) {
281 121499 case AVMEDIA_TYPE_VIDEO:
282 121499 return video_get_buffer(avctx, frame);
283 261106 case AVMEDIA_TYPE_AUDIO:
284 261106 return audio_get_buffer(avctx, frame);
285 default:
286 return -1;
287 }
288 }
289