utils.c 62.4 KB
Newer Older
F
Fabrice Bellard 已提交
1 2
/*
 * utils for libavcodec
3
 * Copyright (c) 2001 Fabrice Bellard
4
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
F
Fabrice Bellard 已提交
5
 *
6
 * This file is part of Libav.
7
 *
8
 * Libav is free software; you can redistribute it and/or
F
Fabrice Bellard 已提交
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.
F
Fabrice Bellard 已提交
12
 *
13
 * Libav is distributed in the hope that it will be useful,
F
Fabrice Bellard 已提交
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
F
Fabrice Bellard 已提交
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
F
Fabrice Bellard 已提交
17
 *
F
Fabrice Bellard 已提交
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
F
Fabrice Bellard 已提交
21
 */
22

M
Michael Niedermayer 已提交
23
/**
24
 * @file
M
Michael Niedermayer 已提交
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"
F
Fabrice Bellard 已提交
37
#include "avcodec.h"
38
#include "dsputil.h"
39
#include "libavutil/opt.h"
40
#include "imgconvert.h"
41
#include "thread.h"
42
#include "audioconvert.h"
43
#include "internal.h"
44
#include "bytestream.h"
45
#include <stdlib.h>
46
#include <stdarg.h>
47
#include <limits.h>
M
Michael Niedermayer 已提交
48
#include <float.h>
F
Fabrice Bellard 已提交
49

50
static int volatile entangled_thread_counter=0;
51
static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
52
static void *codec_mutex;
53
static void *avformat_mutex;
54

55
void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
56
{
57
    if(min_size < *size)
58
        return ptr;
59

60
    min_size= FFMAX(17*min_size/16 + 32, min_size);
61

62
    ptr= av_realloc(ptr, min_size);
63
    if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
64 65 66
        min_size= 0;

    *size= min_size;
67 68

    return ptr;
69 70
}

71
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
72 73 74 75
{
    void **p = ptr;
    if (min_size < *size)
        return;
76
    min_size= FFMAX(17*min_size/16 + 32, min_size);
77
    av_free(*p);
78 79 80
    *p = av_malloc(min_size);
    if (!*p) min_size = 0;
    *size= min_size;
81 82
}

83 84 85 86 87 88 89 90 91 92 93 94 95
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);
}

F
Fabrice Bellard 已提交
96
/* encoder management */
97
static AVCodec *first_avcodec = NULL;
F
Fabrice Bellard 已提交
98

99 100 101 102 103
AVCodec *av_codec_next(AVCodec *c){
    if(c) return c->next;
    else  return first_avcodec;
}

104
static void avcodec_init(void)
105 106 107 108 109 110 111
{
    static int initialized = 0;

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

112
    ff_dsputil_static_init();
113 114
}

115
int av_codec_is_encoder(AVCodec *codec)
116 117 118 119
{
    return codec && (codec->encode || codec->encode2);
}

120
int av_codec_is_decoder(AVCodec *codec)
121 122 123 124
{
    return codec && codec->decode;
}

125
void avcodec_register(AVCodec *codec)
F
Fabrice Bellard 已提交
126 127
{
    AVCodec **p;
128
    avcodec_init();
F
Fabrice Bellard 已提交
129 130
    p = &first_avcodec;
    while (*p != NULL) p = &(*p)->next;
131 132
    *p = codec;
    codec->next = NULL;
133 134 135

    if (codec->init_static_data)
        codec->init_static_data(codec);
F
Fabrice Bellard 已提交
136 137
}

138 139 140 141 142
unsigned avcodec_get_edge_width(void)
{
    return EDGE_WIDTH;
}

143 144 145
void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
    s->coded_width = width;
    s->coded_height= height;
M
Mans Rullgard 已提交
146 147
    s->width  = width;
    s->height = height;
148 149
}

150
#define INTERNAL_BUFFER_SIZE (32+1)
M
cleanup  
Michael Niedermayer 已提交
151

152 153 154 155
void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
                               int linesize_align[AV_NUM_DATA_POINTERS])
{
    int i;
156 157 158
    int w_align= 1;
    int h_align= 1;

M
Michael Niedermayer 已提交
159 160
    switch(s->pix_fmt){
    case PIX_FMT_YUV420P:
161
    case PIX_FMT_YUYV422:
162
    case PIX_FMT_UYVY422:
M
Michael Niedermayer 已提交
163
    case PIX_FMT_YUV422P:
164
    case PIX_FMT_YUV440P:
M
Michael Niedermayer 已提交
165
    case PIX_FMT_YUV444P:
166
    case PIX_FMT_GBRP:
M
Michael Niedermayer 已提交
167
    case PIX_FMT_GRAY8:
K
Kostya Shishkov 已提交
168 169
    case PIX_FMT_GRAY16BE:
    case PIX_FMT_GRAY16LE:
M
Michael Niedermayer 已提交
170 171
    case PIX_FMT_YUVJ420P:
    case PIX_FMT_YUVJ422P:
172
    case PIX_FMT_YUVJ440P:
M
Michael Niedermayer 已提交
173
    case PIX_FMT_YUVJ444P:
174
    case PIX_FMT_YUVA420P:
175 176 177 178
    case PIX_FMT_YUV420P9LE:
    case PIX_FMT_YUV420P9BE:
    case PIX_FMT_YUV420P10LE:
    case PIX_FMT_YUV420P10BE:
179 180
    case PIX_FMT_YUV422P9LE:
    case PIX_FMT_YUV422P9BE:
181 182
    case PIX_FMT_YUV422P10LE:
    case PIX_FMT_YUV422P10BE:
183 184 185 186
    case PIX_FMT_YUV444P9LE:
    case PIX_FMT_YUV444P9BE:
    case PIX_FMT_YUV444P10LE:
    case PIX_FMT_YUV444P10BE:
187 188 189 190
    case PIX_FMT_GBRP9LE:
    case PIX_FMT_GBRP9BE:
    case PIX_FMT_GBRP10LE:
    case PIX_FMT_GBRP10BE:
191 192
        w_align = 16; //FIXME assume 16 pixel per macroblock
        h_align = 16 * 2; // interlaced needs 2 macroblocks height
M
Michael Niedermayer 已提交
193 194
        break;
    case PIX_FMT_YUV411P:
195
    case PIX_FMT_UYYVYY411:
M
Michael Niedermayer 已提交
196 197 198 199 200 201 202 203
        w_align=32;
        h_align=8;
        break;
    case PIX_FMT_YUV410P:
        if(s->codec_id == CODEC_ID_SVQ1){
            w_align=64;
            h_align=64;
        }
204 205 206 207 208 209
    case PIX_FMT_RGB555:
        if(s->codec_id == CODEC_ID_RPZA){
            w_align=4;
            h_align=4;
        }
    case PIX_FMT_PAL8:
210 211
    case PIX_FMT_BGR8:
    case PIX_FMT_RGB8:
212 213 214 215
        if(s->codec_id == CODEC_ID_SMC){
            w_align=4;
            h_align=4;
        }
M
Michael Niedermayer 已提交
216
        break;
217 218 219 220 221 222
    case PIX_FMT_BGR24:
        if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
            w_align=4;
            h_align=4;
        }
        break;
M
Michael Niedermayer 已提交
223 224 225 226 227 228
    default:
        w_align= 1;
        h_align= 1;
        break;
    }

229 230
    *width = FFALIGN(*width , w_align);
    *height= FFALIGN(*height, h_align);
M
Mans Rullgard 已提交
231
    if (s->codec_id == CODEC_ID_H264)
232
        *height+=2; // some of the optimized chroma MC reads one line too much
233

234
    for (i = 0; i < 4; i++)
235
        linesize_align[i] = STRIDE_ALIGN;
236 237 238 239
}

void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
    int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
240
    int linesize_align[AV_NUM_DATA_POINTERS];
