| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2019 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 <smooth.h> |
| 14 | #include <smooth/dll.h> |
| 15 | |
| 16 | #include "lame.h" |
| 17 | |
| 18 | using namespace smooth::IO; |
| 19 | |
| 20 | const String &BoCA::DecoderLAME::GetComponentSpecs() |
| 21 | { |
| 22 | static String componentSpecs; |
| 23 | |
| 24 | if (lamedll != NIL) |
| 25 | { |
| 26 | componentSpecs = " \ |
| 27 | \ |
| 28 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 29 | <component> \ |
| 30 | <name>LAME MP3 Decoder %VERSION%</name> \ |
| 31 | <version>1.0</version> \ |
| 32 | <id>lame-dec</id> \ |
| 33 | <type threadSafe=\"false\">decoder</type> \ |
| 34 | <format> \ |
| 35 | <name>MPEG 1 Audio Layer 3</name> \ |
| 36 | <extension>mp3</extension> \ |
| 37 | <tag id=\"id3v1-tag\" mode=\"append\">ID3v1</tag> \ |
| 38 | <tag id=\"id3v2-tag\" mode=\"prepend\">ID3v2</tag> \ |
| 39 | </format> \ |
| 40 | </component> \ |
| 41 | \ |
| 42 | "; |
| 43 | |
| 44 | componentSpecs.Replace("%VERSION%", String("v").Append(ex_get_lame_short_version())); |
| 45 | } |
| 46 | |
| 47 | return componentSpecs; |
| 48 | } |
| 49 | |
| 50 | Void smooth::AttachDLL(Void *instance) |
| 51 | { |
| 52 | LoadLAMEDLL(); |
| 53 | } |
| 54 | |
| 55 | Void smooth::DetachDLL() |
| 56 | { |
| 57 | FreeLAMEDLL(); |
| 58 | } |
| 59 | |
| 60 | Bool BoCA::DecoderLAME::CanOpenStream(const String &streamURI) |
| 61 | { |
| 62 | return streamURI.ToLower().EndsWith(".mp3"); |
| 63 | } |
| 64 | |
| 65 | Error BoCA::DecoderLAME::GetStreamInfo(const String &streamURI, Track &track) |
| 66 | { |
| 67 | InStream in(STREAM_FILE, streamURI, IS_READ); |
| 68 | |
| 69 | Format format = track.GetFormat(); |
| 70 | |
| 71 | track.fileSize = in.Size(); |
| 72 | track.length = -1; |
| 73 | |
| 74 | SkipID3v2Tag(in); |
| 75 | ParseVBRHeaders(in); |
| 76 | |
| 77 | Buffer<unsigned char> buffer(4096); |
| 78 | |
| 79 | Buffer<short> pcm_l(buffer.Size() * 24); |
| 80 | Buffer<short> pcm_r(buffer.Size() * 24); |
| 81 | |
| 82 | hip_t context = ex_hip_decode_init(); |
| 83 | Int offset = in.GetPos(); |
| 84 | |
| 85 | do |
| 86 | { |
| 87 | Int bytes = Math::Min((Int64) buffer.Size(), in.Size() - in.GetPos()); |
| 88 | |
| 89 | in.InputData((void *) buffer, bytes); |
| 90 | |
| 91 | mp3data_struct mp3data; |
| 92 | |
| 93 | memset(&mp3data, 0, sizeof(mp3data)); |
| 94 | |
| 95 | Int nSamples = ex_hip_decode_headers(context, buffer, bytes, pcm_l, pcm_r, &mp3data); |
| 96 | |
| 97 | if (mp3data.header_parsed && nSamples > mp3data.framesize) |
| 98 | { |
| 99 | format.channels = mp3data.stereo; |
| 100 | format.rate = mp3data.samplerate; |
| 101 | format.bits = 16; |
| 102 | |
| 103 | if (mp3data.nsamp > 0) track.length = mp3data.nsamp - delaySamples - padSamples; |
| 104 | else if (mp3data.bitrate > 0) track.approxLength = (track.fileSize - offset) / (mp3data.bitrate * 1000 / 8) * format.rate; |
| 105 | |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | while (in.GetPos() < in.Size()); |
| 110 | |
| 111 | ex_hip_decode_exit(context); |
| 112 | |
| 113 | if (format == Format()) { errorState = True; errorString = "Invalid file format"; } |
| 114 | |
| 115 | track.SetFormat(format); |
| 116 | |
| 117 | in.Close(); |
| 118 | |
| 119 | if (!errorState) |
| 120 | { |
| 121 | Bool foundTag = False; |
| 122 | |
| 123 | AS::Registry &boca = AS::Registry::Get(); |
| 124 | AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("id3v2-tag"); |
| 125 | |
| 126 | if (tagger != NIL) |
| 127 | { |
| 128 | tagger->SetConfiguration(GetConfiguration()); |
| 129 | |
| 130 | if (tagger->ParseStreamInfo(streamURI, track) == Success()) foundTag = True; |
| 131 | |
| 132 | boca.DeleteComponent(tagger); |
| 133 | } |
| 134 | |
| 135 | if (!foundTag) |
| 136 | { |
| 137 | tagger = (AS::TaggerComponent *) boca.CreateComponentByID("id3v1-tag"); |
| 138 | |
| 139 | if (tagger != NIL) |
| 140 | { |
| 141 | tagger->SetConfiguration(GetConfiguration()); |
| 142 | tagger->ParseStreamInfo(streamURI, track); |
| 143 | |
| 144 | boca.DeleteComponent(tagger); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (errorState) return Error(); |
| 150 | else return Success(); |
| 151 | } |
| 152 | |
| 153 | BoCA::DecoderLAME::DecoderLAME() |
| 154 | { |
| 155 | context = 0; |
| 156 | |
| 157 | delaySamples = 0; |
| 158 | padSamples = 0; |
| 159 | |
| 160 | /* Initialize to decoder delay. |
| 161 | */ |
| 162 | delaySamplesLeft = 529; |
| 163 | } |
| 164 | |
| 165 | BoCA::DecoderLAME::~DecoderLAME() |
| 166 | { |
| 167 | } |
| 168 | |
| 169 | Bool BoCA::DecoderLAME::Activate() |
| 170 | { |
| 171 | /* Skip headers. |
| 172 | */ |
| 173 | InStream in(STREAM_DRIVER, driver); |
| 174 | |
| 175 | SkipID3v2Tag(in); |
| 176 | ParseVBRHeaders(in); |
| 177 | |
| 178 | driver->Seek(in.GetPos()); |
| 179 | |
| 180 | /* Create decoder. |
| 181 | */ |
| 182 | context = ex_hip_decode_init(); |
| 183 | |
| 184 | return True; |
| 185 | } |
| 186 | |
| 187 | Bool BoCA::DecoderLAME::Deactivate() |
| 188 | { |
| 189 | ex_hip_decode_exit(context); |
| 190 | |
| 191 | return True; |
| 192 | } |
| 193 | |
| 194 | Int BoCA::DecoderLAME::ReadData(Buffer<UnsignedByte> &data) |
| 195 | { |
| 196 | /* Read input data. |
| 197 | */ |
| 198 | Int size = driver->ReadData(data, data.Size()); |
| 199 | |
| 200 | if (size <= 0) return -1; |
| 201 | |
| 202 | inBytes += size; |
| 203 | |
| 204 | /* Decode samples. |
| 205 | */ |
| 206 | const Format &format = track.GetFormat(); |
| 207 | |
| 208 | pcm_l.Resize(data.Size() * 24); |
| 209 | pcm_r.Resize(data.Size() * 24); |
| 210 | |
| 211 | Int nSamples = ex_hip_decode(context, data, size, pcm_l, pcm_r); |
| 212 | |
| 213 | /* Try harder in case we got no data yet. |
| 214 | */ |
| 215 | for (Int i = 0; nSamples == 0 && i < size / 384; i++) nSamples = ex_hip_decode(context, NIL, 0, pcm_l, pcm_r); |
| 216 | |
| 217 | data.Resize(0); |
| 218 | |
| 219 | if (nSamples > delaySamplesLeft) |
| 220 | { |
| 221 | data.Resize((nSamples - delaySamplesLeft) * format.channels * (format.bits / 8)); |
| 222 | |
| 223 | for (Int i = delaySamplesLeft; i < nSamples; i++) |
| 224 | { |
| 225 | for (Int j = 0; j < format.channels; j++) ((short *) (unsigned char *) data)[format.channels * (i - delaySamplesLeft) + j] = (j == 0) ? pcm_l[i] : pcm_r[i]; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | delaySamplesLeft = Math::Max(0, delaySamplesLeft - nSamples); |
| 230 | |
| 231 | return data.Size(); |
| 232 | } |
| 233 | |
| 234 | Bool BoCA::DecoderLAME::SkipID3v2Tag(InStream &in) |
| 235 | { |
| 236 | /* Check for an ID3v2 tag at the beginning of the |
| 237 | * file and skip it if it exists as it might cause |
| 238 | * problems if the tag is unsynchronized. |
| 239 | */ |
| 240 | if (in.InputString(3) == "ID3") |
| 241 | { |
| 242 | in.InputNumber(2); // ID3 version |
| 243 | in.InputNumber(1); // Flags |
| 244 | |
| 245 | /* Read tag size as a 4 byte unsynchronized integer. |
| 246 | */ |
| 247 | Int tagSize = (in.InputNumber(1) << 21) + |
| 248 | (in.InputNumber(1) << 14) + |
| 249 | (in.InputNumber(1) << 7) + |
| 250 | (in.InputNumber(1) ); |
| 251 | |
| 252 | in.RelSeek(tagSize); |
| 253 | |
| 254 | inBytes += (tagSize + 10); |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | in.Seek(0); |
| 259 | } |
| 260 | |
| 261 | return True; |
| 262 | } |
| 263 | |
| 264 | Bool BoCA::DecoderLAME::ParseVBRHeaders(InStream &in) |
| 265 | { |
| 266 | /* Check for a LAME header and extract the number of samples if it exists. |
| 267 | */ |
| 268 | Buffer<UnsignedByte> buffer(192); |
| 269 | |
| 270 | /* Read data and seek back to before the Xing header. |
| 271 | */ |
| 272 | in.InputData(buffer, buffer.Size()); |
| 273 | in.RelSeek(-buffer.Size()); |
| 274 | |
| 275 | /* Check for a LAME header and extract length information. |
| 276 | */ |
| 277 | Int offset = ((buffer[1] >> 3) & 1 ? (buffer[3] >> 6 != 3 ? 32 : 17) : |
| 278 | (buffer[3] >> 6 != 3 ? 17 : 9)) + 4; |
| 279 | UnsignedByte *xing = buffer + offset; |
| 280 | UnsignedInt16 crc = Hash::CRC16::Compute(buffer, offset + 0x9A); |
| 281 | |
| 282 | if (xing[0x9A] == (crc >> 8) && xing[0x9B] == (crc & 0xFF)) |
| 283 | { |
| 284 | delaySamples = ( xing[0x8D] << 4) | ((xing[0x8E] & 0xF0) >> 4); |
| 285 | padSamples = ((xing[0x8E] & 0x0F) << 8) | ( xing[0x8F] ); |
| 286 | |
| 287 | delaySamplesLeft += delaySamples; |
| 288 | |
| 289 | return True; |
| 290 | } |
| 291 | |
| 292 | return False; |
| 293 | } |