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