utils.c 65.6 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
 * This file is part of Libav.
7
 *
8
 * Libav is free software; you can redistribute it and/or
Fabrice Bellard's avatar
Fabrice Bellard committed
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
 * Libav 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
Fabrice Bellard's avatar
Fabrice Bellard committed
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
 *
Fabrice Bellard's avatar
Fabrice Bellard committed
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
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 "libavutil/avassert.h"
29
#include "libavutil/avstring.h"
30
#include "libavutil/crc.h"
31
#include "libavutil/mathematics.h"
32
#include "libavutil/pixdesc.h"
33 34 35
#include "libavutil/audioconvert.h"
#include "libavutil/imgutils.h"
#include "libavutil/samplefmt.h"
36
#include "libavutil/dict.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
37
#include "avcodec.h"
38
#include "dsputil.h"
39
#include "libavutil/opt.h"
40
#include "thread.h"
41
#include "audioconvert.h"
42
#include "internal.h"
43
#include "bytestream.h"
44
#include <stdlib.h>
45
#include <stdarg.h>
46
#include <limits.h>
Michael Niedermayer's avatar
Michael Niedermayer committed
47
#include <float.h>
Fabrice Bellard's avatar
Fabrice Bellard committed
48

Luca Barbato's avatar
Luca Barbato committed
49
static int volatile entangled_thread_counter = 0;
50
static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
51
static void *codec_mutex;
52
static void *avformat_mutex;
53

54
void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
55
{
Luca Barbato's avatar
Luca Barbato committed
56
    if (min_size < *size)
57
        return ptr;
58

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

Luca Barbato's avatar
Luca Barbato committed
61 62 63 64 65 66
    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;
67

Luca Barbato's avatar
Luca Barbato committed
68
    *size = min_size;
69 70

    return ptr;
71 72
}

73
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
74 75 76 77
{
    void **p = ptr;
    if (min_size < *size)
        return;
Luca Barbato's avatar
Luca Barbato committed
78
    min_size = FFMAX(17 * min_size / 16 + 32, min_size);
79
    av_free(*p);
80
    *p = av_malloc(min_size);
Luca Barbato's avatar
Luca Barbato committed
81 82 83
    if (!*p)
        min_size = 0;
    *size = min_size;
84 85
}

86 87 88 89 90 91 92 93 94 95 96 97 98
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
{
    void **p = ptr;
    if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
        av_freep(p);
        *size = 0;
        return;
    }
    av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
    if (*size)
        memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
}

Fabrice Bellard's avatar
Fabrice Bellard committed
99
/* encoder management */
100
static AVCodec *first_avcodec = NULL;
Fabrice Bellard's avatar
Fabrice Bellard committed
101

102 103
AVCodec *av_codec_next(const AVCodec *c)
{
Luca Barbato's avatar
Luca Barbato committed
104 105 106 107
    if (c)
        return c->next;
    else
        return first_avcodec;
108 109
}

110
static void avcodec_init(void)
111 112 113 114 115 116 117
{
    static int initialized = 0;

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

118
    ff_dsputil_static_init();
119 120
}

121
int av_codec_is_encoder(const AVCodec *codec)
122
{
123
    return codec && (codec->encode_sub || codec->encode2);
124 125
}

126
int av_codec_is_decoder(const AVCodec *codec)
127 128 129 130
{
    return codec && codec->decode;
}

131
void avcodec_register(AVCodec *codec)
Fabrice Bellard's avatar
Fabrice Bellard committed
132 133
{
    AVCodec **p;
134
    avcodec_init();
Fabrice Bellard's avatar
Fabrice Bellard committed
135
    p = &first_avcodec;
Luca Barbato's avatar
Luca Barbato committed
136 137 138
    while (*p != NULL)
        p = &(*p)->next;
    *p          = codec;
139
    codec->next = NULL;
140 141 142

    if (codec->init_static_data)
        codec->init_static_data(codec);
Fabrice Bellard's avatar
Fabrice Bellard committed
143 144
}

145 146 147 148 149
unsigned avcodec_get_edge_width(void)
{
    return EDGE_WIDTH;
}

