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"
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+1)
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 case PIX_FMT_YUV422P10LE:
144 case PIX_FMT_YUV422P10BE:
145 case PIX_FMT_YUV444P9LE:
146 case PIX_FMT_YUV444P9BE:
147 case PIX_FMT_YUV444P10LE:
148 case PIX_FMT_YUV444P10BE:
149 w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
151 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)
152 h_align= 32; // interlaced is rounded up to 2 MBs
154 case PIX_FMT_YUV411P:
155 case PIX_FMT_UYYVYY411:
159 case PIX_FMT_YUV410P:
160 if(s->codec_id == CODEC_ID_SVQ1){
165 if(s->codec_id == CODEC_ID_RPZA){
172 if(s->codec_id == CODEC_ID_SMC){
178 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
189 *width = FFALIGN(*width , w_align);
190 *height= FFALIGN(*height, h_align);
191 if(s->codec_id == CODEC_ID_H264 || s->lowres)
192 *height+=2; // some of the optimized chroma MC reads one line too much
193 // which is also done in mpeg decoders with lowres > 0
198 linesize_align[3] = STRIDE_ALIGN;
199 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
200 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
201 //picture size unneccessarily in some cases. The solution here is not
202 //pretty and better ideas are welcome!
204 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
205 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
206 s->codec_id == CODEC_ID_VP6A) {
209 linesize_align[2] = 16;
214 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
215 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
216 int linesize_align[4];
218 avcodec_align_dimensions2(s, width, height, linesize_align);
219 align = FFMAX(linesize_align[0], linesize_align[3]);
220 linesize_align[1] <<= chroma_shift;
221 linesize_align[2] <<= chroma_shift;
222 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
223 *width=FFALIGN(*width, align);
226 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
233 if(pic->data[0]!=NULL) {
234 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
237 if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
238 av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
242 if(av_image_check_size(w, h, 0, s))
245 if(s->internal_buffer==NULL){
246 s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
249 s->internal_buffer= av_fast_realloc(
251 &s->internal_buffer_size,
252 sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
256 buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
257 picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
260 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
261 if(s->active_thread_type&FF_THREAD_FRAME) {
262 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
267 av_freep(&buf->base[i]);
273 pic->age= *picture_number - buf->last_pic_num;
274 buf->last_pic_num= *picture_number;
276 int h_chroma_shift, v_chroma_shift;
282 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
284 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
286 avcodec_align_dimensions2(s, &w, &h, stride_align);
288 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
294 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
295 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
296 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
297 // increase alignment of w for next try (rhs gives the lowest bit set in w)
302 unaligned |= picture.linesize[i] % stride_align[i];
306 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
310 for (i=0; i<3 && picture.data[i+1]; i++)
311 size[i] = picture.data[i+1] - picture.data[i];
312 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
314 buf->last_pic_num= -256*256*256*64;
315 memset(buf->base, 0, sizeof(buf->base));
316 memset(buf->data, 0, sizeof(buf->data));
318 for(i=0; i<4 && size[i]; i++){
319 const int h_shift= i==0 ? 0 : h_chroma_shift;
320 const int v_shift= i==0 ? 0 : v_chroma_shift;
322 buf->linesize[i]= picture.linesize[i];
324 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
325 if(buf->base[i]==NULL) return -1;
326 memset(buf->base[i], 128, size[i]);
328 // no edge if EDGE EMU or not planar YUV
329 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
330 buf->data[i] = buf->base[i];
332 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
334 if(size[1] && !size[2])
335 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
336 buf->width = s->width;
337 buf->height = s->height;
338 buf->pix_fmt= s->pix_fmt;
339 pic->age= 256*256*256*64;
341 pic->type= FF_BUFFER_TYPE_INTERNAL;
344 pic->base[i]= buf->base[i];
345 pic->data[i]= buf->data[i];
346 pic->linesize[i]= buf->linesize[i];
348 s->internal_buffer_count++;
350 if(s->pkt) pic->pkt_pts= s->pkt->pts;
351 else pic->pkt_pts= AV_NOPTS_VALUE;
352 pic->reordered_opaque= s->reordered_opaque;
354 if(s->debug&FF_DEBUG_BUFFERS)
355 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
360 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
362 InternalBuffer *buf, *last;
364 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
365 assert(s->internal_buffer_count);
367 if(s->internal_buffer){
368 buf = NULL; /* avoids warning */
369 for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
370 buf= &((InternalBuffer*)s->internal_buffer)[i];
371 if(buf->data[0] == pic->data[0])
374 assert(i < s->internal_buffer_count);
375 s->internal_buffer_count--;
376 last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
378 FFSWAP(InternalBuffer, *buf, *last);
383 // pic->base[i]=NULL;
385 //printf("R%X\n", pic->opaque);
387 if(s->debug&FF_DEBUG_BUFFERS)
388 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
391 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
395 /* If no picture return a new buffer */
396 if(pic->data[0] == NULL) {
397 /* We will copy from buffer, so must be readable */
398 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
399 return s->get_buffer(s, pic);
402 /* If internal buffer type return the same buffer */
403 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
404 if(s->pkt) pic->pkt_pts= s->pkt->pts;
405 else pic->pkt_pts= AV_NOPTS_VALUE;
406 pic->reordered_opaque= s->reordered_opaque;
411 * Not internal type and reget_buffer not overridden, emulate cr buffer
414 for(i = 0; i < 4; i++)
415 pic->data[i] = pic->base[i] = NULL;
417 /* Allocate new frame */
418 if (s->get_buffer(s, pic))
420 /* Copy image data from old buffer to new buffer */
421 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
423 s->release_buffer(s, &temp_pic); // Release old frame
427 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
430 for(i=0; i<count; i++){
431 int r= func(c, (char*)arg + i*size);
437 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
440 for(i=0; i<count; i++){
441 int r= func(c, arg, i, 0);
447 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
448 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
453 void avcodec_get_frame_defaults(AVFrame *pic){
454 memset(pic, 0, sizeof(AVFrame));
456 pic->pts= AV_NOPTS_VALUE;
460 AVFrame *avcodec_alloc_frame(void){
461 AVFrame *pic= av_malloc(sizeof(AVFrame));
463 if(pic==NULL) return NULL;
465 avcodec_get_frame_defaults(pic);
470 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
474 /* If there is a user-supplied mutex locking routine, call it. */
476 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
480 entangled_thread_counter++;
481 if(entangled_thread_counter != 1){
482 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
487 if(avctx->codec || !codec) {
488 ret = AVERROR(EINVAL);
492 if (codec->priv_data_size > 0) {
493 if(!avctx->priv_data){
494 avctx->priv_data = av_mallocz(codec->priv_data_size);
495 if (!avctx->priv_data) {
496 ret = AVERROR(ENOMEM);
499 if(codec->priv_class){ //this can be droped once all user apps use avcodec_get_context_defaults3()
500 *(AVClass**)avctx->priv_data= codec->priv_class;
501 av_opt_set_defaults(avctx->priv_data);
505 avctx->priv_data = NULL;
508 if(avctx->coded_width && avctx->coded_height)
509 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
510 else if(avctx->width && avctx->height)
511 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
513 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
514 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
515 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
516 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
517 avcodec_set_dimensions(avctx, 0, 0);
520 /* if the decoder init function was already called previously,
521 free the already allocated subtitle_header before overwriting it */
523 av_freep(&avctx->subtitle_header);
525 #define SANE_NB_CHANNELS 128U
526 if (avctx->channels > SANE_NB_CHANNELS) {
527 ret = AVERROR(EINVAL);
531 avctx->codec = codec;
532 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
533 avctx->codec_id == CODEC_ID_NONE) {
534 avctx->codec_type = codec->type;
535 avctx->codec_id = codec->id;
537 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
538 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
539 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
540 ret = AVERROR(EINVAL);
543 avctx->frame_number = 0;
545 if (HAVE_THREADS && !avctx->thread_opaque) {
546 ret = ff_thread_init(avctx);
552 if (avctx->codec->max_lowres < avctx->lowres) {
553 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
554 avctx->codec->max_lowres);
555 ret = AVERROR(EINVAL);
558 if (avctx->codec->encode) {
560 if (avctx->codec->sample_fmts) {
561 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
562 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
564 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
565 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
566 ret = AVERROR(EINVAL);
570 if (avctx->codec->supported_samplerates) {
571 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
572 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
574 if (avctx->codec->supported_samplerates[i] == 0) {
575 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
576 ret = AVERROR(EINVAL);
580 if (avctx->codec->channel_layouts) {
581 if (!avctx->channel_layout) {
582 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
584 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
585 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
587 if (avctx->codec->channel_layouts[i] == 0) {
588 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
589 ret = AVERROR(EINVAL);
594 if (avctx->channel_layout && avctx->channels) {
595 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
596 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
597 ret = AVERROR(EINVAL);
600 } else if (avctx->channel_layout) {
601 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
605 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
606 ret = avctx->codec->init(avctx);
612 entangled_thread_counter--;
614 /* Release any user-supplied mutex. */
616 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
620 av_freep(&avctx->priv_data);
625 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
626 const short *samples)
628 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
629 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
632 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
633 int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
634 avctx->frame_number++;
640 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
643 if(buf_size < FF_MIN_BUFFER_SIZE){
644 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
647 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
649 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
650 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
651 avctx->frame_number++;
652 emms_c(); //needed to avoid an emms_c() call before every return;
659 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
660 const AVSubtitle *sub)
663 if(sub->start_display_time) {
664 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
667 if(sub->num_rects == 0 || !sub->rects)
669 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
670 avctx->frame_number++;
674 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
675 int *got_picture_ptr,
681 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
686 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
687 if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
688 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
691 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
693 picture->pkt_dts= avpkt->dts;
696 emms_c(); //needed to avoid an emms_c() call before every return;
698 if (*got_picture_ptr)
699 avctx->frame_number++;
706 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
714 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
715 //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
716 if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
717 av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
720 if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
721 *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
722 av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
726 ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
727 avctx->frame_number++;
735 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
743 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
745 avctx->frame_number++;
749 void avsubtitle_free(AVSubtitle *sub)
753 for (i = 0; i < sub->num_rects; i++)
755 av_freep(&sub->rects[i]->pict.data[0]);
756 av_freep(&sub->rects[i]->pict.data[1]);
757 av_freep(&sub->rects[i]->pict.data[2]);
758 av_freep(&sub->rects[i]->pict.data[3]);
759 av_freep(&sub->rects[i]->text);
760 av_freep(&sub->rects[i]->ass);
761 av_freep(&sub->rects[i]);
764 av_freep(&sub->rects);
766 memset(sub, 0, sizeof(AVSubtitle));
769 av_cold int avcodec_close(AVCodecContext *avctx)
771 /* If there is a user-supplied mutex locking routine, call it. */
773 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
777 entangled_thread_counter++;
778 if(entangled_thread_counter != 1){
779 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
780 entangled_thread_counter--;
784 if (HAVE_THREADS && avctx->thread_opaque)
785 ff_thread_free(avctx);
786 if (avctx->codec && avctx->codec->close)
787 avctx->codec->close(avctx);
788 avcodec_default_free_buffers(avctx);
789 avctx->coded_frame = NULL;
790 if (avctx->codec && avctx->codec->priv_class)
791 av_opt_free(avctx->priv_data);
793 av_freep(&avctx->priv_data);
794 if(avctx->codec && avctx->codec->encode)
795 av_freep(&avctx->extradata);
797 avctx->active_thread_type = 0;
798 entangled_thread_counter--;
800 /* Release any user-supplied mutex. */
802 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
807 AVCodec *avcodec_find_encoder(enum CodecID id)
809 AVCodec *p, *experimental=NULL;
812 if (p->encode != NULL && p->id == id) {
813 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
823 AVCodec *avcodec_find_encoder_by_name(const char *name)
830 if (p->encode != NULL && strcmp(name,p->name) == 0)
837 AVCodec *avcodec_find_decoder(enum CodecID id)
842 if (p->decode != NULL && p->id == id)
849 AVCodec *avcodec_find_decoder_by_name(const char *name)
856 if (p->decode != NULL && strcmp(name,p->name) == 0)
863 static int get_bit_rate(AVCodecContext *ctx)
868 switch(ctx->codec_type) {
869 case AVMEDIA_TYPE_VIDEO:
870 case AVMEDIA_TYPE_DATA:
871 case AVMEDIA_TYPE_SUBTITLE:
872 case AVMEDIA_TYPE_ATTACHMENT:
873 bit_rate = ctx->bit_rate;
875 case AVMEDIA_TYPE_AUDIO:
876 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
877 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
886 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
890 for (i = 0; i < 4; i++) {
891 len = snprintf(buf, buf_size,
892 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
894 buf_size = buf_size > len ? buf_size - len : 0;
901 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
903 const char *codec_name;
904 const char *profile = NULL;
908 AVRational display_aspect_ratio;
911 p = avcodec_find_encoder(enc->codec_id);
913 p = avcodec_find_decoder(enc->codec_id);
916 codec_name = p->name;
917 profile = av_get_profile_name(p, enc->profile);
918 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
919 /* fake mpeg2 transport stream codec (currently not
921 codec_name = "mpeg2ts";
922 } else if (enc->codec_name[0] != '\0') {
923 codec_name = enc->codec_name;
925 /* output avi tags */
927 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
928 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
932 switch(enc->codec_type) {
933 case AVMEDIA_TYPE_VIDEO:
934 snprintf(buf, buf_size,
936 codec_name, enc->mb_decision ? " (hq)" : "");
938 snprintf(buf + strlen(buf), buf_size - strlen(buf),
940 if (enc->pix_fmt != PIX_FMT_NONE) {
941 snprintf(buf + strlen(buf), buf_size - strlen(buf),
943 av_get_pix_fmt_name(enc->pix_fmt));
946 snprintf(buf + strlen(buf), buf_size - strlen(buf),
948 enc->width, enc->height);
949 if (enc->sample_aspect_ratio.num) {
950 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
951 enc->width*enc->sample_aspect_ratio.num,
952 enc->height*enc->sample_aspect_ratio.den,
954 snprintf(buf + strlen(buf), buf_size - strlen(buf),
955 " [PAR %d:%d DAR %d:%d]",
956 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
957 display_aspect_ratio.num, display_aspect_ratio.den);
959 if(av_log_get_level() >= AV_LOG_DEBUG){
960 int g= av_gcd(enc->time_base.num, enc->time_base.den);
961 snprintf(buf + strlen(buf), buf_size - strlen(buf),
963 enc->time_base.num/g, enc->time_base.den/g);
967 snprintf(buf + strlen(buf), buf_size - strlen(buf),
968 ", q=%d-%d", enc->qmin, enc->qmax);
971 case AVMEDIA_TYPE_AUDIO:
972 snprintf(buf, buf_size,
976 snprintf(buf + strlen(buf), buf_size - strlen(buf),
978 if (enc->sample_rate) {
979 snprintf(buf + strlen(buf), buf_size - strlen(buf),
980 ", %d Hz", enc->sample_rate);
982 av_strlcat(buf, ", ", buf_size);
983 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
984 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
985 snprintf(buf + strlen(buf), buf_size - strlen(buf),
986 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
989 case AVMEDIA_TYPE_DATA:
990 snprintf(buf, buf_size, "Data: %s", codec_name);
992 case AVMEDIA_TYPE_SUBTITLE:
993 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
995 case AVMEDIA_TYPE_ATTACHMENT:
996 snprintf(buf, buf_size, "Attachment: %s", codec_name);
999 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1003 if (enc->flags & CODEC_FLAG_PASS1)
1004 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1006 if (enc->flags & CODEC_FLAG_PASS2)
1007 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1010 bitrate = get_bit_rate(enc);
1012 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1013 ", %d kb/s", bitrate / 1000);
1017 const char *av_get_profile_name(const AVCodec *codec, int profile)
1020 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1023 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1024 if (p->profile == profile)
1030 unsigned avcodec_version( void )
1032 return LIBAVCODEC_VERSION_INT;
1035 const char *avcodec_configuration(void)
1037 return LIBAV_CONFIGURATION;
1040 const char *avcodec_license(void)
1042 #define LICENSE_PREFIX "libavcodec license: "
1043 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1046 void avcodec_init(void)
1048 static int initialized = 0;
1050 if (initialized != 0)
1054 dsputil_static_init();
1057 void avcodec_flush_buffers(AVCodecContext *avctx)
1059 if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1060 ff_thread_flush(avctx);
1061 if(avctx->codec->flush)
1062 avctx->codec->flush(avctx);
1065 void avcodec_default_free_buffers(AVCodecContext *s){
1068 if(s->internal_buffer==NULL) return;
1070 if (s->internal_buffer_count)
1071 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
1072 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1073 InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
1075 av_freep(&buf->base[j]);
1079 av_freep(&s->internal_buffer);
1081 s->internal_buffer_count=0;
1084 #if FF_API_OLD_FF_PICT_TYPES
1085 char av_get_pict_type_char(int pict_type){
1086 return av_get_picture_type_char(pict_type);
1090 int av_get_bits_per_sample(enum CodecID codec_id){
1092 case CODEC_ID_ADPCM_SBPRO_2:
1094 case CODEC_ID_ADPCM_SBPRO_3:
1096 case CODEC_ID_ADPCM_SBPRO_4:
1097 case CODEC_ID_ADPCM_CT:
1098 case CODEC_ID_ADPCM_IMA_WAV:
1099 case CODEC_ID_ADPCM_MS:
1100 case CODEC_ID_ADPCM_YAMAHA:
1102 case CODEC_ID_ADPCM_G722:
1103 case CODEC_ID_PCM_ALAW:
1104 case CODEC_ID_PCM_MULAW:
1105 case CODEC_ID_PCM_S8:
1106 case CODEC_ID_PCM_U8:
1107 case CODEC_ID_PCM_ZORK:
1109 case CODEC_ID_PCM_S16BE:
1110 case CODEC_ID_PCM_S16LE:
1111 case CODEC_ID_PCM_S16LE_PLANAR:
1112 case CODEC_ID_PCM_U16BE:
1113 case CODEC_ID_PCM_U16LE:
1115 case CODEC_ID_PCM_S24DAUD:
1116 case CODEC_ID_PCM_S24BE:
1117 case CODEC_ID_PCM_S24LE:
1118 case CODEC_ID_PCM_U24BE:
1119 case CODEC_ID_PCM_U24LE:
1121 case CODEC_ID_PCM_S32BE:
1122 case CODEC_ID_PCM_S32LE:
1123 case CODEC_ID_PCM_U32BE:
1124 case CODEC_ID_PCM_U32LE:
1125 case CODEC_ID_PCM_F32BE:
1126 case CODEC_ID_PCM_F32LE:
1128 case CODEC_ID_PCM_F64BE:
1129 case CODEC_ID_PCM_F64LE:
1136 #if FF_API_OLD_SAMPLE_FMT
1137 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1138 return av_get_bytes_per_sample(sample_fmt) << 3;
1143 int ff_thread_init(AVCodecContext *s){
1148 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1162 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1164 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1168 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1170 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1171 "version to the newest one from Git. If the problem still "
1172 "occurs, it means that your file has a feature which has not "
1173 "been implemented.\n", feature);
1175 av_log_ask_for_sample(avc, NULL);
1178 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1180 va_list argument_list;
1182 va_start(argument_list, msg);
1185 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1186 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1187 "of this file to ftp://upload.libav.org/incoming/ "
1188 "and contact the libav-devel mailing list.\n");
1190 va_end(argument_list);
1193 static AVHWAccel *first_hwaccel = NULL;
1195 void av_register_hwaccel(AVHWAccel *hwaccel)
1197 AVHWAccel **p = &first_hwaccel;
1201 hwaccel->next = NULL;
1204 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1206 return hwaccel ? hwaccel->next : first_hwaccel;
1209 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1211 AVHWAccel *hwaccel=NULL;
1213 while((hwaccel= av_hwaccel_next(hwaccel))){
1214 if ( hwaccel->id == codec_id
1215 && hwaccel->pix_fmt == pix_fmt)
1221 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1223 if (ff_lockmgr_cb) {
1224 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1230 if (ff_lockmgr_cb) {
1231 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1237 unsigned int ff_toupper4(unsigned int x)
1239 return toupper( x &0xFF)
1240 + (toupper((x>>8 )&0xFF)<<8 )
1241 + (toupper((x>>16)&0xFF)<<16)
1242 + (toupper((x>>24)&0xFF)<<24);
1247 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1250 return avctx->get_buffer(avctx, f);
1253 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1255 f->owner->release_buffer(f->owner, f);
1258 void ff_thread_finish_setup(AVCodecContext *avctx)
1262 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1266 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1272 #if FF_API_THREAD_INIT
1273 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1275 s->thread_count = thread_count;
1276 return ff_thread_init(s);