utils.c 71.2 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 "config.h"
29
#include "libavutil/attributes.h"
30
#include "libavutil/avassert.h"
31
#include "libavutil/avstring.h"
32
#include "libavutil/channel_layout.h"
33
#include "libavutil/crc.h"
34
#include "libavutil/frame.h"
35
#include "libavutil/internal.h"
36
#include "libavutil/mathematics.h"
37
#include "libavutil/pixdesc.h"
38 39
#include "libavutil/imgutils.h"
#include "libavutil/samplefmt.h"
40
#include "libavutil/dict.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
41
#include "avcodec.h"
42
#include "dsputil.h"
43
#include "libavutil/opt.h"
44
#include "thread.h"
45
#include "internal.h"
46
#include "bytestream.h"
47
#include "version.h"
48
#include <stdlib.h>
49
#include <stdarg.h>
50
#include <limits.h>
Michael Niedermayer's avatar
Michael Niedermayer committed
51
#include <float.h>
Fabrice Bellard's avatar
Fabrice Bellard committed
52

Luca Barbato's avatar
Luca Barbato committed
53
static int volatile entangled_thread_counter = 0;
54
static int (*lockmgr_cb)(void **mutex, enum AVLockOp op);
55
static void *codec_mutex;
56
static void *avformat_mutex;
57

58 59
#if FF_API_FAST_MALLOC && CONFIG_SHARED && HAVE_SYMVER
FF_SYMVER(void*, av_fast_realloc, (void *ptr, unsigned int *size, size_t min_size), "LIBAVCODEC_55")
60
{
61
    return av_fast_realloc(ptr, size, min_size);
62 63
}

64
FF_SYMVER(void, av_fast_malloc, (void *ptr, unsigned int *size, size_t min_size), "LIBAVCODEC_55")
65
{
66
    av_fast_malloc(ptr, size, min_size);
67
}
68
#endif
69

70 71 72 73 74 75 76 77 78 79 80 81 82
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
83
/* encoder management */
84
static AVCodec *first_avcodec = NULL;
Fabrice Bellard's avatar
Fabrice Bellard committed
85

86 87
AVCodec *av_codec_next(const AVCodec *c)
{
Luca Barbato's avatar
Luca Barbato committed
88 89 90 91
    if (c)
        return c->next;
    else
        return first_avcodec;
92 93
}

94
static av_cold void avcodec_init(void)
95 96 97 98 99 100 101
{
    static int initialized = 0;

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

102 103
    if (CONFIG_DSPUTIL)
        ff_dsputil_static_init();
104 105
}

106
int av_codec_is_encoder(const AVCodec *codec)
107
{
108
    return codec && (codec->encode_sub || codec->encode2);
109 110
}

111
int av_codec_is_decoder(const AVCodec *codec)
112 113 114 115
{
    return codec && codec->decode;
}

116
av_cold void avcodec_register(AVCodec *codec)
Fabrice Bellard's avatar
Fabrice Bellard committed
117 118
{
    AVCodec **p;
119
    avcodec_init();
Fabrice Bellard's avatar
Fabrice Bellard committed
120
    p = &first_avcodec;
Luca Barbato's avatar
Luca Barbato committed
121 122 123
    while (*p != NULL)
        p = &(*p)->next;
    *p          = codec;
124
    codec->next = NULL;
125 126 127

    if (codec->init_static_data)
        codec->init_static_data(codec);
Fabrice Bellard's avatar
Fabrice Bellard committed
128 129
}

130
#if FF_API_EMU_EDGE
131 132 133 134
unsigned avcodec_get_edge_width(void)
{
    return EDGE_WIDTH;
}
135
#endif
136

137
#if FF_API_SET_DIMENSIONS
Luca Barbato's avatar
Luca Barbato committed
138 139
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
{
140 141 142 143 144 145 146 147 148 149 150 151 152 153
    ff_set_dimensions(s, width, height);
}
#endif