Luca Barbato's avatar
Luca Barbato committed
150 151 152 153 154 155
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
{
    s->coded_width  = width;
    s->coded_height = height;
    s->width        = width;
    s->height       = height;
156 157
}

Luca Barbato's avatar
Luca Barbato committed
158
#define INTERNAL_BUFFER_SIZE (32 + 1)
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
159

160 161 162 163
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
164 165
    int w_align = 1;
    int h_align = 1;
166

Luca Barbato's avatar
Luca Barbato committed
167
    switch (s->pix_fmt) {
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    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:
    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:
    case AV_PIX_FMT_YUV420P9LE:
    case AV_PIX_FMT_YUV420P9BE:
    case AV_PIX_FMT_YUV420P10LE:
    case AV_PIX_FMT_YUV420P10BE:
    case AV_PIX_FMT_YUV422P9LE:
    case AV_PIX_FMT_YUV422P9BE:
    case AV_PIX_FMT_YUV422P10LE:
    case AV_PIX_FMT_YUV422P10BE:
    case AV_PIX_FMT_YUV444P9LE:
    case AV_PIX_FMT_YUV444P9BE:
    case AV_PIX_FMT_YUV444P10LE:
    case AV_PIX_FMT_YUV444P10BE:
    case AV_PIX_FMT_GBRP9LE:
    case AV_PIX_FMT_GBRP9BE:
    case AV_PIX_FMT_GBRP10LE:
    case AV_PIX_FMT_GBRP10BE:
199 200
        w_align = 16; //FIXME assume 16 pixel per macroblock
        h_align = 16 * 2; // interlaced needs 2 macroblocks height
201
        break;
202 203
    case AV_PIX_FMT_YUV411P:
    case AV_PIX_FMT_UYYVYY411:
Luca Barbato's avatar
Luca Barbato committed
204 205
        w_align = 32;
        h_align = 8;
206
        break;
207
    case AV_PIX_FMT_YUV410P:
Luca Barbato's avatar
Luca Barbato committed
208 209 210
        if (s->codec_id == AV_CODEC_ID_SVQ1) {
            w_align = 64;
            h_align = 64;
211
        }
212
    case AV_PIX_FMT_RGB555:
Luca Barbato's avatar
Luca Barbato committed
213 214 215
        if (s->codec_id == AV_CODEC_ID_RPZA) {
            w_align = 4;
            h_align = 4;
216
        }
217 218 219
    case AV_PIX_FMT_PAL8:
    case AV_PIX_FMT_BGR8:
    case AV_PIX_FMT_RGB8:
Luca Barbato's avatar
Luca Barbato committed
220 221 222
        if (s->codec_id == AV_CODEC_ID_SMC) {
            w_align = 4;
            h_align = 4;
223
        }
224
        break;
225
    case AV_PIX_FMT_BGR24:
Luca Barbato's avatar
Luca Barbato committed
226 227 228 229
        if ((s->codec_id == AV_CODEC_ID_MSZH) ||
            (s->codec_id == AV_CODEC_ID_ZLIB)) {
            w_align = 4;
            h_align = 4;
230 231
        }
        break;
232
    default:
Luca Barbato's avatar
Luca Barbato committed
233 234
        w_align = 1;
        h_align = 1;
235 236 237
        break;
    }

Luca Barbato's avatar
Luca Barbato committed
238 239
    *width  = FFALIGN(*width, w_align);
    *height = FFALIGN(*height, h_align);
240
    if (s->codec_id == AV_CODEC_ID_H264)
Luca Barbato's avatar
Luca Barbato committed
241 242
        // some of the optimized chroma MC reads one line too much
        *height += 2;
243

244
    for (i = 0; i < 4; i++)
245
        linesize_align[i] = STRIDE_ALIGN;
246 247
}

