]> git.sesse.net Git - vlc/blob - modules/codec/omxil/android_mediacodec.c
mediacodec: remove debug log
[vlc] / modules / codec / omxil / android_mediacodec.c
1 /*****************************************************************************
2  * android_mediacodec.c: Video decoder module using the Android MediaCodec API
3  *****************************************************************************
4  * Copyright (C) 2012 Martin Storsjo
5  *
6  * Authors: Martin Storsjo <martin@martin.st>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <jni.h>
31 #include <stdint.h>
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_codec.h>
36 #include <vlc_block_helper.h>
37 #include <vlc_cpu.h>
38
39 #include "../h264_nal.h"
40 #include "../hevc_nal.h"
41 #include <OMX_Core.h>
42 #include <OMX_Component.h>
43 #include "omxil_utils.h"
44 #include "android_opaque.h"
45 #include "../../video_output/android/android_window.h"
46
47 #define INFO_OUTPUT_BUFFERS_CHANGED -3
48 #define INFO_OUTPUT_FORMAT_CHANGED  -2
49 #define INFO_TRY_AGAIN_LATER        -1
50
51 #define THREAD_NAME "android_mediacodec"
52
53 extern int jni_attach_thread(JNIEnv **env, const char *thread_name);
54 extern void jni_detach_thread();
55 /* JNI functions to get/set an Android Surface object. */
56 extern jobject jni_LockAndGetAndroidJavaSurface();
57 extern void jni_UnlockAndroidSurface();
58 extern void jni_EventHardwareAccelerationError();
59 extern bool jni_IsVideoPlayerActivityCreated();
60
61 /* Implementation of a circular buffer of timestamps with overwriting
62  * of older values. MediaCodec has only one type of timestamp, if a
63  * block has no PTS, we send the DTS instead. Some hardware decoders
64  * cannot cope with this situation and output the frames in the wrong
65  * order. As a workaround in this case, we use a FIFO of timestamps in
66  * order to remember which input packets had no PTS.  Since an
67  * hardware decoder can silently drop frames, this might cause a
68  * growing desynchronization with the actual timestamp. Thus the
69  * circular buffer has a limited size and will overwrite older values.
70  */
71 typedef struct
72 {
73     uint32_t          begin;
74     uint32_t          size;
75     uint32_t          capacity;
76     int64_t           *buffer;
77 } timestamp_fifo_t;
78
79 static timestamp_fifo_t *timestamp_FifoNew(uint32_t capacity)
80 {
81     timestamp_fifo_t *fifo = calloc(1, sizeof(*fifo));
82     if (!fifo)
83         return NULL;
84     fifo->buffer = malloc(capacity * sizeof(*fifo->buffer));
85     if (!fifo->buffer) {
86         free(fifo);
87         return NULL;
88     }
89     fifo->capacity = capacity;
90     return fifo;
91 }
92
93 static void timestamp_FifoRelease(timestamp_fifo_t *fifo)
94 {
95     free(fifo->buffer);
96     free(fifo);
97 }
98
99 static bool timestamp_FifoIsEmpty(timestamp_fifo_t *fifo)
100 {
101     return fifo->size == 0;
102 }
103
104 static bool timestamp_FifoIsFull(timestamp_fifo_t *fifo)
105 {
106     return fifo->size == fifo->capacity;
107 }
108
109 static void timestamp_FifoEmpty(timestamp_fifo_t *fifo)
110 {
111     fifo->size = 0;
112 }
113
114 static void timestamp_FifoPut(timestamp_fifo_t *fifo, int64_t ts)
115 {
116     uint32_t end = (fifo->begin + fifo->size) % fifo->capacity;
117     fifo->buffer[end] = ts;
118     if (!timestamp_FifoIsFull(fifo))
119         fifo->size += 1;
120     else
121         fifo->begin = (fifo->begin + 1) % fifo->capacity;
122 }
123
124 static int64_t timestamp_FifoGet(timestamp_fifo_t *fifo)
125 {
126     if (timestamp_FifoIsEmpty(fifo))
127         return VLC_TS_INVALID;
128
129     int64_t result = fifo->buffer[fifo->begin];
130     fifo->begin = (fifo->begin + 1) % fifo->capacity;
131     fifo->size -= 1;
132     return result;
133 }
134
135 struct decoder_sys_t
136 {
137     jclass media_codec_list_class, media_codec_class, media_format_class;
138     jclass buffer_info_class, byte_buffer_class;
139     jmethodID tostring;
140     jmethodID get_codec_count, get_codec_info_at, is_encoder, get_capabilities_for_type;
141     jfieldID profile_levels_field, profile_field, level_field;
142     jmethodID get_supported_types, get_name;
143     jmethodID create_by_codec_name, configure, start, stop, flush, release;
144     jmethodID get_output_format;
145     jmethodID get_input_buffers, get_input_buffer;
146     jmethodID get_output_buffers, get_output_buffer;
147     jmethodID dequeue_input_buffer, dequeue_output_buffer, queue_input_buffer;
148     jmethodID release_output_buffer;
149     jmethodID create_video_format, set_integer, set_bytebuffer, get_integer;
150     jmethodID buffer_info_ctor;
151     jmethodID allocate_direct, limit;
152     jfieldID size_field, offset_field, pts_field;
153
154     uint32_t nal_size;
155
156     jobject codec;
157     jobject buffer_info;
158     jobject input_buffers, output_buffers;
159     int pixel_format;
160     int stride, slice_height;
161     char *name;
162
163     bool allocated;
164     bool started;
165     bool decoded;
166     bool error_state;
167     bool error_event_sent;
168
169     ArchitectureSpecificCopyData architecture_specific_data;
170
171     /* Direct rendering members. */
172     bool direct_rendering;
173     picture_t** pp_inflight_pictures; /**< stores the inflight picture for each output buffer or NULL */
174     unsigned int i_inflight_pictures;
175
176     timestamp_fifo_t *timestamp_fifo;
177
178     int64_t i_preroll_end;
179 };
180
181 enum Types
182 {
183     METHOD, STATIC_METHOD, FIELD
184 };
185
186 #define OFF(x) offsetof(struct decoder_sys_t, x)
187 struct classname
188 {
189     const char *name;
190     int offset;
191 };
192 static const struct classname classes[] = {
193     { "android/media/MediaCodecList", OFF(media_codec_list_class) },
194     { "android/media/MediaCodec", OFF(media_codec_class) },
195     { "android/media/MediaFormat", OFF(media_format_class) },
196     { "android/media/MediaFormat", OFF(media_format_class) },
197     { "android/media/MediaCodec$BufferInfo", OFF(buffer_info_class) },
198     { "java/nio/ByteBuffer", OFF(byte_buffer_class) },
199     { NULL, 0 },
200 };
201
202 struct member
203 {
204     const char *name;
205     const char *sig;
206     const char *class;
207     int offset;
208     int type;
209     bool critical;
210 };
211 static const struct member members[] = {
212     { "toString", "()Ljava/lang/String;", "java/lang/Object", OFF(tostring), METHOD, true },
213
214     { "getCodecCount", "()I", "android/media/MediaCodecList", OFF(get_codec_count), STATIC_METHOD, true },
215     { "getCodecInfoAt", "(I)Landroid/media/MediaCodecInfo;", "android/media/MediaCodecList", OFF(get_codec_info_at), STATIC_METHOD, true },
216
217     { "isEncoder", "()Z", "android/media/MediaCodecInfo", OFF(is_encoder), METHOD, true },
218     { "getSupportedTypes", "()[Ljava/lang/String;", "android/media/MediaCodecInfo", OFF(get_supported_types), METHOD, true },
219     { "getName", "()Ljava/lang/String;", "android/media/MediaCodecInfo", OFF(get_name), METHOD, true },
220     { "getCapabilitiesForType", "(Ljava/lang/String;)Landroid/media/MediaCodecInfo$CodecCapabilities;", "android/media/MediaCodecInfo", OFF(get_capabilities_for_type), METHOD, true },
221
222     { "profileLevels", "[Landroid/media/MediaCodecInfo$CodecProfileLevel;", "android/media/MediaCodecInfo$CodecCapabilities", OFF(profile_levels_field), FIELD, true },
223     { "profile", "I", "android/media/MediaCodecInfo$CodecProfileLevel", OFF(profile_field), FIELD, true },
224     { "level", "I", "android/media/MediaCodecInfo$CodecProfileLevel", OFF(level_field), FIELD, true },
225
226     { "createByCodecName", "(Ljava/lang/String;)Landroid/media/MediaCodec;", "android/media/MediaCodec", OFF(create_by_codec_name), STATIC_METHOD, true },
227     { "configure", "(Landroid/media/MediaFormat;Landroid/view/Surface;Landroid/media/MediaCrypto;I)V", "android/media/MediaCodec", OFF(configure), METHOD, true },
228     { "start", "()V", "android/media/MediaCodec", OFF(start), METHOD, true },
229     { "stop", "()V", "android/media/MediaCodec", OFF(stop), METHOD, true },
230     { "flush", "()V", "android/media/MediaCodec", OFF(flush), METHOD, true },
231     { "release", "()V", "android/media/MediaCodec", OFF(release), METHOD, true },
232     { "getOutputFormat", "()Landroid/media/MediaFormat;", "android/media/MediaCodec", OFF(get_output_format), METHOD, true },
233     { "getInputBuffers", "()[Ljava/nio/ByteBuffer;", "android/media/MediaCodec", OFF(get_input_buffers), METHOD, false },
234     { "getInputBuffer", "(I)Ljava/nio/ByteBuffer;", "android/media/MediaCodec", OFF(get_input_buffer), METHOD, false },
235     { "getOutputBuffers", "()[Ljava/nio/ByteBuffer;", "android/media/MediaCodec", OFF(get_output_buffers), METHOD, false },
236     { "getOutputBuffer", "(I)Ljava/nio/ByteBuffer;", "android/media/MediaCodec", OFF(get_output_buffer), METHOD, false },
237     { "dequeueInputBuffer", "(J)I", "android/media/MediaCodec", OFF(dequeue_input_buffer), METHOD, true },
238     { "dequeueOutputBuffer", "(Landroid/media/MediaCodec$BufferInfo;J)I", "android/media/MediaCodec", OFF(dequeue_output_buffer), METHOD, true },
239     { "queueInputBuffer", "(IIIJI)V", "android/media/MediaCodec", OFF(queue_input_buffer), METHOD, true },
240     { "releaseOutputBuffer", "(IZ)V", "android/media/MediaCodec", OFF(release_output_buffer), METHOD, true },
241
242     { "createVideoFormat", "(Ljava/lang/String;II)Landroid/media/MediaFormat;", "android/media/MediaFormat", OFF(create_video_format), STATIC_METHOD, true },
243     { "setInteger", "(Ljava/lang/String;I)V", "android/media/MediaFormat", OFF(set_integer), METHOD, true },
244     { "getInteger", "(Ljava/lang/String;)I", "android/media/MediaFormat", OFF(get_integer), METHOD, true },
245     { "setByteBuffer", "(Ljava/lang/String;Ljava/nio/ByteBuffer;)V", "android/media/MediaFormat", OFF(set_bytebuffer), METHOD, true },
246
247     { "<init>", "()V", "android/media/MediaCodec$BufferInfo", OFF(buffer_info_ctor), METHOD, true },
248     { "size", "I", "android/media/MediaCodec$BufferInfo", OFF(size_field), FIELD, true },
249     { "offset", "I", "android/media/MediaCodec$BufferInfo", OFF(offset_field), FIELD, true },
250     { "presentationTimeUs", "J", "android/media/MediaCodec$BufferInfo", OFF(pts_field), FIELD, true },
251
252     { "allocateDirect", "(I)Ljava/nio/ByteBuffer;", "java/nio/ByteBuffer", OFF(allocate_direct), STATIC_METHOD, true },
253     { "limit", "(I)Ljava/nio/Buffer;", "java/nio/ByteBuffer", OFF(limit), METHOD, true },
254
255     { NULL, NULL, NULL, 0, 0, false },
256 };
257
258 #define GET_INTEGER(obj, name) (*env)->CallIntMethod(env, obj, p_sys->get_integer, (*env)->NewStringUTF(env, name))
259
260 /*****************************************************************************
261  * Local prototypes
262  *****************************************************************************/
263 static int  OpenDecoder(vlc_object_t *);
264 static void CloseDecoder(vlc_object_t *);
265
266 static picture_t *DecodeVideo(decoder_t *, block_t **);
267
268 static void InvalidateAllPictures(decoder_t *);
269 static int InsertInflightPicture(decoder_t *, picture_t *, unsigned int );
270
271 /*****************************************************************************
272  * Module descriptor
273  *****************************************************************************/
274 #define DIRECTRENDERING_TEXT N_("Android direct rendering")
275 #define DIRECTRENDERING_LONGTEXT N_(\
276         "Enable Android direct rendering using opaque buffers.")
277
278 #define CFG_PREFIX "mediacodec-"
279
280 vlc_module_begin ()
281     set_description( N_("Video decoder using Android MediaCodec") )
282     set_category( CAT_INPUT )
283     set_subcategory( SUBCAT_INPUT_VCODEC )
284     set_section( N_("Decoding") , NULL )
285     set_capability( "decoder", 0 ) /* Only enabled via commandline arguments */
286     add_bool(CFG_PREFIX "dr", true,
287              DIRECTRENDERING_TEXT, DIRECTRENDERING_LONGTEXT, true)
288     set_callbacks( OpenDecoder, CloseDecoder )
289 vlc_module_end ()
290
291 static int jstrcmp(JNIEnv* env, jobject str, const char* str2)
292 {
293     jsize len = (*env)->GetStringUTFLength(env, str);
294     if (len != (jsize) strlen(str2))
295         return -1;
296     const char *ptr = (*env)->GetStringUTFChars(env, str, NULL);
297     int ret = memcmp(ptr, str2, len);
298     (*env)->ReleaseStringUTFChars(env, str, ptr);
299     return ret;
300 }
301
302 static inline bool check_exception( JNIEnv *env )
303 {
304     if ((*env)->ExceptionOccurred(env)) {
305         (*env)->ExceptionClear(env);
306         return true;
307     }
308     else
309         return false;
310 }
311 #define CHECK_EXCEPTION() check_exception( env )
312
313 static bool codec_is_blacklisted( const char *p_name, unsigned int i_name_len )
314 {
315      static const char *blacklisted_codecs[] = {
316         /* software decoders */
317         "OMX.google.",
318         /* crashes mediaserver */
319         "OMX.MTK.VIDEO.DECODER.MPEG4",
320         NULL,
321      };
322
323      for( const char **pp_bl_codecs = blacklisted_codecs; *pp_bl_codecs != NULL;
324           pp_bl_codecs++ )
325      {
326         if( !strncmp( p_name, *pp_bl_codecs,
327             __MIN( strlen(*pp_bl_codecs), i_name_len ) ) )
328             return true;
329      }
330      return false;
331 }
332
333 /*****************************************************************************
334  * OpenDecoder: Create the decoder instance
335  *****************************************************************************/
336 static int OpenDecoder(vlc_object_t *p_this)
337 {
338     decoder_t *p_dec = (decoder_t*)p_this;
339     decoder_sys_t *p_sys;
340
341     if (p_dec->fmt_in.i_cat != VIDEO_ES && !p_dec->b_force)
342         return VLC_EGENERIC;
343
344     const char *mime = NULL;
345     switch (p_dec->fmt_in.i_codec) {
346     case VLC_CODEC_HEVC: mime = "video/hevc"; break;
347     case VLC_CODEC_H264: mime = "video/avc"; break;
348     case VLC_CODEC_H263: mime = "video/3gpp"; break;
349     case VLC_CODEC_MP4V: mime = "video/mp4v-es"; break;
350     case VLC_CODEC_WMV3: mime = "video/x-ms-wmv"; break;
351     case VLC_CODEC_VC1:  mime = "video/wvc1"; break;
352     case VLC_CODEC_VP8:  mime = "video/x-vnd.on2.vp8"; break;
353     case VLC_CODEC_VP9:  mime = "video/x-vnd.on2.vp9"; break;
354     default:
355         msg_Dbg(p_dec, "codec %4.4s not supported", (char *)&p_dec->fmt_in.i_codec);
356         return VLC_EGENERIC;
357     }
358
359     size_t fmt_profile = 0;
360     if (p_dec->fmt_in.i_codec == VLC_CODEC_H264)
361         h264_get_profile_level(&p_dec->fmt_in, &fmt_profile, NULL, NULL);
362
363     /* Allocate the memory needed to store the decoder's structure */
364     if ((p_dec->p_sys = p_sys = calloc(1, sizeof(*p_sys))) == NULL)
365         return VLC_ENOMEM;
366
367     p_dec->pf_decode_video = DecodeVideo;
368
369     p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
370     p_dec->fmt_out.video = p_dec->fmt_in.video;
371     p_dec->fmt_out.audio = p_dec->fmt_in.audio;
372     p_dec->b_need_packetized = true;
373
374     JNIEnv* env = NULL;
375     jni_attach_thread(&env, THREAD_NAME);
376
377     for (int i = 0; classes[i].name; i++) {
378         *(jclass*)((uint8_t*)p_sys + classes[i].offset) =
379             (*env)->FindClass(env, classes[i].name);
380
381         if (CHECK_EXCEPTION()) {
382             msg_Warn(p_dec, "Unable to find class %s", classes[i].name);
383             goto error;
384         }
385     }
386
387     jclass last_class;
388     for (int i = 0; members[i].name; i++) {
389         if (i == 0 || strcmp(members[i].class, members[i - 1].class))
390             last_class = (*env)->FindClass(env, members[i].class);
391
392         if (CHECK_EXCEPTION()) {
393             msg_Warn(p_dec, "Unable to find class %s", members[i].class);
394             goto error;
395         }
396
397         switch (members[i].type) {
398         case METHOD:
399             *(jmethodID*)((uint8_t*)p_sys + members[i].offset) =
400                 (*env)->GetMethodID(env, last_class, members[i].name, members[i].sig);
401             break;
402         case STATIC_METHOD:
403             *(jmethodID*)((uint8_t*)p_sys + members[i].offset) =
404                 (*env)->GetStaticMethodID(env, last_class, members[i].name, members[i].sig);
405             break;
406         case FIELD:
407             *(jfieldID*)((uint8_t*)p_sys + members[i].offset) =
408                 (*env)->GetFieldID(env, last_class, members[i].name, members[i].sig);
409             break;
410         }
411         if (CHECK_EXCEPTION()) {
412             msg_Warn(p_dec, "Unable to find the member %s in %s",
413                      members[i].name, members[i].class);
414             if (members[i].critical)
415                 goto error;
416         }
417     }
418     /* getInputBuffers and getOutputBuffers are deprecated if API >= 21
419      * use getInputBuffer and getOutputBuffer instead. */
420     if (p_sys->get_input_buffer && p_sys->get_output_buffer) {
421         p_sys->get_output_buffers =
422         p_sys->get_input_buffers = NULL;
423     } else if (!p_sys->get_output_buffers && !p_sys->get_input_buffers) {
424         msg_Warn(p_dec, "Unable to find get Output/Input Buffer/Buffers");
425         goto error;
426     }
427
428     int num_codecs = (*env)->CallStaticIntMethod(env, p_sys->media_codec_list_class,
429                                                  p_sys->get_codec_count);
430     jobject codec_name = NULL;
431
432     for (int i = 0; i < num_codecs; i++) {
433         jobject codec_capabilities = NULL;
434         jobject profile_levels = NULL;
435         jobject info = NULL;
436         jobject name = NULL;
437         jobject types = NULL;
438         jsize name_len = 0;
439         int profile_levels_len = 0, num_types = 0;
440         const char *name_ptr = NULL;
441         bool found = false;
442
443         info = (*env)->CallStaticObjectMethod(env, p_sys->media_codec_list_class,
444                                               p_sys->get_codec_info_at, i);
445         if ((*env)->CallBooleanMethod(env, info, p_sys->is_encoder))
446             goto loopclean;
447
448         codec_capabilities = (*env)->CallObjectMethod(env, info, p_sys->get_capabilities_for_type,
449                                                       (*env)->NewStringUTF(env, mime));
450         if (CHECK_EXCEPTION()) {
451             msg_Warn(p_dec, "Exception occurred in MediaCodecInfo.getCapabilitiesForType");
452             goto loopclean;
453         } else if (codec_capabilities) {
454             profile_levels = (*env)->GetObjectField(env, codec_capabilities, p_sys->profile_levels_field);
455             if (profile_levels)
456                 profile_levels_len = (*env)->GetArrayLength(env, profile_levels);
457         }
458         msg_Dbg(p_dec, "Number of profile levels: %d", profile_levels_len);
459
460         types = (*env)->CallObjectMethod(env, info, p_sys->get_supported_types);
461         num_types = (*env)->GetArrayLength(env, types);
462         name = (*env)->CallObjectMethod(env, info, p_sys->get_name);
463         name_len = (*env)->GetStringUTFLength(env, name);
464         name_ptr = (*env)->GetStringUTFChars(env, name, NULL);
465         found = false;
466
467         if (codec_is_blacklisted( name_ptr, name_len))
468             goto loopclean;
469         for (int j = 0; j < num_types && !found; j++) {
470             jobject type = (*env)->GetObjectArrayElement(env, types, j);
471             if (!jstrcmp(env, type, mime)) {
472                 /* The mime type is matching for this component. We
473                    now check if the capabilities of the codec is
474                    matching the video format. */
475                 if (p_dec->fmt_in.i_codec == VLC_CODEC_H264 && fmt_profile) {
476                     /* This decoder doesn't expose its profiles and is high
477                      * profile capable */
478                     if (!strncmp(name_ptr, "OMX.LUMEVideoDecoder", __MIN(20, name_len)))
479                         found = true;
480
481                     for (int i = 0; i < profile_levels_len && !found; ++i) {
482                         jobject profile_level = (*env)->GetObjectArrayElement(env, profile_levels, i);
483
484                         int omx_profile = (*env)->GetIntField(env, profile_level, p_sys->profile_field);
485                         size_t codec_profile = convert_omx_to_profile_idc(omx_profile);
486                         (*env)->DeleteLocalRef(env, profile_level);
487                         if (codec_profile != fmt_profile)
488                             continue;
489                         /* Some encoders set the level too high, thus we ignore it for the moment.
490                            We could try to guess the actual profile based on the resolution. */
491                         found = true;
492                     }
493                 }
494                 else
495                     found = true;
496             }
497             (*env)->DeleteLocalRef(env, type);
498         }
499         if (found) {
500             msg_Dbg(p_dec, "using %.*s", name_len, name_ptr);
501             p_sys->name = malloc(name_len + 1);
502             memcpy(p_sys->name, name_ptr, name_len);
503             p_sys->name[name_len] = '\0';
504             codec_name = name;
505         }
506 loopclean:
507         if (name)
508             (*env)->ReleaseStringUTFChars(env, name, name_ptr);
509         if (profile_levels)
510             (*env)->DeleteLocalRef(env, profile_levels);
511         if (types)
512             (*env)->DeleteLocalRef(env, types);
513         if (codec_capabilities)
514             (*env)->DeleteLocalRef(env, codec_capabilities);
515         if (info)
516             (*env)->DeleteLocalRef(env, info);
517         if (found)
518             break;
519     }
520
521     if (!codec_name) {
522         msg_Dbg(p_dec, "No suitable codec matching %s was found", mime);
523         goto error;
524     }
525
526     // This method doesn't handle errors nicely, it crashes if the codec isn't found.
527     // (The same goes for createDecoderByType.) This is fixed in latest AOSP and in 4.2,
528     // but not in 4.1 devices.
529     p_sys->codec = (*env)->CallStaticObjectMethod(env, p_sys->media_codec_class,
530                                                   p_sys->create_by_codec_name, codec_name);
531     if (CHECK_EXCEPTION()) {
532         msg_Warn(p_dec, "Exception occurred in MediaCodec.createByCodecName.");
533         goto error;
534     }
535     p_sys->allocated = true;
536     p_sys->codec = (*env)->NewGlobalRef(env, p_sys->codec);
537
538     jobject format = (*env)->CallStaticObjectMethod(env, p_sys->media_format_class,
539                          p_sys->create_video_format, (*env)->NewStringUTF(env, mime),
540                          p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height);
541
542     if (p_dec->fmt_in.i_extra) {
543         // Allocate a byte buffer via allocateDirect in java instead of NewDirectByteBuffer,
544         // since the latter doesn't allocate storage of its own, and we don't know how long
545         // the codec uses the buffer.
546         int buf_size = p_dec->fmt_in.i_extra + 20;
547         jobject bytebuf = (*env)->CallStaticObjectMethod(env, p_sys->byte_buffer_class,
548                                                          p_sys->allocate_direct, buf_size);
549         uint32_t size = p_dec->fmt_in.i_extra;
550         uint8_t *ptr = (*env)->GetDirectBufferAddress(env, bytebuf);
551         if (p_dec->fmt_in.i_codec == VLC_CODEC_H264 && ((uint8_t*)p_dec->fmt_in.p_extra)[0] == 1) {
552             convert_sps_pps(p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
553                             ptr, buf_size,
554                             &size, &p_sys->nal_size);
555         } else if (p_dec->fmt_in.i_codec == VLC_CODEC_HEVC) {
556             convert_hevc_nal_units(p_dec, p_dec->fmt_in.p_extra,
557                                    p_dec->fmt_in.i_extra, ptr, buf_size,
558                                    &size, &p_sys->nal_size);
559         } else {
560             memcpy(ptr, p_dec->fmt_in.p_extra, size);
561         }
562         (*env)->CallObjectMethod(env, bytebuf, p_sys->limit, size);
563         (*env)->CallVoidMethod(env, format, p_sys->set_bytebuffer,
564                                (*env)->NewStringUTF(env, "csd-0"), bytebuf);
565         (*env)->DeleteLocalRef(env, bytebuf);
566     }
567
568     /* If the VideoPlayerActivity is not started, MediaCodec opaque
569        direct rendering should be disabled since no surface will be
570        attached to the JNI. */
571     p_sys->direct_rendering = jni_IsVideoPlayerActivityCreated() && var_InheritBool(p_dec, CFG_PREFIX "dr");
572
573     /* There is no way to rotate the video using direct rendering (and using a
574      * SurfaceView) before  API 21 (Lollipop). Therefore, we deactivate direct
575      * rendering if video doesn't have a normal rotation and if
576      * get_input_buffer method is not present (This method exists since API
577      * 21). */
578     if (p_sys->direct_rendering
579         && p_dec->fmt_in.video.orientation != ORIENT_NORMAL
580         && !p_sys->get_input_buffer)
581         p_sys->direct_rendering = false;
582
583     if (p_sys->direct_rendering) {
584         if (p_dec->fmt_in.video.orientation != ORIENT_NORMAL) {
585             int i_angle;
586
587             switch (p_dec->fmt_in.video.orientation) {
588                 case ORIENT_ROTATED_90:
589                     i_angle = 90;
590                     break;
591                 case ORIENT_ROTATED_180:
592                     i_angle = 180;
593                     break;
594                 case ORIENT_ROTATED_270:
595                     i_angle = 270;
596                     break;
597                 default:
598                     i_angle = 0;
599             }
600             (*env)->CallVoidMethod(env, format, p_sys->set_integer,
601                                    (*env)->NewStringUTF(env, "rotation-degrees"),
602                                    i_angle);
603         }
604
605         jobject surf = jni_LockAndGetAndroidJavaSurface();
606         if (surf) {
607             // Configure MediaCodec with the Android surface.
608             (*env)->CallVoidMethod(env, p_sys->codec, p_sys->configure, format, surf, NULL, 0);
609             if (CHECK_EXCEPTION()) {
610                 msg_Warn(p_dec, "Exception occurred in MediaCodec.configure with an output surface.");
611                 jni_UnlockAndroidSurface();
612                 goto error;
613             }
614             p_dec->fmt_out.i_codec = VLC_CODEC_ANDROID_OPAQUE;
615         } else {
616             msg_Warn(p_dec, "Failed to get the Android Surface, disabling direct rendering.");
617             p_sys->direct_rendering = false;
618         }
619         jni_UnlockAndroidSurface();
620     }
621     if (!p_sys->direct_rendering) {
622         (*env)->CallVoidMethod(env, p_sys->codec, p_sys->configure, format, NULL, NULL, 0);
623         if (CHECK_EXCEPTION()) {
624             msg_Warn(p_dec, "Exception occurred in MediaCodec.configure");
625             goto error;
626         }
627     }
628
629     (*env)->CallVoidMethod(env, p_sys->codec, p_sys->start);
630     if (CHECK_EXCEPTION()) {
631         msg_Warn(p_dec, "Exception occurred in MediaCodec.start");
632         goto error;
633     }
634     p_sys->started = true;
635
636     if (p_sys->get_input_buffers && p_sys->get_output_buffers) {
637         p_sys->input_buffers = (*env)->CallObjectMethod(env, p_sys->codec, p_sys->get_input_buffers);
638         if (CHECK_EXCEPTION()) {
639             msg_Err(p_dec, "Exception in MediaCodec.getInputBuffers (OpenDecoder)");
640             goto error;
641         }
642         p_sys->output_buffers = (*env)->CallObjectMethod(env, p_sys->codec, p_sys->get_output_buffers);
643         if (CHECK_EXCEPTION()) {
644             msg_Err(p_dec, "Exception in MediaCodec.getOutputBuffers (OpenDecoder)");
645             goto error;
646         }
647         p_sys->input_buffers = (*env)->NewGlobalRef(env, p_sys->input_buffers);
648         p_sys->output_buffers = (*env)->NewGlobalRef(env, p_sys->output_buffers);
649     }
650     p_sys->buffer_info = (*env)->NewObject(env, p_sys->buffer_info_class, p_sys->buffer_info_ctor);
651     p_sys->buffer_info = (*env)->NewGlobalRef(env, p_sys->buffer_info);
652     (*env)->DeleteLocalRef(env, format);
653
654     jni_detach_thread();
655
656     const int timestamp_fifo_size = 32;
657     p_sys->timestamp_fifo = timestamp_FifoNew(timestamp_fifo_size);
658     if (!p_sys->timestamp_fifo)
659         goto error;
660
661     return VLC_SUCCESS;
662
663  error:
664     jni_detach_thread();
665     CloseDecoder(p_this);
666     return VLC_EGENERIC;
667 }
668
669 static void CloseDecoder(vlc_object_t *p_this)
670 {
671     decoder_t *p_dec = (decoder_t *)p_this;
672     decoder_sys_t *p_sys = p_dec->p_sys;
673     JNIEnv *env = NULL;
674
675     if (!p_sys)
676         return;
677
678     /* Invalidate all pictures that are currently in flight in order
679      * to prevent the vout from using destroyed output buffers. */
680     if (p_sys->direct_rendering)
681         InvalidateAllPictures(p_dec);
682     jni_attach_thread(&env, THREAD_NAME);
683     if (p_sys->input_buffers)
684         (*env)->DeleteGlobalRef(env, p_sys->input_buffers);
685     if (p_sys->output_buffers)
686         (*env)->DeleteGlobalRef(env, p_sys->output_buffers);
687     if (p_sys->codec) {
688         if (p_sys->started)
689         {
690             (*env)->CallVoidMethod(env, p_sys->codec, p_sys->stop);
691             if (CHECK_EXCEPTION())
692                 msg_Err(p_dec, "Exception in MediaCodec.stop");
693         }
694         if (p_sys->allocated)
695         {
696             (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release);
697             if (CHECK_EXCEPTION())
698                 msg_Err(p_dec, "Exception in MediaCodec.release");
699         }
700         (*env)->DeleteGlobalRef(env, p_sys->codec);
701     }
702     if (p_sys->buffer_info)
703         (*env)->DeleteGlobalRef(env, p_sys->buffer_info);
704     jni_detach_thread();
705
706     free(p_sys->name);
707     ArchitectureSpecificCopyHooksDestroy(p_sys->pixel_format, &p_sys->architecture_specific_data);
708     free(p_sys->pp_inflight_pictures);
709     if (p_sys->timestamp_fifo)
710         timestamp_FifoRelease(p_sys->timestamp_fifo);
711     free(p_sys);
712 }
713
714 /*****************************************************************************
715  * ReleaseOutputBuffer
716  *****************************************************************************/
717 static int ReleaseOutputBuffer(decoder_t *p_dec, JNIEnv *env, int i_index,
718                                bool b_render)
719 {
720     decoder_sys_t *p_sys = p_dec->p_sys;
721
722     (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer,
723                            i_index, b_render);
724     if (CHECK_EXCEPTION()) {
725         msg_Err(p_dec, "Exception in MediaCodec.releaseOutputBuffer");
726         return -1;
727     }
728     return 0;
729 }
730
731 /*****************************************************************************
732  * vout callbacks
733  *****************************************************************************/
734 static void UnlockPicture(picture_t* p_pic, bool b_render)
735 {
736     picture_sys_t *p_picsys = p_pic->p_sys;
737     decoder_t *p_dec = p_picsys->priv.hw.p_dec;
738
739     if (!p_picsys->priv.hw.b_valid)
740         return;
741
742     vlc_mutex_lock(get_android_opaque_mutex());
743
744     /* Picture might have been invalidated while waiting on the mutex. */
745     if (!p_picsys->priv.hw.b_valid) {
746         vlc_mutex_unlock(get_android_opaque_mutex());
747         return;
748     }
749
750     uint32_t i_index = p_picsys->priv.hw.i_index;
751     InsertInflightPicture(p_dec, NULL, i_index);
752
753     /* Release the MediaCodec buffer. */
754     JNIEnv *env = NULL;
755     jni_attach_thread(&env, THREAD_NAME);
756     ReleaseOutputBuffer(p_dec, env, i_index, b_render);
757     jni_detach_thread();
758     p_picsys->priv.hw.b_valid = false;
759
760     vlc_mutex_unlock(get_android_opaque_mutex());
761 }
762
763 static void InvalidateAllPictures(decoder_t *p_dec)
764 {
765     decoder_sys_t *p_sys = p_dec->p_sys;
766
767     vlc_mutex_lock(get_android_opaque_mutex());
768
769     for (unsigned int i = 0; i < p_sys->i_inflight_pictures; ++i) {
770         picture_t *p_pic = p_sys->pp_inflight_pictures[i];
771         if (p_pic) {
772             p_pic->p_sys->priv.hw.b_valid = false;
773             p_sys->pp_inflight_pictures[i] = NULL;
774         }
775     }
776     vlc_mutex_unlock(get_android_opaque_mutex());
777 }
778
779 static int InsertInflightPicture(decoder_t *p_dec, picture_t *p_pic,
780                                  unsigned int i_index)
781 {
782     decoder_sys_t *p_sys = p_dec->p_sys;
783
784     if (i_index >= p_sys->i_inflight_pictures) {
785         picture_t **pp_pics = realloc(p_sys->pp_inflight_pictures,
786                                       (i_index + 1) * sizeof (picture_t *));
787         if (!pp_pics)
788             return -1;
789         if (i_index - p_sys->i_inflight_pictures > 0)
790             memset(&pp_pics[p_sys->i_inflight_pictures], 0,
791                    (i_index - p_sys->i_inflight_pictures) * sizeof (picture_t *));
792         p_sys->pp_inflight_pictures = pp_pics;
793         p_sys->i_inflight_pictures = i_index + 1;
794     }
795     p_sys->pp_inflight_pictures[i_index] = p_pic;
796     return 0;
797 }
798
799 static int PutInput(decoder_t *p_dec, JNIEnv *env, block_t *p_block, jlong timeout)
800 {
801     decoder_sys_t *p_sys = p_dec->p_sys;
802     int index;
803     jobject buf;
804     jsize size;
805     uint8_t *bufptr;
806     struct H264ConvertState convert_state = { 0, 0 };
807
808     index = (*env)->CallIntMethod(env, p_sys->codec,
809                                   p_sys->dequeue_input_buffer, timeout);
810     if (CHECK_EXCEPTION()) {
811         msg_Err(p_dec, "Exception occurred in MediaCodec.dequeueInputBuffer");
812         return -1;
813     }
814     if (index < 0)
815         return 0;
816
817     if (p_sys->get_input_buffers)
818         buf = (*env)->GetObjectArrayElement(env, p_sys->input_buffers, index);
819     else
820         buf = (*env)->CallObjectMethod(env, p_sys->codec, p_sys->get_input_buffer, index);
821     size = (*env)->GetDirectBufferCapacity(env, buf);
822     bufptr = (*env)->GetDirectBufferAddress(env, buf);
823     if (size < 0) {
824         msg_Err(p_dec, "Java buffer has invalid size");
825         return -1;
826     }
827     if ((size_t) size > p_block->i_buffer)
828         size = p_block->i_buffer;
829     memcpy(bufptr, p_block->p_buffer, size);
830
831     convert_h264_to_annexb(bufptr, size, p_sys->nal_size, &convert_state);
832
833     int64_t ts = p_block->i_pts;
834     if (!ts && p_block->i_dts)
835         ts = p_block->i_dts;
836     if (p_block->i_flags & BLOCK_FLAG_PREROLL )
837         p_sys->i_preroll_end = ts;
838     timestamp_FifoPut(p_sys->timestamp_fifo, p_block->i_pts ? VLC_TS_INVALID : p_block->i_dts);
839     (*env)->CallVoidMethod(env, p_sys->codec, p_sys->queue_input_buffer, index, 0, size, ts, 0);
840     (*env)->DeleteLocalRef(env, buf);
841     if (CHECK_EXCEPTION()) {
842         msg_Err(p_dec, "Exception in MediaCodec.queueInputBuffer");
843         return -1;
844     }
845     p_sys->decoded = true;
846
847     return 1;
848 }
849
850 static int GetOutput(decoder_t *p_dec, JNIEnv *env, picture_t *p_pic, jlong timeout)
851 {
852     decoder_sys_t *p_sys = p_dec->p_sys;
853     int index = (*env)->CallIntMethod(env, p_sys->codec, p_sys->dequeue_output_buffer,
854                                       p_sys->buffer_info, timeout);
855     if (CHECK_EXCEPTION()) {
856         msg_Err(p_dec, "Exception in MediaCodec.dequeueOutputBuffer (GetOutput)");
857         return -1;
858     }
859
860     if (index >= 0) {
861         int64_t i_buffer_pts;
862
863         if (!p_sys->pixel_format || !p_pic) {
864             msg_Warn(p_dec, "Buffers returned before output format is set, dropping frame");
865             return ReleaseOutputBuffer(p_dec, env, index, false);
866         }
867
868         i_buffer_pts = (*env)->GetLongField(env, p_sys->buffer_info, p_sys->pts_field);
869         if (i_buffer_pts <= p_sys->i_preroll_end)
870             return ReleaseOutputBuffer(p_dec, env, index, false);
871
872         /* If the oldest input block had no PTS, the timestamp
873          * of the frame returned by MediaCodec might be wrong
874          * so we overwrite it with the corresponding dts. */
875         int64_t forced_ts = timestamp_FifoGet(p_sys->timestamp_fifo);
876         if (forced_ts == VLC_TS_INVALID)
877             p_pic->date = i_buffer_pts;
878         else
879             p_pic->date = forced_ts;
880
881         if (p_sys->direct_rendering) {
882             picture_sys_t *p_picsys = p_pic->p_sys;
883             p_picsys->pf_lock_pic = NULL;
884             p_picsys->pf_unlock_pic = UnlockPicture;
885             p_picsys->priv.hw.p_dec = p_dec;
886             p_picsys->priv.hw.i_index = index;
887             p_picsys->priv.hw.b_valid = true;
888
889             vlc_mutex_lock(get_android_opaque_mutex());
890             InsertInflightPicture(p_dec, p_pic, index);
891             vlc_mutex_unlock(get_android_opaque_mutex());
892         } else {
893             jobject buf;
894             if (p_sys->get_output_buffers)
895                 buf = (*env)->GetObjectArrayElement(env, p_sys->output_buffers, index);
896             else
897                 buf = (*env)->CallObjectMethod(env, p_sys->codec,
898                                                p_sys->get_output_buffer, index);
899             //jsize buf_size = (*env)->GetDirectBufferCapacity(env, buf);
900             uint8_t *ptr = (*env)->GetDirectBufferAddress(env, buf);
901
902             //int size = (*env)->GetIntField(env, p_sys->buffer_info, p_sys->size_field);
903             int offset = (*env)->GetIntField(env, p_sys->buffer_info, p_sys->offset_field);
904             ptr += offset; // Check the size parameter as well
905
906             unsigned int chroma_div;
907             GetVlcChromaSizes(p_dec->fmt_out.i_codec, p_dec->fmt_out.video.i_width,
908                               p_dec->fmt_out.video.i_height, NULL, NULL, &chroma_div);
909             CopyOmxPicture(p_sys->pixel_format, p_pic, p_sys->slice_height, p_sys->stride,
910                            ptr, chroma_div, &p_sys->architecture_specific_data);
911             (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, index, false);
912
913             jthrowable exception = (*env)->ExceptionOccurred(env);
914             if (exception != NULL) {
915                 jclass illegalStateException = (*env)->FindClass(env, "java/lang/IllegalStateException");
916                 if((*env)->IsInstanceOf(env, exception, illegalStateException)) {
917                     msg_Err(p_dec, "Codec error (IllegalStateException) in MediaCodec.releaseOutputBuffer");
918                     (*env)->ExceptionClear(env);
919                     (*env)->DeleteLocalRef(env, illegalStateException);
920                     (*env)->DeleteLocalRef(env, buf);
921                     return -1;
922                 }
923             }
924             (*env)->DeleteLocalRef(env, buf);
925         }
926         return 1;
927     } else if (index == INFO_OUTPUT_BUFFERS_CHANGED) {
928         msg_Dbg(p_dec, "output buffers changed");
929         if (!p_sys->get_output_buffers)
930             return 0;
931         (*env)->DeleteGlobalRef(env, p_sys->output_buffers);
932
933         p_sys->output_buffers = (*env)->CallObjectMethod(env, p_sys->codec,
934                                                          p_sys->get_output_buffers);
935         if (CHECK_EXCEPTION()) {
936             msg_Err(p_dec, "Exception in MediaCodec.getOutputBuffer (GetOutput)");
937             p_sys->output_buffers = NULL;
938             return -1;
939         }
940
941         p_sys->output_buffers = (*env)->NewGlobalRef(env, p_sys->output_buffers);
942     } else if (index == INFO_OUTPUT_FORMAT_CHANGED) {
943         jobject format = (*env)->CallObjectMethod(env, p_sys->codec, p_sys->get_output_format);
944         if (CHECK_EXCEPTION()) {
945             msg_Err(p_dec, "Exception in MediaCodec.getOutputFormat (GetOutput)");
946             return -1;
947         }
948
949         jobject format_string = (*env)->CallObjectMethod(env, format, p_sys->tostring);
950
951         jsize format_len = (*env)->GetStringUTFLength(env, format_string);
952         const char *format_ptr = (*env)->GetStringUTFChars(env, format_string, NULL);
953         msg_Dbg(p_dec, "output format changed: %.*s", format_len, format_ptr);
954         (*env)->ReleaseStringUTFChars(env, format_string, format_ptr);
955
956         ArchitectureSpecificCopyHooksDestroy(p_sys->pixel_format, &p_sys->architecture_specific_data);
957
958         int width           = GET_INTEGER(format, "width");
959         int height          = GET_INTEGER(format, "height");
960         p_sys->stride       = GET_INTEGER(format, "stride");
961         p_sys->slice_height = GET_INTEGER(format, "slice-height");
962         p_sys->pixel_format = GET_INTEGER(format, "color-format");
963         int crop_left       = GET_INTEGER(format, "crop-left");
964         int crop_top        = GET_INTEGER(format, "crop-top");
965         int crop_right      = GET_INTEGER(format, "crop-right");
966         int crop_bottom     = GET_INTEGER(format, "crop-bottom");
967
968         const char *name = "unknown";
969         if (!p_sys->direct_rendering) {
970             if (!GetVlcChromaFormat(p_sys->pixel_format,
971                                     &p_dec->fmt_out.i_codec, &name)) {
972                 msg_Err(p_dec, "color-format not recognized");
973                 return -1;
974             }
975         }
976
977         msg_Err(p_dec, "output: %d %s, %dx%d stride %d %d, crop %d %d %d %d",
978                 p_sys->pixel_format, name, width, height, p_sys->stride, p_sys->slice_height,
979                 crop_left, crop_top, crop_right, crop_bottom);
980
981         p_dec->fmt_out.video.i_width = crop_right + 1 - crop_left;
982         p_dec->fmt_out.video.i_height = crop_bottom + 1 - crop_top;
983         if (p_dec->fmt_out.video.i_width <= 1
984             || p_dec->fmt_out.video.i_height <= 1) {
985             p_dec->fmt_out.video.i_width = width;
986             p_dec->fmt_out.video.i_height = height;
987         }
988         p_dec->fmt_out.video.i_visible_width = p_dec->fmt_out.video.i_width;
989         p_dec->fmt_out.video.i_visible_height = p_dec->fmt_out.video.i_height;
990
991         if (p_sys->stride <= 0)
992             p_sys->stride = width;
993         if (p_sys->slice_height <= 0)
994             p_sys->slice_height = height;
995         CHECK_EXCEPTION();
996
997         ArchitectureSpecificCopyHooks(p_dec, p_sys->pixel_format, p_sys->slice_height,
998                                       p_sys->stride, &p_sys->architecture_specific_data);
999         if (p_sys->pixel_format == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
1000             p_sys->slice_height -= crop_top/2;
1001         if (IgnoreOmxDecoderPadding(p_sys->name)) {
1002             p_sys->slice_height = 0;
1003             p_sys->stride = p_dec->fmt_out.video.i_width;
1004         }
1005     }
1006     return 0;
1007 }
1008
1009 static picture_t *DecodeVideo(decoder_t *p_dec, block_t **pp_block)
1010 {
1011     decoder_sys_t *p_sys = p_dec->p_sys;
1012     picture_t *p_pic = NULL;
1013     JNIEnv *env = NULL;
1014
1015     if (!pp_block || !*pp_block)
1016         return NULL;
1017
1018     if (p_sys->error_state)
1019         goto endclean;
1020
1021     jni_attach_thread(&env, THREAD_NAME);
1022     if (!env)
1023         goto endclean;
1024
1025     if ((*pp_block)->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED)) {
1026         block_Release(*pp_block);
1027         *pp_block = NULL;
1028         p_sys->i_preroll_end = 0;
1029         timestamp_FifoEmpty(p_sys->timestamp_fifo);
1030         if (p_sys->decoded) {
1031             /* Invalidate all pictures that are currently in flight
1032              * since flushing make all previous indices returned by
1033              * MediaCodec invalid. */
1034             if (p_sys->direct_rendering)
1035                 InvalidateAllPictures(p_dec);
1036
1037             (*env)->CallVoidMethod(env, p_sys->codec, p_sys->flush);
1038             if (CHECK_EXCEPTION()) {
1039                 msg_Warn(p_dec, "Exception occurred in MediaCodec.flush");
1040                 p_sys->error_state = true;
1041             }
1042         }
1043         p_sys->decoded = false;
1044         goto endclean;
1045     }
1046
1047     /* Use the aspect ratio provided by the input (ie read from packetizer).
1048      * Don't check the current value of the aspect ratio in fmt_out, since we
1049      * want to allow changes in it to propagate. */
1050     if (p_dec->fmt_in.video.i_sar_num != 0 && p_dec->fmt_in.video.i_sar_den != 0) {
1051         p_dec->fmt_out.video.i_sar_num = p_dec->fmt_in.video.i_sar_num;
1052         p_dec->fmt_out.video.i_sar_den = p_dec->fmt_in.video.i_sar_den;
1053     }
1054
1055     jlong timeout = 0;
1056     int i_output_ret = 0;
1057     int i_input_ret = 0;
1058     /* return when pp_block is processed or when we got an output pic */
1059     while (i_input_ret != 1 && i_output_ret != 1) {
1060         if (i_input_ret == 0) {
1061             i_input_ret = PutInput(p_dec, env, *pp_block, timeout);
1062             if (i_input_ret == 1) {
1063                 block_Release(*pp_block);
1064                 *pp_block = NULL;
1065             } else if (i_input_ret == -1) {
1066                 p_sys->error_state = true;
1067                 break;
1068             }
1069         }
1070
1071         if (i_output_ret == 0) {
1072             /* FIXME: A new picture shouldn't be created each time.
1073              * If decoder_NewPicture fails because the decoder is
1074              * flushing/exiting, GetOutput will either fail (or crash in
1075              * function of devices), or never return an output buffer. Indeed,
1076              * if the Decoder is flushing, MediaCodec can be stalled since the
1077              * input is waiting for the output or vice-versa.  Therefore, call
1078              * decoder_NewPicture before GetOutput as a safeguard. */
1079
1080             if (p_sys->pixel_format) {
1081                 p_pic = decoder_NewPicture(p_dec);
1082                 if (!p_pic) {
1083                     msg_Warn(p_dec, "NewPicture failed");
1084                     goto endclean;
1085                 }
1086             }
1087             i_output_ret = GetOutput(p_dec, env, p_pic, timeout);
1088             if (i_output_ret == -1) {
1089                 p_sys->error_state = true;
1090                 break;
1091             } else if (i_output_ret == 0 && p_pic) {
1092                 picture_Release(p_pic);
1093                 p_pic = NULL;
1094             }
1095         }
1096         timeout = 10 * 1000;
1097     }
1098
1099 endclean:
1100     if (p_sys->error_state) {
1101         if( pp_block && *pp_block )
1102         {
1103             block_Release(*pp_block);
1104             *pp_block = NULL;
1105         }
1106         if (p_pic)
1107             picture_Release(p_pic);
1108         p_pic = NULL;
1109
1110         if (!p_sys->error_event_sent) {
1111             /* Signal the error to the Java. */
1112             jni_EventHardwareAccelerationError();
1113             p_sys->error_event_sent = true;
1114         }
1115     }
1116     if (env != NULL)
1117         jni_detach_thread();
1118
1119     return p_pic;
1120 }