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,
416 p_fmt->audio.i_channels,
419 p_fmt->audio.i_bitspersample,
420 p_fmt->audio.i_blockalign);
421 if (def->eDir == OMX_DirInput) {
422 CHECK_ERROR(omx_error, "SetAudioParameters failed (%x : %s)",
423 omx_error, ErrorToString(omx_error));
425 msg_Warn(p_dec, "SetAudioParameters failed (%x : %s) on output port",
426 omx_error, ErrorToString(omx_error));
427 omx_error = OMX_ErrorNone;
430 if (!strcmp(p_dec->p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER") &&
431 def->eDir == OMX_DirOutput)
433 /* When setting the output buffer size above, the decoder actually
434 * sets the buffer size to a lower value than what was chosen. If
435 * we try to allocate buffers of this size, it fails. Thus, forcibly
436 * use a larger buffer size. */
437 def->nBufferSize *= 2;
444 /*****************************************************************************
445 * GetPortDefinition: set vlc format based on the definition of the omx port
446 *****************************************************************************/
447 static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
450 decoder_sys_t *p_sys = p_dec->p_sys;
451 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
452 OMX_ERRORTYPE omx_error;
453 OMX_CONFIG_RECTTYPE crop_rect;
455 omx_error = OMX_GetParameter(p_port->omx_handle,
456 OMX_IndexParamPortDefinition, def);
457 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
458 omx_error, ErrorToString(omx_error));
463 p_fmt->video.i_width = def->format.video.nFrameWidth;
464 p_fmt->video.i_visible_width = def->format.video.nFrameWidth;
465 p_fmt->video.i_height = def->format.video.nFrameHeight;
466 p_fmt->video.i_visible_height = def->format.video.nFrameHeight;
467 p_fmt->video.i_frame_rate = p_dec->fmt_in.video.i_frame_rate;
468 p_fmt->video.i_frame_rate_base = p_dec->fmt_in.video.i_frame_rate_base;
470 OMX_INIT_STRUCTURE(crop_rect);
471 crop_rect.nPortIndex = def->nPortIndex;
472 omx_error = OMX_GetConfig(p_port->omx_handle, OMX_IndexConfigCommonOutputCrop, &crop_rect);
473 if (omx_error == OMX_ErrorNone)
475 if (!def->format.video.nSliceHeight)
476 def->format.video.nSliceHeight = def->format.video.nFrameHeight;
477 if (!def->format.video.nStride)
478 def->format.video.nStride = def->format.video.nFrameWidth;
479 p_fmt->video.i_width = crop_rect.nWidth;
480 p_fmt->video.i_visible_width = crop_rect.nWidth;
481 p_fmt->video.i_height = crop_rect.nHeight;
482 p_fmt->video.i_visible_height = crop_rect.nHeight;
483 if (def->format.video.eColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
484 def->format.video.nSliceHeight -= crop_rect.nTop/2;
488 /* Don't pass the error back to the caller, this isn't mandatory */
489 omx_error = OMX_ErrorNone;
492 /* Hack: Nexus One (stock firmware with binary OMX driver blob)
493 * claims to output 420Planar even though it in in practice is
495 if(def->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar &&
496 !strncmp(p_sys->psz_component, "OMX.qcom.video.decoder",
497 strlen("OMX.qcom.video.decoder")))
498 def->format.video.eColorFormat = OMX_QCOM_COLOR_FormatYVU420SemiPlanar;
500 if (IgnoreOmxDecoderPadding(p_sys->psz_component)) {
501 def->format.video.nSliceHeight = 0;
502 def->format.video.nStride = p_fmt->video.i_width;
505 if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
506 &p_fmt->i_codec, 0 ) )
508 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
509 &p_fmt->i_codec, 0 ) )
511 omx_error = OMX_ErrorNotImplemented;
512 CHECK_ERROR(omx_error, "OMX color format %i not supported",
513 (int)def->format.video.eColorFormat );
515 GetVlcChromaSizes( p_fmt->i_codec,
516 def->format.video.nFrameWidth,
517 def->format.video.nFrameHeight,
518 &p_port->i_frame_size, &p_port->i_frame_stride,
519 &p_port->i_frame_stride_chroma_div );
521 if(p_port->i_frame_size > def->nBufferSize)
522 def->nBufferSize = p_port->i_frame_size;
523 p_port->i_frame_size = def->nBufferSize;
525 if((int)p_port->i_frame_stride > def->format.video.nStride)
526 def->format.video.nStride = p_port->i_frame_stride;
528 p_port->i_frame_stride = def->format.video.nStride;
532 if( !OmxToVlcAudioFormat( def->format.audio.eEncoding,
533 &p_fmt->i_codec, 0 ) )
535 omx_error = OMX_ErrorNotImplemented;
536 CHECK_ERROR(omx_error, "OMX audio format %i not supported",
537 (int)def->format.audio.eEncoding );
540 omx_error = GetAudioParameters(p_port->omx_handle,
541 &p_port->format_param, def->nPortIndex,
542 def->format.audio.eEncoding,
543 &p_fmt->audio.i_channels,
544 &p_fmt->audio.i_rate,
546 &p_fmt->audio.i_bitspersample,
547 &p_fmt->audio.i_blockalign);
548 CHECK_ERROR(omx_error, "GetAudioParameters failed (%x : %s)",
549 omx_error, ErrorToString(omx_error));
551 if(p_fmt->audio.i_channels < 9)
553 static const int pi_channels_maps[9] =
555 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
556 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
557 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
558 | AOUT_CHAN_REARRIGHT,
559 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
560 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
561 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
562 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
563 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
564 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
565 | AOUT_CHAN_MIDDLERIGHT,
566 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
567 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
570 p_fmt->audio.i_physical_channels =
571 p_fmt->audio.i_original_channels =
572 pi_channels_maps[p_fmt->audio.i_channels];
575 date_Init( &p_dec->p_sys->end_date, p_fmt->audio.i_rate, 1 );
579 default: return OMX_ErrorNotImplemented;
586 /*****************************************************************************
587 * DeinitialiseComponent: Deinitialise and unload an OMX component
588 *****************************************************************************/
589 static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
590 OMX_HANDLETYPE omx_handle)
592 decoder_sys_t *p_sys = p_dec->p_sys;
593 OMX_ERRORTYPE omx_error;
597 if(!omx_handle) return OMX_ErrorNone;
599 omx_error = OMX_GetState(omx_handle, &state);
600 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
602 if(state == OMX_StateExecuting)
604 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
606 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
609 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, &cmd, &state, 0);
610 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
611 // The event queue can contain other OMX_EventCmdComplete items,
612 // such as for OMX_CommandFlush
613 if (cmd == OMX_CommandStateSet && state == OMX_StateIdle)
618 omx_error = OMX_GetState(omx_handle, &state);
619 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
621 if(state == OMX_StateIdle)
623 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
624 OMX_StateLoaded, 0 );
625 CHECK_ERROR(omx_error, "OMX_CommandStateSet Loaded failed (%x)", omx_error );
627 for(i = 0; i < p_sys->ports; i++)
629 OmxPort *p_port = &p_sys->p_ports[i];
630 OMX_BUFFERHEADERTYPE *p_buffer;
632 for(j = 0; j < p_port->i_buffers; j++)
634 OMX_FIFO_GET(&p_port->fifo, p_buffer);
635 if (p_buffer->nFlags & SENTINEL_FLAG) {
640 omx_error = OMX_FreeBuffer( omx_handle,
641 p_port->i_port_index, p_buffer );
643 if(omx_error != OMX_ErrorNone) break;
645 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
646 omx_error, (int)p_port->i_port_index, j );
648 OMX_FIFO_PEEK(&p_port->fifo, p_buffer);
649 if (!p_buffer) break;
651 OMX_FIFO_GET(&p_port->fifo, p_buffer);
652 if (p_buffer->nFlags & SENTINEL_FLAG) {
656 msg_Warn( p_dec, "Stray buffer left in fifo, %p", p_buffer );
660 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
661 CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
665 for(i = 0; i < p_sys->ports; i++)
667 OmxPort *p_port = &p_sys->p_ports[i];
668 free(p_port->pp_buffers);
669 p_port->pp_buffers = 0;
671 omx_error = pf_free_handle( omx_handle );
675 /*****************************************************************************
676 * InitialiseComponent: Load and initialise an OMX component
677 *****************************************************************************/
678 static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
679 OMX_STRING psz_component, OMX_HANDLETYPE *p_handle)
681 static OMX_CALLBACKTYPE callbacks =
682 { OmxEventHandler, OmxEmptyBufferDone, OmxFillBufferDone };
683 decoder_sys_t *p_sys = p_dec->p_sys;
684 OMX_HANDLETYPE omx_handle;
685 OMX_ERRORTYPE omx_error;
687 OMX_U8 psz_role[OMX_MAX_STRINGNAME_SIZE];
688 OMX_PARAM_COMPONENTROLETYPE role;
689 OMX_PARAM_PORTDEFINITIONTYPE definition;
690 OMX_PORT_PARAM_TYPE param;
693 omx_error = pf_get_handle( &omx_handle, psz_component, p_dec, &callbacks );
694 if(omx_error != OMX_ErrorNone)
696 msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
697 omx_error, ErrorToString(omx_error) );
700 strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
702 omx_error = OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
703 if(omx_error == OMX_ErrorNone)
704 msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
706 msg_Dbg(p_dec, "loaded component %s", psz_component);
707 PrintOmx(p_dec, omx_handle, OMX_ALL);
709 /* Set component role */
710 OMX_INIT_STRUCTURE(role);
711 strcpy((char*)role.cRole,
712 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec : p_dec->fmt_in.i_codec,
713 p_dec->fmt_in.i_cat, p_sys->b_enc));
715 omx_error = OMX_SetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
717 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
719 if(omx_error == OMX_ErrorNone)
720 msg_Dbg(p_dec, "component standard role set to %s", role.cRole);
722 /* Find the input / output ports */
723 OMX_INIT_STRUCTURE(param);
724 OMX_INIT_STRUCTURE(definition);
725 omx_error = OMX_GetParameter(omx_handle, p_dec->fmt_in.i_cat == VIDEO_ES ?
726 OMX_IndexParamVideoInit : OMX_IndexParamAudioInit, ¶m);
727 if(omx_error != OMX_ErrorNone) {
730 param.nStartPortNumber = 0;
736 for(i = 0; i < param.nPorts; i++)
740 /* Get port definition */
741 definition.nPortIndex = param.nStartPortNumber + i;
742 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
744 if(omx_error != OMX_ErrorNone) continue;
746 if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
747 else p_port = &p_sys->out;
749 p_port->b_valid = true;
750 p_port->i_port_index = definition.nPortIndex;
751 p_port->definition = definition;
752 p_port->omx_handle = omx_handle;
755 if(!p_sys->in.b_valid || !p_sys->out.b_valid)
757 omx_error = OMX_ErrorInvalidComponent;
758 CHECK_ERROR(omx_error, "couldn't find an input and output port");
761 if(!strncmp(p_sys->psz_component, "OMX.SEC.", 8) &&
762 p_dec->fmt_in.i_cat == VIDEO_ES)
765 omx_error = OMX_GetExtensionIndex(omx_handle, (OMX_STRING) "OMX.SEC.index.ThumbnailMode", &index);
766 if(omx_error == OMX_ErrorNone)
768 OMX_BOOL enable = OMX_TRUE;
769 omx_error = OMX_SetConfig(omx_handle, index, &enable);
770 CHECK_ERROR(omx_error, "Unable to set ThumbnailMode");
772 OMX_BOOL enable = OMX_TRUE;
773 /* Needed on Samsung Galaxy S II */
774 omx_error = OMX_SetConfig(omx_handle, OMX_IndexVendorSetYUV420pMode, &enable);
775 if (omx_error == OMX_ErrorNone)
776 msg_Dbg(p_dec, "Set OMX_IndexVendorSetYUV420pMode successfully");
778 msg_Dbg(p_dec, "Unable to set OMX_IndexVendorSetYUV420pMode: %x", omx_error);
782 /* Set port definitions */
783 for(i = 0; i < p_sys->ports; i++)
785 omx_error = SetPortDefinition(p_dec, &p_sys->p_ports[i],
786 p_sys->p_ports[i].p_fmt);
787 if(omx_error != OMX_ErrorNone) goto error;
790 /* Allocate our array for the omx buffers and enable ports */
791 for(i = 0; i < p_sys->ports; i++)
793 OmxPort *p_port = &p_sys->p_ports[i];
796 malloc(p_port->definition.nBufferCountActual *
797 sizeof(OMX_BUFFERHEADERTYPE*));
798 if(!p_port->pp_buffers)
800 omx_error = OMX_ErrorInsufficientResources;
801 CHECK_ERROR(omx_error, "memory allocation failed");
803 p_port->i_buffers = p_port->definition.nBufferCountActual;
806 if(!p_port->definition.bEnabled)
808 omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
809 p_port->i_port_index, NULL);
810 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
811 (int)p_port->i_port_index, omx_error );
812 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
813 CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
814 (int)p_port->i_port_index, omx_error );
818 *p_handle = omx_handle;
819 return OMX_ErrorNone;
822 DeinitialiseComponent(p_dec, omx_handle);
827 /*****************************************************************************
828 * OpenDecoder: Create the decoder instance
829 *****************************************************************************/
830 static int OpenDecoder( vlc_object_t *p_this )
832 decoder_t *p_dec = (decoder_t*)p_this;
835 if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
839 if( p_dec->fmt_in.i_cat != VIDEO_ES && !p_dec->b_force)
843 status = OpenGeneric( p_this, false );
844 if(status != VLC_SUCCESS) return status;
846 p_dec->pf_decode_video = DecodeVideo;
847 p_dec->pf_decode_audio = DecodeAudio;
852 /*****************************************************************************
853 * OpenEncoder: Create the encoder instance
854 *****************************************************************************/
855 static int OpenEncoder( vlc_object_t *p_this )
857 encoder_t *p_enc = (encoder_t*)p_this;
860 if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
863 status = OpenGeneric( p_this, true );
864 if(status != VLC_SUCCESS) return status;
866 p_enc->pf_encode_video = EncodeVideo;
871 /*****************************************************************************
872 * OpenGeneric: Create the generic decoder/encoder instance
873 *****************************************************************************/
874 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
876 decoder_t *p_dec = (decoder_t*)p_this;
877 decoder_sys_t *p_sys;
878 OMX_ERRORTYPE omx_error;
879 OMX_BUFFERHEADERTYPE *p_header;
882 vlc_mutex_lock( &omx_core_mutex );
883 if( omx_refcount > 0 )
886 /* Load the OMX core */
887 for( i = 0; ppsz_dll_list[i]; i++ )
889 dll_handle = dll_open( ppsz_dll_list[i] );
890 if( dll_handle ) break;
894 vlc_mutex_unlock( &omx_core_mutex );
898 pf_init = dlsym( dll_handle, "OMX_Init" );
899 pf_deinit = dlsym( dll_handle, "OMX_Deinit" );
900 pf_get_handle = dlsym( dll_handle, "OMX_GetHandle" );
901 pf_free_handle = dlsym( dll_handle, "OMX_FreeHandle" );
902 pf_component_enum = dlsym( dll_handle, "OMX_ComponentNameEnum" );
903 pf_get_roles_of_component = dlsym( dll_handle, "OMX_GetRolesOfComponent" );
904 if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle ||
905 !pf_component_enum || !pf_get_roles_of_component )
907 msg_Warn( p_this, "cannot find OMX_* symbols in `%s' (%s)",
908 ppsz_dll_list[i], dlerror() );
909 dll_close(dll_handle);
910 vlc_mutex_unlock( &omx_core_mutex );
915 /* Allocate the memory needed to store the decoder's structure */
916 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
918 if( omx_refcount == 0 )
919 dll_close(dll_handle);
920 vlc_mutex_unlock( &omx_core_mutex );
924 /* Initialise the thread properties */
927 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
928 p_dec->fmt_out.video = p_dec->fmt_in.video;
929 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
930 p_dec->fmt_out.i_codec = 0;
932 p_sys->b_enc = b_encode;
933 p_sys->pp_last_event = &p_sys->p_events;
934 vlc_mutex_init (&p_sys->mutex);
935 vlc_cond_init (&p_sys->cond);
936 vlc_mutex_init (&p_sys->lock);
937 vlc_mutex_init (&p_sys->in.fifo.lock);
938 vlc_cond_init (&p_sys->in.fifo.wait);
939 p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
940 p_sys->in.fifo.pp_last = &p_sys->in.fifo.p_first;
941 p_sys->in.b_direct = false;
942 p_sys->in.b_flushed = true;
943 p_sys->in.p_fmt = &p_dec->fmt_in;
944 vlc_mutex_init (&p_sys->out.fifo.lock);
945 vlc_cond_init (&p_sys->out.fifo.wait);
946 p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
947 p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
948 p_sys->out.b_direct = true;
949 p_sys->out.b_flushed = true;
950 p_sys->out.p_fmt = &p_dec->fmt_out;
952 p_sys->p_ports = &p_sys->in;
953 p_sys->b_use_pts = 0;
955 msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
956 (char *)&p_dec->fmt_out.i_codec);
958 /* Initialise the OMX core */
959 omx_error = omx_refcount > 0 ? OMX_ErrorNone : pf_init();
961 if(omx_error != OMX_ErrorNone)
963 msg_Warn( p_this, "OMX_Init failed (%x: %s)", omx_error,
964 ErrorToString(omx_error) );
965 vlc_mutex_unlock( &omx_core_mutex );
966 CloseGeneric(p_this);
969 p_sys->b_init = true;
970 vlc_mutex_unlock( &omx_core_mutex );
972 /* Enumerate components and build a list of the one we want to try */
973 if( !CreateComponentsList(p_dec,
974 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
975 p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
978 msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
979 (char *)&p_dec->fmt_in.i_codec );
980 CloseGeneric(p_this);
984 /* Try to load and initialise a component */
985 omx_error = OMX_ErrorUndefined;
986 for(i = 0; i < p_sys->components; i++)
989 /* ignore OpenCore software codecs */
990 if (!strncmp(p_sys->ppsz_components[i], "OMX.PV.", 7))
992 /* The same sw codecs, renamed in ICS (perhaps also in honeycomb) */
993 if (!strncmp(p_sys->ppsz_components[i], "OMX.google.", 11))
995 /* This one has been seen on HTC One V - it behaves like it works,
996 * but FillBufferDone returns buffers filled with 0 bytes. The One V
997 * has got a working OMX.qcom.video.decoder.avc instead though. */
998 if (!strncmp(p_sys->ppsz_components[i], "OMX.ARICENT.", 12))
1000 /* Some nVidia codec with DRM */
1001 if (!strncmp(p_sys->ppsz_components[i], "OMX.Nvidia.h264.decode.secure", 29))
1003 /* Use VC1 decoder for WMV3 for now */
1004 if (!strcmp(p_sys->ppsz_components[i], "OMX.SEC.WMV.Decoder"))
1006 /* This decoder does work, but has an insane latency (leading to errors
1007 * about "main audio output playback way too late" and dropped frames).
1008 * At least Samsung Galaxy S III (where this decoder is present) has
1009 * got another one, OMX.SEC.mp3.dec, that works well and has a
1010 * sensible latency. (Also, even if that one isn't found, in general,
1011 * using SW codecs is usually more than fast enough for MP3.) */
1012 if (!strcmp(p_sys->ppsz_components[i], "OMX.SEC.MP3.Decoder"))
1015 omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
1016 &p_sys->omx_handle);
1017 if(omx_error == OMX_ErrorNone) break;
1019 CHECK_ERROR(omx_error, "no component could be initialised" );
1021 /* Move component to Idle then Executing state */
1022 OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
1023 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
1025 /* Allocate omx buffers */
1026 for(i = 0; i < p_sys->ports; i++)
1028 OmxPort *p_port = &p_sys->p_ports[i];
1030 for(j = 0; j < p_port->i_buffers; j++)
1033 #define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
1034 char *p_buf = malloc(p_port->definition.nBufferSize +
1035 p_port->definition.nBufferAlignment);
1036 p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
1039 if(0 && p_port->b_direct)
1041 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
1042 p_port->i_port_index, 0,
1043 p_port->definition.nBufferSize, (void*)1);
1046 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
1047 p_port->i_port_index, 0,
1048 p_port->definition.nBufferSize);
1050 if(omx_error != OMX_ErrorNone) break;
1051 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
1053 p_port->i_buffers = j;
1054 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1055 omx_error, (int)p_port->i_port_index, j );
1058 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1059 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
1061 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
1062 OMX_StateExecuting, 0);
1063 CHECK_ERROR(omx_error, "OMX_CommandStateSet Executing failed (%x)", omx_error );
1064 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1065 CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
1067 /* Send codec configuration data */
1068 if( p_dec->fmt_in.i_extra )
1070 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1071 p_header->nFilledLen = p_dec->fmt_in.i_extra;
1073 /* Convert H.264 NAL format to annex b */
1074 if( p_sys->i_nal_size_length && !p_sys->in.b_direct )
1076 p_header->nFilledLen = 0;
1077 convert_sps_pps( p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
1078 p_header->pBuffer, p_header->nAllocLen,
1079 (uint32_t*) &p_header->nFilledLen, NULL );
1081 else if(p_sys->in.b_direct)
1083 p_header->pOutputPortPrivate = p_header->pBuffer;
1084 p_header->pBuffer = p_dec->fmt_in.p_extra;
1086 else if (p_dec->fmt_in.i_codec == VLC_CODEC_WMV3 &&
1087 p_dec->fmt_in.i_extra >= 4 &&
1088 p_header->nAllocLen >= 36)
1091 // According to OMX IL 1.2.0 spec (4.3.33.2), the codec config
1092 // data for VC-1 Main/Simple (aka WMV3) is according to table 265
1093 // in the VC-1 spec. Most of the fields are just set with placeholders
1094 // (like framerate, hrd_buffer/rate).
1095 static const uint8_t wmv3seq[] = {
1096 0xff, 0xff, 0xff, 0xc5, // numframes=ffffff, marker byte
1097 0x04, 0x00, 0x00, 0x00, // marker byte
1098 0x00, 0x00, 0x00, 0x00, // struct C, almost equal to p_extra
1099 0x00, 0x00, 0x00, 0x00, // struct A, vert size
1100 0x00, 0x00, 0x00, 0x00, // struct A, horiz size
1101 0x0c, 0x00, 0x00, 0x00, // marker byte
1102 0xff, 0xff, 0x00, 0x80, // struct B, level=4, cbr=0, hrd_buffer=ffff
1103 0xff, 0xff, 0x00, 0x00, // struct B, hrd_rate=ffff
1104 0xff, 0xff, 0xff, 0xff, // struct B, framerate=ffffffff
1106 p_header->nFilledLen = sizeof(wmv3seq);
1107 memcpy(p_header->pBuffer, wmv3seq, p_header->nFilledLen);
1108 // Struct C - almost equal to the extradata
1109 memcpy(&p_header->pBuffer[8], p_dec->fmt_in.p_extra, 4);
1110 // Expand profile from the highest 2 bits to the highest 4 bits
1111 profile = p_header->pBuffer[8] >> 6;
1112 p_header->pBuffer[8] = (p_header->pBuffer[8] & 0x0f) | (profile << 4);
1113 // Fill in the height/width for struct A
1114 SetDWLE(&p_header->pBuffer[12], p_dec->fmt_in.video.i_height);
1115 SetDWLE(&p_header->pBuffer[16], p_dec->fmt_in.video.i_width);
1119 if(p_header->nFilledLen > p_header->nAllocLen)
1121 msg_Dbg(p_dec, "buffer too small (%i,%i)", (int)p_header->nFilledLen,
1122 (int)p_header->nAllocLen);
1123 p_header->nFilledLen = p_header->nAllocLen;
1125 memcpy(p_header->pBuffer, p_dec->fmt_in.p_extra, p_header->nFilledLen);
1128 p_header->nOffset = 0;
1129 p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG | OMX_BUFFERFLAG_ENDOFFRAME;
1130 msg_Dbg(p_dec, "sending codec config data %p, %p, %i", p_header,
1131 p_header->pBuffer, (int)p_header->nFilledLen);
1132 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1135 /* Get back output port definition */
1136 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1137 if(omx_error != OMX_ErrorNone) goto error;
1139 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1140 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1142 if(p_sys->b_error) goto error;
1144 p_dec->b_need_packetized = true;
1145 if (!strcmp(p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER"))
1146 p_sys->b_use_pts = 1;
1148 if (!strcmp(p_sys->psz_component, "OMX.STM.Video.Decoder"))
1149 p_sys->b_use_pts = 1;
1151 if (p_sys->b_use_pts)
1152 msg_Dbg( p_dec, "using pts timestamp mode for %s", p_sys->psz_component);
1157 CloseGeneric(p_this);
1158 return VLC_EGENERIC;
1161 /*****************************************************************************
1163 *****************************************************************************/
1164 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
1166 decoder_sys_t *p_sys = p_dec->p_sys;
1167 OMX_PARAM_PORTDEFINITIONTYPE definition;
1168 OMX_BUFFERHEADERTYPE *p_buffer;
1169 OMX_ERRORTYPE omx_error;
1172 /* Sanity checking */
1173 OMX_INIT_STRUCTURE(definition);
1174 definition.nPortIndex = p_port->i_port_index;
1175 omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1177 if(omx_error != OMX_ErrorNone || (p_dec->fmt_in.i_cat == VIDEO_ES &&
1178 (!definition.format.video.nFrameWidth ||
1179 !definition.format.video.nFrameHeight)) )
1180 return OMX_ErrorUndefined;
1182 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
1183 p_port->i_port_index, NULL);
1184 CHECK_ERROR(omx_error, "OMX_CommandPortDisable on %i failed (%x)",
1185 (int)p_port->i_port_index, omx_error );
1187 for(i = 0; i < p_port->i_buffers; i++)
1189 OMX_FIFO_GET(&p_port->fifo, p_buffer);
1190 if (p_buffer->nFlags & SENTINEL_FLAG) {
1195 omx_error = OMX_FreeBuffer( p_sys->omx_handle,
1196 p_port->i_port_index, p_buffer );
1198 if(omx_error != OMX_ErrorNone) break;
1200 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
1201 omx_error, (int)p_port->i_port_index, i );
1203 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1204 CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
1206 /* Get the new port definition */
1207 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1208 if(omx_error != OMX_ErrorNone) goto error;
1210 if( p_dec->fmt_in.i_cat != AUDIO_ES )
1212 /* Don't explicitly set the new parameters that we got with
1213 * OMX_GetParameter above when using audio codecs.
1214 * That struct hasn't been changed since, so there should be
1215 * no need to set it here, unless some codec expects the
1216 * SetParameter call as a trigger event for some part of
1217 * the reconfiguration.
1218 * This fixes using audio decoders on Samsung Galaxy S II,
1220 * Only skipping this for audio codecs, to minimize the
1221 * change for current working configurations for video.
1223 omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1225 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
1226 omx_error, ErrorToString(omx_error));
1229 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
1230 p_port->i_port_index, NULL);
1231 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
1232 (int)p_port->i_port_index, omx_error );
1234 if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
1235 free(p_port->pp_buffers);
1236 p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
1237 if(!p_port->pp_buffers)
1239 omx_error = OMX_ErrorInsufficientResources;
1240 CHECK_ERROR(omx_error, "memory allocation failed");
1243 p_port->i_buffers = p_port->definition.nBufferCountActual;
1244 for(i = 0; i < p_port->i_buffers; i++)
1246 if(0 && p_port->b_direct)
1248 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1249 p_port->i_port_index, 0,
1250 p_port->definition.nBufferSize, (void*)1);
1253 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1254 p_port->i_port_index, 0,
1255 p_port->definition.nBufferSize);
1257 if(omx_error != OMX_ErrorNone) break;
1258 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1260 p_port->i_buffers = i;
1261 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1262 omx_error, (int)p_port->i_port_index, i );
1264 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1265 CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1267 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1268 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1274 /*****************************************************************************
1275 * DecodeVideo: Called to decode one frame
1276 *****************************************************************************/
1277 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1279 decoder_sys_t *p_sys = p_dec->p_sys;
1280 picture_t *p_pic = NULL, *p_next_pic;
1281 OMX_ERRORTYPE omx_error;
1284 OMX_BUFFERHEADERTYPE *p_header;
1287 if( !pp_block || !*pp_block )
1290 p_block = *pp_block;
1292 /* Check for errors from codec */
1295 msg_Dbg(p_dec, "error during decoding");
1296 block_Release( p_block );
1300 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1302 block_Release( p_block );
1303 if(!p_sys->in.b_flushed)
1305 msg_Dbg(p_dec, "flushing");
1306 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1307 p_sys->in.definition.nPortIndex, 0 );
1309 p_sys->in.b_flushed = true;
1313 /* Take care of decoded frames first */
1316 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1317 if(!p_header) break; /* No frame available */
1319 if(p_sys->out.b_update_def)
1321 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1322 p_sys->out.b_update_def = 0;
1325 if(p_header->nFilledLen)
1327 p_pic = p_header->pAppPrivate;
1330 /* We're not in direct rendering mode.
1331 * Get a new picture and copy the content */
1332 p_pic = decoder_NewPicture( p_dec );
1335 CopyOmxPicture(p_sys->out.definition.format.video.eColorFormat,
1336 p_pic, p_sys->out.definition.format.video.nSliceHeight,
1337 p_sys->out.i_frame_stride,
1338 p_header->pBuffer + p_header->nOffset,
1339 p_sys->out.i_frame_stride_chroma_div);
1343 p_pic->date = p_header->nTimeStamp;
1344 p_header->nFilledLen = 0;
1345 p_header->pAppPrivate = 0;
1348 /* Get a new picture */
1349 if(p_sys->in.b_direct && !p_header->pAppPrivate)
1351 p_next_pic = decoder_NewPicture( p_dec );
1352 if(!p_next_pic) break;
1354 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1355 p_header->pAppPrivate = p_next_pic;
1356 p_header->pInputPortPrivate = p_header->pBuffer;
1357 p_header->pBuffer = p_next_pic->p[0].p_pixels;
1361 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1364 #ifdef OMXIL_EXTRA_DEBUG
1365 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1367 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1370 /* Send the input buffer to the component */
1371 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1373 if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1380 p_header->nFilledLen = p_block->i_buffer;
1381 p_header->nOffset = 0;
1382 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1383 if (p_sys->b_use_pts && p_block->i_pts)
1384 p_header->nTimeStamp = p_block->i_pts;
1386 p_header->nTimeStamp = p_block->i_dts;
1388 /* In direct mode we pass the input pointer as is.
1389 * Otherwise we memcopy the data */
1390 if(p_sys->in.b_direct)
1392 p_header->pOutputPortPrivate = p_header->pBuffer;
1393 p_header->pBuffer = p_block->p_buffer;
1394 p_header->pAppPrivate = p_block;
1398 if(p_header->nFilledLen > p_header->nAllocLen)
1400 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1401 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1402 p_header->nFilledLen = p_header->nAllocLen;
1404 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1405 block_Release(p_block);
1408 /* Convert H.264 NAL format to annex b. Doesn't do anything if
1409 * i_nal_size_length == 0, which is the case for codecs other
1411 convert_h264_to_annexb( p_header->pBuffer, p_header->nFilledLen,
1412 p_sys->i_nal_size_length );
1413 #ifdef OMXIL_EXTRA_DEBUG
1414 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1415 (int)p_header->nFilledLen );
1417 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1418 p_sys->in.b_flushed = false;
1419 *pp_block = NULL; /* Avoid being fed the same packet again */
1423 /* Handle the PortSettingsChanged events */
1424 for(i = 0; i < p_sys->ports; i++)
1426 OmxPort *p_port = &p_sys->p_ports[i];
1427 if(p_port->b_reconfigure)
1429 omx_error = PortReconfigure(p_dec, p_port);
1430 p_port->b_reconfigure = 0;
1432 if(p_port->b_update_def)
1434 omx_error = GetPortDefinition(p_dec, p_port, p_port->p_fmt);
1435 p_port->b_update_def = 0;
1442 /*****************************************************************************
1443 * DecodeAudio: Called to decode one frame
1444 *****************************************************************************/
1445 block_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1447 decoder_sys_t *p_sys = p_dec->p_sys;
1448 block_t *p_buffer = NULL;
1449 OMX_BUFFERHEADERTYPE *p_header;
1450 OMX_ERRORTYPE omx_error;
1454 if( !pp_block || !*pp_block ) return NULL;
1456 p_block = *pp_block;
1458 /* Check for errors from codec */
1461 msg_Dbg(p_dec, "error during decoding");
1462 block_Release( p_block );
1466 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1468 block_Release( p_block );
1469 date_Set( &p_sys->end_date, 0 );
1470 if(!p_sys->in.b_flushed)
1472 msg_Dbg(p_dec, "flushing");
1473 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1474 p_sys->in.definition.nPortIndex, 0 );
1476 p_sys->in.b_flushed = true;
1480 if( !date_Get( &p_sys->end_date ) )
1482 if( !p_block->i_pts )
1484 /* We've just started the stream, wait for the first PTS. */
1485 block_Release( p_block );
1488 date_Set( &p_sys->end_date, p_block->i_pts );
1491 /* Take care of decoded frames first */
1494 unsigned int i_samples = 0;
1496 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1497 if(!p_header) break; /* No frame available */
1499 if (p_sys->out.p_fmt->audio.i_channels)
1500 i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1503 p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1504 if( !p_buffer ) break; /* No audio buffer available */
1506 memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1507 p_header->nFilledLen = 0;
1509 if( p_header->nTimeStamp != 0 &&
1510 p_header->nTimeStamp != date_Get( &p_sys->end_date ) )
1511 date_Set( &p_sys->end_date, p_header->nTimeStamp );
1513 p_buffer->i_pts = date_Get( &p_sys->end_date );
1514 p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1518 #ifdef OMXIL_EXTRA_DEBUG
1519 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1521 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1522 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1526 /* Send the input buffer to the component */
1527 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1529 if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1536 p_header->nFilledLen = p_block->i_buffer;
1537 p_header->nOffset = 0;
1538 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1539 p_header->nTimeStamp = p_block->i_dts;
1541 /* In direct mode we pass the input pointer as is.
1542 * Otherwise we memcopy the data */
1543 if(p_sys->in.b_direct)
1545 p_header->pOutputPortPrivate = p_header->pBuffer;
1546 p_header->pBuffer = p_block->p_buffer;
1547 p_header->pAppPrivate = p_block;
1551 if(p_header->nFilledLen > p_header->nAllocLen)
1553 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1554 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1555 p_header->nFilledLen = p_header->nAllocLen;
1557 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1558 block_Release(p_block);
1561 #ifdef OMXIL_EXTRA_DEBUG
1562 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1563 (int)p_header->nFilledLen );
1565 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1566 p_sys->in.b_flushed = false;
1567 *pp_block = NULL; /* Avoid being fed the same packet again */
1571 /* Handle the PortSettingsChanged events */
1572 for(i = 0; i < p_sys->ports; i++)
1574 OmxPort *p_port = &p_sys->p_ports[i];
1575 if(!p_port->b_reconfigure) continue;
1576 p_port->b_reconfigure = 0;
1577 omx_error = PortReconfigure(p_dec, p_port);
1583 /*****************************************************************************
1584 * EncodeVideo: Called to encode one frame
1585 *****************************************************************************/
1586 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1588 decoder_t *p_dec = ( decoder_t *)p_enc;
1589 decoder_sys_t *p_sys = p_dec->p_sys;
1590 OMX_ERRORTYPE omx_error;
1593 OMX_BUFFERHEADERTYPE *p_header;
1594 block_t *p_block = 0;
1596 if( !p_pic ) return NULL;
1598 /* Check for errors from codec */
1601 msg_Dbg(p_dec, "error during encoding");
1605 /* Send the input buffer to the component */
1606 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1609 /* In direct mode we pass the input pointer as is.
1610 * Otherwise we memcopy the data */
1611 if(p_sys->in.b_direct)
1613 p_header->pOutputPortPrivate = p_header->pBuffer;
1614 p_header->pBuffer = p_pic->p[0].p_pixels;
1618 CopyVlcPicture(p_dec, p_header, p_pic);
1621 p_header->nFilledLen = p_sys->in.i_frame_size;
1622 p_header->nOffset = 0;
1623 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1624 p_header->nTimeStamp = p_pic->date;
1625 #ifdef OMXIL_EXTRA_DEBUG
1626 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1627 (int)p_header->nFilledLen );
1629 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1630 p_sys->in.b_flushed = false;
1633 /* Handle the PortSettingsChanged events */
1634 for(i = 0; i < p_sys->ports; i++)
1636 OmxPort *p_port = &p_sys->p_ports[i];
1637 if(!p_port->b_reconfigure) continue;
1638 p_port->b_reconfigure = 0;
1639 omx_error = PortReconfigure(p_dec, p_port);
1642 /* Wait for the decoded frame */
1645 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1647 if(p_header->nFilledLen)
1649 if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1651 /* TODO: need to store codec config */
1652 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1655 p_block = p_header->pAppPrivate;
1658 /* We're not in direct rendering mode.
1659 * Get a new block and copy the content */
1660 p_block = block_Alloc( p_header->nFilledLen );
1661 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1664 p_block->i_buffer = p_header->nFilledLen;
1665 p_block->i_pts = p_block->i_dts = p_header->nTimeStamp;
1666 p_header->nFilledLen = 0;
1667 p_header->pAppPrivate = 0;
1670 #ifdef OMXIL_EXTRA_DEBUG
1671 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1673 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1676 msg_Dbg(p_dec, "done");
1680 /*****************************************************************************
1681 * CloseGeneric: omxil decoder destruction
1682 *****************************************************************************/
1683 static void CloseGeneric( vlc_object_t *p_this )
1685 decoder_t *p_dec = (decoder_t *)p_this;
1686 decoder_sys_t *p_sys = p_dec->p_sys;
1688 if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1689 vlc_mutex_lock( &omx_core_mutex );
1691 if( omx_refcount == 0 )
1693 if( p_sys->b_init ) pf_deinit();
1694 dll_close( dll_handle );
1696 vlc_mutex_unlock( &omx_core_mutex );
1698 vlc_mutex_destroy (&p_sys->mutex);
1699 vlc_cond_destroy (&p_sys->cond);
1700 vlc_mutex_destroy (&p_sys->in.fifo.lock);
1701 vlc_cond_destroy (&p_sys->in.fifo.wait);
1702 vlc_mutex_destroy (&p_sys->out.fifo.lock);
1703 vlc_cond_destroy (&p_sys->out.fifo.wait);
1708 /*****************************************************************************
1710 *****************************************************************************/
1711 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1712 OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1713 OMX_U32 data_2, OMX_PTR event_data )
1715 decoder_t *p_dec = (decoder_t *)app_data;
1716 decoder_sys_t *p_sys = p_dec->p_sys;
1722 case OMX_EventCmdComplete:
1723 switch ((OMX_STATETYPE)data_1)
1725 case OMX_CommandStateSet:
1726 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %s)", EventToString(event),
1727 CommandToString(data_1), StateToString(data_2) );
1731 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u)", EventToString(event),
1732 CommandToString(data_1), (unsigned int)data_2 );
1737 case OMX_EventError:
1738 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u, %s)", EventToString(event),
1739 ErrorToString((OMX_ERRORTYPE)data_1), (unsigned int)data_2,
1740 (const char *)event_data);
1741 //p_sys->b_error = true;
1744 case OMX_EventPortSettingsChanged:
1745 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1746 (unsigned int)data_1, (unsigned int)data_2 );
1747 if( data_2 == 0 || data_2 == OMX_IndexParamPortDefinition )
1749 OMX_BUFFERHEADERTYPE *sentinel;
1750 for(i = 0; i < p_sys->ports; i++)
1751 if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1752 p_sys->p_ports[i].b_reconfigure = true;
1753 sentinel = calloc(1, sizeof(*sentinel));
1755 sentinel->nFlags = SENTINEL_FLAG;
1756 OMX_FIFO_PUT(&p_sys->in.fifo, sentinel);
1759 else if( data_2 == OMX_IndexConfigCommonOutputCrop )
1761 for(i = 0; i < p_sys->ports; i++)
1762 if(p_sys->p_ports[i].definition.nPortIndex == data_1)
1763 p_sys->p_ports[i].b_update_def = true;
1767 msg_Dbg( p_dec, "Unhandled setting change %x", (unsigned int)data_2 );
1772 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1773 (unsigned int)data_1, (unsigned int)data_2 );
1777 PostOmxEvent(p_dec, event, data_1, data_2, event_data);
1778 return OMX_ErrorNone;
1781 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1782 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1784 decoder_t *p_dec = (decoder_t *)app_data;
1785 decoder_sys_t *p_sys = p_dec->p_sys;
1788 #ifdef OMXIL_EXTRA_DEBUG
1789 msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1792 if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1794 block_t *p_block = (block_t *)omx_header->pAppPrivate;
1795 omx_header->pBuffer = omx_header->pOutputPortPrivate;
1796 if(p_block) block_Release(p_block);
1797 omx_header->pAppPrivate = 0;
1799 OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1801 return OMX_ErrorNone;
1804 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1805 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1807 decoder_t *p_dec = (decoder_t *)app_data;
1808 decoder_sys_t *p_sys = p_dec->p_sys;
1811 #ifdef OMXIL_EXTRA_DEBUG
1812 msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
1813 (int)omx_header->nFilledLen );
1816 if(omx_header->pInputPortPrivate)
1818 omx_header->pBuffer = omx_header->pInputPortPrivate;
1820 OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1822 return OMX_ErrorNone;