]> git.sesse.net Git - ffmpeg/blob - libavformat/avs.c
684860b11a9add492cd08df6bc244d7ff1148f86
[ffmpeg] / libavformat / avs.c
1 /*
2  * AVS demuxer.
3  * Copyright (c) 2006  Aurelien Jacobs <aurel@gnuage.org>
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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 #include "avformat.h"
21 #include "voc.h"
22
23
24 typedef struct avs_format {
25     voc_dec_context_t voc;
26     AVStream *st_video;
27     AVStream *st_audio;
28     int width;
29     int height;
30     int bits_per_sample;
31     int fps;
32     int nb_frames;
33     int remaining_frame_size;
34     int remaining_audio_size;
35 } avs_format_t;
36
37 typedef enum avs_block_type {
38     AVS_VIDEO     = 0x01,
39     AVS_AUDIO     = 0x02,
40     AVS_PALETTE   = 0x03,
41     AVS_GAME_DATA = 0x04,
42 } avs_block_type_t;
43
44
45 #ifdef CONFIG_DEMUXERS
46
47 static int avs_probe(AVProbeData * p)
48 {
49     const uint8_t *d;
50
51     if (p->buf_size < 2)
52         return 0;
53     d = p->buf;
54     if (d[0] == 'w' && d[1] == 'W' && d[2] == 0x10 && d[3] == 0)
55         return 50;
56
57     return 0;
58 }
59
60 static int avs_read_header(AVFormatContext * s, AVFormatParameters * ap)
61 {
62     avs_format_t *avs = s->priv_data;
63
64     s->ctx_flags |= AVFMTCTX_NOHEADER;
65
66     url_fskip(&s->pb, 4);
67     avs->width = get_le16(&s->pb);
68     avs->height = get_le16(&s->pb);
69     avs->bits_per_sample = get_le16(&s->pb);
70     avs->fps = get_le16(&s->pb);
71     avs->nb_frames = get_le32(&s->pb);
72     avs->remaining_frame_size = 0;
73     avs->remaining_audio_size = 0;
74
75     avs->st_video = avs->st_audio = NULL;
76
77     if (avs->width != 318 || avs->height != 198)
78         av_log(s, AV_LOG_ERROR, "This avs pretend to be %dx%d "
79                "when the avs format is supposed to be 318x198 only.\n",
80                avs->width, avs->height);
81
82     return 0;
83 }
84
85 static int
86 avs_read_video_packet(AVFormatContext * s, AVPacket * pkt,
87                       avs_block_type_t type, int sub_type, int size,
88                       uint8_t * palette, int palette_size)
89 {
90     avs_format_t *avs = s->priv_data;
91     int ret;
92
93     ret = av_new_packet(pkt, size + palette_size);
94     if (ret < 0)
95         return ret;
96
97     if (palette_size) {
98         pkt->data[0] = 0x00;
99         pkt->data[1] = 0x03;
100         pkt->data[2] = palette_size & 0xFF;
101         pkt->data[3] = (palette_size >> 8) & 0xFF;
102         memcpy(pkt->data + 4, palette, palette_size - 4);
103     }
104
105     pkt->data[palette_size + 0] = sub_type;
106     pkt->data[palette_size + 1] = type;
107     pkt->data[palette_size + 2] = size & 0xFF;
108     pkt->data[palette_size + 3] = (size >> 8) & 0xFF;
109     ret = get_buffer(&s->pb, pkt->data + palette_size + 4, size - 4) + 4;
110     if (ret < size) {
111         av_free_packet(pkt);
112         return AVERROR_IO;
113     }
114
115     pkt->size = ret + palette_size;
116     pkt->stream_index = avs->st_video->index;
117     if (sub_type == 0)
118         pkt->flags |= PKT_FLAG_KEY;
119
120     return 0;
121 }
122
123 static int avs_read_audio_packet(AVFormatContext * s, AVPacket * pkt)
124 {
125     avs_format_t *avs = s->priv_data;
126     int ret, size;
127
128     size = url_ftell(&s->pb);
129     ret = voc_get_packet(s, pkt, avs->st_audio, avs->remaining_audio_size);
130     size = url_ftell(&s->pb) - size;
131     avs->remaining_audio_size -= size;
132
133     if (ret == AVERROR_IO)
134         return 0;    /* this indicate EOS */
135     if (ret < 0)
136         return ret;
137
138     pkt->stream_index = avs->st_audio->index;
139     pkt->flags |= PKT_FLAG_KEY;
140
141     return size;
142 }
143
144 static int avs_read_packet(AVFormatContext * s, AVPacket * pkt)
145 {
146     avs_format_t *avs = s->priv_data;
147     int sub_type = 0, size = 0;
148     avs_block_type_t type = 0;
149     int palette_size = 0;
150     uint8_t palette[4 + 3 * 256];
151     int ret;
152
153     if (avs->remaining_audio_size > 0)
154         if (avs_read_audio_packet(s, pkt) > 0)
155             return 0;
156
157     while (1) {
158         if (avs->remaining_frame_size <= 0) {
159             if (!get_le16(&s->pb))    /* found EOF */
160                 return AVERROR_IO;
161             avs->remaining_frame_size = get_le16(&s->pb) - 4;
162         }
163
164         while (avs->remaining_frame_size > 0) {
165             sub_type = get_byte(&s->pb);
166             type = get_byte(&s->pb);
167             size = get_le16(&s->pb);
168             avs->remaining_frame_size -= size;
169
170             switch (type) {
171             case AVS_PALETTE:
172                 ret = get_buffer(&s->pb, palette, size - 4);
173                 if (ret < size - 4)
174                     return AVERROR_IO;
175                 palette_size = size;
176                 break;
177
178             case AVS_VIDEO:
179                 if (!avs->st_video) {
180                     avs->st_video = av_new_stream(s, AVS_VIDEO);
181                     if (avs->st_video == NULL)
182                         return AVERROR_NOMEM;
183                     avs->st_video->codec->codec_type = CODEC_TYPE_VIDEO;
184                     avs->st_video->codec->codec_id = CODEC_ID_AVS;
185                     avs->st_video->codec->width = avs->width;
186                     avs->st_video->codec->height = avs->height;
187                     avs->st_video->codec->bits_per_sample=avs->bits_per_sample;
188                     avs->st_video->nb_frames = avs->nb_frames;
189                     avs->st_video->codec->time_base = (AVRational) {
190                     1, avs->fps};
191                 }
192                 return avs_read_video_packet(s, pkt, type, sub_type, size,
193                                              palette, palette_size);
194
195             case AVS_AUDIO:
196                 if (!avs->st_audio) {
197                     avs->st_audio = av_new_stream(s, AVS_AUDIO);
198                     if (avs->st_audio == NULL)
199                         return AVERROR_NOMEM;
200                     avs->st_audio->codec->codec_type = CODEC_TYPE_AUDIO;
201                 }
202                 avs->remaining_audio_size = size - 4;
203                 size = avs_read_audio_packet(s, pkt);
204                 if (size != 0)
205                     return size;
206                 break;
207
208             default:
209                 url_fskip(&s->pb, size - 4);
210             }
211         }
212     }
213 }
214
215 static int avs_read_close(AVFormatContext * s)
216 {
217     return 0;
218 }
219
220 static AVInputFormat avs_iformat = {
221     "avs",
222     "avs format",
223     sizeof(avs_format_t),
224     avs_probe,
225     avs_read_header,
226     avs_read_packet,
227     avs_read_close,
228 };
229
230 #endif /* CONFIG_DEMUXERS */
231
232 int avs_init(void)
233 {
234 #ifdef CONFIG_DEMUXERS
235     av_register_input_format(&avs_iformat);
236 #endif /* CONFIG_DEMUXERS */
237     return 0;
238 }