Luca Barbato's avatar
Luca Barbato committed
248 249
void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
{
250 251
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
    int chroma_shift = desc->log2_chroma_w;
252
    int linesize_align[AV_NUM_DATA_POINTERS];
253
    int align;
Luca Barbato's avatar
Luca Barbato committed
254

255
    avcodec_align_dimensions2(s, width, height, linesize_align);
Luca Barbato's avatar
Luca Barbato committed
256
    align               = FFMAX(linesize_align[0], linesize_align[3]);
257 258
    linesize_align[1] <<= chroma_shift;
    linesize_align[2] <<= chroma_shift;
Luca Barbato's avatar
Luca Barbato committed
259 260
    align               = FFMAX3(align, linesize_align[1], linesize_align[2]);
    *width              = FFALIGN(*width, align);
261 262
}

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
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],
                                      buf, nb_channels, frame->nb_samples,
                                      sample_fmt, align)) < 0) {
        if (frame->extended_data != frame->data)
            av_free(frame->extended_data);
        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;
}

Justin Ruggles's avatar
Justin Ruggles committed
299 300 301 302
static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
{
    AVCodecInternal *avci = avctx->internal;
    InternalBuffer *buf;
303
    int buf_size, ret;
Justin Ruggles's avatar
Justin Ruggles committed
304 305 306

    buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
                                          frame->nb_samples, avctx->sample_fmt,
307
                                          0);
Justin Ruggles's avatar
Justin Ruggles committed
308 309 310 311 312 313 314 315 316 317 318 319
    if (buf_size < 0)
        return AVERROR(EINVAL);

    /* allocate InternalBuffer if needed */
    if (!avci->buffer) {
        avci->buffer = av_mallocz(sizeof(InternalBuffer));
        if (!avci->buffer)
            return AVERROR(ENOMEM);
    }
    buf = avci->buffer;

    /* if there is a previously-used internal buffer, check its size and
Luca Barbato's avatar
Luca Barbato committed
320
     * channel count to see if we can reuse it */
Justin Ruggles's avatar
Justin Ruggles committed
321 322 323 324 325
    if (buf->extended_data) {
        /* if current buffer is too small, free it */
        if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
            av_free(buf->extended_data[0]);
            if (buf->extended_data != buf->data)
326
                av_free(buf->extended_data);
Justin Ruggles's avatar
Justin Ruggles committed
327
            buf->extended_data = NULL;
Luca Barbato's avatar
Luca Barbato committed
328
            buf->data[0]       = NULL;
Justin Ruggles's avatar
Justin Ruggles committed
329 330
        }
        /* if number of channels has changed, reset and/or free extended data
Luca Barbato's avatar
Luca Barbato committed
331
         * pointers but leave data buffer in buf->data[0] for reuse */
Justin Ruggles's avatar
Justin Ruggles committed
332 333 334 335 336 337 338 339
        if (buf->nb_channels != avctx->channels) {
            if (buf->extended_data != buf->data)
                av_free(buf->extended_data);
            buf->extended_data = NULL;
        }
    }

    /* if there is no previous buffer or the previous buffer cannot be used
Luca Barbato's avatar
Luca Barbato committed
340
     * as-is, allocate a new buffer and/or rearrange the channel pointers */
Justin Ruggles's avatar
Justin Ruggles committed
341
    if (!buf->extended_data) {
342 343
        if (!buf->data[0]) {
            if (!(buf->data[0] = av_mallocz(buf_size)))
Justin Ruggles's avatar
Justin Ruggles committed
344
                return AVERROR(ENOMEM);
345
            buf->audio_data_size = buf_size;
Justin Ruggles's avatar
Justin Ruggles committed
346
        }
347 348
        if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
                                            avctx->sample_fmt, buf->data[0],
349
                                            buf->audio_data_size, 0)))
Justin Ruggles's avatar
Justin Ruggles committed
350 351
            return ret;

352 353 354 355 356 357 358 359 360 361 362 363
        if (frame->extended_data == frame->data)
            buf->extended_data = buf->data;
        else
            buf->extended_data = frame->extended_data;
        memcpy(buf->data, frame->data, sizeof(frame->data));
        buf->linesize[0] = frame->linesize[0];
        buf->nb_channels = avctx->channels;
    } else {
        /* copy InternalBuffer info to the AVFrame */
        frame->extended_data = buf->extended_data;
        frame->linesize[0]   = buf->linesize[0];
        memcpy(frame->data, buf->data, sizeof(frame->data));
Justin Ruggles's avatar
Justin Ruggles committed
364 365
    }

