av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
[libav.git] / libavformat / flvdec.c
CommitLineData
d4f5d74a
GM
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
21unsigned 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
30static 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
43static int flv_read_header(AVFormatContext *s,
44 AVFormatParameters *ap)
45{
46 int offset, flags;
47 AVStream *st;
48
49 av_set_pts_info(s, 24, 1, 1000); /* 24 bit pts in ms */
50
51 url_fskip(&s->pb, 4);
52 flags = get_byte(&s->pb);
53
54 if ((flags & 1)) {
55 st = av_new_stream(s, 0);
56 if (!st)
57 return AVERROR_NOMEM;
58 st->codec.codec_type = CODEC_TYPE_VIDEO;
59 st->codec.codec_id = CODEC_ID_FLV1;
60 }
61
62 if ((flags & 4)) {
63 st = av_new_stream(s, 1);
64 if (!st)
65 return AVERROR_NOMEM;
66 st->codec.codec_type = CODEC_TYPE_AUDIO;
80783dc2 67 st->codec.codec_id = CODEC_ID_MP3;
d4f5d74a
GM
68 }
69
70 offset = get_be32(&s->pb);
71 url_fseek(&s->pb, offset, SEEK_SET);
72
73 return 0;
74}
75
76static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
77{
78 int ret, i, type, size, pts, flags;
79 AVStream *st;
80
81 redo:
82 url_fskip(&s->pb, 4); /* size of previous packet */
83 type = get_byte(&s->pb);
84 size = get_be24(&s->pb);
85 pts = get_be24(&s->pb);
86 if (url_feof(&s->pb))
87 return -EIO;
88 url_fskip(&s->pb, 4); /* reserved */
89 flags = 0;
90 if (type == 8) {
91 flags = get_byte(&s->pb);
92 size--;
93 if ((flags >> 4) != 2) { /* 0: uncompressed 1: ADPCM 2: mp3 5: Nellymoser 8kHz mono 6: Nellymoser*/
94 goto skip;
95 }
96 } else if (type == 9) {
97 flags = get_byte(&s->pb);
98 size--;
99 if ((flags & 0xF) != 2) { /* 2: only format */
100 goto skip;
101 }
102 } else {
103 skip:
104 /* skip packet */
bc874dae 105 av_log(s, AV_LOG_ERROR, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
d4f5d74a
GM
106 url_fskip(&s->pb, size);
107 goto redo;
108 }
109
110 /* now find stream */
111 for(i=0;i<s->nb_streams;i++) {
112 st = s->streams[i];
113 if (st->id == ((type == 9) ? 0 : 1))
114 goto found;
115 }
116 goto skip;
117 found:
118
119 if (av_new_packet(pkt, size) < 0)
120 return -EIO;
121
122 ret = get_buffer(&s->pb, pkt->data, size);
123 if (ret <= 0) {
124 av_free_packet(pkt);
125 return -EIO;
126 }
127 /* note: we need to modify the packet size here to handle the last
128 packet */
129 pkt->size = ret;
130 pkt->pts = pts;
131 pkt->stream_index = st->index;
132 return ret;
133}
134
135static int flv_read_close(AVFormatContext *s)
136{
137 return 0;
138}
139
140AVInputFormat flv_iformat = {
141 "flv",
142 "flv format",
143 0,
144 flv_probe,
145 flv_read_header,
146 flv_read_packet,
147 flv_read_close,
148 .extensions = "flv",
149 .value = CODEC_ID_FLV1,
150};
151
152int flvdec_init(void)
153{
154 av_register_input_format(&flv_iformat);
155 return 0;
156}