audio-resampler: fix history after buffer resize
authorWim Taymans <wtaymans@redhat.com>
Tue, 26 Jan 2016 21:53:33 +0000 (22:53 +0100)
committerWim Taymans <wtaymans@redhat.com>
Mon, 28 Mar 2016 11:25:51 +0000 (13:25 +0200)
When we resize the temp buffer, move the history in its new place.

gst-libs/gst/audio/audio-resampler.c

index 9bb89f2..d9b75b2 100644 (file)
@@ -910,28 +910,28 @@ get_sample_bufs (GstAudioResampler * resampler, gsize need)
 {
   if (G_LIKELY (resampler->samples_len < need)) {
     guint c, blocks = resampler->blocks;
-    gsize bytes;
-    gint8 *ptr;
+    gsize bytes, to_move = 0;
+    gint8 *ptr, *samples;
 
     GST_LOG ("realloc %d -> %d", (gint) resampler->samples_len, (gint) need);
 
     bytes = GST_ROUND_UP_N (need * resampler->bps * resampler->inc, ALIGN);
 
-    resampler->samples =
-        g_realloc (resampler->samples, blocks * bytes + ALIGN - 1);
-    if (resampler->samples_len == 0) {
-      memset (resampler->samples, 0, blocks * bytes + ALIGN - 1);
-    } else {
-      /* FIXME, move history */
-    }
-    resampler->samples_len = need;
+    samples = g_malloc0 (blocks * bytes + ALIGN - 1);
+    ptr = MEM_ALIGN (samples, ALIGN);
 
-    ptr = MEM_ALIGN (resampler->samples, ALIGN);
+    /* if we had some data, move history */
+    if (resampler->samples_len > 0)
+      to_move = resampler->samples_avail * resampler->bps * resampler->inc;
 
     /* set up new pointers */
     for (c = 0; c < blocks; c++) {
+      memcpy (ptr + (c * bytes), resampler->sbuf[c], to_move);
       resampler->sbuf[c] = ptr + (c * bytes);
     }
+    g_free (resampler->samples);
+    resampler->samples = samples;
+    resampler->samples_len = need;
   }
   return resampler->sbuf;
 }