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
28 #include "libavutil/avassert.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/crc.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/audioconvert.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/samplefmt.h"
36 #include "libavutil/dict.h"
37 #include "libavutil/avassert.h"
40 #include "libavutil/opt.h"
41 #include "imgconvert.h"
43 #include "frame_thread_encoder.h"
44 #include "audioconvert.h"
46 #include "bytestream.h"
52 static int volatile entangled_thread_counter=0;
53 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
54 static void *codec_mutex;
55 static void *avformat_mutex;
57 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
62 min_size= FFMAX(17*min_size/16 + 32, min_size);
64 ptr= av_realloc(ptr, min_size);
65 if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
73 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
78 min_size= FFMAX(17*min_size/16 + 32, min_size);
80 *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
81 if (!*p) min_size = 0;
86 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
88 ff_fast_malloc(ptr, size, min_size, 0);
91 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
94 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
99 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
100 memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
103 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
106 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
111 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
112 memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
115 /* encoder management */
116 static AVCodec *first_avcodec = NULL;
118 AVCodec *av_codec_next(AVCodec *c){
119 if(c) return c->next;
120 else return first_avcodec;
123 static void avcodec_init(void)
125 static int initialized = 0;
127 if (initialized != 0)
131 ff_dsputil_static_init();
134 int av_codec_is_encoder(AVCodec *codec)
136 return codec && (codec->encode || codec->encode2);
139 int av_codec_is_decoder(AVCodec *codec)
141 return codec && codec->decode;
144 void avcodec_register(AVCodec *codec)
149 while (*p != NULL) p = &(*p)->next;
153 if (codec->init_static_data)
154 codec->init_static_data(codec);
157 unsigned avcodec_get_edge_width(void)
162 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
163 s->coded_width = width;
164 s->coded_height= height;
165 s->width = -((-width )>>s->lowres);
166 s->height= -((-height)>>s->lowres);
169 #define INTERNAL_BUFFER_SIZE (32+1)
171 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
172 int linesize_align[AV_NUM_DATA_POINTERS])
179 case PIX_FMT_YUV420P:
180 case PIX_FMT_YUYV422:
181 case PIX_FMT_UYVY422:
182 case PIX_FMT_YUV422P:
183 case PIX_FMT_YUV440P:
184 case PIX_FMT_YUV444P:
187 case PIX_FMT_GRAY16BE:
188 case PIX_FMT_GRAY16LE:
189 case PIX_FMT_YUVJ420P:
190 case PIX_FMT_YUVJ422P:
191 case PIX_FMT_YUVJ440P:
192 case PIX_FMT_YUVJ444P:
193 case PIX_FMT_YUVA420P:
194 case PIX_FMT_YUVA422P:
195 case PIX_FMT_YUVA444P:
196 case PIX_FMT_YUV420P9LE:
197 case PIX_FMT_YUV420P9BE:
198 case PIX_FMT_YUV420P10LE:
199 case PIX_FMT_YUV420P10BE:
200 case PIX_FMT_YUV420P12LE:
201 case PIX_FMT_YUV420P12BE:
202 case PIX_FMT_YUV420P14LE:
203 case PIX_FMT_YUV420P14BE:
204 case PIX_FMT_YUV422P9LE:
205 case PIX_FMT_YUV422P9BE:
206 case PIX_FMT_YUV422P10LE:
207 case PIX_FMT_YUV422P10BE:
208 case PIX_FMT_YUV422P12LE:
209 case PIX_FMT_YUV422P12BE:
210 case PIX_FMT_YUV422P14LE:
211 case PIX_FMT_YUV422P14BE:
212 case PIX_FMT_YUV444P9LE:
213 case PIX_FMT_YUV444P9BE:
214 case PIX_FMT_YUV444P10LE:
215 case PIX_FMT_YUV444P10BE:
216 case PIX_FMT_YUV444P12LE:
217 case PIX_FMT_YUV444P12BE:
218 case PIX_FMT_YUV444P14LE:
219 case PIX_FMT_YUV444P14BE:
220 case PIX_FMT_GBRP9LE:
221 case PIX_FMT_GBRP9BE:
222 case PIX_FMT_GBRP10LE:
223 case PIX_FMT_GBRP10BE:
224 case PIX_FMT_GBRP12LE:
225 case PIX_FMT_GBRP12BE:
226 case PIX_FMT_GBRP14LE:
227 case PIX_FMT_GBRP14BE:
228 w_align = 16; //FIXME assume 16 pixel per macroblock
229 h_align = 16 * 2; // interlaced needs 2 macroblocks height
231 case PIX_FMT_YUV411P:
232 case PIX_FMT_UYYVYY411:
236 case PIX_FMT_YUV410P:
237 if(s->codec_id == CODEC_ID_SVQ1){
242 if(s->codec_id == CODEC_ID_RPZA){
249 if(s->codec_id == CODEC_ID_SMC){
255 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
266 if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
267 w_align= FFMAX(w_align, 8);
270 *width = FFALIGN(*width , w_align);
271 *height= FFALIGN(*height, h_align);
272 if(s->codec_id == CODEC_ID_H264 || s->lowres)
273 *height+=2; // some of the optimized chroma MC reads one line too much
274 // which is also done in mpeg decoders with lowres > 0
276 for (i = 0; i < 4; i++)
277 linesize_align[i] = STRIDE_ALIGN;
280 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
281 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
282 int linesize_align[AV_NUM_DATA_POINTERS];
284 avcodec_align_dimensions2(s, width, height, linesize_align);
285 align = FFMAX(linesize_align[0], linesize_align[3]);
286 linesize_align[1] <<= chroma_shift;
287 linesize_align[2] <<= chroma_shift;
288 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
289 *width=FFALIGN(*width, align);
292 void ff_init_buffer_info(AVCodecContext *s, AVFrame *frame)
295 frame->pkt_pts = s->pkt->pts;
296 frame->pkt_pos = s->pkt->pos;
297 frame->pkt_duration = s->pkt->duration;
299 frame->pkt_pts = AV_NOPTS_VALUE;
301 frame->pkt_duration = 0;
303 frame->reordered_opaque = s->reordered_opaque;
305 switch (s->codec->type) {
306 case AVMEDIA_TYPE_VIDEO:
307 frame->width = s->width;
308 frame->height = s->height;
309 frame->format = s->pix_fmt;
310 frame->sample_aspect_ratio = s->sample_aspect_ratio;
312 case AVMEDIA_TYPE_AUDIO:
313 frame->sample_rate = s->sample_rate;
314 frame->format = s->sample_fmt;
315 frame->channel_layout = s->channel_layout;
316 frame->channels = s->channels;
321 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
322 enum AVSampleFormat sample_fmt, const uint8_t *buf,
323 int buf_size, int align)
325 int ch, planar, needed_size, ret = 0;
327 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
328 frame->nb_samples, sample_fmt,
330 if (buf_size < needed_size)
331 return AVERROR(EINVAL);
333 planar = av_sample_fmt_is_planar(sample_fmt);
334 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
335 if (!(frame->extended_data = av_mallocz(nb_channels *
336 sizeof(*frame->extended_data))))
337 return AVERROR(ENOMEM);
339 frame->extended_data = frame->data;
342 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
343 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
344 sample_fmt, align)) < 0) {
345 if (frame->extended_data != frame->data)
346 av_freep(&frame->extended_data);
349 if (frame->extended_data != frame->data) {
350 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
351 frame->data[ch] = frame->extended_data[ch];
357 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
359 AVCodecInternal *avci = avctx->internal;
363 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
364 frame->nb_samples, avctx->sample_fmt,
367 return AVERROR(EINVAL);
369 /* allocate InternalBuffer if needed */
371 avci->buffer = av_mallocz(sizeof(InternalBuffer));
373 return AVERROR(ENOMEM);
377 /* if there is a previously-used internal buffer, check its size and
378 channel count to see if we can reuse it */
379 if (buf->extended_data) {
380 /* if current buffer is too small, free it */
381 if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
382 av_free(buf->extended_data[0]);
383 if (buf->extended_data != buf->data)
384 av_freep(&buf->extended_data);
385 buf->extended_data = NULL;
388 /* if number of channels has changed, reset and/or free extended data
389 pointers but leave data buffer in buf->data[0] for reuse */
390 if (buf->nb_channels != avctx->channels) {
391 if (buf->extended_data != buf->data)
392 av_free(buf->extended_data);
393 buf->extended_data = NULL;
397 /* if there is no previous buffer or the previous buffer cannot be used
398 as-is, allocate a new buffer and/or rearrange the channel pointers */
399 if (!buf->extended_data) {
401 if (!(buf->data[0] = av_mallocz(buf_size)))
402 return AVERROR(ENOMEM);
403 buf->audio_data_size = buf_size;
405 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
406 avctx->sample_fmt, buf->data[0],
407 buf->audio_data_size, 0)))
410 if (frame->extended_data == frame->data)
411 buf->extended_data = buf->data;
413 buf->extended_data = frame->extended_data;
414 memcpy(buf->data, frame->data, sizeof(frame->data));
415 buf->linesize[0] = frame->linesize[0];
416 buf->nb_channels = avctx->channels;
418 /* copy InternalBuffer info to the AVFrame */
419 frame->extended_data = buf->extended_data;
420 frame->linesize[0] = buf->linesize[0];
421 memcpy(frame->data, buf->data, sizeof(frame->data));
424 frame->type = FF_BUFFER_TYPE_INTERNAL;
425 ff_init_buffer_info(avctx, frame);
427 if (avctx->debug & FF_DEBUG_BUFFERS)
428 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
429 "internal audio buffer used\n", frame);
434 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
440 AVCodecInternal *avci = s->internal;
442 if(pic->data[0]!=NULL) {
443 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
446 if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
447 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
451 if(av_image_check_size(w, h, 0, s) || s->pix_fmt<0) {
452 av_log(s, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
457 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
458 sizeof(InternalBuffer));
461 buf = &avci->buffer[avci->buffer_count];
463 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
464 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
465 av_freep(&buf->base[i]);
471 int h_chroma_shift, v_chroma_shift;
476 int stride_align[AV_NUM_DATA_POINTERS];
477 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
479 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
481 avcodec_align_dimensions2(s, &w, &h, stride_align);
483 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
489 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
490 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
491 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
492 // increase alignment of w for next try (rhs gives the lowest bit set in w)
497 unaligned |= picture.linesize[i] % stride_align[i];
501 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
505 for (i=0; i<3 && picture.data[i+1]; i++)
506 size[i] = picture.data[i+1] - picture.data[i];
507 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
509 memset(buf->base, 0, sizeof(buf->base));
510 memset(buf->data, 0, sizeof(buf->data));
512 for(i=0; i<4 && size[i]; i++){
513 const int h_shift= i==0 ? 0 : h_chroma_shift;
514 const int v_shift= i==0 ? 0 : v_chroma_shift;
516 buf->linesize[i]= picture.linesize[i];
518 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
519 if(buf->base[i]==NULL)
520 return AVERROR(ENOMEM);
521 memset(buf->base[i], 128, size[i]);
523 // no edge if EDGE EMU or not planar YUV
524 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
525 buf->data[i] = buf->base[i];
527 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
529 for (; i < AV_NUM_DATA_POINTERS; i++) {
530 buf->base[i] = buf->data[i] = NULL;
531 buf->linesize[i] = 0;
533 if(size[1] && !size[2])
534 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
535 buf->width = s->width;
536 buf->height = s->height;
537 buf->pix_fmt= s->pix_fmt;
539 pic->type= FF_BUFFER_TYPE_INTERNAL;
541 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
542 pic->base[i]= buf->base[i];
543 pic->data[i]= buf->data[i];
544 pic->linesize[i]= buf->linesize[i];
546 pic->extended_data = pic->data;
547 avci->buffer_count++;
548 pic->width = buf->width;
549 pic->height = buf->height;
550 pic->format = buf->pix_fmt;
552 ff_init_buffer_info(s, pic);
554 if(s->debug&FF_DEBUG_BUFFERS)
555 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
556 "buffers used\n", pic, avci->buffer_count);
561 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
563 switch (avctx->codec_type) {
564 case AVMEDIA_TYPE_VIDEO:
565 return video_get_buffer(avctx, frame);
566 case AVMEDIA_TYPE_AUDIO:
567 return audio_get_buffer(avctx, frame);
573 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
575 InternalBuffer *buf, *last;
576 AVCodecInternal *avci = s->internal;
578 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
580 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
581 assert(avci->buffer_count);
584 buf = NULL; /* avoids warning */
585 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
586 buf = &avci->buffer[i];
587 if (buf->data[0] == pic->data[0])
590 av_assert0(i < avci->buffer_count);
591 avci->buffer_count--;
592 last = &avci->buffer[avci->buffer_count];
595 FFSWAP(InternalBuffer, *buf, *last);
598 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
600 // pic->base[i]=NULL;
602 //printf("R%X\n", pic->opaque);
604 if(s->debug&FF_DEBUG_BUFFERS)
605 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
606 "buffers used\n", pic, avci->buffer_count);
609 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
613 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
615 if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
616 av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
617 pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
618 s->release_buffer(s, pic);
621 ff_init_buffer_info(s, pic);
623 /* If no picture return a new buffer */
624 if(pic->data[0] == NULL) {
625 /* We will copy from buffer, so must be readable */
626 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
627 return s->get_buffer(s, pic);
630 assert(s->pix_fmt == pic->format);
632 /* If internal buffer type return the same buffer */
633 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
638 * Not internal type and reget_buffer not overridden, emulate cr buffer
641 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
642 pic->data[i] = pic->base[i] = NULL;
644 /* Allocate new frame */
645 if (s->get_buffer(s, pic))
647 /* Copy image data from old buffer to new buffer */
648 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
650 s->release_buffer(s, &temp_pic); // Release old frame
654 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
657 for(i=0; i<count; i++){
658 int r= func(c, (char*)arg + i*size);
664 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
667 for(i=0; i<count; i++){
668 int r= func(c, arg, i, 0);
674 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
675 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
680 void avcodec_get_frame_defaults(AVFrame *pic){
681 memset(pic, 0, sizeof(AVFrame));
683 pic->pts = pic->pkt_dts = pic->pkt_pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
684 pic->pkt_duration = 0;
687 pic->sample_aspect_ratio = (AVRational){0, 1};
688 pic->format = -1; /* unknown */
691 AVFrame *avcodec_alloc_frame(void){
692 AVFrame *pic= av_malloc(sizeof(AVFrame));
694 if(pic==NULL) return NULL;
696 avcodec_get_frame_defaults(pic);
701 #define MAKE_ACCESSORS(str, name, type, field) \
702 type av_##name##_get_##field(const str *s) { return s->field; } \
703 void av_##name##_set_##field(str *s, type v) { s->field = v; }
705 MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
706 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
707 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
708 MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
709 MAKE_ACCESSORS(AVFrame, frame, int, channels)
710 MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
711 MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
712 MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
714 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
716 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
718 memset(sub, 0, sizeof(*sub));
719 sub->pts = AV_NOPTS_VALUE;
722 static int get_bit_rate(AVCodecContext *ctx)
727 switch(ctx->codec_type) {
728 case AVMEDIA_TYPE_VIDEO:
729 case AVMEDIA_TYPE_DATA:
730 case AVMEDIA_TYPE_SUBTITLE:
731 case AVMEDIA_TYPE_ATTACHMENT:
732 bit_rate = ctx->bit_rate;
734 case AVMEDIA_TYPE_AUDIO:
735 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
736 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
745 #if FF_API_AVCODEC_OPEN
746 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
748 return avcodec_open2(avctx, codec, NULL);
752 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
755 AVDictionary *tmp = NULL;
757 if (avcodec_is_open(avctx))
760 if ((!codec && !avctx->codec)) {
761 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
762 return AVERROR(EINVAL);
764 if ((codec && avctx->codec && codec != avctx->codec)) {
765 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
766 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
767 return AVERROR(EINVAL);
770 codec = avctx->codec;
772 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
773 return AVERROR(EINVAL);
776 av_dict_copy(&tmp, *options, 0);
778 /* If there is a user-supplied mutex locking routine, call it. */
780 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
784 entangled_thread_counter++;
785 if(entangled_thread_counter != 1){
786 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
791 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
792 if (!avctx->internal) {
793 ret = AVERROR(ENOMEM);
797 if (codec->priv_data_size > 0) {
798 if(!avctx->priv_data){
799 avctx->priv_data = av_mallocz(codec->priv_data_size);
800 if (!avctx->priv_data) {
801 ret = AVERROR(ENOMEM);
804 if (codec->priv_class) {
805 *(const AVClass**)avctx->priv_data= codec->priv_class;
806 av_opt_set_defaults(avctx->priv_data);
809 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
812 avctx->priv_data = NULL;
814 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
817 if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
818 if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
819 av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, try -strict -2\n");
824 //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
825 if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
826 if(avctx->coded_width && avctx->coded_height)
827 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
828 else if(avctx->width && avctx->height)
829 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
832 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
833 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
834 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
835 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
836 avcodec_set_dimensions(avctx, 0, 0);
839 /* if the decoder init function was already called previously,
840 free the already allocated subtitle_header before overwriting it */
841 if (av_codec_is_decoder(codec))
842 av_freep(&avctx->subtitle_header);
844 #define SANE_NB_CHANNELS 128U
845 if (avctx->channels > SANE_NB_CHANNELS) {
846 ret = AVERROR(EINVAL);
850 avctx->codec = codec;
851 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
852 avctx->codec_id == CODEC_ID_NONE) {
853 avctx->codec_type = codec->type;
854 avctx->codec_id = codec->id;
856 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
857 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
858 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
859 ret = AVERROR(EINVAL);
862 avctx->frame_number = 0;
864 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
865 (!avctx->time_base.num || !avctx->time_base.den)) {
866 avctx->time_base.num = 1;
867 avctx->time_base.den = avctx->sample_rate;
871 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
873 entangled_thread_counter--; //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
874 ret = ff_frame_thread_encoder_init(avctx);
875 entangled_thread_counter++;
879 if (HAVE_THREADS && !avctx->thread_opaque
880 && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
881 ret = ff_thread_init(avctx);
886 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
887 avctx->thread_count = 1;
889 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
890 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
891 avctx->codec->max_lowres);
892 ret = AVERROR(EINVAL);
896 if (av_codec_is_encoder(avctx->codec)) {
898 if (avctx->codec->sample_fmts) {
899 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
900 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
902 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
903 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
904 ret = AVERROR(EINVAL);
908 if (avctx->codec->pix_fmts) {
909 for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
910 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
912 if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE
913 && !((avctx->codec_id == CODEC_ID_MJPEG || avctx->codec_id == CODEC_ID_LJPEG)
914 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
915 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
916 ret = AVERROR(EINVAL);
920 if (avctx->codec->supported_samplerates) {
921 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
922 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
924 if (avctx->codec->supported_samplerates[i] == 0) {
925 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
926 ret = AVERROR(EINVAL);
930 if (avctx->codec->channel_layouts) {
931 if (!avctx->channel_layout) {
932 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
934 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
935 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
937 if (avctx->codec->channel_layouts[i] == 0) {
938 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
939 ret = AVERROR(EINVAL);
944 if (avctx->channel_layout && avctx->channels) {
945 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
946 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
947 ret = AVERROR(EINVAL);
950 } else if (avctx->channel_layout) {
951 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
955 avctx->pts_correction_num_faulty_pts =
956 avctx->pts_correction_num_faulty_dts = 0;
957 avctx->pts_correction_last_pts =
958 avctx->pts_correction_last_dts = INT64_MIN;
960 if(avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME) || avctx->internal->frame_thread_encoder)){
961 ret = avctx->codec->init(avctx);
969 if (av_codec_is_decoder(avctx->codec)) {
970 if (!avctx->bit_rate)
971 avctx->bit_rate = get_bit_rate(avctx);
972 /* validate channel layout from the decoder */
973 if (avctx->channel_layout &&
974 av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
975 av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n");
976 avctx->channel_layout = 0;
980 entangled_thread_counter--;
982 /* Release any user-supplied mutex. */
984 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
987 av_dict_free(options);
994 av_freep(&avctx->priv_data);
995 av_freep(&avctx->internal);
1000 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
1002 if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1003 av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
1004 return AVERROR(EINVAL);
1008 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1009 if (!avpkt->data || avpkt->size < size) {
1010 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
1011 avpkt->data = avctx->internal->byte_buffer;
1012 avpkt->size = avctx->internal->byte_buffer_size;
1013 avpkt->destruct = NULL;
1018 void *destruct = avpkt->destruct;
1020 if (avpkt->size < size) {
1021 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
1022 return AVERROR(EINVAL);
1025 av_init_packet(avpkt);
1026 avpkt->destruct = destruct;
1030 int ret = av_new_packet(avpkt, size);
1032 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
1037 int ff_alloc_packet(AVPacket *avpkt, int size)
1039 return ff_alloc_packet2(NULL, avpkt, size);
1043 * Pad last frame with silence.
1045 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1047 AVFrame *frame = NULL;
1048 uint8_t *buf = NULL;
1051 if (!(frame = avcodec_alloc_frame()))
1052 return AVERROR(ENOMEM);
1055 if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
1056 s->frame_size, s->sample_fmt, 0)) < 0)
1059 if (!(buf = av_malloc(ret))) {
1060 ret = AVERROR(ENOMEM);
1064 frame->nb_samples = s->frame_size;
1065 if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
1068 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1069 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1071 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1072 frame->nb_samples - src->nb_samples,
1073 s->channels, s->sample_fmt)) < 0)
1081 if (frame->extended_data != frame->data)
1082 av_freep(&frame->extended_data);
1088 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1090 const AVFrame *frame,
1091 int *got_packet_ptr)
1094 AVFrame *padded_frame = NULL;
1096 AVPacket user_pkt = *avpkt;
1097 int needs_realloc = !user_pkt.data;
1099 *got_packet_ptr = 0;
1101 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1102 av_free_packet(avpkt);
1103 av_init_packet(avpkt);
1107 /* ensure that extended_data is properly set */
1108 if (frame && !frame->extended_data) {
1109 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1110 avctx->channels > AV_NUM_DATA_POINTERS) {
1111 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1112 "with more than %d channels, but extended_data is not set.\n",
1113 AV_NUM_DATA_POINTERS);
1114 return AVERROR(EINVAL);
1116 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1119 tmp.extended_data = tmp.data;
1123 /* check for valid frame size */
1125 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1126 if (frame->nb_samples > avctx->frame_size) {
1127 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1128 return AVERROR(EINVAL);
1130 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1131 if (frame->nb_samples < avctx->frame_size &&
1132 !avctx->internal->last_audio_frame) {
1133 ret = pad_last_frame(avctx, &padded_frame, frame);
1137 frame = padded_frame;
1138 avctx->internal->last_audio_frame = 1;
1141 if (frame->nb_samples != avctx->frame_size) {
1142 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1143 return AVERROR(EINVAL);
1148 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1150 if (*got_packet_ptr) {
1151 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1152 if (avpkt->pts == AV_NOPTS_VALUE)
1153 avpkt->pts = frame->pts;
1154 if (!avpkt->duration)
1155 avpkt->duration = ff_samples_to_time_base(avctx,
1158 avpkt->dts = avpkt->pts;
1163 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1165 if (user_pkt.data) {
1166 if (user_pkt.size >= avpkt->size) {
1167 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1169 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1170 avpkt->size = user_pkt.size;
1173 avpkt->data = user_pkt.data;
1174 avpkt->destruct = user_pkt.destruct;
1176 if (av_dup_packet(avpkt) < 0) {
1177 ret = AVERROR(ENOMEM);
1183 if (needs_realloc && avpkt->data) {
1184 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1186 avpkt->data = new_data;
1189 avctx->frame_number++;
1192 if (ret < 0 || !*got_packet_ptr) {
1193 av_free_packet(avpkt);
1194 av_init_packet(avpkt);
1198 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1199 this needs to be moved to the encoders, but for now we can do it
1200 here to simplify things */
1201 avpkt->flags |= AV_PKT_FLAG_KEY;
1204 av_freep(&padded_frame->data[0]);
1205 if (padded_frame->extended_data != padded_frame->data)
1206 av_freep(&padded_frame->extended_data);
1207 av_freep(&padded_frame);
1213 #if FF_API_OLD_DECODE_AUDIO
1214 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1215 uint8_t *buf, int buf_size,
1216 const short *samples)
1221 int ret, samples_size, got_packet;
1223 av_init_packet(&pkt);
1225 pkt.size = buf_size;
1229 avcodec_get_frame_defaults(frame);
1231 if (avctx->frame_size) {
1232 frame->nb_samples = avctx->frame_size;
1234 /* if frame_size is not set, the number of samples must be
1235 calculated from the buffer size */
1237 if (!av_get_bits_per_sample(avctx->codec_id)) {
1238 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1239 "support this codec\n");
1240 return AVERROR(EINVAL);
1242 nb_samples = (int64_t)buf_size * 8 /
1243 (av_get_bits_per_sample(avctx->codec_id) *
1245 if (nb_samples >= INT_MAX)
1246 return AVERROR(EINVAL);
1247 frame->nb_samples = nb_samples;
1250 /* it is assumed that the samples buffer is large enough based on the
1251 relevant parameters */
1252 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1254 avctx->sample_fmt, 1);
1255 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1257 (const uint8_t *) samples,
1261 /* fabricate frame pts from sample count.
1262 this is needed because the avcodec_encode_audio() API does not have
1263 a way for the user to provide pts */
1264 if(avctx->sample_rate && avctx->time_base.num)
1265 frame->pts = ff_samples_to_time_base(avctx,
1266 avctx->internal->sample_count);
1268 frame->pts = AV_NOPTS_VALUE;
1269 avctx->internal->sample_count += frame->nb_samples;
1275 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1276 if (!ret && got_packet && avctx->coded_frame) {
1277 avctx->coded_frame->pts = pkt.pts;
1278 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1280 /* free any side data since we cannot return it */
1281 ff_packet_free_side_data(&pkt);
1283 if (frame && frame->extended_data != frame->data)
1284 av_freep(&frame->extended_data);
1286 return ret ? ret : pkt.size;
1290 #if FF_API_OLD_ENCODE_VIDEO
1291 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1292 const AVFrame *pict)
1295 int ret, got_packet = 0;
1297 if(buf_size < FF_MIN_BUFFER_SIZE){
1298 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1302 av_init_packet(&pkt);
1304 pkt.size = buf_size;
1306 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1307 if (!ret && got_packet && avctx->coded_frame) {
1308 avctx->coded_frame->pts = pkt.pts;
1309 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1312 /* free any side data since we cannot return it */
1313 if (pkt.side_data_elems > 0) {
1315 for (i = 0; i < pkt.side_data_elems; i++)
1316 av_free(pkt.side_data[i].data);
1317 av_freep(&pkt.side_data);
1318 pkt.side_data_elems = 0;
1321 return ret ? ret : pkt.size;
1325 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1327 const AVFrame *frame,
1328 int *got_packet_ptr)
1331 AVPacket user_pkt = *avpkt;
1332 int needs_realloc = !user_pkt.data;
1334 *got_packet_ptr = 0;
1336 if(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
1337 return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1339 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1340 av_free_packet(avpkt);
1341 av_init_packet(avpkt);
1346 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1347 return AVERROR(EINVAL);
1349 av_assert0(avctx->codec->encode2);
1351 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1352 av_assert0(ret <= 0);
1354 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1356 if (user_pkt.data) {
1357 if (user_pkt.size >= avpkt->size) {
1358 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1360 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1361 avpkt->size = user_pkt.size;
1364 avpkt->data = user_pkt.data;
1365 avpkt->destruct = user_pkt.destruct;
1367 if (av_dup_packet(avpkt) < 0) {
1368 ret = AVERROR(ENOMEM);
1374 if (!*got_packet_ptr)
1376 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1377 avpkt->pts = avpkt->dts = frame->pts;
1379 if (needs_realloc && avpkt->data &&
1380 avpkt->destruct == av_destruct_packet) {
1381 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1383 avpkt->data = new_data;
1386 avctx->frame_number++;
1389 if (ret < 0 || !*got_packet_ptr)
1390 av_free_packet(avpkt);
1396 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1397 const AVSubtitle *sub)
1400 if(sub->start_display_time) {
1401 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1405 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)(intptr_t)sub);
1406 avctx->frame_number++;
1411 * Attempt to guess proper monotonic timestamps for decoded video frames
1412 * which might have incorrect times. Input timestamps may wrap around, in
1413 * which case the output will as well.
1415 * @param pts the pts field of the decoded AVPacket, as passed through
1417 * @param dts the dts field of the decoded AVPacket
1418 * @return one of the input values, may be AV_NOPTS_VALUE
1420 static int64_t guess_correct_pts(AVCodecContext *ctx,
1421 int64_t reordered_pts, int64_t dts)
1423 int64_t pts = AV_NOPTS_VALUE;
1425 if (dts != AV_NOPTS_VALUE) {
1426 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1427 ctx->pts_correction_last_dts = dts;
1429 if (reordered_pts != AV_NOPTS_VALUE) {
1430 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1431 ctx->pts_correction_last_pts = reordered_pts;
1433 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1434 && reordered_pts != AV_NOPTS_VALUE)
1435 pts = reordered_pts;
1442 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1445 const uint8_t *data;
1448 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1451 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1452 if (!data || size < 4)
1454 flags = bytestream_get_le32(&data);
1456 if (size < 4) /* Required for any of the changes */
1458 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1459 avctx->channels = bytestream_get_le32(&data);
1462 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1465 avctx->channel_layout = bytestream_get_le64(&data);
1470 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1471 avctx->sample_rate = bytestream_get_le32(&data);
1474 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1477 avctx->width = bytestream_get_le32(&data);
1478 avctx->height = bytestream_get_le32(&data);
1479 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1484 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1485 int *got_picture_ptr,
1486 const AVPacket *avpkt)
1489 // copy to ensure we do not change avpkt
1490 AVPacket tmp = *avpkt;
1492 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
1493 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
1494 return AVERROR(EINVAL);
1497 *got_picture_ptr= 0;
1498 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1499 return AVERROR(EINVAL);
1501 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
1502 int did_split = av_packet_split_side_data(&tmp);
1503 apply_param_change(avctx, &tmp);
1505 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1506 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1509 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1511 picture->pkt_dts= avpkt->dts;
1513 if(!avctx->has_b_frames){
1514 picture->pkt_pos= avpkt->pos;
1516 //FIXME these should be under if(!avctx->has_b_frames)
1517 if (!picture->sample_aspect_ratio.num)
1518 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1519 if (!picture->width)
1520 picture->width = avctx->width;
1521 if (!picture->height)
1522 picture->height = avctx->height;
1523 if (picture->format == PIX_FMT_NONE)
1524 picture->format = avctx->pix_fmt;
1527 emms_c(); //needed to avoid an emms_c() call before every return;
1531 ff_packet_free_side_data(&tmp);
1536 if (*got_picture_ptr){
1537 avctx->frame_number++;
1538 picture->best_effort_timestamp = guess_correct_pts(avctx,
1548 #if FF_API_OLD_DECODE_AUDIO
1549 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1550 int *frame_size_ptr,
1554 int ret, got_frame = 0;
1556 if (avctx->get_buffer != avcodec_default_get_buffer) {
1557 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1558 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1559 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1560 "avcodec_decode_audio4()\n");
1561 avctx->get_buffer = avcodec_default_get_buffer;
1562 avctx->release_buffer = avcodec_default_release_buffer;
1565 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1567 if (ret >= 0 && got_frame) {
1569 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1570 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1572 avctx->sample_fmt, 1);
1573 if (*frame_size_ptr < data_size) {
1574 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1575 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1576 return AVERROR(EINVAL);
1579 memcpy(samples, frame.extended_data[0], plane_size);
1581 if (planar && avctx->channels > 1) {
1582 uint8_t *out = ((uint8_t *)samples) + plane_size;
1583 for (ch = 1; ch < avctx->channels; ch++) {
1584 memcpy(out, frame.extended_data[ch], plane_size);
1588 *frame_size_ptr = data_size;
1590 *frame_size_ptr = 0;
1596 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1599 const AVPacket *avpkt)
1605 if (!avpkt->data && avpkt->size) {
1606 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1607 return AVERROR(EINVAL);
1609 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
1610 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
1611 return AVERROR(EINVAL);
1614 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1617 // copy to ensure we do not change avpkt
1618 AVPacket tmp = *avpkt;
1619 int did_split = av_packet_split_side_data(&tmp);
1620 apply_param_change(avctx, &tmp);
1623 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
1624 if (ret >= 0 && *got_frame_ptr) {
1625 avctx->frame_number++;
1626 frame->pkt_dts = avpkt->dts;
1627 frame->best_effort_timestamp = guess_correct_pts(avctx,
1630 if (frame->format == AV_SAMPLE_FMT_NONE)
1631 frame->format = avctx->sample_fmt;
1632 if (!frame->channel_layout)
1633 frame->channel_layout = avctx->channel_layout;
1634 if (!frame->channels)
1635 frame->channels = avctx->channels;
1636 if (!frame->sample_rate)
1637 frame->sample_rate = avctx->sample_rate;
1640 side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
1641 if(side && side_size>=10) {
1642 avctx->internal->skip_samples = AV_RL32(side);
1643 av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
1644 avctx->internal->skip_samples);
1646 if (avctx->internal->skip_samples) {
1647 if(frame->nb_samples <= avctx->internal->skip_samples){
1649 avctx->internal->skip_samples -= frame->nb_samples;
1650 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
1651 avctx->internal->skip_samples);
1653 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
1654 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
1655 if(avctx->pkt_timebase.num && avctx->sample_rate) {
1656 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
1657 (AVRational){1, avctx->sample_rate},
1658 avctx->pkt_timebase);
1659 if(frame->pkt_pts!=AV_NOPTS_VALUE)
1660 frame->pkt_pts += diff_ts;
1661 if(frame->pkt_dts!=AV_NOPTS_VALUE)
1662 frame->pkt_dts += diff_ts;
1663 if (frame->pkt_duration >= diff_ts)
1664 frame->pkt_duration -= diff_ts;
1666 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
1668 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
1669 avctx->internal->skip_samples, frame->nb_samples);
1670 frame->nb_samples -= avctx->internal->skip_samples;
1671 avctx->internal->skip_samples = 0;
1677 ff_packet_free_side_data(&tmp);
1685 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1691 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
1692 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
1693 return AVERROR(EINVAL);
1698 avcodec_get_subtitle_defaults(sub);
1699 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1701 avctx->frame_number++;
1705 void avsubtitle_free(AVSubtitle *sub)
1709 for (i = 0; i < sub->num_rects; i++)
1711 av_freep(&sub->rects[i]->pict.data[0]);
1712 av_freep(&sub->rects[i]->pict.data[1]);
1713 av_freep(&sub->rects[i]->pict.data[2]);
1714 av_freep(&sub->rects[i]->pict.data[3]);
1715 av_freep(&sub->rects[i]->text);
1716 av_freep(&sub->rects[i]->ass);
1717 av_freep(&sub->rects[i]);
1720 av_freep(&sub->rects);
1722 memset(sub, 0, sizeof(AVSubtitle));
1725 av_cold int avcodec_close(AVCodecContext *avctx)
1727 /* If there is a user-supplied mutex locking routine, call it. */
1728 if (ff_lockmgr_cb) {
1729 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1733 entangled_thread_counter++;
1734 if(entangled_thread_counter != 1){
1735 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1736 entangled_thread_counter--;
1740 if (avcodec_is_open(avctx)) {
1741 if (avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
1742 entangled_thread_counter --;
1743 ff_frame_thread_encoder_free(avctx);
1744 entangled_thread_counter ++;
1746 if (HAVE_THREADS && avctx->thread_opaque)
1747 ff_thread_free(avctx);
1748 if (avctx->codec && avctx->codec->close)
1749 avctx->codec->close(avctx);
1750 avcodec_default_free_buffers(avctx);
1751 avctx->coded_frame = NULL;
1752 avctx->internal->byte_buffer_size = 0;
1753 av_freep(&avctx->internal->byte_buffer);
1754 av_freep(&avctx->internal);
1757 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1758 av_opt_free(avctx->priv_data);
1760 av_freep(&avctx->priv_data);
1761 if (av_codec_is_encoder(avctx->codec))
1762 av_freep(&avctx->extradata);
1763 avctx->codec = NULL;
1764 avctx->active_thread_type = 0;
1765 entangled_thread_counter--;
1767 /* Release any user-supplied mutex. */
1768 if (ff_lockmgr_cb) {
1769 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1774 static enum CodecID remap_deprecated_codec_id(enum CodecID id)
1777 //This is for future deprecatec codec ids, its empty since
1778 //last major bump but will fill up again over time, please don't remove it
1779 // case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
1780 default : return id;
1784 AVCodec *avcodec_find_encoder(enum CodecID id)
1786 AVCodec *p, *experimental=NULL;
1788 id= remap_deprecated_codec_id(id);
1790 if (av_codec_is_encoder(p) && p->id == id) {
1791 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1798 return experimental;
1801 AVCodec *avcodec_find_encoder_by_name(const char *name)
1808 if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
1815 AVCodec *avcodec_find_decoder(enum CodecID id)
1817 AVCodec *p, *experimental=NULL;
1819 id= remap_deprecated_codec_id(id);
1821 if (av_codec_is_decoder(p) && p->id == id) {
1822 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1829 return experimental;
1832 AVCodec *avcodec_find_decoder_by_name(const char *name)
1839 if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
1846 const char *avcodec_get_name(enum CodecID id)
1852 #include "libavcodec/codec_names.h"
1854 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
1856 codec = avcodec_find_decoder(id);
1859 codec = avcodec_find_encoder(id);
1862 return "unknown_codec";
1865 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1867 int i, len, ret = 0;
1869 #define IS_PRINT(x) \
1870 (((x) >= '0' && (x) <= '9') || \
1871 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
1872 ((x) == '.' || (x) == ' ' || (x) == '-'))
1874 for (i = 0; i < 4; i++) {
1875 len = snprintf(buf, buf_size,
1876 IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1878 buf_size = buf_size > len ? buf_size - len : 0;
1885 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1887 const char *codec_type;
1888 const char *codec_name;
1889 const char *profile = NULL;
1892 AVRational display_aspect_ratio;
1894 if (!buf || buf_size <= 0)
1896 codec_type = av_get_media_type_string(enc->codec_type);
1897 codec_name = avcodec_get_name(enc->codec_id);
1898 if (enc->profile != FF_PROFILE_UNKNOWN) {
1902 p = encode ? avcodec_find_encoder(enc->codec_id) :
1903 avcodec_find_decoder(enc->codec_id);
1905 profile = av_get_profile_name(p, enc->profile);
1908 snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
1909 codec_name, enc->mb_decision ? " (hq)" : "");
1910 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
1912 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
1913 if (enc->codec_tag) {
1915 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1916 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1917 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
1919 switch(enc->codec_type) {
1920 case AVMEDIA_TYPE_VIDEO:
1921 if (enc->pix_fmt != PIX_FMT_NONE) {
1922 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1924 av_get_pix_fmt_name(enc->pix_fmt));
1927 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1929 enc->width, enc->height);
1930 if (enc->sample_aspect_ratio.num) {
1931 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1932 enc->width*enc->sample_aspect_ratio.num,
1933 enc->height*enc->sample_aspect_ratio.den,
1935 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1936 " [SAR %d:%d DAR %d:%d]",
1937 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1938 display_aspect_ratio.num, display_aspect_ratio.den);
1940 if(av_log_get_level() >= AV_LOG_DEBUG){
1941 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1942 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1944 enc->time_base.num/g, enc->time_base.den/g);
1948 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1949 ", q=%d-%d", enc->qmin, enc->qmax);
1952 case AVMEDIA_TYPE_AUDIO:
1953 if (enc->sample_rate) {
1954 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1955 ", %d Hz", enc->sample_rate);
1957 av_strlcat(buf, ", ", buf_size);
1958 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1959 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1960 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1961 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1968 if (enc->flags & CODEC_FLAG_PASS1)
1969 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1971 if (enc->flags & CODEC_FLAG_PASS2)
1972 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1975 bitrate = get_bit_rate(enc);
1977 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1978 ", %d kb/s", bitrate / 1000);
1982 const char *av_get_profile_name(const AVCodec *codec, int profile)
1985 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1988 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1989 if (p->profile == profile)
1995 unsigned avcodec_version( void )
1997 // av_assert0(CODEC_ID_V410==164);
1998 av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
1999 av_assert0(CODEC_ID_ADPCM_G722==69660);
2000 // av_assert0(CODEC_ID_BMV_AUDIO==86071);
2001 av_assert0(CODEC_ID_SRT==94216);
2002 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
2004 return LIBAVCODEC_VERSION_INT;
2007 const char *avcodec_configuration(void)
2009 return FFMPEG_CONFIGURATION;
2012 const char *avcodec_license(void)
2014 #define LICENSE_PREFIX "libavcodec license: "
2015 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2018 void avcodec_flush_buffers(AVCodecContext *avctx)
2020 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
2021 ff_thread_flush(avctx);
2022 else if(avctx->codec->flush)
2023 avctx->codec->flush(avctx);
2025 avctx->pts_correction_last_pts =
2026 avctx->pts_correction_last_dts = INT64_MIN;
2029 static void video_free_buffers(AVCodecContext *s)
2031 AVCodecInternal *avci = s->internal;
2037 if (avci->buffer_count)
2038 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
2039 avci->buffer_count);
2040 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
2041 InternalBuffer *buf = &avci->buffer[i];
2043 av_freep(&buf->base[j]);
2047 av_freep(&avci->buffer);
2049 avci->buffer_count=0;
2052 static void audio_free_buffers(AVCodecContext *avctx)
2054 AVCodecInternal *avci = avctx->internal;
2055 InternalBuffer *buf;
2061 if (buf->extended_data) {
2062 av_free(buf->extended_data[0]);
2063 if (buf->extended_data != buf->data)
2064 av_freep(&buf->extended_data);
2066 av_freep(&avci->buffer);
2069 void avcodec_default_free_buffers(AVCodecContext *avctx)
2071 switch (avctx->codec_type) {
2072 case AVMEDIA_TYPE_VIDEO:
2073 video_free_buffers(avctx);
2075 case AVMEDIA_TYPE_AUDIO:
2076 audio_free_buffers(avctx);
2083 int av_get_exact_bits_per_sample(enum CodecID codec_id)
2086 case CODEC_ID_ADPCM_CT:
2087 case CODEC_ID_ADPCM_IMA_APC:
2088 case CODEC_ID_ADPCM_IMA_EA_SEAD:
2089 case CODEC_ID_ADPCM_IMA_WS:
2090 case CODEC_ID_ADPCM_G722:
2091 case CODEC_ID_ADPCM_YAMAHA:
2093 case CODEC_ID_PCM_ALAW:
2094 case CODEC_ID_PCM_MULAW:
2095 case CODEC_ID_PCM_S8:
2096 case CODEC_ID_PCM_U8:
2097 case CODEC_ID_PCM_ZORK:
2099 case CODEC_ID_PCM_S16BE:
2100 case CODEC_ID_PCM_S16LE:
2101 case CODEC_ID_PCM_S16LE_PLANAR:
2102 case CODEC_ID_PCM_U16BE:
2103 case CODEC_ID_PCM_U16LE:
2105 case CODEC_ID_PCM_S24DAUD:
2106 case CODEC_ID_PCM_S24BE:
2107 case CODEC_ID_PCM_S24LE:
2108 case CODEC_ID_PCM_U24BE:
2109 case CODEC_ID_PCM_U24LE:
2111 case CODEC_ID_PCM_S32BE:
2112 case CODEC_ID_PCM_S32LE:
2113 case CODEC_ID_PCM_U32BE:
2114 case CODEC_ID_PCM_U32LE:
2115 case CODEC_ID_PCM_F32BE:
2116 case CODEC_ID_PCM_F32LE:
2118 case CODEC_ID_PCM_F64BE:
2119 case CODEC_ID_PCM_F64LE:
2126 enum CodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
2128 static const enum CodecID map[AV_SAMPLE_FMT_NB][2] = {
2129 [AV_SAMPLE_FMT_U8 ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
2130 [AV_SAMPLE_FMT_S16 ] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
2131 [AV_SAMPLE_FMT_S32 ] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
2132 [AV_SAMPLE_FMT_FLT ] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
2133 [AV_SAMPLE_FMT_DBL ] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
2134 [AV_SAMPLE_FMT_U8P ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
2135 [AV_SAMPLE_FMT_S16P] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
2136 [AV_SAMPLE_FMT_S32P] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
2137 [AV_SAMPLE_FMT_FLTP] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
2138 [AV_SAMPLE_FMT_DBLP] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
2140 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
2141 return CODEC_ID_NONE;
2142 if (be < 0 || be > 1)
2144 return map[fmt][be];
2147 int av_get_bits_per_sample(enum CodecID codec_id)
2150 case CODEC_ID_ADPCM_SBPRO_2:
2152 case CODEC_ID_ADPCM_SBPRO_3:
2154 case CODEC_ID_ADPCM_SBPRO_4:
2155 case CODEC_ID_ADPCM_IMA_WAV:
2156 case CODEC_ID_ADPCM_IMA_QT:
2157 case CODEC_ID_ADPCM_SWF:
2158 case CODEC_ID_ADPCM_MS:
2161 return av_get_exact_bits_per_sample(codec_id);
2165 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2167 int id, sr, ch, ba, tag, bps;
2169 id = avctx->codec_id;
2170 sr = avctx->sample_rate;
2171 ch = avctx->channels;
2172 ba = avctx->block_align;
2173 tag = avctx->codec_tag;
2174 bps = av_get_exact_bits_per_sample(avctx->codec_id);
2176 /* codecs with an exact constant bits per sample */
2177 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
2178 return (frame_bytes * 8LL) / (bps * ch);
2179 bps = avctx->bits_per_coded_sample;
2181 /* codecs with a fixed packet duration */
2183 case CODEC_ID_ADPCM_ADX: return 32;
2184 case CODEC_ID_ADPCM_IMA_QT: return 64;
2185 case CODEC_ID_ADPCM_EA_XAS: return 128;
2186 case CODEC_ID_AMR_NB:
2188 case CODEC_ID_QCELP:
2189 case CODEC_ID_RA_144:
2190 case CODEC_ID_RA_288: return 160;
2191 case CODEC_ID_IMC: return 256;
2192 case CODEC_ID_AMR_WB:
2193 case CODEC_ID_GSM_MS: return 320;
2194 case CODEC_ID_MP1: return 384;
2195 case CODEC_ID_ATRAC1: return 512;
2196 case CODEC_ID_ATRAC3: return 1024;
2198 case CODEC_ID_MUSEPACK7: return 1152;
2199 case CODEC_ID_AC3: return 1536;
2203 /* calc from sample rate */
2204 if (id == CODEC_ID_TTA)
2205 return 256 * sr / 245;
2208 /* calc from sample rate and channels */
2209 if (id == CODEC_ID_BINKAUDIO_DCT)
2210 return (480 << (sr / 22050)) / ch;
2215 /* calc from block_align */
2216 if (id == CODEC_ID_SIPR) {
2218 case 20: return 160;
2219 case 19: return 144;
2220 case 29: return 288;
2221 case 37: return 480;
2223 } else if (id == CODEC_ID_ILBC) {
2225 case 38: return 160;
2226 case 50: return 240;
2231 if (frame_bytes > 0) {
2232 /* calc from frame_bytes only */
2233 if (id == CODEC_ID_TRUESPEECH)
2234 return 240 * (frame_bytes / 32);
2235 if (id == CODEC_ID_NELLYMOSER)
2236 return 256 * (frame_bytes / 64);
2239 /* calc from frame_bytes and bits_per_coded_sample */
2240 if (id == CODEC_ID_ADPCM_G726)
2241 return frame_bytes * 8 / bps;
2245 /* calc from frame_bytes and channels */
2247 case CODEC_ID_ADPCM_4XM:
2248 case CODEC_ID_ADPCM_IMA_ISS:
2249 return (frame_bytes - 4 * ch) * 2 / ch;
2250 case CODEC_ID_ADPCM_IMA_SMJPEG:
2251 return (frame_bytes - 4) * 2 / ch;
2252 case CODEC_ID_ADPCM_IMA_AMV:
2253 return (frame_bytes - 8) * 2 / ch;
2254 case CODEC_ID_ADPCM_XA:
2255 return (frame_bytes / 128) * 224 / ch;
2256 case CODEC_ID_INTERPLAY_DPCM:
2257 return (frame_bytes - 6 - ch) / ch;
2258 case CODEC_ID_ROQ_DPCM:
2259 return (frame_bytes - 8) / ch;
2260 case CODEC_ID_XAN_DPCM:
2261 return (frame_bytes - 2 * ch) / ch;
2262 case CODEC_ID_MACE3:
2263 return 3 * frame_bytes / ch;
2264 case CODEC_ID_MACE6:
2265 return 6 * frame_bytes / ch;
2266 case CODEC_ID_PCM_LXF:
2267 return 2 * (frame_bytes / (5 * ch));
2271 /* calc from frame_bytes, channels, and codec_tag */
2272 if (id == CODEC_ID_SOL_DPCM) {
2274 return frame_bytes / ch;
2276 return frame_bytes * 2 / ch;
2281 /* calc from frame_bytes, channels, and block_align */
2282 int blocks = frame_bytes / ba;
2283 switch (avctx->codec_id) {
2284 case CODEC_ID_ADPCM_IMA_WAV:
2285 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2286 case CODEC_ID_ADPCM_IMA_DK3:
2287 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2288 case CODEC_ID_ADPCM_IMA_DK4:
2289 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2290 case CODEC_ID_ADPCM_MS:
2291 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2296 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2297 switch (avctx->codec_id) {
2298 case CODEC_ID_PCM_DVD:
2301 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2302 case CODEC_ID_PCM_BLURAY:
2305 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2306 case CODEC_ID_S302M:
2307 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2317 int ff_thread_init(AVCodecContext *s){
2322 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2336 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
2338 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
2342 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2344 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
2345 "version to the newest one from Git. If the problem still "
2346 "occurs, it means that your file has a feature which has not "
2347 "been implemented.\n", feature);
2349 av_log_ask_for_sample(avc, NULL);
2352 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2354 va_list argument_list;
2356 va_start(argument_list, msg);
2359 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2360 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2361 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
2362 "and contact the ffmpeg-devel mailing list.\n");
2364 va_end(argument_list);
2367 static AVHWAccel *first_hwaccel = NULL;
2369 void av_register_hwaccel(AVHWAccel *hwaccel)
2371 AVHWAccel **p = &first_hwaccel;
2375 hwaccel->next = NULL;
2378 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2380 return hwaccel ? hwaccel->next : first_hwaccel;
2383 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
2385 AVHWAccel *hwaccel=NULL;
2387 while((hwaccel= av_hwaccel_next(hwaccel))){
2388 if ( hwaccel->id == codec_id
2389 && hwaccel->pix_fmt == pix_fmt)
2395 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2397 if (ff_lockmgr_cb) {
2398 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2400 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2406 if (ff_lockmgr_cb) {
2407 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2409 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2415 int avpriv_lock_avformat(void)
2417 if (ff_lockmgr_cb) {
2418 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2424 int avpriv_unlock_avformat(void)
2426 if (ff_lockmgr_cb) {
2427 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2433 unsigned int avpriv_toupper4(unsigned int x)
2435 return toupper( x &0xFF)
2436 + (toupper((x>>8 )&0xFF)<<8 )
2437 + (toupper((x>>16)&0xFF)<<16)
2438 + (toupper((x>>24)&0xFF)<<24);
2443 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
2447 ff_init_buffer_info(avctx, f);
2449 return avctx->get_buffer(avctx, f);
2452 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
2454 f->owner->release_buffer(f->owner, f);
2457 void ff_thread_finish_setup(AVCodecContext *avctx)
2461 void ff_thread_report_progress(AVFrame *f, int progress, int field)
2465 void ff_thread_await_progress(AVFrame *f, int progress, int field)
2469 int ff_thread_can_start_frame(AVCodecContext *avctx)
2476 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
2478 AVCodec *c= avcodec_find_decoder(codec_id);
2480 c= avcodec_find_encoder(codec_id);
2484 if (codec_id <= CODEC_ID_NONE)
2485 return AVMEDIA_TYPE_UNKNOWN;
2486 else if (codec_id < CODEC_ID_FIRST_AUDIO)
2487 return AVMEDIA_TYPE_VIDEO;
2488 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
2489 return AVMEDIA_TYPE_AUDIO;
2490 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
2491 return AVMEDIA_TYPE_SUBTITLE;
2493 return AVMEDIA_TYPE_UNKNOWN;
2496 int avcodec_is_open(AVCodecContext *s)
2498 return !!s->internal;