FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/psxstr.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 116 142 81.7%
Functions: 4 4 100.0%
Branches: 51 77 66.2%

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