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