| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2021 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 | extern "C" { |
| 14 | # include <cdda_interface.h> |
| 15 | # include <cdda_paranoia.h> |
| 16 | } |
| 17 | |
| 18 | #include "cdparanoia.h" |
| 19 | #include "config.h" |
| 20 | |
| 21 | using namespace smooth::IO; |
| 22 | using namespace smooth::GUI::Dialogs; |
| 23 | |
| 24 | const String &BoCA::DecoderCDParanoia::GetComponentSpecs() |
| 25 | { |
| 26 | static String componentSpecs = " \ |
| 27 | \ |
| 28 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 29 | <component> \ |
| 30 | <name>cdparanoia Ripper Component %VERSION%</name> \ |
| 31 | <version>1.0</version> \ |
| 32 | <id>cdparanoia-dec</id> \ |
| 33 | <type>decoder</type> \ |
| 34 | <require>cdparanoia-info</require> \ |
| 35 | <format> \ |
| 36 | <name>Windows CD Audio Track</name> \ |
| 37 | <lossless>true</lossless> \ |
| 38 | <extension>cda</extension> \ |
| 39 | </format> \ |
| 40 | </component> \ |
| 41 | \ |
| 42 | "; |
| 43 | |
| 44 | #ifdef PARANOIA_CB_CACHEERR |
| 45 | componentSpecs.Replace("%VERSION%", String("v").Append(cdda_version())); |
| 46 | #else |
| 47 | componentSpecs.Replace("%VERSION%", NIL); |
| 48 | #endif |
| 49 | return componentSpecs; |
| 50 | } |
| 51 | |
| 52 | namespace BoCA |
| 53 | { |
| 54 | void paranoiaCallback(long offset, int state) |
| 55 | { |
| 56 | #ifdef PARANOIA_CB_CACHEERR |
| 57 | if (state == PARANOIA_CB_CACHEERR) DecoderCDParanoia::readDecoder->numCacheErrors++; |
| 58 | #endif |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | Array<UnsignedInt64> BoCA::DecoderCDParanoia::lastRead; |
| 63 | |
| 64 | Threads::Mutex BoCA::DecoderCDParanoia::readMutex; |
| 65 | BoCA::DecoderCDParanoia *BoCA::DecoderCDParanoia::readDecoder = NIL; |
| 66 | |
| 67 | Bool BoCA::DecoderCDParanoia::CanOpenStream(const String &streamURI) |
| 68 | { |
| 69 | String lcURI = streamURI.ToLower(); |
| 70 | |
| 71 | return lcURI.StartsWith("device://cdda:") || |
| 72 | lcURI.EndsWith(".cda"); |
| 73 | } |
| 74 | |
| 75 | Error BoCA::DecoderCDParanoia::GetStreamInfo(const String &streamURI, Track &track) |
| 76 | { |
| 77 | AS::Registry &boca = AS::Registry::Get(); |
| 78 | AS::DeviceInfoComponent *component = (AS::DeviceInfoComponent *) boca.CreateComponentByID("cdparanoia-info"); |
| 79 | |
| 80 | if (component == NIL) return Error(); |
| 81 | |
| 82 | track.isCDTrack = True; |
| 83 | |
| 84 | Format format; |
| 85 | |
| 86 | format.channels = 2; |
| 87 | format.rate = 44100; |
| 88 | format.bits = 16; |
| 89 | format.order = BYTE_INTEL; |
| 90 | |
| 91 | track.SetFormat(format); |
| 92 | |
| 93 | Int trackNumber = -1; |
| 94 | Int trackLength = 0; |
| 95 | Int audiodrive = 0; |
| 96 | |
| 97 | if (streamURI.StartsWith("device://cdda:")) |
| 98 | { |
| 99 | audiodrive = streamURI.SubString(14, 1).ToInt(); |
| 100 | trackNumber = streamURI.SubString(16, streamURI.Length() - 16).ToInt(); |
| 101 | } |
| 102 | else if (streamURI.EndsWith(".cda")) |
| 103 | { |
| 104 | /* Find track number and length. |
| 105 | */ |
| 106 | { |
| 107 | InStream in(STREAM_FILE, streamURI, IS_READ); |
| 108 | |
| 109 | in.Seek(22); trackNumber = in.InputNumber(2); |
| 110 | in.Seek(32); trackLength = in.InputNumber(4); |
| 111 | } |
| 112 | |
| 113 | for (audiodrive = 0; audiodrive < component->GetNumberOfDevices(); audiodrive++) |
| 114 | { |
| 115 | Bool done = False; |
| 116 | const MCDI &mcdi = component->GetNthDeviceMCDI(audiodrive); |
| 117 | |
| 118 | for (Int i = 0; i < mcdi.GetNumberOfEntries(); i++) |
| 119 | { |
| 120 | if (mcdi.GetNthEntryType(i) != ENTRY_AUDIO) continue; |
| 121 | |
| 122 | Int length = mcdi.GetNthEntryOffset(i + 1) - mcdi.GetNthEntryOffset(i); |
| 123 | |
| 124 | if (length == trackLength && mcdi.GetNthEntryTrackNumber(i) == trackNumber) |
| 125 | { |
| 126 | done = True; |
| 127 | |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if (done) break; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (trackNumber == -1) return Error(); |
| 137 | |
| 138 | /* Fill MCDI data. |
| 139 | */ |
| 140 | Info info; |
| 141 | |
| 142 | info.mcdi = component->GetNthDeviceMCDI(audiodrive); |
| 143 | |
| 144 | Int entryNumber = -1; |
| 145 | |
| 146 | if (trackNumber == 0) |
| 147 | { |
| 148 | if (info.mcdi.GetNthEntryType(0) == ENTRY_AUDIO) |
| 149 | { |
| 150 | entryNumber = 0; |
| 151 | trackLength = info.mcdi.GetNthEntryOffset(0); |
| 152 | } |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | for (Int i = 0; i < info.mcdi.GetNumberOfEntries(); i++) |
| 157 | { |
| 158 | if (info.mcdi.GetNthEntryType(i) == ENTRY_AUDIO && info.mcdi.GetNthEntryTrackNumber(i) == trackNumber) |
| 159 | { |
| 160 | entryNumber = i; |
| 161 | trackLength = info.mcdi.GetNthEntryTrackLength(i); |
| 162 | |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (entryNumber == -1) return Error(); |
| 169 | |
| 170 | track.length = trackLength * samplesPerSector; |
| 171 | track.fileSize = trackLength * bytesPerSector; |
| 172 | |
| 173 | track.cdTrack = trackNumber; |
| 174 | track.drive = audiodrive; |
| 175 | track.outputFile = NIL; |
| 176 | |
| 177 | info.track = trackNumber; |
| 178 | info.numTracks = info.mcdi.GetNumberOfAudioTracks(); |
| 179 | |
| 180 | info.SetOtherInfo(INFO_MEDIATYPE, I18n::Get()->TranslateString("CD", "Media types")); |
| 181 | |
| 182 | track.SetInfo(info); |
| 183 | |
| 184 | /* Delete DeviceInfo component. |
| 185 | */ |
| 186 | boca.DeleteComponent(component); |
| 187 | |
| 188 | return Success(); |
| 189 | } |
| 190 | |
| 191 | BoCA::DecoderCDParanoia::DecoderCDParanoia() |
| 192 | { |
| 193 | configLayer = NIL; |
| 194 | |
| 195 | drive = NIL; |
| 196 | paranoia = NIL; |
| 197 | |
| 198 | nextSector = 0; |
| 199 | sectorsLeft = 0; |
| 200 | |
| 201 | readOffset = 0; |
| 202 | |
| 203 | skipSamples = 0; |
| 204 | prependSamples = 0; |
| 205 | appendSamples = 0; |
| 206 | |
| 207 | numCacheErrors = 0; |
| 208 | |
| 209 | lastRead.EnableLocking(); |
| 210 | } |
| 211 | |
| 212 | BoCA::DecoderCDParanoia::~DecoderCDParanoia() |
| 213 | { |
| 214 | if (configLayer != NIL) Object::DeleteObject(configLayer); |
| 215 | } |
| 216 | |
| 217 | Bool BoCA::DecoderCDParanoia::Activate() |
| 218 | { |
| 219 | AS::Registry &boca = AS::Registry::Get(); |
| 220 | AS::DeviceInfoComponent *info = (AS::DeviceInfoComponent *) boca.CreateComponentByID("cdparanoia-info"); |
| 221 | |
| 222 | if (info != NIL) |
| 223 | { |
| 224 | if (lastRead.Length() == 0) for (Int i = 0; i < info->GetNumberOfDevices(); i++) lastRead.Add(0); |
| 225 | |
| 226 | drive = cdda_identify(info->GetNthDeviceInfo(track.drive).path, CDDA_MESSAGE_FORGETIT, NIL); |
| 227 | |
| 228 | boca.DeleteComponent(info); |
| 229 | } |
| 230 | |
| 231 | if (drive == NIL) return False; |
| 232 | |
| 233 | cdda_open(drive); |
| 234 | |
| 235 | /* Set ripping spped. |
| 236 | */ |
| 237 | const Config *config = GetConfiguration(); |
| 238 | |
| 239 | Int speed = config->GetIntValue(ConfigureCDParanoia::ConfigID, String("RippingSpeedDrive-").Append(GetDriveID()), 0); |
| 240 | |
| 241 | if (speed > 0) cdda_speed_set(drive, speed); |
| 242 | else cdda_speed_set(drive, -1); |
| 243 | |
| 244 | /* Enable paranoia mode. |
| 245 | */ |
| 246 | paranoia = NIL; |
| 247 | |
| 248 | if (config->GetIntValue(ConfigureCDParanoia::ConfigID, "CDParanoia", False)) |
| 249 | { |
| 250 | int paranoiaMode = PARANOIA_MODE_FULL ^ PARANOIA_MODE_NEVERSKIP; |
| 251 | |
| 252 | switch (config->GetIntValue(ConfigureCDParanoia::ConfigID, "CDParanoiaMode", 3)) |
| 253 | { |
| 254 | case 0: |
| 255 | paranoiaMode = PARANOIA_MODE_OVERLAP; |
| 256 | break; |
| 257 | case 1: |
| 258 | paranoiaMode &= ~PARANOIA_MODE_VERIFY; |
| 259 | break; |
| 260 | case 2: |
| 261 | paranoiaMode &= ~(PARANOIA_MODE_SCRATCH | PARANOIA_MODE_REPAIR); |
| 262 | break; |
| 263 | } |
| 264 | |
| 265 | paranoia = paranoia_init(drive); |
| 266 | |
| 267 | paranoia_modeset(paranoia, paranoiaMode); |
| 268 | } |
| 269 | |
| 270 | /* Call seek to initialize ripper to start of track. |
| 271 | */ |
| 272 | if (!Seek(0)) return False; |
| 273 | |
| 274 | return True; |
| 275 | } |
| 276 | |
| 277 | Bool BoCA::DecoderCDParanoia::Deactivate() |
| 278 | { |
| 279 | if (drive == NIL) return False; |
| 280 | |
| 281 | /* Check for drive cache errors. |
| 282 | */ |
| 283 | if (numCacheErrors > 0) |
| 284 | { |
| 285 | Config *config = Config::Get(); |
| 286 | Bool noCacheWarning = config->GetIntValue(ConfigureCDParanoia::ConfigID, "NoCacheWarning", False); |
| 287 | |
| 288 | if (!noCacheWarning) |
| 289 | { |
| 290 | MessageDlg *msgBox = new MessageDlg("The CD-ROM drive appears to be seeking impossibly quickly.\n" |
| 291 | "This could be due to timer bugs, a drive that really is improbably fast,\n" |
| 292 | "or, most likely, a bug in cdparanoia's cache modelling.\n\n" |
| 293 | "Please consider using another drive for ripping audio CDs and send a bug\n" |
| 294 | "report to [email protected] to assist developers in correcting the problem.", "Warning", Message::Buttons::Ok, Message::Icon::Warning, "Do not display this warning again", &noCacheWarning); |
| 295 | |
| 296 | msgBox->ShowDialog(); |
| 297 | |
| 298 | config->SetIntValue(ConfigureCDParanoia::ConfigID, "NoCacheWarning", noCacheWarning); |
| 299 | config->SaveSettings(); |
| 300 | |
| 301 | Object::DeleteObject(msgBox); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | if (paranoia != NIL) paranoia_free(paranoia); |
| 306 | |
| 307 | cdda_close(drive); |
| 308 | |
| 309 | return True; |
| 310 | } |
| 311 | |
| 312 | Bool BoCA::DecoderCDParanoia::Seek(Int64 samplePosition) |
| 313 | { |
| 314 | const Config *config = GetConfiguration(); |
| 315 | |
| 316 | Int startSector = 0; |
| 317 | Int endSector = 0; |
| 318 | Bool lastTrack = False; |
| 319 | |
| 320 | if (!GetTrackSectors(startSector, endSector, lastTrack)) return False; |
| 321 | |
| 322 | startSector += samplePosition / samplesPerSector; |
| 323 | |
| 324 | /* Calculate offset values. |
| 325 | */ |
| 326 | readOffset = config->GetIntValue(ConfigureCDParanoia::ConfigID, String("UseOffsetDrive-").Append(GetDriveID()), 0) ? config->GetIntValue(ConfigureCDParanoia::ConfigID, String("ReadOffsetDrive-").Append(GetDriveID()), 0) : 0; |
| 327 | |
| 328 | startSector += readOffset / Int(samplesPerSector); |
| 329 | endSector += readOffset / Int(samplesPerSector); |
| 330 | |
| 331 | if (readOffset % Int(samplesPerSector) < 0) { startSector--; skipSamples = samplesPerSector + readOffset % Int(samplesPerSector); } |
| 332 | else if (readOffset % Int(samplesPerSector) > 0) { if (!lastTrack) endSector++; skipSamples = readOffset % Int(samplesPerSector); } |
| 333 | |
| 334 | if (startSector < 0) { prependSamples = -startSector * samplesPerSector; startSector = 0; } |
| 335 | |
| 336 | if (lastTrack && readOffset > 0) { appendSamples = readOffset; endSector -= readOffset / samplesPerSector; } |
| 337 | |
| 338 | skipSamples += samplePosition % samplesPerSector; |
| 339 | |
| 340 | /* Set nextSector and sectors left to be read. |
| 341 | */ |
| 342 | nextSector = startSector; |
| 343 | sectorsLeft = endSector - startSector + 1; |
| 344 | |
| 345 | /* Open ripper. |
| 346 | */ |
| 347 | if (paranoia != NIL) paranoia_seek(paranoia, startSector, SEEK_SET); |
| 348 | |
| 349 | inBytes = 4 * samplePosition; |
| 350 | |
| 351 | /* Wait for drive to spin up if requested. |
| 352 | */ |
| 353 | Int spinUpTime = config->GetIntValue(ConfigureCDParanoia::ConfigID, String("SpinUpTimeDrive-").Append(GetDriveID()), 0); |
| 354 | UnsignedInt64 startTime = S::System::System::Clock(); |
| 355 | |
| 356 | while (spinUpTime > 0 && startTime - lastRead.GetNth(track.drive) > 2500 && S::System::System::Clock() - startTime < (UnsignedInt64) Math::Abs(spinUpTime * 1000)) |
| 357 | { |
| 358 | if (paranoia != NIL) |
| 359 | { |
| 360 | readMutex.Lock(); |
| 361 | readDecoder = this; |
| 362 | |
| 363 | paranoia_read(paranoia, ¶noiaCallback); |
| 364 | |
| 365 | readMutex.Release(); |
| 366 | |
| 367 | paranoia_seek(paranoia, startSector, SEEK_SET); |
| 368 | } |
| 369 | else |
| 370 | { |
| 371 | Buffer<UnsignedByte> buffer(bytesPerSector); |
| 372 | |
| 373 | cdda_read(drive, buffer, nextSector, 1); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | return True; |
| 378 | } |
| 379 | |
| 380 | Int BoCA::DecoderCDParanoia::ReadData(Buffer<UnsignedByte> &data) |
| 381 | { |
| 382 | if (inBytes >= track.fileSize) return -1; |
| 383 | |
| 384 | /* Set up buffer respecting empty samples to prepend. |
| 385 | */ |
| 386 | Int sectors = Math::Min(sectorsLeft, paranoia == NIL ? 26 : 1); |
| 387 | |
| 388 | Int prependBytes = 4 * prependSamples; |
| 389 | Int appendBytes = 4 * appendSamples; |
| 390 | Int skipBytes = 4 * skipSamples; |
| 391 | |
| 392 | prependSamples = 0; |
| 393 | skipSamples = 0; |
| 394 | |
| 395 | data.Resize(sectors * bytesPerSector + prependBytes); |
| 396 | |
| 397 | if (prependBytes > 0) data.Zero(); |
| 398 | |
| 399 | /* Append offset bytes to last track. |
| 400 | */ |
| 401 | if (sectors == 0) |
| 402 | { |
| 403 | data.Resize(prependBytes + appendBytes - skipBytes); |
| 404 | data.Zero(); |
| 405 | |
| 406 | inBytes += data.Size(); |
| 407 | |
| 408 | return data.Size(); |
| 409 | } |
| 410 | |
| 411 | /* Rip chunk. |
| 412 | */ |
| 413 | if (paranoia != NIL) |
| 414 | { |
| 415 | readMutex.Lock(); |
| 416 | readDecoder = this; |
| 417 | |
| 418 | int16_t *audio = paranoia_read(paranoia, ¶noiaCallback); |
| 419 | |
| 420 | readMutex.Release(); |
| 421 | |
| 422 | if (audio == NIL) return -1; |
| 423 | |
| 424 | memcpy(data + prependBytes, audio, sectors * bytesPerSector); |
| 425 | } |
| 426 | else |
| 427 | { |
| 428 | int sectors = cdda_read(drive, data + prependBytes, nextSector, sectors); |
| 429 | |
| 430 | if (sectors <= 0) return -1; |
| 431 | } |
| 432 | |
| 433 | nextSector += sectors; |
| 434 | sectorsLeft -= sectors; |
| 435 | |
| 436 | lastRead.SetNth(track.drive, S::System::System::Clock()); |
| 437 | |
| 438 | /* Strip samples to skip from the beginning. |
| 439 | */ |
| 440 | Int dataBytes = Math::Min(track.fileSize - inBytes, (Int64) sectors * bytesPerSector + prependBytes - skipBytes); |
| 441 | |
| 442 | if (skipBytes > 0) memmove(data, data + skipBytes, dataBytes); |
| 443 | |
| 444 | inBytes += dataBytes; |
| 445 | |
| 446 | return dataBytes; |
| 447 | } |
| 448 | |
| 449 | Bool BoCA::DecoderCDParanoia::GetTrackSectors(Int &startSector, Int &endSector, Bool &lastTrack) const |
| 450 | { |
| 451 | AS::Registry &boca = AS::Registry::Get(); |
| 452 | AS::DeviceInfoComponent *component = (AS::DeviceInfoComponent *) boca.CreateComponentByID("cdparanoia-info"); |
| 453 | |
| 454 | if (component == NIL) return False; |
| 455 | |
| 456 | MCDI mcdi = component->GetNthDeviceMCDI(track.drive); |
| 457 | |
| 458 | boca.DeleteComponent(component); |
| 459 | |
| 460 | if (track.cdTrack == 0) |
| 461 | { |
| 462 | /* Special handling for hidden track one audio. |
| 463 | */ |
| 464 | if (mcdi.GetNthEntryType(0) == ENTRY_AUDIO) |
| 465 | { |
| 466 | startSector = 0; |
| 467 | endSector = mcdi.GetNthEntryOffset(0) - 1; |
| 468 | lastTrack = False; |
| 469 | |
| 470 | return True; |
| 471 | } |
| 472 | } |
| 473 | else |
| 474 | { |
| 475 | /* Handle regular tracks. |
| 476 | */ |
| 477 | for (Int i = 0; i < mcdi.GetNumberOfEntries(); i++) |
| 478 | { |
| 479 | if (mcdi.GetNthEntryType(i) == ENTRY_AUDIO && mcdi.GetNthEntryTrackNumber(i) == track.cdTrack) |
| 480 | { |
| 481 | startSector = mcdi.GetNthEntryOffset(i); |
| 482 | endSector = startSector + mcdi.GetNthEntryTrackLength(i) - 1; |
| 483 | |
| 484 | if (mcdi.GetNthEntryType(i + 1) == ENTRY_AUDIO) lastTrack = False; |
| 485 | else lastTrack = True; |
| 486 | |
| 487 | return True; |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | return False; |
| 493 | } |
| 494 | |
| 495 | ConfigLayer *BoCA::DecoderCDParanoia::GetConfigurationLayer() |
| 496 | { |
| 497 | if (configLayer == NIL) configLayer = new ConfigureCDParanoia(); |
| 498 | |
| 499 | return configLayer; |
| 500 | } |
| 501 | |
| 502 | String BoCA::DecoderCDParanoia::GetDriveID() const |
| 503 | { |
| 504 | AS::Registry &boca = AS::Registry::Get(); |
| 505 | AS::DeviceInfoComponent *info = (AS::DeviceInfoComponent *) boca.CreateComponentByID("cdparanoia-info"); |
| 506 | |
| 507 | if (info == NIL) return NIL; |
| 508 | |
| 509 | const Device &device = info->GetNthDeviceInfo(track.drive); |
| 510 | |
| 511 | boca.DeleteComponent(info); |
| 512 | |
| 513 | return device.GetID(); |
| 514 | } |