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 "libavutil/avassert.h"
00038 #include "avcodec.h"
00039 #include "dsputil.h"
00040 #include "libavutil/opt.h"
00041 #include "imgconvert.h"
00042 #include "thread.h"
00043 #include "audioconvert.h"
00044 #include "internal.h"
00045 #include "bytestream.h"
00046 #include <stdlib.h>
00047 #include <stdarg.h>
00048 #include <limits.h>
00049 #include <float.h>
00050
00051 static int volatile entangled_thread_counter=0;
00052 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
00053 static void *codec_mutex;
00054 static void *avformat_mutex;
00055
00056 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
00057 {
00058 if(min_size < *size)
00059 return ptr;
00060
00061 min_size= FFMAX(17*min_size/16 + 32, min_size);
00062
00063 ptr= av_realloc(ptr, min_size);
00064 if(!ptr)
00065 min_size= 0;
00066
00067 *size= min_size;
00068
00069 return ptr;
00070 }
00071
00072 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
00073 {
00074 void **p = ptr;
00075 if (min_size < *size)
00076 return 0;
00077 min_size= FFMAX(17*min_size/16 + 32, min_size);
00078 av_free(*p);
00079 *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
00080 if (!*p) min_size = 0;
00081 *size= min_size;
00082 return 1;
00083 }
00084
00085 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
00086 {
00087 ff_fast_malloc(ptr, size, min_size, 0);
00088 }
00089
00090 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
00091 {
00092 uint8_t **p = ptr;
00093 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
00094 av_freep(p);
00095 *size = 0;
00096 return;
00097 }
00098 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
00099 memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00100 }
00101
00102
00103 static AVCodec *first_avcodec = NULL;
00104
00105 AVCodec *av_codec_next(AVCodec *c){
00106 if(c) return c->next;
00107 else return first_avcodec;
00108 }
00109
00110 static void avcodec_init(void)
00111 {
00112 static int initialized = 0;
00113
00114 if (initialized != 0)
00115 return;
00116 initialized = 1;
00117
00118 ff_dsputil_static_init();
00119 }
00120
00121 int av_codec_is_encoder(AVCodec *codec)
00122 {
00123 return codec && (codec->encode || codec->encode2);
00124 }
00125
00126 int av_codec_is_decoder(AVCodec *codec)
00127 {
00128 return codec && codec->decode;
00129 }
00130
00131 void avcodec_register(AVCodec *codec)
00132 {
00133 AVCodec **p;
00134 avcodec_init();
00135 p = &first_avcodec;
00136 while (*p != NULL) p = &(*p)->next;
00137 *p = codec;
00138 codec->next = NULL;
00139
00140 if (codec->init_static_data)
00141 codec->init_static_data(codec);
00142 }
00143
00144 unsigned avcodec_get_edge_width(void)
00145 {
00146 return EDGE_WIDTH;
00147 }
00148
00149 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
00150 s->coded_width = width;
00151 s->coded_height= height;
00152 s->width = -((-width )>>s->lowres);
00153 s->height= -((-height)>>s->lowres);
00154 }
00155
00156 #define INTERNAL_BUFFER_SIZE (32+1)
00157
00158 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
00159 int linesize_align[AV_NUM_DATA_POINTERS])
00160 {
00161 int i;
00162 int w_align= 1;
00163 int h_align= 1;
00164
00165 switch(s->pix_fmt){
00166 case PIX_FMT_YUV420P:
00167 case PIX_FMT_YUYV422:
00168 case PIX_FMT_UYVY422:
00169 case PIX_FMT_YUV422P:
00170 case PIX_FMT_YUV440P:
00171 case PIX_FMT_YUV444P:
00172 case PIX_FMT_GBRP:
00173 case PIX_FMT_GRAY8:
00174 case PIX_FMT_GRAY16BE:
00175 case PIX_FMT_GRAY16LE:
00176 case PIX_FMT_YUVJ420P:
00177 case PIX_FMT_YUVJ422P:
00178 case PIX_FMT_YUVJ440P:
00179 case PIX_FMT_YUVJ444P:
00180 case PIX_FMT_YUVA420P:
00181 case PIX_FMT_YUVA422P:
00182 case PIX_FMT_YUVA444P:
00183 case PIX_FMT_YUV420P9LE:
00184 case PIX_FMT_YUV420P9BE:
00185 case PIX_FMT_YUV420P10LE:
00186 case PIX_FMT_YUV420P10BE:
00187 case PIX_FMT_YUV422P9LE:
00188 case PIX_FMT_YUV422P9BE:
00189 case PIX_FMT_YUV422P10LE:
00190 case PIX_FMT_YUV422P10BE:
00191 case PIX_FMT_YUV444P9LE:
00192 case PIX_FMT_YUV444P9BE:
00193 case PIX_FMT_YUV444P10LE:
00194 case PIX_FMT_YUV444P10BE:
00195 case PIX_FMT_GBRP9LE:
00196 case PIX_FMT_GBRP9BE:
00197 case PIX_FMT_GBRP10LE:
00198 case PIX_FMT_GBRP10BE:
00199 w_align = 16;
00200 h_align = 16 * 2;
00201 break;
00202 case PIX_FMT_YUV411P:
00203 case PIX_FMT_UYYVYY411:
00204 w_align=32;
00205 h_align=8;
00206 break;
00207 case PIX_FMT_YUV410P:
00208 if(s->codec_id == CODEC_ID_SVQ1){
00209 w_align=64;
00210 h_align=64;
00211 }
00212 case PIX_FMT_RGB555:
00213 if(s->codec_id == CODEC_ID_RPZA){
00214 w_align=4;
00215 h_align=4;
00216 }
00217 case PIX_FMT_PAL8:
00218 case PIX_FMT_BGR8:
00219 case PIX_FMT_RGB8:
00220 if(s->codec_id == CODEC_ID_SMC){
00221 w_align=4;
00222 h_align=4;
00223 }
00224 break;
00225 case PIX_FMT_BGR24:
00226 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
00227 w_align=4;
00228 h_align=4;
00229 }
00230 break;
00231 default:
00232 w_align= 1;
00233 h_align= 1;
00234 break;
00235 }
00236
00237 if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
00238 w_align= FFMAX(w_align, 8);
00239 }
00240
00241 *width = FFALIGN(*width , w_align);
00242 *height= FFALIGN(*height, h_align);
00243 if(s->codec_id == CODEC_ID_H264 || s->lowres)
00244 *height+=2;
00245
00246
00247 for (i = 0; i < 4; i++)
00248 linesize_align[i] = STRIDE_ALIGN;
00249 }
00250
00251 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
00252 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
00253 int linesize_align[AV_NUM_DATA_POINTERS];
00254 int align;
00255 avcodec_align_dimensions2(s, width, height, linesize_align);
00256 align = FFMAX(linesize_align[0], linesize_align[3]);
00257 linesize_align[1] <<= chroma_shift;
00258 linesize_align[2] <<= chroma_shift;
00259 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
00260 *width=FFALIGN(*width, align);
00261 }
00262
00263 void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
00264 {
00265 if (s->pkt) {
00266 pic->pkt_pts = s->pkt->pts;
00267 pic->pkt_pos = s->pkt->pos;
00268 } else {
00269 pic->pkt_pts = AV_NOPTS_VALUE;
00270 pic->pkt_pos = -1;
00271 }
00272 pic->reordered_opaque= s->reordered_opaque;
00273 pic->sample_aspect_ratio = s->sample_aspect_ratio;
00274 pic->width = s->width;
00275 pic->height = s->height;
00276 pic->format = s->pix_fmt;
00277 }
00278
00279 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
00280 enum AVSampleFormat sample_fmt, const uint8_t *buf,
00281 int buf_size, int align)
00282 {
00283 int ch, planar, needed_size, ret = 0;
00284
00285 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
00286 frame->nb_samples, sample_fmt,
00287 align);
00288 if (buf_size < needed_size)
00289 return AVERROR(EINVAL);
00290
00291 planar = av_sample_fmt_is_planar(sample_fmt);
00292 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
00293 if (!(frame->extended_data = av_mallocz(nb_channels *
00294 sizeof(*frame->extended_data))))
00295 return AVERROR(ENOMEM);
00296 } else {
00297 frame->extended_data = frame->data;
00298 }
00299
00300 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
00301 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
00302 sample_fmt, align)) < 0) {
00303 if (frame->extended_data != frame->data)
00304 av_freep(&frame->extended_data);
00305 return ret;
00306 }
00307 if (frame->extended_data != frame->data) {
00308 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
00309 frame->data[ch] = frame->extended_data[ch];
00310 }
00311
00312 return ret;
00313 }
00314
00315 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
00316 {
00317 AVCodecInternal *avci = avctx->internal;
00318 InternalBuffer *buf;
00319 int buf_size, ret;
00320
00321 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
00322 frame->nb_samples, avctx->sample_fmt,
00323 0);
00324 if (buf_size < 0)
00325 return AVERROR(EINVAL);
00326
00327
00328 if (!avci->buffer) {
00329 avci->buffer = av_mallocz(sizeof(InternalBuffer));
00330 if (!avci->buffer)
00331 return AVERROR(ENOMEM);
00332 }
00333 buf = avci->buffer;
00334
00335
00336
00337 if (buf->extended_data) {
00338
00339 if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
00340 av_free(buf->extended_data[0]);
00341 if (buf->extended_data != buf->data)
00342 av_freep(&buf->extended_data);
00343 buf->extended_data = NULL;
00344 buf->data[0] = NULL;
00345 }
00346
00347
00348 if (buf->nb_channels != avctx->channels) {
00349 if (buf->extended_data != buf->data)
00350 av_free(buf->extended_data);
00351 buf->extended_data = NULL;
00352 }
00353 }
00354
00355
00356
00357 if (!buf->extended_data) {
00358 if (!buf->data[0]) {
00359 if (!(buf->data[0] = av_mallocz(buf_size)))
00360 return AVERROR(ENOMEM);
00361 buf->audio_data_size = buf_size;
00362 }
00363 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
00364 avctx->sample_fmt, buf->data[0],
00365 buf->audio_data_size, 0)))
00366 return ret;
00367
00368 if (frame->extended_data == frame->data)
00369 buf->extended_data = buf->data;
00370 else
00371 buf->extended_data = frame->extended_data;
00372 memcpy(buf->data, frame->data, sizeof(frame->data));
00373 buf->linesize[0] = frame->linesize[0];
00374 buf->nb_channels = avctx->channels;
00375 } else {
00376
00377 frame->extended_data = buf->extended_data;
00378 frame->linesize[0] = buf->linesize[0];
00379 memcpy(frame->data, buf->data, sizeof(frame->data));
00380 }
00381
00382 frame->type = FF_BUFFER_TYPE_INTERNAL;
00383
00384 if (avctx->pkt) {
00385 frame->pkt_pts = avctx->pkt->pts;
00386 frame->pkt_pos = avctx->pkt->pos;
00387 } else {
00388 frame->pkt_pts = AV_NOPTS_VALUE;
00389 frame->pkt_pos = -1;
00390 }
00391
00392 frame->reordered_opaque = avctx->reordered_opaque;
00393
00394 frame->sample_rate = avctx->sample_rate;
00395 frame->format = avctx->sample_fmt;
00396 frame->channel_layout = avctx->channel_layout;
00397
00398 if (avctx->debug & FF_DEBUG_BUFFERS)
00399 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
00400 "internal audio buffer used\n", frame);
00401
00402 return 0;
00403 }
00404
00405 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
00406 {
00407 int i;
00408 int w= s->width;
00409 int h= s->height;
00410 InternalBuffer *buf;
00411 AVCodecInternal *avci = s->internal;
00412
00413 if(pic->data[0]!=NULL) {
00414 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
00415 return -1;
00416 }
00417 if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
00418 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
00419 return -1;
00420 }
00421
00422 if(av_image_check_size(w, h, 0, s) || s->pix_fmt<0)
00423 return -1;
00424
00425 if (!avci->buffer) {
00426 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
00427 sizeof(InternalBuffer));
00428 }
00429
00430 buf = &avci->buffer[avci->buffer_count];
00431
00432 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
00433 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
00434 av_freep(&buf->base[i]);
00435 buf->data[i]= NULL;
00436 }
00437 }
00438
00439 if (!buf->base[0]) {
00440 int h_chroma_shift, v_chroma_shift;
00441 int size[4] = {0};
00442 int tmpsize;
00443 int unaligned;
00444 AVPicture picture;
00445 int stride_align[AV_NUM_DATA_POINTERS];
00446 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
00447
00448 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
00449
00450 avcodec_align_dimensions2(s, &w, &h, stride_align);
00451
00452 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
00453 w+= EDGE_WIDTH*2;
00454 h+= EDGE_WIDTH*2;
00455 }
00456
00457 do {
00458
00459
00460 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
00461
00462 w += w & ~(w-1);
00463
00464 unaligned = 0;
00465 for (i=0; i<4; i++){
00466 unaligned |= picture.linesize[i] % stride_align[i];
00467 }
00468 } while (unaligned);
00469
00470 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
00471 if (tmpsize < 0)
00472 return -1;
00473
00474 for (i=0; i<3 && picture.data[i+1]; i++)
00475 size[i] = picture.data[i+1] - picture.data[i];
00476 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
00477
00478 memset(buf->base, 0, sizeof(buf->base));
00479 memset(buf->data, 0, sizeof(buf->data));
00480
00481 for(i=0; i<4 && size[i]; i++){
00482 const int h_shift= i==0 ? 0 : h_chroma_shift;
00483 const int v_shift= i==0 ? 0 : v_chroma_shift;
00484
00485 buf->linesize[i]= picture.linesize[i];
00486
00487 buf->base[i]= av_malloc(size[i]+16);
00488 if(buf->base[i]==NULL) return -1;
00489 memset(buf->base[i], 128, size[i]);
00490
00491
00492 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
00493 buf->data[i] = buf->base[i];
00494 else
00495 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
00496 }
00497 for (; i < AV_NUM_DATA_POINTERS; i++) {
00498 buf->base[i] = buf->data[i] = NULL;
00499 buf->linesize[i] = 0;
00500 }
00501 if(size[1] && !size[2])
00502 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
00503 buf->width = s->width;
00504 buf->height = s->height;
00505 buf->pix_fmt= s->pix_fmt;
00506 }
00507 pic->type= FF_BUFFER_TYPE_INTERNAL;
00508
00509 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
00510 pic->base[i]= buf->base[i];
00511 pic->data[i]= buf->data[i];
00512 pic->linesize[i]= buf->linesize[i];
00513 }
00514 pic->extended_data = pic->data;
00515 avci->buffer_count++;
00516 pic->width = buf->width;
00517 pic->height = buf->height;
00518 pic->format = buf->pix_fmt;
00519 pic->sample_aspect_ratio = s->sample_aspect_ratio;
00520
00521 if (s->pkt) {
00522 pic->pkt_pts = s->pkt->pts;
00523 pic->pkt_pos = s->pkt->pos;
00524 } else {
00525 pic->pkt_pts = AV_NOPTS_VALUE;
00526 pic->pkt_pos = -1;
00527 }
00528 pic->reordered_opaque= s->reordered_opaque;
00529 pic->sample_aspect_ratio = s->sample_aspect_ratio;
00530 pic->width = s->width;
00531 pic->height = s->height;
00532 pic->format = s->pix_fmt;
00533
00534 if(s->debug&FF_DEBUG_BUFFERS)
00535 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
00536 "buffers used\n", pic, avci->buffer_count);
00537
00538 return 0;
00539 }
00540
00541 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
00542 {
00543 switch (avctx->codec_type) {
00544 case AVMEDIA_TYPE_VIDEO:
00545 return video_get_buffer(avctx, frame);
00546 case AVMEDIA_TYPE_AUDIO:
00547 return audio_get_buffer(avctx, frame);
00548 default:
00549 return -1;
00550 }
00551 }
00552
00553 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
00554 int i;
00555 InternalBuffer *buf, *last;
00556 AVCodecInternal *avci = s->internal;
00557
00558 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
00559
00560 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
00561 assert(avci->buffer_count);
00562
00563 if (avci->buffer) {
00564 buf = NULL;
00565 for (i = 0; i < avci->buffer_count; i++) {
00566 buf = &avci->buffer[i];
00567 if (buf->data[0] == pic->data[0])
00568 break;
00569 }
00570 av_assert0(i < avci->buffer_count);
00571 avci->buffer_count--;
00572 last = &avci->buffer[avci->buffer_count];
00573
00574 if (buf != last)
00575 FFSWAP(InternalBuffer, *buf, *last);
00576 }
00577
00578 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
00579 pic->data[i]=NULL;
00580
00581 }
00582
00583
00584 if(s->debug&FF_DEBUG_BUFFERS)
00585 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
00586 "buffers used\n", pic, avci->buffer_count);
00587 }
00588
00589 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
00590 AVFrame temp_pic;
00591 int i;
00592
00593 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
00594
00595 if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
00596 av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
00597 pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
00598 s->release_buffer(s, pic);
00599 }
00600
00601 ff_init_buffer_info(s, pic);
00602
00603
00604 if(pic->data[0] == NULL) {
00605
00606 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
00607 return s->get_buffer(s, pic);
00608 }
00609
00610 assert(s->pix_fmt == pic->format);
00611
00612
00613 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
00614 return 0;
00615 }
00616
00617
00618
00619
00620 temp_pic = *pic;
00621 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
00622 pic->data[i] = pic->base[i] = NULL;
00623 pic->opaque = NULL;
00624
00625 if (s->get_buffer(s, pic))
00626 return -1;
00627
00628 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
00629 s->height);
00630 s->release_buffer(s, &temp_pic);
00631 return 0;
00632 }
00633
00634 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
00635 int i;
00636
00637 for(i=0; i<count; i++){
00638 int r= func(c, (char*)arg + i*size);
00639 if(ret) ret[i]= r;
00640 }
00641 return 0;
00642 }
00643
00644 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
00645 int i;
00646
00647 for(i=0; i<count; i++){
00648 int r= func(c, arg, i, 0);
00649 if(ret) ret[i]= r;
00650 }
00651 return 0;
00652 }
00653
00654 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
00655 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
00656 ++fmt;
00657 return fmt[0];
00658 }
00659
00660 void avcodec_get_frame_defaults(AVFrame *pic){
00661 memset(pic, 0, sizeof(AVFrame));
00662
00663 pic->pts = pic->pkt_dts = pic->pkt_pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
00664 pic->pkt_pos = -1;
00665 pic->key_frame= 1;
00666 pic->sample_aspect_ratio = (AVRational){0, 1};
00667 pic->format = -1;
00668 }
00669
00670 AVFrame *avcodec_alloc_frame(void){
00671 AVFrame *pic= av_malloc(sizeof(AVFrame));
00672
00673 if(pic==NULL) return NULL;
00674
00675 avcodec_get_frame_defaults(pic);
00676
00677 return pic;
00678 }
00679
00680 #define MAKE_ACCESSORS(str, name, type, field) \
00681 type av_##name##_get_##field(const str *s) { return s->field; } \
00682 void av_##name##_set_##field(str *s, type v) { s->field = v; }
00683
00684 MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
00685 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
00686 MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
00687 MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
00688
00689 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
00690 {
00691 memset(sub, 0, sizeof(*sub));
00692 sub->pts = AV_NOPTS_VALUE;
00693 }
00694
00695 static int get_bit_rate(AVCodecContext *ctx)
00696 {
00697 int bit_rate;
00698 int bits_per_sample;
00699
00700 switch(ctx->codec_type) {
00701 case AVMEDIA_TYPE_VIDEO:
00702 case AVMEDIA_TYPE_DATA:
00703 case AVMEDIA_TYPE_SUBTITLE:
00704 case AVMEDIA_TYPE_ATTACHMENT:
00705 bit_rate = ctx->bit_rate;
00706 break;
00707 case AVMEDIA_TYPE_AUDIO:
00708 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
00709 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
00710 break;
00711 default:
00712 bit_rate = 0;
00713 break;
00714 }
00715 return bit_rate;
00716 }
00717
00718 #if FF_API_AVCODEC_OPEN
00719 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
00720 {
00721 return avcodec_open2(avctx, codec, NULL);
00722 }
00723 #endif
00724
00725 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
00726 {
00727 int ret = 0;
00728 AVDictionary *tmp = NULL;
00729
00730 if (avcodec_is_open(avctx))
00731 return 0;
00732
00733 if ((!codec && !avctx->codec)) {
00734 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
00735 return AVERROR(EINVAL);
00736 }
00737 if ((codec && avctx->codec && codec != avctx->codec)) {
00738 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
00739 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
00740 return AVERROR(EINVAL);
00741 }
00742 if (!codec)
00743 codec = avctx->codec;
00744
00745 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
00746 return AVERROR(EINVAL);
00747
00748 if (options)
00749 av_dict_copy(&tmp, *options, 0);
00750
00751
00752 if (ff_lockmgr_cb) {
00753 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
00754 return -1;
00755 }
00756
00757 entangled_thread_counter++;
00758 if(entangled_thread_counter != 1){
00759 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
00760 ret = -1;
00761 goto end;
00762 }
00763
00764 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
00765 if (!avctx->internal) {
00766 ret = AVERROR(ENOMEM);
00767 goto end;
00768 }
00769
00770 if (codec->priv_data_size > 0) {
00771 if(!avctx->priv_data){
00772 avctx->priv_data = av_mallocz(codec->priv_data_size);
00773 if (!avctx->priv_data) {
00774 ret = AVERROR(ENOMEM);
00775 goto end;
00776 }
00777 if (codec->priv_class) {
00778 *(const AVClass**)avctx->priv_data= codec->priv_class;
00779 av_opt_set_defaults(avctx->priv_data);
00780 }
00781 }
00782 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
00783 goto free_and_end;
00784 } else {
00785 avctx->priv_data = NULL;
00786 }
00787 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
00788 goto free_and_end;
00789
00790 if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
00791 if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
00792 av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, try -strict -2\n");
00793 ret = -1;
00794 goto free_and_end;
00795 }
00796
00797
00798 if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
00799 if(avctx->coded_width && avctx->coded_height)
00800 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
00801 else if(avctx->width && avctx->height)
00802 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
00803 }
00804
00805 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
00806 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
00807 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
00808 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
00809 avcodec_set_dimensions(avctx, 0, 0);
00810 }
00811
00812
00813
00814 if (av_codec_is_decoder(codec))
00815 av_freep(&avctx->subtitle_header);
00816
00817 #define SANE_NB_CHANNELS 128U
00818 if (avctx->channels > SANE_NB_CHANNELS) {
00819 ret = AVERROR(EINVAL);
00820 goto free_and_end;
00821 }
00822
00823 avctx->codec = codec;
00824 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
00825 avctx->codec_id == CODEC_ID_NONE) {
00826 avctx->codec_type = codec->type;
00827 avctx->codec_id = codec->id;
00828 }
00829 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
00830 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
00831 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
00832 ret = AVERROR(EINVAL);
00833 goto free_and_end;
00834 }
00835 avctx->frame_number = 0;
00836
00837 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
00838 (!avctx->time_base.num || !avctx->time_base.den)) {
00839 avctx->time_base.num = 1;
00840 avctx->time_base.den = avctx->sample_rate;
00841 }
00842
00843 if (!HAVE_THREADS)
00844 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
00845
00846 if (HAVE_THREADS && !avctx->thread_opaque) {
00847 ret = ff_thread_init(avctx);
00848 if (ret < 0) {
00849 goto free_and_end;
00850 }
00851 }
00852 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
00853 avctx->thread_count = 1;
00854
00855 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
00856 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
00857 avctx->codec->max_lowres);
00858 ret = AVERROR(EINVAL);
00859 goto free_and_end;
00860 }
00861
00862 if (av_codec_is_encoder(avctx->codec)) {
00863 int i;
00864 if (avctx->codec->sample_fmts) {
00865 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
00866 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
00867 break;
00868 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
00869 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
00870 ret = AVERROR(EINVAL);
00871 goto free_and_end;
00872 }
00873 }
00874 if (avctx->codec->pix_fmts) {
00875 for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
00876 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
00877 break;
00878 if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE) {
00879 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
00880 ret = AVERROR(EINVAL);
00881 goto free_and_end;
00882 }
00883 }
00884 if (avctx->codec->supported_samplerates) {
00885 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
00886 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
00887 break;
00888 if (avctx->codec->supported_samplerates[i] == 0) {
00889 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
00890 ret = AVERROR(EINVAL);
00891 goto free_and_end;
00892 }
00893 }
00894 if (avctx->codec->channel_layouts) {
00895 if (!avctx->channel_layout) {
00896 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
00897 } else {
00898 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
00899 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
00900 break;
00901 if (avctx->codec->channel_layouts[i] == 0) {
00902 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
00903 ret = AVERROR(EINVAL);
00904 goto free_and_end;
00905 }
00906 }
00907 }
00908 if (avctx->channel_layout && avctx->channels) {
00909 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
00910 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
00911 ret = AVERROR(EINVAL);
00912 goto free_and_end;
00913 }
00914 } else if (avctx->channel_layout) {
00915 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
00916 }
00917 }
00918
00919 avctx->pts_correction_num_faulty_pts =
00920 avctx->pts_correction_num_faulty_dts = 0;
00921 avctx->pts_correction_last_pts =
00922 avctx->pts_correction_last_dts = INT64_MIN;
00923
00924 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
00925 ret = avctx->codec->init(avctx);
00926 if (ret < 0) {
00927 goto free_and_end;
00928 }
00929 }
00930
00931 ret=0;
00932
00933 if (av_codec_is_decoder(avctx->codec)) {
00934 if (!avctx->bit_rate)
00935 avctx->bit_rate = get_bit_rate(avctx);
00936
00937 if (avctx->channel_layout &&
00938 av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
00939 av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n");
00940 avctx->channel_layout = 0;
00941 }
00942 }
00943 end:
00944 entangled_thread_counter--;
00945
00946
00947 if (ff_lockmgr_cb) {
00948 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
00949 }
00950 if (options) {
00951 av_dict_free(options);
00952 *options = tmp;
00953 }
00954
00955 return ret;
00956 free_and_end:
00957 av_dict_free(&tmp);
00958 av_freep(&avctx->priv_data);
00959 av_freep(&avctx->internal);
00960 avctx->codec= NULL;
00961 goto end;
00962 }
00963
00964 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
00965 {
00966 if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
00967 av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
00968 return AVERROR(EINVAL);
00969 }
00970
00971 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
00972 if (!avpkt->data || avpkt->size < size) {
00973 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
00974 avpkt->data = avctx->internal->byte_buffer;
00975 avpkt->size = avctx->internal->byte_buffer_size;
00976 avpkt->destruct = NULL;
00977 }
00978
00979 if (avpkt->data) {
00980 void *destruct = avpkt->destruct;
00981
00982 if (avpkt->size < size) {
00983 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
00984 return AVERROR(EINVAL);
00985 }
00986
00987 av_init_packet(avpkt);
00988 avpkt->destruct = destruct;
00989 avpkt->size = size;
00990 return 0;
00991 } else {
00992 int ret = av_new_packet(avpkt, size);
00993 if (ret < 0)
00994 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
00995 return ret;
00996 }
00997 }
00998
00999 int ff_alloc_packet(AVPacket *avpkt, int size)
01000 {
01001 return ff_alloc_packet2(NULL, avpkt, size);
01002 }
01003
01007 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
01008 {
01009 AVFrame *frame = NULL;
01010 uint8_t *buf = NULL;
01011 int ret;
01012
01013 if (!(frame = avcodec_alloc_frame()))
01014 return AVERROR(ENOMEM);
01015 *frame = *src;
01016
01017 if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
01018 s->frame_size, s->sample_fmt, 0)) < 0)
01019 goto fail;
01020
01021 if (!(buf = av_malloc(ret))) {
01022 ret = AVERROR(ENOMEM);
01023 goto fail;
01024 }
01025
01026 frame->nb_samples = s->frame_size;
01027 if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
01028 buf, ret, 0)) < 0)
01029 goto fail;
01030 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
01031 src->nb_samples, s->channels, s->sample_fmt)) < 0)
01032 goto fail;
01033 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
01034 frame->nb_samples - src->nb_samples,
01035 s->channels, s->sample_fmt)) < 0)
01036 goto fail;
01037
01038 *dst = frame;
01039
01040 return 0;
01041
01042 fail:
01043 if (frame->extended_data != frame->data)
01044 av_freep(&frame->extended_data);
01045 av_freep(&buf);
01046 av_freep(&frame);
01047 return ret;
01048 }
01049
01050 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
01051 AVPacket *avpkt,
01052 const AVFrame *frame,
01053 int *got_packet_ptr)
01054 {
01055 AVFrame tmp;
01056 AVFrame *padded_frame = NULL;
01057 int ret;
01058 AVPacket user_pkt = *avpkt;
01059 int needs_realloc = !user_pkt.data;
01060
01061 *got_packet_ptr = 0;
01062
01063 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
01064 av_free_packet(avpkt);
01065 av_init_packet(avpkt);
01066 return 0;
01067 }
01068
01069
01070 if (frame && !frame->extended_data) {
01071 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
01072 avctx->channels > AV_NUM_DATA_POINTERS) {
01073 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
01074 "with more than %d channels, but extended_data is not set.\n",
01075 AV_NUM_DATA_POINTERS);
01076 return AVERROR(EINVAL);
01077 }
01078 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
01079
01080 tmp = *frame;
01081 tmp.extended_data = tmp.data;
01082 frame = &tmp;
01083 }
01084
01085
01086 if (frame) {
01087 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
01088 if (frame->nb_samples > avctx->frame_size)
01089 return AVERROR(EINVAL);
01090 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
01091 if (frame->nb_samples < avctx->frame_size &&
01092 !avctx->internal->last_audio_frame) {
01093 ret = pad_last_frame(avctx, &padded_frame, frame);
01094 if (ret < 0)
01095 return ret;
01096
01097 frame = padded_frame;
01098 avctx->internal->last_audio_frame = 1;
01099 }
01100
01101 if (frame->nb_samples != avctx->frame_size)
01102 return AVERROR(EINVAL);
01103 }
01104 }
01105
01106 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
01107 if (!ret) {
01108 if (*got_packet_ptr) {
01109 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
01110 if (avpkt->pts == AV_NOPTS_VALUE)
01111 avpkt->pts = frame->pts;
01112 if (!avpkt->duration)
01113 avpkt->duration = ff_samples_to_time_base(avctx,
01114 frame->nb_samples);
01115 }
01116 avpkt->dts = avpkt->pts;
01117 } else {
01118 avpkt->size = 0;
01119 }
01120 }
01121 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
01122 needs_realloc = 0;
01123 if (user_pkt.data) {
01124 if (user_pkt.size >= avpkt->size) {
01125 memcpy(user_pkt.data, avpkt->data, avpkt->size);
01126 } else {
01127 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
01128 avpkt->size = user_pkt.size;
01129 ret = -1;
01130 }
01131 avpkt->data = user_pkt.data;
01132 avpkt->destruct = user_pkt.destruct;
01133 } else {
01134 if (av_dup_packet(avpkt) < 0) {
01135 ret = AVERROR(ENOMEM);
01136 }
01137 }
01138 }
01139
01140 if (!ret) {
01141 if (needs_realloc && avpkt->data) {
01142 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
01143 if (new_data)
01144 avpkt->data = new_data;
01145 }
01146
01147 avctx->frame_number++;
01148 }
01149
01150 if (ret < 0 || !*got_packet_ptr) {
01151 av_free_packet(avpkt);
01152 av_init_packet(avpkt);
01153 return ret;
01154 }
01155
01156
01157
01158
01159 avpkt->flags |= AV_PKT_FLAG_KEY;
01160
01161 if (padded_frame) {
01162 av_freep(&padded_frame->data[0]);
01163 if (padded_frame->extended_data != padded_frame->data)
01164 av_freep(&padded_frame->extended_data);
01165 av_freep(&padded_frame);
01166 }
01167
01168 return ret;
01169 }
01170
01171 #if FF_API_OLD_DECODE_AUDIO
01172 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
01173 uint8_t *buf, int buf_size,
01174 const short *samples)
01175 {
01176 AVPacket pkt;
01177 AVFrame frame0;
01178 AVFrame *frame;
01179 int ret, samples_size, got_packet;
01180
01181 av_init_packet(&pkt);
01182 pkt.data = buf;
01183 pkt.size = buf_size;
01184
01185 if (samples) {
01186 frame = &frame0;
01187 avcodec_get_frame_defaults(frame);
01188
01189 if (avctx->frame_size) {
01190 frame->nb_samples = avctx->frame_size;
01191 } else {
01192
01193
01194 int64_t nb_samples;
01195 if (!av_get_bits_per_sample(avctx->codec_id)) {
01196 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
01197 "support this codec\n");
01198 return AVERROR(EINVAL);
01199 }
01200 nb_samples = (int64_t)buf_size * 8 /
01201 (av_get_bits_per_sample(avctx->codec_id) *
01202 avctx->channels);
01203 if (nb_samples >= INT_MAX)
01204 return AVERROR(EINVAL);
01205 frame->nb_samples = nb_samples;
01206 }
01207
01208
01209
01210 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
01211 frame->nb_samples,
01212 avctx->sample_fmt, 1);
01213 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
01214 avctx->sample_fmt,
01215 (const uint8_t *) samples,
01216 samples_size, 1)))
01217 return ret;
01218
01219
01220
01221
01222 if(avctx->sample_rate && avctx->time_base.num)
01223 frame->pts = ff_samples_to_time_base(avctx,
01224 avctx->internal->sample_count);
01225 else
01226 frame->pts = AV_NOPTS_VALUE;
01227 avctx->internal->sample_count += frame->nb_samples;
01228 } else {
01229 frame = NULL;
01230 }
01231
01232 got_packet = 0;
01233 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
01234 if (!ret && got_packet && avctx->coded_frame) {
01235 avctx->coded_frame->pts = pkt.pts;
01236 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
01237 }
01238
01239 ff_packet_free_side_data(&pkt);
01240
01241 if (frame && frame->extended_data != frame->data)
01242 av_freep(&frame->extended_data);
01243
01244 return ret ? ret : pkt.size;
01245 }
01246 #endif
01247
01248 #if FF_API_OLD_ENCODE_VIDEO
01249 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
01250 const AVFrame *pict)
01251 {
01252 AVPacket pkt;
01253 int ret, got_packet = 0;
01254
01255 if(buf_size < FF_MIN_BUFFER_SIZE){
01256 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
01257 return -1;
01258 }
01259
01260 av_init_packet(&pkt);
01261 pkt.data = buf;
01262 pkt.size = buf_size;
01263
01264 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
01265 if (!ret && got_packet && avctx->coded_frame) {
01266 avctx->coded_frame->pts = pkt.pts;
01267 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
01268 }
01269
01270
01271 if (pkt.side_data_elems > 0) {
01272 int i;
01273 for (i = 0; i < pkt.side_data_elems; i++)
01274 av_free(pkt.side_data[i].data);
01275 av_freep(&pkt.side_data);
01276 pkt.side_data_elems = 0;
01277 }
01278
01279 return ret ? ret : pkt.size;
01280 }
01281 #endif
01282
01283 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
01284 AVPacket *avpkt,
01285 const AVFrame *frame,
01286 int *got_packet_ptr)
01287 {
01288 int ret;
01289 AVPacket user_pkt = *avpkt;
01290 int needs_realloc = !user_pkt.data;
01291
01292 *got_packet_ptr = 0;
01293
01294 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
01295 av_free_packet(avpkt);
01296 av_init_packet(avpkt);
01297 avpkt->size = 0;
01298 return 0;
01299 }
01300
01301 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
01302 return AVERROR(EINVAL);
01303
01304 av_assert0(avctx->codec->encode2);
01305
01306 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
01307 av_assert0(ret <= 0);
01308
01309 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
01310 needs_realloc = 0;
01311 if (user_pkt.data) {
01312 if (user_pkt.size >= avpkt->size) {
01313 memcpy(user_pkt.data, avpkt->data, avpkt->size);
01314 } else {
01315 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
01316 avpkt->size = user_pkt.size;
01317 ret = -1;
01318 }
01319 avpkt->data = user_pkt.data;
01320 avpkt->destruct = user_pkt.destruct;
01321 } else {
01322 if (av_dup_packet(avpkt) < 0) {
01323 ret = AVERROR(ENOMEM);
01324 }
01325 }
01326 }
01327
01328 if (!ret) {
01329 if (!*got_packet_ptr)
01330 avpkt->size = 0;
01331 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
01332 avpkt->pts = avpkt->dts = frame->pts;
01333
01334 if (needs_realloc && avpkt->data &&
01335 avpkt->destruct == av_destruct_packet) {
01336 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
01337 if (new_data)
01338 avpkt->data = new_data;
01339 }
01340
01341 avctx->frame_number++;
01342 }
01343
01344 if (ret < 0 || !*got_packet_ptr)
01345 av_free_packet(avpkt);
01346
01347 emms_c();
01348 return ret;
01349 }
01350
01351 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
01352 const AVSubtitle *sub)
01353 {
01354 int ret;
01355 if(sub->start_display_time) {
01356 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
01357 return -1;
01358 }
01359
01360 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)(intptr_t)sub);
01361 avctx->frame_number++;
01362 return ret;
01363 }
01364
01375 static int64_t guess_correct_pts(AVCodecContext *ctx,
01376 int64_t reordered_pts, int64_t dts)
01377 {
01378 int64_t pts = AV_NOPTS_VALUE;
01379
01380 if (dts != AV_NOPTS_VALUE) {
01381 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
01382 ctx->pts_correction_last_dts = dts;
01383 }
01384 if (reordered_pts != AV_NOPTS_VALUE) {
01385 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
01386 ctx->pts_correction_last_pts = reordered_pts;
01387 }
01388 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
01389 && reordered_pts != AV_NOPTS_VALUE)
01390 pts = reordered_pts;
01391 else
01392 pts = dts;
01393
01394 return pts;
01395 }
01396
01397 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
01398 {
01399 int size = 0;
01400 const uint8_t *data;
01401 uint32_t flags;
01402
01403 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
01404 return;
01405
01406 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
01407 if (!data || size < 4)
01408 return;
01409 flags = bytestream_get_le32(&data);
01410 size -= 4;
01411 if (size < 4)
01412 return;
01413 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
01414 avctx->channels = bytestream_get_le32(&data);
01415 size -= 4;
01416 }
01417 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
01418 if (size < 8)
01419 return;
01420 avctx->channel_layout = bytestream_get_le64(&data);
01421 size -= 8;
01422 }
01423 if (size < 4)
01424 return;
01425 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
01426 avctx->sample_rate = bytestream_get_le32(&data);
01427 size -= 4;
01428 }
01429 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
01430 if (size < 8)
01431 return;
01432 avctx->width = bytestream_get_le32(&data);
01433 avctx->height = bytestream_get_le32(&data);
01434 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
01435 size -= 8;
01436 }
01437 }
01438
01439 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
01440 int *got_picture_ptr,
01441 const AVPacket *avpkt)
01442 {
01443 int ret;
01444
01445 AVPacket tmp = *avpkt;
01446
01447 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
01448 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
01449 return AVERROR(EINVAL);
01450 }
01451
01452 *got_picture_ptr= 0;
01453 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
01454 return -1;
01455
01456 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
01457 int did_split = av_packet_split_side_data(&tmp);
01458 apply_param_change(avctx, &tmp);
01459 avctx->pkt = &tmp;
01460 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
01461 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
01462 &tmp);
01463 else {
01464 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
01465 &tmp);
01466 picture->pkt_dts= avpkt->dts;
01467
01468 if(!avctx->has_b_frames){
01469 picture->pkt_pos= avpkt->pos;
01470 }
01471
01472 if (!picture->sample_aspect_ratio.num)
01473 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
01474 if (!picture->width)
01475 picture->width = avctx->width;
01476 if (!picture->height)
01477 picture->height = avctx->height;
01478 if (picture->format == PIX_FMT_NONE)
01479 picture->format = avctx->pix_fmt;
01480 }
01481
01482 emms_c();
01483
01484 avctx->pkt = NULL;
01485 if (did_split) {
01486 ff_packet_free_side_data(&tmp);
01487 if(ret == tmp.size)
01488 ret = avpkt->size;
01489 }
01490
01491 if (*got_picture_ptr){
01492 avctx->frame_number++;
01493 picture->best_effort_timestamp = guess_correct_pts(avctx,
01494 picture->pkt_pts,
01495 picture->pkt_dts);
01496 }
01497 }else
01498 ret= 0;
01499
01500 return ret;
01501 }
01502
01503 #if FF_API_OLD_DECODE_AUDIO
01504 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
01505 int *frame_size_ptr,
01506 AVPacket *avpkt)
01507 {
01508 AVFrame frame;
01509 int ret, got_frame = 0;
01510
01511 if (avctx->get_buffer != avcodec_default_get_buffer) {
01512 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
01513 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
01514 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
01515 "avcodec_decode_audio4()\n");
01516 avctx->get_buffer = avcodec_default_get_buffer;
01517 avctx->release_buffer = avcodec_default_release_buffer;
01518 }
01519
01520 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
01521
01522 if (ret >= 0 && got_frame) {
01523 int ch, plane_size;
01524 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
01525 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
01526 frame.nb_samples,
01527 avctx->sample_fmt, 1);
01528 if (*frame_size_ptr < data_size) {
01529 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
01530 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
01531 return AVERROR(EINVAL);
01532 }
01533
01534 memcpy(samples, frame.extended_data[0], plane_size);
01535
01536 if (planar && avctx->channels > 1) {
01537 uint8_t *out = ((uint8_t *)samples) + plane_size;
01538 for (ch = 1; ch < avctx->channels; ch++) {
01539 memcpy(out, frame.extended_data[ch], plane_size);
01540 out += plane_size;
01541 }
01542 }
01543 *frame_size_ptr = data_size;
01544 } else {
01545 *frame_size_ptr = 0;
01546 }
01547 return ret;
01548 }
01549 #endif
01550
01551 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
01552 AVFrame *frame,
01553 int *got_frame_ptr,
01554 const AVPacket *avpkt)
01555 {
01556 int ret = 0;
01557
01558 *got_frame_ptr = 0;
01559
01560 if (!avpkt->data && avpkt->size) {
01561 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
01562 return AVERROR(EINVAL);
01563 }
01564 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
01565 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
01566 return AVERROR(EINVAL);
01567 }
01568
01569 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
01570
01571 AVPacket tmp = *avpkt;
01572 int did_split = av_packet_split_side_data(&tmp);
01573 apply_param_change(avctx, &tmp);
01574
01575 avctx->pkt = &tmp;
01576 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
01577 if (ret >= 0 && *got_frame_ptr) {
01578 avctx->frame_number++;
01579 frame->pkt_dts = avpkt->dts;
01580 if (frame->format == AV_SAMPLE_FMT_NONE)
01581 frame->format = avctx->sample_fmt;
01582 if (!frame->channel_layout)
01583 frame->channel_layout = avctx->channel_layout;
01584 if (!frame->sample_rate)
01585 frame->sample_rate = avctx->sample_rate;
01586 }
01587
01588 avctx->pkt = NULL;
01589 if (did_split) {
01590 ff_packet_free_side_data(&tmp);
01591 if(ret == tmp.size)
01592 ret = avpkt->size;
01593 }
01594 }
01595 return ret;
01596 }
01597
01598 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
01599 int *got_sub_ptr,
01600 AVPacket *avpkt)
01601 {
01602 int ret;
01603
01604 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
01605 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
01606 return AVERROR(EINVAL);
01607 }
01608
01609 avctx->pkt = avpkt;
01610 *got_sub_ptr = 0;
01611 avcodec_get_subtitle_defaults(sub);
01612 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
01613 if (*got_sub_ptr)
01614 avctx->frame_number++;
01615 return ret;
01616 }
01617
01618 void avsubtitle_free(AVSubtitle *sub)
01619 {
01620 int i;
01621
01622 for (i = 0; i < sub->num_rects; i++)
01623 {
01624 av_freep(&sub->rects[i]->pict.data[0]);
01625 av_freep(&sub->rects[i]->pict.data[1]);
01626 av_freep(&sub->rects[i]->pict.data[2]);
01627 av_freep(&sub->rects[i]->pict.data[3]);
01628 av_freep(&sub->rects[i]->text);
01629 av_freep(&sub->rects[i]->ass);
01630 av_freep(&sub->rects[i]);
01631 }
01632
01633 av_freep(&sub->rects);
01634
01635 memset(sub, 0, sizeof(AVSubtitle));
01636 }
01637
01638 av_cold int avcodec_close(AVCodecContext *avctx)
01639 {
01640
01641 if (ff_lockmgr_cb) {
01642 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
01643 return -1;
01644 }
01645
01646 entangled_thread_counter++;
01647 if(entangled_thread_counter != 1){
01648 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
01649 entangled_thread_counter--;
01650 return -1;
01651 }
01652
01653 if (avcodec_is_open(avctx)) {
01654 if (HAVE_THREADS && avctx->thread_opaque)
01655 ff_thread_free(avctx);
01656 if (avctx->codec && avctx->codec->close)
01657 avctx->codec->close(avctx);
01658 avcodec_default_free_buffers(avctx);
01659 avctx->coded_frame = NULL;
01660 avctx->internal->byte_buffer_size = 0;
01661 av_freep(&avctx->internal->byte_buffer);
01662 av_freep(&avctx->internal);
01663 }
01664
01665 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
01666 av_opt_free(avctx->priv_data);
01667 av_opt_free(avctx);
01668 av_freep(&avctx->priv_data);
01669 if (av_codec_is_encoder(avctx->codec))
01670 av_freep(&avctx->extradata);
01671 avctx->codec = NULL;
01672 avctx->active_thread_type = 0;
01673 entangled_thread_counter--;
01674
01675
01676 if (ff_lockmgr_cb) {
01677 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
01678 }
01679 return 0;
01680 }
01681
01682 static enum CodecID remap_deprecated_codec_id(enum CodecID id)
01683 {
01684 switch(id){
01685
01686
01687
01688 default : return id;
01689 }
01690 }
01691
01692 AVCodec *avcodec_find_encoder(enum CodecID id)
01693 {
01694 AVCodec *p, *experimental=NULL;
01695 p = first_avcodec;
01696 id= remap_deprecated_codec_id(id);
01697 while (p) {
01698 if (av_codec_is_encoder(p) && p->id == id) {
01699 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
01700 experimental = p;
01701 } else
01702 return p;
01703 }
01704 p = p->next;
01705 }
01706 return experimental;
01707 }
01708
01709 AVCodec *avcodec_find_encoder_by_name(const char *name)
01710 {
01711 AVCodec *p;
01712 if (!name)
01713 return NULL;
01714 p = first_avcodec;
01715 while (p) {
01716 if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
01717 return p;
01718 p = p->next;
01719 }
01720 return NULL;
01721 }
01722
01723 AVCodec *avcodec_find_decoder(enum CodecID id)
01724 {
01725 AVCodec *p, *experimental=NULL;
01726 p = first_avcodec;
01727 id= remap_deprecated_codec_id(id);
01728 while (p) {
01729 if (av_codec_is_decoder(p) && p->id == id) {
01730 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
01731 experimental = p;
01732 } else
01733 return p;
01734 }
01735 p = p->next;
01736 }
01737 return experimental;
01738 }
01739
01740 AVCodec *avcodec_find_decoder_by_name(const char *name)
01741 {
01742 AVCodec *p;
01743 if (!name)
01744 return NULL;
01745 p = first_avcodec;
01746 while (p) {
01747 if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
01748 return p;
01749 p = p->next;
01750 }
01751 return NULL;
01752 }
01753
01754 const char *avcodec_get_name(enum CodecID id)
01755 {
01756 AVCodec *codec;
01757
01758 #if !CONFIG_SMALL
01759 switch (id) {
01760 #include "libavcodec/codec_names.h"
01761 }
01762 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
01763 #endif
01764 codec = avcodec_find_decoder(id);
01765 if (codec)
01766 return codec->name;
01767 codec = avcodec_find_encoder(id);
01768 if (codec)
01769 return codec->name;
01770 return "unknown_codec";
01771 }
01772
01773 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
01774 {
01775 int i, len, ret = 0;
01776
01777 for (i = 0; i < 4; i++) {
01778 len = snprintf(buf, buf_size,
01779 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
01780 buf += len;
01781 buf_size = buf_size > len ? buf_size - len : 0;
01782 ret += len;
01783 codec_tag>>=8;
01784 }
01785 return ret;
01786 }
01787
01788 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
01789 {
01790 const char *codec_type;
01791 const char *codec_name;
01792 const char *profile = NULL;
01793 AVCodec *p;
01794 int bitrate;
01795 AVRational display_aspect_ratio;
01796
01797 if (!buf || buf_size <= 0)
01798 return;
01799 codec_type = av_get_media_type_string(enc->codec_type);
01800 codec_name = avcodec_get_name(enc->codec_id);
01801 if (enc->profile != FF_PROFILE_UNKNOWN) {
01802 if (enc->codec)
01803 p = enc->codec;
01804 else
01805 p = encode ? avcodec_find_encoder(enc->codec_id) :
01806 avcodec_find_decoder(enc->codec_id);
01807 if (p)
01808 profile = av_get_profile_name(p, enc->profile);
01809 }
01810
01811 snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
01812 codec_name, enc->mb_decision ? " (hq)" : "");
01813 buf[0] ^= 'a' ^ 'A';
01814 if (profile)
01815 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
01816 if (enc->codec_tag) {
01817 char tag_buf[32];
01818 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
01819 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01820 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
01821 }
01822 switch(enc->codec_type) {
01823 case AVMEDIA_TYPE_VIDEO:
01824 if (enc->pix_fmt != PIX_FMT_NONE) {
01825 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01826 ", %s",
01827 av_get_pix_fmt_name(enc->pix_fmt));
01828 }
01829 if (enc->width) {
01830 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01831 ", %dx%d",
01832 enc->width, enc->height);
01833 if (enc->sample_aspect_ratio.num) {
01834 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
01835 enc->width*enc->sample_aspect_ratio.num,
01836 enc->height*enc->sample_aspect_ratio.den,
01837 1024*1024);
01838 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01839 " [SAR %d:%d DAR %d:%d]",
01840 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
01841 display_aspect_ratio.num, display_aspect_ratio.den);
01842 }
01843 if(av_log_get_level() >= AV_LOG_DEBUG){
01844 int g= av_gcd(enc->time_base.num, enc->time_base.den);
01845 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01846 ", %d/%d",
01847 enc->time_base.num/g, enc->time_base.den/g);
01848 }
01849 }
01850 if (encode) {
01851 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01852 ", q=%d-%d", enc->qmin, enc->qmax);
01853 }
01854 break;
01855 case AVMEDIA_TYPE_AUDIO:
01856 if (enc->sample_rate) {
01857 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01858 ", %d Hz", enc->sample_rate);
01859 }
01860 av_strlcat(buf, ", ", buf_size);
01861 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
01862 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
01863 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01864 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
01865 }
01866 break;
01867 default:
01868 return;
01869 }
01870 if (encode) {
01871 if (enc->flags & CODEC_FLAG_PASS1)
01872 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01873 ", pass 1");
01874 if (enc->flags & CODEC_FLAG_PASS2)
01875 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01876 ", pass 2");
01877 }
01878 bitrate = get_bit_rate(enc);
01879 if (bitrate != 0) {
01880 snprintf(buf + strlen(buf), buf_size - strlen(buf),
01881 ", %d kb/s", bitrate / 1000);
01882 }
01883 }
01884
01885 const char *av_get_profile_name(const AVCodec *codec, int profile)
01886 {
01887 const AVProfile *p;
01888 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
01889 return NULL;
01890
01891 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
01892 if (p->profile == profile)
01893 return p->name;
01894
01895 return NULL;
01896 }
01897
01898 unsigned avcodec_version( void )
01899 {
01900
01901 av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
01902 av_assert0(CODEC_ID_ADPCM_G722==69660);
01903
01904 av_assert0(CODEC_ID_SRT==94216);
01905 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
01906
01907 return LIBAVCODEC_VERSION_INT;
01908 }
01909
01910 const char *avcodec_configuration(void)
01911 {
01912 return FFMPEG_CONFIGURATION;
01913 }
01914
01915 const char *avcodec_license(void)
01916 {
01917 #define LICENSE_PREFIX "libavcodec license: "
01918 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
01919 }
01920
01921 void avcodec_flush_buffers(AVCodecContext *avctx)
01922 {
01923 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
01924 ff_thread_flush(avctx);
01925 else if(avctx->codec->flush)
01926 avctx->codec->flush(avctx);
01927
01928 avctx->pts_correction_last_pts =
01929 avctx->pts_correction_last_dts = INT64_MIN;
01930 }
01931
01932 static void video_free_buffers(AVCodecContext *s)
01933 {
01934 AVCodecInternal *avci = s->internal;
01935 int i, j;
01936
01937 if (!avci->buffer)
01938 return;
01939
01940 if (avci->buffer_count)
01941 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
01942 avci->buffer_count);
01943 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
01944 InternalBuffer *buf = &avci->buffer[i];
01945 for(j=0; j<4; j++){
01946 av_freep(&buf->base[j]);
01947 buf->data[j]= NULL;
01948 }
01949 }
01950 av_freep(&avci->buffer);
01951
01952 avci->buffer_count=0;
01953 }
01954
01955 static void audio_free_buffers(AVCodecContext *avctx)
01956 {
01957 AVCodecInternal *avci = avctx->internal;
01958 InternalBuffer *buf;
01959
01960 if (!avci->buffer)
01961 return;
01962 buf = avci->buffer;
01963
01964 if (buf->extended_data) {
01965 av_free(buf->extended_data[0]);
01966 if (buf->extended_data != buf->data)
01967 av_freep(&buf->extended_data);
01968 }
01969 av_freep(&avci->buffer);
01970 }
01971
01972 void avcodec_default_free_buffers(AVCodecContext *avctx)
01973 {
01974 switch (avctx->codec_type) {
01975 case AVMEDIA_TYPE_VIDEO:
01976 video_free_buffers(avctx);
01977 break;
01978 case AVMEDIA_TYPE_AUDIO:
01979 audio_free_buffers(avctx);
01980 break;
01981 default:
01982 break;
01983 }
01984 }
01985
01986 int av_get_exact_bits_per_sample(enum CodecID codec_id)
01987 {
01988 switch(codec_id){
01989 case CODEC_ID_ADPCM_CT:
01990 case CODEC_ID_ADPCM_IMA_APC:
01991 case CODEC_ID_ADPCM_IMA_EA_SEAD:
01992 case CODEC_ID_ADPCM_IMA_WS:
01993 case CODEC_ID_ADPCM_G722:
01994 case CODEC_ID_ADPCM_YAMAHA:
01995 return 4;
01996 case CODEC_ID_PCM_ALAW:
01997 case CODEC_ID_PCM_MULAW:
01998 case CODEC_ID_PCM_S8:
01999 case CODEC_ID_PCM_U8:
02000 case CODEC_ID_PCM_ZORK:
02001 return 8;
02002 case CODEC_ID_PCM_S16BE:
02003 case CODEC_ID_PCM_S16LE:
02004 case CODEC_ID_PCM_S16LE_PLANAR:
02005 case CODEC_ID_PCM_U16BE:
02006 case CODEC_ID_PCM_U16LE:
02007 return 16;
02008 case CODEC_ID_PCM_S24DAUD:
02009 case CODEC_ID_PCM_S24BE:
02010 case CODEC_ID_PCM_S24LE:
02011 case CODEC_ID_PCM_U24BE:
02012 case CODEC_ID_PCM_U24LE:
02013 return 24;
02014 case CODEC_ID_PCM_S32BE:
02015 case CODEC_ID_PCM_S32LE:
02016 case CODEC_ID_PCM_U32BE:
02017 case CODEC_ID_PCM_U32LE:
02018 case CODEC_ID_PCM_F32BE:
02019 case CODEC_ID_PCM_F32LE:
02020 return 32;
02021 case CODEC_ID_PCM_F64BE:
02022 case CODEC_ID_PCM_F64LE:
02023 return 64;
02024 default:
02025 return 0;
02026 }
02027 }
02028
02029 enum CodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
02030 {
02031 static const enum CodecID map[AV_SAMPLE_FMT_NB][2] = {
02032 [AV_SAMPLE_FMT_U8 ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
02033 [AV_SAMPLE_FMT_S16 ] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
02034 [AV_SAMPLE_FMT_S32 ] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
02035 [AV_SAMPLE_FMT_FLT ] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
02036 [AV_SAMPLE_FMT_DBL ] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
02037 [AV_SAMPLE_FMT_U8P ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
02038 [AV_SAMPLE_FMT_S16P] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
02039 [AV_SAMPLE_FMT_S32P] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
02040 [AV_SAMPLE_FMT_FLTP] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
02041 [AV_SAMPLE_FMT_DBLP] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
02042 };
02043 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
02044 return CODEC_ID_NONE;
02045 if (be < 0 || be > 1)
02046 be = AV_NE(1, 0);
02047 return map[fmt][be];
02048 }
02049
02050 int av_get_bits_per_sample(enum CodecID codec_id)
02051 {
02052 switch (codec_id) {
02053 case CODEC_ID_ADPCM_SBPRO_2:
02054 return 2;
02055 case CODEC_ID_ADPCM_SBPRO_3:
02056 return 3;
02057 case CODEC_ID_ADPCM_SBPRO_4:
02058 case CODEC_ID_ADPCM_IMA_WAV:
02059 case CODEC_ID_ADPCM_IMA_QT:
02060 case CODEC_ID_ADPCM_SWF:
02061 case CODEC_ID_ADPCM_MS:
02062 return 4;
02063 default:
02064 return av_get_exact_bits_per_sample(codec_id);
02065 }
02066 }
02067
02068 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
02069 {
02070 int id, sr, ch, ba, tag, bps;
02071
02072 id = avctx->codec_id;
02073 sr = avctx->sample_rate;
02074 ch = avctx->channels;
02075 ba = avctx->block_align;
02076 tag = avctx->codec_tag;
02077 bps = av_get_exact_bits_per_sample(avctx->codec_id);
02078
02079
02080 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
02081 return (frame_bytes * 8LL) / (bps * ch);
02082 bps = avctx->bits_per_coded_sample;
02083
02084
02085 switch (id) {
02086 case CODEC_ID_ADPCM_ADX: return 32;
02087 case CODEC_ID_ADPCM_IMA_QT: return 64;
02088 case CODEC_ID_ADPCM_EA_XAS: return 128;
02089 case CODEC_ID_AMR_NB:
02090 case CODEC_ID_GSM:
02091 case CODEC_ID_QCELP:
02092 case CODEC_ID_RA_144:
02093 case CODEC_ID_RA_288: return 160;
02094 case CODEC_ID_IMC: return 256;
02095 case CODEC_ID_AMR_WB:
02096 case CODEC_ID_GSM_MS: return 320;
02097 case CODEC_ID_MP1: return 384;
02098 case CODEC_ID_ATRAC1: return 512;
02099 case CODEC_ID_ATRAC3: return 1024;
02100 case CODEC_ID_MP2:
02101 case CODEC_ID_MUSEPACK7: return 1152;
02102 case CODEC_ID_AC3: return 1536;
02103 }
02104
02105 if (sr > 0) {
02106
02107 if (id == CODEC_ID_TTA)
02108 return 256 * sr / 245;
02109
02110 if (ch > 0) {
02111
02112 if (id == CODEC_ID_BINKAUDIO_DCT)
02113 return (480 << (sr / 22050)) / ch;
02114 }
02115 }
02116
02117 if (ba > 0) {
02118
02119 if (id == CODEC_ID_SIPR) {
02120 switch (ba) {
02121 case 20: return 160;
02122 case 19: return 144;
02123 case 29: return 288;
02124 case 37: return 480;
02125 }
02126 }
02127 }
02128
02129 if (frame_bytes > 0) {
02130
02131 if (id == CODEC_ID_TRUESPEECH)
02132 return 240 * (frame_bytes / 32);
02133 if (id == CODEC_ID_NELLYMOSER)
02134 return 256 * (frame_bytes / 64);
02135
02136 if (bps > 0) {
02137
02138 if (id == CODEC_ID_ADPCM_G726)
02139 return frame_bytes * 8 / bps;
02140 }
02141
02142 if (ch > 0) {
02143
02144 switch (id) {
02145 case CODEC_ID_ADPCM_4XM:
02146 case CODEC_ID_ADPCM_IMA_ISS:
02147 return (frame_bytes - 4 * ch) * 2 / ch;
02148 case CODEC_ID_ADPCM_IMA_SMJPEG:
02149 return (frame_bytes - 4) * 2 / ch;
02150 case CODEC_ID_ADPCM_IMA_AMV:
02151 return (frame_bytes - 8) * 2 / ch;
02152 case CODEC_ID_ADPCM_XA:
02153 return (frame_bytes / 128) * 224 / ch;
02154 case CODEC_ID_INTERPLAY_DPCM:
02155 return (frame_bytes - 6 - ch) / ch;
02156 case CODEC_ID_ROQ_DPCM:
02157 return (frame_bytes - 8) / ch;
02158 case CODEC_ID_XAN_DPCM:
02159 return (frame_bytes - 2 * ch) / ch;
02160 case CODEC_ID_MACE3:
02161 return 3 * frame_bytes / ch;
02162 case CODEC_ID_MACE6:
02163 return 6 * frame_bytes / ch;
02164 case CODEC_ID_PCM_LXF:
02165 return 2 * (frame_bytes / (5 * ch));
02166 }
02167
02168 if (tag) {
02169
02170 if (id == CODEC_ID_SOL_DPCM) {
02171 if (tag == 3)
02172 return frame_bytes / ch;
02173 else
02174 return frame_bytes * 2 / ch;
02175 }
02176 }
02177
02178 if (ba > 0) {
02179
02180 int blocks = frame_bytes / ba;
02181 switch (avctx->codec_id) {
02182 case CODEC_ID_ADPCM_IMA_WAV:
02183 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
02184 case CODEC_ID_ADPCM_IMA_DK3:
02185 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
02186 case CODEC_ID_ADPCM_IMA_DK4:
02187 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
02188 case CODEC_ID_ADPCM_MS:
02189 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
02190 }
02191 }
02192
02193 if (bps > 0) {
02194
02195 switch (avctx->codec_id) {
02196 case CODEC_ID_PCM_DVD:
02197 if(bps<4)
02198 return 0;
02199 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
02200 case CODEC_ID_PCM_BLURAY:
02201 if(bps<4)
02202 return 0;
02203 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
02204 case CODEC_ID_S302M:
02205 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
02206 }
02207 }
02208 }
02209 }
02210
02211 return 0;
02212 }
02213
02214 #if !HAVE_THREADS
02215 int ff_thread_init(AVCodecContext *s){
02216 return -1;
02217 }
02218 #endif
02219
02220 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
02221 {
02222 unsigned int n = 0;
02223
02224 while(v >= 0xff) {
02225 *s++ = 0xff;
02226 v -= 0xff;
02227 n++;
02228 }
02229 *s = v;
02230 n++;
02231 return n;
02232 }
02233
02234 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
02235 int i;
02236 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
02237 return i;
02238 }
02239
02240 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
02241 {
02242 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
02243 "version to the newest one from Git. If the problem still "
02244 "occurs, it means that your file has a feature which has not "
02245 "been implemented.\n", feature);
02246 if(want_sample)
02247 av_log_ask_for_sample(avc, NULL);
02248 }
02249
02250 void av_log_ask_for_sample(void *avc, const char *msg, ...)
02251 {
02252 va_list argument_list;
02253
02254 va_start(argument_list, msg);
02255
02256 if (msg)
02257 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
02258 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
02259 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
02260 "and contact the ffmpeg-devel mailing list.\n");
02261
02262 va_end(argument_list);
02263 }
02264
02265 static AVHWAccel *first_hwaccel = NULL;
02266
02267 void av_register_hwaccel(AVHWAccel *hwaccel)
02268 {
02269 AVHWAccel **p = &first_hwaccel;
02270 while (*p)
02271 p = &(*p)->next;
02272 *p = hwaccel;
02273 hwaccel->next = NULL;
02274 }
02275
02276 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
02277 {
02278 return hwaccel ? hwaccel->next : first_hwaccel;
02279 }
02280
02281 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
02282 {
02283 AVHWAccel *hwaccel=NULL;
02284
02285 while((hwaccel= av_hwaccel_next(hwaccel))){
02286 if ( hwaccel->id == codec_id
02287 && hwaccel->pix_fmt == pix_fmt)
02288 return hwaccel;
02289 }
02290 return NULL;
02291 }
02292
02293 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
02294 {
02295 if (ff_lockmgr_cb) {
02296 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
02297 return -1;
02298 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
02299 return -1;
02300 }
02301
02302 ff_lockmgr_cb = cb;
02303
02304 if (ff_lockmgr_cb) {
02305 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
02306 return -1;
02307 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
02308 return -1;
02309 }
02310 return 0;
02311 }
02312
02313 int avpriv_lock_avformat(void)
02314 {
02315 if (ff_lockmgr_cb) {
02316 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
02317 return -1;
02318 }
02319 return 0;
02320 }
02321
02322 int avpriv_unlock_avformat(void)
02323 {
02324 if (ff_lockmgr_cb) {
02325 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
02326 return -1;
02327 }
02328 return 0;
02329 }
02330
02331 unsigned int avpriv_toupper4(unsigned int x)
02332 {
02333 return toupper( x &0xFF)
02334 + (toupper((x>>8 )&0xFF)<<8 )
02335 + (toupper((x>>16)&0xFF)<<16)
02336 + (toupper((x>>24)&0xFF)<<24);
02337 }
02338
02339 #if !HAVE_THREADS
02340
02341 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
02342 {
02343 f->owner = avctx;
02344
02345 ff_init_buffer_info(avctx, f);
02346
02347 return avctx->get_buffer(avctx, f);
02348 }
02349
02350 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
02351 {
02352 f->owner->release_buffer(f->owner, f);
02353 }
02354
02355 void ff_thread_finish_setup(AVCodecContext *avctx)
02356 {
02357 }
02358
02359 void ff_thread_report_progress(AVFrame *f, int progress, int field)
02360 {
02361 }
02362
02363 void ff_thread_await_progress(AVFrame *f, int progress, int field)
02364 {
02365 }
02366
02367 int ff_thread_can_start_frame(AVCodecContext *avctx)
02368 {
02369 return 1;
02370 }
02371
02372 #endif
02373
02374 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
02375 {
02376 AVCodec *c= avcodec_find_decoder(codec_id);
02377 if(!c)
02378 c= avcodec_find_encoder(codec_id);
02379 if(c)
02380 return c->type;
02381
02382 if (codec_id <= CODEC_ID_NONE)
02383 return AVMEDIA_TYPE_UNKNOWN;
02384 else if (codec_id < CODEC_ID_FIRST_AUDIO)
02385 return AVMEDIA_TYPE_VIDEO;
02386 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
02387 return AVMEDIA_TYPE_AUDIO;
02388 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
02389 return AVMEDIA_TYPE_SUBTITLE;
02390
02391 return AVMEDIA_TYPE_UNKNOWN;
02392 }
02393
02394 int avcodec_is_open(AVCodecContext *s)
02395 {
02396 return !!s->internal;
02397 }