flvdec.c 47.8 KB
Newer Older
1
/*
2
 * FLV demuxer
3
 * Copyright (c) 2003 The FFmpeg Project
4
 *
5 6
 * This demuxer will generate a 1 byte extradata for VP6F content.
 * It is composed of:
7 8
 *  - upper 4 bits: difference between encoded width and visible width
 *  - lower 4 bits: difference between encoded height and visible height
9
 *
10 11 12
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
13 14
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
15
 * version 2.1 of the License, or (at your option) any later version.
16
 *
17
 * FFmpeg is distributed in the hope that it will be useful,
18 19 20 21 22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
23
 * License along with FFmpeg; if not, write to the Free Software
24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25
 */
26

27
#include "libavutil/avstring.h"
28
#include "libavutil/channel_layout.h"
29
#include "libavutil/dict.h"
30
#include "libavutil/opt.h"
31
#include "libavutil/intfloat.h"
32
#include "libavutil/mathematics.h"
33
#include "libavutil/time_internal.h"
34
#include "libavcodec/bytestream.h"
35
#include "avformat.h"
36
#include "internal.h"
37
#include "avio_internal.h"
38
#include "flv.h"
39

40 41
#define VALIDATE_INDEX_TS_THRESH 2500

42 43
#define RESYNC_BUFFER_SIZE (1<<20)

44
typedef struct FLVContext {
45
    const AVClass *class; ///< Class for private options.
46
    int trust_metadata;   ///< configure streams according onMetaData
47
    int trust_datasize;   ///< trust data size of FLVTag
48
    int dump_full_metadata;   ///< Dump full metadata of the onMetadata
49
    int wrong_dts;        ///< wrong dts due to negative cts
50
    uint8_t *new_extradata[FLV_STREAM_TYPE_NB];
51
    int new_extradata_size[FLV_STREAM_TYPE_NB];
52 53
    int last_sample_rate;
    int last_channels;
54 55 56 57 58 59
    struct {
        int64_t dts;
        int64_t pos;
    } validate_index[2];
    int validate_next;
    int validate_count;
60
    int searched_for_end;
61 62

    uint8_t resync_buffer[2*RESYNC_BUFFER_SIZE];
63 64

    int broken_sizes;
65
    int sum_flv_tag_size;
66 67 68

    int last_keyframe_stream_index;
    int keyframe_count;
69 70
    int64_t video_bit_rate;
    int64_t audio_bit_rate;
71 72
    int64_t *keyframe_times;
    int64_t *keyframe_filepositions;
73
    int missing_streams;
74
    AVRational framerate;
75 76 77
    int64_t last_ts;
    int64_t time_offset;
    int64_t time_pos;
78 79
} FLVContext;

80 81 82 83 84 85
/* AMF date type */
typedef struct amf_date {
    double   milliseconds;
    int16_t  timezone;
} amf_date;

86
static int probe(const AVProbeData *p, int live)
87
{
88 89
    const uint8_t *d = p->buf;
    unsigned offset = AV_RB32(d + 5);
90

91 92 93 94
    if (d[0] == 'F' &&
        d[1] == 'L' &&
        d[2] == 'V' &&
        d[3] < 5 && d[5] == 0 &&
95 96 97 98 99 100
        offset + 100 < p->buf_size &&
        offset > 8) {
        int is_live = !memcmp(d + offset + 40, "NGINX RTMP", 10);

        if (live == is_live)
            return AVPROBE_SCORE_MAX;
101 102 103 104
    }
    return 0;
}

105
static int flv_probe(const AVProbeData *p)
106 107 108 109
{
    return probe(p, 0);
}

110
static int live_flv_probe(const AVProbeData *p)
111 112 113 114
{
    return probe(p, 1);
}

115 116 117 118 119 120 121 122 123 124 125 126 127 128
static int kux_probe(const AVProbeData *p)
{
    const uint8_t *d = p->buf;

    if (d[0] == 'K' &&
        d[1] == 'D' &&
        d[2] == 'K' &&
        d[3] == 0 &&
        d[4] == 0) {
        return AVPROBE_SCORE_EXTENSION + 1;
    }
    return 0;
}

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
static void add_keyframes_index(AVFormatContext *s)
{
    FLVContext *flv   = s->priv_data;
    AVStream *stream  = NULL;
    unsigned int i    = 0;

    if (flv->last_keyframe_stream_index < 0) {
        av_log(s, AV_LOG_DEBUG, "keyframe stream hasn't been created\n");
        return;
    }

    av_assert0(flv->last_keyframe_stream_index <= s->nb_streams);
    stream = s->streams[flv->last_keyframe_stream_index];

    if (stream->nb_index_entries == 0) {
        for (i = 0; i < flv->keyframe_count; i++) {
145 146
            av_log(s, AV_LOG_TRACE, "keyframe filepositions = %"PRId64" times = %"PRId64"\n",
                   flv->keyframe_filepositions[i], flv->keyframe_times[i] * 1000);
147 148 149 150 151 152 153 154 155 156 157 158 159
            av_add_index_entry(stream, flv->keyframe_filepositions[i],
                flv->keyframe_times[i] * 1000, 0, 0, AVINDEX_KEYFRAME);
        }
    } else
        av_log(s, AV_LOG_WARNING, "Skipping duplicate index\n");

    if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
        av_freep(&flv->keyframe_times);
        av_freep(&flv->keyframe_filepositions);
        flv->keyframe_count = 0;
    }
}

160
static AVStream *create_stream(AVFormatContext *s, int codec_type)
161
{
162
    FLVContext *flv   = s->priv_data;
163 164 165
    AVStream *st = avformat_new_stream(s, NULL);
    if (!st)
        return NULL;
166
    st->codecpar->codec_type = codec_type;
167
    if (s->nb_streams>=3 ||(   s->nb_streams==2
168
                           && s->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE
169 170 171
                           && s->streams[1]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE
                           && s->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_DATA
                           && s->streams[1]->codecpar->codec_type != AVMEDIA_TYPE_DATA))
172
        s->ctx_flags &= ~AVFMTCTX_NOHEADER;
173
    if (codec_type == AVMEDIA_TYPE_AUDIO) {
174
        st->codecpar->bit_rate = flv->audio_bit_rate;
175
        flv->missing_streams &= ~FLV_HEADER_FLAG_HASAUDIO;
176
    }
177
    if (codec_type == AVMEDIA_TYPE_VIDEO) {
178
        st->codecpar->bit_rate = flv->video_bit_rate;
179
        flv->missing_streams &= ~FLV_HEADER_FLAG_HASVIDEO;
180 181
        st->avg_frame_rate = flv->framerate;
    }
182

183

184
    avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
185 186
    flv->last_keyframe_stream_index = s->nb_streams - 1;
    add_keyframes_index(s);
187 188
    return st;
}
189

