1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
|
/*
* This file has been modified for the cdrkit suite.
*
* The behaviour and appearence of the program code below can differ to a major
* extent from the version distributed by the original author(s).
*
* For details, see Changelog file distributed with the cdrkit package. If you
* received this file from another source then ask the distributing person for
* a log of modifications.
*
*/
/* @(#)paranoia.c 1.33 04/08/17 J. Schilling from cdparanoia-III-alpha9.8 */
/*
* Modifications to make the code portable Copyright (c) 2002 J. Schilling
*/
/*
* CopyPolicy: GNU Public License 2 applies
* Copyright (C) by Monty (xiphmont@mit.edu)
*
* Toplevel file for the paranoia abstraction over the cdda lib
*
*/
/* immediate todo:: */
/* Allow disabling of root fixups? */
/*
* Dupe bytes are creeping into cases that require greater overlap
* than a single fragment can provide. We need to check against a
* larger area* (+/-32 sectors of root?) to better eliminate
* dupes. Of course this leads to other problems... Is it actually a
* practically solvable problem?
*/
/* Bimodal overlap distributions break us. */
/* scratch detection/tolerance not implemented yet */
/*
* Da new shtick: verification now a two-step assymetric process.
*
* A single 'verified/reconstructed' data segment cache, and then the
* multiple fragment cache
*
* verify a newly read block against previous blocks; do it only this
* once. We maintain a list of 'verified sections' from these matches.
*
* We then glom these verified areas into a new data buffer.
* Defragmentation fixups are allowed here alone.
*
* We also now track where read boundaries actually happened; do not
* verify across matching boundaries.
*/
/*
* Silence. "It's BAAAAAAaaack."
*
* audio is now treated as great continents of values floating on a
* mantle of molten silence. Silence is not handled by basic
* verification at all; we simply anchor sections of nonzero audio to a
* position and fill in everything else as silence. We also note the
* audio that interfaces with silence; an edge must be 'wet'.
*/
#include <mconfig.h>
#include <allocax.h>
#include <stdxlib.h>
#include <unixstd.h>
#include <standard.h>
#include <utypes.h>
#include <stdio.h>
#include <strdefs.h>
#include "p_block.h"
#include "cdda_paranoia.h"
#include "overlap.h"
#include "gap.h"
#include "isort.h"
#include "pmalloc.h"
/*
* used by: i_iterate_stage2() / i_stage2_each()
*/
typedef struct sync_result {
long offset;
long begin;
long end;
} sync_result;
static inline long re(root_block *root);
static inline long rb(root_block *root);
static inline long rs(root_block *root);
static inline Int16_t *rv(root_block *root);
static inline long i_paranoia_overlap(Int16_t *buffA, Int16_t *buffB,
long offsetA, long offsetB,
long sizeA, long sizeB,
long *ret_begin, long *ret_end);
static inline long i_paranoia_overlap2(Int16_t *buffA, Int16_t *buffB,
Uchar *flagsA, Uchar *flagsB,
long offsetA, long offsetB,
long sizeA, long sizeB,
long *ret_begin, long *ret_end);
static inline long do_const_sync(c_block *A,
sort_info *B, Uchar *flagB,
long posA, long posB,
long *begin, long *end, long *offset);
static inline long try_sort_sync(cdrom_paranoia *p,
sort_info *A, Uchar *Aflags,
c_block *B,
long post, long *begin, long *end,
long *offset, void (*callback) (long, int));
static inline void stage1_matched(c_block *old, c_block *new,
long matchbegin, long matchend,
long matchoffset,
void (*callback) (long, int));
static long i_iterate_stage1(cdrom_paranoia *p, c_block *old, c_block *new,
void (*callback) (long, int));
static long i_stage1(cdrom_paranoia *p, c_block *new,
void (*callback) (long, int));
static long i_iterate_stage2(cdrom_paranoia *p, v_fragment *v, sync_result *r,
void (*callback)(long, int));
static void i_silence_test(root_block *root);
static long i_silence_match(root_block *root, v_fragment *v,
void (*callback) (long, int));
static long i_stage2_each(root_block *root, v_fragment *v,
void (*callback) (long, int));
static int i_init_root(root_block *root, v_fragment *v,
long begin, void (*callback)(long, int));
static int vsort(const void *a, const void *b);
static int i_stage2(cdrom_paranoia *p, long beginword, long endword,
void (*callback)(long, int));
static void i_end_case(cdrom_paranoia *p, long endword,
void (*callback)(long, int));
static void verify_skip_case(cdrom_paranoia *p,
void (*callback)(long, int));
void paranoia_free(cdrom_paranoia *p);
void paranoia_modeset(cdrom_paranoia *p, int enable);
long paranoia_seek(cdrom_paranoia *p, long seek, int mode);
c_block *i_read_c_block(cdrom_paranoia *p, long beginword, long endword,
void (*callback)(long,int));
Int16_t *paranoia_read(cdrom_paranoia *p, void (*callback)(long, int));
Int16_t *paranoia_read_limited(cdrom_paranoia *p, void (*callback)(long, int),
int max_retries);
void paranoia_overlapset(cdrom_paranoia *p, long overlap);
static inline long re(root_block *root)
{
if (!root)
return (-1);
if (!root->vector)
return (-1);
return (ce(root->vector));
}
static inline long rb(root_block *root)
{
if (!root)
return (-1);
if (!root->vector)
return (-1);
return (cb(root->vector));
}
static inline long rs(root_block *root)
{
if (!root)
return (-1);
if (!root->vector)
return (-1);
return (cs(root->vector));
}
static inline Int16_t *rv(root_block *root)
{
if (!root)
return (NULL);
if (!root->vector)
return (NULL);
return (cv(root->vector));
}
#define rc(r) ((r)->vector)
/*
* matching and analysis code
*/
static inline long
i_paranoia_overlap(Int16_t *buffA, Int16_t *buffB, long offsetA, long offsetB,
long sizeA, long sizeB, long *ret_begin, long *ret_end)
{
long beginA = offsetA;
long endA = offsetA;
long beginB = offsetB;
long endB = offsetB;
for (; beginA >= 0 && beginB >= 0; beginA--, beginB--)
if (buffA[beginA] != buffB[beginB])
break;
beginA++;
beginB++;
for (; endA < sizeA && endB < sizeB; endA++, endB++)
if (buffA[endA] != buffB[endB])
break;
if (ret_begin)
*ret_begin = beginA;
if (ret_end)
*ret_end = endA;
return (endA - beginA);
}
static inline long
i_paranoia_overlap2(Int16_t *buffA, Int16_t *buffB, Uchar *flagsA,
Uchar *flagsB, long offsetA, long offsetB, long sizeA,
long sizeB, long *ret_begin, long *ret_end)
{
long beginA = offsetA;
long endA = offsetA;
long beginB = offsetB;
long endB = offsetB;
for (; beginA >= 0 && beginB >= 0; beginA--, beginB--) {
if (buffA[beginA] != buffB[beginB])
break;
/*
* don't allow matching across matching sector boundaries
*/
if ((flagsA[beginA] & flagsB[beginB] & 1)) {
beginA--;
beginB--;
break;
}
/*
* don't allow matching through known missing data
*/
if ((flagsA[beginA] & 2) || (flagsB[beginB] & 2))
break;
}
beginA++;
beginB++;
for (; endA < sizeA && endB < sizeB; endA++, endB++) {
if (buffA[endA] != buffB[endB])
break;
/*
* don't allow matching across matching sector boundaries
*/
if ((flagsA[endA] & flagsB[endB] & 1) && endA != beginA) {
break;
}
/*
* don't allow matching through known missing data
*/
if ((flagsA[endA] & 2) || (flagsB[endB] & 2))
break;
}
if (ret_begin)
*ret_begin = beginA;
if (ret_end)
*ret_end = endA;
return (endA - beginA);
}
/* Top level of the first stage matcher */
/*
* We match each analysis point of new to the preexisting blocks
* recursively. We can also optionally maintain a list of fragments of
* the preexisting block that didn't match anything, and match them back
* afterward.
*/
#define OVERLAP_ADJ (MIN_WORDS_OVERLAP/2-1)
static inline long
do_const_sync(c_block *A, sort_info *B, Uchar *flagB, long posA, long posB,
long *begin, long *end, long *offset)
{
Uchar *flagA = A->flags;
long ret = 0;
if (flagB == NULL)
ret = i_paranoia_overlap(cv(A), iv(B), posA, posB,
cs(A), is(B), begin, end);
else if ((flagB[posB] & 2) == 0)
ret = i_paranoia_overlap2(cv(A), iv(B), flagA, flagB, posA, posB, cs(A),
is(B), begin, end);
if (ret > MIN_WORDS_SEARCH) {
*offset = (posA + cb(A)) - (posB + ib(B));
*begin += cb(A);
*end += cb(A);
return (ret);
}
return (0);
}
/*
* post is w.r.t. B. in stage one, we post from old. In stage 2 we
* post from root. Begin, end, offset count from B's frame of
* reference
*/
static inline long
try_sort_sync(cdrom_paranoia *p, sort_info *A, Uchar *Aflags, c_block *B,
long post, long *begin, long *end, long *offset,
void (*callback)(long, int))
{
long dynoverlap = p->dynoverlap;
sort_link *ptr = NULL;
Uchar *Bflags = B->flags;
/*
* block flag matches 0x02 (unmatchable)
*/
if (Bflags == NULL || (Bflags[post - cb(B)] & 2) == 0) {
/*
* always try absolute offset zero first!
*/
{
long zeropos = post - ib(A);
if (zeropos >= 0 && zeropos < is(A)) {
if (cv(B)[post - cb(B)] == iv(A)[zeropos]) {
if (do_const_sync(B, A, Aflags,
post - cb(B), zeropos,
begin, end, offset)) {
offset_add_value(p, &(p->stage1), *offset, callback);
return (1);
}
}
}
}
} else
return (0);
ptr = sort_getmatch(A, post - ib(A), dynoverlap, cv(B)[post - cb(B)]);
while (ptr) {
if (do_const_sync(B, A, Aflags,
post - cb(B), ipos(A, ptr),
begin, end, offset)) {
offset_add_value(p, &(p->stage1), *offset, callback);
return (1);
}
ptr = sort_nextmatch(A, ptr);
}
*begin = -1;
*end = -1;
*offset = -1;
return (0);
}
static inline void
stage1_matched(c_block *old, c_block *new, long matchbegin, long matchend,
long matchoffset, void (*callback)(long, int))
{
long i;
long oldadjbegin = matchbegin - cb(old);
long oldadjend = matchend - cb(old);
long newadjbegin = matchbegin - matchoffset - cb(new);
long newadjend = matchend - matchoffset - cb(new);
if (matchbegin - matchoffset <= cb(new) ||
matchbegin <= cb(old) ||
(new->flags[newadjbegin] & 1) ||
(old->flags[oldadjbegin] & 1)) {
if (matchoffset)
if (callback)
(*callback) (matchbegin, PARANOIA_CB_FIXUP_EDGE);
} else if (callback)
(*callback) (matchbegin, PARANOIA_CB_FIXUP_ATOM);
if (matchend - matchoffset >= ce(new) ||
(new->flags[newadjend] & 1) ||
matchend >= ce(old) ||
(old->flags[oldadjend] & 1)) {
if (matchoffset)
if (callback)
(*callback) (matchend, PARANOIA_CB_FIXUP_EDGE);
} else if (callback)
(*callback) (matchend, PARANOIA_CB_FIXUP_ATOM);
/*
* Mark the verification flags. Don't mark the first or last OVERLAP/2
* elements so that overlapping fragments have to overlap by OVERLAP to
* actually merge. We also remove elements from the sort such that
* later sorts do not have to sift through already matched data
*/
newadjbegin += OVERLAP_ADJ;
newadjend -= OVERLAP_ADJ;
for (i = newadjbegin; i < newadjend; i++)
new->flags[i] |= 4; /* mark verified */
oldadjbegin += OVERLAP_ADJ;
oldadjend -= OVERLAP_ADJ;
for (i = oldadjbegin; i < oldadjend; i++)
old->flags[i] |= 4; /* mark verified */
}
#define CB_NULL (void (*)(long, int))0
static long
i_iterate_stage1(cdrom_paranoia *p, c_block *old, c_block *new,
void (*callback)(long, int))
{
long matchbegin = -1;
long matchend = -1;
long matchoffset;
/*
* we no longer try to spread the stage one search area by dynoverlap
*/
long searchend = min(ce(old), ce(new));
long searchbegin = max(cb(old), cb(new));
long searchsize = searchend - searchbegin;
sort_info *i = p->sortcache;
long ret = 0;
long j;
long tried = 0;
long matched = 0;
if (searchsize <= 0)
return (0);
/*
* match return values are in terms of the new vector, not old
*/
for (j = searchbegin; j < searchend; j += 23) {
if ((new->flags[j - cb(new)] & 6) == 0) {
tried++;
if (try_sort_sync(p, i, new->flags, old, j, &matchbegin, &matchend, &matchoffset,
callback) == 1) {
matched += matchend - matchbegin;
/*
* purely cosmetic: if we're matching zeros,
* don't use the callback because they will
* appear to be all skewed
*/
{
long jj = matchbegin - cb(old);
long end = matchend - cb(old);
for (; jj < end; jj++)
if (cv(old)[jj] != 0)
break;
if (jj < end) {
stage1_matched(old, new, matchbegin, matchend, matchoffset, callback);
} else {
stage1_matched(old, new, matchbegin, matchend, matchoffset, CB_NULL);
}
}
ret++;
if (matchend - 1 > j)
j = matchend - 1;
}
}
}
#ifdef NOISY
fprintf(stderr, "iterate_stage1: search area=%ld[%ld-%ld] tried=%ld matched=%ld spans=%ld\n",
searchsize, searchbegin, searchend, tried, matched, ret);
#endif
return (ret);
}
static long
i_stage1(cdrom_paranoia *p, c_block *new, void (*callback)(long, int))
{
long size = cs(new);
c_block *ptr = c_last(p);
int ret = 0;
long begin = 0;
long end;
if (ptr)
sort_setup(p->sortcache, cv(new), &cb(new), cs(new),
cb(new), ce(new));
while (ptr && ptr != new) {
if (callback)
(*callback) (cb(new), PARANOIA_CB_VERIFY);
i_iterate_stage1(p, ptr, new, callback);
ptr = c_prev(ptr);
}
/*
* parse the verified areas of new into v_fragments
*/
begin = 0;
while (begin < size) {
for (; begin < size; begin++)
if (new->flags[begin] & 4)
break;
for (end = begin; end < size; end++)
if ((new->flags[end] & 4) == 0)
break;
if (begin >= size)
break;
ret++;
new_v_fragment(p, new, cb(new) + max(0, begin - OVERLAP_ADJ),
cb(new) + min(size, end + OVERLAP_ADJ),
(end + OVERLAP_ADJ >= size && new->lastsector));
begin = end;
}
return (ret);
}
/*
* reconcile v_fragments to root buffer. Free if matched, fragment/fixup root
* if necessary
*
* do *not* match using zero posts
*/
static long
i_iterate_stage2(cdrom_paranoia *p, v_fragment *v, sync_result *r,
void (*callback)(long, int))
{
root_block *root = &(p->root);
long matchbegin = -1;
long matchend = -1;
long offset;
long fbv;
long fev;
#ifdef NOISY
fprintf(stderr, "Stage 2 search: fbv=%ld fev=%ld\n", fb(v), fe(v));
#endif
if (min(fe(v) + p->dynoverlap, re(root)) -
max(fb(v) - p->dynoverlap, rb(root)) <= 0)
return (0);
if (callback)
(*callback) (fb(v), PARANOIA_CB_VERIFY);
/*
* just a bit of v; determine the correct area
*/
fbv = max(fb(v), rb(root) - p->dynoverlap);
/*
* we want to avoid zeroes
*/
while (fbv < fe(v) && fv(v)[fbv - fb(v)] == 0)
fbv++;
if (fbv == fe(v))
return (0);
fev = min(min(fbv + 256, re(root) + p->dynoverlap), fe(v));
{
/*
* spread the search area a bit. We post from root, so
* containment must strictly adhere to root
*/
long searchend = min(fev + p->dynoverlap, re(root));
long searchbegin = max(fbv - p->dynoverlap, rb(root));
sort_info *i = p->sortcache;
long j;
sort_setup(i, fv(v), &fb(v), fs(v), fbv, fev);
for (j = searchbegin; j < searchend; j += 23) {
while (j < searchend && rv(root)[j - rb(root)] == 0)
j++;
if (j == searchend)
break;
if (try_sort_sync(p, i, (Uchar *)0, rc(root), j,
&matchbegin, &matchend, &offset, callback)) {
r->begin = matchbegin;
r->end = matchend;
r->offset = -offset;
if (offset)
if (callback)
(*callback) (r->begin, PARANOIA_CB_FIXUP_EDGE);
return (1);
}
}
}
return (0);
}
/*
* simple test for a root vector that ends in silence
*/
static void i_silence_test(root_block *root)
{
Int16_t *vec = rv(root);
long end = re(root) - rb(root) - 1;
long j;
for (j = end - 1; j >= 0; j--)
if (vec[j] != 0)
break;
if (j < 0 || end - j > MIN_SILENCE_BOUNDARY) {
if (j < 0)
j = 0;
root->silenceflag = 1;
root->silencebegin = rb(root) + j;
if (root->silencebegin < root->returnedlimit)
root->silencebegin = root->returnedlimit;
}
}
/*
* match into silence vectors at offset zero if at all possible. This
* also must be called with vectors in ascending begin order in case
* there are nonzero islands
*/
static long
i_silence_match(root_block *root, v_fragment *v, void (*callback)(long, int))
{
cdrom_paranoia *p = v->p;
Int16_t *vec = fv(v);
long end = fs(v);
long begin;
long j;
/*
* does this vector begin wet?
*/
if (end < MIN_SILENCE_BOUNDARY)
return (0);
for (j = 0; j < end; j++)
if (vec[j] != 0)
break;
if (j < MIN_SILENCE_BOUNDARY)
return (0);
j += fb(v);
/*
* is the new silent section ahead of the end of the old
* by < p->dynoverlap?
*/
if (fb(v) >= re(root) && fb(v) - p->dynoverlap < re(root)) {
/*
* extend the zeroed area of root
* XXX dynarrays are not needed here.
*/
long addto = fb(v) + MIN_SILENCE_BOUNDARY - re(root);
/* Int16_t avec[addto];*/
#ifdef HAVE_ALLOCA
Int16_t *avec = alloca(addto * sizeof (Int16_t));
#else
Int16_t *avec = _pmalloc(addto * sizeof (Int16_t));
#endif
memset(avec, 0, sizeof (avec));
c_append(rc(root), avec, addto);
#ifndef HAVE_ALLOCA
_pfree(avec);
#endif
}
/*
* do we have an 'effortless' overlap?
*/
begin = max(fb(v), root->silencebegin);
end = min(j, re(root));
if (begin < end) {
/*
* don't use it unless it will extend...
*/
if (fe(v) > re(root)) {
long voff = begin - fb(v);
c_remove(rc(root), begin - rb(root), -1);
c_append(rc(root), vec + voff, fs(v) - voff);
}
offset_add_value(p, &p->stage2, 0, callback);
} else {
if (j < begin) {
/*
* OK, we'll have to force it a bit as the root is
* jittered forward
*/
long voff = j - fb(v);
/*
* don't use it unless it will extend...
*/
if (begin + fs(v) - voff > re(root)) {
c_remove(rc(root), root->silencebegin - rb(root), -1);
c_append(rc(root), vec + voff, fs(v) - voff);
}
offset_add_value(p, &p->stage2, end - begin, callback);
} else
return (0);
}
/*
* test the new root vector for ending in silence
*/
root->silenceflag = 0;
i_silence_test(root);
if (v->lastsector)
root->lastsector = 1;
free_v_fragment(v);
return (1);
}
static long
i_stage2_each(root_block *root, v_fragment *v, void (*callback)(long, int))
{
cdrom_paranoia *p = v->p;
long dynoverlap = p->dynoverlap / 2 * 2;
if (!v || !v->one)
return (0);
if (!rv(root)) {
return (0);
} else {
sync_result r;
if (i_iterate_stage2(p, v, &r, callback)) {
long begin = r.begin - rb(root);
long end = r.end - rb(root);
long offset = r.begin + r.offset - fb(v) - begin;
long temp;
c_block *l = NULL;
/*
* we have a match! We don't rematch off rift, we chase
* the match all the way to both extremes doing rift
* analysis.
*/
#ifdef NOISY
fprintf(stderr, "Stage 2 match\n");
#endif
/*
* chase backward
* note that we don't extend back right now, only
* forward.
*/
while ((begin + offset > 0 && begin > 0)) {
long matchA = 0,
matchB = 0,
matchC = 0;
long beginL = begin + offset;
if (l == NULL) {
Int16_t *buff = _pmalloc(fs(v) * sizeof (Int16_t));
l = c_alloc(buff, fb(v), fs(v));
memcpy(buff, fv(v), fs(v) * sizeof (Int16_t));
}
i_analyze_rift_r(rv(root), cv(l),
rs(root), cs(l),
begin - 1, beginL - 1,
&matchA, &matchB, &matchC);
#ifdef NOISY
fprintf(stderr, "matching rootR: matchA:%ld matchB:%ld matchC:%ld\n",
matchA, matchB, matchC);
#endif
if (matchA) {
/*
* a problem with root
*/
if (matchA > 0) {
/*
* dropped bytes; add back from v
*/
if (callback)
(*callback) (begin + rb(root) - 1, PARANOIA_CB_FIXUP_DROPPED);
if (rb(root) + begin < p->root.returnedlimit)
break;
else {
c_insert(rc(root), begin, cv(l) + beginL - matchA,
matchA);
offset -= matchA;
begin += matchA;
end += matchA;
}
} else {
/*
* duplicate bytes; drop from root
*/
if (callback)
(*callback) (begin + rb(root) - 1, PARANOIA_CB_FIXUP_DUPED);
if (rb(root) + begin + matchA < p->root.returnedlimit)
break;
else {
c_remove(rc(root), begin + matchA, -matchA);
offset -= matchA;
begin += matchA;
end += matchA;
}
}
} else if (matchB) {
/*
* a problem with the fragment
*/
if (matchB > 0) {
/*
* dropped bytes
*/
if (callback)
(*callback) (begin + rb(root) - 1, PARANOIA_CB_FIXUP_DROPPED);
c_insert(l, beginL, rv(root) + begin - matchB,
matchB);
offset += matchB;
} else {
/*
* duplicate bytes
*/
if (callback)
(*callback) (begin + rb(root) - 1, PARANOIA_CB_FIXUP_DUPED);
c_remove(l, beginL + matchB, -matchB);
offset += matchB;
}
} else if (matchC) {
/*
* Uhh... problem with both
* Set 'disagree' flags in root
*/
if (rb(root) + begin - matchC < p->root.returnedlimit)
break;
c_overwrite(rc(root), begin - matchC,
cv(l) + beginL - matchC, matchC);
} else {
/*
* do we have a mismatch due to silence
* beginning/end case?
* in the 'chase back' case, we don't
* do anything.
* Did not determine nature of
* difficulty... report and bail
*/
/* RRR(*callback)(post,PARANOIA_CB_XXX); */
break;
}
/*
* not the most efficient way, but it will do
* for now
*/
beginL = begin + offset;
i_paranoia_overlap(rv(root), cv(l),
begin, beginL,
rs(root), cs(l),
&begin, &end);
}
/*
* chase forward
*/
temp = l ? cs(l) : fs(v);
while (end + offset < temp && end < rs(root)) {
long matchA = 0,
matchB = 0,
matchC = 0;
long beginL = begin + offset;
long endL = end + offset;
if (l == NULL) {
Int16_t *buff = _pmalloc(fs(v) * sizeof (Int16_t));
l = c_alloc(buff, fb(v), fs(v));
memcpy(buff, fv(v), fs(v) * sizeof (Int16_t));
}
i_analyze_rift_f(rv(root), cv(l),
rs(root), cs(l),
end, endL,
&matchA, &matchB, &matchC);
#ifdef NOISY
fprintf(stderr, "matching rootF: matchA:%ld matchB:%ld matchC:%ld\n",
matchA, matchB, matchC);
#endif
if (matchA) {
/*
* a problem with root
*/
if (matchA > 0) {
/*
* dropped bytes; add back from v
*/
if (callback)
(*callback) (end + rb(root), PARANOIA_CB_FIXUP_DROPPED);
if (end + rb(root) < p->root.returnedlimit)
break;
c_insert(rc(root), end, cv(l) + endL, matchA);
} else {
/*
* duplicate bytes; drop from root
*/
if (callback)
(*callback) (end + rb(root), PARANOIA_CB_FIXUP_DUPED);
if (end + rb(root) < p->root.returnedlimit)
break;
c_remove(rc(root), end, -matchA);
}
} else if (matchB) {
/*
* a problem with the fragment
*/
if (matchB > 0) {
/*
* dropped bytes
*/
if (callback)
(*callback) (end + rb(root), PARANOIA_CB_FIXUP_DROPPED);
c_insert(l, endL, rv(root) + end, matchB);
} else {
/*
* duplicate bytes
*/
if (callback)
(*callback) (end + rb(root), PARANOIA_CB_FIXUP_DUPED);
c_remove(l, endL, -matchB);
}
} else if (matchC) {
/*
* Uhh... problem with both
* Set 'disagree' flags in root
*/
if (end + rb(root) < p->root.returnedlimit)
break;
c_overwrite(rc(root), end, cv(l) + endL, matchC);
} else {
analyze_rift_silence_f(rv(root), cv(l),
rs(root), cs(l),
end, endL,
&matchA, &matchB);
if (matchA) {
/*
* silence in root
* Can only do this if we haven't
* already returned data
*/
if (end + rb(root) >= p->root.returnedlimit) {
c_remove(rc(root), end, -1);
}
} else if (matchB) {
/*
* silence in fragment; lose it
*/
if (l)
i_cblock_destructor(l);
free_v_fragment(v);
return (1);
} else {
/*
* Could not determine nature of
* difficulty... report and bail
*/
/* RRR(*callback)(post,PARANOIA_CB_XXX); */
}
break;
}
/*
* not the most efficient way, but it will do for now
*/
i_paranoia_overlap(rv(root), cv(l),
begin, beginL,
rs(root), cs(l),
(long *)0, &end);
}
/*
* if this extends our range, let's glom
*/
{
long sizeA = rs(root);
long sizeB;
long vecbegin;
Int16_t *vector;
if (l) {
sizeB = cs(l);
vector = cv(l);
vecbegin = cb(l);
} else {
sizeB = fs(v);
vector = fv(v);
vecbegin = fb(v);
}
if (sizeB - offset > sizeA || v->lastsector) {
if (v->lastsector) {
root->lastsector = 1;
}
if (end < sizeA)
c_remove(rc(root), end, -1);
if (sizeB - offset - end)
c_append(rc(root), vector + end + offset,
sizeB - offset - end);
i_silence_test(root);
/*
* add offset into dynoverlap stats
*/
offset_add_value(p, &p->stage2, offset + vecbegin - rb(root), callback);
}
}
if (l)
i_cblock_destructor(l);
free_v_fragment(v);
return (1);
} else {
/*
* D'oh. No match. What to do with the fragment?
*/
if (fe(v) + dynoverlap < re(root) && !root->silenceflag) {
/*
* It *should* have matched. No good; free it.
*/
free_v_fragment(v);
}
/*
* otherwise, we likely want this for an upcoming match
* we don't free the sort info (if it was collected)
*/
return (0);
}
}
}
static int
i_init_root(root_block *root, v_fragment *v, long begin,
void (*callback)(long, int))
{
if (fb(v) <= begin && fe(v) > begin) {
root->lastsector = v->lastsector;
root->returnedlimit = begin;
if (rv(root)) {
i_cblock_destructor(rc(root));
rc(root) = NULL;
}
{
Int16_t *buff = _pmalloc(fs(v) * sizeof (Int16_t));
memcpy(buff, fv(v), fs(v) * sizeof (Int16_t));
root->vector = c_alloc(buff, fb(v), fs(v));
}
i_silence_test(root);
return (1);
} else
return (0);
}
static int vsort(const void *a, const void *b)
{
return ((*(v_fragment **) a)->begin - (*(v_fragment **) b)->begin);
}
static int
i_stage2(cdrom_paranoia *p, long beginword, long endword,
void (*callback)(long, int))
{
int flag = 1;
int ret = 0;
root_block *root = &(p->root);
#ifdef NOISY
fprintf(stderr, "Fragments:%ld\n", p->fragments->active);
fflush(stderr);
#endif
/*
* even when the 'silence flag' is lit, we try to do non-silence
* matching in the event that there are still audio vectors with
* content to be sunk before the silence
*/
while (flag) {
/*
* loop through all the current fragments
*/
v_fragment *first = v_first(p);
long active = p->fragments->active,
count = 0;
#ifdef HAVE_DYN_ARRAYS
v_fragment *list[active];
#else
v_fragment **list = _pmalloc(active * sizeof (v_fragment *));
#endif
while (first) {
v_fragment *next = v_next(first);
list[count++] = first;
first = next;
}
flag = 0;
if (count) {
/*
* sorted in ascending order of beginning
*/
qsort(list, active, sizeof (v_fragment *), vsort);
/*
* we try a nonzero based match even if in silent mode
* in the case that there are still cached vectors to
* sink behind continent->ocean boundary
*/
for (count = 0; count < active; count++) {
first = list[count];
if (first->one) {
if (rv(root) == NULL) {
if (i_init_root(&(p->root), first, beginword, callback)) {
free_v_fragment(first);
flag = 1;
ret++;
}
} else {
if (i_stage2_each(root, first, callback)) {
ret++;
flag = 1;
}
}
}
}
/*
* silence handling
*/
if (!flag && p->root.silenceflag) {
for (count = 0; count < active; count++) {
first = list[count];
if (first->one) {
if (rv(root) != NULL) {
if (i_silence_match(root, first, callback)) {
ret++;
flag = 1;
}
}
}
}
}
}
#ifndef HAVE_DYN_ARRAYS
_pfree(list);
#endif
}
return (ret);
}
static void
i_end_case(cdrom_paranoia *p, long endword, void (*callback)(long, int))
{
root_block *root = &p->root;
/*
* have an 'end' flag; if we've just read in the last sector in a
* session, set the flag. If we verify to the end of a fragment
* which has the end flag set, we're done (set a done flag).
* Pad zeroes to the end of the read
*/
if (root->lastsector == 0)
return;
if (endword < re(root))
return;
{
long addto = endword - re(root);
char *temp = _pcalloc(addto, sizeof (char) * 2);
c_append(rc(root), (void *) temp, addto);
_pfree(temp);
/*
* trash da cache
*/
paranoia_resetcache(p);
}
}
/*
* We want to add a sector. Look through the caches for something that
* spans. Also look at the flags on the c_block... if this is an
* obliterated sector, get a bit of a chunk past the obliteration.
*
* Not terribly smart right now, actually. We can probably find
* *some* match with a cache block somewhere. Take it and continue it
* through the skip
*/
static void
verify_skip_case(cdrom_paranoia *p, void (*callback)(long, int))
{
root_block *root = &(p->root);
c_block *graft = NULL;
int vflag = 0;
int gend = 0;
long post;
#ifdef NOISY
fprintf(stderr, "\nskipping\n");
#endif
if (rv(root) == NULL) {
post = 0;
} else {
post = re(root);
}
if (post == -1)
post = 0;
if (callback)
(*callback) (post, PARANOIA_CB_SKIP);
if (p->enable & PARANOIA_MODE_NEVERSKIP)
return;
/*
* We want to add a sector. Look for a c_block that spans,
* preferrably a verified area
*/
{
c_block *c = c_first(p);
while (c) {
long cbegin = cb(c);
long cend = ce(c);
if (cbegin <= post && cend > post) {
long vend = post;
if (c->flags[post - cbegin] & 4) {
/*
* verified area!
*/
while (vend < cend && (c->flags[vend - cbegin] & 4))
vend++;
if (!vflag || vend > vflag) {
graft = c;
gend = vend;
}
vflag = 1;
} else {
/*
* not a verified area
*/
if (!vflag) {
while (vend < cend && (c->flags[vend - cbegin] & 4) == 0)
vend++;
if (graft == NULL || gend > vend) {
/*
* smallest unverified area
*/
graft = c;
gend = vend;
}
}
}
}
c = c_next(c);
}
if (graft) {
long cbegin = cb(graft);
long cend = ce(graft);
while (gend < cend && (graft->flags[gend - cbegin] & 4))
gend++;
gend = min(gend + OVERLAP_ADJ, cend);
if (rv(root) == NULL) {
Int16_t *buff = _pmalloc(cs(graft));
memcpy(buff, cv(graft), cs(graft));
rc(root) = c_alloc(buff, cb(graft), cs(graft));
} else {
c_append(rc(root), cv(graft) + post - cbegin,
gend - post);
}
root->returnedlimit = re(root);
return;
}
}
/*
* No? Fine. Great. Write in some zeroes :-P
*/
{
void *temp = _pcalloc(CD_FRAMESIZE_RAW, sizeof (Int16_t));
if (rv(root) == NULL) {
rc(root) = c_alloc(temp, post, CD_FRAMESIZE_RAW);
} else {
c_append(rc(root), temp, CD_FRAMESIZE_RAW);
_pfree(temp);
}
root->returnedlimit = re(root);
}
}
/*
* toplevel
*/
void paranoia_free(cdrom_paranoia *p)
{
paranoia_resetall(p);
sort_free(p->sortcache);
_pfree(p);
}
void paranoia_modeset(cdrom_paranoia *p, int enable)
{
p->enable = enable;
}
long paranoia_seek(cdrom_paranoia *p, long seek, int mode)
{
long sector;
long ret;
switch (mode) {
case SEEK_SET:
sector = seek;
break;
case SEEK_END:
sector = cdda_disc_lastsector(p->d) + seek;
break;
default:
sector = p->cursor + seek;
break;
}
if (cdda_sector_gettrack(p->d, sector) == -1)
return (-1);
i_cblock_destructor(p->root.vector);
p->root.vector = NULL;
p->root.lastsector = 0;
p->root.returnedlimit = 0;
ret = p->cursor;
p->cursor = sector;
i_paranoia_firstlast(p);
/*
* Evil hack to fix pregap patch for NEC drives! To be rooted out in a10
*/
p->current_firstsector = sector;
return (ret);
}
/*
* returns last block read, -1 on error
*/
c_block *i_read_c_block(cdrom_paranoia *p, long beginword, long endword,
void (*callback)(long, int))
{
/*
* why do it this way? We need to read lots of sectors to kludge
* around stupid read ahead buffers on cheap drives, as well as avoid
* expensive back-seeking. We also want to 'jiggle' the start address
* to try to break borderline drives more noticeably (and make broken
* drives with unaddressable sectors behave more often).
*/
long readat;
long firstread;
long totaltoread = p->readahead;
long sectatonce = p->nsectors;
long driftcomp = (float) p->dyndrift / CD_FRAMEWORDS + .5;
c_block *new = NULL;
root_block *root = &p->root;
Int16_t *buffer = NULL;
void *bufbase = NULL;
Uchar *flags = NULL;
long sofar;
long dynoverlap = (p->dynoverlap + CD_FRAMEWORDS - 1) / CD_FRAMEWORDS;
long anyflag = 0;
int reduce = 0;
static int pagesize = -1;
#define valign(x, a) (((char *)(x)) + ((a) - 1 - ((((UIntptr_t)(x))-1)%(a))))
/*
* What is the first sector to read? want some pre-buffer if we're not
* at the extreme beginning of the disc
*/
if (p->enable & (PARANOIA_MODE_VERIFY | PARANOIA_MODE_OVERLAP)) {
/*
* we want to jitter the read alignment boundary
*/
long target;
if (rv(root) == NULL || rb(root) > beginword)
target = p->cursor - dynoverlap;
else
target = re(root) / (CD_FRAMEWORDS) - dynoverlap;
if (target + MIN_SECTOR_BACKUP > p->lastread && target <= p->lastread)
target = p->lastread - MIN_SECTOR_BACKUP;
/*
* we want to jitter the read alignment boundary, as some
* drives, beginning from a specific point, will tend to
* lose bytes between sectors in the same place. Also, as
* our vectors are being made up of multiple reads, we want
* the overlap boundaries to move....
*/
readat = (target & (~((long) JIGGLE_MODULO - 1))) + p->jitter;
if (readat > target)
readat -= JIGGLE_MODULO;
p->jitter++;
if (p->jitter >= JIGGLE_MODULO)
p->jitter = 0;
} else {
readat = p->cursor;
}
readat += driftcomp;
if (p->enable & (PARANOIA_MODE_OVERLAP | PARANOIA_MODE_VERIFY)) {
flags = _pcalloc(totaltoread * CD_FRAMEWORDS, 1);
new = new_c_block(p);
recover_cache(p);
} else {
/*
* in the case of root it's just the buffer
*/
paranoia_resetall(p);
new = new_c_block(p);
}
/*
* Do not use valloc() as valloc() in glibc is buggy and returns memory
* that cannot be passed to free().
*/
if (pagesize < 0) {
pagesize = getpagesize();
if (pagesize < 0)
pagesize = 4096; /* Just a guess */
}
reduce = pagesize / CD_FRAMESIZE_RAW;
bufbase = _pmalloc(totaltoread * CD_FRAMESIZE_RAW + pagesize);
buffer = (Int16_t *)valign(bufbase, pagesize);
sofar = 0;
firstread = -1;
/*
* actual read loop
*/
while (sofar < totaltoread) {
long secread = sectatonce;
long adjread = readat;
long thisread;
/*
* don't under/overflow the audio session
*/
if (adjread < p->current_firstsector) {
secread -= p->current_firstsector - adjread;
adjread = p->current_firstsector;
}
if (adjread + secread - 1 > p->current_lastsector)
secread = p->current_lastsector - adjread + 1;
if (sofar + secread > totaltoread)
secread = totaltoread - sofar;
/*
* If we are inside the buffer, the transfers are no longer
* page aligned. Reduce the transfer size to avoid problems.
* Such problems are definitely known to appear on FreeBSD.
*/
if ((sofar > 0) && (secread > (sectatonce - reduce)))
secread = sectatonce - reduce;
if (secread > 0) {
if (firstread < 0)
firstread = adjread;
if ((thisread = cdda_read(p->d, buffer + sofar * CD_FRAMEWORDS, adjread,
secread)) < secread) {
if (thisread < 0)
thisread = 0;
/*
* Uhhh... right. Make something up. But
* don't make us seek backward!
*/
if (callback)
(*callback) ((adjread + thisread) * CD_FRAMEWORDS, PARANOIA_CB_READERR);
memset(buffer + (sofar + thisread) * CD_FRAMEWORDS, 0,
CD_FRAMESIZE_RAW * (secread - thisread));
if (flags)
memset(flags + (sofar + thisread) * CD_FRAMEWORDS, 2,
CD_FRAMEWORDS * (secread - thisread));
}
if (thisread != 0)
anyflag = 1;
if (flags && sofar != 0) {
/*
* Don't verify across overlaps that are too
* close to one another
*/
int i = 0;
for (i = -MIN_WORDS_OVERLAP / 2; i < MIN_WORDS_OVERLAP / 2; i++)
flags[sofar * CD_FRAMEWORDS + i] |= 1;
}
p->lastread = adjread + secread;
if (adjread + secread - 1 == p->current_lastsector)
new->lastsector = -1;
if (callback)
(*callback) ((adjread + secread - 1) * CD_FRAMEWORDS, PARANOIA_CB_READ);
sofar += secread;
readat = adjread + secread;
} else if (readat < p->current_firstsector) {
readat += sectatonce;
/*
* due to being before the
* readable area
*/
} else {
break; /* due to being past the readable area */
}
}
if (anyflag) {
new->vector = _pmalloc(totaltoread * CD_FRAMESIZE_RAW);
memcpy(new->vector, buffer, totaltoread * CD_FRAMESIZE_RAW);
_pfree(bufbase);
new->begin = firstread * CD_FRAMEWORDS - p->dyndrift;
new->size = sofar * CD_FRAMEWORDS;
new->flags = flags;
} else {
if (new)
free_c_block(new);
if (bufbase)
_pfree(bufbase);
if (flags)
_pfree(flags);
new = NULL;
bufbase = NULL;
flags = NULL;
}
return (new);
}
/*
* The returned buffer is *not* to be freed by the caller. It will
* persist only until the next call to paranoia_read() for this p
*/
Int16_t *paranoia_read(cdrom_paranoia *p, void (*callback)(long, int))
{
return (paranoia_read_limited(p, callback, 20));
}
/*
* I added max_retry functionality this way in order to avoid breaking any
* old apps using the nerw libs. cdparanoia 9.8 will need the updated libs,
* but nothing else will require it.
*/
Int16_t *paranoia_read_limited(cdrom_paranoia *p, void (*callback)(long, int),
int max_retries)
{
long beginword = p->cursor * (CD_FRAMEWORDS);
long endword = beginword + CD_FRAMEWORDS;
long retry_count = 0;
long lastend = -2;
root_block *root = &p->root;
if (beginword > p->root.returnedlimit)
p->root.returnedlimit = beginword;
lastend = re(root);
/*
* First, is the sector we want already in the root?
*/
while (rv(root) == NULL ||
rb(root) > beginword ||
(re(root) < endword + p->maxdynoverlap &&
p->enable & (PARANOIA_MODE_VERIFY | PARANOIA_MODE_OVERLAP)) ||
re(root) < endword) {
/*
* Nope; we need to build or extend the root verified range
*/
if (p->enable & (PARANOIA_MODE_VERIFY | PARANOIA_MODE_OVERLAP)) {
i_paranoia_trim(p, beginword, endword);
recover_cache(p);
if (rb(root) != -1 && p->root.lastsector)
i_end_case(p, endword + p->maxdynoverlap,
callback);
else
i_stage2(p, beginword,
endword + p->maxdynoverlap,
callback);
} else
i_end_case(p, endword + p->maxdynoverlap,
callback); /* only trips if we're already */
/* done */
if (!(rb(root) == -1 || rb(root) > beginword ||
re(root) < endword + p->maxdynoverlap))
break;
/*
* Hmm, need more. Read another block
*/
{
c_block *new = i_read_c_block(p, beginword, endword, callback);
if (new) {
if (p->enable & (PARANOIA_MODE_OVERLAP | PARANOIA_MODE_VERIFY)) {
if (p->enable & PARANOIA_MODE_VERIFY)
i_stage1(p, new, callback);
else {
/*
* just make v_fragments from the
* boundary information.
*/
long begin = 0,
end = 0;
while (begin < cs(new)) {
while (end < cs(new) && (new->flags[begin] & 1))
begin++;
end = begin + 1;
while (end < cs(new) && (new->flags[end] & 1) == 0)
end++;
{
new_v_fragment(p, new, begin + cb(new),
end + cb(new),
(new->lastsector && cb(new) + end == ce(new)));
}
begin = end;
}
}
} else {
if (p->root.vector)
i_cblock_destructor(p->root.vector);
free_elem(new->e, 0);
p->root.vector = new;
i_end_case(p, endword + p->maxdynoverlap,
callback);
}
}
}
/*
* Are we doing lots of retries? **********************
*
* Check unaddressable sectors first. There's no backoff
* here; jiggle and minimum backseek handle that for us
*/
if (rb(root) != -1 && lastend + 588 < re(root)) {
/*
* If we've not grown half a sector
*/
lastend = re(root);
retry_count = 0;
} else {
/*
* increase overlap or bail
*/
retry_count++;
/*
* The better way to do this is to look at how many
* actual matches we're getting and what kind of gap
*/
if (retry_count % 5 == 0) {
if (p->dynoverlap == p->maxdynoverlap ||
retry_count == max_retries) {
verify_skip_case(p, callback);
retry_count = 0;
} else {
if (p->stage1.offpoints != -1) { /* hack */
p->dynoverlap *= 1.5;
if (p->dynoverlap > p->maxdynoverlap)
p->dynoverlap = p->maxdynoverlap;
if (callback)
(*callback) (p->dynoverlap, PARANOIA_CB_OVERLAP);
}
}
}
}
}
p->cursor++;
return (rv(root) + (beginword - rb(root)));
}
/*
* a temporary hack
*/
void paranoia_overlapset(cdrom_paranoia *p, long overlap)
{
p->dynoverlap = overlap * CD_FRAMEWORDS;
p->stage1.offpoints = -1;
}
|