]> git.sesse.net Git - vlc/blob - modules/codec/omxil/omxil.c
omxil: rpi: Fetch pixel aspect ratio from decoder.
[vlc] / modules / codec / omxil / omxil.c
1 /*****************************************************************************
2  * omxil.c: Video decoder module making use of OpenMAX IL components.
3  *****************************************************************************
4  * Copyright (C) 2010 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <limits.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 #include "../h264_nal.h"
39
40 #include "omxil.h"
41 #include "omxil_core.h"
42 #include "OMX_Broadcom.h"
43
44 #ifndef NDEBUG
45 # define OMXIL_EXTRA_DEBUG
46 #endif
47
48 #define SENTINEL_FLAG 0x10000
49
50 /* Defined in the broadcom version of OMX_Index.h */
51 #define OMX_IndexConfigRequestCallback 0x7f000063
52 #define OMX_IndexParamBrcmPixelAspectRatio 0x7f00004d
53
54 /* Defined in the broadcom version of OMX_Core.h */
55 #define OMX_EventParamOrConfigChanged 0x7F000001
56
57 /*****************************************************************************
58  * Local prototypes
59  *****************************************************************************/
60 static int  OpenDecoder( vlc_object_t * );
61 static int  OpenEncoder( vlc_object_t * );
62 static int  OpenGeneric( vlc_object_t *, bool b_encode );
63 static void CloseGeneric( vlc_object_t * );
64
65 static picture_t *DecodeVideo( decoder_t *, block_t ** );
66 static block_t *DecodeAudio ( decoder_t *, block_t ** );
67 static block_t *EncodeVideo( encoder_t *, picture_t * );
68
69 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE, OMX_PTR, OMX_EVENTTYPE,
70                                       OMX_U32, OMX_U32, OMX_PTR );
71 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE, OMX_PTR,
72                                          OMX_BUFFERHEADERTYPE * );
73 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE, OMX_PTR,
74                                         OMX_BUFFERHEADERTYPE * );
75
76 /*****************************************************************************
77  * Module descriptor
78  *****************************************************************************/
79 vlc_module_begin ()
80     set_description( N_("Audio/Video decoder (using OpenMAX IL)") )
81     set_category( CAT_INPUT )
82     set_subcategory( SUBCAT_INPUT_VCODEC )
83     set_section( N_("Decoding") , NULL )
84 #if defined(USE_IOMX)
85     /* For IOMX, don't enable it automatically via priorities,
86      * enable it only via the --codec iomx command line parameter when
87      * wanted. */
88     set_capability( "decoder", 0 )
89 #else
90     set_capability( "decoder", 80 )
91 #endif
92     set_callbacks( OpenDecoder, CloseGeneric )
93
94     add_submodule ()
95     set_section( N_("Encoding") , NULL )
96     set_description( N_("Video encoder (using OpenMAX IL)") )
97     set_capability( "encoder", 0 )
98     set_callbacks( OpenEncoder, CloseGeneric )
99 vlc_module_end ()
100
101 /*****************************************************************************
102  * ImplementationSpecificWorkarounds: place-holder for implementation
103  * specific workarounds
104  *****************************************************************************/
105 static OMX_ERRORTYPE ImplementationSpecificWorkarounds(decoder_t *p_dec,
106     OmxPort *p_port, es_format_t *p_fmt)
107 {
108     decoder_sys_t *p_sys = p_dec->p_sys;
109     OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
110     int i_profile = 0xFFFF, i_level = 0xFFFF;
111
112     /* Try to find out the profile of the video */
113     while(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
114           p_fmt->i_codec == VLC_CODEC_H264)
115     {
116         uint8_t *p = (uint8_t*)p_dec->fmt_in.p_extra;
117         if(!p || !p_dec->fmt_in.p_extra) break;
118
119         /* Check the profile / level */
120         if(p_dec->fmt_in.i_original_fourcc == VLC_FOURCC('a','v','c','1') &&
121            p[0] == 1)
122         {
123             if(p_dec->fmt_in.i_extra < 12) break;
124             p_sys->i_nal_size_length = 1 + (p[4]&0x03);
125             if( !(p[5]&0x1f) ) break;
126             p += 8;
127         }
128         else
129         {
130             if(p_dec->fmt_in.i_extra < 8) break;
131             if(!p[0] && !p[1] && !p[2] && p[3] == 1) p += 4;
132             else if(!p[0] && !p[1] && p[2] == 1) p += 3;
133             else break;
134         }
135
136         if( ((*p++)&0x1f) != 7) break;
137
138         /* Get profile/level out of first SPS */
139         i_profile = p[0];
140         i_level = p[2];
141         break;
142     }
143
144     if(!strcmp(p_sys->psz_component, "OMX.TI.Video.Decoder"))
145     {
146         if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
147            p_fmt->i_codec == VLC_CODEC_H264 &&
148            (i_profile != 66 || i_level > 30))
149         {
150             msg_Dbg(p_dec, "h264 profile/level not supported (0x%x, 0x%x)",
151                     i_profile, i_level);
152             return OMX_ErrorNotImplemented;
153         }
154
155         if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirOutput &&
156            p_fmt->i_codec == VLC_CODEC_I420)
157         {
158             /* I420 xvideo is slow on OMAP */
159             def->format.video.eColorFormat = OMX_COLOR_FormatCbYCrY;
160             GetVlcChromaFormat( def->format.video.eColorFormat,
161                                 &p_fmt->i_codec, 0 );
162             GetVlcChromaSizes( p_fmt->i_codec,
163                                def->format.video.nFrameWidth,
164                                def->format.video.nFrameHeight,
165                                &p_port->i_frame_size, &p_port->i_frame_stride,
166                                &p_port->i_frame_stride_chroma_div );
167             def->format.video.nStride = p_port->i_frame_stride;
168             def->nBufferSize = p_port->i_frame_size;
169         }
170     }
171     else if(!strcmp(p_sys->psz_component, "OMX.st.video_encoder"))
172     {
173         if(p_fmt->i_cat == VIDEO_ES)
174         {
175             /* Bellagio's encoder doesn't encode the framerate in Q16 */
176             def->format.video.xFramerate >>= 16;
177         }
178     }
179 #if 0 /* FIXME: doesn't apply for HP Touchpad */
180     else if (!strncmp(p_sys->psz_component, "OMX.qcom.video.decoder.",
181                       strlen("OMX.qcom.video.decoder")))
182     {
183         /* qdsp6 refuses buffer size larger than 450K on input port */
184         if (def->nBufferSize > 450 * 1024)
185         {
186             def->nBufferSize = 450 * 1024;
187             p_port->i_frame_size = def->nBufferSize;
188         }
189     }
190 #endif
191 #ifdef RPI_OMX
192     else if (!strcmp(p_sys->psz_component, "OMX.broadcom.video_decode"))
193     {
194         /* Clear these fields before setting parameters, to allow the codec
195          * fill in what it wants (instead of rejecting whatever happened to
196          * be there. */
197         def->format.video.nStride = def->format.video.nSliceHeight = 0;
198     }
199 #endif
200
201     return OMX_ErrorNone;
202 }
203
204 /*****************************************************************************
205  * SetPortDefinition: set definition of the omx port based on the vlc format
206  *****************************************************************************/
207 static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
208                                        es_format_t *p_fmt)
209 {
210     OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
211     OMX_ERRORTYPE omx_error;
212
213     omx_error = OMX_GetParameter(p_port->omx_handle,
214                                  OMX_IndexParamPortDefinition, def);
215     CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
216                 omx_error, ErrorToString(omx_error));
217
218     switch(p_fmt->i_cat)
219     {
220     case VIDEO_ES:
221         def->format.video.nFrameWidth = p_fmt->video.i_width;
222         def->format.video.nFrameHeight = p_fmt->video.i_height;
223         if(def->format.video.eCompressionFormat == OMX_VIDEO_CodingUnused)
224             def->format.video.nStride = def->format.video.nFrameWidth;
225         if( p_fmt->video.i_frame_rate > 0 &&
226             p_fmt->video.i_frame_rate_base > 0 )
227             def->format.video.xFramerate = (p_fmt->video.i_frame_rate << 16) /
228                 p_fmt->video.i_frame_rate_base;
229
230         if(def->eDir == OMX_DirInput || p_dec->p_sys->b_enc)
231         {
232             if (def->eDir == OMX_DirInput && p_dec->p_sys->b_enc)
233                 def->nBufferSize = def->format.video.nFrameWidth *
234                   def->format.video.nFrameHeight * 2;
235             p_port->i_frame_size = def->nBufferSize;
236
237             if(!GetOmxVideoFormat(p_fmt->i_codec,
238                                   &def->format.video.eCompressionFormat, 0) )
239             {
240                 if(!GetOmxChromaFormat(p_fmt->i_codec,
241                                        &def->format.video.eColorFormat, 0) )
242                 {
243                     omx_error = OMX_ErrorNotImplemented;
244                     CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
245                                 (char *)&p_fmt->i_codec );
246                 }
247                 GetVlcChromaSizes( p_fmt->i_codec,
248                                    def->format.video.nFrameWidth,
249                                    def->format.video.nFrameHeight,
250                                    &p_port->i_frame_size, &p_port->i_frame_stride,
251                                    &p_port->i_frame_stride_chroma_div );
252                 def->format.video.nStride = p_port->i_frame_stride;
253                 def->nBufferSize = p_port->i_frame_size;
254             }
255         }
256         else
257         {
258             if( !GetVlcChromaFormat( def->format.video.eColorFormat,
259                                      &p_fmt->i_codec, 0 ) )
260             {
261                 omx_error = OMX_ErrorNotImplemented;
262                 CHECK_ERROR(omx_error, "OMX color format %i not supported",
263                             (int)def->format.video.eColorFormat );
264             }
265             GetVlcChromaSizes( p_fmt->i_codec,
266                                def->format.video.nFrameWidth,
267                                def->format.video.nFrameHeight,
268                                &p_port->i_frame_size, &p_port->i_frame_stride,
269                                &p_port->i_frame_stride_chroma_div );
270             def->format.video.nStride = p_port->i_frame_stride;
271             if (p_port->i_frame_size > def->nBufferSize)
272                 def->nBufferSize = p_port->i_frame_size;
273         }
274         break;
275
276     case AUDIO_ES:
277         p_port->i_frame_size = def->nBufferSize;
278         if(def->eDir == OMX_DirInput)
279         {
280             if(!GetOmxAudioFormat(p_fmt->i_codec,
281                                   &def->format.audio.eEncoding, 0) )
282             {
283                 omx_error = OMX_ErrorNotImplemented;
284                 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
285                             (char *)&p_fmt->i_codec );
286             }
287         }
288         else
289         {
290             if( !OmxToVlcAudioFormat(def->format.audio.eEncoding,
291                                    &p_fmt->i_codec, 0 ) )
292             {
293                 omx_error = OMX_ErrorNotImplemented;
294                 CHECK_ERROR(omx_error, "OMX audio encoding %i not supported",
295                             (int)def->format.audio.eEncoding );
296             }
297         }
298         break;
299
300     default: return OMX_ErrorNotImplemented;
301     }
302
303     omx_error = ImplementationSpecificWorkarounds(p_dec, p_port, p_fmt);
304     CHECK_ERROR(omx_error, "ImplementationSpecificWorkarounds failed (%x : %s)",
305                 omx_error, ErrorToString(omx_error));
306
307     omx_error = OMX_SetParameter(p_port->omx_handle,
308                                  OMX_IndexParamPortDefinition, def);
309     CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
310                 omx_error, ErrorToString(omx_error));
311
312     omx_error = OMX_GetParameter(p_port->omx_handle,
313                                  OMX_IndexParamPortDefinition, def);
314     CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
315                 omx_error, ErrorToString(omx_error));
316
317     if(p_port->i_frame_size > def->nBufferSize)
318         def->nBufferSize = p_port->i_frame_size;
319     p_port->i_frame_size = def->nBufferSize;
320
321     /* Deal with audio params */
322     if(p_fmt->i_cat == AUDIO_ES)
323     {
324         omx_error = SetAudioParameters(p_port->omx_handle,
325                                        &p_port->format_param, def->nPortIndex,
326                                        def->format.audio.eEncoding,
327                                        p_fmt->i_codec,
328                                        p_fmt->audio.i_channels,
329                                        p_fmt->audio.i_rate,
330                                        p_fmt->i_bitrate,
331                                        p_fmt->audio.i_bitspersample,
332                                        p_fmt->audio.i_blockalign);
333         if (def->eDir == OMX_DirInput) {
334             CHECK_ERROR(omx_error, "SetAudioParameters failed (%x : %s)",
335                         omx_error, ErrorToString(omx_error));
336         } else if (omx_error != OMX_ErrorNone) {
337             msg_Warn(p_dec, "SetAudioParameters failed (%x : %s) on output port",
338                      omx_error, ErrorToString(omx_error));
339             omx_error = OMX_ErrorNone;
340         }
341     }
342     if (!strcmp(p_dec->p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER") &&
343                 def->eDir == OMX_DirOutput)
344     {
345         /* When setting the output buffer size above, the decoder actually
346          * sets the buffer size to a lower value than what was chosen. If
347          * we try to allocate buffers of this size, it fails. Thus, forcibly
348          * use a larger buffer size. */
349         def->nBufferSize *= 2;
350     }
351
352  error:
353     return omx_error;
354 }
355
356
357 /*****************************************************************************
358  * UpdatePixelAspect: Update vlc pixel aspect based on the aspect reported on
359  * the omx port - NOTE: Broadcom specific
360  *****************************************************************************/
361 static OMX_ERRORTYPE UpdatePixelAspect(decoder_t *p_dec)
362 {
363     decoder_sys_t *p_sys = p_dec->p_sys;
364     OMX_CONFIG_POINTTYPE pixel_aspect;
365     OMX_INIT_STRUCTURE(pixel_aspect);
366     OMX_ERRORTYPE omx_err;
367
368     if (strncmp(p_sys->psz_component, "OMX.broadcom.", 13))
369         return OMX_ErrorNotImplemented;
370
371     pixel_aspect.nPortIndex = p_sys->out.i_port_index;
372     omx_err = OMX_GetParameter(p_sys->omx_handle,
373             OMX_IndexParamBrcmPixelAspectRatio, &pixel_aspect);
374     if (omx_err != OMX_ErrorNone) {
375         msg_Warn(p_dec, "Failed to retrieve aspect ratio");
376     } else {
377         p_dec->fmt_out.video.i_sar_num = pixel_aspect.nX;
378         p_dec->fmt_out.video.i_sar_den = pixel_aspect.nY;
379     }
380
381     return omx_err;
382 }
383
384 /*****************************************************************************
385  * GetPortDefinition: set vlc format based on the definition of the omx port
386  *****************************************************************************/
387 static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
388                                        es_format_t *p_fmt)
389 {
390     decoder_sys_t *p_sys = p_dec->p_sys;
391     OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
392     OMX_ERRORTYPE omx_error;
393     OMX_CONFIG_RECTTYPE crop_rect;
394
395     omx_error = OMX_GetParameter(p_port->omx_handle,
396                                  OMX_IndexParamPortDefinition, def);
397     CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
398                 omx_error, ErrorToString(omx_error));
399
400     switch(p_fmt->i_cat)
401     {
402     case VIDEO_ES:
403         p_fmt->video.i_width = def->format.video.nFrameWidth;
404         p_fmt->video.i_visible_width = def->format.video.nFrameWidth;
405         p_fmt->video.i_height = def->format.video.nFrameHeight;
406         p_fmt->video.i_visible_height = def->format.video.nFrameHeight;
407         p_fmt->video.i_frame_rate = p_dec->fmt_in.video.i_frame_rate;
408         p_fmt->video.i_frame_rate_base = p_dec->fmt_in.video.i_frame_rate_base;
409
410         OMX_INIT_STRUCTURE(crop_rect);
411         crop_rect.nPortIndex = def->nPortIndex;
412         omx_error = OMX_GetConfig(p_port->omx_handle, OMX_IndexConfigCommonOutputCrop, &crop_rect);
413         if (omx_error == OMX_ErrorNone)
414         {
415             if (!def->format.video.nSliceHeight)
416                 def->format.video.nSliceHeight = def->format.video.nFrameHeight;
417             if (!def->format.video.nStride)
418                 def->format.video.nStride = def->format.video.nFrameWidth;
419             p_fmt->video.i_width = crop_rect.nWidth;
420             p_fmt->video.i_visible_width = crop_rect.nWidth;
421             p_fmt->video.i_height = crop_rect.nHeight;
422             p_fmt->video.i_visible_height = crop_rect.nHeight;
423             if (def->format.video.eColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
424                 def->format.video.nSliceHeight -= crop_rect.nTop/2;
425         }
426         else
427         {
428             /* Don't pass the error back to the caller, this isn't mandatory */
429             omx_error = OMX_ErrorNone;
430         }
431
432         /* Hack: Nexus One (stock firmware with binary OMX driver blob)
433          * claims to output 420Planar even though it in in practice is
434          * NV21. */
435         if(def->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar &&
436            !strncmp(p_sys->psz_component, "OMX.qcom.video.decoder",
437                     strlen("OMX.qcom.video.decoder")))
438             def->format.video.eColorFormat = OMX_QCOM_COLOR_FormatYVU420SemiPlanar;
439
440         if (IgnoreOmxDecoderPadding(p_sys->psz_component)) {
441             def->format.video.nSliceHeight = 0;
442             def->format.video.nStride = p_fmt->video.i_width;
443         }
444
445         if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
446                                &p_fmt->i_codec, 0 ) )
447         {
448             if( !GetVlcChromaFormat( def->format.video.eColorFormat,
449                                      &p_fmt->i_codec, 0 ) )
450             {
451                 omx_error = OMX_ErrorNotImplemented;
452                 CHECK_ERROR(omx_error, "OMX color format %i not supported",
453                             (int)def->format.video.eColorFormat );
454             }
455             GetVlcChromaSizes( p_fmt->i_codec,
456                                def->format.video.nFrameWidth,
457                                def->format.video.nFrameHeight,
458                                &p_port->i_frame_size, &p_port->i_frame_stride,
459                                &p_port->i_frame_stride_chroma_div );
460         }
461         if(p_port->i_frame_size > def->nBufferSize)
462             def->nBufferSize = p_port->i_frame_size;
463         p_port->i_frame_size = def->nBufferSize;
464 #if 0
465         if((int)p_port->i_frame_stride > def->format.video.nStride)
466             def->format.video.nStride = p_port->i_frame_stride;
467 #endif
468         p_port->i_frame_stride = def->format.video.nStride;
469         UpdatePixelAspect(p_dec);
470         break;
471
472     case AUDIO_ES:
473         if( !OmxToVlcAudioFormat( def->format.audio.eEncoding,
474                                 &p_fmt->i_codec, 0 ) )
475         {
476             omx_error = OMX_ErrorNotImplemented;
477             CHECK_ERROR(omx_error, "OMX audio format %i not supported",
478                         (int)def->format.audio.eEncoding );
479         }
480
481         omx_error = GetAudioParameters(p_port->omx_handle,
482                                        &p_port->format_param, def->nPortIndex,
483                                        def->format.audio.eEncoding,
484                                        &p_fmt->audio.i_channels,
485                                        &p_fmt->audio.i_rate,
486                                        &p_fmt->i_bitrate,
487                                        &p_fmt->audio.i_bitspersample,
488                                        &p_fmt->audio.i_blockalign);
489         CHECK_ERROR(omx_error, "GetAudioParameters failed (%x : %s)",
490                     omx_error, ErrorToString(omx_error));
491
492         if(p_fmt->audio.i_channels < 9)
493         {
494             static const int pi_channels_maps[9] =
495             {
496                 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
497                 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
498                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
499                 | AOUT_CHAN_REARRIGHT,
500                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
501                 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
502                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
503                 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
504                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
505                 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
506                 | AOUT_CHAN_MIDDLERIGHT,
507                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
508                 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
509                 | AOUT_CHAN_LFE
510             };
511             p_fmt->audio.i_physical_channels =
512                 p_fmt->audio.i_original_channels =
513                     pi_channels_maps[p_fmt->audio.i_channels];
514         }
515
516         date_Init( &p_dec->p_sys->end_date, p_fmt->audio.i_rate, 1 );
517
518         break;
519
520     default: return OMX_ErrorNotImplemented;
521     }
522
523  error:
524     return omx_error;
525 }
526
527 /*****************************************************************************
528  * DeinitialiseComponent: Deinitialise and unload an OMX component
529  *****************************************************************************/
530 static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
531                                            OMX_HANDLETYPE omx_handle)
532 {
533     decoder_sys_t *p_sys = p_dec->p_sys;
534     OMX_ERRORTYPE omx_error;
535     OMX_STATETYPE state;
536     unsigned int i, j;
537
538     if(!omx_handle) return OMX_ErrorNone;
539
540     omx_error = OMX_GetState(omx_handle, &state);
541     CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
542
543     if(state == OMX_StateExecuting)
544     {
545         omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
546                                      OMX_StateIdle, 0 );
547         CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
548         while (1) {
549             OMX_U32 cmd, state;
550             omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, &cmd, &state, 0);
551             CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
552             // The event queue can contain other OMX_EventCmdComplete items,
553             // such as for OMX_CommandFlush
554             if (cmd == OMX_CommandStateSet && state == OMX_StateIdle)
555                 break;
556         }
557     }
558
559     omx_error = OMX_GetState(omx_handle, &state);
560     CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
561
562     if(state == OMX_StateIdle)
563     {
564         omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
565                                      OMX_StateLoaded, 0 );
566         CHECK_ERROR(omx_error, "OMX_CommandStateSet Loaded failed (%x)", omx_error );
567
568         for(i = 0; i < p_sys->ports; i++)
569         {
570             OmxPort *p_port = &p_sys->p_ports[i];
571             OMX_BUFFERHEADERTYPE *p_buffer;
572
573             for(j = 0; j < p_port->i_buffers; j++)
574             {
575                 OMX_FIFO_GET(&p_port->fifo, p_buffer);
576                 if (p_buffer->nFlags & SENTINEL_FLAG) {
577                     free(p_buffer);
578                     j--;
579                     continue;
580                 }
581                 omx_error = OMX_FreeBuffer( omx_handle,
582                                             p_port->i_port_index, p_buffer );
583
584                 if(omx_error != OMX_ErrorNone) break;
585             }
586             CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
587                         omx_error, (int)p_port->i_port_index, j );
588             while (1) {
589                 OMX_FIFO_PEEK(&p_port->fifo, p_buffer);
590                 if (!p_buffer) break;
591
592                 OMX_FIFO_GET(&p_port->fifo, p_buffer);
593                 if (p_buffer->nFlags & SENTINEL_FLAG) {
594                     free(p_buffer);
595                     continue;
596                 }
597                 msg_Warn( p_dec, "Stray buffer left in fifo, %p", p_buffer );
598             }
599         }
600
601         omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
602         CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
603     }
604
605  error:
606     for(i = 0; i < p_sys->ports; i++)
607     {
608         OmxPort *p_port = &p_sys->p_ports[i];
609         free(p_port->pp_buffers);
610         p_port->pp_buffers = 0;
611     }
612     omx_error = pf_free_handle( omx_handle );
613     return omx_error;
614 }
615
616 /*****************************************************************************
617  * InitialiseComponent: Load and initialise an OMX component
618  *****************************************************************************/
619 static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
620     OMX_STRING psz_component, OMX_HANDLETYPE *p_handle)
621 {
622     static OMX_CALLBACKTYPE callbacks =
623         { OmxEventHandler, OmxEmptyBufferDone, OmxFillBufferDone };
624     decoder_sys_t *p_sys = p_dec->p_sys;
625     OMX_HANDLETYPE omx_handle;
626     OMX_ERRORTYPE omx_error;
627     unsigned int i;
628     OMX_U8 psz_role[OMX_MAX_STRINGNAME_SIZE];
629     OMX_PARAM_COMPONENTROLETYPE role;
630     OMX_PARAM_PORTDEFINITIONTYPE definition;
631     OMX_PORT_PARAM_TYPE param;
632
633     /* Load component */
634     omx_error = pf_get_handle( &omx_handle, psz_component, p_dec, &callbacks );
635     if(omx_error != OMX_ErrorNone)
636     {
637         msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
638                   omx_error, ErrorToString(omx_error) );
639         return omx_error;
640     }
641     strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
642
643     omx_error = OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
644     if(omx_error == OMX_ErrorNone)
645         msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
646     else
647         msg_Dbg(p_dec, "loaded component %s", psz_component);
648     PrintOmx(p_dec, omx_handle, OMX_ALL);
649
650     /* Set component role */
651     OMX_INIT_STRUCTURE(role);
652     strcpy((char*)role.cRole,
653            GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec : p_dec->fmt_in.i_codec,
654                       p_dec->fmt_in.i_cat, p_sys->b_enc));
655
656     omx_error = OMX_SetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
657                                  &role);
658     omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
659                                  &role);
660     if(omx_error == OMX_ErrorNone)
661         msg_Dbg(p_dec, "component standard role set to %s", role.cRole);
662
663     /* Find the input / output ports */
664     OMX_INIT_STRUCTURE(param);
665     OMX_INIT_STRUCTURE(definition);
666     omx_error = OMX_GetParameter(omx_handle, p_dec->fmt_in.i_cat == VIDEO_ES ?
667                                  OMX_IndexParamVideoInit : OMX_IndexParamAudioInit, &param);
668     if(omx_error != OMX_ErrorNone) {
669 #ifdef __ANDROID__
670         param.nPorts = 2;
671         param.nStartPortNumber = 0;
672 #else
673         param.nPorts = 0;
674 #endif
675     }
676
677     for(i = 0; i < param.nPorts; i++)
678     {
679         OmxPort *p_port;
680
681         /* Get port definition */
682         definition.nPortIndex = param.nStartPortNumber + i;
683         omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
684                                      &definition);
685         if(omx_error != OMX_ErrorNone) continue;
686
687         if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
688         else  p_port = &p_sys->out;
689
690         p_port->b_valid = true;
691         p_port->i_port_index = definition.nPortIndex;
692         p_port->definition = definition;
693         p_port->omx_handle = omx_handle;
694     }
695
696     if(!p_sys->in.b_valid || !p_sys->out.b_valid)
697     {
698         omx_error = OMX_ErrorInvalidComponent;
699         CHECK_ERROR(omx_error, "couldn't find an input and output port");
700     }
701
702     if(!strncmp(p_sys->psz_component, "OMX.SEC.", 8) &&
703        p_dec->fmt_in.i_cat == VIDEO_ES)
704     {
705         OMX_INDEXTYPE index;
706         omx_error = OMX_GetExtensionIndex(omx_handle, (OMX_STRING) "OMX.SEC.index.ThumbnailMode", &index);
707         if(omx_error == OMX_ErrorNone)
708         {
709             OMX_BOOL enable = OMX_TRUE;
710             omx_error = OMX_SetConfig(omx_handle, index, &enable);
711             CHECK_ERROR(omx_error, "Unable to set ThumbnailMode");
712         } else {
713             OMX_BOOL enable = OMX_TRUE;
714             /* Needed on Samsung Galaxy S II */
715             omx_error = OMX_SetConfig(omx_handle, OMX_IndexVendorSetYUV420pMode, &enable);
716             if (omx_error == OMX_ErrorNone)
717                 msg_Dbg(p_dec, "Set OMX_IndexVendorSetYUV420pMode successfully");
718             else
719                 msg_Dbg(p_dec, "Unable to set OMX_IndexVendorSetYUV420pMode: %x", omx_error);
720         }
721     }
722
723     if(!strncmp(p_sys->psz_component, "OMX.broadcom.", 13))
724     {
725         OMX_CONFIG_REQUESTCALLBACKTYPE notifications;
726         OMX_INIT_STRUCTURE(notifications);
727
728         notifications.nPortIndex = p_sys->out.i_port_index;
729         notifications.nIndex = OMX_IndexParamBrcmPixelAspectRatio;
730         notifications.bEnable = OMX_TRUE;
731
732         omx_error = OMX_SetParameter(omx_handle,
733                 OMX_IndexConfigRequestCallback, &notifications);
734         if (omx_error == OMX_ErrorNone)
735             msg_Dbg(p_dec, "Enabled aspect ratio notifications");
736         else
737             msg_Dbg(p_dec, "Could not enable aspect ratio notifications");
738     }
739
740     /* Set port definitions */
741     for(i = 0; i < p_sys->ports; i++)
742     {
743         omx_error = SetPortDefinition(p_dec, &p_sys->p_ports[i],
744                                       p_sys->p_ports[i].p_fmt);
745         if(omx_error != OMX_ErrorNone) goto error;
746     }
747
748     /* Allocate our array for the omx buffers and enable ports */
749     for(i = 0; i < p_sys->ports; i++)
750     {
751         OmxPort *p_port = &p_sys->p_ports[i];
752
753         p_port->pp_buffers =
754             malloc(p_port->definition.nBufferCountActual *
755                    sizeof(OMX_BUFFERHEADERTYPE*));
756         if(!p_port->pp_buffers)
757         {
758           omx_error = OMX_ErrorInsufficientResources;
759           CHECK_ERROR(omx_error, "memory allocation failed");
760         }
761         p_port->i_buffers = p_port->definition.nBufferCountActual;
762
763         /* Enable port */
764         if(!p_port->definition.bEnabled)
765         {
766             omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
767                                          p_port->i_port_index, NULL);
768             CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
769                         (int)p_port->i_port_index, omx_error );
770             omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
771             CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
772                         (int)p_port->i_port_index, omx_error );
773         }
774     }
775
776     *p_handle = omx_handle;
777     return OMX_ErrorNone;
778
779  error:
780     DeinitialiseComponent(p_dec, omx_handle);
781     *p_handle = 0;
782     return omx_error;
783 }
784
785 /*****************************************************************************
786  * OpenDecoder: Create the decoder instance
787  *****************************************************************************/
788 static int OpenDecoder( vlc_object_t *p_this )
789 {
790     decoder_t *p_dec = (decoder_t*)p_this;
791     int status;
792
793     if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
794         return VLC_EGENERIC;
795
796     status = OpenGeneric( p_this, false );
797     if(status != VLC_SUCCESS) return status;
798
799     p_dec->pf_decode_video = DecodeVideo;
800     p_dec->pf_decode_audio = DecodeAudio;
801
802     return VLC_SUCCESS;
803 }
804
805 /*****************************************************************************
806  * OpenEncoder: Create the encoder instance
807  *****************************************************************************/
808 static int OpenEncoder( vlc_object_t *p_this )
809 {
810     encoder_t *p_enc = (encoder_t*)p_this;
811     int status;
812
813     if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
814         return VLC_EGENERIC;
815
816     status = OpenGeneric( p_this, true );
817     if(status != VLC_SUCCESS) return status;
818
819     p_enc->pf_encode_video = EncodeVideo;
820
821     return VLC_SUCCESS;
822 }
823
824 /*****************************************************************************
825  * OpenGeneric: Create the generic decoder/encoder instance
826  *****************************************************************************/
827 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
828 {
829     decoder_t *p_dec = (decoder_t*)p_this;
830     decoder_sys_t *p_sys;
831     OMX_ERRORTYPE omx_error;
832     OMX_BUFFERHEADERTYPE *p_header;
833     unsigned int i, j;
834
835     if (InitOmxCore(p_this) != VLC_SUCCESS) {
836         return VLC_EGENERIC;
837     }
838
839     /* Allocate the memory needed to store the decoder's structure */
840     if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
841     {
842         DeinitOmxCore();
843         return VLC_ENOMEM;
844     }
845
846     /* Initialise the thread properties */
847     if(!b_encode)
848     {
849         p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
850         p_dec->fmt_out.video = p_dec->fmt_in.video;
851         p_dec->fmt_out.audio = p_dec->fmt_in.audio;
852         p_dec->fmt_out.i_codec = 0;
853
854         /* set default aspect of 1, if parser did not set it */
855         if (p_dec->fmt_out.video.i_sar_num == 0)
856             p_dec->fmt_out.video.i_sar_num = 1;
857         if (p_dec->fmt_out.video.i_sar_den == 0)
858             p_dec->fmt_out.video.i_sar_den = 1;
859     }
860     p_sys->b_enc = b_encode;
861     InitOmxEventQueue(&p_sys->event_queue);
862     vlc_mutex_init (&p_sys->in.fifo.lock);
863     vlc_cond_init (&p_sys->in.fifo.wait);
864     p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
865     p_sys->in.fifo.pp_last = &p_sys->in.fifo.p_first;
866     p_sys->in.b_direct = false;
867     p_sys->in.b_flushed = true;
868     p_sys->in.p_fmt = &p_dec->fmt_in;
869     vlc_mutex_init (&p_sys->out.fifo.lock);
870     vlc_cond_init (&p_sys->out.fifo.wait);
871     p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
872     p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
873     p_sys->out.b_direct = false;
874     p_sys->out.b_flushed = true;
875     p_sys->out.p_fmt = &p_dec->fmt_out;
876     p_sys->ports = 2;
877     p_sys->p_ports = &p_sys->in;
878     p_sys->b_use_pts = 1;
879
880     msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
881             (char *)&p_dec->fmt_out.i_codec);
882
883     /* Enumerate components and build a list of the one we want to try */
884     p_sys->components =
885         CreateComponentsList(p_this,
886              GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
887                         p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
888                         p_sys->b_enc), p_sys->ppsz_components);
889     if( !p_sys->components )
890     {
891         msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
892                   (char *)&p_dec->fmt_in.i_codec );
893         CloseGeneric(p_this);
894         return VLC_EGENERIC;
895     }
896
897     /* Try to load and initialise a component */
898     omx_error = OMX_ErrorUndefined;
899     for(i = 0; i < p_sys->components; i++)
900     {
901 #ifdef __ANDROID__
902         /* ignore OpenCore software codecs */
903         if (!strncmp(p_sys->ppsz_components[i], "OMX.PV.", 7))
904             continue;
905         /* The same sw codecs, renamed in ICS (perhaps also in honeycomb) */
906         if (!strncmp(p_sys->ppsz_components[i], "OMX.google.", 11))
907             continue;
908         /* This one has been seen on HTC One V - it behaves like it works,
909          * but FillBufferDone returns buffers filled with 0 bytes. The One V
910          * has got a working OMX.qcom.video.decoder.avc instead though. */
911         if (!strncmp(p_sys->ppsz_components[i], "OMX.ARICENT.", 12))
912             continue;
913         /* Codecs with DRM, that don't output plain YUV data but only
914          * support direct rendering where the output can't be intercepted. */
915         if (strstr(p_sys->ppsz_components[i], ".secure"))
916             continue;
917         /* Use VC1 decoder for WMV3 for now */
918         if (!strcmp(p_sys->ppsz_components[i], "OMX.SEC.WMV.Decoder"))
919             continue;
920         /* This decoder does work, but has an insane latency (leading to errors
921          * about "main audio output playback way too late" and dropped frames).
922          * At least Samsung Galaxy S III (where this decoder is present) has
923          * got another one, OMX.SEC.mp3.dec, that works well and has a
924          * sensible latency. (Also, even if that one isn't found, in general,
925          * using SW codecs is usually more than fast enough for MP3.) */
926         if (!strcmp(p_sys->ppsz_components[i], "OMX.SEC.MP3.Decoder"))
927             continue;
928         /* This codec should be able to handle both VC1 and WMV3, but
929          * for VC1 it doesn't output any buffers at all (in the way we use
930          * it) and for WMV3 it outputs plain black buffers. Thus ignore
931          * it until we can make it work properly. */
932         if (!strcmp(p_sys->ppsz_components[i], "OMX.Nvidia.vc1.decode"))
933             continue;
934 #endif
935         omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
936                                         &p_sys->omx_handle);
937         if(omx_error == OMX_ErrorNone) break;
938     }
939     CHECK_ERROR(omx_error, "no component could be initialised" );
940
941     /* Move component to Idle then Executing state */
942     OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
943     CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
944
945     /* Allocate omx buffers */
946     for(i = 0; i < p_sys->ports; i++)
947     {
948         OmxPort *p_port = &p_sys->p_ports[i];
949
950         for(j = 0; j < p_port->i_buffers; j++)
951         {
952 #if 0
953 #define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
954             char *p_buf = malloc(p_port->definition.nBufferSize +
955                                  p_port->definition.nBufferAlignment);
956             p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
957 #endif
958
959             if(p_port->b_direct)
960                 omx_error =
961                     OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
962                                    p_port->i_port_index, 0,
963                                    p_port->definition.nBufferSize, (void*)1);
964             else
965                 omx_error =
966                     OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
967                                         p_port->i_port_index, 0,
968                                         p_port->definition.nBufferSize);
969
970             if(omx_error != OMX_ErrorNone) break;
971             OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
972         }
973         p_port->i_buffers = j;
974         CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
975                     omx_error, (int)p_port->i_port_index, j );
976     }
977
978     omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
979     CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
980
981     omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
982                                  OMX_StateExecuting, 0);
983     CHECK_ERROR(omx_error, "OMX_CommandStateSet Executing failed (%x)", omx_error );
984     omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
985     CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
986
987     /* Send codec configuration data */
988     if( p_dec->fmt_in.i_extra )
989     {
990         OMX_FIFO_GET(&p_sys->in.fifo, p_header);
991         p_header->nFilledLen = p_dec->fmt_in.i_extra;
992
993         /* Convert H.264 NAL format to annex b */
994         if( p_sys->i_nal_size_length && !p_sys->in.b_direct )
995         {
996             p_header->nFilledLen = 0;
997             convert_sps_pps( p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
998                              p_header->pBuffer, p_header->nAllocLen,
999                              (uint32_t*) &p_header->nFilledLen, NULL );
1000         }
1001         else if(p_sys->in.b_direct)
1002         {
1003             p_header->pOutputPortPrivate = p_header->pBuffer;
1004             p_header->pBuffer = p_dec->fmt_in.p_extra;
1005         }
1006         else if (p_dec->fmt_in.i_codec == VLC_CODEC_WMV3 &&
1007                  p_dec->fmt_in.i_extra >= 4 &&
1008                  p_header->nAllocLen >= 36)
1009         {
1010             int profile;
1011             // According to OMX IL 1.2.0 spec (4.3.33.2), the codec config
1012             // data for VC-1 Main/Simple (aka WMV3) is according to table 265
1013             // in the VC-1 spec. Most of the fields are just set with placeholders
1014             // (like framerate, hrd_buffer/rate).
1015             static const uint8_t wmv3seq[] = {
1016                 0xff, 0xff, 0xff, 0xc5, // numframes=ffffff, marker byte
1017                 0x04, 0x00, 0x00, 0x00, // marker byte
1018                 0x00, 0x00, 0x00, 0x00, // struct C, almost equal to p_extra
1019                 0x00, 0x00, 0x00, 0x00, // struct A, vert size
1020                 0x00, 0x00, 0x00, 0x00, // struct A, horiz size
1021                 0x0c, 0x00, 0x00, 0x00, // marker byte
1022                 0xff, 0xff, 0x00, 0x80, // struct B, level=4, cbr=0, hrd_buffer=ffff
1023                 0xff, 0xff, 0x00, 0x00, // struct B, hrd_rate=ffff
1024                 0xff, 0xff, 0xff, 0xff, // struct B, framerate=ffffffff
1025             };
1026             p_header->nFilledLen = sizeof(wmv3seq);
1027             memcpy(p_header->pBuffer, wmv3seq, p_header->nFilledLen);
1028             // Struct C - almost equal to the extradata
1029             memcpy(&p_header->pBuffer[8], p_dec->fmt_in.p_extra, 4);
1030             // Expand profile from the highest 2 bits to the highest 4 bits
1031             profile = p_header->pBuffer[8] >> 6;
1032             p_header->pBuffer[8] = (p_header->pBuffer[8] & 0x0f) | (profile << 4);
1033             // Fill in the height/width for struct A
1034             SetDWLE(&p_header->pBuffer[12], p_dec->fmt_in.video.i_height);
1035             SetDWLE(&p_header->pBuffer[16], p_dec->fmt_in.video.i_width);
1036         }
1037         else
1038         {
1039             if(p_header->nFilledLen > p_header->nAllocLen)
1040             {
1041                 msg_Dbg(p_dec, "buffer too small (%i,%i)", (int)p_header->nFilledLen,
1042                         (int)p_header->nAllocLen);
1043                 p_header->nFilledLen = p_header->nAllocLen;
1044             }
1045             memcpy(p_header->pBuffer, p_dec->fmt_in.p_extra, p_header->nFilledLen);
1046         }
1047
1048         p_header->nOffset = 0;
1049         p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG | OMX_BUFFERFLAG_ENDOFFRAME;
1050         msg_Dbg(p_dec, "sending codec config data %p, %p, %i", p_header,
1051                 p_header->pBuffer, (int)p_header->nFilledLen);
1052         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1053     }
1054
1055     /* Get back output port definition */
1056     omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1057     if(omx_error != OMX_ErrorNone) goto error;
1058
1059     PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1060     PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1061
1062     if(p_sys->b_error) goto error;
1063
1064     p_dec->b_need_packetized = true;
1065
1066     if (!p_sys->b_use_pts)
1067         msg_Dbg( p_dec, "using dts timestamp mode for %s", p_sys->psz_component);
1068
1069     return VLC_SUCCESS;
1070
1071  error:
1072     CloseGeneric(p_this);
1073     return VLC_EGENERIC;
1074 }
1075
1076 /*****************************************************************************
1077  * PortReconfigure
1078  *****************************************************************************/
1079 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
1080 {
1081     decoder_sys_t *p_sys = p_dec->p_sys;
1082     OMX_PARAM_PORTDEFINITIONTYPE definition;
1083     OMX_BUFFERHEADERTYPE *p_buffer;
1084     OMX_ERRORTYPE omx_error;
1085     unsigned int i;
1086
1087     /* Sanity checking */
1088     OMX_INIT_STRUCTURE(definition);
1089     definition.nPortIndex = p_port->i_port_index;
1090     omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1091                                  &definition);
1092     if(omx_error != OMX_ErrorNone || (p_dec->fmt_in.i_cat == VIDEO_ES &&
1093        (!definition.format.video.nFrameWidth ||
1094        !definition.format.video.nFrameHeight)) )
1095         return OMX_ErrorUndefined;
1096
1097     omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
1098                                  p_port->i_port_index, NULL);
1099     CHECK_ERROR(omx_error, "OMX_CommandPortDisable on %i failed (%x)",
1100                 (int)p_port->i_port_index, omx_error );
1101
1102     for(i = 0; i < p_port->i_buffers; i++)
1103     {
1104         OMX_FIFO_GET(&p_port->fifo, p_buffer);
1105         if (p_buffer->pAppPrivate != NULL)
1106             decoder_DeletePicture( p_dec, p_buffer->pAppPrivate );
1107         if (p_buffer->nFlags & SENTINEL_FLAG) {
1108             free(p_buffer);
1109             i--;
1110             continue;
1111         }
1112         omx_error = OMX_FreeBuffer( p_sys->omx_handle,
1113                                     p_port->i_port_index, p_buffer );
1114
1115         if(omx_error != OMX_ErrorNone) break;
1116     }
1117     CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
1118                 omx_error, (int)p_port->i_port_index, i );
1119
1120     omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
1121     CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
1122
1123     /* Get the new port definition */
1124     omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1125     if(omx_error != OMX_ErrorNone) goto error;
1126
1127     if( p_dec->fmt_in.i_cat != AUDIO_ES )
1128     {
1129         /* Don't explicitly set the new parameters that we got with
1130          * OMX_GetParameter above when using audio codecs.
1131          * That struct hasn't been changed since, so there should be
1132          * no need to set it here, unless some codec expects the
1133          * SetParameter call as a trigger event for some part of
1134          * the reconfiguration.
1135          * This fixes using audio decoders on Samsung Galaxy S II,
1136          *
1137          * Only skipping this for audio codecs, to minimize the
1138          * change for current working configurations for video.
1139          */
1140         omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1141                                      &definition);
1142         CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
1143                     omx_error, ErrorToString(omx_error));
1144     }
1145
1146     omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
1147                                  p_port->i_port_index, NULL);
1148     CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
1149                 (int)p_port->i_port_index, omx_error );
1150
1151     if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
1152         free(p_port->pp_buffers);
1153         p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
1154         if(!p_port->pp_buffers)
1155         {
1156             omx_error = OMX_ErrorInsufficientResources;
1157             CHECK_ERROR(omx_error, "memory allocation failed");
1158         }
1159     }
1160     p_port->i_buffers = p_port->definition.nBufferCountActual;
1161     for(i = 0; i < p_port->i_buffers; i++)
1162     {
1163         if(p_port->b_direct)
1164             omx_error =
1165                 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1166                                p_port->i_port_index, 0,
1167                                p_port->definition.nBufferSize, (void*)1);
1168         else
1169             omx_error =
1170                 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1171                                     p_port->i_port_index, 0,
1172                                     p_port->definition.nBufferSize);
1173
1174         if(omx_error != OMX_ErrorNone) break;
1175         OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1176     }
1177     p_port->i_buffers = i;
1178     CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1179                 omx_error, (int)p_port->i_port_index, i );
1180
1181     omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
1182     CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1183
1184     PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1185     PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1186
1187  error:
1188     return omx_error;
1189 }
1190
1191 /*****************************************************************************
1192  * DecodeVideo: Called to decode one frame
1193  *****************************************************************************/
1194 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1195 {
1196     decoder_sys_t *p_sys = p_dec->p_sys;
1197     picture_t *p_pic = NULL, *p_next_pic;
1198     OMX_ERRORTYPE omx_error;
1199     unsigned int i;
1200
1201     OMX_BUFFERHEADERTYPE *p_header;
1202     block_t *p_block;
1203     int i_input_used = 0;
1204     struct H264ConvertState convert_state = { 0, 0 };
1205
1206     if( !pp_block || !*pp_block )
1207         return NULL;
1208
1209     p_block = *pp_block;
1210
1211     /* Check for errors from codec */
1212     if(p_sys->b_error)
1213     {
1214         msg_Dbg(p_dec, "error during decoding");
1215         block_Release( p_block );
1216         return 0;
1217     }
1218
1219     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1220     {
1221         block_Release( p_block );
1222         if(!p_sys->in.b_flushed)
1223         {
1224             msg_Dbg(p_dec, "flushing");
1225             OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1226                              p_sys->in.definition.nPortIndex, 0 );
1227         }
1228         p_sys->in.b_flushed = true;
1229         return NULL;
1230     }
1231
1232     /* Take care of decoded frames first */
1233     while(!p_pic)
1234     {
1235         OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1236         if(!p_header) break; /* No frame available */
1237
1238         if(p_sys->out.b_update_def)
1239         {
1240             omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1241             p_sys->out.b_update_def = 0;
1242         }
1243
1244         if(p_header->nFilledLen)
1245         {
1246             p_pic = p_header->pAppPrivate;
1247             if(!p_pic)
1248             {
1249                 /* We're not in direct rendering mode.
1250                  * Get a new picture and copy the content */
1251                 p_pic = decoder_NewPicture( p_dec );
1252
1253                 if (p_pic)
1254                     CopyOmxPicture(p_sys->out.definition.format.video.eColorFormat,
1255                                    p_pic, p_sys->out.definition.format.video.nSliceHeight,
1256                                    p_sys->out.i_frame_stride,
1257                                    p_header->pBuffer + p_header->nOffset,
1258                                    p_sys->out.i_frame_stride_chroma_div);
1259             }
1260
1261             if (p_pic)
1262                 p_pic->date = FromOmxTicks(p_header->nTimeStamp);
1263             p_header->nFilledLen = 0;
1264             p_header->pAppPrivate = 0;
1265         }
1266
1267         /* Get a new picture */
1268         if(p_sys->out.b_direct && !p_header->pAppPrivate)
1269         {
1270             p_next_pic = decoder_NewPicture( p_dec );
1271             if(!p_next_pic) break;
1272
1273             OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1274             p_header->pAppPrivate = p_next_pic;
1275             p_header->pInputPortPrivate = p_header->pBuffer;
1276             p_header->pBuffer = p_next_pic->p[0].p_pixels;
1277         }
1278         else
1279         {
1280             OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1281         }
1282
1283 #ifdef OMXIL_EXTRA_DEBUG
1284         msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1285 #endif
1286         OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1287     }
1288
1289 more_input:
1290     /* Send the input buffer to the component */
1291     OMX_FIFO_GET_TIMEOUT(&p_sys->in.fifo, p_header, 200000);
1292
1293     if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1294         free(p_header);
1295         goto reconfig;
1296     }
1297
1298     if(p_header)
1299     {
1300         bool decode_more = false;
1301         p_header->nFilledLen = p_block->i_buffer - i_input_used;
1302         p_header->nOffset = 0;
1303         p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1304         if (p_sys->b_use_pts && p_block->i_pts)
1305             p_header->nTimeStamp = ToOmxTicks(p_block->i_pts);
1306         else
1307             p_header->nTimeStamp = ToOmxTicks(p_block->i_dts);
1308
1309         /* In direct mode we pass the input pointer as is.
1310          * Otherwise we memcopy the data */
1311         if(p_sys->in.b_direct)
1312         {
1313             p_header->pOutputPortPrivate = p_header->pBuffer;
1314             p_header->pBuffer = p_block->p_buffer;
1315             p_header->pAppPrivate = p_block;
1316             i_input_used = p_header->nFilledLen;
1317         }
1318         else
1319         {
1320             if(p_header->nFilledLen > p_header->nAllocLen)
1321             {
1322                 p_header->nFilledLen = p_header->nAllocLen;
1323             }
1324             memcpy(p_header->pBuffer, p_block->p_buffer + i_input_used, p_header->nFilledLen);
1325             i_input_used += p_header->nFilledLen;
1326             if (i_input_used == p_block->i_buffer)
1327             {
1328                 block_Release(p_block);
1329             }
1330             else
1331             {
1332                 decode_more = true;
1333                 p_header->nFlags &= ~OMX_BUFFERFLAG_ENDOFFRAME;
1334             }
1335         }
1336
1337         /* Convert H.264 NAL format to annex b. Doesn't do anything if
1338          * i_nal_size_length == 0, which is the case for codecs other
1339          * than H.264 */
1340         convert_h264_to_annexb( p_header->pBuffer, p_header->nFilledLen,
1341                                 p_sys->i_nal_size_length, &convert_state );
1342 #ifdef OMXIL_EXTRA_DEBUG
1343         msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i, %"PRId64, p_header, p_header->pBuffer,
1344                  (int)p_header->nFilledLen, FromOmxTicks(p_header->nTimeStamp) );
1345 #endif
1346         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1347         p_sys->in.b_flushed = false;
1348         if (decode_more)
1349             goto more_input;
1350         else
1351             *pp_block = NULL; /* Avoid being fed the same packet again */
1352     }
1353
1354 reconfig:
1355     /* Handle the PortSettingsChanged events */
1356     for(i = 0; i < p_sys->ports; i++)
1357     {
1358         OmxPort *p_port = &p_sys->p_ports[i];
1359         if(p_port->b_reconfigure)
1360         {
1361             omx_error = PortReconfigure(p_dec, p_port);
1362             p_port->b_reconfigure = 0;
1363         }
1364         if(p_port->b_update_def)
1365         {
1366             omx_error = GetPortDefinition(p_dec, p_port, p_port->p_fmt);
1367             p_port->b_update_def = 0;
1368         }
1369     }
1370
1371     return p_pic;
1372 }
1373
1374 /*****************************************************************************
1375  * DecodeAudio: Called to decode one frame
1376  *****************************************************************************/
1377 block_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1378 {
1379     decoder_sys_t *p_sys = p_dec->p_sys;
1380     block_t *p_buffer = NULL;
1381     OMX_BUFFERHEADERTYPE *p_header;
1382     OMX_ERRORTYPE omx_error;
1383     block_t *p_block;
1384     unsigned int i;
1385
1386     if( !pp_block || !*pp_block ) return NULL;
1387
1388     p_block = *pp_block;
1389
1390     /* Check for errors from codec */
1391     if(p_sys->b_error)
1392     {
1393         msg_Dbg(p_dec, "error during decoding");
1394         block_Release( p_block );
1395         return 0;
1396     }
1397
1398     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1399     {
1400         block_Release( p_block );
1401         date_Set( &p_sys->end_date, 0 );
1402         if(!p_sys->in.b_flushed)
1403         {
1404             msg_Dbg(p_dec, "flushing");
1405             OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1406                              p_sys->in.definition.nPortIndex, 0 );
1407         }
1408         p_sys->in.b_flushed = true;
1409         return NULL;
1410     }
1411
1412     if( !date_Get( &p_sys->end_date ) )
1413     {
1414         if( !p_block->i_pts )
1415         {
1416             /* We've just started the stream, wait for the first PTS. */
1417             block_Release( p_block );
1418             return NULL;
1419         }
1420         date_Set( &p_sys->end_date, p_block->i_pts );
1421     }
1422
1423     /* Take care of decoded frames first */
1424     while(!p_buffer)
1425     {
1426         unsigned int i_samples = 0;
1427
1428         OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1429         if(!p_header) break; /* No frame available */
1430
1431         if (p_sys->out.p_fmt->audio.i_channels)
1432             i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1433         if(i_samples)
1434         {
1435             p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1436             if( !p_buffer ) break; /* No audio buffer available */
1437
1438             memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1439             p_header->nFilledLen = 0;
1440
1441             int64_t timestamp = FromOmxTicks(p_header->nTimeStamp);
1442             if( timestamp != 0 &&
1443                 timestamp != date_Get( &p_sys->end_date ) )
1444                 date_Set( &p_sys->end_date, timestamp );
1445
1446             p_buffer->i_pts = date_Get( &p_sys->end_date );
1447             p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1448                 p_buffer->i_pts;
1449         }
1450
1451 #ifdef OMXIL_EXTRA_DEBUG
1452         msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1453 #endif
1454         OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1455         OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1456     }
1457
1458
1459     /* Send the input buffer to the component */
1460     OMX_FIFO_GET_TIMEOUT(&p_sys->in.fifo, p_header, 200000);
1461
1462     if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1463         free(p_header);
1464         goto reconfig;
1465     }
1466
1467     if(p_header)
1468     {
1469         p_header->nFilledLen = p_block->i_buffer;
1470         p_header->nOffset = 0;
1471         p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1472         p_header->nTimeStamp = ToOmxTicks(p_block->i_dts);
1473
1474         /* In direct mode we pass the input pointer as is.
1475          * Otherwise we memcopy the data */
1476         if(p_sys->in.b_direct)
1477         {
1478             p_header->pOutputPortPrivate = p_header->pBuffer;
1479             p_header->pBuffer = p_block->p_buffer;
1480             p_header->pAppPrivate = p_block;
1481         }
1482         else
1483         {
1484             if(p_header->nFilledLen > p_header->nAllocLen)
1485             {
1486                 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1487                         (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1488                 p_header->nFilledLen = p_header->nAllocLen;
1489             }
1490             memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1491             block_Release(p_block);
1492         }
1493
1494 #ifdef OMXIL_EXTRA_DEBUG
1495         msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1496                  (int)p_header->nFilledLen );
1497 #endif
1498         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1499         p_sys->in.b_flushed = false;
1500         *pp_block = NULL; /* Avoid being fed the same packet again */
1501     }
1502
1503 reconfig:
1504     /* Handle the PortSettingsChanged events */
1505     for(i = 0; i < p_sys->ports; i++)
1506     {
1507         OmxPort *p_port = &p_sys->p_ports[i];
1508         if(!p_port->b_reconfigure) continue;
1509         p_port->b_reconfigure = 0;
1510         omx_error = PortReconfigure(p_dec, p_port);
1511     }
1512
1513     return p_buffer;
1514 }
1515
1516 /*****************************************************************************
1517  * EncodeVideo: Called to encode one frame
1518  *****************************************************************************/
1519 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1520 {
1521     decoder_t *p_dec = ( decoder_t *)p_enc;
1522     decoder_sys_t *p_sys = p_dec->p_sys;
1523     OMX_ERRORTYPE omx_error;
1524     unsigned int i;
1525
1526     OMX_BUFFERHEADERTYPE *p_header;
1527     block_t *p_block = 0;
1528
1529     if( !p_pic ) return NULL;
1530
1531     /* Check for errors from codec */
1532     if(p_sys->b_error)
1533     {
1534         msg_Dbg(p_dec, "error during encoding");
1535         return NULL;
1536     }
1537
1538     /* Send the input buffer to the component */
1539     OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1540     if(p_header)
1541     {
1542         /* In direct mode we pass the input pointer as is.
1543          * Otherwise we memcopy the data */
1544         if(p_sys->in.b_direct)
1545         {
1546             p_header->pOutputPortPrivate = p_header->pBuffer;
1547             p_header->pBuffer = p_pic->p[0].p_pixels;
1548         }
1549         else
1550         {
1551             CopyVlcPicture(p_dec, p_header, p_pic);
1552         }
1553
1554         p_header->nFilledLen = p_sys->in.i_frame_size;
1555         p_header->nOffset = 0;
1556         p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1557         p_header->nTimeStamp = ToOmxTicks(p_pic->date);
1558 #ifdef OMXIL_EXTRA_DEBUG
1559         msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1560                  (int)p_header->nFilledLen );
1561 #endif
1562         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1563         p_sys->in.b_flushed = false;
1564     }
1565
1566     /* Handle the PortSettingsChanged events */
1567     for(i = 0; i < p_sys->ports; i++)
1568     {
1569         OmxPort *p_port = &p_sys->p_ports[i];
1570         if(!p_port->b_reconfigure) continue;
1571         p_port->b_reconfigure = 0;
1572         omx_error = PortReconfigure(p_dec, p_port);
1573     }
1574
1575     /* Wait for the decoded frame */
1576     while(!p_block)
1577     {
1578         OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1579
1580         if(p_header->nFilledLen)
1581         {
1582             if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1583             {
1584                 /* TODO: need to store codec config */
1585                 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1586             }
1587
1588             p_block = p_header->pAppPrivate;
1589             if(!p_block)
1590             {
1591                 /* We're not in direct rendering mode.
1592                  * Get a new block and copy the content */
1593                 p_block = block_Alloc( p_header->nFilledLen );
1594                 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1595             }
1596
1597             p_block->i_buffer = p_header->nFilledLen;
1598             p_block->i_pts = p_block->i_dts = FromOmxTicks(p_header->nTimeStamp);
1599             p_header->nFilledLen = 0;
1600             p_header->pAppPrivate = 0;
1601         }
1602
1603 #ifdef OMXIL_EXTRA_DEBUG
1604         msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1605 #endif
1606         OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1607     }
1608
1609     msg_Dbg(p_dec, "done");
1610     return p_block;
1611 }
1612
1613 /*****************************************************************************
1614  * CloseGeneric: omxil decoder destruction
1615  *****************************************************************************/
1616 static void CloseGeneric( vlc_object_t *p_this )
1617 {
1618     decoder_t *p_dec = (decoder_t *)p_this;
1619     decoder_sys_t *p_sys = p_dec->p_sys;
1620
1621     if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1622
1623     DeinitOmxCore();
1624
1625     DeinitOmxEventQueue(&p_sys->event_queue);
1626     vlc_mutex_destroy (&p_sys->in.fifo.lock);
1627     vlc_cond_destroy (&p_sys->in.fifo.wait);
1628     vlc_mutex_destroy (&p_sys->out.fifo.lock);
1629     vlc_cond_destroy (&p_sys->out.fifo.wait);
1630
1631     free( p_sys );
1632 }
1633
1634 /*****************************************************************************
1635  * OmxEventHandler: 
1636  *****************************************************************************/
1637 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1638     OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1639     OMX_U32 data_2, OMX_PTR event_data )
1640 {
1641     decoder_t *p_dec = (decoder_t *)app_data;
1642     decoder_sys_t *p_sys = p_dec->p_sys;
1643     unsigned int i;
1644     (void)omx_handle;
1645
1646     PrintOmxEvent((vlc_object_t *) p_dec, event, data_1, data_2, event_data);
1647     switch (event)
1648     {
1649     case OMX_EventError:
1650         //p_sys->b_error = true;
1651         break;
1652
1653     case OMX_EventPortSettingsChanged:
1654         if( data_2 == 0 || data_2 == OMX_IndexParamPortDefinition ||
1655             data_2 == OMX_IndexParamAudioPcm )
1656         {
1657             OMX_BUFFERHEADERTYPE *sentinel;
1658             for(i = 0; i < p_sys->ports; i++)
1659                 if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1660                     p_sys->p_ports[i].b_reconfigure = true;
1661             sentinel = calloc(1, sizeof(*sentinel));
1662             if (sentinel) {
1663                 sentinel->nFlags = SENTINEL_FLAG;
1664                 OMX_FIFO_PUT(&p_sys->in.fifo, sentinel);
1665             }
1666         }
1667         else if( data_2 == OMX_IndexConfigCommonOutputCrop )
1668         {
1669             for(i = 0; i < p_sys->ports; i++)
1670                 if(p_sys->p_ports[i].definition.nPortIndex == data_1)
1671                     p_sys->p_ports[i].b_update_def = true;
1672         }
1673         else
1674         {
1675             msg_Dbg( p_dec, "Unhandled setting change %x", (unsigned int)data_2 );
1676         }
1677         break;
1678     case OMX_EventParamOrConfigChanged:
1679         UpdatePixelAspect(p_dec);
1680         break;
1681
1682     default:
1683         break;
1684     }
1685
1686     PostOmxEvent(&p_sys->event_queue, event, data_1, data_2, event_data);
1687     return OMX_ErrorNone;
1688 }
1689
1690 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1691     OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1692 {
1693     decoder_t *p_dec = (decoder_t *)app_data;
1694     decoder_sys_t *p_sys = p_dec->p_sys;
1695     (void)omx_handle;
1696
1697 #ifdef OMXIL_EXTRA_DEBUG
1698     msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1699 #endif
1700
1701     if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1702     {
1703         block_t *p_block = (block_t *)omx_header->pAppPrivate;
1704         omx_header->pBuffer = omx_header->pOutputPortPrivate;
1705         if(p_block) block_Release(p_block);
1706         omx_header->pAppPrivate = 0;
1707     }
1708     OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1709
1710     return OMX_ErrorNone;
1711 }
1712
1713 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1714     OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1715 {
1716     decoder_t *p_dec = (decoder_t *)app_data;
1717     decoder_sys_t *p_sys = p_dec->p_sys;
1718     (void)omx_handle;
1719
1720 #ifdef OMXIL_EXTRA_DEBUG
1721     msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i, %"PRId64, omx_header, omx_header->pBuffer,
1722              (int)omx_header->nFilledLen, FromOmxTicks(omx_header->nTimeStamp) );
1723 #endif
1724
1725     if(omx_header->pInputPortPrivate)
1726     {
1727         omx_header->pBuffer = omx_header->pInputPortPrivate;
1728     }
1729     OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1730
1731     return OMX_ErrorNone;
1732 }