190
static int flv_same_audio_codec(AVCodecParameters *apar, int flags)
191 192
{
    int bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
193
    int flv_codecid           = flags & FLV_AUDIO_CODECID_MASK;
194 195
    int codec_id;

196
    if (!apar->codec_id && !apar->codec_tag)
197 198
        return 1;

199
    if (apar->bits_per_coded_sample != bits_per_coded_sample)
200 201
        return 0;

202 203
    switch (flv_codecid) {
    // no distinction between S16 and S8 PCM codec flags
204
    case FLV_CODECID_PCM:
205 206
        codec_id = bits_per_coded_sample == 8
                   ? AV_CODEC_ID_PCM_U8
207
#if HAVE_BIGENDIAN
208
                   : AV_CODEC_ID_PCM_S16BE;
209
#else
210
                   : AV_CODEC_ID_PCM_S16LE;
211
#endif
212
        return codec_id == apar->codec_id;
213
    case FLV_CODECID_PCM_LE:
214 215 216
        codec_id = bits_per_coded_sample == 8
                   ? AV_CODEC_ID_PCM_U8
                   : AV_CODEC_ID_PCM_S16LE;
217
        return codec_id == apar->codec_id;
218
    case FLV_CODECID_AAC:
219
        return apar->codec_id == AV_CODEC_ID_AAC;
220
    case FLV_CODECID_ADPCM:
221
        return apar->codec_id == AV_CODEC_ID_ADPCM_SWF;
222
    case FLV_CODECID_SPEEX:
223
        return apar->codec_id == AV_CODEC_ID_SPEEX;
224
    case FLV_CODECID_MP3:
225
        return apar->codec_id == AV_CODEC_ID_MP3;
226 227 228
    case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
    case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
    case FLV_CODECID_NELLYMOSER:
229
        return apar->codec_id == AV_CODEC_ID_NELLYMOSER;
230
    case FLV_CODECID_PCM_MULAW:
231 232
        return apar->sample_rate == 8000 &&
               apar->codec_id    == AV_CODEC_ID_PCM_MULAW;
233
    case FLV_CODECID_PCM_ALAW:
234 235
        return apar->sample_rate == 8000 &&
               apar->codec_id    == AV_CODEC_ID_PCM_ALAW;
236
    default:
237
        return apar->codec_tag == (flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
238 239
    }
}
240

241
static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream,
242
                                AVCodecParameters *apar, int flv_codecid)
243 244 245 246
{
    switch (flv_codecid) {
    // no distinction between S16 and S8 PCM codec flags
    case FLV_CODECID_PCM:
247
        apar->codec_id = apar->bits_per_coded_sample == 8
248
                           ? AV_CODEC_ID_PCM_U8
249
#if HAVE_BIGENDIAN
250
                           : AV_CODEC_ID_PCM_S16BE;
Michael Niedermayer's avatar
Michael Niedermayer committed
251
#else
252
                           : AV_CODEC_ID_PCM_S16LE;
Michael Niedermayer's avatar
Michael Niedermayer committed
253
#endif
254 255
        break;
    case FLV_CODECID_PCM_LE:
256
        apar->codec_id = apar->bits_per_coded_sample == 8
257 258 259 260
                           ? AV_CODEC_ID_PCM_U8
                           : AV_CODEC_ID_PCM_S16LE;
        break;
    case FLV_CODECID_AAC:
261
        apar->codec_id = AV_CODEC_ID_AAC;
262 263
        break;
    case FLV_CODECID_ADPCM:
264
        apar->codec_id = AV_CODEC_ID_ADPCM_SWF;
265 266
        break;
    case FLV_CODECID_SPEEX:
267 268
        apar->codec_id    = AV_CODEC_ID_SPEEX;
        apar->sample_rate = 16000;
269 270
        break;
    case FLV_CODECID_MP3:
271
        apar->codec_id      = AV_CODEC_ID_MP3;
272 273 274 275
        astream->need_parsing = AVSTREAM_PARSE_FULL;
        break;
    case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
        // in case metadata does not otherwise declare samplerate
276 277
        apar->sample_rate = 8000;
        apar->codec_id    = AV_CODEC_ID_NELLYMOSER;
278 279
        break;
    case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
280 281
        apar->sample_rate = 16000;
        apar->codec_id    = AV_CODEC_ID_NELLYMOSER;
282 283
        break;
    case FLV_CODECID_NELLYMOSER:
284
        apar->codec_id = AV_CODEC_ID_NELLYMOSER;
285 286
        break;
    case FLV_CODECID_PCM_MULAW:
287 288
        apar->sample_rate = 8000;
        apar->codec_id    = AV_CODEC_ID_PCM_MULAW;
289 290
        break;
    case FLV_CODECID_PCM_ALAW:
291 292
        apar->sample_rate = 8000;
        apar->codec_id    = AV_CODEC_ID_PCM_ALAW;
293 294
        break;
    default:
295
        avpriv_request_sample(s, "Audio codec (%x)",
296
               flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
297
        apar->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
298 299 300
    }
}

301
static int flv_same_video_codec(AVCodecParameters *vpar, int flags)
302 303 304
{
    int flv_codecid = flags & FLV_VIDEO_CODECID_MASK;

305
    if (!vpar->codec_id && !vpar->codec_tag)
306 307 308
        return 1;

    switch (flv_codecid) {
309
    case FLV_CODECID_H263:
310
        return vpar->codec_id == AV_CODEC_ID_FLV1;
311
    case FLV_CODECID_SCREEN:
312
        return vpar->codec_id == AV_CODEC_ID_FLASHSV;
313
    case FLV_CODECID_SCREEN2:
314
        return vpar->codec_id == AV_CODEC_ID_FLASHSV2;
315
    case FLV_CODECID_VP6:
316
        return vpar->codec_id == AV_CODEC_ID_VP6F;
317
    case FLV_CODECID_VP6A:
318
        return vpar->codec_id == AV_CODEC_ID_VP6A;
319
    case FLV_CODECID_H264:
320
        return vpar->codec_id == AV_CODEC_ID_H264;
321
    default:
322
        return vpar->codec_tag == flv_codecid;
323 324 325
    }
}

326 327 328
static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream,
                               int flv_codecid, int read)
{
329
    int ret = 0;
330
    AVCodecParameters *par = vstream->codecpar;
331
    enum AVCodecID old_codec_id = vstream->codecpar->codec_id;
332 333
    switch (flv_codecid) {
    case FLV_CODECID_H263:
334
        par->codec_id = AV_CODEC_ID_FLV1;
335
        break;
336
    case FLV_CODECID_REALH263:
337
        par->codec_id = AV_CODEC_ID_H263;
338
        break; // Really mean it this time
339
    case FLV_CODECID_SCREEN:
340
        par->codec_id = AV_CODEC_ID_FLASHSV;
341 342
        break;
    case FLV_CODECID_SCREEN2:
343
        par->codec_id = AV_CODEC_ID_FLASHSV2;
344 345
        break;
    case FLV_CODECID_VP6:
346
        par->codec_id = AV_CODEC_ID_VP6F;
347 348
    case FLV_CODECID_VP6A:
        if (flv_codecid == FLV_CODECID_VP6A)
349
            par->codec_id = AV_CODEC_ID_VP6A;
350
        if (read) {
351
            if (par->extradata_size != 1) {
352
                ff_alloc_extradata(par, 1);
353
            }
354 355
            if (par->extradata)
                par->extradata[0] = avio_r8(s->pb);
356 357 358
            else
                avio_skip(s->pb, 1);
        }
359 360
        ret = 1;     // 1 byte body size adjustment for flv_read_packet()
        break;
361
    case FLV_CODECID_H264:
362
        par->codec_id = AV_CODEC_ID_H264;
363
        vstream->need_parsing = AVSTREAM_PARSE_HEADERS;
364 365
        ret = 3;     // not 4, reading packet type will consume one byte
        break;
366
    case FLV_CODECID_MPEG4:
367
        par->codec_id = AV_CODEC_ID_MPEG4;
368 369
        ret = 3;
        break;
370
    default:
371
        avpriv_request_sample(s, "Video codec (%x)", flv_codecid);
372
        par->codec_tag = flv_codecid;
373 374
    }

375 376 377 378 379 380
    if (!vstream->internal->need_context_update && par->codec_id != old_codec_id) {
        avpriv_request_sample(s, "Changing the codec id midstream");
        return AVERROR_PATCHWELCOME;
    }

    return ret;
381 382
}

