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