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 /*****************************************************************************
56 * List of OpenMAX IL core we will try in order
57 *****************************************************************************/
58 static const char *ppsz_dll_list[] =
61 "libiomx.so", /* Not used when using IOMX, the lib should already be loaded */
63 "libOMX_Core.so", /* TI OMAP IL core */
64 "libOmxCore.so", /* Qualcomm IL core */
65 "libomxil-bellagio.so", /* Bellagio IL core */
70 /*****************************************************************************
71 * Global OMX Core instance, shared between module instances
72 *****************************************************************************/
73 static vlc_mutex_t omx_core_mutex = VLC_STATIC_MUTEX;
74 static unsigned int omx_refcount = 0;
75 static void *dll_handle;
76 static OMX_ERRORTYPE (*pf_init) (void);
77 static OMX_ERRORTYPE (*pf_deinit) (void);
78 static OMX_ERRORTYPE (*pf_get_handle) (OMX_HANDLETYPE *, OMX_STRING,
79 OMX_PTR, OMX_CALLBACKTYPE *);
80 static OMX_ERRORTYPE (*pf_free_handle) (OMX_HANDLETYPE);
81 static OMX_ERRORTYPE (*pf_component_enum)(OMX_STRING, OMX_U32, OMX_U32);
82 static OMX_ERRORTYPE (*pf_get_roles_of_component)(OMX_STRING, OMX_U32 *, OMX_U8 **);
84 /*****************************************************************************
86 *****************************************************************************/
87 static int OpenDecoder( vlc_object_t * );
88 static int OpenEncoder( vlc_object_t * );
89 static int OpenGeneric( vlc_object_t *, bool b_encode );
90 static void CloseGeneric( vlc_object_t * );
92 static picture_t *DecodeVideo( decoder_t *, block_t ** );
93 static aout_buffer_t *DecodeAudio ( decoder_t *, block_t ** );
94 static block_t *EncodeVideo( encoder_t *, picture_t * );
96 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE, OMX_PTR, OMX_EVENTTYPE,
97 OMX_U32, OMX_U32, OMX_PTR );
98 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE, OMX_PTR,
99 OMX_BUFFERHEADERTYPE * );
100 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE, OMX_PTR,
101 OMX_BUFFERHEADERTYPE * );
103 /*****************************************************************************
105 *****************************************************************************/
107 set_description( N_("Audio/Video decoder (using OpenMAX IL)") )
108 set_category( CAT_INPUT )
109 set_subcategory( SUBCAT_INPUT_VCODEC )
110 set_section( N_("Decoding") , NULL )
111 #if defined(USE_IOMX)
112 /* For IOMX, don't enable it automatically via priorities,
113 * enable it only via the --codec iomx command line parameter when
115 set_capability( "decoder", 0 )
117 set_capability( "decoder", 80 )
119 set_callbacks( OpenDecoder, CloseGeneric )
122 set_section( N_("Encoding") , NULL )
123 set_description( N_("Video encoder (using OpenMAX IL)") )
124 set_capability( "encoder", 0 )
125 set_callbacks( OpenEncoder, CloseGeneric )
128 /*****************************************************************************
129 * CreateComponentsList: creates a list of components matching the given role
130 *****************************************************************************/
131 static int CreateComponentsList(decoder_t *p_dec, const char *psz_role)
133 decoder_sys_t *p_sys = p_dec->p_sys;
134 char psz_name[OMX_MAX_STRINGNAME_SIZE];
135 OMX_ERRORTYPE omx_error;
137 OMX_U8 **ppsz_roles = 0;
138 unsigned int i, j, len;
140 if(!psz_role) goto end;
141 len = strlen(psz_role);
145 bool b_found = false;
147 omx_error = pf_component_enum(psz_name, OMX_MAX_STRINGNAME_SIZE, i);
148 if(omx_error != OMX_ErrorNone) break;
150 msg_Dbg(p_dec, "component %s", psz_name);
152 omx_error = pf_get_roles_of_component(psz_name, &roles, 0);
153 if(omx_error != OMX_ErrorNone || !roles) continue;
155 ppsz_roles = malloc(roles * (sizeof(OMX_U8*) + OMX_MAX_STRINGNAME_SIZE));
156 if(!ppsz_roles) continue;
158 for( j = 0; j < roles; j++ )
159 ppsz_roles[j] = ((OMX_U8 *)(&ppsz_roles[roles])) +
160 j * OMX_MAX_STRINGNAME_SIZE;
162 omx_error = pf_get_roles_of_component(psz_name, &roles, ppsz_roles);
163 if(omx_error != OMX_ErrorNone) roles = 0;
165 for(j = 0; j < roles; j++)
167 msg_Dbg(p_dec, " - role: %s", ppsz_roles[j]);
168 if(!strncmp((char *)ppsz_roles[j], psz_role, len)) b_found = true;
173 if(!b_found) continue;
175 if(p_sys->components >= MAX_COMPONENTS_LIST_SIZE)
177 msg_Dbg(p_dec, "too many matching components");
181 strncpy(p_sys->ppsz_components[p_sys->components], psz_name,
182 OMX_MAX_STRINGNAME_SIZE-1);
187 msg_Dbg(p_dec, "found %i matching components for role %s",
188 p_sys->components, psz_role);
189 for( i = 0; i < p_sys->components; i++ )
190 msg_Dbg(p_dec, "- %s", p_sys->ppsz_components[i]);
192 return p_sys->components;
195 /*****************************************************************************
196 * ImplementationSpecificWorkarounds: place-holder for implementation
197 * specific workarounds
198 *****************************************************************************/
199 static OMX_ERRORTYPE ImplementationSpecificWorkarounds(decoder_t *p_dec,
200 OmxPort *p_port, es_format_t *p_fmt)
202 decoder_sys_t *p_sys = p_dec->p_sys;
203 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
204 int i_profile = 0xFFFF, i_level = 0xFFFF;
206 /* Try to find out the profile of the video */
207 while(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
208 p_fmt->i_codec == VLC_CODEC_H264)
210 uint8_t *p = (uint8_t*)p_dec->fmt_in.p_extra;
211 if(!p || !p_dec->fmt_in.p_extra) break;
213 /* Check the profile / level */
214 if(p_dec->fmt_in.i_original_fourcc == VLC_FOURCC('a','v','c','1') &&
217 if(p_dec->fmt_in.i_extra < 12) break;
218 p_sys->i_nal_size_length = 1 + (p[4]&0x03);
219 if( !(p[5]&0x1f) ) break;
224 if(p_dec->fmt_in.i_extra < 8) break;
225 if(!p[0] && !p[1] && !p[2] && p[3] == 1) p += 4;
226 else if(!p[0] && !p[1] && p[2] == 1) p += 3;
230 if( ((*p++)&0x1f) != 7) break;
232 /* Get profile/level out of first SPS */
238 if(!strcmp(p_sys->psz_component, "OMX.TI.Video.Decoder"))
240 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
241 p_fmt->i_codec == VLC_CODEC_H264 &&
242 (i_profile != 66 || i_level > 30))
244 msg_Dbg(p_dec, "h264 profile/level not supported (0x%x, 0x%x)",
246 return OMX_ErrorNotImplemented;
249 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirOutput &&
250 p_fmt->i_codec == VLC_CODEC_I420)
252 /* I420 xvideo is slow on OMAP */
253 def->format.video.eColorFormat = OMX_COLOR_FormatCbYCrY;
254 GetVlcChromaFormat( def->format.video.eColorFormat,
255 &p_fmt->i_codec, 0 );
256 GetVlcChromaSizes( p_fmt->i_codec,
257 def->format.video.nFrameWidth,
258 def->format.video.nFrameHeight,
259 &p_port->i_frame_size, &p_port->i_frame_stride,
260 &p_port->i_frame_stride_chroma_div );
261 def->format.video.nStride = p_port->i_frame_stride;
262 def->nBufferSize = p_port->i_frame_size;
265 else if(!strcmp(p_sys->psz_component, "OMX.st.video_encoder"))
267 if(p_fmt->i_cat == VIDEO_ES)
269 /* Bellagio's encoder doesn't encode the framerate in Q16 */
270 def->format.video.xFramerate >>= 16;
273 else if (!strncmp(p_sys->psz_component, "OMX.qcom.video.decoder.",
274 strlen("OMX.qcom.video.decoder")))
276 /* qdsp6 refuses buffer size larger than 450K on input port */
277 if (def->nBufferSize > 450 * 1024)
279 def->nBufferSize = 450 * 1024;
280 p_port->i_frame_size = def->nBufferSize;
284 return OMX_ErrorNone;
287 /*****************************************************************************
288 * SetPortDefinition: set definition of the omx port based on the vlc format
289 *****************************************************************************/
290 static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
293 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
294 OMX_ERRORTYPE omx_error;
296 omx_error = OMX_GetParameter(p_port->omx_handle,
297 OMX_IndexParamPortDefinition, def);
298 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
299 omx_error, ErrorToString(omx_error));
304 def->format.video.nFrameWidth = p_fmt->video.i_width;
305 def->format.video.nFrameHeight = p_fmt->video.i_height;
306 if(def->format.video.eCompressionFormat == OMX_VIDEO_CodingUnused)
307 def->format.video.nStride = def->format.video.nFrameWidth;
308 if( p_fmt->video.i_frame_rate > 0 &&
309 p_fmt->video.i_frame_rate_base > 0 )
310 def->format.video.xFramerate = (p_fmt->video.i_frame_rate << 16) /
311 p_fmt->video.i_frame_rate_base;
313 if(def->eDir == OMX_DirInput || p_dec->p_sys->b_enc)
315 def->nBufferSize = def->format.video.nFrameWidth *
316 def->format.video.nFrameHeight * 2;
317 p_port->i_frame_size = def->nBufferSize;
319 if(!GetOmxVideoFormat(p_fmt->i_codec,
320 &def->format.video.eCompressionFormat, 0) )
322 if(!GetOmxChromaFormat(p_fmt->i_codec,
323 &def->format.video.eColorFormat, 0) )
325 omx_error = OMX_ErrorNotImplemented;
326 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
327 (char *)&p_fmt->i_codec );
329 GetVlcChromaSizes( p_fmt->i_codec,
330 def->format.video.nFrameWidth,
331 def->format.video.nFrameHeight,
332 &p_port->i_frame_size, &p_port->i_frame_stride,
333 &p_port->i_frame_stride_chroma_div );
334 def->format.video.nStride = p_port->i_frame_stride;
335 def->nBufferSize = p_port->i_frame_size;
340 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
341 &p_fmt->i_codec, 0 ) )
343 omx_error = OMX_ErrorNotImplemented;
344 CHECK_ERROR(omx_error, "OMX color format %i not supported",
345 (int)def->format.video.eColorFormat );
347 GetVlcChromaSizes( p_fmt->i_codec,
348 def->format.video.nFrameWidth,
349 def->format.video.nFrameHeight,
350 &p_port->i_frame_size, &p_port->i_frame_stride,
351 &p_port->i_frame_stride_chroma_div );
352 def->format.video.nStride = p_port->i_frame_stride;
353 def->nBufferSize = p_port->i_frame_size;
358 p_port->i_frame_size = def->nBufferSize;
359 if(def->eDir == OMX_DirInput)
361 if(!GetOmxAudioFormat(p_fmt->i_codec,
362 &def->format.audio.eEncoding, 0) )
364 omx_error = OMX_ErrorNotImplemented;
365 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
366 (char *)&p_fmt->i_codec );
371 if( !OmxToVlcAudioFormat(def->format.audio.eEncoding,
372 &p_fmt->i_codec, 0 ) )
374 omx_error = OMX_ErrorNotImplemented;
375 CHECK_ERROR(omx_error, "OMX audio encoding %i not supported",
376 (int)def->format.audio.eEncoding );
381 default: return OMX_ErrorNotImplemented;
384 omx_error = ImplementationSpecificWorkarounds(p_dec, p_port, p_fmt);
385 CHECK_ERROR(omx_error, "ImplementationSpecificWorkarounds failed (%x : %s)",
386 omx_error, ErrorToString(omx_error));
388 omx_error = OMX_SetParameter(p_port->omx_handle,
389 OMX_IndexParamPortDefinition, def);
390 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
391 omx_error, ErrorToString(omx_error));
393 omx_error = OMX_GetParameter(p_port->omx_handle,
394 OMX_IndexParamPortDefinition, def);
395 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
396 omx_error, ErrorToString(omx_error));
398 if(p_port->i_frame_size > def->nBufferSize)
399 def->nBufferSize = p_port->i_frame_size;
400 p_port->i_frame_size = def->nBufferSize;
402 /* Deal with audio params */
403 if(p_fmt->i_cat == AUDIO_ES)
405 omx_error = SetAudioParameters(p_port->omx_handle,
406 &p_port->format_param, def->nPortIndex,
407 def->format.audio.eEncoding,
408 p_fmt->audio.i_channels,
411 p_fmt->audio.i_bitspersample,
412 p_fmt->audio.i_blockalign);
413 CHECK_ERROR(omx_error, "SetAudioParameters failed (%x : %s)",
414 omx_error, ErrorToString(omx_error));
421 /*****************************************************************************
422 * GetPortDefinition: set vlc format based on the definition of the omx port
423 *****************************************************************************/
424 static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
427 decoder_sys_t *p_sys = p_dec->p_sys;
428 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
429 OMX_ERRORTYPE omx_error;
431 omx_error = OMX_GetParameter(p_port->omx_handle,
432 OMX_IndexParamPortDefinition, def);
433 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
434 omx_error, ErrorToString(omx_error));
439 p_fmt->video.i_width = def->format.video.nFrameWidth;
440 p_fmt->video.i_visible_width = def->format.video.nFrameWidth;
441 p_fmt->video.i_height = def->format.video.nFrameHeight;
442 p_fmt->video.i_visible_height = def->format.video.nFrameHeight;
443 p_fmt->video.i_frame_rate = p_dec->fmt_in.video.i_frame_rate;
444 p_fmt->video.i_frame_rate_base = p_dec->fmt_in.video.i_frame_rate_base;
446 /* Hack: Nexus One (stock firmware with binary OMX driver blob)
447 * claims to output 420Planar even though it in in practice is
449 if(def->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar &&
450 !strncmp(p_sys->psz_component, "OMX.qcom.video.decoder",
451 strlen("OMX.qcom.video.decoder")))
452 def->format.video.eColorFormat = OMX_QCOM_COLOR_FormatYVU420SemiPlanar;
454 /* Hack: Galaxy S II (stock firmware) gives a slice height larger
455 * than the video height, but this doesn't imply padding between
456 * the video planes. Nexus S also has a slice height larger than
457 * the video height, but there it actually is real padding, thus
458 * Galaxy S II is the buggy one. The Galaxy S II decoder is
459 * named OMX.SEC.avcdec while the one on Nexus S is
460 * OMX.SEC.AVC.Decoder. Thus do this for any OMX.SEC. that don't
461 * contain the string ".Decoder". */
462 if(!strncmp(p_sys->psz_component, "OMX.SEC.", strlen("OMX.SEC.")) &&
463 !strstr(p_sys->psz_component, ".Decoder"))
464 def->format.video.nSliceHeight = 0;
466 if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
467 &p_fmt->i_codec, 0 ) )
469 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
470 &p_fmt->i_codec, 0 ) )
472 omx_error = OMX_ErrorNotImplemented;
473 CHECK_ERROR(omx_error, "OMX color format %i not supported",
474 (int)def->format.video.eColorFormat );
476 GetVlcChromaSizes( p_fmt->i_codec,
477 def->format.video.nFrameWidth,
478 def->format.video.nFrameHeight,
479 &p_port->i_frame_size, &p_port->i_frame_stride,
480 &p_port->i_frame_stride_chroma_div );
482 if(p_port->i_frame_size > def->nBufferSize)
483 def->nBufferSize = p_port->i_frame_size;
484 p_port->i_frame_size = def->nBufferSize;
486 if((int)p_port->i_frame_stride > def->format.video.nStride)
487 def->format.video.nStride = p_port->i_frame_stride;
489 p_port->i_frame_stride = def->format.video.nStride;
493 if( !OmxToVlcAudioFormat( def->format.audio.eEncoding,
494 &p_fmt->i_codec, 0 ) )
496 omx_error = OMX_ErrorNotImplemented;
497 CHECK_ERROR(omx_error, "OMX audio format %i not supported",
498 (int)def->format.audio.eEncoding );
501 omx_error = GetAudioParameters(p_port->omx_handle,
502 &p_port->format_param, def->nPortIndex,
503 def->format.audio.eEncoding,
504 &p_fmt->audio.i_channels,
505 &p_fmt->audio.i_rate,
507 &p_fmt->audio.i_bitspersample,
508 &p_fmt->audio.i_blockalign);
509 CHECK_ERROR(omx_error, "GetAudioParameters failed (%x : %s)",
510 omx_error, ErrorToString(omx_error));
512 if(p_fmt->audio.i_channels < 9)
514 static const int pi_channels_maps[9] =
516 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
517 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
518 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
519 | AOUT_CHAN_REARRIGHT,
520 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
521 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
522 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
523 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
524 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
525 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
526 | AOUT_CHAN_MIDDLERIGHT,
527 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
528 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
531 p_fmt->audio.i_physical_channels =
532 p_fmt->audio.i_original_channels =
533 pi_channels_maps[p_fmt->audio.i_channels];
536 date_Init( &p_dec->p_sys->end_date, p_fmt->audio.i_rate, 1 );
540 default: return OMX_ErrorNotImplemented;
547 /*****************************************************************************
548 * DeinitialiseComponent: Deinitialise and unload an OMX component
549 *****************************************************************************/
550 static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
551 OMX_HANDLETYPE omx_handle)
553 decoder_sys_t *p_sys = p_dec->p_sys;
554 OMX_ERRORTYPE omx_error;
558 if(!omx_handle) return OMX_ErrorNone;
560 omx_error = OMX_GetState(omx_handle, &state);
561 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
563 if(state == OMX_StateExecuting)
565 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
567 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
568 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
569 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
572 omx_error = OMX_GetState(omx_handle, &state);
573 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
575 if(state == OMX_StateIdle)
577 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
578 OMX_StateLoaded, 0 );
579 CHECK_ERROR(omx_error, "OMX_CommandStateSet Loaded failed (%x)", omx_error );
581 for(i = 0; i < p_sys->ports; i++)
583 OmxPort *p_port = &p_sys->p_ports[i];
584 OMX_BUFFERHEADERTYPE *p_buffer;
586 for(j = 0; j < p_port->i_buffers; j++)
588 OMX_FIFO_GET(&p_port->fifo, p_buffer);
589 if (p_buffer == &p_sys->sentinel_buffer)
591 omx_error = OMX_FreeBuffer( omx_handle,
592 p_port->i_port_index, p_buffer );
594 if(omx_error != OMX_ErrorNone) break;
596 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
597 omx_error, (int)p_port->i_port_index, j );
600 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
601 CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
605 for(i = 0; i < p_sys->ports; i++)
607 OmxPort *p_port = &p_sys->p_ports[i];
608 free(p_port->pp_buffers);
609 p_port->pp_buffers = 0;
611 omx_error = pf_free_handle( omx_handle );
615 /*****************************************************************************
616 * InitialiseComponent: Load and initialise an OMX component
617 *****************************************************************************/
618 static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
619 OMX_STRING psz_component, OMX_HANDLETYPE *p_handle)
621 static OMX_CALLBACKTYPE callbacks =
622 { OmxEventHandler, OmxEmptyBufferDone, OmxFillBufferDone };
623 decoder_sys_t *p_sys = p_dec->p_sys;
624 OMX_HANDLETYPE omx_handle;
625 OMX_ERRORTYPE omx_error;
627 OMX_U8 psz_role[OMX_MAX_STRINGNAME_SIZE];
628 OMX_PARAM_COMPONENTROLETYPE role;
629 OMX_PARAM_PORTDEFINITIONTYPE definition;
630 OMX_PORT_PARAM_TYPE param;
633 omx_error = pf_get_handle( &omx_handle, psz_component, p_dec, &callbacks );
634 if(omx_error != OMX_ErrorNone)
636 msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
637 omx_error, ErrorToString(omx_error) );
640 strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
642 omx_error = OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
643 if(omx_error == OMX_ErrorNone)
644 msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
646 msg_Dbg(p_dec, "loaded component %s", psz_component);
647 PrintOmx(p_dec, omx_handle, OMX_ALL);
649 /* Set component role */
650 OMX_INIT_STRUCTURE(role);
651 strcpy((char*)role.cRole,
652 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec : p_dec->fmt_in.i_codec,
653 p_dec->fmt_in.i_cat, p_sys->b_enc));
655 omx_error = OMX_SetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
657 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
659 if(omx_error == OMX_ErrorNone)
660 msg_Dbg(p_dec, "component standard role set to %s", role.cRole);
662 /* Find the input / output ports */
663 OMX_INIT_STRUCTURE(param);
664 OMX_INIT_STRUCTURE(definition);
665 omx_error = OMX_GetParameter(omx_handle, p_dec->fmt_in.i_cat == VIDEO_ES ?
666 OMX_IndexParamVideoInit : OMX_IndexParamAudioInit, ¶m);
667 if(omx_error != OMX_ErrorNone) param.nPorts = 0;
669 for(i = 0; i < param.nPorts; i++)
673 /* Get port definition */
674 definition.nPortIndex = param.nStartPortNumber + i;
675 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
677 if(omx_error != OMX_ErrorNone) continue;
679 if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
680 else p_port = &p_sys->out;
682 p_port->b_valid = true;
683 p_port->i_port_index = definition.nPortIndex;
684 p_port->definition = definition;
685 p_port->omx_handle = omx_handle;
688 if(!p_sys->in.b_valid || !p_sys->out.b_valid)
690 omx_error = OMX_ErrorInvalidComponent;
691 CHECK_ERROR(omx_error, "couldn't find an input and output port");
694 if(!strncmp(p_sys->psz_component, "OMX.SEC.", 8))
697 omx_error = OMX_GetExtensionIndex(omx_handle, (OMX_STRING) "OMX.SEC.index.ThumbnailMode", &index);
698 if(omx_error == OMX_ErrorNone)
700 OMX_BOOL enable = OMX_TRUE;
701 omx_error = OMX_SetConfig(omx_handle, index, &enable);
702 CHECK_ERROR(omx_error, "Unable to set ThumbnailMode");
704 OMX_BOOL enable = OMX_TRUE;
705 /* Needed on Samsung Galaxy S II */
706 omx_error = OMX_SetConfig(omx_handle, OMX_IndexVendorSetYUV420pMode, &enable);
707 if (omx_error == OMX_ErrorNone)
708 msg_Dbg(p_dec, "Set OMX_IndexVendorSetYUV420pMode successfully");
710 msg_Dbg(p_dec, "Unable to set OMX_IndexVendorSetYUV420pMode: %x", omx_error);
714 /* Set port definitions */
715 for(i = 0; i < p_sys->ports; i++)
717 omx_error = SetPortDefinition(p_dec, &p_sys->p_ports[i],
718 p_sys->p_ports[i].p_fmt);
719 if(omx_error != OMX_ErrorNone) goto error;
722 /* Allocate our array for the omx buffers and enable ports */
723 for(i = 0; i < p_sys->ports; i++)
725 OmxPort *p_port = &p_sys->p_ports[i];
728 malloc(p_port->definition.nBufferCountActual *
729 sizeof(OMX_BUFFERHEADERTYPE*));
730 if(!p_port->pp_buffers)
732 omx_error = OMX_ErrorInsufficientResources;
733 CHECK_ERROR(omx_error, "memory allocation failed");
735 p_port->i_buffers = p_port->definition.nBufferCountActual;
738 if(!p_port->definition.bEnabled)
740 omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
741 p_port->i_port_index, NULL);
742 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
743 (int)p_port->i_port_index, omx_error );
744 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
745 CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
746 (int)p_port->i_port_index, omx_error );
750 *p_handle = omx_handle;
751 return OMX_ErrorNone;
754 DeinitialiseComponent(p_dec, omx_handle);
759 /*****************************************************************************
760 * OpenDecoder: Create the decoder instance
761 *****************************************************************************/
762 static int OpenDecoder( vlc_object_t *p_this )
764 decoder_t *p_dec = (decoder_t*)p_this;
767 if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
771 if( p_dec->fmt_in.i_cat != VIDEO_ES && !p_dec->b_force)
775 status = OpenGeneric( p_this, false );
776 if(status != VLC_SUCCESS) return status;
778 p_dec->pf_decode_video = DecodeVideo;
779 p_dec->pf_decode_audio = DecodeAudio;
784 /*****************************************************************************
785 * OpenEncoder: Create the encoder instance
786 *****************************************************************************/
787 static int OpenEncoder( vlc_object_t *p_this )
789 encoder_t *p_enc = (encoder_t*)p_this;
792 if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
795 status = OpenGeneric( p_this, true );
796 if(status != VLC_SUCCESS) return status;
798 p_enc->pf_encode_video = EncodeVideo;
803 /*****************************************************************************
804 * OpenGeneric: Create the generic decoder/encoder instance
805 *****************************************************************************/
806 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
808 decoder_t *p_dec = (decoder_t*)p_this;
809 decoder_sys_t *p_sys;
810 OMX_ERRORTYPE omx_error;
811 OMX_BUFFERHEADERTYPE *p_header;
814 vlc_mutex_lock( &omx_core_mutex );
815 if( omx_refcount > 0 )
818 /* Load the OMX core */
819 for( i = 0; ppsz_dll_list[i]; i++ )
821 dll_handle = dll_open( ppsz_dll_list[i] );
822 if( dll_handle ) break;
826 vlc_mutex_unlock( &omx_core_mutex );
830 pf_init = dlsym( dll_handle, "OMX_Init" );
831 pf_deinit = dlsym( dll_handle, "OMX_Deinit" );
832 pf_get_handle = dlsym( dll_handle, "OMX_GetHandle" );
833 pf_free_handle = dlsym( dll_handle, "OMX_FreeHandle" );
834 pf_component_enum = dlsym( dll_handle, "OMX_ComponentNameEnum" );
835 pf_get_roles_of_component = dlsym( dll_handle, "OMX_GetRolesOfComponent" );
836 if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle ||
837 !pf_component_enum || !pf_get_roles_of_component )
839 msg_Warn( p_this, "cannot find OMX_* symbols in `%s' (%s)",
840 ppsz_dll_list[i], dlerror() );
841 dll_close(dll_handle);
842 vlc_mutex_unlock( &omx_core_mutex );
847 /* Allocate the memory needed to store the decoder's structure */
848 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
850 if( omx_refcount == 0 )
851 dll_close(dll_handle);
852 vlc_mutex_unlock( &omx_core_mutex );
856 /* Initialise the thread properties */
859 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
860 p_dec->fmt_out.video = p_dec->fmt_in.video;
861 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
862 p_dec->fmt_out.i_codec = 0;
864 p_sys->b_enc = b_encode;
865 p_sys->pp_last_event = &p_sys->p_events;
866 vlc_mutex_init (&p_sys->mutex);
867 vlc_cond_init (&p_sys->cond);
868 vlc_mutex_init (&p_sys->lock);
869 vlc_mutex_init (&p_sys->in.fifo.lock);
870 vlc_cond_init (&p_sys->in.fifo.wait);
871 p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
872 p_sys->in.fifo.pp_last = &p_sys->in.fifo.p_first;
873 p_sys->in.b_direct = false;
874 p_sys->in.b_flushed = true;
875 p_sys->in.p_fmt = &p_dec->fmt_in;
876 vlc_mutex_init (&p_sys->out.fifo.lock);
877 vlc_cond_init (&p_sys->out.fifo.wait);
878 p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
879 p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
880 p_sys->out.b_direct = true;
881 p_sys->out.b_flushed = true;
882 p_sys->out.p_fmt = &p_dec->fmt_out;
884 p_sys->p_ports = &p_sys->in;
886 msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
887 (char *)&p_dec->fmt_out.i_codec);
889 /* Initialise the OMX core */
890 omx_error = omx_refcount > 0 ? OMX_ErrorNone : pf_init();
892 if(omx_error != OMX_ErrorNone)
894 msg_Warn( p_this, "OMX_Init failed (%x: %s)", omx_error,
895 ErrorToString(omx_error) );
896 vlc_mutex_unlock( &omx_core_mutex );
897 CloseGeneric(p_this);
900 p_sys->b_init = true;
901 vlc_mutex_unlock( &omx_core_mutex );
903 /* Enumerate components and build a list of the one we want to try */
904 if( !CreateComponentsList(p_dec,
905 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
906 p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
909 msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
910 (char *)&p_dec->fmt_in.i_codec );
911 CloseGeneric(p_this);
915 /* Try to load and initialise a component */
916 omx_error = OMX_ErrorUndefined;
917 for(i = 0; i < p_sys->components; i++)
920 /* ignore OpenCore software codecs */
921 if (!strncmp(p_sys->ppsz_components[i], "OMX.PV.", 7))
923 /* The same sw codecs, renamed in ICS (perhaps also in honeycomb) */
924 if (!strncmp(p_sys->ppsz_components[i], "OMX.google.", 11))
927 omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
929 if(omx_error == OMX_ErrorNone) break;
931 CHECK_ERROR(omx_error, "no component could be initialised" );
933 /* Move component to Idle then Executing state */
934 OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
935 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
937 /* Allocate omx buffers */
938 for(i = 0; i < p_sys->ports; i++)
940 OmxPort *p_port = &p_sys->p_ports[i];
942 for(j = 0; j < p_port->i_buffers; j++)
945 #define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
946 char *p_buf = malloc(p_port->definition.nBufferSize +
947 p_port->definition.nBufferAlignment);
948 p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
951 if(0 && p_port->b_direct)
953 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
954 p_port->i_port_index, 0,
955 p_port->definition.nBufferSize, (void*)1);
958 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
959 p_port->i_port_index, 0,
960 p_port->definition.nBufferSize);
962 if(omx_error != OMX_ErrorNone) break;
963 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
965 p_port->i_buffers = j;
966 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
967 omx_error, (int)p_port->i_port_index, j );
970 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
971 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
973 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
974 OMX_StateExecuting, 0);
975 CHECK_ERROR(omx_error, "OMX_CommandStateSet Executing failed (%x)", omx_error );
976 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
977 CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
979 /* Send codec configuration data */
980 if( p_dec->fmt_in.i_extra )
982 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
983 p_header->nFilledLen = p_dec->fmt_in.i_extra;
985 /* Convert H.264 NAL format to annex b */
986 if( p_sys->i_nal_size_length && !p_sys->in.b_direct )
988 p_header->nFilledLen = 0;
989 convert_sps_pps( p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
990 p_header->pBuffer, p_header->nAllocLen,
991 (uint32_t*) &p_header->nFilledLen, NULL );
993 else if(p_sys->in.b_direct)
995 p_header->pOutputPortPrivate = p_header->pBuffer;
996 p_header->pBuffer = p_dec->fmt_in.p_extra;
1000 if(p_header->nFilledLen > p_header->nAllocLen)
1002 msg_Dbg(p_dec, "buffer too small (%i,%i)", (int)p_header->nFilledLen,
1003 (int)p_header->nAllocLen);
1004 p_header->nFilledLen = p_header->nAllocLen;
1006 memcpy(p_header->pBuffer, p_dec->fmt_in.p_extra, p_header->nFilledLen);
1009 p_header->nOffset = 0;
1010 p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG;
1011 msg_Dbg(p_dec, "sending codec config data %p, %p, %i", p_header,
1012 p_header->pBuffer, (int)p_header->nFilledLen);
1013 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1016 /* Get back output port definition */
1017 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1018 if(omx_error != OMX_ErrorNone) goto error;
1020 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1021 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1023 if(p_sys->b_error) goto error;
1025 p_dec->b_need_packetized = true;
1029 CloseGeneric(p_this);
1030 return VLC_EGENERIC;
1033 /*****************************************************************************
1035 *****************************************************************************/
1036 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
1038 decoder_sys_t *p_sys = p_dec->p_sys;
1039 OMX_PARAM_PORTDEFINITIONTYPE definition;
1040 OMX_BUFFERHEADERTYPE *p_buffer;
1041 OMX_ERRORTYPE omx_error;
1044 /* Sanity checking */
1045 OMX_INIT_STRUCTURE(definition);
1046 definition.nPortIndex = p_port->i_port_index;
1047 omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1049 if(omx_error != OMX_ErrorNone || (p_dec->fmt_in.i_cat == VIDEO_ES &&
1050 (!definition.format.video.nFrameWidth ||
1051 !definition.format.video.nFrameHeight)) )
1052 return OMX_ErrorUndefined;
1054 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
1055 p_port->i_port_index, NULL);
1056 CHECK_ERROR(omx_error, "OMX_CommandPortDisable on %i failed (%x)",
1057 (int)p_port->i_port_index, omx_error );
1059 for(i = 0; i < p_port->i_buffers; i++)
1061 OMX_FIFO_GET(&p_port->fifo, p_buffer);
1062 if (p_buffer == &p_sys->sentinel_buffer)
1064 omx_error = OMX_FreeBuffer( p_sys->omx_handle,
1065 p_port->i_port_index, p_buffer );
1067 if(omx_error != OMX_ErrorNone) break;
1069 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
1070 omx_error, (int)p_port->i_port_index, i );
1072 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1073 CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
1075 /* Get the new port definition */
1076 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1077 if(omx_error != OMX_ErrorNone) goto error;
1079 if( p_dec->fmt_in.i_cat != AUDIO_ES )
1081 /* Don't explicitly set the new parameters that we got with
1082 * OMX_GetParameter above when using audio codecs.
1083 * That struct hasn't been changed since, so there should be
1084 * no need to set it here, unless some codec expects the
1085 * SetParameter call as a trigger event for some part of
1086 * the reconfiguration.
1087 * This fixes using audio decoders on Samsung Galaxy S II,
1089 * Only skipping this for audio codecs, to minimize the
1090 * change for current working configurations for video.
1092 omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1094 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
1095 omx_error, ErrorToString(omx_error));
1098 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
1099 p_port->i_port_index, NULL);
1100 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
1101 (int)p_port->i_port_index, omx_error );
1103 if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
1104 free(p_port->pp_buffers);
1105 p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
1106 if(!p_port->pp_buffers)
1108 omx_error = OMX_ErrorInsufficientResources;
1109 CHECK_ERROR(omx_error, "memory allocation failed");
1112 p_port->i_buffers = p_port->definition.nBufferCountActual;
1113 for(i = 0; i < p_port->i_buffers; i++)
1115 if(0 && p_port->b_direct)
1117 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1118 p_port->i_port_index, 0,
1119 p_port->definition.nBufferSize, (void*)1);
1122 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1123 p_port->i_port_index, 0,
1124 p_port->definition.nBufferSize);
1126 if(omx_error != OMX_ErrorNone) break;
1127 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1129 p_port->i_buffers = i;
1130 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1131 omx_error, (int)p_port->i_port_index, i );
1133 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1134 CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1136 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1137 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1143 /*****************************************************************************
1144 * DecodeVideo: Called to decode one frame
1145 *****************************************************************************/
1146 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1148 decoder_sys_t *p_sys = p_dec->p_sys;
1149 picture_t *p_pic = NULL, *p_next_pic;
1150 OMX_ERRORTYPE omx_error;
1153 OMX_BUFFERHEADERTYPE *p_header;
1156 if( !pp_block || !*pp_block )
1159 p_block = *pp_block;
1161 /* Check for errors from codec */
1164 msg_Dbg(p_dec, "error during decoding");
1165 block_Release( p_block );
1169 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1171 block_Release( p_block );
1172 if(!p_sys->in.b_flushed)
1174 msg_Dbg(p_dec, "flushing");
1175 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1176 p_sys->in.definition.nPortIndex, 0 );
1178 p_sys->in.b_flushed = true;
1182 /* Take care of decoded frames first */
1185 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1186 if(!p_header) break; /* No frame available */
1188 if(p_header->nFilledLen)
1190 p_pic = p_header->pAppPrivate;
1193 /* We're not in direct rendering mode.
1194 * Get a new picture and copy the content */
1195 p_pic = decoder_NewPicture( p_dec );
1196 if( !p_pic ) break; /* No picture available */
1198 CopyOmxPicture(p_dec, p_pic, p_header, p_sys->out.definition.format.video.nSliceHeight);
1201 p_pic->date = p_header->nTimeStamp;
1202 p_header->nFilledLen = 0;
1203 p_header->pAppPrivate = 0;
1206 /* Get a new picture */
1207 if(p_sys->in.b_direct && !p_header->pAppPrivate)
1209 p_next_pic = decoder_NewPicture( p_dec );
1210 if(!p_next_pic) break;
1212 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1213 p_header->pAppPrivate = p_next_pic;
1214 p_header->pInputPortPrivate = p_header->pBuffer;
1215 p_header->pBuffer = p_next_pic->p[0].p_pixels;
1219 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1222 #ifdef OMXIL_EXTRA_DEBUG
1223 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1225 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1228 /* Send the input buffer to the component */
1229 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1231 if (p_header && p_header->nFlags & OMX_BUFFERFLAG_EOS)
1236 p_header->nFilledLen = p_block->i_buffer;
1237 p_header->nOffset = 0;
1238 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1239 p_header->nTimeStamp = p_block->i_dts;
1241 /* In direct mode we pass the input pointer as is.
1242 * Otherwise we memcopy the data */
1243 if(p_sys->in.b_direct)
1245 p_header->pOutputPortPrivate = p_header->pBuffer;
1246 p_header->pBuffer = p_block->p_buffer;
1247 p_header->pAppPrivate = p_block;
1251 if(p_header->nFilledLen > p_header->nAllocLen)
1253 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1254 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1255 p_header->nFilledLen = p_header->nAllocLen;
1257 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1258 block_Release(p_block);
1261 /* Convert H.264 NAL format to annex b */
1262 if( p_sys->i_nal_size_length >= 3 && p_sys->i_nal_size_length <= 4 )
1264 /* This only works for NAL sizes 3-4 */
1265 int i_len = p_header->nFilledLen, i;
1266 uint8_t* ptr = p_header->pBuffer;
1267 while( i_len >= p_sys->i_nal_size_length )
1269 uint32_t nal_len = 0;
1270 for( i = 0; i < p_sys->i_nal_size_length; i++ ) {
1271 nal_len = (nal_len << 8) | ptr[i];
1274 ptr[p_sys->i_nal_size_length - 1] = 1;
1275 if( nal_len > INT_MAX || nal_len > (unsigned int) i_len )
1278 i_len -= nal_len + 4;
1281 #ifdef OMXIL_EXTRA_DEBUG
1282 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1283 (int)p_header->nFilledLen );
1285 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1286 p_sys->in.b_flushed = false;
1287 *pp_block = NULL; /* Avoid being fed the same packet again */
1291 /* Handle the PortSettingsChanged events */
1292 for(i = 0; i < p_sys->ports; i++)
1294 OmxPort *p_port = &p_sys->p_ports[i];
1295 if(!p_port->b_reconfigure) continue;
1296 p_port->b_reconfigure = 0;
1297 omx_error = PortReconfigure(p_dec, p_port);
1303 /*****************************************************************************
1304 * DecodeAudio: Called to decode one frame
1305 *****************************************************************************/
1306 aout_buffer_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1308 decoder_sys_t *p_sys = p_dec->p_sys;
1309 aout_buffer_t *p_buffer = 0;
1310 OMX_BUFFERHEADERTYPE *p_header;
1311 OMX_ERRORTYPE omx_error;
1315 if( !pp_block || !*pp_block ) return NULL;
1317 p_block = *pp_block;
1319 /* Check for errors from codec */
1322 msg_Dbg(p_dec, "error during decoding");
1323 block_Release( p_block );
1327 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1329 block_Release( p_block );
1330 date_Set( &p_sys->end_date, 0 );
1331 if(!p_sys->in.b_flushed)
1333 msg_Dbg(p_dec, "flushing");
1334 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1335 p_sys->in.definition.nPortIndex, 0 );
1337 p_sys->in.b_flushed = true;
1341 if( !date_Get( &p_sys->end_date ) )
1343 if( !p_block->i_pts )
1345 /* We've just started the stream, wait for the first PTS. */
1346 block_Release( p_block );
1349 date_Set( &p_sys->end_date, p_block->i_pts );
1352 /* Take care of decoded frames first */
1355 unsigned int i_samples;
1357 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1358 if(!p_header) break; /* No frame available */
1360 i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1363 p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1364 if( !p_buffer ) break; /* No audio buffer available */
1366 memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1367 p_header->nFilledLen = 0;
1369 if( p_header->nTimeStamp != 0 &&
1370 p_header->nTimeStamp != date_Get( &p_sys->end_date ) )
1371 date_Set( &p_sys->end_date, p_header->nTimeStamp );
1373 p_buffer->i_pts = date_Get( &p_sys->end_date );
1374 p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1378 #ifdef OMXIL_EXTRA_DEBUG
1379 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1381 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1382 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1386 /* Send the input buffer to the component */
1387 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1389 if (p_header && p_header->nFlags & OMX_BUFFERFLAG_EOS)
1394 p_header->nFilledLen = p_block->i_buffer;
1395 p_header->nOffset = 0;
1396 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1397 p_header->nTimeStamp = p_block->i_dts;
1399 /* In direct mode we pass the input pointer as is.
1400 * Otherwise we memcopy the data */
1401 if(p_sys->in.b_direct)
1403 p_header->pOutputPortPrivate = p_header->pBuffer;
1404 p_header->pBuffer = p_block->p_buffer;
1405 p_header->pAppPrivate = p_block;
1409 if(p_header->nFilledLen > p_header->nAllocLen)
1411 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1412 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1413 p_header->nFilledLen = p_header->nAllocLen;
1415 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1416 block_Release(p_block);
1419 #ifdef OMXIL_EXTRA_DEBUG
1420 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1421 (int)p_header->nFilledLen );
1423 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1424 p_sys->in.b_flushed = false;
1425 *pp_block = NULL; /* Avoid being fed the same packet again */
1429 /* Handle the PortSettingsChanged events */
1430 for(i = 0; i < p_sys->ports; i++)
1432 OmxPort *p_port = &p_sys->p_ports[i];
1433 if(!p_port->b_reconfigure) continue;
1434 p_port->b_reconfigure = 0;
1435 omx_error = PortReconfigure(p_dec, p_port);
1441 /*****************************************************************************
1442 * EncodeVideo: Called to encode one frame
1443 *****************************************************************************/
1444 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1446 decoder_t *p_dec = ( decoder_t *)p_enc;
1447 decoder_sys_t *p_sys = p_dec->p_sys;
1448 OMX_ERRORTYPE omx_error;
1451 OMX_BUFFERHEADERTYPE *p_header;
1452 block_t *p_block = 0;
1454 if( !p_pic ) return NULL;
1456 /* Check for errors from codec */
1459 msg_Dbg(p_dec, "error during encoding");
1463 /* Send the input buffer to the component */
1464 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1467 /* In direct mode we pass the input pointer as is.
1468 * Otherwise we memcopy the data */
1469 if(p_sys->in.b_direct)
1471 p_header->pOutputPortPrivate = p_header->pBuffer;
1472 p_header->pBuffer = p_pic->p[0].p_pixels;
1476 CopyVlcPicture(p_dec, p_header, p_pic);
1479 p_header->nFilledLen = p_sys->in.i_frame_size;
1480 p_header->nOffset = 0;
1481 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1482 p_header->nTimeStamp = p_pic->date;
1483 #ifdef OMXIL_EXTRA_DEBUG
1484 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1485 (int)p_header->nFilledLen );
1487 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1488 p_sys->in.b_flushed = false;
1491 /* Handle the PortSettingsChanged events */
1492 for(i = 0; i < p_sys->ports; i++)
1494 OmxPort *p_port = &p_sys->p_ports[i];
1495 if(!p_port->b_reconfigure) continue;
1496 p_port->b_reconfigure = 0;
1497 omx_error = PortReconfigure(p_dec, p_port);
1500 /* Wait for the decoded frame */
1503 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1505 if(p_header->nFilledLen)
1507 if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1509 /* TODO: need to store codec config */
1510 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1513 p_block = p_header->pAppPrivate;
1516 /* We're not in direct rendering mode.
1517 * Get a new block and copy the content */
1518 p_block = block_New( p_dec, p_header->nFilledLen );
1519 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1522 p_block->i_buffer = p_header->nFilledLen;
1523 p_block->i_pts = p_block->i_dts = p_header->nTimeStamp;
1524 p_header->nFilledLen = 0;
1525 p_header->pAppPrivate = 0;
1528 #ifdef OMXIL_EXTRA_DEBUG
1529 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1531 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1534 msg_Dbg(p_dec, "done");
1538 /*****************************************************************************
1539 * CloseGeneric: omxil decoder destruction
1540 *****************************************************************************/
1541 static void CloseGeneric( vlc_object_t *p_this )
1543 decoder_t *p_dec = (decoder_t *)p_this;
1544 decoder_sys_t *p_sys = p_dec->p_sys;
1546 if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1547 vlc_mutex_lock( &omx_core_mutex );
1549 if( omx_refcount == 0 )
1551 if( p_sys->b_init ) pf_deinit();
1552 dll_close( dll_handle );
1554 vlc_mutex_unlock( &omx_core_mutex );
1556 vlc_mutex_destroy (&p_sys->mutex);
1557 vlc_cond_destroy (&p_sys->cond);
1558 vlc_mutex_destroy (&p_sys->in.fifo.lock);
1559 vlc_cond_destroy (&p_sys->in.fifo.wait);
1560 vlc_mutex_destroy (&p_sys->out.fifo.lock);
1561 vlc_cond_destroy (&p_sys->out.fifo.wait);
1566 /*****************************************************************************
1568 *****************************************************************************/
1569 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1570 OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1571 OMX_U32 data_2, OMX_PTR event_data )
1573 decoder_t *p_dec = (decoder_t *)app_data;
1574 decoder_sys_t *p_sys = p_dec->p_sys;
1580 case OMX_EventCmdComplete:
1581 switch ((OMX_STATETYPE)data_1)
1583 case OMX_CommandStateSet:
1584 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %s)", EventToString(event),
1585 CommandToString(data_1), StateToString(data_2) );
1589 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u)", EventToString(event),
1590 CommandToString(data_1), (unsigned int)data_2 );
1595 case OMX_EventError:
1596 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u, %s)", EventToString(event),
1597 ErrorToString((OMX_ERRORTYPE)data_1), (unsigned int)data_2,
1598 (const char *)event_data);
1599 //p_sys->b_error = true;
1602 case OMX_EventPortSettingsChanged:
1603 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1604 (unsigned int)data_1, (unsigned int)data_2 );
1605 for(i = 0; i < p_sys->ports; i++)
1606 if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1607 p_sys->p_ports[i].b_reconfigure = true;
1608 memset(&p_sys->sentinel_buffer, 0, sizeof(p_sys->sentinel_buffer));
1609 p_sys->sentinel_buffer.nFlags = OMX_BUFFERFLAG_EOS;
1610 OMX_FIFO_PUT(&p_sys->in.fifo, &p_sys->sentinel_buffer);
1614 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1615 (unsigned int)data_1, (unsigned int)data_2 );
1619 PostOmxEvent(p_dec, event, data_1, data_2, event_data);
1620 return OMX_ErrorNone;
1623 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1624 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1626 decoder_t *p_dec = (decoder_t *)app_data;
1627 decoder_sys_t *p_sys = p_dec->p_sys;
1630 #ifdef OMXIL_EXTRA_DEBUG
1631 msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1634 if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1636 block_t *p_block = (block_t *)omx_header->pAppPrivate;
1637 omx_header->pBuffer = omx_header->pOutputPortPrivate;
1638 if(p_block) block_Release(p_block);
1639 omx_header->pAppPrivate = 0;
1641 OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1643 return OMX_ErrorNone;
1646 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1647 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1649 decoder_t *p_dec = (decoder_t *)app_data;
1650 decoder_sys_t *p_sys = p_dec->p_sys;
1653 #ifdef OMXIL_EXTRA_DEBUG
1654 msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
1655 (int)omx_header->nFilledLen );
1658 if(omx_header->pInputPortPrivate)
1660 omx_header->pBuffer = omx_header->pInputPortPrivate;
1662 OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1664 return OMX_ErrorNone;