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/channel_layout.h"
00031 #include "libavutil/crc.h"
00032 #include "libavutil/mathematics.h"
00033 #include "libavutil/pixdesc.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 "thread.h"
00042 #include "frame_thread_encoder.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 volatile int ff_avcodec_locked;
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
00065
00066
00067 if (!ptr)
00068 min_size = 0;
00069
00070 *size = min_size;
00071
00072 return ptr;
00073 }
00074
00075 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
00076 {
00077 void **p = ptr;
00078 if (min_size < *size)
00079 return 0;
00080 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
00081 av_free(*p);
00082 *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
00083 if (!*p)
00084 min_size = 0;
00085 *size = min_size;
00086 return 1;
00087 }
00088
00089 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
00090 {
00091 ff_fast_malloc(ptr, size, min_size, 0);
00092 }
00093
00094 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
00095 {
00096 uint8_t **p = ptr;
00097 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
00098 av_freep(p);
00099 *size = 0;
00100 return;
00101 }
00102 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
00103 memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00104 }
00105
00106 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
00107 {
00108 uint8_t **p = ptr;
00109 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
00110 av_freep(p);
00111 *size = 0;
00112 return;
00113 }
00114 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
00115 memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
00116 }
00117
00118
00119 static AVCodec *first_avcodec = NULL;
00120
00121 AVCodec *av_codec_next(const AVCodec *c)
00122 {
00123 if (c)
00124 return c->next;
00125 else
00126 return first_avcodec;
00127 }
00128
00129 static void avcodec_init(void)
00130 {
00131 static int initialized = 0;
00132
00133 if (initialized != 0)
00134 return;
00135 initialized = 1;
00136
00137 ff_dsputil_static_init();
00138 }
00139
00140 int av_codec_is_encoder(const AVCodec *codec)
00141 {
00142 return codec && (codec->encode_sub || codec->encode2);
00143 }
00144
00145 int av_codec_is_decoder(const AVCodec *codec)
00146 {
00147 return codec && codec->decode;
00148 }
00149
00150 void avcodec_register(AVCodec *codec)
00151 {
00152 AVCodec **p;
00153 avcodec_init();
00154 p = &first_avcodec;
00155 while (*p != NULL)
00156 p = &(*p)->next;
00157 *p = codec;
00158 codec->next = NULL;
00159
00160 if (codec->init_static_data)
00161 codec->init_static_data(codec);
00162 }
00163
00164 unsigned avcodec_get_edge_width(void)
00165 {
00166 return EDGE_WIDTH;
00167 }
00168
00169 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
00170 {
00171 s->coded_width = width;
00172 s->coded_height = height;
00173 s->width = -((-width ) >> s->lowres);
00174 s->height = -((-height) >> s->lowres);
00175 }
00176
00177 #define INTERNAL_BUFFER_SIZE (32 + 1)
00178
00179 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
00180 int linesize_align[AV_NUM_DATA_POINTERS])
00181 {
00182 int i;
00183 int w_align = 1;
00184 int h_align = 1;
00185
00186 switch (s->pix_fmt) {
00187 case AV_PIX_FMT_YUV420P:
00188 case AV_PIX_FMT_YUYV422:
00189 case AV_PIX_FMT_UYVY422:
00190 case AV_PIX_FMT_YUV422P:
00191 case AV_PIX_FMT_YUV440P:
00192 case AV_PIX_FMT_YUV444P:
00193 case AV_PIX_FMT_GBRP:
00194 case AV_PIX_FMT_GRAY8:
00195 case AV_PIX_FMT_GRAY16BE:
00196 case AV_PIX_FMT_GRAY16LE:
00197 case AV_PIX_FMT_YUVJ420P:
00198 case AV_PIX_FMT_YUVJ422P:
00199 case AV_PIX_FMT_YUVJ440P:
00200 case AV_PIX_FMT_YUVJ444P:
00201 case AV_PIX_FMT_YUVA420P:
00202 case AV_PIX_FMT_YUVA422P:
00203 case AV_PIX_FMT_YUVA444P:
00204 case AV_PIX_FMT_YUV420P9LE:
00205 case AV_PIX_FMT_YUV420P9BE:
00206 case AV_PIX_FMT_YUV420P10LE:
00207 case AV_PIX_FMT_YUV420P10BE:
00208 case AV_PIX_FMT_YUV420P12LE:
00209 case AV_PIX_FMT_YUV420P12BE:
00210 case AV_PIX_FMT_YUV420P14LE:
00211 case AV_PIX_FMT_YUV420P14BE:
00212 case AV_PIX_FMT_YUV422P9LE:
00213 case AV_PIX_FMT_YUV422P9BE:
00214 case AV_PIX_FMT_YUV422P10LE:
00215 case AV_PIX_FMT_YUV422P10BE:
00216 case AV_PIX_FMT_YUV422P12LE:
00217 case AV_PIX_FMT_YUV422P12BE:
00218 case AV_PIX_FMT_YUV422P14LE:
00219 case AV_PIX_FMT_YUV422P14BE:
00220 case AV_PIX_FMT_YUV444P9LE:
00221 case AV_PIX_FMT_YUV444P9BE:
00222 case AV_PIX_FMT_YUV444P10LE:
00223 case AV_PIX_FMT_YUV444P10BE:
00224 case AV_PIX_FMT_YUV444P12LE:
00225 case AV_PIX_FMT_YUV444P12BE:
00226 case AV_PIX_FMT_YUV444P14LE:
00227 case AV_PIX_FMT_YUV444P14BE:
00228 case AV_PIX_FMT_GBRP9LE:
00229 case AV_PIX_FMT_GBRP9BE:
00230 case AV_PIX_FMT_GBRP10LE:
00231 case AV_PIX_FMT_GBRP10BE:
00232 case AV_PIX_FMT_GBRP12LE:
00233 case AV_PIX_FMT_GBRP12BE:
00234 case AV_PIX_FMT_GBRP14LE:
00235 case AV_PIX_FMT_GBRP14BE:
00236 w_align = 16;
00237 h_align = 16 * 2;
00238 break;
00239 case AV_PIX_FMT_YUV411P:
00240 case AV_PIX_FMT_UYYVYY411:
00241 w_align = 32;
00242 h_align = 8;
00243 break;
00244 case AV_PIX_FMT_YUV410P:
00245 if (s->codec_id == AV_CODEC_ID_SVQ1) {
00246 w_align = 64;
00247 h_align = 64;
00248 }
00249 break;
00250 case AV_PIX_FMT_RGB555:
00251 if (s->codec_id == AV_CODEC_ID_RPZA) {
00252 w_align = 4;
00253 h_align = 4;
00254 }
00255 break;
00256 case AV_PIX_FMT_PAL8:
00257 case AV_PIX_FMT_BGR8:
00258 case AV_PIX_FMT_RGB8:
00259 if (s->codec_id == AV_CODEC_ID_SMC) {
00260 w_align = 4;
00261 h_align = 4;
00262 }
00263 break;
00264 case AV_PIX_FMT_BGR24:
00265 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
00266 (s->codec_id == AV_CODEC_ID_ZLIB)) {
00267 w_align = 4;
00268 h_align = 4;
00269 }
00270 break;
00271 default:
00272 w_align = 1;
00273 h_align = 1;
00274 break;
00275 }
00276
00277 if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
00278 w_align = FFMAX(w_align, 8);
00279 }
00280
00281 *width = FFALIGN(*width, w_align);
00282 *height = FFALIGN(*height, h_align);
00283 if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
00284
00285
00286 *height += 2;
00287
00288 for (i = 0; i < 4; i++)
00289 linesize_align[i] = STRIDE_ALIGN;
00290 }
00291
00292 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
00293 {
00294 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
00295 int chroma_shift = desc->log2_chroma_w;
00296 int linesize_align[AV_NUM_DATA_POINTERS];
00297 int align;
00298
00299 avcodec_align_dimensions2(s, width, height, linesize_align);
00300 align = FFMAX(linesize_align[0], linesize_align[3]);
00301 linesize_align[1] <<= chroma_shift;
00302 linesize_align[2] <<= chroma_shift;
00303 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
00304 *width = FFALIGN(*width, align);
00305 }
00306
00307 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
00308 enum AVSampleFormat sample_fmt, const uint8_t *buf,
00309 int buf_size, int align)
00310 {
00311 int ch, planar, needed_size, ret = 0;
00312
00313 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
00314 frame->nb_samples, sample_fmt,
00315 align);
00316 if (buf_size < needed_size)
00317 return AVERROR(EINVAL);
00318
00319 planar = av_sample_fmt_is_planar(sample_fmt);
00320 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
00321 if (!(frame->extended_data = av_mallocz(nb_channels *
00322 sizeof(*frame->extended_data))))
00323 return AVERROR(ENOMEM);
00324 } else {
00325 frame->extended_data = frame->data;
00326 }
00327
00328 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
00329 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
00330 sample_fmt, align)) < 0) {
00331 if (frame->extended_data != frame->data)
00332 av_freep(&frame->extended_data);
00333 return ret;
00334 }
00335 if (frame->extended_data != frame->data) {
00336 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
00337 frame->data[ch] = frame->extended_data[ch];
00338 }
00339
00340 return ret;
00341 }
00342
00343 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
00344 {
00345 AVCodecInternal *avci = avctx->internal;
00346 int buf_size, ret;
00347
00348 av_freep(&avci->audio_data);
00349 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
00350 frame->nb_samples, avctx->sample_fmt,
00351 0);
00352 if (buf_size < 0)
00353 return AVERROR(EINVAL);
00354
00355 frame->data[0] = av_mallocz(buf_size);
00356 if (!frame->data[0])
00357 return AVERROR(ENOMEM);
00358
00359 ret = avcodec_fill_audio_frame(frame, avctx->channels, avctx->sample_fmt,
00360 frame->data[0], buf_size, 0);
00361 if (ret < 0) {
00362 av_freep(&frame->data[0]);
00363 return ret;
00364 }
00365
00366 avci->audio_data = frame->data[0];
00367 if (avctx->debug & FF_DEBUG_BUFFERS)
00368 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
00369 "internal audio buffer used\n", frame);
00370
00371 return 0;
00372 }
00373
00374 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
00375 {
00376 int i;
00377 int w = s->width;
00378 int h = s->height;
00379 InternalBuffer *buf;
00380 AVCodecInternal *avci = s->internal;
00381
00382 if (pic->data[0] != NULL) {
00383 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
00384 return -1;
00385 }
00386 if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
00387 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
00388 return -1;
00389 }
00390
00391 if (av_image_check_size(w, h, 0, s) || s->pix_fmt<0) {
00392 av_log(s, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
00393 return -1;
00394 }
00395
00396 if (!avci->buffer) {
00397 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
00398 sizeof(InternalBuffer));
00399 }
00400
00401 buf = &avci->buffer[avci->buffer_count];
00402
00403 if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
00404 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
00405 av_freep(&buf->base[i]);
00406 buf->data[i] = NULL;
00407 }
00408 }
00409
00410 if (!buf->base[0]) {
00411 int h_chroma_shift, v_chroma_shift;
00412 int size[4] = { 0 };
00413 int tmpsize;
00414 int unaligned;
00415 AVPicture picture;
00416 int stride_align[AV_NUM_DATA_POINTERS];
00417 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
00418 const int pixel_size = desc->comp[0].step_minus1 + 1;
00419
00420 av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift,
00421 &v_chroma_shift);
00422
00423 avcodec_align_dimensions2(s, &w, &h, stride_align);
00424
00425 if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
00426 w += EDGE_WIDTH * 2;
00427 h += EDGE_WIDTH * 2;
00428 }
00429
00430 do {
00431
00432
00433 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
00434
00435 w += w & ~(w - 1);
00436
00437 unaligned = 0;
00438 for (i = 0; i < 4; i++)
00439 unaligned |= picture.linesize[i] % stride_align[i];
00440 } while (unaligned);
00441
00442 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
00443 if (tmpsize < 0)
00444 return -1;
00445
00446 for (i = 0; i < 3 && picture.data[i + 1]; i++)
00447 size[i] = picture.data[i + 1] - picture.data[i];
00448 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
00449
00450 memset(buf->base, 0, sizeof(buf->base));
00451 memset(buf->data, 0, sizeof(buf->data));
00452
00453 for (i = 0; i < 4 && size[i]; i++) {
00454 const int h_shift = i == 0 ? 0 : h_chroma_shift;
00455 const int v_shift = i == 0 ? 0 : v_chroma_shift;
00456
00457 buf->linesize[i] = picture.linesize[i];
00458
00459 buf->base[i] = av_malloc(size[i] + 16);
00460 if (buf->base[i] == NULL)
00461 return AVERROR(ENOMEM);
00462 memset(buf->base[i], 128, size[i]);
00463
00464
00465 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
00466 buf->data[i] = buf->base[i];
00467 else
00468 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
00469 }
00470 for (; i < AV_NUM_DATA_POINTERS; i++) {
00471 buf->base[i] = buf->data[i] = NULL;
00472 buf->linesize[i] = 0;
00473 }
00474 if (size[1] && !size[2])
00475 avpriv_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
00476 buf->width = s->width;
00477 buf->height = s->height;
00478 buf->pix_fmt = s->pix_fmt;
00479 }
00480
00481 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
00482 pic->base[i] = buf->base[i];
00483 pic->data[i] = buf->data[i];
00484 pic->linesize[i] = buf->linesize[i];
00485 }
00486 pic->extended_data = pic->data;
00487 avci->buffer_count++;
00488
00489 if (s->debug & FF_DEBUG_BUFFERS)
00490 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
00491 "buffers used\n", pic, avci->buffer_count);
00492
00493 return 0;
00494 }
00495
00496 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
00497 {
00498 frame->type = FF_BUFFER_TYPE_INTERNAL;
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 ff_init_buffer_info(AVCodecContext *s, AVFrame *frame)
00510 {
00511 if (s->pkt) {
00512 frame->pkt_pts = s->pkt->pts;
00513 frame->pkt_pos = s->pkt->pos;
00514 frame->pkt_duration = s->pkt->duration;
00515 frame->pkt_size = s->pkt->size;
00516 } else {
00517 frame->pkt_pts = AV_NOPTS_VALUE;
00518 frame->pkt_pos = -1;
00519 frame->pkt_duration = 0;
00520 frame->pkt_size = -1;
00521 }
00522 frame->reordered_opaque = s->reordered_opaque;
00523
00524 switch (s->codec->type) {
00525 case AVMEDIA_TYPE_VIDEO:
00526 frame->width = s->width;
00527 frame->height = s->height;
00528 frame->format = s->pix_fmt;
00529 frame->sample_aspect_ratio = s->sample_aspect_ratio;
00530 break;
00531 case AVMEDIA_TYPE_AUDIO:
00532 frame->sample_rate = s->sample_rate;
00533 frame->format = s->sample_fmt;
00534 frame->channel_layout = s->channel_layout;
00535 frame->channels = s->channels;
00536 break;
00537 }
00538 }
00539
00540 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
00541 {
00542 ff_init_buffer_info(avctx, frame);
00543
00544 return avctx->get_buffer(avctx, frame);
00545 }
00546
00547 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
00548 {
00549 int i;
00550 InternalBuffer *buf, *last;
00551 AVCodecInternal *avci = s->internal;
00552
00553 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
00554
00555 assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
00556 assert(avci->buffer_count);
00557
00558 if (avci->buffer) {
00559 buf = NULL;
00560 for (i = 0; i < avci->buffer_count; i++) {
00561 buf = &avci->buffer[i];
00562 if (buf->data[0] == pic->data[0])
00563 break;
00564 }
00565 av_assert0(i < avci->buffer_count);
00566 avci->buffer_count--;
00567 last = &avci->buffer[avci->buffer_count];
00568
00569 if (buf != last)
00570 FFSWAP(InternalBuffer, *buf, *last);
00571 }
00572
00573 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
00574 pic->data[i] = NULL;
00575
00576
00577 if (s->debug & FF_DEBUG_BUFFERS)
00578 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
00579 "buffers used\n", pic, avci->buffer_count);
00580 }
00581
00582 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
00583 {
00584 AVFrame temp_pic;
00585 int i, ret;
00586
00587 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
00588
00589 if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
00590 av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
00591 pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
00592 s->release_buffer(s, pic);
00593 }
00594
00595 ff_init_buffer_info(s, pic);
00596
00597
00598 if (pic->data[0] == NULL) {
00599
00600 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
00601 return ff_get_buffer(s, pic);
00602 }
00603
00604 assert(s->pix_fmt == pic->format);
00605
00606
00607 if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
00608 return 0;
00609 }
00610
00611
00612
00613
00614 temp_pic = *pic;
00615 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
00616 pic->data[i] = pic->base[i] = NULL;
00617 pic->opaque = NULL;
00618
00619 if ((ret = ff_get_buffer(s, pic)))
00620 return ret;
00621
00622 av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
00623 s->height);
00624 s->release_buffer(s, &temp_pic);
00625 return 0;
00626 }
00627
00628 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
00629 {
00630 int i;
00631
00632 for (i = 0; i < count; i++) {
00633 int r = func(c, (char *)arg + i * size);
00634 if (ret)
00635 ret[i] = r;
00636 }
00637 return 0;
00638 }
00639
00640 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
00641 {
00642 int i;
00643
00644 for (i = 0; i < count; i++) {
00645 int r = func(c, arg, i, 0);
00646 if (ret)
00647 ret[i] = r;
00648 }
00649 return 0;
00650 }
00651
00652 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
00653 {
00654 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
00655 return desc->flags & PIX_FMT_HWACCEL;
00656 }
00657
00658 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
00659 {
00660 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
00661 ++fmt;
00662 return fmt[0];
00663 }
00664
00665 void avcodec_get_frame_defaults(AVFrame *frame)
00666 {
00667 #if LIBAVCODEC_VERSION_MAJOR >= 55
00668
00669
00670 if (frame->extended_data != frame->data && 0)
00671 av_freep(&frame->extended_data);
00672 #endif
00673
00674 memset(frame, 0, sizeof(AVFrame));
00675
00676 frame->pts =
00677 frame->pkt_dts =
00678 frame->pkt_pts =
00679 frame->best_effort_timestamp = AV_NOPTS_VALUE;
00680 frame->pkt_duration = 0;
00681 frame->pkt_pos = -1;
00682 frame->pkt_size = -1;
00683 frame->key_frame = 1;
00684 frame->sample_aspect_ratio = (AVRational) {0, 1 };
00685 frame->format = -1;
00686 frame->extended_data = frame->data;
00687 }
00688
00689 AVFrame *avcodec_alloc_frame(void)
00690 {
00691 AVFrame *frame = av_malloc(sizeof(AVFrame));
00692
00693 if (frame == NULL)
00694 return NULL;
00695
00696 frame->extended_data = NULL;
00697 avcodec_get_frame_defaults(frame);
00698
00699 return frame;
00700 }
00701
00702 void avcodec_free_frame(AVFrame **frame)
00703 {
00704 AVFrame *f;
00705
00706 if (!frame || !*frame)
00707 return;
00708
00709 f = *frame;
00710
00711 if (f->extended_data != f->data)
00712 av_freep(&f->extended_data);
00713
00714 av_freep(frame);
00715 }
00716
00717 #define MAKE_ACCESSORS(str, name, type, field) \
00718 type av_##name##_get_##field(const str *s) { return s->field; } \
00719 void av_##name##_set_##field(str *s, type v) { s->field = v; }
00720
00721 MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
00722 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
00723 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
00724 MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
00725 MAKE_ACCESSORS(AVFrame, frame, int, channels)
00726 MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
00727 MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
00728 MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
00729 MAKE_ACCESSORS(AVFrame, frame, int, pkt_size)
00730
00731 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
00732 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
00733
00734 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
00735 {
00736 memset(sub, 0, sizeof(*sub));
00737 sub->pts = AV_NOPTS_VALUE;
00738 }
00739
00740 static int get_bit_rate(AVCodecContext *ctx)
00741 {
00742 int bit_rate;
00743 int bits_per_sample;
00744
00745 switch (ctx->codec_type) {
00746 case AVMEDIA_TYPE_VIDEO:
00747 case AVMEDIA_TYPE_DATA:
00748 case AVMEDIA_TYPE_SUBTITLE:
00749 case AVMEDIA_TYPE_ATTACHMENT:
00750 bit_rate = ctx->bit_rate;
00751 break;
00752 case AVMEDIA_TYPE_AUDIO:
00753 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
00754 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
00755 break;
00756 default:
00757 bit_rate = 0;
00758 break;
00759 }
00760 return bit_rate;
00761 }
00762
00763 #if FF_API_AVCODEC_OPEN
00764 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
00765 {
00766 return avcodec_open2(avctx, codec, NULL);
00767 }
00768 #endif
00769
00770 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
00771 {
00772 int ret = 0;
00773
00774 ff_unlock_avcodec();
00775
00776 ret = avcodec_open2(avctx, codec, options);
00777
00778 ff_lock_avcodec(avctx);
00779 return ret;
00780 }
00781
00782 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
00783 {
00784 int ret = 0;
00785 AVDictionary *tmp = NULL;
00786
00787 if (avcodec_is_open(avctx))
00788 return 0;
00789
00790 if ((!codec && !avctx->codec)) {
00791 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
00792 return AVERROR(EINVAL);
00793 }
00794 if ((codec && avctx->codec && codec != avctx->codec)) {
00795 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
00796 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
00797 return AVERROR(EINVAL);
00798 }
00799 if (!codec)
00800 codec = avctx->codec;
00801
00802 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
00803 return AVERROR(EINVAL);
00804
00805 if (options)
00806 av_dict_copy(&tmp, *options, 0);
00807
00808 ret = ff_lock_avcodec(avctx);
00809 if (ret < 0)
00810 return ret;
00811
00812 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
00813 if (!avctx->internal) {
00814 ret = AVERROR(ENOMEM);
00815 goto end;
00816 }
00817
00818 if (codec->priv_data_size > 0) {
00819 if (!avctx->priv_data) {
00820 avctx->priv_data = av_mallocz(codec->priv_data_size);
00821 if (!avctx->priv_data) {
00822 ret = AVERROR(ENOMEM);
00823 goto end;
00824 }
00825 if (codec->priv_class) {
00826 *(const AVClass **)avctx->priv_data = codec->priv_class;
00827 av_opt_set_defaults(avctx->priv_data);
00828 }
00829 }
00830 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
00831 goto free_and_end;
00832 } else {
00833 avctx->priv_data = NULL;
00834 }
00835 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
00836 goto free_and_end;
00837
00838
00839 if (!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == AV_CODEC_ID_H264)){
00840
00841 if (avctx->coded_width && avctx->coded_height)
00842 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
00843 else if (avctx->width && avctx->height)
00844 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
00845 }
00846
00847 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
00848 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
00849 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
00850 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
00851 avcodec_set_dimensions(avctx, 0, 0);
00852 }
00853
00854
00855
00856 if (av_codec_is_decoder(codec))
00857 av_freep(&avctx->subtitle_header);
00858
00859 if (avctx->channels > FF_SANE_NB_CHANNELS) {
00860 ret = AVERROR(EINVAL);
00861 goto free_and_end;
00862 }
00863
00864 avctx->codec = codec;
00865 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
00866 avctx->codec_id == AV_CODEC_ID_NONE) {
00867 avctx->codec_type = codec->type;
00868 avctx->codec_id = codec->id;
00869 }
00870 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
00871 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
00872 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
00873 ret = AVERROR(EINVAL);
00874 goto free_and_end;
00875 }
00876 avctx->frame_number = 0;
00877 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
00878
00879 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
00880 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
00881 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
00882 AVCodec *codec2;
00883 av_log(NULL, AV_LOG_ERROR,
00884 "The %s '%s' is experimental but experimental codecs are not enabled, "
00885 "add '-strict %d' if you want to use it.\n",
00886 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
00887 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
00888 if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
00889 av_log(NULL, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
00890 codec_string, codec2->name);
00891 ret = AVERROR_EXPERIMENTAL;
00892 goto free_and_end;
00893 }
00894
00895 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
00896 (!avctx->time_base.num || !avctx->time_base.den)) {
00897 avctx->time_base.num = 1;
00898 avctx->time_base.den = avctx->sample_rate;
00899 }
00900
00901 if (!HAVE_THREADS)
00902 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
00903
00904 if (HAVE_THREADS) {
00905 ff_unlock_avcodec();
00906 ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
00907 ff_lock_avcodec(avctx);
00908 if (ret < 0)
00909 goto free_and_end;
00910 }
00911
00912 if (HAVE_THREADS && !avctx->thread_opaque
00913 && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
00914 ret = ff_thread_init(avctx);
00915 if (ret < 0) {
00916 goto free_and_end;
00917 }
00918 }
00919 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
00920 avctx->thread_count = 1;
00921
00922 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
00923 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
00924 avctx->codec->max_lowres);
00925 ret = AVERROR(EINVAL);
00926 goto free_and_end;
00927 }
00928
00929 if (av_codec_is_encoder(avctx->codec)) {
00930 int i;
00931 if (avctx->codec->sample_fmts) {
00932 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
00933 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
00934 break;
00935 if (avctx->channels == 1 &&
00936 av_get_planar_sample_fmt(avctx->sample_fmt) ==
00937 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
00938 avctx->sample_fmt = avctx->codec->sample_fmts[i];
00939 break;
00940 }
00941 }
00942 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
00943 char buf[128];
00944 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
00945 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
00946 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
00947 ret = AVERROR(EINVAL);
00948 goto free_and_end;
00949 }
00950 }
00951 if (avctx->codec->pix_fmts) {
00952 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
00953 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
00954 break;
00955 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
00956 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
00957 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
00958 char buf[128];
00959 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
00960 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
00961 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
00962 ret = AVERROR(EINVAL);
00963 goto free_and_end;
00964 }
00965 }
00966 if (avctx->codec->supported_samplerates) {
00967 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
00968 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
00969 break;
00970 if (avctx->codec->supported_samplerates[i] == 0) {
00971 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
00972 avctx->sample_rate);
00973 ret = AVERROR(EINVAL);
00974 goto free_and_end;
00975 }
00976 }
00977 if (avctx->codec->channel_layouts) {
00978 if (!avctx->channel_layout) {
00979 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
00980 } else {
00981 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
00982 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
00983 break;
00984 if (avctx->codec->channel_layouts[i] == 0) {
00985 char buf[512];
00986 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
00987 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
00988 ret = AVERROR(EINVAL);
00989 goto free_and_end;
00990 }
00991 }
00992 }
00993 if (avctx->channel_layout && avctx->channels) {
00994 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
00995 if (channels != avctx->channels) {
00996 char buf[512];
00997 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
00998 av_log(avctx, AV_LOG_ERROR,
00999 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
01000 buf, channels, avctx->channels);
01001 ret = AVERROR(EINVAL);
01002 goto free_and_end;
01003 }
01004 } else if (avctx->channel_layout) {
01005 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
01006 }
01007 if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
01008 avctx->codec_id != AV_CODEC_ID_PNG
01009 ) {
01010 if (avctx->width <= 0 || avctx->height <= 0) {
01011 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
01012 ret = AVERROR(EINVAL);
01013 goto free_and_end;
01014 }
01015 }
01016 }
01017
01018 avctx->pts_correction_num_faulty_pts =
01019 avctx->pts_correction_num_faulty_dts = 0;
01020 avctx->pts_correction_last_pts =
01021 avctx->pts_correction_last_dts = INT64_MIN;
01022
01023 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
01024 || avctx->internal->frame_thread_encoder)) {
01025 ret = avctx->codec->init(avctx);
01026 if (ret < 0) {
01027 goto free_and_end;
01028 }
01029 }
01030
01031 ret=0;
01032
01033 if (av_codec_is_decoder(avctx->codec)) {
01034 if (!avctx->bit_rate)
01035 avctx->bit_rate = get_bit_rate(avctx);
01036
01037 if (avctx->channel_layout) {
01038 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
01039 if (!avctx->channels)
01040 avctx->channels = channels;
01041 else if (channels != avctx->channels) {
01042 char buf[512];
01043 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
01044 av_log(avctx, AV_LOG_WARNING,
01045 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
01046 "ignoring specified channel layout\n",
01047 buf, channels, avctx->channels);
01048 avctx->channel_layout = 0;
01049 }
01050 }
01051 if (avctx->channels && avctx->channels < 0 ||
01052 avctx->channels > FF_SANE_NB_CHANNELS) {
01053 ret = AVERROR(EINVAL);
01054 goto free_and_end;
01055 }
01056 }
01057 end:
01058 ff_unlock_avcodec();
01059 if (options) {
01060 av_dict_free(options);
01061 *options = tmp;
01062 }
01063
01064 return ret;
01065 free_and_end:
01066 av_dict_free(&tmp);
01067 av_freep(&avctx->priv_data);
01068 av_freep(&avctx->internal);
01069 avctx->codec = NULL;
01070 goto end;
01071 }
01072
01073 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
01074 {
01075 if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
01076 av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
01077 return AVERROR(EINVAL);
01078 }
01079
01080 if (avctx) {
01081 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
01082 if (!avpkt->data || avpkt->size < size) {
01083 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
01084 avpkt->data = avctx->internal->byte_buffer;
01085 avpkt->size = avctx->internal->byte_buffer_size;
01086 avpkt->destruct = NULL;
01087 }
01088 }
01089
01090 if (avpkt->data) {
01091 void *destruct = avpkt->destruct;
01092
01093 if (avpkt->size < size) {
01094 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
01095 return AVERROR(EINVAL);
01096 }
01097
01098 av_init_packet(avpkt);
01099 avpkt->destruct = destruct;
01100 avpkt->size = size;
01101 return 0;
01102 } else {
01103 int ret = av_new_packet(avpkt, size);
01104 if (ret < 0)
01105 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
01106 return ret;
01107 }
01108 }
01109
01110 int ff_alloc_packet(AVPacket *avpkt, int size)
01111 {
01112 return ff_alloc_packet2(NULL, avpkt, size);
01113 }
01114
01118 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
01119 {
01120 AVFrame *frame = NULL;
01121 uint8_t *buf = NULL;
01122 int ret;
01123
01124 if (!(frame = avcodec_alloc_frame()))
01125 return AVERROR(ENOMEM);
01126 *frame = *src;
01127
01128 if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
01129 s->frame_size, s->sample_fmt, 0)) < 0)
01130 goto fail;
01131
01132 if (!(buf = av_malloc(ret))) {
01133 ret = AVERROR(ENOMEM);
01134 goto fail;
01135 }
01136
01137 frame->nb_samples = s->frame_size;
01138 if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
01139 buf, ret, 0)) < 0)
01140 goto fail;
01141 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
01142 src->nb_samples, s->channels, s->sample_fmt)) < 0)
01143 goto fail;
01144 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
01145 frame->nb_samples - src->nb_samples,
01146 s->channels, s->sample_fmt)) < 0)
01147 goto fail;
01148
01149 *dst = frame;
01150
01151 return 0;
01152
01153 fail:
01154 if (frame->extended_data != frame->data)
01155 av_freep(&frame->extended_data);
01156 av_freep(&buf);
01157 av_freep(&frame);
01158 return ret;
01159 }
01160
01161 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
01162 AVPacket *avpkt,
01163 const AVFrame *frame,
01164 int *got_packet_ptr)
01165 {
01166 AVFrame tmp;
01167 AVFrame *padded_frame = NULL;
01168 int ret;
01169 AVPacket user_pkt = *avpkt;
01170 int needs_realloc = !user_pkt.data;
01171
01172 *got_packet_ptr = 0;
01173
01174 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
01175 av_free_packet(avpkt);
01176 av_init_packet(avpkt);
01177 return 0;
01178 }
01179
01180
01181 if (frame && !frame->extended_data) {
01182 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
01183 avctx->channels > AV_NUM_DATA_POINTERS) {
01184 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
01185 "with more than %d channels, but extended_data is not set.\n",
01186 AV_NUM_DATA_POINTERS);
01187 return AVERROR(EINVAL);
01188 }
01189 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
01190
01191 tmp = *frame;
01192 tmp.extended_data = tmp.data;
01193 frame = &tmp;
01194 }
01195
01196
01197 if (frame) {
01198 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
01199 if (frame->nb_samples > avctx->frame_size) {
01200 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
01201 return AVERROR(EINVAL);
01202 }
01203 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
01204 if (frame->nb_samples < avctx->frame_size &&
01205 !avctx->internal->last_audio_frame) {
01206 ret = pad_last_frame(avctx, &padded_frame, frame);
01207 if (ret < 0)
01208 return ret;
01209
01210 frame = padded_frame;
01211 avctx->internal->last_audio_frame = 1;
01212 }
01213
01214 if (frame->nb_samples != avctx->frame_size) {
01215 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
01216 ret = AVERROR(EINVAL);
01217 goto end;
01218 }
01219 }
01220 }
01221
01222 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
01223 if (!ret) {
01224 if (*got_packet_ptr) {
01225 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
01226 if (avpkt->pts == AV_NOPTS_VALUE)
01227 avpkt->pts = frame->pts;
01228 if (!avpkt->duration)
01229 avpkt->duration = ff_samples_to_time_base(avctx,
01230 frame->nb_samples);
01231 }
01232 avpkt->dts = avpkt->pts;
01233 } else {
01234 avpkt->size = 0;
01235 }
01236 }
01237 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
01238 needs_realloc = 0;
01239 if (user_pkt.data) {
01240 if (user_pkt.size >= avpkt->size) {
01241 memcpy(user_pkt.data, avpkt->data, avpkt->size);
01242 } else {
01243 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
01244 avpkt->size = user_pkt.size;
01245 ret = -1;
01246 }
01247 avpkt->data = user_pkt.data;
01248 avpkt->destruct = user_pkt.destruct;
01249 } else {
01250 if (av_dup_packet(avpkt) < 0) {
01251 ret = AVERROR(ENOMEM);
01252 }
01253 }
01254 }
01255
01256 if (!ret) {
01257 if (needs_realloc && avpkt->data) {
01258 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
01259 if (new_data)
01260 avpkt->data = new_data;
01261 }
01262
01263 avctx->frame_number++;
01264 }
01265
01266 if (ret < 0 || !*got_packet_ptr) {
01267 av_free_packet(avpkt);
01268 av_init_packet(avpkt);
01269 goto end;
01270 }
01271
01272
01273
01274
01275 avpkt->flags |= AV_PKT_FLAG_KEY;
01276
01277 end:
01278 if (padded_frame) {
01279 av_freep(&padded_frame->data[0]);
01280 if (padded_frame->extended_data != padded_frame->data)
01281 av_freep(&padded_frame->extended_data);
01282 av_freep(&padded_frame);
01283 }
01284
01285 return ret;
01286 }
01287
01288 #if FF_API_OLD_ENCODE_AUDIO
01289 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
01290 uint8_t *buf, int buf_size,
01291 const short *samples)
01292 {
01293 AVPacket pkt;
01294 AVFrame frame0 = { 0 };
01295 AVFrame *frame;
01296 int ret, samples_size, got_packet;
01297
01298 av_init_packet(&pkt);
01299 pkt.data = buf;
01300 pkt.size = buf_size;
01301
01302 if (samples) {
01303 frame = &frame0;
01304 avcodec_get_frame_defaults(frame);
01305
01306 if (avctx->frame_size) {
01307 frame->nb_samples = avctx->frame_size;
01308 } else {
01309
01310
01311 int64_t nb_samples;
01312 if (!av_get_bits_per_sample(avctx->codec_id)) {
01313 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
01314 "support this codec\n");
01315 return AVERROR(EINVAL);
01316 }
01317 nb_samples = (int64_t)buf_size * 8 /
01318 (av_get_bits_per_sample(avctx->codec_id) *
01319 avctx->channels);
01320 if (nb_samples >= INT_MAX)
01321 return AVERROR(EINVAL);
01322 frame->nb_samples = nb_samples;
01323 }
01324
01325
01326
01327 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
01328 frame->nb_samples,
01329 avctx->sample_fmt, 1);
01330 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
01331 avctx->sample_fmt,
01332 (const uint8_t *)samples,
01333 samples_size, 1)) < 0)
01334 return ret;
01335
01336
01337
01338
01339 if (avctx->sample_rate && avctx->time_base.num)
01340 frame->pts = ff_samples_to_time_base(avctx,
01341 avctx->internal->sample_count);
01342 else
01343 frame->pts = AV_NOPTS_VALUE;
01344 avctx->internal->sample_count += frame->nb_samples;
01345 } else {
01346 frame = NULL;
01347 }
01348
01349 got_packet = 0;
01350 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
01351 if (!ret && got_packet && avctx->coded_frame) {
01352 avctx->coded_frame->pts = pkt.pts;
01353 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
01354 }
01355
01356 ff_packet_free_side_data(&pkt);
01357
01358 if (frame && frame->extended_data != frame->data)
01359 av_freep(&frame->extended_data);
01360
01361 return ret ? ret : pkt.size;
01362 }
01363
01364 #endif
01365
01366 #if FF_API_OLD_ENCODE_VIDEO
01367 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
01368 const AVFrame *pict)
01369 {
01370 AVPacket pkt;
01371 int ret, got_packet = 0;
01372
01373 if (buf_size < FF_MIN_BUFFER_SIZE) {
01374 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
01375 return -1;
01376 }
01377
01378 av_init_packet(&pkt);
01379 pkt.data = buf;
01380 pkt.size = buf_size;
01381
01382 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
01383 if (!ret && got_packet && avctx->coded_frame) {
01384 avctx->coded_frame->pts = pkt.pts;
01385 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
01386 }
01387
01388
01389 if (pkt.side_data_elems > 0) {
01390 int i;
01391 for (i = 0; i < pkt.side_data_elems; i++)
01392 av_free(pkt.side_data[i].data);
01393 av_freep(&pkt.side_data);
01394 pkt.side_data_elems = 0;
01395 }
01396
01397 return ret ? ret : pkt.size;
01398 }
01399
01400 #endif
01401
01402 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
01403 AVPacket *avpkt,
01404 const AVFrame *frame,
01405 int *got_packet_ptr)
01406 {
01407 int ret;
01408 AVPacket user_pkt = *avpkt;
01409 int needs_realloc = !user_pkt.data;
01410
01411 *got_packet_ptr = 0;
01412
01413 if(HAVE_THREADS && avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
01414 return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
01415
01416 if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
01417 avctx->stats_out[0] = '\0';
01418
01419 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
01420 av_free_packet(avpkt);
01421 av_init_packet(avpkt);
01422 avpkt->size = 0;
01423 return 0;
01424 }
01425
01426 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
01427 return AVERROR(EINVAL);
01428
01429 av_assert0(avctx->codec->encode2);
01430
01431 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
01432 av_assert0(ret <= 0);
01433
01434 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
01435 needs_realloc = 0;
01436 if (user_pkt.data) {
01437 if (user_pkt.size >= avpkt->size) {
01438 memcpy(user_pkt.data, avpkt->data, avpkt->size);
01439 } else {
01440 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
01441 avpkt->size = user_pkt.size;
01442 ret = -1;
01443 }
01444 avpkt->data = user_pkt.data;
01445 avpkt->destruct = user_pkt.destruct;
01446 } else {
01447 if (av_dup_packet(avpkt) < 0) {
01448 ret = AVERROR(ENOMEM);
01449 }
01450 }
01451 }
01452
01453 if (!ret) {
01454 if (!*got_packet_ptr)
01455 avpkt->size = 0;
01456 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
01457 avpkt->pts = avpkt->dts = frame->pts;
01458
01459 if (needs_realloc && avpkt->data &&
01460 avpkt->destruct == av_destruct_packet) {
01461 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
01462 if (new_data)
01463 avpkt->data = new_data;
01464 }
01465
01466 avctx->frame_number++;
01467 }
01468
01469 if (ret < 0 || !*got_packet_ptr)
01470 av_free_packet(avpkt);
01471
01472 emms_c();
01473 return ret;
01474 }
01475
01476 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
01477 const AVSubtitle *sub)
01478 {
01479 int ret;
01480 if (sub->start_display_time) {
01481 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
01482 return -1;
01483 }
01484
01485 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
01486 avctx->frame_number++;
01487 return ret;
01488 }
01489
01500 static int64_t guess_correct_pts(AVCodecContext *ctx,
01501 int64_t reordered_pts, int64_t dts)
01502 {
01503 int64_t pts = AV_NOPTS_VALUE;
01504
01505 if (dts != AV_NOPTS_VALUE) {
01506 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
01507 ctx->pts_correction_last_dts = dts;
01508 }
01509 if (reordered_pts != AV_NOPTS_VALUE) {
01510 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
01511 ctx->pts_correction_last_pts = reordered_pts;
01512 }
01513 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
01514 && reordered_pts != AV_NOPTS_VALUE)
01515 pts = reordered_pts;
01516 else
01517 pts = dts;
01518
01519 return pts;
01520 }
01521
01522 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
01523 {
01524 int size = 0;
01525 const uint8_t *data;
01526 uint32_t flags;
01527
01528 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
01529 return;
01530
01531 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
01532 if (!data || size < 4)
01533 return;
01534 flags = bytestream_get_le32(&data);
01535 size -= 4;
01536 if (size < 4)
01537 return;
01538 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
01539 avctx->channels = bytestream_get_le32(&data);
01540 size -= 4;
01541 }
01542 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
01543 if (size < 8)
01544 return;
01545 avctx->channel_layout = bytestream_get_le64(&data);
01546 size -= 8;
01547 }
01548 if (size < 4)
01549 return;
01550 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
01551 avctx->sample_rate = bytestream_get_le32(&data);
01552 size -= 4;
01553 }
01554 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
01555 if (size < 8)
01556 return;
01557 avctx->width = bytestream_get_le32(&data);
01558 avctx->height = bytestream_get_le32(&data);
01559 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
01560 size -= 8;
01561 }
01562 }
01563
01564 static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
01565 {
01566 int size, ret = 0;
01567 const uint8_t *side_metadata;
01568 const uint8_t *end;
01569
01570 av_dict_free(&avctx->metadata);
01571 side_metadata = av_packet_get_side_data(avctx->pkt,
01572 AV_PKT_DATA_STRINGS_METADATA, &size);
01573 if (!side_metadata)
01574 goto end;
01575 end = side_metadata + size;
01576 while (side_metadata < end) {
01577 const uint8_t *key = side_metadata;
01578 const uint8_t *val = side_metadata + strlen(key) + 1;
01579 int ret = av_dict_set(&frame->metadata, key, val, 0);
01580 if (ret < 0)
01581 break;
01582 side_metadata = val + strlen(val) + 1;
01583 }
01584 end:
01585 avctx->metadata = frame->metadata;
01586 return ret;
01587 }
01588
01589 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
01590 int *got_picture_ptr,
01591 const AVPacket *avpkt)
01592 {
01593 int ret;
01594
01595 AVPacket tmp = *avpkt;
01596
01597 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
01598 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
01599 return AVERROR(EINVAL);
01600 }
01601
01602 *got_picture_ptr = 0;
01603 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
01604 return AVERROR(EINVAL);
01605
01606 avcodec_get_frame_defaults(picture);
01607
01608 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
01609 int did_split = av_packet_split_side_data(&tmp);
01610 apply_param_change(avctx, &tmp);
01611 avctx->pkt = &tmp;
01612 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
01613 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
01614 &tmp);
01615 else {
01616 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
01617 &tmp);
01618 picture->pkt_dts = avpkt->dts;
01619
01620 if(!avctx->has_b_frames){
01621 picture->pkt_pos = avpkt->pos;
01622 }
01623
01624
01625 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
01626 if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
01627 if (!picture->width) picture->width = avctx->width;
01628 if (!picture->height) picture->height = avctx->height;
01629 if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
01630 }
01631 }
01632 add_metadata_from_side_data(avctx, picture);
01633
01634 emms_c();
01635
01636 avctx->pkt = NULL;
01637 if (did_split) {
01638 ff_packet_free_side_data(&tmp);
01639 if(ret == tmp.size)
01640 ret = avpkt->size;
01641 }
01642
01643 if (*got_picture_ptr){
01644 avctx->frame_number++;
01645 picture->best_effort_timestamp = guess_correct_pts(avctx,
01646 picture->pkt_pts,
01647 picture->pkt_dts);
01648 }
01649 } else
01650 ret = 0;
01651
01652
01653
01654 picture->extended_data = picture->data;
01655
01656 return ret;
01657 }
01658
01659 #if FF_API_OLD_DECODE_AUDIO
01660 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
01661 int *frame_size_ptr,
01662 AVPacket *avpkt)
01663 {
01664 AVFrame frame = {0};
01665 int ret, got_frame = 0;
01666
01667 if (avctx->get_buffer != avcodec_default_get_buffer) {
01668 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
01669 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
01670 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
01671 "avcodec_decode_audio4()\n");
01672 avctx->get_buffer = avcodec_default_get_buffer;
01673 avctx->release_buffer = avcodec_default_release_buffer;
01674 }
01675
01676 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
01677
01678 if (ret >= 0 && got_frame) {
01679 int ch, plane_size;
01680 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
01681 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
01682 frame.nb_samples,
01683 avctx->sample_fmt, 1);
01684 if (*frame_size_ptr < data_size) {
01685 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
01686 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
01687 return AVERROR(EINVAL);
01688 }
01689
01690 memcpy(samples, frame.extended_data[0], plane_size);
01691
01692 if (planar && avctx->channels > 1) {
01693 uint8_t *out = ((uint8_t *)samples) + plane_size;
01694 for (ch = 1; ch < avctx->channels; ch++) {
01695 memcpy(out, frame.extended_data[ch], plane_size);
01696 out += plane_size;
01697 }
01698 }
01699 *frame_size_ptr = data_size;
01700 } else {
01701 *frame_size_ptr = 0;
01702 }
01703 return ret;
01704 }
01705
01706 #endif
01707
01708 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
01709 AVFrame *frame,
01710 int *got_frame_ptr,
01711 const AVPacket *avpkt)
01712 {
01713 int planar, channels;
01714 int ret = 0;
01715
01716 *got_frame_ptr = 0;
01717
01718 if (!avpkt->data && avpkt->size) {
01719 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
01720 return AVERROR(EINVAL);
01721 }
01722 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
01723 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
01724 return AVERROR(EINVAL);
01725 }
01726
01727 avcodec_get_frame_defaults(frame);
01728
01729 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
01730 uint8_t *side;
01731 int side_size;
01732
01733 AVPacket tmp = *avpkt;
01734 int did_split = av_packet_split_side_data(&tmp);
01735 apply_param_change(avctx, &tmp);
01736
01737 avctx->pkt = &tmp;
01738 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
01739 if (ret >= 0 && *got_frame_ptr) {
01740 avctx->frame_number++;
01741 frame->pkt_dts = avpkt->dts;
01742 frame->best_effort_timestamp = guess_correct_pts(avctx,
01743 frame->pkt_pts,
01744 frame->pkt_dts);
01745 if (frame->format == AV_SAMPLE_FMT_NONE)
01746 frame->format = avctx->sample_fmt;
01747 if (!frame->channel_layout)
01748 frame->channel_layout = avctx->channel_layout;
01749 if (!frame->channels)
01750 frame->channels = avctx->channels;
01751 if (!frame->sample_rate)
01752 frame->sample_rate = avctx->sample_rate;
01753 }
01754 add_metadata_from_side_data(avctx, frame);
01755
01756 side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
01757 if(side && side_size>=10) {
01758 avctx->internal->skip_samples = AV_RL32(side);
01759 av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
01760 avctx->internal->skip_samples);
01761 }
01762 if (avctx->internal->skip_samples) {
01763 if(frame->nb_samples <= avctx->internal->skip_samples){
01764 *got_frame_ptr = 0;
01765 avctx->internal->skip_samples -= frame->nb_samples;
01766 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
01767 avctx->internal->skip_samples);
01768 } else {
01769 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
01770 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
01771 if(avctx->pkt_timebase.num && avctx->sample_rate) {
01772 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
01773 (AVRational){1, avctx->sample_rate},
01774 avctx->pkt_timebase);
01775 if(frame->pkt_pts!=AV_NOPTS_VALUE)
01776 frame->pkt_pts += diff_ts;
01777 if(frame->pkt_dts!=AV_NOPTS_VALUE)
01778 frame->pkt_dts += diff_ts;
01779 if (frame->pkt_duration >= diff_ts)
01780 frame->pkt_duration -= diff_ts;
01781 } else {
01782 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
01783 }
01784 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
01785 avctx->internal->skip_samples, frame->nb_samples);
01786 frame->nb_samples -= avctx->internal->skip_samples;
01787 avctx->internal->skip_samples = 0;
01788 }
01789 }
01790
01791 avctx->pkt = NULL;
01792 if (did_split) {
01793 ff_packet_free_side_data(&tmp);
01794 if(ret == tmp.size)
01795 ret = avpkt->size;
01796 }
01797 }
01798
01799
01800
01801
01802 if (*got_frame_ptr) {
01803 planar = av_sample_fmt_is_planar(frame->format);
01804 channels = av_get_channel_layout_nb_channels(frame->channel_layout);
01805 if (!(planar && channels > AV_NUM_DATA_POINTERS))
01806 frame->extended_data = frame->data;
01807 } else {
01808 frame->extended_data = NULL;
01809 }
01810
01811 return ret;
01812 }
01813
01814 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
01815 int *got_sub_ptr,
01816 AVPacket *avpkt)
01817 {
01818 int ret = 0;
01819
01820 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
01821 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
01822 return AVERROR(EINVAL);
01823 }
01824
01825 *got_sub_ptr = 0;
01826 avcodec_get_subtitle_defaults(sub);
01827
01828 if (avpkt->size) {
01829 AVPacket tmp = *avpkt;
01830 int did_split = av_packet_split_side_data(&tmp);
01831
01832
01833 avctx->pkt = &tmp;
01834
01835 if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
01836 sub->pts = av_rescale_q(avpkt->pts,
01837 avctx->pkt_timebase, AV_TIME_BASE_Q);
01838 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &tmp);
01839
01840 avctx->pkt = NULL;
01841 if (did_split) {
01842 ff_packet_free_side_data(&tmp);
01843 if(ret == tmp.size)
01844 ret = avpkt->size;
01845 }
01846
01847 if (*got_sub_ptr)
01848 avctx->frame_number++;
01849 }
01850
01851 return ret;
01852 }
01853
01854 void avsubtitle_free(AVSubtitle *sub)
01855 {
01856 int i;
01857
01858 for (i = 0; i < sub->num_rects; i++) {
01859 av_freep(&sub->rects[i]->pict.data[0]);
01860 av_freep(&sub->rects[i]->pict.data[1]);
01861 av_freep(&sub->rects[i]->pict.data[2]);
01862 av_freep(&sub->rects[i]->pict.data[3]);
01863 av_freep(&sub->rects[i]->text);
01864 av_freep(&sub->rects[i]->ass);
01865 av_freep(&sub->rects[i]);
01866 }
01867
01868 av_freep(&sub->rects);
01869
01870 memset(sub, 0, sizeof(AVSubtitle));
01871 }
01872
01873 av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
01874 {
01875 int ret = 0;
01876
01877 ff_unlock_avcodec();
01878
01879 ret = avcodec_close(avctx);
01880
01881 ff_lock_avcodec(NULL);
01882 return ret;
01883 }
01884
01885 av_cold int avcodec_close(AVCodecContext *avctx)
01886 {
01887 int ret = ff_lock_avcodec(avctx);
01888 if (ret < 0)
01889 return ret;
01890
01891 if (avcodec_is_open(avctx)) {
01892 if (HAVE_THREADS && avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
01893 ff_unlock_avcodec();
01894 ff_frame_thread_encoder_free(avctx);
01895 ff_lock_avcodec(avctx);
01896 }
01897 if (HAVE_THREADS && avctx->thread_opaque)
01898 ff_thread_free(avctx);
01899 if (avctx->codec && avctx->codec->close)
01900 avctx->codec->close(avctx);
01901 avcodec_default_free_buffers(avctx);
01902 avctx->coded_frame = NULL;
01903 avctx->internal->byte_buffer_size = 0;
01904 av_freep(&avctx->internal->byte_buffer);
01905 av_freep(&avctx->internal);
01906 av_dict_free(&avctx->metadata);
01907 }
01908
01909 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
01910 av_opt_free(avctx->priv_data);
01911 av_opt_free(avctx);
01912 av_freep(&avctx->priv_data);
01913 if (av_codec_is_encoder(avctx->codec))
01914 av_freep(&avctx->extradata);
01915 avctx->codec = NULL;
01916 avctx->active_thread_type = 0;
01917
01918 ff_unlock_avcodec();
01919 return 0;
01920 }
01921
01922 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
01923 {
01924 switch(id){
01925
01926
01927
01928 case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
01929 case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
01930 default : return id;
01931 }
01932 }
01933
01934 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
01935 {
01936 AVCodec *p, *experimental = NULL;
01937 p = first_avcodec;
01938 id= remap_deprecated_codec_id(id);
01939 while (p) {
01940 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
01941 p->id == id) {
01942 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
01943 experimental = p;
01944 } else
01945 return p;
01946 }
01947 p = p->next;
01948 }
01949 return experimental;
01950 }
01951
01952 AVCodec *avcodec_find_encoder(enum AVCodecID id)
01953 {
01954 return find_encdec(id, 1);
01955 }
01956
01957 AVCodec *avcodec_find_encoder_by_name(const char *name)
01958 {
01959 AVCodec *p;
01960 if (!name)
01961 return NULL;
01962 p = first_avcodec;
01963 while (p) {
01964 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
01965 return p;
01966 p = p->next;
01967 }
01968 return NULL;
01969 }
01970
01971 AVCodec *avcodec_find_decoder(enum AVCodecID id)
01972 {
01973 return find_encdec(id, 0);
01974 }
01975
01976 AVCodec *avcodec_find_decoder_by_name(const char *name)
01977 {
01978 AVCodec *p;
01979 if (!name)
01980 return NULL;
01981 p = first_avcodec;
01982 while (p) {
01983 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
01984 return p;
01985 p = p->next;
01986 }
01987 return NULL;
01988 }
01989
01990 const char *avcodec_get_name(enum AVCodecID id)
01991 {
01992 const AVCodecDescriptor *cd;
01993 AVCodec *codec;
01994
01995 if (id == AV_CODEC_ID_NONE)
01996 return "none";
01997 cd = avcodec_descriptor_get(id);
01998 if (cd)
01999 return cd->name;
02000 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
02001 codec = avcodec_find_decoder(id);
02002 if (codec)
02003 return codec->name;
02004 codec = avcodec_find_encoder(id);
02005 if (codec)
02006 return codec->name;
02007 return "unknown_codec";
02008 }
02009
02010 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
02011 {
02012 int i, len, ret = 0;
02013
02014 #define IS_PRINT(x) \
02015 (((x) >= '0' && (x) <= '9') || \
02016 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
02017 ((x) == '.' || (x) == ' ' || (x) == '-'))
02018
02019 for (i = 0; i < 4; i++) {
02020 len = snprintf(buf, buf_size,
02021 IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
02022 buf += len;
02023 buf_size = buf_size > len ? buf_size - len : 0;
02024 ret += len;
02025 codec_tag >>= 8;
02026 }
02027 return ret;
02028 }
02029
02030 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
02031 {
02032 const char *codec_type;
02033 const char *codec_name;
02034 const char *profile = NULL;
02035 const AVCodec *p;
02036 int bitrate;
02037 AVRational display_aspect_ratio;
02038
02039 if (!buf || buf_size <= 0)
02040 return;
02041 codec_type = av_get_media_type_string(enc->codec_type);
02042 codec_name = avcodec_get_name(enc->codec_id);
02043 if (enc->profile != FF_PROFILE_UNKNOWN) {
02044 if (enc->codec)
02045 p = enc->codec;
02046 else
02047 p = encode ? avcodec_find_encoder(enc->codec_id) :
02048 avcodec_find_decoder(enc->codec_id);
02049 if (p)
02050 profile = av_get_profile_name(p, enc->profile);
02051 }
02052
02053 snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
02054 codec_name, enc->mb_decision ? " (hq)" : "");
02055 buf[0] ^= 'a' ^ 'A';
02056 if (profile)
02057 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
02058 if (enc->codec_tag) {
02059 char tag_buf[32];
02060 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
02061 snprintf(buf + strlen(buf), buf_size - strlen(buf),
02062 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
02063 }
02064
02065 switch (enc->codec_type) {
02066 case AVMEDIA_TYPE_VIDEO:
02067 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
02068 snprintf(buf + strlen(buf), buf_size - strlen(buf),
02069 ", %s",
02070 av_get_pix_fmt_name(enc->pix_fmt));
02071 if (enc->bits_per_raw_sample &&
02072 enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
02073 snprintf(buf + strlen(buf), buf_size - strlen(buf),
02074 " (%d bpc)", enc->bits_per_raw_sample);
02075 }
02076 if (enc->width) {
02077 snprintf(buf + strlen(buf), buf_size - strlen(buf),
02078 ", %dx%d",
02079 enc->width, enc->height);
02080 if (enc->sample_aspect_ratio.num) {
02081 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
02082 enc->width * enc->sample_aspect_ratio.num,
02083 enc->height * enc->sample_aspect_ratio.den,
02084 1024 * 1024);
02085 snprintf(buf + strlen(buf), buf_size - strlen(buf),
02086 " [SAR %d:%d DAR %d:%d]",
02087 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
02088 display_aspect_ratio.num, display_aspect_ratio.den);
02089 }
02090 if (av_log_get_level() >= AV_LOG_DEBUG) {
02091 int g = av_gcd(enc->time_base.num, enc->time_base.den);
02092 snprintf(buf + strlen(buf), buf_size - strlen(buf),
02093 ", %d/%d",
02094 enc->time_base.num / g, enc->time_base.den / g);
02095 }
02096 }
02097 if (encode) {
02098 snprintf(buf + strlen(buf), buf_size - strlen(buf),
02099 ", q=%d-%d", enc->qmin, enc->qmax);
02100 }
02101 break;
02102 case AVMEDIA_TYPE_AUDIO:
02103 if (enc->sample_rate) {
02104 snprintf(buf + strlen(buf), buf_size - strlen(buf),
02105 ", %d Hz", enc->sample_rate);
02106 }
02107 av_strlcat(buf, ", ", buf_size);
02108 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
02109 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
02110 snprintf(buf + strlen(buf), buf_size - strlen(buf),
02111 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
02112 }
02113 break;
02114 default:
02115 return;
02116 }
02117 if (encode) {
02118 if (enc->flags & CODEC_FLAG_PASS1)
02119 snprintf(buf + strlen(buf), buf_size - strlen(buf),
02120 ", pass 1");
02121 if (enc->flags & CODEC_FLAG_PASS2)
02122 snprintf(buf + strlen(buf), buf_size - strlen(buf),
02123 ", pass 2");
02124 }
02125 bitrate = get_bit_rate(enc);
02126 if (bitrate != 0) {
02127 snprintf(buf + strlen(buf), buf_size - strlen(buf),
02128 ", %d kb/s", bitrate / 1000);
02129 }
02130 }
02131
02132 const char *av_get_profile_name(const AVCodec *codec, int profile)
02133 {
02134 const AVProfile *p;
02135 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
02136 return NULL;
02137
02138 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
02139 if (p->profile == profile)
02140 return p->name;
02141
02142 return NULL;
02143 }
02144
02145 unsigned avcodec_version(void)
02146 {
02147
02148 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
02149 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
02150
02151 av_assert0(AV_CODEC_ID_SRT==94216);
02152 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
02153
02154 return LIBAVCODEC_VERSION_INT;
02155 }
02156
02157 const char *avcodec_configuration(void)
02158 {
02159 return FFMPEG_CONFIGURATION;
02160 }
02161
02162 const char *avcodec_license(void)
02163 {
02164 #define LICENSE_PREFIX "libavcodec license: "
02165 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
02166 }
02167
02168 void avcodec_flush_buffers(AVCodecContext *avctx)
02169 {
02170 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
02171 ff_thread_flush(avctx);
02172 else if (avctx->codec->flush)
02173 avctx->codec->flush(avctx);
02174
02175 avctx->pts_correction_last_pts =
02176 avctx->pts_correction_last_dts = INT64_MIN;
02177 }
02178
02179 static void video_free_buffers(AVCodecContext *s)
02180 {
02181 AVCodecInternal *avci = s->internal;
02182 int i, j;
02183
02184 if (!avci->buffer)
02185 return;
02186
02187 if (avci->buffer_count)
02188 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
02189 avci->buffer_count);
02190 for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
02191 InternalBuffer *buf = &avci->buffer[i];
02192 for (j = 0; j < 4; j++) {
02193 av_freep(&buf->base[j]);
02194 buf->data[j] = NULL;
02195 }
02196 }
02197 av_freep(&avci->buffer);
02198
02199 avci->buffer_count = 0;
02200 }
02201
02202 static void audio_free_buffers(AVCodecContext *avctx)
02203 {
02204 AVCodecInternal *avci = avctx->internal;
02205 av_freep(&avci->audio_data);
02206 }
02207
02208 void avcodec_default_free_buffers(AVCodecContext *avctx)
02209 {
02210 switch (avctx->codec_type) {
02211 case AVMEDIA_TYPE_VIDEO:
02212 video_free_buffers(avctx);
02213 break;
02214 case AVMEDIA_TYPE_AUDIO:
02215 audio_free_buffers(avctx);
02216 break;
02217 default:
02218 break;
02219 }
02220 }
02221
02222 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
02223 {
02224 switch (codec_id) {
02225 case AV_CODEC_ID_8SVX_EXP:
02226 case AV_CODEC_ID_8SVX_FIB:
02227 case AV_CODEC_ID_ADPCM_CT:
02228 case AV_CODEC_ID_ADPCM_IMA_APC:
02229 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
02230 case AV_CODEC_ID_ADPCM_IMA_OKI:
02231 case AV_CODEC_ID_ADPCM_IMA_WS:
02232 case AV_CODEC_ID_ADPCM_G722:
02233 case AV_CODEC_ID_ADPCM_YAMAHA:
02234 return 4;
02235 case AV_CODEC_ID_PCM_ALAW:
02236 case AV_CODEC_ID_PCM_MULAW:
02237 case AV_CODEC_ID_PCM_S8:
02238 case AV_CODEC_ID_PCM_S8_PLANAR:
02239 case AV_CODEC_ID_PCM_U8:
02240 case AV_CODEC_ID_PCM_ZORK:
02241 return 8;
02242 case AV_CODEC_ID_PCM_S16BE:
02243 case AV_CODEC_ID_PCM_S16BE_PLANAR:
02244 case AV_CODEC_ID_PCM_S16LE:
02245 case AV_CODEC_ID_PCM_S16LE_PLANAR:
02246 case AV_CODEC_ID_PCM_U16BE:
02247 case AV_CODEC_ID_PCM_U16LE:
02248 return 16;
02249 case AV_CODEC_ID_PCM_S24DAUD:
02250 case AV_CODEC_ID_PCM_S24BE:
02251 case AV_CODEC_ID_PCM_S24LE:
02252 case AV_CODEC_ID_PCM_S24LE_PLANAR:
02253 case AV_CODEC_ID_PCM_U24BE:
02254 case AV_CODEC_ID_PCM_U24LE:
02255 return 24;
02256 case AV_CODEC_ID_PCM_S32BE:
02257 case AV_CODEC_ID_PCM_S32LE:
02258 case AV_CODEC_ID_PCM_S32LE_PLANAR:
02259 case AV_CODEC_ID_PCM_U32BE:
02260 case AV_CODEC_ID_PCM_U32LE:
02261 case AV_CODEC_ID_PCM_F32BE:
02262 case AV_CODEC_ID_PCM_F32LE:
02263 return 32;
02264 case AV_CODEC_ID_PCM_F64BE:
02265 case AV_CODEC_ID_PCM_F64LE:
02266 return 64;
02267 default:
02268 return 0;
02269 }
02270 }
02271
02272 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
02273 {
02274 static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
02275 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
02276 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
02277 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
02278 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
02279 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
02280 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
02281 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
02282 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
02283 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
02284 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
02285 };
02286 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
02287 return AV_CODEC_ID_NONE;
02288 if (be < 0 || be > 1)
02289 be = AV_NE(1, 0);
02290 return map[fmt][be];
02291 }
02292
02293 int av_get_bits_per_sample(enum AVCodecID codec_id)
02294 {
02295 switch (codec_id) {
02296 case AV_CODEC_ID_ADPCM_SBPRO_2:
02297 return 2;
02298 case AV_CODEC_ID_ADPCM_SBPRO_3:
02299 return 3;
02300 case AV_CODEC_ID_ADPCM_SBPRO_4:
02301 case AV_CODEC_ID_ADPCM_IMA_WAV:
02302 case AV_CODEC_ID_ADPCM_IMA_QT:
02303 case AV_CODEC_ID_ADPCM_SWF:
02304 case AV_CODEC_ID_ADPCM_MS:
02305 return 4;
02306 default:
02307 return av_get_exact_bits_per_sample(codec_id);
02308 }
02309 }
02310
02311 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
02312 {
02313 int id, sr, ch, ba, tag, bps;
02314
02315 id = avctx->codec_id;
02316 sr = avctx->sample_rate;
02317 ch = avctx->channels;
02318 ba = avctx->block_align;
02319 tag = avctx->codec_tag;
02320 bps = av_get_exact_bits_per_sample(avctx->codec_id);
02321
02322
02323 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
02324 return (frame_bytes * 8LL) / (bps * ch);
02325 bps = avctx->bits_per_coded_sample;
02326
02327
02328 switch (id) {
02329 case AV_CODEC_ID_ADPCM_ADX: return 32;
02330 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
02331 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
02332 case AV_CODEC_ID_AMR_NB:
02333 case AV_CODEC_ID_GSM:
02334 case AV_CODEC_ID_QCELP:
02335 case AV_CODEC_ID_RA_288: return 160;
02336 case AV_CODEC_ID_AMR_WB:
02337 case AV_CODEC_ID_GSM_MS: return 320;
02338 case AV_CODEC_ID_MP1: return 384;
02339 case AV_CODEC_ID_ATRAC1: return 512;
02340 case AV_CODEC_ID_ATRAC3: return 1024;
02341 case AV_CODEC_ID_MP2:
02342 case AV_CODEC_ID_MUSEPACK7: return 1152;
02343 case AV_CODEC_ID_AC3: return 1536;
02344 }
02345
02346 if (sr > 0) {
02347
02348 if (id == AV_CODEC_ID_TTA)
02349 return 256 * sr / 245;
02350
02351 if (ch > 0) {
02352
02353 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
02354 return (480 << (sr / 22050)) / ch;
02355 }
02356 }
02357
02358 if (ba > 0) {
02359
02360 if (id == AV_CODEC_ID_SIPR) {
02361 switch (ba) {
02362 case 20: return 160;
02363 case 19: return 144;
02364 case 29: return 288;
02365 case 37: return 480;
02366 }
02367 } else if (id == AV_CODEC_ID_ILBC) {
02368 switch (ba) {
02369 case 38: return 160;
02370 case 50: return 240;
02371 }
02372 }
02373 }
02374
02375 if (frame_bytes > 0) {
02376
02377 if (id == AV_CODEC_ID_TRUESPEECH)
02378 return 240 * (frame_bytes / 32);
02379 if (id == AV_CODEC_ID_NELLYMOSER)
02380 return 256 * (frame_bytes / 64);
02381 if (id == AV_CODEC_ID_RA_144)
02382 return 160 * (frame_bytes / 20);
02383 if (id == AV_CODEC_ID_G723_1)
02384 return 240 * (frame_bytes / 24);
02385
02386 if (bps > 0) {
02387
02388 if (id == AV_CODEC_ID_ADPCM_G726)
02389 return frame_bytes * 8 / bps;
02390 }
02391
02392 if (ch > 0) {
02393
02394 switch (id) {
02395 case AV_CODEC_ID_ADPCM_AFC:
02396 return frame_bytes / (9 * ch) * 16;
02397 case AV_CODEC_ID_ADPCM_4XM:
02398 case AV_CODEC_ID_ADPCM_IMA_ISS:
02399 return (frame_bytes - 4 * ch) * 2 / ch;
02400 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
02401 return (frame_bytes - 4) * 2 / ch;
02402 case AV_CODEC_ID_ADPCM_IMA_AMV:
02403 return (frame_bytes - 8) * 2 / ch;
02404 case AV_CODEC_ID_ADPCM_XA:
02405 return (frame_bytes / 128) * 224 / ch;
02406 case AV_CODEC_ID_INTERPLAY_DPCM:
02407 return (frame_bytes - 6 - ch) / ch;
02408 case AV_CODEC_ID_ROQ_DPCM:
02409 return (frame_bytes - 8) / ch;
02410 case AV_CODEC_ID_XAN_DPCM:
02411 return (frame_bytes - 2 * ch) / ch;
02412 case AV_CODEC_ID_MACE3:
02413 return 3 * frame_bytes / ch;
02414 case AV_CODEC_ID_MACE6:
02415 return 6 * frame_bytes / ch;
02416 case AV_CODEC_ID_PCM_LXF:
02417 return 2 * (frame_bytes / (5 * ch));
02418 case AV_CODEC_ID_IAC:
02419 case AV_CODEC_ID_IMC:
02420 return 4 * frame_bytes / ch;
02421 }
02422
02423 if (tag) {
02424
02425 if (id == AV_CODEC_ID_SOL_DPCM) {
02426 if (tag == 3)
02427 return frame_bytes / ch;
02428 else
02429 return frame_bytes * 2 / ch;
02430 }
02431 }
02432
02433 if (ba > 0) {
02434
02435 int blocks = frame_bytes / ba;
02436 switch (avctx->codec_id) {
02437 case AV_CODEC_ID_ADPCM_IMA_WAV:
02438 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
02439 case AV_CODEC_ID_ADPCM_IMA_DK3:
02440 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
02441 case AV_CODEC_ID_ADPCM_IMA_DK4:
02442 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
02443 case AV_CODEC_ID_ADPCM_MS:
02444 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
02445 }
02446 }
02447
02448 if (bps > 0) {
02449
02450 switch (avctx->codec_id) {
02451 case AV_CODEC_ID_PCM_DVD:
02452 if(bps<4)
02453 return 0;
02454 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
02455 case AV_CODEC_ID_PCM_BLURAY:
02456 if(bps<4)
02457 return 0;
02458 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
02459 case AV_CODEC_ID_S302M:
02460 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
02461 }
02462 }
02463 }
02464 }
02465
02466 return 0;
02467 }
02468
02469 #if !HAVE_THREADS
02470 int ff_thread_init(AVCodecContext *s)
02471 {
02472 return -1;
02473 }
02474
02475 #endif
02476
02477 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
02478 {
02479 unsigned int n = 0;
02480
02481 while (v >= 0xff) {
02482 *s++ = 0xff;
02483 v -= 0xff;
02484 n++;
02485 }
02486 *s = v;
02487 n++;
02488 return n;
02489 }
02490
02491 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
02492 {
02493 int i;
02494 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
02495 return i;
02496 }
02497
02498 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
02499 {
02500 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
02501 "version to the newest one from Git. If the problem still "
02502 "occurs, it means that your file has a feature which has not "
02503 "been implemented.\n", feature);
02504 if(want_sample)
02505 av_log_ask_for_sample(avc, NULL);
02506 }
02507
02508 void av_log_ask_for_sample(void *avc, const char *msg, ...)
02509 {
02510 va_list argument_list;
02511
02512 va_start(argument_list, msg);
02513
02514 if (msg)
02515 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
02516 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
02517 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
02518 "and contact the ffmpeg-devel mailing list.\n");
02519
02520 va_end(argument_list);
02521 }
02522
02523 static AVHWAccel *first_hwaccel = NULL;
02524
02525 void av_register_hwaccel(AVHWAccel *hwaccel)
02526 {
02527 AVHWAccel **p = &first_hwaccel;
02528 while (*p)
02529 p = &(*p)->next;
02530 *p = hwaccel;
02531 hwaccel->next = NULL;
02532 }
02533
02534 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
02535 {
02536 return hwaccel ? hwaccel->next : first_hwaccel;
02537 }
02538
02539 AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
02540 {
02541 AVHWAccel *hwaccel = NULL;
02542
02543 while ((hwaccel = av_hwaccel_next(hwaccel)))
02544 if (hwaccel->id == codec_id
02545 && hwaccel->pix_fmt == pix_fmt)
02546 return hwaccel;
02547 return NULL;
02548 }
02549
02550 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
02551 {
02552 if (ff_lockmgr_cb) {
02553 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
02554 return -1;
02555 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
02556 return -1;
02557 }
02558
02559 ff_lockmgr_cb = cb;
02560
02561 if (ff_lockmgr_cb) {
02562 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
02563 return -1;
02564 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
02565 return -1;
02566 }
02567 return 0;
02568 }
02569
02570 int ff_lock_avcodec(AVCodecContext *log_ctx)
02571 {
02572 if (ff_lockmgr_cb) {
02573 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
02574 return -1;
02575 }
02576 entangled_thread_counter++;
02577 if (entangled_thread_counter != 1) {
02578 av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
02579 ff_avcodec_locked = 1;
02580 ff_unlock_avcodec();
02581 return AVERROR(EINVAL);
02582 }
02583 av_assert0(!ff_avcodec_locked);
02584 ff_avcodec_locked = 1;
02585 return 0;
02586 }
02587
02588 int ff_unlock_avcodec(void)
02589 {
02590 av_assert0(ff_avcodec_locked);
02591 ff_avcodec_locked = 0;
02592 entangled_thread_counter--;
02593 if (ff_lockmgr_cb) {
02594 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
02595 return -1;
02596 }
02597 return 0;
02598 }
02599
02600 int avpriv_lock_avformat(void)
02601 {
02602 if (ff_lockmgr_cb) {
02603 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
02604 return -1;
02605 }
02606 return 0;
02607 }
02608
02609 int avpriv_unlock_avformat(void)
02610 {
02611 if (ff_lockmgr_cb) {
02612 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
02613 return -1;
02614 }
02615 return 0;
02616 }
02617
02618 unsigned int avpriv_toupper4(unsigned int x)
02619 {
02620 return toupper(x & 0xFF)
02621 + (toupper((x >> 8) & 0xFF) << 8)
02622 + (toupper((x >> 16) & 0xFF) << 16)
02623 + (toupper((x >> 24) & 0xFF) << 24);
02624 }
02625
02626 #if !HAVE_THREADS
02627
02628 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
02629 {
02630 f->owner = avctx;
02631
02632 return ff_get_buffer(avctx, f);
02633 }
02634
02635 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
02636 {
02637 f->owner->release_buffer(f->owner, f);
02638 }
02639
02640 void ff_thread_finish_setup(AVCodecContext *avctx)
02641 {
02642 }
02643
02644 void ff_thread_report_progress(AVFrame *f, int progress, int field)
02645 {
02646 }
02647
02648 void ff_thread_await_progress(AVFrame *f, int progress, int field)
02649 {
02650 }
02651
02652 int ff_thread_can_start_frame(AVCodecContext *avctx)
02653 {
02654 return 1;
02655 }
02656
02657 #endif
02658
02659 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
02660 {
02661 AVCodec *c= avcodec_find_decoder(codec_id);
02662 if(!c)
02663 c= avcodec_find_encoder(codec_id);
02664 if(c)
02665 return c->type;
02666
02667 if (codec_id <= AV_CODEC_ID_NONE)
02668 return AVMEDIA_TYPE_UNKNOWN;
02669 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
02670 return AVMEDIA_TYPE_VIDEO;
02671 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
02672 return AVMEDIA_TYPE_AUDIO;
02673 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
02674 return AVMEDIA_TYPE_SUBTITLE;
02675
02676 return AVMEDIA_TYPE_UNKNOWN;
02677 }
02678
02679 int avcodec_is_open(AVCodecContext *s)
02680 {
02681 return !!s->internal;
02682 }