Statistics
| Branch: | Revision:

ffmpeg / libavformat / psxstr.c @ 0a5790d1

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 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 3f16d933 Mike Melanson
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 3f16d933 Mike Melanson
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 3f16d933 Mike Melanson
 * 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.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 3f16d933 Mike Melanson
 */
21
22
/**
23
 * @file psxstr.c
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.
30
 */
31
32
#include "avformat.h"
33
34
//#define PRINTSTUFF
35
36 3a278992 Mike Melanson
#define RIFF_TAG MKTAG('R', 'I', 'F', 'F')
37
#define CDXA_TAG MKTAG('C', 'D', 'X', 'A')
38 3f16d933 Mike Melanson
39
#define RAW_CD_SECTOR_SIZE 2352
40
#define RAW_CD_SECTOR_DATA_SIZE 2304
41
#define VIDEO_DATA_CHUNK_SIZE 0x7E0
42
#define VIDEO_DATA_HEADER_SIZE 0x38
43
#define RIFF_HEADER_SIZE 0x2C
44
45
#define CDXA_TYPE_MASK     0x0E
46
#define CDXA_TYPE_DATA     0x08
47
#define CDXA_TYPE_AUDIO    0x04
48
#define CDXA_TYPE_VIDEO    0x02
49
50
#define STR_MAGIC (0x80010160)
51
52
typedef struct StrChannel {
53
54
    int type;
55
#define STR_AUDIO 0
56
#define STR_VIDEO 1
57
58
    /* video parameters */
59
    int width;
60
    int height;
61
    int video_stream_index;
62
63
    /* audio parameters */
64
    int sample_rate;
65
    int channels;
66
    int bits;
67
    int audio_stream_index;
68
} StrChannel;
69
70
typedef struct StrDemuxContext {
71
72
    /* a STR file can contain up to 32 channels of data */
73
    StrChannel channels[32];
74
75
    /* only decode the first audio and video channels encountered */
76
    int video_channel;
77
    int audio_channel;
78
79
    int64_t pts;
80
81
    unsigned char *video_chunk;
82 93cb9d7f Mike Melanson
    AVPacket tmp_pkt;
83 3f16d933 Mike Melanson
} StrDemuxContext;
84
85 ed4e20ac Diego Biurrun
static const char sync_header[12] = {0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00};
86 93cb9d7f Mike Melanson
87 3f16d933 Mike Melanson
static int str_probe(AVProbeData *p)
88
{
89
    int start;
90
91
    /* need at least 0x38 bytes to validate */
92
    if (p->buf_size < 0x38)
93
        return 0;
94
95 fead30d4 Alex Beregszaszi
    if ((AV_RL32(&p->buf[0]) == RIFF_TAG) &&
96
        (AV_RL32(&p->buf[8]) == CDXA_TAG)) {
97 3f16d933 Mike Melanson
98
        /* RIFF header seen; skip 0x2C bytes */
99
        start = RIFF_HEADER_SIZE;
100
    } else
101
        start = 0;
102
103
    /* look for CD sync header (00, 0xFF x 10, 00) */
104 93cb9d7f Mike Melanson
    if (memcmp(p->buf+start,sync_header,sizeof(sync_header)))
105 3f16d933 Mike Melanson
        return 0;
106
107
    /* MPEG files (like those ripped from VCDs) can also look like this;
108
     * only return half certainty */
109
    return 50;
110
}
111
112 88730be6 Måns Rullgård
#if 0
113 93cb9d7f Mike Melanson
static void dump(unsigned char *buf,size_t len)
114
{
115
    int i;
116
    for(i=0;i<len;i++) {
117 bc874dae Michel Bardiaux
        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");
120 93cb9d7f Mike Melanson
    }
121 bc874dae Michel Bardiaux
    av_log(NULL, AV_LOG_DEBUG, "\n");
122 93cb9d7f Mike Melanson
}
123 88730be6 Måns Rullgård
#endif
124 93cb9d7f Mike Melanson
125 3f16d933 Mike Melanson
static int str_read_header(AVFormatContext *s,
126
                           AVFormatParameters *ap)
