1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (chd_stream.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #include <streams/chd_stream.h>
30 #include <retro_endianness.h>
31 #include <libchdr/chd.h>
32 #include <string/stdstring.h>
34 #define SECTOR_SIZE 2352
35 #define SUBCODE_SIZE 96
43 /* Byte offset where track data starts (after pregap) */
45 /* Byte offset where track data ends */
47 /* Byte offset of read cursor */
49 /* Loaded hunk number */
51 /* Size of frame taken from each hunk */
53 /* Offset of data within frame */
54 uint32_t frame_offset;
55 /* Number of frames per hunk */
56 uint32_t frames_per_hunk;
57 /* First frame of track in chd */
59 /* Should we swap bytes? */
63 typedef struct metadata
65 uint32_t frame_offset;
78 static uint32_t padding_frames(uint32_t frames)
80 return ((frames + TRACK_PAD - 1) & ~(TRACK_PAD - 1)) - frames;
84 chdstream_get_meta(chd_file *chd, int idx, metadata_t *md)
87 uint32_t meta_size = 0;
92 memset(md, 0, sizeof(*md));
94 err = chd_get_metadata(chd, CDROM_TRACK_METADATA2_TAG, idx, meta,
95 sizeof(meta), &meta_size, NULL, NULL);
97 if (err == CHDERR_NONE)
99 sscanf(meta, CDROM_TRACK_METADATA2_FORMAT,
100 &md->track, md->type,
101 md->subtype, &md->frames, &md->pregap,
102 md->pgtype, md->pgsub,
104 md->extra = padding_frames(md->frames);
108 err = chd_get_metadata(chd, CDROM_TRACK_METADATA_TAG, idx, meta,
109 sizeof(meta), &meta_size, NULL, NULL);
111 if (err == CHDERR_NONE)
113 sscanf(meta, CDROM_TRACK_METADATA_FORMAT, &md->track, md->type,
114 md->subtype, &md->frames);
115 md->extra = padding_frames(md->frames);
119 err = chd_get_metadata(chd, GDROM_TRACK_METADATA_TAG, idx, meta,
120 sizeof(meta), &meta_size, NULL, NULL);
122 if (err == CHDERR_NONE)
124 sscanf(meta, GDROM_TRACK_METADATA_FORMAT, &md->track, md->type,
125 md->subtype, &md->frames, &md->pad, &md->pregap, md->pgtype,
126 md->pgsub, &md->postgap);
127 md->extra = padding_frames(md->frames);
135 chdstream_find_track_number(chd_file *fd, int32_t track, metadata_t *meta)
138 uint32_t frame_offset = 0;
140 for (i = 0; true; ++i)
142 if (!chdstream_get_meta(fd, i, meta))
145 if (track == (int)meta->track)
147 meta->frame_offset = frame_offset;
151 frame_offset += meta->frames + meta->extra;
156 chdstream_find_special_track(chd_file *fd, int32_t track, metadata_t *meta)
160 int32_t largest_track = 0;
161 uint32_t largest_size = 0;
163 for (i = 1; true; ++i)
165 if (!chdstream_find_track_number(fd, i, &iter))
167 if (track == CHDSTREAM_TRACK_LAST && i > 1)
168 return chdstream_find_track_number(fd, i - 1, meta);
170 if (track == CHDSTREAM_TRACK_PRIMARY && largest_track != 0)
171 return chdstream_find_track_number(fd, largest_track, meta);
178 case CHDSTREAM_TRACK_FIRST_DATA:
179 if (strcmp(iter.type, "AUDIO"))
185 case CHDSTREAM_TRACK_PRIMARY:
186 if (strcmp(iter.type, "AUDIO") && iter.frames > largest_size)
188 largest_size = iter.frames;
189 largest_track = iter.track;
199 chdstream_find_track(chd_file *fd, int32_t track, metadata_t *meta)
202 return chdstream_find_special_track(fd, track, meta);
203 return chdstream_find_track_number(fd, track, meta);
206 chdstream_t *chdstream_open(const char *path, int32_t track)
210 uint8_t *hunkmem = NULL;
211 const chd_header *hd = NULL;
212 chdstream_t *stream = NULL;
213 chd_file *chd = NULL;
214 chd_error err = chd_open(path, CHD_OPEN_READ, NULL, &chd);
216 if (err != CHDERR_NONE)
219 if (!chdstream_find_track(chd, track, &meta))
222 stream = (chdstream_t*)malloc(sizeof(*stream));
227 stream->swab = false;
228 stream->frame_size = 0;
229 stream->frame_offset = 0;
230 stream->frames_per_hunk = 0;
231 stream->track_frame = 0;
232 stream->track_start = 0;
233 stream->track_end = 0;
235 stream->hunkmem = NULL;
236 stream->hunknum = -1;
238 hd = chd_get_header(chd);
239 hunkmem = (uint8_t*)malloc(hd->hunkbytes);
243 stream->hunkmem = hunkmem;
245 if (string_is_equal(meta.type, "MODE1_RAW"))
246 stream->frame_size = SECTOR_SIZE;
247 else if (string_is_equal(meta.type, "MODE2_RAW"))
248 stream->frame_size = SECTOR_SIZE;
249 else if (string_is_equal(meta.type, "AUDIO"))
251 stream->frame_size = SECTOR_SIZE;
255 stream->frame_size = hd->unitbytes;
257 /* Only include pregap data if it was in the track file */
258 if (meta.pgtype[0] != 'V')
259 pregap = meta.pregap;
262 stream->frames_per_hunk = hd->hunkbytes / hd->unitbytes;
263 stream->track_frame = meta.frame_offset;
264 stream->track_start = (size_t)pregap * stream->frame_size;
265 stream->track_end = stream->track_start +
266 (size_t)meta.frames * stream->frame_size;
272 chdstream_close(stream);
280 void chdstream_close(chdstream_t *stream)
286 free(stream->hunkmem);
288 chd_close(stream->chd);
293 chdstream_load_hunk(chdstream_t *stream, uint32_t hunknum)
297 if ((int)hunknum == stream->hunknum)
300 if (chd_read(stream->chd, hunknum, stream->hunkmem) != CHDERR_NONE)
306 uint32_t count = chd_get_header(stream->chd)->hunkbytes / 2;
307 array = (uint16_t*)stream->hunkmem;
308 for (i = 0; i < count; ++i)
309 array[i] = SWAP16(array[i]);
312 stream->hunknum = hunknum;
316 ssize_t chdstream_read(chdstream_t *stream, void *data, size_t bytes)
319 size_t data_offset = 0;
320 const chd_header *hd = chd_get_header(stream->chd);
321 uint8_t *out = (uint8_t*)data;
323 if (stream->track_end - stream->offset < bytes)
324 bytes = stream->track_end - stream->offset;
326 end = stream->offset + bytes;
328 while (stream->offset < end)
330 uint32_t frame_offset = stream->offset % stream->frame_size;
331 uint32_t amount = stream->frame_size - frame_offset;
333 if (amount > end - stream->offset)
334 amount = (uint32_t)(end - stream->offset);
337 if (stream->offset < stream->track_start)
338 memset(out + data_offset, 0, amount);
341 uint32_t chd_frame = (uint32_t)(stream->track_frame +
342 (stream->offset - stream->track_start) / stream->frame_size);
343 uint32_t hunk = chd_frame / stream->frames_per_hunk;
344 uint32_t hunk_offset = (chd_frame % stream->frames_per_hunk)
347 if (!chdstream_load_hunk(stream, hunk))
350 memcpy(out + data_offset,
351 stream->hunkmem + frame_offset
352 + hunk_offset + stream->frame_offset, amount);
355 data_offset += amount;
356 stream->offset += amount;
362 int chdstream_getc(chdstream_t *stream)
366 if (chdstream_read(stream, &c, sizeof(c) != sizeof(c)))
372 char *chdstream_gets(chdstream_t *stream, char *buffer, size_t len)
378 while (offset < len && (c = chdstream_getc(stream)) != EOF)
379 buffer[offset++] = c;
382 buffer[offset] = '\0';
387 uint64_t chdstream_tell(chdstream_t *stream)
389 return stream->offset;
392 void chdstream_rewind(chdstream_t *stream)
397 int64_t chdstream_seek(chdstream_t *stream, int64_t offset, int whence)
407 new_offset = stream->offset + offset;
410 new_offset = stream->track_end + offset;
419 if ((size_t)new_offset > stream->track_end)
420 new_offset = stream->track_end;
422 stream->offset = new_offset;
426 ssize_t chdstream_get_size(chdstream_t *stream)
428 return stream->track_end - stream->track_start;
431 uint32_t chdstream_get_track_start(chdstream_t *stream)
435 uint32_t frame_offset = 0;
437 for (i = 0; chdstream_get_meta(stream->chd, i, &meta); ++i)
439 if (stream->track_frame == frame_offset)
440 return meta.pregap * stream->frame_size;
442 frame_offset += meta.frames + meta.extra;
448 uint32_t chdstream_get_frame_size(chdstream_t *stream)
450 return stream->frame_size;
453 uint32_t chdstream_get_first_track_sector(chdstream_t* stream)
457 uint32_t frame_offset = 0;
458 uint32_t sector_offset = 0;
460 for (i = 0; chdstream_get_meta(stream->chd, i, &meta); ++i)
462 if (stream->track_frame == frame_offset)
463 return sector_offset;
465 sector_offset += meta.frames;
466 frame_offset += meta.frames + meta.extra;