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/integer.h"
30 #include "libavutil/crc.h"
31 #include "libavutil/pixdesc.h"
35 #include "imgconvert.h"
36 #include "audioconvert.h"
43 static int volatile entangled_thread_counter=0;
44 int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
45 static void *codec_mutex;
47 void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
52 *size= FFMAX(17*min_size/16 + 32, min_size);
54 ptr= av_realloc(ptr, *size);
55 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
61 void av_fast_malloc(void *ptr, unsigned int *size, unsigned int min_size)
66 *size= FFMAX(17*min_size/16 + 32, min_size);
68 *p = av_malloc(*size);
72 /* encoder management */
73 static AVCodec *first_avcodec = NULL;
75 AVCodec *av_codec_next(AVCodec *c){
77 else return first_avcodec;
80 void avcodec_register(AVCodec *codec)
85 while (*p != NULL) p = &(*p)->next;
90 #if LIBAVCODEC_VERSION_MAJOR < 53
91 void register_avcodec(AVCodec *codec)
93 avcodec_register(codec);
97 unsigned avcodec_get_edge_width(void)
102 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
103 s->coded_width = width;
104 s->coded_height= height;
105 s->width = -((-width )>>s->lowres);
106 s->height= -((-height)>>s->lowres);
109 typedef struct InternalBuffer{
115 enum PixelFormat pix_fmt;
118 #define INTERNAL_BUFFER_SIZE 32
120 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
125 case PIX_FMT_YUV420P:
126 case PIX_FMT_YUYV422:
127 case PIX_FMT_UYVY422:
128 case PIX_FMT_YUV422P:
129 case PIX_FMT_YUV440P:
130 case PIX_FMT_YUV444P:
132 case PIX_FMT_GRAY16BE:
133 case PIX_FMT_GRAY16LE:
134 case PIX_FMT_YUVJ420P:
135 case PIX_FMT_YUVJ422P:
136 case PIX_FMT_YUVJ440P:
137 case PIX_FMT_YUVJ444P:
138 case PIX_FMT_YUVA420P:
139 w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
141 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)
142 h_align= 32; // interlaced is rounded up to 2 MBs
144 case PIX_FMT_YUV411P:
145 case PIX_FMT_UYYVYY411:
149 case PIX_FMT_YUV410P:
150 if(s->codec_id == CODEC_ID_SVQ1){
155 if(s->codec_id == CODEC_ID_RPZA){
162 if(s->codec_id == CODEC_ID_SMC){
168 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
179 *width = FFALIGN(*width , w_align);
180 *height= FFALIGN(*height, h_align);
181 if(s->codec_id == CODEC_ID_H264)
182 *height+=2; // some of the optimized chroma MC reads one line too much
187 linesize_align[3] = STRIDE_ALIGN;
188 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
189 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
190 //picture size unneccessarily in some cases. The solution here is not
191 //pretty and better ideas are welcome!
193 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
194 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
195 s->codec_id == CODEC_ID_VP6A) {
198 linesize_align[2] = 16;
203 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
204 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
205 int linesize_align[4];
207 avcodec_align_dimensions2(s, width, height, linesize_align);
208 align = FFMAX(linesize_align[0], linesize_align[3]);
209 linesize_align[1] <<= chroma_shift;
210 linesize_align[2] <<= chroma_shift;
211 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
212 *width=FFALIGN(*width, align);
215 int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
216 if((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
219 av_log(av_log_ctx, AV_LOG_ERROR, "picture size invalid (%ux%u)\n", w, h);
220 return AVERROR(EINVAL);
223 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
230 if(pic->data[0]!=NULL) {
231 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
234 if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
235 av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
239 if(avcodec_check_dimensions(s,w,h))
242 if(s->internal_buffer==NULL){
243 s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
246 s->internal_buffer= av_fast_realloc(
248 &s->internal_buffer_size,
249 sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
253 buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
254 picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
257 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
259 av_freep(&buf->base[i]);
265 pic->age= *picture_number - buf->last_pic_num;
266 buf->last_pic_num= *picture_number;
268 int h_chroma_shift, v_chroma_shift;
275 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
277 avcodec_align_dimensions2(s, &w, &h, stride_align);
279 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
285 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
286 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
287 ff_fill_linesize(&picture, s->pix_fmt, w);
288 // increase alignment of w for next try (rhs gives the lowest bit set in w)
293 unaligned |= picture.linesize[i] % stride_align[i];
297 tmpsize = ff_fill_pointer(&picture, NULL, s->pix_fmt, h);
301 for (i=0; i<3 && picture.data[i+1]; i++)
302 size[i] = picture.data[i+1] - picture.data[i];
303 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
305 buf->last_pic_num= -256*256*256*64;
306 memset(buf->base, 0, sizeof(buf->base));
307 memset(buf->data, 0, sizeof(buf->data));
309 for(i=0; i<4 && size[i]; i++){
310 const int h_shift= i==0 ? 0 : h_chroma_shift;
311 const int v_shift= i==0 ? 0 : v_chroma_shift;
313 buf->linesize[i]= picture.linesize[i];
315 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
316 if(buf->base[i]==NULL) return -1;
317 memset(buf->base[i], 128, size[i]);
319 // no edge if EDGE EMU or not planar YUV
320 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
321 buf->data[i] = buf->base[i];
323 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
325 if(size[1] && !size[2])
326 ff_set_systematic_pal((uint32_t*)buf->data[1], s->pix_fmt);
327 buf->width = s->width;
328 buf->height = s->height;
329 buf->pix_fmt= s->pix_fmt;
330 pic->age= 256*256*256*64;
332 pic->type= FF_BUFFER_TYPE_INTERNAL;
335 pic->base[i]= buf->base[i];
336 pic->data[i]= buf->data[i];
337 pic->linesize[i]= buf->linesize[i];
339 s->internal_buffer_count++;
341 pic->reordered_opaque= s->reordered_opaque;
343 if(s->debug&FF_DEBUG_BUFFERS)
344 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
349 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
351 InternalBuffer *buf, *last;
353 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
354 assert(s->internal_buffer_count);
356 buf = NULL; /* avoids warning */
357 for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
358 buf= &((InternalBuffer*)s->internal_buffer)[i];
359 if(buf->data[0] == pic->data[0])
362 assert(i < s->internal_buffer_count);
363 s->internal_buffer_count--;
364 last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
366 FFSWAP(InternalBuffer, *buf, *last);
370 // pic->base[i]=NULL;
372 //printf("R%X\n", pic->opaque);
374 if(s->debug&FF_DEBUG_BUFFERS)
375 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
378 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
382 /* If no picture return a new buffer */
383 if(pic->data[0] == NULL) {
384 /* We will copy from buffer, so must be readable */
385 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
386 return s->get_buffer(s, pic);
389 /* If internal buffer type return the same buffer */
390 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
391 pic->reordered_opaque= s->reordered_opaque;
396 * Not internal type and reget_buffer not overridden, emulate cr buffer
399 for(i = 0; i < 4; i++)
400 pic->data[i] = pic->base[i] = NULL;
402 /* Allocate new frame */
403 if (s->get_buffer(s, pic))
405 /* Copy image data from old buffer to new buffer */
406 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
408 s->release_buffer(s, &temp_pic); // Release old frame
412 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
415 for(i=0; i<count; i++){
416 int r= func(c, (char*)arg + i*size);
422 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
425 for(i=0; i<count; i++){
426 int r= func(c, arg, i, 0);
432 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
433 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
438 void avcodec_get_frame_defaults(AVFrame *pic){
439 memset(pic, 0, sizeof(AVFrame));
441 pic->pts= AV_NOPTS_VALUE;
445 AVFrame *avcodec_alloc_frame(void){
446 AVFrame *pic= av_malloc(sizeof(AVFrame));
448 if(pic==NULL) return NULL;
450 avcodec_get_frame_defaults(pic);
455 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
459 /* If there is a user-supplied mutex locking routine, call it. */
461 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
465 entangled_thread_counter++;
466 if(entangled_thread_counter != 1){
467 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
471 if(avctx->codec || !codec)
474 if (codec->priv_data_size > 0) {
475 avctx->priv_data = av_mallocz(codec->priv_data_size);
476 if (!avctx->priv_data) {
477 ret = AVERROR(ENOMEM);
481 avctx->priv_data = NULL;
484 if(avctx->coded_width && avctx->coded_height)
485 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
486 else if(avctx->width && avctx->height)
487 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
489 #define SANE_NB_CHANNELS 128U
490 if (((avctx->coded_width || avctx->coded_height)
491 && avcodec_check_dimensions(avctx, avctx->coded_width, avctx->coded_height))
492 || avctx->channels > SANE_NB_CHANNELS) {
493 ret = AVERROR(EINVAL);
497 avctx->codec = codec;
498 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
499 avctx->codec_id == CODEC_ID_NONE) {
500 avctx->codec_type = codec->type;
501 avctx->codec_id = codec->id;
503 if(avctx->codec_id != codec->id || avctx->codec_type != codec->type){
504 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
507 avctx->frame_number = 0;
508 if(avctx->codec->init){
509 if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
510 avctx->codec->max_lowres < avctx->lowres){
511 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
512 avctx->codec->max_lowres);
516 ret = avctx->codec->init(avctx);
523 entangled_thread_counter--;
525 /* Release any user-supplied mutex. */
527 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
531 av_freep(&avctx->priv_data);
536 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
537 const short *samples)
539 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
540 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
543 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
544 int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
545 avctx->frame_number++;
551 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
554 if(buf_size < FF_MIN_BUFFER_SIZE){
555 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
558 if(avcodec_check_dimensions(avctx,avctx->width,avctx->height))
560 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
561 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
562 avctx->frame_number++;
563 emms_c(); //needed to avoid an emms_c() call before every return;
570 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
571 const AVSubtitle *sub)
574 if(sub->start_display_time) {
575 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
578 if(sub->num_rects == 0 || !sub->rects)
580 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
581 avctx->frame_number++;
585 #if LIBAVCODEC_VERSION_MAJOR < 53
586 int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
587 int *got_picture_ptr,
588 const uint8_t *buf, int buf_size)
591 av_init_packet(&avpkt);
593 avpkt.size = buf_size;
594 // HACK for CorePNG to decode as normal PNG by default
595 avpkt.flags = AV_PKT_FLAG_KEY;
597 return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
601 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
602 int *got_picture_ptr,
608 if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height))
610 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
611 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
614 emms_c(); //needed to avoid an emms_c() call before every return;
616 if (*got_picture_ptr)
617 avctx->frame_number++;
624 #if LIBAVCODEC_VERSION_MAJOR < 53
625 int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
627 const uint8_t *buf, int buf_size)
630 av_init_packet(&avpkt);
632 avpkt.size = buf_size;
634 return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
638 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
644 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
645 //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
646 if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
647 av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
650 if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
651 *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
652 av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
656 ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
657 avctx->frame_number++;
665 #if LIBAVCODEC_VERSION_MAJOR < 53
666 int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
668 const uint8_t *buf, int buf_size)
671 av_init_packet(&avpkt);
673 avpkt.size = buf_size;
675 return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
679 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
686 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
688 avctx->frame_number++;
692 void avsubtitle_free(AVSubtitle *sub)
696 for (i = 0; i < sub->num_rects; i++)
698 av_freep(&sub->rects[i]->pict.data[0]);
699 av_freep(&sub->rects[i]->pict.data[1]);
700 av_freep(&sub->rects[i]->pict.data[2]);
701 av_freep(&sub->rects[i]->pict.data[3]);
702 av_freep(&sub->rects[i]->text);
703 av_freep(&sub->rects[i]->ass);
704 av_freep(&sub->rects[i]);
707 av_freep(&sub->rects);
709 memset(sub, 0, sizeof(AVSubtitle));
712 av_cold int avcodec_close(AVCodecContext *avctx)
714 /* If there is a user-supplied mutex locking routine, call it. */
716 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
720 entangled_thread_counter++;
721 if(entangled_thread_counter != 1){
722 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
723 entangled_thread_counter--;
727 if (HAVE_THREADS && avctx->thread_opaque)
728 avcodec_thread_free(avctx);
729 if (avctx->codec && avctx->codec->close)
730 avctx->codec->close(avctx);
731 avcodec_default_free_buffers(avctx);
732 avctx->coded_frame = NULL;
733 av_freep(&avctx->priv_data);
734 if(avctx->codec && avctx->codec->encode)
735 av_freep(&avctx->extradata);
737 entangled_thread_counter--;
739 /* Release any user-supplied mutex. */
741 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
746 AVCodec *avcodec_find_encoder(enum CodecID id)
748 AVCodec *p, *experimental=NULL;
751 if (p->encode != NULL && p->id == id) {
752 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
762 AVCodec *avcodec_find_encoder_by_name(const char *name)
769 if (p->encode != NULL && strcmp(name,p->name) == 0)
776 AVCodec *avcodec_find_decoder(enum CodecID id)
781 if (p->decode != NULL && p->id == id)
788 AVCodec *avcodec_find_decoder_by_name(const char *name)
795 if (p->decode != NULL && strcmp(name,p->name) == 0)
802 static int get_bit_rate(AVCodecContext *ctx)
807 switch(ctx->codec_type) {
808 case AVMEDIA_TYPE_VIDEO:
809 case AVMEDIA_TYPE_DATA:
810 case AVMEDIA_TYPE_SUBTITLE:
811 case AVMEDIA_TYPE_ATTACHMENT:
812 bit_rate = ctx->bit_rate;
814 case AVMEDIA_TYPE_AUDIO:
815 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
816 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
825 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
829 for (i = 0; i < 4; i++) {
830 len = snprintf(buf, buf_size,
831 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
833 buf_size = buf_size > len ? buf_size - len : 0;
840 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
842 const char *codec_name;
846 AVRational display_aspect_ratio;
849 p = avcodec_find_encoder(enc->codec_id);
851 p = avcodec_find_decoder(enc->codec_id);
854 codec_name = p->name;
855 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
856 /* fake mpeg2 transport stream codec (currently not
858 codec_name = "mpeg2ts";
859 } else if (enc->codec_name[0] != '\0') {
860 codec_name = enc->codec_name;
862 /* output avi tags */
864 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
865 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
869 switch(enc->codec_type) {
870 case AVMEDIA_TYPE_VIDEO:
871 snprintf(buf, buf_size,
873 codec_name, enc->mb_decision ? " (hq)" : "");
874 if (enc->pix_fmt != PIX_FMT_NONE) {
875 snprintf(buf + strlen(buf), buf_size - strlen(buf),
877 avcodec_get_pix_fmt_name(enc->pix_fmt));
880 snprintf(buf + strlen(buf), buf_size - strlen(buf),
882 enc->width, enc->height);
883 if (enc->sample_aspect_ratio.num) {
884 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
885 enc->width*enc->sample_aspect_ratio.num,
886 enc->height*enc->sample_aspect_ratio.den,
888 snprintf(buf + strlen(buf), buf_size - strlen(buf),
889 " [PAR %d:%d DAR %d:%d]",
890 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
891 display_aspect_ratio.num, display_aspect_ratio.den);
893 if(av_log_get_level() >= AV_LOG_DEBUG){
894 int g= av_gcd(enc->time_base.num, enc->time_base.den);
895 snprintf(buf + strlen(buf), buf_size - strlen(buf),
897 enc->time_base.num/g, enc->time_base.den/g);
901 snprintf(buf + strlen(buf), buf_size - strlen(buf),
902 ", q=%d-%d", enc->qmin, enc->qmax);
905 case AVMEDIA_TYPE_AUDIO:
906 snprintf(buf, buf_size,
909 if (enc->sample_rate) {
910 snprintf(buf + strlen(buf), buf_size - strlen(buf),
911 ", %d Hz", enc->sample_rate);
913 av_strlcat(buf, ", ", buf_size);
914 avcodec_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
915 if (enc->sample_fmt != SAMPLE_FMT_NONE) {
916 snprintf(buf + strlen(buf), buf_size - strlen(buf),
917 ", %s", avcodec_get_sample_fmt_name(enc->sample_fmt));
920 case AVMEDIA_TYPE_DATA:
921 snprintf(buf, buf_size, "Data: %s", codec_name);
923 case AVMEDIA_TYPE_SUBTITLE:
924 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
926 case AVMEDIA_TYPE_ATTACHMENT:
927 snprintf(buf, buf_size, "Attachment: %s", codec_name);
930 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
934 if (enc->flags & CODEC_FLAG_PASS1)
935 snprintf(buf + strlen(buf), buf_size - strlen(buf),
937 if (enc->flags & CODEC_FLAG_PASS2)
938 snprintf(buf + strlen(buf), buf_size - strlen(buf),
941 bitrate = get_bit_rate(enc);
943 snprintf(buf + strlen(buf), buf_size - strlen(buf),
944 ", %d kb/s", bitrate / 1000);
948 unsigned avcodec_version( void )
950 return LIBAVCODEC_VERSION_INT;
953 const char *avcodec_configuration(void)
955 return FFMPEG_CONFIGURATION;
958 const char *avcodec_license(void)
960 #define LICENSE_PREFIX "libavcodec license: "
961 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
964 void avcodec_init(void)
966 static int initialized = 0;
968 if (initialized != 0)
972 dsputil_static_init();
975 void avcodec_flush_buffers(AVCodecContext *avctx)
977 if(avctx->codec->flush)
978 avctx->codec->flush(avctx);
981 void avcodec_default_free_buffers(AVCodecContext *s){
984 if(s->internal_buffer==NULL) return;
986 if (s->internal_buffer_count)
987 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
988 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
989 InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
991 av_freep(&buf->base[j]);
995 av_freep(&s->internal_buffer);
997 s->internal_buffer_count=0;
1000 char av_get_pict_type_char(int pict_type){
1002 case FF_I_TYPE: return 'I';
1003 case FF_P_TYPE: return 'P';
1004 case FF_B_TYPE: return 'B';
1005 case FF_S_TYPE: return 'S';
1006 case FF_SI_TYPE:return 'i';
1007 case FF_SP_TYPE:return 'p';
1008 case FF_BI_TYPE:return 'b';
1009 default: return '?';
1013 int av_get_bits_per_sample(enum CodecID codec_id){
1015 case CODEC_ID_ADPCM_SBPRO_2:
1017 case CODEC_ID_ADPCM_SBPRO_3:
1019 case CODEC_ID_ADPCM_SBPRO_4:
1020 case CODEC_ID_ADPCM_CT:
1021 case CODEC_ID_ADPCM_IMA_WAV:
1022 case CODEC_ID_ADPCM_MS:
1023 case CODEC_ID_ADPCM_YAMAHA:
1025 case CODEC_ID_PCM_ALAW:
1026 case CODEC_ID_PCM_MULAW:
1027 case CODEC_ID_PCM_S8:
1028 case CODEC_ID_PCM_U8:
1029 case CODEC_ID_PCM_ZORK:
1031 case CODEC_ID_PCM_S16BE:
1032 case CODEC_ID_PCM_S16LE:
1033 case CODEC_ID_PCM_S16LE_PLANAR:
1034 case CODEC_ID_PCM_U16BE:
1035 case CODEC_ID_PCM_U16LE:
1037 case CODEC_ID_PCM_S24DAUD:
1038 case CODEC_ID_PCM_S24BE:
1039 case CODEC_ID_PCM_S24LE:
1040 case CODEC_ID_PCM_U24BE:
1041 case CODEC_ID_PCM_U24LE:
1043 case CODEC_ID_PCM_S32BE:
1044 case CODEC_ID_PCM_S32LE:
1045 case CODEC_ID_PCM_U32BE:
1046 case CODEC_ID_PCM_U32LE:
1047 case CODEC_ID_PCM_F32BE:
1048 case CODEC_ID_PCM_F32LE:
1050 case CODEC_ID_PCM_F64BE:
1051 case CODEC_ID_PCM_F64LE:
1058 int av_get_bits_per_sample_format(enum SampleFormat sample_fmt) {
1059 switch (sample_fmt) {
1062 case SAMPLE_FMT_S16:
1064 case SAMPLE_FMT_S32:
1065 case SAMPLE_FMT_FLT:
1067 case SAMPLE_FMT_DBL:
1075 int avcodec_thread_init(AVCodecContext *s, int thread_count){
1076 s->thread_count = thread_count;
1081 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1098 } VideoFrameSizeAbbr;
1102 int rate_num, rate_den;
1103 } VideoFrameRateAbbr;
1105 static const VideoFrameSizeAbbr video_frame_size_abbrs[] = {
1106 { "ntsc", 720, 480 },
1107 { "pal", 720, 576 },
1108 { "qntsc", 352, 240 }, /* VCD compliant NTSC */
1109 { "qpal", 352, 288 }, /* VCD compliant PAL */
1110 { "sntsc", 640, 480 }, /* square pixel NTSC */
1111 { "spal", 768, 576 }, /* square pixel PAL */
1112 { "film", 352, 240 },
1113 { "ntsc-film", 352, 240 },
1114 { "sqcif", 128, 96 },
1115 { "qcif", 176, 144 },
1116 { "cif", 352, 288 },
1117 { "4cif", 704, 576 },
1118 { "16cif", 1408,1152 },
1119 { "qqvga", 160, 120 },
1120 { "qvga", 320, 240 },
1121 { "vga", 640, 480 },
1122 { "svga", 800, 600 },
1123 { "xga", 1024, 768 },
1124 { "uxga", 1600,1200 },
1125 { "qxga", 2048,1536 },
1126 { "sxga", 1280,1024 },
1127 { "qsxga", 2560,2048 },
1128 { "hsxga", 5120,4096 },
1129 { "wvga", 852, 480 },
1130 { "wxga", 1366, 768 },
1131 { "wsxga", 1600,1024 },
1132 { "wuxga", 1920,1200 },
1133 { "woxga", 2560,1600 },
1134 { "wqsxga", 3200,2048 },
1135 { "wquxga", 3840,2400 },
1136 { "whsxga", 6400,4096 },
1137 { "whuxga", 7680,4800 },
1138 { "cga", 320, 200 },
1139 { "ega", 640, 350 },
1140 { "hd480", 852, 480 },
1141 { "hd720", 1280, 720 },
1142 { "hd1080", 1920,1080 },
1145 static const VideoFrameRateAbbr video_frame_rate_abbrs[]= {
1146 { "ntsc", 30000, 1001 },
1148 { "qntsc", 30000, 1001 }, /* VCD compliant NTSC */
1149 { "qpal", 25, 1 }, /* VCD compliant PAL */
1150 { "sntsc", 30000, 1001 }, /* square pixel NTSC */
1151 { "spal", 25, 1 }, /* square pixel PAL */
1153 { "ntsc-film", 24000, 1001 },
1156 int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
1159 int n = FF_ARRAY_ELEMS(video_frame_size_abbrs);
1161 int frame_width = 0, frame_height = 0;
1164 if (!strcmp(video_frame_size_abbrs[i].abbr, str)) {
1165 frame_width = video_frame_size_abbrs[i].width;
1166 frame_height = video_frame_size_abbrs[i].height;
1172 frame_width = strtol(p, &p, 10);
1175 frame_height = strtol(p, &p, 10);
1177 if (frame_width <= 0 || frame_height <= 0)
1179 *width_ptr = frame_width;
1180 *height_ptr = frame_height;
1184 int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
1187 int n = FF_ARRAY_ELEMS(video_frame_rate_abbrs);
1190 /* First, we check our abbreviation table */
1191 for (i = 0; i < n; ++i)
1192 if (!strcmp(video_frame_rate_abbrs[i].abbr, arg)) {
1193 frame_rate->num = video_frame_rate_abbrs[i].rate_num;
1194 frame_rate->den = video_frame_rate_abbrs[i].rate_den;
1198 /* Then, we try to parse it as fraction */
1199 cp = strchr(arg, '/');
1201 cp = strchr(arg, ':');
1204 frame_rate->num = strtol(arg, &cpp, 10);
1205 if (cpp != arg || cpp == cp)
1206 frame_rate->den = strtol(cp+1, &cpp, 10);
1208 frame_rate->num = 0;
1211 /* Finally we give up and parse it as double */
1212 AVRational time_base = av_d2q(strtod(arg, 0), 1001000);
1213 frame_rate->den = time_base.den;
1214 frame_rate->num = time_base.num;
1216 if (!frame_rate->num || !frame_rate->den)
1222 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1224 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1228 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1230 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1231 "version to the newest one from SVN. If the problem still "
1232 "occurs, it means that your file has a feature which has not "
1233 "been implemented.", feature);
1235 av_log_ask_for_sample(avc, NULL);
1237 av_log(avc, AV_LOG_WARNING, "\n");
1240 void av_log_ask_for_sample(void *avc, const char *msg)
1243 av_log(avc, AV_LOG_WARNING, "%s ", msg);
1244 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1245 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1246 "and contact the ffmpeg-devel mailing list.\n");
1249 static AVHWAccel *first_hwaccel = NULL;
1251 void av_register_hwaccel(AVHWAccel *hwaccel)
1253 AVHWAccel **p = &first_hwaccel;
1257 hwaccel->next = NULL;
1260 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1262 return hwaccel ? hwaccel->next : first_hwaccel;
1265 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1267 AVHWAccel *hwaccel=NULL;
1269 while((hwaccel= av_hwaccel_next(hwaccel))){
1270 if ( hwaccel->id == codec_id
1271 && hwaccel->pix_fmt == pix_fmt)
1277 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1279 if (ff_lockmgr_cb) {
1280 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1286 if (ff_lockmgr_cb) {
1287 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1293 unsigned int ff_toupper4(unsigned int x)
1295 return toupper( x &0xFF)
1296 + (toupper((x>>8 )&0xFF)<<8 )
1297 + (toupper((x>>16)&0xFF)<<16)
1298 + (toupper((x>>24)&0xFF)<<24);