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/avassert.h"
00029 #include "libavutil/avstring.h"
00030 #include "libavutil/crc.h"
00031 #include "libavutil/mathematics.h"
00032 #include "libavutil/pixdesc.h"
00033 #include "libavutil/audioconvert.h"
00034 #include "libavutil/imgutils.h"
00035 #include "libavutil/samplefmt.h"
00036 #include "libavutil/dict.h"
00037 #include "libavutil/avassert.h"
00038 #include "avcodec.h"
00039 #include "dsputil.h"
00040 #include "libavutil/opt.h"
00041 #include "imgconvert.h"
00042 #include "thread.h"
00043 #include "audioconvert.h"
00044 #include "internal.h"
00045 #include "bytestream.h"
00046 #include <stdlib.h>
00047 #include <stdarg.h>
00048 #include <limits.h>
00049 #include <float.h>
00050 
00051 static int volatile entangled_thread_counter=0;
00052 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
00053 static void *codec_mutex;
00054 static void *avformat_mutex;
00055 
00056 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
00057 {
00058     if(min_size < *size)
00059         return ptr;
00060 
00061     min_size= FFMAX(17*min_size/16 + 32, min_size);
00062 
00063     ptr= av_realloc(ptr, min_size);
00064     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
00065         min_size= 0;
00066 
00067     *size= min_size;
00068 
00069     return ptr;
00070 }
00071 
00072 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
00073 {
00074     void **p = ptr;
00075     if (min_size < *size)
00076         return 0;
00077     min_size= FFMAX(17*min_size/16 + 32, min_size);
00078     av_free(*p);
00079     *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
00080     if (!*p) min_size = 0;
00081     *size= min_size;
00082     return 1;
00083 }
00084 
00085 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
00086 {
00087     ff_fast_malloc(ptr, size, min_size, 0);
00088 }
00089 
00090 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
00091 {
00092     uint8_t **p = ptr;
00093     if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
00094         *p = NULL;
00095         *size = 0;
00096         return;
00097     }
00098     if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
00099         memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00100 }
00101 
00102 /* encoder management */
00103 static AVCodec *first_avcodec = NULL;
00104 
00105 AVCodec *av_codec_next(AVCodec *c){
00106     if(c) return c->next;
00107     else  return first_avcodec;
00108 }
00109 
00110 #if !FF_API_AVCODEC_INIT
00111 static
00112 #endif
00113 void avcodec_init(void)
00114 {
00115     static int initialized = 0;
00116 
00117     if (initialized != 0)
00118         return;
00119     initialized = 1;
00120 
00121     dsputil_static_init();
00122 }
00123 
00124 static av_always_inline int codec_is_encoder(AVCodec *codec)
00125 {
00126     return codec && (codec->encode || codec->encode2);
00127 }
00128 
00129 static av_always_inline int codec_is_decoder(AVCodec *codec)
00130 {
00131     return codec && codec->decode;
00132 }
00133 
00134 void avcodec_register(AVCodec *codec)
00135 {
00136     AVCodec **p;
00137     avcodec_init();
00138     p = &first_avcodec;
00139     while (*p != NULL) p = &(*p)->next;
00140     *p = codec;
00141     codec->next = NULL;
00142 
00143     if (codec->init_static_data)
00144         codec->init_static_data(codec);
00145 }
00146 
00147 unsigned avcodec_get_edge_width(void)
00148 {
00149     return EDGE_WIDTH;
00150 }
00151 
00152 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
00153     s->coded_width = width;
00154     s->coded_height= height;
00155     s->width = -((-width )>>s->lowres);
00156     s->height= -((-height)>>s->lowres);
00157 }
00158 
00159 #define INTERNAL_BUFFER_SIZE (32+1)
00160 
00161 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
00162                                int linesize_align[AV_NUM_DATA_POINTERS])
00163 {
00164     int i;
00165     int w_align= 1;
00166     int h_align= 1;
00167 
00168     switch(s->pix_fmt){
00169     case PIX_FMT_YUV420P:
00170     case PIX_FMT_YUYV422:
00171     case PIX_FMT_UYVY422:
00172     case PIX_FMT_YUV422P:
00173     case PIX_FMT_YUV440P:
00174     case PIX_FMT_YUV444P:
00175     case PIX_FMT_GBRP:
00176     case PIX_FMT_GRAY8:
00177     case PIX_FMT_GRAY16BE:
00178     case PIX_FMT_GRAY16LE:
00179     case PIX_FMT_YUVJ420P:
00180     case PIX_FMT_YUVJ422P:
00181     case PIX_FMT_YUVJ440P:
00182     case PIX_FMT_YUVJ444P:
00183     case PIX_FMT_YUVA420P:
00184     case PIX_FMT_YUV420P9LE:
00185     case PIX_FMT_YUV420P9BE:
00186     case PIX_FMT_YUV420P10LE:
00187     case PIX_FMT_YUV420P10BE:
00188     case PIX_FMT_YUV422P9LE:
00189     case PIX_FMT_YUV422P9BE:
00190     case PIX_FMT_YUV422P10LE:
00191     case PIX_FMT_YUV422P10BE:
00192     case PIX_FMT_YUV444P9LE:
00193     case PIX_FMT_YUV444P9BE:
00194     case PIX_FMT_YUV444P10LE:
00195     case PIX_FMT_YUV444P10BE:
00196     case PIX_FMT_GBRP9LE:
00197     case PIX_FMT_GBRP9BE:
00198     case PIX_FMT_GBRP10LE:
00199     case PIX_FMT_GBRP10BE:
00200         w_align = 16; //FIXME assume 16 pixel per macroblock
00201         h_align = 16 * 2; // interlaced needs 2 macroblocks height
00202         break;
00203     case PIX_FMT_YUV411P:
00204     case PIX_FMT_UYYVYY411:
00205         w_align=32;
00206         h_align=8;
00207         break;
00208     case PIX_FMT_YUV410P:
00209         if(s->codec_id == CODEC_ID_SVQ1){
00210             w_align=64;
00211             h_align=64;
00212         }
00213     case PIX_FMT_RGB555:
00214         if(s->codec_id == CODEC_ID_RPZA){
00215             w_align=4;
00216             h_align=4;
00217         }
00218     case PIX_FMT_PAL8:
00219     case PIX_FMT_BGR8:
00220     case PIX_FMT_RGB8:
00221         if(s->codec_id == CODEC_ID_SMC){
00222             w_align=4;
00223             h_align=4;
00224         }
00225         break;
00226     case PIX_FMT_BGR24:
00227         if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
00228             w_align=4;
00229             h_align=4;
00230         }
00231         break;
00232     default:
00233         w_align= 1;
00234         h_align= 1;
00235         break;
00236     }
00237 
00238     if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
00239         w_align= FFMAX(w_align, 8);
00240     }
00241 
00242     *width = FFALIGN(*width , w_align);
00243     *height= FFALIGN(*height, h_align);
00244     if(s->codec_id == CODEC_ID_H264 || s->lowres)
00245         *height+=2; // some of the optimized chroma MC reads one line too much
00246                     // which is also done in mpeg decoders with lowres > 0
00247 
00248     for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
00249         linesize_align[i] = STRIDE_ALIGN;
00250 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
00251 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
00252 //picture size unneccessarily in some cases. The solution here is not
00253 //pretty and better ideas are welcome!
00254 #if HAVE_MMX
00255     if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
00256        s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
00257        s->codec_id == CODEC_ID_VP6A || s->codec_id == CODEC_ID_DIRAC) {
00258         for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
00259             linesize_align[i] = 16;
00260     }
00261 #endif
00262 }
00263 
00264 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
00265     int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
00266     int linesize_align[AV_NUM_DATA_POINTERS];
00267     int align;
00268     avcodec_align_dimensions2(s, width, height, linesize_align);
00269     align = FFMAX(linesize_align[0], linesize_align[3]);
00270     linesize_align[1] <<= chroma_shift;
00271     linesize_align[2] <<= chroma_shift;
00272     align = FFMAX3(align, linesize_align[1], linesize_align[2]);
00273     *width=FFALIGN(*width, align);
00274 }
00275 
00276 void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
00277 {
00278     if (s->pkt) {
00279         pic->pkt_pts = s->pkt->pts;
00280         pic->pkt_pos = s->pkt->pos;
00281     } else {
00282         pic->pkt_pts = AV_NOPTS_VALUE;
00283         pic->pkt_pos = -1;
00284     }
00285     pic->reordered_opaque= s->reordered_opaque;
00286     pic->sample_aspect_ratio = s->sample_aspect_ratio;
00287     pic->width               = s->width;
00288     pic->height              = s->height;
00289     pic->format              = s->pix_fmt;
00290 }
00291 
00292 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
00293                              enum AVSampleFormat sample_fmt, const uint8_t *buf,
00294                              int buf_size, int align)
00295 {
00296     int ch, planar, needed_size, ret = 0;
00297 
00298     needed_size = av_samples_get_buffer_size(NULL, nb_channels,
00299                                              frame->nb_samples, sample_fmt,
00300                                              align);
00301     if (buf_size < needed_size)
00302         return AVERROR(EINVAL);
00303 
00304     planar = av_sample_fmt_is_planar(sample_fmt);
00305     if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
00306         if (!(frame->extended_data = av_mallocz(nb_channels *
00307                                                 sizeof(*frame->extended_data))))
00308             return AVERROR(ENOMEM);
00309     } else {
00310         frame->extended_data = frame->data;
00311     }
00312 
00313     if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
00314                                       buf, nb_channels, frame->nb_samples,
00315                                       sample_fmt, align)) < 0) {
00316         if (frame->extended_data != frame->data)
00317             av_freep(&frame->extended_data);
00318         return ret;
00319     }
00320     if (frame->extended_data != frame->data) {
00321         for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
00322             frame->data[ch] = frame->extended_data[ch];
00323     }
00324 
00325     return ret;
00326 }
00327 
00328 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
00329 {
00330     AVCodecInternal *avci = avctx->internal;
00331     InternalBuffer *buf;
00332     int buf_size, ret;
00333 
00334     buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
00335                                           frame->nb_samples, avctx->sample_fmt,
00336                                           32);
00337     if (buf_size < 0)
00338         return AVERROR(EINVAL);
00339 
00340     /* allocate InternalBuffer if needed */
00341     if (!avci->buffer) {
00342         avci->buffer = av_mallocz(sizeof(InternalBuffer));
00343         if (!avci->buffer)
00344             return AVERROR(ENOMEM);
00345     }
00346     buf = avci->buffer;
00347 
00348     /* if there is a previously-used internal buffer, check its size and
00349        channel count to see if we can reuse it */
00350     if (buf->extended_data) {
00351         /* if current buffer is too small, free it */
00352         if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
00353             av_free(buf->extended_data[0]);
00354             if (buf->extended_data != buf->data)
00355                 av_freep(&buf->extended_data);
00356             buf->extended_data = NULL;
00357             buf->data[0] = NULL;
00358         }
00359         /* if number of channels has changed, reset and/or free extended data
00360            pointers but leave data buffer in buf->data[0] for reuse */
00361         if (buf->nb_channels != avctx->channels) {
00362             if (buf->extended_data != buf->data)
00363                 av_free(buf->extended_data);
00364             buf->extended_data = NULL;
00365         }
00366     }
00367 
00368     /* if there is no previous buffer or the previous buffer cannot be used
00369        as-is, allocate a new buffer and/or rearrange the channel pointers */
00370     if (!buf->extended_data) {
00371         if (!buf->data[0]) {
00372             if (!(buf->data[0] = av_mallocz(buf_size)))
00373                 return AVERROR(ENOMEM);
00374             buf->audio_data_size = buf_size;
00375         }
00376         if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
00377                                             avctx->sample_fmt, buf->data[0],
00378                                             buf->audio_data_size, 32)))
00379             return ret;
00380 
00381         if (frame->extended_data == frame->data)
00382             buf->extended_data = buf->data;
00383         else
00384             buf->extended_data = frame->extended_data;
00385         memcpy(buf->data, frame->data, sizeof(frame->data));
00386         buf->linesize[0] = frame->linesize[0];
00387         buf->nb_channels = avctx->channels;
00388     } else {
00389         /* copy InternalBuffer info to the AVFrame */
00390         frame->extended_data = buf->extended_data;
00391         frame->linesize[0]   = buf->linesize[0];
00392         memcpy(frame->data, buf->data, sizeof(frame->data));
00393     }
00394 
00395     frame->type          = FF_BUFFER_TYPE_INTERNAL;
00396 
00397     if (avctx->pkt) {
00398         frame->pkt_pts = avctx->pkt->pts;
00399         frame->pkt_pos = avctx->pkt->pos;
00400     } else {
00401         frame->pkt_pts = AV_NOPTS_VALUE;
00402         frame->pkt_pos = -1;
00403     }
00404 
00405     frame->reordered_opaque = avctx->reordered_opaque;
00406 
00407     if (avctx->debug & FF_DEBUG_BUFFERS)
00408         av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
00409                "internal audio buffer used\n", frame);
00410 
00411     return 0;
00412 }
00413 
00414 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
00415 {
00416     int i;
00417     int w= s->width;
00418     int h= s->height;
00419     InternalBuffer *buf;
00420     AVCodecInternal *avci = s->internal;
00421 
00422     if(pic->data[0]!=NULL) {
00423         av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
00424         return -1;
00425     }
00426     if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
00427         av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
00428         return -1;
00429     }
00430 
00431     if(av_image_check_size(w, h, 0, s))
00432         return -1;
00433 
00434     if (!avci->buffer) {
00435         avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
00436                                   sizeof(InternalBuffer));
00437     }
00438 
00439     buf = &avci->buffer[avci->buffer_count];
00440 
00441     if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
00442         if(s->active_thread_type&FF_THREAD_FRAME) {
00443             av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
00444             return -1;
00445         }
00446 
00447         for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
00448             av_freep(&buf->base[i]);
00449             buf->data[i]= NULL;
00450         }
00451     }
00452 
00453     if (!buf->base[0]) {
00454         int h_chroma_shift, v_chroma_shift;
00455         int size[4] = {0};
00456         int tmpsize;
00457         int unaligned;
00458         AVPicture picture;
00459         int stride_align[AV_NUM_DATA_POINTERS];
00460         const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
00461 
00462         avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
00463 
00464         avcodec_align_dimensions2(s, &w, &h, stride_align);
00465 
00466         if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
00467             w+= EDGE_WIDTH*2;
00468             h+= EDGE_WIDTH*2;
00469         }
00470 
00471         do {
00472             // NOTE: do not align linesizes individually, this breaks e.g. assumptions
00473             // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
00474             av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
00475             // increase alignment of w for next try (rhs gives the lowest bit set in w)
00476             w += w & ~(w-1);
00477 
00478             unaligned = 0;
00479             for (i=0; i<4; i++){
00480                 unaligned |= picture.linesize[i] % stride_align[i];
00481             }
00482         } while (unaligned);
00483 
00484         tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
00485         if (tmpsize < 0)
00486             return -1;
00487 
00488         for (i=0; i<3 && picture.data[i+1]; i++)
00489             size[i] = picture.data[i+1] - picture.data[i];
00490         size[i] = tmpsize - (picture.data[i] - picture.data[0]);
00491 
00492         memset(buf->base, 0, sizeof(buf->base));
00493         memset(buf->data, 0, sizeof(buf->data));
00494 
00495         for(i=0; i<4 && size[i]; i++){
00496             const int h_shift= i==0 ? 0 : h_chroma_shift;
00497             const int v_shift= i==0 ? 0 : v_chroma_shift;
00498 
00499             buf->linesize[i]= picture.linesize[i];
00500 
00501             buf->base[i]= av_malloc(size[i]+16); //FIXME 16
00502             if(buf->base[i]==NULL) return -1;
00503             memset(buf->base[i], 128, size[i]);
00504 
00505             // no edge if EDGE EMU or not planar YUV
00506             if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
00507                 buf->data[i] = buf->base[i];
00508             else
00509                 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
00510         }
00511         for (; i < AV_NUM_DATA_POINTERS; i++) {
00512             buf->base[i] = buf->data[i] = NULL;
00513             buf->linesize[i] = 0;
00514         }
00515         if(size[1] && !size[2])
00516             ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
00517         buf->width  = s->width;
00518         buf->height = s->height;
00519         buf->pix_fmt= s->pix_fmt;
00520     }
00521     pic->type= FF_BUFFER_TYPE_INTERNAL;
00522 
00523     for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
00524         pic->base[i]= buf->base[i];
00525         pic->data[i]= buf->data[i];
00526         pic->linesize[i]= buf->linesize[i];
00527     }
00528     pic->extended_data = pic->data;
00529     avci->buffer_count++;
00530 
00531     if (s->pkt) {
00532         pic->pkt_pts = s->pkt->pts;
00533         pic->pkt_pos = s->pkt->pos;
00534     } else {
00535         pic->pkt_pts = AV_NOPTS_VALUE;
00536         pic->pkt_pos = -1;
00537     }
00538     pic->reordered_opaque= s->reordered_opaque;
00539     pic->sample_aspect_ratio = s->sample_aspect_ratio;
00540     pic->width               = s->width;
00541     pic->height              = s->height;
00542     pic->format              = s->pix_fmt;
00543 
00544     if(s->debug&FF_DEBUG_BUFFERS)
00545         av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
00546                "buffers used\n", pic, avci->buffer_count);
00547 
00548     return 0;
00549 }
00550 
00551 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
00552 {
00553     switch (avctx->codec_type) {
00554     case AVMEDIA_TYPE_VIDEO:
00555         return video_get_buffer(avctx, frame);
00556     case AVMEDIA_TYPE_AUDIO:
00557         return audio_get_buffer(avctx, frame);
00558     default:
00559         return -1;
00560     }
00561 }
00562 
00563 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
00564     int i;
00565     InternalBuffer *buf, *last;
00566     AVCodecInternal *avci = s->internal;
00567 
00568     assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
00569 
00570     assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
00571     assert(avci->buffer_count);
00572 
00573     if (avci->buffer) {
00574         buf = NULL; /* avoids warning */
00575         for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
00576             buf = &avci->buffer[i];
00577             if (buf->data[0] == pic->data[0])
00578                 break;
00579         }
00580         assert(i < avci->buffer_count);
00581         avci->buffer_count--;
00582         last = &avci->buffer[avci->buffer_count];
00583 
00584         if (buf != last)
00585             FFSWAP(InternalBuffer, *buf, *last);
00586     }
00587 
00588     for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
00589         pic->data[i]=NULL;
00590 //        pic->base[i]=NULL;
00591     }
00592 //printf("R%X\n", pic->opaque);
00593 
00594     if(s->debug&FF_DEBUG_BUFFERS)
00595         av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
00596                "buffers used\n", pic, avci->buffer_count);
00597 }
00598 
00599 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
00600     AVFrame temp_pic;
00601     int i;
00602 
00603     assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
00604 
00605     if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
00606         av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
00607                pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
00608         s->release_buffer(s, pic);
00609     }
00610 
00611     ff_init_buffer_info(s, pic);
00612 
00613     /* If no picture return a new buffer */
00614     if(pic->data[0] == NULL) {
00615         /* We will copy from buffer, so must be readable */
00616         pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
00617         return s->get_buffer(s, pic);
00618     }
00619 
00620     /* If internal buffer type return the same buffer */
00621     if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
00622         return 0;
00623     }
00624 
00625     /*
00626      * Not internal type and reget_buffer not overridden, emulate cr buffer
00627      */
00628     temp_pic = *pic;
00629     for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
00630         pic->data[i] = pic->base[i] = NULL;
00631     pic->opaque = NULL;
00632     /* Allocate new frame */
00633     if (s->get_buffer(s, pic))
00634         return -1;
00635     /* Copy image data from old buffer to new buffer */
00636     av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
00637              s->height);
00638     s->release_buffer(s, &temp_pic); // Release old frame
00639     return 0;
00640 }
00641 
00642 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
00643     int i;
00644 
00645     for(i=0; i<count; i++){
00646         int r= func(c, (char*)arg + i*size);
00647         if(ret) ret[i]= r;
00648     }
00649     return 0;
00650 }
00651 
00652 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
00653     int i;
00654 
00655     for(i=0; i<count; i++){
00656         int r= func(c, arg, i, 0);
00657         if(ret) ret[i]= r;
00658     }
00659     return 0;
00660 }
00661 
00662 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
00663     while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
00664         ++fmt;
00665     return fmt[0];
00666 }
00667 
00668 void avcodec_get_frame_defaults(AVFrame *pic){
00669     memset(pic, 0, sizeof(AVFrame));
00670 
00671     pic->pts = pic->pkt_dts = pic->pkt_pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
00672     pic->pkt_pos = -1;
00673     pic->key_frame= 1;
00674     pic->sample_aspect_ratio = (AVRational){0, 1};
00675     pic->format = -1;           /* unknown */
00676 }
00677 
00678 AVFrame *avcodec_alloc_frame(void){
00679     AVFrame *pic= av_malloc(sizeof(AVFrame));
00680 
00681     if(pic==NULL) return NULL;
00682 
00683     avcodec_get_frame_defaults(pic);
00684 
00685     return pic;
00686 }
00687 
00688 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
00689 {
00690     memset(sub, 0, sizeof(*sub));
00691     sub->pts = AV_NOPTS_VALUE;
00692 }
00693 
00694 #if FF_API_AVCODEC_OPEN
00695 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
00696 {
00697     return avcodec_open2(avctx, codec, NULL);
00698 }
00699 #endif
00700 
00701 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
00702 {
00703     int ret = 0;
00704     AVDictionary *tmp = NULL;
00705 
00706     if (avcodec_is_open(avctx))
00707         return 0;
00708 
00709     if ((!codec && !avctx->codec)) {
00710         av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
00711         return AVERROR(EINVAL);
00712     }
00713     if ((codec && avctx->codec && codec != avctx->codec)) {
00714         av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
00715                "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
00716         return AVERROR(EINVAL);
00717     }
00718     if (!codec)
00719         codec = avctx->codec;
00720 
00721     if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
00722         return AVERROR(EINVAL);
00723 
00724     if (options)
00725         av_dict_copy(&tmp, *options, 0);
00726 
00727     /* If there is a user-supplied mutex locking routine, call it. */
00728     if (ff_lockmgr_cb) {
00729         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
00730             return -1;
00731     }
00732 
00733     entangled_thread_counter++;
00734     if(entangled_thread_counter != 1){
00735         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
00736         ret = -1;
00737         goto end;
00738     }
00739 
00740     avctx->internal = av_mallocz(sizeof(AVCodecInternal));
00741     if (!avctx->internal) {
00742         ret = AVERROR(ENOMEM);
00743         goto end;
00744     }
00745 
00746     if (codec->priv_data_size > 0) {
00747       if(!avctx->priv_data){
00748         avctx->priv_data = av_mallocz(codec->priv_data_size);
00749         if (!avctx->priv_data) {
00750             ret = AVERROR(ENOMEM);
00751             goto end;
00752         }
00753         if (codec->priv_class) {
00754             *(AVClass**)avctx->priv_data= codec->priv_class;
00755             av_opt_set_defaults(avctx->priv_data);
00756         }
00757       }
00758       if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
00759           goto free_and_end;
00760     } else {
00761         avctx->priv_data = NULL;
00762     }
00763     if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
00764         goto free_and_end;
00765 
00766     if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
00767         if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
00768             av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, see -strict -2\n");
00769             ret = -1;
00770             goto free_and_end;
00771         }
00772 
00773     //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
00774     if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
00775     if(avctx->coded_width && avctx->coded_height)
00776         avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
00777     else if(avctx->width && avctx->height)
00778         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
00779     }
00780 
00781     if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
00782         && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
00783            || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
00784         av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
00785         avcodec_set_dimensions(avctx, 0, 0);
00786     }
00787 
00788     /* if the decoder init function was already called previously,
00789        free the already allocated subtitle_header before overwriting it */
00790     if (codec_is_decoder(codec))
00791         av_freep(&avctx->subtitle_header);
00792 
00793 #define SANE_NB_CHANNELS 128U
00794     if (avctx->channels > SANE_NB_CHANNELS) {
00795         ret = AVERROR(EINVAL);
00796         goto free_and_end;
00797     }
00798 
00799     avctx->codec = codec;
00800     if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
00801         avctx->codec_id == CODEC_ID_NONE) {
00802         avctx->codec_type = codec->type;
00803         avctx->codec_id   = codec->id;
00804     }
00805     if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
00806                            && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
00807         av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
00808         ret = AVERROR(EINVAL);
00809         goto free_and_end;
00810     }
00811     avctx->frame_number = 0;
00812 #if FF_API_ER
00813 
00814     av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %X\n",
00815            avctx->error_recognition, avctx->err_recognition);
00816     switch(avctx->error_recognition){
00817         case FF_ER_EXPLODE        : avctx->err_recognition |= AV_EF_EXPLODE | AV_EF_COMPLIANT | AV_EF_CAREFUL;
00818             break;
00819         case FF_ER_VERY_AGGRESSIVE:
00820         case FF_ER_AGGRESSIVE     : avctx->err_recognition |= AV_EF_AGGRESSIVE;
00821         case FF_ER_COMPLIANT      : avctx->err_recognition |= AV_EF_COMPLIANT;
00822         case FF_ER_CAREFUL        : avctx->err_recognition |= AV_EF_CAREFUL;
00823     }
00824 
00825     av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %X\n",
00826            avctx->error_recognition, avctx->err_recognition);
00827 #endif
00828 
00829     if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
00830         (!avctx->time_base.num || !avctx->time_base.den)) {
00831         avctx->time_base.num = 1;
00832         avctx->time_base.den = avctx->sample_rate;
00833     }
00834 
00835     if (!HAVE_THREADS)
00836         av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
00837 
00838     if (HAVE_THREADS && !avctx->thread_opaque) {
00839         ret = ff_thread_init(avctx);
00840         if (ret < 0) {
00841             goto free_and_end;
00842         }
00843     }
00844     if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
00845         avctx->thread_count = 1;
00846 
00847     if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
00848         av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
00849                avctx->codec->max_lowres);
00850         ret = AVERROR(EINVAL);
00851         goto free_and_end;
00852     }
00853     if (codec_is_encoder(avctx->codec)) {
00854         int i;
00855         if (avctx->codec->sample_fmts) {
00856             for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
00857                 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
00858                     break;
00859             if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
00860                 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
00861                 ret = AVERROR(EINVAL);
00862                 goto free_and_end;
00863             }
00864         }
00865         if (avctx->codec->supported_samplerates) {
00866             for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
00867                 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
00868                     break;
00869             if (avctx->codec->supported_samplerates[i] == 0) {
00870                 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
00871                 ret = AVERROR(EINVAL);
00872                 goto free_and_end;
00873             }
00874         }
00875         if (avctx->codec->channel_layouts) {
00876             if (!avctx->channel_layout) {
00877                 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
00878             } else {
00879                 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
00880                     if (avctx->channel_layout == avctx->codec->channel_layouts[i])
00881                         break;
00882                 if (avctx->codec->channel_layouts[i] == 0) {
00883                     av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
00884                     ret = AVERROR(EINVAL);
00885                     goto free_and_end;
00886                 }
00887             }
00888         }
00889         if (avctx->channel_layout && avctx->channels) {
00890             if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
00891                 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
00892                 ret = AVERROR(EINVAL);
00893                 goto free_and_end;
00894             }
00895         } else if (avctx->channel_layout) {
00896             avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
00897         }
00898     }
00899 
00900     avctx->pts_correction_num_faulty_pts =
00901     avctx->pts_correction_num_faulty_dts = 0;
00902     avctx->pts_correction_last_pts =
00903     avctx->pts_correction_last_dts = INT64_MIN;
00904 
00905     if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
00906         ret = avctx->codec->init(avctx);
00907         if (ret < 0) {
00908             goto free_and_end;
00909         }
00910     }
00911 
00912     ret=0;
00913 end:
00914     entangled_thread_counter--;
00915 
00916     /* Release any user-supplied mutex. */
00917     if (ff_lockmgr_cb) {
00918         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
00919     }
00920     if (options) {
00921         av_dict_free(options);
00922         *options = tmp;
00923     }
00924 
00925     return ret;
00926 free_and_end:
00927     av_dict_free(&tmp);
00928     av_freep(&avctx->priv_data);
00929     av_freep(&avctx->internal);
00930     avctx->codec= NULL;
00931     goto end;
00932 }
00933 
00934 int ff_alloc_packet(AVPacket *avpkt, int size)
00935 {
00936     if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
00937         return AVERROR(EINVAL);
00938 
00939     if (avpkt->data) {
00940         uint8_t *pkt_data;
00941         int pkt_size;
00942 
00943         if (avpkt->size < size)
00944             return AVERROR(EINVAL);
00945 
00946         pkt_data = avpkt->data;
00947         pkt_size = avpkt->size;
00948         av_init_packet(avpkt);
00949         avpkt->data = pkt_data;
00950         avpkt->size = pkt_size;
00951         return 0;
00952     } else {
00953         return av_new_packet(avpkt, size);
00954     }
00955 }
00956 
00957 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
00958                                               AVPacket *avpkt,
00959                                               const AVFrame *frame,
00960                                               int *got_packet_ptr)
00961 {
00962     int ret;
00963     int user_packet = !!avpkt->data;
00964     int nb_samples;
00965 
00966     if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
00967         av_init_packet(avpkt);
00968         avpkt->size = 0;
00969         return 0;
00970     }
00971 
00972     /* check for valid frame size */
00973     if (frame) {
00974         nb_samples = frame->nb_samples;
00975         if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
00976             if (nb_samples > avctx->frame_size)
00977                 return AVERROR(EINVAL);
00978         } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
00979             if (nb_samples != avctx->frame_size)
00980                 return AVERROR(EINVAL);
00981         }
00982     } else {
00983         nb_samples = avctx->frame_size;
00984     }
00985 
00986     if (avctx->codec->encode2) {
00987         *got_packet_ptr = 0;
00988         ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
00989         if (!ret && *got_packet_ptr &&
00990             !(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
00991             avpkt->pts = frame->pts;
00992             avpkt->duration = av_rescale_q(frame->nb_samples,
00993                                            (AVRational){ 1, avctx->sample_rate },
00994                                            avctx->time_base);
00995         }
00996     } else {
00997         /* for compatibility with encoders not supporting encode2(), we need to
00998            allocate a packet buffer if the user has not provided one or check
00999            the size otherwise */
01000         int fs_tmp   = 0;
01001         int buf_size = avpkt->size;
01002         if (!user_packet) {
01003             if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
01004                 av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
01005                 if (!frame)
01006                     return AVERROR(EINVAL);
01007                 buf_size = nb_samples * avctx->channels *
01008                            av_get_bits_per_sample(avctx->codec_id) / 8;
01009             } else {
01010                 /* this is a guess as to the required size.
01011                    if an encoder needs more than this, it should probably
01012                    implement encode2() */
01013                 buf_size = 2 * avctx->frame_size * avctx->channels *
01014                            av_get_bytes_per_sample(avctx->sample_fmt);
01015                 buf_size += FF_MIN_BUFFER_SIZE;
01016             }
01017         }
01018         if ((ret = ff_alloc_packet(avpkt, buf_size)))
01019             return ret;
01020 
01021         /* Encoders using AVCodec.encode() that support
01022            CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
01023            the smaller size when encoding the last frame.
01024            This code can be removed once all encoders supporting
01025            CODEC_CAP_SMALL_LAST_FRAME use encode2() */
01026         if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
01027             nb_samples < avctx->frame_size) {
01028             fs_tmp = avctx->frame_size;
01029             avctx->frame_size = nb_samples;
01030         }
01031 
01032         /* encode the frame */
01033         ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
01034                                    frame ? frame->data[0] : NULL);
01035         if (ret >= 0) {
01036             if (!ret) {
01037                 /* no output. if the packet data was allocated by libavcodec,
01038                    free it */
01039                 if (!user_packet)
01040                     av_freep(&avpkt->data);
01041             } else {
01042                 if (avctx->coded_frame)
01043                     avpkt->pts = avctx->coded_frame->pts;
01044                 /* Set duration for final small packet. This can be removed
01045                    once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
01046                    encode2() */
01047                 if (fs_tmp) {
01048                     avpkt->duration = av_rescale_q(avctx->frame_size,
01049                                                    (AVRational){ 1, avctx->sample_rate },
01050                                                    avctx->time_base);
01051                 }
01052             }
01053             avpkt->size = ret;
01054             *got_packet_ptr = (ret > 0);
01055             ret = 0;
01056         }
01057 
01058         if (fs_tmp)
01059             avctx->frame_size = fs_tmp;
01060     }
01061     if (!ret)
01062         avctx->frame_number++;
01063 
01064     /* NOTE: if we add any audio encoders which output non-keyframe packets,
01065              this needs to be moved to the encoders, but for now we can do it
01066              here to simplify things */
01067     avpkt->flags |= AV_PKT_FLAG_KEY;
01068 
01069     return ret;
01070 }
01071 
01072 #if FF_API_OLD_DECODE_AUDIO
01073 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
01074                                              uint8_t *buf, int buf_size,
01075                                              const short *samples)
01076 {
01077     AVPacket pkt;
01078     AVFrame frame0;
01079     AVFrame *frame;
01080     int ret, samples_size, got_packet;
01081 
01082     av_init_packet(&pkt);
01083     pkt.data = buf;
01084     pkt.size = buf_size;
01085 
01086     if (samples) {
01087         frame = &frame0;
01088         avcodec_get_frame_defaults(frame);
01089 
01090         if (avctx->frame_size) {
01091             frame->nb_samples = avctx->frame_size;
01092         } else {
01093             /* if frame_size is not set, the number of samples must be
01094                calculated from the buffer size */
01095             int64_t nb_samples;
01096             if (!av_get_bits_per_sample(avctx->codec_id)) {
01097                 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
01098                        "support this codec\n");
01099                 return AVERROR(EINVAL);
01100             }
01101             nb_samples = (int64_t)buf_size * 8 /
01102                          (av_get_bits_per_sample(avctx->codec_id) *
01103                          avctx->channels);
01104             if (nb_samples >= INT_MAX)
01105                 return AVERROR(EINVAL);
01106             frame->nb_samples = nb_samples;
01107         }
01108 
01109         /* it is assumed that the samples buffer is large enough based on the
01110            relevant parameters */
01111         samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
01112                                                   frame->nb_samples,
01113                                                   avctx->sample_fmt, 1);
01114         if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
01115                                             avctx->sample_fmt,
01116                                             samples, samples_size, 1)))
01117             return ret;
01118 
01119         /* fabricate frame pts from sample count.
01120            this is needed because the avcodec_encode_audio() API does not have
01121            a way for the user to provide pts */
01122         if(avctx->sample_rate && avctx->time_base.num)
01123             frame->pts = av_rescale_q(avctx->internal->sample_count,
01124                                   (AVRational){ 1, avctx->sample_rate },
01125                                   avctx->time_base);
01126         else
01127             frame->pts = AV_NOPTS_VALUE;
01128         avctx->internal->sample_count += frame->nb_samples;
01129     } else {
01130         frame = NULL;
01131     }
01132 
01133     got_packet = 0;
01134     ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
01135     if (!ret && got_packet && avctx->coded_frame) {
01136         avctx->coded_frame->pts       = pkt.pts;
01137         avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
01138     }
01139     /* free any side data since we cannot return it */
01140     ff_packet_free_side_data(&pkt);
01141 
01142     if (frame && frame->extended_data != frame->data)
01143         av_freep(&frame->extended_data);
01144 
01145     return ret ? ret : pkt.size;
01146 }
01147 #endif
01148 
01149 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
01150                          const AVFrame *pict)
01151 {
01152     if(buf_size < FF_MIN_BUFFER_SIZE){
01153         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
01154         return -1;
01155     }
01156     if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
01157         return -1;
01158     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
01159         int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
01160         avctx->frame_number++;
01161         emms_c(); //needed to avoid an emms_c() call before every return;
01162 
01163         return ret;
01164     }else
01165         return 0;
01166 }
01167 
01168 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
01169                             const AVSubtitle *sub)
01170 {
01171     int ret;
01172     if(sub->start_display_time) {
01173         av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
01174         return -1;
01175     }
01176 
01177     ret = avctx->codec->encode(avctx, buf, buf_size, sub);
01178     avctx->frame_number++;
01179     return ret;
01180 }
01181 
01192 static int64_t guess_correct_pts(AVCodecContext *ctx,
01193                                  int64_t reordered_pts, int64_t dts)
01194 {
01195     int64_t pts = AV_NOPTS_VALUE;
01196 
01197     if (dts != AV_NOPTS_VALUE) {
01198         ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
01199         ctx->pts_correction_last_dts = dts;
01200     }
01201     if (reordered_pts != AV_NOPTS_VALUE) {
01202         ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
01203         ctx->pts_correction_last_pts = reordered_pts;
01204     }
01205     if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
01206        && reordered_pts != AV_NOPTS_VALUE)
01207         pts = reordered_pts;
01208     else
01209         pts = dts;
01210 
01211     return pts;
01212 }
01213 
01214 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
01215 {
01216     int size = 0;
01217     const uint8_t *data;
01218     uint32_t flags;
01219 
01220     if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
01221         return;
01222 
01223     data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
01224     if (!data || size < 4)
01225         return;
01226     flags = bytestream_get_le32(&data);
01227     size -= 4;
01228     if (size < 4) /* Required for any of the changes */
01229         return;
01230     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
01231         avctx->channels = bytestream_get_le32(&data);
01232         size -= 4;
01233     }
01234     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
01235         if (size < 8)
01236             return;
01237         avctx->channel_layout = bytestream_get_le64(&data);
01238         size -= 8;
01239     }
01240     if (size < 4)
01241         return;
01242     if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
01243         avctx->sample_rate = bytestream_get_le32(&data);
01244         size -= 4;
01245     }
01246     if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
01247         if (size < 8)
01248             return;
01249         avctx->width  = bytestream_get_le32(&data);
01250         avctx->height = bytestream_get_le32(&data);
01251         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
01252         size -= 8;
01253     }
01254 }
01255 
01256 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
01257                          int *got_picture_ptr,
01258                          const AVPacket *avpkt)
01259 {
01260     int ret;
01261     // copy to ensure we do not change avpkt
01262     AVPacket tmp = *avpkt;
01263 
01264     *got_picture_ptr= 0;
01265     if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
01266         return -1;
01267 
01268     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
01269         int did_split = av_packet_split_side_data(&tmp);
01270         apply_param_change(avctx, &tmp);
01271         avctx->pkt = &tmp;
01272         if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
01273              ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
01274                                           &tmp);
01275         else {
01276             ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
01277                               &tmp);
01278             picture->pkt_dts= avpkt->dts;
01279 
01280             if(!avctx->has_b_frames){
01281             picture->pkt_pos= avpkt->pos;
01282             }
01283             //FIXME these should be under if(!avctx->has_b_frames)
01284             if (!picture->sample_aspect_ratio.num)
01285                 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
01286             if (!picture->width)
01287                 picture->width = avctx->width;
01288             if (!picture->height)
01289                 picture->height = avctx->height;
01290             if (picture->format == PIX_FMT_NONE)
01291                 picture->format = avctx->pix_fmt;
01292         }
01293 
01294         emms_c(); //needed to avoid an emms_c() call before every return;
01295 
01296         avctx->pkt = NULL;
01297         if (did_split)
01298             ff_packet_free_side_data(&tmp);
01299 
01300         if (*got_picture_ptr){
01301             avctx->frame_number++;
01302             picture->best_effort_timestamp = guess_correct_pts(avctx,
01303                                                             picture->pkt_pts,
01304                                                             picture->pkt_dts);
01305         }
01306     }else
01307         ret= 0;
01308 
01309     return ret;
01310 }
01311 
01312 #if FF_API_OLD_DECODE_AUDIO
01313 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
01314                          int *frame_size_ptr,
01315                          AVPacket *avpkt)
01316 {
01317     AVFrame frame;
01318     int ret, got_frame = 0;
01319 
01320     if (avctx->get_buffer != avcodec_default_get_buffer) {
01321         av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
01322                "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
01323         av_log(avctx, AV_LOG_ERROR, "Please port your application to "
01324                "avcodec_decode_audio4()\n");
01325         avctx->get_buffer = avcodec_default_get_buffer;
01326         avctx->release_buffer = avcodec_default_release_buffer;
01327     }
01328 
01329     ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
01330 
01331     if (ret >= 0 && got_frame) {
01332         int ch, plane_size;
01333         int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
01334         int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
01335                                                    frame.nb_samples,
01336                                                    avctx->sample_fmt, 1);
01337         if (*frame_size_ptr < data_size) {
01338             av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
01339                    "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
01340             return AVERROR(EINVAL);
01341         }
01342 
01343         memcpy(samples, frame.extended_data[0], plane_size);
01344 
01345         if (planar && avctx->channels > 1) {
01346             uint8_t *out = ((uint8_t *)samples) + plane_size;
01347             for (ch = 1; ch < avctx->channels; ch++) {
01348                 memcpy(out, frame.extended_data[ch], plane_size);
01349                 out += plane_size;
01350             }
01351         }
01352         *frame_size_ptr = data_size;
01353     } else {
01354         *frame_size_ptr = 0;
01355     }
01356     return ret;
01357 }
01358 #endif
01359 
01360 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
01361                                               AVFrame *frame,
01362                                               int *got_frame_ptr,
01363                                               AVPacket *avpkt)
01364 {
01365     int ret = 0;
01366 
01367     *got_frame_ptr = 0;
01368 
01369     if (!avpkt->data && avpkt->size) {
01370         av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
01371         return AVERROR(EINVAL);
01372     }
01373 
01374     if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
01375         av_packet_split_side_data(avpkt);
01376         apply_param_change(avctx, avpkt);
01377 
01378         avctx->pkt = avpkt;
01379         ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
01380         if (ret >= 0 && *got_frame_ptr) {
01381             avctx->frame_number++;
01382             frame->pkt_dts = avpkt->dts;
01383             if (frame->format == AV_SAMPLE_FMT_NONE)
01384                 frame->format = avctx->sample_fmt;
01385         }
01386     }
01387     return ret;
01388 }
01389 
01390 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
01391                             int *got_sub_ptr,
01392                             AVPacket *avpkt)
01393 {
01394     int ret;
01395 
01396     avctx->pkt = avpkt;
01397     *got_sub_ptr = 0;
01398     avcodec_get_subtitle_defaults(sub);
01399     ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
01400     if (*got_sub_ptr)
01401         avctx->frame_number++;
01402     return ret;
01403 }
01404 
01405 void avsubtitle_free(AVSubtitle *sub)
01406 {
01407     int i;
01408 
01409     for (i = 0; i < sub->num_rects; i++)
01410     {
01411         av_freep(&sub->rects[i]->pict.data[0]);
01412         av_freep(&sub->rects[i]->pict.data[1]);
01413         av_freep(&sub->rects[i]->pict.data[2]);
01414         av_freep(&sub->rects[i]->pict.data[3]);
01415         av_freep(&sub->rects[i]->text);
01416         av_freep(&sub->rects[i]->ass);
01417         av_freep(&sub->rects[i]);
01418     }
01419 
01420     av_freep(&sub->rects);
01421 
01422     memset(sub, 0, sizeof(AVSubtitle));
01423 }
01424 
01425 av_cold int avcodec_close(AVCodecContext *avctx)
01426 {
01427     /* If there is a user-supplied mutex locking routine, call it. */
01428     if (ff_lockmgr_cb) {
01429         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
01430             return -1;
01431     }
01432 
01433     entangled_thread_counter++;
01434     if(entangled_thread_counter != 1){
01435         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
01436         entangled_thread_counter--;
01437         return -1;
01438     }
01439 
01440     if (avcodec_is_open(avctx)) {
01441         if (HAVE_THREADS && avctx->thread_opaque)
01442             ff_thread_free(avctx);
01443         if (avctx->codec && avctx->codec->close)
01444             avctx->codec->close(avctx);
01445         avcodec_default_free_buffers(avctx);
01446         avctx->coded_frame = NULL;
01447         av_freep(&avctx->internal);
01448     }
01449 
01450     if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
01451         av_opt_free(avctx->priv_data);
01452     av_opt_free(avctx);
01453     av_freep(&avctx->priv_data);
01454     if (codec_is_encoder(avctx->codec))
01455         av_freep(&avctx->extradata);
01456     avctx->codec = NULL;
01457     avctx->active_thread_type = 0;
01458     entangled_thread_counter--;
01459 
01460     /* Release any user-supplied mutex. */
01461     if (ff_lockmgr_cb) {
01462         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
01463     }
01464     return 0;
01465 }
01466 
01467 static enum CodecID remap_deprecated_codec_id(enum CodecID id)
01468 {
01469     switch(id){
01470         case CODEC_ID_G723_1_DEPRECATED : return CODEC_ID_G723_1;
01471         case CODEC_ID_G729_DEPRECATED   : return CODEC_ID_G729;
01472         case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
01473         default                         : return id;
01474     }
01475 }
01476 
01477 AVCodec *avcodec_find_encoder(enum CodecID id)
01478 {
01479     AVCodec *p, *experimental=NULL;
01480     p = first_avcodec;
01481     id= remap_deprecated_codec_id(id);
01482     while (p) {
01483         if (codec_is_encoder(p) && p->id == id) {
01484             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
01485                 experimental = p;
01486             } else
01487                 return p;
01488         }
01489         p = p->next;
01490     }
01491     return experimental;
01492 }
01493 
01494 AVCodec *avcodec_find_encoder_by_name(const char *name)
01495 {
01496     AVCodec *p;
01497     if (!name)
01498         return NULL;
01499     p = first_avcodec;
01500     while (p) {
01501         if (codec_is_encoder(p) && strcmp(name,p->name) == 0)
01502             return p;
01503         p = p->next;
01504     }
01505     return NULL;
01506 }
01507 
01508 AVCodec *avcodec_find_decoder(enum CodecID id)
01509 {
01510     AVCodec *p, *experimental=NULL;
01511     p = first_avcodec;
01512     id= remap_deprecated_codec_id(id);
01513     while (p) {
01514         if (codec_is_decoder(p) && p->id == id) {
01515             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
01516                 experimental = p;
01517             } else
01518                 return p;
01519         }
01520         p = p->next;
01521     }
01522     return experimental;
01523 }
01524 
01525 AVCodec *avcodec_find_decoder_by_name(const char *name)
01526 {
01527     AVCodec *p;
01528     if (!name)
01529         return NULL;
01530     p = first_avcodec;
01531     while (p) {
01532         if (codec_is_decoder(p) && strcmp(name,p->name) == 0)
01533             return p;
01534         p = p->next;
01535     }
01536     return NULL;
01537 }
01538 
01539 static int get_bit_rate(AVCodecContext *ctx)
01540 {
01541     int bit_rate;
01542     int bits_per_sample;
01543 
01544     switch(ctx->codec_type) {
01545     case AVMEDIA_TYPE_VIDEO:
01546     case AVMEDIA_TYPE_DATA:
01547     case AVMEDIA_TYPE_SUBTITLE:
01548     case AVMEDIA_TYPE_ATTACHMENT:
01549         bit_rate = ctx->bit_rate;
01550         break;
01551     case AVMEDIA_TYPE_AUDIO:
01552         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
01553         bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
01554         break;
01555     default:
01556         bit_rate = 0;
01557         break;
01558     }
01559     return bit_rate;
01560 }
01561 
01562 const char *avcodec_get_name(enum CodecID id)
01563 {
01564     AVCodec *codec;
01565 
01566 #if !CONFIG_SMALL
01567     switch (id) {
01568 #include "libavcodec/codec_names.h"
01569     }
01570     av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
01571 #endif
01572     codec = avcodec_find_decoder(id);
01573     if (codec)
01574         return codec->name;
01575     codec = avcodec_find_encoder(id);
01576     if (codec)
01577         return codec->name;
01578     return "unknown_codec";
01579 }
01580 
01581 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
01582 {
01583     int i, len, ret = 0;
01584 
01585     for (i = 0; i < 4; i++) {
01586         len = snprintf(buf, buf_size,
01587                        isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
01588         buf      += len;
01589         buf_size  = buf_size > len ? buf_size - len : 0;
01590         ret      += len;
01591         codec_tag>>=8;
01592     }
01593     return ret;
01594 }
01595 
01596 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
01597 {
01598     const char *codec_type;
01599     const char *codec_name;
01600     const char *profile = NULL;
01601     AVCodec *p;
01602     int bitrate;
01603     AVRational display_aspect_ratio;
01604 
01605     if (!buf || buf_size <= 0)
01606         return;
01607     codec_type = av_get_media_type_string(enc->codec_type);
01608     codec_name = avcodec_get_name(enc->codec_id);
01609     if (enc->profile != FF_PROFILE_UNKNOWN) {
01610         p = encode ? avcodec_find_encoder(enc->codec_id) :
01611                      avcodec_find_decoder(enc->codec_id);
01612         if (p)
01613             profile = av_get_profile_name(p, enc->profile);
01614     }
01615 
01616     snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
01617              codec_name, enc->mb_decision ? " (hq)" : "");
01618     buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
01619     if (profile)
01620         snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
01621     if (enc->codec_tag) {
01622         char tag_buf[32];
01623         av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
01624         snprintf(buf + strlen(buf), buf_size - strlen(buf),
01625                  " (%s / 0x%04X)", tag_buf, enc->codec_tag);
01626     }
01627     switch(enc->codec_type) {
01628     case AVMEDIA_TYPE_VIDEO:
01629         if (enc->pix_fmt != PIX_FMT_NONE) {
01630             snprintf(buf + strlen(buf), buf_size - strlen(buf),
01631                      ", %s",
01632                      av_get_pix_fmt_name(enc->pix_fmt));
01633         }
01634         if (enc->width) {
01635             snprintf(buf + strlen(buf), buf_size - strlen(buf),
01636                      ", %dx%d",
01637                      enc->width, enc->height);
01638             if (enc->sample_aspect_ratio.num) {
01639                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
01640                           enc->width*enc->sample_aspect_ratio.num,
01641                           enc->height*enc->sample_aspect_ratio.den,
01642                           1024*1024);
01643                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01644                          " [SAR %d:%d DAR %d:%d]",
01645                          enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
01646                          display_aspect_ratio.num, display_aspect_ratio.den);
01647             }
01648             if(av_log_get_level() >= AV_LOG_DEBUG){
01649                 int g= av_gcd(enc->time_base.num, enc->time_base.den);
01650                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01651                      ", %d/%d",
01652                      enc->time_base.num/g, enc->time_base.den/g);
01653             }
01654         }
01655         if (encode) {
01656             snprintf(buf + strlen(buf), buf_size - strlen(buf),
01657                      ", q=%d-%d", enc->qmin, enc->qmax);
01658         }
01659         break;
01660     case AVMEDIA_TYPE_AUDIO:
01661         if (enc->sample_rate) {
01662             snprintf(buf + strlen(buf), buf_size - strlen(buf),
01663                      ", %d Hz", enc->sample_rate);
01664         }
01665         av_strlcat(buf, ", ", buf_size);
01666         av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
01667         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
01668             snprintf(buf + strlen(buf), buf_size - strlen(buf),
01669                      ", %s", av_get_sample_fmt_name(enc->sample_fmt));
01670         }
01671         break;
01672     default:
01673         return;
01674     }
01675     if (encode) {
01676         if (enc->flags & CODEC_FLAG_PASS1)
01677             snprintf(buf + strlen(buf), buf_size - strlen(buf),
01678                      ", pass 1");
01679         if (enc->flags & CODEC_FLAG_PASS2)
01680             snprintf(buf + strlen(buf), buf_size - strlen(buf),
01681                      ", pass 2");
01682     }
01683     bitrate = get_bit_rate(enc);
01684     if (bitrate != 0) {
01685         snprintf(buf + strlen(buf), buf_size - strlen(buf),
01686                  ", %d kb/s", bitrate / 1000);
01687     }
01688 }
01689 
01690 const char *av_get_profile_name(const AVCodec *codec, int profile)
01691 {
01692     const AVProfile *p;
01693     if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
01694         return NULL;
01695 
01696     for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
01697         if (p->profile == profile)
01698             return p->name;
01699 
01700     return NULL;
01701 }
01702 
01703 unsigned avcodec_version( void )
01704 {
01705     av_assert0(CODEC_ID_V410==164);
01706     av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
01707     av_assert0(CODEC_ID_ADPCM_G722==69660);
01708     av_assert0(CODEC_ID_BMV_AUDIO==86071);
01709     av_assert0(CODEC_ID_SRT==94216);
01710     av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
01711 
01712   return LIBAVCODEC_VERSION_INT;
01713 }
01714 
01715 const char *avcodec_configuration(void)
01716 {
01717     return FFMPEG_CONFIGURATION;
01718 }
01719 
01720 const char *avcodec_license(void)
01721 {
01722 #define LICENSE_PREFIX "libavcodec license: "
01723     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
01724 }
01725 
01726 void avcodec_flush_buffers(AVCodecContext *avctx)
01727 {
01728     if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
01729         ff_thread_flush(avctx);
01730     else if(avctx->codec->flush)
01731         avctx->codec->flush(avctx);
01732 }
01733 
01734 static void video_free_buffers(AVCodecContext *s)
01735 {
01736     AVCodecInternal *avci = s->internal;
01737     int i, j;
01738 
01739     if (!avci->buffer)
01740         return;
01741 
01742     if (avci->buffer_count)
01743         av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
01744                avci->buffer_count);
01745     for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
01746         InternalBuffer *buf = &avci->buffer[i];
01747         for(j=0; j<4; j++){
01748             av_freep(&buf->base[j]);
01749             buf->data[j]= NULL;
01750         }
01751     }
01752     av_freep(&avci->buffer);
01753 
01754     avci->buffer_count=0;
01755 }
01756 
01757 static void audio_free_buffers(AVCodecContext *avctx)
01758 {
01759     AVCodecInternal *avci = avctx->internal;
01760     InternalBuffer *buf;
01761 
01762     if (!avci->buffer)
01763         return;
01764     buf = avci->buffer;
01765 
01766     if (buf->extended_data) {
01767         av_free(buf->extended_data[0]);
01768         if (buf->extended_data != buf->data)
01769             av_freep(&buf->extended_data);
01770     }
01771     av_freep(&avci->buffer);
01772 }
01773 
01774 void avcodec_default_free_buffers(AVCodecContext *avctx)
01775 {
01776     switch (avctx->codec_type) {
01777     case AVMEDIA_TYPE_VIDEO:
01778         video_free_buffers(avctx);
01779         break;
01780     case AVMEDIA_TYPE_AUDIO:
01781         audio_free_buffers(avctx);
01782         break;
01783     default:
01784         break;
01785     }
01786 }
01787 
01788 #if FF_API_OLD_FF_PICT_TYPES
01789 char av_get_pict_type_char(int pict_type){
01790     return av_get_picture_type_char(pict_type);
01791 }
01792 #endif
01793 
01794 int av_get_bits_per_sample(enum CodecID codec_id){
01795     switch(codec_id){
01796     case CODEC_ID_ADPCM_SBPRO_2:
01797         return 2;
01798     case CODEC_ID_ADPCM_SBPRO_3:
01799         return 3;
01800     case CODEC_ID_ADPCM_SBPRO_4:
01801     case CODEC_ID_ADPCM_CT:
01802     case CODEC_ID_ADPCM_IMA_APC:
01803     case CODEC_ID_ADPCM_IMA_WAV:
01804     case CODEC_ID_ADPCM_IMA_QT:
01805     case CODEC_ID_ADPCM_SWF:
01806     case CODEC_ID_ADPCM_MS:
01807     case CODEC_ID_ADPCM_YAMAHA:
01808     case CODEC_ID_ADPCM_G722:
01809         return 4;
01810     case CODEC_ID_PCM_ALAW:
01811     case CODEC_ID_PCM_MULAW:
01812     case CODEC_ID_PCM_S8:
01813     case CODEC_ID_PCM_U8:
01814     case CODEC_ID_PCM_ZORK:
01815         return 8;
01816     case CODEC_ID_PCM_S16BE:
01817     case CODEC_ID_PCM_S16LE:
01818     case CODEC_ID_PCM_S16LE_PLANAR:
01819     case CODEC_ID_PCM_U16BE:
01820     case CODEC_ID_PCM_U16LE:
01821         return 16;
01822     case CODEC_ID_PCM_S24DAUD:
01823     case CODEC_ID_PCM_S24BE:
01824     case CODEC_ID_PCM_S24LE:
01825     case CODEC_ID_PCM_U24BE:
01826     case CODEC_ID_PCM_U24LE:
01827         return 24;
01828     case CODEC_ID_PCM_S32BE:
01829     case CODEC_ID_PCM_S32LE:
01830     case CODEC_ID_PCM_U32BE:
01831     case CODEC_ID_PCM_U32LE:
01832     case CODEC_ID_PCM_F32BE:
01833     case CODEC_ID_PCM_F32LE:
01834         return 32;
01835     case CODEC_ID_PCM_F64BE:
01836     case CODEC_ID_PCM_F64LE:
01837         return 64;
01838     default:
01839         return 0;
01840     }
01841 }
01842 
01843 #if FF_API_OLD_SAMPLE_FMT
01844 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
01845     return av_get_bytes_per_sample(sample_fmt) << 3;
01846 }
01847 #endif
01848 
01849 #if !HAVE_THREADS
01850 int ff_thread_init(AVCodecContext *s){
01851     return -1;
01852 }
01853 #endif
01854 
01855 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
01856 {
01857     unsigned int n = 0;
01858 
01859     while(v >= 0xff) {
01860         *s++ = 0xff;
01861         v -= 0xff;
01862         n++;
01863     }
01864     *s = v;
01865     n++;
01866     return n;
01867 }
01868 
01869 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
01870     int i;
01871     for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
01872     return i;
01873 }
01874 
01875 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
01876 {
01877     av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
01878             "version to the newest one from Git. If the problem still "
01879             "occurs, it means that your file has a feature which has not "
01880             "been implemented.\n", feature);
01881     if(want_sample)
01882         av_log_ask_for_sample(avc, NULL);
01883 }
01884 
01885 void av_log_ask_for_sample(void *avc, const char *msg, ...)
01886 {
01887     va_list argument_list;
01888 
01889     va_start(argument_list, msg);
01890 
01891     if (msg)
01892         av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
01893     av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
01894             "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
01895             "and contact the ffmpeg-devel mailing list.\n");
01896 
01897     va_end(argument_list);
01898 }
01899 
01900 static AVHWAccel *first_hwaccel = NULL;
01901 
01902 void av_register_hwaccel(AVHWAccel *hwaccel)
01903 {
01904     AVHWAccel **p = &first_hwaccel;
01905     while (*p)
01906         p = &(*p)->next;
01907     *p = hwaccel;
01908     hwaccel->next = NULL;
01909 }
01910 
01911 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
01912 {
01913     return hwaccel ? hwaccel->next : first_hwaccel;
01914 }
01915 
01916 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
01917 {
01918     AVHWAccel *hwaccel=NULL;
01919 
01920     while((hwaccel= av_hwaccel_next(hwaccel))){
01921         if (   hwaccel->id      == codec_id
01922             && hwaccel->pix_fmt == pix_fmt)
01923             return hwaccel;
01924     }
01925     return NULL;
01926 }
01927 
01928 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
01929 {
01930     if (ff_lockmgr_cb) {
01931         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
01932             return -1;
01933         if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
01934             return -1;
01935     }
01936 
01937     ff_lockmgr_cb = cb;
01938 
01939     if (ff_lockmgr_cb) {
01940         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
01941             return -1;
01942         if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
01943             return -1;
01944     }
01945     return 0;
01946 }
01947 
01948 int avpriv_lock_avformat(void)
01949 {
01950     if (ff_lockmgr_cb) {
01951         if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
01952             return -1;
01953     }
01954     return 0;
01955 }
01956 
01957 int avpriv_unlock_avformat(void)
01958 {
01959     if (ff_lockmgr_cb) {
01960         if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
01961             return -1;
01962     }
01963     return 0;
01964 }
01965 
01966 unsigned int avpriv_toupper4(unsigned int x)
01967 {
01968     return     toupper( x     &0xFF)
01969             + (toupper((x>>8 )&0xFF)<<8 )
01970             + (toupper((x>>16)&0xFF)<<16)
01971             + (toupper((x>>24)&0xFF)<<24);
01972 }
01973 
01974 #if !HAVE_THREADS
01975 
01976 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
01977 {
01978     f->owner = avctx;
01979 
01980     ff_init_buffer_info(avctx, f);
01981 
01982     return avctx->get_buffer(avctx, f);
01983 }
01984 
01985 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
01986 {
01987     f->owner->release_buffer(f->owner, f);
01988 }
01989 
01990 void ff_thread_finish_setup(AVCodecContext *avctx)
01991 {
01992 }
01993 
01994 void ff_thread_report_progress(AVFrame *f, int progress, int field)
01995 {
01996 }
01997 
01998 void ff_thread_await_progress(AVFrame *f, int progress, int field)
01999 {
02000 }
02001 
02002 #endif
02003 
02004 #if FF_API_THREAD_INIT
02005 int avcodec_thread_init(AVCodecContext *s, int thread_count)
02006 {
02007     s->thread_count = thread_count;
02008     return ff_thread_init(s);
02009 }
02010 #endif
02011 
02012 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
02013 {
02014     AVCodec *c= avcodec_find_decoder(codec_id);
02015     if(!c)
02016         c= avcodec_find_encoder(codec_id);
02017     if(c)
02018         return c->type;
02019 
02020     if (codec_id <= CODEC_ID_NONE)
02021         return AVMEDIA_TYPE_UNKNOWN;
02022     else if (codec_id < CODEC_ID_FIRST_AUDIO)
02023         return AVMEDIA_TYPE_VIDEO;
02024     else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
02025         return AVMEDIA_TYPE_AUDIO;
02026     else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
02027         return AVMEDIA_TYPE_SUBTITLE;
02028 
02029     return AVMEDIA_TYPE_UNKNOWN;
02030 }
02031 
02032 int avcodec_is_open(AVCodecContext *s)
02033 {
02034     return !!s->internal;
02035 }