241 242 243 244 245 246 247
    int align;
    avcodec_align_dimensions2(s, width, height, linesize_align);
    align = FFMAX(linesize_align[0], linesize_align[3]);
    linesize_align[1] <<= chroma_shift;
    linesize_align[2] <<= chroma_shift;
    align = FFMAX3(align, linesize_align[1], linesize_align[2]);
    *width=FFALIGN(*width, align);
M
Michael Niedermayer 已提交
248 249
}

250 251 252 253 254 255 256 257 258 259 260 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
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;
}

J
Justin Ruggles 已提交
286 287 288 289
static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
{
    AVCodecInternal *avci = avctx->internal;
    InternalBuffer *buf;
290
    int buf_size, ret;
J
Justin Ruggles 已提交
291 292 293

    buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
                                          frame->nb_samples, avctx->sample_fmt,
294
                                          0);
J
Justin Ruggles 已提交
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 328
    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
       channel count to see if we can reuse it */
    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)
                av_free(&buf->extended_data);
            buf->extended_data = NULL;
            buf->data[0] = NULL;
        }
        /* if number of channels has changed, reset and/or free extended data
           pointers but leave data buffer in buf->data[0] for reuse */
        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
       as-is, allocate a new buffer and/or rearrange the channel pointers */
    if (!buf->extended_data) {
329 330
        if (!buf->data[0]) {
            if (!(buf->data[0] = av_mallocz(buf_size)))
J
Justin Ruggles 已提交
331
                return AVERROR(ENOMEM);
332
            buf->audio_data_size = buf_size;
J
Justin Ruggles 已提交
333
        }
334 335
        if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
                                            avctx->sample_fmt, buf->data[0],
336
                                            buf->audio_data_size, 0)))
J
Justin Ruggles 已提交
337 338
            return ret;

339 340 341 342 343 344 345 346 347 348 349 350
        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));
J
Justin Ruggles 已提交
351 352 353 354 355 356 357 358
    }

    frame->type          = FF_BUFFER_TYPE_INTERNAL;

    if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
    else            frame->pkt_pts = AV_NOPTS_VALUE;
    frame->reordered_opaque = avctx->reordered_opaque;

359 360 361 362
    frame->sample_rate    = avctx->sample_rate;
    frame->format         = avctx->sample_fmt;
    frame->channel_layout = avctx->channel_layout;

J
Justin Ruggles 已提交
363 364 365 366 367 368 369 370 371
    if (avctx->debug & FF_DEBUG_BUFFERS)
        av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
               "internal audio buffer used\n", frame);

    return 0;
}

static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
{
M
cleanup  
Michael Niedermayer 已提交
372
    int i;
M
Michael Niedermayer 已提交
373 374
    int w= s->width;
    int h= s->height;
375
    InternalBuffer *buf;
376
    AVCodecInternal *avci = s->internal;
377

378 379 380 381
    if(pic->data[0]!=NULL) {
        av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
        return -1;
    }
382 383
    if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
        av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
384 385
        return -1;
    }
M
cleanup  
Michael Niedermayer 已提交
386

387
    if(av_image_check_size(w, h, 0, s))
388 389
        return -1;

390 391 392
    if (!avci->buffer) {
        avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
                                  sizeof(InternalBuffer));
393
    }
394

395
    buf = &avci->buffer[avci->buffer_count];
396

397
    if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
398
        for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
399 400 401 402 403
            av_freep(&buf->base[i]);
            buf->data[i]= NULL;
        }
    }

M
Mans Rullgard 已提交
404
    if (!buf->base[0]) {
M
Michael Niedermayer 已提交
405
        int h_chroma_shift, v_chroma_shift;
406 407
        int size[4] = {0};
        int tmpsize;
408
        int unaligned;
M
Michael Niedermayer 已提交
409
        AVPicture picture;
410
        int stride_align[AV_NUM_DATA_POINTERS];
411
        const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
M
Michael Niedermayer 已提交
412

M
cleanup  
Michael Niedermayer 已提交
413
        avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
M
Michael Niedermayer 已提交
414

415
        avcodec_align_dimensions2(s, &w, &h, stride_align);
416

M
cleanup  
Michael Niedermayer 已提交
417 418 419 420
        if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
            w+= EDGE_WIDTH*2;
            h+= EDGE_WIDTH*2;
        }
L
Loren Merritt 已提交
421

422 423 424
        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
425
            av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
426 427
            // increase alignment of w for next try (rhs gives the lowest bit set in w)
            w += w & ~(w-1);
428

429
            unaligned = 0;
430
            for (i=0; i<4; i++){
431
                unaligned |= picture.linesize[i] % stride_align[i];
432
            }
433
        } while (unaligned);
434

435
        tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
436 437
        if (tmpsize < 0)
            return -1;
438 439 440

        for (i=0; i<3 && picture.data[i+1]; i++)
            size[i] = picture.data[i+1] - picture.data[i];
V
Vitor Sessak 已提交
441
        size[i] = tmpsize - (picture.data[i] - picture.data[0]);
M
Michael Niedermayer 已提交
442 443 444

        memset(buf->base, 0, sizeof(buf->base));
        memset(buf->data, 0, sizeof(buf->data));
M
cleanup  
Michael Niedermayer 已提交
445

446
        for(i=0; i<4 && size[i]; i++){
M
10l  
Michael Niedermayer 已提交
447 448
            const int h_shift= i==0 ? 0 : h_chroma_shift;
            const int v_shift= i==0 ? 0 : v_chroma_shift;
M
cleanup  
Michael Niedermayer 已提交
449

M
Michael Niedermayer 已提交
450
            buf->linesize[i]= picture.linesize[i];
M
cleanup  
Michael Niedermayer 已提交
451

M
Michael Niedermayer 已提交
452
            buf->base[i]= av_malloc(size[i]+16); //FIXME 16
453
            if(buf->base[i]==NULL) return -1;
M
Michael Niedermayer 已提交
454 455
            memset(buf->base[i], 128, size[i]);

J
Jai Menon 已提交
456
            // no edge if EDGE EMU or not planar YUV
457
            if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
458
                buf->data[i] = buf->base[i];
M
cleanup  
Michael Niedermayer 已提交
459
            else
460
                buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
M
cleanup  
Michael Niedermayer 已提交
461
        }
462 463 464 465
        for (; i < AV_NUM_DATA_POINTERS; i++) {
            buf->base[i] = buf->data[i] = NULL;
            buf->linesize[i] = 0;
        }
466
        if(size[1] && !size[2])
467
            ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
468 469 470
        buf->width  = s->width;
        buf->height = s->height;
        buf->pix_fmt= s->pix_fmt;
M
cleanup  
Michael Niedermayer 已提交
471
    }
472
    pic->type= FF_BUFFER_TYPE_INTERNAL;
M
cleanup  
Michael Niedermayer 已提交
473

474
    for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
475 476
        pic->base[i]= buf->base[i];
        pic->data[i]= buf->data[i];
477
        pic->linesize[i]= buf->linesize[i];
478
    }
J
Justin Ruggles 已提交
479
    pic->extended_data = pic->data;
480
    avci->buffer_count++;
481 482 483 484
    pic->width  = buf->width;
    pic->height = buf->height;
    pic->format = buf->pix_fmt;
    pic->sample_aspect_ratio = s->sample_aspect_ratio;
485

486 487
    if(s->pkt) pic->pkt_pts= s->pkt->pts;
    else       pic->pkt_pts= AV_NOPTS_VALUE;
488 489
    pic->reordered_opaque= s->reordered_opaque;

490
    if(s->debug&FF_DEBUG_BUFFERS)
491 492
        av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
               "buffers used\n", pic, avci->buffer_count);
493

M
cleanup  
Michael Niedermayer 已提交
494 495 496
    return 0;
}

J
Justin Ruggles 已提交
497 498 499 500 501 502 503 504 505 506 507 508
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;
    }
}

M
Michael Niedermayer 已提交
509
void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
M
cleanup  
Michael Niedermayer 已提交
510
    int i;
511
    InternalBuffer *buf, *last;
512
    AVCodecInternal *avci = s->internal;
513

