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