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 p_fmt->video.i_width = crop_rect.nWidth;
464 p_fmt->video.i_visible_width = crop_rect.nWidth;
465 p_fmt->video.i_height = crop_rect.nHeight;
466 p_fmt->video.i_visible_height = crop_rect.nHeight;
467 if (def->format.video.eColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
468 def->format.video.nSliceHeight -= crop_rect.nTop/2;
472 /* Don't pass the error back to the caller, this isn't mandatory */
473 omx_error = OMX_ErrorNone;
476 /* Hack: Nexus One (stock firmware with binary OMX driver blob)
477 * claims to output 420Planar even though it in in practice is
479 if(def->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar &&
480 !strncmp(p_sys->psz_component, "OMX.qcom.video.decoder",
481 strlen("OMX.qcom.video.decoder")))
482 def->format.video.eColorFormat = OMX_QCOM_COLOR_FormatYVU420SemiPlanar;
484 /* Hack: Galaxy S II (stock firmware) gives a slice height larger
485 * than the video height, but this doesn't imply padding between
486 * the video planes. Nexus S also has a slice height larger than
487 * the video height, but there it actually is real padding, thus
488 * Galaxy S II is the buggy one. The Galaxy S II decoder is
489 * named OMX.SEC.avcdec while the one on Nexus S is
490 * OMX.SEC.AVC.Decoder. Thus do this for any OMX.SEC. that don't
491 * contain the string ".Decoder". */
492 if(!strncmp(p_sys->psz_component, "OMX.SEC.", strlen("OMX.SEC.")) &&
493 !strstr(p_sys->psz_component, ".Decoder"))
494 def->format.video.nSliceHeight = 0;
496 if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
497 &p_fmt->i_codec, 0 ) )
499 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
500 &p_fmt->i_codec, 0 ) )
502 omx_error = OMX_ErrorNotImplemented;
503 CHECK_ERROR(omx_error, "OMX color format %i not supported",
504 (int)def->format.video.eColorFormat );
506 GetVlcChromaSizes( p_fmt->i_codec,
507 def->format.video.nFrameWidth,
508 def->format.video.nFrameHeight,
509 &p_port->i_frame_size, &p_port->i_frame_stride,
510 &p_port->i_frame_stride_chroma_div );
512 if(p_port->i_frame_size > def->nBufferSize)
513 def->nBufferSize = p_port->i_frame_size;
514 p_port->i_frame_size = def->nBufferSize;
516 if((int)p_port->i_frame_stride > def->format.video.nStride)
517 def->format.video.nStride = p_port->i_frame_stride;
519 p_port->i_frame_stride = def->format.video.nStride;
523 if( !OmxToVlcAudioFormat( def->format.audio.eEncoding,
524 &p_fmt->i_codec, 0 ) )
526 omx_error = OMX_ErrorNotImplemented;
527 CHECK_ERROR(omx_error, "OMX audio format %i not supported",
528 (int)def->format.audio.eEncoding );
531 omx_error = GetAudioParameters(p_port->omx_handle,
532 &p_port->format_param, def->nPortIndex,
533 def->format.audio.eEncoding,
534 &p_fmt->audio.i_channels,
535 &p_fmt->audio.i_rate,
537 &p_fmt->audio.i_bitspersample,
538 &p_fmt->audio.i_blockalign);
539 CHECK_ERROR(omx_error, "GetAudioParameters failed (%x : %s)",
540 omx_error, ErrorToString(omx_error));
542 if(p_fmt->audio.i_channels < 9)
544 static const int pi_channels_maps[9] =
546 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
547 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
548 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
549 | AOUT_CHAN_REARRIGHT,
550 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
551 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
552 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
553 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
554 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
555 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
556 | AOUT_CHAN_MIDDLERIGHT,
557 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
558 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
561 p_fmt->audio.i_physical_channels =
562 p_fmt->audio.i_original_channels =
563 pi_channels_maps[p_fmt->audio.i_channels];
566 date_Init( &p_dec->p_sys->end_date, p_fmt->audio.i_rate, 1 );
570 default: return OMX_ErrorNotImplemented;
577 /*****************************************************************************
578 * DeinitialiseComponent: Deinitialise and unload an OMX component
579 *****************************************************************************/
580 static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
581 OMX_HANDLETYPE omx_handle)
583 decoder_sys_t *p_sys = p_dec->p_sys;
584 OMX_ERRORTYPE omx_error;
588 if(!omx_handle) return OMX_ErrorNone;
590 omx_error = OMX_GetState(omx_handle, &state);
591 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
593 if(state == OMX_StateExecuting)
595 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
597 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
598 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
599 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
602 omx_error = OMX_GetState(omx_handle, &state);
603 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
605 if(state == OMX_StateIdle)
607 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
608 OMX_StateLoaded, 0 );
609 CHECK_ERROR(omx_error, "OMX_CommandStateSet Loaded failed (%x)", omx_error );
611 for(i = 0; i < p_sys->ports; i++)
613 OmxPort *p_port = &p_sys->p_ports[i];
614 OMX_BUFFERHEADERTYPE *p_buffer;
616 for(j = 0; j < p_port->i_buffers; j++)
618 OMX_FIFO_GET(&p_port->fifo, p_buffer);
619 if (p_buffer->nFlags & SENTINEL_FLAG) {
624 omx_error = OMX_FreeBuffer( omx_handle,
625 p_port->i_port_index, p_buffer );
627 if(omx_error != OMX_ErrorNone) break;
629 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
630 omx_error, (int)p_port->i_port_index, j );
632 OMX_FIFO_PEEK(&p_port->fifo, p_buffer);
633 if (!p_buffer) break;
635 OMX_FIFO_GET(&p_port->fifo, p_buffer);
636 if (p_buffer->nFlags & SENTINEL_FLAG) {
640 msg_Warn( p_dec, "Stray buffer left in fifo, %p", p_buffer );
644 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
645 CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
649 for(i = 0; i < p_sys->ports; i++)
651 OmxPort *p_port = &p_sys->p_ports[i];
652 free(p_port->pp_buffers);
653 p_port->pp_buffers = 0;
655 omx_error = pf_free_handle( omx_handle );
659 /*****************************************************************************
660 * InitialiseComponent: Load and initialise an OMX component
661 *****************************************************************************/
662 static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
663 OMX_STRING psz_component, OMX_HANDLETYPE *p_handle)
665 static OMX_CALLBACKTYPE callbacks =
666 { OmxEventHandler, OmxEmptyBufferDone, OmxFillBufferDone };
667 decoder_sys_t *p_sys = p_dec->p_sys;
668 OMX_HANDLETYPE omx_handle;
669 OMX_ERRORTYPE omx_error;
671 OMX_U8 psz_role[OMX_MAX_STRINGNAME_SIZE];
672 OMX_PARAM_COMPONENTROLETYPE role;
673 OMX_PARAM_PORTDEFINITIONTYPE definition;
674 OMX_PORT_PARAM_TYPE param;
677 omx_error = pf_get_handle( &omx_handle, psz_component, p_dec, &callbacks );
678 if(omx_error != OMX_ErrorNone)
680 msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
681 omx_error, ErrorToString(omx_error) );
684 strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
686 omx_error = OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
687 if(omx_error == OMX_ErrorNone)
688 msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
690 msg_Dbg(p_dec, "loaded component %s", psz_component);
691 PrintOmx(p_dec, omx_handle, OMX_ALL);
693 /* Set component role */
694 OMX_INIT_STRUCTURE(role);
695 strcpy((char*)role.cRole,
696 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec : p_dec->fmt_in.i_codec,
697 p_dec->fmt_in.i_cat, p_sys->b_enc));
699 omx_error = OMX_SetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
701 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
703 if(omx_error == OMX_ErrorNone)
704 msg_Dbg(p_dec, "component standard role set to %s", role.cRole);
706 /* Find the input / output ports */
707 OMX_INIT_STRUCTURE(param);
708 OMX_INIT_STRUCTURE(definition);
709 omx_error = OMX_GetParameter(omx_handle, p_dec->fmt_in.i_cat == VIDEO_ES ?
710 OMX_IndexParamVideoInit : OMX_IndexParamAudioInit, ¶m);
711 if(omx_error != OMX_ErrorNone) param.nPorts = 0;
713 for(i = 0; i < param.nPorts; i++)
717 /* Get port definition */
718 definition.nPortIndex = param.nStartPortNumber + i;
719 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
721 if(omx_error != OMX_ErrorNone) continue;
723 if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
724 else p_port = &p_sys->out;
726 p_port->b_valid = true;
727 p_port->i_port_index = definition.nPortIndex;
728 p_port->definition = definition;
729 p_port->omx_handle = omx_handle;
732 if(!p_sys->in.b_valid || !p_sys->out.b_valid)
734 omx_error = OMX_ErrorInvalidComponent;
735 CHECK_ERROR(omx_error, "couldn't find an input and output port");
738 if(!strncmp(p_sys->psz_component, "OMX.SEC.", 8))
741 omx_error = OMX_GetExtensionIndex(omx_handle, (OMX_STRING) "OMX.SEC.index.ThumbnailMode", &index);
742 if(omx_error == OMX_ErrorNone)
744 OMX_BOOL enable = OMX_TRUE;
745 omx_error = OMX_SetConfig(omx_handle, index, &enable);
746 CHECK_ERROR(omx_error, "Unable to set ThumbnailMode");
748 OMX_BOOL enable = OMX_TRUE;
749 /* Needed on Samsung Galaxy S II */
750 omx_error = OMX_SetConfig(omx_handle, OMX_IndexVendorSetYUV420pMode, &enable);
751 if (omx_error == OMX_ErrorNone)
752 msg_Dbg(p_dec, "Set OMX_IndexVendorSetYUV420pMode successfully");
754 msg_Dbg(p_dec, "Unable to set OMX_IndexVendorSetYUV420pMode: %x", omx_error);
758 /* Set port definitions */
759 for(i = 0; i < p_sys->ports; i++)
761 omx_error = SetPortDefinition(p_dec, &p_sys->p_ports[i],
762 p_sys->p_ports[i].p_fmt);
763 if(omx_error != OMX_ErrorNone) goto error;
766 /* Allocate our array for the omx buffers and enable ports */
767 for(i = 0; i < p_sys->ports; i++)
769 OmxPort *p_port = &p_sys->p_ports[i];
772 malloc(p_port->definition.nBufferCountActual *
773 sizeof(OMX_BUFFERHEADERTYPE*));
774 if(!p_port->pp_buffers)
776 omx_error = OMX_ErrorInsufficientResources;
777 CHECK_ERROR(omx_error, "memory allocation failed");
779 p_port->i_buffers = p_port->definition.nBufferCountActual;
782 if(!p_port->definition.bEnabled)
784 omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
785 p_port->i_port_index, NULL);
786 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
787 (int)p_port->i_port_index, omx_error );
788 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
789 CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
790 (int)p_port->i_port_index, omx_error );
794 *p_handle = omx_handle;
795 return OMX_ErrorNone;
798 DeinitialiseComponent(p_dec, omx_handle);
803 /*****************************************************************************
804 * OpenDecoder: Create the decoder instance
805 *****************************************************************************/
806 static int OpenDecoder( vlc_object_t *p_this )
808 decoder_t *p_dec = (decoder_t*)p_this;
811 if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
815 if( p_dec->fmt_in.i_cat != VIDEO_ES && !p_dec->b_force)
819 status = OpenGeneric( p_this, false );
820 if(status != VLC_SUCCESS) return status;
822 p_dec->pf_decode_video = DecodeVideo;
823 p_dec->pf_decode_audio = DecodeAudio;
828 /*****************************************************************************
829 * OpenEncoder: Create the encoder instance
830 *****************************************************************************/
831 static int OpenEncoder( vlc_object_t *p_this )
833 encoder_t *p_enc = (encoder_t*)p_this;
836 if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
839 status = OpenGeneric( p_this, true );
840 if(status != VLC_SUCCESS) return status;
842 p_enc->pf_encode_video = EncodeVideo;
847 /*****************************************************************************
848 * OpenGeneric: Create the generic decoder/encoder instance
849 *****************************************************************************/
850 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
852 decoder_t *p_dec = (decoder_t*)p_this;
853 decoder_sys_t *p_sys;
854 OMX_ERRORTYPE omx_error;
855 OMX_BUFFERHEADERTYPE *p_header;
858 vlc_mutex_lock( &omx_core_mutex );
859 if( omx_refcount > 0 )
862 /* Load the OMX core */
863 for( i = 0; ppsz_dll_list[i]; i++ )
865 dll_handle = dll_open( ppsz_dll_list[i] );
866 if( dll_handle ) break;
870 vlc_mutex_unlock( &omx_core_mutex );
874 pf_init = dlsym( dll_handle, "OMX_Init" );
875 pf_deinit = dlsym( dll_handle, "OMX_Deinit" );
876 pf_get_handle = dlsym( dll_handle, "OMX_GetHandle" );
877 pf_free_handle = dlsym( dll_handle, "OMX_FreeHandle" );
878 pf_component_enum = dlsym( dll_handle, "OMX_ComponentNameEnum" );
879 pf_get_roles_of_component = dlsym( dll_handle, "OMX_GetRolesOfComponent" );
880 if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle ||
881 !pf_component_enum || !pf_get_roles_of_component )
883 msg_Warn( p_this, "cannot find OMX_* symbols in `%s' (%s)",
884 ppsz_dll_list[i], dlerror() );
885 dll_close(dll_handle);
886 vlc_mutex_unlock( &omx_core_mutex );
891 /* Allocate the memory needed to store the decoder's structure */
892 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
894 if( omx_refcount == 0 )
895 dll_close(dll_handle);
896 vlc_mutex_unlock( &omx_core_mutex );
900 /* Initialise the thread properties */
903 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
904 p_dec->fmt_out.video = p_dec->fmt_in.video;
905 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
906 p_dec->fmt_out.i_codec = 0;
908 p_sys->b_enc = b_encode;
909 p_sys->pp_last_event = &p_sys->p_events;
910 vlc_mutex_init (&p_sys->mutex);
911 vlc_cond_init (&p_sys->cond);
912 vlc_mutex_init (&p_sys->lock);
913 vlc_mutex_init (&p_sys->in.fifo.lock);
914 vlc_cond_init (&p_sys->in.fifo.wait);
915 p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
916 p_sys->in.fifo.pp_last = &p_sys->in.fifo.p_first;
917 p_sys->in.b_direct = false;
918 p_sys->in.b_flushed = true;
919 p_sys->in.p_fmt = &p_dec->fmt_in;
920 vlc_mutex_init (&p_sys->out.fifo.lock);
921 vlc_cond_init (&p_sys->out.fifo.wait);
922 p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
923 p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
924 p_sys->out.b_direct = true;
925 p_sys->out.b_flushed = true;
926 p_sys->out.p_fmt = &p_dec->fmt_out;
928 p_sys->p_ports = &p_sys->in;
929 p_sys->b_use_pts = 0;
931 msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
932 (char *)&p_dec->fmt_out.i_codec);
934 /* Initialise the OMX core */
935 omx_error = omx_refcount > 0 ? OMX_ErrorNone : pf_init();
937 if(omx_error != OMX_ErrorNone)
939 msg_Warn( p_this, "OMX_Init failed (%x: %s)", omx_error,
940 ErrorToString(omx_error) );
941 vlc_mutex_unlock( &omx_core_mutex );
942 CloseGeneric(p_this);
945 p_sys->b_init = true;
946 vlc_mutex_unlock( &omx_core_mutex );
948 /* Enumerate components and build a list of the one we want to try */
949 if( !CreateComponentsList(p_dec,
950 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
951 p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
954 msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
955 (char *)&p_dec->fmt_in.i_codec );
956 CloseGeneric(p_this);
960 /* Try to load and initialise a component */
961 omx_error = OMX_ErrorUndefined;
962 for(i = 0; i < p_sys->components; i++)
965 /* ignore OpenCore software codecs */
966 if (!strncmp(p_sys->ppsz_components[i], "OMX.PV.", 7))
968 /* The same sw codecs, renamed in ICS (perhaps also in honeycomb) */
969 if (!strncmp(p_sys->ppsz_components[i], "OMX.google.", 11))
972 omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
974 if(omx_error == OMX_ErrorNone) break;
976 CHECK_ERROR(omx_error, "no component could be initialised" );
978 /* Move component to Idle then Executing state */
979 OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
980 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
982 /* Allocate omx buffers */
983 for(i = 0; i < p_sys->ports; i++)
985 OmxPort *p_port = &p_sys->p_ports[i];
987 for(j = 0; j < p_port->i_buffers; j++)
990 #define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
991 char *p_buf = malloc(p_port->definition.nBufferSize +
992 p_port->definition.nBufferAlignment);
993 p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
996 if(0 && p_port->b_direct)
998 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
999 p_port->i_port_index, 0,
1000 p_port->definition.nBufferSize, (void*)1);
1003 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
1004 p_port->i_port_index, 0,
1005 p_port->definition.nBufferSize);
1007 if(omx_error != OMX_ErrorNone) break;
1008 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
1010 p_port->i_buffers = j;
1011 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1012 omx_error, (int)p_port->i_port_index, j );
1015 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1016 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
1018 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
1019 OMX_StateExecuting, 0);
1020 CHECK_ERROR(omx_error, "OMX_CommandStateSet Executing failed (%x)", omx_error );
1021 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1022 CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
1024 /* Send codec configuration data */
1025 if( p_dec->fmt_in.i_extra )
1027 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1028 p_header->nFilledLen = p_dec->fmt_in.i_extra;
1030 /* Convert H.264 NAL format to annex b */
1031 if( p_sys->i_nal_size_length && !p_sys->in.b_direct )
1033 p_header->nFilledLen = 0;
1034 convert_sps_pps( p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
1035 p_header->pBuffer, p_header->nAllocLen,
1036 (uint32_t*) &p_header->nFilledLen, NULL );
1038 else if(p_sys->in.b_direct)
1040 p_header->pOutputPortPrivate = p_header->pBuffer;
1041 p_header->pBuffer = p_dec->fmt_in.p_extra;
1045 if(p_header->nFilledLen > p_header->nAllocLen)
1047 msg_Dbg(p_dec, "buffer too small (%i,%i)", (int)p_header->nFilledLen,
1048 (int)p_header->nAllocLen);
1049 p_header->nFilledLen = p_header->nAllocLen;
1051 memcpy(p_header->pBuffer, p_dec->fmt_in.p_extra, p_header->nFilledLen);
1054 p_header->nOffset = 0;
1055 p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG;
1056 msg_Dbg(p_dec, "sending codec config data %p, %p, %i", p_header,
1057 p_header->pBuffer, (int)p_header->nFilledLen);
1058 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1061 /* Get back output port definition */
1062 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1063 if(omx_error != OMX_ErrorNone) goto error;
1065 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1066 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1068 if(p_sys->b_error) goto error;
1070 p_dec->b_need_packetized = true;
1071 if (!strcmp(p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER"))
1072 p_sys->b_use_pts = 1;
1076 CloseGeneric(p_this);
1077 return VLC_EGENERIC;
1080 /*****************************************************************************
1082 *****************************************************************************/
1083 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
1085 decoder_sys_t *p_sys = p_dec->p_sys;
1086 OMX_PARAM_PORTDEFINITIONTYPE definition;
1087 OMX_BUFFERHEADERTYPE *p_buffer;
1088 OMX_ERRORTYPE omx_error;
1091 /* Sanity checking */
1092 OMX_INIT_STRUCTURE(definition);
1093 definition.nPortIndex = p_port->i_port_index;
1094 omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1096 if(omx_error != OMX_ErrorNone || (p_dec->fmt_in.i_cat == VIDEO_ES &&
1097 (!definition.format.video.nFrameWidth ||
1098 !definition.format.video.nFrameHeight)) )
1099 return OMX_ErrorUndefined;
1101 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
1102 p_port->i_port_index, NULL);
1103 CHECK_ERROR(omx_error, "OMX_CommandPortDisable on %i failed (%x)",
1104 (int)p_port->i_port_index, omx_error );
1106 for(i = 0; i < p_port->i_buffers; i++)
1108 OMX_FIFO_GET(&p_port->fifo, p_buffer);
1109 if (p_buffer->nFlags & SENTINEL_FLAG) {
1114 omx_error = OMX_FreeBuffer( p_sys->omx_handle,
1115 p_port->i_port_index, p_buffer );
1117 if(omx_error != OMX_ErrorNone) break;
1119 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
1120 omx_error, (int)p_port->i_port_index, i );
1122 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1123 CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
1125 /* Get the new port definition */
1126 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1127 if(omx_error != OMX_ErrorNone) goto error;
1129 if( p_dec->fmt_in.i_cat != AUDIO_ES )
1131 /* Don't explicitly set the new parameters that we got with
1132 * OMX_GetParameter above when using audio codecs.
1133 * That struct hasn't been changed since, so there should be
1134 * no need to set it here, unless some codec expects the
1135 * SetParameter call as a trigger event for some part of
1136 * the reconfiguration.
1137 * This fixes using audio decoders on Samsung Galaxy S II,
1139 * Only skipping this for audio codecs, to minimize the
1140 * change for current working configurations for video.
1142 omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1144 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
1145 omx_error, ErrorToString(omx_error));
1148 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
1149 p_port->i_port_index, NULL);
1150 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
1151 (int)p_port->i_port_index, omx_error );
1153 if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
1154 free(p_port->pp_buffers);
1155 p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
1156 if(!p_port->pp_buffers)
1158 omx_error = OMX_ErrorInsufficientResources;
1159 CHECK_ERROR(omx_error, "memory allocation failed");
1162 p_port->i_buffers = p_port->definition.nBufferCountActual;
1163 for(i = 0; i < p_port->i_buffers; i++)
1165 if(0 && p_port->b_direct)
1167 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1168 p_port->i_port_index, 0,
1169 p_port->definition.nBufferSize, (void*)1);
1172 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1173 p_port->i_port_index, 0,
1174 p_port->definition.nBufferSize);
1176 if(omx_error != OMX_ErrorNone) break;
1177 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1179 p_port->i_buffers = i;
1180 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1181 omx_error, (int)p_port->i_port_index, i );
1183 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1184 CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1186 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1187 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1193 /*****************************************************************************
1194 * DecodeVideo: Called to decode one frame
1195 *****************************************************************************/
1196 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1198 decoder_sys_t *p_sys = p_dec->p_sys;
1199 picture_t *p_pic = NULL, *p_next_pic;
1200 OMX_ERRORTYPE omx_error;
1203 OMX_BUFFERHEADERTYPE *p_header;
1206 if( !pp_block || !*pp_block )
1209 p_block = *pp_block;
1211 /* Check for errors from codec */
1214 msg_Dbg(p_dec, "error during decoding");
1215 block_Release( p_block );
1219 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1221 block_Release( p_block );
1222 if(!p_sys->in.b_flushed)
1224 msg_Dbg(p_dec, "flushing");
1225 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1226 p_sys->in.definition.nPortIndex, 0 );
1228 p_sys->in.b_flushed = true;
1232 /* Take care of decoded frames first */
1235 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1236 if(!p_header) break; /* No frame available */
1238 if(p_sys->out.b_update_def)
1240 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1241 p_sys->out.b_update_def = 0;
1244 if(p_header->nFilledLen)
1246 p_pic = p_header->pAppPrivate;
1249 /* We're not in direct rendering mode.
1250 * Get a new picture and copy the content */
1251 p_pic = decoder_NewPicture( p_dec );
1254 CopyOmxPicture(p_dec, p_pic, p_header, p_sys->out.definition.format.video.nSliceHeight);
1258 p_pic->date = p_header->nTimeStamp;
1259 p_header->nFilledLen = 0;
1260 p_header->pAppPrivate = 0;
1263 /* Get a new picture */
1264 if(p_sys->in.b_direct && !p_header->pAppPrivate)
1266 p_next_pic = decoder_NewPicture( p_dec );
1267 if(!p_next_pic) break;
1269 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1270 p_header->pAppPrivate = p_next_pic;
1271 p_header->pInputPortPrivate = p_header->pBuffer;
1272 p_header->pBuffer = p_next_pic->p[0].p_pixels;
1276 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1279 #ifdef OMXIL_EXTRA_DEBUG
1280 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1282 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1285 /* Send the input buffer to the component */
1286 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1288 if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1295 p_header->nFilledLen = p_block->i_buffer;
1296 p_header->nOffset = 0;
1297 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1298 if (p_sys->b_use_pts)
1299 p_header->nTimeStamp = p_block->i_pts;
1301 p_header->nTimeStamp = p_block->i_dts;
1303 /* In direct mode we pass the input pointer as is.
1304 * Otherwise we memcopy the data */
1305 if(p_sys->in.b_direct)
1307 p_header->pOutputPortPrivate = p_header->pBuffer;
1308 p_header->pBuffer = p_block->p_buffer;
1309 p_header->pAppPrivate = p_block;
1313 if(p_header->nFilledLen > p_header->nAllocLen)
1315 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1316 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1317 p_header->nFilledLen = p_header->nAllocLen;
1319 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1320 block_Release(p_block);
1323 /* Convert H.264 NAL format to annex b */
1324 if( p_sys->i_nal_size_length >= 3 && p_sys->i_nal_size_length <= 4 )
1326 /* This only works for NAL sizes 3-4 */
1327 int i_len = p_header->nFilledLen, i;
1328 uint8_t* ptr = p_header->pBuffer;
1329 while( i_len >= p_sys->i_nal_size_length )
1331 uint32_t nal_len = 0;
1332 for( i = 0; i < p_sys->i_nal_size_length; i++ ) {
1333 nal_len = (nal_len << 8) | ptr[i];
1336 ptr[p_sys->i_nal_size_length - 1] = 1;
1337 if( nal_len > INT_MAX || nal_len > (unsigned int) i_len )
1340 i_len -= nal_len + 4;
1343 #ifdef OMXIL_EXTRA_DEBUG
1344 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1345 (int)p_header->nFilledLen );
1347 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1348 p_sys->in.b_flushed = false;
1349 *pp_block = NULL; /* Avoid being fed the same packet again */
1353 /* Handle the PortSettingsChanged events */
1354 for(i = 0; i < p_sys->ports; i++)
1356 OmxPort *p_port = &p_sys->p_ports[i];
1357 if(p_port->b_reconfigure)
1359 omx_error = PortReconfigure(p_dec, p_port);
1360 p_port->b_reconfigure = 0;
1362 if(p_port->b_update_def)
1364 omx_error = GetPortDefinition(p_dec, p_port, p_port->p_fmt);
1365 p_port->b_update_def = 0;
1372 /*****************************************************************************
1373 * DecodeAudio: Called to decode one frame
1374 *****************************************************************************/
1375 aout_buffer_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1377 decoder_sys_t *p_sys = p_dec->p_sys;
1378 aout_buffer_t *p_buffer = 0;
1379 OMX_BUFFERHEADERTYPE *p_header;
1380 OMX_ERRORTYPE omx_error;
1384 if( !pp_block || !*pp_block ) return NULL;
1386 p_block = *pp_block;
1388 /* Check for errors from codec */
1391 msg_Dbg(p_dec, "error during decoding");
1392 block_Release( p_block );
1396 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1398 block_Release( p_block );
1399 date_Set( &p_sys->end_date, 0 );
1400 if(!p_sys->in.b_flushed)
1402 msg_Dbg(p_dec, "flushing");
1403 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1404 p_sys->in.definition.nPortIndex, 0 );
1406 p_sys->in.b_flushed = true;
1410 if( !date_Get( &p_sys->end_date ) )
1412 if( !p_block->i_pts )
1414 /* We've just started the stream, wait for the first PTS. */
1415 block_Release( p_block );
1418 date_Set( &p_sys->end_date, p_block->i_pts );
1421 /* Take care of decoded frames first */
1424 unsigned int i_samples;
1426 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1427 if(!p_header) break; /* No frame available */
1429 i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1432 p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1433 if( !p_buffer ) break; /* No audio buffer available */
1435 memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1436 p_header->nFilledLen = 0;
1438 if( p_header->nTimeStamp != 0 &&
1439 p_header->nTimeStamp != date_Get( &p_sys->end_date ) )
1440 date_Set( &p_sys->end_date, p_header->nTimeStamp );
1442 p_buffer->i_pts = date_Get( &p_sys->end_date );
1443 p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1447 #ifdef OMXIL_EXTRA_DEBUG
1448 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1450 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1451 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1455 /* Send the input buffer to the component */
1456 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1458 if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1465 p_header->nFilledLen = p_block->i_buffer;
1466 p_header->nOffset = 0;
1467 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1468 p_header->nTimeStamp = p_block->i_dts;
1470 /* In direct mode we pass the input pointer as is.
1471 * Otherwise we memcopy the data */
1472 if(p_sys->in.b_direct)
1474 p_header->pOutputPortPrivate = p_header->pBuffer;
1475 p_header->pBuffer = p_block->p_buffer;
1476 p_header->pAppPrivate = p_block;
1480 if(p_header->nFilledLen > p_header->nAllocLen)
1482 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1483 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1484 p_header->nFilledLen = p_header->nAllocLen;
1486 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1487 block_Release(p_block);
1490 #ifdef OMXIL_EXTRA_DEBUG
1491 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1492 (int)p_header->nFilledLen );
1494 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1495 p_sys->in.b_flushed = false;
1496 *pp_block = NULL; /* Avoid being fed the same packet again */
1500 /* Handle the PortSettingsChanged events */
1501 for(i = 0; i < p_sys->ports; i++)
1503 OmxPort *p_port = &p_sys->p_ports[i];
1504 if(!p_port->b_reconfigure) continue;
1505 p_port->b_reconfigure = 0;
1506 omx_error = PortReconfigure(p_dec, p_port);
1512 /*****************************************************************************
1513 * EncodeVideo: Called to encode one frame
1514 *****************************************************************************/
1515 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1517 decoder_t *p_dec = ( decoder_t *)p_enc;
1518 decoder_sys_t *p_sys = p_dec->p_sys;
1519 OMX_ERRORTYPE omx_error;
1522 OMX_BUFFERHEADERTYPE *p_header;
1523 block_t *p_block = 0;
1525 if( !p_pic ) return NULL;
1527 /* Check for errors from codec */
1530 msg_Dbg(p_dec, "error during encoding");
1534 /* Send the input buffer to the component */
1535 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1538 /* In direct mode we pass the input pointer as is.
1539 * Otherwise we memcopy the data */
1540 if(p_sys->in.b_direct)
1542 p_header->pOutputPortPrivate = p_header->pBuffer;
1543 p_header->pBuffer = p_pic->p[0].p_pixels;
1547 CopyVlcPicture(p_dec, p_header, p_pic);
1550 p_header->nFilledLen = p_sys->in.i_frame_size;
1551 p_header->nOffset = 0;
1552 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1553 p_header->nTimeStamp = p_pic->date;
1554 #ifdef OMXIL_EXTRA_DEBUG
1555 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1556 (int)p_header->nFilledLen );
1558 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1559 p_sys->in.b_flushed = false;
1562 /* Handle the PortSettingsChanged events */
1563 for(i = 0; i < p_sys->ports; i++)
1565 OmxPort *p_port = &p_sys->p_ports[i];
1566 if(!p_port->b_reconfigure) continue;
1567 p_port->b_reconfigure = 0;
1568 omx_error = PortReconfigure(p_dec, p_port);
1571 /* Wait for the decoded frame */
1574 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1576 if(p_header->nFilledLen)
1578 if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1580 /* TODO: need to store codec config */
1581 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1584 p_block = p_header->pAppPrivate;
1587 /* We're not in direct rendering mode.
1588 * Get a new block and copy the content */
1589 p_block = block_New( p_dec, p_header->nFilledLen );
1590 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1593 p_block->i_buffer = p_header->nFilledLen;
1594 p_block->i_pts = p_block->i_dts = p_header->nTimeStamp;
1595 p_header->nFilledLen = 0;
1596 p_header->pAppPrivate = 0;
1599 #ifdef OMXIL_EXTRA_DEBUG
1600 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1602 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1605 msg_Dbg(p_dec, "done");
1609 /*****************************************************************************
1610 * CloseGeneric: omxil decoder destruction
1611 *****************************************************************************/
1612 static void CloseGeneric( vlc_object_t *p_this )
1614 decoder_t *p_dec = (decoder_t *)p_this;
1615 decoder_sys_t *p_sys = p_dec->p_sys;
1617 if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1618 vlc_mutex_lock( &omx_core_mutex );
1620 if( omx_refcount == 0 )
1622 if( p_sys->b_init ) pf_deinit();
1623 dll_close( dll_handle );
1625 vlc_mutex_unlock( &omx_core_mutex );
1627 vlc_mutex_destroy (&p_sys->mutex);
1628 vlc_cond_destroy (&p_sys->cond);
1629 vlc_mutex_destroy (&p_sys->in.fifo.lock);
1630 vlc_cond_destroy (&p_sys->in.fifo.wait);
1631 vlc_mutex_destroy (&p_sys->out.fifo.lock);
1632 vlc_cond_destroy (&p_sys->out.fifo.wait);
1637 /*****************************************************************************
1639 *****************************************************************************/
1640 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1641 OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1642 OMX_U32 data_2, OMX_PTR event_data )
1644 decoder_t *p_dec = (decoder_t *)app_data;
1645 decoder_sys_t *p_sys = p_dec->p_sys;
1651 case OMX_EventCmdComplete:
1652 switch ((OMX_STATETYPE)data_1)
1654 case OMX_CommandStateSet:
1655 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %s)", EventToString(event),
1656 CommandToString(data_1), StateToString(data_2) );
1660 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u)", EventToString(event),
1661 CommandToString(data_1), (unsigned int)data_2 );
1666 case OMX_EventError:
1667 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u, %s)", EventToString(event),
1668 ErrorToString((OMX_ERRORTYPE)data_1), (unsigned int)data_2,
1669 (const char *)event_data);
1670 //p_sys->b_error = true;
1673 case OMX_EventPortSettingsChanged:
1674 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1675 (unsigned int)data_1, (unsigned int)data_2 );
1676 if( data_2 == 0 || data_2 == OMX_IndexParamPortDefinition )
1678 OMX_BUFFERHEADERTYPE *sentinel;
1679 for(i = 0; i < p_sys->ports; i++)
1680 if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1681 p_sys->p_ports[i].b_reconfigure = true;
1682 sentinel = calloc(1, sizeof(*sentinel));
1684 sentinel->nFlags = SENTINEL_FLAG;
1685 OMX_FIFO_PUT(&p_sys->in.fifo, sentinel);
1688 else if( data_2 == OMX_IndexConfigCommonOutputCrop )
1690 for(i = 0; i < p_sys->ports; i++)
1691 if(p_sys->p_ports[i].definition.nPortIndex == data_1)
1692 p_sys->p_ports[i].b_update_def = true;
1696 msg_Dbg( p_dec, "Unhandled setting change %x", (unsigned int)data_2 );
1701 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1702 (unsigned int)data_1, (unsigned int)data_2 );
1706 PostOmxEvent(p_dec, event, data_1, data_2, event_data);
1707 return OMX_ErrorNone;
1710 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1711 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1713 decoder_t *p_dec = (decoder_t *)app_data;
1714 decoder_sys_t *p_sys = p_dec->p_sys;
1717 #ifdef OMXIL_EXTRA_DEBUG
1718 msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1721 if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1723 block_t *p_block = (block_t *)omx_header->pAppPrivate;
1724 omx_header->pBuffer = omx_header->pOutputPortPrivate;
1725 if(p_block) block_Release(p_block);
1726 omx_header->pAppPrivate = 0;
1728 OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1730 return OMX_ErrorNone;
1733 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1734 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1736 decoder_t *p_dec = (decoder_t *)app_data;
1737 decoder_sys_t *p_sys = p_dec->p_sys;
1740 #ifdef OMXIL_EXTRA_DEBUG
1741 msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
1742 (int)omx_header->nFilledLen );
1745 if(omx_header->pInputPortPrivate)
1747 omx_header->pBuffer = omx_header->pInputPortPrivate;
1749 OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1751 return OMX_ErrorNone;