]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/psxstr.c
Remove a dozen unused variables.
[frescor/ffmpeg.git] / libavformat / psxstr.c
1 /*
2  * Sony Playstation (PSX) STR File Demuxer
3  * Copyright (c) 2003 The ffmpeg Project
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
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 #define RIFF_TAG MKTAG('R', 'I', 'F', 'F')
37 #define CDXA_TAG MKTAG('C', 'D', 'X', 'A')
38
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     /* video parameters */
54     int video_stream_index;
55
56     /* audio parameters */
57     int audio_stream_index;
58 } StrChannel;
59
60 typedef struct StrDemuxContext {
61
62     /* a STR file can contain up to 32 channels of data */
63     StrChannel channels[32];
64
65     /* only decode the first audio and video channels encountered */
66     int video_channel;
67     int audio_channel;
68
69     int64_t pts;
70
71     AVPacket tmp_pkt;
72 } StrDemuxContext;
73
74 static const char sync_header[12] = {0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00};
75
76 static int str_probe(AVProbeData *p)
77 {
78     int start;
79
80     /* need at least 0x38 bytes to validate */
81     if (p->buf_size < 0x38)
82         return 0;
83
84     if ((AV_RL32(&p->buf[0]) == RIFF_TAG) &&
85         (AV_RL32(&p->buf[8]) == CDXA_TAG)) {
86
87         /* RIFF header seen; skip 0x2C bytes */
88         start = RIFF_HEADER_SIZE;
89     } else
90         start = 0;
91
92     /* look for CD sync header (00, 0xFF x 10, 00) */
93     if (memcmp(p->buf+start,sync_header,sizeof(sync_header)))
94         return 0;
95
96     /* MPEG files (like those ripped from VCDs) can also look like this;
97      * only return half certainty */
98     return 50;
99 }
100
101 #if 0
102 static void dump(unsigned char *buf,size_t len)
103 {
104     int i;
105     for(i=0;i<len;i++) {
106         if ((i&15)==0) av_log(NULL, AV_LOG_DEBUG, "%04x  ",i);
107         av_log(NULL, AV_LOG_DEBUG, "%02x ",buf[i]);
108         if ((i&15)==15) av_log(NULL, AV_LOG_DEBUG, "\n");
109     }
110     av_log(NULL, AV_LOG_DEBUG, "\n");
111 }
112 #endif
113
114 static int str_read_header(AVFormatContext *s,
115                            AVFormatParameters *ap)
116 {
117     ByteIOContext *pb = s->pb;
118     StrDemuxContext *str = s->priv_data;
119     AVStream *st;
120     unsigned char sector[RAW_CD_SECTOR_SIZE];
121     int start;
122     int i;
123     int channel;
124
125     /* initialize context members */
126     str->pts = 0;
127     str->audio_channel = -1;  /* assume to audio or video */
128     str->video_channel = -1;
129
130
131     /* skip over any RIFF header */
132     if (get_buffer(pb, sector, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE)
133         return AVERROR(EIO);
134     if (AV_RL32(&sector[0]) == RIFF_TAG)
135         start = RIFF_HEADER_SIZE;
136     else
137         start = 0;
138
139     url_fseek(pb, start, SEEK_SET);
140
141     /* check through the first 32 sectors for individual channels */
142     for (i = 0; i < 32; i++) {
143         if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
144             return AVERROR(EIO);
145
146 //printf("%02x %02x %02x %02x\n",sector[0x10],sector[0x11],sector[0x12],sector[0x13]);
147
148         channel = sector[0x11];
149         if (channel >= 32)
150             return AVERROR_INVALIDDATA;
151
152         switch (sector[0x12] & CDXA_TYPE_MASK) {
153
154         case CDXA_TYPE_DATA:
155         case CDXA_TYPE_VIDEO:
156             /* check if this channel gets to be the dominant video channel */
157             if (str->video_channel == -1) {
158                 /* qualify the magic number */
159                 if (AV_RL32(&sector[0x18]) != STR_MAGIC)
160                     break;
161                 str->video_channel = channel;
162
163                 /* allocate a new AVStream */
164                 st = av_new_stream(s, 0);
165                 if (!st)
166                     return AVERROR(ENOMEM);
167                 av_set_pts_info(st, 64, 1, 15);
168
169                 str->channels[channel].video_stream_index = st->index;
170
171                 st->codec->codec_type = CODEC_TYPE_VIDEO;
172                 st->codec->codec_id = CODEC_ID_MDEC;
173                 st->codec->codec_tag = 0;  /* no fourcc */
174                 st->codec->width      = AV_RL16(&sector[0x28]);
175                 st->codec->height     = AV_RL16(&sector[0x2A]);
176             }
177             break;
178
179         case CDXA_TYPE_AUDIO:
180             /* check if this channel gets to be the dominant audio channel */
181             if (str->audio_channel == -1) {
182                 int fmt;
183                 str->audio_channel = channel;
184
185                 /* allocate a new AVStream */
186                 st = av_new_stream(s, 0);
187                 if (!st)
188                     return AVERROR(ENOMEM);
189
190                 str->channels[channel].audio_stream_index = st->index;
191
192                 fmt = sector[0x13];
193                 st->codec->codec_type = CODEC_TYPE_AUDIO;
194                 st->codec->codec_id = CODEC_ID_ADPCM_XA;
195                 st->codec->codec_tag = 0;  /* no fourcc */
196                 st->codec->channels = (fmt&1)?2:1;
197                 st->codec->sample_rate = (fmt&4)?18900:37800;
198             //    st->codec->bit_rate = 0; //FIXME;
199                 st->codec->block_align = 128;
200
201                 av_set_pts_info(st, 64, 128, st->codec->sample_rate);
202             }
203             break;
204
205         default:
206             /* ignore */
207             break;
208         }
209     }
210
211     /* back to the start */
212     url_fseek(pb, start, SEEK_SET);
213
214     return 0;
215 }
216
217 static int str_read_packet(AVFormatContext *s,
218                            AVPacket *ret_pkt)
219 {
220     ByteIOContext *pb = s->pb;
221     StrDemuxContext *str = s->priv_data;
222     unsigned char sector[RAW_CD_SECTOR_SIZE];
223     int channel;
224     AVPacket *pkt;
225
226     while (1) {
227
228         if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
229             return AVERROR(EIO);
230
231         channel = sector[0x11];
232         if (channel >= 32)
233             return AVERROR_INVALIDDATA;
234
235         switch (sector[0x12] & CDXA_TYPE_MASK) {
236
237         case CDXA_TYPE_DATA:
238         case CDXA_TYPE_VIDEO:
239             /* check if this the video channel we care about */
240             if (channel == str->video_channel) {
241
242                 int current_sector = AV_RL16(&sector[0x1C]);
243                 int sector_count   = AV_RL16(&sector[0x1E]);
244                 int frame_size = AV_RL32(&sector[0x24]);
245
246                 if(!(   frame_size>=0
247                      && current_sector < sector_count
248                      && sector_count*VIDEO_DATA_CHUNK_SIZE >=frame_size)){
249                     av_log(s, AV_LOG_ERROR, "Invalid parameters %d %d %d\n", current_sector, sector_count, frame_size);
250                     return AVERROR_INVALIDDATA;
251                 }
252
253 //        printf("%d %d %d\n",current_sector,sector_count,frame_size);
254                 /* if this is the first sector of the frame, allocate a pkt */
255                 pkt = &str->tmp_pkt;
256
257                 if(pkt->size != sector_count*VIDEO_DATA_CHUNK_SIZE){
258                     if(pkt->data)
259                         av_log(s, AV_LOG_ERROR, "missmatching sector_count\n");
260                     av_free_packet(pkt);
261                     if (av_new_packet(pkt, sector_count*VIDEO_DATA_CHUNK_SIZE))
262                         return AVERROR(EIO);
263
264                     pkt->pos= url_ftell(pb) - RAW_CD_SECTOR_SIZE;
265                     pkt->stream_index =
266                         str->channels[channel].video_stream_index;
267                //     pkt->pts = str->pts;
268
269                     /* if there is no audio, adjust the pts after every video
270                      * frame; assume 15 fps */
271                    if (str->audio_channel != -1)
272                        str->pts += (90000 / 15);
273                 }
274
275                 memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE,
276                        sector + VIDEO_DATA_HEADER_SIZE,
277                        VIDEO_DATA_CHUNK_SIZE);
278
279                 if (current_sector == sector_count-1) {
280                     pkt->size= frame_size;
281                     *ret_pkt = *pkt;
282                     pkt->data= NULL;
283                     pkt->size= -1;
284                     return 0;
285                 }
286
287             }
288             break;
289
290         case CDXA_TYPE_AUDIO:
291 #ifdef PRINTSTUFF
292 printf (" dropping audio sector\n");
293 #endif
294 #if 1
295             /* check if this the video channel we care about */
296             if (channel == str->audio_channel) {
297                 pkt = ret_pkt;
298                 if (av_new_packet(pkt, 2304))
299                     return AVERROR(EIO);
300                 memcpy(pkt->data,sector+24,2304);
301
302                 pkt->stream_index =
303                     str->channels[channel].audio_stream_index;
304                 //pkt->pts = str->pts;
305                 return 0;
306             }
307 #endif
308             break;
309         default:
310             /* drop the sector and move on */
311 #ifdef PRINTSTUFF
312 printf (" dropping other sector\n");
313 #endif
314             break;
315         }
316
317         if (url_feof(pb))
318             return AVERROR(EIO);
319     }
320 }
321
322 static int str_read_close(AVFormatContext *s)
323 {
324     StrDemuxContext *str = s->priv_data;
325
326     return 0;
327 }
328
329 AVInputFormat str_demuxer = {
330     "psxstr",
331     NULL_IF_CONFIG_SMALL("Sony Playstation STR format"),
332     sizeof(StrDemuxContext),
333     str_probe,
334     str_read_header,
335     str_read_packet,
336     str_read_close,
337 };