2 * Sony Playstation (PSX) STR File Demuxer
3 * Copyright (c) 2003 The FFmpeg project
5 * This file is part of FFmpeg.
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.
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.
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
24 * PSX STR file demuxer
25 * by Mike Melanson (melanson@pcisys.net)
26 * This module handles streams that have been ripped from Sony Playstation
27 * CD games. This demuxer can handle either raw STR files (which are just
28 * concatenations of raw compact disc sectors) or STR files with 0x2C-byte
29 * RIFF headers, followed by CD sectors.
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/internal.h"
34 #include "libavutil/intreadwrite.h"
36 #include "avio_internal.h"
40 #define RIFF_TAG MKTAG('R', 'I', 'F', 'F')
41 #define CDXA_TAG MKTAG('C', 'D', 'X', 'A')
43 #define RAW_CD_SECTOR_SIZE 2352
44 #define RAW_CD_SECTOR_DATA_SIZE 2304
45 #define VIDEO_DATA_CHUNK_SIZE 0x7E0
46 #define VIDEO_DATA_HEADER_SIZE 0x38
47 #define RIFF_HEADER_SIZE 0x2C
49 #define CDXA_TYPE_MASK 0x0E
50 #define CDXA_TYPE_DATA 0x08
51 #define CDXA_TYPE_AUDIO 0x04
52 #define CDXA_TYPE_VIDEO 0x02
53 #define CDXA_TYPE_EMPTY 0x00
55 #define STR_MAGIC (0x80010160)
57 typedef struct StrChannel
{
58 /* video parameters */
59 int video_stream_index
;
62 /* audio parameters */
63 int audio_stream_index
;
66 typedef struct StrDemuxContext
{
68 /* a STR file can contain up to 32 channels of data */
69 StrChannel channels
[32];
72 static const uint8_t sync_header
[12] = {0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00};
74 static int str_probe(const AVProbeData
*p
)
76 const uint8_t *sector
= p
->buf
;
77 const uint8_t *end
= sector
+ p
->buf_size
;
80 if (p
->buf_size
< RAW_CD_SECTOR_SIZE
)
83 if ((AV_RL32(&p
->buf
[0]) == RIFF_TAG
) &&
84 (AV_RL32(&p
->buf
[8]) == CDXA_TAG
)) {
86 /* RIFF header seen; skip 0x2C bytes */
87 sector
+= RIFF_HEADER_SIZE
;
90 while (end
- sector
>= RAW_CD_SECTOR_SIZE
) {
91 /* look for CD sync header (00, 0xFF x 10, 00) */
92 if (memcmp(sector
,sync_header
,sizeof(sync_header
)))
95 if (sector
[0x11] >= 32)
98 switch (sector
[0x12] & CDXA_TYPE_MASK
) {
100 case CDXA_TYPE_VIDEO
: {
101 int current_sector
= AV_RL16(§or
[0x1C]);
102 int sector_count
= AV_RL16(§or
[0x1E]);
103 int frame_size
= AV_RL32(§or
[0x24]);
106 && current_sector
< sector_count
107 && sector_count
*VIDEO_DATA_CHUNK_SIZE
>=frame_size
)){
114 case CDXA_TYPE_AUDIO
:
115 if(sector
[0x13]&0x2A)
120 if(sector
[0x12] & CDXA_TYPE_MASK
)
123 sector
+= RAW_CD_SECTOR_SIZE
;
125 /* MPEG files (like those ripped from VCDs) can also look like this;
126 * only return half certainty */
127 if(vid
+aud
> 3) return AVPROBE_SCORE_EXTENSION
;
128 else if(vid
+aud
) return 1;
132 static int str_read_header(AVFormatContext
*s
)
134 AVIOContext
*pb
= s
->pb
;
135 StrDemuxContext
*str
= s
->priv_data
;
136 unsigned char sector
[RAW_CD_SECTOR_SIZE
];
141 /* skip over any RIFF header */
142 if ((ret
= ffio_read_size(pb
, sector
, RIFF_HEADER_SIZE
)) < 0)
144 if (AV_RL32(§or
[0]) == RIFF_TAG
)
145 start
= RIFF_HEADER_SIZE
;
149 avio_seek(pb
, start
, SEEK_SET
);
152 str
->channels
[i
].video_stream_index
=
153 str
->channels
[i
].audio_stream_index
= -1;
156 s
->ctx_flags
|= AVFMTCTX_NOHEADER
;
161 static int str_read_packet(AVFormatContext
*s
,
164 AVIOContext
*pb
= s
->pb
;
165 StrDemuxContext
*str
= s
->priv_data
;
166 unsigned char sector
[RAW_CD_SECTOR_SIZE
];
172 int read
= avio_read(pb
, sector
, RAW_CD_SECTOR_SIZE
);
174 if (read
== AVERROR_EOF
)
177 if (read
!= RAW_CD_SECTOR_SIZE
)
178 return read
< 0 ? read
: AVERROR_INVALIDDATA
;
180 channel
= sector
[0x11];
182 return AVERROR_INVALIDDATA
;
184 switch (sector
[0x12] & CDXA_TYPE_MASK
) {
187 case CDXA_TYPE_VIDEO
:
190 int current_sector
= AV_RL16(§or
[0x1C]);
191 int sector_count
= AV_RL16(§or
[0x1E]);
192 int frame_size
= AV_RL32(§or
[0x24]);
195 && current_sector
< sector_count
196 && sector_count
*VIDEO_DATA_CHUNK_SIZE
>=frame_size
)){
197 av_log(s
, AV_LOG_ERROR
, "Invalid parameters %d %d %d\n", current_sector
, sector_count
, frame_size
);
201 if(str
->channels
[channel
].video_stream_index
< 0){
202 /* allocate a new AVStream */
203 st
= avformat_new_stream(s
, NULL
);
205 return AVERROR(ENOMEM
);
206 avpriv_set_pts_info(st
, 64, 1, 15);
208 str
->channels
[channel
].video_stream_index
= st
->index
;
210 st
->codecpar
->codec_type
= AVMEDIA_TYPE_VIDEO
;
211 st
->codecpar
->codec_id
= AV_CODEC_ID_MDEC
;
212 st
->codecpar
->codec_tag
= 0; /* no fourcc */
213 st
->codecpar
->width
= AV_RL16(§or
[0x28]);
214 st
->codecpar
->height
= AV_RL16(§or
[0x2A]);
217 /* if this is the first sector of the frame, allocate a pkt */
218 pkt
= &str
->channels
[channel
].tmp_pkt
;
220 if(pkt
->size
!= sector_count
*VIDEO_DATA_CHUNK_SIZE
){
222 av_log(s
, AV_LOG_ERROR
, "mismatching sector_count\n");
223 av_packet_unref(pkt
);
224 ret
= av_new_packet(pkt
, sector_count
* VIDEO_DATA_CHUNK_SIZE
);
227 memset(pkt
->data
, 0, sector_count
*VIDEO_DATA_CHUNK_SIZE
);
229 pkt
->pos
= avio_tell(pb
) - RAW_CD_SECTOR_SIZE
;
231 str
->channels
[channel
].video_stream_index
;
234 memcpy(pkt
->data
+ current_sector
*VIDEO_DATA_CHUNK_SIZE
,
235 sector
+ VIDEO_DATA_HEADER_SIZE
,
236 VIDEO_DATA_CHUNK_SIZE
);
238 if (current_sector
== sector_count
-1) {
239 pkt
->size
= frame_size
;
250 case CDXA_TYPE_AUDIO
:
251 if(str
->channels
[channel
].audio_stream_index
< 0){
252 int fmt
= sector
[0x13];
253 /* allocate a new AVStream */
254 st
= avformat_new_stream(s
, NULL
);
256 return AVERROR(ENOMEM
);
258 str
->channels
[channel
].audio_stream_index
= st
->index
;
260 st
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
261 st
->codecpar
->codec_id
= AV_CODEC_ID_ADPCM_XA
;
262 st
->codecpar
->codec_tag
= 0; /* no fourcc */
263 av_channel_layout_default(&st
->codecpar
->ch_layout
, (fmt
& 1) + 1);
264 st
->codecpar
->sample_rate
= (fmt
&4)?18900:37800;
265 // st->codecpar->bit_rate = 0; //FIXME;
266 st
->codecpar
->block_align
= 128;
268 avpriv_set_pts_info(st
, 64, 18 * 224 / st
->codecpar
->ch_layout
.nb_channels
,
269 st
->codecpar
->sample_rate
);
273 if ((ret
= av_new_packet(pkt
, 2304)) < 0)
275 memcpy(pkt
->data
,sector
+24,2304);
278 str
->channels
[channel
].audio_stream_index
;
281 case CDXA_TYPE_EMPTY
: /* CD-ROM XA, May 1991, 4.3.2.3 */
282 /* NOTE this also catches 0x80 (EOF bit) because of CDXA_TYPE_MASK */
283 /* TODO consider refactoring so as to explicitly handle each case? */
286 av_log(s
, AV_LOG_WARNING
, "Unknown sector type %02X\n", sector
[0x12]);
287 /* drop the sector and move on */
292 return AVERROR_INVALIDDATA
;
296 static int str_read_close(AVFormatContext
*s
)
298 StrDemuxContext
*str
= s
->priv_data
;
301 if(str
->channels
[i
].tmp_pkt
.data
)
302 av_packet_unref(&str
->channels
[i
].tmp_pkt
);
308 const FFInputFormat ff_str_demuxer
= {
310 .p
.long_name
= NULL_IF_CONFIG_SMALL("Sony Playstation STR"),
311 .p
.flags
= AVFMT_NO_BYTE_SEEK
,
312 .priv_data_size
= sizeof(StrDemuxContext
),
313 .read_probe
= str_probe
,
314 .read_header
= str_read_header
,
315 .read_packet
= str_read_packet
,
316 .read_close
= str_read_close
,