FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/avcodec.c
Date: 2026-10-25 17:13:04
Exec Total Coverage
Lines: 386 496 77.8%
Functions: 17 18 94.4%
Branches: 319 450 70.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_GAIN_MAP_PARAMS, AV_FRAME_DATA_GAIN_MAP_PARAMS },
70 { AV_PKT_DATA_NB },
71 };
72
73
74 28312 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
75 {
76 size_t i;
77
78
2/2
✓ Branch 0 taken 1391316 times.
✓ Branch 1 taken 28312 times.
1419628 for (i = 0; i < count; i++) {
79 1391316 size_t offset = i * size;
80
2/2
✓ Branch 0 taken 1363004 times.
✓ Branch 1 taken 28312 times.
1391316 int r = func(c, FF_PTR_ADD((char *)arg, offset));
81
2/2
✓ Branch 0 taken 930 times.
✓ Branch 1 taken 1390386 times.
1391316 if (ret)
82 930 ret[i] = r;
83 }
84 28312 emms_c();
85 28312 return 0;
86 }
87
88 8674 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
89 {
90 int i;
91
92
2/2
✓ Branch 0 taken 231615 times.
✓ Branch 1 taken 8674 times.
240289 for (i = 0; i < count; i++) {
93 231615 int r = func(c, arg, i, 0);
94
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 231565 times.
231615 if (ret)
95 50 ret[i] = r;
96 }
97 8674 emms_c();
98 8674 return 0;
99 }
100
101 static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
102
103 71839 static void lock_avcodec(const FFCodec *codec)
104 {
105
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 71839 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
71839 if (codec->caps_internal & FF_CODEC_CAP_NOT_INIT_THREADSAFE && codec->init)
106 ✗ ff_mutex_lock(&codec_mutex);
107 71839 }
108
109 71839 static void unlock_avcodec(const FFCodec *codec)
110 {
111
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 71839 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
71839 if (codec->caps_internal & FF_CODEC_CAP_NOT_INIT_THREADSAFE && codec->init)
112 ✗ ff_mutex_unlock(&codec_mutex);
113 71839 }
114
115 31279 static int64_t get_bit_rate(AVCodecContext *ctx)
116 {
117 int64_t bit_rate;
118 int bits_per_sample;
119
120
2/3
✓ Branch 0 taken 24785 times.
✓ Branch 1 taken 6494 times.
✗ Branch 2 not taken.
31279 switch (ctx->codec_type) {
121 24785 case AVMEDIA_TYPE_VIDEO:
122 case AVMEDIA_TYPE_DATA:
123 case AVMEDIA_TYPE_SUBTITLE:
124 case AVMEDIA_TYPE_ATTACHMENT:
125 24785 bit_rate = ctx->bit_rate;
126 24785 break;
127 6494 case AVMEDIA_TYPE_AUDIO:
128 6494 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
129
2/2
✓ Branch 0 taken 2929 times.
✓ Branch 1 taken 3565 times.
6494 if (bits_per_sample) {
130 2929 bit_rate = ctx->sample_rate * (int64_t)ctx->ch_layout.nb_channels;
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2929 times.
2929 if (bit_rate > INT64_MAX / bits_per_sample) {
132 ✗ bit_rate = 0;
133 } else
134 2929 bit_rate *= bits_per_sample;
135 } else
136 3565 bit_rate = ctx->bit_rate;
137 6494 break;
138 ✗ default:
139 ✗ bit_rate = 0;
140 ✗ break;
141 }
142 31279 return bit_rate;
143 }
144
145 40140 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
146 {
147 40140 int ret = 0;
148 AVCodecInternal *avci;
149 const FFCodec *codec2;
150 const AVDictionaryEntry *e;
151
152
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 40140 times.
40140 if (avcodec_is_open(avctx))
153 ✗ return 0;
154
155
3/4
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 40091 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 49 times.
40140 if (!codec && !avctx->codec) {
156 ✗ av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
157 ✗ return AVERROR(EINVAL);
158 }
159
5/6
✓ Branch 0 taken 40091 times.
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 30599 times.
✓ Branch 3 taken 9492 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 30599 times.
40140 if (codec && avctx->codec && codec != avctx->codec) {
160 ✗ av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
161 ✗ "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
162 ✗ return AVERROR(EINVAL);
163 }
164
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 40091 times.
40140 if (!codec)
165 49 codec = avctx->codec;
166 40140 codec2 = ffcodec(codec);
167
168
2/4
✓ Branch 0 taken 40140 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40140 times.
✗ Branch 3 not taken.
40140 if ((avctx->codec_type != AVMEDIA_TYPE_UNKNOWN && avctx->codec_type != codec->type) ||
169
2/4
✓ Branch 0 taken 40140 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 40140 times.
40140 (avctx->codec_id != AV_CODEC_ID_NONE && avctx->codec_id != codec->id)) {
170 ✗ av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
171 ✗ return AVERROR(EINVAL);
172 }
173
174 40140 avctx->codec_type = codec->type;
175 40140 avctx->codec_id = codec->id;
176 40140 avctx->codec = codec;
177
178
2/4
✓ Branch 0 taken 40140 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 40140 times.
40140 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
179 ✗ return AVERROR(EINVAL);
180
181 // set the whitelist from provided options dict,
182 // so we can check it immediately
183
2/2
✓ Branch 0 taken 10130 times.
✓ Branch 1 taken 30010 times.
40140 e = options ? av_dict_get(*options, "codec_whitelist", NULL, 0) : NULL;
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40140 times.
40140 if (e) {
185 ✗ ret = av_opt_set(avctx, e->key, e->value, 0);
186 ✗ if (ret < 0)
187 ✗ return ret;
188 }
189
190
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 40140 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
40140 if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
191 ✗ av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
192 ✗ return AVERROR(EINVAL);
193 }
194
195
2/2
✓ Branch 1 taken 17441 times.
✓ Branch 2 taken 22699 times.
40140 avci = ff_codec_is_decoder(codec) ?
196 17441 ff_decode_internal_alloc() :
197 22699 ff_encode_internal_alloc();
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40140 times.
40140 if (!avci) {
199 ✗ ret = AVERROR(ENOMEM);
200 ✗ goto end;
201 }
202 40140 avctx->internal = avci;
203
204 40140 avci->buffer_frame = av_frame_alloc();
205 40140 avci->buffer_pkt = av_packet_alloc();
206
2/4
✓ Branch 0 taken 40140 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 40140 times.
40140 if (!avci->buffer_frame || !avci->buffer_pkt) {
207 ✗ ret = AVERROR(ENOMEM);
208 ✗ goto free_and_end;
209 }
210
211
2/2
✓ Branch 0 taken 18276 times.
✓ Branch 1 taken 21864 times.
40140 if (codec2->priv_data_size > 0) {
212
2/2
✓ Branch 0 taken 9314 times.
✓ Branch 1 taken 8962 times.
18276 if (!avctx->priv_data) {
213 9314 avctx->priv_data = av_mallocz(codec2->priv_data_size);
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9314 times.
9314 if (!avctx->priv_data) {
215 ✗ ret = AVERROR(ENOMEM);
216 ✗ goto free_and_end;
217 }
218
2/2
✓ Branch 0 taken 2941 times.
✓ Branch 1 taken 6373 times.
9314 if (codec->priv_class) {
219 2941 *(const AVClass **)avctx->priv_data = codec->priv_class;
220 2941 av_opt_set_defaults(avctx->priv_data);
221 }
222 }
223 } else {
224 21864 avctx->priv_data = NULL;
225 }
226
227 40140 ret = av_opt_set_dict2(avctx, options, AV_OPT_SEARCH_CHILDREN);
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40140 times.
40140 if (ret < 0)
229 ✗ goto free_and_end;
230
231 // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
232
6/8
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 40099 times.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 35 times.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
40140 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
233
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))) {
234
3/4
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 40099 times.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
40140 if (avctx->coded_width && avctx->coded_height)
235 41 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
236
3/4
✓ Branch 0 taken 29457 times.
✓ Branch 1 taken 10642 times.
✓ Branch 2 taken 29457 times.
✗ Branch 3 not taken.
40099 else if (avctx->width && avctx->height)
237 29457 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40140 times.
40140 if (ret < 0)
239 ✗ goto free_and_end;
240 }
241
242
5/8
✓ Branch 0 taken 10642 times.
✓ Branch 1 taken 29498 times.
✓ Branch 2 taken 10642 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10642 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 10642 times.
40140 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
243
1/2
✓ Branch 1 taken 29498 times.
✗ Branch 2 not taken.
29498 && ( av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
244
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 29498 times.
29498 || av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
245 ✗ av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
246 ✗ ff_set_dimensions(avctx, 0, 0);
247 }
248
249
3/4
✓ Branch 0 taken 29498 times.
✓ Branch 1 taken 10642 times.
✓ Branch 2 taken 29498 times.
✗ Branch 3 not taken.
40140 if (avctx->width > 0 && avctx->height > 0) {
250
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 29496 times.
29498 if (av_image_check_sar(avctx->width, avctx->height,
251 avctx->sample_aspect_ratio) < 0) {
252 2 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
253 avctx->sample_aspect_ratio.num,
254 avctx->sample_aspect_ratio.den);
255 2 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
256 }
257 }
258
259 /* AV_CODEC_CAP_CHANNEL_CONF is a decoder-only flag; so the code below
260 * in particular checks that sample_rate is set for all audio encoders. */
261
1/2
✓ Branch 0 taken 40140 times.
✗ Branch 1 not taken.
40140 if (avctx->sample_rate < 0 ||
262
4/4
✓ Branch 0 taken 34456 times.
✓ Branch 1 taken 5684 times.
✓ Branch 2 taken 375 times.
✓ Branch 3 taken 34081 times.
40140 avctx->sample_rate == 0 && avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 375 times.
375 !(codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)) {
264 ✗ av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate);
265 ✗ ret = AVERROR(EINVAL);
266 ✗ goto free_and_end;
267 }
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40140 times.
40140 if (avctx->block_align < 0) {
269 ✗ av_log(avctx, AV_LOG_ERROR, "Invalid block align: %d\n", avctx->block_align);
270 ✗ ret = AVERROR(EINVAL);
271 ✗ goto free_and_end;
272 }
273
274 /* AV_CODEC_CAP_CHANNEL_CONF is a decoder-only flag; so the code below
275 * in particular checks that nb_channels is set for all audio encoders. */
276
4/4
✓ Branch 0 taken 6059 times.
✓ Branch 1 taken 34081 times.
✓ Branch 2 taken 378 times.
✓ Branch 3 taken 5681 times.
40140 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && !avctx->ch_layout.nb_channels
277
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 377 times.
378 && !(codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)) {
278
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 av_log(avctx, AV_LOG_ERROR, "%s requires channel layout to be set\n",
279 1 ff_codec_is_decoder(codec) ? "Decoder" : "Encoder");
280 1 ret = AVERROR(EINVAL);
281 1 goto free_and_end;
282 }
283
3/4
✓ Branch 0 taken 5681 times.
✓ Branch 1 taken 34458 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5681 times.
40139 if (avctx->ch_layout.nb_channels && !av_channel_layout_check(&avctx->ch_layout)) {
284 ✗ av_log(avctx, AV_LOG_ERROR, "Invalid channel layout\n");
285 ✗ ret = AVERROR(EINVAL);
286 ✗ goto free_and_end;
287 }
288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40139 times.
40139 if (avctx->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) {
289 ✗ av_log(avctx, AV_LOG_ERROR, "Too many channels: %d\n", avctx->ch_layout.nb_channels);
290 ✗ ret = AVERROR(EINVAL);
291 ✗ goto free_and_end;
292 }
293
294 40139 avctx->frame_num = 0;
295 40139 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
296
297
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 40128 times.
40139 if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
299 ✗ const char *codec_string = ff_codec_is_encoder(codec) ? "encoder" : "decoder";
300 const AVCodec *codec2;
301 ✗ av_log(avctx, AV_LOG_ERROR,
302 "The %s '%s' is experimental but experimental codecs are not enabled, "
303 "add '-strict %d' if you want to use it.\n",
304 ✗ codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
305 ✗ codec2 = ff_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
306 ✗ if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
307 ✗ av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
308 ✗ codec_string, codec2->name);
309 ✗ ret = AVERROR_EXPERIMENTAL;
310 ✗ goto free_and_end;
311 }
312
313
2/2
✓ Branch 0 taken 6058 times.
✓ Branch 1 taken 34081 times.
40139 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
314
3/4
✓ Branch 0 taken 1615 times.
✓ Branch 1 taken 4443 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1615 times.
6058 (!avctx->time_base.num || !avctx->time_base.den)) {
315 4443 avctx->time_base.num = 1;
316 4443 avctx->time_base.den = avctx->sample_rate;
317 }
318
319
2/2
✓ Branch 1 taken 22699 times.
✓ Branch 2 taken 17440 times.
40139 if (ff_codec_is_encoder(avctx->codec))
320 22699 ret = ff_encode_preinit(avctx);
321 else
322 17440 ret = ff_decode_preinit(avctx);
323
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40139 times.
40139 if (ret < 0)
324 ✗ goto free_and_end;
325
326
2/2
✓ Branch 0 taken 38385 times.
✓ Branch 1 taken 1754 times.
40139 if (HAVE_THREADS && !avci->frame_thread_encoder) {
327 /* Frame-threaded decoders call FFCodec.init for their child contexts. */
328 38385 lock_avcodec(codec2);
329 38385 ret = ff_thread_init(avctx);
330 38385 unlock_avcodec(codec2);
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38385 times.
38385 if (ret < 0) {
332 ✗ goto free_and_end;
333 }
334 }
335 if (!HAVE_THREADS && !(codec2->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
336 avctx->thread_count = 1;
337
338
2/2
✓ Branch 0 taken 1764 times.
✓ Branch 1 taken 38375 times.
40139 if (!(avctx->active_thread_type & FF_THREAD_FRAME) ||
339
2/2
✓ Branch 0 taken 1754 times.
✓ Branch 1 taken 10 times.
1764 avci->frame_thread_encoder) {
340
2/2
✓ Branch 0 taken 33454 times.
✓ Branch 1 taken 6675 times.
40129 if (codec2->init) {
341 33454 lock_avcodec(codec2);
342 33454 ret = codec2->init(avctx);
343 33454 unlock_avcodec(codec2);
344
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 33445 times.
33454 if (ret < 0) {
345 9 avci->needs_close = codec2->caps_internal & FF_CODEC_CAP_INIT_CLEANUP;
346 9 goto free_and_end;
347 }
348 }
349 40120 avci->needs_close = 1;
350 }
351
352 40130 ret=0;
353
354
2/2
✓ Branch 1 taken 17431 times.
✓ Branch 2 taken 22699 times.
40130 if (ff_codec_is_decoder(avctx->codec)) {
355
2/2
✓ Branch 0 taken 12027 times.
✓ Branch 1 taken 5404 times.
17431 if (!avctx->bit_rate)
356 12027 avctx->bit_rate = get_bit_rate(avctx);
357
358
359
2/2
✓ Branch 0 taken 4424 times.
✓ Branch 1 taken 13007 times.
17431 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
360
3/4
✓ Branch 0 taken 3554 times.
✓ Branch 1 taken 870 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3554 times.
4424 !avctx->frame_size && (avctx->flags2 & AV_CODEC_FLAG2_FIXED_FRAME_SIZE)) {
361 ✗ av_log(avctx, AV_LOG_ERROR, "Fixed frame size requested but no frame_size value set\n");
362 ✗ ret = AVERROR(EINVAL);
363 ✗ goto free_and_end;
364 }
365
366 17431 avci->skip_samples = avctx->delay;
367
368 /* validate channel layout from the decoder */
369
3/4
✓ Branch 0 taken 4119 times.
✓ Branch 1 taken 13312 times.
✓ Branch 3 taken 4119 times.
✗ Branch 4 not taken.
17431 if ((avctx->ch_layout.nb_channels && !av_channel_layout_check(&avctx->ch_layout)) ||
370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17431 times.
17431 avctx->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) {
371 ✗ ret = AVERROR(EINVAL);
372 ✗ goto free_and_end;
373 }
374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17431 times.
17431 if (avctx->bits_per_coded_sample < 0) {
375 ✗ ret = AVERROR(EINVAL);
376 ✗ goto free_and_end;
377 }
378 }
379
2/2
✓ Branch 0 taken 6170 times.
✓ Branch 1 taken 33960 times.
40130 if (codec->priv_class)
380
1/2
✓ Branch 0 taken 6170 times.
✗ Branch 1 not taken.
6170 av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
381
382 40130 end:
383
384 40140 return ret;
385 10 free_and_end:
386 10 ff_codec_close(avctx);
387 10 goto end;
388 }
389
390 40 void avcodec_flush_buffers(AVCodecContext *avctx)
391 {
392 40 AVCodecInternal *avci = avctx->internal;
393
394
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
40 if (av_codec_is_encoder(avctx->codec)) {
395 ✗ int caps = avctx->codec->capabilities;
396
397 ✗ if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) {
398 // Only encoders that explicitly declare support for it can be
399 // flushed. Otherwise, this is a no-op.
400 ✗ av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder "
401 "that doesn't support it\n");
402 ✗ return;
403 }
404 ✗ ff_encode_flush_buffers(avctx);
405 } else
406 40 ff_decode_flush_buffers(avctx);
407
408 40 avci->draining = 0;
409 40 avci->draining_done = 0;
410
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 if (avci->buffer_frame)
411 40 av_frame_unref(avci->buffer_frame);
412
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 if (avci->buffer_pkt)
413 40 av_packet_unref(avci->buffer_pkt);
414
415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME &&
416 ✗ !avci->is_frame_mt)
417 ✗ ff_thread_flush(avctx);
418
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 28 times.
40 else if (ffcodec(avctx->codec)->flush)
419 12 ffcodec(avctx->codec)->flush(avctx);
420 }
421
422 1084 void avsubtitle_free(AVSubtitle *sub)
423 {
424 int i;
425
426
2/2
✓ Branch 0 taken 1029 times.
✓ Branch 1 taken 1084 times.
2113 for (i = 0; i < sub->num_rects; i++) {
427 1029 AVSubtitleRect *const rect = sub->rects[i];
428
429 1029 av_freep(&rect->data[0]);
430 1029 av_freep(&rect->data[1]);
431 1029 av_freep(&rect->data[2]);
432 1029 av_freep(&rect->data[3]);
433 1029 av_freep(&rect->text);
434 1029 av_freep(&rect->ass);
435
436 1029 av_freep(&sub->rects[i]);
437 }
438
439 1084 av_freep(&sub->rects);
440
441 1084 memset(sub, 0, sizeof(*sub));
442 1084 }
443
444 70198 av_cold void ff_codec_close(AVCodecContext *avctx)
445 {
446 int i;
447
448
2/2
✓ Branch 1 taken 40140 times.
✓ Branch 2 taken 30058 times.
70198 if (avcodec_is_open(avctx)) {
449 40140 AVCodecInternal *avci = avctx->internal;
450
451 #if CONFIG_FRAME_THREAD_ENCODER
452
4/4
✓ Branch 0 taken 15774 times.
✓ Branch 1 taken 24366 times.
✓ Branch 2 taken 1754 times.
✓ Branch 3 taken 14020 times.
40140 if (avci->frame_thread_encoder && avctx->thread_count > 1) {
453 1754 ff_frame_thread_encoder_free(avctx);
454 }
455 #endif
456
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 40086 times.
40140 if (HAVE_THREADS && avci->thread_ctx)
457 54 ff_thread_free(avctx);
458
4/4
✓ Branch 0 taken 40120 times.
✓ Branch 1 taken 20 times.
✓ Branch 3 taken 8595 times.
✓ Branch 4 taken 31525 times.
40140 if (avci->needs_close && ffcodec(avctx->codec)->close)
459 8595 ffcodec(avctx->codec)->close(avctx);
460 40140 avci->byte_buffer_size = 0;
461 40140 av_freep(&avci->byte_buffer);
462 40140 av_frame_free(&avci->buffer_frame);
463 40140 av_packet_free(&avci->buffer_pkt);
464 40140 av_packet_free(&avci->last_pkt_props);
465
466 40140 av_packet_free(&avci->in_pkt);
467 40140 av_frame_free(&avci->in_frame);
468 40140 av_frame_free(&avci->recon_frame);
469
470 40140 av_refstruct_unref(&avci->pool);
471 40140 av_refstruct_pool_uninit(&avci->progress_frame_pool);
472
2/2
✓ Branch 1 taken 17441 times.
✓ Branch 2 taken 22699 times.
40140 if (av_codec_is_decoder(avctx->codec))
473 17441 ff_decode_internal_uninit(avctx);
474
475 40140 ff_hwaccel_uninit(avctx);
476
477 40140 av_bsf_free(&avci->bsf);
478
479 #if CONFIG_LCMS2
480 ff_icc_context_uninit(&avci->icc);
481 #endif
482
483 40140 av_freep(&avctx->internal);
484 }
485
486
2/2
✓ Branch 0 taken 5069 times.
✓ Branch 1 taken 70198 times.
75267 for (i = 0; i < avctx->nb_coded_side_data; i++)
487 5069 av_freep(&avctx->coded_side_data[i].data);
488 70198 av_freep(&avctx->coded_side_data);
489 70198 avctx->nb_coded_side_data = 0;
490 70198 av_frame_side_data_free(&avctx->decoded_side_data,
491 &avctx->nb_decoded_side_data);
492
493 70198 av_buffer_unref(&avctx->hw_frames_ctx);
494 70198 av_buffer_unref(&avctx->hw_device_ctx);
495
496
5/6
✓ Branch 0 taken 27580 times.
✓ Branch 1 taken 42618 times.
✓ Branch 2 taken 27580 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9111 times.
✓ Branch 5 taken 18469 times.
70198 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
497 9111 av_opt_free(avctx->priv_data);
498 70198 av_opt_free(avctx);
499 70198 av_freep(&avctx->priv_data);
500
2/2
✓ Branch 1 taken 22703 times.
✓ Branch 2 taken 47495 times.
70198 if (av_codec_is_encoder(avctx->codec)) {
501 22703 av_freep(&avctx->extradata);
502 22703 avctx->extradata_size = 0;
503
2/2
✓ Branch 1 taken 36241 times.
✓ Branch 2 taken 11254 times.
47495 } else if (av_codec_is_decoder(avctx->codec))
504 36241 av_freep(&avctx->subtitle_header);
505
506 70198 avctx->codec = NULL;
507 70198 avctx->active_thread_type = 0;
508 70198 }
509
510 23993 static const char *unknown_if_null(const char *str)
511 {
512
1/2
✓ Branch 0 taken 23993 times.
✗ Branch 1 not taken.
23993 return str ? str : "unknown";
513 }
514
515 19281 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
516 {
517 const char *codec_type;
518 const char *codec_name;
519 19281 const char *profile = NULL;
520 AVBPrint bprint;
521 int64_t bitrate;
522 19281 int new_line = 0;
523 AVRational display_aspect_ratio;
524
2/2
✓ Branch 0 taken 19241 times.
✓ Branch 1 taken 40 times.
19281 const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
525 const char *str;
526
527
2/4
✓ Branch 0 taken 19281 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19281 times.
19281 if (!buf || buf_size <= 0)
528 29 return;
529 19281 av_bprint_init_for_buffer(&bprint, buf, buf_size);
530 19281 codec_type = av_get_media_type_string(enc->codec_type);
531 19281 codec_name = avcodec_get_name(enc->codec_id);
532 19281 profile = avcodec_profile_name(enc->codec_id, enc->profile);
533
534
1/2
✓ Branch 0 taken 19281 times.
✗ Branch 1 not taken.
19281 av_bprintf(&bprint, "%s: %s", codec_type ? codec_type : "unknown",
535 codec_name);
536 19281 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
537
538
4/4
✓ Branch 0 taken 9341 times.
✓ Branch 1 taken 9940 times.
✓ Branch 2 taken 507 times.
✓ Branch 3 taken 8834 times.
19281 if (enc->codec && strcmp(enc->codec->name, codec_name))
539 507 av_bprintf(&bprint, " (%s)", enc->codec->name);
540
541
2/2
✓ Branch 0 taken 2676 times.
✓ Branch 1 taken 16605 times.
19281 if (profile)
542 2676 av_bprintf(&bprint, " (%s)", profile);
543
2/2
✓ Branch 0 taken 14185 times.
✓ Branch 1 taken 5096 times.
19281 if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
544
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14185 times.
14185 && av_log_get_level() >= AV_LOG_VERBOSE
545 ✗ && enc->refs)
546 ✗ av_bprintf(&bprint, ", %d reference frame%s",
547 ✗ enc->refs, enc->refs > 1 ? "s" : "");
548
549
2/2
✓ Branch 0 taken 10839 times.
✓ Branch 1 taken 8442 times.
19281 if (enc->codec_tag)
550 10839 av_bprintf(&bprint, " (%s / 0x%04X)",
551 10839 av_fourcc2str(enc->codec_tag), enc->codec_tag);
552
553
5/5
✓ Branch 0 taken 14185 times.
✓ Branch 1 taken 4770 times.
✓ Branch 2 taken 107 times.
✓ Branch 3 taken 190 times.
✓ Branch 4 taken 29 times.
19281 switch (enc->codec_type) {
554 14185 case AVMEDIA_TYPE_VIDEO:
555 {
556 unsigned len;
557
558 14185 av_bprintf(&bprint, "%s%s", separator,
559
2/2
✓ Branch 0 taken 14129 times.
✓ Branch 1 taken 56 times.
14185 enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
560 14129 unknown_if_null(av_get_pix_fmt_name(enc->pix_fmt)));
561
562 14185 av_bprint_chars(&bprint, '(', 1);
563 14185 len = bprint.len;
564
565 /* The following check ensures that '(' has been written
566 * and therefore allows us to erase it if it turns out
567 * to be unnecessary. */
568
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14185 times.
14185 if (!av_bprint_is_complete(&bprint))
569 ✗ return;
570
571
3/4
✓ Branch 0 taken 1331 times.
✓ Branch 1 taken 12854 times.
✓ Branch 2 taken 1331 times.
✗ Branch 3 not taken.
14185 if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
572
2/2
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 1304 times.
1331 enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
573 27 av_bprintf(&bprint, "%d bpc, ", enc->bits_per_raw_sample);
574
3/4
✓ Branch 0 taken 11573 times.
✓ Branch 1 taken 2612 times.
✓ Branch 2 taken 11573 times.
✗ Branch 3 not taken.
25758 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED &&
575 11573 (str = av_color_range_name(enc->color_range)))
576 11573 av_bprintf(&bprint, "%s, ", str);
577
578
2/2
✓ Branch 0 taken 11058 times.
✓ Branch 1 taken 3127 times.
14185 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
579
2/2
✓ Branch 0 taken 11054 times.
✓ Branch 1 taken 4 times.
11058 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
580
2/2
✓ Branch 0 taken 157 times.
✓ Branch 1 taken 10897 times.
11054 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
581 3288 const char *col = unknown_if_null(av_color_space_name(enc->colorspace));
582 3288 const char *pri = unknown_if_null(av_color_primaries_name(enc->color_primaries));
583 3288 const char *trc = unknown_if_null(av_color_transfer_name(enc->color_trc));
584
4/4
✓ Branch 0 taken 382 times.
✓ Branch 1 taken 2906 times.
✓ Branch 2 taken 209 times.
✓ Branch 3 taken 173 times.
3288 if (strcmp(col, pri) || strcmp(col, trc)) {
585 3115 new_line = 1;
586 3115 av_bprintf(&bprint, "%s/%s/%s, ", col, pri, trc);
587 } else
588 173 av_bprintf(&bprint, "%s, ", col);
589 }
590
591
2/2
✓ Branch 0 taken 7847 times.
✓ Branch 1 taken 6338 times.
14185 if (enc->field_order != AV_FIELD_UNKNOWN) {
592 7847 const char *field_order = "progressive";
593
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 7679 times.
7847 if (enc->field_order == AV_FIELD_TT)
594 168 field_order = "top first";
595
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 7628 times.
7679 else if (enc->field_order == AV_FIELD_BB)
596 51 field_order = "bottom first";
597
2/2
✓ Branch 0 taken 363 times.
✓ Branch 1 taken 7265 times.
7628 else if (enc->field_order == AV_FIELD_TB)
598 363 field_order = "top coded first (swapped)";
599
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 7121 times.
7265 else if (enc->field_order == AV_FIELD_BT)
600 144 field_order = "bottom coded first (swapped)";
601
602 7847 av_bprintf(&bprint, "%s, ", field_order);
603 }
604
605
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14185 times.
14185 if (av_log_get_level() >= AV_LOG_VERBOSE &&
606 ✗ enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED &&
607 ✗ (str = av_chroma_location_name(enc->chroma_sample_location)))
608 ✗ av_bprintf(&bprint, "%s, ", str);
609
610
2/2
✓ Branch 0 taken 1241 times.
✓ Branch 1 taken 12944 times.
14185 if (len == bprint.len) {
611 1241 bprint.str[len - 1] = '\0';
612 1241 bprint.len--;
613 } else {
614
1/2
✓ Branch 0 taken 12944 times.
✗ Branch 1 not taken.
12944 if (bprint.len - 2 < bprint.size) {
615 /* Erase the last ", " */
616 12944 bprint.len -= 2;
617 12944 bprint.str[bprint.len] = '\0';
618 }
619 12944 av_bprint_chars(&bprint, ')', 1);
620 }
621 }
622
623
2/2
✓ Branch 0 taken 14160 times.
✓ Branch 1 taken 25 times.
14185 if (enc->width) {
624
2/2
✓ Branch 0 taken 3112 times.
✓ Branch 1 taken 11048 times.
14160 av_bprintf(&bprint, "%s%dx%d", new_line ? separator : ", ",
625 enc->width, enc->height);
626
627
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14160 times.
14160 if (av_log_get_level() >= AV_LOG_VERBOSE &&
628 ✗ enc->coded_width && enc->coded_height &&
629 ✗ (enc->width != enc->coded_width ||
630 ✗ enc->height != enc->coded_height))
631 ✗ av_bprintf(&bprint, " (%dx%d)",
632 enc->coded_width, enc->coded_height);
633
634
2/2
✓ Branch 0 taken 2967 times.
✓ Branch 1 taken 11193 times.
14160 if (enc->sample_aspect_ratio.num) {
635 2967 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
636 2967 enc->width * (int64_t)enc->sample_aspect_ratio.num,
637 2967 enc->height * (int64_t)enc->sample_aspect_ratio.den,
638 1024 * 1024);
639 2967 av_bprintf(&bprint, " [SAR %d:%d DAR %d:%d]",
640 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
641 display_aspect_ratio.num, display_aspect_ratio.den);
642 }
643
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14160 times.
14160 if (av_log_get_level() >= AV_LOG_DEBUG) {
644 ✗ int g = av_gcd(enc->time_base.num, enc->time_base.den);
645 ✗ av_bprintf(&bprint, ", %d/%d",
646 ✗ enc->time_base.num / g, enc->time_base.den / g);
647 }
648 }
649
2/2
✓ Branch 0 taken 7498 times.
✓ Branch 1 taken 6687 times.
14185 if (encode) {
650 7498 av_bprintf(&bprint, ", q=%d-%d", enc->qmin, enc->qmax);
651 }
652 14185 break;
653 4770 case AVMEDIA_TYPE_AUDIO:
654 4770 av_bprintf(&bprint, "%s", separator);
655
656
2/2
✓ Branch 0 taken 4752 times.
✓ Branch 1 taken 18 times.
4770 if (enc->sample_rate) {
657 4752 av_bprintf(&bprint, "%d Hz, ", enc->sample_rate);
658 }
659 4770 av_channel_layout_describe_bprint(&enc->ch_layout, &bprint);
660
3/4
✓ Branch 0 taken 4759 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 4759 times.
✗ Branch 3 not taken.
9529 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE &&
661 4759 (str = av_get_sample_fmt_name(enc->sample_fmt))) {
662 4759 av_bprintf(&bprint, ", %s", str);
663 }
664
2/2
✓ Branch 0 taken 2330 times.
✓ Branch 1 taken 2440 times.
4770 if ( enc->bits_per_raw_sample > 0
665
2/2
✓ Branch 1 taken 264 times.
✓ Branch 2 taken 2066 times.
2330 && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
666 264 av_bprintf(&bprint, " (%d bit)", enc->bits_per_raw_sample);
667
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 4764 times.
4770 if (av_log_get_level() >= AV_LOG_VERBOSE) {
668
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (enc->initial_padding)
669 3 av_bprintf(&bprint, ", delay %d", enc->initial_padding);
670
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (enc->trailing_padding)
671 ✗ av_bprintf(&bprint, ", padding %d", enc->trailing_padding);
672 }
673 4770 break;
674 107 case AVMEDIA_TYPE_DATA:
675
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 107 times.
107 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 107 break;
682 190 case AVMEDIA_TYPE_SUBTITLE:
683
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 157 times.
190 if (enc->width)
684 33 av_bprintf(&bprint, ", %dx%d", enc->width, enc->height);
685 190 break;
686 29 default:
687 29 return;
688 }
689
2/2
✓ Branch 0 taken 9724 times.
✓ Branch 1 taken 9528 times.
19252 if (encode) {
690
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9724 times.
9724 if (enc->flags & AV_CODEC_FLAG_PASS1)
691 ✗ av_bprintf(&bprint, ", pass 1");
692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9724 times.
9724 if (enc->flags & AV_CODEC_FLAG_PASS2)
693 ✗ av_bprintf(&bprint, ", pass 2");
694 }
695 19252 bitrate = get_bit_rate(enc);
696
2/2
✓ Branch 0 taken 12472 times.
✓ Branch 1 taken 6780 times.
19252 if (bitrate != 0) {
697 12472 av_bprintf(&bprint, ", %"PRId64" kb/s", bitrate / 1000);
698
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6780 times.
6780 } else if (enc->rc_max_rate > 0) {
699 ✗ av_bprintf(&bprint, ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
700 }
701 }
702
703 3307518 int avcodec_is_open(AVCodecContext *s)
704 {
705 3307518 return !!s->internal;
706 }
707
708 882123 int attribute_align_arg avcodec_receive_frame_flags(AVCodecContext *avctx,
709 AVFrame *frame, unsigned flags)
710 {
711 882123 av_frame_unref(frame);
712
713
2/4
✓ Branch 1 taken 882123 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 882123 times.
882123 if (!avcodec_is_open(avctx) || !avctx->codec)
714 ✗ return AVERROR(EINVAL);
715
716
2/2
✓ Branch 1 taken 882019 times.
✓ Branch 2 taken 104 times.
882123 if (ff_codec_is_decoder(avctx->codec))
717 882019 return ff_decode_receive_frame(avctx, frame, flags);
718 104 return ff_encode_receive_frame(avctx, frame);
719 }
720
721 29897 int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
722 {
723 29897 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 = codec2->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 80600 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 80600 const FFCodec *codec2 = ffcodec(codec);
774
775
9/10
✓ Branch 0 taken 33402 times.
✓ Branch 1 taken 6992 times.
✓ Branch 2 taken 3250 times.
✓ Branch 3 taken 3252 times.
✓ Branch 4 taken 3250 times.
✓ Branch 5 taken 6960 times.
✓ Branch 6 taken 6993 times.
✓ Branch 7 taken 9575 times.
✓ Branch 8 taken 6926 times.
✗ Branch 9 not taken.
80600 switch (config) {
776 33402 case AV_CODEC_CONFIG_PIX_FORMAT:
777
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 33402 times.
✓ Branch 2 taken 1909 times.
✓ Branch 3 taken 31493 times.
✓ Branch 4 taken 1909 times.
✓ Branch 5 taken 20423 times.
55734 WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, pix_fmts, pix_fmt, enum AVPixelFormat, pix_fmt == AV_PIX_FMT_NONE);
778 6992 case AV_CODEC_CONFIG_FRAME_RATE:
779
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 6992 times.
✓ Branch 2 taken 77 times.
✓ Branch 3 taken 6915 times.
✓ Branch 4 taken 77 times.
✓ Branch 5 taken 4235 times.
11227 WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, supported_framerates, framerate, AVRational, framerate.num == 0);
780 3250 case AV_CODEC_CONFIG_SAMPLE_RATE:
781
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 3250 times.
✓ Branch 2 taken 413 times.
✓ Branch 3 taken 2837 times.
✓ Branch 4 taken 413 times.
✓ Branch 5 taken 2583 times.
6246 WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, supported_samplerates, samplerate, int, samplerate == 0);
782 3252 case AV_CODEC_CONFIG_SAMPLE_FORMAT:
783
4/6
✗ Branch 0 not taken.
✓ Branch 1 taken 3252 times.
✓ Branch 2 taken 3252 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3252 times.
✓ Branch 5 taken 3502 times.
10006 WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, sample_fmts, sample_fmt, enum AVSampleFormat, sample_fmt == AV_SAMPLE_FMT_NONE);
784 3250 case AV_CODEC_CONFIG_CHANNEL_LAYOUT:
785
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 3250 times.
✓ Branch 2 taken 421 times.
✓ Branch 3 taken 2829 times.
✓ Branch 4 taken 421 times.
✓ Branch 5 taken 1548 times.
4798 WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, ch_layouts, ch_layout, AVChannelLayout, ch_layout.nb_channels == 0);
786
787 6960 case AV_CODEC_CONFIG_COLOR_RANGE:
788
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6960 times.
6960 if (codec->type != AVMEDIA_TYPE_VIDEO)
789 ✗ return AVERROR(EINVAL);
790 6960 unsigned color_ranges = codec2->color_ranges;
791
2/2
✓ Branch 0 taken 506 times.
✓ Branch 1 taken 6454 times.
6960 if (color_ranges)
792 506 *out_configs = color_range_tab + offset_tab[color_ranges];
793 else
794 6454 *out_configs = NULL;
795 6960 *out_num_configs = av_popcount(color_ranges);
796 6960 return 0;
797
798 6993 case AV_CODEC_CONFIG_COLOR_SPACE:
799 6993 *out_configs = NULL;
800 6993 *out_num_configs = 0;
801 6993 return 0;
802
803 9575 case AV_CODEC_CONFIG_ALPHA_MODE:
804
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9575 times.
9575 if (codec->type != AVMEDIA_TYPE_VIDEO)
805 ✗ return AVERROR(EINVAL);
806 9575 unsigned alpha_modes = codec2->alpha_modes;
807
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 9541 times.
9575 if (alpha_modes)
808 34 *out_configs = alpha_mode_tab + offset_tab[alpha_modes];
809 else
810 9541 *out_configs = NULL;
811 9575 *out_num_configs = av_popcount(alpha_modes);
812 9575 return 0;
813
814 6926 case AV_CODEC_CONFIG_CHROMA_LOCATION:
815
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 6926 times.
✓ Branch 2 taken 107 times.
✓ Branch 3 taken 6819 times.
✓ Branch 4 taken 107 times.
✓ Branch 5 taken 107 times.
7140 WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, chroma_locations, loc,
816 enum AVChromaLocation, loc == AVCHROMA_LOC_UNSPECIFIED);
817
818 ✗ default:
819 ✗ return AVERROR(EINVAL);
820 }
821 }
822
823 80700 int avcodec_get_supported_config(const AVCodecContext *avctx, const AVCodec *codec,
824 enum AVCodecConfig config, unsigned flags,
825 const void **out, int *out_num)
826 {
827 const FFCodec *codec2;
828 80700 int dummy_num = 0;
829
2/2
✓ Branch 0 taken 80698 times.
✓ Branch 1 taken 2 times.
80700 if (!codec)
830 80698 codec = avctx->codec;
831
2/2
✓ Branch 0 taken 52197 times.
✓ Branch 1 taken 28503 times.
80700 if (!out_num)
832 52197 out_num = &dummy_num;
833
834 80700 codec2 = ffcodec(codec);
835
2/2
✓ Branch 0 taken 735 times.
✓ Branch 1 taken 79965 times.
80700 if (codec2->get_supported_config) {
836 735 return codec2->get_supported_config(avctx, codec, config, flags, out, out_num);
837 } else {
838 79965 return ff_default_get_supported_config(avctx, codec, config, flags, out, out_num);
839 }
840 }
841
842 ✗ int av_packet_side_data_from_frame(AVPacketSideData **psd, int *pnb_sd,
843 const AVFrameSideData *src, unsigned int flags)
844 {
845 ✗ AVPacketSideData *sd = NULL;
846
847 ✗ for (unsigned j = 0; ff_sd_global_map[j].packet < AV_PKT_DATA_NB; j++) {
848 ✗ if (ff_sd_global_map[j].frame != src->type)
849 ✗ continue;
850
851 ✗ sd = av_packet_side_data_new(psd, pnb_sd, ff_sd_global_map[j].packet,
852 ✗ src->size, 0);
853
854 ✗ if (!sd)
855 ✗ return AVERROR(ENOMEM);
856
857 ✗ memcpy(sd->data, src->data, src->size);
858 ✗ break;
859 }
860
861 ✗ if (!sd)
862 ✗ return AVERROR(EINVAL);
863
864 ✗ return 0;
865 }
866
867 2 int av_packet_side_data_to_frame(AVFrameSideData ***psd, int *pnb_sd,
868 const AVPacketSideData *src, unsigned int flags)
869 {
870 2 AVFrameSideData *sd = NULL;
871
872
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 for (unsigned j = 0; ff_sd_global_map[j].packet < AV_PKT_DATA_NB; j++) {
873
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 2 times.
19 if (ff_sd_global_map[j].packet != src->type)
874 17 continue;
875
876 2 sd = av_frame_side_data_new(psd, pnb_sd, ff_sd_global_map[j].frame,
877 2 src->size, flags);
878
879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!sd)
880 ✗ return AVERROR(ENOMEM);
881
882 2 memcpy(sd->data, src->data, src->size);
883 2 break;
884 }
885
886
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!sd)
887 ✗ return AVERROR(EINVAL);
888
889 2 return 0;
890 }
891