Luca Barbato's avatar
Luca Barbato committed
366
    frame->type = FF_BUFFER_TYPE_INTERNAL;
Justin Ruggles's avatar
Justin Ruggles committed
367

Luca Barbato's avatar
Luca Barbato committed
368 369 370 371
    if (avctx->pkt)
        frame->pkt_pts = avctx->pkt->pts;
    else
        frame->pkt_pts = AV_NOPTS_VALUE;
Justin Ruggles's avatar
Justin Ruggles committed
372 373
    frame->reordered_opaque = avctx->reordered_opaque;

374 375 376 377
    frame->sample_rate    = avctx->sample_rate;
    frame->format         = avctx->sample_fmt;
    frame->channel_layout = avctx->channel_layout;

Justin Ruggles's avatar
Justin Ruggles committed
378 379
    if (avctx->debug & FF_DEBUG_BUFFERS)
        av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
Luca Barbato's avatar
Luca Barbato committed
380
                                    "internal audio buffer used\n", frame);
Justin Ruggles's avatar
Justin Ruggles committed
381 382 383 384 385 386

    return 0;
}

static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
{
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
387
    int i;
Luca Barbato's avatar
Luca Barbato committed
388 389
    int w = s->width;
    int h = s->height;
390
    InternalBuffer *buf;
391
    AVCodecInternal *avci = s->internal;
392

Luca Barbato's avatar
Luca Barbato committed
393
    if (pic->data[0] != NULL) {
394 395 396
        av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
        return -1;
    }
Luca Barbato's avatar
Luca Barbato committed
397
    if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
398
        av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
399 400
        return -1;
    }
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
401

Luca Barbato's avatar
Luca Barbato committed
402
    if (av_image_check_size(w, h, 0, s))
403 404
        return -1;

405
    if (!avci->buffer) {
Luca Barbato's avatar
Luca Barbato committed
406
        avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
407
                                  sizeof(InternalBuffer));
408
    }
409

410
    buf = &avci->buffer[avci->buffer_count];
411

Luca Barbato's avatar
Luca Barbato committed
412
    if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
413
        for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
414
            av_freep(&buf->base[i]);
Luca Barbato's avatar
Luca Barbato committed
415
            buf->data[i] = NULL;
416 417 418
        }
    }

Mans Rullgard's avatar
Mans Rullgard committed
419
    if (!buf->base[0]) {
420
        int h_chroma_shift, v_chroma_shift;
Luca Barbato's avatar
Luca Barbato committed
421
        int size[4] = { 0 };
422
        int tmpsize;
423
        int unaligned;
424
        AVPicture picture;
425
        int stride_align[AV_NUM_DATA_POINTERS];
426 427
        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
        const int pixel_size = desc->comp[0].step_minus1 + 1;
428

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
429
        avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
430

431
        avcodec_align_dimensions2(s, &w, &h, stride_align);
432

Luca Barbato's avatar
Luca Barbato committed
433 434 435
        if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
            w += EDGE_WIDTH * 2;
            h += EDGE_WIDTH * 2;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
436
        }
Loren Merritt's avatar
Loren Merritt committed
437

438 439 440
        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
441
            av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
442
            // increase alignment of w for next try (rhs gives the lowest bit set in w)
Luca Barbato's avatar
Luca Barbato committed
443
            w += w & ~(w - 1);
444

445
            unaligned = 0;
Luca Barbato's avatar
Luca Barbato committed
446
            for (i = 0; i < 4; i++)
447 448
                unaligned |= picture.linesize[i] % stride_align[i];
        } while (unaligned);
449

450
        tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
451 452
        if (tmpsize < 0)
            return -1;
453

Luca Barbato's avatar
Luca Barbato committed
454 455
        for (i = 0; i < 3 && picture.data[i + 1]; i++)
            size[i] = picture.data[i + 1] - picture.data[i];
