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 *****************************************************************************/
33 /* On dll_open, just check that the OMX_Init symbol already is loaded */
34 # define dll_open(name) dlsym(RTLD_DEFAULT, "OMX_Init")
35 # define dll_close(handle) do { } while (0)
36 # define dlsym(handle, name) dlsym(RTLD_DEFAULT, name)
38 # define dll_open(name) dlopen( name, RTLD_NOW )
39 # define dll_close(handle) dlclose(handle)
44 #include <vlc_common.h>
45 #include <vlc_plugin.h>
46 #include <vlc_codec.h>
48 #include <vlc_block_helper.h>
50 #include "../h264_nal.h"
54 //#define OMXIL_EXTRA_DEBUG
56 /*****************************************************************************
57 * List of OpenMAX IL core we will try in order
58 *****************************************************************************/
59 static const char *ppsz_dll_list[] =
62 "libiomx.so", /* Not used when using IOMX, the lib should already be loaded */
64 "libOMX_Core.so", /* TI OMAP IL core */
65 "libOmxCore.so", /* Qualcomm IL core */
66 "libomxil-bellagio.so", /* Bellagio IL core */
71 /*****************************************************************************
72 * Global OMX Core instance, shared between module instances
73 *****************************************************************************/
74 static vlc_mutex_t omx_core_mutex = VLC_STATIC_MUTEX;
75 static unsigned int omx_refcount = 0;
76 static void *dll_handle;
77 static OMX_ERRORTYPE (*pf_init) (void);
78 static OMX_ERRORTYPE (*pf_deinit) (void);
79 static OMX_ERRORTYPE (*pf_get_handle) (OMX_HANDLETYPE *, OMX_STRING,
80 OMX_PTR, OMX_CALLBACKTYPE *);
81 static OMX_ERRORTYPE (*pf_free_handle) (OMX_HANDLETYPE);
82 static OMX_ERRORTYPE (*pf_component_enum)(OMX_STRING, OMX_U32, OMX_U32);
83 static OMX_ERRORTYPE (*pf_get_roles_of_component)(OMX_STRING, OMX_U32 *, OMX_U8 **);
85 /*****************************************************************************
87 *****************************************************************************/
88 static int OpenDecoder( vlc_object_t * );
89 static int OpenEncoder( vlc_object_t * );
90 static int OpenGeneric( vlc_object_t *, bool b_encode );
91 static void CloseGeneric( vlc_object_t * );
93 static picture_t *DecodeVideo( decoder_t *, block_t ** );
94 static aout_buffer_t *DecodeAudio ( decoder_t *, block_t ** );
95 static block_t *EncodeVideo( encoder_t *, picture_t * );
97 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE, OMX_PTR, OMX_EVENTTYPE,
98 OMX_U32, OMX_U32, OMX_PTR );
99 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE, OMX_PTR,
100 OMX_BUFFERHEADERTYPE * );
101 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE, OMX_PTR,
102 OMX_BUFFERHEADERTYPE * );
104 /*****************************************************************************
106 *****************************************************************************/
108 set_description( N_("Audio/Video decoder (using OpenMAX IL)") )
109 set_category( CAT_INPUT )
110 set_subcategory( SUBCAT_INPUT_VCODEC )
111 set_section( N_("Decoding") , NULL )
112 set_capability( "decoder", 80 )
113 set_callbacks( OpenDecoder, CloseGeneric )
116 set_section( N_("Encoding") , NULL )
117 set_description( N_("Video encoder (using OpenMAX IL)") )
118 set_capability( "encoder", 0 )
119 set_callbacks( OpenEncoder, CloseGeneric )
122 /*****************************************************************************
123 * CreateComponentsList: creates a list of components matching the given role
124 *****************************************************************************/
125 static int CreateComponentsList(decoder_t *p_dec, const char *psz_role)
127 decoder_sys_t *p_sys = p_dec->p_sys;
128 char psz_name[OMX_MAX_STRINGNAME_SIZE];
129 OMX_ERRORTYPE omx_error;
131 OMX_U8 **ppsz_roles = 0;
132 unsigned int i, j, len;
134 if(!psz_role) goto end;
135 len = strlen(psz_role);
139 bool b_found = false;
141 omx_error = pf_component_enum(psz_name, OMX_MAX_STRINGNAME_SIZE, i);
142 if(omx_error != OMX_ErrorNone) break;
144 msg_Dbg(p_dec, "component %s", psz_name);
146 omx_error = pf_get_roles_of_component(psz_name, &roles, 0);
147 if(omx_error != OMX_ErrorNone || !roles) continue;
149 ppsz_roles = malloc(roles * (sizeof(OMX_U8*) + OMX_MAX_STRINGNAME_SIZE));
150 if(!ppsz_roles) continue;
152 for( j = 0; j < roles; j++ )
153 ppsz_roles[j] = ((OMX_U8 *)(&ppsz_roles[roles])) +
154 j * OMX_MAX_STRINGNAME_SIZE;
156 omx_error = pf_get_roles_of_component(psz_name, &roles, ppsz_roles);
157 if(omx_error != OMX_ErrorNone) roles = 0;
159 for(j = 0; j < roles; j++)
161 msg_Dbg(p_dec, " - role: %s", ppsz_roles[j]);
162 if(!strncmp((char *)ppsz_roles[j], psz_role, len)) b_found = true;
167 if(!b_found) continue;
169 if(p_sys->components >= MAX_COMPONENTS_LIST_SIZE)
171 msg_Dbg(p_dec, "too many matching components");
175 strncpy(p_sys->ppsz_components[p_sys->components], psz_name,
176 OMX_MAX_STRINGNAME_SIZE-1);
181 msg_Dbg(p_dec, "found %i matching components for role %s",
182 p_sys->components, psz_role);
183 for( i = 0; i < p_sys->components; i++ )
184 msg_Dbg(p_dec, "- %s", p_sys->ppsz_components[i]);
186 return p_sys->components;
189 /*****************************************************************************
190 * ImplementationSpecificWorkarounds: place-holder for implementation
191 * specific workarounds
192 *****************************************************************************/
193 static OMX_ERRORTYPE ImplementationSpecificWorkarounds(decoder_t *p_dec,
194 OmxPort *p_port, es_format_t *p_fmt)
196 decoder_sys_t *p_sys = p_dec->p_sys;
197 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
198 int i_profile = 0xFFFF, i_level = 0xFFFF;
200 /* Try to find out the profile of the video */
201 while(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
202 p_fmt->i_codec == VLC_CODEC_H264)
204 uint8_t *p = (uint8_t*)p_dec->fmt_in.p_extra;
205 if(!p || !p_dec->fmt_in.p_extra) break;
207 /* Check the profile / level */
208 if(p_dec->fmt_in.i_original_fourcc == VLC_FOURCC('a','v','c','1') &&
211 if(p_dec->fmt_in.i_extra < 12) break;
212 p_sys->i_nal_size_length = 1 + (p[4]&0x03);
213 if( !(p[5]&0x1f) ) break;
218 if(p_dec->fmt_in.i_extra < 8) break;
219 if(!p[0] && !p[1] && !p[2] && p[3] == 1) p += 4;
220 else if(!p[0] && !p[1] && p[2] == 1) p += 3;
224 if( ((*p++)&0x1f) != 7) break;
226 /* Get profile/level out of first SPS */
232 if(!strcmp(p_sys->psz_component, "OMX.TI.Video.Decoder"))
234 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
235 p_fmt->i_codec == VLC_CODEC_H264 &&
236 (i_profile != 66 || i_level > 30))
238 msg_Dbg(p_dec, "h264 profile/level not supported (0x%x, 0x%x)",
240 return OMX_ErrorNotImplemented;
243 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirOutput &&
244 p_fmt->i_codec == VLC_CODEC_I420)
246 /* I420 xvideo is slow on OMAP */
247 def->format.video.eColorFormat = OMX_COLOR_FormatCbYCrY;
248 GetVlcChromaFormat( def->format.video.eColorFormat,
249 &p_fmt->i_codec, 0 );
250 GetVlcChromaSizes( p_fmt->i_codec,
251 def->format.video.nFrameWidth,
252 def->format.video.nFrameHeight,
253 &p_port->i_frame_size, &p_port->i_frame_stride,
254 &p_port->i_frame_stride_chroma_div );
255 def->format.video.nStride = p_port->i_frame_stride;
256 def->nBufferSize = p_port->i_frame_size;
259 else if(!strcmp(p_sys->psz_component, "OMX.st.video_encoder"))
261 if(p_fmt->i_cat == VIDEO_ES)
263 /* Bellagio's encoder doesn't encode the framerate in Q16 */
264 def->format.video.xFramerate >>= 16;
267 else if (!strncmp(p_sys->psz_component, "OMX.qcom.video.decoder.",
268 strlen("OMX.qcom.video.decoder")))
270 /* qdsp6 refuses buffer size larger than 450K on input port */
271 if (def->nBufferSize > 450 * 1024)
273 def->nBufferSize = 450 * 1024;
274 p_port->i_frame_size = def->nBufferSize;
278 return OMX_ErrorNone;
281 /*****************************************************************************
282 * SetPortDefinition: set definition of the omx port based on the vlc format
283 *****************************************************************************/
284 static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
287 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
288 OMX_ERRORTYPE omx_error;
290 omx_error = OMX_GetParameter(p_port->omx_handle,
291 OMX_IndexParamPortDefinition, def);
292 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
293 omx_error, ErrorToString(omx_error));
298 def->format.video.nFrameWidth = p_fmt->video.i_width;
299 def->format.video.nFrameHeight = p_fmt->video.i_height;
300 if(def->format.video.eCompressionFormat == OMX_VIDEO_CodingUnused)
301 def->format.video.nStride = def->format.video.nFrameWidth;
302 if( p_fmt->video.i_frame_rate > 0 &&
303 p_fmt->video.i_frame_rate_base > 0 )
304 def->format.video.xFramerate = (p_fmt->video.i_frame_rate << 16) /
305 p_fmt->video.i_frame_rate_base;
307 if(def->eDir == OMX_DirInput || p_dec->p_sys->b_enc)
309 def->nBufferSize = def->format.video.nFrameWidth *
310 def->format.video.nFrameHeight * 2;
311 p_port->i_frame_size = def->nBufferSize;
313 if(!GetOmxVideoFormat(p_fmt->i_codec,
314 &def->format.video.eCompressionFormat, 0) )
316 if(!GetOmxChromaFormat(p_fmt->i_codec,
317 &def->format.video.eColorFormat, 0) )
319 omx_error = OMX_ErrorNotImplemented;
320 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
321 (char *)&p_fmt->i_codec );
323 GetVlcChromaSizes( p_fmt->i_codec,
324 def->format.video.nFrameWidth,
325 def->format.video.nFrameHeight,
326 &p_port->i_frame_size, &p_port->i_frame_stride,
327 &p_port->i_frame_stride_chroma_div );
328 def->format.video.nStride = p_port->i_frame_stride;
329 def->nBufferSize = p_port->i_frame_size;
334 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
335 &p_fmt->i_codec, 0 ) )
337 omx_error = OMX_ErrorNotImplemented;
338 CHECK_ERROR(omx_error, "OMX color format %i not supported",
339 (int)def->format.video.eColorFormat );
341 GetVlcChromaSizes( p_fmt->i_codec,
342 def->format.video.nFrameWidth,
343 def->format.video.nFrameHeight,
344 &p_port->i_frame_size, &p_port->i_frame_stride,
345 &p_port->i_frame_stride_chroma_div );
346 def->format.video.nStride = p_port->i_frame_stride;
347 def->nBufferSize = p_port->i_frame_size;
352 p_port->i_frame_size = def->nBufferSize;
353 if(def->eDir == OMX_DirInput)
355 if(!GetOmxAudioFormat(p_fmt->i_codec,
356 &def->format.audio.eEncoding, 0) )
358 omx_error = OMX_ErrorNotImplemented;
359 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
360 (char *)&p_fmt->i_codec );
365 if( !OmxToVlcAudioFormat(def->format.audio.eEncoding,
366 &p_fmt->i_codec, 0 ) )
368 omx_error = OMX_ErrorNotImplemented;
369 CHECK_ERROR(omx_error, "OMX audio encoding %i not supported",
370 (int)def->format.audio.eEncoding );
375 default: return OMX_ErrorNotImplemented;
378 omx_error = ImplementationSpecificWorkarounds(p_dec, p_port, p_fmt);
379 CHECK_ERROR(omx_error, "ImplementationSpecificWorkarounds failed (%x : %s)",
380 omx_error, ErrorToString(omx_error));
382 omx_error = OMX_SetParameter(p_port->omx_handle,
383 OMX_IndexParamPortDefinition, def);
384 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
385 omx_error, ErrorToString(omx_error));
387 omx_error = OMX_GetParameter(p_port->omx_handle,
388 OMX_IndexParamPortDefinition, def);
389 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
390 omx_error, ErrorToString(omx_error));
392 if(p_port->i_frame_size > def->nBufferSize)
393 def->nBufferSize = p_port->i_frame_size;
394 p_port->i_frame_size = def->nBufferSize;
396 /* Deal with audio params */
397 if(p_fmt->i_cat == AUDIO_ES)
399 omx_error = SetAudioParameters(p_port->omx_handle,
400 &p_port->format_param, def->nPortIndex,
401 def->format.audio.eEncoding,
402 p_fmt->audio.i_channels,
405 p_fmt->audio.i_bitspersample,
406 p_fmt->audio.i_blockalign);
407 CHECK_ERROR(omx_error, "SetAudioParameters failed (%x : %s)",
408 omx_error, ErrorToString(omx_error));
415 /*****************************************************************************
416 * GetPortDefinition: set vlc format based on the definition of the omx port
417 *****************************************************************************/
418 static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
421 decoder_sys_t *p_sys = p_dec->p_sys;
422 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
423 OMX_ERRORTYPE omx_error;
425 omx_error = OMX_GetParameter(p_port->omx_handle,
426 OMX_IndexParamPortDefinition, def);
427 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
428 omx_error, ErrorToString(omx_error));
433 p_fmt->video.i_width = def->format.video.nFrameWidth;
434 p_fmt->video.i_visible_width = def->format.video.nFrameWidth;
435 p_fmt->video.i_height = def->format.video.nFrameHeight;
436 p_fmt->video.i_visible_height = def->format.video.nFrameHeight;
437 p_fmt->video.i_frame_rate = p_dec->fmt_in.video.i_frame_rate;
438 p_fmt->video.i_frame_rate_base = p_dec->fmt_in.video.i_frame_rate_base;
440 /* Hack: Nexus One (stock firmware with binary OMX driver blob)
441 * claims to output 420Planar even though it in in practice is
443 if(def->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar &&
444 !strncmp(p_sys->psz_component, "OMX.qcom.video.decoder",
445 strlen("OMX.qcom.video.decoder")))
446 def->format.video.eColorFormat = OMX_QCOM_COLOR_FormatYVU420SemiPlanar;
448 if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
449 &p_fmt->i_codec, 0 ) )
451 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
452 &p_fmt->i_codec, 0 ) )
454 omx_error = OMX_ErrorNotImplemented;
455 CHECK_ERROR(omx_error, "OMX color format %i not supported",
456 (int)def->format.video.eColorFormat );
458 GetVlcChromaSizes( p_fmt->i_codec,
459 def->format.video.nFrameWidth,
460 def->format.video.nFrameHeight,
461 &p_port->i_frame_size, &p_port->i_frame_stride,
462 &p_port->i_frame_stride_chroma_div );
464 if(p_port->i_frame_size > def->nBufferSize)
465 def->nBufferSize = p_port->i_frame_size;
466 p_port->i_frame_size = def->nBufferSize;
468 if((int)p_port->i_frame_stride > def->format.video.nStride)
469 def->format.video.nStride = p_port->i_frame_stride;
471 p_port->i_frame_stride = def->format.video.nStride;
475 if( !OmxToVlcAudioFormat( def->format.audio.eEncoding,
476 &p_fmt->i_codec, 0 ) )
478 omx_error = OMX_ErrorNotImplemented;
479 CHECK_ERROR(omx_error, "OMX audio format %i not supported",
480 (int)def->format.audio.eEncoding );
483 omx_error = GetAudioParameters(p_port->omx_handle,
484 &p_port->format_param, def->nPortIndex,
485 def->format.audio.eEncoding,
486 &p_fmt->audio.i_channels,
487 &p_fmt->audio.i_rate,
489 &p_fmt->audio.i_bitspersample,
490 &p_fmt->audio.i_blockalign);
491 CHECK_ERROR(omx_error, "GetAudioParameters failed (%x : %s)",
492 omx_error, ErrorToString(omx_error));
494 if(p_fmt->audio.i_channels < 9)
496 static const int pi_channels_maps[9] =
498 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
499 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
500 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
501 | AOUT_CHAN_REARRIGHT,
502 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
503 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
504 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
505 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
506 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
507 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
508 | AOUT_CHAN_MIDDLERIGHT,
509 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
510 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
513 p_fmt->audio.i_physical_channels =
514 p_fmt->audio.i_original_channels =
515 pi_channels_maps[p_fmt->audio.i_channels];
518 date_Init( &p_dec->p_sys->end_date, p_fmt->audio.i_rate, 1 );
522 default: return OMX_ErrorNotImplemented;
529 /*****************************************************************************
530 * DeinitialiseComponent: Deinitialise and unload an OMX component
531 *****************************************************************************/
532 static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
533 OMX_HANDLETYPE omx_handle)
535 decoder_sys_t *p_sys = p_dec->p_sys;
536 OMX_ERRORTYPE omx_error;
540 if(!omx_handle) return OMX_ErrorNone;
542 omx_error = OMX_GetState(omx_handle, &state);
543 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
545 if(state == OMX_StateExecuting)
547 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
549 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
550 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
551 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
554 omx_error = OMX_GetState(omx_handle, &state);
555 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
557 if(state == OMX_StateIdle)
559 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
560 OMX_StateLoaded, 0 );
561 CHECK_ERROR(omx_error, "OMX_CommandStateSet Loaded failed (%x)", omx_error );
563 for(i = 0; i < p_sys->ports; i++)
565 OmxPort *p_port = &p_sys->p_ports[i];
566 OMX_BUFFERHEADERTYPE *p_buffer;
568 for(j = 0; j < p_port->i_buffers; j++)
570 OMX_FIFO_GET(&p_port->fifo, p_buffer);
571 omx_error = OMX_FreeBuffer( omx_handle,
572 p_port->i_port_index, p_buffer );
574 if(omx_error != OMX_ErrorNone) break;
576 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
577 omx_error, (int)p_port->i_port_index, j );
580 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
581 CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
585 for(i = 0; i < p_sys->ports; i++)
587 OmxPort *p_port = &p_sys->p_ports[i];
588 free(p_port->pp_buffers);
589 p_port->pp_buffers = 0;
591 omx_error = pf_free_handle( omx_handle );
595 /*****************************************************************************
596 * InitialiseComponent: Load and initialise an OMX component
597 *****************************************************************************/
598 static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
599 OMX_STRING psz_component, OMX_HANDLETYPE *p_handle)
601 static OMX_CALLBACKTYPE callbacks =
602 { OmxEventHandler, OmxEmptyBufferDone, OmxFillBufferDone };
603 decoder_sys_t *p_sys = p_dec->p_sys;
604 OMX_HANDLETYPE omx_handle;
605 OMX_ERRORTYPE omx_error;
607 OMX_U8 psz_role[OMX_MAX_STRINGNAME_SIZE];
608 OMX_PARAM_COMPONENTROLETYPE role;
609 OMX_PARAM_PORTDEFINITIONTYPE definition;
610 OMX_PORT_PARAM_TYPE param;
613 omx_error = pf_get_handle( &omx_handle, psz_component, p_dec, &callbacks );
614 if(omx_error != OMX_ErrorNone)
616 msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
617 omx_error, ErrorToString(omx_error) );
620 strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
622 omx_error = OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
623 if(omx_error == OMX_ErrorNone)
624 msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
626 msg_Dbg(p_dec, "loaded component %s", psz_component);
627 PrintOmx(p_dec, omx_handle, OMX_ALL);
629 /* Set component role */
630 OMX_INIT_STRUCTURE(role);
631 strcpy((char*)role.cRole,
632 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec : p_dec->fmt_in.i_codec,
633 p_dec->fmt_in.i_cat, p_sys->b_enc));
635 omx_error = OMX_SetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
637 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
639 if(omx_error == OMX_ErrorNone)
640 msg_Dbg(p_dec, "component standard role set to %s", role.cRole);
642 /* Find the input / output ports */
643 OMX_INIT_STRUCTURE(param);
644 OMX_INIT_STRUCTURE(definition);
645 omx_error = OMX_GetParameter(omx_handle, p_dec->fmt_in.i_cat == VIDEO_ES ?
646 OMX_IndexParamVideoInit : OMX_IndexParamAudioInit, ¶m);
647 if(omx_error != OMX_ErrorNone) param.nPorts = 0;
649 for(i = 0; i < param.nPorts; i++)
653 /* Get port definition */
654 definition.nPortIndex = param.nStartPortNumber + i;
655 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
657 if(omx_error != OMX_ErrorNone) continue;
659 if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
660 else p_port = &p_sys->out;
662 p_port->b_valid = true;
663 p_port->i_port_index = definition.nPortIndex;
664 p_port->definition = definition;
665 p_port->omx_handle = omx_handle;
668 if(!p_sys->in.b_valid || !p_sys->out.b_valid)
670 omx_error = OMX_ErrorInvalidComponent;
671 CHECK_ERROR(omx_error, "couldn't find an input and output port");
674 if(!strncmp(p_sys->psz_component, "OMX.SEC.", 8))
677 omx_error = OMX_GetExtensionIndex(omx_handle, (OMX_STRING) "OMX.SEC.index.ThumbnailMode", &index);
678 if(omx_error == OMX_ErrorNone)
680 OMX_BOOL enable = OMX_TRUE;
681 omx_error = OMX_SetConfig(omx_handle, index, &enable);
682 CHECK_ERROR(omx_error, "Unable to set ThumbnailMode");
686 /* Set port definitions */
687 for(i = 0; i < p_sys->ports; i++)
689 omx_error = SetPortDefinition(p_dec, &p_sys->p_ports[i],
690 p_sys->p_ports[i].p_fmt);
691 if(omx_error != OMX_ErrorNone) goto error;
694 /* Allocate our array for the omx buffers and enable ports */
695 for(i = 0; i < p_sys->ports; i++)
697 OmxPort *p_port = &p_sys->p_ports[i];
700 malloc(p_port->definition.nBufferCountActual *
701 sizeof(OMX_BUFFERHEADERTYPE*));
702 if(!p_port->pp_buffers)
704 omx_error = OMX_ErrorInsufficientResources;
705 CHECK_ERROR(omx_error, "memory allocation failed");
707 p_port->i_buffers = p_port->definition.nBufferCountActual;
710 if(!p_port->definition.bEnabled)
712 omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
713 p_port->i_port_index, NULL);
714 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
715 (int)p_port->i_port_index, omx_error );
716 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
717 CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
718 (int)p_port->i_port_index, omx_error );
722 *p_handle = omx_handle;
723 return OMX_ErrorNone;
726 DeinitialiseComponent(p_dec, omx_handle);
731 /*****************************************************************************
732 * OpenDecoder: Create the decoder instance
733 *****************************************************************************/
734 static int OpenDecoder( vlc_object_t *p_this )
736 decoder_t *p_dec = (decoder_t*)p_this;
739 if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
743 if( p_dec->fmt_in.i_cat != VIDEO_ES && !p_dec->b_force)
747 status = OpenGeneric( p_this, false );
748 if(status != VLC_SUCCESS) return status;
750 p_dec->pf_decode_video = DecodeVideo;
751 p_dec->pf_decode_audio = DecodeAudio;
756 /*****************************************************************************
757 * OpenEncoder: Create the encoder instance
758 *****************************************************************************/
759 static int OpenEncoder( vlc_object_t *p_this )
761 encoder_t *p_enc = (encoder_t*)p_this;
764 if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
767 status = OpenGeneric( p_this, true );
768 if(status != VLC_SUCCESS) return status;
770 p_enc->pf_encode_video = EncodeVideo;
775 /*****************************************************************************
776 * OpenGeneric: Create the generic decoder/encoder instance
777 *****************************************************************************/
778 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
780 decoder_t *p_dec = (decoder_t*)p_this;
781 decoder_sys_t *p_sys;
782 OMX_ERRORTYPE omx_error;
783 OMX_BUFFERHEADERTYPE *p_header;
786 vlc_mutex_lock( &omx_core_mutex );
787 if( omx_refcount > 0 )
790 /* Load the OMX core */
791 for( i = 0; ppsz_dll_list[i]; i++ )
793 dll_handle = dll_open( ppsz_dll_list[i] );
794 if( dll_handle ) break;
798 vlc_mutex_unlock( &omx_core_mutex );
802 pf_init = dlsym( dll_handle, "OMX_Init" );
803 pf_deinit = dlsym( dll_handle, "OMX_Deinit" );
804 pf_get_handle = dlsym( dll_handle, "OMX_GetHandle" );
805 pf_free_handle = dlsym( dll_handle, "OMX_FreeHandle" );
806 pf_component_enum = dlsym( dll_handle, "OMX_ComponentNameEnum" );
807 pf_get_roles_of_component = dlsym( dll_handle, "OMX_GetRolesOfComponent" );
808 if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle ||
809 !pf_component_enum || !pf_get_roles_of_component )
811 msg_Warn( p_this, "cannot find OMX_* symbols in `%s' (%s)",
812 ppsz_dll_list[i], dlerror() );
813 dll_close(dll_handle);
814 vlc_mutex_unlock( &omx_core_mutex );
819 /* Allocate the memory needed to store the decoder's structure */
820 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
822 if( omx_refcount == 0 )
823 dll_close(dll_handle);
824 vlc_mutex_unlock( &omx_core_mutex );
828 /* Initialise the thread properties */
831 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
832 p_dec->fmt_out.video = p_dec->fmt_in.video;
833 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
834 p_dec->fmt_out.i_codec = 0;
836 p_sys->b_enc = b_encode;
837 p_sys->pp_last_event = &p_sys->p_events;
838 vlc_mutex_init (&p_sys->mutex);
839 vlc_cond_init (&p_sys->cond);
840 vlc_mutex_init (&p_sys->lock);
841 vlc_mutex_init (&p_sys->in.fifo.lock);
842 vlc_cond_init (&p_sys->in.fifo.wait);
843 p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
844 p_sys->in.fifo.pp_last = &p_sys->in.fifo.p_first;
845 p_sys->in.b_direct = false;
846 p_sys->in.b_flushed = true;
847 p_sys->in.p_fmt = &p_dec->fmt_in;
848 vlc_mutex_init (&p_sys->out.fifo.lock);
849 vlc_cond_init (&p_sys->out.fifo.wait);
850 p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
851 p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
852 p_sys->out.b_direct = true;
853 p_sys->out.b_flushed = true;
854 p_sys->out.p_fmt = &p_dec->fmt_out;
856 p_sys->p_ports = &p_sys->in;
858 msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
859 (char *)&p_dec->fmt_out.i_codec);
861 /* Initialise the OMX core */
862 omx_error = omx_refcount > 0 ? OMX_ErrorNone : pf_init();
864 if(omx_error != OMX_ErrorNone)
866 msg_Warn( p_this, "OMX_Init failed (%x: %s)", omx_error,
867 ErrorToString(omx_error) );
868 vlc_mutex_unlock( &omx_core_mutex );
869 CloseGeneric(p_this);
872 p_sys->b_init = true;
873 vlc_mutex_unlock( &omx_core_mutex );
875 /* Enumerate components and build a list of the one we want to try */
876 if( !CreateComponentsList(p_dec,
877 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
878 p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
881 msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
882 (char *)&p_dec->fmt_in.i_codec );
883 CloseGeneric(p_this);
887 /* Try to load and initialise a component */
888 omx_error = OMX_ErrorUndefined;
889 for(i = 0; i < p_sys->components; i++)
892 /* ignore OpenCore software codecs */
893 if (!strncmp(p_sys->ppsz_components[i], "OMX.PV.", 7))
896 omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
898 if(omx_error == OMX_ErrorNone) break;
900 CHECK_ERROR(omx_error, "no component could be initialised" );
902 /* Move component to Idle then Executing state */
903 OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
904 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
906 /* Allocate omx buffers */
907 for(i = 0; i < p_sys->ports; i++)
909 OmxPort *p_port = &p_sys->p_ports[i];
911 for(j = 0; j < p_port->i_buffers; j++)
914 #define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
915 char *p_buf = malloc(p_port->definition.nBufferSize +
916 p_port->definition.nBufferAlignment);
917 p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
920 if(0 && p_port->b_direct)
922 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
923 p_port->i_port_index, 0,
924 p_port->definition.nBufferSize, (void*)1);
927 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
928 p_port->i_port_index, 0,
929 p_port->definition.nBufferSize);
931 if(omx_error != OMX_ErrorNone) break;
932 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
934 p_port->i_buffers = j;
935 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
936 omx_error, (int)p_port->i_port_index, j );
939 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
940 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
942 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
943 OMX_StateExecuting, 0);
944 CHECK_ERROR(omx_error, "OMX_CommandStateSet Executing failed (%x)", omx_error );
945 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
946 CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
948 /* Send codec configuration data */
949 if( p_dec->fmt_in.i_extra )
951 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
952 p_header->nFilledLen = p_dec->fmt_in.i_extra;
954 /* Convert H.264 NAL format to annex b */
955 if( p_sys->i_nal_size_length && !p_sys->in.b_direct )
957 p_header->nFilledLen = 0;
958 convert_sps_pps( p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
959 p_header->pBuffer, p_header->nAllocLen,
960 (uint32_t*) &p_header->nFilledLen, NULL );
962 else if(p_sys->in.b_direct)
964 p_header->pOutputPortPrivate = p_header->pBuffer;
965 p_header->pBuffer = p_dec->fmt_in.p_extra;
969 if(p_header->nFilledLen > p_header->nAllocLen)
971 msg_Dbg(p_dec, "buffer too small (%i,%i)", (int)p_header->nFilledLen,
972 (int)p_header->nAllocLen);
973 p_header->nFilledLen = p_header->nAllocLen;
975 memcpy(p_header->pBuffer, p_dec->fmt_in.p_extra, p_header->nFilledLen);
978 p_header->nOffset = 0;
979 p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG;
980 msg_Dbg(p_dec, "sending codec config data %p, %p, %i", p_header,
981 p_header->pBuffer, (int)p_header->nFilledLen);
982 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
985 /* Get back output port definition */
986 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
987 if(omx_error != OMX_ErrorNone) goto error;
989 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
990 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
992 if(p_sys->b_error) goto error;
994 p_dec->b_need_packetized = true;
998 CloseGeneric(p_this);
1002 /*****************************************************************************
1004 *****************************************************************************/
1005 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
1007 decoder_sys_t *p_sys = p_dec->p_sys;
1008 OMX_PARAM_PORTDEFINITIONTYPE definition;
1009 OMX_BUFFERHEADERTYPE *p_buffer;
1010 OMX_ERRORTYPE omx_error;
1013 /* Sanity checking */
1014 OMX_INIT_STRUCTURE(definition);
1015 definition.nPortIndex = p_port->i_port_index;
1016 omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1018 if(omx_error != OMX_ErrorNone ||
1019 !definition.format.video.nFrameWidth ||
1020 !definition.format.video.nFrameHeight )
1021 return OMX_ErrorUndefined;
1023 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
1024 p_port->i_port_index, NULL);
1025 CHECK_ERROR(omx_error, "OMX_CommandPortDisable on %i failed (%x)",
1026 (int)p_port->i_port_index, omx_error );
1028 for(i = 0; i < p_port->i_buffers; i++)
1030 OMX_FIFO_GET(&p_port->fifo, p_buffer);
1031 omx_error = OMX_FreeBuffer( p_sys->omx_handle,
1032 p_port->i_port_index, p_buffer );
1034 if(omx_error != OMX_ErrorNone) break;
1036 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
1037 omx_error, (int)p_port->i_port_index, i );
1039 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1040 CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
1042 /* Get the new port definition */
1043 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1044 if(omx_error != OMX_ErrorNone) goto error;
1045 omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1047 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
1048 omx_error, ErrorToString(omx_error));
1050 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
1051 p_port->i_port_index, NULL);
1052 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
1053 (int)p_port->i_port_index, omx_error );
1055 if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
1056 free(p_port->pp_buffers);
1057 p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
1058 if(!p_port->pp_buffers)
1060 omx_error = OMX_ErrorInsufficientResources;
1061 CHECK_ERROR(omx_error, "memory allocation failed");
1064 p_port->i_buffers = p_port->definition.nBufferCountActual;
1065 for(i = 0; i < p_port->i_buffers; i++)
1067 if(0 && p_port->b_direct)
1069 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1070 p_port->i_port_index, 0,
1071 p_port->definition.nBufferSize, (void*)1);
1074 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1075 p_port->i_port_index, 0,
1076 p_port->definition.nBufferSize);
1078 if(omx_error != OMX_ErrorNone) break;
1079 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1081 p_port->i_buffers = i;
1082 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1083 omx_error, (int)p_port->i_port_index, i );
1085 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1086 CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1088 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1089 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1095 /*****************************************************************************
1096 * DecodeVideo: Called to decode one frame
1097 *****************************************************************************/
1098 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1100 decoder_sys_t *p_sys = p_dec->p_sys;
1101 picture_t *p_pic = NULL, *p_next_pic;
1102 OMX_ERRORTYPE omx_error;
1105 OMX_BUFFERHEADERTYPE *p_header;
1108 if( !pp_block || !*pp_block )
1111 p_block = *pp_block;
1113 /* Check for errors from codec */
1116 msg_Dbg(p_dec, "error during decoding");
1117 block_Release( p_block );
1121 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1123 block_Release( p_block );
1124 if(!p_sys->in.b_flushed)
1126 msg_Dbg(p_dec, "flushing");
1127 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1128 p_sys->in.definition.nPortIndex, 0 );
1130 p_sys->in.b_flushed = true;
1134 /* Take care of decoded frames first */
1137 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1138 if(!p_header) break; /* No frame available */
1140 if(p_header->nFilledLen)
1142 p_pic = p_header->pAppPrivate;
1145 /* We're not in direct rendering mode.
1146 * Get a new picture and copy the content */
1147 p_pic = decoder_NewPicture( p_dec );
1148 if( !p_pic ) break; /* No picture available */
1150 CopyOmxPicture(p_dec, p_pic, p_header, p_sys->out.definition.format.video.nSliceHeight);
1153 p_pic->date = p_header->nTimeStamp;
1154 p_header->nFilledLen = 0;
1155 p_header->pAppPrivate = 0;
1158 /* Get a new picture */
1159 if(p_sys->in.b_direct && !p_header->pAppPrivate)
1161 p_next_pic = decoder_NewPicture( p_dec );
1162 if(!p_next_pic) break;
1164 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1165 p_header->pAppPrivate = p_next_pic;
1166 p_header->pInputPortPrivate = p_header->pBuffer;
1167 p_header->pBuffer = p_next_pic->p[0].p_pixels;
1171 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1174 #ifdef OMXIL_EXTRA_DEBUG
1175 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1177 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1180 /* Send the input buffer to the component */
1181 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1183 if (p_header && p_header->nFlags & OMX_BUFFERFLAG_EOS)
1188 p_header->nFilledLen = p_block->i_buffer;
1189 p_header->nOffset = 0;
1190 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1191 p_header->nTimeStamp = p_block->i_dts;
1193 /* In direct mode we pass the input pointer as is.
1194 * Otherwise we memcopy the data */
1195 if(p_sys->in.b_direct)
1197 p_header->pOutputPortPrivate = p_header->pBuffer;
1198 p_header->pBuffer = p_block->p_buffer;
1199 p_header->pAppPrivate = p_block;
1203 if(p_header->nFilledLen > p_header->nAllocLen)
1205 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1206 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1207 p_header->nFilledLen = p_header->nAllocLen;
1209 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1210 block_Release(p_block);
1213 /* Convert H.264 NAL format to annex b */
1214 if( p_sys->i_nal_size_length >= 3 && p_sys->i_nal_size_length <= 4 )
1216 /* This only works for NAL sizes 3-4 */
1217 int i_len = p_header->nFilledLen, i;
1218 uint8_t* ptr = p_header->pBuffer;
1219 while( i_len >= p_sys->i_nal_size_length )
1221 uint32_t nal_len = 0;
1222 for( i = 0; i < p_sys->i_nal_size_length; i++ ) {
1223 nal_len = (nal_len << 8) | ptr[i];
1226 ptr[p_sys->i_nal_size_length - 1] = 1;
1227 if( nal_len > INT_MAX || nal_len > (unsigned int) i_len )
1230 i_len -= nal_len + 4;
1233 #ifdef OMXIL_EXTRA_DEBUG
1234 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1235 (int)p_header->nFilledLen );
1237 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1238 p_sys->in.b_flushed = false;
1239 *pp_block = NULL; /* Avoid being fed the same packet again */
1243 /* Handle the PortSettingsChanged events */
1244 for(i = 0; i < p_sys->ports; i++)
1246 OmxPort *p_port = &p_sys->p_ports[i];
1247 if(!p_port->b_reconfigure) continue;
1248 p_port->b_reconfigure = 0;
1249 omx_error = PortReconfigure(p_dec, p_port);
1255 /*****************************************************************************
1256 * DecodeAudio: Called to decode one frame
1257 *****************************************************************************/
1258 aout_buffer_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1260 decoder_sys_t *p_sys = p_dec->p_sys;
1261 aout_buffer_t *p_buffer = 0;
1262 OMX_BUFFERHEADERTYPE *p_header;
1263 OMX_ERRORTYPE omx_error;
1267 if( !pp_block || !*pp_block ) return NULL;
1269 p_block = *pp_block;
1271 /* Check for errors from codec */
1274 msg_Dbg(p_dec, "error during decoding");
1275 block_Release( p_block );
1279 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1281 block_Release( p_block );
1282 date_Set( &p_sys->end_date, 0 );
1283 if(!p_sys->in.b_flushed)
1285 msg_Dbg(p_dec, "flushing");
1286 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1287 p_sys->in.definition.nPortIndex, 0 );
1289 p_sys->in.b_flushed = true;
1293 if( !date_Get( &p_sys->end_date ) )
1295 if( !p_block->i_pts )
1297 /* We've just started the stream, wait for the first PTS. */
1298 block_Release( p_block );
1301 date_Set( &p_sys->end_date, p_block->i_pts );
1304 /* Take care of decoded frames first */
1307 unsigned int i_samples;
1309 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1310 if(!p_header) break; /* No frame available */
1312 i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1315 p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1316 if( !p_buffer ) break; /* No audio buffer available */
1318 memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1319 p_header->nFilledLen = 0;
1321 if( p_header->nTimeStamp != 0 &&
1322 p_header->nTimeStamp != date_Get( &p_sys->end_date ) )
1323 date_Set( &p_sys->end_date, p_header->nTimeStamp );
1325 p_buffer->i_pts = date_Get( &p_sys->end_date );
1326 p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1330 #ifdef OMXIL_EXTRA_DEBUG
1331 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1333 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1334 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1338 /* Send the input buffer to the component */
1339 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1342 p_header->nFilledLen = p_block->i_buffer;
1343 p_header->nOffset = 0;
1344 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1345 p_header->nTimeStamp = p_block->i_dts;
1347 /* In direct mode we pass the input pointer as is.
1348 * Otherwise we memcopy the data */
1349 if(p_sys->in.b_direct)
1351 p_header->pOutputPortPrivate = p_header->pBuffer;
1352 p_header->pBuffer = p_block->p_buffer;
1353 p_header->pAppPrivate = p_block;
1357 if(p_header->nFilledLen > p_header->nAllocLen)
1359 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1360 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1361 p_header->nFilledLen = p_header->nAllocLen;
1363 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1364 block_Release(p_block);
1367 #ifdef OMXIL_EXTRA_DEBUG
1368 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1369 (int)p_header->nFilledLen );
1371 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1372 p_sys->in.b_flushed = false;
1373 *pp_block = NULL; /* Avoid being fed the same packet again */
1376 /* Handle the PortSettingsChanged events */
1377 for(i = 0; i < p_sys->ports; i++)
1379 OmxPort *p_port = &p_sys->p_ports[i];
1380 if(!p_port->b_reconfigure) continue;
1381 p_port->b_reconfigure = 0;
1382 omx_error = PortReconfigure(p_dec, p_port);
1388 /*****************************************************************************
1389 * EncodeVideo: Called to encode one frame
1390 *****************************************************************************/
1391 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1393 decoder_t *p_dec = ( decoder_t *)p_enc;
1394 decoder_sys_t *p_sys = p_dec->p_sys;
1395 OMX_ERRORTYPE omx_error;
1398 OMX_BUFFERHEADERTYPE *p_header;
1399 block_t *p_block = 0;
1401 if( !p_pic ) return NULL;
1403 /* Check for errors from codec */
1406 msg_Dbg(p_dec, "error during encoding");
1410 /* Send the input buffer to the component */
1411 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1414 /* In direct mode we pass the input pointer as is.
1415 * Otherwise we memcopy the data */
1416 if(p_sys->in.b_direct)
1418 p_header->pOutputPortPrivate = p_header->pBuffer;
1419 p_header->pBuffer = p_pic->p[0].p_pixels;
1423 CopyVlcPicture(p_dec, p_header, p_pic);
1426 p_header->nFilledLen = p_sys->in.i_frame_size;
1427 p_header->nOffset = 0;
1428 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1429 p_header->nTimeStamp = p_pic->date;
1430 #ifdef OMXIL_EXTRA_DEBUG
1431 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1432 (int)p_header->nFilledLen );
1434 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1435 p_sys->in.b_flushed = false;
1438 /* Handle the PortSettingsChanged events */
1439 for(i = 0; i < p_sys->ports; i++)
1441 OmxPort *p_port = &p_sys->p_ports[i];
1442 if(!p_port->b_reconfigure) continue;
1443 p_port->b_reconfigure = 0;
1444 omx_error = PortReconfigure(p_dec, p_port);
1447 /* Wait for the decoded frame */
1450 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1452 if(p_header->nFilledLen)
1454 if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1456 /* TODO: need to store codec config */
1457 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1460 p_block = p_header->pAppPrivate;
1463 /* We're not in direct rendering mode.
1464 * Get a new block and copy the content */
1465 p_block = block_New( p_dec, p_header->nFilledLen );
1466 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1469 p_block->i_buffer = p_header->nFilledLen;
1470 p_block->i_pts = p_block->i_dts = p_header->nTimeStamp;
1471 p_header->nFilledLen = 0;
1472 p_header->pAppPrivate = 0;
1475 #ifdef OMXIL_EXTRA_DEBUG
1476 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1478 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1481 msg_Dbg(p_dec, "done");
1485 /*****************************************************************************
1486 * CloseGeneric: omxil decoder destruction
1487 *****************************************************************************/
1488 static void CloseGeneric( vlc_object_t *p_this )
1490 decoder_t *p_dec = (decoder_t *)p_this;
1491 decoder_sys_t *p_sys = p_dec->p_sys;
1493 if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1494 vlc_mutex_lock( &omx_core_mutex );
1496 if( omx_refcount == 0 )
1498 if( p_sys->b_init ) pf_deinit();
1499 dll_close( dll_handle );
1501 vlc_mutex_unlock( &omx_core_mutex );
1503 vlc_mutex_destroy (&p_sys->mutex);
1504 vlc_cond_destroy (&p_sys->cond);
1505 vlc_mutex_destroy (&p_sys->in.fifo.lock);
1506 vlc_cond_destroy (&p_sys->in.fifo.wait);
1507 vlc_mutex_destroy (&p_sys->out.fifo.lock);
1508 vlc_cond_destroy (&p_sys->out.fifo.wait);
1513 /*****************************************************************************
1515 *****************************************************************************/
1516 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1517 OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1518 OMX_U32 data_2, OMX_PTR event_data )
1520 decoder_t *p_dec = (decoder_t *)app_data;
1521 decoder_sys_t *p_sys = p_dec->p_sys;
1527 case OMX_EventCmdComplete:
1528 switch ((OMX_STATETYPE)data_1)
1530 case OMX_CommandStateSet:
1531 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %s)", EventToString(event),
1532 CommandToString(data_1), StateToString(data_2) );
1536 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u)", EventToString(event),
1537 CommandToString(data_1), (unsigned int)data_2 );
1542 case OMX_EventError:
1543 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u, %s)", EventToString(event),
1544 ErrorToString((OMX_ERRORTYPE)data_1), (unsigned int)data_2,
1545 (const char *)event_data);
1546 //p_sys->b_error = true;
1549 case OMX_EventPortSettingsChanged:
1550 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1551 (unsigned int)data_1, (unsigned int)data_2 );
1552 for(i = 0; i < p_sys->ports; i++)
1553 if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1554 p_sys->p_ports[i].b_reconfigure = true;
1555 memset(&p_sys->sentinel_buffer, 0, sizeof(p_sys->sentinel_buffer));
1556 p_sys->sentinel_buffer.nFlags = OMX_BUFFERFLAG_EOS;
1557 OMX_FIFO_PUT(&p_sys->in.fifo, &p_sys->sentinel_buffer);
1561 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1562 (unsigned int)data_1, (unsigned int)data_2 );
1566 PostOmxEvent(p_dec, event, data_1, data_2, event_data);
1567 return OMX_ErrorNone;
1570 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1571 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1573 decoder_t *p_dec = (decoder_t *)app_data;
1574 decoder_sys_t *p_sys = p_dec->p_sys;
1577 #ifdef OMXIL_EXTRA_DEBUG
1578 msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1581 if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1583 block_t *p_block = (block_t *)omx_header->pAppPrivate;
1584 omx_header->pBuffer = omx_header->pOutputPortPrivate;
1585 if(p_block) block_Release(p_block);
1586 omx_header->pAppPrivate = 0;
1588 OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1590 return OMX_ErrorNone;
1593 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1594 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1596 decoder_t *p_dec = (decoder_t *)app_data;
1597 decoder_sys_t *p_sys = p_dec->p_sys;
1600 #ifdef OMXIL_EXTRA_DEBUG
1601 msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
1602 (int)omx_header->nFilledLen );
1605 if(omx_header->pInputPortPrivate)
1607 omx_header->pBuffer = omx_header->pInputPortPrivate;
1609 OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1611 return OMX_ErrorNone;