An audio player for macOS 10.8 and newer. https://kode54.net/cog
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
8.1KB

  1. #include "meta.h"
  2. #include "../coding/coding.h"
  3. /* SGXD - Sony/SCEI's format (SGB+SGH / SGD / SGX) */
  4. VGMSTREAM * init_vgmstream_sgxd(STREAMFILE *streamFile) {
  5. VGMSTREAM * vgmstream = NULL;
  6. STREAMFILE * streamHeader = NULL;
  7. off_t start_offset, data_offset, chunk_offset, name_offset = 0;
  8. size_t stream_size;
  9. int is_sgx, is_sgb = 0;
  10. int loop_flag, channels, codec;
  11. int sample_rate, num_samples, loop_start_sample, loop_end_sample;
  12. int total_subsongs, target_subsong = streamFile->stream_index;
  13. /* check extension, case insensitive */
  14. /* .sgx: header+data (Genji), .sgd: header+data, .sgh/sgd: header/data */
  15. if (!check_extensions(streamFile,"sgx,sgd,sgb"))
  16. goto fail;
  17. is_sgx = check_extensions(streamFile,"sgx");
  18. is_sgb = check_extensions(streamFile,"sgb");
  19. /* SGB+SGH: use SGH as header; otherwise use the current file as header */
  20. if (is_sgb) {
  21. streamHeader = open_streamfile_by_ext(streamFile, "sgh");
  22. if (!streamHeader) goto fail;
  23. } else {
  24. streamHeader = streamFile;
  25. }
  26. /* SGXD base (size 0x10) */
  27. if (read_32bitBE(0x00,streamHeader) != 0x53475844) /* "SGXD" */
  28. goto fail;
  29. /* 0x04 SGX: full header_size; SGD/SGH: unknown header_size (counting from 0x0/0x8/0x10, varies) */
  30. /* 0x08 SGX: first chunk offset? (0x10); SGD/SGH: full header_size */
  31. /* 0x0c SGX/SGH: full data size with padding; SGD: full data size + 0x80000000 with padding */
  32. if (is_sgb) {
  33. data_offset = 0x00;
  34. } else if ( is_sgx ) {
  35. data_offset = read_32bitLE(0x04,streamHeader);
  36. } else {
  37. data_offset = read_32bitLE(0x08,streamHeader);
  38. }
  39. /* typical chunks: WAVE, RGND, NAME (strings for WAVE or RGND), SEQD (related to SFX), WSUR, WMKR, BUSS */
  40. /* WAVE chunk (size 0x10 + files * 0x38 + optional padding) */
  41. if (is_sgx) { /* position after chunk+size */
  42. if (read_32bitBE(0x10,streamHeader) != 0x57415645) goto fail; /* "WAVE" */
  43. chunk_offset = 0x18;
  44. } else {
  45. if (!find_chunk_le(streamHeader, 0x57415645,0x10,0, &chunk_offset,NULL)) goto fail; /* "WAVE" */
  46. }
  47. /* 0x04 SGX: unknown; SGD/SGH: chunk length, 0x08 null */
  48. /* check multi-streams (usually only SE containers; Puppeteer) */
  49. total_subsongs = read_32bitLE(chunk_offset+0x04,streamHeader);
  50. if (target_subsong == 0) target_subsong = 1;
  51. if (target_subsong < 0 || target_subsong > total_subsongs || total_subsongs < 1) goto fail;
  52. /* read stream header */
  53. {
  54. off_t stream_offset;
  55. chunk_offset += 0x08 + 0x38 * (target_subsong-1); /* position in target header*/
  56. /* 0x00 ? (00/01/02) */
  57. if (!is_sgx) /* meaning unknown in .sgx; offset 0 = not a stream (a RGND sample) */
  58. name_offset = read_32bitLE(chunk_offset+0x04,streamHeader);
  59. codec = read_8bit(chunk_offset+0x08,streamHeader);
  60. channels = read_8bit(chunk_offset+0x09,streamHeader);
  61. /* 0x0a null */
  62. sample_rate = read_32bitLE(chunk_offset+0x0c,streamHeader);
  63. /* 0x10 info_type: meaning of the next value
  64. * (00=null, 30/40=data size without padding (ADPCM, ATRAC3plus), 80/A0=block size (AC3) */
  65. /* 0x14 info_value (see above) */
  66. /* 0x18 unknown (ex. 0x0008/0010/3307/CC02/etc)x2 */
  67. /* 0x1c null */
  68. num_samples = read_32bitLE(chunk_offset+0x20,streamHeader);
  69. loop_start_sample = read_32bitLE(chunk_offset+0x24,streamHeader);
  70. loop_end_sample = read_32bitLE(chunk_offset+0x28,streamHeader);
  71. stream_size = read_32bitLE(chunk_offset+0x2c,streamHeader); /* stream size (without padding) / interleave (for type3) */
  72. if (is_sgx) {
  73. stream_offset = 0x0;
  74. } else{
  75. stream_offset = read_32bitLE(chunk_offset+0x30,streamHeader);
  76. }
  77. /* 0x34 SGX: unknown; SGD/SGH: stream size (with padding) / interleave */
  78. loop_flag = loop_start_sample!=0xffffffff && loop_end_sample!=0xffffffff;
  79. start_offset = data_offset + stream_offset;
  80. }
  81. /* build the VGMSTREAM */
  82. vgmstream = allocate_vgmstream(channels,loop_flag);
  83. if (!vgmstream) goto fail;
  84. vgmstream->sample_rate = sample_rate;
  85. vgmstream->num_samples = num_samples;
  86. vgmstream->loop_start_sample = loop_start_sample;
  87. vgmstream->loop_end_sample = loop_end_sample;
  88. vgmstream->num_streams = total_subsongs;
  89. vgmstream->stream_size = stream_size;
  90. vgmstream->meta_type = meta_SGXD;
  91. if (name_offset)
  92. read_string(vgmstream->stream_name,STREAM_NAME_SIZE, name_offset,streamHeader);
  93. switch (codec) {
  94. case 0x01: /* PCM [LocoRoco Cocoreccho! (PS3)] (rare, locoloco_psn#279) */
  95. vgmstream->coding_type = coding_PCM16BE;
  96. vgmstream->layout_type = layout_interleave;
  97. vgmstream->interleave_block_size = 0x02;
  98. break;
  99. #ifdef VGM_USE_VORBIS
  100. case 0x02: /* Ogg Vorbis [Ni no Kuni: Wrath of the White Witch Remastered (PC)] (codec hijack?) */
  101. vgmstream->codec_data = init_ogg_vorbis(streamFile, start_offset, stream_size, NULL);
  102. if (!vgmstream->codec_data) goto fail;
  103. vgmstream->coding_type = coding_OGG_VORBIS;
  104. vgmstream->layout_type = layout_none;
  105. break;
  106. #endif
  107. case 0x03: /* PS-ADPCM [Genji (PS3), Ape Escape Move (PS3)]*/
  108. vgmstream->coding_type = coding_PSX;
  109. vgmstream->layout_type = layout_interleave;
  110. if (is_sgx || is_sgb) {
  111. vgmstream->interleave_block_size = 0x10;
  112. } else { /* this only seems to happen with SFX */
  113. vgmstream->interleave_block_size = stream_size;
  114. }
  115. /* a few files in LocoRoco set 0 stream size/samples, use an empty file for now */
  116. if (vgmstream->num_samples == 0)
  117. vgmstream->num_samples = 28;
  118. break;
  119. #ifdef VGM_USE_FFMPEG
  120. case 0x04: { /* ATRAC3plus [Kurohyo 1/2 (PSP), BraveStory (PSP)] */
  121. vgmstream->codec_data = init_ffmpeg_atrac3_riff(streamFile, start_offset, NULL);
  122. if (!vgmstream->codec_data) goto fail;
  123. vgmstream->coding_type = coding_FFmpeg;
  124. vgmstream->layout_type = layout_none;
  125. /* SGXD's sample rate has priority over RIFF's sample rate (may not match) */
  126. /* loop/sample values are relative (without skip) vs RIFF (with skip), matching "smpl" otherwise */
  127. break;
  128. }
  129. #endif
  130. case 0x05: /* Short PS-ADPCM [Afrika (PS3), LocoRoco Cocoreccho! (PS3)] */
  131. vgmstream->coding_type = coding_PSX_cfg;
  132. vgmstream->layout_type = layout_interleave;
  133. vgmstream->interleave_block_size = 0x4;
  134. vgmstream->codec_config = 1; /* needs extended table */
  135. break;
  136. #ifdef VGM_USE_FFMPEG
  137. case 0x06: { /* AC3 [Tokyo Jungle (PS3), Afrika (PS3)] */
  138. ffmpeg_codec_data *ffmpeg_data;
  139. ffmpeg_data = init_ffmpeg_offset(streamFile, start_offset, stream_size);
  140. if ( !ffmpeg_data ) goto fail;
  141. vgmstream->codec_data = ffmpeg_data;
  142. vgmstream->coding_type = coding_FFmpeg;
  143. vgmstream->layout_type = layout_none;
  144. /* manually set skip_samples if FFmpeg didn't do it */
  145. if (ffmpeg_data->skipSamples <= 0) {
  146. /* PS3 AC3 consistently has 256 encoder delay samples, and there are ~1000-2000 samples after num_samples.
  147. * Skipping them marginally improves full loops in some Tokyo Jungle tracks (ex. a_1.sgd). */
  148. ffmpeg_set_skip_samples(ffmpeg_data, 256);
  149. }
  150. /* SGXD loop/sample values are relative (without skip samples), no need to adjust */
  151. break;
  152. }
  153. #endif
  154. default:
  155. VGM_LOG("SGDX: unknown codec %i\n", codec);
  156. goto fail;
  157. }
  158. /* open the file for reading */
  159. if (!vgmstream_open_stream(vgmstream,streamFile,start_offset))
  160. goto fail;
  161. if (is_sgb && streamHeader) close_streamfile(streamHeader);
  162. return vgmstream;
  163. fail:
  164. if (is_sgb && streamHeader) close_streamfile(streamHeader);
  165. close_vgmstream(vgmstream);
  166. return NULL;
  167. }