FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/utils.c
Date: 2025-01-20 09:27:23
Exec Total Coverage
Lines: 386 528 73.1%
Functions: 25 30 83.3%
Branches: 288 401 71.8%

Line Branch Exec Source
1 /*
2 * utils for libavcodec
3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * utils.
26 */
27
28 #include "config.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/channel_layout.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/pixfmt.h"
36 #include "avcodec.h"
37 #include "codec.h"
38 #include "codec_desc.h"
39 #include "codec_internal.h"
40 #include "codec_par.h"
41 #include "decode.h"
42 #include "hwconfig.h"
43 #include "libavutil/refstruct.h"
44 #include "thread.h"
45 #include "threadframe.h"
46 #include "internal.h"
47 #include "put_bits.h"
48 #include "startcode.h"
49 #include <stdlib.h>
50 #include <limits.h>
51
52 201124 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
53 {
54 201124 uint8_t **p = ptr;
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 201124 times.
201124 if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
56 av_freep(p);
57 *size = 0;
58 return;
59 }
60 201124 av_fast_mallocz(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
61
1/2
✓ Branch 0 taken 201124 times.
✗ Branch 1 not taken.
201124 if (*p)
62 201124 memset(*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
63 }
64
65 697 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
66 {
67 697 uint8_t **p = ptr;
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 697 times.
697 if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
69 av_freep(p);
70 *size = 0;
71 return;
72 }
73 697 av_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
74
1/2
✓ Branch 0 taken 697 times.
✗ Branch 1 not taken.
697 if (*p)
75 697 memset(*p, 0, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
76 }
77
78 8289081 int av_codec_is_encoder(const AVCodec *avcodec)
79 {
80 8289081 const FFCodec *const codec = ffcodec(avcodec);
81
4/4
✓ Branch 0 taken 8279700 times.
✓ Branch 1 taken 9381 times.
✓ Branch 2 taken 5063891 times.
✓ Branch 3 taken 3215809 times.
13352972 return codec && (codec->cb_type == FF_CODEC_CB_TYPE_ENCODE ||
82
2/2
✓ Branch 0 taken 4978647 times.
✓ Branch 1 taken 85244 times.
5063891 codec->cb_type == FF_CODEC_CB_TYPE_ENCODE_SUB ||
83
2/2
✓ Branch 0 taken 92532 times.
✓ Branch 1 taken 4886115 times.
4978647 codec->cb_type == FF_CODEC_CB_TYPE_RECEIVE_PACKET);
84 }
85
86 12569700 int av_codec_is_decoder(const AVCodec *avcodec)
87 {
88 12569700 const FFCodec *const codec = ffcodec(avcodec);
89
4/4
✓ Branch 0 taken 12560319 times.
✓ Branch 1 taken 9381 times.
✓ Branch 2 taken 5106635 times.
✓ Branch 3 taken 7453684 times.
17676335 return codec && (codec->cb_type == FF_CODEC_CB_TYPE_DECODE ||
90
2/2
✓ Branch 0 taken 5094058 times.
✓ Branch 1 taken 12577 times.
5106635 codec->cb_type == FF_CODEC_CB_TYPE_DECODE_SUB ||
91
2/2
✓ Branch 0 taken 272064 times.
✓ Branch 1 taken 4821994 times.
5094058 codec->cb_type == FF_CODEC_CB_TYPE_RECEIVE_FRAME);
92 }
93
94 74669 int ff_set_dimensions(AVCodecContext *s, int width, int height)
95 {
96 74669 int ret = av_image_check_size2(width, height, s->max_pixels, AV_PIX_FMT_NONE, 0, s);
97
98
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 74668 times.
74669 if (ret < 0)
99 1 width = height = 0;
100
101 74669 s->coded_width = width;
102 74669 s->coded_height = height;
103 74669 s->width = AV_CEIL_RSHIFT(width, s->lowres);
104 74669 s->height = AV_CEIL_RSHIFT(height, s->lowres);
105
106 74669 return ret;
107 }
108
109 2893 int ff_set_sar(AVCodecContext *avctx, AVRational sar)
110 {
111 2893 int ret = av_image_check_sar(avctx->width, avctx->height, sar);
112
113
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2888 times.
2893 if (ret < 0) {
114 5 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
115 sar.num, sar.den);
116 5 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
117 5 return ret;
118 } else {
119 2888 avctx->sample_aspect_ratio = sar;
120 }
121 2888 return 0;
122 }
123
124 2977 int ff_side_data_update_matrix_encoding(AVFrame *frame,
125 enum AVMatrixEncoding matrix_encoding)
126 {
127 AVFrameSideData *side_data;
128 enum AVMatrixEncoding *data;
129
130 2977 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
131
1/2
✓ Branch 0 taken 2977 times.
✗ Branch 1 not taken.
2977 if (!side_data)
132 2977 side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
133 sizeof(enum AVMatrixEncoding));
134
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2977 times.
2977 if (!side_data)
136 return AVERROR(ENOMEM);
137
138 2977 data = (enum AVMatrixEncoding*)side_data->data;
139 2977 *data = matrix_encoding;
140
141 2977 return 0;
142 }
143
144 10442 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
145 int linesize_align[AV_NUM_DATA_POINTERS])
146 {
147 int i;
148 10442 int w_align = 1;
149 10442 int h_align = 1;
150 10442 AVPixFmtDescriptor const *desc = av_pix_fmt_desc_get(s->pix_fmt);
151
152
1/2
✓ Branch 0 taken 10442 times.
✗ Branch 1 not taken.
10442 if (desc) {
153 10442 w_align = 1 << desc->log2_chroma_w;
154 10442 h_align = 1 << desc->log2_chroma_h;
155 }
156
157
9/9
✓ Branch 0 taken 9515 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 38 times.
✓ Branch 4 taken 133 times.
✓ Branch 5 taken 81 times.
✓ Branch 6 taken 215 times.
✓ Branch 7 taken 27 times.
✓ Branch 8 taken 386 times.
10442 switch (s->pix_fmt) {
158 9515 case AV_PIX_FMT_YUV420P:
159 case AV_PIX_FMT_YUYV422:
160 case AV_PIX_FMT_YVYU422:
161 case AV_PIX_FMT_UYVY422:
162 case AV_PIX_FMT_YUV422P:
163 case AV_PIX_FMT_YUV440P:
164 case AV_PIX_FMT_YUV444P:
165 case AV_PIX_FMT_GBRP:
166 case AV_PIX_FMT_GBRAP:
167 case AV_PIX_FMT_GRAY8:
168 case AV_PIX_FMT_GRAY16BE:
169 case AV_PIX_FMT_GRAY16LE:
170 case AV_PIX_FMT_YUVJ420P:
171 case AV_PIX_FMT_YUVJ422P:
172 case AV_PIX_FMT_YUVJ440P:
173 case AV_PIX_FMT_YUVJ444P:
174 case AV_PIX_FMT_YUVA420P:
175 case AV_PIX_FMT_YUVA422P:
176 case AV_PIX_FMT_YUVA444P:
177 case AV_PIX_FMT_YUV420P9LE:
178 case AV_PIX_FMT_YUV420P9BE:
179 case AV_PIX_FMT_YUV420P10LE:
180 case AV_PIX_FMT_YUV420P10BE:
181 case AV_PIX_FMT_YUV420P12LE:
182 case AV_PIX_FMT_YUV420P12BE:
183 case AV_PIX_FMT_YUV420P14LE:
184 case AV_PIX_FMT_YUV420P14BE:
185 case AV_PIX_FMT_YUV420P16LE:
186 case AV_PIX_FMT_YUV420P16BE:
187 case AV_PIX_FMT_YUVA420P9LE:
188 case AV_PIX_FMT_YUVA420P9BE:
189 case AV_PIX_FMT_YUVA420P10LE:
190 case AV_PIX_FMT_YUVA420P10BE:
191 case AV_PIX_FMT_YUVA420P16LE:
192 case AV_PIX_FMT_YUVA420P16BE:
193 case AV_PIX_FMT_YUV422P9LE:
194 case AV_PIX_FMT_YUV422P9BE:
195 case AV_PIX_FMT_YUV422P10LE:
196 case AV_PIX_FMT_YUV422P10BE:
197 case AV_PIX_FMT_YUV422P12LE:
198 case AV_PIX_FMT_YUV422P12BE:
199 case AV_PIX_FMT_YUV422P14LE:
200 case AV_PIX_FMT_YUV422P14BE:
201 case AV_PIX_FMT_YUV422P16LE:
202 case AV_PIX_FMT_YUV422P16BE:
203 case AV_PIX_FMT_YUVA422P9LE:
204 case AV_PIX_FMT_YUVA422P9BE:
205 case AV_PIX_FMT_YUVA422P10LE:
206 case AV_PIX_FMT_YUVA422P10BE:
207 case AV_PIX_FMT_YUVA422P12LE:
208 case AV_PIX_FMT_YUVA422P12BE:
209 case AV_PIX_FMT_YUVA422P16LE:
210 case AV_PIX_FMT_YUVA422P16BE:
211 case AV_PIX_FMT_YUV440P10LE:
212 case AV_PIX_FMT_YUV440P10BE:
213 case AV_PIX_FMT_YUV440P12LE:
214 case AV_PIX_FMT_YUV440P12BE:
215 case AV_PIX_FMT_YUV444P9LE:
216 case AV_PIX_FMT_YUV444P9BE:
217 case AV_PIX_FMT_YUV444P10LE:
218 case AV_PIX_FMT_YUV444P10BE:
219 case AV_PIX_FMT_YUV444P12LE:
220 case AV_PIX_FMT_YUV444P12BE:
221 case AV_PIX_FMT_YUV444P14LE:
222 case AV_PIX_FMT_YUV444P14BE:
223 case AV_PIX_FMT_YUV444P16LE:
224 case AV_PIX_FMT_YUV444P16BE:
225 case AV_PIX_FMT_YUVA444P9LE:
226 case AV_PIX_FMT_YUVA444P9BE:
227 case AV_PIX_FMT_YUVA444P10LE:
228 case AV_PIX_FMT_YUVA444P10BE:
229 case AV_PIX_FMT_YUVA444P12LE:
230 case AV_PIX_FMT_YUVA444P12BE:
231 case AV_PIX_FMT_YUVA444P16LE:
232 case AV_PIX_FMT_YUVA444P16BE:
233 case AV_PIX_FMT_GBRP9LE:
234 case AV_PIX_FMT_GBRP9BE:
235 case AV_PIX_FMT_GBRP10LE:
236 case AV_PIX_FMT_GBRP10BE:
237 case AV_PIX_FMT_GBRP12LE:
238 case AV_PIX_FMT_GBRP12BE:
239 case AV_PIX_FMT_GBRP14LE:
240 case AV_PIX_FMT_GBRP14BE:
241 case AV_PIX_FMT_GBRP16LE:
242 case AV_PIX_FMT_GBRP16BE:
243 case AV_PIX_FMT_GBRAP12LE:
244 case AV_PIX_FMT_GBRAP12BE:
245 case AV_PIX_FMT_GBRAP16LE:
246 case AV_PIX_FMT_GBRAP16BE:
247 9515 w_align = 16; //FIXME assume 16 pixel per macroblock
248 9515 h_align = 16 * 2; // interlaced needs 2 macroblocks height
249
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9512 times.
9515 if (s->codec_id == AV_CODEC_ID_BINKVIDEO)
250 3 w_align = 16*2;
251 9515 break;
252 26 case AV_PIX_FMT_YUV411P:
253 case AV_PIX_FMT_YUVJ411P:
254 case AV_PIX_FMT_UYYVYY411:
255 26 w_align = 32;
256 26 h_align = 16 * 2;
257 26 break;
258 21 case AV_PIX_FMT_YUV410P:
259
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 11 times.
21 if (s->codec_id == AV_CODEC_ID_SVQ1) {
260 10 w_align = 64;
261 10 h_align = 64;
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 } else if (s->codec_id == AV_CODEC_ID_SNOW) {
263 w_align = 16;
264 h_align = 16;
265 }
266 21 break;
267 38 case AV_PIX_FMT_RGB555:
268
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 33 times.
38 if (s->codec_id == AV_CODEC_ID_RPZA) {
269 5 w_align = 4;
270 5 h_align = 4;
271 }
272
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 37 times.
38 if (s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
273 1 w_align = 8;
274 1 h_align = 8;
275 }
276 38 break;
277 133 case AV_PIX_FMT_PAL8:
278 case AV_PIX_FMT_BGR8:
279 case AV_PIX_FMT_RGB8:
280
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 5 times.
133 if (s->codec_id == AV_CODEC_ID_SMC ||
281
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 127 times.
128 s->codec_id == AV_CODEC_ID_CINEPAK) {
282 6 w_align = 4;
283 6 h_align = 4;
284 }
285
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 1 times.
133 if (s->codec_id == AV_CODEC_ID_JV ||
286
1/2
✓ Branch 0 taken 132 times.
✗ Branch 1 not taken.
132 s->codec_id == AV_CODEC_ID_ARGO ||
287
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 131 times.
132 s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
288 2 w_align = 8;
289 2 h_align = 8;
290 }
291
1/2
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
133 if (s->codec_id == AV_CODEC_ID_MJPEG ||
292
1/2
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
133 s->codec_id == AV_CODEC_ID_MJPEGB ||
293
1/2
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
133 s->codec_id == AV_CODEC_ID_LJPEG ||
294
1/2
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
133 s->codec_id == AV_CODEC_ID_SMVJPEG ||
295
1/2
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
133 s->codec_id == AV_CODEC_ID_AMV ||
296
1/2
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
133 s->codec_id == AV_CODEC_ID_SP5X ||
297
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 125 times.
133 s->codec_id == AV_CODEC_ID_JPEGLS) {
298 8 w_align = 8;
299 8 h_align = 2*8;
300 }
301 133 break;
302 81 case AV_PIX_FMT_BGR24:
303
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 1 times.
81 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
304
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 75 times.
80 (s->codec_id == AV_CODEC_ID_ZLIB)) {
305 6 w_align = 4;
306 6 h_align = 4;
307 }
308 81 break;
309 215 case AV_PIX_FMT_RGB24:
310
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 208 times.
215 if (s->codec_id == AV_CODEC_ID_CINEPAK) {
311 7 w_align = 4;
312 7 h_align = 4;
313 }
314 215 break;
315 27 case AV_PIX_FMT_BGR0:
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (s->codec_id == AV_CODEC_ID_ARGO) {
317 w_align = 8;
318 h_align = 8;
319 }
320 27 break;
321 386 default:
322 386 break;
323 }
324
325
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10440 times.
10442 if (s->codec_id == AV_CODEC_ID_IFF_ILBM) {
326 2 w_align = FFMAX(w_align, 16);
327 }
328
329 10442 *width = FFALIGN(*width, w_align);
330 10442 *height = FFALIGN(*height, h_align);
331
4/4
✓ Branch 0 taken 9850 times.
✓ Branch 1 taken 592 times.
✓ Branch 2 taken 9849 times.
✓ Branch 3 taken 1 times.
10442 if (s->codec_id == AV_CODEC_ID_H264 || s->lowres ||
332
4/4
✓ Branch 0 taken 9834 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 9827 times.
✓ Branch 3 taken 7 times.
9849 s->codec_id == AV_CODEC_ID_VC1 || s->codec_id == AV_CODEC_ID_WMV3 ||
333
4/4
✓ Branch 0 taken 9826 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 9817 times.
✓ Branch 3 taken 9 times.
9827 s->codec_id == AV_CODEC_ID_VP5 || s->codec_id == AV_CODEC_ID_VP6 ||
334
4/4
✓ Branch 0 taken 9815 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 9813 times.
9817 s->codec_id == AV_CODEC_ID_VP6F || s->codec_id == AV_CODEC_ID_VP6A
335 ) {
336 // some of the optimized chroma MC reads one line too much
337 // which is also done in mpeg decoders with lowres > 0
338 629 *height += 2;
339
340 // H.264 uses edge emulation for out of frame motion vectors, for this
341 // it requires a temporary area large enough to hold a 21x21 block,
342 // increasing witdth ensure that the temporary area is large enough,
343 // the next rounded up width is 32
344 629 *width = FFMAX(*width, 32);
345 }
346
2/2
✓ Branch 0 taken 2618 times.
✓ Branch 1 taken 7824 times.
10442 if (s->codec_id == AV_CODEC_ID_SVQ3) {
347 2618 *width = FFMAX(*width, 32);
348 }
349
350
2/2
✓ Branch 0 taken 41768 times.
✓ Branch 1 taken 10442 times.
52210 for (i = 0; i < 4; i++)
351 41768 linesize_align[i] = STRIDE_ALIGN;
352 10442 }
353
354 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
355 {
356 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
357 int chroma_shift = desc->log2_chroma_w;
358 int linesize_align[AV_NUM_DATA_POINTERS];
359 int align;
360
361 avcodec_align_dimensions2(s, width, height, linesize_align);
362 align = FFMAX(linesize_align[0], linesize_align[3]);
363 linesize_align[1] <<= chroma_shift;
364 linesize_align[2] <<= chroma_shift;
365 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
366 *width = FFALIGN(*width, align);
367 }
368
369 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
370 enum AVSampleFormat sample_fmt, const uint8_t *buf,
371 int buf_size, int align)
372 {
373 int ch, planar, needed_size, ret = 0;
374
375 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
376 frame->nb_samples, sample_fmt,
377 align);
378 if (buf_size < needed_size)
379 return AVERROR(EINVAL);
380
381 planar = av_sample_fmt_is_planar(sample_fmt);
382 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
383 if (!FF_ALLOCZ_TYPED_ARRAY(frame->extended_data, nb_channels))
384 return AVERROR(ENOMEM);
385 } else {
386 frame->extended_data = frame->data;
387 }
388
389 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
390 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
391 sample_fmt, align)) < 0) {
392 if (frame->extended_data != frame->data)
393 av_freep(&frame->extended_data);
394 return ret;
395 }
396 if (frame->extended_data != frame->data) {
397 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
398 frame->data[ch] = frame->extended_data[ch];
399 }
400
401 return ret;
402 }
403
404
405 195282 int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec){
406 195282 return !!(ffcodec(codec)->caps_internal & FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM);
407 }
408
409 28677 const char *avcodec_get_name(enum AVCodecID id)
410 {
411 const AVCodecDescriptor *cd;
412 const AVCodec *codec;
413
414
2/2
✓ Branch 0 taken 243 times.
✓ Branch 1 taken 28434 times.
28677 if (id == AV_CODEC_ID_NONE)
415 243 return "none";
416 28434 cd = avcodec_descriptor_get(id);
417
2/2
✓ Branch 0 taken 28430 times.
✓ Branch 1 taken 4 times.
28434 if (cd)
418 28430 return cd->name;
419 4 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
420 4 codec = avcodec_find_decoder(id);
421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (codec)
422 return codec->name;
423 4 codec = avcodec_find_encoder(id);
424
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (codec)
425 return codec->name;
426 4 return "unknown_codec";
427 }
428
429 const char *av_get_profile_name(const AVCodec *codec, int profile)
430 {
431 const AVProfile *p;
432 if (profile == AV_PROFILE_UNKNOWN || !codec->profiles)
433 return NULL;
434
435 for (p = codec->profiles; p->profile != AV_PROFILE_UNKNOWN; p++)
436 if (p->profile == profile)
437 return p->name;
438
439 return NULL;
440 }
441
442 16668 const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
443 {
444 16668 const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
445 const AVProfile *p;
446
447
5/6
✓ Branch 0 taken 2156 times.
✓ Branch 1 taken 14512 times.
✓ Branch 2 taken 2156 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 132 times.
✓ Branch 5 taken 2024 times.
16668 if (profile == AV_PROFILE_UNKNOWN || !desc || !desc->profiles)
448 14644 return NULL;
449
450
2/2
✓ Branch 0 taken 5169 times.
✓ Branch 1 taken 34 times.
5203 for (p = desc->profiles; p->profile != AV_PROFILE_UNKNOWN; p++)
451
2/2
✓ Branch 0 taken 1990 times.
✓ Branch 1 taken 3179 times.
5169 if (p->profile == profile)
452 1990 return p->name;
453
454 34 return NULL;
455 }
456
457 809148 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
458 {
459
8/8
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 11406 times.
✓ Branch 2 taken 9941 times.
✓ Branch 3 taken 502520 times.
✓ Branch 4 taken 17036 times.
✓ Branch 5 taken 23894 times.
✓ Branch 6 taken 18405 times.
✓ Branch 7 taken 225922 times.
809148 switch (codec_id) {
460 24 case AV_CODEC_ID_DFPWM:
461 24 return 1;
462 11406 case AV_CODEC_ID_8SVX_EXP:
463 case AV_CODEC_ID_8SVX_FIB:
464 case AV_CODEC_ID_ADPCM_ARGO:
465 case AV_CODEC_ID_ADPCM_CT:
466 case AV_CODEC_ID_ADPCM_IMA_ALP:
467 case AV_CODEC_ID_ADPCM_IMA_AMV:
468 case AV_CODEC_ID_ADPCM_IMA_APC:
469 case AV_CODEC_ID_ADPCM_IMA_APM:
470 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
471 case AV_CODEC_ID_ADPCM_IMA_OKI:
472 case AV_CODEC_ID_ADPCM_IMA_WS:
473 case AV_CODEC_ID_ADPCM_IMA_SSI:
474 case AV_CODEC_ID_ADPCM_G722:
475 case AV_CODEC_ID_ADPCM_YAMAHA:
476 case AV_CODEC_ID_ADPCM_AICA:
477 11406 return 4;
478 9941 case AV_CODEC_ID_DSD_LSBF:
479 case AV_CODEC_ID_DSD_MSBF:
480 case AV_CODEC_ID_DSD_LSBF_PLANAR:
481 case AV_CODEC_ID_DSD_MSBF_PLANAR:
482 case AV_CODEC_ID_PCM_ALAW:
483 case AV_CODEC_ID_PCM_MULAW:
484 case AV_CODEC_ID_PCM_VIDC:
485 case AV_CODEC_ID_PCM_S8:
486 case AV_CODEC_ID_PCM_S8_PLANAR:
487 case AV_CODEC_ID_PCM_SGA:
488 case AV_CODEC_ID_PCM_U8:
489 case AV_CODEC_ID_SDX2_DPCM:
490 case AV_CODEC_ID_CBD2_DPCM:
491 case AV_CODEC_ID_DERF_DPCM:
492 case AV_CODEC_ID_WADY_DPCM:
493 9941 return 8;
494 502520 case AV_CODEC_ID_PCM_S16BE:
495 case AV_CODEC_ID_PCM_S16BE_PLANAR:
496 case AV_CODEC_ID_PCM_S16LE:
497 case AV_CODEC_ID_PCM_S16LE_PLANAR:
498 case AV_CODEC_ID_PCM_U16BE:
499 case AV_CODEC_ID_PCM_U16LE:
500 502520 return 16;
501 17036 case AV_CODEC_ID_PCM_S24DAUD:
502 case AV_CODEC_ID_PCM_S24BE:
503 case AV_CODEC_ID_PCM_S24LE:
504 case AV_CODEC_ID_PCM_S24LE_PLANAR:
505 case AV_CODEC_ID_PCM_U24BE:
506 case AV_CODEC_ID_PCM_U24LE:
507 17036 return 24;
508 23894 case AV_CODEC_ID_PCM_S32BE:
509 case AV_CODEC_ID_PCM_S32LE:
510 case AV_CODEC_ID_PCM_S32LE_PLANAR:
511 case AV_CODEC_ID_PCM_U32BE:
512 case AV_CODEC_ID_PCM_U32LE:
513 case AV_CODEC_ID_PCM_F32BE:
514 case AV_CODEC_ID_PCM_F32LE:
515 case AV_CODEC_ID_PCM_F24LE:
516 case AV_CODEC_ID_PCM_F16LE:
517 23894 return 32;
518 18405 case AV_CODEC_ID_PCM_F64BE:
519 case AV_CODEC_ID_PCM_F64LE:
520 case AV_CODEC_ID_PCM_S64BE:
521 case AV_CODEC_ID_PCM_S64LE:
522 18405 return 64;
523 225922 default:
524 225922 return 0;
525 }
526 }
527
528 24 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
529 {
530 static const enum AVCodecID map[][2] = {
531 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
532 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
533 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
534 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
535 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
536 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
537 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
538 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
539 [AV_SAMPLE_FMT_S64P] = { AV_CODEC_ID_PCM_S64LE, AV_CODEC_ID_PCM_S64BE },
540 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
541 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
542 };
543
2/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
24 if (fmt < 0 || fmt >= FF_ARRAY_ELEMS(map))
544 return AV_CODEC_ID_NONE;
545
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 if (be < 0 || be > 1)
546 24 be = AV_NE(1, 0);
547 24 return map[fmt][be];
548 }
549
550 286562 int av_get_bits_per_sample(enum AVCodecID codec_id)
551 {
552
5/5
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 74 times.
✓ Branch 4 taken 286465 times.
286562 switch (codec_id) {
553 17 case AV_CODEC_ID_DFPWM:
554 17 return 1;
555 3 case AV_CODEC_ID_ADPCM_SBPRO_2:
556 3 return 2;
557 3 case AV_CODEC_ID_ADPCM_SBPRO_3:
558 3 return 3;
559 74 case AV_CODEC_ID_ADPCM_SBPRO_4:
560 case AV_CODEC_ID_ADPCM_IMA_WAV:
561 case AV_CODEC_ID_ADPCM_IMA_XBOX:
562 case AV_CODEC_ID_ADPCM_IMA_QT:
563 case AV_CODEC_ID_ADPCM_SWF:
564 case AV_CODEC_ID_ADPCM_MS:
565 74 return 4;
566 286465 default:
567 286465 return av_get_exact_bits_per_sample(codec_id);
568 }
569 }
570
571 513679 static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
572 uint32_t tag, int bits_per_coded_sample, int64_t bitrate,
573 uint8_t * extradata, int frame_size, int frame_bytes)
574 {
575 513679 int bps = av_get_exact_bits_per_sample(id);
576
4/4
✓ Branch 0 taken 355926 times.
✓ Branch 1 taken 157753 times.
✓ Branch 2 taken 355621 times.
✓ Branch 3 taken 305 times.
513679 int framecount = (ba > 0 && frame_bytes / ba > 0) ? frame_bytes / ba : 1;
577
578 /* codecs with an exact constant bits per sample */
579
7/10
✓ Branch 0 taken 298380 times.
✓ Branch 1 taken 215299 times.
✓ Branch 2 taken 298380 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 298344 times.
✓ Branch 5 taken 36 times.
✓ Branch 6 taken 298344 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 298344 times.
✗ Branch 9 not taken.
513679 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
580 298344 return (frame_bytes * 8LL) / (bps * ch);
581 215335 bps = bits_per_coded_sample;
582
583 /* codecs with a fixed packet duration */
584
11/13
✓ Branch 0 taken 12407 times.
✓ Branch 1 taken 8274 times.
✓ Branch 2 taken 600 times.
✓ Branch 3 taken 5696 times.
✓ Branch 4 taken 1124 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8595 times.
✓ Branch 7 taken 4333 times.
✓ Branch 8 taken 2186 times.
✓ Branch 9 taken 18574 times.
✓ Branch 10 taken 2515 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 151031 times.
215335 switch (id) {
585 12407 case AV_CODEC_ID_ADPCM_ADX: return 32;
586 8274 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
587 600 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
588 5696 case AV_CODEC_ID_AMR_NB:
589 case AV_CODEC_ID_EVRC:
590 case AV_CODEC_ID_GSM:
591 case AV_CODEC_ID_QCELP:
592 5696 case AV_CODEC_ID_RA_288: return 160;
593 1124 case AV_CODEC_ID_AMR_WB:
594 1124 case AV_CODEC_ID_GSM_MS: return 320;
595 case AV_CODEC_ID_MP1: return 384;
596 8595 case AV_CODEC_ID_ATRAC1: return 512;
597 4333 case AV_CODEC_ID_ATRAC9:
598 case AV_CODEC_ID_ATRAC3:
599
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4333 times.
4333 if (framecount > INT_MAX/1024)
600 return 0;
601 4333 return 1024 * framecount;
602 2186 case AV_CODEC_ID_ATRAC3P: return 2048;
603 18574 case AV_CODEC_ID_MP2:
604 18574 case AV_CODEC_ID_MUSEPACK7: return 1152;
605 2515 case AV_CODEC_ID_AC3: return 1536;
606 case AV_CODEC_ID_FTR: return 1024;
607 }
608
609
2/2
✓ Branch 0 taken 147642 times.
✓ Branch 1 taken 3389 times.
151031 if (sr > 0) {
610 /* calc from sample rate */
611
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 147625 times.
147642 if (id == AV_CODEC_ID_TTA)
612 17 return 256ll * sr / 245;
613
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 147625 times.
147625 else if (id == AV_CODEC_ID_DST)
614 return 588ll * sr / 44100;
615
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 147559 times.
147625 else if (id == AV_CODEC_ID_BINKAUDIO_DCT) {
616
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (sr / 22050 > 22)
617 return 0;
618 66 return (480 << (sr / 22050));
619 }
620
621
2/2
✓ Branch 0 taken 6703 times.
✓ Branch 1 taken 140856 times.
147559 if (id == AV_CODEC_ID_MP3)
622
2/2
✓ Branch 0 taken 224 times.
✓ Branch 1 taken 6479 times.
6703 return sr <= 24000 ? 576 : 1152;
623 }
624
625
2/2
✓ Branch 0 taken 41693 times.
✓ Branch 1 taken 102552 times.
144245 if (ba > 0) {
626 /* calc from block_align */
627
2/2
✓ Branch 0 taken 7264 times.
✓ Branch 1 taken 34429 times.
41693 if (id == AV_CODEC_ID_SIPR) {
628
4/5
✓ Branch 0 taken 3265 times.
✓ Branch 1 taken 1983 times.
✓ Branch 2 taken 1680 times.
✓ Branch 3 taken 336 times.
✗ Branch 4 not taken.
7264 switch (ba) {
629 3265 case 20: return 160;
630 1983 case 19: return 144;
631 1680 case 29: return 288;
632 336 case 37: return 480;
633 }
634
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34429 times.
34429 } else if (id == AV_CODEC_ID_ILBC) {
635 switch (ba) {
636 case 38: return 160;
637 case 50: return 240;
638 }
639 }
640 }
641
642
2/2
✓ Branch 0 taken 133521 times.
✓ Branch 1 taken 3460 times.
136981 if (frame_bytes > 0) {
643 /* calc from frame_bytes only */
644
2/2
✓ Branch 0 taken 721 times.
✓ Branch 1 taken 132800 times.
133521 if (id == AV_CODEC_ID_TRUESPEECH)
645 721 return 240 * (frame_bytes / 32);
646
2/2
✓ Branch 0 taken 2074 times.
✓ Branch 1 taken 130726 times.
132800 if (id == AV_CODEC_ID_NELLYMOSER)
647 2074 return 256 * (frame_bytes / 64);
648
2/2
✓ Branch 0 taken 1057 times.
✓ Branch 1 taken 129669 times.
130726 if (id == AV_CODEC_ID_RA_144)
649 1057 return 160 * (frame_bytes / 20);
650
2/2
✓ Branch 0 taken 460 times.
✓ Branch 1 taken 129209 times.
129669 if (id == AV_CODEC_ID_APTX)
651 460 return 4 * (frame_bytes / 4);
652
2/2
✓ Branch 0 taken 460 times.
✓ Branch 1 taken 128749 times.
129209 if (id == AV_CODEC_ID_APTX_HD)
653 460 return 4 * (frame_bytes / 6);
654
655
2/2
✓ Branch 0 taken 46000 times.
✓ Branch 1 taken 82749 times.
128749 if (bps > 0) {
656 /* calc from frame_bytes and bits_per_coded_sample */
657
3/4
✓ Branch 0 taken 45540 times.
✓ Branch 1 taken 460 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 45540 times.
46000 if (id == AV_CODEC_ID_ADPCM_G726 || id == AV_CODEC_ID_ADPCM_G726LE)
658 460 return frame_bytes * 8 / bps;
659 }
660
661
3/4
✓ Branch 0 taken 128162 times.
✓ Branch 1 taken 127 times.
✓ Branch 2 taken 128162 times.
✗ Branch 3 not taken.
128289 if (ch > 0 && ch < INT_MAX/16) {
662 /* calc from frame_bytes and channels */
663
12/17
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 33 times.
✓ Branch 4 taken 452 times.
✓ Branch 5 taken 1951 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 3580 times.
✓ Branch 8 taken 222 times.
✓ Branch 9 taken 327 times.
✓ Branch 10 taken 680 times.
✓ Branch 11 taken 112 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 314 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 1311 times.
✓ Branch 16 taken 119132 times.
128162 switch (id) {
664 case AV_CODEC_ID_FASTAUDIO:
665 return frame_bytes / (40 * ch) * 256;
666 case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
667 return (frame_bytes - 4 * ch) / (128 * ch) * 256;
668 48 case AV_CODEC_ID_ADPCM_AFC:
669 48 return frame_bytes / (9 * ch) * 16;
670 33 case AV_CODEC_ID_ADPCM_PSX:
671 case AV_CODEC_ID_ADPCM_DTK:
672 33 frame_bytes /= 16 * ch;
673
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (frame_bytes > INT_MAX / 28)
674 return 0;
675 33 return frame_bytes * 28;
676 452 case AV_CODEC_ID_ADPCM_4XM:
677 case AV_CODEC_ID_ADPCM_IMA_ACORN:
678 case AV_CODEC_ID_ADPCM_IMA_DAT4:
679 case AV_CODEC_ID_ADPCM_IMA_ISS:
680 452 return (frame_bytes - 4 * ch) * 2 / ch;
681 1951 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
682 1951 return (frame_bytes - 4) * 2 / ch;
683 case AV_CODEC_ID_ADPCM_IMA_AMV:
684 return (frame_bytes - 8) * 2;
685 3580 case AV_CODEC_ID_ADPCM_THP:
686 case AV_CODEC_ID_ADPCM_THP_LE:
687
2/2
✓ Branch 0 taken 3530 times.
✓ Branch 1 taken 50 times.
3580 if (extradata)
688 3530 return frame_bytes * 14LL / (8 * ch);
689 50 break;
690 222 case AV_CODEC_ID_ADPCM_XA:
691 222 return (frame_bytes / 128) * 224 / ch;
692 327 case AV_CODEC_ID_INTERPLAY_DPCM:
693 327 return (frame_bytes - 6 - ch) / ch;
694 680 case AV_CODEC_ID_ROQ_DPCM:
695 680 return (frame_bytes - 8) / ch;
696 112 case AV_CODEC_ID_XAN_DPCM:
697 112 return (frame_bytes - 2 * ch) / ch;
698 case AV_CODEC_ID_MACE3:
699 return 3 * frame_bytes / ch;
700 314 case AV_CODEC_ID_MACE6:
701 314 return 6 * frame_bytes / ch;
702 case AV_CODEC_ID_PCM_LXF:
703 return 2 * (frame_bytes / (5 * ch));
704 1311 case AV_CODEC_ID_IAC:
705 case AV_CODEC_ID_IMC:
706 1311 return 4 * frame_bytes / ch;
707 }
708
709
2/2
✓ Branch 0 taken 28782 times.
✓ Branch 1 taken 90400 times.
119182 if (tag) {
710 /* calc from frame_bytes, channels, and codec_tag */
711
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 28708 times.
28782 if (id == AV_CODEC_ID_SOL_DPCM) {
712
1/2
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
74 if (tag == 3)
713 74 return frame_bytes / ch;
714 else
715 return frame_bytes * 2 / ch;
716 }
717 }
718
719
2/2
✓ Branch 0 taken 29951 times.
✓ Branch 1 taken 89157 times.
119108 if (ba > 0) {
720 /* calc from frame_bytes, channels, and block_align */
721 29951 int blocks = frame_bytes / ba;
722 29951 int64_t tmp = 0;
723
7/9
✓ Branch 0 taken 182 times.
✓ Branch 1 taken 873 times.
✓ Branch 2 taken 669 times.
✓ Branch 3 taken 649 times.
✓ Branch 4 taken 1001 times.
✓ Branch 5 taken 890 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 25687 times.
29951 switch (id) {
724 182 case AV_CODEC_ID_ADPCM_IMA_XBOX:
725
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 182 times.
182 if (bps != 4)
726 return 0;
727 182 tmp = blocks * ((ba - 4 * ch) / (bps * ch) * 8);
728 182 break;
729 873 case AV_CODEC_ID_ADPCM_IMA_WAV:
730
2/4
✓ Branch 0 taken 873 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 873 times.
873 if (bps < 2 || bps > 5)
731 return 0;
732 873 tmp = blocks * (1LL + (ba - 4 * ch) / (bps * ch) * 8LL);
733 873 break;
734 669 case AV_CODEC_ID_ADPCM_IMA_DK3:
735 669 tmp = blocks * (((ba - 16LL) * 2 / 3 * 4) / ch);
736 669 break;
737 649 case AV_CODEC_ID_ADPCM_IMA_DK4:
738 649 tmp = blocks * (1 + (ba - 4LL * ch) * 2 / ch);
739 649 break;
740 1001 case AV_CODEC_ID_ADPCM_IMA_RAD:
741 1001 tmp = blocks * ((ba - 4LL * ch) * 2 / ch);
742 1001 break;
743 890 case AV_CODEC_ID_ADPCM_MS:
744 890 tmp = blocks * (2 + (ba - 7LL * ch) * 2LL / ch);
745 890 break;
746 case AV_CODEC_ID_ADPCM_MTAF:
747 tmp = blocks * (ba - 16LL) * 2 / ch;
748 break;
749 case AV_CODEC_ID_ADPCM_XMD:
750 tmp = blocks * 32;
751 break;
752 }
753
2/2
✓ Branch 0 taken 4264 times.
✓ Branch 1 taken 25687 times.
29951 if (tmp) {
754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4264 times.
4264 if (tmp != (int)tmp)
755 return 0;
756 4264 return tmp;
757 }
758 }
759
760
2/2
✓ Branch 0 taken 37341 times.
✓ Branch 1 taken 77503 times.
114844 if (bps > 0) {
761 /* calc from frame_bytes, channels, and bits_per_coded_sample */
762
2/4
✓ Branch 0 taken 20831 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16510 times.
37341 switch (id) {
763 20831 case AV_CODEC_ID_PCM_DVD:
764
2/4
✓ Branch 0 taken 20831 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20831 times.
20831 if(bps<4 || frame_bytes<3)
765 return 0;
766 20831 return 2 * ((frame_bytes - 3) / ((bps * 2 / 8) * ch));
767 case AV_CODEC_ID_PCM_BLURAY:
768 if(bps<4 || frame_bytes<4)
769 return 0;
770 return (frame_bytes - 4) / ((FFALIGN(ch, 2) * bps) / 8);
771 case AV_CODEC_ID_S302M:
772 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
773 }
774 }
775 }
776 }
777
778 /* Fall back on using frame_size */
779
4/4
✓ Branch 0 taken 53730 times.
✓ Branch 1 taken 43870 times.
✓ Branch 2 taken 53569 times.
✓ Branch 3 taken 161 times.
97600 if (frame_size > 1 && frame_bytes)
780 53569 return frame_size;
781
782 //For WMA we currently have no other means to calculate duration thus we
783 //do it here by assuming CBR, which is true for all known cases.
784
8/8
✓ Branch 0 taken 29138 times.
✓ Branch 1 taken 14893 times.
✓ Branch 2 taken 25694 times.
✓ Branch 3 taken 3444 times.
✓ Branch 4 taken 25663 times.
✓ Branch 5 taken 31 times.
✓ Branch 6 taken 9699 times.
✓ Branch 7 taken 15964 times.
44031 if (bitrate > 0 && frame_bytes > 0 && sr > 0 && ba > 1) {
785
4/4
✓ Branch 0 taken 9494 times.
✓ Branch 1 taken 205 times.
✓ Branch 2 taken 795 times.
✓ Branch 3 taken 8699 times.
9699 if (id == AV_CODEC_ID_WMAV1 || id == AV_CODEC_ID_WMAV2)
786 1000 return (frame_bytes * 8LL * sr) / bitrate;
787 }
788
789 43031 return 0;
790 }
791
792 45149 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
793 {
794 45149 int channels = avctx->ch_layout.nb_channels;
795 int duration;
796
797 45149 duration = get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
798 channels, avctx->block_align,
799 avctx->codec_tag, avctx->bits_per_coded_sample,
800 avctx->bit_rate, avctx->extradata, avctx->frame_size,
801 frame_bytes);
802 45149 return FFMAX(0, duration);
803 }
804
805 468530 int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
806 {
807 468530 int channels = par->ch_layout.nb_channels;
808 int duration;
809
810 468530 duration = get_audio_frame_duration(par->codec_id, par->sample_rate,
811 channels, par->block_align,
812 par->codec_tag, par->bits_per_coded_sample,
813 par->bit_rate, par->extradata, par->frame_size,
814 frame_bytes);
815 468530 return FFMAX(0, duration);
816 }
817
818 56 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
819 {
820 56 unsigned int n = 0;
821
822
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 while (v >= 0xff) {
823 *s++ = 0xff;
824 v -= 0xff;
825 n++;
826 }
827 56 *s = v;
828 56 n++;
829 56 return n;
830 }
831
832 476 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
833 {
834 int i;
835
5/6
✓ Branch 0 taken 1919 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 1448 times.
✓ Branch 3 taken 471 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 471 times.
1924 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
836 476 return i;
837 }
838
839 16169 const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *avcodec, int index)
840 {
841 16169 const FFCodec *const codec = ffcodec(avcodec);
842 int i;
843
3/4
✓ Branch 0 taken 2639 times.
✓ Branch 1 taken 13530 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2639 times.
16169 if (!codec->hw_configs || index < 0)
844 13530 return NULL;
845
2/2
✓ Branch 0 taken 5139 times.
✓ Branch 1 taken 1703 times.
6842 for (i = 0; i <= index; i++)
846
2/2
✓ Branch 0 taken 936 times.
✓ Branch 1 taken 4203 times.
5139 if (!codec->hw_configs[i])
847 936 return NULL;
848 1703 return &codec->hw_configs[index]->public;
849 }
850
851 27463 int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src)
852 {
853 int ret;
854
855 27463 dst->owner[0] = src->owner[0];
856 27463 dst->owner[1] = src->owner[1];
857
858 27463 ret = av_frame_ref(dst->f, src->f);
859
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27463 times.
27463 if (ret < 0)
860 return ret;
861
862
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27463 times.
27463 av_assert0(!dst->progress);
863
864
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 27403 times.
27463 if (src->progress)
865 60 dst->progress = av_refstruct_ref(src->progress);
866
867 27463 return 0;
868 }
869
870 298 int ff_thread_replace_frame(ThreadFrame *dst, const ThreadFrame *src)
871 {
872 int ret;
873
874 298 dst->owner[0] = src->owner[0];
875 298 dst->owner[1] = src->owner[1];
876
877 298 ret = av_frame_replace(dst->f, src->f);
878
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 298 times.
298 if (ret < 0)
879 return ret;
880
881 298 av_refstruct_replace(&dst->progress, src->progress);
882
883 298 return 0;
884 }
885
886 #if !HAVE_THREADS
887
888 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
889 {
890 return ff_get_buffer(avctx, f, flags);
891 }
892
893 int ff_thread_get_ext_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
894 {
895 f->owner[0] = f->owner[1] = avctx;
896 return ff_get_buffer(avctx, f->f, flags);
897 }
898
899 void ff_thread_release_ext_buffer(ThreadFrame *f)
900 {
901 f->owner[0] = f->owner[1] = NULL;
902 if (f->f)
903 av_frame_unref(f->f);
904 }
905
906 void ff_thread_finish_setup(AVCodecContext *avctx)
907 {
908 }
909
910 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
911 {
912 }
913
914 void ff_thread_await_progress(const ThreadFrame *f, int progress, int field)
915 {
916 }
917
918 int ff_thread_can_start_frame(AVCodecContext *avctx)
919 {
920 return 1;
921 }
922 #endif
923
924 664391 const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
925 const uint8_t *end,
926 uint32_t *restrict state)
927 {
928 int i;
929
930
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 664391 times.
664391 av_assert0(p <= end);
931
2/2
✓ Branch 0 taken 913 times.
✓ Branch 1 taken 663478 times.
664391 if (p >= end)
932 913 return end;
933
934
2/2
✓ Branch 0 taken 1984820 times.
✓ Branch 1 taken 659580 times.
2644400 for (i = 0; i < 3; i++) {
935 1984820 uint32_t tmp = *state << 8;
936 1984820 *state = tmp + *(p++);
937
4/4
✓ Branch 0 taken 1983954 times.
✓ Branch 1 taken 866 times.
✓ Branch 2 taken 3032 times.
✓ Branch 3 taken 1980922 times.
1984820 if (tmp == 0x100 || p == end)
938 3898 return p;
939 }
940
941
2/2
✓ Branch 0 taken 390225082 times.
✓ Branch 1 taken 66918 times.
390292000 while (p < end) {
942
2/2
✓ Branch 0 taken 353054928 times.
✓ Branch 1 taken 37170154 times.
390225082 if (p[-1] > 1 ) p += 3;
943
2/2
✓ Branch 0 taken 15964159 times.
✓ Branch 1 taken 21205995 times.
37170154 else if (p[-2] ) p += 2;
944
2/2
✓ Branch 0 taken 20613333 times.
✓ Branch 1 taken 592662 times.
21205995 else if (p[-3]|(p[-1]-1)) p++;
945 else {
946 592662 p++;
947 592662 break;
948 }
949 }
950
951
2/2
✓ Branch 0 taken 41998 times.
✓ Branch 1 taken 617582 times.
659580 p = FFMIN(p, end) - 4;
952 659580 *state = AV_RB32(p);
953
954 659580 return p + 4;
955 }
956
957 413 AVCPBProperties *av_cpb_properties_alloc(size_t *size)
958 {
959 413 AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
960
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 413 times.
413 if (!props)
961 return NULL;
962
963
1/2
✓ Branch 0 taken 413 times.
✗ Branch 1 not taken.
413 if (size)
964 413 *size = sizeof(*props);
965
966 413 props->vbv_delay = UINT64_MAX;
967
968 413 return props;
969 }
970
971 static unsigned bcd2uint(uint8_t bcd)
972 {
973 unsigned low = bcd & 0xf;
974 unsigned high = bcd >> 4;
975 if (low > 9 || high > 9)
976 return 0;
977 return low + 10*high;
978 }
979
980 int ff_alloc_timecode_sei(const AVFrame *frame, AVRational rate, size_t prefix_len,
981 void **data, size_t *sei_size)
982 {
983 AVFrameSideData *sd = NULL;
984 uint8_t *sei_data;
985 PutBitContext pb;
986 uint32_t *tc;
987 int m;
988
989 if (frame)
990 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_S12M_TIMECODE);
991
992 if (!sd) {
993 *data = NULL;
994 return 0;
995 }
996 tc = (uint32_t*)sd->data;
997 m = tc[0] & 3;
998
999 *sei_size = sizeof(uint32_t) * 4;
1000 *data = av_mallocz(*sei_size + prefix_len);
1001 if (!*data)
1002 return AVERROR(ENOMEM);
1003 sei_data = (uint8_t*)*data + prefix_len;
1004
1005 init_put_bits(&pb, sei_data, *sei_size);
1006 put_bits(&pb, 2, m); // num_clock_ts
1007
1008 for (int j = 1; j <= m; j++) {
1009 uint32_t tcsmpte = tc[j];
1010 unsigned hh = bcd2uint(tcsmpte & 0x3f); // 6-bit hours
1011 unsigned mm = bcd2uint(tcsmpte>>8 & 0x7f); // 7-bit minutes
1012 unsigned ss = bcd2uint(tcsmpte>>16 & 0x7f); // 7-bit seconds
1013 unsigned ff = bcd2uint(tcsmpte>>24 & 0x3f); // 6-bit frames
1014 unsigned drop = tcsmpte & 1<<30 && !0; // 1-bit drop if not arbitrary bit
1015
1016 /* Calculate frame number of HEVC by SMPTE ST 12-1:2014 Sec 12.2 if rate > 30FPS */
1017 if (av_cmp_q(rate, (AVRational) {30, 1}) == 1) {
1018 unsigned pc;
1019 ff *= 2;
1020 if (av_cmp_q(rate, (AVRational) {50, 1}) == 0)
1021 pc = !!(tcsmpte & 1 << 7);
1022 else
1023 pc = !!(tcsmpte & 1 << 23);
1024 ff = (ff + pc) & 0x7f;
1025 }
1026
1027 put_bits(&pb, 1, 1); // clock_timestamp_flag
1028 put_bits(&pb, 1, 1); // units_field_based_flag
1029 put_bits(&pb, 5, 0); // counting_type
1030 put_bits(&pb, 1, 1); // full_timestamp_flag
1031 put_bits(&pb, 1, 0); // discontinuity_flag
1032 put_bits(&pb, 1, drop);
1033 put_bits(&pb, 9, ff);
1034 put_bits(&pb, 6, ss);
1035 put_bits(&pb, 6, mm);
1036 put_bits(&pb, 5, hh);
1037 put_bits(&pb, 5, 0);
1038 }
1039 flush_put_bits(&pb);
1040
1041 return 0;
1042 }
1043
1044 18612 int64_t ff_guess_coded_bitrate(AVCodecContext *avctx)
1045 {
1046 18612 AVRational framerate = avctx->framerate;
1047 18612 int bits_per_coded_sample = avctx->bits_per_coded_sample;
1048 int64_t bitrate;
1049
1050
3/4
✓ Branch 0 taken 18493 times.
✓ Branch 1 taken 119 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18493 times.
18612 if (!(framerate.num && framerate.den))
1051 119 framerate = av_inv_q(avctx->time_base);
1052
2/4
✓ Branch 0 taken 18612 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18612 times.
18612 if (!(framerate.num && framerate.den))
1053 return 0;
1054
1055
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18612 times.
18612 if (!bits_per_coded_sample) {
1056 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1057 bits_per_coded_sample = av_get_bits_per_pixel(desc);
1058 }
1059 18612 bitrate = (int64_t)bits_per_coded_sample * avctx->width * avctx->height *
1060 18612 framerate.num / framerate.den;
1061
1062 18612 return bitrate;
1063 }
1064