383 384
static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize)
{
385
    int length = avio_rb16(ioc);
386
    if (length >= buffsize) {
387
        avio_skip(ioc, length);
388 389
        return -1;
    }
390

391
    avio_read(ioc, buffer, length);
392

393
    buffer[length] = '\0';
394

395
    return length;
396 397
}

398
static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, int64_t max_pos)
399 400
{
    FLVContext *flv       = s->priv_data;
401
    unsigned int timeslen = 0, fileposlen = 0, i;
402
    char str_val[256];
403
    int64_t *times         = NULL;
404
    int64_t *filepositions = NULL;
405 406
    int ret                = AVERROR(ENOSYS);
    int64_t initial_pos    = avio_tell(ioc);
407

408
    if (flv->keyframe_count > 0) {
409
        av_log(s, AV_LOG_DEBUG, "keyframes have been parsed\n");
410 411
        return 0;
    }
412 413
    av_assert0(!flv->keyframe_times);
    av_assert0(!flv->keyframe_filepositions);
414

415 416 417
    if (s->flags & AVFMT_FLAG_IGNIDX)
        return 0;

418 419
    while (avio_tell(ioc) < max_pos - 2 &&
           amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
420
        int64_t **current_array;
421
        unsigned int arraylen;
422 423 424 425 426 427

        // Expect array object in context
        if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
            break;

        arraylen = avio_rb32(ioc);
428
        if (arraylen>>28)
429 430
            break;

431 432
        if       (!strcmp(KEYFRAMES_TIMESTAMP_TAG , str_val) && !times) {
            current_array = &times;
433 434 435
            timeslen      = arraylen;
        } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) &&
                   !filepositions) {
436
            current_array = &filepositions;
437 438 439 440
            fileposlen    = arraylen;
        } else
            // unexpected metatag inside keyframes, will not use such
            // metadata for indexing
441 442 443 444 445 446 447
            break;

        if (!(*current_array = av_mallocz(sizeof(**current_array) * arraylen))) {
            ret = AVERROR(ENOMEM);
            goto finish;
        }

448 449
        for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
            if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
450
                goto invalid;
451
            current_array[0][i] = av_int2double(avio_rb64(ioc));
452
        }
453 454 455 456 457 458
        if (times && filepositions) {
            // All done, exiting at a position allowing amf_parse_object
            // to finish parsing the object
            ret = 0;
            break;
        }
459 460
    }

461
    if (timeslen == fileposlen && fileposlen>1 && max_pos <= filepositions[0]) {
462 463 464 465
        for (i = 0; i < FFMIN(2,fileposlen); i++) {
            flv->validate_index[i].pos = filepositions[i];
            flv->validate_index[i].dts = times[i] * 1000;
            flv->validate_count        = i + 1;
466
        }
467 468 469 470 471
        flv->keyframe_times = times;
        flv->keyframe_filepositions = filepositions;
        flv->keyframe_count = timeslen;
        times = NULL;
        filepositions = NULL;
472 473
    } else {
invalid:
474
        av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
475
    }
476 477 478 479

finish:
    av_freep(&times);
    av_freep(&filepositions);
480
    avio_seek(ioc, initial_pos, SEEK_SET);
481 482 483
    return ret;
}

