1 /*
2 * AVS demuxer.
3 * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Argonaut Games' Creature Shock demuxer
25 * @see http://wiki.multimedia.cx/index.php?title=AVS
26 */
27
28 #include "avformat.h"
29 #include "voc.h"
30
31
32 typedef struct avs_format {
33 VocDecContext voc;
34 AVStream *st_video;
35 AVStream *st_audio;
36 int width;
37 int height;
38 int bits_per_sample;
39 int fps;
40 int nb_frames;
41 int remaining_frame_size;
42 int remaining_audio_size;
43 } AvsFormat;
44
45 typedef enum avs_block_type {
46 AVS_NONE = 0x00,
47 AVS_VIDEO = 0x01,
48 AVS_AUDIO = 0x02,
49 AVS_PALETTE = 0x03,
50 AVS_GAME_DATA = 0x04,
51 } AvsBlockType;
52
avs_probe(const AVProbeData * p)53 static int avs_probe(const AVProbeData * p)
54 {
55 const uint8_t *d;
56
57 d = p->buf;
58 if (d[0] == 'w' && d[1] == 'W' && d[2] == 0x10 && d[3] == 0)
59 /* Ensure the buffer probe scores higher than the extension probe.
60 * This avoids problems with misdetection as AviSynth scripts. */
61 return AVPROBE_SCORE_EXTENSION + 5;
62
63 return 0;
64 }
65
avs_read_header(AVFormatContext * s)66 static int avs_read_header(AVFormatContext * s)
67 {
68 AvsFormat *avs = s->priv_data;
69
70 s->ctx_flags |= AVFMTCTX_NOHEADER;
71
72 avio_skip(s->pb, 4);
73 avs->width = avio_rl16(s->pb);
74 avs->height = avio_rl16(s->pb);
75 avs->bits_per_sample = avio_rl16(s->pb);
76 avs->fps = avio_rl16(s->pb);
77 avs->nb_frames = avio_rl32(s->pb);
78 avs->remaining_frame_size = 0;
79 avs->remaining_audio_size = 0;
80
81 avs->st_video = avs->st_audio = NULL;
82
83 if (avs->width != 318 || avs->height != 198)
84 av_log(s, AV_LOG_ERROR, "This avs pretend to be %dx%d "
85 "when the avs format is supposed to be 318x198 only.\n",
86 avs->width, avs->height);
87
88 return 0;
89 }
90
91 static int
avs_read_video_packet(AVFormatContext * s, AVPacket * pkt, AvsBlockType type, int sub_type, int size, uint8_t * palette, int palette_size)92 avs_read_video_packet(AVFormatContext * s, AVPacket * pkt,
93 AvsBlockType type, int sub_type, int size,
94 uint8_t * palette, int palette_size)
95 {
96 AvsFormat *avs = s->priv_data;
97 int ret;
98
99 ret = av_new_packet(pkt, size + palette_size);
100 if (ret < 0)
101 return ret;
102
103 if (palette_size) {
104 pkt->data[0] = 0x00;
105 pkt->data[1] = 0x03;
106 pkt->data[2] = palette_size & 0xFF;
107 pkt->data[3] = (palette_size >> 8) & 0xFF;
108 memcpy(pkt->data + 4, palette, palette_size - 4);
109 }
110
111 pkt->data[palette_size + 0] = sub_type;
112 pkt->data[palette_size + 1] = type;
113 pkt->data[palette_size + 2] = size & 0xFF;
114 pkt->data[palette_size + 3] = (size >> 8) & 0xFF;
115 ret = avio_read(s->pb, pkt->data + palette_size + 4, size - 4) + 4;
116 if (ret < size) {
117 return AVERROR(EIO);
118 }
119
120 pkt->size = ret + palette_size;
121 pkt->stream_index = avs->st_video->index;
122 if (sub_type == 0)
123 pkt->flags |= AV_PKT_FLAG_KEY;
124
125 return 0;
126 }
127
avs_read_audio_packet(AVFormatContext * s, AVPacket * pkt)128 static int avs_read_audio_packet(AVFormatContext * s, AVPacket * pkt)
129 {
130 AvsFormat *avs = s->priv_data;
131 int ret;
132 int64_t size;
133
134 size = avio_tell(s->pb);
135 ret = ff_voc_get_packet(s, pkt, avs->st_audio, avs->remaining_audio_size);
136 size = avio_tell(s->pb) - size;
137 avs->remaining_audio_size -= size;
138
139 if (ret == AVERROR(EIO))
140 return 0; /* this indicate EOS */
141 if (ret < 0)
142 return ret;
143 if (size != (int)size) {
144 av_packet_unref(pkt);
145 return AVERROR(EDOM);
146 }
147
148 pkt->stream_index = avs->st_audio->index;
149 pkt->flags |= AV_PKT_FLAG_KEY;
150
151 return size;
152 }
153
avs_read_packet(AVFormatContext * s, AVPacket * pkt)154 static int avs_read_packet(AVFormatContext * s, AVPacket * pkt)
155 {
156 AvsFormat *avs = s->priv_data;
157 int sub_type = 0, size = 0;
158 AvsBlockType type = AVS_NONE;
159 int palette_size = 0;
160 uint8_t palette[4 + 3 * 256];
161 int ret;
162
163 if (avs->remaining_audio_size > 0)
164 if (avs_read_audio_packet(s, pkt) > 0)
165 return 0;
166
167 while (1) {
168 if (avs->remaining_frame_size <= 0) {
169 if (!avio_rl16(s->pb)) /* found EOF */
170 return AVERROR(EIO);
171 avs->remaining_frame_size = avio_rl16(s->pb) - 4;
172 }
173
174 while (avs->remaining_frame_size > 0) {
175 sub_type = avio_r8(s->pb);
176 type = avio_r8(s->pb);
177 size = avio_rl16(s->pb);
178 if (size < 4)
179 return AVERROR_INVALIDDATA;
180 avs->remaining_frame_size -= size;
181
182 switch (type) {
183 case AVS_PALETTE:
184 if (size - 4 > sizeof(palette))
185 return AVERROR_INVALIDDATA;
186 ret = avio_read(s->pb, palette, size - 4);
187 if (ret < size - 4)
188 return AVERROR(EIO);
189 palette_size = size;
190 break;
191
192 case AVS_VIDEO:
193 if (!avs->st_video) {
194 avs->st_video = avformat_new_stream(s, NULL);
195 if (!avs->st_video)
196 return AVERROR(ENOMEM);
197 avs->st_video->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
198 avs->st_video->codecpar->codec_id = AV_CODEC_ID_AVS;
199 avs->st_video->codecpar->width = avs->width;
200 avs->st_video->codecpar->height = avs->height;
201 avs->st_video->codecpar->bits_per_coded_sample=avs->bits_per_sample;
202 avs->st_video->nb_frames = avs->nb_frames;
203 #if FF_API_R_FRAME_RATE
204 avs->st_video->r_frame_rate =
205 #endif
206 avs->st_video->avg_frame_rate = (AVRational){avs->fps, 1};
207 }
208 return avs_read_video_packet(s, pkt, type, sub_type, size,
209 palette, palette_size);
210
211 case AVS_AUDIO:
212 if (!avs->st_audio) {
213 avs->st_audio = avformat_new_stream(s, NULL);
214 if (!avs->st_audio)
215 return AVERROR(ENOMEM);
216 avs->st_audio->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
217 }
218 avs->remaining_audio_size = size - 4;
219 size = avs_read_audio_packet(s, pkt);
220 if (size != 0)
221 return size;
222 break;
223
224 default:
225 avio_skip(s->pb, size - 4);
226 }
227 }
228 }
229 }
230
231 const AVInputFormat ff_avs_demuxer = {
232 .name = "avs",
233 .long_name = NULL_IF_CONFIG_SMALL("Argonaut Games Creature Shock"),
234 .priv_data_size = sizeof(AvsFormat),
235 .read_probe = avs_probe,
236 .read_header = avs_read_header,
237 .read_packet = avs_read_packet,
238 };
239