Merge remote-tracking branch 'libav/master'
[FFMpeg-mirror/mplayer-patches.git] / libavformat / siff.c
blob8ba7c6007f328eaa80d1466b27126087afab3331
1 /*
2 * Beam Software SIFF demuxer
3 * Copyright (c) 2007 Konstantin Shishkov
5 * This file is part of Libav.
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.
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.
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
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/intreadwrite.h"
24 #include "avformat.h"
25 #include "internal.h"
27 enum SIFFTags{
28 TAG_SIFF = MKTAG('S', 'I', 'F', 'F'),
29 TAG_BODY = MKTAG('B', 'O', 'D', 'Y'),
30 TAG_VBHD = MKTAG('V', 'B', 'H', 'D'),
31 TAG_SHDR = MKTAG('S', 'H', 'D', 'R'),
32 TAG_VBV1 = MKTAG('V', 'B', 'V', '1'),
33 TAG_SOUN = MKTAG('S', 'O', 'U', 'N'),
36 enum VBFlags{
37 VB_HAS_GMC = 0x01,
38 VB_HAS_AUDIO = 0x04,
39 VB_HAS_VIDEO = 0x08,
40 VB_HAS_PALETTE = 0x10,
41 VB_HAS_LENGTH = 0x20
44 typedef struct SIFFContext{
45 int frames;
46 int cur_frame;
47 int rate;
48 int bits;
49 int block_align;
51 int has_video;
52 int has_audio;
54 int curstrm;
55 int pktsize;
56 int gmcsize;
57 int sndsize;
59 int flags;
60 uint8_t gmc[4];
61 }SIFFContext;
63 static int siff_probe(AVProbeData *p)
65 uint32_t tag = AV_RL32(p->buf + 8);
66 /* check file header */
67 if (AV_RL32(p->buf) != TAG_SIFF ||
68 (tag != TAG_VBV1 && tag != TAG_SOUN))
69 return 0;
70 return AVPROBE_SCORE_MAX;
73 static int create_audio_stream(AVFormatContext *s, SIFFContext *c)
75 AVStream *ast;
76 ast = avformat_new_stream(s, NULL);
77 if (!ast)
78 return -1;
79 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
80 ast->codec->codec_id = AV_CODEC_ID_PCM_U8;
81 ast->codec->channels = 1;
82 ast->codec->channel_layout = AV_CH_LAYOUT_MONO;
83 ast->codec->bits_per_coded_sample = 8;
84 ast->codec->sample_rate = c->rate;
85 avpriv_set_pts_info(ast, 16, 1, c->rate);
86 ast->start_time = 0;
87 return 0;
90 static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
92 AVStream *st;
93 int width, height;
95 if (avio_rl32(pb) != TAG_VBHD){
96 av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
97 return -1;
99 if(avio_rb32(pb) != 32){
100 av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
101 return -1;
103 if(avio_rl16(pb) != 1){
104 av_log(s, AV_LOG_ERROR, "Incorrect header version\n");
105 return -1;
107 width = avio_rl16(pb);
108 height = avio_rl16(pb);
109 avio_skip(pb, 4);
110 c->frames = avio_rl16(pb);
111 if(!c->frames){
112 av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
113 return -1;
115 c->bits = avio_rl16(pb);
116 c->rate = avio_rl16(pb);
117 c->block_align = c->rate * (c->bits >> 3);
119 avio_skip(pb, 16); //zeroes
121 st = avformat_new_stream(s, NULL);
122 if (!st)
123 return -1;
124 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
125 st->codec->codec_id = AV_CODEC_ID_VB;
126 st->codec->codec_tag = MKTAG('V', 'B', 'V', '1');
127 st->codec->width = width;
128 st->codec->height = height;
129 st->codec->pix_fmt = AV_PIX_FMT_PAL8;
130 avpriv_set_pts_info(st, 16, 1, 12);
132 c->cur_frame = 0;
133 c->has_video = 1;
134 c->has_audio = !!c->rate;
135 c->curstrm = -1;
136 if (c->has_audio && create_audio_stream(s, c) < 0)
137 return -1;
138 return 0;
141 static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
143 if (avio_rl32(pb) != TAG_SHDR){
144 av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
145 return -1;
147 if(avio_rb32(pb) != 8){
148 av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
149 return -1;
151 avio_skip(pb, 4); //unknown value
152 c->rate = avio_rl16(pb);
153 c->bits = avio_rl16(pb);
154 c->block_align = c->rate * (c->bits >> 3);
155 return create_audio_stream(s, c);
158 static int siff_read_header(AVFormatContext *s)
160 AVIOContext *pb = s->pb;
161 SIFFContext *c = s->priv_data;
162 uint32_t tag;
164 if (avio_rl32(pb) != TAG_SIFF)
165 return -1;
166 avio_skip(pb, 4); //ignore size
167 tag = avio_rl32(pb);
169 if (tag != TAG_VBV1 && tag != TAG_SOUN){
170 av_log(s, AV_LOG_ERROR, "Not a VBV file\n");
171 return -1;
174 if (tag == TAG_VBV1 && siff_parse_vbv1(s, c, pb) < 0)
175 return -1;
176 if (tag == TAG_SOUN && siff_parse_soun(s, c, pb) < 0)
177 return -1;
178 if (avio_rl32(pb) != MKTAG('B', 'O', 'D', 'Y')){
179 av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
180 return -1;
182 avio_skip(pb, 4); //ignore size
184 return 0;
187 static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
189 SIFFContext *c = s->priv_data;
190 int size;
192 if (c->has_video){
193 if (c->cur_frame >= c->frames)
194 return AVERROR(EIO);
195 if (c->curstrm == -1){
196 c->pktsize = avio_rl32(s->pb) - 4;
197 c->flags = avio_rl16(s->pb);
198 c->gmcsize = (c->flags & VB_HAS_GMC) ? 4 : 0;
199 if (c->gmcsize)
200 avio_read(s->pb, c->gmc, c->gmcsize);
201 c->sndsize = (c->flags & VB_HAS_AUDIO) ? avio_rl32(s->pb): 0;
202 c->curstrm = !!(c->flags & VB_HAS_AUDIO);
205 if (!c->curstrm){
206 size = c->pktsize - c->sndsize;
207 if (av_new_packet(pkt, size) < 0)
208 return AVERROR(ENOMEM);
209 AV_WL16(pkt->data, c->flags);
210 if (c->gmcsize)
211 memcpy(pkt->data + 2, c->gmc, c->gmcsize);
212 avio_read(s->pb, pkt->data + 2 + c->gmcsize, size - c->gmcsize - 2);
213 pkt->stream_index = 0;
214 c->curstrm = -1;
215 }else{
216 if ((size = av_get_packet(s->pb, pkt, c->sndsize - 4)) < 0)
217 return AVERROR(EIO);
218 pkt->stream_index = 1;
219 pkt->duration = size;
220 c->curstrm = 0;
222 if(!c->cur_frame || c->curstrm)
223 pkt->flags |= AV_PKT_FLAG_KEY;
224 if (c->curstrm == -1)
225 c->cur_frame++;
226 }else{
227 size = av_get_packet(s->pb, pkt, c->block_align);
228 if(size <= 0)
229 return AVERROR(EIO);
230 pkt->duration = size;
232 return pkt->size;
235 AVInputFormat ff_siff_demuxer = {
236 .name = "siff",
237 .long_name = NULL_IF_CONFIG_SMALL("Beam Software SIFF"),
238 .priv_data_size = sizeof(SIFFContext),
239 .read_probe = siff_probe,
240 .read_header = siff_read_header,
241 .read_packet = siff_read_packet,
242 .extensions = "vb,son",