484 485 486 487
static int amf_parse_object(AVFormatContext *s, AVStream *astream,
                            AVStream *vstream, const char *key,
                            int64_t max_pos, int depth)
{
488
    AVCodecParameters *apar, *vpar;
489
    FLVContext *flv = s->priv_data;
490
    AVIOContext *ioc;
491
    AMFDataType amf_type;
492
    char str_val[1024];
493
    double num_val;
494
    amf_date date;
495

496 497
    num_val  = 0;
    ioc      = s->pb;
498
    amf_type = avio_r8(ioc);
499

500 501 502 503 504 505 506 507
    switch (amf_type) {
    case AMF_DATA_TYPE_NUMBER:
        num_val = av_int2double(avio_rb64(ioc));
        break;
    case AMF_DATA_TYPE_BOOL:
        num_val = avio_r8(ioc);
        break;
    case AMF_DATA_TYPE_STRING:
508 509
        if (amf_get_string(ioc, str_val, sizeof(str_val)) < 0) {
            av_log(s, AV_LOG_ERROR, "AMF_DATA_TYPE_STRING parsing failed\n");
510
            return -1;
511
        }
512 513
        break;
    case AMF_DATA_TYPE_OBJECT:
514
        if (key &&
James Almer's avatar
James Almer committed
515
            (ioc->seekable & AVIO_SEEKABLE_NORMAL) &&
516
            !strcmp(KEYFRAMES_TAG, key) && depth == 1)
517
            if (parse_keyframes_index(s, ioc,
518
                                      max_pos) < 0)
519
                av_log(s, AV_LOG_ERROR, "Keyframe index parsing failed\n");
520 521
            else
                add_keyframes_index(s);
522 523 524 525 526
        while (avio_tell(ioc) < max_pos - 2 &&
               amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
            if (amf_parse_object(s, astream, vstream, str_val, max_pos,
                                 depth + 1) < 0)
                return -1;     // if we couldn't skip, bomb out.
527 528
        if (avio_r8(ioc) != AMF_END_OF_OBJECT) {
            av_log(s, AV_LOG_ERROR, "Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_OBJECT\n");
529
            return -1;
530
        }
531 532 533 534 535 536
        break;
    case AMF_DATA_TYPE_NULL:
    case AMF_DATA_TYPE_UNDEFINED:
    case AMF_DATA_TYPE_UNSUPPORTED:
        break;     // these take up no additional space
    case AMF_DATA_TYPE_MIXEDARRAY:
537 538
    {
        unsigned v;
539 540 541 542 543 544 545
        avio_skip(ioc, 4);     // skip 32-bit max array index
        while (avio_tell(ioc) < max_pos - 2 &&
               amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
            // this is the only case in which we would want a nested
            // parse to not skip over the object
            if (amf_parse_object(s, astream, vstream, str_val, max_pos,
                                 depth + 1) < 0)
546
                return -1;
547 548 549
        v = avio_r8(ioc);
        if (v != AMF_END_OF_OBJECT) {
            av_log(s, AV_LOG_ERROR, "Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_MIXEDARRAY, found %d\n", v);
550
            return -1;
551
        }
552
        break;
553
    }
554 555 556 557 558 559 560 561 562 563 564 565
    case AMF_DATA_TYPE_ARRAY:
    {
        unsigned int arraylen, i;

        arraylen = avio_rb32(ioc);
        for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++)
            if (amf_parse_object(s, NULL, NULL, NULL, max_pos,
                                 depth + 1) < 0)
                return -1;      // if we couldn't skip, bomb out.
    }
    break;
    case AMF_DATA_TYPE_DATE:
566 567 568
        // timestamp (double) and UTC offset (int16)
        date.milliseconds = av_int2double(avio_rb64(ioc));
        date.timezone = avio_rb16(ioc);
569 570
        break;
    default:                    // unsupported type, we couldn't skip
571
        av_log(s, AV_LOG_ERROR, "unsupported amf type %d\n", amf_type);
572
        return -1;
573 574
    }

575
    if (key) {
576 577
        apar = astream ? astream->codecpar : NULL;
        vpar = vstream ? vstream->codecpar : NULL;
578

579 580 581 582 583 584
        // stream info doesn't live any deeper than the first object
        if (depth == 1) {
            if (amf_type == AMF_DATA_TYPE_NUMBER ||
                amf_type == AMF_DATA_TYPE_BOOL) {
                if (!strcmp(key, "duration"))
                    s->duration = num_val * AV_TIME_BASE;
585
                else if (!strcmp(key, "videodatarate") &&
586
                         0 <= (int)(num_val * 1024.0))
587 588
                    flv->video_bit_rate = num_val * 1024.0;
                else if (!strcmp(key, "audiodatarate") &&
589
                         0 <= (int)(num_val * 1024.0))
590
                    flv->audio_bit_rate = num_val * 1024.0;
591
                else if (!strcmp(key, "datastream")) {
592
                    AVStream *st = create_stream(s, AVMEDIA_TYPE_SUBTITLE);
593 594
                    if (!st)
                        return AVERROR(ENOMEM);
595
                    st->codecpar->codec_id = AV_CODEC_ID_TEXT;
596 597 598 599
                } else if (!strcmp(key, "framerate")) {
                    flv->framerate = av_d2q(num_val, 1000);
                    if (vstream)
                        vstream->avg_frame_rate = flv->framerate;
600
                } else if (flv->trust_metadata) {
601
                    if (!strcmp(key, "videocodecid") && vpar) {
602 603 604
                        int ret = flv_set_video_codec(s, vstream, num_val, 0);
                        if (ret < 0)
                            return ret;
605
                    } else if (!strcmp(key, "audiocodecid") && apar) {
606
                        int id = ((int)num_val) << FLV_AUDIO_CODECID_OFFSET;
607 608 609 610 611 612 613 614 615 616 617 618 619 620
                        flv_set_audio_codec(s, astream, apar, id);
                    } else if (!strcmp(key, "audiosamplerate") && apar) {
                        apar->sample_rate = num_val;
                    } else if (!strcmp(key, "audiosamplesize") && apar) {
                        apar->bits_per_coded_sample = num_val;
                    } else if (!strcmp(key, "stereo") && apar) {
                        apar->channels       = num_val + 1;
                        apar->channel_layout = apar->channels == 2 ?
                                               AV_CH_LAYOUT_STEREO :
                                               AV_CH_LAYOUT_MONO;
                    } else if (!strcmp(key, "width") && vpar) {
                        vpar->width = num_val;
                    } else if (!strcmp(key, "height") && vpar) {
                        vpar->height = num_val;
621
                    }
622
                }
623
            }
624 625 626 627 628 629 630
            if (amf_type == AMF_DATA_TYPE_STRING) {
                if (!strcmp(key, "encoder")) {
                    int version = -1;
                    if (1 == sscanf(str_val, "Open Broadcaster Software v0.%d", &version)) {
                        if (version > 0 && version <= 655)
                            flv->broken_sizes = 1;
                    }
631 632 633 634
                } else if (!strcmp(key, "metadatacreator")) {
                    if (   !strcmp (str_val, "MEGA")
                        || !strncmp(str_val, "FlixEngine", 10))
                        flv->broken_sizes = 1;
635 636
                }
            }
637 638
        }

639
        if (amf_type == AMF_DATA_TYPE_OBJECT && s->nb_streams == 1 &&
640 641
           ((!apar && !strcmp(key, "audiocodecid")) ||
            (!vpar && !strcmp(key, "videocodecid"))))
642 643
                s->ctx_flags &= ~AVFMTCTX_NOHEADER; //If there is either audio/video missing, codecid will be an empty object

644
        if ((!strcmp(key, "duration")        ||
645 646 647 648 649 650 651 652 653 654
            !strcmp(key, "filesize")        ||
            !strcmp(key, "width")           ||
            !strcmp(key, "height")          ||
            !strcmp(key, "videodatarate")   ||
            !strcmp(key, "framerate")       ||
            !strcmp(key, "videocodecid")    ||
            !strcmp(key, "audiodatarate")   ||
            !strcmp(key, "audiosamplerate") ||
            !strcmp(key, "audiosamplesize") ||
            !strcmp(key, "stereo")          ||
655
            !strcmp(key, "audiocodecid")    ||
656
            !strcmp(key, "datastream")) && !flv->dump_full_metadata)
657 658
            return 0;

659
        s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
660 661 662
        if (amf_type == AMF_DATA_TYPE_BOOL) {
            av_strlcpy(str_val, num_val > 0 ? "true" : "false",
                       sizeof(str_val));
663
            av_dict_set(&s->metadata, key, str_val, 0);
664
        } else if (amf_type == AMF_DATA_TYPE_NUMBER) {
665
            snprintf(str_val, sizeof(str_val), "%.f", num_val);
666
            av_dict_set(&s->metadata, key, str_val, 0);
667
        } else if (amf_type == AMF_DATA_TYPE_STRING) {
668
            av_dict_set(&s->metadata, key, str_val, 0);
669 670 671 672 673 674 675 676 677 678
        } else if (amf_type == AMF_DATA_TYPE_DATE) {
            time_t time;
            struct tm t;
            char datestr[128];
            time =  date.milliseconds / 1000; // to seconds
            localtime_r(&time, &t);
            strftime(datestr, sizeof(datestr), "%a, %d %b %Y %H:%M:%S %z", &t);

            av_dict_set(&s->metadata, key, datestr, 0);
        }
679 680 681 682 683
    }

    return 0;
}

684
#define TYPE_ONTEXTDATA 1
685
#define TYPE_ONCAPTION 2
686
#define TYPE_ONCAPTIONINFO 3
687
#define TYPE_UNKNOWN 9
688

689 690
static int flv_read_metabody(AVFormatContext *s, int64_t next_pos)
{
691
    FLVContext *flv = s->priv_data;
692
    AMFDataType type;
693 694
    AVStream *stream, *astream, *vstream;
    AVStream av_unused *dstream;
695
    AVIOContext *ioc;
696
    int i;
697
    char buffer[32];
698 699 700

    astream = NULL;
    vstream = NULL;
701
    dstream = NULL;
702
    ioc     = s->pb;
703

704
    // first object needs to be "onMetaData" string
705
    type = avio_r8(ioc);
706 707
    if (type != AMF_DATA_TYPE_STRING ||
        amf_get_string(ioc, buffer, sizeof(buffer)) < 0)
708
        return TYPE_UNKNOWN;
709 710

    if (!strcmp(buffer, "onTextData"))
711
        return TYPE_ONTEXTDATA;
712

713 714 715
    if (!strcmp(buffer, "onCaption"))
        return TYPE_ONCAPTION;

716 717 718
    if (!strcmp(buffer, "onCaptionInfo"))
        return TYPE_ONCAPTIONINFO;

719 720
    if (strcmp(buffer, "onMetaData") && strcmp(buffer, "onCuePoint")) {
        av_log(s, AV_LOG_DEBUG, "Unknown type %s\n", buffer);
721
        return TYPE_UNKNOWN;
722
    }
723

724 725 726
    // find the streams now so that amf_parse_object doesn't need to do
    // the lookup every time it is called.
    for (i = 0; i < s->nb_streams; i++) {
727
        stream = s->streams[i];
728
        if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
729
            vstream = stream;
730 731
            flv->last_keyframe_stream_index = i;
        } else if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
732
            astream = stream;
733 734 735
            if (flv->last_keyframe_stream_index == -1)
                flv->last_keyframe_stream_index = i;
        }
736
        else if (stream->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
737
            dstream = stream;
738 739
    }

740 741
    // parse the second object (we want a mixed array)
    if (amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
742 743 744 745 746
        return -1;

    return 0;
}

747
static int flv_read_header(AVFormatContext *s)
748
{
749
    int flags;
750
    FLVContext *flv = s->priv_data;
751
    int offset;
752
    int pre_tag_size = 0;
753

754 755 756 757
    /* Actual FLV data at 0xe40000 in KUX file */
    if(!strcmp(s->iformat->name, "kux"))
        avio_skip(s->pb, 0xe40000);

758
    avio_skip(s->pb, 4);
759 760 761
    flags = avio_r8(s->pb);

    flv->missing_streams = flags & (FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO);
762

763
    s->ctx_flags |= AVFMTCTX_NOHEADER;
Michael Niedermayer's avatar
Michael Niedermayer committed
764

765
    offset = avio_rb32(s->pb);
766
    avio_seek(s->pb, offset, SEEK_SET);
767 768 769 770 771 772 773 774 775 776

    /* Annex E. The FLV File Format
     * E.3 TheFLVFileBody
     *     Field               Type    Comment
     *     PreviousTagSize0    UI32    Always 0
     * */
    pre_tag_size = avio_rb32(s->pb);
    if (pre_tag_size) {
        av_log(s, AV_LOG_WARNING, "Read FLV header error, input file is not a standard flv format, first PreviousTagSize0 always is 0\n");
    }
777

778
    s->start_time = 0;
779
    flv->sum_flv_tag_size = 0;
780
    flv->last_keyframe_stream_index = -1;
781

782 783 784
    return 0;
}

785 786
static int flv_read_close(AVFormatContext *s)
{
787
    int i;
788
    FLVContext *flv = s->priv_data;
789
    for (i=0; i<FLV_STREAM_TYPE_NB; i++)
790
        av_freep(&flv->new_extradata[i]);
791 792
    av_freep(&flv->keyframe_times);
    av_freep(&flv->keyframe_filepositions);
793 794 795
    return 0;
}

796 797
static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
{
798 799 800
    if (!size)
        return 0;

801
    av_freep(&st->codecpar->extradata);
802
    if (ff_get_extradata(s, st->codecpar, s->pb, size) < 0)
803
        return AVERROR(ENOMEM);
804
    st->internal->need_context_update = 1;
805 806 807
    return 0;
}

808 809 810
static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
                               int size)
{
811 812 813
    if (!size)
        return 0;

814
    av_free(flv->new_extradata[stream]);
815
    flv->new_extradata[stream] = av_mallocz(size +
816
                                            AV_INPUT_BUFFER_PADDING_SIZE);
817 818 819 820 821 822 823
    if (!flv->new_extradata[stream])
        return AVERROR(ENOMEM);
    flv->new_extradata_size[stream] = size;
    avio_read(pb, flv->new_extradata[stream], size);
    return 0;
}

