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 aout_buffer_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 else if (!strncmp(p_sys->psz_component, "OMX.qcom.video.decoder.",
276 strlen("OMX.qcom.video.decoder")))
278 /* qdsp6 refuses buffer size larger than 450K on input port */
279 if (def->nBufferSize > 450 * 1024)
281 def->nBufferSize = 450 * 1024;
282 p_port->i_frame_size = def->nBufferSize;
286 return OMX_ErrorNone;
289 /*****************************************************************************
290 * SetPortDefinition: set definition of the omx port based on the vlc format
291 *****************************************************************************/
292 static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
295 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
296 OMX_ERRORTYPE omx_error;
298 omx_error = OMX_GetParameter(p_port->omx_handle,
299 OMX_IndexParamPortDefinition, def);
300 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
301 omx_error, ErrorToString(omx_error));
306 def->format.video.nFrameWidth = p_fmt->video.i_width;
307 def->format.video.nFrameHeight = p_fmt->video.i_height;
308 if(def->format.video.eCompressionFormat == OMX_VIDEO_CodingUnused)
309 def->format.video.nStride = def->format.video.nFrameWidth;
310 if( p_fmt->video.i_frame_rate > 0 &&
311 p_fmt->video.i_frame_rate_base > 0 )
312 def->format.video.xFramerate = (p_fmt->video.i_frame_rate << 16) /
313 p_fmt->video.i_frame_rate_base;
315 if(def->eDir == OMX_DirInput || p_dec->p_sys->b_enc)
317 def->nBufferSize = def->format.video.nFrameWidth *
318 def->format.video.nFrameHeight * 2;
319 p_port->i_frame_size = def->nBufferSize;
321 if(!GetOmxVideoFormat(p_fmt->i_codec,
322 &def->format.video.eCompressionFormat, 0) )
324 if(!GetOmxChromaFormat(p_fmt->i_codec,
325 &def->format.video.eColorFormat, 0) )
327 omx_error = OMX_ErrorNotImplemented;
328 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
329 (char *)&p_fmt->i_codec );
331 GetVlcChromaSizes( p_fmt->i_codec,
332 def->format.video.nFrameWidth,
333 def->format.video.nFrameHeight,
334 &p_port->i_frame_size, &p_port->i_frame_stride,
335 &p_port->i_frame_stride_chroma_div );
336 def->format.video.nStride = p_port->i_frame_stride;
337 def->nBufferSize = p_port->i_frame_size;
342 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
343 &p_fmt->i_codec, 0 ) )
345 omx_error = OMX_ErrorNotImplemented;
346 CHECK_ERROR(omx_error, "OMX color format %i not supported",
347 (int)def->format.video.eColorFormat );
349 GetVlcChromaSizes( p_fmt->i_codec,
350 def->format.video.nFrameWidth,
351 def->format.video.nFrameHeight,
352 &p_port->i_frame_size, &p_port->i_frame_stride,
353 &p_port->i_frame_stride_chroma_div );
354 def->format.video.nStride = p_port->i_frame_stride;
355 def->nBufferSize = p_port->i_frame_size;
360 p_port->i_frame_size = def->nBufferSize;
361 if(def->eDir == OMX_DirInput)
363 if(!GetOmxAudioFormat(p_fmt->i_codec,
364 &def->format.audio.eEncoding, 0) )
366 omx_error = OMX_ErrorNotImplemented;
367 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
368 (char *)&p_fmt->i_codec );
373 if( !OmxToVlcAudioFormat(def->format.audio.eEncoding,
374 &p_fmt->i_codec, 0 ) )
376 omx_error = OMX_ErrorNotImplemented;
377 CHECK_ERROR(omx_error, "OMX audio encoding %i not supported",
378 (int)def->format.audio.eEncoding );
383 default: return OMX_ErrorNotImplemented;
386 omx_error = ImplementationSpecificWorkarounds(p_dec, p_port, p_fmt);
387 CHECK_ERROR(omx_error, "ImplementationSpecificWorkarounds failed (%x : %s)",
388 omx_error, ErrorToString(omx_error));
390 omx_error = OMX_SetParameter(p_port->omx_handle,
391 OMX_IndexParamPortDefinition, def);
392 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
393 omx_error, ErrorToString(omx_error));
395 omx_error = OMX_GetParameter(p_port->omx_handle,
396 OMX_IndexParamPortDefinition, def);
397 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
398 omx_error, ErrorToString(omx_error));
400 if(p_port->i_frame_size > def->nBufferSize)
401 def->nBufferSize = p_port->i_frame_size;
402 p_port->i_frame_size = def->nBufferSize;
404 /* Deal with audio params */
405 if(p_fmt->i_cat == AUDIO_ES)
407 omx_error = SetAudioParameters(p_port->omx_handle,
408 &p_port->format_param, def->nPortIndex,
409 def->format.audio.eEncoding,
410 p_fmt->audio.i_channels,
413 p_fmt->audio.i_bitspersample,
414 p_fmt->audio.i_blockalign);
415 CHECK_ERROR(omx_error, "SetAudioParameters failed (%x : %s)",
416 omx_error, ErrorToString(omx_error));
418 if (!strcmp(p_dec->p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER") &&
419 def->eDir == OMX_DirOutput)
421 /* When setting the output buffer size above, the decoder actually
422 * sets the buffer size to a lower value than what was chosen. If
423 * we try to allocate buffers of this size, it fails. Thus, forcibly
424 * use a larger buffer size. */
425 def->nBufferSize *= 2;
432 /*****************************************************************************
433 * GetPortDefinition: set vlc format based on the definition of the omx port
434 *****************************************************************************/
435 static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
438 decoder_sys_t *p_sys = p_dec->p_sys;
439 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
440 OMX_ERRORTYPE omx_error;
441 OMX_CONFIG_RECTTYPE crop_rect;
443 omx_error = OMX_GetParameter(p_port->omx_handle,
444 OMX_IndexParamPortDefinition, def);
445 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
446 omx_error, ErrorToString(omx_error));
451 p_fmt->video.i_width = def->format.video.nFrameWidth;
452 p_fmt->video.i_visible_width = def->format.video.nFrameWidth;
453 p_fmt->video.i_height = def->format.video.nFrameHeight;
454 p_fmt->video.i_visible_height = def->format.video.nFrameHeight;
455 p_fmt->video.i_frame_rate = p_dec->fmt_in.video.i_frame_rate;
456 p_fmt->video.i_frame_rate_base = p_dec->fmt_in.video.i_frame_rate_base;
458 OMX_INIT_STRUCTURE(crop_rect);
459 crop_rect.nPortIndex = def->nPortIndex;
460 omx_error = OMX_GetConfig(p_port->omx_handle, OMX_IndexConfigCommonOutputCrop, &crop_rect);
461 if (omx_error == OMX_ErrorNone)
463 if (!def->format.video.nSliceHeight)
464 def->format.video.nSliceHeight = def->format.video.nFrameHeight;
465 if (!def->format.video.nStride)
466 def->format.video.nStride = def->format.video.nFrameWidth;
467 p_fmt->video.i_width = crop_rect.nWidth;
468 p_fmt->video.i_visible_width = crop_rect.nWidth;
469 p_fmt->video.i_height = crop_rect.nHeight;
470 p_fmt->video.i_visible_height = crop_rect.nHeight;
471 if (def->format.video.eColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
472 def->format.video.nSliceHeight -= crop_rect.nTop/2;
476 /* Don't pass the error back to the caller, this isn't mandatory */
477 omx_error = OMX_ErrorNone;
480 /* Hack: Nexus One (stock firmware with binary OMX driver blob)
481 * claims to output 420Planar even though it in in practice is
483 if(def->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar &&
484 !strncmp(p_sys->psz_component, "OMX.qcom.video.decoder",
485 strlen("OMX.qcom.video.decoder")))
486 def->format.video.eColorFormat = OMX_QCOM_COLOR_FormatYVU420SemiPlanar;
488 /* Hack: Galaxy S II (stock firmware) gives a slice height larger
489 * than the video height, but this doesn't imply padding between
490 * the video planes. Nexus S also has a slice height larger than
491 * the video height, but there it actually is real padding, thus
492 * Galaxy S II is the buggy one. The Galaxy S II decoder is
493 * named OMX.SEC.avcdec while the one on Nexus S is
494 * OMX.SEC.AVC.Decoder. Thus do this for any OMX.SEC. that don't
495 * contain the string ".Decoder". */
496 if(!strncmp(p_sys->psz_component, "OMX.SEC.", strlen("OMX.SEC.")) &&
497 !strstr(p_sys->psz_component, ".Decoder"))
498 def->format.video.nSliceHeight = 0;
500 if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
501 &p_fmt->i_codec, 0 ) )
503 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
504 &p_fmt->i_codec, 0 ) )
506 omx_error = OMX_ErrorNotImplemented;
507 CHECK_ERROR(omx_error, "OMX color format %i not supported",
508 (int)def->format.video.eColorFormat );
510 GetVlcChromaSizes( p_fmt->i_codec,
511 def->format.video.nFrameWidth,
512 def->format.video.nFrameHeight,
513 &p_port->i_frame_size, &p_port->i_frame_stride,
514 &p_port->i_frame_stride_chroma_div );
516 if(p_port->i_frame_size > def->nBufferSize)
517 def->nBufferSize = p_port->i_frame_size;
518 p_port->i_frame_size = def->nBufferSize;
520 if((int)p_port->i_frame_stride > def->format.video.nStride)
521 def->format.video.nStride = p_port->i_frame_stride;
523 p_port->i_frame_stride = def->format.video.nStride;
527 if( !OmxToVlcAudioFormat( def->format.audio.eEncoding,
528 &p_fmt->i_codec, 0 ) )
530 omx_error = OMX_ErrorNotImplemented;
531 CHECK_ERROR(omx_error, "OMX audio format %i not supported",
532 (int)def->format.audio.eEncoding );
535 omx_error = GetAudioParameters(p_port->omx_handle,
536 &p_port->format_param, def->nPortIndex,
537 def->format.audio.eEncoding,
538 &p_fmt->audio.i_channels,
539 &p_fmt->audio.i_rate,
541 &p_fmt->audio.i_bitspersample,
542 &p_fmt->audio.i_blockalign);
543 CHECK_ERROR(omx_error, "GetAudioParameters failed (%x : %s)",
544 omx_error, ErrorToString(omx_error));
546 if(p_fmt->audio.i_channels < 9)
548 static const int pi_channels_maps[9] =
550 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
551 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
552 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
553 | AOUT_CHAN_REARRIGHT,
554 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
555 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
556 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
557 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
558 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
559 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
560 | AOUT_CHAN_MIDDLERIGHT,
561 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
562 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
565 p_fmt->audio.i_physical_channels =
566 p_fmt->audio.i_original_channels =
567 pi_channels_maps[p_fmt->audio.i_channels];
570 date_Init( &p_dec->p_sys->end_date, p_fmt->audio.i_rate, 1 );
574 default: return OMX_ErrorNotImplemented;
581 /*****************************************************************************
582 * DeinitialiseComponent: Deinitialise and unload an OMX component
583 *****************************************************************************/
584 static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
585 OMX_HANDLETYPE omx_handle)
587 decoder_sys_t *p_sys = p_dec->p_sys;
588 OMX_ERRORTYPE omx_error;
592 if(!omx_handle) return OMX_ErrorNone;
594 omx_error = OMX_GetState(omx_handle, &state);
595 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
597 if(state == OMX_StateExecuting)
599 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
601 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
602 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
603 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
606 omx_error = OMX_GetState(omx_handle, &state);
607 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
609 if(state == OMX_StateIdle)
611 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
612 OMX_StateLoaded, 0 );
613 CHECK_ERROR(omx_error, "OMX_CommandStateSet Loaded failed (%x)", omx_error );
615 for(i = 0; i < p_sys->ports; i++)
617 OmxPort *p_port = &p_sys->p_ports[i];
618 OMX_BUFFERHEADERTYPE *p_buffer;
620 for(j = 0; j < p_port->i_buffers; j++)
622 OMX_FIFO_GET(&p_port->fifo, p_buffer);
623 if (p_buffer->nFlags & SENTINEL_FLAG) {
628 omx_error = OMX_FreeBuffer( omx_handle,
629 p_port->i_port_index, p_buffer );
631 if(omx_error != OMX_ErrorNone) break;
633 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
634 omx_error, (int)p_port->i_port_index, j );
636 OMX_FIFO_PEEK(&p_port->fifo, p_buffer);
637 if (!p_buffer) break;
639 OMX_FIFO_GET(&p_port->fifo, p_buffer);
640 if (p_buffer->nFlags & SENTINEL_FLAG) {
644 msg_Warn( p_dec, "Stray buffer left in fifo, %p", p_buffer );
648 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
649 CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
653 for(i = 0; i < p_sys->ports; i++)
655 OmxPort *p_port = &p_sys->p_ports[i];
656 free(p_port->pp_buffers);
657 p_port->pp_buffers = 0;
659 omx_error = pf_free_handle( omx_handle );
663 /*****************************************************************************
664 * InitialiseComponent: Load and initialise an OMX component
665 *****************************************************************************/
666 static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
667 OMX_STRING psz_component, OMX_HANDLETYPE *p_handle)
669 static OMX_CALLBACKTYPE callbacks =
670 { OmxEventHandler, OmxEmptyBufferDone, OmxFillBufferDone };
671 decoder_sys_t *p_sys = p_dec->p_sys;
672 OMX_HANDLETYPE omx_handle;
673 OMX_ERRORTYPE omx_error;
675 OMX_U8 psz_role[OMX_MAX_STRINGNAME_SIZE];
676 OMX_PARAM_COMPONENTROLETYPE role;
677 OMX_PARAM_PORTDEFINITIONTYPE definition;
678 OMX_PORT_PARAM_TYPE param;
681 omx_error = pf_get_handle( &omx_handle, psz_component, p_dec, &callbacks );
682 if(omx_error != OMX_ErrorNone)
684 msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
685 omx_error, ErrorToString(omx_error) );
688 strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
690 omx_error = OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
691 if(omx_error == OMX_ErrorNone)
692 msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
694 msg_Dbg(p_dec, "loaded component %s", psz_component);
695 PrintOmx(p_dec, omx_handle, OMX_ALL);
697 /* Set component role */
698 OMX_INIT_STRUCTURE(role);
699 strcpy((char*)role.cRole,
700 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec : p_dec->fmt_in.i_codec,
701 p_dec->fmt_in.i_cat, p_sys->b_enc));
703 omx_error = OMX_SetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
705 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
707 if(omx_error == OMX_ErrorNone)
708 msg_Dbg(p_dec, "component standard role set to %s", role.cRole);
710 /* Find the input / output ports */
711 OMX_INIT_STRUCTURE(param);
712 OMX_INIT_STRUCTURE(definition);
713 omx_error = OMX_GetParameter(omx_handle, p_dec->fmt_in.i_cat == VIDEO_ES ?
714 OMX_IndexParamVideoInit : OMX_IndexParamAudioInit, ¶m);
715 if(omx_error != OMX_ErrorNone) param.nPorts = 0;
717 for(i = 0; i < param.nPorts; i++)
721 /* Get port definition */
722 definition.nPortIndex = param.nStartPortNumber + i;
723 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
725 if(omx_error != OMX_ErrorNone) continue;
727 if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
728 else p_port = &p_sys->out;
730 p_port->b_valid = true;
731 p_port->i_port_index = definition.nPortIndex;
732 p_port->definition = definition;
733 p_port->omx_handle = omx_handle;
736 if(!p_sys->in.b_valid || !p_sys->out.b_valid)
738 omx_error = OMX_ErrorInvalidComponent;
739 CHECK_ERROR(omx_error, "couldn't find an input and output port");
742 if(!strncmp(p_sys->psz_component, "OMX.SEC.", 8))
745 omx_error = OMX_GetExtensionIndex(omx_handle, (OMX_STRING) "OMX.SEC.index.ThumbnailMode", &index);
746 if(omx_error == OMX_ErrorNone)
748 OMX_BOOL enable = OMX_TRUE;
749 omx_error = OMX_SetConfig(omx_handle, index, &enable);
750 CHECK_ERROR(omx_error, "Unable to set ThumbnailMode");
752 OMX_BOOL enable = OMX_TRUE;
753 /* Needed on Samsung Galaxy S II */
754 omx_error = OMX_SetConfig(omx_handle, OMX_IndexVendorSetYUV420pMode, &enable);
755 if (omx_error == OMX_ErrorNone)
756 msg_Dbg(p_dec, "Set OMX_IndexVendorSetYUV420pMode successfully");
758 msg_Dbg(p_dec, "Unable to set OMX_IndexVendorSetYUV420pMode: %x", omx_error);
762 /* Set port definitions */
763 for(i = 0; i < p_sys->ports; i++)
765 omx_error = SetPortDefinition(p_dec, &p_sys->p_ports[i],
766 p_sys->p_ports[i].p_fmt);
767 if(omx_error != OMX_ErrorNone) goto error;
770 /* Allocate our array for the omx buffers and enable ports */
771 for(i = 0; i < p_sys->ports; i++)
773 OmxPort *p_port = &p_sys->p_ports[i];
776 malloc(p_port->definition.nBufferCountActual *
777 sizeof(OMX_BUFFERHEADERTYPE*));
778 if(!p_port->pp_buffers)
780 omx_error = OMX_ErrorInsufficientResources;
781 CHECK_ERROR(omx_error, "memory allocation failed");
783 p_port->i_buffers = p_port->definition.nBufferCountActual;
786 if(!p_port->definition.bEnabled)
788 omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
789 p_port->i_port_index, NULL);
790 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
791 (int)p_port->i_port_index, omx_error );
792 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
793 CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
794 (int)p_port->i_port_index, omx_error );
798 *p_handle = omx_handle;
799 return OMX_ErrorNone;
802 DeinitialiseComponent(p_dec, omx_handle);
807 /*****************************************************************************
808 * OpenDecoder: Create the decoder instance
809 *****************************************************************************/
810 static int OpenDecoder( vlc_object_t *p_this )
812 decoder_t *p_dec = (decoder_t*)p_this;
815 if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
819 if( p_dec->fmt_in.i_cat != VIDEO_ES && !p_dec->b_force)
823 status = OpenGeneric( p_this, false );
824 if(status != VLC_SUCCESS) return status;
826 p_dec->pf_decode_video = DecodeVideo;
827 p_dec->pf_decode_audio = DecodeAudio;
832 /*****************************************************************************
833 * OpenEncoder: Create the encoder instance
834 *****************************************************************************/
835 static int OpenEncoder( vlc_object_t *p_this )
837 encoder_t *p_enc = (encoder_t*)p_this;
840 if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
843 status = OpenGeneric( p_this, true );
844 if(status != VLC_SUCCESS) return status;
846 p_enc->pf_encode_video = EncodeVideo;
851 /*****************************************************************************
852 * OpenGeneric: Create the generic decoder/encoder instance
853 *****************************************************************************/
854 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
856 decoder_t *p_dec = (decoder_t*)p_this;
857 decoder_sys_t *p_sys;
858 OMX_ERRORTYPE omx_error;
859 OMX_BUFFERHEADERTYPE *p_header;
862 vlc_mutex_lock( &omx_core_mutex );
863 if( omx_refcount > 0 )
866 /* Load the OMX core */
867 for( i = 0; ppsz_dll_list[i]; i++ )
869 dll_handle = dll_open( ppsz_dll_list[i] );
870 if( dll_handle ) break;
874 vlc_mutex_unlock( &omx_core_mutex );
878 pf_init = dlsym( dll_handle, "OMX_Init" );
879 pf_deinit = dlsym( dll_handle, "OMX_Deinit" );
880 pf_get_handle = dlsym( dll_handle, "OMX_GetHandle" );
881 pf_free_handle = dlsym( dll_handle, "OMX_FreeHandle" );
882 pf_component_enum = dlsym( dll_handle, "OMX_ComponentNameEnum" );
883 pf_get_roles_of_component = dlsym( dll_handle, "OMX_GetRolesOfComponent" );
884 if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle ||
885 !pf_component_enum || !pf_get_roles_of_component )
887 msg_Warn( p_this, "cannot find OMX_* symbols in `%s' (%s)",
888 ppsz_dll_list[i], dlerror() );
889 dll_close(dll_handle);
890 vlc_mutex_unlock( &omx_core_mutex );
895 /* Allocate the memory needed to store the decoder's structure */
896 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
898 if( omx_refcount == 0 )
899 dll_close(dll_handle);
900 vlc_mutex_unlock( &omx_core_mutex );
904 /* Initialise the thread properties */
907 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
908 p_dec->fmt_out.video = p_dec->fmt_in.video;
909 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
910 p_dec->fmt_out.i_codec = 0;
912 p_sys->b_enc = b_encode;
913 p_sys->pp_last_event = &p_sys->p_events;
914 vlc_mutex_init (&p_sys->mutex);
915 vlc_cond_init (&p_sys->cond);
916 vlc_mutex_init (&p_sys->lock);
917 vlc_mutex_init (&p_sys->in.fifo.lock);
918 vlc_cond_init (&p_sys->in.fifo.wait);
919 p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
920 p_sys->in.fifo.pp_last = &p_sys->in.fifo.p_first;
921 p_sys->in.b_direct = false;
922 p_sys->in.b_flushed = true;
923 p_sys->in.p_fmt = &p_dec->fmt_in;
924 vlc_mutex_init (&p_sys->out.fifo.lock);
925 vlc_cond_init (&p_sys->out.fifo.wait);
926 p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
927 p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
928 p_sys->out.b_direct = true;
929 p_sys->out.b_flushed = true;
930 p_sys->out.p_fmt = &p_dec->fmt_out;
932 p_sys->p_ports = &p_sys->in;
933 p_sys->b_use_pts = 0;
935 msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
936 (char *)&p_dec->fmt_out.i_codec);
938 /* Initialise the OMX core */
939 omx_error = omx_refcount > 0 ? OMX_ErrorNone : pf_init();
941 if(omx_error != OMX_ErrorNone)
943 msg_Warn( p_this, "OMX_Init failed (%x: %s)", omx_error,
944 ErrorToString(omx_error) );
945 vlc_mutex_unlock( &omx_core_mutex );
946 CloseGeneric(p_this);
949 p_sys->b_init = true;
950 vlc_mutex_unlock( &omx_core_mutex );
952 /* Enumerate components and build a list of the one we want to try */
953 if( !CreateComponentsList(p_dec,
954 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
955 p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
958 msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
959 (char *)&p_dec->fmt_in.i_codec );
960 CloseGeneric(p_this);
964 /* Try to load and initialise a component */
965 omx_error = OMX_ErrorUndefined;
966 for(i = 0; i < p_sys->components; i++)
969 /* ignore OpenCore software codecs */
970 if (!strncmp(p_sys->ppsz_components[i], "OMX.PV.", 7))
972 /* The same sw codecs, renamed in ICS (perhaps also in honeycomb) */
973 if (!strncmp(p_sys->ppsz_components[i], "OMX.google.", 11))
976 omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
978 if(omx_error == OMX_ErrorNone) break;
980 CHECK_ERROR(omx_error, "no component could be initialised" );
982 /* Move component to Idle then Executing state */
983 OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
984 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
986 /* Allocate omx buffers */
987 for(i = 0; i < p_sys->ports; i++)
989 OmxPort *p_port = &p_sys->p_ports[i];
991 for(j = 0; j < p_port->i_buffers; j++)
994 #define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
995 char *p_buf = malloc(p_port->definition.nBufferSize +
996 p_port->definition.nBufferAlignment);
997 p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
1000 if(0 && p_port->b_direct)
1002 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
1003 p_port->i_port_index, 0,
1004 p_port->definition.nBufferSize, (void*)1);
1007 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
1008 p_port->i_port_index, 0,
1009 p_port->definition.nBufferSize);
1011 if(omx_error != OMX_ErrorNone) break;
1012 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
1014 p_port->i_buffers = j;
1015 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1016 omx_error, (int)p_port->i_port_index, j );
1019 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1020 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
1022 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
1023 OMX_StateExecuting, 0);
1024 CHECK_ERROR(omx_error, "OMX_CommandStateSet Executing failed (%x)", omx_error );
1025 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1026 CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
1028 /* Send codec configuration data */
1029 if( p_dec->fmt_in.i_extra )
1031 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1032 p_header->nFilledLen = p_dec->fmt_in.i_extra;
1034 /* Convert H.264 NAL format to annex b */
1035 if( p_sys->i_nal_size_length && !p_sys->in.b_direct )
1037 p_header->nFilledLen = 0;
1038 convert_sps_pps( p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
1039 p_header->pBuffer, p_header->nAllocLen,
1040 (uint32_t*) &p_header->nFilledLen, NULL );
1042 else if(p_sys->in.b_direct)
1044 p_header->pOutputPortPrivate = p_header->pBuffer;
1045 p_header->pBuffer = p_dec->fmt_in.p_extra;
1049 if(p_header->nFilledLen > p_header->nAllocLen)
1051 msg_Dbg(p_dec, "buffer too small (%i,%i)", (int)p_header->nFilledLen,
1052 (int)p_header->nAllocLen);
1053 p_header->nFilledLen = p_header->nAllocLen;
1055 memcpy(p_header->pBuffer, p_dec->fmt_in.p_extra, p_header->nFilledLen);
1058 p_header->nOffset = 0;
1059 p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG;
1060 msg_Dbg(p_dec, "sending codec config data %p, %p, %i", p_header,
1061 p_header->pBuffer, (int)p_header->nFilledLen);
1062 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1065 /* Get back output port definition */
1066 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1067 if(omx_error != OMX_ErrorNone) goto error;
1069 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1070 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1072 if(p_sys->b_error) goto error;
1074 p_dec->b_need_packetized = true;
1075 if (!strcmp(p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER"))
1076 p_sys->b_use_pts = 1;
1080 CloseGeneric(p_this);
1081 return VLC_EGENERIC;
1084 /*****************************************************************************
1086 *****************************************************************************/
1087 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
1089 decoder_sys_t *p_sys = p_dec->p_sys;
1090 OMX_PARAM_PORTDEFINITIONTYPE definition;
1091 OMX_BUFFERHEADERTYPE *p_buffer;
1092 OMX_ERRORTYPE omx_error;
1095 /* Sanity checking */
1096 OMX_INIT_STRUCTURE(definition);
1097 definition.nPortIndex = p_port->i_port_index;
1098 omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1100 if(omx_error != OMX_ErrorNone || (p_dec->fmt_in.i_cat == VIDEO_ES &&
1101 (!definition.format.video.nFrameWidth ||
1102 !definition.format.video.nFrameHeight)) )
1103 return OMX_ErrorUndefined;
1105 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
1106 p_port->i_port_index, NULL);
1107 CHECK_ERROR(omx_error, "OMX_CommandPortDisable on %i failed (%x)",
1108 (int)p_port->i_port_index, omx_error );
1110 for(i = 0; i < p_port->i_buffers; i++)
1112 OMX_FIFO_GET(&p_port->fifo, p_buffer);
1113 if (p_buffer->nFlags & SENTINEL_FLAG) {
1118 omx_error = OMX_FreeBuffer( p_sys->omx_handle,
1119 p_port->i_port_index, p_buffer );
1121 if(omx_error != OMX_ErrorNone) break;
1123 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
1124 omx_error, (int)p_port->i_port_index, i );
1126 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1127 CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
1129 /* Get the new port definition */
1130 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1131 if(omx_error != OMX_ErrorNone) goto error;
1133 if( p_dec->fmt_in.i_cat != AUDIO_ES )
1135 /* Don't explicitly set the new parameters that we got with
1136 * OMX_GetParameter above when using audio codecs.
1137 * That struct hasn't been changed since, so there should be
1138 * no need to set it here, unless some codec expects the
1139 * SetParameter call as a trigger event for some part of
1140 * the reconfiguration.
1141 * This fixes using audio decoders on Samsung Galaxy S II,
1143 * Only skipping this for audio codecs, to minimize the
1144 * change for current working configurations for video.
1146 omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1148 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
1149 omx_error, ErrorToString(omx_error));
1152 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
1153 p_port->i_port_index, NULL);
1154 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
1155 (int)p_port->i_port_index, omx_error );
1157 if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
1158 free(p_port->pp_buffers);
1159 p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
1160 if(!p_port->pp_buffers)
1162 omx_error = OMX_ErrorInsufficientResources;
1163 CHECK_ERROR(omx_error, "memory allocation failed");
1166 p_port->i_buffers = p_port->definition.nBufferCountActual;
1167 for(i = 0; i < p_port->i_buffers; i++)
1169 if(0 && p_port->b_direct)
1171 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1172 p_port->i_port_index, 0,
1173 p_port->definition.nBufferSize, (void*)1);
1176 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1177 p_port->i_port_index, 0,
1178 p_port->definition.nBufferSize);
1180 if(omx_error != OMX_ErrorNone) break;
1181 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1183 p_port->i_buffers = i;
1184 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1185 omx_error, (int)p_port->i_port_index, i );
1187 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1188 CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1190 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1191 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1197 /*****************************************************************************
1198 * DecodeVideo: Called to decode one frame
1199 *****************************************************************************/
1200 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1202 decoder_sys_t *p_sys = p_dec->p_sys;
1203 picture_t *p_pic = NULL, *p_next_pic;
1204 OMX_ERRORTYPE omx_error;
1207 OMX_BUFFERHEADERTYPE *p_header;
1210 if( !pp_block || !*pp_block )
1213 p_block = *pp_block;
1215 /* Check for errors from codec */
1218 msg_Dbg(p_dec, "error during decoding");
1219 block_Release( p_block );
1223 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1225 block_Release( p_block );
1226 if(!p_sys->in.b_flushed)
1228 msg_Dbg(p_dec, "flushing");
1229 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1230 p_sys->in.definition.nPortIndex, 0 );
1232 p_sys->in.b_flushed = true;
1236 /* Take care of decoded frames first */
1239 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1240 if(!p_header) break; /* No frame available */
1242 if(p_sys->out.b_update_def)
1244 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1245 p_sys->out.b_update_def = 0;
1248 if(p_header->nFilledLen)
1250 p_pic = p_header->pAppPrivate;
1253 /* We're not in direct rendering mode.
1254 * Get a new picture and copy the content */
1255 p_pic = decoder_NewPicture( p_dec );
1258 CopyOmxPicture(p_dec, p_pic, p_header, p_sys->out.definition.format.video.nSliceHeight);
1262 p_pic->date = p_header->nTimeStamp;
1263 p_header->nFilledLen = 0;
1264 p_header->pAppPrivate = 0;
1267 /* Get a new picture */
1268 if(p_sys->in.b_direct && !p_header->pAppPrivate)
1270 p_next_pic = decoder_NewPicture( p_dec );
1271 if(!p_next_pic) break;
1273 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1274 p_header->pAppPrivate = p_next_pic;
1275 p_header->pInputPortPrivate = p_header->pBuffer;
1276 p_header->pBuffer = p_next_pic->p[0].p_pixels;
1280 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1283 #ifdef OMXIL_EXTRA_DEBUG
1284 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1286 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1289 /* Send the input buffer to the component */
1290 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1292 if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1299 p_header->nFilledLen = p_block->i_buffer;
1300 p_header->nOffset = 0;
1301 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1302 if (p_sys->b_use_pts)
1303 p_header->nTimeStamp = p_block->i_pts;
1305 p_header->nTimeStamp = p_block->i_dts;
1307 /* In direct mode we pass the input pointer as is.
1308 * Otherwise we memcopy the data */
1309 if(p_sys->in.b_direct)
1311 p_header->pOutputPortPrivate = p_header->pBuffer;
1312 p_header->pBuffer = p_block->p_buffer;
1313 p_header->pAppPrivate = p_block;
1317 if(p_header->nFilledLen > p_header->nAllocLen)
1319 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1320 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1321 p_header->nFilledLen = p_header->nAllocLen;
1323 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1324 block_Release(p_block);
1327 /* Convert H.264 NAL format to annex b */
1328 if( p_sys->i_nal_size_length >= 3 && p_sys->i_nal_size_length <= 4 )
1330 /* This only works for NAL sizes 3-4 */
1331 int i_len = p_header->nFilledLen, i;
1332 uint8_t* ptr = p_header->pBuffer;
1333 while( i_len >= p_sys->i_nal_size_length )
1335 uint32_t nal_len = 0;
1336 for( i = 0; i < p_sys->i_nal_size_length; i++ ) {
1337 nal_len = (nal_len << 8) | ptr[i];
1340 ptr[p_sys->i_nal_size_length - 1] = 1;
1341 if( nal_len > INT_MAX || nal_len > (unsigned int) i_len )
1344 i_len -= nal_len + 4;
1347 #ifdef OMXIL_EXTRA_DEBUG
1348 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1349 (int)p_header->nFilledLen );
1351 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1352 p_sys->in.b_flushed = false;
1353 *pp_block = NULL; /* Avoid being fed the same packet again */
1357 /* Handle the PortSettingsChanged events */
1358 for(i = 0; i < p_sys->ports; i++)
1360 OmxPort *p_port = &p_sys->p_ports[i];
1361 if(p_port->b_reconfigure)
1363 omx_error = PortReconfigure(p_dec, p_port);
1364 p_port->b_reconfigure = 0;
1366 if(p_port->b_update_def)
1368 omx_error = GetPortDefinition(p_dec, p_port, p_port->p_fmt);
1369 p_port->b_update_def = 0;
1376 /*****************************************************************************
1377 * DecodeAudio: Called to decode one frame
1378 *****************************************************************************/
1379 aout_buffer_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1381 decoder_sys_t *p_sys = p_dec->p_sys;
1382 aout_buffer_t *p_buffer = 0;
1383 OMX_BUFFERHEADERTYPE *p_header;
1384 OMX_ERRORTYPE omx_error;
1388 if( !pp_block || !*pp_block ) return NULL;
1390 p_block = *pp_block;
1392 /* Check for errors from codec */
1395 msg_Dbg(p_dec, "error during decoding");
1396 block_Release( p_block );
1400 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1402 block_Release( p_block );
1403 date_Set( &p_sys->end_date, 0 );
1404 if(!p_sys->in.b_flushed)
1406 msg_Dbg(p_dec, "flushing");
1407 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1408 p_sys->in.definition.nPortIndex, 0 );
1410 p_sys->in.b_flushed = true;
1414 if( !date_Get( &p_sys->end_date ) )
1416 if( !p_block->i_pts )
1418 /* We've just started the stream, wait for the first PTS. */
1419 block_Release( p_block );
1422 date_Set( &p_sys->end_date, p_block->i_pts );
1425 /* Take care of decoded frames first */
1428 unsigned int i_samples;
1430 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1431 if(!p_header) break; /* No frame available */
1433 i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1436 p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1437 if( !p_buffer ) break; /* No audio buffer available */
1439 memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1440 p_header->nFilledLen = 0;
1442 if( p_header->nTimeStamp != 0 &&
1443 p_header->nTimeStamp != date_Get( &p_sys->end_date ) )
1444 date_Set( &p_sys->end_date, p_header->nTimeStamp );
1446 p_buffer->i_pts = date_Get( &p_sys->end_date );
1447 p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1451 #ifdef OMXIL_EXTRA_DEBUG
1452 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1454 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1455 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1459 /* Send the input buffer to the component */
1460 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1462 if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1469 p_header->nFilledLen = p_block->i_buffer;
1470 p_header->nOffset = 0;
1471 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1472 p_header->nTimeStamp = p_block->i_dts;
1474 /* In direct mode we pass the input pointer as is.
1475 * Otherwise we memcopy the data */
1476 if(p_sys->in.b_direct)
1478 p_header->pOutputPortPrivate = p_header->pBuffer;
1479 p_header->pBuffer = p_block->p_buffer;
1480 p_header->pAppPrivate = p_block;
1484 if(p_header->nFilledLen > p_header->nAllocLen)
1486 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1487 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1488 p_header->nFilledLen = p_header->nAllocLen;
1490 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1491 block_Release(p_block);
1494 #ifdef OMXIL_EXTRA_DEBUG
1495 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1496 (int)p_header->nFilledLen );
1498 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1499 p_sys->in.b_flushed = false;
1500 *pp_block = NULL; /* Avoid being fed the same packet again */
1504 /* Handle the PortSettingsChanged events */
1505 for(i = 0; i < p_sys->ports; i++)
1507 OmxPort *p_port = &p_sys->p_ports[i];
1508 if(!p_port->b_reconfigure) continue;
1509 p_port->b_reconfigure = 0;
1510 omx_error = PortReconfigure(p_dec, p_port);
1516 /*****************************************************************************
1517 * EncodeVideo: Called to encode one frame
1518 *****************************************************************************/
1519 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1521 decoder_t *p_dec = ( decoder_t *)p_enc;
1522 decoder_sys_t *p_sys = p_dec->p_sys;
1523 OMX_ERRORTYPE omx_error;
1526 OMX_BUFFERHEADERTYPE *p_header;
1527 block_t *p_block = 0;
1529 if( !p_pic ) return NULL;
1531 /* Check for errors from codec */
1534 msg_Dbg(p_dec, "error during encoding");
1538 /* Send the input buffer to the component */
1539 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1542 /* In direct mode we pass the input pointer as is.
1543 * Otherwise we memcopy the data */
1544 if(p_sys->in.b_direct)
1546 p_header->pOutputPortPrivate = p_header->pBuffer;
1547 p_header->pBuffer = p_pic->p[0].p_pixels;
1551 CopyVlcPicture(p_dec, p_header, p_pic);
1554 p_header->nFilledLen = p_sys->in.i_frame_size;
1555 p_header->nOffset = 0;
1556 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1557 p_header->nTimeStamp = p_pic->date;
1558 #ifdef OMXIL_EXTRA_DEBUG
1559 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1560 (int)p_header->nFilledLen );
1562 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1563 p_sys->in.b_flushed = false;
1566 /* Handle the PortSettingsChanged events */
1567 for(i = 0; i < p_sys->ports; i++)
1569 OmxPort *p_port = &p_sys->p_ports[i];
1570 if(!p_port->b_reconfigure) continue;
1571 p_port->b_reconfigure = 0;
1572 omx_error = PortReconfigure(p_dec, p_port);
1575 /* Wait for the decoded frame */
1578 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1580 if(p_header->nFilledLen)
1582 if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1584 /* TODO: need to store codec config */
1585 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1588 p_block = p_header->pAppPrivate;
1591 /* We're not in direct rendering mode.
1592 * Get a new block and copy the content */
1593 p_block = block_New( p_dec, p_header->nFilledLen );
1594 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1597 p_block->i_buffer = p_header->nFilledLen;
1598 p_block->i_pts = p_block->i_dts = p_header->nTimeStamp;
1599 p_header->nFilledLen = 0;
1600 p_header->pAppPrivate = 0;
1603 #ifdef OMXIL_EXTRA_DEBUG
1604 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1606 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1609 msg_Dbg(p_dec, "done");
1613 /*****************************************************************************
1614 * CloseGeneric: omxil decoder destruction
1615 *****************************************************************************/
1616 static void CloseGeneric( vlc_object_t *p_this )
1618 decoder_t *p_dec = (decoder_t *)p_this;
1619 decoder_sys_t *p_sys = p_dec->p_sys;
1621 if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1622 vlc_mutex_lock( &omx_core_mutex );
1624 if( omx_refcount == 0 )
1626 if( p_sys->b_init ) pf_deinit();
1627 dll_close( dll_handle );
1629 vlc_mutex_unlock( &omx_core_mutex );
1631 vlc_mutex_destroy (&p_sys->mutex);
1632 vlc_cond_destroy (&p_sys->cond);
1633 vlc_mutex_destroy (&p_sys->in.fifo.lock);
1634 vlc_cond_destroy (&p_sys->in.fifo.wait);
1635 vlc_mutex_destroy (&p_sys->out.fifo.lock);
1636 vlc_cond_destroy (&p_sys->out.fifo.wait);
1641 /*****************************************************************************
1643 *****************************************************************************/
1644 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1645 OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1646 OMX_U32 data_2, OMX_PTR event_data )
1648 decoder_t *p_dec = (decoder_t *)app_data;
1649 decoder_sys_t *p_sys = p_dec->p_sys;
1655 case OMX_EventCmdComplete:
1656 switch ((OMX_STATETYPE)data_1)
1658 case OMX_CommandStateSet:
1659 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %s)", EventToString(event),
1660 CommandToString(data_1), StateToString(data_2) );
1664 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u)", EventToString(event),
1665 CommandToString(data_1), (unsigned int)data_2 );
1670 case OMX_EventError:
1671 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u, %s)", EventToString(event),
1672 ErrorToString((OMX_ERRORTYPE)data_1), (unsigned int)data_2,
1673 (const char *)event_data);
1674 //p_sys->b_error = true;
1677 case OMX_EventPortSettingsChanged:
1678 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1679 (unsigned int)data_1, (unsigned int)data_2 );
1680 if( data_2 == 0 || data_2 == OMX_IndexParamPortDefinition )
1682 OMX_BUFFERHEADERTYPE *sentinel;
1683 for(i = 0; i < p_sys->ports; i++)
1684 if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1685 p_sys->p_ports[i].b_reconfigure = true;
1686 sentinel = calloc(1, sizeof(*sentinel));
1688 sentinel->nFlags = SENTINEL_FLAG;
1689 OMX_FIFO_PUT(&p_sys->in.fifo, sentinel);
1692 else if( data_2 == OMX_IndexConfigCommonOutputCrop )
1694 for(i = 0; i < p_sys->ports; i++)
1695 if(p_sys->p_ports[i].definition.nPortIndex == data_1)
1696 p_sys->p_ports[i].b_update_def = true;
1700 msg_Dbg( p_dec, "Unhandled setting change %x", (unsigned int)data_2 );
1705 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1706 (unsigned int)data_1, (unsigned int)data_2 );
1710 PostOmxEvent(p_dec, event, data_1, data_2, event_data);
1711 return OMX_ErrorNone;
1714 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1715 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1717 decoder_t *p_dec = (decoder_t *)app_data;
1718 decoder_sys_t *p_sys = p_dec->p_sys;
1721 #ifdef OMXIL_EXTRA_DEBUG
1722 msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1725 if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1727 block_t *p_block = (block_t *)omx_header->pAppPrivate;
1728 omx_header->pBuffer = omx_header->pOutputPortPrivate;
1729 if(p_block) block_Release(p_block);
1730 omx_header->pAppPrivate = 0;
1732 OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1734 return OMX_ErrorNone;
1737 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1738 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1740 decoder_t *p_dec = (decoder_t *)app_data;
1741 decoder_sys_t *p_sys = p_dec->p_sys;
1744 #ifdef OMXIL_EXTRA_DEBUG
1745 msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
1746 (int)omx_header->nFilledLen );
1749 if(omx_header->pInputPortPrivate)
1751 omx_header->pBuffer = omx_header->pInputPortPrivate;
1753 OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1755 return OMX_ErrorNone;