1 /*****************************************************************************
2 * omxil.c: Video decoder module making use of OpenMAX IL components.
3 *****************************************************************************
4 * Copyright (C) 2010 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #if defined(HAVE_DL_DLOPEN)
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_codec.h>
39 #include <vlc_block_helper.h>
44 //#define OMXIL_EXTRA_DEBUG
46 /*****************************************************************************
47 * List of OpenMAX IL core we will try in order
48 *****************************************************************************/
49 static const char *ppsz_dll_list[] =
51 "libOMX_Core.so", /* TI OMAP IL core */
52 "libomxil-bellagio.so", /* Bellagio IL core */
56 /*****************************************************************************
58 *****************************************************************************/
59 #if defined(HAVE_DL_DLOPEN)
60 # define dll_open(name) dlopen( name, RTLD_NOW )
61 # define dll_close(handle) dlclose(handle)
63 # define dll_open(name) (NULL)
64 # define dll_close(handle) (void)0
67 /*****************************************************************************
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 * );
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 * );
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 * );
86 /*****************************************************************************
88 *****************************************************************************/
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 )
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 )
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)
109 decoder_sys_t *p_sys = p_dec->p_sys;
110 char psz_name[OMX_MAX_STRINGNAME_SIZE];
111 OMX_ERRORTYPE omx_error;
113 OMX_U8 **ppsz_roles = 0;
114 unsigned int i, j, len;
116 if(!psz_role) goto end;
117 len = strlen(psz_role);
121 bool b_found = false;
123 omx_error = p_sys->pf_component_enum(psz_name, OMX_MAX_STRINGNAME_SIZE, i);
124 if(omx_error != OMX_ErrorNone) break;
126 msg_Dbg(p_dec, "component %s", psz_name);
128 omx_error = p_sys->pf_get_roles_of_component(psz_name, &roles, 0);
129 if(omx_error != OMX_ErrorNone || !roles) continue;
131 ppsz_roles = malloc(roles * (sizeof(OMX_U8*) + OMX_MAX_STRINGNAME_SIZE));
132 if(!ppsz_roles) continue;
134 for( j = 0; j < roles; j++ )
135 ppsz_roles[j] = ((OMX_U8 *)(&ppsz_roles[roles])) +
136 j * OMX_MAX_STRINGNAME_SIZE;
138 omx_error = p_sys->pf_get_roles_of_component(psz_name, &roles, ppsz_roles);
139 if(omx_error != OMX_ErrorNone) roles = 0;
141 for(j = 0; j < roles; j++)
143 msg_Dbg(p_dec, " - role: %s", ppsz_roles[j]);
144 if(!strncmp((char *)ppsz_roles[j], psz_role, len)) b_found = true;
149 if(!b_found) continue;
151 if(p_sys->components >= MAX_COMPONENTS_LIST_SIZE)
153 msg_Dbg(p_dec, "too many matching components");
157 strncpy(p_sys->ppsz_components[p_sys->components], psz_name,
158 OMX_MAX_STRINGNAME_SIZE-1);
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]);
168 return p_sys->components;
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)
178 decoder_sys_t *p_sys = p_dec->p_sys;
179 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
181 if(!strcmp(p_sys->psz_component, "OMX.TI.Video.Decoder"))
183 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirOutput &&
184 p_fmt->i_codec == VLC_CODEC_I420)
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;
199 else if(!strcmp(p_sys->psz_component, "OMX.st.video_encoder"))
201 if(p_fmt->i_cat == VIDEO_ES)
203 /* Bellagio's encoder doesn't encode the framerate in Q16 */
204 def->format.video.xFramerate >>= 16;
208 return OMX_ErrorNone;
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,
217 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
218 OMX_ERRORTYPE omx_error;
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));
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;
237 if(def->eDir == OMX_DirInput || p_dec->p_sys->b_enc)
239 def->nBufferSize = def->format.video.nFrameWidth *
240 def->format.video.nFrameHeight * 2;
241 p_port->i_frame_size = def->nBufferSize;
243 if(!GetOmxVideoFormat(p_fmt->i_codec,
244 &def->format.video.eCompressionFormat, 0) )
246 if(!GetOmxChromaFormat(p_fmt->i_codec,
247 &def->format.video.eColorFormat, 0) )
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 );
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;
264 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
265 &p_fmt->i_codec, 0 ) )
267 omx_error = OMX_ErrorNotImplemented;
268 CHECK_ERROR(omx_error, "OMX color format %i not supported",
269 (int)def->format.video.eColorFormat );
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;
282 p_port->i_frame_size = def->nBufferSize;
283 if(def->eDir == OMX_DirInput)
285 if(!GetOmxAudioFormat(p_fmt->i_codec,
286 &def->format.audio.eEncoding, 0) )
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 );
295 if( !GetVlcAudioFormat(def->format.audio.eEncoding,
296 &p_fmt->i_codec, 0 ) )
298 omx_error = OMX_ErrorNotImplemented;
299 CHECK_ERROR(omx_error, "OMX audio encoding %i not supported",
300 (int)def->format.audio.eEncoding );
305 default: return OMX_ErrorNotImplemented;
308 ImplementationSpecificWorkarounds(p_dec, p_port, p_fmt);
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));
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));
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;
324 /* Deal with audio params */
325 if(p_fmt->i_cat == AUDIO_ES)
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,
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));
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,
349 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
350 OMX_ERRORTYPE omx_error;
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));
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;
367 if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
368 &p_fmt->i_codec, 0 ) )
370 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
371 &p_fmt->i_codec, 0 ) )
373 omx_error = OMX_ErrorNotImplemented;
374 CHECK_ERROR(omx_error, "OMX color format %i not supported",
375 (int)def->format.video.eColorFormat );
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 );
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;
387 if((int)p_port->i_frame_stride > def->format.video.nStride)
388 def->format.video.nStride = p_port->i_frame_stride;
390 p_port->i_frame_stride = def->format.video.nStride;
394 if( !GetVlcAudioFormat( def->format.audio.eEncoding,
395 &p_fmt->i_codec, 0 ) )
397 omx_error = OMX_ErrorNotImplemented;
398 CHECK_ERROR(omx_error, "OMX audio format %i not supported",
399 (int)def->format.audio.eEncoding );
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,
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));
413 if(p_fmt->audio.i_channels < 9)
415 static const int pi_channels_maps[9] =
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
432 p_fmt->audio.i_physical_channels =
433 p_fmt->audio.i_original_channels =
434 pi_channels_maps[p_fmt->audio.i_channels];
437 date_Init( &p_dec->p_sys->end_date, p_fmt->audio.i_rate, 1 );
441 default: return OMX_ErrorNotImplemented;
448 /*****************************************************************************
449 * DeinitialiseComponent: Deinitialise and unload an OMX component
450 *****************************************************************************/
451 static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
452 OMX_HANDLETYPE omx_handle)
454 decoder_sys_t *p_sys = p_dec->p_sys;
455 OMX_ERRORTYPE omx_error;
459 if(!omx_handle) return OMX_ErrorNone;
461 omx_error = OMX_GetState(omx_handle, &state);
462 CHECK_ERROR(omx_error, "ONX_GetState failed (%x)", omx_error );
464 if(state == OMX_StateExecuting)
466 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
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 );
473 omx_error = OMX_GetState(omx_handle, &state);
474 CHECK_ERROR(omx_error, "ONX_GetState failed (%x)", omx_error );
476 if(state == OMX_StateIdle)
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 );
482 for(i = 0; i < p_sys->ports; i++)
484 OmxPort *p_port = &p_sys->p_ports[i];
485 OMX_BUFFERHEADERTYPE *p_buffer;
487 for(j = 0; j < p_port->i_buffers; j++)
489 OMX_FIFO_GET(&p_port->fifo, p_buffer);
490 omx_error = OMX_FreeBuffer( omx_handle,
491 p_port->i_port_index, p_buffer );
493 if(omx_error != OMX_ErrorNone) break;
495 CHECK_ERROR(omx_error, "ONX_FreeBuffer failed (%x, %i, %i)",
496 omx_error, (int)p_port->i_port_index, j );
499 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
500 CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
504 for(i = 0; i < p_sys->ports; i++)
506 OmxPort *p_port = &p_sys->p_ports[i];
507 free(p_port->pp_buffers);
508 p_port->pp_buffers = 0;
510 omx_error = p_sys->pf_free_handle( omx_handle );
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)
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;
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;
532 omx_error = p_sys->pf_get_handle( &omx_handle, psz_component,
534 if(omx_error != OMX_ErrorNone)
536 msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
537 omx_error, ErrorToString(omx_error) );
540 strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
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);
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));
552 omx_error = OMX_SetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
554 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
556 if(omx_error == OMX_ErrorNone)
557 msg_Dbg(p_dec, "component standard role set to %s", role.cRole);
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, ¶m);
564 if(omx_error != OMX_ErrorNone) param.nPorts = 0;
566 for(i = 0; i < param.nPorts; i++)
570 /* Get port definition */
571 definition.nPortIndex = param.nStartPortNumber + i;
572 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
574 if(omx_error != OMX_ErrorNone) continue;
576 if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
577 else p_port = &p_sys->out;
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;
585 if(!p_sys->in.b_valid || !p_sys->out.b_valid)
587 omx_error = OMX_ErrorInvalidComponent;
588 CHECK_ERROR(omx_error, "couldn't find an input and output port");
591 /* Set port definitions */
592 for(i = 0; i < p_sys->ports; i++)
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;
599 /* Allocate our array for the omx buffers and enable ports */
600 for(i = 0; i < p_sys->ports; i++)
602 OmxPort *p_port = &p_sys->p_ports[i];
605 malloc(p_port->definition.nBufferCountActual *
606 sizeof(OMX_BUFFERHEADERTYPE*));
607 if(!p_port->pp_buffers)
609 omx_error = OMX_ErrorInsufficientResources;
610 CHECK_ERROR(omx_error, "memory allocation failed");
612 p_port->i_buffers = p_port->definition.nBufferCountActual;
615 if(!p_port->definition.bEnabled)
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 );
627 *p_handle = omx_handle;
628 return OMX_ErrorNone;
631 DeinitialiseComponent(p_dec, omx_handle);
636 /*****************************************************************************
637 * OpenDecoder: Create the decoder instance
638 *****************************************************************************/
639 static int OpenDecoder( vlc_object_t *p_this )
641 decoder_t *p_dec = (decoder_t*)p_this;
644 if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
647 status = OpenGeneric( p_this, false );
648 if(status != VLC_SUCCESS) return status;
650 p_dec->pf_decode_video = DecodeVideo;
651 p_dec->pf_decode_audio = DecodeAudio;
656 /*****************************************************************************
657 * OpenEncoder: Create the encoder instance
658 *****************************************************************************/
659 static int OpenEncoder( vlc_object_t *p_this )
661 encoder_t *p_enc = (encoder_t*)p_this;
664 if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
667 status = OpenGeneric( p_this, true );
668 if(status != VLC_SUCCESS) return status;
670 p_enc->pf_encode_video = EncodeVideo;
675 /*****************************************************************************
676 * OpenGeneric: Create the generic decoder/encoder instance
677 *****************************************************************************/
678 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
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;
689 /* Load the OMX core */
690 for( i = 0; ppsz_dll_list[i]; i++ )
692 dll_handle = dll_open( ppsz_dll_list[i] );
693 if( dll_handle ) break;
695 if( !dll_handle ) return VLC_EGENERIC;
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 )
706 msg_Warn( p_this, "cannot find OMX_* symbols in `%s' (%s)",
707 ppsz_dll_list[i], dlerror() );
708 dll_close(dll_handle);
711 if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle || !pf_component_enum )
713 dll_close(dll_handle);
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 )
720 dll_close(dll_handle);
724 /* Initialise the thread properties */
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;
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;
759 p_sys->p_ports = &p_sys->in;
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);
764 /* Initialise the OMX core */
765 omx_error = p_sys->pf_init();
766 if(omx_error != OMX_ErrorNone)
768 msg_Warn( p_this, "OMX_Init failed (%x: %s)", omx_error,
769 ErrorToString(omx_error) );
770 CloseGeneric(p_this);
773 p_sys->b_init = true;
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,
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);
787 /* Try to load and initialise a component */
788 for(i = 0; i < p_sys->components; i++)
790 omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
792 if(omx_error == OMX_ErrorNone) break;
794 CHECK_ERROR(omx_error, "no component could be initialised" );
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 );
800 /* Allocate omx buffers */
801 for(i = 0; i < p_sys->ports; i++)
803 OmxPort *p_port = &p_sys->p_ports[i];
805 for(j = 0; j < p_port->i_buffers; j++)
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);
814 if(0 && p_port->b_direct)
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);
821 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
822 p_port->i_port_index, 0,
823 p_port->definition.nBufferSize);
825 if(omx_error != OMX_ErrorNone) break;
826 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
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 );
833 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
834 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
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 );
842 /* Send codec configuration data */
843 if( p_dec->fmt_in.i_extra )
845 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
846 p_header->nFilledLen = p_dec->fmt_in.i_extra;
848 if(p_sys->in.b_direct)
850 p_header->pOutputPortPrivate = p_header->pBuffer;
851 p_header->pBuffer = p_dec->fmt_in.p_extra;
855 if(p_header->nFilledLen > p_header->nAllocLen)
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;
861 memcpy(p_header->pBuffer, p_dec->fmt_in.p_extra, p_header->nFilledLen);
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);
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;
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);
878 if(p_sys->b_error) goto error;
880 p_dec->b_need_packetized = true;
884 CloseGeneric(p_this);
888 /*****************************************************************************
890 *****************************************************************************/
891 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
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;
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,
904 if(omx_error != OMX_ErrorNone ||
905 !definition.format.video.nFrameWidth ||
906 !definition.format.video.nFrameHeight )
907 return OMX_ErrorUndefined;
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 );
914 for(i = 0; i < p_port->i_buffers; i++)
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 );
920 if(omx_error != OMX_ErrorNone) break;
922 CHECK_ERROR(omx_error, "ONX_FreeBuffer failed (%x, %i, %i)",
923 omx_error, (int)p_port->i_port_index, i );
925 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
926 CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
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,
933 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
934 omx_error, ErrorToString(omx_error));
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 );
941 for(i = 0; i < p_port->i_buffers; i++)
943 if(0 && p_port->b_direct)
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);
950 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
951 p_port->i_port_index, 0,
952 p_port->definition.nBufferSize);
954 if(omx_error != OMX_ErrorNone) break;
955 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
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 );
961 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
962 CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
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);
971 /*****************************************************************************
972 * DecodeVideo: Called to decode one frame
973 *****************************************************************************/
974 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
976 decoder_sys_t *p_sys = p_dec->p_sys;
977 picture_t *p_pic = NULL, *p_next_pic;
978 OMX_ERRORTYPE omx_error;
981 OMX_BUFFERHEADERTYPE *p_header;
984 if( !pp_block || !*pp_block )
989 /* Check for errors from codec */
992 msg_Dbg(p_dec, "error during decoding");
993 block_Release( p_block );
997 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
999 block_Release( p_block );
1000 if(!p_sys->in.b_flushed)
1002 msg_Dbg(p_dec, "flushing");
1003 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1004 p_sys->in.definition.nPortIndex, 0 );
1006 p_sys->in.b_flushed = true;
1010 /* Take care of decoded frames first */
1013 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1014 if(!p_header) break; /* No frame available */
1016 if(p_header->nFilledLen)
1018 p_pic = p_header->pAppPrivate;
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 */
1026 CopyOmxPicture(p_dec, p_pic, p_header);
1029 p_pic->date = p_header->nTimeStamp;
1030 p_header->nFilledLen = 0;
1031 p_header->pAppPrivate = 0;
1034 /* Get a new picture */
1035 if(p_sys->in.b_direct && !p_header->pAppPrivate)
1037 p_next_pic = decoder_NewPicture( p_dec );
1038 if(!p_next_pic) break;
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;
1047 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1050 #ifdef OMXIL_EXTRA_DEBUG
1051 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1053 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1056 /* Send the input buffer to the component */
1057 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
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;
1065 /* In direct mode we pass the input pointer as is.
1066 * Otherwise we memcopy the data */
1067 if(p_sys->in.b_direct)
1069 p_header->pOutputPortPrivate = p_header->pBuffer;
1070 p_header->pBuffer = p_block->p_buffer;
1071 p_header->pAppPrivate = p_block;
1075 if(p_header->nFilledLen > p_header->nAllocLen)
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;
1081 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1082 block_Release(p_block);
1085 #ifdef OMXIL_EXTRA_DEBUG
1086 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1087 (int)p_header->nFilledLen );
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 */
1094 /* Handle the PortSettingsChanged events */
1095 for(i = 0; i < p_sys->ports; i++)
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);
1106 /*****************************************************************************
1107 * DecodeAudio: Called to decode one frame
1108 *****************************************************************************/
1109 aout_buffer_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
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;
1118 if( !pp_block || !*pp_block ) return NULL;
1120 p_block = *pp_block;
1122 /* Check for errors from codec */
1125 msg_Dbg(p_dec, "error during decoding");
1126 block_Release( p_block );
1130 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1132 block_Release( p_block );
1133 date_Set( &p_sys->end_date, 0 );
1134 if(!p_sys->in.b_flushed)
1136 msg_Dbg(p_dec, "flushing");
1137 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1138 p_sys->in.definition.nPortIndex, 0 );
1140 p_sys->in.b_flushed = true;
1144 if( !date_Get( &p_sys->end_date ) )
1146 if( !p_block->i_pts )
1148 /* We've just started the stream, wait for the first PTS. */
1149 block_Release( p_block );
1152 date_Set( &p_sys->end_date, p_block->i_pts );
1155 /* Take care of decoded frames first */
1158 unsigned int i_samples;
1160 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1161 if(!p_header) break; /* No frame available */
1163 i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1166 p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1167 if( !p_buffer ) break; /* No audio buffer available */
1169 memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1170 p_header->nFilledLen = 0;
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 );
1176 p_buffer->i_pts = date_Get( &p_sys->end_date );
1177 p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1181 #ifdef OMXIL_EXTRA_DEBUG
1182 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1184 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1185 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1189 /* Send the input buffer to the component */
1190 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
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;
1198 /* In direct mode we pass the input pointer as is.
1199 * Otherwise we memcopy the data */
1200 if(p_sys->in.b_direct)
1202 p_header->pOutputPortPrivate = p_header->pBuffer;
1203 p_header->pBuffer = p_block->p_buffer;
1204 p_header->pAppPrivate = p_block;
1208 if(p_header->nFilledLen > p_header->nAllocLen)
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;
1214 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1215 block_Release(p_block);
1218 #ifdef OMXIL_EXTRA_DEBUG
1219 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1220 (int)p_header->nFilledLen );
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 */
1227 /* Handle the PortSettingsChanged events */
1228 for(i = 0; i < p_sys->ports; i++)
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);
1239 /*****************************************************************************
1240 * EncodeVideo: Called to encode one frame
1241 *****************************************************************************/
1242 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1244 decoder_t *p_dec = ( decoder_t *)p_enc;
1245 decoder_sys_t *p_sys = p_dec->p_sys;
1246 OMX_ERRORTYPE omx_error;
1249 OMX_BUFFERHEADERTYPE *p_header;
1250 block_t *p_block = 0;
1252 /* Check for errors from codec */
1255 msg_Dbg(p_dec, "error during encoding");
1259 /* Send the input buffer to the component */
1260 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1263 /* In direct mode we pass the input pointer as is.
1264 * Otherwise we memcopy the data */
1265 if(p_sys->in.b_direct)
1267 p_header->pOutputPortPrivate = p_header->pBuffer;
1268 p_header->pBuffer = p_pic->p[0].p_pixels;
1272 CopyVlcPicture(p_dec, p_header, p_pic);
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 );
1283 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1284 p_sys->in.b_flushed = false;
1287 /* Handle the PortSettingsChanged events */
1288 for(i = 0; i < p_sys->ports; i++)
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);
1296 /* Wait for the decoded frame */
1299 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1301 if(p_header->nFilledLen)
1303 if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1305 /* TODO: need to store codec config */
1306 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1309 p_block = p_header->pAppPrivate;
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 );
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;
1324 #ifdef OMXIL_EXTRA_DEBUG
1325 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1327 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1330 msg_Dbg(p_dec, "done");
1334 /*****************************************************************************
1335 * CloseGeneric: omxil decoder destruction
1336 *****************************************************************************/
1337 static void CloseGeneric( vlc_object_t *p_this )
1339 decoder_t *p_dec = (decoder_t *)p_this;
1340 decoder_sys_t *p_sys = p_dec->p_sys;
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 );
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);
1356 /*****************************************************************************
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 )
1363 decoder_t *p_dec = (decoder_t *)app_data;
1364 decoder_sys_t *p_sys = p_dec->p_sys;
1370 case OMX_EventCmdComplete:
1371 switch ((OMX_STATETYPE)data_1)
1373 case OMX_CommandStateSet:
1374 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %s)", EventToString(event),
1375 CommandToString(data_1), StateToString(data_2) );
1379 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u)", EventToString(event),
1380 CommandToString(data_1), (unsigned int)data_2 );
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;
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;
1401 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1402 (unsigned int)data_1, (unsigned int)data_2 );
1406 PostOmxEvent(p_dec, event, data_1, data_2, event_data);
1407 return OMX_ErrorNone;
1410 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1411 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1413 decoder_t *p_dec = (decoder_t *)app_data;
1414 decoder_sys_t *p_sys = p_dec->p_sys;
1417 #ifdef OMXIL_EXTRA_DEBUG
1418 msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1421 if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
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;
1428 OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1430 return OMX_ErrorNone;
1433 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1434 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1436 decoder_t *p_dec = (decoder_t *)app_data;
1437 decoder_sys_t *p_sys = p_dec->p_sys;
1440 #ifdef OMXIL_EXTRA_DEBUG
1441 msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
1442 (int)omx_header->nFilledLen );
1445 if(omx_header->pInputPortPrivate)
1447 omx_header->pBuffer = omx_header->pInputPortPrivate;
1449 OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1451 return OMX_ErrorNone;