jpegdec: Fix regression on markers parsing
authorThiago Santos <thiago.sousa.santos@collabora.co.uk>
Mon, 19 Jul 2010 17:47:32 +0000 (14:47 -0300)
committerThiago Santos <thiago.sousa.santos@collabora.co.uk>
Mon, 19 Jul 2010 18:21:04 +0000 (15:21 -0300)
Fixes a regression introduced when fixing bug #583047 in
commit a391bf52cc3c580c7a0a2316ca52eb66da3b85c1

Skip the data when libjpeg asks it to be skipped on
one of its callbacks.

ext/jpeg/gstjpegdec.c

index 26e1717..0e90819 100644 (file)
@@ -234,11 +234,29 @@ gst_jpeg_dec_init_source (j_decompress_ptr cinfo)
 static void
 gst_jpeg_dec_skip_input_data (j_decompress_ptr cinfo, glong num_bytes)
 {
-  GST_DEBUG_OBJECT (CINFO_GET_JPEGDEC (cinfo), "skip %ld bytes", num_bytes);
+  GstJpegDec *dec = CINFO_GET_JPEGDEC (cinfo);
+
+  GST_DEBUG_OBJECT (dec, "skip %ld bytes", num_bytes);
 
   if (num_bytes > 0 && cinfo->src->bytes_in_buffer >= num_bytes) {
     cinfo->src->next_input_byte += (size_t) num_bytes;
     cinfo->src->bytes_in_buffer -= (size_t) num_bytes;
+  } else if (num_bytes > 0) {
+    gint available;
+
+    num_bytes -= cinfo->src->bytes_in_buffer;
+    cinfo->src->next_input_byte += (size_t) cinfo->src->bytes_in_buffer;
+    cinfo->src->bytes_in_buffer = 0;
+
+    available = gst_adapter_available (dec->adapter);
+    if (available < num_bytes || available < dec->rem_img_len) {
+      GST_WARNING_OBJECT (dec, "Less bytes to skip than available in the "
+          "adapter or the remaining image length %ld < %d or %u",
+          num_bytes, available, dec->rem_img_len);
+    }
+    num_bytes = MIN (MIN (num_bytes, available), dec->rem_img_len);
+    gst_adapter_flush (dec->adapter, num_bytes);
+    dec->rem_img_len -= num_bytes;
   }
 }