2 * This file has been modified for the cdrkit suite.
4 * The behaviour and appearence of the program code below can differ to a major
5 * extent from the version distributed by the original author(s).
7 * For details, see Changelog file distributed with the cdrkit package. If you
8 * received this file from another source then ask the distributing person for
9 * a log of modifications.
13 /* @(#)paranoia.c 1.33 04/08/17 J. Schilling from cdparanoia-III-alpha9.8 */
15 * Modifications to make the code portable Copyright (c) 2002 J. Schilling
18 * CopyPolicy: GNU Public License 2 applies
19 * Copyright (C) by Monty (xiphmont@mit.edu)
21 * Toplevel file for the paranoia abstraction over the cdda lib
25 /* immediate todo:: */
26 /* Allow disabling of root fixups? */
29 * Dupe bytes are creeping into cases that require greater overlap
30 * than a single fragment can provide. We need to check against a
31 * larger area* (+/-32 sectors of root?) to better eliminate
32 * dupes. Of course this leads to other problems... Is it actually a
33 * practically solvable problem?
35 /* Bimodal overlap distributions break us. */
36 /* scratch detection/tolerance not implemented yet */
39 * Da new shtick: verification now a two-step assymetric process.
41 * A single 'verified/reconstructed' data segment cache, and then the
42 * multiple fragment cache
44 * verify a newly read block against previous blocks; do it only this
45 * once. We maintain a list of 'verified sections' from these matches.
47 * We then glom these verified areas into a new data buffer.
48 * Defragmentation fixups are allowed here alone.
50 * We also now track where read boundaries actually happened; do not
51 * verify across matching boundaries.
55 * Silence. "It's BAAAAAAaaack."
57 * audio is now treated as great continents of values floating on a
58 * mantle of molten silence. Silence is not handled by basic
59 * verification at all; we simply anchor sections of nonzero audio to a
60 * position and fill in everything else as silence. We also note the
61 * audio that interfaces with silence; an edge must be 'wet'.
73 #include "cdda_paranoia.h"
80 * used by: i_iterate_stage2() / i_stage2_each()
82 typedef struct sync_result {
88 static inline long re(root_block *root);
89 static inline long rb(root_block *root);
90 static inline long rs(root_block *root);
91 static inline Int16_t *rv(root_block *root);
92 static inline long i_paranoia_overlap(Int16_t *buffA, Int16_t *buffB,
93 long offsetA, long offsetB,
94 long sizeA, long sizeB,
95 long *ret_begin, long *ret_end);
96 static inline long i_paranoia_overlap2(Int16_t *buffA, Int16_t *buffB,
97 Uchar *flagsA, Uchar *flagsB,
98 long offsetA, long offsetB,
99 long sizeA, long sizeB,
100 long *ret_begin, long *ret_end);
101 static inline long do_const_sync(c_block *A,
102 sort_info *B, Uchar *flagB,
103 long posA, long posB,
104 long *begin, long *end, long *offset);
105 static inline long try_sort_sync(cdrom_paranoia *p,
106 sort_info *A, Uchar *Aflags,
108 long post, long *begin, long *end,
109 long *offset, void (*callback) (long, int));
110 static inline void stage1_matched(c_block *old, c_block *new,
111 long matchbegin, long matchend,
113 void (*callback) (long, int));
114 static long i_iterate_stage1(cdrom_paranoia *p, c_block *old, c_block *new,
115 void (*callback) (long, int));
116 static long i_stage1(cdrom_paranoia *p, c_block *new,
117 void (*callback) (long, int));
118 static long i_iterate_stage2(cdrom_paranoia *p, v_fragment *v, sync_result *r,
119 void (*callback)(long, int));
120 static void i_silence_test(root_block *root);
121 static long i_silence_match(root_block *root, v_fragment *v,
122 void (*callback) (long, int));
123 static long i_stage2_each(root_block *root, v_fragment *v,
124 void (*callback) (long, int));
125 static int i_init_root(root_block *root, v_fragment *v,
126 long begin, void (*callback)(long, int));
127 static int vsort(const void *a, const void *b);
128 static int i_stage2(cdrom_paranoia *p, long beginword, long endword,
129 void (*callback)(long, int));
130 static void i_end_case(cdrom_paranoia *p, long endword,
131 void (*callback)(long, int));
132 static void verify_skip_case(cdrom_paranoia *p,
133 void (*callback)(long, int));
134 void paranoia_free(cdrom_paranoia *p);
135 void paranoia_modeset(cdrom_paranoia *p, int enable);
136 long paranoia_seek(cdrom_paranoia *p, long seek, int mode);
137 c_block *i_read_c_block(cdrom_paranoia *p, long beginword, long endword,
138 void (*callback)(long,int));
139 Int16_t *paranoia_read(cdrom_paranoia *p, void (*callback)(long, int));
140 Int16_t *paranoia_read_limited(cdrom_paranoia *p, void (*callback)(long, int),
142 void paranoia_overlapset(cdrom_paranoia *p, long overlap);
145 static inline long re(root_block *root)
151 return (ce(root->vector));
154 static inline long rb(root_block *root)
160 return (cb(root->vector));
163 static inline long rs(root_block *root)
169 return (cs(root->vector));
172 static inline Int16_t *rv(root_block *root)
178 return (cv(root->vector));
181 #define rc(r) ((r)->vector)
184 * matching and analysis code
187 i_paranoia_overlap(Int16_t *buffA, Int16_t *buffB, long offsetA, long offsetB,
188 long sizeA, long sizeB, long *ret_begin, long *ret_end)
190 long beginA = offsetA;
192 long beginB = offsetB;
195 for (; beginA >= 0 && beginB >= 0; beginA--, beginB--)
196 if (buffA[beginA] != buffB[beginB])
201 for (; endA < sizeA && endB < sizeB; endA++, endB++)
202 if (buffA[endA] != buffB[endB])
209 return (endA - beginA);
213 i_paranoia_overlap2(Int16_t *buffA, Int16_t *buffB, Uchar *flagsA,
214 Uchar *flagsB, long offsetA, long offsetB, long sizeA,
215 long sizeB, long *ret_begin, long *ret_end)
217 long beginA = offsetA;
219 long beginB = offsetB;
222 for (; beginA >= 0 && beginB >= 0; beginA--, beginB--) {
223 if (buffA[beginA] != buffB[beginB])
226 * don't allow matching across matching sector boundaries
228 if ((flagsA[beginA] & flagsB[beginB] & 1)) {
234 * don't allow matching through known missing data
236 if ((flagsA[beginA] & 2) || (flagsB[beginB] & 2))
242 for (; endA < sizeA && endB < sizeB; endA++, endB++) {
243 if (buffA[endA] != buffB[endB])
246 * don't allow matching across matching sector boundaries
248 if ((flagsA[endA] & flagsB[endB] & 1) && endA != beginA) {
252 * don't allow matching through known missing data
254 if ((flagsA[endA] & 2) || (flagsB[endB] & 2))
262 return (endA - beginA);
265 /* Top level of the first stage matcher */
268 * We match each analysis point of new to the preexisting blocks
269 * recursively. We can also optionally maintain a list of fragments of
270 * the preexisting block that didn't match anything, and match them back
273 #define OVERLAP_ADJ (MIN_WORDS_OVERLAP/2-1)
276 do_const_sync(c_block *A, sort_info *B, Uchar *flagB, long posA, long posB,
277 long *begin, long *end, long *offset)
279 Uchar *flagA = A->flags;
283 ret = i_paranoia_overlap(cv(A), iv(B), posA, posB,
284 cs(A), is(B), begin, end);
285 else if ((flagB[posB] & 2) == 0)
286 ret = i_paranoia_overlap2(cv(A), iv(B), flagA, flagB, posA, posB, cs(A),
289 if (ret > MIN_WORDS_SEARCH) {
290 *offset = (posA + cb(A)) - (posB + ib(B));
299 * post is w.r.t. B. in stage one, we post from old. In stage 2 we
300 * post from root. Begin, end, offset count from B's frame of
304 try_sort_sync(cdrom_paranoia *p, sort_info *A, Uchar *Aflags, c_block *B,
305 long post, long *begin, long *end, long *offset,
306 void (*callback)(long, int))
309 long dynoverlap = p->dynoverlap;
310 sort_link *ptr = NULL;
311 Uchar *Bflags = B->flags;
314 * block flag matches 0x02 (unmatchable)
316 if (Bflags == NULL || (Bflags[post - cb(B)] & 2) == 0) {
318 * always try absolute offset zero first!
321 long zeropos = post - ib(A);
323 if (zeropos >= 0 && zeropos < is(A)) {
324 if (cv(B)[post - cb(B)] == iv(A)[zeropos]) {
325 if (do_const_sync(B, A, Aflags,
326 post - cb(B), zeropos,
327 begin, end, offset)) {
329 offset_add_value(p, &(p->stage1), *offset, callback);
339 ptr = sort_getmatch(A, post - ib(A), dynoverlap, cv(B)[post - cb(B)]);
343 if (do_const_sync(B, A, Aflags,
344 post - cb(B), ipos(A, ptr),
345 begin, end, offset)) {
346 offset_add_value(p, &(p->stage1), *offset, callback);
349 ptr = sort_nextmatch(A, ptr);
359 stage1_matched(c_block *old, c_block *new, long matchbegin, long matchend,
360 long matchoffset, void (*callback)(long, int))
363 long oldadjbegin = matchbegin - cb(old);
364 long oldadjend = matchend - cb(old);
365 long newadjbegin = matchbegin - matchoffset - cb(new);
366 long newadjend = matchend - matchoffset - cb(new);
368 if (matchbegin - matchoffset <= cb(new) ||
369 matchbegin <= cb(old) ||
370 (new->flags[newadjbegin] & 1) ||
371 (old->flags[oldadjbegin] & 1)) {
374 (*callback) (matchbegin, PARANOIA_CB_FIXUP_EDGE);
376 (*callback) (matchbegin, PARANOIA_CB_FIXUP_ATOM);
378 if (matchend - matchoffset >= ce(new) ||
379 (new->flags[newadjend] & 1) ||
380 matchend >= ce(old) ||
381 (old->flags[oldadjend] & 1)) {
384 (*callback) (matchend, PARANOIA_CB_FIXUP_EDGE);
386 (*callback) (matchend, PARANOIA_CB_FIXUP_ATOM);
389 * Mark the verification flags. Don't mark the first or last OVERLAP/2
390 * elements so that overlapping fragments have to overlap by OVERLAP to
391 * actually merge. We also remove elements from the sort such that
392 * later sorts do not have to sift through already matched data
394 newadjbegin += OVERLAP_ADJ;
395 newadjend -= OVERLAP_ADJ;
396 for (i = newadjbegin; i < newadjend; i++)
397 new->flags[i] |= 4; /* mark verified */
399 oldadjbegin += OVERLAP_ADJ;
400 oldadjend -= OVERLAP_ADJ;
401 for (i = oldadjbegin; i < oldadjend; i++)
402 old->flags[i] |= 4; /* mark verified */
406 #define CB_NULL (void (*)(long, int))0
409 i_iterate_stage1(cdrom_paranoia *p, c_block *old, c_block *new,
410 void (*callback)(long, int))
413 long matchbegin = -1;
418 * we no longer try to spread the stage one search area by dynoverlap
420 long searchend = min(ce(old), ce(new));
421 long searchbegin = max(cb(old), cb(new));
422 long searchsize = searchend - searchbegin;
423 sort_info *i = p->sortcache;
434 * match return values are in terms of the new vector, not old
436 for (j = searchbegin; j < searchend; j += 23) {
437 if ((new->flags[j - cb(new)] & 6) == 0) {
439 if (try_sort_sync(p, i, new->flags, old, j, &matchbegin, &matchend, &matchoffset,
442 matched += matchend - matchbegin;
445 * purely cosmetic: if we're matching zeros,
446 * don't use the callback because they will
447 * appear to be all skewed
450 long jj = matchbegin - cb(old);
451 long end = matchend - cb(old);
453 for (; jj < end; jj++)
454 if (cv(old)[jj] != 0)
457 stage1_matched(old, new, matchbegin, matchend, matchoffset, callback);
459 stage1_matched(old, new, matchbegin, matchend, matchoffset, CB_NULL);
463 if (matchend - 1 > j)
469 fprintf(stderr, "iterate_stage1: search area=%ld[%ld-%ld] tried=%ld matched=%ld spans=%ld\n",
470 searchsize, searchbegin, searchend, tried, matched, ret);
477 i_stage1(cdrom_paranoia *p, c_block *new, void (*callback)(long, int))
481 c_block *ptr = c_last(p);
487 sort_setup(p->sortcache, cv(new), &cb(new), cs(new),
490 while (ptr && ptr != new) {
493 (*callback) (cb(new), PARANOIA_CB_VERIFY);
494 i_iterate_stage1(p, ptr, new, callback);
500 * parse the verified areas of new into v_fragments
503 while (begin < size) {
504 for (; begin < size; begin++)
505 if (new->flags[begin] & 4)
507 for (end = begin; end < size; end++)
508 if ((new->flags[end] & 4) == 0)
515 new_v_fragment(p, new, cb(new) + max(0, begin - OVERLAP_ADJ),
516 cb(new) + min(size, end + OVERLAP_ADJ),
517 (end + OVERLAP_ADJ >= size && new->lastsector));
526 * reconcile v_fragments to root buffer. Free if matched, fragment/fixup root
529 * do *not* match using zero posts
532 i_iterate_stage2(cdrom_paranoia *p, v_fragment *v, sync_result *r,
533 void (*callback)(long, int))
535 root_block *root = &(p->root);
536 long matchbegin = -1;
543 fprintf(stderr, "Stage 2 search: fbv=%ld fev=%ld\n", fb(v), fe(v));
546 if (min(fe(v) + p->dynoverlap, re(root)) -
547 max(fb(v) - p->dynoverlap, rb(root)) <= 0)
551 (*callback) (fb(v), PARANOIA_CB_VERIFY);
554 * just a bit of v; determine the correct area
556 fbv = max(fb(v), rb(root) - p->dynoverlap);
559 * we want to avoid zeroes
561 while (fbv < fe(v) && fv(v)[fbv - fb(v)] == 0)
565 fev = min(min(fbv + 256, re(root) + p->dynoverlap), fe(v));
569 * spread the search area a bit. We post from root, so
570 * containment must strictly adhere to root
572 long searchend = min(fev + p->dynoverlap, re(root));
573 long searchbegin = max(fbv - p->dynoverlap, rb(root));
574 sort_info *i = p->sortcache;
577 sort_setup(i, fv(v), &fb(v), fs(v), fbv, fev);
578 for (j = searchbegin; j < searchend; j += 23) {
579 while (j < searchend && rv(root)[j - rb(root)] == 0)
584 if (try_sort_sync(p, i, (Uchar *)0, rc(root), j,
585 &matchbegin, &matchend, &offset, callback)) {
587 r->begin = matchbegin;
592 (*callback) (r->begin, PARANOIA_CB_FIXUP_EDGE);
601 * simple test for a root vector that ends in silence
603 static void i_silence_test(root_block *root)
605 Int16_t *vec = rv(root);
606 long end = re(root) - rb(root) - 1;
609 for (j = end - 1; j >= 0; j--)
612 if (j < 0 || end - j > MIN_SILENCE_BOUNDARY) {
615 root->silenceflag = 1;
616 root->silencebegin = rb(root) + j;
617 if (root->silencebegin < root->returnedlimit)
618 root->silencebegin = root->returnedlimit;
623 * match into silence vectors at offset zero if at all possible. This
624 * also must be called with vectors in ascending begin order in case
625 * there are nonzero islands
628 i_silence_match(root_block *root, v_fragment *v, void (*callback)(long, int))
631 cdrom_paranoia *p = v->p;
632 Int16_t *vec = fv(v);
638 * does this vector begin wet?
640 if (end < MIN_SILENCE_BOUNDARY)
642 for (j = 0; j < end; j++)
645 if (j < MIN_SILENCE_BOUNDARY)
650 * is the new silent section ahead of the end of the old
651 * by < p->dynoverlap?
653 if (fb(v) >= re(root) && fb(v) - p->dynoverlap < re(root)) {
655 * extend the zeroed area of root
656 * XXX dynarrays are not needed here.
658 long addto = fb(v) + MIN_SILENCE_BOUNDARY - re(root);
659 /* Int16_t avec[addto];*/
661 Int16_t *avec = alloca(addto * sizeof (Int16_t));
663 Int16_t *avec = _pmalloc(addto * sizeof (Int16_t));
666 memset(avec, 0, sizeof (avec));
667 c_append(rc(root), avec, addto);
673 * do we have an 'effortless' overlap?
675 begin = max(fb(v), root->silencebegin);
676 end = min(j, re(root));
680 * don't use it unless it will extend...
682 if (fe(v) > re(root)) {
683 long voff = begin - fb(v);
685 c_remove(rc(root), begin - rb(root), -1);
686 c_append(rc(root), vec + voff, fs(v) - voff);
688 offset_add_value(p, &p->stage2, 0, callback);
693 * OK, we'll have to force it a bit as the root is
696 long voff = j - fb(v);
699 * don't use it unless it will extend...
701 if (begin + fs(v) - voff > re(root)) {
702 c_remove(rc(root), root->silencebegin - rb(root), -1);
703 c_append(rc(root), vec + voff, fs(v) - voff);
705 offset_add_value(p, &p->stage2, end - begin, callback);
711 * test the new root vector for ending in silence
713 root->silenceflag = 0;
714 i_silence_test(root);
717 root->lastsector = 1;
723 i_stage2_each(root_block *root, v_fragment *v, void (*callback)(long, int))
726 cdrom_paranoia *p = v->p;
727 long dynoverlap = p->dynoverlap / 2 * 2;
737 if (i_iterate_stage2(p, v, &r, callback)) {
739 long begin = r.begin - rb(root);
740 long end = r.end - rb(root);
741 long offset = r.begin + r.offset - fb(v) - begin;
746 * we have a match! We don't rematch off rift, we chase
747 * the match all the way to both extremes doing rift
751 fprintf(stderr, "Stage 2 match\n");
756 * note that we don't extend back right now, only
759 while ((begin + offset > 0 && begin > 0)) {
763 long beginL = begin + offset;
766 Int16_t *buff = _pmalloc(fs(v) * sizeof (Int16_t));
768 l = c_alloc(buff, fb(v), fs(v));
769 memcpy(buff, fv(v), fs(v) * sizeof (Int16_t));
771 i_analyze_rift_r(rv(root), cv(l),
773 begin - 1, beginL - 1,
774 &matchA, &matchB, &matchC);
777 fprintf(stderr, "matching rootR: matchA:%ld matchB:%ld matchC:%ld\n",
778 matchA, matchB, matchC);
783 * a problem with root
787 * dropped bytes; add back from v
790 (*callback) (begin + rb(root) - 1, PARANOIA_CB_FIXUP_DROPPED);
791 if (rb(root) + begin < p->root.returnedlimit)
794 c_insert(rc(root), begin, cv(l) + beginL - matchA,
802 * duplicate bytes; drop from root
805 (*callback) (begin + rb(root) - 1, PARANOIA_CB_FIXUP_DUPED);
806 if (rb(root) + begin + matchA < p->root.returnedlimit)
809 c_remove(rc(root), begin + matchA, -matchA);
817 * a problem with the fragment
824 (*callback) (begin + rb(root) - 1, PARANOIA_CB_FIXUP_DROPPED);
825 c_insert(l, beginL, rv(root) + begin - matchB,
833 (*callback) (begin + rb(root) - 1, PARANOIA_CB_FIXUP_DUPED);
834 c_remove(l, beginL + matchB, -matchB);
839 * Uhh... problem with both
840 * Set 'disagree' flags in root
842 if (rb(root) + begin - matchC < p->root.returnedlimit)
844 c_overwrite(rc(root), begin - matchC,
845 cv(l) + beginL - matchC, matchC);
849 * do we have a mismatch due to silence
850 * beginning/end case?
851 * in the 'chase back' case, we don't
853 * Did not determine nature of
854 * difficulty... report and bail
856 /* RRR(*callback)(post,PARANOIA_CB_XXX); */
860 * not the most efficient way, but it will do
863 beginL = begin + offset;
864 i_paranoia_overlap(rv(root), cv(l),
873 temp = l ? cs(l) : fs(v);
874 while (end + offset < temp && end < rs(root)) {
878 long beginL = begin + offset;
879 long endL = end + offset;
882 Int16_t *buff = _pmalloc(fs(v) * sizeof (Int16_t));
884 l = c_alloc(buff, fb(v), fs(v));
885 memcpy(buff, fv(v), fs(v) * sizeof (Int16_t));
887 i_analyze_rift_f(rv(root), cv(l),
890 &matchA, &matchB, &matchC);
893 fprintf(stderr, "matching rootF: matchA:%ld matchB:%ld matchC:%ld\n",
894 matchA, matchB, matchC);
899 * a problem with root
903 * dropped bytes; add back from v
906 (*callback) (end + rb(root), PARANOIA_CB_FIXUP_DROPPED);
907 if (end + rb(root) < p->root.returnedlimit)
909 c_insert(rc(root), end, cv(l) + endL, matchA);
912 * duplicate bytes; drop from root
915 (*callback) (end + rb(root), PARANOIA_CB_FIXUP_DUPED);
916 if (end + rb(root) < p->root.returnedlimit)
918 c_remove(rc(root), end, -matchA);
922 * a problem with the fragment
929 (*callback) (end + rb(root), PARANOIA_CB_FIXUP_DROPPED);
930 c_insert(l, endL, rv(root) + end, matchB);
936 (*callback) (end + rb(root), PARANOIA_CB_FIXUP_DUPED);
937 c_remove(l, endL, -matchB);
941 * Uhh... problem with both
942 * Set 'disagree' flags in root
944 if (end + rb(root) < p->root.returnedlimit)
946 c_overwrite(rc(root), end, cv(l) + endL, matchC);
948 analyze_rift_silence_f(rv(root), cv(l),
955 * Can only do this if we haven't
956 * already returned data
958 if (end + rb(root) >= p->root.returnedlimit) {
959 c_remove(rc(root), end, -1);
963 * silence in fragment; lose it
966 i_cblock_destructor(l);
972 * Could not determine nature of
973 * difficulty... report and bail
975 /* RRR(*callback)(post,PARANOIA_CB_XXX); */
980 * not the most efficient way, but it will do for now
982 i_paranoia_overlap(rv(root), cv(l),
989 * if this extends our range, let's glom
992 long sizeA = rs(root);
1007 if (sizeB - offset > sizeA || v->lastsector) {
1008 if (v->lastsector) {
1009 root->lastsector = 1;
1012 c_remove(rc(root), end, -1);
1014 if (sizeB - offset - end)
1015 c_append(rc(root), vector + end + offset,
1016 sizeB - offset - end);
1018 i_silence_test(root);
1021 * add offset into dynoverlap stats
1023 offset_add_value(p, &p->stage2, offset + vecbegin - rb(root), callback);
1027 i_cblock_destructor(l);
1033 * D'oh. No match. What to do with the fragment?
1035 if (fe(v) + dynoverlap < re(root) && !root->silenceflag) {
1037 * It *should* have matched. No good; free it.
1042 * otherwise, we likely want this for an upcoming match
1043 * we don't free the sort info (if it was collected)
1052 i_init_root(root_block *root, v_fragment *v, long begin,
1053 void (*callback)(long, int))
1055 if (fb(v) <= begin && fe(v) > begin) {
1057 root->lastsector = v->lastsector;
1058 root->returnedlimit = begin;
1061 i_cblock_destructor(rc(root));
1065 Int16_t *buff = _pmalloc(fs(v) * sizeof (Int16_t));
1067 memcpy(buff, fv(v), fs(v) * sizeof (Int16_t));
1068 root->vector = c_alloc(buff, fb(v), fs(v));
1070 i_silence_test(root);
1077 static int vsort(const void *a, const void *b)
1079 return ((*(v_fragment **) a)->begin - (*(v_fragment **) b)->begin);
1083 i_stage2(cdrom_paranoia *p, long beginword, long endword,
1084 void (*callback)(long, int))
1089 root_block *root = &(p->root);
1092 fprintf(stderr, "Fragments:%ld\n", p->fragments->active);
1097 * even when the 'silence flag' is lit, we try to do non-silence
1098 * matching in the event that there are still audio vectors with
1099 * content to be sunk before the silence
1103 * loop through all the current fragments
1105 v_fragment *first = v_first(p);
1106 long active = p->fragments->active,
1108 #ifdef HAVE_DYN_ARRAYS
1109 v_fragment *list[active];
1111 v_fragment **list = _pmalloc(active * sizeof (v_fragment *));
1115 v_fragment *next = v_next(first);
1117 list[count++] = first;
1124 * sorted in ascending order of beginning
1126 qsort(list, active, sizeof (v_fragment *), vsort);
1129 * we try a nonzero based match even if in silent mode
1130 * in the case that there are still cached vectors to
1131 * sink behind continent->ocean boundary
1133 for (count = 0; count < active; count++) {
1134 first = list[count];
1136 if (rv(root) == NULL) {
1137 if (i_init_root(&(p->root), first, beginword, callback)) {
1138 free_v_fragment(first);
1143 if (i_stage2_each(root, first, callback)) {
1154 if (!flag && p->root.silenceflag) {
1155 for (count = 0; count < active; count++) {
1156 first = list[count];
1158 if (rv(root) != NULL) {
1159 if (i_silence_match(root, first, callback)) {
1168 #ifndef HAVE_DYN_ARRAYS
1176 i_end_case(cdrom_paranoia *p, long endword, void (*callback)(long, int))
1179 root_block *root = &p->root;
1182 * have an 'end' flag; if we've just read in the last sector in a
1183 * session, set the flag. If we verify to the end of a fragment
1184 * which has the end flag set, we're done (set a done flag).
1185 * Pad zeroes to the end of the read
1187 if (root->lastsector == 0)
1189 if (endword < re(root))
1193 long addto = endword - re(root);
1194 char *temp = _pcalloc(addto, sizeof (char) * 2);
1196 c_append(rc(root), (void *) temp, addto);
1202 paranoia_resetcache(p);
1208 * We want to add a sector. Look through the caches for something that
1209 * spans. Also look at the flags on the c_block... if this is an
1210 * obliterated sector, get a bit of a chunk past the obliteration.
1212 * Not terribly smart right now, actually. We can probably find
1213 * *some* match with a cache block somewhere. Take it and continue it
1217 verify_skip_case(cdrom_paranoia *p, void (*callback)(long, int))
1220 root_block *root = &(p->root);
1221 c_block *graft = NULL;
1227 fprintf(stderr, "\nskipping\n");
1230 if (rv(root) == NULL) {
1239 (*callback) (post, PARANOIA_CB_SKIP);
1241 if (p->enable & PARANOIA_MODE_NEVERSKIP)
1245 * We want to add a sector. Look for a c_block that spans,
1246 * preferrably a verified area
1249 c_block *c = c_first(p);
1252 long cbegin = cb(c);
1255 if (cbegin <= post && cend > post) {
1258 if (c->flags[post - cbegin] & 4) {
1262 while (vend < cend && (c->flags[vend - cbegin] & 4))
1264 if (!vflag || vend > vflag) {
1271 * not a verified area
1274 while (vend < cend && (c->flags[vend - cbegin] & 4) == 0)
1276 if (graft == NULL || gend > vend) {
1278 * smallest unverified area
1290 long cbegin = cb(graft);
1291 long cend = ce(graft);
1293 while (gend < cend && (graft->flags[gend - cbegin] & 4))
1295 gend = min(gend + OVERLAP_ADJ, cend);
1297 if (rv(root) == NULL) {
1298 Int16_t *buff = _pmalloc(cs(graft));
1300 memcpy(buff, cv(graft), cs(graft));
1301 rc(root) = c_alloc(buff, cb(graft), cs(graft));
1303 c_append(rc(root), cv(graft) + post - cbegin,
1307 root->returnedlimit = re(root);
1313 * No? Fine. Great. Write in some zeroes :-P
1316 void *temp = _pcalloc(CD_FRAMESIZE_RAW, sizeof (Int16_t));
1318 if (rv(root) == NULL) {
1319 rc(root) = c_alloc(temp, post, CD_FRAMESIZE_RAW);
1321 c_append(rc(root), temp, CD_FRAMESIZE_RAW);
1324 root->returnedlimit = re(root);
1331 void paranoia_free(cdrom_paranoia *p)
1333 paranoia_resetall(p);
1334 sort_free(p->sortcache);
1338 void paranoia_modeset(cdrom_paranoia *p, int enable)
1343 long paranoia_seek(cdrom_paranoia *p, long seek, int mode)
1353 sector = cdda_disc_lastsector(p->d) + seek;
1356 sector = p->cursor + seek;
1360 if (cdda_sector_gettrack(p->d, sector) == -1)
1363 i_cblock_destructor(p->root.vector);
1364 p->root.vector = NULL;
1365 p->root.lastsector = 0;
1366 p->root.returnedlimit = 0;
1371 i_paranoia_firstlast(p);
1374 * Evil hack to fix pregap patch for NEC drives! To be rooted out in a10
1376 p->current_firstsector = sector;
1382 * returns last block read, -1 on error
1384 c_block *i_read_c_block(cdrom_paranoia *p, long beginword, long endword,
1385 void (*callback)(long, int))
1388 * why do it this way? We need to read lots of sectors to kludge
1389 * around stupid read ahead buffers on cheap drives, as well as avoid
1390 * expensive back-seeking. We also want to 'jiggle' the start address
1391 * to try to break borderline drives more noticeably (and make broken
1392 * drives with unaddressable sectors behave more often).
1396 long totaltoread = p->readahead;
1397 long sectatonce = p->nsectors;
1398 long driftcomp = (float) p->dyndrift / CD_FRAMEWORDS + .5;
1399 c_block *new = NULL;
1400 root_block *root = &p->root;
1401 Int16_t *buffer = NULL;
1402 void *bufbase = NULL;
1403 Uchar *flags = NULL;
1405 long dynoverlap = (p->dynoverlap + CD_FRAMEWORDS - 1) / CD_FRAMEWORDS;
1408 static int pagesize = -1;
1409 #define valign(x, a) (((char *)(x)) + ((a) - 1 - ((((UIntptr_t)(x))-1)%(a))))
1412 * What is the first sector to read? want some pre-buffer if we're not
1413 * at the extreme beginning of the disc
1415 if (p->enable & (PARANOIA_MODE_VERIFY | PARANOIA_MODE_OVERLAP)) {
1418 * we want to jitter the read alignment boundary
1422 if (rv(root) == NULL || rb(root) > beginword)
1423 target = p->cursor - dynoverlap;
1425 target = re(root) / (CD_FRAMEWORDS) - dynoverlap;
1427 if (target + MIN_SECTOR_BACKUP > p->lastread && target <= p->lastread)
1428 target = p->lastread - MIN_SECTOR_BACKUP;
1431 * we want to jitter the read alignment boundary, as some
1432 * drives, beginning from a specific point, will tend to
1433 * lose bytes between sectors in the same place. Also, as
1434 * our vectors are being made up of multiple reads, we want
1435 * the overlap boundaries to move....
1437 readat = (target & (~((long) JIGGLE_MODULO - 1))) + p->jitter;
1438 if (readat > target)
1439 readat -= JIGGLE_MODULO;
1441 if (p->jitter >= JIGGLE_MODULO)
1448 readat += driftcomp;
1450 if (p->enable & (PARANOIA_MODE_OVERLAP | PARANOIA_MODE_VERIFY)) {
1451 flags = _pcalloc(totaltoread * CD_FRAMEWORDS, 1);
1452 new = new_c_block(p);
1456 * in the case of root it's just the buffer
1458 paranoia_resetall(p);
1459 new = new_c_block(p);
1463 * Do not use valloc() as valloc() in glibc is buggy and returns memory
1464 * that cannot be passed to free().
1467 pagesize = getpagesize();
1469 pagesize = 4096; /* Just a guess */
1471 reduce = pagesize / CD_FRAMESIZE_RAW;
1472 bufbase = _pmalloc(totaltoread * CD_FRAMESIZE_RAW + pagesize);
1473 buffer = (Int16_t *)valign(bufbase, pagesize);
1480 while (sofar < totaltoread) {
1481 long secread = sectatonce;
1482 long adjread = readat;
1486 * don't under/overflow the audio session
1488 if (adjread < p->current_firstsector) {
1489 secread -= p->current_firstsector - adjread;
1490 adjread = p->current_firstsector;
1492 if (adjread + secread - 1 > p->current_lastsector)
1493 secread = p->current_lastsector - adjread + 1;
1495 if (sofar + secread > totaltoread)
1496 secread = totaltoread - sofar;
1499 * If we are inside the buffer, the transfers are no longer
1500 * page aligned. Reduce the transfer size to avoid problems.
1501 * Such problems are definitely known to appear on FreeBSD.
1503 if ((sofar > 0) && (secread > (sectatonce - reduce)))
1504 secread = sectatonce - reduce;
1509 firstread = adjread;
1510 if ((thisread = cdda_read(p->d, buffer + sofar * CD_FRAMEWORDS, adjread,
1511 secread)) < secread) {
1517 * Uhhh... right. Make something up. But
1518 * don't make us seek backward!
1521 (*callback) ((adjread + thisread) * CD_FRAMEWORDS, PARANOIA_CB_READERR);
1522 memset(buffer + (sofar + thisread) * CD_FRAMEWORDS, 0,
1523 CD_FRAMESIZE_RAW * (secread - thisread));
1525 memset(flags + (sofar + thisread) * CD_FRAMEWORDS, 2,
1526 CD_FRAMEWORDS * (secread - thisread));
1531 if (flags && sofar != 0) {
1533 * Don't verify across overlaps that are too
1534 * close to one another
1538 for (i = -MIN_WORDS_OVERLAP / 2; i < MIN_WORDS_OVERLAP / 2; i++)
1539 flags[sofar * CD_FRAMEWORDS + i] |= 1;
1541 p->lastread = adjread + secread;
1543 if (adjread + secread - 1 == p->current_lastsector)
1544 new->lastsector = -1;
1547 (*callback) ((adjread + secread - 1) * CD_FRAMEWORDS, PARANOIA_CB_READ);
1550 readat = adjread + secread;
1551 } else if (readat < p->current_firstsector) {
1552 readat += sectatonce;
1554 * due to being before the
1558 break; /* due to being past the readable area */
1563 new->vector = _pmalloc(totaltoread * CD_FRAMESIZE_RAW);
1564 memcpy(new->vector, buffer, totaltoread * CD_FRAMESIZE_RAW);
1567 new->begin = firstread * CD_FRAMEWORDS - p->dyndrift;
1568 new->size = sofar * CD_FRAMEWORDS;
1585 * The returned buffer is *not* to be freed by the caller. It will
1586 * persist only until the next call to paranoia_read() for this p
1588 Int16_t *paranoia_read(cdrom_paranoia *p, void (*callback)(long, int))
1590 return (paranoia_read_limited(p, callback, 20));
1594 * I added max_retry functionality this way in order to avoid breaking any
1595 * old apps using the nerw libs. cdparanoia 9.8 will need the updated libs,
1596 * but nothing else will require it.
1598 Int16_t *paranoia_read_limited(cdrom_paranoia *p, void (*callback)(long, int),
1601 long beginword = p->cursor * (CD_FRAMEWORDS);
1602 long endword = beginword + CD_FRAMEWORDS;
1603 long retry_count = 0;
1605 root_block *root = &p->root;
1607 if (beginword > p->root.returnedlimit)
1608 p->root.returnedlimit = beginword;
1612 * First, is the sector we want already in the root?
1614 while (rv(root) == NULL ||
1615 rb(root) > beginword ||
1616 (re(root) < endword + p->maxdynoverlap &&
1617 p->enable & (PARANOIA_MODE_VERIFY | PARANOIA_MODE_OVERLAP)) ||
1618 re(root) < endword) {
1621 * Nope; we need to build or extend the root verified range
1623 if (p->enable & (PARANOIA_MODE_VERIFY | PARANOIA_MODE_OVERLAP)) {
1624 i_paranoia_trim(p, beginword, endword);
1626 if (rb(root) != -1 && p->root.lastsector)
1627 i_end_case(p, endword + p->maxdynoverlap,
1630 i_stage2(p, beginword,
1631 endword + p->maxdynoverlap,
1634 i_end_case(p, endword + p->maxdynoverlap,
1635 callback); /* only trips if we're already */
1638 if (!(rb(root) == -1 || rb(root) > beginword ||
1639 re(root) < endword + p->maxdynoverlap))
1643 * Hmm, need more. Read another block
1646 c_block *new = i_read_c_block(p, beginword, endword, callback);
1649 if (p->enable & (PARANOIA_MODE_OVERLAP | PARANOIA_MODE_VERIFY)) {
1651 if (p->enable & PARANOIA_MODE_VERIFY)
1652 i_stage1(p, new, callback);
1655 * just make v_fragments from the
1656 * boundary information.
1661 while (begin < cs(new)) {
1662 while (end < cs(new) && (new->flags[begin] & 1))
1665 while (end < cs(new) && (new->flags[end] & 1) == 0)
1668 new_v_fragment(p, new, begin + cb(new),
1670 (new->lastsector && cb(new) + end == ce(new)));
1679 i_cblock_destructor(p->root.vector);
1680 free_elem(new->e, 0);
1681 p->root.vector = new;
1683 i_end_case(p, endword + p->maxdynoverlap,
1691 * Are we doing lots of retries? **********************
1693 * Check unaddressable sectors first. There's no backoff
1694 * here; jiggle and minimum backseek handle that for us
1696 if (rb(root) != -1 && lastend + 588 < re(root)) {
1698 * If we've not grown half a sector
1704 * increase overlap or bail
1709 * The better way to do this is to look at how many
1710 * actual matches we're getting and what kind of gap
1712 if (retry_count % 5 == 0) {
1713 if (p->dynoverlap == p->maxdynoverlap ||
1714 retry_count == max_retries) {
1715 verify_skip_case(p, callback);
1718 if (p->stage1.offpoints != -1) { /* hack */
1719 p->dynoverlap *= 1.5;
1720 if (p->dynoverlap > p->maxdynoverlap)
1721 p->dynoverlap = p->maxdynoverlap;
1723 (*callback) (p->dynoverlap, PARANOIA_CB_OVERLAP);
1731 return (rv(root) + (beginword - rb(root)));
1737 void paranoia_overlapset(cdrom_paranoia *p, long overlap)
1739 p->dynoverlap = overlap * CD_FRAMEWORDS;
1740 p->stage1.offpoints = -1;