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