824 825 826
static void clear_index_entries(AVFormatContext *s, int64_t pos)
{
    int i, j, out;
827 828
    av_log(s, AV_LOG_WARNING,
           "Found invalid index entries, clearing the index.\n");
829 830 831 832
    for (i = 0; i < s->nb_streams; i++) {
        AVStream *st = s->streams[i];
        /* Remove all index entries that point to >= pos */
        out = 0;
833
        for (j = 0; j < st->nb_index_entries; j++)
834 835 836 837 838 839
            if (st->index_entries[j].pos < pos)
                st->index_entries[out++] = st->index_entries[j];
        st->nb_index_entries = out;
    }
}

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
static int amf_skip_tag(AVIOContext *pb, AMFDataType type)
{
    int nb = -1, ret, parse_name = 1;

    switch (type) {
    case AMF_DATA_TYPE_NUMBER:
        avio_skip(pb, 8);
        break;
    case AMF_DATA_TYPE_BOOL:
        avio_skip(pb, 1);
        break;
    case AMF_DATA_TYPE_STRING:
        avio_skip(pb, avio_rb16(pb));
        break;
    case AMF_DATA_TYPE_ARRAY:
        parse_name = 0;
    case AMF_DATA_TYPE_MIXEDARRAY:
        nb = avio_rb32(pb);
    case AMF_DATA_TYPE_OBJECT:
        while(!pb->eof_reached && (nb-- > 0 || type != AMF_DATA_TYPE_ARRAY)) {
            if (parse_name) {
                int size = avio_rb16(pb);
                if (!size) {
                    avio_skip(pb, 1);
                    break;
                }
                avio_skip(pb, size);
            }
            if ((ret = amf_skip_tag(pb, avio_r8(pb))) < 0)
                return ret;
        }
        break;
    case AMF_DATA_TYPE_NULL:
    case AMF_DATA_TYPE_OBJECT_END:
        break;
    default:
        return AVERROR_INVALIDDATA;
    }
    return 0;
}

