Statistics
| Branch: | Revision:

ffmpeg / libavformat / psxstr.c @ 27d5f18f

History | View | Annotate | Download (11.1 KB)

1 3f16d933 Mike Melanson
/*
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 3a278992 Mike Melanson
#define RIFF_TAG MKTAG('R', 'I', 'F', 'F')
35
#define CDXA_TAG MKTAG('C', 'D', 'X', 'A')
36 3f16d933 Mike Melanson
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 93cb9d7f Mike Melanson
    AVPacket tmp_pkt;
81 3f16d933 Mike Melanson
} StrDemuxContext;
82
83 93cb9d7f Mike Melanson
const static char sync_header[12] = {0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00};
84
85 3f16d933 Mike Melanson
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 93cb9d7f Mike Melanson
    if (memcmp(p->buf+start,sync_header,sizeof(sync_header)))
103 3f16d933 Mike Melanson
        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 93cb9d7f Mike Melanson
static void dump(unsigned char *buf,size_t len)
111
{
112
    int i;
113
    for(i=0;i<len;i++) {
114 bc874dae Michel Bardiaux
        if ((i&15)==0) av_log(NULL, AV_LOG_DEBUG, "%04x  ",i);
115
        av_log(NULL, AV_LOG_DEBUG, "%02x ",buf[i]);
116
        if ((i&15)==15) av_log(NULL, AV_LOG_DEBUG, "\n");
117 93cb9d7f Mike Melanson
    }
118 bc874dae Michel Bardiaux
    av_log(NULL, AV_LOG_DEBUG, "\n");
119 93cb9d7f Mike Melanson
}
120
121 3f16d933 Mike Melanson
static int str_read_header(AVFormatContext *s,
122
                           AVFormatParameters *ap)
123
{
124
    ByteIOContext *pb = &s->pb;
125
    StrDemuxContext *str = (StrDemuxContext *)s->priv_data;
126
    AVStream *st;
127
    unsigned char sector[RAW_CD_SECTOR_SIZE];
128
    int start;
129
    int i;
130
    int channel;
131
132
    /* initialize context members */
133
    str->pts = 0;
134
    str->audio_channel = -1;  /* assume to audio or video */
135
    str->video_channel = -1;
136
    str->video_chunk = NULL;
137
138
139
    /* skip over any RIFF header */
