avcodec: add a public function, avcodec_fill_audio_frame().
[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
7fb94406 28#include "libavutil/avstring.h"
245976da 29#include "libavutil/crc.h"
0ebcdf5c 30#include "libavutil/mathematics.h"
eb285cfe 31#include "libavutil/pixdesc.h"
737eb597
RT
32#include "libavutil/audioconvert.h"
33#include "libavutil/imgutils.h"
34#include "libavutil/samplefmt.h"
0b950fe2 35#include "libavutil/dict.h"
de6d9b64 36#include "avcodec.h"
3123dd79 37#include "dsputil.h"
6ed04040 38#include "libavutil/opt.h"
db7ae7d1 39#include "imgconvert.h"
37b00b47 40#include "thread.h"
9e82a113 41#include "audioconvert.h"
0ba39dd1 42#include "internal.h"
f13db94d 43#include "bytestream.h"
7246177d 44#include <stdlib.h>
9b879566 45#include <stdarg.h>
4c263142 46#include <limits.h>
860a40c8 47#include <float.h>
de6d9b64 48
ddebfb15 49static int volatile entangled_thread_counter=0;
bb875b75 50static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
f988ce6c 51static void *codec_mutex;
2d1b6fb7 52static void *avformat_mutex;
ddebfb15 53
3453a231 54void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
8e1e6f31 55{
115329f1 56 if(min_size < *size)
8e1e6f31 57 return ptr;
115329f1 58
372c3f82 59 min_size= FFMAX(17*min_size/16 + 32, min_size);
8e1e6f31 60
372c3f82 61 ptr= av_realloc(ptr, min_size);
978805b2 62 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
63 min_size= 0;
64
65 *size= min_size;
978805b2
MN
66
67 return ptr;
8e1e6f31
FB
68}
69
3453a231 70void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
238ef6da
RD
71{
72 void **p = ptr;
73 if (min_size < *size)
74 return;
372c3f82 75 min_size= FFMAX(17*min_size/16 + 32, min_size);
238ef6da 76 av_free(*p);
372c3f82
MN
77 *p = av_malloc(min_size);
78 if (!*p) min_size = 0;
79 *size= min_size;
238ef6da
RD
80}
81
de6d9b64 82/* encoder management */
e6df765e 83static AVCodec *first_avcodec = NULL;
de6d9b64 84
55b9e69a
MN
85AVCodec *av_codec_next(AVCodec *c){
86 if(c) return c->next;
87 else return first_avcodec;
88}
89
9ecfbb3e
AK
90#if !FF_API_AVCODEC_INIT
91static
92#endif
93void avcodec_init(void)
94{
95 static int initialized = 0;
96
97 if (initialized != 0)
98 return;
99 initialized = 1;
100
101 dsputil_static_init();
102}
103
85662f49 104void avcodec_register(AVCodec *codec)
de6d9b64
FB
105{
106 AVCodec **p;
7a961a46 107 avcodec_init();
de6d9b64
FB
108 p = &first_avcodec;
109 while (*p != NULL) p = &(*p)->next;
335a761a
SS
110 *p = codec;
111 codec->next = NULL;
d97efd7f
AK
112
113 if (codec->init_static_data)
114 codec->init_static_data(codec);
de6d9b64
FB
115}
116
0fb49b59
BB
117unsigned avcodec_get_edge_width(void)
118{
119 return EDGE_WIDTH;
120}
121
21adafec
MN
122void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
123 s->coded_width = width;
124 s->coded_height= height;
125 s->width = -((-width )>>s->lowres);
126 s->height= -((-height)>>s->lowres);
127}
128
6a9c8594 129#define INTERNAL_BUFFER_SIZE (32+1)
1e491e29 130
560f773c
JR
131void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
132 int linesize_align[AV_NUM_DATA_POINTERS])
133{
134 int i;
115329f1
DB
135 int w_align= 1;
136 int h_align= 1;
137
f0bbfc4a
MN
138 switch(s->pix_fmt){
139 case PIX_FMT_YUV420P:
71e445fc 140 case PIX_FMT_YUYV422:
ebb177dd 141 case PIX_FMT_UYVY422:
f0bbfc4a 142 case PIX_FMT_YUV422P:
98c82d69 143 case PIX_FMT_YUV440P:
f0bbfc4a 144 case PIX_FMT_YUV444P:
db431f7e 145 case PIX_FMT_GBRP:
f0bbfc4a 146 case PIX_FMT_GRAY8:
34380af0
KS
147 case PIX_FMT_GRAY16BE:
148 case PIX_FMT_GRAY16LE:
f0bbfc4a
MN
149 case PIX_FMT_YUVJ420P:
150 case PIX_FMT_YUVJ422P:
98c82d69 151 case PIX_FMT_YUVJ440P:
f0bbfc4a 152 case PIX_FMT_YUVJ444P:
b70335a2 153 case PIX_FMT_YUVA420P:
42239ced
OA
154 case PIX_FMT_YUV420P9LE:
155 case PIX_FMT_YUV420P9BE:
156 case PIX_FMT_YUV420P10LE:
157 case PIX_FMT_YUV420P10BE:
dc49bf12
RB
158 case PIX_FMT_YUV422P9LE:
159 case PIX_FMT_YUV422P9BE:
5c511ad4
BC
160 case PIX_FMT_YUV422P10LE:
161 case PIX_FMT_YUV422P10BE:
da55ee6c
JGG
162 case PIX_FMT_YUV444P9LE:
163 case PIX_FMT_YUV444P9BE:
164 case PIX_FMT_YUV444P10LE:
165 case PIX_FMT_YUV444P10BE:
db431f7e
RB
166 case PIX_FMT_GBRP9LE:
167 case PIX_FMT_GBRP9BE:
168 case PIX_FMT_GBRP10LE:
169 case PIX_FMT_GBRP10BE:
37c0dc62
RC
170 w_align = 16; //FIXME assume 16 pixel per macroblock
171 h_align = 16 * 2; // interlaced needs 2 macroblocks height
f0bbfc4a
MN
172 break;
173 case PIX_FMT_YUV411P:
71e445fc 174 case PIX_FMT_UYYVYY411:
f0bbfc4a
MN
175 w_align=32;
176 h_align=8;
177 break;
178 case PIX_FMT_YUV410P:
179 if(s->codec_id == CODEC_ID_SVQ1){
180 w_align=64;
181 h_align=64;
182 }
d99fbbf4
RT
183 case PIX_FMT_RGB555:
184 if(s->codec_id == CODEC_ID_RPZA){
185 w_align=4;
186 h_align=4;
187 }
188 case PIX_FMT_PAL8:
6337178b
MN
189 case PIX_FMT_BGR8:
190 case PIX_FMT_RGB8:
d99fbbf4
RT
191 if(s->codec_id == CODEC_ID_SMC){
192 w_align=4;
193 h_align=4;
194 }
f0bbfc4a 195 break;
c31b8121
RT
196 case PIX_FMT_BGR24:
197 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
198 w_align=4;
199 h_align=4;
200 }
201 break;
f0bbfc4a
MN
202 default:
203 w_align= 1;
204 h_align= 1;
205 break;
206 }
207
ef516f73
DC
208 *width = FFALIGN(*width , w_align);
209 *height= FFALIGN(*height, h_align);
ba68d9d3 210 if(s->codec_id == CODEC_ID_H264 || s->lowres)
ae4ffe9f 211 *height+=2; // some of the optimized chroma MC reads one line too much
ba68d9d3 212 // which is also done in mpeg decoders with lowres > 0
eb285cfe 213
560f773c
JR
214 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
215 linesize_align[i] = STRIDE_ALIGN;
eb285cfe
RD
216//STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
217//we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
218//picture size unneccessarily in some cases. The solution here is not
219//pretty and better ideas are welcome!
220#if HAVE_MMX
221 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
222 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
223 s->codec_id == CODEC_ID_VP6A) {
0eea2129
JR
224 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
225 linesize_align[i] = 16;
eb285cfe
RD
226 }
227#endif
228}
229
230void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
231 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
560f773c 232 int linesize_align[AV_NUM_DATA_POINTERS];
eb285cfe
RD
233 int align;
234 avcodec_align_dimensions2(s, width, height, linesize_align);
235 align = FFMAX(linesize_align[0], linesize_align[3]);
236 linesize_align[1] <<= chroma_shift;
237 linesize_align[2] <<= chroma_shift;
238 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
239 *width=FFALIGN(*width, align);
f0bbfc4a
MN
240}
241
5ee5fa02
JR
242int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
243 enum AVSampleFormat sample_fmt, const uint8_t *buf,
244 int buf_size, int align)
245{
246 int ch, planar, needed_size, ret = 0;
247
248 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
249 frame->nb_samples, sample_fmt,
250 align);
251 if (buf_size < needed_size)
252 return AVERROR(EINVAL);
253
254 planar = av_sample_fmt_is_planar(sample_fmt);
255 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
256 if (!(frame->extended_data = av_mallocz(nb_channels *
257 sizeof(*frame->extended_data))))
258 return AVERROR(ENOMEM);
259 } else {
260 frame->extended_data = frame->data;
261 }
262
263 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
264 buf, nb_channels, frame->nb_samples,
265 sample_fmt, align)) < 0) {
266 if (frame->extended_data != frame->data)
267 av_free(frame->extended_data);
268 return ret;
269 }
270 if (frame->extended_data != frame->data) {
271 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
272 frame->data[ch] = frame->extended_data[ch];
273 }
274
275 return ret;
276}
277
0eea2129
JR
278static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
279{
280 AVCodecInternal *avci = avctx->internal;
281 InternalBuffer *buf;
5ee5fa02 282 int buf_size, ret;
0eea2129
JR
283
284 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
285 frame->nb_samples, avctx->sample_fmt,
286 32);
287 if (buf_size < 0)
288 return AVERROR(EINVAL);
289
0eea2129
JR
290 /* allocate InternalBuffer if needed */
291 if (!avci->buffer) {
292 avci->buffer = av_mallocz(sizeof(InternalBuffer));
293 if (!avci->buffer)
294 return AVERROR(ENOMEM);
295 }
296 buf = avci->buffer;
297
298 /* if there is a previously-used internal buffer, check its size and
299 channel count to see if we can reuse it */
300 if (buf->extended_data) {
301 /* if current buffer is too small, free it */
302 if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
303 av_free(buf->extended_data[0]);
304 if (buf->extended_data != buf->data)
305 av_free(&buf->extended_data);
306 buf->extended_data = NULL;
307 buf->data[0] = NULL;
308 }
309 /* if number of channels has changed, reset and/or free extended data
310 pointers but leave data buffer in buf->data[0] for reuse */
311 if (buf->nb_channels != avctx->channels) {
312 if (buf->extended_data != buf->data)
313 av_free(buf->extended_data);
314 buf->extended_data = NULL;
315 }
316 }
317
318 /* if there is no previous buffer or the previous buffer cannot be used
319 as-is, allocate a new buffer and/or rearrange the channel pointers */
320 if (!buf->extended_data) {
5ee5fa02
JR
321 if (!buf->data[0]) {
322 if (!(buf->data[0] = av_mallocz(buf_size)))
0eea2129 323 return AVERROR(ENOMEM);
5ee5fa02 324 buf->audio_data_size = buf_size;
0eea2129 325 }
5ee5fa02
JR
326 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
327 avctx->sample_fmt, buf->data[0],
328 buf->audio_data_size, 32)))
0eea2129
JR
329 return ret;
330
5ee5fa02
JR
331 if (frame->extended_data == frame->data)
332 buf->extended_data = buf->data;
333 else
334 buf->extended_data = frame->extended_data;
335 memcpy(buf->data, frame->data, sizeof(frame->data));
336 buf->linesize[0] = frame->linesize[0];
337 buf->nb_channels = avctx->channels;
338 } else {
339 /* copy InternalBuffer info to the AVFrame */
340 frame->extended_data = buf->extended_data;
341 frame->linesize[0] = buf->linesize[0];
342 memcpy(frame->data, buf->data, sizeof(frame->data));
0eea2129
JR
343 }
344
0eea2129 345 frame->type = FF_BUFFER_TYPE_INTERNAL;
0eea2129
JR
346
347 if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
348 else frame->pkt_pts = AV_NOPTS_VALUE;
349 frame->reordered_opaque = avctx->reordered_opaque;
350
351 if (avctx->debug & FF_DEBUG_BUFFERS)
352 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
353 "internal audio buffer used\n", frame);
354
355 return 0;
356}
357
358static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
359{
1e491e29 360 int i;
f0bbfc4a
MN
361 int w= s->width;
362 int h= s->height;
d90cf87b 363 InternalBuffer *buf;
f3a29b75 364 AVCodecInternal *avci = s->internal;
0ecca7a4 365
65d999d6
MB
366 if(pic->data[0]!=NULL) {
367 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
368 return -1;
369 }
f3a29b75
JR
370 if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
371 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
65d999d6
MB
372 return -1;
373 }
1e491e29 374
e16f217c 375 if(av_image_check_size(w, h, 0, s))
0ecca7a4
MN
376 return -1;
377
f3a29b75
JR
378 if (!avci->buffer) {
379 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
380 sizeof(InternalBuffer));
d90cf87b 381 }
115329f1 382
f3a29b75 383 buf = &avci->buffer[avci->buffer_count];
115329f1 384
0701006e 385 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
37b00b47
AS
386 if(s->active_thread_type&FF_THREAD_FRAME) {
387 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
388 return -1;
389 }
390
560f773c 391 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
0701006e
MN
392 av_freep(&buf->base[i]);
393 buf->data[i]= NULL;
394 }
395 }
396
8400b126 397 if (!buf->base[0]) {
f0bbfc4a 398 int h_chroma_shift, v_chroma_shift;
db7ae7d1
VS
399 int size[4] = {0};
400 int tmpsize;
c9d6e847 401 int unaligned;
c7622f9a 402 AVPicture picture;
560f773c 403 int stride_align[AV_NUM_DATA_POINTERS];
6e3ef511 404 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
c7622f9a 405
1e491e29 406 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
f0bbfc4a 407
eb285cfe 408 avcodec_align_dimensions2(s, &w, &h, stride_align);
115329f1 409
1e491e29
MN
410 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
411 w+= EDGE_WIDTH*2;
412 h+= EDGE_WIDTH*2;
413 }
5b67307a 414
c9d6e847
RD
415 do {
416 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
417 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
9d2e0ad8 418 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
c9d6e847
RD
419 // increase alignment of w for next try (rhs gives the lowest bit set in w)
420 w += w & ~(w-1);
db7ae7d1 421
c9d6e847 422 unaligned = 0;
45bae968 423 for (i=0; i<4; i++){
c9d6e847 424 unaligned |= picture.linesize[i] % stride_align[i];
45bae968 425 }
c9d6e847 426 } while (unaligned);
db7ae7d1 427
9d2e0ad8 428 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
f8c96d01
RD
429 if (tmpsize < 0)
430 return -1;
db7ae7d1
VS
431
432 for (i=0; i<3 && picture.data[i+1]; i++)
433 size[i] = picture.data[i+1] - picture.data[i];
cf73e32a 434 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
c7622f9a 435
c7622f9a
MN
436 memset(buf->base, 0, sizeof(buf->base));
437 memset(buf->data, 0, sizeof(buf->data));
1e491e29 438
b70335a2 439 for(i=0; i<4 && size[i]; i++){
2c19981a
MN
440 const int h_shift= i==0 ? 0 : h_chroma_shift;
441 const int v_shift= i==0 ? 0 : v_chroma_shift;
1e491e29 442
c7622f9a 443 buf->linesize[i]= picture.linesize[i];
1e491e29 444
c7622f9a 445 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
d90cf87b 446 if(buf->base[i]==NULL) return -1;
c7622f9a
MN
447 memset(buf->base[i], 128, size[i]);
448
3491a9b2 449 // no edge if EDGE EMU or not planar YUV
6337178b 450 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
d90cf87b 451 buf->data[i] = buf->base[i];
1e491e29 452 else
6e3ef511 453 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
1e491e29 454 }
560f773c
JR
455 for (; i < AV_NUM_DATA_POINTERS; i++) {
456 buf->base[i] = buf->data[i] = NULL;
457 buf->linesize[i] = 0;
458 }
6337178b 459 if(size[1] && !size[2])
ed5d30d9 460 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
0701006e
MN
461 buf->width = s->width;
462 buf->height = s->height;
463 buf->pix_fmt= s->pix_fmt;
1e491e29 464 }
237e4938 465 pic->type= FF_BUFFER_TYPE_INTERNAL;
1e491e29 466
560f773c 467 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
d90cf87b
MN
468 pic->base[i]= buf->base[i];
469 pic->data[i]= buf->data[i];
237e4938 470 pic->linesize[i]= buf->linesize[i];
d90cf87b 471 }
0eea2129 472 pic->extended_data = pic->data;
f3a29b75 473 avci->buffer_count++;
d90cf87b 474
393cbb96
MN
475 if(s->pkt) pic->pkt_pts= s->pkt->pts;
476 else pic->pkt_pts= AV_NOPTS_VALUE;
79de84f2
MN
477 pic->reordered_opaque= s->reordered_opaque;
478
385c820b 479 if(s->debug&FF_DEBUG_BUFFERS)
f3a29b75
JR
480 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
481 "buffers used\n", pic, avci->buffer_count);
385c820b 482
1e491e29
MN
483 return 0;
484}
485
0eea2129
JR
486int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
487{
488 switch (avctx->codec_type) {
489 case AVMEDIA_TYPE_VIDEO:
490 return video_get_buffer(avctx, frame);
491 case AVMEDIA_TYPE_AUDIO:
492 return audio_get_buffer(avctx, frame);
493 default:
494 return -1;
495 }
496}
497
492cd3a9 498void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
1e491e29 499 int i;
6829ac8d 500 InternalBuffer *buf, *last;
f3a29b75 501 AVCodecInternal *avci = s->internal;
d90cf87b 502
0eea2129
JR
503 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
504
4e00e76b 505 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
f3a29b75
JR
506 assert(avci->buffer_count);
507
508 if (avci->buffer) {
509 buf = NULL; /* avoids warning */
510 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
511 buf = &avci->buffer[i];
512 if (buf->data[0] == pic->data[0])
513 break;
514 }
515 assert(i < avci->buffer_count);
516 avci->buffer_count--;
517 last = &avci->buffer[avci->buffer_count];
d90cf87b 518
a09bb3ba
MR
519 if (buf != last)
520 FFSWAP(InternalBuffer, *buf, *last);
6a9c8594 521 }
d90cf87b 522
560f773c 523 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
1e491e29 524 pic->data[i]=NULL;
d90cf87b
MN
525// pic->base[i]=NULL;
526 }
1e491e29 527//printf("R%X\n", pic->opaque);
385c820b
AS
528
529 if(s->debug&FF_DEBUG_BUFFERS)
f3a29b75
JR
530 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
531 "buffers used\n", pic, avci->buffer_count);
1e491e29
MN
532}
533
e1c2a5a0
RT
534int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
535 AVFrame temp_pic;
536 int i;
537
0eea2129
JR
538 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
539
e1c2a5a0
RT
540 /* If no picture return a new buffer */
541 if(pic->data[0] == NULL) {
542 /* We will copy from buffer, so must be readable */
543 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
544 return s->get_buffer(s, pic);
545 }
546
547 /* If internal buffer type return the same buffer */
b8af4fe9 548 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
62ecd363
NG
549 if(s->pkt) pic->pkt_pts= s->pkt->pts;
550 else pic->pkt_pts= AV_NOPTS_VALUE;
b8af4fe9 551 pic->reordered_opaque= s->reordered_opaque;
e1c2a5a0 552 return 0;
b8af4fe9 553 }
e1c2a5a0
RT
554
555 /*
556 * Not internal type and reget_buffer not overridden, emulate cr buffer
557 */
558 temp_pic = *pic;
560f773c 559 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
e1c2a5a0
RT
560 pic->data[i] = pic->base[i] = NULL;
561 pic->opaque = NULL;
562 /* Allocate new frame */
563 if (s->get_buffer(s, pic))
564 return -1;
565 /* Copy image data from old buffer to new buffer */
636d6a4a 566 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
e1c2a5a0
RT
567 s->height);
568 s->release_buffer(s, &temp_pic); // Release old frame
569 return 0;
570}
571
3a84713a 572int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
9c3d33d6
MN
573 int i;
574
575 for(i=0; i<count; i++){
3a84713a 576 int r= func(c, (char*)arg + i*size);
9c3d33d6
MN
577 if(ret) ret[i]= r;
578 }
579 return 0;
580}
581
8d23a86f
RD
582int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
583 int i;
584
585 for(i=0; i<count; i++){
586 int r= func(c, arg, i, 0);
587 if(ret) ret[i]= r;
588 }
589 return 0;
590}
591
c269cf68
MN
592enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
593 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
594 ++fmt;
a33c7159
MN
595 return fmt[0];
596}
597
9740beff
MN
598void avcodec_get_frame_defaults(AVFrame *pic){
599 memset(pic, 0, sizeof(AVFrame));
600
601 pic->pts= AV_NOPTS_VALUE;
c342499d 602 pic->key_frame= 1;
b58dbb5b 603 pic->sample_aspect_ratio = (AVRational){0, 1};
8a4a5f6f 604 pic->format = -1; /* unknown */
9740beff
MN
605}
606
492cd3a9 607AVFrame *avcodec_alloc_frame(void){
9740beff 608 AVFrame *pic= av_malloc(sizeof(AVFrame));
115329f1 609
9740beff 610 if(pic==NULL) return NULL;
115329f1 611
9740beff 612 avcodec_get_frame_defaults(pic);
115329f1 613
1e491e29
MN
614 return pic;
615}
616
0b950fe2 617#if FF_API_AVCODEC_OPEN
5e4c7ca2 618int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
de6d9b64 619{
0b950fe2
AK
620 return avcodec_open2(avctx, codec, NULL);
621}
622#endif
623
624int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
625{
31d76ec2 626 int ret = 0;
0b950fe2
AK
627 AVDictionary *tmp = NULL;
628
4df30f71
AC
629 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
630 return AVERROR(EINVAL);
631
0b950fe2
AK
632 if (options)
633 av_dict_copy(&tmp, *options, 0);
115329f1 634
f988ce6c
635 /* If there is a user-supplied mutex locking routine, call it. */
636 if (ff_lockmgr_cb) {
637 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
638 return -1;
639 }
640
ddebfb15
MN
641 entangled_thread_counter++;
642 if(entangled_thread_counter != 1){
643 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
31d76ec2 644 ret = -1;
ddebfb15
MN
645 goto end;
646 }
de6d9b64 647
31d76ec2
JR
648 if(avctx->codec || !codec) {
649 ret = AVERROR(EINVAL);
ddebfb15 650 goto end;
31d76ec2 651 }
09770af8 652
f3a29b75
JR
653 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
654 if (!avctx->internal) {
655 ret = AVERROR(ENOMEM);
656 goto end;
657 }
658
0edf8a7a 659 if (codec->priv_data_size > 0) {
dc51a72b 660 if(!avctx->priv_data){
0edf8a7a 661 avctx->priv_data = av_mallocz(codec->priv_data_size);
90f06cea
PI
662 if (!avctx->priv_data) {
663 ret = AVERROR(ENOMEM);
ddebfb15 664 goto end;
90f06cea 665 }
0b950fe2 666 if (codec->priv_class) {
dc51a72b
MN
667 *(AVClass**)avctx->priv_data= codec->priv_class;
668 av_opt_set_defaults(avctx->priv_data);
669 }
670 }
1d36fb13 671 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
0b950fe2 672 goto free_and_end;
0edf8a7a
PG
673 } else {
674 avctx->priv_data = NULL;
675 }
0b950fe2
AK
676 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
677 goto free_and_end;
21adafec
MN
678
679 if(avctx->coded_width && avctx->coded_height)
680 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
681 else if(avctx->width && avctx->height)
682 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
683
82eac2f3
RD
684 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
685 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
686 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
687 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
688 avcodec_set_dimensions(avctx, 0, 0);
689 }
690
f19c58b4
AJ
691 /* if the decoder init function was already called previously,
692 free the already allocated subtitle_header before overwriting it */
693 if (codec->decode)
694 av_freep(&avctx->subtitle_header);
695
fa77dd63 696#define SANE_NB_CHANNELS 128U
82eac2f3 697 if (avctx->channels > SANE_NB_CHANNELS) {
7868349a 698 ret = AVERROR(EINVAL);
2a9b5c9b 699 goto free_and_end;
0ecca7a4
MN
700 }
701
b5c85991 702 avctx->codec = codec;
72415b2a 703 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
681c180d
RD
704 avctx->codec_id == CODEC_ID_NONE) {
705 avctx->codec_type = codec->type;
706 avctx->codec_id = codec->id;
707 }
e83c716e
AJ
708 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
709 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
4c0dda2b 710 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
31d76ec2 711 ret = AVERROR(EINVAL);
2a9b5c9b 712 goto free_and_end;
4c0dda2b 713 }
b5c85991 714 avctx->frame_number = 0;
5ea0001f
DB
715#if FF_API_ER
716
717 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
718 avctx->error_recognition, avctx->err_recognition);
719 /* FF_ER_CAREFUL (==1) implies AV_EF_CRCCHECK (== 1<<1 - 1),
720 FF_ER_COMPLIANT (==2) implies AV_EF_{CRCCHECK,BITSTREAM} (== 1<<2 - 1), et cetera} */
721 avctx->err_recognition |= (1<<(avctx->error_recognition-(avctx->error_recognition>=FF_ER_VERY_AGGRESSIVE))) - 1;
722 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
723 avctx->error_recognition, avctx->err_recognition);
724#endif
37b00b47
AS
725
726 if (HAVE_THREADS && !avctx->thread_opaque) {
ba9ef8d0 727 ret = ff_thread_init(avctx);
37b00b47
AS
728 if (ret < 0) {
729 goto free_and_end;
730 }
731 }
b6064d9a
JG
732 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
733 avctx->thread_count = 1;
37b00b47 734
527c91e3
CEH
735 if (avctx->codec->max_lowres < avctx->lowres) {
736 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
737 avctx->codec->max_lowres);
31d76ec2 738 ret = AVERROR(EINVAL);
527c91e3
CEH
739 goto free_and_end;
740 }
8b00ab01 741 if (avctx->codec->encode) {
2cfa2d92 742 int i;
8b00ab01 743 if (avctx->codec->sample_fmts) {
3dfc3e70
JR
744 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
745 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
746 break;
747 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
748 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
749 ret = AVERROR(EINVAL);
750 goto free_and_end;
751 }
8b00ab01
JR
752 }
753 if (avctx->codec->supported_samplerates) {
754 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
755 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
756 break;
757 if (avctx->codec->supported_samplerates[i] == 0) {
758 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
759 ret = AVERROR(EINVAL);
760 goto free_and_end;
761 }
762 }
763 if (avctx->codec->channel_layouts) {
764 if (!avctx->channel_layout) {
765 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
766 } else {
767 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
768 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
769 break;
770 if (avctx->codec->channel_layouts[i] == 0) {
771 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
772 ret = AVERROR(EINVAL);
773 goto free_and_end;
774 }
775 }
776 }
168f9e8c
JR
777 if (avctx->channel_layout && avctx->channels) {
778 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
779 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
780 ret = AVERROR(EINVAL);
781 goto free_and_end;
782 }
688b09fa
JR
783 } else if (avctx->channel_layout) {
784 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
168f9e8c 785 }
2cfa2d92 786 }
0fd0ef79 787
37b00b47 788 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
2de4f9eb
MN
789 ret = avctx->codec->init(avctx);
790 if (ret < 0) {
2a9b5c9b 791 goto free_and_end;
2de4f9eb 792 }
6e546aaa 793 }
ddebfb15
MN
794end:
795 entangled_thread_counter--;
f988ce6c
796
797 /* Release any user-supplied mutex. */
798 if (ff_lockmgr_cb) {
799 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
800 }
0b950fe2
AK
801 if (options) {
802 av_dict_free(options);
803 *options = tmp;
804 }
805
ddebfb15 806 return ret;
2a9b5c9b 807free_and_end:
0b950fe2 808 av_dict_free(&tmp);
2a9b5c9b 809 av_freep(&avctx->priv_data);
f3a29b75 810 av_freep(&avctx->internal);
2a9b5c9b
MN
811 avctx->codec= NULL;
812 goto end;
de6d9b64
FB
813}
814
5e4c7ca2 815int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
de6d9b64
FB
816 const short *samples)
817{
0ecca7a4 818 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
5286d11f 819 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
0ecca7a4
MN
820 return -1;
821 }
6f824977 822 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
8eb027c8 823 int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
6f824977
MN
824 avctx->frame_number++;
825 return ret;
826 }else
827 return 0;
de6d9b64
FB
828}
829
5e4c7ca2 830int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
492cd3a9 831 const AVFrame *pict)
de6d9b64 832{
0ecca7a4 833 if(buf_size < FF_MIN_BUFFER_SIZE){
5286d11f 834 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
0ecca7a4
MN
835 return -1;
836 }
e16f217c 837 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
0ecca7a4 838 return -1;
6f824977 839 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
8eb027c8 840 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
6f824977 841 avctx->frame_number++;
bb628dae 842 emms_c(); //needed to avoid an emms_c() call before every return;
115329f1 843
6f824977
MN
844 return ret;
845 }else
846 return 0;
de6d9b64
FB
847}
848
115329f1 849int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
240c1657
FB
850 const AVSubtitle *sub)
851{
852 int ret;
9413db9e
BA
853 if(sub->start_display_time) {
854 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
855 return -1;
856 }
505aa6c9
BA
857 if(sub->num_rects == 0 || !sub->rects)
858 return -1;
8eb027c8 859 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
240c1657
FB
860 avctx->frame_number++;
861 return ret;
862}
863
867f923d
MS
864static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
865{
866 int size = 0;
867 const uint8_t *data;
868 uint32_t flags;
869
870 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
871 return;
872
873 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
874 if (!data || size < 4)
875 return;
876 flags = bytestream_get_le32(&data);
877 size -= 4;
878 if (size < 4) /* Required for any of the changes */
879 return;
880 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
881 avctx->channels = bytestream_get_le32(&data);
882 size -= 4;
883 }
884 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
885 if (size < 8)
886 return;
887 avctx->channel_layout = bytestream_get_le64(&data);
888 size -= 8;
889 }
890 if (size < 4)
891 return;
892 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
893 avctx->sample_rate = bytestream_get_le32(&data);
894 size -= 4;
895 }
896 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
897 if (size < 8)
898 return;
899 avctx->width = bytestream_get_le32(&data);
900 avctx->height = bytestream_get_le32(&data);
c5d907b6 901 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
867f923d
MS
902 size -= 8;
903 }
904}
905
7a00bbad
TB
906int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
907 int *got_picture_ptr,
908 AVPacket *avpkt)
909{
de6d9b64 910 int ret;
115329f1 911
53db1cae 912 *got_picture_ptr= 0;
e16f217c 913 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
0ecca7a4 914 return -1;
393cbb96
MN
915
916 avctx->pkt = avpkt;
c5d907b6 917 apply_param_change(avctx, avpkt);
393cbb96 918
37b00b47 919 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
27237d52 920 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
37b00b47
AS
921 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
922 avpkt);
923 else {
924 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
925 avpkt);
926 picture->pkt_dts= avpkt->dts;
b58dbb5b 927 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
3a2ddf7c
SS
928 picture->width = avctx->width;
929 picture->height = avctx->height;
8a4a5f6f 930 picture->format = avctx->pix_fmt;
37b00b47 931 }
6bb925f4 932
bb628dae 933 emms_c(); //needed to avoid an emms_c() call before every return;
115329f1
DB
934
935 if (*got_picture_ptr)
934982c4
MN
936 avctx->frame_number++;
937 }else
938 ret= 0;
939
de6d9b64
FB
940 return ret;
941}
942
0eea2129 943#if FF_API_OLD_DECODE_AUDIO
7a00bbad
TB
944int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
945 int *frame_size_ptr,
946 AVPacket *avpkt)
947{
0eea2129
JR
948 AVFrame frame;
949 int ret, got_frame = 0;
950
951 if (avctx->get_buffer != avcodec_default_get_buffer) {
e2ff436e
RT
952 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
953 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
954 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
955 "avcodec_decode_audio4()\n");
956 avctx->get_buffer = avcodec_default_get_buffer;
0eea2129
JR
957 }
958
959 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
960
961 if (ret >= 0 && got_frame) {
962 int ch, plane_size;
963 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
964 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
965 frame.nb_samples,
966 avctx->sample_fmt, 1);
967 if (*frame_size_ptr < data_size) {
968 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
969 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
970 return AVERROR(EINVAL);
971 }
972
973 memcpy(samples, frame.extended_data[0], plane_size);
974
975 if (planar && avctx->channels > 1) {
976 uint8_t *out = ((uint8_t *)samples) + plane_size;
977 for (ch = 1; ch < avctx->channels; ch++) {
978 memcpy(out, frame.extended_data[ch], plane_size);
979 out += plane_size;
980 }
981 }
982 *frame_size_ptr = data_size;
983 } else {
984 *frame_size_ptr = 0;
985 }
986 return ret;
987}
988#endif
989
990int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
991 AVFrame *frame,
992 int *got_frame_ptr,
993 AVPacket *avpkt)
994{
995 int ret = 0;
996
997 *got_frame_ptr = 0;
de6d9b64 998
393cbb96
MN
999 avctx->pkt = avpkt;
1000
6326afd5
JR
1001 if (!avpkt->data && avpkt->size) {
1002 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1003 return AVERROR(EINVAL);
1004 }
1005
f13db94d
MS
1006 apply_param_change(avctx, avpkt);
1007
0eea2129
JR
1008 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1009 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1010 if (ret >= 0 && *got_frame_ptr) {
1011 avctx->frame_number++;
1012 frame->pkt_dts = avpkt->dts;
8a4a5f6f
SS
1013 if (frame->format == AV_SAMPLE_FMT_NONE)
1014 frame->format = avctx->sample_fmt;
9c856d62 1015 }
ac66834c 1016 }
de6d9b64
FB
1017 return ret;
1018}
1019
7a00bbad
TB
1020int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1021 int *got_sub_ptr,
1022 AVPacket *avpkt)
1023{
240c1657
FB
1024 int ret;
1025
393cbb96 1026 avctx->pkt = avpkt;
240c1657 1027 *got_sub_ptr = 0;
7a00bbad 1028 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
240c1657
FB
1029 if (*got_sub_ptr)
1030 avctx->frame_number++;
1031 return ret;
1032}
1033
e1d7c883
RD
1034void avsubtitle_free(AVSubtitle *sub)
1035{
1036 int i;
1037
1038 for (i = 0; i < sub->num_rects; i++)
1039 {
8b834ac5
RD
1040 av_freep(&sub->rects[i]->pict.data[0]);
1041 av_freep(&sub->rects[i]->pict.data[1]);
1042 av_freep(&sub->rects[i]->pict.data[2]);
1043 av_freep(&sub->rects[i]->pict.data[3]);
1044 av_freep(&sub->rects[i]->text);
1045 av_freep(&sub->rects[i]->ass);
1046 av_freep(&sub->rects[i]);
e1d7c883
RD
1047 }
1048
8b834ac5 1049 av_freep(&sub->rects);
e1d7c883
RD
1050
1051 memset(sub, 0, sizeof(AVSubtitle));
1052}
1053
0752cd39 1054av_cold int avcodec_close(AVCodecContext *avctx)
de6d9b64 1055{
f988ce6c
1056 /* If there is a user-supplied mutex locking routine, call it. */
1057 if (ff_lockmgr_cb) {
1058 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1059 return -1;
1060 }
1061
ddebfb15
MN
1062 entangled_thread_counter++;
1063 if(entangled_thread_counter != 1){
1064 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1065 entangled_thread_counter--;
1066 return -1;
1067 }
1068
49fb20cb 1069 if (HAVE_THREADS && avctx->thread_opaque)
c0b102ca 1070 ff_thread_free(avctx);
dccda293 1071 if (avctx->codec && avctx->codec->close)
de6d9b64 1072 avctx->codec->close(avctx);
eea8c08f 1073 avcodec_default_free_buffers(avctx);
b3ab3199 1074 avctx->coded_frame = NULL;
f3a29b75 1075 av_freep(&avctx->internal);
ceff045d 1076 if (avctx->codec && avctx->codec->priv_class)
36773283
AK
1077 av_opt_free(avctx->priv_data);
1078 av_opt_free(avctx);
3123dd79 1079 av_freep(&avctx->priv_data);
77a670e7 1080 if(avctx->codec && avctx->codec->encode)
c4f267ab 1081 av_freep(&avctx->extradata);
de6d9b64 1082 avctx->codec = NULL;
37b00b47 1083 avctx->active_thread_type = 0;
ddebfb15 1084 entangled_thread_counter--;
f988ce6c
1085
1086 /* Release any user-supplied mutex. */
1087 if (ff_lockmgr_cb) {
1088 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1089 }
de6d9b64
FB
1090 return 0;
1091}
1092
1093AVCodec *avcodec_find_encoder(enum CodecID id)
1094{
93ebfeea 1095 AVCodec *p, *experimental=NULL;
de6d9b64
FB
1096 p = first_avcodec;
1097 while (p) {
93ebfeea
JG
1098 if (p->encode != NULL && p->id == id) {
1099 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1100 experimental = p;
1101 } else
1102 return p;
1103 }
de6d9b64
FB
1104 p = p->next;
1105 }
93ebfeea 1106 return experimental;
de6d9b64
FB
1107}
1108
98f3b098
A
1109AVCodec *avcodec_find_encoder_by_name(const char *name)
1110{
1111 AVCodec *p;
fc228c90
AJ
1112 if (!name)
1113 return NULL;
98f3b098
A
1114 p = first_avcodec;
1115 while (p) {
1116 if (p->encode != NULL && strcmp(name,p->name) == 0)
1117 return p;
1118 p = p->next;
1119 }
1120 return NULL;
1121}
1122
de6d9b64
FB
1123AVCodec *avcodec_find_decoder(enum CodecID id)
1124{
1125 AVCodec *p;
1126 p = first_avcodec;
1127 while (p) {
1128 if (p->decode != NULL && p->id == id)
1129 return p;
1130 p = p->next;
1131 }
1132 return NULL;
1133}
1134
1135AVCodec *avcodec_find_decoder_by_name(const char *name)
1136{
1137 AVCodec *p;
fc228c90
AJ
1138 if (!name)
1139 return NULL;
de6d9b64
FB
1140 p = first_avcodec;
1141 while (p) {
1142 if (p->decode != NULL && strcmp(name,p->name) == 0)
1143 return p;
1144 p = p->next;
1145 }
1146 return NULL;
1147}
1148
406aa93f 1149static int get_bit_rate(AVCodecContext *ctx)
ce34ff6b
RK
1150{
1151 int bit_rate;
1152 int bits_per_sample;
1153
1154 switch(ctx->codec_type) {
72415b2a 1155 case AVMEDIA_TYPE_VIDEO:
4563cf24
SS
1156 case AVMEDIA_TYPE_DATA:
1157 case AVMEDIA_TYPE_SUBTITLE:
1158 case AVMEDIA_TYPE_ATTACHMENT:
ce34ff6b
RK
1159 bit_rate = ctx->bit_rate;
1160 break;
72415b2a 1161 case AVMEDIA_TYPE_AUDIO:
ce34ff6b
RK
1162 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1163 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1164 break;
ce34ff6b
RK
1165 default:
1166 bit_rate = 0;
1167 break;
1168 }
1169 return bit_rate;
1170}
1171
7e566bbe
SS
1172size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1173{
1174 int i, len, ret = 0;
1175
1176 for (i = 0; i < 4; i++) {
1177 len = snprintf(buf, buf_size,
1178 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1179 buf += len;
1180 buf_size = buf_size > len ? buf_size - len : 0;
1181 ret += len;
1182 codec_tag>>=8;
1183 }
1184 return ret;
1185}
1186
de6d9b64
FB
1187void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1188{
1189 const char *codec_name;
2a81f4bd 1190 const char *profile = NULL;
de6d9b64
FB
1191 AVCodec *p;
1192 char buf1[32];
a96b68b7 1193 int bitrate;
59771f71 1194 AVRational display_aspect_ratio;
de6d9b64
FB
1195
1196 if (encode)
1197 p = avcodec_find_encoder(enc->codec_id);
1198 else
1199 p = avcodec_find_decoder(enc->codec_id);
1200
1201 if (p) {
1202 codec_name = p->name;
2a81f4bd 1203 profile = av_get_profile_name(p, enc->profile);
985180a1
FB
1204 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
1205 /* fake mpeg2 transport stream codec (currently not
1206 registered) */
1207 codec_name = "mpeg2ts";
de6d9b64
FB
1208 } else if (enc->codec_name[0] != '\0') {
1209 codec_name = enc->codec_name;
1210 } else {
1211 /* output avi tags */
ab0b5378
SS
1212 char tag_buf[32];
1213 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1214 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
de6d9b64
FB
1215 codec_name = buf1;
1216 }
1217
1218 switch(enc->codec_type) {
72415b2a 1219 case AVMEDIA_TYPE_VIDEO:
de6d9b64
FB
1220 snprintf(buf, buf_size,
1221 "Video: %s%s",
7d1c3fc1 1222 codec_name, enc->mb_decision ? " (hq)" : "");
2a81f4bd
AH
1223 if (profile)
1224 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1225 " (%s)", profile);
82c0c4ba 1226 if (enc->pix_fmt != PIX_FMT_NONE) {
cf087595
FB
1227 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1228 ", %s",
94bed8e5 1229 av_get_pix_fmt_name(enc->pix_fmt));
cf087595 1230 }
de6d9b64
FB
1231 if (enc->width) {
1232 snprintf(buf + strlen(buf), buf_size - strlen(buf),
21189011
MN
1233 ", %dx%d",
1234 enc->width, enc->height);
7ee4dd02 1235 if (enc->sample_aspect_ratio.num) {
cbaf50f8
BC
1236 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1237 enc->width*enc->sample_aspect_ratio.num,
1238 enc->height*enc->sample_aspect_ratio.den,
1239 1024*1024);
1240 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1241 " [PAR %d:%d DAR %d:%d]",
1242 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1243 display_aspect_ratio.num, display_aspect_ratio.den);
7ee4dd02 1244 }
a309073b 1245 if(av_log_get_level() >= AV_LOG_DEBUG){
9ce6c138 1246 int g= av_gcd(enc->time_base.num, enc->time_base.den);
21189011
MN
1247 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1248 ", %d/%d",
1249 enc->time_base.num/g, enc->time_base.den/g);
1250 }
de6d9b64 1251 }
4bfad535
FB
1252 if (encode) {
1253 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1254 ", q=%d-%d", enc->qmin, enc->qmax);
1255 }
de6d9b64 1256 break;
72415b2a 1257 case AVMEDIA_TYPE_AUDIO:
de6d9b64
FB
1258 snprintf(buf, buf_size,
1259 "Audio: %s",
1260 codec_name);
2a81f4bd
AH
1261 if (profile)
1262 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1263 " (%s)", profile);
de6d9b64
FB
1264 if (enc->sample_rate) {
1265 snprintf(buf + strlen(buf), buf_size - strlen(buf),
0d72e7d0 1266 ", %d Hz", enc->sample_rate);
de6d9b64 1267 }
0d72e7d0 1268 av_strlcat(buf, ", ", buf_size);
63e8d976 1269 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
5d6e4c16 1270 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
9e82a113 1271 snprintf(buf + strlen(buf), buf_size - strlen(buf),
ba7d6e79 1272 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
9e82a113 1273 }
de6d9b64 1274 break;
72415b2a 1275 case AVMEDIA_TYPE_DATA:
985180a1 1276 snprintf(buf, buf_size, "Data: %s", codec_name);
240c1657 1277 break;
72415b2a 1278 case AVMEDIA_TYPE_SUBTITLE:
240c1657 1279 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
985180a1 1280 break;
72415b2a 1281 case AVMEDIA_TYPE_ATTACHMENT:
f8d7c9d3 1282 snprintf(buf, buf_size, "Attachment: %s", codec_name);
f8d7c9d3 1283 break;
de6d9b64 1284 default:
9fe5a7b8
MN
1285 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1286 return;
de6d9b64 1287 }
4bfad535
FB
1288 if (encode) {
1289 if (enc->flags & CODEC_FLAG_PASS1)
1290 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1291 ", pass 1");
1292 if (enc->flags & CODEC_FLAG_PASS2)
1293 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1294 ", pass 2");
1295 }
406aa93f 1296 bitrate = get_bit_rate(enc);
a96b68b7 1297 if (bitrate != 0) {
115329f1 1298 snprintf(buf + strlen(buf), buf_size - strlen(buf),
a96b68b7 1299 ", %d kb/s", bitrate / 1000);
de6d9b64
FB
1300 }
1301}
1302
060ec0a8
AH
1303const char *av_get_profile_name(const AVCodec *codec, int profile)
1304{
1305 const AVProfile *p;
1306 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1307 return NULL;
1308
1309 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1310 if (p->profile == profile)
1311 return p->name;
1312
1313 return NULL;
1314}
1315
156e5023
NK
1316unsigned avcodec_version( void )
1317{
1318 return LIBAVCODEC_VERSION_INT;
1319}
cf087595 1320
41600690 1321const char *avcodec_configuration(void)
c1736936 1322{
29ba0911 1323 return LIBAV_CONFIGURATION;
c1736936
DB
1324}
1325
41600690 1326const char *avcodec_license(void)
c1736936
DB
1327{
1328#define LICENSE_PREFIX "libavcodec license: "
a03be6e1 1329 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
c1736936
DB
1330}
1331
1c2a8c7f
MN
1332void avcodec_flush_buffers(AVCodecContext *avctx)
1333{
27237d52 1334 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
37b00b47 1335 ff_thread_flush(avctx);
d1cf4591 1336 else if(avctx->codec->flush)
7a06ff14 1337 avctx->codec->flush(avctx);
1c2a8c7f
MN
1338}
1339
0eea2129
JR
1340static void video_free_buffers(AVCodecContext *s)
1341{
f3a29b75 1342 AVCodecInternal *avci = s->internal;
d90cf87b
MN
1343 int i, j;
1344
f3a29b75
JR
1345 if (!avci->buffer)
1346 return;
115329f1 1347
f3a29b75
JR
1348 if (avci->buffer_count)
1349 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1350 avci->buffer_count);
d90cf87b 1351 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
f3a29b75 1352 InternalBuffer *buf = &avci->buffer[i];
d90cf87b
MN
1353 for(j=0; j<4; j++){
1354 av_freep(&buf->base[j]);
1355 buf->data[j]= NULL;
1356 }
1357 }
f3a29b75 1358 av_freep(&avci->buffer);
115329f1 1359
f3a29b75 1360 avci->buffer_count=0;
d90cf87b
MN
1361}
1362
0eea2129
JR
1363static void audio_free_buffers(AVCodecContext *avctx)
1364{
1365 AVCodecInternal *avci = avctx->internal;
1366 InternalBuffer *buf;
1367
1368 if (!avci->buffer)
1369 return;
1370 buf = avci->buffer;
1371
1372 if (buf->extended_data) {
1373 av_free(buf->extended_data[0]);
1374 if (buf->extended_data != buf->data)
1375 av_free(buf->extended_data);
1376 }
1377 av_freep(&avci->buffer);
1378}
1379
1380void avcodec_default_free_buffers(AVCodecContext *avctx)
1381{
1382 switch (avctx->codec_type) {
1383 case AVMEDIA_TYPE_VIDEO:
1384 video_free_buffers(avctx);
1385 break;
1386 case AVMEDIA_TYPE_AUDIO:
1387 audio_free_buffers(avctx);
1388 break;
1389 default:
1390 break;
1391 }
1392}
1393
bebe72f4 1394#if FF_API_OLD_FF_PICT_TYPES
d8085ea7 1395char av_get_pict_type_char(int pict_type){
bebe72f4 1396 return av_get_picture_type_char(pict_type);
d8085ea7 1397}
bebe72f4 1398#endif
d8085ea7 1399
ac3e1834
BC
1400int av_get_bits_per_sample(enum CodecID codec_id){
1401 switch(codec_id){
5da71469 1402 case CODEC_ID_ADPCM_SBPRO_2:
f1b163e0 1403 return 2;
5da71469 1404 case CODEC_ID_ADPCM_SBPRO_3:
f1b163e0 1405 return 3;
5da71469 1406 case CODEC_ID_ADPCM_SBPRO_4:
f1b163e0 1407 case CODEC_ID_ADPCM_CT:
07f2a575 1408 case CODEC_ID_ADPCM_IMA_WAV:
9ff6d079
JR
1409 case CODEC_ID_ADPCM_IMA_QT:
1410 case CODEC_ID_ADPCM_SWF:
9df9b810
DV
1411 case CODEC_ID_ADPCM_MS:
1412 case CODEC_ID_ADPCM_YAMAHA:
029f966c 1413 case CODEC_ID_ADPCM_G722:
f32fd318 1414 return 4;
ac3e1834
BC
1415 case CODEC_ID_PCM_ALAW:
1416 case CODEC_ID_PCM_MULAW:
1417 case CODEC_ID_PCM_S8:
1418 case CODEC_ID_PCM_U8:
9d49b8ff 1419 case CODEC_ID_PCM_ZORK:
ac3e1834
BC
1420 return 8;
1421 case CODEC_ID_PCM_S16BE:
1422 case CODEC_ID_PCM_S16LE:
725d86bf 1423 case CODEC_ID_PCM_S16LE_PLANAR:
ac3e1834
BC
1424 case CODEC_ID_PCM_U16BE:
1425 case CODEC_ID_PCM_U16LE:
1426 return 16;
1427 case CODEC_ID_PCM_S24DAUD:
1428 case CODEC_ID_PCM_S24BE:
1429 case CODEC_ID_PCM_S24LE:
1430 case CODEC_ID_PCM_U24BE:
1431 case CODEC_ID_PCM_U24LE:
1432 return 24;
1433 case CODEC_ID_PCM_S32BE:
1434 case CODEC_ID_PCM_S32LE:
1435 case CODEC_ID_PCM_U32BE:
1436 case CODEC_ID_PCM_U32LE:
aa29709e 1437 case CODEC_ID_PCM_F32BE:
143a5d6f 1438 case CODEC_ID_PCM_F32LE:
ac3e1834 1439 return 32;
143a5d6f
PR
1440 case CODEC_ID_PCM_F64BE:
1441 case CODEC_ID_PCM_F64LE:
1442 return 64;
ac3e1834
BC
1443 default:
1444 return 0;
1445 }
1446}
1447
6f84cd12 1448#if FF_API_OLD_SAMPLE_FMT
5d6e4c16 1449int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
e6c52cee 1450 return av_get_bytes_per_sample(sample_fmt) << 3;
42c71907 1451}
6f84cd12 1452#endif
42c71907 1453
b250f9c6 1454#if !HAVE_THREADS
ba9ef8d0 1455int ff_thread_init(AVCodecContext *s){
ca8ad847
MN
1456 return -1;
1457}
1458#endif
ad2b531d
MR
1459
1460unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1461{
1462 unsigned int n = 0;
1463
1464 while(v >= 0xff) {
1465 *s++ = 0xff;
1466 v -= 0xff;
1467 n++;
1468 }
1469 *s = v;
1470 n++;
1471 return n;
1472}
1005f542 1473
c46eeae2
MN
1474int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1475 int i;
1476 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1477 return i;
1478}
1479
ce863d7f 1480void av_log_missing_feature(void *avc, const char *feature, int want_sample)
ea779d91 1481{
6001dad6 1482 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
db323491 1483 "version to the newest one from Git. If the problem still "
ea779d91 1484 "occurs, it means that your file has a feature which has not "
3fd3632f 1485 "been implemented.\n", feature);
ea779d91 1486 if(want_sample)
ce863d7f 1487 av_log_ask_for_sample(avc, NULL);
0ba39dd1
KG
1488}
1489
44f566b7 1490void av_log_ask_for_sample(void *avc, const char *msg, ...)
0ba39dd1 1491{
44f566b7
DB
1492 va_list argument_list;
1493
1494 va_start(argument_list, msg);
1495
0ba39dd1 1496 if (msg)
44f566b7 1497 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
0ba39dd1 1498 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
f0a41afd 1499 "of this file to ftp://upload.libav.org/incoming/ "
21de9204 1500 "and contact the libav-devel mailing list.\n");
44f566b7
DB
1501
1502 va_end(argument_list);
ea779d91 1503}
c895618b
MN
1504
1505static AVHWAccel *first_hwaccel = NULL;
1506
1507void av_register_hwaccel(AVHWAccel *hwaccel)
1508{
1509 AVHWAccel **p = &first_hwaccel;
1510 while (*p)
1511 p = &(*p)->next;
1512 *p = hwaccel;
1513 hwaccel->next = NULL;
1514}
414d9d7f
MN
1515
1516AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1517{
1518 return hwaccel ? hwaccel->next : first_hwaccel;
1519}
6059f13c
MN
1520
1521AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1522{
1523 AVHWAccel *hwaccel=NULL;
1524
1525 while((hwaccel= av_hwaccel_next(hwaccel))){
1526 if ( hwaccel->id == codec_id
1527 && hwaccel->pix_fmt == pix_fmt)
1528 return hwaccel;
1529 }
1530 return NULL;
1531}
f988ce6c
1532
1533int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1534{
1535 if (ff_lockmgr_cb) {
1536 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1537 return -1;
2d1b6fb7
MS
1538 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1539 return -1;
f988ce6c
1540 }
1541
1542 ff_lockmgr_cb = cb;
1543
1544 if (ff_lockmgr_cb) {
1545 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1546 return -1;
2d1b6fb7
MS
1547 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1548 return -1;
1549 }
1550 return 0;
1551}
1552
1553int avpriv_lock_avformat(void)
1554{
1555 if (ff_lockmgr_cb) {
1556 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1557 return -1;
1558 }
1559 return 0;
1560}
1561
1562int avpriv_unlock_avformat(void)
1563{
1564 if (ff_lockmgr_cb) {
1565 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1566 return -1;
f988ce6c
1567 }
1568 return 0;
1569}
603a5f04 1570
0842d589 1571unsigned int avpriv_toupper4(unsigned int x)
603a5f04
FL
1572{
1573 return toupper( x &0xFF)
1574 + (toupper((x>>8 )&0xFF)<<8 )
1575 + (toupper((x>>16)&0xFF)<<16)
1576 + (toupper((x>>24)&0xFF)<<24);
1577}
37b00b47 1578
27237d52 1579#if !HAVE_THREADS
37b00b47
AS
1580
1581int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1582{
1583 f->owner = avctx;
1584 return avctx->get_buffer(avctx, f);
1585}
1586
1587void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1588{
1589 f->owner->release_buffer(f->owner, f);
1590}
1591
1592void ff_thread_finish_setup(AVCodecContext *avctx)
1593{
1594}
1595
1596void ff_thread_report_progress(AVFrame *f, int progress, int field)
1597{
1598}
1599
1600void ff_thread_await_progress(AVFrame *f, int progress, int field)
1601{
1602}
1603
1604#endif
65af48b5
AK
1605
1606#if FF_API_THREAD_INIT
1607int avcodec_thread_init(AVCodecContext *s, int thread_count)
1608{
1609 s->thread_count = thread_count;
1610 return ff_thread_init(s);
1611}
1612#endif
bca06e77
AK
1613
1614enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1615{
1616 if (codec_id <= CODEC_ID_NONE)
1617 return AVMEDIA_TYPE_UNKNOWN;
1618 else if (codec_id < CODEC_ID_FIRST_AUDIO)
1619 return AVMEDIA_TYPE_VIDEO;
1620 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1621 return AVMEDIA_TYPE_AUDIO;
1622 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1623 return AVMEDIA_TYPE_SUBTITLE;
1624
1625 return AVMEDIA_TYPE_UNKNOWN;
1626}