| 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 "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 | 7474 | static int str_probe(const AVProbeData *p) | |
| 75 | { | ||
| 76 | 7474 | const uint8_t *sector= p->buf; | |
| 77 | 7474 | const uint8_t *end= sector + p->buf_size; | |
| 78 | 7474 | int aud=0, vid=0; | |
| 79 | |||
| 80 |
2/2✓ Branch 0 taken 4122 times.
✓ Branch 1 taken 3352 times.
|
7474 | if (p->buf_size < RAW_CD_SECTOR_SIZE) |
| 81 | 4122 | return 0; | |
| 82 | |||
| 83 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3346 times.
|
3352 | if ((AV_RL32(&p->buf[0]) == RIFF_TAG) && |
| 84 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | (AV_RL32(&p->buf[8]) == CDXA_TAG)) { |
| 85 | |||
| 86 | /* RIFF header seen; skip 0x2C bytes */ | ||
| 87 | 6 | sector += RIFF_HEADER_SIZE; | |
| 88 | } | ||
| 89 | |||
| 90 |
2/2✓ Branch 0 taken 3373 times.
✓ Branch 1 taken 9 times.
|
3382 | while (end - sector >= RAW_CD_SECTOR_SIZE) { |
| 91 | /* look for CD sync header (00, 0xFF x 10, 00) */ | ||
| 92 |
2/2✓ Branch 0 taken 3343 times.
✓ Branch 1 taken 30 times.
|
3373 | if (memcmp(sector,sync_header,sizeof(sync_header))) |
| 93 | 3343 | return 0; | |
| 94 | |||
| 95 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (sector[0x11] >= 32) |
| 96 | ✗ | return 0; | |
| 97 | |||
| 98 |
1/3✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
30 | switch (sector[0x12] & CDXA_TYPE_MASK) { |
| 99 | 30 | case CDXA_TYPE_DATA: | |
| 100 | case CDXA_TYPE_VIDEO: { | ||
| 101 | 30 | int current_sector = AV_RL16(§or[0x1C]); | |
| 102 | 30 | int sector_count = AV_RL16(§or[0x1E]); | |
| 103 | 30 | int frame_size = AV_RL32(§or[0x24]); | |
| 104 | |||
| 105 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | if(!( frame_size>=0 |
| 106 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | && current_sector < sector_count |
| 107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | && sector_count*VIDEO_DATA_CHUNK_SIZE >=frame_size)){ |
| 108 | ✗ | return 0; | |
| 109 | } | ||
| 110 | 30 | vid++; | |
| 111 | |||
| 112 | } | ||
| 113 | 30 | 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 | 30 | 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 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
|
9 | if(vid+aud > 3) return AVPROBE_SCORE_EXTENSION; |
| 128 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | else if(vid+aud) return 1; |
| 129 | ✗ | else return 0; | |
| 130 | } | ||
| 131 | |||
| 132 | 3 | static int str_read_header(AVFormatContext *s) | |
| 133 | { | ||
| 134 | 3 | AVIOContext *pb = s->pb; | |
| 135 | 3 | 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 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if ((ret = ffio_read_size(pb, sector, RIFF_HEADER_SIZE)) < 0) |
| 143 | ✗ | return ret; | |
| 144 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if (AV_RL32(§or[0]) == RIFF_TAG) |
| 145 | 2 | start = RIFF_HEADER_SIZE; | |
| 146 | else | ||
| 147 | 1 | start = 0; | |
| 148 | |||
| 149 | 3 | avio_seek(pb, start, SEEK_SET); | |
| 150 | |||
| 151 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 3 times.
|
99 | for(i=0; i<32; i++){ |
| 152 | 96 | str->channels[i].video_stream_index= | |
| 153 | 96 | str->channels[i].audio_stream_index= -1; | |
| 154 | } | ||
| 155 | |||
| 156 | 3 | s->ctx_flags |= AVFMTCTX_NOHEADER; | |
| 157 | |||
| 158 | 3 | return 0; | |
| 159 | } | ||
| 160 | |||
| 161 | 397 | static int str_read_packet(AVFormatContext *s, | |
| 162 | AVPacket *ret_pkt) | ||
| 163 | { | ||
| 164 | 397 | AVIOContext *pb = s->pb; | |
| 165 | 397 | 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 | 1093 | while (1) { | |
| 172 | 1490 | int read = avio_read(pb, sector, RAW_CD_SECTOR_SIZE); | |
| 173 | |||
| 174 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1488 times.
|
1490 | if (read == AVERROR_EOF) |
| 175 | 2 | return AVERROR_EOF; | |
| 176 | |||
| 177 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1485 times.
|
1488 | if (read != RAW_CD_SECTOR_SIZE) |
| 178 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | return read < 0 ? read : AVERROR_INVALIDDATA; |
| 179 | |||
| 180 | 1485 | channel = sector[0x11]; | |
| 181 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1485 times.
|
1485 | if (channel >= 32) |
| 182 | ✗ | return AVERROR_INVALIDDATA; | |
| 183 | |||
| 184 |
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) { |
| 185 | |||
| 186 | 1300 | case CDXA_TYPE_DATA: | |
| 187 | case CDXA_TYPE_VIDEO: | ||
| 188 | { | ||
| 189 | |||
| 190 | 1300 | int current_sector = AV_RL16(§or[0x1C]); | |
| 191 | 1300 | int sector_count = AV_RL16(§or[0x1E]); | |
| 192 | 1300 | int frame_size = AV_RL32(§or[0x24]); | |
| 193 | |||
| 194 |
1/2✓ Branch 0 taken 1300 times.
✗ Branch 1 not taken.
|
1300 | if(!( frame_size>=0 |
| 195 |
1/2✓ Branch 0 taken 1300 times.
✗ Branch 1 not taken.
|
1300 | && current_sector < sector_count |
| 196 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1300 times.
|
1300 | && 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 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1297 times.
|
1300 | if(str->channels[channel].video_stream_index < 0){ |
| 202 | /* allocate a new AVStream */ | ||
| 203 | 3 | st = avformat_new_stream(s, NULL); | |
| 204 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!st) |
| 205 | ✗ | return AVERROR(ENOMEM); | |
| 206 | 3 | avpriv_set_pts_info(st, 64, 1, 15); | |
| 207 | |||
| 208 | 3 | str->channels[channel].video_stream_index = st->index; | |
| 209 | |||
| 210 | 3 | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
| 211 | 3 | st->codecpar->codec_id = AV_CODEC_ID_MDEC; | |
| 212 | 3 | st->codecpar->codec_tag = 0; /* no fourcc */ | |
| 213 | 3 | st->codecpar->width = AV_RL16(§or[0x28]); | |
| 214 | 3 | st->codecpar->height = AV_RL16(§or[0x2A]); | |
| 215 | } | ||
| 216 | |||
| 217 | /* if this is the first sector of the frame, allocate a pkt */ | ||
| 218 | 1300 | pkt = &str->channels[channel].tmp_pkt; | |
| 219 | |||
| 220 |
2/2✓ Branch 0 taken 210 times.
✓ Branch 1 taken 1090 times.
|
1300 | if(pkt->size != sector_count*VIDEO_DATA_CHUNK_SIZE){ |
| 221 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
|
210 | if(pkt->data) |
| 222 | ✗ | av_log(s, AV_LOG_ERROR, "mismatching sector_count\n"); | |
| 223 | 210 | av_packet_unref(pkt); | |
| 224 | 210 | ret = av_new_packet(pkt, sector_count * VIDEO_DATA_CHUNK_SIZE); | |
| 225 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
|
210 | if (ret < 0) |
| 226 | ✗ | return ret; | |
| 227 | 210 | memset(pkt->data, 0, sector_count*VIDEO_DATA_CHUNK_SIZE); | |
| 228 | |||
| 229 | 210 | pkt->pos= avio_tell(pb) - RAW_CD_SECTOR_SIZE; | |
| 230 | 210 | pkt->stream_index = | |
| 231 | 210 | str->channels[channel].video_stream_index; | |
| 232 | } | ||
| 233 | |||
| 234 | 1300 | memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE, | |
| 235 | sector + VIDEO_DATA_HEADER_SIZE, | ||
| 236 | VIDEO_DATA_CHUNK_SIZE); | ||
| 237 | |||
| 238 |
2/2✓ Branch 0 taken 207 times.
✓ Branch 1 taken 1093 times.
|
1300 | if (current_sector == sector_count-1) { |
| 239 | 207 | pkt->size= frame_size; | |
| 240 | 207 | *ret_pkt = *pkt; | |
| 241 | 207 | pkt->data= NULL; | |
| 242 | 207 | pkt->size= -1; | |
| 243 | 207 | pkt->buf = NULL; | |
| 244 | 207 | return 0; | |
| 245 | } | ||
| 246 | |||
| 247 | } | ||
| 248 | 1093 | break; | |
| 249 | |||
| 250 | 185 | case CDXA_TYPE_AUDIO: | |
| 251 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 182 times.
|
185 | if(str->channels[channel].audio_stream_index < 0){ |
| 252 | 3 | int fmt = sector[0x13]; | |
| 253 | /* allocate a new AVStream */ | ||
| 254 | 3 | st = avformat_new_stream(s, NULL); | |
| 255 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!st) |
| 256 | ✗ | return AVERROR(ENOMEM); | |
| 257 | |||
| 258 | 3 | str->channels[channel].audio_stream_index = st->index; | |
| 259 | |||
| 260 | 3 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 261 | 3 | st->codecpar->codec_id = AV_CODEC_ID_ADPCM_XA; | |
| 262 | 3 | st->codecpar->codec_tag = 0; /* no fourcc */ | |
| 263 | 3 | av_channel_layout_default(&st->codecpar->ch_layout, (fmt & 1) + 1); | |
| 264 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | st->codecpar->sample_rate = (fmt&4)?18900:37800; |
| 265 | // st->codecpar->bit_rate = 0; //FIXME; | ||
| 266 | 3 | st->codecpar->block_align = 128; | |
| 267 | |||
| 268 | 3 | avpriv_set_pts_info(st, 64, 18 * 224 / st->codecpar->ch_layout.nb_channels, | |
| 269 | 3 | st->codecpar->sample_rate); | |
| 270 | 3 | st->start_time = 0; | |
| 271 | } | ||
| 272 | 185 | pkt = ret_pkt; | |
| 273 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
|
185 | if ((ret = av_new_packet(pkt, 2304)) < 0) |
| 274 | ✗ | return ret; | |
| 275 | 185 | memcpy(pkt->data,sector+24,2304); | |
| 276 | |||
| 277 | 185 | pkt->stream_index = | |
| 278 | 185 | str->channels[channel].audio_stream_index; | |
| 279 | 185 | pkt->duration = 1; | |
| 280 | 185 | 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 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1093 times.
|
1093 | if (avio_feof(pb)) |
| 292 | ✗ | return AVERROR_INVALIDDATA; | |
| 293 | } | ||
| 294 | } | ||
| 295 | |||
| 296 | 3 | static int str_read_close(AVFormatContext *s) | |
| 297 | { | ||
| 298 | 3 | StrDemuxContext *str = s->priv_data; | |
| 299 | int i; | ||
| 300 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 3 times.
|
99 | for(i=0; i<32; i++){ |
| 301 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 93 times.
|
96 | if(str->channels[i].tmp_pkt.data) |
| 302 | 3 | av_packet_unref(&str->channels[i].tmp_pkt); | |
| 303 | } | ||
| 304 | |||
| 305 | 3 | 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 | }; | ||
| 318 |