Index: src/io/source_avcodec.c
===================================================================
--- src/io/source_avcodec.c	(revision 7760b40739022f53c2867ceff27579f974fc1568)
+++ src/io/source_avcodec.c	(revision af272654dfc3b6b37ba4a21ee354074ec7721901)
@@ -59,4 +59,5 @@
   uint_t read_index;
   sint_t selected_stream;
+  uint_t eof;
 };
 
@@ -76,4 +77,7 @@
   // register all formats and codecs
   av_register_all();
+
+  // if path[0] != '/'
+  //avformat_network_init();
 
   // try opening the file and get some info about it
@@ -125,5 +129,5 @@
     goto beach;
   }
-  AUBIO_DBG("Taking stream %d in file %s\n", selected_stream, s->path);
+  //AUBIO_DBG("Taking stream %d in file %s\n", selected_stream, s->path);
   s->selected_stream = selected_stream;
 
@@ -198,4 +202,6 @@
   s->avr = avr;
 
+  s->eof = 0;
+
   //av_log_set_level(AV_LOG_QUIET);
 
@@ -222,4 +228,9 @@
     int err = av_read_frame (avFormatCtx, &avPacket);
     if (err != 0) {
+      if (err == AVERROR_EOF) {
+        s->eof = 1;
+        *read_samples = 0;
+        return;
+      }
       uint8_t errorstr_len = 128;
       char errorstr[errorstr_len];
@@ -238,9 +249,13 @@
 
   if (len < 0) {
+    av_free_packet(&avPacket);
     AUBIO_ERR("Error while decoding %s\n", s->path);
     return;
   }
   if (got_frame == 0) {
-    AUBIO_ERR("Could not get frame for (%s)\n", s->path);
+    av_free_packet(&avPacket);
+    //AUBIO_ERR("Could not get frame for (%s)\n", s->path);
+    *read_samples = 0;
+    return;
   } /* else {
     int data_size =
@@ -267,5 +282,5 @@
   //    max_out_samples, AUBIO_AVCODEC_MIN_BUFFER_SIZE);
 
-  //AUBIO_WRN("Converted %d to %d samples\n", in_samples, out_samples);
+  //AUBIO_WRN("aubio_source_avcodec_readframe converted %d to %d samples\n", in_samples, out_samples);
   //for (i = 0; i < out_samples; i ++) {
   //  AUBIO_DBG("%f\n", SHORT_TO_FLOAT(output[i]));
@@ -284,40 +299,30 @@
 void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_data, uint_t * read){
   uint_t i;
-  //AUBIO_DBG("entering 'do' with %d, %d\n", s->read_samples, s->read_index);
-  if (s->read_samples < s->hop_size) {
-    // write the end of the buffer to the beginning of read_data
-    uint_t partial = s->read_samples;
-    for (i = 0; i < partial; i++) {
-      read_data->data[i] = SHORT_TO_FLOAT(s->output[i + s->read_index]);
-    }
-    // get more data
-    uint_t avcodec_read = 0;
-    aubio_source_avcodec_readframe(s, &avcodec_read);
-    s->read_samples = avcodec_read;
-    s->read_index = 0;
-    // write the beginning of the buffer to the end of read_data
-    uint_t end = MIN(s->hop_size, s->read_samples);
-    if (avcodec_read == 0) {
-      end = partial;
-    }
-    for (i = partial; i < end; i++) {
-      read_data->data[i] = SHORT_TO_FLOAT(s->output[i - partial + s->read_index]);
-    }
-    if (end < s->hop_size) {
-      for (i = end; i < s->hop_size; i++) {
-        read_data->data[i] = 0.;
+  uint_t end = 0;
+  uint_t total_wrote = 0;
+  while (total_wrote < s->hop_size) {
+    end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote);
+    for (i = 0; i < end; i++) {
+      read_data->data[i + total_wrote] = SHORT_TO_FLOAT(s->output[i + s->read_index]);
+    }
+    total_wrote += end;
+    if (total_wrote < s->hop_size) {
+      uint_t avcodec_read = 0;
+      aubio_source_avcodec_readframe(s, &avcodec_read);
+      s->read_samples = avcodec_read;
+      s->read_index = 0;
+      if (s->eof) {
+        break;
       }
-    }
-    s->read_index += end - partial;
-    s->read_samples -= end - partial;
-    *read = end;
-  } else {
-    for (i = 0; i < s->hop_size; i++) {
-      read_data->data[i] = SHORT_TO_FLOAT(s->output[i + s->read_index]);
-    }
-    s->read_index += s->hop_size;
-    s->read_samples -= s->hop_size;
-    *read = s->hop_size;
-  }
+    } else {
+      s->read_index += end;
+    }
+  }
+  if (total_wrote < s->hop_size) {
+    for (i = end; i < s->hop_size; i++) {
+      read_data->data[i] = 0.;
+    }
+  }
+  *read = total_wrote;
 }
 
