/src/ffmpeg/libavcodec/utils.c
Line | Count | 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 "libavutil/timecode_internal.h" |
37 | | #include "avcodec.h" |
38 | | #include "codec.h" |
39 | | #include "codec_desc.h" |
40 | | #include "codec_internal.h" |
41 | | #include "codec_par.h" |
42 | | #include "decode.h" |
43 | | #include "hwconfig.h" |
44 | | #include "libavutil/refstruct.h" |
45 | | #include "thread.h" |
46 | | #include "threadframe.h" |
47 | | #include "internal.h" |
48 | | #include "put_bits.h" |
49 | | #include "startcode.h" |
50 | | #include <stdlib.h> |
51 | | #include <limits.h> |
52 | | |
53 | | void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size) |
54 | 28.3M | { |
55 | 28.3M | uint8_t **p = ptr; |
56 | 28.3M | if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) { |
57 | 0 | av_freep(p); |
58 | 0 | *size = 0; |
59 | 0 | return; |
60 | 0 | } |
61 | 28.3M | av_fast_mallocz(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE); |
62 | 28.3M | if (*p) |
63 | 28.3M | memset(*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); |
64 | 28.3M | } |
65 | | |
66 | | void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size) |
67 | 156k | { |
68 | 156k | uint8_t **p = ptr; |
69 | 156k | if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) { |
70 | 0 | av_freep(p); |
71 | 0 | *size = 0; |
72 | 0 | return; |
73 | 0 | } |
74 | 156k | av_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE); |
75 | 156k | if (*p) |
76 | 156k | memset(*p, 0, min_size + AV_INPUT_BUFFER_PADDING_SIZE); |
77 | 156k | } |
78 | | |
79 | | int av_codec_is_encoder(const AVCodec *avcodec) |
80 | 97.0M | { |
81 | 97.0M | const FFCodec *const codec = ffcodec(avcodec); |
82 | 97.0M | return codec && !codec->is_decoder; |
83 | 97.0M | } |
84 | | |
85 | | int av_codec_is_decoder(const AVCodec *avcodec) |
86 | 204M | { |
87 | 204M | const FFCodec *const codec = ffcodec(avcodec); |
88 | 204M | return codec && codec->is_decoder; |
89 | 204M | } |
90 | | |
91 | | int ff_set_dimensions(AVCodecContext *s, int width, int height) |
92 | 14.6M | { |
93 | 14.6M | int ret = av_image_check_size2(width, height, s->max_pixels, AV_PIX_FMT_NONE, 0, s); |
94 | | |
95 | 14.6M | if (ret < 0) |
96 | 418k | width = height = 0; |
97 | | |
98 | 14.6M | s->coded_width = width; |
99 | 14.6M | s->coded_height = height; |
100 | 14.6M | s->width = AV_CEIL_RSHIFT(width, s->lowres); |
101 | 14.6M | s->height = AV_CEIL_RSHIFT(height, s->lowres); |
102 | | |
103 | 14.6M | return ret; |
104 | 14.6M | } |
105 | | |
106 | | int ff_set_sar(AVCodecContext *avctx, AVRational sar) |
107 | 804k | { |
108 | 804k | int ret = av_image_check_sar(avctx->width, avctx->height, sar); |
109 | | |
110 | 804k | if (ret < 0) { |
111 | 35.2k | av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n", |
112 | 35.2k | sar.num, sar.den); |
113 | 35.2k | avctx->sample_aspect_ratio = (AVRational){ 0, 1 }; |
114 | 35.2k | return ret; |
115 | 769k | } else { |
116 | 769k | avctx->sample_aspect_ratio = sar; |
117 | 769k | } |
118 | 769k | return 0; |
119 | 804k | } |
120 | | |
121 | | int ff_side_data_update_matrix_encoding(AVFrame *frame, |
122 | | enum AVMatrixEncoding matrix_encoding) |
123 | 231k | { |
124 | 231k | AVFrameSideData *side_data; |
125 | 231k | enum AVMatrixEncoding *data; |
126 | | |
127 | 231k | side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING); |
128 | 231k | if (!side_data) |
129 | 231k | side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING, |
130 | 231k | sizeof(enum AVMatrixEncoding)); |
131 | | |
132 | 231k | if (!side_data) |
133 | 0 | return AVERROR(ENOMEM); |
134 | | |
135 | 231k | data = (enum AVMatrixEncoding*)side_data->data; |
136 | 231k | *data = matrix_encoding; |
137 | | |
138 | 231k | return 0; |
139 | 231k | } |
140 | | |
141 | | void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, |
142 | | int linesize_align[AV_NUM_DATA_POINTERS]) |
143 | 34.9M | { |
144 | 34.9M | int i; |
145 | 34.9M | int w_align = 1; |
146 | 34.9M | int h_align = 1; |
147 | 34.9M | AVPixFmtDescriptor const *desc = av_pix_fmt_desc_get(s->pix_fmt); |
148 | | |
149 | 34.9M | if (desc) { |
150 | 34.9M | w_align = 1 << desc->log2_chroma_w; |
151 | 34.9M | h_align = 1 << desc->log2_chroma_h; |
152 | 34.9M | } |
153 | | |
154 | 34.9M | switch (s->pix_fmt) { |
155 | 7.37M | case AV_PIX_FMT_YUV420P: |
156 | 7.65M | case AV_PIX_FMT_YUYV422: |
157 | 7.65M | case AV_PIX_FMT_YVYU422: |
158 | 8.30M | case AV_PIX_FMT_UYVY422: |
159 | 9.26M | case AV_PIX_FMT_YUV422P: |
160 | 9.32M | case AV_PIX_FMT_YUV440P: |
161 | 10.0M | case AV_PIX_FMT_YUV444P: |
162 | 10.2M | case AV_PIX_FMT_GBRP: |
163 | 10.4M | case AV_PIX_FMT_GBRAP: |
164 | 11.3M | case AV_PIX_FMT_GRAY8: |
165 | 11.4M | case AV_PIX_FMT_GRAY16BE: |
166 | 11.6M | case AV_PIX_FMT_GRAY16LE: |
167 | 12.5M | case AV_PIX_FMT_YUVJ420P: |
168 | 12.6M | case AV_PIX_FMT_YUVJ422P: |
169 | 12.7M | case AV_PIX_FMT_YUVJ440P: |
170 | 12.8M | case AV_PIX_FMT_YUVJ444P: |
171 | 13.0M | case AV_PIX_FMT_YUVA420P: |
172 | 13.1M | case AV_PIX_FMT_YUVA422P: |
173 | 13.2M | case AV_PIX_FMT_YUVA444P: |
174 | 13.4M | case AV_PIX_FMT_YUV420P9LE: |
175 | 13.4M | case AV_PIX_FMT_YUV420P9BE: |
176 | 13.7M | case AV_PIX_FMT_YUV420P10LE: |
177 | 13.7M | case AV_PIX_FMT_YUV420P10BE: |
178 | 14.0M | case AV_PIX_FMT_YUV420P12LE: |
179 | 14.0M | case AV_PIX_FMT_YUV420P12BE: |
180 | 14.3M | case AV_PIX_FMT_YUV420P14LE: |
181 | 14.3M | case AV_PIX_FMT_YUV420P14BE: |
182 | 14.3M | case AV_PIX_FMT_YUV420P16LE: |
183 | 14.3M | case AV_PIX_FMT_YUV420P16BE: |
184 | 14.3M | case AV_PIX_FMT_YUVA420P9LE: |
185 | 14.3M | case AV_PIX_FMT_YUVA420P9BE: |
186 | 14.3M | case AV_PIX_FMT_YUVA420P10LE: |
187 | 14.3M | case AV_PIX_FMT_YUVA420P10BE: |
188 | 14.3M | case AV_PIX_FMT_YUVA420P16LE: |
189 | 14.3M | case AV_PIX_FMT_YUVA420P16BE: |
190 | 14.8M | case AV_PIX_FMT_YUV422P9LE: |
191 | 14.8M | case AV_PIX_FMT_YUV422P9BE: |
192 | 15.0M | case AV_PIX_FMT_YUV422P10LE: |
193 | 15.0M | case AV_PIX_FMT_YUV422P10BE: |
194 | 15.3M | case AV_PIX_FMT_YUV422P12LE: |
195 | 15.3M | case AV_PIX_FMT_YUV422P12BE: |
196 | 15.5M | case AV_PIX_FMT_YUV422P14LE: |
197 | 15.5M | case AV_PIX_FMT_YUV422P14BE: |
198 | 15.9M | case AV_PIX_FMT_YUV422P16LE: |
199 | 15.9M | case AV_PIX_FMT_YUV422P16BE: |
200 | 15.9M | case AV_PIX_FMT_YUVA422P9LE: |
201 | 15.9M | case AV_PIX_FMT_YUVA422P9BE: |
202 | 15.9M | case AV_PIX_FMT_YUVA422P10LE: |
203 | 15.9M | case AV_PIX_FMT_YUVA422P10BE: |
204 | 15.9M | case AV_PIX_FMT_YUVA422P12LE: |
205 | 15.9M | case AV_PIX_FMT_YUVA422P12BE: |
206 | 15.9M | case AV_PIX_FMT_YUVA422P16LE: |
207 | 15.9M | case AV_PIX_FMT_YUVA422P16BE: |
208 | 15.9M | case AV_PIX_FMT_YUV440P10LE: |
209 | 15.9M | case AV_PIX_FMT_YUV440P10BE: |
210 | 16.0M | case AV_PIX_FMT_YUV440P12LE: |
211 | 16.0M | case AV_PIX_FMT_YUV440P12BE: |
212 | 16.1M | case AV_PIX_FMT_YUV444P9LE: |
213 | 16.1M | case AV_PIX_FMT_YUV444P9BE: |
214 | 16.2M | case AV_PIX_FMT_YUV444P10LE: |
215 | 16.2M | case AV_PIX_FMT_YUV444P10BE: |
216 | 16.7M | case AV_PIX_FMT_YUV444P12LE: |
217 | 16.7M | case AV_PIX_FMT_YUV444P12BE: |
218 | 16.8M | case AV_PIX_FMT_YUV444P14LE: |
219 | 16.8M | case AV_PIX_FMT_YUV444P14BE: |
220 | 16.9M | case AV_PIX_FMT_YUV444P16LE: |
221 | 16.9M | case AV_PIX_FMT_YUV444P16BE: |
222 | 16.9M | case AV_PIX_FMT_YUVA444P9LE: |
223 | 16.9M | case AV_PIX_FMT_YUVA444P9BE: |
224 | 16.9M | case AV_PIX_FMT_YUVA444P10LE: |
225 | 16.9M | case AV_PIX_FMT_YUVA444P10BE: |
226 | 17.0M | case AV_PIX_FMT_YUVA444P12LE: |
227 | 17.0M | case AV_PIX_FMT_YUVA444P12BE: |
228 | 17.0M | case AV_PIX_FMT_YUVA444P16LE: |
229 | 17.0M | case AV_PIX_FMT_YUVA444P16BE: |
230 | 17.0M | case AV_PIX_FMT_GBRP9LE: |
231 | 17.0M | case AV_PIX_FMT_GBRP9BE: |
232 | 17.2M | case AV_PIX_FMT_GBRP10LE: |
233 | 17.2M | case AV_PIX_FMT_GBRP10BE: |
234 | 17.2M | case AV_PIX_FMT_GBRP12LE: |
235 | 17.2M | case AV_PIX_FMT_GBRP12BE: |
236 | 17.2M | case AV_PIX_FMT_GBRP14LE: |
237 | 17.2M | case AV_PIX_FMT_GBRP14BE: |
238 | 17.2M | case AV_PIX_FMT_GBRP16LE: |
239 | 17.2M | case AV_PIX_FMT_GBRP16BE: |
240 | 17.3M | case AV_PIX_FMT_GBRAP12LE: |
241 | 17.3M | case AV_PIX_FMT_GBRAP12BE: |
242 | 17.3M | case AV_PIX_FMT_GBRAP16LE: |
243 | 17.3M | case AV_PIX_FMT_GBRAP16BE: |
244 | 17.3M | w_align = 16; //FIXME assume 16 pixel per macroblock |
245 | 17.3M | h_align = 16 * 2; // interlaced needs 2 macroblocks height |
246 | 17.3M | if (s->codec_id == AV_CODEC_ID_BINKVIDEO) |
247 | 67.5k | w_align = 16*2; |
248 | 17.3M | break; |
249 | 634k | case AV_PIX_FMT_YUV411P: |
250 | 643k | case AV_PIX_FMT_YUVJ411P: |
251 | 643k | case AV_PIX_FMT_UYYVYY411: |
252 | 643k | w_align = 32; |
253 | 643k | h_align = 16 * 2; |
254 | 643k | break; |
255 | 607k | case AV_PIX_FMT_YUV410P: |
256 | 607k | if (s->codec_id == AV_CODEC_ID_SVQ1) { |
257 | 39.7k | w_align = 64; |
258 | 39.7k | h_align = 64; |
259 | 567k | } else if (s->codec_id == AV_CODEC_ID_SNOW) { |
260 | 2.26k | w_align = 16; |
261 | 2.26k | h_align = 16; |
262 | 2.26k | } |
263 | 607k | break; |
264 | 727k | case AV_PIX_FMT_RGB555: |
265 | 727k | if (s->codec_id == AV_CODEC_ID_RPZA) { |
266 | 53.9k | w_align = 4; |
267 | 53.9k | h_align = 4; |
268 | 53.9k | } |
269 | 727k | if (s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) { |
270 | 12.2k | w_align = 8; |
271 | 12.2k | h_align = 8; |
272 | 12.2k | } |
273 | 727k | break; |
274 | 7.55M | case AV_PIX_FMT_PAL8: |
275 | 7.66M | case AV_PIX_FMT_BGR8: |
276 | 7.66M | case AV_PIX_FMT_RGB8: |
277 | 7.66M | if (s->codec_id == AV_CODEC_ID_SMC || |
278 | 7.55M | s->codec_id == AV_CODEC_ID_CINEPAK) { |
279 | 110k | w_align = 4; |
280 | 110k | h_align = 4; |
281 | 110k | } |
282 | 7.66M | if (s->codec_id == AV_CODEC_ID_JV || |
283 | 7.63M | s->codec_id == AV_CODEC_ID_ARGO || |
284 | 7.56M | s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) { |
285 | 247k | w_align = 8; |
286 | 247k | h_align = 8; |
287 | 247k | } |
288 | 7.66M | if (s->codec_id == AV_CODEC_ID_MJPEG || |
289 | 7.65M | s->codec_id == AV_CODEC_ID_MJPEGB || |
290 | 7.65M | s->codec_id == AV_CODEC_ID_LJPEG || |
291 | 7.65M | s->codec_id == AV_CODEC_ID_SMVJPEG || |
292 | 7.64M | s->codec_id == AV_CODEC_ID_AMV || |
293 | 7.63M | s->codec_id == AV_CODEC_ID_SP5X || |
294 | 7.63M | s->codec_id == AV_CODEC_ID_JPEGLS) { |
295 | 36.2k | w_align = 8; |
296 | 36.2k | h_align = 2*8; |
297 | 36.2k | } |
298 | 7.66M | break; |
299 | 749k | case AV_PIX_FMT_BGR24: |
300 | 749k | if ((s->codec_id == AV_CODEC_ID_MSZH) || |
301 | 744k | (s->codec_id == AV_CODEC_ID_ZLIB)) { |
302 | 28.1k | w_align = 4; |
303 | 28.1k | h_align = 4; |
304 | 28.1k | } |
305 | 749k | break; |
306 | 665k | case AV_PIX_FMT_RGB24: |
307 | 665k | if (s->codec_id == AV_CODEC_ID_CINEPAK) { |
308 | 8.10k | w_align = 4; |
309 | 8.10k | h_align = 4; |
310 | 8.10k | } |
311 | 665k | break; |
312 | 421k | case AV_PIX_FMT_BGR0: |
313 | 421k | if (s->codec_id == AV_CODEC_ID_ARGO) { |
314 | 9.18k | w_align = 8; |
315 | 9.18k | h_align = 8; |
316 | 9.18k | } |
317 | 421k | break; |
318 | 4.08k | case AV_PIX_FMT_BAYER_BGGR8: |
319 | 4.76k | case AV_PIX_FMT_BAYER_RGGB8: |
320 | 4.92k | case AV_PIX_FMT_BAYER_GBRG8: |
321 | 5.18k | case AV_PIX_FMT_BAYER_GRBG8: |
322 | 42.7k | case AV_PIX_FMT_BAYER_BGGR16LE: |
323 | 42.7k | case AV_PIX_FMT_BAYER_BGGR16BE: |
324 | 69.1k | case AV_PIX_FMT_BAYER_RGGB16LE: |
325 | 69.1k | case AV_PIX_FMT_BAYER_RGGB16BE: |
326 | 130k | case AV_PIX_FMT_BAYER_GBRG16LE: |
327 | 130k | case AV_PIX_FMT_BAYER_GBRG16BE: |
328 | 342k | case AV_PIX_FMT_BAYER_GRBG16LE: |
329 | 342k | case AV_PIX_FMT_BAYER_GRBG16BE: |
330 | 342k | w_align = FFMAX(w_align, 2); |
331 | 342k | h_align = FFMAX(h_align, 2); |
332 | 342k | break; |
333 | 5.81M | default: |
334 | 5.81M | break; |
335 | 34.9M | } |
336 | | |
337 | 34.9M | if (s->codec_id == AV_CODEC_ID_IFF_ILBM) { |
338 | 107k | w_align = FFMAX(w_align, 16); |
339 | 107k | } |
340 | | |
341 | 34.9M | *width = FFALIGN(*width, w_align); |
342 | 34.9M | *height = FFALIGN(*height, h_align); |
343 | 34.9M | if (s->codec_id == AV_CODEC_ID_H264 || s->lowres || |
344 | 30.5M | s->codec_id == AV_CODEC_ID_VC1 || s->codec_id == AV_CODEC_ID_WMV3 || |
345 | 29.9M | s->codec_id == AV_CODEC_ID_VP5 || s->codec_id == AV_CODEC_ID_VP6 || |
346 | 29.7M | s->codec_id == AV_CODEC_ID_VP6F || s->codec_id == AV_CODEC_ID_VP6A |
347 | 34.9M | ) { |
348 | | // some of the optimized chroma MC reads one line too much |
349 | | // which is also done in mpeg decoders with lowres > 0 |
350 | 5.39M | *height += 2; |
351 | | |
352 | | // H.264 uses edge emulation for out of frame motion vectors, for this |
353 | | // it requires a temporary area large enough to hold a 21x21 block, |
354 | | // increasing width ensure that the temporary area is large enough, |
355 | | // the next rounded up width is 32 |
356 | 5.39M | *width = FFMAX(*width, 32); |
357 | 5.39M | } |
358 | 34.9M | if (s->codec_id == AV_CODEC_ID_SVQ3) { |
359 | 142k | *width = FFMAX(*width, 32); |
360 | 142k | } |
361 | | |
362 | 174M | for (i = 0; i < 4; i++) |
363 | 139M | linesize_align[i] = STRIDE_ALIGN; |
364 | 34.9M | } |
365 | | |
366 | | void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height) |
367 | 0 | { |
368 | 0 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt); |
369 | 0 | int chroma_shift = desc->log2_chroma_w; |
370 | 0 | int linesize_align[AV_NUM_DATA_POINTERS]; |
371 | 0 | int align; |
372 | |
|
373 | 0 | avcodec_align_dimensions2(s, width, height, linesize_align); |
374 | 0 | align = FFMAX(linesize_align[0], linesize_align[3]); |
375 | 0 | linesize_align[1] <<= chroma_shift; |
376 | 0 | linesize_align[2] <<= chroma_shift; |
377 | 0 | align = FFMAX3(align, linesize_align[1], linesize_align[2]); |
378 | 0 | *width = FFALIGN(*width, align); |
379 | 0 | } |
380 | | |
381 | | int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels, |
382 | | enum AVSampleFormat sample_fmt, const uint8_t *buf, |
383 | | int buf_size, int align) |
384 | 0 | { |
385 | 0 | int ch, planar, needed_size, ret = 0; |
386 | |
|
387 | 0 | needed_size = av_samples_get_buffer_size(NULL, nb_channels, |
388 | 0 | frame->nb_samples, sample_fmt, |
389 | 0 | align); |
390 | 0 | if (buf_size < needed_size) |
391 | 0 | return AVERROR(EINVAL); |
392 | | |
393 | 0 | planar = av_sample_fmt_is_planar(sample_fmt); |
394 | 0 | if (planar && nb_channels > AV_NUM_DATA_POINTERS) { |
395 | 0 | if (!FF_ALLOCZ_TYPED_ARRAY(frame->extended_data, nb_channels)) |
396 | 0 | return AVERROR(ENOMEM); |
397 | 0 | } else { |
398 | 0 | frame->extended_data = frame->data; |
399 | 0 | } |
400 | | |
401 | 0 | if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0], |
402 | 0 | (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples, |
403 | 0 | sample_fmt, align)) < 0) { |
404 | 0 | if (frame->extended_data != frame->data) |
405 | 0 | av_freep(&frame->extended_data); |
406 | 0 | return ret; |
407 | 0 | } |
408 | 0 | if (frame->extended_data != frame->data) { |
409 | 0 | for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++) |
410 | 0 | frame->data[ch] = frame->extended_data[ch]; |
411 | 0 | } |
412 | |
|
413 | 0 | return ret; |
414 | 0 | } |
415 | | |
416 | | |
417 | 0 | int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec){ |
418 | 0 | return !!(ffcodec(codec)->caps_internal & FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM); |
419 | 0 | } |
420 | | |
421 | | const char *avcodec_get_name(enum AVCodecID id) |
422 | 2.43M | { |
423 | 2.43M | const AVCodecDescriptor *cd; |
424 | | |
425 | 2.43M | if (id == AV_CODEC_ID_NONE) |
426 | 849k | return "none"; |
427 | 1.58M | cd = avcodec_descriptor_get(id); |
428 | 1.58M | if (cd) |
429 | 1.58M | return cd->name; |
430 | 282 | return "unknown_codec"; |
431 | 1.58M | } |
432 | | |
433 | | const char *av_get_profile_name(const AVCodec *codec, int profile) |
434 | 0 | { |
435 | 0 | const AVProfile *p; |
436 | 0 | if (profile == AV_PROFILE_UNKNOWN || !codec->profiles) |
437 | 0 | return NULL; |
438 | | |
439 | 0 | for (p = codec->profiles; p->profile != AV_PROFILE_UNKNOWN; p++) |
440 | 0 | if (p->profile == profile) |
441 | 0 | return p->name; |
442 | | |
443 | 0 | return NULL; |
444 | 0 | } |
445 | | |
446 | | const char *avcodec_profile_name(enum AVCodecID codec_id, int profile) |
447 | 2.07M | { |
448 | 2.07M | const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id); |
449 | 2.07M | const AVProfile *p; |
450 | | |
451 | 2.07M | if (profile == AV_PROFILE_UNKNOWN || !desc || !desc->profiles) |
452 | 1.99M | return NULL; |
453 | | |
454 | 137k | for (p = desc->profiles; p->profile != AV_PROFILE_UNKNOWN; p++) |
455 | 136k | if (p->profile == profile) |
456 | 79.2k | return p->name; |
457 | | |
458 | 1.39k | return NULL; |
459 | 80.6k | } |
460 | | |
461 | | int av_get_exact_bits_per_sample(enum AVCodecID codec_id) |
462 | 68.0M | { |
463 | 68.0M | switch (codec_id) { |
464 | 55.0k | case AV_CODEC_ID_DFPWM: |
465 | 55.0k | return 1; |
466 | 16.1k | case AV_CODEC_ID_8SVX_EXP: |
467 | 35.8k | case AV_CODEC_ID_8SVX_FIB: |
468 | 37.8k | case AV_CODEC_ID_ADPCM_ARGO: |
469 | 2.21M | case AV_CODEC_ID_ADPCM_CT: |
470 | 2.21M | case AV_CODEC_ID_ADPCM_IMA_ALP: |
471 | 2.21M | case AV_CODEC_ID_ADPCM_IMA_AMV: |
472 | 2.26M | case AV_CODEC_ID_ADPCM_IMA_APC: |
473 | 2.26M | case AV_CODEC_ID_ADPCM_IMA_APM: |
474 | 2.26M | case AV_CODEC_ID_ADPCM_IMA_EA_SEAD: |
475 | 2.26M | case AV_CODEC_ID_ADPCM_IMA_MAGIX: |
476 | 2.28M | case AV_CODEC_ID_ADPCM_IMA_OKI: |
477 | 2.73M | case AV_CODEC_ID_ADPCM_IMA_WS: |
478 | 2.73M | case AV_CODEC_ID_ADPCM_IMA_SSI: |
479 | 3.13M | case AV_CODEC_ID_ADPCM_G722: |
480 | 3.15M | case AV_CODEC_ID_ADPCM_YAMAHA: |
481 | 3.57M | case AV_CODEC_ID_ADPCM_AICA: |
482 | 3.57M | return 4; |
483 | 61 | case AV_CODEC_ID_DSD_LSBF: |
484 | 592k | case AV_CODEC_ID_DSD_MSBF: |
485 | 596k | case AV_CODEC_ID_DSD_LSBF_PLANAR: |
486 | 596k | case AV_CODEC_ID_DSD_MSBF_PLANAR: |
487 | 934k | case AV_CODEC_ID_PCM_ALAW: |
488 | 1.09M | case AV_CODEC_ID_PCM_MULAW: |
489 | 1.13M | case AV_CODEC_ID_PCM_VIDC: |
490 | 2.13M | case AV_CODEC_ID_PCM_S8: |
491 | 2.40M | case AV_CODEC_ID_PCM_S8_PLANAR: |
492 | 2.40M | case AV_CODEC_ID_PCM_SGA: |
493 | 9.05M | case AV_CODEC_ID_PCM_U8: |
494 | 9.06M | case AV_CODEC_ID_SDX2_DPCM: |
495 | 9.06M | case AV_CODEC_ID_CBD2_DPCM: |
496 | 9.09M | case AV_CODEC_ID_DERF_DPCM: |
497 | 9.32M | case AV_CODEC_ID_WADY_DPCM: |
498 | 9.32M | case AV_CODEC_ID_ADPCM_CIRCUS: |
499 | 9.32M | return 8; |
500 | 55.0k | case AV_CODEC_ID_PCM_S16BE: |
501 | 216k | case AV_CODEC_ID_PCM_S16BE_PLANAR: |
502 | 7.51M | case AV_CODEC_ID_PCM_S16LE: |
503 | 7.70M | case AV_CODEC_ID_PCM_S16LE_PLANAR: |
504 | 7.72M | case AV_CODEC_ID_PCM_U16BE: |
505 | 7.83M | case AV_CODEC_ID_PCM_U16LE: |
506 | 7.83M | return 16; |
507 | 82.7k | case AV_CODEC_ID_PCM_S24DAUD: |
508 | 157k | case AV_CODEC_ID_PCM_S24BE: |
509 | 198k | case AV_CODEC_ID_PCM_S24LE: |
510 | 198k | case AV_CODEC_ID_PCM_S24LE_PLANAR: |
511 | 236k | case AV_CODEC_ID_PCM_U24BE: |
512 | 262k | case AV_CODEC_ID_PCM_U24LE: |
513 | 262k | return 24; |
514 | 100k | case AV_CODEC_ID_PCM_S32BE: |
515 | 134k | case AV_CODEC_ID_PCM_S32LE: |
516 | 135k | case AV_CODEC_ID_PCM_S32LE_PLANAR: |
517 | 151k | case AV_CODEC_ID_PCM_U32BE: |
518 | 322k | case AV_CODEC_ID_PCM_U32LE: |
519 | 348k | case AV_CODEC_ID_PCM_F32BE: |
520 | 384k | case AV_CODEC_ID_PCM_F32LE: |
521 | 384k | case AV_CODEC_ID_PCM_F24LE: |
522 | 385k | case AV_CODEC_ID_PCM_F16LE: |
523 | 385k | return 32; |
524 | 9.31k | case AV_CODEC_ID_PCM_F64BE: |
525 | 40.0k | case AV_CODEC_ID_PCM_F64LE: |
526 | 74.7k | case AV_CODEC_ID_PCM_S64BE: |
527 | 94.8k | case AV_CODEC_ID_PCM_S64LE: |
528 | 94.8k | return 64; |
529 | 46.5M | default: |
530 | 46.5M | return 0; |
531 | 68.0M | } |
532 | 68.0M | } |
533 | | |
534 | | enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be) |
535 | 0 | { |
536 | 0 | static const enum AVCodecID map[][2] = { |
537 | 0 | [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 }, |
538 | 0 | [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE }, |
539 | 0 | [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE }, |
540 | 0 | [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE }, |
541 | 0 | [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE }, |
542 | 0 | [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 }, |
543 | 0 | [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE }, |
544 | 0 | [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE }, |
545 | 0 | [AV_SAMPLE_FMT_S64P] = { AV_CODEC_ID_PCM_S64LE, AV_CODEC_ID_PCM_S64BE }, |
546 | 0 | [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE }, |
547 | 0 | [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE }, |
548 | 0 | }; |
549 | 0 | if (fmt < 0 || fmt >= FF_ARRAY_ELEMS(map)) |
550 | 0 | return AV_CODEC_ID_NONE; |
551 | 0 | if (be < 0 || be > 1) |
552 | 0 | be = AV_NE(1, 0); |
553 | 0 | return map[fmt][be]; |
554 | 0 | } |
555 | | |
556 | | int av_get_bits_per_sample(enum AVCodecID codec_id) |
557 | 3.61M | { |
558 | 3.61M | switch (codec_id) { |
559 | 54.2k | case AV_CODEC_ID_DFPWM: |
560 | 54.2k | return 1; |
561 | 160 | case AV_CODEC_ID_ADPCM_SBPRO_2: |
562 | 2.13k | case AV_CODEC_ID_G728: |
563 | 2.13k | return 2; |
564 | 211 | case AV_CODEC_ID_ADPCM_SBPRO_3: |
565 | 211 | return 3; |
566 | 220 | case AV_CODEC_ID_ADPCM_SBPRO_4: |
567 | 3.03k | case AV_CODEC_ID_ADPCM_IMA_WAV: |
568 | 8.72k | case AV_CODEC_ID_ADPCM_IMA_XBOX: |
569 | 9.03k | case AV_CODEC_ID_ADPCM_IMA_QT: |
570 | 19.0k | case AV_CODEC_ID_ADPCM_SWF: |
571 | 25.5k | case AV_CODEC_ID_ADPCM_MS: |
572 | 25.5k | return 4; |
573 | 3.53M | default: |
574 | 3.53M | return av_get_exact_bits_per_sample(codec_id); |
575 | 3.61M | } |
576 | 3.61M | } |
577 | | |
578 | | static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba, |
579 | | uint32_t tag, int bits_per_coded_sample, int64_t bitrate, |
580 | | uint8_t * extradata, int frame_size, int frame_bytes) |
581 | 64.5M | { |
582 | 64.5M | int bps = av_get_exact_bits_per_sample(id); |
583 | 64.5M | int framecount = (ba > 0 && frame_bytes / ba > 0) ? frame_bytes / ba : 1; |
584 | | |
585 | | /* codecs with an exact constant bits per sample */ |
586 | 64.5M | if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768) |
587 | 13.5M | return (frame_bytes * 8LL) / (bps * ch); |
588 | 50.9M | bps = bits_per_coded_sample; |
589 | | |
590 | | /* codecs with a fixed packet duration */ |
591 | 50.9M | switch (id) { |
592 | 247k | case AV_CODEC_ID_ADPCM_ADX: return 32; |
593 | 10.6k | case AV_CODEC_ID_ADPCM_IMA_QT: return 64; |
594 | 419k | case AV_CODEC_ID_ADPCM_EA_XAS: return 128; |
595 | 387k | case AV_CODEC_ID_AMR_NB: |
596 | 1.13M | case AV_CODEC_ID_EVRC: |
597 | 1.13M | case AV_CODEC_ID_GSM: |
598 | 1.73M | case AV_CODEC_ID_QCELP: |
599 | 1.76M | case AV_CODEC_ID_RA_288: return 160; |
600 | 441k | case AV_CODEC_ID_AMR_WB: |
601 | 2.51M | case AV_CODEC_ID_GSM_MS: return 320; |
602 | 142k | case AV_CODEC_ID_MP1: return 384; |
603 | 136k | case AV_CODEC_ID_ATRAC1: return 512; |
604 | 2.07k | case AV_CODEC_ID_ATRAC9: |
605 | 1.44M | case AV_CODEC_ID_ATRAC3: |
606 | 1.44M | if (framecount > INT_MAX/1024) |
607 | 10 | return 0; |
608 | 1.44M | return 1024 * framecount; |
609 | 15.3k | case AV_CODEC_ID_ATRAC3P: return 2048; |
610 | 751k | case AV_CODEC_ID_MP2: |
611 | 871k | case AV_CODEC_ID_MUSEPACK7: return 1152; |
612 | 1.67M | case AV_CODEC_ID_AC3: return 1536; |
613 | 3.09k | case AV_CODEC_ID_FTR: return 1024; |
614 | 50.9M | } |
615 | | |
616 | 41.7M | if (sr > 0) { |
617 | | /* calc from sample rate */ |
618 | 15.6M | if (id == AV_CODEC_ID_TTA) |
619 | 2.15k | return 256ll * sr / 245; |
620 | 15.6M | else if (id == AV_CODEC_ID_DST) |
621 | 36 | return 588ll * sr / 44100; |
622 | 15.6M | else if (id == AV_CODEC_ID_BINKAUDIO_DCT) { |
623 | 117k | if (sr / 22050 > 22) |
624 | 51 | return 0; |
625 | 117k | return (480 << (sr / 22050)); |
626 | 117k | } |
627 | | |
628 | 15.5M | if (id == AV_CODEC_ID_MP3) |
629 | 1.59M | return sr <= 24000 ? 576 : 1152; |
630 | 15.5M | } |
631 | | |
632 | 39.9M | if (ba > 0) { |
633 | | /* calc from block_align */ |
634 | 11.7M | if (id == AV_CODEC_ID_SIPR) { |
635 | 210k | switch (ba) { |
636 | 26.4k | case 20: return 160; |
637 | 89.2k | case 19: return 144; |
638 | 36.4k | case 29: return 288; |
639 | 49.3k | case 37: return 480; |
640 | 210k | } |
641 | 11.5M | } else if (id == AV_CODEC_ID_ILBC) { |
642 | 3 | switch (ba) { |
643 | 1 | case 38: return 160; |
644 | 1 | case 50: return 240; |
645 | 3 | } |
646 | 3 | } |
647 | 11.7M | } |
648 | | |
649 | 39.7M | if (frame_bytes > 0) { |
650 | | /* calc from frame_bytes only */ |
651 | 31.9M | int64_t d = INT64_MIN; |
652 | 31.9M | switch(id) { |
653 | 9.39k | case AV_CODEC_ID_TRUESPEECH : d = 240LL * (frame_bytes / 32); break; |
654 | 154k | case AV_CODEC_ID_NELLYMOSER : d = 256LL * (frame_bytes / 64); break; |
655 | 5.69k | case AV_CODEC_ID_RA_144 : d = 160LL * (frame_bytes / 20); break; |
656 | 54.7k | case AV_CODEC_ID_APTX : d = 4LL * (frame_bytes / 4); break; |
657 | 39.4k | case AV_CODEC_ID_APTX_HD : d = 4LL * (frame_bytes / 6); break; |
658 | 31.9M | } |
659 | 31.9M | if (d > INT64_MIN) |
660 | 263k | return ((int)d == d && d > 0) ? d : 0; |
661 | | |
662 | 31.6M | if (bps > 0) { |
663 | | /* calc from frame_bytes and bits_per_coded_sample */ |
664 | 5.22M | if (id == AV_CODEC_ID_ADPCM_G726 || id == AV_CODEC_ID_ADPCM_G726LE) |
665 | 107k | return frame_bytes * 8 / bps; |
666 | 5.22M | } |
667 | | |
668 | 31.5M | if (ch > 0 && ch < INT_MAX/16) { |
669 | | /* calc from frame_bytes and channels */ |
670 | 14.9M | switch (id) { |
671 | 477k | case AV_CODEC_ID_FASTAUDIO: |
672 | 477k | return frame_bytes / (40 * ch) * 256; |
673 | 739 | case AV_CODEC_ID_ADPCM_IMA_MOFLEX: |
674 | 739 | return (frame_bytes - 4 * ch) / (128 * ch) * 256; |
675 | 22.4k | case AV_CODEC_ID_ADPCM_AFC: |
676 | 22.4k | return frame_bytes / (9 * ch) * 16; |
677 | 207 | case AV_CODEC_ID_ADPCM_N64: |
678 | 207 | frame_bytes /= 9 * ch; |
679 | 207 | if (frame_bytes > INT_MAX / 16) |
680 | 0 | return 0; |
681 | 207 | return frame_bytes * 16; |
682 | 571k | case AV_CODEC_ID_ADPCM_PSX: |
683 | 638k | case AV_CODEC_ID_ADPCM_DTK: |
684 | 638k | frame_bytes /= 16 * ch; |
685 | 638k | if (frame_bytes > INT_MAX / 28) |
686 | 20 | return 0; |
687 | 638k | return frame_bytes * 28; |
688 | 0 | case AV_CODEC_ID_ADPCM_PSXC: |
689 | 0 | frame_bytes = (frame_bytes - 1) / ch; |
690 | 0 | if (frame_bytes > INT_MAX / 2) |
691 | 0 | return 0; |
692 | 0 | return frame_bytes * 2; |
693 | 2.28k | case AV_CODEC_ID_ADPCM_4XM: |
694 | 2.28k | case AV_CODEC_ID_ADPCM_IMA_ACORN: |
695 | 165k | case AV_CODEC_ID_ADPCM_IMA_DAT4: |
696 | 166k | case AV_CODEC_ID_ADPCM_IMA_ISS: |
697 | 166k | case AV_CODEC_ID_ADPCM_IMA_PDA: |
698 | 166k | return (frame_bytes - 4 * ch) * 2 / ch; |
699 | 2.05k | case AV_CODEC_ID_ADPCM_IMA_SMJPEG: |
700 | 2.05k | return (frame_bytes - 4) * 2 / ch; |
701 | 353 | case AV_CODEC_ID_ADPCM_IMA_AMV: |
702 | 353 | return (frame_bytes - 8) * 2; |
703 | 905k | case AV_CODEC_ID_ADPCM_THP: |
704 | 908k | case AV_CODEC_ID_ADPCM_THP_LE: |
705 | 908k | if (extradata) |
706 | 902k | return frame_bytes * 14LL / (8 * ch); |
707 | 6.08k | break; |
708 | 6.08k | case AV_CODEC_ID_ADPCM_XA: |
709 | 776 | return (frame_bytes / 128) * 224 / ch; |
710 | 4.06k | case AV_CODEC_ID_INTERPLAY_DPCM: |
711 | 4.06k | return (frame_bytes - 6 - ch) / ch; |
712 | 10.4k | case AV_CODEC_ID_ROQ_DPCM: |
713 | 10.4k | return (frame_bytes - 8) / ch; |
714 | 4.64k | case AV_CODEC_ID_XAN_DPCM: |
715 | 4.64k | return (frame_bytes - 2 * ch) / ch; |
716 | 107 | case AV_CODEC_ID_MACE3: |
717 | 107 | return 3 * frame_bytes / ch; |
718 | 72 | case AV_CODEC_ID_MACE6: |
719 | 72 | return 6 * frame_bytes / ch; |
720 | 195 | case AV_CODEC_ID_PCM_LXF: |
721 | 195 | return 2 * (frame_bytes / (5 * ch)); |
722 | 5.46k | case AV_CODEC_ID_IAC: |
723 | 29.8k | case AV_CODEC_ID_IMC: |
724 | 29.8k | return 4 * frame_bytes / ch; |
725 | 14.9M | } |
726 | | |
727 | 12.6M | if (tag) { |
728 | | /* calc from frame_bytes, channels, and codec_tag */ |
729 | 3.86M | if (id == AV_CODEC_ID_SOL_DPCM) { |
730 | 9.62k | if (tag == 3) |
731 | 1.63k | return frame_bytes / ch; |
732 | 7.99k | else |
733 | 7.99k | return frame_bytes * 2 / ch; |
734 | 9.62k | } |
735 | 3.86M | } |
736 | | |
737 | 12.6M | if (ba > 0) { |
738 | | /* calc from frame_bytes, channels, and block_align */ |
739 | 7.23M | int blocks = frame_bytes / ba; |
740 | 7.23M | int64_t tmp = 0; |
741 | 7.23M | switch (id) { |
742 | 8.90k | case AV_CODEC_ID_ADPCM_IMA_XBOX: |
743 | 8.90k | if (bps != 4) |
744 | 5.30k | return 0; |
745 | 3.60k | tmp = blocks * ((ba - 4 * ch) / (bps * ch) * 8); |
746 | 3.60k | break; |
747 | 34.3k | case AV_CODEC_ID_ADPCM_IMA_WAV: |
748 | 34.3k | if (bps < 2 || bps > 5) |
749 | 6.64k | return 0; |
750 | 27.6k | tmp = blocks * (1LL + (ba - 4 * ch) / (bps * ch) * 8LL); |
751 | 27.6k | break; |
752 | 8.28k | case AV_CODEC_ID_ADPCM_IMA_DK3: |
753 | 8.28k | tmp = blocks * (((ba - 16LL) * 2 / 3 * 4) / ch); |
754 | 8.28k | break; |
755 | 5.01k | case AV_CODEC_ID_ADPCM_IMA_DK4: |
756 | 5.01k | tmp = blocks * (1 + (ba - 4LL * ch) * 2 / ch); |
757 | 5.01k | break; |
758 | 1.01k | case AV_CODEC_ID_ADPCM_IMA_RAD: |
759 | 1.01k | tmp = blocks * ((ba - 4LL * ch) * 2 / ch); |
760 | 1.01k | break; |
761 | 3.11M | case AV_CODEC_ID_ADPCM_MS: |
762 | 3.11M | tmp = blocks * (2 + (ba - 7LL * ch) * 2LL / ch); |
763 | 3.11M | break; |
764 | 51.3k | case AV_CODEC_ID_ADPCM_MTAF: |
765 | 51.3k | tmp = blocks * (ba - 16LL) * 2 / ch; |
766 | 51.3k | break; |
767 | 25.6k | case AV_CODEC_ID_ADPCM_XMD: |
768 | 25.6k | tmp = blocks * 32; |
769 | 25.6k | break; |
770 | 7.23M | } |
771 | 7.22M | if (tmp) { |
772 | 3.21M | if (tmp != (int)tmp) |
773 | 7 | return 0; |
774 | 3.21M | return tmp; |
775 | 3.21M | } |
776 | 7.22M | } |
777 | | |
778 | 9.42M | if (bps > 0) { |
779 | | /* calc from frame_bytes, channels, and bits_per_coded_sample */ |
780 | 4.52M | switch (id) { |
781 | 0 | case AV_CODEC_ID_PCM_DVD: |
782 | 0 | if(bps<4 || frame_bytes<3) |
783 | 0 | return 0; |
784 | 0 | return 2 * ((frame_bytes - 3) / ((bps * 2 / 8) * ch)); |
785 | 0 | case AV_CODEC_ID_PCM_BLURAY: |
786 | 0 | if(bps<4 || frame_bytes<4) |
787 | 0 | return 0; |
788 | 0 | return (frame_bytes - 4) / ((FFALIGN(ch, 2) * bps) / 8); |
789 | 454 | case AV_CODEC_ID_S302M: |
790 | 454 | return 2 * (frame_bytes / ((bps + 4) / 4)) / ch; |
791 | 4.52M | } |
792 | 4.52M | } |
793 | 9.42M | } |
794 | 31.5M | } |
795 | | |
796 | | /* Fall back on using frame_size */ |
797 | 33.9M | if (frame_size > 1 && frame_bytes) |
798 | 3.78M | return frame_size; |
799 | | |
800 | | //For WMA we currently have no other means to calculate duration thus we |
801 | | //do it here by assuming CBR, which is true for all known cases. |
802 | 30.1M | if (bitrate > 0 && frame_bytes > 0 && sr > 0 && ba > 1) { |
803 | 1.06M | if (id == AV_CODEC_ID_WMAV1 || id == AV_CODEC_ID_WMAV2) |
804 | 176k | return (frame_bytes * 8LL * sr) / bitrate; |
805 | 1.06M | } |
806 | | |
807 | 29.9M | return 0; |
808 | 30.1M | } |
809 | | |
810 | | int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes) |
811 | 52.5M | { |
812 | 52.5M | int channels = avctx->ch_layout.nb_channels; |
813 | 52.5M | int duration; |
814 | | |
815 | 52.5M | duration = get_audio_frame_duration(avctx->codec_id, avctx->sample_rate, |
816 | 52.5M | channels, avctx->block_align, |
817 | 52.5M | avctx->codec_tag, avctx->bits_per_coded_sample, |
818 | 52.5M | avctx->bit_rate, avctx->extradata, avctx->frame_size, |
819 | 52.5M | frame_bytes); |
820 | 52.5M | return FFMAX(0, duration); |
821 | 52.5M | } |
822 | | |
823 | | int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes) |
824 | 11.9M | { |
825 | 11.9M | int channels = par->ch_layout.nb_channels; |
826 | 11.9M | int duration; |
827 | | |
828 | 11.9M | duration = get_audio_frame_duration(par->codec_id, par->sample_rate, |
829 | 11.9M | channels, par->block_align, |
830 | 11.9M | par->codec_tag, par->bits_per_coded_sample, |
831 | 11.9M | par->bit_rate, par->extradata, par->frame_size, |
832 | 11.9M | frame_bytes); |
833 | 11.9M | return FFMAX(0, duration); |
834 | 11.9M | } |
835 | | |
836 | | unsigned int av_xiphlacing(unsigned char *s, unsigned int v) |
837 | 14.4k | { |
838 | 14.4k | unsigned int n = 0; |
839 | | |
840 | 71.4M | while (v >= 0xff) { |
841 | 71.4M | *s++ = 0xff; |
842 | 71.4M | v -= 0xff; |
843 | 71.4M | n++; |
844 | 71.4M | } |
845 | 14.4k | *s = v; |
846 | 14.4k | n++; |
847 | 14.4k | return n; |
848 | 14.4k | } |
849 | | |
850 | | int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b) |
851 | 6.79k | { |
852 | 6.79k | int i; |
853 | 51.4k | for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ; |
854 | 6.79k | return i; |
855 | 6.79k | } |
856 | | |
857 | | const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *avcodec, int index) |
858 | 0 | { |
859 | 0 | const FFCodec *const codec = ffcodec(avcodec); |
860 | 0 | int i; |
861 | 0 | if (!codec->hw_configs || index < 0) |
862 | 0 | return NULL; |
863 | 0 | for (i = 0; i <= index; i++) |
864 | 0 | if (!codec->hw_configs[i]) |
865 | 0 | return NULL; |
866 | 0 | return &codec->hw_configs[index]->public; |
867 | 0 | } |
868 | | |
869 | | int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src) |
870 | 5.85M | { |
871 | 5.85M | int ret; |
872 | | |
873 | 5.85M | dst->owner[0] = src->owner[0]; |
874 | 5.85M | dst->owner[1] = src->owner[1]; |
875 | | |
876 | 5.85M | ret = av_frame_ref(dst->f, src->f); |
877 | 5.85M | if (ret < 0) |
878 | 0 | return ret; |
879 | | |
880 | 5.85M | av_assert0(!dst->progress); |
881 | | |
882 | 5.85M | if (src->progress) |
883 | 0 | dst->progress = av_refstruct_ref(src->progress); |
884 | | |
885 | 5.85M | return 0; |
886 | 5.85M | } |
887 | | |
888 | | int ff_thread_replace_frame(ThreadFrame *dst, const ThreadFrame *src) |
889 | 0 | { |
890 | 0 | int ret; |
891 | |
|
892 | 0 | dst->owner[0] = src->owner[0]; |
893 | 0 | dst->owner[1] = src->owner[1]; |
894 | |
|
895 | 0 | ret = av_frame_replace(dst->f, src->f); |
896 | 0 | if (ret < 0) |
897 | 0 | return ret; |
898 | | |
899 | 0 | av_refstruct_replace(&dst->progress, src->progress); |
900 | |
|
901 | 0 | return 0; |
902 | 0 | } |
903 | | |
904 | | #if !HAVE_THREADS |
905 | | |
906 | | int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags) |
907 | | { |
908 | | return ff_get_buffer(avctx, f, flags); |
909 | | } |
910 | | |
911 | | int ff_thread_get_ext_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags) |
912 | | { |
913 | | f->owner[0] = f->owner[1] = avctx; |
914 | | return ff_get_buffer(avctx, f->f, flags); |
915 | | } |
916 | | |
917 | | void ff_thread_release_ext_buffer(ThreadFrame *f) |
918 | | { |
919 | | f->owner[0] = f->owner[1] = NULL; |
920 | | if (f->f) |
921 | | av_frame_unref(f->f); |
922 | | } |
923 | | |
924 | | void ff_thread_finish_setup(AVCodecContext *avctx) |
925 | | { |
926 | | } |
927 | | |
928 | | void ff_thread_report_progress(ThreadFrame *f, int progress, int field) |
929 | | { |
930 | | } |
931 | | |
932 | | void ff_thread_await_progress(const ThreadFrame *f, int progress, int field) |
933 | | { |
934 | | } |
935 | | |
936 | | int ff_thread_can_start_frame(AVCodecContext *avctx) |
937 | | { |
938 | | return 1; |
939 | | } |
940 | | #endif |
941 | | |
942 | | const uint8_t *avpriv_find_start_code(const uint8_t *restrict p, |
943 | | const uint8_t *end, |
944 | | uint32_t *restrict state) |
945 | 81.7M | { |
946 | 81.7M | int i; |
947 | | |
948 | 81.7M | av_assert0(p <= end); |
949 | 81.7M | if (p >= end) |
950 | 69.5k | return end; |
951 | | |
952 | 323M | for (i = 0; i < 3; i++) { |
953 | 243M | uint32_t tmp = *state << 8; |
954 | 243M | *state = tmp + *(p++); |
955 | 243M | if (tmp == 0x100 || p == end) |
956 | 1.87M | return p; |
957 | 243M | } |
958 | | |
959 | 2.35G | while (p < end) { |
960 | 2.34G | if (p[-1] > 1 ) p += 3; |
961 | 1.07G | else if (p[-2] ) p += 2; |
962 | 939M | else if (p[-3]|(p[-1]-1)) p++; |
963 | 76.0M | else { |
964 | 76.0M | p++; |
965 | 76.0M | break; |
966 | 76.0M | } |
967 | 2.34G | } |
968 | | |
969 | 79.7M | p = FFMIN(p, end) - 4; |
970 | 79.7M | *state = AV_RB32(p); |
971 | | |
972 | 79.7M | return p + 4; |
973 | 81.6M | } |
974 | | |
975 | | AVCPBProperties *av_cpb_properties_alloc(size_t *size) |
976 | 14.8k | { |
977 | 14.8k | AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties)); |
978 | 14.8k | if (!props) |
979 | 0 | return NULL; |
980 | | |
981 | 14.8k | if (size) |
982 | 14.8k | *size = sizeof(*props); |
983 | | |
984 | 14.8k | props->vbv_delay = UINT64_MAX; |
985 | | |
986 | 14.8k | return props; |
987 | 14.8k | } |
988 | | |
989 | | int ff_alloc_timecode_sei(const AVFrame *frame, AVRational rate, size_t prefix_len, |
990 | | void **data, size_t *sei_size) |
991 | 0 | { |
992 | 0 | AVFrameSideData *sd = NULL; |
993 | 0 | uint8_t *sei_data; |
994 | 0 | PutBitContext pb; |
995 | 0 | uint32_t *tc; |
996 | 0 | int m; |
997 | |
|
998 | 0 | if (frame) |
999 | 0 | sd = av_frame_get_side_data(frame, AV_FRAME_DATA_S12M_TIMECODE); |
1000 | |
|
1001 | 0 | if (!sd) { |
1002 | 0 | *data = NULL; |
1003 | 0 | return 0; |
1004 | 0 | } |
1005 | 0 | tc = (uint32_t*)sd->data; |
1006 | 0 | m = tc[0] & 3; |
1007 | |
|
1008 | 0 | *sei_size = sizeof(uint32_t) * 4; |
1009 | 0 | *data = av_mallocz(*sei_size + prefix_len); |
1010 | 0 | if (!*data) |
1011 | 0 | return AVERROR(ENOMEM); |
1012 | 0 | sei_data = (uint8_t*)*data + prefix_len; |
1013 | |
|
1014 | 0 | init_put_bits(&pb, sei_data, *sei_size); |
1015 | 0 | put_bits(&pb, 2, m); // num_clock_ts |
1016 | |
|
1017 | 0 | for (int j = 1; j <= m; j++) { |
1018 | 0 | unsigned hh, mm, ss, ff, drop; |
1019 | 0 | ff_timecode_set_smpte(&drop, &hh, &mm, &ss, &ff, rate, tc[j], 0, 0); |
1020 | |
|
1021 | 0 | put_bits(&pb, 1, 1); // clock_timestamp_flag |
1022 | 0 | put_bits(&pb, 1, 1); // units_field_based_flag |
1023 | 0 | put_bits(&pb, 5, 0); // counting_type |
1024 | 0 | put_bits(&pb, 1, 1); // full_timestamp_flag |
1025 | 0 | put_bits(&pb, 1, 0); // discontinuity_flag |
1026 | 0 | put_bits(&pb, 1, drop); |
1027 | 0 | put_bits(&pb, 9, ff); |
1028 | 0 | put_bits(&pb, 6, ss); |
1029 | 0 | put_bits(&pb, 6, mm); |
1030 | 0 | put_bits(&pb, 5, hh); |
1031 | 0 | put_bits(&pb, 5, 0); |
1032 | 0 | } |
1033 | 0 | flush_put_bits(&pb); |
1034 | |
|
1035 | 0 | return 0; |
1036 | 0 | } |
1037 | | |
1038 | | int64_t ff_guess_coded_bitrate(AVCodecContext *avctx) |
1039 | 2.26k | { |
1040 | 2.26k | AVRational framerate = avctx->framerate; |
1041 | 2.26k | int bits_per_coded_sample = avctx->bits_per_coded_sample; |
1042 | 2.26k | int64_t bitrate; |
1043 | | |
1044 | 2.26k | if (!(framerate.num && framerate.den)) |
1045 | 308 | framerate = av_inv_q(avctx->time_base); |
1046 | 2.26k | if (!(framerate.num && framerate.den)) |
1047 | 0 | return 0; |
1048 | | |
1049 | 2.26k | if (!bits_per_coded_sample) { |
1050 | 0 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt); |
1051 | 0 | bits_per_coded_sample = av_get_bits_per_pixel(desc); |
1052 | 0 | } |
1053 | 2.26k | bitrate = (int64_t)bits_per_coded_sample * avctx->width * avctx->height * |
1054 | 2.26k | framerate.num / framerate.den; |
1055 | | |
1056 | 2.26k | return bitrate; |
1057 | 2.26k | } |