00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #include "libavutil/avstring.h"
00029 #include "libavutil/integer.h"
00030 #include "libavutil/crc.h"
00031 #include "libavutil/pixdesc.h"
00032 #include "libavcore/audioconvert.h"
00033 #include "libavcore/imgutils.h"
00034 #include "libavcore/internal.h"
00035 #include "libavcore/samplefmt.h"
00036 #include "avcodec.h"
00037 #include "dsputil.h"
00038 #include "libavutil/opt.h"
00039 #include "imgconvert.h"
00040 #include "audioconvert.h"
00041 #include "internal.h"
00042 #include <stdlib.h>
00043 #include <stdarg.h>
00044 #include <limits.h>
00045 #include <float.h>
00046
00047 static int volatile entangled_thread_counter=0;
00048 int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
00049 static void *codec_mutex;
00050
00051 void *av_fast_realloc(void *ptr, unsigned int *size, FF_INTERNALC_MEM_TYPE min_size)
00052 {
00053 if(min_size < *size)
00054 return ptr;
00055
00056 min_size= FFMAX(17*min_size/16 + 32, min_size);
00057
00058 ptr= av_realloc(ptr, min_size);
00059 if(!ptr)
00060 min_size= 0;
00061
00062 *size= min_size;
00063
00064 return ptr;
00065 }
00066
00067 void av_fast_malloc(void *ptr, unsigned int *size, FF_INTERNALC_MEM_TYPE min_size)
00068 {
00069 void **p = ptr;
00070 if (min_size < *size)
00071 return;
00072 min_size= FFMAX(17*min_size/16 + 32, min_size);
00073 av_free(*p);
00074 *p = av_malloc(min_size);
00075 if (!*p) min_size = 0;
00076 *size= min_size;
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 || s->codec_id == CODEC_ID_H264)
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 || s->lowres)
00189 *height+=2;
00190
00191
00192 linesize_align[0] =
00193 linesize_align[1] =
00194 linesize_align[2] =
00195 linesize_align[3] = STRIDE_ALIGN;
00196
00197
00198
00199
00200 #if HAVE_MMX
00201 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
00202 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
00203 s->codec_id == CODEC_ID_VP6A) {
00204 linesize_align[0] =
00205 linesize_align[1] =
00206 linesize_align[2] = 16;
00207 }
00208 #endif
00209 }
00210
00211 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
00212 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
00213 int linesize_align[4];
00214 int align;
00215 avcodec_align_dimensions2(s, width, height, linesize_align);
00216 align = FFMAX(linesize_align[0], linesize_align[3]);
00217 linesize_align[1] <<= chroma_shift;
00218 linesize_align[2] <<= chroma_shift;
00219 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
00220 *width=FFALIGN(*width, align);
00221 }
00222
00223 #if LIBAVCODEC_VERSION_MAJOR < 53
00224 int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
00225 return av_image_check_size(w, h, 0, av_log_ctx);
00226 }
00227 #endif
00228
00229 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
00230 int i;
00231 int w= s->width;
00232 int h= s->height;
00233 InternalBuffer *buf;
00234 int *picture_number;
00235
00236 if(pic->data[0]!=NULL) {
00237 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
00238 return -1;
00239 }
00240 if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
00241 av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
00242 return -1;
00243 }
00244
00245 if(av_image_check_size(w, h, 0, s))
00246 return -1;
00247
00248 if(s->internal_buffer==NULL){
00249 s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
00250 }
00251 #if 0
00252 s->internal_buffer= av_fast_realloc(
00253 s->internal_buffer,
00254 &s->internal_buffer_size,
00255 sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)
00256 );
00257 #endif
00258
00259 buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
00260 picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num;
00261 (*picture_number)++;
00262
00263 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
00264 for(i=0; i<4; i++){
00265 av_freep(&buf->base[i]);
00266 buf->data[i]= NULL;
00267 }
00268 }
00269
00270 if(buf->base[0]){
00271 pic->age= *picture_number - buf->last_pic_num;
00272 buf->last_pic_num= *picture_number;
00273 }else{
00274 int h_chroma_shift, v_chroma_shift;
00275 int size[4] = {0};
00276 int tmpsize;
00277 int unaligned;
00278 AVPicture picture;
00279 int stride_align[4];
00280
00281 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
00282
00283 avcodec_align_dimensions2(s, &w, &h, stride_align);
00284
00285 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
00286 w+= EDGE_WIDTH*2;
00287 h+= EDGE_WIDTH*2;
00288 }
00289
00290 do {
00291
00292
00293 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
00294
00295 w += w & ~(w-1);
00296
00297 unaligned = 0;
00298 for (i=0; i<4; i++){
00299 unaligned |= picture.linesize[i] % stride_align[i];
00300 }
00301 } while (unaligned);
00302
00303 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
00304 if (tmpsize < 0)
00305 return -1;
00306
00307 for (i=0; i<3 && picture.data[i+1]; i++)
00308 size[i] = picture.data[i+1] - picture.data[i];
00309 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
00310
00311 buf->last_pic_num= -256*256*256*64;
00312 memset(buf->base, 0, sizeof(buf->base));
00313 memset(buf->data, 0, sizeof(buf->data));
00314
00315 for(i=0; i<4 && size[i]; i++){
00316 const int h_shift= i==0 ? 0 : h_chroma_shift;
00317 const int v_shift= i==0 ? 0 : v_chroma_shift;
00318
00319 buf->linesize[i]= picture.linesize[i];
00320
00321 buf->base[i]= av_malloc(size[i]+16);
00322 if(buf->base[i]==NULL) return -1;
00323 memset(buf->base[i], 128, size[i]);
00324
00325
00326 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
00327 buf->data[i] = buf->base[i];
00328 else
00329 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
00330 }
00331 if(size[1] && !size[2])
00332 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
00333 buf->width = s->width;
00334 buf->height = s->height;
00335 buf->pix_fmt= s->pix_fmt;
00336 pic->age= 256*256*256*64;
00337 }
00338 pic->type= FF_BUFFER_TYPE_INTERNAL;
00339
00340 for(i=0; i<4; i++){
00341 pic->base[i]= buf->base[i];
00342 pic->data[i]= buf->data[i];
00343 pic->linesize[i]= buf->linesize[i];
00344 }
00345 s->internal_buffer_count++;
00346
00347 if(s->pkt) pic->pkt_pts= s->pkt->pts;
00348 else pic->pkt_pts= AV_NOPTS_VALUE;
00349 pic->reordered_opaque= s->reordered_opaque;
00350
00351 if(s->debug&FF_DEBUG_BUFFERS)
00352 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
00353
00354 return 0;
00355 }
00356
00357 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
00358 int i;
00359 InternalBuffer *buf, *last;
00360
00361 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
00362 assert(s->internal_buffer_count);
00363
00364 buf = NULL;
00365 for(i=0; i<s->internal_buffer_count; i++){
00366 buf= &((InternalBuffer*)s->internal_buffer)[i];
00367 if(buf->data[0] == pic->data[0])
00368 break;
00369 }
00370 assert(i < s->internal_buffer_count);
00371 s->internal_buffer_count--;
00372 last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
00373
00374 FFSWAP(InternalBuffer, *buf, *last);
00375
00376 for(i=0; i<4; i++){
00377 pic->data[i]=NULL;
00378
00379 }
00380
00381
00382 if(s->debug&FF_DEBUG_BUFFERS)
00383 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
00384 }
00385
00386 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
00387 AVFrame temp_pic;
00388 int i;
00389
00390
00391 if(pic->data[0] == NULL) {
00392
00393 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
00394 return s->get_buffer(s, pic);
00395 }
00396
00397
00398 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
00399 pic->reordered_opaque= s->reordered_opaque;
00400 return 0;
00401 }
00402
00403
00404
00405
00406 temp_pic = *pic;
00407 for(i = 0; i < 4; i++)
00408 pic->data[i] = pic->base[i] = NULL;
00409 pic->opaque = NULL;
00410
00411 if (s->get_buffer(s, pic))
00412 return -1;
00413
00414 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
00415 s->height);
00416 s->release_buffer(s, &temp_pic);
00417 return 0;
00418 }
00419
00420 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
00421 int i;
00422
00423 for(i=0; i<count; i++){
00424 int r= func(c, (char*)arg + i*size);
00425 if(ret) ret[i]= r;
00426 }
00427 return 0;
00428 }
00429
00430 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
00431 int i;
00432
00433 for(i=0; i<count; i++){
00434 int r= func(c, arg, i, 0);
00435 if(ret) ret[i]= r;
00436 }
00437 return 0;
00438 }
00439
00440 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
00441 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
00442 ++fmt;
00443 return fmt[0];
00444 }
00445
00446 void avcodec_get_frame_defaults(AVFrame *pic){
00447 memset(pic, 0, sizeof(AVFrame));
00448
00449 pic->pts= AV_NOPTS_VALUE;
00450 pic->key_frame= 1;
00451 }
00452
00453 AVFrame *avcodec_alloc_frame(void){
00454 AVFrame *pic= av_malloc(sizeof(AVFrame));
00455
00456 if(pic==NULL) return NULL;
00457
00458 avcodec_get_frame_defaults(pic);
00459
00460 return pic;
00461 }
00462
00463 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
00464 {
00465 int ret= -1;
00466
00467
00468 if (ff_lockmgr_cb) {
00469 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
00470 return -1;
00471 }
00472
00473 entangled_thread_counter++;
00474 if(entangled_thread_counter != 1){
00475 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
00476 goto end;
00477 }
00478
00479 if(avctx->codec || !codec)
00480 goto end;
00481
00482 if (codec->priv_data_size > 0) {
00483 if(!avctx->priv_data){
00484 avctx->priv_data = av_mallocz(codec->priv_data_size);
00485 if (!avctx->priv_data) {
00486 ret = AVERROR(ENOMEM);
00487 goto end;
00488 }
00489 if(codec->priv_class){
00490 *(AVClass**)avctx->priv_data= codec->priv_class;
00491 av_opt_set_defaults(avctx->priv_data);
00492 }
00493 }
00494 } else {
00495 avctx->priv_data = NULL;
00496 }
00497
00498 if(avctx->coded_width && avctx->coded_height)
00499 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
00500 else if(avctx->width && avctx->height)
00501 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
00502
00503 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
00504 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
00505 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
00506 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
00507 avcodec_set_dimensions(avctx, 0, 0);
00508 }
00509
00510
00511
00512 if (codec->decode)
00513 av_freep(&avctx->subtitle_header);
00514
00515 #define SANE_NB_CHANNELS 128U
00516 if (avctx->channels > SANE_NB_CHANNELS) {
00517 ret = AVERROR(EINVAL);
00518 goto free_and_end;
00519 }
00520
00521 avctx->codec = codec;
00522 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
00523 avctx->codec_id == CODEC_ID_NONE) {
00524 avctx->codec_type = codec->type;
00525 avctx->codec_id = codec->id;
00526 }
00527 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
00528 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
00529 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
00530 goto free_and_end;
00531 }
00532 avctx->frame_number = 0;
00533 if (avctx->codec->max_lowres < avctx->lowres) {
00534 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
00535 avctx->codec->max_lowres);
00536 goto free_and_end;
00537 }
00538
00539 if(avctx->codec->init){
00540 ret = avctx->codec->init(avctx);
00541 if (ret < 0) {
00542 goto free_and_end;
00543 }
00544 }
00545 ret=0;
00546 end:
00547 entangled_thread_counter--;
00548
00549
00550 if (ff_lockmgr_cb) {
00551 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
00552 }
00553 return ret;
00554 free_and_end:
00555 av_freep(&avctx->priv_data);
00556 avctx->codec= NULL;
00557 goto end;
00558 }
00559
00560 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
00561 const short *samples)
00562 {
00563 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
00564 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
00565 return -1;
00566 }
00567 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
00568 int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
00569 avctx->frame_number++;
00570 return ret;
00571 }else
00572 return 0;
00573 }
00574
00575 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
00576 const AVFrame *pict)
00577 {
00578 if(buf_size < FF_MIN_BUFFER_SIZE){
00579 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
00580 return -1;
00581 }
00582 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
00583 return -1;
00584 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
00585 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
00586 avctx->frame_number++;
00587 emms_c();
00588
00589 return ret;
00590 }else
00591 return 0;
00592 }
00593
00594 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
00595 const AVSubtitle *sub)
00596 {
00597 int ret;
00598 if(sub->start_display_time) {
00599 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
00600 return -1;
00601 }
00602 if(sub->num_rects == 0 || !sub->rects)
00603 return -1;
00604 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
00605 avctx->frame_number++;
00606 return ret;
00607 }
00608
00609 #if FF_API_VIDEO_OLD
00610 int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
00611 int *got_picture_ptr,
00612 const uint8_t *buf, int buf_size)
00613 {
00614 AVPacket avpkt;
00615 av_init_packet(&avpkt);
00616 avpkt.data = buf;
00617 avpkt.size = buf_size;
00618
00619 avpkt.flags = AV_PKT_FLAG_KEY;
00620
00621 return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
00622 }
00623 #endif
00624
00625 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
00626 int *got_picture_ptr,
00627 AVPacket *avpkt)
00628 {
00629 int ret;
00630
00631 *got_picture_ptr= 0;
00632 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
00633 return -1;
00634
00635 avctx->pkt = avpkt;
00636
00637 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
00638 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
00639 avpkt);
00640
00641 emms_c();
00642
00643 picture->pkt_dts= avpkt->dts;
00644
00645 if (*got_picture_ptr)
00646 avctx->frame_number++;
00647 }else
00648 ret= 0;
00649
00650 return ret;
00651 }
00652
00653 #if FF_API_AUDIO_OLD
00654 int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
00655 int *frame_size_ptr,
00656 const uint8_t *buf, int buf_size)
00657 {
00658 AVPacket avpkt;
00659 av_init_packet(&avpkt);
00660 avpkt.data = buf;
00661 avpkt.size = buf_size;
00662
00663 return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
00664 }
00665 #endif
00666
00667 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
00668 int *frame_size_ptr,
00669 AVPacket *avpkt)
00670 {
00671 int ret;
00672
00673 avctx->pkt = avpkt;
00674
00675 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
00676
00677 if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
00678 av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
00679 return -1;
00680 }
00681 if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
00682 *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
00683 av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
00684 return -1;
00685 }
00686
00687 ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
00688 avctx->frame_number++;
00689 }else{
00690 ret= 0;
00691 *frame_size_ptr=0;
00692 }
00693 return ret;
00694 }
00695
00696 #if FF_API_SUBTITLE_OLD
00697 int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
00698 int *got_sub_ptr,
00699 const uint8_t *buf, int buf_size)
00700 {
00701 AVPacket avpkt;
00702 av_init_packet(&avpkt);
00703 avpkt.data = buf;
00704 avpkt.size = buf_size;
00705
00706 return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
00707 }
00708 #endif
00709
00710 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
00711 int *got_sub_ptr,
00712 AVPacket *avpkt)
00713 {
00714 int ret;
00715
00716 avctx->pkt = avpkt;
00717 *got_sub_ptr = 0;
00718 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
00719 if (*got_sub_ptr)
00720 avctx->frame_number++;
00721 return ret;
00722 }
00723
00724 void avsubtitle_free(AVSubtitle *sub)
00725 {
00726 int i;
00727
00728 for (i = 0; i < sub->num_rects; i++)
00729 {
00730 av_freep(&sub->rects[i]->pict.data[0]);
00731 av_freep(&sub->rects[i]->pict.data[1]);
00732 av_freep(&sub->rects[i]->pict.data[2]);
00733 av_freep(&sub->rects[i]->pict.data[3]);
00734 av_freep(&sub->rects[i]->text);
00735 av_freep(&sub->rects[i]->ass);
00736 av_freep(&sub->rects[i]);
00737 }
00738
00739 av_freep(&sub->rects);
00740
00741 memset(sub, 0, sizeof(AVSubtitle));
00742 }
00743
00744 av_cold int avcodec_close(AVCodecContext *avctx)
00745 {
00746
00747 if (ff_lockmgr_cb) {
00748 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
00749 return -1;
00750 }
00751
00752 entangled_thread_counter++;
00753 if(entangled_thread_counter != 1){
00754 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
00755 entangled_thread_counter--;
00756 return -1;
00757 }
00758
00759 if (HAVE_THREADS && avctx->thread_opaque)
00760 avcodec_thread_free(avctx);
00761 if (avctx->codec && avctx->codec->close)
00762 avctx->codec->close(avctx);
00763 avcodec_default_free_buffers(avctx);
00764 avctx->coded_frame = NULL;
00765 av_freep(&avctx->priv_data);
00766 if(avctx->codec && avctx->codec->encode)
00767 av_freep(&avctx->extradata);
00768 avctx->codec = NULL;
00769 entangled_thread_counter--;
00770
00771
00772 if (ff_lockmgr_cb) {
00773 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
00774 }
00775 return 0;
00776 }
00777
00778 AVCodec *avcodec_find_encoder(enum CodecID id)
00779 {
00780 AVCodec *p, *experimental=NULL;
00781 p = first_avcodec;
00782 while (p) {
00783 if (p->encode != NULL && p->id == id) {
00784 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
00785 experimental = p;
00786 } else
00787 return p;
00788 }
00789 p = p->next;
00790 }
00791 return experimental;
00792 }
00793
00794 AVCodec *avcodec_find_encoder_by_name(const char *name)
00795 {
00796 AVCodec *p;
00797 if (!name)
00798 return NULL;
00799 p = first_avcodec;
00800 while (p) {
00801 if (p->encode != NULL && strcmp(name,p->name) == 0)
00802 return p;
00803 p = p->next;
00804 }
00805 return NULL;
00806 }
00807
00808 AVCodec *avcodec_find_decoder(enum CodecID id)
00809 {
00810 AVCodec *p;
00811 p = first_avcodec;
00812 while (p) {
00813 if (p->decode != NULL && p->id == id)
00814 return p;
00815 p = p->next;
00816 }
00817 return NULL;
00818 }
00819
00820 AVCodec *avcodec_find_decoder_by_name(const char *name)
00821 {
00822 AVCodec *p;
00823 if (!name)
00824 return NULL;
00825 p = first_avcodec;
00826 while (p) {
00827 if (p->decode != NULL && strcmp(name,p->name) == 0)
00828 return p;
00829 p = p->next;
00830 }
00831 return NULL;
00832 }
00833
00834 static int get_bit_rate(AVCodecContext *ctx)
00835 {
00836 int bit_rate;
00837 int bits_per_sample;
00838
00839 switch(ctx->codec_type) {
00840 case AVMEDIA_TYPE_VIDEO:
00841 case AVMEDIA_TYPE_DATA:
00842 case AVMEDIA_TYPE_SUBTITLE:
00843 case AVMEDIA_TYPE_ATTACHMENT:
00844 bit_rate = ctx->bit_rate;
00845 break;
00846 case AVMEDIA_TYPE_AUDIO:
00847 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
00848 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
00849 break;
00850 default:
00851 bit_rate = 0;
00852 break;
00853 }
00854 return bit_rate;
00855 }
00856
00857 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
00858 {
00859 int i, len, ret = 0;
00860
00861 for (i = 0; i < 4; i++) {
00862 len = snprintf(buf, buf_size,
00863 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
00864 buf += len;
00865 buf_size = buf_size > len ? buf_size - len : 0;
00866 ret += len;
00867 codec_tag>>=8;
00868 }
00869 return ret;
00870 }
00871
00872 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
00873 {
00874 const char *codec_name;
00875 const char *profile = NULL;
00876 AVCodec *p;
00877 char buf1[32];
00878 int bitrate;
00879 AVRational display_aspect_ratio;
00880
00881 if (encode)
00882 p = avcodec_find_encoder(enc->codec_id);
00883 else
00884 p = avcodec_find_decoder(enc->codec_id);
00885
00886 if (p) {
00887 codec_name = p->name;
00888 profile = av_get_profile_name(p, enc->profile);
00889 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
00890
00891
00892 codec_name = "mpeg2ts";
00893 } else if (enc->codec_name[0] != '\0') {
00894 codec_name = enc->codec_name;
00895 } else {
00896
00897 char tag_buf[32];
00898 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
00899 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
00900 codec_name = buf1;
00901 }
00902
00903 switch(enc->codec_type) {
00904 case AVMEDIA_TYPE_VIDEO:
00905 snprintf(buf, buf_size,
00906 "Video: %s%s",
00907 codec_name, enc->mb_decision ? " (hq)" : "");
00908 if (profile)
00909 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00910 " (%s)", profile);
00911 if (enc->pix_fmt != PIX_FMT_NONE) {
00912 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00913 ", %s",
00914 avcodec_get_pix_fmt_name(enc->pix_fmt));
00915 }
00916 if (enc->width) {
00917 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00918 ", %dx%d",
00919 enc->width, enc->height);
00920 if (enc->sample_aspect_ratio.num) {
00921 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
00922 enc->width*enc->sample_aspect_ratio.num,
00923 enc->height*enc->sample_aspect_ratio.den,
00924 1024*1024);
00925 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00926 " [PAR %d:%d DAR %d:%d]",
00927 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
00928 display_aspect_ratio.num, display_aspect_ratio.den);
00929 }
00930 if(av_log_get_level() >= AV_LOG_DEBUG){
00931 int g= av_gcd(enc->time_base.num, enc->time_base.den);
00932 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00933 ", %d/%d",
00934 enc->time_base.num/g, enc->time_base.den/g);
00935 }
00936 }
00937 if (encode) {
00938 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00939 ", q=%d-%d", enc->qmin, enc->qmax);
00940 }
00941 break;
00942 case AVMEDIA_TYPE_AUDIO:
00943 snprintf(buf, buf_size,
00944 "Audio: %s",
00945 codec_name);
00946 if (profile)
00947 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00948 " (%s)", profile);
00949 if (enc->sample_rate) {
00950 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00951 ", %d Hz", enc->sample_rate);
00952 }
00953 av_strlcat(buf, ", ", buf_size);
00954 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
00955 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
00956 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00957 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
00958 }
00959 break;
00960 case AVMEDIA_TYPE_DATA:
00961 snprintf(buf, buf_size, "Data: %s", codec_name);
00962 break;
00963 case AVMEDIA_TYPE_SUBTITLE:
00964 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
00965 break;
00966 case AVMEDIA_TYPE_ATTACHMENT:
00967 snprintf(buf, buf_size, "Attachment: %s", codec_name);
00968 break;
00969 default:
00970 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
00971 return;
00972 }
00973 if (encode) {
00974 if (enc->flags & CODEC_FLAG_PASS1)
00975 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00976 ", pass 1");
00977 if (enc->flags & CODEC_FLAG_PASS2)
00978 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00979 ", pass 2");
00980 }
00981 bitrate = get_bit_rate(enc);
00982 if (bitrate != 0) {
00983 snprintf(buf + strlen(buf), buf_size - strlen(buf),
00984 ", %d kb/s", bitrate / 1000);
00985 }
00986 }
00987
00988 const char *av_get_profile_name(const AVCodec *codec, int profile)
00989 {
00990 const AVProfile *p;
00991 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
00992 return NULL;
00993
00994 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
00995 if (p->profile == profile)
00996 return p->name;
00997
00998 return NULL;
00999 }
01000
01001 unsigned avcodec_version( void )
01002 {
01003 return LIBAVCODEC_VERSION_INT;
01004 }
01005
01006 const char *avcodec_configuration(void)
01007 {
01008 return FFMPEG_CONFIGURATION;
01009 }
01010
01011 const char *avcodec_license(void)
01012 {
01013 #define LICENSE_PREFIX "libavcodec license: "
01014 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
01015 }
01016
01017 void avcodec_init(void)
01018 {
01019 static int initialized = 0;
01020
01021 if (initialized != 0)
01022 return;
01023 initialized = 1;
01024
01025 dsputil_static_init();
01026 }
01027
01028 void avcodec_flush_buffers(AVCodecContext *avctx)
01029 {
01030 if(avctx->codec->flush)
01031 avctx->codec->flush(avctx);
01032 }
01033
01034 void avcodec_default_free_buffers(AVCodecContext *s){
01035 int i, j;
01036
01037 if(s->internal_buffer==NULL) return;
01038
01039 if (s->internal_buffer_count)
01040 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
01041 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
01042 InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
01043 for(j=0; j<4; j++){
01044 av_freep(&buf->base[j]);
01045 buf->data[j]= NULL;
01046 }
01047 }
01048 av_freep(&s->internal_buffer);
01049
01050 s->internal_buffer_count=0;
01051 }
01052
01053 char av_get_pict_type_char(int pict_type){
01054 switch(pict_type){
01055 case FF_I_TYPE: return 'I';
01056 case FF_P_TYPE: return 'P';
01057 case FF_B_TYPE: return 'B';
01058 case FF_S_TYPE: return 'S';
01059 case FF_SI_TYPE:return 'i';
01060 case FF_SP_TYPE:return 'p';
01061 case FF_BI_TYPE:return 'b';
01062 default: return '?';
01063 }
01064 }
01065
01066 int av_get_bits_per_sample(enum CodecID codec_id){
01067 switch(codec_id){
01068 case CODEC_ID_ADPCM_SBPRO_2:
01069 return 2;
01070 case CODEC_ID_ADPCM_SBPRO_3:
01071 return 3;
01072 case CODEC_ID_ADPCM_SBPRO_4:
01073 case CODEC_ID_ADPCM_CT:
01074 case CODEC_ID_ADPCM_IMA_WAV:
01075 case CODEC_ID_ADPCM_MS:
01076 case CODEC_ID_ADPCM_YAMAHA:
01077 return 4;
01078 case CODEC_ID_PCM_ALAW:
01079 case CODEC_ID_PCM_MULAW:
01080 case CODEC_ID_PCM_S8:
01081 case CODEC_ID_PCM_U8:
01082 case CODEC_ID_PCM_ZORK:
01083 return 8;
01084 case CODEC_ID_PCM_S16BE:
01085 case CODEC_ID_PCM_S16LE:
01086 case CODEC_ID_PCM_S16LE_PLANAR:
01087 case CODEC_ID_PCM_U16BE:
01088 case CODEC_ID_PCM_U16LE:
01089 return 16;
01090 case CODEC_ID_PCM_S24DAUD:
01091 case CODEC_ID_PCM_S24BE:
01092 case CODEC_ID_PCM_S24LE:
01093 case CODEC_ID_PCM_U24BE:
01094 case CODEC_ID_PCM_U24LE:
01095 return 24;
01096 case CODEC_ID_PCM_S32BE:
01097 case CODEC_ID_PCM_S32LE:
01098 case CODEC_ID_PCM_U32BE:
01099 case CODEC_ID_PCM_U32LE:
01100 case CODEC_ID_PCM_F32BE:
01101 case CODEC_ID_PCM_F32LE:
01102 return 32;
01103 case CODEC_ID_PCM_F64BE:
01104 case CODEC_ID_PCM_F64LE:
01105 return 64;
01106 default:
01107 return 0;
01108 }
01109 }
01110
01111 #if FF_API_OLD_SAMPLE_FMT
01112 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
01113 return av_get_bits_per_sample_fmt(sample_fmt);
01114 }
01115 #endif
01116
01117 #if !HAVE_THREADS
01118 int avcodec_thread_init(AVCodecContext *s, int thread_count){
01119 s->thread_count = thread_count;
01120 return -1;
01121 }
01122 #endif
01123
01124 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
01125 {
01126 unsigned int n = 0;
01127
01128 while(v >= 0xff) {
01129 *s++ = 0xff;
01130 v -= 0xff;
01131 n++;
01132 }
01133 *s = v;
01134 n++;
01135 return n;
01136 }
01137
01138 #if LIBAVCODEC_VERSION_MAJOR < 53
01139 #include "libavcore/parseutils.h"
01140
01141 int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
01142 {
01143 return av_parse_video_size(width_ptr, height_ptr, str);
01144 }
01145
01146 int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
01147 {
01148 return av_parse_video_rate(frame_rate, arg);
01149 }
01150 #endif
01151
01152 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
01153 int i;
01154 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
01155 return i;
01156 }
01157
01158 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
01159 {
01160 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
01161 "version to the newest one from SVN. If the problem still "
01162 "occurs, it means that your file has a feature which has not "
01163 "been implemented.", feature);
01164 if(want_sample)
01165 av_log_ask_for_sample(avc, NULL);
01166 else
01167 av_log(avc, AV_LOG_WARNING, "\n");
01168 }
01169
01170 void av_log_ask_for_sample(void *avc, const char *msg)
01171 {
01172 if (msg)
01173 av_log(avc, AV_LOG_WARNING, "%s ", msg);
01174 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
01175 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
01176 "and contact the ffmpeg-devel mailing list.\n");
01177 }
01178
01179 static AVHWAccel *first_hwaccel = NULL;
01180
01181 void av_register_hwaccel(AVHWAccel *hwaccel)
01182 {
01183 AVHWAccel **p = &first_hwaccel;
01184 while (*p)
01185 p = &(*p)->next;
01186 *p = hwaccel;
01187 hwaccel->next = NULL;
01188 }
01189
01190 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
01191 {
01192 return hwaccel ? hwaccel->next : first_hwaccel;
01193 }
01194
01195 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
01196 {
01197 AVHWAccel *hwaccel=NULL;
01198
01199 while((hwaccel= av_hwaccel_next(hwaccel))){
01200 if ( hwaccel->id == codec_id
01201 && hwaccel->pix_fmt == pix_fmt)
01202 return hwaccel;
01203 }
01204 return NULL;
01205 }
01206
01207 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
01208 {
01209 if (ff_lockmgr_cb) {
01210 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
01211 return -1;
01212 }
01213
01214 ff_lockmgr_cb = cb;
01215
01216 if (ff_lockmgr_cb) {
01217 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
01218 return -1;
01219 }
01220 return 0;
01221 }
01222
01223 unsigned int ff_toupper4(unsigned int x)
01224 {
01225 return toupper( x &0xFF)
01226 + (toupper((x>>8 )&0xFF)<<8 )
01227 + (toupper((x>>16)&0xFF)<<16)
01228 + (toupper((x>>24)&0xFF)<<24);
01229 }