mediacodec: Use the input aspect ratio when sizing the direct rendering surface
[vlc.git] / 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 <OMX_Core.h>
41 #include <OMX_Component.h>
42 #include "omxil_utils.h"
43 #include "android_opaque.h"
44
45 #define INFO_OUTPUT_BUFFERS_CHANGED -3
46 #define INFO_OUTPUT_FORMAT_CHANGED  -2
47 #define INFO_TRY_AGAIN_LATER        -1
48
49 extern JavaVM *myVm;
50 /* JNI functions to get/set an Android Surface object. */
51 extern jobject jni_LockAndGetAndroidJavaSurface();
52 extern void jni_UnlockAndroidSurface();
53 extern void jni_SetAndroidSurfaceSizeEnv(JNIEnv *p_env, int width, int height, int visible_width, int visible_height, int sar_num, int sar_den);
54
55 struct decoder_sys_t
56 {
57     jclass media_codec_list_class, media_codec_class, media_format_class;
58     jclass buffer_info_class, byte_buffer_class;
59     jmethodID tostring;
60     jmethodID get_codec_count, get_codec_info_at, is_encoder;
61     jmethodID get_supported_types, get_name;
62     jmethodID create_by_codec_name, configure, start, stop, flush, release;
63     jmethodID get_output_format, get_input_buffers, get_output_buffers;
64     jmethodID dequeue_input_buffer, dequeue_output_buffer, queue_input_buffer;
65     jmethodID release_output_buffer;
66     jmethodID create_video_format, set_integer, set_bytebuffer, get_integer;
67     jmethodID buffer_info_ctor;
68     jmethodID allocate_direct, limit;
69     jfieldID size_field, offset_field, pts_field;
70
71     uint32_t nal_size;
72
73     jobject codec;
74     jobject buffer_info;
75     jobject input_buffers, output_buffers;
76     int pixel_format;
77     int stride, slice_height;
78     int crop_top, crop_left;
79     char *name;
80
81     bool started;
82     bool decoded;
83
84     ArchitectureSpecificCopyData architecture_specific_data;
85
86     /* Direct rendering members. */
87     bool direct_rendering;
88     int i_output_buffers; /**< number of MediaCodec output buffers */
89     picture_t** inflight_picture; /**< stores the inflight picture for each output buffer or NULL */
90 };
91
92 enum Types
93 {
94     METHOD, STATIC_METHOD, FIELD
95 };
96
97 #define OFF(x) offsetof(struct decoder_sys_t, x)
98 struct classname
99 {
100     const char *name;
101     int offset;
102 };
103 static const struct classname classes[] = {
104     { "android/media/MediaCodecList", OFF(media_codec_list_class) },
105     { "android/media/MediaCodec", OFF(media_codec_class) },
106     { "android/media/MediaFormat", OFF(media_format_class) },
107     { "android/media/MediaFormat", OFF(media_format_class) },
108     { "android/media/MediaCodec$BufferInfo", OFF(buffer_info_class) },
109     { "java/nio/ByteBuffer", OFF(byte_buffer_class) },
110     { NULL, 0 },
111 };
112
113 struct member
114 {
115     const char *name;
116     const char *sig;
117     const char *class;
118     int offset;
119     int type;
120 };
121 static const struct member members[] = {
122     { "toString", "()Ljava/lang/String;", "java/lang/Object", OFF(tostring), METHOD },
123
124     { "getCodecCount", "()I", "android/media/MediaCodecList", OFF(get_codec_count), STATIC_METHOD },
125     { "getCodecInfoAt", "(I)Landroid/media/MediaCodecInfo;", "android/media/MediaCodecList", OFF(get_codec_info_at), STATIC_METHOD },
126
127     { "isEncoder", "()Z", "android/media/MediaCodecInfo", OFF(is_encoder), METHOD },
128     { "getSupportedTypes", "()[Ljava/lang/String;", "android/media/MediaCodecInfo", OFF(get_supported_types), METHOD },
129     { "getName", "()Ljava/lang/String;", "android/media/MediaCodecInfo", OFF(get_name), METHOD },
130
131     { "createByCodecName", "(Ljava/lang/String;)Landroid/media/MediaCodec;", "android/media/MediaCodec", OFF(create_by_codec_name), STATIC_METHOD },
132     { "configure", "(Landroid/media/MediaFormat;Landroid/view/Surface;Landroid/media/MediaCrypto;I)V", "android/media/MediaCodec", OFF(configure), METHOD },
133     { "start", "()V", "android/media/MediaCodec", OFF(start), METHOD },
134     { "stop", "()V", "android/media/MediaCodec", OFF(stop), METHOD },
135     { "flush", "()V", "android/media/MediaCodec", OFF(flush), METHOD },
136     { "release", "()V", "android/media/MediaCodec", OFF(release), METHOD },
137     { "getOutputFormat", "()Landroid/media/MediaFormat;", "android/media/MediaCodec", OFF(get_output_format), METHOD },
138     { "getInputBuffers", "()[Ljava/nio/ByteBuffer;", "android/media/MediaCodec", OFF(get_input_buffers), METHOD },
139     { "getOutputBuffers", "()[Ljava/nio/ByteBuffer;", "android/media/MediaCodec", OFF(get_output_buffers), METHOD },
140     { "dequeueInputBuffer", "(J)I", "android/media/MediaCodec", OFF(dequeue_input_buffer), METHOD },
141     { "dequeueOutputBuffer", "(Landroid/media/MediaCodec$BufferInfo;J)I", "android/media/MediaCodec", OFF(dequeue_output_buffer), METHOD },
142     { "queueInputBuffer", "(IIIJI)V", "android/media/MediaCodec", OFF(queue_input_buffer), METHOD },
143     { "releaseOutputBuffer", "(IZ)V", "android/media/MediaCodec", OFF(release_output_buffer), METHOD },
144
145     { "createVideoFormat", "(Ljava/lang/String;II)Landroid/media/MediaFormat;", "android/media/MediaFormat", OFF(create_video_format), STATIC_METHOD },
146     { "setInteger", "(Ljava/lang/String;I)V", "android/media/MediaFormat", OFF(set_integer), METHOD },
147     { "getInteger", "(Ljava/lang/String;)I", "android/media/MediaFormat", OFF(get_integer), METHOD },
148     { "setByteBuffer", "(Ljava/lang/String;Ljava/nio/ByteBuffer;)V", "android/media/MediaFormat", OFF(set_bytebuffer), METHOD },
149
150     { "<init>", "()V", "android/media/MediaCodec$BufferInfo", OFF(buffer_info_ctor), METHOD },
151     { "size", "I", "android/media/MediaCodec$BufferInfo", OFF(size_field), FIELD },
152     { "offset", "I", "android/media/MediaCodec$BufferInfo", OFF(offset_field), FIELD },
153     { "presentationTimeUs", "J", "android/media/MediaCodec$BufferInfo", OFF(pts_field), FIELD },
154
155     { "allocateDirect", "(I)Ljava/nio/ByteBuffer;", "java/nio/ByteBuffer", OFF(allocate_direct), STATIC_METHOD },
156     { "limit", "(I)Ljava/nio/Buffer;", "java/nio/ByteBuffer", OFF(limit), METHOD },
157
158     { NULL, NULL, NULL, 0, 0 },
159 };
160
161 #define GET_INTEGER(obj, name) (*env)->CallIntMethod(env, obj, p_sys->get_integer, (*env)->NewStringUTF(env, name))
162
163 /*****************************************************************************
164  * Local prototypes
165  *****************************************************************************/
166 static int  OpenDecoder(vlc_object_t *);
167 static void CloseDecoder(vlc_object_t *);
168
169 static picture_t *DecodeVideo(decoder_t *, block_t **);
170
171 static void InvalidateAllPictures(decoder_t *);
172
173 /*****************************************************************************
174  * Module descriptor
175  *****************************************************************************/
176 #define DIRECTRENDERING_TEXT N_("Android direct rendering")
177 #define DIRECTRENDERING_LONGTEXT N_(\
178         "Enable Android direct rendering using opaque buffers.")
179
180 #define CFG_PREFIX "mediacodec-"
181
182 vlc_module_begin ()
183     set_description( N_("Video decoder using Android MediaCodec") )
184     set_category( CAT_INPUT )
185     set_subcategory( SUBCAT_INPUT_VCODEC )
186     set_section( N_("Decoding") , NULL )
187     set_capability( "decoder", 0 ) /* Only enabled via commandline arguments */
188     add_bool(CFG_PREFIX "dr", true,
189              DIRECTRENDERING_TEXT, DIRECTRENDERING_LONGTEXT, true)
190     set_callbacks( OpenDecoder, CloseDecoder )
191 vlc_module_end ()
192
193 static int jstrcmp(JNIEnv* env, jobject str, const char* str2)
194 {
195     jsize len = (*env)->GetStringUTFLength(env, str);
196     if (len != (jsize) strlen(str2))
197         return -1;
198     const char *ptr = (*env)->GetStringUTFChars(env, str, NULL);
199     int ret = memcmp(ptr, str2, len);
200     (*env)->ReleaseStringUTFChars(env, str, ptr);
201     return ret;
202 }
203
204 /*****************************************************************************
205  * OpenDecoder: Create the decoder instance
206  *****************************************************************************/
207 static int OpenDecoder(vlc_object_t *p_this)
208 {
209     decoder_t *p_dec = (decoder_t*)p_this;
210     decoder_sys_t *p_sys;
211
212     if (p_dec->fmt_in.i_cat != VIDEO_ES && !p_dec->b_force)
213         return VLC_EGENERIC;
214
215     const char *mime = NULL;
216     switch (p_dec->fmt_in.i_codec) {
217     case VLC_CODEC_H264: mime = "video/avc"; break;
218     case VLC_CODEC_H263: mime = "video/3gpp"; break;
219     case VLC_CODEC_MP4V: mime = "video/mp4v-es"; break;
220     case VLC_CODEC_VC1:  mime = "video/wvc1"; break;
221     case VLC_CODEC_VP8:  mime = "video/x-vnd.on2.vp8"; break;
222     default:
223         msg_Dbg(p_dec, "codec %d not supported", p_dec->fmt_in.i_codec);
224         return VLC_EGENERIC;
225     }
226     /* Allocate the memory needed to store the decoder's structure */
227     if ((p_dec->p_sys = p_sys = calloc(1, sizeof(*p_sys))) == NULL)
228         return VLC_ENOMEM;
229
230     p_dec->pf_decode_video = DecodeVideo;
231
232
233     p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
234     p_dec->fmt_out.video = p_dec->fmt_in.video;
235     p_dec->fmt_out.audio = p_dec->fmt_in.audio;
236     p_dec->b_need_packetized = true;
237
238     JNIEnv* env = NULL;
239     (*myVm)->AttachCurrentThread(myVm, &env, NULL);
240
241     for (int i = 0; classes[i].name; i++) {
242         *(jclass*)((uint8_t*)p_sys + classes[i].offset) =
243             (*env)->FindClass(env, classes[i].name);
244
245         if ((*env)->ExceptionOccurred(env)) {
246             msg_Warn(p_dec, "Unable to find class %s", classes[i].name);
247             (*env)->ExceptionClear(env);
248             goto error;
249         }
250     }
251
252     jclass last_class;
253     for (int i = 0; members[i].name; i++) {
254         if (i == 0 || strcmp(members[i].class, members[i - 1].class))
255             last_class = (*env)->FindClass(env, members[i].class);
256
257         if ((*env)->ExceptionOccurred(env)) {
258             msg_Warn(p_dec, "Unable to find class %s", members[i].class);
259             (*env)->ExceptionClear(env);
260             goto error;
261         }
262
263         switch (members[i].type) {
264         case METHOD:
265             *(jmethodID*)((uint8_t*)p_sys + members[i].offset) =
266                 (*env)->GetMethodID(env, last_class, members[i].name, members[i].sig);
267             break;
268         case STATIC_METHOD:
269             *(jmethodID*)((uint8_t*)p_sys + members[i].offset) =
270                 (*env)->GetStaticMethodID(env, last_class, members[i].name, members[i].sig);
271             break;
272         case FIELD:
273             *(jfieldID*)((uint8_t*)p_sys + members[i].offset) =
274                 (*env)->GetFieldID(env, last_class, members[i].name, members[i].sig);
275             break;
276         }
277         if ((*env)->ExceptionOccurred(env)) {
278             msg_Warn(p_dec, "Unable to find the member %s in %s",
279                      members[i].name, members[i].class);
280             (*env)->ExceptionClear(env);
281             goto error;
282         }
283     }
284
285     int num_codecs = (*env)->CallStaticIntMethod(env, p_sys->media_codec_list_class,
286                                                  p_sys->get_codec_count);
287     jobject codec_name = NULL;
288
289     for (int i = 0; i < num_codecs; i++) {
290         jobject info = (*env)->CallStaticObjectMethod(env, p_sys->media_codec_list_class,
291                                                       p_sys->get_codec_info_at, i);
292         if ((*env)->CallBooleanMethod(env, info, p_sys->is_encoder)) {
293             (*env)->DeleteLocalRef(env, info);
294             continue;
295         }
296         jobject types = (*env)->CallObjectMethod(env, info, p_sys->get_supported_types);
297         int num_types = (*env)->GetArrayLength(env, types);
298         bool found = false;
299         for (int j = 0; j < num_types && !found; j++) {
300             jobject type = (*env)->GetObjectArrayElement(env, types, j);
301             if (!jstrcmp(env, type, mime))
302                 found = true;
303             (*env)->DeleteLocalRef(env, type);
304         }
305         if (found) {
306             jobject name = (*env)->CallObjectMethod(env, info, p_sys->get_name);
307             jsize name_len = (*env)->GetStringUTFLength(env, name);
308             const char *name_ptr = (*env)->GetStringUTFChars(env, name, NULL);
309             msg_Dbg(p_dec, "using %.*s", name_len, name_ptr);
310             p_sys->name = malloc(name_len + 1);
311             memcpy(p_sys->name, name_ptr, name_len);
312             p_sys->name[name_len] = '\0';
313             (*env)->ReleaseStringUTFChars(env, name, name_ptr);
314             codec_name = name;
315             break;
316         }
317         (*env)->DeleteLocalRef(env, info);
318     }
319
320     if (!codec_name) {
321         msg_Dbg(p_dec, "No suitable codec matching %s was found", mime);
322         goto error;
323     }
324
325     // This method doesn't handle errors nicely, it crashes if the codec isn't found.
326     // (The same goes for createDecoderByType.) This is fixed in latest AOSP and in 4.2,
327     // but not in 4.1 devices.
328     p_sys->codec = (*env)->CallStaticObjectMethod(env, p_sys->media_codec_class,
329                                                   p_sys->create_by_codec_name, codec_name);
330     p_sys->codec = (*env)->NewGlobalRef(env, p_sys->codec);
331
332     jobject format = (*env)->CallStaticObjectMethod(env, p_sys->media_format_class,
333                          p_sys->create_video_format, (*env)->NewStringUTF(env, mime),
334                          p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height);
335
336     if (p_dec->fmt_in.i_extra) {
337         // Allocate a byte buffer via allocateDirect in java instead of NewDirectByteBuffer,
338         // since the latter doesn't allocate storage of its own, and we don't know how long
339         // the codec uses the buffer.
340         int buf_size = p_dec->fmt_in.i_extra + 20;
341         jobject bytebuf = (*env)->CallStaticObjectMethod(env, p_sys->byte_buffer_class,
342                                                          p_sys->allocate_direct, buf_size);
343         uint32_t size = p_dec->fmt_in.i_extra;
344         uint8_t *ptr = (*env)->GetDirectBufferAddress(env, bytebuf);
345         if (p_dec->fmt_in.i_codec == VLC_CODEC_H264 && ((uint8_t*)p_dec->fmt_in.p_extra)[0] == 1) {
346             convert_sps_pps(p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
347                             ptr, buf_size,
348                             &size, &p_sys->nal_size);
349         } else {
350             memcpy(ptr, p_dec->fmt_in.p_extra, size);
351         }
352         (*env)->CallObjectMethod(env, bytebuf, p_sys->limit, size);
353         (*env)->CallVoidMethod(env, format, p_sys->set_bytebuffer,
354                                (*env)->NewStringUTF(env, "csd-0"), bytebuf);
355         (*env)->DeleteLocalRef(env, bytebuf);
356     }
357
358     p_sys->direct_rendering = var_InheritBool(p_dec, CFG_PREFIX "dr");
359     if (p_sys->direct_rendering) {
360         jobject surf = jni_LockAndGetAndroidJavaSurface();
361         if (surf) {
362             // Configure MediaCodec with the Android surface.
363             (*env)->CallVoidMethod(env, p_sys->codec, p_sys->configure, format, surf, NULL, 0);
364             if ((*env)->ExceptionOccurred(env)) {
365                 msg_Warn(p_dec, "Exception occurred in MediaCodec.configure");
366                 (*env)->ExceptionClear(env);
367                 goto error;
368             }
369             p_dec->fmt_out.i_codec = VLC_CODEC_ANDROID_OPAQUE;
370         } else {
371             msg_Warn(p_dec, "Failed to get the Android Surface, disabling direct rendering.");
372             p_sys->direct_rendering = false;
373         }
374         jni_UnlockAndroidSurface();
375     }
376     if (!p_sys->direct_rendering) {
377         (*env)->CallVoidMethod(env, p_sys->codec, p_sys->configure, format, NULL, NULL, 0);
378         if ((*env)->ExceptionOccurred(env)) {
379             msg_Warn(p_dec, "Exception occurred in MediaCodec.configure");
380             (*env)->ExceptionClear(env);
381             goto error;
382         }
383     }
384
385     (*env)->CallVoidMethod(env, p_sys->codec, p_sys->start);
386     if ((*env)->ExceptionOccurred(env)) {
387         msg_Warn(p_dec, "Exception occurred in MediaCodec.start");
388         (*env)->ExceptionClear(env);
389         goto error;
390     }
391     p_sys->started = true;
392
393     p_sys->input_buffers = (*env)->CallObjectMethod(env, p_sys->codec, p_sys->get_input_buffers);
394     p_sys->output_buffers = (*env)->CallObjectMethod(env, p_sys->codec, p_sys->get_output_buffers);
395     p_sys->buffer_info = (*env)->NewObject(env, p_sys->buffer_info_class, p_sys->buffer_info_ctor);
396     p_sys->input_buffers = (*env)->NewGlobalRef(env, p_sys->input_buffers);
397     p_sys->output_buffers = (*env)->NewGlobalRef(env, p_sys->output_buffers);
398     p_sys->buffer_info = (*env)->NewGlobalRef(env, p_sys->buffer_info);
399     p_sys->i_output_buffers = (*env)->GetArrayLength(env, p_sys->output_buffers);
400     p_sys->inflight_picture = calloc(1, sizeof(picture_t*) * p_sys->i_output_buffers);
401     if (!p_sys->inflight_picture)
402         goto error;
403     (*env)->DeleteLocalRef(env, format);
404
405     (*myVm)->DetachCurrentThread(myVm);
406     return VLC_SUCCESS;
407
408  error:
409     (*myVm)->DetachCurrentThread(myVm);
410     CloseDecoder(p_this);
411     return VLC_EGENERIC;
412 }
413
414 static void CloseDecoder(vlc_object_t *p_this)
415 {
416     decoder_t *p_dec = (decoder_t *)p_this;
417     decoder_sys_t *p_sys = p_dec->p_sys;
418     JNIEnv *env = NULL;
419
420     if (!p_sys)
421         return;
422
423     /* Invalidate all pictures that are currently in flight in order
424      * to prevent the vout from using destroyed output buffers. */
425     if (p_sys->direct_rendering)
426         InvalidateAllPictures(p_dec);
427     (*myVm)->AttachCurrentThread(myVm, &env, NULL);
428     if (p_sys->input_buffers)
429         (*env)->DeleteGlobalRef(env, p_sys->input_buffers);
430     if (p_sys->output_buffers)
431         (*env)->DeleteGlobalRef(env, p_sys->output_buffers);
432     if (p_sys->codec) {
433         if (p_sys->started)
434             (*env)->CallVoidMethod(env, p_sys->codec, p_sys->stop);
435         (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release);
436         (*env)->DeleteGlobalRef(env, p_sys->codec);
437     }
438     if (p_sys->buffer_info)
439         (*env)->DeleteGlobalRef(env, p_sys->buffer_info);
440     (*myVm)->DetachCurrentThread(myVm);
441
442     free(p_sys->name);
443     ArchitectureSpecificCopyHooksDestroy(p_sys->pixel_format, &p_sys->architecture_specific_data);
444     free(p_sys->inflight_picture);
445     free(p_sys);
446 }
447
448 /*****************************************************************************
449  * vout callbacks
450  *****************************************************************************/
451 static void DisplayBuffer(picture_sys_t* p_picsys, bool b_render)
452 {
453     decoder_t *p_dec = p_picsys->p_dec;
454     decoder_sys_t *p_sys = p_dec->p_sys;
455
456     if (!p_picsys->b_valid)
457         return;
458
459     vlc_mutex_lock(get_android_opaque_mutex());
460
461     /* Picture might have been invalidated while waiting on the mutex. */
462     if (!p_picsys->b_valid)
463     {
464         vlc_mutex_unlock(get_android_opaque_mutex());
465         return;
466     }
467
468     uint32_t i_index = p_picsys->i_index;
469     p_sys->inflight_picture[i_index] = NULL;
470
471     /* Release the MediaCodec buffer. */
472     JNIEnv *env = NULL;
473     (*myVm)->AttachCurrentThread(myVm, &env, NULL);
474     (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, i_index, b_render);
475     if ((*env)->ExceptionOccurred(env)) {
476         msg_Err(p_dec, "Exception in MediaCodec.releaseOutputBuffer (DisplayBuffer)");
477         (*env)->ExceptionClear(env);
478     }
479
480     (*myVm)->DetachCurrentThread(myVm);
481     p_picsys->b_valid = false;
482
483     vlc_mutex_unlock(get_android_opaque_mutex());
484 }
485
486 static void UnlockCallback(picture_sys_t* p_picsys)
487 {
488     DisplayBuffer(p_picsys, false);
489 }
490
491 static void DisplayCallback(picture_sys_t* p_picsys)
492 {
493     DisplayBuffer(p_picsys, true);
494 }
495
496 static void InvalidateAllPictures(decoder_t *p_dec)
497 {
498     decoder_sys_t *p_sys = p_dec->p_sys;
499
500     vlc_mutex_lock(get_android_opaque_mutex());
501     for (int i = 0; i < p_sys->i_output_buffers; ++i)
502     {
503         picture_t *p_pic = p_sys->inflight_picture[i];
504         if (p_pic) {
505             p_pic->p_sys->b_valid = false;
506             p_sys->inflight_picture[i] = NULL;
507         }
508     }
509     vlc_mutex_unlock(get_android_opaque_mutex());
510 }
511
512 static void GetOutput(decoder_t *p_dec, JNIEnv *env, picture_t **pp_pic)
513 {
514     decoder_sys_t *p_sys = p_dec->p_sys;
515     while (1) {
516         int index = (*env)->CallIntMethod(env, p_sys->codec, p_sys->dequeue_output_buffer,
517                                           p_sys->buffer_info, (jlong) 0);
518         if (index >= 0) {
519             if (!p_sys->pixel_format) {
520                 msg_Warn(p_dec, "Buffers returned before output format is set, dropping frame");
521                 (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, index, false);
522                 continue;
523             }
524
525             if (!*pp_pic) {
526                 *pp_pic = decoder_NewPicture(p_dec);
527             } else if (p_sys->direct_rendering) {
528                 picture_t *p_pic = *pp_pic;
529                 picture_sys_t *p_picsys = p_pic->p_sys;
530                 int i_prev_index = p_picsys->i_index;
531                 (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, i_prev_index, false);
532
533                 // No need to lock here since the previous picture was not sent.
534                 p_sys->inflight_picture[i_prev_index] = NULL;
535             }
536             if (*pp_pic) {
537
538                 picture_t *p_pic = *pp_pic;
539                 // TODO: Use crop_top/crop_left as well? Or is that already taken into account?
540                 // On OMX_TI_COLOR_FormatYUV420PackedSemiPlanar the offset already incldues
541                 // the cropping, so the top/left cropping params should just be ignored.
542                 p_pic->date = (*env)->GetLongField(env, p_sys->buffer_info, p_sys->pts_field);
543                 if (p_sys->direct_rendering) {
544                     picture_sys_t *p_picsys = p_pic->p_sys;
545                     p_picsys->pf_display_callback = DisplayCallback;
546                     p_picsys->pf_unlock_callback = UnlockCallback;
547                     p_picsys->p_dec = p_dec;
548                     p_picsys->i_index = index;
549                     p_picsys->b_valid = true;
550
551                     p_sys->inflight_picture[index] = p_pic;
552                 } else {
553                     jobject buf = (*env)->GetObjectArrayElement(env, p_sys->output_buffers, index);
554                     jsize buf_size = (*env)->GetDirectBufferCapacity(env, buf);
555                     uint8_t *ptr = (*env)->GetDirectBufferAddress(env, buf);
556
557                     int size = (*env)->GetIntField(env, p_sys->buffer_info, p_sys->size_field);
558                     int offset = (*env)->GetIntField(env, p_sys->buffer_info, p_sys->offset_field);
559                     ptr += offset; // Check the size parameter as well
560
561                     unsigned int chroma_div;
562                     GetVlcChromaSizes(p_dec->fmt_out.i_codec, p_dec->fmt_out.video.i_width,
563                                       p_dec->fmt_out.video.i_height, NULL, NULL, &chroma_div);
564                     CopyOmxPicture(p_sys->pixel_format, p_pic, p_sys->slice_height, p_sys->stride,
565                                    ptr, chroma_div, &p_sys->architecture_specific_data);
566                     (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, index, false);
567
568                     jthrowable exception = (*env)->ExceptionOccurred(env);
569                     if(exception != NULL) {
570                         jclass illegalStateException = (*env)->FindClass(env, "java/lang/IllegalStateException");
571                         if((*env)->IsInstanceOf(env, exception, illegalStateException)) {
572                             msg_Err(p_dec, "Codec error (IllegalStateException) in MediaCodec.releaseOutputBuffer");
573                             (*env)->ExceptionClear(env);
574                             (*env)->DeleteLocalRef(env, illegalStateException);
575                         }
576                     }
577                     (*env)->DeleteLocalRef(env, buf);
578                 }
579             } else {
580                 msg_Warn(p_dec, "NewPicture failed");
581                 (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, index, false);
582             }
583
584             return;
585         } else if (index == INFO_OUTPUT_BUFFERS_CHANGED) {
586             msg_Dbg(p_dec, "output buffers changed");
587             (*env)->DeleteGlobalRef(env, p_sys->output_buffers);
588
589             p_sys->output_buffers = (*env)->CallObjectMethod(env, p_sys->codec,
590                                                              p_sys->get_output_buffers);
591             p_sys->output_buffers = (*env)->NewGlobalRef(env, p_sys->output_buffers);
592
593             vlc_mutex_lock(get_android_opaque_mutex());
594             free(p_sys->inflight_picture);
595             p_sys->i_output_buffers = (*env)->GetArrayLength(env, p_sys->output_buffers);
596             p_sys->inflight_picture = calloc(1, sizeof(picture_t*) * p_sys->i_output_buffers);
597             vlc_mutex_unlock(get_android_opaque_mutex());
598
599         } else if (index == INFO_OUTPUT_FORMAT_CHANGED) {
600
601             jobject format = (*env)->CallObjectMethod(env, p_sys->codec, p_sys->get_output_format);
602             jobject format_string = (*env)->CallObjectMethod(env, format, p_sys->tostring);
603
604             jsize format_len = (*env)->GetStringUTFLength(env, format_string);
605             const char *format_ptr = (*env)->GetStringUTFChars(env, format_string, NULL);
606             msg_Dbg(p_dec, "output format changed: %.*s", format_len, format_ptr);
607             (*env)->ReleaseStringUTFChars(env, format_string, format_ptr);
608
609             ArchitectureSpecificCopyHooksDestroy(p_sys->pixel_format, &p_sys->architecture_specific_data);
610
611             int width           = GET_INTEGER(format, "width");
612             int height          = GET_INTEGER(format, "height");
613             p_sys->stride       = GET_INTEGER(format, "stride");
614             p_sys->slice_height = GET_INTEGER(format, "slice-height");
615             p_sys->pixel_format = GET_INTEGER(format, "color-format");
616             p_sys->crop_left    = GET_INTEGER(format, "crop-left");
617             p_sys->crop_top     = GET_INTEGER(format, "crop-top");
618             int crop_right      = GET_INTEGER(format, "crop-right");
619             int crop_bottom     = GET_INTEGER(format, "crop-bottom");
620
621             const char *name = "unknown";
622             if (p_sys->direct_rendering) {
623                 int sar_num = 1, sar_den = 1;
624                 if (p_dec->fmt_in.video.i_sar_num != 0 && p_dec->fmt_in.video.i_sar_den != 0) {
625                     sar_num = p_dec->fmt_in.video.i_sar_num;
626                     sar_den = p_dec->fmt_in.video.i_sar_den;
627                 }
628                 jni_SetAndroidSurfaceSizeEnv(env, width, height, width, height, sar_num, sar_den);
629             } else
630                 GetVlcChromaFormat(p_sys->pixel_format, &p_dec->fmt_out.i_codec, &name);
631
632             msg_Dbg(p_dec, "output: %d %s, %dx%d stride %d %d, crop %d %d %d %d",
633                     p_sys->pixel_format, name, width, height, p_sys->stride, p_sys->slice_height,
634                     p_sys->crop_left, p_sys->crop_top, crop_right, crop_bottom);
635
636             p_dec->fmt_out.video.i_width = crop_right + 1 - p_sys->crop_left;
637             p_dec->fmt_out.video.i_height = crop_bottom + 1 - p_sys->crop_top;
638             if (p_sys->stride <= 0)
639                 p_sys->stride = width;
640             if (p_sys->slice_height <= 0)
641                 p_sys->slice_height = height;
642             if ((*env)->ExceptionOccurred(env))
643                 (*env)->ExceptionClear(env);
644
645             ArchitectureSpecificCopyHooks(p_dec, p_sys->pixel_format, p_sys->slice_height,
646                                           p_sys->stride, &p_sys->architecture_specific_data);
647             if (p_sys->pixel_format == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar) {
648                 p_sys->slice_height -= p_sys->crop_top/2;
649                 /* Reset crop top/left here, since the offset parameter already includes this.
650                  * If we'd ignore the offset parameter in the BufferInfo, we could just keep
651                  * the original slice height and apply the top/left cropping instead. */
652                 p_sys->crop_top = 0;
653                 p_sys->crop_left = 0;
654             }
655             if (IgnoreOmxDecoderPadding(p_sys->name)) {
656                 p_sys->slice_height = 0;
657                 p_sys->stride = p_dec->fmt_out.video.i_width;
658             }
659
660         } else {
661             return;
662         }
663     }
664 }
665
666 static picture_t *DecodeVideo(decoder_t *p_dec, block_t **pp_block)
667 {
668     decoder_sys_t *p_sys = p_dec->p_sys;
669     picture_t *p_pic = NULL;
670     JNIEnv *env = NULL;
671     struct H264ConvertState convert_state = { 0, 0 };
672
673     if (!pp_block || !*pp_block)
674         return NULL;
675
676     block_t *p_block = *pp_block;
677
678     (*myVm)->AttachCurrentThread(myVm, &env, NULL);
679
680     if (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED)) {
681         block_Release(p_block);
682         if (p_sys->decoded) {
683             /* Invalidate all pictures that are currently in flight
684              * since flushing make all previous indices returned by
685              * MediaCodec invalid. */
686             if (p_sys->direct_rendering)
687                 InvalidateAllPictures(p_dec);
688
689             (*env)->CallVoidMethod(env, p_sys->codec, p_sys->flush);
690             if ((*env)->ExceptionOccurred(env)) {
691                 msg_Warn(p_dec, "Exception occurred in MediaCodec.flush");
692                 (*env)->ExceptionClear(env);
693             }
694         }
695         p_sys->decoded = false;
696         (*myVm)->DetachCurrentThread(myVm);
697         return NULL;
698     }
699
700     /* Use the aspect ratio provided by the input (ie read from packetizer).
701      * Don't check the current value of the aspect ratio in fmt_out, since we
702      * want to allow changes in it to propagate. */
703     if (p_dec->fmt_in.video.i_sar_num != 0 && p_dec->fmt_in.video.i_sar_den != 0)
704     {
705         p_dec->fmt_out.video.i_sar_num = p_dec->fmt_in.video.i_sar_num;
706         p_dec->fmt_out.video.i_sar_den = p_dec->fmt_in.video.i_sar_den;
707     }
708
709     jlong timeout = 0;
710     const int max_polling_attempts = 50;
711     int attempts = 0;
712     while (true) {
713         int index = (*env)->CallIntMethod(env, p_sys->codec, p_sys->dequeue_input_buffer, timeout);
714         if (index < 0) {
715             GetOutput(p_dec, env, &p_pic);
716             if (p_pic) {
717                 /* If we couldn't get an available input buffer but a
718                  * decoded frame is available, we return the frame
719                  * without assigning NULL to *pp_block. The next call
720                  * to DecodeVideo will try to send the input packet again.
721                  */
722                 (*myVm)->DetachCurrentThread(myVm);
723                 return p_pic;
724             }
725             timeout = 30*1000;
726             ++attempts;
727             /* With opaque DR the output buffers are released by the
728                vout therefore we implement a timeout for polling in
729                order to avoid being indefinitely stalled in this loop. */
730             if (p_sys->direct_rendering && attempts == max_polling_attempts) {
731                 picture_t *invalid_picture = decoder_NewPicture(p_dec);
732                 if (invalid_picture) {
733                     invalid_picture->date = VLC_TS_INVALID;
734                     picture_sys_t *p_picsys = invalid_picture->p_sys;
735                     p_picsys->pf_display_callback = NULL;
736                     p_picsys->pf_unlock_callback = NULL;
737                     p_picsys->p_dec = NULL;
738                     p_picsys->i_index = -1;
739                     p_picsys->b_valid = false;
740                 }
741                 else {
742                     /* If we cannot return a picture we must free the
743                        block since the decoder will proceed with the
744                        next block. */
745                     block_Release(p_block);
746                     *pp_block = NULL;
747                 }
748                 (*myVm)->DetachCurrentThread(myVm);
749                 return invalid_picture;
750             }
751             continue;
752         }
753         jobject buf = (*env)->GetObjectArrayElement(env, p_sys->input_buffers, index);
754         jsize size = (*env)->GetDirectBufferCapacity(env, buf);
755         uint8_t *bufptr = (*env)->GetDirectBufferAddress(env, buf);
756         if (size > p_block->i_buffer)
757             size = p_block->i_buffer;
758         memcpy(bufptr, p_block->p_buffer, size);
759
760         convert_h264_to_annexb(bufptr, size, p_sys->nal_size, &convert_state);
761
762         int64_t ts = p_block->i_pts;
763         if (!ts && p_block->i_dts)
764             ts = p_block->i_dts;
765         (*env)->CallVoidMethod(env, p_sys->codec, p_sys->queue_input_buffer, index, 0, size, ts, 0);
766         (*env)->DeleteLocalRef(env, buf);
767         p_sys->decoded = true;
768         break;
769     }
770     if (!p_pic)
771         GetOutput(p_dec, env, &p_pic);
772     (*myVm)->DetachCurrentThread(myVm);
773
774     block_Release(p_block);
775     *pp_block = NULL;
776
777     return p_pic;
778 }