utils.c 106 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1 2
/*
 * utils for libavcodec
3
 * Copyright (c) 2001 Fabrice Bellard
4
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
Fabrice Bellard's avatar
Fabrice Bellard committed
5
 *
6 7 8
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
9 10
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Fabrice Bellard's avatar
Fabrice Bellard committed
21
 */
22

Michael Niedermayer's avatar
Michael Niedermayer committed
23
/**
24
 * @file
Michael Niedermayer's avatar
Michael Niedermayer committed
25 26
 * utils.
 */
27

28
#include "config.h"
29
#include "libavutil/accessors.h"
30
#include "libavutil/atomic.h"
31
#include "libavutil/attributes.h"
32
#include "libavutil/avassert.h"
33
#include "libavutil/avstring.h"
34
#include "libavutil/bprint.h"
35
#include "libavutil/channel_layout.h"
36
#include "libavutil/crc.h"
37
#include "libavutil/frame.h"
38
#include "libavutil/internal.h"
39
#include "libavutil/mathematics.h"
40
#include "libavutil/pixdesc.h"
41 42
#include "libavutil/imgutils.h"
#include "libavutil/samplefmt.h"
43
#include "libavutil/dict.h"
44
#include "libavutil/avassert.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
45
#include "avcodec.h"
46
#include "dsputil.h"
47
#include "libavutil/opt.h"
48
#include "thread.h"
49
#include "frame_thread_encoder.h"
50
#include "internal.h"
51
#include "bytestream.h"
52
#include "version.h"
53
#include <stdlib.h>
54
#include <stdarg.h>
55
#include <limits.h>
Michael Niedermayer's avatar
Michael Niedermayer committed
56
#include <float.h>
57
#if CONFIG_ICONV
58 59
# include <iconv.h>
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
60

61
volatile int ff_avcodec_locked;
Luca Barbato's avatar
Luca Barbato committed
62
static int volatile entangled_thread_counter = 0;
63
static int (*lockmgr_cb)(void **mutex, enum AVLockOp op);
64
static void *codec_mutex;
65
static void *avformat_mutex;
66

67
void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
68
{
Luca Barbato's avatar
Luca Barbato committed
69
    if (min_size < *size)
70
        return ptr;
71

Luca Barbato's avatar
Luca Barbato committed
72
    min_size = FFMAX(17 * min_size / 16 + 32, min_size);
73

Luca Barbato's avatar
Luca Barbato committed
74 75 76 77 78 79
    ptr = av_realloc(ptr, min_size);
    /* we could set this to the unmodified min_size but this is safer
     * if the user lost the ptr and uses NULL now
     */
    if (!ptr)
        min_size = 0;
80

Luca Barbato's avatar
Luca Barbato committed
81
    *size = min_size;
82 83

    return ptr;
84 85
}

86
static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
87 88 89
{
    void **p = ptr;
    if (min_size < *size)
90
        return 0;
Luca Barbato's avatar
Luca Barbato committed
91
    min_size = FFMAX(17 * min_size / 16 + 32, min_size);
92
    av_free(*p);
93
    *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
Luca Barbato's avatar
Luca Barbato committed
94 95 96
    if (!*p)
        min_size = 0;
    *size = min_size;
97 98 99 100 101 102 103 104 105 106 107 108
    return 1;
}

void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
{
    ff_fast_malloc(ptr, size, min_size, 0);
}

void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
{
    uint8_t **p = ptr;
    if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
109
        av_freep(p);
110 111 112 113 114
        *size = 0;
        return;
    }
    if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
        memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
115 116
}

117 118 119 120 121 122 123 124 125 126 127 128
void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
{
    uint8_t **p = ptr;
    if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
        av_freep(p);
        *size = 0;
        return;
    }
    if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
        memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
}

Fabrice Bellard's avatar
Fabrice Bellard committed
129
/* encoder management */
130
static AVCodec *first_avcodec = NULL;
Fabrice Bellard's avatar
Fabrice Bellard committed
131

132 133
AVCodec *av_codec_next(const AVCodec *c)
{
Luca Barbato's avatar
Luca Barbato committed
134 135 136 137
    if (c)
        return c->next;
    else
        return first_avcodec;
138 139
}

140
static av_cold void avcodec_init(void)
141 142 143 144 145 146 147
{
    static int initialized = 0;

    if (initialized != 0)
        return;
    initialized = 1;

148 149
    if (CONFIG_DSPUTIL)
        ff_dsputil_static_init();
150 151
}

152
int av_codec_is_encoder(const AVCodec *codec)
153
{
154
    return codec && (codec->encode_sub || codec->encode2);
155 156
}

157
int av_codec_is_decoder(const AVCodec *codec)
158 159 160 161
{
    return codec && codec->decode;
}

