cmdutils: update copyright year to 2012.
[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
112 dsputil_static_init();
113}
114
b2c75b6e
JR
115static av_always_inline int codec_is_encoder(AVCodec *codec)
116{
117 return codec && (codec->encode || codec->encode2);
118}
119
120static av_always_inline int codec_is_decoder(AVCodec *codec)
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;
146 s->width = -((-width )>>s->lowres);
147 s->height= -((-height)>>s->lowres);
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);
ba68d9d3 231 if(s->codec_id == CODEC_ID_H264 || s->lowres)
ae4ffe9f 232 *height+=2; // some of the optimized chroma MC reads one line too much
ba68d9d3 233 // which is also done in mpeg decoders with lowres > 0
eb285cfe 234
2d9535ad 235 for (i = 0; i < 4; i++)
560f773c 236 linesize_align[i] = STRIDE_ALIGN;
eb285cfe
RD
237}
238
239void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
240 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
560f773c 241 int linesize_align[AV_NUM_DATA_POINTERS];
eb285cfe
RD
242 int align;
243 avcodec_align_dimensions2(s, width, height, linesize_align);
244 align = FFMAX(linesize_align[0], linesize_align[3]);
245 linesize_align[1] <<= chroma_shift;
246 linesize_align[2] <<= chroma_shift;
247 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
248 *width=FFALIGN(*width, align);
f0bbfc4a
MN
249}
250
5ee5fa02
JR
251int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
252 enum AVSampleFormat sample_fmt, const uint8_t *buf,
253 int buf_size, int align)
254{
255 int ch, planar, needed_size, ret = 0;
256
257 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
258 frame->nb_samples, sample_fmt,
259 align);
260 if (buf_size < needed_size)
261 return AVERROR(EINVAL);
262
263 planar = av_sample_fmt_is_planar(sample_fmt);
264 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
265 if (!(frame->extended_data = av_mallocz(nb_channels *
266 sizeof(*frame->extended_data))))
267 return AVERROR(ENOMEM);
268 } else {
269 frame->extended_data = frame->data;
270 }
271
272 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
273 buf, nb_channels, frame->nb_samples,
274 sample_fmt, align)) < 0) {
275 if (frame->extended_data != frame->data)
276 av_free(frame->extended_data);
277 return ret;
278 }
279 if (frame->extended_data != frame->data) {
280 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
281 frame->data[ch] = frame->extended_data[ch];
282 }
283
284 return ret;
285}
286
0eea2129
JR
287static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
288{
289 AVCodecInternal *avci = avctx->internal;
290 InternalBuffer *buf;
5ee5fa02 291 int buf_size, ret;
0eea2129
JR
292
293 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
294 frame->nb_samples, avctx->sample_fmt,
295 32);
296 if (buf_size < 0)
297 return AVERROR(EINVAL);
298
0eea2129
JR
299 /* allocate InternalBuffer if needed */
300 if (!avci->buffer) {
301 avci->buffer = av_mallocz(sizeof(InternalBuffer));
302 if (!avci->buffer)
303 return AVERROR(ENOMEM);
304 }
305 buf = avci->buffer;
306
307 /* if there is a previously-used internal buffer, check its size and
308 channel count to see if we can reuse it */
309 if (buf->extended_data) {
310 /* if current buffer is too small, free it */
311 if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
312 av_free(buf->extended_data[0]);
313 if (buf->extended_data != buf->data)
314 av_free(&buf->extended_data);
315 buf->extended_data = NULL;
316 buf->data[0] = NULL;
317 }
318 /* if number of channels has changed, reset and/or free extended data
319 pointers but leave data buffer in buf->data[0] for reuse */
320 if (buf->nb_channels != avctx->channels) {
321 if (buf->extended_data != buf->data)
322 av_free(buf->extended_data);
323 buf->extended_data = NULL;
324 }
325 }
326
327 /* if there is no previous buffer or the previous buffer cannot be used
328 as-is, allocate a new buffer and/or rearrange the channel pointers */
329 if (!buf->extended_data) {
5ee5fa02
JR
330 if (!buf->data[0]) {
331 if (!(buf->data[0] = av_mallocz(buf_size)))
0eea2129 332 return AVERROR(ENOMEM);
5ee5fa02 333 buf->audio_data_size = buf_size;
0eea2129 334 }
5ee5fa02
JR
335 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
336 avctx->sample_fmt, buf->data[0],
337 buf->audio_data_size, 32)))
0eea2129
JR
338 return ret;
339
5ee5fa02
JR
340 if (frame->extended_data == frame->data)
341 buf->extended_data = buf->data;
342 else
343 buf->extended_data = frame->extended_data;
344 memcpy(buf->data, frame->data, sizeof(frame->data));
345 buf->linesize[0] = frame->linesize[0];
346 buf->nb_channels = avctx->channels;
347 } else {
348 /* copy InternalBuffer info to the AVFrame */
349 frame->extended_data = buf->extended_data;
350 frame->linesize[0] = buf->linesize[0];
351 memcpy(frame->data, buf->data, sizeof(frame->data));
0eea2129
JR
352 }
353
0eea2129 354 frame->type = FF_BUFFER_TYPE_INTERNAL;
0eea2129
JR
355
356 if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
357 else frame->pkt_pts = AV_NOPTS_VALUE;
358 frame->reordered_opaque = avctx->reordered_opaque;
359
360 if (avctx->debug & FF_DEBUG_BUFFERS)
361 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
362 "internal audio buffer used\n", frame);
363
364 return 0;
365}
366
367static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
368{
1e491e29 369 int i;
f0bbfc4a
MN
370 int w= s->width;
371 int h= s->height;
d90cf87b 372 InternalBuffer *buf;
f3a29b75 373 AVCodecInternal *avci = s->internal;
0ecca7a4 374
65d999d6
MB
375 if(pic->data[0]!=NULL) {
376 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
377 return -1;
378 }
f3a29b75
JR
379 if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
380 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
65d999d6
MB
381 return -1;
382 }
1e491e29 383
e16f217c 384 if(av_image_check_size(w, h, 0, s))
0ecca7a4
MN
385 return -1;
386
f3a29b75
JR
387 if (!avci->buffer) {
388 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
389 sizeof(InternalBuffer));
d90cf87b 390 }
115329f1 391
f3a29b75 392 buf = &avci->buffer[avci->buffer_count];
115329f1 393
0701006e 394 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
37b00b47
AS
395 if(s->active_thread_type&FF_THREAD_FRAME) {
396 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
397 return -1;
398 }
399
560f773c 400 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
0701006e
MN
401 av_freep(&buf->base[i]);
402 buf->data[i]= NULL;
403 }
404 }
405
8400b126 406 if (!buf->base[0]) {
f0bbfc4a 407 int h_chroma_shift, v_chroma_shift;
db7ae7d1
VS
408 int size[4] = {0};
409 int tmpsize;
c9d6e847 410 int unaligned;
c7622f9a 411 AVPicture picture;
560f773c 412 int stride_align[AV_NUM_DATA_POINTERS];
6e3ef511 413 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
c7622f9a 414
1e491e29 415 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
f0bbfc4a 416
eb285cfe 417 avcodec_align_dimensions2(s, &w, &h, stride_align);
115329f1 418
1e491e29
MN
419 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
420 w+= EDGE_WIDTH*2;
421 h+= EDGE_WIDTH*2;
422 }
5b67307a 423
c9d6e847
RD
424 do {
425 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
426 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
9d2e0ad8 427 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
c9d6e847
RD
428 // increase alignment of w for next try (rhs gives the lowest bit set in w)
429 w += w & ~(w-1);
db7ae7d1 430
c9d6e847 431 unaligned = 0;
45bae968 432 for (i=0; i<4; i++){
c9d6e847 433 unaligned |= picture.linesize[i] % stride_align[i];
45bae968 434 }
c9d6e847 435 } while (unaligned);
db7ae7d1 436
9d2e0ad8 437 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
f8c96d01
RD
438 if (tmpsize < 0)
439 return -1;
db7ae7d1
VS
440
441 for (i=0; i<3 && picture.data[i+1]; i++)
442 size[i] = picture.data[i+1] - picture.data[i];
cf73e32a 443 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
c7622f9a 444
c7622f9a
MN
445 memset(buf->base, 0, sizeof(buf->base));
446 memset(buf->data, 0, sizeof(buf->data));
1e491e29 447
b70335a2 448 for(i=0; i<4 && size[i]; i++){
2c19981a
MN
449 const int h_shift= i==0 ? 0 : h_chroma_shift;
450 const int v_shift= i==0 ? 0 : v_chroma_shift;
1e491e29 451
c7622f9a 452 buf->linesize[i]= picture.linesize[i];
1e491e29 453
c7622f9a 454 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
d90cf87b 455 if(buf->base[i]==NULL) return -1;
c7622f9a
MN
456 memset(buf->base[i], 128, size[i]);
457
3491a9b2 458 // no edge if EDGE EMU or not planar YUV
6337178b 459 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
d90cf87b 460 buf->data[i] = buf->base[i];
1e491e29 461 else
6e3ef511 462 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
1e491e29 463 }
560f773c
JR
464 for (; i < AV_NUM_DATA_POINTERS; i++) {
465 buf->base[i] = buf->data[i] = NULL;
466 buf->linesize[i] = 0;
467 }
6337178b 468 if(size[1] && !size[2])
ed5d30d9 469 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
0701006e
MN
470 buf->width = s->width;
471 buf->height = s->height;
472 buf->pix_fmt= s->pix_fmt;
1e491e29 473 }
237e4938 474 pic->type= FF_BUFFER_TYPE_INTERNAL;
1e491e29 475
560f773c 476 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
d90cf87b
MN
477 pic->base[i]= buf->base[i];
478 pic->data[i]= buf->data[i];
237e4938 479 pic->linesize[i]= buf->linesize[i];
d90cf87b 480 }
0eea2129 481 pic->extended_data = pic->data;
f3a29b75 482 avci->buffer_count++;
d90cf87b 483
393cbb96
MN
484 if(s->pkt) pic->pkt_pts= s->pkt->pts;
485 else pic->pkt_pts= AV_NOPTS_VALUE;
79de84f2
MN
486 pic->reordered_opaque= s->reordered_opaque;
487
385c820b 488 if(s->debug&FF_DEBUG_BUFFERS)
f3a29b75
JR
489 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
490 "buffers used\n", pic, avci->buffer_count);
385c820b 491
1e491e29
MN
492 return 0;
493}
494
0eea2129
JR
495int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
496{
497 switch (avctx->codec_type) {
498 case AVMEDIA_TYPE_VIDEO:
499 return video_get_buffer(avctx, frame);
500 case AVMEDIA_TYPE_AUDIO:
501 return audio_get_buffer(avctx, frame);
502 default:
503 return -1;
504 }
505}
506
492cd3a9 507void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
1e491e29 508 int i;
6829ac8d 509 InternalBuffer *buf, *last;
f3a29b75 510 AVCodecInternal *avci = s->internal;
d90cf87b 511
0eea2129
JR
512 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
513
4e00e76b 514 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
f3a29b75
JR
515 assert(avci->buffer_count);
516
517 if (avci->buffer) {
518 buf = NULL; /* avoids warning */
519 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
520 buf = &avci->buffer[i];
521 if (buf->data[0] == pic->data[0])
522 break;
523 }
524 assert(i < avci->buffer_count);
525 avci->buffer_count--;
526 last = &avci->buffer[avci->buffer_count];
d90cf87b 527
a09bb3ba
MR
528 if (buf != last)
529 FFSWAP(InternalBuffer, *buf, *last);
6a9c8594 530 }
d90cf87b 531
560f773c 532 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
1e491e29 533 pic->data[i]=NULL;
d90cf87b
MN
534// pic->base[i]=NULL;
535 }
1e491e29 536//printf("R%X\n", pic->opaque);
385c820b
AS
537
538 if(s->debug&FF_DEBUG_BUFFERS)
f3a29b75
JR
539 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
540 "buffers used\n", pic, avci->buffer_count);
1e491e29
MN
541}
542
e1c2a5a0
RT
543int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
544 AVFrame temp_pic;
545 int i;
546
0eea2129
JR
547 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
548
e1c2a5a0
RT
549 /* If no picture return a new buffer */
550 if(pic->data[0] == NULL) {
551 /* We will copy from buffer, so must be readable */
552 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
553 return s->get_buffer(s, pic);
554 }
555
556 /* If internal buffer type return the same buffer */
b8af4fe9 557 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
62ecd363
NG
558 if(s->pkt) pic->pkt_pts= s->pkt->pts;
559 else pic->pkt_pts= AV_NOPTS_VALUE;
b8af4fe9 560 pic->reordered_opaque= s->reordered_opaque;
e1c2a5a0 561 return 0;
b8af4fe9 562 }
e1c2a5a0
RT
563
564 /*
565 * Not internal type and reget_buffer not overridden, emulate cr buffer
566 */
567 temp_pic = *pic;
560f773c 568 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
e1c2a5a0
RT
569 pic->data[i] = pic->base[i] = NULL;
570 pic->opaque = NULL;
571 /* Allocate new frame */
572 if (s->get_buffer(s, pic))
573 return -1;
574 /* Copy image data from old buffer to new buffer */
636d6a4a 575 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
e1c2a5a0
RT
576 s->height);
577 s->release_buffer(s, &temp_pic); // Release old frame
578 return 0;
579}
580
3a84713a 581int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
9c3d33d6
MN
582 int i;
583
584 for(i=0; i<count; i++){
3a84713a 585 int r= func(c, (char*)arg + i*size);
9c3d33d6
MN
586 if(ret) ret[i]= r;
587 }
588 return 0;
589}
590
8d23a86f
RD
591int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
592 int i;
593
594 for(i=0; i<count; i++){
595 int r= func(c, arg, i, 0);
596 if(ret) ret[i]= r;
597 }
598 return 0;
599}
600
c269cf68
MN
601enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
602 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
603 ++fmt;
a33c7159
MN
604 return fmt[0];
605}
606
9740beff
MN
607void avcodec_get_frame_defaults(AVFrame *pic){
608 memset(pic, 0, sizeof(AVFrame));
609
610 pic->pts= AV_NOPTS_VALUE;
c342499d 611 pic->key_frame= 1;
b58dbb5b 612 pic->sample_aspect_ratio = (AVRational){0, 1};
8a4a5f6f 613 pic->format = -1; /* unknown */
9740beff
MN
614}
615
492cd3a9 616AVFrame *avcodec_alloc_frame(void){
9740beff 617 AVFrame *pic= av_malloc(sizeof(AVFrame));
115329f1 618
9740beff 619 if(pic==NULL) return NULL;
115329f1 620
9740beff 621 avcodec_get_frame_defaults(pic);
115329f1 622
1e491e29
MN
623 return pic;
624}
625
0b950fe2
AK
626int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
627{
31d76ec2 628 int ret = 0;
0b950fe2
AK
629 AVDictionary *tmp = NULL;
630
af08d9ae
AK
631 if (avcodec_is_open(avctx))
632 return 0;
633
bc901998
AK
634 if ((!codec && !avctx->codec)) {
635 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
636 return AVERROR(EINVAL);
637 }
638 if ((codec && avctx->codec && codec != avctx->codec)) {
639 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
640 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
641 return AVERROR(EINVAL);
642 }
643 if (!codec)
644 codec = avctx->codec;
645
4df30f71
AC
646 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
647 return AVERROR(EINVAL);
648
0b950fe2
AK
649 if (options)
650 av_dict_copy(&tmp, *options, 0);
115329f1 651
f988ce6c
652 /* If there is a user-supplied mutex locking routine, call it. */
653 if (ff_lockmgr_cb) {
654 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
655 return -1;
656 }
657
ddebfb15
MN
658 entangled_thread_counter++;
659 if(entangled_thread_counter != 1){
660 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
31d76ec2 661 ret = -1;
ddebfb15
MN
662 goto end;
663 }
de6d9b64 664
f3a29b75
JR
665 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
666 if (!avctx->internal) {
667 ret = AVERROR(ENOMEM);
668 goto end;
669 }
670
0edf8a7a 671 if (codec->priv_data_size > 0) {
dc51a72b 672 if(!avctx->priv_data){
0edf8a7a 673 avctx->priv_data = av_mallocz(codec->priv_data_size);
90f06cea
PI
674 if (!avctx->priv_data) {
675 ret = AVERROR(ENOMEM);
ddebfb15 676 goto end;
90f06cea 677 }
0b950fe2 678 if (codec->priv_class) {
dc51a72b
MN
679 *(AVClass**)avctx->priv_data= codec->priv_class;
680 av_opt_set_defaults(avctx->priv_data);
681 }
682 }
1d36fb13 683 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
0b950fe2 684 goto free_and_end;
0edf8a7a
PG
685 } else {
686 avctx->priv_data = NULL;
687 }
0b950fe2
AK
688 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
689 goto free_and_end;
21adafec
MN
690
691 if(avctx->coded_width && avctx->coded_height)
692 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
693 else if(avctx->width && avctx->height)
694 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
695
82eac2f3
RD
696 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
697 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
698 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
699 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
700 avcodec_set_dimensions(avctx, 0, 0);
701 }
702
f19c58b4
AJ
703 /* if the decoder init function was already called previously,
704 free the already allocated subtitle_header before overwriting it */
b2c75b6e 705 if (codec_is_decoder(codec))
f19c58b4
AJ
706 av_freep(&avctx->subtitle_header);
707
fa77dd63 708#define SANE_NB_CHANNELS 128U
82eac2f3 709 if (avctx->channels > SANE_NB_CHANNELS) {
7868349a 710 ret = AVERROR(EINVAL);
2a9b5c9b 711 goto free_and_end;
0ecca7a4
MN
712 }
713
b5c85991 714 avctx->codec = codec;
72415b2a 715 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
681c180d
RD
716 avctx->codec_id == CODEC_ID_NONE) {
717 avctx->codec_type = codec->type;
718 avctx->codec_id = codec->id;
719 }
e83c716e
AJ
720 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
721 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
4c0dda2b 722 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
31d76ec2 723 ret = AVERROR(EINVAL);
2a9b5c9b 724 goto free_and_end;
4c0dda2b 725 }
b5c85991 726 avctx->frame_number = 0;
37b00b47 727
9a7dc618
MS
728 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
729 (!avctx->time_base.num || !avctx->time_base.den)) {
730 avctx->time_base.num = 1;
731 avctx->time_base.den = avctx->sample_rate;
732 }
733
37b00b47 734 if (HAVE_THREADS && !avctx->thread_opaque) {
ba9ef8d0 735 ret = ff_thread_init(avctx);
37b00b47
AS
736 if (ret < 0) {
737 goto free_and_end;
738 }
739 }
b6064d9a
JG
740 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
741 avctx->thread_count = 1;
37b00b47 742
527c91e3
CEH
743 if (avctx->codec->max_lowres < avctx->lowres) {
744 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
745 avctx->codec->max_lowres);
31d76ec2 746 ret = AVERROR(EINVAL);
527c91e3
CEH
747 goto free_and_end;
748 }
b2c75b6e 749 if (codec_is_encoder(avctx->codec)) {
2cfa2d92 750 int i;
8b00ab01 751 if (avctx->codec->sample_fmts) {
3dfc3e70
JR
752 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
753 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
754 break;
755 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
756 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
757 ret = AVERROR(EINVAL);
758 goto free_and_end;
759 }
8b00ab01
JR
760 }
761 if (avctx->codec->supported_samplerates) {
762 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
763 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
764 break;
765 if (avctx->codec->supported_samplerates[i] == 0) {
766 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
767 ret = AVERROR(EINVAL);
768 goto free_and_end;
769 }
770 }
771 if (avctx->codec->channel_layouts) {
772 if (!avctx->channel_layout) {
773 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
774 } else {
775 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
776 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
777 break;
778 if (avctx->codec->channel_layouts[i] == 0) {
779 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
780 ret = AVERROR(EINVAL);
781 goto free_and_end;
782 }
783 }
784 }
168f9e8c
JR
785 if (avctx->channel_layout && avctx->channels) {
786 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
787 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
788 ret = AVERROR(EINVAL);
789 goto free_and_end;
790 }
688b09fa
JR
791 } else if (avctx->channel_layout) {
792 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
168f9e8c 793 }
2cfa2d92 794 }
0fd0ef79 795
37b00b47 796 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
2de4f9eb
MN
797 ret = avctx->codec->init(avctx);
798 if (ret < 0) {
2a9b5c9b 799 goto free_and_end;
2de4f9eb 800 }
6e546aaa 801 }
ddebfb15
MN
802end:
803 entangled_thread_counter--;
f988ce6c
804
805 /* Release any user-supplied mutex. */
806 if (ff_lockmgr_cb) {
807 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
808 }
0b950fe2
AK
809 if (options) {
810 av_dict_free(options);
811 *options = tmp;
812 }
813
ddebfb15 814 return ret;
2a9b5c9b 815free_and_end:
0b950fe2 816 av_dict_free(&tmp);
2a9b5c9b 817 av_freep(&avctx->priv_data);
f3a29b75 818 av_freep(&avctx->internal);
2a9b5c9b
MN
819 avctx->codec= NULL;
820 goto end;
de6d9b64
FB
821}
822
b2c75b6e 823int ff_alloc_packet(AVPacket *avpkt, int size)
de6d9b64 824{
b2c75b6e
JR
825 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
826 return AVERROR(EINVAL);
827
828 if (avpkt->data) {
829 uint8_t *pkt_data;
b2c75b6e
JR
830
831 if (avpkt->size < size)
832 return AVERROR(EINVAL);
833
834 pkt_data = avpkt->data;
b2c75b6e
JR
835 av_init_packet(avpkt);
836 avpkt->data = pkt_data;
1a670973 837 avpkt->size = size;
b2c75b6e
JR
838 return 0;
839 } else {
840 return av_new_packet(avpkt, size);
0ecca7a4 841 }
b2c75b6e
JR
842}
843
844int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
845 AVPacket *avpkt,
846 const AVFrame *frame,
847 int *got_packet_ptr)
848{
849 int ret;
850 int user_packet = !!avpkt->data;
851 int nb_samples;
852
853 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
854 av_init_packet(avpkt);
855 avpkt->size = 0;
6f824977 856 return 0;
b2c75b6e
JR
857 }
858
859 /* check for valid frame size */
860 if (frame) {
861 nb_samples = frame->nb_samples;
862 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
863 if (nb_samples > avctx->frame_size)
864 return AVERROR(EINVAL);
865 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
866 if (nb_samples != avctx->frame_size)
867 return AVERROR(EINVAL);
868 }
869 } else {
870 nb_samples = avctx->frame_size;
871 }
872
873 if (avctx->codec->encode2) {
874 *got_packet_ptr = 0;
875 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
876 if (!ret && *got_packet_ptr &&
877 !(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
878 avpkt->pts = frame->pts;
879 avpkt->duration = av_rescale_q(frame->nb_samples,
880 (AVRational){ 1, avctx->sample_rate },
881 avctx->time_base);
882 }
883 } else {
884 /* for compatibility with encoders not supporting encode2(), we need to
885 allocate a packet buffer if the user has not provided one or check
886 the size otherwise */
887 int fs_tmp = 0;
888 int buf_size = avpkt->size;
889 if (!user_packet) {
890 if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
891 av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
892 buf_size = nb_samples * avctx->channels *
893 av_get_bits_per_sample(avctx->codec_id) / 8;
894 } else {
895 /* this is a guess as to the required size.
896 if an encoder needs more than this, it should probably
897 implement encode2() */
898 buf_size = 2 * avctx->frame_size * avctx->channels *
899 av_get_bytes_per_sample(avctx->sample_fmt);
900 buf_size += FF_MIN_BUFFER_SIZE;
901 }
902 }
903 if ((ret = ff_alloc_packet(avpkt, buf_size)))
904 return ret;
905
906 /* Encoders using AVCodec.encode() that support
907 CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
908 the smaller size when encoding the last frame.
909 This code can be removed once all encoders supporting
910 CODEC_CAP_SMALL_LAST_FRAME use encode2() */
911 if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
912 nb_samples < avctx->frame_size) {
913 fs_tmp = avctx->frame_size;
914 avctx->frame_size = nb_samples;
915 }
916
917 /* encode the frame */
918 ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
919 frame ? frame->data[0] : NULL);
920 if (ret >= 0) {
921 if (!ret) {
922 /* no output. if the packet data was allocated by libavcodec,
923 free it */
924 if (!user_packet)
925 av_freep(&avpkt->data);
926 } else {
927 if (avctx->coded_frame)
928 avpkt->pts = avctx->coded_frame->pts;
929 /* Set duration for final small packet. This can be removed
930 once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
931 encode2() */
932 if (fs_tmp) {
933 avpkt->duration = av_rescale_q(avctx->frame_size,
934 (AVRational){ 1, avctx->sample_rate },
935 avctx->time_base);
936 }
937 }
938 avpkt->size = ret;
939 *got_packet_ptr = (ret > 0);
940 ret = 0;
941 }
942
943 if (fs_tmp)
944 avctx->frame_size = fs_tmp;
945 }
946 if (!ret)
947 avctx->frame_number++;
948
949 /* NOTE: if we add any audio encoders which output non-keyframe packets,
950 this needs to be moved to the encoders, but for now we can do it
951 here to simplify things */
952 avpkt->flags |= AV_PKT_FLAG_KEY;
953
954 return ret;
de6d9b64
FB
955}
956
b2c75b6e
JR
957#if FF_API_OLD_DECODE_AUDIO
958int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
959 uint8_t *buf, int buf_size,
960 const short *samples)
961{
962 AVPacket pkt;
963 AVFrame frame0;
964 AVFrame *frame;
965 int ret, samples_size, got_packet;
966
967 av_init_packet(&pkt);
968 pkt.data = buf;
969 pkt.size = buf_size;
970
971 if (samples) {
972 frame = &frame0;
973 avcodec_get_frame_defaults(frame);
974
975 if (avctx->frame_size) {
976 frame->nb_samples = avctx->frame_size;
977 } else {
978 /* if frame_size is not set, the number of samples must be
979 calculated from the buffer size */
980 int64_t nb_samples;
981 if (!av_get_bits_per_sample(avctx->codec_id)) {
982 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
983 "support this codec\n");
984 return AVERROR(EINVAL);
985 }
986 nb_samples = (int64_t)buf_size * 8 /
987 (av_get_bits_per_sample(avctx->codec_id) *
988 avctx->channels);
989 if (nb_samples >= INT_MAX)
990 return AVERROR(EINVAL);
991 frame->nb_samples = nb_samples;
992 }
993
994 /* it is assumed that the samples buffer is large enough based on the
995 relevant parameters */
996 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
997 frame->nb_samples,
998 avctx->sample_fmt, 1);
999 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1000 avctx->sample_fmt,
1001 samples, samples_size, 1)))
1002 return ret;
1003
1004 /* fabricate frame pts from sample count.
1005 this is needed because the avcodec_encode_audio() API does not have
1006 a way for the user to provide pts */
1007 frame->pts = av_rescale_q(avctx->internal->sample_count,
1008 (AVRational){ 1, avctx->sample_rate },
1009 avctx->time_base);
1010 avctx->internal->sample_count += frame->nb_samples;
1011 } else {
1012 frame = NULL;
1013 }
1014
1015 got_packet = 0;
1016 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1017 if (!ret && got_packet && avctx->coded_frame) {
1018 avctx->coded_frame->pts = pkt.pts;
1019 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1020 }
1021 /* free any side data since we cannot return it */
1022 if (pkt.side_data_elems > 0) {
1023 int i;
1024 for (i = 0; i < pkt.side_data_elems; i++)
1025 av_free(pkt.side_data[i].data);
1026 av_freep(&pkt.side_data);
1027 pkt.side_data_elems = 0;
1028 }
1029
1030 if (frame && frame->extended_data != frame->data)
1031 av_free(frame->extended_data);
1032
1033 return ret ? ret : pkt.size;
1034}
1035#endif
1036
5e4c7ca2 1037int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
492cd3a9 1038 const AVFrame *pict)
de6d9b64 1039{
0ecca7a4 1040 if(buf_size < FF_MIN_BUFFER_SIZE){
5286d11f 1041 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
0ecca7a4
MN
1042 return -1;
1043 }
e16f217c 1044 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
0ecca7a4 1045 return -1;
6f824977 1046 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
8eb027c8 1047 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
6f824977 1048 avctx->frame_number++;
bb628dae 1049 emms_c(); //needed to avoid an emms_c() call before every return;
115329f1 1050
6f824977
MN
1051 return ret;
1052 }else
1053 return 0;
de6d9b64
FB
1054}
1055
115329f1 1056int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
240c1657
FB
1057 const AVSubtitle *sub)
1058{
1059 int ret;
9413db9e
BA
1060 if(sub->start_display_time) {
1061 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1062 return -1;
1063 }
505aa6c9
BA
1064 if(sub->num_rects == 0 || !sub->rects)
1065 return -1;
8eb027c8 1066 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
240c1657
FB
1067 avctx->frame_number++;
1068 return ret;
1069}
1070
867f923d
MS
1071static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1072{
1073 int size = 0;
1074 const uint8_t *data;
1075 uint32_t flags;
1076
1077 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1078 return;
1079
1080 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1081 if (!data || size < 4)
1082 return;
1083 flags = bytestream_get_le32(&data);
1084 size -= 4;
1085 if (size < 4) /* Required for any of the changes */
1086 return;
1087 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1088 avctx->channels = bytestream_get_le32(&data);
1089 size -= 4;
1090 }
1091 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1092 if (size < 8)
1093 return;
1094 avctx->channel_layout = bytestream_get_le64(&data);
1095 size -= 8;
1096 }
1097 if (size < 4)
1098 return;
1099 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1100 avctx->sample_rate = bytestream_get_le32(&data);
1101 size -= 4;
1102 }
1103 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1104 if (size < 8)
1105 return;
1106 avctx->width = bytestream_get_le32(&data);
1107 avctx->height = bytestream_get_le32(&data);
c5d907b6 1108 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
867f923d
MS
1109 size -= 8;
1110 }
1111}
1112
7a00bbad
TB
1113int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1114 int *got_picture_ptr,
1115 AVPacket *avpkt)
1116{
de6d9b64 1117 int ret;
115329f1 1118
53db1cae 1119 *got_picture_ptr= 0;
e16f217c 1120 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
0ecca7a4 1121 return -1;
393cbb96
MN
1122
1123 avctx->pkt = avpkt;
c5d907b6 1124 apply_param_change(avctx, avpkt);
393cbb96 1125
37b00b47 1126 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
27237d52 1127 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
37b00b47
AS
1128 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1129 avpkt);
1130 else {
1131 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1132 avpkt);
1133 picture->pkt_dts= avpkt->dts;
b58dbb5b 1134 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
3a2ddf7c
SS
1135 picture->width = avctx->width;
1136 picture->height = avctx->height;
8a4a5f6f 1137 picture->format = avctx->pix_fmt;
37b00b47 1138 }
6bb925f4 1139
bb628dae 1140 emms_c(); //needed to avoid an emms_c() call before every return;
115329f1
DB
1141
1142 if (*got_picture_ptr)
934982c4
MN
1143 avctx->frame_number++;
1144 }else
1145 ret= 0;
1146
de6d9b64
FB
1147 return ret;
1148}
1149
0eea2129 1150#if FF_API_OLD_DECODE_AUDIO
7a00bbad
TB
1151int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1152 int *frame_size_ptr,
1153 AVPacket *avpkt)
1154{
0eea2129
JR
1155 AVFrame frame;
1156 int ret, got_frame = 0;
1157
1158 if (avctx->get_buffer != avcodec_default_get_buffer) {
e2ff436e
RT
1159 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1160 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1161 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1162 "avcodec_decode_audio4()\n");
1163 avctx->get_buffer = avcodec_default_get_buffer;
0eea2129
JR
1164 }
1165
1166 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1167
1168 if (ret >= 0 && got_frame) {
1169 int ch, plane_size;
1170 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1171 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1172 frame.nb_samples,
1173 avctx->sample_fmt, 1);
1174 if (*frame_size_ptr < data_size) {
1175 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1176 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1177 return AVERROR(EINVAL);
1178 }
1179
1180 memcpy(samples, frame.extended_data[0], plane_size);
1181
1182 if (planar && avctx->channels > 1) {
1183 uint8_t *out = ((uint8_t *)samples) + plane_size;
1184 for (ch = 1; ch < avctx->channels; ch++) {
1185 memcpy(out, frame.extended_data[ch], plane_size);
1186 out += plane_size;
1187 }
1188 }
1189 *frame_size_ptr = data_size;
1190 } else {
1191 *frame_size_ptr = 0;
1192 }
1193 return ret;
1194}
1195#endif
1196
1197int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1198 AVFrame *frame,
1199 int *got_frame_ptr,
1200 AVPacket *avpkt)
1201{
1202 int ret = 0;
1203
1204 *got_frame_ptr = 0;
de6d9b64 1205
393cbb96
MN
1206 avctx->pkt = avpkt;
1207
6326afd5
JR
1208 if (!avpkt->data && avpkt->size) {
1209 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1210 return AVERROR(EINVAL);
1211 }
1212
f13db94d
MS
1213 apply_param_change(avctx, avpkt);
1214
0eea2129
JR
1215 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1216 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1217 if (ret >= 0 && *got_frame_ptr) {
1218 avctx->frame_number++;
1219 frame->pkt_dts = avpkt->dts;
8a4a5f6f
SS
1220 if (frame->format == AV_SAMPLE_FMT_NONE)
1221 frame->format = avctx->sample_fmt;
9c856d62 1222 }
ac66834c 1223 }
de6d9b64
FB
1224 return ret;
1225}
1226
7a00bbad
TB
1227int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1228 int *got_sub_ptr,
1229 AVPacket *avpkt)
1230{
240c1657
FB
1231 int ret;
1232
393cbb96 1233 avctx->pkt = avpkt;
240c1657 1234 *got_sub_ptr = 0;
7a00bbad 1235 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
240c1657
FB
1236 if (*got_sub_ptr)
1237 avctx->frame_number++;
1238 return ret;
1239}
1240
e1d7c883
RD
1241void avsubtitle_free(AVSubtitle *sub)
1242{
1243 int i;
1244
1245 for (i = 0; i < sub->num_rects; i++)
1246 {
8b834ac5
RD
1247 av_freep(&sub->rects[i]->pict.data[0]);
1248 av_freep(&sub->rects[i]->pict.data[1]);
1249 av_freep(&sub->rects[i]->pict.data[2]);
1250 av_freep(&sub->rects[i]->pict.data[3]);
1251 av_freep(&sub->rects[i]->text);
1252 av_freep(&sub->rects[i]->ass);
1253 av_freep(&sub->rects[i]);
e1d7c883
RD
1254 }
1255
8b834ac5 1256 av_freep(&sub->rects);
e1d7c883
RD
1257
1258 memset(sub, 0, sizeof(AVSubtitle));
1259}
1260
0752cd39 1261av_cold int avcodec_close(AVCodecContext *avctx)
de6d9b64 1262{
f988ce6c
1263 /* If there is a user-supplied mutex locking routine, call it. */
1264 if (ff_lockmgr_cb) {
1265 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1266 return -1;
1267 }
1268
ddebfb15
MN
1269 entangled_thread_counter++;
1270 if(entangled_thread_counter != 1){
1271 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1272 entangled_thread_counter--;
1273 return -1;
1274 }
1275
0e72ad95
AK
1276 if (avcodec_is_open(avctx)) {
1277 if (HAVE_THREADS && avctx->thread_opaque)
1278 ff_thread_free(avctx);
1279 if (avctx->codec && avctx->codec->close)
1280 avctx->codec->close(avctx);
1281 avcodec_default_free_buffers(avctx);
1282 avctx->coded_frame = NULL;
1283 av_freep(&avctx->internal);
1284 }
1285
1286 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
36773283
AK
1287 av_opt_free(avctx->priv_data);
1288 av_opt_free(avctx);
3123dd79 1289 av_freep(&avctx->priv_data);
b2c75b6e 1290 if (codec_is_encoder(avctx->codec))
c4f267ab 1291 av_freep(&avctx->extradata);
de6d9b64 1292 avctx->codec = NULL;
37b00b47 1293 avctx->active_thread_type = 0;
ddebfb15 1294 entangled_thread_counter--;
f988ce6c
1295
1296 /* Release any user-supplied mutex. */
1297 if (ff_lockmgr_cb) {
1298 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1299 }
de6d9b64
FB
1300 return 0;
1301}
1302
1303AVCodec *avcodec_find_encoder(enum CodecID id)
1304{
93ebfeea 1305 AVCodec *p, *experimental=NULL;
de6d9b64
FB
1306 p = first_avcodec;
1307 while (p) {
b2c75b6e 1308 if (codec_is_encoder(p) && p->id == id) {
93ebfeea
JG
1309 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1310 experimental = p;
1311 } else
1312 return p;
1313 }
de6d9b64
FB
1314 p = p->next;
1315 }
93ebfeea 1316 return experimental;
de6d9b64
FB
1317}
1318
98f3b098
A
1319AVCodec *avcodec_find_encoder_by_name(const char *name)
1320{
1321 AVCodec *p;
fc228c90
AJ
1322 if (!name)
1323 return NULL;
98f3b098
A
1324 p = first_avcodec;
1325 while (p) {
b2c75b6e 1326 if (codec_is_encoder(p) && strcmp(name,p->name) == 0)
98f3b098
A
1327 return p;
1328 p = p->next;
1329 }
1330 return NULL;
1331}
1332
de6d9b64
FB
1333AVCodec *avcodec_find_decoder(enum CodecID id)
1334{
1335 AVCodec *p;
1336 p = first_avcodec;
1337 while (p) {
b2c75b6e 1338 if (codec_is_decoder(p) && p->id == id)
de6d9b64
FB
1339 return p;
1340 p = p->next;
1341 }
1342 return NULL;
1343}
1344
1345AVCodec *avcodec_find_decoder_by_name(const char *name)
1346{
1347 AVCodec *p;
fc228c90
AJ
1348 if (!name)
1349 return NULL;
de6d9b64
FB
1350 p = first_avcodec;
1351 while (p) {
b2c75b6e 1352 if (codec_is_decoder(p) && strcmp(name,p->name) == 0)
de6d9b64
FB
1353 return p;
1354 p = p->next;
1355 }
1356 return NULL;
1357}
1358
406aa93f 1359static int get_bit_rate(AVCodecContext *ctx)
ce34ff6b
RK
1360{
1361 int bit_rate;
1362 int bits_per_sample;
1363
1364 switch(ctx->codec_type) {
72415b2a 1365 case AVMEDIA_TYPE_VIDEO:
4563cf24
SS
1366 case AVMEDIA_TYPE_DATA:
1367 case AVMEDIA_TYPE_SUBTITLE:
1368 case AVMEDIA_TYPE_ATTACHMENT:
ce34ff6b
RK
1369 bit_rate = ctx->bit_rate;
1370 break;
72415b2a 1371 case AVMEDIA_TYPE_AUDIO:
ce34ff6b
RK
1372 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1373 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1374 break;
ce34ff6b
RK
1375 default:
1376 bit_rate = 0;
1377 break;
1378 }
1379 return bit_rate;
1380}
1381
7e566bbe
SS
1382size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1383{
1384 int i, len, ret = 0;
1385
1386 for (i = 0; i < 4; i++) {
1387 len = snprintf(buf, buf_size,
1388 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1389 buf += len;
1390 buf_size = buf_size > len ? buf_size - len : 0;
1391 ret += len;
1392 codec_tag>>=8;
1393 }
1394 return ret;
1395}
1396
de6d9b64
FB
1397void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1398{
1399 const char *codec_name;
2a81f4bd 1400 const char *profile = NULL;
de6d9b64
FB
1401 AVCodec *p;
1402 char buf1[32];
a96b68b7 1403 int bitrate;
59771f71 1404 AVRational display_aspect_ratio;
de6d9b64
FB
1405
1406 if (encode)
1407 p = avcodec_find_encoder(enc->codec_id);
1408 else
1409 p = avcodec_find_decoder(enc->codec_id);
1410
1411 if (p) {
1412 codec_name = p->name;
2a81f4bd 1413 profile = av_get_profile_name(p, enc->profile);
985180a1
FB
1414 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
1415 /* fake mpeg2 transport stream codec (currently not
1416 registered) */
1417 codec_name = "mpeg2ts";
de6d9b64
FB
1418 } else if (enc->codec_name[0] != '\0') {
1419 codec_name = enc->codec_name;
1420 } else {
1421 /* output avi tags */
ab0b5378
SS
1422 char tag_buf[32];
1423 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1424 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
de6d9b64
FB
1425 codec_name = buf1;
1426 }
1427
1428 switch(enc->codec_type) {
72415b2a 1429 case AVMEDIA_TYPE_VIDEO:
de6d9b64
FB
1430 snprintf(buf, buf_size,
1431 "Video: %s%s",
7d1c3fc1 1432 codec_name, enc->mb_decision ? " (hq)" : "");
2a81f4bd
AH
1433 if (profile)
1434 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1435 " (%s)", profile);
82c0c4ba 1436 if (enc->pix_fmt != PIX_FMT_NONE) {
cf087595
FB
1437 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1438 ", %s",
94bed8e5 1439 av_get_pix_fmt_name(enc->pix_fmt));
cf087595 1440 }
de6d9b64
FB
1441 if (enc->width) {
1442 snprintf(buf + strlen(buf), buf_size - strlen(buf),
21189011
MN
1443 ", %dx%d",
1444 enc->width, enc->height);
7ee4dd02 1445 if (enc->sample_aspect_ratio.num) {
cbaf50f8
BC
1446 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1447 enc->width*enc->sample_aspect_ratio.num,
1448 enc->height*enc->sample_aspect_ratio.den,
1449 1024*1024);
1450 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1451 " [PAR %d:%d DAR %d:%d]",
1452 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1453 display_aspect_ratio.num, display_aspect_ratio.den);
7ee4dd02 1454 }
a309073b 1455 if(av_log_get_level() >= AV_LOG_DEBUG){
9ce6c138 1456 int g= av_gcd(enc->time_base.num, enc->time_base.den);
21189011
MN
1457 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1458 ", %d/%d",
1459 enc->time_base.num/g, enc->time_base.den/g);
1460 }
de6d9b64 1461 }
4bfad535
FB
1462 if (encode) {
1463 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1464 ", q=%d-%d", enc->qmin, enc->qmax);
1465 }
de6d9b64 1466 break;
72415b2a 1467 case AVMEDIA_TYPE_AUDIO:
de6d9b64
FB
1468 snprintf(buf, buf_size,
1469 "Audio: %s",
1470 codec_name);
2a81f4bd
AH
1471 if (profile)
1472 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1473 " (%s)", profile);
de6d9b64
FB
1474 if (enc->sample_rate) {
1475 snprintf(buf + strlen(buf), buf_size - strlen(buf),
0d72e7d0 1476 ", %d Hz", enc->sample_rate);
de6d9b64 1477 }
0d72e7d0 1478 av_strlcat(buf, ", ", buf_size);
63e8d976 1479 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
5d6e4c16 1480 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
9e82a113 1481 snprintf(buf + strlen(buf), buf_size - strlen(buf),
ba7d6e79 1482 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
9e82a113 1483 }
de6d9b64 1484 break;
72415b2a 1485 case AVMEDIA_TYPE_DATA:
985180a1 1486 snprintf(buf, buf_size, "Data: %s", codec_name);
240c1657 1487 break;
72415b2a 1488 case AVMEDIA_TYPE_SUBTITLE:
240c1657 1489 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
985180a1 1490 break;
72415b2a 1491 case AVMEDIA_TYPE_ATTACHMENT:
f8d7c9d3 1492 snprintf(buf, buf_size, "Attachment: %s", codec_name);
f8d7c9d3 1493 break;
de6d9b64 1494 default:
9fe5a7b8
MN
1495 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1496 return;
de6d9b64 1497 }
4bfad535
FB
1498 if (encode) {
1499 if (enc->flags & CODEC_FLAG_PASS1)
1500 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1501 ", pass 1");
1502 if (enc->flags & CODEC_FLAG_PASS2)
1503 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1504 ", pass 2");
1505 }
406aa93f 1506 bitrate = get_bit_rate(enc);
a96b68b7 1507 if (bitrate != 0) {
115329f1 1508 snprintf(buf + strlen(buf), buf_size - strlen(buf),
a96b68b7 1509 ", %d kb/s", bitrate / 1000);
de6d9b64
FB
1510 }
1511}
1512
060ec0a8
AH
1513const char *av_get_profile_name(const AVCodec *codec, int profile)
1514{
1515 const AVProfile *p;
1516 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1517 return NULL;
1518
1519 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1520 if (p->profile == profile)
1521 return p->name;
1522
1523 return NULL;
1524}
1525
156e5023
NK
1526unsigned avcodec_version( void )
1527{
1528 return LIBAVCODEC_VERSION_INT;
1529}
cf087595 1530
41600690 1531const char *avcodec_configuration(void)
c1736936 1532{
29ba0911 1533 return LIBAV_CONFIGURATION;
c1736936
DB
1534}
1535
41600690 1536const char *avcodec_license(void)
c1736936
DB
1537{
1538#define LICENSE_PREFIX "libavcodec license: "
a03be6e1 1539 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
c1736936
DB
1540}
1541
1c2a8c7f
MN
1542void avcodec_flush_buffers(AVCodecContext *avctx)
1543{
27237d52 1544 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
37b00b47 1545 ff_thread_flush(avctx);
d1cf4591 1546 else if(avctx->codec->flush)
7a06ff14 1547 avctx->codec->flush(avctx);
1c2a8c7f
MN
1548}
1549
0eea2129
JR
1550static void video_free_buffers(AVCodecContext *s)
1551{
f3a29b75 1552 AVCodecInternal *avci = s->internal;
d90cf87b
MN
1553 int i, j;
1554
f3a29b75
JR
1555 if (!avci->buffer)
1556 return;
115329f1 1557
f3a29b75
JR
1558 if (avci->buffer_count)
1559 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1560 avci->buffer_count);
d90cf87b 1561 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
f3a29b75 1562 InternalBuffer *buf = &avci->buffer[i];
d90cf87b
MN
1563 for(j=0; j<4; j++){
1564 av_freep(&buf->base[j]);
1565 buf->data[j]= NULL;
1566 }
1567 }
f3a29b75 1568 av_freep(&avci->buffer);
115329f1 1569
f3a29b75 1570 avci->buffer_count=0;
d90cf87b
MN
1571}
1572
0eea2129
JR
1573static void audio_free_buffers(AVCodecContext *avctx)
1574{
1575 AVCodecInternal *avci = avctx->internal;
1576 InternalBuffer *buf;
1577
1578 if (!avci->buffer)
1579 return;
1580 buf = avci->buffer;
1581
1582 if (buf->extended_data) {
1583 av_free(buf->extended_data[0]);
1584 if (buf->extended_data != buf->data)
1585 av_free(buf->extended_data);
1586 }
1587 av_freep(&avci->buffer);
1588}
1589
1590void avcodec_default_free_buffers(AVCodecContext *avctx)
1591{
1592 switch (avctx->codec_type) {
1593 case AVMEDIA_TYPE_VIDEO:
1594 video_free_buffers(avctx);
1595 break;
1596 case AVMEDIA_TYPE_AUDIO:
1597 audio_free_buffers(avctx);
1598 break;
1599 default:
1600 break;
1601 }
1602}
1603
ac3e1834
BC
1604int av_get_bits_per_sample(enum CodecID codec_id){
1605 switch(codec_id){
5da71469 1606 case CODEC_ID_ADPCM_SBPRO_2:
f1b163e0 1607 return 2;
5da71469 1608 case CODEC_ID_ADPCM_SBPRO_3:
f1b163e0 1609 return 3;
5da71469 1610 case CODEC_ID_ADPCM_SBPRO_4:
f1b163e0 1611 case CODEC_ID_ADPCM_CT:
220506d2 1612 case CODEC_ID_ADPCM_IMA_APC:
07f2a575 1613 case CODEC_ID_ADPCM_IMA_WAV:
9ff6d079
JR
1614 case CODEC_ID_ADPCM_IMA_QT:
1615 case CODEC_ID_ADPCM_SWF:
9df9b810
DV
1616 case CODEC_ID_ADPCM_MS:
1617 case CODEC_ID_ADPCM_YAMAHA:
029f966c 1618 case CODEC_ID_ADPCM_G722:
f32fd318 1619 return 4;
ac3e1834
BC
1620 case CODEC_ID_PCM_ALAW:
1621 case CODEC_ID_PCM_MULAW:
1622 case CODEC_ID_PCM_S8:
1623 case CODEC_ID_PCM_U8:
9d49b8ff 1624 case CODEC_ID_PCM_ZORK:
ac3e1834
BC
1625 return 8;
1626 case CODEC_ID_PCM_S16BE:
1627 case CODEC_ID_PCM_S16LE:
725d86bf 1628 case CODEC_ID_PCM_S16LE_PLANAR:
ac3e1834
BC
1629 case CODEC_ID_PCM_U16BE:
1630 case CODEC_ID_PCM_U16LE:
1631 return 16;
1632 case CODEC_ID_PCM_S24DAUD:
1633 case CODEC_ID_PCM_S24BE:
1634 case CODEC_ID_PCM_S24LE:
1635 case CODEC_ID_PCM_U24BE:
1636 case CODEC_ID_PCM_U24LE:
1637 return 24;
1638 case CODEC_ID_PCM_S32BE:
1639 case CODEC_ID_PCM_S32LE:
1640 case CODEC_ID_PCM_U32BE:
1641 case CODEC_ID_PCM_U32LE:
aa29709e 1642 case CODEC_ID_PCM_F32BE:
143a5d6f 1643 case CODEC_ID_PCM_F32LE:
ac3e1834 1644 return 32;
143a5d6f
PR
1645 case CODEC_ID_PCM_F64BE:
1646 case CODEC_ID_PCM_F64LE:
1647 return 64;
ac3e1834
BC
1648 default:
1649 return 0;
1650 }
1651}
1652
b250f9c6 1653#if !HAVE_THREADS
ba9ef8d0 1654int ff_thread_init(AVCodecContext *s){
ca8ad847
MN
1655 return -1;
1656}
1657#endif
ad2b531d
MR
1658
1659unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1660{
1661 unsigned int n = 0;
1662
1663 while(v >= 0xff) {
1664 *s++ = 0xff;
1665 v -= 0xff;
1666 n++;
1667 }
1668 *s = v;
1669 n++;
1670 return n;
1671}
1005f542 1672
c46eeae2
MN
1673int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1674 int i;
1675 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1676 return i;
1677}
1678
ce863d7f 1679void av_log_missing_feature(void *avc, const char *feature, int want_sample)
ea779d91 1680{
6001dad6 1681 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
db323491 1682 "version to the newest one from Git. If the problem still "
ea779d91 1683 "occurs, it means that your file has a feature which has not "
3fd3632f 1684 "been implemented.\n", feature);
ea779d91 1685 if(want_sample)
ce863d7f 1686 av_log_ask_for_sample(avc, NULL);
0ba39dd1
KG
1687}
1688
44f566b7 1689void av_log_ask_for_sample(void *avc, const char *msg, ...)
0ba39dd1 1690{
44f566b7
DB
1691 va_list argument_list;
1692
1693 va_start(argument_list, msg);
1694
0ba39dd1 1695 if (msg)
44f566b7 1696 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
0ba39dd1 1697 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
f0a41afd 1698 "of this file to ftp://upload.libav.org/incoming/ "
21de9204 1699 "and contact the libav-devel mailing list.\n");
44f566b7
DB
1700
1701 va_end(argument_list);
ea779d91 1702}
c895618b
MN
1703
1704static AVHWAccel *first_hwaccel = NULL;
1705
1706void av_register_hwaccel(AVHWAccel *hwaccel)
1707{
1708 AVHWAccel **p = &first_hwaccel;
1709 while (*p)
1710 p = &(*p)->next;
1711 *p = hwaccel;
1712 hwaccel->next = NULL;
1713}
414d9d7f
MN
1714
1715AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1716{
1717 return hwaccel ? hwaccel->next : first_hwaccel;
1718}
6059f13c
MN
1719
1720AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1721{
1722 AVHWAccel *hwaccel=NULL;
1723
1724 while((hwaccel= av_hwaccel_next(hwaccel))){
1725 if ( hwaccel->id == codec_id
1726 && hwaccel->pix_fmt == pix_fmt)
1727 return hwaccel;
1728 }
1729 return NULL;
1730}
f988ce6c
1731
1732int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1733{
1734 if (ff_lockmgr_cb) {
1735 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1736 return -1;
2d1b6fb7
MS
1737 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1738 return -1;
f988ce6c
1739 }
1740
1741 ff_lockmgr_cb = cb;
1742
1743 if (ff_lockmgr_cb) {
1744 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1745 return -1;
2d1b6fb7
MS
1746 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1747 return -1;
1748 }
1749 return 0;
1750}
1751
1752int avpriv_lock_avformat(void)
1753{
1754 if (ff_lockmgr_cb) {
1755 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1756 return -1;
1757 }
1758 return 0;
1759}
1760
1761int avpriv_unlock_avformat(void)
1762{
1763 if (ff_lockmgr_cb) {
1764 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1765 return -1;
f988ce6c
1766 }
1767 return 0;
1768}
603a5f04 1769
0842d589 1770unsigned int avpriv_toupper4(unsigned int x)
603a5f04
FL
1771{
1772 return toupper( x &0xFF)
1773 + (toupper((x>>8 )&0xFF)<<8 )
1774 + (toupper((x>>16)&0xFF)<<16)
1775 + (toupper((x>>24)&0xFF)<<24);
1776}
37b00b47 1777
27237d52 1778#if !HAVE_THREADS
37b00b47
AS
1779
1780int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1781{
1782 f->owner = avctx;
1783 return avctx->get_buffer(avctx, f);
1784}
1785
1786void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1787{
1788 f->owner->release_buffer(f->owner, f);
1789}
1790
1791void ff_thread_finish_setup(AVCodecContext *avctx)
1792{
1793}
1794
1795void ff_thread_report_progress(AVFrame *f, int progress, int field)
1796{
1797}
1798
1799void ff_thread_await_progress(AVFrame *f, int progress, int field)
1800{
1801}
1802
1803#endif
65af48b5 1804
bca06e77
AK
1805enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1806{
1807 if (codec_id <= CODEC_ID_NONE)
1808 return AVMEDIA_TYPE_UNKNOWN;
1809 else if (codec_id < CODEC_ID_FIRST_AUDIO)
1810 return AVMEDIA_TYPE_VIDEO;
1811 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1812 return AVMEDIA_TYPE_AUDIO;
1813 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1814 return AVMEDIA_TYPE_SUBTITLE;
1815
1816 return AVMEDIA_TYPE_UNKNOWN;
1817}
af08d9ae
AK
1818
1819int avcodec_is_open(AVCodecContext *s)
1820{
1821 return !!s->internal;
1822}