avcodec: add av_get_exact_bits_per_sample() function
[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;
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
d10319d8 556 assert(s->pix_fmt == pic->format);
87840eeb 557
e1c2a5a0 558 /* If internal buffer type return the same buffer */
b8af4fe9 559 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
62ecd363
NG
560 if(s->pkt) pic->pkt_pts= s->pkt->pts;
561 else pic->pkt_pts= AV_NOPTS_VALUE;
b8af4fe9 562 pic->reordered_opaque= s->reordered_opaque;
e1c2a5a0 563 return 0;
b8af4fe9 564 }
e1c2a5a0
RT
565
566 /*
567 * Not internal type and reget_buffer not overridden, emulate cr buffer
568 */
569 temp_pic = *pic;
560f773c 570 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
e1c2a5a0
RT
571 pic->data[i] = pic->base[i] = NULL;
572 pic->opaque = NULL;
573 /* Allocate new frame */
574 if (s->get_buffer(s, pic))
575 return -1;
576 /* Copy image data from old buffer to new buffer */
636d6a4a 577 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
e1c2a5a0
RT
578 s->height);
579 s->release_buffer(s, &temp_pic); // Release old frame
580 return 0;
581}
582
3a84713a 583int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
9c3d33d6
MN
584 int i;
585
586 for(i=0; i<count; i++){
3a84713a 587 int r= func(c, (char*)arg + i*size);
9c3d33d6
MN
588 if(ret) ret[i]= r;
589 }
590 return 0;
591}
592
8d23a86f
RD
593int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
594 int i;
595
596 for(i=0; i<count; i++){
597 int r= func(c, arg, i, 0);
598 if(ret) ret[i]= r;
599 }
600 return 0;
601}
602
c269cf68
MN
603enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
604 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
605 ++fmt;
a33c7159
MN
606 return fmt[0];
607}
608
9740beff
MN
609void avcodec_get_frame_defaults(AVFrame *pic){
610 memset(pic, 0, sizeof(AVFrame));
611
612 pic->pts= AV_NOPTS_VALUE;
c342499d 613 pic->key_frame= 1;
b58dbb5b 614 pic->sample_aspect_ratio = (AVRational){0, 1};
8a4a5f6f 615 pic->format = -1; /* unknown */
9740beff
MN
616}
617
492cd3a9 618AVFrame *avcodec_alloc_frame(void){
9740beff 619 AVFrame *pic= av_malloc(sizeof(AVFrame));
115329f1 620
9740beff 621 if(pic==NULL) return NULL;
115329f1 622
9740beff 623 avcodec_get_frame_defaults(pic);
115329f1 624
1e491e29
MN
625 return pic;
626}
627
0b950fe2
AK
628int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
629{
31d76ec2 630 int ret = 0;
0b950fe2
AK
631 AVDictionary *tmp = NULL;
632
af08d9ae
AK
633 if (avcodec_is_open(avctx))
634 return 0;
635
bc901998
AK
636 if ((!codec && !avctx->codec)) {
637 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
638 return AVERROR(EINVAL);
639 }
640 if ((codec && avctx->codec && codec != avctx->codec)) {
641 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
642 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
643 return AVERROR(EINVAL);
644 }
645 if (!codec)
646 codec = avctx->codec;
647
4df30f71
AC
648 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
649 return AVERROR(EINVAL);
650
0b950fe2
AK
651 if (options)
652 av_dict_copy(&tmp, *options, 0);
115329f1 653
f988ce6c
654 /* If there is a user-supplied mutex locking routine, call it. */
655 if (ff_lockmgr_cb) {
656 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
657 return -1;
658 }
659
ddebfb15
MN
660 entangled_thread_counter++;
661 if(entangled_thread_counter != 1){
662 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
31d76ec2 663 ret = -1;
ddebfb15
MN
664 goto end;
665 }
de6d9b64 666
f3a29b75
JR
667 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
668 if (!avctx->internal) {
669 ret = AVERROR(ENOMEM);
670 goto end;
671 }
672
0edf8a7a 673 if (codec->priv_data_size > 0) {
dc51a72b 674 if(!avctx->priv_data){
0edf8a7a 675 avctx->priv_data = av_mallocz(codec->priv_data_size);
90f06cea
PI
676 if (!avctx->priv_data) {
677 ret = AVERROR(ENOMEM);
ddebfb15 678 goto end;
90f06cea 679 }
0b950fe2 680 if (codec->priv_class) {
dc51a72b
MN
681 *(AVClass**)avctx->priv_data= codec->priv_class;
682 av_opt_set_defaults(avctx->priv_data);
683 }
684 }
1d36fb13 685 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
0b950fe2 686 goto free_and_end;
0edf8a7a
PG
687 } else {
688 avctx->priv_data = NULL;
689 }
0b950fe2
AK
690 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
691 goto free_and_end;
21adafec
MN
692
693 if(avctx->coded_width && avctx->coded_height)
694 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
695 else if(avctx->width && avctx->height)
696 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
697
82eac2f3
RD
698 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
699 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
700 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
701 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
702 avcodec_set_dimensions(avctx, 0, 0);
703 }
704
f19c58b4
AJ
705 /* if the decoder init function was already called previously,
706 free the already allocated subtitle_header before overwriting it */
44fe77b3 707 if (av_codec_is_decoder(codec))
f19c58b4
AJ
708 av_freep(&avctx->subtitle_header);
709
fa77dd63 710#define SANE_NB_CHANNELS 128U
82eac2f3 711 if (avctx->channels > SANE_NB_CHANNELS) {
7868349a 712 ret = AVERROR(EINVAL);
2a9b5c9b 713 goto free_and_end;
0ecca7a4
MN
714 }
715
b5c85991 716 avctx->codec = codec;
72415b2a 717 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
681c180d
RD
718 avctx->codec_id == CODEC_ID_NONE) {
719 avctx->codec_type = codec->type;
720 avctx->codec_id = codec->id;
721 }
e83c716e
AJ
722 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
723 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
4c0dda2b 724 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
31d76ec2 725 ret = AVERROR(EINVAL);
2a9b5c9b 726 goto free_and_end;
4c0dda2b 727 }
b5c85991 728 avctx->frame_number = 0;
37b00b47 729
9a7dc618
MS
730 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
731 (!avctx->time_base.num || !avctx->time_base.den)) {
732 avctx->time_base.num = 1;
733 avctx->time_base.den = avctx->sample_rate;
734 }
735
37b00b47 736 if (HAVE_THREADS && !avctx->thread_opaque) {
ba9ef8d0 737 ret = ff_thread_init(avctx);
37b00b47
AS
738 if (ret < 0) {
739 goto free_and_end;
740 }
741 }
b6064d9a
JG
742 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
743 avctx->thread_count = 1;
37b00b47 744
527c91e3
CEH
745 if (avctx->codec->max_lowres < avctx->lowres) {
746 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
747 avctx->codec->max_lowres);
31d76ec2 748 ret = AVERROR(EINVAL);
527c91e3
CEH
749 goto free_and_end;
750 }
44fe77b3 751 if (av_codec_is_encoder(avctx->codec)) {
2cfa2d92 752 int i;
8b00ab01 753 if (avctx->codec->sample_fmts) {
3dfc3e70
JR
754 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
755 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
756 break;
757 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
758 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
759 ret = AVERROR(EINVAL);
760 goto free_and_end;
761 }
8b00ab01 762 }
dcd2b55e
PM
763 if (avctx->codec->pix_fmts) {
764 for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
765 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
766 break;
767 if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE) {
768 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
769 ret = AVERROR(EINVAL);
770 goto free_and_end;
771 }
772 }
8b00ab01
JR
773 if (avctx->codec->supported_samplerates) {
774 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
775 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
776 break;
777 if (avctx->codec->supported_samplerates[i] == 0) {
778 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
779 ret = AVERROR(EINVAL);
780 goto free_and_end;
781 }
782 }
783 if (avctx->codec->channel_layouts) {
784 if (!avctx->channel_layout) {
785 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
786 } else {
787 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
788 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
789 break;
790 if (avctx->codec->channel_layouts[i] == 0) {
791 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
792 ret = AVERROR(EINVAL);
793 goto free_and_end;
794 }
795 }
796 }
168f9e8c
JR
797 if (avctx->channel_layout && avctx->channels) {
798 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
799 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
800 ret = AVERROR(EINVAL);
801 goto free_and_end;
802 }
688b09fa
JR
803 } else if (avctx->channel_layout) {
804 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
168f9e8c 805 }
2cfa2d92 806 }
0fd0ef79 807
37b00b47 808 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
2de4f9eb
MN
809 ret = avctx->codec->init(avctx);
810 if (ret < 0) {
2a9b5c9b 811 goto free_and_end;
2de4f9eb 812 }
6e546aaa 813 }
ddebfb15
MN
814end:
815 entangled_thread_counter--;
f988ce6c
816
817 /* Release any user-supplied mutex. */
818 if (ff_lockmgr_cb) {
819 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
820 }
0b950fe2
AK
821 if (options) {
822 av_dict_free(options);
823 *options = tmp;
824 }
825
ddebfb15 826 return ret;
2a9b5c9b 827free_and_end:
0b950fe2 828 av_dict_free(&tmp);
2a9b5c9b 829 av_freep(&avctx->priv_data);
f3a29b75 830 av_freep(&avctx->internal);
2a9b5c9b
MN
831 avctx->codec= NULL;
832 goto end;
de6d9b64
FB
833}
834
b2c75b6e 835int ff_alloc_packet(AVPacket *avpkt, int size)
de6d9b64 836{
b2c75b6e
JR
837 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
838 return AVERROR(EINVAL);
839
840 if (avpkt->data) {
e42e9b0e 841 void *destruct = avpkt->destruct;
b2c75b6e
JR
842
843 if (avpkt->size < size)
844 return AVERROR(EINVAL);
845
b2c75b6e 846 av_init_packet(avpkt);
e42e9b0e 847 avpkt->destruct = destruct;
1a670973 848 avpkt->size = size;
b2c75b6e
JR
849 return 0;
850 } else {
851 return av_new_packet(avpkt, size);
0ecca7a4 852 }
b2c75b6e
JR
853}
854
855int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
856 AVPacket *avpkt,
857 const AVFrame *frame,
858 int *got_packet_ptr)
859{
860 int ret;
861 int user_packet = !!avpkt->data;
862 int nb_samples;
863
52953d61
AK
864 *got_packet_ptr = 0;
865
b2c75b6e 866 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
7fb6c922 867 av_free_packet(avpkt);
b2c75b6e
JR
868 av_init_packet(avpkt);
869 avpkt->size = 0;
6f824977 870 return 0;
b2c75b6e
JR
871 }
872
873 /* check for valid frame size */
874 if (frame) {
875 nb_samples = frame->nb_samples;
876 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
877 if (nb_samples > avctx->frame_size)
878 return AVERROR(EINVAL);
879 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
880 if (nb_samples != avctx->frame_size)
881 return AVERROR(EINVAL);
882 }
883 } else {
884 nb_samples = avctx->frame_size;
885 }
886
887 if (avctx->codec->encode2) {
b2c75b6e 888 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
a75bc764
JR
889 if (!ret && *got_packet_ptr) {
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
JR
900 }
901 } else {
902 /* for compatibility with encoders not supporting encode2(), we need to
903 allocate a packet buffer if the user has not provided one or check
904 the size otherwise */
905 int fs_tmp = 0;
906 int buf_size = avpkt->size;
907 if (!user_packet) {
908 if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
909 av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
910 buf_size = nb_samples * avctx->channels *
911 av_get_bits_per_sample(avctx->codec_id) / 8;
912 } else {
913 /* this is a guess as to the required size.
914 if an encoder needs more than this, it should probably
915 implement encode2() */
916 buf_size = 2 * avctx->frame_size * avctx->channels *
917 av_get_bytes_per_sample(avctx->sample_fmt);
918 buf_size += FF_MIN_BUFFER_SIZE;
919 }
920 }
921 if ((ret = ff_alloc_packet(avpkt, buf_size)))
922 return ret;
923
924 /* Encoders using AVCodec.encode() that support
925 CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
926 the smaller size when encoding the last frame.
927 This code can be removed once all encoders supporting
928 CODEC_CAP_SMALL_LAST_FRAME use encode2() */
929 if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
930 nb_samples < avctx->frame_size) {
931 fs_tmp = avctx->frame_size;
932 avctx->frame_size = nb_samples;
933 }
934
935 /* encode the frame */
936 ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
937 frame ? frame->data[0] : NULL);
938 if (ret >= 0) {
939 if (!ret) {
940 /* no output. if the packet data was allocated by libavcodec,
941 free it */
942 if (!user_packet)
943 av_freep(&avpkt->data);
944 } else {
945 if (avctx->coded_frame)
a75bc764 946 avpkt->pts = avpkt->dts = avctx->coded_frame->pts;
b2c75b6e
JR
947 /* Set duration for final small packet. This can be removed
948 once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
949 encode2() */
950 if (fs_tmp) {
91a28b0e
JR
951 avpkt->duration = ff_samples_to_time_base(avctx,
952 avctx->frame_size);
b2c75b6e
JR
953 }
954 }
955 avpkt->size = ret;
956 *got_packet_ptr = (ret > 0);
957 ret = 0;
958 }
959
960 if (fs_tmp)
961 avctx->frame_size = fs_tmp;
962 }
963 if (!ret)
964 avctx->frame_number++;
965
7fb6c922
AK
966 if (ret < 0 || !*got_packet_ptr)
967 av_free_packet(avpkt);
968
b2c75b6e
JR
969 /* NOTE: if we add any audio encoders which output non-keyframe packets,
970 this needs to be moved to the encoders, but for now we can do it
971 here to simplify things */
972 avpkt->flags |= AV_PKT_FLAG_KEY;
973
974 return ret;
de6d9b64
FB
975}
976
b2c75b6e
JR
977#if FF_API_OLD_DECODE_AUDIO
978int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
979 uint8_t *buf, int buf_size,
980 const short *samples)
981{
982 AVPacket pkt;
983 AVFrame frame0;
984 AVFrame *frame;
985 int ret, samples_size, got_packet;
986
987 av_init_packet(&pkt);
988 pkt.data = buf;
989 pkt.size = buf_size;
990
991 if (samples) {
992 frame = &frame0;
993 avcodec_get_frame_defaults(frame);
994
995 if (avctx->frame_size) {
996 frame->nb_samples = avctx->frame_size;
997 } else {
998 /* if frame_size is not set, the number of samples must be
999 calculated from the buffer size */
1000 int64_t nb_samples;
1001 if (!av_get_bits_per_sample(avctx->codec_id)) {
1002 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1003 "support this codec\n");
1004 return AVERROR(EINVAL);
1005 }
1006 nb_samples = (int64_t)buf_size * 8 /
1007 (av_get_bits_per_sample(avctx->codec_id) *
1008 avctx->channels);
1009 if (nb_samples >= INT_MAX)
1010 return AVERROR(EINVAL);
1011 frame->nb_samples = nb_samples;
1012 }
1013
1014 /* it is assumed that the samples buffer is large enough based on the
1015 relevant parameters */
1016 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1017 frame->nb_samples,
1018 avctx->sample_fmt, 1);
1019 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1020 avctx->sample_fmt,
1021 samples, samples_size, 1)))
1022 return ret;
1023
1024 /* fabricate frame pts from sample count.
1025 this is needed because the avcodec_encode_audio() API does not have
1026 a way for the user to provide pts */
91a28b0e
JR
1027 frame->pts = ff_samples_to_time_base(avctx,
1028 avctx->internal->sample_count);
b2c75b6e
JR
1029 avctx->internal->sample_count += frame->nb_samples;
1030 } else {
1031 frame = NULL;
1032 }
1033
1034 got_packet = 0;
1035 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1036 if (!ret && got_packet && avctx->coded_frame) {
1037 avctx->coded_frame->pts = pkt.pts;
1038 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1039 }
1040 /* free any side data since we cannot return it */
1041 if (pkt.side_data_elems > 0) {
1042 int i;
1043 for (i = 0; i < pkt.side_data_elems; i++)
1044 av_free(pkt.side_data[i].data);
1045 av_freep(&pkt.side_data);
1046 pkt.side_data_elems = 0;
1047 }
1048
1049 if (frame && frame->extended_data != frame->data)
1050 av_free(frame->extended_data);
1051
1052 return ret ? ret : pkt.size;
1053}
1054#endif
1055
52f82a11 1056#if FF_API_OLD_ENCODE_VIDEO
5e4c7ca2 1057int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
492cd3a9 1058 const AVFrame *pict)
de6d9b64 1059{
52f82a11
AK
1060 AVPacket pkt;
1061 int ret, got_packet = 0;
1062
0ecca7a4 1063 if(buf_size < FF_MIN_BUFFER_SIZE){
5286d11f 1064 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
0ecca7a4
MN
1065 return -1;
1066 }
115329f1 1067
52f82a11
AK
1068 av_init_packet(&pkt);
1069 pkt.data = buf;
1070 pkt.size = buf_size;
1071
1072 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1073 if (!ret && got_packet && avctx->coded_frame) {
1074 avctx->coded_frame->pts = pkt.pts;
1075 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1076 }
1077
1078 /* free any side data since we cannot return it */
1079 if (pkt.side_data_elems > 0) {
1080 int i;
1081 for (i = 0; i < pkt.side_data_elems; i++)
1082 av_free(pkt.side_data[i].data);
1083 av_freep(&pkt.side_data);
1084 pkt.side_data_elems = 0;
1085 }
1086
1087 return ret ? ret : pkt.size;
1088}
1089#endif
1090
52f82a11
AK
1091int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1092 AVPacket *avpkt,
1093 const AVFrame *frame,
1094 int *got_packet_ptr)
1095{
1096 int ret;
1097 int user_packet = !!avpkt->data;
1098
d55fa6f9
AK
1099 *got_packet_ptr = 0;
1100
52f82a11 1101 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
7fb6c922 1102 av_free_packet(avpkt);
52f82a11
AK
1103 av_init_packet(avpkt);
1104 avpkt->size = 0;
6f824977 1105 return 0;
52f82a11
AK
1106 }
1107
1108 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1109 return AVERROR(EINVAL);
1110
ff311c09 1111 av_assert0(avctx->codec->encode2);
52f82a11 1112
ff311c09
AK
1113 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1114 if (!ret) {
1115 if (!*got_packet_ptr)
1116 avpkt->size = 0;
1117 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1118 avpkt->pts = avpkt->dts = frame->pts;
52f82a11 1119
eb727387
AK
1120 if (!user_packet && avpkt->data) {
1121 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
1122 if (new_data)
1123 avpkt->data = new_data;
1124 }
1125
52f82a11 1126 avctx->frame_number++;
03ca0a5b 1127 }
52f82a11 1128
7fb6c922
AK
1129 if (ret < 0 || !*got_packet_ptr)
1130 av_free_packet(avpkt);
1131
52f82a11
AK
1132 emms_c();
1133 return ret;
de6d9b64
FB
1134}
1135
115329f1 1136int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
240c1657
FB
1137 const AVSubtitle *sub)
1138{
1139 int ret;
9413db9e
BA
1140 if(sub->start_display_time) {
1141 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1142 return -1;
1143 }
505aa6c9
BA
1144 if(sub->num_rects == 0 || !sub->rects)
1145 return -1;
8eb027c8 1146 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
240c1657
FB
1147 avctx->frame_number++;
1148 return ret;
1149}
1150
867f923d
MS
1151static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1152{
1153 int size = 0;
1154 const uint8_t *data;
1155 uint32_t flags;
1156
1157 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1158 return;
1159
1160 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1161 if (!data || size < 4)
1162 return;
1163 flags = bytestream_get_le32(&data);
1164 size -= 4;
1165 if (size < 4) /* Required for any of the changes */
1166 return;
1167 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1168 avctx->channels = bytestream_get_le32(&data);
1169 size -= 4;
1170 }
1171 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1172 if (size < 8)
1173 return;
1174 avctx->channel_layout = bytestream_get_le64(&data);
1175 size -= 8;
1176 }
1177 if (size < 4)
1178 return;
1179 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1180 avctx->sample_rate = bytestream_get_le32(&data);
1181 size -= 4;
1182 }
1183 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1184 if (size < 8)
1185 return;
1186 avctx->width = bytestream_get_le32(&data);
1187 avctx->height = bytestream_get_le32(&data);
c5d907b6 1188 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
867f923d
MS
1189 size -= 8;
1190 }
1191}
1192
7a00bbad
TB
1193int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1194 int *got_picture_ptr,
1195 AVPacket *avpkt)
1196{
de6d9b64 1197 int ret;
115329f1 1198
53db1cae 1199 *got_picture_ptr= 0;
e16f217c 1200 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
0ecca7a4 1201 return -1;
393cbb96
MN
1202
1203 avctx->pkt = avpkt;
c5d907b6 1204 apply_param_change(avctx, avpkt);
393cbb96 1205
37b00b47 1206 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
27237d52 1207 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
37b00b47
AS
1208 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1209 avpkt);
1210 else {
1211 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1212 avpkt);
1213 picture->pkt_dts= avpkt->dts;
b58dbb5b 1214 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
3a2ddf7c
SS
1215 picture->width = avctx->width;
1216 picture->height = avctx->height;
8a4a5f6f 1217 picture->format = avctx->pix_fmt;
37b00b47 1218 }
6bb925f4 1219
bb628dae 1220 emms_c(); //needed to avoid an emms_c() call before every return;
115329f1
DB
1221
1222 if (*got_picture_ptr)
934982c4
MN
1223 avctx->frame_number++;
1224 }else
1225 ret= 0;
1226
de6d9b64
FB
1227 return ret;
1228}
1229
0eea2129 1230#if FF_API_OLD_DECODE_AUDIO
7a00bbad
TB
1231int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1232 int *frame_size_ptr,
1233 AVPacket *avpkt)
1234{
0eea2129
JR
1235 AVFrame frame;
1236 int ret, got_frame = 0;
1237
1238 if (avctx->get_buffer != avcodec_default_get_buffer) {
e2ff436e
RT
1239 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1240 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1241 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1242 "avcodec_decode_audio4()\n");
1243 avctx->get_buffer = avcodec_default_get_buffer;
0eea2129
JR
1244 }
1245
1246 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1247
1248 if (ret >= 0 && got_frame) {
1249 int ch, plane_size;
1250 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1251 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1252 frame.nb_samples,
1253 avctx->sample_fmt, 1);
1254 if (*frame_size_ptr < data_size) {
1255 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1256 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1257 return AVERROR(EINVAL);
1258 }
1259
1260 memcpy(samples, frame.extended_data[0], plane_size);
1261
1262 if (planar && avctx->channels > 1) {
1263 uint8_t *out = ((uint8_t *)samples) + plane_size;
1264 for (ch = 1; ch < avctx->channels; ch++) {
1265 memcpy(out, frame.extended_data[ch], plane_size);
1266 out += plane_size;
1267 }
1268 }
1269 *frame_size_ptr = data_size;
1270 } else {
1271 *frame_size_ptr = 0;
1272 }
1273 return ret;
1274}
1275#endif
1276
1277int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1278 AVFrame *frame,
1279 int *got_frame_ptr,
1280 AVPacket *avpkt)
1281{
1282 int ret = 0;
1283
1284 *got_frame_ptr = 0;
de6d9b64 1285
393cbb96
MN
1286 avctx->pkt = avpkt;
1287
6326afd5
JR
1288 if (!avpkt->data && avpkt->size) {
1289 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1290 return AVERROR(EINVAL);
1291 }
1292
f13db94d
MS
1293 apply_param_change(avctx, avpkt);
1294
0eea2129
JR
1295 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1296 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1297 if (ret >= 0 && *got_frame_ptr) {
1298 avctx->frame_number++;
1299 frame->pkt_dts = avpkt->dts;
8a4a5f6f
SS
1300 if (frame->format == AV_SAMPLE_FMT_NONE)
1301 frame->format = avctx->sample_fmt;
9c856d62 1302 }
ac66834c 1303 }
de6d9b64
FB
1304 return ret;
1305}
1306
7a00bbad
TB
1307int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1308 int *got_sub_ptr,
1309 AVPacket *avpkt)
1310{
240c1657
FB
1311 int ret;
1312
393cbb96 1313 avctx->pkt = avpkt;
240c1657 1314 *got_sub_ptr = 0;
7a00bbad 1315 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
240c1657
FB
1316 if (*got_sub_ptr)
1317 avctx->frame_number++;
1318 return ret;
1319}
1320
e1d7c883
RD
1321void avsubtitle_free(AVSubtitle *sub)
1322{
1323 int i;
1324
1325 for (i = 0; i < sub->num_rects; i++)
1326 {
8b834ac5
RD
1327 av_freep(&sub->rects[i]->pict.data[0]);
1328 av_freep(&sub->rects[i]->pict.data[1]);
1329 av_freep(&sub->rects[i]->pict.data[2]);
1330 av_freep(&sub->rects[i]->pict.data[3]);
1331 av_freep(&sub->rects[i]->text);
1332 av_freep(&sub->rects[i]->ass);
1333 av_freep(&sub->rects[i]);
e1d7c883
RD
1334 }
1335
8b834ac5 1336 av_freep(&sub->rects);
e1d7c883
RD
1337
1338 memset(sub, 0, sizeof(AVSubtitle));
1339}
1340
0752cd39 1341av_cold int avcodec_close(AVCodecContext *avctx)
de6d9b64 1342{
f988ce6c
1343 /* If there is a user-supplied mutex locking routine, call it. */
1344 if (ff_lockmgr_cb) {
1345 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1346 return -1;
1347 }
1348
ddebfb15
MN
1349 entangled_thread_counter++;
1350 if(entangled_thread_counter != 1){
1351 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1352 entangled_thread_counter--;
1353 return -1;
1354 }
1355
0e72ad95
AK
1356 if (avcodec_is_open(avctx)) {
1357 if (HAVE_THREADS && avctx->thread_opaque)
1358 ff_thread_free(avctx);
1359 if (avctx->codec && avctx->codec->close)
1360 avctx->codec->close(avctx);
1361 avcodec_default_free_buffers(avctx);
1362 avctx->coded_frame = NULL;
1363 av_freep(&avctx->internal);
1364 }
1365
1366 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
36773283
AK
1367 av_opt_free(avctx->priv_data);
1368 av_opt_free(avctx);
3123dd79 1369 av_freep(&avctx->priv_data);
44fe77b3 1370 if (av_codec_is_encoder(avctx->codec))
c4f267ab 1371 av_freep(&avctx->extradata);
de6d9b64 1372 avctx->codec = NULL;
37b00b47 1373 avctx->active_thread_type = 0;
ddebfb15 1374 entangled_thread_counter--;
f988ce6c
1375
1376 /* Release any user-supplied mutex. */
1377 if (ff_lockmgr_cb) {
1378 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1379 }
de6d9b64
FB
1380 return 0;
1381}
1382
1383AVCodec *avcodec_find_encoder(enum CodecID id)
1384{
93ebfeea 1385 AVCodec *p, *experimental=NULL;
de6d9b64
FB
1386 p = first_avcodec;
1387 while (p) {
44fe77b3 1388 if (av_codec_is_encoder(p) && p->id == id) {
93ebfeea
JG
1389 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1390 experimental = p;
1391 } else
1392 return p;
1393 }
de6d9b64
FB
1394 p = p->next;
1395 }
93ebfeea 1396 return experimental;
de6d9b64
FB
1397}
1398
98f3b098
A
1399AVCodec *avcodec_find_encoder_by_name(const char *name)
1400{
1401 AVCodec *p;
fc228c90
AJ
1402 if (!name)
1403 return NULL;
98f3b098
A
1404 p = first_avcodec;
1405 while (p) {
44fe77b3 1406 if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
98f3b098
A
1407 return p;
1408 p = p->next;
1409 }
1410 return NULL;
1411}
1412
de6d9b64
FB
1413AVCodec *avcodec_find_decoder(enum CodecID id)
1414{
1415 AVCodec *p;
1416 p = first_avcodec;
1417 while (p) {
44fe77b3 1418 if (av_codec_is_decoder(p) && p->id == id)
de6d9b64
FB
1419 return p;
1420 p = p->next;
1421 }
1422 return NULL;
1423}
1424
1425AVCodec *avcodec_find_decoder_by_name(const char *name)
1426{
1427 AVCodec *p;
fc228c90
AJ
1428 if (!name)
1429 return NULL;
de6d9b64
FB
1430 p = first_avcodec;
1431 while (p) {
44fe77b3 1432 if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
de6d9b64
FB
1433 return p;
1434 p = p->next;
1435 }
1436 return NULL;
1437}
1438
406aa93f 1439static int get_bit_rate(AVCodecContext *ctx)
ce34ff6b
RK
1440{
1441 int bit_rate;
1442 int bits_per_sample;
1443
1444 switch(ctx->codec_type) {
72415b2a 1445 case AVMEDIA_TYPE_VIDEO:
4563cf24
SS
1446 case AVMEDIA_TYPE_DATA:
1447 case AVMEDIA_TYPE_SUBTITLE:
1448 case AVMEDIA_TYPE_ATTACHMENT:
ce34ff6b
RK
1449 bit_rate = ctx->bit_rate;
1450 break;
72415b2a 1451 case AVMEDIA_TYPE_AUDIO:
ce34ff6b
RK
1452 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1453 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1454 break;
ce34ff6b
RK
1455 default:
1456 bit_rate = 0;
1457 break;
1458 }
1459 return bit_rate;
1460}
1461
7e566bbe
SS
1462size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1463{
1464 int i, len, ret = 0;
1465
1466 for (i = 0; i < 4; i++) {
1467 len = snprintf(buf, buf_size,
1468 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1469 buf += len;
1470 buf_size = buf_size > len ? buf_size - len : 0;
1471 ret += len;
1472 codec_tag>>=8;
1473 }
1474 return ret;
1475}
1476
de6d9b64
FB
1477void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1478{
1479 const char *codec_name;
2a81f4bd 1480 const char *profile = NULL;
de6d9b64
FB
1481 AVCodec *p;
1482 char buf1[32];
a96b68b7 1483 int bitrate;
59771f71 1484 AVRational display_aspect_ratio;
de6d9b64
FB
1485
1486 if (encode)
1487 p = avcodec_find_encoder(enc->codec_id);
1488 else
1489 p = avcodec_find_decoder(enc->codec_id);
1490
1491 if (p) {
1492 codec_name = p->name;
2a81f4bd 1493 profile = av_get_profile_name(p, enc->profile);
985180a1
FB
1494 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
1495 /* fake mpeg2 transport stream codec (currently not
1496 registered) */
1497 codec_name = "mpeg2ts";
de6d9b64
FB
1498 } else if (enc->codec_name[0] != '\0') {
1499 codec_name = enc->codec_name;
1500 } else {
1501 /* output avi tags */
ab0b5378
SS
1502 char tag_buf[32];
1503 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1504 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
de6d9b64
FB
1505 codec_name = buf1;
1506 }
1507
1508 switch(enc->codec_type) {
72415b2a 1509 case AVMEDIA_TYPE_VIDEO:
de6d9b64
FB
1510 snprintf(buf, buf_size,
1511 "Video: %s%s",
7d1c3fc1 1512 codec_name, enc->mb_decision ? " (hq)" : "");
2a81f4bd
AH
1513 if (profile)
1514 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1515 " (%s)", profile);
82c0c4ba 1516 if (enc->pix_fmt != PIX_FMT_NONE) {
cf087595
FB
1517 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1518 ", %s",
94bed8e5 1519 av_get_pix_fmt_name(enc->pix_fmt));
cf087595 1520 }
de6d9b64
FB
1521 if (enc->width) {
1522 snprintf(buf + strlen(buf), buf_size - strlen(buf),
21189011
MN
1523 ", %dx%d",
1524 enc->width, enc->height);
7ee4dd02 1525 if (enc->sample_aspect_ratio.num) {
cbaf50f8
BC
1526 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1527 enc->width*enc->sample_aspect_ratio.num,
1528 enc->height*enc->sample_aspect_ratio.den,
1529 1024*1024);
1530 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1531 " [PAR %d:%d DAR %d:%d]",
1532 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1533 display_aspect_ratio.num, display_aspect_ratio.den);
7ee4dd02 1534 }
a309073b 1535 if(av_log_get_level() >= AV_LOG_DEBUG){
9ce6c138 1536 int g= av_gcd(enc->time_base.num, enc->time_base.den);
21189011
MN
1537 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1538 ", %d/%d",
1539 enc->time_base.num/g, enc->time_base.den/g);
1540 }
de6d9b64 1541 }
4bfad535
FB
1542 if (encode) {
1543 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1544 ", q=%d-%d", enc->qmin, enc->qmax);
1545 }
de6d9b64 1546 break;
72415b2a 1547 case AVMEDIA_TYPE_AUDIO:
de6d9b64
FB
1548 snprintf(buf, buf_size,
1549 "Audio: %s",
1550 codec_name);
2a81f4bd
AH
1551 if (profile)
1552 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1553 " (%s)", profile);
de6d9b64
FB
1554 if (enc->sample_rate) {
1555 snprintf(buf + strlen(buf), buf_size - strlen(buf),
0d72e7d0 1556 ", %d Hz", enc->sample_rate);
de6d9b64 1557 }
0d72e7d0 1558 av_strlcat(buf, ", ", buf_size);
63e8d976 1559 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
5d6e4c16 1560 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
9e82a113 1561 snprintf(buf + strlen(buf), buf_size - strlen(buf),
ba7d6e79 1562 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
9e82a113 1563 }
de6d9b64 1564 break;
72415b2a 1565 case AVMEDIA_TYPE_DATA:
985180a1 1566 snprintf(buf, buf_size, "Data: %s", codec_name);
240c1657 1567 break;
72415b2a 1568 case AVMEDIA_TYPE_SUBTITLE:
240c1657 1569 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
985180a1 1570 break;
72415b2a 1571 case AVMEDIA_TYPE_ATTACHMENT:
f8d7c9d3 1572 snprintf(buf, buf_size, "Attachment: %s", codec_name);
f8d7c9d3 1573 break;
de6d9b64 1574 default:
9fe5a7b8
MN
1575 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1576 return;
de6d9b64 1577 }
4bfad535
FB
1578 if (encode) {
1579 if (enc->flags & CODEC_FLAG_PASS1)
1580 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1581 ", pass 1");
1582 if (enc->flags & CODEC_FLAG_PASS2)
1583 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1584 ", pass 2");
1585 }
406aa93f 1586 bitrate = get_bit_rate(enc);
a96b68b7 1587 if (bitrate != 0) {
115329f1 1588 snprintf(buf + strlen(buf), buf_size - strlen(buf),
a96b68b7 1589 ", %d kb/s", bitrate / 1000);
de6d9b64
FB
1590 }
1591}
1592
060ec0a8
AH
1593const char *av_get_profile_name(const AVCodec *codec, int profile)
1594{
1595 const AVProfile *p;
1596 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1597 return NULL;
1598
1599 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1600 if (p->profile == profile)
1601 return p->name;
1602
1603 return NULL;
1604}
1605
156e5023
NK
1606unsigned avcodec_version( void )
1607{
1608 return LIBAVCODEC_VERSION_INT;
1609}
cf087595 1610
41600690 1611const char *avcodec_configuration(void)
c1736936 1612{
29ba0911 1613 return LIBAV_CONFIGURATION;
c1736936
DB
1614}
1615
41600690 1616const char *avcodec_license(void)
c1736936
DB
1617{
1618#define LICENSE_PREFIX "libavcodec license: "
a03be6e1 1619 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
c1736936
DB
1620}
1621
1c2a8c7f
MN
1622void avcodec_flush_buffers(AVCodecContext *avctx)
1623{
27237d52 1624 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
37b00b47 1625 ff_thread_flush(avctx);
d1cf4591 1626 else if(avctx->codec->flush)
7a06ff14 1627 avctx->codec->flush(avctx);
1c2a8c7f
MN
1628}
1629
0eea2129
JR
1630static void video_free_buffers(AVCodecContext *s)
1631{
f3a29b75 1632 AVCodecInternal *avci = s->internal;
d90cf87b
MN
1633 int i, j;
1634
f3a29b75
JR
1635 if (!avci->buffer)
1636 return;
115329f1 1637
f3a29b75
JR
1638 if (avci->buffer_count)
1639 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1640 avci->buffer_count);
d90cf87b 1641 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
f3a29b75 1642 InternalBuffer *buf = &avci->buffer[i];
d90cf87b
MN
1643 for(j=0; j<4; j++){
1644 av_freep(&buf->base[j]);
1645 buf->data[j]= NULL;
1646 }
1647 }
f3a29b75 1648 av_freep(&avci->buffer);
115329f1 1649
f3a29b75 1650 avci->buffer_count=0;
d90cf87b
MN
1651}
1652
0eea2129
JR
1653static void audio_free_buffers(AVCodecContext *avctx)
1654{
1655 AVCodecInternal *avci = avctx->internal;
1656 InternalBuffer *buf;
1657
1658 if (!avci->buffer)
1659 return;
1660 buf = avci->buffer;
1661
1662 if (buf->extended_data) {
1663 av_free(buf->extended_data[0]);
1664 if (buf->extended_data != buf->data)
1665 av_free(buf->extended_data);
1666 }
1667 av_freep(&avci->buffer);
1668}
1669
1670void avcodec_default_free_buffers(AVCodecContext *avctx)
1671{
1672 switch (avctx->codec_type) {
1673 case AVMEDIA_TYPE_VIDEO:
1674 video_free_buffers(avctx);
1675 break;
1676 case AVMEDIA_TYPE_AUDIO:
1677 audio_free_buffers(avctx);
1678 break;
1679 default:
1680 break;
1681 }
1682}
1683
6699d074
JR
1684int av_get_exact_bits_per_sample(enum CodecID codec_id)
1685{
ac3e1834 1686 switch(codec_id){
f1b163e0 1687 case CODEC_ID_ADPCM_CT:
220506d2 1688 case CODEC_ID_ADPCM_IMA_APC:
6699d074
JR
1689 case CODEC_ID_ADPCM_IMA_EA_SEAD:
1690 case CODEC_ID_ADPCM_IMA_WS:
029f966c 1691 case CODEC_ID_ADPCM_G722:
6699d074 1692 case CODEC_ID_ADPCM_YAMAHA:
f32fd318 1693 return 4;
ac3e1834
BC
1694 case CODEC_ID_PCM_ALAW:
1695 case CODEC_ID_PCM_MULAW:
1696 case CODEC_ID_PCM_S8:
1697 case CODEC_ID_PCM_U8:
9d49b8ff 1698 case CODEC_ID_PCM_ZORK:
ac3e1834
BC
1699 return 8;
1700 case CODEC_ID_PCM_S16BE:
1701 case CODEC_ID_PCM_S16LE:
725d86bf 1702 case CODEC_ID_PCM_S16LE_PLANAR:
ac3e1834
BC
1703 case CODEC_ID_PCM_U16BE:
1704 case CODEC_ID_PCM_U16LE:
1705 return 16;
1706 case CODEC_ID_PCM_S24DAUD:
1707 case CODEC_ID_PCM_S24BE:
1708 case CODEC_ID_PCM_S24LE:
1709 case CODEC_ID_PCM_U24BE:
1710 case CODEC_ID_PCM_U24LE:
1711 return 24;
1712 case CODEC_ID_PCM_S32BE:
1713 case CODEC_ID_PCM_S32LE:
1714 case CODEC_ID_PCM_U32BE:
1715 case CODEC_ID_PCM_U32LE:
aa29709e 1716 case CODEC_ID_PCM_F32BE:
143a5d6f 1717 case CODEC_ID_PCM_F32LE:
ac3e1834 1718 return 32;
143a5d6f
PR
1719 case CODEC_ID_PCM_F64BE:
1720 case CODEC_ID_PCM_F64LE:
1721 return 64;
ac3e1834
BC
1722 default:
1723 return 0;
1724 }
1725}
1726
6699d074
JR
1727int av_get_bits_per_sample(enum CodecID codec_id)
1728{
1729 switch (codec_id) {
1730 case CODEC_ID_ADPCM_SBPRO_2:
1731 return 2;
1732 case CODEC_ID_ADPCM_SBPRO_3:
1733 return 3;
1734 case CODEC_ID_ADPCM_SBPRO_4:
1735 case CODEC_ID_ADPCM_IMA_WAV:
1736 case CODEC_ID_ADPCM_IMA_QT:
1737 case CODEC_ID_ADPCM_SWF:
1738 case CODEC_ID_ADPCM_MS:
1739 return 4;
1740 default:
1741 return av_get_exact_bits_per_sample(codec_id);
1742 }
1743}
1744
b250f9c6 1745#if !HAVE_THREADS
ba9ef8d0 1746int ff_thread_init(AVCodecContext *s){
ca8ad847
MN
1747 return -1;
1748}
1749#endif
ad2b531d
MR
1750
1751unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1752{
1753 unsigned int n = 0;
1754
1755 while(v >= 0xff) {
1756 *s++ = 0xff;
1757 v -= 0xff;
1758 n++;
1759 }
1760 *s = v;
1761 n++;
1762 return n;
1763}
1005f542 1764
c46eeae2
MN
1765int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1766 int i;
1767 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1768 return i;
1769}
1770
ce863d7f 1771void av_log_missing_feature(void *avc, const char *feature, int want_sample)
ea779d91 1772{
6001dad6 1773 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
db323491 1774 "version to the newest one from Git. If the problem still "
ea779d91 1775 "occurs, it means that your file has a feature which has not "
3fd3632f 1776 "been implemented.\n", feature);
ea779d91 1777 if(want_sample)
ce863d7f 1778 av_log_ask_for_sample(avc, NULL);
0ba39dd1
KG
1779}
1780
44f566b7 1781void av_log_ask_for_sample(void *avc, const char *msg, ...)
0ba39dd1 1782{
44f566b7
DB
1783 va_list argument_list;
1784
1785 va_start(argument_list, msg);
1786
0ba39dd1 1787 if (msg)
44f566b7 1788 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
0ba39dd1 1789 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
f0a41afd 1790 "of this file to ftp://upload.libav.org/incoming/ "
21de9204 1791 "and contact the libav-devel mailing list.\n");
44f566b7
DB
1792
1793 va_end(argument_list);
ea779d91 1794}
c895618b
MN
1795
1796static AVHWAccel *first_hwaccel = NULL;
1797
1798void av_register_hwaccel(AVHWAccel *hwaccel)
1799{
1800 AVHWAccel **p = &first_hwaccel;
1801 while (*p)
1802 p = &(*p)->next;
1803 *p = hwaccel;
1804 hwaccel->next = NULL;
1805}
414d9d7f
MN
1806
1807AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1808{
1809 return hwaccel ? hwaccel->next : first_hwaccel;
1810}
6059f13c
MN
1811
1812AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1813{
1814 AVHWAccel *hwaccel=NULL;
1815
1816 while((hwaccel= av_hwaccel_next(hwaccel))){
1817 if ( hwaccel->id == codec_id
1818 && hwaccel->pix_fmt == pix_fmt)
1819 return hwaccel;
1820 }
1821 return NULL;
1822}
f988ce6c
1823
1824int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1825{
1826 if (ff_lockmgr_cb) {
1827 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1828 return -1;
2d1b6fb7
MS
1829 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1830 return -1;
f988ce6c
1831 }
1832
1833 ff_lockmgr_cb = cb;
1834
1835 if (ff_lockmgr_cb) {
1836 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1837 return -1;
2d1b6fb7
MS
1838 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1839 return -1;
1840 }
1841 return 0;
1842}
1843
1844int avpriv_lock_avformat(void)
1845{
1846 if (ff_lockmgr_cb) {
1847 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1848 return -1;
1849 }
1850 return 0;
1851}
1852
1853int avpriv_unlock_avformat(void)
1854{
1855 if (ff_lockmgr_cb) {
1856 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1857 return -1;
f988ce6c
1858 }
1859 return 0;
1860}
603a5f04 1861
0842d589 1862unsigned int avpriv_toupper4(unsigned int x)
603a5f04
FL
1863{
1864 return toupper( x &0xFF)
1865 + (toupper((x>>8 )&0xFF)<<8 )
1866 + (toupper((x>>16)&0xFF)<<16)
1867 + (toupper((x>>24)&0xFF)<<24);
1868}
37b00b47 1869
27237d52 1870#if !HAVE_THREADS
37b00b47
AS
1871
1872int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1873{
1874 f->owner = avctx;
1875 return avctx->get_buffer(avctx, f);
1876}
1877
1878void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1879{
1880 f->owner->release_buffer(f->owner, f);
1881}
1882
1883void ff_thread_finish_setup(AVCodecContext *avctx)
1884{
1885}
1886
1887void ff_thread_report_progress(AVFrame *f, int progress, int field)
1888{
1889}
1890
1891void ff_thread_await_progress(AVFrame *f, int progress, int field)
1892{
1893}
1894
1895#endif
65af48b5 1896
bca06e77
AK
1897enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1898{
1899 if (codec_id <= CODEC_ID_NONE)
1900 return AVMEDIA_TYPE_UNKNOWN;
1901 else if (codec_id < CODEC_ID_FIRST_AUDIO)
1902 return AVMEDIA_TYPE_VIDEO;
1903 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1904 return AVMEDIA_TYPE_AUDIO;
1905 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1906 return AVMEDIA_TYPE_SUBTITLE;
1907
1908 return AVMEDIA_TYPE_UNKNOWN;
1909}
af08d9ae
AK
1910
1911int avcodec_is_open(AVCodecContext *s)
1912{
1913 return !!s->internal;
1914}