127
{
128
    ByteIOContext *pb = &s->pb;
129 e4141433 Nicholas Tung
    StrDemuxContext *str = s->priv_data;
130 3f16d933 Mike Melanson
    AVStream *st;
131
    unsigned char sector[RAW_CD_SECTOR_SIZE];
132
    int start;
133
    int i;
134
    int channel;
135
136
    /* initialize context members */
137
    str->pts = 0;
138
    str->audio_channel = -1;  /* assume to audio or video */
139
    str->video_channel = -1;
140
    str->video_chunk = NULL;
141
142
143
    /* skip over any RIFF header */
144
    if (get_buffer(pb, sector, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE)
145 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
146 fead30d4 Alex Beregszaszi
    if (AV_RL32(&sector[0]) == RIFF_TAG)
147 3f16d933 Mike Melanson
        start = RIFF_HEADER_SIZE;
148
    else
149
        start = 0;
150
151
    url_fseek(pb, start, SEEK_SET);
152
153
    /* check through the first 32 sectors for individual channels */
154
    for (i = 0; i < 32; i++) {
155
        if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
156 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
157 3f16d933 Mike Melanson
158 93cb9d7f Mike Melanson
//printf("%02x %02x %02x %02x\n",sector[0x10],sector[0x11],sector[0x12],sector[0x13]);
159
160 3f16d933 Mike Melanson
        channel = sector[0x11];
161
        if (channel >= 32)
162
            return AVERROR_INVALIDDATA;
163
164
        switch (sector[0x12] & CDXA_TYPE_MASK) {
165
166
        case CDXA_TYPE_DATA:
167
        case CDXA_TYPE_VIDEO:
168
            /* check if this channel gets to be the dominant video channel */
169
            if (str->video_channel == -1) {
170
                /* qualify the magic number */
171 fead30d4 Alex Beregszaszi
                if (AV_RL32(&sector[0x18]) != STR_MAGIC)
172 3f16d933 Mike Melanson
                    break;
173
                str->video_channel = channel;
174
                str->channels[channel].type = STR_VIDEO;
175 fead30d4 Alex Beregszaszi
                str->channels[channel].width = AV_RL16(&sector[0x28]);
176
                str->channels[channel].height = AV_RL16(&sector[0x2A]);
177 3f16d933 Mike Melanson
178
                /* allocate a new AVStream */
179
                st = av_new_stream(s, 0);
180
                if (!st)
181 769e10f0 Panagiotis Issaris
                    return AVERROR(ENOMEM);
182 8341e4e3 Michael Niedermayer
                av_set_pts_info(st, 64, 1, 15);
183 3f16d933 Mike Melanson
184
                str->channels[channel].video_stream_index = st->index;
185
186 01f4895c Michael Niedermayer
                st->codec->codec_type = CODEC_TYPE_VIDEO;
187 115329f1 Diego Biurrun
                st->codec->codec_id = CODEC_ID_MDEC;
188 01f4895c Michael Niedermayer
                st->codec->codec_tag = 0;  /* no fourcc */
189
                st->codec->width = str->channels[channel].width;
190
                st->codec->height = str->channels[channel].height;
191 3f16d933 Mike Melanson
            }
192
            break;
193
194
        case CDXA_TYPE_AUDIO:
195
            /* check if this channel gets to be the dominant audio channel */
196
            if (str->audio_channel == -1) {
197 93cb9d7f Mike Melanson
                int fmt;
198 3f16d933 Mike Melanson
                str->audio_channel = channel;
199
                str->channels[channel].type = STR_AUDIO;
200 115329f1 Diego Biurrun
                str->channels[channel].channels =
201 3f16d933 Mike Melanson
                    (sector[0x13] & 0x01) ? 2 : 1;
202 115329f1 Diego Biurrun
                str->channels[channel].sample_rate =
203 3f16d933 Mike Melanson
                    (sector[0x13] & 0x04) ? 18900 : 37800;
204 115329f1 Diego Biurrun
                str->channels[channel].bits =
205 3f16d933 Mike Melanson
                    (sector[0x13] & 0x10) ? 8 : 4;
206 93cb9d7f Mike Melanson
207
                /* allocate a new AVStream */
208
                st = av_new_stream(s, 0);
209
                if (!st)
210 769e10f0 Panagiotis Issaris
                    return AVERROR(ENOMEM);
211 8341e4e3 Michael Niedermayer
                av_set_pts_info(st, 64, 128, str->channels[channel].sample_rate);
212 93cb9d7f Mike Melanson
213
                str->channels[channel].audio_stream_index = st->index;
214
215
                fmt = sector[0x13];
216 01f4895c Michael Niedermayer
                st->codec->codec_type = CODEC_TYPE_AUDIO;
217 115329f1 Diego Biurrun
                st->codec->codec_id = CODEC_ID_ADPCM_XA;
218 01f4895c Michael Niedermayer
                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;
223 3f16d933 Mike Melanson
            }
224
            break;
225
226
        default:
227
            /* ignore */
228
            break;
229
        }
230
    }
231
232
if (str->video_channel != -1)
233 bc874dae Michel Bardiaux
  av_log (s, AV_LOG_DEBUG, " video channel = %d, %d x %d %d\n", str->video_channel,
234 3f16d933 Mike Melanson
    str->channels[str->video_channel].width,
235 93cb9d7f Mike Melanson
    str->channels[str->video_channel].height,str->channels[str->video_channel].video_stream_index);
236 3f16d933 Mike Melanson
if (str->audio_channel != -1)
237 115329f1 Diego Biurrun
   av_log (s, AV_LOG_DEBUG, " audio channel = %d, %d Hz, %d channels, %d bits/sample %d\n",
238 93cb9d7f Mike Melanson
    str->audio_channel,
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);
242 3f16d933 Mike Melanson
243
    /* back to the start */
244
    url_fseek(pb, start, SEEK_SET);
245
246
    return 0;
247
}
248
249
static int str_read_packet(AVFormatContext *s,
250 93cb9d7f Mike Melanson
                           AVPacket *ret_pkt)
251 3f16d933 Mike Melanson
{
252
    ByteIOContext *pb = &s->pb;
253 e4141433 Nicholas Tung
    StrDemuxContext *str = s->priv_data;
254 3f16d933 Mike Melanson
    unsigned char sector[RAW_CD_SECTOR_SIZE];
255
    int channel;
256
    int packet_read = 0;
257
    int ret = 0;
258 93cb9d7f Mike Melanson
    AVPacket *pkt;
259 3f16d933 Mike Melanson
260
    while (!packet_read) {
261
262
        if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
263 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
264 3f16d933 Mike Melanson
265
        channel = sector[0x11];
266
        if (channel >= 32)
267
            return AVERROR_INVALIDDATA;
268
269
        switch (sector[0x12] & CDXA_TYPE_MASK) {
270
271
        case CDXA_TYPE_DATA:
272
        case CDXA_TYPE_VIDEO:
273
            /* check if this the video channel we care about */
274
            if (channel == str->video_channel) {
275
276 fead30d4 Alex Beregszaszi
                int current_sector = AV_RL16(&sector[0x1C]);
277
                int sector_count   = AV_RL16(&sector[0x1E]);
278
                int frame_size = AV_RL32(&sector[0x24]);
279 93cb9d7f Mike Melanson
                int bytes_to_copy;
280
//        printf("%d %d %d\n",current_sector,sector_count,frame_size);
281 3f16d933 Mike Melanson
                /* if this is the first sector of the frame, allocate a pkt */
282 93cb9d7f Mike Melanson
                pkt = &str->tmp_pkt;
283
                if (current_sector == 0) {
284
                    if (av_new_packet(pkt, frame_size))
285 6f3e0b21 Panagiotis Issaris
                        return AVERROR(EIO);
286 3f16d933 Mike Melanson
287 2692067a Michael Niedermayer
                    pkt->pos= url_ftell(pb) - RAW_CD_SECTOR_SIZE;
288 115329f1 Diego Biurrun
                    pkt->stream_index =
289 3f16d933 Mike Melanson
                        str->channels[channel].video_stream_index;
290 93cb9d7f Mike Melanson
               //     pkt->pts = str->pts;
291 3f16d933 Mike Melanson
292
                    /* if there is no audio, adjust the pts after every video
293
                     * frame; assume 15 fps */
294
                   if (str->audio_channel != -1)
295
                       str->pts += (90000 / 15);
296
                }
297
298
                /* load all the constituent chunks in the video packet */
299 93cb9d7f Mike Melanson
                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);
304
                }
305
                if (current_sector == sector_count-1) {
306
                    *ret_pkt = *pkt;
307
                    return 0;
308
                }
309 3f16d933 Mike Melanson
310
            }
311
            break;
312
313
        case CDXA_TYPE_AUDIO:
314
#ifdef PRINTSTUFF
315
printf (" dropping audio sector\n");
316
#endif
317 93cb9d7f Mike Melanson
#if 1
318
            /* check if this the video channel we care about */
319
            if (channel == str->audio_channel) {
320
                pkt = ret_pkt;
321
                if (av_new_packet(pkt, 2304))
322 6f3e0b21 Panagiotis Issaris
                    return AVERROR(EIO);
323 93cb9d7f Mike Melanson
                memcpy(pkt->data,sector+24,2304);
324
325 115329f1 Diego Biurrun
                pkt->stream_index =
326 93cb9d7f Mike Melanson
                    str->channels[channel].audio_stream_index;
327
                //pkt->pts = str->pts;
328
                return 0;
329
            }
330
#endif
331
            break;
332 3f16d933 Mike Melanson
        default:
333
            /* drop the sector and move on */
334
#ifdef PRINTSTUFF
335
printf (" dropping other sector\n");
336
#endif
337
            break;
338
        }
339
340
        if (url_feof(pb))
341 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
342 3f16d933 Mike Melanson
    }
343
344
    return ret;
345
}
346
347
static int str_read_close(AVFormatContext *s)
348
{
349 e4141433 Nicholas Tung
    StrDemuxContext *str = s->priv_data;
350 3f16d933 Mike Melanson
351
    av_free(str->video_chunk);
352
353
    return 0;
354
}
355
356 ff70e601 Måns Rullgård
AVInputFormat str_demuxer = {
357 3f16d933 Mike Melanson
    "psxstr",
358
    "Sony Playstation STR format",
359
    sizeof(StrDemuxContext),
360
    str_probe,
361
    str_read_header,
362
    str_read_packet,
363
    str_read_close,
364
};