FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/get_buffer.c
Date: 2022-05-23 03:24:52
Exec Total Coverage
Lines: 109 160 68.1%
Branches: 71 104 68.3%

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
36 typedef struct FramePool {
37 /**
38 * Pools for each data plane. For audio all the planes have the same size,
39 * so only pools[0] is used.
40 */
41 AVBufferPool *pools[4];
42
43 /*
44 * Pool parameters
45 */
46 int format;
47 int width, height;
48 int stride_align[AV_NUM_DATA_POINTERS];
49 int linesize[4];
50 int planes;
51 int channels;
52 int samples;
53 } FramePool;
54
55 16949 static void frame_pool_free(void *opaque, uint8_t *data)
56 {
57 16949 FramePool *pool = (FramePool*)data;
58 int i;
59
60
2/2
✓ Branch 0 taken 67796 times.
✓ Branch 1 taken 16949 times.
84745 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
61 67796 av_buffer_pool_uninit(&pool->pools[i]);
62
63 16949 av_freep(&data);
64 16949 }
65
66 16949 static AVBufferRef *frame_pool_alloc(void)
67 {
68 16949 FramePool *pool = av_mallocz(sizeof(*pool));
69 AVBufferRef *buf;
70
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16949 times.
16949 if (!pool)
72 return NULL;
73
74 16949 buf = av_buffer_create((uint8_t*)pool, sizeof(*pool),
75 frame_pool_free, NULL, 0);
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16949 times.
16949 if (!buf) {
77 av_freep(&pool);
78 return NULL;
79 }
80
81 16949 return buf;
82 }
83
84 387227 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
85 {
86 774454 FramePool *pool = avctx->internal->pool ?
87
2/2
✓ Branch 0 taken 377084 times.
✓ Branch 1 taken 10143 times.
387227 (FramePool*)avctx->internal->pool->data : NULL;
88 AVBufferRef *pool_buf;
89 int i, ret, ch, planes;
90
91
2/2
✓ Branch 0 taken 293899 times.
✓ Branch 1 taken 93328 times.
387227 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
92 293899 int planar = av_sample_fmt_is_planar(frame->format);
93 293899 ch = frame->ch_layout.nb_channels;
94 #if FF_API_OLD_CHANNEL_LAYOUT
95 FF_DISABLE_DEPRECATION_WARNINGS
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293899 times.
293899 if (!ch)
97 ch = frame->channels;
98 FF_ENABLE_DEPRECATION_WARNINGS
99 #endif
100
2/2
✓ Branch 0 taken 160398 times.
✓ Branch 1 taken 133501 times.
293899 planes = planar ? ch : 1;
101 }
102
103
4/4
✓ Branch 0 taken 377084 times.
✓ Branch 1 taken 10143 times.
✓ Branch 2 taken 376907 times.
✓ Branch 3 taken 177 times.
387227 if (pool && pool->format == frame->format) {
104
2/2
✓ Branch 0 taken 84784 times.
✓ Branch 1 taken 292123 times.
376907 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
105
4/4
✓ Branch 0 taken 84562 times.
✓ Branch 1 taken 222 times.
✓ Branch 2 taken 84551 times.
✓ Branch 3 taken 11 times.
84784 pool->width == frame->width && pool->height == frame->height)
106 84551 return 0;
107
4/4
✓ Branch 0 taken 292123 times.
✓ Branch 1 taken 233 times.
✓ Branch 2 taken 292110 times.
✓ Branch 3 taken 13 times.
292356 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && pool->planes == planes &&
108
3/4
✓ Branch 0 taken 292110 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 285727 times.
✓ Branch 3 taken 6383 times.
292110 pool->channels == ch && frame->nb_samples == pool->samples)
109 285727 return 0;
110 }
111
112 16949 pool_buf = frame_pool_alloc();
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16949 times.
16949 if (!pool_buf)
114 return AVERROR(ENOMEM);
115 16949 pool = (FramePool*)pool_buf->data;
116
117
2/3
✓ Branch 0 taken 8777 times.
✓ Branch 1 taken 8172 times.
✗ Branch 2 not taken.
16949 switch (avctx->codec_type) {
118 8777 case AVMEDIA_TYPE_VIDEO: {
119 int linesize[4];
120 8777 int w = frame->width;
121 8777 int h = frame->height;
122 int unaligned;
123 ptrdiff_t linesize1[4];
124 size_t size[4];
125
126 8777 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
127
128 do {
129 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
130 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
131 17641 ret = av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17641 times.
17641 if (ret < 0)
133 goto fail;
134 // increase alignment of w for next try (rhs gives the lowest bit set in w)
135 17641 w += w & ~(w - 1);
136
137 17641 unaligned = 0;
138
2/2
✓ Branch 0 taken 70564 times.
✓ Branch 1 taken 17641 times.
88205 for (i = 0; i < 4; i++)
139 70564 unaligned |= linesize[i] % pool->stride_align[i];
140
2/2
✓ Branch 0 taken 8864 times.
✓ Branch 1 taken 8777 times.
17641 } while (unaligned);
141
142
2/2
✓ Branch 0 taken 35108 times.
✓ Branch 1 taken 8777 times.
43885 for (i = 0; i < 4; i++)
143 35108 linesize1[i] = linesize[i];
144 8777 ret = av_image_fill_plane_sizes(size, avctx->pix_fmt, h, linesize1);
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8777 times.
8777 if (ret < 0)
146 goto fail;
147
148
2/2
✓ Branch 0 taken 35108 times.
✓ Branch 1 taken 8777 times.
43885 for (i = 0; i < 4; i++) {
149 35108 pool->linesize[i] = linesize[i];
150
2/2
✓ Branch 0 taken 24766 times.
✓ Branch 1 taken 10342 times.
35108 if (size[i]) {
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24766 times.
24766 if (size[i] > INT_MAX - (16 + STRIDE_ALIGN - 1)) {
152 ret = AVERROR(EINVAL);
153 goto fail;
154 }
155 24766 pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
156 CONFIG_MEMORY_POISONING ?
157 NULL :
158 av_buffer_allocz);
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24766 times.
24766 if (!pool->pools[i]) {
160 ret = AVERROR(ENOMEM);
161 goto fail;
162 }
163 }
164 }
165 8777 pool->format = frame->format;
166 8777 pool->width = frame->width;
167 8777 pool->height = frame->height;
168
169 8777 break;
170 }
171 8172 case AVMEDIA_TYPE_AUDIO: {
172 8172 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
173 8172 frame->nb_samples, frame->format, 0);
174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8172 times.
8172 if (ret < 0)
175 goto fail;
176
177 8172 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8172 times.
8172 if (!pool->pools[0]) {
179 ret = AVERROR(ENOMEM);
180 goto fail;
181 }
182
183 8172 pool->format = frame->format;
184 8172 pool->planes = planes;
185 8172 pool->channels = ch;
186 8172 pool->samples = frame->nb_samples;
187 8172 break;
188 }
189 default: av_assert0(0);
190 }
191
192 16949 av_buffer_unref(&avctx->internal->pool);
193 16949 avctx->internal->pool = pool_buf;
194
195 16949 return 0;
196 fail:
197 av_buffer_unref(&pool_buf);
198 return ret;
199 }
200
201 293899 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
202 {
203 293899 FramePool *pool = (FramePool*)avctx->internal->pool->data;
204 293899 int planes = pool->planes;
205 int i;
206
207 293899 frame->linesize[0] = pool->linesize[0];
208
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293899 times.
293899 if (planes > AV_NUM_DATA_POINTERS) {
210 frame->extended_data = av_calloc(planes, sizeof(*frame->extended_data));
211 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
212 frame->extended_buf = av_calloc(frame->nb_extended_buf,
213 sizeof(*frame->extended_buf));
214 if (!frame->extended_data || !frame->extended_buf) {
215 av_freep(&frame->extended_data);
216 av_freep(&frame->extended_buf);
217 return AVERROR(ENOMEM);
218 }
219 } else {
220 293899 frame->extended_data = frame->data;
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293899 times.
293899 av_assert0(frame->nb_extended_buf == 0);
222 }
223
224
2/2
✓ Branch 0 taken 436322 times.
✓ Branch 1 taken 293899 times.
730221 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
225 436322 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 436322 times.
436322 if (!frame->buf[i])
227 goto fail;
228 436322 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
229 }
230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293899 times.
293899 for (i = 0; i < frame->nb_extended_buf; i++) {
231 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
232 if (!frame->extended_buf[i])
233 goto fail;
234 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
235 }
236
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293899 times.
293899 if (avctx->debug & FF_DEBUG_BUFFERS)
238 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
239
240 293899 return 0;
241 fail:
242 av_frame_unref(frame);
243 return AVERROR(ENOMEM);
244 }
245
246 93328 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
247 {
248 93328 FramePool *pool = (FramePool*)s->internal->pool->data;
249 93328 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
250 int i;
251
252
4/8
✓ Branch 0 taken 93328 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 93328 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 93328 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 93328 times.
93328 if (pic->data[0] || pic->data[1] || pic->data[2] || pic->data[3]) {
253 av_log(s, AV_LOG_ERROR, "pic->data[*]!=NULL in avcodec_default_get_buffer\n");
254 return -1;
255 }
256
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93328 times.
93328 if (!desc) {
258 av_log(s, AV_LOG_ERROR,
259 "Unable to get pixel format descriptor for format %s\n",
260 av_get_pix_fmt_name(pic->format));
261 return AVERROR(EINVAL);
262 }
263
264 93328 memset(pic->data, 0, sizeof(pic->data));
265 93328 pic->extended_data = pic->data;
266
267
4/4
✓ Branch 0 taken 359641 times.
✓ Branch 1 taken 657 times.
✓ Branch 2 taken 266970 times.
✓ Branch 3 taken 92671 times.
360298 for (i = 0; i < 4 && pool->pools[i]; i++) {
268 266970 pic->linesize[i] = pool->linesize[i];
269
270 266970 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 266970 times.
266970 if (!pic->buf[i])
272 goto fail;
273
274 266970 pic->data[i] = pic->buf[i]->data;
275 }
276
2/2
✓ Branch 0 taken 479654 times.
✓ Branch 1 taken 93328 times.
572982 for (; i < AV_NUM_DATA_POINTERS; i++) {
277 479654 pic->data[i] = NULL;
278 479654 pic->linesize[i] = 0;
279 }
280
281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93328 times.
93328 if (s->debug & FF_DEBUG_BUFFERS)
282 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
283
284 93328 return 0;
285 fail:
286 av_frame_unref(pic);
287 return AVERROR(ENOMEM);
288 }
289
290 387227 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
291 {
292 int ret;
293
294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 387227 times.
387227 if (avctx->hw_frames_ctx) {
295 ret = av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
296 frame->width = avctx->coded_width;
297 frame->height = avctx->coded_height;
298 return ret;
299 }
300
301
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 387227 times.
387227 if ((ret = update_frame_pool(avctx, frame)) < 0)
302 return ret;
303
304
2/3
✓ Branch 0 taken 93328 times.
✓ Branch 1 taken 293899 times.
✗ Branch 2 not taken.
387227 switch (avctx->codec_type) {
305 93328 case AVMEDIA_TYPE_VIDEO:
306 93328 return video_get_buffer(avctx, frame);
307 293899 case AVMEDIA_TYPE_AUDIO:
308 293899 return audio_get_buffer(avctx, frame);
309 default:
310 return -1;
311 }
312 }
313