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