Vitor Sessak's avatar
Vitor Sessak committed
456
        size[i] = tmpsize - (picture.data[i] - picture.data[0]);
457 458 459

        memset(buf->base, 0, sizeof(buf->base));
        memset(buf->data, 0, sizeof(buf->data));
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
460

Luca Barbato's avatar
Luca Barbato committed
461 462 463
        for (i = 0; i < 4 && size[i]; i++) {
            const int h_shift = i == 0 ? 0 : h_chroma_shift;
            const int v_shift = i == 0 ? 0 : v_chroma_shift;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
464

Luca Barbato's avatar
Luca Barbato committed
465
            buf->linesize[i] = picture.linesize[i];
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
466

Luca Barbato's avatar
Luca Barbato committed
467 468 469
            buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
            if (buf->base[i] == NULL)
                return -1;
470 471
            memset(buf->base[i], 128, size[i]);

Jai Menon's avatar
Jai Menon committed
472
            // no edge if EDGE EMU or not planar YUV
Luca Barbato's avatar
Luca Barbato committed
473
            if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
474
                buf->data[i] = buf->base[i];
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
475
            else
Luca Barbato's avatar
Luca Barbato committed
476
                buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
477
        }
478
        for (; i < AV_NUM_DATA_POINTERS; i++) {
Luca Barbato's avatar
Luca Barbato committed
479
            buf->base[i]     = buf->data[i] = NULL;
480 481
            buf->linesize[i] = 0;
        }
Luca Barbato's avatar
Luca Barbato committed
482 483 484 485 486
        if (size[1] && !size[2])
            ff_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
        buf->width   = s->width;
        buf->height  = s->height;
        buf->pix_fmt = s->pix_fmt;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
487
    }
Luca Barbato's avatar
Luca Barbato committed
488
    pic->type = FF_BUFFER_TYPE_INTERNAL;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
489

490
    for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
Luca Barbato's avatar
Luca Barbato committed
491 492 493
        pic->base[i]     = buf->base[i];
        pic->data[i]     = buf->data[i];
        pic->linesize[i] = buf->linesize[i];
494
    }
Justin Ruggles's avatar
Justin Ruggles committed
495
    pic->extended_data = pic->data;
496
    avci->buffer_count++;
Luca Barbato's avatar
Luca Barbato committed
497 498 499
    pic->width               = buf->width;
    pic->height              = buf->height;
    pic->format              = buf->pix_fmt;
500
    pic->sample_aspect_ratio = s->sample_aspect_ratio;
501

Luca Barbato's avatar
Luca Barbato committed
502 503 504 505 506
    if (s->pkt)
        pic->pkt_pts = s->pkt->pts;
    else
        pic->pkt_pts = AV_NOPTS_VALUE;
    pic->reordered_opaque = s->reordered_opaque;
507

Luca Barbato's avatar
Luca Barbato committed
508
    if (s->debug & FF_DEBUG_BUFFERS)
509
        av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
Luca Barbato's avatar
Luca Barbato committed
510
                                "buffers used\n", pic, avci->buffer_count);
511

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
512 513 514
    return 0;
}

Justin Ruggles's avatar
Justin Ruggles committed
515 516 517 518 519 520 521 522 523 524 525 526
int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
{
    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;
    }
}

Luca Barbato's avatar
Luca Barbato committed
527 528
void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
{
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
529
    int i;
530
    InternalBuffer *buf, *last;
531
    AVCodecInternal *avci = s->internal;
532

Justin Ruggles's avatar
Justin Ruggles committed
533 534
    assert(s->codec_type == AVMEDIA_TYPE_VIDEO);

Luca Barbato's avatar
Luca Barbato committed
535
    assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
536 537 538 539 540 541 542 543 544 545 546 547
    assert(avci->buffer_count);

    if (avci->buffer) {
        buf = NULL; /* avoids warning */
        for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
            buf = &avci->buffer[i];
            if (buf->data[0] == pic->data[0])
                break;
        }
        assert(i < avci->buffer_count);
        avci->buffer_count--;
        last = &avci->buffer[avci->buffer_count];
548

549 550
        if (buf != last)
            FFSWAP(InternalBuffer, *buf, *last);
551
    }
