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

Fabrice Bellard's avatar
Fabrice Bellard committed
22
#include "avformat.h"
23
#include "avio_internal.h"
24
#include "internal.h"
25
#include "libavcodec/internal.h"
26
#include "libavcodec/raw.h"
27
#include "libavcodec/bytestream.h"
28
#include "libavutil/accessors.h"
29
#include "libavutil/avassert.h"
30
#include "libavutil/opt.h"
31
#include "libavutil/dict.h"
32
#include "libavutil/internal.h"
33
#include "libavutil/pixdesc.h"
34
#include "metadata.h"
35
#include "id3v2.h"
36
#include "libavutil/avassert.h"
37
#include "libavutil/avstring.h"
38
#include "libavutil/mathematics.h"
39
#include "libavutil/parseutils.h"
40
#include "libavutil/time.h"
41
#include "libavutil/timestamp.h"
42
#include "riff.h"
43
#include "audiointerleave.h"
44
#include "url.h"
45 46 47 48
#include <stdarg.h>
#if CONFIG_NETWORK
#include "network.h"
#endif
49

50 51 52
#undef NDEBUG
#include <assert.h>

53
/**
54
 * @file
55
 * various utility functions for use within FFmpeg
56 57
 */

58 59
unsigned avformat_version(void)
{
60
    av_assert0(LIBAVFORMAT_VERSION_MICRO >= 100);
61 62 63
    return LIBAVFORMAT_VERSION_INT;
}

64
const char *avformat_configuration(void)
65
{
66
    return FFMPEG_CONFIGURATION;
67 68
}

69
const char *avformat_license(void)
70 71
{
#define LICENSE_PREFIX "libavformat license: "
72
    return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
73 74
}

75
#define RELATIVE_TS_BASE (INT64_MAX - (1LL<<48))
76 77

static int is_relative(int64_t ts) {
78
    return ts > (RELATIVE_TS_BASE - (1LL<<48));
79 80
}

81 82 83 84 85 86 87 88 89
/**
 * Wrap a given time stamp, if there is an indication for an overflow
 *
 * @param st stream
 * @param timestamp the time stamp to wrap
 * @return resulting time stamp
 */
static int64_t wrap_timestamp(AVStream *st, int64_t timestamp)
{
90
    if (st->pts_wrap_behavior != AV_PTS_WRAP_IGNORE &&
91 92 93
        st->pts_wrap_reference != AV_NOPTS_VALUE && timestamp != AV_NOPTS_VALUE) {
        if (st->pts_wrap_behavior == AV_PTS_WRAP_ADD_OFFSET &&
            timestamp < st->pts_wrap_reference)
94
            return timestamp + (1ULL<<st->pts_wrap_bits);
95 96
        else if (st->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET &&
            timestamp >= st->pts_wrap_reference)
97
            return timestamp - (1ULL<<st->pts_wrap_bits);
98 99 100 101
    }
    return timestamp;
}

102 103
MAKE_ACCESSORS(AVStream, stream, AVRational, r_frame_rate)

104 105
/* an arbitrarily chosen "sane" max packet size -- 50M */
#define SANE_CHUNK_SIZE (50000000)
Fabrice Bellard's avatar
Fabrice Bellard committed
106

107
int ffio_limit(AVIOContext *s, int size)
Michael Niedermayer's avatar
Michael Niedermayer committed
108
{
109
    if(s->maxsize>=0){
110
        int64_t remaining= s->maxsize - avio_tell(s);
111 112 113
        if(remaining < size){
            int64_t newsize= avio_size(s);
            if(!s->maxsize || s->maxsize<newsize)
114
                s->maxsize= newsize - !newsize;
115
            remaining= s->maxsize - avio_tell(s);
116
            remaining= FFMAX(remaining, 0);
117 118
        }

119
        if(s->maxsize>=0 && remaining+1 < size){
120
            av_log(NULL, remaining ? AV_LOG_ERROR : AV_LOG_DEBUG, "Truncating packet of size %d to %"PRId64"\n", size, remaining+1);
121 122
            size= remaining+1;
        }
123
    }
124 125 126
    return size;
}

127 128 129 130 131
/*
 * Read the data in sane-sized chunks and append to pkt.
 * Return the number of bytes read or an error.
 */
