| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2022 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 <engine/decoder.h> |
| 14 | |
| 15 | #include <smooth/io/drivers/driver_zero.h> |
| 16 | |
| 17 | using namespace smooth::IO; |
| 18 | |
| 19 | using namespace BoCA; |
| 20 | using namespace BoCA::AS; |
| 21 | |
| 22 | freac::Decoder::Decoder(const BoCA::Config *iConfiguration) : Component(iConfiguration) |
| 23 | { |
| 24 | stream = NIL; |
| 25 | decoder = NIL; |
| 26 | |
| 27 | sampleOffset = 0; |
| 28 | decodedSamples = 0; |
| 29 | |
| 30 | calculateCRC = False; |
| 31 | crcSum = 0; |
| 32 | } |
| 33 | |
| 34 | freac::Decoder::~Decoder() |
| 35 | { |
| 36 | Destroy(); |
| 37 | } |
| 38 | |
| 39 | Bool freac::Decoder::Create(const String &streamURI, const Track &track) |
| 40 | { |
| 41 | static DriverZero zero_in; |
| 42 | |
| 43 | Registry &boca = Registry::Get(); |
| 44 | File file(streamURI); |
| 45 | |
| 46 | format = track.GetFormat(); |
| 47 | |
| 48 | if (streamURI.StartsWith("device://")) stream = new InStream(STREAM_DRIVER, &zero_in); |
| 49 | else stream = new InStream(STREAM_FILE, streamURI, IS_READ); |
| 50 | |
| 51 | stream->SetPackageSize((track.length >= 0 && track.lossless ? 32768 : 4096) * format.channels * (format.bits / 8)); |
| 52 | |
| 53 | if (stream->GetLastError() != IO_ERROR_OK) |
| 54 | { |
| 55 | SetErrorInfo(True, "Unable to access input file: %1\n\nFile: %1\nPath: %2", streamURI.Contains("://") ? streamURI : file.GetFileName(), |
| 56 | streamURI.Contains("://") ? streamURI : file.GetFilePath()); |
| 57 | |
| 58 | delete stream; |
| 59 | |
| 60 | stream = NIL; |
| 61 | |
| 62 | return False; |
| 63 | } |
| 64 | |
| 65 | /* Create decoder component. |
| 66 | */ |
| 67 | if (track.decoderID != NIL && track.fileName == streamURI) |
| 68 | { |
| 69 | decoder = (DecoderComponent *) boca.CreateComponentByID(track.decoderID); |
| 70 | |
| 71 | if (decoder != NIL) decoder->SetConfiguration(configuration); |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | decoder = boca.CreateDecoderForStream(streamURI, configuration); |
| 76 | } |
| 77 | |
| 78 | if (decoder == NIL) |
| 79 | { |
| 80 | SetErrorInfo(True, "Could not create decoder component for input file: %1\n\nFile: %1\nPath: %2", streamURI.Contains("://") ? streamURI : file.GetFileName(), |
| 81 | streamURI.Contains("://") ? streamURI : file.GetFilePath()); |
| 82 | |
| 83 | delete stream; |
| 84 | |
| 85 | stream = NIL; |
| 86 | |
| 87 | return False; |
| 88 | } |
| 89 | |
| 90 | /* Lock component if it's not thread safe. |
| 91 | */ |
| 92 | LockComponent(decoder); |
| 93 | |
| 94 | /* Add decoder to stream. |
| 95 | */ |
| 96 | Track trackInfo = track; |
| 97 | |
| 98 | trackInfo.fileName = streamURI; |
| 99 | |
| 100 | decoder->SetConfiguration(configuration); |
| 101 | decoder->SetAudioTrackInfo(trackInfo); |
| 102 | |
| 103 | if (stream->SetFilter(decoder) == False) |
| 104 | { |
| 105 | SetErrorInfo(True, "Could not set up decoder for input file: %1\n\nFile: %1\nPath: %2\n\nError: %3", streamURI.Contains("://") ? streamURI : file.GetFileName(), |
| 106 | streamURI.Contains("://") ? streamURI : file.GetFilePath(), decoder->GetErrorString()); |
| 107 | |
| 108 | UnlockComponent(decoder); |
| 109 | |
| 110 | delete stream; |
| 111 | |
| 112 | boca.DeleteComponent(decoder); |
| 113 | |
| 114 | decoder = NIL; |
| 115 | stream = NIL; |
| 116 | |
| 117 | return False; |
| 118 | } |
| 119 | |
| 120 | /* Seek to sampleOffset if necessary. |
| 121 | */ |
| 122 | if (track.sampleOffset > 0 && !decoder->Seek(track.sampleOffset)) |
| 123 | { |
| 124 | Int64 bytesLeft = track.sampleOffset * format.channels * (format.bits / 8); |
| 125 | Buffer<UnsignedByte> buffer; |
| 126 | |
| 127 | while (bytesLeft) |
| 128 | { |
| 129 | buffer.Resize(Math::Min(Int64(1024), bytesLeft)); |
| 130 | |
| 131 | bytesLeft -= Read(buffer); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | fileName = streamURI; |
| 136 | sampleOffset = track.sampleOffset; |
| 137 | |
| 138 | return True; |
| 139 | } |
| 140 | |
| 141 | Bool freac::Decoder::Destroy() |
| 142 | { |
| 143 | if (decoder == NIL || stream == NIL) return False; |
| 144 | |
| 145 | Registry &boca = Registry::Get(); |
| 146 | |
| 147 | stream->RemoveFilter(); |
| 148 | |
| 149 | SetErrorInfo(decoder->GetErrorState(), decoder->GetErrorString()); |
| 150 | |
| 151 | UnlockComponent(decoder); |
| 152 | |
| 153 | delete stream; |
| 154 | |
| 155 | crcSum = crc.Finish(); |
| 156 | md5Sum = decoder->GetMD5Checksum(); |
| 157 | |
| 158 | boca.DeleteComponent(decoder); |
| 159 | |
| 160 | decoder = NIL; |
| 161 | stream = NIL; |
| 162 | |
| 163 | fileName = NIL; |
| 164 | sampleOffset = 0; |
| 165 | |
| 166 | return True; |
| 167 | } |
| 168 | |
| 169 | Bool freac::Decoder::GetStreamInfo(Track &track) const |
| 170 | { |
| 171 | if (decoder == NIL) return False; |
| 172 | |
| 173 | return decoder->GetStreamInfo(fileName, track); |
| 174 | } |
| 175 | |
| 176 | Int freac::Decoder::Read(Buffer<UnsignedByte> &buffer) |
| 177 | { |
| 178 | if (decoder == NIL || stream == NIL) return 0; |
| 179 | |
| 180 | /* Get data from decoder component. |
| 181 | */ |
| 182 | Int bytes = stream->InputData(buffer, buffer.Size()); |
| 183 | |
| 184 | if (bytes >= 0) |
| 185 | { |
| 186 | buffer.Resize(bytes); |
| 187 | |
| 188 | if (calculateCRC) crc.Feed(buffer); |
| 189 | |
| 190 | decodedSamples += buffer.Size() / format.channels / (format.bits / 8); |
| 191 | } |
| 192 | |
| 193 | return bytes; |
| 194 | } |
| 195 | |
| 196 | Bool freac::Decoder::Seek(Int64 sample) |
| 197 | { |
| 198 | if (decoder == NIL) return False; |
| 199 | |
| 200 | return decoder->Seek(sampleOffset + sample); |
| 201 | } |
| 202 | |
| 203 | Int64 freac::Decoder::GetInBytes() const |
| 204 | { |
| 205 | if (decoder == NIL) return 0; |
| 206 | |
| 207 | return decoder->GetInBytes(); |
| 208 | } |
| 209 | |
| 210 | String freac::Decoder::GetDecoderName() const |
| 211 | { |
| 212 | if (decoder == NIL) return String(); |
| 213 | |
| 214 | return decoder->GetName(); |
| 215 | } |
| 216 | |
| 217 | Void freac::Decoder::SetCalculateCRC(Bool calculate) |
| 218 | { |
| 219 | calculateCRC = calculate; |
| 220 | } |
| 221 | |
| 222 | UnsignedInt32 freac::Decoder::GetCRCChecksum() |
| 223 | { |
| 224 | Destroy(); |
| 225 | |
| 226 | return crcSum; |
| 227 | } |
| 228 | |
| 229 | Void freac::Decoder::SetCalculateMD5(Bool calculateMD5) |
| 230 | { |
| 231 | if (decoder == NIL) return; |
| 232 | |
| 233 | decoder->SetCalculateMD5(calculateMD5); |
| 234 | } |
| 235 | |
| 236 | String freac::Decoder::GetMD5Checksum() |
| 237 | { |
| 238 | Destroy(); |
| 239 | |
| 240 | return md5Sum; |
| 241 | } |