00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028
00029 #define _XOPEN_SOURCE 600
00030
00031 #include "libavutil/avstring.h"
00032 #include "libavutil/integer.h"
00033 #include "libavutil/crc.h"
00034 #include "avcodec.h"
00035 #include "dsputil.h"
00036 #include "opt.h"
00037 #include "imgconvert.h"
00038 #include "audioconvert.h"
00039 #include "internal.h"
00040 #include <stdlib.h>
00041 #include <stdarg.h>
00042 #include <limits.h>
00043 #include <float.h>
00044 #if !HAVE_MKSTEMP
00045 #include <fcntl.h>
00046 #endif
00047
00048 const uint8_t ff_reverse[256]={
00049 0x00,0x80,0x40,0xC0,0x20,0xA0,0x60,0xE0,0x10,0x90,0x50,0xD0,0x30,0xB0,0x70,0xF0,
00050 0x08,0x88,0x48,0xC8,0x28,0xA8,0x68,0xE8,0x18,0x98,0x58,0xD8,0x38,0xB8,0x78,0xF8,
00051 0x04,0x84,0x44,0xC4,0x24,0xA4,0x64,0xE4,0x14,0x94,0x54,0xD4,0x34,0xB4,0x74,0xF4,
00052 0x0C,0x8C,0x4C,0xCC,0x2C,0xAC,0x6C,0xEC,0x1C,0x9C,0x5C,0xDC,0x3C,0xBC,0x7C,0xFC,
00053 0x02,0x82,0x42,0xC2,0x22,0xA2,0x62,0xE2,0x12,0x92,0x52,0xD2,0x32,0xB2,0x72,0xF2,
00054 0x0A,0x8A,0x4A,0xCA,0x2A,0xAA,0x6A,0xEA,0x1A,0x9A,0x5A,0xDA,0x3A,0xBA,0x7A,0xFA,
00055 0x06,0x86,0x46,0xC6,0x26,0xA6,0x66,0xE6,0x16,0x96,0x56,0xD6,0x36,0xB6,0x76,0xF6,
00056 0x0E,0x8E,0x4E,0xCE,0x2E,0xAE,0x6E,0xEE,0x1E,0x9E,0x5E,0xDE,0x3E,0xBE,0x7E,0xFE,
00057 0x01,0x81,0x41,0xC1,0x21,0xA1,0x61,0xE1,0x11,0x91,0x51,0xD1,0x31,0xB1,0x71,0xF1,
00058 0x09,0x89,0x49,0xC9,0x29,0xA9,0x69,0xE9,0x19,0x99,0x59,0xD9,0x39,0xB9,0x79,0xF9,
00059 0x05,0x85,0x45,0xC5,0x25,0xA5,0x65,0xE5,0x15,0x95,0x55,0xD5,0x35,0xB5,0x75,0xF5,
00060 0x0D,0x8D,0x4D,0xCD,0x2D,0xAD,0x6D,0xED,0x1D,0x9D,0x5D,0xDD,0x3D,0xBD,0x7D,0xFD,
00061 0x03,0x83,0x43,0xC3,0x23,0xA3,0x63,0xE3,0x13,0x93,0x53,0xD3,0x33,0xB3,0x73,0xF3,
00062 0x0B,0x8B,0x4B,0xCB,0x2B,0xAB,0x6B,0xEB,0x1B,0x9B,0x5B,0xDB,0x3B,0xBB,0x7B,0xFB,
00063 0x07,0x87,0x47,0xC7,0x27,0xA7,0x67,0xE7,0x17,0x97,0x57,0xD7,0x37,0xB7,0x77,0xF7,
00064 0x0F,0x8F,0x4F,0xCF,0x2F,0xAF,0x6F,0xEF,0x1F,0x9F,0x5F,0xDF,0x3F,0xBF,0x7F,0xFF,
00065 };
00066
00067 static int volatile entangled_thread_counter=0;
00068 int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
00069 static void *codec_mutex;
00070
00071 void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
00072 {
00073 if(min_size < *size)
00074 return ptr;
00075
00076 *size= FFMAX(17*min_size/16 + 32, min_size);
00077
00078 ptr= av_realloc(ptr, *size);
00079 if(!ptr)
00080 *size= 0;
00081
00082 return ptr;
00083 }
00084
00085
00086 static AVCodec *first_avcodec = NULL;
00087
00088 AVCodec *av_codec_next(AVCodec *c){
00089 if(c) return c->next;
00090 else return first_avcodec;
00091 }
00092
00093 void avcodec_register(AVCodec *codec)
00094 {
00095 AVCodec **p;
00096 avcodec_init();
00097 p = &first_avcodec;
00098 while (*p != NULL) p = &(*p)->next;
00099 *p = codec;
00100 codec->next = NULL;
00101 }
00102
00103 #if LIBAVCODEC_VERSION_MAJOR < 53
00104 void register_avcodec(AVCodec *codec)
00105 {
00106 avcodec_register(codec);
00107 }
00108 #endif
00109
00110 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
00111 s->coded_width = width;
00112 s->coded_height= height;
00113 s->width = -((-width )>>s->lowres);
00114 s->height= -((-height)>>s->lowres);
00115 }
00116
00117 typedef struct InternalBuffer{
00118 int last_pic_num;
00119 uint8_t *base[4];
00120 uint8_t *data[4];
00121 int linesize[4];
00122 int width, height;
00123 enum PixelFormat pix_fmt;
00124 }InternalBuffer;
00125
00126 #define INTERNAL_BUFFER_SIZE 32
00127
00128 #define ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
00129
00130 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
00131 int w_align= 1;
00132 int h_align= 1;
00133
00134 switch(s->pix_fmt){
00135 case PIX_FMT_YUV420P:
00136 case PIX_FMT_YUYV422:
00137 case PIX_FMT_UYVY422:
00138 case PIX_FMT_YUV422P:
00139 case PIX_FMT_YUV444P:
00140 case PIX_FMT_GRAY8:
00141 case PIX_FMT_GRAY16BE:
00142 case PIX_FMT_GRAY16LE:
00143 case PIX_FMT_YUVJ420P:
00144 case PIX_FMT_YUVJ422P:
00145 case PIX_FMT_YUVJ444P:
00146 case PIX_FMT_YUVA420P:
00147 w_align= 16;
00148 h_align= 16;
00149 break;
00150 case PIX_FMT_YUV411P:
00151 case PIX_FMT_UYYVYY411:
00152 w_align=32;
00153 h_align=8;
00154 break;
00155 case PIX_FMT_YUV410P:
00156 if(s->codec_id == CODEC_ID_SVQ1){
00157 w_align=64;
00158 h_align=64;
00159 }
00160 case PIX_FMT_RGB555:
00161 if(s->codec_id == CODEC_ID_RPZA){
00162 w_align=4;
00163 h_align=4;
00164 }
00165 case PIX_FMT_PAL8:
00166 case PIX_FMT_BGR8:
00167 case PIX_FMT_RGB8:
00168 if(s->codec_id == CODEC_ID_SMC){
00169 w_align=4;
00170 h_align=4;
00171 }
00172 break;
00173 case PIX_FMT_BGR24:
00174 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
00175 w_align=4;
00176 h_align=4;
00177 }
00178 break;
00179 default:
00180 w_align= 1;
00181 h_align= 1;
00182 break;
00183 }
00184
00185 *width = ALIGN(*width , w_align);
00186 *height= ALIGN(*height, h_align);
00187 if(s->codec_id == CODEC_ID_H264)
00188 *height+=2;
00189 }
00190
00191 int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
00192 if((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
00193 return 0;
00194
00195 av_log(av_log_ctx, AV_LOG_ERROR, "picture size invalid (%ux%u)\n", w, h);
00196 return -1;
00197 }
00198
00199 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
00200 int i;
00201 int w= s->width;
00202 int h= s->height;
00203 InternalBuffer *buf;
00204 int *picture_number;
00205
00206 if(pic->data[0]!=NULL) {
00207 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
00208 return -1;
00209 }
00210 if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
00211 av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
00212 return -1;
00213 }
00214
00215 if(avcodec_check_dimensions(s,w,h))
00216 return -1;
00217
00218 if(s->internal_buffer==NULL){
00219 s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
00220 }
00221 #if 0
00222 s->internal_buffer= av_fast_realloc(
00223 s->internal_buffer,
00224 &s->internal_buffer_size,
00225 sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)
00226 );
00227 #endif
00228
00229 buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
00230 picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num;
00231 (*picture_number)++;
00232
00233 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
00234 for(i=0; i<4; i++){
00235 av_freep(&buf->base[i]);
00236 buf->data[i]= NULL;
00237 }
00238 }
00239
00240 if(buf->base[0]){
00241 pic->age= *picture_number - buf->last_pic_num;
00242 buf->last_pic_num= *picture_number;
00243 }else{
00244 int h_chroma_shift, v_chroma_shift;
00245 int size[4] = {0};
00246 int tmpsize;
00247 AVPicture picture;
00248 int stride_align[4];
00249
00250 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
00251
00252 avcodec_align_dimensions(s, &w, &h);
00253
00254 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
00255 w+= EDGE_WIDTH*2;
00256 h+= EDGE_WIDTH*2;
00257 }
00258
00259 ff_fill_linesize(&picture, s->pix_fmt, w);
00260
00261 for (i=0; i<4; i++){
00262
00263
00264
00265
00266 #if HAVE_MMX
00267 if(s->codec_id == CODEC_ID_SVQ1)
00268 stride_align[i]= 16;
00269 else
00270 #endif
00271 stride_align[i] = STRIDE_ALIGN;
00272 picture.linesize[i] = ALIGN(picture.linesize[i], stride_align[i]);
00273 }
00274
00275 tmpsize = ff_fill_pointer(&picture, NULL, s->pix_fmt, h);
00276 if (tmpsize < 0)
00277 return -1;
00278
00279 for (i=0; i<3 && picture.data[i+1]; i++)
00280 size[i] = picture.data[i+1] - picture.data[i];
00281 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
00282
00283 buf->last_pic_num= -256*256*256*64;
00284 memset(buf->base, 0, sizeof(buf->base));
00285 memset(buf->data, 0, sizeof(buf->data));
00286
00287 for(i=0; i<4 && size[i]; i++){
00288 const int h_shift= i==0 ? 0 : h_chroma_shift;
00289 const int v_shift= i==0 ? 0 : v_chroma_shift;
00290
00291 buf->linesize[i]= picture.linesize[i];
00292
00293 buf->base[i]= av_malloc(size[i]+16);
00294 if(buf->base[i]==NULL) return -1;
00295 memset(buf->base[i], 128, size[i]);
00296
00297
00298 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
00299 buf->data[i] = buf->base[i];
00300 else
00301 buf->data[i] = buf->base[i] + ALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
00302 }
00303 if(size[1] && !size[2])
00304 ff_set_systematic_pal((uint32_t*)buf->data[1], s->pix_fmt);
00305 buf->width = s->width;
00306 buf->height = s->height;
00307 buf->pix_fmt= s->pix_fmt;
00308 pic->age= 256*256*256*64;
00309 }
00310 pic->type= FF_BUFFER_TYPE_INTERNAL;
00311
00312 for(i=0; i<4; i++){
00313 pic->base[i]= buf->base[i];
00314 pic->data[i]= buf->data[i];
00315 pic->linesize[i]= buf->linesize[i];
00316 }
00317 s->internal_buffer_count++;
00318
00319 pic->reordered_opaque= s->reordered_opaque;
00320
00321 if(s->debug&FF_DEBUG_BUFFERS)
00322 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
00323
00324 return 0;
00325 }
00326
00327 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
00328 int i;
00329 InternalBuffer *buf, *last;
00330
00331 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
00332 assert(s->internal_buffer_count);
00333
00334 buf = NULL;
00335 for(i=0; i<s->internal_buffer_count; i++){
00336 buf= &((InternalBuffer*)s->internal_buffer)[i];
00337 if(buf->data[0] == pic->data[0])
00338 break;
00339 }
00340 assert(i < s->internal_buffer_count);
00341 s->internal_buffer_count--;
00342 last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
00343
00344 FFSWAP(InternalBuffer, *buf, *last);
00345
00346 for(i=0; i<4; i++){
00347 pic->data[i]=NULL;
00348
00349 }
00350
00351
00352 if(s->debug&FF_DEBUG_BUFFERS)
00353 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
00354 }
00355
00356 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
00357 AVFrame temp_pic;
00358 int i;
00359
00360
00361 if(pic->data[0] == NULL) {
00362
00363 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
00364 return s->get_buffer(s, pic);
00365 }
00366
00367
00368 if(pic->type == FF_BUFFER_TYPE_INTERNAL)
00369 return 0;
00370
00371
00372
00373
00374 temp_pic = *pic;
00375 for(i = 0; i < 4; i++)
00376 pic->data[i] = pic->base[i] = NULL;
00377 pic->opaque = NULL;
00378
00379 if (s->get_buffer(s, pic))
00380 return -1;
00381
00382 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
00383 s->height);
00384 s->release_buffer(s, &temp_pic);
00385 return 0;
00386 }
00387
00388 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
00389 int i;
00390
00391 for(i=0; i<count; i++){
00392 int r= func(c, (char*)arg + i*size);
00393 if(ret) ret[i]= r;
00394 }
00395 return 0;
00396 }
00397
00398 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
00399 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
00400 ++fmt;
00401 return fmt[0];
00402 }
00403
00404 void avcodec_get_frame_defaults(AVFrame *pic){
00405 memset(pic, 0, sizeof(AVFrame));
00406
00407 pic->pts= AV_NOPTS_VALUE;
00408 pic->key_frame= 1;
00409 }
00410
00411 AVFrame *avcodec_alloc_frame(void){
00412 AVFrame *pic= av_malloc(sizeof(AVFrame));
00413
00414 if(pic==NULL) return NULL;
00415
00416 avcodec_get_frame_defaults(pic);
00417
00418 return pic;
00419 }
00420
00421 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
00422 {
00423 int ret= -1;
00424
00425
00426 if (ff_lockmgr_cb) {
00427 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
00428 return -1;
00429 }
00430
00431 entangled_thread_counter++;
00432 if(entangled_thread_counter != 1){
00433 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
00434 goto end;
00435 }
00436
00437 if(avctx->codec || !codec)
00438 goto end;
00439
00440 if (codec->priv_data_size > 0) {
00441 avctx->priv_data = av_mallocz(codec->priv_data_size);
00442 if (!avctx->priv_data) {
00443 ret = AVERROR(ENOMEM);
00444 goto end;
00445 }
00446 } else {
00447 avctx->priv_data = NULL;
00448 }
00449
00450 if(avctx->coded_width && avctx->coded_height)
00451 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
00452 else if(avctx->width && avctx->height)
00453 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
00454
00455 if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height)){
00456 av_freep(&avctx->priv_data);
00457 ret = AVERROR(EINVAL);
00458 goto end;
00459 }
00460
00461 avctx->codec = codec;
00462 avctx->codec_id = codec->id;
00463 avctx->frame_number = 0;
00464 if(avctx->codec->init){
00465 ret = avctx->codec->init(avctx);
00466 if (ret < 0) {
00467 av_freep(&avctx->priv_data);
00468 avctx->codec= NULL;
00469 goto end;
00470 }
00471 }
00472 ret=0;
00473 end:
00474 entangled_thread_counter--;
00475
00476
00477 if (ff_lockmgr_cb) {
00478 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
00479 }
00480 return ret;
00481 }
00482
00483 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
00484 const short *samples)
00485 {
00486 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
00487 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
00488 return -1;
00489 }
00490 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
00491 int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
00492 avctx->frame_number++;
00493 return ret;
00494 }else
00495 return 0;
00496 }
00497
00498 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
00499 const AVFrame *pict)
00500 {
00501 if(buf_size < FF_MIN_BUFFER_SIZE){
00502 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
00503 return -1;
00504 }
00505 if(avcodec_check_dimensions(avctx,avctx->width,avctx->height))
00506 return -1;
00507 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
00508 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
00509 avctx->frame_number++;
00510 emms_c();
00511
00512 return ret;
00513 }else
00514 return 0;
00515 }
00516
00517 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
00518 const AVSubtitle *sub)
00519 {
00520 int ret;
00521 if(sub->start_display_time) {
00522 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
00523 return -1;
00524 }
00525 if(sub->num_rects == 0 || !sub->rects)
00526 return -1;
00527 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
00528 avctx->frame_number++;
00529 return ret;
00530 }
00531
00532 int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
00533 int *got_picture_ptr,
00534 const uint8_t *buf, int buf_size)
00535 {
00536 int ret;
00537
00538 *got_picture_ptr= 0;
00539 if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height))
00540 return -1;
00541 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
00542 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
00543 buf, buf_size);
00544
00545 emms_c();
00546
00547 if (*got_picture_ptr)
00548 avctx->frame_number++;
00549 }else
00550 ret= 0;
00551
00552 return ret;
00553 }
00554
00555 int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
00556 int *frame_size_ptr,
00557 const uint8_t *buf, int buf_size)
00558 {
00559 int ret;
00560
00561 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
00562
00563 if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
00564 av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
00565 return -1;
00566 }
00567 if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
00568 *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
00569 av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
00570 return -1;
00571 }
00572
00573 ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
00574 buf, buf_size);
00575 avctx->frame_number++;
00576 }else{
00577 ret= 0;
00578 *frame_size_ptr=0;
00579 }
00580 return ret;
00581 }
00582
00583 int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
00584 int *got_sub_ptr,
00585 const uint8_t *buf, int buf_size)
00586 {
00587 int ret;
00588
00589 *got_sub_ptr = 0;
00590 ret = avctx->codec->decode(avctx, sub, got_sub_ptr,
00591 buf, buf_size);
00592 if (*got_sub_ptr)
00593 avctx->frame_number++;
00594 return ret;
00595 }
00596
00597 int avcodec_close(AVCodecContext *avctx)
00598 {
00599
00600 if (ff_lockmgr_cb) {
00601 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
00602 return -1;
00603 }
00604
00605 entangled_thread_counter++;
00606 if(entangled_thread_counter != 1){
00607 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
00608 entangled_thread_counter--;
00609 return -1;
00610 }
00611
00612 if (HAVE_THREADS && avctx->thread_opaque)
00613 avcodec_thread_free(avctx);
00614 if (avctx->codec->close)
00615 avctx->codec->close(avctx);
00616 avcodec_default_free_buffers(avctx);
00617 av_freep(&avctx->priv_data);
00618 avctx->codec = NULL;
00619 entangled_thread_counter--;
00620
00621
00622 if (ff_lockmgr_cb) {
00623 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
00624 }
00625 return 0;
00626 }
00627
00628 AVCodec *avcodec_find_encoder(enum CodecID id)
00629 {
00630 AVCodec *p;
00631 p = first_avcodec;
00632 while (p) {
00633 if (p->encode != NULL && p->id == id)
00634 return p;
00635 p = p->next;
00636 }
00637 return NULL;
00638 }
00639
00640 AVCodec *avcodec_find_encoder_by_name(const char *name)
00641 {
00642 AVCodec *p;
00643 if (!name)
00644 return NULL;
00645 p = first_avcodec;
00646 while (p) {
00647 if (p->encode != NULL && strcmp(name,p->name) == 0)
00648 return p;
00649 p = p->next;
00650 }
00651 return NULL;
00652 }
00653
00654 AVCodec *avcodec_find_decoder(enum CodecID id)
00655 {
00656 AVCodec *p;
00657 p = first_avcodec;
00658 while (p) {
00659 if (p->decode != NULL && p->id == id)
00660 return p;
00661 p = p->next;
00662 }
00663 return NULL;
00664 }
00665
00666 AVCodec *avcodec_find_decoder_by_name(const char *name)
00667 {
00668 AVCodec *p;
00669 if (!name)
00670 return NULL;
00671 p = first_avcodec;
00672 while (p) {
00673 if (p->decode != NULL && strcmp(name,p->name) == 0)
00674 return p;
00675 p = p->next;
00676 }
00677 return NULL;
00678 }
00679
00680 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
00681 {
00682 const char *codec_name;
00683 AVCodec *p;
00684 char buf1[32];
00685 int bitrate;
00686 AVRational display_aspect_ratio;
00687
00688 if (encode)
00689 p = avcodec_find_encoder(enc->codec_id);
00690 else
00691 p = avcodec_find_decoder(enc->codec_id);
00692
00693 if (p) {
00694 codec_name = p->name;
00695 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
00696
00697
00698 codec_name = "mpeg2ts";
00699 } else if (enc->codec_name[0] != '\0') {
00700 codec_name = enc->codec_name;
00701 } else {
00702
00703 if( isprint(enc->codec_tag&0xFF) && isprint((enc->codec_tag>>8)&0xFF)
00704 && isprint((enc->codec_tag>>16)&0xFF) && isprint((enc->codec_tag>>24)&0xFF)){
00705 snprintf(buf1, sizeof(buf1), "%c%c%c%c / 0x%04X",
00706 enc->codec_tag & 0xff,
00707 (enc->codec_tag >> 8) & 0xff,
00708 (enc->codec_tag >> 16) & 0xff,
00709 (enc->codec_tag >> 24) & 0xff,
00710 enc->codec_tag);
00711 } else {
00712 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
00713 }
00714 codec_name = buf1;
00715 }
00716
00717 switch(enc->codec_type) {
00718 case CODEC_TYPE_VIDEO:
00719 snprintf(buf, buf_size,
00720 "Video: %s%s",
00721 codec_name, enc->mb_decision ? " (hq)" : "");
00722 if (enc->pix_fmt != PIX_FMT_NONE) {
00723 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00724 ", %s",
00725 avcodec_get_pix_fmt_name(enc->pix_fmt));
00726 }
00727 if (enc->width) {
00728 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00729 ", %dx%d",
00730 enc->width, enc->height);
00731 if (enc->sample_aspect_ratio.num) {
00732 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
00733 enc->width*enc->sample_aspect_ratio.num,
00734 enc->height*enc->sample_aspect_ratio.den,
00735 1024*1024);
00736 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00737 " [PAR %d:%d DAR %d:%d]",
00738 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
00739 display_aspect_ratio.num, display_aspect_ratio.den);
00740 }
00741 if(av_log_get_level() >= AV_LOG_DEBUG){
00742 int g= av_gcd(enc->time_base.num, enc->time_base.den);
00743 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00744 ", %d/%d",
00745 enc->time_base.num/g, enc->time_base.den/g);
00746 }
00747 }
00748 if (encode) {
00749 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00750 ", q=%d-%d", enc->qmin, enc->qmax);
00751 }
00752 bitrate = enc->bit_rate;
00753 break;
00754 case CODEC_TYPE_AUDIO:
00755 snprintf(buf, buf_size,
00756 "Audio: %s",
00757 codec_name);
00758 if (enc->sample_rate) {
00759 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00760 ", %d Hz", enc->sample_rate);
00761 }
00762 av_strlcat(buf, ", ", buf_size);
00763 avcodec_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
00764 if (enc->sample_fmt != SAMPLE_FMT_NONE) {
00765 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00766 ", %s", avcodec_get_sample_fmt_name(enc->sample_fmt));
00767 }
00768
00769
00770 switch(enc->codec_id) {
00771 case CODEC_ID_PCM_F64BE:
00772 case CODEC_ID_PCM_F64LE:
00773 bitrate = enc->sample_rate * enc->channels * 64;
00774 break;
00775 case CODEC_ID_PCM_S32LE:
00776 case CODEC_ID_PCM_S32BE:
00777 case CODEC_ID_PCM_U32LE:
00778 case CODEC_ID_PCM_U32BE:
00779 case CODEC_ID_PCM_F32BE:
00780 case CODEC_ID_PCM_F32LE:
00781 bitrate = enc->sample_rate * enc->channels * 32;
00782 break;
00783 case CODEC_ID_PCM_S24LE:
00784 case CODEC_ID_PCM_S24BE:
00785 case CODEC_ID_PCM_U24LE:
00786 case CODEC_ID_PCM_U24BE:
00787 case CODEC_ID_PCM_S24DAUD:
00788 bitrate = enc->sample_rate * enc->channels * 24;
00789 break;
00790 case CODEC_ID_PCM_S16LE:
00791 case CODEC_ID_PCM_S16BE:
00792 case CODEC_ID_PCM_S16LE_PLANAR:
00793 case CODEC_ID_PCM_U16LE:
00794 case CODEC_ID_PCM_U16BE:
00795 bitrate = enc->sample_rate * enc->channels * 16;
00796 break;
00797 case CODEC_ID_PCM_S8:
00798 case CODEC_ID_PCM_U8:
00799 case CODEC_ID_PCM_ALAW:
00800 case CODEC_ID_PCM_MULAW:
00801 case CODEC_ID_PCM_ZORK:
00802 bitrate = enc->sample_rate * enc->channels * 8;
00803 break;
00804 default:
00805 bitrate = enc->bit_rate;
00806 break;
00807 }
00808 break;
00809 case CODEC_TYPE_DATA:
00810 snprintf(buf, buf_size, "Data: %s", codec_name);
00811 bitrate = enc->bit_rate;
00812 break;
00813 case CODEC_TYPE_SUBTITLE:
00814 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
00815 bitrate = enc->bit_rate;
00816 break;
00817 case CODEC_TYPE_ATTACHMENT:
00818 snprintf(buf, buf_size, "Attachment: %s", codec_name);
00819 bitrate = enc->bit_rate;
00820 break;
00821 default:
00822 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
00823 return;
00824 }
00825 if (encode) {
00826 if (enc->flags & CODEC_FLAG_PASS1)
00827 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00828 ", pass 1");
00829 if (enc->flags & CODEC_FLAG_PASS2)
00830 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00831 ", pass 2");
00832 }
00833 if (bitrate != 0) {
00834 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00835 ", %d kb/s", bitrate / 1000);
00836 }
00837 }
00838
00839 unsigned avcodec_version( void )
00840 {
00841 return LIBAVCODEC_VERSION_INT;
00842 }
00843
00844 void avcodec_init(void)
00845 {
00846 static int initialized = 0;
00847
00848 if (initialized != 0)
00849 return;
00850 initialized = 1;
00851
00852 dsputil_static_init();
00853 }
00854
00855 void avcodec_flush_buffers(AVCodecContext *avctx)
00856 {
00857 if(avctx->codec->flush)
00858 avctx->codec->flush(avctx);
00859 }
00860
00861 void avcodec_default_free_buffers(AVCodecContext *s){
00862 int i, j;
00863
00864 if(s->internal_buffer==NULL) return;
00865
00866 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
00867 InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
00868 for(j=0; j<4; j++){
00869 av_freep(&buf->base[j]);
00870 buf->data[j]= NULL;
00871 }
00872 }
00873 av_freep(&s->internal_buffer);
00874
00875 s->internal_buffer_count=0;
00876 }
00877
00878 char av_get_pict_type_char(int pict_type){
00879 switch(pict_type){
00880 case FF_I_TYPE: return 'I';
00881 case FF_P_TYPE: return 'P';
00882 case FF_B_TYPE: return 'B';
00883 case FF_S_TYPE: return 'S';
00884 case FF_SI_TYPE:return 'i';
00885 case FF_SP_TYPE:return 'p';
00886 case FF_BI_TYPE:return 'b';
00887 default: return '?';
00888 }
00889 }
00890
00891 int av_get_bits_per_sample(enum CodecID codec_id){
00892 switch(codec_id){
00893 case CODEC_ID_ADPCM_SBPRO_2:
00894 return 2;
00895 case CODEC_ID_ADPCM_SBPRO_3:
00896 return 3;
00897 case CODEC_ID_ADPCM_SBPRO_4:
00898 case CODEC_ID_ADPCM_CT:
00899 return 4;
00900 case CODEC_ID_PCM_ALAW:
00901 case CODEC_ID_PCM_MULAW:
00902 case CODEC_ID_PCM_S8:
00903 case CODEC_ID_PCM_U8:
00904 case CODEC_ID_PCM_ZORK:
00905 return 8;
00906 case CODEC_ID_PCM_S16BE:
00907 case CODEC_ID_PCM_S16LE:
00908 case CODEC_ID_PCM_S16LE_PLANAR:
00909 case CODEC_ID_PCM_U16BE:
00910 case CODEC_ID_PCM_U16LE:
00911 return 16;
00912 case CODEC_ID_PCM_S24DAUD:
00913 case CODEC_ID_PCM_S24BE:
00914 case CODEC_ID_PCM_S24LE:
00915 case CODEC_ID_PCM_U24BE:
00916 case CODEC_ID_PCM_U24LE:
00917 return 24;
00918 case CODEC_ID_PCM_S32BE:
00919 case CODEC_ID_PCM_S32LE:
00920 case CODEC_ID_PCM_U32BE:
00921 case CODEC_ID_PCM_U32LE:
00922 case CODEC_ID_PCM_F32BE:
00923 case CODEC_ID_PCM_F32LE:
00924 return 32;
00925 case CODEC_ID_PCM_F64BE:
00926 case CODEC_ID_PCM_F64LE:
00927 return 64;
00928 default:
00929 return 0;
00930 }
00931 }
00932
00933 int av_get_bits_per_sample_format(enum SampleFormat sample_fmt) {
00934 switch (sample_fmt) {
00935 case SAMPLE_FMT_U8:
00936 return 8;
00937 case SAMPLE_FMT_S16:
00938 return 16;
00939 case SAMPLE_FMT_S32:
00940 case SAMPLE_FMT_FLT:
00941 return 32;
00942 case SAMPLE_FMT_DBL:
00943 return 64;
00944 default:
00945 return 0;
00946 }
00947 }
00948
00949 #if !HAVE_THREADS
00950 int avcodec_thread_init(AVCodecContext *s, int thread_count){
00951 return -1;
00952 }
00953 #endif
00954
00955 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
00956 {
00957 unsigned int n = 0;
00958
00959 while(v >= 0xff) {
00960 *s++ = 0xff;
00961 v -= 0xff;
00962 n++;
00963 }
00964 *s = v;
00965 n++;
00966 return n;
00967 }
00968
00969
00970
00971
00972
00973
00974 int av_tempfile(char *prefix, char **filename) {
00975 int fd=-1;
00976 #if !HAVE_MKSTEMP
00977 *filename = tempnam(".", prefix);
00978 #else
00979 size_t len = strlen(prefix) + 12;
00980 *filename = av_malloc(len);
00981 #endif
00982
00983 if (*filename == NULL) {
00984 av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
00985 return -1;
00986 }
00987 #if !HAVE_MKSTEMP
00988 fd = open(*filename, O_RDWR | O_BINARY | O_CREAT, 0444);
00989 #else
00990 snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
00991 fd = mkstemp(*filename);
00992 if (fd < 0) {
00993 snprintf(*filename, len, "./%sXXXXXX", prefix);
00994 fd = mkstemp(*filename);
00995 }
00996 #endif
00997
00998 if (fd < 0) {
00999 av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
01000 return -1;
01001 }
01002 return fd;
01003 }
01004
01005 typedef struct {
01006 const char *abbr;
01007 int width, height;
01008 } VideoFrameSizeAbbr;
01009
01010 typedef struct {
01011 const char *abbr;
01012 int rate_num, rate_den;
01013 } VideoFrameRateAbbr;
01014
01015 static const VideoFrameSizeAbbr video_frame_size_abbrs[] = {
01016 { "ntsc", 720, 480 },
01017 { "pal", 720, 576 },
01018 { "qntsc", 352, 240 },
01019 { "qpal", 352, 288 },
01020 { "sntsc", 640, 480 },
01021 { "spal", 768, 576 },
01022 { "film", 352, 240 },
01023 { "ntsc-film", 352, 240 },
01024 { "sqcif", 128, 96 },
01025 { "qcif", 176, 144 },
01026 { "cif", 352, 288 },
01027 { "4cif", 704, 576 },
01028 { "qqvga", 160, 120 },
01029 { "qvga", 320, 240 },
01030 { "vga", 640, 480 },
01031 { "svga", 800, 600 },
01032 { "xga", 1024, 768 },
01033 { "uxga", 1600,1200 },
01034 { "qxga", 2048,1536 },
01035 { "sxga", 1280,1024 },
01036 { "qsxga", 2560,2048 },
01037 { "hsxga", 5120,4096 },
01038 { "wvga", 852, 480 },
01039 { "wxga", 1366, 768 },
01040 { "wsxga", 1600,1024 },
01041 { "wuxga", 1920,1200 },
01042 { "woxga", 2560,1600 },
01043 { "wqsxga", 3200,2048 },
01044 { "wquxga", 3840,2400 },
01045 { "whsxga", 6400,4096 },
01046 { "whuxga", 7680,4800 },
01047 { "cga", 320, 200 },
01048 { "ega", 640, 350 },
01049 { "hd480", 852, 480 },
01050 { "hd720", 1280, 720 },
01051 { "hd1080", 1920,1080 },
01052 };
01053
01054 static const VideoFrameRateAbbr video_frame_rate_abbrs[]= {
01055 { "ntsc", 30000, 1001 },
01056 { "pal", 25, 1 },
01057 { "qntsc", 30000, 1001 },
01058 { "qpal", 25, 1 },
01059 { "sntsc", 30000, 1001 },
01060 { "spal", 25, 1 },
01061 { "film", 24, 1 },
01062 { "ntsc-film", 24000, 1001 },
01063 };
01064
01065 int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
01066 {
01067 int i;
01068 int n = FF_ARRAY_ELEMS(video_frame_size_abbrs);
01069 char *p;
01070 int frame_width = 0, frame_height = 0;
01071
01072 for(i=0;i<n;i++) {
01073 if (!strcmp(video_frame_size_abbrs[i].abbr, str)) {
01074 frame_width = video_frame_size_abbrs[i].width;
01075 frame_height = video_frame_size_abbrs[i].height;
01076 break;
01077 }
01078 }
01079 if (i == n) {
01080 p = str;
01081 frame_width = strtol(p, &p, 10);
01082 if (*p)
01083 p++;
01084 frame_height = strtol(p, &p, 10);
01085 }
01086 if (frame_width <= 0 || frame_height <= 0)
01087 return -1;
01088 *width_ptr = frame_width;
01089 *height_ptr = frame_height;
01090 return 0;
01091 }
01092
01093 int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
01094 {
01095 int i;
01096 int n = FF_ARRAY_ELEMS(video_frame_rate_abbrs);
01097 char* cp;
01098
01099
01100 for (i = 0; i < n; ++i)
01101 if (!strcmp(video_frame_rate_abbrs[i].abbr, arg)) {
01102 frame_rate->num = video_frame_rate_abbrs[i].rate_num;
01103 frame_rate->den = video_frame_rate_abbrs[i].rate_den;
01104 return 0;
01105 }
01106
01107
01108 cp = strchr(arg, '/');
01109 if (!cp)
01110 cp = strchr(arg, ':');
01111 if (cp) {
01112 char* cpp;
01113 frame_rate->num = strtol(arg, &cpp, 10);
01114 if (cpp != arg || cpp == cp)
01115 frame_rate->den = strtol(cp+1, &cpp, 10);
01116 else
01117 frame_rate->num = 0;
01118 }
01119 else {
01120
01121 AVRational time_base = av_d2q(strtod(arg, 0), 1001000);
01122 frame_rate->den = time_base.den;
01123 frame_rate->num = time_base.num;
01124 }
01125 if (!frame_rate->num || !frame_rate->den)
01126 return -1;
01127 else
01128 return 0;
01129 }
01130
01131 void ff_log_missing_feature(void *avc, const char *feature, int want_sample)
01132 {
01133 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
01134 "version to the newest one from SVN. If the problem still "
01135 "occurs, it means that your file has a feature which has not "
01136 "been implemented.", feature);
01137 if(want_sample)
01138 ff_log_ask_for_sample(avc, NULL);
01139 else
01140 av_log(avc, AV_LOG_WARNING, "\n");
01141 }
01142
01143 void ff_log_ask_for_sample(void *avc, const char *msg)
01144 {
01145 if (msg)
01146 av_log(avc, AV_LOG_WARNING, "%s ", msg);
01147 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
01148 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
01149 "and contact the ffmpeg-devel mailing list.\n");
01150 }
01151
01152 static AVHWAccel *first_hwaccel = NULL;
01153
01154 void av_register_hwaccel(AVHWAccel *hwaccel)
01155 {
01156 AVHWAccel **p = &first_hwaccel;
01157 while (*p)
01158 p = &(*p)->next;
01159 *p = hwaccel;
01160 hwaccel->next = NULL;
01161 }
01162
01163 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
01164 {
01165 return hwaccel ? hwaccel->next : first_hwaccel;
01166 }
01167
01168 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
01169 {
01170 AVHWAccel *hwaccel=NULL;
01171
01172 while((hwaccel= av_hwaccel_next(hwaccel))){
01173 if ( hwaccel->id == codec_id
01174 && hwaccel->pix_fmt == pix_fmt)
01175 return hwaccel;
01176 }
01177 return NULL;
01178 }
01179
01180 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
01181 {
01182 if (ff_lockmgr_cb) {
01183 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
01184 return -1;
01185 }
01186
01187 ff_lockmgr_cb = cb;
01188
01189 if (ff_lockmgr_cb) {
01190 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
01191 return -1;
01192 }
01193 return 0;
01194 }