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