552

Luca Barbato's avatar
Luca Barbato committed
553 554
    for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
        pic->data[i] = NULL;
555
//        pic->base[i]=NULL;
556

Luca Barbato's avatar
Luca Barbato committed
557
    if (s->debug & FF_DEBUG_BUFFERS)
558
        av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
Luca Barbato's avatar
Luca Barbato committed
559
                                "buffers used\n", pic, avci->buffer_count);
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
560 561
}

Luca Barbato's avatar
Luca Barbato committed
562 563
int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
{
564 565 566
    AVFrame temp_pic;
    int i;

Justin Ruggles's avatar
Justin Ruggles committed
567 568
    assert(s->codec_type == AVMEDIA_TYPE_VIDEO);

569
    /* If no picture return a new buffer */
Luca Barbato's avatar
Luca Barbato committed
570
    if (pic->data[0] == NULL) {
571 572 573 574 575
        /* We will copy from buffer, so must be readable */
        pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
        return s->get_buffer(s, pic);
    }

576
    assert(s->pix_fmt == pic->format);
577

578
    /* If internal buffer type return the same buffer */
Luca Barbato's avatar
Luca Barbato committed
579 580 581 582 583 584
    if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
        if (s->pkt)
            pic->pkt_pts = s->pkt->pts;
        else
            pic->pkt_pts = AV_NOPTS_VALUE;
        pic->reordered_opaque = s->reordered_opaque;
585
        return 0;
586
    }
587 588 589 590 591

    /*
     * Not internal type and reget_buffer not overridden, emulate cr buffer
     */
    temp_pic = *pic;
Luca Barbato's avatar
Luca Barbato committed
592
    for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
593 594 595 596 597 598
        pic->data[i] = pic->base[i] = NULL;
    pic->opaque = NULL;
    /* Allocate new frame */
    if (s->get_buffer(s, pic))
        return -1;
    /* Copy image data from old buffer to new buffer */
Luca Barbato's avatar
Luca Barbato committed
599 600
    av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
                    s->height);
601 602 603 604
    s->release_buffer(s, &temp_pic); // Release old frame
    return 0;
}

Luca Barbato's avatar
Luca Barbato committed
605 606
int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
{
607 608
    int i;

Luca Barbato's avatar
Luca Barbato committed
609 610 611 612
    for (i = 0; i < count; i++) {
        int r = func(c, (char *)arg + i * size);
        if (ret)
            ret[i] = r;
613 614 615 616
    }
    return 0;
}

Luca Barbato's avatar
Luca Barbato committed
617 618
int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
{
619 620
    int i;

Luca Barbato's avatar
Luca Barbato committed
621 622 623 624
    for (i = 0; i < count; i++) {
        int r = func(c, arg, i, 0);
        if (ret)
            ret[i] = r;
625 626 627 628
    }
    return 0;
}

629
enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
Luca Barbato's avatar
Luca Barbato committed
630
{
631
    while (*fmt != AV_PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
632
        ++fmt;
Michael Niedermayer's avatar
Michael Niedermayer committed
633 634 635
    return fmt[0];
}

636
void avcodec_get_frame_defaults(AVFrame *frame)
Luca Barbato's avatar
Luca Barbato committed
637
{
638 639 640
    if (frame->extended_data != frame->data)
        av_freep(&frame->extended_data);

641
    memset(frame, 0, sizeof(AVFrame));
642

643 644 645 646
    frame->pts                 = AV_NOPTS_VALUE;
    frame->key_frame           = 1;
    frame->sample_aspect_ratio = (AVRational) {0, 1 };
    frame->format              = -1; /* unknown */
647
    frame->extended_data       = frame->data;
648 649
}

Luca Barbato's avatar
Luca Barbato committed
650 651
AVFrame *avcodec_alloc_frame(void)
{
652
    AVFrame *frame = av_mallocz(sizeof(AVFrame));
653

654
    if (frame == NULL)
Luca Barbato's avatar
Luca Barbato committed
655
        return NULL;
656

657
    avcodec_get_frame_defaults(frame);
658

659
    return frame;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
660 661
}

662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
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);
}

