FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/avcodec.c
Date: 2026-01-20 15:28:03
Exec Total Coverage
Lines: 366 494 74.1%
Functions: 16 18 88.9%
Branches: 301 443 67.9%

Line Branch Exec Source
1 /*
2 * AVCodecContext functions for libavcodec
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 /**
22 * @file
23 * AVCodecContext functions for libavcodec
24 */
25
26 #include <assert.h>
27
28 #include "config.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/bprint.h"
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/common.h"
34 #include "libavutil/emms.h"
35 #include "libavutil/imgutils.h"
36 #include "libavutil/mem.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/thread.h"
39 #include "avcodec.h"
40 #include "avcodec_internal.h"
41 #include "bsf.h"
42 #include "codec_desc.h"
43 #include "codec_internal.h"
44 #include "decode.h"
45 #include "frame_thread_encoder.h"
46 #include "hwconfig.h"
47 #include "internal.h"
48 #include "libavutil/refstruct.h"
49
50 /**
51 * Maximum size in bytes of extradata.
52 * This value was chosen such that every bit of the buffer is
53 * addressable by a 32-bit signed integer as used by get_bits.
54 */
55 #define FF_MAX_EXTRADATA_SIZE ((1 << 28) - AV_INPUT_BUFFER_PADDING_SIZE)
56
57 const SideDataMap ff_sd_global_map[] = {
58 { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
59 { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
60 { AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL },
61 { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
62 { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
63 { AV_PKT_DATA_MASTERING_DISPLAY_METADATA, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA },
64 { AV_PKT_DATA_CONTENT_LIGHT_LEVEL, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL },
65 { AV_PKT_DATA_ICC_PROFILE, AV_FRAME_DATA_ICC_PROFILE },
66 { AV_PKT_DATA_AMBIENT_VIEWING_ENVIRONMENT,AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT },
67 { AV_PKT_DATA_3D_REFERENCE_DISPLAYS, AV_FRAME_DATA_3D_REFERENCE_DISPLAYS },
68 { AV_PKT_DATA_EXIF, AV_FRAME_DATA_EXIF },
69 { AV_PKT_DATA_NB },
70 };
71
72
73 27829 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
74 {
75 size_t i;
76
77
2/2
✓ Branch 0 taken 1390026 times.
✓ Branch 1 taken 27829 times.
1417855 for (i = 0; i < count; i++) {
78 1390026 size_t offset = i * size;
79
2/2
✓ Branch 0 taken 1362197 times.
✓ Branch 1 taken 27829 times.
1390026 int r = func(c, FF_PTR_ADD((char *)arg, offset));
80
2/2
✓ Branch 0 taken 930 times.
✓ Branch 1 taken 1389096 times.
1390026 if (ret)
81 930 ret[i] = r;
82 }
83 27829 emms_c();
84 27829 return 0;
85 }
86
87 8707 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
88 {
89 int i;
90
91
2/2
✓ Branch 0 taken 231714 times.
✓ Branch 1 taken 8707 times.
240421 for (i = 0; i < count; i++) {
92 231714 int r = func(c, arg, i, 0);
93
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 231664 times.
231714 if (ret)
94 50 ret[i] = r;
95 }
96 8707 emms_c();
97 8707 return 0;
98 }
99
100 static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
101
102 66626 static void lock_avcodec(const FFCodec *codec)
103 {
104
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 66626 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
66626 if (codec->caps_internal & FF_CODEC_CAP_NOT_INIT_THREADSAFE && codec->init)
105 ff_mutex_lock(&codec_mutex);
106 66626 }
107
108 66626 static void unlock_avcodec(const FFCodec *codec)
109 {
110
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 66626 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
66626 if (codec->caps_internal & FF_CODEC_CAP_NOT_INIT_THREADSAFE && codec->init)
111 ff_mutex_unlock(&codec_mutex);
112 66626 }
113
114 28648 static int64_t get_bit_rate(AVCodecContext *ctx)
115 {
116 int64_t bit_rate;
117 int bits_per_sample;
118
119
2/3
✓ Branch 0 taken 23670 times.
✓ Branch 1 taken 4978 times.
✗ Branch 2 not taken.
28648 switch (ctx->codec_type) {
120 23670 case AVMEDIA_TYPE_VIDEO:
121 case AVMEDIA_TYPE_DATA:
122 case AVMEDIA_TYPE_SUBTITLE:
123 case AVMEDIA_TYPE_ATTACHMENT:
124 23670 bit_rate = ctx->bit_rate;
125 23670 break;
126 4978 case AVMEDIA_TYPE_AUDIO:
127 4978 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
128
2/2
✓ Branch 0 taken 2462 times.
✓ Branch 1 taken 2516 times.
4978 if (bits_per_sample) {
129 2462 bit_rate = ctx->sample_rate * (int64_t)ctx->ch_layout.nb_channels;
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2462 times.
2462 if (bit_rate > INT64_MAX / bits_per_sample) {
131 bit_rate = 0;
132 } else
133 2462 bit_rate *= bits_per_sample;
134 } else
135 2516 bit_rate = ctx->bit_rate;
136 4978 break;
137 default:
138 bit_rate = 0;
139 break;
140 }
141 28648 return bit_rate;
142 }
143
144 37445 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
145 {
146 37445 int ret = 0;
147 AVCodecInternal *avci;
148 const FFCodec *codec2;
149 const AVDictionaryEntry *e;
150
151
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37445 times.
37445 if (avcodec_is_open(avctx))
152 return 0;
153
154
3/4
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 37410 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 35 times.
37445 if (!codec && !avctx->codec) {
155 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
156 return AVERROR(EINVAL);
157 }
158
5/6
✓ Branch 0 taken 37410 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 28883 times.
✓ Branch 3 taken 8527 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 28883 times.
37445 if (codec && avctx->codec && codec != avctx->codec) {
159 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
160 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
161 return AVERROR(EINVAL);
162 }
163
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 37410 times.
37445 if (!codec)
164 35 codec = avctx->codec;
165 37445 codec2 = ffcodec(codec);
166
167
2/4
✓ Branch 0 taken 37445 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37445 times.
✗ Branch 3 not taken.
37445 if ((avctx->codec_type != AVMEDIA_TYPE_UNKNOWN && avctx->codec_type != codec->type) ||
168
2/4
✓ Branch 0 taken 37445 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37445 times.
37445 (avctx->codec_id != AV_CODEC_ID_NONE && avctx->codec_id != codec->id)) {
169 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
170 return AVERROR(EINVAL);
171 }
172
173 37445 avctx->codec_type = codec->type;
174 37445 avctx->codec_id = codec->id;
175 37445 avctx->codec = codec;
176
177
2/4
✓ Branch 0 taken 37445 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37445 times.
37445 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
178 return AVERROR(EINVAL);
179
180 // set the whitelist from provided options dict,
181 // so we can check it immediately
182
2/2
✓ Branch 0 taken 8881 times.
✓ Branch 1 taken 28564 times.
37445 e = options ? av_dict_get(*options, "codec_whitelist", NULL, 0) : NULL;
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37445 times.
37445 if (e) {
184 ret = av_opt_set(avctx, e->key, e->value, 0);
185 if (ret < 0)
186 return ret;
187 }
188
189
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 37445 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
37445 if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
190 av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
191 return AVERROR(EINVAL);
192 }
193
194
2/2
✓ Branch 1 taken 15895 times.
✓ Branch 2 taken 21550 times.
37445 avci = ff_codec_is_decoder(codec) ?
195 15895 ff_decode_internal_alloc() :
196 21550 ff_encode_internal_alloc();
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37445 times.
37445 if (!avci) {
198 ret = AVERROR(ENOMEM);
199 goto end;
200 }
201 37445 avctx->internal = avci;
202
203 37445 avci->buffer_frame = av_frame_alloc();
204 37445 avci->buffer_pkt = av_packet_alloc();
205
2/4
✓ Branch 0 taken 37445 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37445 times.
37445 if (!avci->buffer_frame || !avci->buffer_pkt) {
206 ret = AVERROR(ENOMEM);
207 goto free_and_end;
208 }
209
210
2/2
✓ Branch 0 taken 16577 times.
✓ Branch 1 taken 20868 times.
37445 if (codec2->priv_data_size > 0) {
211
2/2
✓ Branch 0 taken 8376 times.
✓ Branch 1 taken 8201 times.
16577 if (!avctx->priv_data) {
212 8376 avctx->priv_data = av_mallocz(codec2->priv_data_size);
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8376 times.
8376 if (!avctx->priv_data) {
214 ret = AVERROR(ENOMEM);
215 goto free_and_end;
216 }
217
2/2
✓ Branch 0 taken 2473 times.
✓ Branch 1 taken 5903 times.
8376 if (codec->priv_class) {
218 2473 *(const AVClass **)avctx->priv_data = codec->priv_class;
219 2473 av_opt_set_defaults(avctx->priv_data);
220 }
221 }
222 } else {
223 20868 avctx->priv_data = NULL;
224 }
225
226 37445 ret = av_opt_set_dict2(avctx, options, AV_OPT_SEARCH_CHILDREN);
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37445 times.
37445 if (ret < 0)
228 goto free_and_end;
229
230 // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
231
6/8
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 37411 times.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 28 times.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
37445 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
232
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
6 (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
233
3/4
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 37411 times.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
37445 if (avctx->coded_width && avctx->coded_height)
234 34 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
235
3/4
✓ Branch 0 taken 28144 times.
✓ Branch 1 taken 9267 times.
✓ Branch 2 taken 28144 times.
✗ Branch 3 not taken.
37411 else if (avctx->width && avctx->height)
236 28144 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37445 times.
37445 if (ret < 0)
238 goto free_and_end;
239 }
240
241
5/8
✓ Branch 0 taken 9267 times.
✓ Branch 1 taken 28178 times.
✓ Branch 2 taken 9267 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9267 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 9267 times.
37445 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
242
1/2
✓ Branch 1 taken 28178 times.
✗ Branch 2 not taken.
28178 && ( av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
243
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28178 times.
28178 || av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
244 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
245 ff_set_dimensions(avctx, 0, 0);
246 }
247
248
3/4
✓ Branch 0 taken 28178 times.
✓ Branch 1 taken 9267 times.
✓ Branch 2 taken 28178 times.
✗ Branch 3 not taken.
37445 if (avctx->width > 0 && avctx->height > 0) {
249
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28178 times.
28178 if (av_image_check_sar(avctx->width, avctx->height,
250 avctx->sample_aspect_ratio) < 0) {
251 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
252 avctx->sample_aspect_ratio.num,
253 avctx->sample_aspect_ratio.den);
254 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
255 }
256 }
257
258 /* AV_CODEC_CAP_CHANNEL_CONF is a decoder-only flag; so the code below
259 * in particular checks that sample_rate is set for all audio encoders. */
260
1/2
✓ Branch 0 taken 37445 times.
✗ Branch 1 not taken.
37445 if (avctx->sample_rate < 0 ||
261
4/4
✓ Branch 0 taken 32888 times.
✓ Branch 1 taken 4557 times.
✓ Branch 2 taken 242 times.
✓ Branch 3 taken 32646 times.
37445 avctx->sample_rate == 0 && avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 242 times.
242 !(codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)) {
263 av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate);
264 ret = AVERROR(EINVAL);
265 goto free_and_end;
266 }
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37445 times.
37445 if (avctx->block_align < 0) {
268 av_log(avctx, AV_LOG_ERROR, "Invalid block align: %d\n", avctx->block_align);
269 ret = AVERROR(EINVAL);
270 goto free_and_end;
271 }
272
273 /* AV_CODEC_CAP_CHANNEL_CONF is a decoder-only flag; so the code below
274 * in particular checks that nb_channels is set for all audio encoders. */
275
4/4
✓ Branch 0 taken 4799 times.
✓ Branch 1 taken 32646 times.
✓ Branch 2 taken 241 times.
✓ Branch 3 taken 4558 times.
37445 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && !avctx->ch_layout.nb_channels
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 241 times.
241 && !(codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)) {
277 av_log(avctx, AV_LOG_ERROR, "%s requires channel layout to be set\n",
278 ff_codec_is_decoder(codec) ? "Decoder" : "Encoder");
279 ret = AVERROR(EINVAL);
280 goto free_and_end;
281 }
282
3/4
✓ Branch 0 taken 4558 times.
✓ Branch 1 taken 32887 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4558 times.
37445 if (avctx->ch_layout.nb_channels && !av_channel_layout_check(&avctx->ch_layout)) {
283 av_log(avctx, AV_LOG_ERROR, "Invalid channel layout\n");
284 ret = AVERROR(EINVAL);
285 goto free_and_end;
286 }
287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37445 times.
37445 if (avctx->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) {
288 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d\n", avctx->ch_layout.nb_channels);
289 ret = AVERROR(EINVAL);
290 goto free_and_end;
291 }
292
293 37445 avctx->frame_num = 0;
294 37445 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
295
296
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 37438 times.
37445 if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
298 const char *codec_string = ff_codec_is_encoder(codec) ? "encoder" : "decoder";
299 const AVCodec *codec2;
300 av_log(avctx, AV_LOG_ERROR,
301 "The %s '%s' is experimental but experimental codecs are not enabled, "
302 "add '-strict %d' if you want to use it.\n",
303 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
304 codec2 = ff_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
305 if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
306 av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
307 codec_string, codec2->name);
308 ret = AVERROR_EXPERIMENTAL;
309 goto free_and_end;
310 }
311
312
2/2
✓ Branch 0 taken 4799 times.
✓ Branch 1 taken 32646 times.
37445 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
313
3/4
✓ Branch 0 taken 1343 times.
✓ Branch 1 taken 3456 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1343 times.
4799 (!avctx->time_base.num || !avctx->time_base.den)) {
314 3456 avctx->time_base.num = 1;
315 3456 avctx->time_base.den = avctx->sample_rate;
316 }
317
318
2/2
✓ Branch 1 taken 21550 times.
✓ Branch 2 taken 15895 times.
37445 if (ff_codec_is_encoder(avctx->codec))
319 21550 ret = ff_encode_preinit(avctx);
320 else
321 15895 ret = ff_decode_preinit(avctx);
322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37445 times.
37445 if (ret < 0)
323 goto free_and_end;
324
325
2/2
✓ Branch 0 taken 35781 times.
✓ Branch 1 taken 1664 times.
37445 if (HAVE_THREADS && !avci->frame_thread_encoder) {
326 /* Frame-threaded decoders call FFCodec.init for their child contexts. */
327 35781 lock_avcodec(codec2);
328 35781 ret = ff_thread_init(avctx);
329 35781 unlock_avcodec(codec2);
330
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35781 times.
35781 if (ret < 0) {
331 goto free_and_end;
332 }
333 }
334 if (!HAVE_THREADS && !(codec2->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
335 avctx->thread_count = 1;
336
337
2/2
✓ Branch 0 taken 1674 times.
✓ Branch 1 taken 35771 times.
37445 if (!(avctx->active_thread_type & FF_THREAD_FRAME) ||
338
2/2
✓ Branch 0 taken 1664 times.
✓ Branch 1 taken 10 times.
1674 avci->frame_thread_encoder) {
339
2/2
✓ Branch 0 taken 30845 times.
✓ Branch 1 taken 6590 times.
37435 if (codec2->init) {
340 30845 lock_avcodec(codec2);
341 30845 ret = codec2->init(avctx);
342 30845 unlock_avcodec(codec2);
343
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 30837 times.
30845 if (ret < 0) {
344 8 avci->needs_close = codec2->caps_internal & FF_CODEC_CAP_INIT_CLEANUP;
345 8 goto free_and_end;
346 }
347 }
348 37427 avci->needs_close = 1;
349 }
350
351 37437 ret=0;
352
353
2/2
✓ Branch 1 taken 15887 times.
✓ Branch 2 taken 21550 times.
37437 if (ff_codec_is_decoder(avctx->codec)) {
354
2/2
✓ Branch 0 taken 11151 times.
✓ Branch 1 taken 4736 times.
15887 if (!avctx->bit_rate)
355 11151 avctx->bit_rate = get_bit_rate(avctx);
356
357 /* validate channel layout from the decoder */
358
3/4
✓ Branch 0 taken 3258 times.
✓ Branch 1 taken 12629 times.
✓ Branch 3 taken 3258 times.
✗ Branch 4 not taken.
15887 if ((avctx->ch_layout.nb_channels && !av_channel_layout_check(&avctx->ch_layout)) ||
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15887 times.
15887 avctx->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) {
360 ret = AVERROR(EINVAL);
361 goto free_and_end;
362 }
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15887 times.
15887 if (avctx->bits_per_coded_sample < 0) {
364 ret = AVERROR(EINVAL);
365 goto free_and_end;
366 }
367 }
368
2/2
✓ Branch 0 taken 5316 times.
✓ Branch 1 taken 32121 times.
37437 if (codec->priv_class)
369
1/2
✓ Branch 0 taken 5316 times.
✗ Branch 1 not taken.
5316 av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
370
371 37437 end:
372
373 37445 return ret;
374 8 free_and_end:
375 8 ff_codec_close(avctx);
376 8 goto end;
377 }
378
379 111 void avcodec_flush_buffers(AVCodecContext *avctx)
380 {
381 111 AVCodecInternal *avci = avctx->internal;
382
383
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 111 times.
111 if (av_codec_is_encoder(avctx->codec)) {
384 int caps = avctx->codec->capabilities;
385
386 if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) {
387 // Only encoders that explicitly declare support for it can be
388 // flushed. Otherwise, this is a no-op.
389 av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder "
390 "that doesn't support it\n");
391 return;
392 }
393 ff_encode_flush_buffers(avctx);
394 } else
395 111 ff_decode_flush_buffers(avctx);
396
397 111 avci->draining = 0;
398 111 avci->draining_done = 0;
399
1/2
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
111 if (avci->buffer_frame)
400 111 av_frame_unref(avci->buffer_frame);
401
1/2
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
111 if (avci->buffer_pkt)
402 111 av_packet_unref(avci->buffer_pkt);
403
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 111 times.
111 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME &&
405 !avci->is_frame_mt)
406 ff_thread_flush(avctx);
407
2/2
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 77 times.
111 else if (ffcodec(avctx->codec)->flush)
408 34 ffcodec(avctx->codec)->flush(avctx);
409 }
410
411 1075 void avsubtitle_free(AVSubtitle *sub)
412 {
413 int i;
414
415
2/2
✓ Branch 0 taken 1020 times.
✓ Branch 1 taken 1075 times.
2095 for (i = 0; i < sub->num_rects; i++) {
416 1020 AVSubtitleRect *const rect = sub->rects[i];
417
418 1020 av_freep(&rect->data[0]);
419 1020 av_freep(&rect->data[1]);
420 1020 av_freep(&rect->data[2]);
421 1020 av_freep(&rect->data[3]);
422 1020 av_freep(&rect->text);
423 1020 av_freep(&rect->ass);
424
425 1020 av_freep(&sub->rects[i]);
426 }
427
428 1075 av_freep(&sub->rects);
429
430 1075 memset(sub, 0, sizeof(*sub));
431 1075 }
432
433 64328 av_cold void ff_codec_close(AVCodecContext *avctx)
434 {
435 int i;
436
437
2/2
✓ Branch 1 taken 37445 times.
✓ Branch 2 taken 26883 times.
64328 if (avcodec_is_open(avctx)) {
438 37445 AVCodecInternal *avci = avctx->internal;
439
440 #if CONFIG_FRAME_THREAD_ENCODER
441
4/4
✓ Branch 0 taken 14964 times.
✓ Branch 1 taken 22481 times.
✓ Branch 2 taken 1664 times.
✓ Branch 3 taken 13300 times.
37445 if (avci->frame_thread_encoder && avctx->thread_count > 1) {
442 1664 ff_frame_thread_encoder_free(avctx);
443 }
444 #endif
445
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 37409 times.
37445 if (HAVE_THREADS && avci->thread_ctx)
446 36 ff_thread_free(avctx);
447
4/4
✓ Branch 0 taken 37427 times.
✓ Branch 1 taken 18 times.
✓ Branch 3 taken 7619 times.
✓ Branch 4 taken 29808 times.
37445 if (avci->needs_close && ffcodec(avctx->codec)->close)
448 7619 ffcodec(avctx->codec)->close(avctx);
449 37445 avci->byte_buffer_size = 0;
450 37445 av_freep(&avci->byte_buffer);
451 37445 av_frame_free(&avci->buffer_frame);
452 37445 av_packet_free(&avci->buffer_pkt);
453 37445 av_packet_free(&avci->last_pkt_props);
454
455 37445 av_packet_free(&avci->in_pkt);
456 37445 av_frame_free(&avci->in_frame);
457 37445 av_frame_free(&avci->recon_frame);
458
459 37445 av_refstruct_unref(&avci->pool);
460 37445 av_refstruct_pool_uninit(&avci->progress_frame_pool);
461
2/2
✓ Branch 1 taken 15895 times.
✓ Branch 2 taken 21550 times.
37445 if (av_codec_is_decoder(avctx->codec))
462 15895 ff_decode_internal_uninit(avctx);
463
464 37445 ff_hwaccel_uninit(avctx);
465
466 37445 av_bsf_free(&avci->bsf);
467
468 #if CONFIG_LCMS2
469 ff_icc_context_uninit(&avci->icc);
470 #endif
471
472 37445 av_freep(&avctx->internal);
473 }
474
475
2/2
✓ Branch 0 taken 4382 times.
✓ Branch 1 taken 64328 times.
68710 for (i = 0; i < avctx->nb_coded_side_data; i++)
476 4382 av_freep(&avctx->coded_side_data[i].data);
477 64328 av_freep(&avctx->coded_side_data);
478 64328 avctx->nb_coded_side_data = 0;
479 64328 av_frame_side_data_free(&avctx->decoded_side_data,
480 &avctx->nb_decoded_side_data);
481
482 64328 av_buffer_unref(&avctx->hw_frames_ctx);
483 64328 av_buffer_unref(&avctx->hw_device_ctx);
484
485
5/6
✓ Branch 0 taken 24945 times.
✓ Branch 1 taken 39383 times.
✓ Branch 2 taken 24945 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7789 times.
✓ Branch 5 taken 17156 times.
64328 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
486 7789 av_opt_free(avctx->priv_data);
487 64328 av_opt_free(avctx);
488 64328 av_freep(&avctx->priv_data);
489
2/2
✓ Branch 1 taken 21550 times.
✓ Branch 2 taken 42778 times.
64328 if (av_codec_is_encoder(avctx->codec)) {
490 21550 av_freep(&avctx->extradata);
491 21550 avctx->extradata_size = 0;
492
2/2
✓ Branch 1 taken 32745 times.
✓ Branch 2 taken 10033 times.
42778 } else if (av_codec_is_decoder(avctx->codec))
493 32745 av_freep(&avctx->subtitle_header);
494
495 64328 avctx->codec = NULL;
496 64328 avctx->active_thread_type = 0;
497 64328 }
498
499 22551 static const char *unknown_if_null(const char *str)
500 {
501
1/2
✓ Branch 0 taken 22551 times.
✗ Branch 1 not taken.
22551 return str ? str : "unknown";
502 }
503
504 17506 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
505 {
506 const char *codec_type;
507 const char *codec_name;
508 17506 const char *profile = NULL;
509 AVBPrint bprint;
510 int64_t bitrate;
511 17506 int new_line = 0;
512 AVRational display_aspect_ratio;
513
2/2
✓ Branch 0 taken 17480 times.
✓ Branch 1 taken 26 times.
17506 const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
514 const char *str;
515
516
2/4
✓ Branch 0 taken 17506 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 17506 times.
17506 if (!buf || buf_size <= 0)
517 9 return;
518 17506 av_bprint_init_for_buffer(&bprint, buf, buf_size);
519 17506 codec_type = av_get_media_type_string(enc->codec_type);
520 17506 codec_name = avcodec_get_name(enc->codec_id);
521 17506 profile = avcodec_profile_name(enc->codec_id, enc->profile);
522
523
1/2
✓ Branch 0 taken 17506 times.
✗ Branch 1 not taken.
17506 av_bprintf(&bprint, "%s: %s", codec_type ? codec_type : "unknown",
524 codec_name);
525 17506 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
526
527
4/4
✓ Branch 0 taken 8352 times.
✓ Branch 1 taken 9154 times.
✓ Branch 2 taken 296 times.
✓ Branch 3 taken 8056 times.
17506 if (enc->codec && strcmp(enc->codec->name, codec_name))
528 296 av_bprintf(&bprint, " (%s)", enc->codec->name);
529
530
2/2
✓ Branch 0 taken 2064 times.
✓ Branch 1 taken 15442 times.
17506 if (profile)
531 2064 av_bprintf(&bprint, " (%s)", profile);
532
2/2
✓ Branch 0 taken 13514 times.
✓ Branch 1 taken 3992 times.
17506 if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
533
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13514 times.
13514 && av_log_get_level() >= AV_LOG_VERBOSE
534 && enc->refs)
535 av_bprintf(&bprint, ", %d reference frame%s",
536 enc->refs, enc->refs > 1 ? "s" : "");
537
538
2/2
✓ Branch 0 taken 9903 times.
✓ Branch 1 taken 7603 times.
17506 if (enc->codec_tag)
539 9903 av_bprintf(&bprint, " (%s / 0x%04X)",
540 9903 av_fourcc2str(enc->codec_tag), enc->codec_tag);
541
542
5/5
✓ Branch 0 taken 13514 times.
✓ Branch 1 taken 3713 times.
✓ Branch 2 taken 90 times.
✓ Branch 3 taken 180 times.
✓ Branch 4 taken 9 times.
17506 switch (enc->codec_type) {
543 13514 case AVMEDIA_TYPE_VIDEO:
544 {
545 unsigned len;
546
547 13514 av_bprintf(&bprint, "%s%s", separator,
548
2/2
✓ Branch 0 taken 13464 times.
✓ Branch 1 taken 50 times.
13514 enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
549 13464 unknown_if_null(av_get_pix_fmt_name(enc->pix_fmt)));
550
551 13514 av_bprint_chars(&bprint, '(', 1);
552 13514 len = bprint.len;
553
554 /* The following check ensures that '(' has been written
555 * and therefore allows us to erase it if it turns out
556 * to be unnecessary. */
557
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13514 times.
13514 if (!av_bprint_is_complete(&bprint))
558 return;
559
560
3/4
✓ Branch 0 taken 1211 times.
✓ Branch 1 taken 12303 times.
✓ Branch 2 taken 1211 times.
✗ Branch 3 not taken.
13514 if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
561
2/2
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 1184 times.
1211 enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
562 27 av_bprintf(&bprint, "%d bpc, ", enc->bits_per_raw_sample);
563
3/4
✓ Branch 0 taken 11175 times.
✓ Branch 1 taken 2339 times.
✓ Branch 2 taken 11175 times.
✗ Branch 3 not taken.
24689 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED &&
564 11175 (str = av_color_range_name(enc->color_range)))
565 11175 av_bprintf(&bprint, "%s, ", str);
566
567
2/2
✓ Branch 0 taken 10604 times.
✓ Branch 1 taken 2910 times.
13514 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
568
2/2
✓ Branch 0 taken 10602 times.
✓ Branch 1 taken 2 times.
10604 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
569
2/2
✓ Branch 0 taken 117 times.
✓ Branch 1 taken 10485 times.
10602 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
570 3029 const char *col = unknown_if_null(av_color_space_name(enc->colorspace));
571 3029 const char *pri = unknown_if_null(av_color_primaries_name(enc->color_primaries));
572 3029 const char *trc = unknown_if_null(av_color_transfer_name(enc->color_trc));
573
4/4
✓ Branch 0 taken 290 times.
✓ Branch 1 taken 2739 times.
✓ Branch 2 taken 132 times.
✓ Branch 3 taken 158 times.
3029 if (strcmp(col, pri) || strcmp(col, trc)) {
574 2871 new_line = 1;
575 2871 av_bprintf(&bprint, "%s/%s/%s, ", col, pri, trc);
576 } else
577 158 av_bprintf(&bprint, "%s, ", col);
578 }
579
580
2/2
✓ Branch 0 taken 7578 times.
✓ Branch 1 taken 5936 times.
13514 if (enc->field_order != AV_FIELD_UNKNOWN) {
581 7578 const char *field_order = "progressive";
582
2/2
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 7436 times.
7578 if (enc->field_order == AV_FIELD_TT)
583 142 field_order = "top first";
584
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 7385 times.
7436 else if (enc->field_order == AV_FIELD_BB)
585 51 field_order = "bottom first";
586
2/2
✓ Branch 0 taken 362 times.
✓ Branch 1 taken 7023 times.
7385 else if (enc->field_order == AV_FIELD_TB)
587 362 field_order = "top coded first (swapped)";
588
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 6892 times.
7023 else if (enc->field_order == AV_FIELD_BT)
589 131 field_order = "bottom coded first (swapped)";
590
591 7578 av_bprintf(&bprint, "%s, ", field_order);
592 }
593
594
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13514 times.
13514 if (av_log_get_level() >= AV_LOG_VERBOSE &&
595 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED &&
596 (str = av_chroma_location_name(enc->chroma_sample_location)))
597 av_bprintf(&bprint, "%s, ", str);
598
599
2/2
✓ Branch 0 taken 1092 times.
✓ Branch 1 taken 12422 times.
13514 if (len == bprint.len) {
600 1092 bprint.str[len - 1] = '\0';
601 1092 bprint.len--;
602 } else {
603
1/2
✓ Branch 0 taken 12422 times.
✗ Branch 1 not taken.
12422 if (bprint.len - 2 < bprint.size) {
604 /* Erase the last ", " */
605 12422 bprint.len -= 2;
606 12422 bprint.str[bprint.len] = '\0';
607 }
608 12422 av_bprint_chars(&bprint, ')', 1);
609 }
610 }
611
612
2/2
✓ Branch 0 taken 13494 times.
✓ Branch 1 taken 20 times.
13514 if (enc->width) {
613
2/2
✓ Branch 0 taken 2869 times.
✓ Branch 1 taken 10625 times.
13494 av_bprintf(&bprint, "%s%dx%d", new_line ? separator : ", ",
614 enc->width, enc->height);
615
616
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13494 times.
13494 if (av_log_get_level() >= AV_LOG_VERBOSE &&
617 enc->coded_width && enc->coded_height &&
618 (enc->width != enc->coded_width ||
619 enc->height != enc->coded_height))
620 av_bprintf(&bprint, " (%dx%d)",
621 enc->coded_width, enc->coded_height);
622
623
2/2
✓ Branch 0 taken 2645 times.
✓ Branch 1 taken 10849 times.
13494 if (enc->sample_aspect_ratio.num) {
624 2645 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
625 2645 enc->width * (int64_t)enc->sample_aspect_ratio.num,
626 2645 enc->height * (int64_t)enc->sample_aspect_ratio.den,
627 1024 * 1024);
628 2645 av_bprintf(&bprint, " [SAR %d:%d DAR %d:%d]",
629 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
630 display_aspect_ratio.num, display_aspect_ratio.den);
631 }
632
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13494 times.
13494 if (av_log_get_level() >= AV_LOG_DEBUG) {
633 int g = av_gcd(enc->time_base.num, enc->time_base.den);
634 av_bprintf(&bprint, ", %d/%d",
635 enc->time_base.num / g, enc->time_base.den / g);
636 }
637 }
638
2/2
✓ Branch 0 taken 7229 times.
✓ Branch 1 taken 6285 times.
13514 if (encode) {
639 7229 av_bprintf(&bprint, ", q=%d-%d", enc->qmin, enc->qmax);
640 } else {
641 #if FF_API_CODEC_PROPS
642 FF_DISABLE_DEPRECATION_WARNINGS
643
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6285 times.
6285 if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
644 av_bprintf(&bprint, ", Closed Captions");
645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6285 times.
6285 if (enc->properties & FF_CODEC_PROPERTY_FILM_GRAIN)
646 av_bprintf(&bprint, ", Film Grain");
647
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6285 times.
6285 if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
648 av_bprintf(&bprint, ", lossless");
649 FF_ENABLE_DEPRECATION_WARNINGS
650 #endif
651 }
652 13514 break;
653 3713 case AVMEDIA_TYPE_AUDIO:
654 3713 av_bprintf(&bprint, "%s", separator);
655
656
2/2
✓ Branch 0 taken 3697 times.
✓ Branch 1 taken 16 times.
3713 if (enc->sample_rate) {
657 3697 av_bprintf(&bprint, "%d Hz, ", enc->sample_rate);
658 }
659 3713 av_channel_layout_describe_bprint(&enc->ch_layout, &bprint);
660
2/4
✓ Branch 0 taken 3713 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3713 times.
✗ Branch 3 not taken.
7426 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE &&
661 3713 (str = av_get_sample_fmt_name(enc->sample_fmt))) {
662 3713 av_bprintf(&bprint, ", %s", str);
663 }
664
2/2
✓ Branch 0 taken 1958 times.
✓ Branch 1 taken 1755 times.
3713 if ( enc->bits_per_raw_sample > 0
665
2/2
✓ Branch 1 taken 218 times.
✓ Branch 2 taken 1740 times.
1958 && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
666 218 av_bprintf(&bprint, " (%d bit)", enc->bits_per_raw_sample);
667
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 3709 times.
3713 if (av_log_get_level() >= AV_LOG_VERBOSE) {
668
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (enc->initial_padding)
669 2 av_bprintf(&bprint, ", delay %d", enc->initial_padding);
670
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (enc->trailing_padding)
671 av_bprintf(&bprint, ", padding %d", enc->trailing_padding);
672 }
673 3713 break;
674 90 case AVMEDIA_TYPE_DATA:
675
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
90 if (av_log_get_level() >= AV_LOG_DEBUG) {
676 int g = av_gcd(enc->time_base.num, enc->time_base.den);
677 if (g)
678 av_bprintf(&bprint, ", %d/%d",
679 enc->time_base.num / g, enc->time_base.den / g);
680 }
681 90 break;
682 180 case AVMEDIA_TYPE_SUBTITLE:
683
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 147 times.
180 if (enc->width)
684 33 av_bprintf(&bprint, ", %dx%d", enc->width, enc->height);
685 180 break;
686 9 default:
687 9 return;
688 }
689
2/2
✓ Branch 0 taken 9032 times.
✓ Branch 1 taken 8465 times.
17497 if (encode) {
690
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9032 times.
9032 if (enc->flags & AV_CODEC_FLAG_PASS1)
691 av_bprintf(&bprint, ", pass 1");
692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9032 times.
9032 if (enc->flags & AV_CODEC_FLAG_PASS2)
693 av_bprintf(&bprint, ", pass 2");
694 }
695 17497 bitrate = get_bit_rate(enc);
696
2/2
✓ Branch 0 taken 11292 times.
✓ Branch 1 taken 6205 times.
17497 if (bitrate != 0) {
697 11292 av_bprintf(&bprint, ", %"PRId64" kb/s", bitrate / 1000);
698
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6205 times.
6205 } else if (enc->rc_max_rate > 0) {
699 av_bprintf(&bprint, ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
700 }
701 }
702
703 2872492 int avcodec_is_open(AVCodecContext *s)
704 {
705 2872492 return !!s->internal;
706 }
707
708 778057 int attribute_align_arg avcodec_receive_frame_flags(AVCodecContext *avctx,
709 AVFrame *frame, unsigned flags)
710 {
711 778057 av_frame_unref(frame);
712
713
2/4
✓ Branch 1 taken 778057 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 778057 times.
778057 if (!avcodec_is_open(avctx) || !avctx->codec)
714 return AVERROR(EINVAL);
715
716
2/2
✓ Branch 1 taken 777995 times.
✓ Branch 2 taken 62 times.
778057 if (ff_codec_is_decoder(avctx->codec))
717 777995 return ff_decode_receive_frame(avctx, frame, flags);
718 62 return ff_encode_receive_frame(avctx, frame);
719 }
720
721 27611 int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
722 {
723 27611 return avcodec_receive_frame_flags(avctx, frame, 0);
724 }
725
726 #define WRAP_CONFIG(allowed_type, field, var, field_type, sentinel_check) \
727 do { \
728 if (codec->type != (allowed_type)) \
729 return AVERROR(EINVAL); \
730 const field_type *ptr = codec->field; \
731 *out_configs = ptr; \
732 if (ptr) { \
733 for (int i = 0;; i++) { \
734 const field_type var = ptr[i]; \
735 if (sentinel_check) { \
736 *out_num_configs = i; \
737 break; \
738 } \
739 } \
740 } else \
741 *out_num_configs = 0; \
742 return 0; \
743 } while (0)
744
745 static const enum AVColorRange color_range_tab[] = {
746 AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG, AVCOL_RANGE_UNSPECIFIED,
747 AVCOL_RANGE_MPEG, AVCOL_RANGE_UNSPECIFIED,
748 };
749
750 static const enum AVAlphaMode alpha_mode_tab[] = {
751 AVALPHA_MODE_PREMULTIPLIED, AVALPHA_MODE_STRAIGHT, AVALPHA_MODE_UNSPECIFIED,
752 AVALPHA_MODE_PREMULTIPLIED, AVALPHA_MODE_UNSPECIFIED
753 };
754
755 static_assert((int)AVCOL_RANGE_MPEG == (int)AVALPHA_MODE_PREMULTIPLIED, "unexpected enum values");
756 static_assert((int)AVCOL_RANGE_JPEG == (int)AVALPHA_MODE_STRAIGHT, "unexpected enum values");
757 static_assert(AVCOL_RANGE_UNSPECIFIED == 0 && AVALPHA_MODE_UNSPECIFIED == 0, "unexpected enum values");
758 static_assert(AVCOL_RANGE_NB == 3 && AVALPHA_MODE_NB == 3, "unexpected enum values");
759
760 static const uint8_t offset_tab[] = {
761 [AVCOL_RANGE_MPEG] = 3,
762 [AVCOL_RANGE_JPEG] = 1,
763 [AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG] = 0,
764 };
765
766 70282 int ff_default_get_supported_config(const AVCodecContext *avctx,
767 const AVCodec *codec,
768 enum AVCodecConfig config,
769 unsigned flags,
770 const void **out_configs,
771 int *out_num_configs)
772 {
773 70282 const FFCodec *codec2 = ffcodec(codec);
774
775
8/9
✓ Branch 0 taken 32342 times.
✓ Branch 1 taken 6846 times.
✓ Branch 2 taken 2702 times.
✓ Branch 3 taken 2702 times.
✓ Branch 4 taken 2702 times.
✓ Branch 5 taken 6820 times.
✓ Branch 6 taken 6847 times.
✓ Branch 7 taken 9321 times.
✗ Branch 8 not taken.
70282 switch (config) {
776 FF_DISABLE_DEPRECATION_WARNINGS
777 32342 case AV_CODEC_CONFIG_PIX_FORMAT:
778
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 32342 times.
✓ Branch 2 taken 1813 times.
✓ Branch 3 taken 30529 times.
✓ Branch 4 taken 1813 times.
✓ Branch 5 taken 20002 times.
54157 WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, pix_fmts, pix_fmt, enum AVPixelFormat, pix_fmt == AV_PIX_FMT_NONE);
779 6846 case AV_CODEC_CONFIG_FRAME_RATE:
780
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 6846 times.
✓ Branch 2 taken 57 times.
✓ Branch 3 taken 6789 times.
✓ Branch 4 taken 57 times.
✓ Branch 5 taken 2995 times.
9841 WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, supported_framerates, framerate, AVRational, framerate.num == 0);
781 2702 case AV_CODEC_CONFIG_SAMPLE_RATE:
782
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2702 times.
✓ Branch 2 taken 122 times.
✓ Branch 3 taken 2580 times.
✓ Branch 4 taken 122 times.
✓ Branch 5 taken 718 times.
3542 WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, supported_samplerates, samplerate, int, samplerate == 0);
783 2702 case AV_CODEC_CONFIG_SAMPLE_FORMAT:
784
4/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2702 times.
✓ Branch 2 taken 2702 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2702 times.
✓ Branch 5 taken 2930 times.
8334 WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, sample_fmts, sample_fmt, enum AVSampleFormat, sample_fmt == AV_SAMPLE_FMT_NONE);
785 2702 case AV_CODEC_CONFIG_CHANNEL_LAYOUT:
786
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2702 times.
✓ Branch 2 taken 158 times.
✓ Branch 3 taken 2544 times.
✓ Branch 4 taken 158 times.
✓ Branch 5 taken 772 times.
3474 WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, ch_layouts, ch_layout, AVChannelLayout, ch_layout.nb_channels == 0);
787 FF_ENABLE_DEPRECATION_WARNINGS
788
789 6820 case AV_CODEC_CONFIG_COLOR_RANGE:
790
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6820 times.
6820 if (codec->type != AVMEDIA_TYPE_VIDEO)
791 return AVERROR(EINVAL);
792 6820 unsigned color_ranges = codec2->color_ranges;
793
2/2
✓ Branch 0 taken 475 times.
✓ Branch 1 taken 6345 times.
6820 if (color_ranges)
794 475 *out_configs = color_range_tab + offset_tab[color_ranges];
795 else
796 6345 *out_configs = NULL;
797 6820 *out_num_configs = av_popcount(color_ranges);
798 6820 return 0;
799
800 6847 case AV_CODEC_CONFIG_COLOR_SPACE:
801 6847 *out_configs = NULL;
802 6847 *out_num_configs = 0;
803 6847 return 0;
804
805 9321 case AV_CODEC_CONFIG_ALPHA_MODE:
806
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9321 times.
9321 if (codec->type != AVMEDIA_TYPE_VIDEO)
807 return AVERROR(EINVAL);
808 9321 unsigned alpha_modes = codec2->alpha_modes;
809
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 9288 times.
9321 if (alpha_modes)
810 33 *out_configs = alpha_mode_tab + offset_tab[alpha_modes];
811 else
812 9288 *out_configs = NULL;
813 9321 *out_num_configs = av_popcount(alpha_modes);
814 9321 return 0;
815
816 default:
817 return AVERROR(EINVAL);
818 }
819 }
820
821 70309 int avcodec_get_supported_config(const AVCodecContext *avctx, const AVCodec *codec,
822 enum AVCodecConfig config, unsigned flags,
823 const void **out, int *out_num)
824 {
825 const FFCodec *codec2;
826 70309 int dummy_num = 0;
827
1/2
✓ Branch 0 taken 70309 times.
✗ Branch 1 not taken.
70309 if (!codec)
828 70309 codec = avctx->codec;
829
2/2
✓ Branch 0 taken 43609 times.
✓ Branch 1 taken 26700 times.
70309 if (!out_num)
830 43609 out_num = &dummy_num;
831
832 70309 codec2 = ffcodec(codec);
833
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 70122 times.
70309 if (codec2->get_supported_config) {
834 187 return codec2->get_supported_config(avctx, codec, config, flags, out, out_num);
835 } else {
836 70122 return ff_default_get_supported_config(avctx, codec, config, flags, out, out_num);
837 }
838 }
839
840 int av_packet_side_data_from_frame(AVPacketSideData **psd, int *pnb_sd,
841 const AVFrameSideData *src, unsigned int flags)
842 {
843 AVPacketSideData *sd = NULL;
844
845 for (unsigned j = 0; ff_sd_global_map[j].packet < AV_PKT_DATA_NB; j++) {
846 if (ff_sd_global_map[j].frame != src->type)
847 continue;
848
849 sd = av_packet_side_data_new(psd, pnb_sd, ff_sd_global_map[j].packet,
850 src->size, 0);
851
852 if (!sd)
853 return AVERROR(ENOMEM);
854
855 memcpy(sd->data, src->data, src->size);
856 break;
857 }
858
859 if (!sd)
860 return AVERROR(EINVAL);
861
862 return 0;
863 }
864
865 int av_packet_side_data_to_frame(AVFrameSideData ***psd, int *pnb_sd,
866 const AVPacketSideData *src, unsigned int flags)
867 {
868 AVFrameSideData *sd = NULL;
869
870 for (unsigned j = 0; ff_sd_global_map[j].packet < AV_PKT_DATA_NB; j++) {
871 if (ff_sd_global_map[j].packet != src->type)
872 continue;
873
874 sd = av_frame_side_data_new(psd, pnb_sd, ff_sd_global_map[j].frame,
875 src->size, flags);
876
877 if (!sd)
878 return AVERROR(ENOMEM);
879
880 memcpy(sd->data, src->data, src->size);
881 break;
882 }
883
884 if (!sd)
885 return AVERROR(EINVAL);
886
887 return 0;
888 }
889