FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/avcodec.c
Date: 2026-08-21 04:21:01
Exec Total Coverage
Lines: 368 494 74.5%
Functions: 16 18 88.9%
Branches: 305 443 68.8%

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 28042 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 1390239 times.
✓ Branch 1 taken 28042 times.
1418281 for (i = 0; i < count; i++) {
78 1390239 size_t offset = i * size;
79
2/2
✓ Branch 0 taken 1362197 times.
✓ Branch 1 taken 28042 times.
1390239 int r = func(c, FF_PTR_ADD((char *)arg, offset));
80
2/2
✓ Branch 0 taken 930 times.
✓ Branch 1 taken 1389309 times.
1390239 if (ret)
81 930 ret[i] = r;
82 }
83 28042 emms_c();
84 28042 return 0;
85 }
86
87 8751 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 231758 times.
✓ Branch 1 taken 8751 times.
240509 for (i = 0; i < count; i++) {
92 231758 int r = func(c, arg, i, 0);
93
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 231708 times.
231758 if (ret)
94 50 ret[i] = r;
95 }
96 8751 emms_c();
97 8751 return 0;
98 }
99
100 static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
101
102 68602 static void lock_avcodec(const FFCodec *codec)
103 {
104
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 68602 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
68602 if (codec->caps_internal & FF_CODEC_CAP_NOT_INIT_THREADSAFE && codec->init)
105 ff_mutex_lock(&codec_mutex);
106 68602 }
107
108 68602 static void unlock_avcodec(const FFCodec *codec)
109 {
110
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 68602 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
68602 if (codec->caps_internal & FF_CODEC_CAP_NOT_INIT_THREADSAFE && codec->init)
111 ff_mutex_unlock(&codec_mutex);
112 68602 }
113
114 29683 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 24124 times.
✓ Branch 1 taken 5559 times.
✗ Branch 2 not taken.
29683 switch (ctx->codec_type) {
120 24124 case AVMEDIA_TYPE_VIDEO:
121 case AVMEDIA_TYPE_DATA:
122 case AVMEDIA_TYPE_SUBTITLE:
123 case AVMEDIA_TYPE_ATTACHMENT:
124 24124 bit_rate = ctx->bit_rate;
125 24124 break;
126 5559 case AVMEDIA_TYPE_AUDIO:
127 5559 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
128
2/2
✓ Branch 0 taken 2585 times.
✓ Branch 1 taken 2974 times.
5559 if (bits_per_sample) {
129 2585 bit_rate = ctx->sample_rate * (int64_t)ctx->ch_layout.nb_channels;
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2585 times.
2585 if (bit_rate > INT64_MAX / bits_per_sample) {
131 bit_rate = 0;
132 } else
133 2585 bit_rate *= bits_per_sample;
134 } else
135 2974 bit_rate = ctx->bit_rate;
136 5559 break;
137 default:
138 bit_rate = 0;
139 break;
140 }
141 29683 return bit_rate;
142 }
143
144 38486 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
145 {
146 38486 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 38486 times.
38486 if (avcodec_is_open(avctx))
152 return 0;
153
154
3/4
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 38449 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 37 times.
38486 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 38449 times.
✓ Branch 1 taken 37 times.
✓ Branch 2 taken 29564 times.
✓ Branch 3 taken 8885 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 29564 times.
38486 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 37 times.
✓ Branch 1 taken 38449 times.
38486 if (!codec)
164 37 codec = avctx->codec;
165 38486 codec2 = ffcodec(codec);
166
167
2/4
✓ Branch 0 taken 38486 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38486 times.
✗ Branch 3 not taken.
38486 if ((avctx->codec_type != AVMEDIA_TYPE_UNKNOWN && avctx->codec_type != codec->type) ||
168
2/4
✓ Branch 0 taken 38486 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 38486 times.
38486 (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 38486 avctx->codec_type = codec->type;
174 38486 avctx->codec_id = codec->id;
175 38486 avctx->codec = codec;
176
177
2/4
✓ Branch 0 taken 38486 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 38486 times.
38486 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 9344 times.
✓ Branch 1 taken 29142 times.
38486 e = options ? av_dict_get(*options, "codec_whitelist", NULL, 0) : NULL;
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38486 times.
38486 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 38486 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
38486 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 16500 times.
✓ Branch 2 taken 21986 times.
38486 avci = ff_codec_is_decoder(codec) ?
195 16500 ff_decode_internal_alloc() :
196 21986 ff_encode_internal_alloc();
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38486 times.
38486 if (!avci) {
198 ret = AVERROR(ENOMEM);
199 goto end;
200 }
201 38486 avctx->internal = avci;
202
203 38486 avci->buffer_frame = av_frame_alloc();
204 38486 avci->buffer_pkt = av_packet_alloc();
205
2/4
✓ Branch 0 taken 38486 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 38486 times.
38486 if (!avci->buffer_frame || !avci->buffer_pkt) {
206 ret = AVERROR(ENOMEM);
207 goto free_and_end;
208 }
209
210
2/2
✓ Branch 0 taken 17207 times.
✓ Branch 1 taken 21279 times.
38486 if (codec2->priv_data_size > 0) {
211
2/2
✓ Branch 0 taken 8715 times.
✓ Branch 1 taken 8492 times.
17207 if (!avctx->priv_data) {
212 8715 avctx->priv_data = av_mallocz(codec2->priv_data_size);
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8715 times.
8715 if (!avctx->priv_data) {
214 ret = AVERROR(ENOMEM);
215 goto free_and_end;
216 }
217
2/2
✓ Branch 0 taken 2650 times.
✓ Branch 1 taken 6065 times.
8715 if (codec->priv_class) {
218 2650 *(const AVClass **)avctx->priv_data = codec->priv_class;
219 2650 av_opt_set_defaults(avctx->priv_data);
220 }
221 }
222 } else {
223 21279 avctx->priv_data = NULL;
224 }
225
226 38486 ret = av_opt_set_dict2(avctx, options, AV_OPT_SEARCH_CHILDREN);
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38486 times.
38486 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 35 times.
✓ Branch 1 taken 38451 times.
✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 29 times.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
38486 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 35 times.
✓ Branch 1 taken 38451 times.
✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
38486 if (avctx->coded_width && avctx->coded_height)
234 35 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
235
3/4
✓ Branch 0 taken 28664 times.
✓ Branch 1 taken 9787 times.
✓ Branch 2 taken 28664 times.
✗ Branch 3 not taken.
38451 else if (avctx->width && avctx->height)
236 28664 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38486 times.
38486 if (ret < 0)
238 goto free_and_end;
239 }
240
241
5/8
✓ Branch 0 taken 9787 times.
✓ Branch 1 taken 28699 times.
✓ Branch 2 taken 9787 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9787 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 9787 times.
38486 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
242
1/2
✓ Branch 1 taken 28699 times.
✗ Branch 2 not taken.
28699 && ( 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 28699 times.
28699 || 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 28699 times.
✓ Branch 1 taken 9787 times.
✓ Branch 2 taken 28699 times.
✗ Branch 3 not taken.
38486 if (avctx->width > 0 && avctx->height > 0) {
249
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 28698 times.
28699 if (av_image_check_sar(avctx->width, avctx->height,
250 avctx->sample_aspect_ratio) < 0) {
251 1 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 1 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 38486 times.
✗ Branch 1 not taken.
38486 if (avctx->sample_rate < 0 ||
261
4/4
✓ Branch 0 taken 33523 times.
✓ Branch 1 taken 4963 times.
✓ Branch 2 taken 310 times.
✓ Branch 3 taken 33213 times.
38486 avctx->sample_rate == 0 && avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 310 times.
310 !(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 38486 times.
38486 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 5273 times.
✓ Branch 1 taken 33213 times.
✓ Branch 2 taken 310 times.
✓ Branch 3 taken 4963 times.
38486 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && !avctx->ch_layout.nb_channels
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 310 times.
310 && !(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 4963 times.
✓ Branch 1 taken 33523 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4963 times.
38486 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 38486 times.
38486 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 38486 avctx->frame_num = 0;
294 38486 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
295
296
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 38476 times.
38486 if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 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 5273 times.
✓ Branch 1 taken 33213 times.
38486 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
313
3/4
✓ Branch 0 taken 1429 times.
✓ Branch 1 taken 3844 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1429 times.
5273 (!avctx->time_base.num || !avctx->time_base.den)) {
314 3844 avctx->time_base.num = 1;
315 3844 avctx->time_base.den = avctx->sample_rate;
316 }
317
318
2/2
✓ Branch 1 taken 21986 times.
✓ Branch 2 taken 16500 times.
38486 if (ff_codec_is_encoder(avctx->codec))
319 21986 ret = ff_encode_preinit(avctx);
320 else
321 16500 ret = ff_decode_preinit(avctx);
322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38486 times.
38486 if (ret < 0)
323 goto free_and_end;
324
325
2/2
✓ Branch 0 taken 36787 times.
✓ Branch 1 taken 1699 times.
38486 if (HAVE_THREADS && !avci->frame_thread_encoder) {
326 /* Frame-threaded decoders call FFCodec.init for their child contexts. */
327 36787 lock_avcodec(codec2);
328 36787 ret = ff_thread_init(avctx);
329 36787 unlock_avcodec(codec2);
330
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36787 times.
36787 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 1709 times.
✓ Branch 1 taken 36777 times.
38486 if (!(avctx->active_thread_type & FF_THREAD_FRAME) ||
338
2/2
✓ Branch 0 taken 1699 times.
✓ Branch 1 taken 10 times.
1709 avci->frame_thread_encoder) {
339
2/2
✓ Branch 0 taken 31815 times.
✓ Branch 1 taken 6661 times.
38476 if (codec2->init) {
340 31815 lock_avcodec(codec2);
341 31815 ret = codec2->init(avctx);
342 31815 unlock_avcodec(codec2);
343
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 31807 times.
31815 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 38468 avci->needs_close = 1;
349 }
350
351 38478 ret=0;
352
353
2/2
✓ Branch 1 taken 16492 times.
✓ Branch 2 taken 21986 times.
38478 if (ff_codec_is_decoder(avctx->codec)) {
354
2/2
✓ Branch 0 taken 11532 times.
✓ Branch 1 taken 4960 times.
16492 if (!avctx->bit_rate)
355 11532 avctx->bit_rate = get_bit_rate(avctx);
356
357
358
2/2
✓ Branch 0 taken 3826 times.
✓ Branch 1 taken 12666 times.
16492 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
359
3/4
✓ Branch 0 taken 3197 times.
✓ Branch 1 taken 629 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3197 times.
3826 !avctx->frame_size && (avctx->flags2 & AV_CODEC_FLAG2_FIXED_FRAME_SIZE)) {
360 av_log(avctx, AV_LOG_ERROR, "Fixed frame size requested but no frame_size value set\n");
361 ret = AVERROR(EINVAL);
362 goto free_and_end;
363 }
364
365 16492 avci->skip_samples = avctx->delay;
366
367 /* validate channel layout from the decoder */
368
3/4
✓ Branch 0 taken 3580 times.
✓ Branch 1 taken 12912 times.
✓ Branch 3 taken 3580 times.
✗ Branch 4 not taken.
16492 if ((avctx->ch_layout.nb_channels && !av_channel_layout_check(&avctx->ch_layout)) ||
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16492 times.
16492 avctx->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) {
370 ret = AVERROR(EINVAL);
371 goto free_and_end;
372 }
373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16492 times.
16492 if (avctx->bits_per_coded_sample < 0) {
374 ret = AVERROR(EINVAL);
375 goto free_and_end;
376 }
377 }
378
2/2
✓ Branch 0 taken 5651 times.
✓ Branch 1 taken 32827 times.
38478 if (codec->priv_class)
379
1/2
✓ Branch 0 taken 5651 times.
✗ Branch 1 not taken.
5651 av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
380
381 38478 end:
382
383 38486 return ret;
384 8 free_and_end:
385 8 ff_codec_close(avctx);
386 8 goto end;
387 }
388
389 34 void avcodec_flush_buffers(AVCodecContext *avctx)
390 {
391 34 AVCodecInternal *avci = avctx->internal;
392
393
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
34 if (av_codec_is_encoder(avctx->codec)) {
394 int caps = avctx->codec->capabilities;
395
396 if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) {
397 // Only encoders that explicitly declare support for it can be
398 // flushed. Otherwise, this is a no-op.
399 av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder "
400 "that doesn't support it\n");
401 return;
402 }
403 ff_encode_flush_buffers(avctx);
404 } else
405 34 ff_decode_flush_buffers(avctx);
406
407 34 avci->draining = 0;
408 34 avci->draining_done = 0;
409
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (avci->buffer_frame)
410 34 av_frame_unref(avci->buffer_frame);
411
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (avci->buffer_pkt)
412 34 av_packet_unref(avci->buffer_pkt);
413
414
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME &&
415 !avci->is_frame_mt)
416 ff_thread_flush(avctx);
417
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 28 times.
34 else if (ffcodec(avctx->codec)->flush)
418 6 ffcodec(avctx->codec)->flush(avctx);
419 }
420
421 1075 void avsubtitle_free(AVSubtitle *sub)
422 {
423 int i;
424
425
2/2
✓ Branch 0 taken 1020 times.
✓ Branch 1 taken 1075 times.
2095 for (i = 0; i < sub->num_rects; i++) {
426 1020 AVSubtitleRect *const rect = sub->rects[i];
427
428 1020 av_freep(&rect->data[0]);
429 1020 av_freep(&rect->data[1]);
430 1020 av_freep(&rect->data[2]);
431 1020 av_freep(&rect->data[3]);
432 1020 av_freep(&rect->text);
433 1020 av_freep(&rect->ass);
434
435 1020 av_freep(&sub->rects[i]);
436 }
437
438 1075 av_freep(&sub->rects);
439
440 1075 memset(sub, 0, sizeof(*sub));
441 1075 }
442
443 66510 av_cold void ff_codec_close(AVCodecContext *avctx)
444 {
445 int i;
446
447
2/2
✓ Branch 1 taken 38486 times.
✓ Branch 2 taken 28024 times.
66510 if (avcodec_is_open(avctx)) {
448 38486 AVCodecInternal *avci = avctx->internal;
449
450 #if CONFIG_FRAME_THREAD_ENCODER
451
4/4
✓ Branch 0 taken 15279 times.
✓ Branch 1 taken 23207 times.
✓ Branch 2 taken 1699 times.
✓ Branch 3 taken 13580 times.
38486 if (avci->frame_thread_encoder && avctx->thread_count > 1) {
452 1699 ff_frame_thread_encoder_free(avctx);
453 }
454 #endif
455
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 38440 times.
38486 if (HAVE_THREADS && avci->thread_ctx)
456 46 ff_thread_free(avctx);
457
4/4
✓ Branch 0 taken 38468 times.
✓ Branch 1 taken 18 times.
✓ Branch 3 taken 8010 times.
✓ Branch 4 taken 30458 times.
38486 if (avci->needs_close && ffcodec(avctx->codec)->close)
458 8010 ffcodec(avctx->codec)->close(avctx);
459 38486 avci->byte_buffer_size = 0;
460 38486 av_freep(&avci->byte_buffer);
461 38486 av_frame_free(&avci->buffer_frame);
462 38486 av_packet_free(&avci->buffer_pkt);
463 38486 av_packet_free(&avci->last_pkt_props);
464
465 38486 av_packet_free(&avci->in_pkt);
466 38486 av_frame_free(&avci->in_frame);
467 38486 av_frame_free(&avci->recon_frame);
468
469 38486 av_refstruct_unref(&avci->pool);
470 38486 av_refstruct_pool_uninit(&avci->progress_frame_pool);
471
2/2
✓ Branch 1 taken 16500 times.
✓ Branch 2 taken 21986 times.
38486 if (av_codec_is_decoder(avctx->codec))
472 16500 ff_decode_internal_uninit(avctx);
473
474 38486 ff_hwaccel_uninit(avctx);
475
476 38486 av_bsf_free(&avci->bsf);
477
478 #if CONFIG_LCMS2
479 ff_icc_context_uninit(&avci->icc);
480 #endif
481
482 38486 av_freep(&avctx->internal);
483 }
484
485
2/2
✓ Branch 0 taken 4840 times.
✓ Branch 1 taken 66510 times.
71350 for (i = 0; i < avctx->nb_coded_side_data; i++)
486 4840 av_freep(&avctx->coded_side_data[i].data);
487 66510 av_freep(&avctx->coded_side_data);
488 66510 avctx->nb_coded_side_data = 0;
489 66510 av_frame_side_data_free(&avctx->decoded_side_data,
490 &avctx->nb_decoded_side_data);
491
492 66510 av_buffer_unref(&avctx->hw_frames_ctx);
493 66510 av_buffer_unref(&avctx->hw_device_ctx);
494
495
5/6
✓ Branch 0 taken 25914 times.
✓ Branch 1 taken 40596 times.
✓ Branch 2 taken 25914 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8301 times.
✓ Branch 5 taken 17613 times.
66510 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
496 8301 av_opt_free(avctx->priv_data);
497 66510 av_opt_free(avctx);
498 66510 av_freep(&avctx->priv_data);
499
2/2
✓ Branch 1 taken 21989 times.
✓ Branch 2 taken 44521 times.
66510 if (av_codec_is_encoder(avctx->codec)) {
500 21989 av_freep(&avctx->extradata);
501 21989 avctx->extradata_size = 0;
502
2/2
✓ Branch 1 taken 34096 times.
✓ Branch 2 taken 10425 times.
44521 } else if (av_codec_is_decoder(avctx->codec))
503 34096 av_freep(&avctx->subtitle_header);
504
505 66510 avctx->codec = NULL;
506 66510 avctx->active_thread_type = 0;
507 66510 }
508
509 23173 static const char *unknown_if_null(const char *str)
510 {
511
1/2
✓ Branch 0 taken 23173 times.
✗ Branch 1 not taken.
23173 return str ? str : "unknown";
512 }
513
514 18180 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
515 {
516 const char *codec_type;
517 const char *codec_name;
518 18180 const char *profile = NULL;
519 AVBPrint bprint;
520 int64_t bitrate;
521 18180 int new_line = 0;
522 AVRational display_aspect_ratio;
523
2/2
✓ Branch 0 taken 18142 times.
✓ Branch 1 taken 38 times.
18180 const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
524 const char *str;
525
526
2/4
✓ Branch 0 taken 18180 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18180 times.
18180 if (!buf || buf_size <= 0)
527 29 return;
528 18180 av_bprint_init_for_buffer(&bprint, buf, buf_size);
529 18180 codec_type = av_get_media_type_string(enc->codec_type);
530 18180 codec_name = avcodec_get_name(enc->codec_id);
531 18180 profile = avcodec_profile_name(enc->codec_id, enc->profile);
532
533
1/2
✓ Branch 0 taken 18180 times.
✗ Branch 1 not taken.
18180 av_bprintf(&bprint, "%s: %s", codec_type ? codec_type : "unknown",
534 codec_name);
535 18180 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
536
537
4/4
✓ Branch 0 taken 8741 times.
✓ Branch 1 taken 9439 times.
✓ Branch 2 taken 375 times.
✓ Branch 3 taken 8366 times.
18180 if (enc->codec && strcmp(enc->codec->name, codec_name))
538 375 av_bprintf(&bprint, " (%s)", enc->codec->name);
539
540
2/2
✓ Branch 0 taken 2291 times.
✓ Branch 1 taken 15889 times.
18180 if (profile)
541 2291 av_bprintf(&bprint, " (%s)", profile);
542
2/2
✓ Branch 0 taken 13776 times.
✓ Branch 1 taken 4404 times.
18180 if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
543
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13776 times.
13776 && av_log_get_level() >= AV_LOG_VERBOSE
544 && enc->refs)
545 av_bprintf(&bprint, ", %d reference frame%s",
546 enc->refs, enc->refs > 1 ? "s" : "");
547
548
2/2
✓ Branch 0 taken 10239 times.
✓ Branch 1 taken 7941 times.
18180 if (enc->codec_tag)
549 10239 av_bprintf(&bprint, " (%s / 0x%04X)",
550 10239 av_fourcc2str(enc->codec_tag), enc->codec_tag);
551
552
5/5
✓ Branch 0 taken 13776 times.
✓ Branch 1 taken 4100 times.
✓ Branch 2 taken 93 times.
✓ Branch 3 taken 182 times.
✓ Branch 4 taken 29 times.
18180 switch (enc->codec_type) {
553 13776 case AVMEDIA_TYPE_VIDEO:
554 {
555 unsigned len;
556
557 13776 av_bprintf(&bprint, "%s%s", separator,
558
2/2
✓ Branch 0 taken 13720 times.
✓ Branch 1 taken 56 times.
13776 enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
559 13720 unknown_if_null(av_get_pix_fmt_name(enc->pix_fmt)));
560
561 13776 av_bprint_chars(&bprint, '(', 1);
562 13776 len = bprint.len;
563
564 /* The following check ensures that '(' has been written
565 * and therefore allows us to erase it if it turns out
566 * to be unnecessary. */
567
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13776 times.
13776 if (!av_bprint_is_complete(&bprint))
568 return;
569
570
3/4
✓ Branch 0 taken 1239 times.
✓ Branch 1 taken 12537 times.
✓ Branch 2 taken 1239 times.
✗ Branch 3 not taken.
13776 if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
571
2/2
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 1212 times.
1239 enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
572 27 av_bprintf(&bprint, "%d bpc, ", enc->bits_per_raw_sample);
573
3/4
✓ Branch 0 taken 11357 times.
✓ Branch 1 taken 2419 times.
✓ Branch 2 taken 11357 times.
✗ Branch 3 not taken.
25133 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED &&
574 11357 (str = av_color_range_name(enc->color_range)))
575 11357 av_bprintf(&bprint, "%s, ", str);
576
577
2/2
✓ Branch 0 taken 10743 times.
✓ Branch 1 taken 3033 times.
13776 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
578
2/2
✓ Branch 0 taken 10741 times.
✓ Branch 1 taken 2 times.
10743 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
579
2/2
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 10625 times.
10741 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
580 3151 const char *col = unknown_if_null(av_color_space_name(enc->colorspace));
581 3151 const char *pri = unknown_if_null(av_color_primaries_name(enc->color_primaries));
582 3151 const char *trc = unknown_if_null(av_color_transfer_name(enc->color_trc));
583
4/4
✓ Branch 0 taken 295 times.
✓ Branch 1 taken 2856 times.
✓ Branch 2 taken 135 times.
✓ Branch 3 taken 160 times.
3151 if (strcmp(col, pri) || strcmp(col, trc)) {
584 2991 new_line = 1;
585 2991 av_bprintf(&bprint, "%s/%s/%s, ", col, pri, trc);
586 } else
587 160 av_bprintf(&bprint, "%s, ", col);
588 }
589
590
2/2
✓ Branch 0 taken 7665 times.
✓ Branch 1 taken 6111 times.
13776 if (enc->field_order != AV_FIELD_UNKNOWN) {
591 7665 const char *field_order = "progressive";
592
2/2
✓ Branch 0 taken 161 times.
✓ Branch 1 taken 7504 times.
7665 if (enc->field_order == AV_FIELD_TT)
593 161 field_order = "top first";
594
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 7453 times.
7504 else if (enc->field_order == AV_FIELD_BB)
595 51 field_order = "bottom first";
596
2/2
✓ Branch 0 taken 363 times.
✓ Branch 1 taken 7090 times.
7453 else if (enc->field_order == AV_FIELD_TB)
597 363 field_order = "top coded first (swapped)";
598
2/2
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 6956 times.
7090 else if (enc->field_order == AV_FIELD_BT)
599 134 field_order = "bottom coded first (swapped)";
600
601 7665 av_bprintf(&bprint, "%s, ", field_order);
602 }
603
604
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13776 times.
13776 if (av_log_get_level() >= AV_LOG_VERBOSE &&
605 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED &&
606 (str = av_chroma_location_name(enc->chroma_sample_location)))
607 av_bprintf(&bprint, "%s, ", str);
608
609
2/2
✓ Branch 0 taken 1137 times.
✓ Branch 1 taken 12639 times.
13776 if (len == bprint.len) {
610 1137 bprint.str[len - 1] = '\0';
611 1137 bprint.len--;
612 } else {
613
1/2
✓ Branch 0 taken 12639 times.
✗ Branch 1 not taken.
12639 if (bprint.len - 2 < bprint.size) {
614 /* Erase the last ", " */
615 12639 bprint.len -= 2;
616 12639 bprint.str[bprint.len] = '\0';
617 }
618 12639 av_bprint_chars(&bprint, ')', 1);
619 }
620 }
621
622
2/2
✓ Branch 0 taken 13752 times.
✓ Branch 1 taken 24 times.
13776 if (enc->width) {
623
2/2
✓ Branch 0 taken 2989 times.
✓ Branch 1 taken 10763 times.
13752 av_bprintf(&bprint, "%s%dx%d", new_line ? separator : ", ",
624 enc->width, enc->height);
625
626
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13752 times.
13752 if (av_log_get_level() >= AV_LOG_VERBOSE &&
627 enc->coded_width && enc->coded_height &&
628 (enc->width != enc->coded_width ||
629 enc->height != enc->coded_height))
630 av_bprintf(&bprint, " (%dx%d)",
631 enc->coded_width, enc->coded_height);
632
633
2/2
✓ Branch 0 taken 2791 times.
✓ Branch 1 taken 10961 times.
13752 if (enc->sample_aspect_ratio.num) {
634 2791 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
635 2791 enc->width * (int64_t)enc->sample_aspect_ratio.num,
636 2791 enc->height * (int64_t)enc->sample_aspect_ratio.den,
637 1024 * 1024);
638 2791 av_bprintf(&bprint, " [SAR %d:%d DAR %d:%d]",
639 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
640 display_aspect_ratio.num, display_aspect_ratio.den);
641 }
642
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13752 times.
13752 if (av_log_get_level() >= AV_LOG_DEBUG) {
643 int g = av_gcd(enc->time_base.num, enc->time_base.den);
644 av_bprintf(&bprint, ", %d/%d",
645 enc->time_base.num / g, enc->time_base.den / g);
646 }
647 }
648
2/2
✓ Branch 0 taken 7326 times.
✓ Branch 1 taken 6450 times.
13776 if (encode) {
649 7326 av_bprintf(&bprint, ", q=%d-%d", enc->qmin, enc->qmax);
650 }
651 13776 break;
652 4100 case AVMEDIA_TYPE_AUDIO:
653 4100 av_bprintf(&bprint, "%s", separator);
654
655
2/2
✓ Branch 0 taken 4084 times.
✓ Branch 1 taken 16 times.
4100 if (enc->sample_rate) {
656 4084 av_bprintf(&bprint, "%d Hz, ", enc->sample_rate);
657 }
658 4100 av_channel_layout_describe_bprint(&enc->ch_layout, &bprint);
659
2/4
✓ Branch 0 taken 4100 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4100 times.
✗ Branch 3 not taken.
8200 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE &&
660 4100 (str = av_get_sample_fmt_name(enc->sample_fmt))) {
661 4100 av_bprintf(&bprint, ", %s", str);
662 }
663
2/2
✓ Branch 0 taken 2129 times.
✓ Branch 1 taken 1971 times.
4100 if ( enc->bits_per_raw_sample > 0
664
2/2
✓ Branch 1 taken 260 times.
✓ Branch 2 taken 1869 times.
2129 && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
665 260 av_bprintf(&bprint, " (%d bit)", enc->bits_per_raw_sample);
666
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 4094 times.
4100 if (av_log_get_level() >= AV_LOG_VERBOSE) {
667
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (enc->initial_padding)
668 3 av_bprintf(&bprint, ", delay %d", enc->initial_padding);
669
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (enc->trailing_padding)
670 av_bprintf(&bprint, ", padding %d", enc->trailing_padding);
671 }
672 4100 break;
673 93 case AVMEDIA_TYPE_DATA:
674
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 93 times.
93 if (av_log_get_level() >= AV_LOG_DEBUG) {
675 int g = av_gcd(enc->time_base.num, enc->time_base.den);
676 if (g)
677 av_bprintf(&bprint, ", %d/%d",
678 enc->time_base.num / g, enc->time_base.den / g);
679 }
680 93 break;
681 182 case AVMEDIA_TYPE_SUBTITLE:
682
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 149 times.
182 if (enc->width)
683 33 av_bprintf(&bprint, ", %dx%d", enc->width, enc->height);
684 182 break;
685 29 default:
686 29 return;
687 }
688
2/2
✓ Branch 0 taken 9271 times.
✓ Branch 1 taken 8880 times.
18151 if (encode) {
689
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9271 times.
9271 if (enc->flags & AV_CODEC_FLAG_PASS1)
690 av_bprintf(&bprint, ", pass 1");
691
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9271 times.
9271 if (enc->flags & AV_CODEC_FLAG_PASS2)
692 av_bprintf(&bprint, ", pass 2");
693 }
694 18151 bitrate = get_bit_rate(enc);
695
2/2
✓ Branch 0 taken 11666 times.
✓ Branch 1 taken 6485 times.
18151 if (bitrate != 0) {
696 11666 av_bprintf(&bprint, ", %"PRId64" kb/s", bitrate / 1000);
697
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6485 times.
6485 } else if (enc->rc_max_rate > 0) {
698 av_bprintf(&bprint, ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
699 }
700 }
701
702 3258809 int avcodec_is_open(AVCodecContext *s)
703 {
704 3258809 return !!s->internal;
705 }
706
707 870727 int attribute_align_arg avcodec_receive_frame_flags(AVCodecContext *avctx,
708 AVFrame *frame, unsigned flags)
709 {
710 870727 av_frame_unref(frame);
711
712
2/4
✓ Branch 1 taken 870727 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 870727 times.
870727 if (!avcodec_is_open(avctx) || !avctx->codec)
713 return AVERROR(EINVAL);
714
715
2/2
✓ Branch 1 taken 870657 times.
✓ Branch 2 taken 70 times.
870727 if (ff_codec_is_decoder(avctx->codec))
716 870657 return ff_decode_receive_frame(avctx, frame, flags);
717 70 return ff_encode_receive_frame(avctx, frame);
718 }
719
720 28640 int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
721 {
722 28640 return avcodec_receive_frame_flags(avctx, frame, 0);
723 }
724
725 #define WRAP_CONFIG(allowed_type, field, var, field_type, sentinel_check) \
726 do { \
727 if (codec->type != (allowed_type)) \
728 return AVERROR(EINVAL); \
729 const field_type *ptr = codec2->field; \
730 *out_configs = ptr; \
731 if (ptr) { \
732 for (int i = 0;; i++) { \
733 const field_type var = ptr[i]; \
734 if (sentinel_check) { \
735 *out_num_configs = i; \
736 break; \
737 } \
738 } \
739 } else \
740 *out_num_configs = 0; \
741 return 0; \
742 } while (0)
743
744 static const enum AVColorRange color_range_tab[] = {
745 AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG, AVCOL_RANGE_UNSPECIFIED,
746 AVCOL_RANGE_MPEG, AVCOL_RANGE_UNSPECIFIED,
747 };
748
749 static const enum AVAlphaMode alpha_mode_tab[] = {
750 AVALPHA_MODE_PREMULTIPLIED, AVALPHA_MODE_STRAIGHT, AVALPHA_MODE_UNSPECIFIED,
751 AVALPHA_MODE_PREMULTIPLIED, AVALPHA_MODE_UNSPECIFIED
752 };
753
754 static_assert((int)AVCOL_RANGE_MPEG == (int)AVALPHA_MODE_PREMULTIPLIED, "unexpected enum values");
755 static_assert((int)AVCOL_RANGE_JPEG == (int)AVALPHA_MODE_STRAIGHT, "unexpected enum values");
756 static_assert(AVCOL_RANGE_UNSPECIFIED == 0 && AVALPHA_MODE_UNSPECIFIED == 0, "unexpected enum values");
757 static_assert(AVCOL_RANGE_NB == 3 && AVALPHA_MODE_NB == 3, "unexpected enum values");
758
759 static const uint8_t offset_tab[] = {
760 [AVCOL_RANGE_MPEG] = 3,
761 [AVCOL_RANGE_JPEG] = 1,
762 [AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG] = 0,
763 };
764
765 71590 int ff_default_get_supported_config(const AVCodecContext *avctx,
766 const AVCodec *codec,
767 enum AVCodecConfig config,
768 unsigned flags,
769 const void **out_configs,
770 int *out_num_configs)
771 {
772 71590 const FFCodec *codec2 = ffcodec(codec);
773
774
8/9
✓ Branch 0 taken 32779 times.
✓ Branch 1 taken 6911 times.
✓ Branch 2 taken 2878 times.
✓ Branch 3 taken 2880 times.
✓ Branch 4 taken 2878 times.
✓ Branch 5 taken 6885 times.
✓ Branch 6 taken 6912 times.
✓ Branch 7 taken 9467 times.
✗ Branch 8 not taken.
71590 switch (config) {
775 32779 case AV_CODEC_CONFIG_PIX_FORMAT:
776
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 32779 times.
✓ Branch 2 taken 1869 times.
✓ Branch 3 taken 30910 times.
✓ Branch 4 taken 1869 times.
✓ Branch 5 taken 20253 times.
54901 WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, pix_fmts, pix_fmt, enum AVPixelFormat, pix_fmt == AV_PIX_FMT_NONE);
777 6911 case AV_CODEC_CONFIG_FRAME_RATE:
778
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 6911 times.
✓ Branch 2 taken 76 times.
✓ Branch 3 taken 6835 times.
✓ Branch 4 taken 76 times.
✓ Branch 5 taken 4173 times.
11084 WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, supported_framerates, framerate, AVRational, framerate.num == 0);
779 2878 case AV_CODEC_CONFIG_SAMPLE_RATE:
780
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2878 times.
✓ Branch 2 taken 176 times.
✓ Branch 3 taken 2702 times.
✓ Branch 4 taken 176 times.
✓ Branch 5 taken 1022 times.
4076 WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, supported_samplerates, samplerate, int, samplerate == 0);
781 2880 case AV_CODEC_CONFIG_SAMPLE_FORMAT:
782
4/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2880 times.
✓ Branch 2 taken 2880 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2880 times.
✓ Branch 5 taken 3126 times.
8886 WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, sample_fmts, sample_fmt, enum AVSampleFormat, sample_fmt == AV_SAMPLE_FMT_NONE);
783 2878 case AV_CODEC_CONFIG_CHANNEL_LAYOUT:
784
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2878 times.
✓ Branch 2 taken 204 times.
✓ Branch 3 taken 2674 times.
✓ Branch 4 taken 204 times.
✓ Branch 5 taken 1012 times.
3890 WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, ch_layouts, ch_layout, AVChannelLayout, ch_layout.nb_channels == 0);
785
786 6885 case AV_CODEC_CONFIG_COLOR_RANGE:
787
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6885 times.
6885 if (codec->type != AVMEDIA_TYPE_VIDEO)
788 return AVERROR(EINVAL);
789 6885 unsigned color_ranges = codec2->color_ranges;
790
2/2
✓ Branch 0 taken 496 times.
✓ Branch 1 taken 6389 times.
6885 if (color_ranges)
791 496 *out_configs = color_range_tab + offset_tab[color_ranges];
792 else
793 6389 *out_configs = NULL;
794 6885 *out_num_configs = av_popcount(color_ranges);
795 6885 return 0;
796
797 6912 case AV_CODEC_CONFIG_COLOR_SPACE:
798 6912 *out_configs = NULL;
799 6912 *out_num_configs = 0;
800 6912 return 0;
801
802 9467 case AV_CODEC_CONFIG_ALPHA_MODE:
803
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9467 times.
9467 if (codec->type != AVMEDIA_TYPE_VIDEO)
804 return AVERROR(EINVAL);
805 9467 unsigned alpha_modes = codec2->alpha_modes;
806
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 9433 times.
9467 if (alpha_modes)
807 34 *out_configs = alpha_mode_tab + offset_tab[alpha_modes];
808 else
809 9433 *out_configs = NULL;
810 9467 *out_num_configs = av_popcount(alpha_modes);
811 9467 return 0;
812
813 default:
814 return AVERROR(EINVAL);
815 }
816 }
817
818 71617 int avcodec_get_supported_config(const AVCodecContext *avctx, const AVCodec *codec,
819 enum AVCodecConfig config, unsigned flags,
820 const void **out, int *out_num)
821 {
822 const FFCodec *codec2;
823 71617 int dummy_num = 0;
824
2/2
✓ Branch 0 taken 71615 times.
✓ Branch 1 taken 2 times.
71617 if (!codec)
825 71615 codec = avctx->codec;
826
2/2
✓ Branch 0 taken 44222 times.
✓ Branch 1 taken 27395 times.
71617 if (!out_num)
827 44222 out_num = &dummy_num;
828
829 71617 codec2 = ffcodec(codec);
830
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 71430 times.
71617 if (codec2->get_supported_config) {
831 187 return codec2->get_supported_config(avctx, codec, config, flags, out, out_num);
832 } else {
833 71430 return ff_default_get_supported_config(avctx, codec, config, flags, out, out_num);
834 }
835 }
836
837 int av_packet_side_data_from_frame(AVPacketSideData **psd, int *pnb_sd,
838 const AVFrameSideData *src, unsigned int flags)
839 {
840 AVPacketSideData *sd = NULL;
841
842 for (unsigned j = 0; ff_sd_global_map[j].packet < AV_PKT_DATA_NB; j++) {
843 if (ff_sd_global_map[j].frame != src->type)
844 continue;
845
846 sd = av_packet_side_data_new(psd, pnb_sd, ff_sd_global_map[j].packet,
847 src->size, 0);
848
849 if (!sd)
850 return AVERROR(ENOMEM);
851
852 memcpy(sd->data, src->data, src->size);
853 break;
854 }
855
856 if (!sd)
857 return AVERROR(EINVAL);
858
859 return 0;
860 }
861
862 int av_packet_side_data_to_frame(AVFrameSideData ***psd, int *pnb_sd,
863 const AVPacketSideData *src, unsigned int flags)
864 {
865 AVFrameSideData *sd = NULL;
866
867 for (unsigned j = 0; ff_sd_global_map[j].packet < AV_PKT_DATA_NB; j++) {
868 if (ff_sd_global_map[j].packet != src->type)
869 continue;
870
871 sd = av_frame_side_data_new(psd, pnb_sd, ff_sd_global_map[j].frame,
872 src->size, flags);
873
874 if (!sd)
875 return AVERROR(ENOMEM);
876
877 memcpy(sd->data, src->data, src->size);
878 break;
879 }
880
881 if (!sd)
882 return AVERROR(EINVAL);
883
884 return 0;
885 }
886