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