J
Justin Ruggles 已提交
514 515
    assert(s->codec_type == AVMEDIA_TYPE_VIDEO);

M
Michael Niedermayer 已提交
516
    assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
517 518 519 520 521 522 523 524 525 526 527 528
    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];
529

530 531
        if (buf != last)
            FFSWAP(InternalBuffer, *buf, *last);
532
    }
533

534
    for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
M
cleanup  
Michael Niedermayer 已提交
535
        pic->data[i]=NULL;
536 537
//        pic->base[i]=NULL;
    }
M
cleanup  
Michael Niedermayer 已提交
538
//printf("R%X\n", pic->opaque);
539 540

    if(s->debug&FF_DEBUG_BUFFERS)
541 542
        av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
               "buffers used\n", pic, avci->buffer_count);
M
cleanup  
Michael Niedermayer 已提交
543 544
}

545 546 547 548
int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
    AVFrame temp_pic;
    int i;

J
Justin Ruggles 已提交
549 550
    assert(s->codec_type == AVMEDIA_TYPE_VIDEO);

551 552 553 554 555 556 557
    /* If no picture return a new buffer */
    if(pic->data[0] == NULL) {
        /* We will copy from buffer, so must be readable */
        pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
        return s->get_buffer(s, pic);
    }

558
    assert(s->pix_fmt == pic->format);
559

560
    /* If internal buffer type return the same buffer */
561
    if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
562 563
        if(s->pkt) pic->pkt_pts= s->pkt->pts;
        else       pic->pkt_pts= AV_NOPTS_VALUE;
564
        pic->reordered_opaque= s->reordered_opaque;
565
        return 0;
566
    }
567 568 569 570 571

    /*
     * Not internal type and reget_buffer not overridden, emulate cr buffer
     */
    temp_pic = *pic;
572
    for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
573 574 575 576 577 578
        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 */
579
    av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
580 581 582 583 584
             s->height);
    s->release_buffer(s, &temp_pic); // Release old frame
    return 0;
}

585
int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
586 587 588
    int i;

    for(i=0; i<count; i++){
589
        int r= func(c, (char*)arg + i*size);
590 591 592 593 594
        if(ret) ret[i]= r;
    }
    return 0;
}

595 596 597 598 599 600 601 602 603 604
int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
    int i;

    for(i=0; i<count; i++){
        int r= func(c, arg, i, 0);
        if(ret) ret[i]= r;
    }
    return 0;
}

605 606 607
enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
    while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
        ++fmt;
M
Michael Niedermayer 已提交
608 609 610
    return fmt[0];
}

611 612 613 614
void avcodec_get_frame_defaults(AVFrame *pic){
    memset(pic, 0, sizeof(AVFrame));

    pic->pts= AV_NOPTS_VALUE;
615
    pic->key_frame= 1;
616
    pic->sample_aspect_ratio = (AVRational){0, 1};
617
    pic->format = -1;           /* unknown */
618 619
}

M
Michael Niedermayer 已提交
620
AVFrame *avcodec_alloc_frame(void){
621
    AVFrame *pic= av_malloc(sizeof(AVFrame));
622

623
    if(pic==NULL) return NULL;
624

625
    avcodec_get_frame_defaults(pic);
626

M
cleanup  
Michael Niedermayer 已提交
627 628 629
    return pic;
}

630
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
F
Fabrice Bellard 已提交
631
{
632
    int ret = 0;
633 634
    AVDictionary *tmp = NULL;

A
Anton Khirnov 已提交
635 636 637
    if (avcodec_is_open(avctx))
        return 0;

638 639 640 641 642 643 644 645 646 647 648 649
    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, "
               "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
        return AVERROR(EINVAL);
    }
    if (!codec)
        codec = avctx->codec;

650 651 652
    if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
        return AVERROR(EINVAL);

653 654
    if (options)
        av_dict_copy(&tmp, *options, 0);
655

656 657 658 659 660 661
    /* 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;
    }

662 663 664
    entangled_thread_counter++;
    if(entangled_thread_counter != 1){
        av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
665
        ret = -1;
666 667
        goto end;
    }
F
Fabrice Bellard 已提交
668

669 670 671 672 673 674
    avctx->internal = av_mallocz(sizeof(AVCodecInternal));
    if (!avctx->internal) {
        ret = AVERROR(ENOMEM);
        goto end;
    }

675
    if (codec->priv_data_size > 0) {
676
      if(!avctx->priv_data){
677
        avctx->priv_data = av_mallocz(codec->priv_data_size);
678 679
        if (!avctx->priv_data) {
            ret = AVERROR(ENOMEM);
680
            goto end;
681
        }
682
        if (codec->priv_class) {
683 684 685 686
            *(AVClass**)avctx->priv_data= codec->priv_class;
            av_opt_set_defaults(avctx->priv_data);
        }
      }
687
      if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
688
          goto free_and_end;
689 690 691
    } else {
        avctx->priv_data = NULL;
    }
692 693
    if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
        goto free_and_end;
694 695 696 697 698 699

    if(avctx->coded_width && avctx->coded_height)
        avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
    else if(avctx->width && avctx->height)
        avcodec_set_dimensions(avctx, avctx->width, avctx->height);

700 701 702 703 704 705 706
    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);
    }

707 708
    /* if the decoder init function was already called previously,
       free the already allocated subtitle_header before overwriting it */
709
    if (av_codec_is_decoder(codec))
710 711
        av_freep(&avctx->subtitle_header);

712
#define SANE_NB_CHANNELS 128U
713
    if (avctx->channels > SANE_NB_CHANNELS) {
714
        ret = AVERROR(EINVAL);
715
        goto free_and_end;
716 717
    }

718
    avctx->codec = codec;
719
    if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
720 721 722 723
        avctx->codec_id == CODEC_ID_NONE) {
        avctx->codec_type = codec->type;
        avctx->codec_id   = codec->id;
    }
724 725
    if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
                           && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
726
        av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
727
        ret = AVERROR(EINVAL);
728
        goto free_and_end;
729
    }
730
    avctx->frame_number = 0;
731

732 733 734 735 736 737
    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;
    }

738
    if (HAVE_THREADS && !avctx->thread_opaque) {
739
        ret = ff_thread_init(avctx);
740 741 742 743
        if (ret < 0) {
            goto free_and_end;
        }
    }
744 745
    if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
        avctx->thread_count = 1;
746

747
    if (av_codec_is_encoder(avctx->codec)) {
748
        int i;
749
        if (avctx->codec->sample_fmts) {
J
Justin Ruggles 已提交
750 751 752 753 754 755 756 757
            for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
                if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
                    break;
            if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
                av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
                ret = AVERROR(EINVAL);
                goto free_and_end;
            }
758
        }
759 760 761 762 763 764 765 766 767 768
        if (avctx->codec->pix_fmts) {
            for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
                if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
                    break;
            if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE) {
                av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
                ret = AVERROR(EINVAL);
                goto free_and_end;
            }
        }
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
        if (avctx->codec->supported_samplerates) {
            for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
                if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
                    break;
            if (avctx->codec->supported_samplerates[i] == 0) {
                av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
                ret = AVERROR(EINVAL);
                goto free_and_end;
            }
        }
        if (avctx->codec->channel_layouts) {
            if (!avctx->channel_layout) {
                av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
            } else {
                for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
                    if (avctx->channel_layout == avctx->codec->channel_layouts[i])
                        break;
                if (avctx->codec->channel_layouts[i] == 0) {
                    av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
                    ret = AVERROR(EINVAL);
                    goto free_and_end;
                }
            }
        }
793 794 795 796 797 798
        if (avctx->channel_layout && avctx->channels) {
            if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
                av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
                ret = AVERROR(EINVAL);
                goto free_and_end;
            }
799 800
        } else if (avctx->channel_layout) {
            avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
801
        }
802
    }
803

804
    if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
M
Michael Niedermayer 已提交
805 806
        ret = avctx->codec->init(avctx);
        if (ret < 0) {
807
            goto free_and_end;
M
Michael Niedermayer 已提交
808
        }