static int append_packet_chunked(AVIOContext *s, AVPacket *pkt, int size)
132
{
133
    int64_t orig_pos   = pkt->pos; // av_grow_packet might reset pos
134
    int orig_size      = pkt->size;
135
    int ret;
136 137 138 139 140 141 142 143 144

    do {
        int prev_size = pkt->size;
        int read_size;

        /*
         * When the caller requests a lot of data, limit it to the amount left
         * in file or SANE_CHUNK_SIZE when it is not known
         */
145 146 147 148 149 150 151
        read_size = size;
        if (read_size > SANE_CHUNK_SIZE/10) {
            read_size = ffio_limit(s, read_size);
            // If filesize/maxsize is unknown, limit to SANE_CHUNK_SIZE
            if (s->maxsize < 0)
                read_size = FFMIN(read_size, SANE_CHUNK_SIZE);
        }
152

153 154 155
        ret = av_grow_packet(pkt, read_size);
        if (ret < 0)
            break;
Michael Niedermayer's avatar
Michael Niedermayer committed
156

157 158 159 160 161
        ret = avio_read(s, pkt->data + prev_size, read_size);
        if (ret != read_size) {
            av_shrink_packet(pkt, prev_size + FFMAX(ret, 0));
            break;
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
162

163 164
        size -= read_size;
    } while (size > 0);
165 166
    if (size > 0)
        pkt->flags |= AV_PKT_FLAG_CORRUPT;
Michael Niedermayer's avatar
Michael Niedermayer committed
167

168 169
    pkt->pos = orig_pos;
    if (!pkt->size)
Michael Niedermayer's avatar
Michael Niedermayer committed
170
        av_free_packet(pkt);
171 172 173 174 175 176 177 178 179
    return pkt->size > orig_size ? pkt->size - orig_size : ret;
}

int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
{
    av_init_packet(pkt);
    pkt->data = NULL;
    pkt->size = 0;
    pkt->pos  = avio_tell(s);
Michael Niedermayer's avatar
Michael Niedermayer committed
180

181
    return append_packet_chunked(s, pkt, size);
Michael Niedermayer's avatar
Michael Niedermayer committed
182 183
}

184
int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
185 186 187
{
    if (!pkt->size)
        return av_get_packet(s, pkt, size);
188
    return append_packet_chunked(s, pkt, size);
189 190
}

191

192
int av_filename_number_test(const char *filename)
193 194
{
    char buf[1024];
195
    return filename && (av_get_frame_filename(buf, sizeof(buf), filename, 1)>=0);
196 197
}

198
AVInputFormat *av_probe_input_format3(AVProbeData *pd, int is_opened, int *score_ret)
199
{
200
    AVProbeData lpd = *pd;
201
    AVInputFormat *fmt1 = NULL, *fmt;
202
    int score, nodat = 0, score_max=0;
203 204 205 206
    const static uint8_t zerobuffer[AVPROBE_PADDING_SIZE];

    if (!lpd.buf)
        lpd.buf = zerobuffer;
207

208 209 210 211 212
    if (lpd.buf_size > 10 && ff_id3v2_match(lpd.buf, ID3v2_DEFAULT_MAGIC)) {
        int id3len = ff_id3v2_tag_len(lpd.buf);
        if (lpd.buf_size > id3len + 16) {
            lpd.buf += id3len;
            lpd.buf_size -= id3len;
213 214
        }else
            nodat = 1;
215 216
    }

217
    fmt = NULL;
218
    while ((fmt1 = av_iformat_next(fmt1))) {
219
        if (!is_opened == !(fmt1->flags & AVFMT_NOFILE))
220 221
            continue;
        score = 0;
222
        if (fmt1->read_probe) {
223
            score = fmt1->read_probe(&lpd);
224
            if(fmt1->extensions && av_match_ext(lpd.filename, fmt1->extensions))
225
                score = FFMAX(score, nodat ? AVPROBE_SCORE_EXTENSION / 2 - 1 : 1);
226
        } else if (fmt1->extensions) {
227
            if (av_match_ext(lpd.filename, fmt1->extensions)) {
228
                score = AVPROBE_SCORE_EXTENSION;
229
            }
230
        }
231 232
        if (score > score_max) {
            score_max = score;
233
            fmt = fmt1;
234
        }else if (score == score_max)
235
            fmt = NULL;
236
    }
237
    if(nodat)
238
        score_max = FFMIN(AVPROBE_SCORE_EXTENSION / 2 - 1, score_max);
239
    *score_ret= score_max;
240

241 242 243
    return fmt;
}

244 245 246 247 248 249 250 251 252 253 254
AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened, int *score_max)
{
    int score_ret;
    AVInputFormat *fmt= av_probe_input_format3(pd, is_opened, &score_ret);
    if(score_ret > *score_max){
        *score_max= score_ret;
        return fmt;
    }else
        return NULL;
}

255 256 257 258 259
AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened){
    int score=0;
    return av_probe_input_format2(pd, is_opened, &score);
}

