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.

120 lines
4.0KB

  1. #include "meta.h"
  2. #include "../coding/coding.h"
  3. typedef enum { ADX, HCA, AT9, VAG } awb_type;
  4. /* CRI AFS2, container of streaming ADX or HCA, often (but not always) together with a .acb CUE */
  5. VGMSTREAM * init_vgmstream_awb(STREAMFILE *streamFile) {
  6. VGMSTREAM *vgmstream = NULL;
  7. STREAMFILE *temp_streamFile = NULL;
  8. off_t offset, subfile_offset, subfile_next;
  9. size_t subfile_size;
  10. int total_subsongs, target_subsong = streamFile->stream_index;
  11. //uint32_t flags;
  12. uint16_t alignment, subkey;
  13. awb_type type;
  14. char *extension = NULL;
  15. /* checks
  16. * .awb: standard
  17. * .afs2: sometimes [Okami HD (PS4)] */
  18. if (!check_extensions(streamFile, "awb,afs2"))
  19. goto fail;
  20. if (read_32bitBE(0x00,streamFile) != 0x41465332) /* "AFS2" */
  21. goto fail;
  22. //flags = read_32bitLE(0x08,streamFile);
  23. total_subsongs = read_32bitLE(0x08,streamFile);
  24. alignment = (uint16_t)read_16bitLE(0x0c,streamFile);
  25. subkey = (uint16_t)read_16bitLE(0x0e,streamFile);
  26. if (target_subsong == 0) target_subsong = 1;
  27. if (target_subsong > total_subsongs || total_subsongs <= 0) goto fail;
  28. offset = 0x10;
  29. /* id(?) table: skip */
  30. offset += total_subsongs * 0x02;
  31. /* offset table: find target
  32. * offset are absolute but sometimes misaligned (specially first that just points to offset table end) */
  33. {
  34. off_t file_size = get_streamfile_size(streamFile);
  35. offset += (target_subsong-1) * 0x04;
  36. /* last offset is always file end, so table entries = total_subsongs+1 */
  37. subfile_offset = read_32bitLE(offset+0x00,streamFile);
  38. subfile_next = read_32bitLE(offset+0x04,streamFile);
  39. subfile_offset += (subfile_offset % alignment) ?
  40. alignment - (subfile_offset % alignment) : 0;
  41. subfile_next += (subfile_next % alignment) && subfile_next < file_size ?
  42. alignment - (subfile_next % alignment) : 0;
  43. subfile_size = subfile_next - subfile_offset;
  44. //todo: flags & 0x200 are uint16 offsets?
  45. }
  46. //;VGM_LOG("TXTH: subfile offset=%lx + %x\n", subfile_offset, subfile_size);
  47. /* autodetect as there isn't anything, plus can mix types
  48. * (waveid<>codec info is usually in the companion .acb) */
  49. if ((uint16_t)read_16bitBE(subfile_offset, streamFile) == 0x8000) { /* ADX id */
  50. type = ADX;
  51. extension = "adx";
  52. }
  53. else if (((uint32_t)read_32bitBE(subfile_offset,streamFile) & 0x7f7f7f7f) == 0x48434100) { /* "HCA\0" */
  54. type = HCA;
  55. extension = "hca";
  56. }
  57. else if (read_32bitBE(subfile_offset,streamFile) == 0x52494646) { /* "RIFF" */
  58. type = AT9;
  59. extension = "at9";
  60. }
  61. else if (read_32bitBE(subfile_offset,streamFile) == 0x56414770) { /* "VAGp" */
  62. type = VAG;
  63. extension = "vag";
  64. }
  65. else {
  66. goto fail;
  67. }
  68. temp_streamFile = setup_subfile_streamfile(streamFile, subfile_offset,subfile_size, extension);
  69. if (!temp_streamFile) goto fail;
  70. switch(type) {
  71. case HCA: /* most common */
  72. vgmstream = init_vgmstream_hca_subkey(temp_streamFile, subkey);
  73. if (!vgmstream) goto fail;
  74. break;
  75. case ADX: /* Okami HD (PS4) */
  76. vgmstream = init_vgmstream_adx(temp_streamFile);
  77. if (!vgmstream) goto fail;
  78. break;
  79. case AT9: /* Ukiyo no Roushi (Vita) */
  80. vgmstream = init_vgmstream_riff(temp_streamFile);
  81. if (!vgmstream) goto fail;
  82. break;
  83. case VAG: /* Ukiyo no Roushi (Vita) */
  84. vgmstream = init_vgmstream_vag(temp_streamFile);
  85. if (!vgmstream) goto fail;
  86. break;
  87. default:
  88. goto fail;
  89. }
  90. //todo: could try to get name in .acb for this waveid
  91. vgmstream->num_streams = total_subsongs;
  92. close_streamfile(temp_streamFile);
  93. return vgmstream;
  94. fail:
  95. close_streamfile(temp_streamFile);
  96. close_vgmstream(vgmstream);
  97. return NULL;
  98. }