162
av_cold void avcodec_register(AVCodec *codec)
Fabrice Bellard's avatar
Fabrice Bellard committed
163 164
{
    AVCodec **p;
165
    avcodec_init();
Fabrice Bellard's avatar
Fabrice Bellard committed
166
    p = &first_avcodec;
167
    codec->next = NULL;
168 169
    while(avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
        p = &(*p)->next;
170 171 172

    if (codec->init_static_data)
        codec->init_static_data(codec);
Fabrice Bellard's avatar
Fabrice Bellard committed
173 174
}

175 176 177 178 179
unsigned avcodec_get_edge_width(void)
{
    return EDGE_WIDTH;
}

Luca Barbato's avatar
Luca Barbato committed
180 181 182 183
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
{
    s->coded_width  = width;
    s->coded_height = height;
184 185
    s->width        = FF_CEIL_RSHIFT(width,  s->lowres);
    s->height       = FF_CEIL_RSHIFT(height, s->lowres);
186 187
}

188 189 190 191 192 193
#if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
#   define STRIDE_ALIGN 16
#else
#   define STRIDE_ALIGN 8
#endif

194 195 196 197
void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
                               int linesize_align[AV_NUM_DATA_POINTERS])
{
    int i;
Luca Barbato's avatar
Luca Barbato committed
198 199
    int w_align = 1;
    int h_align = 1;
200

Luca Barbato's avatar
Luca Barbato committed
201
    switch (s->pix_fmt) {
202 203 204 205 206 207
    case AV_PIX_FMT_YUV420P:
    case AV_PIX_FMT_YUYV422:
    case AV_PIX_FMT_UYVY422:
    case AV_PIX_FMT_YUV422P:
    case AV_PIX_FMT_YUV440P:
    case AV_PIX_FMT_YUV444P:
208
    case AV_PIX_FMT_GBRAP:
209 210 211 212 213 214 215 216 217
    case AV_PIX_FMT_GBRP:
    case AV_PIX_FMT_GRAY8:
    case AV_PIX_FMT_GRAY16BE:
    case AV_PIX_FMT_GRAY16LE:
    case AV_PIX_FMT_YUVJ420P:
    case AV_PIX_FMT_YUVJ422P:
    case AV_PIX_FMT_YUVJ440P:
    case AV_PIX_FMT_YUVJ444P:
    case AV_PIX_FMT_YUVA420P:
218 219
    case AV_PIX_FMT_YUVA422P:
    case AV_PIX_FMT_YUVA444P:
220 221 222 223
    case AV_PIX_FMT_YUV420P9LE:
    case AV_PIX_FMT_YUV420P9BE:
    case AV_PIX_FMT_YUV420P10LE:
    case AV_PIX_FMT_YUV420P10BE:
224 225 226 227
    case AV_PIX_FMT_YUV420P12LE:
    case AV_PIX_FMT_YUV420P12BE:
    case AV_PIX_FMT_YUV420P14LE:
    case AV_PIX_FMT_YUV420P14BE:
228 229 230 231
    case AV_PIX_FMT_YUV422P9LE:
    case AV_PIX_FMT_YUV422P9BE:
    case AV_PIX_FMT_YUV422P10LE:
    case AV_PIX_FMT_YUV422P10BE:
232 233 234 235
    case AV_PIX_FMT_YUV422P12LE:
    case AV_PIX_FMT_YUV422P12BE:
    case AV_PIX_FMT_YUV422P14LE:
    case AV_PIX_FMT_YUV422P14BE:
236 237 238 239
    case AV_PIX_FMT_YUV444P9LE:
    case AV_PIX_FMT_YUV444P9BE:
    case AV_PIX_FMT_YUV444P10LE:
    case AV_PIX_FMT_YUV444P10BE:
240 241 242 243
    case AV_PIX_FMT_YUV444P12LE:
    case AV_PIX_FMT_YUV444P12BE:
    case AV_PIX_FMT_YUV444P14LE:
    case AV_PIX_FMT_YUV444P14BE:
244 245 246 247 248 249 250 251 252 253 254 255
    case AV_PIX_FMT_YUVA420P9LE:
    case AV_PIX_FMT_YUVA420P9BE:
    case AV_PIX_FMT_YUVA420P10LE:
    case AV_PIX_FMT_YUVA420P10BE:
    case AV_PIX_FMT_YUVA422P9LE:
    case AV_PIX_FMT_YUVA422P9BE:
    case AV_PIX_FMT_YUVA422P10LE:
    case AV_PIX_FMT_YUVA422P10BE:
    case AV_PIX_FMT_YUVA444P9LE:
    case AV_PIX_FMT_YUVA444P9BE:
    case AV_PIX_FMT_YUVA444P10LE:
    case AV_PIX_FMT_YUVA444P10BE:
256 257 258 259
    case AV_PIX_FMT_GBRP9LE:
    case AV_PIX_FMT_GBRP9BE:
    case AV_PIX_FMT_GBRP10LE:
    case AV_PIX_FMT_GBRP10BE:
260 261 262 263
    case AV_PIX_FMT_GBRP12LE:
    case AV_PIX_FMT_GBRP12BE:
    case AV_PIX_FMT_GBRP14LE:
    case AV_PIX_FMT_GBRP14BE:
264 265
        w_align = 16; //FIXME assume 16 pixel per macroblock
        h_align = 16 * 2; // interlaced needs 2 macroblocks height
266
        break;
267
    case AV_PIX_FMT_YUV411P:
Michael Niedermayer's avatar
Michael Niedermayer committed
268
    case AV_PIX_FMT_YUVJ411P:
269
    case AV_PIX_FMT_UYYVYY411:
Luca Barbato's avatar
Luca Barbato committed
270 271
        w_align = 32;
        h_align = 8;
272
        break;
273
    case AV_PIX_FMT_YUV410P:
Luca Barbato's avatar
Luca Barbato committed
274 275 276
        if (s->codec_id == AV_CODEC_ID_SVQ1) {
            w_align = 64;
            h_align = 64;
277
        }
278
        break;
279
    case AV_PIX_FMT_RGB555:
Luca Barbato's avatar
Luca Barbato committed
280 281 282
        if (s->codec_id == AV_CODEC_ID_RPZA) {
            w_align = 4;
            h_align = 4;
283
        }
284
        break;
285 286 287
    case AV_PIX_FMT_PAL8:
    case AV_PIX_FMT_BGR8:
    case AV_PIX_FMT_RGB8:
288 289
        if (s->codec_id == AV_CODEC_ID_SMC ||
            s->codec_id == AV_CODEC_ID_CINEPAK) {
Luca Barbato's avatar
Luca Barbato committed
290 291
            w_align = 4;
            h_align = 4;
292
        }
293
        break;
294
    case AV_PIX_FMT_BGR24:
Luca Barbato's avatar
Luca Barbato committed
295 296 297 298
        if ((s->codec_id == AV_CODEC_ID_MSZH) ||
            (s->codec_id == AV_CODEC_ID_ZLIB)) {
            w_align = 4;
            h_align = 4;
299
        }
300 301 302 303 304 305
        break;
    case AV_PIX_FMT_RGB24:
        if (s->codec_id == AV_CODEC_ID_CINEPAK) {
            w_align = 4;
            h_align = 4;
        }
306
        break;
307
    default:
Luca Barbato's avatar
Luca Barbato committed
308 309
        w_align = 1;
        h_align = 1;
310 311 312
        break;
    }

313 314
    if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
        w_align = FFMAX(w_align, 8);
315 316
    }

