flvdec.c 22 KB
Newer Older
1
/*
2
 * FLV demuxer
3
 * Copyright (c) 2003 The Libav Project
4
 *
5 6 7 8 9
 * This demuxer will generate a 1 byte extradata for VP6F content.
 * It is composed of:
 *  - upper 4bits: difference between encoded width and visible width
 *  - lower 4bits: difference between encoded height and visible height
 *
10
 * This file is part of Libav.
11
 *
12
 * Libav 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
 * Libav 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 Libav; 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/dict.h"
29
#include "libavutil/intfloat.h"
30
#include "libavutil/mathematics.h"
31
#include "libavcodec/bytestream.h"
32
#include "libavcodec/mpeg4audio.h"
33
#include "avformat.h"
34
#include "internal.h"
35
#include "avio_internal.h"
36
#include "flv.h"
37

38 39 40 41
#define KEYFRAMES_TAG            "keyframes"
#define KEYFRAMES_TIMESTAMP_TAG  "times"
#define KEYFRAMES_BYTEOFFSET_TAG "filepositions"

Baptiste Coudurier's avatar
Baptiste Coudurier committed
42 43 44 45
typedef struct {
    int wrong_dts; ///< wrong dts due to negative cts
} FLVContext;

46 47 48 49 50
static int flv_probe(AVProbeData *p)
{
    const uint8_t *d;

    d = p->buf;
51
    if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V' && d[3] < 5 && d[5]==0 && AV_RB32(d+5)>8) {
52
        return AVPROBE_SCORE_MAX;
53 54 55 56
    }
    return 0;
}

Allan Hsu's avatar
Allan Hsu committed
57 58 59 60
static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, int flv_codecid) {
    AVCodecContext *acodec = astream->codec;
    switch(flv_codecid) {
        //no distinction between S16 and S8 PCM codec flags
61
        case FLV_CODECID_PCM:
62
            acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 :
63
#if HAVE_BIGENDIAN
Michael Niedermayer's avatar
Michael Niedermayer committed
64 65 66 67 68
                                CODEC_ID_PCM_S16BE;
#else
                                CODEC_ID_PCM_S16LE;
#endif
            break;
Allan Hsu's avatar
Allan Hsu committed
69
        case FLV_CODECID_PCM_LE:
70
            acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 : CODEC_ID_PCM_S16LE; break;
71
        case FLV_CODECID_AAC  : acodec->codec_id = CODEC_ID_AAC;                                    break;
Allan Hsu's avatar
Allan Hsu committed
72
        case FLV_CODECID_ADPCM: acodec->codec_id = CODEC_ID_ADPCM_SWF;                              break;
73 74 75 76
        case FLV_CODECID_SPEEX:
            acodec->codec_id = CODEC_ID_SPEEX;
            acodec->sample_rate = 16000;
            break;
Aurelien Jacobs's avatar
Aurelien Jacobs committed
77
        case FLV_CODECID_MP3  : acodec->codec_id = CODEC_ID_MP3      ; astream->need_parsing = AVSTREAM_PARSE_FULL; break;
78
        case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
Allan Hsu's avatar
Allan Hsu committed
79
            acodec->sample_rate = 8000; //in case metadata does not otherwise declare samplerate
80 81 82 83 84 85
            acodec->codec_id = CODEC_ID_NELLYMOSER;
            break;
        case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
            acodec->sample_rate = 16000;
            acodec->codec_id = CODEC_ID_NELLYMOSER;
            break;
Allan Hsu's avatar
Allan Hsu committed
86
        case FLV_CODECID_NELLYMOSER:
Benjamin Larsson's avatar
Benjamin Larsson committed
87 88
            acodec->codec_id = CODEC_ID_NELLYMOSER;
            break;
Allan Hsu's avatar
Allan Hsu committed
89 90 91 92 93 94 95 96 97 98 99
        default:
            av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
            acodec->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
    }
}

static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid) {
    AVCodecContext *vcodec = vstream->codec;
    switch(flv_codecid) {
        case FLV_CODECID_H263  : vcodec->codec_id = CODEC_ID_FLV1   ; break;
        case FLV_CODECID_SCREEN: vcodec->codec_id = CODEC_ID_FLASHSV; break;
100
        case FLV_CODECID_SCREEN2: vcodec->codec_id = CODEC_ID_FLASHSV2; break;
Allan Hsu's avatar
Allan Hsu committed
101
        case FLV_CODECID_VP6   : vcodec->codec_id = CODEC_ID_VP6F   ;
Aurelien Jacobs's avatar
Aurelien Jacobs committed
102 103 104
        case FLV_CODECID_VP6A  :
            if(flv_codecid == FLV_CODECID_VP6A)
                vcodec->codec_id = CODEC_ID_VP6A;
Allan Hsu's avatar
Allan Hsu committed
105 106 107 108
            if(vcodec->extradata_size != 1) {
                vcodec->extradata_size = 1;
                vcodec->extradata = av_malloc(1);
            }
109
            vcodec->extradata[0] = avio_r8(s->pb);
Allan Hsu's avatar
Allan Hsu committed
110
            return 1; // 1 byte body size adjustment for flv_read_packet()
111 112 113
        case FLV_CODECID_H264:
            vcodec->codec_id = CODEC_ID_H264;
            return 3; // not 4, reading packet type will consume one byte
Allan Hsu's avatar
Allan Hsu committed
114 115 116 117 118 119 120 121
        default:
            av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid);
            vcodec->codec_tag = flv_codecid;
    }

    return 0;
}

122
static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize) {
123
    int length = avio_rb16(ioc);
124
    if(length >= buffsize) {
125
        avio_skip(ioc, length);
126 127
        return -1;
    }
128

129
    avio_read(ioc, buffer, length);
130

131
    buffer[length] = '\0';
132

133
    return length;
134 135
}

136 137 138 139 140 141
static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, AVStream *vstream, int64_t max_pos) {
    unsigned int arraylen = 0, timeslen = 0, fileposlen = 0, i;
    double num_val;
    char str_val[256];
    int64_t *times = NULL;
    int64_t *filepositions = NULL;
142
    int ret = AVERROR(ENOSYS);
143
    int64_t initial_pos = avio_tell(ioc);
144 145 146 147 148 149 150 151 152 153 154 155
    AVDictionaryEntry *creator = av_dict_get(s->metadata, "metadatacreator",
                                             NULL, 0);

    if (creator && !strcmp(creator->value, "MEGA")) {
        /* Files with this metadatacreator tag seem to have filepositions
         * pointing at the 4 trailer bytes of the previous packet,
         * which isn't the norm (nor what we expect here, nor what
         * jwplayer + lighttpd expect, nor what flvtool2 produces).
         * Just ignore the index in this case, instead of risking trying
         * to adjust it to something that might or might not work. */
        return 0;
    }