int ff_set_dimensions(AVCodecContext *s, int width, int height)
{
    int ret = av_image_check_size(width, height, 0, s);

    if (ret < 0)
        width = height = 0;
    s->width  = s->coded_width  = width;
    s->height = s->coded_height = height;

    return ret;
154 155
}

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
int ff_side_data_update_matrix_encoding(AVFrame *frame,
                                        enum AVMatrixEncoding matrix_encoding)
{
    AVFrameSideData *side_data;
    enum AVMatrixEncoding *data;

    side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
    if (!side_data)
        side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
                                           sizeof(enum AVMatrixEncoding));

    if (!side_data)
        return AVERROR(ENOMEM);

    data  = (enum AVMatrixEncoding*)side_data->data;
    *data = matrix_encoding;

    return 0;
}

176
#if HAVE_NEON || ARCH_PPC || HAVE_MMX
177 178 179 180 181
#   define STRIDE_ALIGN 16
#else
#   define STRIDE_ALIGN 8
#endif

182 183 184 185
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
186 187
    int w_align = 1;
    int h_align = 1;
188

Luca Barbato's avatar
Luca Barbato committed
189
    switch (s->pix_fmt) {
190 191
    case AV_PIX_FMT_YUV420P:
    case AV_PIX_FMT_YUYV422:
192
    case AV_PIX_FMT_YVYU422:
193 194 195 196 197 198 199 200 201 202 203 204 205
    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:
206 207
    case AV_PIX_FMT_YUVA422P:
    case AV_PIX_FMT_YUVA444P:
208 209 210 211 212 213 214 215
    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:
216 217
    case AV_PIX_FMT_YUVA422P10LE:
    case AV_PIX_FMT_YUVA422P10BE:
218 219 220 221
    case AV_PIX_FMT_YUV444P9LE:
    case AV_PIX_FMT_YUV444P9BE:
    case AV_PIX_FMT_YUV444P10LE:
    case AV_PIX_FMT_YUV444P10BE:
222 223
    case AV_PIX_FMT_YUVA444P10LE:
    case AV_PIX_FMT_YUVA444P10BE:
224 225 226 227
    case AV_PIX_FMT_GBRP9LE:
    case AV_PIX_FMT_GBRP9BE:
    case AV_PIX_FMT_GBRP10LE:
    case AV_PIX_FMT_GBRP10BE:
228 229
        w_align = 16; //FIXME assume 16 pixel per macroblock
        h_align = 16 * 2; // interlaced needs 2 macroblocks height
230
        break;
231 232
    case AV_PIX_FMT_YUV411P:
    case AV_PIX_FMT_UYYVYY411:
Luca Barbato's avatar
Luca Barbato committed
233 234
        w_align = 32;
        h_align = 8;
235
        break;
236
    case AV_PIX_FMT_YUV410P:
Luca Barbato's avatar
Luca Barbato committed
237 238 239
        if (s->codec_id == AV_CODEC_ID_SVQ1) {
            w_align = 64;
            h_align = 64;
240
        }
241
    case AV_PIX_FMT_RGB555:
Luca Barbato's avatar
Luca Barbato committed
242 243 244
        if (s->codec_id == AV_CODEC_ID_RPZA) {
            w_align = 4;
            h_align = 4;
245
        }
246 247 248
    case AV_PIX_FMT_PAL8:
    case AV_PIX_FMT_BGR8:
    case AV_PIX_FMT_RGB8:
Luca Barbato's avatar
Luca Barbato committed
249 250 251
        if (s->codec_id == AV_CODEC_ID_SMC) {
            w_align = 4;
            h_align = 4;
252
        }
253
        break;
254
    case AV_PIX_FMT_BGR24:
Luca Barbato's avatar
Luca Barbato committed
255 256 257 258
        if ((s->codec_id == AV_CODEC_ID_MSZH) ||
            (s->codec_id == AV_CODEC_ID_ZLIB)) {
            w_align = 4;
            h_align = 4;
259 260
        }
        break;
261
    default:
Luca Barbato's avatar
Luca Barbato committed
262 263
        w_align = 1;
        h_align = 1;
264 265 266
        break;
    }

