Statistics
| Branch: | Revision:

ffmpeg / libavformat / flvdec.c @ 27d5f18f

History | View | Annotate | Download (4.67 KB)

1 d4f5d74a Garrick Meeker
/*
2
 * FLV encoder.
3
 * Copyright (c) 2003 The FFmpeg Project.
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
#include "avformat.h"
20
21
unsigned int get_be24(ByteIOContext *s)
22
{
23
    unsigned int val;
24
    val = get_byte(s) << 16;
25
    val |= get_byte(s) << 8;
26
    val |= get_byte(s);
27
    return val;
28
}
29
30
static int flv_probe(AVProbeData *p)
31
{
32
    const uint8_t *d;
33
34
    if (p->buf_size < 6)
35
        return 0;
36
    d = p->buf;
37
    if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V') {
38
        return 50;
39
    }
40
    return 0;
41
}
42
43
static int flv_read_header(AVFormatContext *s,
44
                           AVFormatParameters *ap)
45
{
46
    int offset, flags;
47
    AVStream *st;
48 bb01a3f0 Michael Niedermayer
    
49
    s->ctx_flags |= AVFMTCTX_NOHEADER; //ok we have a header but theres no fps, codec type, sample_rate, ...
50 d4f5d74a Garrick Meeker
51
    url_fskip(&s->pb, 4);
52
    flags = get_byte(&s->pb);
53
54
    offset = get_be32(&s->pb);
55
    url_fseek(&s->pb, offset, SEEK_SET);
56
57
    return 0;
58
}
59
60
static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
61
{
62 068f2a22 Michael Niedermayer
    int ret, i, type, size, pts, flags, is_audio;
63 d4f5d74a Garrick Meeker
    AVStream *st;
64
    
65 068f2a22 Michael Niedermayer
 for(;;){
66 d4f5d74a Garrick Meeker
    url_fskip(&s->pb, 4); /* size of previous packet */
67
    type = get_byte(&s->pb);
68
    size = get_be24(&s->pb);
69
    pts = get_be24(&s->pb);
70 92a26775 Michael Niedermayer
//    av_log(s, AV_LOG_DEBUG, "type:%d, size:%d, pts:%d\n", type, size, pts);
71 d4f5d74a Garrick Meeker
    if (url_feof(&s->pb))
72 0bd586c5 Mike Melanson
        return AVERROR_IO;
73 d4f5d74a Garrick Meeker
    url_fskip(&s->pb, 4); /* reserved */
74
    flags = 0;
75 068f2a22 Michael Niedermayer
    
76
    if(size == 0)
77
        continue;
78
    
79 d4f5d74a Garrick Meeker
    if (type == 8) {
80 068f2a22 Michael Niedermayer
        is_audio=1;
81 d4f5d74a Garrick Meeker
        flags = get_byte(&s->pb);
82
        size--;
83
    } else if (type == 9) {
84 068f2a22 Michael Niedermayer
        is_audio=0;
85 d4f5d74a Garrick Meeker
        flags = get_byte(&s->pb);
86
        size--;
87
    } else {
88
        /* skip packet */
89 bc874dae Michel Bardiaux
        av_log(s, AV_LOG_ERROR, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
90 d4f5d74a Garrick Meeker
        url_fskip(&s->pb, size);
91 068f2a22 Michael Niedermayer
        continue;
92 d4f5d74a Garrick Meeker
    }
93
94
    /* now find stream */
95
    for(i=0;i<s->nb_streams;i++) {
96
        st = s->streams[i];
97 068f2a22 Michael Niedermayer
        if (st->id == is_audio)
98
            break;
99 d4f5d74a Garrick Meeker
    }
100 068f2a22 Michael Niedermayer
    if(i == s->nb_streams){
101
        st = av_new_stream(s, is_audio);
102
        if (!st)
103
            return AVERROR_NOMEM;
104 9ee91c2f Michael Niedermayer
105
        av_set_pts_info(st, 24, 1, 1000); /* 24 bit pts in ms */
106 068f2a22 Michael Niedermayer
        st->codec.frame_rate_base= 0;
107 6ed08157 Michael Niedermayer
    }
108 068f2a22 Michael Niedermayer
    break;
109
 }
110
111
    if(is_audio){
112
        if(st->codec.sample_rate == 0){
113
            st->codec.codec_type = CODEC_TYPE_AUDIO;
114
            st->codec.channels = (flags&1)+1;
115
            if((flags >> 4) == 5)
116
                st->codec.sample_rate= 8000;
117
            else
118
                st->codec.sample_rate = (44100<<((flags>>2)&3))>>3;
119
            switch(flags >> 4){/* 0: uncompressed 1: ADPCM 2: mp3 5: Nellymoser 8kHz mono 6: Nellymoser*/
120
            case 2: st->codec.codec_id = CODEC_ID_MP3; break;
121
            default:
122
                st->codec.codec_tag= (flags >> 4);
123
            }
124
        }
125
    }else{
126
        if(st->codec.frame_rate_base == 0){
127
            st->codec.codec_type = CODEC_TYPE_VIDEO;
128
            //guess the frame rate
129
            if(pts){
130
                st->codec.frame_rate_base=1;
131
                st->codec.frame_rate= (1000 + pts/2)/pts;
132
            }
133
            switch(flags & 0xF){
134
            case 2: st->codec.codec_id = CODEC_ID_FLV1; break;
135
            default:
136
                st->codec.codec_tag= flags & 0xF;
137
            }
138
        }
139 bb01a3f0 Michael Niedermayer
    }
140
141 d4f5d74a Garrick Meeker
    if (av_new_packet(pkt, size) < 0)
142 0bd586c5 Mike Melanson
        return AVERROR_IO;
143 d4f5d74a Garrick Meeker
144
    ret = get_buffer(&s->pb, pkt->data, size);
145
    if (ret <= 0) {
146
        av_free_packet(pkt);
147 0bd586c5 Mike Melanson
        return AVERROR_IO;
148 d4f5d74a Garrick Meeker
    }
149
    /* note: we need to modify the packet size here to handle the last
150
       packet */
151
    pkt->size = ret;
152
    pkt->pts = pts;
153
    pkt->stream_index = st->index;
154 d08f4bcb Alex Beregszaszi
    
155
    if (!is_audio && ((flags >> 4)==1))
156
        pkt->flags |= PKT_FLAG_KEY;
157
    
158 d4f5d74a Garrick Meeker
    return ret;
159
}
160
161
static int flv_read_close(AVFormatContext *s)
162
{
163
    return 0;
164
}
165
166
AVInputFormat flv_iformat = {
167
    "flv",
168
    "flv format",
169
    0,
170
    flv_probe,
171
    flv_read_header,
172
    flv_read_packet,
173
    flv_read_close,
174
    .extensions = "flv",
175
    .value = CODEC_ID_FLV1,
176
};
177
178
int flvdec_init(void)
179
{
180
    av_register_input_format(&flv_iformat);
181
    return 0;
182
}