156 157 158 159 160 161 162 163 164

    while (avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
        int64_t* current_array;

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

        arraylen = avio_rb32(ioc);
165 166 167
        if (arraylen >> 28)
            break;

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
        /*
         * Expect only 'times' or 'filepositions' sub-arrays in other case refuse to use such metadata
         * for indexing
         */
        if (!strcmp(KEYFRAMES_TIMESTAMP_TAG, str_val) && !times) {
            if (!(times = av_mallocz(sizeof(*times) * arraylen))) {
                ret = AVERROR(ENOMEM);
                goto finish;
            }
            timeslen = arraylen;
            current_array = times;
        } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) && !filepositions) {
            if (!(filepositions = av_mallocz(sizeof(*filepositions) * arraylen))) {
                ret = AVERROR(ENOMEM);
                goto finish;
            }
            fileposlen = arraylen;
            current_array = filepositions;
        } else // unexpected metatag inside keyframes, will not use such metadata for indexing
            break;

        for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
            if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
                goto finish;
192
            num_val = av_int2double(avio_rb64(ioc));
193 194
            current_array[i] = num_val;
        }
195 196 197 198 199 200
        if (times && filepositions) {
            // All done, exiting at a position allowing amf_parse_object
            // to finish parsing the object
            ret = 0;
            break;
        }
201 202
    }

203 204
    if (!ret && timeslen == fileposlen)
         for (i = 0; i < fileposlen; i++)
205 206 207 208 209 210 211
             av_add_index_entry(vstream, filepositions[i], times[i]*1000, 0, 0, AVINDEX_KEYFRAME);
    else
        av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");

finish:
    av_freep(&times);
    av_freep(&filepositions);
212 213 214 215
    // If we got unexpected data, but successfully reset back to
    // the start pos, the caller can continue parsing
    if (ret < 0 && avio_seek(ioc, initial_pos, SEEK_SET) > 0)
        return 0;
