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/atomic.h"
30 #include "libavutil/attributes.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/avstring.h"
33 #include "libavutil/bprint.h"
34 #include "libavutil/channel_layout.h"
35 #include "libavutil/crc.h"
36 #include "libavutil/frame.h"
37 #include "libavutil/internal.h"
38 #include "libavutil/mathematics.h"
39 #include "libavutil/pixdesc.h"
40 #include "libavutil/imgutils.h"
41 #include "libavutil/samplefmt.h"
42 #include "libavutil/dict.h"
44 #include "libavutil/opt.h"
46 #include "mpegvideo.h"
48 #include "frame_thread_encoder.h"
51 #include "bytestream.h"
64 #include "compat/w32pthreads.h"
66 #include "compat/os2threads.h"
69 #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
70 static int default_lockmgr_cb(void **arg
, enum AVLockOp op
)
72 void * volatile * mutex
= arg
;
80 pthread_mutex_t
*tmp
= av_malloc(sizeof(pthread_mutex_t
));
82 return AVERROR(ENOMEM
);
83 if ((err
= pthread_mutex_init(tmp
, NULL
))) {
87 if (avpriv_atomic_ptr_cas(mutex
, NULL
, tmp
)) {
88 pthread_mutex_destroy(tmp
);
93 if ((err
= pthread_mutex_lock(*mutex
)))
98 if ((err
= pthread_mutex_unlock(*mutex
)))
102 case AV_LOCK_DESTROY
:
104 pthread_mutex_destroy(*mutex
);
106 avpriv_atomic_ptr_cas(mutex
, *mutex
, NULL
);
111 static int (*lockmgr_cb
)(void **mutex
, enum AVLockOp op
) = default_lockmgr_cb
;
113 static int (*lockmgr_cb
)(void **mutex
, enum AVLockOp op
) = NULL
;
117 volatile int ff_avcodec_locked
;
118 static int volatile entangled_thread_counter
= 0;
119 static void *codec_mutex
;
120 static void *avformat_mutex
;
122 static inline int ff_fast_malloc(void *ptr
, unsigned int *size
, size_t min_size
, int zero_realloc
)
125 if (min_size
< *size
)
127 min_size
= FFMAX(17 * min_size
/ 16 + 32, min_size
);
129 *p
= zero_realloc
? av_mallocz(min_size
) : av_malloc(min_size
);
136 void av_fast_padded_malloc(void *ptr
, unsigned int *size
, size_t min_size
)
139 if (min_size
> SIZE_MAX
- FF_INPUT_BUFFER_PADDING_SIZE
) {
144 if (!ff_fast_malloc(p
, size
, min_size
+ FF_INPUT_BUFFER_PADDING_SIZE
, 1))
145 memset(*p
+ min_size
, 0, FF_INPUT_BUFFER_PADDING_SIZE
);
148 void av_fast_padded_mallocz(void *ptr
, unsigned int *size
, size_t min_size
)
151 if (min_size
> SIZE_MAX
- FF_INPUT_BUFFER_PADDING_SIZE
) {
156 if (!ff_fast_malloc(p
, size
, min_size
+ FF_INPUT_BUFFER_PADDING_SIZE
, 1))
157 memset(*p
, 0, min_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
160 /* encoder management */
161 static AVCodec
*first_avcodec
= NULL
;
162 static AVCodec
**last_avcodec
= &first_avcodec
;
164 AVCodec
*av_codec_next(const AVCodec
*c
)
169 return first_avcodec
;
172 static av_cold
void avcodec_init(void)
174 static int initialized
= 0;
176 if (initialized
!= 0)
181 ff_me_cmp_init_static();
184 int av_codec_is_encoder(const AVCodec
*codec
)
186 return codec
&& (codec
->encode_sub
|| codec
->encode2
);
189 int av_codec_is_decoder(const AVCodec
*codec
)
191 return codec
&& codec
->decode
;
194 av_cold
void avcodec_register(AVCodec
*codec
)
201 while(*p
|| avpriv_atomic_ptr_cas((void * volatile *)p
, NULL
, codec
))
203 last_avcodec
= &codec
->next
;
205 if (codec
->init_static_data
)
206 codec
->init_static_data(codec
);
210 unsigned avcodec_get_edge_width(void)
216 #if FF_API_SET_DIMENSIONS
217 void avcodec_set_dimensions(AVCodecContext
*s
, int width
, int height
)
219 int ret
= ff_set_dimensions(s
, width
, height
);
221 av_log(s
, AV_LOG_WARNING
, "Failed to set dimensions %d %d\n", width
, height
);
226 int ff_set_dimensions(AVCodecContext
*s
, int width
, int height
)
228 int ret
= av_image_check_size(width
, height
, 0, s
);
233 s
->coded_width
= width
;
234 s
->coded_height
= height
;
235 s
->width
= FF_CEIL_RSHIFT(width
, s
->lowres
);
236 s
->height
= FF_CEIL_RSHIFT(height
, s
->lowres
);
241 int ff_set_sar(AVCodecContext
*avctx
, AVRational sar
)
243 int ret
= av_image_check_sar(avctx
->width
, avctx
->height
, sar
);
246 av_log(avctx
, AV_LOG_WARNING
, "ignoring invalid SAR: %u/%u\n",
248 avctx
->sample_aspect_ratio
= (AVRational
){ 0, 1 };
251 avctx
->sample_aspect_ratio
= sar
;
256 int ff_side_data_update_matrix_encoding(AVFrame
*frame
,
257 enum AVMatrixEncoding matrix_encoding
)
259 AVFrameSideData
*side_data
;
260 enum AVMatrixEncoding
*data
;
262 side_data
= av_frame_get_side_data(frame
, AV_FRAME_DATA_MATRIXENCODING
);
264 side_data
= av_frame_new_side_data(frame
, AV_FRAME_DATA_MATRIXENCODING
,
265 sizeof(enum AVMatrixEncoding
));
268 return AVERROR(ENOMEM
);
270 data
= (enum AVMatrixEncoding
*)side_data
->data
;
271 *data
= matrix_encoding
;
276 void avcodec_align_dimensions2(AVCodecContext
*s
, int *width
, int *height
,
277 int linesize_align
[AV_NUM_DATA_POINTERS
])
282 AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(s
->pix_fmt
);
285 w_align
= 1 << desc
->log2_chroma_w
;
286 h_align
= 1 << desc
->log2_chroma_h
;
289 switch (s
->pix_fmt
) {
290 case AV_PIX_FMT_YUV420P
:
291 case AV_PIX_FMT_YUYV422
:
292 case AV_PIX_FMT_YVYU422
:
293 case AV_PIX_FMT_UYVY422
:
294 case AV_PIX_FMT_YUV422P
:
295 case AV_PIX_FMT_YUV440P
:
296 case AV_PIX_FMT_YUV444P
:
297 case AV_PIX_FMT_GBRAP
:
298 case AV_PIX_FMT_GBRP
:
299 case AV_PIX_FMT_GRAY8
:
300 case AV_PIX_FMT_GRAY16BE
:
301 case AV_PIX_FMT_GRAY16LE
:
302 case AV_PIX_FMT_YUVJ420P
:
303 case AV_PIX_FMT_YUVJ422P
:
304 case AV_PIX_FMT_YUVJ440P
:
305 case AV_PIX_FMT_YUVJ444P
:
306 case AV_PIX_FMT_YUVA420P
:
307 case AV_PIX_FMT_YUVA422P
:
308 case AV_PIX_FMT_YUVA444P
:
309 case AV_PIX_FMT_YUV420P9LE
:
310 case AV_PIX_FMT_YUV420P9BE
:
311 case AV_PIX_FMT_YUV420P10LE
:
312 case AV_PIX_FMT_YUV420P10BE
:
313 case AV_PIX_FMT_YUV420P12LE
:
314 case AV_PIX_FMT_YUV420P12BE
:
315 case AV_PIX_FMT_YUV420P14LE
:
316 case AV_PIX_FMT_YUV420P14BE
:
317 case AV_PIX_FMT_YUV420P16LE
:
318 case AV_PIX_FMT_YUV420P16BE
:
319 case AV_PIX_FMT_YUVA420P9LE
:
320 case AV_PIX_FMT_YUVA420P9BE
:
321 case AV_PIX_FMT_YUVA420P10LE
:
322 case AV_PIX_FMT_YUVA420P10BE
:
323 case AV_PIX_FMT_YUVA420P16LE
:
324 case AV_PIX_FMT_YUVA420P16BE
:
325 case AV_PIX_FMT_YUV422P9LE
:
326 case AV_PIX_FMT_YUV422P9BE
:
327 case AV_PIX_FMT_YUV422P10LE
:
328 case AV_PIX_FMT_YUV422P10BE
:
329 case AV_PIX_FMT_YUV422P12LE
:
330 case AV_PIX_FMT_YUV422P12BE
:
331 case AV_PIX_FMT_YUV422P14LE
:
332 case AV_PIX_FMT_YUV422P14BE
:
333 case AV_PIX_FMT_YUV422P16LE
:
334 case AV_PIX_FMT_YUV422P16BE
:
335 case AV_PIX_FMT_YUVA422P9LE
:
336 case AV_PIX_FMT_YUVA422P9BE
:
337 case AV_PIX_FMT_YUVA422P10LE
:
338 case AV_PIX_FMT_YUVA422P10BE
:
339 case AV_PIX_FMT_YUVA422P16LE
:
340 case AV_PIX_FMT_YUVA422P16BE
:
341 case AV_PIX_FMT_YUV444P9LE
:
342 case AV_PIX_FMT_YUV444P9BE
:
343 case AV_PIX_FMT_YUV444P10LE
:
344 case AV_PIX_FMT_YUV444P10BE
:
345 case AV_PIX_FMT_YUV444P12LE
:
346 case AV_PIX_FMT_YUV444P12BE
:
347 case AV_PIX_FMT_YUV444P14LE
:
348 case AV_PIX_FMT_YUV444P14BE
:
349 case AV_PIX_FMT_YUV444P16LE
:
350 case AV_PIX_FMT_YUV444P16BE
:
351 case AV_PIX_FMT_YUVA444P9LE
:
352 case AV_PIX_FMT_YUVA444P9BE
:
353 case AV_PIX_FMT_YUVA444P10LE
:
354 case AV_PIX_FMT_YUVA444P10BE
:
355 case AV_PIX_FMT_YUVA444P16LE
:
356 case AV_PIX_FMT_YUVA444P16BE
:
357 case AV_PIX_FMT_GBRP9LE
:
358 case AV_PIX_FMT_GBRP9BE
:
359 case AV_PIX_FMT_GBRP10LE
:
360 case AV_PIX_FMT_GBRP10BE
:
361 case AV_PIX_FMT_GBRP12LE
:
362 case AV_PIX_FMT_GBRP12BE
:
363 case AV_PIX_FMT_GBRP14LE
:
364 case AV_PIX_FMT_GBRP14BE
:
365 case AV_PIX_FMT_GBRP16LE
:
366 case AV_PIX_FMT_GBRP16BE
:
367 w_align
= 16; //FIXME assume 16 pixel per macroblock
368 h_align
= 16 * 2; // interlaced needs 2 macroblocks height
370 case AV_PIX_FMT_YUV411P
:
371 case AV_PIX_FMT_YUVJ411P
:
372 case AV_PIX_FMT_UYYVYY411
:
376 case AV_PIX_FMT_YUV410P
:
377 if (s
->codec_id
== AV_CODEC_ID_SVQ1
) {
382 case AV_PIX_FMT_RGB555
:
383 if (s
->codec_id
== AV_CODEC_ID_RPZA
) {
388 case AV_PIX_FMT_PAL8
:
389 case AV_PIX_FMT_BGR8
:
390 case AV_PIX_FMT_RGB8
:
391 if (s
->codec_id
== AV_CODEC_ID_SMC
||
392 s
->codec_id
== AV_CODEC_ID_CINEPAK
) {
396 if (s
->codec_id
== AV_CODEC_ID_JV
) {
401 case AV_PIX_FMT_BGR24
:
402 if ((s
->codec_id
== AV_CODEC_ID_MSZH
) ||
403 (s
->codec_id
== AV_CODEC_ID_ZLIB
)) {
408 case AV_PIX_FMT_RGB24
:
409 if (s
->codec_id
== AV_CODEC_ID_CINEPAK
) {
418 if (s
->codec_id
== AV_CODEC_ID_IFF_ILBM
|| s
->codec_id
== AV_CODEC_ID_IFF_BYTERUN1
) {
419 w_align
= FFMAX(w_align
, 8);
422 *width
= FFALIGN(*width
, w_align
);
423 *height
= FFALIGN(*height
, h_align
);
424 if (s
->codec_id
== AV_CODEC_ID_H264
|| s
->lowres
)
425 // some of the optimized chroma MC reads one line too much
426 // which is also done in mpeg decoders with lowres > 0
429 for (i
= 0; i
< 4; i
++)
430 linesize_align
[i
] = STRIDE_ALIGN
;
433 void avcodec_align_dimensions(AVCodecContext
*s
, int *width
, int *height
)
435 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(s
->pix_fmt
);
436 int chroma_shift
= desc
->log2_chroma_w
;
437 int linesize_align
[AV_NUM_DATA_POINTERS
];
440 avcodec_align_dimensions2(s
, width
, height
, linesize_align
);
441 align
= FFMAX(linesize_align
[0], linesize_align
[3]);
442 linesize_align
[1] <<= chroma_shift
;
443 linesize_align
[2] <<= chroma_shift
;
444 align
= FFMAX3(align
, linesize_align
[1], linesize_align
[2]);
445 *width
= FFALIGN(*width
, align
);
448 int avcodec_enum_to_chroma_pos(int *xpos
, int *ypos
, enum AVChromaLocation pos
)
450 if (pos
<= AVCHROMA_LOC_UNSPECIFIED
|| pos
>= AVCHROMA_LOC_NB
)
451 return AVERROR(EINVAL
);
454 *xpos
= (pos
&1) * 128;
455 *ypos
= ((pos
>>1)^(pos
<4)) * 128;
460 enum AVChromaLocation
avcodec_chroma_pos_to_enum(int xpos
, int ypos
)
464 for (pos
= AVCHROMA_LOC_UNSPECIFIED
+ 1; pos
< AVCHROMA_LOC_NB
; pos
++) {
465 if (avcodec_enum_to_chroma_pos(&xout
, &yout
, pos
) == 0 && xout
== xpos
&& yout
== ypos
)
468 return AVCHROMA_LOC_UNSPECIFIED
;
471 int avcodec_fill_audio_frame(AVFrame
*frame
, int nb_channels
,
472 enum AVSampleFormat sample_fmt
, const uint8_t *buf
,
473 int buf_size
, int align
)
475 int ch
, planar
, needed_size
, ret
= 0;
477 needed_size
= av_samples_get_buffer_size(NULL
, nb_channels
,
478 frame
->nb_samples
, sample_fmt
,
480 if (buf_size
< needed_size
)
481 return AVERROR(EINVAL
);
483 planar
= av_sample_fmt_is_planar(sample_fmt
);
484 if (planar
&& nb_channels
> AV_NUM_DATA_POINTERS
) {
485 if (!(frame
->extended_data
= av_mallocz_array(nb_channels
,
486 sizeof(*frame
->extended_data
))))
487 return AVERROR(ENOMEM
);
489 frame
->extended_data
= frame
->data
;
492 if ((ret
= av_samples_fill_arrays(frame
->extended_data
, &frame
->linesize
[0],
493 (uint8_t *)(intptr_t)buf
, nb_channels
, frame
->nb_samples
,
494 sample_fmt
, align
)) < 0) {
495 if (frame
->extended_data
!= frame
->data
)
496 av_freep(&frame
->extended_data
);
499 if (frame
->extended_data
!= frame
->data
) {
500 for (ch
= 0; ch
< AV_NUM_DATA_POINTERS
; ch
++)
501 frame
->data
[ch
] = frame
->extended_data
[ch
];
507 static int update_frame_pool(AVCodecContext
*avctx
, AVFrame
*frame
)
509 FramePool
*pool
= avctx
->internal
->pool
;
512 switch (avctx
->codec_type
) {
513 case AVMEDIA_TYPE_VIDEO
: {
516 int w
= frame
->width
;
517 int h
= frame
->height
;
518 int tmpsize
, unaligned
;
520 if (pool
->format
== frame
->format
&&
521 pool
->width
== frame
->width
&& pool
->height
== frame
->height
)
524 avcodec_align_dimensions2(avctx
, &w
, &h
, pool
->stride_align
);
527 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
528 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
529 av_image_fill_linesizes(picture
.linesize
, avctx
->pix_fmt
, w
);
530 // increase alignment of w for next try (rhs gives the lowest bit set in w)
534 for (i
= 0; i
< 4; i
++)
535 unaligned
|= picture
.linesize
[i
] % pool
->stride_align
[i
];
538 tmpsize
= av_image_fill_pointers(picture
.data
, avctx
->pix_fmt
, h
,
539 NULL
, picture
.linesize
);
543 for (i
= 0; i
< 3 && picture
.data
[i
+ 1]; i
++)
544 size
[i
] = picture
.data
[i
+ 1] - picture
.data
[i
];
545 size
[i
] = tmpsize
- (picture
.data
[i
] - picture
.data
[0]);
547 for (i
= 0; i
< 4; i
++) {
548 av_buffer_pool_uninit(&pool
->pools
[i
]);
549 pool
->linesize
[i
] = picture
.linesize
[i
];
551 pool
->pools
[i
] = av_buffer_pool_init(size
[i
] + 16 + STRIDE_ALIGN
- 1,
552 CONFIG_MEMORY_POISONING
?
555 if (!pool
->pools
[i
]) {
556 ret
= AVERROR(ENOMEM
);
561 pool
->format
= frame
->format
;
562 pool
->width
= frame
->width
;
563 pool
->height
= frame
->height
;
567 case AVMEDIA_TYPE_AUDIO
: {
568 int ch
= av_frame_get_channels(frame
); //av_get_channel_layout_nb_channels(frame->channel_layout);
569 int planar
= av_sample_fmt_is_planar(frame
->format
);
570 int planes
= planar
? ch
: 1;
572 if (pool
->format
== frame
->format
&& pool
->planes
== planes
&&
573 pool
->channels
== ch
&& frame
->nb_samples
== pool
->samples
)
576 av_buffer_pool_uninit(&pool
->pools
[0]);
577 ret
= av_samples_get_buffer_size(&pool
->linesize
[0], ch
,
578 frame
->nb_samples
, frame
->format
, 0);
582 pool
->pools
[0] = av_buffer_pool_init(pool
->linesize
[0], NULL
);
583 if (!pool
->pools
[0]) {
584 ret
= AVERROR(ENOMEM
);
588 pool
->format
= frame
->format
;
589 pool
->planes
= planes
;
591 pool
->samples
= frame
->nb_samples
;
594 default: av_assert0(0);
598 for (i
= 0; i
< 4; i
++)
599 av_buffer_pool_uninit(&pool
->pools
[i
]);
601 pool
->planes
= pool
->channels
= pool
->samples
= 0;
602 pool
->width
= pool
->height
= 0;
606 static int audio_get_buffer(AVCodecContext
*avctx
, AVFrame
*frame
)
608 FramePool
*pool
= avctx
->internal
->pool
;
609 int planes
= pool
->planes
;
612 frame
->linesize
[0] = pool
->linesize
[0];
614 if (planes
> AV_NUM_DATA_POINTERS
) {
615 frame
->extended_data
= av_mallocz_array(planes
, sizeof(*frame
->extended_data
));
616 frame
->nb_extended_buf
= planes
- AV_NUM_DATA_POINTERS
;
617 frame
->extended_buf
= av_mallocz_array(frame
->nb_extended_buf
,
618 sizeof(*frame
->extended_buf
));
619 if (!frame
->extended_data
|| !frame
->extended_buf
) {
620 av_freep(&frame
->extended_data
);
621 av_freep(&frame
->extended_buf
);
622 return AVERROR(ENOMEM
);
625 frame
->extended_data
= frame
->data
;
626 av_assert0(frame
->nb_extended_buf
== 0);
629 for (i
= 0; i
< FFMIN(planes
, AV_NUM_DATA_POINTERS
); i
++) {
630 frame
->buf
[i
] = av_buffer_pool_get(pool
->pools
[0]);
633 frame
->extended_data
[i
] = frame
->data
[i
] = frame
->buf
[i
]->data
;
635 for (i
= 0; i
< frame
->nb_extended_buf
; i
++) {
636 frame
->extended_buf
[i
] = av_buffer_pool_get(pool
->pools
[0]);
637 if (!frame
->extended_buf
[i
])
639 frame
->extended_data
[i
+ AV_NUM_DATA_POINTERS
] = frame
->extended_buf
[i
]->data
;
642 if (avctx
->debug
& FF_DEBUG_BUFFERS
)
643 av_log(avctx
, AV_LOG_DEBUG
, "default_get_buffer called on frame %p", frame
);
647 av_frame_unref(frame
);
648 return AVERROR(ENOMEM
);
651 static int video_get_buffer(AVCodecContext
*s
, AVFrame
*pic
)
653 FramePool
*pool
= s
->internal
->pool
;
657 av_log(s
, AV_LOG_ERROR
, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
661 memset(pic
->data
, 0, sizeof(pic
->data
));
662 pic
->extended_data
= pic
->data
;
664 for (i
= 0; i
< 4 && pool
->pools
[i
]; i
++) {
665 pic
->linesize
[i
] = pool
->linesize
[i
];
667 pic
->buf
[i
] = av_buffer_pool_get(pool
->pools
[i
]);
671 pic
->data
[i
] = pic
->buf
[i
]->data
;
673 for (; i
< AV_NUM_DATA_POINTERS
; i
++) {
675 pic
->linesize
[i
] = 0;
677 if (pic
->data
[1] && !pic
->data
[2])
678 avpriv_set_systematic_pal2((uint32_t *)pic
->data
[1], s
->pix_fmt
);
680 if (s
->debug
& FF_DEBUG_BUFFERS
)
681 av_log(s
, AV_LOG_DEBUG
, "default_get_buffer called on pic %p\n", pic
);
686 return AVERROR(ENOMEM
);
689 void avpriv_color_frame(AVFrame
*frame
, const int c
[4])
691 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(frame
->format
);
694 av_assert0(desc
->flags
& AV_PIX_FMT_FLAG_PLANAR
);
696 for (p
= 0; p
<desc
->nb_components
; p
++) {
697 uint8_t *dst
= frame
->data
[p
];
698 int is_chroma
= p
== 1 || p
== 2;
699 int bytes
= is_chroma
? FF_CEIL_RSHIFT(frame
->width
, desc
->log2_chroma_w
) : frame
->width
;
700 int height
= is_chroma
? FF_CEIL_RSHIFT(frame
->height
, desc
->log2_chroma_h
) : frame
->height
;
701 for (y
= 0; y
< height
; y
++) {
702 if (desc
->comp
[0].depth_minus1
>= 8) {
703 for (x
= 0; x
<bytes
; x
++)
704 ((uint16_t*)dst
)[x
] = c
[p
];
706 memset(dst
, c
[p
], bytes
);
707 dst
+= frame
->linesize
[p
];
712 int avcodec_default_get_buffer2(AVCodecContext
*avctx
, AVFrame
*frame
, int flags
)
716 if ((ret
= update_frame_pool(avctx
, frame
)) < 0)
719 #if FF_API_GET_BUFFER
720 FF_DISABLE_DEPRECATION_WARNINGS
721 frame
->type
= FF_BUFFER_TYPE_INTERNAL
;
722 FF_ENABLE_DEPRECATION_WARNINGS
725 switch (avctx
->codec_type
) {
726 case AVMEDIA_TYPE_VIDEO
:
727 return video_get_buffer(avctx
, frame
);
728 case AVMEDIA_TYPE_AUDIO
:
729 return audio_get_buffer(avctx
, frame
);
735 int ff_init_buffer_info(AVCodecContext
*avctx
, AVFrame
*frame
)
737 AVPacket
*pkt
= avctx
->internal
->pkt
;
741 AVFrameSideData
*frame_sd
;
743 frame
->pkt_pts
= pkt
->pts
;
744 av_frame_set_pkt_pos (frame
, pkt
->pos
);
745 av_frame_set_pkt_duration(frame
, pkt
->duration
);
746 av_frame_set_pkt_size (frame
, pkt
->size
);
748 /* copy the replaygain data to the output frame */
749 packet_sd
= av_packet_get_side_data(pkt
, AV_PKT_DATA_REPLAYGAIN
, &size
);
751 frame_sd
= av_frame_new_side_data(frame
, AV_FRAME_DATA_REPLAYGAIN
, size
);
753 return AVERROR(ENOMEM
);
755 memcpy(frame_sd
->data
, packet_sd
, size
);
758 /* copy the displaymatrix to the output frame */
759 packet_sd
= av_packet_get_side_data(pkt
, AV_PKT_DATA_DISPLAYMATRIX
, &size
);
761 frame_sd
= av_frame_new_side_data(frame
, AV_FRAME_DATA_DISPLAYMATRIX
, size
);
763 return AVERROR(ENOMEM
);
765 memcpy(frame_sd
->data
, packet_sd
, size
);
768 /* copy the stereo3d format to the output frame */
769 packet_sd
= av_packet_get_side_data(pkt
, AV_PKT_DATA_STEREO3D
, &size
);
771 frame_sd
= av_frame_new_side_data(frame
, AV_FRAME_DATA_STEREO3D
, size
);
773 return AVERROR(ENOMEM
);
775 memcpy(frame_sd
->data
, packet_sd
, size
);
778 frame
->pkt_pts
= AV_NOPTS_VALUE
;
779 av_frame_set_pkt_pos (frame
, -1);
780 av_frame_set_pkt_duration(frame
, 0);
781 av_frame_set_pkt_size (frame
, -1);
783 frame
->reordered_opaque
= avctx
->reordered_opaque
;
785 if (frame
->color_primaries
== AVCOL_PRI_UNSPECIFIED
)
786 frame
->color_primaries
= avctx
->color_primaries
;
787 if (frame
->color_trc
== AVCOL_TRC_UNSPECIFIED
)
788 frame
->color_trc
= avctx
->color_trc
;
789 if (av_frame_get_colorspace(frame
) == AVCOL_SPC_UNSPECIFIED
)
790 av_frame_set_colorspace(frame
, avctx
->colorspace
);
791 if (av_frame_get_color_range(frame
) == AVCOL_RANGE_UNSPECIFIED
)
792 av_frame_set_color_range(frame
, avctx
->color_range
);
793 if (frame
->chroma_location
== AVCHROMA_LOC_UNSPECIFIED
)
794 frame
->chroma_location
= avctx
->chroma_sample_location
;
796 switch (avctx
->codec
->type
) {
797 case AVMEDIA_TYPE_VIDEO
:
798 frame
->format
= avctx
->pix_fmt
;
799 if (!frame
->sample_aspect_ratio
.num
)
800 frame
->sample_aspect_ratio
= avctx
->sample_aspect_ratio
;
802 if (frame
->width
&& frame
->height
&&
803 av_image_check_sar(frame
->width
, frame
->height
,
804 frame
->sample_aspect_ratio
) < 0) {
805 av_log(avctx
, AV_LOG_WARNING
, "ignoring invalid SAR: %u/%u\n",
806 frame
->sample_aspect_ratio
.num
,
807 frame
->sample_aspect_ratio
.den
);
808 frame
->sample_aspect_ratio
= (AVRational
){ 0, 1 };
812 case AVMEDIA_TYPE_AUDIO
:
813 if (!frame
->sample_rate
)
814 frame
->sample_rate
= avctx
->sample_rate
;
815 if (frame
->format
< 0)
816 frame
->format
= avctx
->sample_fmt
;
817 if (!frame
->channel_layout
) {
818 if (avctx
->channel_layout
) {
819 if (av_get_channel_layout_nb_channels(avctx
->channel_layout
) !=
821 av_log(avctx
, AV_LOG_ERROR
, "Inconsistent channel "
823 return AVERROR(EINVAL
);
826 frame
->channel_layout
= avctx
->channel_layout
;
828 if (avctx
->channels
> FF_SANE_NB_CHANNELS
) {
829 av_log(avctx
, AV_LOG_ERROR
, "Too many channels: %d.\n",
831 return AVERROR(ENOSYS
);
835 av_frame_set_channels(frame
, avctx
->channels
);
841 #if FF_API_GET_BUFFER
842 FF_DISABLE_DEPRECATION_WARNINGS
843 int avcodec_default_get_buffer(AVCodecContext
*avctx
, AVFrame
*frame
)
845 return avcodec_default_get_buffer2(avctx
, frame
, 0);
848 typedef struct CompatReleaseBufPriv
{
849 AVCodecContext avctx
;
851 uint8_t avframe_padding
[1024]; // hack to allow linking to a avutil with larger AVFrame
852 } CompatReleaseBufPriv
;
854 static void compat_free_buffer(void *opaque
, uint8_t *data
)
856 CompatReleaseBufPriv
*priv
= opaque
;
857 if (priv
->avctx
.release_buffer
)
858 priv
->avctx
.release_buffer(&priv
->avctx
, &priv
->frame
);
862 static void compat_release_buffer(void *opaque
, uint8_t *data
)
864 AVBufferRef
*buf
= opaque
;
865 av_buffer_unref(&buf
);
867 FF_ENABLE_DEPRECATION_WARNINGS
870 int ff_decode_frame_props(AVCodecContext
*avctx
, AVFrame
*frame
)
872 return ff_init_buffer_info(avctx
, frame
);
875 static int get_buffer_internal(AVCodecContext
*avctx
, AVFrame
*frame
, int flags
)
877 const AVHWAccel
*hwaccel
= avctx
->hwaccel
;
878 int override_dimensions
= 1;
881 if (avctx
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
882 if ((ret
= av_image_check_size(avctx
->width
, avctx
->height
, 0, avctx
)) < 0 || avctx
->pix_fmt
<0) {
883 av_log(avctx
, AV_LOG_ERROR
, "video_get_buffer: image parameters invalid\n");
884 return AVERROR(EINVAL
);
887 if (avctx
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
888 if (frame
->width
<= 0 || frame
->height
<= 0) {
889 frame
->width
= FFMAX(avctx
->width
, FF_CEIL_RSHIFT(avctx
->coded_width
, avctx
->lowres
));
890 frame
->height
= FFMAX(avctx
->height
, FF_CEIL_RSHIFT(avctx
->coded_height
, avctx
->lowres
));
891 override_dimensions
= 0;
894 ret
= ff_decode_frame_props(avctx
, frame
);
897 if ((ret
= ff_init_buffer_info(avctx
, frame
)) < 0)
900 if (hwaccel
&& hwaccel
->alloc_frame
) {
901 ret
= hwaccel
->alloc_frame(avctx
, frame
);
905 #if FF_API_GET_BUFFER
906 FF_DISABLE_DEPRECATION_WARNINGS
908 * Wrap an old get_buffer()-allocated buffer in a bunch of AVBuffers.
909 * We wrap each plane in its own AVBuffer. Each of those has a reference to
910 * a dummy AVBuffer as its private data, unreffing it on free.
911 * When all the planes are freed, the dummy buffer's free callback calls
914 if (avctx
->get_buffer
) {
915 CompatReleaseBufPriv
*priv
= NULL
;
916 AVBufferRef
*dummy_buf
= NULL
;
919 if (flags
& AV_GET_BUFFER_FLAG_REF
)
920 frame
->reference
= 1;
922 ret
= avctx
->get_buffer(avctx
, frame
);
926 /* return if the buffers are already set up
927 * this would happen e.g. when a custom get_buffer() calls
928 * avcodec_default_get_buffer
933 priv
= av_mallocz(sizeof(*priv
));
935 ret
= AVERROR(ENOMEM
);
938 priv
->avctx
= *avctx
;
939 priv
->frame
= *frame
;
941 dummy_buf
= av_buffer_create(NULL
, 0, compat_free_buffer
, priv
, 0);
943 ret
= AVERROR(ENOMEM
);
947 #define WRAP_PLANE(ref_out, data, data_size) \
949 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
951 ret = AVERROR(ENOMEM); \
954 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
957 av_frame_unref(frame); \
958 ret = AVERROR(ENOMEM); \
963 if (avctx
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
964 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(frame
->format
);
966 planes
= av_pix_fmt_count_planes(frame
->format
);
967 /* workaround for AVHWAccel plane count of 0, buf[0] is used as
968 check for allocated buffers: make libavcodec happy */
969 if (desc
&& desc
->flags
& AV_PIX_FMT_FLAG_HWACCEL
)
971 if (!desc
|| planes
<= 0) {
972 ret
= AVERROR(EINVAL
);
976 for (i
= 0; i
< planes
; i
++) {
977 int v_shift
= (i
== 1 || i
== 2) ? desc
->log2_chroma_h
: 0;
978 int plane_size
= (frame
->height
>> v_shift
) * frame
->linesize
[i
];
980 WRAP_PLANE(frame
->buf
[i
], frame
->data
[i
], plane_size
);
983 int planar
= av_sample_fmt_is_planar(frame
->format
);
984 planes
= planar
? avctx
->channels
: 1;
986 if (planes
> FF_ARRAY_ELEMS(frame
->buf
)) {
987 frame
->nb_extended_buf
= planes
- FF_ARRAY_ELEMS(frame
->buf
);
988 frame
->extended_buf
= av_malloc_array(sizeof(*frame
->extended_buf
),
989 frame
->nb_extended_buf
);
990 if (!frame
->extended_buf
) {
991 ret
= AVERROR(ENOMEM
);
996 for (i
= 0; i
< FFMIN(planes
, FF_ARRAY_ELEMS(frame
->buf
)); i
++)
997 WRAP_PLANE(frame
->buf
[i
], frame
->extended_data
[i
], frame
->linesize
[0]);
999 for (i
= 0; i
< frame
->nb_extended_buf
; i
++)
1000 WRAP_PLANE(frame
->extended_buf
[i
],
1001 frame
->extended_data
[i
+ FF_ARRAY_ELEMS(frame
->buf
)],
1002 frame
->linesize
[0]);
1005 av_buffer_unref(&dummy_buf
);
1008 frame
->width
= avctx
->width
;
1009 frame
->height
= avctx
->height
;
1014 avctx
->release_buffer(avctx
, frame
);
1016 av_buffer_unref(&dummy_buf
);
1019 FF_ENABLE_DEPRECATION_WARNINGS
1022 ret
= avctx
->get_buffer2(avctx
, frame
, flags
);
1025 if (avctx
->codec_type
== AVMEDIA_TYPE_VIDEO
&& !override_dimensions
) {
1026 frame
->width
= avctx
->width
;
1027 frame
->height
= avctx
->height
;
1033 int ff_get_buffer(AVCodecContext
*avctx
, AVFrame
*frame
, int flags
)
1035 int ret
= get_buffer_internal(avctx
, frame
, flags
);
1037 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
1041 static int reget_buffer_internal(AVCodecContext
*avctx
, AVFrame
*frame
)
1046 av_assert0(avctx
->codec_type
== AVMEDIA_TYPE_VIDEO
);
1048 if (frame
->data
[0] && (frame
->width
!= avctx
->width
|| frame
->height
!= avctx
->height
|| frame
->format
!= avctx
->pix_fmt
)) {
1049 av_log(avctx
, AV_LOG_WARNING
, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
1050 frame
->width
, frame
->height
, av_get_pix_fmt_name(frame
->format
), avctx
->width
, avctx
->height
, av_get_pix_fmt_name(avctx
->pix_fmt
));
1051 av_frame_unref(frame
);
1054 ff_init_buffer_info(avctx
, frame
);
1056 if (!frame
->data
[0])
1057 return ff_get_buffer(avctx
, frame
, AV_GET_BUFFER_FLAG_REF
);
1059 if (av_frame_is_writable(frame
))
1060 return ff_decode_frame_props(avctx
, frame
);
1062 tmp
= av_frame_alloc();
1064 return AVERROR(ENOMEM
);
1066 av_frame_move_ref(tmp
, frame
);
1068 ret
= ff_get_buffer(avctx
, frame
, AV_GET_BUFFER_FLAG_REF
);
1070 av_frame_free(&tmp
);
1074 av_frame_copy(frame
, tmp
);
1075 av_frame_free(&tmp
);
1080 int ff_reget_buffer(AVCodecContext
*avctx
, AVFrame
*frame
)
1082 int ret
= reget_buffer_internal(avctx
, frame
);
1084 av_log(avctx
, AV_LOG_ERROR
, "reget_buffer() failed\n");
1088 #if FF_API_GET_BUFFER
1089 void avcodec_default_release_buffer(AVCodecContext
*s
, AVFrame
*pic
)
1091 av_assert0(s
->codec_type
== AVMEDIA_TYPE_VIDEO
);
1093 av_frame_unref(pic
);
1096 int avcodec_default_reget_buffer(AVCodecContext
*s
, AVFrame
*pic
)
1103 int avcodec_default_execute(AVCodecContext
*c
, int (*func
)(AVCodecContext
*c2
, void *arg2
), void *arg
, int *ret
, int count
, int size
)
1107 for (i
= 0; i
< count
; i
++) {
1108 int r
= func(c
, (char *)arg
+ i
* size
);
1115 int avcodec_default_execute2(AVCodecContext
*c
, int (*func
)(AVCodecContext
*c2
, void *arg2
, int jobnr
, int threadnr
), void *arg
, int *ret
, int count
)
1119 for (i
= 0; i
< count
; i
++) {
1120 int r
= func(c
, arg
, i
, 0);
1127 enum AVPixelFormat
avpriv_find_pix_fmt(const PixelFormatTag
*tags
,
1128 unsigned int fourcc
)
1130 while (tags
->pix_fmt
>= 0) {
1131 if (tags
->fourcc
== fourcc
)
1132 return tags
->pix_fmt
;
1135 return AV_PIX_FMT_NONE
;
1138 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt
)
1140 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(pix_fmt
);
1141 return desc
->flags
& AV_PIX_FMT_FLAG_HWACCEL
;
1144 enum AVPixelFormat
avcodec_default_get_format(struct AVCodecContext
*s
, const enum AVPixelFormat
*fmt
)
1146 while (*fmt
!= AV_PIX_FMT_NONE
&& is_hwaccel_pix_fmt(*fmt
))
1151 static AVHWAccel
*find_hwaccel(enum AVCodecID codec_id
,
1152 enum AVPixelFormat pix_fmt
)
1154 AVHWAccel
*hwaccel
= NULL
;
1156 while ((hwaccel
= av_hwaccel_next(hwaccel
)))
1157 if (hwaccel
->id
== codec_id
1158 && hwaccel
->pix_fmt
== pix_fmt
)
1164 int ff_get_format(AVCodecContext
*avctx
, const enum AVPixelFormat
*fmt
)
1166 const AVPixFmtDescriptor
*desc
;
1167 enum AVPixelFormat ret
= avctx
->get_format(avctx
, fmt
);
1169 desc
= av_pix_fmt_desc_get(ret
);
1171 return AV_PIX_FMT_NONE
;
1173 if (avctx
->hwaccel
&& avctx
->hwaccel
->uninit
)
1174 avctx
->hwaccel
->uninit(avctx
);
1175 av_freep(&avctx
->internal
->hwaccel_priv_data
);
1176 avctx
->hwaccel
= NULL
;
1178 if (desc
->flags
& AV_PIX_FMT_FLAG_HWACCEL
&&
1179 !(avctx
->codec
->capabilities
&CODEC_CAP_HWACCEL_VDPAU
)) {
1183 hwaccel
= find_hwaccel(avctx
->codec_id
, ret
);
1185 av_log(avctx
, AV_LOG_ERROR
,
1186 "Could not find an AVHWAccel for the pixel format: %s",
1188 return AV_PIX_FMT_NONE
;
1191 if (hwaccel
->priv_data_size
) {
1192 avctx
->internal
->hwaccel_priv_data
= av_mallocz(hwaccel
->priv_data_size
);
1193 if (!avctx
->internal
->hwaccel_priv_data
)
1194 return AV_PIX_FMT_NONE
;
1197 if (hwaccel
->init
) {
1198 err
= hwaccel
->init(avctx
);
1200 av_freep(&avctx
->internal
->hwaccel_priv_data
);
1201 return AV_PIX_FMT_NONE
;
1204 avctx
->hwaccel
= hwaccel
;
1210 #if FF_API_AVFRAME_LAVC
1211 void avcodec_get_frame_defaults(AVFrame
*frame
)
1213 #if LIBAVCODEC_VERSION_MAJOR >= 55
1214 // extended_data should explicitly be freed when needed, this code is unsafe currently
1215 // also this is not compatible to the <55 ABI/API
1216 if (frame
->extended_data
!= frame
->data
&& 0)
1217 av_freep(&frame
->extended_data
);
1220 memset(frame
, 0, sizeof(AVFrame
));
1221 av_frame_unref(frame
);
1224 AVFrame
*avcodec_alloc_frame(void)
1226 return av_frame_alloc();
1229 void avcodec_free_frame(AVFrame
**frame
)
1231 av_frame_free(frame
);
1235 MAKE_ACCESSORS(AVCodecContext
, codec
, AVRational
, pkt_timebase
)
1236 MAKE_ACCESSORS(AVCodecContext
, codec
, const AVCodecDescriptor
*, codec_descriptor
)
1237 MAKE_ACCESSORS(AVCodecContext
, codec
, int, lowres
)
1238 MAKE_ACCESSORS(AVCodecContext
, codec
, int, seek_preroll
)
1239 MAKE_ACCESSORS(AVCodecContext
, codec
, uint16_t*, chroma_intra_matrix
)
1241 int av_codec_get_max_lowres(const AVCodec
*codec
)
1243 return codec
->max_lowres
;
1246 static void avcodec_get_subtitle_defaults(AVSubtitle
*sub
)
1248 memset(sub
, 0, sizeof(*sub
));
1249 sub
->pts
= AV_NOPTS_VALUE
;
1252 static int get_bit_rate(AVCodecContext
*ctx
)
1255 int bits_per_sample
;
1257 switch (ctx
->codec_type
) {
1258 case AVMEDIA_TYPE_VIDEO
:
1259 case AVMEDIA_TYPE_DATA
:
1260 case AVMEDIA_TYPE_SUBTITLE
:
1261 case AVMEDIA_TYPE_ATTACHMENT
:
1262 bit_rate
= ctx
->bit_rate
;
1264 case AVMEDIA_TYPE_AUDIO
:
1265 bits_per_sample
= av_get_bits_per_sample(ctx
->codec_id
);
1266 bit_rate
= bits_per_sample
? ctx
->sample_rate
* ctx
->channels
* bits_per_sample
: ctx
->bit_rate
;
1275 int attribute_align_arg
ff_codec_open2_recursive(AVCodecContext
*avctx
, const AVCodec
*codec
, AVDictionary
**options
)
1279 ff_unlock_avcodec();
1281 ret
= avcodec_open2(avctx
, codec
, options
);
1283 ff_lock_avcodec(avctx
);
1287 int attribute_align_arg
avcodec_open2(AVCodecContext
*avctx
, const AVCodec
*codec
, AVDictionary
**options
)
1290 AVDictionary
*tmp
= NULL
;
1292 if (avcodec_is_open(avctx
))
1295 if ((!codec
&& !avctx
->codec
)) {
1296 av_log(avctx
, AV_LOG_ERROR
, "No codec provided to avcodec_open2()\n");
1297 return AVERROR(EINVAL
);
1299 if ((codec
&& avctx
->codec
&& codec
!= avctx
->codec
)) {
1300 av_log(avctx
, AV_LOG_ERROR
, "This AVCodecContext was allocated for %s, "
1301 "but %s passed to avcodec_open2()\n", avctx
->codec
->name
, codec
->name
);
1302 return AVERROR(EINVAL
);
1305 codec
= avctx
->codec
;
1307 if (avctx
->extradata_size
< 0 || avctx
->extradata_size
>= FF_MAX_EXTRADATA_SIZE
)
1308 return AVERROR(EINVAL
);
1311 av_dict_copy(&tmp
, *options
, 0);
1313 ret
= ff_lock_avcodec(avctx
);
1317 avctx
->internal
= av_mallocz(sizeof(AVCodecInternal
));
1318 if (!avctx
->internal
) {
1319 ret
= AVERROR(ENOMEM
);
1323 avctx
->internal
->pool
= av_mallocz(sizeof(*avctx
->internal
->pool
));
1324 if (!avctx
->internal
->pool
) {
1325 ret
= AVERROR(ENOMEM
);
1329 avctx
->internal
->to_free
= av_frame_alloc();
1330 if (!avctx
->internal
->to_free
) {
1331 ret
= AVERROR(ENOMEM
);
1335 if (codec
->priv_data_size
> 0) {
1336 if (!avctx
->priv_data
) {
1337 avctx
->priv_data
= av_mallocz(codec
->priv_data_size
);
1338 if (!avctx
->priv_data
) {
1339 ret
= AVERROR(ENOMEM
);
1342 if (codec
->priv_class
) {
1343 *(const AVClass
**)avctx
->priv_data
= codec
->priv_class
;
1344 av_opt_set_defaults(avctx
->priv_data
);
1347 if (codec
->priv_class
&& (ret
= av_opt_set_dict(avctx
->priv_data
, &tmp
)) < 0)
1350 avctx
->priv_data
= NULL
;
1352 if ((ret
= av_opt_set_dict(avctx
, &tmp
)) < 0)
1355 // only call ff_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
1356 if (!(avctx
->coded_width
&& avctx
->coded_height
&& avctx
->width
&& avctx
->height
&&
1357 (avctx
->codec_id
== AV_CODEC_ID_H264
|| avctx
->codec_id
== AV_CODEC_ID_VP6F
))) {
1358 if (avctx
->coded_width
&& avctx
->coded_height
)
1359 ret
= ff_set_dimensions(avctx
, avctx
->coded_width
, avctx
->coded_height
);
1360 else if (avctx
->width
&& avctx
->height
)
1361 ret
= ff_set_dimensions(avctx
, avctx
->width
, avctx
->height
);
1366 if ((avctx
->coded_width
|| avctx
->coded_height
|| avctx
->width
|| avctx
->height
)
1367 && ( av_image_check_size(avctx
->coded_width
, avctx
->coded_height
, 0, avctx
) < 0
1368 || av_image_check_size(avctx
->width
, avctx
->height
, 0, avctx
) < 0)) {
1369 av_log(avctx
, AV_LOG_WARNING
, "Ignoring invalid width/height values\n");
1370 ff_set_dimensions(avctx
, 0, 0);
1373 if (avctx
->width
> 0 && avctx
->height
> 0) {
1374 if (av_image_check_sar(avctx
->width
, avctx
->height
,
1375 avctx
->sample_aspect_ratio
) < 0) {
1376 av_log(avctx
, AV_LOG_WARNING
, "ignoring invalid SAR: %u/%u\n",
1377 avctx
->sample_aspect_ratio
.num
,
1378 avctx
->sample_aspect_ratio
.den
);
1379 avctx
->sample_aspect_ratio
= (AVRational
){ 0, 1 };
1383 /* if the decoder init function was already called previously,
1384 * free the already allocated subtitle_header before overwriting it */
1385 if (av_codec_is_decoder(codec
))
1386 av_freep(&avctx
->subtitle_header
);
1388 if (avctx
->channels
> FF_SANE_NB_CHANNELS
) {
1389 ret
= AVERROR(EINVAL
);
1393 avctx
->codec
= codec
;
1394 if ((avctx
->codec_type
== AVMEDIA_TYPE_UNKNOWN
|| avctx
->codec_type
== codec
->type
) &&
1395 avctx
->codec_id
== AV_CODEC_ID_NONE
) {
1396 avctx
->codec_type
= codec
->type
;
1397 avctx
->codec_id
= codec
->id
;
1399 if (avctx
->codec_id
!= codec
->id
|| (avctx
->codec_type
!= codec
->type
1400 && avctx
->codec_type
!= AVMEDIA_TYPE_ATTACHMENT
)) {
1401 av_log(avctx
, AV_LOG_ERROR
, "Codec type or id mismatches\n");
1402 ret
= AVERROR(EINVAL
);
1405 avctx
->frame_number
= 0;
1406 avctx
->codec_descriptor
= avcodec_descriptor_get(avctx
->codec_id
);
1408 if (avctx
->codec
->capabilities
& CODEC_CAP_EXPERIMENTAL
&&
1409 avctx
->strict_std_compliance
> FF_COMPLIANCE_EXPERIMENTAL
) {
1410 const char *codec_string
= av_codec_is_encoder(codec
) ? "encoder" : "decoder";
1412 av_log(avctx
, AV_LOG_ERROR
,
1413 "The %s '%s' is experimental but experimental codecs are not enabled, "
1414 "add '-strict %d' if you want to use it.\n",
1415 codec_string
, codec
->name
, FF_COMPLIANCE_EXPERIMENTAL
);
1416 codec2
= av_codec_is_encoder(codec
) ? avcodec_find_encoder(codec
->id
) : avcodec_find_decoder(codec
->id
);
1417 if (!(codec2
->capabilities
& CODEC_CAP_EXPERIMENTAL
))
1418 av_log(avctx
, AV_LOG_ERROR
, "Alternatively use the non experimental %s '%s'.\n",
1419 codec_string
, codec2
->name
);
1420 ret
= AVERROR_EXPERIMENTAL
;
1424 if (avctx
->codec_type
== AVMEDIA_TYPE_AUDIO
&&
1425 (!avctx
->time_base
.num
|| !avctx
->time_base
.den
)) {
1426 avctx
->time_base
.num
= 1;
1427 avctx
->time_base
.den
= avctx
->sample_rate
;
1431 av_log(avctx
, AV_LOG_WARNING
, "Warning: not compiled with thread support, using thread emulation\n");
1433 if (CONFIG_FRAME_THREAD_ENCODER
) {
1434 ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
1435 ret
= ff_frame_thread_encoder_init(avctx
, options
? *options
: NULL
);
1436 ff_lock_avcodec(avctx
);
1442 && !(avctx
->internal
->frame_thread_encoder
&& (avctx
->active_thread_type
&FF_THREAD_FRAME
))) {
1443 ret
= ff_thread_init(avctx
);
1448 if (!HAVE_THREADS
&& !(codec
->capabilities
& CODEC_CAP_AUTO_THREADS
))
1449 avctx
->thread_count
= 1;
1451 if (avctx
->codec
->max_lowres
< avctx
->lowres
|| avctx
->lowres
< 0) {
1452 av_log(avctx
, AV_LOG_ERROR
, "The maximum value for lowres supported by the decoder is %d\n",
1453 avctx
->codec
->max_lowres
);
1454 ret
= AVERROR(EINVAL
);
1459 if (avctx
->debug_mv
)
1460 av_log(avctx
, AV_LOG_WARNING
, "The 'vismv' option is deprecated, "
1461 "see the codecview filter instead.\n");
1464 if (av_codec_is_encoder(avctx
->codec
)) {
1466 if (avctx
->codec
->sample_fmts
) {
1467 for (i
= 0; avctx
->codec
->sample_fmts
[i
] != AV_SAMPLE_FMT_NONE
; i
++) {
1468 if (avctx
->sample_fmt
== avctx
->codec
->sample_fmts
[i
])
1470 if (avctx
->channels
== 1 &&
1471 av_get_planar_sample_fmt(avctx
->sample_fmt
) ==
1472 av_get_planar_sample_fmt(avctx
->codec
->sample_fmts
[i
])) {
1473 avctx
->sample_fmt
= avctx
->codec
->sample_fmts
[i
];
1477 if (avctx
->codec
->sample_fmts
[i
] == AV_SAMPLE_FMT_NONE
) {
1479 snprintf(buf
, sizeof(buf
), "%d", avctx
->sample_fmt
);
1480 av_log(avctx
, AV_LOG_ERROR
, "Specified sample format %s is invalid or not supported\n",
1481 (char *)av_x_if_null(av_get_sample_fmt_name(avctx
->sample_fmt
), buf
));
1482 ret
= AVERROR(EINVAL
);
1486 if (avctx
->codec
->pix_fmts
) {
1487 for (i
= 0; avctx
->codec
->pix_fmts
[i
] != AV_PIX_FMT_NONE
; i
++)
1488 if (avctx
->pix_fmt
== avctx
->codec
->pix_fmts
[i
])
1490 if (avctx
->codec
->pix_fmts
[i
] == AV_PIX_FMT_NONE
1491 && !((avctx
->codec_id
== AV_CODEC_ID_MJPEG
|| avctx
->codec_id
== AV_CODEC_ID_LJPEG
)
1492 && avctx
->strict_std_compliance
<= FF_COMPLIANCE_UNOFFICIAL
)) {
1494 snprintf(buf
, sizeof(buf
), "%d", avctx
->pix_fmt
);
1495 av_log(avctx
, AV_LOG_ERROR
, "Specified pixel format %s is invalid or not supported\n",
1496 (char *)av_x_if_null(av_get_pix_fmt_name(avctx
->pix_fmt
), buf
));
1497 ret
= AVERROR(EINVAL
);
1501 if (avctx
->codec
->supported_samplerates
) {
1502 for (i
= 0; avctx
->codec
->supported_samplerates
[i
] != 0; i
++)
1503 if (avctx
->sample_rate
== avctx
->codec
->supported_samplerates
[i
])
1505 if (avctx
->codec
->supported_samplerates
[i
] == 0) {
1506 av_log(avctx
, AV_LOG_ERROR
, "Specified sample rate %d is not supported\n",
1507 avctx
->sample_rate
);
1508 ret
= AVERROR(EINVAL
);
1512 if (avctx
->codec
->channel_layouts
) {
1513 if (!avctx
->channel_layout
) {
1514 av_log(avctx
, AV_LOG_WARNING
, "Channel layout not specified\n");
1516 for (i
= 0; avctx
->codec
->channel_layouts
[i
] != 0; i
++)
1517 if (avctx
->channel_layout
== avctx
->codec
->channel_layouts
[i
])
1519 if (avctx
->codec
->channel_layouts
[i
] == 0) {
1521 av_get_channel_layout_string(buf
, sizeof(buf
), -1, avctx
->channel_layout
);
1522 av_log(avctx
, AV_LOG_ERROR
, "Specified channel layout '%s' is not supported\n", buf
);
1523 ret
= AVERROR(EINVAL
);
1528 if (avctx
->channel_layout
&& avctx
->channels
) {
1529 int channels
= av_get_channel_layout_nb_channels(avctx
->channel_layout
);
1530 if (channels
!= avctx
->channels
) {
1532 av_get_channel_layout_string(buf
, sizeof(buf
), -1, avctx
->channel_layout
);
1533 av_log(avctx
, AV_LOG_ERROR
,
1534 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1535 buf
, channels
, avctx
->channels
);
1536 ret
= AVERROR(EINVAL
);
1539 } else if (avctx
->channel_layout
) {
1540 avctx
->channels
= av_get_channel_layout_nb_channels(avctx
->channel_layout
);
1542 if(avctx
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
1543 if (avctx
->width
<= 0 || avctx
->height
<= 0) {
1544 av_log(avctx
, AV_LOG_ERROR
, "dimensions not set\n");
1545 ret
= AVERROR(EINVAL
);
1549 if ( (avctx
->codec_type
== AVMEDIA_TYPE_VIDEO
|| avctx
->codec_type
== AVMEDIA_TYPE_AUDIO
)
1550 && avctx
->bit_rate
>0 && avctx
->bit_rate
<1000) {
1551 av_log(avctx
, AV_LOG_WARNING
, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx
->bit_rate
, avctx
->bit_rate
);
1554 if (!avctx
->rc_initial_buffer_occupancy
)
1555 avctx
->rc_initial_buffer_occupancy
= avctx
->rc_buffer_size
* 3 / 4;
1558 avctx
->pts_correction_num_faulty_pts
=
1559 avctx
->pts_correction_num_faulty_dts
= 0;
1560 avctx
->pts_correction_last_pts
=
1561 avctx
->pts_correction_last_dts
= INT64_MIN
;
1563 if ( avctx
->codec
->init
&& (!(avctx
->active_thread_type
&FF_THREAD_FRAME
)
1564 || avctx
->internal
->frame_thread_encoder
)) {
1565 ret
= avctx
->codec
->init(avctx
);
1573 if (av_codec_is_decoder(avctx
->codec
)) {
1574 if (!avctx
->bit_rate
)
1575 avctx
->bit_rate
= get_bit_rate(avctx
);
1576 /* validate channel layout from the decoder */
1577 if (avctx
->channel_layout
) {
1578 int channels
= av_get_channel_layout_nb_channels(avctx
->channel_layout
);
1579 if (!avctx
->channels
)
1580 avctx
->channels
= channels
;
1581 else if (channels
!= avctx
->channels
) {
1583 av_get_channel_layout_string(buf
, sizeof(buf
), -1, avctx
->channel_layout
);
1584 av_log(avctx
, AV_LOG_WARNING
,
1585 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1586 "ignoring specified channel layout\n",
1587 buf
, channels
, avctx
->channels
);
1588 avctx
->channel_layout
= 0;
1591 if (avctx
->channels
&& avctx
->channels
< 0 ||
1592 avctx
->channels
> FF_SANE_NB_CHANNELS
) {
1593 ret
= AVERROR(EINVAL
);
1596 if (avctx
->sub_charenc
) {
1597 if (avctx
->codec_type
!= AVMEDIA_TYPE_SUBTITLE
) {
1598 av_log(avctx
, AV_LOG_ERROR
, "Character encoding is only "
1599 "supported with subtitles codecs\n");
1600 ret
= AVERROR(EINVAL
);
1602 } else if (avctx
->codec_descriptor
->props
& AV_CODEC_PROP_BITMAP_SUB
) {
1603 av_log(avctx
, AV_LOG_WARNING
, "Codec '%s' is bitmap-based, "
1604 "subtitles character encoding will be ignored\n",
1605 avctx
->codec_descriptor
->name
);
1606 avctx
->sub_charenc_mode
= FF_SUB_CHARENC_MODE_DO_NOTHING
;
1608 /* input character encoding is set for a text based subtitle
1609 * codec at this point */
1610 if (avctx
->sub_charenc_mode
== FF_SUB_CHARENC_MODE_AUTOMATIC
)
1611 avctx
->sub_charenc_mode
= FF_SUB_CHARENC_MODE_PRE_DECODER
;
1613 if (avctx
->sub_charenc_mode
== FF_SUB_CHARENC_MODE_PRE_DECODER
) {
1615 iconv_t cd
= iconv_open("UTF-8", avctx
->sub_charenc
);
1616 if (cd
== (iconv_t
)-1) {
1617 av_log(avctx
, AV_LOG_ERROR
, "Unable to open iconv context "
1618 "with input character encoding \"%s\"\n", avctx
->sub_charenc
);
1619 ret
= AVERROR(errno
);
1624 av_log(avctx
, AV_LOG_ERROR
, "Character encoding subtitles "
1625 "conversion needs a libavcodec built with iconv support "
1626 "for this codec\n");
1627 ret
= AVERROR(ENOSYS
);
1635 ff_unlock_avcodec();
1637 av_dict_free(options
);
1644 av_freep(&avctx
->priv_data
);
1645 if (avctx
->internal
) {
1646 av_frame_free(&avctx
->internal
->to_free
);
1647 av_freep(&avctx
->internal
->pool
);
1649 av_freep(&avctx
->internal
);
1650 avctx
->codec
= NULL
;
1654 int ff_alloc_packet2(AVCodecContext
*avctx
, AVPacket
*avpkt
, int64_t size
)
1656 if (avpkt
->size
< 0) {
1657 av_log(avctx
, AV_LOG_ERROR
, "Invalid negative user packet size %d\n", avpkt
->size
);
1658 return AVERROR(EINVAL
);
1660 if (size
< 0 || size
> INT_MAX
- FF_INPUT_BUFFER_PADDING_SIZE
) {
1661 av_log(avctx
, AV_LOG_ERROR
, "Invalid minimum required packet size %"PRId64
" (max allowed is %d)\n",
1662 size
, INT_MAX
- FF_INPUT_BUFFER_PADDING_SIZE
);
1663 return AVERROR(EINVAL
);
1667 av_assert0(!avpkt
->data
|| avpkt
->data
!= avctx
->internal
->byte_buffer
);
1668 if (!avpkt
->data
|| avpkt
->size
< size
) {
1669 av_fast_padded_malloc(&avctx
->internal
->byte_buffer
, &avctx
->internal
->byte_buffer_size
, size
);
1670 avpkt
->data
= avctx
->internal
->byte_buffer
;
1671 avpkt
->size
= avctx
->internal
->byte_buffer_size
;
1672 #if FF_API_DESTRUCT_PACKET
1673 FF_DISABLE_DEPRECATION_WARNINGS
1674 avpkt
->destruct
= NULL
;
1675 FF_ENABLE_DEPRECATION_WARNINGS
1681 AVBufferRef
*buf
= avpkt
->buf
;
1682 #if FF_API_DESTRUCT_PACKET
1683 FF_DISABLE_DEPRECATION_WARNINGS
1684 void *destruct
= avpkt
->destruct
;
1685 FF_ENABLE_DEPRECATION_WARNINGS
1688 if (avpkt
->size
< size
) {
1689 av_log(avctx
, AV_LOG_ERROR
, "User packet is too small (%d < %"PRId64
")\n", avpkt
->size
, size
);
1690 return AVERROR(EINVAL
);
1693 av_init_packet(avpkt
);
1694 #if FF_API_DESTRUCT_PACKET
1695 FF_DISABLE_DEPRECATION_WARNINGS
1696 avpkt
->destruct
= destruct
;
1697 FF_ENABLE_DEPRECATION_WARNINGS
1703 int ret
= av_new_packet(avpkt
, size
);
1705 av_log(avctx
, AV_LOG_ERROR
, "Failed to allocate packet of size %"PRId64
"\n", size
);
1710 int ff_alloc_packet(AVPacket
*avpkt
, int size
)
1712 return ff_alloc_packet2(NULL
, avpkt
, size
);
1716 * Pad last frame with silence.
1718 static int pad_last_frame(AVCodecContext
*s
, AVFrame
**dst
, const AVFrame
*src
)
1720 AVFrame
*frame
= NULL
;
1723 if (!(frame
= av_frame_alloc()))
1724 return AVERROR(ENOMEM
);
1726 frame
->format
= src
->format
;
1727 frame
->channel_layout
= src
->channel_layout
;
1728 av_frame_set_channels(frame
, av_frame_get_channels(src
));
1729 frame
->nb_samples
= s
->frame_size
;
1730 ret
= av_frame_get_buffer(frame
, 32);
1734 ret
= av_frame_copy_props(frame
, src
);
1738 if ((ret
= av_samples_copy(frame
->extended_data
, src
->extended_data
, 0, 0,
1739 src
->nb_samples
, s
->channels
, s
->sample_fmt
)) < 0)
1741 if ((ret
= av_samples_set_silence(frame
->extended_data
, src
->nb_samples
,
1742 frame
->nb_samples
- src
->nb_samples
,
1743 s
->channels
, s
->sample_fmt
)) < 0)
1751 av_frame_free(&frame
);
1755 int attribute_align_arg
avcodec_encode_audio2(AVCodecContext
*avctx
,
1757 const AVFrame
*frame
,
1758 int *got_packet_ptr
)
1760 AVFrame
*extended_frame
= NULL
;
1761 AVFrame
*padded_frame
= NULL
;
1763 AVPacket user_pkt
= *avpkt
;
1764 int needs_realloc
= !user_pkt
.data
;
1766 *got_packet_ptr
= 0;
1768 if (!(avctx
->codec
->capabilities
& CODEC_CAP_DELAY
) && !frame
) {
1769 av_free_packet(avpkt
);
1770 av_init_packet(avpkt
);
1774 /* ensure that extended_data is properly set */
1775 if (frame
&& !frame
->extended_data
) {
1776 if (av_sample_fmt_is_planar(avctx
->sample_fmt
) &&
1777 avctx
->channels
> AV_NUM_DATA_POINTERS
) {
1778 av_log(avctx
, AV_LOG_ERROR
, "Encoding to a planar sample format, "
1779 "with more than %d channels, but extended_data is not set.\n",
1780 AV_NUM_DATA_POINTERS
);
1781 return AVERROR(EINVAL
);
1783 av_log(avctx
, AV_LOG_WARNING
, "extended_data is not set.\n");
1785 extended_frame
= av_frame_alloc();
1786 if (!extended_frame
)
1787 return AVERROR(ENOMEM
);
1789 memcpy(extended_frame
, frame
, sizeof(AVFrame
));
1790 extended_frame
->extended_data
= extended_frame
->data
;
1791 frame
= extended_frame
;
1794 /* check for valid frame size */
1796 if (avctx
->codec
->capabilities
& CODEC_CAP_SMALL_LAST_FRAME
) {
1797 if (frame
->nb_samples
> avctx
->frame_size
) {
1798 av_log(avctx
, AV_LOG_ERROR
, "more samples than frame size (avcodec_encode_audio2)\n");
1799 ret
= AVERROR(EINVAL
);
1802 } else if (!(avctx
->codec
->capabilities
& CODEC_CAP_VARIABLE_FRAME_SIZE
)) {
1803 if (frame
->nb_samples
< avctx
->frame_size
&&
1804 !avctx
->internal
->last_audio_frame
) {
1805 ret
= pad_last_frame(avctx
, &padded_frame
, frame
);
1809 frame
= padded_frame
;
1810 avctx
->internal
->last_audio_frame
= 1;
1813 if (frame
->nb_samples
!= avctx
->frame_size
) {
1814 av_log(avctx
, AV_LOG_ERROR
, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame
->nb_samples
, avctx
->frame_size
);
1815 ret
= AVERROR(EINVAL
);
1821 ret
= avctx
->codec
->encode2(avctx
, avpkt
, frame
, got_packet_ptr
);
1823 if (*got_packet_ptr
) {
1824 if (!(avctx
->codec
->capabilities
& CODEC_CAP_DELAY
)) {
1825 if (avpkt
->pts
== AV_NOPTS_VALUE
)
1826 avpkt
->pts
= frame
->pts
;
1827 if (!avpkt
->duration
)
1828 avpkt
->duration
= ff_samples_to_time_base(avctx
,
1831 avpkt
->dts
= avpkt
->pts
;
1836 if (avpkt
->data
&& avpkt
->data
== avctx
->internal
->byte_buffer
) {
1838 if (user_pkt
.data
) {
1839 if (user_pkt
.size
>= avpkt
->size
) {
1840 memcpy(user_pkt
.data
, avpkt
->data
, avpkt
->size
);
1842 av_log(avctx
, AV_LOG_ERROR
, "Provided packet is too small, needs to be %d\n", avpkt
->size
);
1843 avpkt
->size
= user_pkt
.size
;
1846 avpkt
->buf
= user_pkt
.buf
;
1847 avpkt
->data
= user_pkt
.data
;
1848 #if FF_API_DESTRUCT_PACKET
1849 FF_DISABLE_DEPRECATION_WARNINGS
1850 avpkt
->destruct
= user_pkt
.destruct
;
1851 FF_ENABLE_DEPRECATION_WARNINGS
1854 if (av_dup_packet(avpkt
) < 0) {
1855 ret
= AVERROR(ENOMEM
);
1861 if (needs_realloc
&& avpkt
->data
) {
1862 ret
= av_buffer_realloc(&avpkt
->buf
, avpkt
->size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
1864 avpkt
->data
= avpkt
->buf
->data
;
1867 avctx
->frame_number
++;
1870 if (ret
< 0 || !*got_packet_ptr
) {
1871 av_free_packet(avpkt
);
1872 av_init_packet(avpkt
);
1876 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1877 * this needs to be moved to the encoders, but for now we can do it
1878 * here to simplify things */
1879 avpkt
->flags
|= AV_PKT_FLAG_KEY
;
1882 av_frame_free(&padded_frame
);
1883 av_free(extended_frame
);
1888 #if FF_API_OLD_ENCODE_AUDIO
1889 int attribute_align_arg
avcodec_encode_audio(AVCodecContext
*avctx
,
1890 uint8_t *buf
, int buf_size
,
1891 const short *samples
)
1895 int ret
, samples_size
, got_packet
;
1897 av_init_packet(&pkt
);
1899 pkt
.size
= buf_size
;
1902 frame
= av_frame_alloc();
1904 return AVERROR(ENOMEM
);
1906 if (avctx
->frame_size
) {
1907 frame
->nb_samples
= avctx
->frame_size
;
1909 /* if frame_size is not set, the number of samples must be
1910 * calculated from the buffer size */
1912 if (!av_get_bits_per_sample(avctx
->codec_id
)) {
1913 av_log(avctx
, AV_LOG_ERROR
, "avcodec_encode_audio() does not "
1914 "support this codec\n");
1915 av_frame_free(&frame
);
1916 return AVERROR(EINVAL
);
1918 nb_samples
= (int64_t)buf_size
* 8 /
1919 (av_get_bits_per_sample(avctx
->codec_id
) *
1921 if (nb_samples
>= INT_MAX
) {
1922 av_frame_free(&frame
);
1923 return AVERROR(EINVAL
);
1925 frame
->nb_samples
= nb_samples
;
1928 /* it is assumed that the samples buffer is large enough based on the
1929 * relevant parameters */
1930 samples_size
= av_samples_get_buffer_size(NULL
, avctx
->channels
,
1932 avctx
->sample_fmt
, 1);
1933 if ((ret
= avcodec_fill_audio_frame(frame
, avctx
->channels
,
1935 (const uint8_t *)samples
,
1936 samples_size
, 1)) < 0) {
1937 av_frame_free(&frame
);
1941 /* fabricate frame pts from sample count.
1942 * this is needed because the avcodec_encode_audio() API does not have
1943 * a way for the user to provide pts */
1944 if (avctx
->sample_rate
&& avctx
->time_base
.num
)
1945 frame
->pts
= ff_samples_to_time_base(avctx
,
1946 avctx
->internal
->sample_count
);
1948 frame
->pts
= AV_NOPTS_VALUE
;
1949 avctx
->internal
->sample_count
+= frame
->nb_samples
;
1955 ret
= avcodec_encode_audio2(avctx
, &pkt
, frame
, &got_packet
);
1956 if (!ret
&& got_packet
&& avctx
->coded_frame
) {
1957 avctx
->coded_frame
->pts
= pkt
.pts
;
1958 avctx
->coded_frame
->key_frame
= !!(pkt
.flags
& AV_PKT_FLAG_KEY
);
1960 /* free any side data since we cannot return it */
1961 av_packet_free_side_data(&pkt
);
1963 if (frame
&& frame
->extended_data
!= frame
->data
)
1964 av_freep(&frame
->extended_data
);
1966 av_frame_free(&frame
);
1967 return ret
? ret
: pkt
.size
;
1972 #if FF_API_OLD_ENCODE_VIDEO
1973 int attribute_align_arg
avcodec_encode_video(AVCodecContext
*avctx
, uint8_t *buf
, int buf_size
,
1974 const AVFrame
*pict
)
1977 int ret
, got_packet
= 0;
1979 if (buf_size
< FF_MIN_BUFFER_SIZE
) {
1980 av_log(avctx
, AV_LOG_ERROR
, "buffer smaller than minimum size\n");
1984 av_init_packet(&pkt
);
1986 pkt
.size
= buf_size
;
1988 ret
= avcodec_encode_video2(avctx
, &pkt
, pict
, &got_packet
);
1989 if (!ret
&& got_packet
&& avctx
->coded_frame
) {
1990 avctx
->coded_frame
->pts
= pkt
.pts
;
1991 avctx
->coded_frame
->key_frame
= !!(pkt
.flags
& AV_PKT_FLAG_KEY
);
1994 /* free any side data since we cannot return it */
1995 if (pkt
.side_data_elems
> 0) {
1997 for (i
= 0; i
< pkt
.side_data_elems
; i
++)
1998 av_free(pkt
.side_data
[i
].data
);
1999 av_freep(&pkt
.side_data
);
2000 pkt
.side_data_elems
= 0;
2003 return ret
? ret
: pkt
.size
;
2008 int attribute_align_arg
avcodec_encode_video2(AVCodecContext
*avctx
,
2010 const AVFrame
*frame
,
2011 int *got_packet_ptr
)
2014 AVPacket user_pkt
= *avpkt
;
2015 int needs_realloc
= !user_pkt
.data
;
2017 *got_packet_ptr
= 0;
2019 if(CONFIG_FRAME_THREAD_ENCODER
&&
2020 avctx
->internal
->frame_thread_encoder
&& (avctx
->active_thread_type
&FF_THREAD_FRAME
))
2021 return ff_thread_video_encode_frame(avctx
, avpkt
, frame
, got_packet_ptr
);
2023 if ((avctx
->flags
&CODEC_FLAG_PASS1
) && avctx
->stats_out
)
2024 avctx
->stats_out
[0] = '\0';
2026 if (!(avctx
->codec
->capabilities
& CODEC_CAP_DELAY
) && !frame
) {
2027 av_free_packet(avpkt
);
2028 av_init_packet(avpkt
);
2033 if (av_image_check_size(avctx
->width
, avctx
->height
, 0, avctx
))
2034 return AVERROR(EINVAL
);
2036 av_assert0(avctx
->codec
->encode2
);
2038 ret
= avctx
->codec
->encode2(avctx
, avpkt
, frame
, got_packet_ptr
);
2039 av_assert0(ret
<= 0);
2041 if (avpkt
->data
&& avpkt
->data
== avctx
->internal
->byte_buffer
) {
2043 if (user_pkt
.data
) {
2044 if (user_pkt
.size
>= avpkt
->size
) {
2045 memcpy(user_pkt
.data
, avpkt
->data
, avpkt
->size
);
2047 av_log(avctx
, AV_LOG_ERROR
, "Provided packet is too small, needs to be %d\n", avpkt
->size
);
2048 avpkt
->size
= user_pkt
.size
;
2051 avpkt
->buf
= user_pkt
.buf
;
2052 avpkt
->data
= user_pkt
.data
;
2053 #if FF_API_DESTRUCT_PACKET
2054 FF_DISABLE_DEPRECATION_WARNINGS
2055 avpkt
->destruct
= user_pkt
.destruct
;
2056 FF_ENABLE_DEPRECATION_WARNINGS
2059 if (av_dup_packet(avpkt
) < 0) {
2060 ret
= AVERROR(ENOMEM
);
2066 if (!*got_packet_ptr
)
2068 else if (!(avctx
->codec
->capabilities
& CODEC_CAP_DELAY
))
2069 avpkt
->pts
= avpkt
->dts
= frame
->pts
;
2071 if (needs_realloc
&& avpkt
->data
) {
2072 ret
= av_buffer_realloc(&avpkt
->buf
, avpkt
->size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
2074 avpkt
->data
= avpkt
->buf
->data
;
2077 avctx
->frame_number
++;
2080 if (ret
< 0 || !*got_packet_ptr
)
2081 av_free_packet(avpkt
);
2083 av_packet_merge_side_data(avpkt
);
2089 int avcodec_encode_subtitle(AVCodecContext
*avctx
, uint8_t *buf
, int buf_size
,
2090 const AVSubtitle
*sub
)
2093 if (sub
->start_display_time
) {
2094 av_log(avctx
, AV_LOG_ERROR
, "start_display_time must be 0.\n");
2098 ret
= avctx
->codec
->encode_sub(avctx
, buf
, buf_size
, sub
);
2099 avctx
->frame_number
++;
2104 * Attempt to guess proper monotonic timestamps for decoded video frames
2105 * which might have incorrect times. Input timestamps may wrap around, in
2106 * which case the output will as well.
2108 * @param pts the pts field of the decoded AVPacket, as passed through
2110 * @param dts the dts field of the decoded AVPacket
2111 * @return one of the input values, may be AV_NOPTS_VALUE
2113 static int64_t guess_correct_pts(AVCodecContext
*ctx
,
2114 int64_t reordered_pts
, int64_t dts
)
2116 int64_t pts
= AV_NOPTS_VALUE
;
2118 if (dts
!= AV_NOPTS_VALUE
) {
2119 ctx
->pts_correction_num_faulty_dts
+= dts
<= ctx
->pts_correction_last_dts
;
2120 ctx
->pts_correction_last_dts
= dts
;
2121 } else if (reordered_pts
!= AV_NOPTS_VALUE
)
2122 ctx
->pts_correction_last_dts
= reordered_pts
;
2124 if (reordered_pts
!= AV_NOPTS_VALUE
) {
2125 ctx
->pts_correction_num_faulty_pts
+= reordered_pts
<= ctx
->pts_correction_last_pts
;
2126 ctx
->pts_correction_last_pts
= reordered_pts
;
2127 } else if(dts
!= AV_NOPTS_VALUE
)
2128 ctx
->pts_correction_last_pts
= dts
;
2130 if ((ctx
->pts_correction_num_faulty_pts
<=ctx
->pts_correction_num_faulty_dts
|| dts
== AV_NOPTS_VALUE
)
2131 && reordered_pts
!= AV_NOPTS_VALUE
)
2132 pts
= reordered_pts
;
2139 static int apply_param_change(AVCodecContext
*avctx
, AVPacket
*avpkt
)
2142 const uint8_t *data
;
2145 data
= av_packet_get_side_data(avpkt
, AV_PKT_DATA_PARAM_CHANGE
, &size
);
2149 if (!(avctx
->codec
->capabilities
& CODEC_CAP_PARAM_CHANGE
)) {
2150 av_log(avctx
, AV_LOG_ERROR
, "This decoder does not support parameter "
2151 "changes, but PARAM_CHANGE side data was sent to it.\n");
2152 return AVERROR(EINVAL
);
2158 flags
= bytestream_get_le32(&data
);
2161 if (flags
& AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT
) {
2164 avctx
->channels
= bytestream_get_le32(&data
);
2167 if (flags
& AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT
) {
2170 avctx
->channel_layout
= bytestream_get_le64(&data
);
2173 if (flags
& AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE
) {
2176 avctx
->sample_rate
= bytestream_get_le32(&data
);
2179 if (flags
& AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS
) {
2182 avctx
->width
= bytestream_get_le32(&data
);
2183 avctx
->height
= bytestream_get_le32(&data
);
2185 ret
= ff_set_dimensions(avctx
, avctx
->width
, avctx
->height
);
2192 av_log(avctx
, AV_LOG_ERROR
, "PARAM_CHANGE side data too small.\n");
2193 return AVERROR_INVALIDDATA
;
2196 static int add_metadata_from_side_data(AVCodecContext
*avctx
, AVFrame
*frame
)
2199 const uint8_t *side_metadata
;
2201 AVDictionary
**frame_md
= avpriv_frame_get_metadatap(frame
);
2203 side_metadata
= av_packet_get_side_data(avctx
->internal
->pkt
,
2204 AV_PKT_DATA_STRINGS_METADATA
, &size
);
2205 return av_packet_unpack_dictionary(side_metadata
, size
, frame_md
);
2208 static int unrefcount_frame(AVCodecInternal
*avci
, AVFrame
*frame
)
2212 /* move the original frame to our backup */
2213 av_frame_unref(avci
->to_free
);
2214 av_frame_move_ref(avci
->to_free
, frame
);
2216 /* now copy everything except the AVBufferRefs back
2217 * note that we make a COPY of the side data, so calling av_frame_free() on
2218 * the caller's frame will work properly */
2219 ret
= av_frame_copy_props(frame
, avci
->to_free
);
2223 memcpy(frame
->data
, avci
->to_free
->data
, sizeof(frame
->data
));
2224 memcpy(frame
->linesize
, avci
->to_free
->linesize
, sizeof(frame
->linesize
));
2225 if (avci
->to_free
->extended_data
!= avci
->to_free
->data
) {
2226 int planes
= av_frame_get_channels(avci
->to_free
);
2227 int size
= planes
* sizeof(*frame
->extended_data
);
2230 av_frame_unref(frame
);
2234 frame
->extended_data
= av_malloc(size
);
2235 if (!frame
->extended_data
) {
2236 av_frame_unref(frame
);
2237 return AVERROR(ENOMEM
);
2239 memcpy(frame
->extended_data
, avci
->to_free
->extended_data
,
2242 frame
->extended_data
= frame
->data
;
2244 frame
->format
= avci
->to_free
->format
;
2245 frame
->width
= avci
->to_free
->width
;
2246 frame
->height
= avci
->to_free
->height
;
2247 frame
->channel_layout
= avci
->to_free
->channel_layout
;
2248 frame
->nb_samples
= avci
->to_free
->nb_samples
;
2249 av_frame_set_channels(frame
, av_frame_get_channels(avci
->to_free
));
2254 int attribute_align_arg
avcodec_decode_video2(AVCodecContext
*avctx
, AVFrame
*picture
,
2255 int *got_picture_ptr
,
2256 const AVPacket
*avpkt
)
2258 AVCodecInternal
*avci
= avctx
->internal
;
2260 // copy to ensure we do not change avpkt
2261 AVPacket tmp
= *avpkt
;
2264 return AVERROR(EINVAL
);
2265 if (avctx
->codec
->type
!= AVMEDIA_TYPE_VIDEO
) {
2266 av_log(avctx
, AV_LOG_ERROR
, "Invalid media type for video\n");
2267 return AVERROR(EINVAL
);
2270 *got_picture_ptr
= 0;
2271 if ((avctx
->coded_width
|| avctx
->coded_height
) && av_image_check_size(avctx
->coded_width
, avctx
->coded_height
, 0, avctx
))
2272 return AVERROR(EINVAL
);
2274 av_frame_unref(picture
);
2276 if ((avctx
->codec
->capabilities
& CODEC_CAP_DELAY
) || avpkt
->size
|| (avctx
->active_thread_type
& FF_THREAD_FRAME
)) {
2277 int did_split
= av_packet_split_side_data(&tmp
);
2278 ret
= apply_param_change(avctx
, &tmp
);
2280 av_log(avctx
, AV_LOG_ERROR
, "Error applying parameter changes.\n");
2281 if (avctx
->err_recognition
& AV_EF_EXPLODE
)
2285 avctx
->internal
->pkt
= &tmp
;
2286 if (HAVE_THREADS
&& avctx
->active_thread_type
& FF_THREAD_FRAME
)
2287 ret
= ff_thread_decode_frame(avctx
, picture
, got_picture_ptr
,
2290 ret
= avctx
->codec
->decode(avctx
, picture
, got_picture_ptr
,
2292 picture
->pkt_dts
= avpkt
->dts
;
2294 if(!avctx
->has_b_frames
){
2295 av_frame_set_pkt_pos(picture
, avpkt
->pos
);
2297 //FIXME these should be under if(!avctx->has_b_frames)
2298 /* get_buffer is supposed to set frame parameters */
2299 if (!(avctx
->codec
->capabilities
& CODEC_CAP_DR1
)) {
2300 if (!picture
->sample_aspect_ratio
.num
) picture
->sample_aspect_ratio
= avctx
->sample_aspect_ratio
;
2301 if (!picture
->width
) picture
->width
= avctx
->width
;
2302 if (!picture
->height
) picture
->height
= avctx
->height
;
2303 if (picture
->format
== AV_PIX_FMT_NONE
) picture
->format
= avctx
->pix_fmt
;
2306 add_metadata_from_side_data(avctx
, picture
);
2309 emms_c(); //needed to avoid an emms_c() call before every return;
2311 avctx
->internal
->pkt
= NULL
;
2313 av_packet_free_side_data(&tmp
);
2318 if (*got_picture_ptr
) {
2319 if (!avctx
->refcounted_frames
) {
2320 int err
= unrefcount_frame(avci
, picture
);
2325 avctx
->frame_number
++;
2326 av_frame_set_best_effort_timestamp(picture
,
2327 guess_correct_pts(avctx
,
2331 av_frame_unref(picture
);
2335 /* many decoders assign whole AVFrames, thus overwriting extended_data;
2336 * make sure it's set correctly */
2337 av_assert0(!picture
->extended_data
|| picture
->extended_data
== picture
->data
);
2342 #if FF_API_OLD_DECODE_AUDIO
2343 int attribute_align_arg
avcodec_decode_audio3(AVCodecContext
*avctx
, int16_t *samples
,
2344 int *frame_size_ptr
,
2347 AVFrame
*frame
= av_frame_alloc();
2348 int ret
, got_frame
= 0;
2351 return AVERROR(ENOMEM
);
2352 if (avctx
->get_buffer
!= avcodec_default_get_buffer
) {
2353 av_log(avctx
, AV_LOG_ERROR
, "Custom get_buffer() for use with"
2354 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
2355 av_log(avctx
, AV_LOG_ERROR
, "Please port your application to "
2356 "avcodec_decode_audio4()\n");
2357 avctx
->get_buffer
= avcodec_default_get_buffer
;
2358 avctx
->release_buffer
= avcodec_default_release_buffer
;
2361 ret
= avcodec_decode_audio4(avctx
, frame
, &got_frame
, avpkt
);
2363 if (ret
>= 0 && got_frame
) {
2365 int planar
= av_sample_fmt_is_planar(avctx
->sample_fmt
);
2366 int data_size
= av_samples_get_buffer_size(&plane_size
, avctx
->channels
,
2368 avctx
->sample_fmt
, 1);
2369 if (*frame_size_ptr
< data_size
) {
2370 av_log(avctx
, AV_LOG_ERROR
, "output buffer size is too small for "
2371 "the current frame (%d < %d)\n", *frame_size_ptr
, data_size
);
2372 av_frame_free(&frame
);
2373 return AVERROR(EINVAL
);
2376 memcpy(samples
, frame
->extended_data
[0], plane_size
);
2378 if (planar
&& avctx
->channels
> 1) {
2379 uint8_t *out
= ((uint8_t *)samples
) + plane_size
;
2380 for (ch
= 1; ch
< avctx
->channels
; ch
++) {
2381 memcpy(out
, frame
->extended_data
[ch
], plane_size
);
2385 *frame_size_ptr
= data_size
;
2387 *frame_size_ptr
= 0;
2389 av_frame_free(&frame
);
2395 int attribute_align_arg
avcodec_decode_audio4(AVCodecContext
*avctx
,
2398 const AVPacket
*avpkt
)
2400 AVCodecInternal
*avci
= avctx
->internal
;
2405 if (!avpkt
->data
&& avpkt
->size
) {
2406 av_log(avctx
, AV_LOG_ERROR
, "invalid packet: NULL data, size != 0\n");
2407 return AVERROR(EINVAL
);
2410 return AVERROR(EINVAL
);
2411 if (avctx
->codec
->type
!= AVMEDIA_TYPE_AUDIO
) {
2412 av_log(avctx
, AV_LOG_ERROR
, "Invalid media type for audio\n");
2413 return AVERROR(EINVAL
);
2416 av_frame_unref(frame
);
2418 if ((avctx
->codec
->capabilities
& CODEC_CAP_DELAY
) || avpkt
->size
|| (avctx
->active_thread_type
& FF_THREAD_FRAME
)) {
2421 uint32_t discard_padding
= 0;
2422 // copy to ensure we do not change avpkt
2423 AVPacket tmp
= *avpkt
;
2424 int did_split
= av_packet_split_side_data(&tmp
);
2425 ret
= apply_param_change(avctx
, &tmp
);
2427 av_log(avctx
, AV_LOG_ERROR
, "Error applying parameter changes.\n");
2428 if (avctx
->err_recognition
& AV_EF_EXPLODE
)
2432 avctx
->internal
->pkt
= &tmp
;
2433 if (HAVE_THREADS
&& avctx
->active_thread_type
& FF_THREAD_FRAME
)
2434 ret
= ff_thread_decode_frame(avctx
, frame
, got_frame_ptr
, &tmp
);
2436 ret
= avctx
->codec
->decode(avctx
, frame
, got_frame_ptr
, &tmp
);
2437 frame
->pkt_dts
= avpkt
->dts
;
2439 if (ret
>= 0 && *got_frame_ptr
) {
2440 add_metadata_from_side_data(avctx
, frame
);
2441 avctx
->frame_number
++;
2442 av_frame_set_best_effort_timestamp(frame
,
2443 guess_correct_pts(avctx
,
2446 if (frame
->format
== AV_SAMPLE_FMT_NONE
)
2447 frame
->format
= avctx
->sample_fmt
;
2448 if (!frame
->channel_layout
)
2449 frame
->channel_layout
= avctx
->channel_layout
;
2450 if (!av_frame_get_channels(frame
))
2451 av_frame_set_channels(frame
, avctx
->channels
);
2452 if (!frame
->sample_rate
)
2453 frame
->sample_rate
= avctx
->sample_rate
;
2456 side
= av_packet_get_side_data(avctx
->internal
->pkt
, AV_PKT_DATA_SKIP_SAMPLES
, &side_size
);
2457 if(side
&& side_size
>=10) {
2458 avctx
->internal
->skip_samples
= AV_RL32(side
);
2459 av_log(avctx
, AV_LOG_DEBUG
, "skip %d samples due to side data\n",
2460 avctx
->internal
->skip_samples
);
2461 discard_padding
= AV_RL32(side
+ 4);
2463 if (avctx
->internal
->skip_samples
&& *got_frame_ptr
) {
2464 if(frame
->nb_samples
<= avctx
->internal
->skip_samples
){
2466 avctx
->internal
->skip_samples
-= frame
->nb_samples
;
2467 av_log(avctx
, AV_LOG_DEBUG
, "skip whole frame, skip left: %d\n",
2468 avctx
->internal
->skip_samples
);
2470 av_samples_copy(frame
->extended_data
, frame
->extended_data
, 0, avctx
->internal
->skip_samples
,
2471 frame
->nb_samples
- avctx
->internal
->skip_samples
, avctx
->channels
, frame
->format
);
2472 if(avctx
->pkt_timebase
.num
&& avctx
->sample_rate
) {
2473 int64_t diff_ts
= av_rescale_q(avctx
->internal
->skip_samples
,
2474 (AVRational
){1, avctx
->sample_rate
},
2475 avctx
->pkt_timebase
);
2476 if(frame
->pkt_pts
!=AV_NOPTS_VALUE
)
2477 frame
->pkt_pts
+= diff_ts
;
2478 if(frame
->pkt_dts
!=AV_NOPTS_VALUE
)
2479 frame
->pkt_dts
+= diff_ts
;
2480 if (av_frame_get_pkt_duration(frame
) >= diff_ts
)
2481 av_frame_set_pkt_duration(frame
, av_frame_get_pkt_duration(frame
) - diff_ts
);
2483 av_log(avctx
, AV_LOG_WARNING
, "Could not update timestamps for skipped samples.\n");
2485 av_log(avctx
, AV_LOG_DEBUG
, "skip %d/%d samples\n",
2486 avctx
->internal
->skip_samples
, frame
->nb_samples
);
2487 frame
->nb_samples
-= avctx
->internal
->skip_samples
;
2488 avctx
->internal
->skip_samples
= 0;
2492 if (discard_padding
> 0 && discard_padding
<= frame
->nb_samples
&& *got_frame_ptr
) {
2493 if (discard_padding
== frame
->nb_samples
) {
2496 if(avctx
->pkt_timebase
.num
&& avctx
->sample_rate
) {
2497 int64_t diff_ts
= av_rescale_q(frame
->nb_samples
- discard_padding
,
2498 (AVRational
){1, avctx
->sample_rate
},
2499 avctx
->pkt_timebase
);
2500 if (av_frame_get_pkt_duration(frame
) >= diff_ts
)
2501 av_frame_set_pkt_duration(frame
, av_frame_get_pkt_duration(frame
) - diff_ts
);
2503 av_log(avctx
, AV_LOG_WARNING
, "Could not update timestamps for discarded samples.\n");
2505 av_log(avctx
, AV_LOG_DEBUG
, "discard %d/%d samples\n",
2506 discard_padding
, frame
->nb_samples
);
2507 frame
->nb_samples
-= discard_padding
;
2511 avctx
->internal
->pkt
= NULL
;
2513 av_packet_free_side_data(&tmp
);
2518 if (ret
>= 0 && *got_frame_ptr
) {
2519 if (!avctx
->refcounted_frames
) {
2520 int err
= unrefcount_frame(avci
, frame
);
2525 av_frame_unref(frame
);
2531 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
2532 static int recode_subtitle(AVCodecContext
*avctx
,
2533 AVPacket
*outpkt
, const AVPacket
*inpkt
)
2536 iconv_t cd
= (iconv_t
)-1;
2543 if (avctx
->sub_charenc_mode
!= FF_SUB_CHARENC_MODE_PRE_DECODER
|| inpkt
->size
== 0)
2547 cd
= iconv_open("UTF-8", avctx
->sub_charenc
);
2548 av_assert0(cd
!= (iconv_t
)-1);
2553 if (inl
>= INT_MAX
/ UTF8_MAX_BYTES
- FF_INPUT_BUFFER_PADDING_SIZE
) {
2554 av_log(avctx
, AV_LOG_ERROR
, "Subtitles packet is too big for recoding\n");
2555 ret
= AVERROR(ENOMEM
);
2559 ret
= av_new_packet(&tmp
, inl
* UTF8_MAX_BYTES
);
2562 outpkt
->buf
= tmp
.buf
;
2563 outpkt
->data
= tmp
.data
;
2564 outpkt
->size
= tmp
.size
;
2565 outb
= outpkt
->data
;
2566 outl
= outpkt
->size
;
2568 if (iconv(cd
, &inb
, &inl
, &outb
, &outl
) == (size_t)-1 ||
2569 iconv(cd
, NULL
, NULL
, &outb
, &outl
) == (size_t)-1 ||
2570 outl
>= outpkt
->size
|| inl
!= 0) {
2571 av_log(avctx
, AV_LOG_ERROR
, "Unable to recode subtitle event \"%s\" "
2572 "from %s to UTF-8\n", inpkt
->data
, avctx
->sub_charenc
);
2573 av_free_packet(&tmp
);
2574 ret
= AVERROR(errno
);
2577 outpkt
->size
-= outl
;
2578 memset(outpkt
->data
+ outpkt
->size
, 0, outl
);
2581 if (cd
!= (iconv_t
)-1)
2585 av_log(avctx
, AV_LOG_ERROR
, "requesting subtitles recoding without iconv");
2586 return AVERROR(EINVAL
);
2590 static int utf8_check(const uint8_t *str
)
2592 const uint8_t *byte
;
2593 uint32_t codepoint
, min
;
2597 GET_UTF8(codepoint
, *(byte
++), return 0;);
2598 min
= byte
- str
== 1 ? 0 : byte
- str
== 2 ? 0x80 :
2599 1 << (5 * (byte
- str
) - 4);
2600 if (codepoint
< min
|| codepoint
>= 0x110000 ||
2601 codepoint
== 0xFFFE /* BOM */ ||
2602 codepoint
>= 0xD800 && codepoint
<= 0xDFFF /* surrogates */)
2609 int avcodec_decode_subtitle2(AVCodecContext
*avctx
, AVSubtitle
*sub
,
2615 if (!avpkt
->data
&& avpkt
->size
) {
2616 av_log(avctx
, AV_LOG_ERROR
, "invalid packet: NULL data, size != 0\n");
2617 return AVERROR(EINVAL
);
2620 return AVERROR(EINVAL
);
2621 if (avctx
->codec
->type
!= AVMEDIA_TYPE_SUBTITLE
) {
2622 av_log(avctx
, AV_LOG_ERROR
, "Invalid media type for subtitles\n");
2623 return AVERROR(EINVAL
);
2627 avcodec_get_subtitle_defaults(sub
);
2629 if ((avctx
->codec
->capabilities
& CODEC_CAP_DELAY
) || avpkt
->size
) {
2630 AVPacket pkt_recoded
;
2631 AVPacket tmp
= *avpkt
;
2632 int did_split
= av_packet_split_side_data(&tmp
);
2633 //apply_param_change(avctx, &tmp);
2636 /* FFMIN() prevents overflow in case the packet wasn't allocated with
2638 * If the side data is smaller than the buffer padding size, the
2639 * remaining bytes should have already been filled with zeros by the
2640 * original packet allocation anyway. */
2641 memset(tmp
.data
+ tmp
.size
, 0,
2642 FFMIN(avpkt
->size
- tmp
.size
, FF_INPUT_BUFFER_PADDING_SIZE
));
2646 ret
= recode_subtitle(avctx
, &pkt_recoded
, &tmp
);
2650 avctx
->internal
->pkt
= &pkt_recoded
;
2652 if (avctx
->pkt_timebase
.den
&& avpkt
->pts
!= AV_NOPTS_VALUE
)
2653 sub
->pts
= av_rescale_q(avpkt
->pts
,
2654 avctx
->pkt_timebase
, AV_TIME_BASE_Q
);
2655 ret
= avctx
->codec
->decode(avctx
, sub
, got_sub_ptr
, &pkt_recoded
);
2656 av_assert1((ret
>= 0) >= !!*got_sub_ptr
&&
2657 !!*got_sub_ptr
>= !!sub
->num_rects
);
2659 if (sub
->num_rects
&& !sub
->end_display_time
&& avpkt
->duration
&&
2660 avctx
->pkt_timebase
.num
) {
2661 AVRational ms
= { 1, 1000 };
2662 sub
->end_display_time
= av_rescale_q(avpkt
->duration
,
2663 avctx
->pkt_timebase
, ms
);
2666 for (i
= 0; i
< sub
->num_rects
; i
++) {
2667 if (sub
->rects
[i
]->ass
&& !utf8_check(sub
->rects
[i
]->ass
)) {
2668 av_log(avctx
, AV_LOG_ERROR
,
2669 "Invalid UTF-8 in decoded subtitles text; "
2670 "maybe missing -sub_charenc option\n");
2671 avsubtitle_free(sub
);
2672 return AVERROR_INVALIDDATA
;
2676 if (tmp
.data
!= pkt_recoded
.data
) { // did we recode?
2677 /* prevent from destroying side data from original packet */
2678 pkt_recoded
.side_data
= NULL
;
2679 pkt_recoded
.side_data_elems
= 0;
2681 av_free_packet(&pkt_recoded
);
2683 if (avctx
->codec_descriptor
->props
& AV_CODEC_PROP_BITMAP_SUB
)
2685 else if (avctx
->codec_descriptor
->props
& AV_CODEC_PROP_TEXT_SUB
)
2687 avctx
->internal
->pkt
= NULL
;
2691 av_packet_free_side_data(&tmp
);
2697 avctx
->frame_number
++;
2703 void avsubtitle_free(AVSubtitle
*sub
)
2707 for (i
= 0; i
< sub
->num_rects
; i
++) {
2708 av_freep(&sub
->rects
[i
]->pict
.data
[0]);
2709 av_freep(&sub
->rects
[i
]->pict
.data
[1]);
2710 av_freep(&sub
->rects
[i
]->pict
.data
[2]);
2711 av_freep(&sub
->rects
[i
]->pict
.data
[3]);
2712 av_freep(&sub
->rects
[i
]->text
);
2713 av_freep(&sub
->rects
[i
]->ass
);
2714 av_freep(&sub
->rects
[i
]);
2717 av_freep(&sub
->rects
);
2719 memset(sub
, 0, sizeof(AVSubtitle
));
2722 av_cold
int avcodec_close(AVCodecContext
*avctx
)
2727 if (avcodec_is_open(avctx
)) {
2728 FramePool
*pool
= avctx
->internal
->pool
;
2730 if (CONFIG_FRAME_THREAD_ENCODER
&&
2731 avctx
->internal
->frame_thread_encoder
&& avctx
->thread_count
> 1) {
2732 ff_frame_thread_encoder_free(avctx
);
2734 if (HAVE_THREADS
&& avctx
->internal
->thread_ctx
)
2735 ff_thread_free(avctx
);
2736 if (avctx
->codec
&& avctx
->codec
->close
)
2737 avctx
->codec
->close(avctx
);
2738 avctx
->coded_frame
= NULL
;
2739 avctx
->internal
->byte_buffer_size
= 0;
2740 av_freep(&avctx
->internal
->byte_buffer
);
2741 av_frame_free(&avctx
->internal
->to_free
);
2742 for (i
= 0; i
< FF_ARRAY_ELEMS(pool
->pools
); i
++)
2743 av_buffer_pool_uninit(&pool
->pools
[i
]);
2744 av_freep(&avctx
->internal
->pool
);
2746 if (avctx
->hwaccel
&& avctx
->hwaccel
->uninit
)
2747 avctx
->hwaccel
->uninit(avctx
);
2748 av_freep(&avctx
->internal
->hwaccel_priv_data
);
2750 av_freep(&avctx
->internal
);
2753 if (avctx
->priv_data
&& avctx
->codec
&& avctx
->codec
->priv_class
)
2754 av_opt_free(avctx
->priv_data
);
2756 av_freep(&avctx
->priv_data
);
2757 if (av_codec_is_encoder(avctx
->codec
))
2758 av_freep(&avctx
->extradata
);
2759 avctx
->codec
= NULL
;
2760 avctx
->active_thread_type
= 0;
2765 static enum AVCodecID
remap_deprecated_codec_id(enum AVCodecID id
)
2768 //This is for future deprecatec codec ids, its empty since
2769 //last major bump but will fill up again over time, please don't remove it
2770 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
2771 case AV_CODEC_ID_BRENDER_PIX_DEPRECATED
: return AV_CODEC_ID_BRENDER_PIX
;
2772 case AV_CODEC_ID_OPUS_DEPRECATED
: return AV_CODEC_ID_OPUS
;
2773 case AV_CODEC_ID_TAK_DEPRECATED
: return AV_CODEC_ID_TAK
;
2774 case AV_CODEC_ID_PAF_AUDIO_DEPRECATED
: return AV_CODEC_ID_PAF_AUDIO
;
2775 case AV_CODEC_ID_PCM_S24LE_PLANAR_DEPRECATED
: return AV_CODEC_ID_PCM_S24LE_PLANAR
;
2776 case AV_CODEC_ID_PCM_S32LE_PLANAR_DEPRECATED
: return AV_CODEC_ID_PCM_S32LE_PLANAR
;
2777 case AV_CODEC_ID_ADPCM_VIMA_DEPRECATED
: return AV_CODEC_ID_ADPCM_VIMA
;
2778 case AV_CODEC_ID_ESCAPE130_DEPRECATED
: return AV_CODEC_ID_ESCAPE130
;
2779 case AV_CODEC_ID_EXR_DEPRECATED
: return AV_CODEC_ID_EXR
;
2780 case AV_CODEC_ID_G2M_DEPRECATED
: return AV_CODEC_ID_G2M
;
2781 case AV_CODEC_ID_PAF_VIDEO_DEPRECATED
: return AV_CODEC_ID_PAF_VIDEO
;
2782 case AV_CODEC_ID_WEBP_DEPRECATED
: return AV_CODEC_ID_WEBP
;
2783 case AV_CODEC_ID_HEVC_DEPRECATED
: return AV_CODEC_ID_HEVC
;
2784 case AV_CODEC_ID_MVC1_DEPRECATED
: return AV_CODEC_ID_MVC1
;
2785 case AV_CODEC_ID_MVC2_DEPRECATED
: return AV_CODEC_ID_MVC2
;
2786 case AV_CODEC_ID_SANM_DEPRECATED
: return AV_CODEC_ID_SANM
;
2787 case AV_CODEC_ID_SGIRLE_DEPRECATED
: return AV_CODEC_ID_SGIRLE
;
2788 case AV_CODEC_ID_VP7_DEPRECATED
: return AV_CODEC_ID_VP7
;
2789 default : return id
;
2793 static AVCodec
*find_encdec(enum AVCodecID id
, int encoder
)
2795 AVCodec
*p
, *experimental
= NULL
;
2797 id
= remap_deprecated_codec_id(id
);
2799 if ((encoder
? av_codec_is_encoder(p
) : av_codec_is_decoder(p
)) &&
2801 if (p
->capabilities
& CODEC_CAP_EXPERIMENTAL
&& !experimental
) {
2808 return experimental
;
2811 AVCodec
*avcodec_find_encoder(enum AVCodecID id
)
2813 return find_encdec(id
, 1);
2816 AVCodec
*avcodec_find_encoder_by_name(const char *name
)
2823 if (av_codec_is_encoder(p
) && strcmp(name
, p
->name
) == 0)
2830 AVCodec
*avcodec_find_decoder(enum AVCodecID id
)
2832 return find_encdec(id
, 0);
2835 AVCodec
*avcodec_find_decoder_by_name(const char *name
)
2842 if (av_codec_is_decoder(p
) && strcmp(name
, p
->name
) == 0)
2849 const char *avcodec_get_name(enum AVCodecID id
)
2851 const AVCodecDescriptor
*cd
;
2854 if (id
== AV_CODEC_ID_NONE
)
2856 cd
= avcodec_descriptor_get(id
);
2859 av_log(NULL
, AV_LOG_WARNING
, "Codec 0x%x is not in the full list.\n", id
);
2860 codec
= avcodec_find_decoder(id
);
2863 codec
= avcodec_find_encoder(id
);
2866 return "unknown_codec";
2869 size_t av_get_codec_tag_string(char *buf
, size_t buf_size
, unsigned int codec_tag
)
2871 int i
, len
, ret
= 0;
2873 #define TAG_PRINT(x) \
2874 (((x) >= '0' && (x) <= '9') || \
2875 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2876 ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2878 for (i
= 0; i
< 4; i
++) {
2879 len
= snprintf(buf
, buf_size
,
2880 TAG_PRINT(codec_tag
& 0xFF) ? "%c" : "[%d]", codec_tag
& 0xFF);
2882 buf_size
= buf_size
> len
? buf_size
- len
: 0;
2889 void avcodec_string(char *buf
, int buf_size
, AVCodecContext
*enc
, int encode
)
2891 const char *codec_type
;
2892 const char *codec_name
;
2893 const char *profile
= NULL
;
2896 AVRational display_aspect_ratio
;
2898 if (!buf
|| buf_size
<= 0)
2900 codec_type
= av_get_media_type_string(enc
->codec_type
);
2901 codec_name
= avcodec_get_name(enc
->codec_id
);
2902 if (enc
->profile
!= FF_PROFILE_UNKNOWN
) {
2906 p
= encode
? avcodec_find_encoder(enc
->codec_id
) :
2907 avcodec_find_decoder(enc
->codec_id
);
2909 profile
= av_get_profile_name(p
, enc
->profile
);
2912 snprintf(buf
, buf_size
, "%s: %s", codec_type
? codec_type
: "unknown",
2914 buf
[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2916 if (enc
->codec
&& strcmp(enc
->codec
->name
, codec_name
))
2917 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
), " (%s)", enc
->codec
->name
);
2920 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
), " (%s)", profile
);
2921 if (enc
->codec_tag
) {
2923 av_get_codec_tag_string(tag_buf
, sizeof(tag_buf
), enc
->codec_tag
);
2924 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
2925 " (%s / 0x%04X)", tag_buf
, enc
->codec_tag
);
2928 switch (enc
->codec_type
) {
2929 case AVMEDIA_TYPE_VIDEO
:
2930 if (enc
->pix_fmt
!= AV_PIX_FMT_NONE
) {
2931 char detail
[256] = "(";
2932 const char *colorspace_name
;
2933 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
2935 av_get_pix_fmt_name(enc
->pix_fmt
));
2936 if (enc
->bits_per_raw_sample
&&
2937 enc
->bits_per_raw_sample
<= av_pix_fmt_desc_get(enc
->pix_fmt
)->comp
[0].depth_minus1
)
2938 av_strlcatf(detail
, sizeof(detail
), "%d bpc, ", enc
->bits_per_raw_sample
);
2939 if (enc
->color_range
!= AVCOL_RANGE_UNSPECIFIED
)
2940 av_strlcatf(detail
, sizeof(detail
),
2941 enc
->color_range
== AVCOL_RANGE_MPEG
? "tv, ": "pc, ");
2943 colorspace_name
= av_get_colorspace_name(enc
->colorspace
);
2944 if (colorspace_name
)
2945 av_strlcatf(detail
, sizeof(detail
), "%s, ", colorspace_name
);
2947 if (strlen(detail
) > 1) {
2948 detail
[strlen(detail
) - 2] = 0;
2949 av_strlcatf(buf
, buf_size
, "%s)", detail
);
2953 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
2955 enc
->width
, enc
->height
);
2956 if (enc
->sample_aspect_ratio
.num
) {
2957 av_reduce(&display_aspect_ratio
.num
, &display_aspect_ratio
.den
,
2958 enc
->width
* enc
->sample_aspect_ratio
.num
,
2959 enc
->height
* enc
->sample_aspect_ratio
.den
,
2961 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
2962 " [SAR %d:%d DAR %d:%d]",
2963 enc
->sample_aspect_ratio
.num
, enc
->sample_aspect_ratio
.den
,
2964 display_aspect_ratio
.num
, display_aspect_ratio
.den
);
2966 if (av_log_get_level() >= AV_LOG_DEBUG
) {
2967 int g
= av_gcd(enc
->time_base
.num
, enc
->time_base
.den
);
2968 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
2970 enc
->time_base
.num
/ g
, enc
->time_base
.den
/ g
);
2974 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
2975 ", q=%d-%d", enc
->qmin
, enc
->qmax
);
2978 case AVMEDIA_TYPE_AUDIO
:
2979 if (enc
->sample_rate
) {
2980 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
2981 ", %d Hz", enc
->sample_rate
);
2983 av_strlcat(buf
, ", ", buf_size
);
2984 av_get_channel_layout_string(buf
+ strlen(buf
), buf_size
- strlen(buf
), enc
->channels
, enc
->channel_layout
);
2985 if (enc
->sample_fmt
!= AV_SAMPLE_FMT_NONE
) {
2986 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
2987 ", %s", av_get_sample_fmt_name(enc
->sample_fmt
));
2989 if ( enc
->bits_per_raw_sample
> 0
2990 && enc
->bits_per_raw_sample
!= av_get_bytes_per_sample(enc
->sample_fmt
) * 8)
2991 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
2992 " (%d bit)", enc
->bits_per_raw_sample
);
2994 case AVMEDIA_TYPE_DATA
:
2995 if (av_log_get_level() >= AV_LOG_DEBUG
) {
2996 int g
= av_gcd(enc
->time_base
.num
, enc
->time_base
.den
);
2998 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
3000 enc
->time_base
.num
/ g
, enc
->time_base
.den
/ g
);
3003 case AVMEDIA_TYPE_SUBTITLE
:
3005 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
3006 ", %dx%d", enc
->width
, enc
->height
);
3012 if (enc
->flags
& CODEC_FLAG_PASS1
)
3013 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
3015 if (enc
->flags
& CODEC_FLAG_PASS2
)
3016 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
3019 bitrate
= get_bit_rate(enc
);
3021 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
3022 ", %d kb/s", bitrate
/ 1000);
3023 } else if (enc
->rc_max_rate
> 0) {
3024 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
3025 ", max. %d kb/s", enc
->rc_max_rate
/ 1000);
3029 const char *av_get_profile_name(const AVCodec
*codec
, int profile
)
3032 if (profile
== FF_PROFILE_UNKNOWN
|| !codec
->profiles
)
3035 for (p
= codec
->profiles
; p
->profile
!= FF_PROFILE_UNKNOWN
; p
++)
3036 if (p
->profile
== profile
)
3042 unsigned avcodec_version(void)
3044 // av_assert0(AV_CODEC_ID_V410==164);
3045 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR
==65563);
3046 av_assert0(AV_CODEC_ID_ADPCM_G722
==69660);
3047 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
3048 av_assert0(AV_CODEC_ID_SRT
==94216);
3049 av_assert0(LIBAVCODEC_VERSION_MICRO
>= 100);
3051 av_assert0(CODEC_ID_CLLC
== AV_CODEC_ID_CLLC
);
3052 av_assert0(CODEC_ID_PCM_S8_PLANAR
== AV_CODEC_ID_PCM_S8_PLANAR
);
3053 av_assert0(CODEC_ID_ADPCM_IMA_APC
== AV_CODEC_ID_ADPCM_IMA_APC
);
3054 av_assert0(CODEC_ID_ILBC
== AV_CODEC_ID_ILBC
);
3055 av_assert0(CODEC_ID_SRT
== AV_CODEC_ID_SRT
);
3056 return LIBAVCODEC_VERSION_INT
;
3059 const char *avcodec_configuration(void)
3061 return FFMPEG_CONFIGURATION
;
3064 const char *avcodec_license(void)
3066 #define LICENSE_PREFIX "libavcodec license: "
3067 return LICENSE_PREFIX FFMPEG_LICENSE
+ sizeof(LICENSE_PREFIX
) - 1;
3070 void avcodec_flush_buffers(AVCodecContext
*avctx
)
3072 if (HAVE_THREADS
&& avctx
->active_thread_type
& FF_THREAD_FRAME
)
3073 ff_thread_flush(avctx
);
3074 else if (avctx
->codec
->flush
)
3075 avctx
->codec
->flush(avctx
);
3077 avctx
->pts_correction_last_pts
=
3078 avctx
->pts_correction_last_dts
= INT64_MIN
;
3080 if (!avctx
->refcounted_frames
)
3081 av_frame_unref(avctx
->internal
->to_free
);
3084 int av_get_exact_bits_per_sample(enum AVCodecID codec_id
)
3087 case AV_CODEC_ID_8SVX_EXP
:
3088 case AV_CODEC_ID_8SVX_FIB
:
3089 case AV_CODEC_ID_ADPCM_CT
:
3090 case AV_CODEC_ID_ADPCM_IMA_APC
:
3091 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD
:
3092 case AV_CODEC_ID_ADPCM_IMA_OKI
:
3093 case AV_CODEC_ID_ADPCM_IMA_WS
:
3094 case AV_CODEC_ID_ADPCM_G722
:
3095 case AV_CODEC_ID_ADPCM_YAMAHA
:
3097 case AV_CODEC_ID_DSD_LSBF
:
3098 case AV_CODEC_ID_DSD_MSBF
:
3099 case AV_CODEC_ID_DSD_LSBF_PLANAR
:
3100 case AV_CODEC_ID_DSD_MSBF_PLANAR
:
3101 case AV_CODEC_ID_PCM_ALAW
:
3102 case AV_CODEC_ID_PCM_MULAW
:
3103 case AV_CODEC_ID_PCM_S8
:
3104 case AV_CODEC_ID_PCM_S8_PLANAR
:
3105 case AV_CODEC_ID_PCM_U8
:
3106 case AV_CODEC_ID_PCM_ZORK
:
3108 case AV_CODEC_ID_PCM_S16BE
:
3109 case AV_CODEC_ID_PCM_S16BE_PLANAR
:
3110 case AV_CODEC_ID_PCM_S16LE
:
3111 case AV_CODEC_ID_PCM_S16LE_PLANAR
:
3112 case AV_CODEC_ID_PCM_U16BE
:
3113 case AV_CODEC_ID_PCM_U16LE
:
3115 case AV_CODEC_ID_PCM_S24DAUD
:
3116 case AV_CODEC_ID_PCM_S24BE
:
3117 case AV_CODEC_ID_PCM_S24LE
:
3118 case AV_CODEC_ID_PCM_S24LE_PLANAR
:
3119 case AV_CODEC_ID_PCM_U24BE
:
3120 case AV_CODEC_ID_PCM_U24LE
:
3122 case AV_CODEC_ID_PCM_S32BE
:
3123 case AV_CODEC_ID_PCM_S32LE
:
3124 case AV_CODEC_ID_PCM_S32LE_PLANAR
:
3125 case AV_CODEC_ID_PCM_U32BE
:
3126 case AV_CODEC_ID_PCM_U32LE
:
3127 case AV_CODEC_ID_PCM_F32BE
:
3128 case AV_CODEC_ID_PCM_F32LE
:
3130 case AV_CODEC_ID_PCM_F64BE
:
3131 case AV_CODEC_ID_PCM_F64LE
:
3138 enum AVCodecID
av_get_pcm_codec(enum AVSampleFormat fmt
, int be
)
3140 static const enum AVCodecID map
[AV_SAMPLE_FMT_NB
][2] = {
3141 [AV_SAMPLE_FMT_U8
] = { AV_CODEC_ID_PCM_U8
, AV_CODEC_ID_PCM_U8
},
3142 [AV_SAMPLE_FMT_S16
] = { AV_CODEC_ID_PCM_S16LE
, AV_CODEC_ID_PCM_S16BE
},
3143 [AV_SAMPLE_FMT_S32
] = { AV_CODEC_ID_PCM_S32LE
, AV_CODEC_ID_PCM_S32BE
},
3144 [AV_SAMPLE_FMT_FLT
] = { AV_CODEC_ID_PCM_F32LE
, AV_CODEC_ID_PCM_F32BE
},
3145 [AV_SAMPLE_FMT_DBL
] = { AV_CODEC_ID_PCM_F64LE
, AV_CODEC_ID_PCM_F64BE
},
3146 [AV_SAMPLE_FMT_U8P
] = { AV_CODEC_ID_PCM_U8
, AV_CODEC_ID_PCM_U8
},
3147 [AV_SAMPLE_FMT_S16P
] = { AV_CODEC_ID_PCM_S16LE
, AV_CODEC_ID_PCM_S16BE
},
3148 [AV_SAMPLE_FMT_S32P
] = { AV_CODEC_ID_PCM_S32LE
, AV_CODEC_ID_PCM_S32BE
},
3149 [AV_SAMPLE_FMT_FLTP
] = { AV_CODEC_ID_PCM_F32LE
, AV_CODEC_ID_PCM_F32BE
},
3150 [AV_SAMPLE_FMT_DBLP
] = { AV_CODEC_ID_PCM_F64LE
, AV_CODEC_ID_PCM_F64BE
},
3152 if (fmt
< 0 || fmt
>= AV_SAMPLE_FMT_NB
)
3153 return AV_CODEC_ID_NONE
;
3154 if (be
< 0 || be
> 1)
3156 return map
[fmt
][be
];
3159 int av_get_bits_per_sample(enum AVCodecID codec_id
)
3162 case AV_CODEC_ID_ADPCM_SBPRO_2
:
3164 case AV_CODEC_ID_ADPCM_SBPRO_3
:
3166 case AV_CODEC_ID_ADPCM_SBPRO_4
:
3167 case AV_CODEC_ID_ADPCM_IMA_WAV
:
3168 case AV_CODEC_ID_ADPCM_IMA_QT
:
3169 case AV_CODEC_ID_ADPCM_SWF
:
3170 case AV_CODEC_ID_ADPCM_MS
:
3173 return av_get_exact_bits_per_sample(codec_id
);
3177 int av_get_audio_frame_duration(AVCodecContext
*avctx
, int frame_bytes
)
3179 int id
, sr
, ch
, ba
, tag
, bps
;
3181 id
= avctx
->codec_id
;
3182 sr
= avctx
->sample_rate
;
3183 ch
= avctx
->channels
;
3184 ba
= avctx
->block_align
;
3185 tag
= avctx
->codec_tag
;
3186 bps
= av_get_exact_bits_per_sample(avctx
->codec_id
);
3188 /* codecs with an exact constant bits per sample */
3189 if (bps
> 0 && ch
> 0 && frame_bytes
> 0 && ch
< 32768 && bps
< 32768)
3190 return (frame_bytes
* 8LL) / (bps
* ch
);
3191 bps
= avctx
->bits_per_coded_sample
;
3193 /* codecs with a fixed packet duration */
3195 case AV_CODEC_ID_ADPCM_ADX
: return 32;
3196 case AV_CODEC_ID_ADPCM_IMA_QT
: return 64;
3197 case AV_CODEC_ID_ADPCM_EA_XAS
: return 128;
3198 case AV_CODEC_ID_AMR_NB
:
3199 case AV_CODEC_ID_EVRC
:
3200 case AV_CODEC_ID_GSM
:
3201 case AV_CODEC_ID_QCELP
:
3202 case AV_CODEC_ID_RA_288
: return 160;
3203 case AV_CODEC_ID_AMR_WB
:
3204 case AV_CODEC_ID_GSM_MS
: return 320;
3205 case AV_CODEC_ID_MP1
: return 384;
3206 case AV_CODEC_ID_ATRAC1
: return 512;
3207 case AV_CODEC_ID_ATRAC3
: return 1024;
3208 case AV_CODEC_ID_MP2
:
3209 case AV_CODEC_ID_MUSEPACK7
: return 1152;
3210 case AV_CODEC_ID_AC3
: return 1536;
3214 /* calc from sample rate */
3215 if (id
== AV_CODEC_ID_TTA
)
3216 return 256 * sr
/ 245;
3219 /* calc from sample rate and channels */
3220 if (id
== AV_CODEC_ID_BINKAUDIO_DCT
)
3221 return (480 << (sr
/ 22050)) / ch
;
3226 /* calc from block_align */
3227 if (id
== AV_CODEC_ID_SIPR
) {
3229 case 20: return 160;
3230 case 19: return 144;
3231 case 29: return 288;
3232 case 37: return 480;
3234 } else if (id
== AV_CODEC_ID_ILBC
) {
3236 case 38: return 160;
3237 case 50: return 240;
3242 if (frame_bytes
> 0) {
3243 /* calc from frame_bytes only */
3244 if (id
== AV_CODEC_ID_TRUESPEECH
)
3245 return 240 * (frame_bytes
/ 32);
3246 if (id
== AV_CODEC_ID_NELLYMOSER
)
3247 return 256 * (frame_bytes
/ 64);
3248 if (id
== AV_CODEC_ID_RA_144
)
3249 return 160 * (frame_bytes
/ 20);
3250 if (id
== AV_CODEC_ID_G723_1
)
3251 return 240 * (frame_bytes
/ 24);
3254 /* calc from frame_bytes and bits_per_coded_sample */
3255 if (id
== AV_CODEC_ID_ADPCM_G726
)
3256 return frame_bytes
* 8 / bps
;
3260 /* calc from frame_bytes and channels */
3262 case AV_CODEC_ID_ADPCM_AFC
:
3263 return frame_bytes
/ (9 * ch
) * 16;
3264 case AV_CODEC_ID_ADPCM_DTK
:
3265 return frame_bytes
/ (16 * ch
) * 28;
3266 case AV_CODEC_ID_ADPCM_4XM
:
3267 case AV_CODEC_ID_ADPCM_IMA_ISS
:
3268 return (frame_bytes
- 4 * ch
) * 2 / ch
;
3269 case AV_CODEC_ID_ADPCM_IMA_SMJPEG
:
3270 return (frame_bytes
- 4) * 2 / ch
;
3271 case AV_CODEC_ID_ADPCM_IMA_AMV
:
3272 return (frame_bytes
- 8) * 2 / ch
;
3273 case AV_CODEC_ID_ADPCM_XA
:
3274 return (frame_bytes
/ 128) * 224 / ch
;
3275 case AV_CODEC_ID_INTERPLAY_DPCM
:
3276 return (frame_bytes
- 6 - ch
) / ch
;
3277 case AV_CODEC_ID_ROQ_DPCM
:
3278 return (frame_bytes
- 8) / ch
;
3279 case AV_CODEC_ID_XAN_DPCM
:
3280 return (frame_bytes
- 2 * ch
) / ch
;
3281 case AV_CODEC_ID_MACE3
:
3282 return 3 * frame_bytes
/ ch
;
3283 case AV_CODEC_ID_MACE6
:
3284 return 6 * frame_bytes
/ ch
;
3285 case AV_CODEC_ID_PCM_LXF
:
3286 return 2 * (frame_bytes
/ (5 * ch
));
3287 case AV_CODEC_ID_IAC
:
3288 case AV_CODEC_ID_IMC
:
3289 return 4 * frame_bytes
/ ch
;
3293 /* calc from frame_bytes, channels, and codec_tag */
3294 if (id
== AV_CODEC_ID_SOL_DPCM
) {
3296 return frame_bytes
/ ch
;
3298 return frame_bytes
* 2 / ch
;
3303 /* calc from frame_bytes, channels, and block_align */
3304 int blocks
= frame_bytes
/ ba
;
3305 switch (avctx
->codec_id
) {
3306 case AV_CODEC_ID_ADPCM_IMA_WAV
:
3307 if (bps
< 2 || bps
> 5)
3309 return blocks
* (1 + (ba
- 4 * ch
) / (bps
* ch
) * 8);
3310 case AV_CODEC_ID_ADPCM_IMA_DK3
:
3311 return blocks
* (((ba
- 16) * 2 / 3 * 4) / ch
);
3312 case AV_CODEC_ID_ADPCM_IMA_DK4
:
3313 return blocks
* (1 + (ba
- 4 * ch
) * 2 / ch
);
3314 case AV_CODEC_ID_ADPCM_IMA_RAD
:
3315 return blocks
* ((ba
- 4 * ch
) * 2 / ch
);
3316 case AV_CODEC_ID_ADPCM_MS
:
3317 return blocks
* (2 + (ba
- 7 * ch
) * 2 / ch
);
3322 /* calc from frame_bytes, channels, and bits_per_coded_sample */
3323 switch (avctx
->codec_id
) {
3324 case AV_CODEC_ID_PCM_DVD
:
3327 return 2 * (frame_bytes
/ ((bps
* 2 / 8) * ch
));
3328 case AV_CODEC_ID_PCM_BLURAY
:
3331 return frame_bytes
/ ((FFALIGN(ch
, 2) * bps
) / 8);
3332 case AV_CODEC_ID_S302M
:
3333 return 2 * (frame_bytes
/ ((bps
+ 4) / 4)) / ch
;
3339 /* Fall back on using frame_size */
3340 if (avctx
->frame_size
> 1 && frame_bytes
)
3341 return avctx
->frame_size
;
3343 //For WMA we currently have no other means to calculate duration thus we
3344 //do it here by assuming CBR, which is true for all known cases.
3345 if (avctx
->bit_rate
>0 && frame_bytes
>0 && avctx
->sample_rate
>0 && avctx
->block_align
>1) {
3346 if (avctx
->codec_id
== AV_CODEC_ID_WMAV1
|| avctx
->codec_id
== AV_CODEC_ID_WMAV2
)
3347 return (frame_bytes
* 8LL * avctx
->sample_rate
) / avctx
->bit_rate
;
3354 int ff_thread_init(AVCodecContext
*s
)
3361 unsigned int av_xiphlacing(unsigned char *s
, unsigned int v
)
3375 int ff_match_2uint16(const uint16_t(*tab
)[2], int size
, int a
, int b
)
3378 for (i
= 0; i
< size
&& !(tab
[i
][0] == a
&& tab
[i
][1] == b
); i
++) ;
3382 #if FF_API_MISSING_SAMPLE
3383 FF_DISABLE_DEPRECATION_WARNINGS
3384 void av_log_missing_feature(void *avc
, const char *feature
, int want_sample
)
3386 av_log(avc
, AV_LOG_WARNING
, "%s is not implemented. Update your FFmpeg "
3387 "version to the newest one from Git. If the problem still "
3388 "occurs, it means that your file has a feature which has not "
3389 "been implemented.\n", feature
);
3391 av_log_ask_for_sample(avc
, NULL
);
3394 void av_log_ask_for_sample(void *avc
, const char *msg
, ...)
3396 va_list argument_list
;
3398 va_start(argument_list
, msg
);
3401 av_vlog(avc
, AV_LOG_WARNING
, msg
, argument_list
);
3402 av_log(avc
, AV_LOG_WARNING
, "If you want to help, upload a sample "
3403 "of this file to ftp://upload.ffmpeg.org/incoming/ "
3404 "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
3406 va_end(argument_list
);
3408 FF_ENABLE_DEPRECATION_WARNINGS
3409 #endif /* FF_API_MISSING_SAMPLE */
3411 static AVHWAccel
*first_hwaccel
= NULL
;
3412 static AVHWAccel
**last_hwaccel
= &first_hwaccel
;
3414 void av_register_hwaccel(AVHWAccel
*hwaccel
)
3416 AVHWAccel
**p
= last_hwaccel
;
3417 hwaccel
->next
= NULL
;
3418 while(*p
|| avpriv_atomic_ptr_cas((void * volatile *)p
, NULL
, hwaccel
))
3420 last_hwaccel
= &hwaccel
->next
;
3423 AVHWAccel
*av_hwaccel_next(const AVHWAccel
*hwaccel
)
3425 return hwaccel
? hwaccel
->next
: first_hwaccel
;
3428 int av_lockmgr_register(int (*cb
)(void **mutex
, enum AVLockOp op
))
3431 if (lockmgr_cb(&codec_mutex
, AV_LOCK_DESTROY
))
3433 if (lockmgr_cb(&avformat_mutex
, AV_LOCK_DESTROY
))
3440 if (lockmgr_cb(&codec_mutex
, AV_LOCK_CREATE
))
3442 if (lockmgr_cb(&avformat_mutex
, AV_LOCK_CREATE
))
3448 int ff_lock_avcodec(AVCodecContext
*log_ctx
)
3451 if ((*lockmgr_cb
)(&codec_mutex
, AV_LOCK_OBTAIN
))
3454 entangled_thread_counter
++;
3455 if (entangled_thread_counter
!= 1) {
3456 av_log(log_ctx
, AV_LOG_ERROR
, "Insufficient thread locking around avcodec_open/close()\n");
3458 av_log(log_ctx
, AV_LOG_ERROR
, "No lock manager is set, please see av_lockmgr_register()\n");
3459 ff_avcodec_locked
= 1;
3460 ff_unlock_avcodec();
3461 return AVERROR(EINVAL
);
3463 av_assert0(!ff_avcodec_locked
);
3464 ff_avcodec_locked
= 1;
3468 int ff_unlock_avcodec(void)
3470 av_assert0(ff_avcodec_locked
);
3471 ff_avcodec_locked
= 0;
3472 entangled_thread_counter
--;
3474 if ((*lockmgr_cb
)(&codec_mutex
, AV_LOCK_RELEASE
))
3480 int avpriv_lock_avformat(void)
3483 if ((*lockmgr_cb
)(&avformat_mutex
, AV_LOCK_OBTAIN
))
3489 int avpriv_unlock_avformat(void)
3492 if ((*lockmgr_cb
)(&avformat_mutex
, AV_LOCK_RELEASE
))
3498 unsigned int avpriv_toupper4(unsigned int x
)
3500 return av_toupper(x
& 0xFF) +
3501 (av_toupper((x
>> 8) & 0xFF) << 8) +
3502 (av_toupper((x
>> 16) & 0xFF) << 16) +
3503 ((unsigned)av_toupper((x
>> 24) & 0xFF) << 24);
3506 int ff_thread_ref_frame(ThreadFrame
*dst
, ThreadFrame
*src
)
3510 dst
->owner
= src
->owner
;
3512 ret
= av_frame_ref(dst
->f
, src
->f
);
3516 if (src
->progress
&&
3517 !(dst
->progress
= av_buffer_ref(src
->progress
))) {
3518 ff_thread_release_buffer(dst
->owner
, dst
);
3519 return AVERROR(ENOMEM
);
3527 enum AVPixelFormat
ff_thread_get_format(AVCodecContext
*avctx
, const enum AVPixelFormat
*fmt
)
3529 return ff_get_format(avctx
, fmt
);
3532 int ff_thread_get_buffer(AVCodecContext
*avctx
, ThreadFrame
*f
, int flags
)
3535 return ff_get_buffer(avctx
, f
->f
, flags
);
3538 void ff_thread_release_buffer(AVCodecContext
*avctx
, ThreadFrame
*f
)
3541 av_frame_unref(f
->f
);
3544 void ff_thread_finish_setup(AVCodecContext
*avctx
)
3548 void ff_thread_report_progress(ThreadFrame
*f
, int progress
, int field
)
3552 void ff_thread_await_progress(ThreadFrame
*f
, int progress
, int field
)
3556 int ff_thread_can_start_frame(AVCodecContext
*avctx
)
3561 int ff_alloc_entries(AVCodecContext
*avctx
, int count
)
3566 void ff_reset_entries(AVCodecContext
*avctx
)
3570 void ff_thread_await_progress2(AVCodecContext
*avctx
, int field
, int thread
, int shift
)
3574 void ff_thread_report_progress2(AVCodecContext
*avctx
, int field
, int thread
, int n
)
3580 enum AVMediaType
avcodec_get_type(enum AVCodecID codec_id
)
3582 AVCodec
*c
= avcodec_find_decoder(codec_id
);
3584 c
= avcodec_find_encoder(codec_id
);
3588 if (codec_id
<= AV_CODEC_ID_NONE
)
3589 return AVMEDIA_TYPE_UNKNOWN
;
3590 else if (codec_id
< AV_CODEC_ID_FIRST_AUDIO
)
3591 return AVMEDIA_TYPE_VIDEO
;
3592 else if (codec_id
< AV_CODEC_ID_FIRST_SUBTITLE
)
3593 return AVMEDIA_TYPE_AUDIO
;
3594 else if (codec_id
< AV_CODEC_ID_FIRST_UNKNOWN
)
3595 return AVMEDIA_TYPE_SUBTITLE
;
3597 return AVMEDIA_TYPE_UNKNOWN
;
3600 int avcodec_is_open(AVCodecContext
*s
)
3602 return !!s
->internal
;
3605 int avpriv_bprint_to_extradata(AVCodecContext
*avctx
, struct AVBPrint
*buf
)
3610 ret
= av_bprint_finalize(buf
, &str
);
3613 avctx
->extradata
= str
;
3614 /* Note: the string is NUL terminated (so extradata can be read as a
3615 * string), but the ending character is not accounted in the size (in
3616 * binary formats you are likely not supposed to mux that character). When
3617 * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
3619 avctx
->extradata_size
= buf
->len
;
3623 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p
,
3625 uint32_t *av_restrict state
)
3629 av_assert0(p
<= end
);
3633 for (i
= 0; i
< 3; i
++) {
3634 uint32_t tmp
= *state
<< 8;
3635 *state
= tmp
+ *(p
++);
3636 if (tmp
== 0x100 || p
== end
)
3641 if (p
[-1] > 1 ) p
+= 3;
3642 else if (p
[-2] ) p
+= 2;
3643 else if (p
[-3]|(p
[-1]-1)) p
++;
3650 p
= FFMIN(p
, end
) - 4;
3651 *state
= AV_RB32(p
);