260
static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st, AVProbeData *pd)
261
{
262
    static const struct {
263
        const char *name; enum AVCodecID id; enum AVMediaType type;
264
    } fmt_id_type[] = {
265 266 267 268 269
        { "aac"      , AV_CODEC_ID_AAC       , AVMEDIA_TYPE_AUDIO },
        { "ac3"      , AV_CODEC_ID_AC3       , AVMEDIA_TYPE_AUDIO },
        { "dts"      , AV_CODEC_ID_DTS       , AVMEDIA_TYPE_AUDIO },
        { "eac3"     , AV_CODEC_ID_EAC3      , AVMEDIA_TYPE_AUDIO },
        { "h264"     , AV_CODEC_ID_H264      , AVMEDIA_TYPE_VIDEO },
270
        { "loas"     , AV_CODEC_ID_AAC_LATM  , AVMEDIA_TYPE_AUDIO },
271 272 273
        { "m4v"      , AV_CODEC_ID_MPEG4     , AVMEDIA_TYPE_VIDEO },
        { "mp3"      , AV_CODEC_ID_MP3       , AVMEDIA_TYPE_AUDIO },
        { "mpegvideo", AV_CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
274 275
        { 0 }
    };
276 277
    int score;
    AVInputFormat *fmt = av_probe_input_format3(pd, 1, &score);
278

279
    if (fmt && st->request_probe <= score) {
280
        int i;
281 282
        av_log(s, AV_LOG_DEBUG, "Probe with size=%d, packets=%d detected %s with score=%d\n",
               pd->buf_size, MAX_PROBE_PACKETS - st->probe_packets, fmt->name, score);
283 284 285 286 287 288
        for (i = 0; fmt_id_type[i].name; i++) {
            if (!strcmp(fmt->name, fmt_id_type[i].name)) {
                st->codec->codec_id   = fmt_id_type[i].id;
                st->codec->codec_type = fmt_id_type[i].type;
                break;
            }
289
        }
290
    }
291
    return score;
292 293
}

294 295
/************************************************************/
/* input media file */
296

297
int av_demuxer_open(AVFormatContext *ic){
298 299 300
    int err;

    if (ic->iformat->read_header) {
301
        err = ic->iformat->read_header(ic);
302 303 304 305 306 307 308 309 310 311 312
        if (err < 0)
            return err;
    }

    if (ic->pb && !ic->data_offset)
        ic->data_offset = avio_tell(ic->pb);

    return 0;
}


313
/** size of probe buffer, for guessing file type from file contents */
314
#define PROBE_BUF_MIN 2048
315
#define PROBE_BUF_MAX (1<<20)
316

317
int av_probe_input_buffer(AVIOContext *pb, AVInputFormat **fmt,
318 319 320 321 322
                          const char *filename, void *logctx,
                          unsigned int offset, unsigned int max_probe_size)
{
    AVProbeData pd = { filename ? filename : "", NULL, -offset };
    unsigned char *buf = NULL;
323
    uint8_t *mime_type;
324
    int ret = 0, probe_size, buf_offset = 0;
325 326 327 328 329 330

    if (!max_probe_size) {
        max_probe_size = PROBE_BUF_MAX;
    } else if (max_probe_size > PROBE_BUF_MAX) {
        max_probe_size = PROBE_BUF_MAX;
    } else if (max_probe_size < PROBE_BUF_MIN) {
331 332
        av_log(logctx, AV_LOG_ERROR,
               "Specified probe size value %u cannot be < %u\n", max_probe_size, PROBE_BUF_MIN);
333 334 335 336 337 338 339
        return AVERROR(EINVAL);
    }

    if (offset >= max_probe_size) {
        return AVERROR(EINVAL);
    }

340
    if (!*fmt && pb->av_class && av_opt_get(pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &mime_type) >= 0 && mime_type) {
341
        if (!av_strcasecmp(mime_type, "audio/aacp")) {
342 343
            *fmt = av_find_input_format("aac");
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
344
        av_freep(&mime_type);
345 346
    }

347
    for(probe_size= PROBE_BUF_MIN; probe_size<=max_probe_size && !*fmt;
348
        probe_size = FFMIN(probe_size<<1, FFMAX(max_probe_size, probe_size+1))) {
349
        int score = probe_size < max_probe_size ? AVPROBE_SCORE_RETRY : 0;
350
        void *buftmp;
351 352 353 354 355 356

        if (probe_size < offset) {
            continue;
        }

        /* read probe data */
357 358 359 360 361 362
        buftmp = av_realloc(buf, probe_size + AVPROBE_PADDING_SIZE);
        if(!buftmp){
            av_free(buf);
            return AVERROR(ENOMEM);
        }
        buf=buftmp;
363
        if ((ret = avio_read(pb, buf + buf_offset, probe_size - buf_offset)) < 0) {
364 365 366 367 368 369
            /* fail if error was not end of file, otherwise, lower score */
            if (ret != AVERROR_EOF) {
                av_free(buf);
                return ret;
            }
            score = 0;
370
            ret = 0;            /* error was end of file, nothing read */
371
        }
372
        pd.buf_size = buf_offset += ret;
373 374 375 376 377 378 379
        pd.buf = &buf[offset];

        memset(pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE);

        /* guess file format */
        *fmt = av_probe_input_format2(&pd, 1, &score);
        if(*fmt){
380
            if(score <= AVPROBE_SCORE_RETRY){ //this can only be true in the last iteration
381
                av_log(logctx, AV_LOG_WARNING, "Format %s detected only with low score of %d, misdetection possible!\n", (*fmt)->name, score);
382
            }else
383
                av_log(logctx, AV_LOG_DEBUG, "Format %s probed with size=%d and score=%d\n", (*fmt)->name, probe_size, score);
384 385 386
        }
    }

387
    if (!*fmt) {
388
        av_free(buf);
389 390 391
        return AVERROR_INVALIDDATA;
    }

392
    /* rewind. reuse probe buffer to avoid seeking */
393
    ret = ffio_rewind_with_probe_data(pb, &buf, pd.buf_size);
394

395
    return ret;
396 397
}

398
/* open input file and probe the format if necessary */
399
static int init_input(AVFormatContext *s, const char *filename, AVDictionary **options)
400 401 402
{
    int ret;
    AVProbeData pd = {filename, NULL, 0};
403
    int score = AVPROBE_SCORE_RETRY;
404 405 406 407

    if (s->pb) {
        s->flags |= AVFMT_FLAG_CUSTOM_IO;
        if (!s->iformat)
408
            return av_probe_input_buffer(s->pb, &s->iformat, filename, s, 0, s->probesize);
409
        else if (s->iformat->flags & AVFMT_NOFILE)
410 411
            av_log(s, AV_LOG_WARNING, "Custom AVIOContext makes no sense and "
                                      "will be ignored with AVFMT_NOFILE format.\n");
412
        return 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
413 414
    }

415
    if ( (s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
416
        (!s->iformat && (s->iformat = av_probe_input_format2(&pd, 0, &score))))
417 418
        return 0;

419
    if ((ret = avio_open2(&s->pb, filename, AVIO_FLAG_READ | s->avio_flags,
420
                          &s->interrupt_callback, options)) < 0)
421
        return ret;
422 423
    if (s->iformat)
        return 0;
424
    return av_probe_input_buffer(s->pb, &s->iformat, filename, s, 0, s->probesize);
425 426
}

427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
static AVPacket *add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt,
                               AVPacketList **plast_pktl){
    AVPacketList *pktl = av_mallocz(sizeof(AVPacketList));
    if (!pktl)
        return NULL;

    if (*packet_buffer)
        (*plast_pktl)->next = pktl;
    else
        *packet_buffer = pktl;

    /* add the packet in the buffered packet list */
    *plast_pktl = pktl;
    pktl->pkt= *pkt;
    return &pktl->pkt;
}

444
int avformat_queue_attached_pictures(AVFormatContext *s)
445 446 447
{
    int i;
    for (i = 0; i < s->nb_streams; i++)
448 449
        if (s->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC &&
            s->streams[i]->discard < AVDISCARD_ALL) {
450
            AVPacket copy = s->streams[i]->attached_pic;
451 452 453 454
            copy.buf      = av_buffer_ref(copy.buf);
            if (!copy.buf)
                return AVERROR(ENOMEM);

455 456
            add_to_pktbuf(&s->raw_packet_buffer, &copy, &s->raw_packet_buffer_end);
        }
457
    return 0;
458 459
}

460 461 462
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
{
    AVFormatContext *s = *ps;
463
    int ret = 0;
464
    AVDictionary *tmp = NULL;
465
    ID3v2ExtraMeta *id3v2_extra_meta = NULL;
466 467 468

    if (!s && !(s = avformat_alloc_context()))
        return AVERROR(ENOMEM);
469
    if (!s->av_class){
470
        av_log(NULL, AV_LOG_ERROR, "Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n");
471 472
        return AVERROR(EINVAL);
    }
473 474 475 476 477 478 479 480 481
    if (fmt)
        s->iformat = fmt;

    if (options)
        av_dict_copy(&tmp, *options, 0);

    if ((ret = av_opt_set_dict(s, &tmp)) < 0)
        goto fail;

482
    if ((ret = init_input(s, filename, &tmp)) < 0)
483
        goto fail;
484
    avio_skip(s->pb, s->skip_initial_bytes);
485 486 487 488 489

    /* check filename in case an image number is expected */
    if (s->iformat->flags & AVFMT_NEEDNUMBER) {
        if (!av_filename_number_test(filename)) {
            ret = AVERROR(EINVAL);
490
            goto fail;
491
        }
492 493 494
    }

    s->duration = s->start_time = AV_NOPTS_VALUE;
495
    av_strlcpy(s->filename, filename ? filename : "", sizeof(s->filename));
496 497 498 499 500

    /* allocate private data */
    if (s->iformat->priv_data_size > 0) {
        if (!(s->priv_data = av_mallocz(s->iformat->priv_data_size))) {
            ret = AVERROR(ENOMEM);
501
            goto fail;
502
        }
503 504 505 506 507 508
        if (s->iformat->priv_class) {
            *(const AVClass**)s->priv_data = s->iformat->priv_class;
            av_opt_set_defaults(s->priv_data);
            if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
                goto fail;
        }
509 510
    }

511 512
    /* e.g. AVFMT_NOFILE formats will not have a AVIOContext */
    if (s->pb)
513
        ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
514

515
    if (!(s->flags&AVFMT_FLAG_PRIV_OPT) && s->iformat->read_header)
516
        if ((ret = s->iformat->read_header(s)) < 0)
517
            goto fail;
518

519
    if (id3v2_extra_meta) {
520 521
        if (!strcmp(s->iformat->name, "mp3") || !strcmp(s->iformat->name, "aac") ||
            !strcmp(s->iformat->name, "tta")) {
522 523 524
            if((ret = ff_id3v2_parse_apic(s, &id3v2_extra_meta)) < 0)
                goto fail;
        } else
525
            av_log(s, AV_LOG_DEBUG, "demuxer does not support additional id3 data, skipping\n");
526
    }
527 528
    ff_id3v2_free_extra_meta(&id3v2_extra_meta);

529
    if ((ret = avformat_queue_attached_pictures(s)) < 0)
530
        goto fail;
531

532
    if (!(s->flags&AVFMT_FLAG_PRIV_OPT) && s->pb && !s->data_offset)
533 534 535 536 537 538 539
        s->data_offset = avio_tell(s->pb);

    s->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;

    if (options) {
        av_dict_free(options);
        *options = tmp;
540
    }
541
    *ps = s;
542
    return 0;
543

544
fail:
545
    ff_id3v2_free_extra_meta(&id3v2_extra_meta);
546 547 548 549 550 551
    av_dict_free(&tmp);
    if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO))
        avio_close(s->pb);
    avformat_free_context(s);
    *ps = NULL;
    return ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
552 553
}

