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