]> git.sesse.net Git - vlc/blobdiff - modules/codec/omxil/android_mediacodec.c
mediacodec: Fix freeze when seeking on pause
[vlc] / modules / codec / omxil / android_mediacodec.c
index bab852248ecbff8b6ee3ce55153df8aa061ea96c..282b7bbf22694728894c572e2be50a3ec729ceed 100644 (file)
@@ -141,7 +141,9 @@ struct decoder_sys_t
     jfieldID profile_levels_field, profile_field, level_field;
     jmethodID get_supported_types, get_name;
     jmethodID create_by_codec_name, configure, start, stop, flush, release;
-    jmethodID get_output_format, get_input_buffers, get_output_buffers;
+    jmethodID get_output_format;
+    jmethodID get_input_buffers, get_input_buffer;
+    jmethodID get_output_buffers, get_output_buffer;
     jmethodID dequeue_input_buffer, dequeue_output_buffer, queue_input_buffer;
     jmethodID release_output_buffer;
     jmethodID create_video_format, set_integer, set_bytebuffer, get_integer;
@@ -172,6 +174,8 @@ struct decoder_sys_t
     unsigned int i_inflight_pictures;
 
     timestamp_fifo_t *timestamp_fifo;
+
+    int64_t i_preroll_end;
 };
 
 enum Types
@@ -202,50 +206,53 @@ struct member
     const char *class;
     int offset;
     int type;
+    bool critical;
 };
 static const struct member members[] = {
-    { "toString", "()Ljava/lang/String;", "java/lang/Object", OFF(tostring), METHOD },
-
-    { "getCodecCount", "()I", "android/media/MediaCodecList", OFF(get_codec_count), STATIC_METHOD },
-    { "getCodecInfoAt", "(I)Landroid/media/MediaCodecInfo;", "android/media/MediaCodecList", OFF(get_codec_info_at), STATIC_METHOD },
-
-    { "isEncoder", "()Z", "android/media/MediaCodecInfo", OFF(is_encoder), METHOD },
-    { "getSupportedTypes", "()[Ljava/lang/String;", "android/media/MediaCodecInfo", OFF(get_supported_types), METHOD },
-    { "getName", "()Ljava/lang/String;", "android/media/MediaCodecInfo", OFF(get_name), METHOD },
-    { "getCapabilitiesForType", "(Ljava/lang/String;)Landroid/media/MediaCodecInfo$CodecCapabilities;", "android/media/MediaCodecInfo", OFF(get_capabilities_for_type), METHOD },
-
-    { "profileLevels", "[Landroid/media/MediaCodecInfo$CodecProfileLevel;", "android/media/MediaCodecInfo$CodecCapabilities", OFF(profile_levels_field), FIELD },
-    { "profile", "I", "android/media/MediaCodecInfo$CodecProfileLevel", OFF(profile_field), FIELD },
-    { "level", "I", "android/media/MediaCodecInfo$CodecProfileLevel", OFF(level_field), FIELD },
-
-    { "createByCodecName", "(Ljava/lang/String;)Landroid/media/MediaCodec;", "android/media/MediaCodec", OFF(create_by_codec_name), STATIC_METHOD },
-    { "configure", "(Landroid/media/MediaFormat;Landroid/view/Surface;Landroid/media/MediaCrypto;I)V", "android/media/MediaCodec", OFF(configure), METHOD },
-    { "start", "()V", "android/media/MediaCodec", OFF(start), METHOD },
-    { "stop", "()V", "android/media/MediaCodec", OFF(stop), METHOD },
-    { "flush", "()V", "android/media/MediaCodec", OFF(flush), METHOD },
-    { "release", "()V", "android/media/MediaCodec", OFF(release), METHOD },
-    { "getOutputFormat", "()Landroid/media/MediaFormat;", "android/media/MediaCodec", OFF(get_output_format), METHOD },
-    { "getInputBuffers", "()[Ljava/nio/ByteBuffer;", "android/media/MediaCodec", OFF(get_input_buffers), METHOD },
-    { "getOutputBuffers", "()[Ljava/nio/ByteBuffer;", "android/media/MediaCodec", OFF(get_output_buffers), METHOD },
-    { "dequeueInputBuffer", "(J)I", "android/media/MediaCodec", OFF(dequeue_input_buffer), METHOD },
-    { "dequeueOutputBuffer", "(Landroid/media/MediaCodec$BufferInfo;J)I", "android/media/MediaCodec", OFF(dequeue_output_buffer), METHOD },
-    { "queueInputBuffer", "(IIIJI)V", "android/media/MediaCodec", OFF(queue_input_buffer), METHOD },
-    { "releaseOutputBuffer", "(IZ)V", "android/media/MediaCodec", OFF(release_output_buffer), METHOD },
-
-    { "createVideoFormat", "(Ljava/lang/String;II)Landroid/media/MediaFormat;", "android/media/MediaFormat", OFF(create_video_format), STATIC_METHOD },
-    { "setInteger", "(Ljava/lang/String;I)V", "android/media/MediaFormat", OFF(set_integer), METHOD },
-    { "getInteger", "(Ljava/lang/String;)I", "android/media/MediaFormat", OFF(get_integer), METHOD },
-    { "setByteBuffer", "(Ljava/lang/String;Ljava/nio/ByteBuffer;)V", "android/media/MediaFormat", OFF(set_bytebuffer), METHOD },
-
-    { "<init>", "()V", "android/media/MediaCodec$BufferInfo", OFF(buffer_info_ctor), METHOD },
-    { "size", "I", "android/media/MediaCodec$BufferInfo", OFF(size_field), FIELD },
-    { "offset", "I", "android/media/MediaCodec$BufferInfo", OFF(offset_field), FIELD },
-    { "presentationTimeUs", "J", "android/media/MediaCodec$BufferInfo", OFF(pts_field), FIELD },
-
-    { "allocateDirect", "(I)Ljava/nio/ByteBuffer;", "java/nio/ByteBuffer", OFF(allocate_direct), STATIC_METHOD },
-    { "limit", "(I)Ljava/nio/Buffer;", "java/nio/ByteBuffer", OFF(limit), METHOD },
-
-    { NULL, NULL, NULL, 0, 0 },
+    { "toString", "()Ljava/lang/String;", "java/lang/Object", OFF(tostring), METHOD, true },
+
+    { "getCodecCount", "()I", "android/media/MediaCodecList", OFF(get_codec_count), STATIC_METHOD, true },
+    { "getCodecInfoAt", "(I)Landroid/media/MediaCodecInfo;", "android/media/MediaCodecList", OFF(get_codec_info_at), STATIC_METHOD, true },
+
+    { "isEncoder", "()Z", "android/media/MediaCodecInfo", OFF(is_encoder), METHOD, true },
+    { "getSupportedTypes", "()[Ljava/lang/String;", "android/media/MediaCodecInfo", OFF(get_supported_types), METHOD, true },
+    { "getName", "()Ljava/lang/String;", "android/media/MediaCodecInfo", OFF(get_name), METHOD, true },
+    { "getCapabilitiesForType", "(Ljava/lang/String;)Landroid/media/MediaCodecInfo$CodecCapabilities;", "android/media/MediaCodecInfo", OFF(get_capabilities_for_type), METHOD, true },
+
+    { "profileLevels", "[Landroid/media/MediaCodecInfo$CodecProfileLevel;", "android/media/MediaCodecInfo$CodecCapabilities", OFF(profile_levels_field), FIELD, true },
+    { "profile", "I", "android/media/MediaCodecInfo$CodecProfileLevel", OFF(profile_field), FIELD, true },
+    { "level", "I", "android/media/MediaCodecInfo$CodecProfileLevel", OFF(level_field), FIELD, true },
+
+    { "createByCodecName", "(Ljava/lang/String;)Landroid/media/MediaCodec;", "android/media/MediaCodec", OFF(create_by_codec_name), STATIC_METHOD, true },
+    { "configure", "(Landroid/media/MediaFormat;Landroid/view/Surface;Landroid/media/MediaCrypto;I)V", "android/media/MediaCodec", OFF(configure), METHOD, true },
+    { "start", "()V", "android/media/MediaCodec", OFF(start), METHOD, true },
+    { "stop", "()V", "android/media/MediaCodec", OFF(stop), METHOD, true },
+    { "flush", "()V", "android/media/MediaCodec", OFF(flush), METHOD, true },
+    { "release", "()V", "android/media/MediaCodec", OFF(release), METHOD, true },
+    { "getOutputFormat", "()Landroid/media/MediaFormat;", "android/media/MediaCodec", OFF(get_output_format), METHOD, true },
+    { "getInputBuffers", "()[Ljava/nio/ByteBuffer;", "android/media/MediaCodec", OFF(get_input_buffers), METHOD, false },
+    { "getInputBuffer", "(I)Ljava/nio/ByteBuffer;", "android/media/MediaCodec", OFF(get_input_buffer), METHOD, false },
+    { "getOutputBuffers", "()[Ljava/nio/ByteBuffer;", "android/media/MediaCodec", OFF(get_output_buffers), METHOD, false },
+    { "getOutputBuffer", "(I)Ljava/nio/ByteBuffer;", "android/media/MediaCodec", OFF(get_output_buffer), METHOD, false },
+    { "dequeueInputBuffer", "(J)I", "android/media/MediaCodec", OFF(dequeue_input_buffer), METHOD, true },
+    { "dequeueOutputBuffer", "(Landroid/media/MediaCodec$BufferInfo;J)I", "android/media/MediaCodec", OFF(dequeue_output_buffer), METHOD, true },
+    { "queueInputBuffer", "(IIIJI)V", "android/media/MediaCodec", OFF(queue_input_buffer), METHOD, true },
+    { "releaseOutputBuffer", "(IZ)V", "android/media/MediaCodec", OFF(release_output_buffer), METHOD, true },
+
+    { "createVideoFormat", "(Ljava/lang/String;II)Landroid/media/MediaFormat;", "android/media/MediaFormat", OFF(create_video_format), STATIC_METHOD, true },
+    { "setInteger", "(Ljava/lang/String;I)V", "android/media/MediaFormat", OFF(set_integer), METHOD, true },
+    { "getInteger", "(Ljava/lang/String;)I", "android/media/MediaFormat", OFF(get_integer), METHOD, true },
+    { "setByteBuffer", "(Ljava/lang/String;Ljava/nio/ByteBuffer;)V", "android/media/MediaFormat", OFF(set_bytebuffer), METHOD, true },
+
+    { "<init>", "()V", "android/media/MediaCodec$BufferInfo", OFF(buffer_info_ctor), METHOD, true },
+    { "size", "I", "android/media/MediaCodec$BufferInfo", OFF(size_field), FIELD, true },
+    { "offset", "I", "android/media/MediaCodec$BufferInfo", OFF(offset_field), FIELD, true },
+    { "presentationTimeUs", "J", "android/media/MediaCodec$BufferInfo", OFF(pts_field), FIELD, true },
+
+    { "allocateDirect", "(I)Ljava/nio/ByteBuffer;", "java/nio/ByteBuffer", OFF(allocate_direct), STATIC_METHOD, true },
+    { "limit", "(I)Ljava/nio/Buffer;", "java/nio/ByteBuffer", OFF(limit), METHOD, true },
+
+    { NULL, NULL, NULL, 0, 0, false },
 };
 
 #define GET_INTEGER(obj, name) (*env)->CallIntMethod(env, obj, p_sys->get_integer, (*env)->NewStringUTF(env, name))
