samplefmt: add a function for filling a buffer with silence.
[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
860int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
861 AVPacket *avpkt,
862 const AVFrame *frame,
863 int *got_packet_ptr)
864{
865 int ret;
866 int user_packet = !!avpkt->data;
b2c75b6e 867
52953d61
AK
868 *got_packet_ptr = 0;
869
b2c75b6e 870 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
7fb6c922 871 av_free_packet(avpkt);
b2c75b6e 872 av_init_packet(avpkt);
6f824977 873 return 0;
b2c75b6e
JR
874 }
875
876 /* check for valid frame size */
877 if (frame) {
b2c75b6e 878 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
b461cd4d 879 if (frame->nb_samples > avctx->frame_size)
b2c75b6e
JR
880 return AVERROR(EINVAL);
881 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
b461cd4d 882 if (frame->nb_samples != avctx->frame_size)
b2c75b6e
JR
883 return AVERROR(EINVAL);
884 }
b2c75b6e
JR
885 }
886
fa0319b4
JR
887 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
888 if (!ret) {
889 if (*got_packet_ptr) {
a75bc764 890 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
70749c48
JR
891 if (avpkt->pts == AV_NOPTS_VALUE)
892 avpkt->pts = frame->pts;
893 if (!avpkt->duration)
894 avpkt->duration = ff_samples_to_time_base(avctx,
895 frame->nb_samples);
a75bc764
JR
896 }
897 avpkt->dts = avpkt->pts;
b758cf73
JR
898 } else {
899 avpkt->size = 0;
b2c75b6e 900 }
b2c75b6e 901
3c6607eb 902 if (!user_packet && avpkt->size) {
a1977e01
JR
903 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
904 if (new_data)
905 avpkt->data = new_data;
906 }
907
b2c75b6e 908 avctx->frame_number++;
a1977e01 909 }
b2c75b6e 910
74e10b62 911 if (ret < 0 || !*got_packet_ptr) {
7fb6c922 912 av_free_packet(avpkt);
74e10b62
JR
913 av_init_packet(avpkt);
914 return ret;
915 }
7fb6c922 916
b2c75b6e
JR
917 /* NOTE: if we add any audio encoders which output non-keyframe packets,
918 this needs to be moved to the encoders, but for now we can do it
919 here to simplify things */
920 avpkt->flags |= AV_PKT_FLAG_KEY;
921
922 return ret;
de6d9b64
FB
923}
924
b2c75b6e
JR
925#if FF_API_OLD_DECODE_AUDIO
926int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
927 uint8_t *buf, int buf_size,
928 const short *samples)
929{
930 AVPacket pkt;
931 AVFrame frame0;
932 AVFrame *frame;
933 int ret, samples_size, got_packet;
934
935 av_init_packet(&pkt);
936 pkt.data = buf;
937 pkt.size = buf_size;
938
939 if (samples) {
940 frame = &frame0;
941 avcodec_get_frame_defaults(frame);
942
943 if (avctx->frame_size) {
944 frame->nb_samples = avctx->frame_size;
945 } else {
946 /* if frame_size is not set, the number of samples must be
947 calculated from the buffer size */
948 int64_t nb_samples;
949 if (!av_get_bits_per_sample(avctx->codec_id)) {
950 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
951 "support this codec\n");
952 return AVERROR(EINVAL);
953 }
954 nb_samples = (int64_t)buf_size * 8 /
955 (av_get_bits_per_sample(avctx->codec_id) *
956 avctx->channels);
957 if (nb_samples >= INT_MAX)
958 return AVERROR(EINVAL);
959 frame->nb_samples = nb_samples;
960 }
961
962 /* it is assumed that the samples buffer is large enough based on the
963 relevant parameters */
964 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
965 frame->nb_samples,
966 avctx->sample_fmt, 1);
967 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
968 avctx->sample_fmt,
969 samples, samples_size, 1)))
970 return ret;
971
972 /* fabricate frame pts from sample count.
973 this is needed because the avcodec_encode_audio() API does not have
974 a way for the user to provide pts */
91a28b0e
JR
975 frame->pts = ff_samples_to_time_base(avctx,
976 avctx->internal->sample_count);
b2c75b6e
JR
977 avctx->internal->sample_count += frame->nb_samples;
978 } else {
979 frame = NULL;
980 }
981
982 got_packet = 0;
983 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
984 if (!ret && got_packet && avctx->coded_frame) {
985 avctx->coded_frame->pts = pkt.pts;
986 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
987 }
988 /* free any side data since we cannot return it */
989 if (pkt.side_data_elems > 0) {
990 int i;
991 for (i = 0; i < pkt.side_data_elems; i++)
992 av_free(pkt.side_data[i].data);
993 av_freep(&pkt.side_data);
994 pkt.side_data_elems = 0;
995 }
996
997 if (frame && frame->extended_data != frame->data)
998 av_free(frame->extended_data);
999
1000 return ret ? ret : pkt.size;
1001}
1002#endif
1003
52f82a11 1004#if FF_API_OLD_ENCODE_VIDEO
5e4c7ca2 1005int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
492cd3a9 1006 const AVFrame *pict)
de6d9b64 1007{
52f82a11
AK
1008 AVPacket pkt;
1009 int ret, got_packet = 0;
1010
0ecca7a4 1011 if(buf_size < FF_MIN_BUFFER_SIZE){
5286d11f 1012 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
0ecca7a4
MN
1013 return -1;
1014 }
115329f1 1015
52f82a11
AK
1016 av_init_packet(&pkt);
1017 pkt.data = buf;
1018 pkt.size = buf_size;
1019
1020 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1021 if (!ret && got_packet && avctx->coded_frame) {
1022 avctx->coded_frame->pts = pkt.pts;
1023 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1024 }
1025
1026 /* free any side data since we cannot return it */
1027 if (pkt.side_data_elems > 0) {
1028 int i;
1029 for (i = 0; i < pkt.side_data_elems; i++)
1030 av_free(pkt.side_data[i].data);
1031 av_freep(&pkt.side_data);
1032 pkt.side_data_elems = 0;
1033 }
1034
1035 return ret ? ret : pkt.size;
1036}
1037#endif
1038
52f82a11
AK
1039int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1040 AVPacket *avpkt,
1041 const AVFrame *frame,
1042 int *got_packet_ptr)
1043{
1044 int ret;
1045 int user_packet = !!avpkt->data;
1046
d55fa6f9
AK
1047 *got_packet_ptr = 0;
1048
52f82a11 1049 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
7fb6c922 1050 av_free_packet(avpkt);
52f82a11
AK
1051 av_init_packet(avpkt);
1052 avpkt->size = 0;
6f824977 1053 return 0;
52f82a11
AK
1054 }
1055
1056 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1057 return AVERROR(EINVAL);
1058
ff311c09 1059 av_assert0(avctx->codec->encode2);
52f82a11 1060
ff311c09
AK
1061 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1062 if (!ret) {
1063 if (!*got_packet_ptr)
1064 avpkt->size = 0;
1065 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1066 avpkt->pts = avpkt->dts = frame->pts;
52f82a11 1067
3c6607eb 1068 if (!user_packet && avpkt->size) {
eb727387
AK
1069 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
1070 if (new_data)
1071 avpkt->data = new_data;
1072 }
1073
52f82a11 1074 avctx->frame_number++;
03ca0a5b 1075 }
52f82a11 1076
7fb6c922
AK
1077 if (ret < 0 || !*got_packet_ptr)
1078 av_free_packet(avpkt);
1079
52f82a11
AK
1080 emms_c();
1081 return ret;
de6d9b64
FB
1082}
1083
115329f1 1084int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
240c1657
FB
1085 const AVSubtitle *sub)
1086{
1087 int ret;
9413db9e
BA
1088 if(sub->start_display_time) {
1089 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1090 return -1;
1091 }
505aa6c9
BA
1092 if(sub->num_rects == 0 || !sub->rects)
1093 return -1;
8eb027c8 1094 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
240c1657
FB
1095 avctx->frame_number++;
1096 return ret;
1097}
1098
867f923d
MS
1099static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1100{
1101 int size = 0;
1102 const uint8_t *data;
1103 uint32_t flags;
1104
1105 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1106 return;
1107
1108 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1109 if (!data || size < 4)
1110 return;
1111 flags = bytestream_get_le32(&data);
1112 size -= 4;
1113 if (size < 4) /* Required for any of the changes */
1114 return;
1115 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1116 avctx->channels = bytestream_get_le32(&data);
1117 size -= 4;
1118 }
1119 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1120 if (size < 8)
1121 return;
1122 avctx->channel_layout = bytestream_get_le64(&data);
1123 size -= 8;
1124 }
1125 if (size < 4)
1126 return;
1127 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1128 avctx->sample_rate = bytestream_get_le32(&data);
1129 size -= 4;
1130 }
1131 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1132 if (size < 8)
1133 return;
1134 avctx->width = bytestream_get_le32(&data);
1135 avctx->height = bytestream_get_le32(&data);
c5d907b6 1136 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
867f923d
MS
1137 size -= 8;
1138 }
1139}
1140
7a00bbad
TB
1141int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1142 int *got_picture_ptr,
1143 AVPacket *avpkt)
1144{
de6d9b64 1145 int ret;
115329f1 1146
53db1cae 1147 *got_picture_ptr= 0;
e16f217c 1148 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
0ecca7a4 1149 return -1;
393cbb96
MN
1150
1151 avctx->pkt = avpkt;
c5d907b6 1152 apply_param_change(avctx, avpkt);
393cbb96 1153
37b00b47 1154 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
27237d52 1155 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
37b00b47
AS
1156 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1157 avpkt);
1158 else {
1159 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1160 avpkt);
1161 picture->pkt_dts= avpkt->dts;
b58dbb5b 1162 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
3a2ddf7c
SS
1163 picture->width = avctx->width;
1164 picture->height = avctx->height;
8a4a5f6f 1165 picture->format = avctx->pix_fmt;
37b00b47 1166 }
6bb925f4 1167
bb628dae 1168 emms_c(); //needed to avoid an emms_c() call before every return;
115329f1
DB
1169
1170 if (*got_picture_ptr)
934982c4
MN
1171 avctx->frame_number++;
1172 }else
1173 ret= 0;
1174
de6d9b64
FB
1175 return ret;
1176}
1177
0eea2129 1178#if FF_API_OLD_DECODE_AUDIO
7a00bbad
TB
1179int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1180 int *frame_size_ptr,
1181 AVPacket *avpkt)
1182{
0eea2129
JR
1183 AVFrame frame;
1184 int ret, got_frame = 0;
1185
1186 if (avctx->get_buffer != avcodec_default_get_buffer) {
e2ff436e
RT
1187 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1188 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1189 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1190 "avcodec_decode_audio4()\n");
1191 avctx->get_buffer = avcodec_default_get_buffer;
0eea2129
JR
1192 }
1193
1194 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1195
1196 if (ret >= 0 && got_frame) {
1197 int ch, plane_size;
1198 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1199 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1200 frame.nb_samples,
1201 avctx->sample_fmt, 1);
1202 if (*frame_size_ptr < data_size) {
1203 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1204 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1205 return AVERROR(EINVAL);
1206 }
1207
1208 memcpy(samples, frame.extended_data[0], plane_size);
1209
1210 if (planar && avctx->channels > 1) {
1211 uint8_t *out = ((uint8_t *)samples) + plane_size;
1212 for (ch = 1; ch < avctx->channels; ch++) {
1213 memcpy(out, frame.extended_data[ch], plane_size);
1214 out += plane_size;
1215 }
1216 }
1217 *frame_size_ptr = data_size;
1218 } else {
1219 *frame_size_ptr = 0;
1220 }
1221 return ret;
1222}
1223#endif
1224
1225int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1226 AVFrame *frame,
1227 int *got_frame_ptr,
1228 AVPacket *avpkt)
1229{
1230 int ret = 0;
1231
1232 *got_frame_ptr = 0;
de6d9b64 1233
393cbb96
MN
1234 avctx->pkt = avpkt;
1235
6326afd5
JR
1236 if (!avpkt->data && avpkt->size) {
1237 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1238 return AVERROR(EINVAL);
1239 }
1240
f13db94d
MS
1241 apply_param_change(avctx, avpkt);
1242
0eea2129
JR
1243 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1244 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1245 if (ret >= 0 && *got_frame_ptr) {
1246 avctx->frame_number++;
1247 frame->pkt_dts = avpkt->dts;
8a4a5f6f
SS
1248 if (frame->format == AV_SAMPLE_FMT_NONE)
1249 frame->format = avctx->sample_fmt;
9c856d62 1250 }
ac66834c 1251 }
de6d9b64
FB
1252 return ret;
1253}
1254
7a00bbad
TB
1255int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1256 int *got_sub_ptr,
1257 AVPacket *avpkt)
1258{
240c1657
FB
1259 int ret;
1260
393cbb96 1261 avctx->pkt = avpkt;
240c1657 1262 *got_sub_ptr = 0;
7a00bbad 1263 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
240c1657
FB
1264 if (*got_sub_ptr)
1265 avctx->frame_number++;
1266 return ret;
1267}
1268
e1d7c883
RD
1269void avsubtitle_free(AVSubtitle *sub)
1270{
1271 int i;
1272
1273 for (i = 0; i < sub->num_rects; i++)
1274 {
8b834ac5
RD
1275 av_freep(&sub->rects[i]->pict.data[0]);
1276 av_freep(&sub->rects[i]->pict.data[1]);
1277 av_freep(&sub->rects[i]->pict.data[2]);
1278 av_freep(&sub->rects[i]->pict.data[3]);
1279 av_freep(&sub->rects[i]->text);
1280 av_freep(&sub->rects[i]->ass);
1281 av_freep(&sub->rects[i]);
e1d7c883
RD
1282 }
1283
8b834ac5 1284 av_freep(&sub->rects);
e1d7c883
RD
1285
1286 memset(sub, 0, sizeof(AVSubtitle));
1287}
1288
0752cd39 1289av_cold int avcodec_close(AVCodecContext *avctx)
de6d9b64 1290{
f988ce6c
1291 /* If there is a user-supplied mutex locking routine, call it. */
1292 if (ff_lockmgr_cb) {
1293 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1294 return -1;
1295 }
1296
ddebfb15
MN
1297 entangled_thread_counter++;
1298 if(entangled_thread_counter != 1){
1299 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1300 entangled_thread_counter--;
1301 return -1;
1302 }
1303
0e72ad95
AK
1304 if (avcodec_is_open(avctx)) {
1305 if (HAVE_THREADS && avctx->thread_opaque)
1306 ff_thread_free(avctx);
1307 if (avctx->codec && avctx->codec->close)
1308 avctx->codec->close(avctx);
1309 avcodec_default_free_buffers(avctx);
1310 avctx->coded_frame = NULL;
1311 av_freep(&avctx->internal);
1312 }
1313
1314 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
36773283
AK
1315 av_opt_free(avctx->priv_data);
1316 av_opt_free(avctx);
3123dd79 1317 av_freep(&avctx->priv_data);
44fe77b3 1318 if (av_codec_is_encoder(avctx->codec))
c4f267ab 1319 av_freep(&avctx->extradata);
de6d9b64 1320 avctx->codec = NULL;
37b00b47 1321 avctx->active_thread_type = 0;
ddebfb15 1322 entangled_thread_counter--;
f988ce6c
1323
1324 /* Release any user-supplied mutex. */
1325 if (ff_lockmgr_cb) {
1326 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1327 }
de6d9b64
FB
1328 return 0;
1329}
1330
1331AVCodec *avcodec_find_encoder(enum CodecID id)
1332{
93ebfeea 1333 AVCodec *p, *experimental=NULL;
de6d9b64
FB
1334 p = first_avcodec;
1335 while (p) {
44fe77b3 1336 if (av_codec_is_encoder(p) && p->id == id) {
93ebfeea
JG
1337 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1338 experimental = p;
1339 } else
1340 return p;
1341 }
de6d9b64
FB
1342 p = p->next;
1343 }
93ebfeea 1344 return experimental;
de6d9b64
FB
1345}
1346
98f3b098
A
1347AVCodec *avcodec_find_encoder_by_name(const char *name)
1348{
1349 AVCodec *p;
fc228c90
AJ
1350 if (!name)
1351 return NULL;
98f3b098
A
1352 p = first_avcodec;
1353 while (p) {
44fe77b3 1354 if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
98f3b098
A
1355 return p;
1356 p = p->next;
1357 }
1358 return NULL;
1359}
1360
de6d9b64
FB
1361AVCodec *avcodec_find_decoder(enum CodecID id)
1362{
1363 AVCodec *p;
1364 p = first_avcodec;
1365 while (p) {
44fe77b3 1366 if (av_codec_is_decoder(p) && p->id == id)
de6d9b64
FB
1367 return p;
1368 p = p->next;
1369 }
1370 return NULL;
1371}
1372
1373AVCodec *avcodec_find_decoder_by_name(const char *name)
1374{
1375 AVCodec *p;
fc228c90
AJ
1376 if (!name)
1377 return NULL;
de6d9b64
FB
1378 p = first_avcodec;
1379 while (p) {
44fe77b3 1380 if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
de6d9b64
FB
1381 return p;
1382 p = p->next;
1383 }
1384 return NULL;
1385}
1386
406aa93f 1387static int get_bit_rate(AVCodecContext *ctx)
ce34ff6b
RK
1388{
1389 int bit_rate;
1390 int bits_per_sample;
1391
1392 switch(ctx->codec_type) {
72415b2a 1393 case AVMEDIA_TYPE_VIDEO:
4563cf24
SS
1394 case AVMEDIA_TYPE_DATA:
1395 case AVMEDIA_TYPE_SUBTITLE:
1396 case AVMEDIA_TYPE_ATTACHMENT:
ce34ff6b
RK
1397 bit_rate = ctx->bit_rate;
1398 break;
72415b2a 1399 case AVMEDIA_TYPE_AUDIO:
ce34ff6b
RK
1400 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1401 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1402 break;
ce34ff6b
RK
1403 default:
1404 bit_rate = 0;
1405 break;
1406 }
1407 return bit_rate;
1408}
1409
7e566bbe
SS
1410size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1411{
1412 int i, len, ret = 0;
1413
1414 for (i = 0; i < 4; i++) {
1415 len = snprintf(buf, buf_size,
1416 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1417 buf += len;
1418 buf_size = buf_size > len ? buf_size - len : 0;
1419 ret += len;
1420 codec_tag>>=8;
1421 }
1422 return ret;
1423}
1424
de6d9b64
FB
1425void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1426{
1427 const char *codec_name;
2a81f4bd 1428 const char *profile = NULL;
de6d9b64
FB
1429 AVCodec *p;
1430 char buf1[32];
a96b68b7 1431 int bitrate;
59771f71 1432 AVRational display_aspect_ratio;
de6d9b64 1433
df8d5eaa
AC
1434 if (enc->codec)
1435 p = enc->codec;
1436 else if (encode)
de6d9b64
FB
1437 p = avcodec_find_encoder(enc->codec_id);
1438 else
1439 p = avcodec_find_decoder(enc->codec_id);
1440
1441 if (p) {
1442 codec_name = p->name;
2a81f4bd 1443 profile = av_get_profile_name(p, enc->profile);
985180a1
FB
1444 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
1445 /* fake mpeg2 transport stream codec (currently not
1446 registered) */
1447 codec_name = "mpeg2ts";
de6d9b64
FB
1448 } else if (enc->codec_name[0] != '\0') {
1449 codec_name = enc->codec_name;
1450 } else {
1451 /* output avi tags */
ab0b5378
SS
1452 char tag_buf[32];
1453 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1454 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
de6d9b64
FB
1455 codec_name = buf1;
1456 }
1457
1458 switch(enc->codec_type) {
72415b2a 1459 case AVMEDIA_TYPE_VIDEO:
de6d9b64
FB
1460 snprintf(buf, buf_size,
1461 "Video: %s%s",
7d1c3fc1 1462 codec_name, enc->mb_decision ? " (hq)" : "");
2a81f4bd
AH
1463 if (profile)
1464 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1465 " (%s)", profile);
82c0c4ba 1466 if (enc->pix_fmt != PIX_FMT_NONE) {
cf087595
FB
1467 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1468 ", %s",
94bed8e5 1469 av_get_pix_fmt_name(enc->pix_fmt));
cf087595 1470 }
de6d9b64
FB
1471 if (enc->width) {
1472 snprintf(buf + strlen(buf), buf_size - strlen(buf),
21189011
MN
1473 ", %dx%d",
1474 enc->width, enc->height);
7ee4dd02 1475 if (enc->sample_aspect_ratio.num) {
cbaf50f8
BC
1476 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1477 enc->width*enc->sample_aspect_ratio.num,
1478 enc->height*enc->sample_aspect_ratio.den,
1479 1024*1024);
1480 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1481 " [PAR %d:%d DAR %d:%d]",
1482 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1483 display_aspect_ratio.num, display_aspect_ratio.den);
7ee4dd02 1484 }
a309073b 1485 if(av_log_get_level() >= AV_LOG_DEBUG){
9ce6c138 1486 int g= av_gcd(enc->time_base.num, enc->time_base.den);
21189011
MN
1487 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1488 ", %d/%d",
1489 enc->time_base.num/g, enc->time_base.den/g);
1490 }
de6d9b64 1491 }
4bfad535
FB
1492 if (encode) {
1493 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1494 ", q=%d-%d", enc->qmin, enc->qmax);
1495 }
de6d9b64 1496 break;
72415b2a 1497 case AVMEDIA_TYPE_AUDIO:
de6d9b64
FB
1498 snprintf(buf, buf_size,
1499 "Audio: %s",
1500 codec_name);
2a81f4bd
AH
1501 if (profile)
1502 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1503 " (%s)", profile);
de6d9b64
FB
1504 if (enc->sample_rate) {
1505 snprintf(buf + strlen(buf), buf_size - strlen(buf),
0d72e7d0 1506 ", %d Hz", enc->sample_rate);
de6d9b64 1507 }
0d72e7d0 1508 av_strlcat(buf, ", ", buf_size);
63e8d976 1509 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
5d6e4c16 1510 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
9e82a113 1511 snprintf(buf + strlen(buf), buf_size - strlen(buf),
ba7d6e79 1512 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
9e82a113 1513 }
de6d9b64 1514 break;
72415b2a 1515 case AVMEDIA_TYPE_DATA:
985180a1 1516 snprintf(buf, buf_size, "Data: %s", codec_name);
240c1657 1517 break;
72415b2a 1518 case AVMEDIA_TYPE_SUBTITLE:
240c1657 1519 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
985180a1 1520 break;
72415b2a 1521 case AVMEDIA_TYPE_ATTACHMENT:
f8d7c9d3 1522 snprintf(buf, buf_size, "Attachment: %s", codec_name);
f8d7c9d3 1523 break;
de6d9b64 1524 default:
9fe5a7b8
MN
1525 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1526 return;
de6d9b64 1527 }
4bfad535
FB
1528 if (encode) {
1529 if (enc->flags & CODEC_FLAG_PASS1)
1530 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1531 ", pass 1");
1532 if (enc->flags & CODEC_FLAG_PASS2)
1533 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1534 ", pass 2");
1535 }
406aa93f 1536 bitrate = get_bit_rate(enc);
a96b68b7 1537 if (bitrate != 0) {
115329f1 1538 snprintf(buf + strlen(buf), buf_size - strlen(buf),
a96b68b7 1539 ", %d kb/s", bitrate / 1000);
de6d9b64
FB
1540 }
1541}
1542
060ec0a8
AH
1543const char *av_get_profile_name(const AVCodec *codec, int profile)
1544{
1545 const AVProfile *p;
1546 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1547 return NULL;
1548
1549 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1550 if (p->profile == profile)
1551 return p->name;
1552
1553 return NULL;
1554}
1555
156e5023
NK
1556unsigned avcodec_version( void )
1557{
1558 return LIBAVCODEC_VERSION_INT;
1559}
cf087595 1560
41600690 1561const char *avcodec_configuration(void)
c1736936 1562{
29ba0911 1563 return LIBAV_CONFIGURATION;
c1736936
DB
1564}
1565
41600690 1566const char *avcodec_license(void)
c1736936
DB
1567{
1568#define LICENSE_PREFIX "libavcodec license: "
a03be6e1 1569 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
c1736936
DB
1570}
1571
1c2a8c7f
MN
1572void avcodec_flush_buffers(AVCodecContext *avctx)
1573{
27237d52 1574 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
37b00b47 1575 ff_thread_flush(avctx);
d1cf4591 1576 else if(avctx->codec->flush)
7a06ff14 1577 avctx->codec->flush(avctx);
1c2a8c7f
MN
1578}
1579
0eea2129
JR
1580static void video_free_buffers(AVCodecContext *s)
1581{
f3a29b75 1582 AVCodecInternal *avci = s->internal;
d90cf87b
MN
1583 int i, j;
1584
f3a29b75
JR
1585 if (!avci->buffer)
1586 return;
115329f1 1587
f3a29b75
JR
1588 if (avci->buffer_count)
1589 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1590 avci->buffer_count);
d90cf87b 1591 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
f3a29b75 1592 InternalBuffer *buf = &avci->buffer[i];
d90cf87b
MN
1593 for(j=0; j<4; j++){
1594 av_freep(&buf->base[j]);
1595 buf->data[j]= NULL;
1596 }
1597 }
f3a29b75 1598 av_freep(&avci->buffer);
115329f1 1599
f3a29b75 1600 avci->buffer_count=0;
d90cf87b
MN
1601}
1602
0eea2129
JR
1603static void audio_free_buffers(AVCodecContext *avctx)
1604{
1605 AVCodecInternal *avci = avctx->internal;
1606 InternalBuffer *buf;
1607
1608 if (!avci->buffer)
1609 return;
1610 buf = avci->buffer;
1611
1612 if (buf->extended_data) {
1613 av_free(buf->extended_data[0]);
1614 if (buf->extended_data != buf->data)
1615 av_free(buf->extended_data);
1616 }
1617 av_freep(&avci->buffer);
1618}
1619
1620void avcodec_default_free_buffers(AVCodecContext *avctx)
1621{
1622 switch (avctx->codec_type) {
1623 case AVMEDIA_TYPE_VIDEO:
1624 video_free_buffers(avctx);
1625 break;
1626 case AVMEDIA_TYPE_AUDIO:
1627 audio_free_buffers(avctx);
1628 break;
1629 default:
1630 break;
1631 }
1632}
1633
6699d074
JR
1634int av_get_exact_bits_per_sample(enum CodecID codec_id)
1635{
ac3e1834 1636 switch(codec_id){
f1b163e0 1637 case CODEC_ID_ADPCM_CT:
220506d2 1638 case CODEC_ID_ADPCM_IMA_APC:
6699d074
JR
1639 case CODEC_ID_ADPCM_IMA_EA_SEAD:
1640 case CODEC_ID_ADPCM_IMA_WS:
029f966c 1641 case CODEC_ID_ADPCM_G722:
6699d074 1642 case CODEC_ID_ADPCM_YAMAHA:
f32fd318 1643 return 4;
ac3e1834
BC
1644 case CODEC_ID_PCM_ALAW:
1645 case CODEC_ID_PCM_MULAW:
1646 case CODEC_ID_PCM_S8:
1647 case CODEC_ID_PCM_U8:
9d49b8ff 1648 case CODEC_ID_PCM_ZORK:
ac3e1834
BC
1649 return 8;
1650 case CODEC_ID_PCM_S16BE:
1651 case CODEC_ID_PCM_S16LE:
725d86bf 1652 case CODEC_ID_PCM_S16LE_PLANAR:
ac3e1834
BC
1653 case CODEC_ID_PCM_U16BE:
1654 case CODEC_ID_PCM_U16LE:
1655 return 16;
1656 case CODEC_ID_PCM_S24DAUD:
1657 case CODEC_ID_PCM_S24BE:
1658 case CODEC_ID_PCM_S24LE:
1659 case CODEC_ID_PCM_U24BE:
1660 case CODEC_ID_PCM_U24LE:
1661 return 24;
1662 case CODEC_ID_PCM_S32BE:
1663 case CODEC_ID_PCM_S32LE:
1664 case CODEC_ID_PCM_U32BE:
1665 case CODEC_ID_PCM_U32LE:
aa29709e 1666 case CODEC_ID_PCM_F32BE:
143a5d6f 1667 case CODEC_ID_PCM_F32LE:
ac3e1834 1668 return 32;
143a5d6f
PR
1669 case CODEC_ID_PCM_F64BE:
1670 case CODEC_ID_PCM_F64LE:
1671 return 64;
ac3e1834
BC
1672 default:
1673 return 0;
1674 }
1675}
1676
6699d074
JR
1677int av_get_bits_per_sample(enum CodecID codec_id)
1678{
1679 switch (codec_id) {
1680 case CODEC_ID_ADPCM_SBPRO_2:
1681 return 2;
1682 case CODEC_ID_ADPCM_SBPRO_3:
1683 return 3;
1684 case CODEC_ID_ADPCM_SBPRO_4:
1685 case CODEC_ID_ADPCM_IMA_WAV:
1686 case CODEC_ID_ADPCM_IMA_QT:
1687 case CODEC_ID_ADPCM_SWF:
1688 case CODEC_ID_ADPCM_MS:
1689 return 4;
1690 default:
1691 return av_get_exact_bits_per_sample(codec_id);
1692 }
1693}
1694
9524cf79
JR
1695int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
1696{
1697 int id, sr, ch, ba, tag, bps;
1698
1699 id = avctx->codec_id;
1700 sr = avctx->sample_rate;
1701 ch = avctx->channels;
1702 ba = avctx->block_align;
1703 tag = avctx->codec_tag;
1704 bps = av_get_exact_bits_per_sample(avctx->codec_id);
1705
1706 /* codecs with an exact constant bits per sample */
1707 if (bps > 0 && ch > 0 && frame_bytes > 0)
1708 return (frame_bytes * 8) / (bps * ch);
1709 bps = avctx->bits_per_coded_sample;
1710
1711 /* codecs with a fixed packet duration */
1712 switch (id) {
1713 case CODEC_ID_ADPCM_ADX: return 32;
1714 case CODEC_ID_ADPCM_IMA_QT: return 64;
1715 case CODEC_ID_ADPCM_EA_XAS: return 128;
1716 case CODEC_ID_AMR_NB:
1717 case CODEC_ID_GSM:
1718 case CODEC_ID_QCELP:
1719 case CODEC_ID_RA_144:
1720 case CODEC_ID_RA_288: return 160;
1721 case CODEC_ID_IMC: return 256;
1722 case CODEC_ID_AMR_WB:
1723 case CODEC_ID_GSM_MS: return 320;
1724 case CODEC_ID_MP1: return 384;
1725 case CODEC_ID_ATRAC1: return 512;
1726 case CODEC_ID_ATRAC3: return 1024;
1727 case CODEC_ID_MP2:
1728 case CODEC_ID_MUSEPACK7: return 1152;
1729 case CODEC_ID_AC3: return 1536;
1730 }
1731
1732 if (sr > 0) {
1733 /* calc from sample rate */
1734 if (id == CODEC_ID_TTA)
1735 return 256 * sr / 245;
1736
1737 if (ch > 0) {
1738 /* calc from sample rate and channels */
1739 if (id == CODEC_ID_BINKAUDIO_DCT)
1740 return (480 << (sr / 22050)) / ch;
1741 }
1742 }
1743
1744 if (ba > 0) {
1745 /* calc from block_align */
1746 if (id == CODEC_ID_SIPR) {
1747 switch (ba) {
1748 case 20: return 160;
1749 case 19: return 144;
1750 case 29: return 288;
1751 case 37: return 480;
1752 }
1753 }
1754 }
1755
1756 if (frame_bytes > 0) {
1757 /* calc from frame_bytes only */
1758 if (id == CODEC_ID_TRUESPEECH)
1759 return 240 * (frame_bytes / 32);
1760 if (id == CODEC_ID_NELLYMOSER)
1761 return 256 * (frame_bytes / 64);
1762
1763 if (bps > 0) {
1764 /* calc from frame_bytes and bits_per_coded_sample */
1765 if (id == CODEC_ID_ADPCM_G726)
1766 return frame_bytes * 8 / bps;
1767 }
1768
1769 if (ch > 0) {
1770 /* calc from frame_bytes and channels */
1771 switch (id) {
1772 case CODEC_ID_ADPCM_4XM:
1773 case CODEC_ID_ADPCM_IMA_ISS:
1774 return (frame_bytes - 4 * ch) * 2 / ch;
1775 case CODEC_ID_ADPCM_IMA_SMJPEG:
1776 return (frame_bytes - 4) * 2 / ch;
1777 case CODEC_ID_ADPCM_IMA_AMV:
1778 return (frame_bytes - 8) * 2 / ch;
1779 case CODEC_ID_ADPCM_XA:
1780 return (frame_bytes / 128) * 224 / ch;
1781 case CODEC_ID_INTERPLAY_DPCM:
1782 return (frame_bytes - 6 - ch) / ch;
1783 case CODEC_ID_ROQ_DPCM:
1784 return (frame_bytes - 8) / ch;
1785 case CODEC_ID_XAN_DPCM:
1786 return (frame_bytes - 2 * ch) / ch;
1787 case CODEC_ID_MACE3:
1788 return 3 * frame_bytes / ch;
1789 case CODEC_ID_MACE6:
1790 return 6 * frame_bytes / ch;
1791 case CODEC_ID_PCM_LXF:
1792 return 2 * (frame_bytes / (5 * ch));
1793 }
1794
1795 if (tag) {
1796 /* calc from frame_bytes, channels, and codec_tag */
1797 if (id == CODEC_ID_SOL_DPCM) {
1798 if (tag == 3)
1799 return frame_bytes / ch;
1800 else
1801 return frame_bytes * 2 / ch;
1802 }
1803 }
1804
1805 if (ba > 0) {
1806 /* calc from frame_bytes, channels, and block_align */
1807 int blocks = frame_bytes / ba;
1808 switch (avctx->codec_id) {
1809 case CODEC_ID_ADPCM_IMA_WAV:
1810 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
1811 case CODEC_ID_ADPCM_IMA_DK3:
c346f630 1812 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
9524cf79
JR
1813 case CODEC_ID_ADPCM_IMA_DK4:
1814 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
1815 case CODEC_ID_ADPCM_MS:
1816 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
1817 }
1818 }
1819
1820 if (bps > 0) {
1821 /* calc from frame_bytes, channels, and bits_per_coded_sample */
1822 switch (avctx->codec_id) {
1823 case CODEC_ID_PCM_DVD:
1824 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
1825 case CODEC_ID_PCM_BLURAY:
1826 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
1827 case CODEC_ID_S302M:
1828 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
1829 }
1830 }
1831 }
1832 }
1833
1834 return 0;
1835}
1836
b250f9c6 1837#if !HAVE_THREADS
ba9ef8d0 1838int ff_thread_init(AVCodecContext *s){
ca8ad847
MN
1839 return -1;
1840}
1841#endif
ad2b531d
MR
1842
1843unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1844{
1845 unsigned int n = 0;
1846
1847 while(v >= 0xff) {
1848 *s++ = 0xff;
1849 v -= 0xff;
1850 n++;
1851 }
1852 *s = v;
1853 n++;
1854 return n;
1855}
1005f542 1856
c46eeae2
MN
1857int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1858 int i;
1859 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1860 return i;
1861}
1862
ce863d7f 1863void av_log_missing_feature(void *avc, const char *feature, int want_sample)
ea779d91 1864{
6001dad6 1865 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
db323491 1866 "version to the newest one from Git. If the problem still "
ea779d91 1867 "occurs, it means that your file has a feature which has not "
3fd3632f 1868 "been implemented.\n", feature);
ea779d91 1869 if(want_sample)
ce863d7f 1870 av_log_ask_for_sample(avc, NULL);
0ba39dd1
KG
1871}
1872
44f566b7 1873void av_log_ask_for_sample(void *avc, const char *msg, ...)
0ba39dd1 1874{
44f566b7
DB
1875 va_list argument_list;
1876
1877 va_start(argument_list, msg);
1878
0ba39dd1 1879 if (msg)
44f566b7 1880 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
0ba39dd1 1881 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
f0a41afd 1882 "of this file to ftp://upload.libav.org/incoming/ "
21de9204 1883 "and contact the libav-devel mailing list.\n");
44f566b7
DB
1884
1885 va_end(argument_list);
ea779d91 1886}
c895618b
MN
1887
1888static AVHWAccel *first_hwaccel = NULL;
1889
1890void av_register_hwaccel(AVHWAccel *hwaccel)
1891{
1892 AVHWAccel **p = &first_hwaccel;
1893 while (*p)
1894 p = &(*p)->next;
1895 *p = hwaccel;
1896 hwaccel->next = NULL;
1897}
414d9d7f
MN
1898
1899AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1900{
1901 return hwaccel ? hwaccel->next : first_hwaccel;
1902}
6059f13c
MN
1903
1904AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1905{
1906 AVHWAccel *hwaccel=NULL;
1907
1908 while((hwaccel= av_hwaccel_next(hwaccel))){
1909 if ( hwaccel->id == codec_id
1910 && hwaccel->pix_fmt == pix_fmt)
1911 return hwaccel;
1912 }
1913 return NULL;
1914}
f988ce6c
1915
1916int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1917{
1918 if (ff_lockmgr_cb) {
1919 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1920 return -1;
2d1b6fb7
MS
1921 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1922 return -1;
f988ce6c
1923 }
1924
1925 ff_lockmgr_cb = cb;
1926
1927 if (ff_lockmgr_cb) {
1928 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1929 return -1;
2d1b6fb7
MS
1930 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1931 return -1;
1932 }
1933 return 0;
1934}
1935
1936int avpriv_lock_avformat(void)
1937{
1938 if (ff_lockmgr_cb) {
1939 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1940 return -1;
1941 }
1942 return 0;
1943}
1944
1945int avpriv_unlock_avformat(void)
1946{
1947 if (ff_lockmgr_cb) {
1948 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1949 return -1;
f988ce6c
1950 }
1951 return 0;
1952}
603a5f04 1953
0842d589 1954unsigned int avpriv_toupper4(unsigned int x)
603a5f04
FL
1955{
1956 return toupper( x &0xFF)
1957 + (toupper((x>>8 )&0xFF)<<8 )
1958 + (toupper((x>>16)&0xFF)<<16)
1959 + (toupper((x>>24)&0xFF)<<24);
1960}
37b00b47 1961
27237d52 1962#if !HAVE_THREADS
37b00b47
AS
1963
1964int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1965{
1966 f->owner = avctx;
1967 return avctx->get_buffer(avctx, f);
1968}
1969
1970void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1971{
1972 f->owner->release_buffer(f->owner, f);
1973}
1974
1975void ff_thread_finish_setup(AVCodecContext *avctx)
1976{
1977}
1978
1979void ff_thread_report_progress(AVFrame *f, int progress, int field)
1980{
1981}
1982
1983void ff_thread_await_progress(AVFrame *f, int progress, int field)
1984{
1985}
1986
1987#endif
65af48b5 1988
bca06e77
AK
1989enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1990{
1991 if (codec_id <= CODEC_ID_NONE)
1992 return AVMEDIA_TYPE_UNKNOWN;
1993 else if (codec_id < CODEC_ID_FIRST_AUDIO)
1994 return AVMEDIA_TYPE_VIDEO;
1995 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1996 return AVMEDIA_TYPE_AUDIO;
1997 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1998 return AVMEDIA_TYPE_SUBTITLE;
1999
2000 return AVMEDIA_TYPE_UNKNOWN;
2001}
af08d9ae
AK
2002
2003int avcodec_is_open(AVCodecContext *s)
2004{
2005 return !!s->internal;
2006}