dvdrip-retry-botched-vobs: Retry using the newly determined block list.
[dvdrip] / dvd-sector-copy.c
index f757e49..ea34461 100644 (file)
@@ -15,49 +15,6 @@ static double tvdiff(const struct timeval *tv_lo,
          (tv_hi->tv_usec - tv_lo->tv_usec)/1.0e6);
 }
 
-static void carefully_write(int fd, const void *buf, size_t sz)
-{
-  const unsigned char *p = buf;
-  ssize_t n;
-
-  if (fd < 0) return;
-  while (sz) {
-    n = write(fd, p, sz);
-    if (n < 0) {
-      if (errno == EINTR) continue;
-      bail_syserr(errno, "failed to write to output file");
-    }
-    if (!n) bail("unexpected short write to output file");
-    p += n; sz -= n;
-  }
-}
-
-static void open_file_on_demand(const char *file, FILE **fp_inout,
-                               const char *what)
-{
-  FILE *fp;
-
-  if (!*fp_inout) {
-    fp = fopen(file, "w");
-    if (!fp)
-      bail_syserr(errno, "failed to open %s file `%s'", what, file);
-    fprintf(fp, "## %s\n\n", what);
-    *fp_inout = fp;
-  }
-}
-
-static void check_write(FILE *fp, const char *what)
-{
-  fflush(fp);
-  if (ferror(fp)) bail_syserr(errno, "error writing %s file", what);
-}
-
-static void carefully_fclose(FILE *fp, const char *what)
-{
-  if (fp && (ferror(fp) || fclose(fp)))
-    bail_syserr(errno, "error writing %s file", what);
-}
-
 #define DEFVEC(vtype, etype)                                           \
        typedef struct { etype *v; size_t n, sz; } vtype
 #define VEC_INIT { 0, 0, 0 }
@@ -313,6 +270,8 @@ static void render_badblock_progress(struct progress_item *item,
   progress_shownotice(render, bg, 7);
 }
 
+static double alpha = 0.1;
+
 static void update_progress(secaddr pos)
 {
   struct timeval now;
@@ -321,8 +280,7 @@ static void update_progress(secaddr pos)
   gettimeofday(&now, 0);
   t = tvdiff(&last_time, &now);
 
-#define ALPHA 0.1
-#define BETA (1 - ALPHA)
+#define BETA (1 - alpha)
 
   if (t) {
     g = wcount ? pow(BETA, t) : 0.0; f = (1 - g)/(1 - BETA);
@@ -332,7 +290,6 @@ static void update_progress(secaddr pos)
     last_time = now; last_pos = pos;
   }
 
-#undef ALPHA
 #undef BETA
 
   throbix++; if (!throbber[throbix]) throbix = 0;
@@ -736,11 +693,11 @@ static ssize_t find_good_sector(secaddr *pos_inout, secaddr end,
       recovered(bad_lo, end); *pos_inout = end;
       return (0);
     }
-    step = step_factor*(bad_hi - bad_lo);
+    step = (step_factor - 1)*(bad_hi - bad_lo);
     if (step < step_min) step = step_min;
     if (step_max && step > step_max) step = step_max;
-    if (step > end - bad_lo) step = end - bad_lo;
-    pos = bad_lo + step - 1;
+    if (step > end - bad_hi) step = end - bad_hi;
+    pos = bad_hi + step - 1;
     want = run_length_wanted(pos, step, end);
     n = recovery_read(&r, pos, want);
 #ifdef DEBUG
@@ -1035,6 +992,8 @@ int main(int argc, char *argv[])
            step_max = parse_int(&p, 1, SECLIMIT - 1, "step maximum");
          else if (SKIP_PREFIX("retry"))
            max_retries = parse_int(&p, 0, INT_MAX, "retries");
+         else if (SKIP_PREFIX("alpha"))
+           alpha = parse_float(&p, 0, 1, "average decay factor");
          else if (SKIP_PREFIX("_badwait"))
            bad_block_delay = parse_float(&p, 0, DBL_MAX, "bad-block delay");
          else if (SKIP_PREFIX("_blkwait"))