Luca Barbato's avatar
Luca Barbato committed
317 318
    *width  = FFALIGN(*width, w_align);
    *height = FFALIGN(*height, h_align);
319
    if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
Luca Barbato's avatar
Luca Barbato committed
320
        // some of the optimized chroma MC reads one line too much
321
        // which is also done in mpeg decoders with lowres > 0
Luca Barbato's avatar
Luca Barbato committed
322
        *height += 2;
323

324
    for (i = 0; i < 4; i++)
325
        linesize_align[i] = STRIDE_ALIGN;
326 327
}

Luca Barbato's avatar
Luca Barbato committed
328 329
void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
{
330 331
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
    int chroma_shift = desc->log2_chroma_w;
332
    int linesize_align[AV_NUM_DATA_POINTERS];
333
    int align;
Luca Barbato's avatar
Luca Barbato committed
334

335
    avcodec_align_dimensions2(s, width, height, linesize_align);
Luca Barbato's avatar
Luca Barbato committed
336
    align               = FFMAX(linesize_align[0], linesize_align[3]);
337 338
    linesize_align[1] <<= chroma_shift;
    linesize_align[2] <<= chroma_shift;
Luca Barbato's avatar
Luca Barbato committed
339 340
    align               = FFMAX3(align, linesize_align[1], linesize_align[2]);
    *width              = FFALIGN(*width, align);
341 342
}

343 344 345 346 347 348 349 350 351 352 353 354
int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
{
    if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
        return AVERROR(EINVAL);
    pos--;

    *xpos = (pos&1) * 128;
    *ypos = ((pos>>1)^(pos<4)) * 128;

    return 0;
}

355 356 357 358 359 360 361 362 363 364 365
enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
{
    int pos, xout, yout;

    for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
        if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
            return pos;
    }
    return AVCHROMA_LOC_UNSPECIFIED;
}

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
                             enum AVSampleFormat sample_fmt, const uint8_t *buf,
                             int buf_size, int align)
{
    int ch, planar, needed_size, ret = 0;

    needed_size = av_samples_get_buffer_size(NULL, nb_channels,
                                             frame->nb_samples, sample_fmt,
                                             align);
    if (buf_size < needed_size)
        return AVERROR(EINVAL);

    planar = av_sample_fmt_is_planar(sample_fmt);
    if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
        if (!(frame->extended_data = av_mallocz(nb_channels *
                                                sizeof(*frame->extended_data))))
            return AVERROR(ENOMEM);
    } else {
        frame->extended_data = frame->data;
    }

    if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
388
                                      (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
389 390
                                      sample_fmt, align)) < 0) {
        if (frame->extended_data != frame->data)
391
            av_freep(&frame->extended_data);
392 393 394 395 396 397 398 399 400 401
        return ret;
    }
    if (frame->extended_data != frame->data) {
        for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
            frame->data[ch] = frame->extended_data[ch];
    }

    return ret;
}

