1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
|
/* Copyright (C) 2010-2020 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (chd_stream.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <boolean.h>
#include <streams/chd_stream.h>
#include <retro_endianness.h>
#include <libchdr/chd.h>
#include <string/stdstring.h>
#define SECTOR_SIZE 2352
#define SUBCODE_SIZE 96
#define TRACK_PAD 4
struct chdstream
{
chd_file *chd;
/* Loaded hunk */
uint8_t *hunkmem;
/* Byte offset where track data starts (after pregap) */
size_t track_start;
/* Byte offset where track data ends */
size_t track_end;
/* Byte offset of read cursor */
size_t offset;
/* Loaded hunk number */
int32_t hunknum;
/* Size of frame taken from each hunk */
uint32_t frame_size;
/* Offset of data within frame */
uint32_t frame_offset;
/* Number of frames per hunk */
uint32_t frames_per_hunk;
/* First frame of track in chd */
uint32_t track_frame;
/* Should we swap bytes? */
bool swab;
};
typedef struct metadata
{
uint32_t frame_offset;
uint32_t frames;
uint32_t pad;
uint32_t extra;
uint32_t pregap;
uint32_t postgap;
uint32_t track;
char type[64];
char subtype[32];
char pgtype[32];
char pgsub[32];
} metadata_t;
static uint32_t padding_frames(uint32_t frames)
{
return ((frames + TRACK_PAD - 1) & ~(TRACK_PAD - 1)) - frames;
}
static bool
chdstream_get_meta(chd_file *chd, int idx, metadata_t *md)
{
char meta[256];
uint32_t meta_size = 0;
chd_error err;
meta[0] = '\0';
memset(md, 0, sizeof(*md));
err = chd_get_metadata(chd, CDROM_TRACK_METADATA2_TAG, idx, meta,
sizeof(meta), &meta_size, NULL, NULL);
if (err == CHDERR_NONE)
{
sscanf(meta, CDROM_TRACK_METADATA2_FORMAT,
&md->track, md->type,
md->subtype, &md->frames, &md->pregap,
md->pgtype, md->pgsub,
&md->postgap);
md->extra = padding_frames(md->frames);
return true;
}
err = chd_get_metadata(chd, CDROM_TRACK_METADATA_TAG, idx, meta,
sizeof(meta), &meta_size, NULL, NULL);
if (err == CHDERR_NONE)
{
sscanf(meta, CDROM_TRACK_METADATA_FORMAT, &md->track, md->type,
md->subtype, &md->frames);
md->extra = padding_frames(md->frames);
return true;
}
err = chd_get_metadata(chd, GDROM_TRACK_METADATA_TAG, idx, meta,
sizeof(meta), &meta_size, NULL, NULL);
if (err == CHDERR_NONE)
{
sscanf(meta, GDROM_TRACK_METADATA_FORMAT, &md->track, md->type,
md->subtype, &md->frames, &md->pad, &md->pregap, md->pgtype,
md->pgsub, &md->postgap);
md->extra = padding_frames(md->frames);
return true;
}
return false;
}
static bool
chdstream_find_track_number(chd_file *fd, int32_t track, metadata_t *meta)
{
uint32_t i;
uint32_t frame_offset = 0;
for (i = 0; true; ++i)
{
if (!chdstream_get_meta(fd, i, meta))
return false;
if (track == (int)meta->track)
{
meta->frame_offset = frame_offset;
return true;
}
frame_offset += meta->frames + meta->extra;
}
}
static bool
chdstream_find_special_track(chd_file *fd, int32_t track, metadata_t *meta)
{
int32_t i;
metadata_t iter;
int32_t largest_track = 0;
uint32_t largest_size = 0;
for (i = 1; true; ++i)
{
if (!chdstream_find_track_number(fd, i, &iter))
{
if (track == CHDSTREAM_TRACK_LAST && i > 1)
return chdstream_find_track_number(fd, i - 1, meta);
if (track == CHDSTREAM_TRACK_PRIMARY && largest_track != 0)
return chdstream_find_track_number(fd, largest_track, meta);
return false;
}
switch (track)
{
case CHDSTREAM_TRACK_FIRST_DATA:
if (strcmp(iter.type, "AUDIO"))
{
*meta = iter;
return true;
}
break;
case CHDSTREAM_TRACK_PRIMARY:
if (strcmp(iter.type, "AUDIO") && iter.frames > largest_size)
{
largest_size = iter.frames;
largest_track = iter.track;
}
break;
default:
break;
}
}
}
static bool
chdstream_find_track(chd_file *fd, int32_t track, metadata_t *meta)
{
if (track < 0)
return chdstream_find_special_track(fd, track, meta);
return chdstream_find_track_number(fd, track, meta);
}
chdstream_t *chdstream_open(const char *path, int32_t track)
{
metadata_t meta;
uint32_t pregap = 0;
uint8_t *hunkmem = NULL;
const chd_header *hd = NULL;
chdstream_t *stream = NULL;
chd_file *chd = NULL;
chd_error err = chd_open(path, CHD_OPEN_READ, NULL, &chd);
if (err != CHDERR_NONE)
return NULL;
if (!chdstream_find_track(chd, track, &meta))
goto error;
stream = (chdstream_t*)malloc(sizeof(*stream));
if (!stream)
goto error;
stream->chd = NULL;
stream->swab = false;
stream->frame_size = 0;
stream->frame_offset = 0;
stream->frames_per_hunk = 0;
stream->track_frame = 0;
stream->track_start = 0;
stream->track_end = 0;
stream->offset = 0;
stream->hunkmem = NULL;
stream->hunknum = -1;
hd = chd_get_header(chd);
hunkmem = (uint8_t*)malloc(hd->hunkbytes);
if (!hunkmem)
goto error;
stream->hunkmem = hunkmem;
if (string_is_equal(meta.type, "MODE1_RAW"))
stream->frame_size = SECTOR_SIZE;
else if (string_is_equal(meta.type, "MODE2_RAW"))
stream->frame_size = SECTOR_SIZE;
else if (string_is_equal(meta.type, "AUDIO"))
{
stream->frame_size = SECTOR_SIZE;
stream->swab = true;
}
else
stream->frame_size = hd->unitbytes;
/* Only include pregap data if it was in the track file */
if (meta.pgtype[0] != 'V')
pregap = meta.pregap;
stream->chd = chd;
stream->frames_per_hunk = hd->hunkbytes / hd->unitbytes;
stream->track_frame = meta.frame_offset;
stream->track_start = (size_t)pregap * stream->frame_size;
stream->track_end = stream->track_start +
(size_t)meta.frames * stream->frame_size;
return stream;
error:
chdstream_close(stream);
if (chd)
chd_close(chd);
return NULL;
}
void chdstream_close(chdstream_t *stream)
{
if (!stream)
return;
if (stream->hunkmem)
free(stream->hunkmem);
if (stream->chd)
chd_close(stream->chd);
free(stream);
}
static bool
chdstream_load_hunk(chdstream_t *stream, uint32_t hunknum)
{
uint16_t *array;
if ((int)hunknum == stream->hunknum)
return true;
if (chd_read(stream->chd, hunknum, stream->hunkmem) != CHDERR_NONE)
return false;
if (stream->swab)
{
uint32_t i;
uint32_t count = chd_get_header(stream->chd)->hunkbytes / 2;
array = (uint16_t*)stream->hunkmem;
for (i = 0; i < count; ++i)
array[i] = SWAP16(array[i]);
}
stream->hunknum = hunknum;
return true;
}
ssize_t chdstream_read(chdstream_t *stream, void *data, size_t bytes)
{
size_t end;
size_t data_offset = 0;
const chd_header *hd = chd_get_header(stream->chd);
uint8_t *out = (uint8_t*)data;
if (stream->track_end - stream->offset < bytes)
bytes = stream->track_end - stream->offset;
end = stream->offset + bytes;
while (stream->offset < end)
{
uint32_t frame_offset = stream->offset % stream->frame_size;
uint32_t amount = stream->frame_size - frame_offset;
if (amount > end - stream->offset)
amount = (uint32_t)(end - stream->offset);
/* In pregap */
if (stream->offset < stream->track_start)
memset(out + data_offset, 0, amount);
else
{
uint32_t chd_frame = (uint32_t)(stream->track_frame +
(stream->offset - stream->track_start) / stream->frame_size);
uint32_t hunk = chd_frame / stream->frames_per_hunk;
uint32_t hunk_offset = (chd_frame % stream->frames_per_hunk)
* hd->unitbytes;
if (!chdstream_load_hunk(stream, hunk))
return -1;
memcpy(out + data_offset,
stream->hunkmem + frame_offset
+ hunk_offset + stream->frame_offset, amount);
}
data_offset += amount;
stream->offset += amount;
}
return bytes;
}
int chdstream_getc(chdstream_t *stream)
{
char c = 0;
if (chdstream_read(stream, &c, sizeof(c) != sizeof(c)))
return EOF;
return c;
}
char *chdstream_gets(chdstream_t *stream, char *buffer, size_t len)
{
int c;
size_t offset = 0;
while (offset < len && (c = chdstream_getc(stream)) != EOF)
buffer[offset++] = c;
if (offset < len)
buffer[offset] = '\0';
return buffer;
}
uint64_t chdstream_tell(chdstream_t *stream)
{
return stream->offset;
}
void chdstream_rewind(chdstream_t *stream)
{
stream->offset = 0;
}
int64_t chdstream_seek(chdstream_t *stream, int64_t offset, int whence)
{
int64_t new_offset;
switch (whence)
{
case SEEK_SET:
new_offset = offset;
break;
case SEEK_CUR:
new_offset = stream->offset + offset;
break;
case SEEK_END:
new_offset = stream->track_end + offset;
break;
default:
return -1;
}
if (new_offset < 0)
return -1;
if ((size_t)new_offset > stream->track_end)
new_offset = stream->track_end;
stream->offset = new_offset;
return 0;
}
ssize_t chdstream_get_size(chdstream_t *stream)
{
return stream->track_end - stream->track_start;
}
uint32_t chdstream_get_track_start(chdstream_t *stream)
{
uint32_t i;
metadata_t meta;
uint32_t frame_offset = 0;
for (i = 0; chdstream_get_meta(stream->chd, i, &meta); ++i)
{
if (stream->track_frame == frame_offset)
return meta.pregap * stream->frame_size;
frame_offset += meta.frames + meta.extra;
}
return 0;
}
uint32_t chdstream_get_frame_size(chdstream_t *stream)
{
return stream->frame_size;
}
uint32_t chdstream_get_first_track_sector(chdstream_t* stream)
{
uint32_t i;
metadata_t meta;
uint32_t frame_offset = 0;
uint32_t sector_offset = 0;
for (i = 0; chdstream_get_meta(stream->chd, i, &meta); ++i)
{
if (stream->track_frame == frame_offset)
return sector_offset;
sector_offset += meta.frames;
frame_offset += meta.frames + meta.extra;
}
return 0;
}
|