@@ -292,6 +299,37 @@ static int jstrcmp(JNIEnv* env, jobject str, const char* str2)
     return ret;
 }
 
+static inline bool check_exception( JNIEnv *env )
+{
+    if ((*env)->ExceptionOccurred(env)) {
+        (*env)->ExceptionClear(env);
+        return true;
+    }
+    else
+        return false;
+}
+#define CHECK_EXCEPTION() check_exception( env )
+
+static bool codec_is_blacklisted( const char *p_name, int i_name_len )
+{
+     static const char *blacklisted_codecs[] = {
+        /* software decoders */
+        "OMX.google.",
+        /* crashes mediaserver */
+        "OMX.MTK.VIDEO.DECODER.MPEG4",
+        NULL,
+     };
+
+     for( const char **pp_bl_codecs = blacklisted_codecs; *pp_bl_codecs != NULL;
+          pp_bl_codecs++ )
+     {
+        if( !strncmp( p_name, *pp_bl_codecs,
+            __MIN( strlen(*pp_bl_codecs), i_name_len ) ) )
+            return true;
+     }
+     return false;
+}
+
 /*****************************************************************************
  * OpenDecoder: Create the decoder instance
  *****************************************************************************/
@@ -340,9 +378,8 @@ static int OpenDecoder(vlc_object_t *p_this)
         *(jclass*)((uint8_t*)p_sys + classes[i].offset) =
             (*env)->FindClass(env, classes[i].name);
 
