Statistics
| Branch: | Revision:

ffmpeg / libavformat / flvdec.c @ e356fc57

History | View | Annotate | Download (17.7 KB)

1 d4f5d74a Garrick Meeker
/*
2 7fbde343 Aurelien Jacobs
 * FLV demuxer
3 406792e7 Diego Biurrun
 * Copyright (c) 2003 The FFmpeg Project
4 d4f5d74a Garrick Meeker
 *
5 7b94177e Diego Biurrun
 * This demuxer will generate a 1 byte extradata for VP6F content.
6
 * It is composed of:
7
 *  - upper 4bits: difference between encoded width and visible width
8
 *  - lower 4bits: difference between encoded height and visible height
9
 *
10 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
11
 *
12
 * FFmpeg is free software; you can redistribute it and/or
13 d4f5d74a Garrick Meeker
 * modify it under the terms of the GNU Lesser General Public
14
 * License as published by the Free Software Foundation; either
15 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
16 d4f5d74a Garrick Meeker
 *
17 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
18 d4f5d74a Garrick Meeker
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20
 * Lesser General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Lesser General Public
23 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
24 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 d4f5d74a Garrick Meeker
 */
26 d2718187 Baptiste Coudurier
27 df2bd71a Aurelien Jacobs
#include "libavutil/avstring.h"
28 c1c206b3 Justin Ruggles
#include "libavcodec/bytestream.h"
29 d2718187 Baptiste Coudurier
#include "libavcodec/mpeg4audio.h"
30 d4f5d74a Garrick Meeker
#include "avformat.h"
31 6cac3a3b Allan Hsu
#include "flv.h"
32 d4f5d74a Garrick Meeker
33 ebd61055 Baptiste Coudurier
typedef struct {
34
    int wrong_dts; ///< wrong dts due to negative cts
35
} FLVContext;
36
37 d4f5d74a Garrick Meeker
static int flv_probe(AVProbeData *p)
38
{
39
    const uint8_t *d;
40
41
    d = p->buf;
42 37e34df5 Michael Niedermayer
    if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V' && d[3] < 5 && d[5]==0 && AV_RB32(d+5)>8) {
43 74248229 Michael Niedermayer
        return AVPROBE_SCORE_MAX;
44 d4f5d74a Garrick Meeker
    }
45
    return 0;
