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