Statistics
| Branch: | Revision:

ffmpeg / libavformat / flvdec.c @ 5a757507

History | View | Annotate | Download (4.49 KB)

1
/*
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
#define FRAME_RATE_UNKNOWN 31415 //yes i know its beatifull
44

    
45
static int flv_read_header(AVFormatContext *s,
46
                           AVFormatParameters *ap)
47
{
48
    int offset, flags;
49
    AVStream *st;
50
    
51
    s->ctx_flags |= AVFMTCTX_NOHEADER; //ok we have a header but theres no fps, codec type, sample_rate, ...
52

    
53
    av_set_pts_info(s, 24, 1, 1000); /* 24 bit pts in ms */
54

    
55
    url_fskip(&s->pb, 4);
56
    flags = get_byte(&s->pb);
57

    
58
    if ((flags & 1)) {
59
        st = av_new_stream(s, 0);
60
        if (!st)
61
            return AVERROR_NOMEM;
62
        st->codec.codec_type = CODEC_TYPE_VIDEO;
63
        st->codec.codec_id = CODEC_ID_FLV1;
64
        st->codec.frame_rate= FRAME_RATE_UNKNOWN;
65
//        st->codec.frame_rate= ap->frame_rate;
66
//        st->codec.frame_rate_base= ap->frame_rate_base;
67
    }
68

    
69
    if ((flags & 4)) {
70
        st = av_new_stream(s, 1);
71
        if (!st)
72
            return AVERROR_NOMEM;
73
        st->codec.codec_type = CODEC_TYPE_AUDIO;
74
        st->codec.codec_id = CODEC_ID_MP3;
75
    }
76

    
77
    offset = get_be32(&s->pb);
78
    url_fseek(&s->pb, offset, SEEK_SET);
79

    
80
    return 0;
81
}
82

    
83
static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
84
{
85
    int ret, i, type, size, pts, flags;
86
    AVStream *st;
87
    
88
 redo:
89
    url_fskip(&s->pb, 4); /* size of previous packet */
90
    type = get_byte(&s->pb);
91
    size = get_be24(&s->pb);
92
    pts = get_be24(&s->pb);
93
//    av_log(s, AV_LOG_DEBUG, "type:%d, size:%d, pts:%d\n", type, size, pts);
94
    if (url_feof(&s->pb))
95
        return -EIO;
96
    url_fskip(&s->pb, 4); /* reserved */
97
    flags = 0;
98
    if (type == 8) {
99
        flags = get_byte(&s->pb);
100
        size--;
101
        if ((flags >> 4) != 2) { /* 0: uncompressed 1: ADPCM 2: mp3 5: Nellymoser 8kHz mono 6: Nellymoser*/
102
            goto skip;
103
        }
104
    } else if (type == 9) {
105
        flags = get_byte(&s->pb);
106
        size--;
107
        if ((flags & 0xF) != 2) { /* 2: only format */
108
            goto skip;
109
        }
110
    } else {
111
    skip:
112
        /* skip packet */
113
        av_log(s, AV_LOG_ERROR, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
114
        url_fskip(&s->pb, size);
115
        goto redo;
116
    }
117

    
118
    /* now find stream */
119
    for(i=0;i<s->nb_streams;i++) {
120
        st = s->streams[i];
121
        if (st->id == ((type == 9) ? 0 : 1))
122
            goto found;
123
    }
124
    goto skip;
125
 found:
126
    if(type == 8 && st->codec.sample_rate == 0){
127
        st->codec.channels = (flags&1)+1;
128
        st->codec.sample_rate = (44100<<((flags>>2)&3))>>3;
129
    }
130

    
131
    //guess the frame rate
132
    if(type==9 && st->codec.frame_rate == FRAME_RATE_UNKNOWN && pts){
133
        st->codec.frame_rate_base=1;
134
        st->codec.frame_rate= (1000 + pts/2)/pts;
135
    }
136

    
137
    if (av_new_packet(pkt, size) < 0)
138
        return -EIO;
139

    
140
    ret = get_buffer(&s->pb, pkt->data, size);
141
    if (ret <= 0) {
142
        av_free_packet(pkt);
143
        return -EIO;
144
    }
145
    /* note: we need to modify the packet size here to handle the last
146
       packet */
147
    pkt->size = ret;
148
    pkt->pts = pts;
149
    pkt->stream_index = st->index;
150
    return ret;
151
}
152

    
153
static int flv_read_close(AVFormatContext *s)
154
{
155
    return 0;
156
}
157

    
158
AVInputFormat flv_iformat = {
159
    "flv",
160
    "flv format",
161
    0,
162
    flv_probe,
163
    flv_read_header,
164
    flv_read_packet,
165
    flv_read_close,
166
    .extensions = "flv",
167
    .value = CODEC_ID_FLV1,
168
};
169

    
170
int flvdec_init(void)
171
{
172
    av_register_input_format(&flv_iformat);
173
    return 0;
174
}