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/crc.h"
00030 #include "libavutil/mathematics.h"
00031 #include "libavutil/pixdesc.h"
00032 #include "libavutil/audioconvert.h"
00033 #include "libavutil/imgutils.h"
00034 #include "libavutil/samplefmt.h"
00035 #include "libavutil/dict.h"
00036 #include "libavutil/avassert.h"
00037 #include "avcodec.h"
00038 #include "dsputil.h"
00039 #include "libavutil/opt.h"
00040 #include "imgconvert.h"
00041 #include "thread.h"
00042 #include "audioconvert.h"
00043 #include "internal.h"
00044 #include <stdlib.h>
00045 #include <stdarg.h>
00046 #include <limits.h>
00047 #include <float.h>
00048
00049 static int volatile entangled_thread_counter=0;
00050 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
00051 static void *codec_mutex;
00052 static void *avformat_mutex;
00053
00054 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
00055 {
00056 if(min_size < *size)
00057 return ptr;
00058
00059 min_size= FFMAX(17*min_size/16 + 32, min_size);
00060
00061 ptr= av_realloc(ptr, min_size);
00062 if(!ptr)
00063 min_size= 0;
00064
00065 *size= min_size;
00066
00067 return ptr;
00068 }
00069
00070 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
00071 {
00072 void **p = ptr;
00073 if (min_size < *size)
00074 return;
00075 min_size= FFMAX(17*min_size/16 + 32, min_size);
00076 av_free(*p);
00077 *p = av_malloc(min_size);
00078 if (!*p) min_size = 0;
00079 *size= min_size;
00080 }
00081
00082
00083 static AVCodec *first_avcodec = NULL;
00084
00085 AVCodec *av_codec_next(AVCodec *c){
00086 if(c) return c->next;
00087 else return first_avcodec;
00088 }
00089
00090 #if !FF_API_AVCODEC_INIT
00091 static
00092 #endif
00093 void avcodec_init(void)
00094 {
00095 static int initialized = 0;
00096
00097 if (initialized != 0)
00098 return;
00099 initialized = 1;
00100
00101 dsputil_static_init();
00102 }
00103
00104 void avcodec_register(AVCodec *codec)
00105 {
00106 AVCodec **p;
00107 avcodec_init();
00108 p = &first_avcodec;
00109 while (*p != NULL) p = &(*p)->next;
00110 *p = codec;
00111 codec->next = NULL;
00112
00113 if (codec->init_static_data)
00114 codec->init_static_data(codec);
00115 }
00116
00117 unsigned avcodec_get_edge_width(void)
00118 {
00119 return EDGE_WIDTH;
00120 }
00121
00122 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
00123 s->coded_width = width;
00124 s->coded_height= height;
00125 s->width = -((-width )>>s->lowres);
00126 s->height= -((-height)>>s->lowres);
00127 }
00128
00129 #define INTERNAL_BUFFER_SIZE (32+1)
00130
00131 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
00132 int linesize_align[AV_NUM_DATA_POINTERS])
00133 {
00134 int i;
00135 int w_align= 1;
00136 int h_align= 1;
00137
00138 switch(s->pix_fmt){
00139 case PIX_FMT_YUV420P:
00140 case PIX_FMT_YUYV422:
00141 case PIX_FMT_UYVY422:
00142 case PIX_FMT_YUV422P:
00143 case PIX_FMT_YUV440P:
00144 case PIX_FMT_YUV444P:
00145 case PIX_FMT_GBRP:
00146 case PIX_FMT_GRAY8:
00147 case PIX_FMT_GRAY16BE:
00148 case PIX_FMT_GRAY16LE:
00149 case PIX_FMT_YUVJ420P:
00150 case PIX_FMT_YUVJ422P:
00151 case PIX_FMT_YUVJ440P:
00152 case PIX_FMT_YUVJ444P:
00153 case PIX_FMT_YUVA420P:
00154 case PIX_FMT_YUV420P9LE:
00155 case PIX_FMT_YUV420P9BE:
00156 case PIX_FMT_YUV420P10LE:
00157 case PIX_FMT_YUV420P10BE:
00158 case PIX_FMT_YUV422P9LE:
00159 case PIX_FMT_YUV422P9BE:
00160 case PIX_FMT_YUV422P10LE:
00161 case PIX_FMT_YUV422P10BE:
00162 case PIX_FMT_YUV444P9LE:
00163 case PIX_FMT_YUV444P9BE:
00164 case PIX_FMT_YUV444P10LE:
00165 case PIX_FMT_YUV444P10BE:
00166 case PIX_FMT_GBRP9LE:
00167 case PIX_FMT_GBRP9BE:
00168 case PIX_FMT_GBRP10LE:
00169 case PIX_FMT_GBRP10BE:
00170 w_align = 16;
00171 h_align = 16 * 2;
00172 break;
00173 case PIX_FMT_YUV411P:
00174 case PIX_FMT_UYYVYY411:
00175 w_align=32;
00176 h_align=8;
00177 break;
00178 case PIX_FMT_YUV410P:
00179 if(s->codec_id == CODEC_ID_SVQ1){
00180 w_align=64;
00181 h_align=64;
00182 }
00183 case PIX_FMT_RGB555:
00184 if(s->codec_id == CODEC_ID_RPZA){
00185 w_align=4;
00186 h_align=4;
00187 }
00188 case PIX_FMT_PAL8:
00189 case PIX_FMT_BGR8:
00190 case PIX_FMT_RGB8:
00191 if(s->codec_id == CODEC_ID_SMC){
00192 w_align=4;
00193 h_align=4;
00194 }
00195 break;
00196 case PIX_FMT_BGR24:
00197 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
00198 w_align=4;
00199 h_align=4;
00200 }
00201 break;
00202 default:
00203 w_align= 1;
00204 h_align= 1;
00205 break;
00206 }
00207
00208 if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
00209 w_align= FFMAX(w_align, 8);
00210 }
00211
00212 *width = FFALIGN(*width , w_align);
00213 *height= FFALIGN(*height, h_align);
00214 if(s->codec_id == CODEC_ID_H264 || s->lowres)
00215 *height+=2;
00216
00217
00218 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
00219 linesize_align[i] = STRIDE_ALIGN;
00220
00221
00222
00223
00224 #if HAVE_MMX
00225 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
00226 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
00227 s->codec_id == CODEC_ID_VP6A || s->codec_id == CODEC_ID_DIRAC) {
00228 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
00229 linesize_align[i] = 16;
00230 }
00231 #endif
00232 }
00233
00234 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
00235 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
00236 int linesize_align[AV_NUM_DATA_POINTERS];
00237 int align;
00238 avcodec_align_dimensions2(s, width, height, linesize_align);
00239 align = FFMAX(linesize_align[0], linesize_align[3]);
00240 linesize_align[1] <<= chroma_shift;
00241 linesize_align[2] <<= chroma_shift;
00242 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
00243 *width=FFALIGN(*width, align);
00244 }
00245
00246 void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
00247 {
00248 if (s->pkt) {
00249 pic->pkt_pts = s->pkt->pts;
00250 pic->pkt_pos = s->pkt->pos;
00251 } else {
00252 pic->pkt_pts = AV_NOPTS_VALUE;
00253 pic->pkt_pos = -1;
00254 }
00255 pic->reordered_opaque= s->reordered_opaque;
00256 pic->sample_aspect_ratio = s->sample_aspect_ratio;
00257 pic->width = s->width;
00258 pic->height = s->height;
00259 pic->format = s->pix_fmt;
00260 }
00261
00262 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
00263 {
00264 AVCodecInternal *avci = avctx->internal;
00265 InternalBuffer *buf;
00266 int buf_size, ret, i, needs_extended_data;
00267
00268 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
00269 frame->nb_samples, avctx->sample_fmt,
00270 32);
00271 if (buf_size < 0)
00272 return AVERROR(EINVAL);
00273
00274 needs_extended_data = av_sample_fmt_is_planar(avctx->sample_fmt) &&
00275 avctx->channels > AV_NUM_DATA_POINTERS;
00276
00277
00278 if (!avci->buffer) {
00279 avci->buffer = av_mallocz(sizeof(InternalBuffer));
00280 if (!avci->buffer)
00281 return AVERROR(ENOMEM);
00282 }
00283 buf = avci->buffer;
00284
00285
00286
00287 if (buf->extended_data) {
00288
00289 if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
00290 av_free(buf->extended_data[0]);
00291 if (buf->extended_data != buf->data)
00292 av_free(&buf->extended_data);
00293 buf->extended_data = NULL;
00294 buf->data[0] = NULL;
00295 }
00296
00297
00298 if (buf->nb_channels != avctx->channels) {
00299 if (buf->extended_data != buf->data)
00300 av_free(buf->extended_data);
00301 buf->extended_data = NULL;
00302 }
00303 }
00304
00305
00306
00307 if (!buf->extended_data) {
00308
00309
00310 if (needs_extended_data) {
00311 buf->extended_data = av_mallocz(avctx->channels *
00312 sizeof(*buf->extended_data));
00313 if (!buf->extended_data)
00314 return AVERROR(ENOMEM);
00315 } else {
00316 buf->extended_data = buf->data;
00317 }
00318
00319
00320
00321
00322 if (buf->extended_data[0]) {
00323 ret = av_samples_fill_arrays(buf->extended_data, &buf->linesize[0],
00324 buf->extended_data[0], avctx->channels,
00325 frame->nb_samples, avctx->sample_fmt,
00326 32);
00327 } else {
00328 ret = av_samples_alloc(buf->extended_data, &buf->linesize[0],
00329 avctx->channels, frame->nb_samples,
00330 avctx->sample_fmt, 32);
00331 }
00332 if (ret)
00333 return ret;
00334
00335
00336
00337 if (needs_extended_data) {
00338 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
00339 buf->data[i] = buf->extended_data[i];
00340 }
00341 buf->audio_data_size = buf_size;
00342 buf->nb_channels = avctx->channels;
00343 }
00344
00345
00346 frame->type = FF_BUFFER_TYPE_INTERNAL;
00347 frame->extended_data = buf->extended_data;
00348 frame->linesize[0] = buf->linesize[0];
00349 memcpy(frame->data, buf->data, sizeof(frame->data));
00350
00351 if (avctx->pkt) {
00352 frame->pkt_pts = avctx->pkt->pts;
00353 frame->pkt_pos = avctx->pkt->pos;
00354 } else {
00355 frame->pkt_pts = AV_NOPTS_VALUE;
00356 frame->pkt_pos = -1;
00357 }
00358
00359 frame->reordered_opaque = avctx->reordered_opaque;
00360
00361 if (avctx->debug & FF_DEBUG_BUFFERS)
00362 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
00363 "internal audio buffer used\n", frame);
00364
00365 return 0;
00366 }
00367
00368 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
00369 {
00370 int i;
00371 int w= s->width;
00372 int h= s->height;
00373 InternalBuffer *buf;
00374 int *picture_number;
00375 AVCodecInternal *avci = s->internal;
00376
00377 if(pic->data[0]!=NULL) {
00378 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
00379 return -1;
00380 }
00381 if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
00382 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
00383 return -1;
00384 }
00385
00386 if(av_image_check_size(w, h, 0, s))
00387 return -1;
00388
00389 if (!avci->buffer) {
00390 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
00391 sizeof(InternalBuffer));
00392 }
00393
00394 buf = &avci->buffer[avci->buffer_count];
00395 picture_number = &(avci->buffer[INTERNAL_BUFFER_SIZE]).last_pic_num;
00396 (*picture_number)++;
00397
00398 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
00399 if(s->active_thread_type&FF_THREAD_FRAME) {
00400 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
00401 return -1;
00402 }
00403
00404 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
00405 av_freep(&buf->base[i]);
00406 buf->data[i]= NULL;
00407 }
00408 }
00409
00410 if(buf->base[0]){
00411 pic->age= *picture_number - buf->last_pic_num;
00412 buf->last_pic_num= *picture_number;
00413 }else{
00414 int h_chroma_shift, v_chroma_shift;
00415 int size[4] = {0};
00416 int tmpsize;
00417 int unaligned;
00418 AVPicture picture;
00419 int stride_align[AV_NUM_DATA_POINTERS];
00420 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
00421
00422 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
00423
00424 avcodec_align_dimensions2(s, &w, &h, stride_align);
00425
00426 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
00427 w+= EDGE_WIDTH*2;
00428 h+= EDGE_WIDTH*2;
00429 }
00430
00431 do {
00432
00433
00434 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
00435
00436 w += w & ~(w-1);
00437
00438 unaligned = 0;
00439 for (i=0; i<4; i++){
00440 unaligned |= picture.linesize[i] % stride_align[i];
00441 }
00442 } while (unaligned);
00443
00444 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
00445 if (tmpsize < 0)
00446 return -1;
00447
00448 for (i=0; i<3 && picture.data[i+1]; i++)
00449 size[i] = picture.data[i+1] - picture.data[i];
00450 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
00451
00452 buf->last_pic_num= -256*256*256*64;
00453 memset(buf->base, 0, sizeof(buf->base));
00454 memset(buf->data, 0, sizeof(buf->data));
00455
00456 for(i=0; i<4 && size[i]; i++){
00457 const int h_shift= i==0 ? 0 : h_chroma_shift;
00458 const int v_shift= i==0 ? 0 : v_chroma_shift;
00459
00460 buf->linesize[i]= picture.linesize[i];
00461
00462 buf->base[i]= av_malloc(size[i]+16);
00463 if(buf->base[i]==NULL) return -1;
00464 memset(buf->base[i], 128, size[i]);
00465
00466
00467 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
00468 buf->data[i] = buf->base[i];
00469 else
00470 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
00471 }
00472 for (; i < AV_NUM_DATA_POINTERS; i++) {
00473 buf->base[i] = buf->data[i] = NULL;
00474 buf->linesize[i] = 0;
00475 }
00476 if(size[1] && !size[2])
00477 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
00478 buf->width = s->width;
00479 buf->height = s->height;
00480 buf->pix_fmt= s->pix_fmt;
00481 pic->age= 256*256*256*64;
00482 }
00483 pic->type= FF_BUFFER_TYPE_INTERNAL;
00484
00485 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
00486 pic->base[i]= buf->base[i];
00487 pic->data[i]= buf->data[i];
00488 pic->linesize[i]= buf->linesize[i];
00489 }
00490 pic->extended_data = pic->data;
00491 avci->buffer_count++;
00492
00493 if (s->pkt) {
00494 pic->pkt_pts = s->pkt->pts;
00495 pic->pkt_pos = s->pkt->pos;
00496 } else {
00497 pic->pkt_pts = AV_NOPTS_VALUE;
00498 pic->pkt_pos = -1;
00499 }
00500 pic->reordered_opaque= s->reordered_opaque;
00501 pic->sample_aspect_ratio = s->sample_aspect_ratio;
00502 pic->width = s->width;
00503 pic->height = s->height;
00504 pic->format = s->pix_fmt;
00505
00506 if(s->debug&FF_DEBUG_BUFFERS)
00507 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
00508 "buffers used\n", pic, avci->buffer_count);
00509
00510 return 0;
00511 }
00512
00513 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
00514 {
00515 switch (avctx->codec_type) {
00516 case AVMEDIA_TYPE_VIDEO:
00517 return video_get_buffer(avctx, frame);
00518 case AVMEDIA_TYPE_AUDIO:
00519 return audio_get_buffer(avctx, frame);
00520 default:
00521 return -1;
00522 }
00523 }
00524
00525 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
00526 int i;
00527 InternalBuffer *buf, *last;
00528 AVCodecInternal *avci = s->internal;
00529
00530 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
00531
00532 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
00533 assert(avci->buffer_count);
00534
00535 if (avci->buffer) {
00536 buf = NULL;
00537 for (i = 0; i < avci->buffer_count; i++) {
00538 buf = &avci->buffer[i];
00539 if (buf->data[0] == pic->data[0])
00540 break;
00541 }
00542 assert(i < avci->buffer_count);
00543 avci->buffer_count--;
00544 last = &avci->buffer[avci->buffer_count];
00545
00546 FFSWAP(InternalBuffer, *buf, *last);
00547 }
00548
00549 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
00550 pic->data[i]=NULL;
00551
00552 }
00553
00554
00555 if(s->debug&FF_DEBUG_BUFFERS)
00556 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
00557 "buffers used\n", pic, avci->buffer_count);
00558 }
00559
00560 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
00561 AVFrame temp_pic;
00562 int i;
00563
00564 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
00565
00566
00567 if(pic->data[0] == NULL) {
00568
00569 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
00570 return s->get_buffer(s, pic);
00571 }
00572
00573
00574 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
00575 if(s->pkt) pic->pkt_pts= s->pkt->pts;
00576 else pic->pkt_pts= AV_NOPTS_VALUE;
00577 pic->reordered_opaque= s->reordered_opaque;
00578 return 0;
00579 }
00580
00581
00582
00583
00584 temp_pic = *pic;
00585 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
00586 pic->data[i] = pic->base[i] = NULL;
00587 pic->opaque = NULL;
00588
00589 if (s->get_buffer(s, pic))
00590 return -1;
00591
00592 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
00593 s->height);
00594 s->release_buffer(s, &temp_pic);
00595 return 0;
00596 }
00597
00598 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
00599 int i;
00600
00601 for(i=0; i<count; i++){
00602 int r= func(c, (char*)arg + i*size);
00603 if(ret) ret[i]= r;
00604 }
00605 return 0;
00606 }
00607
00608 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
00609 int i;
00610
00611 for(i=0; i<count; i++){
00612 int r= func(c, arg, i, 0);
00613 if(ret) ret[i]= r;
00614 }
00615 return 0;
00616 }
00617
00618 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
00619 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
00620 ++fmt;
00621 return fmt[0];
00622 }
00623
00624 void avcodec_get_frame_defaults(AVFrame *pic){
00625 memset(pic, 0, sizeof(AVFrame));
00626
00627 pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
00628 pic->pkt_pos = -1;
00629 pic->key_frame= 1;
00630 pic->sample_aspect_ratio = (AVRational){0, 1};
00631 pic->format = -1;
00632 }
00633
00634 AVFrame *avcodec_alloc_frame(void){
00635 AVFrame *pic= av_malloc(sizeof(AVFrame));
00636
00637 if(pic==NULL) return NULL;
00638
00639 avcodec_get_frame_defaults(pic);
00640
00641 return pic;
00642 }
00643
00644 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
00645 {
00646 memset(sub, 0, sizeof(*sub));
00647 sub->pts = AV_NOPTS_VALUE;
00648 }
00649
00650 #if FF_API_AVCODEC_OPEN
00651 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
00652 {
00653 return avcodec_open2(avctx, codec, NULL);
00654 }
00655 #endif
00656
00657 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
00658 {
00659 int ret = 0;
00660 AVDictionary *tmp = NULL;
00661
00662 if (options)
00663 av_dict_copy(&tmp, *options, 0);
00664
00665
00666 if (ff_lockmgr_cb) {
00667 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
00668 return -1;
00669 }
00670
00671 entangled_thread_counter++;
00672 if(entangled_thread_counter != 1){
00673 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
00674 ret = -1;
00675 goto end;
00676 }
00677
00678 if(avctx->codec || !codec) {
00679 ret = AVERROR(EINVAL);
00680 goto end;
00681 }
00682
00683 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
00684 if (!avctx->internal) {
00685 ret = AVERROR(ENOMEM);
00686 goto end;
00687 }
00688
00689 if (codec->priv_data_size > 0) {
00690 if(!avctx->priv_data){
00691 avctx->priv_data = av_mallocz(codec->priv_data_size);
00692 if (!avctx->priv_data) {
00693 ret = AVERROR(ENOMEM);
00694 goto end;
00695 }
00696 if (codec->priv_class) {
00697 *(AVClass**)avctx->priv_data= codec->priv_class;
00698 av_opt_set_defaults(avctx->priv_data);
00699 }
00700 }
00701 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
00702 goto free_and_end;
00703 } else {
00704 avctx->priv_data = NULL;
00705 }
00706 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
00707 goto free_and_end;
00708
00709
00710 if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
00711 if(avctx->coded_width && avctx->coded_height)
00712 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
00713 else if(avctx->width && avctx->height)
00714 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
00715 }
00716
00717 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
00718 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
00719 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
00720 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
00721 avcodec_set_dimensions(avctx, 0, 0);
00722 }
00723
00724
00725
00726 if (codec->decode)
00727 av_freep(&avctx->subtitle_header);
00728
00729 #define SANE_NB_CHANNELS 128U
00730 if (avctx->channels > SANE_NB_CHANNELS) {
00731 ret = AVERROR(EINVAL);
00732 goto free_and_end;
00733 }
00734
00735 avctx->codec = codec;
00736 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
00737 avctx->codec_id == CODEC_ID_NONE) {
00738 avctx->codec_type = codec->type;
00739 avctx->codec_id = codec->id;
00740 }
00741 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
00742 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
00743 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
00744 ret = AVERROR(EINVAL);
00745 goto free_and_end;
00746 }
00747 avctx->frame_number = 0;
00748 #if FF_API_ER
00749
00750 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
00751 avctx->error_recognition, avctx->err_recognition);
00752 switch(avctx->error_recognition){
00753 case FF_ER_EXPLODE : avctx->err_recognition |= AV_EF_EXPLODE | AV_EF_COMPLIANT | AV_EF_CAREFUL;
00754 break;
00755 case FF_ER_VERY_AGGRESSIVE:
00756 case FF_ER_AGGRESSIVE : avctx->err_recognition |= AV_EF_AGGRESSIVE;
00757 case FF_ER_COMPLIANT : avctx->err_recognition |= AV_EF_COMPLIANT;
00758 case FF_ER_CAREFUL : avctx->err_recognition |= AV_EF_CAREFUL;
00759 }
00760
00761 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
00762 avctx->error_recognition, avctx->err_recognition);
00763 #endif
00764
00765 if (!HAVE_THREADS)
00766 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
00767
00768 if (HAVE_THREADS && !avctx->thread_opaque) {
00769 ret = ff_thread_init(avctx);
00770 if (ret < 0) {
00771 goto free_and_end;
00772 }
00773 }
00774
00775 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
00776 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
00777 avctx->codec->max_lowres);
00778 ret = AVERROR(EINVAL);
00779 goto free_and_end;
00780 }
00781 if (avctx->codec->encode) {
00782 int i;
00783 if (avctx->codec->sample_fmts) {
00784 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
00785 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
00786 break;
00787 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
00788 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
00789 ret = AVERROR(EINVAL);
00790 goto free_and_end;
00791 }
00792 }
00793 if (avctx->codec->supported_samplerates) {
00794 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
00795 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
00796 break;
00797 if (avctx->codec->supported_samplerates[i] == 0) {
00798 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
00799 ret = AVERROR(EINVAL);
00800 goto free_and_end;
00801 }
00802 }
00803 if (avctx->codec->channel_layouts) {
00804 if (!avctx->channel_layout) {
00805 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
00806 } else {
00807 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
00808 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
00809 break;
00810 if (avctx->codec->channel_layouts[i] == 0) {
00811 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
00812 ret = AVERROR(EINVAL);
00813 goto free_and_end;
00814 }
00815 }
00816 }
00817 if (avctx->channel_layout && avctx->channels) {
00818 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
00819 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
00820 ret = AVERROR(EINVAL);
00821 goto free_and_end;
00822 }
00823 } else if (avctx->channel_layout) {
00824 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
00825 }
00826 }
00827
00828 avctx->pts_correction_num_faulty_pts =
00829 avctx->pts_correction_num_faulty_dts = 0;
00830 avctx->pts_correction_last_pts =
00831 avctx->pts_correction_last_dts = INT64_MIN;
00832
00833 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
00834 ret = avctx->codec->init(avctx);
00835 if (ret < 0) {
00836 goto free_and_end;
00837 }
00838 }
00839
00840 ret=0;
00841 end:
00842 entangled_thread_counter--;
00843
00844
00845 if (ff_lockmgr_cb) {
00846 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
00847 }
00848 if (options) {
00849 av_dict_free(options);
00850 *options = tmp;
00851 }
00852
00853 return ret;
00854 free_and_end:
00855 av_dict_free(&tmp);
00856 av_freep(&avctx->priv_data);
00857 av_freep(&avctx->internal);
00858 avctx->codec= NULL;
00859 goto end;
00860 }
00861
00862 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
00863 const short *samples)
00864 {
00865 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
00866 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
00867 return -1;
00868 }
00869 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
00870 int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
00871 avctx->frame_number++;
00872 return ret;
00873 }else
00874 return 0;
00875 }
00876
00877 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
00878 const AVFrame *pict)
00879 {
00880 if(buf_size < FF_MIN_BUFFER_SIZE){
00881 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
00882 return -1;
00883 }
00884 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
00885 return -1;
00886 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
00887 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
00888 avctx->frame_number++;
00889 emms_c();
00890
00891 return ret;
00892 }else
00893 return 0;
00894 }
00895
00896 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
00897 const AVSubtitle *sub)
00898 {
00899 int ret;
00900 if(sub->start_display_time) {
00901 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
00902 return -1;
00903 }
00904
00905 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
00906 avctx->frame_number++;
00907 return ret;
00908 }
00909
00920 static int64_t guess_correct_pts(AVCodecContext *ctx,
00921 int64_t reordered_pts, int64_t dts)
00922 {
00923 int64_t pts = AV_NOPTS_VALUE;
00924
00925 if (dts != AV_NOPTS_VALUE) {
00926 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
00927 ctx->pts_correction_last_dts = dts;
00928 }
00929 if (reordered_pts != AV_NOPTS_VALUE) {
00930 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
00931 ctx->pts_correction_last_pts = reordered_pts;
00932 }
00933 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
00934 && reordered_pts != AV_NOPTS_VALUE)
00935 pts = reordered_pts;
00936 else
00937 pts = dts;
00938
00939 return pts;
00940 }
00941
00942 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
00943 int *got_picture_ptr,
00944 AVPacket *avpkt)
00945 {
00946 int ret;
00947
00948 *got_picture_ptr= 0;
00949 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
00950 return -1;
00951
00952 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
00953 av_packet_split_side_data(avpkt);
00954 avctx->pkt = avpkt;
00955 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
00956 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
00957 avpkt);
00958 else {
00959 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
00960 avpkt);
00961 picture->pkt_dts= avpkt->dts;
00962
00963 if(!avctx->has_b_frames){
00964 picture->pkt_pos= avpkt->pos;
00965 }
00966
00967 if (!picture->sample_aspect_ratio.num)
00968 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
00969 if (!picture->width)
00970 picture->width = avctx->width;
00971 if (!picture->height)
00972 picture->height = avctx->height;
00973 if (picture->format == PIX_FMT_NONE)
00974 picture->format = avctx->pix_fmt;
00975 }
00976
00977 emms_c();
00978
00979
00980 if (*got_picture_ptr){
00981 avctx->frame_number++;
00982 picture->best_effort_timestamp = guess_correct_pts(avctx,
00983 picture->pkt_pts,
00984 picture->pkt_dts);
00985 }
00986 }else
00987 ret= 0;
00988
00989 return ret;
00990 }
00991
00992 #if FF_API_OLD_DECODE_AUDIO
00993 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
00994 int *frame_size_ptr,
00995 AVPacket *avpkt)
00996 {
00997 AVFrame frame;
00998 int ret, got_frame = 0;
00999
01000 if (avctx->get_buffer != avcodec_default_get_buffer) {
01001 av_log(avctx, AV_LOG_ERROR, "Overriding custom get_buffer() for "
01002 "avcodec_decode_audio3()\n");
01003 avctx->get_buffer = avcodec_default_get_buffer;
01004 avctx->release_buffer = avcodec_default_release_buffer;
01005 }
01006
01007 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
01008
01009 if (ret >= 0 && got_frame) {
01010 int ch, plane_size;
01011 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
01012 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
01013 frame.nb_samples,
01014 avctx->sample_fmt, 1);
01015 if (*frame_size_ptr < data_size) {
01016 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
01017 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
01018 return AVERROR(EINVAL);
01019 }
01020
01021 memcpy(samples, frame.extended_data[0], plane_size);
01022
01023 if (planar && avctx->channels > 1) {
01024 uint8_t *out = ((uint8_t *)samples) + plane_size;
01025 for (ch = 1; ch < avctx->channels; ch++) {
01026 memcpy(out, frame.extended_data[ch], plane_size);
01027 out += plane_size;
01028 }
01029 }
01030 *frame_size_ptr = data_size;
01031 } else {
01032 *frame_size_ptr = 0;
01033 }
01034 return ret;
01035 }
01036 #endif
01037
01038 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
01039 AVFrame *frame,
01040 int *got_frame_ptr,
01041 AVPacket *avpkt)
01042 {
01043 int ret = 0;
01044
01045 *got_frame_ptr = 0;
01046
01047 if (!avpkt->data && avpkt->size) {
01048 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
01049 return AVERROR(EINVAL);
01050 }
01051
01052 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
01053 av_packet_split_side_data(avpkt);
01054 avctx->pkt = avpkt;
01055 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
01056 if (ret >= 0 && *got_frame_ptr) {
01057 avctx->frame_number++;
01058 frame->pkt_dts = avpkt->dts;
01059 }
01060 }
01061 return ret;
01062 }
01063
01064 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
01065 int *got_sub_ptr,
01066 AVPacket *avpkt)
01067 {
01068 int ret;
01069
01070 avctx->pkt = avpkt;
01071 *got_sub_ptr = 0;
01072 avcodec_get_subtitle_defaults(sub);
01073 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
01074 if (*got_sub_ptr)
01075 avctx->frame_number++;
01076 return ret;
01077 }
01078
01079 void avsubtitle_free(AVSubtitle *sub)
01080 {
01081 int i;
01082
01083 for (i = 0; i < sub->num_rects; i++)
01084 {
01085 av_freep(&sub->rects[i]->pict.data[0]);
01086 av_freep(&sub->rects[i]->pict.data[1]);
01087 av_freep(&sub->rects[i]->pict.data[2]);
01088 av_freep(&sub->rects[i]->pict.data[3]);
01089 av_freep(&sub->rects[i]->text);
01090 av_freep(&sub->rects[i]->ass);
01091 av_freep(&sub->rects[i]);
01092 }
01093
01094 av_freep(&sub->rects);
01095
01096 memset(sub, 0, sizeof(AVSubtitle));
01097 }
01098
01099 av_cold int avcodec_close(AVCodecContext *avctx)
01100 {
01101
01102 if (ff_lockmgr_cb) {
01103 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
01104 return -1;
01105 }
01106
01107 entangled_thread_counter++;
01108 if(entangled_thread_counter != 1){
01109 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
01110 entangled_thread_counter--;
01111 return -1;
01112 }
01113
01114 if (HAVE_THREADS && avctx->thread_opaque)
01115 ff_thread_free(avctx);
01116 if (avctx->codec && avctx->codec->close)
01117 avctx->codec->close(avctx);
01118 avcodec_default_free_buffers(avctx);
01119 avctx->coded_frame = NULL;
01120 av_freep(&avctx->internal);
01121 if (avctx->codec && avctx->codec->priv_class)
01122 av_opt_free(avctx->priv_data);
01123 av_opt_free(avctx);
01124 av_freep(&avctx->priv_data);
01125 if(avctx->codec && avctx->codec->encode)
01126 av_freep(&avctx->extradata);
01127 avctx->codec = NULL;
01128 avctx->active_thread_type = 0;
01129 entangled_thread_counter--;
01130
01131
01132 if (ff_lockmgr_cb) {
01133 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
01134 }
01135 return 0;
01136 }
01137
01138 static enum CodecID remap_deprecated_codec_id(enum CodecID id)
01139 {
01140 switch(id){
01141 case CODEC_ID_G723_1_DEPRECATED : return CODEC_ID_G723_1;
01142 case CODEC_ID_G729_DEPRECATED : return CODEC_ID_G729;
01143 case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
01144 default : return id;
01145 }
01146 }
01147
01148 AVCodec *avcodec_find_encoder(enum CodecID id)
01149 {
01150 AVCodec *p, *experimental=NULL;
01151 p = first_avcodec;
01152 id= remap_deprecated_codec_id(id);
01153 while (p) {
01154 if (p->encode != NULL && p->id == id) {
01155 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
01156 experimental = p;
01157 } else
01158 return p;
01159 }
01160 p = p->next;
01161 }
01162 return experimental;
01163 }
01164
01165 AVCodec *avcodec_find_encoder_by_name(const char *name)
01166 {
01167 AVCodec *p;
01168 if (!name)
01169 return NULL;
01170 p = first_avcodec;
01171 while (p) {
01172 if (p->encode != NULL && strcmp(name,p->name) == 0)
01173 return p;
01174 p = p->next;
01175 }
01176 return NULL;
01177 }
01178
01179 AVCodec *avcodec_find_decoder(enum CodecID id)
01180 {
01181 AVCodec *p, *experimental=NULL;
01182 p = first_avcodec;
01183 id= remap_deprecated_codec_id(id);
01184 while (p) {
01185 if (p->decode != NULL && p->id == id) {
01186 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
01187 experimental = p;
01188 } else
01189 return p;
01190 }
01191 p = p->next;
01192 }
01193 return experimental;
01194 }
01195
01196 AVCodec *avcodec_find_decoder_by_name(const char *name)
01197 {
01198 AVCodec *p;
01199 if (!name)
01200 return NULL;
01201 p = first_avcodec;
01202 while (p) {
01203 if (p->decode != NULL && strcmp(name,p->name) == 0)
01204 return p;
01205 p = p->next;
01206 }
01207 return NULL;
01208 }
01209
01210 static int get_bit_rate(AVCodecContext *ctx)
01211 {
01212 int bit_rate;
01213 int bits_per_sample;
01214
01215 switch(ctx->codec_type) {
01216 case AVMEDIA_TYPE_VIDEO:
01217 case AVMEDIA_TYPE_DATA:
01218 case AVMEDIA_TYPE_SUBTITLE:
01219 case AVMEDIA_TYPE_ATTACHMENT:
01220 bit_rate = ctx->bit_rate;
01221 break;
01222 case AVMEDIA_TYPE_AUDIO:
01223 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
01224 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
01225 break;
01226 default:
01227 bit_rate = 0;
01228 break;
01229 }
01230 return bit_rate;
01231 }
01232
01233 const char *avcodec_get_name(enum CodecID id)
01234 {
01235 AVCodec *codec;
01236
01237 #if !CONFIG_SMALL
01238 switch (id) {
01239 #include "libavcodec/codec_names.h"
01240 }
01241 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
01242 #endif
01243 codec = avcodec_find_decoder(id);
01244 if (codec)
01245 return codec->name;
01246 codec = avcodec_find_encoder(id);
01247 if (codec)
01248 return codec->name;
01249 return "unknown_codec";
01250 }
01251
01252 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
01253 {
01254 int i, len, ret = 0;
01255
01256 for (i = 0; i < 4; i++) {
01257 len = snprintf(buf, buf_size,
01258 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
01259 buf += len;
01260 buf_size = buf_size > len ? buf_size - len : 0;
01261 ret += len;
01262 codec_tag>>=8;
01263 }
01264 return ret;
01265 }
01266
01267 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
01268 {
01269 const char *codec_type;
01270 const char *codec_name;
01271 const char *profile = NULL;
01272 AVCodec *p;
01273 int bitrate;
01274 AVRational display_aspect_ratio;
01275
01276 if (!buf || buf_size <= 0)
01277 return;
01278 codec_type = av_get_media_type_string(enc->codec_type);
01279 codec_name = avcodec_get_name(enc->codec_id);
01280 if (enc->profile != FF_PROFILE_UNKNOWN) {
01281 p = encode ? avcodec_find_encoder(enc->codec_id) :
01282 avcodec_find_decoder(enc->codec_id);
01283 if (p)
01284 profile = av_get_profile_name(p, enc->profile);
01285 }
01286
01287 snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
01288 codec_name, enc->mb_decision ? " (hq)" : "");
01289 buf[0] ^= 'a' ^ 'A';
01290 if (profile)
01291 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
01292 if (enc->codec_tag) {
01293 char tag_buf[32];
01294 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
01295 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01296 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
01297 }
01298 switch(enc->codec_type) {
01299 case AVMEDIA_TYPE_VIDEO:
01300 if (enc->pix_fmt != PIX_FMT_NONE) {
01301 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01302 ", %s",
01303 av_get_pix_fmt_name(enc->pix_fmt));
01304 }
01305 if (enc->width) {
01306 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01307 ", %dx%d",
01308 enc->width, enc->height);
01309 if (enc->sample_aspect_ratio.num) {
01310 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
01311 enc->width*enc->sample_aspect_ratio.num,
01312 enc->height*enc->sample_aspect_ratio.den,
01313 1024*1024);
01314 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01315 " [SAR %d:%d DAR %d:%d]",
01316 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
01317 display_aspect_ratio.num, display_aspect_ratio.den);
01318 }
01319 if(av_log_get_level() >= AV_LOG_DEBUG){
01320 int g= av_gcd(enc->time_base.num, enc->time_base.den);
01321 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01322 ", %d/%d",
01323 enc->time_base.num/g, enc->time_base.den/g);
01324 }
01325 }
01326 if (encode) {
01327 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01328 ", q=%d-%d", enc->qmin, enc->qmax);
01329 }
01330 break;
01331 case AVMEDIA_TYPE_AUDIO:
01332 if (enc->sample_rate) {
01333 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01334 ", %d Hz", enc->sample_rate);
01335 }
01336 av_strlcat(buf, ", ", buf_size);
01337 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
01338 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
01339 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01340 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
01341 }
01342 break;
01343 default:
01344 return;
01345 }
01346 if (encode) {
01347 if (enc->flags & CODEC_FLAG_PASS1)
01348 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01349 ", pass 1");
01350 if (enc->flags & CODEC_FLAG_PASS2)
01351 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01352 ", pass 2");
01353 }
01354 bitrate = get_bit_rate(enc);
01355 if (bitrate != 0) {
01356 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01357 ", %d kb/s", bitrate / 1000);
01358 }
01359 }
01360
01361 const char *av_get_profile_name(const AVCodec *codec, int profile)
01362 {
01363 const AVProfile *p;
01364 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
01365 return NULL;
01366
01367 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
01368 if (p->profile == profile)
01369 return p->name;
01370
01371 return NULL;
01372 }
01373
01374 unsigned avcodec_version( void )
01375 {
01376 av_assert0(CODEC_ID_V410==164);
01377 av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
01378 av_assert0(CODEC_ID_ADPCM_G722==69660);
01379 av_assert0(CODEC_ID_BMV_AUDIO==86071);
01380 av_assert0(CODEC_ID_SRT==94216);
01381
01382 return LIBAVCODEC_VERSION_INT;
01383 }
01384
01385 const char *avcodec_configuration(void)
01386 {
01387 return FFMPEG_CONFIGURATION;
01388 }
01389
01390 const char *avcodec_license(void)
01391 {
01392 #define LICENSE_PREFIX "libavcodec license: "
01393 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
01394 }
01395
01396 void avcodec_flush_buffers(AVCodecContext *avctx)
01397 {
01398 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
01399 ff_thread_flush(avctx);
01400 else if(avctx->codec->flush)
01401 avctx->codec->flush(avctx);
01402 }
01403
01404 static void video_free_buffers(AVCodecContext *s)
01405 {
01406 AVCodecInternal *avci = s->internal;
01407 int i, j;
01408
01409 if (!avci->buffer)
01410 return;
01411
01412 if (avci->buffer_count)
01413 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
01414 avci->buffer_count);
01415 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
01416 InternalBuffer *buf = &avci->buffer[i];
01417 for(j=0; j<4; j++){
01418 av_freep(&buf->base[j]);
01419 buf->data[j]= NULL;
01420 }
01421 }
01422 av_freep(&avci->buffer);
01423
01424 avci->buffer_count=0;
01425 }
01426
01427 static void audio_free_buffers(AVCodecContext *avctx)
01428 {
01429 AVCodecInternal *avci = avctx->internal;
01430 InternalBuffer *buf;
01431
01432 if (!avci->buffer)
01433 return;
01434 buf = avci->buffer;
01435
01436 if (buf->extended_data) {
01437 av_free(buf->extended_data[0]);
01438 if (buf->extended_data != buf->data)
01439 av_free(buf->extended_data);
01440 }
01441 av_freep(&avci->buffer);
01442 }
01443
01444 void avcodec_default_free_buffers(AVCodecContext *avctx)
01445 {
01446 switch (avctx->codec_type) {
01447 case AVMEDIA_TYPE_VIDEO:
01448 video_free_buffers(avctx);
01449 break;
01450 case AVMEDIA_TYPE_AUDIO:
01451 audio_free_buffers(avctx);
01452 break;
01453 default:
01454 break;
01455 }
01456 }
01457
01458 #if FF_API_OLD_FF_PICT_TYPES
01459 char av_get_pict_type_char(int pict_type){
01460 return av_get_picture_type_char(pict_type);
01461 }
01462 #endif
01463
01464 int av_get_bits_per_sample(enum CodecID codec_id){
01465 switch(codec_id){
01466 case CODEC_ID_ADPCM_SBPRO_2:
01467 return 2;
01468 case CODEC_ID_ADPCM_SBPRO_3:
01469 return 3;
01470 case CODEC_ID_ADPCM_SBPRO_4:
01471 case CODEC_ID_ADPCM_CT:
01472 case CODEC_ID_ADPCM_IMA_WAV:
01473 case CODEC_ID_ADPCM_IMA_QT:
01474 case CODEC_ID_ADPCM_SWF:
01475 case CODEC_ID_ADPCM_MS:
01476 case CODEC_ID_ADPCM_YAMAHA:
01477 case CODEC_ID_ADPCM_G722:
01478 return 4;
01479 case CODEC_ID_PCM_ALAW:
01480 case CODEC_ID_PCM_MULAW:
01481 case CODEC_ID_PCM_S8:
01482 case CODEC_ID_PCM_U8:
01483 case CODEC_ID_PCM_ZORK:
01484 return 8;
01485 case CODEC_ID_PCM_S16BE:
01486 case CODEC_ID_PCM_S16LE:
01487 case CODEC_ID_PCM_S16LE_PLANAR:
01488 case CODEC_ID_PCM_U16BE:
01489 case CODEC_ID_PCM_U16LE:
01490 return 16;
01491 case CODEC_ID_PCM_S24DAUD:
01492 case CODEC_ID_PCM_S24BE:
01493 case CODEC_ID_PCM_S24LE:
01494 case CODEC_ID_PCM_U24BE:
01495 case CODEC_ID_PCM_U24LE:
01496 return 24;
01497 case CODEC_ID_PCM_S32BE:
01498 case CODEC_ID_PCM_S32LE:
01499 case CODEC_ID_PCM_U32BE:
01500 case CODEC_ID_PCM_U32LE:
01501 case CODEC_ID_PCM_F32BE:
01502 case CODEC_ID_PCM_F32LE:
01503 return 32;
01504 case CODEC_ID_PCM_F64BE:
01505 case CODEC_ID_PCM_F64LE:
01506 return 64;
01507 default:
01508 return 0;
01509 }
01510 }
01511
01512 #if FF_API_OLD_SAMPLE_FMT
01513 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
01514 return av_get_bytes_per_sample(sample_fmt) << 3;
01515 }
01516 #endif
01517
01518 #if !HAVE_THREADS
01519 int ff_thread_init(AVCodecContext *s){
01520 return -1;
01521 }
01522 #endif
01523
01524 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
01525 {
01526 unsigned int n = 0;
01527
01528 while(v >= 0xff) {
01529 *s++ = 0xff;
01530 v -= 0xff;
01531 n++;
01532 }
01533 *s = v;
01534 n++;
01535 return n;
01536 }
01537
01538 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
01539 int i;
01540 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
01541 return i;
01542 }
01543
01544 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
01545 {
01546 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
01547 "version to the newest one from Git. If the problem still "
01548 "occurs, it means that your file has a feature which has not "
01549 "been implemented.\n", feature);
01550 if(want_sample)
01551 av_log_ask_for_sample(avc, NULL);
01552 }
01553
01554 void av_log_ask_for_sample(void *avc, const char *msg, ...)
01555 {
01556 va_list argument_list;
01557
01558 va_start(argument_list, msg);
01559
01560 if (msg)
01561 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
01562 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
01563 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
01564 "and contact the ffmpeg-devel mailing list.\n");
01565
01566 va_end(argument_list);
01567 }
01568
01569 static AVHWAccel *first_hwaccel = NULL;
01570
01571 void av_register_hwaccel(AVHWAccel *hwaccel)
01572 {
01573 AVHWAccel **p = &first_hwaccel;
01574 while (*p)
01575 p = &(*p)->next;
01576 *p = hwaccel;
01577 hwaccel->next = NULL;
01578 }
01579
01580 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
01581 {
01582 return hwaccel ? hwaccel->next : first_hwaccel;
01583 }
01584
01585 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
01586 {
01587 AVHWAccel *hwaccel=NULL;
01588
01589 while((hwaccel= av_hwaccel_next(hwaccel))){
01590 if ( hwaccel->id == codec_id
01591 && hwaccel->pix_fmt == pix_fmt)
01592 return hwaccel;
01593 }
01594 return NULL;
01595 }
01596
01597 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
01598 {
01599 if (ff_lockmgr_cb) {
01600 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
01601 return -1;
01602 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
01603 return -1;
01604 }
01605
01606 ff_lockmgr_cb = cb;
01607
01608 if (ff_lockmgr_cb) {
01609 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
01610 return -1;
01611 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
01612 return -1;
01613 }
01614 return 0;
01615 }
01616
01617 int avpriv_lock_avformat(void)
01618 {
01619 if (ff_lockmgr_cb) {
01620 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
01621 return -1;
01622 }
01623 return 0;
01624 }
01625
01626 int avpriv_unlock_avformat(void)
01627 {
01628 if (ff_lockmgr_cb) {
01629 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
01630 return -1;
01631 }
01632 return 0;
01633 }
01634
01635 unsigned int avpriv_toupper4(unsigned int x)
01636 {
01637 return toupper( x &0xFF)
01638 + (toupper((x>>8 )&0xFF)<<8 )
01639 + (toupper((x>>16)&0xFF)<<16)
01640 + (toupper((x>>24)&0xFF)<<24);
01641 }
01642
01643 #if !HAVE_THREADS
01644
01645 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
01646 {
01647 f->owner = avctx;
01648
01649 ff_init_buffer_info(avctx, f);
01650
01651 return avctx->get_buffer(avctx, f);
01652 }
01653
01654 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
01655 {
01656 f->owner->release_buffer(f->owner, f);
01657 }
01658
01659 void ff_thread_finish_setup(AVCodecContext *avctx)
01660 {
01661 }
01662
01663 void ff_thread_report_progress(AVFrame *f, int progress, int field)
01664 {
01665 }
01666
01667 void ff_thread_await_progress(AVFrame *f, int progress, int field)
01668 {
01669 }
01670
01671 #endif
01672
01673 #if FF_API_THREAD_INIT
01674 int avcodec_thread_init(AVCodecContext *s, int thread_count)
01675 {
01676 s->thread_count = thread_count;
01677 return ff_thread_init(s);
01678 }
01679 #endif
01680
01681 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
01682 {
01683 AVCodec *c= avcodec_find_decoder(codec_id);
01684 if(!c)
01685 c= avcodec_find_encoder(codec_id);
01686 if(c)
01687 return c->type;
01688
01689 if (codec_id <= CODEC_ID_NONE)
01690 return AVMEDIA_TYPE_UNKNOWN;
01691 else if (codec_id < CODEC_ID_FIRST_AUDIO)
01692 return AVMEDIA_TYPE_VIDEO;
01693 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
01694 return AVMEDIA_TYPE_AUDIO;
01695 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
01696 return AVMEDIA_TYPE_SUBTITLE;
01697
01698 return AVMEDIA_TYPE_UNKNOWN;
01699 }