avcodec: add a public function, avcodec_fill_audio_frame().
[libav.git] / libavcodec / utils.c
1 /*
2 * utils for libavcodec
3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of Libav.
7 *
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * utils.
26 */
27
28 #include "libavutil/avstring.h"
29 #include "libavutil/crc.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/audioconvert.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/samplefmt.h"
35 #include "libavutil/dict.h"
36 #include "avcodec.h"
37 #include "dsputil.h"
38 #include "libavutil/opt.h"
39 #include "imgconvert.h"
40 #include "thread.h"
41 #include "audioconvert.h"
42 #include "internal.h"
43 #include "bytestream.h"
44 #include <stdlib.h>
45 #include <stdarg.h>
46 #include <limits.h>
47 #include <float.h>
48
49 static int volatile entangled_thread_counter=0;
50 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
51 static void *codec_mutex;
52 static void *avformat_mutex;
53
54 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
55 {
56 if(min_size < *size)
57 return ptr;
58
59 min_size= FFMAX(17*min_size/16 + 32, min_size);
60
61 ptr= av_realloc(ptr, min_size);
62 if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
63 min_size= 0;
64
65 *size= min_size;
66
67 return ptr;
68 }
69
70 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
71 {
72 void **p = ptr;
73 if (min_size < *size)
74 return;
75 min_size= FFMAX(17*min_size/16 + 32, min_size);
76 av_free(*p);
77 *p = av_malloc(min_size);
78 if (!*p) min_size = 0;
79 *size= min_size;
80 }
81
82 /* encoder management */
83 static AVCodec *first_avcodec = NULL;
84
85 AVCodec *av_codec_next(AVCodec *c){
86 if(c) return c->next;
87 else return first_avcodec;
88 }
89
90 #if !FF_API_AVCODEC_INIT
91 static
92 #endif
93 void avcodec_init(void)
94 {
95 static int initialized = 0;
96
97 if (initialized != 0)
98 return;
99 initialized = 1;
100
101 dsputil_static_init();
102 }
103
104 void avcodec_register(AVCodec *codec)
105 {
106 AVCodec **p;
107 avcodec_init();
108 p = &first_avcodec;
109 while (*p != NULL) p = &(*p)->next;
110 *p = codec;
111 codec->next = NULL;
112
113 if (codec->init_static_data)
114 codec->init_static_data(codec);
115 }
116
117 unsigned avcodec_get_edge_width(void)
118 {
119 return EDGE_WIDTH;
120 }
121
122 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
123 s->coded_width = width;
124 s->coded_height= height;
125 s->width = -((-width )>>s->lowres);
126 s->height= -((-height)>>s->lowres);
127 }
128
129 #define INTERNAL_BUFFER_SIZE (32+1)
130
131 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
132 int linesize_align[AV_NUM_DATA_POINTERS])
133 {
134 int i;
135 int w_align= 1;
136 int h_align= 1;
137
138 switch(s->pix_fmt){
139 case PIX_FMT_YUV420P:
140 case PIX_FMT_YUYV422:
141 case PIX_FMT_UYVY422:
142 case PIX_FMT_YUV422P:
143 case PIX_FMT_YUV440P:
144 case PIX_FMT_YUV444P:
145 case PIX_FMT_GBRP:
146 case PIX_FMT_GRAY8:
147 case PIX_FMT_GRAY16BE:
148 case PIX_FMT_GRAY16LE:
149 case PIX_FMT_YUVJ420P:
150 case PIX_FMT_YUVJ422P:
151 case PIX_FMT_YUVJ440P:
152 case PIX_FMT_YUVJ444P:
153 case PIX_FMT_YUVA420P:
154 case PIX_FMT_YUV420P9LE:
155 case PIX_FMT_YUV420P9BE:
156 case PIX_FMT_YUV420P10LE:
157 case PIX_FMT_YUV420P10BE:
158 case PIX_FMT_YUV422P9LE:
159 case PIX_FMT_YUV422P9BE:
160 case PIX_FMT_YUV422P10LE:
161 case PIX_FMT_YUV422P10BE:
162 case PIX_FMT_YUV444P9LE:
163 case PIX_FMT_YUV444P9BE:
164 case PIX_FMT_YUV444P10LE:
165 case PIX_FMT_YUV444P10BE:
166 case PIX_FMT_GBRP9LE:
167 case PIX_FMT_GBRP9BE:
168 case PIX_FMT_GBRP10LE:
169 case PIX_FMT_GBRP10BE:
170 w_align = 16; //FIXME assume 16 pixel per macroblock
171 h_align = 16 * 2; // interlaced needs 2 macroblocks height
172 break;
173 case PIX_FMT_YUV411P:
174 case PIX_FMT_UYYVYY411:
175 w_align=32;
176 h_align=8;
177 break;
178 case PIX_FMT_YUV410P:
179 if(s->codec_id == CODEC_ID_SVQ1){
180 w_align=64;
181 h_align=64;
182 }
183 case PIX_FMT_RGB555:
184 if(s->codec_id == CODEC_ID_RPZA){
185 w_align=4;
186 h_align=4;
187 }
188 case PIX_FMT_PAL8:
189 case PIX_FMT_BGR8:
190 case PIX_FMT_RGB8:
191 if(s->codec_id == CODEC_ID_SMC){
192 w_align=4;
193 h_align=4;
194 }
195 break;
196 case PIX_FMT_BGR24:
197 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
198 w_align=4;
199 h_align=4;
200 }
201 break;
202 default:
203 w_align= 1;
204 h_align= 1;
205 break;
206 }
207
208 *width = FFALIGN(*width , w_align);
209 *height= FFALIGN(*height, h_align);
210 if(s->codec_id == CODEC_ID_H264 || s->lowres)
211 *height+=2; // some of the optimized chroma MC reads one line too much
212 // which is also done in mpeg decoders with lowres > 0
213
214 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
215 linesize_align[i] = STRIDE_ALIGN;
216 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
217 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
218 //picture size unneccessarily in some cases. The solution here is not
219 //pretty and better ideas are welcome!
220 #if HAVE_MMX
221 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
222 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
223 s->codec_id == CODEC_ID_VP6A) {
224 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
225 linesize_align[i] = 16;
226 }
227 #endif
228 }
229
230 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
231 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
232 int linesize_align[AV_NUM_DATA_POINTERS];
233 int align;
234 avcodec_align_dimensions2(s, width, height, linesize_align);
235 align = FFMAX(linesize_align[0], linesize_align[3]);
236 linesize_align[1] <<= chroma_shift;
237 linesize_align[2] <<= chroma_shift;
238 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
239 *width=FFALIGN(*width, align);
240 }
241
242 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
243 enum AVSampleFormat sample_fmt, const uint8_t *buf,
244 int buf_size, int align)
245 {
246 int ch, planar, needed_size, ret = 0;
247
248 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
249 frame->nb_samples, sample_fmt,
250 align);
251 if (buf_size < needed_size)
252 return AVERROR(EINVAL);
253
254 planar = av_sample_fmt_is_planar(sample_fmt);
255 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
256 if (!(frame->extended_data = av_mallocz(nb_channels *
257 sizeof(*frame->extended_data))))
258 return AVERROR(ENOMEM);
259 } else {
260 frame->extended_data = frame->data;
261 }
262
263 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
264 buf, nb_channels, frame->nb_samples,
265 sample_fmt, align)) < 0) {
266 if (frame->extended_data != frame->data)
267 av_free(frame->extended_data);
268 return ret;
269 }
270 if (frame->extended_data != frame->data) {
271 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
272 frame->data[ch] = frame->extended_data[ch];
273 }
274
275 return ret;
276 }
277
278 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
279 {
280 AVCodecInternal *avci = avctx->internal;
281 InternalBuffer *buf;
282 int buf_size, ret;
283
284 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
285 frame->nb_samples, avctx->sample_fmt,
286 32);
287 if (buf_size < 0)
288 return AVERROR(EINVAL);
289
290 /* allocate InternalBuffer if needed */
291 if (!avci->buffer) {
292 avci->buffer = av_mallocz(sizeof(InternalBuffer));
293 if (!avci->buffer)
294 return AVERROR(ENOMEM);
295 }
296 buf = avci->buffer;
297
298 /* if there is a previously-used internal buffer, check its size and
299 channel count to see if we can reuse it */
300 if (buf->extended_data) {
301 /* if current buffer is too small, free it */
302 if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
303 av_free(buf->extended_data[0]);
304 if (buf->extended_data != buf->data)
305 av_free(&buf->extended_data);
306 buf->extended_data = NULL;
307 buf->data[0] = NULL;
308 }
309 /* if number of channels has changed, reset and/or free extended data
310 pointers but leave data buffer in buf->data[0] for reuse */
311 if (buf->nb_channels != avctx->channels) {
312 if (buf->extended_data != buf->data)
313 av_free(buf->extended_data);
314 buf->extended_data = NULL;
315 }
316 }
317
318 /* if there is no previous buffer or the previous buffer cannot be used
319 as-is, allocate a new buffer and/or rearrange the channel pointers */
320 if (!buf->extended_data) {
321 if (!buf->data[0]) {
322 if (!(buf->data[0] = av_mallocz(buf_size)))
323 return AVERROR(ENOMEM);
324 buf->audio_data_size = buf_size;
325 }
326 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
327 avctx->sample_fmt, buf->data[0],
328 buf->audio_data_size, 32)))
329 return ret;
330
331 if (frame->extended_data == frame->data)
332 buf->extended_data = buf->data;
333 else
334 buf->extended_data = frame->extended_data;
335 memcpy(buf->data, frame->data, sizeof(frame->data));
336 buf->linesize[0] = frame->linesize[0];
337 buf->nb_channels = avctx->channels;
338 } else {
339 /* copy InternalBuffer info to the AVFrame */
340 frame->extended_data = buf->extended_data;
341 frame->linesize[0] = buf->linesize[0];
342 memcpy(frame->data, buf->data, sizeof(frame->data));
343 }
344
345 frame->type = FF_BUFFER_TYPE_INTERNAL;
346
347 if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
348 else frame->pkt_pts = AV_NOPTS_VALUE;
349 frame->reordered_opaque = avctx->reordered_opaque;
350
351 if (avctx->debug & FF_DEBUG_BUFFERS)
352 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
353 "internal audio buffer used\n", frame);
354
355 return 0;
356 }
357
358 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
359 {
360 int i;
361 int w= s->width;
362 int h= s->height;
363 InternalBuffer *buf;
364 AVCodecInternal *avci = s->internal;
365
366 if(pic->data[0]!=NULL) {
367 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
368 return -1;
369 }
370 if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
371 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
372 return -1;
373 }
374
375 if(av_image_check_size(w, h, 0, s))
376 return -1;
377
378 if (!avci->buffer) {
379 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
380 sizeof(InternalBuffer));
381 }
382
383 buf = &avci->buffer[avci->buffer_count];
384
385 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
386 if(s->active_thread_type&FF_THREAD_FRAME) {
387 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
388 return -1;
389 }
390
391 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
392 av_freep(&buf->base[i]);
393 buf->data[i]= NULL;
394 }
395 }
396
397 if (!buf->base[0]) {
398 int h_chroma_shift, v_chroma_shift;
399 int size[4] = {0};
400 int tmpsize;
401 int unaligned;
402 AVPicture picture;
403 int stride_align[AV_NUM_DATA_POINTERS];
404 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
405
406 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
407
408 avcodec_align_dimensions2(s, &w, &h, stride_align);
409
410 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
411 w+= EDGE_WIDTH*2;
412 h+= EDGE_WIDTH*2;
413 }
414
415 do {
416 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
417 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
418 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
419 // increase alignment of w for next try (rhs gives the lowest bit set in w)
420 w += w & ~(w-1);
421
422 unaligned = 0;
423 for (i=0; i<4; i++){
424 unaligned |= picture.linesize[i] % stride_align[i];
425 }
426 } while (unaligned);
427
428 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
429 if (tmpsize < 0)
430 return -1;
431
432 for (i=0; i<3 && picture.data[i+1]; i++)
433 size[i] = picture.data[i+1] - picture.data[i];
434 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
435
436 memset(buf->base, 0, sizeof(buf->base));
437 memset(buf->data, 0, sizeof(buf->data));
438
439 for(i=0; i<4 && size[i]; i++){
440 const int h_shift= i==0 ? 0 : h_chroma_shift;
441 const int v_shift= i==0 ? 0 : v_chroma_shift;
442
443 buf->linesize[i]= picture.linesize[i];
444
445 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
446 if(buf->base[i]==NULL) return -1;
447 memset(buf->base[i], 128, size[i]);
448
449 // no edge if EDGE EMU or not planar YUV
450 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
451 buf->data[i] = buf->base[i];
452 else
453 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
454 }
455 for (; i < AV_NUM_DATA_POINTERS; i++) {
456 buf->base[i] = buf->data[i] = NULL;
457 buf->linesize[i] = 0;
458 }
459 if(size[1] && !size[2])
460 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
461 buf->width = s->width;
462 buf->height = s->height;
463 buf->pix_fmt= s->pix_fmt;
464 }
465 pic->type= FF_BUFFER_TYPE_INTERNAL;
466
467 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
468 pic->base[i]= buf->base[i];
469 pic->data[i]= buf->data[i];
470 pic->linesize[i]= buf->linesize[i];
471 }
472 pic->extended_data = pic->data;
473 avci->buffer_count++;
474
475 if(s->pkt) pic->pkt_pts= s->pkt->pts;
476 else pic->pkt_pts= AV_NOPTS_VALUE;
477 pic->reordered_opaque= s->reordered_opaque;
478
479 if(s->debug&FF_DEBUG_BUFFERS)
480 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
481 "buffers used\n", pic, avci->buffer_count);
482
483 return 0;
484 }
485
486 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
487 {
488 switch (avctx->codec_type) {
489 case AVMEDIA_TYPE_VIDEO:
490 return video_get_buffer(avctx, frame);
491 case AVMEDIA_TYPE_AUDIO:
492 return audio_get_buffer(avctx, frame);
493 default:
494 return -1;
495 }
496 }
497
498 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
499 int i;
500 InternalBuffer *buf, *last;
501 AVCodecInternal *avci = s->internal;
502
503 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
504
505 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
506 assert(avci->buffer_count);
507
508 if (avci->buffer) {
509 buf = NULL; /* avoids warning */
510 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
511 buf = &avci->buffer[i];
512 if (buf->data[0] == pic->data[0])
513 break;
514 }
515 assert(i < avci->buffer_count);
516 avci->buffer_count--;
517 last = &avci->buffer[avci->buffer_count];
518
519 if (buf != last)
520 FFSWAP(InternalBuffer, *buf, *last);
521 }
522
523 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
524 pic->data[i]=NULL;
525 // pic->base[i]=NULL;
526 }
527 //printf("R%X\n", pic->opaque);
528
529 if(s->debug&FF_DEBUG_BUFFERS)
530 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
531 "buffers used\n", pic, avci->buffer_count);
532 }
533
534 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
535 AVFrame temp_pic;
536 int i;
537
538 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
539
540 /* If no picture return a new buffer */
541 if(pic->data[0] == NULL) {
542 /* We will copy from buffer, so must be readable */
543 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
544 return s->get_buffer(s, pic);
545 }
546
547 /* If internal buffer type return the same buffer */
548 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
549 if(s->pkt) pic->pkt_pts= s->pkt->pts;
550 else pic->pkt_pts= AV_NOPTS_VALUE;
551 pic->reordered_opaque= s->reordered_opaque;
552 return 0;
553 }
554
555 /*
556 * Not internal type and reget_buffer not overridden, emulate cr buffer
557 */
558 temp_pic = *pic;
559 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
560 pic->data[i] = pic->base[i] = NULL;
561 pic->opaque = NULL;
562 /* Allocate new frame */
563 if (s->get_buffer(s, pic))
564 return -1;
565 /* Copy image data from old buffer to new buffer */
566 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
567 s->height);
568 s->release_buffer(s, &temp_pic); // Release old frame
569 return 0;
570 }
571
572 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
573 int i;
574
575 for(i=0; i<count; i++){
576 int r= func(c, (char*)arg + i*size);
577 if(ret) ret[i]= r;
578 }
579 return 0;
580 }
581
582 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
583 int i;
584
585 for(i=0; i<count; i++){
586 int r= func(c, arg, i, 0);
587 if(ret) ret[i]= r;
588 }
589 return 0;
590 }
591
592 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
593 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
594 ++fmt;
595 return fmt[0];
596 }
597
598 void avcodec_get_frame_defaults(AVFrame *pic){
599 memset(pic, 0, sizeof(AVFrame));
600
601 pic->pts= AV_NOPTS_VALUE;
602 pic->key_frame= 1;
603 pic->sample_aspect_ratio = (AVRational){0, 1};
604 pic->format = -1; /* unknown */
605 }
606
607 AVFrame *avcodec_alloc_frame(void){
608 AVFrame *pic= av_malloc(sizeof(AVFrame));
609
610 if(pic==NULL) return NULL;
611
612 avcodec_get_frame_defaults(pic);
613
614 return pic;
615 }
616
617 #if FF_API_AVCODEC_OPEN
618 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
619 {
620 return avcodec_open2(avctx, codec, NULL);
621 }
622 #endif
623
624 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
625 {
626 int ret = 0;
627 AVDictionary *tmp = NULL;
628
629 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
630 return AVERROR(EINVAL);
631
632 if (options)
633 av_dict_copy(&tmp, *options, 0);
634
635 /* If there is a user-supplied mutex locking routine, call it. */
636 if (ff_lockmgr_cb) {
637 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
638 return -1;
639 }
640
641 entangled_thread_counter++;
642 if(entangled_thread_counter != 1){
643 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
644 ret = -1;
645 goto end;
646 }
647
648 if(avctx->codec || !codec) {
649 ret = AVERROR(EINVAL);
650 goto end;
651 }
652
653 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
654 if (!avctx->internal) {
655 ret = AVERROR(ENOMEM);
656 goto end;
657 }
658
659 if (codec->priv_data_size > 0) {
660 if(!avctx->priv_data){
661 avctx->priv_data = av_mallocz(codec->priv_data_size);
662 if (!avctx->priv_data) {
663 ret = AVERROR(ENOMEM);
664 goto end;
665 }
666 if (codec->priv_class) {
667 *(AVClass**)avctx->priv_data= codec->priv_class;
668 av_opt_set_defaults(avctx->priv_data);
669 }
670 }
671 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
672 goto free_and_end;
673 } else {
674 avctx->priv_data = NULL;
675 }
676 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
677 goto free_and_end;
678
679 if(avctx->coded_width && avctx->coded_height)
680 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
681 else if(avctx->width && avctx->height)
682 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
683
684 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
685 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
686 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
687 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
688 avcodec_set_dimensions(avctx, 0, 0);
689 }
690
691 /* if the decoder init function was already called previously,
692 free the already allocated subtitle_header before overwriting it */
693 if (codec->decode)
694 av_freep(&avctx->subtitle_header);
695
696 #define SANE_NB_CHANNELS 128U
697 if (avctx->channels > SANE_NB_CHANNELS) {
698 ret = AVERROR(EINVAL);
699 goto free_and_end;
700 }
701
702 avctx->codec = codec;
703 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
704 avctx->codec_id == CODEC_ID_NONE) {
705 avctx->codec_type = codec->type;
706 avctx->codec_id = codec->id;
707 }
708 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
709 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
710 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
711 ret = AVERROR(EINVAL);
712 goto free_and_end;
713 }
714 avctx->frame_number = 0;
715 #if FF_API_ER
716
717 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
718 avctx->error_recognition, avctx->err_recognition);
719 /* FF_ER_CAREFUL (==1) implies AV_EF_CRCCHECK (== 1<<1 - 1),
720 FF_ER_COMPLIANT (==2) implies AV_EF_{CRCCHECK,BITSTREAM} (== 1<<2 - 1), et cetera} */
721 avctx->err_recognition |= (1<<(avctx->error_recognition-(avctx->error_recognition>=FF_ER_VERY_AGGRESSIVE))) - 1;
722 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
723 avctx->error_recognition, avctx->err_recognition);
724 #endif
725
726 if (HAVE_THREADS && !avctx->thread_opaque) {
727 ret = ff_thread_init(avctx);
728 if (ret < 0) {
729 goto free_and_end;
730 }
731 }
732 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
733 avctx->thread_count = 1;
734
735 if (avctx->codec->max_lowres < avctx->lowres) {
736 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
737 avctx->codec->max_lowres);
738 ret = AVERROR(EINVAL);
739 goto free_and_end;
740 }
741 if (avctx->codec->encode) {
742 int i;
743 if (avctx->codec->sample_fmts) {
744 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
745 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
746 break;
747 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
748 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
749 ret = AVERROR(EINVAL);
750 goto free_and_end;
751 }
752 }
753 if (avctx->codec->supported_samplerates) {
754 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
755 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
756 break;
757 if (avctx->codec->supported_samplerates[i] == 0) {
758 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
759 ret = AVERROR(EINVAL);
760 goto free_and_end;
761 }
762 }
763 if (avctx->codec->channel_layouts) {
764 if (!avctx->channel_layout) {
765 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
766 } else {
767 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
768 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
769 break;
770 if (avctx->codec->channel_layouts[i] == 0) {
771 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
772 ret = AVERROR(EINVAL);
773 goto free_and_end;
774 }
775 }
776 }
777 if (avctx->channel_layout && avctx->channels) {
778 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
779 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
780 ret = AVERROR(EINVAL);
781 goto free_and_end;
782 }
783 } else if (avctx->channel_layout) {
784 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
785 }
786 }
787
788 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
789 ret = avctx->codec->init(avctx);
790 if (ret < 0) {
791 goto free_and_end;
792 }
793 }
794 end:
795 entangled_thread_counter--;
796
797 /* Release any user-supplied mutex. */
798 if (ff_lockmgr_cb) {
799 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
800 }
801 if (options) {
802 av_dict_free(options);
803 *options = tmp;
804 }
805
806 return ret;
807 free_and_end:
808 av_dict_free(&tmp);
809 av_freep(&avctx->priv_data);
810 av_freep(&avctx->internal);
811 avctx->codec= NULL;
812 goto end;
813 }
814
815 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
816 const short *samples)
817 {
818 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
819 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
820 return -1;
821 }
822 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
823 int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
824 avctx->frame_number++;
825 return ret;
826 }else
827 return 0;
828 }
829
830 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
831 const AVFrame *pict)
832 {
833 if(buf_size < FF_MIN_BUFFER_SIZE){
834 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
835 return -1;
836 }
837 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
838 return -1;
839 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
840 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
841 avctx->frame_number++;
842 emms_c(); //needed to avoid an emms_c() call before every return;
843
844 return ret;
845 }else
846 return 0;
847 }
848
849 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
850 const AVSubtitle *sub)
851 {
852 int ret;
853 if(sub->start_display_time) {
854 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
855 return -1;
856 }
857 if(sub->num_rects == 0 || !sub->rects)
858 return -1;
859 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
860 avctx->frame_number++;
861 return ret;
862 }
863
864 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
865 {
866 int size = 0;
867 const uint8_t *data;
868 uint32_t flags;
869
870 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
871 return;
872
873 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
874 if (!data || size < 4)
875 return;
876 flags = bytestream_get_le32(&data);
877 size -= 4;
878 if (size < 4) /* Required for any of the changes */
879 return;
880 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
881 avctx->channels = bytestream_get_le32(&data);
882 size -= 4;
883 }
884 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
885 if (size < 8)
886 return;
887 avctx->channel_layout = bytestream_get_le64(&data);
888 size -= 8;
889 }
890 if (size < 4)
891 return;
892 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
893 avctx->sample_rate = bytestream_get_le32(&data);
894 size -= 4;
895 }
896 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
897 if (size < 8)
898 return;
899 avctx->width = bytestream_get_le32(&data);
900 avctx->height = bytestream_get_le32(&data);
901 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
902 size -= 8;
903 }
904 }
905
906 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
907 int *got_picture_ptr,
908 AVPacket *avpkt)
909 {
910 int ret;
911
912 *got_picture_ptr= 0;
913 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
914 return -1;
915
916 avctx->pkt = avpkt;
917 apply_param_change(avctx, avpkt);
918
919 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
920 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
921 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
922 avpkt);
923 else {
924 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
925 avpkt);
926 picture->pkt_dts= avpkt->dts;
927 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
928 picture->width = avctx->width;
929 picture->height = avctx->height;
930 picture->format = avctx->pix_fmt;
931 }
932
933 emms_c(); //needed to avoid an emms_c() call before every return;
934
935 if (*got_picture_ptr)
936 avctx->frame_number++;
937 }else
938 ret= 0;
939
940 return ret;
941 }
942
943 #if FF_API_OLD_DECODE_AUDIO
944 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
945 int *frame_size_ptr,
946 AVPacket *avpkt)
947 {
948 AVFrame frame;
949 int ret, got_frame = 0;
950
951 if (avctx->get_buffer != avcodec_default_get_buffer) {
952 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
953 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
954 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
955 "avcodec_decode_audio4()\n");
956 avctx->get_buffer = avcodec_default_get_buffer;
957 }
958
959 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
960
961 if (ret >= 0 && got_frame) {
962 int ch, plane_size;
963 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
964 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
965 frame.nb_samples,
966 avctx->sample_fmt, 1);
967 if (*frame_size_ptr < data_size) {
968 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
969 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
970 return AVERROR(EINVAL);
971 }
972
973 memcpy(samples, frame.extended_data[0], plane_size);
974
975 if (planar && avctx->channels > 1) {
976 uint8_t *out = ((uint8_t *)samples) + plane_size;
977 for (ch = 1; ch < avctx->channels; ch++) {
978 memcpy(out, frame.extended_data[ch], plane_size);
979 out += plane_size;
980 }
981 }
982 *frame_size_ptr = data_size;
983 } else {
984 *frame_size_ptr = 0;
985 }
986 return ret;
987 }
988 #endif
989
990 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
991 AVFrame *frame,
992 int *got_frame_ptr,
993 AVPacket *avpkt)
994 {
995 int ret = 0;
996
997 *got_frame_ptr = 0;
998
999 avctx->pkt = avpkt;
1000
1001 if (!avpkt->data && avpkt->size) {
1002 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1003 return AVERROR(EINVAL);
1004 }
1005
1006 apply_param_change(avctx, avpkt);
1007
1008 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1009 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1010 if (ret >= 0 && *got_frame_ptr) {
1011 avctx->frame_number++;
1012 frame->pkt_dts = avpkt->dts;
1013 if (frame->format == AV_SAMPLE_FMT_NONE)
1014 frame->format = avctx->sample_fmt;
1015 }
1016 }
1017 return ret;
1018 }
1019
1020 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1021 int *got_sub_ptr,
1022 AVPacket *avpkt)
1023 {
1024 int ret;
1025
1026 avctx->pkt = avpkt;
1027 *got_sub_ptr = 0;
1028 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1029 if (*got_sub_ptr)
1030 avctx->frame_number++;
1031 return ret;
1032 }
1033
1034 void avsubtitle_free(AVSubtitle *sub)
1035 {
1036 int i;
1037
1038 for (i = 0; i < sub->num_rects; i++)
1039 {
1040 av_freep(&sub->rects[i]->pict.data[0]);
1041 av_freep(&sub->rects[i]->pict.data[1]);
1042 av_freep(&sub->rects[i]->pict.data[2]);
1043 av_freep(&sub->rects[i]->pict.data[3]);
1044 av_freep(&sub->rects[i]->text);
1045 av_freep(&sub->rects[i]->ass);
1046 av_freep(&sub->rects[i]);
1047 }
1048
1049 av_freep(&sub->rects);
1050
1051 memset(sub, 0, sizeof(AVSubtitle));
1052 }
1053
1054 av_cold int avcodec_close(AVCodecContext *avctx)
1055 {
1056 /* If there is a user-supplied mutex locking routine, call it. */
1057 if (ff_lockmgr_cb) {
1058 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1059 return -1;
1060 }
1061
1062 entangled_thread_counter++;
1063 if(entangled_thread_counter != 1){
1064 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1065 entangled_thread_counter--;
1066 return -1;
1067 }
1068
1069 if (HAVE_THREADS && avctx->thread_opaque)
1070 ff_thread_free(avctx);
1071 if (avctx->codec && avctx->codec->close)
1072 avctx->codec->close(avctx);
1073 avcodec_default_free_buffers(avctx);
1074 avctx->coded_frame = NULL;
1075 av_freep(&avctx->internal);
1076 if (avctx->codec && avctx->codec->priv_class)
1077 av_opt_free(avctx->priv_data);
1078 av_opt_free(avctx);
1079 av_freep(&avctx->priv_data);
1080 if(avctx->codec && avctx->codec->encode)
1081 av_freep(&avctx->extradata);
1082 avctx->codec = NULL;
1083 avctx->active_thread_type = 0;
1084 entangled_thread_counter--;
1085
1086 /* Release any user-supplied mutex. */
1087 if (ff_lockmgr_cb) {
1088 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1089 }
1090 return 0;
1091 }
1092
1093 AVCodec *avcodec_find_encoder(enum CodecID id)
1094 {
1095 AVCodec *p, *experimental=NULL;
1096 p = first_avcodec;
1097 while (p) {
1098 if (p->encode != NULL && p->id == id) {
1099 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1100 experimental = p;
1101 } else
1102 return p;
1103 }
1104 p = p->next;
1105 }
1106 return experimental;
1107 }
1108
1109 AVCodec *avcodec_find_encoder_by_name(const char *name)
1110 {
1111 AVCodec *p;
1112 if (!name)
1113 return NULL;
1114 p = first_avcodec;
1115 while (p) {
1116 if (p->encode != NULL && strcmp(name,p->name) == 0)
1117 return p;
1118 p = p->next;
1119 }
1120 return NULL;
1121 }
1122
1123 AVCodec *avcodec_find_decoder(enum CodecID id)
1124 {
1125 AVCodec *p;
1126 p = first_avcodec;
1127 while (p) {
1128 if (p->decode != NULL && p->id == id)
1129 return p;
1130 p = p->next;
1131 }
1132 return NULL;
1133 }
1134
1135 AVCodec *avcodec_find_decoder_by_name(const char *name)
1136 {
1137 AVCodec *p;
1138 if (!name)
1139 return NULL;
1140 p = first_avcodec;
1141 while (p) {
1142 if (p->decode != NULL && strcmp(name,p->name) == 0)
1143 return p;
1144 p = p->next;
1145 }
1146 return NULL;
1147 }
1148
1149 static int get_bit_rate(AVCodecContext *ctx)
1150 {
1151 int bit_rate;
1152 int bits_per_sample;
1153
1154 switch(ctx->codec_type) {
1155 case AVMEDIA_TYPE_VIDEO:
1156 case AVMEDIA_TYPE_DATA:
1157 case AVMEDIA_TYPE_SUBTITLE:
1158 case AVMEDIA_TYPE_ATTACHMENT:
1159 bit_rate = ctx->bit_rate;
1160 break;
1161 case AVMEDIA_TYPE_AUDIO:
1162 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1163 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1164 break;
1165 default:
1166 bit_rate = 0;
1167 break;
1168 }
1169 return bit_rate;
1170 }
1171
1172 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1173 {
1174 int i, len, ret = 0;
1175
1176 for (i = 0; i < 4; i++) {
1177 len = snprintf(buf, buf_size,
1178 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1179 buf += len;
1180 buf_size = buf_size > len ? buf_size - len : 0;
1181 ret += len;
1182 codec_tag>>=8;
1183 }
1184 return ret;
1185 }
1186
1187 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1188 {
1189 const char *codec_name;
1190 const char *profile = NULL;
1191 AVCodec *p;
1192 char buf1[32];
1193 int bitrate;
1194 AVRational display_aspect_ratio;
1195
1196 if (encode)
1197 p = avcodec_find_encoder(enc->codec_id);
1198 else
1199 p = avcodec_find_decoder(enc->codec_id);
1200
1201 if (p) {
1202 codec_name = p->name;
1203 profile = av_get_profile_name(p, enc->profile);
1204 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
1205 /* fake mpeg2 transport stream codec (currently not
1206 registered) */
1207 codec_name = "mpeg2ts";
1208 } else if (enc->codec_name[0] != '\0') {
1209 codec_name = enc->codec_name;
1210 } else {
1211 /* output avi tags */
1212 char tag_buf[32];
1213 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1214 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1215 codec_name = buf1;
1216 }
1217
1218 switch(enc->codec_type) {
1219 case AVMEDIA_TYPE_VIDEO:
1220 snprintf(buf, buf_size,
1221 "Video: %s%s",
1222 codec_name, enc->mb_decision ? " (hq)" : "");
1223 if (profile)
1224 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1225 " (%s)", profile);
1226 if (enc->pix_fmt != PIX_FMT_NONE) {
1227 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1228 ", %s",
1229 av_get_pix_fmt_name(enc->pix_fmt));
1230 }
1231 if (enc->width) {
1232 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1233 ", %dx%d",
1234 enc->width, enc->height);
1235 if (enc->sample_aspect_ratio.num) {
1236 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1237 enc->width*enc->sample_aspect_ratio.num,
1238 enc->height*enc->sample_aspect_ratio.den,
1239 1024*1024);
1240 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1241 " [PAR %d:%d DAR %d:%d]",
1242 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1243 display_aspect_ratio.num, display_aspect_ratio.den);
1244 }
1245 if(av_log_get_level() >= AV_LOG_DEBUG){
1246 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1247 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1248 ", %d/%d",
1249 enc->time_base.num/g, enc->time_base.den/g);
1250 }
1251 }
1252 if (encode) {
1253 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1254 ", q=%d-%d", enc->qmin, enc->qmax);
1255 }
1256 break;
1257 case AVMEDIA_TYPE_AUDIO:
1258 snprintf(buf, buf_size,
1259 "Audio: %s",
1260 codec_name);
1261 if (profile)
1262 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1263 " (%s)", profile);
1264 if (enc->sample_rate) {
1265 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1266 ", %d Hz", enc->sample_rate);
1267 }
1268 av_strlcat(buf, ", ", buf_size);
1269 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1270 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1271 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1272 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1273 }
1274 break;
1275 case AVMEDIA_TYPE_DATA:
1276 snprintf(buf, buf_size, "Data: %s", codec_name);
1277 break;
1278 case AVMEDIA_TYPE_SUBTITLE:
1279 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1280 break;
1281 case AVMEDIA_TYPE_ATTACHMENT:
1282 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1283 break;
1284 default:
1285 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1286 return;
1287 }
1288 if (encode) {
1289 if (enc->flags & CODEC_FLAG_PASS1)
1290 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1291 ", pass 1");
1292 if (enc->flags & CODEC_FLAG_PASS2)
1293 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1294 ", pass 2");
1295 }
1296 bitrate = get_bit_rate(enc);
1297 if (bitrate != 0) {
1298 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1299 ", %d kb/s", bitrate / 1000);
1300 }
1301 }
1302
1303 const char *av_get_profile_name(const AVCodec *codec, int profile)
1304 {
1305 const AVProfile *p;
1306 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1307 return NULL;
1308
1309 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1310 if (p->profile == profile)
1311 return p->name;
1312
1313 return NULL;
1314 }
1315
1316 unsigned avcodec_version( void )
1317 {
1318 return LIBAVCODEC_VERSION_INT;
1319 }
1320
1321 const char *avcodec_configuration(void)
1322 {
1323 return LIBAV_CONFIGURATION;
1324 }
1325
1326 const char *avcodec_license(void)
1327 {
1328 #define LICENSE_PREFIX "libavcodec license: "
1329 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1330 }
1331
1332 void avcodec_flush_buffers(AVCodecContext *avctx)
1333 {
1334 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1335 ff_thread_flush(avctx);
1336 else if(avctx->codec->flush)
1337 avctx->codec->flush(avctx);
1338 }
1339
1340 static void video_free_buffers(AVCodecContext *s)
1341 {
1342 AVCodecInternal *avci = s->internal;
1343 int i, j;
1344
1345 if (!avci->buffer)
1346 return;
1347
1348 if (avci->buffer_count)
1349 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1350 avci->buffer_count);
1351 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1352 InternalBuffer *buf = &avci->buffer[i];
1353 for(j=0; j<4; j++){
1354 av_freep(&buf->base[j]);
1355 buf->data[j]= NULL;
1356 }
1357 }
1358 av_freep(&avci->buffer);
1359
1360 avci->buffer_count=0;
1361 }
1362
1363 static void audio_free_buffers(AVCodecContext *avctx)
1364 {
1365 AVCodecInternal *avci = avctx->internal;
1366 InternalBuffer *buf;
1367
1368 if (!avci->buffer)
1369 return;
1370 buf = avci->buffer;
1371
1372 if (buf->extended_data) {
1373 av_free(buf->extended_data[0]);
1374 if (buf->extended_data != buf->data)
1375 av_free(buf->extended_data);
1376 }
1377 av_freep(&avci->buffer);
1378 }
1379
1380 void avcodec_default_free_buffers(AVCodecContext *avctx)
1381 {
1382 switch (avctx->codec_type) {
1383 case AVMEDIA_TYPE_VIDEO:
1384 video_free_buffers(avctx);
1385 break;
1386 case AVMEDIA_TYPE_AUDIO:
1387 audio_free_buffers(avctx);
1388 break;
1389 default:
1390 break;
1391 }
1392 }
1393
1394 #if FF_API_OLD_FF_PICT_TYPES
1395 char av_get_pict_type_char(int pict_type){
1396 return av_get_picture_type_char(pict_type);
1397 }
1398 #endif
1399
1400 int av_get_bits_per_sample(enum CodecID codec_id){
1401 switch(codec_id){
1402 case CODEC_ID_ADPCM_SBPRO_2:
1403 return 2;
1404 case CODEC_ID_ADPCM_SBPRO_3:
1405 return 3;
1406 case CODEC_ID_ADPCM_SBPRO_4:
1407 case CODEC_ID_ADPCM_CT:
1408 case CODEC_ID_ADPCM_IMA_WAV:
1409 case CODEC_ID_ADPCM_IMA_QT:
1410 case CODEC_ID_ADPCM_SWF:
1411 case CODEC_ID_ADPCM_MS:
1412 case CODEC_ID_ADPCM_YAMAHA:
1413 case CODEC_ID_ADPCM_G722:
1414 return 4;
1415 case CODEC_ID_PCM_ALAW:
1416 case CODEC_ID_PCM_MULAW:
1417 case CODEC_ID_PCM_S8:
1418 case CODEC_ID_PCM_U8:
1419 case CODEC_ID_PCM_ZORK:
1420 return 8;
1421 case CODEC_ID_PCM_S16BE:
1422 case CODEC_ID_PCM_S16LE:
1423 case CODEC_ID_PCM_S16LE_PLANAR:
1424 case CODEC_ID_PCM_U16BE:
1425 case CODEC_ID_PCM_U16LE:
1426 return 16;
1427 case CODEC_ID_PCM_S24DAUD:
1428 case CODEC_ID_PCM_S24BE:
1429 case CODEC_ID_PCM_S24LE:
1430 case CODEC_ID_PCM_U24BE:
1431 case CODEC_ID_PCM_U24LE:
1432 return 24;
1433 case CODEC_ID_PCM_S32BE:
1434 case CODEC_ID_PCM_S32LE:
1435 case CODEC_ID_PCM_U32BE:
1436 case CODEC_ID_PCM_U32LE:
1437 case CODEC_ID_PCM_F32BE:
1438 case CODEC_ID_PCM_F32LE:
1439 return 32;
1440 case CODEC_ID_PCM_F64BE:
1441 case CODEC_ID_PCM_F64LE:
1442 return 64;
1443 default:
1444 return 0;
1445 }
1446 }
1447
1448 #if FF_API_OLD_SAMPLE_FMT
1449 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1450 return av_get_bytes_per_sample(sample_fmt) << 3;
1451 }
1452 #endif
1453
1454 #if !HAVE_THREADS
1455 int ff_thread_init(AVCodecContext *s){
1456 return -1;
1457 }
1458 #endif
1459
1460 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1461 {
1462 unsigned int n = 0;
1463
1464 while(v >= 0xff) {
1465 *s++ = 0xff;
1466 v -= 0xff;
1467 n++;
1468 }
1469 *s = v;
1470 n++;
1471 return n;
1472 }
1473
1474 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1475 int i;
1476 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1477 return i;
1478 }
1479
1480 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1481 {
1482 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1483 "version to the newest one from Git. If the problem still "
1484 "occurs, it means that your file has a feature which has not "
1485 "been implemented.\n", feature);
1486 if(want_sample)
1487 av_log_ask_for_sample(avc, NULL);
1488 }
1489
1490 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1491 {
1492 va_list argument_list;
1493
1494 va_start(argument_list, msg);
1495
1496 if (msg)
1497 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1498 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1499 "of this file to ftp://upload.libav.org/incoming/ "
1500 "and contact the libav-devel mailing list.\n");
1501
1502 va_end(argument_list);
1503 }
1504
1505 static AVHWAccel *first_hwaccel = NULL;
1506
1507 void av_register_hwaccel(AVHWAccel *hwaccel)
1508 {
1509 AVHWAccel **p = &first_hwaccel;
1510 while (*p)
1511 p = &(*p)->next;
1512 *p = hwaccel;
1513 hwaccel->next = NULL;
1514 }
1515
1516 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1517 {
1518 return hwaccel ? hwaccel->next : first_hwaccel;
1519 }
1520
1521 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1522 {
1523 AVHWAccel *hwaccel=NULL;
1524
1525 while((hwaccel= av_hwaccel_next(hwaccel))){
1526 if ( hwaccel->id == codec_id
1527 && hwaccel->pix_fmt == pix_fmt)
1528 return hwaccel;
1529 }
1530 return NULL;
1531 }
1532
1533 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1534 {
1535 if (ff_lockmgr_cb) {
1536 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1537 return -1;
1538 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1539 return -1;
1540 }
1541
1542 ff_lockmgr_cb = cb;
1543
1544 if (ff_lockmgr_cb) {
1545 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1546 return -1;
1547 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1548 return -1;
1549 }
1550 return 0;
1551 }
1552
1553 int avpriv_lock_avformat(void)
1554 {
1555 if (ff_lockmgr_cb) {
1556 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1557 return -1;
1558 }
1559 return 0;
1560 }
1561
1562 int avpriv_unlock_avformat(void)
1563 {
1564 if (ff_lockmgr_cb) {
1565 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1566 return -1;
1567 }
1568 return 0;
1569 }
1570
1571 unsigned int avpriv_toupper4(unsigned int x)
1572 {
1573 return toupper( x &0xFF)
1574 + (toupper((x>>8 )&0xFF)<<8 )
1575 + (toupper((x>>16)&0xFF)<<16)
1576 + (toupper((x>>24)&0xFF)<<24);
1577 }
1578
1579 #if !HAVE_THREADS
1580
1581 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1582 {
1583 f->owner = avctx;
1584 return avctx->get_buffer(avctx, f);
1585 }
1586
1587 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1588 {
1589 f->owner->release_buffer(f->owner, f);
1590 }
1591
1592 void ff_thread_finish_setup(AVCodecContext *avctx)
1593 {
1594 }
1595
1596 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1597 {
1598 }
1599
1600 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1601 {
1602 }
1603
1604 #endif
1605
1606 #if FF_API_THREAD_INIT
1607 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1608 {
1609 s->thread_count = thread_count;
1610 return ff_thread_init(s);
1611 }
1612 #endif
1613
1614 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1615 {
1616 if (codec_id <= CODEC_ID_NONE)
1617 return AVMEDIA_TYPE_UNKNOWN;
1618 else if (codec_id < CODEC_ID_FIRST_AUDIO)
1619 return AVMEDIA_TYPE_VIDEO;
1620 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1621 return AVMEDIA_TYPE_AUDIO;
1622 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1623 return AVMEDIA_TYPE_SUBTITLE;
1624
1625 return AVMEDIA_TYPE_UNKNOWN;
1626 }