402
static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
403
{
404 405
    FramePool *pool = avctx->internal->pool;
    int i, ret;
406

407 408
    switch (avctx->codec_type) {
    case AVMEDIA_TYPE_VIDEO: {
409
        AVPicture picture;
410 411 412 413
        int size[4] = { 0 };
        int w = frame->width;
        int h = frame->height;
        int tmpsize, unaligned;
414

415 416 417
        if (pool->format == frame->format &&
            pool->width == frame->width && pool->height == frame->height)
            return 0;
418

419
        avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
420

421
        if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
Luca Barbato's avatar
Luca Barbato committed
422 423
            w += EDGE_WIDTH * 2;
            h += EDGE_WIDTH * 2;
Michael Niedermayer's avatar
Michael Niedermayer committed
424
        }
425

426 427 428
        do {
            // NOTE: do not align linesizes individually, this breaks e.g. assumptions
            // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
429
            av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
430
            // increase alignment of w for next try (rhs gives the lowest bit set in w)
Luca Barbato's avatar
Luca Barbato committed
431
            w += w & ~(w - 1);
432

433
            unaligned = 0;
Luca Barbato's avatar
Luca Barbato committed
434
            for (i = 0; i < 4; i++)
435
                unaligned |= picture.linesize[i] % pool->stride_align[i];
436
        } while (unaligned);
437

438 439
        tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
                                         NULL, picture.linesize);
440 441
        if (tmpsize < 0)
            return -1;
442

Luca Barbato's avatar
Luca Barbato committed
443 444
        for (i = 0; i < 3 && picture.data[i + 1]; i++)
            size[i] = picture.data[i + 1] - picture.data[i];
445
        size[i] = tmpsize - (picture.data[i] - picture.data[0]);
446

447 448 449 450
        for (i = 0; i < 4; i++) {
            av_buffer_pool_uninit(&pool->pools[i]);
            pool->linesize[i] = picture.linesize[i];
            if (size[i]) {
451
                pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
452 453 454
                                                     CONFIG_MEMORY_POISONING ?
                                                        NULL :
                                                        av_buffer_allocz);
455 456 457 458 459 460 461 462 463
                if (!pool->pools[i]) {
                    ret = AVERROR(ENOMEM);
                    goto fail;
                }
            }
        }
        pool->format = frame->format;
        pool->width  = frame->width;
        pool->height = frame->height;
Michael Niedermayer's avatar
Michael Niedermayer committed
464

465 466 467
        break;
        }
    case AVMEDIA_TYPE_AUDIO: {
468
        int ch     = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
469 470 471 472 473 474 475 476 477 478 479 480
        int planar = av_sample_fmt_is_planar(frame->format);
        int planes = planar ? ch : 1;

        if (pool->format == frame->format && pool->planes == planes &&
            pool->channels == ch && frame->nb_samples == pool->samples)
            return 0;

        av_buffer_pool_uninit(&pool->pools[0]);
        ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
                                         frame->nb_samples, frame->format, 0);
        if (ret < 0)
            goto fail;
Michael Niedermayer's avatar
Michael Niedermayer committed
481

482 483 484 485 486
        pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
        if (!pool->pools[0]) {
            ret = AVERROR(ENOMEM);
            goto fail;
        }
487

488 489 490 491 492
        pool->format     = frame->format;
        pool->planes     = planes;
        pool->channels   = ch;
        pool->samples = frame->nb_samples;
        break;
Michael Niedermayer's avatar
Michael Niedermayer committed
493
        }
494 495 496 497 498 499 500 501 502 503 504
    default: av_assert0(0);
    }
    return 0;
fail:
    for (i = 0; i < 4; i++)
        av_buffer_pool_uninit(&pool->pools[i]);
    pool->format = -1;
    pool->planes = pool->channels = pool->samples = 0;
    pool->width  = pool->height = 0;
    return ret;
}
Michael Niedermayer's avatar
Michael Niedermayer committed
505

506 507 508 509 510
static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
{
    FramePool *pool = avctx->internal->pool;
    int planes = pool->planes;
    int i;
511

512 513 514 515 516 517 518 519 520 521 522
    frame->linesize[0] = pool->linesize[0];

    if (planes > AV_NUM_DATA_POINTERS) {
        frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
        frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
        frame->extended_buf  = av_mallocz(frame->nb_extended_buf *
                                          sizeof(*frame->extended_buf));
        if (!frame->extended_data || !frame->extended_buf) {
            av_freep(&frame->extended_data);
            av_freep(&frame->extended_buf);
            return AVERROR(ENOMEM);
523
        }
524
    } else {
525
        frame->extended_data = frame->data;
526 527
        av_assert0(frame->nb_extended_buf == 0);
    }
528 529 530 531 532 533

    for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
        frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
        if (!frame->buf[i])
            goto fail;
        frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
Michael Niedermayer's avatar
Michael Niedermayer committed
534
    }
535 536 537 538 539
    for (i = 0; i < frame->nb_extended_buf; i++) {
        frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
        if (!frame->extended_buf[i])
            goto fail;
        frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
Michael Niedermayer's avatar
Michael Niedermayer committed
540 541
    }

542 543 544 545 546 547 548 549
    if (avctx->debug & FF_DEBUG_BUFFERS)
        av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);

    return 0;
