]> git.sesse.net Git - vlc/blob - modules/codec/omxil/omxil.c
omxil: Release picture in direct rendering mode.
[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->pAppPrivate != NULL)
1051             decoder_DeletePicture( p_dec, p_buffer->pAppPrivate );
1052         if (p_buffer->nFlags & SENTINEL_FLAG) {
1053             free(p_buffer);
1054             i--;
1055             continue;
1056         }
1057         omx_error = OMX_FreeBuffer( p_sys->omx_handle,
1058                                     p_port->i_port_index, p_buffer );
1059
1060         if(omx_error != OMX_ErrorNone) break;
1061     }
1062     CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
1063                 omx_error, (int)p_port->i_port_index, i );
1064
1065     omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
1066     CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
1067
1068     /* Get the new port definition */
1069     omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1070     if(omx_error != OMX_ErrorNone) goto error;
1071
1072     if( p_dec->fmt_in.i_cat != AUDIO_ES )
1073     {
1074         /* Don't explicitly set the new parameters that we got with
1075          * OMX_GetParameter above when using audio codecs.
1076          * That struct hasn't been changed since, so there should be
1077          * no need to set it here, unless some codec expects the
1078          * SetParameter call as a trigger event for some part of
1079          * the reconfiguration.
1080          * This fixes using audio decoders on Samsung Galaxy S II,
1081          *
1082          * Only skipping this for audio codecs, to minimize the
1083          * change for current working configurations for video.
1084          */
1085         omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1086                                      &definition);
1087         CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
1088                     omx_error, ErrorToString(omx_error));
1089     }
1090
1091     omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
1092                                  p_port->i_port_index, NULL);
1093     CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
1094                 (int)p_port->i_port_index, omx_error );
1095
1096     if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
1097         free(p_port->pp_buffers);
1098         p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
1099         if(!p_port->pp_buffers)
1100         {
1101             omx_error = OMX_ErrorInsufficientResources;
1102             CHECK_ERROR(omx_error, "memory allocation failed");
1103         }
1104     }
1105     p_port->i_buffers = p_port->definition.nBufferCountActual;
1106     for(i = 0; i < p_port->i_buffers; i++)
1107     {
1108         if(p_port->b_direct)
1109             omx_error =
1110                 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1111                                p_port->i_port_index, 0,
1112                                p_port->definition.nBufferSize, (void*)1);
1113         else
1114             omx_error =
1115                 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1116                                     p_port->i_port_index, 0,
1117                                     p_port->definition.nBufferSize);
1118
1119         if(omx_error != OMX_ErrorNone) break;
1120         OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1121     }
1122     p_port->i_buffers = i;
1123     CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1124                 omx_error, (int)p_port->i_port_index, i );
1125
1126     omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
1127     CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1128
1129     PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1130     PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1131
1132  error:
1133     return omx_error;
1134 }
1135
1136 /*****************************************************************************
1137  * DecodeVideo: Called to decode one frame
1138  *****************************************************************************/
1139 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1140 {
1141     decoder_sys_t *p_sys = p_dec->p_sys;
1142     picture_t *p_pic = NULL, *p_next_pic;
1143     OMX_ERRORTYPE omx_error;
1144     unsigned int i;
1145
1146     OMX_BUFFERHEADERTYPE *p_header;
1147     block_t *p_block;
1148     int i_input_used = 0;
1149     struct H264ConvertState convert_state = { 0, 0 };
1150
1151     if( !pp_block || !*pp_block )
1152         return NULL;
1153
1154     p_block = *pp_block;
1155
1156     /* Check for errors from codec */
1157     if(p_sys->b_error)
1158     {
1159         msg_Dbg(p_dec, "error during decoding");
1160         block_Release( p_block );
1161         return 0;
1162     }
1163
1164     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1165     {
1166         block_Release( p_block );
1167         if(!p_sys->in.b_flushed)
1168         {
1169             msg_Dbg(p_dec, "flushing");
1170             OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1171                              p_sys->in.definition.nPortIndex, 0 );
1172         }
1173         p_sys->in.b_flushed = true;
1174         return NULL;
1175     }
1176
1177     /* Take care of decoded frames first */
1178     while(!p_pic)
1179     {
1180         OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1181         if(!p_header) break; /* No frame available */
1182
1183         if(p_sys->out.b_update_def)
1184         {
1185             omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1186             p_sys->out.b_update_def = 0;
1187         }
1188
1189         if(p_header->nFilledLen)
1190         {
1191             p_pic = p_header->pAppPrivate;
1192             if(!p_pic)
1193             {
1194                 /* We're not in direct rendering mode.
1195                  * Get a new picture and copy the content */
1196                 p_pic = decoder_NewPicture( p_dec );
1197
1198                 if (p_pic)
1199                     CopyOmxPicture(p_sys->out.definition.format.video.eColorFormat,
1200                                    p_pic, p_sys->out.definition.format.video.nSliceHeight,
1201                                    p_sys->out.i_frame_stride,
1202                                    p_header->pBuffer + p_header->nOffset,
1203                                    p_sys->out.i_frame_stride_chroma_div);
1204             }
1205
1206             if (p_pic)
1207                 p_pic->date = FromOmxTicks(p_header->nTimeStamp);
1208             p_header->nFilledLen = 0;
1209             p_header->pAppPrivate = 0;
1210         }
1211
1212         /* Get a new picture */
1213         if(p_sys->out.b_direct && !p_header->pAppPrivate)
1214         {
1215             p_next_pic = decoder_NewPicture( p_dec );
1216             if(!p_next_pic) break;
1217
1218             OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1219             p_header->pAppPrivate = p_next_pic;
1220             p_header->pInputPortPrivate = p_header->pBuffer;
1221             p_header->pBuffer = p_next_pic->p[0].p_pixels;
1222         }
1223         else
1224         {
1225             OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1226         }
1227
1228 #ifdef OMXIL_EXTRA_DEBUG
1229         msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1230 #endif
1231         OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1232     }
1233
1234 more_input:
1235     /* Send the input buffer to the component */
1236     OMX_FIFO_GET_TIMEOUT(&p_sys->in.fifo, p_header, 200000);
1237
1238     if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1239         free(p_header);
1240         goto reconfig;
1241     }
1242
1243     if(p_header)
1244     {
1245         bool decode_more = false;
1246         p_header->nFilledLen = p_block->i_buffer - i_input_used;
1247         p_header->nOffset = 0;
1248         p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1249         if (p_sys->b_use_pts && p_block->i_pts)
1250             p_header->nTimeStamp = ToOmxTicks(p_block->i_pts);
1251         else
1252             p_header->nTimeStamp = ToOmxTicks(p_block->i_dts);
1253
1254         /* In direct mode we pass the input pointer as is.
1255          * Otherwise we memcopy the data */
1256         if(p_sys->in.b_direct)
1257         {
1258             p_header->pOutputPortPrivate = p_header->pBuffer;
1259             p_header->pBuffer = p_block->p_buffer;
1260             p_header->pAppPrivate = p_block;
1261             i_input_used = p_header->nFilledLen;
1262         }
1263         else
1264         {
1265             if(p_header->nFilledLen > p_header->nAllocLen)
1266             {
1267                 p_header->nFilledLen = p_header->nAllocLen;
1268             }
1269             memcpy(p_header->pBuffer, p_block->p_buffer + i_input_used, p_header->nFilledLen);
1270             i_input_used += p_header->nFilledLen;
1271             if (i_input_used == p_block->i_buffer)
1272             {
1273                 block_Release(p_block);
1274             }
1275             else
1276             {
1277                 decode_more = true;
1278                 p_header->nFlags &= ~OMX_BUFFERFLAG_ENDOFFRAME;
1279             }
1280         }
1281
1282         /* Convert H.264 NAL format to annex b. Doesn't do anything if
1283          * i_nal_size_length == 0, which is the case for codecs other
1284          * than H.264 */
1285         convert_h264_to_annexb( p_header->pBuffer, p_header->nFilledLen,
1286                                 p_sys->i_nal_size_length, &convert_state );
1287 #ifdef OMXIL_EXTRA_DEBUG
1288         msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1289                  (int)p_header->nFilledLen );
1290 #endif
1291         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1292         p_sys->in.b_flushed = false;
1293         if (decode_more)
1294             goto more_input;
1295         else
1296             *pp_block = NULL; /* Avoid being fed the same packet again */
1297     }
1298
1299 reconfig:
1300     /* Handle the PortSettingsChanged events */
1301     for(i = 0; i < p_sys->ports; i++)
1302     {
1303         OmxPort *p_port = &p_sys->p_ports[i];
1304         if(p_port->b_reconfigure)
1305         {
1306             omx_error = PortReconfigure(p_dec, p_port);
1307             p_port->b_reconfigure = 0;
1308         }
1309         if(p_port->b_update_def)
1310         {
1311             omx_error = GetPortDefinition(p_dec, p_port, p_port->p_fmt);
1312             p_port->b_update_def = 0;
1313         }
1314     }
1315
1316     return p_pic;
1317 }
1318
1319 /*****************************************************************************
1320  * DecodeAudio: Called to decode one frame
1321  *****************************************************************************/
1322 block_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1323 {
1324     decoder_sys_t *p_sys = p_dec->p_sys;
1325     block_t *p_buffer = NULL;
1326     OMX_BUFFERHEADERTYPE *p_header;
1327     OMX_ERRORTYPE omx_error;
1328     block_t *p_block;
1329     unsigned int i;
1330
1331     if( !pp_block || !*pp_block ) return NULL;
1332
1333     p_block = *pp_block;
1334
1335     /* Check for errors from codec */
1336     if(p_sys->b_error)
1337     {
1338         msg_Dbg(p_dec, "error during decoding");
1339         block_Release( p_block );
1340         return 0;
1341     }
1342
1343     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1344     {
1345         block_Release( p_block );
1346         date_Set( &p_sys->end_date, 0 );
1347         if(!p_sys->in.b_flushed)
1348         {
1349             msg_Dbg(p_dec, "flushing");
1350             OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1351                              p_sys->in.definition.nPortIndex, 0 );
1352         }
1353         p_sys->in.b_flushed = true;
1354         return NULL;
1355     }
1356
1357     if( !date_Get( &p_sys->end_date ) )
1358     {
1359         if( !p_block->i_pts )
1360         {
1361             /* We've just started the stream, wait for the first PTS. */
1362             block_Release( p_block );
1363             return NULL;
1364         }
1365         date_Set( &p_sys->end_date, p_block->i_pts );
1366     }
1367
1368     /* Take care of decoded frames first */
1369     while(!p_buffer)
1370     {
1371         unsigned int i_samples = 0;
1372
1373         OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1374         if(!p_header) break; /* No frame available */
1375
1376         if (p_sys->out.p_fmt->audio.i_channels)
1377             i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1378         if(i_samples)
1379         {
1380             p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1381             if( !p_buffer ) break; /* No audio buffer available */
1382
1383             memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1384             p_header->nFilledLen = 0;
1385
1386             int64_t timestamp = FromOmxTicks(p_header->nTimeStamp);
1387             if( timestamp != 0 &&
1388                 timestamp != date_Get( &p_sys->end_date ) )
1389                 date_Set( &p_sys->end_date, timestamp );
1390
1391             p_buffer->i_pts = date_Get( &p_sys->end_date );
1392             p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1393                 p_buffer->i_pts;
1394         }
1395
1396 #ifdef OMXIL_EXTRA_DEBUG
1397         msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1398 #endif
1399         OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1400         OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1401     }
1402
1403
1404     /* Send the input buffer to the component */
1405     OMX_FIFO_GET_TIMEOUT(&p_sys->in.fifo, p_header, 200000);
1406
1407     if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1408         free(p_header);
1409         goto reconfig;
1410     }
1411
1412     if(p_header)
1413     {
1414         p_header->nFilledLen = p_block->i_buffer;
1415         p_header->nOffset = 0;
1416         p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1417         p_header->nTimeStamp = ToOmxTicks(p_block->i_dts);
1418
1419         /* In direct mode we pass the input pointer as is.
1420          * Otherwise we memcopy the data */
1421         if(p_sys->in.b_direct)
1422         {
1423             p_header->pOutputPortPrivate = p_header->pBuffer;
1424             p_header->pBuffer = p_block->p_buffer;
1425             p_header->pAppPrivate = p_block;
1426         }
1427         else
1428         {
1429             if(p_header->nFilledLen > p_header->nAllocLen)
1430             {
1431                 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1432                         (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1433                 p_header->nFilledLen = p_header->nAllocLen;
1434             }
1435             memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1436             block_Release(p_block);
1437         }
1438
1439 #ifdef OMXIL_EXTRA_DEBUG
1440         msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1441                  (int)p_header->nFilledLen );
1442 #endif
1443         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1444         p_sys->in.b_flushed = false;
1445         *pp_block = NULL; /* Avoid being fed the same packet again */
1446     }
1447
1448 reconfig:
1449     /* Handle the PortSettingsChanged events */
1450     for(i = 0; i < p_sys->ports; i++)
1451     {
1452         OmxPort *p_port = &p_sys->p_ports[i];
1453         if(!p_port->b_reconfigure) continue;
1454         p_port->b_reconfigure = 0;
1455         omx_error = PortReconfigure(p_dec, p_port);
1456     }
1457
1458     return p_buffer;
1459 }
1460
1461 /*****************************************************************************
1462  * EncodeVideo: Called to encode one frame
1463  *****************************************************************************/
1464 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1465 {
1466     decoder_t *p_dec = ( decoder_t *)p_enc;
1467     decoder_sys_t *p_sys = p_dec->p_sys;
1468     OMX_ERRORTYPE omx_error;
1469     unsigned int i;
1470
1471     OMX_BUFFERHEADERTYPE *p_header;
1472     block_t *p_block = 0;
1473
1474     if( !p_pic ) return NULL;
1475
1476     /* Check for errors from codec */
1477     if(p_sys->b_error)
1478     {
1479         msg_Dbg(p_dec, "error during encoding");
1480         return NULL;
1481     }
1482
1483     /* Send the input buffer to the component */
1484     OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1485     if(p_header)
1486     {
1487         /* In direct mode we pass the input pointer as is.
1488          * Otherwise we memcopy the data */
1489         if(p_sys->in.b_direct)
1490         {
1491             p_header->pOutputPortPrivate = p_header->pBuffer;
1492             p_header->pBuffer = p_pic->p[0].p_pixels;
1493         }
1494         else
1495         {
1496             CopyVlcPicture(p_dec, p_header, p_pic);
1497         }
1498
1499         p_header->nFilledLen = p_sys->in.i_frame_size;
1500         p_header->nOffset = 0;
1501         p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1502         p_header->nTimeStamp = ToOmxTicks(p_pic->date);
1503 #ifdef OMXIL_EXTRA_DEBUG
1504         msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1505                  (int)p_header->nFilledLen );
1506 #endif
1507         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1508         p_sys->in.b_flushed = false;
1509     }
1510
1511     /* Handle the PortSettingsChanged events */
1512     for(i = 0; i < p_sys->ports; i++)
1513     {
1514         OmxPort *p_port = &p_sys->p_ports[i];
1515         if(!p_port->b_reconfigure) continue;
1516         p_port->b_reconfigure = 0;
1517         omx_error = PortReconfigure(p_dec, p_port);
1518     }
1519
1520     /* Wait for the decoded frame */
1521     while(!p_block)
1522     {
1523         OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1524
1525         if(p_header->nFilledLen)
1526         {
1527             if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1528             {
1529                 /* TODO: need to store codec config */
1530                 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1531             }
1532
1533             p_block = p_header->pAppPrivate;
1534             if(!p_block)
1535             {
1536                 /* We're not in direct rendering mode.
1537                  * Get a new block and copy the content */
1538                 p_block = block_Alloc( p_header->nFilledLen );
1539                 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1540             }
1541
1542             p_block->i_buffer = p_header->nFilledLen;
1543             p_block->i_pts = p_block->i_dts = FromOmxTicks(p_header->nTimeStamp);
1544             p_header->nFilledLen = 0;
1545             p_header->pAppPrivate = 0;
1546         }
1547
1548 #ifdef OMXIL_EXTRA_DEBUG
1549         msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1550 #endif
1551         OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1552     }
1553
1554     msg_Dbg(p_dec, "done");
1555     return p_block;
1556 }
1557
1558 /*****************************************************************************
1559  * CloseGeneric: omxil decoder destruction
1560  *****************************************************************************/
1561 static void CloseGeneric( vlc_object_t *p_this )
1562 {
1563     decoder_t *p_dec = (decoder_t *)p_this;
1564     decoder_sys_t *p_sys = p_dec->p_sys;
1565
1566     if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1567
1568     DeinitOmxCore();
1569
1570     DeinitOmxEventQueue(&p_sys->event_queue);
1571     vlc_mutex_destroy (&p_sys->in.fifo.lock);
1572     vlc_cond_destroy (&p_sys->in.fifo.wait);
1573     vlc_mutex_destroy (&p_sys->out.fifo.lock);
1574     vlc_cond_destroy (&p_sys->out.fifo.wait);
1575
1576     free( p_sys );
1577 }
1578
1579 /*****************************************************************************
1580  * OmxEventHandler: 
1581  *****************************************************************************/
1582 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1583     OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1584     OMX_U32 data_2, OMX_PTR event_data )
1585 {
1586     decoder_t *p_dec = (decoder_t *)app_data;
1587     decoder_sys_t *p_sys = p_dec->p_sys;
1588     unsigned int i;
1589     (void)omx_handle;
1590
1591     PrintOmxEvent((vlc_object_t *) p_dec, event, data_1, data_2, event_data);
1592     switch (event)
1593     {
1594     case OMX_EventError:
1595         //p_sys->b_error = true;
1596         break;
1597
1598     case OMX_EventPortSettingsChanged:
1599         if( data_2 == 0 || data_2 == OMX_IndexParamPortDefinition ||
1600             data_2 == OMX_IndexParamAudioPcm )
1601         {
1602             OMX_BUFFERHEADERTYPE *sentinel;
1603             for(i = 0; i < p_sys->ports; i++)
1604                 if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1605                     p_sys->p_ports[i].b_reconfigure = true;
1606             sentinel = calloc(1, sizeof(*sentinel));
1607             if (sentinel) {
1608                 sentinel->nFlags = SENTINEL_FLAG;
1609                 OMX_FIFO_PUT(&p_sys->in.fifo, sentinel);
1610             }
1611         }
1612         else if( data_2 == OMX_IndexConfigCommonOutputCrop )
1613         {
1614             for(i = 0; i < p_sys->ports; i++)
1615                 if(p_sys->p_ports[i].definition.nPortIndex == data_1)
1616                     p_sys->p_ports[i].b_update_def = true;
1617         }
1618         else
1619         {
1620             msg_Dbg( p_dec, "Unhandled setting change %x", (unsigned int)data_2 );
1621         }
1622         break;
1623
1624     default:
1625         break;
1626     }
1627
1628     PostOmxEvent(&p_sys->event_queue, event, data_1, data_2, event_data);
1629     return OMX_ErrorNone;
1630 }
1631
1632 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1633     OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1634 {
1635     decoder_t *p_dec = (decoder_t *)app_data;
1636     decoder_sys_t *p_sys = p_dec->p_sys;
1637     (void)omx_handle;
1638
1639 #ifdef OMXIL_EXTRA_DEBUG
1640     msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1641 #endif
1642
1643     if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1644     {
1645         block_t *p_block = (block_t *)omx_header->pAppPrivate;
1646         omx_header->pBuffer = omx_header->pOutputPortPrivate;
1647         if(p_block) block_Release(p_block);
1648         omx_header->pAppPrivate = 0;
1649     }
1650     OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1651
1652     return OMX_ErrorNone;
1653 }
1654
1655 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1656     OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1657 {
1658     decoder_t *p_dec = (decoder_t *)app_data;
1659     decoder_sys_t *p_sys = p_dec->p_sys;
1660     (void)omx_handle;
1661
1662 #ifdef OMXIL_EXTRA_DEBUG
1663     msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
1664              (int)omx_header->nFilledLen );
1665 #endif
1666
1667     if(omx_header->pInputPortPrivate)
1668     {
1669         omx_header->pBuffer = omx_header->pInputPortPrivate;
1670     }
1671     OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1672
1673     return OMX_ErrorNone;
1674 }