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