3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; 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/mathematics.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/audioconvert.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/samplefmt.h"
35 #include "libavutil/dict.h"
38 #include "libavutil/opt.h"
39 #include "imgconvert.h"
41 #include "audioconvert.h"
48 static int volatile entangled_thread_counter=0;
49 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
50 static void *codec_mutex;
51 static void *avformat_mutex;
53 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
58 min_size= FFMAX(17*min_size/16 + 32, min_size);
60 ptr= av_realloc(ptr, min_size);
61 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
69 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
74 min_size= FFMAX(17*min_size/16 + 32, min_size);
76 *p = av_malloc(min_size);
77 if (!*p) min_size = 0;
81 /* encoder management */
82 static AVCodec *first_avcodec = NULL;
84 AVCodec *av_codec_next(AVCodec *c){
86 else return first_avcodec;
89 #if !FF_API_AVCODEC_INIT
92 void avcodec_init(void)
94 static int initialized = 0;
100 dsputil_static_init();
103 void avcodec_register(AVCodec *codec)
108 while (*p != NULL) p = &(*p)->next;
112 if (codec->init_static_data)
113 codec->init_static_data(codec);
116 unsigned avcodec_get_edge_width(void)
121 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
122 s->coded_width = width;
123 s->coded_height= height;
124 s->width = -((-width )>>s->lowres);
125 s->height= -((-height)>>s->lowres);
128 typedef struct InternalBuffer{
134 enum PixelFormat pix_fmt;
137 #define INTERNAL_BUFFER_SIZE (32+1)
139 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
144 case PIX_FMT_YUV420P:
145 case PIX_FMT_YUYV422:
146 case PIX_FMT_UYVY422:
147 case PIX_FMT_YUV422P:
148 case PIX_FMT_YUV440P:
149 case PIX_FMT_YUV444P:
151 case PIX_FMT_GRAY16BE:
152 case PIX_FMT_GRAY16LE:
153 case PIX_FMT_YUVJ420P:
154 case PIX_FMT_YUVJ422P:
155 case PIX_FMT_YUVJ440P:
156 case PIX_FMT_YUVJ444P:
157 case PIX_FMT_YUVA420P:
158 case PIX_FMT_YUV420P9LE:
159 case PIX_FMT_YUV420P9BE:
160 case PIX_FMT_YUV420P10LE:
161 case PIX_FMT_YUV420P10BE:
162 case PIX_FMT_YUV422P9LE:
163 case PIX_FMT_YUV422P9BE:
164 case PIX_FMT_YUV422P10LE:
165 case PIX_FMT_YUV422P10BE:
166 case PIX_FMT_YUV444P9LE:
167 case PIX_FMT_YUV444P9BE:
168 case PIX_FMT_YUV444P10LE:
169 case PIX_FMT_YUV444P10BE:
170 w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
172 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)
173 h_align= 32; // interlaced is rounded up to 2 MBs
175 case PIX_FMT_YUV411P:
176 case PIX_FMT_UYYVYY411:
180 case PIX_FMT_YUV410P:
181 if(s->codec_id == CODEC_ID_SVQ1){
186 if(s->codec_id == CODEC_ID_RPZA){
193 if(s->codec_id == CODEC_ID_SMC){
199 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
210 *width = FFALIGN(*width , w_align);
211 *height= FFALIGN(*height, h_align);
212 if(s->codec_id == CODEC_ID_H264 || s->lowres)
213 *height+=2; // some of the optimized chroma MC reads one line too much
214 // which is also done in mpeg decoders with lowres > 0
219 linesize_align[3] = STRIDE_ALIGN;
220 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
221 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
222 //picture size unneccessarily in some cases. The solution here is not
223 //pretty and better ideas are welcome!
225 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
226 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
227 s->codec_id == CODEC_ID_VP6A) {
230 linesize_align[2] = 16;
235 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
236 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
237 int linesize_align[4];
239 avcodec_align_dimensions2(s, width, height, linesize_align);
240 align = FFMAX(linesize_align[0], linesize_align[3]);
241 linesize_align[1] <<= chroma_shift;
242 linesize_align[2] <<= chroma_shift;
243 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
244 *width=FFALIGN(*width, align);
247 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
254 if(pic->data[0]!=NULL) {
255 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
258 if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
259 av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
263 if(av_image_check_size(w, h, 0, s))
266 if(s->internal_buffer==NULL){
267 s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
270 s->internal_buffer= av_fast_realloc(
272 &s->internal_buffer_size,
273 sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
277 buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
278 picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
281 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
282 if(s->active_thread_type&FF_THREAD_FRAME) {
283 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
288 av_freep(&buf->base[i]);
294 pic->age= *picture_number - buf->last_pic_num;
295 buf->last_pic_num= *picture_number;
297 int h_chroma_shift, v_chroma_shift;
303 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
305 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
307 avcodec_align_dimensions2(s, &w, &h, stride_align);
309 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
315 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
316 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
317 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
318 // increase alignment of w for next try (rhs gives the lowest bit set in w)
323 unaligned |= picture.linesize[i] % stride_align[i];
327 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
331 for (i=0; i<3 && picture.data[i+1]; i++)
332 size[i] = picture.data[i+1] - picture.data[i];
333 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
335 buf->last_pic_num= -256*256*256*64;
336 memset(buf->base, 0, sizeof(buf->base));
337 memset(buf->data, 0, sizeof(buf->data));
339 for(i=0; i<4 && size[i]; i++){
340 const int h_shift= i==0 ? 0 : h_chroma_shift;
341 const int v_shift= i==0 ? 0 : v_chroma_shift;
343 buf->linesize[i]= picture.linesize[i];
345 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
346 if(buf->base[i]==NULL) return -1;
347 memset(buf->base[i], 128, size[i]);
349 // no edge if EDGE EMU or not planar YUV
350 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
351 buf->data[i] = buf->base[i];
353 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
355 if(size[1] && !size[2])
356 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
357 buf->width = s->width;
358 buf->height = s->height;
359 buf->pix_fmt= s->pix_fmt;
360 pic->age= 256*256*256*64;
362 pic->type= FF_BUFFER_TYPE_INTERNAL;
365 pic->base[i]= buf->base[i];
366 pic->data[i]= buf->data[i];
367 pic->linesize[i]= buf->linesize[i];
369 s->internal_buffer_count++;
371 if(s->pkt) pic->pkt_pts= s->pkt->pts;
372 else pic->pkt_pts= AV_NOPTS_VALUE;
373 pic->reordered_opaque= s->reordered_opaque;
375 if(s->debug&FF_DEBUG_BUFFERS)
376 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
381 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
383 InternalBuffer *buf, *last;
385 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
386 assert(s->internal_buffer_count);
388 if(s->internal_buffer){
389 buf = NULL; /* avoids warning */
390 for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
391 buf= &((InternalBuffer*)s->internal_buffer)[i];
392 if(buf->data[0] == pic->data[0])
395 assert(i < s->internal_buffer_count);
396 s->internal_buffer_count--;
397 last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
399 FFSWAP(InternalBuffer, *buf, *last);
404 // pic->base[i]=NULL;
406 //printf("R%X\n", pic->opaque);
408 if(s->debug&FF_DEBUG_BUFFERS)
409 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
412 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
416 /* If no picture return a new buffer */
417 if(pic->data[0] == NULL) {
418 /* We will copy from buffer, so must be readable */
419 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
420 return s->get_buffer(s, pic);
423 /* If internal buffer type return the same buffer */
424 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
425 if(s->pkt) pic->pkt_pts= s->pkt->pts;
426 else pic->pkt_pts= AV_NOPTS_VALUE;
427 pic->reordered_opaque= s->reordered_opaque;
432 * Not internal type and reget_buffer not overridden, emulate cr buffer
435 for(i = 0; i < 4; i++)
436 pic->data[i] = pic->base[i] = NULL;
438 /* Allocate new frame */
439 if (s->get_buffer(s, pic))
441 /* Copy image data from old buffer to new buffer */
442 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
444 s->release_buffer(s, &temp_pic); // Release old frame
448 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
451 for(i=0; i<count; i++){
452 int r= func(c, (char*)arg + i*size);
458 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
461 for(i=0; i<count; i++){
462 int r= func(c, arg, i, 0);
468 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
469 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
474 void avcodec_get_frame_defaults(AVFrame *pic){
475 memset(pic, 0, sizeof(AVFrame));
477 pic->pts= AV_NOPTS_VALUE;
481 AVFrame *avcodec_alloc_frame(void){
482 AVFrame *pic= av_malloc(sizeof(AVFrame));
484 if(pic==NULL) return NULL;
486 avcodec_get_frame_defaults(pic);
491 #if FF_API_AVCODEC_OPEN
492 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
494 return avcodec_open2(avctx, codec, NULL);
498 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
501 AVDictionary *tmp = NULL;
504 av_dict_copy(&tmp, *options, 0);
506 /* If there is a user-supplied mutex locking routine, call it. */
508 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
512 entangled_thread_counter++;
513 if(entangled_thread_counter != 1){
514 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
519 if(avctx->codec || !codec) {
520 ret = AVERROR(EINVAL);
524 if (codec->priv_data_size > 0) {
525 if(!avctx->priv_data){
526 avctx->priv_data = av_mallocz(codec->priv_data_size);
527 if (!avctx->priv_data) {
528 ret = AVERROR(ENOMEM);
531 if (codec->priv_class) {
532 *(AVClass**)avctx->priv_data= codec->priv_class;
533 av_opt_set_defaults(avctx->priv_data);
536 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
539 avctx->priv_data = NULL;
541 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
544 if(avctx->coded_width && avctx->coded_height)
545 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
546 else if(avctx->width && avctx->height)
547 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
549 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
550 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
551 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
552 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
553 avcodec_set_dimensions(avctx, 0, 0);
556 /* if the decoder init function was already called previously,
557 free the already allocated subtitle_header before overwriting it */
559 av_freep(&avctx->subtitle_header);
561 #define SANE_NB_CHANNELS 128U
562 if (avctx->channels > SANE_NB_CHANNELS) {
563 ret = AVERROR(EINVAL);
567 avctx->codec = codec;
568 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
569 avctx->codec_id == CODEC_ID_NONE) {
570 avctx->codec_type = codec->type;
571 avctx->codec_id = codec->id;
573 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
574 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
575 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
576 ret = AVERROR(EINVAL);
579 avctx->frame_number = 0;
582 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
583 avctx->error_recognition, avctx->err_recognition);
584 /* FF_ER_CAREFUL (==1) implies AV_EF_CRCCHECK (== 1<<1 - 1),
585 FF_ER_COMPLIANT (==2) implies AV_EF_{CRCCHECK,BITSTREAM} (== 1<<2 - 1), et cetera} */
586 avctx->err_recognition |= (1<<(avctx->error_recognition-(avctx->error_recognition>=FF_ER_VERY_AGGRESSIVE))) - 1;
587 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
588 avctx->error_recognition, avctx->err_recognition);
591 if (HAVE_THREADS && !avctx->thread_opaque) {
592 ret = ff_thread_init(avctx);
598 if (avctx->codec->max_lowres < avctx->lowres) {
599 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
600 avctx->codec->max_lowres);
601 ret = AVERROR(EINVAL);
604 if (avctx->codec->encode) {
606 if (avctx->codec->sample_fmts) {
607 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
608 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
610 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
611 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
612 ret = AVERROR(EINVAL);
616 if (avctx->codec->supported_samplerates) {
617 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
618 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
620 if (avctx->codec->supported_samplerates[i] == 0) {
621 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
622 ret = AVERROR(EINVAL);
626 if (avctx->codec->channel_layouts) {
627 if (!avctx->channel_layout) {
628 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
630 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
631 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
633 if (avctx->codec->channel_layouts[i] == 0) {
634 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
635 ret = AVERROR(EINVAL);
640 if (avctx->channel_layout && avctx->channels) {
641 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
642 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
643 ret = AVERROR(EINVAL);
646 } else if (avctx->channel_layout) {
647 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
651 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
652 ret = avctx->codec->init(avctx);
658 entangled_thread_counter--;
660 /* Release any user-supplied mutex. */
662 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
665 av_dict_free(options);
672 av_freep(&avctx->priv_data);
677 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
678 const short *samples)
680 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
681 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
684 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
685 int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
686 avctx->frame_number++;
692 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
695 if(buf_size < FF_MIN_BUFFER_SIZE){
696 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
699 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
701 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
702 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
703 avctx->frame_number++;
704 emms_c(); //needed to avoid an emms_c() call before every return;
711 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
712 const AVSubtitle *sub)
715 if(sub->start_display_time) {
716 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
719 if(sub->num_rects == 0 || !sub->rects)
721 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
722 avctx->frame_number++;
726 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
727 int *got_picture_ptr,
733 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
738 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
739 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
740 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
743 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
745 picture->pkt_dts= avpkt->dts;
748 emms_c(); //needed to avoid an emms_c() call before every return;
750 if (*got_picture_ptr)
751 avctx->frame_number++;
758 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
766 if (!avpkt->data && avpkt->size) {
767 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
768 return AVERROR(EINVAL);
771 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
772 //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
773 if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
774 av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
777 if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
778 *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
779 av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
783 ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
784 avctx->frame_number++;
792 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
800 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
802 avctx->frame_number++;
806 void avsubtitle_free(AVSubtitle *sub)
810 for (i = 0; i < sub->num_rects; i++)
812 av_freep(&sub->rects[i]->pict.data[0]);
813 av_freep(&sub->rects[i]->pict.data[1]);
814 av_freep(&sub->rects[i]->pict.data[2]);
815 av_freep(&sub->rects[i]->pict.data[3]);
816 av_freep(&sub->rects[i]->text);
817 av_freep(&sub->rects[i]->ass);
818 av_freep(&sub->rects[i]);
821 av_freep(&sub->rects);
823 memset(sub, 0, sizeof(AVSubtitle));
826 av_cold int avcodec_close(AVCodecContext *avctx)
828 /* If there is a user-supplied mutex locking routine, call it. */
830 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
834 entangled_thread_counter++;
835 if(entangled_thread_counter != 1){
836 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
837 entangled_thread_counter--;
841 if (HAVE_THREADS && avctx->thread_opaque)
842 ff_thread_free(avctx);
843 if (avctx->codec && avctx->codec->close)
844 avctx->codec->close(avctx);
845 avcodec_default_free_buffers(avctx);
846 avctx->coded_frame = NULL;
847 if (avctx->codec && avctx->codec->priv_class)
848 av_opt_free(avctx->priv_data);
850 av_freep(&avctx->priv_data);
851 if(avctx->codec && avctx->codec->encode)
852 av_freep(&avctx->extradata);
854 avctx->active_thread_type = 0;
855 entangled_thread_counter--;
857 /* Release any user-supplied mutex. */
859 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
864 AVCodec *avcodec_find_encoder(enum CodecID id)
866 AVCodec *p, *experimental=NULL;
869 if (p->encode != NULL && p->id == id) {
870 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
880 AVCodec *avcodec_find_encoder_by_name(const char *name)
887 if (p->encode != NULL && strcmp(name,p->name) == 0)
894 AVCodec *avcodec_find_decoder(enum CodecID id)
899 if (p->decode != NULL && p->id == id)
906 AVCodec *avcodec_find_decoder_by_name(const char *name)
913 if (p->decode != NULL && strcmp(name,p->name) == 0)
920 static int get_bit_rate(AVCodecContext *ctx)
925 switch(ctx->codec_type) {
926 case AVMEDIA_TYPE_VIDEO:
927 case AVMEDIA_TYPE_DATA:
928 case AVMEDIA_TYPE_SUBTITLE:
929 case AVMEDIA_TYPE_ATTACHMENT:
930 bit_rate = ctx->bit_rate;
932 case AVMEDIA_TYPE_AUDIO:
933 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
934 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
943 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
947 for (i = 0; i < 4; i++) {
948 len = snprintf(buf, buf_size,
949 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
951 buf_size = buf_size > len ? buf_size - len : 0;
958 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
960 const char *codec_name;
961 const char *profile = NULL;
965 AVRational display_aspect_ratio;
968 p = avcodec_find_encoder(enc->codec_id);
970 p = avcodec_find_decoder(enc->codec_id);
973 codec_name = p->name;
974 profile = av_get_profile_name(p, enc->profile);
975 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
976 /* fake mpeg2 transport stream codec (currently not
978 codec_name = "mpeg2ts";
979 } else if (enc->codec_name[0] != '\0') {
980 codec_name = enc->codec_name;
982 /* output avi tags */
984 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
985 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
989 switch(enc->codec_type) {
990 case AVMEDIA_TYPE_VIDEO:
991 snprintf(buf, buf_size,
993 codec_name, enc->mb_decision ? " (hq)" : "");
995 snprintf(buf + strlen(buf), buf_size - strlen(buf),
997 if (enc->pix_fmt != PIX_FMT_NONE) {
998 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1000 av_get_pix_fmt_name(enc->pix_fmt));
1003 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1005 enc->width, enc->height);
1006 if (enc->sample_aspect_ratio.num) {
1007 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1008 enc->width*enc->sample_aspect_ratio.num,
1009 enc->height*enc->sample_aspect_ratio.den,
1011 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1012 " [PAR %d:%d DAR %d:%d]",
1013 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1014 display_aspect_ratio.num, display_aspect_ratio.den);
1016 if(av_log_get_level() >= AV_LOG_DEBUG){
1017 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1018 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1020 enc->time_base.num/g, enc->time_base.den/g);
1024 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1025 ", q=%d-%d", enc->qmin, enc->qmax);
1028 case AVMEDIA_TYPE_AUDIO:
1029 snprintf(buf, buf_size,
1033 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1035 if (enc->sample_rate) {
1036 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1037 ", %d Hz", enc->sample_rate);
1039 av_strlcat(buf, ", ", buf_size);
1040 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1041 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1042 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1043 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1046 case AVMEDIA_TYPE_DATA:
1047 snprintf(buf, buf_size, "Data: %s", codec_name);
1049 case AVMEDIA_TYPE_SUBTITLE:
1050 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1052 case AVMEDIA_TYPE_ATTACHMENT:
1053 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1056 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1060 if (enc->flags & CODEC_FLAG_PASS1)
1061 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1063 if (enc->flags & CODEC_FLAG_PASS2)
1064 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1067 bitrate = get_bit_rate(enc);
1069 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1070 ", %d kb/s", bitrate / 1000);
1074 const char *av_get_profile_name(const AVCodec *codec, int profile)
1077 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1080 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1081 if (p->profile == profile)
1087 unsigned avcodec_version( void )
1089 return LIBAVCODEC_VERSION_INT;
1092 const char *avcodec_configuration(void)
1094 return LIBAV_CONFIGURATION;
1097 const char *avcodec_license(void)
1099 #define LICENSE_PREFIX "libavcodec license: "
1100 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1103 void avcodec_flush_buffers(AVCodecContext *avctx)
1105 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1106 ff_thread_flush(avctx);
1107 else if(avctx->codec->flush)
1108 avctx->codec->flush(avctx);
1111 void avcodec_default_free_buffers(AVCodecContext *s){
1114 if(s->internal_buffer==NULL) return;
1116 if (s->internal_buffer_count)
1117 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
1118 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1119 InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
1121 av_freep(&buf->base[j]);
1125 av_freep(&s->internal_buffer);
1127 s->internal_buffer_count=0;
1130 #if FF_API_OLD_FF_PICT_TYPES
1131 char av_get_pict_type_char(int pict_type){
1132 return av_get_picture_type_char(pict_type);
1136 int av_get_bits_per_sample(enum CodecID codec_id){
1138 case CODEC_ID_ADPCM_SBPRO_2:
1140 case CODEC_ID_ADPCM_SBPRO_3:
1142 case CODEC_ID_ADPCM_SBPRO_4:
1143 case CODEC_ID_ADPCM_CT:
1144 case CODEC_ID_ADPCM_IMA_WAV:
1145 case CODEC_ID_ADPCM_IMA_QT:
1146 case CODEC_ID_ADPCM_SWF:
1147 case CODEC_ID_ADPCM_MS:
1148 case CODEC_ID_ADPCM_YAMAHA:
1150 case CODEC_ID_ADPCM_G722:
1151 case CODEC_ID_PCM_ALAW:
1152 case CODEC_ID_PCM_MULAW:
1153 case CODEC_ID_PCM_S8:
1154 case CODEC_ID_PCM_U8:
1155 case CODEC_ID_PCM_ZORK:
1157 case CODEC_ID_PCM_S16BE:
1158 case CODEC_ID_PCM_S16LE:
1159 case CODEC_ID_PCM_S16LE_PLANAR:
1160 case CODEC_ID_PCM_U16BE:
1161 case CODEC_ID_PCM_U16LE:
1163 case CODEC_ID_PCM_S24DAUD:
1164 case CODEC_ID_PCM_S24BE:
1165 case CODEC_ID_PCM_S24LE:
1166 case CODEC_ID_PCM_U24BE:
1167 case CODEC_ID_PCM_U24LE:
1169 case CODEC_ID_PCM_S32BE:
1170 case CODEC_ID_PCM_S32LE:
1171 case CODEC_ID_PCM_U32BE:
1172 case CODEC_ID_PCM_U32LE:
1173 case CODEC_ID_PCM_F32BE:
1174 case CODEC_ID_PCM_F32LE:
1176 case CODEC_ID_PCM_F64BE:
1177 case CODEC_ID_PCM_F64LE:
1184 #if FF_API_OLD_SAMPLE_FMT
1185 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1186 return av_get_bytes_per_sample(sample_fmt) << 3;
1191 int ff_thread_init(AVCodecContext *s){
1196 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1210 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1212 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1216 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1218 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1219 "version to the newest one from Git. If the problem still "
1220 "occurs, it means that your file has a feature which has not "
1221 "been implemented.\n", feature);
1223 av_log_ask_for_sample(avc, NULL);
1226 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1228 va_list argument_list;
1230 va_start(argument_list, msg);
1233 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1234 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1235 "of this file to ftp://upload.libav.org/incoming/ "
1236 "and contact the libav-devel mailing list.\n");
1238 va_end(argument_list);
1241 static AVHWAccel *first_hwaccel = NULL;
1243 void av_register_hwaccel(AVHWAccel *hwaccel)
1245 AVHWAccel **p = &first_hwaccel;
1249 hwaccel->next = NULL;
1252 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1254 return hwaccel ? hwaccel->next : first_hwaccel;
1257 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1259 AVHWAccel *hwaccel=NULL;
1261 while((hwaccel= av_hwaccel_next(hwaccel))){
1262 if ( hwaccel->id == codec_id
1263 && hwaccel->pix_fmt == pix_fmt)
1269 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1271 if (ff_lockmgr_cb) {
1272 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1274 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1280 if (ff_lockmgr_cb) {
1281 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1283 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1289 int avpriv_lock_avformat(void)
1291 if (ff_lockmgr_cb) {
1292 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1298 int avpriv_unlock_avformat(void)
1300 if (ff_lockmgr_cb) {
1301 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1307 unsigned int avpriv_toupper4(unsigned int x)
1309 return toupper( x &0xFF)
1310 + (toupper((x>>8 )&0xFF)<<8 )
1311 + (toupper((x>>16)&0xFF)<<16)
1312 + (toupper((x>>24)&0xFF)<<24);
1317 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1320 return avctx->get_buffer(avctx, f);
1323 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1325 f->owner->release_buffer(f->owner, f);
1328 void ff_thread_finish_setup(AVCodecContext *avctx)
1332 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1336 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1342 #if FF_API_THREAD_INIT
1343 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1345 s->thread_count = thread_count;
1346 return ff_thread_init(s);
1350 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1352 if (codec_id <= CODEC_ID_NONE)
1353 return AVMEDIA_TYPE_UNKNOWN;
1354 else if (codec_id < CODEC_ID_FIRST_AUDIO)
1355 return AVMEDIA_TYPE_VIDEO;
1356 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1357 return AVMEDIA_TYPE_AUDIO;
1358 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1359 return AVMEDIA_TYPE_SUBTITLE;
1361 return AVMEDIA_TYPE_UNKNOWN;