Luca Barbato's avatar
Luca Barbato committed
267 268
    *width  = FFALIGN(*width, w_align);
    *height = FFALIGN(*height, h_align);
269
    if (s->codec_id == AV_CODEC_ID_H264)
Luca Barbato's avatar
Luca Barbato committed
270 271
        // some of the optimized chroma MC reads one line too much
        *height += 2;
272

273
    for (i = 0; i < 4; i++)
274
        linesize_align[i] = STRIDE_ALIGN;
275 276
}

Luca Barbato's avatar
Luca Barbato committed
277 278
void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
{
279 280
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
    int chroma_shift = desc->log2_chroma_w;
281
    int linesize_align[AV_NUM_DATA_POINTERS];
282
    int align;
Luca Barbato's avatar
Luca Barbato committed
283

284
    avcodec_align_dimensions2(s, width, height, linesize_align);
Luca Barbato's avatar
Luca Barbato committed
285
    align               = FFMAX(linesize_align[0], linesize_align[3]);
286 287
    linesize_align[1] <<= chroma_shift;
    linesize_align[2] <<= chroma_shift;
Luca Barbato's avatar
Luca Barbato committed
288 289
    align               = FFMAX3(align, linesize_align[1], linesize_align[2]);
    *width              = FFALIGN(*width, align);
290 291
}

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
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;
}

328
static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
Justin Ruggles's avatar
Justin Ruggles committed
329
{
330 331
    FramePool *pool = avctx->internal->pool;
    int i, ret;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
332

333 334
    switch (avctx->codec_type) {
    case AVMEDIA_TYPE_VIDEO: {
335
        AVPicture picture;
336 337 338 339
        int size[4] = { 0 };
        int w = frame->width;
        int h = frame->height;
        int tmpsize, unaligned;
340

341 342 343
        if (pool->format == frame->format &&
            pool->width == frame->width && pool->height == frame->height)
            return 0;
344

345
        avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
346

347
        if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
Luca Barbato's avatar
Luca Barbato committed
348 349
            w += EDGE_WIDTH * 2;
            h += EDGE_WIDTH * 2;
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
350
        }
Loren Merritt's avatar
Loren Merritt committed
351

352 353 354
        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
355
            av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
356
            // increase alignment of w for next try (rhs gives the lowest bit set in w)
Luca Barbato's avatar
Luca Barbato committed
357
            w += w & ~(w - 1);
358

359
            unaligned = 0;
Luca Barbato's avatar
Luca Barbato committed
360
            for (i = 0; i < 4; i++)
361
                unaligned |= picture.linesize[i] % pool->stride_align[i];
362
        } while (unaligned);
363

364 365
        tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
                                         NULL, picture.linesize);
366 367
        if (tmpsize < 0)
            return -1;
368

Luca Barbato's avatar
Luca Barbato committed
369 370
        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
371
        size[i] = tmpsize - (picture.data[i] - picture.data[0]);
372

373 374 375 376 377 378 379 380 381 382 383 384 385 386
        for (i = 0; i < 4; i++) {
            av_buffer_pool_uninit(&pool->pools[i]);
            pool->linesize[i] = picture.linesize[i];
            if (size[i]) {
                pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
                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
cleanup  
Michael Niedermayer committed
387

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
        break;
        }
    case AVMEDIA_TYPE_AUDIO: {
        int ch     = av_get_channel_layout_nb_channels(frame->channel_layout);
        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;

        pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
        if (!pool->pools[0]) {
            ret = AVERROR(ENOMEM);
            goto fail;
        }
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
410

411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
        pool->format     = frame->format;
        pool->planes     = planes;
        pool->channels   = ch;
        pool->samples = frame->nb_samples;
        break;
        }
    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
cleanup  
Michael Niedermayer committed
428

429 430 431 432 433
static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
{
    FramePool *pool = avctx->internal->pool;
    int planes = pool->planes;
    int i;
434

435 436 437 438 439 440 441 442 443 444 445
    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);
446
        }
