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 def->nBufferSize = def->format.video.nFrameWidth *
322 def->format.video.nFrameHeight * 2;
323 p_port->i_frame_size = def->nBufferSize;
325 if(!GetOmxVideoFormat(p_fmt->i_codec,
326 &def->format.video.eCompressionFormat, 0) )
328 if(!GetOmxChromaFormat(p_fmt->i_codec,
329 &def->format.video.eColorFormat, 0) )
331 omx_error = OMX_ErrorNotImplemented;
332 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
333 (char *)&p_fmt->i_codec );
335 GetVlcChromaSizes( p_fmt->i_codec,
336 def->format.video.nFrameWidth,
337 def->format.video.nFrameHeight,
338 &p_port->i_frame_size, &p_port->i_frame_stride,
339 &p_port->i_frame_stride_chroma_div );
340 def->format.video.nStride = p_port->i_frame_stride;
341 def->nBufferSize = p_port->i_frame_size;
346 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
347 &p_fmt->i_codec, 0 ) )
349 omx_error = OMX_ErrorNotImplemented;
350 CHECK_ERROR(omx_error, "OMX color format %i not supported",
351 (int)def->format.video.eColorFormat );
353 GetVlcChromaSizes( p_fmt->i_codec,
354 def->format.video.nFrameWidth,
355 def->format.video.nFrameHeight,
356 &p_port->i_frame_size, &p_port->i_frame_stride,
357 &p_port->i_frame_stride_chroma_div );
358 def->format.video.nStride = p_port->i_frame_stride;
359 if (p_port->i_frame_size > def->nBufferSize)
360 def->nBufferSize = p_port->i_frame_size;
365 p_port->i_frame_size = def->nBufferSize;
366 if(def->eDir == OMX_DirInput)
368 if(!GetOmxAudioFormat(p_fmt->i_codec,
369 &def->format.audio.eEncoding, 0) )
371 omx_error = OMX_ErrorNotImplemented;
372 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
373 (char *)&p_fmt->i_codec );
378 if( !OmxToVlcAudioFormat(def->format.audio.eEncoding,
379 &p_fmt->i_codec, 0 ) )
381 omx_error = OMX_ErrorNotImplemented;
382 CHECK_ERROR(omx_error, "OMX audio encoding %i not supported",
383 (int)def->format.audio.eEncoding );
388 default: return OMX_ErrorNotImplemented;
391 omx_error = ImplementationSpecificWorkarounds(p_dec, p_port, p_fmt);
392 CHECK_ERROR(omx_error, "ImplementationSpecificWorkarounds failed (%x : %s)",
393 omx_error, ErrorToString(omx_error));
395 omx_error = OMX_SetParameter(p_port->omx_handle,
396 OMX_IndexParamPortDefinition, def);
397 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
398 omx_error, ErrorToString(omx_error));
400 omx_error = OMX_GetParameter(p_port->omx_handle,
401 OMX_IndexParamPortDefinition, def);
402 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
403 omx_error, ErrorToString(omx_error));
405 if(p_port->i_frame_size > def->nBufferSize)
406 def->nBufferSize = p_port->i_frame_size;
407 p_port->i_frame_size = def->nBufferSize;
409 /* Deal with audio params */
410 if(p_fmt->i_cat == AUDIO_ES)
412 omx_error = SetAudioParameters(p_port->omx_handle,
413 &p_port->format_param, def->nPortIndex,
414 def->format.audio.eEncoding,
415 p_fmt->audio.i_channels,
418 p_fmt->audio.i_bitspersample,
419 p_fmt->audio.i_blockalign);
420 CHECK_ERROR(omx_error, "SetAudioParameters failed (%x : %s)",
421 omx_error, ErrorToString(omx_error));
423 if (!strcmp(p_dec->p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER") &&
424 def->eDir == OMX_DirOutput)
426 /* When setting the output buffer size above, the decoder actually
427 * sets the buffer size to a lower value than what was chosen. If
428 * we try to allocate buffers of this size, it fails. Thus, forcibly
429 * use a larger buffer size. */
430 def->nBufferSize *= 2;
437 /*****************************************************************************
438 * GetPortDefinition: set vlc format based on the definition of the omx port
439 *****************************************************************************/
440 static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
443 decoder_sys_t *p_sys = p_dec->p_sys;
444 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
445 OMX_ERRORTYPE omx_error;
446 OMX_CONFIG_RECTTYPE crop_rect;
448 omx_error = OMX_GetParameter(p_port->omx_handle,
449 OMX_IndexParamPortDefinition, def);
450 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
451 omx_error, ErrorToString(omx_error));
456 p_fmt->video.i_width = def->format.video.nFrameWidth;
457 p_fmt->video.i_visible_width = def->format.video.nFrameWidth;
458 p_fmt->video.i_height = def->format.video.nFrameHeight;
459 p_fmt->video.i_visible_height = def->format.video.nFrameHeight;
460 p_fmt->video.i_frame_rate = p_dec->fmt_in.video.i_frame_rate;
461 p_fmt->video.i_frame_rate_base = p_dec->fmt_in.video.i_frame_rate_base;
463 OMX_INIT_STRUCTURE(crop_rect);
464 crop_rect.nPortIndex = def->nPortIndex;
465 omx_error = OMX_GetConfig(p_port->omx_handle, OMX_IndexConfigCommonOutputCrop, &crop_rect);
466 if (omx_error == OMX_ErrorNone)
468 if (!def->format.video.nSliceHeight)
469 def->format.video.nSliceHeight = def->format.video.nFrameHeight;
470 if (!def->format.video.nStride)
471 def->format.video.nStride = def->format.video.nFrameWidth;
472 p_fmt->video.i_width = crop_rect.nWidth;
473 p_fmt->video.i_visible_width = crop_rect.nWidth;
474 p_fmt->video.i_height = crop_rect.nHeight;
475 p_fmt->video.i_visible_height = crop_rect.nHeight;
476 if (def->format.video.eColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
477 def->format.video.nSliceHeight -= crop_rect.nTop/2;
481 /* Don't pass the error back to the caller, this isn't mandatory */
482 omx_error = OMX_ErrorNone;
485 /* Hack: Nexus One (stock firmware with binary OMX driver blob)
486 * claims to output 420Planar even though it in in practice is
488 if(def->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar &&
489 !strncmp(p_sys->psz_component, "OMX.qcom.video.decoder",
490 strlen("OMX.qcom.video.decoder")))
491 def->format.video.eColorFormat = OMX_QCOM_COLOR_FormatYVU420SemiPlanar;
493 /* Hack: Galaxy S II (stock firmware) gives a slice height larger
494 * than the video height, but this doesn't imply padding between
495 * the video planes. Nexus S also has a slice height larger than
496 * the video height, but there it actually is real padding, thus
497 * Galaxy S II is the buggy one. The Galaxy S II decoder is
498 * named OMX.SEC.avcdec while the one on Nexus S is
499 * OMX.SEC.AVC.Decoder. Thus do this for any OMX.SEC. that don't
500 * contain the string ".Decoder". */
501 if(!strncmp(p_sys->psz_component, "OMX.SEC.", strlen("OMX.SEC.")) &&
502 !strstr(p_sys->psz_component, ".Decoder"))
503 def->format.video.nSliceHeight = 0;
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 );
607 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
608 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
611 omx_error = OMX_GetState(omx_handle, &state);
612 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
614 if(state == OMX_StateIdle)
616 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
617 OMX_StateLoaded, 0 );
618 CHECK_ERROR(omx_error, "OMX_CommandStateSet Loaded failed (%x)", omx_error );
620 for(i = 0; i < p_sys->ports; i++)
622 OmxPort *p_port = &p_sys->p_ports[i];
623 OMX_BUFFERHEADERTYPE *p_buffer;
625 for(j = 0; j < p_port->i_buffers; j++)
627 OMX_FIFO_GET(&p_port->fifo, p_buffer);
628 if (p_buffer->nFlags & SENTINEL_FLAG) {
633 omx_error = OMX_FreeBuffer( omx_handle,
634 p_port->i_port_index, p_buffer );
636 if(omx_error != OMX_ErrorNone) break;
638 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
639 omx_error, (int)p_port->i_port_index, j );
641 OMX_FIFO_PEEK(&p_port->fifo, p_buffer);
642 if (!p_buffer) break;
644 OMX_FIFO_GET(&p_port->fifo, p_buffer);
645 if (p_buffer->nFlags & SENTINEL_FLAG) {
649 msg_Warn( p_dec, "Stray buffer left in fifo, %p", p_buffer );
653 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
654 CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
658 for(i = 0; i < p_sys->ports; i++)
660 OmxPort *p_port = &p_sys->p_ports[i];
661 free(p_port->pp_buffers);
662 p_port->pp_buffers = 0;
664 omx_error = pf_free_handle( omx_handle );
668 /*****************************************************************************
669 * InitialiseComponent: Load and initialise an OMX component
670 *****************************************************************************/
671 static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
672 OMX_STRING psz_component, OMX_HANDLETYPE *p_handle)
674 static OMX_CALLBACKTYPE callbacks =
675 { OmxEventHandler, OmxEmptyBufferDone, OmxFillBufferDone };
676 decoder_sys_t *p_sys = p_dec->p_sys;
677 OMX_HANDLETYPE omx_handle;
678 OMX_ERRORTYPE omx_error;
680 OMX_U8 psz_role[OMX_MAX_STRINGNAME_SIZE];
681 OMX_PARAM_COMPONENTROLETYPE role;
682 OMX_PARAM_PORTDEFINITIONTYPE definition;
683 OMX_PORT_PARAM_TYPE param;
686 omx_error = pf_get_handle( &omx_handle, psz_component, p_dec, &callbacks );
687 if(omx_error != OMX_ErrorNone)
689 msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
690 omx_error, ErrorToString(omx_error) );
693 strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
695 omx_error = OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
696 if(omx_error == OMX_ErrorNone)
697 msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
699 msg_Dbg(p_dec, "loaded component %s", psz_component);
700 PrintOmx(p_dec, omx_handle, OMX_ALL);
702 /* Set component role */
703 OMX_INIT_STRUCTURE(role);
704 strcpy((char*)role.cRole,
705 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec : p_dec->fmt_in.i_codec,
706 p_dec->fmt_in.i_cat, p_sys->b_enc));
708 omx_error = OMX_SetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
710 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
712 if(omx_error == OMX_ErrorNone)
713 msg_Dbg(p_dec, "component standard role set to %s", role.cRole);
715 /* Find the input / output ports */
716 OMX_INIT_STRUCTURE(param);
717 OMX_INIT_STRUCTURE(definition);
718 omx_error = OMX_GetParameter(omx_handle, p_dec->fmt_in.i_cat == VIDEO_ES ?
719 OMX_IndexParamVideoInit : OMX_IndexParamAudioInit, ¶m);
720 if(omx_error != OMX_ErrorNone) param.nPorts = 0;
722 for(i = 0; i < param.nPorts; i++)
726 /* Get port definition */
727 definition.nPortIndex = param.nStartPortNumber + i;
728 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
730 if(omx_error != OMX_ErrorNone) continue;
732 if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
733 else p_port = &p_sys->out;
735 p_port->b_valid = true;
736 p_port->i_port_index = definition.nPortIndex;
737 p_port->definition = definition;
738 p_port->omx_handle = omx_handle;
741 if(!p_sys->in.b_valid || !p_sys->out.b_valid)
743 omx_error = OMX_ErrorInvalidComponent;
744 CHECK_ERROR(omx_error, "couldn't find an input and output port");
747 if(!strncmp(p_sys->psz_component, "OMX.SEC.", 8))
750 omx_error = OMX_GetExtensionIndex(omx_handle, (OMX_STRING) "OMX.SEC.index.ThumbnailMode", &index);
751 if(omx_error == OMX_ErrorNone)
753 OMX_BOOL enable = OMX_TRUE;
754 omx_error = OMX_SetConfig(omx_handle, index, &enable);
755 CHECK_ERROR(omx_error, "Unable to set ThumbnailMode");
757 OMX_BOOL enable = OMX_TRUE;
758 /* Needed on Samsung Galaxy S II */
759 omx_error = OMX_SetConfig(omx_handle, OMX_IndexVendorSetYUV420pMode, &enable);
760 if (omx_error == OMX_ErrorNone)
761 msg_Dbg(p_dec, "Set OMX_IndexVendorSetYUV420pMode successfully");
763 msg_Dbg(p_dec, "Unable to set OMX_IndexVendorSetYUV420pMode: %x", omx_error);
767 /* Set port definitions */
768 for(i = 0; i < p_sys->ports; i++)
770 omx_error = SetPortDefinition(p_dec, &p_sys->p_ports[i],
771 p_sys->p_ports[i].p_fmt);
772 if(omx_error != OMX_ErrorNone) goto error;
775 /* Allocate our array for the omx buffers and enable ports */
776 for(i = 0; i < p_sys->ports; i++)
778 OmxPort *p_port = &p_sys->p_ports[i];
781 malloc(p_port->definition.nBufferCountActual *
782 sizeof(OMX_BUFFERHEADERTYPE*));
783 if(!p_port->pp_buffers)
785 omx_error = OMX_ErrorInsufficientResources;
786 CHECK_ERROR(omx_error, "memory allocation failed");
788 p_port->i_buffers = p_port->definition.nBufferCountActual;
791 if(!p_port->definition.bEnabled)
793 omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
794 p_port->i_port_index, NULL);
795 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
796 (int)p_port->i_port_index, omx_error );
797 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
798 CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
799 (int)p_port->i_port_index, omx_error );
803 *p_handle = omx_handle;
804 return OMX_ErrorNone;
807 DeinitialiseComponent(p_dec, omx_handle);
812 /*****************************************************************************
813 * OpenDecoder: Create the decoder instance
814 *****************************************************************************/
815 static int OpenDecoder( vlc_object_t *p_this )
817 decoder_t *p_dec = (decoder_t*)p_this;
820 if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
824 if( p_dec->fmt_in.i_cat != VIDEO_ES && !p_dec->b_force)
828 status = OpenGeneric( p_this, false );
829 if(status != VLC_SUCCESS) return status;
831 p_dec->pf_decode_video = DecodeVideo;
832 p_dec->pf_decode_audio = DecodeAudio;
837 /*****************************************************************************
838 * OpenEncoder: Create the encoder instance
839 *****************************************************************************/
840 static int OpenEncoder( vlc_object_t *p_this )
842 encoder_t *p_enc = (encoder_t*)p_this;
845 if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
848 status = OpenGeneric( p_this, true );
849 if(status != VLC_SUCCESS) return status;
851 p_enc->pf_encode_video = EncodeVideo;
856 /*****************************************************************************
857 * OpenGeneric: Create the generic decoder/encoder instance
858 *****************************************************************************/
859 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
861 decoder_t *p_dec = (decoder_t*)p_this;
862 decoder_sys_t *p_sys;
863 OMX_ERRORTYPE omx_error;
864 OMX_BUFFERHEADERTYPE *p_header;
867 vlc_mutex_lock( &omx_core_mutex );
868 if( omx_refcount > 0 )
871 /* Load the OMX core */
872 for( i = 0; ppsz_dll_list[i]; i++ )
874 dll_handle = dll_open( ppsz_dll_list[i] );
875 if( dll_handle ) break;
879 vlc_mutex_unlock( &omx_core_mutex );
883 pf_init = dlsym( dll_handle, "OMX_Init" );
884 pf_deinit = dlsym( dll_handle, "OMX_Deinit" );
885 pf_get_handle = dlsym( dll_handle, "OMX_GetHandle" );
886 pf_free_handle = dlsym( dll_handle, "OMX_FreeHandle" );
887 pf_component_enum = dlsym( dll_handle, "OMX_ComponentNameEnum" );
888 pf_get_roles_of_component = dlsym( dll_handle, "OMX_GetRolesOfComponent" );
889 if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle ||
890 !pf_component_enum || !pf_get_roles_of_component )
892 msg_Warn( p_this, "cannot find OMX_* symbols in `%s' (%s)",
893 ppsz_dll_list[i], dlerror() );
894 dll_close(dll_handle);
895 vlc_mutex_unlock( &omx_core_mutex );
900 /* Allocate the memory needed to store the decoder's structure */
901 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
903 if( omx_refcount == 0 )
904 dll_close(dll_handle);
905 vlc_mutex_unlock( &omx_core_mutex );
909 /* Initialise the thread properties */
912 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
913 p_dec->fmt_out.video = p_dec->fmt_in.video;
914 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
915 p_dec->fmt_out.i_codec = 0;
917 p_sys->b_enc = b_encode;
918 p_sys->pp_last_event = &p_sys->p_events;
919 vlc_mutex_init (&p_sys->mutex);
920 vlc_cond_init (&p_sys->cond);
921 vlc_mutex_init (&p_sys->lock);
922 vlc_mutex_init (&p_sys->in.fifo.lock);
923 vlc_cond_init (&p_sys->in.fifo.wait);
924 p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
925 p_sys->in.fifo.pp_last = &p_sys->in.fifo.p_first;
926 p_sys->in.b_direct = false;
927 p_sys->in.b_flushed = true;
928 p_sys->in.p_fmt = &p_dec->fmt_in;
929 vlc_mutex_init (&p_sys->out.fifo.lock);
930 vlc_cond_init (&p_sys->out.fifo.wait);
931 p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
932 p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
933 p_sys->out.b_direct = true;
934 p_sys->out.b_flushed = true;
935 p_sys->out.p_fmt = &p_dec->fmt_out;
937 p_sys->p_ports = &p_sys->in;
938 p_sys->b_use_pts = 0;
940 msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
941 (char *)&p_dec->fmt_out.i_codec);
943 /* Initialise the OMX core */
944 omx_error = omx_refcount > 0 ? OMX_ErrorNone : pf_init();
946 if(omx_error != OMX_ErrorNone)
948 msg_Warn( p_this, "OMX_Init failed (%x: %s)", omx_error,
949 ErrorToString(omx_error) );
950 vlc_mutex_unlock( &omx_core_mutex );
951 CloseGeneric(p_this);
954 p_sys->b_init = true;
955 vlc_mutex_unlock( &omx_core_mutex );
957 /* Enumerate components and build a list of the one we want to try */
958 if( !CreateComponentsList(p_dec,
959 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
960 p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
963 msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
964 (char *)&p_dec->fmt_in.i_codec );
965 CloseGeneric(p_this);
969 /* Try to load and initialise a component */
970 omx_error = OMX_ErrorUndefined;
971 for(i = 0; i < p_sys->components; i++)
974 /* ignore OpenCore software codecs */
975 if (!strncmp(p_sys->ppsz_components[i], "OMX.PV.", 7))
977 /* The same sw codecs, renamed in ICS (perhaps also in honeycomb) */
978 if (!strncmp(p_sys->ppsz_components[i], "OMX.google.", 11))
980 /* This one has been seen on HTC One V - it behaves like it works,
981 * but FillBufferDone returns buffers filled with 0 bytes. The One V
982 * has got a working OMX.qcom.video.decoder.avc instead though. */
983 if (!strncmp(p_sys->ppsz_components[i], "OMX.ARICENT.", 12))
985 /* Some nVidia codec with DRM */
986 if (!strncmp(p_sys->ppsz_components[i], "OMX.Nvidia.h264.decode.secure", 29))
989 omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
991 if(omx_error == OMX_ErrorNone) break;
993 CHECK_ERROR(omx_error, "no component could be initialised" );
995 /* Move component to Idle then Executing state */
996 OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
997 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
999 /* Allocate omx buffers */
1000 for(i = 0; i < p_sys->ports; i++)
1002 OmxPort *p_port = &p_sys->p_ports[i];
1004 for(j = 0; j < p_port->i_buffers; j++)
1007 #define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
1008 char *p_buf = malloc(p_port->definition.nBufferSize +
1009 p_port->definition.nBufferAlignment);
1010 p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
1013 if(0 && p_port->b_direct)
1015 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
1016 p_port->i_port_index, 0,
1017 p_port->definition.nBufferSize, (void*)1);
1020 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
1021 p_port->i_port_index, 0,
1022 p_port->definition.nBufferSize);
1024 if(omx_error != OMX_ErrorNone) break;
1025 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
1027 p_port->i_buffers = j;
1028 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1029 omx_error, (int)p_port->i_port_index, j );
1032 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1033 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
1035 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
1036 OMX_StateExecuting, 0);
1037 CHECK_ERROR(omx_error, "OMX_CommandStateSet Executing failed (%x)", omx_error );
1038 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1039 CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
1041 /* Send codec configuration data */
1042 if( p_dec->fmt_in.i_extra )
1044 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1045 p_header->nFilledLen = p_dec->fmt_in.i_extra;
1047 /* Convert H.264 NAL format to annex b */
1048 if( p_sys->i_nal_size_length && !p_sys->in.b_direct )
1050 p_header->nFilledLen = 0;
1051 convert_sps_pps( p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
1052 p_header->pBuffer, p_header->nAllocLen,
1053 (uint32_t*) &p_header->nFilledLen, NULL );
1055 else if(p_sys->in.b_direct)
1057 p_header->pOutputPortPrivate = p_header->pBuffer;
1058 p_header->pBuffer = p_dec->fmt_in.p_extra;
1062 if(p_header->nFilledLen > p_header->nAllocLen)
1064 msg_Dbg(p_dec, "buffer too small (%i,%i)", (int)p_header->nFilledLen,
1065 (int)p_header->nAllocLen);
1066 p_header->nFilledLen = p_header->nAllocLen;
1068 memcpy(p_header->pBuffer, p_dec->fmt_in.p_extra, p_header->nFilledLen);
1071 p_header->nOffset = 0;
1072 p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG;
1073 msg_Dbg(p_dec, "sending codec config data %p, %p, %i", p_header,
1074 p_header->pBuffer, (int)p_header->nFilledLen);
1075 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1078 /* Get back output port definition */
1079 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1080 if(omx_error != OMX_ErrorNone) goto error;
1082 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1083 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1085 if(p_sys->b_error) goto error;
1087 p_dec->b_need_packetized = true;
1088 if (!strcmp(p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER"))
1089 p_sys->b_use_pts = 1;
1093 CloseGeneric(p_this);
1094 return VLC_EGENERIC;
1097 /*****************************************************************************
1099 *****************************************************************************/
1100 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
1102 decoder_sys_t *p_sys = p_dec->p_sys;
1103 OMX_PARAM_PORTDEFINITIONTYPE definition;
1104 OMX_BUFFERHEADERTYPE *p_buffer;
1105 OMX_ERRORTYPE omx_error;
1108 /* Sanity checking */
1109 OMX_INIT_STRUCTURE(definition);
1110 definition.nPortIndex = p_port->i_port_index;
1111 omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1113 if(omx_error != OMX_ErrorNone || (p_dec->fmt_in.i_cat == VIDEO_ES &&
1114 (!definition.format.video.nFrameWidth ||
1115 !definition.format.video.nFrameHeight)) )
1116 return OMX_ErrorUndefined;
1118 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
1119 p_port->i_port_index, NULL);
1120 CHECK_ERROR(omx_error, "OMX_CommandPortDisable on %i failed (%x)",
1121 (int)p_port->i_port_index, omx_error );
1123 for(i = 0; i < p_port->i_buffers; i++)
1125 OMX_FIFO_GET(&p_port->fifo, p_buffer);
1126 if (p_buffer->nFlags & SENTINEL_FLAG) {
1131 omx_error = OMX_FreeBuffer( p_sys->omx_handle,
1132 p_port->i_port_index, p_buffer );
1134 if(omx_error != OMX_ErrorNone) break;
1136 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
1137 omx_error, (int)p_port->i_port_index, i );
1139 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1140 CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
1142 /* Get the new port definition */
1143 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1144 if(omx_error != OMX_ErrorNone) goto error;
1146 if( p_dec->fmt_in.i_cat != AUDIO_ES )
1148 /* Don't explicitly set the new parameters that we got with
1149 * OMX_GetParameter above when using audio codecs.
1150 * That struct hasn't been changed since, so there should be
1151 * no need to set it here, unless some codec expects the
1152 * SetParameter call as a trigger event for some part of
1153 * the reconfiguration.
1154 * This fixes using audio decoders on Samsung Galaxy S II,
1156 * Only skipping this for audio codecs, to minimize the
1157 * change for current working configurations for video.
1159 omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1161 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
1162 omx_error, ErrorToString(omx_error));
1165 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
1166 p_port->i_port_index, NULL);
1167 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
1168 (int)p_port->i_port_index, omx_error );
1170 if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
1171 free(p_port->pp_buffers);
1172 p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
1173 if(!p_port->pp_buffers)
1175 omx_error = OMX_ErrorInsufficientResources;
1176 CHECK_ERROR(omx_error, "memory allocation failed");
1179 p_port->i_buffers = p_port->definition.nBufferCountActual;
1180 for(i = 0; i < p_port->i_buffers; i++)
1182 if(0 && p_port->b_direct)
1184 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1185 p_port->i_port_index, 0,
1186 p_port->definition.nBufferSize, (void*)1);
1189 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1190 p_port->i_port_index, 0,
1191 p_port->definition.nBufferSize);
1193 if(omx_error != OMX_ErrorNone) break;
1194 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1196 p_port->i_buffers = i;
1197 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1198 omx_error, (int)p_port->i_port_index, i );
1200 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1201 CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1203 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1204 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1210 /*****************************************************************************
1211 * DecodeVideo: Called to decode one frame
1212 *****************************************************************************/
1213 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1215 decoder_sys_t *p_sys = p_dec->p_sys;
1216 picture_t *p_pic = NULL, *p_next_pic;
1217 OMX_ERRORTYPE omx_error;
1220 OMX_BUFFERHEADERTYPE *p_header;
1223 if( !pp_block || !*pp_block )
1226 p_block = *pp_block;
1228 /* Check for errors from codec */
1231 msg_Dbg(p_dec, "error during decoding");
1232 block_Release( p_block );
1236 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1238 block_Release( p_block );
1239 if(!p_sys->in.b_flushed)
1241 msg_Dbg(p_dec, "flushing");
1242 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1243 p_sys->in.definition.nPortIndex, 0 );
1245 p_sys->in.b_flushed = true;
1249 /* Take care of decoded frames first */
1252 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1253 if(!p_header) break; /* No frame available */
1255 if(p_sys->out.b_update_def)
1257 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1258 p_sys->out.b_update_def = 0;
1261 if(p_header->nFilledLen)
1263 p_pic = p_header->pAppPrivate;
1266 /* We're not in direct rendering mode.
1267 * Get a new picture and copy the content */
1268 p_pic = decoder_NewPicture( p_dec );
1271 CopyOmxPicture(p_dec, p_pic, p_header, p_sys->out.definition.format.video.nSliceHeight);
1275 p_pic->date = p_header->nTimeStamp;
1276 p_header->nFilledLen = 0;
1277 p_header->pAppPrivate = 0;
1280 /* Get a new picture */
1281 if(p_sys->in.b_direct && !p_header->pAppPrivate)
1283 p_next_pic = decoder_NewPicture( p_dec );
1284 if(!p_next_pic) break;
1286 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1287 p_header->pAppPrivate = p_next_pic;
1288 p_header->pInputPortPrivate = p_header->pBuffer;
1289 p_header->pBuffer = p_next_pic->p[0].p_pixels;
1293 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1296 #ifdef OMXIL_EXTRA_DEBUG
1297 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1299 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1302 /* Send the input buffer to the component */
1303 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1305 if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1312 p_header->nFilledLen = p_block->i_buffer;
1313 p_header->nOffset = 0;
1314 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1315 if (p_sys->b_use_pts)
1316 p_header->nTimeStamp = p_block->i_pts;
1318 p_header->nTimeStamp = p_block->i_dts;
1320 /* In direct mode we pass the input pointer as is.
1321 * Otherwise we memcopy the data */
1322 if(p_sys->in.b_direct)
1324 p_header->pOutputPortPrivate = p_header->pBuffer;
1325 p_header->pBuffer = p_block->p_buffer;
1326 p_header->pAppPrivate = p_block;
1330 if(p_header->nFilledLen > p_header->nAllocLen)
1332 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1333 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1334 p_header->nFilledLen = p_header->nAllocLen;
1336 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1337 block_Release(p_block);
1340 /* Convert H.264 NAL format to annex b */
1341 if( p_sys->i_nal_size_length >= 3 && p_sys->i_nal_size_length <= 4 )
1343 /* This only works for NAL sizes 3-4 */
1344 int i_len = p_header->nFilledLen, i;
1345 uint8_t* ptr = p_header->pBuffer;
1346 while( i_len >= p_sys->i_nal_size_length )
1348 uint32_t nal_len = 0;
1349 for( i = 0; i < p_sys->i_nal_size_length; i++ ) {
1350 nal_len = (nal_len << 8) | ptr[i];
1353 ptr[p_sys->i_nal_size_length - 1] = 1;
1354 if( nal_len > INT_MAX || nal_len > (unsigned int) i_len )
1356 ptr += nal_len + p_sys->i_nal_size_length;
1357 i_len -= nal_len + p_sys->i_nal_size_length;
1360 #ifdef OMXIL_EXTRA_DEBUG
1361 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1362 (int)p_header->nFilledLen );
1364 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1365 p_sys->in.b_flushed = false;
1366 *pp_block = NULL; /* Avoid being fed the same packet again */
1370 /* Handle the PortSettingsChanged events */
1371 for(i = 0; i < p_sys->ports; i++)
1373 OmxPort *p_port = &p_sys->p_ports[i];
1374 if(p_port->b_reconfigure)
1376 omx_error = PortReconfigure(p_dec, p_port);
1377 p_port->b_reconfigure = 0;
1379 if(p_port->b_update_def)
1381 omx_error = GetPortDefinition(p_dec, p_port, p_port->p_fmt);
1382 p_port->b_update_def = 0;
1389 /*****************************************************************************
1390 * DecodeAudio: Called to decode one frame
1391 *****************************************************************************/
1392 block_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1394 decoder_sys_t *p_sys = p_dec->p_sys;
1395 block_t *p_buffer = NULL;
1396 OMX_BUFFERHEADERTYPE *p_header;
1397 OMX_ERRORTYPE omx_error;
1401 if( !pp_block || !*pp_block ) return NULL;
1403 p_block = *pp_block;
1405 /* Check for errors from codec */
1408 msg_Dbg(p_dec, "error during decoding");
1409 block_Release( p_block );
1413 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1415 block_Release( p_block );
1416 date_Set( &p_sys->end_date, 0 );
1417 if(!p_sys->in.b_flushed)
1419 msg_Dbg(p_dec, "flushing");
1420 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1421 p_sys->in.definition.nPortIndex, 0 );
1423 p_sys->in.b_flushed = true;
1427 if( !date_Get( &p_sys->end_date ) )
1429 if( !p_block->i_pts )
1431 /* We've just started the stream, wait for the first PTS. */
1432 block_Release( p_block );
1435 date_Set( &p_sys->end_date, p_block->i_pts );
1438 /* Take care of decoded frames first */
1441 unsigned int i_samples;
1443 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1444 if(!p_header) break; /* No frame available */
1446 i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1449 p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1450 if( !p_buffer ) break; /* No audio buffer available */
1452 memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1453 p_header->nFilledLen = 0;
1455 if( p_header->nTimeStamp != 0 &&
1456 p_header->nTimeStamp != date_Get( &p_sys->end_date ) )
1457 date_Set( &p_sys->end_date, p_header->nTimeStamp );
1459 p_buffer->i_pts = date_Get( &p_sys->end_date );
1460 p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1464 #ifdef OMXIL_EXTRA_DEBUG
1465 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1467 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1468 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1472 /* Send the input buffer to the component */
1473 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1475 if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1482 p_header->nFilledLen = p_block->i_buffer;
1483 p_header->nOffset = 0;
1484 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1485 p_header->nTimeStamp = p_block->i_dts;
1487 /* In direct mode we pass the input pointer as is.
1488 * Otherwise we memcopy the data */
1489 if(p_sys->in.b_direct)
1491 p_header->pOutputPortPrivate = p_header->pBuffer;
1492 p_header->pBuffer = p_block->p_buffer;
1493 p_header->pAppPrivate = p_block;
1497 if(p_header->nFilledLen > p_header->nAllocLen)
1499 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1500 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1501 p_header->nFilledLen = p_header->nAllocLen;
1503 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1504 block_Release(p_block);
1507 #ifdef OMXIL_EXTRA_DEBUG
1508 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1509 (int)p_header->nFilledLen );
1511 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1512 p_sys->in.b_flushed = false;
1513 *pp_block = NULL; /* Avoid being fed the same packet again */
1517 /* Handle the PortSettingsChanged events */
1518 for(i = 0; i < p_sys->ports; i++)
1520 OmxPort *p_port = &p_sys->p_ports[i];
1521 if(!p_port->b_reconfigure) continue;
1522 p_port->b_reconfigure = 0;
1523 omx_error = PortReconfigure(p_dec, p_port);
1529 /*****************************************************************************
1530 * EncodeVideo: Called to encode one frame
1531 *****************************************************************************/
1532 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1534 decoder_t *p_dec = ( decoder_t *)p_enc;
1535 decoder_sys_t *p_sys = p_dec->p_sys;
1536 OMX_ERRORTYPE omx_error;
1539 OMX_BUFFERHEADERTYPE *p_header;
1540 block_t *p_block = 0;
1542 if( !p_pic ) return NULL;
1544 /* Check for errors from codec */
1547 msg_Dbg(p_dec, "error during encoding");
1551 /* Send the input buffer to the component */
1552 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1555 /* In direct mode we pass the input pointer as is.
1556 * Otherwise we memcopy the data */
1557 if(p_sys->in.b_direct)
1559 p_header->pOutputPortPrivate = p_header->pBuffer;
1560 p_header->pBuffer = p_pic->p[0].p_pixels;
1564 CopyVlcPicture(p_dec, p_header, p_pic);
1567 p_header->nFilledLen = p_sys->in.i_frame_size;
1568 p_header->nOffset = 0;
1569 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1570 p_header->nTimeStamp = p_pic->date;
1571 #ifdef OMXIL_EXTRA_DEBUG
1572 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1573 (int)p_header->nFilledLen );
1575 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1576 p_sys->in.b_flushed = false;
1579 /* Handle the PortSettingsChanged events */
1580 for(i = 0; i < p_sys->ports; i++)
1582 OmxPort *p_port = &p_sys->p_ports[i];
1583 if(!p_port->b_reconfigure) continue;
1584 p_port->b_reconfigure = 0;
1585 omx_error = PortReconfigure(p_dec, p_port);
1588 /* Wait for the decoded frame */
1591 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1593 if(p_header->nFilledLen)
1595 if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1597 /* TODO: need to store codec config */
1598 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1601 p_block = p_header->pAppPrivate;
1604 /* We're not in direct rendering mode.
1605 * Get a new block and copy the content */
1606 p_block = block_New( p_dec, p_header->nFilledLen );
1607 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1610 p_block->i_buffer = p_header->nFilledLen;
1611 p_block->i_pts = p_block->i_dts = p_header->nTimeStamp;
1612 p_header->nFilledLen = 0;
1613 p_header->pAppPrivate = 0;
1616 #ifdef OMXIL_EXTRA_DEBUG
1617 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1619 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1622 msg_Dbg(p_dec, "done");
1626 /*****************************************************************************
1627 * CloseGeneric: omxil decoder destruction
1628 *****************************************************************************/
1629 static void CloseGeneric( vlc_object_t *p_this )
1631 decoder_t *p_dec = (decoder_t *)p_this;
1632 decoder_sys_t *p_sys = p_dec->p_sys;
1634 if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1635 vlc_mutex_lock( &omx_core_mutex );
1637 if( omx_refcount == 0 )
1639 if( p_sys->b_init ) pf_deinit();
1640 dll_close( dll_handle );
1642 vlc_mutex_unlock( &omx_core_mutex );
1644 vlc_mutex_destroy (&p_sys->mutex);
1645 vlc_cond_destroy (&p_sys->cond);
1646 vlc_mutex_destroy (&p_sys->in.fifo.lock);
1647 vlc_cond_destroy (&p_sys->in.fifo.wait);
1648 vlc_mutex_destroy (&p_sys->out.fifo.lock);
1649 vlc_cond_destroy (&p_sys->out.fifo.wait);
1654 /*****************************************************************************
1656 *****************************************************************************/
1657 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1658 OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1659 OMX_U32 data_2, OMX_PTR event_data )
1661 decoder_t *p_dec = (decoder_t *)app_data;
1662 decoder_sys_t *p_sys = p_dec->p_sys;
1668 case OMX_EventCmdComplete:
1669 switch ((OMX_STATETYPE)data_1)
1671 case OMX_CommandStateSet:
1672 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %s)", EventToString(event),
1673 CommandToString(data_1), StateToString(data_2) );
1677 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u)", EventToString(event),
1678 CommandToString(data_1), (unsigned int)data_2 );
1683 case OMX_EventError:
1684 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u, %s)", EventToString(event),
1685 ErrorToString((OMX_ERRORTYPE)data_1), (unsigned int)data_2,
1686 (const char *)event_data);
1687 //p_sys->b_error = true;
1690 case OMX_EventPortSettingsChanged:
1691 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1692 (unsigned int)data_1, (unsigned int)data_2 );
1693 if( data_2 == 0 || data_2 == OMX_IndexParamPortDefinition )
1695 OMX_BUFFERHEADERTYPE *sentinel;
1696 for(i = 0; i < p_sys->ports; i++)
1697 if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1698 p_sys->p_ports[i].b_reconfigure = true;
1699 sentinel = calloc(1, sizeof(*sentinel));
1701 sentinel->nFlags = SENTINEL_FLAG;
1702 OMX_FIFO_PUT(&p_sys->in.fifo, sentinel);
1705 else if( data_2 == OMX_IndexConfigCommonOutputCrop )
1707 for(i = 0; i < p_sys->ports; i++)
1708 if(p_sys->p_ports[i].definition.nPortIndex == data_1)
1709 p_sys->p_ports[i].b_update_def = true;
1713 msg_Dbg( p_dec, "Unhandled setting change %x", (unsigned int)data_2 );
1718 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1719 (unsigned int)data_1, (unsigned int)data_2 );
1723 PostOmxEvent(p_dec, event, data_1, data_2, event_data);
1724 return OMX_ErrorNone;
1727 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1728 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1730 decoder_t *p_dec = (decoder_t *)app_data;
1731 decoder_sys_t *p_sys = p_dec->p_sys;
1734 #ifdef OMXIL_EXTRA_DEBUG
1735 msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1738 if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1740 block_t *p_block = (block_t *)omx_header->pAppPrivate;
1741 omx_header->pBuffer = omx_header->pOutputPortPrivate;
1742 if(p_block) block_Release(p_block);
1743 omx_header->pAppPrivate = 0;
1745 OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1747 return OMX_ErrorNone;
1750 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1751 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1753 decoder_t *p_dec = (decoder_t *)app_data;
1754 decoder_sys_t *p_sys = p_dec->p_sys;
1757 #ifdef OMXIL_EXTRA_DEBUG
1758 msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
1759 (int)omx_header->nFilledLen );
1762 if(omx_header->pInputPortPrivate)
1764 omx_header->pBuffer = omx_header->pInputPortPrivate;
1766 OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1768 return OMX_ErrorNone;