FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/avcodec.c
Date: 2021-09-23 20:34:37
Exec Total Coverage
Lines: 341 433 78.8%
Branches: 272 382 71.2%

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 "config.h"
27 #include "libavutil/avassert.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/bprint.h"
30 #include "libavutil/channel_layout.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/thread.h"
35 #include "avcodec.h"
36 #include "bsf.h"
37 #include "decode.h"
38 #include "encode.h"
39 #include "frame_thread_encoder.h"
40 #include "internal.h"
41 #include "thread.h"
42
43 #include "libavutil/ffversion.h"
44 const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
45
46 6119 unsigned avcodec_version(void)
47 {
48 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
49 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
50 av_assert0(AV_CODEC_ID_SRT==94216);
51 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
52
53 6119 return LIBAVCODEC_VERSION_INT;
54 }
55
56 6119 const char *avcodec_configuration(void)
57 {
58 6119 return FFMPEG_CONFIGURATION;
59 }
60
61 const char *avcodec_license(void)
62 {
63 #define LICENSE_PREFIX "libavcodec license: "
64 return &LICENSE_PREFIX FFMPEG_LICENSE[sizeof(LICENSE_PREFIX) - 1];
65 }
66
67 47031 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
68 {
69 int i;
70
71
2/2
✓ Branch 0 taken 1341662 times.
✓ Branch 1 taken 47031 times.
1388693 for (i = 0; i < count; i++) {
72 1341662 int r = func(c, (char *)arg + i * size);
73
2/2
✓ Branch 0 taken 28403 times.
✓ Branch 1 taken 1313259 times.
1341662 if (ret)
74 28403 ret[i] = r;
75 }
76 47031 emms_c();
77 47031 return 0;
78 }
79
80 6860 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
81 {
82 int i;
83
84
2/2
✓ Branch 0 taken 206842 times.
✓ Branch 1 taken 6860 times.
213702 for (i = 0; i < count; i++) {
85 206842 int r = func(c, arg, i, 0);
86
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 206792 times.
206842 if (ret)
87 50 ret[i] = r;
88 }
89 6860 emms_c();
90 6860 return 0;
91 }
92
93 static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
94
95 24864 static void lock_avcodec(const AVCodec *codec)
96 {
97
4/4
✓ Branch 0 taken 5621 times.
✓ Branch 1 taken 19243 times.
✓ Branch 2 taken 631 times.
✓ Branch 3 taken 4990 times.
24864 if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
98 631 ff_mutex_lock(&codec_mutex);
99 24864 }
100
101 24864 static void unlock_avcodec(const AVCodec *codec)
102 {
103
4/4
✓ Branch 0 taken 5621 times.
✓ Branch 1 taken 19243 times.
✓ Branch 2 taken 631 times.
✓ Branch 3 taken 4990 times.
24864 if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
104 631 ff_mutex_unlock(&codec_mutex);
105 24864 }
106
107 21462 static int64_t get_bit_rate(AVCodecContext *ctx)
108 {
109 int64_t bit_rate;
110 int bits_per_sample;
111
112
2/3
✓ Branch 0 taken 17872 times.
✓ Branch 1 taken 3590 times.
✗ Branch 2 not taken.
21462 switch (ctx->codec_type) {
113 17872 case AVMEDIA_TYPE_VIDEO:
114 case AVMEDIA_TYPE_DATA:
115 case AVMEDIA_TYPE_SUBTITLE:
116 case AVMEDIA_TYPE_ATTACHMENT:
117 17872 bit_rate = ctx->bit_rate;
118 17872 break;
119 3590 case AVMEDIA_TYPE_AUDIO:
120 3590 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
121
2/2
✓ Branch 0 taken 2138 times.
✓ Branch 1 taken 1452 times.
3590 if (bits_per_sample) {
122 2138 bit_rate = ctx->sample_rate * (int64_t)ctx->channels;
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2138 times.
2138 if (bit_rate > INT64_MAX / bits_per_sample) {
124 bit_rate = 0;
125 } else
126 2138 bit_rate *= bits_per_sample;
127 } else
128 1452 bit_rate = ctx->bit_rate;
129 3590 break;
130 default:
131 bit_rate = 0;
132 break;
133 }
134 21462 return bit_rate;
135 }
136
137 18862 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
138 {
139 18862 int ret = 0;
140 AVCodecInternal *avci;
141
142
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18862 times.
18862 if (avcodec_is_open(avctx))
143 return 0;
144
145
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 18858 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
18862 if (!codec && !avctx->codec) {
146 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
147 return AVERROR(EINVAL);
148 }
149
5/6
✓ Branch 0 taken 18858 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 12034 times.
✓ Branch 3 taken 6824 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 12034 times.
18862 if (codec && avctx->codec && codec != avctx->codec) {
150 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
151 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
152 return AVERROR(EINVAL);
153 }
154
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 18858 times.
18862 if (!codec)
155 4 codec = avctx->codec;
156
157
3/4
✓ Branch 0 taken 18860 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 18860 times.
✗ Branch 3 not taken.
18862 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
158
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 18860 times.
18862 avctx->codec_id == AV_CODEC_ID_NONE) {
159 2 avctx->codec_type = codec->type;
160 2 avctx->codec_id = codec->id;
161 }
162
2/4
✓ Branch 0 taken 18862 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18862 times.
18862 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type &&
163 avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
164 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
165 return AVERROR(EINVAL);
166 }
167 18862 avctx->codec = codec;
168
169
2/4
✓ Branch 0 taken 18862 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18862 times.
18862 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
170 return AVERROR(EINVAL);
171
172 18862 lock_avcodec(codec);
173
174 18862 avci = av_mallocz(sizeof(*avci));
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18862 times.
18862 if (!avci) {
176 ret = AVERROR(ENOMEM);
177 goto end;
178 }
179 18862 avctx->internal = avci;
180
181 18862 avci->buffer_frame = av_frame_alloc();
182 18862 avci->buffer_pkt = av_packet_alloc();
183 18862 avci->es.in_frame = av_frame_alloc();
184 18862 avci->ds.in_pkt = av_packet_alloc();
185 18862 avci->last_pkt_props = av_packet_alloc();
186 18862 avci->pkt_props = av_fifo_alloc(sizeof(*avci->last_pkt_props));
187
2/4
✓ Branch 0 taken 18862 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18862 times.
✗ Branch 3 not taken.
18862 if (!avci->buffer_frame || !avci->buffer_pkt ||
188
2/4
✓ Branch 0 taken 18862 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18862 times.
✗ Branch 3 not taken.
18862 !avci->es.in_frame || !avci->ds.in_pkt ||
189
2/4
✓ Branch 0 taken 18862 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18862 times.
18862 !avci->last_pkt_props || !avci->pkt_props) {
190 ret = AVERROR(ENOMEM);
191 goto free_and_end;
192 }
193
194 18862 avci->skip_samples_multiplier = 1;
195
196
2/2
✓ Branch 0 taken 13412 times.
✓ Branch 1 taken 5450 times.
18862 if (codec->priv_data_size > 0) {
197
2/2
✓ Branch 0 taken 6712 times.
✓ Branch 1 taken 6700 times.
13412 if (!avctx->priv_data) {
198 6712 avctx->priv_data = av_mallocz(codec->priv_data_size);
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6712 times.
6712 if (!avctx->priv_data) {
200 ret = AVERROR(ENOMEM);
201 goto free_and_end;
202 }
203
2/2
✓ Branch 0 taken 1736 times.
✓ Branch 1 taken 4976 times.
6712 if (codec->priv_class) {
204 1736 *(const AVClass **)avctx->priv_data = codec->priv_class;
205 1736 av_opt_set_defaults(avctx->priv_data);
206 }
207 }
208
3/4
✓ Branch 0 taken 3946 times.
✓ Branch 1 taken 9466 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3946 times.
13412 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, options)) < 0)
209 goto free_and_end;
210 } else {
211 5450 avctx->priv_data = NULL;
212 }
213
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18862 times.
18862 if ((ret = av_opt_set_dict(avctx, options)) < 0)
214 goto free_and_end;
215
216
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 18862 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
18862 if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
217 av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
218 ret = AVERROR(EINVAL);
219 goto free_and_end;
220 }
221
222 // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
223
6/8
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 18714 times.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 139 times.
✓ Branch 5 taken 9 times.
✓ Branch 6 taken 139 times.
✗ Branch 7 not taken.
18862 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
224
3/6
✓ Branch 0 taken 139 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 139 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 139 times.
✗ Branch 5 not taken.
139 (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
225
3/4
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 18714 times.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
18862 if (avctx->coded_width && avctx->coded_height)
226 148 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
227
3/4
✓ Branch 0 taken 11253 times.
✓ Branch 1 taken 7461 times.
✓ Branch 2 taken 11253 times.
✗ Branch 3 not taken.
18714 else if (avctx->width && avctx->height)
228 11253 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18862 times.
18862 if (ret < 0)
230 goto free_and_end;
231 }
232
233
5/8
✓ Branch 0 taken 7461 times.
✓ Branch 1 taken 11401 times.
✓ Branch 2 taken 7461 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7461 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 7461 times.
18862 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
234
1/2
✓ Branch 1 taken 11401 times.
✗ Branch 2 not taken.
11401 && ( av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
235
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11401 times.
11401 || av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
236 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
237 ff_set_dimensions(avctx, 0, 0);
238 }
239
240
3/4
✓ Branch 0 taken 11401 times.
✓ Branch 1 taken 7461 times.
✓ Branch 2 taken 11401 times.
✗ Branch 3 not taken.
18862 if (avctx->width > 0 && avctx->height > 0) {
241
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11401 times.
11401 if (av_image_check_sar(avctx->width, avctx->height,
242 avctx->sample_aspect_ratio) < 0) {
243 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
244 avctx->sample_aspect_ratio.num,
245 avctx->sample_aspect_ratio.den);
246 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
247 }
248 }
249
250
2/4
✓ Branch 0 taken 18862 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18862 times.
18862 if (avctx->channels > FF_SANE_NB_CHANNELS || avctx->channels < 0) {
251 av_log(avctx, AV_LOG_ERROR, "Too many or invalid channels: %d\n", avctx->channels);
252 ret = AVERROR(EINVAL);
253 goto free_and_end;
254 }
255
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18862 times.
18862 if (avctx->sample_rate < 0) {
257 av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate);
258 ret = AVERROR(EINVAL);
259 goto free_and_end;
260 }
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18862 times.
18862 if (avctx->block_align < 0) {
262 av_log(avctx, AV_LOG_ERROR, "Invalid block align: %d\n", avctx->block_align);
263 ret = AVERROR(EINVAL);
264 goto free_and_end;
265 }
266
267 18862 avctx->frame_number = 0;
268 18862 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
269
270
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 18855 times.
18862 if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
272 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
273 const AVCodec *codec2;
274 av_log(avctx, AV_LOG_ERROR,
275 "The %s '%s' is experimental but experimental codecs are not enabled, "
276 "add '-strict %d' if you want to use it.\n",
277 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
278 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
279 if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
280 av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
281 codec_string, codec2->name);
282 ret = AVERROR_EXPERIMENTAL;
283 goto free_and_end;
284 }
285
286
2/2
✓ Branch 0 taken 3982 times.
✓ Branch 1 taken 14880 times.
18862 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
287
4/4
✓ Branch 0 taken 1142 times.
✓ Branch 1 taken 2840 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 1131 times.
3982 (!avctx->time_base.num || !avctx->time_base.den)) {
288 2851 avctx->time_base.num = 1;
289 2851 avctx->time_base.den = avctx->sample_rate;
290 }
291
292
2/2
✓ Branch 1 taken 6002 times.
✓ Branch 2 taken 12860 times.
18862 if (av_codec_is_encoder(avctx->codec))
293 6002 ret = ff_encode_preinit(avctx);
294 else
295 12860 ret = ff_decode_preinit(avctx);
296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18862 times.
18862 if (ret < 0)
297 goto free_and_end;
298
299 if (!HAVE_THREADS)
300 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
301
302
2/2
✓ Branch 1 taken 6002 times.
✓ Branch 2 taken 12860 times.
18862 if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
303 6002 unlock_avcodec(codec); //we will instantiate a few encoders thus kick the counter to prevent false detection of a problem
304 6002 ret = ff_frame_thread_encoder_init(avctx);
305 6002 lock_avcodec(codec);
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6002 times.
6002 if (ret < 0)
307 goto free_and_end;
308 }
309
310 18862 if (HAVE_THREADS
311
3/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18844 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18862 && !(avci->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
312 18844 ret = ff_thread_init(avctx);
313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18844 times.
18844 if (ret < 0) {
314 goto free_and_end;
315 }
316 }
317 if (!HAVE_THREADS && !(codec->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
318 avctx->thread_count = 1;
319
320
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 18832 times.
18862 if (!(avctx->active_thread_type & FF_THREAD_FRAME) ||
321
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 12 times.
30 avci->frame_thread_encoder) {
322
2/2
✓ Branch 0 taken 13788 times.
✓ Branch 1 taken 5062 times.
18850 if (avctx->codec->init) {
323 13788 ret = avctx->codec->init(avctx);
324
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 13778 times.
13788 if (ret < 0) {
325 10 avci->needs_close = avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP;
326 10 goto free_and_end;
327 }
328 }
329 18840 avci->needs_close = 1;
330 }
331
332 18852 ret=0;
333
334
2/2
✓ Branch 1 taken 12850 times.
✓ Branch 2 taken 6002 times.
18852 if (av_codec_is_decoder(avctx->codec)) {
335
2/2
✓ Branch 0 taken 8657 times.
✓ Branch 1 taken 4193 times.
12850 if (!avctx->bit_rate)
336 8657 avctx->bit_rate = get_bit_rate(avctx);
337 /* validate channel layout from the decoder */
338
2/2
✓ Branch 0 taken 1705 times.
✓ Branch 1 taken 11145 times.
12850 if (avctx->channel_layout) {
339 1705 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
340
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1689 times.
1705 if (!avctx->channels)
341 16 avctx->channels = channels;
342
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1682 times.
1689 else if (channels != avctx->channels) {
343 char buf[512];
344 7 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
345 7 av_log(avctx, AV_LOG_WARNING,
346 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
347 "ignoring specified channel layout\n",
348 buf, channels, avctx->channels);
349 7 avctx->channel_layout = 0;
350 }
351 }
352
3/4
✓ Branch 0 taken 2694 times.
✓ Branch 1 taken 10156 times.
✓ Branch 2 taken 2694 times.
✗ Branch 3 not taken.
12850 if (avctx->channels && avctx->channels < 0 ||
353
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12850 times.
12850 avctx->channels > FF_SANE_NB_CHANNELS) {
354 ret = AVERROR(EINVAL);
355 goto free_and_end;
356 }
357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12850 times.
12850 if (avctx->bits_per_coded_sample < 0) {
358 ret = AVERROR(EINVAL);
359 goto free_and_end;
360 }
361
362 #if FF_API_AVCTX_TIMEBASE
363
3/4
✓ Branch 0 taken 4599 times.
✓ Branch 1 taken 8251 times.
✓ Branch 2 taken 4599 times.
✗ Branch 3 not taken.
12850 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
364 4599 avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
365 #endif
366 }
367
5/6
✓ Branch 0 taken 13402 times.
✓ Branch 1 taken 5450 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13402 times.
✓ Branch 4 taken 9456 times.
✓ Branch 5 taken 3946 times.
18852 if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
368
1/2
✓ Branch 0 taken 3946 times.
✗ Branch 1 not taken.
3946 av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
369 }
370
371 18852 end:
372 18862 unlock_avcodec(codec);
373
374 18862 return ret;
375 10 free_and_end:
376 10 avcodec_close(avctx);
377 10 goto end;
378 }
379
380 28 void avcodec_flush_buffers(AVCodecContext *avctx)
381 {
382 28 AVCodecInternal *avci = avctx->internal;
383
384
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
28 if (av_codec_is_encoder(avctx->codec)) {
385 int caps = avctx->codec->capabilities;
386
387 if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) {
388 // Only encoders that explicitly declare support for it can be
389 // flushed. Otherwise, this is a no-op.
390 av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder "
391 "that doesn't support it\n");
392 return;
393 }
394
395 // We haven't implemented flushing for frame-threaded encoders.
396 av_assert0(!(caps & AV_CODEC_CAP_FRAME_THREADS));
397 }
398
399 28 avci->draining = 0;
400 28 avci->draining_done = 0;
401 28 avci->nb_draining_errors = 0;
402 28 av_frame_unref(avci->buffer_frame);
403 28 av_packet_unref(avci->buffer_pkt);
404
405 28 av_packet_unref(avci->last_pkt_props);
406
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
28 while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) {
407 av_fifo_generic_read(avci->pkt_props,
408 avci->last_pkt_props, sizeof(*avci->last_pkt_props),
409 NULL);
410 av_packet_unref(avci->last_pkt_props);
411 }
412 28 av_fifo_reset(avci->pkt_props);
413
414 28 av_frame_unref(avci->es.in_frame);
415 28 av_packet_unref(avci->ds.in_pkt);
416
417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
418 ff_thread_flush(avctx);
419
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 else if (avctx->codec->flush)
420 avctx->codec->flush(avctx);
421
422 28 avctx->pts_correction_last_pts =
423 28 avctx->pts_correction_last_dts = INT64_MIN;
424
425
1/2
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
28 if (av_codec_is_decoder(avctx->codec))
426 28 av_bsf_flush(avci->bsf);
427 }
428
429 7182 void avsubtitle_free(AVSubtitle *sub)
430 {
431 int i;
432
433
2/2
✓ Branch 0 taken 796 times.
✓ Branch 1 taken 7182 times.
7978 for (i = 0; i < sub->num_rects; i++) {
434 796 av_freep(&sub->rects[i]->data[0]);
435 796 av_freep(&sub->rects[i]->data[1]);
436 796 av_freep(&sub->rects[i]->data[2]);
437 796 av_freep(&sub->rects[i]->data[3]);
438 796 av_freep(&sub->rects[i]->text);
439 796 av_freep(&sub->rects[i]->ass);
440 796 av_freep(&sub->rects[i]);
441 }
442
443 7182 av_freep(&sub->rects);
444
445 7182 memset(sub, 0, sizeof(*sub));
446 7182 }
447
448 51644 av_cold int avcodec_close(AVCodecContext *avctx)
449 {
450 int i;
451
452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51644 times.
51644 if (!avctx)
453 return 0;
454
455
2/2
✓ Branch 1 taken 18862 times.
✓ Branch 2 taken 32782 times.
51644 if (avcodec_is_open(avctx)) {
456 18862 AVCodecInternal *avci = avctx->internal;
457
458 18862 if (CONFIG_FRAME_THREAD_ENCODER &&
459
4/4
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 18712 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 132 times.
18862 avci->frame_thread_encoder && avctx->thread_count > 1) {
460 18 ff_frame_thread_encoder_free(avctx);
461 }
462
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 18834 times.
18862 if (HAVE_THREADS && avci->thread_ctx)
463 28 ff_thread_free(avctx);
464
4/4
✓ Branch 0 taken 18842 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 6177 times.
✓ Branch 3 taken 12665 times.
18862 if (avci->needs_close && avctx->codec->close)
465 6177 avctx->codec->close(avctx);
466 18862 avci->byte_buffer_size = 0;
467 18862 av_freep(&avci->byte_buffer);
468 18862 av_frame_free(&avci->buffer_frame);
469 18862 av_packet_free(&avci->buffer_pkt);
470
1/2
✓ Branch 0 taken 18862 times.
✗ Branch 1 not taken.
18862 if (avci->pkt_props) {
471
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18862 times.
18862 while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) {
472 av_packet_unref(avci->last_pkt_props);
473 av_fifo_generic_read(avci->pkt_props, avci->last_pkt_props,
474 sizeof(*avci->last_pkt_props), NULL);
475 }
476 18862 av_fifo_freep(&avci->pkt_props);
477 }
478 18862 av_packet_free(&avci->last_pkt_props);
479
480 18862 av_packet_free(&avci->ds.in_pkt);
481 18862 av_frame_free(&avci->es.in_frame);
482
483 18862 av_buffer_unref(&avci->pool);
484
485
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 18862 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
18862 if (avctx->hwaccel && avctx->hwaccel->uninit)
486 avctx->hwaccel->uninit(avctx);
487 18862 av_freep(&avci->hwaccel_priv_data);
488
489 18862 av_bsf_free(&avci->bsf);
490
491 18862 av_freep(&avctx->internal);
492 }
493
494
2/2
✓ Branch 0 taken 390 times.
✓ Branch 1 taken 51644 times.
52034 for (i = 0; i < avctx->nb_coded_side_data; i++)
495 390 av_freep(&avctx->coded_side_data[i].data);
496 51644 av_freep(&avctx->coded_side_data);
497 51644 avctx->nb_coded_side_data = 0;
498
499 51644 av_buffer_unref(&avctx->hw_frames_ctx);
500 51644 av_buffer_unref(&avctx->hw_device_ctx);
501
502
5/6
✓ Branch 0 taken 13929 times.
✓ Branch 1 taken 37715 times.
✓ Branch 2 taken 13929 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4128 times.
✓ Branch 5 taken 9801 times.
51644 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
503 4128 av_opt_free(avctx->priv_data);
504 51644 av_opt_free(avctx);
505 51644 av_freep(&avctx->priv_data);
506
2/2
✓ Branch 1 taken 6004 times.
✓ Branch 2 taken 45640 times.
51644 if (av_codec_is_encoder(avctx->codec)) {
507 6004 av_freep(&avctx->extradata);
508 6004 avctx->extradata_size = 0;
509
2/2
✓ Branch 1 taken 13386 times.
✓ Branch 2 taken 32254 times.
45640 } else if (av_codec_is_decoder(avctx->codec))
510 13386 av_freep(&avctx->subtitle_header);
511
512 51644 avctx->codec = NULL;
513 51644 avctx->active_thread_type = 0;
514
515 51644 return 0;
516 }
517
518 14583 static const char *unknown_if_null(const char *str)
519 {
520
1/2
✓ Branch 0 taken 14583 times.
✗ Branch 1 not taken.
14583 return str ? str : "unknown";
521 }
522
523 12808 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
524 {
525 const char *codec_type;
526 const char *codec_name;
527 12808 const char *profile = NULL;
528 AVBPrint bprint;
529 int64_t bitrate;
530 12808 int new_line = 0;
531 AVRational display_aspect_ratio;
532
2/2
✓ Branch 0 taken 12774 times.
✓ Branch 1 taken 34 times.
12808 const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
533 const char *str;
534
535
2/4
✓ Branch 0 taken 12808 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12808 times.
12808 if (!buf || buf_size <= 0)
536 3 return;
537 12808 av_bprint_init_for_buffer(&bprint, buf, buf_size);
538 12808 codec_type = av_get_media_type_string(enc->codec_type);
539 12808 codec_name = avcodec_get_name(enc->codec_id);
540 12808 profile = avcodec_profile_name(enc->codec_id, enc->profile);
541
542
1/2
✓ Branch 0 taken 12808 times.
✗ Branch 1 not taken.
12808 av_bprintf(&bprint, "%s: %s", codec_type ? codec_type : "unknown",
543 codec_name);
544 12808 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
545
546
4/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 12776 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 31 times.
12808 if (enc->codec && strcmp(enc->codec->name, codec_name))
547 1 av_bprintf(&bprint, " (%s)", enc->codec->name);
548
549
2/2
✓ Branch 0 taken 1594 times.
✓ Branch 1 taken 11214 times.
12808 if (profile)
550 1594 av_bprintf(&bprint, " (%s)", profile);
551
2/2
✓ Branch 0 taken 9888 times.
✓ Branch 1 taken 2920 times.
12808 if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
552
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9888 times.
9888 && av_log_get_level() >= AV_LOG_VERBOSE
553 && enc->refs)
554 av_bprintf(&bprint, ", %d reference frame%s",
555 enc->refs, enc->refs > 1 ? "s" : "");
556
557
2/2
✓ Branch 0 taken 7364 times.
✓ Branch 1 taken 5444 times.
12808 if (enc->codec_tag)
558 7364 av_bprintf(&bprint, " (%s / 0x%04X)",
559 7364 av_fourcc2str(enc->codec_tag), enc->codec_tag);
560
561
5/5
✓ Branch 0 taken 9888 times.
✓ Branch 1 taken 2733 times.
✓ Branch 2 taken 72 times.
✓ Branch 3 taken 112 times.
✓ Branch 4 taken 3 times.
12808 switch (enc->codec_type) {
562 9888 case AVMEDIA_TYPE_VIDEO:
563 {
564 unsigned len;
565
566 9888 av_bprintf(&bprint, "%s%s", separator,
567
2/2
✓ Branch 0 taken 9807 times.
✓ Branch 1 taken 81 times.
9888 enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
568 9807 unknown_if_null(av_get_pix_fmt_name(enc->pix_fmt)));
569
570 9888 av_bprint_chars(&bprint, '(', 1);
571 9888 len = bprint.len;
572
573 /* The following check ensures that '(' has been written
574 * and therefore allows us to erase it if it turns out
575 * to be unnecessary. */
576
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9888 times.
9888 if (!av_bprint_is_complete(&bprint))
577 return;
578
579
3/4
✓ Branch 0 taken 3467 times.
✓ Branch 1 taken 6421 times.
✓ Branch 2 taken 3467 times.
✗ Branch 3 not taken.
9888 if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
580
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 3450 times.
3467 enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
581 17 av_bprintf(&bprint, "%d bpc, ", enc->bits_per_raw_sample);
582
3/4
✓ Branch 0 taken 4548 times.
✓ Branch 1 taken 5340 times.
✓ Branch 2 taken 4548 times.
✗ Branch 3 not taken.
14436 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED &&
583 4548 (str = av_color_range_name(enc->color_range)))
584 4548 av_bprintf(&bprint, "%s, ", str);
585
586
2/2
✓ Branch 0 taken 8314 times.
✓ Branch 1 taken 1574 times.
9888 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
587
1/2
✓ Branch 0 taken 8314 times.
✗ Branch 1 not taken.
8314 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
588
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 8296 times.
8314 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
589 1592 const char *col = unknown_if_null(av_color_space_name(enc->colorspace));
590 1592 const char *pri = unknown_if_null(av_color_primaries_name(enc->color_primaries));
591 1592 const char *trc = unknown_if_null(av_color_transfer_name(enc->color_trc));
592
4/4
✓ Branch 0 taken 157 times.
✓ Branch 1 taken 1435 times.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 130 times.
1592 if (strcmp(col, pri) || strcmp(col, trc)) {
593 1462 new_line = 1;
594 1462 av_bprintf(&bprint, "%s/%s/%s, ", col, pri, trc);
595 } else
596 130 av_bprintf(&bprint, "%s, ", col);
597 }
598
599
2/2
✓ Branch 0 taken 5275 times.
✓ Branch 1 taken 4613 times.
9888 if (enc->field_order != AV_FIELD_UNKNOWN) {
600 5275 const char *field_order = "progressive";
601
2/2
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 5161 times.
5275 if (enc->field_order == AV_FIELD_TT)
602 114 field_order = "top first";
603
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 5126 times.
5161 else if (enc->field_order == AV_FIELD_BB)
604 35 field_order = "bottom first";
605
2/2
✓ Branch 0 taken 319 times.
✓ Branch 1 taken 4807 times.
5126 else if (enc->field_order == AV_FIELD_TB)
606 319 field_order = "top coded first (swapped)";
607
2/2
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 4736 times.
4807 else if (enc->field_order == AV_FIELD_BT)
608 71 field_order = "bottom coded first (swapped)";
609
610 5275 av_bprintf(&bprint, "%s, ", field_order);
611 }
612
613
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9888 times.
9888 if (av_log_get_level() >= AV_LOG_VERBOSE &&
614 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED &&
615 (str = av_chroma_location_name(enc->chroma_sample_location)))
616 av_bprintf(&bprint, "%s, ", str);
617
618
2/2
✓ Branch 0 taken 3764 times.
✓ Branch 1 taken 6124 times.
9888 if (len == bprint.len) {
619 3764 bprint.str[len - 1] = '\0';
620 3764 bprint.len--;
621 } else {
622
1/2
✓ Branch 0 taken 6124 times.
✗ Branch 1 not taken.
6124 if (bprint.len - 2 < bprint.size) {
623 /* Erase the last ", " */
624 6124 bprint.len -= 2;
625 6124 bprint.str[bprint.len] = '\0';
626 }
627 6124 av_bprint_chars(&bprint, ')', 1);
628 }
629 }
630
631
2/2
✓ Branch 0 taken 9878 times.
✓ Branch 1 taken 10 times.
9888 if (enc->width) {
632
2/2
✓ Branch 0 taken 1460 times.
✓ Branch 1 taken 8418 times.
9878 av_bprintf(&bprint, "%s%dx%d", new_line ? separator : ", ",
633 enc->width, enc->height);
634
635
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9878 times.
9878 if (av_log_get_level() >= AV_LOG_VERBOSE &&
636 (enc->width != enc->coded_width ||
637 enc->height != enc->coded_height))
638 av_bprintf(&bprint, " (%dx%d)",
639 enc->coded_width, enc->coded_height);
640
641
2/2
✓ Branch 0 taken 1270 times.
✓ Branch 1 taken 8608 times.
9878 if (enc->sample_aspect_ratio.num) {
642 1270 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
643 1270 enc->width * (int64_t)enc->sample_aspect_ratio.num,
644 1270 enc->height * (int64_t)enc->sample_aspect_ratio.den,
645 1024 * 1024);
646 1270 av_bprintf(&bprint, " [SAR %d:%d DAR %d:%d]",
647 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
648 display_aspect_ratio.num, display_aspect_ratio.den);
649 }
650
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9878 times.
9878 if (av_log_get_level() >= AV_LOG_DEBUG) {
651 int g = av_gcd(enc->time_base.num, enc->time_base.den);
652 av_bprintf(&bprint, ", %d/%d",
653 enc->time_base.num / g, enc->time_base.den / g);
654 }
655 }
656
2/2
✓ Branch 0 taken 4880 times.
✓ Branch 1 taken 5008 times.
9888 if (encode) {
657 4880 av_bprintf(&bprint, ", q=%d-%d", enc->qmin, enc->qmax);
658 } else {
659
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5006 times.
5008 if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
660 2 av_bprintf(&bprint, ", Closed Captions");
661
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5004 times.
5008 if (enc->properties & FF_CODEC_PROPERTY_FILM_GRAIN)
662 4 av_bprintf(&bprint, ", Film Grain");
663
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 4985 times.
5008 if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
664 23 av_bprintf(&bprint, ", lossless");
665 }
666 9888 break;
667 2733 case AVMEDIA_TYPE_AUDIO:
668 2733 av_bprintf(&bprint, "%s", separator);
669
670
2/2
✓ Branch 0 taken 2727 times.
✓ Branch 1 taken 6 times.
2733 if (enc->sample_rate) {
671 2727 av_bprintf(&bprint, "%d Hz, ", enc->sample_rate);
672 }
673 2733 av_bprint_channel_layout(&bprint, enc->channels, enc->channel_layout);
674
3/4
✓ Branch 0 taken 2732 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2732 times.
✗ Branch 3 not taken.
5465 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE &&
675 2732 (str = av_get_sample_fmt_name(enc->sample_fmt))) {
676 2732 av_bprintf(&bprint, ", %s", str);
677 }
678
2/2
✓ Branch 0 taken 362 times.
✓ Branch 1 taken 2371 times.
2733 if ( enc->bits_per_raw_sample > 0
679
2/2
✓ Branch 1 taken 122 times.
✓ Branch 2 taken 240 times.
362 && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
680 122 av_bprintf(&bprint, " (%d bit)", enc->bits_per_raw_sample);
681
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2729 times.
2733 if (av_log_get_level() >= AV_LOG_VERBOSE) {
682
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (enc->initial_padding)
683 2 av_bprintf(&bprint, ", delay %d", enc->initial_padding);
684
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (enc->trailing_padding)
685 av_bprintf(&bprint, ", padding %d", enc->trailing_padding);
686 }
687 2733 break;
688 72 case AVMEDIA_TYPE_DATA:
689
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
72 if (av_log_get_level() >= AV_LOG_DEBUG) {
690 int g = av_gcd(enc->time_base.num, enc->time_base.den);
691 if (g)
692 av_bprintf(&bprint, ", %d/%d",
693 enc->time_base.num / g, enc->time_base.den / g);
694 }
695 72 break;
696 112 case AVMEDIA_TYPE_SUBTITLE:
697
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 95 times.
112 if (enc->width)
698 17 av_bprintf(&bprint, ", %dx%d", enc->width, enc->height);
699 112 break;
700 3 default:
701 3 return;
702 }
703
2/2
✓ Branch 0 taken 6194 times.
✓ Branch 1 taken 6611 times.
12805 if (encode) {
704
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6194 times.
6194 if (enc->flags & AV_CODEC_FLAG_PASS1)
705 av_bprintf(&bprint, ", pass 1");
706
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6194 times.
6194 if (enc->flags & AV_CODEC_FLAG_PASS2)
707 av_bprintf(&bprint, ", pass 2");
708 }
709 12805 bitrate = get_bit_rate(enc);
710
2/2
✓ Branch 0 taken 8308 times.
✓ Branch 1 taken 4497 times.
12805 if (bitrate != 0) {
711 8308 av_bprintf(&bprint, ", %"PRId64" kb/s", bitrate / 1000);
712
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4497 times.
4497 } else if (enc->rc_max_rate > 0) {
713 av_bprintf(&bprint, ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
714 }
715 }
716
717 2608501 int avcodec_is_open(AVCodecContext *s)
718 {
719 2608501 return !!s->internal;
720 }
721