FFmpeg
Loading...
Searching...
No Matches
psxstr.c
Go to the documentation of this file.
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
33#include "libavutil/internal.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
57typedef struct StrChannel {
58 /* video parameters */
61
62 /* audio parameters */
65
66typedef struct StrDemuxContext {
67
68 /* a STR file can contain up to 32 channels of data */
71
72static const uint8_t sync_header[12] = {0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00};
73
74static 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
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++){
153 str->channels[i].audio_stream_index= -1;
154 }
155
156 s->ctx_flags |= AVFMTCTX_NOHEADER;
157
158 return 0;
159}
160
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
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 */
204 if (!st)
205 return AVERROR(ENOMEM);
206 avpriv_set_pts_info(st, 64, 1, 15);
207
209
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");
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 =
232 }
233
234 memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE,
235 sector + VIDEO_DATA_HEADER_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 */
255 if (!st)
256 return AVERROR(ENOMEM);
257
259
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 =
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
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)
303 }
304
305 return 0;
306}
307
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),
317};
const FFInputFormat ff_str_demuxer
Definition psxstr.c:308
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition avformat.h:1284
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition avformat.h:506
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:481
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static uint32_t BS_FUNC read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static int FUNC aud(CodedBitstreamContext *ctx, RWContext *rw, H264RawAUD *current)
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
static AVPacket * pkt
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition ebur128.h:39
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
static const uint8_t frame_size[4]
Definition g723_1.h:222
@ AV_CODEC_ID_ADPCM_XA
Definition codec_id.h:378
@ AV_CODEC_ID_MDEC
Definition codec_id.h:87
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_RL32(p)
#define AV_RL16(p)
#define RIFF_TAG
Definition 4xm.c:39
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
#define CDXA_TYPE_DATA
Definition psxstr.c:50
#define CDXA_TYPE_AUDIO
Definition psxstr.c:51
static const uint8_t sync_header[12]
Definition psxstr.c:72
static int str_read_header(AVFormatContext *s)
Definition psxstr.c:132
#define RIFF_HEADER_SIZE
Definition psxstr.c:47
#define CDXA_TYPE_MASK
Definition psxstr.c:49
#define CDXA_TYPE_VIDEO
Definition psxstr.c:52
#define RAW_CD_SECTOR_SIZE
Definition psxstr.c:43
static int str_probe(const AVProbeData *p)
Definition psxstr.c:74
#define CDXA_TYPE_EMPTY
Definition psxstr.c:53
static int str_read_close(AVFormatContext *s)
Definition psxstr.c:296
#define VIDEO_DATA_CHUNK_SIZE
Definition psxstr.c:45
#define VIDEO_DATA_HEADER_SIZE
Definition psxstr.c:46
#define CDXA_TAG
Definition psxstr.c:41
static int str_read_packet(AVFormatContext *s, AVPacket *ret_pkt)
Definition psxstr.c:161
int nb_channels
Number of channels in this layout.
int height
The height of the video frame in pixels.
Definition codec_par.h:150
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
uint8_t * data
Definition packet.h:603
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int index
stream index in AVFormatContext
Definition avformat.h:772
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
int video_stream_index
Definition psxstr.c:59
int audio_stream_index
Definition psxstr.c:63
AVPacket tmp_pkt
Definition psxstr.c:60
StrChannel channels[32]
Definition psxstr.c:69
#define av_log(a,...)