fail:
    av_frame_unref(frame);
    return AVERROR(ENOMEM);
}
Michael Niedermayer's avatar
Michael Niedermayer committed
550

551 552 553 554 555 556 557 558 559 560 561
static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
{
    FramePool *pool = s->internal->pool;
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
    int pixel_size = desc->comp[0].step_minus1 + 1;
    int h_chroma_shift, v_chroma_shift;
    int i;

    if (pic->data[0] != NULL) {
        av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
        return -1;
562
    }
563 564

    memset(pic->data, 0, sizeof(pic->data));
565
    pic->extended_data = pic->data;
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593

    av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);

    for (i = 0; i < 4 && pool->pools[i]; i++) {
        const int h_shift = i == 0 ? 0 : h_chroma_shift;
        const int v_shift = i == 0 ? 0 : v_chroma_shift;

        pic->linesize[i] = pool->linesize[i];

        pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
        if (!pic->buf[i])
            goto fail;

        // no edge if EDGE EMU or not planar YUV
        if ((s->flags & CODEC_FLAG_EMU_EDGE) || !pool->pools[2])
            pic->data[i] = pic->buf[i]->data;
        else {
            pic->data[i] = pic->buf[i]->data +
                FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
                        (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
        }
    }
    for (; i < AV_NUM_DATA_POINTERS; i++) {
        pic->data[i] = NULL;
        pic->linesize[i] = 0;
    }
    if (pic->data[1] && !pic->data[2])
        avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
594

Luca Barbato's avatar
Luca Barbato committed
595
    if (s->debug & FF_DEBUG_BUFFERS)
596
        av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
597

Michael Niedermayer's avatar
Michael Niedermayer committed
598
    return 0;
599 600 601
fail:
    av_frame_unref(pic);
    return AVERROR(ENOMEM);
Michael Niedermayer's avatar
Michael Niedermayer committed
602 603
}

604 605 606 607 608
void avpriv_color_frame(AVFrame *frame, const int c[4])
{
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
    int p, y, x;

609
    av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
610 611 612 613

    for (p = 0; p<desc->nb_components; p++) {
        uint8_t *dst = frame->data[p];
        int is_chroma = p == 1 || p == 2;
614 615 616
        int bytes  = is_chroma ? FF_CEIL_RSHIFT(frame->width,  desc->log2_chroma_w) : frame->width;
        int height = is_chroma ? FF_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
        for (y = 0; y < height; y++) {
617 618 619 620 621 622 623 624 625 626
            if (desc->comp[0].depth_minus1 >= 8) {
                for (x = 0; x<bytes; x++)
                    ((uint16_t*)dst)[x] = c[p];
            }else
                memset(dst, c[p], bytes);
            dst += frame->linesize[p];
        }
    }
}

627
int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
628
{
629 630 631 632 633 634
    int ret;

    if ((ret = update_frame_pool(avctx, frame)) < 0)
        return ret;

#if FF_API_GET_BUFFER
635
FF_DISABLE_DEPRECATION_WARNINGS
636
    frame->type = FF_BUFFER_TYPE_INTERNAL;
637
FF_ENABLE_DEPRECATION_WARNINGS
638 639
#endif

640 641 642 643 644 645 646 647 648 649
    switch (avctx->codec_type) {
    case AVMEDIA_TYPE_VIDEO:
        return video_get_buffer(avctx, frame);
    case AVMEDIA_TYPE_AUDIO:
        return audio_get_buffer(avctx, frame);
    default:
        return -1;
    }
}

650
int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
651
{
652 653 654 655 656
    if (avctx->pkt) {
        frame->pkt_pts = avctx->pkt->pts;
        av_frame_set_pkt_pos     (frame, avctx->pkt->pos);
        av_frame_set_pkt_duration(frame, avctx->pkt->duration);
        av_frame_set_pkt_size    (frame, avctx->pkt->size);
657 658
    } else {
        frame->pkt_pts = AV_NOPTS_VALUE;
659 660 661
        av_frame_set_pkt_pos     (frame, -1);
        av_frame_set_pkt_duration(frame, 0);
        av_frame_set_pkt_size    (frame, -1);
662
    }
663
    frame->reordered_opaque = avctx->reordered_opaque;
664

665
    switch (avctx->codec->type) {
666
    case AVMEDIA_TYPE_VIDEO:
667 668
        frame->width  = FFMAX(avctx->width,  FF_CEIL_RSHIFT(avctx->coded_width,  avctx->lowres));
        frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
669 670 671 672
        if (frame->format < 0)
            frame->format              = avctx->pix_fmt;
        if (!frame->sample_aspect_ratio.num)
            frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
673 674 675 676
        if (av_frame_get_colorspace(frame) == AVCOL_SPC_UNSPECIFIED)
            av_frame_set_colorspace(frame, avctx->colorspace);
        if (av_frame_get_color_range(frame) == AVCOL_RANGE_UNSPECIFIED)
            av_frame_set_color_range(frame, avctx->color_range);
677 678
        break;
    case AVMEDIA_TYPE_AUDIO:
679 680 681 682
        if (!frame->sample_rate)
            frame->sample_rate    = avctx->sample_rate;
        if (frame->format < 0)
            frame->format         = avctx->sample_fmt;
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
        if (!frame->channel_layout) {
            if (avctx->channel_layout) {
                 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
                     avctx->channels) {
                     av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
                            "configuration.\n");
                     return AVERROR(EINVAL);
                 }

                frame->channel_layout = avctx->channel_layout;
            } else {
                if (avctx->channels > FF_SANE_NB_CHANNELS) {
                    av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
                           avctx->channels);
                    return AVERROR(ENOSYS);
                }
            }
        }
