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 AVERROR(EINVAL);
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, *experimental=NULL;
00728 p = first_avcodec;
00729 while (p) {
00730 if (p->encode != NULL && p->id == id) {
00731 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
00732 experimental = p;
00733 } else
00734 return p;
00735 }
00736 p = p->next;
00737 }
00738 return experimental;
00739 }
00740
00741 AVCodec *avcodec_find_encoder_by_name(const char *name)
00742 {
00743 AVCodec *p;
00744 if (!name)
00745 return NULL;
00746 p = first_avcodec;
00747 while (p) {
00748 if (p->encode != NULL && strcmp(name,p->name) == 0)
00749 return p;
00750 p = p->next;
00751 }
00752 return NULL;
00753 }
00754
00755 AVCodec *avcodec_find_decoder(enum CodecID id)
00756 {
00757 AVCodec *p;
00758 p = first_avcodec;
00759 while (p) {
00760 if (p->decode != NULL && p->id == id)
00761 return p;
00762 p = p->next;
00763 }
00764 return NULL;
00765 }
00766
00767 AVCodec *avcodec_find_decoder_by_name(const char *name)
00768 {
00769 AVCodec *p;
00770 if (!name)
00771 return NULL;
00772 p = first_avcodec;
00773 while (p) {
00774 if (p->decode != NULL && strcmp(name,p->name) == 0)
00775 return p;
00776 p = p->next;
00777 }
00778 return NULL;
00779 }
00780
00781 static int get_bit_rate(AVCodecContext *ctx)
00782 {
00783 int bit_rate;
00784 int bits_per_sample;
00785
00786 switch(ctx->codec_type) {
00787 case AVMEDIA_TYPE_VIDEO:
00788 case AVMEDIA_TYPE_DATA:
00789 case AVMEDIA_TYPE_SUBTITLE:
00790 case AVMEDIA_TYPE_ATTACHMENT:
00791 bit_rate = ctx->bit_rate;
00792 break;
00793 case AVMEDIA_TYPE_AUDIO:
00794 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
00795 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
00796 break;
00797 default:
00798 bit_rate = 0;
00799 break;
00800 }
00801 return bit_rate;
00802 }
00803
00804 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
00805 {
00806 const char *codec_name;
00807 AVCodec *p;
00808 char buf1[32];
00809 int bitrate;
00810 AVRational display_aspect_ratio;
00811
00812 if (encode)
00813 p = avcodec_find_encoder(enc->codec_id);
00814 else
00815 p = avcodec_find_decoder(enc->codec_id);
00816
00817 if (p) {
00818 codec_name = p->name;
00819 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
00820
00821
00822 codec_name = "mpeg2ts";
00823 } else if (enc->codec_name[0] != '\0') {
00824 codec_name = enc->codec_name;
00825 } else {
00826
00827 if( isprint(enc->codec_tag&0xFF) && isprint((enc->codec_tag>>8)&0xFF)
00828 && isprint((enc->codec_tag>>16)&0xFF) && isprint((enc->codec_tag>>24)&0xFF)){
00829 snprintf(buf1, sizeof(buf1), "%c%c%c%c / 0x%04X",
00830 enc->codec_tag & 0xff,
00831 (enc->codec_tag >> 8) & 0xff,
00832 (enc->codec_tag >> 16) & 0xff,
00833 (enc->codec_tag >> 24) & 0xff,
00834 enc->codec_tag);
00835 } else {
00836 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
00837 }
00838 codec_name = buf1;
00839 }
00840
00841 switch(enc->codec_type) {
00842 case AVMEDIA_TYPE_VIDEO:
00843 snprintf(buf, buf_size,
00844 "Video: %s%s",
00845 codec_name, enc->mb_decision ? " (hq)" : "");
00846 if (enc->pix_fmt != PIX_FMT_NONE) {
00847 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00848 ", %s",
00849 avcodec_get_pix_fmt_name(enc->pix_fmt));
00850 }
00851 if (enc->width) {
00852 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00853 ", %dx%d",
00854 enc->width, enc->height);
00855 if (enc->sample_aspect_ratio.num) {
00856 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
00857 enc->width*enc->sample_aspect_ratio.num,
00858 enc->height*enc->sample_aspect_ratio.den,
00859 1024*1024);
00860 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00861 " [PAR %d:%d DAR %d:%d]",
00862 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
00863 display_aspect_ratio.num, display_aspect_ratio.den);
00864 }
00865 if(av_log_get_level() >= AV_LOG_DEBUG){
00866 int g= av_gcd(enc->time_base.num, enc->time_base.den);
00867 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00868 ", %d/%d",
00869 enc->time_base.num/g, enc->time_base.den/g);
00870 }
00871 }
00872 if (encode) {
00873 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00874 ", q=%d-%d", enc->qmin, enc->qmax);
00875 }
00876 break;
00877 case AVMEDIA_TYPE_AUDIO:
00878 snprintf(buf, buf_size,
00879 "Audio: %s",
00880 codec_name);
00881 if (enc->sample_rate) {
00882 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00883 ", %d Hz", enc->sample_rate);
00884 }
00885 av_strlcat(buf, ", ", buf_size);
00886 avcodec_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
00887 if (enc->sample_fmt != SAMPLE_FMT_NONE) {
00888 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00889 ", %s", avcodec_get_sample_fmt_name(enc->sample_fmt));
00890 }
00891 break;
00892 case AVMEDIA_TYPE_DATA:
00893 snprintf(buf, buf_size, "Data: %s", codec_name);
00894 break;
00895 case AVMEDIA_TYPE_SUBTITLE:
00896 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
00897 break;
00898 case AVMEDIA_TYPE_ATTACHMENT:
00899 snprintf(buf, buf_size, "Attachment: %s", codec_name);
00900 break;
00901 default:
00902 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
00903 return;
00904 }
00905 if (encode) {
00906 if (enc->flags & CODEC_FLAG_PASS1)
00907 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00908 ", pass 1");
00909 if (enc->flags & CODEC_FLAG_PASS2)
00910 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00911 ", pass 2");
00912 }
00913 bitrate = get_bit_rate(enc);
00914 if (bitrate != 0) {
00915 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00916 ", %d kb/s", bitrate / 1000);
00917 }
00918 }
00919
00920 unsigned avcodec_version( void )
00921 {
00922 return LIBAVCODEC_VERSION_INT;
00923 }
00924
00925 const char *avcodec_configuration(void)
00926 {
00927 return FFMPEG_CONFIGURATION;
00928 }
00929
00930 const char *avcodec_license(void)
00931 {
00932 #define LICENSE_PREFIX "libavcodec license: "
00933 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
00934 }
00935
00936 void avcodec_init(void)
00937 {
00938 static int initialized = 0;
00939
00940 if (initialized != 0)
00941 return;
00942 initialized = 1;
00943
00944 dsputil_static_init();
00945 }
00946
00947 void avcodec_flush_buffers(AVCodecContext *avctx)
00948 {
00949 if(avctx->codec->flush)
00950 avctx->codec->flush(avctx);
00951 }
00952
00953 void avcodec_default_free_buffers(AVCodecContext *s){
00954 int i, j;
00955
00956 if(s->internal_buffer==NULL) return;
00957
00958 if (s->internal_buffer_count)
00959 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
00960 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
00961 InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
00962 for(j=0; j<4; j++){
00963 av_freep(&buf->base[j]);
00964 buf->data[j]= NULL;
00965 }
00966 }
00967 av_freep(&s->internal_buffer);
00968
00969 s->internal_buffer_count=0;
00970 }
00971
00972 char av_get_pict_type_char(int pict_type){
00973 switch(pict_type){
00974 case FF_I_TYPE: return 'I';
00975 case FF_P_TYPE: return 'P';
00976 case FF_B_TYPE: return 'B';
00977 case FF_S_TYPE: return 'S';
00978 case FF_SI_TYPE:return 'i';
00979 case FF_SP_TYPE:return 'p';
00980 case FF_BI_TYPE:return 'b';
00981 default: return '?';
00982 }
00983 }
00984
00985 int av_get_bits_per_sample(enum CodecID codec_id){
00986 switch(codec_id){
00987 case CODEC_ID_ADPCM_SBPRO_2:
00988 return 2;
00989 case CODEC_ID_ADPCM_SBPRO_3:
00990 return 3;
00991 case CODEC_ID_ADPCM_SBPRO_4:
00992 case CODEC_ID_ADPCM_CT:
00993 case CODEC_ID_ADPCM_IMA_WAV:
00994 case CODEC_ID_ADPCM_MS:
00995 case CODEC_ID_ADPCM_YAMAHA:
00996 return 4;
00997 case CODEC_ID_PCM_ALAW:
00998 case CODEC_ID_PCM_MULAW:
00999 case CODEC_ID_PCM_S8:
01000 case CODEC_ID_PCM_U8:
01001 case CODEC_ID_PCM_ZORK:
01002 return 8;
01003 case CODEC_ID_PCM_S16BE:
01004 case CODEC_ID_PCM_S16LE:
01005 case CODEC_ID_PCM_S16LE_PLANAR:
01006 case CODEC_ID_PCM_U16BE:
01007 case CODEC_ID_PCM_U16LE:
01008 return 16;
01009 case CODEC_ID_PCM_S24DAUD:
01010 case CODEC_ID_PCM_S24BE:
01011 case CODEC_ID_PCM_S24LE:
01012 case CODEC_ID_PCM_U24BE:
01013 case CODEC_ID_PCM_U24LE:
01014 return 24;
01015 case CODEC_ID_PCM_S32BE:
01016 case CODEC_ID_PCM_S32LE:
01017 case CODEC_ID_PCM_U32BE:
01018 case CODEC_ID_PCM_U32LE:
01019 case CODEC_ID_PCM_F32BE:
01020 case CODEC_ID_PCM_F32LE:
01021 return 32;
01022 case CODEC_ID_PCM_F64BE:
01023 case CODEC_ID_PCM_F64LE:
01024 return 64;
01025 default:
01026 return 0;
01027 }
01028 }
01029
01030 int av_get_bits_per_sample_format(enum SampleFormat sample_fmt) {
01031 switch (sample_fmt) {
01032 case SAMPLE_FMT_U8:
01033 return 8;
01034 case SAMPLE_FMT_S16:
01035 return 16;
01036 case SAMPLE_FMT_S32:
01037 case SAMPLE_FMT_FLT:
01038 return 32;
01039 case SAMPLE_FMT_DBL:
01040 return 64;
01041 default:
01042 return 0;
01043 }
01044 }
01045
01046 #if !HAVE_THREADS
01047 int avcodec_thread_init(AVCodecContext *s, int thread_count){
01048 s->thread_count = thread_count;
01049 return -1;
01050 }
01051 #endif
01052
01053 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
01054 {
01055 unsigned int n = 0;
01056
01057 while(v >= 0xff) {
01058 *s++ = 0xff;
01059 v -= 0xff;
01060 n++;
01061 }
01062 *s = v;
01063 n++;
01064 return n;
01065 }
01066
01067
01068
01069
01070
01071
01072 int av_tempfile(char *prefix, char **filename) {
01073 int fd=-1;
01074 #if !HAVE_MKSTEMP
01075 *filename = tempnam(".", prefix);
01076 #else
01077 size_t len = strlen(prefix) + 12;
01078 *filename = av_malloc(len);
01079 #endif
01080
01081 if (*filename == NULL) {
01082 av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
01083 return -1;
01084 }
01085 #if !HAVE_MKSTEMP
01086 fd = open(*filename, O_RDWR | O_BINARY | O_CREAT, 0444);
01087 #else
01088 snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
01089 fd = mkstemp(*filename);
01090 if (fd < 0) {
01091 snprintf(*filename, len, "./%sXXXXXX", prefix);
01092 fd = mkstemp(*filename);
01093 }
01094 #endif
01095
01096 if (fd < 0) {
01097 av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
01098 return -1;
01099 }
01100 return fd;
01101 }
01102
01103 typedef struct {
01104 const char *abbr;
01105 int width, height;
01106 } VideoFrameSizeAbbr;
01107
01108 typedef struct {
01109 const char *abbr;
01110 int rate_num, rate_den;
01111 } VideoFrameRateAbbr;
01112
01113 static const VideoFrameSizeAbbr video_frame_size_abbrs[] = {
01114 { "ntsc", 720, 480 },
01115 { "pal", 720, 576 },
01116 { "qntsc", 352, 240 },
01117 { "qpal", 352, 288 },
01118 { "sntsc", 640, 480 },
01119 { "spal", 768, 576 },
01120 { "film", 352, 240 },
01121 { "ntsc-film", 352, 240 },
01122 { "sqcif", 128, 96 },
01123 { "qcif", 176, 144 },
01124 { "cif", 352, 288 },
01125 { "4cif", 704, 576 },
01126 { "16cif", 1408,1152 },
01127 { "qqvga", 160, 120 },
01128 { "qvga", 320, 240 },
01129 { "vga", 640, 480 },
01130 { "svga", 800, 600 },
01131 { "xga", 1024, 768 },
01132 { "uxga", 1600,1200 },
01133 { "qxga", 2048,1536 },
01134 { "sxga", 1280,1024 },
01135 { "qsxga", 2560,2048 },
01136 { "hsxga", 5120,4096 },
01137 { "wvga", 852, 480 },
01138 { "wxga", 1366, 768 },
01139 { "wsxga", 1600,1024 },
01140 { "wuxga", 1920,1200 },
01141 { "woxga", 2560,1600 },
01142 { "wqsxga", 3200,2048 },
01143 { "wquxga", 3840,2400 },
01144 { "whsxga", 6400,4096 },
01145 { "whuxga", 7680,4800 },
01146 { "cga", 320, 200 },
01147 { "ega", 640, 350 },
01148 { "hd480", 852, 480 },
01149 { "hd720", 1280, 720 },
01150 { "hd1080", 1920,1080 },
01151 };
01152
01153 static const VideoFrameRateAbbr video_frame_rate_abbrs[]= {
01154 { "ntsc", 30000, 1001 },
01155 { "pal", 25, 1 },
01156 { "qntsc", 30000, 1001 },
01157 { "qpal", 25, 1 },
01158 { "sntsc", 30000, 1001 },
01159 { "spal", 25, 1 },
01160 { "film", 24, 1 },
01161 { "ntsc-film", 24000, 1001 },
01162 };
01163
01164 int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
01165 {
01166 int i;
01167 int n = FF_ARRAY_ELEMS(video_frame_size_abbrs);
01168 char *p;
01169 int frame_width = 0, frame_height = 0;
01170
01171 for(i=0;i<n;i++) {
01172 if (!strcmp(video_frame_size_abbrs[i].abbr, str)) {
01173 frame_width = video_frame_size_abbrs[i].width;
01174 frame_height = video_frame_size_abbrs[i].height;
01175 break;
01176 }
01177 }
01178 if (i == n) {
01179 p = str;
01180 frame_width = strtol(p, &p, 10);
01181 if (*p)
01182 p++;
01183 frame_height = strtol(p, &p, 10);
01184 }
01185 if (frame_width <= 0 || frame_height <= 0)
01186 return -1;
01187 *width_ptr = frame_width;
01188 *height_ptr = frame_height;
01189 return 0;
01190 }
01191
01192 int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
01193 {
01194 int i;
01195 int n = FF_ARRAY_ELEMS(video_frame_rate_abbrs);
01196 char* cp;
01197
01198
01199 for (i = 0; i < n; ++i)
01200 if (!strcmp(video_frame_rate_abbrs[i].abbr, arg)) {
01201 frame_rate->num = video_frame_rate_abbrs[i].rate_num;
01202 frame_rate->den = video_frame_rate_abbrs[i].rate_den;
01203 return 0;
01204 }
01205
01206
01207 cp = strchr(arg, '/');
01208 if (!cp)
01209 cp = strchr(arg, ':');
01210 if (cp) {
01211 char* cpp;
01212 frame_rate->num = strtol(arg, &cpp, 10);
01213 if (cpp != arg || cpp == cp)
01214 frame_rate->den = strtol(cp+1, &cpp, 10);
01215 else
01216 frame_rate->num = 0;
01217 }
01218 else {
01219
01220 AVRational time_base = av_d2q(strtod(arg, 0), 1001000);
01221 frame_rate->den = time_base.den;
01222 frame_rate->num = time_base.num;
01223 }
01224 if (!frame_rate->num || !frame_rate->den)
01225 return -1;
01226 else
01227 return 0;
01228 }
01229
01230 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
01231 int i;
01232 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
01233 return i;
01234 }
01235
01236 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
01237 {
01238 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
01239 "version to the newest one from SVN. If the problem still "
01240 "occurs, it means that your file has a feature which has not "
01241 "been implemented.", feature);
01242 if(want_sample)
01243 av_log_ask_for_sample(avc, NULL);
01244 else
01245 av_log(avc, AV_LOG_WARNING, "\n");
01246 }
01247
01248 void av_log_ask_for_sample(void *avc, const char *msg)
01249 {
01250 if (msg)
01251 av_log(avc, AV_LOG_WARNING, "%s ", msg);
01252 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
01253 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
01254 "and contact the ffmpeg-devel mailing list.\n");
01255 }
01256
01257 static AVHWAccel *first_hwaccel = NULL;
01258
01259 void av_register_hwaccel(AVHWAccel *hwaccel)
01260 {
01261 AVHWAccel **p = &first_hwaccel;
01262 while (*p)
01263 p = &(*p)->next;
01264 *p = hwaccel;
01265 hwaccel->next = NULL;
01266 }
01267
01268 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
01269 {
01270 return hwaccel ? hwaccel->next : first_hwaccel;
01271 }
01272
01273 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
01274 {
01275 AVHWAccel *hwaccel=NULL;
01276
01277 while((hwaccel= av_hwaccel_next(hwaccel))){
01278 if ( hwaccel->id == codec_id
01279 && hwaccel->pix_fmt == pix_fmt)
01280 return hwaccel;
01281 }
01282 return NULL;
01283 }
01284
01285 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
01286 {
01287 if (ff_lockmgr_cb) {
01288 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
01289 return -1;
01290 }
01291
01292 ff_lockmgr_cb = cb;
01293
01294 if (ff_lockmgr_cb) {
01295 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
01296 return -1;
01297 }
01298 return 0;
01299 }