Debian Codegraph @

components/decoder/cdrip/cdrip.cpp

raw symbols SCIP
1 /* BoCA - BonkEnc Component Architecture
2 * Copyright (C) 2007-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 <smooth.h>
14 #include <smooth/dll.h>
15
16 #include <cdda_paranoia.h>
17
18 #include "cdrip.h"
19 #include "config.h"
20
21 using namespace smooth::IO;
22 using namespace smooth::GUI::Dialogs;
23
24 const String &BoCA::DecoderCDRip::GetComponentSpecs()
25 {
26 static String componentSpecs;
27
28 if (cdripdll != NIL)
29 {
30 componentSpecs = " \
31 \
32 <?xml version=\"1.0\" encoding=\"UTF-8\"?> \
33 <component> \
34 <name>CDRip Ripper Component %VERSION%</name> \
35 <version>1.0</version> \
36 <id>cdrip-dec</id> \
37 <type>decoder</type> \
38 <require>cdrip-info</require> \
39 <format> \
40 <name>Windows CD Audio Track</name> \
41 <lossless>true</lossless> \
42 <extension>cda</extension> \
43 </format> \
44 </component> \
45 \
46 ";
47
48 LONG cdripVersion = ex_CR_GetCDRipVersion();
49
50 componentSpecs.Replace("%VERSION%", String("v").Append(String::FromInt(cdripVersion / 100)).Append(".").Append(String::FromInt(cdripVersion % 100)));
51 }
52
53 return componentSpecs;
54 }
55
56 Void smooth::AttachDLL(Void *instance)
57 {
58 LoadCDRipDLL();
59 }
60
61 Void smooth::DetachDLL()
62 {
63 FreeCDRipDLL();
64 }
65
66 BoCA::CDPlayerIni BoCA::DecoderCDRip::cdPlayer;
67 UnsignedInt32 BoCA::DecoderCDRip::cdPlayerDiscID = 0;
68
69 BoCA::CDText BoCA::DecoderCDRip::cdText;
70 UnsignedInt32 BoCA::DecoderCDRip::cdTextDiscID = 0;
71
72 Array<UnsignedInt64> BoCA::DecoderCDRip::lastRead;
73
74 Bool BoCA::DecoderCDRip::CanOpenStream(const String &streamURI)
75 {
76 String lcURI = streamURI.ToLower();
77
78 return lcURI.StartsWith("device://cdda:") ||
79 lcURI.EndsWith(".cda");
80 }
81
82 Error BoCA::DecoderCDRip::GetStreamInfo(const String &streamURI, Track &track)
83 {
84 AS::Registry &boca = AS::Registry::Get();
85 AS::DeviceInfoComponent *component = (AS::DeviceInfoComponent *) boca.CreateComponentByID("cdrip-info");
86
87 if (component == NIL) return Error();
88
89 const Config *config = GetConfiguration();
90
91 track.isCDTrack = True;
92
93 Format format;
94
95 format.channels = 2;
96 format.rate = 44100;
97 format.bits = 16;
98 format.order = BYTE_INTEL;
99
100 track.SetFormat(format);
101
102 Int trackNumber = -1;
103 Int trackLength = 0;
104 Int audiodrive = 0;
105
106 if (streamURI.StartsWith("device://cdda:"))
107 {
108 audiodrive = streamURI.SubString(14, 1).ToInt();
109 trackNumber = streamURI.SubString(16, streamURI.Length() - 16).ToInt();
110 }
111 else if (streamURI.EndsWith(".cda"))
112 {
113 /* Find track number and length.
114 */
115 {
116 InStream in(STREAM_FILE, streamURI, IS_READ);
117
118 in.Seek(22); trackNumber = in.InputNumber(2);
119 in.Seek(32); trackLength = in.InputNumber(4);
120 }
121
122 for (audiodrive = 0; audiodrive < component->GetNumberOfDevices(); audiodrive++)
123 {
124 Bool done = False;
125 const MCDI &mcdi = component->GetNthDeviceMCDI(audiodrive);
126
127 for (Int i = 0; i < mcdi.GetNumberOfEntries(); i++)
128 {
129 if (mcdi.GetNthEntryType(i) != ENTRY_AUDIO) continue;
130
131 Int length = mcdi.GetNthEntryOffset(i + 1) - mcdi.GetNthEntryOffset(i);
132
133 if (length == trackLength && mcdi.GetNthEntryTrackNumber(i) == trackNumber)
134 {
135 done = True;
136
137 break;
138 }
139 }
140
141 if (done) break;
142 }
143 }
144
145 if (trackNumber == -1) return Error();
146
147 /* Fill MCDI data.
148 */
149 Info info;
150
151 info.mcdi = component->GetNthDeviceMCDI(audiodrive);
152
153 Int entryNumber = -1;
154
155 if (trackNumber == 0)
156 {
157 if (info.mcdi.GetNthEntryType(0) == ENTRY_AUDIO)
158 {
159 entryNumber = 0;
160 trackLength = info.mcdi.GetNthEntryOffset(0);
161 }
162 }
163 else
164 {
165 for (Int i = 0; i < info.mcdi.GetNumberOfEntries(); i++)
166 {
167 if (info.mcdi.GetNthEntryType(i) == ENTRY_AUDIO && info.mcdi.GetNthEntryTrackNumber(i) == trackNumber)
168 {
169 entryNumber = i;
170 trackLength = info.mcdi.GetNthEntryTrackLength(i);
171
172 break;
173 }
174 }
175 }
176
177 if (entryNumber == -1) return Error();
178
179 track.length = trackLength * samplesPerSector;
180 track.fileSize = trackLength * bytesPerSector;
181
182 track.cdTrack = trackNumber;
183 track.drive = audiodrive;
184 track.outputFile = NIL;
185
186 info.track = trackNumber;
187 info.numTracks = info.mcdi.GetNumberOfAudioTracks();
188
189 info.SetOtherInfo(INFO_MEDIATYPE, I18n::Get()->TranslateString("CD", "Media types"));
190
191 /* Read CD-Text and cdplayer.ini.
192 */
193 UnsignedInt32 discid = ComputeDiscID(info.mcdi);
194
195 if (config->GetIntValue(ConfigureCDRip::ConfigID, "ReadCDText", True) && cdTextDiscID != discid) { cdText.ReadCDText(track.drive); cdTextDiscID = discid; }
196 if (config->GetIntValue(ConfigureCDRip::ConfigID, "ReadCDPlayerIni", True) && cdPlayerDiscID != discid) { cdPlayer.ReadCDInfo(track.drive); cdPlayerDiscID = discid; }
197
198 if (config->GetIntValue(ConfigureCDRip::ConfigID, "ReadCDText", True) && cdText.GetCDInfo().GetTrackTitle(trackNumber) != NIL)
199 {
200 const CDInfo &cdInfo = cdText.GetCDInfo();
201
202 info.SetOtherInfo(INFO_ALBUMARTIST, cdInfo.GetArtist());
203
204 info.title = cdInfo.GetTrackTitle(trackNumber);
205 info.album = cdInfo.GetTitle();
206 info.genre = cdInfo.GetGenre();
207
208 if (cdInfo.GetTrackArtist(trackNumber) != NIL) info.artist = cdInfo.GetTrackArtist(trackNumber);
209 else info.artist = cdInfo.GetArtist();
210
211 if (cdInfo.GetTrackComment(trackNumber) != NIL) info.comment = cdInfo.GetTrackComment(trackNumber);
212 else info.comment = cdInfo.GetComment();
213
214 if (cdInfo.GetTrackSongwriter(trackNumber) != NIL) info.SetOtherInfo(INFO_LYRICIST, cdInfo.GetTrackSongwriter(trackNumber));
215 else if (cdInfo.GetSongwriter() != NIL) info.SetOtherInfo(INFO_LYRICIST, cdInfo.GetSongwriter());
216
217 if (cdInfo.GetTrackComposer(trackNumber) != NIL) info.SetOtherInfo(INFO_COMPOSER, cdInfo.GetTrackComposer(trackNumber));
218 else if (cdInfo.GetComposer() != NIL) info.SetOtherInfo(INFO_COMPOSER, cdInfo.GetComposer());
219
220 if (cdInfo.GetTrackArranger(trackNumber) != NIL) info.SetOtherInfo(INFO_ARRANGER, cdInfo.GetTrackArranger(trackNumber));
221 else if (cdInfo.GetArranger() != NIL) info.SetOtherInfo(INFO_ARRANGER, cdInfo.GetArranger());
222
223 if (cdInfo.GetCatalog() != NIL) info.SetOtherInfo(INFO_CATALOGNUMBER, cdInfo.GetCatalog());
224 if (cdInfo.GetBarcode() != NIL) info.SetOtherInfo(INFO_BARCODE, cdInfo.GetBarcode());
225
226 if (Info::IsISRC(cdInfo.GetTrackISRC(trackNumber))) info.isrc = cdInfo.GetTrackISRC(trackNumber);
227 }
228 else if (config->GetIntValue(ConfigureCDRip::ConfigID, "ReadCDPlayerIni", True) && cdPlayer.GetCDInfo().GetTrackTitle(trackNumber) != NIL)
229 {
230 const CDInfo &cdInfo = cdPlayer.GetCDInfo();
231
232 info.SetOtherInfo(INFO_ALBUMARTIST, cdInfo.GetArtist());
233
234 info.artist = cdInfo.GetArtist();
235 info.title = cdInfo.GetTrackTitle(trackNumber);
236 info.album = cdInfo.GetTitle();
237 }
238
239 /* Read ISRC if requested.
240 */
241 if (config->GetIntValue(ConfigureCDRip::ConfigID, "ReadISRC", 0))
242 {
243 CDROMDRIVE *cd = ex_CR_OpenCDROM(track.drive);
244
245 if (cd != NIL)
246 {
247 ISRC data;
248
249 ex_CR_ReadAndGetISRC(cd, &data, track.cdTrack);
250
251 /* Check if the ISRC is valid.
252 */
253 if (Info::IsISRC(data.isrc)) info.isrc = data.isrc;
254
255 ex_CR_CloseCDROM(cd);
256 }
257 }
258
259 track.SetInfo(info);
260
261 /* Delete DeviceInfo component.
262 */
263 boca.DeleteComponent(component);
264
265 return Success();
266 }
267
268 BoCA::DecoderCDRip::DecoderCDRip()
269 {
270 configLayer = NIL;
271
272 cd = NIL;
273
274 readOffset = 0;
275
276 skipSamples = 0;
277 prependSamples = 0;
278 appendSamples = 0;
279
280 dataBufferSize = 0;
281
282 lastRead.EnableLocking();
283 }
284
285 BoCA::DecoderCDRip::~DecoderCDRip()
286 {
287 if (configLayer != NIL) Object::DeleteObject(configLayer);
288 }
289
290 Bool BoCA::DecoderCDRip::Activate()
291 {
292 const Config *config = GetConfiguration();
293
294 if (lastRead.Length() == 0) for (Int i = 0; i < ex_CR_GetNumCDROM(); i++) lastRead.Add(0);
295
296 /* Get paranoia mode.
297 */
298 int nParanoiaMode = PARANOIA_MODE_FULL ^ PARANOIA_MODE_NEVERSKIP;
299
300 switch (config->GetIntValue(ConfigureCDRip::ConfigID, "CDParanoiaMode", 3))
301 {
302 case 0:
303 nParanoiaMode = PARANOIA_MODE_OVERLAP;
304 break;
305 case 1:
306 nParanoiaMode &= ~PARANOIA_MODE_VERIFY;
307 break;
308 case 2:
309 nParanoiaMode &= ~(PARANOIA_MODE_SCRATCH | PARANOIA_MODE_REPAIR);
310 break;
311 }
312
313 /* Change drive parameters.
314 */
315 cd = ex_CR_OpenCDROM(track.drive);
316
317 if (cd == NIL) return False;
318
319 CDROMPARAMS params;
320
321 ex_CR_GetCDROMParameters(cd, &params);
322
323 params.nRippingMode = config->GetIntValue(ConfigureCDRip::ConfigID, "CDParanoia", False);
324 params.nParanoiaMode = nParanoiaMode;
325 params.bSwapLefRightChannel = config->GetIntValue(ConfigureCDRip::ConfigID, "SwapChannels", False);
326 params.bJitterCorrection = config->GetIntValue(ConfigureCDRip::ConfigID, "JitterCorrection", False);
327 // params.bDetectJitterErrors = config->GetIntValue(ConfigureCDRip::ConfigID, "DetectJitterErrors", True);
328 // params.bDetectC2Errors = config->GetIntValue(ConfigureCDRip::ConfigID, "DetectC2Errors", True);
329 params.nSpeed = config->GetIntValue(ConfigureCDRip::ConfigID, String("RippingSpeedDrive-").Append(GetDriveID()), 0);
330 params.bEnableMultiRead = False;
331 params.nMultiReadCount = 0;
332
333 /* Set maximum speed if no limit is requested.
334 */
335 if (params.nSpeed == 0) params.nSpeed = 64;
336
337 ex_CR_SetCDROMParameters(cd, &params);
338
339 /* Call seek to initialize ripper to start of track.
340 */
341 if (!Seek(0)) return False;
342
343 /* Read CD TOC to initialize internal structures.
344 */
345 ex_CR_ReadToc(cd);
346
347 return True;
348 }
349
350 Bool BoCA::DecoderCDRip::Deactivate()
351 {
352 /* Check for drive cache errors.
353 */
354 if (ex_CR_GetNumberOfCacheErrors(cd) > 0)
355 {
356 Config *config = Config::Get();
357 Bool noCacheWarning = config->GetIntValue(ConfigureCDRip::ConfigID, "NoCacheWarning", False);
358
359 if (!noCacheWarning)
360 {
361 MessageDlg *msgBox = new MessageDlg("The CD-ROM drive appears to be seeking impossibly quickly.\n"
362 "This could be due to timer bugs, a drive that really is improbably fast,\n"
363 "or, most likely, a bug in cdparanoia's cache modelling.\n\n"
364 "Please consider using another drive for ripping audio CDs and send a bug\n"
365 "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);
366
367 msgBox->ShowDialog();
368
369 config->SetIntValue(ConfigureCDRip::ConfigID, "NoCacheWarning", noCacheWarning);
370 config->SaveSettings();
371
372 Object::DeleteObject(msgBox);
373 }
374 }
375
376 ex_CR_CloseRipper(cd);
377 ex_CR_CloseCDROM(cd);
378
379 return True;
380 }
381
382 Bool BoCA::DecoderCDRip::Seek(Int64 samplePosition)
383 {
384 const Config *config = GetConfiguration();
385
386 ex_CR_CloseRipper(cd);
387
388 Int startSector = 0;
389 Int endSector = 0;
390 Bool lastTrack = False;
391
392 if (!GetTrackSectors(startSector, endSector, lastTrack)) return False;
393
394 startSector += samplePosition / samplesPerSector;
395
396 /* Calculate offset values.
397 */
398 readOffset = config->GetIntValue(ConfigureCDRip::ConfigID, String("UseOffsetDrive-").Append(GetDriveID()), 0) ? config->GetIntValue(ConfigureCDRip::ConfigID, String("ReadOffsetDrive-").Append(GetDriveID()), 0) : 0;
399
400 startSector += readOffset / Int(samplesPerSector);
401 endSector += readOffset / Int(samplesPerSector);
402
403 if (readOffset % Int(samplesPerSector) < 0) { startSector--; skipSamples = samplesPerSector + readOffset % Int(samplesPerSector); }
404 else if (readOffset % Int(samplesPerSector) > 0) { if (!lastTrack) endSector++; skipSamples = readOffset % Int(samplesPerSector); }
405
406 if (startSector < 0) { prependSamples = -startSector * samplesPerSector; startSector = 0; }
407
408 if (lastTrack && readOffset > 0) { appendSamples = readOffset; endSector -= readOffset / samplesPerSector; }
409
410 skipSamples += samplePosition % samplesPerSector;
411
412 /* Open ripper.
413 */
414 LONG bufferSize = 0;
415
416 ex_CR_OpenRipper(cd, &bufferSize, startSector, endSector);
417
418 dataBufferSize = bufferSize;
419
420 inBytes = 4 * samplePosition;
421
422 /* Wait for drive to spin up if requested.
423 */
424 Int spinUpTime = config->GetIntValue(ConfigureCDRip::ConfigID, String("SpinUpTimeDrive-").Append(GetDriveID()), 0);
425 UnsignedInt64 startTime = S::System::System::Clock();
426
427 while (spinUpTime > 0 && startTime - lastRead.GetNth(track.drive) > 2500 && S::System::System::Clock() - startTime < (UnsignedInt64) Math::Abs(spinUpTime * 1000))
428 {
429 Buffer<UnsignedByte> buffer(dataBufferSize);
430
431 BOOL abort = false;
432 LONG bytes = dataBufferSize;
433
434 ex_CR_RipChunk(cd, buffer, &bytes, abort);
435
436 ex_CR_CloseRipper(cd);
437 ex_CR_OpenRipper(cd, &bufferSize, startSector, endSector);
438 }
439
440 return True;
441 }
442
443 Int BoCA::DecoderCDRip::ReadData(Buffer<UnsignedByte> &data)
444 {
445 if (inBytes >= track.fileSize) return -1;
446
447 /* Set up buffer respecting empty samples to prepend.
448 */
449 Int prependBytes = 4 * prependSamples;
450 Int appendBytes = 4 * appendSamples;
451 Int skipBytes = 4 * skipSamples;
452
453 prependSamples = 0;
454 skipSamples = 0;
455
456 data.Resize(dataBufferSize + prependBytes);
457
458 if (prependBytes > 0) data.Zero();
459
460 /* Rip chunk.
461 */
462 BOOL abort = false;
463 LONG bytes = dataBufferSize;
464 CDEX_ERR error = ex_CR_RipChunk(cd, data + prependBytes, &bytes, abort);
465
466 if (error != CDEX_OK) return -1;
467
468 lastRead.SetNth(track.drive, S::System::System::Clock());
469
470 /* Append offset bytes to last track.
471 */
472 if (bytes == 0)
473 {
474 data.Resize(prependBytes + appendBytes - skipBytes);
475 data.Zero();
476
477 inBytes += data.Size();
478
479 return data.Size();
480 }
481
482 /* Strip samples to skip from the beginning.
483 */
484 Int dataBytes = Math::Min(track.fileSize - inBytes, (Int64) bytes + prependBytes - skipBytes);
485
486 if (skipBytes > 0) memmove(data, data + skipBytes, dataBytes);
487
488 inBytes += dataBytes;
489
490 return dataBytes;
491 }
492
493 Bool BoCA::DecoderCDRip::GetTrackSectors(Int &startSector, Int &endSector, Bool &lastTrack) const
494 {
495 AS::Registry &boca = AS::Registry::Get();
496 AS::DeviceInfoComponent *component = (AS::DeviceInfoComponent *) boca.CreateComponentByID("cdrip-info");
497
498 if (component == NIL) return False;
499
500 MCDI mcdi = component->GetNthDeviceMCDI(track.drive);
501
502 boca.DeleteComponent(component);
503
504 if (track.cdTrack == 0)
505 {
506 /* Special handling for hidden track one audio.
507 */
508 if (mcdi.GetNthEntryType(0) == ENTRY_AUDIO)
509 {
510 startSector = 0;
511 endSector = mcdi.GetNthEntryOffset(0) - 1;
512 lastTrack = False;
513
514 return True;
515 }
516 }
517 else
518 {
519 /* Handle regular tracks.
520 */
521 for (Int i = 0; i < mcdi.GetNumberOfEntries(); i++)
522 {
523 if (mcdi.GetNthEntryType(i) == ENTRY_AUDIO && mcdi.GetNthEntryTrackNumber(i) == track.cdTrack)
524 {
525 startSector = mcdi.GetNthEntryOffset(i);
526 endSector = startSector + mcdi.GetNthEntryTrackLength(i) - 1;
527
528 if (mcdi.GetNthEntryType(i + 1) == ENTRY_AUDIO) lastTrack = False;
529 else lastTrack = True;
530
531 return True;
532 }
533 }
534 }
535
536 return False;
537 }
538
539 ConfigLayer *BoCA::DecoderCDRip::GetConfigurationLayer()
540 {
541 if (configLayer == NIL) configLayer = new ConfigureCDRip();
542
543 return configLayer;
544 }
545
546 String BoCA::DecoderCDRip::GetDriveID() const
547 {
548 AS::Registry &boca = AS::Registry::Get();
549 AS::DeviceInfoComponent *info = (AS::DeviceInfoComponent *) boca.CreateComponentByID("cdrip-info");
550
551 if (info == NIL) return NIL;
552
553 const Device &device = info->GetNthDeviceInfo(track.drive);
554
555 boca.DeleteComponent(info);
556
557 return device.GetID();
558 }
559
560 static int cddb_sum(int n)
561 {
562 int ret = 0;
563
564 while (n > 0)
565 {
566 ret = ret + (n % 10);
567 n = n / 10;
568 }
569
570 return ret;
571 }
572
573 UnsignedInt32 BoCA::DecoderCDRip::ComputeDiscID(const MCDI &mcdi) const
574 {
575 Int numTocEntries = mcdi.GetNumberOfEntries();
576 Int n = 0;
577
578 for (Int i = 0; i < numTocEntries; i++)
579 {
580 Int offset = mcdi.GetNthEntryOffset(i) + 150;
581
582 n += cddb_sum(offset / 75);
583 }
584
585 Int t = mcdi.GetNthEntryOffset(numTocEntries) / 75 - mcdi.GetNthEntryOffset(0) / 75;
586
587 return ((n % 0xff) << 24 | t << 8 | numTocEntries);
588 }