tools/sofa2wavs: fix build on Windows
[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
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/channel_layout.h"
33 #include "libavutil/internal.h"
34 #include "libavutil/intreadwrite.h"
35 #include "avformat.h"
36 #include "avio_internal.h"
37 #include "demux.h"
38 #include "internal.h"
39
40 #define RIFF_TAG MKTAG('R', 'I', 'F', 'F')
41 #define CDXA_TAG MKTAG('C', 'D', 'X', 'A')
42
43 #define RAW_CD_SECTOR_SIZE 2352
44 #define RAW_CD_SECTOR_DATA_SIZE 2304
45 #define VIDEO_DATA_CHUNK_SIZE 0x7E0
46 #define VIDEO_DATA_HEADER_SIZE 0x38
47 #define RIFF_HEADER_SIZE 0x2C
48
49 #define CDXA_TYPE_MASK 0x0E
50 #define CDXA_TYPE_DATA 0x08
51 #define CDXA_TYPE_AUDIO 0x04
52 #define CDXA_TYPE_VIDEO 0x02
53 #define CDXA_TYPE_EMPTY 0x00
54
55 #define STR_MAGIC (0x80010160)
56
57 typedef struct StrChannel {
58 /* video parameters */
59 int video_stream_index;
60 AVPacket tmp_pkt;
61
62 /* audio parameters */
63 int audio_stream_index;
64 } StrChannel;
65
66 typedef struct StrDemuxContext {
67
68 /* a STR file can contain up to 32 channels of data */
69 StrChannel channels[32];
70 } StrDemuxContext;
71
72 static const uint8_t sync_header[12] = {0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00};
73
74 static int str_probe(const AVProbeData *p)
75 {
76 const uint8_t *sector= p->buf;
77 const uint8_t *end= sector + p->buf_size;
78 int aud=0, vid=0;
79
80 if (p->buf_size < RAW_CD_SECTOR_SIZE)
81 return 0;
82
83 if ((AV_RL32(&p->buf[0]) == RIFF_TAG) &&
84 (AV_RL32(&p->buf[8]) == CDXA_TAG)) {
85
86 /* RIFF header seen; skip 0x2C bytes */
87 sector += RIFF_HEADER_SIZE;
88 }
89
90 while (end - sector >= RAW_CD_SECTOR_SIZE) {
91 /* look for CD sync header (00, 0xFF x 10, 00) */
92 if (memcmp(sector,sync_header,sizeof(sync_header)))
93 return 0;
94
95 if (sector[0x11] >= 32)
96 return 0;
97
98 switch (sector[0x12] & CDXA_TYPE_MASK) {
99 case CDXA_TYPE_DATA:
100 case CDXA_TYPE_VIDEO: {
101 int current_sector = AV_RL16(&sector[0x1C]);
102 int sector_count = AV_RL16(&sector[0x1E]);
103 int frame_size = AV_RL32(&sector[0x24]);
104
105 if(!( frame_size>=0
106 && current_sector < sector_count
107 && sector_count*VIDEO_DATA_CHUNK_SIZE >=frame_size)){
108 return 0;
109 }
110 vid++;
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 AVPROBE_SCORE_EXTENSION;
128 else if(vid+aud) return 1;
129 else return 0;
130 }
131
132 static int str_read_header(AVFormatContext *s)
133 {
134 AVIOContext *pb = s->pb;
135 StrDemuxContext *str = s->priv_data;
136 unsigned char sector[RAW_CD_SECTOR_SIZE];
137 int start;
138 int ret;
139 int i;
140
141 /* skip over any RIFF header */
142 if ((ret = ffio_read_size(pb, sector, RIFF_HEADER_SIZE)) < 0)
143 return ret;
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, ret;
168 AVPacket *pkt;
169 AVStream *st;
170
171 while (1) {
172 int read = avio_read(pb, sector, RAW_CD_SECTOR_SIZE);
173
174 if (read == AVERROR_EOF)
175 return AVERROR_EOF;
176
177 if (read != RAW_CD_SECTOR_SIZE)
178 return read < 0 ? read : AVERROR_INVALIDDATA;
179
180 channel = sector[0x11];
181 if (channel >= 32)
182 return AVERROR_INVALIDDATA;
183
184 switch (sector[0x12] & CDXA_TYPE_MASK) {
185
186 case CDXA_TYPE_DATA:
187 case CDXA_TYPE_VIDEO:
188 {
189
190 int current_sector = AV_RL16(&sector[0x1C]);
191 int sector_count = AV_RL16(&sector[0x1E]);
192 int frame_size = AV_RL32(&sector[0x24]);
193
194 if(!( frame_size>=0
195 && current_sector < sector_count
196 && sector_count*VIDEO_DATA_CHUNK_SIZE >=frame_size)){
197 av_log(s, AV_LOG_ERROR, "Invalid parameters %d %d %d\n", current_sector, sector_count, frame_size);
198 break;
199 }
200
201 if(str->channels[channel].video_stream_index < 0){
202 /* allocate a new AVStream */
203 st = avformat_new_stream(s, NULL);
204 if (!st)
205 return AVERROR(ENOMEM);
206 avpriv_set_pts_info(st, 64, 1, 15);
207
208 str->channels[channel].video_stream_index = st->index;
209
210 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
211 st->codecpar->codec_id = AV_CODEC_ID_MDEC;
212 st->codecpar->codec_tag = 0; /* no fourcc */
213 st->codecpar->width = AV_RL16(&sector[0x28]);
214 st->codecpar->height = AV_RL16(&sector[0x2A]);
215 }
216
217 /* if this is the first sector of the frame, allocate a pkt */
218 pkt = &str->channels[channel].tmp_pkt;
219
220 if(pkt->size != sector_count*VIDEO_DATA_CHUNK_SIZE){
221 if(pkt->data)
222 av_log(s, AV_LOG_ERROR, "mismatching sector_count\n");
223 av_packet_unref(pkt);
224 ret = av_new_packet(pkt, sector_count * VIDEO_DATA_CHUNK_SIZE);
225 if (ret < 0)
226 return ret;
227 memset(pkt->data, 0, sector_count*VIDEO_DATA_CHUNK_SIZE);
228
229 pkt->pos= avio_tell(pb) - RAW_CD_SECTOR_SIZE;
230 pkt->stream_index =
231 str->channels[channel].video_stream_index;
232 }
233
234 memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE,
235 sector + VIDEO_DATA_HEADER_SIZE,
236 VIDEO_DATA_CHUNK_SIZE);
237
238 if (current_sector == sector_count-1) {
239 pkt->size= frame_size;
240 *ret_pkt = *pkt;
241 pkt->data= NULL;
242 pkt->size= -1;
243 pkt->buf = NULL;
244 return 0;
245 }
246
247 }
248 break;
249
250 case CDXA_TYPE_AUDIO:
251 if(str->channels[channel].audio_stream_index < 0){
252 int fmt = sector[0x13];
253 /* allocate a new AVStream */
254 st = avformat_new_stream(s, NULL);
255 if (!st)
256 return AVERROR(ENOMEM);
257
258 str->channels[channel].audio_stream_index = st->index;
259
260 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
261 st->codecpar->codec_id = AV_CODEC_ID_ADPCM_XA;
262 st->codecpar->codec_tag = 0; /* no fourcc */
263 av_channel_layout_default(&st->codecpar->ch_layout, (fmt & 1) + 1);
264 st->codecpar->sample_rate = (fmt&4)?18900:37800;
265 // st->codecpar->bit_rate = 0; //FIXME;
266 st->codecpar->block_align = 128;
267
268 avpriv_set_pts_info(st, 64, 18 * 224 / st->codecpar->ch_layout.nb_channels,
269 st->codecpar->sample_rate);
270 st->start_time = 0;
271 }
272 pkt = ret_pkt;
273 if ((ret = av_new_packet(pkt, 2304)) < 0)
274 return ret;
275 memcpy(pkt->data,sector+24,2304);
276
277 pkt->stream_index =
278 str->channels[channel].audio_stream_index;
279 pkt->duration = 1;
280 return 0;
281 case CDXA_TYPE_EMPTY: /* CD-ROM XA, May 1991, 4.3.2.3 */
282 /* NOTE this also catches 0x80 (EOF bit) because of CDXA_TYPE_MASK */
283 /* TODO consider refactoring so as to explicitly handle each case? */
284 break;
285 default:
286 av_log(s, AV_LOG_WARNING, "Unknown sector type %02X\n", sector[0x12]);
287 /* drop the sector and move on */
288 break;
289 }
290
291 if (avio_feof(pb))
292 return AVERROR_INVALIDDATA;
293 }
294 }
295
296 static int str_read_close(AVFormatContext *s)
297 {
298 StrDemuxContext *str = s->priv_data;
299 int i;
300 for(i=0; i<32; i++){
301 if(str->channels[i].tmp_pkt.data)
302 av_packet_unref(&str->channels[i].tmp_pkt);
303 }
304
305 return 0;
306 }
307
308 const FFInputFormat ff_str_demuxer = {
309 .p.name = "psxstr",
310 .p.long_name = NULL_IF_CONFIG_SMALL("Sony Playstation STR"),
311 .p.flags = AVFMT_NO_BYTE_SEEK,
312 .priv_data_size = sizeof(StrDemuxContext),
313 .read_probe = str_probe,
314 .read_header = str_read_header,
315 .read_packet = str_read_packet,
316 .read_close = str_read_close,
317 };