../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
384 / 494
77.7%
Branches:
319 / 450
70.9%
Functions:
17 / 18
94.4%

libavcodec/avcodec.c
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/imgutils.h"
35 #include "libavutil/mem.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/thread.h"
38 #include "avcodec.h"
39 #include "avcodec_internal.h"
40 #include "bsf.h"
41 #include "codec_desc.h"
42 #include "codec_internal.h"
43 #include "decode.h"
44 #include "frame_thread_encoder.h"
45 #include "hwconfig.h"
46 #include "internal.h"
47 #include "libavutil/refstruct.h"
48
49 /**
50 * Maximum size in bytes of extradata.
51 * This value was chosen such that every bit of the buffer is
52 * addressable by a 32-bit signed integer as used by get_bits.
53 */
54 #define FF_MAX_EXTRADATA_SIZE ((1 << 28) - AV_INPUT_BUFFER_PADDING_SIZE)
55
56 const SideDataMap ff_sd_global_map[] = {
57 { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
58 { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
59 { AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL },
60 { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
61 { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
62 { AV_PKT_DATA_MASTERING_DISPLAY_METADATA, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA },
63 { AV_PKT_DATA_CONTENT_LIGHT_LEVEL, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL },
64 { AV_PKT_DATA_ICC_PROFILE, AV_FRAME_DATA_ICC_PROFILE },
65 { AV_PKT_DATA_AMBIENT_VIEWING_ENVIRONMENT,AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT },
66 { AV_PKT_DATA_3D_REFERENCE_DISPLAYS, AV_FRAME_DATA_3D_REFERENCE_DISPLAYS },
67 { AV_PKT_DATA_EXIF, AV_FRAME_DATA_EXIF },
68 { AV_PKT_DATA_GAIN_MAP_PARAMS, AV_FRAME_DATA_GAIN_MAP_PARAMS },
69 { AV_PKT_DATA_NB },
70 };
71
72
73 28942 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 10 → 3 taken 1391962 times.
✓ Branch 10 → 11 taken 28942 times.
1420904 for (i = 0; i < count; i++) {
78 1391962 size_t offset = i * size;
79
2/2
✓ Branch 3 → 4 taken 1363020 times.
✓ Branch 3 → 5 taken 28942 times.
1391962 int r = func(c, FF_PTR_ADD((char *)arg, offset));
80
2/2
✓ Branch 7 → 8 taken 930 times.
✓ Branch 7 → 9 taken 1391032 times.
1391962 if (ret)
81 930 ret[i] = r;
82 }
83 28942 return 0;
84 }
85
86 8681 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
87 {
88 int i;
89
90
2/2
✓ Branch 7 → 3 taken 232799 times.
✓ Branch 7 → 8 taken 8681 times.
241480 for (i = 0; i < count; i++) {
91 232799 int r = func(c, arg, i, 0);
92
2/2
✓ Branch 4 → 5 taken 50 times.
✓ Branch 4 → 6 taken 232749 times.
232799 if (ret)
93 50 ret[i] = r;
94 }
95 8681 return 0;
96 }
97
98 static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
99
100 73551 static void lock_avcodec(const FFCodec *codec)
101 {
102
1/4
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 73551 times.
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
73551 if (codec->caps_internal & FF_CODEC_CAP_NOT_INIT_THREADSAFE && codec->init)
103 ✗ ff_mutex_lock(&codec_mutex);
104 73551 }
105
106 73551 static void unlock_avcodec(const FFCodec *codec)
107 {
108
1/4
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 73551 times.
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
73551 if (codec->caps_internal & FF_CODEC_CAP_NOT_INIT_THREADSAFE && codec->init)
109 ✗ ff_mutex_unlock(&codec_mutex);
110 73551 }
111
112 32230 static int64_t get_bit_rate(AVCodecContext *ctx)
113 {
114 int64_t bit_rate;
115 int bits_per_sample;
116
117
2/3
✓ Branch 2 → 3 taken 25330 times.
✓ Branch 2 → 4 taken 6900 times.
✗ Branch 2 → 11 not taken.
32230 switch (ctx->codec_type) {
118 25330 case AVMEDIA_TYPE_VIDEO:
119 case AVMEDIA_TYPE_DATA:
120 case AVMEDIA_TYPE_SUBTITLE:
121 case AVMEDIA_TYPE_ATTACHMENT:
122 25330 bit_rate = ctx->bit_rate;
123 25330 break;
124 6900 case AVMEDIA_TYPE_AUDIO:
125 6900 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
126
2/2
✓ Branch 5 → 6 taken 3078 times.
✓ Branch 5 → 9 taken 3822 times.
6900 if (bits_per_sample) {
127 3078 bit_rate = ctx->sample_rate * (int64_t)ctx->ch_layout.nb_channels;
128
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 3078 times.
3078 if (bit_rate > INT64_MAX / bits_per_sample) {
129 ✗ bit_rate = 0;
130 } else
131 3078 bit_rate *= bits_per_sample;
132 } else
133 3822 bit_rate = ctx->bit_rate;
134 6900 break;
135 ✗ default:
136 ✗ bit_rate = 0;
137 ✗ break;
138 }
139 32230 return bit_rate;
140 }
141
142 41091 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
143 {
144 41091 int ret = 0;
145 AVCodecInternal *avci;
146 const FFCodec *codec2;
147 const AVDictionaryEntry *e;
148
149
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 41091 times.
41091 if (avcodec_is_open(avctx))
150 ✗ return 0;
151
152
3/4
✓ Branch 5 → 6 taken 49 times.
✓ Branch 5 → 9 taken 41042 times.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 9 taken 49 times.
41091 if (!codec && !avctx->codec) {
153 ✗ av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
154 ✗ return AVERROR(EINVAL);
155 }
156
5/6
✓ Branch 9 → 10 taken 41042 times.
✓ Branch 9 → 14 taken 49 times.
✓ Branch 10 → 11 taken 31251 times.
✓ Branch 10 → 14 taken 9791 times.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 14 taken 31251 times.
41091 if (codec && avctx->codec && codec != avctx->codec) {
157 ✗ av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
158 ✗ "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
159 ✗ return AVERROR(EINVAL);
160 }
161
2/2
✓ Branch 14 → 15 taken 49 times.
✓ Branch 14 → 16 taken 41042 times.
41091 if (!codec)
162 49 codec = avctx->codec;
163 41091 codec2 = ffcodec(codec);
164
165
2/4
✓ Branch 17 → 18 taken 41091 times.
✗ Branch 17 → 19 not taken.
✓ Branch 18 → 19 taken 41091 times.
✗ Branch 18 → 21 not taken.
41091 if ((avctx->codec_type != AVMEDIA_TYPE_UNKNOWN && avctx->codec_type != codec->type) ||
166
2/4
✓ Branch 19 → 20 taken 41091 times.
✗ Branch 19 → 23 not taken.
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 23 taken 41091 times.
41091 (avctx->codec_id != AV_CODEC_ID_NONE && avctx->codec_id != codec->id)) {
167 ✗ av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
168 ✗ return AVERROR(EINVAL);
169 }
170
171 41091 avctx->codec_type = codec->type;
172 41091 avctx->codec_id = codec->id;
173 41091 avctx->codec = codec;
174
175
2/4
✓ Branch 23 → 24 taken 41091 times.
✗ Branch 23 → 25 not taken.
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 41091 times.
41091 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
176 ✗ return AVERROR(EINVAL);
177
178 // set the whitelist from provided options dict,
179 // so we can check it immediately
180
2/2
✓ Branch 26 → 27 taken 10474 times.
✓ Branch 26 → 28 taken 30617 times.
41091 e = options ? av_dict_get(*options, "codec_whitelist", NULL, 0) : NULL;
181
1/2
✗ Branch 29 → 30 not taken.
✓ Branch 29 → 33 taken 41091 times.
41091 if (e) {
182 ✗ ret = av_opt_set(avctx, e->key, e->value, 0);
183
0/2
✗ Branch 31 → 32 not taken.
✗ Branch 31 → 33 not taken.
✗ if (ret < 0)
184 ✗ return ret;
185 }
186
187
1/4
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 38 taken 41091 times.
✗ Branch 35 → 36 not taken.
✗ Branch 35 → 38 not taken.
41091 if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
188 ✗ av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
189 ✗ return AVERROR(EINVAL);
190 }
191
192
2/2
✓ Branch 39 → 40 taken 17975 times.
✓ Branch 39 → 41 taken 23116 times.
41091 avci = ff_codec_is_decoder(codec) ?
193 17975 ff_decode_internal_alloc() :
194 23116 ff_encode_internal_alloc();
195
1/2
✗ Branch 42 → 43 not taken.
✓ Branch 42 → 44 taken 41091 times.
41091 if (!avci) {
196 ✗ ret = AVERROR(ENOMEM);
197 ✗ goto end;
198 }
199 41091 avctx->internal = avci;
200
201 41091 avci->buffer_frame = av_frame_alloc();
202 41091 avci->buffer_pkt = av_packet_alloc();
203
2/4
✓ Branch 46 → 47 taken 41091 times.
✗ Branch 46 → 48 not taken.
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 41091 times.
41091 if (!avci->buffer_frame || !avci->buffer_pkt) {
204 ✗ ret = AVERROR(ENOMEM);
205 ✗ goto free_and_end;
206 }
207
208
2/2
✓ Branch 49 → 50 taken 18867 times.
✓ Branch 49 → 56 taken 22224 times.
41091 if (codec2->priv_data_size > 0) {
209
2/2
✓ Branch 50 → 51 taken 9607 times.
✓ Branch 50 → 57 taken 9260 times.
18867 if (!avctx->priv_data) {
210 9607 avctx->priv_data = av_mallocz(codec2->priv_data_size);
211
1/2
✗ Branch 52 → 53 not taken.
✓ Branch 52 → 54 taken 9607 times.
9607 if (!avctx->priv_data) {
212 ✗ ret = AVERROR(ENOMEM);
213 ✗ goto free_and_end;
214 }
215
2/2
✓ Branch 54 → 55 taken 3044 times.
✓ Branch 54 → 57 taken 6563 times.
9607 if (codec->priv_class) {
216 3044 *(const AVClass **)avctx->priv_data = codec->priv_class;
217 3044 av_opt_set_defaults(avctx->priv_data);
218 }
219 }
220 } else {
221 22224 avctx->priv_data = NULL;
222 }
223
224 41091 ret = av_opt_set_dict2(avctx, options, AV_OPT_SEARCH_CHILDREN);
225
1/2
✗ Branch 58 → 59 not taken.
✓ Branch 58 → 60 taken 41091 times.
41091 if (ret < 0)
226 ✗ goto free_and_end;
227
228 // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
229
6/8
✓ Branch 60 → 61 taken 41 times.
✓ Branch 60 → 67 taken 41050 times.
✓ Branch 61 → 62 taken 41 times.
✗ Branch 61 → 67 not taken.
✓ Branch 62 → 63 taken 6 times.
✓ Branch 62 → 67 taken 35 times.
✓ Branch 63 → 64 taken 6 times.
✗ Branch 63 → 67 not taken.
41091 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
230
3/6
✓ Branch 64 → 65 taken 6 times.
✗ Branch 64 → 75 not taken.
✓ Branch 65 → 66 taken 6 times.
✗ Branch 65 → 75 not taken.
✓ Branch 66 → 67 taken 6 times.
✗ Branch 66 → 75 not taken.
6 (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
231
3/4
✓ Branch 67 → 68 taken 41 times.
✓ Branch 67 → 70 taken 41050 times.
✓ Branch 68 → 69 taken 41 times.
✗ Branch 68 → 70 not taken.
41091 if (avctx->coded_width && avctx->coded_height)
232 41 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
233
3/4
✓ Branch 70 → 71 taken 29960 times.
✓ Branch 70 → 73 taken 11090 times.
✓ Branch 71 → 72 taken 29960 times.
✗ Branch 71 → 73 not taken.
41050 else if (avctx->width && avctx->height)
234 29960 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
235
1/2
✗ Branch 73 → 74 not taken.
✓ Branch 73 → 75 taken 41091 times.
41091 if (ret < 0)
236 ✗ goto free_and_end;
237 }
238
239
5/8
✓ Branch 75 → 76 taken 11090 times.
✓ Branch 75 → 79 taken 30001 times.
✓ Branch 76 → 77 taken 11090 times.
✗ Branch 76 → 79 not taken.
✓ Branch 77 → 78 taken 11090 times.
✗ Branch 77 → 79 not taken.
✗ Branch 78 → 79 not taken.
✓ Branch 78 → 85 taken 11090 times.
41091 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
240
1/2
✓ Branch 80 → 81 taken 30001 times.
✗ Branch 80 → 83 not taken.
30001 && ( av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
241
1/2
✗ Branch 82 → 83 not taken.
✓ Branch 82 → 85 taken 30001 times.
30001 || av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
242 ✗ av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
243 ✗ ff_set_dimensions(avctx, 0, 0);
244 }
245
246
3/4
✓ Branch 85 → 86 taken 30001 times.
✓ Branch 85 → 91 taken 11090 times.
✓ Branch 86 → 87 taken 30001 times.
✗ Branch 86 → 91 not taken.
41091 if (avctx->width > 0 && avctx->height > 0) {
247
2/2
✓ Branch 88 → 89 taken 2 times.
✓ Branch 88 → 91 taken 29999 times.
30001 if (av_image_check_sar(avctx->width, avctx->height,
248 avctx->sample_aspect_ratio) < 0) {
249 2 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
250 avctx->sample_aspect_ratio.num,
251 avctx->sample_aspect_ratio.den);
252 2 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
253 }
254 }
255
256 /* AV_CODEC_CAP_CHANNEL_CONF is a decoder-only flag; so the code below
257 * in particular checks that sample_rate is set for all audio encoders. */
258
1/2
✓ Branch 91 → 92 taken 41091 times.
✗ Branch 91 → 95 not taken.
41091 if (avctx->sample_rate < 0 ||
259
4/4
✓ Branch 92 → 93 taken 35126 times.
✓ Branch 92 → 97 taken 5965 times.
✓ Branch 93 → 94 taken 421 times.
✓ Branch 93 → 97 taken 34705 times.
41091 avctx->sample_rate == 0 && avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
260
1/2
✗ Branch 94 → 95 not taken.
✓ Branch 94 → 97 taken 421 times.
421 !(codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)) {
261 ✗ av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate);
262 ✗ ret = AVERROR(EINVAL);
263 ✗ goto free_and_end;
264 }
265
1/2
✗ Branch 97 → 98 not taken.
✓ Branch 97 → 100 taken 41091 times.
41091 if (avctx->block_align < 0) {
266 ✗ av_log(avctx, AV_LOG_ERROR, "Invalid block align: %d\n", avctx->block_align);
267 ✗ ret = AVERROR(EINVAL);
268 ✗ goto free_and_end;
269 }
270
271 /* AV_CODEC_CAP_CHANNEL_CONF is a decoder-only flag; so the code below
272 * in particular checks that nb_channels is set for all audio encoders. */
273
4/4
✓ Branch 100 → 101 taken 6386 times.
✓ Branch 100 → 109 taken 34705 times.
✓ Branch 101 → 102 taken 424 times.
✓ Branch 101 → 109 taken 5962 times.
41091 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && !avctx->ch_layout.nb_channels
274
2/2
✓ Branch 102 → 103 taken 1 time.
✓ Branch 102 → 109 taken 423 times.
424 && !(codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)) {
275
1/2
✓ Branch 104 → 105 taken 1 time.
✗ Branch 104 → 106 not taken.
1 av_log(avctx, AV_LOG_ERROR, "%s requires channel layout to be set\n",
276 1 ff_codec_is_decoder(codec) ? "Decoder" : "Encoder");
277 1 ret = AVERROR(EINVAL);
278 1 goto free_and_end;
279 }
280
3/4
✓ Branch 109 → 110 taken 5962 times.
✓ Branch 109 → 114 taken 35128 times.
✗ Branch 111 → 112 not taken.
✓ Branch 111 → 114 taken 5962 times.
41090 if (avctx->ch_layout.nb_channels && !av_channel_layout_check(&avctx->ch_layout)) {
281 ✗ av_log(avctx, AV_LOG_ERROR, "Invalid channel layout\n");
282 ✗ ret = AVERROR(EINVAL);
283 ✗ goto free_and_end;
284 }
285
1/2
✗ Branch 114 → 115 not taken.
✓ Branch 114 → 117 taken 41090 times.
41090 if (avctx->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) {
286 ✗ av_log(avctx, AV_LOG_ERROR, "Too many channels: %d\n", avctx->ch_layout.nb_channels);
287 ✗ ret = AVERROR(EINVAL);
288 ✗ goto free_and_end;
289 }
290
291 41090 avctx->frame_num = 0;
292 41090 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
293
294
2/2
✓ Branch 118 → 119 taken 17 times.
✓ Branch 118 → 132 taken 41073 times.
41090 if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
295
1/2
✗ Branch 119 → 120 not taken.
✓ Branch 119 → 132 taken 17 times.
17 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
296
0/2
✗ Branch 121 → 122 not taken.
✗ Branch 121 → 123 not taken.
✗ const char *codec_string = ff_codec_is_encoder(codec) ? "encoder" : "decoder";
297 const AVCodec *codec2;
298 ✗ av_log(avctx, AV_LOG_ERROR,
299 "The %s '%s' is experimental but experimental codecs are not enabled, "
300 "add '-strict %d' if you want to use it.\n",
301 ✗ codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
302
0/2
✗ Branch 126 → 127 not taken.
✗ Branch 126 → 128 not taken.
✗ codec2 = ff_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
303
0/2
✗ Branch 129 → 130 not taken.
✗ Branch 129 → 131 not taken.
✗ if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
304 ✗ av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
305 ✗ codec_string, codec2->name);
306 ✗ ret = AVERROR_EXPERIMENTAL;
307 ✗ goto free_and_end;
308 }
309
310
2/2
✓ Branch 132 → 133 taken 6385 times.
✓ Branch 132 → 136 taken 34705 times.
41090 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
311
3/4
✓ Branch 133 → 134 taken 1698 times.
✓ Branch 133 → 135 taken 4687 times.
✗ Branch 134 → 135 not taken.
✓ Branch 134 → 136 taken 1698 times.
6385 (!avctx->time_base.num || !avctx->time_base.den)) {
312 4687 avctx->time_base.num = 1;
313 4687 avctx->time_base.den = avctx->sample_rate;
314 }
315
316
2/2
✓ Branch 137 → 138 taken 23116 times.
✓ Branch 137 → 139 taken 17974 times.
41090 if (ff_codec_is_encoder(avctx->codec))
317 23116 ret = ff_encode_preinit(avctx);
318 else
319 17974 ret = ff_decode_preinit(avctx);
320
1/2
✗ Branch 140 → 141 not taken.
✓ Branch 140 → 142 taken 41090 times.
41090 if (ret < 0)
321 ✗ goto free_and_end;
322
323
2/2
✓ Branch 142 → 143 taken 39309 times.
✓ Branch 142 → 148 taken 1781 times.
41090 if (HAVE_THREADS && !avci->frame_thread_encoder) {
324 /* Frame-threaded decoders call FFCodec.init for their child contexts. */
325 39309 lock_avcodec(codec2);
326 39309 ret = ff_thread_init(avctx);
327 39309 unlock_avcodec(codec2);
328
1/2
✗ Branch 146 → 147 not taken.
✓ Branch 146 → 148 taken 39309 times.
39309 if (ret < 0) {
329 ✗ goto free_and_end;
330 }
331 }
332 if (!HAVE_THREADS && !(codec2->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
333 avctx->thread_count = 1;
334
335
2/2
✓ Branch 148 → 149 taken 1797 times.
✓ Branch 148 → 150 taken 39293 times.
41090 if (!(avctx->active_thread_type & FF_THREAD_FRAME) ||
336
2/2
✓ Branch 149 → 150 taken 1781 times.
✓ Branch 149 → 157 taken 16 times.
1797 avci->frame_thread_encoder) {
337
2/2
✓ Branch 150 → 151 taken 34242 times.
✓ Branch 150 → 156 taken 6832 times.
41074 if (codec2->init) {
338 34242 lock_avcodec(codec2);
339 34242 ret = codec2->init(avctx);
340 34242 unlock_avcodec(codec2);
341
2/2
✓ Branch 154 → 155 taken 9 times.
✓ Branch 154 → 156 taken 34233 times.
34242 if (ret < 0) {
342 9 avci->needs_close = codec2->caps_internal & FF_CODEC_CAP_INIT_CLEANUP;
343 9 goto free_and_end;
344 }
345 }
346 41065 avci->needs_close = 1;
347 }
348
349 41081 ret=0;
350
351
2/2
✓ Branch 158 → 159 taken 17965 times.
✓ Branch 158 → 174 taken 23116 times.
41081 if (ff_codec_is_decoder(avctx->codec)) {
352
2/2
✓ Branch 159 → 160 taken 12429 times.
✓ Branch 159 → 162 taken 5536 times.
17965 if (!avctx->bit_rate)
353 12429 avctx->bit_rate = get_bit_rate(avctx);
354
355
356
2/2
✓ Branch 162 → 163 taken 4668 times.
✓ Branch 162 → 167 taken 13297 times.
17965 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
357
3/4
✓ Branch 163 → 164 taken 3780 times.
✓ Branch 163 → 167 taken 888 times.
✗ Branch 164 → 165 not taken.
✓ Branch 164 → 167 taken 3780 times.
4668 !avctx->frame_size && (avctx->flags2 & AV_CODEC_FLAG2_FIXED_FRAME_SIZE)) {
358 ✗ av_log(avctx, AV_LOG_ERROR, "Fixed frame size requested but no frame_size value set\n");
359 ✗ ret = AVERROR(EINVAL);
360 ✗ goto free_and_end;
361 }
362
363 17965 avci->skip_samples = avctx->delay;
364
365 /* validate channel layout from the decoder */
366
3/4
✓ Branch 167 → 168 taken 4343 times.
✓ Branch 167 → 170 taken 13622 times.
✓ Branch 169 → 170 taken 4343 times.
✗ Branch 169 → 171 not taken.
17965 if ((avctx->ch_layout.nb_channels && !av_channel_layout_check(&avctx->ch_layout)) ||
367
1/2
✗ Branch 170 → 171 not taken.
✓ Branch 170 → 172 taken 17965 times.
17965 avctx->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) {
368 ✗ ret = AVERROR(EINVAL);
369 ✗ goto free_and_end;
370 }
371
1/2
✗ Branch 172 → 173 not taken.
✓ Branch 172 → 174 taken 17965 times.
17965 if (avctx->bits_per_coded_sample < 0) {
372 ✗ ret = AVERROR(EINVAL);
373 ✗ goto free_and_end;
374 }
375 }
376
2/2
✓ Branch 174 → 175 taken 6400 times.
✓ Branch 174 → 179 taken 34681 times.
41081 if (codec->priv_class)
377
1/2
✓ Branch 175 → 176 taken 6400 times.
✗ Branch 175 → 177 not taken.
6400 av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
378
379 41081 end:
380
381 41091 return ret;
382 10 free_and_end:
383 10 ff_codec_close(avctx);
384 10 goto end;
385 }
386
387 41 void avcodec_flush_buffers(AVCodecContext *avctx)
388 {
389 41 AVCodecInternal *avci = avctx->internal;
390
391
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 8 taken 41 times.
41 if (av_codec_is_encoder(avctx->codec)) {
392 ✗ int caps = avctx->codec->capabilities;
393
394
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 7 not taken.
✗ if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) {
395 // Only encoders that explicitly declare support for it can be
396 // flushed. Otherwise, this is a no-op.
397 ✗ av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder "
398 "that doesn't support it\n");
399 ✗ return;
400 }
401 ✗ ff_encode_flush_buffers(avctx);
402 } else
403 41 ff_decode_flush_buffers(avctx);
404
405 41 avci->draining = 0;
406 41 avci->draining_done = 0;
407
1/2
✓ Branch 9 → 10 taken 41 times.
✗ Branch 9 → 11 not taken.
41 if (avci->buffer_frame)
408 41 av_frame_unref(avci->buffer_frame);
409
1/2
✓ Branch 11 → 12 taken 41 times.
✗ Branch 11 → 13 not taken.
41 if (avci->buffer_pkt)
410 41 av_packet_unref(avci->buffer_pkt);
411
412
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 16 taken 41 times.
41 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME &&
413
0/2
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 16 not taken.
✗ !avci->is_frame_mt)
414 ✗ ff_thread_flush(avctx);
415
2/2
✓ Branch 17 → 18 taken 12 times.
✓ Branch 17 → 20 taken 29 times.
41 else if (ffcodec(avctx->codec)->flush)
416 12 ffcodec(avctx->codec)->flush(avctx);
417 }
418
419 1129 void avsubtitle_free(AVSubtitle *sub)
420 {
421 int i;
422
423
2/2
✓ Branch 11 → 3 taken 1095 times.
✓ Branch 11 → 12 taken 1129 times.
2224 for (i = 0; i < sub->num_rects; i++) {
424 1095 AVSubtitleRect *const rect = sub->rects[i];
425
426 1095 av_freep(&rect->data[0]);
427 1095 av_freep(&rect->data[1]);
428 1095 av_freep(&rect->data[2]);
429 1095 av_freep(&rect->data[3]);
430 1095 av_freep(&rect->text);
431 1095 av_freep(&rect->ass);
432
433 1095 av_freep(&sub->rects[i]);
434 }
435
436 1129 av_freep(&sub->rects);
437
438 1129 memset(sub, 0, sizeof(*sub));
439 1129 }
440
441 72078 av_cold void ff_codec_close(AVCodecContext *avctx)
442 {
443 int i;
444
445
2/2
✓ Branch 3 → 4 taken 41091 times.
✓ Branch 3 → 29 taken 30987 times.
72078 if (avcodec_is_open(avctx)) {
446 41091 AVCodecInternal *avci = avctx->internal;
447
448 #if CONFIG_FRAME_THREAD_ENCODER
449
4/4
✓ Branch 4 → 5 taken 16017 times.
✓ Branch 4 → 7 taken 25074 times.
✓ Branch 5 → 6 taken 1781 times.
✓ Branch 5 → 7 taken 14236 times.
41091 if (avci->frame_thread_encoder && avctx->thread_count > 1) {
450 1781 ff_frame_thread_encoder_free(avctx);
451 }
452 #endif
453
2/2
✓ Branch 7 → 8 taken 65 times.
✓ Branch 7 → 9 taken 41026 times.
41091 if (HAVE_THREADS && avci->thread_ctx)
454 65 ff_thread_free(avctx);
455
4/4
✓ Branch 9 → 10 taken 41065 times.
✓ Branch 9 → 14 taken 26 times.
✓ Branch 11 → 12 taken 8864 times.
✓ Branch 11 → 14 taken 32201 times.
41091 if (avci->needs_close && ffcodec(avctx->codec)->close)
456 8864 ffcodec(avctx->codec)->close(avctx);
457 41091 avci->byte_buffer_size = 0;
458 41091 av_freep(&avci->byte_buffer);
459 41091 av_frame_free(&avci->buffer_frame);
460 41091 av_packet_free(&avci->buffer_pkt);
461 41091 av_packet_free(&avci->last_pkt_props);
462
463 41091 av_packet_free(&avci->in_pkt);
464 41091 av_frame_free(&avci->in_frame);
465 41091 av_frame_free(&avci->recon_frame);
466
467 41091 av_refstruct_unref(&avci->pool);
468 41091 av_refstruct_pool_uninit(&avci->progress_frame_pool);
469
2/2
✓ Branch 24 → 25 taken 17975 times.
✓ Branch 24 → 26 taken 23116 times.
41091 if (av_codec_is_decoder(avctx->codec))
470 17975 ff_decode_internal_uninit(avctx);
471
472 41091 ff_hwaccel_uninit(avctx);
473
474 41091 av_bsf_free(&avci->bsf);
475
476 #if CONFIG_LCMS2
477 ff_icc_context_uninit(&avci->icc);
478 #endif
479
480 41091 av_freep(&avctx->internal);
481 }
482
483
2/2
✓ Branch 32 → 30 taken 5095 times.
✓ Branch 32 → 33 taken 72078 times.
77173 for (i = 0; i < avctx->nb_coded_side_data; i++)
484 5095 av_freep(&avctx->coded_side_data[i].data);
485 72078 av_freep(&avctx->coded_side_data);
486 72078 avctx->nb_coded_side_data = 0;
487 72078 av_frame_side_data_free(&avctx->decoded_side_data,
488 &avctx->nb_decoded_side_data);
489
490 72078 av_buffer_unref(&avctx->hw_frames_ctx);
491 72078 av_buffer_unref(&avctx->hw_device_ctx);
492
493
5/6
✓ Branch 37 → 38 taken 28464 times.
✓ Branch 37 → 41 taken 43614 times.
✓ Branch 38 → 39 taken 28464 times.
✗ Branch 38 → 41 not taken.
✓ Branch 39 → 40 taken 9444 times.
✓ Branch 39 → 41 taken 19020 times.
72078 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
494 9444 av_opt_free(avctx->priv_data);
495 72078 av_opt_free(avctx);
496 72078 av_freep(&avctx->priv_data);
497
2/2
✓ Branch 44 → 45 taken 23120 times.
✓ Branch 44 → 47 taken 48958 times.
72078 if (av_codec_is_encoder(avctx->codec)) {
498 23120 av_freep(&avctx->extradata);
499 23120 avctx->extradata_size = 0;
500
2/2
✓ Branch 48 → 49 taken 37358 times.
✓ Branch 48 → 50 taken 11600 times.
48958 } else if (av_codec_is_decoder(avctx->codec))
501 37358 av_freep(&avctx->subtitle_header);
502
503 72078 avctx->codec = NULL;
504 72078 avctx->active_thread_type = 0;
505 72078 }
506
507 24546 static const char *unknown_if_null(const char *str)
508 {
509
1/2
✓ Branch 2 → 3 taken 24546 times.
✗ Branch 2 → 4 not taken.
24546 return str ? str : "unknown";
510 }
511
512 19830 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
513 {
514 const char *codec_type;
515 const char *codec_name;
516 19830 const char *profile = NULL;
517 AVBPrint bprint;
518 int64_t bitrate;
519 19830 int new_line = 0;
520 AVRational display_aspect_ratio;
521
2/2
✓ Branch 2 → 3 taken 19786 times.
✓ Branch 2 → 4 taken 44 times.
19830 const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
522 const char *str;
523
524
2/4
✓ Branch 5 → 6 taken 19830 times.
✗ Branch 5 → 7 not taken.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 19830 times.
19830 if (!buf || buf_size <= 0)
525 29 return;
526 19830 av_bprint_init_for_buffer(&bprint, buf, buf_size);
527 19830 codec_type = av_get_media_type_string(enc->codec_type);
528 19830 codec_name = avcodec_get_name(enc->codec_id);
529 19830 profile = avcodec_profile_name(enc->codec_id, enc->profile);
530
531
1/2
✓ Branch 12 → 13 taken 19830 times.
✗ Branch 12 → 14 not taken.
19830 av_bprintf(&bprint, "%s: %s", codec_type ? codec_type : "unknown",
532 codec_name);
533 19830 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
534
535
4/4
✓ Branch 16 → 17 taken 9629 times.
✓ Branch 16 → 19 taken 10201 times.
✓ Branch 17 → 18 taken 532 times.
✓ Branch 17 → 19 taken 9097 times.
19830 if (enc->codec && strcmp(enc->codec->name, codec_name))
536 532 av_bprintf(&bprint, " (%s)", enc->codec->name);
537
538
2/2
✓ Branch 19 → 20 taken 2739 times.
✓ Branch 19 → 21 taken 17091 times.
19830 if (profile)
539 2739 av_bprintf(&bprint, " (%s)", profile);
540
2/2
✓ Branch 21 → 22 taken 14459 times.
✓ Branch 21 → 29 taken 5371 times.
19830 if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
541
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 29 taken 14459 times.
14459 && av_log_get_level() >= AV_LOG_VERBOSE
542
0/2
✗ Branch 24 → 25 not taken.
✗ Branch 24 → 29 not taken.
✗ && enc->refs)
543 ✗ av_bprintf(&bprint, ", %d reference frame%s",
544
0/2
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 27 not taken.
✗ enc->refs, enc->refs > 1 ? "s" : "");
545
546
2/2
✓ Branch 29 → 30 taken 11041 times.
✓ Branch 29 → 33 taken 8789 times.
19830 if (enc->codec_tag)
547 11041 av_bprintf(&bprint, " (%s / 0x%04X)",
548 11041 av_fourcc2str(enc->codec_tag), enc->codec_tag);
549
550
5/5
✓ Branch 33 → 34 taken 14459 times.
✓ Branch 33 → 107 taken 5012 times.
✓ Branch 33 → 126 taken 110 times.
✓ Branch 33 → 131 taken 220 times.
✓ Branch 33 → 134 taken 29 times.
19830 switch (enc->codec_type) {
551 14459 case AVMEDIA_TYPE_VIDEO:
552 {
553 unsigned len;
554
555 14459 av_bprintf(&bprint, "%s%s", separator,
556
2/2
✓ Branch 34 → 35 taken 14397 times.
✓ Branch 34 → 37 taken 62 times.
14459 enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
557 14397 unknown_if_null(av_get_pix_fmt_name(enc->pix_fmt)));
558
559 14459 av_bprint_chars(&bprint, '(', 1);
560 14459 len = bprint.len;
561
562 /* The following check ensures that '(' has been written
563 * and therefore allows us to erase it if it turns out
564 * to be unnecessary. */
565
1/2
✗ Branch 41 → 42 not taken.
✓ Branch 41 → 43 taken 14459 times.
14459 if (!av_bprint_is_complete(&bprint))
566 ✗ return;
567
568
3/4
✓ Branch 43 → 44 taken 1357 times.
✓ Branch 43 → 48 taken 13102 times.
✓ Branch 44 → 45 taken 1357 times.
✗ Branch 44 → 48 not taken.
14459 if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
569
2/2
✓ Branch 46 → 47 taken 27 times.
✓ Branch 46 → 48 taken 1330 times.
1357 enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
570 27 av_bprintf(&bprint, "%d bpc, ", enc->bits_per_raw_sample);
571
3/4
✓ Branch 48 → 49 taken 11717 times.
✓ Branch 48 → 52 taken 2742 times.
✓ Branch 50 → 51 taken 11717 times.
✗ Branch 50 → 52 not taken.
26176 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED &&
572 11717 (str = av_color_range_name(enc->color_range)))
573 11717 av_bprintf(&bprint, "%s, ", str);
574
575
2/2
✓ Branch 52 → 53 taken 11238 times.
✓ Branch 52 → 55 taken 3221 times.
14459 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
576
2/2
✓ Branch 53 → 54 taken 11234 times.
✓ Branch 53 → 55 taken 4 times.
11238 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
577
2/2
✓ Branch 54 → 55 taken 158 times.
✓ Branch 54 → 65 taken 11076 times.
11234 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
578 3383 const char *col = unknown_if_null(av_color_space_name(enc->colorspace));
579 3383 const char *pri = unknown_if_null(av_color_primaries_name(enc->color_primaries));
580 3383 const char *trc = unknown_if_null(av_color_transfer_name(enc->color_trc));
581
4/4
✓ Branch 61 → 62 taken 390 times.
✓ Branch 61 → 63 taken 2993 times.
✓ Branch 62 → 63 taken 211 times.
✓ Branch 62 → 64 taken 179 times.
3383 if (strcmp(col, pri) || strcmp(col, trc)) {
582 3204 new_line = 1;
583 3204 av_bprintf(&bprint, "%s/%s/%s, ", col, pri, trc);
584 } else
585 179 av_bprintf(&bprint, "%s, ", col);
586 }
587
588
2/2
✓ Branch 65 → 66 taken 7989 times.
✓ Branch 65 → 75 taken 6470 times.
14459 if (enc->field_order != AV_FIELD_UNKNOWN) {
589 7989 const char *field_order = "progressive";
590
2/2
✓ Branch 66 → 67 taken 174 times.
✓ Branch 66 → 68 taken 7815 times.
7989 if (enc->field_order == AV_FIELD_TT)
591 174 field_order = "top first";
592
2/2
✓ Branch 68 → 69 taken 52 times.
✓ Branch 68 → 70 taken 7763 times.
7815 else if (enc->field_order == AV_FIELD_BB)
593 52 field_order = "bottom first";
594
2/2
✓ Branch 70 → 71 taken 370 times.
✓ Branch 70 → 72 taken 7393 times.
7763 else if (enc->field_order == AV_FIELD_TB)
595 370 field_order = "top coded first (swapped)";
596
2/2
✓ Branch 72 → 73 taken 149 times.
✓ Branch 72 → 74 taken 7244 times.
7393 else if (enc->field_order == AV_FIELD_BT)
597 149 field_order = "bottom coded first (swapped)";
598
599 7989 av_bprintf(&bprint, "%s, ", field_order);
600 }
601
602
1/2
✗ Branch 76 → 77 not taken.
✓ Branch 76 → 81 taken 14459 times.
14459 if (av_log_get_level() >= AV_LOG_VERBOSE &&
603
0/4
✗ Branch 77 → 78 not taken.
✗ Branch 77 → 81 not taken.
✗ Branch 79 → 80 not taken.
✗ Branch 79 → 81 not taken.
✗ enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED &&
604 ✗ (str = av_chroma_location_name(enc->chroma_sample_location)))
605 ✗ av_bprintf(&bprint, "%s, ", str);
606
607
2/2
✓ Branch 81 → 82 taken 1276 times.
✓ Branch 81 → 83 taken 13183 times.
14459 if (len == bprint.len) {
608 1276 bprint.str[len - 1] = '\0';
609 1276 bprint.len--;
610 } else {
611
1/2
✓ Branch 83 → 84 taken 13183 times.
✗ Branch 83 → 85 not taken.
13183 if (bprint.len - 2 < bprint.size) {
612 /* Erase the last ", " */
613 13183 bprint.len -= 2;
614 13183 bprint.str[bprint.len] = '\0';
615 }
616 13183 av_bprint_chars(&bprint, ')', 1);
617 }
618 }
619
620
2/2
✓ Branch 86 → 87 taken 14430 times.
✓ Branch 86 → 104 taken 29 times.
14459 if (enc->width) {
621
2/2
✓ Branch 87 → 88 taken 3201 times.
✓ Branch 87 → 89 taken 11229 times.
14430 av_bprintf(&bprint, "%s%dx%d", new_line ? separator : ", ",
622 enc->width, enc->height);
623
624
1/2
✗ Branch 92 → 93 not taken.
✓ Branch 92 → 98 taken 14430 times.
14430 if (av_log_get_level() >= AV_LOG_VERBOSE &&
625
0/4
✗ Branch 93 → 94 not taken.
✗ Branch 93 → 98 not taken.
✗ Branch 94 → 95 not taken.
✗ Branch 94 → 98 not taken.
✗ enc->coded_width && enc->coded_height &&
626
0/2
✗ Branch 95 → 96 not taken.
✗ Branch 95 → 97 not taken.
✗ (enc->width != enc->coded_width ||
627
0/2
✗ Branch 96 → 97 not taken.
✗ Branch 96 → 98 not taken.
✗ enc->height != enc->coded_height))
628 ✗ av_bprintf(&bprint, " (%dx%d)",
629 enc->coded_width, enc->coded_height);
630
631
2/2
✓ Branch 98 → 99 taken 3016 times.
✓ Branch 98 → 101 taken 11414 times.
14430 if (enc->sample_aspect_ratio.num) {
632 3016 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
633 3016 enc->width * (int64_t)enc->sample_aspect_ratio.num,
634 3016 enc->height * (int64_t)enc->sample_aspect_ratio.den,
635 1024 * 1024);
636 3016 av_bprintf(&bprint, " [SAR %d:%d DAR %d:%d]",
637 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
638 display_aspect_ratio.num, display_aspect_ratio.den);
639 }
640
1/2
✗ Branch 102 → 103 not taken.
✓ Branch 102 → 104 taken 14430 times.
14430 if (av_log_get_level() >= AV_LOG_DEBUG) {
641 ✗ int g = av_gcd(enc->time_base.num, enc->time_base.den);
642 ✗ av_bprintf(&bprint, ", %d/%d",
643 ✗ enc->time_base.num / g, enc->time_base.den / g);
644 }
645 }
646
2/2
✓ Branch 104 → 105 taken 7629 times.
✓ Branch 104 → 106 taken 6830 times.
14459 if (encode) {
647 7629 av_bprintf(&bprint, ", q=%d-%d", enc->qmin, enc->qmax);
648 }
649 14459 break;
650 5012 case AVMEDIA_TYPE_AUDIO:
651 5012 av_bprintf(&bprint, "%s", separator);
652
653
2/2
✓ Branch 108 → 109 taken 4990 times.
✓ Branch 108 → 110 taken 22 times.
5012 if (enc->sample_rate) {
654 4990 av_bprintf(&bprint, "%d Hz, ", enc->sample_rate);
655 }
656 5012 av_channel_layout_describe_bprint(&enc->ch_layout, &bprint);
657
3/4
✓ Branch 111 → 112 taken 5001 times.
✓ Branch 111 → 115 taken 11 times.
✓ Branch 113 → 114 taken 5001 times.
✗ Branch 113 → 115 not taken.
10013 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE &&
658 5001 (str = av_get_sample_fmt_name(enc->sample_fmt))) {
659 5001 av_bprintf(&bprint, ", %s", str);
660 }
661
2/2
✓ Branch 115 → 116 taken 2451 times.
✓ Branch 115 → 119 taken 2561 times.
5012 if ( enc->bits_per_raw_sample > 0
662
2/2
✓ Branch 117 → 118 taken 277 times.
✓ Branch 117 → 119 taken 2174 times.
2451 && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
663 277 av_bprintf(&bprint, " (%d bit)", enc->bits_per_raw_sample);
664
2/2
✓ Branch 120 → 121 taken 6 times.
✓ Branch 120 → 125 taken 5006 times.
5012 if (av_log_get_level() >= AV_LOG_VERBOSE) {
665
2/2
✓ Branch 121 → 122 taken 3 times.
✓ Branch 121 → 123 taken 3 times.
6 if (enc->initial_padding)
666 3 av_bprintf(&bprint, ", delay %d", enc->initial_padding);
667
1/2
✗ Branch 123 → 124 not taken.
✓ Branch 123 → 125 taken 6 times.
6 if (enc->trailing_padding)
668 ✗ av_bprintf(&bprint, ", padding %d", enc->trailing_padding);
669 }
670 5012 break;
671 110 case AVMEDIA_TYPE_DATA:
672
1/2
✗ Branch 127 → 128 not taken.
✓ Branch 127 → 130 taken 110 times.
110 if (av_log_get_level() >= AV_LOG_DEBUG) {
673 ✗ int g = av_gcd(enc->time_base.num, enc->time_base.den);
674
0/2
✗ Branch 128 → 129 not taken.
✗ Branch 128 → 130 not taken.
✗ if (g)
675 ✗ av_bprintf(&bprint, ", %d/%d",
676 ✗ enc->time_base.num / g, enc->time_base.den / g);
677 }
678 110 break;
679 220 case AVMEDIA_TYPE_SUBTITLE:
680
2/2
✓ Branch 131 → 132 taken 33 times.
✓ Branch 131 → 133 taken 187 times.
220 if (enc->width)
681 33 av_bprintf(&bprint, ", %dx%d", enc->width, enc->height);
682 220 break;
683 29 default:
684 29 return;
685 }
686
2/2
✓ Branch 135 → 136 taken 9978 times.
✓ Branch 135 → 140 taken 9823 times.
19801 if (encode) {
687
1/2
✗ Branch 136 → 137 not taken.
✓ Branch 136 → 138 taken 9978 times.
9978 if (enc->flags & AV_CODEC_FLAG_PASS1)
688 ✗ av_bprintf(&bprint, ", pass 1");
689
1/2
✗ Branch 138 → 139 not taken.
✓ Branch 138 → 140 taken 9978 times.
9978 if (enc->flags & AV_CODEC_FLAG_PASS2)
690 ✗ av_bprintf(&bprint, ", pass 2");
691 }
692 19801 bitrate = get_bit_rate(enc);
693
2/2
✓ Branch 141 → 142 taken 12804 times.
✓ Branch 141 → 143 taken 6997 times.
19801 if (bitrate != 0) {
694 12804 av_bprintf(&bprint, ", %"PRId64" kb/s", bitrate / 1000);
695
1/2
✗ Branch 143 → 144 not taken.
✓ Branch 143 → 145 taken 6997 times.
6997 } else if (enc->rc_max_rate > 0) {
696 ✗ av_bprintf(&bprint, ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
697 }
698 }
699
700 4536671 int avcodec_is_open(AVCodecContext *s)
701 {
702 4536671 return !!s->internal;
703 }
704
705 1649510 int attribute_align_arg avcodec_receive_frame_flags(AVCodecContext *avctx,
706 AVFrame *frame, unsigned flags)
707 {
708 1649510 av_frame_unref(frame);
709
710
2/4
✓ Branch 4 → 5 taken 1649510 times.
✗ Branch 4 → 6 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 1649510 times.
1649510 if (!avcodec_is_open(avctx) || !avctx->codec)
711 ✗ return AVERROR(EINVAL);
712
713
2/2
✓ Branch 8 → 9 taken 1649406 times.
✓ Branch 8 → 11 taken 104 times.
1649510 if (ff_codec_is_decoder(avctx->codec))
714 1649406 return ff_decode_receive_frame(avctx, frame, flags);
715 104 return ff_encode_receive_frame(avctx, frame);
716 }
717
718 30137 int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
719 {
720 30137 return avcodec_receive_frame_flags(avctx, frame, 0);
721 }
722
723 #define WRAP_CONFIG(allowed_type, field, var, field_type, sentinel_check) \
724 do { \
725 if (codec->type != (allowed_type)) \
726 return AVERROR(EINVAL); \
727 const field_type *ptr = codec2->field; \
728 *out_configs = ptr; \
729 if (ptr) { \
730 for (int i = 0;; i++) { \
731 const field_type var = ptr[i]; \
732 if (sentinel_check) { \
733 *out_num_configs = i; \
734 break; \
735 } \
736 } \
737 } else \
738 *out_num_configs = 0; \
739 return 0; \
740 } while (0)
741
742 static const enum AVColorRange color_range_tab[] = {
743 AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG, AVCOL_RANGE_UNSPECIFIED,
744 AVCOL_RANGE_MPEG, AVCOL_RANGE_UNSPECIFIED,
745 };
746
747 static const enum AVAlphaMode alpha_mode_tab[] = {
748 AVALPHA_MODE_PREMULTIPLIED, AVALPHA_MODE_STRAIGHT, AVALPHA_MODE_UNSPECIFIED,
749 AVALPHA_MODE_PREMULTIPLIED, AVALPHA_MODE_UNSPECIFIED
750 };
751
752 static_assert((int)AVCOL_RANGE_MPEG == (int)AVALPHA_MODE_PREMULTIPLIED, "unexpected enum values");
753 static_assert((int)AVCOL_RANGE_JPEG == (int)AVALPHA_MODE_STRAIGHT, "unexpected enum values");
754 static_assert(AVCOL_RANGE_UNSPECIFIED == 0 && AVALPHA_MODE_UNSPECIFIED == 0, "unexpected enum values");
755 static_assert(AVCOL_RANGE_NB == 3 && AVALPHA_MODE_NB == 3, "unexpected enum values");
756
757 static const uint8_t offset_tab[] = {
758 [AVCOL_RANGE_MPEG] = 3,
759 [AVCOL_RANGE_JPEG] = 1,
760 [AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG] = 0,
761 };
762
763 82323 int ff_default_get_supported_config(const AVCodecContext *avctx,
764 const AVCodec *codec,
765 enum AVCodecConfig config,
766 unsigned flags,
767 const void **out_configs,
768 int *out_num_configs)
769 {
770 82323 const FFCodec *codec2 = ffcodec(codec);
771
772
9/10
✓ Branch 3 → 4 taken 33942 times.
✓ Branch 3 → 14 taken 7109 times.
✓ Branch 3 → 24 taken 3416 times.
✓ Branch 3 → 34 taken 3418 times.
✓ Branch 3 → 44 taken 3416 times.
✓ Branch 3 → 54 taken 7076 times.
✓ Branch 3 → 60 taken 7110 times.
✓ Branch 3 → 61 taken 9794 times.
✓ Branch 3 → 67 taken 7042 times.
✗ Branch 3 → 77 not taken.
82323 switch (config) {
773 33942 case AV_CODEC_CONFIG_PIX_FORMAT:
774
5/6
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 33942 times.
✓ Branch 6 → 7 taken 1945 times.
✓ Branch 6 → 12 taken 31997 times.
✓ Branch 8 → 9 taken 1945 times.
✓ Branch 8 → 10 taken 20875 times.
56762 WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, pix_fmts, pix_fmt, enum AVPixelFormat, pix_fmt == AV_PIX_FMT_NONE);
775 7109 case AV_CODEC_CONFIG_FRAME_RATE:
776
5/6
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 7109 times.
✓ Branch 16 → 17 taken 78 times.
✓ Branch 16 → 22 taken 7031 times.
✓ Branch 18 → 19 taken 78 times.
✓ Branch 18 → 20 taken 4297 times.
11406 WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, supported_framerates, framerate, AVRational, framerate.num == 0);
777 3416 case AV_CODEC_CONFIG_SAMPLE_RATE:
778
5/6
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 3416 times.
✓ Branch 26 → 27 taken 457 times.
✓ Branch 26 → 32 taken 2959 times.
✓ Branch 28 → 29 taken 457 times.
✓ Branch 28 → 30 taken 2811 times.
6684 WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, supported_samplerates, samplerate, int, samplerate == 0);
779 3418 case AV_CODEC_CONFIG_SAMPLE_FORMAT:
780
4/6
✗ Branch 34 → 35 not taken.
✓ Branch 34 → 36 taken 3418 times.
✓ Branch 36 → 37 taken 3418 times.
✗ Branch 36 → 42 not taken.
✓ Branch 38 → 39 taken 3418 times.
✓ Branch 38 → 40 taken 3720 times.
10556 WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, sample_fmts, sample_fmt, enum AVSampleFormat, sample_fmt == AV_SAMPLE_FMT_NONE);
781 3416 case AV_CODEC_CONFIG_CHANNEL_LAYOUT:
782
5/6
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 46 taken 3416 times.
✓ Branch 46 → 47 taken 459 times.
✓ Branch 46 → 52 taken 2957 times.
✓ Branch 48 → 49 taken 459 times.
✓ Branch 48 → 50 taken 1640 times.
5056 WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, ch_layouts, ch_layout, AVChannelLayout, ch_layout.nb_channels == 0);
783
784 7076 case AV_CODEC_CONFIG_COLOR_RANGE:
785
1/2
✗ Branch 54 → 55 not taken.
✓ Branch 54 → 56 taken 7076 times.
7076 if (codec->type != AVMEDIA_TYPE_VIDEO)
786 ✗ return AVERROR(EINVAL);
787 7076 unsigned color_ranges = codec2->color_ranges;
788
2/2
✓ Branch 56 → 57 taken 518 times.
✓ Branch 56 → 58 taken 6558 times.
7076 if (color_ranges)
789 518 *out_configs = color_range_tab + offset_tab[color_ranges];
790 else
791 6558 *out_configs = NULL;
792 7076 *out_num_configs = av_popcount(color_ranges);
793 7076 return 0;
794
795 7110 case AV_CODEC_CONFIG_COLOR_SPACE:
796 7110 *out_configs = NULL;
797 7110 *out_num_configs = 0;
798 7110 return 0;
799
800 9794 case AV_CODEC_CONFIG_ALPHA_MODE:
801
1/2
✗ Branch 61 → 62 not taken.
✓ Branch 61 → 63 taken 9794 times.
9794 if (codec->type != AVMEDIA_TYPE_VIDEO)
802 ✗ return AVERROR(EINVAL);
803 9794 unsigned alpha_modes = codec2->alpha_modes;
804
2/2
✓ Branch 63 → 64 taken 34 times.
✓ Branch 63 → 65 taken 9760 times.
9794 if (alpha_modes)
805 34 *out_configs = alpha_mode_tab + offset_tab[alpha_modes];
806 else
807 9760 *out_configs = NULL;
808 9794 *out_num_configs = av_popcount(alpha_modes);
809 9794 return 0;
810
811 7042 case AV_CODEC_CONFIG_CHROMA_LOCATION:
812
5/6
✗ Branch 67 → 68 not taken.
✓ Branch 67 → 69 taken 7042 times.
✓ Branch 69 → 70 taken 113 times.
✓ Branch 69 → 75 taken 6929 times.
✓ Branch 71 → 72 taken 113 times.
✓ Branch 71 → 73 taken 113 times.
7268 WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, chroma_locations, loc,
813 enum AVChromaLocation, loc == AVCHROMA_LOC_UNSPECIFIED);
814
815 ✗ default:
816 ✗ return AVERROR(EINVAL);
817 }
818 }
819
820 82425 int avcodec_get_supported_config(const AVCodecContext *avctx, const AVCodec *codec,
821 enum AVCodecConfig config, unsigned flags,
822 const void **out, int *out_num)
823 {
824 const FFCodec *codec2;
825 82425 int dummy_num = 0;
826
2/2
✓ Branch 2 → 3 taken 82423 times.
✓ Branch 2 → 4 taken 2 times.
82425 if (!codec)
827 82423 codec = avctx->codec;
828
2/2
✓ Branch 4 → 5 taken 53238 times.
✓ Branch 4 → 6 taken 29187 times.
82425 if (!out_num)
829 53238 out_num = &dummy_num;
830
831 82425 codec2 = ffcodec(codec);
832
2/2
✓ Branch 7 → 8 taken 749 times.
✓ Branch 7 → 10 taken 81676 times.
82425 if (codec2->get_supported_config) {
833 749 return codec2->get_supported_config(avctx, codec, config, flags, out, out_num);
834 } else {
835 81676 return ff_default_get_supported_config(avctx, codec, config, flags, out, out_num);
836 }
837 }
838
839 ✗ int av_packet_side_data_from_frame(AVPacketSideData **psd, int *pnb_sd,
840 const AVFrameSideData *src, unsigned int flags)
841 {
842 ✗ AVPacketSideData *sd = NULL;
843
844
0/2
✗ Branch 10 → 3 not taken.
✗ Branch 10 → 11 not taken.
✗ for (unsigned j = 0; ff_sd_global_map[j].packet < AV_PKT_DATA_NB; j++) {
845
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ if (ff_sd_global_map[j].frame != src->type)
846 ✗ continue;
847
848 ✗ sd = av_packet_side_data_new(psd, pnb_sd, ff_sd_global_map[j].packet,
849 ✗ src->size, 0);
850
851
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if (!sd)
852 ✗ return AVERROR(ENOMEM);
853
854 ✗ memcpy(sd->data, src->data, src->size);
855 ✗ break;
856 }
857
858
0/2
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 13 not taken.
✗ if (!sd)
859 ✗ return AVERROR(EINVAL);
860
861 ✗ return 0;
862 }
863
864 2 int av_packet_side_data_to_frame(AVFrameSideData ***psd, int *pnb_sd,
865 const AVPacketSideData *src, unsigned int flags)
866 {
867 2 AVFrameSideData *sd = NULL;
868
869
1/2
✓ Branch 10 → 3 taken 19 times.
✗ Branch 10 → 11 not taken.
19 for (unsigned j = 0; ff_sd_global_map[j].packet < AV_PKT_DATA_NB; j++) {
870
2/2
✓ Branch 3 → 4 taken 17 times.
✓ Branch 3 → 5 taken 2 times.
19 if (ff_sd_global_map[j].packet != src->type)
871 17 continue;
872
873 2 sd = av_frame_side_data_new(psd, pnb_sd, ff_sd_global_map[j].frame,
874 2 src->size, flags);
875
876
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 2 times.
2 if (!sd)
877 ✗ return AVERROR(ENOMEM);
878
879 2 memcpy(sd->data, src->data, src->size);
880 2 break;
881 }
882
883
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 2 times.
2 if (!sd)
884 ✗ return AVERROR(EINVAL);
885
886 2 return 0;
887 }
888