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