title: Encoder crash on Adreno GPU while encoding from Surface
body: <p>I've been struggling with this issue for more than a week, and most likely it is a bug in the <strong>Qualcomm GPU/hardware video encoder</strong>. Since we are pressed to release the application, and their developer forums did not provide any feedback, I am posting it here too, hoping that someone is able to provide some clues, or even better, a workaround so that the bug in the encoder is not triggered.</p>
  
  <p>The application encodes from a Surface. When certain images are rendered to the surface, the encoder fails (100%) in the same place (when the frame currently being encoded is a key frame). The Logcat output from the encoder at the point of the crash is(with the last lines repeated at a very high rate):</p>
  
  <pre><code>11-26 11:41:33.312: E/OMX-VENC-720p(25949): ERROR: async_msg_process() - Error statuscode = 1
  11-26 11:41:33.312: E/ACodec(29317): [OMX.qcom.video.encoder.avc] ERROR(0x80001009)
  11-26 11:41:33.312: E/MediaCodec(29317): Codec reported an error. (omx error 0x80001009, internalError -2147483648)
  11-26 11:41:33.362: E/OMX-VENC-720p(25949): ERROR: venc_use_buf:set input buffer failed 
  11-26 11:41:33.362: E/OMX-VENC-720p(25949): ERROR: in dev_use_buf
  11-26 11:41:33.362: E/OMX-VENC-720p(25949): ERROR: empty_buffer_done() failed!
  11-26 11:41:33.372: E/OMX-VENC-720p(25949): m_fbd_count at o/p flush = 306
  11-26 11:41:33.372: E/OMX-VENC-720p(25949): m_etb_count at i/p flush = 313
  11-26 11:41:33.372: E/OMX-VENC-720p(25949): ERROR: ioctl VEN_IOCTL_CMD_FILL_OUTPUT_BUFFER failed
  11-26 11:41:33.372: E/OMX-VENC-720p(25949): ERROR: dev_fill_buf() Failed
  11-26 11:41:33.372: E/OMX-VENC-720p(25949): ERROR: FTBProxy() failed!
  </code></pre>
  
  <p>I can replicate it also on <a href="https://github.com/google/grafika" rel="nofollow noreferrer">Grafika</a> by simply replacing in the <em>Record GL app</em> the drawing of two geometric forms with drawing one of the problematic images, full screen, in landscape mode, and changing the bitrate to a higher value (7Mbps).
  The crash of the encoder happens sooner for larger bitrate.</p>
  
  <p>Here is one <a href="http://turl.ca/tqzs" rel="nofollow noreferrer">image</a> that seems to break it easily and here is <a href="http://pastebin.com/gYXn5UWa" rel="nofollow noreferrer">RecordFBOActivity.java</a> with the required changes. </p>
  
  <p>With Grafika, I tested and the encoder crashes on both Samsung S4, international version, and on the original Nexus 4. With our software, which is a little more complex than rendering the plain image, it still crashes on both. Did not test on any other Adreno devices. On Samsung S3, with a Mali 400 GPU, it works fine.</p>
  
  <p>At 4Mbps, the encoder in our application still crashes on both S4 and N4, but later. Grafika crashes on N4 in the same place, but not on S4.</p>
  
  <p>EDIT: According to comments bellow, it can be reproduced also when encoding the same image from the buffer. Various tests seem to narrow the conditions to reproduce it at: h264 hw encoder on Qualcomm devices, encoding a still image for many frames (this determines very low bitrates in the encoder due to similar frames), error appearing while encoding a key frame (the error appears only when encoding certain images, which seem to have more detail, i.e. need many bits for intra coding).</p>