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"
32 #include "libavutil/audioconvert.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/samplefmt.h"
37 #include "libavutil/opt.h"
38 #include "imgconvert.h"
40 #include "audioconvert.h"
47 static int volatile entangled_thread_counter=0;
48 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
49 static void *codec_mutex;
51 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
56 min_size= FFMAX(17*min_size/16 + 32, min_size);
58 ptr= av_realloc(ptr, min_size);
59 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
67 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
72 min_size= FFMAX(17*min_size/16 + 32, min_size);
74 *p = av_malloc(min_size);
75 if (!*p) min_size = 0;
79 /* encoder management */
80 static AVCodec *first_avcodec = NULL;
82 AVCodec *av_codec_next(AVCodec *c){
84 else return first_avcodec;
87 void avcodec_register(AVCodec *codec)
92 while (*p != NULL) p = &(*p)->next;
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 case PIX_FMT_YUV420P9LE:
140 case PIX_FMT_YUV420P9BE:
141 case PIX_FMT_YUV420P10LE:
142 case PIX_FMT_YUV420P10BE:
143 w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
145 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)
146 h_align= 32; // interlaced is rounded up to 2 MBs
148 case PIX_FMT_YUV411P:
149 case PIX_FMT_UYYVYY411:
153 case PIX_FMT_YUV410P:
154 if(s->codec_id == CODEC_ID_SVQ1){
159 if(s->codec_id == CODEC_ID_RPZA){
166 if(s->codec_id == CODEC_ID_SMC){
172 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
183 *width = FFALIGN(*width , w_align);
184 *height= FFALIGN(*height, h_align);
185 if(s->codec_id == CODEC_ID_H264 || s->lowres)
186 *height+=2; // some of the optimized chroma MC reads one line too much
187 // which is also done in mpeg decoders with lowres > 0
192 linesize_align[3] = STRIDE_ALIGN;
193 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
194 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
195 //picture size unneccessarily in some cases. The solution here is not
196 //pretty and better ideas are welcome!
198 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
199 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
200 s->codec_id == CODEC_ID_VP6A) {
203 linesize_align[2] = 16;
208 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
209 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
210 int linesize_align[4];
212 avcodec_align_dimensions2(s, width, height, linesize_align);
213 align = FFMAX(linesize_align[0], linesize_align[3]);
214 linesize_align[1] <<= chroma_shift;
215 linesize_align[2] <<= chroma_shift;
216 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
217 *width=FFALIGN(*width, align);
220 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
227 if(pic->data[0]!=NULL) {
228 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
231 if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
232 av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
236 if(av_image_check_size(w, h, 0, s))
239 if(s->internal_buffer==NULL){
240 s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
243 s->internal_buffer= av_fast_realloc(
245 &s->internal_buffer_size,
246 sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
250 buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
251 picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
254 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
255 if(s->active_thread_type&FF_THREAD_FRAME) {
256 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
261 av_freep(&buf->base[i]);
267 pic->age= *picture_number - buf->last_pic_num;
268 buf->last_pic_num= *picture_number;
270 int h_chroma_shift, v_chroma_shift;
276 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
278 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
280 avcodec_align_dimensions2(s, &w, &h, stride_align);
282 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
288 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
289 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
290 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
291 // increase alignment of w for next try (rhs gives the lowest bit set in w)
296 unaligned |= picture.linesize[i] % stride_align[i];
300 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
304 for (i=0; i<3 && picture.data[i+1]; i++)
305 size[i] = picture.data[i+1] - picture.data[i];
306 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
308 buf->last_pic_num= -256*256*256*64;
309 memset(buf->base, 0, sizeof(buf->base));
310 memset(buf->data, 0, sizeof(buf->data));
312 for(i=0; i<4 && size[i]; i++){
313 const int h_shift= i==0 ? 0 : h_chroma_shift;
314 const int v_shift= i==0 ? 0 : v_chroma_shift;
316 buf->linesize[i]= picture.linesize[i];
318 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
319 if(buf->base[i]==NULL) return -1;
320 memset(buf->base[i], 128, size[i]);
322 // no edge if EDGE EMU or not planar YUV
323 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
324 buf->data[i] = buf->base[i];
326 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
328 if(size[1] && !size[2])
329 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
330 buf->width = s->width;
331 buf->height = s->height;
332 buf->pix_fmt= s->pix_fmt;
333 pic->age= 256*256*256*64;
335 pic->type= FF_BUFFER_TYPE_INTERNAL;
338 pic->base[i]= buf->base[i];
339 pic->data[i]= buf->data[i];
340 pic->linesize[i]= buf->linesize[i];
342 s->internal_buffer_count++;
345 pic->pkt_pts = s->pkt->pts;
346 pic->pkt_pos = s->pkt->pos;
348 pic->pkt_pts = AV_NOPTS_VALUE;
351 pic->reordered_opaque= s->reordered_opaque;
353 if(s->debug&FF_DEBUG_BUFFERS)
354 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
359 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
361 InternalBuffer *buf, *last;
363 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
364 assert(s->internal_buffer_count);
366 if(s->internal_buffer){
367 buf = NULL; /* avoids warning */
368 for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
369 buf= &((InternalBuffer*)s->internal_buffer)[i];
370 if(buf->data[0] == pic->data[0])
373 assert(i < s->internal_buffer_count);
374 s->internal_buffer_count--;
375 last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
377 FFSWAP(InternalBuffer, *buf, *last);
382 // pic->base[i]=NULL;
384 //printf("R%X\n", pic->opaque);
386 if(s->debug&FF_DEBUG_BUFFERS)
387 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
390 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
394 /* If no picture return a new buffer */
395 if(pic->data[0] == NULL) {
396 /* We will copy from buffer, so must be readable */
397 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
398 return s->get_buffer(s, pic);
401 /* If internal buffer type return the same buffer */
402 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
403 if(s->pkt) pic->pkt_pts= s->pkt->pts;
404 else pic->pkt_pts= AV_NOPTS_VALUE;
405 pic->reordered_opaque= s->reordered_opaque;
410 * Not internal type and reget_buffer not overridden, emulate cr buffer
413 for(i = 0; i < 4; i++)
414 pic->data[i] = pic->base[i] = NULL;
416 /* Allocate new frame */
417 if (s->get_buffer(s, pic))
419 /* Copy image data from old buffer to new buffer */
420 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
422 s->release_buffer(s, &temp_pic); // Release old frame
426 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
429 for(i=0; i<count; i++){
430 int r= func(c, (char*)arg + i*size);
436 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
439 for(i=0; i<count; i++){
440 int r= func(c, arg, i, 0);
446 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
447 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
452 void avcodec_get_frame_defaults(AVFrame *pic){
453 memset(pic, 0, sizeof(AVFrame));
455 pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
458 pic->sample_aspect_ratio = (AVRational){0, 1};
461 AVFrame *avcodec_alloc_frame(void){
462 AVFrame *pic= av_malloc(sizeof(AVFrame));
464 if(pic==NULL) return NULL;
466 avcodec_get_frame_defaults(pic);
471 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
473 memset(sub, 0, sizeof(*sub));
474 sub->pts = AV_NOPTS_VALUE;
477 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
481 /* If there is a user-supplied mutex locking routine, call it. */
483 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
487 entangled_thread_counter++;
488 if(entangled_thread_counter != 1){
489 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
494 if(avctx->codec || !codec) {
495 ret = AVERROR(EINVAL);
499 if (codec->priv_data_size > 0) {
500 if(!avctx->priv_data){
501 avctx->priv_data = av_mallocz(codec->priv_data_size);
502 if (!avctx->priv_data) {
503 ret = AVERROR(ENOMEM);
506 if(codec->priv_class){ //this can be droped once all user apps use avcodec_get_context_defaults3()
507 *(AVClass**)avctx->priv_data= codec->priv_class;
508 av_opt_set_defaults(avctx->priv_data);
512 avctx->priv_data = NULL;
515 if(avctx->coded_width && avctx->coded_height)
516 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
517 else if(avctx->width && avctx->height)
518 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
520 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
521 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
522 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
523 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
524 avcodec_set_dimensions(avctx, 0, 0);
527 /* if the decoder init function was already called previously,
528 free the already allocated subtitle_header before overwriting it */
530 av_freep(&avctx->subtitle_header);
532 #define SANE_NB_CHANNELS 128U
533 if (avctx->channels > SANE_NB_CHANNELS) {
534 ret = AVERROR(EINVAL);
538 avctx->codec = codec;
539 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
540 avctx->codec_id == CODEC_ID_NONE) {
541 avctx->codec_type = codec->type;
542 avctx->codec_id = codec->id;
544 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
545 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
546 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
547 ret = AVERROR(EINVAL);
550 avctx->frame_number = 0;
552 if (HAVE_THREADS && !avctx->thread_opaque) {
553 ret = ff_thread_init(avctx);
559 if (avctx->codec->max_lowres < avctx->lowres) {
560 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
561 avctx->codec->max_lowres);
562 ret = AVERROR(EINVAL);
565 if (avctx->codec->encode) {
567 if (avctx->codec->sample_fmts) {
568 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
569 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
571 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
572 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
573 ret = AVERROR(EINVAL);
577 if (avctx->codec->supported_samplerates) {
578 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
579 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
581 if (avctx->codec->supported_samplerates[i] == 0) {
582 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
583 ret = AVERROR(EINVAL);
587 if (avctx->codec->channel_layouts) {
588 if (!avctx->channel_layout) {
589 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
591 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
592 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
594 if (avctx->codec->channel_layouts[i] == 0) {
595 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
596 ret = AVERROR(EINVAL);
601 if (avctx->channel_layout && avctx->channels) {
602 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
603 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
604 ret = AVERROR(EINVAL);
607 } else if (avctx->channel_layout) {
608 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
612 avctx->pts_correction_num_faulty_pts =
613 avctx->pts_correction_num_faulty_dts = 0;
614 avctx->pts_correction_last_pts =
615 avctx->pts_correction_last_dts = INT64_MIN;
617 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
618 ret = avctx->codec->init(avctx);
626 entangled_thread_counter--;
628 /* Release any user-supplied mutex. */
630 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
634 av_freep(&avctx->priv_data);
639 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
640 const short *samples)
642 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
643 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
646 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
647 int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
648 avctx->frame_number++;
654 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
657 if(buf_size < FF_MIN_BUFFER_SIZE){
658 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
661 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
663 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
664 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
665 avctx->frame_number++;
666 emms_c(); //needed to avoid an emms_c() call before every return;
673 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
674 const AVSubtitle *sub)
677 if(sub->start_display_time) {
678 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
681 if(sub->num_rects == 0 || !sub->rects)
683 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
684 avctx->frame_number++;
689 * Attempt to guess proper monotonic timestamps for decoded video frames
690 * which might have incorrect times. Input timestamps may wrap around, in
691 * which case the output will as well.
693 * @param pts the pts field of the decoded AVPacket, as passed through
695 * @param dts the dts field of the decoded AVPacket
696 * @return one of the input values, may be AV_NOPTS_VALUE
698 static int64_t guess_correct_pts(AVCodecContext *ctx,
699 int64_t reordered_pts, int64_t dts)
701 int64_t pts = AV_NOPTS_VALUE;
703 if (dts != AV_NOPTS_VALUE) {
704 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
705 ctx->pts_correction_last_dts = dts;
707 if (reordered_pts != AV_NOPTS_VALUE) {
708 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
709 ctx->pts_correction_last_pts = reordered_pts;
711 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
712 && reordered_pts != AV_NOPTS_VALUE)
720 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
721 int *got_picture_ptr,
727 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
732 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
733 if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
734 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
737 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
739 picture->pkt_dts= avpkt->dts;
740 picture->pkt_pos= avpkt->pos;
741 if (!picture->sample_aspect_ratio.num)
742 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
745 emms_c(); //needed to avoid an emms_c() call before every return;
748 if (*got_picture_ptr){
749 avctx->frame_number++;
750 picture->best_effort_timestamp = guess_correct_pts(avctx,
760 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
768 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
769 //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
770 if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
771 av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
774 if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
775 *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
776 av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
780 ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
781 avctx->frame_number++;
789 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
797 avcodec_get_subtitle_defaults(sub);
798 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
800 avctx->frame_number++;
804 void avsubtitle_free(AVSubtitle *sub)
808 for (i = 0; i < sub->num_rects; i++)
810 av_freep(&sub->rects[i]->pict.data[0]);
811 av_freep(&sub->rects[i]->pict.data[1]);
812 av_freep(&sub->rects[i]->pict.data[2]);
813 av_freep(&sub->rects[i]->pict.data[3]);
814 av_freep(&sub->rects[i]->text);
815 av_freep(&sub->rects[i]->ass);
816 av_freep(&sub->rects[i]);
819 av_freep(&sub->rects);
821 memset(sub, 0, sizeof(AVSubtitle));
824 av_cold int avcodec_close(AVCodecContext *avctx)
826 /* If there is a user-supplied mutex locking routine, call it. */
828 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
832 entangled_thread_counter++;
833 if(entangled_thread_counter != 1){
834 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
835 entangled_thread_counter--;
839 if (HAVE_THREADS && avctx->thread_opaque)
840 ff_thread_free(avctx);
841 if (avctx->codec && avctx->codec->close)
842 avctx->codec->close(avctx);
843 avcodec_default_free_buffers(avctx);
844 avctx->coded_frame = NULL;
845 av_freep(&avctx->priv_data);
846 if(avctx->codec && avctx->codec->encode)
847 av_freep(&avctx->extradata);
849 avctx->active_thread_type = 0;
850 entangled_thread_counter--;
852 /* Release any user-supplied mutex. */
854 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
859 AVCodec *avcodec_find_encoder(enum CodecID id)
861 AVCodec *p, *experimental=NULL;
864 if (p->encode != NULL && p->id == id) {
865 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
875 AVCodec *avcodec_find_encoder_by_name(const char *name)
882 if (p->encode != NULL && strcmp(name,p->name) == 0)
889 AVCodec *avcodec_find_decoder(enum CodecID id)
894 if (p->decode != NULL && p->id == id)
901 AVCodec *avcodec_find_decoder_by_name(const char *name)
908 if (p->decode != NULL && strcmp(name,p->name) == 0)
915 static int get_bit_rate(AVCodecContext *ctx)
920 switch(ctx->codec_type) {
921 case AVMEDIA_TYPE_VIDEO:
922 case AVMEDIA_TYPE_DATA:
923 case AVMEDIA_TYPE_SUBTITLE:
924 case AVMEDIA_TYPE_ATTACHMENT:
925 bit_rate = ctx->bit_rate;
927 case AVMEDIA_TYPE_AUDIO:
928 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
929 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
938 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
942 for (i = 0; i < 4; i++) {
943 len = snprintf(buf, buf_size,
944 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
946 buf_size = buf_size > len ? buf_size - len : 0;
953 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
955 const char *codec_name;
956 const char *profile = NULL;
960 AVRational display_aspect_ratio;
963 p = avcodec_find_encoder(enc->codec_id);
965 p = avcodec_find_decoder(enc->codec_id);
968 codec_name = p->name;
969 profile = av_get_profile_name(p, enc->profile);
970 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
971 /* fake mpeg2 transport stream codec (currently not
973 codec_name = "mpeg2ts";
974 } else if (enc->codec_name[0] != '\0') {
975 codec_name = enc->codec_name;
977 /* output avi tags */
979 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
980 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
984 switch(enc->codec_type) {
985 case AVMEDIA_TYPE_VIDEO:
986 snprintf(buf, buf_size,
988 codec_name, enc->mb_decision ? " (hq)" : "");
990 snprintf(buf + strlen(buf), buf_size - strlen(buf),
992 if (enc->pix_fmt != PIX_FMT_NONE) {
993 snprintf(buf + strlen(buf), buf_size - strlen(buf),
995 avcodec_get_pix_fmt_name(enc->pix_fmt));
998 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1000 enc->width, enc->height);
1001 if (enc->sample_aspect_ratio.num) {
1002 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1003 enc->width*enc->sample_aspect_ratio.num,
1004 enc->height*enc->sample_aspect_ratio.den,
1006 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1007 " [PAR %d:%d DAR %d:%d]",
1008 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1009 display_aspect_ratio.num, display_aspect_ratio.den);
1011 if(av_log_get_level() >= AV_LOG_DEBUG){
1012 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1013 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1015 enc->time_base.num/g, enc->time_base.den/g);
1019 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1020 ", q=%d-%d", enc->qmin, enc->qmax);
1023 case AVMEDIA_TYPE_AUDIO:
1024 snprintf(buf, buf_size,
1028 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1030 if (enc->sample_rate) {
1031 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1032 ", %d Hz", enc->sample_rate);
1034 av_strlcat(buf, ", ", buf_size);
1035 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1036 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1037 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1038 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1041 case AVMEDIA_TYPE_DATA:
1042 snprintf(buf, buf_size, "Data: %s", codec_name);
1044 case AVMEDIA_TYPE_SUBTITLE:
1045 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1047 case AVMEDIA_TYPE_ATTACHMENT:
1048 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1051 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1055 if (enc->flags & CODEC_FLAG_PASS1)
1056 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1058 if (enc->flags & CODEC_FLAG_PASS2)
1059 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1062 bitrate = get_bit_rate(enc);
1064 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1065 ", %d kb/s", bitrate / 1000);
1069 const char *av_get_profile_name(const AVCodec *codec, int profile)
1072 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1075 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1076 if (p->profile == profile)
1082 unsigned avcodec_version( void )
1084 return LIBAVCODEC_VERSION_INT;
1087 const char *avcodec_configuration(void)
1089 return FFMPEG_CONFIGURATION;
1092 const char *avcodec_license(void)
1094 #define LICENSE_PREFIX "libavcodec license: "
1095 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1098 void avcodec_init(void)
1100 static int initialized = 0;
1102 if (initialized != 0)
1106 dsputil_static_init();
1109 void avcodec_flush_buffers(AVCodecContext *avctx)
1111 if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1112 ff_thread_flush(avctx);
1113 if(avctx->codec->flush)
1114 avctx->codec->flush(avctx);
1117 void avcodec_default_free_buffers(AVCodecContext *s){
1120 if(s->internal_buffer==NULL) return;
1122 if (s->internal_buffer_count)
1123 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
1124 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1125 InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
1127 av_freep(&buf->base[j]);
1131 av_freep(&s->internal_buffer);
1133 s->internal_buffer_count=0;
1136 #if FF_API_OLD_FF_PICT_TYPES
1137 char av_get_pict_type_char(int pict_type){
1138 return av_get_picture_type_char(pict_type);
1142 int av_get_bits_per_sample(enum CodecID codec_id){
1144 case CODEC_ID_ADPCM_SBPRO_2:
1146 case CODEC_ID_ADPCM_SBPRO_3:
1148 case CODEC_ID_ADPCM_SBPRO_4:
1149 case CODEC_ID_ADPCM_CT:
1150 case CODEC_ID_ADPCM_IMA_WAV:
1151 case CODEC_ID_ADPCM_MS:
1152 case CODEC_ID_ADPCM_YAMAHA:
1154 case CODEC_ID_ADPCM_G722:
1155 case CODEC_ID_PCM_ALAW:
1156 case CODEC_ID_PCM_MULAW:
1157 case CODEC_ID_PCM_S8:
1158 case CODEC_ID_PCM_U8:
1159 case CODEC_ID_PCM_ZORK:
1161 case CODEC_ID_PCM_S16BE:
1162 case CODEC_ID_PCM_S16LE:
1163 case CODEC_ID_PCM_S16LE_PLANAR:
1164 case CODEC_ID_PCM_U16BE:
1165 case CODEC_ID_PCM_U16LE:
1167 case CODEC_ID_PCM_S24DAUD:
1168 case CODEC_ID_PCM_S24BE:
1169 case CODEC_ID_PCM_S24LE:
1170 case CODEC_ID_PCM_U24BE:
1171 case CODEC_ID_PCM_U24LE:
1173 case CODEC_ID_PCM_S32BE:
1174 case CODEC_ID_PCM_S32LE:
1175 case CODEC_ID_PCM_U32BE:
1176 case CODEC_ID_PCM_U32LE:
1177 case CODEC_ID_PCM_F32BE:
1178 case CODEC_ID_PCM_F32LE:
1180 case CODEC_ID_PCM_F64BE:
1181 case CODEC_ID_PCM_F64LE:
1188 #if FF_API_OLD_SAMPLE_FMT
1189 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1190 return av_get_bits_per_sample_fmt(sample_fmt);
1195 int ff_thread_init(AVCodecContext *s){
1200 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1214 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1216 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1220 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1222 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1223 "version to the newest one from Git. If the problem still "
1224 "occurs, it means that your file has a feature which has not "
1225 "been implemented.\n", feature);
1227 av_log_ask_for_sample(avc, NULL);
1230 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1232 va_list argument_list;
1234 va_start(argument_list, msg);
1237 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1238 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1239 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1240 "and contact the ffmpeg-devel mailing list.\n");
1242 va_end(argument_list);
1245 static AVHWAccel *first_hwaccel = NULL;
1247 void av_register_hwaccel(AVHWAccel *hwaccel)
1249 AVHWAccel **p = &first_hwaccel;
1253 hwaccel->next = NULL;
1256 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1258 return hwaccel ? hwaccel->next : first_hwaccel;
1261 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1263 AVHWAccel *hwaccel=NULL;
1265 while((hwaccel= av_hwaccel_next(hwaccel))){
1266 if ( hwaccel->id == codec_id
1267 && hwaccel->pix_fmt == pix_fmt)
1273 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1275 if (ff_lockmgr_cb) {
1276 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1282 if (ff_lockmgr_cb) {
1283 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1289 unsigned int ff_toupper4(unsigned int x)
1291 return toupper( x &0xFF)
1292 + (toupper((x>>8 )&0xFF)<<8 )
1293 + (toupper((x>>16)&0xFF)<<16)
1294 + (toupper((x>>24)&0xFF)<<24);
1299 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1302 return avctx->get_buffer(avctx, f);
1305 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1307 f->owner->release_buffer(f->owner, f);
1310 void ff_thread_finish_setup(AVCodecContext *avctx)
1314 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1318 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1324 #if FF_API_THREAD_INIT
1325 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1327 s->thread_count = thread_count;
1328 return ff_thread_init(s);