46
}
47
48 428cc588 Allan Hsu
static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, int flv_codecid) {
49
    AVCodecContext *acodec = astream->codec;
50
    switch(flv_codecid) {
51
        //no distinction between S16 and S8 PCM codec flags
52 44de39f9 Michael Niedermayer
        case FLV_CODECID_PCM:
53 8e9efe43 Daniel Verkamp
            acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 :
54 63613fe6 Måns Rullgård
#if HAVE_BIGENDIAN
55 58293e57 Michael Niedermayer
                                CODEC_ID_PCM_S16BE;
56
#else
57
                                CODEC_ID_PCM_S16LE;
58
#endif
59
            break;
60 428cc588 Allan Hsu
        case FLV_CODECID_PCM_LE:
61 8e9efe43 Daniel Verkamp
            acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 : CODEC_ID_PCM_S16LE; break;
62 04fd3e81 Baptiste Coudurier
        case FLV_CODECID_AAC  : acodec->codec_id = CODEC_ID_AAC;                                    break;
63 428cc588 Allan Hsu
        case FLV_CODECID_ADPCM: acodec->codec_id = CODEC_ID_ADPCM_SWF;                              break;
64 f96d0eef Baptiste Coudurier
        case FLV_CODECID_SPEEX:
65
            acodec->codec_id = CODEC_ID_SPEEX;
66
            acodec->sample_rate = 16000;
67
            break;
68 57004ff1 Aurelien Jacobs
        case FLV_CODECID_MP3  : acodec->codec_id = CODEC_ID_MP3      ; astream->need_parsing = AVSTREAM_PARSE_FULL; break;
69 88cb61bb Alexander Wichers
        case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
70 428cc588 Allan Hsu
            acodec->sample_rate = 8000; //in case metadata does not otherwise declare samplerate
71 caa7ad5d Thierry Foucu
            acodec->codec_id = CODEC_ID_NELLYMOSER;
72
            break;
73
        case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
74
            acodec->sample_rate = 16000;
75
            acodec->codec_id = CODEC_ID_NELLYMOSER;
76
            break;
77 428cc588 Allan Hsu
        case FLV_CODECID_NELLYMOSER:
78 636b13c5 Benjamin Larsson
            acodec->codec_id = CODEC_ID_NELLYMOSER;
79
            break;
80 428cc588 Allan Hsu
        default:
81
            av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
82
            acodec->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
83
    }
84
}
85
86
static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid) {
87
    AVCodecContext *vcodec = vstream->codec;
88
    switch(flv_codecid) {
89
        case FLV_CODECID_H263  : vcodec->codec_id = CODEC_ID_FLV1   ; break;
90
        case FLV_CODECID_SCREEN: vcodec->codec_id = CODEC_ID_FLASHSV; break;
91 0aa6a518 Daniel Verkamp
        case FLV_CODECID_SCREEN2: vcodec->codec_id = CODEC_ID_FLASHSV2; break;
92 428cc588 Allan Hsu
        case FLV_CODECID_VP6   : vcodec->codec_id = CODEC_ID_VP6F   ;
93 99904603 Aurelien Jacobs
        case FLV_CODECID_VP6A  :
94
            if(flv_codecid == FLV_CODECID_VP6A)
95
                vcodec->codec_id = CODEC_ID_VP6A;
96 428cc588 Allan Hsu
            if(vcodec->extradata_size != 1) {
97
                vcodec->extradata_size = 1;
98
                vcodec->extradata = av_malloc(1);
99
            }
100 b7effd4e Anton Khirnov
            vcodec->extradata[0] = avio_r8(s->pb);
101 428cc588 Allan Hsu
            return 1; // 1 byte body size adjustment for flv_read_packet()
102 04fd3e81 Baptiste Coudurier
        case FLV_CODECID_H264:
103
            vcodec->codec_id = CODEC_ID_H264;
104
            return 3; // not 4, reading packet type will consume one byte
105 428cc588 Allan Hsu
        default:
106
            av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid);
107
            vcodec->codec_tag = flv_codecid;
108
    }
109
110
    return 0;