554 555
/*******************************************************/

556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
static void force_codec_ids(AVFormatContext *s, AVStream *st)
{
    switch(st->codec->codec_type){
    case AVMEDIA_TYPE_VIDEO:
        if(s->video_codec_id)   st->codec->codec_id= s->video_codec_id;
        break;
    case AVMEDIA_TYPE_AUDIO:
        if(s->audio_codec_id)   st->codec->codec_id= s->audio_codec_id;
        break;
    case AVMEDIA_TYPE_SUBTITLE:
        if(s->subtitle_codec_id)st->codec->codec_id= s->subtitle_codec_id;
        break;
    }
}

571 572
static void probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
{
573
    if(st->request_probe>0){
574
        AVProbeData *pd = &st->probe_data;
575 576
        int end;
        av_log(s, AV_LOG_DEBUG, "probing stream %d pp:%d\n", st->index, st->probe_packets);
577 578
        --st->probe_packets;

579
        if (pkt) {
580 581 582 583
            uint8_t *new_buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
            if(!new_buf)
                goto no_packet;
            pd->buf = new_buf;
584 585 586 587
            memcpy(pd->buf+pd->buf_size, pkt->data, pkt->size);
            pd->buf_size += pkt->size;
            memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE);
        } else {
588
no_packet:
589
            st->probe_packets = 0;
590
            if (!pd->buf_size) {
591
                av_log(s, AV_LOG_WARNING, "nothing to probe for stream %d\n",
592 593
                       st->index);
            }
594
        }
595

596 597 598 599 600
        end=    s->raw_packet_buffer_remaining_size <= 0
                || st->probe_packets<=0;

        if(end || av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)){
            int score= set_codec_from_probe_data(s, st, pd);
601
            if(    (st->codec->codec_id != AV_CODEC_ID_NONE && score > AVPROBE_SCORE_RETRY)
602
                || end){
603 604
                pd->buf_size=0;
                av_freep(&pd->buf);
605
                st->request_probe= -1;
606
                if(st->codec->codec_id != AV_CODEC_ID_NONE){
607 608 609
                    av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index);
                }else
                    av_log(s, AV_LOG_WARNING, "probed stream %d failed\n", st->index);
610
            }
