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