140
    if (get_buffer(pb, sector, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE)
141
        return AVERROR_IO;
142
    if (LE_32(&sector[0]) == RIFF_TAG)
143
        start = RIFF_HEADER_SIZE;
144
    else
145
        start = 0;
146
147
    url_fseek(pb, start, SEEK_SET);
148
149
    /* check through the first 32 sectors for individual channels */
150
    for (i = 0; i < 32; i++) {
151
        if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
152
            return AVERROR_IO;
153
154 93cb9d7f Mike Melanson
//printf("%02x %02x %02x %02x\n",sector[0x10],sector[0x11],sector[0x12],sector[0x13]);
155
156 3f16d933 Mike Melanson
        channel = sector[0x11];
157
        if (channel >= 32)
158
            return AVERROR_INVALIDDATA;
159
160
        switch (sector[0x12] & CDXA_TYPE_MASK) {
161
162
        case CDXA_TYPE_DATA:
163
        case CDXA_TYPE_VIDEO:
164
            /* check if this channel gets to be the dominant video channel */
165
            if (str->video_channel == -1) {
166
                /* qualify the magic number */
167
                if (LE_32(&sector[0x18]) != STR_MAGIC)
168
                    break;
169
                str->video_channel = channel;
170
                str->channels[channel].type = STR_VIDEO;
171
                str->channels[channel].width = LE_16(&sector[0x28]);
172
                str->channels[channel].height = LE_16(&sector[0x2A]);
173
174
                /* allocate a new AVStream */
175
                st = av_new_stream(s, 0);
176
                if (!st)
177
                    return AVERROR_NOMEM;
178 9ee91c2f Michael Niedermayer
                /* set the pts reference (1 pts = 1/90000) */
179
                av_set_pts_info(st, 33, 1, 90000);
180 3f16d933 Mike Melanson
181
                str->channels[channel].video_stream_index = st->index;
182
183
                st->codec.codec_type = CODEC_TYPE_VIDEO;
184
                st->codec.codec_id = CODEC_ID_MDEC; 
185
                st->codec.codec_tag = 0;  /* no fourcc */
186
                st->codec.width = str->channels[channel].width;
187
                st->codec.height = str->channels[channel].height;
188
            }
189
            break;
190
191
        case CDXA_TYPE_AUDIO:
192
            /* check if this channel gets to be the dominant audio channel */
193
            if (str->audio_channel == -1) {
194 93cb9d7f Mike Melanson
                int fmt;
195 3f16d933 Mike Melanson
                str->audio_channel = channel;
196
                str->channels[channel].type = STR_AUDIO;
197
                str->channels[channel].channels = 
198
                    (sector[0x13] & 0x01) ? 2 : 1;
199
                str->channels[channel].sample_rate = 
200
                    (sector[0x13] & 0x04) ? 18900 : 37800;
201
                str->channels[channel].bits = 
202
                    (sector[0x13] & 0x10) ? 8 : 4;
203 93cb9d7f Mike Melanson
204
                /* allocate a new AVStream */
205
                st = av_new_stream(s, 0);
206
                if (!st)
207
                    return AVERROR_NOMEM;
208 9ee91c2f Michael Niedermayer
                av_set_pts_info(st, 33, 1, 90000);
209 93cb9d7f Mike Melanson
210
                str->channels[channel].audio_stream_index = st->index;
211
212
                fmt = sector[0x13];
213
                st->codec.codec_type = CODEC_TYPE_AUDIO;
214
                st->codec.codec_id = CODEC_ID_ADPCM_XA; 
215
                st->codec.codec_tag = 0;  /* no fourcc */
216
                st->codec.channels = (fmt&1)?2:1;
217
                st->codec.sample_rate = (fmt&4)?18900:37800;
218
            //    st->codec.bit_rate = 0; //FIXME;
219
                st->codec.block_align = 128;
220 3f16d933 Mike Melanson
            }
221
            break;
222
223
        default:
224
            /* ignore */
225
            break;
226
        }
227
    }
228
229
if (str->video_channel != -1)
230 bc874dae Michel Bardiaux
  av_log (s, AV_LOG_DEBUG, " video channel = %d, %d x %d %d\n", str->video_channel,
231 3f16d933 Mike Melanson
    str->channels[str->video_channel].width,
232 93cb9d7f Mike Melanson
    str->channels[str->video_channel].height,str->channels[str->video_channel].video_stream_index);
233 3f16d933 Mike Melanson
if (str->audio_channel != -1)
234 bc874dae Michel Bardiaux
   av_log (s, AV_LOG_DEBUG, " audio channel = %d, %d Hz, %d channels, %d bits/sample %d\n", 
235 93cb9d7f Mike Melanson
    str->audio_channel,
236
    str->channels[str->audio_channel].sample_rate,
237
    str->channels[str->audio_channel].channels,
238
    str->channels[str->audio_channel].bits,str->channels[str->audio_channel].audio_stream_index);
239 3f16d933 Mike Melanson
240
    /* back to the start */
241
    url_fseek(pb, start, SEEK_SET);
242
243
    return 0;
244
}
245
246
static int str_read_packet(AVFormatContext *s,
247 93cb9d7f Mike Melanson
                           AVPacket *ret_pkt)
