]> git.sesse.net Git - vlc/blob - modules/codec/omxil/omxil.c
OMX IL assumes dlfcn.h, remove redumdant ifdefs
[vlc] / modules / codec / omxil / omxil.c
1 /*****************************************************************************
2  * omxil.c: Video decoder module making use of OpenMAX IL components.
3  *****************************************************************************
4  * Copyright (C) 2010 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, 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 <dlfcn.h>
32 #define dll_open(name) dlopen( name, RTLD_NOW )
33 #define dll_close(handle) dlclose(handle)
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_codec.h>
38 #include <vlc_aout.h>
39 #include <vlc_block_helper.h>
40 #include <vlc_cpu.h>
41
42 #include "omxil.h"
43
44 //#define OMXIL_EXTRA_DEBUG
45
46 /*****************************************************************************
47  * List of OpenMAX IL core we will try in order
48  *****************************************************************************/
49 static const char *ppsz_dll_list[] =
50 {
51     "libOMX_Core.so", /* TI OMAP IL core */
52     "libomxil-bellagio.so",  /* Bellagio IL core */
53     0
54 };
55
56 /*****************************************************************************
57  * Local prototypes
58  *****************************************************************************/
59 static int  OpenDecoder( vlc_object_t * );
60 static int  OpenEncoder( vlc_object_t * );
61 static int  OpenGeneric( vlc_object_t *, bool b_encode );
62 static void CloseGeneric( vlc_object_t * );
63
64 static picture_t *DecodeVideo( decoder_t *, block_t ** );
65 static aout_buffer_t *DecodeAudio ( decoder_t *, block_t ** );
66 static block_t *EncodeVideo( encoder_t *, picture_t * );
67
68 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE, OMX_PTR, OMX_EVENTTYPE,
69                                       OMX_U32, OMX_U32, OMX_PTR );
70 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE, OMX_PTR,
71                                          OMX_BUFFERHEADERTYPE * );
72 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE, OMX_PTR,
73                                         OMX_BUFFERHEADERTYPE * );
74
75 /*****************************************************************************
76  * Module descriptor
77  *****************************************************************************/
78 vlc_module_begin ()
79     set_description( N_("Audio/Video decoder (using OpenMAX IL)") )
80     set_category( CAT_INPUT )
81     set_subcategory( SUBCAT_INPUT_VCODEC )
82     set_section( N_("Decoding") , NULL )
83 #ifdef HAVE_MAEMO
84     set_capability( "decoder", 80 )
85 #else
86     set_capability( "decoder", 0 )
87 #endif
88     set_callbacks( OpenDecoder, CloseGeneric )
89
90     add_submodule ()
91     set_section( N_("Encoding") , NULL )
92     set_description( N_("Video encoder (using OpenMAX IL)") )
93     set_capability( "encoder", 0 )
94     set_callbacks( OpenEncoder, CloseGeneric )
95 vlc_module_end ()
96
97 /*****************************************************************************
98  * CreateComponentsList: creates a list of components matching the given role
99  *****************************************************************************/
100 static int CreateComponentsList(decoder_t *p_dec, const char *psz_role)
101 {
102     decoder_sys_t *p_sys = p_dec->p_sys;
103     char psz_name[OMX_MAX_STRINGNAME_SIZE];
104     OMX_ERRORTYPE omx_error;
105     OMX_U32 roles = 0;
106     OMX_U8 **ppsz_roles = 0;
107     unsigned int i, j, len;
108
109     if(!psz_role) goto end;
110     len = strlen(psz_role);
111
112     for( i = 0; ; i++ )
113     {
114         bool b_found = false;
115
116         omx_error = p_sys->pf_component_enum(psz_name, OMX_MAX_STRINGNAME_SIZE, i);
117         if(omx_error != OMX_ErrorNone) break;
118
119         msg_Dbg(p_dec, "component %s", psz_name);
120
121         omx_error = p_sys->pf_get_roles_of_component(psz_name, &roles, 0);
122         if(omx_error != OMX_ErrorNone || !roles) continue;
123
124         ppsz_roles = malloc(roles * (sizeof(OMX_U8*) + OMX_MAX_STRINGNAME_SIZE));
125         if(!ppsz_roles) continue;
126
127         for( j = 0; j < roles; j++ )
128             ppsz_roles[j] = ((OMX_U8 *)(&ppsz_roles[roles])) +
129                 j * OMX_MAX_STRINGNAME_SIZE;
130
131         omx_error = p_sys->pf_get_roles_of_component(psz_name, &roles, ppsz_roles);
132         if(omx_error != OMX_ErrorNone) roles = 0;
133
134         for(j = 0; j < roles; j++)
135         {
136             msg_Dbg(p_dec, "  - role: %s", ppsz_roles[j]);
137             if(!strncmp((char *)ppsz_roles[j], psz_role, len)) b_found = true;
138         }
139
140         free(ppsz_roles);
141
142         if(!b_found) continue;
143
144         if(p_sys->components >= MAX_COMPONENTS_LIST_SIZE)
145         {
146             msg_Dbg(p_dec, "too many matching components");
147             continue;
148         }
149
150         strncpy(p_sys->ppsz_components[p_sys->components], psz_name,
151                 OMX_MAX_STRINGNAME_SIZE-1);
152         p_sys->components++;
153     }
154
155  end:
156     msg_Dbg(p_dec, "found %i matching components for role %s",
157             p_sys->components, psz_role);
158     for( i = 0; i < p_sys->components; i++ )
159         msg_Dbg(p_dec, "- %s", p_sys->ppsz_components[i]);
160
161     return p_sys->components;
162 }
163
164 /*****************************************************************************
165  * ImplementationSpecificWorkarounds: place-holder for implementation
166  * specific workarounds
167  *****************************************************************************/
168 static OMX_ERRORTYPE ImplementationSpecificWorkarounds(decoder_t *p_dec,
169     OmxPort *p_port, es_format_t *p_fmt)
170 {
171     decoder_sys_t *p_sys = p_dec->p_sys;
172     OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
173     int i_profile = 0xFFFF, i_level = 0xFFFF;
174
175     /* Try to find out the profile of the video */
176     while(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
177           p_fmt->i_codec == VLC_CODEC_H264)
178     {
179         uint8_t *p = (uint8_t*)p_dec->fmt_in.p_extra;
180         if(!p || !p_dec->fmt_in.p_extra) break;
181
182         /* Check the profile / level */
183         if(p_dec->fmt_in.i_original_fourcc == VLC_FOURCC('a','v','c','1') &&
184            p[0] == 1)
185         {
186             if(p_dec->fmt_in.i_extra < 12) break;
187             p_sys->i_nal_size_length = 1 + (p[4]&0x03);
188             if( !(p[5]&0x1f) ) break;
189             p += 8;
190         }
191         else
192         {
193             if(p_dec->fmt_in.i_extra < 8) break;
194             if(!p[0] && !p[1] && !p[2] && p[3] == 1) p += 4;
195             else if(!p[0] && !p[1] && p[2] == 1) p += 3;
196             else break;
197         }
198
199         if( ((*p++)&0x1f) != 7) break;
200
201         /* Get profile/level out of first SPS */
202         i_profile = p[0];
203         i_level = p[2];
204         break;
205     }
206
207     if(!strcmp(p_sys->psz_component, "OMX.TI.Video.Decoder"))
208     {
209         if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
210            p_fmt->i_codec == VLC_CODEC_H264 &&
211            (i_profile != 66 || i_level > 30))
212         {
213             msg_Dbg(p_dec, "h264 profile/level not supported (0x%x, 0x%x)",
214                     i_profile, i_level);
215             return OMX_ErrorNotImplemented;
216         }
217
218         if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirOutput &&
219            p_fmt->i_codec == VLC_CODEC_I420)
220         {
221             /* I420 xvideo is slow on OMAP */
222             def->format.video.eColorFormat = OMX_COLOR_FormatCbYCrY;
223             GetVlcChromaFormat( def->format.video.eColorFormat,
224                                 &p_fmt->i_codec, 0 );
225             GetVlcChromaSizes( p_fmt->i_codec,
226                                def->format.video.nFrameWidth,
227                                def->format.video.nFrameHeight,
228                                &p_port->i_frame_size, &p_port->i_frame_stride,
229                                &p_port->i_frame_stride_chroma_div );
230             def->format.video.nStride = p_port->i_frame_stride;
231             def->nBufferSize = p_port->i_frame_size;
232         }
233     }
234     else if(!strcmp(p_sys->psz_component, "OMX.st.video_encoder"))
235     {
236         if(p_fmt->i_cat == VIDEO_ES)
237         {
238             /* Bellagio's encoder doesn't encode the framerate in Q16 */
239             def->format.video.xFramerate >>= 16;
240         }
241     }
242
243     return OMX_ErrorNone;
244 }
245
246 /*****************************************************************************
247  * SetPortDefinition: set definition of the omx port based on the vlc format
248  *****************************************************************************/
249 static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
250                                        es_format_t *p_fmt)
251 {
252     OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
253     OMX_ERRORTYPE omx_error;
254
255     omx_error = OMX_GetParameter(p_port->omx_handle,
256                                  OMX_IndexParamPortDefinition, def);
257     CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
258                 omx_error, ErrorToString(omx_error));
259
260     switch(p_fmt->i_cat)
261     {
262     case VIDEO_ES:
263         def->format.video.nFrameWidth = p_fmt->video.i_width;
264         def->format.video.nFrameHeight = p_fmt->video.i_height;
265         if(def->format.video.eCompressionFormat == OMX_VIDEO_CodingUnused)
266             def->format.video.nStride = def->format.video.nFrameWidth;
267         if( p_fmt->video.i_frame_rate > 0 &&
268             p_fmt->video.i_frame_rate_base > 0 )
269             def->format.video.xFramerate = (p_fmt->video.i_frame_rate << 16) /
270                 p_fmt->video.i_frame_rate_base;
271
272         if(def->eDir == OMX_DirInput || p_dec->p_sys->b_enc)
273         {
274             def->nBufferSize = def->format.video.nFrameWidth *
275               def->format.video.nFrameHeight * 2;
276             p_port->i_frame_size = def->nBufferSize;
277
278             if(!GetOmxVideoFormat(p_fmt->i_codec,
279                                   &def->format.video.eCompressionFormat, 0) )
280             {
281                 if(!GetOmxChromaFormat(p_fmt->i_codec,
282                                        &def->format.video.eColorFormat, 0) )
283                 {
284                     omx_error = OMX_ErrorNotImplemented;
285                     CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
286                                 (char *)&p_fmt->i_codec );
287                 }
288                 GetVlcChromaSizes( p_fmt->i_codec,
289                                    def->format.video.nFrameWidth,
290                                    def->format.video.nFrameHeight,
291                                    &p_port->i_frame_size, &p_port->i_frame_stride,
292                                    &p_port->i_frame_stride_chroma_div );
293                 def->format.video.nStride = p_port->i_frame_stride;
294                 def->nBufferSize = p_port->i_frame_size;
295             }
296         }
297         else
298         {
299             if( !GetVlcChromaFormat( def->format.video.eColorFormat,
300                                      &p_fmt->i_codec, 0 ) )
301             {
302                 omx_error = OMX_ErrorNotImplemented;
303                 CHECK_ERROR(omx_error, "OMX color format %i not supported",
304                             (int)def->format.video.eColorFormat );
305             }
306             GetVlcChromaSizes( p_fmt->i_codec,
307                                def->format.video.nFrameWidth,
308                                def->format.video.nFrameHeight,
309                                &p_port->i_frame_size, &p_port->i_frame_stride,
310                                &p_port->i_frame_stride_chroma_div );
311             def->format.video.nStride = p_port->i_frame_stride;
312             def->nBufferSize = p_port->i_frame_size;
313         }
314         break;
315
316     case AUDIO_ES:
317         p_port->i_frame_size = def->nBufferSize;
318         if(def->eDir == OMX_DirInput)
319         {
320             if(!GetOmxAudioFormat(p_fmt->i_codec,
321                                   &def->format.audio.eEncoding, 0) )
322             {
323                 omx_error = OMX_ErrorNotImplemented;
324                 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
325                             (char *)&p_fmt->i_codec );
326             }
327         }
328         else
329         {
330             if( !GetVlcAudioFormat(def->format.audio.eEncoding,
331                                    &p_fmt->i_codec, 0 ) )
332             {
333                 omx_error = OMX_ErrorNotImplemented;
334                 CHECK_ERROR(omx_error, "OMX audio encoding %i not supported",
335                             (int)def->format.audio.eEncoding );
336             }
337         }
338         break;
339
340     default: return OMX_ErrorNotImplemented;
341     }
342
343     omx_error = ImplementationSpecificWorkarounds(p_dec, p_port, p_fmt);
344     CHECK_ERROR(omx_error, "ImplementationSpecificWorkarounds failed (%x : %s)",
345                 omx_error, ErrorToString(omx_error));
346
347     omx_error = OMX_SetParameter(p_port->omx_handle,
348                                  OMX_IndexParamPortDefinition, def);
349     CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
350                 omx_error, ErrorToString(omx_error));
351
352     omx_error = OMX_GetParameter(p_port->omx_handle,
353                                  OMX_IndexParamPortDefinition, def);
354     CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
355                 omx_error, ErrorToString(omx_error));
356
357     if(p_port->i_frame_size > def->nBufferSize)
358         def->nBufferSize = p_port->i_frame_size;
359     p_port->i_frame_size = def->nBufferSize;
360
361     /* Deal with audio params */
362     if(p_fmt->i_cat == AUDIO_ES)
363     {
364         omx_error = SetAudioParameters(p_port->omx_handle,
365                                        &p_port->format_param, def->nPortIndex,
366                                        def->format.audio.eEncoding,
367                                        p_fmt->audio.i_channels,
368                                        p_fmt->audio.i_rate,
369                                        p_fmt->i_bitrate,
370                                        p_fmt->audio.i_bitspersample,
371                                        p_fmt->audio.i_blockalign);
372         CHECK_ERROR(omx_error, "SetAudioParameters failed (%x : %s)",
373                     omx_error, ErrorToString(omx_error));
374     }
375
376  error:
377     return omx_error;
378 }
379
380 /*****************************************************************************
381  * GetPortDefinition: set vlc format based on the definition of the omx port
382  *****************************************************************************/
383 static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
384                                        es_format_t *p_fmt)
385 {
386     OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
387     OMX_ERRORTYPE omx_error;
388
389     omx_error = OMX_GetParameter(p_port->omx_handle,
390                                  OMX_IndexParamPortDefinition, def);
391     CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
392                 omx_error, ErrorToString(omx_error));
393
394     switch(p_fmt->i_cat)
395     {
396     case VIDEO_ES:
397         p_fmt->video.i_width = def->format.video.nFrameWidth;
398         p_fmt->video.i_visible_width = def->format.video.nFrameWidth;
399         p_fmt->video.i_height = def->format.video.nFrameHeight;
400         p_fmt->video.i_visible_height = def->format.video.nFrameHeight;
401         p_fmt->video.i_frame_rate = p_dec->fmt_in.video.i_frame_rate;
402         p_fmt->video.i_frame_rate_base = p_dec->fmt_in.video.i_frame_rate_base;
403
404         if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
405                                &p_fmt->i_codec, 0 ) )
406         {
407             if( !GetVlcChromaFormat( def->format.video.eColorFormat,
408                                      &p_fmt->i_codec, 0 ) )
409             {
410                 omx_error = OMX_ErrorNotImplemented;
411                 CHECK_ERROR(omx_error, "OMX color format %i not supported",
412                             (int)def->format.video.eColorFormat );
413             }
414             GetVlcChromaSizes( p_fmt->i_codec,
415                                def->format.video.nFrameWidth,
416                                def->format.video.nFrameHeight,
417                                &p_port->i_frame_size, &p_port->i_frame_stride,
418                                &p_port->i_frame_stride_chroma_div );
419         }
420         if(p_port->i_frame_size > def->nBufferSize)
421             def->nBufferSize = p_port->i_frame_size;
422         p_port->i_frame_size = def->nBufferSize;
423 #if 0
424         if((int)p_port->i_frame_stride > def->format.video.nStride)
425             def->format.video.nStride = p_port->i_frame_stride;
426 #endif
427         p_port->i_frame_stride = def->format.video.nStride;
428         break;
429
430     case AUDIO_ES:
431         if( !GetVlcAudioFormat( def->format.audio.eEncoding,
432                                 &p_fmt->i_codec, 0 ) )
433         {
434             omx_error = OMX_ErrorNotImplemented;
435             CHECK_ERROR(omx_error, "OMX audio format %i not supported",
436                         (int)def->format.audio.eEncoding );
437         }
438
439         omx_error = GetAudioParameters(p_port->omx_handle,
440                                        &p_port->format_param, def->nPortIndex,
441                                        def->format.audio.eEncoding,
442                                        &p_fmt->audio.i_channels,
443                                        &p_fmt->audio.i_rate,
444                                        &p_fmt->i_bitrate,
445                                        &p_fmt->audio.i_bitspersample,
446                                        &p_fmt->audio.i_blockalign);
447         CHECK_ERROR(omx_error, "GetAudioParameters failed (%x : %s)",
448                     omx_error, ErrorToString(omx_error));
449
450         if(p_fmt->audio.i_channels < 9)
451         {
452             static const int pi_channels_maps[9] =
453             {
454                 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
455                 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
456                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
457                 | AOUT_CHAN_REARRIGHT,
458                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
459                 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
460                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
461                 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
462                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
463                 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
464                 | AOUT_CHAN_MIDDLERIGHT,
465                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
466                 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
467                 | AOUT_CHAN_LFE
468             };
469             p_fmt->audio.i_physical_channels =
470                 p_fmt->audio.i_original_channels =
471                     pi_channels_maps[p_fmt->audio.i_channels];
472         }
473
474         date_Init( &p_dec->p_sys->end_date, p_fmt->audio.i_rate, 1 );
475
476         break;
477
478     default: return OMX_ErrorNotImplemented;
479     }
480
481  error:
482     return omx_error;
483 }
484
485 /*****************************************************************************
486  * DeinitialiseComponent: Deinitialise and unload an OMX component
487  *****************************************************************************/
488 static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
489                                            OMX_HANDLETYPE omx_handle)
490 {
491     decoder_sys_t *p_sys = p_dec->p_sys;
492     OMX_ERRORTYPE omx_error;
493     OMX_STATETYPE state;
494     unsigned int i, j;
495
496     if(!omx_handle) return OMX_ErrorNone;
497
498     omx_error = OMX_GetState(omx_handle, &state);
499     CHECK_ERROR(omx_error, "ONX_GetState failed (%x)", omx_error );
500
501     if(state == OMX_StateExecuting)
502     {
503         omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
504                                      OMX_StateIdle, 0 );
505         CHECK_ERROR(omx_error, "ONX_CommandStateSet Idle failed (%x)", omx_error );
506         omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
507         CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
508     }
509
510     omx_error = OMX_GetState(omx_handle, &state);
511     CHECK_ERROR(omx_error, "ONX_GetState failed (%x)", omx_error );
512
513     if(state == OMX_StateIdle)
514     {
515         omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
516                                      OMX_StateLoaded, 0 );
517         CHECK_ERROR(omx_error, "ONX_CommandStateSet Loaded failed (%x)", omx_error );
518
519         for(i = 0; i < p_sys->ports; i++)
520         {
521             OmxPort *p_port = &p_sys->p_ports[i];
522             OMX_BUFFERHEADERTYPE *p_buffer;
523
524             for(j = 0; j < p_port->i_buffers; j++)
525             {
526                 OMX_FIFO_GET(&p_port->fifo, p_buffer);
527                 omx_error = OMX_FreeBuffer( omx_handle,
528                                             p_port->i_port_index, p_buffer );
529
530                 if(omx_error != OMX_ErrorNone) break;
531             }
532             CHECK_ERROR(omx_error, "ONX_FreeBuffer failed (%x, %i, %i)",
533                         omx_error, (int)p_port->i_port_index, j );
534         }
535
536         omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
537         CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
538     }
539
540  error:
541     for(i = 0; i < p_sys->ports; i++)
542     {
543         OmxPort *p_port = &p_sys->p_ports[i];
544         free(p_port->pp_buffers);
545         p_port->pp_buffers = 0;
546     }
547     omx_error = p_sys->pf_free_handle( omx_handle );
548     return omx_error;
549 }
550
551 /*****************************************************************************
552  * InitialiseComponent: Load and initialise an OMX component
553  *****************************************************************************/
554 static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
555     OMX_STRING psz_component, OMX_HANDLETYPE *p_handle)
556 {
557     static OMX_CALLBACKTYPE callbacks =
558         { OmxEventHandler, OmxEmptyBufferDone, OmxFillBufferDone };
559     decoder_sys_t *p_sys = p_dec->p_sys;
560     OMX_HANDLETYPE omx_handle;
561     OMX_ERRORTYPE omx_error;
562     unsigned int i;
563     OMX_U8 psz_role[OMX_MAX_STRINGNAME_SIZE];
564     OMX_PARAM_COMPONENTROLETYPE role;
565     OMX_PARAM_PORTDEFINITIONTYPE definition;
566     OMX_PORT_PARAM_TYPE param;
567
568     /* Load component */
569     omx_error = p_sys->pf_get_handle( &omx_handle, psz_component,
570                                       p_dec, &callbacks );
571     if(omx_error != OMX_ErrorNone)
572     {
573         msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
574                   omx_error, ErrorToString(omx_error) );
575         return omx_error;
576     }
577     strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
578
579     OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
580     msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
581     PrintOmx(p_dec, omx_handle, OMX_ALL);
582
583     /* Set component role */
584     OMX_INIT_STRUCTURE(role);
585     strcpy((char*)role.cRole,
586            GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec : p_dec->fmt_in.i_codec,
587                       p_dec->fmt_in.i_cat, p_sys->b_enc));
588
589     omx_error = OMX_SetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
590                                  &role);
591     omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
592                                  &role);
593     if(omx_error == OMX_ErrorNone)
594         msg_Dbg(p_dec, "component standard role set to %s", role.cRole);
595
596     /* Find the input / output ports */
597     OMX_INIT_STRUCTURE(param);
598     OMX_INIT_STRUCTURE(definition);
599     omx_error = OMX_GetParameter(omx_handle, p_dec->fmt_in.i_cat == VIDEO_ES ?
600                                  OMX_IndexParamVideoInit : OMX_IndexParamAudioInit, &param);
601     if(omx_error != OMX_ErrorNone) param.nPorts = 0;
602
603     for(i = 0; i < param.nPorts; i++)
604     {
605         OmxPort *p_port;
606
607         /* Get port definition */
608         definition.nPortIndex = param.nStartPortNumber + i;
609         omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
610                                      &definition);
611         if(omx_error != OMX_ErrorNone) continue;
612
613         if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
614         else  p_port = &p_sys->out;
615
616         p_port->b_valid = true;
617         p_port->i_port_index = definition.nPortIndex;
618         p_port->definition = definition;
619         p_port->omx_handle = omx_handle;
620     }
621
622     if(!p_sys->in.b_valid || !p_sys->out.b_valid)
623     {
624         omx_error = OMX_ErrorInvalidComponent;
625         CHECK_ERROR(omx_error, "couldn't find an input and output port");
626     }
627
628     /* Set port definitions */
629     for(i = 0; i < p_sys->ports; i++)
630     {
631         omx_error = SetPortDefinition(p_dec, &p_sys->p_ports[i],
632                                       p_sys->p_ports[i].p_fmt);
633         if(omx_error != OMX_ErrorNone) goto error;
634     }
635
636     /* Allocate our array for the omx buffers and enable ports */
637     for(i = 0; i < p_sys->ports; i++)
638     {
639         OmxPort *p_port = &p_sys->p_ports[i];
640
641         p_port->pp_buffers =
642             malloc(p_port->definition.nBufferCountActual *
643                    sizeof(OMX_BUFFERHEADERTYPE*));
644         if(!p_port->pp_buffers)
645         {
646           omx_error = OMX_ErrorInsufficientResources;
647           CHECK_ERROR(omx_error, "memory allocation failed");
648         }
649         p_port->i_buffers = p_port->definition.nBufferCountActual;
650
651         /* Enable port */
652         if(!p_port->definition.bEnabled)
653         {
654             omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
655                                          p_port->i_port_index, NULL);
656             CHECK_ERROR(omx_error, "ONX_CommandPortEnable on %i failed (%x)",
657                         (int)p_port->i_port_index, omx_error );
658             omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
659             CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
660                         (int)p_port->i_port_index, omx_error );
661         }
662     }
663
664     *p_handle = omx_handle;
665     return OMX_ErrorNone;
666
667  error:
668     DeinitialiseComponent(p_dec, omx_handle);
669     *p_handle = 0;
670     return omx_error;
671 }
672
673 /*****************************************************************************
674  * OpenDecoder: Create the decoder instance
675  *****************************************************************************/
676 static int OpenDecoder( vlc_object_t *p_this )
677 {
678     decoder_t *p_dec = (decoder_t*)p_this;
679     int status;
680
681     if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
682         return VLC_EGENERIC;
683
684 #ifdef HAVE_MAEMO
685     if( p_dec->fmt_in.i_cat != VIDEO_ES && !p_dec->b_force)
686         return VLC_EGENERIC;
687 #endif
688
689     status = OpenGeneric( p_this, false );
690     if(status != VLC_SUCCESS) return status;
691
692     p_dec->pf_decode_video = DecodeVideo;
693     p_dec->pf_decode_audio = DecodeAudio;
694
695     return VLC_SUCCESS;
696 }
697
698 /*****************************************************************************
699  * OpenEncoder: Create the encoder instance
700  *****************************************************************************/
701 static int OpenEncoder( vlc_object_t *p_this )
702 {
703     encoder_t *p_enc = (encoder_t*)p_this;
704     int status;
705
706     if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
707         return VLC_EGENERIC;
708
709     status = OpenGeneric( p_this, true );
710     if(status != VLC_SUCCESS) return status;
711
712     p_enc->pf_encode_video = EncodeVideo;
713
714     return VLC_SUCCESS;
715 }
716
717 /*****************************************************************************
718  * OpenGeneric: Create the generic decoder/encoder instance
719  *****************************************************************************/
720 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
721 {
722     void *dll_handle = 0, *pf_init = 0, *pf_deinit = 0;
723     void *pf_get_handle = 0, *pf_free_handle = 0, *pf_component_enum = 0;
724     void *pf_get_roles_of_component = 0;
725     decoder_t *p_dec = (decoder_t*)p_this;
726     decoder_sys_t *p_sys;
727     OMX_ERRORTYPE omx_error;
728     OMX_BUFFERHEADERTYPE *p_header;
729     unsigned int i, j;
730
731     /* Load the OMX core */
732     for( i = 0; ppsz_dll_list[i]; i++ )
733     {
734         dll_handle = dll_open( ppsz_dll_list[i] );
735         if( dll_handle ) break;
736     }
737     if( !dll_handle ) return VLC_EGENERIC;
738
739     pf_init = dlsym( dll_handle, "OMX_Init" );
740     pf_deinit = dlsym( dll_handle, "OMX_Deinit" );
741     pf_get_handle = dlsym( dll_handle, "OMX_GetHandle" );
742     pf_free_handle = dlsym( dll_handle, "OMX_FreeHandle" );
743     pf_component_enum = dlsym( dll_handle, "OMX_ComponentNameEnum" );
744     pf_get_roles_of_component = dlsym( dll_handle, "OMX_GetRolesOfComponent" );
745     if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle ||
746         !pf_component_enum || !pf_get_roles_of_component )
747     {
748         msg_Warn( p_this, "cannot find OMX_* symbols in `%s' (%s)",
749                   ppsz_dll_list[i], dlerror() );
750         dll_close(dll_handle);
751     }
752
753     if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle || !pf_component_enum )
754     {
755         dll_close(dll_handle);
756         return VLC_EGENERIC;
757     }
758
759     /* Allocate the memory needed to store the decoder's structure */
760     if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
761     {
762         dll_close(dll_handle);
763         return VLC_ENOMEM;
764     }
765
766     /* Initialise the thread properties */
767     if(!b_encode)
768     {
769         p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
770         p_dec->fmt_out.video = p_dec->fmt_in.video;
771         p_dec->fmt_out.audio = p_dec->fmt_in.audio;
772         p_dec->fmt_out.i_codec = 0;
773     }
774     p_sys->b_enc = b_encode;
775     p_sys->dll_handle = dll_handle;
776     p_sys->pf_init = pf_init;
777     p_sys->pf_deinit = pf_deinit;
778     p_sys->pf_get_handle = pf_get_handle;
779     p_sys->pf_free_handle = pf_free_handle;
780     p_sys->pf_component_enum = pf_component_enum;
781     p_sys->pf_get_roles_of_component = pf_get_roles_of_component;
782     p_sys->pp_last_event = &p_sys->p_events;
783     vlc_mutex_init (&p_sys->mutex);
784     vlc_cond_init (&p_sys->cond);
785     vlc_mutex_init (&p_sys->lock);
786     vlc_mutex_init (&p_sys->in.fifo.lock);
787     vlc_cond_init (&p_sys->in.fifo.wait);
788     p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
789     p_sys->in.fifo.pp_last = &p_sys->in.fifo.p_first;
790     p_sys->in.b_direct = false;
791     p_sys->in.b_flushed = true;
792     p_sys->in.p_fmt = &p_dec->fmt_in;
793     vlc_mutex_init (&p_sys->out.fifo.lock);
794     vlc_cond_init (&p_sys->out.fifo.wait);
795     p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
796     p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
797     p_sys->out.b_direct = true;
798     p_sys->out.b_flushed = true;
799     p_sys->out.p_fmt = &p_dec->fmt_out;
800     p_sys->ports = 2;
801     p_sys->p_ports = &p_sys->in;
802
803     msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
804             (char *)&p_dec->fmt_out.i_codec);
805
806     /* Initialise the OMX core */
807     omx_error = p_sys->pf_init();
808     if(omx_error != OMX_ErrorNone)
809     {
810         msg_Warn( p_this, "OMX_Init failed (%x: %s)", omx_error,
811                   ErrorToString(omx_error) );
812         CloseGeneric(p_this);
813         return VLC_EGENERIC;
814     }
815     p_sys->b_init = true;
816
817     /* Enumerate components and build a list of the one we want to try */
818     if( !CreateComponentsList(p_dec,
819              GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
820                         p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
821                         p_sys->b_enc)) )
822     {
823         msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
824                   (char *)&p_dec->fmt_in.i_codec );
825         CloseGeneric(p_this);
826         return VLC_EGENERIC;
827     }
828
829     /* Try to load and initialise a component */
830     for(i = 0; i < p_sys->components; i++)
831     {
832         omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
833                                         &p_sys->omx_handle);
834         if(omx_error == OMX_ErrorNone) break;
835     }
836     CHECK_ERROR(omx_error, "no component could be initialised" );
837
838     /* Move component to Idle then Executing state */
839     OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
840     CHECK_ERROR(omx_error, "ONX_CommandStateSet Idle failed (%x)", omx_error );
841
842     /* Allocate omx buffers */
843     for(i = 0; i < p_sys->ports; i++)
844     {
845         OmxPort *p_port = &p_sys->p_ports[i];
846
847         for(j = 0; j < p_port->i_buffers; j++)
848         {
849 #if 0
850 #define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
851             char *p_buf = malloc(p_port->definition.nBufferSize +
852                                  p_port->definition.nBufferAlignment);
853             p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
854 #endif
855
856             if(0 && p_port->b_direct)
857                 omx_error =
858                     OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
859                                    p_port->i_port_index, 0,
860                                    p_port->definition.nBufferSize, (void*)1);
861             else
862                 omx_error =
863                     OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
864                                         p_port->i_port_index, 0,
865                                         p_port->definition.nBufferSize);
866
867             if(omx_error != OMX_ErrorNone) break;
868             OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
869         }
870         p_port->i_buffers = j;
871         CHECK_ERROR(omx_error, "ONX_UseBuffer failed (%x, %i, %i)",
872                     omx_error, (int)p_port->i_port_index, j );
873     }
874
875     omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
876     CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
877
878     omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
879                                  OMX_StateExecuting, 0);
880     CHECK_ERROR(omx_error, "ONX_CommandStateSet Executing failed (%x)", omx_error );
881     omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
882     CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
883
884     /* Send codec configuration data */
885     if( p_dec->fmt_in.i_extra )
886     {
887         OMX_FIFO_GET(&p_sys->in.fifo, p_header);
888         p_header->nFilledLen = p_dec->fmt_in.i_extra;
889
890         if(p_sys->in.b_direct)
891         {
892             p_header->pOutputPortPrivate = p_header->pBuffer;
893             p_header->pBuffer = p_dec->fmt_in.p_extra;
894         }
895         else
896         {
897             if(p_header->nFilledLen > p_header->nAllocLen)
898             {
899                 msg_Dbg(p_dec, "buffer too small (%i,%i)", (int)p_header->nFilledLen,
900                         (int)p_header->nAllocLen);
901                 p_header->nFilledLen = p_header->nAllocLen;
902             }
903             memcpy(p_header->pBuffer, p_dec->fmt_in.p_extra, p_header->nFilledLen);
904         }
905
906         p_header->nOffset = 0;
907         p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG;
908         msg_Dbg(p_dec, "sending codec config data %p, %p, %i", p_header,
909                 p_header->pBuffer, (int)p_header->nFilledLen);
910         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
911     }
912
913     /* Get back output port definition */
914     omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
915     if(omx_error != OMX_ErrorNone) goto error;
916
917     PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
918     PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
919
920     if(p_sys->b_error) goto error;
921
922     p_dec->b_need_packetized = true;
923     return VLC_SUCCESS;
924
925  error:
926     CloseGeneric(p_this);
927     return VLC_EGENERIC;
928 }
929
930 /*****************************************************************************
931  * PortReconfigure
932  *****************************************************************************/
933 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
934 {
935     decoder_sys_t *p_sys = p_dec->p_sys;
936     OMX_PARAM_PORTDEFINITIONTYPE definition;
937     OMX_BUFFERHEADERTYPE *p_buffer;
938     OMX_ERRORTYPE omx_error;
939     unsigned int i;
940
941     /* Sanity checking */
942     OMX_INIT_STRUCTURE(definition);
943     definition.nPortIndex = p_port->i_port_index;
944     omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
945                                  &definition);
946     if(omx_error != OMX_ErrorNone ||
947        !definition.format.video.nFrameWidth ||
948        !definition.format.video.nFrameHeight )
949         return OMX_ErrorUndefined;
950
951     omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
952                                  p_port->i_port_index, NULL);
953     CHECK_ERROR(omx_error, "ONX_CommandPortDisable on %i failed (%x)",
954                 (int)p_port->i_port_index, omx_error );
955
956     for(i = 0; i < p_port->i_buffers; i++)
957     {
958         OMX_FIFO_GET(&p_port->fifo, p_buffer);
959         omx_error = OMX_FreeBuffer( p_sys->omx_handle,
960                                     p_port->i_port_index, p_buffer );
961
962         if(omx_error != OMX_ErrorNone) break;
963     }
964     CHECK_ERROR(omx_error, "ONX_FreeBuffer failed (%x, %i, %i)",
965                 omx_error, (int)p_port->i_port_index, i );
966
967     omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
968     CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
969
970     /* Get the new port definition */
971     omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
972     if(omx_error != OMX_ErrorNone) goto error;
973     omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
974                                  &definition);
975     CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
976                 omx_error, ErrorToString(omx_error));
977
978     omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
979                                  p_port->i_port_index, NULL);
980     CHECK_ERROR(omx_error, "ONX_CommandPortEnable on %i failed (%x)",
981                 (int)p_port->i_port_index, omx_error );
982
983     for(i = 0; i < p_port->i_buffers; i++)
984     {
985         if(0 && p_port->b_direct)
986             omx_error =
987                 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
988                                p_port->i_port_index, 0,
989                                p_port->definition.nBufferSize, (void*)1);
990         else
991             omx_error =
992                 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
993                                     p_port->i_port_index, 0,
994                                     p_port->definition.nBufferSize);
995
996         if(omx_error != OMX_ErrorNone) break;
997         OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
998     }
999     p_port->i_buffers = i;
1000     CHECK_ERROR(omx_error, "ONX_UseBuffer failed (%x, %i, %i)",
1001                 omx_error, (int)p_port->i_port_index, i );
1002
1003     omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1004     CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1005
1006     PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1007     PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1008
1009  error:
1010     return omx_error;
1011 }
1012
1013 /*****************************************************************************
1014  * DecodeVideo: Called to decode one frame
1015  *****************************************************************************/
1016 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1017 {
1018     decoder_sys_t *p_sys = p_dec->p_sys;
1019     picture_t *p_pic = NULL, *p_next_pic;
1020     OMX_ERRORTYPE omx_error;
1021     unsigned int i;
1022
1023     OMX_BUFFERHEADERTYPE *p_header;
1024     block_t *p_block;
1025
1026     if( !pp_block || !*pp_block )
1027         return NULL;
1028
1029     p_block = *pp_block;
1030
1031     /* Check for errors from codec */
1032     if(p_sys->b_error)
1033     {
1034         msg_Dbg(p_dec, "error during decoding");
1035         block_Release( p_block );
1036         return 0;
1037     }
1038
1039     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1040     {
1041         block_Release( p_block );
1042         if(!p_sys->in.b_flushed)
1043         {
1044             msg_Dbg(p_dec, "flushing");
1045             OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1046                              p_sys->in.definition.nPortIndex, 0 );
1047         }
1048         p_sys->in.b_flushed = true;
1049         return NULL;
1050     }
1051
1052     /* Take care of decoded frames first */
1053     while(!p_pic)
1054     {
1055         OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1056         if(!p_header) break; /* No frame available */
1057
1058         if(p_header->nFilledLen)
1059         {
1060             p_pic = p_header->pAppPrivate;
1061             if(!p_pic)
1062             {
1063                 /* We're not in direct rendering mode.
1064                  * Get a new picture and copy the content */
1065                 p_pic = decoder_NewPicture( p_dec );
1066                 if( !p_pic ) break; /* No picture available */
1067
1068                 CopyOmxPicture(p_dec, p_pic, p_header);
1069             }
1070
1071             p_pic->date = p_header->nTimeStamp;
1072             p_header->nFilledLen = 0;
1073             p_header->pAppPrivate = 0;
1074         }
1075
1076         /* Get a new picture */
1077         if(p_sys->in.b_direct && !p_header->pAppPrivate)
1078         {
1079             p_next_pic = decoder_NewPicture( p_dec );
1080             if(!p_next_pic) break;
1081
1082             OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1083             p_header->pAppPrivate = p_next_pic;
1084             p_header->pInputPortPrivate = p_header->pBuffer;
1085             p_header->pBuffer = p_next_pic->p[0].p_pixels;
1086         }
1087         else
1088         {
1089             OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1090         }
1091
1092 #ifdef OMXIL_EXTRA_DEBUG
1093         msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1094 #endif
1095         OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1096     }
1097
1098     /* Send the input buffer to the component */
1099     OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1100     if(p_header)
1101     {
1102         p_header->nFilledLen = p_block->i_buffer;
1103         p_header->nOffset = 0;
1104         p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1105         p_header->nTimeStamp = p_block->i_dts;
1106
1107         /* In direct mode we pass the input pointer as is.
1108          * Otherwise we memcopy the data */
1109         if(p_sys->in.b_direct)
1110         {
1111             p_header->pOutputPortPrivate = p_header->pBuffer;
1112             p_header->pBuffer = p_block->p_buffer;
1113             p_header->pAppPrivate = p_block;
1114         }
1115         else
1116         {
1117             if(p_header->nFilledLen > p_header->nAllocLen)
1118             {
1119                 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1120                         (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1121                 p_header->nFilledLen = p_header->nAllocLen;
1122             }
1123             memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1124             block_Release(p_block);
1125         }
1126
1127 #ifdef OMXIL_EXTRA_DEBUG
1128         msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1129                  (int)p_header->nFilledLen );
1130 #endif
1131         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1132         p_sys->in.b_flushed = false;
1133         *pp_block = NULL; /* Avoid being fed the same packet again */
1134     }
1135
1136     /* Handle the PortSettingsChanged events */
1137     for(i = 0; i < p_sys->ports; i++)
1138     {
1139         OmxPort *p_port = &p_sys->p_ports[i];
1140         if(!p_port->b_reconfigure) continue;
1141         p_port->b_reconfigure = 0;
1142         omx_error = PortReconfigure(p_dec, p_port);
1143     }
1144
1145     return p_pic;
1146 }
1147
1148 /*****************************************************************************
1149  * DecodeAudio: Called to decode one frame
1150  *****************************************************************************/
1151 aout_buffer_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1152 {
1153     decoder_sys_t *p_sys = p_dec->p_sys;
1154     aout_buffer_t *p_buffer = 0;
1155     OMX_BUFFERHEADERTYPE *p_header;
1156     OMX_ERRORTYPE omx_error;
1157     block_t *p_block;
1158     unsigned int i;
1159
1160     if( !pp_block || !*pp_block ) return NULL;
1161
1162     p_block = *pp_block;
1163
1164     /* Check for errors from codec */
1165     if(p_sys->b_error)
1166     {
1167         msg_Dbg(p_dec, "error during decoding");
1168         block_Release( p_block );
1169         return 0;
1170     }
1171
1172     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1173     {
1174         block_Release( p_block );
1175         date_Set( &p_sys->end_date, 0 );
1176         if(!p_sys->in.b_flushed)
1177         {
1178             msg_Dbg(p_dec, "flushing");
1179             OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1180                              p_sys->in.definition.nPortIndex, 0 );
1181         }
1182         p_sys->in.b_flushed = true;
1183         return NULL;
1184     }
1185
1186     if( !date_Get( &p_sys->end_date ) )
1187     {
1188         if( !p_block->i_pts )
1189         {
1190             /* We've just started the stream, wait for the first PTS. */
1191             block_Release( p_block );
1192             return NULL;
1193         }
1194         date_Set( &p_sys->end_date, p_block->i_pts );
1195     }
1196
1197     /* Take care of decoded frames first */
1198     while(!p_buffer)
1199     {
1200         unsigned int i_samples;
1201
1202         OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1203         if(!p_header) break; /* No frame available */
1204
1205         i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1206         if(i_samples)
1207         {
1208             p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1209             if( !p_buffer ) break; /* No audio buffer available */
1210
1211             memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1212             p_header->nFilledLen = 0;
1213
1214             if( p_header->nTimeStamp != 0 &&
1215                 p_header->nTimeStamp != date_Get( &p_sys->end_date ) )
1216                 date_Set( &p_sys->end_date, p_header->nTimeStamp );
1217
1218             p_buffer->i_pts = date_Get( &p_sys->end_date );
1219             p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1220                 p_buffer->i_pts;
1221         }
1222
1223 #ifdef OMXIL_EXTRA_DEBUG
1224         msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1225 #endif
1226         OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1227         OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1228     }
1229
1230
1231     /* Send the input buffer to the component */
1232     OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1233     if(p_header)
1234     {
1235         p_header->nFilledLen = p_block->i_buffer;
1236         p_header->nOffset = 0;
1237         p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1238         p_header->nTimeStamp = p_block->i_dts;
1239
1240         /* In direct mode we pass the input pointer as is.
1241          * Otherwise we memcopy the data */
1242         if(p_sys->in.b_direct)
1243         {
1244             p_header->pOutputPortPrivate = p_header->pBuffer;
1245             p_header->pBuffer = p_block->p_buffer;
1246             p_header->pAppPrivate = p_block;
1247         }
1248         else
1249         {
1250             if(p_header->nFilledLen > p_header->nAllocLen)
1251             {
1252                 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1253                         (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1254                 p_header->nFilledLen = p_header->nAllocLen;
1255             }
1256             memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1257             block_Release(p_block);
1258         }
1259
1260 #ifdef OMXIL_EXTRA_DEBUG
1261         msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1262                  (int)p_header->nFilledLen );
1263 #endif
1264         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1265         p_sys->in.b_flushed = false;
1266         *pp_block = NULL; /* Avoid being fed the same packet again */
1267     }
1268
1269     /* Handle the PortSettingsChanged events */
1270     for(i = 0; i < p_sys->ports; i++)
1271     {
1272         OmxPort *p_port = &p_sys->p_ports[i];
1273         if(!p_port->b_reconfigure) continue;
1274         p_port->b_reconfigure = 0;
1275         omx_error = PortReconfigure(p_dec, p_port);
1276     }
1277
1278     return p_buffer;
1279 }
1280
1281 /*****************************************************************************
1282  * EncodeVideo: Called to encode one frame
1283  *****************************************************************************/
1284 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1285 {
1286     decoder_t *p_dec = ( decoder_t *)p_enc;
1287     decoder_sys_t *p_sys = p_dec->p_sys;
1288     OMX_ERRORTYPE omx_error;
1289     unsigned int i;
1290
1291     OMX_BUFFERHEADERTYPE *p_header;
1292     block_t *p_block = 0;
1293
1294     if( !p_pic ) return NULL;
1295
1296     /* Check for errors from codec */
1297     if(p_sys->b_error)
1298     {
1299         msg_Dbg(p_dec, "error during encoding");
1300         return NULL;
1301     }
1302
1303     /* Send the input buffer to the component */
1304     OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1305     if(p_header)
1306     {
1307         /* In direct mode we pass the input pointer as is.
1308          * Otherwise we memcopy the data */
1309         if(p_sys->in.b_direct)
1310         {
1311             p_header->pOutputPortPrivate = p_header->pBuffer;
1312             p_header->pBuffer = p_pic->p[0].p_pixels;
1313         }
1314         else
1315         {
1316             CopyVlcPicture(p_dec, p_header, p_pic);
1317         }
1318
1319         p_header->nFilledLen = p_sys->in.i_frame_size;
1320         p_header->nOffset = 0;
1321         p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1322         p_header->nTimeStamp = p_pic->date;
1323 #ifdef OMXIL_EXTRA_DEBUG
1324         msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1325                  (int)p_header->nFilledLen );
1326 #endif
1327         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1328         p_sys->in.b_flushed = false;
1329     }
1330
1331     /* Handle the PortSettingsChanged events */
1332     for(i = 0; i < p_sys->ports; i++)
1333     {
1334         OmxPort *p_port = &p_sys->p_ports[i];
1335         if(!p_port->b_reconfigure) continue;
1336         p_port->b_reconfigure = 0;
1337         omx_error = PortReconfigure(p_dec, p_port);
1338     }
1339
1340     /* Wait for the decoded frame */
1341     while(!p_block)
1342     {
1343         OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1344
1345         if(p_header->nFilledLen)
1346         {
1347             if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1348             {
1349                 /* TODO: need to store codec config */
1350                 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1351             }
1352
1353             p_block = p_header->pAppPrivate;
1354             if(!p_block)
1355             {
1356                 /* We're not in direct rendering mode.
1357                  * Get a new block and copy the content */
1358                 p_block = block_New( p_dec, p_header->nFilledLen );
1359                 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1360             }
1361
1362             p_block->i_buffer = p_header->nFilledLen;
1363             p_block->i_pts = p_block->i_dts = p_header->nTimeStamp;
1364             p_header->nFilledLen = 0;
1365             p_header->pAppPrivate = 0;
1366         }
1367
1368 #ifdef OMXIL_EXTRA_DEBUG
1369         msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1370 #endif
1371         OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1372     }
1373
1374     msg_Dbg(p_dec, "done");
1375     return p_block;
1376 }
1377
1378 /*****************************************************************************
1379  * CloseGeneric: omxil decoder destruction
1380  *****************************************************************************/
1381 static void CloseGeneric( vlc_object_t *p_this )
1382 {
1383     decoder_t *p_dec = (decoder_t *)p_this;
1384     decoder_sys_t *p_sys = p_dec->p_sys;
1385
1386     if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1387     if(p_sys->b_init) p_sys->pf_deinit();
1388     dll_close( p_sys->dll_handle );
1389
1390     vlc_mutex_destroy (&p_sys->mutex);
1391     vlc_cond_destroy (&p_sys->cond);
1392     vlc_mutex_destroy (&p_sys->in.fifo.lock);
1393     vlc_cond_destroy (&p_sys->in.fifo.wait);
1394     vlc_mutex_destroy (&p_sys->out.fifo.lock);
1395     vlc_cond_destroy (&p_sys->out.fifo.wait);
1396
1397     free( p_sys );
1398 }
1399
1400 /*****************************************************************************
1401  * OmxEventHandler: 
1402  *****************************************************************************/
1403 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1404     OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1405     OMX_U32 data_2, OMX_PTR event_data )
1406 {
1407     decoder_t *p_dec = (decoder_t *)app_data;
1408     decoder_sys_t *p_sys = p_dec->p_sys;
1409     unsigned int i;
1410     (void)omx_handle;
1411
1412     switch (event)
1413     {
1414     case OMX_EventCmdComplete:
1415         switch ((OMX_STATETYPE)data_1)
1416         {
1417         case OMX_CommandStateSet:
1418             msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %s)", EventToString(event),
1419                      CommandToString(data_1), StateToString(data_2) );
1420             break;
1421
1422         default:
1423             msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u)", EventToString(event),
1424                      CommandToString(data_1), (unsigned int)data_2 );
1425             break;
1426         }
1427         break;
1428
1429     case OMX_EventError:
1430         msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u, %s)", EventToString(event),
1431                  ErrorToString((OMX_ERRORTYPE)data_1), (unsigned int)data_2,
1432                  (const char *)event_data);
1433         //p_sys->b_error = true;
1434         break;
1435
1436     case OMX_EventPortSettingsChanged:
1437         msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1438                  (unsigned int)data_1, (unsigned int)data_2 );
1439         for(i = 0; i < p_sys->ports; i++)
1440             if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1441                 p_sys->p_ports[i].b_reconfigure = true;
1442         break;
1443
1444     default:
1445         msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1446                  (unsigned int)data_1, (unsigned int)data_2 );
1447         break;
1448     }
1449
1450     PostOmxEvent(p_dec, event, data_1, data_2, event_data);
1451     return OMX_ErrorNone;
1452 }
1453
1454 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1455     OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1456 {
1457     decoder_t *p_dec = (decoder_t *)app_data;
1458     decoder_sys_t *p_sys = p_dec->p_sys;
1459     (void)omx_handle;
1460
1461 #ifdef OMXIL_EXTRA_DEBUG
1462     msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1463 #endif
1464
1465     if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1466     {
1467         block_t *p_block = (block_t *)omx_header->pAppPrivate;
1468         omx_header->pBuffer = omx_header->pOutputPortPrivate;
1469         if(p_block) block_Release(p_block);
1470         omx_header->pAppPrivate = 0;
1471     }
1472     OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1473
1474     return OMX_ErrorNone;
1475 }
1476
1477 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1478     OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1479 {
1480     decoder_t *p_dec = (decoder_t *)app_data;
1481     decoder_sys_t *p_sys = p_dec->p_sys;
1482     (void)omx_handle;
1483
1484 #ifdef OMXIL_EXTRA_DEBUG
1485     msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
1486              (int)omx_header->nFilledLen );
1487 #endif
1488
1489     if(omx_header->pInputPortPrivate)
1490     {
1491         omx_header->pBuffer = omx_header->pInputPortPrivate;
1492     }
1493     OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1494
1495     return OMX_ErrorNone;
1496 }