809
    }
810 811 812 813 814 815 816 817 818

    if (av_codec_is_decoder(avctx->codec)) {
        /* validate channel layout from the decoder */
        if (avctx->channel_layout &&
            av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
            av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n");
            avctx->channel_layout = 0;
        }
    }
819 820
end:
    entangled_thread_counter--;
821 822 823 824 825

    /* Release any user-supplied mutex. */
    if (ff_lockmgr_cb) {
        (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
    }
826 827 828 829 830
    if (options) {
        av_dict_free(options);
        *options = tmp;
    }

831
    return ret;
832
free_and_end:
833
    av_dict_free(&tmp);
834
    av_freep(&avctx->priv_data);
835
    av_freep(&avctx->internal);
836 837
    avctx->codec= NULL;
    goto end;
F
Fabrice Bellard 已提交
838 839
}

840
int ff_alloc_packet(AVPacket *avpkt, int size)
F
Fabrice Bellard 已提交
841
{
842 843 844 845
    if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
        return AVERROR(EINVAL);

    if (avpkt->data) {
846
        void *destruct = avpkt->destruct;
847 848 849 850 851

        if (avpkt->size < size)
            return AVERROR(EINVAL);

        av_init_packet(avpkt);
852
        avpkt->destruct = destruct;
853
        avpkt->size = size;
854 855 856
        return 0;
    } else {
        return av_new_packet(avpkt, size);
857
    }
858 859
}

860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
/**
 * Pad last frame with silence.
 */
static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
{
    AVFrame *frame = NULL;
    uint8_t *buf   = NULL;
    int ret;

    if (!(frame = avcodec_alloc_frame()))
        return AVERROR(ENOMEM);
    *frame = *src;

    if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
                                          s->frame_size, s->sample_fmt, 0)) < 0)
        goto fail;

    if (!(buf = av_malloc(ret))) {
        ret = AVERROR(ENOMEM);
        goto fail;
    }

    frame->nb_samples = s->frame_size;
    if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
                                        buf, ret, 0)) < 0)
        goto fail;
    if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
                               src->nb_samples, s->channels, s->sample_fmt)) < 0)
        goto fail;
    if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
                                      frame->nb_samples - src->nb_samples,
                                      s->channels, s->sample_fmt)) < 0)
        goto fail;

    *dst = frame;

    return 0;

fail:
    if (frame->extended_data != frame->data)
        av_freep(&frame->extended_data);
    av_freep(&buf);
    av_freep(&frame);
    return ret;
}

906 907 908 909 910
int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
                                              AVPacket *avpkt,
                                              const AVFrame *frame,
                                              int *got_packet_ptr)
{
911
    AVFrame tmp;
912
    AVFrame *padded_frame = NULL;
913 914 915
    int ret;
    int user_packet = !!avpkt->data;

916 917
    *got_packet_ptr = 0;

918
    if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
919
        av_free_packet(avpkt);
920
        av_init_packet(avpkt);
921
        return 0;
922 923
    }

924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
    /* ensure that extended_data is properly set */
    if (frame && !frame->extended_data) {
        if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
            avctx->channels > AV_NUM_DATA_POINTERS) {
            av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
                   "with more than %d channels, but extended_data is not set.\n",
                   AV_NUM_DATA_POINTERS);
            return AVERROR(EINVAL);
        }
        av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");

        tmp = *frame;
        tmp.extended_data = tmp.data;
        frame = &tmp;
    }

940 941 942
    /* check for valid frame size */
    if (frame) {
        if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
943
            if (frame->nb_samples > avctx->frame_size)
944 945
                return AVERROR(EINVAL);
        } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
946 947 948 949 950 951 952 953 954 955
            if (frame->nb_samples < avctx->frame_size &&
                !avctx->internal->last_audio_frame) {
                ret = pad_last_frame(avctx, &padded_frame, frame);
                if (ret < 0)
                    return ret;

                frame = padded_frame;
                avctx->internal->last_audio_frame = 1;
            }

956
            if (frame->nb_samples != avctx->frame_size)
957 958 959 960
                return AVERROR(EINVAL);
        }
    }

961 962 963
    ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
    if (!ret) {
        if (*got_packet_ptr) {
964
            if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
965 966 967 968 969
                if (avpkt->pts == AV_NOPTS_VALUE)
                    avpkt->pts = frame->pts;
                if (!avpkt->duration)
                    avpkt->duration = ff_samples_to_time_base(avctx,
                                                              frame->nb_samples);
970 971
            }
            avpkt->dts = avpkt->pts;
972 973
        } else {
            avpkt->size = 0;
974 975
        }

976
        if (!user_packet && avpkt->size) {
977 978 979 980 981
            uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
            if (new_data)
                avpkt->data = new_data;
        }

982
        avctx->frame_number++;
983
    }
984

985
    if (ret < 0 || !*got_packet_ptr) {
986
        av_free_packet(avpkt);
987 988 989
        av_init_packet(avpkt);
        return ret;
    }
990

991 992 993 994 995
    /* NOTE: if we add any audio encoders which output non-keyframe packets,
             this needs to be moved to the encoders, but for now we can do it
             here to simplify things */
    avpkt->flags |= AV_PKT_FLAG_KEY;

996 997 998 999 1000 1001 1002
    if (padded_frame) {
        av_freep(&padded_frame->data[0]);
        if (padded_frame->extended_data != padded_frame->data)
            av_freep(&padded_frame->extended_data);
        av_freep(&padded_frame);
    }

1003
    return ret;
F
Fabrice Bellard 已提交
1004 1005
}

1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
#if FF_API_OLD_DECODE_AUDIO
int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
                                             uint8_t *buf, int buf_size,
                                             const short *samples)
{
    AVPacket pkt;
    AVFrame frame0;
    AVFrame *frame;
    int ret, samples_size, got_packet;

    av_init_packet(&pkt);
    pkt.data = buf;
    pkt.size = buf_size;

    if (samples) {
        frame = &frame0;
        avcodec_get_frame_defaults(frame);

        if (avctx->frame_size) {
            frame->nb_samples = avctx->frame_size;
        } else {
            /* if frame_size is not set, the number of samples must be
               calculated from the buffer size */
            int64_t nb_samples;
            if (!av_get_bits_per_sample(avctx->codec_id)) {
                av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
                       "support this codec\n");
                return AVERROR(EINVAL);
            }
            nb_samples = (int64_t)buf_size * 8 /
                         (av_get_bits_per_sample(avctx->codec_id) *
                         avctx->channels);
            if (nb_samples >= INT_MAX)
                return AVERROR(EINVAL);
            frame->nb_samples = nb_samples;
        }

        /* it is assumed that the samples buffer is large enough based on the
           relevant parameters */
        samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
                                                  frame->nb_samples,
                                                  avctx->sample_fmt, 1);
        if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
                                            avctx->sample_fmt,
                                            samples, samples_size, 1)))
            return ret;

        /* fabricate frame pts from sample count.
           this is needed because the avcodec_encode_audio() API does not have
           a way for the user to provide pts */
1056 1057
        frame->pts = ff_samples_to_time_base(avctx,
                                             avctx->internal->sample_count);
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
        avctx->internal->sample_count += frame->nb_samples;
    } else {
        frame = NULL;
    }

    got_packet = 0;
    ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
    if (!ret && got_packet && avctx->coded_frame) {
        avctx->coded_frame->pts       = pkt.pts;
        avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
    }
    /* free any side data since we cannot return it */
    if (pkt.side_data_elems > 0) {
        int i;
        for (i = 0; i < pkt.side_data_elems; i++)
            av_free(pkt.side_data[i].data);
        av_freep(&pkt.side_data);
        pkt.side_data_elems = 0;
    }

    if (frame && frame->extended_data != frame->data)
        av_free(frame->extended_data);

    return ret ? ret : pkt.size;
}
#endif