677
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Fabrice Bellard's avatar
Fabrice Bellard committed
678
{
679
    int ret = 0;
680 681
    AVDictionary *tmp = NULL;

Anton Khirnov's avatar
Anton Khirnov committed
682 683 684
    if (avcodec_is_open(avctx))
        return 0;

685 686 687 688 689 690
    if ((!codec && !avctx->codec)) {
        av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
        return AVERROR(EINVAL);
    }
    if ((codec && avctx->codec && codec != avctx->codec)) {
        av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
Luca Barbato's avatar
Luca Barbato committed
691
                                    "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
692 693 694 695 696
        return AVERROR(EINVAL);
    }
    if (!codec)
        codec = avctx->codec;

697 698 699
    if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
        return AVERROR(EINVAL);

700 701
    if (options)
        av_dict_copy(&tmp, *options, 0);
702

703 704 705 706 707 708
    /* If there is a user-supplied mutex locking routine, call it. */
    if (ff_lockmgr_cb) {
        if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
            return -1;
    }

709
    entangled_thread_counter++;
Luca Barbato's avatar
Luca Barbato committed
710
    if (entangled_thread_counter != 1) {
711
        av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
712
        ret = -1;
713 714
        goto end;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
715

716 717 718 719 720 721
    avctx->internal = av_mallocz(sizeof(AVCodecInternal));
    if (!avctx->internal) {
        ret = AVERROR(ENOMEM);
        goto end;
    }

722
    if (codec->priv_data_size > 0) {
723
        if (!avctx->priv_data) {
Luca Barbato's avatar
Luca Barbato committed
724 725 726 727 728 729 730 731 732
            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);
            }
733
        }
Luca Barbato's avatar
Luca Barbato committed
734 735
        if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
            goto free_and_end;
736 737 738
    } else {
        avctx->priv_data = NULL;
    }
739 740
    if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
        goto free_and_end;
741

Luca Barbato's avatar
Luca Barbato committed
742
    if (avctx->coded_width && avctx->coded_height)
743
        avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
Luca Barbato's avatar
Luca Barbato committed
744
    else if (avctx->width && avctx->height)
745 746
        avcodec_set_dimensions(avctx, avctx->width, avctx->height);

747 748 749 750 751 752 753
    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)) {
        av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
        avcodec_set_dimensions(avctx, 0, 0);
    }

754
    /* if the decoder init function was already called previously,
Luca Barbato's avatar
Luca Barbato committed
755
     * free the already allocated subtitle_header before overwriting it */
756
    if (av_codec_is_decoder(codec))
757 758
        av_freep(&avctx->subtitle_header);

759
#define SANE_NB_CHANNELS 128U
760
    if (avctx->channels > SANE_NB_CHANNELS) {
761
        ret = AVERROR(EINVAL);
762
        goto free_and_end;
763 764
    }

765
    avctx->codec = codec;
766
    if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
767
        avctx->codec_id == AV_CODEC_ID_NONE) {
768 769 770
        avctx->codec_type = codec->type;
        avctx->codec_id   = codec->id;
    }
771
    if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
Luca Barbato's avatar
Luca Barbato committed
772
                                         && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
773
        av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
774
        ret = AVERROR(EINVAL);
775
        goto free_and_end;
776
    }
777
    avctx->frame_number = 0;
778

779 780 781 782 783 784
    if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
        (!avctx->time_base.num || !avctx->time_base.den)) {
        avctx->time_base.num = 1;
        avctx->time_base.den = avctx->sample_rate;
    }

785
    if (HAVE_THREADS && !avctx->thread_opaque) {
786
        ret = ff_thread_init(avctx);
787 788 789 790
        if (ret < 0) {
            goto free_and_end;
        }
    }
791 792
    if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
        avctx->thread_count = 1;
793

794
    if (av_codec_is_encoder(avctx->codec)) {
795
        int i;