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