2 * Sony Playstation (PSX) STR File Demuxer
3 * Copyright (c) 2003 The ffmpeg Project
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.
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.
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
22 * PSX STR file demuxer
23 * by Mike Melanson (melanson@pcisys.net)
24 * This module handles streams that have been ripped from Sony Playstation
25 * CD games. This demuxer can handle either raw STR files (which are just
26 * concatenations of raw compact disc sectors) or STR files with 0x2C-byte
27 * RIFF headers, followed by CD sectors.
34 #define RIFF_TAG MKTAG('R', 'I', 'F', 'F')
35 #define CDXA_TAG MKTAG('C', 'D', 'X', 'A')
37 #define RAW_CD_SECTOR_SIZE 2352
38 #define RAW_CD_SECTOR_DATA_SIZE 2304
39 #define VIDEO_DATA_CHUNK_SIZE 0x7E0
40 #define VIDEO_DATA_HEADER_SIZE 0x38
41 #define RIFF_HEADER_SIZE 0x2C
43 #define CDXA_TYPE_MASK 0x0E
44 #define CDXA_TYPE_DATA 0x08
45 #define CDXA_TYPE_AUDIO 0x04
46 #define CDXA_TYPE_VIDEO 0x02
48 #define STR_MAGIC (0x80010160)
50 typedef struct StrChannel {
56 /* video parameters */
59 int video_stream_index;
61 /* audio parameters */
65 int audio_stream_index;
68 typedef struct StrDemuxContext {
70 /* a STR file can contain up to 32 channels of data */
71 StrChannel channels[32];
73 /* only decode the first audio and video channels encountered */
79 unsigned char *video_chunk;
83 const static char sync_header[12] = {0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00};
85 static int str_probe(AVProbeData *p)
89 /* need at least 0x38 bytes to validate */
90 if (p->buf_size < 0x38)
93 if ((LE_32(&p->buf[0]) == RIFF_TAG) &&
94 (LE_32(&p->buf[8]) == CDXA_TAG)) {
96 /* RIFF header seen; skip 0x2C bytes */
97 start = RIFF_HEADER_SIZE;
101 /* look for CD sync header (00, 0xFF x 10, 00) */
102 if (memcmp(p->buf+start,sync_header,sizeof(sync_header)))
105 /* MPEG files (like those ripped from VCDs) can also look like this;
106 * only return half certainty */
111 static void dump(unsigned char *buf,size_t len)
115 if ((i&15)==0) av_log(NULL, AV_LOG_DEBUG, "%04x ",i);
116 av_log(NULL, AV_LOG_DEBUG, "%02x ",buf[i]);
117 if ((i&15)==15) av_log(NULL, AV_LOG_DEBUG, "\n");
119 av_log(NULL, AV_LOG_DEBUG, "\n");
123 static int str_read_header(AVFormatContext *s,
124 AVFormatParameters *ap)
126 ByteIOContext *pb = &s->pb;
127 StrDemuxContext *str = (StrDemuxContext *)s->priv_data;
129 unsigned char sector[RAW_CD_SECTOR_SIZE];
134 /* initialize context members */
136 str->audio_channel = -1; /* assume to audio or video */
137 str->video_channel = -1;
138 str->video_chunk = NULL;
141 /* skip over any RIFF header */
142 if (get_buffer(pb, sector, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE)
144 if (LE_32(§or[0]) == RIFF_TAG)
145 start = RIFF_HEADER_SIZE;
149 url_fseek(pb, start, SEEK_SET);
151 /* check through the first 32 sectors for individual channels */
152 for (i = 0; i < 32; i++) {
153 if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
156 //printf("%02x %02x %02x %02x\n",sector[0x10],sector[0x11],sector[0x12],sector[0x13]);
158 channel = sector[0x11];
160 return AVERROR_INVALIDDATA;
162 switch (sector[0x12] & CDXA_TYPE_MASK) {
165 case CDXA_TYPE_VIDEO:
166 /* check if this channel gets to be the dominant video channel */
167 if (str->video_channel == -1) {
168 /* qualify the magic number */
169 if (LE_32(§or[0x18]) != STR_MAGIC)
171 str->video_channel = channel;
172 str->channels[channel].type = STR_VIDEO;
173 str->channels[channel].width = LE_16(§or[0x28]);
174 str->channels[channel].height = LE_16(§or[0x2A]);
176 /* allocate a new AVStream */
177 st = av_new_stream(s, 0);
179 return AVERROR_NOMEM;
180 /* set the pts reference (1 pts = 1/90000) */
181 av_set_pts_info(st, 33, 1, 90000);
183 str->channels[channel].video_stream_index = st->index;
185 st->codec.codec_type = CODEC_TYPE_VIDEO;
186 st->codec.codec_id = CODEC_ID_MDEC;
187 st->codec.codec_tag = 0; /* no fourcc */
188 st->codec.width = str->channels[channel].width;
189 st->codec.height = str->channels[channel].height;
193 case CDXA_TYPE_AUDIO:
194 /* check if this channel gets to be the dominant audio channel */
195 if (str->audio_channel == -1) {
197 str->audio_channel = channel;
198 str->channels[channel].type = STR_AUDIO;
199 str->channels[channel].channels =
200 (sector[0x13] & 0x01) ? 2 : 1;
201 str->channels[channel].sample_rate =
202 (sector[0x13] & 0x04) ? 18900 : 37800;
203 str->channels[channel].bits =
204 (sector[0x13] & 0x10) ? 8 : 4;
206 /* allocate a new AVStream */
207 st = av_new_stream(s, 0);
209 return AVERROR_NOMEM;
210 av_set_pts_info(st, 33, 1, 90000);
212 str->channels[channel].audio_stream_index = st->index;
215 st->codec.codec_type = CODEC_TYPE_AUDIO;
216 st->codec.codec_id = CODEC_ID_ADPCM_XA;
217 st->codec.codec_tag = 0; /* no fourcc */
218 st->codec.channels = (fmt&1)?2:1;
219 st->codec.sample_rate = (fmt&4)?18900:37800;
220 // st->codec.bit_rate = 0; //FIXME;
221 st->codec.block_align = 128;
231 if (str->video_channel != -1)
232 av_log (s, AV_LOG_DEBUG, " video channel = %d, %d x %d %d\n", str->video_channel,
233 str->channels[str->video_channel].width,
234 str->channels[str->video_channel].height,str->channels[str->video_channel].video_stream_index);
235 if (str->audio_channel != -1)
236 av_log (s, AV_LOG_DEBUG, " audio channel = %d, %d Hz, %d channels, %d bits/sample %d\n",
238 str->channels[str->audio_channel].sample_rate,
239 str->channels[str->audio_channel].channels,
240 str->channels[str->audio_channel].bits,str->channels[str->audio_channel].audio_stream_index);
242 /* back to the start */
243 url_fseek(pb, start, SEEK_SET);
248 static int str_read_packet(AVFormatContext *s,
251 ByteIOContext *pb = &s->pb;
252 StrDemuxContext *str = (StrDemuxContext *)s->priv_data;
253 unsigned char sector[RAW_CD_SECTOR_SIZE];
259 while (!packet_read) {
261 if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
264 channel = sector[0x11];
266 return AVERROR_INVALIDDATA;
268 switch (sector[0x12] & CDXA_TYPE_MASK) {
271 case CDXA_TYPE_VIDEO:
272 /* check if this the video channel we care about */
273 if (channel == str->video_channel) {
275 int current_sector = LE_16(§or[0x1C]);
276 int sector_count = LE_16(§or[0x1E]);
277 int frame_size = LE_32(§or[0x24]);
279 // printf("%d %d %d\n",current_sector,sector_count,frame_size);
280 /* if this is the first sector of the frame, allocate a pkt */
282 if (current_sector == 0) {
283 if (av_new_packet(pkt, frame_size))
286 pkt->pos= url_ftell(pb) - RAW_CD_SECTOR_SIZE;
288 str->channels[channel].video_stream_index;
289 // pkt->pts = str->pts;
291 /* if there is no audio, adjust the pts after every video
292 * frame; assume 15 fps */
293 if (str->audio_channel != -1)
294 str->pts += (90000 / 15);
297 /* load all the constituent chunks in the video packet */
298 bytes_to_copy = frame_size - current_sector*VIDEO_DATA_CHUNK_SIZE;
299 if (bytes_to_copy>0) {
300 if (bytes_to_copy>VIDEO_DATA_CHUNK_SIZE) bytes_to_copy=VIDEO_DATA_CHUNK_SIZE;
301 memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE,
302 sector + VIDEO_DATA_HEADER_SIZE, bytes_to_copy);
304 if (current_sector == sector_count-1) {
312 case CDXA_TYPE_AUDIO:
314 printf (" dropping audio sector\n");
317 /* check if this the video channel we care about */
318 if (channel == str->audio_channel) {
320 if (av_new_packet(pkt, 2304))
322 memcpy(pkt->data,sector+24,2304);
325 str->channels[channel].audio_stream_index;
326 //pkt->pts = str->pts;
332 /* drop the sector and move on */
334 printf (" dropping other sector\n");
346 static int str_read_close(AVFormatContext *s)
348 StrDemuxContext *str = (StrDemuxContext *)s->priv_data;
350 av_free(str->video_chunk);
355 static AVInputFormat str_iformat = {
357 "Sony Playstation STR format",
358 sizeof(StrDemuxContext),
367 av_register_input_format(&str_iformat);