1085
#if FF_API_OLD_ENCODE_VIDEO
1086
int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
M
Michael Niedermayer 已提交
1087
                         const AVFrame *pict)
F
Fabrice Bellard 已提交
1088
{
1089 1090 1091
    AVPacket pkt;
    int ret, got_packet = 0;

1092
    if(buf_size < FF_MIN_BUFFER_SIZE){
M
Michel Bardiaux 已提交
1093
        av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1094 1095
        return -1;
    }
1096

1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
    av_init_packet(&pkt);
    pkt.data = buf;
    pkt.size = buf_size;

    ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
    if (!ret && got_packet && avctx->coded_frame) {
        avctx->coded_frame->pts       = pkt.pts;
        avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
    }

    /* free any side data since we cannot return it */
    if (pkt.side_data_elems > 0) {
        int i;
        for (i = 0; i < pkt.side_data_elems; i++)
            av_free(pkt.side_data[i].data);
        av_freep(&pkt.side_data);
        pkt.side_data_elems = 0;
    }

    return ret ? ret : pkt.size;
}
#endif

int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
                                              AVPacket *avpkt,
                                              const AVFrame *frame,
                                              int *got_packet_ptr)
{
    int ret;
    int user_packet = !!avpkt->data;

1128 1129
    *got_packet_ptr = 0;

1130
    if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1131
        av_free_packet(avpkt);
1132 1133
        av_init_packet(avpkt);
        avpkt->size     = 0;
1134
        return 0;
1135 1136 1137 1138 1139
    }

    if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
        return AVERROR(EINVAL);

1140
    av_assert0(avctx->codec->encode2);
1141

1142 1143 1144 1145 1146 1147
    ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
    if (!ret) {
        if (!*got_packet_ptr)
            avpkt->size = 0;
        else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
            avpkt->pts = avpkt->dts = frame->pts;
1148

1149
        if (!user_packet && avpkt->size) {
1150 1151 1152 1153 1154
            uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
            if (new_data)
                avpkt->data = new_data;
        }

1155
        avctx->frame_number++;
1156
    }
1157

1158 1159 1160
    if (ret < 0 || !*got_packet_ptr)
        av_free_packet(avpkt);

1161 1162
    emms_c();
    return ret;
F
Fabrice Bellard 已提交
1163 1164
}

1165
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
F
Fabrice Bellard 已提交
1166 1167 1168
                            const AVSubtitle *sub)
{
    int ret;
1169 1170 1171 1172
    if(sub->start_display_time) {
        av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
        return -1;
    }
1173 1174
    if(sub->num_rects == 0 || !sub->rects)
        return -1;
1175
    ret = avctx->codec->encode(avctx, buf, buf_size, sub);
F
Fabrice Bellard 已提交
1176 1177 1178 1179
    avctx->frame_number++;
    return ret;
}

1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
{
    int size = 0;
    const uint8_t *data;
    uint32_t flags;

    if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
        return;

    data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
    if (!data || size < 4)
        return;
    flags = bytestream_get_le32(&data);
    size -= 4;
    if (size < 4) /* Required for any of the changes */
        return;
    if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
        avctx->channels = bytestream_get_le32(&data);
        size -= 4;
    }
    if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
        if (size < 8)
            return;
        avctx->channel_layout = bytestream_get_le64(&data);
        size -= 8;
    }
    if (size < 4)
        return;
    if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
        avctx->sample_rate = bytestream_get_le32(&data);
        size -= 4;
    }
    if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
        if (size < 8)
            return;
        avctx->width  = bytestream_get_le32(&data);
        avctx->height = bytestream_get_le32(&data);
1217
        avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1218 1219 1220 1221
        size -= 8;
    }
}

1222 1223 1224
int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
                         int *got_picture_ptr,
                         AVPacket *avpkt)
F
Fabrice Bellard 已提交
1225 1226
{
    int ret;
1227

M
Michael Niedermayer 已提交
1228
    *got_picture_ptr= 0;
1229
    if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1230
        return -1;
1231 1232

    avctx->pkt = avpkt;
1233
    apply_param_change(avctx, avpkt);
1234

1235
    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
1236
        if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1237 1238 1239 1240 1241 1242
             ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
                                          avpkt);
        else {
            ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
                              avpkt);
            picture->pkt_dts= avpkt->dts;
1243
            picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1244 1245
            picture->width  = avctx->width;
            picture->height = avctx->height;
1246
            picture->format = avctx->pix_fmt;
1247
        }
1248

D
Diego Biurrun 已提交
1249
        emms_c(); //needed to avoid an emms_c() call before every return;
1250 1251

        if (*got_picture_ptr)
1252 1253 1254 1255
            avctx->frame_number++;
    }else
        ret= 0;

F
Fabrice Bellard 已提交
1256 1257 1258
    return ret;
}

J
Justin Ruggles 已提交
1259
#if FF_API_OLD_DECODE_AUDIO
1260 1261 1262
int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
                         int *frame_size_ptr,
                         AVPacket *avpkt)
F
Fabrice Bellard 已提交
1263
{
J
Justin Ruggles 已提交
1264 1265 1266 1267
    AVFrame frame;
    int ret, got_frame = 0;

    if (avctx->get_buffer != avcodec_default_get_buffer) {
1268 1269 1270 1271 1272
        av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
               "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
        av_log(avctx, AV_LOG_ERROR, "Please port your application to "
               "avcodec_decode_audio4()\n");
        avctx->get_buffer = avcodec_default_get_buffer;
J
Justin Ruggles 已提交
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
    }

    ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);

    if (ret >= 0 && got_frame) {
        int ch, plane_size;
        int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
        int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
                                                   frame.nb_samples,
                                                   avctx->sample_fmt, 1);
        if (*frame_size_ptr < data_size) {
            av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
                   "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
            return AVERROR(EINVAL);
        }

        memcpy(samples, frame.extended_data[0], plane_size);

        if (planar && avctx->channels > 1) {
            uint8_t *out = ((uint8_t *)samples) + plane_size;
            for (ch = 1; ch < avctx->channels; ch++) {
                memcpy(out, frame.extended_data[ch], plane_size);
                out += plane_size;
            }
        }
        *frame_size_ptr = data_size;
    } else {
        *frame_size_ptr = 0;
    }
    return ret;
}
#endif

int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
                                              AVFrame *frame,
                                              int *got_frame_ptr,
                                              AVPacket *avpkt)
{
    int ret = 0;

    *got_frame_ptr = 0;
F
Fabrice Bellard 已提交
1314

1315 1316
    avctx->pkt = avpkt;

1317 1318 1319 1320 1321
    if (!avpkt->data && avpkt->size) {
        av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
        return AVERROR(EINVAL);
    }

1322 1323
    apply_param_change(avctx, avpkt);

J
Justin Ruggles 已提交
1324 1325 1326 1327 1328
    if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
        ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
        if (ret >= 0 && *got_frame_ptr) {
            avctx->frame_number++;
            frame->pkt_dts = avpkt->dts;
1329 1330
            if (frame->format == AV_SAMPLE_FMT_NONE)
                frame->format = avctx->sample_fmt;
1331
        }
M
Michael Niedermayer 已提交
1332
    }
F
Fabrice Bellard 已提交
1333 1334 1335
    return ret;
}

1336 1337 1338
int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
                            int *got_sub_ptr,
                            AVPacket *avpkt)
F
Fabrice Bellard 已提交
1339 1340 1341
{
    int ret;

1342
    avctx->pkt = avpkt;
F
Fabrice Bellard 已提交
1343
    *got_sub_ptr = 0;
1344
    ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
F
Fabrice Bellard 已提交
1345 1346 1347 1348 1349
    if (*got_sub_ptr)
        avctx->frame_number++;
    return ret;
}

