title: Error with java video decoding by Xuggler
body: <p>I am learning Xuggler(a library supports video streaming for Java) by following the code in a <a href="http://blog.xuggle.com/2009/01/23/how-to-use-xuggler-to-decode-and-play-video/" rel="nofollow">tutorial</a> teaching <a href="http://www.youtube.com/watch?v=qEs5Hh4l5hY" rel="nofollow">how to decode and play video</a>.</p>
  
  <p>I supposed this snippet of code is reliable, but when I want to play the video read on my window, I got the error telling me </p>
  
  <p><code>Exception in thread "main" java.lang.RuntimeException: got error decoding video in: C:/Users/swnmlab/1.mp4</code></p>
  
  <p>This error happens when this line got executed</p>
  
  <p><code>int bytesDecoded = videoCoder.decodeVideo(picture, packet,offset);</code></p>
  
  <p>I used debugger to step into and find that xuggle-xuggler.jar has no source attachment, does anyone has encountered this problem before?</p>
  
  <pre><code>import java.awt.image.BufferedImage;
  
  import com.xuggle.xuggler.ICodec.Type;
  import com.xuggle.xuggler.IContainer;
  import com.xuggle.xuggler.IPacket;
  import com.xuggle.xuggler.IPixelFormat;
  import com.xuggle.xuggler.IStream;
  import com.xuggle.xuggler.IStreamCoder;
  import com.xuggle.xuggler.IVideoPicture;
  import com.xuggle.xuggler.IVideoResampler;
  import com.xuggle.xuggler.Utils;
  import com.xuggle.xuggler.demos.VideoImage;
  
  public class DecodeAndPlayVideo {
      public static void main(String[] args) {
          String filename = "C:/Users/swnmlab/1.mp4";
  
          // Create a Xuggler container object
          IContainer container = IContainer.make();
  
          // Open up the container
          if (container.open(filename, IContainer.Type.READ, null) &lt; 0)
              throw new IllegalArgumentException("could not open file: "
                      + filename);
  
          // query how many streams the call to open found
          int numStreams = container.getNumStreams();
  
          // and iterate through the streams to find the first video stream
          int videoStreamId = -1;
          IStreamCoder videoCoder = null;
          for (int i = 0; i &lt; numStreams; i++) {
              // Find the stream object
              IStream stream = container.getStream(i);
              // Get the pre-configured decoder that can decode this stream;
              IStreamCoder coder = stream.getStreamCoder();
  
              if (coder.getCodecType() == Type.CODEC_TYPE_VIDEO) {
                  videoStreamId = i;
                  videoCoder = coder;
                  break;
              }
          }
          if (videoStreamId == -1)
              throw new RuntimeException(
                      "could not find video stream in container: " + filename);
  
          if (videoCoder.acquire() &lt; 0)
              throw new RuntimeException(
                      "could not open video decoder for container: " + filename);
  
          IVideoResampler resampler = null;
          if (videoCoder.getPixelType() != IPixelFormat.Type.BGR24) {
              // if this stream is not in BGR24, we're going to need to
              // convert it. The VideoResampler does that for us.
              resampler = IVideoResampler.make(videoCoder.getWidth(),
                      videoCoder.getHeight(), IPixelFormat.Type.BGR24,
                      videoCoder.getWidth(), videoCoder.getHeight(),
                      videoCoder.getPixelType());
              if (resampler == null)
                  throw new RuntimeException("could not create color space "
                          + "resampler for: " + filename);
          }
  
          /*
           * And once we have that, we draw a window on screen
           */
          openJavaWindow();
  
          IPacket packet = IPacket.make();
          while (container.readNextPacket(packet) &gt;= 0) {
              /*
               * Now we have a packet, let's see if it belongs to our video stream
               */
              if (packet.getStreamIndex() == videoStreamId) {
                  IVideoPicture picture = IVideoPicture.make(
                          videoCoder.getPixelType(), videoCoder.getWidth(),
                          videoCoder.getHeight());
  
                  int offset = 0;
                  while (offset &lt; packet.getSize()) {
                      /*
                       * Now, we decode the video, checking for any errors.
                       */
                      int bytesDecoded = videoCoder.decodeVideo(picture, packet,
                              offset);
                      if (bytesDecoded &lt; 0)
                          throw new RuntimeException(
                                  "got error decoding video in: " + filename);
                      offset += bytesDecoded;
  
                      /*
                       * Some decoders will consume data in a packet, but will not
                       * be able to construct a full video picture yet. Therefore
                       * you should always check if you got a complete picture
                       * from the decoder
                       */
                      if (picture.isComplete()) {
                          IVideoPicture newPic = picture;
                          /*
                           * If the resampler is not null, that means we didn't
                           * get the video in BGR24 format and need to convert it
                           * into BGR24 format.
                           */
                          if (resampler != null) {
                              // we must resample
                              newPic = IVideoPicture.make(
                                      resampler.getOutputPixelFormat(),
                                      picture.getWidth(), picture.getHeight());
                              if (resampler.resample(newPic, picture) &lt; 0)
                                  throw new RuntimeException(
                                          "could not resample video from: "
                                                  + filename);
                          }
                          if (newPic.getPixelType() != IPixelFormat.Type.BGR24)
                              throw new RuntimeException("could not decode video"
                                      + " as BGR 24 bit data in: " + filename);
  
  
                          @SuppressWarnings("deprecation")
                          BufferedImage javaImage = Utils.videoPictureToImage(newPic);
  
                          // and display it on the Java Swing window
                          updateJavaWindow(javaImage);    
                      }
  
                  }
              } else {
                  /*
                   * This packet isn't part of our video stream, so we just silently drop it.
                   */
                  do {
                  } while (false);
              }
          }
  
          closeJavaWindow();
  
      }
  
      private static VideoImage mScreen = null;
      private static void updateJavaWindow(BufferedImage javaImage) {
          mScreen.setImage(javaImage);
      }
  
      private static void openJavaWindow() {
          mScreen = new VideoImage();
      }
  
      private static void closeJavaWindow() {
          System.exit(0);
      }
  }
  </code></pre>
  
  <p>P.S. If you want to try this library, you can find the installing file <a href="http://www.huergoguil.es/descargas/xuggle-xuggler.5.1.0-win64-setup.exe" rel="nofollow">here</a>, and then follow the steps on <a href="http://blog.xuggle.com/2009/01/23/how-to-install-xuggler-on-microsoft-windows/" rel="nofollow">this page</a> finish installing this library on Windows.</p>
  
  <hr>
  
  <p>I found the error happened because I cahnged the original code</p>
  
  <pre><code>        if (videoCoder.open() &lt; 0)
              throw new RuntimeException(
                      "could not open video decoder for container: " + filename);
  </code></pre>
  
  <p>to</p>
  
  <pre><code>        if (videoCoder.acquire() &lt; 0)
              throw new RuntimeException(
                      "could not open video decoder for container: " + filename);
  </code></pre>
  
  <p>since open() method causes deprecation warning, so I used auto complete to find a method that looks like open(), then changed to acquire(). I thought that was OK since no "could not open video decoder for container: " exception thrown out.So just follow the sample code.</p>