FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/avcodec.c
Date: 2022-01-21 12:56:39
Exec Total Coverage
Lines: 341 432 78.9%
Branches: 269 376 71.5%

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 6271 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 6271 return LIBAVCODEC_VERSION_INT;
54 }
55
56 6271 const char *avcodec_configuration(void)
57 {
58 6271 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 47223 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 1341854 times.
✓ Branch 1 taken 47223 times.
1389077 for (i = 0; i < count; i++) {
72 1341854 int r = func(c, (char *)arg + i * size);
73
2/2
✓ Branch 0 taken 28405 times.
✓ Branch 1 taken 1313449 times.
1341854 if (ret)
74 28405 ret[i] = r;
75 }
76 47223 emms_c();
77 47223 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 49048 static void lock_avcodec(const AVCodec *codec)
96 {
97
4/4
✓ Branch 0 taken 5842 times.
✓ Branch 1 taken 43206 times.
✓ Branch 2 taken 641 times.
✓ Branch 3 taken 5201 times.
49048 if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
98 641 ff_mutex_lock(&codec_mutex);
99 49048 }
100
101 49048 static void unlock_avcodec(const AVCodec *codec)
102 {
103
4/4
✓ Branch 0 taken 5842 times.
✓ Branch 1 taken 43206 times.
✓ Branch 2 taken 641 times.
✓ Branch 3 taken 5201 times.
49048 if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
104 641 ff_mutex_unlock(&codec_mutex);
105 49048 }
106
107 22117 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 18408 times.
✓ Branch 1 taken 3709 times.
✗ Branch 2 not taken.
22117 switch (ctx->codec_type) {
113 18408 case AVMEDIA_TYPE_VIDEO:
114 case AVMEDIA_TYPE_DATA:
115 case AVMEDIA_TYPE_SUBTITLE:
116 case AVMEDIA_TYPE_ATTACHMENT:
117 18408 bit_rate = ctx->bit_rate;
118 18408 break;
119 3709 case AVMEDIA_TYPE_AUDIO:
120 3709 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
121
2/2
✓ Branch 0 taken 2179 times.
✓ Branch 1 taken 1530 times.
3709 if (bits_per_sample) {
122 2179 bit_rate = ctx->sample_rate * (int64_t)ctx->channels;
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2179 times.
2179 if (bit_rate > INT64_MAX / bits_per_sample) {
124 bit_rate = 0;
125 } else
126 2179 bit_rate *= bits_per_sample;
127 } else
128 1530 bit_rate = ctx->bit_rate;
129 3709 break;
130 default:
131 bit_rate = 0;
132 break;
133 }
134 22117 return bit_rate;
135 }
136
137 31122 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
138 {
139 31122 int ret = 0;
140 AVCodecInternal *avci;
141
142
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 31122 times.
31122 if (avcodec_is_open(avctx))
143 return 0;
144
145
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 31118 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
31122 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 31118 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 24093 times.
✓ Branch 3 taken 7025 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24093 times.
31122 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 31118 times.
31122 if (!codec)
155 4 codec = avctx->codec;
156
157
3/4
✓ Branch 0 taken 31118 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 31118 times.
✗ Branch 3 not taken.
31122 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
158
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 31118 times.
31122 avctx->codec_id == AV_CODEC_ID_NONE) {
159 4 avctx->codec_type = codec->type;
160 4 avctx->codec_id = codec->id;
161 }
162
2/4
✓ Branch 0 taken 31122 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 31122 times.
31122 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 31122 avctx->codec = codec;
168
169
2/4
✓ Branch 0 taken 31122 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 31122 times.
31122 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
170 return AVERROR(EINVAL);
171
172 31122 lock_avcodec(codec);
173
174 31122 avci = av_mallocz(sizeof(*avci));
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31122 times.
31122 if (!avci) {
176 ret = AVERROR(ENOMEM);
177 goto end;
178 }
179 31122 avctx->internal = avci;
180
181 31122 avci->buffer_frame = av_frame_alloc();
182 31122 avci->buffer_pkt = av_packet_alloc();
183 31122 avci->es.in_frame = av_frame_alloc();
184 31122 avci->in_pkt = av_packet_alloc();
185 31122 avci->last_pkt_props = av_packet_alloc();
186 31122 avci->pkt_props = av_fifo_alloc(sizeof(*avci->last_pkt_props));
187
2/4
✓ Branch 0 taken 31122 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31122 times.
✗ Branch 3 not taken.
31122 if (!avci->buffer_frame || !avci->buffer_pkt ||
188
2/4
✓ Branch 0 taken 31122 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31122 times.
✗ Branch 3 not taken.
31122 !avci->es.in_frame || !avci->in_pkt ||
189
2/4
✓ Branch 0 taken 31122 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 31122 times.
31122 !avci->last_pkt_props || !avci->pkt_props) {
190 ret = AVERROR(ENOMEM);
191 goto free_and_end;
192 }
193
194 31122 avci->skip_samples_multiplier = 1;
195
196
2/2
✓ Branch 0 taken 13749 times.
✓ Branch 1 taken 17373 times.
31122 if (codec->priv_data_size > 0) {
197
2/2
✓ Branch 0 taken 6909 times.
✓ Branch 1 taken 6840 times.
13749 if (!avctx->priv_data) {
198 6909 avctx->priv_data = av_mallocz(codec->priv_data_size);
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6909 times.
6909 if (!avctx->priv_data) {
200 ret = AVERROR(ENOMEM);
201 goto free_and_end;
202 }
203
2/2
✓ Branch 0 taken 1780 times.
✓ Branch 1 taken 5129 times.
6909 if (codec->priv_class) {
204 1780 *(const AVClass **)avctx->priv_data = codec->priv_class;
205 1780 av_opt_set_defaults(avctx->priv_data);
206 }
207 }
208
3/4
✓ Branch 0 taken 4016 times.
✓ Branch 1 taken 9733 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4016 times.
13749 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, options)) < 0)
209 goto free_and_end;
210 } else {
211 17373 avctx->priv_data = NULL;
212 }
213
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 31122 times.
31122 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 31122 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
31122 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 11955 times.
✓ Branch 1 taken 19167 times.
✓ Branch 2 taken 11955 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11946 times.
✓ Branch 5 taken 9 times.
✓ Branch 6 taken 11946 times.
✗ Branch 7 not taken.
31122 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
224
3/6
✓ Branch 0 taken 11946 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11946 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11946 times.
✗ Branch 5 not taken.
11946 (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 11955 times.
✓ Branch 1 taken 19167 times.
✓ Branch 2 taken 11955 times.
✗ Branch 3 not taken.
31122 if (avctx->coded_width && avctx->coded_height)
226 11955 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
227
3/4
✓ Branch 0 taken 11508 times.
✓ Branch 1 taken 7659 times.
✓ Branch 2 taken 11508 times.
✗ Branch 3 not taken.
19167 else if (avctx->width && avctx->height)
228 11508 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31122 times.
31122 if (ret < 0)
230 goto free_and_end;
231 }
232
233
5/8
✓ Branch 0 taken 7659 times.
✓ Branch 1 taken 23463 times.
✓ Branch 2 taken 7659 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7659 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 7659 times.
31122 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
234
1/2
✓ Branch 1 taken 23463 times.
✗ Branch 2 not taken.
23463 && ( 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 23463 times.
23463 || 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 23463 times.
✓ Branch 1 taken 7659 times.
✓ Branch 2 taken 23463 times.
✗ Branch 3 not taken.
31122 if (avctx->width > 0 && avctx->height > 0) {
241
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 23463 times.
23463 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 31122 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 31122 times.
31122 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 31122 times.
31122 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 31122 times.
31122 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 31122 avctx->frame_number = 0;
268 31122 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
269
270
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 31115 times.
31122 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 4067 times.
✓ Branch 1 taken 27055 times.
31122 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
287
4/4
✓ Branch 0 taken 1153 times.
✓ Branch 1 taken 2914 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 1142 times.
4067 (!avctx->time_base.num || !avctx->time_base.den)) {
288 2925 avctx->time_base.num = 1;
289 2925 avctx->time_base.den = avctx->sample_rate;
290 }
291
292
2/2
✓ Branch 1 taken 17926 times.
✓ Branch 2 taken 13196 times.
31122 if (av_codec_is_encoder(avctx->codec))
293 17926 ret = ff_encode_preinit(avctx);
294 else
295 13196 ret = ff_decode_preinit(avctx);
296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31122 times.
31122 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 17926 times.
✓ Branch 2 taken 13196 times.
31122 if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
303 17926 unlock_avcodec(codec); //we will instantiate a few encoders thus kick the counter to prevent false detection of a problem
304 17926 ret = ff_frame_thread_encoder_init(avctx);
305 17926 lock_avcodec(codec);
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17926 times.
17926 if (ret < 0)
307 goto free_and_end;
308 }
309
310 31122 if (HAVE_THREADS
311
3/4
✓ Branch 0 taken 1494 times.
✓ Branch 1 taken 29628 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1494 times.
31122 && !(avci->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
312 29628 ret = ff_thread_init(avctx);
313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29628 times.
29628 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 1506 times.
✓ Branch 1 taken 29616 times.
31122 if (!(avctx->active_thread_type & FF_THREAD_FRAME) ||
321
2/2
✓ Branch 0 taken 1494 times.
✓ Branch 1 taken 12 times.
1506 avci->frame_thread_encoder) {
322
2/2
✓ Branch 0 taken 25839 times.
✓ Branch 1 taken 5271 times.
31110 if (avctx->codec->init) {
323 25839 ret = avctx->codec->init(avctx);
324
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 25825 times.
25839 if (ret < 0) {
325 14 avci->needs_close = avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP;
326 14 goto free_and_end;
327 }
328 }
329 31096 avci->needs_close = 1;
330 }
331
332 31108 ret=0;
333
334
2/2
✓ Branch 1 taken 13182 times.
✓ Branch 2 taken 17926 times.
31108 if (av_codec_is_decoder(avctx->codec)) {
335
2/2
✓ Branch 0 taken 8928 times.
✓ Branch 1 taken 4254 times.
13182 if (!avctx->bit_rate)
336 8928 avctx->bit_rate = get_bit_rate(avctx);
337 /* validate channel layout from the decoder */
338
2/2
✓ Branch 0 taken 1749 times.
✓ Branch 1 taken 11433 times.
13182 if (avctx->channel_layout) {
339 1749 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
340
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1733 times.
1749 if (!avctx->channels)
341 16 avctx->channels = channels;
342
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1726 times.
1733 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 2760 times.
✓ Branch 1 taken 10422 times.
✓ Branch 2 taken 2760 times.
✗ Branch 3 not taken.
13182 if (avctx->channels && avctx->channels < 0 ||
353
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13182 times.
13182 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 13182 times.
13182 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 4711 times.
✓ Branch 1 taken 8471 times.
✓ Branch 2 taken 4711 times.
✗ Branch 3 not taken.
13182 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
364 4711 avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
365 #endif
366 }
367
2/2
✓ Branch 0 taken 4016 times.
✓ Branch 1 taken 27092 times.
31108 if (codec->priv_class)
368
1/2
✓ Branch 0 taken 4016 times.
✗ Branch 1 not taken.
4016 av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
369
370 31108 end:
371 31122 unlock_avcodec(codec);
372
373 31122 return ret;
374 14 free_and_end:
375 14 avcodec_close(avctx);
376 14 goto end;
377 }
378
379 28 void avcodec_flush_buffers(AVCodecContext *avctx)
380 {
381 28 AVCodecInternal *avci = avctx->internal;
382
383
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
28 if (av_codec_is_encoder(avctx->codec)) {
384 int caps = avctx->codec->capabilities;
385
386 if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) {
387 // Only encoders that explicitly declare support for it can be
388 // flushed. Otherwise, this is a no-op.
389 av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder "
390 "that doesn't support it\n");
391 return;
392 }
393 }
394
395 28 avci->draining = 0;
396 28 avci->draining_done = 0;
397 28 avci->nb_draining_errors = 0;
398 28 av_frame_unref(avci->buffer_frame);
399 28 av_packet_unref(avci->buffer_pkt);
400
401 28 av_packet_unref(avci->last_pkt_props);
402
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
28 while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) {
403 av_fifo_generic_read(avci->pkt_props,
404 avci->last_pkt_props, sizeof(*avci->last_pkt_props),
405 NULL);
406 av_packet_unref(avci->last_pkt_props);
407 }
408 28 av_fifo_reset(avci->pkt_props);
409
410 28 av_frame_unref(avci->es.in_frame);
411 28 av_packet_unref(avci->in_pkt);
412
413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
414 ff_thread_flush(avctx);
415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 else if (avctx->codec->flush)
416 avctx->codec->flush(avctx);
417
418 28 avctx->pts_correction_last_pts =
419 28 avctx->pts_correction_last_dts = INT64_MIN;
420
421
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (avci->bsf)
422 28 av_bsf_flush(avci->bsf);
423 }
424
425 7491 void avsubtitle_free(AVSubtitle *sub)
426 {
427 int i;
428
429
2/2
✓ Branch 0 taken 839 times.
✓ Branch 1 taken 7491 times.
8330 for (i = 0; i < sub->num_rects; i++) {
430 839 av_freep(&sub->rects[i]->data[0]);
431 839 av_freep(&sub->rects[i]->data[1]);
432 839 av_freep(&sub->rects[i]->data[2]);
433 839 av_freep(&sub->rects[i]->data[3]);
434 839 av_freep(&sub->rects[i]->text);
435 839 av_freep(&sub->rects[i]->ass);
436 839 av_freep(&sub->rects[i]);
437 }
438
439 7491 av_freep(&sub->rects);
440
441 7491 memset(sub, 0, sizeof(*sub));
442 7491 }
443
444 64918 av_cold int avcodec_close(AVCodecContext *avctx)
445 {
446 int i;
447
448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64918 times.
64918 if (!avctx)
449 return 0;
450
451
2/2
✓ Branch 1 taken 31122 times.
✓ Branch 2 taken 33796 times.
64918 if (avcodec_is_open(avctx)) {
452 31122 AVCodecInternal *avci = avctx->internal;
453
454 31122 if (CONFIG_FRAME_THREAD_ENCODER &&
455
4/4
✓ Branch 0 taken 13434 times.
✓ Branch 1 taken 17688 times.
✓ Branch 2 taken 1494 times.
✓ Branch 3 taken 11940 times.
31122 avci->frame_thread_encoder && avctx->thread_count > 1) {
456 1494 ff_frame_thread_encoder_free(avctx);
457 }
458
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 31094 times.
31122 if (HAVE_THREADS && avci->thread_ctx)
459 28 ff_thread_free(avctx);
460
4/4
✓ Branch 0 taken 31102 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 6276 times.
✓ Branch 3 taken 24826 times.
31122 if (avci->needs_close && avctx->codec->close)
461 6276 avctx->codec->close(avctx);
462 31122 avci->byte_buffer_size = 0;
463 31122 av_freep(&avci->byte_buffer);
464 31122 av_frame_free(&avci->buffer_frame);
465 31122 av_packet_free(&avci->buffer_pkt);
466
1/2
✓ Branch 0 taken 31122 times.
✗ Branch 1 not taken.
31122 if (avci->pkt_props) {
467
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 31122 times.
31122 while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) {
468 av_packet_unref(avci->last_pkt_props);
469 av_fifo_generic_read(avci->pkt_props, avci->last_pkt_props,
470 sizeof(*avci->last_pkt_props), NULL);
471 }
472 31122 av_fifo_freep(&avci->pkt_props);
473 }
474 31122 av_packet_free(&avci->last_pkt_props);
475
476 31122 av_packet_free(&avci->in_pkt);
477 31122 av_frame_free(&avci->es.in_frame);
478
479 31122 av_buffer_unref(&avci->pool);
480
481
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 31122 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
31122 if (avctx->hwaccel && avctx->hwaccel->uninit)
482 avctx->hwaccel->uninit(avctx);
483 31122 av_freep(&avci->hwaccel_priv_data);
484
485 31122 av_bsf_free(&avci->bsf);
486
487 31122 av_freep(&avctx->internal);
488 }
489
490
2/2
✓ Branch 0 taken 392 times.
✓ Branch 1 taken 64918 times.
65310 for (i = 0; i < avctx->nb_coded_side_data; i++)
491 392 av_freep(&avctx->coded_side_data[i].data);
492 64918 av_freep(&avctx->coded_side_data);
493 64918 avctx->nb_coded_side_data = 0;
494
495 64918 av_buffer_unref(&avctx->hw_frames_ctx);
496 64918 av_buffer_unref(&avctx->hw_device_ctx);
497
498
5/6
✓ Branch 0 taken 14329 times.
✓ Branch 1 taken 50589 times.
✓ Branch 2 taken 14329 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4223 times.
✓ Branch 5 taken 10106 times.
64918 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
499 4223 av_opt_free(avctx->priv_data);
500 64918 av_opt_free(avctx);
501 64918 av_freep(&avctx->priv_data);
502
2/2
✓ Branch 1 taken 17928 times.
✓ Branch 2 taken 46990 times.
64918 if (av_codec_is_encoder(avctx->codec)) {
503 17928 av_freep(&avctx->extradata);
504 17928 avctx->extradata_size = 0;
505
2/2
✓ Branch 1 taken 13786 times.
✓ Branch 2 taken 33204 times.
46990 } else if (av_codec_is_decoder(avctx->codec))
506 13786 av_freep(&avctx->subtitle_header);
507
508 64918 avctx->codec = NULL;
509 64918 avctx->active_thread_type = 0;
510
511 64918 return 0;
512 }
513
514 14966 static const char *unknown_if_null(const char *str)
515 {
516
1/2
✓ Branch 0 taken 14966 times.
✗ Branch 1 not taken.
14966 return str ? str : "unknown";
517 }
518
519 13192 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
520 {
521 const char *codec_type;
522 const char *codec_name;
523 13192 const char *profile = NULL;
524 AVBPrint bprint;
525 int64_t bitrate;
526 13192 int new_line = 0;
527 AVRational display_aspect_ratio;
528
2/2
✓ Branch 0 taken 13154 times.
✓ Branch 1 taken 38 times.
13192 const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
529 const char *str;
530
531
2/4
✓ Branch 0 taken 13192 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13192 times.
13192 if (!buf || buf_size <= 0)
532 3 return;
533 13192 av_bprint_init_for_buffer(&bprint, buf, buf_size);
534 13192 codec_type = av_get_media_type_string(enc->codec_type);
535 13192 codec_name = avcodec_get_name(enc->codec_id);
536 13192 profile = avcodec_profile_name(enc->codec_id, enc->profile);
537
538
1/2
✓ Branch 0 taken 13192 times.
✗ Branch 1 not taken.
13192 av_bprintf(&bprint, "%s: %s", codec_type ? codec_type : "unknown",
539 codec_name);
540 13192 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
541
542
4/4
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 13156 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 35 times.
13192 if (enc->codec && strcmp(enc->codec->name, codec_name))
543 1 av_bprintf(&bprint, " (%s)", enc->codec->name);
544
545
2/2
✓ Branch 0 taken 1631 times.
✓ Branch 1 taken 11561 times.
13192 if (profile)
546 1631 av_bprintf(&bprint, " (%s)", profile);
547
2/2
✓ Branch 0 taken 10158 times.
✓ Branch 1 taken 3034 times.
13192 if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
548
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10158 times.
10158 && av_log_get_level() >= AV_LOG_VERBOSE
549 && enc->refs)
550 av_bprintf(&bprint, ", %d reference frame%s",
551 enc->refs, enc->refs > 1 ? "s" : "");
552
553
2/2
✓ Branch 0 taken 7569 times.
✓ Branch 1 taken 5623 times.
13192 if (enc->codec_tag)
554 7569 av_bprintf(&bprint, " (%s / 0x%04X)",
555 7569 av_fourcc2str(enc->codec_tag), enc->codec_tag);
556
557
5/5
✓ Branch 0 taken 10158 times.
✓ Branch 1 taken 2831 times.
✓ Branch 2 taken 80 times.
✓ Branch 3 taken 120 times.
✓ Branch 4 taken 3 times.
13192 switch (enc->codec_type) {
558 10158 case AVMEDIA_TYPE_VIDEO:
559 {
560 unsigned len;
561
562 10158 av_bprintf(&bprint, "%s%s", separator,
563
2/2
✓ Branch 0 taken 10073 times.
✓ Branch 1 taken 85 times.
10158 enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
564 10073 unknown_if_null(av_get_pix_fmt_name(enc->pix_fmt)));
565
566 10158 av_bprint_chars(&bprint, '(', 1);
567 10158 len = bprint.len;
568
569 /* The following check ensures that '(' has been written
570 * and therefore allows us to erase it if it turns out
571 * to be unnecessary. */
572
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10158 times.
10158 if (!av_bprint_is_complete(&bprint))
573 return;
574
575
3/4
✓ Branch 0 taken 3161 times.
✓ Branch 1 taken 6997 times.
✓ Branch 2 taken 3161 times.
✗ Branch 3 not taken.
10158 if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
576
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 3150 times.
3161 enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
577 11 av_bprintf(&bprint, "%d bpc, ", enc->bits_per_raw_sample);
578
3/4
✓ Branch 0 taken 4691 times.
✓ Branch 1 taken 5467 times.
✓ Branch 2 taken 4691 times.
✗ Branch 3 not taken.
14849 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED &&
579 4691 (str = av_color_range_name(enc->color_range)))
580 4691 av_bprintf(&bprint, "%s, ", str);
581
582
2/2
✓ Branch 0 taken 8545 times.
✓ Branch 1 taken 1613 times.
10158 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
583
1/2
✓ Branch 0 taken 8545 times.
✗ Branch 1 not taken.
8545 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
584
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 8527 times.
8545 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
585 1631 const char *col = unknown_if_null(av_color_space_name(enc->colorspace));
586 1631 const char *pri = unknown_if_null(av_color_primaries_name(enc->color_primaries));
587 1631 const char *trc = unknown_if_null(av_color_transfer_name(enc->color_trc));
588
4/4
✓ Branch 0 taken 157 times.
✓ Branch 1 taken 1474 times.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 130 times.
1631 if (strcmp(col, pri) || strcmp(col, trc)) {
589 1501 new_line = 1;
590 1501 av_bprintf(&bprint, "%s/%s/%s, ", col, pri, trc);
591 } else
592 130 av_bprintf(&bprint, "%s, ", col);
593 }
594
595
2/2
✓ Branch 0 taken 5387 times.
✓ Branch 1 taken 4771 times.
10158 if (enc->field_order != AV_FIELD_UNKNOWN) {
596 5387 const char *field_order = "progressive";
597
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 5269 times.
5387 if (enc->field_order == AV_FIELD_TT)
598 118 field_order = "top first";
599
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 5234 times.
5269 else if (enc->field_order == AV_FIELD_BB)
600 35 field_order = "bottom first";
601
2/2
✓ Branch 0 taken 328 times.
✓ Branch 1 taken 4906 times.
5234 else if (enc->field_order == AV_FIELD_TB)
602 328 field_order = "top coded first (swapped)";
603
2/2
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 4835 times.
4906 else if (enc->field_order == AV_FIELD_BT)
604 71 field_order = "bottom coded first (swapped)";
605
606 5387 av_bprintf(&bprint, "%s, ", field_order);
607 }
608
609
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10158 times.
10158 if (av_log_get_level() >= AV_LOG_VERBOSE &&
610 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED &&
611 (str = av_chroma_location_name(enc->chroma_sample_location)))
612 av_bprintf(&bprint, "%s, ", str);
613
614
2/2
✓ Branch 0 taken 3886 times.
✓ Branch 1 taken 6272 times.
10158 if (len == bprint.len) {
615 3886 bprint.str[len - 1] = '\0';
616 3886 bprint.len--;
617 } else {
618
1/2
✓ Branch 0 taken 6272 times.
✗ Branch 1 not taken.
6272 if (bprint.len - 2 < bprint.size) {
619 /* Erase the last ", " */
620 6272 bprint.len -= 2;
621 6272 bprint.str[bprint.len] = '\0';
622 }
623 6272 av_bprint_chars(&bprint, ')', 1);
624 }
625 }
626
627
2/2
✓ Branch 0 taken 10144 times.
✓ Branch 1 taken 14 times.
10158 if (enc->width) {
628
2/2
✓ Branch 0 taken 1499 times.
✓ Branch 1 taken 8645 times.
10144 av_bprintf(&bprint, "%s%dx%d", new_line ? separator : ", ",
629 enc->width, enc->height);
630
631
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10144 times.
10144 if (av_log_get_level() >= AV_LOG_VERBOSE &&
632 (enc->width != enc->coded_width ||
633 enc->height != enc->coded_height))
634 av_bprintf(&bprint, " (%dx%d)",
635 enc->coded_width, enc->coded_height);
636
637
2/2
✓ Branch 0 taken 1298 times.
✓ Branch 1 taken 8846 times.
10144 if (enc->sample_aspect_ratio.num) {
638 1298 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
639 1298 enc->width * (int64_t)enc->sample_aspect_ratio.num,
640 1298 enc->height * (int64_t)enc->sample_aspect_ratio.den,
641 1024 * 1024);
642 1298 av_bprintf(&bprint, " [SAR %d:%d DAR %d:%d]",
643 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
644 display_aspect_ratio.num, display_aspect_ratio.den);
645 }
646
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10144 times.
10144 if (av_log_get_level() >= AV_LOG_DEBUG) {
647 int g = av_gcd(enc->time_base.num, enc->time_base.den);
648 av_bprintf(&bprint, ", %d/%d",
649 enc->time_base.num / g, enc->time_base.den / g);
650 }
651 }
652
2/2
✓ Branch 0 taken 5009 times.
✓ Branch 1 taken 5149 times.
10158 if (encode) {
653 5009 av_bprintf(&bprint, ", q=%d-%d", enc->qmin, enc->qmax);
654 } else {
655
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5147 times.
5149 if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
656 2 av_bprintf(&bprint, ", Closed Captions");
657
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5145 times.
5149 if (enc->properties & FF_CODEC_PROPERTY_FILM_GRAIN)
658 4 av_bprintf(&bprint, ", Film Grain");
659
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 5126 times.
5149 if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
660 23 av_bprintf(&bprint, ", lossless");
661 }
662 10158 break;
663 2831 case AVMEDIA_TYPE_AUDIO:
664 2831 av_bprintf(&bprint, "%s", separator);
665
666
2/2
✓ Branch 0 taken 2821 times.
✓ Branch 1 taken 10 times.
2831 if (enc->sample_rate) {
667 2821 av_bprintf(&bprint, "%d Hz, ", enc->sample_rate);
668 }
669 2831 av_bprint_channel_layout(&bprint, enc->channels, enc->channel_layout);
670
3/4
✓ Branch 0 taken 2826 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 2826 times.
✗ Branch 3 not taken.
5657 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE &&
671 2826 (str = av_get_sample_fmt_name(enc->sample_fmt))) {
672 2826 av_bprintf(&bprint, ", %s", str);
673 }
674
2/2
✓ Branch 0 taken 245 times.
✓ Branch 1 taken 2586 times.
2831 if ( enc->bits_per_raw_sample > 0
675
2/2
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 157 times.
245 && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
676 88 av_bprintf(&bprint, " (%d bit)", enc->bits_per_raw_sample);
677
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2827 times.
2831 if (av_log_get_level() >= AV_LOG_VERBOSE) {
678
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (enc->initial_padding)
679 2 av_bprintf(&bprint, ", delay %d", enc->initial_padding);
680
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (enc->trailing_padding)
681 av_bprintf(&bprint, ", padding %d", enc->trailing_padding);
682 }
683 2831 break;
684 80 case AVMEDIA_TYPE_DATA:
685
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
80 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 80 break;
692 120 case AVMEDIA_TYPE_SUBTITLE:
693
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 103 times.
120 if (enc->width)
694 17 av_bprintf(&bprint, ", %dx%d", enc->width, enc->height);
695 120 break;
696 3 default:
697 3 return;
698 }
699
2/2
✓ Branch 0 taken 6368 times.
✓ Branch 1 taken 6821 times.
13189 if (encode) {
700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6368 times.
6368 if (enc->flags & AV_CODEC_FLAG_PASS1)
701 av_bprintf(&bprint, ", pass 1");
702
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6368 times.
6368 if (enc->flags & AV_CODEC_FLAG_PASS2)
703 av_bprintf(&bprint, ", pass 2");
704 }
705 13189 bitrate = get_bit_rate(enc);
706
2/2
✓ Branch 0 taken 8524 times.
✓ Branch 1 taken 4665 times.
13189 if (bitrate != 0) {
707 8524 av_bprintf(&bprint, ", %"PRId64" kb/s", bitrate / 1000);
708
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4665 times.
4665 } else if (enc->rc_max_rate > 0) {
709 av_bprintf(&bprint, ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
710 }
711 }
712
713 2677763 int avcodec_is_open(AVCodecContext *s)
714 {
715 2677763 return !!s->internal;
716 }
717