701
        av_frame_set_channels(frame, avctx->channels);
702 703
        break;
    }
704
    return 0;
705
}
706

707
#if FF_API_GET_BUFFER
708
FF_DISABLE_DEPRECATION_WARNINGS
709
int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
710
{
711 712
    return avcodec_default_get_buffer2(avctx, frame, 0);
}
713

714 715 716 717 718 719 720 721
typedef struct CompatReleaseBufPriv {
    AVCodecContext avctx;
    AVFrame frame;
} CompatReleaseBufPriv;

static void compat_free_buffer(void *opaque, uint8_t *data)
{
    CompatReleaseBufPriv *priv = opaque;
722 723
    if (priv->avctx.release_buffer)
        priv->avctx.release_buffer(&priv->avctx, &priv->frame);
724
    av_freep(&priv);
725 726
}

727
static void compat_release_buffer(void *opaque, uint8_t *data)
Luca Barbato's avatar
Luca Barbato committed
728
{
729 730 731
    AVBufferRef *buf = opaque;
    av_buffer_unref(&buf);
}
732
FF_ENABLE_DEPRECATION_WARNINGS
733
#endif
734

735
static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
736 737 738
{
    int ret;

739 740 741 742 743
    if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
        if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
            av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
            return AVERROR(EINVAL);
        }
744
    }
745 746
    if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
        return ret;
747

748
#if FF_API_GET_BUFFER
749
FF_DISABLE_DEPRECATION_WARNINGS
750 751 752 753 754 755 756 757 758 759 760
    /*
     * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
     * We wrap each plane in its own AVBuffer. Each of those has a reference to
     * a dummy AVBuffer as its private data, unreffing it on free.
     * When all the planes are freed, the dummy buffer's free callback calls
     * release_buffer().
     */
    if (avctx->get_buffer) {
        CompatReleaseBufPriv *priv = NULL;
        AVBufferRef *dummy_buf = NULL;
        int planes, i, ret;
761

762 763
        if (flags & AV_GET_BUFFER_FLAG_REF)
            frame->reference    = 1;
764

765 766 767
        ret = avctx->get_buffer(avctx, frame);
        if (ret < 0)
            return ret;
768

769 770 771 772 773
        /* return if the buffers are already set up
         * this would happen e.g. when a custom get_buffer() calls
         * avcodec_default_get_buffer
         */
        if (frame->buf[0])
774
            goto end;
775 776 777 778 779

        priv = av_mallocz(sizeof(*priv));
        if (!priv) {
            ret = AVERROR(ENOMEM);
            goto fail;
780
        }
781 782
        priv->avctx = *avctx;
        priv->frame = *frame;
783

784 785 786 787
        dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
        if (!dummy_buf) {
            ret = AVERROR(ENOMEM);
            goto fail;
788
        }
789

790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
#define WRAP_PLANE(ref_out, data, data_size)                            \
do {                                                                    \
    AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf);                  \
    if (!dummy_ref) {                                                   \
        ret = AVERROR(ENOMEM);                                          \
        goto fail;                                                      \
    }                                                                   \
    ref_out = av_buffer_create(data, data_size, compat_release_buffer,  \
                               dummy_ref, 0);                           \
    if (!ref_out) {                                                     \
        av_frame_unref(frame);                                          \
        ret = AVERROR(ENOMEM);                                          \
        goto fail;                                                      \
    }                                                                   \
} while (0)

        if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
            const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);

809
            planes = av_pix_fmt_count_planes(frame->format);
810 811
            /* workaround for AVHWAccel plane count of 0, buf[0] is used as
               check for allocated buffers: make libavcodec happy */
812
            if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
813
                planes = 1;
814
            if (!desc || planes <= 0) {
815 816 817
                ret = AVERROR(EINVAL);
                goto fail;
            }
818

819
            for (i = 0; i < planes; i++) {
820 821
                int v_shift    = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
                int plane_size = (frame->height >> v_shift) * frame->linesize[i];
Michael Niedermayer's avatar
Michael Niedermayer committed
822

823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
                WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
            }
        } else {
            int planar = av_sample_fmt_is_planar(frame->format);
            planes = planar ? avctx->channels : 1;

            if (planes > FF_ARRAY_ELEMS(frame->buf)) {
                frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
                frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
                                                frame->nb_extended_buf);
                if (!frame->extended_buf) {
                    ret = AVERROR(ENOMEM);
                    goto fail;
                }
            }
838

839 840
            for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
                WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
841

842
            for (i = 0; i < frame->nb_extended_buf; i++)
843 844 845 846 847 848
                WRAP_PLANE(frame->extended_buf[i],
                           frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
                           frame->linesize[0]);
        }

        av_buffer_unref(&dummy_buf);
