FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/avcodec.c
Date: 2024-04-27 00:58:15
Exec Total Coverage
Lines: 323 410 78.8%
Functions: 13 14 92.9%
Branches: 256 356 71.9%

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/emms.h"
32 #include "libavutil/fifo.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/mem.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/thread.h"
37 #include "avcodec.h"
38 #include "avcodec_internal.h"
39 #include "bsf.h"
40 #include "codec_desc.h"
41 #include "codec_internal.h"
42 #include "decode.h"
43 #include "encode.h"
44 #include "frame_thread_encoder.h"
45 #include "hwconfig.h"
46 #include "internal.h"
47 #include "refstruct.h"
48 #include "thread.h"
49
50 /**
51 * Maximum size in bytes of extradata.
52 * This value was chosen such that every bit of the buffer is
53 * addressable by a 32-bit signed integer as used by get_bits.
54 */
55 #define FF_MAX_EXTRADATA_SIZE ((1 << 28) - AV_INPUT_BUFFER_PADDING_SIZE)
56
57 const SideDataMap ff_sd_global_map[] = {
58 { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
59 { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
60 { AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL },
61 { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
62 { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
63 { AV_PKT_DATA_MASTERING_DISPLAY_METADATA, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA },
64 { AV_PKT_DATA_CONTENT_LIGHT_LEVEL, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL },
65 { AV_PKT_DATA_ICC_PROFILE, AV_FRAME_DATA_ICC_PROFILE },
66 { AV_PKT_DATA_AMBIENT_VIEWING_ENVIRONMENT,AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT },
67 { AV_PKT_DATA_NB },
68 };
69
70
71 51284 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
72 {
73 size_t i;
74
75
2/2
✓ Branch 0 taken 1410333 times.
✓ Branch 1 taken 51284 times.
1461617 for (i = 0; i < count; i++) {
76 1410333 size_t offset = i * size;
77
2/2
✓ Branch 0 taken 1359049 times.
✓ Branch 1 taken 51284 times.
1410333 int r = func(c, FF_PTR_ADD((char *)arg, offset));
78
2/2
✓ Branch 0 taken 28454 times.
✓ Branch 1 taken 1381879 times.
1410333 if (ret)
79 28454 ret[i] = r;
80 }
81 51284 emms_c();
82 51284 return 0;
83 }
84
85 7462 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
86 {
87 int i;
88
89
2/2
✓ Branch 0 taken 241461 times.
✓ Branch 1 taken 7462 times.
248923 for (i = 0; i < count; i++) {
90 241461 int r = func(c, arg, i, 0);
91
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 241411 times.
241461 if (ret)
92 50 ret[i] = r;
93 }
94 7462 emms_c();
95 7462 return 0;
96 }
97
98 static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
99
100 60101 static void lock_avcodec(const FFCodec *codec)
101 {
102
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 60101 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
60101 if (codec->caps_internal & FF_CODEC_CAP_NOT_INIT_THREADSAFE && codec->init)
103 ff_mutex_lock(&codec_mutex);
104 60101 }
105
106 60101 static void unlock_avcodec(const FFCodec *codec)
107 {
108
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 60101 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
60101 if (codec->caps_internal & FF_CODEC_CAP_NOT_INIT_THREADSAFE && codec->init)
109 ff_mutex_unlock(&codec_mutex);
110 60101 }
111
112 24831 static int64_t get_bit_rate(AVCodecContext *ctx)
113 {
114 int64_t bit_rate;
115 int bits_per_sample;
116
117
2/3
✓ Branch 0 taken 20266 times.
✓ Branch 1 taken 4565 times.
✗ Branch 2 not taken.
24831 switch (ctx->codec_type) {
118 20266 case AVMEDIA_TYPE_VIDEO:
119 case AVMEDIA_TYPE_DATA:
120 case AVMEDIA_TYPE_SUBTITLE:
121 case AVMEDIA_TYPE_ATTACHMENT:
122 20266 bit_rate = ctx->bit_rate;
123 20266 break;
124 4565 case AVMEDIA_TYPE_AUDIO:
125 4565 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
126
2/2
✓ Branch 0 taken 2353 times.
✓ Branch 1 taken 2212 times.
4565 if (bits_per_sample) {
127 2353 bit_rate = ctx->sample_rate * (int64_t)ctx->ch_layout.nb_channels;
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2353 times.
2353 if (bit_rate > INT64_MAX / bits_per_sample) {
129 bit_rate = 0;
130 } else
131 2353 bit_rate *= bits_per_sample;
132 } else
133 2212 bit_rate = ctx->bit_rate;
134 4565 break;
135 default:
136 bit_rate = 0;
137 break;
138 }
139 24831 return bit_rate;
140 }
141
142 33792 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
143 {
144 33792 int ret = 0;
145 AVCodecInternal *avci;
146 const FFCodec *codec2;
147
148
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33792 times.
33792 if (avcodec_is_open(avctx))
149 return 0;
150
151
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 33788 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
33792 if (!codec && !avctx->codec) {
152 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
153 return AVERROR(EINVAL);
154 }
155
6/6
✓ Branch 0 taken 33788 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 25968 times.
✓ Branch 3 taken 7820 times.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 25958 times.
33792 if (codec && avctx->codec && codec != avctx->codec) {
156 10 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
157 10 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
158 10 return AVERROR(EINVAL);
159 }
160
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 33778 times.
33782 if (!codec)
161 4 codec = avctx->codec;
162 33782 codec2 = ffcodec(codec);
163
164
3/4
✓ Branch 0 taken 33778 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 33778 times.
✗ Branch 3 not taken.
33782 if ((avctx->codec_type != AVMEDIA_TYPE_UNKNOWN && avctx->codec_type != codec->type) ||
165
3/4
✓ Branch 0 taken 33778 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 33778 times.
33782 (avctx->codec_id != AV_CODEC_ID_NONE && avctx->codec_id != codec->id)) {
166 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
167 return AVERROR(EINVAL);
168 }
169
170 33782 avctx->codec_type = codec->type;
171 33782 avctx->codec_id = codec->id;
172 33782 avctx->codec = codec;
173
174
2/4
✓ Branch 0 taken 33782 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33782 times.
33782 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
175 return AVERROR(EINVAL);
176
177
2/2
✓ Branch 1 taken 14544 times.
✓ Branch 2 taken 19238 times.
33782 avci = av_codec_is_decoder(codec) ?
178 14544 ff_decode_internal_alloc() :
179 19238 ff_encode_internal_alloc();
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33782 times.
33782 if (!avci) {
181 ret = AVERROR(ENOMEM);
182 goto end;
183 }
184 33782 avctx->internal = avci;
185
186 33782 avci->buffer_frame = av_frame_alloc();
187 33782 avci->buffer_pkt = av_packet_alloc();
188
2/4
✓ Branch 0 taken 33782 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33782 times.
33782 if (!avci->buffer_frame || !avci->buffer_pkt) {
189 ret = AVERROR(ENOMEM);
190 goto free_and_end;
191 }
192
193
2/2
✓ Branch 0 taken 15143 times.
✓ Branch 1 taken 18639 times.
33782 if (codec2->priv_data_size > 0) {
194
2/2
✓ Branch 0 taken 7658 times.
✓ Branch 1 taken 7485 times.
15143 if (!avctx->priv_data) {
195 7658 avctx->priv_data = av_mallocz(codec2->priv_data_size);
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7658 times.
7658 if (!avctx->priv_data) {
197 ret = AVERROR(ENOMEM);
198 goto free_and_end;
199 }
200
2/2
✓ Branch 0 taken 2229 times.
✓ Branch 1 taken 5429 times.
7658 if (codec->priv_class) {
201 2229 *(const AVClass **)avctx->priv_data = codec->priv_class;
202 2229 av_opt_set_defaults(avctx->priv_data);
203 }
204 }
205
3/4
✓ Branch 0 taken 4853 times.
✓ Branch 1 taken 10290 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4853 times.
15143 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, options)) < 0)
206 goto free_and_end;
207 } else {
208 18639 avctx->priv_data = NULL;
209 }
210
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33782 times.
33782 if ((ret = av_opt_set_dict(avctx, options)) < 0)
211 goto free_and_end;
212
213
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 33782 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
33782 if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
214 av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
215 ret = AVERROR(EINVAL);
216 goto free_and_end;
217 }
218
219 // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
220
6/8
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 33735 times.
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✓ Branch 5 taken 15 times.
✓ Branch 6 taken 32 times.
✗ Branch 7 not taken.
33782 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
221
3/6
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
32 (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
222
3/4
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 33735 times.
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
33782 if (avctx->coded_width && avctx->coded_height)
223 47 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
224
3/4
✓ Branch 0 taken 25260 times.
✓ Branch 1 taken 8475 times.
✓ Branch 2 taken 25260 times.
✗ Branch 3 not taken.
33735 else if (avctx->width && avctx->height)
225 25260 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33782 times.
33782 if (ret < 0)
227 goto free_and_end;
228 }
229
230
5/8
✓ Branch 0 taken 8475 times.
✓ Branch 1 taken 25307 times.
✓ Branch 2 taken 8475 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8475 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 8475 times.
33782 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
231
1/2
✓ Branch 1 taken 25307 times.
✗ Branch 2 not taken.
25307 && ( av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
232
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25307 times.
25307 || av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
233 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
234 ff_set_dimensions(avctx, 0, 0);
235 }
236
237
3/4
✓ Branch 0 taken 25307 times.
✓ Branch 1 taken 8475 times.
✓ Branch 2 taken 25307 times.
✗ Branch 3 not taken.
33782 if (avctx->width > 0 && avctx->height > 0) {
238
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25307 times.
25307 if (av_image_check_sar(avctx->width, avctx->height,
239 avctx->sample_aspect_ratio) < 0) {
240 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
241 avctx->sample_aspect_ratio.num,
242 avctx->sample_aspect_ratio.den);
243 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
244 }
245 }
246
247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33782 times.
33782 if (avctx->sample_rate < 0) {
248 av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate);
249 ret = AVERROR(EINVAL);
250 goto free_and_end;
251 }
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33782 times.
33782 if (avctx->block_align < 0) {
253 av_log(avctx, AV_LOG_ERROR, "Invalid block align: %d\n", avctx->block_align);
254 ret = AVERROR(EINVAL);
255 goto free_and_end;
256 }
257
258 /* AV_CODEC_CAP_CHANNEL_CONF is a decoder-only flag; so the code below
259 * in particular checks that nb_channels is set for all audio encoders. */
260
4/4
✓ Branch 0 taken 4505 times.
✓ Branch 1 taken 29277 times.
✓ Branch 2 taken 236 times.
✓ Branch 3 taken 4269 times.
33782 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && !avctx->ch_layout.nb_channels
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 && !(codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)) {
262 av_log(avctx, AV_LOG_ERROR, "%s requires channel layout to be set\n",
263 av_codec_is_decoder(codec) ? "Decoder" : "Encoder");
264 ret = AVERROR(EINVAL);
265 goto free_and_end;
266 }
267
3/4
✓ Branch 0 taken 4269 times.
✓ Branch 1 taken 29513 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4269 times.
33782 if (avctx->ch_layout.nb_channels && !av_channel_layout_check(&avctx->ch_layout)) {
268 av_log(avctx, AV_LOG_ERROR, "Invalid channel layout\n");
269 ret = AVERROR(EINVAL);
270 goto free_and_end;
271 }
272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33782 times.
33782 if (avctx->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) {
273 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d\n", avctx->ch_layout.nb_channels);
274 ret = AVERROR(EINVAL);
275 goto free_and_end;
276 }
277
278 33782 avctx->frame_num = 0;
279 33782 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
280
281
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 33673 times.
33782 if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
282
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 57 times.
109 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
283
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 52 times.
52 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
284 const AVCodec *codec2;
285 52 av_log(avctx, AV_LOG_ERROR,
286 "The %s '%s' is experimental but experimental codecs are not enabled, "
287 "add '-strict %d' if you want to use it.\n",
288 52 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
289
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 52 times.
52 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
291 av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
292 codec_string, codec2->name);
293 52 ret = AVERROR_EXPERIMENTAL;
294 52 goto free_and_end;
295 }
296
297
2/2
✓ Branch 0 taken 4505 times.
✓ Branch 1 taken 29225 times.
33730 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
298
3/4
✓ Branch 0 taken 1242 times.
✓ Branch 1 taken 3263 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1242 times.
4505 (!avctx->time_base.num || !avctx->time_base.den)) {
299 3263 avctx->time_base.num = 1;
300 3263 avctx->time_base.den = avctx->sample_rate;
301 }
302
303
2/2
✓ Branch 1 taken 19238 times.
✓ Branch 2 taken 14492 times.
33730 if (av_codec_is_encoder(avctx->codec))
304 19238 ret = ff_encode_preinit(avctx);
305 else
306 14492 ret = ff_decode_preinit(avctx);
307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33730 times.
33730 if (ret < 0)
308 goto free_and_end;
309
310
2/2
✓ Branch 0 taken 32135 times.
✓ Branch 1 taken 1595 times.
33730 if (HAVE_THREADS && !avci->frame_thread_encoder) {
311 /* Frame-threaded decoders call FFCodec.init for their child contexts. */
312 32135 lock_avcodec(codec2);
313 32135 ret = ff_thread_init(avctx);
314 32135 unlock_avcodec(codec2);
315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32135 times.
32135 if (ret < 0) {
316 goto free_and_end;
317 }
318 }
319 if (!HAVE_THREADS && !(codec2->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
320 avctx->thread_count = 1;
321
322
2/2
✓ Branch 0 taken 1605 times.
✓ Branch 1 taken 32125 times.
33730 if (!(avctx->active_thread_type & FF_THREAD_FRAME) ||
323
2/2
✓ Branch 0 taken 1595 times.
✓ Branch 1 taken 10 times.
1605 avci->frame_thread_encoder) {
324
2/2
✓ Branch 0 taken 27966 times.
✓ Branch 1 taken 5754 times.
33720 if (codec2->init) {
325 27966 lock_avcodec(codec2);
326 27966 ret = codec2->init(avctx);
327 27966 unlock_avcodec(codec2);
328
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 27958 times.
27966 if (ret < 0) {
329 8 avci->needs_close = codec2->caps_internal & FF_CODEC_CAP_INIT_CLEANUP;
330 8 goto free_and_end;
331 }
332 }
333 33712 avci->needs_close = 1;
334 }
335
336 33722 ret=0;
337
338
2/2
✓ Branch 1 taken 14484 times.
✓ Branch 2 taken 19238 times.
33722 if (av_codec_is_decoder(avctx->codec)) {
339
2/2
✓ Branch 0 taken 9972 times.
✓ Branch 1 taken 4512 times.
14484 if (!avctx->bit_rate)
340 9972 avctx->bit_rate = get_bit_rate(avctx);
341
342 /* validate channel layout from the decoder */
343
3/4
✓ Branch 0 taken 3067 times.
✓ Branch 1 taken 11417 times.
✓ Branch 3 taken 3067 times.
✗ Branch 4 not taken.
14484 if ((avctx->ch_layout.nb_channels && !av_channel_layout_check(&avctx->ch_layout)) ||
344
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14484 times.
14484 avctx->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) {
345 ret = AVERROR(EINVAL);
346 goto free_and_end;
347 }
348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14484 times.
14484 if (avctx->bits_per_coded_sample < 0) {
349 ret = AVERROR(EINVAL);
350 goto free_and_end;
351 }
352 }
353
2/2
✓ Branch 0 taken 4853 times.
✓ Branch 1 taken 28869 times.
33722 if (codec->priv_class)
354
1/2
✓ Branch 0 taken 4853 times.
✗ Branch 1 not taken.
4853 av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
355
356 33722 end:
357
358 33782 return ret;
359 60 free_and_end:
360 60 ff_codec_close(avctx);
361 60 goto end;
362 }
363
364 102 void avcodec_flush_buffers(AVCodecContext *avctx)
365 {
366 102 AVCodecInternal *avci = avctx->internal;
367
368
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 102 times.
102 if (av_codec_is_encoder(avctx->codec)) {
369 int caps = avctx->codec->capabilities;
370
371 if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) {
372 // Only encoders that explicitly declare support for it can be
373 // flushed. Otherwise, this is a no-op.
374 av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder "
375 "that doesn't support it\n");
376 return;
377 }
378 ff_encode_flush_buffers(avctx);
379 } else
380 102 ff_decode_flush_buffers(avctx);
381
382 102 avci->draining = 0;
383 102 avci->draining_done = 0;
384 102 av_frame_unref(avci->buffer_frame);
385 102 av_packet_unref(avci->buffer_pkt);
386
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 102 times.
102 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
388 ff_thread_flush(avctx);
389
2/2
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 73 times.
102 else if (ffcodec(avctx->codec)->flush)
390 29 ffcodec(avctx->codec)->flush(avctx);
391 }
392
393 915 void avsubtitle_free(AVSubtitle *sub)
394 {
395 int i;
396
397
2/2
✓ Branch 0 taken 860 times.
✓ Branch 1 taken 915 times.
1775 for (i = 0; i < sub->num_rects; i++) {
398 860 AVSubtitleRect *const rect = sub->rects[i];
399
400 860 av_freep(&rect->data[0]);
401 860 av_freep(&rect->data[1]);
402 860 av_freep(&rect->data[2]);
403 860 av_freep(&rect->data[3]);
404 860 av_freep(&rect->text);
405 860 av_freep(&rect->ass);
406
407 860 av_freep(&sub->rects[i]);
408 }
409
410 915 av_freep(&sub->rects);
411
412 915 memset(sub, 0, sizeof(*sub));
413 915 }
414
415 57297 av_cold void ff_codec_close(AVCodecContext *avctx)
416 {
417 int i;
418
419
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57297 times.
57297 if (!avctx)
420 return;
421
422
2/2
✓ Branch 1 taken 33782 times.
✓ Branch 2 taken 23515 times.
57297 if (avcodec_is_open(avctx)) {
423 33782 AVCodecInternal *avci = avctx->internal;
424
425 33782 if (CONFIG_FRAME_THREAD_ENCODER &&
426
4/4
✓ Branch 0 taken 14343 times.
✓ Branch 1 taken 19439 times.
✓ Branch 2 taken 1595 times.
✓ Branch 3 taken 12748 times.
33782 avci->frame_thread_encoder && avctx->thread_count > 1) {
427 1595 ff_frame_thread_encoder_free(avctx);
428 }
429
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 33752 times.
33782 if (HAVE_THREADS && avci->thread_ctx)
430 30 ff_thread_free(avctx);
431
4/4
✓ Branch 0 taken 33712 times.
✓ Branch 1 taken 70 times.
✓ Branch 3 taken 7032 times.
✓ Branch 4 taken 26680 times.
33782 if (avci->needs_close && ffcodec(avctx->codec)->close)
432 7032 ffcodec(avctx->codec)->close(avctx);
433 33782 avci->byte_buffer_size = 0;
434 33782 av_freep(&avci->byte_buffer);
435 33782 av_frame_free(&avci->buffer_frame);
436 33782 av_packet_free(&avci->buffer_pkt);
437 33782 av_packet_free(&avci->last_pkt_props);
438
439 33782 av_packet_free(&avci->in_pkt);
440 33782 av_frame_free(&avci->in_frame);
441 33782 av_frame_free(&avci->recon_frame);
442
443 33782 ff_refstruct_unref(&avci->pool);
444 33782 ff_refstruct_pool_uninit(&avci->progress_frame_pool);
445
446 33782 ff_hwaccel_uninit(avctx);
447
448 33782 av_bsf_free(&avci->bsf);
449
450 #if FF_API_DROPCHANGED
451 33782 av_channel_layout_uninit(&avci->initial_ch_layout);
452 #endif
453
454 #if CONFIG_LCMS2
455 ff_icc_context_uninit(&avci->icc);
456 #endif
457
458 33782 av_freep(&avctx->internal);
459 }
460
461
2/2
✓ Branch 0 taken 4092 times.
✓ Branch 1 taken 57297 times.
61389 for (i = 0; i < avctx->nb_coded_side_data; i++)
462 4092 av_freep(&avctx->coded_side_data[i].data);
463 57297 av_freep(&avctx->coded_side_data);
464 57297 avctx->nb_coded_side_data = 0;
465
466 57297 av_buffer_unref(&avctx->hw_frames_ctx);
467 57297 av_buffer_unref(&avctx->hw_device_ctx);
468
469
5/6
✓ Branch 0 taken 22751 times.
✓ Branch 1 taken 34546 times.
✓ Branch 2 taken 22751 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7082 times.
✓ Branch 5 taken 15669 times.
57297 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
470 7082 av_opt_free(avctx->priv_data);
471 57297 av_opt_free(avctx);
472 57297 av_freep(&avctx->priv_data);
473
2/2
✓ Branch 1 taken 19238 times.
✓ Branch 2 taken 38059 times.
57297 if (av_codec_is_encoder(avctx->codec)) {
474 19238 av_freep(&avctx->extradata);
475 19238 avctx->extradata_size = 0;
476
2/2
✓ Branch 1 taken 29868 times.
✓ Branch 2 taken 8191 times.
38059 } else if (av_codec_is_decoder(avctx->codec))
477 29868 av_freep(&avctx->subtitle_header);
478
479 57297 avctx->codec = NULL;
480 57297 avctx->active_thread_type = 0;
481 }
482
483 #if FF_API_AVCODEC_CLOSE
484 int avcodec_close(AVCodecContext *avctx)
485 {
486 ff_codec_close(avctx);
487 return 0;
488 }
489 #endif
490
491 17926 static const char *unknown_if_null(const char *str)
492 {
493
1/2
✓ Branch 0 taken 17926 times.
✗ Branch 1 not taken.
17926 return str ? str : "unknown";
494 }
495
496 14868 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
497 {
498 const char *codec_type;
499 const char *codec_name;
500 14868 const char *profile = NULL;
501 AVBPrint bprint;
502 int64_t bitrate;
503 14868 int new_line = 0;
504 AVRational display_aspect_ratio;
505
2/2
✓ Branch 0 taken 14836 times.
✓ Branch 1 taken 32 times.
14868 const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
506 const char *str;
507
508
2/4
✓ Branch 0 taken 14868 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14868 times.
14868 if (!buf || buf_size <= 0)
509 9 return;
510 14868 av_bprint_init_for_buffer(&bprint, buf, buf_size);
511 14868 codec_type = av_get_media_type_string(enc->codec_type);
512 14868 codec_name = avcodec_get_name(enc->codec_id);
513 14868 profile = avcodec_profile_name(enc->codec_id, enc->profile);
514
515
1/2
✓ Branch 0 taken 14868 times.
✗ Branch 1 not taken.
14868 av_bprintf(&bprint, "%s: %s", codec_type ? codec_type : "unknown",
516 codec_name);
517 14868 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
518
519
4/4
✓ Branch 0 taken 7581 times.
✓ Branch 1 taken 7287 times.
✓ Branch 2 taken 310 times.
✓ Branch 3 taken 7271 times.
14868 if (enc->codec && strcmp(enc->codec->name, codec_name))
520 310 av_bprintf(&bprint, " (%s)", enc->codec->name);
521
522
2/2
✓ Branch 0 taken 1905 times.
✓ Branch 1 taken 12963 times.
14868 if (profile)
523 1905 av_bprintf(&bprint, " (%s)", profile);
524
2/2
✓ Branch 0 taken 11194 times.
✓ Branch 1 taken 3674 times.
14868 if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
525
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11194 times.
11194 && av_log_get_level() >= AV_LOG_VERBOSE
526 && enc->refs)
527 av_bprintf(&bprint, ", %d reference frame%s",
528 enc->refs, enc->refs > 1 ? "s" : "");
529
530
2/2
✓ Branch 0 taken 8266 times.
✓ Branch 1 taken 6602 times.
14868 if (enc->codec_tag)
531 8266 av_bprintf(&bprint, " (%s / 0x%04X)",
532 8266 av_fourcc2str(enc->codec_tag), enc->codec_tag);
533
534
5/5
✓ Branch 0 taken 11194 times.
✓ Branch 1 taken 3423 times.
✓ Branch 2 taken 82 times.
✓ Branch 3 taken 160 times.
✓ Branch 4 taken 9 times.
14868 switch (enc->codec_type) {
535 11194 case AVMEDIA_TYPE_VIDEO:
536 {
537 unsigned len;
538
539 11194 av_bprintf(&bprint, "%s%s", separator,
540
2/2
✓ Branch 0 taken 11098 times.
✓ Branch 1 taken 96 times.
11194 enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
541 11098 unknown_if_null(av_get_pix_fmt_name(enc->pix_fmt)));
542
543 11194 av_bprint_chars(&bprint, '(', 1);
544 11194 len = bprint.len;
545
546 /* The following check ensures that '(' has been written
547 * and therefore allows us to erase it if it turns out
548 * to be unnecessary. */
549
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11194 times.
11194 if (!av_bprint_is_complete(&bprint))
550 return;
551
552
3/4
✓ Branch 0 taken 1119 times.
✓ Branch 1 taken 10075 times.
✓ Branch 2 taken 1119 times.
✗ Branch 3 not taken.
11194 if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
553
2/2
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 1092 times.
1119 enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
554 27 av_bprintf(&bprint, "%d bpc, ", enc->bits_per_raw_sample);
555
3/4
✓ Branch 0 taken 7647 times.
✓ Branch 1 taken 3547 times.
✓ Branch 2 taken 7647 times.
✗ Branch 3 not taken.
18841 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED &&
556 7647 (str = av_color_range_name(enc->color_range)))
557 7647 av_bprintf(&bprint, "%s, ", str);
558
559
2/2
✓ Branch 0 taken 9036 times.
✓ Branch 1 taken 2158 times.
11194 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
560
2/2
✓ Branch 0 taken 9034 times.
✓ Branch 1 taken 2 times.
9036 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
561
2/2
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 8918 times.
9034 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
562 2276 const char *col = unknown_if_null(av_color_space_name(enc->colorspace));
563 2276 const char *pri = unknown_if_null(av_color_primaries_name(enc->color_primaries));
564 2276 const char *trc = unknown_if_null(av_color_transfer_name(enc->color_trc));
565
4/4
✓ Branch 0 taken 278 times.
✓ Branch 1 taken 1998 times.
✓ Branch 2 taken 130 times.
✓ Branch 3 taken 148 times.
2276 if (strcmp(col, pri) || strcmp(col, trc)) {
566 2128 new_line = 1;
567 2128 av_bprintf(&bprint, "%s/%s/%s, ", col, pri, trc);
568 } else
569 148 av_bprintf(&bprint, "%s, ", col);
570 }
571
572
2/2
✓ Branch 0 taken 5877 times.
✓ Branch 1 taken 5317 times.
11194 if (enc->field_order != AV_FIELD_UNKNOWN) {
573 5877 const char *field_order = "progressive";
574
2/2
✓ Branch 0 taken 137 times.
✓ Branch 1 taken 5740 times.
5877 if (enc->field_order == AV_FIELD_TT)
575 137 field_order = "top first";
576
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 5696 times.
5740 else if (enc->field_order == AV_FIELD_BB)
577 44 field_order = "bottom first";
578
2/2
✓ Branch 0 taken 342 times.
✓ Branch 1 taken 5354 times.
5696 else if (enc->field_order == AV_FIELD_TB)
579 342 field_order = "top coded first (swapped)";
580
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 5277 times.
5354 else if (enc->field_order == AV_FIELD_BT)
581 77 field_order = "bottom coded first (swapped)";
582
583 5877 av_bprintf(&bprint, "%s, ", field_order);
584 }
585
586
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11194 times.
11194 if (av_log_get_level() >= AV_LOG_VERBOSE &&
587 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED &&
588 (str = av_chroma_location_name(enc->chroma_sample_location)))
589 av_bprintf(&bprint, "%s, ", str);
590
591
2/2
✓ Branch 0 taken 1540 times.
✓ Branch 1 taken 9654 times.
11194 if (len == bprint.len) {
592 1540 bprint.str[len - 1] = '\0';
593 1540 bprint.len--;
594 } else {
595
1/2
✓ Branch 0 taken 9654 times.
✗ Branch 1 not taken.
9654 if (bprint.len - 2 < bprint.size) {
596 /* Erase the last ", " */
597 9654 bprint.len -= 2;
598 9654 bprint.str[bprint.len] = '\0';
599 }
600 9654 av_bprint_chars(&bprint, ')', 1);
601 }
602 }
603
604
2/2
✓ Branch 0 taken 11178 times.
✓ Branch 1 taken 16 times.
11194 if (enc->width) {
605
2/2
✓ Branch 0 taken 2126 times.
✓ Branch 1 taken 9052 times.
11178 av_bprintf(&bprint, "%s%dx%d", new_line ? separator : ", ",
606 enc->width, enc->height);
607
608
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11178 times.
11178 if (av_log_get_level() >= AV_LOG_VERBOSE &&
609 enc->coded_width && enc->coded_height &&
610 (enc->width != enc->coded_width ||
611 enc->height != enc->coded_height))
612 av_bprintf(&bprint, " (%dx%d)",
613 enc->coded_width, enc->coded_height);
614
615
2/2
✓ Branch 0 taken 1384 times.
✓ Branch 1 taken 9794 times.
11178 if (enc->sample_aspect_ratio.num) {
616 1384 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
617 1384 enc->width * (int64_t)enc->sample_aspect_ratio.num,
618 1384 enc->height * (int64_t)enc->sample_aspect_ratio.den,
619 1024 * 1024);
620 1384 av_bprintf(&bprint, " [SAR %d:%d DAR %d:%d]",
621 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
622 display_aspect_ratio.num, display_aspect_ratio.den);
623 }
624
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11178 times.
11178 if (av_log_get_level() >= AV_LOG_DEBUG) {
625 int g = av_gcd(enc->time_base.num, enc->time_base.den);
626 av_bprintf(&bprint, ", %d/%d",
627 enc->time_base.num / g, enc->time_base.den / g);
628 }
629 }
630
2/2
✓ Branch 0 taken 5523 times.
✓ Branch 1 taken 5671 times.
11194 if (encode) {
631 5523 av_bprintf(&bprint, ", q=%d-%d", enc->qmin, enc->qmax);
632 } else {
633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5671 times.
5671 if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
634 av_bprintf(&bprint, ", Closed Captions");
635
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5671 times.
5671 if (enc->properties & FF_CODEC_PROPERTY_FILM_GRAIN)
636 av_bprintf(&bprint, ", Film Grain");
637
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5671 times.
5671 if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
638 av_bprintf(&bprint, ", lossless");
639 }
640 11194 break;
641 3423 case AVMEDIA_TYPE_AUDIO:
642 3423 av_bprintf(&bprint, "%s", separator);
643
644
2/2
✓ Branch 0 taken 3411 times.
✓ Branch 1 taken 12 times.
3423 if (enc->sample_rate) {
645 3411 av_bprintf(&bprint, "%d Hz, ", enc->sample_rate);
646 }
647 3423 av_channel_layout_describe_bprint(&enc->ch_layout, &bprint);
648
3/4
✓ Branch 0 taken 3403 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 3403 times.
✗ Branch 3 not taken.
6826 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE &&
649 3403 (str = av_get_sample_fmt_name(enc->sample_fmt))) {
650 3403 av_bprintf(&bprint, ", %s", str);
651 }
652
2/2
✓ Branch 0 taken 1756 times.
✓ Branch 1 taken 1667 times.
3423 if ( enc->bits_per_raw_sample > 0
653
2/2
✓ Branch 1 taken 212 times.
✓ Branch 2 taken 1544 times.
1756 && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
654 212 av_bprintf(&bprint, " (%d bit)", enc->bits_per_raw_sample);
655
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 3419 times.
3423 if (av_log_get_level() >= AV_LOG_VERBOSE) {
656
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (enc->initial_padding)
657 2 av_bprintf(&bprint, ", delay %d", enc->initial_padding);
658
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (enc->trailing_padding)
659 av_bprintf(&bprint, ", padding %d", enc->trailing_padding);
660 }
661 3423 break;
662 82 case AVMEDIA_TYPE_DATA:
663
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 82 times.
82 if (av_log_get_level() >= AV_LOG_DEBUG) {
664 int g = av_gcd(enc->time_base.num, enc->time_base.den);
665 if (g)
666 av_bprintf(&bprint, ", %d/%d",
667 enc->time_base.num / g, enc->time_base.den / g);
668 }
669 82 break;
670 160 case AVMEDIA_TYPE_SUBTITLE:
671
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 127 times.
160 if (enc->width)
672 33 av_bprintf(&bprint, ", %dx%d", enc->width, enc->height);
673 160 break;
674 9 default:
675 9 return;
676 }
677
2/2
✓ Branch 0 taken 7154 times.
✓ Branch 1 taken 7705 times.
14859 if (encode) {
678
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7154 times.
7154 if (enc->flags & AV_CODEC_FLAG_PASS1)
679 av_bprintf(&bprint, ", pass 1");
680
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7154 times.
7154 if (enc->flags & AV_CODEC_FLAG_PASS2)
681 av_bprintf(&bprint, ", pass 2");
682 }
683 14859 bitrate = get_bit_rate(enc);
684
2/2
✓ Branch 0 taken 9375 times.
✓ Branch 1 taken 5484 times.
14859 if (bitrate != 0) {
685 9375 av_bprintf(&bprint, ", %"PRId64" kb/s", bitrate / 1000);
686
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5484 times.
5484 } else if (enc->rc_max_rate > 0) {
687 av_bprintf(&bprint, ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
688 }
689 }
690
691 2656734 int avcodec_is_open(AVCodecContext *s)
692 {
693 2656734 return !!s->internal;
694 }
695
696 760044 int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
697 {
698 760044 av_frame_unref(frame);
699
700
2/2
✓ Branch 1 taken 759984 times.
✓ Branch 2 taken 60 times.
760044 if (av_codec_is_decoder(avctx->codec))
701 759984 return ff_decode_receive_frame(avctx, frame);
702 60 return ff_encode_receive_frame(avctx, frame);
703 }
704