]> git.sesse.net Git - ffmpeg/blob - libavformat/psxstr.c
Merge remote-tracking branch 'shariman/wmall'
[ffmpeg] / 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
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 "libavutil/intreadwrite.h"
33 #include "avformat.h"
34
35 #define RIFF_TAG MKTAG('R', 'I', 'F', 'F')
36 #define CDXA_TAG MKTAG('C', 'D', 'X', 'A')
37
38 #define RAW_CD_SECTOR_SIZE      2352
39 #define RAW_CD_SECTOR_DATA_SIZE 2304
40 #define VIDEO_DATA_CHUNK_SIZE   0x7E0
41 #define VIDEO_DATA_HEADER_SIZE  0x38
42 #define RIFF_HEADER_SIZE        0x2C
43
44 #define CDXA_TYPE_MASK     0x0E
45 #define CDXA_TYPE_DATA     0x08
46 #define CDXA_TYPE_AUDIO    0x04
47 #define CDXA_TYPE_VIDEO    0x02
48
49 #define STR_MAGIC (0x80010160)
50
51 typedef struct StrChannel {
52     /* video parameters */
53     int video_stream_index;
54     AVPacket tmp_pkt;
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 } StrDemuxContext;
65
66 static const char sync_header[12] = {0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00};
67
68 static int str_probe(AVProbeData *p)
69 {
70     uint8_t *sector= p->buf;
71     uint8_t *end= sector + p->buf_size;
72     int aud=0, vid=0;
73
74     if (p->buf_size < RAW_CD_SECTOR_SIZE)
75         return 0;
76
77     if ((AV_RL32(&p->buf[0]) == RIFF_TAG) &&
78         (AV_RL32(&p->buf[8]) == CDXA_TAG)) {
79
80         /* RIFF header seen; skip 0x2C bytes */
81         sector += RIFF_HEADER_SIZE;
82     }
83
84     while (end - sector >= RAW_CD_SECTOR_SIZE) {
85         /* look for CD sync header (00, 0xFF x 10, 00) */
86         if (memcmp(sector,sync_header,sizeof(sync_header)))
87             return 0;
88
89         if (sector[0x11] >= 32)
90             return 0;
91
92         switch (sector[0x12] & CDXA_TYPE_MASK) {
93         case CDXA_TYPE_DATA:
94         case CDXA_TYPE_VIDEO: {
95                 int current_sector = AV_RL16(&sector[0x1C]);
96                 int sector_count   = AV_RL16(&sector[0x1E]);
97                 int frame_size = AV_RL32(&sector[0x24]);
98
99                 if(!(   frame_size>=0
100                      && current_sector < sector_count
101                      && sector_count*VIDEO_DATA_CHUNK_SIZE >=frame_size)){
102                     return 0;
103                 }
104
105                 /*st->codec->width      = AV_RL16(&sector[0x28]);
106                 st->codec->height     = AV_RL16(&sector[0x2A]);*/
107
108 //                 if (current_sector == sector_count-1) {
109                     vid++;
110 //                 }
111
112             }
113             break;
114         case CDXA_TYPE_AUDIO:
115             if(sector[0x13]&0x2A)
116                 return 0;
117             aud++;
118             break;
119         default:
120             if(sector[0x12] & CDXA_TYPE_MASK)
121                 return 0;
122         }
123         sector += RAW_CD_SECTOR_SIZE;
124     }
125     /* MPEG files (like those ripped from VCDs) can also look like this;
126      * only return half certainty */
127     if(vid+aud > 3)  return 50;
128     else if(vid+aud) return 1;
129     else             return 0;
130 }
131
132 static int str_read_header(AVFormatContext *s,
133                            AVFormatParameters *ap)
134 {
135     AVIOContext *pb = s->pb;
136     StrDemuxContext *str = s->priv_data;
137     unsigned char sector[RAW_CD_SECTOR_SIZE];
138     int start;
139     int i;
140
141     /* skip over any RIFF header */
142     if (avio_read(pb, sector, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE)
143         return AVERROR(EIO);
144     if (AV_RL32(&sector[0]) == RIFF_TAG)
145         start = RIFF_HEADER_SIZE;
146     else
147         start = 0;
148
149     avio_seek(pb, start, SEEK_SET);
150
151     for(i=0; i<32; i++){
152         str->channels[i].video_stream_index=
153         str->channels[i].audio_stream_index= -1;
154     }
155
156     s->ctx_flags |= AVFMTCTX_NOHEADER;
157
158     return 0;
159 }
160
161 static int str_read_packet(AVFormatContext *s,
162                            AVPacket *ret_pkt)
163 {
164     AVIOContext *pb = s->pb;
165     StrDemuxContext *str = s->priv_data;
166     unsigned char sector[RAW_CD_SECTOR_SIZE];
167     int channel;
168     AVPacket *pkt;
169     AVStream *st;
170
171     while (1) {
172
173         if (avio_read(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
174             return AVERROR(EIO);
175
176         channel = sector[0x11];
177         if (channel >= 32)
178             return AVERROR_INVALIDDATA;
179
180         switch (sector[0x12] & CDXA_TYPE_MASK) {
181
182         case CDXA_TYPE_DATA:
183         case CDXA_TYPE_VIDEO:
184             {
185
186                 int current_sector = AV_RL16(&sector[0x1C]);
187                 int sector_count   = AV_RL16(&sector[0x1E]);
188                 int frame_size = AV_RL32(&sector[0x24]);
189
190                 if(!(   frame_size>=0
191                      && current_sector < sector_count
192                      && sector_count*VIDEO_DATA_CHUNK_SIZE >=frame_size)){
193                     av_log(s, AV_LOG_ERROR, "Invalid parameters %d %d %d\n", current_sector, sector_count, frame_size);
194                     break;
195                 }
196
197                 if(str->channels[channel].video_stream_index < 0){
198                     /* allocate a new AVStream */
199                     st = avformat_new_stream(s, NULL);
200                     if (!st)
201                         return AVERROR(ENOMEM);
202                     av_set_pts_info(st, 64, 1, 15);
203
204                     str->channels[channel].video_stream_index = st->index;
205
206                     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
207                     st->codec->codec_id   = CODEC_ID_MDEC;
208                     st->codec->codec_tag  = 0;  /* no fourcc */
209                     st->codec->width      = AV_RL16(&sector[0x28]);
210                     st->codec->height     = AV_RL16(&sector[0x2A]);
211                 }
212
213                 /* if this is the first sector of the frame, allocate a pkt */
214                 pkt = &str->channels[channel].tmp_pkt;
215
216                 if(pkt->size != sector_count*VIDEO_DATA_CHUNK_SIZE){
217                     if(pkt->data)
218                         av_log(s, AV_LOG_ERROR, "missmatching sector_count\n");
219                     av_free_packet(pkt);
220                     if (av_new_packet(pkt, sector_count*VIDEO_DATA_CHUNK_SIZE))
221                         return AVERROR(EIO);
222
223                     pkt->pos= avio_tell(pb) - RAW_CD_SECTOR_SIZE;
224                     pkt->stream_index =
225                         str->channels[channel].video_stream_index;
226                 }
227
228                 memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE,
229                        sector + VIDEO_DATA_HEADER_SIZE,
230                        VIDEO_DATA_CHUNK_SIZE);
231
232                 if (current_sector == sector_count-1) {
233                     pkt->size= frame_size;
234                     *ret_pkt = *pkt;
235                     pkt->data= NULL;
236                     pkt->size= -1;
237                     return 0;
238                 }
239
240             }
241             break;
242
243         case CDXA_TYPE_AUDIO:
244             if(str->channels[channel].audio_stream_index < 0){
245                 int fmt = sector[0x13];
246                 /* allocate a new AVStream */
247                 st = avformat_new_stream(s, NULL);
248                 if (!st)
249                     return AVERROR(ENOMEM);
250
251                 str->channels[channel].audio_stream_index = st->index;
252
253                 st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
254                 st->codec->codec_id    = CODEC_ID_ADPCM_XA;
255                 st->codec->codec_tag   = 0;  /* no fourcc */
256                 st->codec->channels    = (fmt&1)?2:1;
257                 st->codec->sample_rate = (fmt&4)?18900:37800;
258             //    st->codec->bit_rate = 0; //FIXME;
259                 st->codec->block_align = 128;
260
261                 av_set_pts_info(st, 64, 128, st->codec->sample_rate);
262             }
263             pkt = ret_pkt;
264             if (av_new_packet(pkt, 2304))
265                 return AVERROR(EIO);
266             memcpy(pkt->data,sector+24,2304);
267
268             pkt->stream_index =
269                 str->channels[channel].audio_stream_index;
270             return 0;
271         default:
272             av_log(s, AV_LOG_WARNING, "Unknown sector type %02X\n", sector[0x12]);
273             /* drop the sector and move on */
274             break;
275         }
276
277         if (url_feof(pb))
278             return AVERROR(EIO);
279     }
280 }
281
282 static int str_read_close(AVFormatContext *s)
283 {
284     StrDemuxContext *str = s->priv_data;
285     int i;
286     for(i=0; i<32; i++){
287         if(str->channels[i].tmp_pkt.data)
288             av_free_packet(&str->channels[i].tmp_pkt);
289     }
290
291     return 0;
292 }
293
294 AVInputFormat ff_str_demuxer = {
295     .name           = "psxstr",
296     .long_name      = NULL_IF_CONFIG_SMALL("Sony Playstation STR format"),
297     .priv_data_size = sizeof(StrDemuxContext),
298     .read_probe     = str_probe,
299     .read_header    = str_read_header,
300     .read_packet    = str_read_packet,
301     .read_close     = str_read_close,
302 };