111
}
112
113 ae628ec1 Anton Khirnov
static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize) {
114 b7effd4e Anton Khirnov
    int length = avio_rb16(ioc);
115 cc38e063 Michael Niedermayer
    if(length >= buffsize) {
116 e356fc57 Anton Khirnov
        avio_seek(ioc, length, SEEK_CUR);
117 cc38e063 Michael Niedermayer
        return -1;
118
    }
119 896bcd2e Michael Niedermayer
120 b7effd4e Anton Khirnov
    avio_read(ioc, buffer, length);
121 896bcd2e Michael Niedermayer
122 cc38e063 Michael Niedermayer
    buffer[length] = '\0';
123 896bcd2e Michael Niedermayer
124 cc38e063 Michael Niedermayer
    return length;
125 896bcd2e Michael Niedermayer
}
126
127 4fe8a452 Pascal Massimino
static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, int64_t max_pos, int depth) {
128 428cc588 Allan Hsu
    AVCodecContext *acodec, *vcodec;
129 ae628ec1 Anton Khirnov
    AVIOContext *ioc;
130 428cc588 Allan Hsu
    AMFDataType amf_type;
131 cc38e063 Michael Niedermayer
    char str_val[256];
132 428cc588 Allan Hsu
    double num_val;
133
134
    num_val = 0;
135 899681cd Björn Axelsson
    ioc = s->pb;
136 428cc588 Allan Hsu
137 b7effd4e Anton Khirnov
    amf_type = avio_r8(ioc);
138 428cc588 Allan Hsu
139
    switch(amf_type) {
140
        case AMF_DATA_TYPE_NUMBER:
141 b7effd4e Anton Khirnov
            num_val = av_int2dbl(avio_rb64(ioc)); break;
142 428cc588 Allan Hsu
        case AMF_DATA_TYPE_BOOL:
143 b7effd4e Anton Khirnov
            num_val = avio_r8(ioc); break;
144 428cc588 Allan Hsu
        case AMF_DATA_TYPE_STRING:
145 cc38e063 Michael Niedermayer
            if(amf_get_string(ioc, str_val, sizeof(str_val)) < 0)
146 428cc588 Allan Hsu
                return -1;
147
            break;
148
        case AMF_DATA_TYPE_OBJECT: {
149
            unsigned int keylen;
150
151 b7effd4e Anton Khirnov
            while(url_ftell(ioc) < max_pos - 2 && (keylen = avio_rb16(ioc))) {
152 e356fc57 Anton Khirnov
                avio_seek(ioc, keylen, SEEK_CUR); //skip key string
153 428cc588 Allan Hsu
                if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
154
                    return -1; //if we couldn't skip, bomb out.
155
            }
156 b7effd4e Anton Khirnov
            if(avio_r8(ioc) != AMF_END_OF_OBJECT)
157 428cc588 Allan Hsu
                return -1;
158
        }
159
            break;
160
        case AMF_DATA_TYPE_NULL:
161
        case AMF_DATA_TYPE_UNDEFINED:
162
        case AMF_DATA_TYPE_UNSUPPORTED:
163
            break; //these take up no additional space
164
        case AMF_DATA_TYPE_MIXEDARRAY:
165 e356fc57 Anton Khirnov
            avio_seek(ioc, 4, SEEK_CUR); //skip 32-bit max array index
166 cc38e063 Michael Niedermayer
            while(url_ftell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
167 428cc588 Allan Hsu
                //this is the only case in which we would want a nested parse to not skip over the object
168 cc38e063 Michael Niedermayer
                if(amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
169 428cc588 Allan Hsu
                    return -1;
170
            }
171 b7effd4e Anton Khirnov
            if(avio_r8(ioc) != AMF_END_OF_OBJECT)
172 428cc588 Allan Hsu
                return -1;
173
            break;
174
        case AMF_DATA_TYPE_ARRAY: {
175
            unsigned int arraylen, i;
176
177 b7effd4e Anton Khirnov
            arraylen = avio_rb32(ioc);
178 428cc588 Allan Hsu
            for(i = 0; i < arraylen && url_ftell(ioc) < max_pos - 1; i++) {
179
                if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
180
                    return -1; //if we couldn't skip, bomb out.
181
            }
182
        }
183
            break;
184
        case AMF_DATA_TYPE_DATE:
185 e356fc57 Anton Khirnov
            avio_seek(ioc, 8 + 2, SEEK_CUR); //timestamp (double) and UTC offset (int16)
186 428cc588 Allan Hsu
            break;
187
        default: //unsupported type, we couldn't skip
188
            return -1;
189
    }
190
191
    if(depth == 1 && key) { //only look for metadata values when we are not nested and key != NULL
192
        acodec = astream ? astream->codec : NULL;
193
        vcodec = vstream ? vstream->codec : NULL;
194
195
        if(amf_type == AMF_DATA_TYPE_BOOL) {
196 cc38e063 Michael Niedermayer
            av_strlcpy(str_val, num_val > 0 ? "true" : "false", sizeof(str_val));
197 2ef6c124 Stefano Sabatini
            av_metadata_set2(&s->metadata, key, str_val, 0);
198 428cc588 Allan Hsu
        } else if(amf_type == AMF_DATA_TYPE_NUMBER) {
199 cc38e063 Michael Niedermayer
            snprintf(str_val, sizeof(str_val), "%.f", num_val);
200 2ef6c124 Stefano Sabatini
            av_metadata_set2(&s->metadata, key, str_val, 0);
201 428cc588 Allan Hsu
            if(!strcmp(key, "duration")) s->duration = num_val * AV_TIME_BASE;
202 9a354fe3 Stefan de Konink
            else if(!strcmp(key, "videodatarate") && vcodec && 0 <= (int)(num_val * 1024.0))
203
                vcodec->bit_rate = num_val * 1024.0;
204 7e939205 Howard Chu
            else if(!strcmp(key, "audiodatarate") && acodec && 0 <= (int)(num_val * 1024.0))
205
                acodec->bit_rate = num_val * 1024.0;
206 df2bd71a Aurelien Jacobs
        } else if (amf_type == AMF_DATA_TYPE_STRING)
207 2ef6c124 Stefano Sabatini
            av_metadata_set2(&s->metadata, key, str_val, 0);
208 428cc588 Allan Hsu
    }
209
210
    return 0;
211
}
212
213 4fe8a452 Pascal Massimino
static int flv_read_metabody(AVFormatContext *s, int64_t next_pos) {
214 428cc588 Allan Hsu
    AMFDataType type;
215
    AVStream *stream, *astream, *vstream;
216 ae628ec1 Anton Khirnov
    AVIOContext *ioc;
217 4eec2606 Michael Niedermayer
    int i;
218 cc38e063 Michael Niedermayer
    char buffer[11]; //only needs to hold the string "onMetaData". Anything longer is something we don't want.
219 428cc588 Allan Hsu
220
    astream = NULL;
221
    vstream = NULL;
222 899681cd Björn Axelsson
    ioc = s->pb;
223 428cc588 Allan Hsu
224
    //first object needs to be "onMetaData" string
225 b7effd4e Anton Khirnov
    type = avio_r8(ioc);
226 cc38e063 Michael Niedermayer
    if(type != AMF_DATA_TYPE_STRING || amf_get_string(ioc, buffer, sizeof(buffer)) < 0 || strcmp(buffer, "onMetaData"))
227 428cc588 Allan Hsu
        return -1;
228
229
    //find the streams now so that amf_parse_object doesn't need to do the lookup every time it is called.
230
    for(i = 0; i < s->nb_streams; i++) {
231
        stream = s->streams[i];
232 72415b2a Stefano Sabatini
        if     (stream->codec->codec_type == AVMEDIA_TYPE_AUDIO) astream = stream;
233
        else if(stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) vstream = stream;
234 428cc588 Allan Hsu
    }
235
236
    //parse the second object (we want a mixed array)
237 cc38e063 Michael Niedermayer
    if(amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
238 428cc588 Allan Hsu
        return -1;
239
240
    return 0;
241
}
242
243 6f910bcf Michael Niedermayer
static AVStream *create_stream(AVFormatContext *s, int is_audio){
244
    AVStream *st = av_new_stream(s, is_audio);
245
    if (!st)
246
        return NULL;
247 72415b2a Stefano Sabatini
    st->codec->codec_type = is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO;
248 19719bc6 Baptiste Coudurier
    av_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
249 6f910bcf Michael Niedermayer
    return st;
250
}
251
252 d4f5d74a Garrick Meeker
static int flv_read_header(AVFormatContext *s,
253
                           AVFormatParameters *ap)
254
{
255 15f14fc7 Michael Niedermayer
    int offset, flags;
256 d4f5d74a Garrick Meeker
257 e356fc57 Anton Khirnov
    avio_seek(s->pb, 4, SEEK_CUR);
258 b7effd4e Anton Khirnov
    flags = avio_r8(s->pb);
259 031311cb Alex Beregszaszi
    /* old flvtool cleared this field */
260
    /* FIXME: better fix needed */
261
    if (!flags) {
262
        flags = FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO;
263
        av_log(s, AV_LOG_WARNING, "Broken FLV file, which says no streams present, this might fail\n");
264
    }
265 d4f5d74a Garrick Meeker
266 b41497e9 Michael Niedermayer
    if((flags & (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
267
             != (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
268
        s->ctx_flags |= AVFMTCTX_NOHEADER;
269
270 4eb0c665 Michael Niedermayer
    if(flags & FLV_HEADER_FLAG_HASVIDEO){
271 6f910bcf Michael Niedermayer
        if(!create_stream(s, 0))
272 769e10f0 Panagiotis Issaris
            return AVERROR(ENOMEM);
273 4eb0c665 Michael Niedermayer
    }
274
    if(flags & FLV_HEADER_FLAG_HASAUDIO){
275 6f910bcf Michael Niedermayer
        if(!create_stream(s, 1))
276 769e10f0 Panagiotis Issaris
            return AVERROR(ENOMEM);
277 4eb0c665 Michael Niedermayer
    }
278
279 b7effd4e Anton Khirnov
    offset = avio_rb32(s->pb);
280 6b4aa5da Anton Khirnov
    avio_seek(s->pb, offset, SEEK_SET);
281 e356fc57 Anton Khirnov
    avio_seek(s->pb, 4, SEEK_CUR);
282 d4f5d74a Garrick Meeker
283 aeb20f7f Nazo
    s->start_time = 0;
284
285 d4f5d74a Garrick Meeker
    return 0;
286
}
287
288 04fd3e81 Baptiste Coudurier
static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
289
{
290
    av_free(st->codec->extradata);
291
    st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
292
    if (!st->codec->extradata)
293
        return AVERROR(ENOMEM);
294
    st->codec->extradata_size = size;
295 b7effd4e Anton Khirnov
    avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
296 04fd3e81 Baptiste Coudurier
    return 0;
297
}
298
299 d4f5d74a Garrick Meeker
static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
300
{
301 ebd61055 Baptiste Coudurier
    FLVContext *flv = s->priv_data;
302 4fe8a452 Pascal Massimino
    int ret, i, type, size, flags, is_audio;
303
    int64_t next, pos;
304 ebd61055 Baptiste Coudurier
    int64_t dts, pts = AV_NOPTS_VALUE;
305 923bd441 Alex Beregszaszi
    AVStream *st = NULL;
306 115329f1 Diego Biurrun
307 e356fc57 Anton Khirnov
 for(;;avio_seek(s->pb, 4, SEEK_CUR)){ /* pkt size is repeated at end. skip it */
308 899681cd Björn Axelsson
    pos = url_ftell(s->pb);
309 b7effd4e Anton Khirnov
    type = avio_r8(s->pb);
310
    size = avio_rb24(s->pb);
311
    dts = avio_rb24(s->pb);
312
    dts |= avio_r8(s->pb) << 24;
313 7ef94d22 Baptiste Coudurier
//    av_log(s, AV_LOG_DEBUG, "type:%d, size:%d, dts:%d\n", type, size, dts);
314 899681cd Björn Axelsson
    if (url_feof(s->pb))
315 0e9602ef Peter Ross
        return AVERROR_EOF;
316 e356fc57 Anton Khirnov
    avio_seek(s->pb, 3, SEEK_CUR); /* stream id, always 0 */
317 d4f5d74a Garrick Meeker
    flags = 0;
318 115329f1 Diego Biurrun
319 068f2a22 Michael Niedermayer
    if(size == 0)
320
        continue;
321 115329f1 Diego Biurrun
322 899681cd Björn Axelsson
    next= size + url_ftell(s->pb);
323 dd9f5916 Michael Niedermayer
324 6cac3a3b Allan Hsu
    if (type == FLV_TAG_TYPE_AUDIO) {
325 068f2a22 Michael Niedermayer
        is_audio=1;
326 b7effd4e Anton Khirnov
        flags = avio_r8(s->pb);
327 6298eb81 Baptiste Coudurier
        size--;
328 6cac3a3b Allan Hsu
    } else if (type == FLV_TAG_TYPE_VIDEO) {
329 068f2a22 Michael Niedermayer
        is_audio=0;
330 b7effd4e Anton Khirnov
        flags = avio_r8(s->pb);
331 6298eb81 Baptiste Coudurier
        size--;
332 3d9aecb0 Baptiste Coudurier
        if ((flags & 0xf0) == 0x50) /* video info / command frame */
333
            goto skip;
334 d4f5d74a Garrick Meeker
    } else {
335 09e54e1f Aurelien Jacobs
        if (type == FLV_TAG_TYPE_META && size > 13+1+4)
336 428cc588 Allan Hsu
            flv_read_metabody(s, next);
337
        else /* skip packet */
338 e1d8d7bb Art Clarke
            av_log(s, AV_LOG_DEBUG, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
339 3d9aecb0 Baptiste Coudurier
    skip:
340 6b4aa5da Anton Khirnov
        avio_seek(s->pb, next, SEEK_SET);
341 ded01499 Michael Niedermayer
        continue;
342 d4f5d74a Garrick Meeker
    }
343
344 ae58b54b Baptiste Coudurier
    /* skip empty data packets */
345
    if (!size)
346
        continue;
347
348 d4f5d74a Garrick Meeker
    /* now find stream */
349
    for(i=0;i<s->nb_streams;i++) {
350
        st = s->streams[i];
351 068f2a22 Michael Niedermayer
        if (st->id == is_audio)
352
            break;
353 d4f5d74a Garrick Meeker
    }
354 068f2a22 Michael Niedermayer
    if(i == s->nb_streams){
355 c1023470 Benoit Fouet
        av_log(s, AV_LOG_ERROR, "invalid stream\n");
356 c8652b57 Michael Niedermayer
        st= create_stream(s, is_audio);
357 a33cfa30 Michael Niedermayer
        s->ctx_flags &= ~AVFMTCTX_NOHEADER;
358 6ed08157 Michael Niedermayer
    }
359 c1023470 Benoit Fouet
//    av_log(s, AV_LOG_DEBUG, "%d %X %d \n", is_audio, flags, st->discard);
360 6cac3a3b Allan Hsu
    if(  (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||         is_audio))
361
       ||(st->discard >= AVDISCARD_BIDIR  &&  ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))
362 f3356e9c Michael Niedermayer
       || st->discard >= AVDISCARD_ALL
363
       ){
364 6b4aa5da Anton Khirnov
        avio_seek(s->pb, next, SEEK_SET);
365 ded01499 Michael Niedermayer
        continue;
366 b9866ebc Michael Niedermayer
    }
367 6cac3a3b Allan Hsu
    if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
368 7ef94d22 Baptiste Coudurier
        av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
369 068f2a22 Michael Niedermayer
    break;
370
 }
371
372 15f14fc7 Michael Niedermayer
    // if not streamed and no duration from metadata then seek to end to find the duration from the timestamps
373 fdf46622 Baptiste Coudurier
    if(!url_is_streamed(s->pb) && (!s->duration || s->duration==AV_NOPTS_VALUE)){
374 15f14fc7 Michael Niedermayer
        int size;
375 4fe8a452 Pascal Massimino
        const int64_t pos= url_ftell(s->pb);
376
        const int64_t fsize= url_fsize(s->pb);
377 6b4aa5da Anton Khirnov
        avio_seek(s->pb, fsize-4, SEEK_SET);
378 b7effd4e Anton Khirnov
        size= avio_rb32(s->pb);
379 6b4aa5da Anton Khirnov
        avio_seek(s->pb, fsize-3-size, SEEK_SET);
380 b7effd4e Anton Khirnov
        if(size == avio_rb24(s->pb) + 11){
381
            uint32_t ts = avio_rb24(s->pb);
382
            ts |= avio_r8(s->pb) << 24;
383 b126dee9 Martin Storsjö
            s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
384 15f14fc7 Michael Niedermayer
        }
385 6b4aa5da Anton Khirnov
        avio_seek(s->pb, pos, SEEK_SET);
386 15f14fc7 Michael Niedermayer
    }
387
388 068f2a22 Michael Niedermayer
    if(is_audio){
389 2f3d7ea9 Michael Niedermayer
        if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample) {
390 6cac3a3b Allan Hsu
            st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
391 7f8cd075 Baptiste Coudurier
            st->codec->sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
392 dd1c8f3e Luca Abeni
            st->codec->bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
393 2f3d7ea9 Michael Niedermayer
        }
394
        if(!st->codec->codec_id){
395 428cc588 Allan Hsu
            flv_set_audio_codec(s, st, flags & FLV_AUDIO_CODECID_MASK);
396 068f2a22 Michael Niedermayer
        }
397
    }else{
398 428cc588 Allan Hsu
        size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);
399 bb01a3f0 Michael Niedermayer
    }
400
401 04fd3e81 Baptiste Coudurier
    if (st->codec->codec_id == CODEC_ID_AAC ||
402
        st->codec->codec_id == CODEC_ID_H264) {
403 b7effd4e Anton Khirnov
        int type = avio_r8(s->pb);
404 04fd3e81 Baptiste Coudurier
        size--;
405
        if (st->codec->codec_id == CODEC_ID_H264) {
406 b7effd4e Anton Khirnov
            int32_t cts = (avio_rb24(s->pb)+0xff800000)^0xff800000; // sign extension
407 ebd61055 Baptiste Coudurier
            pts = dts + cts;
408
            if (cts < 0) { // dts are wrong
409
                flv->wrong_dts = 1;
410
                av_log(s, AV_LOG_WARNING, "negative cts, previous timestamps might be wrong\n");
411
            }
412
            if (flv->wrong_dts)
413
                dts = AV_NOPTS_VALUE;
414 04fd3e81 Baptiste Coudurier
        }
415
        if (type == 0) {
416 6298eb81 Baptiste Coudurier
            if ((ret = flv_get_extradata(s, st, size)) < 0)
417 04fd3e81 Baptiste Coudurier
                return ret;
418 d2718187 Baptiste Coudurier
            if (st->codec->codec_id == CODEC_ID_AAC) {
419
                MPEG4AudioConfig cfg;
420
                ff_mpeg4audio_get_config(&cfg, st->codec->extradata,
421
                                         st->codec->extradata_size);
422 5aea268d Alex Converse
                st->codec->channels = cfg.channels;
423 7d6096e4 Baptiste Coudurier
                if (cfg.ext_sample_rate)
424
                    st->codec->sample_rate = cfg.ext_sample_rate;
425
                else
426
                    st->codec->sample_rate = cfg.sample_rate;
427 dfd2a005 Luca Barbato
                av_dlog(s, "mp4a config channels %d sample rate %d\n",
428 d2718187 Baptiste Coudurier
                        st->codec->channels, st->codec->sample_rate);
429
            }
430
431 527c2e64 Howard Chu
            ret = AVERROR(EAGAIN);
432
            goto leave;
433 04fd3e81 Baptiste Coudurier
        }
434
    }
435
436 fcb4228c Baptiste Coudurier
    /* skip empty data packets */
437 527c2e64 Howard Chu
    if (!size) {
438
        ret = AVERROR(EAGAIN);
439
        goto leave;
440
    }
441 fcb4228c Baptiste Coudurier
442 6298eb81 Baptiste Coudurier
    ret= av_get_packet(s->pb, pkt, size);
443 634b927f Michael Niedermayer
    if (ret < 0) {
444 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
445 d4f5d74a Garrick Meeker
    }
446
    /* note: we need to modify the packet size here to handle the last
447
       packet */
448
    pkt->size = ret;
449 7ef94d22 Baptiste Coudurier
    pkt->dts = dts;
450 ebd61055 Baptiste Coudurier
    pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
451 d4f5d74a Garrick Meeker
    pkt->stream_index = st->index;
452 115329f1 Diego Biurrun
453 6cac3a3b Allan Hsu
    if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
454 cc947f04 Jean-Daniel Dupas
        pkt->flags |= AV_PKT_FLAG_KEY;
455 115329f1 Diego Biurrun
456 527c2e64 Howard Chu
leave:
457 e356fc57 Anton Khirnov
    avio_seek(s->pb, 4, SEEK_CUR);
458 d4f5d74a Garrick Meeker
    return ret;
459
}
460
461 fc8fa007 Howard Chu
static int flv_read_seek(AVFormatContext *s, int stream_index,
462
    int64_t ts, int flags)
463
{
464
    return av_url_read_fseek(s->pb, stream_index, ts, flags);
465
}
466
467
#if 0 /* don't know enough to implement this */
468
static int flv_read_seek2(AVFormatContext *s, int stream_index,
469
    int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
470
{
471
    int ret = AVERROR(ENOSYS);
472

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

475
    if (url_is_streamed(s->pb)) {
476
        if (stream_index < 0) {
477
            stream_index = av_find_default_stream_index(s);
478
            if (stream_index < 0)
479
                return -1;
480

481
            /* timestamp for default must be expressed in AV_TIME_BASE units */
482
            ts = av_rescale_rnd(ts, 1000, AV_TIME_BASE,
483
                flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
484
        }
485
        ret = av_url_read_fseek(s->pb, stream_index, ts, flags);
486
    }
487

488
    if (ret == AVERROR(ENOSYS))
489
        ret = av_seek_frame(s, stream_index, ts, flags);
490
    return ret;
491
}
492
#endif
493
494 c6610a21 Diego Elio Pettenò
AVInputFormat ff_flv_demuxer = {
495 d4f5d74a Garrick Meeker
    "flv",
496 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("FLV format"),
497 ebd61055 Baptiste Coudurier
    sizeof(FLVContext),
498 d4f5d74a Garrick Meeker
    flv_probe,
499
    flv_read_header,
500
    flv_read_packet,
501 fc8fa007 Howard Chu
    .read_seek = flv_read_seek,
502
#if 0
503
    .read_seek2 = flv_read_seek2,
504
#endif
505 d4f5d74a Garrick Meeker
    .extensions = "flv",
506
    .value = CODEC_ID_FLV1,
507
};