2
2
* Sony Playstation (PSX) STR File Demuxer
3
3
* Copyright (c) 2003 The ffmpeg Project
5
* This library is free software; you can redistribute it and/or
5
* This file is part of FFmpeg.
7
* FFmpeg is free software; you can redistribute it and/or
6
8
* modify it under the terms of the GNU Lesser General Public
7
9
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
12
* FFmpeg is distributed in the hope that it will be useful,
11
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
15
* Lesser General Public License for more details.
15
17
* 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
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
32
34
//#define PRINTSTUFF
34
#define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
35
#define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
36
(((uint8_t*)(x))[2] << 16) | \
37
(((uint8_t*)(x))[1] << 8) | \
40
#define FOURCC_TAG( ch0, ch1, ch2, ch3 ) \
41
( (long)(unsigned char)(ch0) | \
42
( (long)(unsigned char)(ch1) << 8 ) | \
43
( (long)(unsigned char)(ch2) << 16 ) | \
44
( (long)(unsigned char)(ch3) << 24 ) )
46
#define RIFF_TAG FOURCC_TAG('R', 'I', 'F', 'F')
47
#define CDXA_TAG FOURCC_TAG('C', 'D', 'X', 'A')
36
#define RIFF_TAG MKTAG('R', 'I', 'F', 'F')
37
#define CDXA_TAG MKTAG('C', 'D', 'X', 'A')
49
39
#define RAW_CD_SECTOR_SIZE 2352
50
40
#define RAW_CD_SECTOR_DATA_SIZE 2304
91
81
unsigned char *video_chunk;
85
static const char sync_header[12] = {0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00};
94
87
static int str_probe(AVProbeData *p)
110
103
/* look for CD sync header (00, 0xFF x 10, 00) */
111
if ((p->buf[start + 0] != 0x00) || (p->buf[start + 1] != 0xFF) ||
112
(p->buf[start + 2] != 0xFF) || (p->buf[start + 3] != 0xFF) ||
113
(p->buf[start + 4] != 0xFF) || (p->buf[start + 5] != 0xFF) ||
114
(p->buf[start + 6] != 0xFF) || (p->buf[start + 7] != 0xFF) ||
115
(p->buf[start + 8] != 0xFF) || (p->buf[start + 9] != 0xFF) ||
116
(p->buf[start + 10] != 0xFF) || (p->buf[start + 11] != 0x00))
104
if (memcmp(p->buf+start,sync_header,sizeof(sync_header)))
119
107
/* MPEG files (like those ripped from VCDs) can also look like this;
113
static void dump(unsigned char *buf,size_t len)
117
if ((i&15)==0) av_log(NULL, AV_LOG_DEBUG, "%04x ",i);
118
av_log(NULL, AV_LOG_DEBUG, "%02x ",buf[i]);
119
if ((i&15)==15) av_log(NULL, AV_LOG_DEBUG, "\n");
121
av_log(NULL, AV_LOG_DEBUG, "\n");
124
125
static int str_read_header(AVFormatContext *s,
125
126
AVFormatParameters *ap)
168
168
/* check if this channel gets to be the dominant video channel */
169
169
if (str->video_channel == -1) {
170
170
/* qualify the magic number */
171
if (LE_32(§or[0x18]) != STR_MAGIC)
171
if (AV_RL32(§or[0x18]) != STR_MAGIC)
173
173
str->video_channel = channel;
174
174
str->channels[channel].type = STR_VIDEO;
175
str->channels[channel].width = LE_16(§or[0x28]);
176
str->channels[channel].height = LE_16(§or[0x2A]);
175
str->channels[channel].width = AV_RL16(§or[0x28]);
176
str->channels[channel].height = AV_RL16(§or[0x2A]);
178
178
/* allocate a new AVStream */
179
179
st = av_new_stream(s, 0);
181
181
return AVERROR_NOMEM;
182
av_set_pts_info(st, 64, 1, 15);
183
184
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;
186
st->codec->codec_type = CODEC_TYPE_VIDEO;
187
st->codec->codec_id = CODEC_ID_MDEC;
188
st->codec->codec_tag = 0; /* no fourcc */
189
st->codec->width = str->channels[channel].width;
190
st->codec->height = str->channels[channel].height;
193
194
case CDXA_TYPE_AUDIO:
194
195
/* check if this channel gets to be the dominant audio channel */
195
196
if (str->audio_channel == -1) {
196
198
str->audio_channel = channel;
197
199
str->channels[channel].type = STR_AUDIO;
198
str->channels[channel].channels =
200
str->channels[channel].channels =
199
201
(sector[0x13] & 0x01) ? 2 : 1;
200
str->channels[channel].sample_rate =
202
str->channels[channel].sample_rate =
201
203
(sector[0x13] & 0x04) ? 18900 : 37800;
202
str->channels[channel].bits =
204
str->channels[channel].bits =
203
205
(sector[0x13] & 0x10) ? 8 : 4;
207
/* allocate a new AVStream */
208
st = av_new_stream(s, 0);
210
return AVERROR_NOMEM;
211
av_set_pts_info(st, 64, 128, str->channels[channel].sample_rate);
213
str->channels[channel].audio_stream_index = st->index;
216
st->codec->codec_type = CODEC_TYPE_AUDIO;
217
st->codec->codec_id = CODEC_ID_ADPCM_XA;
218
st->codec->codec_tag = 0; /* no fourcc */
219
st->codec->channels = (fmt&1)?2:1;
220
st->codec->sample_rate = (fmt&4)?18900:37800;
221
// st->codec->bit_rate = 0; //FIXME;
222
st->codec->block_align = 128;
213
232
if (str->video_channel != -1)
214
printf (" video channel = %d, %d x %d\n", str->video_channel,
233
av_log (s, AV_LOG_DEBUG, " video channel = %d, %d x %d %d\n", str->video_channel,
215
234
str->channels[str->video_channel].width,
216
str->channels[str->video_channel].height);
235
str->channels[str->video_channel].height,str->channels[str->video_channel].video_stream_index);
217
236
if (str->audio_channel != -1)
218
printf (" audio channel = %d, %d Hz, %d channels, %d bits/sample\n",
220
str->channels[str->video_channel].sample_rate,
221
str->channels[str->video_channel].channels,
222
str->channels[str->video_channel].bits);
237
av_log (s, AV_LOG_DEBUG, " audio channel = %d, %d Hz, %d channels, %d bits/sample %d\n",
239
str->channels[str->audio_channel].sample_rate,
240
str->channels[str->audio_channel].channels,
241
str->channels[str->audio_channel].bits,str->channels[str->audio_channel].audio_stream_index);
224
243
/* back to the start */
225
244
url_fseek(pb, start, SEEK_SET);
230
249
static int str_read_packet(AVFormatContext *s,
233
252
ByteIOContext *pb = &s->pb;
234
253
StrDemuxContext *str = (StrDemuxContext *)s->priv_data;
235
254
unsigned char sector[RAW_CD_SECTOR_SIZE];
237
256
int packet_read = 0;
238
int video_sector_count = 0;
239
int current_video_sector = 0;
240
int video_frame_size = 0;
241
int video_frame_index = 0;
245
260
while (!packet_read) {
247
262
if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
250
265
channel = sector[0x11];
251
266
if (channel >= 32)
258
273
/* check if this the video channel we care about */
259
274
if (channel == str->video_channel) {
276
int current_sector = AV_RL16(§or[0x1C]);
277
int sector_count = AV_RL16(§or[0x1E]);
278
int frame_size = AV_RL32(§or[0x24]);
280
// printf("%d %d %d\n",current_sector,sector_count,frame_size);
261
281
/* if this is the first sector of the frame, allocate a pkt */
262
if (current_video_sector == 0) {
263
video_frame_size = LE_32(§or[0x24]);
264
video_sector_count = LE_16(§or[0x1E]);
265
if (av_new_packet(pkt, video_frame_size))
283
if (current_sector == 0) {
284
if (av_new_packet(pkt, frame_size))
287
pkt->pos= url_ftell(pb) - RAW_CD_SECTOR_SIZE;
269
289
str->channels[channel].video_stream_index;
290
// pkt->pts = str->pts;
272
292
/* if there is no audio, adjust the pts after every video
273
293
* frame; assume 15 fps */
278
298
/* load all the constituent chunks in the video packet */
279
if (video_frame_size - video_frame_index < VIDEO_DATA_CHUNK_SIZE)
280
bytes_to_copy = video_frame_size - video_frame_index;
282
bytes_to_copy = VIDEO_DATA_CHUNK_SIZE;
283
if (video_frame_index < video_frame_size)
284
memcpy(&pkt->data[video_frame_index],
285
§or[VIDEO_DATA_HEADER_SIZE], bytes_to_copy);
288
printf (" chunk %d/%d (bytes %d/%d), first 6 bytes = %02X %02X %02X %02X %02X %02X\n",
289
current_video_sector, video_sector_count,
290
video_frame_index, video_frame_size,
291
pkt->data[current_video_sector * VIDEO_DATA_CHUNK_SIZE + 0],
292
pkt->data[current_video_sector * VIDEO_DATA_CHUNK_SIZE + 1],
293
pkt->data[current_video_sector * VIDEO_DATA_CHUNK_SIZE + 2],
294
pkt->data[current_video_sector * VIDEO_DATA_CHUNK_SIZE + 3],
295
pkt->data[current_video_sector * VIDEO_DATA_CHUNK_SIZE + 4],
296
pkt->data[current_video_sector * VIDEO_DATA_CHUNK_SIZE + 5]);
299
video_frame_index += bytes_to_copy;
300
/* must keep reading sectors until all current video sectors
302
current_video_sector++;
303
if (current_video_sector >= video_sector_count)
299
bytes_to_copy = frame_size - current_sector*VIDEO_DATA_CHUNK_SIZE;
300
if (bytes_to_copy>0) {
301
if (bytes_to_copy>VIDEO_DATA_CHUNK_SIZE) bytes_to_copy=VIDEO_DATA_CHUNK_SIZE;
302
memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE,
303
sector + VIDEO_DATA_HEADER_SIZE, bytes_to_copy);
305
if (current_sector == sector_count-1) {
310
314
#ifdef PRINTSTUFF
311
315
printf (" dropping audio sector\n");
318
/* check if this the video channel we care about */
319
if (channel == str->audio_channel) {
321
if (av_new_packet(pkt, 2304))
323
memcpy(pkt->data,sector+24,2304);
326
str->channels[channel].audio_stream_index;
327
//pkt->pts = str->pts;
315
333
/* drop the sector and move on */
316
334
#ifdef PRINTSTUFF