1 /*****************************************************************************
2 * omxil.c: Video decoder module making use of OpenMAX IL components.
3 *****************************************************************************
4 * Copyright (C) 2010 VLC authors and VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * 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, "I" 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>
47 #include <vlc_block_helper.h>
49 #include "../h264_nal.h"
54 # define OMXIL_EXTRA_DEBUG
57 #define SENTINEL_FLAG 0x10000
59 /*****************************************************************************
60 * List of OpenMAX IL core we will try in order
61 *****************************************************************************/
62 static const char *ppsz_dll_list[] =
65 "libiomx.so", /* Not used when using IOMX, the lib should already be loaded */
67 "libOMX_Core.so", /* TI OMAP IL core */
68 "libOmxCore.so", /* Qualcomm IL core */
69 "libomxil-bellagio.so", /* Bellagio IL core */
74 /*****************************************************************************
75 * Global OMX Core instance, shared between module instances
76 *****************************************************************************/
77 static vlc_mutex_t omx_core_mutex = VLC_STATIC_MUTEX;
78 static unsigned int omx_refcount = 0;
79 static void *dll_handle;
80 static OMX_ERRORTYPE (*pf_init) (void);
81 static OMX_ERRORTYPE (*pf_deinit) (void);
82 static OMX_ERRORTYPE (*pf_get_handle) (OMX_HANDLETYPE *, OMX_STRING,
83 OMX_PTR, OMX_CALLBACKTYPE *);
84 static OMX_ERRORTYPE (*pf_free_handle) (OMX_HANDLETYPE);
85 static OMX_ERRORTYPE (*pf_component_enum)(OMX_STRING, OMX_U32, OMX_U32);
86 static OMX_ERRORTYPE (*pf_get_roles_of_component)(OMX_STRING, OMX_U32 *, OMX_U8 **);
88 /*****************************************************************************
90 *****************************************************************************/
91 static int OpenDecoder( vlc_object_t * );
92 static int OpenEncoder( vlc_object_t * );
93 static int OpenGeneric( vlc_object_t *, bool b_encode );
94 static void CloseGeneric( vlc_object_t * );
96 static picture_t *DecodeVideo( decoder_t *, block_t ** );
97 static block_t *DecodeAudio ( decoder_t *, block_t ** );
98 static block_t *EncodeVideo( encoder_t *, picture_t * );
100 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE, OMX_PTR, OMX_EVENTTYPE,
101 OMX_U32, OMX_U32, OMX_PTR );
102 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE, OMX_PTR,
103 OMX_BUFFERHEADERTYPE * );
104 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE, OMX_PTR,
105 OMX_BUFFERHEADERTYPE * );
107 /*****************************************************************************
109 *****************************************************************************/
111 set_description( N_("Audio/Video decoder (using OpenMAX IL)") )
112 set_category( CAT_INPUT )
113 set_subcategory( SUBCAT_INPUT_VCODEC )
114 set_section( N_("Decoding") , NULL )
115 #if defined(USE_IOMX)
116 /* For IOMX, don't enable it automatically via priorities,
117 * enable it only via the --codec iomx command line parameter when
119 set_capability( "decoder", 0 )
121 set_capability( "decoder", 80 )
123 set_callbacks( OpenDecoder, CloseGeneric )
126 set_section( N_("Encoding") , NULL )
127 set_description( N_("Video encoder (using OpenMAX IL)") )
128 set_capability( "encoder", 0 )
129 set_callbacks( OpenEncoder, CloseGeneric )
132 /*****************************************************************************
133 * CreateComponentsList: creates a list of components matching the given role
134 *****************************************************************************/
135 static int CreateComponentsList(decoder_t *p_dec, const char *psz_role)
137 decoder_sys_t *p_sys = p_dec->p_sys;
138 char psz_name[OMX_MAX_STRINGNAME_SIZE];
139 OMX_ERRORTYPE omx_error;
141 OMX_U8 **ppsz_roles = 0;
142 unsigned int i, j, len;
144 if(!psz_role) goto end;
145 len = strlen(psz_role);
149 bool b_found = false;
151 omx_error = pf_component_enum(psz_name, OMX_MAX_STRINGNAME_SIZE, i);
152 if(omx_error != OMX_ErrorNone) break;
154 msg_Dbg(p_dec, "component %s", psz_name);
156 omx_error = pf_get_roles_of_component(psz_name, &roles, 0);
157 if(omx_error != OMX_ErrorNone || !roles) continue;
159 ppsz_roles = malloc(roles * (sizeof(OMX_U8*) + OMX_MAX_STRINGNAME_SIZE));
160 if(!ppsz_roles) continue;
162 for( j = 0; j < roles; j++ )
163 ppsz_roles[j] = ((OMX_U8 *)(&ppsz_roles[roles])) +
164 j * OMX_MAX_STRINGNAME_SIZE;
166 omx_error = pf_get_roles_of_component(psz_name, &roles, ppsz_roles);
167 if(omx_error != OMX_ErrorNone) roles = 0;
169 for(j = 0; j < roles; j++)
171 msg_Dbg(p_dec, " - role: %s", ppsz_roles[j]);
172 if(!strcmp((char *)ppsz_roles[j], psz_role)) b_found = true;
177 if(!b_found) continue;
179 if(p_sys->components >= MAX_COMPONENTS_LIST_SIZE)
181 msg_Dbg(p_dec, "too many matching components");
185 strncpy(p_sys->ppsz_components[p_sys->components], psz_name,
186 OMX_MAX_STRINGNAME_SIZE-1);
191 msg_Dbg(p_dec, "found %i matching components for role %s",
192 p_sys->components, psz_role);
193 for( i = 0; i < p_sys->components; i++ )
194 msg_Dbg(p_dec, "- %s", p_sys->ppsz_components[i]);
196 return p_sys->components;
199 /*****************************************************************************
200 * ImplementationSpecificWorkarounds: place-holder for implementation
201 * specific workarounds
202 *****************************************************************************/
203 static OMX_ERRORTYPE ImplementationSpecificWorkarounds(decoder_t *p_dec,
204 OmxPort *p_port, es_format_t *p_fmt)
206 decoder_sys_t *p_sys = p_dec->p_sys;
207 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
208 int i_profile = 0xFFFF, i_level = 0xFFFF;
210 /* Try to find out the profile of the video */
211 while(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
212 p_fmt->i_codec == VLC_CODEC_H264)
214 uint8_t *p = (uint8_t*)p_dec->fmt_in.p_extra;
215 if(!p || !p_dec->fmt_in.p_extra) break;
217 /* Check the profile / level */
218 if(p_dec->fmt_in.i_original_fourcc == VLC_FOURCC('a','v','c','1') &&
221 if(p_dec->fmt_in.i_extra < 12) break;
222 p_sys->i_nal_size_length = 1 + (p[4]&0x03);
223 if( !(p[5]&0x1f) ) break;
228 if(p_dec->fmt_in.i_extra < 8) break;
229 if(!p[0] && !p[1] && !p[2] && p[3] == 1) p += 4;
230 else if(!p[0] && !p[1] && p[2] == 1) p += 3;
234 if( ((*p++)&0x1f) != 7) break;
236 /* Get profile/level out of first SPS */
242 if(!strcmp(p_sys->psz_component, "OMX.TI.Video.Decoder"))
244 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
245 p_fmt->i_codec == VLC_CODEC_H264 &&
246 (i_profile != 66 || i_level > 30))
248 msg_Dbg(p_dec, "h264 profile/level not supported (0x%x, 0x%x)",
250 return OMX_ErrorNotImplemented;
253 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirOutput &&
254 p_fmt->i_codec == VLC_CODEC_I420)
256 /* I420 xvideo is slow on OMAP */
257 def->format.video.eColorFormat = OMX_COLOR_FormatCbYCrY;
258 GetVlcChromaFormat( def->format.video.eColorFormat,
259 &p_fmt->i_codec, 0 );
260 GetVlcChromaSizes( p_fmt->i_codec,
261 def->format.video.nFrameWidth,
262 def->format.video.nFrameHeight,
263 &p_port->i_frame_size, &p_port->i_frame_stride,
264 &p_port->i_frame_stride_chroma_div );
265 def->format.video.nStride = p_port->i_frame_stride;
266 def->nBufferSize = p_port->i_frame_size;
269 else if(!strcmp(p_sys->psz_component, "OMX.st.video_encoder"))
271 if(p_fmt->i_cat == VIDEO_ES)
273 /* Bellagio's encoder doesn't encode the framerate in Q16 */
274 def->format.video.xFramerate >>= 16;
277 #if 0 /* FIXME: doesn't apply for HP Touchpad */
278 else if (!strncmp(p_sys->psz_component, "OMX.qcom.video.decoder.",
279 strlen("OMX.qcom.video.decoder")))
281 /* qdsp6 refuses buffer size larger than 450K on input port */
282 if (def->nBufferSize > 450 * 1024)
284 def->nBufferSize = 450 * 1024;
285 p_port->i_frame_size = def->nBufferSize;
290 return OMX_ErrorNone;
293 /*****************************************************************************
294 * SetPortDefinition: set definition of the omx port based on the vlc format
295 *****************************************************************************/
296 static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
299 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
300 OMX_ERRORTYPE omx_error;
302 omx_error = OMX_GetParameter(p_port->omx_handle,
303 OMX_IndexParamPortDefinition, def);
304 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
305 omx_error, ErrorToString(omx_error));
310 def->format.video.nFrameWidth = p_fmt->video.i_width;
311 def->format.video.nFrameHeight = p_fmt->video.i_height;
312 if(def->format.video.eCompressionFormat == OMX_VIDEO_CodingUnused)
313 def->format.video.nStride = def->format.video.nFrameWidth;
314 if( p_fmt->video.i_frame_rate > 0 &&
315 p_fmt->video.i_frame_rate_base > 0 )
316 def->format.video.xFramerate = (p_fmt->video.i_frame_rate << 16) /
317 p_fmt->video.i_frame_rate_base;
319 if(def->eDir == OMX_DirInput || p_dec->p_sys->b_enc)
321 if (def->eDir == OMX_DirInput && p_dec->p_sys->b_enc)
322 def->nBufferSize = def->format.video.nFrameWidth *
323 def->format.video.nFrameHeight * 2;
324 p_port->i_frame_size = def->nBufferSize;
326 if(!GetOmxVideoFormat(p_fmt->i_codec,
327 &def->format.video.eCompressionFormat, 0) )
329 if(!GetOmxChromaFormat(p_fmt->i_codec,
330 &def->format.video.eColorFormat, 0) )
332 omx_error = OMX_ErrorNotImplemented;
333 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
334 (char *)&p_fmt->i_codec );
336 GetVlcChromaSizes( p_fmt->i_codec,
337 def->format.video.nFrameWidth,
338 def->format.video.nFrameHeight,
339 &p_port->i_frame_size, &p_port->i_frame_stride,
340 &p_port->i_frame_stride_chroma_div );
341 def->format.video.nStride = p_port->i_frame_stride;
342 def->nBufferSize = p_port->i_frame_size;
347 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
348 &p_fmt->i_codec, 0 ) )
350 omx_error = OMX_ErrorNotImplemented;
351 CHECK_ERROR(omx_error, "OMX color format %i not supported",
352 (int)def->format.video.eColorFormat );
354 GetVlcChromaSizes( p_fmt->i_codec,
355 def->format.video.nFrameWidth,
356 def->format.video.nFrameHeight,
357 &p_port->i_frame_size, &p_port->i_frame_stride,
358 &p_port->i_frame_stride_chroma_div );
359 def->format.video.nStride = p_port->i_frame_stride;
360 if (p_port->i_frame_size > def->nBufferSize)
361 def->nBufferSize = p_port->i_frame_size;
366 p_port->i_frame_size = def->nBufferSize;
367 if(def->eDir == OMX_DirInput)
369 if(!GetOmxAudioFormat(p_fmt->i_codec,
370 &def->format.audio.eEncoding, 0) )
372 omx_error = OMX_ErrorNotImplemented;
373 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
374 (char *)&p_fmt->i_codec );
379 if( !OmxToVlcAudioFormat(def->format.audio.eEncoding,
380 &p_fmt->i_codec, 0 ) )
382 omx_error = OMX_ErrorNotImplemented;
383 CHECK_ERROR(omx_error, "OMX audio encoding %i not supported",
384 (int)def->format.audio.eEncoding );
389 default: return OMX_ErrorNotImplemented;
392 omx_error = ImplementationSpecificWorkarounds(p_dec, p_port, p_fmt);
393 CHECK_ERROR(omx_error, "ImplementationSpecificWorkarounds failed (%x : %s)",
394 omx_error, ErrorToString(omx_error));
396 omx_error = OMX_SetParameter(p_port->omx_handle,
397 OMX_IndexParamPortDefinition, def);
398 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
399 omx_error, ErrorToString(omx_error));
401 omx_error = OMX_GetParameter(p_port->omx_handle,
402 OMX_IndexParamPortDefinition, def);
403 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
404 omx_error, ErrorToString(omx_error));
406 if(p_port->i_frame_size > def->nBufferSize)
407 def->nBufferSize = p_port->i_frame_size;
408 p_port->i_frame_size = def->nBufferSize;
410 /* Deal with audio params */
411 if(p_fmt->i_cat == AUDIO_ES)
413 omx_error = SetAudioParameters(p_port->omx_handle,
414 &p_port->format_param, def->nPortIndex,
415 def->format.audio.eEncoding,
417 p_fmt->audio.i_channels,
420 p_fmt->audio.i_bitspersample,
421 p_fmt->audio.i_blockalign);
422 if (def->eDir == OMX_DirInput) {
423 CHECK_ERROR(omx_error, "SetAudioParameters failed (%x : %s)",
424 omx_error, ErrorToString(omx_error));
425 } else if (omx_error != OMX_ErrorNone) {
426 msg_Warn(p_dec, "SetAudioParameters failed (%x : %s) on output port",
427 omx_error, ErrorToString(omx_error));
428 omx_error = OMX_ErrorNone;
431 if (!strcmp(p_dec->p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER") &&
432 def->eDir == OMX_DirOutput)
434 /* When setting the output buffer size above, the decoder actually
435 * sets the buffer size to a lower value than what was chosen. If
436 * we try to allocate buffers of this size, it fails. Thus, forcibly
437 * use a larger buffer size. */
438 def->nBufferSize *= 2;
445 /*****************************************************************************
446 * GetPortDefinition: set vlc format based on the definition of the omx port
447 *****************************************************************************/
448 static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
451 decoder_sys_t *p_sys = p_dec->p_sys;
452 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
453 OMX_ERRORTYPE omx_error;
454 OMX_CONFIG_RECTTYPE crop_rect;
456 omx_error = OMX_GetParameter(p_port->omx_handle,
457 OMX_IndexParamPortDefinition, def);
458 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
459 omx_error, ErrorToString(omx_error));
464 p_fmt->video.i_width = def->format.video.nFrameWidth;
465 p_fmt->video.i_visible_width = def->format.video.nFrameWidth;
466 p_fmt->video.i_height = def->format.video.nFrameHeight;
467 p_fmt->video.i_visible_height = def->format.video.nFrameHeight;
468 p_fmt->video.i_frame_rate = p_dec->fmt_in.video.i_frame_rate;
469 p_fmt->video.i_frame_rate_base = p_dec->fmt_in.video.i_frame_rate_base;
471 OMX_INIT_STRUCTURE(crop_rect);
472 crop_rect.nPortIndex = def->nPortIndex;
473 omx_error = OMX_GetConfig(p_port->omx_handle, OMX_IndexConfigCommonOutputCrop, &crop_rect);
474 if (omx_error == OMX_ErrorNone)
476 if (!def->format.video.nSliceHeight)
477 def->format.video.nSliceHeight = def->format.video.nFrameHeight;
478 if (!def->format.video.nStride)
479 def->format.video.nStride = def->format.video.nFrameWidth;
480 p_fmt->video.i_width = crop_rect.nWidth;
481 p_fmt->video.i_visible_width = crop_rect.nWidth;
482 p_fmt->video.i_height = crop_rect.nHeight;
483 p_fmt->video.i_visible_height = crop_rect.nHeight;
484 if (def->format.video.eColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
485 def->format.video.nSliceHeight -= crop_rect.nTop/2;
489 /* Don't pass the error back to the caller, this isn't mandatory */
490 omx_error = OMX_ErrorNone;
493 /* Hack: Nexus One (stock firmware with binary OMX driver blob)
494 * claims to output 420Planar even though it in in practice is
496 if(def->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar &&
497 !strncmp(p_sys->psz_component, "OMX.qcom.video.decoder",
498 strlen("OMX.qcom.video.decoder")))
499 def->format.video.eColorFormat = OMX_QCOM_COLOR_FormatYVU420SemiPlanar;
501 if (IgnoreOmxDecoderPadding(p_sys->psz_component)) {
502 def->format.video.nSliceHeight = 0;
503 def->format.video.nStride = p_fmt->video.i_width;
506 if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
507 &p_fmt->i_codec, 0 ) )
509 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
510 &p_fmt->i_codec, 0 ) )
512 omx_error = OMX_ErrorNotImplemented;
513 CHECK_ERROR(omx_error, "OMX color format %i not supported",
514 (int)def->format.video.eColorFormat );
516 GetVlcChromaSizes( p_fmt->i_codec,
517 def->format.video.nFrameWidth,
518 def->format.video.nFrameHeight,
519 &p_port->i_frame_size, &p_port->i_frame_stride,
520 &p_port->i_frame_stride_chroma_div );
522 if(p_port->i_frame_size > def->nBufferSize)
523 def->nBufferSize = p_port->i_frame_size;
524 p_port->i_frame_size = def->nBufferSize;
526 if((int)p_port->i_frame_stride > def->format.video.nStride)
527 def->format.video.nStride = p_port->i_frame_stride;
529 p_port->i_frame_stride = def->format.video.nStride;
533 if( !OmxToVlcAudioFormat( def->format.audio.eEncoding,
534 &p_fmt->i_codec, 0 ) )
536 omx_error = OMX_ErrorNotImplemented;
537 CHECK_ERROR(omx_error, "OMX audio format %i not supported",
538 (int)def->format.audio.eEncoding );
541 omx_error = GetAudioParameters(p_port->omx_handle,
542 &p_port->format_param, def->nPortIndex,
543 def->format.audio.eEncoding,
544 &p_fmt->audio.i_channels,
545 &p_fmt->audio.i_rate,
547 &p_fmt->audio.i_bitspersample,
548 &p_fmt->audio.i_blockalign);
549 CHECK_ERROR(omx_error, "GetAudioParameters failed (%x : %s)",
550 omx_error, ErrorToString(omx_error));
552 if(p_fmt->audio.i_channels < 9)
554 static const int pi_channels_maps[9] =
556 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
557 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
558 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
559 | AOUT_CHAN_REARRIGHT,
560 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
561 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
562 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
563 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
564 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
565 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
566 | AOUT_CHAN_MIDDLERIGHT,
567 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
568 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
571 p_fmt->audio.i_physical_channels =
572 p_fmt->audio.i_original_channels =
573 pi_channels_maps[p_fmt->audio.i_channels];
576 date_Init( &p_dec->p_sys->end_date, p_fmt->audio.i_rate, 1 );
580 default: return OMX_ErrorNotImplemented;
587 /*****************************************************************************
588 * DeinitialiseComponent: Deinitialise and unload an OMX component
589 *****************************************************************************/
590 static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
591 OMX_HANDLETYPE omx_handle)
593 decoder_sys_t *p_sys = p_dec->p_sys;
594 OMX_ERRORTYPE omx_error;
598 if(!omx_handle) return OMX_ErrorNone;
600 omx_error = OMX_GetState(omx_handle, &state);
601 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
603 if(state == OMX_StateExecuting)
605 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
607 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
610 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, &cmd, &state, 0);
611 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
612 // The event queue can contain other OMX_EventCmdComplete items,
613 // such as for OMX_CommandFlush
614 if (cmd == OMX_CommandStateSet && state == OMX_StateIdle)
619 omx_error = OMX_GetState(omx_handle, &state);
620 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
622 if(state == OMX_StateIdle)
624 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
625 OMX_StateLoaded, 0 );
626 CHECK_ERROR(omx_error, "OMX_CommandStateSet Loaded failed (%x)", omx_error );
628 for(i = 0; i < p_sys->ports; i++)
630 OmxPort *p_port = &p_sys->p_ports[i];
631 OMX_BUFFERHEADERTYPE *p_buffer;
633 for(j = 0; j < p_port->i_buffers; j++)
635 OMX_FIFO_GET(&p_port->fifo, p_buffer);
636 if (p_buffer->nFlags & SENTINEL_FLAG) {
641 omx_error = OMX_FreeBuffer( omx_handle,
642 p_port->i_port_index, p_buffer );
644 if(omx_error != OMX_ErrorNone) break;
646 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
647 omx_error, (int)p_port->i_port_index, j );
649 OMX_FIFO_PEEK(&p_port->fifo, p_buffer);
650 if (!p_buffer) break;
652 OMX_FIFO_GET(&p_port->fifo, p_buffer);
653 if (p_buffer->nFlags & SENTINEL_FLAG) {
657 msg_Warn( p_dec, "Stray buffer left in fifo, %p", p_buffer );
661 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
662 CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
666 for(i = 0; i < p_sys->ports; i++)
668 OmxPort *p_port = &p_sys->p_ports[i];
669 free(p_port->pp_buffers);
670 p_port->pp_buffers = 0;
672 omx_error = pf_free_handle( omx_handle );
676 /*****************************************************************************
677 * InitialiseComponent: Load and initialise an OMX component
678 *****************************************************************************/
679 static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
680 OMX_STRING psz_component, OMX_HANDLETYPE *p_handle)
682 static OMX_CALLBACKTYPE callbacks =
683 { OmxEventHandler, OmxEmptyBufferDone, OmxFillBufferDone };
684 decoder_sys_t *p_sys = p_dec->p_sys;
685 OMX_HANDLETYPE omx_handle;
686 OMX_ERRORTYPE omx_error;
688 OMX_U8 psz_role[OMX_MAX_STRINGNAME_SIZE];
689 OMX_PARAM_COMPONENTROLETYPE role;
690 OMX_PARAM_PORTDEFINITIONTYPE definition;
691 OMX_PORT_PARAM_TYPE param;
694 omx_error = pf_get_handle( &omx_handle, psz_component, p_dec, &callbacks );
695 if(omx_error != OMX_ErrorNone)
697 msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
698 omx_error, ErrorToString(omx_error) );
701 strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
703 omx_error = OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
704 if(omx_error == OMX_ErrorNone)
705 msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
707 msg_Dbg(p_dec, "loaded component %s", psz_component);
708 PrintOmx(p_dec, omx_handle, OMX_ALL);
710 /* Set component role */
711 OMX_INIT_STRUCTURE(role);
712 strcpy((char*)role.cRole,
713 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec : p_dec->fmt_in.i_codec,
714 p_dec->fmt_in.i_cat, p_sys->b_enc));
716 omx_error = OMX_SetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
718 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
720 if(omx_error == OMX_ErrorNone)
721 msg_Dbg(p_dec, "component standard role set to %s", role.cRole);
723 /* Find the input / output ports */
724 OMX_INIT_STRUCTURE(param);
725 OMX_INIT_STRUCTURE(definition);
726 omx_error = OMX_GetParameter(omx_handle, p_dec->fmt_in.i_cat == VIDEO_ES ?
727 OMX_IndexParamVideoInit : OMX_IndexParamAudioInit, ¶m);
728 if(omx_error != OMX_ErrorNone) {
731 param.nStartPortNumber = 0;
737 for(i = 0; i < param.nPorts; i++)
741 /* Get port definition */
742 definition.nPortIndex = param.nStartPortNumber + i;
743 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
745 if(omx_error != OMX_ErrorNone) continue;
747 if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
748 else p_port = &p_sys->out;
750 p_port->b_valid = true;
751 p_port->i_port_index = definition.nPortIndex;
752 p_port->definition = definition;
753 p_port->omx_handle = omx_handle;
756 if(!p_sys->in.b_valid || !p_sys->out.b_valid)
758 omx_error = OMX_ErrorInvalidComponent;
759 CHECK_ERROR(omx_error, "couldn't find an input and output port");
762 if(!strncmp(p_sys->psz_component, "OMX.SEC.", 8) &&
763 p_dec->fmt_in.i_cat == VIDEO_ES)
766 omx_error = OMX_GetExtensionIndex(omx_handle, (OMX_STRING) "OMX.SEC.index.ThumbnailMode", &index);
767 if(omx_error == OMX_ErrorNone)
769 OMX_BOOL enable = OMX_TRUE;
770 omx_error = OMX_SetConfig(omx_handle, index, &enable);
771 CHECK_ERROR(omx_error, "Unable to set ThumbnailMode");
773 OMX_BOOL enable = OMX_TRUE;
774 /* Needed on Samsung Galaxy S II */
775 omx_error = OMX_SetConfig(omx_handle, OMX_IndexVendorSetYUV420pMode, &enable);
776 if (omx_error == OMX_ErrorNone)
777 msg_Dbg(p_dec, "Set OMX_IndexVendorSetYUV420pMode successfully");
779 msg_Dbg(p_dec, "Unable to set OMX_IndexVendorSetYUV420pMode: %x", omx_error);
783 /* Set port definitions */
784 for(i = 0; i < p_sys->ports; i++)
786 omx_error = SetPortDefinition(p_dec, &p_sys->p_ports[i],
787 p_sys->p_ports[i].p_fmt);
788 if(omx_error != OMX_ErrorNone) goto error;
791 /* Allocate our array for the omx buffers and enable ports */
792 for(i = 0; i < p_sys->ports; i++)
794 OmxPort *p_port = &p_sys->p_ports[i];
797 malloc(p_port->definition.nBufferCountActual *
798 sizeof(OMX_BUFFERHEADERTYPE*));
799 if(!p_port->pp_buffers)
801 omx_error = OMX_ErrorInsufficientResources;
802 CHECK_ERROR(omx_error, "memory allocation failed");
804 p_port->i_buffers = p_port->definition.nBufferCountActual;
807 if(!p_port->definition.bEnabled)
809 omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
810 p_port->i_port_index, NULL);
811 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
812 (int)p_port->i_port_index, omx_error );
813 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
814 CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
815 (int)p_port->i_port_index, omx_error );
819 *p_handle = omx_handle;
820 return OMX_ErrorNone;
823 DeinitialiseComponent(p_dec, omx_handle);
828 /*****************************************************************************
829 * OpenDecoder: Create the decoder instance
830 *****************************************************************************/
831 static int OpenDecoder( vlc_object_t *p_this )
833 decoder_t *p_dec = (decoder_t*)p_this;
836 if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
839 status = OpenGeneric( p_this, false );
840 if(status != VLC_SUCCESS) return status;
842 p_dec->pf_decode_video = DecodeVideo;
843 p_dec->pf_decode_audio = DecodeAudio;
848 /*****************************************************************************
849 * OpenEncoder: Create the encoder instance
850 *****************************************************************************/
851 static int OpenEncoder( vlc_object_t *p_this )
853 encoder_t *p_enc = (encoder_t*)p_this;
856 if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
859 status = OpenGeneric( p_this, true );
860 if(status != VLC_SUCCESS) return status;
862 p_enc->pf_encode_video = EncodeVideo;
867 /*****************************************************************************
868 * OpenGeneric: Create the generic decoder/encoder instance
869 *****************************************************************************/
870 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
872 decoder_t *p_dec = (decoder_t*)p_this;
873 decoder_sys_t *p_sys;
874 OMX_ERRORTYPE omx_error;
875 OMX_BUFFERHEADERTYPE *p_header;
878 vlc_mutex_lock( &omx_core_mutex );
879 if( omx_refcount > 0 )
882 /* Load the OMX core */
883 for( i = 0; ppsz_dll_list[i]; i++ )
885 dll_handle = dll_open( ppsz_dll_list[i] );
886 if( dll_handle ) break;
890 vlc_mutex_unlock( &omx_core_mutex );
894 pf_init = dlsym( dll_handle, "OMX_Init" );
895 pf_deinit = dlsym( dll_handle, "OMX_Deinit" );
896 pf_get_handle = dlsym( dll_handle, "OMX_GetHandle" );
897 pf_free_handle = dlsym( dll_handle, "OMX_FreeHandle" );
898 pf_component_enum = dlsym( dll_handle, "OMX_ComponentNameEnum" );
899 pf_get_roles_of_component = dlsym( dll_handle, "OMX_GetRolesOfComponent" );
900 if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle ||
901 !pf_component_enum || !pf_get_roles_of_component )
903 msg_Warn( p_this, "cannot find OMX_* symbols in `%s' (%s)",
904 ppsz_dll_list[i], dlerror() );
905 dll_close(dll_handle);
906 vlc_mutex_unlock( &omx_core_mutex );
911 /* Allocate the memory needed to store the decoder's structure */
912 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
914 if( omx_refcount == 0 )
915 dll_close(dll_handle);
916 vlc_mutex_unlock( &omx_core_mutex );
920 /* Initialise the thread properties */
923 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
924 p_dec->fmt_out.video = p_dec->fmt_in.video;
925 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
926 p_dec->fmt_out.i_codec = 0;
928 p_sys->b_enc = b_encode;
929 p_sys->pp_last_event = &p_sys->p_events;
930 vlc_mutex_init (&p_sys->mutex);
931 vlc_cond_init (&p_sys->cond);
932 vlc_mutex_init (&p_sys->lock);
933 vlc_mutex_init (&p_sys->in.fifo.lock);
934 vlc_cond_init (&p_sys->in.fifo.wait);
935 p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
936 p_sys->in.fifo.pp_last = &p_sys->in.fifo.p_first;
937 p_sys->in.b_direct = false;
938 p_sys->in.b_flushed = true;
939 p_sys->in.p_fmt = &p_dec->fmt_in;
940 vlc_mutex_init (&p_sys->out.fifo.lock);
941 vlc_cond_init (&p_sys->out.fifo.wait);
942 p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
943 p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
944 p_sys->out.b_direct = true;
945 p_sys->out.b_flushed = true;
946 p_sys->out.p_fmt = &p_dec->fmt_out;
948 p_sys->p_ports = &p_sys->in;
949 p_sys->b_use_pts = 0;
951 msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
952 (char *)&p_dec->fmt_out.i_codec);
954 /* Initialise the OMX core */
955 omx_error = omx_refcount > 0 ? OMX_ErrorNone : pf_init();
957 if(omx_error != OMX_ErrorNone)
959 msg_Warn( p_this, "OMX_Init failed (%x: %s)", omx_error,
960 ErrorToString(omx_error) );
961 vlc_mutex_unlock( &omx_core_mutex );
962 CloseGeneric(p_this);
965 p_sys->b_init = true;
966 vlc_mutex_unlock( &omx_core_mutex );
968 /* Enumerate components and build a list of the one we want to try */
969 if( !CreateComponentsList(p_dec,
970 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
971 p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
974 msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
975 (char *)&p_dec->fmt_in.i_codec );
976 CloseGeneric(p_this);
980 /* Try to load and initialise a component */
981 omx_error = OMX_ErrorUndefined;
982 for(i = 0; i < p_sys->components; i++)
985 /* ignore OpenCore software codecs */
986 if (!strncmp(p_sys->ppsz_components[i], "OMX.PV.", 7))
988 /* The same sw codecs, renamed in ICS (perhaps also in honeycomb) */
989 if (!strncmp(p_sys->ppsz_components[i], "OMX.google.", 11))
991 /* This one has been seen on HTC One V - it behaves like it works,
992 * but FillBufferDone returns buffers filled with 0 bytes. The One V
993 * has got a working OMX.qcom.video.decoder.avc instead though. */
994 if (!strncmp(p_sys->ppsz_components[i], "OMX.ARICENT.", 12))
996 /* Some nVidia codec with DRM */
997 if (!strncmp(p_sys->ppsz_components[i], "OMX.Nvidia.h264.decode.secure", 29))
999 /* Use VC1 decoder for WMV3 for now */
1000 if (!strcmp(p_sys->ppsz_components[i], "OMX.SEC.WMV.Decoder"))
1002 /* This decoder does work, but has an insane latency (leading to errors
1003 * about "main audio output playback way too late" and dropped frames).
1004 * At least Samsung Galaxy S III (where this decoder is present) has
1005 * got another one, OMX.SEC.mp3.dec, that works well and has a
1006 * sensible latency. (Also, even if that one isn't found, in general,
1007 * using SW codecs is usually more than fast enough for MP3.) */
1008 if (!strcmp(p_sys->ppsz_components[i], "OMX.SEC.MP3.Decoder"))
1011 omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
1012 &p_sys->omx_handle);
1013 if(omx_error == OMX_ErrorNone) break;
1015 CHECK_ERROR(omx_error, "no component could be initialised" );
1017 /* Move component to Idle then Executing state */
1018 OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
1019 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
1021 /* Allocate omx buffers */
1022 for(i = 0; i < p_sys->ports; i++)
1024 OmxPort *p_port = &p_sys->p_ports[i];
1026 for(j = 0; j < p_port->i_buffers; j++)
1029 #define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
1030 char *p_buf = malloc(p_port->definition.nBufferSize +
1031 p_port->definition.nBufferAlignment);
1032 p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
1035 if(0 && p_port->b_direct)
1037 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
1038 p_port->i_port_index, 0,
1039 p_port->definition.nBufferSize, (void*)1);
1042 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
1043 p_port->i_port_index, 0,
1044 p_port->definition.nBufferSize);
1046 if(omx_error != OMX_ErrorNone) break;
1047 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
1049 p_port->i_buffers = j;
1050 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1051 omx_error, (int)p_port->i_port_index, j );
1054 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1055 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
1057 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
1058 OMX_StateExecuting, 0);
1059 CHECK_ERROR(omx_error, "OMX_CommandStateSet Executing failed (%x)", omx_error );
1060 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1061 CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
1063 /* Send codec configuration data */
1064 if( p_dec->fmt_in.i_extra )
1066 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1067 p_header->nFilledLen = p_dec->fmt_in.i_extra;
1069 /* Convert H.264 NAL format to annex b */
1070 if( p_sys->i_nal_size_length && !p_sys->in.b_direct )
1072 p_header->nFilledLen = 0;
1073 convert_sps_pps( p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
1074 p_header->pBuffer, p_header->nAllocLen,
1075 (uint32_t*) &p_header->nFilledLen, NULL );
1077 else if(p_sys->in.b_direct)
1079 p_header->pOutputPortPrivate = p_header->pBuffer;
1080 p_header->pBuffer = p_dec->fmt_in.p_extra;
1082 else if (p_dec->fmt_in.i_codec == VLC_CODEC_WMV3 &&
1083 p_dec->fmt_in.i_extra >= 4 &&
1084 p_header->nAllocLen >= 36)
1087 // According to OMX IL 1.2.0 spec (4.3.33.2), the codec config
1088 // data for VC-1 Main/Simple (aka WMV3) is according to table 265
1089 // in the VC-1 spec. Most of the fields are just set with placeholders
1090 // (like framerate, hrd_buffer/rate).
1091 static const uint8_t wmv3seq[] = {
1092 0xff, 0xff, 0xff, 0xc5, // numframes=ffffff, marker byte
1093 0x04, 0x00, 0x00, 0x00, // marker byte
1094 0x00, 0x00, 0x00, 0x00, // struct C, almost equal to p_extra
1095 0x00, 0x00, 0x00, 0x00, // struct A, vert size
1096 0x00, 0x00, 0x00, 0x00, // struct A, horiz size
1097 0x0c, 0x00, 0x00, 0x00, // marker byte
1098 0xff, 0xff, 0x00, 0x80, // struct B, level=4, cbr=0, hrd_buffer=ffff
1099 0xff, 0xff, 0x00, 0x00, // struct B, hrd_rate=ffff
1100 0xff, 0xff, 0xff, 0xff, // struct B, framerate=ffffffff
1102 p_header->nFilledLen = sizeof(wmv3seq);
1103 memcpy(p_header->pBuffer, wmv3seq, p_header->nFilledLen);
1104 // Struct C - almost equal to the extradata
1105 memcpy(&p_header->pBuffer[8], p_dec->fmt_in.p_extra, 4);
1106 // Expand profile from the highest 2 bits to the highest 4 bits
1107 profile = p_header->pBuffer[8] >> 6;
1108 p_header->pBuffer[8] = (p_header->pBuffer[8] & 0x0f) | (profile << 4);
1109 // Fill in the height/width for struct A
1110 SetDWLE(&p_header->pBuffer[12], p_dec->fmt_in.video.i_height);
1111 SetDWLE(&p_header->pBuffer[16], p_dec->fmt_in.video.i_width);
1115 if(p_header->nFilledLen > p_header->nAllocLen)
1117 msg_Dbg(p_dec, "buffer too small (%i,%i)", (int)p_header->nFilledLen,
1118 (int)p_header->nAllocLen);
1119 p_header->nFilledLen = p_header->nAllocLen;
1121 memcpy(p_header->pBuffer, p_dec->fmt_in.p_extra, p_header->nFilledLen);
1124 p_header->nOffset = 0;
1125 p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG | OMX_BUFFERFLAG_ENDOFFRAME;
1126 msg_Dbg(p_dec, "sending codec config data %p, %p, %i", p_header,
1127 p_header->pBuffer, (int)p_header->nFilledLen);
1128 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1131 /* Get back output port definition */
1132 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1133 if(omx_error != OMX_ErrorNone) goto error;
1135 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1136 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1138 if(p_sys->b_error) goto error;
1140 p_dec->b_need_packetized = true;
1141 if (!strcmp(p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER"))
1142 p_sys->b_use_pts = 1;
1144 if (!strcmp(p_sys->psz_component, "OMX.STM.Video.Decoder"))
1145 p_sys->b_use_pts = 1;
1147 if (p_sys->b_use_pts)
1148 msg_Dbg( p_dec, "using pts timestamp mode for %s", p_sys->psz_component);
1153 CloseGeneric(p_this);
1154 return VLC_EGENERIC;
1157 /*****************************************************************************
1159 *****************************************************************************/
1160 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
1162 decoder_sys_t *p_sys = p_dec->p_sys;
1163 OMX_PARAM_PORTDEFINITIONTYPE definition;
1164 OMX_BUFFERHEADERTYPE *p_buffer;
1165 OMX_ERRORTYPE omx_error;
1168 /* Sanity checking */
1169 OMX_INIT_STRUCTURE(definition);
1170 definition.nPortIndex = p_port->i_port_index;
1171 omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1173 if(omx_error != OMX_ErrorNone || (p_dec->fmt_in.i_cat == VIDEO_ES &&
1174 (!definition.format.video.nFrameWidth ||
1175 !definition.format.video.nFrameHeight)) )
1176 return OMX_ErrorUndefined;
1178 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
1179 p_port->i_port_index, NULL);
1180 CHECK_ERROR(omx_error, "OMX_CommandPortDisable on %i failed (%x)",
1181 (int)p_port->i_port_index, omx_error );
1183 for(i = 0; i < p_port->i_buffers; i++)
1185 OMX_FIFO_GET(&p_port->fifo, p_buffer);
1186 if (p_buffer->nFlags & SENTINEL_FLAG) {
1191 omx_error = OMX_FreeBuffer( p_sys->omx_handle,
1192 p_port->i_port_index, p_buffer );
1194 if(omx_error != OMX_ErrorNone) break;
1196 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
1197 omx_error, (int)p_port->i_port_index, i );
1199 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1200 CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
1202 /* Get the new port definition */
1203 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1204 if(omx_error != OMX_ErrorNone) goto error;
1206 if( p_dec->fmt_in.i_cat != AUDIO_ES )
1208 /* Don't explicitly set the new parameters that we got with
1209 * OMX_GetParameter above when using audio codecs.
1210 * That struct hasn't been changed since, so there should be
1211 * no need to set it here, unless some codec expects the
1212 * SetParameter call as a trigger event for some part of
1213 * the reconfiguration.
1214 * This fixes using audio decoders on Samsung Galaxy S II,
1216 * Only skipping this for audio codecs, to minimize the
1217 * change for current working configurations for video.
1219 omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1221 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
1222 omx_error, ErrorToString(omx_error));
1225 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
1226 p_port->i_port_index, NULL);
1227 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
1228 (int)p_port->i_port_index, omx_error );
1230 if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
1231 free(p_port->pp_buffers);
1232 p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
1233 if(!p_port->pp_buffers)
1235 omx_error = OMX_ErrorInsufficientResources;
1236 CHECK_ERROR(omx_error, "memory allocation failed");
1239 p_port->i_buffers = p_port->definition.nBufferCountActual;
1240 for(i = 0; i < p_port->i_buffers; i++)
1242 if(0 && p_port->b_direct)
1244 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1245 p_port->i_port_index, 0,
1246 p_port->definition.nBufferSize, (void*)1);
1249 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1250 p_port->i_port_index, 0,
1251 p_port->definition.nBufferSize);
1253 if(omx_error != OMX_ErrorNone) break;
1254 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1256 p_port->i_buffers = i;
1257 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1258 omx_error, (int)p_port->i_port_index, i );
1260 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1261 CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1263 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1264 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1270 /*****************************************************************************
1271 * DecodeVideo: Called to decode one frame
1272 *****************************************************************************/
1273 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1275 decoder_sys_t *p_sys = p_dec->p_sys;
1276 picture_t *p_pic = NULL, *p_next_pic;
1277 OMX_ERRORTYPE omx_error;
1280 OMX_BUFFERHEADERTYPE *p_header;
1283 if( !pp_block || !*pp_block )
1286 p_block = *pp_block;
1288 /* Check for errors from codec */
1291 msg_Dbg(p_dec, "error during decoding");
1292 block_Release( p_block );
1296 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1298 block_Release( p_block );
1299 if(!p_sys->in.b_flushed)
1301 msg_Dbg(p_dec, "flushing");
1302 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1303 p_sys->in.definition.nPortIndex, 0 );
1305 p_sys->in.b_flushed = true;
1309 /* Take care of decoded frames first */
1312 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1313 if(!p_header) break; /* No frame available */
1315 if(p_sys->out.b_update_def)
1317 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1318 p_sys->out.b_update_def = 0;
1321 if(p_header->nFilledLen)
1323 p_pic = p_header->pAppPrivate;
1326 /* We're not in direct rendering mode.
1327 * Get a new picture and copy the content */
1328 p_pic = decoder_NewPicture( p_dec );
1331 CopyOmxPicture(p_sys->out.definition.format.video.eColorFormat,
1332 p_pic, p_sys->out.definition.format.video.nSliceHeight,
1333 p_sys->out.i_frame_stride,
1334 p_header->pBuffer + p_header->nOffset,
1335 p_sys->out.i_frame_stride_chroma_div);
1339 p_pic->date = p_header->nTimeStamp;
1340 p_header->nFilledLen = 0;
1341 p_header->pAppPrivate = 0;
1344 /* Get a new picture */
1345 if(p_sys->in.b_direct && !p_header->pAppPrivate)
1347 p_next_pic = decoder_NewPicture( p_dec );
1348 if(!p_next_pic) break;
1350 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1351 p_header->pAppPrivate = p_next_pic;
1352 p_header->pInputPortPrivate = p_header->pBuffer;
1353 p_header->pBuffer = p_next_pic->p[0].p_pixels;
1357 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1360 #ifdef OMXIL_EXTRA_DEBUG
1361 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1363 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1366 /* Send the input buffer to the component */
1367 OMX_FIFO_GET_TIMEOUT(&p_sys->in.fifo, p_header, 200000);
1369 if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1376 p_header->nFilledLen = p_block->i_buffer;
1377 p_header->nOffset = 0;
1378 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1379 if (p_sys->b_use_pts && p_block->i_pts)
1380 p_header->nTimeStamp = p_block->i_pts;
1382 p_header->nTimeStamp = p_block->i_dts;
1384 /* In direct mode we pass the input pointer as is.
1385 * Otherwise we memcopy the data */
1386 if(p_sys->in.b_direct)
1388 p_header->pOutputPortPrivate = p_header->pBuffer;
1389 p_header->pBuffer = p_block->p_buffer;
1390 p_header->pAppPrivate = p_block;
1394 if(p_header->nFilledLen > p_header->nAllocLen)
1396 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1397 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1398 p_header->nFilledLen = p_header->nAllocLen;
1400 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1401 block_Release(p_block);
1404 /* Convert H.264 NAL format to annex b. Doesn't do anything if
1405 * i_nal_size_length == 0, which is the case for codecs other
1407 convert_h264_to_annexb( p_header->pBuffer, p_header->nFilledLen,
1408 p_sys->i_nal_size_length );
1409 #ifdef OMXIL_EXTRA_DEBUG
1410 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1411 (int)p_header->nFilledLen );
1413 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1414 p_sys->in.b_flushed = false;
1415 *pp_block = NULL; /* Avoid being fed the same packet again */
1419 /* Handle the PortSettingsChanged events */
1420 for(i = 0; i < p_sys->ports; i++)
1422 OmxPort *p_port = &p_sys->p_ports[i];
1423 if(p_port->b_reconfigure)
1425 omx_error = PortReconfigure(p_dec, p_port);
1426 p_port->b_reconfigure = 0;
1428 if(p_port->b_update_def)
1430 omx_error = GetPortDefinition(p_dec, p_port, p_port->p_fmt);
1431 p_port->b_update_def = 0;
1438 /*****************************************************************************
1439 * DecodeAudio: Called to decode one frame
1440 *****************************************************************************/
1441 block_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1443 decoder_sys_t *p_sys = p_dec->p_sys;
1444 block_t *p_buffer = NULL;
1445 OMX_BUFFERHEADERTYPE *p_header;
1446 OMX_ERRORTYPE omx_error;
1450 if( !pp_block || !*pp_block ) return NULL;
1452 p_block = *pp_block;
1454 /* Check for errors from codec */
1457 msg_Dbg(p_dec, "error during decoding");
1458 block_Release( p_block );
1462 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1464 block_Release( p_block );
1465 date_Set( &p_sys->end_date, 0 );
1466 if(!p_sys->in.b_flushed)
1468 msg_Dbg(p_dec, "flushing");
1469 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1470 p_sys->in.definition.nPortIndex, 0 );
1472 p_sys->in.b_flushed = true;
1476 if( !date_Get( &p_sys->end_date ) )
1478 if( !p_block->i_pts )
1480 /* We've just started the stream, wait for the first PTS. */
1481 block_Release( p_block );
1484 date_Set( &p_sys->end_date, p_block->i_pts );
1487 /* Take care of decoded frames first */
1490 unsigned int i_samples = 0;
1492 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1493 if(!p_header) break; /* No frame available */
1495 if (p_sys->out.p_fmt->audio.i_channels)
1496 i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1499 p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1500 if( !p_buffer ) break; /* No audio buffer available */
1502 memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1503 p_header->nFilledLen = 0;
1505 if( p_header->nTimeStamp != 0 &&
1506 p_header->nTimeStamp != date_Get( &p_sys->end_date ) )
1507 date_Set( &p_sys->end_date, p_header->nTimeStamp );
1509 p_buffer->i_pts = date_Get( &p_sys->end_date );
1510 p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1514 #ifdef OMXIL_EXTRA_DEBUG
1515 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1517 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1518 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1522 /* Send the input buffer to the component */
1523 OMX_FIFO_GET_TIMEOUT(&p_sys->in.fifo, p_header, 200000);
1525 if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1532 p_header->nFilledLen = p_block->i_buffer;
1533 p_header->nOffset = 0;
1534 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1535 p_header->nTimeStamp = p_block->i_dts;
1537 /* In direct mode we pass the input pointer as is.
1538 * Otherwise we memcopy the data */
1539 if(p_sys->in.b_direct)
1541 p_header->pOutputPortPrivate = p_header->pBuffer;
1542 p_header->pBuffer = p_block->p_buffer;
1543 p_header->pAppPrivate = p_block;
1547 if(p_header->nFilledLen > p_header->nAllocLen)
1549 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1550 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1551 p_header->nFilledLen = p_header->nAllocLen;
1553 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1554 block_Release(p_block);
1557 #ifdef OMXIL_EXTRA_DEBUG
1558 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1559 (int)p_header->nFilledLen );
1561 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1562 p_sys->in.b_flushed = false;
1563 *pp_block = NULL; /* Avoid being fed the same packet again */
1567 /* Handle the PortSettingsChanged events */
1568 for(i = 0; i < p_sys->ports; i++)
1570 OmxPort *p_port = &p_sys->p_ports[i];
1571 if(!p_port->b_reconfigure) continue;
1572 p_port->b_reconfigure = 0;
1573 omx_error = PortReconfigure(p_dec, p_port);
1579 /*****************************************************************************
1580 * EncodeVideo: Called to encode one frame
1581 *****************************************************************************/
1582 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1584 decoder_t *p_dec = ( decoder_t *)p_enc;
1585 decoder_sys_t *p_sys = p_dec->p_sys;
1586 OMX_ERRORTYPE omx_error;
1589 OMX_BUFFERHEADERTYPE *p_header;
1590 block_t *p_block = 0;
1592 if( !p_pic ) return NULL;
1594 /* Check for errors from codec */
1597 msg_Dbg(p_dec, "error during encoding");
1601 /* Send the input buffer to the component */
1602 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1605 /* In direct mode we pass the input pointer as is.
1606 * Otherwise we memcopy the data */
1607 if(p_sys->in.b_direct)
1609 p_header->pOutputPortPrivate = p_header->pBuffer;
1610 p_header->pBuffer = p_pic->p[0].p_pixels;
1614 CopyVlcPicture(p_dec, p_header, p_pic);
1617 p_header->nFilledLen = p_sys->in.i_frame_size;
1618 p_header->nOffset = 0;
1619 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1620 p_header->nTimeStamp = p_pic->date;
1621 #ifdef OMXIL_EXTRA_DEBUG
1622 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1623 (int)p_header->nFilledLen );
1625 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1626 p_sys->in.b_flushed = false;
1629 /* Handle the PortSettingsChanged events */
1630 for(i = 0; i < p_sys->ports; i++)
1632 OmxPort *p_port = &p_sys->p_ports[i];
1633 if(!p_port->b_reconfigure) continue;
1634 p_port->b_reconfigure = 0;
1635 omx_error = PortReconfigure(p_dec, p_port);
1638 /* Wait for the decoded frame */
1641 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1643 if(p_header->nFilledLen)
1645 if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1647 /* TODO: need to store codec config */
1648 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1651 p_block = p_header->pAppPrivate;
1654 /* We're not in direct rendering mode.
1655 * Get a new block and copy the content */
1656 p_block = block_Alloc( p_header->nFilledLen );
1657 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1660 p_block->i_buffer = p_header->nFilledLen;
1661 p_block->i_pts = p_block->i_dts = p_header->nTimeStamp;
1662 p_header->nFilledLen = 0;
1663 p_header->pAppPrivate = 0;
1666 #ifdef OMXIL_EXTRA_DEBUG
1667 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1669 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1672 msg_Dbg(p_dec, "done");
1676 /*****************************************************************************
1677 * CloseGeneric: omxil decoder destruction
1678 *****************************************************************************/
1679 static void CloseGeneric( vlc_object_t *p_this )
1681 decoder_t *p_dec = (decoder_t *)p_this;
1682 decoder_sys_t *p_sys = p_dec->p_sys;
1684 if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1685 vlc_mutex_lock( &omx_core_mutex );
1687 if( omx_refcount == 0 )
1689 if( p_sys->b_init ) pf_deinit();
1690 dll_close( dll_handle );
1692 vlc_mutex_unlock( &omx_core_mutex );
1694 vlc_mutex_destroy (&p_sys->mutex);
1695 vlc_cond_destroy (&p_sys->cond);
1696 vlc_mutex_destroy (&p_sys->in.fifo.lock);
1697 vlc_cond_destroy (&p_sys->in.fifo.wait);
1698 vlc_mutex_destroy (&p_sys->out.fifo.lock);
1699 vlc_cond_destroy (&p_sys->out.fifo.wait);
1704 /*****************************************************************************
1706 *****************************************************************************/
1707 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1708 OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1709 OMX_U32 data_2, OMX_PTR event_data )
1711 decoder_t *p_dec = (decoder_t *)app_data;
1712 decoder_sys_t *p_sys = p_dec->p_sys;
1718 case OMX_EventCmdComplete:
1719 switch ((OMX_STATETYPE)data_1)
1721 case OMX_CommandStateSet:
1722 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %s)", EventToString(event),
1723 CommandToString(data_1), StateToString(data_2) );
1727 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u)", EventToString(event),
1728 CommandToString(data_1), (unsigned int)data_2 );
1733 case OMX_EventError:
1734 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u, %s)", EventToString(event),
1735 ErrorToString((OMX_ERRORTYPE)data_1), (unsigned int)data_2,
1736 (const char *)event_data);
1737 //p_sys->b_error = true;
1740 case OMX_EventPortSettingsChanged:
1741 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1742 (unsigned int)data_1, (unsigned int)data_2 );
1743 if( data_2 == 0 || data_2 == OMX_IndexParamPortDefinition )
1745 OMX_BUFFERHEADERTYPE *sentinel;
1746 for(i = 0; i < p_sys->ports; i++)
1747 if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1748 p_sys->p_ports[i].b_reconfigure = true;
1749 sentinel = calloc(1, sizeof(*sentinel));
1751 sentinel->nFlags = SENTINEL_FLAG;
1752 OMX_FIFO_PUT(&p_sys->in.fifo, sentinel);
1755 else if( data_2 == OMX_IndexConfigCommonOutputCrop )
1757 for(i = 0; i < p_sys->ports; i++)
1758 if(p_sys->p_ports[i].definition.nPortIndex == data_1)
1759 p_sys->p_ports[i].b_update_def = true;
1763 msg_Dbg( p_dec, "Unhandled setting change %x", (unsigned int)data_2 );
1768 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1769 (unsigned int)data_1, (unsigned int)data_2 );
1773 PostOmxEvent(p_dec, event, data_1, data_2, event_data);
1774 return OMX_ErrorNone;
1777 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1778 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1780 decoder_t *p_dec = (decoder_t *)app_data;
1781 decoder_sys_t *p_sys = p_dec->p_sys;
1784 #ifdef OMXIL_EXTRA_DEBUG
1785 msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1788 if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1790 block_t *p_block = (block_t *)omx_header->pAppPrivate;
1791 omx_header->pBuffer = omx_header->pOutputPortPrivate;
1792 if(p_block) block_Release(p_block);
1793 omx_header->pAppPrivate = 0;
1795 OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1797 return OMX_ErrorNone;
1800 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1801 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1803 decoder_t *p_dec = (decoder_t *)app_data;
1804 decoder_sys_t *p_sys = p_dec->p_sys;
1807 #ifdef OMXIL_EXTRA_DEBUG
1808 msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
1809 (int)omx_header->nFilledLen );
1812 if(omx_header->pInputPortPrivate)
1814 omx_header->pBuffer = omx_header->pInputPortPrivate;
1816 OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1818 return OMX_ErrorNone;