881 882 883 884
static int flv_data_packet(AVFormatContext *s, AVPacket *pkt,
                           int64_t dts, int64_t next)
{
    AVIOContext *pb = s->pb;
885
    AVStream *st    = NULL;
886
    char buf[20];
887 888
    int ret = AVERROR_INVALIDDATA;
    int i, length = -1;
889
    int array = 0;
890

891
    switch (avio_r8(pb)) {
892 893
    case AMF_DATA_TYPE_ARRAY:
        array = 1;
894
    case AMF_DATA_TYPE_MIXEDARRAY:
895
        avio_seek(pb, 4, SEEK_CUR);
896 897 898 899 900
    case AMF_DATA_TYPE_OBJECT:
        break;
    default:
        goto skip;
    }
901

902
    while (array || (ret = amf_get_string(pb, buf, sizeof(buf))) > 0) {
903
        AMFDataType type = avio_r8(pb);
904
        if (type == AMF_DATA_TYPE_STRING && (array || !strcmp(buf, "text"))) {
905 906 907 908 909 910 911 912 913 914 915
            length = avio_rb16(pb);
            ret    = av_get_packet(pb, pkt, length);
            if (ret < 0)
                goto skip;
            else
                break;
        } else {
            if ((ret = amf_skip_tag(pb, type)) < 0)
                goto skip;
        }
    }
916

917 918 919 920
    if (length < 0) {
        ret = AVERROR_INVALIDDATA;
        goto skip;
    }
921 922 923

    for (i = 0; i < s->nb_streams; i++) {
        st = s->streams[i];
924
        if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
925 926 927 928
            break;
    }

    if (i == s->nb_streams) {
929
        st = create_stream(s, AVMEDIA_TYPE_SUBTITLE);
930
        if (!st)
931
            return AVERROR(ENOMEM);
932
        st->codecpar->codec_id = AV_CODEC_ID_TEXT;
933 934 935 936 937 938 939
    }

    pkt->dts  = dts;
    pkt->pts  = dts;
    pkt->size = ret;

    pkt->stream_index = st->index;
940
    pkt->flags       |= AV_PKT_FLAG_KEY;
941

942
skip:
943
    avio_seek(s->pb, next + 4, SEEK_SET);
944

945 946 947
    return ret;
}

948 949 950 951 952 953 954 955 956 957 958 959
static int resync(AVFormatContext *s)
{
    FLVContext *flv = s->priv_data;
    int64_t i;
    int64_t pos = avio_tell(s->pb);

    for (i=0; !avio_feof(s->pb); i++) {
        int j  = i & (RESYNC_BUFFER_SIZE-1);
        int j1 = j + RESYNC_BUFFER_SIZE;
        flv->resync_buffer[j ] =
        flv->resync_buffer[j1] = avio_r8(s->pb);

960 961 962 963 964 965 966 967 968 969 970 971
        if (i >= 8 && pos) {
            uint8_t *d = flv->resync_buffer + j1 - 8;
            if (d[0] == 'F' &&
                d[1] == 'L' &&
                d[2] == 'V' &&
                d[3] < 5 && d[5] == 0) {
                av_log(s, AV_LOG_WARNING, "Concatenated FLV detected, might fail to demux, decode and seek %"PRId64"\n", flv->last_ts);
                flv->time_offset = flv->last_ts + 1;
                flv->time_pos    = avio_tell(s->pb);
            }
        }

972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
        if (i > 22) {
            unsigned lsize2 = AV_RB32(flv->resync_buffer + j1 - 4);
            if (lsize2 >= 11 && lsize2 + 8LL < FFMIN(i, RESYNC_BUFFER_SIZE)) {
                unsigned  size2 = AV_RB24(flv->resync_buffer + j1 - lsize2 + 1 - 4);
                unsigned lsize1 = AV_RB32(flv->resync_buffer + j1 - lsize2 - 8);
                if (lsize1 >= 11 && lsize1 + 8LL + lsize2 < FFMIN(i, RESYNC_BUFFER_SIZE)) {
                    unsigned  size1 = AV_RB24(flv->resync_buffer + j1 - lsize1 + 1 - lsize2 - 8);
                    if (size1 == lsize1 - 11 && size2  == lsize2 - 11) {
                        avio_seek(s->pb, pos + i - lsize1 - lsize2 - 8, SEEK_SET);
                        return 1;
                    }
                }
            }
        }
    }
    return AVERROR_EOF;
}

990 991
static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
{
992
    FLVContext *flv = s->priv_data;
993
    int ret, i, size, flags;
994
    enum FlvTagType type;
995
    int stream_type=-1;
996
    int64_t next, pos, meta_pos;
997
    int64_t dts, pts = AV_NOPTS_VALUE;
998 999
    int av_uninit(channels);
    int av_uninit(sample_rate);
1000
    AVStream *st    = NULL;
1001
    int last = -1;
1002
    int orig_size;
1003

1004
retry:
1005
    /* pkt size is repeated at end. skip it */
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
    pos  = avio_tell(s->pb);
    type = (avio_r8(s->pb) & 0x1F);
    orig_size =
    size = avio_rb24(s->pb);
    flv->sum_flv_tag_size += size + 11;
    dts  = avio_rb24(s->pb);
    dts |= (unsigned)avio_r8(s->pb) << 24;
    av_log(s, AV_LOG_TRACE, "type:%d, size:%d, last:%d, dts:%"PRId64" pos:%"PRId64"\n", type, size, last, dts, avio_tell(s->pb));
    if (avio_feof(s->pb))
        return AVERROR_EOF;
    avio_skip(s->pb, 3); /* stream id, always 0 */
    flags = 0;

    if (flv->validate_next < flv->validate_count) {
        int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
        if (pos == validate_pos) {
            if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=
                VALIDATE_INDEX_TS_THRESH) {
                flv->validate_next++;
            } else {
1026 1027 1028
                clear_index_entries(s, validate_pos);
                flv->validate_count = 0;
            }
1029 1030 1031
        } else if (pos > validate_pos) {
            clear_index_entries(s, validate_pos);
            flv->validate_count = 0;
1032
        }
1033
    }
1034

1035 1036 1037 1038
    if (size == 0) {
        ret = FFERROR_REDO;
        goto leave;
    }
1039

1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
    next = size + avio_tell(s->pb);

    if (type == FLV_TAG_TYPE_AUDIO) {
        stream_type = FLV_STREAM_TYPE_AUDIO;
        flags    = avio_r8(s->pb);
        size--;
    } else if (type == FLV_TAG_TYPE_VIDEO) {
        stream_type = FLV_STREAM_TYPE_VIDEO;
        flags    = avio_r8(s->pb);
        size--;
        if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_VIDEO_INFO_CMD)
            goto skip;
    } else if (type == FLV_TAG_TYPE_META) {
        stream_type=FLV_STREAM_TYPE_SUBTITLE;
        if (size > 13 + 1 + 4) { // Header-type metadata stuff
            int type;
            meta_pos = avio_tell(s->pb);
            type = flv_read_metabody(s, next);
            if (type == 0 && dts == 0 || type < 0) {
                if (type < 0 && flv->validate_count &&
                    flv->validate_index[0].pos     > next &&
                    flv->validate_index[0].pos - 4 < next
                ) {
                    av_log(s, AV_LOG_WARNING, "Adjusting next position due to index mismatch\n");
                    next = flv->validate_index[0].pos - 4;
1065
                }
1066 1067 1068 1069 1070 1071 1072 1073
                goto skip;
            } else if (type == TYPE_ONTEXTDATA) {
                avpriv_request_sample(s, "OnTextData packet");
                return flv_data_packet(s, pkt, dts, next);
            } else if (type == TYPE_ONCAPTION) {
                return flv_data_packet(s, pkt, dts, next);
            } else if (type == TYPE_UNKNOWN) {
                stream_type = FLV_STREAM_TYPE_DATA;
1074
            }
1075 1076 1077 1078 1079 1080
            avio_seek(s->pb, meta_pos, SEEK_SET);
        }
    } else {
        av_log(s, AV_LOG_DEBUG,
               "Skipping flv packet: type %d, size %d, flags %d.\n",
               type, size, flags);
1081
skip:
1082 1083 1084 1085 1086 1087
        if (avio_seek(s->pb, next, SEEK_SET) != next) {
            // This can happen if flv_read_metabody above read past
            // next, on a non-seekable input, and the preceding data has
            // been flushed out from the IO buffer.
            av_log(s, AV_LOG_ERROR, "Unable to seek to the next packet\n");
            return AVERROR_INVALIDDATA;
1088
        }
1089 1090 1091
        ret = FFERROR_REDO;
        goto leave;
    }
