]> git.sesse.net Git - ffmpeg/blob - libavformat/siff.c
siff: K&R formatting cosmetics
[ffmpeg] / libavformat / siff.c
1 /*
2  * Beam Software SIFF demuxer
3  * Copyright (c) 2007 Konstantin Shishkov
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/intreadwrite.h"
24
25 #include "avformat.h"
26 #include "internal.h"
27
28 enum SIFFTags {
29     TAG_SIFF = MKTAG('S', 'I', 'F', 'F'),
30     TAG_BODY = MKTAG('B', 'O', 'D', 'Y'),
31     TAG_VBHD = MKTAG('V', 'B', 'H', 'D'),
32     TAG_SHDR = MKTAG('S', 'H', 'D', 'R'),
33     TAG_VBV1 = MKTAG('V', 'B', 'V', '1'),
34     TAG_SOUN = MKTAG('S', 'O', 'U', 'N'),
35 };
36
37 enum VBFlags {
38     VB_HAS_GMC     = 0x01,
39     VB_HAS_AUDIO   = 0x04,
40     VB_HAS_VIDEO   = 0x08,
41     VB_HAS_PALETTE = 0x10,
42     VB_HAS_LENGTH  = 0x20
43 };
44
45 typedef struct SIFFContext {
46     int frames;
47     int cur_frame;
48     int rate;
49     int bits;
50     int block_align;
51
52     int has_video;
53     int has_audio;
54
55     int curstrm;
56     int pktsize;
57     int gmcsize;
58     int sndsize;
59
60     int flags;
61     uint8_t gmc[4];
62 } SIFFContext;
63
64 static int siff_probe(AVProbeData *p)
65 {
66     uint32_t tag = AV_RL32(p->buf + 8);
67     /* check file header */
68     if (AV_RL32(p->buf) != TAG_SIFF ||
69         (tag != TAG_VBV1 && tag != TAG_SOUN))
70         return 0;
71     return AVPROBE_SCORE_MAX;
72 }
73
74 static int create_audio_stream(AVFormatContext *s, SIFFContext *c)
75 {
76     AVStream *ast;
77     ast = avformat_new_stream(s, NULL);
78     if (!ast)
79         return -1;
80     ast->codec->codec_type            = AVMEDIA_TYPE_AUDIO;
81     ast->codec->codec_id              = AV_CODEC_ID_PCM_U8;
82     ast->codec->channels              = 1;
83     ast->codec->channel_layout        = AV_CH_LAYOUT_MONO;
84     ast->codec->bits_per_coded_sample = 8;
85     ast->codec->sample_rate           = c->rate;
86     avpriv_set_pts_info(ast, 16, 1, c->rate);
87     ast->start_time                   = 0;
88     return 0;
89 }
90
91 static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
92 {
93     AVStream *st;
94     int width, height;
95
96     if (avio_rl32(pb) != TAG_VBHD) {
97         av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
98         return -1;
99     }
100     if (avio_rb32(pb) != 32) {
101         av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
102         return -1;
103     }
104     if (avio_rl16(pb) != 1) {
105         av_log(s, AV_LOG_ERROR, "Incorrect header version\n");
106         return -1;
107     }
108     width  = avio_rl16(pb);
109     height = avio_rl16(pb);
110     avio_skip(pb, 4);
111     c->frames = avio_rl16(pb);
112     if (!c->frames) {
113         av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
114         return -1;
115     }
116     c->bits        = avio_rl16(pb);
117     c->rate        = avio_rl16(pb);
118     c->block_align = c->rate * (c->bits >> 3);
119
120     avio_skip(pb, 16); // zeroes
121
122     st = avformat_new_stream(s, NULL);
123     if (!st)
124         return -1;
125     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
126     st->codec->codec_id   = AV_CODEC_ID_VB;
127     st->codec->codec_tag  = MKTAG('V', 'B', 'V', '1');
128     st->codec->width      = width;
129     st->codec->height     = height;
130     st->codec->pix_fmt    = AV_PIX_FMT_PAL8;
131     avpriv_set_pts_info(st, 16, 1, 12);
132
133     c->cur_frame = 0;
134     c->has_video = 1;
135     c->has_audio = !!c->rate;
136     c->curstrm   = -1;
137     if (c->has_audio && create_audio_stream(s, c) < 0)
138         return -1;
139     return 0;
140 }
141
142 static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
143 {
144     if (avio_rl32(pb) != TAG_SHDR) {
145         av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
146         return -1;
147     }
148     if (avio_rb32(pb) != 8) {
149         av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
150         return -1;
151     }
152     avio_skip(pb, 4); // unknown value
153     c->rate        = avio_rl16(pb);
154     c->bits        = avio_rl16(pb);
155     c->block_align = c->rate * (c->bits >> 3);
156     return create_audio_stream(s, c);
157 }
158
159 static int siff_read_header(AVFormatContext *s)
160 {
161     AVIOContext *pb = s->pb;
162     SIFFContext *c  = s->priv_data;
163     uint32_t tag;
164
165     if (avio_rl32(pb) != TAG_SIFF)
166         return -1;
167     avio_skip(pb, 4); // ignore size
168     tag = avio_rl32(pb);
169
170     if (tag != TAG_VBV1 && tag != TAG_SOUN) {
171         av_log(s, AV_LOG_ERROR, "Not a VBV file\n");
172         return -1;
173     }
174
175     if (tag == TAG_VBV1 && siff_parse_vbv1(s, c, pb) < 0)
176         return -1;
177     if (tag == TAG_SOUN && siff_parse_soun(s, c, pb) < 0)
178         return -1;
179     if (avio_rl32(pb) != MKTAG('B', 'O', 'D', 'Y')) {
180         av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
181         return -1;
182     }
183     avio_skip(pb, 4); // ignore size
184
185     return 0;
186 }
187
188 static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
189 {
190     SIFFContext *c = s->priv_data;
191     int size;
192
193     if (c->has_video) {
194         if (c->cur_frame >= c->frames)
195             return AVERROR(EIO);
196         if (c->curstrm == -1) {
197             c->pktsize = avio_rl32(s->pb) - 4;
198             c->flags   = avio_rl16(s->pb);
199             c->gmcsize = (c->flags & VB_HAS_GMC) ? 4 : 0;
200             if (c->gmcsize)
201                 avio_read(s->pb, c->gmc, c->gmcsize);
202             c->sndsize = (c->flags & VB_HAS_AUDIO) ? avio_rl32(s->pb) : 0;
203             c->curstrm = !!(c->flags & VB_HAS_AUDIO);
204         }
205
206         if (!c->curstrm) {
207             size = c->pktsize - c->sndsize;
208             if (av_new_packet(pkt, size) < 0)
209                 return AVERROR(ENOMEM);
210             AV_WL16(pkt->data, c->flags);
211             if (c->gmcsize)
212                 memcpy(pkt->data + 2, c->gmc, c->gmcsize);
213             avio_read(s->pb, pkt->data + 2 + c->gmcsize, size - c->gmcsize - 2);
214             pkt->stream_index = 0;
215             c->curstrm        = -1;
216         } else {
217             if ((size = av_get_packet(s->pb, pkt, c->sndsize - 4)) < 0)
218                 return AVERROR(EIO);
219             pkt->stream_index = 1;
220             pkt->duration     = size;
221             c->curstrm        = 0;
222         }
223         if (!c->cur_frame || c->curstrm)
224             pkt->flags |= AV_PKT_FLAG_KEY;
225         if (c->curstrm == -1)
226             c->cur_frame++;
227     } else {
228         size = av_get_packet(s->pb, pkt, c->block_align);
229         if (size <= 0)
230             return AVERROR(EIO);
231         pkt->duration = size;
232     }
233     return pkt->size;
234 }
235
236 AVInputFormat ff_siff_demuxer = {
237     .name           = "siff",
238     .long_name      = NULL_IF_CONFIG_SMALL("Beam Software SIFF"),
239     .priv_data_size = sizeof(SIFFContext),
240     .read_probe     = siff_probe,
241     .read_header    = siff_read_header,
242     .read_packet    = siff_read_packet,
243     .extensions     = "vb,son",
244 };