-        if ((*env)->ExceptionOccurred(env)) {
+        if (CHECK_EXCEPTION()) {
             msg_Warn(p_dec, "Unable to find class %s", classes[i].name);
-            (*env)->ExceptionClear(env);
             goto error;
         }
     }
@@ -352,9 +389,8 @@ static int OpenDecoder(vlc_object_t *p_this)
         if (i == 0 || strcmp(members[i].class, members[i - 1].class))
             last_class = (*env)->FindClass(env, members[i].class);
 
-        if ((*env)->ExceptionOccurred(env)) {
+        if (CHECK_EXCEPTION()) {
             msg_Warn(p_dec, "Unable to find class %s", members[i].class);
-            (*env)->ExceptionClear(env);
             goto error;
         }
 
@@ -372,13 +408,22 @@ static int OpenDecoder(vlc_object_t *p_this)
                 (*env)->GetFieldID(env, last_class, members[i].name, members[i].sig);
             break;
         }
-        if ((*env)->ExceptionOccurred(env)) {
+        if (CHECK_EXCEPTION()) {
             msg_Warn(p_dec, "Unable to find the member %s in %s",
                      members[i].name, members[i].class);
-            (*env)->ExceptionClear(env);
-            goto error;
+            if (members[i].critical)
+                goto error;
         }
     }
+    /* getInputBuffers and getOutputBuffers are deprecated if API >= 21
+     * use getInputBuffer and getOutputBuffer instead. */
+    if (p_sys->get_input_buffer && p_sys->get_output_buffer) {
+        p_sys->get_output_buffers =
+        p_sys->get_input_buffers = NULL;
+    } else if (!p_sys->get_output_buffers && !p_sys->get_input_buffers) {
+        msg_Warn(p_dec, "Unable to find get Output/Input Buffer/Buffers");
+        goto error;
+    }
 
     int num_codecs = (*env)->CallStaticIntMethod(env, p_sys->media_codec_list_class,
                                                  p_sys->get_codec_count);
@@ -402,9 +447,8 @@ static int OpenDecoder(vlc_object_t *p_this)
 
         codec_capabilities = (*env)->CallObjectMethod(env, info, p_sys->get_capabilities_for_type,
                                                       (*env)->NewStringUTF(env, mime));