447 448 449 450 451 452 453 454
    } else
        frame->extended_data = frame->data;

    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
cleanup  
Michael Niedermayer committed
455
    }
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
    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;
    }

    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
cleanup  
Michael Niedermayer committed
471

472 473 474 475 476 477 478 479 480 481 482
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;
483
    }
484 485

    memset(pic->data, 0, sizeof(pic->data));
Justin Ruggles's avatar
Justin Ruggles committed
486
    pic->extended_data = pic->data;
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514

    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);
515

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

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
519
    return 0;
520 521 522
fail:
    av_frame_unref(pic);
    return AVERROR(ENOMEM);
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
523 524
}

525
int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
Justin Ruggles's avatar
Justin Ruggles committed
526
{
527 528 529 530 531 532
    int ret;

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

#if FF_API_GET_BUFFER
533
FF_DISABLE_DEPRECATION_WARNINGS
534
    frame->type = FF_BUFFER_TYPE_INTERNAL;
535
FF_ENABLE_DEPRECATION_WARNINGS
536 537
#endif

Justin Ruggles's avatar
Justin Ruggles committed
538 539 540 541 542 543 544 545 546 547
    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;
    }
}

548
#if FF_API_GET_BUFFER
549
FF_DISABLE_DEPRECATION_WARNINGS
550
int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
551
{
552 553 554 555 556 557 558 559 560 561 562
    return avcodec_default_get_buffer2(avctx, frame, 0);
}

typedef struct CompatReleaseBufPriv {
    AVCodecContext avctx;
    AVFrame frame;
} CompatReleaseBufPriv;

static void compat_free_buffer(void *opaque, uint8_t *data)
{
    CompatReleaseBufPriv *priv = opaque;
563 564
    if (priv->avctx.release_buffer)
        priv->avctx.release_buffer(&priv->avctx, &priv->frame);
565 566 567 568 569 570 571 572
    av_freep(&priv);
}

static void compat_release_buffer(void *opaque, uint8_t *data)
{
    AVBufferRef *buf = opaque;
    av_buffer_unref(&buf);
}
573
FF_ENABLE_DEPRECATION_WARNINGS
574 575
#endif

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
{
    AVPacket *pkt = avctx->internal->pkt;
    uint8_t *packet_sd;
    int size;
    AVFrameSideData *frame_sd;


    frame->reordered_opaque = avctx->reordered_opaque;
    if (!pkt) {
        frame->pkt_pts = AV_NOPTS_VALUE;
        return 0;
    }

    frame->pkt_pts = pkt->pts;

    /* copy the replaygain data to the output frame */
    packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_REPLAYGAIN, &size);
    if (packet_sd) {
        frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_REPLAYGAIN, size);
        if (!frame_sd)
            return AVERROR(ENOMEM);

        memcpy(frame_sd->data, packet_sd, size);
    }

    return 0;
}

605 606
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
{
607
    const AVHWAccel *hwaccel = avctx->hwaccel;
608
    int override_dimensions = 1;
609 610
    int ret;

611 612
    switch (avctx->codec_type) {
    case AVMEDIA_TYPE_VIDEO:
613 614 615 616 617
        if (frame->width <= 0 || frame->height <= 0) {
            frame->width  = FFMAX(avctx->width, avctx->coded_width);
            frame->height = FFMAX(avctx->height, avctx->coded_height);
            override_dimensions = 0;
        }
618 619 620 621
        if (frame->format < 0)
            frame->format              = avctx->pix_fmt;
        if (!frame->sample_aspect_ratio.num)
            frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
622 623 624

        if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
            return ret;
625 626
        break;
    case AVMEDIA_TYPE_AUDIO:
627 628 629 630
        if (!frame->sample_rate)
            frame->sample_rate    = avctx->sample_rate;
        if (frame->format < 0)
            frame->format         = avctx->sample_fmt;
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
        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);
                }

                frame->channel_layout = av_get_default_channel_layout(avctx->channels);
                if (!frame->channel_layout)
                    frame->channel_layout = (1ULL << avctx->channels) - 1;
            }
        }