1350 1351 1352 1353 1354 1355
void avsubtitle_free(AVSubtitle *sub)
{
    int i;

    for (i = 0; i < sub->num_rects; i++)
    {
1356 1357 1358 1359 1360 1361 1362
        av_freep(&sub->rects[i]->pict.data[0]);
        av_freep(&sub->rects[i]->pict.data[1]);
        av_freep(&sub->rects[i]->pict.data[2]);
        av_freep(&sub->rects[i]->pict.data[3]);
        av_freep(&sub->rects[i]->text);
        av_freep(&sub->rects[i]->ass);
        av_freep(&sub->rects[i]);
1363 1364
    }

1365
    av_freep(&sub->rects);
1366 1367 1368 1369

    memset(sub, 0, sizeof(AVSubtitle));
}

1370
av_cold int avcodec_close(AVCodecContext *avctx)
F
Fabrice Bellard 已提交
1371
{
1372 1373 1374 1375 1376 1377
    /* 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;
    }

1378 1379 1380 1381 1382 1383 1384
    entangled_thread_counter++;
    if(entangled_thread_counter != 1){
        av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
        entangled_thread_counter--;
        return -1;
    }

1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
    if (avcodec_is_open(avctx)) {
        if (HAVE_THREADS && avctx->thread_opaque)
            ff_thread_free(avctx);
        if (avctx->codec && avctx->codec->close)
            avctx->codec->close(avctx);
        avcodec_default_free_buffers(avctx);
        avctx->coded_frame = NULL;
        av_freep(&avctx->internal);
    }

    if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1396 1397
        av_opt_free(avctx->priv_data);
    av_opt_free(avctx);
1398
    av_freep(&avctx->priv_data);
1399
    if (av_codec_is_encoder(avctx->codec))
1400
        av_freep(&avctx->extradata);
F
Fabrice Bellard 已提交
1401
    avctx->codec = NULL;
1402
    avctx->active_thread_type = 0;
1403
    entangled_thread_counter--;
1404 1405 1406 1407 1408

    /* Release any user-supplied mutex. */
    if (ff_lockmgr_cb) {
        (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
    }
F
Fabrice Bellard 已提交
1409 1410 1411 1412 1413
    return 0;
}

AVCodec *avcodec_find_encoder(enum CodecID id)
{
1414
    AVCodec *p, *experimental=NULL;
F
Fabrice Bellard 已提交
1415 1416
    p = first_avcodec;
    while (p) {
1417
        if (av_codec_is_encoder(p) && p->id == id) {
1418 1419 1420 1421 1422
            if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
                experimental = p;
            } else
                return p;
        }
F
Fabrice Bellard 已提交
1423 1424
        p = p->next;
    }
1425
    return experimental;
F
Fabrice Bellard 已提交
1426 1427
}

1428 1429 1430
AVCodec *avcodec_find_encoder_by_name(const char *name)
{
    AVCodec *p;
1431 1432
    if (!name)
        return NULL;
1433 1434
    p = first_avcodec;
    while (p) {
1435
        if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
1436 1437 1438 1439 1440 1441
            return p;
        p = p->next;
    }
    return NULL;
}

F
Fabrice Bellard 已提交
1442 1443 1444 1445 1446
AVCodec *avcodec_find_decoder(enum CodecID id)
{
    AVCodec *p;
    p = first_avcodec;
    while (p) {
1447
        if (av_codec_is_decoder(p) && p->id == id)
F
Fabrice Bellard 已提交
1448 1449 1450 1451 1452 1453 1454 1455 1456
            return p;
        p = p->next;
    }
    return NULL;
}

AVCodec *avcodec_find_decoder_by_name(const char *name)
{
    AVCodec *p;
1457 1458
    if (!name)
        return NULL;
F
Fabrice Bellard 已提交
1459 1460
    p = first_avcodec;
    while (p) {
1461
        if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
F
Fabrice Bellard 已提交
1462 1463 1464 1465 1466 1467
            return p;
        p = p->next;
    }
    return NULL;
}

1468
static int get_bit_rate(AVCodecContext *ctx)
1469 1470 1471 1472 1473
{
    int bit_rate;
    int bits_per_sample;

    switch(ctx->codec_type) {
1474
    case AVMEDIA_TYPE_VIDEO:
1475 1476 1477
    case AVMEDIA_TYPE_DATA:
    case AVMEDIA_TYPE_SUBTITLE:
    case AVMEDIA_TYPE_ATTACHMENT:
1478 1479
        bit_rate = ctx->bit_rate;
        break;
1480
    case AVMEDIA_TYPE_AUDIO:
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
        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;
}

1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
{
    int i, len, ret = 0;

    for (i = 0; i < 4; i++) {
        len = snprintf(buf, buf_size,
                       isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
        buf      += len;
        buf_size  = buf_size > len ? buf_size - len : 0;
        ret      += len;
        codec_tag>>=8;
    }
    return ret;
}

F
Fabrice Bellard 已提交
1506 1507 1508
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
{
    const char *codec_name;
1509
    const char *profile = NULL;
F
Fabrice Bellard 已提交
1510 1511
    AVCodec *p;
    char buf1[32];
F
Fabrice Bellard 已提交
1512
    int bitrate;
1513
    AVRational display_aspect_ratio;
F
Fabrice Bellard 已提交
1514

1515 1516 1517
    if (enc->codec)
        p = enc->codec;
    else if (encode)
F
Fabrice Bellard 已提交
1518 1519 1520 1521 1522 1523
        p = avcodec_find_encoder(enc->codec_id);
    else
        p = avcodec_find_decoder(enc->codec_id);

    if (p) {
        codec_name = p->name;
1524
        profile = av_get_profile_name(p, enc->profile);
1525 1526 1527 1528
    } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
        /* fake mpeg2 transport stream codec (currently not
           registered) */
        codec_name = "mpeg2ts";
F
Fabrice Bellard 已提交
1529 1530 1531 1532
    } else if (enc->codec_name[0] != '\0') {
        codec_name = enc->codec_name;
    } else {
        /* output avi tags */
1533 1534 1535
        char tag_buf[32];
        av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
        snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
F
Fabrice Bellard 已提交
1536 1537 1538 1539
        codec_name = buf1;
    }

    switch(enc->codec_type) {
1540
    case AVMEDIA_TYPE_VIDEO:
F
Fabrice Bellard 已提交
1541 1542
        snprintf(buf, buf_size,
                 "Video: %s%s",
1543
                 codec_name, enc->mb_decision ? " (hq)" : "");
1544 1545 1546
        if (profile)
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
                     " (%s)", profile);
1547
        if (enc->pix_fmt != PIX_FMT_NONE) {
F
Fabrice Bellard 已提交
1548 1549
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
                     ", %s",
1550
                     av_get_pix_fmt_name(enc->pix_fmt));
F
Fabrice Bellard 已提交
1551
        }
F
Fabrice Bellard 已提交
1552 1553
        if (enc->width) {
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1554 1555
                     ", %dx%d",
                     enc->width, enc->height);
1556
            if (enc->sample_aspect_ratio.num) {
B
Baptiste Coudurier 已提交
1557 1558 1559 1560 1561 1562 1563 1564
                av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
                          enc->width*enc->sample_aspect_ratio.num,
                          enc->height*enc->sample_aspect_ratio.den,
                          1024*1024);
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
                         " [PAR %d:%d DAR %d:%d]",
                         enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
                         display_aspect_ratio.num, display_aspect_ratio.den);
1565
            }
M
Måns Rullgård 已提交
1566
            if(av_log_get_level() >= AV_LOG_DEBUG){
1567
                int g= av_gcd(enc->time_base.num, enc->time_base.den);
1568 1569 1570 1571
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
                     ", %d/%d",
                     enc->time_base.num/g, enc->time_base.den/g);
            }
F
Fabrice Bellard 已提交
1572
        }
F
Fabrice Bellard 已提交
1573 1574 1575 1576
        if (encode) {
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
                     ", q=%d-%d", enc->qmin, enc->qmax);
        }
F
Fabrice Bellard 已提交
1577
        break;
1578
    case AVMEDIA_TYPE_AUDIO:
F
Fabrice Bellard 已提交
1579 1580 1581
        snprintf(buf, buf_size,
                 "Audio: %s",
                 codec_name);