611
            force_codec_ids(s, st);
612 613 614 615
        }
    }
}

616
int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
617
{
618
    int ret, i;
619
    AVStream *st;
620 621 622 623 624 625

    for(;;){
        AVPacketList *pktl = s->raw_packet_buffer;

        if (pktl) {
            *pkt = pktl->pkt;
626
            st = s->streams[pkt->stream_index];
627 628
            if (s->raw_packet_buffer_remaining_size <= 0)
                probe_codec(s, st, NULL);
629
            if(st->request_probe <= 0){
630
                s->raw_packet_buffer = pktl->next;
631
                s->raw_packet_buffer_remaining_size += pkt->size;
632 633 634 635 636
                av_free(pktl);
                return 0;
            }
        }

637 638
        pkt->data = NULL;
        pkt->size = 0;
Michael Niedermayer's avatar
Michael Niedermayer committed
639 640
        av_init_packet(pkt);
        ret= s->iformat->read_packet(s, pkt);
641 642 643
        if (ret < 0) {
            if (!pktl || ret == AVERROR(EAGAIN))
                return ret;
644 645 646 647 648
            for (i = 0; i < s->nb_streams; i++) {
                st = s->streams[i];
                if (st->probe_packets) {
                    probe_codec(s, st, NULL);
                }
649
                av_assert0(st->request_probe <= 0);
650
            }
651 652
            continue;
        }
653

654 655 656 657 658
        if ((s->flags & AVFMT_FLAG_DISCARD_CORRUPT) &&
            (pkt->flags & AV_PKT_FLAG_CORRUPT)) {
            av_log(s, AV_LOG_WARNING,
                   "Dropped corrupted packet (stream = %d)\n",
                   pkt->stream_index);
659
            av_free_packet(pkt);
660 661 662
            continue;
        }

663 664
        if(!(s->flags & AVFMT_FLAG_KEEP_SIDE_DATA))
            av_packet_merge_side_data(pkt);
665

666 667 668 669 670
        if(pkt->stream_index >= (unsigned)s->nb_streams){
            av_log(s, AV_LOG_ERROR, "Invalid stream index %d\n", pkt->stream_index);
            continue;
        }

Michael Niedermayer's avatar
Michael Niedermayer committed
671
        st= s->streams[pkt->stream_index];
672 673
        pkt->dts = wrap_timestamp(st, pkt->dts);
        pkt->pts = wrap_timestamp(st, pkt->pts);
674

675 676
        force_codec_ids(s, st);

677 678 679
        /* TODO: audio: time filter; video: frame reordering (pts != dts) */
        if (s->use_wallclock_as_timestamps)
            pkt->dts = pkt->pts = av_rescale_q(av_gettime(), AV_TIME_BASE_Q, st->time_base);
680

681
        if(!pktl && st->request_probe <= 0)
682 683
            return ret;

684
        add_to_pktbuf(&s->raw_packet_buffer, pkt, &s->raw_packet_buffer_end);
685
        s->raw_packet_buffer_remaining_size -= pkt->size;
686

687
        probe_codec(s, st, pkt);
688
    }
689 690
}

691 692 693 694 695 696 697 698
#if FF_API_READ_PACKET
int av_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    return ff_read_packet(s, pkt);
}
#endif


699 700
/**********************************************************/

701 702
static int determinable_frame_size(AVCodecContext *avctx)
{
703 704 705 706 707
    if (/*avctx->codec_id == AV_CODEC_ID_AAC ||*/
        avctx->codec_id == AV_CODEC_ID_MP1 ||
        avctx->codec_id == AV_CODEC_ID_MP2 ||
        avctx->codec_id == AV_CODEC_ID_MP3/* ||
        avctx->codec_id == AV_CODEC_ID_CELT*/)
708 709 710 711
        return 1;
    return 0;
}

712
/**
713
 * Get the number of samples of an audio frame. Return -1 on error.
714
 */
