FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/avcodec.c
Date: 2022-07-04 00:18:54
Exec Total Coverage
Lines: 337 432 78.0%
Branches: 275 382 72.0%

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/fifo.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/mem.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/thread.h"
36 #include "avcodec.h"
37 #include "bsf.h"
38 #include "codec_internal.h"
39 #include "decode.h"
40 #include "encode.h"
41 #include "frame_thread_encoder.h"
42 #include "internal.h"
43 #include "thread.h"
44
45 48042 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
46 {
47 int i;
48
49
2/2
✓ Branch 0 taken 1345024 times.
✓ Branch 1 taken 48042 times.
1393066 for (i = 0; i < count; i++) {
50 1345024 int r = func(c, (char *)arg + i * size);
51
2/2
✓ Branch 0 taken 28407 times.
✓ Branch 1 taken 1316617 times.
1345024 if (ret)
52 28407 ret[i] = r;
53 }
54 48042 emms_c();
55 48042 return 0;
56 }
57
58 7034 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
59 {
60 int i;
61
62
2/2
✓ Branch 0 taken 243892 times.
✓ Branch 1 taken 7034 times.
250926 for (i = 0; i < count; i++) {
63 243892 int r = func(c, arg, i, 0);
64
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 243842 times.
243892 if (ret)
65 50 ret[i] = r;
66 }
67 7034 emms_c();
68 7034 return 0;
69 }
70
71 static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
72
73 56283 static void lock_avcodec(const FFCodec *codec)
74 {
75
3/4
✓ Branch 0 taken 5204 times.
✓ Branch 1 taken 51079 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5204 times.
56283 if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
76 ff_mutex_lock(&codec_mutex);
77 56283 }
78
79 56283 static void unlock_avcodec(const FFCodec *codec)
80 {
81
3/4
✓ Branch 0 taken 5204 times.
✓ Branch 1 taken 51079 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5204 times.
56283 if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
82 ff_mutex_unlock(&codec_mutex);
83 56283 }
84
85 22485 static int64_t get_bit_rate(AVCodecContext *ctx)
86 {
87 int64_t bit_rate;
88 int bits_per_sample;
89
90
2/3
✓ Branch 0 taken 18720 times.
✓ Branch 1 taken 3765 times.
✗ Branch 2 not taken.
22485 switch (ctx->codec_type) {
91 18720 case AVMEDIA_TYPE_VIDEO:
92 case AVMEDIA_TYPE_DATA:
93 case AVMEDIA_TYPE_SUBTITLE:
94 case AVMEDIA_TYPE_ATTACHMENT:
95 18720 bit_rate = ctx->bit_rate;
96 18720 break;
97 3765 case AVMEDIA_TYPE_AUDIO:
98 3765 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
99
2/2
✓ Branch 0 taken 2212 times.
✓ Branch 1 taken 1553 times.
3765 if (bits_per_sample) {
100 2212 bit_rate = ctx->sample_rate * (int64_t)ctx->ch_layout.nb_channels;
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2212 times.
2212 if (bit_rate > INT64_MAX / bits_per_sample) {
102 bit_rate = 0;
103 } else
104 2212 bit_rate *= bits_per_sample;
105 } else
106 1553 bit_rate = ctx->bit_rate;
107 3765 break;
108 default:
109 bit_rate = 0;
110 break;
111 }
112 22485 return bit_rate;
113 }
114
115 31568 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
116 {
117 31568 int ret = 0;
118 AVCodecInternal *avci;
119 const FFCodec *codec2;
120
121
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 31568 times.
31568 if (avcodec_is_open(avctx))
122 return 0;
123
124
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 31564 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
31568 if (!codec && !avctx->codec) {
125 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
126 return AVERROR(EINVAL);
127 }
128
5/6
✓ Branch 0 taken 31564 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 24431 times.
✓ Branch 3 taken 7133 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24431 times.
31568 if (codec && avctx->codec && codec != avctx->codec) {
129 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
130 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
131 return AVERROR(EINVAL);
132 }
133
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 31564 times.
31568 if (!codec)
134 4 codec = avctx->codec;
135 31568 codec2 = ffcodec(codec);
136
137
3/4
✓ Branch 0 taken 31564 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 31564 times.
✗ Branch 3 not taken.
31568 if ((avctx->codec_type != AVMEDIA_TYPE_UNKNOWN && avctx->codec_type != codec->type) ||
138
3/4
✓ Branch 0 taken 31564 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 31564 times.
31568 (avctx->codec_id != AV_CODEC_ID_NONE && avctx->codec_id != codec->id)) {
139 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
140 return AVERROR(EINVAL);
141 }
142
143 31568 avctx->codec_type = codec->type;
144 31568 avctx->codec_id = codec->id;
145 31568 avctx->codec = codec;
146
147
2/4
✓ Branch 0 taken 31568 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 31568 times.
31568 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
148 return AVERROR(EINVAL);
149
150 31568 avci = av_mallocz(sizeof(*avci));
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31568 times.
31568 if (!avci) {
152 ret = AVERROR(ENOMEM);
153 goto end;
154 }
155 31568 avctx->internal = avci;
156
157 31568 avci->buffer_frame = av_frame_alloc();
158 31568 avci->buffer_pkt = av_packet_alloc();
159
2/4
✓ Branch 0 taken 31568 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 31568 times.
31568 if (!avci->buffer_frame || !avci->buffer_pkt) {
160 ret = AVERROR(ENOMEM);
161 goto free_and_end;
162 }
163
164 31568 avci->skip_samples_multiplier = 1;
165
166
2/2
✓ Branch 0 taken 13962 times.
✓ Branch 1 taken 17606 times.
31568 if (codec2->priv_data_size > 0) {
167
2/2
✓ Branch 0 taken 7016 times.
✓ Branch 1 taken 6946 times.
13962 if (!avctx->priv_data) {
168 7016 avctx->priv_data = av_mallocz(codec2->priv_data_size);
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7016 times.
7016 if (!avctx->priv_data) {
170 ret = AVERROR(ENOMEM);
171 goto free_and_end;
172 }
173
2/2
✓ Branch 0 taken 1825 times.
✓ Branch 1 taken 5191 times.
7016 if (codec->priv_class) {
174 1825 *(const AVClass **)avctx->priv_data = codec->priv_class;
175 1825 av_opt_set_defaults(avctx->priv_data);
176 }
177 }
178
3/4
✓ Branch 0 taken 4122 times.
✓ Branch 1 taken 9840 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4122 times.
13962 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, options)) < 0)
179 goto free_and_end;
180 } else {
181 17606 avctx->priv_data = NULL;
182 }
183
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 31568 times.
31568 if ((ret = av_opt_set_dict(avctx, options)) < 0)
184 goto free_and_end;
185
186
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 31568 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
31568 if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
187 av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
188 ret = AVERROR(EINVAL);
189 goto free_and_end;
190 }
191
192 // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
193
6/8
✓ Branch 0 taken 12131 times.
✓ Branch 1 taken 19437 times.
✓ Branch 2 taken 12131 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12122 times.
✓ Branch 5 taken 9 times.
✓ Branch 6 taken 12122 times.
✗ Branch 7 not taken.
31568 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
194
3/6
✓ Branch 0 taken 12122 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12122 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12122 times.
✗ Branch 5 not taken.
12122 (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
195
3/4
✓ Branch 0 taken 12131 times.
✓ Branch 1 taken 19437 times.
✓ Branch 2 taken 12131 times.
✗ Branch 3 not taken.
31568 if (avctx->coded_width && avctx->coded_height)
196 12131 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
197
3/4
✓ Branch 0 taken 11677 times.
✓ Branch 1 taken 7760 times.
✓ Branch 2 taken 11677 times.
✗ Branch 3 not taken.
19437 else if (avctx->width && avctx->height)
198 11677 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31568 times.
31568 if (ret < 0)
200 goto free_and_end;
201 }
202
203
5/8
✓ Branch 0 taken 7760 times.
✓ Branch 1 taken 23808 times.
✓ Branch 2 taken 7760 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7760 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 7760 times.
31568 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
204
1/2
✓ Branch 1 taken 23808 times.
✗ Branch 2 not taken.
23808 && ( av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
205
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 23808 times.
23808 || av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
206 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
207 ff_set_dimensions(avctx, 0, 0);
208 }
209
210
3/4
✓ Branch 0 taken 23808 times.
✓ Branch 1 taken 7760 times.
✓ Branch 2 taken 23808 times.
✗ Branch 3 not taken.
31568 if (avctx->width > 0 && avctx->height > 0) {
211
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 23808 times.
23808 if (av_image_check_sar(avctx->width, avctx->height,
212 avctx->sample_aspect_ratio) < 0) {
213 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
214 avctx->sample_aspect_ratio.num,
215 avctx->sample_aspect_ratio.den);
216 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
217 }
218 }
219
220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31568 times.
31568 if (avctx->sample_rate < 0) {
221 av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate);
222 ret = AVERROR(EINVAL);
223 goto free_and_end;
224 }
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31568 times.
31568 if (avctx->block_align < 0) {
226 av_log(avctx, AV_LOG_ERROR, "Invalid block align: %d\n", avctx->block_align);
227 ret = AVERROR(EINVAL);
228 goto free_and_end;
229 }
230
231 #if FF_API_OLD_CHANNEL_LAYOUT
232 FF_DISABLE_DEPRECATION_WARNINGS
233 /* compat wrapper for old-style callers */
234
3/4
✓ Branch 0 taken 866 times.
✓ Branch 1 taken 30702 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 866 times.
31568 if (avctx->channel_layout && !avctx->channels)
235 avctx->channels = av_popcount64(avctx->channel_layout);
236
237
4/4
✓ Branch 0 taken 2717 times.
✓ Branch 1 taken 28851 times.
✓ Branch 2 taken 2714 times.
✓ Branch 3 taken 3 times.
31568 if ((avctx->channels > 0 && avctx->ch_layout.nb_channels != avctx->channels) ||
238
3/4
✓ Branch 0 taken 866 times.
✓ Branch 1 taken 30699 times.
✓ Branch 2 taken 866 times.
✗ Branch 3 not taken.
31565 (avctx->channel_layout && (avctx->ch_layout.order != AV_CHANNEL_ORDER_NATIVE ||
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 866 times.
866 avctx->ch_layout.u.mask != avctx->channel_layout))) {
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (avctx->channel_layout) {
241 av_channel_layout_from_mask(&avctx->ch_layout, avctx->channel_layout);
242 } else {
243 3 avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
244 3 avctx->ch_layout.nb_channels = avctx->channels;
245 }
246 }
247 FF_ENABLE_DEPRECATION_WARNINGS
248 #endif
249
250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31568 times.
31568 if (avctx->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) {
251 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d\n", avctx->ch_layout.nb_channels);
252 ret = AVERROR(EINVAL);
253 goto free_and_end;
254 }
255
256 31568 avctx->frame_number = 0;
257 31568 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
258
259
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 31561 times.
31568 if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
261 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
262 const AVCodec *codec2;
263 av_log(avctx, AV_LOG_ERROR,
264 "The %s '%s' is experimental but experimental codecs are not enabled, "
265 "add '-strict %d' if you want to use it.\n",
266 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
267 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
268 if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
269 av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
270 codec_string, codec2->name);
271 ret = AVERROR_EXPERIMENTAL;
272 goto free_and_end;
273 }
274
275
2/2
✓ Branch 0 taken 4097 times.
✓ Branch 1 taken 27471 times.
31568 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
276
4/4
✓ Branch 0 taken 1161 times.
✓ Branch 1 taken 2936 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 1150 times.
4097 (!avctx->time_base.num || !avctx->time_base.den)) {
277 2947 avctx->time_base.num = 1;
278 2947 avctx->time_base.den = avctx->sample_rate;
279 }
280
281
2/2
✓ Branch 1 taken 18185 times.
✓ Branch 2 taken 13383 times.
31568 if (av_codec_is_encoder(avctx->codec))
282 18185 ret = ff_encode_preinit(avctx);
283 else
284 13383 ret = ff_decode_preinit(avctx);
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31568 times.
31568 if (ret < 0)
286 goto free_and_end;
287
288
2/2
✓ Branch 1 taken 18185 times.
✓ Branch 2 taken 13383 times.
31568 if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
289 18185 ret = ff_frame_thread_encoder_init(avctx);
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18185 times.
18185 if (ret < 0)
291 goto free_and_end;
292 }
293
294 31568 if (HAVE_THREADS
295
3/4
✓ Branch 0 taken 1516 times.
✓ Branch 1 taken 30052 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1516 times.
31568 && !(avci->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
296 /* Frame-threaded decoders call FFCodec.init for their child contexts. */
297 30052 lock_avcodec(codec2);
298 30052 ret = ff_thread_init(avctx);
299 30052 unlock_avcodec(codec2);
300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30052 times.
30052 if (ret < 0) {
301 goto free_and_end;
302 }
303 }
304 if (!HAVE_THREADS && !(codec2->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
305 avctx->thread_count = 1;
306
307
2/2
✓ Branch 0 taken 1524 times.
✓ Branch 1 taken 30044 times.
31568 if (!(avctx->active_thread_type & FF_THREAD_FRAME) ||
308
2/2
✓ Branch 0 taken 1516 times.
✓ Branch 1 taken 8 times.
1524 avci->frame_thread_encoder) {
309
2/2
✓ Branch 0 taken 26231 times.
✓ Branch 1 taken 5329 times.
31560 if (codec2->init) {
310 26231 lock_avcodec(codec2);
311 26231 ret = codec2->init(avctx);
312 26231 unlock_avcodec(codec2);
313
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 26217 times.
26231 if (ret < 0) {
314 14 avci->needs_close = codec2->caps_internal & FF_CODEC_CAP_INIT_CLEANUP;
315 14 goto free_and_end;
316 }
317 }
318 31546 avci->needs_close = 1;
319 }
320
321 31554 ret=0;
322
323
2/2
✓ Branch 1 taken 13369 times.
✓ Branch 2 taken 18185 times.
31554 if (av_codec_is_decoder(avctx->codec)) {
324
2/2
✓ Branch 0 taken 9072 times.
✓ Branch 1 taken 4297 times.
13369 if (!avctx->bit_rate)
325 9072 avctx->bit_rate = get_bit_rate(avctx);
326
327 #if FF_API_OLD_CHANNEL_LAYOUT
328 FF_DISABLE_DEPRECATION_WARNINGS
329 /* update the deprecated fields for old-style callers */
330 13369 avctx->channels = avctx->ch_layout.nb_channels;
331 26738 avctx->channel_layout = avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
332
2/2
✓ Branch 0 taken 1790 times.
✓ Branch 1 taken 11579 times.
13369 avctx->ch_layout.u.mask : 0;
333
334 /* validate channel layout from the decoder */
335
2/2
✓ Branch 0 taken 1790 times.
✓ Branch 1 taken 11579 times.
13369 if (avctx->channel_layout) {
336 1790 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1790 times.
1790 if (!avctx->channels)
338 avctx->channels = channels;
339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1790 times.
1790 else if (channels != avctx->channels) {
340 char buf[512];
341 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
342 av_log(avctx, AV_LOG_WARNING,
343 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
344 "ignoring specified channel layout\n",
345 buf, channels, avctx->channels);
346 avctx->channel_layout = 0;
347 }
348 }
349
3/4
✓ Branch 0 taken 2780 times.
✓ Branch 1 taken 10589 times.
✓ Branch 2 taken 2780 times.
✗ Branch 3 not taken.
13369 if (avctx->channels && avctx->channels < 0 ||
350
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13369 times.
13369 avctx->channels > FF_SANE_NB_CHANNELS) {
351 ret = AVERROR(EINVAL);
352 goto free_and_end;
353 }
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13369 times.
13369 if (avctx->bits_per_coded_sample < 0) {
355 ret = AVERROR(EINVAL);
356 goto free_and_end;
357 }
358 FF_ENABLE_DEPRECATION_WARNINGS
359 #endif
360
361 #if FF_API_AVCTX_TIMEBASE
362
3/4
✓ Branch 0 taken 4774 times.
✓ Branch 1 taken 8595 times.
✓ Branch 2 taken 4774 times.
✗ Branch 3 not taken.
13369 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
363 4774 avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
364 #endif
365 }
366
2/2
✓ Branch 0 taken 4122 times.
✓ Branch 1 taken 27432 times.
31554 if (codec->priv_class)
367
1/2
✓ Branch 0 taken 4122 times.
✗ Branch 1 not taken.
4122 av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
368
369 31554 end:
370
371 31568 return ret;
372 14 free_and_end:
373 14 avcodec_close(avctx);
374 14 goto end;
375 }
376
377 89 void avcodec_flush_buffers(AVCodecContext *avctx)
378 {
379 89 AVCodecInternal *avci = avctx->internal;
380
381
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 89 times.
89 if (av_codec_is_encoder(avctx->codec)) {
382 int caps = avctx->codec->capabilities;
383
384 if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) {
385 // Only encoders that explicitly declare support for it can be
386 // flushed. Otherwise, this is a no-op.
387 av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder "
388 "that doesn't support it\n");
389 return;
390 }
391 if (avci->in_frame)
392 av_frame_unref(avci->in_frame);
393 } else {
394 89 av_packet_unref(avci->last_pkt_props);
395
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 89 times.
89 while (av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1) >= 0)
396 av_packet_unref(avci->last_pkt_props);
397
398 89 av_packet_unref(avci->in_pkt);
399
400 89 avctx->pts_correction_last_pts =
401 89 avctx->pts_correction_last_dts = INT64_MIN;
402
403 89 av_bsf_flush(avci->bsf);
404 }
405
406 89 avci->draining = 0;
407 89 avci->draining_done = 0;
408 89 avci->nb_draining_errors = 0;
409 89 av_frame_unref(avci->buffer_frame);
410 89 av_packet_unref(avci->buffer_pkt);
411
412
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89 times.
89 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
413 ff_thread_flush(avctx);
414
2/2
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 71 times.
89 else if (ffcodec(avctx->codec)->flush)
415 18 ffcodec(avctx->codec)->flush(avctx);
416 }
417
418 7600 void avsubtitle_free(AVSubtitle *sub)
419 {
420 int i;
421
422
2/2
✓ Branch 0 taken 851 times.
✓ Branch 1 taken 7600 times.
8451 for (i = 0; i < sub->num_rects; i++) {
423 851 AVSubtitleRect *const rect = sub->rects[i];
424
425 851 av_freep(&rect->data[0]);
426 851 av_freep(&rect->data[1]);
427 851 av_freep(&rect->data[2]);
428 851 av_freep(&rect->data[3]);
429 851 av_freep(&rect->text);
430 851 av_freep(&rect->ass);
431
432 851 av_freep(&sub->rects[i]);
433 }
434
435 7600 av_freep(&sub->rects);
436
437 7600 memset(sub, 0, sizeof(*sub));
438 7600 }
439
440 65941 av_cold int avcodec_close(AVCodecContext *avctx)
441 {
442 int i;
443
444
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65941 times.
65941 if (!avctx)
445 return 0;
446
447
2/2
✓ Branch 1 taken 31568 times.
✓ Branch 2 taken 34373 times.
65941 if (avcodec_is_open(avctx)) {
448 31568 AVCodecInternal *avci = avctx->internal;
449
450 31568 if (CONFIG_FRAME_THREAD_ENCODER &&
451
4/4
✓ Branch 0 taken 13632 times.
✓ Branch 1 taken 17936 times.
✓ Branch 2 taken 1516 times.
✓ Branch 3 taken 12116 times.
31568 avci->frame_thread_encoder && avctx->thread_count > 1) {
452 1516 ff_frame_thread_encoder_free(avctx);
453 }
454
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 31543 times.
31568 if (HAVE_THREADS && avci->thread_ctx)
455 25 ff_thread_free(avctx);
456
4/4
✓ Branch 0 taken 31552 times.
✓ Branch 1 taken 16 times.
✓ Branch 3 taken 6428 times.
✓ Branch 4 taken 25124 times.
31568 if (avci->needs_close && ffcodec(avctx->codec)->close)
457 6428 ffcodec(avctx->codec)->close(avctx);
458 31568 avci->byte_buffer_size = 0;
459 31568 av_freep(&avci->byte_buffer);
460 31568 av_frame_free(&avci->buffer_frame);
461 31568 av_packet_free(&avci->buffer_pkt);
462
2/2
✓ Branch 0 taken 13383 times.
✓ Branch 1 taken 18185 times.
31568 if (avci->pkt_props) {
463
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13383 times.
13383 while (av_fifo_can_read(avci->pkt_props)) {
464 av_packet_unref(avci->last_pkt_props);
465 av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1);
466 }
467 13383 av_fifo_freep2(&avci->pkt_props);
468 }
469 31568 av_packet_free(&avci->last_pkt_props);
470
471 31568 av_packet_free(&avci->in_pkt);
472 31568 av_frame_free(&avci->in_frame);
473
474 31568 av_buffer_unref(&avci->pool);
475
476
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 31568 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
31568 if (avctx->hwaccel && avctx->hwaccel->uninit)
477 avctx->hwaccel->uninit(avctx);
478 31568 av_freep(&avci->hwaccel_priv_data);
479
480 31568 av_bsf_free(&avci->bsf);
481
482 31568 av_channel_layout_uninit(&avci->initial_ch_layout);
483
484 31568 av_freep(&avctx->internal);
485 }
486
487
2/2
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 65941 times.
66341 for (i = 0; i < avctx->nb_coded_side_data; i++)
488 400 av_freep(&avctx->coded_side_data[i].data);
489 65941 av_freep(&avctx->coded_side_data);
490 65941 avctx->nb_coded_side_data = 0;
491
492 65941 av_buffer_unref(&avctx->hw_frames_ctx);
493 65941 av_buffer_unref(&avctx->hw_device_ctx);
494
495
5/6
✓ Branch 0 taken 14570 times.
✓ Branch 1 taken 51371 times.
✓ Branch 2 taken 14570 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4340 times.
✓ Branch 5 taken 10230 times.
65941 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
496 4340 av_opt_free(avctx->priv_data);
497 65941 av_opt_free(avctx);
498 65941 av_freep(&avctx->priv_data);
499
2/2
✓ Branch 1 taken 18185 times.
✓ Branch 2 taken 47756 times.
65941 if (av_codec_is_encoder(avctx->codec)) {
500 18185 av_freep(&avctx->extradata);
501 18185 avctx->extradata_size = 0;
502
2/2
✓ Branch 1 taken 14001 times.
✓ Branch 2 taken 33755 times.
47756 } else if (av_codec_is_decoder(avctx->codec))
503 14001 av_freep(&avctx->subtitle_header);
504
505 65941 avctx->codec = NULL;
506 65941 avctx->active_thread_type = 0;
507
508 65941 return 0;
509 }
510
511 15212 static const char *unknown_if_null(const char *str)
512 {
513
1/2
✓ Branch 0 taken 15212 times.
✗ Branch 1 not taken.
15212 return str ? str : "unknown";
514 }
515
516 13416 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
517 {
518 const char *codec_type;
519 const char *codec_name;
520 13416 const char *profile = NULL;
521 AVBPrint bprint;
522 int64_t bitrate;
523 13416 int new_line = 0;
524 AVRational display_aspect_ratio;
525
2/2
✓ Branch 0 taken 13373 times.
✓ Branch 1 taken 43 times.
13416 const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
526 const char *str;
527
528
2/4
✓ Branch 0 taken 13416 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13416 times.
13416 if (!buf || buf_size <= 0)
529 3 return;
530 13416 av_bprint_init_for_buffer(&bprint, buf, buf_size);
531 13416 codec_type = av_get_media_type_string(enc->codec_type);
532 13416 codec_name = avcodec_get_name(enc->codec_id);
533 13416 profile = avcodec_profile_name(enc->codec_id, enc->profile);
534
535
1/2
✓ Branch 0 taken 13416 times.
✗ Branch 1 not taken.
13416 av_bprintf(&bprint, "%s: %s", codec_type ? codec_type : "unknown",
536 codec_name);
537 13416 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
538
539
4/4
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 13378 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 36 times.
13416 if (enc->codec && strcmp(enc->codec->name, codec_name))
540 2 av_bprintf(&bprint, " (%s)", enc->codec->name);
541
542
2/2
✓ Branch 0 taken 1645 times.
✓ Branch 1 taken 11771 times.
13416 if (profile)
543 1645 av_bprintf(&bprint, " (%s)", profile);
544
2/2
✓ Branch 0 taken 10319 times.
✓ Branch 1 taken 3097 times.
13416 if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
545
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10319 times.
10319 && av_log_get_level() >= AV_LOG_VERBOSE
546 && enc->refs)
547 av_bprintf(&bprint, ", %d reference frame%s",
548 enc->refs, enc->refs > 1 ? "s" : "");
549
550
2/2
✓ Branch 0 taken 7668 times.
✓ Branch 1 taken 5748 times.
13416 if (enc->codec_tag)
551 7668 av_bprintf(&bprint, " (%s / 0x%04X)",
552 7668 av_fourcc2str(enc->codec_tag), enc->codec_tag);
553
554
5/5
✓ Branch 0 taken 10319 times.
✓ Branch 1 taken 2874 times.
✓ Branch 2 taken 83 times.
✓ Branch 3 taken 137 times.
✓ Branch 4 taken 3 times.
13416 switch (enc->codec_type) {
555 10319 case AVMEDIA_TYPE_VIDEO:
556 {
557 unsigned len;
558
559 10319 av_bprintf(&bprint, "%s%s", separator,
560
2/2
✓ Branch 0 taken 10232 times.
✓ Branch 1 taken 87 times.
10319 enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
561 10232 unknown_if_null(av_get_pix_fmt_name(enc->pix_fmt)));
562
563 10319 av_bprint_chars(&bprint, '(', 1);
564 10319 len = bprint.len;
565
566 /* The following check ensures that '(' has been written
567 * and therefore allows us to erase it if it turns out
568 * to be unnecessary. */
569
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10319 times.
10319 if (!av_bprint_is_complete(&bprint))
570 return;
571
572
3/4
✓ Branch 0 taken 3465 times.
✓ Branch 1 taken 6854 times.
✓ Branch 2 taken 3465 times.
✗ Branch 3 not taken.
10319 if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
573
2/2
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 3450 times.
3465 enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
574 15 av_bprintf(&bprint, "%d bpc, ", enc->bits_per_raw_sample);
575
3/4
✓ Branch 0 taken 4758 times.
✓ Branch 1 taken 5561 times.
✓ Branch 2 taken 4758 times.
✗ Branch 3 not taken.
15077 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED &&
576 4758 (str = av_color_range_name(enc->color_range)))
577 4758 av_bprintf(&bprint, "%s, ", str);
578
579
2/2
✓ Branch 0 taken 8677 times.
✓ Branch 1 taken 1642 times.
10319 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
580
1/2
✓ Branch 0 taken 8677 times.
✗ Branch 1 not taken.
8677 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
581
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 8659 times.
8677 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
582 1660 const char *col = unknown_if_null(av_color_space_name(enc->colorspace));
583 1660 const char *pri = unknown_if_null(av_color_primaries_name(enc->color_primaries));
584 1660 const char *trc = unknown_if_null(av_color_transfer_name(enc->color_trc));
585
4/4
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 1498 times.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 135 times.
1660 if (strcmp(col, pri) || strcmp(col, trc)) {
586 1525 new_line = 1;
587 1525 av_bprintf(&bprint, "%s/%s/%s, ", col, pri, trc);
588 } else
589 135 av_bprintf(&bprint, "%s, ", col);
590 }
591
592
2/2
✓ Branch 0 taken 5469 times.
✓ Branch 1 taken 4850 times.
10319 if (enc->field_order != AV_FIELD_UNKNOWN) {
593 5469 const char *field_order = "progressive";
594
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 5351 times.
5469 if (enc->field_order == AV_FIELD_TT)
595 118 field_order = "top first";
596
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 5311 times.
5351 else if (enc->field_order == AV_FIELD_BB)
597 40 field_order = "bottom first";
598
2/2
✓ Branch 0 taken 328 times.
✓ Branch 1 taken 4983 times.
5311 else if (enc->field_order == AV_FIELD_TB)
599 328 field_order = "top coded first (swapped)";
600
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 4911 times.
4983 else if (enc->field_order == AV_FIELD_BT)
601 72 field_order = "bottom coded first (swapped)";
602
603 5469 av_bprintf(&bprint, "%s, ", field_order);
604 }
605
606
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10319 times.
10319 if (av_log_get_level() >= AV_LOG_VERBOSE &&
607 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED &&
608 (str = av_chroma_location_name(enc->chroma_sample_location)))
609 av_bprintf(&bprint, "%s, ", str);
610
611
2/2
✓ Branch 0 taken 3944 times.
✓ Branch 1 taken 6375 times.
10319 if (len == bprint.len) {
612 3944 bprint.str[len - 1] = '\0';
613 3944 bprint.len--;
614 } else {
615
1/2
✓ Branch 0 taken 6375 times.
✗ Branch 1 not taken.
6375 if (bprint.len - 2 < bprint.size) {
616 /* Erase the last ", " */
617 6375 bprint.len -= 2;
618 6375 bprint.str[bprint.len] = '\0';
619 }
620 6375 av_bprint_chars(&bprint, ')', 1);
621 }
622 }
623
624
2/2
✓ Branch 0 taken 10303 times.
✓ Branch 1 taken 16 times.
10319 if (enc->width) {
625
2/2
✓ Branch 0 taken 1523 times.
✓ Branch 1 taken 8780 times.
10303 av_bprintf(&bprint, "%s%dx%d", new_line ? separator : ", ",
626 enc->width, enc->height);
627
628
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10303 times.
10303 if (av_log_get_level() >= AV_LOG_VERBOSE &&
629 (enc->width != enc->coded_width ||
630 enc->height != enc->coded_height))
631 av_bprintf(&bprint, " (%dx%d)",
632 enc->coded_width, enc->coded_height);
633
634
2/2
✓ Branch 0 taken 1346 times.
✓ Branch 1 taken 8957 times.
10303 if (enc->sample_aspect_ratio.num) {
635 1346 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
636 1346 enc->width * (int64_t)enc->sample_aspect_ratio.num,
637 1346 enc->height * (int64_t)enc->sample_aspect_ratio.den,
638 1024 * 1024);
639 1346 av_bprintf(&bprint, " [SAR %d:%d DAR %d:%d]",
640 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
641 display_aspect_ratio.num, display_aspect_ratio.den);
642 }
643
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10303 times.
10303 if (av_log_get_level() >= AV_LOG_DEBUG) {
644 int g = av_gcd(enc->time_base.num, enc->time_base.den);
645 av_bprintf(&bprint, ", %d/%d",
646 enc->time_base.num / g, enc->time_base.den / g);
647 }
648 }
649
2/2
✓ Branch 0 taken 5089 times.
✓ Branch 1 taken 5230 times.
10319 if (encode) {
650 5089 av_bprintf(&bprint, ", q=%d-%d", enc->qmin, enc->qmax);
651 } else {
652
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5228 times.
5230 if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
653 2 av_bprintf(&bprint, ", Closed Captions");
654
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5226 times.
5230 if (enc->properties & FF_CODEC_PROPERTY_FILM_GRAIN)
655 4 av_bprintf(&bprint, ", Film Grain");
656
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 5207 times.
5230 if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
657 23 av_bprintf(&bprint, ", lossless");
658 }
659 10319 break;
660 2874 case AVMEDIA_TYPE_AUDIO:
661 2874 av_bprintf(&bprint, "%s", separator);
662
663
2/2
✓ Branch 0 taken 2862 times.
✓ Branch 1 taken 12 times.
2874 if (enc->sample_rate) {
664 2862 av_bprintf(&bprint, "%d Hz, ", enc->sample_rate);
665 }
666 {
667 char buf[512];
668 2874 int ret = av_channel_layout_describe(&enc->ch_layout, buf, sizeof(buf));
669
1/2
✓ Branch 0 taken 2874 times.
✗ Branch 1 not taken.
2874 if (ret >= 0)
670 2874 av_bprintf(&bprint, "%s", buf);
671 }
672
3/4
✓ Branch 0 taken 2866 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2866 times.
✗ Branch 3 not taken.
5740 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE &&
673 2866 (str = av_get_sample_fmt_name(enc->sample_fmt))) {
674 2866 av_bprintf(&bprint, ", %s", str);
675 }
676
2/2
✓ Branch 0 taken 1364 times.
✓ Branch 1 taken 1510 times.
2874 if ( enc->bits_per_raw_sample > 0
677
2/2
✓ Branch 1 taken 93 times.
✓ Branch 2 taken 1271 times.
1364 && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
678 93 av_bprintf(&bprint, " (%d bit)", enc->bits_per_raw_sample);
679
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2870 times.
2874 if (av_log_get_level() >= AV_LOG_VERBOSE) {
680
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (enc->initial_padding)
681 2 av_bprintf(&bprint, ", delay %d", enc->initial_padding);
682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (enc->trailing_padding)
683 av_bprintf(&bprint, ", padding %d", enc->trailing_padding);
684 }
685 2874 break;
686 83 case AVMEDIA_TYPE_DATA:
687
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
83 if (av_log_get_level() >= AV_LOG_DEBUG) {
688 int g = av_gcd(enc->time_base.num, enc->time_base.den);
689 if (g)
690 av_bprintf(&bprint, ", %d/%d",
691 enc->time_base.num / g, enc->time_base.den / g);
692 }
693 83 break;
694 137 case AVMEDIA_TYPE_SUBTITLE:
695
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 112 times.
137 if (enc->width)
696 25 av_bprintf(&bprint, ", %dx%d", enc->width, enc->height);
697 137 break;
698 3 default:
699 3 return;
700 }
701
2/2
✓ Branch 0 taken 6478 times.
✓ Branch 1 taken 6935 times.
13413 if (encode) {
702
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6478 times.
6478 if (enc->flags & AV_CODEC_FLAG_PASS1)
703 av_bprintf(&bprint, ", pass 1");
704
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6478 times.
6478 if (enc->flags & AV_CODEC_FLAG_PASS2)
705 av_bprintf(&bprint, ", pass 2");
706 }
707 13413 bitrate = get_bit_rate(enc);
708
2/2
✓ Branch 0 taken 8662 times.
✓ Branch 1 taken 4751 times.
13413 if (bitrate != 0) {
709 8662 av_bprintf(&bprint, ", %"PRId64" kb/s", bitrate / 1000);
710
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4751 times.
4751 } else if (enc->rc_max_rate > 0) {
711 av_bprintf(&bprint, ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
712 }
713 }
714
715 2663279 int avcodec_is_open(AVCodecContext *s)
716 {
717 2663279 return !!s->internal;
718 }
719