FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/avcodec.c
Date: 2026-05-27 12:09:14
Exec Total Coverage
Lines: 369 500 73.8%
Functions: 16 18 88.9%
Branches: 307 449 68.4%

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 27837 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 1390034 times.
✓ Branch 1 taken 27837 times.
1417871 for (i = 0; i < count; i++) {
78 1390034 size_t offset = i * size;
79
2/2
✓ Branch 0 taken 1362197 times.
✓ Branch 1 taken 27837 times.
1390034 int r = func(c, FF_PTR_ADD((char *)arg, offset));
80
2/2
✓ Branch 0 taken 930 times.
✓ Branch 1 taken 1389104 times.
1390034 if (ret)
81 930 ret[i] = r;
82 }
83 27837 emms_c();
84 27837 return 0;
85 }
86
87 8752 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 231759 times.
✓ Branch 1 taken 8752 times.
240511 for (i = 0; i < count; i++) {
92 231759 int r = func(c, arg, i, 0);
93
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 231709 times.
231759 if (ret)
94 50 ret[i] = r;
95 }
96 8752 emms_c();
97 8752 return 0;
98 }
99
100 static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
101
102 67565 static void lock_avcodec(const FFCodec *codec)
103 {
104
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 67565 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
67565 if (codec->caps_internal & FF_CODEC_CAP_NOT_INIT_THREADSAFE && codec->init)
105 ff_mutex_lock(&codec_mutex);
106 67565 }
107
108 67565 static void unlock_avcodec(const FFCodec *codec)
109 {
110
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 67565 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
67565 if (codec->caps_internal & FF_CODEC_CAP_NOT_INIT_THREADSAFE && codec->init)
111 ff_mutex_unlock(&codec_mutex);
112 67565 }
113
114 29153 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 23868 times.
✓ Branch 1 taken 5285 times.
✗ Branch 2 not taken.
29153 switch (ctx->codec_type) {
120 23868 case AVMEDIA_TYPE_VIDEO:
121 case AVMEDIA_TYPE_DATA:
122 case AVMEDIA_TYPE_SUBTITLE:
123 case AVMEDIA_TYPE_ATTACHMENT:
124 23868 bit_rate = ctx->bit_rate;
125 23868 break;
126 5285 case AVMEDIA_TYPE_AUDIO:
127 5285 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
128
2/2
✓ Branch 0 taken 2497 times.
✓ Branch 1 taken 2788 times.
5285 if (bits_per_sample) {
129 2497 bit_rate = ctx->sample_rate * (int64_t)ctx->ch_layout.nb_channels;
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2497 times.
2497 if (bit_rate > INT64_MAX / bits_per_sample) {
131 bit_rate = 0;
132 } else
133 2497 bit_rate *= bits_per_sample;
134 } else
135 2788 bit_rate = ctx->bit_rate;
136 5285 break;
137 default:
138 bit_rate = 0;
139 break;
140 }
141 29153 return bit_rate;
142 }
143
144 37947 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
145 {
146 37947 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 37947 times.
37947 if (avcodec_is_open(avctx))
152 return 0;
153
154
3/4
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 37912 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 35 times.
37947 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 37912 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 29229 times.
✓ Branch 3 taken 8683 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 29229 times.
37947 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 37912 times.
37947 if (!codec)
164 35 codec = avctx->codec;
165 37947 codec2 = ffcodec(codec);
166
167
2/4
✓ Branch 0 taken 37947 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37947 times.
✗ Branch 3 not taken.
37947 if ((avctx->codec_type != AVMEDIA_TYPE_UNKNOWN && avctx->codec_type != codec->type) ||
168
2/4
✓ Branch 0 taken 37947 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37947 times.
37947 (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 37947 avctx->codec_type = codec->type;
174 37947 avctx->codec_id = codec->id;
175 37947 avctx->codec = codec;
176
177
2/4
✓ Branch 0 taken 37947 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37947 times.
37947 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 9078 times.
✓ Branch 1 taken 28869 times.
37947 e = options ? av_dict_get(*options, "codec_whitelist", NULL, 0) : NULL;
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37947 times.
37947 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 37947 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
37947 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 16159 times.
✓ Branch 2 taken 21788 times.
37947 avci = ff_codec_is_decoder(codec) ?
195 16159 ff_decode_internal_alloc() :
196 21788 ff_encode_internal_alloc();
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37947 times.
37947 if (!avci) {
198 ret = AVERROR(ENOMEM);
199 goto end;
200 }
201 37947 avctx->internal = avci;
202
203 37947 avci->buffer_frame = av_frame_alloc();
204 37947 avci->buffer_pkt = av_packet_alloc();
205
2/4
✓ Branch 0 taken 37947 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37947 times.
37947 if (!avci->buffer_frame || !avci->buffer_pkt) {
206 ret = AVERROR(ENOMEM);
207 goto free_and_end;
208 }
209
210
2/2
✓ Branch 0 taken 16844 times.
✓ Branch 1 taken 21103 times.
37947 if (codec2->priv_data_size > 0) {
211
2/2
✓ Branch 0 taken 8519 times.
✓ Branch 1 taken 8325 times.
16844 if (!avctx->priv_data) {
212 8519 avctx->priv_data = av_mallocz(codec2->priv_data_size);
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8519 times.
8519 if (!avctx->priv_data) {
214 ret = AVERROR(ENOMEM);
215 goto free_and_end;
216 }
217
2/2
✓ Branch 0 taken 2542 times.
✓ Branch 1 taken 5977 times.
8519 if (codec->priv_class) {
218 2542 *(const AVClass **)avctx->priv_data = codec->priv_class;
219 2542 av_opt_set_defaults(avctx->priv_data);
220 }
221 }
222 } else {
223 21103 avctx->priv_data = NULL;
224 }
225
226 37947 ret = av_opt_set_dict2(avctx, options, AV_OPT_SEARCH_CHILDREN);
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37947 times.
37947 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 37912 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.
37947 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 37912 times.
✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
37947 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 28406 times.
✓ Branch 1 taken 9506 times.
✓ Branch 2 taken 28406 times.
✗ Branch 3 not taken.
37912 else if (avctx->width && avctx->height)
236 28406 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37947 times.
37947 if (ret < 0)
238 goto free_and_end;
239 }
240
241
5/8
✓ Branch 0 taken 9506 times.
✓ Branch 1 taken 28441 times.
✓ Branch 2 taken 9506 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9506 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 9506 times.
37947 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
242
1/2
✓ Branch 1 taken 28441 times.
✗ Branch 2 not taken.
28441 && ( 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 28441 times.
28441 || 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 28441 times.
✓ Branch 1 taken 9506 times.
✓ Branch 2 taken 28441 times.
✗ Branch 3 not taken.
37947 if (avctx->width > 0 && avctx->height > 0) {
249
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28441 times.
28441 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 37947 times.
✗ Branch 1 not taken.
37947 if (avctx->sample_rate < 0 ||
261
4/4
✓ Branch 0 taken 33215 times.
✓ Branch 1 taken 4732 times.
✓ Branch 2 taken 281 times.
✓ Branch 3 taken 32934 times.
37947 avctx->sample_rate == 0 && avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 281 times.
281 !(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 37947 times.
37947 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 5013 times.
✓ Branch 1 taken 32934 times.
✓ Branch 2 taken 280 times.
✓ Branch 3 taken 4733 times.
37947 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && !avctx->ch_layout.nb_channels
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 280 times.
280 && !(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 4733 times.
✓ Branch 1 taken 33214 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4733 times.
37947 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 37947 times.
37947 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 37947 avctx->frame_num = 0;
294 37947 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
295
296
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 37937 times.
37947 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 5013 times.
✓ Branch 1 taken 32934 times.
37947 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
313
3/4
✓ Branch 0 taken 1378 times.
✓ Branch 1 taken 3635 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1378 times.
5013 (!avctx->time_base.num || !avctx->time_base.den)) {
314 3635 avctx->time_base.num = 1;
315 3635 avctx->time_base.den = avctx->sample_rate;
316 }
317
318
2/2
✓ Branch 1 taken 21788 times.
✓ Branch 2 taken 16159 times.
37947 if (ff_codec_is_encoder(avctx->codec))
319 21788 ret = ff_encode_preinit(avctx);
320 else
321 16159 ret = ff_decode_preinit(avctx);
322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37947 times.
37947 if (ret < 0)
323 goto free_and_end;
324
325
2/2
✓ Branch 0 taken 36262 times.
✓ Branch 1 taken 1685 times.
37947 if (HAVE_THREADS && !avci->frame_thread_encoder) {
326 /* Frame-threaded decoders call FFCodec.init for their child contexts. */
327 36262 lock_avcodec(codec2);
328 36262 ret = ff_thread_init(avctx);
329 36262 unlock_avcodec(codec2);
330
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36262 times.
36262 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 1695 times.
✓ Branch 1 taken 36252 times.
37947 if (!(avctx->active_thread_type & FF_THREAD_FRAME) ||
338
2/2
✓ Branch 0 taken 1685 times.
✓ Branch 1 taken 10 times.
1695 avci->frame_thread_encoder) {
339
2/2
✓ Branch 0 taken 31303 times.
✓ Branch 1 taken 6634 times.
37937 if (codec2->init) {
340 31303 lock_avcodec(codec2);
341 31303 ret = codec2->init(avctx);
342 31303 unlock_avcodec(codec2);
343
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 31295 times.
31303 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 37929 avci->needs_close = 1;
349 }
350
351 37939 ret=0;
352
353
2/2
✓ Branch 1 taken 16151 times.
✓ Branch 2 taken 21788 times.
37939 if (ff_codec_is_decoder(avctx->codec)) {
354
2/2
✓ Branch 0 taken 11346 times.
✓ Branch 1 taken 4805 times.
16151 if (!avctx->bit_rate)
355 11346 avctx->bit_rate = get_bit_rate(avctx);
356
357
358
2/2
✓ Branch 0 taken 3619 times.
✓ Branch 1 taken 12532 times.
16151 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
359
3/4
✓ Branch 0 taken 3174 times.
✓ Branch 1 taken 445 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3174 times.
3619 !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 16151 avci->skip_samples = avctx->delay;
366
367 /* validate channel layout from the decoder */
368
3/4
✓ Branch 0 taken 3402 times.
✓ Branch 1 taken 12749 times.
✓ Branch 3 taken 3402 times.
✗ Branch 4 not taken.
16151 if ((avctx->ch_layout.nb_channels && !av_channel_layout_check(&avctx->ch_layout)) ||
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16151 times.
16151 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 16151 times.
16151 if (avctx->bits_per_coded_sample < 0) {
374 ret = AVERROR(EINVAL);
375 goto free_and_end;
376 }
377 }
378
2/2
✓ Branch 0 taken 5442 times.
✓ Branch 1 taken 32497 times.
37939 if (codec->priv_class)
379
1/2
✓ Branch 0 taken 5442 times.
✗ Branch 1 not taken.
5442 av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
380
381 37939 end:
382
383 37947 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 1074 void avsubtitle_free(AVSubtitle *sub)
422 {
423 int i;
424
425
2/2
✓ Branch 0 taken 1019 times.
✓ Branch 1 taken 1074 times.
2093 for (i = 0; i < sub->num_rects; i++) {
426 1019 AVSubtitleRect *const rect = sub->rects[i];
427
428 1019 av_freep(&rect->data[0]);
429 1019 av_freep(&rect->data[1]);
430 1019 av_freep(&rect->data[2]);
431 1019 av_freep(&rect->data[3]);
432 1019 av_freep(&rect->text);
433 1019 av_freep(&rect->ass);
434
435 1019 av_freep(&sub->rects[i]);
436 }
437
438 1074 av_freep(&sub->rects);
439
440 1074 memset(sub, 0, sizeof(*sub));
441 1074 }
442
443 65344 av_cold void ff_codec_close(AVCodecContext *avctx)
444 {
445 int i;
446
447
2/2
✓ Branch 1 taken 37947 times.
✓ Branch 2 taken 27397 times.
65344 if (avcodec_is_open(avctx)) {
448 37947 AVCodecInternal *avci = avctx->internal;
449
450 #if CONFIG_FRAME_THREAD_ENCODER
451
4/4
✓ Branch 0 taken 15153 times.
✓ Branch 1 taken 22794 times.
✓ Branch 2 taken 1685 times.
✓ Branch 3 taken 13468 times.
37947 if (avci->frame_thread_encoder && avctx->thread_count > 1) {
452 1685 ff_frame_thread_encoder_free(avctx);
453 }
454 #endif
455
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 37905 times.
37947 if (HAVE_THREADS && avci->thread_ctx)
456 42 ff_thread_free(avctx);
457
4/4
✓ Branch 0 taken 37929 times.
✓ Branch 1 taken 18 times.
✓ Branch 3 taken 7779 times.
✓ Branch 4 taken 30150 times.
37947 if (avci->needs_close && ffcodec(avctx->codec)->close)
458 7779 ffcodec(avctx->codec)->close(avctx);
459 37947 avci->byte_buffer_size = 0;
460 37947 av_freep(&avci->byte_buffer);
461 37947 av_frame_free(&avci->buffer_frame);
462 37947 av_packet_free(&avci->buffer_pkt);
463 37947 av_packet_free(&avci->last_pkt_props);
464
465 37947 av_packet_free(&avci->in_pkt);
466 37947 av_frame_free(&avci->in_frame);
467 37947 av_frame_free(&avci->recon_frame);
468
469 37947 av_refstruct_unref(&avci->pool);
470 37947 av_refstruct_pool_uninit(&avci->progress_frame_pool);
471
2/2
✓ Branch 1 taken 16159 times.
✓ Branch 2 taken 21788 times.
37947 if (av_codec_is_decoder(avctx->codec))
472 16159 ff_decode_internal_uninit(avctx);
473
474 37947 ff_hwaccel_uninit(avctx);
475
476 37947 av_bsf_free(&avci->bsf);
477
478 #if CONFIG_LCMS2
479 ff_icc_context_uninit(&avci->icc);
480 #endif
481
482 37947 av_freep(&avctx->internal);
483 }
484
485
2/2
✓ Branch 0 taken 4408 times.
✓ Branch 1 taken 65344 times.
69752 for (i = 0; i < avctx->nb_coded_side_data; i++)
486 4408 av_freep(&avctx->coded_side_data[i].data);
487 65344 av_freep(&avctx->coded_side_data);
488 65344 avctx->nb_coded_side_data = 0;
489 65344 av_frame_side_data_free(&avctx->decoded_side_data,
490 &avctx->nb_decoded_side_data);
491
492 65344 av_buffer_unref(&avctx->hw_frames_ctx);
493 65344 av_buffer_unref(&avctx->hw_device_ctx);
494
495
5/6
✓ Branch 0 taken 25355 times.
✓ Branch 1 taken 39989 times.
✓ Branch 2 taken 25355 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7984 times.
✓ Branch 5 taken 17371 times.
65344 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
496 7984 av_opt_free(avctx->priv_data);
497 65344 av_opt_free(avctx);
498 65344 av_freep(&avctx->priv_data);
499
2/2
✓ Branch 1 taken 21789 times.
✓ Branch 2 taken 43555 times.
65344 if (av_codec_is_encoder(avctx->codec)) {
500 21789 av_freep(&avctx->extradata);
501 21789 avctx->extradata_size = 0;
502
2/2
✓ Branch 1 taken 33349 times.
✓ Branch 2 taken 10206 times.
43555 } else if (av_codec_is_decoder(avctx->codec))
503 33349 av_freep(&avctx->subtitle_header);
504
505 65344 avctx->codec = NULL;
506 65344 avctx->active_thread_type = 0;
507 65344 }
508
509 22721 static const char *unknown_if_null(const char *str)
510 {
511
1/2
✓ Branch 0 taken 22721 times.
✗ Branch 1 not taken.
22721 return str ? str : "unknown";
512 }
513
514 17816 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
515 {
516 const char *codec_type;
517 const char *codec_name;
518 17816 const char *profile = NULL;
519 AVBPrint bprint;
520 int64_t bitrate;
521 17816 int new_line = 0;
522 AVRational display_aspect_ratio;
523
2/2
✓ Branch 0 taken 17789 times.
✓ Branch 1 taken 27 times.
17816 const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
524 const char *str;
525
526
2/4
✓ Branch 0 taken 17816 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 17816 times.
17816 if (!buf || buf_size <= 0)
527 9 return;
528 17816 av_bprint_init_for_buffer(&bprint, buf, buf_size);
529 17816 codec_type = av_get_media_type_string(enc->codec_type);
530 17816 codec_name = avcodec_get_name(enc->codec_id);
531 17816 profile = avcodec_profile_name(enc->codec_id, enc->profile);
532
533
1/2
✓ Branch 0 taken 17816 times.
✗ Branch 1 not taken.
17816 av_bprintf(&bprint, "%s: %s", codec_type ? codec_type : "unknown",
534 codec_name);
535 17816 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
536
537
4/4
✓ Branch 0 taken 8536 times.
✓ Branch 1 taken 9280 times.
✓ Branch 2 taken 345 times.
✓ Branch 3 taken 8191 times.
17816 if (enc->codec && strcmp(enc->codec->name, codec_name))
538 345 av_bprintf(&bprint, " (%s)", enc->codec->name);
539
540
2/2
✓ Branch 0 taken 2143 times.
✓ Branch 1 taken 15673 times.
17816 if (profile)
541 2143 av_bprintf(&bprint, " (%s)", profile);
542
2/2
✓ Branch 0 taken 13637 times.
✓ Branch 1 taken 4179 times.
17816 if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
543
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13637 times.
13637 && 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 10026 times.
✓ Branch 1 taken 7790 times.
17816 if (enc->codec_tag)
549 10026 av_bprintf(&bprint, " (%s / 0x%04X)",
550 10026 av_fourcc2str(enc->codec_tag), enc->codec_tag);
551
552
5/5
✓ Branch 0 taken 13637 times.
✓ Branch 1 taken 3900 times.
✓ Branch 2 taken 90 times.
✓ Branch 3 taken 180 times.
✓ Branch 4 taken 9 times.
17816 switch (enc->codec_type) {
553 13637 case AVMEDIA_TYPE_VIDEO:
554 {
555 unsigned len;
556
557 13637 av_bprintf(&bprint, "%s%s", separator,
558
2/2
✓ Branch 0 taken 13583 times.
✓ Branch 1 taken 54 times.
13637 enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
559 13583 unknown_if_null(av_get_pix_fmt_name(enc->pix_fmt)));
560
561 13637 av_bprint_chars(&bprint, '(', 1);
562 13637 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 13637 times.
13637 if (!av_bprint_is_complete(&bprint))
568 return;
569
570
3/4
✓ Branch 0 taken 1234 times.
✓ Branch 1 taken 12403 times.
✓ Branch 2 taken 1234 times.
✗ Branch 3 not taken.
13637 if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
571
2/2
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 1207 times.
1234 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 11235 times.
✓ Branch 1 taken 2402 times.
✓ Branch 2 taken 11235 times.
✗ Branch 3 not taken.
24872 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED &&
574 11235 (str = av_color_range_name(enc->color_range)))
575 11235 av_bprintf(&bprint, "%s, ", str);
576
577
2/2
✓ Branch 0 taken 10710 times.
✓ Branch 1 taken 2927 times.
13637 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
578
2/2
✓ Branch 0 taken 10708 times.
✓ Branch 1 taken 2 times.
10710 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
579
2/2
✓ Branch 0 taken 117 times.
✓ Branch 1 taken 10591 times.
10708 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
580 3046 const char *col = unknown_if_null(av_color_space_name(enc->colorspace));
581 3046 const char *pri = unknown_if_null(av_color_primaries_name(enc->color_primaries));
582 3046 const char *trc = unknown_if_null(av_color_transfer_name(enc->color_trc));
583
4/4
✓ Branch 0 taken 294 times.
✓ Branch 1 taken 2752 times.
✓ Branch 2 taken 136 times.
✓ Branch 3 taken 158 times.
3046 if (strcmp(col, pri) || strcmp(col, trc)) {
584 2888 new_line = 1;
585 2888 av_bprintf(&bprint, "%s/%s/%s, ", col, pri, trc);
586 } else
587 158 av_bprintf(&bprint, "%s, ", col);
588 }
589
590
2/2
✓ Branch 0 taken 7633 times.
✓ Branch 1 taken 6004 times.
13637 if (enc->field_order != AV_FIELD_UNKNOWN) {
591 7633 const char *field_order = "progressive";
592
2/2
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 7491 times.
7633 if (enc->field_order == AV_FIELD_TT)
593 142 field_order = "top first";
594
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 7440 times.
7491 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 7077 times.
7440 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 6943 times.
7077 else if (enc->field_order == AV_FIELD_BT)
599 134 field_order = "bottom coded first (swapped)";
600
601 7633 av_bprintf(&bprint, "%s, ", field_order);
602 }
603
604
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13637 times.
13637 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 1125 times.
✓ Branch 1 taken 12512 times.
13637 if (len == bprint.len) {
610 1125 bprint.str[len - 1] = '\0';
611 1125 bprint.len--;
612 } else {
613
1/2
✓ Branch 0 taken 12512 times.
✗ Branch 1 not taken.
12512 if (bprint.len - 2 < bprint.size) {
614 /* Erase the last ", " */
615 12512 bprint.len -= 2;
616 12512 bprint.str[bprint.len] = '\0';
617 }
618 12512 av_bprint_chars(&bprint, ')', 1);
619 }
620 }
621
622
2/2
✓ Branch 0 taken 13615 times.
✓ Branch 1 taken 22 times.
13637 if (enc->width) {
623
2/2
✓ Branch 0 taken 2886 times.
✓ Branch 1 taken 10729 times.
13615 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 13615 times.
13615 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 2685 times.
✓ Branch 1 taken 10930 times.
13615 if (enc->sample_aspect_ratio.num) {
634 2685 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
635 2685 enc->width * (int64_t)enc->sample_aspect_ratio.num,
636 2685 enc->height * (int64_t)enc->sample_aspect_ratio.den,
637 1024 * 1024);
638 2685 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 13615 times.
13615 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 7274 times.
✓ Branch 1 taken 6363 times.
13637 if (encode) {
649 7274 av_bprintf(&bprint, ", q=%d-%d", enc->qmin, enc->qmax);
650 } else {
651 #if FF_API_CODEC_PROPS
652 FF_DISABLE_DEPRECATION_WARNINGS
653
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6363 times.
6363 if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
654 av_bprintf(&bprint, ", Closed Captions");
655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6363 times.
6363 if (enc->properties & FF_CODEC_PROPERTY_FILM_GRAIN)
656 av_bprintf(&bprint, ", Film Grain");
657
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6363 times.
6363 if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
658 av_bprintf(&bprint, ", lossless");
659 FF_ENABLE_DEPRECATION_WARNINGS
660 #endif
661 }
662 13637 break;
663 3900 case AVMEDIA_TYPE_AUDIO:
664 3900 av_bprintf(&bprint, "%s", separator);
665
666
2/2
✓ Branch 0 taken 3884 times.
✓ Branch 1 taken 16 times.
3900 if (enc->sample_rate) {
667 3884 av_bprintf(&bprint, "%d Hz, ", enc->sample_rate);
668 }
669 3900 av_channel_layout_describe_bprint(&enc->ch_layout, &bprint);
670
2/4
✓ Branch 0 taken 3900 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3900 times.
✗ Branch 3 not taken.
7800 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE &&
671 3900 (str = av_get_sample_fmt_name(enc->sample_fmt))) {
672 3900 av_bprintf(&bprint, ", %s", str);
673 }
674
2/2
✓ Branch 0 taken 2065 times.
✓ Branch 1 taken 1835 times.
3900 if ( enc->bits_per_raw_sample > 0
675
2/2
✓ Branch 1 taken 244 times.
✓ Branch 2 taken 1821 times.
2065 && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
676 244 av_bprintf(&bprint, " (%d bit)", enc->bits_per_raw_sample);
677
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 3894 times.
3900 if (av_log_get_level() >= AV_LOG_VERBOSE) {
678
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (enc->initial_padding)
679 3 av_bprintf(&bprint, ", delay %d", enc->initial_padding);
680
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (enc->trailing_padding)
681 av_bprintf(&bprint, ", padding %d", enc->trailing_padding);
682 }
683 3900 break;
684 90 case AVMEDIA_TYPE_DATA:
685
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
90 if (av_log_get_level() >= AV_LOG_DEBUG) {
686 int g = av_gcd(enc->time_base.num, enc->time_base.den);
687 if (g)
688 av_bprintf(&bprint, ", %d/%d",
689 enc->time_base.num / g, enc->time_base.den / g);
690 }
691 90 break;
692 180 case AVMEDIA_TYPE_SUBTITLE:
693
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 147 times.
180 if (enc->width)
694 33 av_bprintf(&bprint, ", %dx%d", enc->width, enc->height);
695 180 break;
696 9 default:
697 9 return;
698 }
699
2/2
✓ Branch 0 taken 9137 times.
✓ Branch 1 taken 8670 times.
17807 if (encode) {
700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9137 times.
9137 if (enc->flags & AV_CODEC_FLAG_PASS1)
701 av_bprintf(&bprint, ", pass 1");
702
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9137 times.
9137 if (enc->flags & AV_CODEC_FLAG_PASS2)
703 av_bprintf(&bprint, ", pass 2");
704 }
705 17807 bitrate = get_bit_rate(enc);
706
2/2
✓ Branch 0 taken 11443 times.
✓ Branch 1 taken 6364 times.
17807 if (bitrate != 0) {
707 11443 av_bprintf(&bprint, ", %"PRId64" kb/s", bitrate / 1000);
708
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6364 times.
6364 } else if (enc->rc_max_rate > 0) {
709 av_bprintf(&bprint, ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
710 }
711 }
712
713 3192960 int avcodec_is_open(AVCodecContext *s)
714 {
715 3192960 return !!s->internal;
716 }
717
718 850548 int attribute_align_arg avcodec_receive_frame_flags(AVCodecContext *avctx,
719 AVFrame *frame, unsigned flags)
720 {
721 850548 av_frame_unref(frame);
722
723
2/4
✓ Branch 1 taken 850548 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 850548 times.
850548 if (!avcodec_is_open(avctx) || !avctx->codec)
724 return AVERROR(EINVAL);
725
726
2/2
✓ Branch 1 taken 850486 times.
✓ Branch 2 taken 62 times.
850548 if (ff_codec_is_decoder(avctx->codec))
727 850486 return ff_decode_receive_frame(avctx, frame, flags);
728 62 return ff_encode_receive_frame(avctx, frame);
729 }
730
731 28024 int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
732 {
733 28024 return avcodec_receive_frame_flags(avctx, frame, 0);
734 }
735
736 #define WRAP_CONFIG(allowed_type, field, var, field_type, sentinel_check) \
737 do { \
738 if (codec->type != (allowed_type)) \
739 return AVERROR(EINVAL); \
740 const field_type *ptr = codec->field; \
741 *out_configs = ptr; \
742 if (ptr) { \
743 for (int i = 0;; i++) { \
744 const field_type var = ptr[i]; \
745 if (sentinel_check) { \
746 *out_num_configs = i; \
747 break; \
748 } \
749 } \
750 } else \
751 *out_num_configs = 0; \
752 return 0; \
753 } while (0)
754
755 static const enum AVColorRange color_range_tab[] = {
756 AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG, AVCOL_RANGE_UNSPECIFIED,
757 AVCOL_RANGE_MPEG, AVCOL_RANGE_UNSPECIFIED,
758 };
759
760 static const enum AVAlphaMode alpha_mode_tab[] = {
761 AVALPHA_MODE_PREMULTIPLIED, AVALPHA_MODE_STRAIGHT, AVALPHA_MODE_UNSPECIFIED,
762 AVALPHA_MODE_PREMULTIPLIED, AVALPHA_MODE_UNSPECIFIED
763 };
764
765 static_assert((int)AVCOL_RANGE_MPEG == (int)AVALPHA_MODE_PREMULTIPLIED, "unexpected enum values");
766 static_assert((int)AVCOL_RANGE_JPEG == (int)AVALPHA_MODE_STRAIGHT, "unexpected enum values");
767 static_assert(AVCOL_RANGE_UNSPECIFIED == 0 && AVALPHA_MODE_UNSPECIFIED == 0, "unexpected enum values");
768 static_assert(AVCOL_RANGE_NB == 3 && AVALPHA_MODE_NB == 3, "unexpected enum values");
769
770 static const uint8_t offset_tab[] = {
771 [AVCOL_RANGE_MPEG] = 3,
772 [AVCOL_RANGE_JPEG] = 1,
773 [AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG] = 0,
774 };
775
776 70911 int ff_default_get_supported_config(const AVCodecContext *avctx,
777 const AVCodec *codec,
778 enum AVCodecConfig config,
779 unsigned flags,
780 const void **out_configs,
781 int *out_num_configs)
782 {
783 70911 const FFCodec *codec2 = ffcodec(codec);
784
785
8/9
✓ Branch 0 taken 32598 times.
✓ Branch 1 taken 6880 times.
✓ Branch 2 taken 2772 times.
✓ Branch 3 taken 2772 times.
✓ Branch 4 taken 2772 times.
✓ Branch 5 taken 6854 times.
✓ Branch 6 taken 6881 times.
✓ Branch 7 taken 9382 times.
✗ Branch 8 not taken.
70911 switch (config) {
786 FF_DISABLE_DEPRECATION_WARNINGS
787 32598 case AV_CODEC_CONFIG_PIX_FORMAT:
788
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 32598 times.
✓ Branch 2 taken 1835 times.
✓ Branch 3 taken 30763 times.
✓ Branch 4 taken 1835 times.
✓ Branch 5 taken 20036 times.
54469 WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, pix_fmts, pix_fmt, enum AVPixelFormat, pix_fmt == AV_PIX_FMT_NONE);
789 6880 case AV_CODEC_CONFIG_FRAME_RATE:
790
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 6880 times.
✓ Branch 2 taken 63 times.
✓ Branch 3 taken 6817 times.
✓ Branch 4 taken 63 times.
✓ Branch 5 taken 3367 times.
10247 WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, supported_framerates, framerate, AVRational, framerate.num == 0);
791 2772 case AV_CODEC_CONFIG_SAMPLE_RATE:
792
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2772 times.
✓ Branch 2 taken 160 times.
✓ Branch 3 taken 2612 times.
✓ Branch 4 taken 160 times.
✓ Branch 5 taken 868 times.
3800 WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, supported_samplerates, samplerate, int, samplerate == 0);
793 2772 case AV_CODEC_CONFIG_SAMPLE_FORMAT:
794
4/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2772 times.
✓ Branch 2 taken 2772 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2772 times.
✓ Branch 5 taken 3014 times.
8558 WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, sample_fmts, sample_fmt, enum AVSampleFormat, sample_fmt == AV_SAMPLE_FMT_NONE);
795 2772 case AV_CODEC_CONFIG_CHANNEL_LAYOUT:
796
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2772 times.
✓ Branch 2 taken 188 times.
✓ Branch 3 taken 2584 times.
✓ Branch 4 taken 188 times.
✓ Branch 5 taken 928 times.
3700 WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, ch_layouts, ch_layout, AVChannelLayout, ch_layout.nb_channels == 0);
797 FF_ENABLE_DEPRECATION_WARNINGS
798
799 6854 case AV_CODEC_CONFIG_COLOR_RANGE:
800
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6854 times.
6854 if (codec->type != AVMEDIA_TYPE_VIDEO)
801 return AVERROR(EINVAL);
802 6854 unsigned color_ranges = codec2->color_ranges;
803
2/2
✓ Branch 0 taken 481 times.
✓ Branch 1 taken 6373 times.
6854 if (color_ranges)
804 481 *out_configs = color_range_tab + offset_tab[color_ranges];
805 else
806 6373 *out_configs = NULL;
807 6854 *out_num_configs = av_popcount(color_ranges);
808 6854 return 0;
809
810 6881 case AV_CODEC_CONFIG_COLOR_SPACE:
811 6881 *out_configs = NULL;
812 6881 *out_num_configs = 0;
813 6881 return 0;
814
815 9382 case AV_CODEC_CONFIG_ALPHA_MODE:
816
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9382 times.
9382 if (codec->type != AVMEDIA_TYPE_VIDEO)
817 return AVERROR(EINVAL);
818 9382 unsigned alpha_modes = codec2->alpha_modes;
819
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 9349 times.
9382 if (alpha_modes)
820 33 *out_configs = alpha_mode_tab + offset_tab[alpha_modes];
821 else
822 9349 *out_configs = NULL;
823 9382 *out_num_configs = av_popcount(alpha_modes);
824 9382 return 0;
825
826 default:
827 return AVERROR(EINVAL);
828 }
829 }
830
831 70938 int avcodec_get_supported_config(const AVCodecContext *avctx, const AVCodec *codec,
832 enum AVCodecConfig config, unsigned flags,
833 const void **out, int *out_num)
834 {
835 const FFCodec *codec2;
836 70938 int dummy_num = 0;
837
2/2
✓ Branch 0 taken 70936 times.
✓ Branch 1 taken 2 times.
70938 if (!codec)
838 70936 codec = avctx->codec;
839
2/2
✓ Branch 0 taken 43901 times.
✓ Branch 1 taken 27037 times.
70938 if (!out_num)
840 43901 out_num = &dummy_num;
841
842 70938 codec2 = ffcodec(codec);
843
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 70751 times.
70938 if (codec2->get_supported_config) {
844 187 return codec2->get_supported_config(avctx, codec, config, flags, out, out_num);
845 } else {
846 70751 return ff_default_get_supported_config(avctx, codec, config, flags, out, out_num);
847 }
848 }
849
850 int av_packet_side_data_from_frame(AVPacketSideData **psd, int *pnb_sd,
851 const AVFrameSideData *src, unsigned int flags)
852 {
853 AVPacketSideData *sd = NULL;
854
855 for (unsigned j = 0; ff_sd_global_map[j].packet < AV_PKT_DATA_NB; j++) {
856 if (ff_sd_global_map[j].frame != src->type)
857 continue;
858
859 sd = av_packet_side_data_new(psd, pnb_sd, ff_sd_global_map[j].packet,
860 src->size, 0);
861
862 if (!sd)
863 return AVERROR(ENOMEM);
864
865 memcpy(sd->data, src->data, src->size);
866 break;
867 }
868
869 if (!sd)
870 return AVERROR(EINVAL);
871
872 return 0;
873 }
874
875 int av_packet_side_data_to_frame(AVFrameSideData ***psd, int *pnb_sd,
876 const AVPacketSideData *src, unsigned int flags)
877 {
878 AVFrameSideData *sd = NULL;
879
880 for (unsigned j = 0; ff_sd_global_map[j].packet < AV_PKT_DATA_NB; j++) {
881 if (ff_sd_global_map[j].packet != src->type)
882 continue;
883
884 sd = av_frame_side_data_new(psd, pnb_sd, ff_sd_global_map[j].frame,
885 src->size, flags);
886
887 if (!sd)
888 return AVERROR(ENOMEM);
889
890 memcpy(sd->data, src->data, src->size);
891 break;
892 }
893
894 if (!sd)
895 return AVERROR(EINVAL);
896
897 return 0;
898 }
899