715
int ff_get_audio_frame_size(AVCodecContext *enc, int size, int mux)
716 717 718
{
    int frame_size;

719 720 721
    /* give frame_size priority if demuxing */
    if (!mux && enc->frame_size > 1)
        return enc->frame_size;
722

723 724 725
    if ((frame_size = av_get_audio_frame_duration(enc, size)) > 0)
        return frame_size;

Diego Biurrun's avatar
Diego Biurrun committed
726
    /* Fall back on using frame_size if muxing. */
727 728 729
    if (enc->frame_size > 1)
        return enc->frame_size;

730 731 732 733 734 735 736
    //For WMA we currently have no other means to calculate duration thus we
    //do it here by assuming CBR, which is true for all known cases.
    if(!mux && enc->bit_rate>0 && size>0 && enc->sample_rate>0 && enc->block_align>1) {
        if (enc->codec_id == AV_CODEC_ID_WMAV1 || enc->codec_id == AV_CODEC_ID_WMAV2)
            return  ((int64_t)size * 8 * enc->sample_rate) / enc->bit_rate;
    }

737
    return -1;
738 739 740
}


741
/**
742
 * Return the frame duration in seconds. Return 0 if not available.
743
 */
744 745
void ff_compute_frame_duration(int *pnum, int *pden, AVStream *st,
                               AVCodecParserContext *pc, AVPacket *pkt)
746 747 748 749 750
{
    int frame_size;

    *pnum = 0;
    *pden = 0;
751
    switch(st->codec->codec_type) {
752
    case AVMEDIA_TYPE_VIDEO:
753
        if (st->r_frame_rate.num && !pc) {
754 755 756
            *pnum = st->r_frame_rate.den;
            *pden = st->r_frame_rate.num;
        } else if(st->time_base.num*1000LL > st->time_base.den) {
757 758
            *pnum = st->time_base.num;
            *pden = st->time_base.den;
759 760 761
        }else if(st->codec->time_base.num*1000LL > st->codec->time_base.den){
            *pnum = st->codec->time_base.num;
            *pden = st->codec->time_base.den;
762
            if (pc && pc->repeat_pict) {
763 764 765 766
                if (*pnum > INT_MAX / (1 + pc->repeat_pict))
                    *pden /= 1 + pc->repeat_pict;
                else
                    *pnum *= 1 + pc->repeat_pict;
767
            }
768 769 770 771 772
            //If this codec can be interlaced or progressive then we need a parser to compute duration of a packet
            //Thus if we have no parser in such case leave duration undefined.
            if(st->codec->ticks_per_frame>1 && !pc){
                *pnum = *pden = 0;
            }
773 774
        }
        break;
775
    case AVMEDIA_TYPE_AUDIO:
776
        frame_size = ff_get_audio_frame_size(st->codec, pkt->size, 0);
777
        if (frame_size <= 0 || st->codec->sample_rate <= 0)
778 779
            break;
        *pnum = frame_size;
780
        *pden = st->codec->sample_rate;
781 782 783 784 785 786
        break;
    default:
        break;
    }
}

787
static int is_intra_only(AVCodecContext *enc){
788
    const AVCodecDescriptor *desc;
789 790

    if(enc->codec_type != AVMEDIA_TYPE_VIDEO)
791
        return 1;
792 793 794 795 796

    desc = av_codec_get_codec_descriptor(enc);
    if (!desc) {
        desc = avcodec_descriptor_get(enc->codec_id);
        av_codec_set_codec_descriptor(enc, desc);
797
    }
798 799
    if (desc)
        return !!(desc->props & AV_CODEC_PROP_INTRA_ONLY);
800 801 802
    return 0;
}

803 804
static int has_decode_delay_been_guessed(AVStream *st)
{
805
    if(st->codec->codec_id != AV_CODEC_ID_H264) return 1;
806
    if(!st->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy
807
        return 1;
808 809 810 811 812 813
#if CONFIG_H264_DECODER
    if(st->codec->has_b_frames &&
       avpriv_h264_has_num_reorder_frames(st->codec) == st->codec->has_b_frames)
        return 1;
#endif
    if(st->codec->has_b_frames<3)
814
        return st->nb_decoded_frames >= 7;
815
    else if(st->codec->has_b_frames<4)
816
        return st->nb_decoded_frames >= 18;
817
    else
818
        return st->nb_decoded_frames >= 20;
819 820
}

821 822 823 824 825 826 827 828 829
static AVPacketList *get_next_pkt(AVFormatContext *s, AVStream *st, AVPacketList *pktl)
{
    if (pktl->next)
        return pktl->next;
    if (pktl == s->parse_queue_end)
        return s->packet_buffer;
    return NULL;
}

830 831
static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index)
{
832
    if (s->correct_ts_overflow && st->pts_wrap_bits < 63 &&
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 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
        st->pts_wrap_reference == AV_NOPTS_VALUE && st->first_dts != AV_NOPTS_VALUE) {
        int i;

        // reference time stamp should be 60 s before first time stamp
        int64_t pts_wrap_reference = st->first_dts - av_rescale(60, st->time_base.den, st->time_base.num);
        // if first time stamp is not more than 1/8 and 60s before the wrap point, subtract rather than add wrap offset
        int pts_wrap_behavior = (st->first_dts < (1LL<<st->pts_wrap_bits) - (1LL<<st->pts_wrap_bits-3)) ||
            (st->first_dts < (1LL<<st->pts_wrap_bits) - av_rescale(60, st->time_base.den, st->time_base.num)) ?
            AV_PTS_WRAP_ADD_OFFSET : AV_PTS_WRAP_SUB_OFFSET;

        AVProgram *first_program = av_find_program_from_stream(s, NULL, stream_index);

        if (!first_program) {
            int default_stream_index = av_find_default_stream_index(s);
            if (s->streams[default_stream_index]->pts_wrap_reference == AV_NOPTS_VALUE) {
                for (i=0; i<s->nb_streams; i++) {
                    s->streams[i]->pts_wrap_reference = pts_wrap_reference;
                    s->streams[i]->pts_wrap_behavior = pts_wrap_behavior;
                }
            }
            else {
                st->pts_wrap_reference = s->streams[default_stream_index]->pts_wrap_reference;
                st->pts_wrap_behavior = s->streams[default_stream_index]->pts_wrap_behavior;
            }
        }
        else {
            AVProgram *program = first_program;
            while (program) {
                if (program->pts_wrap_reference != AV_NOPTS_VALUE) {
                    pts_wrap_reference = program->pts_wrap_reference;
                    pts_wrap_behavior = program->pts_wrap_behavior;
                    break;
                }
                program = av_find_program_from_stream(s, program, stream_index);
            }

            // update every program with differing pts_wrap_reference
            program = first_program;
            while(program) {
                if (program->pts_wrap_reference != pts_wrap_reference) {
                    for (i=0; i<program->nb_stream_indexes; i++) {
                        s->streams[program->stream_index[i]]->pts_wrap_reference = pts_wrap_reference;
                        s->streams[program->stream_index[i]]->pts_wrap_behavior = pts_wrap_behavior;
                    }

                    program->pts_wrap_reference = pts_wrap_reference;
                    program->pts_wrap_behavior = pts_wrap_behavior;
                }
                program = av_find_program_from_stream(s, program, stream_index);
            }
        }
        return 1;
    }
    return 0;
}