-        if ((*env)->ExceptionOccurred(env)) {
+        if (CHECK_EXCEPTION()) {
             msg_Warn(p_dec, "Exception occurred in MediaCodecInfo.getCapabilitiesForType");
-            (*env)->ExceptionClear(env);
             goto loopclean;
         } else if (codec_capabilities) {
             profile_levels = (*env)->GetObjectField(env, codec_capabilities, p_sys->profile_levels_field);
@@ -420,7 +464,7 @@ static int OpenDecoder(vlc_object_t *p_this)
         name_ptr = (*env)->GetStringUTFChars(env, name, NULL);
         found = false;
 
-        if (!strncmp(name_ptr, "OMX.google.", __MIN(11, name_len)))
+        if (codec_is_blacklisted( name_ptr, name_len))
             goto loopclean;
         for (int j = 0; j < num_types && !found; j++) {
             jobject type = (*env)->GetObjectArrayElement(env, types, j);
@@ -429,6 +473,11 @@ static int OpenDecoder(vlc_object_t *p_this)
                    now check if the capabilities of the codec is
                    matching the video format. */
                 if (p_dec->fmt_in.i_codec == VLC_CODEC_H264 && fmt_profile) {
+                    /* This decoder doesn't expose its profiles and is high
+                     * profile capable */
+                    if (!strncmp(name_ptr, "OMX.LUMEVideoDecoder", __MIN(20, name_len)))
+                        found = true;
+
                     for (int i = 0; i < profile_levels_len && !found; ++i) {
                         jobject profile_level = (*env)->GetObjectArrayElement(env, profile_levels, i);
 
@@ -479,9 +528,8 @@ loopclean:
     // but not in 4.1 devices.
     p_sys->codec = (*env)->CallStaticObjectMethod(env, p_sys->media_codec_class,
                                                   p_sys->create_by_codec_name, codec_name);
-    if ((*env)->ExceptionOccurred(env)) {
+    if (CHECK_EXCEPTION()) {
         msg_Warn(p_dec, "Exception occurred in MediaCodec.createByCodecName.");
-        (*env)->ExceptionClear(env);
         goto error;
     }
     p_sys->allocated = true;
@@ -521,6 +569,17 @@ loopclean:
        direct rendering should be disabled since no surface will be
        attached to the JNI. */
     p_sys->direct_rendering = jni_IsVideoPlayerActivityCreated() && var_InheritBool(p_dec, CFG_PREFIX "dr");
+
+    /* There is no way to rotate the video using direct rendering (and using a
+     * SurfaceView) before  API 21 (Lollipop). Therefore, we deactivate direct
+     * rendering if video doesn't have a normal rotation and if
+     * get_input_buffer method is not present (This method exists since API
+     * 21). */
+    if (p_sys->direct_rendering
+        && p_dec->fmt_in.video.orientation != ORIENT_NORMAL
+        && !p_sys->get_input_buffer)
+        p_sys->direct_rendering = false;
+
     if (p_sys->direct_rendering) {
         if (p_dec->fmt_in.video.orientation != ORIENT_NORMAL) {
             int i_angle;
@@ -547,9 +606,8 @@ loopclean:
         if (surf) {
             // Configure MediaCodec with the Android surface.
             (*env)->CallVoidMethod(env, p_sys->codec, p_sys->configure, format, surf, NULL, 0);
-            if ((*env)->ExceptionOccurred(env)) {
+            if (CHECK_EXCEPTION()) {
                 msg_Warn(p_dec, "Exception occurred in MediaCodec.configure with an output surface.");
-                (*env)->ExceptionClear(env);
                 jni_UnlockAndroidSurface();
                 goto error;
             }
@@ -562,26 +620,34 @@ loopclean:
     }
     if (!p_sys->direct_rendering) {
         (*env)->CallVoidMethod(env, p_sys->codec, p_sys->configure, format, NULL, NULL, 0);
-        if ((*env)->ExceptionOccurred(env)) {
+        if (CHECK_EXCEPTION()) {
             msg_Warn(p_dec, "Exception occurred in MediaCodec.configure");
-            (*env)->ExceptionClear(env);
             goto error;
         }
     }
 
     (*env)->CallVoidMethod(env, p_sys->codec, p_sys->start);
-    if ((*env)->ExceptionOccurred(env)) {
+    if (CHECK_EXCEPTION()) {
         msg_Warn(p_dec, "Exception occurred in MediaCodec.start");
-        (*env)->ExceptionClear(env);
         goto error;
     }
     p_sys->started = true;
 
-    p_sys->input_buffers = (*env)->CallObjectMethod(env, p_sys->codec, p_sys->get_input_buffers);
-    p_sys->output_buffers = (*env)->CallObjectMethod(env, p_sys->codec, p_sys->get_output_buffers);
+    if (p_sys->get_input_buffers && p_sys->get_output_buffers) {
+        p_sys->input_buffers = (*env)->CallObjectMethod(env, p_sys->codec, p_sys->get_input_buffers);
+        if (CHECK_EXCEPTION()) {
+            msg_Err(p_dec, "Exception in MediaCodec.getInputBuffers (OpenDecoder)");
+            goto error;
+        }
+        p_sys->output_buffers = (*env)->CallObjectMethod(env, p_sys->codec, p_sys->get_output_buffers);
+        if (CHECK_EXCEPTION()) {
+            msg_Err(p_dec, "Exception in MediaCodec.getOutputBuffers (OpenDecoder)");
+            goto error;
+        }
+        p_sys->input_buffers = (*env)->NewGlobalRef(env, p_sys->input_buffers);
+        p_sys->output_buffers = (*env)->NewGlobalRef(env, p_sys->output_buffers);
+    }
     p_sys->buffer_info = (*env)->NewObject(env, p_sys->buffer_info_class, p_sys->buffer_info_ctor);
-    p_sys->input_buffers = (*env)->NewGlobalRef(env, p_sys->input_buffers);
-    p_sys->output_buffers = (*env)->NewGlobalRef(env, p_sys->output_buffers);
     p_sys->buffer_info = (*env)->NewGlobalRef(env, p_sys->buffer_info);
     (*env)->DeleteLocalRef(env, format);
 
@@ -622,18 +688,14 @@ static void CloseDecoder(vlc_object_t *p_this)
         if (p_sys->started)
         {
             (*env)->CallVoidMethod(env, p_sys->codec, p_sys->stop);
-            if ((*env)->ExceptionOccurred(env)) {
+            if (CHECK_EXCEPTION())
                 msg_Err(p_dec, "Exception in MediaCodec.stop");
-                (*env)->ExceptionClear(env);
-            }
         }
         if (p_sys->allocated)
         {
             (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release);
-            if ((*env)->ExceptionOccurred(env)) {
+            if (CHECK_EXCEPTION())
                 msg_Err(p_dec, "Exception in MediaCodec.release");
-                (*env)->ExceptionClear(env);
-            }
         }
         (*env)->DeleteGlobalRef(env, p_sys->codec);
     }
@@ -649,6 +711,23 @@ static void CloseDecoder(vlc_object_t *p_this)
     free(p_sys);
 }
 
+/*****************************************************************************
+ * ReleaseOutputBuffer
+ *****************************************************************************/
+static int ReleaseOutputBuffer(decoder_t *p_dec, JNIEnv *env, int i_index,
+                               bool b_render)
+{
+    decoder_sys_t *p_sys = p_dec->p_sys;
+
+    (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer,
+                           i_index, b_render);
+    if (CHECK_EXCEPTION()) {
+        msg_Err(p_dec, "Exception in MediaCodec.releaseOutputBuffer");
+        return -1;
+    }
+    return 0;
+}
+
 /*****************************************************************************
  * vout callbacks
  *****************************************************************************/
@@ -656,7 +735,6 @@ static void UnlockPicture(picture_t* p_pic, bool b_render)
 {
     picture_sys_t *p_picsys = p_pic->p_sys;
     decoder_t *p_dec = p_picsys->priv.hw.p_dec;
-    decoder_sys_t *p_sys = p_dec->p_sys;
 
     if (!p_picsys->priv.hw.b_valid)
         return;
@@ -675,12 +753,7 @@ static void UnlockPicture(picture_t* p_pic, bool b_render)
     /* Release the MediaCodec buffer. */
     JNIEnv *env = NULL;
     jni_attach_thread(&env, THREAD_NAME);
-    (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, i_index, b_render);
-    if ((*env)->ExceptionOccurred(env)) {
-        msg_Err(p_dec, "Exception in MediaCodec.releaseOutputBuffer (DisplayBuffer)");
-        (*env)->ExceptionClear(env);
-    }
-
+    ReleaseOutputBuffer(p_dec, env, i_index, b_render);
     jni_detach_thread();
     p_picsys->priv.hw.b_valid = false;
 
@@ -723,197 +796,214 @@ static int InsertInflightPicture(decoder_t *p_dec, picture_t *p_pic,
     return 0;
 }
 
-static void GetOutput(decoder_t *p_dec, JNIEnv *env, picture_t **pp_pic, jlong timeout)
+static int PutInput(decoder_t *p_dec, JNIEnv *env, block_t *p_block, jlong timeout)
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
-    while (1) {
-        int index = (*env)->CallIntMethod(env, p_sys->codec, p_sys->dequeue_output_buffer,
-                                          p_sys->buffer_info, timeout);
-        if ((*env)->ExceptionOccurred(env)) {
-            msg_Err(p_dec, "Exception in MediaCodec.dequeueOutputBuffer (GetOutput)");
-            (*env)->ExceptionClear(env);
-            p_sys->error_state = true;
-            return;
-        }
+    int index;
+    jobject buf;
+    jsize size;
+    uint8_t *bufptr;
+    struct H264ConvertState convert_state = { 0, 0 };
 
-        if (index >= 0) {
-            if (!p_sys->pixel_format) {
-                msg_Warn(p_dec, "Buffers returned before output format is set, dropping frame");
-                (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, index, false);
-                if ((*env)->ExceptionOccurred(env)) {
-                    msg_Err(p_dec, "Exception in MediaCodec.releaseOutputBuffer");
-                    (*env)->ExceptionClear(env);
-                    p_sys->error_state = true;
-                    return;
-                }
-                continue;
-            }
+    index = (*env)->CallIntMethod(env, p_sys->codec,
+                                  p_sys->dequeue_input_buffer, timeout);
+    if (CHECK_EXCEPTION()) {
+        msg_Err(p_dec, "Exception occurred in MediaCodec.dequeueInputBuffer");
+        return -1;
+    }
+    if (index < 0)
+        return 0;
 
-            if (!*pp_pic) {
-                *pp_pic = decoder_NewPicture(p_dec);
-            } else if (p_sys->direct_rendering) {
-                picture_t *p_pic = *pp_pic;
-                picture_sys_t *p_picsys = p_pic->p_sys;
-                int i_prev_index = p_picsys->priv.hw.i_index;
-                (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, i_prev_index, false);
-                if ((*env)->ExceptionOccurred(env)) {
-                    msg_Err(p_dec, "Exception in MediaCodec.releaseOutputBuffer " \
-                            "(GetOutput, overwriting previous picture)");
-                    (*env)->ExceptionClear(env);
-                    p_sys->error_state = true;
-                    return;
-                }
+    if (p_sys->get_input_buffers)
+        buf = (*env)->GetObjectArrayElement(env, p_sys->input_buffers, index);
+    else
+        buf = (*env)->CallObjectMethod(env, p_sys->codec, p_sys->get_input_buffer, index);
+    size = (*env)->GetDirectBufferCapacity(env, buf);
+    bufptr = (*env)->GetDirectBufferAddress(env, buf);
+    if (size < 0) {
+        msg_Err(p_dec, "Java buffer has invalid size");
+        return -1;
+    }
+    if ((size_t) size > p_block->i_buffer)
+        size = p_block->i_buffer;
+    memcpy(bufptr, p_block->p_buffer, size);
+
+    convert_h264_to_annexb(bufptr, size, p_sys->nal_size, &convert_state);
+
+    int64_t ts = p_block->i_pts;
+    if (!ts && p_block->i_dts)
+        ts = p_block->i_dts;
+    if (p_block->i_flags & BLOCK_FLAG_PREROLL )
+        p_sys->i_preroll_end = ts;
+    timestamp_FifoPut(p_sys->timestamp_fifo, p_block->i_pts ? VLC_TS_INVALID : p_block->i_dts);
+    (*env)->CallVoidMethod(env, p_sys->codec, p_sys->queue_input_buffer, index, 0, size, ts, 0);
+    (*env)->DeleteLocalRef(env, buf);
+    if (CHECK_EXCEPTION()) {
+        msg_Err(p_dec, "Exception in MediaCodec.queueInputBuffer");
+        return -1;
+    }
+    p_sys->decoded = true;
 
-                // No need to lock here since the previous picture was not sent.
-                InsertInflightPicture(p_dec, NULL, i_prev_index);
-            }
-            if (*pp_pic) {
-
-                picture_t *p_pic = *pp_pic;
-                /* If the oldest input block had no PTS, the timestamp
-                 * of the frame returned by MediaCodec might be wrong
-                 * so we overwrite it with the corresponding dts. */
-                int64_t forced_ts = timestamp_FifoGet(p_sys->timestamp_fifo);
-                if (forced_ts == VLC_TS_INVALID)
-                    p_pic->date = (*env)->GetLongField(env, p_sys->buffer_info, p_sys->pts_field);
-                else
-                    p_pic->date = forced_ts;
-
-                if (p_sys->direct_rendering) {
-                    picture_sys_t *p_picsys = p_pic->p_sys;
-                    p_picsys->pf_lock_pic = NULL;
-                    p_picsys->pf_unlock_pic = UnlockPicture;
-                    p_picsys->priv.hw.p_dec = p_dec;
-                    p_picsys->priv.hw.i_index = index;
-                    p_picsys->priv.hw.b_valid = true;
-
-                    InsertInflightPicture(p_dec, p_pic, index);
-                } else {
-                    jobject buf = (*env)->GetObjectArrayElement(env, p_sys->output_buffers, index);
-                    //jsize buf_size = (*env)->GetDirectBufferCapacity(env, buf);
-                    uint8_t *ptr = (*env)->GetDirectBufferAddress(env, buf);
-
-                    //int size = (*env)->GetIntField(env, p_sys->buffer_info, p_sys->size_field);
-                    int offset = (*env)->GetIntField(env, p_sys->buffer_info, p_sys->offset_field);
-                    ptr += offset; // Check the size parameter as well
-
-                    unsigned int chroma_div;
-                    GetVlcChromaSizes(p_dec->fmt_out.i_codec, p_dec->fmt_out.video.i_width,
-                                      p_dec->fmt_out.video.i_height, NULL, NULL, &chroma_div);
-                    CopyOmxPicture(p_sys->pixel_format, p_pic, p_sys->slice_height, p_sys->stride,
-                                   ptr, chroma_div, &p_sys->architecture_specific_data);
-                    (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, index, false);
-
-                    jthrowable exception = (*env)->ExceptionOccurred(env);
-                    if (exception != NULL) {
-                        jclass illegalStateException = (*env)->FindClass(env, "java/lang/IllegalStateException");
-                        if((*env)->IsInstanceOf(env, exception, illegalStateException)) {
-                            msg_Err(p_dec, "Codec error (IllegalStateException) in MediaCodec.releaseOutputBuffer");
-                            (*env)->ExceptionClear(env);
-                            (*env)->DeleteLocalRef(env, illegalStateException);
-                            p_sys->error_state = true;
-                        }
-                    }
-                    (*env)->DeleteLocalRef(env, buf);
-                }
-            } else {
-                msg_Warn(p_dec, "NewPicture failed");
-                (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, index, false);
-                if ((*env)->ExceptionOccurred(env)) {
-                    msg_Err(p_dec, "Exception in MediaCodec.releaseOutputBuffer (GetOutput)");
-                    (*env)->ExceptionClear(env);
-                    p_sys->error_state = true;
-                }
-            }
-            return;
-
-        } else if (index == INFO_OUTPUT_BUFFERS_CHANGED) {
-            msg_Dbg(p_dec, "output buffers changed");
-            (*env)->DeleteGlobalRef(env, p_sys->output_buffers);
-
-            p_sys->output_buffers = (*env)->CallObjectMethod(env, p_sys->codec,
-                                                             p_sys->get_output_buffers);
-            if ((*env)->ExceptionOccurred(env)) {
-                msg_Err(p_dec, "Exception in MediaCodec.getOutputBuffer (GetOutput)");
-                (*env)->ExceptionClear(env);
-                p_sys->output_buffers = NULL;
-                p_sys->error_state = true;
-                return;
-            }
+    return 1;
+}
 
-            p_sys->output_buffers = (*env)->NewGlobalRef(env, p_sys->output_buffers);
-        } else if (index == INFO_OUTPUT_FORMAT_CHANGED) {
-            jobject format = (*env)->CallObjectMethod(env, p_sys->codec, p_sys->get_output_format);
-            if ((*env)->ExceptionOccurred(env)) {
-                msg_Err(p_dec, "Exception in MediaCodec.getOutputFormat (GetOutput)");
-                (*env)->ExceptionClear(env);
-                p_sys->error_state = true;
-                return;
-            }
+static int GetOutput(decoder_t *p_dec, JNIEnv *env, picture_t *p_pic, jlong timeout)
+{
+    decoder_sys_t *p_sys = p_dec->p_sys;
+    int index = (*env)->CallIntMethod(env, p_sys->codec, p_sys->dequeue_output_buffer,
+                                      p_sys->buffer_info, timeout);
+    if (CHECK_EXCEPTION()) {
+        msg_Err(p_dec, "Exception in MediaCodec.dequeueOutputBuffer (GetOutput)");
+        return -1;
+    }
+
+    if (index >= 0) {
+        int64_t i_buffer_pts;
 
-            jobject format_string = (*env)->CallObjectMethod(env, format, p_sys->tostring);
-
-            jsize format_len = (*env)->GetStringUTFLength(env, format_string);
-            const char *format_ptr = (*env)->GetStringUTFChars(env, format_string, NULL);
-            msg_Dbg(p_dec, "output format changed: %.*s", format_len, format_ptr);
-            (*env)->ReleaseStringUTFChars(env, format_string, format_ptr);
-
-            ArchitectureSpecificCopyHooksDestroy(p_sys->pixel_format, &p_sys->architecture_specific_data);
-
-            int width           = GET_INTEGER(format, "width");
-            int height          = GET_INTEGER(format, "height");
-            p_sys->stride       = GET_INTEGER(format, "stride");
-            p_sys->slice_height = GET_INTEGER(format, "slice-height");
-            p_sys->pixel_format = GET_INTEGER(format, "color-format");
-            int crop_left       = GET_INTEGER(format, "crop-left");
-            int crop_top        = GET_INTEGER(format, "crop-top");
-            int crop_right      = GET_INTEGER(format, "crop-right");
-            int crop_bottom     = GET_INTEGER(format, "crop-bottom");
-
-            const char *name = "unknown";
-            if (!p_sys->direct_rendering) {
-                if (!GetVlcChromaFormat(p_sys->pixel_format,
-                                        &p_dec->fmt_out.i_codec, &name)) {
-                    msg_Err(p_dec, "color-format not recognized");
-                    p_sys->error_state = true;
-                    return;
+        if (!p_sys->pixel_format || !p_pic) {
+            msg_Warn(p_dec, "Buffers returned before output format is set, dropping frame");
+            return ReleaseOutputBuffer(p_dec, env, index, false);
+        }
+
+        i_buffer_pts = (*env)->GetLongField(env, p_sys->buffer_info, p_sys->pts_field);
+        if (i_buffer_pts <= p_sys->i_preroll_end)
+            return ReleaseOutputBuffer(p_dec, env, index, false);
+
+        /* If the oldest input block had no PTS, the timestamp
+         * of the frame returned by MediaCodec might be wrong
+         * so we overwrite it with the corresponding dts. */
+        int64_t forced_ts = timestamp_FifoGet(p_sys->timestamp_fifo);
+        if (forced_ts == VLC_TS_INVALID)
+            p_pic->date = i_buffer_pts;
+        else
+            p_pic->date = forced_ts;
+
+        if (p_sys->direct_rendering) {
+            picture_sys_t *p_picsys = p_pic->p_sys;
+            p_picsys->pf_lock_pic = NULL;
+            p_picsys->pf_unlock_pic = UnlockPicture;
+            p_picsys->priv.hw.p_dec = p_dec;
+            p_picsys->priv.hw.i_index = index;
+            p_picsys->priv.hw.b_valid = true;
+
+            vlc_mutex_lock(get_android_opaque_mutex());
+            InsertInflightPicture(p_dec, p_pic, index);
+            vlc_mutex_unlock(get_android_opaque_mutex());
+        } else {
+            jobject buf;
+            if (p_sys->get_output_buffers)
+                buf = (*env)->GetObjectArrayElement(env, p_sys->output_buffers, index);
+            else
+                buf = (*env)->CallObjectMethod(env, p_sys->codec,
+                                               p_sys->get_output_buffer, index);
+            //jsize buf_size = (*env)->GetDirectBufferCapacity(env, buf);
+            uint8_t *ptr = (*env)->GetDirectBufferAddress(env, buf);
+
+            //int size = (*env)->GetIntField(env, p_sys->buffer_info, p_sys->size_field);
+            int offset = (*env)->GetIntField(env, p_sys->buffer_info, p_sys->offset_field);
+            ptr += offset; // Check the size parameter as well
+
+            unsigned int chroma_div;
+            GetVlcChromaSizes(p_dec->fmt_out.i_codec, p_dec->fmt_out.video.i_width,
+                              p_dec->fmt_out.video.i_height, NULL, NULL, &chroma_div);
+            CopyOmxPicture(p_sys->pixel_format, p_pic, p_sys->slice_height, p_sys->stride,
+                           ptr, chroma_div, &p_sys->architecture_specific_data);
+            (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, index, false);
+
+            jthrowable exception = (*env)->ExceptionOccurred(env);
+            if (exception != NULL) {
+                jclass illegalStateException = (*env)->FindClass(env, "java/lang/IllegalStateException");
+                if((*env)->IsInstanceOf(env, exception, illegalStateException)) {
+                    msg_Err(p_dec, "Codec error (IllegalStateException) in MediaCodec.releaseOutputBuffer");
+                    (*env)->ExceptionClear(env);
+                    (*env)->DeleteLocalRef(env, illegalStateException);
+                    (*env)->DeleteLocalRef(env, buf);
+                    return -1;
                 }
             }
+            (*env)->DeleteLocalRef(env, buf);
+        }
+        return 1;
+    } else if (index == INFO_OUTPUT_BUFFERS_CHANGED) {
+        msg_Dbg(p_dec, "output buffers changed");
+        if (!p_sys->get_output_buffers)
+            return 0;
+        (*env)->DeleteGlobalRef(env, p_sys->output_buffers);
+
+        p_sys->output_buffers = (*env)->CallObjectMethod(env, p_sys->codec,
+                                                         p_sys->get_output_buffers);
+        if (CHECK_EXCEPTION()) {
+            msg_Err(p_dec, "Exception in MediaCodec.getOutputBuffer (GetOutput)");
+            p_sys->output_buffers = NULL;
+            return -1;
+        }
 
-            msg_Err(p_dec, "output: %d %s, %dx%d stride %d %d, crop %d %d %d %d",
-                    p_sys->pixel_format, name, width, height, p_sys->stride, p_sys->slice_height,
-                    crop_left, crop_top, crop_right, crop_bottom);
+        p_sys->output_buffers = (*env)->NewGlobalRef(env, p_sys->output_buffers);
+    } else if (index == INFO_OUTPUT_FORMAT_CHANGED) {
+        jobject format = (*env)->CallObjectMethod(env, p_sys->codec, p_sys->get_output_format);
+        if (CHECK_EXCEPTION()) {
+            msg_Err(p_dec, "Exception in MediaCodec.getOutputFormat (GetOutput)");
+            return -1;
+        }
 
-            p_dec->fmt_out.video.i_width = crop_right + 1 - crop_left;
-            p_dec->fmt_out.video.i_height = crop_bottom + 1 - crop_top;
-            if (p_dec->fmt_out.video.i_width <= 1
-                || p_dec->fmt_out.video.i_height <= 1) {
-                p_dec->fmt_out.video.i_width = width;
-                p_dec->fmt_out.video.i_height = height;
-            }
-            p_dec->fmt_out.video.i_visible_width = p_dec->fmt_out.video.i_width;
-            p_dec->fmt_out.video.i_visible_height = p_dec->fmt_out.video.i_height;
-
-            if (p_sys->stride <= 0)
-                p_sys->stride = width;
-            if (p_sys->slice_height <= 0)
-                p_sys->slice_height = height;
-            if ((*env)->ExceptionOccurred(env))
-                (*env)->ExceptionClear(env);
-
-            ArchitectureSpecificCopyHooks(p_dec, p_sys->pixel_format, p_sys->slice_height,
-                                          p_sys->stride, &p_sys->architecture_specific_data);
-            if (p_sys->pixel_format == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
-                p_sys->slice_height -= crop_top/2;
-            if (IgnoreOmxDecoderPadding(p_sys->name)) {
-                p_sys->slice_height = 0;
-                p_sys->stride = p_dec->fmt_out.video.i_width;
+        jobject format_string = (*env)->CallObjectMethod(env, format, p_sys->tostring);
+
+        jsize format_len = (*env)->GetStringUTFLength(env, format_string);
+        const char *format_ptr = (*env)->GetStringUTFChars(env, format_string, NULL);
+        msg_Dbg(p_dec, "output format changed: %.*s", format_len, format_ptr);
+        (*env)->ReleaseStringUTFChars(env, format_string, format_ptr);
+
+        ArchitectureSpecificCopyHooksDestroy(p_sys->pixel_format, &p_sys->architecture_specific_data);
+
+        int width           = GET_INTEGER(format, "width");
+        int height          = GET_INTEGER(format, "height");
+        p_sys->stride       = GET_INTEGER(format, "stride");
+        p_sys->slice_height = GET_INTEGER(format, "slice-height");
+        p_sys->pixel_format = GET_INTEGER(format, "color-format");
+        int crop_left       = GET_INTEGER(format, "crop-left");
+        int crop_top        = GET_INTEGER(format, "crop-top");
+        int crop_right      = GET_INTEGER(format, "crop-right");
+        int crop_bottom     = GET_INTEGER(format, "crop-bottom");
+
+        const char *name = "unknown";
+        if (!p_sys->direct_rendering) {
+            if (!GetVlcChromaFormat(p_sys->pixel_format,
+                                    &p_dec->fmt_out.i_codec, &name)) {
+                msg_Err(p_dec, "color-format not recognized");
+                return -1;
             }
+        }
 
-        } else {
-            return;
+        msg_Err(p_dec, "output: %d %s, %dx%d stride %d %d, crop %d %d %d %d",
+                p_sys->pixel_format, name, width, height, p_sys->stride, p_sys->slice_height,
+                crop_left, crop_top, crop_right, crop_bottom);
+
+        p_dec->fmt_out.video.i_width = crop_right + 1 - crop_left;
+        p_dec->fmt_out.video.i_height = crop_bottom + 1 - crop_top;
+        if (p_dec->fmt_out.video.i_width <= 1
+            || p_dec->fmt_out.video.i_height <= 1) {
+            p_dec->fmt_out.video.i_width = width;
+            p_dec->fmt_out.video.i_height = height;
+        }
+        p_dec->fmt_out.video.i_visible_width = p_dec->fmt_out.video.i_width;
+        p_dec->fmt_out.video.i_visible_height = p_dec->fmt_out.video.i_height;
+
+        if (p_sys->stride <= 0)
+            p_sys->stride = width;
+        if (p_sys->slice_height <= 0)
+            p_sys->slice_height = height;
+        CHECK_EXCEPTION();
+
+        ArchitectureSpecificCopyHooks(p_dec, p_sys->pixel_format, p_sys->slice_height,
+                                      p_sys->stride, &p_sys->architecture_specific_data);
+        if (p_sys->pixel_format == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
+            p_sys->slice_height -= crop_top/2;
+        if (IgnoreOmxDecoderPadding(p_sys->name)) {
+            p_sys->slice_height = 0;
+            p_sys->stride = p_dec->fmt_out.video.i_width;
         }
     }
+    return 0;
 }
 
 static picture_t *DecodeVideo(decoder_t *p_dec, block_t **pp_block)
@@ -921,27 +1011,21 @@ static picture_t *DecodeVideo(decoder_t *p_dec, block_t **pp_block)
     decoder_sys_t *p_sys = p_dec->p_sys;
     picture_t *p_pic = NULL;
     JNIEnv *env = NULL;
-    struct H264ConvertState convert_state = { 0, 0 };
 
     if (!pp_block || !*pp_block)
         return NULL;
 
-    block_t *p_block = *pp_block;
-
-    if (p_sys->error_state) {
-        block_Release(p_block);
-        if (!p_sys->error_event_sent) {
-            /* Signal the error to the Java. */
-            jni_EventHardwareAccelerationError();
-            p_sys->error_event_sent = true;
-        }
-        return NULL;
-    }
+    if (p_sys->error_state)
+        goto endclean;
 
     jni_attach_thread(&env, THREAD_NAME);
+    if (!env)
+        goto endclean;
 
-    if (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED)) {
-        block_Release(p_block);
+    if ((*pp_block)->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED)) {
+        block_Release(*pp_block);
+        *pp_block = NULL;
+        p_sys->i_preroll_end = 0;
         timestamp_FifoEmpty(p_sys->timestamp_fifo);
         if (p_sys->decoded) {
             /* Invalidate all pictures that are currently in flight
@@ -951,15 +1035,13 @@ static picture_t *DecodeVideo(decoder_t *p_dec, block_t **pp_block)
                 InvalidateAllPictures(p_dec);
 
             (*env)->CallVoidMethod(env, p_sys->codec, p_sys->flush);
-            if ((*env)->ExceptionOccurred(env)) {
+            if (CHECK_EXCEPTION()) {
                 msg_Warn(p_dec, "Exception occurred in MediaCodec.flush");
-                (*env)->ExceptionClear(env);
                 p_sys->error_state = true;
             }
         }
         p_sys->decoded = false;
-        jni_detach_thread();
-        return NULL;
+        goto endclean;
     }
 
     /* Use the aspect ratio provided by the input (ie read from packetizer).
@@ -971,100 +1053,69 @@ static picture_t *DecodeVideo(decoder_t *p_dec, block_t **pp_block)
     }
 
     jlong timeout = 0;
-    const int max_polling_attempts = 50;
-    int attempts = 0;
-    while (true) {
-        int index = (*env)->CallIntMethod(env, p_sys->codec, p_sys->dequeue_input_buffer, (jlong) 0);
-        if ((*env)->ExceptionOccurred(env)) {
-            msg_Err(p_dec, "Exception occurred in MediaCodec.dequeueInputBuffer");
-            (*env)->ExceptionClear(env);
-            p_sys->error_state = true;
-            break;
-        }
-
-        if (index < 0) {
-            GetOutput(p_dec, env, &p_pic, timeout);
-            if (p_sys->error_state)
+    int i_output_ret = 0;
+    int i_input_ret = 0;
+    /* return when pp_block is processed or when we got an output pic */
+    while (i_input_ret != 1 && i_output_ret != 1) {
+        if (i_input_ret == 0) {
+            i_input_ret = PutInput(p_dec, env, *pp_block, timeout);
+            if (i_input_ret == 1) {
+                block_Release(*pp_block);
+                *pp_block = NULL;
+            } else if (i_input_ret == -1) {
+                p_sys->error_state = true;
                 break;
-            if (p_pic) {
-                /* If we couldn't get an available input buffer but a
-                 * decoded frame is available, we return the frame
-                 * without assigning NULL to *pp_block. The next call
-                 * to DecodeVideo will try to send the input packet again.
-                 */
-                jni_detach_thread();
-                return p_pic;
             }
-            timeout = 30 * 1000;
-            ++attempts;
-            /* With opaque DR the output buffers are released by the
-               vout therefore we implement a timeout for polling in
-               order to avoid being indefinitely stalled in this loop. */
-            if (p_sys->direct_rendering && attempts == max_polling_attempts) {
-                picture_t *invalid_picture = decoder_NewPicture(p_dec);
-                if (invalid_picture) {
-                    invalid_picture->date = VLC_TS_INVALID;
-                    picture_sys_t *p_picsys = invalid_picture->p_sys;
-                    p_picsys->pf_lock_pic = NULL;
-                    p_picsys->pf_unlock_pic = NULL;
-                    p_picsys->priv.hw.p_dec = NULL;
-                    p_picsys->priv.hw.i_index = -1;
-                    p_picsys->priv.hw.b_valid = false;
-                }
-                else {
-                    /* If we cannot return a picture we must free the
-                       block since the decoder will proceed with the
-                       next block. */
-                    block_Release(p_block);
-                    *pp_block = NULL;
-                }
-                jni_detach_thread();
-                return invalid_picture;
-            }
-            continue;
         }
 
-        jobject buf = (*env)->GetObjectArrayElement(env, p_sys->input_buffers, index);
-        jsize size = (*env)->GetDirectBufferCapacity(env, buf);
-        uint8_t *bufptr = (*env)->GetDirectBufferAddress(env, buf);
-        if (size < 0) {
-            msg_Err(p_dec, "Java buffer has invalid size");
-            p_sys->error_state = true;
-            break;
-        }
-        if ((size_t) size > p_block->i_buffer)
-            size = p_block->i_buffer;
-        memcpy(bufptr, p_block->p_buffer, size);
-
-        convert_h264_to_annexb(bufptr, size, p_sys->nal_size, &convert_state);
-
-        int64_t ts = p_block->i_pts;
-        if (!ts && p_block->i_dts)
-            ts = p_block->i_dts;
-        timestamp_FifoPut(p_sys->timestamp_fifo, p_block->i_pts ? VLC_TS_INVALID : p_block->i_dts);
-        (*env)->CallVoidMethod(env, p_sys->codec, p_sys->queue_input_buffer, index, 0, size, ts, 0);
-        (*env)->DeleteLocalRef(env, buf);
-        if ((*env)->ExceptionOccurred(env)) {
-            msg_Err(p_dec, "Exception in MediaCodec.queueInputBuffer");
-            (*env)->ExceptionClear(env);
-            p_sys->error_state = true;
-            break;
+        if (i_output_ret == 0) {
+            /* FIXME: A new picture shouldn't be created each time.
+             * If decoder_NewPicture fails because the decoder is
+             * flushing/exiting, GetOutput will either fail (or crash in
+             * function of devices), or never return an output buffer. Indeed,
+             * if the Decoder is flushing, MediaCodec can be stalled since the
+             * input is waiting for the output or vice-versa.  Therefore, call
+             * decoder_NewPicture before GetOutput as a safeguard. */
+
+            if (p_sys->pixel_format) {
+                p_pic = decoder_NewPicture(p_dec);
+                if (!p_pic) {
+                    msg_Warn(p_dec, "NewPicture failed");
+                    goto endclean;
+                }
+            }
+            i_output_ret = GetOutput(p_dec, env, p_pic, timeout);
+            msg_Err(p_dec, "i_output_ret: %d", i_output_ret);
+            if (i_output_ret == -1) {
+                p_sys->error_state = true;
+                break;
+            } else if (i_output_ret == 0 && p_pic) {
+                picture_Release(p_pic);
+                p_pic = NULL;
+            }
         }
-        p_sys->decoded = true;
-        break;
+        timeout = 10 * 1000;
     }
+
+endclean:
     if (p_sys->error_state) {
+        if( pp_block && *pp_block )
+        {
+            block_Release(*pp_block);
+            *pp_block = NULL;
+        }
         if (p_pic)
             picture_Release(p_pic);
-        jni_detach_thread();
-        return NULL;
-    }
-    if (!p_pic)
-        GetOutput(p_dec, env, &p_pic, 0);
-    jni_detach_thread();
+        p_pic = NULL;
 
-    block_Release(p_block);
-    *pp_block = NULL;
+        if (!p_sys->error_event_sent) {
+            /* Signal the error to the Java. */
+            jni_EventHardwareAccelerationError();
+            p_sys->error_event_sent = true;
+        }
+    }
+    if (env != NULL)
+        jni_detach_thread();
 
     return p_pic;
 }