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"
53 //#define OMXIL_EXTRA_DEBUG
55 #define SENTINEL_FLAG 0x10000
57 /*****************************************************************************
58 * List of OpenMAX IL core we will try in order
59 *****************************************************************************/
60 static const char *ppsz_dll_list[] =
63 "libiomx.so", /* Not used when using IOMX, the lib should already be loaded */
65 "libOMX_Core.so", /* TI OMAP IL core */
66 "libOmxCore.so", /* Qualcomm IL core */
67 "libomxil-bellagio.so", /* Bellagio IL core */
72 /*****************************************************************************
73 * Global OMX Core instance, shared between module instances
74 *****************************************************************************/
75 static vlc_mutex_t omx_core_mutex = VLC_STATIC_MUTEX;
76 static unsigned int omx_refcount = 0;
77 static void *dll_handle;
78 static OMX_ERRORTYPE (*pf_init) (void);
79 static OMX_ERRORTYPE (*pf_deinit) (void);
80 static OMX_ERRORTYPE (*pf_get_handle) (OMX_HANDLETYPE *, OMX_STRING,
81 OMX_PTR, OMX_CALLBACKTYPE *);
82 static OMX_ERRORTYPE (*pf_free_handle) (OMX_HANDLETYPE);
83 static OMX_ERRORTYPE (*pf_component_enum)(OMX_STRING, OMX_U32, OMX_U32);
84 static OMX_ERRORTYPE (*pf_get_roles_of_component)(OMX_STRING, OMX_U32 *, OMX_U8 **);
86 /*****************************************************************************
88 *****************************************************************************/
89 static int OpenDecoder( vlc_object_t * );
90 static int OpenEncoder( vlc_object_t * );
91 static int OpenGeneric( vlc_object_t *, bool b_encode );
92 static void CloseGeneric( vlc_object_t * );
94 static picture_t *DecodeVideo( decoder_t *, block_t ** );
95 static block_t *DecodeAudio ( decoder_t *, block_t ** );
96 static block_t *EncodeVideo( encoder_t *, picture_t * );
98 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE, OMX_PTR, OMX_EVENTTYPE,
99 OMX_U32, OMX_U32, OMX_PTR );
100 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE, OMX_PTR,
101 OMX_BUFFERHEADERTYPE * );
102 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE, OMX_PTR,
103 OMX_BUFFERHEADERTYPE * );
105 /*****************************************************************************
107 *****************************************************************************/
109 set_description( N_("Audio/Video decoder (using OpenMAX IL)") )
110 set_category( CAT_INPUT )
111 set_subcategory( SUBCAT_INPUT_VCODEC )
112 set_section( N_("Decoding") , NULL )
113 #if defined(USE_IOMX)
114 /* For IOMX, don't enable it automatically via priorities,
115 * enable it only via the --codec iomx command line parameter when
117 set_capability( "decoder", 0 )
119 set_capability( "decoder", 80 )
121 set_callbacks( OpenDecoder, CloseGeneric )
124 set_section( N_("Encoding") , NULL )
125 set_description( N_("Video encoder (using OpenMAX IL)") )
126 set_capability( "encoder", 0 )
127 set_callbacks( OpenEncoder, CloseGeneric )
130 /*****************************************************************************
131 * CreateComponentsList: creates a list of components matching the given role
132 *****************************************************************************/
133 static int CreateComponentsList(decoder_t *p_dec, const char *psz_role)
135 decoder_sys_t *p_sys = p_dec->p_sys;
136 char psz_name[OMX_MAX_STRINGNAME_SIZE];
137 OMX_ERRORTYPE omx_error;
139 OMX_U8 **ppsz_roles = 0;
140 unsigned int i, j, len;
142 if(!psz_role) goto end;
143 len = strlen(psz_role);
147 bool b_found = false;
149 omx_error = pf_component_enum(psz_name, OMX_MAX_STRINGNAME_SIZE, i);
150 if(omx_error != OMX_ErrorNone) break;
152 msg_Dbg(p_dec, "component %s", psz_name);
154 omx_error = pf_get_roles_of_component(psz_name, &roles, 0);
155 if(omx_error != OMX_ErrorNone || !roles) continue;
157 ppsz_roles = malloc(roles * (sizeof(OMX_U8*) + OMX_MAX_STRINGNAME_SIZE));
158 if(!ppsz_roles) continue;
160 for( j = 0; j < roles; j++ )
161 ppsz_roles[j] = ((OMX_U8 *)(&ppsz_roles[roles])) +
162 j * OMX_MAX_STRINGNAME_SIZE;
164 omx_error = pf_get_roles_of_component(psz_name, &roles, ppsz_roles);
165 if(omx_error != OMX_ErrorNone) roles = 0;
167 for(j = 0; j < roles; j++)
169 msg_Dbg(p_dec, " - role: %s", ppsz_roles[j]);
170 if(!strncmp((char *)ppsz_roles[j], psz_role, len)) b_found = true;
175 if(!b_found) continue;
177 if(p_sys->components >= MAX_COMPONENTS_LIST_SIZE)
179 msg_Dbg(p_dec, "too many matching components");
183 strncpy(p_sys->ppsz_components[p_sys->components], psz_name,
184 OMX_MAX_STRINGNAME_SIZE-1);
189 msg_Dbg(p_dec, "found %i matching components for role %s",
190 p_sys->components, psz_role);
191 for( i = 0; i < p_sys->components; i++ )
192 msg_Dbg(p_dec, "- %s", p_sys->ppsz_components[i]);
194 return p_sys->components;
197 /*****************************************************************************
198 * ImplementationSpecificWorkarounds: place-holder for implementation
199 * specific workarounds
200 *****************************************************************************/
201 static OMX_ERRORTYPE ImplementationSpecificWorkarounds(decoder_t *p_dec,
202 OmxPort *p_port, es_format_t *p_fmt)
204 decoder_sys_t *p_sys = p_dec->p_sys;
205 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
206 int i_profile = 0xFFFF, i_level = 0xFFFF;
208 /* Try to find out the profile of the video */
209 while(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
210 p_fmt->i_codec == VLC_CODEC_H264)
212 uint8_t *p = (uint8_t*)p_dec->fmt_in.p_extra;
213 if(!p || !p_dec->fmt_in.p_extra) break;
215 /* Check the profile / level */
216 if(p_dec->fmt_in.i_original_fourcc == VLC_FOURCC('a','v','c','1') &&
219 if(p_dec->fmt_in.i_extra < 12) break;
220 p_sys->i_nal_size_length = 1 + (p[4]&0x03);
221 if( !(p[5]&0x1f) ) break;
226 if(p_dec->fmt_in.i_extra < 8) break;
227 if(!p[0] && !p[1] && !p[2] && p[3] == 1) p += 4;
228 else if(!p[0] && !p[1] && p[2] == 1) p += 3;
232 if( ((*p++)&0x1f) != 7) break;
234 /* Get profile/level out of first SPS */
240 if(!strcmp(p_sys->psz_component, "OMX.TI.Video.Decoder"))
242 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
243 p_fmt->i_codec == VLC_CODEC_H264 &&
244 (i_profile != 66 || i_level > 30))
246 msg_Dbg(p_dec, "h264 profile/level not supported (0x%x, 0x%x)",
248 return OMX_ErrorNotImplemented;
251 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirOutput &&
252 p_fmt->i_codec == VLC_CODEC_I420)
254 /* I420 xvideo is slow on OMAP */
255 def->format.video.eColorFormat = OMX_COLOR_FormatCbYCrY;
256 GetVlcChromaFormat( def->format.video.eColorFormat,
257 &p_fmt->i_codec, 0 );
258 GetVlcChromaSizes( p_fmt->i_codec,
259 def->format.video.nFrameWidth,
260 def->format.video.nFrameHeight,
261 &p_port->i_frame_size, &p_port->i_frame_stride,
262 &p_port->i_frame_stride_chroma_div );
263 def->format.video.nStride = p_port->i_frame_stride;
264 def->nBufferSize = p_port->i_frame_size;
267 else if(!strcmp(p_sys->psz_component, "OMX.st.video_encoder"))
269 if(p_fmt->i_cat == VIDEO_ES)
271 /* Bellagio's encoder doesn't encode the framerate in Q16 */
272 def->format.video.xFramerate >>= 16;
275 #if 0 /* FIXME: doesn't apply for HP Touchpad */
276 else if (!strncmp(p_sys->psz_component, "OMX.qcom.video.decoder.",
277 strlen("OMX.qcom.video.decoder")))
279 /* qdsp6 refuses buffer size larger than 450K on input port */
280 if (def->nBufferSize > 450 * 1024)
282 def->nBufferSize = 450 * 1024;
283 p_port->i_frame_size = def->nBufferSize;
288 return OMX_ErrorNone;
291 /*****************************************************************************
292 * SetPortDefinition: set definition of the omx port based on the vlc format
293 *****************************************************************************/
294 static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
297 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
298 OMX_ERRORTYPE omx_error;
300 omx_error = OMX_GetParameter(p_port->omx_handle,
301 OMX_IndexParamPortDefinition, def);
302 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
303 omx_error, ErrorToString(omx_error));
308 def->format.video.nFrameWidth = p_fmt->video.i_width;
309 def->format.video.nFrameHeight = p_fmt->video.i_height;
310 if(def->format.video.eCompressionFormat == OMX_VIDEO_CodingUnused)
311 def->format.video.nStride = def->format.video.nFrameWidth;
312 if( p_fmt->video.i_frame_rate > 0 &&
313 p_fmt->video.i_frame_rate_base > 0 )
314 def->format.video.xFramerate = (p_fmt->video.i_frame_rate << 16) /
315 p_fmt->video.i_frame_rate_base;
317 if(def->eDir == OMX_DirInput || p_dec->p_sys->b_enc)
319 def->nBufferSize = def->format.video.nFrameWidth *
320 def->format.video.nFrameHeight * 2;
321 p_port->i_frame_size = def->nBufferSize;
323 if(!GetOmxVideoFormat(p_fmt->i_codec,
324 &def->format.video.eCompressionFormat, 0) )
326 if(!GetOmxChromaFormat(p_fmt->i_codec,
327 &def->format.video.eColorFormat, 0) )
329 omx_error = OMX_ErrorNotImplemented;
330 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
331 (char *)&p_fmt->i_codec );
333 GetVlcChromaSizes( p_fmt->i_codec,
334 def->format.video.nFrameWidth,
335 def->format.video.nFrameHeight,
336 &p_port->i_frame_size, &p_port->i_frame_stride,
337 &p_port->i_frame_stride_chroma_div );
338 def->format.video.nStride = p_port->i_frame_stride;
339 def->nBufferSize = p_port->i_frame_size;
344 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
345 &p_fmt->i_codec, 0 ) )
347 omx_error = OMX_ErrorNotImplemented;
348 CHECK_ERROR(omx_error, "OMX color format %i not supported",
349 (int)def->format.video.eColorFormat );
351 GetVlcChromaSizes( p_fmt->i_codec,
352 def->format.video.nFrameWidth,
353 def->format.video.nFrameHeight,
354 &p_port->i_frame_size, &p_port->i_frame_stride,
355 &p_port->i_frame_stride_chroma_div );
356 def->format.video.nStride = p_port->i_frame_stride;
357 if (p_port->i_frame_size > def->nBufferSize)
358 def->nBufferSize = p_port->i_frame_size;
363 p_port->i_frame_size = def->nBufferSize;
364 if(def->eDir == OMX_DirInput)
366 if(!GetOmxAudioFormat(p_fmt->i_codec,
367 &def->format.audio.eEncoding, 0) )
369 omx_error = OMX_ErrorNotImplemented;
370 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
371 (char *)&p_fmt->i_codec );
376 if( !OmxToVlcAudioFormat(def->format.audio.eEncoding,
377 &p_fmt->i_codec, 0 ) )
379 omx_error = OMX_ErrorNotImplemented;
380 CHECK_ERROR(omx_error, "OMX audio encoding %i not supported",
381 (int)def->format.audio.eEncoding );
386 default: return OMX_ErrorNotImplemented;
389 omx_error = ImplementationSpecificWorkarounds(p_dec, p_port, p_fmt);
390 CHECK_ERROR(omx_error, "ImplementationSpecificWorkarounds failed (%x : %s)",
391 omx_error, ErrorToString(omx_error));
393 omx_error = OMX_SetParameter(p_port->omx_handle,
394 OMX_IndexParamPortDefinition, def);
395 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
396 omx_error, ErrorToString(omx_error));
398 omx_error = OMX_GetParameter(p_port->omx_handle,
399 OMX_IndexParamPortDefinition, def);
400 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
401 omx_error, ErrorToString(omx_error));
403 if(p_port->i_frame_size > def->nBufferSize)
404 def->nBufferSize = p_port->i_frame_size;
405 p_port->i_frame_size = def->nBufferSize;
407 /* Deal with audio params */
408 if(p_fmt->i_cat == AUDIO_ES)
410 omx_error = SetAudioParameters(p_port->omx_handle,
411 &p_port->format_param, def->nPortIndex,
412 def->format.audio.eEncoding,
413 p_fmt->audio.i_channels,
416 p_fmt->audio.i_bitspersample,
417 p_fmt->audio.i_blockalign);
418 CHECK_ERROR(omx_error, "SetAudioParameters failed (%x : %s)",
419 omx_error, ErrorToString(omx_error));
421 if (!strcmp(p_dec->p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER") &&
422 def->eDir == OMX_DirOutput)
424 /* When setting the output buffer size above, the decoder actually
425 * sets the buffer size to a lower value than what was chosen. If
426 * we try to allocate buffers of this size, it fails. Thus, forcibly
427 * use a larger buffer size. */
428 def->nBufferSize *= 2;
435 /*****************************************************************************
436 * GetPortDefinition: set vlc format based on the definition of the omx port
437 *****************************************************************************/
438 static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
441 decoder_sys_t *p_sys = p_dec->p_sys;
442 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
443 OMX_ERRORTYPE omx_error;
444 OMX_CONFIG_RECTTYPE crop_rect;
446 omx_error = OMX_GetParameter(p_port->omx_handle,
447 OMX_IndexParamPortDefinition, def);
448 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
449 omx_error, ErrorToString(omx_error));
454 p_fmt->video.i_width = def->format.video.nFrameWidth;
455 p_fmt->video.i_visible_width = def->format.video.nFrameWidth;
456 p_fmt->video.i_height = def->format.video.nFrameHeight;
457 p_fmt->video.i_visible_height = def->format.video.nFrameHeight;
458 p_fmt->video.i_frame_rate = p_dec->fmt_in.video.i_frame_rate;
459 p_fmt->video.i_frame_rate_base = p_dec->fmt_in.video.i_frame_rate_base;
461 OMX_INIT_STRUCTURE(crop_rect);
462 crop_rect.nPortIndex = def->nPortIndex;
463 omx_error = OMX_GetConfig(p_port->omx_handle, OMX_IndexConfigCommonOutputCrop, &crop_rect);
464 if (omx_error == OMX_ErrorNone)
466 if (!def->format.video.nSliceHeight)
467 def->format.video.nSliceHeight = def->format.video.nFrameHeight;
468 if (!def->format.video.nStride)
469 def->format.video.nStride = def->format.video.nFrameWidth;
470 p_fmt->video.i_width = crop_rect.nWidth;
471 p_fmt->video.i_visible_width = crop_rect.nWidth;
472 p_fmt->video.i_height = crop_rect.nHeight;
473 p_fmt->video.i_visible_height = crop_rect.nHeight;
474 if (def->format.video.eColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
475 def->format.video.nSliceHeight -= crop_rect.nTop/2;
479 /* Don't pass the error back to the caller, this isn't mandatory */
480 omx_error = OMX_ErrorNone;
483 /* Hack: Nexus One (stock firmware with binary OMX driver blob)
484 * claims to output 420Planar even though it in in practice is
486 if(def->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar &&
487 !strncmp(p_sys->psz_component, "OMX.qcom.video.decoder",
488 strlen("OMX.qcom.video.decoder")))
489 def->format.video.eColorFormat = OMX_QCOM_COLOR_FormatYVU420SemiPlanar;
491 /* Hack: Galaxy S II (stock firmware) gives a slice height larger
492 * than the video height, but this doesn't imply padding between
493 * the video planes. Nexus S also has a slice height larger than
494 * the video height, but there it actually is real padding, thus
495 * Galaxy S II is the buggy one. The Galaxy S II decoder is
496 * named OMX.SEC.avcdec while the one on Nexus S is
497 * OMX.SEC.AVC.Decoder. Thus do this for any OMX.SEC. that don't
498 * contain the string ".Decoder". */
499 if(!strncmp(p_sys->psz_component, "OMX.SEC.", strlen("OMX.SEC.")) &&
500 !strstr(p_sys->psz_component, ".Decoder"))
501 def->format.video.nSliceHeight = 0;
503 if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
504 &p_fmt->i_codec, 0 ) )
506 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
507 &p_fmt->i_codec, 0 ) )
509 omx_error = OMX_ErrorNotImplemented;
510 CHECK_ERROR(omx_error, "OMX color format %i not supported",
511 (int)def->format.video.eColorFormat );
513 GetVlcChromaSizes( p_fmt->i_codec,
514 def->format.video.nFrameWidth,
515 def->format.video.nFrameHeight,
516 &p_port->i_frame_size, &p_port->i_frame_stride,
517 &p_port->i_frame_stride_chroma_div );
519 if(p_port->i_frame_size > def->nBufferSize)
520 def->nBufferSize = p_port->i_frame_size;
521 p_port->i_frame_size = def->nBufferSize;
523 if((int)p_port->i_frame_stride > def->format.video.nStride)
524 def->format.video.nStride = p_port->i_frame_stride;
526 p_port->i_frame_stride = def->format.video.nStride;
530 if( !OmxToVlcAudioFormat( def->format.audio.eEncoding,
531 &p_fmt->i_codec, 0 ) )
533 omx_error = OMX_ErrorNotImplemented;
534 CHECK_ERROR(omx_error, "OMX audio format %i not supported",
535 (int)def->format.audio.eEncoding );
538 omx_error = GetAudioParameters(p_port->omx_handle,
539 &p_port->format_param, def->nPortIndex,
540 def->format.audio.eEncoding,
541 &p_fmt->audio.i_channels,
542 &p_fmt->audio.i_rate,
544 &p_fmt->audio.i_bitspersample,
545 &p_fmt->audio.i_blockalign);
546 CHECK_ERROR(omx_error, "GetAudioParameters failed (%x : %s)",
547 omx_error, ErrorToString(omx_error));
549 if(p_fmt->audio.i_channels < 9)
551 static const int pi_channels_maps[9] =
553 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
554 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
555 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
556 | AOUT_CHAN_REARRIGHT,
557 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
558 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
559 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
560 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
561 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
562 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
563 | AOUT_CHAN_MIDDLERIGHT,
564 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
565 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
568 p_fmt->audio.i_physical_channels =
569 p_fmt->audio.i_original_channels =
570 pi_channels_maps[p_fmt->audio.i_channels];
573 date_Init( &p_dec->p_sys->end_date, p_fmt->audio.i_rate, 1 );
577 default: return OMX_ErrorNotImplemented;
584 /*****************************************************************************
585 * DeinitialiseComponent: Deinitialise and unload an OMX component
586 *****************************************************************************/
587 static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
588 OMX_HANDLETYPE omx_handle)
590 decoder_sys_t *p_sys = p_dec->p_sys;
591 OMX_ERRORTYPE omx_error;
595 if(!omx_handle) return OMX_ErrorNone;
597 omx_error = OMX_GetState(omx_handle, &state);
598 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
600 if(state == OMX_StateExecuting)
602 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
604 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
605 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
606 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
609 omx_error = OMX_GetState(omx_handle, &state);
610 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
612 if(state == OMX_StateIdle)
614 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
615 OMX_StateLoaded, 0 );
616 CHECK_ERROR(omx_error, "OMX_CommandStateSet Loaded failed (%x)", omx_error );
618 for(i = 0; i < p_sys->ports; i++)
620 OmxPort *p_port = &p_sys->p_ports[i];
621 OMX_BUFFERHEADERTYPE *p_buffer;
623 for(j = 0; j < p_port->i_buffers; j++)
625 OMX_FIFO_GET(&p_port->fifo, p_buffer);
626 if (p_buffer->nFlags & SENTINEL_FLAG) {
631 omx_error = OMX_FreeBuffer( omx_handle,
632 p_port->i_port_index, p_buffer );
634 if(omx_error != OMX_ErrorNone) break;
636 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
637 omx_error, (int)p_port->i_port_index, j );
639 OMX_FIFO_PEEK(&p_port->fifo, p_buffer);
640 if (!p_buffer) break;
642 OMX_FIFO_GET(&p_port->fifo, p_buffer);
643 if (p_buffer->nFlags & SENTINEL_FLAG) {
647 msg_Warn( p_dec, "Stray buffer left in fifo, %p", p_buffer );
651 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
652 CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
656 for(i = 0; i < p_sys->ports; i++)
658 OmxPort *p_port = &p_sys->p_ports[i];
659 free(p_port->pp_buffers);
660 p_port->pp_buffers = 0;
662 omx_error = pf_free_handle( omx_handle );
666 /*****************************************************************************
667 * InitialiseComponent: Load and initialise an OMX component
668 *****************************************************************************/
669 static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
670 OMX_STRING psz_component, OMX_HANDLETYPE *p_handle)
672 static OMX_CALLBACKTYPE callbacks =
673 { OmxEventHandler, OmxEmptyBufferDone, OmxFillBufferDone };
674 decoder_sys_t *p_sys = p_dec->p_sys;
675 OMX_HANDLETYPE omx_handle;
676 OMX_ERRORTYPE omx_error;
678 OMX_U8 psz_role[OMX_MAX_STRINGNAME_SIZE];
679 OMX_PARAM_COMPONENTROLETYPE role;
680 OMX_PARAM_PORTDEFINITIONTYPE definition;
681 OMX_PORT_PARAM_TYPE param;
684 omx_error = pf_get_handle( &omx_handle, psz_component, p_dec, &callbacks );
685 if(omx_error != OMX_ErrorNone)
687 msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
688 omx_error, ErrorToString(omx_error) );
691 strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
693 omx_error = OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
694 if(omx_error == OMX_ErrorNone)
695 msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
697 msg_Dbg(p_dec, "loaded component %s", psz_component);
698 PrintOmx(p_dec, omx_handle, OMX_ALL);
700 /* Set component role */
701 OMX_INIT_STRUCTURE(role);
702 strcpy((char*)role.cRole,
703 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec : p_dec->fmt_in.i_codec,
704 p_dec->fmt_in.i_cat, p_sys->b_enc));
706 omx_error = OMX_SetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
708 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
710 if(omx_error == OMX_ErrorNone)
711 msg_Dbg(p_dec, "component standard role set to %s", role.cRole);
713 /* Find the input / output ports */
714 OMX_INIT_STRUCTURE(param);
715 OMX_INIT_STRUCTURE(definition);
716 omx_error = OMX_GetParameter(omx_handle, p_dec->fmt_in.i_cat == VIDEO_ES ?
717 OMX_IndexParamVideoInit : OMX_IndexParamAudioInit, ¶m);
718 if(omx_error != OMX_ErrorNone) param.nPorts = 0;
720 for(i = 0; i < param.nPorts; i++)
724 /* Get port definition */
725 definition.nPortIndex = param.nStartPortNumber + i;
726 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
728 if(omx_error != OMX_ErrorNone) continue;
730 if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
731 else p_port = &p_sys->out;
733 p_port->b_valid = true;
734 p_port->i_port_index = definition.nPortIndex;
735 p_port->definition = definition;
736 p_port->omx_handle = omx_handle;
739 if(!p_sys->in.b_valid || !p_sys->out.b_valid)
741 omx_error = OMX_ErrorInvalidComponent;
742 CHECK_ERROR(omx_error, "couldn't find an input and output port");
745 if(!strncmp(p_sys->psz_component, "OMX.SEC.", 8))
748 omx_error = OMX_GetExtensionIndex(omx_handle, (OMX_STRING) "OMX.SEC.index.ThumbnailMode", &index);
749 if(omx_error == OMX_ErrorNone)
751 OMX_BOOL enable = OMX_TRUE;
752 omx_error = OMX_SetConfig(omx_handle, index, &enable);
753 CHECK_ERROR(omx_error, "Unable to set ThumbnailMode");
755 OMX_BOOL enable = OMX_TRUE;
756 /* Needed on Samsung Galaxy S II */
757 omx_error = OMX_SetConfig(omx_handle, OMX_IndexVendorSetYUV420pMode, &enable);
758 if (omx_error == OMX_ErrorNone)
759 msg_Dbg(p_dec, "Set OMX_IndexVendorSetYUV420pMode successfully");
761 msg_Dbg(p_dec, "Unable to set OMX_IndexVendorSetYUV420pMode: %x", omx_error);
765 /* Set port definitions */
766 for(i = 0; i < p_sys->ports; i++)
768 omx_error = SetPortDefinition(p_dec, &p_sys->p_ports[i],
769 p_sys->p_ports[i].p_fmt);
770 if(omx_error != OMX_ErrorNone) goto error;
773 /* Allocate our array for the omx buffers and enable ports */
774 for(i = 0; i < p_sys->ports; i++)
776 OmxPort *p_port = &p_sys->p_ports[i];
779 malloc(p_port->definition.nBufferCountActual *
780 sizeof(OMX_BUFFERHEADERTYPE*));
781 if(!p_port->pp_buffers)
783 omx_error = OMX_ErrorInsufficientResources;
784 CHECK_ERROR(omx_error, "memory allocation failed");
786 p_port->i_buffers = p_port->definition.nBufferCountActual;
789 if(!p_port->definition.bEnabled)
791 omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
792 p_port->i_port_index, NULL);
793 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
794 (int)p_port->i_port_index, omx_error );
795 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
796 CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
797 (int)p_port->i_port_index, omx_error );
801 *p_handle = omx_handle;
802 return OMX_ErrorNone;
805 DeinitialiseComponent(p_dec, omx_handle);
810 /*****************************************************************************
811 * OpenDecoder: Create the decoder instance
812 *****************************************************************************/
813 static int OpenDecoder( vlc_object_t *p_this )
815 decoder_t *p_dec = (decoder_t*)p_this;
818 if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
822 if( p_dec->fmt_in.i_cat != VIDEO_ES && !p_dec->b_force)
826 status = OpenGeneric( p_this, false );
827 if(status != VLC_SUCCESS) return status;
829 p_dec->pf_decode_video = DecodeVideo;
830 p_dec->pf_decode_audio = DecodeAudio;
835 /*****************************************************************************
836 * OpenEncoder: Create the encoder instance
837 *****************************************************************************/
838 static int OpenEncoder( vlc_object_t *p_this )
840 encoder_t *p_enc = (encoder_t*)p_this;
843 if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
846 status = OpenGeneric( p_this, true );
847 if(status != VLC_SUCCESS) return status;
849 p_enc->pf_encode_video = EncodeVideo;
854 /*****************************************************************************
855 * OpenGeneric: Create the generic decoder/encoder instance
856 *****************************************************************************/
857 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
859 decoder_t *p_dec = (decoder_t*)p_this;
860 decoder_sys_t *p_sys;
861 OMX_ERRORTYPE omx_error;
862 OMX_BUFFERHEADERTYPE *p_header;
865 vlc_mutex_lock( &omx_core_mutex );
866 if( omx_refcount > 0 )
869 /* Load the OMX core */
870 for( i = 0; ppsz_dll_list[i]; i++ )
872 dll_handle = dll_open( ppsz_dll_list[i] );
873 if( dll_handle ) break;
877 vlc_mutex_unlock( &omx_core_mutex );
881 pf_init = dlsym( dll_handle, "OMX_Init" );
882 pf_deinit = dlsym( dll_handle, "OMX_Deinit" );
883 pf_get_handle = dlsym( dll_handle, "OMX_GetHandle" );
884 pf_free_handle = dlsym( dll_handle, "OMX_FreeHandle" );
885 pf_component_enum = dlsym( dll_handle, "OMX_ComponentNameEnum" );
886 pf_get_roles_of_component = dlsym( dll_handle, "OMX_GetRolesOfComponent" );
887 if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle ||
888 !pf_component_enum || !pf_get_roles_of_component )
890 msg_Warn( p_this, "cannot find OMX_* symbols in `%s' (%s)",
891 ppsz_dll_list[i], dlerror() );
892 dll_close(dll_handle);
893 vlc_mutex_unlock( &omx_core_mutex );
898 /* Allocate the memory needed to store the decoder's structure */
899 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
901 if( omx_refcount == 0 )
902 dll_close(dll_handle);
903 vlc_mutex_unlock( &omx_core_mutex );
907 /* Initialise the thread properties */
910 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
911 p_dec->fmt_out.video = p_dec->fmt_in.video;
912 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
913 p_dec->fmt_out.i_codec = 0;
915 p_sys->b_enc = b_encode;
916 p_sys->pp_last_event = &p_sys->p_events;
917 vlc_mutex_init (&p_sys->mutex);
918 vlc_cond_init (&p_sys->cond);
919 vlc_mutex_init (&p_sys->lock);
920 vlc_mutex_init (&p_sys->in.fifo.lock);
921 vlc_cond_init (&p_sys->in.fifo.wait);
922 p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
923 p_sys->in.fifo.pp_last = &p_sys->in.fifo.p_first;
924 p_sys->in.b_direct = false;
925 p_sys->in.b_flushed = true;
926 p_sys->in.p_fmt = &p_dec->fmt_in;
927 vlc_mutex_init (&p_sys->out.fifo.lock);
928 vlc_cond_init (&p_sys->out.fifo.wait);
929 p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
930 p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
931 p_sys->out.b_direct = true;
932 p_sys->out.b_flushed = true;
933 p_sys->out.p_fmt = &p_dec->fmt_out;
935 p_sys->p_ports = &p_sys->in;
936 p_sys->b_use_pts = 0;
938 msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
939 (char *)&p_dec->fmt_out.i_codec);
941 /* Initialise the OMX core */
942 omx_error = omx_refcount > 0 ? OMX_ErrorNone : pf_init();
944 if(omx_error != OMX_ErrorNone)
946 msg_Warn( p_this, "OMX_Init failed (%x: %s)", omx_error,
947 ErrorToString(omx_error) );
948 vlc_mutex_unlock( &omx_core_mutex );
949 CloseGeneric(p_this);
952 p_sys->b_init = true;
953 vlc_mutex_unlock( &omx_core_mutex );
955 /* Enumerate components and build a list of the one we want to try */
956 if( !CreateComponentsList(p_dec,
957 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
958 p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
961 msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
962 (char *)&p_dec->fmt_in.i_codec );
963 CloseGeneric(p_this);
967 /* Try to load and initialise a component */
968 omx_error = OMX_ErrorUndefined;
969 for(i = 0; i < p_sys->components; i++)
972 /* ignore OpenCore software codecs */
973 if (!strncmp(p_sys->ppsz_components[i], "OMX.PV.", 7))
975 /* The same sw codecs, renamed in ICS (perhaps also in honeycomb) */
976 if (!strncmp(p_sys->ppsz_components[i], "OMX.google.", 11))
978 /* This one has been seen on HTC One V - it behaves like it works,
979 * but FillBufferDone returns buffers filled with 0 bytes. The One V
980 * has got a working OMX.qcom.video.decoder.avc instead though. */
981 if (!strncmp(p_sys->ppsz_components[i], "OMX.ARICENT.", 12))
983 /* Some nVidia codec with DRM */
984 if (!strncmp(p_sys->ppsz_components[i], "OMX.Nvidia.h264.decode.secure", 29))
987 omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
989 if(omx_error == OMX_ErrorNone) break;
991 CHECK_ERROR(omx_error, "no component could be initialised" );
993 /* Move component to Idle then Executing state */
994 OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
995 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
997 /* Allocate omx buffers */
998 for(i = 0; i < p_sys->ports; i++)
1000 OmxPort *p_port = &p_sys->p_ports[i];
1002 for(j = 0; j < p_port->i_buffers; j++)
1005 #define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
1006 char *p_buf = malloc(p_port->definition.nBufferSize +
1007 p_port->definition.nBufferAlignment);
1008 p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
1011 if(0 && p_port->b_direct)
1013 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
1014 p_port->i_port_index, 0,
1015 p_port->definition.nBufferSize, (void*)1);
1018 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
1019 p_port->i_port_index, 0,
1020 p_port->definition.nBufferSize);
1022 if(omx_error != OMX_ErrorNone) break;
1023 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
1025 p_port->i_buffers = j;
1026 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1027 omx_error, (int)p_port->i_port_index, j );
1030 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1031 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
1033 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
1034 OMX_StateExecuting, 0);
1035 CHECK_ERROR(omx_error, "OMX_CommandStateSet Executing failed (%x)", omx_error );
1036 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1037 CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
1039 /* Send codec configuration data */
1040 if( p_dec->fmt_in.i_extra )
1042 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1043 p_header->nFilledLen = p_dec->fmt_in.i_extra;
1045 /* Convert H.264 NAL format to annex b */
1046 if( p_sys->i_nal_size_length && !p_sys->in.b_direct )
1048 p_header->nFilledLen = 0;
1049 convert_sps_pps( p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
1050 p_header->pBuffer, p_header->nAllocLen,
1051 (uint32_t*) &p_header->nFilledLen, NULL );
1053 else if(p_sys->in.b_direct)
1055 p_header->pOutputPortPrivate = p_header->pBuffer;
1056 p_header->pBuffer = p_dec->fmt_in.p_extra;
1060 if(p_header->nFilledLen > p_header->nAllocLen)
1062 msg_Dbg(p_dec, "buffer too small (%i,%i)", (int)p_header->nFilledLen,
1063 (int)p_header->nAllocLen);
1064 p_header->nFilledLen = p_header->nAllocLen;
1066 memcpy(p_header->pBuffer, p_dec->fmt_in.p_extra, p_header->nFilledLen);
1069 p_header->nOffset = 0;
1070 p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG;
1071 msg_Dbg(p_dec, "sending codec config data %p, %p, %i", p_header,
1072 p_header->pBuffer, (int)p_header->nFilledLen);
1073 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1076 /* Get back output port definition */
1077 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1078 if(omx_error != OMX_ErrorNone) goto error;
1080 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1081 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1083 if(p_sys->b_error) goto error;
1085 p_dec->b_need_packetized = true;
1086 if (!strcmp(p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER"))
1087 p_sys->b_use_pts = 1;
1091 CloseGeneric(p_this);
1092 return VLC_EGENERIC;
1095 /*****************************************************************************
1097 *****************************************************************************/
1098 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
1100 decoder_sys_t *p_sys = p_dec->p_sys;
1101 OMX_PARAM_PORTDEFINITIONTYPE definition;
1102 OMX_BUFFERHEADERTYPE *p_buffer;
1103 OMX_ERRORTYPE omx_error;
1106 /* Sanity checking */
1107 OMX_INIT_STRUCTURE(definition);
1108 definition.nPortIndex = p_port->i_port_index;
1109 omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1111 if(omx_error != OMX_ErrorNone || (p_dec->fmt_in.i_cat == VIDEO_ES &&
1112 (!definition.format.video.nFrameWidth ||
1113 !definition.format.video.nFrameHeight)) )
1114 return OMX_ErrorUndefined;
1116 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
1117 p_port->i_port_index, NULL);
1118 CHECK_ERROR(omx_error, "OMX_CommandPortDisable on %i failed (%x)",
1119 (int)p_port->i_port_index, omx_error );
1121 for(i = 0; i < p_port->i_buffers; i++)
1123 OMX_FIFO_GET(&p_port->fifo, p_buffer);
1124 if (p_buffer->nFlags & SENTINEL_FLAG) {
1129 omx_error = OMX_FreeBuffer( p_sys->omx_handle,
1130 p_port->i_port_index, p_buffer );
1132 if(omx_error != OMX_ErrorNone) break;
1134 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
1135 omx_error, (int)p_port->i_port_index, i );
1137 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1138 CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
1140 /* Get the new port definition */
1141 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1142 if(omx_error != OMX_ErrorNone) goto error;
1144 if( p_dec->fmt_in.i_cat != AUDIO_ES )
1146 /* Don't explicitly set the new parameters that we got with
1147 * OMX_GetParameter above when using audio codecs.
1148 * That struct hasn't been changed since, so there should be
1149 * no need to set it here, unless some codec expects the
1150 * SetParameter call as a trigger event for some part of
1151 * the reconfiguration.
1152 * This fixes using audio decoders on Samsung Galaxy S II,
1154 * Only skipping this for audio codecs, to minimize the
1155 * change for current working configurations for video.
1157 omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1159 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
1160 omx_error, ErrorToString(omx_error));
1163 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
1164 p_port->i_port_index, NULL);
1165 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
1166 (int)p_port->i_port_index, omx_error );
1168 if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
1169 free(p_port->pp_buffers);
1170 p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
1171 if(!p_port->pp_buffers)
1173 omx_error = OMX_ErrorInsufficientResources;
1174 CHECK_ERROR(omx_error, "memory allocation failed");
1177 p_port->i_buffers = p_port->definition.nBufferCountActual;
1178 for(i = 0; i < p_port->i_buffers; i++)
1180 if(0 && p_port->b_direct)
1182 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1183 p_port->i_port_index, 0,
1184 p_port->definition.nBufferSize, (void*)1);
1187 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1188 p_port->i_port_index, 0,
1189 p_port->definition.nBufferSize);
1191 if(omx_error != OMX_ErrorNone) break;
1192 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1194 p_port->i_buffers = i;
1195 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1196 omx_error, (int)p_port->i_port_index, i );
1198 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1199 CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1201 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1202 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1208 /*****************************************************************************
1209 * DecodeVideo: Called to decode one frame
1210 *****************************************************************************/
1211 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1213 decoder_sys_t *p_sys = p_dec->p_sys;
1214 picture_t *p_pic = NULL, *p_next_pic;
1215 OMX_ERRORTYPE omx_error;
1218 OMX_BUFFERHEADERTYPE *p_header;
1221 if( !pp_block || !*pp_block )
1224 p_block = *pp_block;
1226 /* Check for errors from codec */
1229 msg_Dbg(p_dec, "error during decoding");
1230 block_Release( p_block );
1234 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1236 block_Release( p_block );
1237 if(!p_sys->in.b_flushed)
1239 msg_Dbg(p_dec, "flushing");
1240 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1241 p_sys->in.definition.nPortIndex, 0 );
1243 p_sys->in.b_flushed = true;
1247 /* Take care of decoded frames first */
1250 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1251 if(!p_header) break; /* No frame available */
1253 if(p_sys->out.b_update_def)
1255 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1256 p_sys->out.b_update_def = 0;
1259 if(p_header->nFilledLen)
1261 p_pic = p_header->pAppPrivate;
1264 /* We're not in direct rendering mode.
1265 * Get a new picture and copy the content */
1266 p_pic = decoder_NewPicture( p_dec );
1269 CopyOmxPicture(p_dec, p_pic, p_header, p_sys->out.definition.format.video.nSliceHeight);
1273 p_pic->date = p_header->nTimeStamp;
1274 p_header->nFilledLen = 0;
1275 p_header->pAppPrivate = 0;
1278 /* Get a new picture */
1279 if(p_sys->in.b_direct && !p_header->pAppPrivate)
1281 p_next_pic = decoder_NewPicture( p_dec );
1282 if(!p_next_pic) break;
1284 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1285 p_header->pAppPrivate = p_next_pic;
1286 p_header->pInputPortPrivate = p_header->pBuffer;
1287 p_header->pBuffer = p_next_pic->p[0].p_pixels;
1291 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1294 #ifdef OMXIL_EXTRA_DEBUG
1295 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1297 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1300 /* Send the input buffer to the component */
1301 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1303 if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1310 p_header->nFilledLen = p_block->i_buffer;
1311 p_header->nOffset = 0;
1312 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1313 if (p_sys->b_use_pts)
1314 p_header->nTimeStamp = p_block->i_pts;
1316 p_header->nTimeStamp = p_block->i_dts;
1318 /* In direct mode we pass the input pointer as is.
1319 * Otherwise we memcopy the data */
1320 if(p_sys->in.b_direct)
1322 p_header->pOutputPortPrivate = p_header->pBuffer;
1323 p_header->pBuffer = p_block->p_buffer;
1324 p_header->pAppPrivate = p_block;
1328 if(p_header->nFilledLen > p_header->nAllocLen)
1330 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1331 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1332 p_header->nFilledLen = p_header->nAllocLen;
1334 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1335 block_Release(p_block);
1338 /* Convert H.264 NAL format to annex b */
1339 if( p_sys->i_nal_size_length >= 3 && p_sys->i_nal_size_length <= 4 )
1341 /* This only works for NAL sizes 3-4 */
1342 int i_len = p_header->nFilledLen, i;
1343 uint8_t* ptr = p_header->pBuffer;
1344 while( i_len >= p_sys->i_nal_size_length )
1346 uint32_t nal_len = 0;
1347 for( i = 0; i < p_sys->i_nal_size_length; i++ ) {
1348 nal_len = (nal_len << 8) | ptr[i];
1351 ptr[p_sys->i_nal_size_length - 1] = 1;
1352 if( nal_len > INT_MAX || nal_len > (unsigned int) i_len )
1354 ptr += nal_len + p_sys->i_nal_size_length;
1355 i_len -= nal_len + p_sys->i_nal_size_length;
1358 #ifdef OMXIL_EXTRA_DEBUG
1359 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1360 (int)p_header->nFilledLen );
1362 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1363 p_sys->in.b_flushed = false;
1364 *pp_block = NULL; /* Avoid being fed the same packet again */
1368 /* Handle the PortSettingsChanged events */
1369 for(i = 0; i < p_sys->ports; i++)
1371 OmxPort *p_port = &p_sys->p_ports[i];
1372 if(p_port->b_reconfigure)
1374 omx_error = PortReconfigure(p_dec, p_port);
1375 p_port->b_reconfigure = 0;
1377 if(p_port->b_update_def)
1379 omx_error = GetPortDefinition(p_dec, p_port, p_port->p_fmt);
1380 p_port->b_update_def = 0;
1387 /*****************************************************************************
1388 * DecodeAudio: Called to decode one frame
1389 *****************************************************************************/
1390 block_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1392 decoder_sys_t *p_sys = p_dec->p_sys;
1393 block_t *p_buffer = NULL;
1394 OMX_BUFFERHEADERTYPE *p_header;
1395 OMX_ERRORTYPE omx_error;
1399 if( !pp_block || !*pp_block ) return NULL;
1401 p_block = *pp_block;
1403 /* Check for errors from codec */
1406 msg_Dbg(p_dec, "error during decoding");
1407 block_Release( p_block );
1411 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1413 block_Release( p_block );
1414 date_Set( &p_sys->end_date, 0 );
1415 if(!p_sys->in.b_flushed)
1417 msg_Dbg(p_dec, "flushing");
1418 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1419 p_sys->in.definition.nPortIndex, 0 );
1421 p_sys->in.b_flushed = true;
1425 if( !date_Get( &p_sys->end_date ) )
1427 if( !p_block->i_pts )
1429 /* We've just started the stream, wait for the first PTS. */
1430 block_Release( p_block );
1433 date_Set( &p_sys->end_date, p_block->i_pts );
1436 /* Take care of decoded frames first */
1439 unsigned int i_samples;
1441 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1442 if(!p_header) break; /* No frame available */
1444 i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1447 p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1448 if( !p_buffer ) break; /* No audio buffer available */
1450 memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1451 p_header->nFilledLen = 0;
1453 if( p_header->nTimeStamp != 0 &&
1454 p_header->nTimeStamp != date_Get( &p_sys->end_date ) )
1455 date_Set( &p_sys->end_date, p_header->nTimeStamp );
1457 p_buffer->i_pts = date_Get( &p_sys->end_date );
1458 p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1462 #ifdef OMXIL_EXTRA_DEBUG
1463 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1465 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1466 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1470 /* Send the input buffer to the component */
1471 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1473 if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1480 p_header->nFilledLen = p_block->i_buffer;
1481 p_header->nOffset = 0;
1482 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1483 p_header->nTimeStamp = p_block->i_dts;
1485 /* In direct mode we pass the input pointer as is.
1486 * Otherwise we memcopy the data */
1487 if(p_sys->in.b_direct)
1489 p_header->pOutputPortPrivate = p_header->pBuffer;
1490 p_header->pBuffer = p_block->p_buffer;
1491 p_header->pAppPrivate = p_block;
1495 if(p_header->nFilledLen > p_header->nAllocLen)
1497 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1498 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1499 p_header->nFilledLen = p_header->nAllocLen;
1501 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1502 block_Release(p_block);
1505 #ifdef OMXIL_EXTRA_DEBUG
1506 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1507 (int)p_header->nFilledLen );
1509 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1510 p_sys->in.b_flushed = false;
1511 *pp_block = NULL; /* Avoid being fed the same packet again */
1515 /* Handle the PortSettingsChanged events */
1516 for(i = 0; i < p_sys->ports; i++)
1518 OmxPort *p_port = &p_sys->p_ports[i];
1519 if(!p_port->b_reconfigure) continue;
1520 p_port->b_reconfigure = 0;
1521 omx_error = PortReconfigure(p_dec, p_port);
1527 /*****************************************************************************
1528 * EncodeVideo: Called to encode one frame
1529 *****************************************************************************/
1530 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1532 decoder_t *p_dec = ( decoder_t *)p_enc;
1533 decoder_sys_t *p_sys = p_dec->p_sys;
1534 OMX_ERRORTYPE omx_error;
1537 OMX_BUFFERHEADERTYPE *p_header;
1538 block_t *p_block = 0;
1540 if( !p_pic ) return NULL;
1542 /* Check for errors from codec */
1545 msg_Dbg(p_dec, "error during encoding");
1549 /* Send the input buffer to the component */
1550 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1553 /* In direct mode we pass the input pointer as is.
1554 * Otherwise we memcopy the data */
1555 if(p_sys->in.b_direct)
1557 p_header->pOutputPortPrivate = p_header->pBuffer;
1558 p_header->pBuffer = p_pic->p[0].p_pixels;
1562 CopyVlcPicture(p_dec, p_header, p_pic);
1565 p_header->nFilledLen = p_sys->in.i_frame_size;
1566 p_header->nOffset = 0;
1567 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1568 p_header->nTimeStamp = p_pic->date;
1569 #ifdef OMXIL_EXTRA_DEBUG
1570 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1571 (int)p_header->nFilledLen );
1573 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1574 p_sys->in.b_flushed = false;
1577 /* Handle the PortSettingsChanged events */
1578 for(i = 0; i < p_sys->ports; i++)
1580 OmxPort *p_port = &p_sys->p_ports[i];
1581 if(!p_port->b_reconfigure) continue;
1582 p_port->b_reconfigure = 0;
1583 omx_error = PortReconfigure(p_dec, p_port);
1586 /* Wait for the decoded frame */
1589 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1591 if(p_header->nFilledLen)
1593 if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1595 /* TODO: need to store codec config */
1596 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1599 p_block = p_header->pAppPrivate;
1602 /* We're not in direct rendering mode.
1603 * Get a new block and copy the content */
1604 p_block = block_New( p_dec, p_header->nFilledLen );
1605 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1608 p_block->i_buffer = p_header->nFilledLen;
1609 p_block->i_pts = p_block->i_dts = p_header->nTimeStamp;
1610 p_header->nFilledLen = 0;
1611 p_header->pAppPrivate = 0;
1614 #ifdef OMXIL_EXTRA_DEBUG
1615 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1617 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1620 msg_Dbg(p_dec, "done");
1624 /*****************************************************************************
1625 * CloseGeneric: omxil decoder destruction
1626 *****************************************************************************/
1627 static void CloseGeneric( vlc_object_t *p_this )
1629 decoder_t *p_dec = (decoder_t *)p_this;
1630 decoder_sys_t *p_sys = p_dec->p_sys;
1632 if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1633 vlc_mutex_lock( &omx_core_mutex );
1635 if( omx_refcount == 0 )
1637 if( p_sys->b_init ) pf_deinit();
1638 dll_close( dll_handle );
1640 vlc_mutex_unlock( &omx_core_mutex );
1642 vlc_mutex_destroy (&p_sys->mutex);
1643 vlc_cond_destroy (&p_sys->cond);
1644 vlc_mutex_destroy (&p_sys->in.fifo.lock);
1645 vlc_cond_destroy (&p_sys->in.fifo.wait);
1646 vlc_mutex_destroy (&p_sys->out.fifo.lock);
1647 vlc_cond_destroy (&p_sys->out.fifo.wait);
1652 /*****************************************************************************
1654 *****************************************************************************/
1655 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1656 OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1657 OMX_U32 data_2, OMX_PTR event_data )
1659 decoder_t *p_dec = (decoder_t *)app_data;
1660 decoder_sys_t *p_sys = p_dec->p_sys;
1666 case OMX_EventCmdComplete:
1667 switch ((OMX_STATETYPE)data_1)
1669 case OMX_CommandStateSet:
1670 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %s)", EventToString(event),
1671 CommandToString(data_1), StateToString(data_2) );
1675 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u)", EventToString(event),
1676 CommandToString(data_1), (unsigned int)data_2 );
1681 case OMX_EventError:
1682 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u, %s)", EventToString(event),
1683 ErrorToString((OMX_ERRORTYPE)data_1), (unsigned int)data_2,
1684 (const char *)event_data);
1685 //p_sys->b_error = true;
1688 case OMX_EventPortSettingsChanged:
1689 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1690 (unsigned int)data_1, (unsigned int)data_2 );
1691 if( data_2 == 0 || data_2 == OMX_IndexParamPortDefinition )
1693 OMX_BUFFERHEADERTYPE *sentinel;
1694 for(i = 0; i < p_sys->ports; i++)
1695 if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1696 p_sys->p_ports[i].b_reconfigure = true;
1697 sentinel = calloc(1, sizeof(*sentinel));
1699 sentinel->nFlags = SENTINEL_FLAG;
1700 OMX_FIFO_PUT(&p_sys->in.fifo, sentinel);
1703 else if( data_2 == OMX_IndexConfigCommonOutputCrop )
1705 for(i = 0; i < p_sys->ports; i++)
1706 if(p_sys->p_ports[i].definition.nPortIndex == data_1)
1707 p_sys->p_ports[i].b_update_def = true;
1711 msg_Dbg( p_dec, "Unhandled setting change %x", (unsigned int)data_2 );
1716 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1717 (unsigned int)data_1, (unsigned int)data_2 );
1721 PostOmxEvent(p_dec, event, data_1, data_2, event_data);
1722 return OMX_ErrorNone;
1725 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1726 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1728 decoder_t *p_dec = (decoder_t *)app_data;
1729 decoder_sys_t *p_sys = p_dec->p_sys;
1732 #ifdef OMXIL_EXTRA_DEBUG
1733 msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1736 if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1738 block_t *p_block = (block_t *)omx_header->pAppPrivate;
1739 omx_header->pBuffer = omx_header->pOutputPortPrivate;
1740 if(p_block) block_Release(p_block);
1741 omx_header->pAppPrivate = 0;
1743 OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1745 return OMX_ErrorNone;
1748 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1749 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1751 decoder_t *p_dec = (decoder_t *)app_data;
1752 decoder_sys_t *p_sys = p_dec->p_sys;
1755 #ifdef OMXIL_EXTRA_DEBUG
1756 msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
1757 (int)omx_header->nFilledLen );
1760 if(omx_header->pInputPortPrivate)
1762 omx_header->pBuffer = omx_header->pInputPortPrivate;
1764 OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1766 return OMX_ErrorNone;