889
static void update_initial_timestamps(AVFormatContext *s, int stream_index,
890
                                      int64_t dts, int64_t pts, AVPacket *pkt)
891
{
892
    AVStream *st= s->streams[stream_index];
893
    AVPacketList *pktl= s->parse_queue ? s->parse_queue : s->packet_buffer;
894
    int64_t pts_buffer[MAX_REORDER_DELAY+1];
895
    int64_t shift;
896
    int i, delay;
897

898
    if(st->first_dts != AV_NOPTS_VALUE || dts == AV_NOPTS_VALUE || st->cur_dts == AV_NOPTS_VALUE || is_relative(dts))
899 900
        return;

901
    delay = st->codec->has_b_frames;
902
    st->first_dts= dts - (st->cur_dts - RELATIVE_TS_BASE);
903
    st->cur_dts= dts;
904
    shift = st->first_dts - RELATIVE_TS_BASE;
905

906
    for (i=0; i<MAX_REORDER_DELAY+1; i++)
907 908
        pts_buffer[i] = AV_NOPTS_VALUE;

909
    if (is_relative(pts))
910
        pts += shift;
911

912
    for(; pktl; pktl= get_next_pkt(s, st, pktl)){
913 914
        if(pktl->pkt.stream_index != stream_index)
            continue;
915
        if(is_relative(pktl->pkt.pts))
916
            pktl->pkt.pts += shift;
917

918
        if(is_relative(pktl->pkt.dts))
919
            pktl->pkt.dts += shift;
920 921 922

        if(st->start_time == AV_NOPTS_VALUE && pktl->pkt.pts != AV_NOPTS_VALUE)
            st->start_time= pktl->pkt.pts;
923 924 925 926 927 928 929 930

        if(pktl->pkt.pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY && has_decode_delay_been_guessed(st)){
            pts_buffer[0]= pktl->pkt.pts;
            for(i=0; i<delay && pts_buffer[i] > pts_buffer[i+1]; i++)
                FFSWAP(int64_t, pts_buffer[i], pts_buffer[i+1]);
            if(pktl->pkt.dts == AV_NOPTS_VALUE)
                pktl->pkt.dts= pts_buffer[0];
        }
931
    }
932 933 934 935 936 937 938 939 940 941

    if (update_wrap_reference(s, st, stream_index) && st->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET) {
        // correct first time stamps to negative values
        st->first_dts = wrap_timestamp(st, st->first_dts);
        st->cur_dts = wrap_timestamp(st, st->cur_dts);
        pkt->dts = wrap_timestamp(st, pkt->dts);
        pkt->pts = wrap_timestamp(st, pkt->pts);
        pts = wrap_timestamp(st, pts);
    }

942 943
    if (st->start_time == AV_NOPTS_VALUE)
        st->start_time = pts;
944 945
}

946 947
static void update_initial_durations(AVFormatContext *s, AVStream *st,
                                     int stream_index, int duration)
