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