849

850
end:
851 852 853
        frame->width  = avctx->width;
        frame->height = avctx->height;

854
        return 0;
855

856 857 858 859 860
fail:
        avctx->release_buffer(avctx, frame);
        av_freep(&priv);
        av_buffer_unref(&dummy_buf);
        return ret;
861
    }
862
FF_ENABLE_DEPRECATION_WARNINGS
863 864
#endif

865 866 867 868 869 870 871 872
    ret = avctx->get_buffer2(avctx, frame, flags);

    if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
        frame->width  = avctx->width;
        frame->height = avctx->height;
    }

    return ret;
873 874
}

875 876 877 878 879 880 881 882 883
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
{
    int ret = get_buffer_internal(avctx, frame, flags);
    if (ret < 0)
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
    return ret;
}

static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
884 885 886
{
    AVFrame tmp;
    int ret;
887

888
    av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
889

890 891 892 893
    if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
        av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
               frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
        av_frame_unref(frame);
894 895
    }

896
    ff_init_buffer_info(avctx, frame);
897

898 899
    if (!frame->data[0])
        return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
900

901
    if (av_frame_is_writable(frame))
902 903
        return 0;

904 905 906 907 908
    av_frame_move_ref(&tmp, frame);

    ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
    if (ret < 0) {
        av_frame_unref(&tmp);
909
        return ret;
910
    }
911

912 913 914 915 916
    av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
                  frame->format, frame->width, frame->height);

    av_frame_unref(&tmp);

917 918 919
    return 0;
}

920 921 922 923 924 925 926 927
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
{
    int ret = reget_buffer_internal(avctx, frame);
    if (ret < 0)
        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
    return ret;
}

928 929 930
#if FF_API_GET_BUFFER
void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
{
931 932
    av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);

933 934 935 936 937 938
    av_frame_unref(pic);
}

int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
{
    av_assert0(0);
939
    return AVERROR_BUG;
940 941 942
}
#endif

Luca Barbato's avatar
Luca Barbato committed
943 944
int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
{
945 946
    int i;

Luca Barbato's avatar
Luca Barbato committed
947 948 949 950
    for (i = 0; i < count; i++) {
        int r = func(c, (char *)arg + i * size);
        if (ret)
            ret[i] = r;
951 952 953 954
    }
    return 0;
}

Luca Barbato's avatar
Luca Barbato committed
955 956
int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
{
957 958
    int i;

Luca Barbato's avatar
Luca Barbato committed
959 960 961 962
    for (i = 0; i < count; i++) {
        int r = func(c, arg, i, 0);
        if (ret)
            ret[i] = r;
963 964 965 966
    }
    return 0;
}

967 968 969
static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
{
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
970
    return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
971 972
}

973
enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
Luca Barbato's avatar
Luca Barbato committed
974
{
975
    while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
976
        ++fmt;
Michael Niedermayer's avatar
Michael Niedermayer committed
977 978 979
    return fmt[0];
}

980
void avcodec_get_frame_defaults(AVFrame *frame)
Luca Barbato's avatar
Luca Barbato committed
981
{
982 983 984 985
#if LIBAVCODEC_VERSION_MAJOR >= 55
     // extended_data should explicitly be freed when needed, this code is unsafe currently
     // also this is not compatible to the <55 ABI/API
    if (frame->extended_data != frame->data && 0)
986
        av_freep(&frame->extended_data);
987
#endif
988

989
    memset(frame, 0, sizeof(AVFrame));
990

991 992
    frame->pts                   =
    frame->pkt_dts               =
993 994 995 996 997
    frame->pkt_pts               = AV_NOPTS_VALUE;
    av_frame_set_best_effort_timestamp(frame, AV_NOPTS_VALUE);
    av_frame_set_pkt_duration         (frame, 0);
    av_frame_set_pkt_pos              (frame, -1);
    av_frame_set_pkt_size             (frame, -1);
998 999 1000
    frame->key_frame           = 1;
    frame->sample_aspect_ratio = (AVRational) {0, 1 };
    frame->format              = -1; /* unknown */
1001
    frame->extended_data       = frame->data;
1002
    av_frame_set_colorspace(frame, AVCOL_SPC_UNSPECIFIED);
1003 1004
}

Luca Barbato's avatar
Luca Barbato committed
1005 1006
AVFrame *avcodec_alloc_frame(void)
{
1007
    AVFrame *frame = av_malloc(sizeof(AVFrame));
1008

1009
    if (frame == NULL)
Luca Barbato's avatar
Luca Barbato committed
1010
        return NULL;
1011

1012
    frame->extended_data = NULL;
1013
    avcodec_get_frame_defaults(frame);
1014

1015
    return frame;
Michael Niedermayer's avatar
Michael Niedermayer committed
1016 1017
}

1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
void avcodec_free_frame(AVFrame **frame)
{
    AVFrame *f;

    if (!frame || !*frame)
        return;

    f = *frame;

    if (f->extended_data != f->data)
        av_freep(&f->extended_data);

    av_freep(frame);
Michael Niedermayer's avatar
Michael Niedermayer committed
1031 1032
}

