1 /* cdrdao - write audio CD-Rs in disc-at-once mode
2 *
3 * Copyright (C) 1998-2001 Andreas Mueller <mueller@daneb.ping.de>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20
21 #include "CdTextEncoder.h"
22
23 #include <stddef.h>
24 #include <string.h>
25 #include <assert.h>
26
27 #include "log.h"
28 #include "Toc.h"
29 #include "CdTextItem.h"
30 #include "PWSubChannel96.h"
31
32
33 unsigned short CdTextEncoder::CRCTAB_[256] = {
34 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, 0x8108,
35 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, 0x1231, 0x0210,
36 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, 0x9339, 0x8318, 0xB37B,
37 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, 0x2462, 0x3443, 0x0420, 0x1401,
38 0x64E6, 0x74C7, 0x44A4, 0x5485, 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE,
39 0xF5CF, 0xC5AC, 0xD58D, 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6,
40 0x5695, 0x46B4, 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D,
41 0xC7BC, 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
42 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, 0x5AF5,
43 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12, 0xDBFD, 0xCBDC,
44 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A, 0x6CA6, 0x7C87, 0x4CE4,
45 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD,
46 0xAD2A, 0xBD0B, 0x8D68, 0x9D49, 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13,
47 0x2E32, 0x1E51, 0x0E70, 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A,
48 0x9F59, 0x8F78, 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E,
49 0xE16F, 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
50 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, 0x02B1,
51 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256, 0xB5EA, 0xA5CB,
52 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D, 0x34E2, 0x24C3, 0x14A0,
53 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, 0xA7DB, 0xB7FA, 0x8799, 0x97B8,
54 0xE75F, 0xF77E, 0xC71D, 0xD73C, 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657,
55 0x7676, 0x4615, 0x5634, 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9,
56 0xB98A, 0xA9AB, 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882,
57 0x28A3, 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
58 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, 0xFD2E,
59 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, 0x7C26, 0x6C07,
60 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1, 0xEF1F, 0xFF3E, 0xCF5D,
61 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, 0x6E17, 0x7E36, 0x4E55, 0x5E74,
62 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
63 };
64
65
66 class CdTextPackEntry {
67 public:
68 CdTextPackEntry(unsigned char packType, unsigned char trackNr,
69 unsigned char packId);
70
71 int blockNr();
72 void blockNr(int);
73
74 void characterPos(int);
75
76 union {
77 CdTextPack pack;
78 unsigned char packData[18];
79 };
80
81 CdTextPackEntry *next_;
82 };
83
CdTextPackEntry(unsigned char packType,unsigned char trackNr,unsigned char packId)84 CdTextPackEntry::CdTextPackEntry(unsigned char packType, unsigned char trackNr,
85 unsigned char packId)
86 {
87 memset(&pack, 0, sizeof(pack));
88 next_ = NULL;
89
90 pack.packType = packType;
91 pack.trackNumber = trackNr;
92 pack.sequenceNumber = packId;
93 }
94
blockNr()95 int CdTextPackEntry::blockNr()
96 {
97 return (pack.blockCharacter >> 4) & 0x07;
98 }
99
blockNr(int blockNr)100 void CdTextPackEntry::blockNr(int blockNr)
101 {
102 pack.blockCharacter &= 0x8f;
103 pack.blockCharacter |= (blockNr << 4) & 0x70;
104 }
105
characterPos(int pos)106 void CdTextPackEntry::characterPos(int pos)
107 {
108 pack.blockCharacter &= 0xf0;
109
110 if (pos > 15) {
111 pack.blockCharacter |= 0x0f;
112 }
113 else {
114 pack.blockCharacter |= pos;
115 }
116 }
117
118
CdTextEncoder(const Toc * toc)119 CdTextEncoder::CdTextEncoder(const Toc *toc)
120 {
121 int i;
122
123 toc_ = toc;
124
125 packCount_ = 0;
126 packs_ = lastPack_ = NULL;
127 lastPackPos_ = 0;
128
129 subChannels_ = NULL;
130 subChannelCount_ = 0;
131
132 for (i = 0; i < 8; i++)
133 memset(&(sizeInfo_[i]), 0, sizeof(CdTextSizeInfo));
134 }
135
~CdTextEncoder()136 CdTextEncoder::~CdTextEncoder()
137 {
138 long i;
139 CdTextPackEntry *pnext;
140
141 toc_ = NULL;
142
143 while (packs_ != NULL) {
144 pnext = packs_->next_;
145
146 delete packs_;
147 packs_ = pnext;
148 }
149
150 lastPack_ = NULL;
151
152 if (subChannels_ != NULL) {
153 for (i = 0; i < subChannelCount_; i++) {
154 delete subChannels_[i];
155 subChannels_[i] = NULL;
156 }
157
158 delete[] subChannels_;
159 subChannels_ = NULL;
160 }
161 }
162
encode()163 int CdTextEncoder::encode()
164 {
165 buildPacks();
166
167 calcCrcs();
168
169 if (packs_ != NULL) {
170 log_message(4, "\nCD-TEXT packs:");
171 CdTextPackEntry *prun;
172 for (prun = packs_; prun != NULL; prun = prun->next_) {
173 log_message(4, "%02x %02x %02x %02x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x CRC: %02x %02x", prun->pack.packType,
174 prun->pack.trackNumber, prun->pack.sequenceNumber,
175 prun->pack.blockCharacter, prun->pack.data[0],
176 prun->pack.data[1], prun->pack.data[2], prun->pack.data[3],
177 prun->pack.data[4], prun->pack.data[5], prun->pack.data[6],
178 prun->pack.data[7], prun->pack.data[8], prun->pack.data[9],
179 prun->pack.data[10], prun->pack.data[11],
180 prun->pack.crc0, prun->pack.crc1);
181 }
182 }
183
184 buildSubChannels();
185
186 return 0;
187 }
188
getSubChannels(long * subChannelCount)189 const PWSubChannel96 **CdTextEncoder::getSubChannels(long *subChannelCount)
190 {
191 *subChannelCount = subChannelCount_;
192
193 return (const PWSubChannel96 **)subChannels_;
194 }
195
buildPacks()196 void CdTextEncoder::buildPacks()
197 {
198 int blockNr;
199
200 for (blockNr = 0; blockNr <= 7; blockNr++) {
201 if (toc_->cdTextLanguage(blockNr) >= 0) {
202 // only build the packs if the language code is defined
203 packId_ = 0;
204
205 buildPacks(blockNr, CdTextItem::CDTEXT_TITLE);
206 buildPacks(blockNr, CdTextItem::CDTEXT_PERFORMER);
207 buildPacks(blockNr, CdTextItem::CDTEXT_SONGWRITER);
208 buildPacks(blockNr, CdTextItem::CDTEXT_COMPOSER);
209 buildPacks(blockNr, CdTextItem::CDTEXT_ARRANGER);
210 buildPacks(blockNr, CdTextItem::CDTEXT_MESSAGE);
211 buildPacks(blockNr, CdTextItem::CDTEXT_DISK_ID);
212 buildPacks(blockNr, CdTextItem::CDTEXT_GENRE);
213 buildPacks(blockNr, CdTextItem::CDTEXT_TOC_INFO1);
214 buildPacks(blockNr, CdTextItem::CDTEXT_TOC_INFO2);
215 buildPacks(blockNr, CdTextItem::CDTEXT_UPCEAN_ISRC);
216 }
217 }
218
219 buildSizeInfoPacks();
220 }
221
222 // Build packs for specified language (blockNr) and pack type. First the
223 // global pack is created and then the track specific packs.
buildPacks(int blockNr,CdTextItem::PackType type)224 void CdTextEncoder::buildPacks(int blockNr, CdTextItem::PackType type)
225 {
226 const CdTextItem *globalItem;
227 const CdTextItem *item;
228 int i;
229 int n = toc_->nofTracks();
230 int tracks;
231 int offset = toc_->firstTrackNo() == 0 ? 0 : toc_->firstTrackNo() - 1;
232
233 if ((globalItem = toc_->getCdTextItem(0, blockNr, type)) != NULL) {
234 encodeCdTextItem(0, blockNr, globalItem);
235 }
236
237 if (CdTextItem::isTrackPack(type)) {
238 // count tracks that have the current pack type defined
239 tracks = 0;
240
241 for (i = 1; i <= n; i++) {
242 if (toc_->getCdTextItem(i, blockNr, type) != NULL)
243 tracks++;
244 }
245
246 if (tracks > 0) {
247 if (globalItem == NULL && type == CdTextItem::CDTEXT_UPCEAN_ISRC) {
248 // Special handling for UPC/EAN field. If this field is not defined
249 // but ISRC codes are defined for the tracks an pack with an empty
250 // string is created.
251 CdTextItem upcEan(CdTextItem::CDTEXT_UPCEAN_ISRC, blockNr, "");
252 encodeCdTextItem(0, blockNr, &upcEan);
253 }
254
255 for (i = 1; i <= n; i++) {
256 if ((item = toc_->getCdTextItem(i, blockNr, type)) == NULL)
257 item = globalItem;
258
259 if (item != NULL)
260 encodeCdTextItem(offset + i, blockNr, item);
261 }
262 }
263 }
264 }
265
266 // Build CD-TEXT packs for a CdTextItem. Space from the last pack is
267 // used if the pack type and language are matching.
encodeCdTextItem(int trackNr,int blockNr,const CdTextItem * item)268 void CdTextEncoder::encodeCdTextItem(int trackNr, int blockNr,
269 const CdTextItem *item)
270 {
271 long dataLen = item->dataLen();
272 const unsigned char *data = item->data();
273 long pos, n;
274 CdTextPackEntry *pack = NULL;
275 int first = 1;
276
277 if (CdTextItem::isBinaryPack(item->packType())) {
278 pos = 0;
279
280 while (dataLen > 0) {
281 switch (item->packType()) {
282 case CdTextItem::CDTEXT_GENRE:
283 pack = new CdTextPackEntry(item->packType(), 0, packId_++);
284 if (pos > 0)
285 pack->characterPos((pos * 12) - 2);
286 break;
287 case CdTextItem::CDTEXT_TOC_INFO1:
288 if (pos == 0)
289 pack = new CdTextPackEntry(item->packType(), 0, packId_++);
290 else
291 pack = new CdTextPackEntry(item->packType(), ((pos - 1) * 4) + 1,
292 packId_++);
293 break;
294 default:
295 pack = new CdTextPackEntry(item->packType(), pos, packId_++);
296 break;
297 }
298
299 n = (dataLen > 12) ? 12 : dataLen;
300
301 memcpy(pack->pack.data, data, n);
302 pack->blockNr(blockNr);
303
304 appendPack(pack);
305 lastPackPos_ = 12;
306
307 data += n;
308 dataLen -= n;
309 pos++;
310 }
311 }
312 else {
313 pos = 0;
314
315 while (dataLen > 0) {
316 if (first && lastPack_ != NULL &&
317 lastPack_->pack.packType == item->packType() &&
318 lastPack_->blockNr() == blockNr &&
319 lastPackPos_ < 12) {
320 // we can use space from previous block
321 pack = lastPack_;
322 }
323 else {
324 pack = new CdTextPackEntry(item->packType(), trackNr, packId_++);
325 pack->blockNr(blockNr);
326 pack->characterPos(pos);
327 appendPack(pack);
328 lastPackPos_ = 0;
329 }
330
331 n = (dataLen > 12 - lastPackPos_) ? 12 - lastPackPos_ : dataLen;
332
333 memcpy(pack->pack.data + lastPackPos_, data, n);
334
335 data += n;
336 lastPackPos_ += n;
337 pos += n;
338 dataLen -= n;
339
340 first = 0;
341 }
342 }
343 }
344
345 // Create the SIZE_INFO CD-TEXT packs which contains a summary about all
346 // CD-TEXT packs.
buildSizeInfoPacks()347 void CdTextEncoder::buildSizeInfoPacks()
348 {
349 CdTextItem *sizeInfoItem;
350 int i;
351 int b;
352
353 if (lastPack_ == NULL) {
354 // no packs at all -> nothing to do
355 return;
356 }
357
358 for (b = 0; b < 8; b++) {
359 sizeInfo_[b].characterCode = 0; // ISO/IEC 8859-1
360
361 sizeInfo_[b].firstTrack = toc_->firstTrackNo() == 0 ? 1 : toc_->firstTrackNo();
362 sizeInfo_[b].lastTrack = toc_->firstTrackNo() == 0 ? toc_->nofTracks() : toc_->firstTrackNo() + toc_->nofTracks() - 1;
363
364 sizeInfo_[b].copyright = 0; // no copy protection
365
366 for (i = 0; i < 8; i++) {
367 if (sizeInfo_[b].lastSequenceNumber[i] > 0) {
368 // set language code if we have at least one pack for this language
369 sizeInfo_[b].languageCode[i] = toc_->cdTextLanguage(i);
370 // adjust the last sequence number for the packs we will create
371 sizeInfo_[b].lastSequenceNumber[i] += 3;
372 }
373 }
374
375 // adjust the counter for the packs we're currently creating
376 sizeInfo_[b].packTypeCount[15] += 3; // we create 3 packs
377 }
378
379 for (b = 0; b < 8; b++) {
380 if (sizeInfo_[b].lastSequenceNumber[b] > 0) {
381 // ' - 3' because we've already adjusted the pack count
382 packId_ = sizeInfo_[b].lastSequenceNumber[b] - 3 + 1;
383
384 sizeInfoItem = new CdTextItem(CdTextItem::CDTEXT_SIZE_INFO, b,
385 (unsigned char*)&(sizeInfo_[b]),
386 sizeof(CdTextSizeInfo));
387
388 encodeCdTextItem(0, b, sizeInfoItem);
389
390 delete sizeInfoItem;
391 }
392 }
393 }
394
395 // Calculates checksum for each CD-TEXT pack.
calcCrcs()396 void CdTextEncoder::calcCrcs()
397 {
398 CdTextPackEntry *prun;
399 register unsigned char data;
400 register unsigned short crc;
401 register int i;
402
403 for (prun = packs_; prun != NULL; prun = prun->next_) {
404 crc = 0;
405
406 for (i = 0; i < 16; i++) {
407 data = prun->packData[i];
408 crc = CRCTAB_[(crc >> 8) ^ data] ^ (crc << 8);
409 }
410
411 crc = ~crc;
412
413 prun->pack.crc0 = crc >> 8;
414 prun->pack.crc1 = crc;
415 }
416 }
417
418 // Builds the R-W sub-channel data for all CD-TEXT packs. The sub-channel
419 // data of each sector can hold 4 CD-TEXT packs.
buildSubChannels()420 void CdTextEncoder::buildSubChannels()
421 {
422 long i, j;
423 CdTextPackEntry *prun;
424 unsigned char buf[72];
425
426 switch (packCount_ % 4) {
427 case 0:
428 subChannelCount_ = packCount_ / 4;
429 break;
430 case 2:
431 subChannelCount_ = packCount_ / 2;
432 break;
433 default:
434 subChannelCount_ = packCount_;
435 break;
436 }
437
438 if (subChannelCount_ == 0) {
439 subChannels_ = NULL;
440 return;
441 }
442
443 subChannels_ = new PWSubChannel96*[subChannelCount_];
444
445 prun = packs_;
446
447 for (i = 0; i < subChannelCount_; i++) {
448 subChannels_[i] = new PWSubChannel96;
449
450 for (j = 0; j < 4; j++) {
451 memcpy(buf + j * 18, prun->packData, 18);
452
453 if ((prun = prun->next_) == NULL)
454 prun = packs_;
455 }
456
457 subChannels_[i]->setRawRWdata(buf);
458
459 #if 0
460 int k;
461
462 for (j = 0; j < 6; j++) {
463 log_message(0, "%ld:%d: ", i, j);
464 for (k = 0; k < 16; k++) {
465 log_message(0, "%02x ", subChannels_[i]->data()[j * 16 + k]);
466 }
467 log_message(0, "");
468 }
469
470 unsigned char buf1[72];
471
472 subChannels_[i]->getRawRWdata(buf1);
473
474 log_message(0, "Match: %d", memcmp(buf, buf1, 72));
475 #endif
476 }
477
478
479 assert(prun == packs_);
480 }
481
482
appendPack(CdTextPackEntry * pack)483 void CdTextEncoder::appendPack(CdTextPackEntry *pack)
484 {
485 int b;
486
487 assert(pack->blockNr() >= 0 && pack->blockNr() <= 7);
488
489 pack->next_ = NULL;
490
491 if (packs_ == NULL) {
492 packs_ = lastPack_ = pack;
493 }
494 else {
495 if (pack->blockNr() >= lastPack_->blockNr()) {
496 lastPack_->next_ = pack;
497 lastPack_ = pack;
498 }
499 else {
500 CdTextPackEntry *prun, *ppred;
501
502 for (ppred = NULL, prun = packs_; prun != NULL;
503 ppred = prun, prun = prun->next_) {
504 if (pack->blockNr() < prun->blockNr())
505 break;
506 }
507
508 if (prun == NULL) {
509 // this case should have been handled above
510 lastPack_->next_ = pack;
511 lastPack_ = pack;
512 }
513 else {
514 if (ppred == NULL) {
515 pack->next_ = packs_;
516 packs_ = pack;
517 }
518 else {
519 ppred->next_ = pack;
520 pack->next_ = prun;
521 }
522 }
523 }
524 }
525
526 packCount_++;
527
528 // update summary data used for creating the SIZE_INFO pack
529 sizeInfo_[pack->blockNr()].packTypeCount[pack->pack.packType - 0x80] += 1;
530
531 for (b = 0; b < 8; b++) {
532 if (pack->pack.sequenceNumber >
533 sizeInfo_[b].lastSequenceNumber[pack->blockNr()]) {
534 sizeInfo_[b].lastSequenceNumber[pack->blockNr()] =
535 pack->pack.sequenceNumber;
536 }
537 }
538 }
539