1 /*****************************************************************************
2 * omxil.c: Video decoder module making use of OpenMAX IL components.
3 *****************************************************************************
4 * Copyright (C) 2010 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 /* On dll_open, just check that the OMX_Init symbol already is loaded */
34 # define dll_open(name) dlsym(RTLD_DEFAULT, "OMX_Init")
35 # define dll_close(handle) do { } while (0)
36 # define dlsym(handle, name) dlsym(RTLD_DEFAULT, "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(!strncmp((char *)ppsz_roles[j], psz_role, len)) 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 CHECK_ERROR(omx_error, "SetAudioParameters failed (%x : %s)",
422 omx_error, ErrorToString(omx_error));
424 if (!strcmp(p_dec->p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER") &&
425 def->eDir == OMX_DirOutput)
427 /* When setting the output buffer size above, the decoder actually
428 * sets the buffer size to a lower value than what was chosen. If
429 * we try to allocate buffers of this size, it fails. Thus, forcibly
430 * use a larger buffer size. */
431 def->nBufferSize *= 2;
438 /*****************************************************************************
439 * GetPortDefinition: set vlc format based on the definition of the omx port
440 *****************************************************************************/
441 static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
444 decoder_sys_t *p_sys = p_dec->p_sys;
445 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
446 OMX_ERRORTYPE omx_error;
447 OMX_CONFIG_RECTTYPE crop_rect;
449 omx_error = OMX_GetParameter(p_port->omx_handle,
450 OMX_IndexParamPortDefinition, def);
451 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
452 omx_error, ErrorToString(omx_error));
457 p_fmt->video.i_width = def->format.video.nFrameWidth;
458 p_fmt->video.i_visible_width = def->format.video.nFrameWidth;
459 p_fmt->video.i_height = def->format.video.nFrameHeight;
460 p_fmt->video.i_visible_height = def->format.video.nFrameHeight;
461 p_fmt->video.i_frame_rate = p_dec->fmt_in.video.i_frame_rate;
462 p_fmt->video.i_frame_rate_base = p_dec->fmt_in.video.i_frame_rate_base;
464 OMX_INIT_STRUCTURE(crop_rect);
465 crop_rect.nPortIndex = def->nPortIndex;
466 omx_error = OMX_GetConfig(p_port->omx_handle, OMX_IndexConfigCommonOutputCrop, &crop_rect);
467 if (omx_error == OMX_ErrorNone)
469 if (!def->format.video.nSliceHeight)
470 def->format.video.nSliceHeight = def->format.video.nFrameHeight;
471 if (!def->format.video.nStride)
472 def->format.video.nStride = def->format.video.nFrameWidth;
473 p_fmt->video.i_width = crop_rect.nWidth;
474 p_fmt->video.i_visible_width = crop_rect.nWidth;
475 p_fmt->video.i_height = crop_rect.nHeight;
476 p_fmt->video.i_visible_height = crop_rect.nHeight;
477 if (def->format.video.eColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
478 def->format.video.nSliceHeight -= crop_rect.nTop/2;
482 /* Don't pass the error back to the caller, this isn't mandatory */
483 omx_error = OMX_ErrorNone;
486 /* Hack: Nexus One (stock firmware with binary OMX driver blob)
487 * claims to output 420Planar even though it in in practice is
489 if(def->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar &&
490 !strncmp(p_sys->psz_component, "OMX.qcom.video.decoder",
491 strlen("OMX.qcom.video.decoder")))
492 def->format.video.eColorFormat = OMX_QCOM_COLOR_FormatYVU420SemiPlanar;
494 /* Hack: Galaxy S II (stock firmware) gives a slice height larger
495 * than the video height, but this doesn't imply padding between
496 * the video planes. Nexus S also has a slice height larger than
497 * the video height, but there it actually is real padding, thus
498 * Galaxy S II is the buggy one. The Galaxy S II decoder is
499 * named OMX.SEC.avcdec while the one on Nexus S is
500 * OMX.SEC.AVC.Decoder. Thus do this for any OMX.SEC. that don't
501 * contain the string ".Decoder". */
502 if(!strncmp(p_sys->psz_component, "OMX.SEC.", strlen("OMX.SEC.")) &&
503 !strstr(p_sys->psz_component, ".Decoder"))
504 def->format.video.nSliceHeight = 0;
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 );
608 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
609 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
612 omx_error = OMX_GetState(omx_handle, &state);
613 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
615 if(state == OMX_StateIdle)
617 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
618 OMX_StateLoaded, 0 );
619 CHECK_ERROR(omx_error, "OMX_CommandStateSet Loaded failed (%x)", omx_error );
621 for(i = 0; i < p_sys->ports; i++)
623 OmxPort *p_port = &p_sys->p_ports[i];
624 OMX_BUFFERHEADERTYPE *p_buffer;
626 for(j = 0; j < p_port->i_buffers; j++)
628 OMX_FIFO_GET(&p_port->fifo, p_buffer);
629 if (p_buffer->nFlags & SENTINEL_FLAG) {
634 omx_error = OMX_FreeBuffer( omx_handle,
635 p_port->i_port_index, p_buffer );
637 if(omx_error != OMX_ErrorNone) break;
639 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
640 omx_error, (int)p_port->i_port_index, j );
642 OMX_FIFO_PEEK(&p_port->fifo, p_buffer);
643 if (!p_buffer) break;
645 OMX_FIFO_GET(&p_port->fifo, p_buffer);
646 if (p_buffer->nFlags & SENTINEL_FLAG) {
650 msg_Warn( p_dec, "Stray buffer left in fifo, %p", p_buffer );
654 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
655 CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
659 for(i = 0; i < p_sys->ports; i++)
661 OmxPort *p_port = &p_sys->p_ports[i];
662 free(p_port->pp_buffers);
663 p_port->pp_buffers = 0;
665 omx_error = pf_free_handle( omx_handle );
669 /*****************************************************************************
670 * InitialiseComponent: Load and initialise an OMX component
671 *****************************************************************************/
672 static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
673 OMX_STRING psz_component, OMX_HANDLETYPE *p_handle)
675 static OMX_CALLBACKTYPE callbacks =
676 { OmxEventHandler, OmxEmptyBufferDone, OmxFillBufferDone };
677 decoder_sys_t *p_sys = p_dec->p_sys;
678 OMX_HANDLETYPE omx_handle;
679 OMX_ERRORTYPE omx_error;
681 OMX_U8 psz_role[OMX_MAX_STRINGNAME_SIZE];
682 OMX_PARAM_COMPONENTROLETYPE role;
683 OMX_PARAM_PORTDEFINITIONTYPE definition;
684 OMX_PORT_PARAM_TYPE param;
687 omx_error = pf_get_handle( &omx_handle, psz_component, p_dec, &callbacks );
688 if(omx_error != OMX_ErrorNone)
690 msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
691 omx_error, ErrorToString(omx_error) );
694 strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
696 omx_error = OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
697 if(omx_error == OMX_ErrorNone)
698 msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
700 msg_Dbg(p_dec, "loaded component %s", psz_component);
701 PrintOmx(p_dec, omx_handle, OMX_ALL);
703 /* Set component role */
704 OMX_INIT_STRUCTURE(role);
705 strcpy((char*)role.cRole,
706 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec : p_dec->fmt_in.i_codec,
707 p_dec->fmt_in.i_cat, p_sys->b_enc));
709 omx_error = OMX_SetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
711 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
713 if(omx_error == OMX_ErrorNone)
714 msg_Dbg(p_dec, "component standard role set to %s", role.cRole);
716 /* Find the input / output ports */
717 OMX_INIT_STRUCTURE(param);
718 OMX_INIT_STRUCTURE(definition);
719 omx_error = OMX_GetParameter(omx_handle, p_dec->fmt_in.i_cat == VIDEO_ES ?
720 OMX_IndexParamVideoInit : OMX_IndexParamAudioInit, ¶m);
721 if(omx_error != OMX_ErrorNone) param.nPorts = 0;
723 for(i = 0; i < param.nPorts; i++)
727 /* Get port definition */
728 definition.nPortIndex = param.nStartPortNumber + i;
729 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
731 if(omx_error != OMX_ErrorNone) continue;
733 if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
734 else p_port = &p_sys->out;
736 p_port->b_valid = true;
737 p_port->i_port_index = definition.nPortIndex;
738 p_port->definition = definition;
739 p_port->omx_handle = omx_handle;
742 if(!p_sys->in.b_valid || !p_sys->out.b_valid)
744 omx_error = OMX_ErrorInvalidComponent;
745 CHECK_ERROR(omx_error, "couldn't find an input and output port");
748 if(!strncmp(p_sys->psz_component, "OMX.SEC.", 8))
751 omx_error = OMX_GetExtensionIndex(omx_handle, (OMX_STRING) "OMX.SEC.index.ThumbnailMode", &index);
752 if(omx_error == OMX_ErrorNone)
754 OMX_BOOL enable = OMX_TRUE;
755 omx_error = OMX_SetConfig(omx_handle, index, &enable);
756 CHECK_ERROR(omx_error, "Unable to set ThumbnailMode");
758 OMX_BOOL enable = OMX_TRUE;
759 /* Needed on Samsung Galaxy S II */
760 omx_error = OMX_SetConfig(omx_handle, OMX_IndexVendorSetYUV420pMode, &enable);
761 if (omx_error == OMX_ErrorNone)
762 msg_Dbg(p_dec, "Set OMX_IndexVendorSetYUV420pMode successfully");
764 msg_Dbg(p_dec, "Unable to set OMX_IndexVendorSetYUV420pMode: %x", omx_error);
768 /* Set port definitions */
769 for(i = 0; i < p_sys->ports; i++)
771 omx_error = SetPortDefinition(p_dec, &p_sys->p_ports[i],
772 p_sys->p_ports[i].p_fmt);
773 if(omx_error != OMX_ErrorNone) goto error;
776 /* Allocate our array for the omx buffers and enable ports */
777 for(i = 0; i < p_sys->ports; i++)
779 OmxPort *p_port = &p_sys->p_ports[i];
782 malloc(p_port->definition.nBufferCountActual *
783 sizeof(OMX_BUFFERHEADERTYPE*));
784 if(!p_port->pp_buffers)
786 omx_error = OMX_ErrorInsufficientResources;
787 CHECK_ERROR(omx_error, "memory allocation failed");
789 p_port->i_buffers = p_port->definition.nBufferCountActual;
792 if(!p_port->definition.bEnabled)
794 omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
795 p_port->i_port_index, NULL);
796 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
797 (int)p_port->i_port_index, omx_error );
798 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
799 CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
800 (int)p_port->i_port_index, omx_error );
804 *p_handle = omx_handle;
805 return OMX_ErrorNone;
808 DeinitialiseComponent(p_dec, omx_handle);
813 /*****************************************************************************
814 * OpenDecoder: Create the decoder instance
815 *****************************************************************************/
816 static int OpenDecoder( vlc_object_t *p_this )
818 decoder_t *p_dec = (decoder_t*)p_this;
821 if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
825 if( p_dec->fmt_in.i_cat != VIDEO_ES && !p_dec->b_force)
829 status = OpenGeneric( p_this, false );
830 if(status != VLC_SUCCESS) return status;
832 p_dec->pf_decode_video = DecodeVideo;
833 p_dec->pf_decode_audio = DecodeAudio;
838 /*****************************************************************************
839 * OpenEncoder: Create the encoder instance
840 *****************************************************************************/
841 static int OpenEncoder( vlc_object_t *p_this )
843 encoder_t *p_enc = (encoder_t*)p_this;
846 if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
849 status = OpenGeneric( p_this, true );
850 if(status != VLC_SUCCESS) return status;
852 p_enc->pf_encode_video = EncodeVideo;
857 /*****************************************************************************
858 * OpenGeneric: Create the generic decoder/encoder instance
859 *****************************************************************************/
860 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
862 decoder_t *p_dec = (decoder_t*)p_this;
863 decoder_sys_t *p_sys;
864 OMX_ERRORTYPE omx_error;
865 OMX_BUFFERHEADERTYPE *p_header;
868 vlc_mutex_lock( &omx_core_mutex );
869 if( omx_refcount > 0 )
872 /* Load the OMX core */
873 for( i = 0; ppsz_dll_list[i]; i++ )
875 dll_handle = dll_open( ppsz_dll_list[i] );
876 if( dll_handle ) break;
880 vlc_mutex_unlock( &omx_core_mutex );
884 pf_init = dlsym( dll_handle, "OMX_Init" );
885 pf_deinit = dlsym( dll_handle, "OMX_Deinit" );
886 pf_get_handle = dlsym( dll_handle, "OMX_GetHandle" );
887 pf_free_handle = dlsym( dll_handle, "OMX_FreeHandle" );
888 pf_component_enum = dlsym( dll_handle, "OMX_ComponentNameEnum" );
889 pf_get_roles_of_component = dlsym( dll_handle, "OMX_GetRolesOfComponent" );
890 if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle ||
891 !pf_component_enum || !pf_get_roles_of_component )
893 msg_Warn( p_this, "cannot find OMX_* symbols in `%s' (%s)",
894 ppsz_dll_list[i], dlerror() );
895 dll_close(dll_handle);
896 vlc_mutex_unlock( &omx_core_mutex );
901 /* Allocate the memory needed to store the decoder's structure */
902 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
904 if( omx_refcount == 0 )
905 dll_close(dll_handle);
906 vlc_mutex_unlock( &omx_core_mutex );
910 /* Initialise the thread properties */
913 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
914 p_dec->fmt_out.video = p_dec->fmt_in.video;
915 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
916 p_dec->fmt_out.i_codec = 0;
918 p_sys->b_enc = b_encode;
919 p_sys->pp_last_event = &p_sys->p_events;
920 vlc_mutex_init (&p_sys->mutex);
921 vlc_cond_init (&p_sys->cond);
922 vlc_mutex_init (&p_sys->lock);
923 vlc_mutex_init (&p_sys->in.fifo.lock);
924 vlc_cond_init (&p_sys->in.fifo.wait);
925 p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
926 p_sys->in.fifo.pp_last = &p_sys->in.fifo.p_first;
927 p_sys->in.b_direct = false;
928 p_sys->in.b_flushed = true;
929 p_sys->in.p_fmt = &p_dec->fmt_in;
930 vlc_mutex_init (&p_sys->out.fifo.lock);
931 vlc_cond_init (&p_sys->out.fifo.wait);
932 p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
933 p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
934 p_sys->out.b_direct = true;
935 p_sys->out.b_flushed = true;
936 p_sys->out.p_fmt = &p_dec->fmt_out;
938 p_sys->p_ports = &p_sys->in;
939 p_sys->b_use_pts = 0;
941 msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
942 (char *)&p_dec->fmt_out.i_codec);
944 /* Initialise the OMX core */
945 omx_error = omx_refcount > 0 ? OMX_ErrorNone : pf_init();
947 if(omx_error != OMX_ErrorNone)
949 msg_Warn( p_this, "OMX_Init failed (%x: %s)", omx_error,
950 ErrorToString(omx_error) );
951 vlc_mutex_unlock( &omx_core_mutex );
952 CloseGeneric(p_this);
955 p_sys->b_init = true;
956 vlc_mutex_unlock( &omx_core_mutex );
958 /* Enumerate components and build a list of the one we want to try */
959 if( !CreateComponentsList(p_dec,
960 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
961 p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
964 msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
965 (char *)&p_dec->fmt_in.i_codec );
966 CloseGeneric(p_this);
970 /* Try to load and initialise a component */
971 omx_error = OMX_ErrorUndefined;
972 for(i = 0; i < p_sys->components; i++)
975 /* ignore OpenCore software codecs */
976 if (!strncmp(p_sys->ppsz_components[i], "OMX.PV.", 7))
978 /* The same sw codecs, renamed in ICS (perhaps also in honeycomb) */
979 if (!strncmp(p_sys->ppsz_components[i], "OMX.google.", 11))
981 /* This one has been seen on HTC One V - it behaves like it works,
982 * but FillBufferDone returns buffers filled with 0 bytes. The One V
983 * has got a working OMX.qcom.video.decoder.avc instead though. */
984 if (!strncmp(p_sys->ppsz_components[i], "OMX.ARICENT.", 12))
986 /* Some nVidia codec with DRM */
987 if (!strncmp(p_sys->ppsz_components[i], "OMX.Nvidia.h264.decode.secure", 29))
990 omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
992 if(omx_error == OMX_ErrorNone) break;
994 CHECK_ERROR(omx_error, "no component could be initialised" );
996 /* Move component to Idle then Executing state */
997 OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
998 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
1000 /* Allocate omx buffers */
1001 for(i = 0; i < p_sys->ports; i++)
1003 OmxPort *p_port = &p_sys->p_ports[i];
1005 for(j = 0; j < p_port->i_buffers; j++)
1008 #define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
1009 char *p_buf = malloc(p_port->definition.nBufferSize +
1010 p_port->definition.nBufferAlignment);
1011 p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
1014 if(0 && p_port->b_direct)
1016 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
1017 p_port->i_port_index, 0,
1018 p_port->definition.nBufferSize, (void*)1);
1021 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
1022 p_port->i_port_index, 0,
1023 p_port->definition.nBufferSize);
1025 if(omx_error != OMX_ErrorNone) break;
1026 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
1028 p_port->i_buffers = j;
1029 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1030 omx_error, (int)p_port->i_port_index, j );
1033 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1034 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
1036 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
1037 OMX_StateExecuting, 0);
1038 CHECK_ERROR(omx_error, "OMX_CommandStateSet Executing failed (%x)", omx_error );
1039 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1040 CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
1042 /* Send codec configuration data */
1043 if( p_dec->fmt_in.i_extra )
1045 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1046 p_header->nFilledLen = p_dec->fmt_in.i_extra;
1048 /* Convert H.264 NAL format to annex b */
1049 if( p_sys->i_nal_size_length && !p_sys->in.b_direct )
1051 p_header->nFilledLen = 0;
1052 convert_sps_pps( p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
1053 p_header->pBuffer, p_header->nAllocLen,
1054 (uint32_t*) &p_header->nFilledLen, NULL );
1056 else if(p_sys->in.b_direct)
1058 p_header->pOutputPortPrivate = p_header->pBuffer;
1059 p_header->pBuffer = p_dec->fmt_in.p_extra;
1063 if(p_header->nFilledLen > p_header->nAllocLen)
1065 msg_Dbg(p_dec, "buffer too small (%i,%i)", (int)p_header->nFilledLen,
1066 (int)p_header->nAllocLen);
1067 p_header->nFilledLen = p_header->nAllocLen;
1069 memcpy(p_header->pBuffer, p_dec->fmt_in.p_extra, p_header->nFilledLen);
1072 p_header->nOffset = 0;
1073 p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG;
1074 msg_Dbg(p_dec, "sending codec config data %p, %p, %i", p_header,
1075 p_header->pBuffer, (int)p_header->nFilledLen);
1076 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1079 /* Get back output port definition */
1080 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1081 if(omx_error != OMX_ErrorNone) goto error;
1083 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1084 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1086 if(p_sys->b_error) goto error;
1088 p_dec->b_need_packetized = true;
1089 if (!strcmp(p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER"))
1090 p_sys->b_use_pts = 1;
1094 CloseGeneric(p_this);
1095 return VLC_EGENERIC;
1098 /*****************************************************************************
1100 *****************************************************************************/
1101 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
1103 decoder_sys_t *p_sys = p_dec->p_sys;
1104 OMX_PARAM_PORTDEFINITIONTYPE definition;
1105 OMX_BUFFERHEADERTYPE *p_buffer;
1106 OMX_ERRORTYPE omx_error;
1109 /* Sanity checking */
1110 OMX_INIT_STRUCTURE(definition);
1111 definition.nPortIndex = p_port->i_port_index;
1112 omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1114 if(omx_error != OMX_ErrorNone || (p_dec->fmt_in.i_cat == VIDEO_ES &&
1115 (!definition.format.video.nFrameWidth ||
1116 !definition.format.video.nFrameHeight)) )
1117 return OMX_ErrorUndefined;
1119 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
1120 p_port->i_port_index, NULL);
1121 CHECK_ERROR(omx_error, "OMX_CommandPortDisable on %i failed (%x)",
1122 (int)p_port->i_port_index, omx_error );
1124 for(i = 0; i < p_port->i_buffers; i++)
1126 OMX_FIFO_GET(&p_port->fifo, p_buffer);
1127 if (p_buffer->nFlags & SENTINEL_FLAG) {
1132 omx_error = OMX_FreeBuffer( p_sys->omx_handle,
1133 p_port->i_port_index, p_buffer );
1135 if(omx_error != OMX_ErrorNone) break;
1137 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
1138 omx_error, (int)p_port->i_port_index, i );
1140 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1141 CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
1143 /* Get the new port definition */
1144 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1145 if(omx_error != OMX_ErrorNone) goto error;
1147 if( p_dec->fmt_in.i_cat != AUDIO_ES )
1149 /* Don't explicitly set the new parameters that we got with
1150 * OMX_GetParameter above when using audio codecs.
1151 * That struct hasn't been changed since, so there should be
1152 * no need to set it here, unless some codec expects the
1153 * SetParameter call as a trigger event for some part of
1154 * the reconfiguration.
1155 * This fixes using audio decoders on Samsung Galaxy S II,
1157 * Only skipping this for audio codecs, to minimize the
1158 * change for current working configurations for video.
1160 omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1162 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
1163 omx_error, ErrorToString(omx_error));
1166 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
1167 p_port->i_port_index, NULL);
1168 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
1169 (int)p_port->i_port_index, omx_error );
1171 if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
1172 free(p_port->pp_buffers);
1173 p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
1174 if(!p_port->pp_buffers)
1176 omx_error = OMX_ErrorInsufficientResources;
1177 CHECK_ERROR(omx_error, "memory allocation failed");
1180 p_port->i_buffers = p_port->definition.nBufferCountActual;
1181 for(i = 0; i < p_port->i_buffers; i++)
1183 if(0 && p_port->b_direct)
1185 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1186 p_port->i_port_index, 0,
1187 p_port->definition.nBufferSize, (void*)1);
1190 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1191 p_port->i_port_index, 0,
1192 p_port->definition.nBufferSize);
1194 if(omx_error != OMX_ErrorNone) break;
1195 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1197 p_port->i_buffers = i;
1198 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1199 omx_error, (int)p_port->i_port_index, i );
1201 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1202 CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1204 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1205 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1211 /*****************************************************************************
1212 * DecodeVideo: Called to decode one frame
1213 *****************************************************************************/
1214 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1216 decoder_sys_t *p_sys = p_dec->p_sys;
1217 picture_t *p_pic = NULL, *p_next_pic;
1218 OMX_ERRORTYPE omx_error;
1221 OMX_BUFFERHEADERTYPE *p_header;
1224 if( !pp_block || !*pp_block )
1227 p_block = *pp_block;
1229 /* Check for errors from codec */
1232 msg_Dbg(p_dec, "error during decoding");
1233 block_Release( p_block );
1237 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1239 block_Release( p_block );
1240 if(!p_sys->in.b_flushed)
1242 msg_Dbg(p_dec, "flushing");
1243 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1244 p_sys->in.definition.nPortIndex, 0 );
1246 p_sys->in.b_flushed = true;
1250 /* Take care of decoded frames first */
1253 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1254 if(!p_header) break; /* No frame available */
1256 if(p_sys->out.b_update_def)
1258 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1259 p_sys->out.b_update_def = 0;
1262 if(p_header->nFilledLen)
1264 p_pic = p_header->pAppPrivate;
1267 /* We're not in direct rendering mode.
1268 * Get a new picture and copy the content */
1269 p_pic = decoder_NewPicture( p_dec );
1272 CopyOmxPicture(p_dec, p_pic, p_header, p_sys->out.definition.format.video.nSliceHeight);
1276 p_pic->date = p_header->nTimeStamp;
1277 p_header->nFilledLen = 0;
1278 p_header->pAppPrivate = 0;
1281 /* Get a new picture */
1282 if(p_sys->in.b_direct && !p_header->pAppPrivate)
1284 p_next_pic = decoder_NewPicture( p_dec );
1285 if(!p_next_pic) break;
1287 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1288 p_header->pAppPrivate = p_next_pic;
1289 p_header->pInputPortPrivate = p_header->pBuffer;
1290 p_header->pBuffer = p_next_pic->p[0].p_pixels;
1294 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1297 #ifdef OMXIL_EXTRA_DEBUG
1298 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1300 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1303 /* Send the input buffer to the component */
1304 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1306 if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1313 p_header->nFilledLen = p_block->i_buffer;
1314 p_header->nOffset = 0;
1315 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1316 if (p_sys->b_use_pts)
1317 p_header->nTimeStamp = p_block->i_pts;
1319 p_header->nTimeStamp = p_block->i_dts;
1321 /* In direct mode we pass the input pointer as is.
1322 * Otherwise we memcopy the data */
1323 if(p_sys->in.b_direct)
1325 p_header->pOutputPortPrivate = p_header->pBuffer;
1326 p_header->pBuffer = p_block->p_buffer;
1327 p_header->pAppPrivate = p_block;
1331 if(p_header->nFilledLen > p_header->nAllocLen)
1333 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1334 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1335 p_header->nFilledLen = p_header->nAllocLen;
1337 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1338 block_Release(p_block);
1341 /* Convert H.264 NAL format to annex b */
1342 if( p_sys->i_nal_size_length >= 3 && p_sys->i_nal_size_length <= 4 )
1344 /* This only works for NAL sizes 3-4 */
1345 int i_len = p_header->nFilledLen, i;
1346 uint8_t* ptr = p_header->pBuffer;
1347 while( i_len >= p_sys->i_nal_size_length )
1349 uint32_t nal_len = 0;
1350 for( i = 0; i < p_sys->i_nal_size_length; i++ ) {
1351 nal_len = (nal_len << 8) | ptr[i];
1354 ptr[p_sys->i_nal_size_length - 1] = 1;
1355 if( nal_len > INT_MAX || nal_len > (unsigned int) i_len )
1357 ptr += nal_len + p_sys->i_nal_size_length;
1358 i_len -= nal_len + p_sys->i_nal_size_length;
1361 #ifdef OMXIL_EXTRA_DEBUG
1362 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1363 (int)p_header->nFilledLen );
1365 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1366 p_sys->in.b_flushed = false;
1367 *pp_block = NULL; /* Avoid being fed the same packet again */
1371 /* Handle the PortSettingsChanged events */
1372 for(i = 0; i < p_sys->ports; i++)
1374 OmxPort *p_port = &p_sys->p_ports[i];
1375 if(p_port->b_reconfigure)
1377 omx_error = PortReconfigure(p_dec, p_port);
1378 p_port->b_reconfigure = 0;
1380 if(p_port->b_update_def)
1382 omx_error = GetPortDefinition(p_dec, p_port, p_port->p_fmt);
1383 p_port->b_update_def = 0;
1390 /*****************************************************************************
1391 * DecodeAudio: Called to decode one frame
1392 *****************************************************************************/
1393 block_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1395 decoder_sys_t *p_sys = p_dec->p_sys;
1396 block_t *p_buffer = NULL;
1397 OMX_BUFFERHEADERTYPE *p_header;
1398 OMX_ERRORTYPE omx_error;
1402 if( !pp_block || !*pp_block ) return NULL;
1404 p_block = *pp_block;
1406 /* Check for errors from codec */
1409 msg_Dbg(p_dec, "error during decoding");
1410 block_Release( p_block );
1414 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1416 block_Release( p_block );
1417 date_Set( &p_sys->end_date, 0 );
1418 if(!p_sys->in.b_flushed)
1420 msg_Dbg(p_dec, "flushing");
1421 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1422 p_sys->in.definition.nPortIndex, 0 );
1424 p_sys->in.b_flushed = true;
1428 if( !date_Get( &p_sys->end_date ) )
1430 if( !p_block->i_pts )
1432 /* We've just started the stream, wait for the first PTS. */
1433 block_Release( p_block );
1436 date_Set( &p_sys->end_date, p_block->i_pts );
1439 /* Take care of decoded frames first */
1442 unsigned int i_samples;
1444 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1445 if(!p_header) break; /* No frame available */
1447 i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1450 p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1451 if( !p_buffer ) break; /* No audio buffer available */
1453 memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1454 p_header->nFilledLen = 0;
1456 if( p_header->nTimeStamp != 0 &&
1457 p_header->nTimeStamp != date_Get( &p_sys->end_date ) )
1458 date_Set( &p_sys->end_date, p_header->nTimeStamp );
1460 p_buffer->i_pts = date_Get( &p_sys->end_date );
1461 p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1465 #ifdef OMXIL_EXTRA_DEBUG
1466 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1468 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1469 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1473 /* Send the input buffer to the component */
1474 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1476 if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1483 p_header->nFilledLen = p_block->i_buffer;
1484 p_header->nOffset = 0;
1485 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1486 p_header->nTimeStamp = p_block->i_dts;
1488 /* In direct mode we pass the input pointer as is.
1489 * Otherwise we memcopy the data */
1490 if(p_sys->in.b_direct)
1492 p_header->pOutputPortPrivate = p_header->pBuffer;
1493 p_header->pBuffer = p_block->p_buffer;
1494 p_header->pAppPrivate = p_block;
1498 if(p_header->nFilledLen > p_header->nAllocLen)
1500 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1501 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1502 p_header->nFilledLen = p_header->nAllocLen;
1504 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1505 block_Release(p_block);
1508 #ifdef OMXIL_EXTRA_DEBUG
1509 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1510 (int)p_header->nFilledLen );
1512 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1513 p_sys->in.b_flushed = false;
1514 *pp_block = NULL; /* Avoid being fed the same packet again */
1518 /* Handle the PortSettingsChanged events */
1519 for(i = 0; i < p_sys->ports; i++)
1521 OmxPort *p_port = &p_sys->p_ports[i];
1522 if(!p_port->b_reconfigure) continue;
1523 p_port->b_reconfigure = 0;
1524 omx_error = PortReconfigure(p_dec, p_port);
1530 /*****************************************************************************
1531 * EncodeVideo: Called to encode one frame
1532 *****************************************************************************/
1533 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1535 decoder_t *p_dec = ( decoder_t *)p_enc;
1536 decoder_sys_t *p_sys = p_dec->p_sys;
1537 OMX_ERRORTYPE omx_error;
1540 OMX_BUFFERHEADERTYPE *p_header;
1541 block_t *p_block = 0;
1543 if( !p_pic ) return NULL;
1545 /* Check for errors from codec */
1548 msg_Dbg(p_dec, "error during encoding");
1552 /* Send the input buffer to the component */
1553 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1556 /* In direct mode we pass the input pointer as is.
1557 * Otherwise we memcopy the data */
1558 if(p_sys->in.b_direct)
1560 p_header->pOutputPortPrivate = p_header->pBuffer;
1561 p_header->pBuffer = p_pic->p[0].p_pixels;
1565 CopyVlcPicture(p_dec, p_header, p_pic);
1568 p_header->nFilledLen = p_sys->in.i_frame_size;
1569 p_header->nOffset = 0;
1570 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1571 p_header->nTimeStamp = p_pic->date;
1572 #ifdef OMXIL_EXTRA_DEBUG
1573 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1574 (int)p_header->nFilledLen );
1576 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1577 p_sys->in.b_flushed = false;
1580 /* Handle the PortSettingsChanged events */
1581 for(i = 0; i < p_sys->ports; i++)
1583 OmxPort *p_port = &p_sys->p_ports[i];
1584 if(!p_port->b_reconfigure) continue;
1585 p_port->b_reconfigure = 0;
1586 omx_error = PortReconfigure(p_dec, p_port);
1589 /* Wait for the decoded frame */
1592 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1594 if(p_header->nFilledLen)
1596 if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1598 /* TODO: need to store codec config */
1599 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1602 p_block = p_header->pAppPrivate;
1605 /* We're not in direct rendering mode.
1606 * Get a new block and copy the content */
1607 p_block = block_New( p_dec, p_header->nFilledLen );
1608 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1611 p_block->i_buffer = p_header->nFilledLen;
1612 p_block->i_pts = p_block->i_dts = p_header->nTimeStamp;
1613 p_header->nFilledLen = 0;
1614 p_header->pAppPrivate = 0;
1617 #ifdef OMXIL_EXTRA_DEBUG
1618 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1620 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1623 msg_Dbg(p_dec, "done");
1627 /*****************************************************************************
1628 * CloseGeneric: omxil decoder destruction
1629 *****************************************************************************/
1630 static void CloseGeneric( vlc_object_t *p_this )
1632 decoder_t *p_dec = (decoder_t *)p_this;
1633 decoder_sys_t *p_sys = p_dec->p_sys;
1635 if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1636 vlc_mutex_lock( &omx_core_mutex );
1638 if( omx_refcount == 0 )
1640 if( p_sys->b_init ) pf_deinit();
1641 dll_close( dll_handle );
1643 vlc_mutex_unlock( &omx_core_mutex );
1645 vlc_mutex_destroy (&p_sys->mutex);
1646 vlc_cond_destroy (&p_sys->cond);
1647 vlc_mutex_destroy (&p_sys->in.fifo.lock);
1648 vlc_cond_destroy (&p_sys->in.fifo.wait);
1649 vlc_mutex_destroy (&p_sys->out.fifo.lock);
1650 vlc_cond_destroy (&p_sys->out.fifo.wait);
1655 /*****************************************************************************
1657 *****************************************************************************/
1658 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1659 OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1660 OMX_U32 data_2, OMX_PTR event_data )
1662 decoder_t *p_dec = (decoder_t *)app_data;
1663 decoder_sys_t *p_sys = p_dec->p_sys;
1669 case OMX_EventCmdComplete:
1670 switch ((OMX_STATETYPE)data_1)
1672 case OMX_CommandStateSet:
1673 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %s)", EventToString(event),
1674 CommandToString(data_1), StateToString(data_2) );
1678 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u)", EventToString(event),
1679 CommandToString(data_1), (unsigned int)data_2 );
1684 case OMX_EventError:
1685 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u, %s)", EventToString(event),
1686 ErrorToString((OMX_ERRORTYPE)data_1), (unsigned int)data_2,
1687 (const char *)event_data);
1688 //p_sys->b_error = true;
1691 case OMX_EventPortSettingsChanged:
1692 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1693 (unsigned int)data_1, (unsigned int)data_2 );
1694 if( data_2 == 0 || data_2 == OMX_IndexParamPortDefinition )
1696 OMX_BUFFERHEADERTYPE *sentinel;
1697 for(i = 0; i < p_sys->ports; i++)
1698 if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1699 p_sys->p_ports[i].b_reconfigure = true;
1700 sentinel = calloc(1, sizeof(*sentinel));
1702 sentinel->nFlags = SENTINEL_FLAG;
1703 OMX_FIFO_PUT(&p_sys->in.fifo, sentinel);
1706 else if( data_2 == OMX_IndexConfigCommonOutputCrop )
1708 for(i = 0; i < p_sys->ports; i++)
1709 if(p_sys->p_ports[i].definition.nPortIndex == data_1)
1710 p_sys->p_ports[i].b_update_def = true;
1714 msg_Dbg( p_dec, "Unhandled setting change %x", (unsigned int)data_2 );
1719 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1720 (unsigned int)data_1, (unsigned int)data_2 );
1724 PostOmxEvent(p_dec, event, data_1, data_2, event_data);
1725 return OMX_ErrorNone;
1728 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1729 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1731 decoder_t *p_dec = (decoder_t *)app_data;
1732 decoder_sys_t *p_sys = p_dec->p_sys;
1735 #ifdef OMXIL_EXTRA_DEBUG
1736 msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1739 if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1741 block_t *p_block = (block_t *)omx_header->pAppPrivate;
1742 omx_header->pBuffer = omx_header->pOutputPortPrivate;
1743 if(p_block) block_Release(p_block);
1744 omx_header->pAppPrivate = 0;
1746 OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1748 return OMX_ErrorNone;
1751 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1752 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1754 decoder_t *p_dec = (decoder_t *)app_data;
1755 decoder_sys_t *p_sys = p_dec->p_sys;
1758 #ifdef OMXIL_EXTRA_DEBUG
1759 msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
1760 (int)omx_header->nFilledLen );
1763 if(omx_header->pInputPortPrivate)
1765 omx_header->pBuffer = omx_header->pInputPortPrivate;
1767 OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1769 return OMX_ErrorNone;