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