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