Make avcodec_check_dimensions() return AVERROR(EINVAL) rather than -1
[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 *
b78e7197
DB
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg 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 *
b78e7197 13 * FFmpeg 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
b78e7197 19 * License along with FFmpeg; 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
7246177d 28/* needed for mkstemp() */
d0feff2a 29#define _XOPEN_SOURCE 600
7246177d 30
7fb94406 31#include "libavutil/avstring.h"
245976da
DB
32#include "libavutil/integer.h"
33#include "libavutil/crc.h"
eb285cfe 34#include "libavutil/pixdesc.h"
de6d9b64 35#include "avcodec.h"
3123dd79 36#include "dsputil.h"
6fd46414 37#include "opt.h"
db7ae7d1 38#include "imgconvert.h"
9e82a113 39#include "audioconvert.h"
88f83607 40#include "libxvid_internal.h"
0ba39dd1 41#include "internal.h"
7246177d 42#include <stdlib.h>
9b879566 43#include <stdarg.h>
4c263142 44#include <limits.h>
860a40c8 45#include <float.h>
b250f9c6 46#if !HAVE_MKSTEMP
1005f542
CH
47#include <fcntl.h>
48#endif
de6d9b64 49
ddebfb15 50static int volatile entangled_thread_counter=0;
f988ce6c
51int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
52static void *codec_mutex;
ddebfb15 53
5c91a675 54void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
8e1e6f31 55{
115329f1 56 if(min_size < *size)
8e1e6f31 57 return ptr;
115329f1 58
0ecca7a4 59 *size= FFMAX(17*min_size/16 + 32, min_size);
8e1e6f31 60
978805b2
MN
61 ptr= av_realloc(ptr, *size);
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
63 *size= 0;
64
65 return ptr;
8e1e6f31
FB
66}
67
238ef6da
RD
68void av_fast_malloc(void *ptr, unsigned int *size, unsigned int min_size)
69{
70 void **p = ptr;
71 if (min_size < *size)
72 return;
73 *size= FFMAX(17*min_size/16 + 32, min_size);
74 av_free(*p);
75 *p = av_malloc(*size);
76 if (!*p) *size = 0;
77}
78
de6d9b64 79/* encoder management */
e6df765e 80static AVCodec *first_avcodec = NULL;
de6d9b64 81
55b9e69a
MN
82AVCodec *av_codec_next(AVCodec *c){
83 if(c) return c->next;
84 else return first_avcodec;
85}
86
85662f49 87void avcodec_register(AVCodec *codec)
de6d9b64
FB
88{
89 AVCodec **p;
7a961a46 90 avcodec_init();
de6d9b64
FB
91 p = &first_avcodec;
92 while (*p != NULL) p = &(*p)->next;
335a761a
SS
93 *p = codec;
94 codec->next = NULL;
de6d9b64
FB
95}
96
9d385cfe 97#if LIBAVCODEC_VERSION_MAJOR < 53
85662f49
SS
98void register_avcodec(AVCodec *codec)
99{
100 avcodec_register(codec);
101}
9d385cfe 102#endif
85662f49 103
0fb49b59
BB
104unsigned avcodec_get_edge_width(void)
105{
106 return EDGE_WIDTH;
107}
108
21adafec
MN
109void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
110 s->coded_width = width;
111 s->coded_height= height;
112 s->width = -((-width )>>s->lowres);
113 s->height= -((-height)>>s->lowres);
114}
115
d90cf87b 116typedef struct InternalBuffer{
1e491e29 117 int last_pic_num;
d90cf87b 118 uint8_t *base[4];
1e491e29 119 uint8_t *data[4];
237e4938 120 int linesize[4];
0701006e
MN
121 int width, height;
122 enum PixelFormat pix_fmt;
d90cf87b
MN
123}InternalBuffer;
124
125#define INTERNAL_BUFFER_SIZE 32
1e491e29 126
eb285cfe 127void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
115329f1
DB
128 int w_align= 1;
129 int h_align= 1;
130
f0bbfc4a
MN
131 switch(s->pix_fmt){
132 case PIX_FMT_YUV420P:
71e445fc 133 case PIX_FMT_YUYV422:
ebb177dd 134 case PIX_FMT_UYVY422:
f0bbfc4a 135 case PIX_FMT_YUV422P:
98c82d69 136 case PIX_FMT_YUV440P:
f0bbfc4a
MN
137 case PIX_FMT_YUV444P:
138 case PIX_FMT_GRAY8:
34380af0
KS
139 case PIX_FMT_GRAY16BE:
140 case PIX_FMT_GRAY16LE:
f0bbfc4a
MN
141 case PIX_FMT_YUVJ420P:
142 case PIX_FMT_YUVJ422P:
98c82d69 143 case PIX_FMT_YUVJ440P:
f0bbfc4a 144 case PIX_FMT_YUVJ444P:
b70335a2 145 case PIX_FMT_YUVA420P:
f0bbfc4a
MN
146 w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
147 h_align= 16;
98c82d69 148 if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP)
c81185a1 149 h_align= 32; // interlaced is rounded up to 2 MBs
f0bbfc4a
MN
150 break;
151 case PIX_FMT_YUV411P:
71e445fc 152 case PIX_FMT_UYYVYY411:
f0bbfc4a
MN
153 w_align=32;
154 h_align=8;
155 break;
156 case PIX_FMT_YUV410P:
157 if(s->codec_id == CODEC_ID_SVQ1){
158 w_align=64;
159 h_align=64;
160 }
d99fbbf4
RT
161 case PIX_FMT_RGB555:
162 if(s->codec_id == CODEC_ID_RPZA){
163 w_align=4;
164 h_align=4;
165 }
166 case PIX_FMT_PAL8:
6337178b
MN
167 case PIX_FMT_BGR8:
168 case PIX_FMT_RGB8:
d99fbbf4
RT
169 if(s->codec_id == CODEC_ID_SMC){
170 w_align=4;
171 h_align=4;
172 }
f0bbfc4a 173 break;
c31b8121
RT
174 case PIX_FMT_BGR24:
175 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
176 w_align=4;
177 h_align=4;
178 }
179 break;
f0bbfc4a
MN
180 default:
181 w_align= 1;
182 h_align= 1;
183 break;
184 }
185
ef516f73
DC
186 *width = FFALIGN(*width , w_align);
187 *height= FFALIGN(*height, h_align);
ae4ffe9f
MN
188 if(s->codec_id == CODEC_ID_H264)
189 *height+=2; // some of the optimized chroma MC reads one line too much
eb285cfe
RD
190
191 linesize_align[0] =
192 linesize_align[1] =
193 linesize_align[2] =
194 linesize_align[3] = STRIDE_ALIGN;
195//STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
196//we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
197//picture size unneccessarily in some cases. The solution here is not
198//pretty and better ideas are welcome!
199#if HAVE_MMX
200 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
201 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
202 s->codec_id == CODEC_ID_VP6A) {
203 linesize_align[0] =
204 linesize_align[1] =
205 linesize_align[2] = 16;
206 }
207#endif
208}
209
210void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
211 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
212 int linesize_align[4];
213 int align;
214 avcodec_align_dimensions2(s, width, height, linesize_align);
215 align = FFMAX(linesize_align[0], linesize_align[3]);
216 linesize_align[1] <<= chroma_shift;
217 linesize_align[2] <<= chroma_shift;
218 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
219 *width=FFALIGN(*width, align);
f0bbfc4a
MN
220}
221
0ecca7a4 222int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
445f0a8b 223 if((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
0ecca7a4 224 return 0;
115329f1 225
0ecca7a4 226 av_log(av_log_ctx, AV_LOG_ERROR, "picture size invalid (%ux%u)\n", w, h);
4cf1ba04 227 return AVERROR(EINVAL);
0ecca7a4
MN
228}
229
492cd3a9 230int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
1e491e29 231 int i;
f0bbfc4a
MN
232 int w= s->width;
233 int h= s->height;
d90cf87b 234 InternalBuffer *buf;
237e4938 235 int *picture_number;
0ecca7a4 236
65d999d6
MB
237 if(pic->data[0]!=NULL) {
238 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
239 return -1;
240 }
241 if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
242 av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
243 return -1;
244 }
1e491e29 245
0ecca7a4
MN
246 if(avcodec_check_dimensions(s,w,h))
247 return -1;
248
d90cf87b 249 if(s->internal_buffer==NULL){
6ebc89ac 250 s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
d90cf87b
MN
251 }
252#if 0
253 s->internal_buffer= av_fast_realloc(
115329f1
DB
254 s->internal_buffer,
255 &s->internal_buffer_size,
d90cf87b
MN
256 sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
257 );
258#endif
115329f1 259
d90cf87b 260 buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
6ebc89ac 261 picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
237e4938 262 (*picture_number)++;
115329f1 263
0701006e
MN
264 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
265 for(i=0; i<4; i++){
266 av_freep(&buf->base[i]);
267 buf->data[i]= NULL;
268 }
269 }
270
d90cf87b 271 if(buf->base[0]){
237e4938
MN
272 pic->age= *picture_number - buf->last_pic_num;
273 buf->last_pic_num= *picture_number;
1e491e29 274 }else{
f0bbfc4a 275 int h_chroma_shift, v_chroma_shift;
db7ae7d1
VS
276 int size[4] = {0};
277 int tmpsize;
c9d6e847 278 int unaligned;
c7622f9a 279 AVPicture picture;
503bc402 280 int stride_align[4];
c7622f9a 281
1e491e29 282 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
f0bbfc4a 283
eb285cfe 284 avcodec_align_dimensions2(s, &w, &h, stride_align);
115329f1 285
1e491e29
MN
286 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
287 w+= EDGE_WIDTH*2;
288 h+= EDGE_WIDTH*2;
289 }
5b67307a 290
c9d6e847
RD
291 do {
292 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
293 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
45bae968 294 ff_fill_linesize(&picture, s->pix_fmt, w);
c9d6e847
RD
295 // increase alignment of w for next try (rhs gives the lowest bit set in w)
296 w += w & ~(w-1);
db7ae7d1 297
c9d6e847 298 unaligned = 0;
45bae968 299 for (i=0; i<4; i++){
c9d6e847 300 unaligned |= picture.linesize[i] % stride_align[i];
45bae968 301 }
c9d6e847 302 } while (unaligned);
db7ae7d1 303
db7ae7d1 304 tmpsize = ff_fill_pointer(&picture, NULL, s->pix_fmt, h);
f8c96d01
RD
305 if (tmpsize < 0)
306 return -1;
db7ae7d1
VS
307
308 for (i=0; i<3 && picture.data[i+1]; i++)
309 size[i] = picture.data[i+1] - picture.data[i];
cf73e32a 310 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
c7622f9a 311
d90cf87b 312 buf->last_pic_num= -256*256*256*64;
c7622f9a
MN
313 memset(buf->base, 0, sizeof(buf->base));
314 memset(buf->data, 0, sizeof(buf->data));
1e491e29 315
b70335a2 316 for(i=0; i<4 && size[i]; i++){
2c19981a
MN
317 const int h_shift= i==0 ? 0 : h_chroma_shift;
318 const int v_shift= i==0 ? 0 : v_chroma_shift;
1e491e29 319
c7622f9a 320 buf->linesize[i]= picture.linesize[i];
1e491e29 321
c7622f9a 322 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
d90cf87b 323 if(buf->base[i]==NULL) return -1;
c7622f9a
MN
324 memset(buf->base[i], 128, size[i]);
325
6337178b
MN
326 // no edge if EDEG EMU or not planar YUV
327 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
d90cf87b 328 buf->data[i] = buf->base[i];
1e491e29 329 else
ef516f73 330 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
1e491e29 331 }
6337178b
MN
332 if(size[1] && !size[2])
333 ff_set_systematic_pal((uint32_t*)buf->data[1], s->pix_fmt);
0701006e
MN
334 buf->width = s->width;
335 buf->height = s->height;
336 buf->pix_fmt= s->pix_fmt;
1e491e29
MN
337 pic->age= 256*256*256*64;
338 }
237e4938 339 pic->type= FF_BUFFER_TYPE_INTERNAL;
1e491e29 340
d90cf87b
MN
341 for(i=0; i<4; i++){
342 pic->base[i]= buf->base[i];
343 pic->data[i]= buf->data[i];
237e4938 344 pic->linesize[i]= buf->linesize[i];
d90cf87b
MN
345 }
346 s->internal_buffer_count++;
347
79de84f2
MN
348 pic->reordered_opaque= s->reordered_opaque;
349
385c820b
AS
350 if(s->debug&FF_DEBUG_BUFFERS)
351 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
352
1e491e29
MN
353 return 0;
354}
355
492cd3a9 356void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
1e491e29 357 int i;
6829ac8d 358 InternalBuffer *buf, *last;
d90cf87b 359
4e00e76b 360 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
59c673d5 361 assert(s->internal_buffer_count);
d90cf87b 362
b1609412 363 buf = NULL; /* avoids warning */
d90cf87b
MN
364 for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
365 buf= &((InternalBuffer*)s->internal_buffer)[i];
366 if(buf->data[0] == pic->data[0])
367 break;
368 }
369 assert(i < s->internal_buffer_count);
370 s->internal_buffer_count--;
371 last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
372
6829ac8d 373 FFSWAP(InternalBuffer, *buf, *last);
d90cf87b 374
b70335a2 375 for(i=0; i<4; i++){
1e491e29 376 pic->data[i]=NULL;
d90cf87b
MN
377// pic->base[i]=NULL;
378 }
1e491e29 379//printf("R%X\n", pic->opaque);
385c820b
AS
380
381 if(s->debug&FF_DEBUG_BUFFERS)
382 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
1e491e29
MN
383}
384
e1c2a5a0
RT
385int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
386 AVFrame temp_pic;
387 int i;
388
389 /* If no picture return a new buffer */
390 if(pic->data[0] == NULL) {
391 /* We will copy from buffer, so must be readable */
392 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
393 return s->get_buffer(s, pic);
394 }
395
396 /* If internal buffer type return the same buffer */
b8af4fe9
MN
397 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
398 pic->reordered_opaque= s->reordered_opaque;
e1c2a5a0 399 return 0;
b8af4fe9 400 }
e1c2a5a0
RT
401
402 /*
403 * Not internal type and reget_buffer not overridden, emulate cr buffer
404 */
405 temp_pic = *pic;
406 for(i = 0; i < 4; i++)
407 pic->data[i] = pic->base[i] = NULL;
408 pic->opaque = NULL;
409 /* Allocate new frame */
410 if (s->get_buffer(s, pic))
411 return -1;
412 /* Copy image data from old buffer to new buffer */
636d6a4a 413 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
e1c2a5a0
RT
414 s->height);
415 s->release_buffer(s, &temp_pic); // Release old frame
416 return 0;
417}
418
3a84713a 419int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
9c3d33d6
MN
420 int i;
421
422 for(i=0; i<count; i++){
3a84713a 423 int r= func(c, (char*)arg + i*size);
9c3d33d6
MN
424 if(ret) ret[i]= r;
425 }
426 return 0;
427}
428
8d23a86f
RD
429int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
430 int i;
431
432 for(i=0; i<count; i++){
433 int r= func(c, arg, i, 0);
434 if(ret) ret[i]= r;
435 }
436 return 0;
437}
438
c269cf68
MN
439enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
440 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
441 ++fmt;
a33c7159
MN
442 return fmt[0];
443}
444
9740beff
MN
445void avcodec_get_frame_defaults(AVFrame *pic){
446 memset(pic, 0, sizeof(AVFrame));
447
448 pic->pts= AV_NOPTS_VALUE;
c342499d 449 pic->key_frame= 1;
9740beff
MN
450}
451
492cd3a9 452AVFrame *avcodec_alloc_frame(void){
9740beff 453 AVFrame *pic= av_malloc(sizeof(AVFrame));
115329f1 454
9740beff 455 if(pic==NULL) return NULL;
115329f1 456
9740beff 457 avcodec_get_frame_defaults(pic);
115329f1 458
1e491e29
MN
459 return pic;
460}
461
5e4c7ca2 462int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
de6d9b64 463{
ddebfb15 464 int ret= -1;
115329f1 465
f988ce6c
466 /* If there is a user-supplied mutex locking routine, call it. */
467 if (ff_lockmgr_cb) {
468 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
469 return -1;
470 }
471
ddebfb15
MN
472 entangled_thread_counter++;
473 if(entangled_thread_counter != 1){
474 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
475 goto end;
476 }
de6d9b64 477
9c87b8f7 478 if(avctx->codec || !codec)
ddebfb15 479 goto end;
09770af8 480
0edf8a7a
PG
481 if (codec->priv_data_size > 0) {
482 avctx->priv_data = av_mallocz(codec->priv_data_size);
90f06cea
PI
483 if (!avctx->priv_data) {
484 ret = AVERROR(ENOMEM);
ddebfb15 485 goto end;
90f06cea 486 }
0edf8a7a
PG
487 } else {
488 avctx->priv_data = NULL;
489 }
21adafec
MN
490
491 if(avctx->coded_width && avctx->coded_height)
492 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
493 else if(avctx->width && avctx->height)
494 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
495
fa77dd63 496#define SANE_NB_CHANNELS 128U
40461e54
DB
497 if (((avctx->coded_width || avctx->coded_height)
498 && avcodec_check_dimensions(avctx, avctx->coded_width, avctx->coded_height))
499 || avctx->channels > SANE_NB_CHANNELS) {
7868349a 500 ret = AVERROR(EINVAL);
2a9b5c9b 501 goto free_and_end;
0ecca7a4
MN
502 }
503
b5c85991 504 avctx->codec = codec;
72415b2a 505 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
681c180d
RD
506 avctx->codec_id == CODEC_ID_NONE) {
507 avctx->codec_type = codec->type;
508 avctx->codec_id = codec->id;
509 }
4c0dda2b
MN
510 if(avctx->codec_id != codec->id || avctx->codec_type != codec->type){
511 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
2a9b5c9b 512 goto free_and_end;
4c0dda2b 513 }
b5c85991 514 avctx->frame_number = 0;
6e546aaa 515 if(avctx->codec->init){
2de4f9eb
MN
516 ret = avctx->codec->init(avctx);
517 if (ret < 0) {
2a9b5c9b 518 goto free_and_end;
2de4f9eb 519 }
6e546aaa 520 }
ddebfb15
MN
521 ret=0;
522end:
523 entangled_thread_counter--;
f988ce6c
524
525 /* Release any user-supplied mutex. */
526 if (ff_lockmgr_cb) {
527 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
528 }
ddebfb15 529 return ret;
2a9b5c9b
MN
530free_and_end:
531 av_freep(&avctx->priv_data);
532 avctx->codec= NULL;
533 goto end;
de6d9b64
FB
534}
535
5e4c7ca2 536int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
de6d9b64
FB
537 const short *samples)
538{
0ecca7a4 539 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
5286d11f 540 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
0ecca7a4
MN
541 return -1;
542 }
6f824977 543 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
8eb027c8 544 int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
6f824977
MN
545 avctx->frame_number++;
546 return ret;
547 }else
548 return 0;
de6d9b64
FB
549}
550
5e4c7ca2 551int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
492cd3a9 552 const AVFrame *pict)
de6d9b64 553{
0ecca7a4 554 if(buf_size < FF_MIN_BUFFER_SIZE){
5286d11f 555 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
0ecca7a4
MN
556 return -1;
557 }
558 if(avcodec_check_dimensions(avctx,avctx->width,avctx->height))
559 return -1;
6f824977 560 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
8eb027c8 561 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
6f824977 562 avctx->frame_number++;
bb628dae 563 emms_c(); //needed to avoid an emms_c() call before every return;
115329f1 564
6f824977
MN
565 return ret;
566 }else
567 return 0;
de6d9b64
FB
568}
569
115329f1 570int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
240c1657
FB
571 const AVSubtitle *sub)
572{
573 int ret;
9413db9e
BA
574 if(sub->start_display_time) {
575 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
576 return -1;
577 }
505aa6c9
BA
578 if(sub->num_rects == 0 || !sub->rects)
579 return -1;
8eb027c8 580 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
240c1657
FB
581 avctx->frame_number++;
582 return ret;
583}
584
7a00bbad 585#if LIBAVCODEC_VERSION_MAJOR < 53
5e4c7ca2 586int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
de6d9b64 587 int *got_picture_ptr,
2efa7fd1 588 const uint8_t *buf, int buf_size)
de6d9b64 589{
7a00bbad
TB
590 AVPacket avpkt;
591 av_init_packet(&avpkt);
592 avpkt.data = buf;
593 avpkt.size = buf_size;
10f9ff9b
RD
594 // HACK for CorePNG to decode as normal PNG by default
595 avpkt.flags = AV_PKT_FLAG_KEY;
7a00bbad
TB
596
597 return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
598}
599#endif
600
601int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
602 int *got_picture_ptr,
603 AVPacket *avpkt)
604{
de6d9b64 605 int ret;
115329f1 606
53db1cae 607 *got_picture_ptr= 0;
0ecca7a4
MN
608 if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height))
609 return -1;
7a00bbad 610 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
115329f1 611 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
7a00bbad 612 avpkt);
6bb925f4 613
bb628dae 614 emms_c(); //needed to avoid an emms_c() call before every return;
115329f1
DB
615
616 if (*got_picture_ptr)
934982c4
MN
617 avctx->frame_number++;
618 }else
619 ret= 0;
620
de6d9b64
FB
621 return ret;
622}
623
7a00bbad 624#if LIBAVCODEC_VERSION_MAJOR < 53
5e4c7ca2 625int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
de6d9b64 626 int *frame_size_ptr,
2efa7fd1 627 const uint8_t *buf, int buf_size)
de6d9b64 628{
7a00bbad
TB
629 AVPacket avpkt;
630 av_init_packet(&avpkt);
631 avpkt.data = buf;
632 avpkt.size = buf_size;
633
634 return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
635}
636#endif
637
638int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
639 int *frame_size_ptr,
640 AVPacket *avpkt)
641{
de6d9b64
FB
642 int ret;
643
7a00bbad 644 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
9c856d62
MN
645 //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
646 if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
647 av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
648 return -1;
649 }
650 if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
81fc2f37 651 *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
9c856d62
MN
652 av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
653 return -1;
654 }
655
7a00bbad 656 ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
fec9ccb7 657 avctx->frame_number++;
ac66834c 658 }else{
fec9ccb7 659 ret= 0;
ac66834c
MN
660 *frame_size_ptr=0;
661 }
de6d9b64
FB
662 return ret;
663}
664
7a00bbad 665#if LIBAVCODEC_VERSION_MAJOR < 53
240c1657
FB
666int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
667 int *got_sub_ptr,
668 const uint8_t *buf, int buf_size)
669{
7a00bbad
TB
670 AVPacket avpkt;
671 av_init_packet(&avpkt);
672 avpkt.data = buf;
673 avpkt.size = buf_size;
674
675 return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
676}
677#endif
678
679int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
680 int *got_sub_ptr,
681 AVPacket *avpkt)
682{
240c1657
FB
683 int ret;
684
685 *got_sub_ptr = 0;
7a00bbad 686 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
240c1657
FB
687 if (*got_sub_ptr)
688 avctx->frame_number++;
689 return ret;
690}
691
0752cd39 692av_cold int avcodec_close(AVCodecContext *avctx)
de6d9b64 693{
f988ce6c
694 /* If there is a user-supplied mutex locking routine, call it. */
695 if (ff_lockmgr_cb) {
696 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
697 return -1;
698 }
699
ddebfb15
MN
700 entangled_thread_counter++;
701 if(entangled_thread_counter != 1){
702 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
703 entangled_thread_counter--;
704 return -1;
705 }
706
49fb20cb 707 if (HAVE_THREADS && avctx->thread_opaque)
ab2cfc59 708 avcodec_thread_free(avctx);
dccda293 709 if (avctx->codec && avctx->codec->close)
de6d9b64 710 avctx->codec->close(avctx);
eea8c08f 711 avcodec_default_free_buffers(avctx);
3123dd79 712 av_freep(&avctx->priv_data);
77a670e7 713 if(avctx->codec && avctx->codec->encode)
c4f267ab 714 av_freep(&avctx->extradata);
de6d9b64 715 avctx->codec = NULL;
ddebfb15 716 entangled_thread_counter--;
f988ce6c
717
718 /* Release any user-supplied mutex. */
719 if (ff_lockmgr_cb) {
720 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
721 }
de6d9b64
FB
722 return 0;
723}
724
725AVCodec *avcodec_find_encoder(enum CodecID id)
726{
727 AVCodec *p;
728 p = first_avcodec;
729 while (p) {
730 if (p->encode != NULL && p->id == id)
731 return p;
732 p = p->next;
733 }
734 return NULL;
735}
736
98f3b098
A
737AVCodec *avcodec_find_encoder_by_name(const char *name)
738{
739 AVCodec *p;
fc228c90
AJ
740 if (!name)
741 return NULL;
98f3b098
A
742 p = first_avcodec;
743 while (p) {
744 if (p->encode != NULL && strcmp(name,p->name) == 0)
745 return p;
746 p = p->next;
747 }
748 return NULL;
749}
750
de6d9b64
FB
751AVCodec *avcodec_find_decoder(enum CodecID id)
752{
753 AVCodec *p;
754 p = first_avcodec;
755 while (p) {
756 if (p->decode != NULL && p->id == id)
757 return p;
758 p = p->next;
759 }
760 return NULL;
761}
762
763AVCodec *avcodec_find_decoder_by_name(const char *name)
764{
765 AVCodec *p;
fc228c90
AJ
766 if (!name)
767 return NULL;
de6d9b64
FB
768 p = first_avcodec;
769 while (p) {
770 if (p->decode != NULL && strcmp(name,p->name) == 0)
771 return p;
772 p = p->next;
773 }
774 return NULL;
775}
776
406aa93f 777static int get_bit_rate(AVCodecContext *ctx)
ce34ff6b
RK
778{
779 int bit_rate;
780 int bits_per_sample;
781
782 switch(ctx->codec_type) {
72415b2a 783 case AVMEDIA_TYPE_VIDEO:
4563cf24
SS
784 case AVMEDIA_TYPE_DATA:
785 case AVMEDIA_TYPE_SUBTITLE:
786 case AVMEDIA_TYPE_ATTACHMENT:
ce34ff6b
RK
787 bit_rate = ctx->bit_rate;
788 break;
72415b2a 789 case AVMEDIA_TYPE_AUDIO:
ce34ff6b
RK
790 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
791 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
792 break;
ce34ff6b
RK
793 default:
794 bit_rate = 0;
795 break;
796 }
797 return bit_rate;
798}
799
de6d9b64
FB
800void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
801{
802 const char *codec_name;
803 AVCodec *p;
804 char buf1[32];
a96b68b7 805 int bitrate;
59771f71 806 AVRational display_aspect_ratio;
de6d9b64
FB
807
808 if (encode)
809 p = avcodec_find_encoder(enc->codec_id);
810 else
811 p = avcodec_find_decoder(enc->codec_id);
812
813 if (p) {
814 codec_name = p->name;
985180a1
FB
815 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
816 /* fake mpeg2 transport stream codec (currently not
817 registered) */
818 codec_name = "mpeg2ts";
de6d9b64
FB
819 } else if (enc->codec_name[0] != '\0') {
820 codec_name = enc->codec_name;
821 } else {
822 /* output avi tags */
115329f1 823 if( isprint(enc->codec_tag&0xFF) && isprint((enc->codec_tag>>8)&0xFF)
b983a5c6 824 && isprint((enc->codec_tag>>16)&0xFF) && isprint((enc->codec_tag>>24)&0xFF)){
115329f1 825 snprintf(buf1, sizeof(buf1), "%c%c%c%c / 0x%04X",
de6d9b64
FB
826 enc->codec_tag & 0xff,
827 (enc->codec_tag >> 8) & 0xff,
828 (enc->codec_tag >> 16) & 0xff,
b983a5c6
MN
829 (enc->codec_tag >> 24) & 0xff,
830 enc->codec_tag);
de6d9b64
FB
831 } else {
832 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
833 }
834 codec_name = buf1;
835 }
836
837 switch(enc->codec_type) {
72415b2a 838 case AVMEDIA_TYPE_VIDEO:
de6d9b64
FB
839 snprintf(buf, buf_size,
840 "Video: %s%s",
7d1c3fc1 841 codec_name, enc->mb_decision ? " (hq)" : "");
82c0c4ba 842 if (enc->pix_fmt != PIX_FMT_NONE) {
cf087595
FB
843 snprintf(buf + strlen(buf), buf_size - strlen(buf),
844 ", %s",
9bbffbb1 845 avcodec_get_pix_fmt_name(enc->pix_fmt));
cf087595 846 }
de6d9b64
FB
847 if (enc->width) {
848 snprintf(buf + strlen(buf), buf_size - strlen(buf),
21189011
MN
849 ", %dx%d",
850 enc->width, enc->height);
7ee4dd02 851 if (enc->sample_aspect_ratio.num) {
cbaf50f8
BC
852 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
853 enc->width*enc->sample_aspect_ratio.num,
854 enc->height*enc->sample_aspect_ratio.den,
855 1024*1024);
856 snprintf(buf + strlen(buf), buf_size - strlen(buf),
857 " [PAR %d:%d DAR %d:%d]",
858 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
859 display_aspect_ratio.num, display_aspect_ratio.den);
7ee4dd02 860 }
a309073b 861 if(av_log_get_level() >= AV_LOG_DEBUG){
9ce6c138 862 int g= av_gcd(enc->time_base.num, enc->time_base.den);
21189011
MN
863 snprintf(buf + strlen(buf), buf_size - strlen(buf),
864 ", %d/%d",
865 enc->time_base.num/g, enc->time_base.den/g);
866 }
de6d9b64 867 }
4bfad535
FB
868 if (encode) {
869 snprintf(buf + strlen(buf), buf_size - strlen(buf),
870 ", q=%d-%d", enc->qmin, enc->qmax);
871 }
de6d9b64 872 break;
72415b2a 873 case AVMEDIA_TYPE_AUDIO:
de6d9b64
FB
874 snprintf(buf, buf_size,
875 "Audio: %s",
876 codec_name);
877 if (enc->sample_rate) {
878 snprintf(buf + strlen(buf), buf_size - strlen(buf),
0d72e7d0 879 ", %d Hz", enc->sample_rate);
de6d9b64 880 }
0d72e7d0
PR
881 av_strlcat(buf, ", ", buf_size);
882 avcodec_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
9e82a113
PR
883 if (enc->sample_fmt != SAMPLE_FMT_NONE) {
884 snprintf(buf + strlen(buf), buf_size - strlen(buf),
885 ", %s", avcodec_get_sample_fmt_name(enc->sample_fmt));
886 }
de6d9b64 887 break;
72415b2a 888 case AVMEDIA_TYPE_DATA:
985180a1 889 snprintf(buf, buf_size, "Data: %s", codec_name);
240c1657 890 break;
72415b2a 891 case AVMEDIA_TYPE_SUBTITLE:
240c1657 892 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
985180a1 893 break;
72415b2a 894 case AVMEDIA_TYPE_ATTACHMENT:
f8d7c9d3 895 snprintf(buf, buf_size, "Attachment: %s", codec_name);
f8d7c9d3 896 break;
de6d9b64 897 default:
9fe5a7b8
MN
898 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
899 return;
de6d9b64 900 }
4bfad535
FB
901 if (encode) {
902 if (enc->flags & CODEC_FLAG_PASS1)
903 snprintf(buf + strlen(buf), buf_size - strlen(buf),
904 ", pass 1");
905 if (enc->flags & CODEC_FLAG_PASS2)
906 snprintf(buf + strlen(buf), buf_size - strlen(buf),
907 ", pass 2");
908 }
406aa93f 909 bitrate = get_bit_rate(enc);
a96b68b7 910 if (bitrate != 0) {
115329f1 911 snprintf(buf + strlen(buf), buf_size - strlen(buf),
a96b68b7 912 ", %d kb/s", bitrate / 1000);
de6d9b64
FB
913 }
914}
915
156e5023
NK
916unsigned avcodec_version( void )
917{
918 return LIBAVCODEC_VERSION_INT;
919}
cf087595 920
41600690 921const char *avcodec_configuration(void)
c1736936
DB
922{
923 return FFMPEG_CONFIGURATION;
924}
925
41600690 926const char *avcodec_license(void)
c1736936
DB
927{
928#define LICENSE_PREFIX "libavcodec license: "
929 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
930}
931
de6d9b64
FB
932void avcodec_init(void)
933{
5e534865 934 static int initialized = 0;
0344cd0a 935
5e534865 936 if (initialized != 0)
bb270c08 937 return;
5e534865 938 initialized = 1;
0344cd0a 939
486497e0 940 dsputil_static_init();
de6d9b64
FB
941}
942
1c2a8c7f
MN
943void avcodec_flush_buffers(AVCodecContext *avctx)
944{
7a06ff14
MN
945 if(avctx->codec->flush)
946 avctx->codec->flush(avctx);
1c2a8c7f
MN
947}
948
b100eab8 949void avcodec_default_free_buffers(AVCodecContext *s){
d90cf87b
MN
950 int i, j;
951
952 if(s->internal_buffer==NULL) return;
115329f1 953
ea09f691
RD
954 if (s->internal_buffer_count)
955 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
d90cf87b
MN
956 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
957 InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
958 for(j=0; j<4; j++){
959 av_freep(&buf->base[j]);
960 buf->data[j]= NULL;
961 }
962 }
963 av_freep(&s->internal_buffer);
115329f1 964
d90cf87b
MN
965 s->internal_buffer_count=0;
966}
967
d8085ea7
MN
968char av_get_pict_type_char(int pict_type){
969 switch(pict_type){
fbd4293d
AJ
970 case FF_I_TYPE: return 'I';
971 case FF_P_TYPE: return 'P';
972 case FF_B_TYPE: return 'B';
973 case FF_S_TYPE: return 'S';
974 case FF_SI_TYPE:return 'i';
975 case FF_SP_TYPE:return 'p';
48d533e6 976 case FF_BI_TYPE:return 'b';
c06ccb13 977 default: return '?';
d8085ea7
MN
978 }
979}
980
ac3e1834
BC
981int av_get_bits_per_sample(enum CodecID codec_id){
982 switch(codec_id){
5da71469 983 case CODEC_ID_ADPCM_SBPRO_2:
f1b163e0 984 return 2;
5da71469 985 case CODEC_ID_ADPCM_SBPRO_3:
f1b163e0 986 return 3;
5da71469 987 case CODEC_ID_ADPCM_SBPRO_4:
f1b163e0 988 case CODEC_ID_ADPCM_CT:
07f2a575 989 case CODEC_ID_ADPCM_IMA_WAV:
9df9b810
DV
990 case CODEC_ID_ADPCM_MS:
991 case CODEC_ID_ADPCM_YAMAHA:
f1b163e0 992 return 4;
ac3e1834
BC
993 case CODEC_ID_PCM_ALAW:
994 case CODEC_ID_PCM_MULAW:
995 case CODEC_ID_PCM_S8:
996 case CODEC_ID_PCM_U8:
9d49b8ff 997 case CODEC_ID_PCM_ZORK:
ac3e1834
BC
998 return 8;
999 case CODEC_ID_PCM_S16BE:
1000 case CODEC_ID_PCM_S16LE:
725d86bf 1001 case CODEC_ID_PCM_S16LE_PLANAR:
ac3e1834
BC
1002 case CODEC_ID_PCM_U16BE:
1003 case CODEC_ID_PCM_U16LE:
1004 return 16;
1005 case CODEC_ID_PCM_S24DAUD:
1006 case CODEC_ID_PCM_S24BE:
1007 case CODEC_ID_PCM_S24LE:
1008 case CODEC_ID_PCM_U24BE:
1009 case CODEC_ID_PCM_U24LE:
1010 return 24;
1011 case CODEC_ID_PCM_S32BE:
1012 case CODEC_ID_PCM_S32LE:
1013 case CODEC_ID_PCM_U32BE:
1014 case CODEC_ID_PCM_U32LE:
aa29709e 1015 case CODEC_ID_PCM_F32BE:
143a5d6f 1016 case CODEC_ID_PCM_F32LE:
ac3e1834 1017 return 32;
143a5d6f
PR
1018 case CODEC_ID_PCM_F64BE:
1019 case CODEC_ID_PCM_F64LE:
1020 return 64;
ac3e1834
BC
1021 default:
1022 return 0;
1023 }
1024}
1025
42c71907
DC
1026int av_get_bits_per_sample_format(enum SampleFormat sample_fmt) {
1027 switch (sample_fmt) {
1028 case SAMPLE_FMT_U8:
1029 return 8;
1030 case SAMPLE_FMT_S16:
1031 return 16;
42c71907
DC
1032 case SAMPLE_FMT_S32:
1033 case SAMPLE_FMT_FLT:
1034 return 32;
e06b00f3
PR
1035 case SAMPLE_FMT_DBL:
1036 return 64;
42c71907
DC
1037 default:
1038 return 0;
1039 }
1040}
1041
b250f9c6 1042#if !HAVE_THREADS
ca8ad847 1043int avcodec_thread_init(AVCodecContext *s, int thread_count){
2e418f5e 1044 s->thread_count = thread_count;
ca8ad847
MN
1045 return -1;
1046}
1047#endif
ad2b531d
MR
1048
1049unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1050{
1051 unsigned int n = 0;
1052
1053 while(v >= 0xff) {
1054 *s++ = 0xff;
1055 v -= 0xff;
1056 n++;
1057 }
1058 *s = v;
1059 n++;
1060 return n;
1061}
1005f542
CH
1062
1063/* Wrapper to work around the lack of mkstemp() on mingw/cygin.
1064 * Also, tries to create file in /tmp first, if possible.
1065 * *prefix can be a character constant; *filename will be allocated internally.
1066 * Returns file descriptor of opened file (or -1 on error)
1067 * and opened file name in **filename. */
1068int av_tempfile(char *prefix, char **filename) {
1069 int fd=-1;
b250f9c6 1070#if !HAVE_MKSTEMP
1005f542
CH
1071 *filename = tempnam(".", prefix);
1072#else
1073 size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
f2508b17 1074 *filename = av_malloc(len);
1005f542
CH
1075#endif
1076 /* -----common section-----*/
1077 if (*filename == NULL) {
1078 av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
1079 return -1;
1080 }
b250f9c6 1081#if !HAVE_MKSTEMP
76353b42 1082 fd = open(*filename, O_RDWR | O_BINARY | O_CREAT, 0444);
1005f542
CH
1083#else
1084 snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
1085 fd = mkstemp(*filename);
1086 if (fd < 0) {
1087 snprintf(*filename, len, "./%sXXXXXX", prefix);
1088 fd = mkstemp(*filename);
1089 }
1090#endif
1091 /* -----common section-----*/
1092 if (fd < 0) {
1093 av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
1094 return -1;
1095 }
1096 return fd; /* success */
1097}
26ef3220
SS
1098
1099typedef struct {
c0ad72a7 1100 const char *abbr;
26ef3220 1101 int width, height;
c0ad72a7
SS
1102} VideoFrameSizeAbbr;
1103
1104typedef struct {
1105 const char *abbr;
1106 int rate_num, rate_den;
1107} VideoFrameRateAbbr;
1108
cf2baeb3 1109static const VideoFrameSizeAbbr video_frame_size_abbrs[] = {
c0ad72a7
SS
1110 { "ntsc", 720, 480 },
1111 { "pal", 720, 576 },
1112 { "qntsc", 352, 240 }, /* VCD compliant NTSC */
1113 { "qpal", 352, 288 }, /* VCD compliant PAL */
1114 { "sntsc", 640, 480 }, /* square pixel NTSC */
1115 { "spal", 768, 576 }, /* square pixel PAL */
1116 { "film", 352, 240 },
1117 { "ntsc-film", 352, 240 },
1118 { "sqcif", 128, 96 },
1119 { "qcif", 176, 144 },
1120 { "cif", 352, 288 },
1121 { "4cif", 704, 576 },
41913a35 1122 { "16cif", 1408,1152 },
c0ad72a7
SS
1123 { "qqvga", 160, 120 },
1124 { "qvga", 320, 240 },
1125 { "vga", 640, 480 },
1126 { "svga", 800, 600 },
1127 { "xga", 1024, 768 },
1128 { "uxga", 1600,1200 },
1129 { "qxga", 2048,1536 },
1130 { "sxga", 1280,1024 },
1131 { "qsxga", 2560,2048 },
1132 { "hsxga", 5120,4096 },
1133 { "wvga", 852, 480 },
1134 { "wxga", 1366, 768 },
1135 { "wsxga", 1600,1024 },
1136 { "wuxga", 1920,1200 },
1137 { "woxga", 2560,1600 },
1138 { "wqsxga", 3200,2048 },
1139 { "wquxga", 3840,2400 },
1140 { "whsxga", 6400,4096 },
1141 { "whuxga", 7680,4800 },
1142 { "cga", 320, 200 },
1143 { "ega", 640, 350 },
1144 { "hd480", 852, 480 },
1145 { "hd720", 1280, 720 },
1146 { "hd1080", 1920,1080 },
1147};
1148
cf2baeb3 1149static const VideoFrameRateAbbr video_frame_rate_abbrs[]= {
c0ad72a7
SS
1150 { "ntsc", 30000, 1001 },
1151 { "pal", 25, 1 },
1152 { "qntsc", 30000, 1001 }, /* VCD compliant NTSC */
1153 { "qpal", 25, 1 }, /* VCD compliant PAL */
1154 { "sntsc", 30000, 1001 }, /* square pixel NTSC */
1155 { "spal", 25, 1 }, /* square pixel PAL */
1156 { "film", 24, 1 },
1157 { "ntsc-film", 24000, 1001 },
26ef3220
SS
1158};
1159
1160int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
1161{
1162 int i;
37d3e066 1163 int n = FF_ARRAY_ELEMS(video_frame_size_abbrs);
8468664b 1164 char *p;
26ef3220
SS
1165 int frame_width = 0, frame_height = 0;
1166
1167 for(i=0;i<n;i++) {
c0ad72a7
SS
1168 if (!strcmp(video_frame_size_abbrs[i].abbr, str)) {
1169 frame_width = video_frame_size_abbrs[i].width;
1170 frame_height = video_frame_size_abbrs[i].height;
26ef3220
SS
1171 break;
1172 }
1173 }
1174 if (i == n) {
1175 p = str;
8468664b 1176 frame_width = strtol(p, &p, 10);
26ef3220
SS
1177 if (*p)
1178 p++;
8468664b 1179 frame_height = strtol(p, &p, 10);
26ef3220
SS
1180 }
1181 if (frame_width <= 0 || frame_height <= 0)
1182 return -1;
1183 *width_ptr = frame_width;
1184 *height_ptr = frame_height;
1185 return 0;
1186}
1187
1188int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
1189{
1190 int i;
37d3e066 1191 int n = FF_ARRAY_ELEMS(video_frame_rate_abbrs);
26ef3220
SS
1192 char* cp;
1193
1194 /* First, we check our abbreviation table */
c0ad72a7
SS
1195 for (i = 0; i < n; ++i)
1196 if (!strcmp(video_frame_rate_abbrs[i].abbr, arg)) {
1197 frame_rate->num = video_frame_rate_abbrs[i].rate_num;
1198 frame_rate->den = video_frame_rate_abbrs[i].rate_den;
26ef3220
SS
1199 return 0;
1200 }
1201
1202 /* Then, we try to parse it as fraction */
1203 cp = strchr(arg, '/');
1204 if (!cp)
1205 cp = strchr(arg, ':');
1206 if (cp) {
1207 char* cpp;
1208 frame_rate->num = strtol(arg, &cpp, 10);
1209 if (cpp != arg || cpp == cp)
1210 frame_rate->den = strtol(cp+1, &cpp, 10);
1211 else
1212 frame_rate->num = 0;
1213 }
1214 else {
1215 /* Finally we give up and parse it as double */
38360668 1216 AVRational time_base = av_d2q(strtod(arg, 0), 1001000);
26ef3220
SS
1217 frame_rate->den = time_base.den;
1218 frame_rate->num = time_base.num;
1219 }
1220 if (!frame_rate->num || !frame_rate->den)
1221 return -1;
1222 else
1223 return 0;
1224}
ea779d91 1225
c46eeae2
MN
1226int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1227 int i;
1228 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1229 return i;
1230}
1231
ce863d7f 1232void av_log_missing_feature(void *avc, const char *feature, int want_sample)
ea779d91
JR
1233{
1234 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1235 "version to the newest one from SVN. If the problem still "
1236 "occurs, it means that your file has a feature which has not "
1237 "been implemented.", feature);
1238 if(want_sample)
ce863d7f 1239 av_log_ask_for_sample(avc, NULL);
0ba39dd1
KG
1240 else
1241 av_log(avc, AV_LOG_WARNING, "\n");
1242}
1243
ce863d7f 1244void av_log_ask_for_sample(void *avc, const char *msg)
0ba39dd1
KG
1245{
1246 if (msg)
1247 av_log(avc, AV_LOG_WARNING, "%s ", msg);
1248 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1249 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1250 "and contact the ffmpeg-devel mailing list.\n");
ea779d91 1251}
c895618b
MN
1252
1253static AVHWAccel *first_hwaccel = NULL;
1254
1255void av_register_hwaccel(AVHWAccel *hwaccel)
1256{
1257 AVHWAccel **p = &first_hwaccel;
1258 while (*p)
1259 p = &(*p)->next;
1260 *p = hwaccel;
1261 hwaccel->next = NULL;
1262}
414d9d7f
MN
1263
1264AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1265{
1266 return hwaccel ? hwaccel->next : first_hwaccel;
1267}
6059f13c
MN
1268
1269AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1270{
1271 AVHWAccel *hwaccel=NULL;
1272
1273 while((hwaccel= av_hwaccel_next(hwaccel))){
1274 if ( hwaccel->id == codec_id
1275 && hwaccel->pix_fmt == pix_fmt)
1276 return hwaccel;
1277 }
1278 return NULL;
1279}
f988ce6c
1280
1281int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1282{
1283 if (ff_lockmgr_cb) {
1284 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1285 return -1;
1286 }
1287
1288 ff_lockmgr_cb = cb;
1289
1290 if (ff_lockmgr_cb) {
1291 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1292 return -1;
1293 }
1294 return 0;
1295}