1582 1583 1584
        if (profile)
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
                     " (%s)", profile);
F
Fabrice Bellard 已提交
1585 1586
        if (enc->sample_rate) {
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1587
                     ", %d Hz", enc->sample_rate);
F
Fabrice Bellard 已提交
1588
        }
1589
        av_strlcat(buf, ", ", buf_size);
1590
        av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1591
        if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1592
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
1593
                     ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1594
        }
F
Fabrice Bellard 已提交
1595
        break;
1596
    case AVMEDIA_TYPE_DATA:
1597
        snprintf(buf, buf_size, "Data: %s", codec_name);
F
Fabrice Bellard 已提交
1598
        break;
1599
    case AVMEDIA_TYPE_SUBTITLE:
F
Fabrice Bellard 已提交
1600
        snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1601
        break;
1602
    case AVMEDIA_TYPE_ATTACHMENT:
1603 1604
        snprintf(buf, buf_size, "Attachment: %s", codec_name);
        break;
F
Fabrice Bellard 已提交
1605
    default:
M
Michael Niedermayer 已提交
1606 1607
        snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
        return;
F
Fabrice Bellard 已提交
1608
    }
F
Fabrice Bellard 已提交
1609 1610 1611 1612 1613 1614 1615 1616
    if (encode) {
        if (enc->flags & CODEC_FLAG_PASS1)
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
                     ", pass 1");
        if (enc->flags & CODEC_FLAG_PASS2)
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
                     ", pass 2");
    }
1617
    bitrate = get_bit_rate(enc);
F
Fabrice Bellard 已提交
1618
    if (bitrate != 0) {
1619
        snprintf(buf + strlen(buf), buf_size - strlen(buf),
F
Fabrice Bellard 已提交
1620
                 ", %d kb/s", bitrate / 1000);
F
Fabrice Bellard 已提交
1621 1622 1623
    }
}

1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
const char *av_get_profile_name(const AVCodec *codec, int profile)
{
    const AVProfile *p;
    if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
        return NULL;

    for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
        if (p->profile == profile)
            return p->name;

    return NULL;
}

N
Nick Kurshev 已提交
1637 1638 1639 1640
unsigned avcodec_version( void )
{
  return LIBAVCODEC_VERSION_INT;
}
F
Fabrice Bellard 已提交
1641

1642
const char *avcodec_configuration(void)
1643
{
1644
    return LIBAV_CONFIGURATION;
1645 1646
}

1647
const char *avcodec_license(void)
1648 1649
{
#define LICENSE_PREFIX "libavcodec license: "
1650
    return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1651 1652
}

M
Michael Niedermayer 已提交
1653 1654
void avcodec_flush_buffers(AVCodecContext *avctx)
{
1655
    if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1656
        ff_thread_flush(avctx);
1657
    else if(avctx->codec->flush)
M
Michael Niedermayer 已提交
1658
        avctx->codec->flush(avctx);
M
Michael Niedermayer 已提交
1659 1660
}

J
Justin Ruggles 已提交
1661 1662
static void video_free_buffers(AVCodecContext *s)
{
1663
    AVCodecInternal *avci = s->internal;
1664 1665
    int i, j;

1666 1667
    if (!avci->buffer)
        return;
1668

1669 1670 1671
    if (avci->buffer_count)
        av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
               avci->buffer_count);
1672
    for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1673
        InternalBuffer *buf = &avci->buffer[i];
1674 1675 1676 1677 1678
        for(j=0; j<4; j++){
            av_freep(&buf->base[j]);
            buf->data[j]= NULL;
        }
    }
1679
    av_freep(&avci->buffer);
1680

1681
    avci->buffer_count=0;
1682 1683
}

J
Justin Ruggles 已提交
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
static void audio_free_buffers(AVCodecContext *avctx)
{
    AVCodecInternal *avci = avctx->internal;
    InternalBuffer *buf;

    if (!avci->buffer)
        return;
    buf = avci->buffer;

    if (buf->extended_data) {
        av_free(buf->extended_data[0]);
        if (buf->extended_data != buf->data)
            av_free(buf->extended_data);
    }
    av_freep(&avci->buffer);
}

void avcodec_default_free_buffers(AVCodecContext *avctx)
{
    switch (avctx->codec_type) {
    case AVMEDIA_TYPE_VIDEO:
        video_free_buffers(avctx);
        break;
    case AVMEDIA_TYPE_AUDIO:
        audio_free_buffers(avctx);
        break;
    default:
        break;
    }
}

1715 1716
int av_get_exact_bits_per_sample(enum CodecID codec_id)
{
1717
    switch(codec_id){
1718
    case CODEC_ID_ADPCM_CT:
1719
    case CODEC_ID_ADPCM_IMA_APC:
1720 1721
    case CODEC_ID_ADPCM_IMA_EA_SEAD:
    case CODEC_ID_ADPCM_IMA_WS:
1722
    case CODEC_ID_ADPCM_G722:
1723
    case CODEC_ID_ADPCM_YAMAHA:
S
Sjoerd Simons 已提交
1724
        return 4;
1725 1726 1727 1728
    case CODEC_ID_PCM_ALAW:
    case CODEC_ID_PCM_MULAW:
    case CODEC_ID_PCM_S8:
    case CODEC_ID_PCM_U8:
1729
    case CODEC_ID_PCM_ZORK:
1730 1731 1732
        return 8;
    case CODEC_ID_PCM_S16BE:
    case CODEC_ID_PCM_S16LE:
1733
    case CODEC_ID_PCM_S16LE_PLANAR:
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746
    case CODEC_ID_PCM_U16BE:
    case CODEC_ID_PCM_U16LE:
        return 16;
    case CODEC_ID_PCM_S24DAUD:
    case CODEC_ID_PCM_S24BE:
    case CODEC_ID_PCM_S24LE:
    case CODEC_ID_PCM_U24BE:
    case CODEC_ID_PCM_U24LE:
        return 24;
    case CODEC_ID_PCM_S32BE:
    case CODEC_ID_PCM_S32LE:
    case CODEC_ID_PCM_U32BE:
    case CODEC_ID_PCM_U32LE:
1747
    case CODEC_ID_PCM_F32BE:
1748
    case CODEC_ID_PCM_F32LE:
1749
        return 32;
1750 1751 1752
    case CODEC_ID_PCM_F64BE:
    case CODEC_ID_PCM_F64LE:
        return 64;
1753 1754 1755 1756 1757
    default:
        return 0;
    }
}

1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
int av_get_bits_per_sample(enum CodecID codec_id)
{
    switch (codec_id) {
    case CODEC_ID_ADPCM_SBPRO_2:
        return 2;
    case CODEC_ID_ADPCM_SBPRO_3:
        return 3;
    case CODEC_ID_ADPCM_SBPRO_4:
    case CODEC_ID_ADPCM_IMA_WAV:
    case CODEC_ID_ADPCM_IMA_QT:
    case CODEC_ID_ADPCM_SWF:
    case CODEC_ID_ADPCM_MS:
        return 4;
    default:
        return av_get_exact_bits_per_sample(codec_id);
    }
}

