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/avstring.h"
29 #include "libavutil/crc.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/audioconvert.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/samplefmt.h"
36 #include "libavutil/opt.h"
37 #include "imgconvert.h"
39 #include "audioconvert.h"
46 static int volatile entangled_thread_counter=0;
47 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
48 static void *codec_mutex;
50 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
55 min_size= FFMAX(17*min_size/16 + 32, min_size);
57 ptr= av_realloc(ptr, min_size);
58 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
66 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
71 min_size= FFMAX(17*min_size/16 + 32, min_size);
73 *p = av_malloc(min_size);
74 if (!*p) min_size = 0;
78 /* encoder management */
79 static AVCodec *first_avcodec = NULL;
81 AVCodec *av_codec_next(AVCodec *c){
83 else return first_avcodec;
86 void avcodec_register(AVCodec *codec)
91 while (*p != NULL) p = &(*p)->next;
96 unsigned avcodec_get_edge_width(void)
101 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
102 s->coded_width = width;
103 s->coded_height= height;
104 s->width = -((-width )>>s->lowres);
105 s->height= -((-height)>>s->lowres);
108 typedef struct InternalBuffer{
114 enum PixelFormat pix_fmt;
117 #define INTERNAL_BUFFER_SIZE (32+1)
119 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
124 case PIX_FMT_YUV420P:
125 case PIX_FMT_YUYV422:
126 case PIX_FMT_UYVY422:
127 case PIX_FMT_YUV422P:
128 case PIX_FMT_YUV440P:
129 case PIX_FMT_YUV444P:
131 case PIX_FMT_GRAY16BE:
132 case PIX_FMT_GRAY16LE:
133 case PIX_FMT_YUVJ420P:
134 case PIX_FMT_YUVJ422P:
135 case PIX_FMT_YUVJ440P:
136 case PIX_FMT_YUVJ444P:
137 case PIX_FMT_YUVA420P:
138 case PIX_FMT_YUV420P9LE:
139 case PIX_FMT_YUV420P9BE:
140 case PIX_FMT_YUV420P10LE:
141 case PIX_FMT_YUV420P10BE:
142 case PIX_FMT_YUV422P10LE:
143 case PIX_FMT_YUV422P10BE:
144 case PIX_FMT_YUV444P9LE:
145 case PIX_FMT_YUV444P9BE:
146 case PIX_FMT_YUV444P10LE:
147 case PIX_FMT_YUV444P10BE:
148 w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
150 if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP || s->codec_id == CODEC_ID_H264)
151 h_align= 32; // interlaced is rounded up to 2 MBs
153 case PIX_FMT_YUV411P:
154 case PIX_FMT_UYYVYY411:
158 case PIX_FMT_YUV410P:
159 if(s->codec_id == CODEC_ID_SVQ1){
164 if(s->codec_id == CODEC_ID_RPZA){
171 if(s->codec_id == CODEC_ID_SMC){
177 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
188 *width = FFALIGN(*width , w_align);
189 *height= FFALIGN(*height, h_align);
190 if(s->codec_id == CODEC_ID_H264 || s->lowres)
191 *height+=2; // some of the optimized chroma MC reads one line too much
192 // which is also done in mpeg decoders with lowres > 0
197 linesize_align[3] = STRIDE_ALIGN;
198 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
199 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
200 //picture size unneccessarily in some cases. The solution here is not
201 //pretty and better ideas are welcome!
203 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
204 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
205 s->codec_id == CODEC_ID_VP6A) {
208 linesize_align[2] = 16;
213 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
214 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
215 int linesize_align[4];
217 avcodec_align_dimensions2(s, width, height, linesize_align);
218 align = FFMAX(linesize_align[0], linesize_align[3]);
219 linesize_align[1] <<= chroma_shift;
220 linesize_align[2] <<= chroma_shift;
221 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
222 *width=FFALIGN(*width, align);
225 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
232 if(pic->data[0]!=NULL) {
233 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
236 if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
237 av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
241 if(av_image_check_size(w, h, 0, s))
244 if(s->internal_buffer==NULL){
245 s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
248 s->internal_buffer= av_fast_realloc(
250 &s->internal_buffer_size,
251 sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
255 buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
256 picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
259 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
260 if(s->active_thread_type&FF_THREAD_FRAME) {
261 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
266 av_freep(&buf->base[i]);
272 pic->age= *picture_number - buf->last_pic_num;
273 buf->last_pic_num= *picture_number;
275 int h_chroma_shift, v_chroma_shift;
281 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
283 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
285 avcodec_align_dimensions2(s, &w, &h, stride_align);
287 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
293 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
294 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
295 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
296 // increase alignment of w for next try (rhs gives the lowest bit set in w)
301 unaligned |= picture.linesize[i] % stride_align[i];
305 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
309 for (i=0; i<3 && picture.data[i+1]; i++)
310 size[i] = picture.data[i+1] - picture.data[i];
311 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
313 buf->last_pic_num= -256*256*256*64;
314 memset(buf->base, 0, sizeof(buf->base));
315 memset(buf->data, 0, sizeof(buf->data));
317 for(i=0; i<4 && size[i]; i++){
318 const int h_shift= i==0 ? 0 : h_chroma_shift;
319 const int v_shift= i==0 ? 0 : v_chroma_shift;
321 buf->linesize[i]= picture.linesize[i];
323 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
324 if(buf->base[i]==NULL) return -1;
325 memset(buf->base[i], 128, size[i]);
327 // no edge if EDGE EMU or not planar YUV
328 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
329 buf->data[i] = buf->base[i];
331 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
333 if(size[1] && !size[2])
334 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
335 buf->width = s->width;
336 buf->height = s->height;
337 buf->pix_fmt= s->pix_fmt;
338 pic->age= 256*256*256*64;
340 pic->type= FF_BUFFER_TYPE_INTERNAL;
343 pic->base[i]= buf->base[i];
344 pic->data[i]= buf->data[i];
345 pic->linesize[i]= buf->linesize[i];
347 s->internal_buffer_count++;
350 pic->pkt_pts = s->pkt->pts;
351 pic->pkt_pos = s->pkt->pos;
353 pic->pkt_pts = AV_NOPTS_VALUE;
356 pic->reordered_opaque= s->reordered_opaque;
357 pic->sample_aspect_ratio = s->sample_aspect_ratio;
358 pic->width = s->width;
359 pic->height = s->height;
360 pic->format = s->pix_fmt;
362 if(s->debug&FF_DEBUG_BUFFERS)
363 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
368 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
370 InternalBuffer *buf, *last;
372 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
373 assert(s->internal_buffer_count);
375 if(s->internal_buffer){
376 buf = NULL; /* avoids warning */
377 for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
378 buf= &((InternalBuffer*)s->internal_buffer)[i];
379 if(buf->data[0] == pic->data[0])
382 assert(i < s->internal_buffer_count);
383 s->internal_buffer_count--;
384 last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
386 FFSWAP(InternalBuffer, *buf, *last);
391 // pic->base[i]=NULL;
393 //printf("R%X\n", pic->opaque);
395 if(s->debug&FF_DEBUG_BUFFERS)
396 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
399 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
403 /* If no picture return a new buffer */
404 if(pic->data[0] == NULL) {
405 /* We will copy from buffer, so must be readable */
406 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
407 return s->get_buffer(s, pic);
410 /* If internal buffer type return the same buffer */
411 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
412 if(s->pkt) pic->pkt_pts= s->pkt->pts;
413 else pic->pkt_pts= AV_NOPTS_VALUE;
414 pic->reordered_opaque= s->reordered_opaque;
419 * Not internal type and reget_buffer not overridden, emulate cr buffer
422 for(i = 0; i < 4; i++)
423 pic->data[i] = pic->base[i] = NULL;
425 /* Allocate new frame */
426 if (s->get_buffer(s, pic))
428 /* Copy image data from old buffer to new buffer */
429 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
431 s->release_buffer(s, &temp_pic); // Release old frame
435 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
438 for(i=0; i<count; i++){
439 int r= func(c, (char*)arg + i*size);
445 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
448 for(i=0; i<count; i++){
449 int r= func(c, arg, i, 0);
455 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
456 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
461 void avcodec_get_frame_defaults(AVFrame *pic){
462 memset(pic, 0, sizeof(AVFrame));
464 pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
467 pic->sample_aspect_ratio = (AVRational){0, 1};
468 pic->format = -1; /* unknown */
471 AVFrame *avcodec_alloc_frame(void){
472 AVFrame *pic= av_malloc(sizeof(AVFrame));
474 if(pic==NULL) return NULL;
476 avcodec_get_frame_defaults(pic);
481 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
483 memset(sub, 0, sizeof(*sub));
484 sub->pts = AV_NOPTS_VALUE;
487 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
491 /* If there is a user-supplied mutex locking routine, call it. */
493 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
497 entangled_thread_counter++;
498 if(entangled_thread_counter != 1){
499 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
504 if(avctx->codec || !codec) {
505 ret = AVERROR(EINVAL);
509 if (codec->priv_data_size > 0) {
510 if(!avctx->priv_data){
511 avctx->priv_data = av_mallocz(codec->priv_data_size);
512 if (!avctx->priv_data) {
513 ret = AVERROR(ENOMEM);
516 if(codec->priv_class){ //this can be droped once all user apps use avcodec_get_context_defaults3()
517 *(AVClass**)avctx->priv_data= codec->priv_class;
518 av_opt_set_defaults(avctx->priv_data);
522 avctx->priv_data = NULL;
525 if(avctx->coded_width && avctx->coded_height)
526 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
527 else if(avctx->width && avctx->height)
528 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
530 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
531 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
532 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
533 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
534 avcodec_set_dimensions(avctx, 0, 0);
537 /* if the decoder init function was already called previously,
538 free the already allocated subtitle_header before overwriting it */
540 av_freep(&avctx->subtitle_header);
542 #define SANE_NB_CHANNELS 128U
543 if (avctx->channels > SANE_NB_CHANNELS) {
544 ret = AVERROR(EINVAL);
548 avctx->codec = codec;
549 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
550 avctx->codec_id == CODEC_ID_NONE) {
551 avctx->codec_type = codec->type;
552 avctx->codec_id = codec->id;
554 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
555 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
556 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
557 ret = AVERROR(EINVAL);
560 avctx->frame_number = 0;
562 if (HAVE_THREADS && !avctx->thread_opaque) {
563 ret = ff_thread_init(avctx);
569 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
570 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
571 avctx->codec->max_lowres);
572 ret = AVERROR(EINVAL);
575 if (avctx->codec->encode) {
577 if (avctx->codec->sample_fmts) {
578 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
579 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
581 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
582 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
583 ret = AVERROR(EINVAL);
587 if (avctx->codec->supported_samplerates) {
588 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
589 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
591 if (avctx->codec->supported_samplerates[i] == 0) {
592 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
593 ret = AVERROR(EINVAL);
597 if (avctx->codec->channel_layouts) {
598 if (!avctx->channel_layout) {
599 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
601 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
602 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
604 if (avctx->codec->channel_layouts[i] == 0) {
605 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
606 ret = AVERROR(EINVAL);
611 if (avctx->channel_layout && avctx->channels) {
612 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
613 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
614 ret = AVERROR(EINVAL);
617 } else if (avctx->channel_layout) {
618 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
622 avctx->pts_correction_num_faulty_pts =
623 avctx->pts_correction_num_faulty_dts = 0;
624 avctx->pts_correction_last_pts =
625 avctx->pts_correction_last_dts = INT64_MIN;
627 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
628 ret = avctx->codec->init(avctx);
636 entangled_thread_counter--;
638 /* Release any user-supplied mutex. */
640 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
644 av_freep(&avctx->priv_data);
649 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
650 const short *samples)
652 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
653 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
656 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
657 int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
658 avctx->frame_number++;
664 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
667 if(buf_size < FF_MIN_BUFFER_SIZE){
668 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
671 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
673 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
674 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
675 avctx->frame_number++;
676 emms_c(); //needed to avoid an emms_c() call before every return;
683 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
684 const AVSubtitle *sub)
687 if(sub->start_display_time) {
688 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
692 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
693 avctx->frame_number++;
698 * Attempt to guess proper monotonic timestamps for decoded video frames
699 * which might have incorrect times. Input timestamps may wrap around, in
700 * which case the output will as well.
702 * @param pts the pts field of the decoded AVPacket, as passed through
704 * @param dts the dts field of the decoded AVPacket
705 * @return one of the input values, may be AV_NOPTS_VALUE
707 static int64_t guess_correct_pts(AVCodecContext *ctx,
708 int64_t reordered_pts, int64_t dts)
710 int64_t pts = AV_NOPTS_VALUE;
712 if (dts != AV_NOPTS_VALUE) {
713 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
714 ctx->pts_correction_last_dts = dts;
716 if (reordered_pts != AV_NOPTS_VALUE) {
717 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
718 ctx->pts_correction_last_pts = reordered_pts;
720 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
721 && reordered_pts != AV_NOPTS_VALUE)
729 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
730 int *got_picture_ptr,
736 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
739 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
740 av_packet_split_side_data(avpkt);
742 if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
743 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
746 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
748 picture->pkt_dts= avpkt->dts;
750 if(!avctx->has_b_frames){
751 picture->pkt_pos= avpkt->pos;
753 //FIXME these should be under if(!avctx->has_b_frames)
754 if (!picture->sample_aspect_ratio.num)
755 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
757 picture->width = avctx->width;
758 if (!picture->height)
759 picture->height = avctx->height;
760 if (picture->format == PIX_FMT_NONE)
761 picture->format = avctx->pix_fmt;
764 emms_c(); //needed to avoid an emms_c() call before every return;
767 if (*got_picture_ptr){
768 avctx->frame_number++;
769 picture->best_effort_timestamp = guess_correct_pts(avctx,
779 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
787 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
788 //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
789 if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
790 av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
793 if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
794 *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
795 av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
799 ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
800 avctx->frame_number++;
808 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
816 avcodec_get_subtitle_defaults(sub);
817 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
819 avctx->frame_number++;
823 void avsubtitle_free(AVSubtitle *sub)
827 for (i = 0; i < sub->num_rects; i++)
829 av_freep(&sub->rects[i]->pict.data[0]);
830 av_freep(&sub->rects[i]->pict.data[1]);
831 av_freep(&sub->rects[i]->pict.data[2]);
832 av_freep(&sub->rects[i]->pict.data[3]);
833 av_freep(&sub->rects[i]->text);
834 av_freep(&sub->rects[i]->ass);
835 av_freep(&sub->rects[i]);
838 av_freep(&sub->rects);
840 memset(sub, 0, sizeof(AVSubtitle));
843 av_cold int avcodec_close(AVCodecContext *avctx)
845 /* If there is a user-supplied mutex locking routine, call it. */
847 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
851 entangled_thread_counter++;
852 if(entangled_thread_counter != 1){
853 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
854 entangled_thread_counter--;
858 if (HAVE_THREADS && avctx->thread_opaque)
859 ff_thread_free(avctx);
860 if (avctx->codec && avctx->codec->close)
861 avctx->codec->close(avctx);
862 avcodec_default_free_buffers(avctx);
863 avctx->coded_frame = NULL;
864 if (avctx->codec && avctx->codec->priv_class)
865 av_opt_free(avctx->priv_data);
867 av_freep(&avctx->priv_data);
868 if(avctx->codec && avctx->codec->encode)
869 av_freep(&avctx->extradata);
871 avctx->active_thread_type = 0;
872 entangled_thread_counter--;
874 /* Release any user-supplied mutex. */
876 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
881 AVCodec *avcodec_find_encoder(enum CodecID id)
883 AVCodec *p, *experimental=NULL;
886 if (p->encode != NULL && p->id == id) {
887 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
897 AVCodec *avcodec_find_encoder_by_name(const char *name)
904 if (p->encode != NULL && strcmp(name,p->name) == 0)
911 AVCodec *avcodec_find_decoder(enum CodecID id)
913 AVCodec *p, *experimental=NULL;
916 if (p->decode != NULL && p->id == id) {
917 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
927 AVCodec *avcodec_find_decoder_by_name(const char *name)
934 if (p->decode != NULL && strcmp(name,p->name) == 0)
941 static int get_bit_rate(AVCodecContext *ctx)
946 switch(ctx->codec_type) {
947 case AVMEDIA_TYPE_VIDEO:
948 case AVMEDIA_TYPE_DATA:
949 case AVMEDIA_TYPE_SUBTITLE:
950 case AVMEDIA_TYPE_ATTACHMENT:
951 bit_rate = ctx->bit_rate;
953 case AVMEDIA_TYPE_AUDIO:
954 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
955 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
964 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
968 for (i = 0; i < 4; i++) {
969 len = snprintf(buf, buf_size,
970 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
972 buf_size = buf_size > len ? buf_size - len : 0;
979 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
981 const char *codec_name;
982 const char *profile = NULL;
986 AVRational display_aspect_ratio;
989 p = avcodec_find_encoder(enc->codec_id);
991 p = avcodec_find_decoder(enc->codec_id);
994 codec_name = p->name;
995 profile = av_get_profile_name(p, enc->profile);
996 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
997 /* fake mpeg2 transport stream codec (currently not
999 codec_name = "mpeg2ts";
1000 } else if (enc->codec_name[0] != '\0') {
1001 codec_name = enc->codec_name;
1003 /* output avi tags */
1005 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1006 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1010 switch(enc->codec_type) {
1011 case AVMEDIA_TYPE_VIDEO:
1012 snprintf(buf, buf_size,
1014 codec_name, enc->mb_decision ? " (hq)" : "");
1016 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1018 if (enc->pix_fmt != PIX_FMT_NONE) {
1019 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1021 av_get_pix_fmt_name(enc->pix_fmt));
1024 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1026 enc->width, enc->height);
1027 if (enc->sample_aspect_ratio.num) {
1028 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1029 enc->width*enc->sample_aspect_ratio.num,
1030 enc->height*enc->sample_aspect_ratio.den,
1032 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1033 " [PAR %d:%d DAR %d:%d]",
1034 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1035 display_aspect_ratio.num, display_aspect_ratio.den);
1037 if(av_log_get_level() >= AV_LOG_DEBUG){
1038 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1039 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1041 enc->time_base.num/g, enc->time_base.den/g);
1045 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1046 ", q=%d-%d", enc->qmin, enc->qmax);
1049 case AVMEDIA_TYPE_AUDIO:
1050 snprintf(buf, buf_size,
1054 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1056 if (enc->sample_rate) {
1057 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1058 ", %d Hz", enc->sample_rate);
1060 av_strlcat(buf, ", ", buf_size);
1061 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1062 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1063 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1064 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1067 case AVMEDIA_TYPE_DATA:
1068 snprintf(buf, buf_size, "Data: %s", codec_name);
1070 case AVMEDIA_TYPE_SUBTITLE:
1071 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1073 case AVMEDIA_TYPE_ATTACHMENT:
1074 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1077 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1081 if (enc->flags & CODEC_FLAG_PASS1)
1082 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1084 if (enc->flags & CODEC_FLAG_PASS2)
1085 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1088 bitrate = get_bit_rate(enc);
1090 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1091 ", %d kb/s", bitrate / 1000);
1095 const char *av_get_profile_name(const AVCodec *codec, int profile)
1098 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1101 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1102 if (p->profile == profile)
1108 unsigned avcodec_version( void )
1110 return LIBAVCODEC_VERSION_INT;
1113 const char *avcodec_configuration(void)
1115 return FFMPEG_CONFIGURATION;
1118 const char *avcodec_license(void)
1120 #define LICENSE_PREFIX "libavcodec license: "
1121 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1124 void avcodec_init(void)
1126 static int initialized = 0;
1128 if (initialized != 0)
1132 dsputil_static_init();
1135 void avcodec_flush_buffers(AVCodecContext *avctx)
1137 if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1138 ff_thread_flush(avctx);
1139 if(avctx->codec->flush)
1140 avctx->codec->flush(avctx);
1143 void avcodec_default_free_buffers(AVCodecContext *s){
1146 if(s->internal_buffer==NULL) return;
1148 if (s->internal_buffer_count)
1149 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
1150 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1151 InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
1153 av_freep(&buf->base[j]);
1157 av_freep(&s->internal_buffer);
1159 s->internal_buffer_count=0;
1162 #if FF_API_OLD_FF_PICT_TYPES
1163 char av_get_pict_type_char(int pict_type){
1164 return av_get_picture_type_char(pict_type);
1168 int av_get_bits_per_sample(enum CodecID codec_id){
1170 case CODEC_ID_ADPCM_SBPRO_2:
1172 case CODEC_ID_ADPCM_SBPRO_3:
1174 case CODEC_ID_ADPCM_SBPRO_4:
1175 case CODEC_ID_ADPCM_CT:
1176 case CODEC_ID_ADPCM_IMA_WAV:
1177 case CODEC_ID_ADPCM_MS:
1178 case CODEC_ID_ADPCM_YAMAHA:
1180 case CODEC_ID_ADPCM_G722:
1181 case CODEC_ID_PCM_ALAW:
1182 case CODEC_ID_PCM_MULAW:
1183 case CODEC_ID_PCM_S8:
1184 case CODEC_ID_PCM_U8:
1185 case CODEC_ID_PCM_ZORK:
1187 case CODEC_ID_PCM_S16BE:
1188 case CODEC_ID_PCM_S16LE:
1189 case CODEC_ID_PCM_S16LE_PLANAR:
1190 case CODEC_ID_PCM_U16BE:
1191 case CODEC_ID_PCM_U16LE:
1193 case CODEC_ID_PCM_S24DAUD:
1194 case CODEC_ID_PCM_S24BE:
1195 case CODEC_ID_PCM_S24LE:
1196 case CODEC_ID_PCM_U24BE:
1197 case CODEC_ID_PCM_U24LE:
1199 case CODEC_ID_PCM_S32BE:
1200 case CODEC_ID_PCM_S32LE:
1201 case CODEC_ID_PCM_U32BE:
1202 case CODEC_ID_PCM_U32LE:
1203 case CODEC_ID_PCM_F32BE:
1204 case CODEC_ID_PCM_F32LE:
1206 case CODEC_ID_PCM_F64BE:
1207 case CODEC_ID_PCM_F64LE:
1214 #if FF_API_OLD_SAMPLE_FMT
1215 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1216 return av_get_bytes_per_sample(sample_fmt) << 3;
1221 int ff_thread_init(AVCodecContext *s){
1226 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1240 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1242 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1246 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1248 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1249 "version to the newest one from Git. If the problem still "
1250 "occurs, it means that your file has a feature which has not "
1251 "been implemented.\n", feature);
1253 av_log_ask_for_sample(avc, NULL);
1256 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1258 va_list argument_list;
1260 va_start(argument_list, msg);
1263 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1264 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1265 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1266 "and contact the ffmpeg-devel mailing list.\n");
1268 va_end(argument_list);
1271 static AVHWAccel *first_hwaccel = NULL;
1273 void av_register_hwaccel(AVHWAccel *hwaccel)
1275 AVHWAccel **p = &first_hwaccel;
1279 hwaccel->next = NULL;
1282 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1284 return hwaccel ? hwaccel->next : first_hwaccel;
1287 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1289 AVHWAccel *hwaccel=NULL;
1291 while((hwaccel= av_hwaccel_next(hwaccel))){
1292 if ( hwaccel->id == codec_id
1293 && hwaccel->pix_fmt == pix_fmt)
1299 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1301 if (ff_lockmgr_cb) {
1302 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1308 if (ff_lockmgr_cb) {
1309 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1315 unsigned int ff_toupper4(unsigned int x)
1317 return toupper( x &0xFF)
1318 + (toupper((x>>8 )&0xFF)<<8 )
1319 + (toupper((x>>16)&0xFF)<<16)
1320 + (toupper((x>>24)&0xFF)<<24);
1325 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1328 return avctx->get_buffer(avctx, f);
1331 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1333 f->owner->release_buffer(f->owner, f);
1336 void ff_thread_finish_setup(AVCodecContext *avctx)
1340 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1344 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1350 #if FF_API_THREAD_INIT
1351 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1353 s->thread_count = thread_count;
1354 return ff_thread_init(s);