948
{
949
    AVPacketList *pktl= s->parse_queue ? s->parse_queue : s->packet_buffer;
950
    int64_t cur_dts= RELATIVE_TS_BASE;
951 952 953

    if(st->first_dts != AV_NOPTS_VALUE){
        cur_dts= st->first_dts;
954
        for(; pktl; pktl= get_next_pkt(s, st, pktl)){
955
            if(pktl->pkt.stream_index == stream_index){
956 957
                if(pktl->pkt.pts != pktl->pkt.dts || pktl->pkt.dts != AV_NOPTS_VALUE || pktl->pkt.duration)
                    break;
958
                cur_dts -= duration;
959 960
            }
        }
961
        if(pktl && pktl->pkt.dts != st->first_dts) {
962
            av_log(s, AV_LOG_DEBUG, "first_dts %s not matching first dts %s in the queue\n", av_ts2str(st->first_dts), av_ts2str(pktl->pkt.dts));
963 964 965
            return;
        }
        if(!pktl) {
966
            av_log(s, AV_LOG_DEBUG, "first_dts %s but no packet with dts in the queue\n", av_ts2str(st->first_dts));
967 968
            return;
        }
969
        pktl= s->parse_queue ? s->parse_queue : s->packet_buffer;
970
        st->first_dts = cur_dts;
971
    }else if(st->cur_dts != RELATIVE_TS_BASE)
972
        return;
973

974
    for(; pktl; pktl= get_next_pkt(s, st, pktl)){
975
        if(pktl->pkt.stream_index != stream_index)
976
            continue;
977
        if(pktl->pkt.pts == pktl->pkt.dts && (pktl->pkt.dts == AV_NOPTS_VALUE || pktl->pkt.dts == st->first_dts)
978
           && !pktl->pkt.duration){
979
            pktl->pkt.dts= cur_dts;
980
            if(!st->codec->has_b_frames)
981
                pktl->pkt.pts= cur_dts;
982
//            if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO)
983
                pktl->pkt.duration = duration;
984 985
        }else
            break;
986
        cur_dts = pktl->pkt.dts + pktl->pkt.duration;
987
    }
988
    if(!pktl)
989
        st->cur_dts= cur_dts;
990 991
}

992
static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
993 994
                               AVCodecParserContext *pc, AVPacket *pkt)
{
995
    int num, den, presentation_delayed, delay, i;
996
    int64_t offset;
997

998 999 1000
    if (s->flags & AVFMT_FLAG_NOFILLIN)
        return;

1001 1002 1003
    if((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
        pkt->dts= AV_NOPTS_VALUE;

1004
    if (st->codec->codec_id != AV_CODEC_ID_H264 && pc && pc->pict_type == AV_PICTURE_TYPE_B)
1005
        //FIXME Set low_delay = 0 when has_b_frames = 1
1006 1007
        st->codec->has_b_frames = 1;

1008 1009 1010
    /* do we have a video B-frame ? */
    delay= st->codec->has_b_frames;
    presentation_delayed = 0;
1011

1012 1013 1014
    /* XXX: need has_b_frame, but cannot get it if the codec is
        not initialized */
    if (delay &&
1015
        pc && pc->pict_type != AV_PICTURE_TYPE_B)
1016 1017
        presentation_delayed = 1;

1018 1019 1020
    if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
        st->pts_wrap_bits < 63 &&
        pkt->dts - (1LL << (st->pts_wrap_bits - 1)) > pkt->pts) {
1021 1022 1023 1024
        if(is_relative(st->cur_dts) || pkt->dts - (1LL<<(st->pts_wrap_bits-1)) > st->cur_dts) {
            pkt->dts -= 1LL<<st->pts_wrap_bits;
        } else
            pkt->pts += 1LL<<st->pts_wrap_bits;
1025 1026
    }

1027 1028 1029 1030
    // some mpeg2 in mpeg-ps lack dts (issue171 / input_file.mpg)
    // we take the conservative approach and discard both
    // Note, if this is misbehaving for a H.264 file then possibly presentation_delayed is not set correctly.
    if(delay==1 && pkt->dts == pkt->pts && pkt->dts != AV_NOPTS_VALUE && presentation_delayed){
1031
        av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination %"PRIi64"\n", pkt->dts);
1032 1033
        if(strcmp(s->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2")) // otherwise we discard correct timestamps for vc1-wmapro.ism
            pkt->dts= AV_NOPTS_VALUE;
1034 1035
    }

1036
    if (pkt->duration == 0) {
1037
        ff_compute_frame_duration(&num, &den, st, pc, pkt);
1038
        if (den && num) {
1039
            pkt->duration = av_rescale_rnd(1, num * (int64_t)st->time_base.den, den * (int64_t)st->time_base.num, AV_ROUND_DOWN);
1040 1041
        }
    }
1042 1043
    if(pkt->duration != 0 && (s->packet_buffer || s->parse_queue))
        update_initial_durations(s, st, pkt->stream_index, pkt->duration);
1044

1045 1046
    /* correct timestamps with byte offset if demuxers only have timestamps
       on packet boundaries */
1047 1048 1049 1050 1051 1052 1053 1054 1055
    if(pc && st->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size){
        /* this will estimate bitrate based on this frame's duration and size */
        offset = av_rescale(pc->offset, pkt->duration, pkt->size);
        if(pkt->pts != AV_NOPTS_VALUE)
            pkt->pts += offset;
        if(pkt->dts != AV_NOPTS_VALUE)
            pkt->dts += offset;
    }

1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
    if (pc && pc->dts_sync_point >= 0) {
        // we have synchronization info from the parser
        int64_t den = st->codec->time_base.den * (int64_t) st->time_base.num;
        if (den > 0) {
            int64_t num = st->codec->time_base.num * (int64_t) st->time_base.den;
            if (pkt->dts != AV_NOPTS_VALUE) {
                // got DTS from the stream, update reference timestamp
                st->reference_dts = pkt->dts - pc->dts_ref_dts_delta * num / den;
                pkt->pts = pkt->dts + pc->pts_dts_delta * num / den;
            } else if (st->reference_dts != AV_NOPTS_VALUE) {
                // compute DTS based on reference timestamp
                pkt->dts = st->reference_dts + pc->dts_ref_dts_delta * num / den;
                pkt->pts = pkt->dts + pc->pts_dts_delta * num / den;