1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
{
    int id, sr, ch, ba, tag, bps;

    id  = avctx->codec_id;
    sr  = avctx->sample_rate;
    ch  = avctx->channels;
    ba  = avctx->block_align;
    tag = avctx->codec_tag;
    bps = av_get_exact_bits_per_sample(avctx->codec_id);

    /* codecs with an exact constant bits per sample */
    if (bps > 0 && ch > 0 && frame_bytes > 0)
        return (frame_bytes * 8) / (bps * ch);
    bps = avctx->bits_per_coded_sample;

    /* codecs with a fixed packet duration */
    switch (id) {
    case CODEC_ID_ADPCM_ADX:    return   32;
    case CODEC_ID_ADPCM_IMA_QT: return   64;
    case CODEC_ID_ADPCM_EA_XAS: return  128;
    case CODEC_ID_AMR_NB:
    case CODEC_ID_GSM:
    case CODEC_ID_QCELP:
    case CODEC_ID_RA_144:
    case CODEC_ID_RA_288:       return  160;
    case CODEC_ID_IMC:          return  256;
    case CODEC_ID_AMR_WB:
    case CODEC_ID_GSM_MS:       return  320;
    case CODEC_ID_MP1:          return  384;
    case CODEC_ID_ATRAC1:       return  512;
    case CODEC_ID_ATRAC3:       return 1024;
    case CODEC_ID_MP2:
    case CODEC_ID_MUSEPACK7:    return 1152;
    case CODEC_ID_AC3:          return 1536;
    }

    if (sr > 0) {
        /* calc from sample rate */
        if (id == CODEC_ID_TTA)
            return 256 * sr / 245;

        if (ch > 0) {
            /* calc from sample rate and channels */
            if (id == CODEC_ID_BINKAUDIO_DCT)
                return (480 << (sr / 22050)) / ch;
        }
    }

    if (ba > 0) {
        /* calc from block_align */
        if (id == CODEC_ID_SIPR) {
            switch (ba) {
            case 20: return 160;
            case 19: return 144;
            case 29: return 288;
            case 37: return 480;
            }
        }
    }

    if (frame_bytes > 0) {
        /* calc from frame_bytes only */
        if (id == CODEC_ID_TRUESPEECH)
            return 240 * (frame_bytes / 32);
        if (id == CODEC_ID_NELLYMOSER)
            return 256 * (frame_bytes / 64);

        if (bps > 0) {
            /* calc from frame_bytes and bits_per_coded_sample */
            if (id == CODEC_ID_ADPCM_G726)
                return frame_bytes * 8 / bps;
        }

        if (ch > 0) {
            /* calc from frame_bytes and channels */
            switch (id) {
            case CODEC_ID_ADPCM_4XM:
            case CODEC_ID_ADPCM_IMA_ISS:
                return (frame_bytes - 4 * ch) * 2 / ch;
            case CODEC_ID_ADPCM_IMA_SMJPEG:
                return (frame_bytes - 4) * 2 / ch;
            case CODEC_ID_ADPCM_IMA_AMV:
                return (frame_bytes - 8) * 2 / ch;
            case CODEC_ID_ADPCM_XA:
                return (frame_bytes / 128) * 224 / ch;
            case CODEC_ID_INTERPLAY_DPCM:
                return (frame_bytes - 6 - ch) / ch;
            case CODEC_ID_ROQ_DPCM:
                return (frame_bytes - 8) / ch;
            case CODEC_ID_XAN_DPCM:
                return (frame_bytes - 2 * ch) / ch;
            case CODEC_ID_MACE3:
                return 3 * frame_bytes / ch;
            case CODEC_ID_MACE6:
                return 6 * frame_bytes / ch;
            case CODEC_ID_PCM_LXF:
                return 2 * (frame_bytes / (5 * ch));
            }

            if (tag) {
                /* calc from frame_bytes, channels, and codec_tag */
                if (id == CODEC_ID_SOL_DPCM) {
                    if (tag == 3)
                        return frame_bytes / ch;
                    else
                        return frame_bytes * 2 / ch;
                }
            }

            if (ba > 0) {
                /* calc from frame_bytes, channels, and block_align */
                int blocks = frame_bytes / ba;
                switch (avctx->codec_id) {
                case CODEC_ID_ADPCM_IMA_WAV:
                    return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
                case CODEC_ID_ADPCM_IMA_DK3:
1893
                    return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
                case CODEC_ID_ADPCM_IMA_DK4:
                    return blocks * (1 + (ba - 4 * ch) * 2 / ch);
                case CODEC_ID_ADPCM_MS:
                    return blocks * (2 + (ba - 7 * ch) * 2 / ch);
                }
            }

            if (bps > 0) {
                /* calc from frame_bytes, channels, and bits_per_coded_sample */
                switch (avctx->codec_id) {
                case CODEC_ID_PCM_DVD:
                    return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
                case CODEC_ID_PCM_BLURAY:
                    return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
                case CODEC_ID_S302M:
                    return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
                }
            }
        }
    }

    return 0;
}

1918
#if !HAVE_THREADS
1919
int ff_thread_init(AVCodecContext *s){
1920 1921 1922
    return -1;
}
#endif
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936

unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
{
    unsigned int n = 0;

    while(v >= 0xff) {
        *s++ = 0xff;
        v -= 0xff;
        n++;
    }
    *s = v;
    n++;
    return n;
}
1937

1938 1939 1940 1941 1942 1943
int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
    int i;
    for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
    return i;
}

1944
void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1945
{
1946
    av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1947
            "version to the newest one from Git. If the problem still "
1948
            "occurs, it means that your file has a feature which has not "
1949
            "been implemented.\n", feature);
1950
    if(want_sample)
1951
        av_log_ask_for_sample(avc, NULL);
1952 1953
}

1954
void av_log_ask_for_sample(void *avc, const char *msg, ...)
1955
{
1956 1957 1958 1959
    va_list argument_list;

    va_start(argument_list, msg);

1960
    if (msg)
1961
        av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1962
    av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1963
            "of this file to ftp://upload.libav.org/incoming/ "
1964
            "and contact the libav-devel mailing list.\n");
1965 1966

    va_end(argument_list);
1967
}
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978

static AVHWAccel *first_hwaccel = NULL;

void av_register_hwaccel(AVHWAccel *hwaccel)
{
    AVHWAccel **p = &first_hwaccel;
    while (*p)
        p = &(*p)->next;
    *p = hwaccel;
    hwaccel->next = NULL;
}
1979 1980 1981 1982 1983

AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
{
    return hwaccel ? hwaccel->next : first_hwaccel;
}
M
Michael Niedermayer 已提交
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995

AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat 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;
}
1996 1997 1998 1999 2000 2001

int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
{
    if (ff_lockmgr_cb) {
        if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
            return -1;
2002 2003
        if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
            return -1;
2004 2005 2006 2007 2008 2009 2010
    }

    ff_lockmgr_cb = cb;

    if (ff_lockmgr_cb) {
        if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
            return -1;
2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
        if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
            return -1;
    }
    return 0;
}

int avpriv_lock_avformat(void)
{
    if (ff_lockmgr_cb) {
        if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
            return -1;
    }
    return 0;
}

int avpriv_unlock_avformat(void)
{
    if (ff_lockmgr_cb) {
        if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
            return -1;
2031 2032 2033
    }
    return 0;
}
2034

2035
unsigned int avpriv_toupper4(unsigned int x)
2036 2037 2038 2039 2040 2041
{
    return     toupper( x     &0xFF)
            + (toupper((x>>8 )&0xFF)<<8 )
            + (toupper((x>>16)&0xFF)<<16)
            + (toupper((x>>24)&0xFF)<<24);
}
2042

2043
#if !HAVE_THREADS
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068

int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
{
    f->owner = avctx;
    return avctx->get_buffer(avctx, f);
}

void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
{
    f->owner->release_buffer(f->owner, f);
}

void ff_thread_finish_setup(AVCodecContext *avctx)
{
}

void ff_thread_report_progress(AVFrame *f, int progress, int field)
{
}

void ff_thread_await_progress(AVFrame *f, int progress, int field)
{
}

#endif
2069

2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082
enum AVMediaType avcodec_get_type(enum CodecID codec_id)
{
    if (codec_id <= CODEC_ID_NONE)
        return AVMEDIA_TYPE_UNKNOWN;
    else if (codec_id < CODEC_ID_FIRST_AUDIO)
        return AVMEDIA_TYPE_VIDEO;
    else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
        return AVMEDIA_TYPE_AUDIO;
    else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
        return AVMEDIA_TYPE_SUBTITLE;

    return AVMEDIA_TYPE_UNKNOWN;
}
A
Anton Khirnov 已提交
2083 2084 2085 2086 2087

int avcodec_is_open(AVCodecContext *s)
{
    return !!s->internal;
}