FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/get_buffer.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 98 143 68.5%
Functions: 5 5 100.0%
Branches: 66 96 68.8%

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 15156 static void frame_pool_free(FFRefStructOpaque unused, void *obj)
57 {
58 15156 FramePool *pool = obj;
59 int i;
60
61
2/2
✓ Branch 0 taken 60624 times.
✓ Branch 1 taken 15156 times.
75780 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
62 60624 av_buffer_pool_uninit(&pool->pools[i]);
63 15156 }
64
65 428777 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
66 {
67 428777 FramePool *pool = avctx->internal->pool;
68 int i, ret, ch, planes;
69
70
2/2
✓ Branch 0 taken 307994 times.
✓ Branch 1 taken 120783 times.
428777 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
71 307994 int planar = av_sample_fmt_is_planar(frame->format);
72 307994 ch = frame->ch_layout.nb_channels;
73 #if FF_API_OLD_CHANNEL_LAYOUT
74 FF_DISABLE_DEPRECATION_WARNINGS
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 307994 times.
307994 if (!ch)
76 ch = frame->channels;
77 FF_ENABLE_DEPRECATION_WARNINGS
78 #endif
79
2/2
✓ Branch 0 taken 162129 times.
✓ Branch 1 taken 145865 times.
307994 planes = planar ? ch : 1;
80 }
81
82
4/4
✓ Branch 0 taken 420645 times.
✓ Branch 1 taken 8132 times.
✓ Branch 2 taken 420465 times.
✓ Branch 3 taken 180 times.
428777 if (pool && pool->format == frame->format) {
83
2/2
✓ Branch 0 taken 114334 times.
✓ Branch 1 taken 306131 times.
420465 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
84
4/4
✓ Branch 0 taken 114108 times.
✓ Branch 1 taken 226 times.
✓ Branch 2 taken 114095 times.
✓ Branch 3 taken 13 times.
114334 pool->width == frame->width && pool->height == frame->height)
85 114095 return 0;
86
4/4
✓ Branch 0 taken 306131 times.
✓ Branch 1 taken 239 times.
✓ Branch 2 taken 306118 times.
✓ Branch 3 taken 13 times.
306370 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && pool->planes == planes &&
87
3/4
✓ Branch 0 taken 306118 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 299526 times.
✓ Branch 3 taken 6592 times.
306118 pool->channels == ch && frame->nb_samples == pool->samples)
88 299526 return 0;
89 }
90
91 15156 pool = ff_refstruct_alloc_ext(sizeof(*pool), 0, NULL, frame_pool_free);
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15156 times.
15156 if (!pool)
93 return AVERROR(ENOMEM);
94
95
2/3
✓ Branch 0 taken 6688 times.
✓ Branch 1 taken 8468 times.
✗ Branch 2 not taken.
15156 switch (avctx->codec_type) {
96 6688 case AVMEDIA_TYPE_VIDEO: {
97 int linesize[4];
98 6688 int w = frame->width;
99 6688 int h = frame->height;
100 int unaligned;
101 ptrdiff_t linesize1[4];
102 size_t size[4];
103
104 6688 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
105
106 do {
107 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
108 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
109 13334 ret = av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13334 times.
13334 if (ret < 0)
111 goto fail;
112 // increase alignment of w for next try (rhs gives the lowest bit set in w)
113 13334 w += w & ~(w - 1);
114
115 13334 unaligned = 0;
116
2/2
✓ Branch 0 taken 53336 times.
✓ Branch 1 taken 13334 times.
66670 for (i = 0; i < 4; i++)
117 53336 unaligned |= linesize[i] % pool->stride_align[i];
118
2/2
✓ Branch 0 taken 6646 times.
✓ Branch 1 taken 6688 times.
13334 } while (unaligned);
119
120
2/2
✓ Branch 0 taken 26752 times.
✓ Branch 1 taken 6688 times.
33440 for (i = 0; i < 4; i++)
121 26752 linesize1[i] = linesize[i];
122 6688 ret = av_image_fill_plane_sizes(size, avctx->pix_fmt, h, linesize1);
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6688 times.
6688 if (ret < 0)
124 goto fail;
125
126
2/2
✓ Branch 0 taken 26752 times.
✓ Branch 1 taken 6688 times.
33440 for (i = 0; i < 4; i++) {
127 26752 pool->linesize[i] = linesize[i];
128
2/2
✓ Branch 0 taken 18525 times.
✓ Branch 1 taken 8227 times.
26752 if (size[i]) {
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18525 times.
18525 if (size[i] > INT_MAX - (16 + STRIDE_ALIGN - 1)) {
130 ret = AVERROR(EINVAL);
131 goto fail;
132 }
133 18525 pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
134 CONFIG_MEMORY_POISONING ?
135 NULL :
136 av_buffer_allocz);
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18525 times.
18525 if (!pool->pools[i]) {
138 ret = AVERROR(ENOMEM);
139 goto fail;
140 }
141 }
142 }
143 6688 pool->format = frame->format;
144 6688 pool->width = frame->width;
145 6688 pool->height = frame->height;
146
147 6688 break;
148 }
149 8468 case AVMEDIA_TYPE_AUDIO: {
150 8468 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
151 8468 frame->nb_samples, frame->format, 0);
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8468 times.
8468 if (ret < 0)
153 goto fail;
154
155 8468 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8468 times.
8468 if (!pool->pools[0]) {
157 ret = AVERROR(ENOMEM);
158 goto fail;
159 }
160
161 8468 pool->format = frame->format;
162 8468 pool->planes = planes;
163 8468 pool->channels = ch;
164 8468 pool->samples = frame->nb_samples;
165 8468 break;
166 }
167 default: av_assert0(0);
168 }
169
170 15156 ff_refstruct_unref(&avctx->internal->pool);
171 15156 avctx->internal->pool = pool;
172
173 15156 return 0;
174 fail:
175 ff_refstruct_unref(&pool);
176 return ret;
177 }
178
179 307994 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
180 {
181 307994 FramePool *pool = avctx->internal->pool;
182 307994 int planes = pool->planes;
183 int i;
184
185 307994 frame->linesize[0] = pool->linesize[0];
186
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 307994 times.
307994 if (planes > AV_NUM_DATA_POINTERS) {
188 frame->extended_data = av_calloc(planes, sizeof(*frame->extended_data));
189 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
190 frame->extended_buf = av_calloc(frame->nb_extended_buf,
191 sizeof(*frame->extended_buf));
192 if (!frame->extended_data || !frame->extended_buf) {
193 av_freep(&frame->extended_data);
194 av_freep(&frame->extended_buf);
195 return AVERROR(ENOMEM);
196 }
197 } else {
198 307994 frame->extended_data = frame->data;
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 307994 times.
307994 av_assert0(frame->nb_extended_buf == 0);
200 }
201
202
2/2
✓ Branch 0 taken 452172 times.
✓ Branch 1 taken 307994 times.
760166 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
203 452172 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 452172 times.
452172 if (!frame->buf[i])
205 goto fail;
206 452172 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
207 }
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 307994 times.
307994 for (i = 0; i < frame->nb_extended_buf; i++) {
209 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
210 if (!frame->extended_buf[i])
211 goto fail;
212 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
213 }
214
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 307994 times.
307994 if (avctx->debug & FF_DEBUG_BUFFERS)
216 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
217
218 307994 return 0;
219 fail:
220 av_frame_unref(frame);
221 return AVERROR(ENOMEM);
222 }
223
224 120783 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
225 {
226 120783 FramePool *pool = s->internal->pool;
227 int i;
228
229
4/8
✓ Branch 0 taken 120783 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 120783 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 120783 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 120783 times.
120783 if (pic->data[0] || pic->data[1] || pic->data[2] || pic->data[3]) {
230 av_log(s, AV_LOG_ERROR, "pic->data[*]!=NULL in avcodec_default_get_buffer\n");
231 return -1;
232 }
233
234 120783 memset(pic->data, 0, sizeof(pic->data));
235 120783 pic->extended_data = pic->data;
236
237
4/4
✓ Branch 0 taken 460941 times.
✓ Branch 1 taken 891 times.
✓ Branch 2 taken 341049 times.
✓ Branch 3 taken 119892 times.
461832 for (i = 0; i < 4 && pool->pools[i]; i++) {
238 341049 pic->linesize[i] = pool->linesize[i];
239
240 341049 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 341049 times.
341049 if (!pic->buf[i])
242 goto fail;
243
244 341049 pic->data[i] = pic->buf[i]->data;
245 }
246
2/2
✓ Branch 0 taken 625215 times.
✓ Branch 1 taken 120783 times.
745998 for (; i < AV_NUM_DATA_POINTERS; i++) {
247 625215 pic->data[i] = NULL;
248 625215 pic->linesize[i] = 0;
249 }
250
251
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 120783 times.
120783 if (s->debug & FF_DEBUG_BUFFERS)
252 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
253
254 120783 return 0;
255 fail:
256 av_frame_unref(pic);
257 return AVERROR(ENOMEM);
258 }
259
260 428777 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
261 {
262 int ret;
263
264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 428777 times.
428777 if (avctx->hw_frames_ctx) {
265 ret = av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
266 frame->width = avctx->coded_width;
267 frame->height = avctx->coded_height;
268 return ret;
269 }
270
271
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 428777 times.
428777 if ((ret = update_frame_pool(avctx, frame)) < 0)
272 return ret;
273
274
2/3
✓ Branch 0 taken 120783 times.
✓ Branch 1 taken 307994 times.
✗ Branch 2 not taken.
428777 switch (avctx->codec_type) {
275 120783 case AVMEDIA_TYPE_VIDEO:
276 120783 return video_get_buffer(avctx, frame);
277 307994 case AVMEDIA_TYPE_AUDIO:
278 307994 return audio_get_buffer(avctx, frame);
279 default:
280 return -1;
281 }
282 }
283