| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2020 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #include <stdint.h> |
| 14 | #include <math.h> |
| 15 | |
| 16 | #include "aiff.h" |
| 17 | |
| 18 | using namespace smooth::IO; |
| 19 | |
| 20 | const String &BoCA::DecoderAIFF::GetComponentSpecs() |
| 21 | { |
| 22 | static String componentSpecs = " \ |
| 23 | \ |
| 24 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 25 | <component> \ |
| 26 | <name>Apple Audio File Decoder</name> \ |
| 27 | <version>1.0</version> \ |
| 28 | <id>aiff-dec</id> \ |
| 29 | <type>decoder</type> \ |
| 30 | <format> \ |
| 31 | <name>Apple Audio Files</name> \ |
| 32 | <lossless>true</lossless> \ |
| 33 | <extension>aif</extension> \ |
| 34 | <extension>aiff</extension> \ |
| 35 | <extension>aifc</extension> \ |
| 36 | <tag id=\"id3v2-tag\" mode=\"other\">ID3v2</tag> \ |
| 37 | <tag id=\"tocplist-tag\" mode=\"other\">.TOC.plist</tag> \ |
| 38 | </format> \ |
| 39 | </component> \ |
| 40 | \ |
| 41 | "; |
| 42 | |
| 43 | return componentSpecs; |
| 44 | } |
| 45 | |
| 46 | Bool BoCA::DecoderAIFF::CanOpenStream(const String &streamURI) |
| 47 | { |
| 48 | InStream in(STREAM_FILE, streamURI, IS_READ); |
| 49 | |
| 50 | if (in.InputString(4) != "FORM") return False; |
| 51 | |
| 52 | in.RelSeek(4); |
| 53 | |
| 54 | String fileType = in.InputString(4); |
| 55 | |
| 56 | if (fileType == "AIFF") return True; |
| 57 | else if (fileType != "AIFC") return False; |
| 58 | |
| 59 | String chunk; |
| 60 | |
| 61 | while (chunk != "COMM") |
| 62 | { |
| 63 | if (in.GetPos() >= in.Size()) break; |
| 64 | |
| 65 | /* Read next chunk. |
| 66 | */ |
| 67 | chunk = in.InputString(4); |
| 68 | |
| 69 | UnsignedInt32 cSize = in.InputNumberRaw(4); |
| 70 | |
| 71 | if (chunk == "COMM") |
| 72 | { |
| 73 | in.RelSeek(18); |
| 74 | |
| 75 | /* Parse AIFF-C compression type. |
| 76 | */ |
| 77 | String compression = in.InputString(4); |
| 78 | |
| 79 | if (compression == "NONE" || compression == "sowt") return True; |
| 80 | |
| 81 | /* Skip rest of chunk. |
| 82 | */ |
| 83 | if (!in.RelSeek(cSize - 22 + cSize % 2)) break; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | /* Skip chunk. |
| 88 | */ |
| 89 | if (!in.RelSeek(cSize + cSize % 2)) break; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return False; |
| 94 | } |
| 95 | |
| 96 | Error BoCA::DecoderAIFF::GetStreamInfo(const String &streamURI, Track &track) |
| 97 | { |
| 98 | InStream in(STREAM_FILE, streamURI, IS_READ); |
| 99 | |
| 100 | track.fileSize = in.Size(); |
| 101 | |
| 102 | /* Read FORM chunk. |
| 103 | */ |
| 104 | if (in.InputString(4) != "FORM") { errorState = True; errorString = "Unknown file type"; } |
| 105 | |
| 106 | in.RelSeek(4); |
| 107 | |
| 108 | String fileType = in.InputString(4); |
| 109 | |
| 110 | if (fileType != "AIFF" && fileType != "AIFC") { errorState = True; errorString = "Unknown file type"; } |
| 111 | |
| 112 | String chunk; |
| 113 | |
| 114 | while (!errorState) |
| 115 | { |
| 116 | if (in.GetPos() >= in.Size()) break; |
| 117 | |
| 118 | /* Read next chunk. |
| 119 | */ |
| 120 | chunk = in.InputString(4); |
| 121 | |
| 122 | UnsignedInt32 cSize = in.InputNumberRaw(4); |
| 123 | |
| 124 | if (chunk == "COMM") |
| 125 | { |
| 126 | Int cStart = in.GetPos(); |
| 127 | |
| 128 | Format format = track.GetFormat(); |
| 129 | |
| 130 | format.channels = (unsigned short) in.InputNumberRaw(2); |
| 131 | track.length = (unsigned long) in.InputNumberRaw(4); |
| 132 | |
| 133 | format.order = BYTE_RAW; |
| 134 | format.bits = (unsigned short) in.InputNumberRaw(2); |
| 135 | |
| 136 | /* Read sample rate as 80 bit float. |
| 137 | */ |
| 138 | int16_t exp = (in.InputNumberRaw(2) & 0x7FFF) - 16383 - 63; |
| 139 | uint64_t man = 0; |
| 140 | |
| 141 | for (int i = 0; i < 8; i++) man = (man << 8) | in.InputNumberRaw(1); |
| 142 | |
| 143 | format.rate = ldexp((double) man, exp); |
| 144 | |
| 145 | /* Parse AIFF-C compression type. |
| 146 | */ |
| 147 | if (fileType == "AIFC") |
| 148 | { |
| 149 | String compression = in.InputString(4); |
| 150 | |
| 151 | if (compression == "NONE") format.order = BYTE_RAW; |
| 152 | else if (compression == "sowt") format.order = BYTE_INTEL; |
| 153 | else { errorState = True; errorString = "Unsupported audio format"; } |
| 154 | } |
| 155 | |
| 156 | track.SetFormat(format); |
| 157 | |
| 158 | /* Skip rest of chunk. |
| 159 | */ |
| 160 | in.Seek(cStart + cSize + cSize % 2); |
| 161 | } |
| 162 | else if (chunk == "AUTH" || |
| 163 | chunk == "NAME" || |
| 164 | chunk == "ANNO") |
| 165 | { |
| 166 | Info info = track.GetInfo(); |
| 167 | |
| 168 | if (chunk == "AUTH") info.artist = in.InputString(cSize); |
| 169 | else if (chunk == "NAME") info.title = in.InputString(cSize); |
| 170 | else if (chunk == "ANNO") info.comment = in.InputString(cSize); |
| 171 | |
| 172 | track.SetInfo(info); |
| 173 | |
| 174 | /* Skip rest of chunk. |
| 175 | */ |
| 176 | in.RelSeek(cSize % 2); |
| 177 | } |
| 178 | else if (chunk == "ID3 ") |
| 179 | { |
| 180 | Buffer<UnsignedByte> buffer(cSize); |
| 181 | |
| 182 | in.InputData(buffer, cSize); |
| 183 | |
| 184 | AS::Registry &boca = AS::Registry::Get(); |
| 185 | AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("id3v2-tag"); |
| 186 | |
| 187 | if (tagger != NIL) |
| 188 | { |
| 189 | tagger->SetConfiguration(GetConfiguration()); |
| 190 | tagger->ParseBuffer(buffer, track); |
| 191 | |
| 192 | boca.DeleteComponent(tagger); |
| 193 | } |
| 194 | |
| 195 | /* Skip rest of chunk. |
| 196 | */ |
| 197 | in.RelSeek(cSize % 2); |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | /* Skip chunk. |
| 202 | */ |
| 203 | if (!in.RelSeek(cSize + cSize % 2)) { errorState = True; errorString = "Invalid file format"; } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /* Read .TOC.plist if it exists. |
| 208 | */ |
| 209 | AS::Registry &boca = AS::Registry::Get(); |
| 210 | AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("tocplist-tag"); |
| 211 | |
| 212 | if (tagger != NIL) |
| 213 | { |
| 214 | tagger->SetConfiguration(GetConfiguration()); |
| 215 | tagger->ParseStreamInfo(streamURI, track); |
| 216 | |
| 217 | boca.DeleteComponent(tagger); |
| 218 | } |
| 219 | |
| 220 | if (errorState) return Error(); |
| 221 | else return Success(); |
| 222 | } |
| 223 | |
| 224 | BoCA::DecoderAIFF::DecoderAIFF() |
| 225 | { |
| 226 | dataOffset = 0; |
| 227 | } |
| 228 | |
| 229 | BoCA::DecoderAIFF::~DecoderAIFF() |
| 230 | { |
| 231 | } |
| 232 | |
| 233 | Bool BoCA::DecoderAIFF::Activate() |
| 234 | { |
| 235 | InStream in(STREAM_DRIVER, driver); |
| 236 | |
| 237 | in.Seek(12); |
| 238 | |
| 239 | String chunk; |
| 240 | |
| 241 | do |
| 242 | { |
| 243 | /* Read next chunk |
| 244 | */ |
| 245 | chunk = in.InputString(4); |
| 246 | |
| 247 | UnsignedInt32 cSize = in.InputNumberRaw(4); |
| 248 | |
| 249 | if (chunk != "SSND") |
| 250 | { |
| 251 | if (!in.RelSeek(cSize + cSize % 2)) return False; |
| 252 | } |
| 253 | } |
| 254 | while (chunk != "SSND"); |
| 255 | |
| 256 | dataOffset = in.GetPos() + 8; |
| 257 | |
| 258 | driver->Seek(dataOffset); |
| 259 | |
| 260 | return True; |
| 261 | } |
| 262 | |
| 263 | Bool BoCA::DecoderAIFF::Seek(Int64 samplePosition) |
| 264 | { |
| 265 | const Format &format = track.GetFormat(); |
| 266 | |
| 267 | driver->Seek(dataOffset + samplePosition * format.channels * (format.bits / 8)); |
| 268 | |
| 269 | return True; |
| 270 | } |
| 271 | |
| 272 | Int BoCA::DecoderAIFF::ReadData(Buffer<UnsignedByte> &data) |
| 273 | { |
| 274 | if (driver->GetPos() == driver->GetSize()) return -1; |
| 275 | |
| 276 | return driver->ReadData(data, data.Size()); |
| 277 | } |