]> git.sesse.net Git - vlc/blob - modules/codec/omxil/android_mediacodec.c
bb27aa8cc17b5464de25d1c1173f292c748c413a
[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 <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 extern void jni_EventHardwareAccelerationError();
55 extern bool jni_IsVideoPlayerActivityCreated();
56
57 /* Implementation of a circular buffer of timestamps with overwriting
58  * of older values. MediaCodec has only one type of timestamp, if a
59  * block has no PTS, we send the DTS instead. Some hardware decoders
60  * cannot cope with this situation and output the frames in the wrong
61  * order. As a workaround in this case, we use a FIFO of timestamps in
62  * order to remember which input packets had no PTS.  Since an
63  * hardware decoder can silently drop frames, this might cause a
64  * growing desynchronization with the actual timestamp. Thus the
65  * circular buffer has a limited size and will overwrite older values.
66  */
67 typedef struct
68 {
69     uint32_t          begin;
70     uint32_t          size;
71     uint32_t          capacity;
72     int64_t           *buffer;
73 } timestamp_fifo_t;
74
75 static timestamp_fifo_t *timestamp_FifoNew(uint32_t capacity)
76 {
77     timestamp_fifo_t *fifo = calloc(1, sizeof(*fifo));
78     if (!fifo)
79         return NULL;
80     fifo->buffer = malloc(capacity * sizeof(*fifo->buffer));
81     if (!fifo->buffer) {
82         free(fifo);
83         return NULL;
84     }
85     fifo->capacity = capacity;
86     return fifo;
87 }
88
89 static void timestamp_FifoRelease(timestamp_fifo_t *fifo)
90 {
91     free(fifo->buffer);
92     free(fifo);
93 }
94
95 static bool timestamp_FifoIsEmpty(timestamp_fifo_t *fifo)
96 {
97     return fifo->size == 0;
98 }
99
100 static bool timestamp_FifoIsFull(timestamp_fifo_t *fifo)
101 {
102     return fifo->size == fifo->capacity;
103 }
104
105 static void timestamp_FifoEmpty(timestamp_fifo_t *fifo)
106 {
107     fifo->size = 0;
108 }
109
110 static void timestamp_FifoPut(timestamp_fifo_t *fifo, int64_t ts)
111 {
112     uint32_t end = (fifo->begin + fifo->size) % fifo->capacity;
113     fifo->buffer[end] = ts;
114     if (!timestamp_FifoIsFull(fifo))
115         fifo->size += 1;
116     else
117         fifo->begin = (fifo->begin + 1) % fifo->capacity;
118 }
119
120 static int64_t timestamp_FifoGet(timestamp_fifo_t *fifo)
121 {
122     if (timestamp_FifoIsEmpty(fifo))
123         return VLC_TS_INVALID;
124
125     int64_t result = fifo->buffer[fifo->begin];
126     fifo->begin = (fifo->begin + 1) % fifo->capacity;
127     fifo->size -= 1;
128     return result;
129 }
130
131 struct decoder_sys_t
132 {
133     jclass media_codec_list_class, media_codec_class, media_format_class;
134     jclass buffer_info_class, byte_buffer_class;
135     jmethodID tostring;
136     jmethodID get_codec_count, get_codec_info_at, is_encoder, get_capabilities_for_type;
137     jfieldID profile_levels_field, profile_field, level_field;
138     jmethodID get_supported_types, get_name;
139     jmethodID create_by_codec_name, configure, start, stop, flush, release;
140     jmethodID get_output_format, get_input_buffers, get_output_buffers;
141     jmethodID dequeue_input_buffer, dequeue_output_buffer, queue_input_buffer;
142     jmethodID release_output_buffer;
143     jmethodID create_video_format, set_integer, set_bytebuffer, get_integer;
144     jmethodID buffer_info_ctor;
145     jmethodID allocate_direct, limit;
146     jfieldID size_field, offset_field, pts_field;
147
148     uint32_t nal_size;
149
150     jobject codec;
151     jobject buffer_info;
152     jobject input_buffers, output_buffers;
153     int pixel_format;
154     int stride, slice_height;
155     int crop_top, crop_left;
156     char *name;
157
158     bool allocated;
159     bool started;
160     bool decoded;
161     bool error_state;
162     bool error_event_sent;
163
164     ArchitectureSpecificCopyData architecture_specific_data;
165
166     /* Direct rendering members. */
167     bool direct_rendering;
168     int i_output_buffers; /**< number of MediaCodec output buffers */
169     picture_t** inflight_picture; /**< stores the inflight picture for each output buffer or NULL */
170
171     timestamp_fifo_t *timestamp_fifo;
172 };
173
174 enum Types
175 {
176     METHOD, STATIC_METHOD, FIELD
177 };
178
179 #define OFF(x) offsetof(struct decoder_sys_t, x)
180 struct classname
181 {
182     const char *name;
183     int offset;
184 };
185 static const struct classname classes[] = {
186     { "android/media/MediaCodecList", OFF(media_codec_list_class) },
187     { "android/media/MediaCodec", OFF(media_codec_class) },
188     { "android/media/MediaFormat", OFF(media_format_class) },
189     { "android/media/MediaFormat", OFF(media_format_class) },
190     { "android/media/MediaCodec$BufferInfo", OFF(buffer_info_class) },
191     { "java/nio/ByteBuffer", OFF(byte_buffer_class) },
192     { NULL, 0 },
193 };
194
195 struct member
196 {
197     const char *name;
198     const char *sig;
199     const char *class;
200     int offset;
201     int type;
202 };
203 static const struct member members[] = {
204     { "toString", "()Ljava/lang/String;", "java/lang/Object", OFF(tostring), METHOD },
205
206     { "getCodecCount", "()I", "android/media/MediaCodecList", OFF(get_codec_count), STATIC_METHOD },
207     { "getCodecInfoAt", "(I)Landroid/media/MediaCodecInfo;", "android/media/MediaCodecList", OFF(get_codec_info_at), STATIC_METHOD },
208
209     { "isEncoder", "()Z", "android/media/MediaCodecInfo", OFF(is_encoder), METHOD },
210     { "getSupportedTypes", "()[Ljava/lang/String;", "android/media/MediaCodecInfo", OFF(get_supported_types), METHOD },
211     { "getName", "()Ljava/lang/String;", "android/media/MediaCodecInfo", OFF(get_name), METHOD },
212     { "getCapabilitiesForType", "(Ljava/lang/String;)Landroid/media/MediaCodecInfo$CodecCapabilities;", "android/media/MediaCodecInfo", OFF(get_capabilities_for_type), METHOD },
213
214     { "profileLevels", "[Landroid/media/MediaCodecInfo$CodecProfileLevel;", "android/media/MediaCodecInfo$CodecCapabilities", OFF(profile_levels_field), FIELD },
215     { "profile", "I", "android/media/MediaCodecInfo$CodecProfileLevel", OFF(profile_field), FIELD },
216     { "level", "I", "android/media/MediaCodecInfo$CodecProfileLevel", OFF(level_field), FIELD },
217
218     { "createByCodecName", "(Ljava/lang/String;)Landroid/media/MediaCodec;", "android/media/MediaCodec", OFF(create_by_codec_name), STATIC_METHOD },
219     { "configure", "(Landroid/media/MediaFormat;Landroid/view/Surface;Landroid/media/MediaCrypto;I)V", "android/media/MediaCodec", OFF(configure), METHOD },
220     { "start", "()V", "android/media/MediaCodec", OFF(start), METHOD },
221     { "stop", "()V", "android/media/MediaCodec", OFF(stop), METHOD },
222     { "flush", "()V", "android/media/MediaCodec", OFF(flush), METHOD },
223     { "release", "()V", "android/media/MediaCodec", OFF(release), METHOD },
224     { "getOutputFormat", "()Landroid/media/MediaFormat;", "android/media/MediaCodec", OFF(get_output_format), METHOD },
225     { "getInputBuffers", "()[Ljava/nio/ByteBuffer;", "android/media/MediaCodec", OFF(get_input_buffers), METHOD },
226     { "getOutputBuffers", "()[Ljava/nio/ByteBuffer;", "android/media/MediaCodec", OFF(get_output_buffers), METHOD },
227     { "dequeueInputBuffer", "(J)I", "android/media/MediaCodec", OFF(dequeue_input_buffer), METHOD },
228     { "dequeueOutputBuffer", "(Landroid/media/MediaCodec$BufferInfo;J)I", "android/media/MediaCodec", OFF(dequeue_output_buffer), METHOD },
229     { "queueInputBuffer", "(IIIJI)V", "android/media/MediaCodec", OFF(queue_input_buffer), METHOD },
230     { "releaseOutputBuffer", "(IZ)V", "android/media/MediaCodec", OFF(release_output_buffer), METHOD },
231
232     { "createVideoFormat", "(Ljava/lang/String;II)Landroid/media/MediaFormat;", "android/media/MediaFormat", OFF(create_video_format), STATIC_METHOD },
233     { "setInteger", "(Ljava/lang/String;I)V", "android/media/MediaFormat", OFF(set_integer), METHOD },
234     { "getInteger", "(Ljava/lang/String;)I", "android/media/MediaFormat", OFF(get_integer), METHOD },
235     { "setByteBuffer", "(Ljava/lang/String;Ljava/nio/ByteBuffer;)V", "android/media/MediaFormat", OFF(set_bytebuffer), METHOD },
236
237     { "<init>", "()V", "android/media/MediaCodec$BufferInfo", OFF(buffer_info_ctor), METHOD },
238     { "size", "I", "android/media/MediaCodec$BufferInfo", OFF(size_field), FIELD },
239     { "offset", "I", "android/media/MediaCodec$BufferInfo", OFF(offset_field), FIELD },
240     { "presentationTimeUs", "J", "android/media/MediaCodec$BufferInfo", OFF(pts_field), FIELD },
241
242     { "allocateDirect", "(I)Ljava/nio/ByteBuffer;", "java/nio/ByteBuffer", OFF(allocate_direct), STATIC_METHOD },
243     { "limit", "(I)Ljava/nio/Buffer;", "java/nio/ByteBuffer", OFF(limit), METHOD },
244
245     { NULL, NULL, NULL, 0, 0 },
246 };
247
248 #define GET_INTEGER(obj, name) (*env)->CallIntMethod(env, obj, p_sys->get_integer, (*env)->NewStringUTF(env, name))
249
250 /*****************************************************************************
251  * Local prototypes
252  *****************************************************************************/
253 static int  OpenDecoder(vlc_object_t *);
254 static void CloseDecoder(vlc_object_t *);
255
256 static picture_t *DecodeVideo(decoder_t *, block_t **);
257
258 static void InvalidateAllPictures(decoder_t *);
259
260 /*****************************************************************************
261  * Module descriptor
262  *****************************************************************************/
263 #define DIRECTRENDERING_TEXT N_("Android direct rendering")
264 #define DIRECTRENDERING_LONGTEXT N_(\
265         "Enable Android direct rendering using opaque buffers.")
266
267 #define CFG_PREFIX "mediacodec-"
268
269 vlc_module_begin ()
270     set_description( N_("Video decoder using Android MediaCodec") )
271     set_category( CAT_INPUT )
272     set_subcategory( SUBCAT_INPUT_VCODEC )
273     set_section( N_("Decoding") , NULL )
274     set_capability( "decoder", 0 ) /* Only enabled via commandline arguments */
275     add_bool(CFG_PREFIX "dr", true,
276              DIRECTRENDERING_TEXT, DIRECTRENDERING_LONGTEXT, true)
277     set_callbacks( OpenDecoder, CloseDecoder )
278 vlc_module_end ()
279
280 static int jstrcmp(JNIEnv* env, jobject str, const char* str2)
281 {
282     jsize len = (*env)->GetStringUTFLength(env, str);
283     if (len != (jsize) strlen(str2))
284         return -1;
285     const char *ptr = (*env)->GetStringUTFChars(env, str, NULL);
286     int ret = memcmp(ptr, str2, len);
287     (*env)->ReleaseStringUTFChars(env, str, ptr);
288     return ret;
289 }
290
291 /*****************************************************************************
292  * OpenDecoder: Create the decoder instance
293  *****************************************************************************/
294 static int OpenDecoder(vlc_object_t *p_this)
295 {
296     decoder_t *p_dec = (decoder_t*)p_this;
297     decoder_sys_t *p_sys;
298
299     if (p_dec->fmt_in.i_cat != VIDEO_ES && !p_dec->b_force)
300         return VLC_EGENERIC;
301
302     const char *mime = NULL;
303     switch (p_dec->fmt_in.i_codec) {
304     case VLC_CODEC_H264: mime = "video/avc"; break;
305     case VLC_CODEC_H263: mime = "video/3gpp"; break;
306     case VLC_CODEC_MP4V: mime = "video/mp4v-es"; break;
307     case VLC_CODEC_VC1:  mime = "video/wvc1"; break;
308     case VLC_CODEC_VP8:  mime = "video/x-vnd.on2.vp8"; break;
309     default:
310         msg_Dbg(p_dec, "codec %d not supported", p_dec->fmt_in.i_codec);
311         return VLC_EGENERIC;
312     }
313
314     size_t fmt_profile = 0;
315     if (p_dec->fmt_in.i_codec == VLC_CODEC_H264)
316         h264_get_profile_level(&p_dec->fmt_in, &fmt_profile, NULL, NULL);
317
318     /* Allocate the memory needed to store the decoder's structure */
319     if ((p_dec->p_sys = p_sys = calloc(1, sizeof(*p_sys))) == NULL)
320         return VLC_ENOMEM;
321
322     p_dec->pf_decode_video = DecodeVideo;
323
324     p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
325     p_dec->fmt_out.video = p_dec->fmt_in.video;
326     p_dec->fmt_out.audio = p_dec->fmt_in.audio;
327     p_dec->b_need_packetized = true;
328
329     JNIEnv* env = NULL;
330     (*myVm)->AttachCurrentThread(myVm, &env, NULL);
331
332     for (int i = 0; classes[i].name; i++) {
333         *(jclass*)((uint8_t*)p_sys + classes[i].offset) =
334             (*env)->FindClass(env, classes[i].name);
335
336         if ((*env)->ExceptionOccurred(env)) {
337             msg_Warn(p_dec, "Unable to find class %s", classes[i].name);
338             (*env)->ExceptionClear(env);
339             goto error;
340         }
341     }
342
343     jclass last_class;
344     for (int i = 0; members[i].name; i++) {
345         if (i == 0 || strcmp(members[i].class, members[i - 1].class))
346             last_class = (*env)->FindClass(env, members[i].class);
347
348         if ((*env)->ExceptionOccurred(env)) {
349             msg_Warn(p_dec, "Unable to find class %s", members[i].class);
350             (*env)->ExceptionClear(env);
351             goto error;
352         }
353
354         switch (members[i].type) {
355         case METHOD:
356             *(jmethodID*)((uint8_t*)p_sys + members[i].offset) =
357                 (*env)->GetMethodID(env, last_class, members[i].name, members[i].sig);
358             break;
359         case STATIC_METHOD:
360             *(jmethodID*)((uint8_t*)p_sys + members[i].offset) =
361                 (*env)->GetStaticMethodID(env, last_class, members[i].name, members[i].sig);
362             break;
363         case FIELD:
364             *(jfieldID*)((uint8_t*)p_sys + members[i].offset) =
365                 (*env)->GetFieldID(env, last_class, members[i].name, members[i].sig);
366             break;
367         }
368         if ((*env)->ExceptionOccurred(env)) {
369             msg_Warn(p_dec, "Unable to find the member %s in %s",
370                      members[i].name, members[i].class);
371             (*env)->ExceptionClear(env);
372             goto error;
373         }
374     }
375
376     int num_codecs = (*env)->CallStaticIntMethod(env, p_sys->media_codec_list_class,
377                                                  p_sys->get_codec_count);
378     jobject codec_name = NULL;
379
380     for (int i = 0; i < num_codecs; i++) {
381         jobject info = (*env)->CallStaticObjectMethod(env, p_sys->media_codec_list_class,
382                                                       p_sys->get_codec_info_at, i);
383         if ((*env)->CallBooleanMethod(env, info, p_sys->is_encoder)) {
384             (*env)->DeleteLocalRef(env, info);
385             continue;
386         }
387
388         jobject codec_capabilities = (*env)->CallObjectMethod(env, info, p_sys->get_capabilities_for_type,
389                                                               (*env)->NewStringUTF(env, mime));
390         jobject profile_levels = NULL;
391         int profile_levels_len = 0;
392         if ((*env)->ExceptionOccurred(env)) {
393             msg_Warn(p_dec, "Exception occurred in MediaCodecInfo.getCapabilitiesForType");
394             (*env)->ExceptionClear(env);
395             break;
396         } else if (codec_capabilities) {
397             profile_levels = (*env)->GetObjectField(env, codec_capabilities, p_sys->profile_levels_field);
398             if (profile_levels)
399                 profile_levels_len = (*env)->GetArrayLength(env, profile_levels);
400         }
401         msg_Dbg(p_dec, "Number of profile levels: %d", profile_levels_len);
402
403         jobject types = (*env)->CallObjectMethod(env, info, p_sys->get_supported_types);
404         int num_types = (*env)->GetArrayLength(env, types);
405         bool found = false;
406         for (int j = 0; j < num_types && !found; j++) {
407             jobject type = (*env)->GetObjectArrayElement(env, types, j);
408             if (!jstrcmp(env, type, mime)) {
409                 /* The mime type is matching for this component. We
410                    now check if the capabilities of the codec is
411                    matching the video format. */
412                 if (p_dec->fmt_in.i_codec == VLC_CODEC_H264 && fmt_profile) {
413                     for (int i = 0; i < profile_levels_len && !found; ++i) {
414                         jobject profile_level = (*env)->GetObjectArrayElement(env, profile_levels, i);
415
416                         int omx_profile = (*env)->GetIntField(env, profile_level, p_sys->profile_field);
417                         size_t codec_profile = convert_omx_to_profile_idc(omx_profile);
418                         if (codec_profile != fmt_profile)
419                             continue;
420                         /* Some encoders set the level too high, thus we ignore it for the moment.
421                            We could try to guess the actual profile based on the resolution. */
422                         found = true;
423                     }
424                 }
425                 else
426                     found = true;
427             }
428             (*env)->DeleteLocalRef(env, type);
429         }
430         if (found) {
431             jobject name = (*env)->CallObjectMethod(env, info, p_sys->get_name);
432             jsize name_len = (*env)->GetStringUTFLength(env, name);
433             const char *name_ptr = (*env)->GetStringUTFChars(env, name, NULL);
434             msg_Dbg(p_dec, "using %.*s", name_len, name_ptr);
435             p_sys->name = malloc(name_len + 1);
436             memcpy(p_sys->name, name_ptr, name_len);
437             p_sys->name[name_len] = '\0';
438             (*env)->ReleaseStringUTFChars(env, name, name_ptr);
439             codec_name = name;
440             break;
441         }
442         (*env)->DeleteLocalRef(env, info);
443     }
444
445     if (!codec_name) {
446         msg_Dbg(p_dec, "No suitable codec matching %s was found", mime);
447         goto error;
448     }
449
450     // This method doesn't handle errors nicely, it crashes if the codec isn't found.
451     // (The same goes for createDecoderByType.) This is fixed in latest AOSP and in 4.2,
452     // but not in 4.1 devices.
453     p_sys->codec = (*env)->CallStaticObjectMethod(env, p_sys->media_codec_class,
454                                                   p_sys->create_by_codec_name, codec_name);
455     if ((*env)->ExceptionOccurred(env)) {
456         msg_Warn(p_dec, "Exception occurred in MediaCodec.createByCodecName.");
457         (*env)->ExceptionClear(env);
458         goto error;
459     }
460     p_sys->allocated = true;
461     p_sys->codec = (*env)->NewGlobalRef(env, p_sys->codec);
462
463     jobject format = (*env)->CallStaticObjectMethod(env, p_sys->media_format_class,
464                          p_sys->create_video_format, (*env)->NewStringUTF(env, mime),
465                          p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height);
466
467     if (p_dec->fmt_in.i_extra) {
468         // Allocate a byte buffer via allocateDirect in java instead of NewDirectByteBuffer,
469         // since the latter doesn't allocate storage of its own, and we don't know how long
470         // the codec uses the buffer.
471         int buf_size = p_dec->fmt_in.i_extra + 20;
472         jobject bytebuf = (*env)->CallStaticObjectMethod(env, p_sys->byte_buffer_class,
473                                                          p_sys->allocate_direct, buf_size);
474         uint32_t size = p_dec->fmt_in.i_extra;
475         uint8_t *ptr = (*env)->GetDirectBufferAddress(env, bytebuf);
476         if (p_dec->fmt_in.i_codec == VLC_CODEC_H264 && ((uint8_t*)p_dec->fmt_in.p_extra)[0] == 1) {
477             convert_sps_pps(p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
478                             ptr, buf_size,
479                             &size, &p_sys->nal_size);
480         } else {
481             memcpy(ptr, p_dec->fmt_in.p_extra, size);
482         }
483         (*env)->CallObjectMethod(env, bytebuf, p_sys->limit, size);
484         (*env)->CallVoidMethod(env, format, p_sys->set_bytebuffer,
485                                (*env)->NewStringUTF(env, "csd-0"), bytebuf);
486         (*env)->DeleteLocalRef(env, bytebuf);
487     }
488
489     /* If the VideoPlayerActivity is not started, MediaCodec opaque
490        direct rendering should be disabled since no surface will be
491        attached to the JNI. */
492     p_sys->direct_rendering = jni_IsVideoPlayerActivityCreated() && var_InheritBool(p_dec, CFG_PREFIX "dr");
493     if (p_sys->direct_rendering) {
494         jobject surf = jni_LockAndGetAndroidJavaSurface();
495         if (surf) {
496             // Configure MediaCodec with the Android surface.
497             (*env)->CallVoidMethod(env, p_sys->codec, p_sys->configure, format, surf, NULL, 0);
498             if ((*env)->ExceptionOccurred(env)) {
499                 msg_Warn(p_dec, "Exception occurred in MediaCodec.configure with an output surface.");
500                 (*env)->ExceptionClear(env);
501                 jni_UnlockAndroidSurface();
502                 goto error;
503             }
504             p_dec->fmt_out.i_codec = VLC_CODEC_ANDROID_OPAQUE;
505         } else {
506             msg_Warn(p_dec, "Failed to get the Android Surface, disabling direct rendering.");
507             p_sys->direct_rendering = false;
508         }
509         jni_UnlockAndroidSurface();
510     }
511     if (!p_sys->direct_rendering) {
512         (*env)->CallVoidMethod(env, p_sys->codec, p_sys->configure, format, NULL, NULL, 0);
513         if ((*env)->ExceptionOccurred(env)) {
514             msg_Warn(p_dec, "Exception occurred in MediaCodec.configure");
515             (*env)->ExceptionClear(env);
516             goto error;
517         }
518     }
519
520     (*env)->CallVoidMethod(env, p_sys->codec, p_sys->start);
521     if ((*env)->ExceptionOccurred(env)) {
522         msg_Warn(p_dec, "Exception occurred in MediaCodec.start");
523         (*env)->ExceptionClear(env);
524         goto error;
525     }
526     p_sys->started = true;
527
528     p_sys->input_buffers = (*env)->CallObjectMethod(env, p_sys->codec, p_sys->get_input_buffers);
529     p_sys->output_buffers = (*env)->CallObjectMethod(env, p_sys->codec, p_sys->get_output_buffers);
530     p_sys->buffer_info = (*env)->NewObject(env, p_sys->buffer_info_class, p_sys->buffer_info_ctor);
531     p_sys->input_buffers = (*env)->NewGlobalRef(env, p_sys->input_buffers);
532     p_sys->output_buffers = (*env)->NewGlobalRef(env, p_sys->output_buffers);
533     p_sys->buffer_info = (*env)->NewGlobalRef(env, p_sys->buffer_info);
534     p_sys->i_output_buffers = (*env)->GetArrayLength(env, p_sys->output_buffers);
535     p_sys->inflight_picture = calloc(1, sizeof(picture_t*) * p_sys->i_output_buffers);
536     if (!p_sys->inflight_picture)
537         goto error;
538     (*env)->DeleteLocalRef(env, format);
539
540     (*myVm)->DetachCurrentThread(myVm);
541
542     const int timestamp_fifo_size = 32;
543     p_sys->timestamp_fifo = timestamp_FifoNew(timestamp_fifo_size);
544     if (!p_sys->timestamp_fifo)
545         goto error;
546
547     return VLC_SUCCESS;
548
549  error:
550     (*myVm)->DetachCurrentThread(myVm);
551     CloseDecoder(p_this);
552     return VLC_EGENERIC;
553 }
554
555 static void CloseDecoder(vlc_object_t *p_this)
556 {
557     decoder_t *p_dec = (decoder_t *)p_this;
558     decoder_sys_t *p_sys = p_dec->p_sys;
559     JNIEnv *env = NULL;
560
561     if (!p_sys)
562         return;
563
564     /* Invalidate all pictures that are currently in flight in order
565      * to prevent the vout from using destroyed output buffers. */
566     if (p_sys->direct_rendering)
567         InvalidateAllPictures(p_dec);
568     (*myVm)->AttachCurrentThread(myVm, &env, NULL);
569     if (p_sys->input_buffers)
570         (*env)->DeleteGlobalRef(env, p_sys->input_buffers);
571     if (p_sys->output_buffers)
572         (*env)->DeleteGlobalRef(env, p_sys->output_buffers);
573     if (p_sys->codec) {
574         if (p_sys->started)
575         {
576             (*env)->CallVoidMethod(env, p_sys->codec, p_sys->stop);
577             if ((*env)->ExceptionOccurred(env)) {
578                 msg_Err(p_dec, "Exception in MediaCodec.stop");
579                 (*env)->ExceptionClear(env);
580             }
581         }
582         if (p_sys->allocated)
583         {
584             (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release);
585             if ((*env)->ExceptionOccurred(env)) {
586                 msg_Err(p_dec, "Exception in MediaCodec.release");
587                 (*env)->ExceptionClear(env);
588             }
589         }
590         (*env)->DeleteGlobalRef(env, p_sys->codec);
591     }
592     if (p_sys->buffer_info)
593         (*env)->DeleteGlobalRef(env, p_sys->buffer_info);
594     (*myVm)->DetachCurrentThread(myVm);
595
596     free(p_sys->name);
597     ArchitectureSpecificCopyHooksDestroy(p_sys->pixel_format, &p_sys->architecture_specific_data);
598     free(p_sys->inflight_picture);
599     if (p_sys->timestamp_fifo)
600         timestamp_FifoRelease(p_sys->timestamp_fifo);
601     free(p_sys);
602 }
603
604 /*****************************************************************************
605  * vout callbacks
606  *****************************************************************************/
607 static void DisplayBuffer(picture_sys_t* p_picsys, bool b_render)
608 {
609     decoder_t *p_dec = p_picsys->p_dec;
610     decoder_sys_t *p_sys = p_dec->p_sys;
611
612     if (!p_picsys->b_valid)
613         return;
614
615     vlc_mutex_lock(get_android_opaque_mutex());
616
617     /* Picture might have been invalidated while waiting on the mutex. */
618     if (!p_picsys->b_valid) {
619         vlc_mutex_unlock(get_android_opaque_mutex());
620         return;
621     }
622
623     uint32_t i_index = p_picsys->i_index;
624     p_sys->inflight_picture[i_index] = NULL;
625
626     /* Release the MediaCodec buffer. */
627     JNIEnv *env = NULL;
628     (*myVm)->AttachCurrentThread(myVm, &env, NULL);
629     (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, i_index, b_render);
630     if ((*env)->ExceptionOccurred(env)) {
631         msg_Err(p_dec, "Exception in MediaCodec.releaseOutputBuffer (DisplayBuffer)");
632         (*env)->ExceptionClear(env);
633     }
634
635     (*myVm)->DetachCurrentThread(myVm);
636     p_picsys->b_valid = false;
637
638     vlc_mutex_unlock(get_android_opaque_mutex());
639 }
640
641 static void UnlockCallback(picture_sys_t* p_picsys)
642 {
643     DisplayBuffer(p_picsys, false);
644 }
645
646 static void DisplayCallback(picture_sys_t* p_picsys)
647 {
648     DisplayBuffer(p_picsys, true);
649 }
650
651 static void InvalidateAllPictures(decoder_t *p_dec)
652 {
653     decoder_sys_t *p_sys = p_dec->p_sys;
654
655     vlc_mutex_lock(get_android_opaque_mutex());
656     for (int i = 0; i < p_sys->i_output_buffers; ++i) {
657         picture_t *p_pic = p_sys->inflight_picture[i];
658         if (p_pic) {
659             p_pic->p_sys->b_valid = false;
660             p_sys->inflight_picture[i] = NULL;
661         }
662     }
663     vlc_mutex_unlock(get_android_opaque_mutex());
664 }
665
666 static void GetOutput(decoder_t *p_dec, JNIEnv *env, picture_t **pp_pic, jlong timeout)
667 {
668     decoder_sys_t *p_sys = p_dec->p_sys;
669     while (1) {
670         int index = (*env)->CallIntMethod(env, p_sys->codec, p_sys->dequeue_output_buffer,
671                                           p_sys->buffer_info, timeout);
672         if ((*env)->ExceptionOccurred(env)) {
673             msg_Err(p_dec, "Exception in MediaCodec.dequeueOutputBuffer (GetOutput)");
674             (*env)->ExceptionClear(env);
675             p_sys->error_state = true;
676             return;
677         }
678
679         if (index >= 0) {
680             if (!p_sys->pixel_format) {
681                 msg_Warn(p_dec, "Buffers returned before output format is set, dropping frame");
682                 (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, index, false);
683                 if ((*env)->ExceptionOccurred(env)) {
684                     msg_Err(p_dec, "Exception in MediaCodec.releaseOutputBuffer");
685                     (*env)->ExceptionClear(env);
686                     p_sys->error_state = true;
687                     return;
688                 }
689                 continue;
690             }
691
692             if (!*pp_pic) {
693                 *pp_pic = decoder_NewPicture(p_dec);
694             } else if (p_sys->direct_rendering) {
695                 picture_t *p_pic = *pp_pic;
696                 picture_sys_t *p_picsys = p_pic->p_sys;
697                 int i_prev_index = p_picsys->i_index;
698                 (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, i_prev_index, false);
699                 if ((*env)->ExceptionOccurred(env)) {
700                     msg_Err(p_dec, "Exception in MediaCodec.releaseOutputBuffer " \
701                             "(GetOutput, overwriting previous picture)");
702                     (*env)->ExceptionClear(env);
703                     p_sys->error_state = true;
704                     return;
705                 }
706
707                 // No need to lock here since the previous picture was not sent.
708                 p_sys->inflight_picture[i_prev_index] = NULL;
709             }
710             if (*pp_pic) {
711
712                 picture_t *p_pic = *pp_pic;
713                 // TODO: Use crop_top/crop_left as well? Or is that already taken into account?
714                 // On OMX_TI_COLOR_FormatYUV420PackedSemiPlanar the offset already incldues
715                 // the cropping, so the top/left cropping params should just be ignored.
716
717                 /* If the oldest input block had no PTS, the timestamp
718                  * of the frame returned by MediaCodec might be wrong
719                  * so we overwrite it with the corresponding dts. */
720                 int64_t forced_ts = timestamp_FifoGet(p_sys->timestamp_fifo);
721                 if (forced_ts == VLC_TS_INVALID)
722                     p_pic->date = (*env)->GetLongField(env, p_sys->buffer_info, p_sys->pts_field);
723                 else
724                     p_pic->date = forced_ts;
725
726                 if (p_sys->direct_rendering) {
727                     picture_sys_t *p_picsys = p_pic->p_sys;
728                     p_picsys->pf_display_callback = DisplayCallback;
729                     p_picsys->pf_unlock_callback = UnlockCallback;
730                     p_picsys->p_dec = p_dec;
731                     p_picsys->i_index = index;
732                     p_picsys->b_valid = true;
733
734                     p_sys->inflight_picture[index] = p_pic;
735                 } else {
736                     jobject buf = (*env)->GetObjectArrayElement(env, p_sys->output_buffers, index);
737                     jsize buf_size = (*env)->GetDirectBufferCapacity(env, buf);
738                     uint8_t *ptr = (*env)->GetDirectBufferAddress(env, buf);
739
740                     int size = (*env)->GetIntField(env, p_sys->buffer_info, p_sys->size_field);
741                     int offset = (*env)->GetIntField(env, p_sys->buffer_info, p_sys->offset_field);
742                     ptr += offset; // Check the size parameter as well
743
744                     unsigned int chroma_div;
745                     GetVlcChromaSizes(p_dec->fmt_out.i_codec, p_dec->fmt_out.video.i_width,
746                                       p_dec->fmt_out.video.i_height, NULL, NULL, &chroma_div);
747                     CopyOmxPicture(p_sys->pixel_format, p_pic, p_sys->slice_height, p_sys->stride,
748                                    ptr, chroma_div, &p_sys->architecture_specific_data);
749                     (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, index, false);
750
751                     jthrowable exception = (*env)->ExceptionOccurred(env);
752                     if (exception != NULL) {
753                         jclass illegalStateException = (*env)->FindClass(env, "java/lang/IllegalStateException");
754                         if((*env)->IsInstanceOf(env, exception, illegalStateException)) {
755                             msg_Err(p_dec, "Codec error (IllegalStateException) in MediaCodec.releaseOutputBuffer");
756                             (*env)->ExceptionClear(env);
757                             (*env)->DeleteLocalRef(env, illegalStateException);
758                             p_sys->error_state = true;
759                         }
760                     }
761                     (*env)->DeleteLocalRef(env, buf);
762                 }
763             } else {
764                 msg_Warn(p_dec, "NewPicture failed");
765                 (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, index, false);
766                 if ((*env)->ExceptionOccurred(env)) {
767                     msg_Err(p_dec, "Exception in MediaCodec.releaseOutputBuffer (GetOutput)");
768                     (*env)->ExceptionClear(env);
769                     p_sys->error_state = true;
770                 }
771             }
772             return;
773
774         } else if (index == INFO_OUTPUT_BUFFERS_CHANGED) {
775             msg_Dbg(p_dec, "output buffers changed");
776             (*env)->DeleteGlobalRef(env, p_sys->output_buffers);
777
778             p_sys->output_buffers = (*env)->CallObjectMethod(env, p_sys->codec,
779                                                              p_sys->get_output_buffers);
780             if ((*env)->ExceptionOccurred(env)) {
781                 msg_Err(p_dec, "Exception in MediaCodec.getOutputBuffer (GetOutput)");
782                 (*env)->ExceptionClear(env);
783                 p_sys->output_buffers = NULL;
784                 p_sys->error_state = true;
785                 return;
786             }
787
788             p_sys->output_buffers = (*env)->NewGlobalRef(env, p_sys->output_buffers);
789
790             vlc_mutex_lock(get_android_opaque_mutex());
791             free(p_sys->inflight_picture);
792             p_sys->i_output_buffers = (*env)->GetArrayLength(env, p_sys->output_buffers);
793             p_sys->inflight_picture = calloc(1, sizeof(picture_t*) * p_sys->i_output_buffers);
794             vlc_mutex_unlock(get_android_opaque_mutex());
795
796         } else if (index == INFO_OUTPUT_FORMAT_CHANGED) {
797             jobject format = (*env)->CallObjectMethod(env, p_sys->codec, p_sys->get_output_format);
798             if ((*env)->ExceptionOccurred(env)) {
799                 msg_Err(p_dec, "Exception in MediaCodec.getOutputFormat (GetOutput)");
800                 (*env)->ExceptionClear(env);
801                 p_sys->error_state = true;
802                 return;
803             }
804
805             jobject format_string = (*env)->CallObjectMethod(env, format, p_sys->tostring);
806
807             jsize format_len = (*env)->GetStringUTFLength(env, format_string);
808             const char *format_ptr = (*env)->GetStringUTFChars(env, format_string, NULL);
809             msg_Dbg(p_dec, "output format changed: %.*s", format_len, format_ptr);
810             (*env)->ReleaseStringUTFChars(env, format_string, format_ptr);
811
812             ArchitectureSpecificCopyHooksDestroy(p_sys->pixel_format, &p_sys->architecture_specific_data);
813
814             int width           = GET_INTEGER(format, "width");
815             int height          = GET_INTEGER(format, "height");
816             p_sys->stride       = GET_INTEGER(format, "stride");
817             p_sys->slice_height = GET_INTEGER(format, "slice-height");
818             p_sys->pixel_format = GET_INTEGER(format, "color-format");
819             p_sys->crop_left    = GET_INTEGER(format, "crop-left");
820             p_sys->crop_top     = GET_INTEGER(format, "crop-top");
821             int crop_right      = GET_INTEGER(format, "crop-right");
822             int crop_bottom     = GET_INTEGER(format, "crop-bottom");
823
824             const char *name = "unknown";
825             if (p_sys->direct_rendering) {
826                 int sar_num = 1, sar_den = 1;
827                 if (p_dec->fmt_in.video.i_sar_num != 0 && p_dec->fmt_in.video.i_sar_den != 0) {
828                     sar_num = p_dec->fmt_in.video.i_sar_num;
829                     sar_den = p_dec->fmt_in.video.i_sar_den;
830                 }
831                 jni_SetAndroidSurfaceSizeEnv(env, width, height, width, height, sar_num, sar_den);
832             } else
833                 GetVlcChromaFormat(p_sys->pixel_format, &p_dec->fmt_out.i_codec, &name);
834
835             msg_Dbg(p_dec, "output: %d %s, %dx%d stride %d %d, crop %d %d %d %d",
836                     p_sys->pixel_format, name, width, height, p_sys->stride, p_sys->slice_height,
837                     p_sys->crop_left, p_sys->crop_top, crop_right, crop_bottom);
838
839             p_dec->fmt_out.video.i_width = crop_right + 1 - p_sys->crop_left;
840             p_dec->fmt_out.video.i_height = crop_bottom + 1 - p_sys->crop_top;
841             if (p_sys->stride <= 0)
842                 p_sys->stride = width;
843             if (p_sys->slice_height <= 0)
844                 p_sys->slice_height = height;
845             if ((*env)->ExceptionOccurred(env))
846                 (*env)->ExceptionClear(env);
847
848             ArchitectureSpecificCopyHooks(p_dec, p_sys->pixel_format, p_sys->slice_height,
849                                           p_sys->stride, &p_sys->architecture_specific_data);
850             if (p_sys->pixel_format == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar) {
851                 p_sys->slice_height -= p_sys->crop_top/2;
852                 /* Reset crop top/left here, since the offset parameter already includes this.
853                  * If we'd ignore the offset parameter in the BufferInfo, we could just keep
854                  * the original slice height and apply the top/left cropping instead. */
855                 p_sys->crop_top = 0;
856                 p_sys->crop_left = 0;
857             }
858             if (IgnoreOmxDecoderPadding(p_sys->name)) {
859                 p_sys->slice_height = 0;
860                 p_sys->stride = p_dec->fmt_out.video.i_width;
861             }
862
863         } else {
864             return;
865         }
866     }
867 }
868
869 static picture_t *DecodeVideo(decoder_t *p_dec, block_t **pp_block)
870 {
871     decoder_sys_t *p_sys = p_dec->p_sys;
872     picture_t *p_pic = NULL;
873     JNIEnv *env = NULL;
874     struct H264ConvertState convert_state = { 0, 0 };
875
876     if (!pp_block || !*pp_block)
877         return NULL;
878
879     block_t *p_block = *pp_block;
880
881     if (p_sys->error_state) {
882         block_Release(p_block);
883         if (!p_sys->error_event_sent) {
884             /* Signal the error to the Java. */
885             jni_EventHardwareAccelerationError();
886             p_sys->error_event_sent = true;
887         }
888         return NULL;
889     }
890
891     (*myVm)->AttachCurrentThread(myVm, &env, NULL);
892
893     if (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED)) {
894         block_Release(p_block);
895         timestamp_FifoEmpty(p_sys->timestamp_fifo);
896         if (p_sys->decoded) {
897             /* Invalidate all pictures that are currently in flight
898              * since flushing make all previous indices returned by
899              * MediaCodec invalid. */
900             if (p_sys->direct_rendering)
901                 InvalidateAllPictures(p_dec);
902
903             (*env)->CallVoidMethod(env, p_sys->codec, p_sys->flush);
904             if ((*env)->ExceptionOccurred(env)) {
905                 msg_Warn(p_dec, "Exception occurred in MediaCodec.flush");
906                 (*env)->ExceptionClear(env);
907                 p_sys->error_state = true;
908             }
909         }
910         p_sys->decoded = false;
911         (*myVm)->DetachCurrentThread(myVm);
912         return NULL;
913     }
914
915     /* Use the aspect ratio provided by the input (ie read from packetizer).
916      * Don't check the current value of the aspect ratio in fmt_out, since we
917      * want to allow changes in it to propagate. */
918     if (p_dec->fmt_in.video.i_sar_num != 0 && p_dec->fmt_in.video.i_sar_den != 0) {
919         p_dec->fmt_out.video.i_sar_num = p_dec->fmt_in.video.i_sar_num;
920         p_dec->fmt_out.video.i_sar_den = p_dec->fmt_in.video.i_sar_den;
921     }
922
923     jlong timeout = 0;
924     const int max_polling_attempts = 50;
925     int attempts = 0;
926     while (true) {
927         int index = (*env)->CallIntMethod(env, p_sys->codec, p_sys->dequeue_input_buffer, (jlong) 0);
928         if ((*env)->ExceptionOccurred(env)) {
929             msg_Err(p_dec, "Exception occurred in MediaCodec.dequeueInputBuffer");
930             (*env)->ExceptionClear(env);
931             p_sys->error_state = true;
932             break;
933         }
934
935         if (index < 0) {
936             GetOutput(p_dec, env, &p_pic, timeout);
937             if (p_pic) {
938                 /* If we couldn't get an available input buffer but a
939                  * decoded frame is available, we return the frame
940                  * without assigning NULL to *pp_block. The next call
941                  * to DecodeVideo will try to send the input packet again.
942                  */
943                 (*myVm)->DetachCurrentThread(myVm);
944                 return p_pic;
945             }
946             timeout = 30 * 1000;
947             ++attempts;
948             /* With opaque DR the output buffers are released by the
949                vout therefore we implement a timeout for polling in
950                order to avoid being indefinitely stalled in this loop. */
951             if (p_sys->direct_rendering && attempts == max_polling_attempts) {
952                 picture_t *invalid_picture = decoder_NewPicture(p_dec);
953                 if (invalid_picture) {
954                     invalid_picture->date = VLC_TS_INVALID;
955                     picture_sys_t *p_picsys = invalid_picture->p_sys;
956                     p_picsys->pf_display_callback = NULL;
957                     p_picsys->pf_unlock_callback = NULL;
958                     p_picsys->p_dec = NULL;
959                     p_picsys->i_index = -1;
960                     p_picsys->b_valid = false;
961                 }
962                 else {
963                     /* If we cannot return a picture we must free the
964                        block since the decoder will proceed with the
965                        next block. */
966                     block_Release(p_block);
967                     *pp_block = NULL;
968                 }
969                 (*myVm)->DetachCurrentThread(myVm);
970                 return invalid_picture;
971             }
972             continue;
973         }
974
975         jobject buf = (*env)->GetObjectArrayElement(env, p_sys->input_buffers, index);
976         jsize size = (*env)->GetDirectBufferCapacity(env, buf);
977         uint8_t *bufptr = (*env)->GetDirectBufferAddress(env, buf);
978         if (size > p_block->i_buffer)
979             size = p_block->i_buffer;
980         memcpy(bufptr, p_block->p_buffer, size);
981
982         convert_h264_to_annexb(bufptr, size, p_sys->nal_size, &convert_state);
983
984         int64_t ts = p_block->i_pts;
985         if (!ts && p_block->i_dts)
986             ts = p_block->i_dts;
987         timestamp_FifoPut(p_sys->timestamp_fifo, p_block->i_pts ? VLC_TS_INVALID : p_block->i_dts);
988         (*env)->CallVoidMethod(env, p_sys->codec, p_sys->queue_input_buffer, index, 0, size, ts, 0);
989         (*env)->DeleteLocalRef(env, buf);
990         if ((*env)->ExceptionOccurred(env)) {
991             msg_Err(p_dec, "Exception in MediaCodec.queueInputBuffer");
992             (*env)->ExceptionClear(env);
993             p_sys->error_state = true;
994             break;
995         }
996         p_sys->decoded = true;
997         break;
998     }
999     if (!p_pic)
1000         GetOutput(p_dec, env, &p_pic, 0);
1001     (*myVm)->DetachCurrentThread(myVm);
1002
1003     block_Release(p_block);
1004     *pp_block = NULL;
1005
1006     return p_pic;
1007 }