packetizer: h264: extract spsext with sps/pps
[vlc.git] / modules / codec / omxil / mediacodec.c
1 /*****************************************************************************
2  * 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 <stdint.h>
31 #include <assert.h>
32
33 #include <vlc_common.h>
34 #include <vlc_aout.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
37 #include <vlc_block_helper.h>
38 #include <vlc_memory.h>
39 #include <vlc_timestamp_helper.h>
40 #include <vlc_threads.h>
41 #include <vlc_bits.h>
42
43 #include "mediacodec.h"
44 #include "../../packetizer/h264_nal.h"
45 #include "../../packetizer/hevc_nal.h"
46 #include "../../packetizer/hxxx_nal.h"
47 #include <OMX_Core.h>
48 #include <OMX_Component.h>
49 #include "omxil_utils.h"
50 #include "../../video_output/android/android_window.h"
51
52 #define BLOCK_FLAG_CSD (0x01 << BLOCK_FLAG_PRIVATE_SHIFT)
53
54 /* Codec Specific Data */
55 struct csd
56 {
57     uint8_t *p_buf;
58     size_t i_size;
59 };
60
61 #define NEWBLOCK_FLAG_RESTART (0x01)
62 #define NEWBLOCK_FLAG_FLUSH (0x02)
63 /**
64  * Callback called when a new block is processed from DecodeCommon.
65  * It returns -1 in case of error, 0 if block should be dropped, 1 otherwise.
66  */
67 typedef int (*dec_on_new_block_cb)(decoder_t *, block_t **, int *);
68
69 /**
70  * Callback called when decoder is flushing.
71  */
72 typedef void (*dec_on_flush_cb)(decoder_t *);
73
74 /**
75  * Callback called when DecodeCommon try to get an output buffer (pic or block).
76  * It returns -1 in case of error, or the number of output buffer returned.
77  */
78 typedef int (*dec_process_output_cb)(decoder_t *, mc_api_out *, picture_t **, block_t **);
79
80 struct decoder_sys_t
81 {
82     mc_api *api;
83
84     /* Codec Specific Data buffer: sent in DecodeCommon after a start or a flush
85      * with the BUFFER_FLAG_CODEC_CONFIG flag.*/
86     block_t **pp_csd;
87     size_t i_csd_count;
88     size_t i_csd_send;
89
90     bool b_update_format;
91     bool b_has_format;
92
93     int64_t i_preroll_end;
94     int     i_quirks;
95
96     /* Specific Audio/Video callbacks */
97     dec_on_new_block_cb     pf_on_new_block;
98     dec_on_flush_cb         pf_on_flush;
99     dec_process_output_cb   pf_process_output;
100
101     vlc_mutex_t     lock;
102     vlc_thread_t    out_thread;
103     /* Cond used to signal the output thread */
104     vlc_cond_t      cond;
105     /* Cond used to signal the decoder thread */
106     vlc_cond_t      dec_cond;
107     /* Set to true by pf_flush to signal the output thread to flush */
108     bool            b_flush_out;
109     /* If true, the output thread will start to dequeue output pictures */
110     bool            b_output_ready;
111     /* If true, the first input block was successfully dequeued */
112     bool            b_input_dequeued;
113     bool            b_aborted;
114     /* TODO: remove */
115     bool            b_error_signaled;
116
117     union
118     {
119         struct
120         {
121             AWindowHandler *p_awh;
122             unsigned int i_stride, i_slice_height, i_width, i_height;
123             int i_pixel_format;
124             uint8_t i_nal_length_size;
125             size_t i_h264_profile;
126             bool b_first_mp4v_iframe;
127             /* stores the inflight picture for each output buffer or NULL */
128             picture_sys_t** pp_inflight_pictures;
129             unsigned int i_inflight_pictures;
130             timestamp_fifo_t *timestamp_fifo;
131         } video;
132         struct {
133             date_t i_end_date;
134             int i_channels;
135             bool b_extract;
136             /* Some audio decoders need a valid channel count */
137             bool b_need_channels;
138             int pi_extraction[AOUT_CHAN_MAX];
139         } audio;
140     } u;
141 };
142
143 /*****************************************************************************
144  * Local prototypes
145  *****************************************************************************/
146 static int  OpenDecoderJni(vlc_object_t *);
147 static int  OpenDecoderNdk(vlc_object_t *);
148 static void CleanDecoder(decoder_t *);
149 static void CloseDecoder(vlc_object_t *);
150
151 static int Video_OnNewBlock(decoder_t *, block_t **, int *);
152 static int VideoH264_OnNewBlock(decoder_t *, block_t **, int *);
153 static int VideoHEVC_OnNewBlock(decoder_t *, block_t **, int *);
154 static int VideoMP4V_OnNewBlock(decoder_t *, block_t **, int *);
155 static int VideoVC1_OnNewBlock(decoder_t *, block_t **, int *);
156 static void Video_OnFlush(decoder_t *);
157 static int Video_ProcessOutput(decoder_t *, mc_api_out *, picture_t **, block_t **);
158 static picture_t *DecodeVideo(decoder_t *, block_t **);
159
160 static int Audio_OnNewBlock(decoder_t *, block_t **, int *);
161 static void Audio_OnFlush(decoder_t *);
162 static int Audio_ProcessOutput(decoder_t *, mc_api_out *, picture_t **, block_t **);
163 static block_t *DecodeAudio(decoder_t *, block_t **);
164
165 static void DecodeFlushLocked(decoder_t *);
166 static void DecodeFlush(decoder_t *);
167 static void StopMediaCodec(decoder_t *);
168 static void *OutThread(void *);
169
170 static void InvalidateAllPictures(decoder_t *);
171 static void RemoveInflightPictures(decoder_t *);
172
173 /*****************************************************************************
174  * Module descriptor
175  *****************************************************************************/
176 #define DIRECTRENDERING_TEXT N_("Android direct rendering")
177 #define DIRECTRENDERING_LONGTEXT N_(\
178         "Enable Android direct rendering using opaque buffers.")
179
180 #define MEDIACODEC_AUDIO_TEXT "Use MediaCodec for audio decoding"
181 #define MEDIACODEC_AUDIO_LONGTEXT "Still experimental."
182
183 #define MEDIACODEC_TUNNELEDPLAYBACK_TEXT "Use a tunneled surface for playback"
184
185 #define CFG_PREFIX "mediacodec-"
186
187 vlc_module_begin ()
188     set_description( N_("Video decoder using Android MediaCodec via NDK") )
189     set_category( CAT_INPUT )
190     set_subcategory( SUBCAT_INPUT_VCODEC )
191     set_section( N_("Decoding") , NULL )
192     set_capability( "decoder", 0 ) /* Only enabled via commandline arguments */
193     add_bool(CFG_PREFIX "dr", true,
194              DIRECTRENDERING_TEXT, DIRECTRENDERING_LONGTEXT, true)
195     add_bool(CFG_PREFIX "audio", false,
196              MEDIACODEC_AUDIO_TEXT, MEDIACODEC_AUDIO_LONGTEXT, true)
197     add_bool(CFG_PREFIX "tunneled-playback", false,
198              MEDIACODEC_TUNNELEDPLAYBACK_TEXT, NULL, true)
199     set_callbacks( OpenDecoderNdk, CloseDecoder )
200     add_shortcut( "mediacodec_ndk" )
201     add_submodule ()
202         set_description( N_("Video decoder using Android MediaCodec via JNI") )
203         set_capability( "decoder", 0 )
204         set_callbacks( OpenDecoderJni, CloseDecoder )
205         add_shortcut( "mediacodec_jni" )
206 vlc_module_end ()
207
208 static void CSDFree(decoder_t *p_dec)
209 {
210     decoder_sys_t *p_sys = p_dec->p_sys;
211
212     if (p_sys->pp_csd)
213     {
214         for (unsigned int i = 0; i < p_sys->i_csd_count; ++i)
215             block_Release(p_sys->pp_csd[i]);
216         free(p_sys->pp_csd);
217         p_sys->pp_csd = NULL;
218     }
219     p_sys->i_csd_count = 0;
220 }
221
222 /* Create the p_sys->p_csd that will be sent from DecodeCommon */
223 static int CSDDup(decoder_t *p_dec, const struct csd *p_csd, size_t i_count)
224 {
225     decoder_sys_t *p_sys = p_dec->p_sys;
226
227     CSDFree(p_dec);
228
229     p_sys->pp_csd = malloc(i_count * sizeof(block_t *));
230     if (!p_sys->pp_csd)
231         return VLC_ENOMEM;
232
233     for (size_t i = 0; i < i_count; ++i)
234     {
235         p_sys->pp_csd[i] = block_Alloc(p_csd[i].i_size);
236         if (!p_sys->pp_csd[i])
237         {
238             CSDFree(p_dec);
239             return VLC_ENOMEM;
240         }
241         p_sys->pp_csd[i]->i_flags = BLOCK_FLAG_CSD;
242         memcpy(p_sys->pp_csd[i]->p_buffer, p_csd[i].p_buf, p_csd[i].i_size);
243         p_sys->i_csd_count++;
244     }
245
246     p_sys->i_csd_send = 0;
247     return VLC_SUCCESS;
248 }
249
250 static bool CSDCmp(decoder_t *p_dec, struct csd *p_csd, size_t i_csd_count)
251 {
252     decoder_sys_t *p_sys = p_dec->p_sys;
253
254     if (p_sys->i_csd_count != i_csd_count)
255         return false;
256     for (size_t i = 0; i < i_csd_count; ++i)
257     {
258         if (p_sys->pp_csd[i]->i_buffer != p_csd[i].i_size
259          || memcmp(p_sys->pp_csd[i]->p_buffer, p_csd[i].p_buf,
260                    p_csd[i].i_size) != 0)
261             return false;
262     }
263     return true;
264 }
265
266 /* Fill the p_sys->p_csd struct with H264 Parameter Sets */
267 static int H264SetCSD(decoder_t *p_dec, void *p_buf, size_t i_size,
268                       bool *p_size_changed)
269 {
270     decoder_sys_t *p_sys = p_dec->p_sys;
271     uint8_t *p_sps_buf = NULL, *p_pps_buf = NULL, *p_ext_buf = NULL;
272     size_t i_sps_size = 0, i_pps_size = 0, i_ext_size = 0;
273
274     /* Check if p_buf contains a valid SPS PPS */
275     if (h264_get_spspps(p_buf, i_size,
276                         &p_sps_buf, &i_sps_size,
277                         &p_pps_buf, &i_pps_size,
278                         &p_ext_buf, &i_ext_size) == 0 )
279     {
280         struct csd csd[2];
281         int i_csd_count = 0;
282
283         const uint8_t *p_buffer = p_sps_buf;
284         size_t i_buffer = i_sps_size;
285         if(!hxxx_strip_AnnexB_startcode(&p_buffer, &i_buffer))
286             return VLC_EGENERIC;
287
288         h264_sequence_parameter_set_t *p_sps = h264_decode_sps(p_buffer, i_buffer, true);
289         if( !p_sps )
290             return VLC_EGENERIC;
291
292         unsigned vsize[4];
293         (void) h264_get_picture_size( p_sps, &vsize[0], &vsize[1], &vsize[2], &vsize[3] );
294         /* FIXME: what to do with visible width/height ? */
295
296         if (i_sps_size)
297         {
298             csd[i_csd_count].p_buf = p_sps_buf;
299             csd[i_csd_count].i_size = i_sps_size;
300             i_csd_count++;
301         }
302         if (i_pps_size)
303         {
304             csd[i_csd_count].p_buf = p_pps_buf;
305             csd[i_csd_count].i_size = i_pps_size;
306             i_csd_count++;
307         }
308
309         /* Compare the SPS PPS with the old one */
310         if (!CSDCmp(p_dec, csd, i_csd_count))
311         {
312             msg_Warn(p_dec, "New SPS/PPS found, id: %d size: %ux%u sps: %d pps: %d",
313                      p_sps->i_id, vsize[0], vsize[1],
314                      i_sps_size, i_pps_size);
315
316             /* In most use cases, p_sys->p_csd[0] contains a SPS, and
317              * p_sys->p_csd[1] contains a PPS */
318             if (CSDDup(p_dec, csd, i_csd_count))
319             {
320                 h264_release_sps( p_sps );
321                 return VLC_ENOMEM;
322             }
323
324             if (p_size_changed)
325                 *p_size_changed = (vsize[0] != p_sys->u.video.i_width
326                                 || vsize[1] != p_sys->u.video.i_height);
327
328             p_sys->u.video.i_width = vsize[0];
329             p_sys->u.video.i_height = vsize[1];
330
331             h264_release_sps( p_sps );
332
333             return VLC_SUCCESS;
334         }
335
336         h264_release_sps( p_sps );
337     }
338
339     return VLC_EGENERIC;
340 }
341
342 static int ParseVideoExtraH264(decoder_t *p_dec, uint8_t *p_extra, int i_extra)
343 {
344     decoder_sys_t *p_sys = p_dec->p_sys;
345
346     if (h264_isavcC(p_extra, i_extra))
347     {
348         size_t i_size = 0;
349         uint8_t *p_buf = h264_avcC_to_AnnexB_NAL(p_extra, i_extra, &i_size,
350                                                  &p_sys->u.video.i_nal_length_size);
351         if (!p_buf)
352             return VLC_EGENERIC;
353
354         int i_ret = H264SetCSD(p_dec, p_buf, i_size, NULL);
355         free(p_buf);
356         return i_ret;
357     }
358     else
359         return H264SetCSD(p_dec, p_extra, i_extra, NULL);
360 }
361
362 static int ParseVideoExtraHEVC(decoder_t *p_dec, uint8_t *p_extra, int i_extra)
363 {
364     decoder_sys_t *p_sys = p_dec->p_sys;
365
366     if (hevc_ishvcC(p_extra, i_extra))
367     {
368         struct csd csd;
369         csd.p_buf = hevc_hvcC_to_AnnexB_NAL(p_extra, i_extra, &csd.i_size,
370                                             &p_sys->u.video.i_nal_length_size);
371         if (csd.p_buf)
372         {
373             CSDDup(p_dec, &csd, 1);
374             free(csd.p_buf);
375         }
376     }
377     /* FIXME: what to do with AnnexB ? */
378
379     return VLC_SUCCESS;
380 }
381
382 static int ParseVideoExtraVc1(decoder_t *p_dec, uint8_t *p_extra, int i_extra)
383 {
384     int offset = 0;
385     struct csd csd;
386
387     if (i_extra < 4)
388         return VLC_EGENERIC;
389
390     /* Initialisation data starts with : 0x00 0x00 0x01 0x0f */
391     /* Skipping unecessary data */
392     static const uint8_t vc1_start_code[4] = {0x00, 0x00, 0x01, 0x0f};
393     for (; offset < i_extra - 4 ; ++offset)
394     {
395         if (!memcmp(&p_extra[offset], vc1_start_code, 4))
396             break;
397     }
398
399     /* Could not find the sequence header start code */
400     if (offset >= i_extra - 4)
401         return VLC_EGENERIC;
402
403     csd.i_size = i_extra - offset;
404     csd.p_buf = p_extra + offset;
405
406     return CSDDup(p_dec, &csd, 1);
407 }
408
409 static int ParseVideoExtraWmv3(decoder_t *p_dec, uint8_t *p_extra, int i_extra)
410 {
411     /* WMV3 initialisation data :
412      * 8 fixed bytes
413      * 4 extradata bytes
414      * 4 height bytes (little endian)
415      * 4 width bytes (little endian)
416      * 16 fixed bytes */
417
418     if (i_extra < 4)
419         return VLC_EGENERIC;
420
421     uint8_t p_data[36] = {
422         0x8e, 0x01, 0x00, 0xc5, /* Fixed bytes values */
423         0x04, 0x00, 0x00, 0x00, /* Same */
424         0x00, 0x00, 0x00, 0x00, /* extradata emplacement */
425         0x00, 0x00, 0x00, 0x00, /* height emplacement (little endian) */
426         0x00, 0x00, 0x00, 0x00, /* width emplacement (little endian) */
427         0x0c, 0x00, 0x00, 0x00, /* Fixed byte pattern */
428         0x00, 0x00, 0x00, 0x00,
429         0x00, 0x00, 0x00, 0x00,
430         0x00, 0x00, 0x00, 0x00
431     };
432
433     struct csd csd;
434
435     csd.i_size = sizeof(p_data);
436     /* Adding extradata */
437     memcpy(&p_data[8], p_extra, 4);
438     /* Adding height and width, little endian */
439     SetDWLE(&(p_data[12]), p_dec->fmt_in.video.i_height);
440     SetDWLE(&(p_data[16]), p_dec->fmt_in.video.i_width);
441
442     csd.p_buf = p_data;
443     return CSDDup(p_dec, &csd, 1);
444 }
445
446 static int ParseVideoExtra(decoder_t *p_dec)
447 {
448     uint8_t *p_extra = p_dec->fmt_in.p_extra;
449     int i_extra = p_dec->fmt_in.i_extra;
450
451     switch (p_dec->fmt_in.i_codec)
452     {
453     case VLC_CODEC_H264:
454         return ParseVideoExtraH264(p_dec, p_extra, i_extra);
455     case VLC_CODEC_HEVC:
456         return ParseVideoExtraHEVC(p_dec, p_extra, i_extra);
457     case VLC_CODEC_WMV3:
458         return ParseVideoExtraWmv3(p_dec, p_extra, i_extra);
459     case VLC_CODEC_VC1:
460         return ParseVideoExtraVc1(p_dec, p_extra, i_extra);
461     default:
462         return VLC_SUCCESS;
463     }
464 }
465
466 /*****************************************************************************
467  * StartMediaCodec: Create the mediacodec instance
468  *****************************************************************************/
469 static int StartMediaCodec(decoder_t *p_dec)
470 {
471     decoder_sys_t *p_sys = p_dec->p_sys;
472     union mc_api_args args;
473
474     if (((p_sys->api->i_quirks & MC_API_QUIRKS_NEED_CSD) && !p_sys->pp_csd))
475     {
476         msg_Warn(p_dec, "waiting for extra data for codec %4.4s",
477                  (const char *)&p_dec->fmt_in.i_codec);
478         return VLC_ENOOBJ;
479     }
480
481     if (p_dec->fmt_in.i_cat == VIDEO_ES)
482     {
483         if (!p_sys->u.video.i_width || !p_sys->u.video.i_height)
484         {
485             msg_Warn(p_dec, "waiting for a valid video size for codec %4.4s",
486                      (const char *)&p_dec->fmt_in.i_codec);
487             return VLC_ENOOBJ;
488         }
489         args.video.i_width = p_sys->u.video.i_width;
490         args.video.i_height = p_sys->u.video.i_height;
491
492         switch (p_dec->fmt_in.video.orientation)
493         {
494             case ORIENT_ROTATED_90:
495                 args.video.i_angle = 90;
496                 break;
497             case ORIENT_ROTATED_180:
498                 args.video.i_angle = 180;
499                 break;
500             case ORIENT_ROTATED_270:
501                 args.video.i_angle = 270;
502                 break;
503             default:
504                 args.video.i_angle = 0;
505         }
506
507         /* Configure again if h264 profile changed */
508         if (p_dec->fmt_in.i_codec == VLC_CODEC_H264
509          && !p_sys->u.video.i_h264_profile)
510         {
511             h264_get_profile_level(&p_dec->fmt_in,
512                                    &p_sys->u.video.i_h264_profile, NULL, NULL);
513             if (p_sys->u.video.i_h264_profile)
514             {
515                 if (p_sys->api->configure(p_sys->api,
516                                           p_sys->u.video.i_h264_profile) != 0 )
517                     return VLC_EGENERIC;
518             }
519         }
520
521         if (!p_sys->u.video.p_awh && var_InheritBool(p_dec, CFG_PREFIX "dr"))
522         {
523             if ((p_sys->u.video.p_awh = AWindowHandler_new(VLC_OBJECT(p_dec))))
524             {
525                 /* Direct rendering:
526                  * The surface must be released by the Vout before calling
527                  * start. Request a valid OPAQUE Vout to release any non-OPAQUE
528                  * Vout that will release the surface.
529                  */
530                 p_dec->fmt_out.video.i_width = p_sys->u.video.i_width;
531                 p_dec->fmt_out.video.i_height = p_sys->u.video.i_height;
532                 p_dec->fmt_out.i_codec = VLC_CODEC_ANDROID_OPAQUE;
533                 if (decoder_UpdateVideoFormat(p_dec) != 0)
534                 {
535                     msg_Err(p_dec, "Opaque Vout request failed: "
536                                    "fallback to non opaque");
537
538                     AWindowHandler_destroy(p_sys->u.video.p_awh);
539                     p_sys->u.video.p_awh = NULL;
540                 }
541             }
542         }
543         args.video.p_awh = p_sys->u.video.p_awh;
544         args.video.b_tunneled_playback = args.video.p_awh ?
545                 var_InheritBool(p_dec, CFG_PREFIX "tunneled-playback") : false;
546     }
547     else
548     {
549         date_Set(&p_sys->u.audio.i_end_date, VLC_TS_INVALID);
550
551         args.audio.i_sample_rate    = p_dec->fmt_in.audio.i_rate;
552         args.audio.i_channel_count  = p_dec->p_sys->u.audio.i_channels;
553     }
554
555     return p_sys->api->start(p_sys->api, &args);
556 }
557
558 /*****************************************************************************
559  * StopMediaCodec: Close the mediacodec instance
560  *****************************************************************************/
561 static void StopMediaCodec(decoder_t *p_dec)
562 {
563     decoder_sys_t *p_sys = p_dec->p_sys;
564
565     /* Remove all pictures that are currently in flight in order
566      * to prevent the vout from using destroyed output buffers. */
567     if (p_sys->api->b_direct_rendering)
568         RemoveInflightPictures(p_dec);
569
570     p_sys->api->stop(p_sys->api);
571     if (p_dec->fmt_in.i_cat == VIDEO_ES && p_sys->u.video.p_awh)
572         AWindowHandler_releaseSurface(p_sys->u.video.p_awh, AWindow_Video);
573 }
574
575 /*****************************************************************************
576  * OpenDecoder: Create the decoder instance
577  *****************************************************************************/
578 static int OpenDecoder(vlc_object_t *p_this, pf_MediaCodecApi_init pf_init)
579 {
580     decoder_t *p_dec = (decoder_t *)p_this;
581     decoder_sys_t *p_sys;
582     mc_api *api;
583     int i_ret;
584     size_t i_h264_profile = 0;
585     const char *mime = NULL;
586
587     /* Video or Audio if "mediacodec-audio" bool is true */
588     if (p_dec->fmt_in.i_cat != VIDEO_ES && (p_dec->fmt_in.i_cat != AUDIO_ES
589      || !var_InheritBool(p_dec, CFG_PREFIX "audio")))
590         return VLC_EGENERIC;
591
592     if (p_dec->fmt_in.i_cat == VIDEO_ES)
593     {
594         if (!p_dec->fmt_in.video.i_width || !p_dec->fmt_in.video.i_height)
595         {
596             /* We can handle h264 without a valid video size
597              * TODO handle VC1 with no size */
598             if (p_dec->fmt_in.i_codec != VLC_CODEC_H264)
599             {
600                 msg_Dbg(p_dec, "resolution (%dx%d) not supported",
601                         p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height);
602                 return VLC_EGENERIC;
603             }
604         }
605
606         switch (p_dec->fmt_in.i_codec) {
607         case VLC_CODEC_HEVC: mime = "video/hevc"; break;
608         case VLC_CODEC_H264: mime = "video/avc"; break;
609         case VLC_CODEC_H263: mime = "video/3gpp"; break;
610         case VLC_CODEC_MP4V: mime = "video/mp4v-es"; break;
611         case VLC_CODEC_WMV3: mime = "video/x-ms-wmv"; break;
612         case VLC_CODEC_VC1:  mime = "video/wvc1"; break;
613         case VLC_CODEC_VP8:  mime = "video/x-vnd.on2.vp8"; break;
614         case VLC_CODEC_VP9:  mime = "video/x-vnd.on2.vp9"; break;
615         case VLC_CODEC_MPGV: mime = "video/mpeg2"; break;
616         }
617     }
618     else
619     {
620         switch (p_dec->fmt_in.i_codec) {
621         case VLC_CODEC_AMR_NB: mime = "audio/3gpp"; break;
622         case VLC_CODEC_AMR_WB: mime = "audio/amr-wb"; break;
623         case VLC_CODEC_MPGA:
624         case VLC_CODEC_MP3:    mime = "audio/mpeg"; break;
625         case VLC_CODEC_MP2:    mime = "audio/mpeg-L2"; break;
626         case VLC_CODEC_MP4A:   mime = "audio/mp4a-latm"; break;
627         case VLC_CODEC_QCELP:  mime = "audio/qcelp"; break;
628         case VLC_CODEC_VORBIS: mime = "audio/vorbis"; break;
629         case VLC_CODEC_OPUS:   mime = "audio/opus"; break;
630         case VLC_CODEC_ALAW:   mime = "audio/g711-alaw"; break;
631         case VLC_CODEC_MULAW:  mime = "audio/g711-mlaw"; break;
632         case VLC_CODEC_FLAC:   mime = "audio/flac"; break;
633         case VLC_CODEC_GSM:    mime = "audio/gsm"; break;
634         case VLC_CODEC_A52:    mime = "audio/ac3"; break;
635         case VLC_CODEC_EAC3:   mime = "audio/eac3"; break;
636         case VLC_CODEC_ALAC:   mime = "audio/alac"; break;
637         case VLC_CODEC_DTS:    mime = "audio/vnd.dts"; break;
638         /* case VLC_CODEC_: mime = "audio/mpeg-L1"; break; */
639         /* case VLC_CODEC_: mime = "audio/aac-adts"; break; */
640         }
641     }
642     if (!mime)
643     {
644         msg_Dbg(p_dec, "codec %4.4s not supported",
645                 (char *)&p_dec->fmt_in.i_codec);
646         return VLC_EGENERIC;
647     }
648
649     if ((api = calloc(1, sizeof(mc_api))) == NULL)
650         return VLC_ENOMEM;
651     api->p_obj = p_this;
652     api->i_codec = p_dec->fmt_in.i_codec;
653     api->i_cat = p_dec->fmt_in.i_cat;
654     api->psz_mime = mime;
655
656     if (p_dec->fmt_in.i_codec == VLC_CODEC_H264)
657         h264_get_profile_level(&p_dec->fmt_in, &i_h264_profile, NULL, NULL);
658     if (pf_init(api) != 0)
659     {
660         free(api);
661         return VLC_EGENERIC;
662     }
663     if (api->configure(api, i_h264_profile) != 0)
664     {
665         /* If the device can't handle video/wvc1,
666          * it can probably handle video/x-ms-wmv */
667         if (!strcmp(mime, "video/wvc1") && p_dec->fmt_in.i_codec == VLC_CODEC_VC1)
668         {
669             api->psz_mime = "video/x-ms-wmv";
670             if (api->configure(api, i_h264_profile) != 0)
671             {
672                 api->clean(api);
673                 free(api);
674                 return (VLC_EGENERIC);
675             }
676         }
677         else
678         {
679             api->clean(api);
680             free(api);
681             return VLC_EGENERIC;
682         }
683     }
684
685     /* Allocate the memory needed to store the decoder's structure */
686     if ((p_sys = calloc(1, sizeof(*p_sys))) == NULL)
687     {
688         api->clean(api);
689         free(api);
690         return VLC_ENOMEM;
691     }
692     p_sys->api = api;
693     p_dec->p_sys = p_sys;
694
695     p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
696     p_dec->fmt_out.video = p_dec->fmt_in.video;
697     p_dec->fmt_out.audio = p_dec->fmt_in.audio;
698
699     vlc_mutex_init(&p_sys->lock);
700     vlc_cond_init(&p_sys->cond);
701     vlc_cond_init(&p_sys->dec_cond);
702
703     if (p_dec->fmt_in.i_cat == VIDEO_ES)
704     {
705         switch (p_dec->fmt_in.i_codec)
706         {
707         case VLC_CODEC_H264:
708             p_sys->pf_on_new_block = VideoH264_OnNewBlock;
709             break;
710         case VLC_CODEC_HEVC:
711             p_sys->pf_on_new_block = VideoHEVC_OnNewBlock;
712             break;
713         case VLC_CODEC_MP4V:
714             p_sys->pf_on_new_block = VideoMP4V_OnNewBlock;
715             break;
716         case VLC_CODEC_VC1:
717             p_sys->pf_on_new_block = VideoVC1_OnNewBlock;
718             break;
719         default:
720             p_sys->pf_on_new_block = Video_OnNewBlock;
721             break;
722         }
723         p_sys->pf_on_flush = Video_OnFlush;
724         p_sys->pf_process_output = Video_ProcessOutput;
725         p_sys->u.video.i_width = p_dec->fmt_in.video.i_width;
726         p_sys->u.video.i_height = p_dec->fmt_in.video.i_height;
727         p_sys->u.video.i_h264_profile = i_h264_profile;
728
729         p_sys->u.video.timestamp_fifo = timestamp_FifoNew(32);
730         if (!p_sys->u.video.timestamp_fifo)
731             goto bailout;
732
733         TAB_INIT( p_sys->u.video.i_inflight_pictures,
734                   p_sys->u.video.pp_inflight_pictures );
735     }
736     else
737     {
738         p_sys->pf_on_new_block = Audio_OnNewBlock;
739         p_sys->pf_on_flush = Audio_OnFlush;
740         p_sys->pf_process_output = Audio_ProcessOutput;
741         p_sys->u.audio.i_channels = p_dec->fmt_in.audio.i_channels;
742
743         if ((p_sys->api->i_quirks & MC_API_AUDIO_QUIRKS_NEED_CHANNELS)
744          && !p_sys->u.audio.i_channels)
745         {
746             msg_Warn(p_dec, "codec need a valid channel count");
747             goto bailout;
748         }
749     }
750
751     if (p_dec->fmt_in.i_extra)
752     {
753         /* Try first to configure specific Video CSD */
754         if (p_dec->fmt_in.i_cat == VIDEO_ES)
755             if (ParseVideoExtra(p_dec) != VLC_SUCCESS)
756                 return VLC_EGENERIC;
757
758         /* Set default CSD if ParseVideoExtra failed to configure one */
759         if (!p_sys->pp_csd)
760         {
761             struct csd csd;
762
763             csd.p_buf = p_dec->fmt_in.p_extra;
764             csd.i_size = p_dec->fmt_in.i_extra;
765             CSDDup(p_dec, &csd, 1);
766         }
767     }
768
769     i_ret = StartMediaCodec(p_dec);
770     switch (i_ret)
771     {
772     case VLC_SUCCESS:
773         break;
774     case VLC_ENOOBJ:
775         if (p_dec->fmt_in.i_codec == VLC_CODEC_MP4V)
776             msg_Warn(p_dec, "late opening with MPEG4 not handled"); /* TODO */
777         else
778             break;
779     default:
780         msg_Err(p_dec, "StartMediaCodec failed");
781         goto bailout;
782     }
783
784     if (vlc_clone(&p_sys->out_thread, OutThread, p_dec,
785                   VLC_THREAD_PRIORITY_LOW))
786     {
787         msg_Err(p_dec, "vlc_clone failed");
788         vlc_mutex_unlock(&p_sys->lock);
789         goto bailout;
790     }
791
792     p_dec->pf_decode_video = DecodeVideo;
793     p_dec->pf_decode_audio = DecodeAudio;
794     p_dec->pf_flush        = DecodeFlush;
795
796     return VLC_SUCCESS;
797
798 bailout:
799     CleanDecoder(p_dec);
800     return VLC_EGENERIC;
801 }
802
803 static int OpenDecoderNdk(vlc_object_t *p_this)
804 {
805     return OpenDecoder(p_this, MediaCodecNdk_Init);
806 }
807
808 static int OpenDecoderJni(vlc_object_t *p_this)
809 {
810     return OpenDecoder(p_this, MediaCodecJni_Init);
811 }
812
813 static void AbortDecoderLocked(decoder_t *p_dec)
814 {
815     decoder_sys_t *p_sys = p_dec->p_sys;
816
817     if (!p_sys->b_aborted)
818     {
819         p_sys->b_aborted = true;
820         vlc_cancel(p_sys->out_thread);
821     }
822 }
823
824 static void CleanDecoder(decoder_t *p_dec)
825 {
826     decoder_sys_t *p_sys = p_dec->p_sys;
827
828     vlc_mutex_destroy(&p_sys->lock);
829     vlc_cond_destroy(&p_sys->cond);
830     vlc_cond_destroy(&p_sys->dec_cond);
831
832     StopMediaCodec(p_dec);
833
834     CSDFree(p_dec);
835     p_sys->api->clean(p_sys->api);
836
837     if (p_dec->fmt_in.i_cat == VIDEO_ES)
838     {
839         if (p_sys->u.video.timestamp_fifo)
840             timestamp_FifoRelease(p_sys->u.video.timestamp_fifo);
841         if (p_sys->u.video.p_awh)
842             AWindowHandler_destroy(p_sys->u.video.p_awh);
843     }
844     free(p_sys->api);
845     free(p_sys);
846 }
847
848 /*****************************************************************************
849  * CloseDecoder: Close the decoder instance
850  *****************************************************************************/
851 static void CloseDecoder(vlc_object_t *p_this)
852 {
853     decoder_t *p_dec = (decoder_t *)p_this;
854     decoder_sys_t *p_sys = p_dec->p_sys;
855
856     vlc_mutex_lock(&p_sys->lock);
857     /* Unblock output thread waiting in dequeue_out */
858     DecodeFlushLocked(p_dec);
859     /* Cancel the output thread */
860     AbortDecoderLocked(p_dec);
861     vlc_mutex_unlock(&p_sys->lock);
862
863     vlc_join(p_sys->out_thread, NULL);
864
865     CleanDecoder(p_dec);
866 }
867
868 /*****************************************************************************
869  * vout callbacks
870  *****************************************************************************/
871 static void ReleasePicture(decoder_t *p_dec, unsigned i_index, bool b_render)
872 {
873     decoder_sys_t *p_sys = p_dec->p_sys;
874
875     p_sys->api->release_out(p_sys->api, i_index, b_render);
876 }
877
878 static void InvalidateAllPictures(decoder_t *p_dec)
879 {
880     decoder_sys_t *p_sys = p_dec->p_sys;
881
882     for (unsigned int i = 0; i < p_sys->u.video.i_inflight_pictures; ++i)
883         AndroidOpaquePicture_Release(p_sys->u.video.pp_inflight_pictures[i],
884                                      false);
885 }
886
887 static int InsertInflightPicture(decoder_t *p_dec, picture_sys_t *p_picsys)
888 {
889     decoder_sys_t *p_sys = p_dec->p_sys;
890
891     if (!p_picsys->priv.hw.p_dec)
892     {
893         p_picsys->priv.hw.p_dec = p_dec;
894         p_picsys->priv.hw.pf_release = ReleasePicture;
895         TAB_APPEND_CAST((picture_sys_t **),
896                         p_sys->u.video.i_inflight_pictures,
897                         p_sys->u.video.pp_inflight_pictures,
898                         p_picsys);
899     } /* else already attached */
900     return 0;
901 }
902
903 static void RemoveInflightPictures(decoder_t *p_dec)
904 {
905     decoder_sys_t *p_sys = p_dec->p_sys;
906
907     for (unsigned int i = 0; i < p_sys->u.video.i_inflight_pictures; ++i)
908         AndroidOpaquePicture_DetachDecoder(p_sys->u.video.pp_inflight_pictures[i]);
909     TAB_CLEAN(p_sys->u.video.i_inflight_pictures,
910               p_sys->u.video.pp_inflight_pictures);
911 }
912
913 static int Video_ProcessOutput(decoder_t *p_dec, mc_api_out *p_out,
914                                picture_t **pp_out_pic, block_t **pp_out_block)
915 {
916     decoder_sys_t *p_sys = p_dec->p_sys;
917     (void) pp_out_block;
918     assert(pp_out_pic);
919
920     if (p_out->type == MC_OUT_TYPE_BUF)
921     {
922         picture_t *p_pic = NULL;
923
924         /* Use the aspect ratio provided by the input (ie read from packetizer).
925          * Don't check the current value of the aspect ratio in fmt_out, since we
926          * want to allow changes in it to propagate. */
927         if (p_dec->fmt_in.video.i_sar_num != 0 && p_dec->fmt_in.video.i_sar_den != 0
928          && (p_dec->fmt_out.video.i_sar_num != p_dec->fmt_in.video.i_sar_num ||
929              p_dec->fmt_out.video.i_sar_den != p_dec->fmt_in.video.i_sar_den))
930         {
931             p_dec->fmt_out.video.i_sar_num = p_dec->fmt_in.video.i_sar_num;
932             p_dec->fmt_out.video.i_sar_den = p_dec->fmt_in.video.i_sar_den;
933             p_sys->b_update_format = true;
934         }
935
936         if (p_sys->b_update_format)
937         {
938             p_sys->b_update_format = false;
939             if (decoder_UpdateVideoFormat(p_dec) != 0)
940             {
941                 msg_Err(p_dec, "decoder_UpdateVideoFormat failed");
942                 p_sys->api->release_out(p_sys->api, p_out->u.buf.i_index, false);
943                 return -1;
944             }
945         }
946
947         /* If the oldest input block had no PTS, the timestamp of
948          * the frame returned by MediaCodec might be wrong so we
949          * overwrite it with the corresponding dts. Call FifoGet
950          * first in order to avoid a gap if buffers are released
951          * due to an invalid format or a preroll */
952         int64_t forced_ts = timestamp_FifoGet(p_sys->u.video.timestamp_fifo);
953
954         if (!p_sys->b_has_format) {
955             msg_Warn(p_dec, "Buffers returned before output format is set, dropping frame");
956             return p_sys->api->release_out(p_sys->api, p_out->u.buf.i_index, false);
957         }
958
959         if (p_out->u.buf.i_ts <= p_sys->i_preroll_end)
960             return p_sys->api->release_out(p_sys->api, p_out->u.buf.i_index, false);
961
962         if (!p_sys->api->b_direct_rendering && p_out->u.buf.p_ptr == NULL)
963         {
964             /* This can happen when receiving an EOS buffer */
965             msg_Warn(p_dec, "Invalid buffer, dropping frame");
966             return p_sys->api->release_out(p_sys->api, p_out->u.buf.i_index, false);
967         }
968
969         p_pic = decoder_NewPicture(p_dec);
970         if (!p_pic) {
971             msg_Warn(p_dec, "NewPicture failed");
972             return p_sys->api->release_out(p_sys->api, p_out->u.buf.i_index, false);
973         }
974
975         if (forced_ts == VLC_TS_INVALID)
976             p_pic->date = p_out->u.buf.i_ts;
977         else
978             p_pic->date = forced_ts;
979
980         if (p_sys->api->b_direct_rendering)
981         {
982             p_pic->p_sys->priv.hw.i_index = p_out->u.buf.i_index;
983             InsertInflightPicture(p_dec, p_pic->p_sys);
984         } else {
985             unsigned int chroma_div;
986             GetVlcChromaSizes(p_dec->fmt_out.i_codec,
987                               p_dec->fmt_out.video.i_width,
988                               p_dec->fmt_out.video.i_height,
989                               NULL, NULL, &chroma_div);
990             CopyOmxPicture(p_sys->u.video.i_pixel_format, p_pic,
991                            p_sys->u.video.i_slice_height, p_sys->u.video.i_stride,
992                            (uint8_t *)p_out->u.buf.p_ptr, chroma_div, NULL);
993
994             if (p_sys->api->release_out(p_sys->api, p_out->u.buf.i_index, false))
995             {
996                 picture_Release(p_pic);
997                 return -1;
998             }
999         }
1000         assert(!(*pp_out_pic));
1001         *pp_out_pic = p_pic;
1002         return 1;
1003     } else {
1004         assert(p_out->type == MC_OUT_TYPE_CONF);
1005         p_sys->u.video.i_pixel_format = p_out->u.conf.video.pixel_format;
1006
1007         const char *name = "unknown";
1008         if (p_sys->api->b_direct_rendering)
1009             p_dec->fmt_out.i_codec = VLC_CODEC_ANDROID_OPAQUE;
1010         else
1011         {
1012             if (!GetVlcChromaFormat(p_sys->u.video.i_pixel_format,
1013                                     &p_dec->fmt_out.i_codec, &name)) {
1014                 msg_Err(p_dec, "color-format not recognized");
1015                 return -1;
1016             }
1017         }
1018
1019         msg_Err(p_dec, "output: %d %s, %dx%d stride %d %d, crop %d %d %d %d",
1020                 p_sys->u.video.i_pixel_format, name, p_out->u.conf.video.width, p_out->u.conf.video.height,
1021                 p_out->u.conf.video.stride, p_out->u.conf.video.slice_height,
1022                 p_out->u.conf.video.crop_left, p_out->u.conf.video.crop_top,
1023                 p_out->u.conf.video.crop_right, p_out->u.conf.video.crop_bottom);
1024
1025         p_dec->fmt_out.video.i_width = p_out->u.conf.video.crop_right + 1 - p_out->u.conf.video.crop_left;
1026         p_dec->fmt_out.video.i_height = p_out->u.conf.video.crop_bottom + 1 - p_out->u.conf.video.crop_top;
1027         if (p_dec->fmt_out.video.i_width <= 1
1028             || p_dec->fmt_out.video.i_height <= 1) {
1029             p_dec->fmt_out.video.i_width = p_out->u.conf.video.width;
1030             p_dec->fmt_out.video.i_height = p_out->u.conf.video.height;
1031         }
1032         p_dec->fmt_out.video.i_visible_width = p_dec->fmt_out.video.i_width;
1033         p_dec->fmt_out.video.i_visible_height = p_dec->fmt_out.video.i_height;
1034
1035         p_sys->u.video.i_stride = p_out->u.conf.video.stride;
1036         p_sys->u.video.i_slice_height = p_out->u.conf.video.slice_height;
1037         if (p_sys->u.video.i_stride <= 0)
1038             p_sys->u.video.i_stride = p_out->u.conf.video.width;
1039         if (p_sys->u.video.i_slice_height <= 0)
1040             p_sys->u.video.i_slice_height = p_out->u.conf.video.height;
1041
1042         if (p_sys->u.video.i_pixel_format == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
1043             p_sys->u.video.i_slice_height -= p_out->u.conf.video.crop_top/2;
1044         if ((p_sys->api->i_quirks & MC_API_VIDEO_QUIRKS_IGNORE_PADDING))
1045         {
1046             p_sys->u.video.i_slice_height = 0;
1047             p_sys->u.video.i_stride = p_dec->fmt_out.video.i_width;
1048         }
1049         p_sys->b_update_format = true;
1050         p_sys->b_has_format = true;
1051         return 0;
1052     }
1053 }
1054
1055 /* samples will be in the following order: FL FR FC LFE BL BR BC SL SR */
1056 static uint32_t pi_audio_order_src[] =
1057 {
1058     AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, AOUT_CHAN_LFE,
1059     AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, AOUT_CHAN_REARCENTER,
1060     AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
1061 };
1062
1063 static int Audio_ProcessOutput(decoder_t *p_dec, mc_api_out *p_out,
1064                                picture_t **pp_out_pic, block_t **pp_out_block)
1065 {
1066     decoder_sys_t *p_sys = p_dec->p_sys;
1067     (void) pp_out_pic;
1068     assert(pp_out_block);
1069
1070     if (p_out->type == MC_OUT_TYPE_BUF)
1071     {
1072         block_t *p_block = NULL;
1073         if (p_out->u.buf.p_ptr == NULL)
1074         {
1075             /* This can happen when receiving an EOS buffer */
1076             msg_Warn(p_dec, "Invalid buffer, dropping frame");
1077             return p_sys->api->release_out(p_sys->api, p_out->u.buf.i_index, false);
1078         }
1079
1080         if (!p_sys->b_has_format) {
1081             msg_Warn(p_dec, "Buffers returned before output format is set, dropping frame");
1082             return p_sys->api->release_out(p_sys->api, p_out->u.buf.i_index, false);
1083         }
1084
1085         p_block = block_Alloc(p_out->u.buf.i_size);
1086         if (!p_block)
1087             return -1;
1088         p_block->i_nb_samples = p_out->u.buf.i_size
1089                               / p_dec->fmt_out.audio.i_bytes_per_frame;
1090
1091         if (p_sys->u.audio.b_extract)
1092         {
1093             aout_ChannelExtract(p_block->p_buffer,
1094                                 p_dec->fmt_out.audio.i_channels,
1095                                 p_out->u.buf.p_ptr, p_sys->u.audio.i_channels,
1096                                 p_block->i_nb_samples, p_sys->u.audio.pi_extraction,
1097                                 p_dec->fmt_out.audio.i_bitspersample);
1098         }
1099         else
1100             memcpy(p_block->p_buffer, p_out->u.buf.p_ptr, p_out->u.buf.i_size);
1101
1102         if (p_out->u.buf.i_ts != 0 && p_out->u.buf.i_ts != date_Get(&p_sys->u.audio.i_end_date))
1103             date_Set(&p_sys->u.audio.i_end_date, p_out->u.buf.i_ts);
1104
1105         p_block->i_pts = date_Get(&p_sys->u.audio.i_end_date);
1106         p_block->i_length = date_Increment(&p_sys->u.audio.i_end_date,
1107                                            p_block->i_nb_samples)
1108                           - p_block->i_pts;
1109
1110         if (p_sys->api->release_out(p_sys->api, p_out->u.buf.i_index, false))
1111         {
1112             block_Release(p_block);
1113             return -1;
1114         }
1115         *pp_out_block = p_block;
1116         return 1;
1117     } else {
1118         uint32_t i_layout_dst;
1119         int      i_channels_dst;
1120
1121         assert(p_out->type == MC_OUT_TYPE_CONF);
1122
1123         if (p_out->u.conf.audio.channel_count <= 0
1124          || p_out->u.conf.audio.channel_count > 8
1125          || p_out->u.conf.audio.sample_rate <= 0)
1126         {
1127             msg_Warn( p_dec, "invalid audio properties channels count %d, sample rate %d",
1128                       p_out->u.conf.audio.channel_count,
1129                       p_out->u.conf.audio.sample_rate);
1130             return -1;
1131         }
1132
1133         msg_Err(p_dec, "output: channel_count: %d, channel_mask: 0x%X, rate: %d",
1134                 p_out->u.conf.audio.channel_count, p_out->u.conf.audio.channel_mask,
1135                 p_out->u.conf.audio.sample_rate);
1136
1137         p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
1138         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1139
1140         p_dec->fmt_out.audio.i_rate = p_out->u.conf.audio.sample_rate;
1141         date_Init(&p_sys->u.audio.i_end_date, p_out->u.conf.audio.sample_rate, 1 );
1142
1143         p_sys->u.audio.i_channels = p_out->u.conf.audio.channel_count;
1144         p_sys->u.audio.b_extract =
1145             aout_CheckChannelExtraction(p_sys->u.audio.pi_extraction,
1146                                         &i_layout_dst, &i_channels_dst,
1147                                         NULL, pi_audio_order_src,
1148                                         p_sys->u.audio.i_channels);
1149
1150         if (p_sys->u.audio.b_extract)
1151             msg_Warn(p_dec, "need channel extraction: %d -> %d",
1152                      p_sys->u.audio.i_channels, i_channels_dst);
1153
1154         p_dec->fmt_out.audio.i_original_channels =
1155         p_dec->fmt_out.audio.i_physical_channels = i_layout_dst;
1156         aout_FormatPrepare(&p_dec->fmt_out.audio);
1157
1158         if (decoder_UpdateAudioFormat(p_dec))
1159             return -1;
1160
1161         p_sys->b_has_format = true;
1162         return 0;
1163     }
1164 }
1165
1166 static void DecodeFlushLocked(decoder_t *p_dec)
1167 {
1168     decoder_sys_t *p_sys = p_dec->p_sys;
1169     bool b_had_input = p_sys->b_input_dequeued;
1170
1171     p_sys->b_input_dequeued = false;
1172     p_sys->b_flush_out = true;
1173     p_sys->i_preroll_end = 0;
1174     p_sys->b_output_ready = false;
1175     /* Resend CODEC_CONFIG buffer after a flush */
1176     p_sys->i_csd_send = 0;
1177
1178     p_sys->pf_on_flush(p_dec);
1179
1180     if (b_had_input && p_sys->api->flush(p_sys->api) != VLC_SUCCESS)
1181     {
1182         AbortDecoderLocked(p_dec);
1183         return;
1184     }
1185
1186     vlc_cond_broadcast(&p_sys->cond);
1187
1188     while (!p_sys->b_aborted && p_sys->b_flush_out)
1189         vlc_cond_wait(&p_sys->dec_cond, &p_sys->lock);
1190 }
1191
1192 static void DecodeFlush(decoder_t *p_dec)
1193 {
1194     decoder_sys_t *p_sys = p_dec->p_sys;
1195
1196     vlc_mutex_lock(&p_sys->lock);
1197     DecodeFlushLocked(p_dec);
1198     vlc_mutex_unlock(&p_sys->lock);
1199 }
1200
1201 static void *OutThread(void *data)
1202 {
1203     decoder_t *p_dec = data;
1204     decoder_sys_t *p_sys = p_dec->p_sys;
1205
1206     vlc_mutex_lock(&p_sys->lock);
1207     mutex_cleanup_push(&p_sys->lock);
1208     for (;;)
1209     {
1210         int i_index;
1211
1212         /* Wait for output ready */
1213         while (!p_sys->b_flush_out && !p_sys->b_output_ready)
1214             vlc_cond_wait(&p_sys->cond, &p_sys->lock);
1215
1216         if (p_sys->b_flush_out)
1217         {
1218             /* Acknowledge flushed state */
1219             p_sys->b_flush_out = false;
1220             vlc_cond_broadcast(&p_sys->dec_cond);
1221             continue;
1222         }
1223
1224         int canc = vlc_savecancel();
1225
1226         vlc_mutex_unlock(&p_sys->lock);
1227
1228         /* Wait for an output buffer. This function returns when a new output
1229          * is available or if output is flushed. */
1230         i_index = p_sys->api->dequeue_out(p_sys->api, -1);
1231
1232         vlc_mutex_lock(&p_sys->lock);
1233
1234         /* Ignore dequeue_out errors caused by flush */
1235         if (p_sys->b_flush_out)
1236         {
1237             /* If i_index >= 0, Release it. There is no way to know if i_index
1238              * is owned by us, so don't check the error. */
1239             if (i_index >= 0)
1240                 p_sys->api->release_out(p_sys->api, i_index, false);
1241
1242             /* Parse output format/buffers even when we are flushing */
1243             if (i_index != MC_API_INFO_OUTPUT_FORMAT_CHANGED
1244              && i_index != MC_API_INFO_OUTPUT_BUFFERS_CHANGED)
1245             {
1246                 vlc_restorecancel(canc);
1247                 continue;
1248             }
1249         }
1250
1251         /* Process output returned by dequeue_out */
1252         if (i_index >= 0 || i_index == MC_API_INFO_OUTPUT_FORMAT_CHANGED
1253          || i_index == MC_API_INFO_OUTPUT_BUFFERS_CHANGED)
1254         {
1255             struct mc_api_out out;
1256             int i_ret = p_sys->api->get_out(p_sys->api, i_index, &out);
1257
1258             if (i_ret == 1)
1259             {
1260                 picture_t *p_pic = NULL;
1261                 block_t *p_block = NULL;
1262
1263                 if (p_sys->pf_process_output(p_dec, &out, &p_pic,
1264                                              &p_block) == -1 && !out.b_eos)
1265                 {
1266                     msg_Err(p_dec, "pf_process_output failed");
1267                     vlc_restorecancel(canc);
1268                     break;
1269                 }
1270                 if (p_pic)
1271                     decoder_QueueVideo(p_dec, p_pic);
1272                 else if (p_block)
1273                     decoder_QueueAudio(p_dec, p_block);
1274
1275                 if (out.b_eos)
1276                 {
1277                     msg_Warn(p_dec, "EOS received, abort OutThread");
1278                     vlc_restorecancel(canc);
1279                     break;
1280                 }
1281             } else if (i_ret != 0)
1282             {
1283                 msg_Err(p_dec, "get_out failed");
1284                 vlc_restorecancel(canc);
1285                 break;
1286             }
1287         }
1288         else
1289         {
1290             msg_Err(p_dec, "dequeue_out failed");
1291             vlc_restorecancel(canc);
1292             break;
1293         }
1294         vlc_restorecancel(canc);
1295     }
1296     msg_Warn(p_dec, "OutThread stopped");
1297
1298     /* Signal DecoderFlush that the output thread aborted */
1299     p_sys->b_aborted = true;
1300     vlc_cond_signal(&p_sys->dec_cond);
1301
1302     vlc_cleanup_pop();
1303     vlc_mutex_unlock(&p_sys->lock);
1304
1305     return NULL;
1306 }
1307
1308 static block_t *GetNextBlock(decoder_sys_t *p_sys, block_t *p_block)
1309 {
1310     if (p_sys->i_csd_send < p_sys->i_csd_count)
1311         return p_sys->pp_csd[p_sys->i_csd_send++];
1312     else
1313         return p_block;
1314 }
1315
1316 /**
1317  * DecodeCommon called from DecodeVideo or DecodeAudio.
1318  * It returns -1 in case of error, 0 otherwise.
1319  */
1320 static int DecodeCommon(decoder_t *p_dec, block_t **pp_block)
1321 {
1322     decoder_sys_t *p_sys = p_dec->p_sys;
1323     int i_flags = 0;
1324     int i_ret;
1325     bool b_dequeue_timeout = false;
1326     bool b_draining;
1327
1328     if (pp_block != NULL && *pp_block == NULL)
1329         return 0;
1330
1331     vlc_mutex_lock(&p_sys->lock);
1332
1333     if (p_sys->b_aborted)
1334         goto end;
1335
1336     if (pp_block != NULL)
1337     {
1338         block_t *p_block = *pp_block;
1339         b_draining = false;
1340         if (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED))
1341         {
1342             DecodeFlushLocked(p_dec);
1343             if (p_sys->b_aborted)
1344                 goto end;
1345             if (p_block->i_flags & BLOCK_FLAG_CORRUPTED)
1346                 goto end;
1347         }
1348
1349         /* Parse input block */
1350         if ((i_ret = p_sys->pf_on_new_block(p_dec, pp_block, &i_flags)) == 1)
1351         {
1352             if (i_flags & (NEWBLOCK_FLAG_FLUSH|NEWBLOCK_FLAG_RESTART))
1353             {
1354                 msg_Warn(p_dec, "Flushing from DecodeCommon");
1355
1356                 /* Flush before restart to unblock OutThread */
1357                 DecodeFlushLocked(p_dec);
1358                 if (p_sys->b_aborted)
1359                     goto end;
1360
1361                 if (i_flags & NEWBLOCK_FLAG_RESTART)
1362                 {
1363                     StopMediaCodec(p_dec);
1364
1365                     int i_ret = StartMediaCodec(p_dec);
1366                     switch (i_ret)
1367                     {
1368                     case VLC_SUCCESS:
1369                         msg_Warn(p_dec, "Restarted from DecodeCommon");
1370                         break;
1371                     case VLC_ENOOBJ:
1372                         break;
1373                     default:
1374                         msg_Err(p_dec, "StartMediaCodec failed");
1375                         AbortDecoderLocked(p_dec);
1376                         goto end;
1377                     }
1378                 }
1379             }
1380         }
1381         else
1382         {
1383             if (i_ret != 0)
1384             {
1385                 AbortDecoderLocked(p_dec);
1386                 msg_Err(p_dec, "pf_on_new_block failed");
1387             }
1388             goto end;
1389         }
1390     }
1391     else
1392     {
1393         /* No input block, decoder is draining */
1394         msg_Err(p_dec, "Decoder is draining");
1395         b_draining = true;
1396
1397         if (!p_sys->b_output_ready )
1398         {
1399             /* Output no ready, no need to drain */
1400             goto end;
1401         }
1402     }
1403
1404     /* Abort if MediaCodec is not yet started */
1405     if (!p_sys->api->b_started)
1406         goto end;
1407
1408     /* Queue CSD blocks and input blocks */
1409     block_t *p_block = NULL;
1410     while (b_draining || (p_block = GetNextBlock(p_sys, *pp_block)))
1411     {
1412         int i_index;
1413
1414         vlc_mutex_unlock(&p_sys->lock);
1415         /* Wait for an input buffer. This function returns when a new input
1416          * buffer is available or after 1sec of timeout. */
1417         i_index = p_sys->api->dequeue_in(p_sys->api,
1418                                          p_sys->api->b_direct_rendering ?
1419                                          INT64_C(1000000) : -1);
1420         vlc_mutex_lock(&p_sys->lock);
1421
1422         if (p_sys->b_aborted)
1423             goto end;
1424
1425         bool b_config = false;
1426         mtime_t i_ts = 0;
1427         p_sys->b_input_dequeued = true;
1428         const void *p_buf = NULL;
1429         size_t i_size = 0;
1430
1431         if (i_index >= 0)
1432         {
1433             assert(b_draining || p_block != NULL);
1434             if (p_block != NULL)
1435             {
1436                 b_config = (p_block->i_flags & BLOCK_FLAG_CSD);
1437                 if (!b_config)
1438                 {
1439                     i_ts = p_block->i_pts;
1440                     if (!i_ts && p_block->i_dts)
1441                         i_ts = p_block->i_dts;
1442                 }
1443                 p_buf = p_block->p_buffer;
1444                 i_size = p_block->i_buffer;
1445             }
1446
1447             if (p_sys->api->queue_in(p_sys->api, i_index, p_buf, i_size,
1448                                      i_ts, b_config) == 0)
1449             {
1450                 if (!b_config && p_block != NULL)
1451                 {
1452                     if (p_block->i_flags & BLOCK_FLAG_PREROLL )
1453                         p_sys->i_preroll_end = i_ts;
1454
1455                     /* One input buffer is queued, signal OutThread that will
1456                      * fetch output buffers */
1457                     p_sys->b_output_ready = true;
1458                     vlc_cond_broadcast(&p_sys->cond);
1459
1460                     assert(p_block == *pp_block),
1461                     block_Release(p_block);
1462                     *pp_block = NULL;
1463                 }
1464                 b_dequeue_timeout = false;
1465                 if (b_draining)
1466                     break;
1467             } else
1468             {
1469                 msg_Err(p_dec, "queue_in failed");
1470                 AbortDecoderLocked(p_dec);
1471                 goto end;
1472             }
1473         }
1474         else if (i_index == MC_API_INFO_TRYAGAIN)
1475         {
1476             /* HACK: When direct rendering is enabled, there is a possible
1477              * deadlock between the Decoder and the Vout. It happens when the
1478              * Vout is paused and when the Decoder is flushing. In that case,
1479              * the Vout won't release any output buffers, therefore MediaCodec
1480              * won't dequeue any input buffers. To work around this issue,
1481              * release all output buffers if DecodeCommon is waiting more than
1482              * 1sec for a new input buffer. */
1483             if (!b_dequeue_timeout)
1484             {
1485                 msg_Warn(p_dec, "Decoder stuck: invalidate all buffers");
1486                 InvalidateAllPictures(p_dec);
1487                 b_dequeue_timeout = true;
1488                 continue;
1489             }
1490             else
1491             {
1492                 msg_Err(p_dec, "dequeue_in timeout: no input available for 2secs");
1493                 AbortDecoderLocked(p_dec);
1494                 goto end;
1495             }
1496         }
1497         else
1498         {
1499             msg_Err(p_dec, "dequeue_in failed");
1500             AbortDecoderLocked(p_dec);
1501             goto end;
1502         }
1503     }
1504
1505     if (b_draining)
1506     {
1507         msg_Warn(p_dec, "EOS sent, waiting for OutThread");
1508
1509         /* Wait for the OutThread to stop (and process all remaining output
1510          * frames. Use a timeout here since we can't know if all decoders will
1511          * behave correctly. */
1512         mtime_t deadline = mdate() + INT64_C(1000000);
1513         while (!p_sys->b_aborted
1514             && vlc_cond_timedwait(&p_sys->dec_cond, &p_sys->lock, deadline) == 0);
1515
1516         if (!p_sys->b_aborted)
1517             msg_Err(p_dec, "OutThread timed out");
1518
1519         /* In case pf_decode is called again (it shouldn't happen) */
1520         p_sys->b_error_signaled = true;
1521
1522         vlc_mutex_unlock(&p_sys->lock);
1523         return 0;
1524     }
1525
1526 end:
1527     if (pp_block && *pp_block)
1528     {
1529         block_Release(*pp_block);
1530         *pp_block = NULL;
1531     }
1532     if (p_sys->b_aborted)
1533     {
1534         if (!p_sys->b_error_signaled) {
1535             /* Signal the error to the Java.
1536              * TODO: remove this when there is a decoder fallback */
1537             if (p_dec->fmt_in.i_cat == VIDEO_ES)
1538                 AWindowHandler_sendHardwareAccelerationError(VLC_OBJECT(p_dec),
1539                                                              p_sys->u.video.p_awh);
1540             p_sys->b_error_signaled = true;
1541             vlc_cond_broadcast(&p_sys->cond);
1542         }
1543         vlc_mutex_unlock(&p_sys->lock);
1544         return -1;
1545     }
1546     else
1547     {
1548         vlc_mutex_unlock(&p_sys->lock);
1549         return 0;
1550     }
1551 }
1552
1553 static int Video_OnNewBlock(decoder_t *p_dec, block_t **pp_block, int *p_flags)
1554 {
1555     decoder_sys_t *p_sys = p_dec->p_sys;
1556     block_t *p_block = *pp_block;
1557     VLC_UNUSED(p_flags);
1558
1559     if (p_block->i_flags & BLOCK_FLAG_INTERLACED_MASK
1560         && !p_sys->api->b_support_interlaced)
1561         return -1;
1562
1563     timestamp_FifoPut(p_sys->u.video.timestamp_fifo,
1564                       p_block->i_pts ? VLC_TS_INVALID : p_block->i_dts);
1565
1566     return 1;
1567 }
1568
1569 static int VideoH264_OnNewBlock(decoder_t *p_dec, block_t **pp_block,
1570                                 int *p_flags)
1571 {
1572     decoder_sys_t *p_sys = p_dec->p_sys;
1573     block_t *p_block = *pp_block;
1574     bool b_size_changed;
1575
1576     assert(p_dec->fmt_in.i_codec == VLC_CODEC_H264 && p_block);
1577
1578     if (p_sys->u.video.i_nal_length_size)
1579     {
1580         h264_AVC_to_AnnexB(p_block->p_buffer, p_block->i_buffer,
1581                                p_sys->u.video.i_nal_length_size);
1582     } else if (H264SetCSD(p_dec, p_block->p_buffer, p_block->i_buffer,
1583                           &b_size_changed) == VLC_SUCCESS)
1584     {
1585         if (b_size_changed || !p_sys->api->b_started)
1586         {
1587             if (p_sys->api->b_started)
1588                 msg_Err(p_dec, "SPS/PPS changed during playback and "
1589                         "video size are different. Restart it !");
1590             *p_flags |= NEWBLOCK_FLAG_RESTART;
1591         } else
1592         {
1593             msg_Err(p_dec, "SPS/PPS changed during playback. Flush it");
1594             *p_flags |= NEWBLOCK_FLAG_FLUSH;
1595         }
1596     }
1597
1598     return Video_OnNewBlock(p_dec, pp_block, p_flags);
1599 }
1600
1601 static int VideoHEVC_OnNewBlock(decoder_t *p_dec, block_t **pp_block,
1602                                 int *p_flags)
1603 {
1604     decoder_sys_t *p_sys = p_dec->p_sys;
1605     block_t *p_block = *pp_block;
1606
1607     assert(p_dec->fmt_in.i_codec == VLC_CODEC_HEVC && p_block);
1608
1609     if (p_sys->u.video.i_nal_length_size)
1610     {
1611         h264_AVC_to_AnnexB(p_block->p_buffer, p_block->i_buffer,
1612                                p_sys->u.video.i_nal_length_size);
1613     }
1614
1615     return Video_OnNewBlock(p_dec, pp_block, p_flags);
1616 }
1617
1618 static int VideoMP4V_OnNewBlock(decoder_t *p_dec, block_t **pp_block,
1619                                 int *p_flags)
1620 {
1621     decoder_sys_t *p_sys = p_dec->p_sys;
1622     block_t *p_block = *pp_block;
1623
1624     /* The first input sent to MediaCodec must be an I-Frame */
1625     if (!p_sys->u.video.b_first_mp4v_iframe)
1626     {
1627         if ((p_block->i_flags & BLOCK_FLAG_TYPE_I))
1628             p_sys->u.video.b_first_mp4v_iframe = true;
1629         else
1630         {
1631             static const uint8_t p_mp4v_startcode[3] = {0x00, 0x00, 0x01};
1632
1633             if (p_block->i_buffer > 6
1634              && memcmp(p_mp4v_startcode, p_block->p_buffer, 3) == 0
1635              && p_block->p_buffer[3] == 0xb6)
1636             {
1637                 bs_t s;
1638                 bs_init(&s, &p_block->p_buffer[4], 2);
1639                 if (bs_read(&s, 2) == 0)
1640                     p_sys->u.video.b_first_mp4v_iframe = true;
1641             }
1642         }
1643         if (!p_sys->u.video.b_first_mp4v_iframe)
1644              return 0; /* Drop current block */
1645     }
1646
1647     return Video_OnNewBlock(p_dec, pp_block, p_flags);
1648 }
1649
1650 static int VideoVC1_OnNewBlock(decoder_t *p_dec, block_t **pp_block,
1651                                int *p_flags)
1652 {
1653     block_t *p_block = *pp_block;
1654
1655     /* Adding frame start code */
1656     p_block = *pp_block = block_Realloc(p_block, 4, p_block->i_buffer);
1657     if (p_block == NULL)
1658         return VLC_ENOMEM;
1659     p_block->p_buffer[0] = 0x00;
1660     p_block->p_buffer[1] = 0x00;
1661     p_block->p_buffer[2] = 0x01;
1662     p_block->p_buffer[3] = 0x0d;
1663
1664     return Video_OnNewBlock(p_dec, pp_block, p_flags);
1665 }
1666
1667 static void Video_OnFlush(decoder_t *p_dec)
1668 {
1669     decoder_sys_t *p_sys = p_dec->p_sys;
1670
1671     timestamp_FifoEmpty(p_sys->u.video.timestamp_fifo);
1672     /* Invalidate all pictures that are currently in flight
1673      * since flushing make all previous indices returned by
1674      * MediaCodec invalid. */
1675     if (p_sys->api->b_direct_rendering)
1676         InvalidateAllPictures(p_dec);
1677 }
1678
1679 static picture_t *DecodeVideo(decoder_t *p_dec, block_t **pp_block)
1680 {
1681     DecodeCommon(p_dec, pp_block);
1682     return NULL;
1683 }
1684
1685 static int Audio_OnNewBlock(decoder_t *p_dec, block_t **pp_block, int *p_flags)
1686 {
1687     decoder_sys_t *p_sys = p_dec->p_sys;
1688     block_t *p_block = *pp_block;
1689     VLC_UNUSED(p_flags);
1690
1691     /* We've just started the stream, wait for the first PTS. */
1692     if (!date_Get(&p_sys->u.audio.i_end_date))
1693     {
1694         if (p_block->i_pts <= VLC_TS_INVALID)
1695             return 0;
1696         date_Set(&p_sys->u.audio.i_end_date, p_block->i_pts);
1697     }
1698
1699     return 1;
1700 }
1701
1702 static void Audio_OnFlush(decoder_t *p_dec)
1703 {
1704     decoder_sys_t *p_sys = p_dec->p_sys;
1705
1706     date_Set(&p_sys->u.audio.i_end_date, VLC_TS_INVALID);
1707 }
1708
1709 static block_t *DecodeAudio(decoder_t *p_dec, block_t **pp_block)
1710 {
1711     DecodeCommon(p_dec, pp_block);
1712     return NULL;
1713 }