3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
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.
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.
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
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"
38 #include "codec_internal.h"
41 #include "threadframe.h"
44 #include "startcode.h"
48 void av_fast_padded_malloc(void *ptr
, unsigned int *size
, size_t min_size
)
51 if (min_size
> SIZE_MAX
- AV_INPUT_BUFFER_PADDING_SIZE
) {
56 av_fast_mallocz(p
, size
, min_size
+ AV_INPUT_BUFFER_PADDING_SIZE
);
58 memset(*p
+ min_size
, 0, AV_INPUT_BUFFER_PADDING_SIZE
);
61 void av_fast_padded_mallocz(void *ptr
, unsigned int *size
, size_t min_size
)
64 if (min_size
> SIZE_MAX
- AV_INPUT_BUFFER_PADDING_SIZE
) {
69 av_fast_malloc(p
, size
, min_size
+ AV_INPUT_BUFFER_PADDING_SIZE
);
71 memset(*p
, 0, min_size
+ AV_INPUT_BUFFER_PADDING_SIZE
);
74 int av_codec_is_encoder(const AVCodec
*avcodec
)
76 const FFCodec
*const codec
= ffcodec(avcodec
);
77 return codec
&& (codec
->cb_type
== FF_CODEC_CB_TYPE_ENCODE
||
78 codec
->cb_type
== FF_CODEC_CB_TYPE_ENCODE_SUB
||
79 codec
->cb_type
== FF_CODEC_CB_TYPE_RECEIVE_PACKET
);
82 int av_codec_is_decoder(const AVCodec
*avcodec
)
84 const FFCodec
*const codec
= ffcodec(avcodec
);
85 return codec
&& (codec
->cb_type
== FF_CODEC_CB_TYPE_DECODE
||
86 codec
->cb_type
== FF_CODEC_CB_TYPE_DECODE_SUB
||
87 codec
->cb_type
== FF_CODEC_CB_TYPE_RECEIVE_FRAME
);
90 int ff_set_dimensions(AVCodecContext
*s
, int width
, int height
)
92 int ret
= av_image_check_size2(width
, height
, s
->max_pixels
, AV_PIX_FMT_NONE
, 0, s
);
97 s
->coded_width
= width
;
98 s
->coded_height
= height
;
99 s
->width
= AV_CEIL_RSHIFT(width
, s
->lowres
);
100 s
->height
= AV_CEIL_RSHIFT(height
, s
->lowres
);
105 int ff_set_sar(AVCodecContext
*avctx
, AVRational sar
)
107 int ret
= av_image_check_sar(avctx
->width
, avctx
->height
, sar
);
110 av_log(avctx
, AV_LOG_WARNING
, "ignoring invalid SAR: %d/%d\n",
112 avctx
->sample_aspect_ratio
= (AVRational
){ 0, 1 };
115 avctx
->sample_aspect_ratio
= sar
;
120 int ff_side_data_update_matrix_encoding(AVFrame
*frame
,
121 enum AVMatrixEncoding matrix_encoding
)
123 AVFrameSideData
*side_data
;
124 enum AVMatrixEncoding
*data
;
126 side_data
= av_frame_get_side_data(frame
, AV_FRAME_DATA_MATRIXENCODING
);
128 side_data
= av_frame_new_side_data(frame
, AV_FRAME_DATA_MATRIXENCODING
,
129 sizeof(enum AVMatrixEncoding
));
132 return AVERROR(ENOMEM
);
134 data
= (enum AVMatrixEncoding
*)side_data
->data
;
135 *data
= matrix_encoding
;
140 void avcodec_align_dimensions2(AVCodecContext
*s
, int *width
, int *height
,
141 int linesize_align
[AV_NUM_DATA_POINTERS
])
146 AVPixFmtDescriptor
const *desc
= av_pix_fmt_desc_get(s
->pix_fmt
);
149 w_align
= 1 << desc
->log2_chroma_w
;
150 h_align
= 1 << desc
->log2_chroma_h
;
153 switch (s
->pix_fmt
) {
154 case AV_PIX_FMT_YUV420P
:
155 case AV_PIX_FMT_YUYV422
:
156 case AV_PIX_FMT_YVYU422
:
157 case AV_PIX_FMT_UYVY422
:
158 case AV_PIX_FMT_YUV422P
:
159 case AV_PIX_FMT_YUV440P
:
160 case AV_PIX_FMT_YUV444P
:
161 case AV_PIX_FMT_GBRP
:
162 case AV_PIX_FMT_GBRAP
:
163 case AV_PIX_FMT_GRAY8
:
164 case AV_PIX_FMT_GRAY16BE
:
165 case AV_PIX_FMT_GRAY16LE
:
166 case AV_PIX_FMT_YUVJ420P
:
167 case AV_PIX_FMT_YUVJ422P
:
168 case AV_PIX_FMT_YUVJ440P
:
169 case AV_PIX_FMT_YUVJ444P
:
170 case AV_PIX_FMT_YUVA420P
:
171 case AV_PIX_FMT_YUVA422P
:
172 case AV_PIX_FMT_YUVA444P
:
173 case AV_PIX_FMT_YUV420P9LE
:
174 case AV_PIX_FMT_YUV420P9BE
:
175 case AV_PIX_FMT_YUV420P10LE
:
176 case AV_PIX_FMT_YUV420P10BE
:
177 case AV_PIX_FMT_YUV420P12LE
:
178 case AV_PIX_FMT_YUV420P12BE
:
179 case AV_PIX_FMT_YUV420P14LE
:
180 case AV_PIX_FMT_YUV420P14BE
:
181 case AV_PIX_FMT_YUV420P16LE
:
182 case AV_PIX_FMT_YUV420P16BE
:
183 case AV_PIX_FMT_YUVA420P9LE
:
184 case AV_PIX_FMT_YUVA420P9BE
:
185 case AV_PIX_FMT_YUVA420P10LE
:
186 case AV_PIX_FMT_YUVA420P10BE
:
187 case AV_PIX_FMT_YUVA420P16LE
:
188 case AV_PIX_FMT_YUVA420P16BE
:
189 case AV_PIX_FMT_YUV422P9LE
:
190 case AV_PIX_FMT_YUV422P9BE
:
191 case AV_PIX_FMT_YUV422P10LE
:
192 case AV_PIX_FMT_YUV422P10BE
:
193 case AV_PIX_FMT_YUV422P12LE
:
194 case AV_PIX_FMT_YUV422P12BE
:
195 case AV_PIX_FMT_YUV422P14LE
:
196 case AV_PIX_FMT_YUV422P14BE
:
197 case AV_PIX_FMT_YUV422P16LE
:
198 case AV_PIX_FMT_YUV422P16BE
:
199 case AV_PIX_FMT_YUVA422P9LE
:
200 case AV_PIX_FMT_YUVA422P9BE
:
201 case AV_PIX_FMT_YUVA422P10LE
:
202 case AV_PIX_FMT_YUVA422P10BE
:
203 case AV_PIX_FMT_YUVA422P12LE
:
204 case AV_PIX_FMT_YUVA422P12BE
:
205 case AV_PIX_FMT_YUVA422P16LE
:
206 case AV_PIX_FMT_YUVA422P16BE
:
207 case AV_PIX_FMT_YUV440P10LE
:
208 case AV_PIX_FMT_YUV440P10BE
:
209 case AV_PIX_FMT_YUV440P12LE
:
210 case AV_PIX_FMT_YUV440P12BE
:
211 case AV_PIX_FMT_YUV444P9LE
:
212 case AV_PIX_FMT_YUV444P9BE
:
213 case AV_PIX_FMT_YUV444P10LE
:
214 case AV_PIX_FMT_YUV444P10BE
:
215 case AV_PIX_FMT_YUV444P12LE
:
216 case AV_PIX_FMT_YUV444P12BE
:
217 case AV_PIX_FMT_YUV444P14LE
:
218 case AV_PIX_FMT_YUV444P14BE
:
219 case AV_PIX_FMT_YUV444P16LE
:
220 case AV_PIX_FMT_YUV444P16BE
:
221 case AV_PIX_FMT_YUVA444P9LE
:
222 case AV_PIX_FMT_YUVA444P9BE
:
223 case AV_PIX_FMT_YUVA444P10LE
:
224 case AV_PIX_FMT_YUVA444P10BE
:
225 case AV_PIX_FMT_YUVA444P12LE
:
226 case AV_PIX_FMT_YUVA444P12BE
:
227 case AV_PIX_FMT_YUVA444P16LE
:
228 case AV_PIX_FMT_YUVA444P16BE
:
229 case AV_PIX_FMT_GBRP9LE
:
230 case AV_PIX_FMT_GBRP9BE
:
231 case AV_PIX_FMT_GBRP10LE
:
232 case AV_PIX_FMT_GBRP10BE
:
233 case AV_PIX_FMT_GBRP12LE
:
234 case AV_PIX_FMT_GBRP12BE
:
235 case AV_PIX_FMT_GBRP14LE
:
236 case AV_PIX_FMT_GBRP14BE
:
237 case AV_PIX_FMT_GBRP16LE
:
238 case AV_PIX_FMT_GBRP16BE
:
239 case AV_PIX_FMT_GBRAP12LE
:
240 case AV_PIX_FMT_GBRAP12BE
:
241 case AV_PIX_FMT_GBRAP16LE
:
242 case AV_PIX_FMT_GBRAP16BE
:
243 w_align
= 16; //FIXME assume 16 pixel per macroblock
244 h_align
= 16 * 2; // interlaced needs 2 macroblocks height
245 if (s
->codec_id
== AV_CODEC_ID_BINKVIDEO
)
248 case AV_PIX_FMT_YUV411P
:
249 case AV_PIX_FMT_YUVJ411P
:
250 case AV_PIX_FMT_UYYVYY411
:
254 case AV_PIX_FMT_YUV410P
:
255 if (s
->codec_id
== AV_CODEC_ID_SVQ1
) {
258 } else if (s
->codec_id
== AV_CODEC_ID_SNOW
) {
263 case AV_PIX_FMT_RGB555
:
264 if (s
->codec_id
== AV_CODEC_ID_RPZA
) {
268 if (s
->codec_id
== AV_CODEC_ID_INTERPLAY_VIDEO
) {
273 case AV_PIX_FMT_PAL8
:
274 case AV_PIX_FMT_BGR8
:
275 case AV_PIX_FMT_RGB8
:
276 if (s
->codec_id
== AV_CODEC_ID_SMC
||
277 s
->codec_id
== AV_CODEC_ID_CINEPAK
) {
281 if (s
->codec_id
== AV_CODEC_ID_JV
||
282 s
->codec_id
== AV_CODEC_ID_ARGO
||
283 s
->codec_id
== AV_CODEC_ID_INTERPLAY_VIDEO
) {
287 if (s
->codec_id
== AV_CODEC_ID_MJPEG
||
288 s
->codec_id
== AV_CODEC_ID_MJPEGB
||
289 s
->codec_id
== AV_CODEC_ID_LJPEG
||
290 s
->codec_id
== AV_CODEC_ID_SMVJPEG
||
291 s
->codec_id
== AV_CODEC_ID_AMV
||
292 s
->codec_id
== AV_CODEC_ID_SP5X
||
293 s
->codec_id
== AV_CODEC_ID_JPEGLS
) {
298 case AV_PIX_FMT_BGR24
:
299 if ((s
->codec_id
== AV_CODEC_ID_MSZH
) ||
300 (s
->codec_id
== AV_CODEC_ID_ZLIB
)) {
305 case AV_PIX_FMT_RGB24
:
306 if (s
->codec_id
== AV_CODEC_ID_CINEPAK
) {
311 case AV_PIX_FMT_BGR0
:
312 if (s
->codec_id
== AV_CODEC_ID_ARGO
) {
321 if (s
->codec_id
== AV_CODEC_ID_IFF_ILBM
) {
322 w_align
= FFMAX(w_align
, 16);
325 *width
= FFALIGN(*width
, w_align
);
326 *height
= FFALIGN(*height
, h_align
);
327 if (s
->codec_id
== AV_CODEC_ID_H264
|| s
->lowres
||
328 s
->codec_id
== AV_CODEC_ID_VC1
|| s
->codec_id
== AV_CODEC_ID_WMV3
||
329 s
->codec_id
== AV_CODEC_ID_VP5
|| s
->codec_id
== AV_CODEC_ID_VP6
||
330 s
->codec_id
== AV_CODEC_ID_VP6F
|| s
->codec_id
== AV_CODEC_ID_VP6A
332 // some of the optimized chroma MC reads one line too much
333 // which is also done in mpeg decoders with lowres > 0
336 // H.264 uses edge emulation for out of frame motion vectors, for this
337 // it requires a temporary area large enough to hold a 21x21 block,
338 // increasing witdth ensure that the temporary area is large enough,
339 // the next rounded up width is 32
340 *width
= FFMAX(*width
, 32);
342 if (s
->codec_id
== AV_CODEC_ID_SVQ3
) {
343 *width
= FFMAX(*width
, 32);
346 for (i
= 0; i
< 4; i
++)
347 linesize_align
[i
] = STRIDE_ALIGN
;
350 void avcodec_align_dimensions(AVCodecContext
*s
, int *width
, int *height
)
352 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(s
->pix_fmt
);
353 int chroma_shift
= desc
->log2_chroma_w
;
354 int linesize_align
[AV_NUM_DATA_POINTERS
];
357 avcodec_align_dimensions2(s
, width
, height
, linesize_align
);
358 align
= FFMAX(linesize_align
[0], linesize_align
[3]);
359 linesize_align
[1] <<= chroma_shift
;
360 linesize_align
[2] <<= chroma_shift
;
361 align
= FFMAX3(align
, linesize_align
[1], linesize_align
[2]);
362 *width
= FFALIGN(*width
, align
);
365 int avcodec_enum_to_chroma_pos(int *xpos
, int *ypos
, enum AVChromaLocation pos
)
367 if (pos
<= AVCHROMA_LOC_UNSPECIFIED
|| pos
>= AVCHROMA_LOC_NB
)
368 return AVERROR(EINVAL
);
371 *xpos
= (pos
&1) * 128;
372 *ypos
= ((pos
>>1)^(pos
<4)) * 128;
377 enum AVChromaLocation
avcodec_chroma_pos_to_enum(int xpos
, int ypos
)
381 for (pos
= AVCHROMA_LOC_UNSPECIFIED
+ 1; pos
< AVCHROMA_LOC_NB
; pos
++) {
382 if (avcodec_enum_to_chroma_pos(&xout
, &yout
, pos
) == 0 && xout
== xpos
&& yout
== ypos
)
385 return AVCHROMA_LOC_UNSPECIFIED
;
388 int avcodec_fill_audio_frame(AVFrame
*frame
, int nb_channels
,
389 enum AVSampleFormat sample_fmt
, const uint8_t *buf
,
390 int buf_size
, int align
)
392 int ch
, planar
, needed_size
, ret
= 0;
394 needed_size
= av_samples_get_buffer_size(NULL
, nb_channels
,
395 frame
->nb_samples
, sample_fmt
,
397 if (buf_size
< needed_size
)
398 return AVERROR(EINVAL
);
400 planar
= av_sample_fmt_is_planar(sample_fmt
);
401 if (planar
&& nb_channels
> AV_NUM_DATA_POINTERS
) {
402 if (!FF_ALLOCZ_TYPED_ARRAY(frame
->extended_data
, nb_channels
))
403 return AVERROR(ENOMEM
);
405 frame
->extended_data
= frame
->data
;
408 if ((ret
= av_samples_fill_arrays(frame
->extended_data
, &frame
->linesize
[0],
409 (uint8_t *)(intptr_t)buf
, nb_channels
, frame
->nb_samples
,
410 sample_fmt
, align
)) < 0) {
411 if (frame
->extended_data
!= frame
->data
)
412 av_freep(&frame
->extended_data
);
415 if (frame
->extended_data
!= frame
->data
) {
416 for (ch
= 0; ch
< AV_NUM_DATA_POINTERS
; ch
++)
417 frame
->data
[ch
] = frame
->extended_data
[ch
];
423 void ff_color_frame(AVFrame
*frame
, const int c
[4])
425 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(frame
->format
);
428 av_assert0(desc
->flags
& AV_PIX_FMT_FLAG_PLANAR
);
430 for (p
= 0; p
<desc
->nb_components
; p
++) {
431 uint8_t *dst
= frame
->data
[p
];
432 int is_chroma
= p
== 1 || p
== 2;
433 int bytes
= is_chroma
? AV_CEIL_RSHIFT(frame
->width
, desc
->log2_chroma_w
) : frame
->width
;
434 int height
= is_chroma
? AV_CEIL_RSHIFT(frame
->height
, desc
->log2_chroma_h
) : frame
->height
;
435 if (desc
->comp
[0].depth
>= 9) {
436 ((uint16_t*)dst
)[0] = c
[p
];
437 av_memcpy_backptr(dst
+ 2, 2, bytes
- 2);
438 dst
+= frame
->linesize
[p
];
439 for (y
= 1; y
< height
; y
++) {
440 memcpy(dst
, frame
->data
[p
], 2*bytes
);
441 dst
+= frame
->linesize
[p
];
444 for (y
= 0; y
< height
; y
++) {
445 memset(dst
, c
[p
], bytes
);
446 dst
+= frame
->linesize
[p
];
452 int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec
*codec
){
453 return !!(ffcodec(codec
)->caps_internal
& FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
);
456 const char *avcodec_get_name(enum AVCodecID id
)
458 const AVCodecDescriptor
*cd
;
459 const AVCodec
*codec
;
461 if (id
== AV_CODEC_ID_NONE
)
463 cd
= avcodec_descriptor_get(id
);
466 av_log(NULL
, AV_LOG_WARNING
, "Codec 0x%x is not in the full list.\n", id
);
467 codec
= avcodec_find_decoder(id
);
470 codec
= avcodec_find_encoder(id
);
473 return "unknown_codec";
476 const char *av_get_profile_name(const AVCodec
*codec
, int profile
)
479 if (profile
== FF_PROFILE_UNKNOWN
|| !codec
->profiles
)
482 for (p
= codec
->profiles
; p
->profile
!= FF_PROFILE_UNKNOWN
; p
++)
483 if (p
->profile
== profile
)
489 const char *avcodec_profile_name(enum AVCodecID codec_id
, int profile
)
491 const AVCodecDescriptor
*desc
= avcodec_descriptor_get(codec_id
);
494 if (profile
== FF_PROFILE_UNKNOWN
|| !desc
|| !desc
->profiles
)
497 for (p
= desc
->profiles
; p
->profile
!= FF_PROFILE_UNKNOWN
; p
++)
498 if (p
->profile
== profile
)
504 int av_get_exact_bits_per_sample(enum AVCodecID codec_id
)
507 case AV_CODEC_ID_DFPWM
:
509 case AV_CODEC_ID_8SVX_EXP
:
510 case AV_CODEC_ID_8SVX_FIB
:
511 case AV_CODEC_ID_ADPCM_ARGO
:
512 case AV_CODEC_ID_ADPCM_CT
:
513 case AV_CODEC_ID_ADPCM_IMA_ALP
:
514 case AV_CODEC_ID_ADPCM_IMA_AMV
:
515 case AV_CODEC_ID_ADPCM_IMA_APC
:
516 case AV_CODEC_ID_ADPCM_IMA_APM
:
517 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD
:
518 case AV_CODEC_ID_ADPCM_IMA_OKI
:
519 case AV_CODEC_ID_ADPCM_IMA_WS
:
520 case AV_CODEC_ID_ADPCM_IMA_SSI
:
521 case AV_CODEC_ID_ADPCM_G722
:
522 case AV_CODEC_ID_ADPCM_YAMAHA
:
523 case AV_CODEC_ID_ADPCM_AICA
:
525 case AV_CODEC_ID_DSD_LSBF
:
526 case AV_CODEC_ID_DSD_MSBF
:
527 case AV_CODEC_ID_DSD_LSBF_PLANAR
:
528 case AV_CODEC_ID_DSD_MSBF_PLANAR
:
529 case AV_CODEC_ID_PCM_ALAW
:
530 case AV_CODEC_ID_PCM_MULAW
:
531 case AV_CODEC_ID_PCM_VIDC
:
532 case AV_CODEC_ID_PCM_S8
:
533 case AV_CODEC_ID_PCM_S8_PLANAR
:
534 case AV_CODEC_ID_PCM_SGA
:
535 case AV_CODEC_ID_PCM_U8
:
536 case AV_CODEC_ID_SDX2_DPCM
:
537 case AV_CODEC_ID_DERF_DPCM
:
539 case AV_CODEC_ID_PCM_S16BE
:
540 case AV_CODEC_ID_PCM_S16BE_PLANAR
:
541 case AV_CODEC_ID_PCM_S16LE
:
542 case AV_CODEC_ID_PCM_S16LE_PLANAR
:
543 case AV_CODEC_ID_PCM_U16BE
:
544 case AV_CODEC_ID_PCM_U16LE
:
546 case AV_CODEC_ID_PCM_S24DAUD
:
547 case AV_CODEC_ID_PCM_S24BE
:
548 case AV_CODEC_ID_PCM_S24LE
:
549 case AV_CODEC_ID_PCM_S24LE_PLANAR
:
550 case AV_CODEC_ID_PCM_U24BE
:
551 case AV_CODEC_ID_PCM_U24LE
:
553 case AV_CODEC_ID_PCM_S32BE
:
554 case AV_CODEC_ID_PCM_S32LE
:
555 case AV_CODEC_ID_PCM_S32LE_PLANAR
:
556 case AV_CODEC_ID_PCM_U32BE
:
557 case AV_CODEC_ID_PCM_U32LE
:
558 case AV_CODEC_ID_PCM_F32BE
:
559 case AV_CODEC_ID_PCM_F32LE
:
560 case AV_CODEC_ID_PCM_F24LE
:
561 case AV_CODEC_ID_PCM_F16LE
:
563 case AV_CODEC_ID_PCM_F64BE
:
564 case AV_CODEC_ID_PCM_F64LE
:
565 case AV_CODEC_ID_PCM_S64BE
:
566 case AV_CODEC_ID_PCM_S64LE
:
573 enum AVCodecID
av_get_pcm_codec(enum AVSampleFormat fmt
, int be
)
575 static const enum AVCodecID map
[][2] = {
576 [AV_SAMPLE_FMT_U8
] = { AV_CODEC_ID_PCM_U8
, AV_CODEC_ID_PCM_U8
},
577 [AV_SAMPLE_FMT_S16
] = { AV_CODEC_ID_PCM_S16LE
, AV_CODEC_ID_PCM_S16BE
},
578 [AV_SAMPLE_FMT_S32
] = { AV_CODEC_ID_PCM_S32LE
, AV_CODEC_ID_PCM_S32BE
},
579 [AV_SAMPLE_FMT_FLT
] = { AV_CODEC_ID_PCM_F32LE
, AV_CODEC_ID_PCM_F32BE
},
580 [AV_SAMPLE_FMT_DBL
] = { AV_CODEC_ID_PCM_F64LE
, AV_CODEC_ID_PCM_F64BE
},
581 [AV_SAMPLE_FMT_U8P
] = { AV_CODEC_ID_PCM_U8
, AV_CODEC_ID_PCM_U8
},
582 [AV_SAMPLE_FMT_S16P
] = { AV_CODEC_ID_PCM_S16LE
, AV_CODEC_ID_PCM_S16BE
},
583 [AV_SAMPLE_FMT_S32P
] = { AV_CODEC_ID_PCM_S32LE
, AV_CODEC_ID_PCM_S32BE
},
584 [AV_SAMPLE_FMT_S64P
] = { AV_CODEC_ID_PCM_S64LE
, AV_CODEC_ID_PCM_S64BE
},
585 [AV_SAMPLE_FMT_FLTP
] = { AV_CODEC_ID_PCM_F32LE
, AV_CODEC_ID_PCM_F32BE
},
586 [AV_SAMPLE_FMT_DBLP
] = { AV_CODEC_ID_PCM_F64LE
, AV_CODEC_ID_PCM_F64BE
},
588 if (fmt
< 0 || fmt
>= FF_ARRAY_ELEMS(map
))
589 return AV_CODEC_ID_NONE
;
590 if (be
< 0 || be
> 1)
595 int av_get_bits_per_sample(enum AVCodecID codec_id
)
598 case AV_CODEC_ID_DFPWM
:
600 case AV_CODEC_ID_ADPCM_SBPRO_2
:
602 case AV_CODEC_ID_ADPCM_SBPRO_3
:
604 case AV_CODEC_ID_ADPCM_SBPRO_4
:
605 case AV_CODEC_ID_ADPCM_IMA_WAV
:
606 case AV_CODEC_ID_ADPCM_IMA_QT
:
607 case AV_CODEC_ID_ADPCM_SWF
:
608 case AV_CODEC_ID_ADPCM_MS
:
611 return av_get_exact_bits_per_sample(codec_id
);
615 static int get_audio_frame_duration(enum AVCodecID id
, int sr
, int ch
, int ba
,
616 uint32_t tag
, int bits_per_coded_sample
, int64_t bitrate
,
617 uint8_t * extradata
, int frame_size
, int frame_bytes
)
619 int bps
= av_get_exact_bits_per_sample(id
);
620 int framecount
= (ba
> 0 && frame_bytes
/ ba
> 0) ? frame_bytes
/ ba
: 1;
622 /* codecs with an exact constant bits per sample */
623 if (bps
> 0 && ch
> 0 && frame_bytes
> 0 && ch
< 32768 && bps
< 32768)
624 return (frame_bytes
* 8LL) / (bps
* ch
);
625 bps
= bits_per_coded_sample
;
627 /* codecs with a fixed packet duration */
629 case AV_CODEC_ID_ADPCM_ADX
: return 32;
630 case AV_CODEC_ID_ADPCM_IMA_QT
: return 64;
631 case AV_CODEC_ID_ADPCM_EA_XAS
: return 128;
632 case AV_CODEC_ID_AMR_NB
:
633 case AV_CODEC_ID_EVRC
:
634 case AV_CODEC_ID_GSM
:
635 case AV_CODEC_ID_QCELP
:
636 case AV_CODEC_ID_RA_288
: return 160;
637 case AV_CODEC_ID_AMR_WB
:
638 case AV_CODEC_ID_GSM_MS
: return 320;
639 case AV_CODEC_ID_MP1
: return 384;
640 case AV_CODEC_ID_ATRAC1
: return 512;
641 case AV_CODEC_ID_ATRAC9
:
642 case AV_CODEC_ID_ATRAC3
:
643 if (framecount
> INT_MAX
/1024)
645 return 1024 * framecount
;
646 case AV_CODEC_ID_ATRAC3P
: return 2048;
647 case AV_CODEC_ID_MP2
:
648 case AV_CODEC_ID_MUSEPACK7
: return 1152;
649 case AV_CODEC_ID_AC3
: return 1536;
653 /* calc from sample rate */
654 if (id
== AV_CODEC_ID_TTA
)
655 return 256ll * sr
/ 245;
656 else if (id
== AV_CODEC_ID_DST
)
657 return 588ll * sr
/ 44100;
658 else if (id
== AV_CODEC_ID_BINKAUDIO_DCT
) {
661 return (480 << (sr
/ 22050));
664 if (id
== AV_CODEC_ID_MP3
)
665 return sr
<= 24000 ? 576 : 1152;
669 /* calc from block_align */
670 if (id
== AV_CODEC_ID_SIPR
) {
677 } else if (id
== AV_CODEC_ID_ILBC
) {
685 if (frame_bytes
> 0) {
686 /* calc from frame_bytes only */
687 if (id
== AV_CODEC_ID_TRUESPEECH
)
688 return 240 * (frame_bytes
/ 32);
689 if (id
== AV_CODEC_ID_NELLYMOSER
)
690 return 256 * (frame_bytes
/ 64);
691 if (id
== AV_CODEC_ID_RA_144
)
692 return 160 * (frame_bytes
/ 20);
695 /* calc from frame_bytes and bits_per_coded_sample */
696 if (id
== AV_CODEC_ID_ADPCM_G726
|| id
== AV_CODEC_ID_ADPCM_G726LE
)
697 return frame_bytes
* 8 / bps
;
700 if (ch
> 0 && ch
< INT_MAX
/16) {
701 /* calc from frame_bytes and channels */
703 case AV_CODEC_ID_FASTAUDIO
:
704 return frame_bytes
/ (40 * ch
) * 256;
705 case AV_CODEC_ID_ADPCM_IMA_MOFLEX
:
706 return (frame_bytes
- 4 * ch
) / (128 * ch
) * 256;
707 case AV_CODEC_ID_ADPCM_AFC
:
708 return frame_bytes
/ (9 * ch
) * 16;
709 case AV_CODEC_ID_ADPCM_PSX
:
710 case AV_CODEC_ID_ADPCM_DTK
:
711 frame_bytes
/= 16 * ch
;
712 if (frame_bytes
> INT_MAX
/ 28)
714 return frame_bytes
* 28;
715 case AV_CODEC_ID_ADPCM_4XM
:
716 case AV_CODEC_ID_ADPCM_IMA_ACORN
:
717 case AV_CODEC_ID_ADPCM_IMA_DAT4
:
718 case AV_CODEC_ID_ADPCM_IMA_ISS
:
719 return (frame_bytes
- 4 * ch
) * 2 / ch
;
720 case AV_CODEC_ID_ADPCM_IMA_SMJPEG
:
721 return (frame_bytes
- 4) * 2 / ch
;
722 case AV_CODEC_ID_ADPCM_IMA_AMV
:
723 return (frame_bytes
- 8) * 2;
724 case AV_CODEC_ID_ADPCM_THP
:
725 case AV_CODEC_ID_ADPCM_THP_LE
:
727 return frame_bytes
* 14LL / (8 * ch
);
729 case AV_CODEC_ID_ADPCM_XA
:
730 return (frame_bytes
/ 128) * 224 / ch
;
731 case AV_CODEC_ID_INTERPLAY_DPCM
:
732 return (frame_bytes
- 6 - ch
) / ch
;
733 case AV_CODEC_ID_ROQ_DPCM
:
734 return (frame_bytes
- 8) / ch
;
735 case AV_CODEC_ID_XAN_DPCM
:
736 return (frame_bytes
- 2 * ch
) / ch
;
737 case AV_CODEC_ID_MACE3
:
738 return 3 * frame_bytes
/ ch
;
739 case AV_CODEC_ID_MACE6
:
740 return 6 * frame_bytes
/ ch
;
741 case AV_CODEC_ID_PCM_LXF
:
742 return 2 * (frame_bytes
/ (5 * ch
));
743 case AV_CODEC_ID_IAC
:
744 case AV_CODEC_ID_IMC
:
745 return 4 * frame_bytes
/ ch
;
749 /* calc from frame_bytes, channels, and codec_tag */
750 if (id
== AV_CODEC_ID_SOL_DPCM
) {
752 return frame_bytes
/ ch
;
754 return frame_bytes
* 2 / ch
;
759 /* calc from frame_bytes, channels, and block_align */
760 int blocks
= frame_bytes
/ ba
;
763 case AV_CODEC_ID_ADPCM_IMA_WAV
:
764 if (bps
< 2 || bps
> 5)
766 tmp
= blocks
* (1LL + (ba
- 4 * ch
) / (bps
* ch
) * 8LL);
768 case AV_CODEC_ID_ADPCM_IMA_DK3
:
769 tmp
= blocks
* (((ba
- 16LL) * 2 / 3 * 4) / ch
);
771 case AV_CODEC_ID_ADPCM_IMA_DK4
:
772 tmp
= blocks
* (1 + (ba
- 4LL * ch
) * 2 / ch
);
774 case AV_CODEC_ID_ADPCM_IMA_RAD
:
775 tmp
= blocks
* ((ba
- 4LL * ch
) * 2 / ch
);
777 case AV_CODEC_ID_ADPCM_MS
:
778 tmp
= blocks
* (2 + (ba
- 7LL * ch
) * 2LL / ch
);
780 case AV_CODEC_ID_ADPCM_MTAF
:
781 tmp
= blocks
* (ba
- 16LL) * 2 / ch
;
792 /* calc from frame_bytes, channels, and bits_per_coded_sample */
794 case AV_CODEC_ID_PCM_DVD
:
795 if(bps
<4 || frame_bytes
<3)
797 return 2 * ((frame_bytes
- 3) / ((bps
* 2 / 8) * ch
));
798 case AV_CODEC_ID_PCM_BLURAY
:
799 if(bps
<4 || frame_bytes
<4)
801 return (frame_bytes
- 4) / ((FFALIGN(ch
, 2) * bps
) / 8);
802 case AV_CODEC_ID_S302M
:
803 return 2 * (frame_bytes
/ ((bps
+ 4) / 4)) / ch
;
809 /* Fall back on using frame_size */
810 if (frame_size
> 1 && frame_bytes
)
813 //For WMA we currently have no other means to calculate duration thus we
814 //do it here by assuming CBR, which is true for all known cases.
815 if (bitrate
> 0 && frame_bytes
> 0 && sr
> 0 && ba
> 1) {
816 if (id
== AV_CODEC_ID_WMAV1
|| id
== AV_CODEC_ID_WMAV2
)
817 return (frame_bytes
* 8LL * sr
) / bitrate
;
823 int av_get_audio_frame_duration(AVCodecContext
*avctx
, int frame_bytes
)
825 int channels
= avctx
->ch_layout
.nb_channels
;
827 #if FF_API_OLD_CHANNEL_LAYOUT
828 FF_DISABLE_DEPRECATION_WARNINGS
830 channels
= avctx
->channels
;
831 FF_ENABLE_DEPRECATION_WARNINGS
833 duration
= get_audio_frame_duration(avctx
->codec_id
, avctx
->sample_rate
,
834 channels
, avctx
->block_align
,
835 avctx
->codec_tag
, avctx
->bits_per_coded_sample
,
836 avctx
->bit_rate
, avctx
->extradata
, avctx
->frame_size
,
838 return FFMAX(0, duration
);
841 int av_get_audio_frame_duration2(AVCodecParameters
*par
, int frame_bytes
)
843 int channels
= par
->ch_layout
.nb_channels
;
845 #if FF_API_OLD_CHANNEL_LAYOUT
846 FF_DISABLE_DEPRECATION_WARNINGS
848 channels
= par
->channels
;
849 FF_ENABLE_DEPRECATION_WARNINGS
851 duration
= get_audio_frame_duration(par
->codec_id
, par
->sample_rate
,
852 channels
, par
->block_align
,
853 par
->codec_tag
, par
->bits_per_coded_sample
,
854 par
->bit_rate
, par
->extradata
, par
->frame_size
,
856 return FFMAX(0, duration
);
860 int ff_thread_init(AVCodecContext
*s
)
867 unsigned int av_xiphlacing(unsigned char *s
, unsigned int v
)
881 int ff_match_2uint16(const uint16_t(*tab
)[2], int size
, int a
, int b
)
884 for (i
= 0; i
< size
&& !(tab
[i
][0] == a
&& tab
[i
][1] == b
); i
++) ;
888 const AVCodecHWConfig
*avcodec_get_hw_config(const AVCodec
*avcodec
, int index
)
890 const FFCodec
*const codec
= ffcodec(avcodec
);
892 if (!codec
->hw_configs
|| index
< 0)
894 for (i
= 0; i
<= index
; i
++)
895 if (!codec
->hw_configs
[i
])
897 return &codec
->hw_configs
[index
]->public;
900 int ff_thread_ref_frame(ThreadFrame
*dst
, const ThreadFrame
*src
)
904 dst
->owner
[0] = src
->owner
[0];
905 dst
->owner
[1] = src
->owner
[1];
907 ret
= av_frame_ref(dst
->f
, src
->f
);
911 av_assert0(!dst
->progress
);
914 !(dst
->progress
= av_buffer_ref(src
->progress
))) {
915 ff_thread_release_ext_buffer(dst
->owner
[0], dst
);
916 return AVERROR(ENOMEM
);
924 enum AVPixelFormat
ff_thread_get_format(AVCodecContext
*avctx
, const enum AVPixelFormat
*fmt
)
926 return ff_get_format(avctx
, fmt
);
929 int ff_thread_get_buffer(AVCodecContext
*avctx
, AVFrame
*f
, int flags
)
931 return ff_get_buffer(avctx
, f
, flags
);
934 int ff_thread_get_ext_buffer(AVCodecContext
*avctx
, ThreadFrame
*f
, int flags
)
936 f
->owner
[0] = f
->owner
[1] = avctx
;
937 return ff_get_buffer(avctx
, f
->f
, flags
);
940 void ff_thread_release_buffer(AVCodecContext
*avctx
, AVFrame
*f
)
946 void ff_thread_release_ext_buffer(AVCodecContext
*avctx
, ThreadFrame
*f
)
948 f
->owner
[0] = f
->owner
[1] = NULL
;
950 av_frame_unref(f
->f
);
953 void ff_thread_finish_setup(AVCodecContext
*avctx
)
957 void ff_thread_report_progress(ThreadFrame
*f
, int progress
, int field
)
961 void ff_thread_await_progress(ThreadFrame
*f
, int progress
, int field
)
965 int ff_thread_can_start_frame(AVCodecContext
*avctx
)
970 int ff_slice_thread_init_progress(AVCodecContext
*avctx
)
975 int ff_alloc_entries(AVCodecContext
*avctx
, int count
)
980 void ff_reset_entries(AVCodecContext
*avctx
)
984 void ff_thread_await_progress2(AVCodecContext
*avctx
, int field
, int thread
, int shift
)
988 void ff_thread_report_progress2(AVCodecContext
*avctx
, int field
, int thread
, int n
)
994 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p
,
996 uint32_t *av_restrict state
)
1000 av_assert0(p
<= end
);
1004 for (i
= 0; i
< 3; i
++) {
1005 uint32_t tmp
= *state
<< 8;
1006 *state
= tmp
+ *(p
++);
1007 if (tmp
== 0x100 || p
== end
)
1012 if (p
[-1] > 1 ) p
+= 3;
1013 else if (p
[-2] ) p
+= 2;
1014 else if (p
[-3]|(p
[-1]-1)) p
++;
1021 p
= FFMIN(p
, end
) - 4;
1022 *state
= AV_RB32(p
);
1027 AVCPBProperties
*av_cpb_properties_alloc(size_t *size
)
1029 AVCPBProperties
*props
= av_mallocz(sizeof(AVCPBProperties
));
1034 *size
= sizeof(*props
);
1036 props
->vbv_delay
= UINT64_MAX
;
1041 AVCPBProperties
*ff_add_cpb_side_data(AVCodecContext
*avctx
)
1043 AVPacketSideData
*tmp
;
1044 AVCPBProperties
*props
;
1048 for (i
= 0; i
< avctx
->nb_coded_side_data
; i
++)
1049 if (avctx
->coded_side_data
[i
].type
== AV_PKT_DATA_CPB_PROPERTIES
)
1050 return (AVCPBProperties
*)avctx
->coded_side_data
[i
].data
;
1052 props
= av_cpb_properties_alloc(&size
);
1056 tmp
= av_realloc_array(avctx
->coded_side_data
, avctx
->nb_coded_side_data
+ 1, sizeof(*tmp
));
1062 avctx
->coded_side_data
= tmp
;
1063 avctx
->nb_coded_side_data
++;
1065 avctx
->coded_side_data
[avctx
->nb_coded_side_data
- 1].type
= AV_PKT_DATA_CPB_PROPERTIES
;
1066 avctx
->coded_side_data
[avctx
->nb_coded_side_data
- 1].data
= (uint8_t*)props
;
1067 avctx
->coded_side_data
[avctx
->nb_coded_side_data
- 1].size
= size
;
1072 static unsigned bcd2uint(uint8_t bcd
)
1074 unsigned low
= bcd
& 0xf;
1075 unsigned high
= bcd
>> 4;
1076 if (low
> 9 || high
> 9)
1078 return low
+ 10*high
;
1081 int ff_alloc_timecode_sei(const AVFrame
*frame
, AVRational rate
, size_t prefix_len
,
1082 void **data
, size_t *sei_size
)
1084 AVFrameSideData
*sd
= NULL
;
1091 sd
= av_frame_get_side_data(frame
, AV_FRAME_DATA_S12M_TIMECODE
);
1097 tc
= (uint32_t*)sd
->data
;
1100 *sei_size
= sizeof(uint32_t) * 4;
1101 *data
= av_mallocz(*sei_size
+ prefix_len
);
1103 return AVERROR(ENOMEM
);
1104 sei_data
= (uint8_t*)*data
+ prefix_len
;
1106 init_put_bits(&pb
, sei_data
, *sei_size
);
1107 put_bits(&pb
, 2, m
); // num_clock_ts
1109 for (int j
= 1; j
<= m
; j
++) {
1110 uint32_t tcsmpte
= tc
[j
];
1111 unsigned hh
= bcd2uint(tcsmpte
& 0x3f); // 6-bit hours
1112 unsigned mm
= bcd2uint(tcsmpte
>>8 & 0x7f); // 7-bit minutes
1113 unsigned ss
= bcd2uint(tcsmpte
>>16 & 0x7f); // 7-bit seconds
1114 unsigned ff
= bcd2uint(tcsmpte
>>24 & 0x3f); // 6-bit frames
1115 unsigned drop
= tcsmpte
& 1<<30 && !0; // 1-bit drop if not arbitrary bit
1117 /* Calculate frame number of HEVC by SMPTE ST 12-1:2014 Sec 12.2 if rate > 30FPS */
1118 if (av_cmp_q(rate
, (AVRational
) {30, 1}) == 1) {
1121 if (av_cmp_q(rate
, (AVRational
) {50, 1}) == 0)
1122 pc
= !!(tcsmpte
& 1 << 7);
1124 pc
= !!(tcsmpte
& 1 << 23);
1125 ff
= (ff
+ pc
) & 0x7f;
1128 put_bits(&pb
, 1, 1); // clock_timestamp_flag
1129 put_bits(&pb
, 1, 1); // units_field_based_flag
1130 put_bits(&pb
, 5, 0); // counting_type
1131 put_bits(&pb
, 1, 1); // full_timestamp_flag
1132 put_bits(&pb
, 1, 0); // discontinuity_flag
1133 put_bits(&pb
, 1, drop
);
1134 put_bits(&pb
, 9, ff
);
1135 put_bits(&pb
, 6, ss
);
1136 put_bits(&pb
, 6, mm
);
1137 put_bits(&pb
, 5, hh
);
1138 put_bits(&pb
, 5, 0);
1140 flush_put_bits(&pb
);
1145 int64_t ff_guess_coded_bitrate(AVCodecContext
*avctx
)
1147 AVRational framerate
= avctx
->framerate
;
1148 int bits_per_coded_sample
= avctx
->bits_per_coded_sample
;
1151 if (!(framerate
.num
&& framerate
.den
))
1152 framerate
= av_inv_q(avctx
->time_base
);
1153 if (!(framerate
.num
&& framerate
.den
))
1156 if (!bits_per_coded_sample
) {
1157 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(avctx
->pix_fmt
);
1158 bits_per_coded_sample
= av_get_bits_per_pixel(desc
);
1160 bitrate
= (int64_t)bits_per_coded_sample
* avctx
->width
* avctx
->height
*
1161 framerate
.num
/ framerate
.den
;
1166 int ff_int_from_list_or_default(void *ctx
, const char * val_name
, int val
,
1167 const int * array_valid_values
, int default_value
)
1172 ref_val
= array_valid_values
[i
];
1173 if (ref_val
== INT_MAX
)
1179 /* val is not a valid value */
1180 av_log(ctx
, AV_LOG_DEBUG
,
1181 "%s %d are not supported. Set to default value : %d\n", val_name
, val
, default_value
);
1182 return default_value
;