1092

1093 1094 1095 1096 1097
    /* skip empty data packets */
    if (!size) {
        ret = FFERROR_REDO;
        goto leave;
    }
1098

1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
    /* now find stream */
    for (i = 0; i < s->nb_streams; i++) {
        st = s->streams[i];
        if (stream_type == FLV_STREAM_TYPE_AUDIO) {
            if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
                (s->audio_codec_id || flv_same_audio_codec(st->codecpar, flags)))
                break;
        } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
            if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
                (s->video_codec_id || flv_same_video_codec(st->codecpar, flags)))
                break;
        } else if (stream_type == FLV_STREAM_TYPE_SUBTITLE) {
            if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
                break;
        } else if (stream_type == FLV_STREAM_TYPE_DATA) {
            if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA)
                break;
1116
        }
1117 1118 1119 1120 1121 1122
    }
    if (i == s->nb_streams) {
        static const enum AVMediaType stream_types[] = {AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_SUBTITLE, AVMEDIA_TYPE_DATA};
        st = create_stream(s, stream_types[stream_type]);
        if (!st)
            return AVERROR(ENOMEM);
1123

1124 1125
    }
    av_log(s, AV_LOG_TRACE, "%d %X %d \n", stream_type, flags, st->discard);
1126

1127 1128 1129
    if (flv->time_pos <= pos) {
        dts += flv->time_offset;
    }
1130

1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
    if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
        ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||
         stream_type == FLV_STREAM_TYPE_AUDIO))
        av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);

    if (  (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || (stream_type == FLV_STREAM_TYPE_AUDIO)))
          ||(st->discard >= AVDISCARD_BIDIR  &&  ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && (stream_type == FLV_STREAM_TYPE_VIDEO)))
          || st->discard >= AVDISCARD_ALL
    ) {
        avio_seek(s->pb, next, SEEK_SET);
        ret = FFERROR_REDO;
        goto leave;
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
1144

1145 1146
    // if not streamed and no duration from metadata then seek to end to find
    // the duration from the timestamps
1147 1148
    if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
        (!s->duration || s->duration == AV_NOPTS_VALUE) &&
1149
        !flv->searched_for_end) {
1150
        int size;
1151
        const int64_t pos   = avio_tell(s->pb);
1152 1153
        // Read the last 4 bytes of the file, this should be the size of the
        // previous FLV tag. Use the timestamp of its payload as duration.
1154
        int64_t fsize       = avio_size(s->pb);
1155
retry_duration:
1156 1157
        avio_seek(s->pb, fsize - 4, SEEK_SET);
        size = avio_rb32(s->pb);
1158 1159 1160 1161 1162 1163 1164
        if (size > 0 && size < fsize) {
            // Seek to the start of the last FLV tag at position (fsize - 4 - size)
            // but skip the byte indicating the type.
            avio_seek(s->pb, fsize - 3 - size, SEEK_SET);
            if (size == avio_rb24(s->pb) + 11) {
                uint32_t ts = avio_rb24(s->pb);
                ts         |= avio_r8(s->pb) << 24;
1165 1166 1167 1168 1169 1170
                if (ts)
                    s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
                else if (fsize >= 8 && fsize - 8 >= size) {
                    fsize -= size+4;
                    goto retry_duration;
                }
1171
            }
1172
        }
1173

1174
        avio_seek(s->pb, pos, SEEK_SET);
1175
        flv->searched_for_end = 1;
1176 1177
    }

1178
    if (stream_type == FLV_STREAM_TYPE_AUDIO) {
1179
        int bits_per_coded_sample;
1180 1181 1182
        channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
        sample_rate = 44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >>
                                FLV_AUDIO_SAMPLERATE_OFFSET) >> 3;
1183
        bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
1184 1185 1186 1187
        if (!st->codecpar->channels || !st->codecpar->sample_rate ||
            !st->codecpar->bits_per_coded_sample) {
            st->codecpar->channels              = channels;
            st->codecpar->channel_layout        = channels == 1
1188 1189
                                               ? AV_CH_LAYOUT_MONO
                                               : AV_CH_LAYOUT_STEREO;
1190 1191
            st->codecpar->sample_rate           = sample_rate;
            st->codecpar->bits_per_coded_sample = bits_per_coded_sample;
1192
        }
1193 1194
        if (!st->codecpar->codec_id) {
            flv_set_audio_codec(s, st, st->codecpar,
1195 1196
                                flags & FLV_AUDIO_CODECID_MASK);
            flv->last_sample_rate =
1197
            sample_rate           = st->codecpar->sample_rate;
1198
            flv->last_channels    =
1199
            channels              = st->codecpar->channels;
1200
        } else {
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
            AVCodecParameters *par = avcodec_parameters_alloc();
            if (!par) {
                ret = AVERROR(ENOMEM);
                goto leave;
            }
            par->sample_rate = sample_rate;
            par->bits_per_coded_sample = bits_per_coded_sample;
            flv_set_audio_codec(s, st, par, flags & FLV_AUDIO_CODECID_MASK);
            sample_rate = par->sample_rate;
            avcodec_parameters_free(&par);
Michael Niedermayer's avatar
Michael Niedermayer committed
1211
        }
1212
    } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
1213 1214 1215 1216
        int ret = flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK, 1);
        if (ret < 0)
            return ret;
        size -= ret;
1217
    } else if (stream_type == FLV_STREAM_TYPE_SUBTITLE) {
1218
        st->codecpar->codec_id = AV_CODEC_ID_TEXT;
1219 1220
    } else if (stream_type == FLV_STREAM_TYPE_DATA) {
        st->codecpar->codec_id = AV_CODEC_ID_NONE; // Opaque AMF data
Michael Niedermayer's avatar
Michael Niedermayer committed
1221 1222
    }

