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