| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2023 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/encoder.h> |
| 14 | |
| 15 | using namespace smooth::IO; |
| 16 | |
| 17 | using namespace BoCA; |
| 18 | using namespace BoCA::AS; |
| 19 | |
| 20 | namespace freac |
| 21 | { |
| 22 | class EncoderThread : public Threads::Thread |
| 23 | { |
| 24 | private: |
| 25 | Threads::Semaphore processSignal; |
| 26 | Threads::Semaphore readySignal; |
| 27 | |
| 28 | Bool finish; |
| 29 | Bool error; |
| 30 | |
| 31 | EncoderComponent *encoder; |
| 32 | OutStream *stream; |
| 33 | |
| 34 | Buffer<UnsignedByte> backBuffer; |
| 35 | Buffer<UnsignedByte> samplesBuffer; |
| 36 | |
| 37 | Int bufferLimit; |
| 38 | |
| 39 | Int EncodeLoop(); |
| 40 | public: |
| 41 | EncoderThread(EncoderComponent *, OutStream *, const Format &); |
| 42 | |
| 43 | Int Encode(Buffer<UnsignedByte> &); |
| 44 | Void Finish(); |
| 45 | |
| 46 | Bool GetErrorState() const { return encoder->GetErrorState() || error; } |
| 47 | const String &GetErrorString() const { return encoder->GetErrorString(); } |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | freac::Encoder::Encoder(const BoCA::Config *iConfiguration) : Component(iConfiguration) |
| 52 | { |
| 53 | stream = NIL; |
| 54 | encoder = NIL; |
| 55 | |
| 56 | encoderThread = NIL; |
| 57 | |
| 58 | chapter = 0; |
| 59 | offset = 0; |
| 60 | |
| 61 | encodedSamples = 0; |
| 62 | } |
| 63 | |
| 64 | freac::Encoder::~Encoder() |
| 65 | { |
| 66 | Destroy(); |
| 67 | } |
| 68 | |
| 69 | Bool freac::Encoder::Create(const String &encoderID, const String &streamURI, const Track &track) |
| 70 | { |
| 71 | Registry &boca = Registry::Get(); |
| 72 | File file(streamURI); |
| 73 | |
| 74 | sourceFormat = track.GetFormat(); |
| 75 | |
| 76 | album = track; |
| 77 | album.outputFile = streamURI; |
| 78 | |
| 79 | chapter = 0; |
| 80 | offset = 0; |
| 81 | |
| 82 | /* Create output file. |
| 83 | */ |
| 84 | file.Delete(); |
| 85 | |
| 86 | stream = new OutStream(STREAM_FILE, BoCA::Utilities::CreateDirectoryForFile(streamURI), OS_REPLACE); |
| 87 | stream->SetPackageSize(32768 * sourceFormat.channels * (sourceFormat.bits / 8)); |
| 88 | |
| 89 | if (stream->GetLastError() != IO_ERROR_OK) |
| 90 | { |
| 91 | SetErrorInfo(True, "Unable to create output file: %1\n\nFile: %1\nPath: %2", file.GetFileName(), file.GetFilePath()); |
| 92 | |
| 93 | delete stream; |
| 94 | |
| 95 | stream = NIL; |
| 96 | |
| 97 | return False; |
| 98 | } |
| 99 | |
| 100 | /* Create encoder component. |
| 101 | */ |
| 102 | encoder = (EncoderComponent *) boca.CreateComponentByID(encoderID); |
| 103 | |
| 104 | if (encoder == NIL) |
| 105 | { |
| 106 | SetErrorInfo(True, "Could not create encoder component: %1", encoderID); |
| 107 | |
| 108 | delete stream; |
| 109 | |
| 110 | stream = NIL; |
| 111 | |
| 112 | return False; |
| 113 | } |
| 114 | |
| 115 | /* Lock encoder if it's not thread safe. |
| 116 | */ |
| 117 | LockComponent(encoder); |
| 118 | |
| 119 | /* Add encoder to stream. |
| 120 | */ |
| 121 | encoder->SetConfiguration(configuration); |
| 122 | encoder->SetAudioTrackInfo(album); |
| 123 | |
| 124 | if (stream->SetFilter(encoder) == False) |
| 125 | { |
| 126 | SetErrorInfo(True, "Could not set up encoder for output file: %1\n\nFile: %1\nPath: %2\n\nError: %3", file.GetFileName(), file.GetFilePath(), encoder->GetErrorString()); |
| 127 | |
| 128 | UnlockComponent(encoder); |
| 129 | |
| 130 | delete stream; |
| 131 | |
| 132 | boca.DeleteComponent(encoder); |
| 133 | |
| 134 | encoder = NIL; |
| 135 | stream = NIL; |
| 136 | |
| 137 | return False; |
| 138 | } |
| 139 | |
| 140 | /* Set album target format. |
| 141 | */ |
| 142 | targetFormat = FormatConverter::GetBestTargetFormat(sourceFormat, encoder); |
| 143 | |
| 144 | album.AdjustSampleCounts(targetFormat); |
| 145 | album.SetFormat(targetFormat); |
| 146 | |
| 147 | /* Start encoder thread. |
| 148 | */ |
| 149 | encoderThread = new EncoderThread(encoder, stream, sourceFormat); |
| 150 | encoderThread->Start(); |
| 151 | |
| 152 | return True; |
| 153 | } |
| 154 | |
| 155 | Bool freac::Encoder::Destroy() |
| 156 | { |
| 157 | if (encoderThread == NIL) return False; |
| 158 | |
| 159 | /* Tell encoder thread to finish. |
| 160 | */ |
| 161 | encoderThread->Finish(); |
| 162 | |
| 163 | /* Free encoder component. |
| 164 | */ |
| 165 | Registry &boca = Registry::Get(); |
| 166 | |
| 167 | encoder->SetAudioTrackInfo(album); |
| 168 | |
| 169 | stream->RemoveFilter(); |
| 170 | |
| 171 | SetErrorInfo(encoderThread->GetErrorState(), encoderThread->GetErrorString()); |
| 172 | |
| 173 | UnlockComponent(encoder); |
| 174 | |
| 175 | delete stream; |
| 176 | |
| 177 | md5Sum = encoder->GetMD5Checksum(); |
| 178 | |
| 179 | boca.DeleteComponent(encoder); |
| 180 | |
| 181 | encoder = NIL; |
| 182 | stream = NIL; |
| 183 | |
| 184 | /* Delete encoder thread. |
| 185 | */ |
| 186 | delete encoderThread; |
| 187 | |
| 188 | encoderThread = NIL; |
| 189 | |
| 190 | return True; |
| 191 | } |
| 192 | |
| 193 | Int freac::Encoder::Write(Buffer<UnsignedByte> &buffer) |
| 194 | { |
| 195 | if (encoderThread == NIL) return 0; |
| 196 | |
| 197 | encodedSamples += buffer.Size() / sourceFormat.channels / (sourceFormat.bits / 8); |
| 198 | |
| 199 | /* Hand data to encoder thread. |
| 200 | */ |
| 201 | return encoderThread->Encode(buffer); |
| 202 | } |
| 203 | |
| 204 | Void freac::Encoder::SignalChapterChange() |
| 205 | { |
| 206 | if (album.tracks.Length() <= chapter) return; |
| 207 | |
| 208 | /* This adjusts chapter lengths in order to be able to |
| 209 | * set accurate chapter marks even in case we have tracks |
| 210 | * with only approxLength set in the first place. |
| 211 | */ |
| 212 | Track &track = album.tracks.GetNthReference(chapter); |
| 213 | |
| 214 | track.length = encodedSamples - offset; |
| 215 | offset = encodedSamples; |
| 216 | |
| 217 | track.length = track.length * targetFormat.rate / sourceFormat.rate; |
| 218 | |
| 219 | chapter++; |
| 220 | } |
| 221 | |
| 222 | Bool freac::Encoder::IsLossless() const |
| 223 | { |
| 224 | if (encoder == NIL) return False; |
| 225 | |
| 226 | return encoder->IsLossless(); |
| 227 | } |
| 228 | |
| 229 | String freac::Encoder::GetEncoderName() const |
| 230 | { |
| 231 | if (encoder == NIL) return String(); |
| 232 | |
| 233 | return encoder->GetName(); |
| 234 | } |
| 235 | |
| 236 | Void freac::Encoder::SetCalculateMD5(Bool calculateMD5) |
| 237 | { |
| 238 | if (encoder == NIL) return; |
| 239 | |
| 240 | encoder->SetCalculateMD5(calculateMD5); |
| 241 | } |
| 242 | |
| 243 | String freac::Encoder::GetMD5Checksum() |
| 244 | { |
| 245 | Destroy(); |
| 246 | |
| 247 | return md5Sum; |
| 248 | } |
| 249 | |
| 250 | freac::EncoderThread::EncoderThread(EncoderComponent *iEncoder, OutStream *iStream, const Format &format) : processSignal(1), readySignal(1) |
| 251 | { |
| 252 | encoder = iEncoder; |
| 253 | stream = iStream; |
| 254 | |
| 255 | finish = False; |
| 256 | error = False; |
| 257 | |
| 258 | bufferLimit = 65536 * format.channels * (format.bits / 8); |
| 259 | |
| 260 | processSignal.Wait(); |
| 261 | |
| 262 | threadMain.Connect(&EncoderThread::EncodeLoop, this); |
| 263 | } |
| 264 | |
| 265 | Int freac::EncoderThread::Encode(Buffer<UnsignedByte> &buffer) |
| 266 | { |
| 267 | /* Move samples to back buffer. |
| 268 | */ |
| 269 | backBuffer.Resize(backBuffer.Size() + buffer.Size()); |
| 270 | |
| 271 | if (buffer.Size() > 0) memcpy(backBuffer + backBuffer.Size() - buffer.Size(), buffer, buffer.Size()); |
| 272 | |
| 273 | /* Aquire lock to handle data. |
| 274 | */ |
| 275 | Bool proceed = False; |
| 276 | |
| 277 | if (backBuffer.Size() >= bufferLimit) proceed = readySignal.Wait(); |
| 278 | else proceed = readySignal.TryWait(); |
| 279 | |
| 280 | if (proceed) |
| 281 | { |
| 282 | if (Threads::Access::Value(error)) return -1; |
| 283 | |
| 284 | /* Pass new samples to encoder thread. |
| 285 | */ |
| 286 | samplesBuffer.Resize(backBuffer.Size()); |
| 287 | |
| 288 | if (backBuffer.Size() > 0) memcpy(samplesBuffer, backBuffer, backBuffer.Size()); |
| 289 | |
| 290 | backBuffer.Resize(0); |
| 291 | |
| 292 | /* Signal availability of new samples. |
| 293 | */ |
| 294 | processSignal.Release(); |
| 295 | } |
| 296 | |
| 297 | return buffer.Size(); |
| 298 | } |
| 299 | |
| 300 | Void freac::EncoderThread::Finish() |
| 301 | { |
| 302 | if (!Threads::Access::Value(error)) |
| 303 | { |
| 304 | readySignal.Wait(); |
| 305 | |
| 306 | /* Pass new samples to encoder thread. |
| 307 | */ |
| 308 | samplesBuffer.Resize(backBuffer.Size()); |
| 309 | |
| 310 | if (backBuffer.Size() > 0) memcpy(samplesBuffer, backBuffer, backBuffer.Size()); |
| 311 | |
| 312 | backBuffer.Resize(0); |
| 313 | |
| 314 | /* Signal availability of new samples and wait for them to be processed. |
| 315 | */ |
| 316 | processSignal.Release(); |
| 317 | readySignal.Wait(); |
| 318 | |
| 319 | /* Wait for thread to finish. |
| 320 | */ |
| 321 | Threads::Access::Set(finish, True); |
| 322 | |
| 323 | processSignal.Release(); |
| 324 | } |
| 325 | |
| 326 | Wait(); |
| 327 | } |
| 328 | |
| 329 | Int freac::EncoderThread::EncodeLoop() |
| 330 | { |
| 331 | while (!Threads::Access::Value(error)) |
| 332 | { |
| 333 | processSignal.Wait(); |
| 334 | |
| 335 | if (Threads::Access::Value(finish)) break; |
| 336 | |
| 337 | /* Hand data to encoder component. |
| 338 | */ |
| 339 | if (stream->OutputData(samplesBuffer, samplesBuffer.Size()) == False || encoder->GetErrorState()) Threads::Access::Set(error, True); |
| 340 | |
| 341 | readySignal.Release(); |
| 342 | } |
| 343 | |
| 344 | return Success(); |
| 345 | } |