216 217 218
    return ret;
}

Pascal Massimino's avatar
Pascal Massimino committed
219
static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, int64_t max_pos, int depth) {
Allan Hsu's avatar
Allan Hsu committed
220
    AVCodecContext *acodec, *vcodec;
221
    AVIOContext *ioc;
Allan Hsu's avatar
Allan Hsu committed
222
    AMFDataType amf_type;
223
    char str_val[256];
Allan Hsu's avatar
Allan Hsu committed
224 225 226
    double num_val;

    num_val = 0;
227
    ioc = s->pb;
Allan Hsu's avatar
Allan Hsu committed
228

229
    amf_type = avio_r8(ioc);
Allan Hsu's avatar
Allan Hsu committed
230 231 232

    switch(amf_type) {
        case AMF_DATA_TYPE_NUMBER:
233
            num_val = av_int2double(avio_rb64(ioc)); break;
Allan Hsu's avatar
Allan Hsu committed
234
        case AMF_DATA_TYPE_BOOL:
235
            num_val = avio_r8(ioc); break;
Allan Hsu's avatar
Allan Hsu committed
236
        case AMF_DATA_TYPE_STRING:
237
            if(amf_get_string(ioc, str_val, sizeof(str_val)) < 0)
Allan Hsu's avatar
Allan Hsu committed
238 239 240 241 242
                return -1;
            break;
        case AMF_DATA_TYPE_OBJECT: {
            unsigned int keylen;

243 244 245
            if ((vstream || astream) && key && !strcmp(KEYFRAMES_TAG, key) && depth == 1)
                if (parse_keyframes_index(s, ioc, vstream ? vstream : astream,
                                          max_pos) < 0)
246 247
                    return -1;

248
            while(avio_tell(ioc) < max_pos - 2 && (keylen = avio_rb16(ioc))) {
249
                avio_skip(ioc, keylen); //skip key string
Allan Hsu's avatar
Allan Hsu committed
250 251 252
                if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
                    return -1; //if we couldn't skip, bomb out.
            }
253
            if(avio_r8(ioc) != AMF_END_OF_OBJECT)
Allan Hsu's avatar
Allan Hsu committed
254 255 256 257 258 259 260 261
                return -1;
        }
            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:
262
            avio_skip(ioc, 4); //skip 32-bit max array index
263
            while(avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
Allan Hsu's avatar
Allan Hsu committed
264
                //this is the only case in which we would want a nested parse to not skip over the object
265
                if(amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
Allan Hsu's avatar
Allan Hsu committed
266 267
                    return -1;
            }
268
            if(avio_r8(ioc) != AMF_END_OF_OBJECT)
Allan Hsu's avatar
Allan Hsu committed
269 270 271 272 273
                return -1;
            break;
        case AMF_DATA_TYPE_ARRAY: {
            unsigned int arraylen, i;

274
            arraylen = avio_rb32(ioc);
275
            for(i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
Allan Hsu's avatar
Allan Hsu committed
276 277 278 279 280 281
                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:
282
            avio_skip(ioc, 8 + 2); //timestamp (double) and UTC offset (int16)
Allan Hsu's avatar
Allan Hsu committed
283 284 285 286 287 288 289 290 291
            break;
        default: //unsupported type, we couldn't skip
            return -1;
    }

    if(depth == 1 && key) { //only look for metadata values when we are not nested and key != NULL
        acodec = astream ? astream->codec : NULL;
        vcodec = vstream ? vstream->codec : NULL;

292 293 294 295 296 297 298 299 300
        if (amf_type == AMF_DATA_TYPE_NUMBER) {
            if (!strcmp(key, "duration"))
                s->duration = num_val * AV_TIME_BASE;
            else if (!strcmp(key, "videodatarate") && vcodec && 0 <= (int)(num_val * 1024.0))
                vcodec->bit_rate = num_val * 1024.0;
            else if (!strcmp(key, "audiodatarate") && acodec && 0 <= (int)(num_val * 1024.0))
                acodec->bit_rate = num_val * 1024.0;
        }

301 302 303 304 305 306 307 308 309 310 311 312 313 314
        if (!strcmp(key, "duration")        ||
            !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")          ||
            !strcmp(key, "audiocodecid"))
            return 0;

Allan Hsu's avatar
Allan Hsu committed
315
        if(amf_type == AMF_DATA_TYPE_BOOL) {
316
            av_strlcpy(str_val, num_val > 0 ? "true" : "false", sizeof(str_val));
317
            av_dict_set(&s->metadata, key, str_val, 0);
Allan Hsu's avatar
Allan Hsu committed
318
        } else if(amf_type == AMF_DATA_TYPE_NUMBER) {
319
            snprintf(str_val, sizeof(str_val), "%.f", num_val);
320
            av_dict_set(&s->metadata, key, str_val, 0);
321
        } else if (amf_type == AMF_DATA_TYPE_STRING)
322
            av_dict_set(&s->metadata, key, str_val, 0);
Allan Hsu's avatar
Allan Hsu committed
323 324 325 326 327
    }

    return 0;
}

Pascal Massimino's avatar
Pascal Massimino committed
328
static int flv_read_metabody(AVFormatContext *s, int64_t next_pos) {
Allan Hsu's avatar
Allan Hsu committed
329 330
    AMFDataType type;
    AVStream *stream, *astream, *vstream;
331
    AVIOContext *ioc;
332
    int i;
333
    char buffer[11]; //only needs to hold the string "onMetaData". Anything longer is something we don't want.
Allan Hsu's avatar
Allan Hsu committed
334 335 336

    astream = NULL;
    vstream = NULL;
337
    ioc = s->pb;
Allan Hsu's avatar
Allan Hsu committed
338 339

    //first object needs to be "onMetaData" string
340
    type = avio_r8(ioc);
341
    if(type != AMF_DATA_TYPE_STRING || amf_get_string(ioc, buffer, sizeof(buffer)) < 0 || strcmp(buffer, "onMetaData"))
Allan Hsu's avatar
Allan Hsu committed
342 343 344 345 346
        return -1;

    //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++) {
        stream = s->streams[i];
347 348
        if     (stream->codec->codec_type == AVMEDIA_TYPE_AUDIO) astream = stream;
        else if(stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) vstream = stream;
Allan Hsu's avatar
Allan Hsu committed
349 350 351
    }

    //parse the second object (we want a mixed array)
352
    if(amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
Allan Hsu's avatar
Allan Hsu committed
353 354 355 356 357
        return -1;

    return 0;
}

358
static AVStream *create_stream(AVFormatContext *s, int is_audio){
359
    AVStream *st = avformat_new_stream(s, NULL);
360 361
    if (!st)
        return NULL;
362
    st->id = is_audio;
363
    st->codec->codec_type = is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO;
364
    avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
365 366 367
    return st;
}

368 369 370
static int flv_read_header(AVFormatContext *s,
                           AVFormatParameters *ap)
{
371
    int offset, flags;
372

373
    avio_skip(s->pb, 4);
374
    flags = avio_r8(s->pb);
375 376 377 378 379 380
    /* old flvtool cleared this field */
    /* FIXME: better fix needed */
    if (!flags) {
        flags = FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO;
        av_log(s, AV_LOG_WARNING, "Broken FLV file, which says no streams present, this might fail\n");
    }
381

Michael Niedermayer's avatar
Michael Niedermayer committed
382 383 384 385
    if((flags & (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
             != (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
        s->ctx_flags |= AVFMTCTX_NOHEADER;

386
    if(flags & FLV_HEADER_FLAG_HASVIDEO){
387
        if(!create_stream(s, 0))
388
            return AVERROR(ENOMEM);
389 390
    }
    if(flags & FLV_HEADER_FLAG_HASAUDIO){
391
        if(!create_stream(s, 1))
392
            return AVERROR(ENOMEM);
393 394
    }

395
    offset = avio_rb32(s->pb);
396
    avio_seek(s->pb, offset, SEEK_SET);
397
    avio_skip(s->pb, 4);
398

399 400
    s->start_time = 0;

401 402 403
    return 0;
}

404 405 406 407 408 409 410
static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
{
    av_free(st->codec->extradata);
    st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
    if (!st->codec->extradata)
        return AVERROR(ENOMEM);
    st->codec->extradata_size = size;
411
    avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
412 413 414
    return 0;
}

415 416
static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
{
Baptiste Coudurier's avatar
Baptiste Coudurier committed
417
    FLVContext *flv = s->priv_data;
Pascal Massimino's avatar
Pascal Massimino committed
418 419
    int ret, i, type, size, flags, is_audio;
    int64_t next, pos;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
420
    int64_t dts, pts = AV_NOPTS_VALUE;
Alex Beregszaszi's avatar
Alex Beregszaszi committed
421
    AVStream *st = NULL;
422

423
 for(;;avio_skip(s->pb, 4)){ /* pkt size is repeated at end. skip it */
424
    pos = avio_tell(s->pb);
425 426 427 428
    type = avio_r8(s->pb);
    size = avio_rb24(s->pb);
    dts = avio_rb24(s->pb);
    dts |= avio_r8(s->pb) << 24;
Alex Converse's avatar
Alex Converse committed
429
    av_dlog(s, "type:%d, size:%d, dts:%"PRId64"\n", type, size, dts);
Anton Khirnov's avatar
Anton Khirnov committed
430
    if (s->pb->eof_reached)
431
        return AVERROR_EOF;
432
    avio_skip(s->pb, 3); /* stream id, always 0 */
433
    flags = 0;
434

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
435 436
    if(size == 0)
        continue;
437

438
    next= size + avio_tell(s->pb);
439

440
    if (type == FLV_TAG_TYPE_AUDIO) {
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
441
        is_audio=1;
442
        flags = avio_r8(s->pb);
443
        size--;
444
    } else if (type == FLV_TAG_TYPE_VIDEO) {
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
445
        is_audio=0;
446
        flags = avio_r8(s->pb);
447
        size--;
448 449
        if ((flags & 0xf0) == 0x50) /* video info / command frame */
            goto skip;
450
    } else {
451
        if (type == FLV_TAG_TYPE_META && size > 13+1+4)
Allan Hsu's avatar
Allan Hsu committed
452 453
            flv_read_metabody(s, next);
        else /* skip packet */
454
            av_log(s, AV_LOG_DEBUG, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
455
    skip:
456
        avio_seek(s->pb, next, SEEK_SET);
457
        continue;
458 459
    }

460 461 462 463
    /* skip empty data packets */
    if (!size)
        continue;

464 465 466
    /* now find stream */
    for(i=0;i<s->nb_streams;i++) {
        st = s->streams[i];
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
467 468
        if (st->id == is_audio)
            break;
469
    }
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
470
    if(i == s->nb_streams){
471
        av_log(s, AV_LOG_ERROR, "invalid stream\n");
472
        st= create_stream(s, is_audio);
473
        s->ctx_flags &= ~AVFMTCTX_NOHEADER;
474
    }
Alex Converse's avatar
Alex Converse committed
475
    av_dlog(s, "%d %X %d \n", is_audio, flags, st->discard);
476 477
    if(  (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||         is_audio))
       ||(st->discard >= AVDISCARD_BIDIR  &&  ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))
478 479
       || st->discard >= AVDISCARD_ALL
       ){
480
        avio_seek(s->pb, next, SEEK_SET);
481
        continue;
482
    }
483
    if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
484
        av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
485 486 487
    break;
 }

488
    // if not streamed and no duration from metadata then seek to end to find the duration from the timestamps
489
    if(s->pb->seekable && (!s->duration || s->duration==AV_NOPTS_VALUE)){
490
        int size;
491
        const int64_t pos= avio_tell(s->pb);
492
        const int64_t fsize= avio_size(s->pb);
493
        avio_seek(s->pb, fsize-4, SEEK_SET);
494
        size= avio_rb32(s->pb);
495
        avio_seek(s->pb, fsize-3-size, SEEK_SET);
496 497 498
        if(size == avio_rb24(s->pb) + 11){
            uint32_t ts = avio_rb24(s->pb);
            ts |= avio_r8(s->pb) << 24;
499
            s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
500
        }
501
        avio_seek(s->pb, pos, SEEK_SET);
502 503
    }

Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
504
    if(is_audio){
505
        if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample) {
506
            st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
507
            st->codec->sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
508
            st->codec->bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
509 510
        }
        if(!st->codec->codec_id){
Allan Hsu's avatar
Allan Hsu committed
511
            flv_set_audio_codec(s, st, flags & FLV_AUDIO_CODECID_MASK);
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
512 513
        }
    }else{
Allan Hsu's avatar
Allan Hsu committed
514
        size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);
Michael Niedermayer's avatar
Michael Niedermayer committed
515 516
    }

517 518
    if (st->codec->codec_id == CODEC_ID_AAC ||
        st->codec->codec_id == CODEC_ID_H264) {
519
        int type = avio_r8(s->pb);
520 521
        size--;
        if (st->codec->codec_id == CODEC_ID_H264) {
522
            int32_t cts = (avio_rb24(s->pb)+0xff800000)^0xff800000; // sign extension
Baptiste Coudurier's avatar
Baptiste Coudurier committed
523 524 525 526 527 528 529
            pts = dts + cts;
            if (cts < 0) { // dts are wrong
                flv->wrong_dts = 1;
                av_log(s, AV_LOG_WARNING, "negative cts, previous timestamps might be wrong\n");
            }
            if (flv->wrong_dts)
                dts = AV_NOPTS_VALUE;
530 531
        }
        if (type == 0) {
532
            if ((ret = flv_get_extradata(s, st, size)) < 0)
533
                return ret;
534 535
            if (st->codec->codec_id == CODEC_ID_AAC) {
                MPEG4AudioConfig cfg;
536
                avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata,
537
                                             st->codec->extradata_size * 8, 1);
538
                st->codec->channels = cfg.channels;
539 540 541 542
                if (cfg.ext_sample_rate)
                    st->codec->sample_rate = cfg.ext_sample_rate;
                else
                    st->codec->sample_rate = cfg.sample_rate;
Luca Barbato's avatar
Luca Barbato committed
543
                av_dlog(s, "mp4a config channels %d sample rate %d\n",
544 545 546
                        st->codec->channels, st->codec->sample_rate);
            }

Howard Chu's avatar
Howard Chu committed
547 548
            ret = AVERROR(EAGAIN);
            goto leave;
549 550 551
        }
    }

552
    /* skip empty data packets */
Howard Chu's avatar
Howard Chu committed
553 554 555 556
    if (!size) {
        ret = AVERROR(EAGAIN);
        goto leave;
    }
557

558
    ret= av_get_packet(s->pb, pkt, size);
559
    if (ret < 0) {
560
        return AVERROR(EIO);
561 562 563 564
    }
    /* note: we need to modify the packet size here to handle the last
       packet */
    pkt->size = ret;
565
    pkt->dts = dts;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
566
    pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
567
    pkt->stream_index = st->index;
568

569
    if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
570
        pkt->flags |= AV_PKT_FLAG_KEY;
571

Howard Chu's avatar
Howard Chu committed
572
leave:
573
    avio_skip(s->pb, 4);
574 575 576
    return ret;
}

Howard Chu's avatar
Howard Chu committed
577 578 579
static int flv_read_seek(AVFormatContext *s, int stream_index,
    int64_t ts, int flags)
{
580
    return avio_seek_time(s->pb, stream_index, ts, flags);
Howard Chu's avatar
Howard Chu committed
581 582 583 584 585 586 587 588 589 590
}

#if 0 /* don't know enough to implement this */
static int flv_read_seek2(AVFormatContext *s, int stream_index,
    int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
{
    int ret = AVERROR(ENOSYS);

    if (ts - min_ts > (uint64_t)(max_ts - ts)) flags |= AVSEEK_FLAG_BACKWARD;

591
    if (!s->pb->seekable) {
Howard Chu's avatar
Howard Chu committed
592 593 594 595 596 597 598 599 600
        if (stream_index < 0) {
            stream_index = av_find_default_stream_index(s);
            if (stream_index < 0)
                return -1;

            /* timestamp for default must be expressed in AV_TIME_BASE units */
            ts = av_rescale_rnd(ts, 1000, AV_TIME_BASE,
                flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
        }
601
        ret = avio_seek_time(s->pb, stream_index, ts, flags);
Howard Chu's avatar
Howard Chu committed
602 603 604 605 606 607 608 609
    }

    if (ret == AVERROR(ENOSYS))
        ret = av_seek_frame(s, stream_index, ts, flags);
    return ret;
}
#endif

610
AVInputFormat ff_flv_demuxer = {
611 612 613 614 615 616
    .name           = "flv",
    .long_name      = NULL_IF_CONFIG_SMALL("FLV format"),
    .priv_data_size = sizeof(FLVContext),
    .read_probe     = flv_probe,
    .read_header    = flv_read_header,
    .read_packet    = flv_read_packet,
Howard Chu's avatar
Howard Chu committed
617 618 619 620
    .read_seek = flv_read_seek,
#if 0
    .read_seek2 = flv_read_seek2,
#endif
621 622 623
    .extensions = "flv",
    .value = CODEC_ID_FLV1,
};