1033
MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
1034
MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
1035
MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
1036

1037 1038 1039 1040 1041 1042
static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
{
    memset(sub, 0, sizeof(*sub));
    sub->pts = AV_NOPTS_VALUE;
}

1043 1044 1045 1046 1047
static int get_bit_rate(AVCodecContext *ctx)
{
    int bit_rate;
    int bits_per_sample;

1048
    switch (ctx->codec_type) {
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
    case AVMEDIA_TYPE_VIDEO:
    case AVMEDIA_TYPE_DATA:
    case AVMEDIA_TYPE_SUBTITLE:
    case AVMEDIA_TYPE_ATTACHMENT:
        bit_rate = ctx->bit_rate;
        break;
    case AVMEDIA_TYPE_AUDIO:
        bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
        bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
        break;
    default:
        bit_rate = 0;
        break;
    }
    return bit_rate;
}

1066
#if FF_API_AVCODEC_OPEN
1067
int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
1068 1069 1070 1071 1072
{
    return avcodec_open2(avctx, codec, NULL);
}
#endif

1073 1074 1075 1076
int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
{
    int ret = 0;

1077
    ff_unlock_avcodec();
1078 1079 1080

    ret = avcodec_open2(avctx, codec, options);

1081
    ff_lock_avcodec(avctx);
1082 1083 1084
    return ret;
}

1085
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Fabrice Bellard's avatar
Fabrice Bellard committed
1086
{
1087
    int ret = 0;
1088 1089
    AVDictionary *tmp = NULL;

1090 1091 1092
    if (avcodec_is_open(avctx))
        return 0;

1093
    if ((!codec && !avctx->codec)) {
1094
        av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
1095 1096 1097 1098
        return AVERROR(EINVAL);
    }
    if ((codec && avctx->codec && codec != avctx->codec)) {
        av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
1099
                                    "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
1100 1101 1102 1103 1104
        return AVERROR(EINVAL);
    }
    if (!codec)
        codec = avctx->codec;

1105 1106 1107
    if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
        return AVERROR(EINVAL);

1108 1109
    if (options)
        av_dict_copy(&tmp, *options, 0);
1110

1111 1112
    ret = ff_lock_avcodec(avctx);
    if (ret < 0)
1113
        return ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
1114

1115 1116 1117 1118 1119 1120
    avctx->internal = av_mallocz(sizeof(AVCodecInternal));
    if (!avctx->internal) {
        ret = AVERROR(ENOMEM);
        goto end;
    }

1121 1122 1123 1124 1125 1126
    avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
    if (!avctx->internal->pool) {
        ret = AVERROR(ENOMEM);
        goto free_and_end;
    }

1127
    if (codec->priv_data_size > 0) {
1128
        if (!avctx->priv_data) {
Luca Barbato's avatar
Luca Barbato committed
1129 1130 1131 1132 1133 1134 1135 1136 1137
            avctx->priv_data = av_mallocz(codec->priv_data_size);
            if (!avctx->priv_data) {
                ret = AVERROR(ENOMEM);
                goto end;
            }
            if (codec->priv_class) {
                *(const AVClass **)avctx->priv_data = codec->priv_class;
                av_opt_set_defaults(avctx->priv_data);
            }
1138
        }
Luca Barbato's avatar
Luca Barbato committed
1139 1140
        if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
            goto free_and_end;
1141 1142 1143
    } else {
        avctx->priv_data = NULL;
    }
1144 1145
    if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
        goto free_and_end;
1146

1147 1148 1149
    // only call avcodec_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
    if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
          (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
Luca Barbato's avatar
Luca Barbato committed
1150
    if (avctx->coded_width && avctx->coded_height)
1151
        avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
Luca Barbato's avatar
Luca Barbato committed
1152
    else if (avctx->width && avctx->height)
1153
        avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1154
    }
1155

1156 1157 1158
    if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
        && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
           || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
1159
        av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
1160 1161 1162
        avcodec_set_dimensions(avctx, 0, 0);
    }

1163
    /* if the decoder init function was already called previously,
Luca Barbato's avatar
Luca Barbato committed
1164
     * free the already allocated subtitle_header before overwriting it */
1165
    if (av_codec_is_decoder(codec))
1166 1167
        av_freep(&avctx->subtitle_header);

1168
    if (avctx->channels > FF_SANE_NB_CHANNELS) {
1169
        ret = AVERROR(EINVAL);
1170
        goto free_and_end;
1171 1172
    }

1173
    avctx->codec = codec;
1174
    if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1175
        avctx->codec_id == AV_CODEC_ID_NONE) {
1176 1177 1178
        avctx->codec_type = codec->type;
        avctx->codec_id   = codec->id;
    }
1179
    if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
Luca Barbato's avatar
Luca Barbato committed
1180
                                         && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1181
        av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
1182
        ret = AVERROR(EINVAL);
1183
        goto free_and_end;
1184
    }
1185
    avctx->frame_number = 0;
1186
    avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
1187

1188 1189
    if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
        avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1190 1191
        const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
        AVCodec *codec2;
1192
        av_log(avctx, AV_LOG_ERROR,