653 654 655 656
        break;
    default: return AVERROR(EINVAL);
    }

657 658 659
    ret = ff_decode_frame_props(avctx, frame);
    if (ret < 0)
        return ret;
660

661 662 663 664 665
    if (hwaccel && hwaccel->alloc_frame) {
        ret = hwaccel->alloc_frame(avctx, frame);
        goto end;
    }

666
#if FF_API_GET_BUFFER
667
FF_DISABLE_DEPRECATION_WARNINGS
668 669 670 671 672 673 674 675 676 677 678
    /*
     * 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;
679

680 681
        if (flags & AV_GET_BUFFER_FLAG_REF)
            frame->reference    = 1;
682

683 684 685
        ret = avctx->get_buffer(avctx, frame);
        if (ret < 0)
            return ret;
Justin Ruggles's avatar
Justin Ruggles committed
686

687 688 689 690 691 692 693 694 695 696 697 698 699 700
        /* 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])
            return 0;

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

702 703 704 705
        dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
        if (!dummy_buf) {
            ret = AVERROR(ENOMEM);
            goto fail;
706
        }
707

708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
#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);

727
            planes = av_pix_fmt_count_planes(frame->format);
728 729
            /* workaround for AVHWAccel plane count of 0, buf[0] is used as
               check for allocated buffers: make libavcodec happy */
730
            if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
731
                planes = 1;
732
            if (!desc || planes <= 0) {
733 734 735
                ret = AVERROR(EINVAL);
                goto fail;
            }
736

737
            for (i = 0; i < planes; i++) {
738 739
                int v_shift    = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
                int plane_size = (frame->height >> v_shift) * frame->linesize[i];
740

741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
                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;
                }
            }
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
756

757 758 759
            for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
                WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);

760
            for (i = 0; i < frame->nb_extended_buf; i++)
761 762 763 764 765 766
                WRAP_PLANE(frame->extended_buf[i],
                           frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
                           frame->linesize[0]);
        }

        av_buffer_unref(&dummy_buf);
767

768 769 770
        frame->width  = avctx->width;
        frame->height = avctx->height;

771
        return 0;
Justin Ruggles's avatar
Justin Ruggles committed
772

773 774 775 776 777
fail:
        avctx->release_buffer(avctx, frame);
        av_freep(&priv);
        av_buffer_unref(&dummy_buf);
        return ret;
778
    }
779
FF_ENABLE_DEPRECATION_WARNINGS
780 781
#endif

782 783
    ret = avctx->get_buffer2(avctx, frame, flags);

784
end:
785
    if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
786 787 788 789 790
        frame->width  = avctx->width;
        frame->height = avctx->height;
    }

    return ret;
791 792 793 794
}

int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
{
795
    AVFrame *tmp;
796
    int ret;
797

798 799 800 801
    av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);

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

803 804
    if (av_frame_is_writable(frame))
        return ff_decode_frame_props(avctx, frame);
805

806 807 808 809 810
    tmp = av_frame_alloc();
    if (!tmp)
        return AVERROR(ENOMEM);

    av_frame_move_ref(tmp, frame);
811 812 813

    ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
    if (ret < 0) {
814
        av_frame_free(&tmp);
815
        return ret;
816
    }
817

818 819
    av_frame_copy(frame, tmp);
    av_frame_free(&tmp);
820

821 822 823
    return 0;
}

824 825 826 827 828 829 830 831 832
#if FF_API_GET_BUFFER
void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
{
    av_frame_unref(pic);
}

int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
{
    av_assert0(0);
833
    return AVERROR_BUG;
834 835 836
}
#endif