248 3f16d933 Mike Melanson
{
249
    ByteIOContext *pb = &s->pb;
250
    StrDemuxContext *str = (StrDemuxContext *)s->priv_data;
251
    unsigned char sector[RAW_CD_SECTOR_SIZE];
252
    int channel;
253
    int packet_read = 0;
254
    int ret = 0;
255 93cb9d7f Mike Melanson
    AVPacket *pkt;
256 3f16d933 Mike Melanson
257
    while (!packet_read) {
258
259
        if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
260 0bd586c5 Mike Melanson
            return AVERROR_IO;
261 3f16d933 Mike Melanson
262
        channel = sector[0x11];
263
        if (channel >= 32)
264
            return AVERROR_INVALIDDATA;
265
266
        switch (sector[0x12] & CDXA_TYPE_MASK) {
267
268
        case CDXA_TYPE_DATA:
269
        case CDXA_TYPE_VIDEO:
270
            /* check if this the video channel we care about */
271
            if (channel == str->video_channel) {
272
273 93cb9d7f Mike Melanson
                int current_sector = LE_16(&sector[0x1C]);
274
                int sector_count   = LE_16(&sector[0x1E]);
275
                int frame_size = LE_32(&sector[0x24]);
276
                int bytes_to_copy;
277
//        printf("%d %d %d\n",current_sector,sector_count,frame_size);
278 3f16d933 Mike Melanson
                /* if this is the first sector of the frame, allocate a pkt */
279 93cb9d7f Mike Melanson
                pkt = &str->tmp_pkt;
280
                if (current_sector == 0) {
281
                    if (av_new_packet(pkt, frame_size))
282 0bd586c5 Mike Melanson
                        return AVERROR_IO;
283 3f16d933 Mike Melanson
284
                    pkt->stream_index = 
285
                        str->channels[channel].video_stream_index;
286 93cb9d7f Mike Melanson
               //     pkt->pts = str->pts;
287 3f16d933 Mike Melanson
288
                    /* if there is no audio, adjust the pts after every video
289
                     * frame; assume 15 fps */
290
                   if (str->audio_channel != -1)
291
                       str->pts += (90000 / 15);
292
                }
293
294
                /* load all the constituent chunks in the video packet */
295 93cb9d7f Mike Melanson
                bytes_to_copy = frame_size - current_sector*VIDEO_DATA_CHUNK_SIZE;
296
                if (bytes_to_copy>0) {
297
                    if (bytes_to_copy>VIDEO_DATA_CHUNK_SIZE) bytes_to_copy=VIDEO_DATA_CHUNK_SIZE;
298
                    memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE,
299
                        sector + VIDEO_DATA_HEADER_SIZE, bytes_to_copy);
300
                }
301
                if (current_sector == sector_count-1) {
302
                    *ret_pkt = *pkt;
303
                    return 0;
304
                }
305 3f16d933 Mike Melanson
306
            }
307
            break;
308
309
        case CDXA_TYPE_AUDIO:
310
#ifdef PRINTSTUFF
311
printf (" dropping audio sector\n");
312
#endif
313 93cb9d7f Mike Melanson
#if 1
314
            /* check if this the video channel we care about */
315
            if (channel == str->audio_channel) {
316
                pkt = ret_pkt;
317
                if (av_new_packet(pkt, 2304))
318 0bd586c5 Mike Melanson
                    return AVERROR_IO;
319 93cb9d7f Mike Melanson
                memcpy(pkt->data,sector+24,2304);
320
321
                pkt->stream_index = 
322
                    str->channels[channel].audio_stream_index;
323
                //pkt->pts = str->pts;
324
                return 0;
325
            }
326
#endif
327
            break;
328 3f16d933 Mike Melanson
        default:
329
            /* drop the sector and move on */
330
#ifdef PRINTSTUFF
331
printf (" dropping other sector\n");
332
#endif
333
            break;
334
        }
335
336
        if (url_feof(pb))
337 0bd586c5 Mike Melanson
            return AVERROR_IO;
338 3f16d933 Mike Melanson
    }
339
340
    return ret;
341
}
342
343
static int str_read_close(AVFormatContext *s)
344
{
345
    StrDemuxContext *str = (StrDemuxContext *)s->priv_data;
346
347
    av_free(str->video_chunk);
348
349
    return 0;
350
}
351
352
static AVInputFormat str_iformat = {
353
    "psxstr",
354
    "Sony Playstation STR format",
355
    sizeof(StrDemuxContext),
356
    str_probe,
357
    str_read_header,
358
    str_read_packet,
359
    str_read_close,
360
};
361
362
int str_init(void)
363
{
364
    av_register_input_format(&str_iformat);
365
    return 0;
366
}