• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/utils.c

Go to the documentation of this file.
00001 /*
00002  * utils for libavcodec
00003  * Copyright (c) 2001 Fabrice Bellard
00004  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00028 #include "libavutil/avstring.h"
00029 #include "libavutil/integer.h"
00030 #include "libavutil/crc.h"
00031 #include "libavutil/pixdesc.h"
00032 #include "libavutil/audioconvert.h"
00033 #include "libavutil/imgutils.h"
00034 #include "libavutil/samplefmt.h"
00035 #include "libavutil/dict.h"
00036 #include "avcodec.h"
00037 #include "dsputil.h"
00038 #include "libavutil/opt.h"
00039 #include "imgconvert.h"
00040 #include "thread.h"
00041 #include "audioconvert.h"
00042 #include "internal.h"
00043 #include <stdlib.h>
00044 #include <stdarg.h>
00045 #include <limits.h>
00046 #include <float.h>
00047 
00048 static int volatile entangled_thread_counter=0;
00049 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
00050 static void *codec_mutex;
00051 
00052 void *av_fast_realloc(void *ptr, unsigned int *size, FF_INTERNALC_MEM_TYPE min_size)
00053 {
00054     if(min_size < *size)
00055         return ptr;
00056 
00057     min_size= FFMAX(17*min_size/16 + 32, min_size);
00058 
00059     ptr= av_realloc(ptr, min_size);
00060     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
00061         min_size= 0;
00062 
00063     *size= min_size;
00064 
00065     return ptr;
00066 }
00067 
00068 void av_fast_malloc(void *ptr, unsigned int *size, FF_INTERNALC_MEM_TYPE min_size)
00069 {
00070     void **p = ptr;
00071     if (min_size < *size)
00072         return;
00073     min_size= FFMAX(17*min_size/16 + 32, min_size);
00074     av_free(*p);
00075     *p = av_malloc(min_size);
00076     if (!*p) min_size = 0;
00077     *size= min_size;
00078 }
00079 
00080 /* encoder management */
00081 static AVCodec *first_avcodec = NULL;
00082 
00083 AVCodec *av_codec_next(AVCodec *c){
00084     if(c) return c->next;
00085     else  return first_avcodec;
00086 }
00087 
00088 void avcodec_register(AVCodec *codec)
00089 {
00090     AVCodec **p;
00091     avcodec_init();
00092     p = &first_avcodec;
00093     while (*p != NULL) p = &(*p)->next;
00094     *p = codec;
00095     codec->next = NULL;
00096 }
00097 
00098 #if LIBAVCODEC_VERSION_MAJOR < 53
00099 void register_avcodec(AVCodec *codec)
00100 {
00101     avcodec_register(codec);
00102 }
00103 #endif
00104 
00105 unsigned avcodec_get_edge_width(void)
00106 {
00107     return EDGE_WIDTH;
00108 }
00109 
00110 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
00111     s->coded_width = width;
00112     s->coded_height= height;
00113     s->width = -((-width )>>s->lowres);
00114     s->height= -((-height)>>s->lowres);
00115 }
00116 
00117 typedef struct InternalBuffer{
00118     int last_pic_num;
00119     uint8_t *base[4];
00120     uint8_t *data[4];
00121     int linesize[4];
00122     int width, height;
00123     enum PixelFormat pix_fmt;
00124 }InternalBuffer;
00125 
00126 #define INTERNAL_BUFFER_SIZE (32+1)
00127 
00128 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
00129     int w_align= 1;
00130     int h_align= 1;
00131 
00132     switch(s->pix_fmt){
00133     case PIX_FMT_YUV420P:
00134     case PIX_FMT_YUYV422:
00135     case PIX_FMT_UYVY422:
00136     case PIX_FMT_YUV422P:
00137     case PIX_FMT_YUV440P:
00138     case PIX_FMT_YUV444P:
00139     case PIX_FMT_GRAY8:
00140     case PIX_FMT_GRAY16BE:
00141     case PIX_FMT_GRAY16LE:
00142     case PIX_FMT_YUVJ420P:
00143     case PIX_FMT_YUVJ422P:
00144     case PIX_FMT_YUVJ440P:
00145     case PIX_FMT_YUVJ444P:
00146     case PIX_FMT_YUVA420P:
00147     case PIX_FMT_YUV420P9LE:
00148     case PIX_FMT_YUV420P9BE:
00149     case PIX_FMT_YUV420P10LE:
00150     case PIX_FMT_YUV420P10BE:
00151     case PIX_FMT_YUV422P10LE:
00152     case PIX_FMT_YUV422P10BE:
00153     case PIX_FMT_YUV444P9LE:
00154     case PIX_FMT_YUV444P9BE:
00155     case PIX_FMT_YUV444P10LE:
00156     case PIX_FMT_YUV444P10BE:
00157         w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
00158         h_align= 16;
00159         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)
00160             h_align= 32; // interlaced is rounded up to 2 MBs
00161         break;
00162     case PIX_FMT_YUV411P:
00163     case PIX_FMT_UYYVYY411:
00164         w_align=32;
00165         h_align=8;
00166         break;
00167     case PIX_FMT_YUV410P:
00168         if(s->codec_id == CODEC_ID_SVQ1){
00169             w_align=64;
00170             h_align=64;
00171         }
00172     case PIX_FMT_RGB555:
00173         if(s->codec_id == CODEC_ID_RPZA){
00174             w_align=4;
00175             h_align=4;
00176         }
00177     case PIX_FMT_PAL8:
00178     case PIX_FMT_BGR8:
00179     case PIX_FMT_RGB8:
00180         if (s->codec_id == CODEC_ID_SMC ||
00181             s->codec_id == CODEC_ID_CINEPAK) {
00182             w_align = 4;
00183             h_align = 4;
00184         }
00185         break;
00186     case PIX_FMT_BGR24:
00187         if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
00188             w_align=4;
00189             h_align=4;
00190         }
00191         break;
00192     case PIX_FMT_RGB24:
00193         if (s->codec_id == CODEC_ID_CINEPAK) {
00194             w_align = 4;
00195             h_align = 4;
00196         }
00197         break;
00198     default:
00199         w_align= 1;
00200         h_align= 1;
00201         break;
00202     }
00203 
00204     *width = FFALIGN(*width , w_align);
00205     *height= FFALIGN(*height, h_align);
00206     if(s->codec_id == CODEC_ID_H264 || s->lowres)
00207         *height+=2; // some of the optimized chroma MC reads one line too much
00208                     // which is also done in mpeg decoders with lowres > 0
00209 
00210     linesize_align[0] =
00211     linesize_align[1] =
00212     linesize_align[2] =
00213     linesize_align[3] = STRIDE_ALIGN;
00214 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
00215 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
00216 //picture size unneccessarily in some cases. The solution here is not
00217 //pretty and better ideas are welcome!
00218 #if HAVE_MMX
00219     if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
00220        s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
00221        s->codec_id == CODEC_ID_VP6A) {
00222         linesize_align[0] =
00223         linesize_align[1] =
00224         linesize_align[2] = 16;
00225     }
00226 #endif
00227 }
00228 
00229 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
00230     int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
00231     int linesize_align[4];
00232     int align;
00233     avcodec_align_dimensions2(s, width, height, linesize_align);
00234     align = FFMAX(linesize_align[0], linesize_align[3]);
00235     linesize_align[1] <<= chroma_shift;
00236     linesize_align[2] <<= chroma_shift;
00237     align = FFMAX3(align, linesize_align[1], linesize_align[2]);
00238     *width=FFALIGN(*width, align);
00239 }
00240 
00241 #if LIBAVCODEC_VERSION_MAJOR < 53
00242 int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
00243     return av_image_check_size(w, h, 0, av_log_ctx);
00244 }
00245 #endif
00246 
00247 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
00248     int i;
00249     int w= s->width;
00250     int h= s->height;
00251     InternalBuffer *buf;
00252     int *picture_number;
00253 
00254     if(pic->data[0]!=NULL) {
00255         av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
00256         return -1;
00257     }
00258     if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
00259         av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
00260         return -1;
00261     }
00262 
00263     if(av_image_check_size(w, h, 0, s))
00264         return -1;
00265 
00266     if(s->internal_buffer==NULL){
00267         s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
00268     }
00269 #if 0
00270     s->internal_buffer= av_fast_realloc(
00271         s->internal_buffer,
00272         &s->internal_buffer_size,
00273         sizeof(InternalBuffer)*FFMAX(99,  s->internal_buffer_count+1)/*FIXME*/
00274         );
00275 #endif
00276 
00277     buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
00278     picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
00279     (*picture_number)++;
00280 
00281     if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
00282         if(s->active_thread_type&FF_THREAD_FRAME) {
00283             av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
00284             return -1;
00285         }
00286 
00287         for(i=0; i<4; i++){
00288             av_freep(&buf->base[i]);
00289             buf->data[i]= NULL;
00290         }
00291     }
00292 
00293     if(buf->base[0]){
00294         pic->age= *picture_number - buf->last_pic_num;
00295         buf->last_pic_num= *picture_number;
00296     }else{
00297         int h_chroma_shift, v_chroma_shift;
00298         int size[4] = {0};
00299         int tmpsize;
00300         int unaligned;
00301         AVPicture picture;
00302         int stride_align[4];
00303         const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
00304 
00305         avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
00306 
00307         avcodec_align_dimensions2(s, &w, &h, stride_align);
00308 
00309         if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
00310             w+= EDGE_WIDTH*2;
00311             h+= EDGE_WIDTH*2;
00312         }
00313 
00314         do {
00315             // NOTE: do not align linesizes individually, this breaks e.g. assumptions
00316             // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
00317             av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
00318             // increase alignment of w for next try (rhs gives the lowest bit set in w)
00319             w += w & ~(w-1);
00320 
00321             unaligned = 0;
00322             for (i=0; i<4; i++){
00323                 unaligned |= picture.linesize[i] % stride_align[i];
00324             }
00325         } while (unaligned);
00326 
00327         tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
00328         if (tmpsize < 0)
00329             return -1;
00330 
00331         for (i=0; i<3 && picture.data[i+1]; i++)
00332             size[i] = picture.data[i+1] - picture.data[i];
00333         size[i] = tmpsize - (picture.data[i] - picture.data[0]);
00334 
00335         buf->last_pic_num= -256*256*256*64;
00336         memset(buf->base, 0, sizeof(buf->base));
00337         memset(buf->data, 0, sizeof(buf->data));
00338 
00339         for(i=0; i<4 && size[i]; i++){
00340             const int h_shift= i==0 ? 0 : h_chroma_shift;
00341             const int v_shift= i==0 ? 0 : v_chroma_shift;
00342 
00343             buf->linesize[i]= picture.linesize[i];
00344 
00345             buf->base[i]= av_malloc(size[i]+16); //FIXME 16
00346             if(buf->base[i]==NULL) return -1;
00347             memset(buf->base[i], 128, size[i]);
00348 
00349             // no edge if EDGE EMU or not planar YUV
00350             if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
00351                 buf->data[i] = buf->base[i];
00352             else
00353                 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
00354         }
00355         if(size[1] && !size[2])
00356             ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
00357         buf->width  = s->width;
00358         buf->height = s->height;
00359         buf->pix_fmt= s->pix_fmt;
00360         pic->age= 256*256*256*64;
00361     }
00362     pic->type= FF_BUFFER_TYPE_INTERNAL;
00363 
00364     for(i=0; i<4; i++){
00365         pic->base[i]= buf->base[i];
00366         pic->data[i]= buf->data[i];
00367         pic->linesize[i]= buf->linesize[i];
00368     }
00369     s->internal_buffer_count++;
00370 
00371     if (s->pkt) {
00372         pic->pkt_pts = s->pkt->pts;
00373         pic->pkt_pos = s->pkt->pos;
00374     } else {
00375         pic->pkt_pts = AV_NOPTS_VALUE;
00376         pic->pkt_pos = -1;
00377     }
00378     pic->reordered_opaque= s->reordered_opaque;
00379     pic->sample_aspect_ratio = s->sample_aspect_ratio;
00380     pic->width               = s->width;
00381     pic->height              = s->height;
00382     pic->format              = s->pix_fmt;
00383 
00384     if(s->debug&FF_DEBUG_BUFFERS)
00385         av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
00386 
00387     return 0;
00388 }
00389 
00390 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
00391     int i;
00392     InternalBuffer *buf, *last;
00393 
00394     assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
00395     assert(s->internal_buffer_count);
00396 
00397     if(s->internal_buffer){
00398     buf = NULL; /* avoids warning */
00399     for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
00400         buf= &((InternalBuffer*)s->internal_buffer)[i];
00401         if(buf->data[0] == pic->data[0])
00402             break;
00403     }
00404     assert(i < s->internal_buffer_count);
00405     s->internal_buffer_count--;
00406     last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
00407 
00408     FFSWAP(InternalBuffer, *buf, *last);
00409     }
00410 
00411     for(i=0; i<4; i++){
00412         pic->data[i]=NULL;
00413 //        pic->base[i]=NULL;
00414     }
00415 //printf("R%X\n", pic->opaque);
00416 
00417     if(s->debug&FF_DEBUG_BUFFERS)
00418         av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
00419 }
00420 
00421 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
00422     AVFrame temp_pic;
00423     int i;
00424 
00425     /* If no picture return a new buffer */
00426     if(pic->data[0] == NULL) {
00427         /* We will copy from buffer, so must be readable */
00428         pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
00429         return s->get_buffer(s, pic);
00430     }
00431 
00432     /* If internal buffer type return the same buffer */
00433     if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
00434         if(s->pkt) pic->pkt_pts= s->pkt->pts;
00435         else       pic->pkt_pts= AV_NOPTS_VALUE;
00436         pic->reordered_opaque= s->reordered_opaque;
00437         return 0;
00438     }
00439 
00440     /*
00441      * Not internal type and reget_buffer not overridden, emulate cr buffer
00442      */
00443     temp_pic = *pic;
00444     for(i = 0; i < 4; i++)
00445         pic->data[i] = pic->base[i] = NULL;
00446     pic->opaque = NULL;
00447     /* Allocate new frame */
00448     if (s->get_buffer(s, pic))
00449         return -1;
00450     /* Copy image data from old buffer to new buffer */
00451     av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
00452              s->height);
00453     s->release_buffer(s, &temp_pic); // Release old frame
00454     return 0;
00455 }
00456 
00457 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
00458     int i;
00459 
00460     for(i=0; i<count; i++){
00461         int r= func(c, (char*)arg + i*size);
00462         if(ret) ret[i]= r;
00463     }
00464     return 0;
00465 }
00466 
00467 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
00468     int i;
00469 
00470     for(i=0; i<count; i++){
00471         int r= func(c, arg, i, 0);
00472         if(ret) ret[i]= r;
00473     }
00474     return 0;
00475 }
00476 
00477 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
00478     while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
00479         ++fmt;
00480     return fmt[0];
00481 }
00482 
00483 void avcodec_get_frame_defaults(AVFrame *pic){
00484     memset(pic, 0, sizeof(AVFrame));
00485 
00486     pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
00487     pic->pkt_pos = -1;
00488     pic->key_frame= 1;
00489     pic->sample_aspect_ratio = (AVRational){0, 1};
00490     pic->format = -1;           /* unknown */
00491 }
00492 
00493 AVFrame *avcodec_alloc_frame(void){
00494     AVFrame *pic= av_malloc(sizeof(AVFrame));
00495 
00496     if(pic==NULL) return NULL;
00497 
00498     avcodec_get_frame_defaults(pic);
00499 
00500     return pic;
00501 }
00502 
00503 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
00504 {
00505     memset(sub, 0, sizeof(*sub));
00506     sub->pts = AV_NOPTS_VALUE;
00507 }
00508 
00509 #if FF_API_AVCODEC_OPEN
00510 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
00511 {
00512     return avcodec_open2(avctx, codec, NULL);
00513 }
00514 #endif
00515 
00516 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
00517 {
00518     int ret = 0;
00519     AVDictionary *tmp = NULL;
00520 
00521     if (options)
00522         av_dict_copy(&tmp, *options, 0);
00523 
00524     /* If there is a user-supplied mutex locking routine, call it. */
00525     if (ff_lockmgr_cb) {
00526         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
00527             return -1;
00528     }
00529 
00530     entangled_thread_counter++;
00531     if(entangled_thread_counter != 1){
00532         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
00533         ret = -1;
00534         goto end;
00535     }
00536 
00537     if(avctx->codec || !codec) {
00538         ret = AVERROR(EINVAL);
00539         goto end;
00540     }
00541 
00542     if (codec->priv_data_size > 0) {
00543       if(!avctx->priv_data){
00544         avctx->priv_data = av_mallocz(codec->priv_data_size);
00545         if (!avctx->priv_data) {
00546             ret = AVERROR(ENOMEM);
00547             goto end;
00548         }
00549         if (codec->priv_class) {
00550             *(AVClass**)avctx->priv_data= codec->priv_class;
00551             av_opt_set_defaults(avctx->priv_data);
00552         }
00553       }
00554       if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
00555           goto free_and_end;
00556     } else {
00557         avctx->priv_data = NULL;
00558     }
00559     if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
00560         goto free_and_end;
00561 
00562     if(avctx->coded_width && avctx->coded_height)
00563         avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
00564     else if(avctx->width && avctx->height)
00565         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
00566 
00567     if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
00568         && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
00569            || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
00570         av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
00571         avcodec_set_dimensions(avctx, 0, 0);
00572     }
00573 
00574     /* if the decoder init function was already called previously,
00575        free the already allocated subtitle_header before overwriting it */
00576     if (codec->decode)
00577         av_freep(&avctx->subtitle_header);
00578 
00579 #define SANE_NB_CHANNELS 128U
00580     if (avctx->channels > SANE_NB_CHANNELS) {
00581         ret = AVERROR(EINVAL);
00582         goto free_and_end;
00583     }
00584 
00585     avctx->codec = codec;
00586     if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
00587         avctx->codec_id == CODEC_ID_NONE) {
00588         avctx->codec_type = codec->type;
00589         avctx->codec_id   = codec->id;
00590     }
00591     if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
00592                            && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
00593         av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
00594         ret = AVERROR(EINVAL);
00595         goto free_and_end;
00596     }
00597     avctx->frame_number = 0;
00598 
00599     if (HAVE_THREADS && !avctx->thread_opaque) {
00600         ret = ff_thread_init(avctx);
00601         if (ret < 0) {
00602             goto free_and_end;
00603         }
00604     }
00605 
00606     if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
00607         av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
00608                avctx->codec->max_lowres);
00609         ret = AVERROR(EINVAL);
00610         goto free_and_end;
00611     }
00612     if (avctx->codec->encode) {
00613         int i;
00614         if (avctx->codec->sample_fmts) {
00615             if (avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
00616                 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00617             for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
00618                 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
00619                     break;
00620             if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
00621                 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
00622                 ret = AVERROR(EINVAL);
00623                 goto free_and_end;
00624             }
00625         }
00626         if (avctx->codec->supported_samplerates) {
00627             for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
00628                 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
00629                     break;
00630             if (avctx->codec->supported_samplerates[i] == 0) {
00631                 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
00632                 ret = AVERROR(EINVAL);
00633                 goto free_and_end;
00634             }
00635         }
00636         if (avctx->codec->channel_layouts) {
00637             if (!avctx->channel_layout) {
00638                 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
00639             } else {
00640                 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
00641                     if (avctx->channel_layout == avctx->codec->channel_layouts[i])
00642                         break;
00643                 if (avctx->codec->channel_layouts[i] == 0) {
00644                     av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
00645                     ret = AVERROR(EINVAL);
00646                     goto free_and_end;
00647                 }
00648             }
00649         }
00650         if (avctx->channel_layout && avctx->channels) {
00651             if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
00652                 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
00653                 ret = AVERROR(EINVAL);
00654                 goto free_and_end;
00655             }
00656         } else if (avctx->channel_layout) {
00657             avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
00658         }
00659     }
00660 
00661     avctx->pts_correction_num_faulty_pts =
00662     avctx->pts_correction_num_faulty_dts = 0;
00663     avctx->pts_correction_last_pts =
00664     avctx->pts_correction_last_dts = INT64_MIN;
00665 
00666     if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
00667         ret = avctx->codec->init(avctx);
00668         if (ret < 0) {
00669             goto free_and_end;
00670         }
00671     }
00672 
00673     ret=0;
00674 end:
00675     entangled_thread_counter--;
00676 
00677     /* Release any user-supplied mutex. */
00678     if (ff_lockmgr_cb) {
00679         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
00680     }
00681     if (options) {
00682         av_dict_free(options);
00683         *options = tmp;
00684     }
00685 
00686     return ret;
00687 free_and_end:
00688     av_dict_free(&tmp);
00689     av_freep(&avctx->priv_data);
00690     avctx->codec= NULL;
00691     goto end;
00692 }
00693 
00694 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
00695                          const short *samples)
00696 {
00697     if(buf_size < FF_MIN_BUFFER_SIZE && 0){
00698         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
00699         return -1;
00700     }
00701     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
00702         int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
00703         avctx->frame_number++;
00704         return ret;
00705     }else
00706         return 0;
00707 }
00708 
00709 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
00710                          const AVFrame *pict)
00711 {
00712     if(buf_size < FF_MIN_BUFFER_SIZE){
00713         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
00714         return -1;
00715     }
00716     if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
00717         return -1;
00718     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
00719         int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
00720         avctx->frame_number++;
00721         emms_c(); //needed to avoid an emms_c() call before every return;
00722 
00723         return ret;
00724     }else
00725         return 0;
00726 }
00727 
00728 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
00729                             const AVSubtitle *sub)
00730 {
00731     int ret;
00732     if(sub->start_display_time) {
00733         av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
00734         return -1;
00735     }
00736 
00737     ret = avctx->codec->encode(avctx, buf, buf_size, sub);
00738     avctx->frame_number++;
00739     return ret;
00740 }
00741 
00752 static int64_t guess_correct_pts(AVCodecContext *ctx,
00753                                  int64_t reordered_pts, int64_t dts)
00754 {
00755     int64_t pts = AV_NOPTS_VALUE;
00756 
00757     if (dts != AV_NOPTS_VALUE) {
00758         ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
00759         ctx->pts_correction_last_dts = dts;
00760     }
00761     if (reordered_pts != AV_NOPTS_VALUE) {
00762         ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
00763         ctx->pts_correction_last_pts = reordered_pts;
00764     }
00765     if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
00766        && reordered_pts != AV_NOPTS_VALUE)
00767         pts = reordered_pts;
00768     else
00769         pts = dts;
00770 
00771     return pts;
00772 }
00773 
00774 #if FF_API_VIDEO_OLD
00775 int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
00776                          int *got_picture_ptr,
00777                          const uint8_t *buf, int buf_size)
00778 {
00779     AVPacket avpkt;
00780     av_init_packet(&avpkt);
00781     avpkt.data = buf;
00782     avpkt.size = buf_size;
00783     // HACK for CorePNG to decode as normal PNG by default
00784     avpkt.flags = AV_PKT_FLAG_KEY;
00785 
00786     return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
00787 }
00788 #endif
00789 
00790 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
00791                          int *got_picture_ptr,
00792                          AVPacket *avpkt)
00793 {
00794     int ret;
00795 
00796     *got_picture_ptr= 0;
00797     if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
00798         return -1;
00799 
00800     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
00801         avctx->pkt = avpkt;
00802         if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
00803              ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
00804                                           avpkt);
00805         else {
00806             ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
00807                               avpkt);
00808             picture->pkt_dts= avpkt->dts;
00809 
00810             if(!avctx->has_b_frames){
00811             picture->pkt_pos= avpkt->pos;
00812             }
00813             //FIXME these should be under if(!avctx->has_b_frames)
00814             if (!picture->sample_aspect_ratio.num)
00815                 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
00816             if (!picture->width)
00817                 picture->width = avctx->width;
00818             if (!picture->height)
00819                 picture->height = avctx->height;
00820             if (picture->format == PIX_FMT_NONE)
00821                 picture->format = avctx->pix_fmt;
00822         }
00823 
00824         emms_c(); //needed to avoid an emms_c() call before every return;
00825 
00826 
00827         if (*got_picture_ptr){
00828             avctx->frame_number++;
00829             picture->best_effort_timestamp = guess_correct_pts(avctx,
00830                                                             picture->pkt_pts,
00831                                                             picture->pkt_dts);
00832         }
00833     }else
00834         ret= 0;
00835 
00836     return ret;
00837 }
00838 
00839 #if FF_API_AUDIO_OLD
00840 int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
00841                          int *frame_size_ptr,
00842                          const uint8_t *buf, int buf_size)
00843 {
00844     AVPacket avpkt;
00845     av_init_packet(&avpkt);
00846     avpkt.data = buf;
00847     avpkt.size = buf_size;
00848 
00849     return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
00850 }
00851 #endif
00852 
00853 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
00854                          int *frame_size_ptr,
00855                          AVPacket *avpkt)
00856 {
00857     int ret;
00858 
00859     avctx->pkt = avpkt;
00860 
00861     if (!avpkt->data && avpkt->size) {
00862         av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
00863         return AVERROR(EINVAL);
00864     }
00865 
00866     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
00867         //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
00868         if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
00869             av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
00870             return -1;
00871         }
00872         if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
00873         *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
00874             av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
00875             return -1;
00876         }
00877 
00878         ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
00879         avctx->frame_number++;
00880     }else{
00881         ret= 0;
00882         *frame_size_ptr=0;
00883     }
00884     return ret;
00885 }
00886 
00887 #if FF_API_SUBTITLE_OLD
00888 int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
00889                             int *got_sub_ptr,
00890                             const uint8_t *buf, int buf_size)
00891 {
00892     AVPacket avpkt;
00893     av_init_packet(&avpkt);
00894     avpkt.data = buf;
00895     avpkt.size = buf_size;
00896 
00897     return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
00898 }
00899 #endif
00900 
00901 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
00902                             int *got_sub_ptr,
00903                             AVPacket *avpkt)
00904 {
00905     int ret;
00906 
00907     avctx->pkt = avpkt;
00908     *got_sub_ptr = 0;
00909     avcodec_get_subtitle_defaults(sub);
00910     ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
00911     if (*got_sub_ptr)
00912         avctx->frame_number++;
00913     return ret;
00914 }
00915 
00916 void avsubtitle_free(AVSubtitle *sub)
00917 {
00918     int i;
00919 
00920     for (i = 0; i < sub->num_rects; i++)
00921     {
00922         av_freep(&sub->rects[i]->pict.data[0]);
00923         av_freep(&sub->rects[i]->pict.data[1]);
00924         av_freep(&sub->rects[i]->pict.data[2]);
00925         av_freep(&sub->rects[i]->pict.data[3]);
00926         av_freep(&sub->rects[i]->text);
00927         av_freep(&sub->rects[i]->ass);
00928         av_freep(&sub->rects[i]);
00929     }
00930 
00931     av_freep(&sub->rects);
00932 
00933     memset(sub, 0, sizeof(AVSubtitle));
00934 }
00935 
00936 av_cold int avcodec_close(AVCodecContext *avctx)
00937 {
00938     /* If there is a user-supplied mutex locking routine, call it. */
00939     if (ff_lockmgr_cb) {
00940         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
00941             return -1;
00942     }
00943 
00944     entangled_thread_counter++;
00945     if(entangled_thread_counter != 1){
00946         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
00947         entangled_thread_counter--;
00948         return -1;
00949     }
00950 
00951     if (HAVE_THREADS && avctx->thread_opaque)
00952         ff_thread_free(avctx);
00953     if (avctx->codec && avctx->codec->close)
00954         avctx->codec->close(avctx);
00955     avcodec_default_free_buffers(avctx);
00956     avctx->coded_frame = NULL;
00957     if (avctx->codec && avctx->codec->priv_class)
00958         av_opt_free(avctx->priv_data);
00959     av_opt_free(avctx);
00960     av_freep(&avctx->priv_data);
00961     if(avctx->codec && avctx->codec->encode)
00962         av_freep(&avctx->extradata);
00963     avctx->codec = NULL;
00964     avctx->active_thread_type = 0;
00965     entangled_thread_counter--;
00966 
00967     /* Release any user-supplied mutex. */
00968     if (ff_lockmgr_cb) {
00969         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
00970     }
00971     return 0;
00972 }
00973 
00974 AVCodec *avcodec_find_encoder(enum CodecID id)
00975 {
00976     AVCodec *p, *experimental=NULL;
00977     p = first_avcodec;
00978     while (p) {
00979         if (p->encode != NULL && p->id == id) {
00980             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
00981                 experimental = p;
00982             } else
00983                 return p;
00984         }
00985         p = p->next;
00986     }
00987     return experimental;
00988 }
00989 
00990 AVCodec *avcodec_find_encoder_by_name(const char *name)
00991 {
00992     AVCodec *p;
00993     if (!name)
00994         return NULL;
00995     p = first_avcodec;
00996     while (p) {
00997         if (p->encode != NULL && strcmp(name,p->name) == 0)
00998             return p;
00999         p = p->next;
01000     }
01001     return NULL;
01002 }
01003 
01004 AVCodec *avcodec_find_decoder(enum CodecID id)
01005 {
01006     AVCodec *p, *experimental=NULL;
01007     p = first_avcodec;
01008     while (p) {
01009         if (p->decode != NULL && p->id == id) {
01010             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
01011                 experimental = p;
01012             } else
01013                 return p;
01014         }
01015         p = p->next;
01016     }
01017     return experimental;
01018 }
01019 
01020 AVCodec *avcodec_find_decoder_by_name(const char *name)
01021 {
01022     AVCodec *p;
01023     if (!name)
01024         return NULL;
01025     p = first_avcodec;
01026     while (p) {
01027         if (p->decode != NULL && strcmp(name,p->name) == 0)
01028             return p;
01029         p = p->next;
01030     }
01031     return NULL;
01032 }
01033 
01034 static int get_bit_rate(AVCodecContext *ctx)
01035 {
01036     int bit_rate;
01037     int bits_per_sample;
01038 
01039     switch(ctx->codec_type) {
01040     case AVMEDIA_TYPE_VIDEO:
01041     case AVMEDIA_TYPE_DATA:
01042     case AVMEDIA_TYPE_SUBTITLE:
01043     case AVMEDIA_TYPE_ATTACHMENT:
01044         bit_rate = ctx->bit_rate;
01045         break;
01046     case AVMEDIA_TYPE_AUDIO:
01047         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
01048         bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
01049         break;
01050     default:
01051         bit_rate = 0;
01052         break;
01053     }
01054     return bit_rate;
01055 }
01056 
01057 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
01058 {
01059     int i, len, ret = 0;
01060 
01061     for (i = 0; i < 4; i++) {
01062         len = snprintf(buf, buf_size,
01063                        isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
01064         buf      += len;
01065         buf_size  = buf_size > len ? buf_size - len : 0;
01066         ret      += len;
01067         codec_tag>>=8;
01068     }
01069     return ret;
01070 }
01071 
01072 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
01073 {
01074     const char *codec_name;
01075     const char *profile = NULL;
01076     AVCodec *p;
01077     char buf1[32];
01078     int bitrate;
01079     AVRational display_aspect_ratio;
01080 
01081     if (encode)
01082         p = avcodec_find_encoder(enc->codec_id);
01083     else
01084         p = avcodec_find_decoder(enc->codec_id);
01085 
01086     if (p) {
01087         codec_name = p->name;
01088         profile = av_get_profile_name(p, enc->profile);
01089     } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
01090         /* fake mpeg2 transport stream codec (currently not
01091            registered) */
01092         codec_name = "mpeg2ts";
01093     } else if (enc->codec_name[0] != '\0') {
01094         codec_name = enc->codec_name;
01095     } else {
01096         /* output avi tags */
01097         char tag_buf[32];
01098         av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
01099         snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
01100         codec_name = buf1;
01101     }
01102 
01103     switch(enc->codec_type) {
01104     case AVMEDIA_TYPE_VIDEO:
01105         snprintf(buf, buf_size,
01106                  "Video: %s%s",
01107                  codec_name, enc->mb_decision ? " (hq)" : "");
01108         if (profile)
01109             snprintf(buf + strlen(buf), buf_size - strlen(buf),
01110                      " (%s)", profile);
01111         if (enc->pix_fmt != PIX_FMT_NONE) {
01112             snprintf(buf + strlen(buf), buf_size - strlen(buf),
01113                      ", %s",
01114                      av_get_pix_fmt_name(enc->pix_fmt));
01115         }
01116         if (enc->width) {
01117             snprintf(buf + strlen(buf), buf_size - strlen(buf),
01118                      ", %dx%d",
01119                      enc->width, enc->height);
01120             if (enc->sample_aspect_ratio.num) {
01121                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
01122                           enc->width*enc->sample_aspect_ratio.num,
01123                           enc->height*enc->sample_aspect_ratio.den,
01124                           1024*1024);
01125                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01126                          " [PAR %d:%d DAR %d:%d]",
01127                          enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
01128                          display_aspect_ratio.num, display_aspect_ratio.den);
01129             }
01130             if(av_log_get_level() >= AV_LOG_DEBUG){
01131                 int g= av_gcd(enc->time_base.num, enc->time_base.den);
01132                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01133                      ", %d/%d",
01134                      enc->time_base.num/g, enc->time_base.den/g);
01135             }
01136         }
01137         if (encode) {
01138             snprintf(buf + strlen(buf), buf_size - strlen(buf),
01139                      ", q=%d-%d", enc->qmin, enc->qmax);
01140         }
01141         break;
01142     case AVMEDIA_TYPE_AUDIO:
01143         snprintf(buf, buf_size,
01144                  "Audio: %s",
01145                  codec_name);
01146         if (profile)
01147             snprintf(buf + strlen(buf), buf_size - strlen(buf),
01148                      " (%s)", profile);
01149         if (enc->sample_rate) {
01150             snprintf(buf + strlen(buf), buf_size - strlen(buf),
01151                      ", %d Hz", enc->sample_rate);
01152         }
01153         av_strlcat(buf, ", ", buf_size);
01154         av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
01155         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
01156             snprintf(buf + strlen(buf), buf_size - strlen(buf),
01157                      ", %s", av_get_sample_fmt_name(enc->sample_fmt));
01158         }
01159         break;
01160     case AVMEDIA_TYPE_DATA:
01161         snprintf(buf, buf_size, "Data: %s", codec_name);
01162         break;
01163     case AVMEDIA_TYPE_SUBTITLE:
01164         snprintf(buf, buf_size, "Subtitle: %s", codec_name);
01165         break;
01166     case AVMEDIA_TYPE_ATTACHMENT:
01167         snprintf(buf, buf_size, "Attachment: %s", codec_name);
01168         break;
01169     default:
01170         snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
01171         return;
01172     }
01173     if (encode) {
01174         if (enc->flags & CODEC_FLAG_PASS1)
01175             snprintf(buf + strlen(buf), buf_size - strlen(buf),
01176                      ", pass 1");
01177         if (enc->flags & CODEC_FLAG_PASS2)
01178             snprintf(buf + strlen(buf), buf_size - strlen(buf),
01179                      ", pass 2");
01180     }
01181     bitrate = get_bit_rate(enc);
01182     if (bitrate != 0) {
01183         snprintf(buf + strlen(buf), buf_size - strlen(buf),
01184                  ", %d kb/s", bitrate / 1000);
01185     }
01186 }
01187 
01188 const char *av_get_profile_name(const AVCodec *codec, int profile)
01189 {
01190     const AVProfile *p;
01191     if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
01192         return NULL;
01193 
01194     for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
01195         if (p->profile == profile)
01196             return p->name;
01197 
01198     return NULL;
01199 }
01200 
01201 unsigned avcodec_version( void )
01202 {
01203   return LIBAVCODEC_VERSION_INT;
01204 }
01205 
01206 const char *avcodec_configuration(void)
01207 {
01208     return FFMPEG_CONFIGURATION;
01209 }
01210 
01211 const char *avcodec_license(void)
01212 {
01213 #define LICENSE_PREFIX "libavcodec license: "
01214     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
01215 }
01216 
01217 void avcodec_init(void)
01218 {
01219     static int initialized = 0;
01220 
01221     if (initialized != 0)
01222         return;
01223     initialized = 1;
01224 
01225     dsputil_static_init();
01226 }
01227 
01228 void avcodec_flush_buffers(AVCodecContext *avctx)
01229 {
01230     if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
01231         ff_thread_flush(avctx);
01232     else if(avctx->codec->flush)
01233         avctx->codec->flush(avctx);
01234 }
01235 
01236 void avcodec_default_free_buffers(AVCodecContext *s){
01237     int i, j;
01238 
01239     if(s->internal_buffer==NULL) return;
01240 
01241     if (s->internal_buffer_count)
01242         av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
01243     for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
01244         InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
01245         for(j=0; j<4; j++){
01246             av_freep(&buf->base[j]);
01247             buf->data[j]= NULL;
01248         }
01249     }
01250     av_freep(&s->internal_buffer);
01251 
01252     s->internal_buffer_count=0;
01253 }
01254 
01255 #if FF_API_OLD_FF_PICT_TYPES
01256 char av_get_pict_type_char(int pict_type){
01257     return av_get_picture_type_char(pict_type);
01258 }
01259 #endif
01260 
01261 int av_get_bits_per_sample(enum CodecID codec_id){
01262     switch(codec_id){
01263     case CODEC_ID_ADPCM_SBPRO_2:
01264         return 2;
01265     case CODEC_ID_ADPCM_SBPRO_3:
01266         return 3;
01267     case CODEC_ID_ADPCM_SBPRO_4:
01268     case CODEC_ID_ADPCM_CT:
01269     case CODEC_ID_ADPCM_IMA_WAV:
01270     case CODEC_ID_ADPCM_MS:
01271     case CODEC_ID_ADPCM_YAMAHA:
01272         return 4;
01273     case CODEC_ID_ADPCM_G722:
01274     case CODEC_ID_PCM_ALAW:
01275     case CODEC_ID_PCM_MULAW:
01276     case CODEC_ID_PCM_S8:
01277     case CODEC_ID_PCM_U8:
01278     case CODEC_ID_PCM_ZORK:
01279         return 8;
01280     case CODEC_ID_PCM_S16BE:
01281     case CODEC_ID_PCM_S16LE:
01282     case CODEC_ID_PCM_S16LE_PLANAR:
01283     case CODEC_ID_PCM_U16BE:
01284     case CODEC_ID_PCM_U16LE:
01285         return 16;
01286     case CODEC_ID_PCM_S24DAUD:
01287     case CODEC_ID_PCM_S24BE:
01288     case CODEC_ID_PCM_S24LE:
01289     case CODEC_ID_PCM_U24BE:
01290     case CODEC_ID_PCM_U24LE:
01291         return 24;
01292     case CODEC_ID_PCM_S32BE:
01293     case CODEC_ID_PCM_S32LE:
01294     case CODEC_ID_PCM_U32BE:
01295     case CODEC_ID_PCM_U32LE:
01296     case CODEC_ID_PCM_F32BE:
01297     case CODEC_ID_PCM_F32LE:
01298         return 32;
01299     case CODEC_ID_PCM_F64BE:
01300     case CODEC_ID_PCM_F64LE:
01301         return 64;
01302     default:
01303         return 0;
01304     }
01305 }
01306 
01307 #if FF_API_OLD_SAMPLE_FMT
01308 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
01309     return av_get_bytes_per_sample(sample_fmt) << 3;
01310 }
01311 #endif
01312 
01313 #if !HAVE_THREADS
01314 int ff_thread_init(AVCodecContext *s){
01315     return -1;
01316 }
01317 #endif
01318 
01319 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
01320 {
01321     unsigned int n = 0;
01322 
01323     while(v >= 0xff) {
01324         *s++ = 0xff;
01325         v -= 0xff;
01326         n++;
01327     }
01328     *s = v;
01329     n++;
01330     return n;
01331 }
01332 
01333 #if LIBAVCODEC_VERSION_MAJOR < 53
01334 #include "libavutil/parseutils.h"
01335 
01336 int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
01337 {
01338     return av_parse_video_size(width_ptr, height_ptr, str);
01339 }
01340 
01341 int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
01342 {
01343     return av_parse_video_rate(frame_rate, arg);
01344 }
01345 #endif
01346 
01347 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
01348     int i;
01349     for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
01350     return i;
01351 }
01352 
01353 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
01354 {
01355     av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
01356             "version to the newest one from Git. If the problem still "
01357             "occurs, it means that your file has a feature which has not "
01358             "been implemented.\n", feature);
01359     if(want_sample)
01360         av_log_ask_for_sample(avc, NULL);
01361 }
01362 
01363 void av_log_ask_for_sample(void *avc, const char *msg, ...)
01364 {
01365     va_list argument_list;
01366 
01367     va_start(argument_list, msg);
01368 
01369     if (msg)
01370         av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
01371     av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
01372             "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
01373             "and contact the ffmpeg-devel mailing list.\n");
01374 
01375     va_end(argument_list);
01376 }
01377 
01378 static AVHWAccel *first_hwaccel = NULL;
01379 
01380 void av_register_hwaccel(AVHWAccel *hwaccel)
01381 {
01382     AVHWAccel **p = &first_hwaccel;
01383     while (*p)
01384         p = &(*p)->next;
01385     *p = hwaccel;
01386     hwaccel->next = NULL;
01387 }
01388 
01389 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
01390 {
01391     return hwaccel ? hwaccel->next : first_hwaccel;
01392 }
01393 
01394 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
01395 {
01396     AVHWAccel *hwaccel=NULL;
01397 
01398     while((hwaccel= av_hwaccel_next(hwaccel))){
01399         if (   hwaccel->id      == codec_id
01400             && hwaccel->pix_fmt == pix_fmt)
01401             return hwaccel;
01402     }
01403     return NULL;
01404 }
01405 
01406 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
01407 {
01408     if (ff_lockmgr_cb) {
01409         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
01410             return -1;
01411     }
01412 
01413     ff_lockmgr_cb = cb;
01414 
01415     if (ff_lockmgr_cb) {
01416         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
01417             return -1;
01418     }
01419     return 0;
01420 }
01421 
01422 unsigned int ff_toupper4(unsigned int x)
01423 {
01424     return     toupper( x     &0xFF)
01425             + (toupper((x>>8 )&0xFF)<<8 )
01426             + (toupper((x>>16)&0xFF)<<16)
01427             + (toupper((x>>24)&0xFF)<<24);
01428 }
01429 
01430 #if !HAVE_PTHREADS
01431 
01432 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
01433 {
01434     f->owner = avctx;
01435     return avctx->get_buffer(avctx, f);
01436 }
01437 
01438 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
01439 {
01440     f->owner->release_buffer(f->owner, f);
01441 }
01442 
01443 void ff_thread_finish_setup(AVCodecContext *avctx)
01444 {
01445 }
01446 
01447 void ff_thread_report_progress(AVFrame *f, int progress, int field)
01448 {
01449 }
01450 
01451 void ff_thread_await_progress(AVFrame *f, int progress, int field)
01452 {
01453 }
01454 
01455 #endif
01456 
01457 #if FF_API_THREAD_INIT
01458 int avcodec_thread_init(AVCodecContext *s, int thread_count)
01459 {
01460     s->thread_count = thread_count;
01461     return ff_thread_init(s);
01462 }
01463 
01464 void avcodec_thread_free(AVCodecContext *s)
01465 {
01466 #if HAVE_THREADS
01467     ff_thread_free(s);
01468 #endif
01469 }
01470 
01471 #endif

Generated on Fri Feb 22 2013 07:24:29 for FFmpeg by  doxygen 1.7.1