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