1223
    if (st->codecpar->codec_id == AV_CODEC_ID_AAC ||
1224 1225
        st->codecpar->codec_id == AV_CODEC_ID_H264 ||
        st->codecpar->codec_id == AV_CODEC_ID_MPEG4) {
1226
        int type = avio_r8(s->pb);
1227
        size--;
1228 1229 1230 1231 1232 1233

        if (size < 0) {
            ret = AVERROR_INVALIDDATA;
            goto leave;
        }

1234
        if (st->codecpar->codec_id == AV_CODEC_ID_H264 || st->codecpar->codec_id == AV_CODEC_ID_MPEG4) {
1235 1236
            // sign extension
            int32_t cts = (avio_rb24(s->pb) + 0xff800000) ^ 0xff800000;
1237
            pts = dts + cts;
1238 1239 1240 1241
            if (cts < 0) { // dts might be wrong
                if (!flv->wrong_dts)
                    av_log(s, AV_LOG_WARNING,
                        "Negative cts, previous timestamps might be wrong.\n");
1242
                flv->wrong_dts = 1;
1243 1244 1245 1246
            } else if (FFABS(dts - pts) > 1000*60*15) {
                av_log(s, AV_LOG_WARNING,
                       "invalid timestamps %"PRId64" %"PRId64"\n", dts, pts);
                dts = pts = AV_NOPTS_VALUE;
1247
            }
1248
        }
1249 1250
        if (type == 0 && (!st->codecpar->extradata || st->codecpar->codec_id == AV_CODEC_ID_AAC ||
            st->codecpar->codec_id == AV_CODEC_ID_H264)) {
1251 1252
            AVDictionaryEntry *t;

1253
            if (st->codecpar->extradata) {
1254
                if ((ret = flv_queue_extradata(flv, s->pb, stream_type, size)) < 0)
1255
                    return ret;
1256
                ret = FFERROR_REDO;
1257 1258
                goto leave;
            }
1259
            if ((ret = flv_get_extradata(s, st, size)) < 0)
1260
                return ret;
1261

1262 1263
            /* Workaround for buggy Omnia A/XE encoder */
            t = av_dict_get(s->metadata, "Encoder", NULL, 0);
1264 1265
            if (st->codecpar->codec_id == AV_CODEC_ID_AAC && t && !strcmp(t->value, "Omnia A/XE"))
                st->codecpar->extradata_size = 2;
1266

1267
            ret = FFERROR_REDO;
Howard Chu's avatar
Howard Chu committed
1268
            goto leave;
1269 1270 1271
        }
    }

1272
    /* skip empty data packets */
Howard Chu's avatar
Howard Chu committed
1273
    if (!size) {
1274
        ret = FFERROR_REDO;
Howard Chu's avatar
Howard Chu committed
1275 1276
        goto leave;
    }
1277

1278
    ret = av_get_packet(s->pb, pkt, size);
1279 1280
    if (ret < 0)
        return ret;
1281 1282
    pkt->dts          = dts;
    pkt->pts          = pts == AV_NOPTS_VALUE ? dts : pts;
1283
    pkt->stream_index = st->index;
1284
    pkt->pos          = pos;
1285
    if (flv->new_extradata[stream_type]) {
1286
        uint8_t *side = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
1287
                                                flv->new_extradata_size[stream_type]);
1288
        if (side) {
1289 1290 1291 1292
            memcpy(side, flv->new_extradata[stream_type],
                   flv->new_extradata_size[stream_type]);
            av_freep(&flv->new_extradata[stream_type]);
            flv->new_extradata_size[stream_type] = 0;
1293 1294
        }
    }
1295 1296
    if (stream_type == FLV_STREAM_TYPE_AUDIO &&
                    (sample_rate != flv->last_sample_rate ||
1297
                     channels    != flv->last_channels)) {
1298 1299 1300 1301
        flv->last_sample_rate = sample_rate;
        flv->last_channels    = channels;
        ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
    }
1302

1303
    if (    stream_type == FLV_STREAM_TYPE_AUDIO ||
1304
            ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY) ||
1305 1306
            stream_type == FLV_STREAM_TYPE_SUBTITLE ||
            stream_type == FLV_STREAM_TYPE_DATA)
1307
        pkt->flags |= AV_PKT_FLAG_KEY;
1308

Howard Chu's avatar
Howard Chu committed
1309
leave:
1310
    last = avio_rb32(s->pb);
1311
    if (!flv->trust_datasize) {
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
        if (last != orig_size + 11 && last != orig_size + 10 &&
            !avio_feof(s->pb) &&
            (last != orig_size || !last) && last != flv->sum_flv_tag_size &&
            !flv->broken_sizes) {
            av_log(s, AV_LOG_ERROR, "Packet mismatch %d %d %d\n", last, orig_size + 11, flv->sum_flv_tag_size);
            avio_seek(s->pb, pos + 1, SEEK_SET);
            ret = resync(s);
            av_packet_unref(pkt);
            if (ret >= 0) {
                goto retry;
            }
1323 1324
        }
    }
1325 1326 1327 1328

    if (ret >= 0)
        flv->last_ts = pkt->dts;

1329 1330 1331
    return ret;
}

1332
static int flv_read_seek(AVFormatContext *s, int stream_index,
1333
                         int64_t ts, int flags)
1334
{
1335 1336
    FLVContext *flv = s->priv_data;
    flv->validate_count = 0;
1337
    return avio_seek_time(s->pb, stream_index, ts, flags);
1338 1339
}

1340 1341 1342
#define OFFSET(x) offsetof(FLVContext, x)
#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
static const AVOption options[] = {
1343
    { "flv_metadata", "Allocate streams according to the onMetaData array", OFFSET(trust_metadata), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
1344
    { "flv_full_metadata", "Dump full metadata of the onMetadata", OFFSET(dump_full_metadata), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
1345
    { "flv_ignore_prevtag", "Ignore the Size of previous tag", OFFSET(trust_datasize), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
1346
    { "missing_streams", "", OFFSET(missing_streams), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 0xFF, VD | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
1347 1348 1349
    { NULL }
};

1350
static const AVClass flv_class = {
1351 1352 1353 1354 1355 1356
    .class_name = "flvdec",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};

1357
AVInputFormat ff_flv_demuxer = {
1358
    .name           = "flv",
1359
    .long_name      = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
1360 1361 1362 1363
    .priv_data_size = sizeof(FLVContext),
    .read_probe     = flv_probe,
    .read_header    = flv_read_header,
    .read_packet    = flv_read_packet,
1364 1365 1366
    .read_seek      = flv_read_seek,
    .read_close     = flv_read_close,
    .extensions     = "flv",
1367
    .priv_class     = &flv_class,
1368
};
1369 1370

static const AVClass live_flv_class = {
1371
    .class_name = "live_flvdec",
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};

AVInputFormat ff_live_flv_demuxer = {
    .name           = "live_flv",
    .long_name      = NULL_IF_CONFIG_SMALL("live RTMP FLV (Flash Video)"),
    .priv_data_size = sizeof(FLVContext),
    .read_probe     = live_flv_probe,
    .read_header    = flv_read_header,
    .read_packet    = flv_read_packet,
    .read_seek      = flv_read_seek,
    .read_close     = flv_read_close,
    .extensions     = "flv",
    .priv_class     = &live_flv_class,
    .flags          = AVFMT_TS_DISCONT
};
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409

static const AVClass kux_class = {
    .class_name = "kuxdec",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};

AVInputFormat ff_kux_demuxer = {
    .name           = "kux",
    .long_name      = NULL_IF_CONFIG_SMALL("KUX (YouKu)"),
    .priv_data_size = sizeof(FLVContext),
    .read_probe     = kux_probe,
    .read_header    = flv_read_header,
    .read_packet    = flv_read_packet,
    .read_seek      = flv_read_seek,
    .read_close     = flv_read_close,
    .extensions     = "kux",
    .priv_class     = &kux_class,
};