mpegvideo_enc: drop support for reusing the input motion vectors.
[libav.git] / libavcodec / utils.c
CommitLineData
de6d9b64
FB
1/*
2 * utils for libavcodec
406792e7 3 * Copyright (c) 2001 Fabrice Bellard
8f2ab833 4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
de6d9b64 5 *
2912e87a 6 * This file is part of Libav.
b78e7197 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
ff4ec49e
FB
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 12 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
de6d9b64 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
de6d9b64 17 *
ff4ec49e 18 * You should have received a copy of the GNU Lesser General Public
2912e87a 19 * License along with Libav; if not, write to the Free Software
5509bffa 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 21 */
115329f1 22
983e3246 23/**
ba87f080 24 * @file
983e3246
MN
25 * utils.
26 */
115329f1 27
c6507946 28#include "config.h"
b2c75b6e 29#include "libavutil/avassert.h"
7fb94406 30#include "libavutil/avstring.h"
a903f8f0 31#include "libavutil/channel_layout.h"
245976da 32#include "libavutil/crc.h"
0ebcdf5c 33#include "libavutil/mathematics.h"
eb285cfe 34#include "libavutil/pixdesc.h"
737eb597
RT
35#include "libavutil/imgutils.h"
36#include "libavutil/samplefmt.h"
0b950fe2 37#include "libavutil/dict.h"
de6d9b64 38#include "avcodec.h"
3123dd79 39#include "dsputil.h"
6ed04040 40#include "libavutil/opt.h"
37b00b47 41#include "thread.h"
0ba39dd1 42#include "internal.h"
f13db94d 43#include "bytestream.h"
7246177d 44#include <stdlib.h>
9b879566 45#include <stdarg.h>
4c263142 46#include <limits.h>
860a40c8 47#include <float.h>
de6d9b64 48
419ffb23 49static int volatile entangled_thread_counter = 0;
bb875b75 50static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
f988ce6c 51static void *codec_mutex;
2d1b6fb7 52static void *avformat_mutex;
ddebfb15 53
3453a231 54void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
8e1e6f31 55{
419ffb23 56 if (min_size < *size)
8e1e6f31 57 return ptr;
115329f1 58
419ffb23 59 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
8e1e6f31 60
419ffb23
LB
61 ptr = av_realloc(ptr, min_size);
62 /* we could set this to the unmodified min_size but this is safer
63 * if the user lost the ptr and uses NULL now
64 */
65 if (!ptr)
66 min_size = 0;
372c3f82 67
419ffb23 68 *size = min_size;
978805b2
MN
69
70 return ptr;
8e1e6f31
FB
71}
72
3453a231 73void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
238ef6da
RD
74{
75 void **p = ptr;
76 if (min_size < *size)
77 return;
419ffb23 78 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
238ef6da 79 av_free(*p);
372c3f82 80 *p = av_malloc(min_size);
419ffb23
LB
81 if (!*p)
82 min_size = 0;
83 *size = min_size;
238ef6da
RD
84}
85
316fc744
JG
86void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
87{
88 void **p = ptr;
89 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
90 av_freep(p);
91 *size = 0;
92 return;
93 }
94 av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
95 if (*size)
96 memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
97}
98
de6d9b64 99/* encoder management */
e6df765e 100static AVCodec *first_avcodec = NULL;
de6d9b64 101
0a0f19b5
AK
102AVCodec *av_codec_next(const AVCodec *c)
103{
419ffb23
LB
104 if (c)
105 return c->next;
106 else
107 return first_avcodec;
55b9e69a
MN
108}
109
3211932c 110static void avcodec_init(void)
9ecfbb3e
AK
111{
112 static int initialized = 0;
113
114 if (initialized != 0)
115 return;
116 initialized = 1;
117
9cf0841e 118 ff_dsputil_static_init();
9ecfbb3e
AK
119}
120
0a0f19b5 121int av_codec_is_encoder(const AVCodec *codec)
b2c75b6e 122{
466b39ef 123 return codec && (codec->encode_sub || codec->encode2);
b2c75b6e
JR
124}
125
0a0f19b5 126int av_codec_is_decoder(const AVCodec *codec)
b2c75b6e
JR
127{
128 return codec && codec->decode;
129}
130
85662f49 131void avcodec_register(AVCodec *codec)
de6d9b64
FB
132{
133 AVCodec **p;
7a961a46 134 avcodec_init();
de6d9b64 135 p = &first_avcodec;
419ffb23
LB
136 while (*p != NULL)
137 p = &(*p)->next;
138 *p = codec;
335a761a 139 codec->next = NULL;
d97efd7f
AK
140
141 if (codec->init_static_data)
142 codec->init_static_data(codec);
de6d9b64
FB
143}
144
0fb49b59
BB
145unsigned avcodec_get_edge_width(void)
146{
147 return EDGE_WIDTH;
148}
149
419ffb23
LB
150void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
151{
152 s->coded_width = width;
153 s->coded_height = height;
154 s->width = width;
155 s->height = height;
21adafec
MN
156}
157
419ffb23 158#define INTERNAL_BUFFER_SIZE (32 + 1)
1e491e29 159
c6507946
DB
160#if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
161# define STRIDE_ALIGN 16
162#else
163# define STRIDE_ALIGN 8
164#endif
165
560f773c
JR
166void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
167 int linesize_align[AV_NUM_DATA_POINTERS])
168{
169 int i;
419ffb23
LB
170 int w_align = 1;
171 int h_align = 1;
115329f1 172
419ffb23 173 switch (s->pix_fmt) {
716d413c
AK
174 case AV_PIX_FMT_YUV420P:
175 case AV_PIX_FMT_YUYV422:
176 case AV_PIX_FMT_UYVY422:
177 case AV_PIX_FMT_YUV422P:
178 case AV_PIX_FMT_YUV440P:
179 case AV_PIX_FMT_YUV444P:
180 case AV_PIX_FMT_GBRP:
181 case AV_PIX_FMT_GRAY8:
182 case AV_PIX_FMT_GRAY16BE:
183 case AV_PIX_FMT_GRAY16LE:
184 case AV_PIX_FMT_YUVJ420P:
185 case AV_PIX_FMT_YUVJ422P:
186 case AV_PIX_FMT_YUVJ440P:
187 case AV_PIX_FMT_YUVJ444P:
188 case AV_PIX_FMT_YUVA420P:
6d5600e8
LB
189 case AV_PIX_FMT_YUVA422P:
190 case AV_PIX_FMT_YUVA444P:
716d413c
AK
191 case AV_PIX_FMT_YUV420P9LE:
192 case AV_PIX_FMT_YUV420P9BE:
193 case AV_PIX_FMT_YUV420P10LE:
194 case AV_PIX_FMT_YUV420P10BE:
195 case AV_PIX_FMT_YUV422P9LE:
196 case AV_PIX_FMT_YUV422P9BE:
197 case AV_PIX_FMT_YUV422P10LE:
198 case AV_PIX_FMT_YUV422P10BE:
199 case AV_PIX_FMT_YUV444P9LE:
200 case AV_PIX_FMT_YUV444P9BE:
201 case AV_PIX_FMT_YUV444P10LE:
202 case AV_PIX_FMT_YUV444P10BE:
203 case AV_PIX_FMT_GBRP9LE:
204 case AV_PIX_FMT_GBRP9BE:
205 case AV_PIX_FMT_GBRP10LE:
206 case AV_PIX_FMT_GBRP10BE:
37c0dc62
RC
207 w_align = 16; //FIXME assume 16 pixel per macroblock
208 h_align = 16 * 2; // interlaced needs 2 macroblocks height
f0bbfc4a 209 break;
716d413c
AK
210 case AV_PIX_FMT_YUV411P:
211 case AV_PIX_FMT_UYYVYY411:
419ffb23
LB
212 w_align = 32;
213 h_align = 8;
f0bbfc4a 214 break;
716d413c 215 case AV_PIX_FMT_YUV410P:
419ffb23
LB
216 if (s->codec_id == AV_CODEC_ID_SVQ1) {
217 w_align = 64;
218 h_align = 64;
f0bbfc4a 219 }
716d413c 220 case AV_PIX_FMT_RGB555:
419ffb23
LB
221 if (s->codec_id == AV_CODEC_ID_RPZA) {
222 w_align = 4;
223 h_align = 4;
d99fbbf4 224 }
716d413c
AK
225 case AV_PIX_FMT_PAL8:
226 case AV_PIX_FMT_BGR8:
227 case AV_PIX_FMT_RGB8:
419ffb23
LB
228 if (s->codec_id == AV_CODEC_ID_SMC) {
229 w_align = 4;
230 h_align = 4;
d99fbbf4 231 }
f0bbfc4a 232 break;
716d413c 233 case AV_PIX_FMT_BGR24:
419ffb23
LB
234 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
235 (s->codec_id == AV_CODEC_ID_ZLIB)) {
236 w_align = 4;
237 h_align = 4;
c31b8121
RT
238 }
239 break;
f0bbfc4a 240 default:
419ffb23
LB
241 w_align = 1;
242 h_align = 1;
f0bbfc4a
MN
243 break;
244 }
245
419ffb23
LB
246 *width = FFALIGN(*width, w_align);
247 *height = FFALIGN(*height, h_align);
36ef5369 248 if (s->codec_id == AV_CODEC_ID_H264)
419ffb23
LB
249 // some of the optimized chroma MC reads one line too much
250 *height += 2;
eb285cfe 251
2d9535ad 252 for (i = 0; i < 4; i++)
560f773c 253 linesize_align[i] = STRIDE_ALIGN;
eb285cfe
RD
254}
255
419ffb23
LB
256void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
257{
50ba57e0
AK
258 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
259 int chroma_shift = desc->log2_chroma_w;
560f773c 260 int linesize_align[AV_NUM_DATA_POINTERS];
eb285cfe 261 int align;
419ffb23 262
eb285cfe 263 avcodec_align_dimensions2(s, width, height, linesize_align);
419ffb23 264 align = FFMAX(linesize_align[0], linesize_align[3]);
eb285cfe
RD
265 linesize_align[1] <<= chroma_shift;
266 linesize_align[2] <<= chroma_shift;
419ffb23
LB
267 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
268 *width = FFALIGN(*width, align);
f0bbfc4a
MN
269}
270
5ee5fa02
JR
271int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
272 enum AVSampleFormat sample_fmt, const uint8_t *buf,
273 int buf_size, int align)
274{
275 int ch, planar, needed_size, ret = 0;
276
277 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
278 frame->nb_samples, sample_fmt,
279 align);
280 if (buf_size < needed_size)
281 return AVERROR(EINVAL);
282
283 planar = av_sample_fmt_is_planar(sample_fmt);
284 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
285 if (!(frame->extended_data = av_mallocz(nb_channels *
286 sizeof(*frame->extended_data))))
287 return AVERROR(ENOMEM);
288 } else {
289 frame->extended_data = frame->data;
290 }
291
292 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
293 buf, nb_channels, frame->nb_samples,
294 sample_fmt, align)) < 0) {
295 if (frame->extended_data != frame->data)
296 av_free(frame->extended_data);
297 return ret;
298 }
299 if (frame->extended_data != frame->data) {
300 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
301 frame->data[ch] = frame->extended_data[ch];
302 }
303
304 return ret;
305}
306
0eea2129
JR
307static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
308{
309 AVCodecInternal *avci = avctx->internal;
5ee5fa02 310 int buf_size, ret;
0eea2129 311
e57c4706 312 av_freep(&avci->audio_data);
0eea2129
JR
313 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
314 frame->nb_samples, avctx->sample_fmt,
c58846f3 315 0);
0eea2129
JR
316 if (buf_size < 0)
317 return AVERROR(EINVAL);
318
e57c4706
AK
319 frame->data[0] = av_mallocz(buf_size);
320 if (!frame->data[0])
321 return AVERROR(ENOMEM);
0eea2129 322
e57c4706
AK
323 ret = avcodec_fill_audio_frame(frame, avctx->channels, avctx->sample_fmt,
324 frame->data[0], buf_size, 0);
325 if (ret < 0) {
326 av_freep(&frame->data[0]);
327 return ret;
0eea2129
JR
328 }
329
e57c4706 330 avci->audio_data = frame->data[0];
0eea2129
JR
331 if (avctx->debug & FF_DEBUG_BUFFERS)
332 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
419ffb23 333 "internal audio buffer used\n", frame);
0eea2129
JR
334
335 return 0;
336}
337
338static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
339{
1e491e29 340 int i;
419ffb23
LB
341 int w = s->width;
342 int h = s->height;
d90cf87b 343 InternalBuffer *buf;
f3a29b75 344 AVCodecInternal *avci = s->internal;
0ecca7a4 345
419ffb23 346 if (pic->data[0] != NULL) {
65d999d6
MB
347 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
348 return -1;
349 }
419ffb23 350 if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
f3a29b75 351 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
65d999d6
MB
352 return -1;
353 }
1e491e29 354
419ffb23 355 if (av_image_check_size(w, h, 0, s))
0ecca7a4
MN
356 return -1;
357
f3a29b75 358 if (!avci->buffer) {
419ffb23 359 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
f3a29b75 360 sizeof(InternalBuffer));
d90cf87b 361 }
115329f1 362
f3a29b75 363 buf = &avci->buffer[avci->buffer_count];
115329f1 364
419ffb23 365 if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
560f773c 366 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
0701006e 367 av_freep(&buf->base[i]);
419ffb23 368 buf->data[i] = NULL;
0701006e
MN
369 }
370 }
371
8400b126 372 if (!buf->base[0]) {
f0bbfc4a 373 int h_chroma_shift, v_chroma_shift;
419ffb23 374 int size[4] = { 0 };
db7ae7d1 375 int tmpsize;
c9d6e847 376 int unaligned;
c7622f9a 377 AVPicture picture;
560f773c 378 int stride_align[AV_NUM_DATA_POINTERS];
50ba57e0
AK
379 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
380 const int pixel_size = desc->comp[0].step_minus1 + 1;
c7622f9a 381
c1a02e88
LB
382 av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift,
383 &v_chroma_shift);
f0bbfc4a 384
eb285cfe 385 avcodec_align_dimensions2(s, &w, &h, stride_align);
115329f1 386
419ffb23
LB
387 if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
388 w += EDGE_WIDTH * 2;
389 h += EDGE_WIDTH * 2;
1e491e29 390 }
5b67307a 391
c9d6e847
RD
392 do {
393 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
394 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
9d2e0ad8 395 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
c9d6e847 396 // increase alignment of w for next try (rhs gives the lowest bit set in w)
419ffb23 397 w += w & ~(w - 1);
db7ae7d1 398
c9d6e847 399 unaligned = 0;
419ffb23 400 for (i = 0; i < 4; i++)
c9d6e847 401 unaligned |= picture.linesize[i] % stride_align[i];
c9d6e847 402 } while (unaligned);
db7ae7d1 403
9d2e0ad8 404 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
f8c96d01
RD
405 if (tmpsize < 0)
406 return -1;
db7ae7d1 407
419ffb23
LB
408 for (i = 0; i < 3 && picture.data[i + 1]; i++)
409 size[i] = picture.data[i + 1] - picture.data[i];
cf73e32a 410 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
c7622f9a 411
c7622f9a
MN
412 memset(buf->base, 0, sizeof(buf->base));
413 memset(buf->data, 0, sizeof(buf->data));
1e491e29 414
419ffb23
LB
415 for (i = 0; i < 4 && size[i]; i++) {
416 const int h_shift = i == 0 ? 0 : h_chroma_shift;
417 const int v_shift = i == 0 ? 0 : v_chroma_shift;
1e491e29 418
419ffb23 419 buf->linesize[i] = picture.linesize[i];
1e491e29 420
419ffb23
LB
421 buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
422 if (buf->base[i] == NULL)
423 return -1;
c7622f9a 424
3491a9b2 425 // no edge if EDGE EMU or not planar YUV
419ffb23 426 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
d90cf87b 427 buf->data[i] = buf->base[i];
1e491e29 428 else
419ffb23 429 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
1e491e29 430 }
560f773c 431 for (; i < AV_NUM_DATA_POINTERS; i++) {
419ffb23 432 buf->base[i] = buf->data[i] = NULL;
560f773c
JR
433 buf->linesize[i] = 0;
434 }
419ffb23 435 if (size[1] && !size[2])
52d113ee 436 avpriv_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
419ffb23
LB
437 buf->width = s->width;
438 buf->height = s->height;
439 buf->pix_fmt = s->pix_fmt;
1e491e29
MN
440 }
441
560f773c 442 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
419ffb23
LB
443 pic->base[i] = buf->base[i];
444 pic->data[i] = buf->data[i];
445 pic->linesize[i] = buf->linesize[i];
d90cf87b 446 }
0eea2129 447 pic->extended_data = pic->data;
f3a29b75 448 avci->buffer_count++;
79de84f2 449
419ffb23 450 if (s->debug & FF_DEBUG_BUFFERS)
f3a29b75 451 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
419ffb23 452 "buffers used\n", pic, avci->buffer_count);
385c820b 453
1e491e29
MN
454 return 0;
455}
456
0eea2129
JR
457int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
458{
387bef95 459 frame->type = FF_BUFFER_TYPE_INTERNAL;
0eea2129
JR
460 switch (avctx->codec_type) {
461 case AVMEDIA_TYPE_VIDEO:
462 return video_get_buffer(avctx, frame);
463 case AVMEDIA_TYPE_AUDIO:
464 return audio_get_buffer(avctx, frame);
465 default:
466 return -1;
467 }
468}
469
594d4d5d
AK
470int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
471{
ff953fec
AK
472 switch (avctx->codec_type) {
473 case AVMEDIA_TYPE_VIDEO:
474 frame->width = avctx->width;
475 frame->height = avctx->height;
476 frame->format = avctx->pix_fmt;
477 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
478 break;
479 case AVMEDIA_TYPE_AUDIO:
480 frame->sample_rate = avctx->sample_rate;
481 frame->format = avctx->sample_fmt;
482 frame->channel_layout = avctx->channel_layout;
483 break;
484 default: return AVERROR(EINVAL);
485 }
486
487 frame->pkt_pts = avctx->pkt ? avctx->pkt->pts : AV_NOPTS_VALUE;
488 frame->reordered_opaque = avctx->reordered_opaque;
489
594d4d5d
AK
490 return avctx->get_buffer(avctx, frame);
491}
492
419ffb23
LB
493void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
494{
1e491e29 495 int i;
6829ac8d 496 InternalBuffer *buf, *last;
f3a29b75 497 AVCodecInternal *avci = s->internal;
d90cf87b 498
0eea2129
JR
499 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
500
419ffb23 501 assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
f3a29b75
JR
502 assert(avci->buffer_count);
503
504 if (avci->buffer) {
505 buf = NULL; /* avoids warning */
506 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
507 buf = &avci->buffer[i];
508 if (buf->data[0] == pic->data[0])
509 break;
510 }
511 assert(i < avci->buffer_count);
512 avci->buffer_count--;
513 last = &avci->buffer[avci->buffer_count];
d90cf87b 514
a09bb3ba
MR
515 if (buf != last)
516 FFSWAP(InternalBuffer, *buf, *last);
6a9c8594 517 }
d90cf87b 518
419ffb23
LB
519 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
520 pic->data[i] = NULL;
d90cf87b 521// pic->base[i]=NULL;
385c820b 522
419ffb23 523 if (s->debug & FF_DEBUG_BUFFERS)
f3a29b75 524 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
419ffb23 525 "buffers used\n", pic, avci->buffer_count);
1e491e29
MN
526}
527
419ffb23
LB
528int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
529{
e1c2a5a0
RT
530 AVFrame temp_pic;
531 int i;
532
0eea2129
JR
533 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
534
e1c2a5a0 535 /* If no picture return a new buffer */
419ffb23 536 if (pic->data[0] == NULL) {
e1c2a5a0
RT
537 /* We will copy from buffer, so must be readable */
538 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
594d4d5d 539 return ff_get_buffer(s, pic);
e1c2a5a0
RT
540 }
541
d10319d8 542 assert(s->pix_fmt == pic->format);
87840eeb 543
e1c2a5a0 544 /* If internal buffer type return the same buffer */
419ffb23
LB
545 if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
546 if (s->pkt)
547 pic->pkt_pts = s->pkt->pts;
548 else
549 pic->pkt_pts = AV_NOPTS_VALUE;
550 pic->reordered_opaque = s->reordered_opaque;
e1c2a5a0 551 return 0;
b8af4fe9 552 }
e1c2a5a0
RT
553
554 /*
555 * Not internal type and reget_buffer not overridden, emulate cr buffer
556 */
557 temp_pic = *pic;
419ffb23 558 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
e1c2a5a0
RT
559 pic->data[i] = pic->base[i] = NULL;
560 pic->opaque = NULL;
561 /* Allocate new frame */
594d4d5d 562 if (ff_get_buffer(s, pic))
e1c2a5a0
RT
563 return -1;
564 /* Copy image data from old buffer to new buffer */
419ffb23
LB
565 av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
566 s->height);
e1c2a5a0
RT
567 s->release_buffer(s, &temp_pic); // Release old frame
568 return 0;
569}
570
419ffb23
LB
571int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
572{
9c3d33d6
MN
573 int i;
574
419ffb23
LB
575 for (i = 0; i < count; i++) {
576 int r = func(c, (char *)arg + i * size);
577 if (ret)
578 ret[i] = r;
9c3d33d6
MN
579 }
580 return 0;
581}
582
419ffb23
LB
583int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
584{
8d23a86f
RD
585 int i;
586
419ffb23
LB
587 for (i = 0; i < count; i++) {
588 int r = func(c, arg, i, 0);
589 if (ret)
590 ret[i] = r;
8d23a86f
RD
591 }
592 return 0;
593}
594
cc085993
LB
595static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
596{
597 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
598 return desc->flags & PIX_FMT_HWACCEL;
599}
600
716d413c 601enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
419ffb23 602{
cc085993 603 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
c269cf68 604 ++fmt;
a33c7159
MN
605 return fmt[0];
606}
607
a83499b1 608void avcodec_get_frame_defaults(AVFrame *frame)
419ffb23 609{
2bc0de38
AK
610 if (frame->extended_data != frame->data)
611 av_freep(&frame->extended_data);
612
a83499b1 613 memset(frame, 0, sizeof(AVFrame));
9740beff 614
a83499b1
AK
615 frame->pts = AV_NOPTS_VALUE;
616 frame->key_frame = 1;
617 frame->sample_aspect_ratio = (AVRational) {0, 1 };
618 frame->format = -1; /* unknown */
2bc0de38 619 frame->extended_data = frame->data;
9740beff
MN
620}
621
419ffb23
LB
622AVFrame *avcodec_alloc_frame(void)
623{
c084a975 624 AVFrame *frame = av_mallocz(sizeof(AVFrame));
115329f1 625
a83499b1 626 if (frame == NULL)
419ffb23 627 return NULL;
115329f1 628
a83499b1 629 avcodec_get_frame_defaults(frame);
115329f1 630
a83499b1 631 return frame;
1e491e29
MN
632}
633
a42aadab
AK
634void avcodec_free_frame(AVFrame **frame)
635{
636 AVFrame *f;
637
638 if (!frame || !*frame)
639 return;
640
641 f = *frame;
642
643 if (f->extended_data != f->data)
644 av_freep(&f->extended_data);
645
646 av_freep(frame);
647}
648
0a0f19b5 649int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
0b950fe2 650{
31d76ec2 651 int ret = 0;
0b950fe2
AK
652 AVDictionary *tmp = NULL;
653
af08d9ae
AK
654 if (avcodec_is_open(avctx))
655 return 0;
656
bc901998
AK
657 if ((!codec && !avctx->codec)) {
658 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
659 return AVERROR(EINVAL);
660 }
661 if ((codec && avctx->codec && codec != avctx->codec)) {
662 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
419ffb23 663 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
bc901998
AK
664 return AVERROR(EINVAL);
665 }
666 if (!codec)
667 codec = avctx->codec;
668
4df30f71
AC
669 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
670 return AVERROR(EINVAL);
671
0b950fe2
AK
672 if (options)
673 av_dict_copy(&tmp, *options, 0);
115329f1 674
f988ce6c
675 /* If there is a user-supplied mutex locking routine, call it. */
676 if (ff_lockmgr_cb) {
677 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
678 return -1;
679 }
680
ddebfb15 681 entangled_thread_counter++;
419ffb23 682 if (entangled_thread_counter != 1) {
ddebfb15 683 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
31d76ec2 684 ret = -1;
ddebfb15
MN
685 goto end;
686 }
de6d9b64 687
f3a29b75
JR
688 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
689 if (!avctx->internal) {
690 ret = AVERROR(ENOMEM);
691 goto end;
692 }
693
0edf8a7a 694 if (codec->priv_data_size > 0) {
90f06cea 695 if (!avctx->priv_data) {
419ffb23
LB
696 avctx->priv_data = av_mallocz(codec->priv_data_size);
697 if (!avctx->priv_data) {
698 ret = AVERROR(ENOMEM);
699 goto end;
700 }
701 if (codec->priv_class) {
702 *(const AVClass **)avctx->priv_data = codec->priv_class;
703 av_opt_set_defaults(avctx->priv_data);
704 }
dc51a72b 705 }
419ffb23
LB
706 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
707 goto free_and_end;
0edf8a7a
PG
708 } else {
709 avctx->priv_data = NULL;
710 }
0b950fe2
AK
711 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
712 goto free_and_end;
21adafec 713
419ffb23 714 if (avctx->coded_width && avctx->coded_height)
21adafec 715 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
419ffb23 716 else if (avctx->width && avctx->height)
21adafec
MN
717 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
718
82eac2f3
RD
719 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
720 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
721 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
722 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
723 avcodec_set_dimensions(avctx, 0, 0);
724 }
725
f19c58b4 726 /* if the decoder init function was already called previously,
419ffb23 727 * free the already allocated subtitle_header before overwriting it */
44fe77b3 728 if (av_codec_is_decoder(codec))
f19c58b4
AJ
729 av_freep(&avctx->subtitle_header);
730
bb6941af 731 if (avctx->channels > FF_SANE_NB_CHANNELS) {
7868349a 732 ret = AVERROR(EINVAL);
2a9b5c9b 733 goto free_and_end;
0ecca7a4
MN
734 }
735
b5c85991 736 avctx->codec = codec;
72415b2a 737 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
36ef5369 738 avctx->codec_id == AV_CODEC_ID_NONE) {
681c180d
RD
739 avctx->codec_type = codec->type;
740 avctx->codec_id = codec->id;
741 }
e83c716e 742 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
419ffb23 743 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
4c0dda2b 744 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
31d76ec2 745 ret = AVERROR(EINVAL);
2a9b5c9b 746 goto free_and_end;
4c0dda2b 747 }
b5c85991 748 avctx->frame_number = 0;
37b00b47 749
c854102d
NC
750 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
751 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
752 ret = AVERROR_EXPERIMENTAL;
753 goto free_and_end;
754 }
755
9a7dc618
MS
756 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
757 (!avctx->time_base.num || !avctx->time_base.den)) {
758 avctx->time_base.num = 1;
759 avctx->time_base.den = avctx->sample_rate;
760 }
761
37b00b47 762 if (HAVE_THREADS && !avctx->thread_opaque) {
ba9ef8d0 763 ret = ff_thread_init(avctx);
37b00b47
AS
764 if (ret < 0) {
765 goto free_and_end;
766 }
767 }
b6064d9a
JG
768 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
769 avctx->thread_count = 1;
37b00b47 770
44fe77b3 771 if (av_codec_is_encoder(avctx->codec)) {
2cfa2d92 772 int i;
8b00ab01 773 if (avctx->codec->sample_fmts) {
37f701f1 774 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
3dfc3e70
JR
775 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
776 break;
37f701f1
JR
777 if (avctx->channels == 1 &&
778 av_get_planar_sample_fmt(avctx->sample_fmt) ==
779 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
780 avctx->sample_fmt = avctx->codec->sample_fmts[i];
781 break;
782 }
783 }
3dfc3e70
JR
784 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
785 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
786 ret = AVERROR(EINVAL);
787 goto free_and_end;
788 }
8b00ab01 789 }
dcd2b55e 790 if (avctx->codec->pix_fmts) {
716d413c 791 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
dcd2b55e
PM
792 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
793 break;
716d413c 794 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
dcd2b55e
PM
795 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
796 ret = AVERROR(EINVAL);
797 goto free_and_end;
798 }
799 }
8b00ab01
JR
800 if (avctx->codec->supported_samplerates) {
801 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
802 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
803 break;
804 if (avctx->codec->supported_samplerates[i] == 0) {
805 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
806 ret = AVERROR(EINVAL);
807 goto free_and_end;
808 }
809 }
810 if (avctx->codec->channel_layouts) {
811 if (!avctx->channel_layout) {
812 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
813 } else {
814 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
815 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
816 break;
817 if (avctx->codec->channel_layouts[i] == 0) {
818 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
819 ret = AVERROR(EINVAL);
820 goto free_and_end;
821 }
822 }
823 }
168f9e8c
JR
824 if (avctx->channel_layout && avctx->channels) {
825 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
826 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
827 ret = AVERROR(EINVAL);
828 goto free_and_end;
829 }
688b09fa
JR
830 } else if (avctx->channel_layout) {
831 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
168f9e8c 832 }
bff36075
LB
833
834 if (!avctx->rc_initial_buffer_occupancy)
835 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
2cfa2d92 836 }
0fd0ef79 837
419ffb23 838 if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
2de4f9eb
MN
839 ret = avctx->codec->init(avctx);
840 if (ret < 0) {
2a9b5c9b 841 goto free_and_end;
2de4f9eb 842 }
6e546aaa 843 }
1337de0c
JR
844
845 if (av_codec_is_decoder(avctx->codec)) {
846 /* validate channel layout from the decoder */
12e1e834
AK
847 if (avctx->channel_layout) {
848 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
849 if (!avctx->channels)
850 avctx->channels = channels;
851 else if (channels != avctx->channels) {
852 av_log(avctx, AV_LOG_WARNING,
853 "channel layout does not match number of channels\n");
854 avctx->channel_layout = 0;
855 }
1337de0c 856 }
0366664e
JR
857 if (avctx->channels && avctx->channels < 0 ||
858 avctx->channels > FF_SANE_NB_CHANNELS) {
859 ret = AVERROR(EINVAL);
860 goto free_and_end;
861 }
1337de0c 862 }
ddebfb15
MN
863end:
864 entangled_thread_counter--;
f988ce6c
865
866 /* Release any user-supplied mutex. */
867 if (ff_lockmgr_cb) {
868 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
869 }
0b950fe2
AK
870 if (options) {
871 av_dict_free(options);
872 *options = tmp;
873 }
874
ddebfb15 875 return ret;
2a9b5c9b 876free_and_end:
0b950fe2 877 av_dict_free(&tmp);
2a9b5c9b 878 av_freep(&avctx->priv_data);
f3a29b75 879 av_freep(&avctx->internal);
419ffb23 880 avctx->codec = NULL;
2a9b5c9b 881 goto end;
de6d9b64
FB
882}
883
b2c75b6e 884int ff_alloc_packet(AVPacket *avpkt, int size)
de6d9b64 885{
b2c75b6e
JR
886 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
887 return AVERROR(EINVAL);
888
889 if (avpkt->data) {
1afddbe5
AK
890 AVBufferRef *buf = avpkt->buf;
891#if FF_API_DESTRUCT_PACKET
e42e9b0e 892 void *destruct = avpkt->destruct;
1afddbe5 893#endif
b2c75b6e
JR
894
895 if (avpkt->size < size)
896 return AVERROR(EINVAL);
897
b2c75b6e 898 av_init_packet(avpkt);
1afddbe5 899#if FF_API_DESTRUCT_PACKET
e42e9b0e 900 avpkt->destruct = destruct;
1afddbe5
AK
901#endif
902 avpkt->buf = buf;
419ffb23 903 avpkt->size = size;
b2c75b6e
JR
904 return 0;
905 } else {
906 return av_new_packet(avpkt, size);
0ecca7a4 907 }
b2c75b6e
JR
908}
909
a5117a24
AK
910/**
911 * Pad last frame with silence.
912 */
913static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
914{
915 AVFrame *frame = NULL;
916 uint8_t *buf = NULL;
917 int ret;
918
919 if (!(frame = avcodec_alloc_frame()))
920 return AVERROR(ENOMEM);
921 *frame = *src;
922
923 if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
924 s->frame_size, s->sample_fmt, 0)) < 0)
925 goto fail;
926
927 if (!(buf = av_malloc(ret))) {
928 ret = AVERROR(ENOMEM);
929 goto fail;
930 }
931
932 frame->nb_samples = s->frame_size;
933 if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
934 buf, ret, 0)) < 0)
935 goto fail;
936 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
937 src->nb_samples, s->channels, s->sample_fmt)) < 0)
938 goto fail;
939 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
940 frame->nb_samples - src->nb_samples,
941 s->channels, s->sample_fmt)) < 0)
942 goto fail;
943
944 *dst = frame;
945
946 return 0;
947
948fail:
949 if (frame->extended_data != frame->data)
950 av_freep(&frame->extended_data);
951 av_freep(&buf);
952 av_freep(&frame);
953 return ret;
954}
955
b2c75b6e
JR
956int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
957 AVPacket *avpkt,
958 const AVFrame *frame,
959 int *got_packet_ptr)
960{
c22953b8 961 AVFrame tmp;
a5117a24 962 AVFrame *padded_frame = NULL;
b2c75b6e
JR
963 int ret;
964 int user_packet = !!avpkt->data;
b2c75b6e 965
52953d61
AK
966 *got_packet_ptr = 0;
967
b2c75b6e 968 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
7fb6c922 969 av_free_packet(avpkt);
b2c75b6e 970 av_init_packet(avpkt);
6f824977 971 return 0;
b2c75b6e
JR
972 }
973
c22953b8
AK
974 /* ensure that extended_data is properly set */
975 if (frame && !frame->extended_data) {
976 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
977 avctx->channels > AV_NUM_DATA_POINTERS) {
978 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
419ffb23 979 "with more than %d channels, but extended_data is not set.\n",
c22953b8
AK
980 AV_NUM_DATA_POINTERS);
981 return AVERROR(EINVAL);
982 }
983 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
984
985 tmp = *frame;
986 tmp.extended_data = tmp.data;
987 frame = &tmp;
988 }
989
b2c75b6e
JR
990 /* check for valid frame size */
991 if (frame) {
b2c75b6e 992 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
b461cd4d 993 if (frame->nb_samples > avctx->frame_size)
b2c75b6e
JR
994 return AVERROR(EINVAL);
995 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
a5117a24
AK
996 if (frame->nb_samples < avctx->frame_size &&
997 !avctx->internal->last_audio_frame) {
998 ret = pad_last_frame(avctx, &padded_frame, frame);
999 if (ret < 0)
1000 return ret;
1001
1002 frame = padded_frame;
1003 avctx->internal->last_audio_frame = 1;
1004 }
1005
0ccf051a
JR
1006 if (frame->nb_samples != avctx->frame_size) {
1007 ret = AVERROR(EINVAL);
1008 goto end;
1009 }
b2c75b6e 1010 }
b2c75b6e
JR
1011 }
1012
fa0319b4
JR
1013 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1014 if (!ret) {
1015 if (*got_packet_ptr) {
a75bc764 1016 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
70749c48
JR
1017 if (avpkt->pts == AV_NOPTS_VALUE)
1018 avpkt->pts = frame->pts;
1019 if (!avpkt->duration)
1020 avpkt->duration = ff_samples_to_time_base(avctx,
1021 frame->nb_samples);
a75bc764
JR
1022 }
1023 avpkt->dts = avpkt->pts;
b758cf73
JR
1024 } else {
1025 avpkt->size = 0;
b2c75b6e 1026 }
b2c75b6e 1027
3c6607eb 1028 if (!user_packet && avpkt->size) {
1afddbe5
AK
1029 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1030 if (ret >= 0)
1031 avpkt->data = avpkt->buf->data;
a1977e01
JR
1032 }
1033
b2c75b6e 1034 avctx->frame_number++;
a1977e01 1035 }
b2c75b6e 1036
74e10b62 1037 if (ret < 0 || !*got_packet_ptr) {
7fb6c922 1038 av_free_packet(avpkt);
74e10b62 1039 av_init_packet(avpkt);
0ccf051a 1040 goto end;
74e10b62 1041 }
7fb6c922 1042
b2c75b6e 1043 /* NOTE: if we add any audio encoders which output non-keyframe packets,
419ffb23
LB
1044 * this needs to be moved to the encoders, but for now we can do it
1045 * here to simplify things */
b2c75b6e
JR
1046 avpkt->flags |= AV_PKT_FLAG_KEY;
1047
0ccf051a 1048end:
a5117a24
AK
1049 if (padded_frame) {
1050 av_freep(&padded_frame->data[0]);
1051 if (padded_frame->extended_data != padded_frame->data)
1052 av_freep(&padded_frame->extended_data);
1053 av_freep(&padded_frame);
1054 }
1055
b2c75b6e 1056 return ret;
de6d9b64
FB
1057}
1058
a5ef830b 1059#if FF_API_OLD_ENCODE_AUDIO
b2c75b6e
JR
1060int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1061 uint8_t *buf, int buf_size,
1062 const short *samples)
1063{
1064 AVPacket pkt;
015da965 1065 AVFrame frame0 = { { 0 } };
b2c75b6e
JR
1066 AVFrame *frame;
1067 int ret, samples_size, got_packet;
1068
1069 av_init_packet(&pkt);
1070 pkt.data = buf;
1071 pkt.size = buf_size;
1072
1073 if (samples) {
1074 frame = &frame0;
1075 avcodec_get_frame_defaults(frame);
1076
1077 if (avctx->frame_size) {
1078 frame->nb_samples = avctx->frame_size;
1079 } else {
1080 /* if frame_size is not set, the number of samples must be
419ffb23 1081 * calculated from the buffer size */
b2c75b6e
JR
1082 int64_t nb_samples;
1083 if (!av_get_bits_per_sample(avctx->codec_id)) {
1084 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
419ffb23 1085 "support this codec\n");
b2c75b6e
JR
1086 return AVERROR(EINVAL);
1087 }
1088 nb_samples = (int64_t)buf_size * 8 /
1089 (av_get_bits_per_sample(avctx->codec_id) *
419ffb23 1090 avctx->channels);
b2c75b6e
JR
1091 if (nb_samples >= INT_MAX)
1092 return AVERROR(EINVAL);
1093 frame->nb_samples = nb_samples;
1094 }
1095
1096 /* it is assumed that the samples buffer is large enough based on the
419ffb23 1097 * relevant parameters */
b2c75b6e
JR
1098 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1099 frame->nb_samples,
1100 avctx->sample_fmt, 1);
1101 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1102 avctx->sample_fmt,
419ffb23 1103 (const uint8_t *)samples,
0c517644 1104 samples_size, 1)))
b2c75b6e
JR
1105 return ret;
1106
1107 /* fabricate frame pts from sample count.
419ffb23
LB
1108 * this is needed because the avcodec_encode_audio() API does not have
1109 * a way for the user to provide pts */
91a28b0e
JR
1110 frame->pts = ff_samples_to_time_base(avctx,
1111 avctx->internal->sample_count);
b2c75b6e
JR
1112 avctx->internal->sample_count += frame->nb_samples;
1113 } else {
1114 frame = NULL;
1115 }
1116
1117 got_packet = 0;
1118 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1119 if (!ret && got_packet && avctx->coded_frame) {
1120 avctx->coded_frame->pts = pkt.pts;
1121 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1122 }
1123 /* free any side data since we cannot return it */
1124 if (pkt.side_data_elems > 0) {
1125 int i;
1126 for (i = 0; i < pkt.side_data_elems; i++)
1127 av_free(pkt.side_data[i].data);
1128 av_freep(&pkt.side_data);
1129 pkt.side_data_elems = 0;
1130 }
1131
1132 if (frame && frame->extended_data != frame->data)
1133 av_free(frame->extended_data);
1134
1135 return ret ? ret : pkt.size;
1136}
419ffb23 1137
b2c75b6e
JR
1138#endif
1139
52f82a11 1140#if FF_API_OLD_ENCODE_VIDEO
5e4c7ca2 1141int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
419ffb23 1142 const AVFrame *pict)
de6d9b64 1143{
52f82a11
AK
1144 AVPacket pkt;
1145 int ret, got_packet = 0;
1146
419ffb23 1147 if (buf_size < FF_MIN_BUFFER_SIZE) {
5286d11f 1148 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
0ecca7a4
MN
1149 return -1;
1150 }
115329f1 1151
52f82a11
AK
1152 av_init_packet(&pkt);
1153 pkt.data = buf;
1154 pkt.size = buf_size;
1155
1156 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1157 if (!ret && got_packet && avctx->coded_frame) {
1158 avctx->coded_frame->pts = pkt.pts;
1159 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1160 }
1161
1162 /* free any side data since we cannot return it */
1163 if (pkt.side_data_elems > 0) {
1164 int i;
1165 for (i = 0; i < pkt.side_data_elems; i++)
1166 av_free(pkt.side_data[i].data);
1167 av_freep(&pkt.side_data);
1168 pkt.side_data_elems = 0;
1169 }
1170
1171 return ret ? ret : pkt.size;
1172}
419ffb23 1173
52f82a11
AK
1174#endif
1175
52f82a11
AK
1176int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1177 AVPacket *avpkt,
1178 const AVFrame *frame,
1179 int *got_packet_ptr)
1180{
1181 int ret;
1182 int user_packet = !!avpkt->data;
1183
d55fa6f9
AK
1184 *got_packet_ptr = 0;
1185
52f82a11 1186 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
7fb6c922 1187 av_free_packet(avpkt);
52f82a11 1188 av_init_packet(avpkt);
419ffb23 1189 avpkt->size = 0;
6f824977 1190 return 0;
52f82a11
AK
1191 }
1192
1193 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1194 return AVERROR(EINVAL);
1195
ff311c09 1196 av_assert0(avctx->codec->encode2);
52f82a11 1197
ff311c09
AK
1198 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1199 if (!ret) {
1200 if (!*got_packet_ptr)
1201 avpkt->size = 0;
1202 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1203 avpkt->pts = avpkt->dts = frame->pts;
52f82a11 1204
3c6607eb 1205 if (!user_packet && avpkt->size) {
1afddbe5
AK
1206 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1207 if (ret >= 0)
1208 avpkt->data = avpkt->buf->data;
eb727387
AK
1209 }
1210
52f82a11 1211 avctx->frame_number++;
03ca0a5b 1212 }
52f82a11 1213
7fb6c922
AK
1214 if (ret < 0 || !*got_packet_ptr)
1215 av_free_packet(avpkt);
1216
52f82a11
AK
1217 emms_c();
1218 return ret;
de6d9b64
FB
1219}
1220
115329f1 1221int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
240c1657
FB
1222 const AVSubtitle *sub)
1223{
1224 int ret;
419ffb23 1225 if (sub->start_display_time) {
9413db9e
BA
1226 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1227 return -1;
1228 }
419ffb23 1229 if (sub->num_rects == 0 || !sub->rects)
505aa6c9 1230 return -1;
466b39ef 1231 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
240c1657
FB
1232 avctx->frame_number++;
1233 return ret;
1234}
1235
867f923d
MS
1236static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1237{
1238 int size = 0;
1239 const uint8_t *data;
1240 uint32_t flags;
1241
1242 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1243 return;
1244
1245 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1246 if (!data || size < 4)
1247 return;
1248 flags = bytestream_get_le32(&data);
1249 size -= 4;
1250 if (size < 4) /* Required for any of the changes */
1251 return;
1252 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1253 avctx->channels = bytestream_get_le32(&data);
1254 size -= 4;
1255 }
1256 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1257 if (size < 8)
1258 return;
1259 avctx->channel_layout = bytestream_get_le64(&data);
1260 size -= 8;
1261 }
1262 if (size < 4)
1263 return;
1264 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1265 avctx->sample_rate = bytestream_get_le32(&data);
1266 size -= 4;
1267 }
1268 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1269 if (size < 8)
1270 return;
1271 avctx->width = bytestream_get_le32(&data);
1272 avctx->height = bytestream_get_le32(&data);
c5d907b6 1273 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
867f923d
MS
1274 size -= 8;
1275 }
1276}
1277
7a00bbad 1278int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
419ffb23
LB
1279 int *got_picture_ptr,
1280 AVPacket *avpkt)
7a00bbad 1281{
de6d9b64 1282 int ret;
115329f1 1283
419ffb23
LB
1284 *got_picture_ptr = 0;
1285 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
0ecca7a4 1286 return -1;
393cbb96
MN
1287
1288 avctx->pkt = avpkt;
c5d907b6 1289 apply_param_change(avctx, avpkt);
393cbb96 1290
1bc64c28
AK
1291 avcodec_get_frame_defaults(picture);
1292
419ffb23
LB
1293 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1294 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1295 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1296 avpkt);
37b00b47
AS
1297 else {
1298 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
419ffb23 1299 avpkt);
bd255f9f
JG
1300 picture->pkt_dts = avpkt->dts;
1301 /* get_buffer is supposed to set frame parameters */
1302 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
1303 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1304 picture->width = avctx->width;
1305 picture->height = avctx->height;
1306 picture->format = avctx->pix_fmt;
1307 }
37b00b47 1308 }
6bb925f4 1309
bb628dae 1310 emms_c(); //needed to avoid an emms_c() call before every return;
115329f1
DB
1311
1312 if (*got_picture_ptr)
934982c4 1313 avctx->frame_number++;
419ffb23
LB
1314 } else
1315 ret = 0;
934982c4 1316
b437cec1
AK
1317 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1318 * make sure it's set correctly */
1319 picture->extended_data = picture->data;
1320
de6d9b64
FB
1321 return ret;
1322}
1323
0eea2129 1324#if FF_API_OLD_DECODE_AUDIO
7a00bbad 1325int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
419ffb23
LB
1326 int *frame_size_ptr,
1327 AVPacket *avpkt)
7a00bbad 1328{
015da965 1329 AVFrame frame = { { 0 } };
0eea2129
JR
1330 int ret, got_frame = 0;
1331
1332 if (avctx->get_buffer != avcodec_default_get_buffer) {
e2ff436e 1333 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
419ffb23 1334 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
e2ff436e 1335 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
419ffb23 1336 "avcodec_decode_audio4()\n");
e2ff436e 1337 avctx->get_buffer = avcodec_default_get_buffer;
0eea2129
JR
1338 }
1339
1340 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1341
1342 if (ret >= 0 && got_frame) {
1343 int ch, plane_size;
419ffb23 1344 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
0eea2129
JR
1345 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1346 frame.nb_samples,
1347 avctx->sample_fmt, 1);
1348 if (*frame_size_ptr < data_size) {
1349 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
419ffb23 1350 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
0eea2129
JR
1351 return AVERROR(EINVAL);
1352 }
1353
1354 memcpy(samples, frame.extended_data[0], plane_size);
1355
1356 if (planar && avctx->channels > 1) {
1357 uint8_t *out = ((uint8_t *)samples) + plane_size;
1358 for (ch = 1; ch < avctx->channels; ch++) {
1359 memcpy(out, frame.extended_data[ch], plane_size);
1360 out += plane_size;
1361 }
1362 }
1363 *frame_size_ptr = data_size;
1364 } else {
1365 *frame_size_ptr = 0;
1366 }
1367 return ret;
1368}
419ffb23 1369
0eea2129
JR
1370#endif
1371
1372int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1373 AVFrame *frame,
1374 int *got_frame_ptr,
1375 AVPacket *avpkt)
1376{
b437cec1 1377 int planar, channels;
0eea2129
JR
1378 int ret = 0;
1379
1380 *got_frame_ptr = 0;
de6d9b64 1381
393cbb96
MN
1382 avctx->pkt = avpkt;
1383
6326afd5
JR
1384 if (!avpkt->data && avpkt->size) {
1385 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1386 return AVERROR(EINVAL);
1387 }
1388
f13db94d
MS
1389 apply_param_change(avctx, avpkt);
1390
1bc64c28
AK
1391 avcodec_get_frame_defaults(frame);
1392
0eea2129
JR
1393 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1394 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1395 if (ret >= 0 && *got_frame_ptr) {
1396 avctx->frame_number++;
1397 frame->pkt_dts = avpkt->dts;
8a4a5f6f
SS
1398 if (frame->format == AV_SAMPLE_FMT_NONE)
1399 frame->format = avctx->sample_fmt;
9c856d62 1400 }
ac66834c 1401 }
b437cec1
AK
1402
1403 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1404 * make sure it's set correctly; assume decoders that actually use
1405 * extended_data are doing it correctly */
1406 planar = av_sample_fmt_is_planar(frame->format);
1407 channels = av_get_channel_layout_nb_channels(frame->channel_layout);
1408 if (!(planar && channels > AV_NUM_DATA_POINTERS))
1409 frame->extended_data = frame->data;
1410
de6d9b64
FB
1411 return ret;
1412}
1413
7a00bbad 1414int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
419ffb23
LB
1415 int *got_sub_ptr,
1416 AVPacket *avpkt)
7a00bbad 1417{
240c1657
FB
1418 int ret;
1419
393cbb96 1420 avctx->pkt = avpkt;
240c1657 1421 *got_sub_ptr = 0;
7a00bbad 1422 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
240c1657
FB
1423 if (*got_sub_ptr)
1424 avctx->frame_number++;
1425 return ret;
1426}
1427
e1d7c883
RD
1428void avsubtitle_free(AVSubtitle *sub)
1429{
1430 int i;
1431
419ffb23 1432 for (i = 0; i < sub->num_rects; i++) {
8b834ac5
RD
1433 av_freep(&sub->rects[i]->pict.data[0]);
1434 av_freep(&sub->rects[i]->pict.data[1]);
1435 av_freep(&sub->rects[i]->pict.data[2]);
1436 av_freep(&sub->rects[i]->pict.data[3]);
1437 av_freep(&sub->rects[i]->text);
1438 av_freep(&sub->rects[i]->ass);
1439 av_freep(&sub->rects[i]);
e1d7c883
RD
1440 }
1441
8b834ac5 1442 av_freep(&sub->rects);
e1d7c883
RD
1443
1444 memset(sub, 0, sizeof(AVSubtitle));
1445}
1446
0752cd39 1447av_cold int avcodec_close(AVCodecContext *avctx)
de6d9b64 1448{
f988ce6c
1449 /* If there is a user-supplied mutex locking routine, call it. */
1450 if (ff_lockmgr_cb) {
1451 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1452 return -1;
1453 }
1454
ddebfb15 1455 entangled_thread_counter++;
419ffb23 1456 if (entangled_thread_counter != 1) {
ddebfb15
MN
1457 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1458 entangled_thread_counter--;
1459 return -1;
1460 }
1461
0e72ad95
AK
1462 if (avcodec_is_open(avctx)) {
1463 if (HAVE_THREADS && avctx->thread_opaque)
1464 ff_thread_free(avctx);
1465 if (avctx->codec && avctx->codec->close)
1466 avctx->codec->close(avctx);
1467 avcodec_default_free_buffers(avctx);
1468 avctx->coded_frame = NULL;
1469 av_freep(&avctx->internal);
1470 }
1471
1472 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
36773283
AK
1473 av_opt_free(avctx->priv_data);
1474 av_opt_free(avctx);
3123dd79 1475 av_freep(&avctx->priv_data);
44fe77b3 1476 if (av_codec_is_encoder(avctx->codec))
c4f267ab 1477 av_freep(&avctx->extradata);
de6d9b64 1478 avctx->codec = NULL;
37b00b47 1479 avctx->active_thread_type = 0;
ddebfb15 1480 entangled_thread_counter--;
f988ce6c
1481
1482 /* Release any user-supplied mutex. */
1483 if (ff_lockmgr_cb) {
1484 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1485 }
de6d9b64
FB
1486 return 0;
1487}
1488
a4aa20fb 1489static AVCodec *find_encdec(enum AVCodecID id, int encoder)
de6d9b64 1490{
419ffb23 1491 AVCodec *p, *experimental = NULL;
de6d9b64
FB
1492 p = first_avcodec;
1493 while (p) {
a4aa20fb
NC
1494 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1495 p->id == id) {
93ebfeea
JG
1496 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1497 experimental = p;
1498 } else
1499 return p;
1500 }
de6d9b64
FB
1501 p = p->next;
1502 }
93ebfeea 1503 return experimental;
de6d9b64
FB
1504}
1505
a4aa20fb
NC
1506AVCodec *avcodec_find_encoder(enum AVCodecID id)
1507{
1508 return find_encdec(id, 1);
1509}
1510
98f3b098
A
1511AVCodec *avcodec_find_encoder_by_name(const char *name)
1512{
1513 AVCodec *p;
fc228c90
AJ
1514 if (!name)
1515 return NULL;
98f3b098
A
1516 p = first_avcodec;
1517 while (p) {
419ffb23 1518 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
98f3b098
A
1519 return p;
1520 p = p->next;
1521 }
1522 return NULL;
1523}
1524
36ef5369 1525AVCodec *avcodec_find_decoder(enum AVCodecID id)
de6d9b64 1526{
a4aa20fb 1527 return find_encdec(id, 0);
de6d9b64
FB
1528}
1529
1530AVCodec *avcodec_find_decoder_by_name(const char *name)
1531{
1532 AVCodec *p;
fc228c90
AJ
1533 if (!name)
1534 return NULL;
de6d9b64
FB
1535 p = first_avcodec;
1536 while (p) {
419ffb23 1537 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
de6d9b64
FB
1538 return p;
1539 p = p->next;
1540 }
1541 return NULL;
1542}
1543
406aa93f 1544static int get_bit_rate(AVCodecContext *ctx)
ce34ff6b
RK
1545{
1546 int bit_rate;
1547 int bits_per_sample;
1548
419ffb23 1549 switch (ctx->codec_type) {
72415b2a 1550 case AVMEDIA_TYPE_VIDEO:
4563cf24
SS
1551 case AVMEDIA_TYPE_DATA:
1552 case AVMEDIA_TYPE_SUBTITLE:
1553 case AVMEDIA_TYPE_ATTACHMENT:
ce34ff6b
RK
1554 bit_rate = ctx->bit_rate;
1555 break;
72415b2a 1556 case AVMEDIA_TYPE_AUDIO:
ce34ff6b
RK
1557 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1558 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1559 break;
ce34ff6b
RK
1560 default:
1561 bit_rate = 0;
1562 break;
1563 }
1564 return bit_rate;
1565}
1566
7e566bbe
SS
1567size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1568{
1569 int i, len, ret = 0;
1570
70762508
SS
1571#define TAG_PRINT(x) \
1572 (((x) >= '0' && (x) <= '9') || \
1573 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
1574 ((x) == '.' || (x) == ' '))
1575
7e566bbe
SS
1576 for (i = 0; i < 4; i++) {
1577 len = snprintf(buf, buf_size,
70762508 1578 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
419ffb23
LB
1579 buf += len;
1580 buf_size = buf_size > len ? buf_size - len : 0;
1581 ret += len;
1582 codec_tag >>= 8;
7e566bbe
SS
1583 }
1584 return ret;
1585}
1586
de6d9b64
FB
1587void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1588{
1589 const char *codec_name;
2a81f4bd 1590 const char *profile = NULL;
0a0f19b5 1591 const AVCodec *p;
de6d9b64 1592 char buf1[32];
a96b68b7 1593 int bitrate;
59771f71 1594 AVRational display_aspect_ratio;
de6d9b64 1595
df8d5eaa
AC
1596 if (enc->codec)
1597 p = enc->codec;
1598 else if (encode)
de6d9b64
FB
1599 p = avcodec_find_encoder(enc->codec_id);
1600 else
1601 p = avcodec_find_decoder(enc->codec_id);
1602
1603 if (p) {
1604 codec_name = p->name;
2a81f4bd 1605 profile = av_get_profile_name(p, enc->profile);
36ef5369 1606 } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
985180a1 1607 /* fake mpeg2 transport stream codec (currently not
419ffb23 1608 * registered) */
985180a1 1609 codec_name = "mpeg2ts";
de6d9b64
FB
1610 } else if (enc->codec_name[0] != '\0') {
1611 codec_name = enc->codec_name;
1612 } else {
1613 /* output avi tags */
ab0b5378
SS
1614 char tag_buf[32];
1615 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1616 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
de6d9b64
FB
1617 codec_name = buf1;
1618 }
1619
419ffb23 1620 switch (enc->codec_type) {
72415b2a 1621 case AVMEDIA_TYPE_VIDEO:
de6d9b64
FB
1622 snprintf(buf, buf_size,
1623 "Video: %s%s",
7d1c3fc1 1624 codec_name, enc->mb_decision ? " (hq)" : "");
2a81f4bd
AH
1625 if (profile)
1626 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1627 " (%s)", profile);
716d413c 1628 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
cf087595
FB
1629 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1630 ", %s",
94bed8e5 1631 av_get_pix_fmt_name(enc->pix_fmt));
cf087595 1632 }
de6d9b64
FB
1633 if (enc->width) {
1634 snprintf(buf + strlen(buf), buf_size - strlen(buf),
21189011
MN
1635 ", %dx%d",
1636 enc->width, enc->height);
7ee4dd02 1637 if (enc->sample_aspect_ratio.num) {
cbaf50f8 1638 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
419ffb23
LB
1639 enc->width * enc->sample_aspect_ratio.num,
1640 enc->height * enc->sample_aspect_ratio.den,
1641 1024 * 1024);
cbaf50f8
BC
1642 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1643 " [PAR %d:%d DAR %d:%d]",
1644 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1645 display_aspect_ratio.num, display_aspect_ratio.den);
7ee4dd02 1646 }
419ffb23
LB
1647 if (av_log_get_level() >= AV_LOG_DEBUG) {
1648 int g = av_gcd(enc->time_base.num, enc->time_base.den);
21189011 1649 snprintf(buf + strlen(buf), buf_size - strlen(buf),
419ffb23
LB
1650 ", %d/%d",
1651 enc->time_base.num / g, enc->time_base.den / g);
21189011 1652 }
de6d9b64 1653 }
4bfad535
FB
1654 if (encode) {
1655 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1656 ", q=%d-%d", enc->qmin, enc->qmax);
1657 }
de6d9b64 1658 break;
72415b2a 1659 case AVMEDIA_TYPE_AUDIO:
de6d9b64
FB
1660 snprintf(buf, buf_size,
1661 "Audio: %s",
1662 codec_name);
2a81f4bd
AH
1663 if (profile)
1664 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1665 " (%s)", profile);
de6d9b64
FB
1666 if (enc->sample_rate) {
1667 snprintf(buf + strlen(buf), buf_size - strlen(buf),
0d72e7d0 1668 ", %d Hz", enc->sample_rate);
de6d9b64 1669 }
0d72e7d0 1670 av_strlcat(buf, ", ", buf_size);
63e8d976 1671 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
5d6e4c16 1672 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
9e82a113 1673 snprintf(buf + strlen(buf), buf_size - strlen(buf),
ba7d6e79 1674 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
9e82a113 1675 }
de6d9b64 1676 break;
72415b2a 1677 case AVMEDIA_TYPE_DATA:
985180a1 1678 snprintf(buf, buf_size, "Data: %s", codec_name);
240c1657 1679 break;
72415b2a 1680 case AVMEDIA_TYPE_SUBTITLE:
240c1657 1681 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
985180a1 1682 break;
72415b2a 1683 case AVMEDIA_TYPE_ATTACHMENT:
f8d7c9d3 1684 snprintf(buf, buf_size, "Attachment: %s", codec_name);
f8d7c9d3 1685 break;
de6d9b64 1686 default:
9fe5a7b8
MN
1687 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1688 return;
de6d9b64 1689 }
4bfad535
FB
1690 if (encode) {
1691 if (enc->flags & CODEC_FLAG_PASS1)
1692 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1693 ", pass 1");
1694 if (enc->flags & CODEC_FLAG_PASS2)
1695 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1696 ", pass 2");
1697 }
406aa93f 1698 bitrate = get_bit_rate(enc);
a96b68b7 1699 if (bitrate != 0) {
115329f1 1700 snprintf(buf + strlen(buf), buf_size - strlen(buf),
a96b68b7 1701 ", %d kb/s", bitrate / 1000);
de6d9b64
FB
1702 }
1703}
1704
060ec0a8
AH
1705const char *av_get_profile_name(const AVCodec *codec, int profile)
1706{
1707 const AVProfile *p;
1708 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1709 return NULL;
1710
1711 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1712 if (p->profile == profile)
1713 return p->name;
1714
1715 return NULL;
1716}
1717
419ffb23 1718unsigned avcodec_version(void)
156e5023 1719{
419ffb23 1720 return LIBAVCODEC_VERSION_INT;
156e5023 1721}
cf087595 1722
41600690 1723const char *avcodec_configuration(void)
c1736936 1724{
29ba0911 1725 return LIBAV_CONFIGURATION;
c1736936
DB
1726}
1727
41600690 1728const char *avcodec_license(void)
c1736936
DB
1729{
1730#define LICENSE_PREFIX "libavcodec license: "
a03be6e1 1731 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
c1736936
DB
1732}
1733
1c2a8c7f
MN
1734void avcodec_flush_buffers(AVCodecContext *avctx)
1735{
419ffb23 1736 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
37b00b47 1737 ff_thread_flush(avctx);
419ffb23 1738 else if (avctx->codec->flush)
7a06ff14 1739 avctx->codec->flush(avctx);
1c2a8c7f
MN
1740}
1741
0eea2129
JR
1742static void video_free_buffers(AVCodecContext *s)
1743{
f3a29b75 1744 AVCodecInternal *avci = s->internal;
d90cf87b
MN
1745 int i, j;
1746
f3a29b75
JR
1747 if (!avci->buffer)
1748 return;
115329f1 1749
f3a29b75
JR
1750 if (avci->buffer_count)
1751 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1752 avci->buffer_count);
419ffb23 1753 for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
f3a29b75 1754 InternalBuffer *buf = &avci->buffer[i];
419ffb23 1755 for (j = 0; j < 4; j++) {
d90cf87b 1756 av_freep(&buf->base[j]);
419ffb23 1757 buf->data[j] = NULL;
d90cf87b
MN
1758 }
1759 }
f3a29b75 1760 av_freep(&avci->buffer);
115329f1 1761
419ffb23 1762 avci->buffer_count = 0;
d90cf87b
MN
1763}
1764
0eea2129
JR
1765static void audio_free_buffers(AVCodecContext *avctx)
1766{
1767 AVCodecInternal *avci = avctx->internal;
e57c4706 1768 av_freep(&avci->audio_data);
0eea2129
JR
1769}
1770
1771void avcodec_default_free_buffers(AVCodecContext *avctx)
1772{
1773 switch (avctx->codec_type) {
1774 case AVMEDIA_TYPE_VIDEO:
1775 video_free_buffers(avctx);
1776 break;
1777 case AVMEDIA_TYPE_AUDIO:
1778 audio_free_buffers(avctx);
1779 break;
1780 default:
1781 break;
1782 }
1783}
1784
36ef5369 1785int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
6699d074 1786{
419ffb23 1787 switch (codec_id) {
36ef5369
AK
1788 case AV_CODEC_ID_ADPCM_CT:
1789 case AV_CODEC_ID_ADPCM_IMA_APC:
1790 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1791 case AV_CODEC_ID_ADPCM_IMA_WS:
1792 case AV_CODEC_ID_ADPCM_G722:
1793 case AV_CODEC_ID_ADPCM_YAMAHA:
f32fd318 1794 return 4;
36ef5369
AK
1795 case AV_CODEC_ID_PCM_ALAW:
1796 case AV_CODEC_ID_PCM_MULAW:
1797 case AV_CODEC_ID_PCM_S8:
1798 case AV_CODEC_ID_PCM_U8:
1799 case AV_CODEC_ID_PCM_ZORK:
ac3e1834 1800 return 8;
36ef5369
AK
1801 case AV_CODEC_ID_PCM_S16BE:
1802 case AV_CODEC_ID_PCM_S16LE:
1803 case AV_CODEC_ID_PCM_S16LE_PLANAR:
1804 case AV_CODEC_ID_PCM_U16BE:
1805 case AV_CODEC_ID_PCM_U16LE:
ac3e1834 1806 return 16;
36ef5369
AK
1807 case AV_CODEC_ID_PCM_S24DAUD:
1808 case AV_CODEC_ID_PCM_S24BE:
1809 case AV_CODEC_ID_PCM_S24LE:
1810 case AV_CODEC_ID_PCM_U24BE:
1811 case AV_CODEC_ID_PCM_U24LE:
ac3e1834 1812 return 24;
36ef5369
AK
1813 case AV_CODEC_ID_PCM_S32BE:
1814 case AV_CODEC_ID_PCM_S32LE:
1815 case AV_CODEC_ID_PCM_U32BE:
1816 case AV_CODEC_ID_PCM_U32LE:
1817 case AV_CODEC_ID_PCM_F32BE:
1818 case AV_CODEC_ID_PCM_F32LE:
ac3e1834 1819 return 32;
36ef5369
AK
1820 case AV_CODEC_ID_PCM_F64BE:
1821 case AV_CODEC_ID_PCM_F64LE:
143a5d6f 1822 return 64;
ac3e1834
BC
1823 default:
1824 return 0;
1825 }
1826}
1827
36ef5369 1828int av_get_bits_per_sample(enum AVCodecID codec_id)
6699d074
JR
1829{
1830 switch (codec_id) {
36ef5369 1831 case AV_CODEC_ID_ADPCM_SBPRO_2:
6699d074 1832 return 2;
36ef5369 1833 case AV_CODEC_ID_ADPCM_SBPRO_3:
6699d074 1834 return 3;
36ef5369
AK
1835 case AV_CODEC_ID_ADPCM_SBPRO_4:
1836 case AV_CODEC_ID_ADPCM_IMA_WAV:
1837 case AV_CODEC_ID_ADPCM_IMA_QT:
1838 case AV_CODEC_ID_ADPCM_SWF:
1839 case AV_CODEC_ID_ADPCM_MS:
6699d074
JR
1840 return 4;
1841 default:
1842 return av_get_exact_bits_per_sample(codec_id);
1843 }
1844}
1845
9524cf79
JR
1846int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
1847{
1848 int id, sr, ch, ba, tag, bps;
1849
1850 id = avctx->codec_id;
1851 sr = avctx->sample_rate;
1852 ch = avctx->channels;
1853 ba = avctx->block_align;
1854 tag = avctx->codec_tag;
1855 bps = av_get_exact_bits_per_sample(avctx->codec_id);
1856
1857 /* codecs with an exact constant bits per sample */
1858 if (bps > 0 && ch > 0 && frame_bytes > 0)
1859 return (frame_bytes * 8) / (bps * ch);
1860 bps = avctx->bits_per_coded_sample;
1861
1862 /* codecs with a fixed packet duration */
1863 switch (id) {
36ef5369
AK
1864 case AV_CODEC_ID_ADPCM_ADX: return 32;
1865 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
1866 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
1867 case AV_CODEC_ID_AMR_NB:
1868 case AV_CODEC_ID_GSM:
1869 case AV_CODEC_ID_QCELP:
1870 case AV_CODEC_ID_RA_144:
1871 case AV_CODEC_ID_RA_288: return 160;
1872 case AV_CODEC_ID_IMC: return 256;
1873 case AV_CODEC_ID_AMR_WB:
1874 case AV_CODEC_ID_GSM_MS: return 320;
1875 case AV_CODEC_ID_MP1: return 384;
1876 case AV_CODEC_ID_ATRAC1: return 512;
1877 case AV_CODEC_ID_ATRAC3: return 1024;
1878 case AV_CODEC_ID_MP2:
1879 case AV_CODEC_ID_MUSEPACK7: return 1152;
1880 case AV_CODEC_ID_AC3: return 1536;
9524cf79
JR
1881 }
1882
1883 if (sr > 0) {
1884 /* calc from sample rate */
36ef5369 1885 if (id == AV_CODEC_ID_TTA)
9524cf79
JR
1886 return 256 * sr / 245;
1887
1888 if (ch > 0) {
1889 /* calc from sample rate and channels */
36ef5369 1890 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
9524cf79
JR
1891 return (480 << (sr / 22050)) / ch;
1892 }
1893 }
1894
1895 if (ba > 0) {
1896 /* calc from block_align */
36ef5369 1897 if (id == AV_CODEC_ID_SIPR) {
9524cf79
JR
1898 switch (ba) {
1899 case 20: return 160;
1900 case 19: return 144;
1901 case 29: return 288;
1902 case 37: return 480;
1903 }
36ef5369 1904 } else if (id == AV_CODEC_ID_ILBC) {
3641b048
MS
1905 switch (ba) {
1906 case 38: return 160;
1907 case 50: return 240;
1908 }
9524cf79
JR
1909 }
1910 }
1911
1912 if (frame_bytes > 0) {
1913 /* calc from frame_bytes only */
36ef5369 1914 if (id == AV_CODEC_ID_TRUESPEECH)
9524cf79 1915 return 240 * (frame_bytes / 32);
36ef5369 1916 if (id == AV_CODEC_ID_NELLYMOSER)
9524cf79
JR
1917 return 256 * (frame_bytes / 64);
1918
1919 if (bps > 0) {
1920 /* calc from frame_bytes and bits_per_coded_sample */
36ef5369 1921 if (id == AV_CODEC_ID_ADPCM_G726)
9524cf79
JR
1922 return frame_bytes * 8 / bps;
1923 }
1924
1925 if (ch > 0) {
1926 /* calc from frame_bytes and channels */
1927 switch (id) {
36ef5369
AK
1928 case AV_CODEC_ID_ADPCM_4XM:
1929 case AV_CODEC_ID_ADPCM_IMA_ISS:
9524cf79 1930 return (frame_bytes - 4 * ch) * 2 / ch;
36ef5369 1931 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
9524cf79 1932 return (frame_bytes - 4) * 2 / ch;
36ef5369 1933 case AV_CODEC_ID_ADPCM_IMA_AMV:
9524cf79 1934 return (frame_bytes - 8) * 2 / ch;
36ef5369 1935 case AV_CODEC_ID_ADPCM_XA:
9524cf79 1936 return (frame_bytes / 128) * 224 / ch;
36ef5369 1937 case AV_CODEC_ID_INTERPLAY_DPCM:
9524cf79 1938 return (frame_bytes - 6 - ch) / ch;
36ef5369 1939 case AV_CODEC_ID_ROQ_DPCM:
9524cf79 1940 return (frame_bytes - 8) / ch;
36ef5369 1941 case AV_CODEC_ID_XAN_DPCM:
9524cf79 1942 return (frame_bytes - 2 * ch) / ch;
36ef5369 1943 case AV_CODEC_ID_MACE3:
9524cf79 1944 return 3 * frame_bytes / ch;
36ef5369 1945 case AV_CODEC_ID_MACE6:
9524cf79 1946 return 6 * frame_bytes / ch;
36ef5369 1947 case AV_CODEC_ID_PCM_LXF:
9524cf79
JR
1948 return 2 * (frame_bytes / (5 * ch));
1949 }
1950
1951 if (tag) {
1952 /* calc from frame_bytes, channels, and codec_tag */
36ef5369 1953 if (id == AV_CODEC_ID_SOL_DPCM) {
9524cf79
JR
1954 if (tag == 3)
1955 return frame_bytes / ch;
1956 else
1957 return frame_bytes * 2 / ch;
1958 }
1959 }
1960
1961 if (ba > 0) {
1962 /* calc from frame_bytes, channels, and block_align */
1963 int blocks = frame_bytes / ba;
1964 switch (avctx->codec_id) {
36ef5369 1965 case AV_CODEC_ID_ADPCM_IMA_WAV:
9524cf79 1966 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
36ef5369 1967 case AV_CODEC_ID_ADPCM_IMA_DK3:
c346f630 1968 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
36ef5369 1969 case AV_CODEC_ID_ADPCM_IMA_DK4:
9524cf79 1970 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
36ef5369 1971 case AV_CODEC_ID_ADPCM_MS:
9524cf79
JR
1972 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
1973 }
1974 }
1975
1976 if (bps > 0) {
1977 /* calc from frame_bytes, channels, and bits_per_coded_sample */
1978 switch (avctx->codec_id) {
36ef5369 1979 case AV_CODEC_ID_PCM_DVD:
9524cf79 1980 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
36ef5369 1981 case AV_CODEC_ID_PCM_BLURAY:
9524cf79 1982 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
36ef5369 1983 case AV_CODEC_ID_S302M:
9524cf79
JR
1984 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
1985 }
1986 }
1987 }
1988 }
1989
1990 return 0;
1991}
1992
b250f9c6 1993#if !HAVE_THREADS
419ffb23
LB
1994int ff_thread_init(AVCodecContext *s)
1995{
ca8ad847
MN
1996 return -1;
1997}
419ffb23 1998
ca8ad847 1999#endif
ad2b531d
MR
2000
2001unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2002{
2003 unsigned int n = 0;
2004
419ffb23 2005 while (v >= 0xff) {
ad2b531d
MR
2006 *s++ = 0xff;
2007 v -= 0xff;
2008 n++;
2009 }
2010 *s = v;
2011 n++;
2012 return n;
2013}
1005f542 2014
419ffb23
LB
2015int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2016{
c46eeae2 2017 int i;
419ffb23 2018 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
c46eeae2
MN
2019 return i;
2020}
2021
ce863d7f 2022void av_log_missing_feature(void *avc, const char *feature, int want_sample)
ea779d91 2023{
f75f4194 2024 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
db323491 2025 "version to the newest one from Git. If the problem still "
ea779d91 2026 "occurs, it means that your file has a feature which has not "
3fd3632f 2027 "been implemented.\n", feature);
ea779d91 2028 if(want_sample)
ce863d7f 2029 av_log_ask_for_sample(avc, NULL);
0ba39dd1
KG
2030}
2031
44f566b7 2032void av_log_ask_for_sample(void *avc, const char *msg, ...)
0ba39dd1 2033{
44f566b7
DB
2034 va_list argument_list;
2035
2036 va_start(argument_list, msg);
2037
0ba39dd1 2038 if (msg)
44f566b7 2039 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
0ba39dd1 2040 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
f0a41afd 2041 "of this file to ftp://upload.libav.org/incoming/ "
21de9204 2042 "and contact the libav-devel mailing list.\n");
44f566b7
DB
2043
2044 va_end(argument_list);
ea779d91 2045}
c895618b
MN
2046
2047static AVHWAccel *first_hwaccel = NULL;
2048
2049void av_register_hwaccel(AVHWAccel *hwaccel)
2050{
2051 AVHWAccel **p = &first_hwaccel;
2052 while (*p)
2053 p = &(*p)->next;
2054 *p = hwaccel;
2055 hwaccel->next = NULL;
2056}
414d9d7f
MN
2057
2058AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2059{
2060 return hwaccel ? hwaccel->next : first_hwaccel;
2061}
6059f13c 2062
716d413c 2063AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
6059f13c 2064{
419ffb23 2065 AVHWAccel *hwaccel = NULL;
6059f13c 2066
419ffb23
LB
2067 while ((hwaccel = av_hwaccel_next(hwaccel)))
2068 if (hwaccel->id == codec_id
6059f13c
MN
2069 && hwaccel->pix_fmt == pix_fmt)
2070 return hwaccel;
6059f13c
MN
2071 return NULL;
2072}
f988ce6c
2073
2074int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2075{
2076 if (ff_lockmgr_cb) {
2077 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2078 return -1;
2d1b6fb7
MS
2079 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2080 return -1;
f988ce6c
2081 }
2082
2083 ff_lockmgr_cb = cb;
2084
2085 if (ff_lockmgr_cb) {
2086 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2087 return -1;
2d1b6fb7
MS
2088 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2089 return -1;
2090 }
2091 return 0;
2092}
2093
2094int avpriv_lock_avformat(void)
2095{
2096 if (ff_lockmgr_cb) {
2097 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2098 return -1;
2099 }
2100 return 0;
2101}
2102
2103int avpriv_unlock_avformat(void)
2104{
2105 if (ff_lockmgr_cb) {
2106 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2107 return -1;
f988ce6c
2108 }
2109 return 0;
2110}
603a5f04 2111
0842d589 2112unsigned int avpriv_toupper4(unsigned int x)
603a5f04 2113{
efa7f420
RD
2114 return av_toupper(x & 0xFF) +
2115 (av_toupper((x >> 8) & 0xFF) << 8) +
2116 (av_toupper((x >> 16) & 0xFF) << 16) +
2117 (av_toupper((x >> 24) & 0xFF) << 24);
603a5f04 2118}
37b00b47 2119
27237d52 2120#if !HAVE_THREADS
37b00b47
AS
2121
2122int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
2123{
2124 f->owner = avctx;
594d4d5d 2125 return ff_get_buffer(avctx, f);
37b00b47
AS
2126}
2127
2128void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
2129{
2130 f->owner->release_buffer(f->owner, f);
2131}
2132
2133void ff_thread_finish_setup(AVCodecContext *avctx)
2134{
2135}
2136
2137void ff_thread_report_progress(AVFrame *f, int progress, int field)
2138{
2139}
2140
2141void ff_thread_await_progress(AVFrame *f, int progress, int field)
2142{
2143}
2144
2145#endif
65af48b5 2146
36ef5369 2147enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
bca06e77 2148{
36ef5369 2149 if (codec_id <= AV_CODEC_ID_NONE)
bca06e77 2150 return AVMEDIA_TYPE_UNKNOWN;
36ef5369 2151 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
bca06e77 2152 return AVMEDIA_TYPE_VIDEO;
36ef5369 2153 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
bca06e77 2154 return AVMEDIA_TYPE_AUDIO;
36ef5369 2155 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
bca06e77
AK
2156 return AVMEDIA_TYPE_SUBTITLE;
2157
2158 return AVMEDIA_TYPE_UNKNOWN;
2159}
af08d9ae
AK
2160
2161int avcodec_is_open(AVCodecContext *s)
2162{
2163 return !!s->internal;
2164}