ffmpeg / libavformat / psxstr.c @ 5509bffa
History | View | Annotate | Download (11.2 KB)
1 |
/*
|
---|---|
2 |
* Sony Playstation (PSX) STR File Demuxer
|
3 |
* Copyright (c) 2003 The ffmpeg Project
|
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 Street, Fifth Floor, Boston, MA 02110-1301 USA
|
18 |
*/
|
19 |
|
20 |
/**
|
21 |
* @file psxstr.c
|
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.
|
28 |
*/
|
29 |
|
30 |
#include "avformat.h" |
31 |
|
32 |
//#define PRINTSTUFF
|
33 |
|
34 |
#define RIFF_TAG MKTAG('R', 'I', 'F', 'F') |
35 |
#define CDXA_TAG MKTAG('C', 'D', 'X', 'A') |
36 |
|
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 |
42 |
|
43 |
#define CDXA_TYPE_MASK 0x0E |
44 |
#define CDXA_TYPE_DATA 0x08 |
45 |
#define CDXA_TYPE_AUDIO 0x04 |
46 |
#define CDXA_TYPE_VIDEO 0x02 |
47 |
|
48 |
#define STR_MAGIC (0x80010160) |
49 |
|
50 |
typedef struct StrChannel { |
51 |
|
52 |
int type;
|
53 |
#define STR_AUDIO 0 |
54 |
#define STR_VIDEO 1 |
55 |
|
56 |
/* video parameters */
|
57 |
int width;
|
58 |
int height;
|
59 |
int video_stream_index;
|
60 |
|
61 |
/* audio parameters */
|
62 |
int sample_rate;
|
63 |
int channels;
|
64 |
int bits;
|
65 |
int audio_stream_index;
|
66 |
} StrChannel; |
67 |
|
68 |
typedef struct StrDemuxContext { |
69 |
|
70 |
/* a STR file can contain up to 32 channels of data */
|
71 |
StrChannel channels[32];
|
72 |
|
73 |
/* only decode the first audio and video channels encountered */
|
74 |
int video_channel;
|
75 |
int audio_channel;
|
76 |
|
77 |
int64_t pts; |
78 |
|
79 |
unsigned char *video_chunk; |
80 |
AVPacket tmp_pkt; |
81 |
} StrDemuxContext; |
82 |
|
83 |
const static char sync_header[12] = {0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00}; |
84 |
|
85 |
static int str_probe(AVProbeData *p) |
86 |
{ |
87 |
int start;
|
88 |
|
89 |
/* need at least 0x38 bytes to validate */
|
90 |
if (p->buf_size < 0x38) |
91 |
return 0; |
92 |
|
93 |
if ((LE_32(&p->buf[0]) == RIFF_TAG) && |
94 |
(LE_32(&p->buf[8]) == CDXA_TAG)) {
|
95 |
|
96 |
/* RIFF header seen; skip 0x2C bytes */
|
97 |
start = RIFF_HEADER_SIZE; |
98 |
} else
|
99 |
start = 0;
|
100 |
|
101 |
/* look for CD sync header (00, 0xFF x 10, 00) */
|
102 |
if (memcmp(p->buf+start,sync_header,sizeof(sync_header))) |
103 |
return 0; |
104 |
|
105 |
/* MPEG files (like those ripped from VCDs) can also look like this;
|
106 |
* only return half certainty */
|
107 |
return 50; |
108 |
} |
109 |
|
110 |
#if 0
|
111 |
static void dump(unsigned char *buf,size_t len)
|
112 |
{
|
113 |
int i;
|
114 |
for(i=0;i<len;i++) {
|
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");
|
118 |
}
|
119 |
av_log(NULL, AV_LOG_DEBUG, "\n");
|
120 |
}
|
121 |
#endif
|
122 |
|
123 |
static int str_read_header(AVFormatContext *s, |
124 |
AVFormatParameters *ap) |
125 |
{ |
126 |
ByteIOContext *pb = &s->pb; |
127 |
StrDemuxContext *str = (StrDemuxContext *)s->priv_data; |
128 |
AVStream *st; |
129 |
unsigned char sector[RAW_CD_SECTOR_SIZE]; |
130 |
int start;
|
131 |
int i;
|
132 |
int channel;
|
133 |
|
134 |
/* initialize context members */
|
135 |
str->pts = 0;
|
136 |
str->audio_channel = -1; /* assume to audio or video */ |
137 |
str->video_channel = -1;
|
138 |
str->video_chunk = NULL;
|
139 |
|
140 |
|
141 |
/* skip over any RIFF header */
|
142 |
if (get_buffer(pb, sector, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE)
|
143 |
return AVERROR_IO;
|
144 |
if (LE_32(§or[0]) == RIFF_TAG) |
145 |
start = RIFF_HEADER_SIZE; |
146 |
else
|
147 |
start = 0;
|
148 |
|
149 |
url_fseek(pb, start, SEEK_SET); |
150 |
|
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)
|
154 |
return AVERROR_IO;
|
155 |
|
156 |
//printf("%02x %02x %02x %02x\n",sector[0x10],sector[0x11],sector[0x12],sector[0x13]);
|
157 |
|
158 |
channel = sector[0x11];
|
159 |
if (channel >= 32) |
160 |
return AVERROR_INVALIDDATA;
|
161 |
|
162 |
switch (sector[0x12] & CDXA_TYPE_MASK) { |
163 |
|
164 |
case CDXA_TYPE_DATA:
|
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) |
170 |
break;
|
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]);
|
175 |
|
176 |
/* allocate a new AVStream */
|
177 |
st = av_new_stream(s, 0);
|
178 |
if (!st)
|
179 |
return AVERROR_NOMEM;
|
180 |
av_set_pts_info(st, 64, 1, 15); |
181 |
|
182 |
str->channels[channel].video_stream_index = st->index; |
183 |
|
184 |
st->codec->codec_type = CODEC_TYPE_VIDEO; |
185 |
st->codec->codec_id = CODEC_ID_MDEC; |
186 |
st->codec->codec_tag = 0; /* no fourcc */ |
187 |
st->codec->width = str->channels[channel].width; |
188 |
st->codec->height = str->channels[channel].height; |
189 |
} |
190 |
break;
|
191 |
|
192 |
case CDXA_TYPE_AUDIO:
|
193 |
/* check if this channel gets to be the dominant audio channel */
|
194 |
if (str->audio_channel == -1) { |
195 |
int fmt;
|
196 |
str->audio_channel = channel; |
197 |
str->channels[channel].type = STR_AUDIO; |
198 |
str->channels[channel].channels = |
199 |
(sector[0x13] & 0x01) ? 2 : 1; |
200 |
str->channels[channel].sample_rate = |
201 |
(sector[0x13] & 0x04) ? 18900 : 37800; |
202 |
str->channels[channel].bits = |
203 |
(sector[0x13] & 0x10) ? 8 : 4; |
204 |
|
205 |
/* allocate a new AVStream */
|
206 |
st = av_new_stream(s, 0);
|
207 |
if (!st)
|
208 |
return AVERROR_NOMEM;
|
209 |
av_set_pts_info(st, 64, 128, str->channels[channel].sample_rate); |
210 |
|
211 |
str->channels[channel].audio_stream_index = st->index; |
212 |
|
213 |
fmt = sector[0x13];
|
214 |
st->codec->codec_type = CODEC_TYPE_AUDIO; |
215 |
st->codec->codec_id = CODEC_ID_ADPCM_XA; |
216 |
st->codec->codec_tag = 0; /* no fourcc */ |
217 |
st->codec->channels = (fmt&1)?2:1; |
218 |
st->codec->sample_rate = (fmt&4)?18900:37800; |
219 |
// st->codec->bit_rate = 0; //FIXME;
|
220 |
st->codec->block_align = 128;
|
221 |
} |
222 |
break;
|
223 |
|
224 |
default:
|
225 |
/* ignore */
|
226 |
break;
|
227 |
} |
228 |
} |
229 |
|
230 |
if (str->video_channel != -1) |
231 |
av_log (s, AV_LOG_DEBUG, " video channel = %d, %d x %d %d\n", str->video_channel,
|
232 |
str->channels[str->video_channel].width, |
233 |
str->channels[str->video_channel].height,str->channels[str->video_channel].video_stream_index); |
234 |
if (str->audio_channel != -1) |
235 |
av_log (s, AV_LOG_DEBUG, " audio channel = %d, %d Hz, %d channels, %d bits/sample %d\n",
|
236 |
str->audio_channel, |
237 |
str->channels[str->audio_channel].sample_rate, |
238 |
str->channels[str->audio_channel].channels, |
239 |
str->channels[str->audio_channel].bits,str->channels[str->audio_channel].audio_stream_index); |
240 |
|
241 |
/* back to the start */
|
242 |
url_fseek(pb, start, SEEK_SET); |
243 |
|
244 |
return 0; |
245 |
} |
246 |
|
247 |
static int str_read_packet(AVFormatContext *s, |
248 |
AVPacket *ret_pkt) |
249 |
{ |
250 |
ByteIOContext *pb = &s->pb; |
251 |
StrDemuxContext *str = (StrDemuxContext *)s->priv_data; |
252 |
unsigned char sector[RAW_CD_SECTOR_SIZE]; |
253 |
int channel;
|
254 |
int packet_read = 0; |
255 |
int ret = 0; |
256 |
AVPacket *pkt; |
257 |
|
258 |
while (!packet_read) {
|
259 |
|
260 |
if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
|
261 |
return AVERROR_IO;
|
262 |
|
263 |
channel = sector[0x11];
|
264 |
if (channel >= 32) |
265 |
return AVERROR_INVALIDDATA;
|
266 |
|
267 |
switch (sector[0x12] & CDXA_TYPE_MASK) { |
268 |
|
269 |
case CDXA_TYPE_DATA:
|
270 |
case CDXA_TYPE_VIDEO:
|
271 |
/* check if this the video channel we care about */
|
272 |
if (channel == str->video_channel) {
|
273 |
|
274 |
int current_sector = LE_16(§or[0x1C]); |
275 |
int sector_count = LE_16(§or[0x1E]); |
276 |
int frame_size = LE_32(§or[0x24]); |
277 |
int bytes_to_copy;
|
278 |
// printf("%d %d %d\n",current_sector,sector_count,frame_size);
|
279 |
/* if this is the first sector of the frame, allocate a pkt */
|
280 |
pkt = &str->tmp_pkt; |
281 |
if (current_sector == 0) { |
282 |
if (av_new_packet(pkt, frame_size))
|
283 |
return AVERROR_IO;
|
284 |
|
285 |
pkt->pos= url_ftell(pb) - RAW_CD_SECTOR_SIZE; |
286 |
pkt->stream_index = |
287 |
str->channels[channel].video_stream_index; |
288 |
// pkt->pts = str->pts;
|
289 |
|
290 |
/* if there is no audio, adjust the pts after every video
|
291 |
* frame; assume 15 fps */
|
292 |
if (str->audio_channel != -1) |
293 |
str->pts += (90000 / 15); |
294 |
} |
295 |
|
296 |
/* load all the constituent chunks in the video packet */
|
297 |
bytes_to_copy = frame_size - current_sector*VIDEO_DATA_CHUNK_SIZE; |
298 |
if (bytes_to_copy>0) { |
299 |
if (bytes_to_copy>VIDEO_DATA_CHUNK_SIZE) bytes_to_copy=VIDEO_DATA_CHUNK_SIZE;
|
300 |
memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE, |
301 |
sector + VIDEO_DATA_HEADER_SIZE, bytes_to_copy); |
302 |
} |
303 |
if (current_sector == sector_count-1) { |
304 |
*ret_pkt = *pkt; |
305 |
return 0; |
306 |
} |
307 |
|
308 |
} |
309 |
break;
|
310 |
|
311 |
case CDXA_TYPE_AUDIO:
|
312 |
#ifdef PRINTSTUFF
|
313 |
printf (" dropping audio sector\n");
|
314 |
#endif
|
315 |
#if 1 |
316 |
/* check if this the video channel we care about */
|
317 |
if (channel == str->audio_channel) {
|
318 |
pkt = ret_pkt; |
319 |
if (av_new_packet(pkt, 2304)) |
320 |
return AVERROR_IO;
|
321 |
memcpy(pkt->data,sector+24,2304); |
322 |
|
323 |
pkt->stream_index = |
324 |
str->channels[channel].audio_stream_index; |
325 |
//pkt->pts = str->pts;
|
326 |
return 0; |
327 |
} |
328 |
#endif
|
329 |
break;
|
330 |
default:
|
331 |
/* drop the sector and move on */
|
332 |
#ifdef PRINTSTUFF
|
333 |
printf (" dropping other sector\n");
|
334 |
#endif
|
335 |
break;
|
336 |
} |
337 |
|
338 |
if (url_feof(pb))
|
339 |
return AVERROR_IO;
|
340 |
} |
341 |
|
342 |
return ret;
|
343 |
} |
344 |
|
345 |
static int str_read_close(AVFormatContext *s) |
346 |
{ |
347 |
StrDemuxContext *str = (StrDemuxContext *)s->priv_data; |
348 |
|
349 |
av_free(str->video_chunk); |
350 |
|
351 |
return 0; |
352 |
} |
353 |
|
354 |
static AVInputFormat str_iformat = {
|
355 |
"psxstr",
|
356 |
"Sony Playstation STR format",
|
357 |
sizeof(StrDemuxContext),
|
358 |
str_probe, |
359 |
str_read_header, |
360 |
str_read_packet, |
361 |
str_read_close, |
362 |
}; |
363 |
|
364 |
int str_init(void) |
365 |
{ |
366 |
av_register_input_format(&str_iformat); |
367 |
return 0; |
368 |
} |