]> git.sesse.net Git - ffmpeg/blob - libavcodec/utils.c
b4c294e0de0a20f900ed0d3ef12b5f52bbe5b58b
[ffmpeg] / 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 "config.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/channel_layout.h"
32 #include "libavutil/crc.h"
33 #include "libavutil/frame.h"
34 #include "libavutil/mathematics.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/imgutils.h"
37 #include "libavutil/samplefmt.h"
38 #include "libavutil/dict.h"
39 #include "avcodec.h"
40 #include "dsputil.h"
41 #include "libavutil/opt.h"
42 #include "thread.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     /* we could set this to the unmodified min_size but this is safer
64      * if the user lost the ptr and uses NULL now
65      */
66     if (!ptr)
67         min_size = 0;
68
69     *size = min_size;
70
71     return ptr;
72 }
73
74 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
75 {
76     void **p = ptr;
77     if (min_size < *size)
78         return;
79     min_size = FFMAX(17 * min_size / 16 + 32, min_size);
80     av_free(*p);
81     *p = av_malloc(min_size);
82     if (!*p)
83         min_size = 0;
84     *size = min_size;
85 }
86
87 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
88 {
89     void **p = ptr;
90     if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
91         av_freep(p);
92         *size = 0;
93         return;
94     }
95     av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
96     if (*size)
97         memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
98 }
99
100 /* encoder management */
101 static AVCodec *first_avcodec = NULL;
102
103 AVCodec *av_codec_next(const AVCodec *c)
104 {
105     if (c)
106         return c->next;
107     else
108         return first_avcodec;
109 }
110
111 static void avcodec_init(void)
112 {
113     static int initialized = 0;
114
115     if (initialized != 0)
116         return;
117     initialized = 1;
118
119     ff_dsputil_static_init();
120 }
121
122 int av_codec_is_encoder(const AVCodec *codec)
123 {
124     return codec && (codec->encode_sub || codec->encode2);
125 }
126
127 int av_codec_is_decoder(const AVCodec *codec)
128 {
129     return codec && codec->decode;
130 }
131
132 void avcodec_register(AVCodec *codec)
133 {
134     AVCodec **p;
135     avcodec_init();
136     p = &first_avcodec;
137     while (*p != NULL)
138         p = &(*p)->next;
139     *p          = codec;
140     codec->next = NULL;
141
142     if (codec->init_static_data)
143         codec->init_static_data(codec);
144 }
145
146 unsigned avcodec_get_edge_width(void)
147 {
148     return EDGE_WIDTH;
149 }
150
151 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
152 {
153     s->coded_width  = width;
154     s->coded_height = height;
155     s->width        = width;
156     s->height       = height;
157 }
158
159 #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
160 #   define STRIDE_ALIGN 16
161 #else
162 #   define STRIDE_ALIGN 8
163 #endif
164
165 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
166                                int linesize_align[AV_NUM_DATA_POINTERS])
167 {
168     int i;
169     int w_align = 1;
170     int h_align = 1;
171
172     switch (s->pix_fmt) {
173     case AV_PIX_FMT_YUV420P:
174     case AV_PIX_FMT_YUYV422:
175     case AV_PIX_FMT_UYVY422:
176     case AV_PIX_FMT_YUV422P:
177     case AV_PIX_FMT_YUV440P:
178     case AV_PIX_FMT_YUV444P:
179     case AV_PIX_FMT_GBRP:
180     case AV_PIX_FMT_GRAY8:
181     case AV_PIX_FMT_GRAY16BE:
182     case AV_PIX_FMT_GRAY16LE:
183     case AV_PIX_FMT_YUVJ420P:
184     case AV_PIX_FMT_YUVJ422P:
185     case AV_PIX_FMT_YUVJ440P:
186     case AV_PIX_FMT_YUVJ444P:
187     case AV_PIX_FMT_YUVA420P:
188     case AV_PIX_FMT_YUVA422P:
189     case AV_PIX_FMT_YUVA444P:
190     case AV_PIX_FMT_YUV420P9LE:
191     case AV_PIX_FMT_YUV420P9BE:
192     case AV_PIX_FMT_YUV420P10LE:
193     case AV_PIX_FMT_YUV420P10BE:
194     case AV_PIX_FMT_YUV422P9LE:
195     case AV_PIX_FMT_YUV422P9BE:
196     case AV_PIX_FMT_YUV422P10LE:
197     case AV_PIX_FMT_YUV422P10BE:
198     case AV_PIX_FMT_YUV444P9LE:
199     case AV_PIX_FMT_YUV444P9BE:
200     case AV_PIX_FMT_YUV444P10LE:
201     case AV_PIX_FMT_YUV444P10BE:
202     case AV_PIX_FMT_GBRP9LE:
203     case AV_PIX_FMT_GBRP9BE:
204     case AV_PIX_FMT_GBRP10LE:
205     case AV_PIX_FMT_GBRP10BE:
206         w_align = 16; //FIXME assume 16 pixel per macroblock
207         h_align = 16 * 2; // interlaced needs 2 macroblocks height
208         break;
209     case AV_PIX_FMT_YUV411P:
210     case AV_PIX_FMT_UYYVYY411:
211         w_align = 32;
212         h_align = 8;
213         break;
214     case AV_PIX_FMT_YUV410P:
215         if (s->codec_id == AV_CODEC_ID_SVQ1) {
216             w_align = 64;
217             h_align = 64;
218         }
219     case AV_PIX_FMT_RGB555:
220         if (s->codec_id == AV_CODEC_ID_RPZA) {
221             w_align = 4;
222             h_align = 4;
223         }
224     case AV_PIX_FMT_PAL8:
225     case AV_PIX_FMT_BGR8:
226     case AV_PIX_FMT_RGB8:
227         if (s->codec_id == AV_CODEC_ID_SMC) {
228             w_align = 4;
229             h_align = 4;
230         }
231         break;
232     case AV_PIX_FMT_BGR24:
233         if ((s->codec_id == AV_CODEC_ID_MSZH) ||
234             (s->codec_id == AV_CODEC_ID_ZLIB)) {
235             w_align = 4;
236             h_align = 4;
237         }
238         break;
239     default:
240         w_align = 1;
241         h_align = 1;
242         break;
243     }
244
245     *width  = FFALIGN(*width, w_align);
246     *height = FFALIGN(*height, h_align);
247     if (s->codec_id == AV_CODEC_ID_H264)
248         // some of the optimized chroma MC reads one line too much
249         *height += 2;
250
251     for (i = 0; i < 4; i++)
252         linesize_align[i] = STRIDE_ALIGN;
253 }
254
255 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
256 {
257     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
258     int chroma_shift = desc->log2_chroma_w;
259     int linesize_align[AV_NUM_DATA_POINTERS];
260     int align;
261
262     avcodec_align_dimensions2(s, width, height, linesize_align);
263     align               = FFMAX(linesize_align[0], linesize_align[3]);
264     linesize_align[1] <<= chroma_shift;
265     linesize_align[2] <<= chroma_shift;
266     align               = FFMAX3(align, linesize_align[1], linesize_align[2]);
267     *width              = FFALIGN(*width, align);
268 }
269
270 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
271                              enum AVSampleFormat sample_fmt, const uint8_t *buf,
272                              int buf_size, int align)
273 {
274     int ch, planar, needed_size, ret = 0;
275
276     needed_size = av_samples_get_buffer_size(NULL, nb_channels,
277                                              frame->nb_samples, sample_fmt,
278                                              align);
279     if (buf_size < needed_size)
280         return AVERROR(EINVAL);
281
282     planar = av_sample_fmt_is_planar(sample_fmt);
283     if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
284         if (!(frame->extended_data = av_mallocz(nb_channels *
285                                                 sizeof(*frame->extended_data))))
286             return AVERROR(ENOMEM);
287     } else {
288         frame->extended_data = frame->data;
289     }
290
291     if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
292                                       buf, nb_channels, frame->nb_samples,
293                                       sample_fmt, align)) < 0) {
294         if (frame->extended_data != frame->data)
295             av_free(frame->extended_data);
296         return ret;
297     }
298     if (frame->extended_data != frame->data) {
299         for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
300             frame->data[ch] = frame->extended_data[ch];
301     }
302
303     return ret;
304 }
305
306 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
307 {
308     FramePool *pool = avctx->internal->pool;
309     int i, ret;
310
311     switch (avctx->codec_type) {
312     case AVMEDIA_TYPE_VIDEO: {
313         AVPicture picture;
314         int size[4] = { 0 };
315         int w = frame->width;
316         int h = frame->height;
317         int tmpsize, unaligned;
318
319         if (pool->format == frame->format &&
320             pool->width == frame->width && pool->height == frame->height)
321             return 0;
322
323         avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
324
325         if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
326             w += EDGE_WIDTH * 2;
327             h += EDGE_WIDTH * 2;
328         }
329
330         do {
331             // NOTE: do not align linesizes individually, this breaks e.g. assumptions
332             // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
333             av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
334             // increase alignment of w for next try (rhs gives the lowest bit set in w)
335             w += w & ~(w - 1);
336
337             unaligned = 0;
338             for (i = 0; i < 4; i++)
339                 unaligned |= picture.linesize[i] % pool->stride_align[i];
340         } while (unaligned);
341
342         tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
343                                          NULL, picture.linesize);
344         if (tmpsize < 0)
345             return -1;
346
347         for (i = 0; i < 3 && picture.data[i + 1]; i++)
348             size[i] = picture.data[i + 1] - picture.data[i];
349         size[i] = tmpsize - (picture.data[i] - picture.data[0]);
350
351         for (i = 0; i < 4; i++) {
352             av_buffer_pool_uninit(&pool->pools[i]);
353             pool->linesize[i] = picture.linesize[i];
354             if (size[i]) {
355                 pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
356                 if (!pool->pools[i]) {
357                     ret = AVERROR(ENOMEM);
358                     goto fail;
359                 }
360             }
361         }
362         pool->format = frame->format;
363         pool->width  = frame->width;
364         pool->height = frame->height;
365
366         break;
367         }
368     case AVMEDIA_TYPE_AUDIO: {
369         int ch     = av_get_channel_layout_nb_channels(frame->channel_layout);
370         int planar = av_sample_fmt_is_planar(frame->format);
371         int planes = planar ? ch : 1;
372
373         if (pool->format == frame->format && pool->planes == planes &&
374             pool->channels == ch && frame->nb_samples == pool->samples)
375             return 0;
376
377         av_buffer_pool_uninit(&pool->pools[0]);
378         ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
379                                          frame->nb_samples, frame->format, 0);
380         if (ret < 0)
381             goto fail;
382
383         pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
384         if (!pool->pools[0]) {
385             ret = AVERROR(ENOMEM);
386             goto fail;
387         }
388
389         pool->format     = frame->format;
390         pool->planes     = planes;
391         pool->channels   = ch;
392         pool->samples = frame->nb_samples;
393         break;
394         }
395     default: av_assert0(0);
396     }
397     return 0;
398 fail:
399     for (i = 0; i < 4; i++)
400         av_buffer_pool_uninit(&pool->pools[i]);
401     pool->format = -1;
402     pool->planes = pool->channels = pool->samples = 0;
403     pool->width  = pool->height = 0;
404     return ret;
405 }
406
407 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
408 {
409     FramePool *pool = avctx->internal->pool;
410     int planes = pool->planes;
411     int i;
412
413     frame->linesize[0] = pool->linesize[0];
414
415     if (planes > AV_NUM_DATA_POINTERS) {
416         frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
417         frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
418         frame->extended_buf  = av_mallocz(frame->nb_extended_buf *
419                                           sizeof(*frame->extended_buf));
420         if (!frame->extended_data || !frame->extended_buf) {
421             av_freep(&frame->extended_data);
422             av_freep(&frame->extended_buf);
423             return AVERROR(ENOMEM);
424         }
425     } else
426         frame->extended_data = frame->data;
427
428     for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
429         frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
430         if (!frame->buf[i])
431             goto fail;
432         frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
433     }
434     for (i = 0; i < frame->nb_extended_buf; i++) {
435         frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
436         if (!frame->extended_buf[i])
437             goto fail;
438         frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
439     }
440
441     if (avctx->debug & FF_DEBUG_BUFFERS)
442         av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
443
444     return 0;
445 fail:
446     av_frame_unref(frame);
447     return AVERROR(ENOMEM);
448 }
449
450 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
451 {
452     FramePool *pool = s->internal->pool;
453     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
454     int pixel_size = desc->comp[0].step_minus1 + 1;
455     int h_chroma_shift, v_chroma_shift;
456     int i;
457
458     if (pic->data[0] != NULL) {
459         av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
460         return -1;
461     }
462
463     memset(pic->data, 0, sizeof(pic->data));
464     pic->extended_data = pic->data;
465
466     av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
467
468     for (i = 0; i < 4 && pool->pools[i]; i++) {
469         const int h_shift = i == 0 ? 0 : h_chroma_shift;
470         const int v_shift = i == 0 ? 0 : v_chroma_shift;
471
472         pic->linesize[i] = pool->linesize[i];
473
474         pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
475         if (!pic->buf[i])
476             goto fail;
477
478         // no edge if EDGE EMU or not planar YUV
479         if ((s->flags & CODEC_FLAG_EMU_EDGE) || !pool->pools[2])
480             pic->data[i] = pic->buf[i]->data;
481         else {
482             pic->data[i] = pic->buf[i]->data +
483                 FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
484                         (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
485         }
486     }
487     for (; i < AV_NUM_DATA_POINTERS; i++) {
488         pic->data[i] = NULL;
489         pic->linesize[i] = 0;
490     }
491     if (pic->data[1] && !pic->data[2])
492         avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
493
494     if (s->debug & FF_DEBUG_BUFFERS)
495         av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
496
497     return 0;
498 fail:
499     av_frame_unref(pic);
500     return AVERROR(ENOMEM);
501 }
502
503 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
504 {
505     int ret;
506
507     if ((ret = update_frame_pool(avctx, frame)) < 0)
508         return ret;
509
510 #if FF_API_GET_BUFFER
511     frame->type = FF_BUFFER_TYPE_INTERNAL;
512 #endif
513
514     switch (avctx->codec_type) {
515     case AVMEDIA_TYPE_VIDEO:
516         return video_get_buffer(avctx, frame);
517     case AVMEDIA_TYPE_AUDIO:
518         return audio_get_buffer(avctx, frame);
519     default:
520         return -1;
521     }
522 }
523
524 #if FF_API_GET_BUFFER
525 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
526 {
527     return avcodec_default_get_buffer2(avctx, frame, 0);
528 }
529
530 typedef struct CompatReleaseBufPriv {
531     AVCodecContext avctx;
532     AVFrame frame;
533 } CompatReleaseBufPriv;
534
535 static void compat_free_buffer(void *opaque, uint8_t *data)
536 {
537     CompatReleaseBufPriv *priv = opaque;
538     if (priv->avctx.release_buffer)
539         priv->avctx.release_buffer(&priv->avctx, &priv->frame);
540     av_freep(&priv);
541 }
542
543 static void compat_release_buffer(void *opaque, uint8_t *data)
544 {
545     AVBufferRef *buf = opaque;
546     av_buffer_unref(&buf);
547 }
548 #endif
549
550 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
551 {
552     int ret;
553
554     switch (avctx->codec_type) {
555     case AVMEDIA_TYPE_VIDEO:
556         if (!frame->width)
557             frame->width               = avctx->width;
558         if (!frame->height)
559             frame->height              = avctx->height;
560         if (frame->format < 0)
561             frame->format              = avctx->pix_fmt;
562         if (!frame->sample_aspect_ratio.num)
563             frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
564
565         if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
566             return ret;
567         break;
568     case AVMEDIA_TYPE_AUDIO:
569         if (!frame->sample_rate)
570             frame->sample_rate    = avctx->sample_rate;
571         if (frame->format < 0)
572             frame->format         = avctx->sample_fmt;
573         if (!frame->channel_layout) {
574             if (avctx->channel_layout) {
575                  if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
576                      avctx->channels) {
577                      av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
578                             "configuration.\n");
579                      return AVERROR(EINVAL);
580                  }
581
582                 frame->channel_layout = avctx->channel_layout;
583             } else {
584                 if (avctx->channels > FF_SANE_NB_CHANNELS) {
585                     av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
586                            avctx->channels);
587                     return AVERROR(ENOSYS);
588                 }
589
590                 frame->channel_layout = av_get_default_channel_layout(avctx->channels);
591                 if (!frame->channel_layout)
592                     frame->channel_layout = (1ULL << avctx->channels) - 1;
593             }
594         }
595         break;
596     default: return AVERROR(EINVAL);
597     }
598
599     frame->pkt_pts = avctx->pkt ? avctx->pkt->pts : AV_NOPTS_VALUE;
600     frame->reordered_opaque = avctx->reordered_opaque;
601
602 #if FF_API_GET_BUFFER
603     /*
604      * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
605      * We wrap each plane in its own AVBuffer. Each of those has a reference to
606      * a dummy AVBuffer as its private data, unreffing it on free.
607      * When all the planes are freed, the dummy buffer's free callback calls
608      * release_buffer().
609      */
610     if (avctx->get_buffer) {
611         CompatReleaseBufPriv *priv = NULL;
612         AVBufferRef *dummy_buf = NULL;
613         int planes, i, ret;
614
615         if (flags & AV_GET_BUFFER_FLAG_REF)
616             frame->reference    = 1;
617
618         ret = avctx->get_buffer(avctx, frame);
619         if (ret < 0)
620             return ret;
621
622         /* return if the buffers are already set up
623          * this would happen e.g. when a custom get_buffer() calls
624          * avcodec_default_get_buffer
625          */
626         if (frame->buf[0])
627             return 0;
628
629         priv = av_mallocz(sizeof(*priv));
630         if (!priv) {
631             ret = AVERROR(ENOMEM);
632             goto fail;
633         }
634         priv->avctx = *avctx;
635         priv->frame = *frame;
636
637         dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
638         if (!dummy_buf) {
639             ret = AVERROR(ENOMEM);
640             goto fail;
641         }
642
643 #define WRAP_PLANE(ref_out, data, data_size)                            \
644 do {                                                                    \
645     AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf);                  \
646     if (!dummy_ref) {                                                   \
647         ret = AVERROR(ENOMEM);                                          \
648         goto fail;                                                      \
649     }                                                                   \
650     ref_out = av_buffer_create(data, data_size, compat_release_buffer,  \
651                                dummy_ref, 0);                           \
652     if (!ref_out) {                                                     \
653         av_frame_unref(frame);                                          \
654         ret = AVERROR(ENOMEM);                                          \
655         goto fail;                                                      \
656     }                                                                   \
657 } while (0)
658
659         if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
660             const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
661
662             if (!desc) {
663                 ret = AVERROR(EINVAL);
664                 goto fail;
665             }
666             planes = (desc->flags & PIX_FMT_PLANAR) ? desc->nb_components : 1;
667
668             for (i = 0; i < planes; i++) {
669                 int h_shift    = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
670                 int plane_size = (frame->width >> h_shift) * frame->linesize[i];
671
672                 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
673             }
674         } else {
675             int planar = av_sample_fmt_is_planar(frame->format);
676             planes = planar ? avctx->channels : 1;
677
678             if (planes > FF_ARRAY_ELEMS(frame->buf)) {
679                 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
680                 frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
681                                                 frame->nb_extended_buf);
682                 if (!frame->extended_buf) {
683                     ret = AVERROR(ENOMEM);
684                     goto fail;
685                 }
686             }
687
688             for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
689                 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
690
691             for (i = 0; i < frame->nb_extended_buf; i++)
692                 WRAP_PLANE(frame->extended_buf[i],
693                            frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
694                            frame->linesize[0]);
695         }
696
697         av_buffer_unref(&dummy_buf);
698
699         return 0;
700
701 fail:
702         avctx->release_buffer(avctx, frame);
703         av_freep(&priv);
704         av_buffer_unref(&dummy_buf);
705         return ret;
706     }
707 #endif
708
709     return avctx->get_buffer2(avctx, frame, flags);
710 }
711
712 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
713 {
714     AVFrame tmp;
715     int ret;
716
717     av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
718
719     if (!frame->data[0])
720         return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
721
722     if (av_frame_is_writable(frame))
723         return 0;
724
725     av_frame_move_ref(&tmp, frame);
726
727     ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
728     if (ret < 0) {
729         av_frame_unref(&tmp);
730         return ret;
731     }
732
733     av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
734                   frame->format, frame->width, frame->height);
735
736     av_frame_unref(&tmp);
737
738     return 0;
739 }
740
741 #if FF_API_GET_BUFFER
742 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
743 {
744     av_frame_unref(pic);
745 }
746
747 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
748 {
749     av_assert0(0);
750 }
751 #endif
752
753 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
754 {
755     int i;
756
757     for (i = 0; i < count; i++) {
758         int r = func(c, (char *)arg + i * size);
759         if (ret)
760             ret[i] = r;
761     }
762     return 0;
763 }
764
765 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
766 {
767     int i;
768
769     for (i = 0; i < count; i++) {
770         int r = func(c, arg, i, 0);
771         if (ret)
772             ret[i] = r;
773     }
774     return 0;
775 }
776
777 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
778 {
779     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
780     return desc->flags & PIX_FMT_HWACCEL;
781 }
782
783 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
784 {
785     while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
786         ++fmt;
787     return fmt[0];
788 }
789
790 void avcodec_get_frame_defaults(AVFrame *frame)
791 {
792     if (frame->extended_data != frame->data)
793         av_freep(&frame->extended_data);
794
795     memset(frame, 0, sizeof(AVFrame));
796
797     frame->pts                 = AV_NOPTS_VALUE;
798     frame->key_frame           = 1;
799     frame->sample_aspect_ratio = (AVRational) {0, 1 };
800     frame->format              = -1; /* unknown */
801     frame->extended_data       = frame->data;
802 }
803
804 AVFrame *avcodec_alloc_frame(void)
805 {
806     AVFrame *frame = av_mallocz(sizeof(AVFrame));
807
808     if (frame == NULL)
809         return NULL;
810
811     avcodec_get_frame_defaults(frame);
812
813     return frame;
814 }
815
816 void avcodec_free_frame(AVFrame **frame)
817 {
818     AVFrame *f;
819
820     if (!frame || !*frame)
821         return;
822
823     f = *frame;
824
825     if (f->extended_data != f->data)
826         av_freep(&f->extended_data);
827
828     av_freep(frame);
829 }
830
831 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
832 {
833     int ret = 0;
834     AVDictionary *tmp = NULL;
835
836     if (avcodec_is_open(avctx))
837         return 0;
838
839     if ((!codec && !avctx->codec)) {
840         av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
841         return AVERROR(EINVAL);
842     }
843     if ((codec && avctx->codec && codec != avctx->codec)) {
844         av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
845                                     "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
846         return AVERROR(EINVAL);
847     }
848     if (!codec)
849         codec = avctx->codec;
850
851     if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
852         return AVERROR(EINVAL);
853
854     if (options)
855         av_dict_copy(&tmp, *options, 0);
856
857     /* If there is a user-supplied mutex locking routine, call it. */
858     if (ff_lockmgr_cb) {
859         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
860             return -1;
861     }
862
863     entangled_thread_counter++;
864     if (entangled_thread_counter != 1) {
865         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
866         ret = -1;
867         goto end;
868     }
869
870     avctx->internal = av_mallocz(sizeof(AVCodecInternal));
871     if (!avctx->internal) {
872         ret = AVERROR(ENOMEM);
873         goto end;
874     }
875
876     avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
877     if (!avctx->internal->pool) {
878         ret = AVERROR(ENOMEM);
879         goto free_and_end;
880     }
881
882     if (codec->priv_data_size > 0) {
883         if (!avctx->priv_data) {
884             avctx->priv_data = av_mallocz(codec->priv_data_size);
885             if (!avctx->priv_data) {
886                 ret = AVERROR(ENOMEM);
887                 goto end;
888             }
889             if (codec->priv_class) {
890                 *(const AVClass **)avctx->priv_data = codec->priv_class;
891                 av_opt_set_defaults(avctx->priv_data);
892             }
893         }
894         if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
895             goto free_and_end;
896     } else {
897         avctx->priv_data = NULL;
898     }
899     if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
900         goto free_and_end;
901
902     if (avctx->coded_width && avctx->coded_height)
903         avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
904     else if (avctx->width && avctx->height)
905         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
906
907     if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
908         && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
909            || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
910         av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
911         avcodec_set_dimensions(avctx, 0, 0);
912     }
913
914     /* if the decoder init function was already called previously,
915      * free the already allocated subtitle_header before overwriting it */
916     if (av_codec_is_decoder(codec))
917         av_freep(&avctx->subtitle_header);
918
919     if (avctx->channels > FF_SANE_NB_CHANNELS) {
920         ret = AVERROR(EINVAL);
921         goto free_and_end;
922     }
923
924     avctx->codec = codec;
925     if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
926         avctx->codec_id == AV_CODEC_ID_NONE) {
927         avctx->codec_type = codec->type;
928         avctx->codec_id   = codec->id;
929     }
930     if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
931                                          && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
932         av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
933         ret = AVERROR(EINVAL);
934         goto free_and_end;
935     }
936     avctx->frame_number = 0;
937
938     if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
939         avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
940         ret = AVERROR_EXPERIMENTAL;
941         goto free_and_end;
942     }
943
944     if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
945         (!avctx->time_base.num || !avctx->time_base.den)) {
946         avctx->time_base.num = 1;
947         avctx->time_base.den = avctx->sample_rate;
948     }
949
950     if (HAVE_THREADS && !avctx->thread_opaque) {
951         ret = ff_thread_init(avctx);
952         if (ret < 0) {
953             goto free_and_end;
954         }
955     }
956     if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
957         avctx->thread_count = 1;
958
959     if (av_codec_is_encoder(avctx->codec)) {
960         int i;
961         if (avctx->codec->sample_fmts) {
962             for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
963                 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
964                     break;
965                 if (avctx->channels == 1 &&
966                     av_get_planar_sample_fmt(avctx->sample_fmt) ==
967                     av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
968                     avctx->sample_fmt = avctx->codec->sample_fmts[i];
969                     break;
970                 }
971             }
972             if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
973                 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
974                 ret = AVERROR(EINVAL);
975                 goto free_and_end;
976             }
977         }
978         if (avctx->codec->pix_fmts) {
979             for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
980                 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
981                     break;
982             if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
983                 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
984                 ret = AVERROR(EINVAL);
985                 goto free_and_end;
986             }
987         }
988         if (avctx->codec->supported_samplerates) {
989             for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
990                 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
991                     break;
992             if (avctx->codec->supported_samplerates[i] == 0) {
993                 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
994                 ret = AVERROR(EINVAL);
995                 goto free_and_end;
996             }
997         }
998         if (avctx->codec->channel_layouts) {
999             if (!avctx->channel_layout) {
1000                 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
1001             } else {
1002                 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1003                     if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1004                         break;
1005                 if (avctx->codec->channel_layouts[i] == 0) {
1006                     av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
1007                     ret = AVERROR(EINVAL);
1008                     goto free_and_end;
1009                 }
1010             }
1011         }
1012         if (avctx->channel_layout && avctx->channels) {
1013             if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
1014                 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
1015                 ret = AVERROR(EINVAL);
1016                 goto free_and_end;
1017             }
1018         } else if (avctx->channel_layout) {
1019             avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1020         }
1021
1022         if (!avctx->rc_initial_buffer_occupancy)
1023             avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1024     }
1025
1026     if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
1027         ret = avctx->codec->init(avctx);
1028         if (ret < 0) {
1029             goto free_and_end;
1030         }
1031     }
1032
1033     if (av_codec_is_decoder(avctx->codec)) {
1034         /* validate channel layout from the decoder */
1035         if (avctx->channel_layout) {
1036             int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1037             if (!avctx->channels)
1038                 avctx->channels = channels;
1039             else if (channels != avctx->channels) {
1040                 av_log(avctx, AV_LOG_WARNING,
1041                        "channel layout does not match number of channels\n");
1042                 avctx->channel_layout = 0;
1043             }
1044         }
1045         if (avctx->channels && avctx->channels < 0 ||
1046             avctx->channels > FF_SANE_NB_CHANNELS) {
1047             ret = AVERROR(EINVAL);
1048             goto free_and_end;
1049         }
1050     }
1051 end:
1052     entangled_thread_counter--;
1053
1054     /* Release any user-supplied mutex. */
1055     if (ff_lockmgr_cb) {
1056         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1057     }
1058     if (options) {
1059         av_dict_free(options);
1060         *options = tmp;
1061     }
1062
1063     return ret;
1064 free_and_end:
1065     av_dict_free(&tmp);
1066     av_freep(&avctx->priv_data);
1067     if (avctx->internal)
1068         av_freep(&avctx->internal->pool);
1069     av_freep(&avctx->internal);
1070     avctx->codec = NULL;
1071     goto end;
1072 }
1073
1074 int ff_alloc_packet(AVPacket *avpkt, int size)
1075 {
1076     if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
1077         return AVERROR(EINVAL);
1078
1079     if (avpkt->data) {
1080         AVBufferRef *buf = avpkt->buf;
1081 #if FF_API_DESTRUCT_PACKET
1082         void *destruct = avpkt->destruct;
1083 #endif
1084
1085         if (avpkt->size < size)
1086             return AVERROR(EINVAL);
1087
1088         av_init_packet(avpkt);
1089 #if FF_API_DESTRUCT_PACKET
1090         avpkt->destruct = destruct;
1091 #endif
1092         avpkt->buf      = buf;
1093         avpkt->size     = size;
1094         return 0;
1095     } else {
1096         return av_new_packet(avpkt, size);
1097     }
1098 }
1099
1100 /**
1101  * Pad last frame with silence.
1102  */
1103 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1104 {
1105     AVFrame *frame = NULL;
1106     uint8_t *buf   = NULL;
1107     int ret;
1108
1109     if (!(frame = avcodec_alloc_frame()))
1110         return AVERROR(ENOMEM);
1111     *frame = *src;
1112
1113     if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
1114                                           s->frame_size, s->sample_fmt, 0)) < 0)
1115         goto fail;
1116
1117     if (!(buf = av_malloc(ret))) {
1118         ret = AVERROR(ENOMEM);
1119         goto fail;
1120     }
1121
1122     frame->nb_samples = s->frame_size;
1123     if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
1124                                         buf, ret, 0)) < 0)
1125         goto fail;
1126     if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1127                                src->nb_samples, s->channels, s->sample_fmt)) < 0)
1128         goto fail;
1129     if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1130                                       frame->nb_samples - src->nb_samples,
1131                                       s->channels, s->sample_fmt)) < 0)
1132         goto fail;
1133
1134     *dst = frame;
1135
1136     return 0;
1137
1138 fail:
1139     if (frame->extended_data != frame->data)
1140         av_freep(&frame->extended_data);
1141     av_freep(&buf);
1142     av_freep(&frame);
1143     return ret;
1144 }
1145
1146 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1147                                               AVPacket *avpkt,
1148                                               const AVFrame *frame,
1149                                               int *got_packet_ptr)
1150 {
1151     AVFrame tmp;
1152     AVFrame *padded_frame = NULL;
1153     int ret;
1154     int user_packet = !!avpkt->data;
1155
1156     *got_packet_ptr = 0;
1157
1158     if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1159         av_free_packet(avpkt);
1160         av_init_packet(avpkt);
1161         return 0;
1162     }
1163
1164     /* ensure that extended_data is properly set */
1165     if (frame && !frame->extended_data) {
1166         if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1167             avctx->channels > AV_NUM_DATA_POINTERS) {
1168             av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1169                                         "with more than %d channels, but extended_data is not set.\n",
1170                    AV_NUM_DATA_POINTERS);
1171             return AVERROR(EINVAL);
1172         }
1173         av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1174
1175         tmp = *frame;
1176         tmp.extended_data = tmp.data;
1177         frame = &tmp;
1178     }
1179
1180     /* check for valid frame size */
1181     if (frame) {
1182         if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1183             if (frame->nb_samples > avctx->frame_size)
1184                 return AVERROR(EINVAL);
1185         } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1186             if (frame->nb_samples < avctx->frame_size &&
1187                 !avctx->internal->last_audio_frame) {
1188                 ret = pad_last_frame(avctx, &padded_frame, frame);
1189                 if (ret < 0)
1190                     return ret;
1191
1192                 frame = padded_frame;
1193                 avctx->internal->last_audio_frame = 1;
1194             }
1195
1196             if (frame->nb_samples != avctx->frame_size) {
1197                 ret = AVERROR(EINVAL);
1198                 goto end;
1199             }
1200         }
1201     }
1202
1203     ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1204     if (!ret) {
1205         if (*got_packet_ptr) {
1206             if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1207                 if (avpkt->pts == AV_NOPTS_VALUE)
1208                     avpkt->pts = frame->pts;
1209                 if (!avpkt->duration)
1210                     avpkt->duration = ff_samples_to_time_base(avctx,
1211                                                               frame->nb_samples);
1212             }
1213             avpkt->dts = avpkt->pts;
1214         } else {
1215             avpkt->size = 0;
1216         }
1217
1218         if (!user_packet && avpkt->size) {
1219             ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1220             if (ret >= 0)
1221                 avpkt->data = avpkt->buf->data;
1222         }
1223
1224         avctx->frame_number++;
1225     }
1226
1227     if (ret < 0 || !*got_packet_ptr) {
1228         av_free_packet(avpkt);
1229         av_init_packet(avpkt);
1230         goto end;
1231     }
1232
1233     /* NOTE: if we add any audio encoders which output non-keyframe packets,
1234      *       this needs to be moved to the encoders, but for now we can do it
1235      *       here to simplify things */
1236     avpkt->flags |= AV_PKT_FLAG_KEY;
1237
1238 end:
1239     if (padded_frame) {
1240         av_freep(&padded_frame->data[0]);
1241         if (padded_frame->extended_data != padded_frame->data)
1242             av_freep(&padded_frame->extended_data);
1243         av_freep(&padded_frame);
1244     }
1245
1246     return ret;
1247 }
1248
1249 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1250                                               AVPacket *avpkt,
1251                                               const AVFrame *frame,
1252                                               int *got_packet_ptr)
1253 {
1254     int ret;
1255     int user_packet = !!avpkt->data;
1256
1257     *got_packet_ptr = 0;
1258
1259     if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1260         av_free_packet(avpkt);
1261         av_init_packet(avpkt);
1262         avpkt->size = 0;
1263         return 0;
1264     }
1265
1266     if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1267         return AVERROR(EINVAL);
1268
1269     av_assert0(avctx->codec->encode2);
1270
1271     ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1272     if (!ret) {
1273         if (!*got_packet_ptr)
1274             avpkt->size = 0;
1275         else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1276             avpkt->pts = avpkt->dts = frame->pts;
1277
1278         if (!user_packet && avpkt->size) {
1279             ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1280             if (ret >= 0)
1281                 avpkt->data = avpkt->buf->data;
1282         }
1283
1284         avctx->frame_number++;
1285     }
1286
1287     if (ret < 0 || !*got_packet_ptr)
1288         av_free_packet(avpkt);
1289
1290     emms_c();
1291     return ret;
1292 }
1293
1294 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1295                             const AVSubtitle *sub)
1296 {
1297     int ret;
1298     if (sub->start_display_time) {
1299         av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1300         return -1;
1301     }
1302     if (sub->num_rects == 0 || !sub->rects)
1303         return -1;
1304     ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1305     avctx->frame_number++;
1306     return ret;
1307 }
1308
1309 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1310 {
1311     int size = 0;
1312     const uint8_t *data;
1313     uint32_t flags;
1314
1315     if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1316         return;
1317
1318     data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1319     if (!data || size < 4)
1320         return;
1321     flags = bytestream_get_le32(&data);
1322     size -= 4;
1323     if (size < 4) /* Required for any of the changes */
1324         return;
1325     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1326         avctx->channels = bytestream_get_le32(&data);
1327         size -= 4;
1328     }
1329     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1330         if (size < 8)
1331             return;
1332         avctx->channel_layout = bytestream_get_le64(&data);
1333         size -= 8;
1334     }
1335     if (size < 4)
1336         return;
1337     if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1338         avctx->sample_rate = bytestream_get_le32(&data);
1339         size -= 4;
1340     }
1341     if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1342         if (size < 8)
1343             return;
1344         avctx->width  = bytestream_get_le32(&data);
1345         avctx->height = bytestream_get_le32(&data);
1346         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1347         size -= 8;
1348     }
1349 }
1350
1351 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1352                                               int *got_picture_ptr,
1353                                               AVPacket *avpkt)
1354 {
1355     AVCodecInternal *avci = avctx->internal;
1356     int ret;
1357
1358     *got_picture_ptr = 0;
1359     if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1360         return -1;
1361
1362     avctx->pkt = avpkt;
1363     apply_param_change(avctx, avpkt);
1364
1365     avcodec_get_frame_defaults(picture);
1366
1367     if (!avctx->refcounted_frames)
1368         av_frame_unref(&avci->to_free);
1369
1370     if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1371         if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1372             ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1373                                          avpkt);
1374         else {
1375             ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1376                                        avpkt);
1377             picture->pkt_dts = avpkt->dts;
1378             /* get_buffer is supposed to set frame parameters */
1379             if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
1380                 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1381                 picture->width               = avctx->width;
1382                 picture->height              = avctx->height;
1383                 picture->format              = avctx->pix_fmt;
1384             }
1385         }
1386
1387         emms_c(); //needed to avoid an emms_c() call before every return;
1388
1389         if (ret < 0 && picture->data[0])
1390             av_frame_unref(picture);
1391
1392         if (*got_picture_ptr) {
1393             if (!avctx->refcounted_frames) {
1394                 avci->to_free = *picture;
1395                 avci->to_free.extended_data = avci->to_free.data;
1396             }
1397
1398             avctx->frame_number++;
1399         }
1400     } else
1401         ret = 0;
1402
1403     /* many decoders assign whole AVFrames, thus overwriting extended_data;
1404      * make sure it's set correctly */
1405     picture->extended_data = picture->data;
1406
1407     return ret;
1408 }
1409
1410 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1411                                               AVFrame *frame,
1412                                               int *got_frame_ptr,
1413                                               AVPacket *avpkt)
1414 {
1415     AVCodecInternal *avci = avctx->internal;
1416     int planar, channels;
1417     int ret = 0;
1418
1419     *got_frame_ptr = 0;
1420
1421     avctx->pkt = avpkt;
1422
1423     if (!avpkt->data && avpkt->size) {
1424         av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1425         return AVERROR(EINVAL);
1426     }
1427
1428     apply_param_change(avctx, avpkt);
1429
1430     avcodec_get_frame_defaults(frame);
1431
1432     if (!avctx->refcounted_frames)
1433         av_frame_unref(&avci->to_free);
1434
1435     if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1436         ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1437         if (ret >= 0 && *got_frame_ptr) {
1438             avctx->frame_number++;
1439             frame->pkt_dts = avpkt->dts;
1440             if (frame->format == AV_SAMPLE_FMT_NONE)
1441                 frame->format = avctx->sample_fmt;
1442
1443             if (!avctx->refcounted_frames) {
1444                 avci->to_free = *frame;
1445                 avci->to_free.extended_data = avci->to_free.data;
1446             }
1447         }
1448
1449         if (ret < 0 && frame->data[0])
1450             av_frame_unref(frame);
1451     }
1452
1453     /* many decoders assign whole AVFrames, thus overwriting extended_data;
1454      * make sure it's set correctly; assume decoders that actually use
1455      * extended_data are doing it correctly */
1456     planar   = av_sample_fmt_is_planar(frame->format);
1457     channels = av_get_channel_layout_nb_channels(frame->channel_layout);
1458     if (!(planar && channels > AV_NUM_DATA_POINTERS))
1459         frame->extended_data = frame->data;
1460
1461     return ret;
1462 }
1463
1464 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1465                              int *got_sub_ptr,
1466                              AVPacket *avpkt)
1467 {
1468     int ret;
1469
1470     avctx->pkt = avpkt;
1471     *got_sub_ptr = 0;
1472     ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1473     if (*got_sub_ptr)
1474         avctx->frame_number++;
1475     return ret;
1476 }
1477
1478 void avsubtitle_free(AVSubtitle *sub)
1479 {
1480     int i;
1481
1482     for (i = 0; i < sub->num_rects; i++) {
1483         av_freep(&sub->rects[i]->pict.data[0]);
1484         av_freep(&sub->rects[i]->pict.data[1]);
1485         av_freep(&sub->rects[i]->pict.data[2]);
1486         av_freep(&sub->rects[i]->pict.data[3]);
1487         av_freep(&sub->rects[i]->text);
1488         av_freep(&sub->rects[i]->ass);
1489         av_freep(&sub->rects[i]);
1490     }
1491
1492     av_freep(&sub->rects);
1493
1494     memset(sub, 0, sizeof(AVSubtitle));
1495 }
1496
1497 av_cold int avcodec_close(AVCodecContext *avctx)
1498 {
1499     /* If there is a user-supplied mutex locking routine, call it. */
1500     if (ff_lockmgr_cb) {
1501         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1502             return -1;
1503     }
1504
1505     entangled_thread_counter++;
1506     if (entangled_thread_counter != 1) {
1507         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1508         entangled_thread_counter--;
1509         return -1;
1510     }
1511
1512     if (avcodec_is_open(avctx)) {
1513         FramePool *pool = avctx->internal->pool;
1514         int i;
1515         if (HAVE_THREADS && avctx->thread_opaque)
1516             ff_thread_free(avctx);
1517         if (avctx->codec && avctx->codec->close)
1518             avctx->codec->close(avctx);
1519         avctx->coded_frame = NULL;
1520         if (!avctx->refcounted_frames)
1521             av_frame_unref(&avctx->internal->to_free);
1522         for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
1523             av_buffer_pool_uninit(&pool->pools[i]);
1524         av_freep(&avctx->internal->pool);
1525         av_freep(&avctx->internal);
1526     }
1527
1528     if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1529         av_opt_free(avctx->priv_data);
1530     av_opt_free(avctx);
1531     av_freep(&avctx->priv_data);
1532     if (av_codec_is_encoder(avctx->codec))
1533         av_freep(&avctx->extradata);
1534     avctx->codec = NULL;
1535     avctx->active_thread_type = 0;
1536     entangled_thread_counter--;
1537
1538     /* Release any user-supplied mutex. */
1539     if (ff_lockmgr_cb) {
1540         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1541     }
1542     return 0;
1543 }
1544
1545 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1546 {
1547     AVCodec *p, *experimental = NULL;
1548     p = first_avcodec;
1549     while (p) {
1550         if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1551             p->id == id) {
1552             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1553                 experimental = p;
1554             } else
1555                 return p;
1556         }
1557         p = p->next;
1558     }
1559     return experimental;
1560 }
1561
1562 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1563 {
1564     return find_encdec(id, 1);
1565 }
1566
1567 AVCodec *avcodec_find_encoder_by_name(const char *name)
1568 {
1569     AVCodec *p;
1570     if (!name)
1571         return NULL;
1572     p = first_avcodec;
1573     while (p) {
1574         if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
1575             return p;
1576         p = p->next;
1577     }
1578     return NULL;
1579 }
1580
1581 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1582 {
1583     return find_encdec(id, 0);
1584 }
1585
1586 AVCodec *avcodec_find_decoder_by_name(const char *name)
1587 {
1588     AVCodec *p;
1589     if (!name)
1590         return NULL;
1591     p = first_avcodec;
1592     while (p) {
1593         if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
1594             return p;
1595         p = p->next;
1596     }
1597     return NULL;
1598 }
1599
1600 static int get_bit_rate(AVCodecContext *ctx)
1601 {
1602     int bit_rate;
1603     int bits_per_sample;
1604
1605     switch (ctx->codec_type) {
1606     case AVMEDIA_TYPE_VIDEO:
1607     case AVMEDIA_TYPE_DATA:
1608     case AVMEDIA_TYPE_SUBTITLE:
1609     case AVMEDIA_TYPE_ATTACHMENT:
1610         bit_rate = ctx->bit_rate;
1611         break;
1612     case AVMEDIA_TYPE_AUDIO:
1613         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1614         bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1615         break;
1616     default:
1617         bit_rate = 0;
1618         break;
1619     }
1620     return bit_rate;
1621 }
1622
1623 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1624 {
1625     int i, len, ret = 0;
1626
1627 #define TAG_PRINT(x)                                              \
1628     (((x) >= '0' && (x) <= '9') ||                                \
1629      ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') ||  \
1630      ((x) == '.' || (x) == ' '))
1631
1632     for (i = 0; i < 4; i++) {
1633         len = snprintf(buf, buf_size,
1634                        TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
1635         buf        += len;
1636         buf_size    = buf_size > len ? buf_size - len : 0;
1637         ret        += len;
1638         codec_tag >>= 8;
1639     }
1640     return ret;
1641 }
1642
1643 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1644 {
1645     const char *codec_name;
1646     const char *profile = NULL;
1647     const AVCodec *p;
1648     char buf1[32];
1649     int bitrate;
1650     AVRational display_aspect_ratio;
1651
1652     if (enc->codec)
1653         p = enc->codec;
1654     else if (encode)
1655         p = avcodec_find_encoder(enc->codec_id);
1656     else
1657         p = avcodec_find_decoder(enc->codec_id);
1658
1659     if (p) {
1660         codec_name = p->name;
1661         profile = av_get_profile_name(p, enc->profile);
1662     } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
1663         /* fake mpeg2 transport stream codec (currently not
1664          * registered) */
1665         codec_name = "mpeg2ts";
1666     } else if (enc->codec_name[0] != '\0') {
1667         codec_name = enc->codec_name;
1668     } else {
1669         /* output avi tags */
1670         char tag_buf[32];
1671         av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1672         snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1673         codec_name = buf1;
1674     }
1675
1676     switch (enc->codec_type) {
1677     case AVMEDIA_TYPE_VIDEO:
1678         snprintf(buf, buf_size,
1679                  "Video: %s%s",
1680                  codec_name, enc->mb_decision ? " (hq)" : "");
1681         if (profile)
1682             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1683                      " (%s)", profile);
1684         if (enc->pix_fmt != AV_PIX_FMT_NONE) {
1685             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1686                      ", %s",
1687                      av_get_pix_fmt_name(enc->pix_fmt));
1688         }
1689         if (enc->width) {
1690             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1691                      ", %dx%d",
1692                      enc->width, enc->height);
1693             if (enc->sample_aspect_ratio.num) {
1694                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1695                           enc->width * enc->sample_aspect_ratio.num,
1696                           enc->height * enc->sample_aspect_ratio.den,
1697                           1024 * 1024);
1698                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1699                          " [PAR %d:%d DAR %d:%d]",
1700                          enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1701                          display_aspect_ratio.num, display_aspect_ratio.den);
1702             }
1703             if (av_log_get_level() >= AV_LOG_DEBUG) {
1704                 int g = av_gcd(enc->time_base.num, enc->time_base.den);
1705                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1706                          ", %d/%d",
1707                          enc->time_base.num / g, enc->time_base.den / g);
1708             }
1709         }
1710         if (encode) {
1711             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1712                      ", q=%d-%d", enc->qmin, enc->qmax);
1713         }
1714         break;
1715     case AVMEDIA_TYPE_AUDIO:
1716         snprintf(buf, buf_size,
1717                  "Audio: %s",
1718                  codec_name);
1719         if (profile)
1720             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1721                      " (%s)", profile);
1722         if (enc->sample_rate) {
1723             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1724                      ", %d Hz", enc->sample_rate);
1725         }
1726         av_strlcat(buf, ", ", buf_size);
1727         av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1728         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1729             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1730                      ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1731         }
1732         break;
1733     case AVMEDIA_TYPE_DATA:
1734         snprintf(buf, buf_size, "Data: %s", codec_name);
1735         break;
1736     case AVMEDIA_TYPE_SUBTITLE:
1737         snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1738         break;
1739     case AVMEDIA_TYPE_ATTACHMENT:
1740         snprintf(buf, buf_size, "Attachment: %s", codec_name);
1741         break;
1742     default:
1743         snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1744         return;
1745     }
1746     if (encode) {
1747         if (enc->flags & CODEC_FLAG_PASS1)
1748             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1749                      ", pass 1");
1750         if (enc->flags & CODEC_FLAG_PASS2)
1751             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1752                      ", pass 2");
1753     }
1754     bitrate = get_bit_rate(enc);
1755     if (bitrate != 0) {
1756         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1757                  ", %d kb/s", bitrate / 1000);
1758     }
1759 }
1760
1761 const char *av_get_profile_name(const AVCodec *codec, int profile)
1762 {
1763     const AVProfile *p;
1764     if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1765         return NULL;
1766
1767     for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1768         if (p->profile == profile)
1769             return p->name;
1770
1771     return NULL;
1772 }
1773
1774 unsigned avcodec_version(void)
1775 {
1776     return LIBAVCODEC_VERSION_INT;
1777 }
1778
1779 const char *avcodec_configuration(void)
1780 {
1781     return LIBAV_CONFIGURATION;
1782 }
1783
1784 const char *avcodec_license(void)
1785 {
1786 #define LICENSE_PREFIX "libavcodec license: "
1787     return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1788 }
1789
1790 void avcodec_flush_buffers(AVCodecContext *avctx)
1791 {
1792     if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1793         ff_thread_flush(avctx);
1794     else if (avctx->codec->flush)
1795         avctx->codec->flush(avctx);
1796 }
1797
1798 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
1799 {
1800     switch (codec_id) {
1801     case AV_CODEC_ID_ADPCM_CT:
1802     case AV_CODEC_ID_ADPCM_IMA_APC:
1803     case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1804     case AV_CODEC_ID_ADPCM_IMA_WS:
1805     case AV_CODEC_ID_ADPCM_G722:
1806     case AV_CODEC_ID_ADPCM_YAMAHA:
1807         return 4;
1808     case AV_CODEC_ID_PCM_ALAW:
1809     case AV_CODEC_ID_PCM_MULAW:
1810     case AV_CODEC_ID_PCM_S8:
1811     case AV_CODEC_ID_PCM_U8:
1812     case AV_CODEC_ID_PCM_ZORK:
1813         return 8;
1814     case AV_CODEC_ID_PCM_S16BE:
1815     case AV_CODEC_ID_PCM_S16LE:
1816     case AV_CODEC_ID_PCM_S16LE_PLANAR:
1817     case AV_CODEC_ID_PCM_U16BE:
1818     case AV_CODEC_ID_PCM_U16LE:
1819         return 16;
1820     case AV_CODEC_ID_PCM_S24DAUD:
1821     case AV_CODEC_ID_PCM_S24BE:
1822     case AV_CODEC_ID_PCM_S24LE:
1823     case AV_CODEC_ID_PCM_U24BE:
1824     case AV_CODEC_ID_PCM_U24LE:
1825         return 24;
1826     case AV_CODEC_ID_PCM_S32BE:
1827     case AV_CODEC_ID_PCM_S32LE:
1828     case AV_CODEC_ID_PCM_U32BE:
1829     case AV_CODEC_ID_PCM_U32LE:
1830     case AV_CODEC_ID_PCM_F32BE:
1831     case AV_CODEC_ID_PCM_F32LE:
1832         return 32;
1833     case AV_CODEC_ID_PCM_F64BE:
1834     case AV_CODEC_ID_PCM_F64LE:
1835         return 64;
1836     default:
1837         return 0;
1838     }
1839 }
1840
1841 int av_get_bits_per_sample(enum AVCodecID codec_id)
1842 {
1843     switch (codec_id) {
1844     case AV_CODEC_ID_ADPCM_SBPRO_2:
1845         return 2;
1846     case AV_CODEC_ID_ADPCM_SBPRO_3:
1847         return 3;
1848     case AV_CODEC_ID_ADPCM_SBPRO_4:
1849     case AV_CODEC_ID_ADPCM_IMA_WAV:
1850     case AV_CODEC_ID_ADPCM_IMA_QT:
1851     case AV_CODEC_ID_ADPCM_SWF:
1852     case AV_CODEC_ID_ADPCM_MS:
1853         return 4;
1854     default:
1855         return av_get_exact_bits_per_sample(codec_id);
1856     }
1857 }
1858
1859 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
1860 {
1861     int id, sr, ch, ba, tag, bps;
1862
1863     id  = avctx->codec_id;
1864     sr  = avctx->sample_rate;
1865     ch  = avctx->channels;
1866     ba  = avctx->block_align;
1867     tag = avctx->codec_tag;
1868     bps = av_get_exact_bits_per_sample(avctx->codec_id);
1869
1870     /* codecs with an exact constant bits per sample */
1871     if (bps > 0 && ch > 0 && frame_bytes > 0)
1872         return (frame_bytes * 8) / (bps * ch);
1873     bps = avctx->bits_per_coded_sample;
1874
1875     /* codecs with a fixed packet duration */
1876     switch (id) {
1877     case AV_CODEC_ID_ADPCM_ADX:    return   32;
1878     case AV_CODEC_ID_ADPCM_IMA_QT: return   64;
1879     case AV_CODEC_ID_ADPCM_EA_XAS: return  128;
1880     case AV_CODEC_ID_AMR_NB:
1881     case AV_CODEC_ID_GSM:
1882     case AV_CODEC_ID_QCELP:
1883     case AV_CODEC_ID_RA_144:
1884     case AV_CODEC_ID_RA_288:       return  160;
1885     case AV_CODEC_ID_IMC:          return  256;
1886     case AV_CODEC_ID_AMR_WB:
1887     case AV_CODEC_ID_GSM_MS:       return  320;
1888     case AV_CODEC_ID_MP1:          return  384;
1889     case AV_CODEC_ID_ATRAC1:       return  512;
1890     case AV_CODEC_ID_ATRAC3:       return 1024;
1891     case AV_CODEC_ID_MP2:
1892     case AV_CODEC_ID_MUSEPACK7:    return 1152;
1893     case AV_CODEC_ID_AC3:          return 1536;
1894     }
1895
1896     if (sr > 0) {
1897         /* calc from sample rate */
1898         if (id == AV_CODEC_ID_TTA)
1899             return 256 * sr / 245;
1900
1901         if (ch > 0) {
1902             /* calc from sample rate and channels */
1903             if (id == AV_CODEC_ID_BINKAUDIO_DCT)
1904                 return (480 << (sr / 22050)) / ch;
1905         }
1906     }
1907
1908     if (ba > 0) {
1909         /* calc from block_align */
1910         if (id == AV_CODEC_ID_SIPR) {
1911             switch (ba) {
1912             case 20: return 160;
1913             case 19: return 144;
1914             case 29: return 288;
1915             case 37: return 480;
1916             }
1917         } else if (id == AV_CODEC_ID_ILBC) {
1918             switch (ba) {
1919             case 38: return 160;
1920             case 50: return 240;
1921             }
1922         }
1923     }
1924
1925     if (frame_bytes > 0) {
1926         /* calc from frame_bytes only */
1927         if (id == AV_CODEC_ID_TRUESPEECH)
1928             return 240 * (frame_bytes / 32);
1929         if (id == AV_CODEC_ID_NELLYMOSER)
1930             return 256 * (frame_bytes / 64);
1931
1932         if (bps > 0) {
1933             /* calc from frame_bytes and bits_per_coded_sample */
1934             if (id == AV_CODEC_ID_ADPCM_G726)
1935                 return frame_bytes * 8 / bps;
1936         }
1937
1938         if (ch > 0) {
1939             /* calc from frame_bytes and channels */
1940             switch (id) {
1941             case AV_CODEC_ID_ADPCM_4XM:
1942             case AV_CODEC_ID_ADPCM_IMA_ISS:
1943                 return (frame_bytes - 4 * ch) * 2 / ch;
1944             case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1945                 return (frame_bytes - 4) * 2 / ch;
1946             case AV_CODEC_ID_ADPCM_IMA_AMV:
1947                 return (frame_bytes - 8) * 2 / ch;
1948             case AV_CODEC_ID_ADPCM_XA:
1949                 return (frame_bytes / 128) * 224 / ch;
1950             case AV_CODEC_ID_INTERPLAY_DPCM:
1951                 return (frame_bytes - 6 - ch) / ch;
1952             case AV_CODEC_ID_ROQ_DPCM:
1953                 return (frame_bytes - 8) / ch;
1954             case AV_CODEC_ID_XAN_DPCM:
1955                 return (frame_bytes - 2 * ch) / ch;
1956             case AV_CODEC_ID_MACE3:
1957                 return 3 * frame_bytes / ch;
1958             case AV_CODEC_ID_MACE6:
1959                 return 6 * frame_bytes / ch;
1960             case AV_CODEC_ID_PCM_LXF:
1961                 return 2 * (frame_bytes / (5 * ch));
1962             }
1963
1964             if (tag) {
1965                 /* calc from frame_bytes, channels, and codec_tag */
1966                 if (id == AV_CODEC_ID_SOL_DPCM) {
1967                     if (tag == 3)
1968                         return frame_bytes / ch;
1969                     else
1970                         return frame_bytes * 2 / ch;
1971                 }
1972             }
1973
1974             if (ba > 0) {
1975                 /* calc from frame_bytes, channels, and block_align */
1976                 int blocks = frame_bytes / ba;
1977                 switch (avctx->codec_id) {
1978                 case AV_CODEC_ID_ADPCM_IMA_WAV:
1979                     return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
1980                 case AV_CODEC_ID_ADPCM_IMA_DK3:
1981                     return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
1982                 case AV_CODEC_ID_ADPCM_IMA_DK4:
1983                     return blocks * (1 + (ba - 4 * ch) * 2 / ch);
1984                 case AV_CODEC_ID_ADPCM_MS:
1985                     return blocks * (2 + (ba - 7 * ch) * 2 / ch);
1986                 }
1987             }
1988
1989             if (bps > 0) {
1990                 /* calc from frame_bytes, channels, and bits_per_coded_sample */
1991                 switch (avctx->codec_id) {
1992                 case AV_CODEC_ID_PCM_DVD:
1993                     return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
1994                 case AV_CODEC_ID_PCM_BLURAY:
1995                     return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
1996                 case AV_CODEC_ID_S302M:
1997                     return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
1998                 }
1999             }
2000         }
2001     }
2002
2003     return 0;
2004 }
2005
2006 #if !HAVE_THREADS
2007 int ff_thread_init(AVCodecContext *s)
2008 {
2009     return -1;
2010 }
2011
2012 #endif
2013
2014 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2015 {
2016     unsigned int n = 0;
2017
2018     while (v >= 0xff) {
2019         *s++ = 0xff;
2020         v -= 0xff;
2021         n++;
2022     }
2023     *s = v;
2024     n++;
2025     return n;
2026 }
2027
2028 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2029 {
2030     int i;
2031     for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2032     return i;
2033 }
2034
2035 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2036 {
2037     av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
2038             "version to the newest one from Git. If the problem still "
2039             "occurs, it means that your file has a feature which has not "
2040             "been implemented.\n", feature);
2041     if(want_sample)
2042         av_log_ask_for_sample(avc, NULL);
2043 }
2044
2045 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2046 {
2047     va_list argument_list;
2048
2049     va_start(argument_list, msg);
2050
2051     if (msg)
2052         av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2053     av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2054             "of this file to ftp://upload.libav.org/incoming/ "
2055             "and contact the libav-devel mailing list.\n");
2056
2057     va_end(argument_list);
2058 }
2059
2060 static AVHWAccel *first_hwaccel = NULL;
2061
2062 void av_register_hwaccel(AVHWAccel *hwaccel)
2063 {
2064     AVHWAccel **p = &first_hwaccel;
2065     while (*p)
2066         p = &(*p)->next;
2067     *p = hwaccel;
2068     hwaccel->next = NULL;
2069 }
2070
2071 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2072 {
2073     return hwaccel ? hwaccel->next : first_hwaccel;
2074 }
2075
2076 AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
2077 {
2078     AVHWAccel *hwaccel = NULL;
2079
2080     while ((hwaccel = av_hwaccel_next(hwaccel)))
2081         if (hwaccel->id == codec_id
2082             && hwaccel->pix_fmt == pix_fmt)
2083             return hwaccel;
2084     return NULL;
2085 }
2086
2087 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2088 {
2089     if (ff_lockmgr_cb) {
2090         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2091             return -1;
2092         if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2093             return -1;
2094     }
2095
2096     ff_lockmgr_cb = cb;
2097
2098     if (ff_lockmgr_cb) {
2099         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2100             return -1;
2101         if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2102             return -1;
2103     }
2104     return 0;
2105 }
2106
2107 int avpriv_lock_avformat(void)
2108 {
2109     if (ff_lockmgr_cb) {
2110         if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2111             return -1;
2112     }
2113     return 0;
2114 }
2115
2116 int avpriv_unlock_avformat(void)
2117 {
2118     if (ff_lockmgr_cb) {
2119         if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2120             return -1;
2121     }
2122     return 0;
2123 }
2124
2125 unsigned int avpriv_toupper4(unsigned int x)
2126 {
2127     return av_toupper(x & 0xFF) +
2128           (av_toupper((x >>  8) & 0xFF) << 8)  +
2129           (av_toupper((x >> 16) & 0xFF) << 16) +
2130           (av_toupper((x >> 24) & 0xFF) << 24);
2131 }
2132
2133 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
2134 {
2135     int ret;
2136
2137     dst->owner = src->owner;
2138
2139     ret = av_frame_ref(dst->f, src->f);
2140     if (ret < 0)
2141         return ret;
2142
2143     if (src->progress &&
2144         !(dst->progress = av_buffer_ref(src->progress))) {
2145         ff_thread_release_buffer(dst->owner, dst);
2146         return AVERROR(ENOMEM);
2147     }
2148
2149     return 0;
2150 }
2151
2152 #if !HAVE_THREADS
2153
2154 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
2155 {
2156     f->owner = avctx;
2157     return ff_get_buffer(avctx, f->f, flags);
2158 }
2159
2160 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
2161 {
2162     av_frame_unref(f->f);
2163 }
2164
2165 void ff_thread_finish_setup(AVCodecContext *avctx)
2166 {
2167 }
2168
2169 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
2170 {
2171 }
2172
2173 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
2174 {
2175 }
2176
2177 #endif
2178
2179 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
2180 {
2181     if (codec_id <= AV_CODEC_ID_NONE)
2182         return AVMEDIA_TYPE_UNKNOWN;
2183     else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2184         return AVMEDIA_TYPE_VIDEO;
2185     else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2186         return AVMEDIA_TYPE_AUDIO;
2187     else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2188         return AVMEDIA_TYPE_SUBTITLE;
2189
2190     return AVMEDIA_TYPE_UNKNOWN;
2191 }
2192
2193 int avcodec_is_open(AVCodecContext *s)
2194 {
2195     return !!s->internal;
2196 }