Luca Barbato's avatar
Luca Barbato committed
837 838
int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
{
839 840
    int i;

Luca Barbato's avatar
Luca Barbato committed
841 842 843 844
    for (i = 0; i < count; i++) {
        int r = func(c, (char *)arg + i * size);
        if (ret)
            ret[i] = r;
845 846 847 848
    }
    return 0;
}

Luca Barbato's avatar
Luca Barbato committed
849 850
int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
{
851 852
    int i;

Luca Barbato's avatar
Luca Barbato committed
853 854 855 856
    for (i = 0; i < count; i++) {
        int r = func(c, arg, i, 0);
        if (ret)
            ret[i] = r;
857 858 859 860
    }
    return 0;
}

861 862 863
static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
{
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
864
    return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
865 866
}

867
enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
Luca Barbato's avatar
Luca Barbato committed
868
{
869
    while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
870
        ++fmt;
Michael Niedermayer's avatar
Michael Niedermayer committed
871 872 873
    return fmt[0];
}

874 875 876 877 878 879 880 881 882 883 884 885 886
static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
                               enum AVPixelFormat pix_fmt)
{
    AVHWAccel *hwaccel = NULL;

    while ((hwaccel = av_hwaccel_next(hwaccel)))
        if (hwaccel->id == codec_id
            && hwaccel->pix_fmt == pix_fmt)
            return hwaccel;
    return NULL;
}


887 888
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
{
889 890 891 892 893 894 895
    const AVPixFmtDescriptor *desc;
    enum AVPixelFormat ret = avctx->get_format(avctx, fmt);

    desc = av_pix_fmt_desc_get(ret);
    if (!desc)
        return AV_PIX_FMT_NONE;

896 897 898 899 900
    if (avctx->hwaccel && avctx->hwaccel->uninit)
        avctx->hwaccel->uninit(avctx);
    av_freep(&avctx->internal->hwaccel_priv_data);
    avctx->hwaccel = NULL;

901
    if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
902 903 904 905 906
        AVHWAccel *hwaccel;
        int err;

        hwaccel = find_hwaccel(avctx->codec_id, ret);
        if (!hwaccel) {
907 908 909 910 911
            av_log(avctx, AV_LOG_ERROR,
                   "Could not find an AVHWAccel for the pixel format: %s",
                   desc->name);
            return AV_PIX_FMT_NONE;
        }
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926

        if (hwaccel->priv_data_size) {
            avctx->internal->hwaccel_priv_data = av_mallocz(hwaccel->priv_data_size);
            if (!avctx->internal->hwaccel_priv_data)
                return AV_PIX_FMT_NONE;
        }

        if (hwaccel->init) {
            err = hwaccel->init(avctx);
            if (err < 0) {
                av_freep(&avctx->internal->hwaccel_priv_data);
                return AV_PIX_FMT_NONE;
            }
        }
        avctx->hwaccel = hwaccel;
927 928 929
    }

    return ret;
930 931
}

932
#if FF_API_AVFRAME_LAVC
933
void avcodec_get_frame_defaults(AVFrame *frame)
Luca Barbato's avatar
Luca Barbato committed
934
{
935 936 937
    if (frame->extended_data != frame->data)
        av_freep(&frame->extended_data);

938
    memset(frame, 0, sizeof(AVFrame));
939

940 941 942 943
    frame->pts                 = AV_NOPTS_VALUE;
    frame->key_frame           = 1;
    frame->sample_aspect_ratio = (AVRational) {0, 1 };
    frame->format              = -1; /* unknown */
944
    frame->extended_data       = frame->data;
945 946
}

Luca Barbato's avatar
Luca Barbato committed
947 948
AVFrame *avcodec_alloc_frame(void)
{
949
    AVFrame *frame = av_mallocz(sizeof(AVFrame));
950

951
    if (frame == NULL)
Luca Barbato's avatar
Luca Barbato committed
952
        return NULL;
953

954
FF_DISABLE_DEPRECATION_WARNINGS
955
    avcodec_get_frame_defaults(frame);
956
FF_ENABLE_DEPRECATION_WARNINGS
957