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>
48 #include <vlc_block_helper.h>
50 #include "../h264_nal.h"
54 //#define OMXIL_EXTRA_DEBUG
56 /*****************************************************************************
57 * List of OpenMAX IL core we will try in order
58 *****************************************************************************/
59 static const char *ppsz_dll_list[] =
62 "libiomx.so", /* Not used when using IOMX, the lib should already be loaded */
64 "libOMX_Core.so", /* TI OMAP IL core */
65 "libOmxCore.so", /* Qualcomm IL core */
66 "libomxil-bellagio.so", /* Bellagio IL core */
71 /*****************************************************************************
72 * Global OMX Core instance, shared between module instances
73 *****************************************************************************/
74 static vlc_mutex_t omx_core_mutex = VLC_STATIC_MUTEX;
75 static unsigned int omx_refcount = 0;
76 static void *dll_handle;
77 static OMX_ERRORTYPE (*pf_init) (void);
78 static OMX_ERRORTYPE (*pf_deinit) (void);
79 static OMX_ERRORTYPE (*pf_get_handle) (OMX_HANDLETYPE *, OMX_STRING,
80 OMX_PTR, OMX_CALLBACKTYPE *);
81 static OMX_ERRORTYPE (*pf_free_handle) (OMX_HANDLETYPE);
82 static OMX_ERRORTYPE (*pf_component_enum)(OMX_STRING, OMX_U32, OMX_U32);
83 static OMX_ERRORTYPE (*pf_get_roles_of_component)(OMX_STRING, OMX_U32 *, OMX_U8 **);
85 /*****************************************************************************
87 *****************************************************************************/
88 static int OpenDecoder( vlc_object_t * );
89 static int OpenEncoder( vlc_object_t * );
90 static int OpenGeneric( vlc_object_t *, bool b_encode );
91 static void CloseGeneric( vlc_object_t * );
93 static picture_t *DecodeVideo( decoder_t *, block_t ** );
94 static aout_buffer_t *DecodeAudio ( decoder_t *, block_t ** );
95 static block_t *EncodeVideo( encoder_t *, picture_t * );
97 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE, OMX_PTR, OMX_EVENTTYPE,
98 OMX_U32, OMX_U32, OMX_PTR );
99 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE, OMX_PTR,
100 OMX_BUFFERHEADERTYPE * );
101 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE, OMX_PTR,
102 OMX_BUFFERHEADERTYPE * );
104 /*****************************************************************************
106 *****************************************************************************/
108 set_description( N_("Audio/Video decoder (using OpenMAX IL)") )
109 set_category( CAT_INPUT )
110 set_subcategory( SUBCAT_INPUT_VCODEC )
111 set_section( N_("Decoding") , NULL )
112 #if defined(USE_IOMX)
113 /* For IOMX, don't enable it automatically via priorities,
114 * enable it only via the --codec iomx command line parameter when
116 set_capability( "decoder", 0 )
118 set_capability( "decoder", 80 )
120 set_callbacks( OpenDecoder, CloseGeneric )
123 set_section( N_("Encoding") , NULL )
124 set_description( N_("Video encoder (using OpenMAX IL)") )
125 set_capability( "encoder", 0 )
126 set_callbacks( OpenEncoder, CloseGeneric )
129 /*****************************************************************************
130 * CreateComponentsList: creates a list of components matching the given role
131 *****************************************************************************/
132 static int CreateComponentsList(decoder_t *p_dec, const char *psz_role)
134 decoder_sys_t *p_sys = p_dec->p_sys;
135 char psz_name[OMX_MAX_STRINGNAME_SIZE];
136 OMX_ERRORTYPE omx_error;
138 OMX_U8 **ppsz_roles = 0;
139 unsigned int i, j, len;
141 if(!psz_role) goto end;
142 len = strlen(psz_role);
146 bool b_found = false;
148 omx_error = pf_component_enum(psz_name, OMX_MAX_STRINGNAME_SIZE, i);
149 if(omx_error != OMX_ErrorNone) break;
151 msg_Dbg(p_dec, "component %s", psz_name);
153 omx_error = pf_get_roles_of_component(psz_name, &roles, 0);
154 if(omx_error != OMX_ErrorNone || !roles) continue;
156 ppsz_roles = malloc(roles * (sizeof(OMX_U8*) + OMX_MAX_STRINGNAME_SIZE));
157 if(!ppsz_roles) continue;
159 for( j = 0; j < roles; j++ )
160 ppsz_roles[j] = ((OMX_U8 *)(&ppsz_roles[roles])) +
161 j * OMX_MAX_STRINGNAME_SIZE;
163 omx_error = pf_get_roles_of_component(psz_name, &roles, ppsz_roles);
164 if(omx_error != OMX_ErrorNone) roles = 0;
166 for(j = 0; j < roles; j++)
168 msg_Dbg(p_dec, " - role: %s", ppsz_roles[j]);
169 if(!strncmp((char *)ppsz_roles[j], psz_role, len)) b_found = true;
174 if(!b_found) continue;
176 if(p_sys->components >= MAX_COMPONENTS_LIST_SIZE)
178 msg_Dbg(p_dec, "too many matching components");
182 strncpy(p_sys->ppsz_components[p_sys->components], psz_name,
183 OMX_MAX_STRINGNAME_SIZE-1);
188 msg_Dbg(p_dec, "found %i matching components for role %s",
189 p_sys->components, psz_role);
190 for( i = 0; i < p_sys->components; i++ )
191 msg_Dbg(p_dec, "- %s", p_sys->ppsz_components[i]);
193 return p_sys->components;
196 /*****************************************************************************
197 * ImplementationSpecificWorkarounds: place-holder for implementation
198 * specific workarounds
199 *****************************************************************************/
200 static OMX_ERRORTYPE ImplementationSpecificWorkarounds(decoder_t *p_dec,
201 OmxPort *p_port, es_format_t *p_fmt)
203 decoder_sys_t *p_sys = p_dec->p_sys;
204 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
205 int i_profile = 0xFFFF, i_level = 0xFFFF;
207 /* Try to find out the profile of the video */
208 while(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
209 p_fmt->i_codec == VLC_CODEC_H264)
211 uint8_t *p = (uint8_t*)p_dec->fmt_in.p_extra;
212 if(!p || !p_dec->fmt_in.p_extra) break;
214 /* Check the profile / level */
215 if(p_dec->fmt_in.i_original_fourcc == VLC_FOURCC('a','v','c','1') &&
218 if(p_dec->fmt_in.i_extra < 12) break;
219 p_sys->i_nal_size_length = 1 + (p[4]&0x03);
220 if( !(p[5]&0x1f) ) break;
225 if(p_dec->fmt_in.i_extra < 8) break;
226 if(!p[0] && !p[1] && !p[2] && p[3] == 1) p += 4;
227 else if(!p[0] && !p[1] && p[2] == 1) p += 3;
231 if( ((*p++)&0x1f) != 7) break;
233 /* Get profile/level out of first SPS */
239 if(!strcmp(p_sys->psz_component, "OMX.TI.Video.Decoder"))
241 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
242 p_fmt->i_codec == VLC_CODEC_H264 &&
243 (i_profile != 66 || i_level > 30))
245 msg_Dbg(p_dec, "h264 profile/level not supported (0x%x, 0x%x)",
247 return OMX_ErrorNotImplemented;
250 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirOutput &&
251 p_fmt->i_codec == VLC_CODEC_I420)
253 /* I420 xvideo is slow on OMAP */
254 def->format.video.eColorFormat = OMX_COLOR_FormatCbYCrY;
255 GetVlcChromaFormat( def->format.video.eColorFormat,
256 &p_fmt->i_codec, 0 );
257 GetVlcChromaSizes( p_fmt->i_codec,
258 def->format.video.nFrameWidth,
259 def->format.video.nFrameHeight,
260 &p_port->i_frame_size, &p_port->i_frame_stride,
261 &p_port->i_frame_stride_chroma_div );
262 def->format.video.nStride = p_port->i_frame_stride;
263 def->nBufferSize = p_port->i_frame_size;
266 else if(!strcmp(p_sys->psz_component, "OMX.st.video_encoder"))
268 if(p_fmt->i_cat == VIDEO_ES)
270 /* Bellagio's encoder doesn't encode the framerate in Q16 */
271 def->format.video.xFramerate >>= 16;
274 else if (!strncmp(p_sys->psz_component, "OMX.qcom.video.decoder.",
275 strlen("OMX.qcom.video.decoder")))
277 /* qdsp6 refuses buffer size larger than 450K on input port */
278 if (def->nBufferSize > 450 * 1024)
280 def->nBufferSize = 450 * 1024;
281 p_port->i_frame_size = def->nBufferSize;
285 return OMX_ErrorNone;
288 /*****************************************************************************
289 * SetPortDefinition: set definition of the omx port based on the vlc format
290 *****************************************************************************/
291 static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
294 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
295 OMX_ERRORTYPE omx_error;
297 omx_error = OMX_GetParameter(p_port->omx_handle,
298 OMX_IndexParamPortDefinition, def);
299 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
300 omx_error, ErrorToString(omx_error));
305 def->format.video.nFrameWidth = p_fmt->video.i_width;
306 def->format.video.nFrameHeight = p_fmt->video.i_height;
307 if(def->format.video.eCompressionFormat == OMX_VIDEO_CodingUnused)
308 def->format.video.nStride = def->format.video.nFrameWidth;
309 if( p_fmt->video.i_frame_rate > 0 &&
310 p_fmt->video.i_frame_rate_base > 0 )
311 def->format.video.xFramerate = (p_fmt->video.i_frame_rate << 16) /
312 p_fmt->video.i_frame_rate_base;
314 if(def->eDir == OMX_DirInput || p_dec->p_sys->b_enc)
316 def->nBufferSize = def->format.video.nFrameWidth *
317 def->format.video.nFrameHeight * 2;
318 p_port->i_frame_size = def->nBufferSize;
320 if(!GetOmxVideoFormat(p_fmt->i_codec,
321 &def->format.video.eCompressionFormat, 0) )
323 if(!GetOmxChromaFormat(p_fmt->i_codec,
324 &def->format.video.eColorFormat, 0) )
326 omx_error = OMX_ErrorNotImplemented;
327 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
328 (char *)&p_fmt->i_codec );
330 GetVlcChromaSizes( p_fmt->i_codec,
331 def->format.video.nFrameWidth,
332 def->format.video.nFrameHeight,
333 &p_port->i_frame_size, &p_port->i_frame_stride,
334 &p_port->i_frame_stride_chroma_div );
335 def->format.video.nStride = p_port->i_frame_stride;
336 def->nBufferSize = p_port->i_frame_size;
341 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
342 &p_fmt->i_codec, 0 ) )
344 omx_error = OMX_ErrorNotImplemented;
345 CHECK_ERROR(omx_error, "OMX color format %i not supported",
346 (int)def->format.video.eColorFormat );
348 GetVlcChromaSizes( p_fmt->i_codec,
349 def->format.video.nFrameWidth,
350 def->format.video.nFrameHeight,
351 &p_port->i_frame_size, &p_port->i_frame_stride,
352 &p_port->i_frame_stride_chroma_div );
353 def->format.video.nStride = p_port->i_frame_stride;
354 def->nBufferSize = p_port->i_frame_size;
359 p_port->i_frame_size = def->nBufferSize;
360 if(def->eDir == OMX_DirInput)
362 if(!GetOmxAudioFormat(p_fmt->i_codec,
363 &def->format.audio.eEncoding, 0) )
365 omx_error = OMX_ErrorNotImplemented;
366 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
367 (char *)&p_fmt->i_codec );
372 if( !OmxToVlcAudioFormat(def->format.audio.eEncoding,
373 &p_fmt->i_codec, 0 ) )
375 omx_error = OMX_ErrorNotImplemented;
376 CHECK_ERROR(omx_error, "OMX audio encoding %i not supported",
377 (int)def->format.audio.eEncoding );
382 default: return OMX_ErrorNotImplemented;
385 omx_error = ImplementationSpecificWorkarounds(p_dec, p_port, p_fmt);
386 CHECK_ERROR(omx_error, "ImplementationSpecificWorkarounds failed (%x : %s)",
387 omx_error, ErrorToString(omx_error));
389 omx_error = OMX_SetParameter(p_port->omx_handle,
390 OMX_IndexParamPortDefinition, def);
391 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
392 omx_error, ErrorToString(omx_error));
394 omx_error = OMX_GetParameter(p_port->omx_handle,
395 OMX_IndexParamPortDefinition, def);
396 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
397 omx_error, ErrorToString(omx_error));
399 if(p_port->i_frame_size > def->nBufferSize)
400 def->nBufferSize = p_port->i_frame_size;
401 p_port->i_frame_size = def->nBufferSize;
403 /* Deal with audio params */
404 if(p_fmt->i_cat == AUDIO_ES)
406 omx_error = SetAudioParameters(p_port->omx_handle,
407 &p_port->format_param, def->nPortIndex,
408 def->format.audio.eEncoding,
409 p_fmt->audio.i_channels,
412 p_fmt->audio.i_bitspersample,
413 p_fmt->audio.i_blockalign);
414 CHECK_ERROR(omx_error, "SetAudioParameters failed (%x : %s)",
415 omx_error, ErrorToString(omx_error));
422 /*****************************************************************************
423 * GetPortDefinition: set vlc format based on the definition of the omx port
424 *****************************************************************************/
425 static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
428 decoder_sys_t *p_sys = p_dec->p_sys;
429 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
430 OMX_ERRORTYPE omx_error;
432 omx_error = OMX_GetParameter(p_port->omx_handle,
433 OMX_IndexParamPortDefinition, def);
434 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
435 omx_error, ErrorToString(omx_error));
440 p_fmt->video.i_width = def->format.video.nFrameWidth;
441 p_fmt->video.i_visible_width = def->format.video.nFrameWidth;
442 p_fmt->video.i_height = def->format.video.nFrameHeight;
443 p_fmt->video.i_visible_height = def->format.video.nFrameHeight;
444 p_fmt->video.i_frame_rate = p_dec->fmt_in.video.i_frame_rate;
445 p_fmt->video.i_frame_rate_base = p_dec->fmt_in.video.i_frame_rate_base;
447 /* Hack: Nexus One (stock firmware with binary OMX driver blob)
448 * claims to output 420Planar even though it in in practice is
450 if(def->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar &&
451 !strncmp(p_sys->psz_component, "OMX.qcom.video.decoder",
452 strlen("OMX.qcom.video.decoder")))
453 def->format.video.eColorFormat = OMX_QCOM_COLOR_FormatYVU420SemiPlanar;
455 /* Hack: Galaxy S II (stock firmware) gives a slice height larger
456 * than the video height, but this doesn't imply padding between
457 * the video planes. Nexus S also has a slice height larger than
458 * the video height, but there it actually is real padding, thus
459 * Galaxy S II is the buggy one. The Galaxy S II decoder is
460 * named OMX.SEC.avcdec while the one on Nexus S is
461 * OMX.SEC.AVC.Decoder. Thus do this for any OMX.SEC. that don't
462 * contain the string ".Decoder". */
463 if(!strncmp(p_sys->psz_component, "OMX.SEC.", strlen("OMX.SEC.")) &&
464 !strstr(p_sys->psz_component, ".Decoder"))
465 def->format.video.nSliceHeight = 0;
467 if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
468 &p_fmt->i_codec, 0 ) )
470 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
471 &p_fmt->i_codec, 0 ) )
473 omx_error = OMX_ErrorNotImplemented;
474 CHECK_ERROR(omx_error, "OMX color format %i not supported",
475 (int)def->format.video.eColorFormat );
477 GetVlcChromaSizes( p_fmt->i_codec,
478 def->format.video.nFrameWidth,
479 def->format.video.nFrameHeight,
480 &p_port->i_frame_size, &p_port->i_frame_stride,
481 &p_port->i_frame_stride_chroma_div );
483 if(p_port->i_frame_size > def->nBufferSize)
484 def->nBufferSize = p_port->i_frame_size;
485 p_port->i_frame_size = def->nBufferSize;
487 if((int)p_port->i_frame_stride > def->format.video.nStride)
488 def->format.video.nStride = p_port->i_frame_stride;
490 p_port->i_frame_stride = def->format.video.nStride;
494 if( !OmxToVlcAudioFormat( def->format.audio.eEncoding,
495 &p_fmt->i_codec, 0 ) )
497 omx_error = OMX_ErrorNotImplemented;
498 CHECK_ERROR(omx_error, "OMX audio format %i not supported",
499 (int)def->format.audio.eEncoding );
502 omx_error = GetAudioParameters(p_port->omx_handle,
503 &p_port->format_param, def->nPortIndex,
504 def->format.audio.eEncoding,
505 &p_fmt->audio.i_channels,
506 &p_fmt->audio.i_rate,
508 &p_fmt->audio.i_bitspersample,
509 &p_fmt->audio.i_blockalign);
510 CHECK_ERROR(omx_error, "GetAudioParameters failed (%x : %s)",
511 omx_error, ErrorToString(omx_error));
513 if(p_fmt->audio.i_channels < 9)
515 static const int pi_channels_maps[9] =
517 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
518 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
519 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
520 | AOUT_CHAN_REARRIGHT,
521 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
522 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
523 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
524 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
525 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
526 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
527 | AOUT_CHAN_MIDDLERIGHT,
528 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
529 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
532 p_fmt->audio.i_physical_channels =
533 p_fmt->audio.i_original_channels =
534 pi_channels_maps[p_fmt->audio.i_channels];
537 date_Init( &p_dec->p_sys->end_date, p_fmt->audio.i_rate, 1 );
541 default: return OMX_ErrorNotImplemented;
548 /*****************************************************************************
549 * DeinitialiseComponent: Deinitialise and unload an OMX component
550 *****************************************************************************/
551 static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
552 OMX_HANDLETYPE omx_handle)
554 decoder_sys_t *p_sys = p_dec->p_sys;
555 OMX_ERRORTYPE omx_error;
559 if(!omx_handle) return OMX_ErrorNone;
561 omx_error = OMX_GetState(omx_handle, &state);
562 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
564 if(state == OMX_StateExecuting)
566 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
568 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
569 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
570 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
573 omx_error = OMX_GetState(omx_handle, &state);
574 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
576 if(state == OMX_StateIdle)
578 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
579 OMX_StateLoaded, 0 );
580 CHECK_ERROR(omx_error, "OMX_CommandStateSet Loaded failed (%x)", omx_error );
582 for(i = 0; i < p_sys->ports; i++)
584 OmxPort *p_port = &p_sys->p_ports[i];
585 OMX_BUFFERHEADERTYPE *p_buffer;
587 for(j = 0; j < p_port->i_buffers; j++)
589 OMX_FIFO_GET(&p_port->fifo, p_buffer);
590 if (p_buffer == &p_sys->sentinel_buffer)
592 omx_error = OMX_FreeBuffer( omx_handle,
593 p_port->i_port_index, p_buffer );
595 if(omx_error != OMX_ErrorNone) break;
597 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
598 omx_error, (int)p_port->i_port_index, j );
601 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
602 CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
606 for(i = 0; i < p_sys->ports; i++)
608 OmxPort *p_port = &p_sys->p_ports[i];
609 free(p_port->pp_buffers);
610 p_port->pp_buffers = 0;
612 omx_error = pf_free_handle( omx_handle );
616 /*****************************************************************************
617 * InitialiseComponent: Load and initialise an OMX component
618 *****************************************************************************/
619 static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
620 OMX_STRING psz_component, OMX_HANDLETYPE *p_handle)
622 static OMX_CALLBACKTYPE callbacks =
623 { OmxEventHandler, OmxEmptyBufferDone, OmxFillBufferDone };
624 decoder_sys_t *p_sys = p_dec->p_sys;
625 OMX_HANDLETYPE omx_handle;
626 OMX_ERRORTYPE omx_error;
628 OMX_U8 psz_role[OMX_MAX_STRINGNAME_SIZE];
629 OMX_PARAM_COMPONENTROLETYPE role;
630 OMX_PARAM_PORTDEFINITIONTYPE definition;
631 OMX_PORT_PARAM_TYPE param;
634 omx_error = pf_get_handle( &omx_handle, psz_component, p_dec, &callbacks );
635 if(omx_error != OMX_ErrorNone)
637 msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
638 omx_error, ErrorToString(omx_error) );
641 strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
643 omx_error = OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
644 if(omx_error == OMX_ErrorNone)
645 msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
647 msg_Dbg(p_dec, "loaded component %s", psz_component);
648 PrintOmx(p_dec, omx_handle, OMX_ALL);
650 /* Set component role */
651 OMX_INIT_STRUCTURE(role);
652 strcpy((char*)role.cRole,
653 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec : p_dec->fmt_in.i_codec,
654 p_dec->fmt_in.i_cat, p_sys->b_enc));
656 omx_error = OMX_SetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
658 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
660 if(omx_error == OMX_ErrorNone)
661 msg_Dbg(p_dec, "component standard role set to %s", role.cRole);
663 /* Find the input / output ports */
664 OMX_INIT_STRUCTURE(param);
665 OMX_INIT_STRUCTURE(definition);
666 omx_error = OMX_GetParameter(omx_handle, p_dec->fmt_in.i_cat == VIDEO_ES ?
667 OMX_IndexParamVideoInit : OMX_IndexParamAudioInit, ¶m);
668 if(omx_error != OMX_ErrorNone) param.nPorts = 0;
670 for(i = 0; i < param.nPorts; i++)
674 /* Get port definition */
675 definition.nPortIndex = param.nStartPortNumber + i;
676 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
678 if(omx_error != OMX_ErrorNone) continue;
680 if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
681 else p_port = &p_sys->out;
683 p_port->b_valid = true;
684 p_port->i_port_index = definition.nPortIndex;
685 p_port->definition = definition;
686 p_port->omx_handle = omx_handle;
689 if(!p_sys->in.b_valid || !p_sys->out.b_valid)
691 omx_error = OMX_ErrorInvalidComponent;
692 CHECK_ERROR(omx_error, "couldn't find an input and output port");
695 if(!strncmp(p_sys->psz_component, "OMX.SEC.", 8))
698 omx_error = OMX_GetExtensionIndex(omx_handle, (OMX_STRING) "OMX.SEC.index.ThumbnailMode", &index);
699 if(omx_error == OMX_ErrorNone)
701 OMX_BOOL enable = OMX_TRUE;
702 omx_error = OMX_SetConfig(omx_handle, index, &enable);
703 CHECK_ERROR(omx_error, "Unable to set ThumbnailMode");
705 OMX_BOOL enable = OMX_TRUE;
706 /* Needed on Samsung Galaxy S II */
707 omx_error = OMX_SetConfig(omx_handle, OMX_IndexVendorSetYUV420pMode, &enable);
708 if (omx_error == OMX_ErrorNone)
709 msg_Dbg(p_dec, "Set OMX_IndexVendorSetYUV420pMode successfully");
711 msg_Dbg(p_dec, "Unable to set OMX_IndexVendorSetYUV420pMode: %x", omx_error);
715 /* Set port definitions */
716 for(i = 0; i < p_sys->ports; i++)
718 omx_error = SetPortDefinition(p_dec, &p_sys->p_ports[i],
719 p_sys->p_ports[i].p_fmt);
720 if(omx_error != OMX_ErrorNone) goto error;
723 /* Allocate our array for the omx buffers and enable ports */
724 for(i = 0; i < p_sys->ports; i++)
726 OmxPort *p_port = &p_sys->p_ports[i];
729 malloc(p_port->definition.nBufferCountActual *
730 sizeof(OMX_BUFFERHEADERTYPE*));
731 if(!p_port->pp_buffers)
733 omx_error = OMX_ErrorInsufficientResources;
734 CHECK_ERROR(omx_error, "memory allocation failed");
736 p_port->i_buffers = p_port->definition.nBufferCountActual;
739 if(!p_port->definition.bEnabled)
741 omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
742 p_port->i_port_index, NULL);
743 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
744 (int)p_port->i_port_index, omx_error );
745 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
746 CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
747 (int)p_port->i_port_index, omx_error );
751 *p_handle = omx_handle;
752 return OMX_ErrorNone;
755 DeinitialiseComponent(p_dec, omx_handle);
760 /*****************************************************************************
761 * OpenDecoder: Create the decoder instance
762 *****************************************************************************/
763 static int OpenDecoder( vlc_object_t *p_this )
765 decoder_t *p_dec = (decoder_t*)p_this;
768 if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
772 if( p_dec->fmt_in.i_cat != VIDEO_ES && !p_dec->b_force)
776 status = OpenGeneric( p_this, false );
777 if(status != VLC_SUCCESS) return status;
779 p_dec->pf_decode_video = DecodeVideo;
780 p_dec->pf_decode_audio = DecodeAudio;
785 /*****************************************************************************
786 * OpenEncoder: Create the encoder instance
787 *****************************************************************************/
788 static int OpenEncoder( vlc_object_t *p_this )
790 encoder_t *p_enc = (encoder_t*)p_this;
793 if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
796 status = OpenGeneric( p_this, true );
797 if(status != VLC_SUCCESS) return status;
799 p_enc->pf_encode_video = EncodeVideo;
804 /*****************************************************************************
805 * OpenGeneric: Create the generic decoder/encoder instance
806 *****************************************************************************/
807 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
809 decoder_t *p_dec = (decoder_t*)p_this;
810 decoder_sys_t *p_sys;
811 OMX_ERRORTYPE omx_error;
812 OMX_BUFFERHEADERTYPE *p_header;
815 vlc_mutex_lock( &omx_core_mutex );
816 if( omx_refcount > 0 )
819 /* Load the OMX core */
820 for( i = 0; ppsz_dll_list[i]; i++ )
822 dll_handle = dll_open( ppsz_dll_list[i] );
823 if( dll_handle ) break;
827 vlc_mutex_unlock( &omx_core_mutex );
831 pf_init = dlsym( dll_handle, "OMX_Init" );
832 pf_deinit = dlsym( dll_handle, "OMX_Deinit" );
833 pf_get_handle = dlsym( dll_handle, "OMX_GetHandle" );
834 pf_free_handle = dlsym( dll_handle, "OMX_FreeHandle" );
835 pf_component_enum = dlsym( dll_handle, "OMX_ComponentNameEnum" );
836 pf_get_roles_of_component = dlsym( dll_handle, "OMX_GetRolesOfComponent" );
837 if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle ||
838 !pf_component_enum || !pf_get_roles_of_component )
840 msg_Warn( p_this, "cannot find OMX_* symbols in `%s' (%s)",
841 ppsz_dll_list[i], dlerror() );
842 dll_close(dll_handle);
843 vlc_mutex_unlock( &omx_core_mutex );
848 /* Allocate the memory needed to store the decoder's structure */
849 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
851 if( omx_refcount == 0 )
852 dll_close(dll_handle);
853 vlc_mutex_unlock( &omx_core_mutex );
857 /* Initialise the thread properties */
860 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
861 p_dec->fmt_out.video = p_dec->fmt_in.video;
862 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
863 p_dec->fmt_out.i_codec = 0;
865 p_sys->b_enc = b_encode;
866 p_sys->pp_last_event = &p_sys->p_events;
867 vlc_mutex_init (&p_sys->mutex);
868 vlc_cond_init (&p_sys->cond);
869 vlc_mutex_init (&p_sys->lock);
870 vlc_mutex_init (&p_sys->in.fifo.lock);
871 vlc_cond_init (&p_sys->in.fifo.wait);
872 p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
873 p_sys->in.fifo.pp_last = &p_sys->in.fifo.p_first;
874 p_sys->in.b_direct = false;
875 p_sys->in.b_flushed = true;
876 p_sys->in.p_fmt = &p_dec->fmt_in;
877 vlc_mutex_init (&p_sys->out.fifo.lock);
878 vlc_cond_init (&p_sys->out.fifo.wait);
879 p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
880 p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
881 p_sys->out.b_direct = true;
882 p_sys->out.b_flushed = true;
883 p_sys->out.p_fmt = &p_dec->fmt_out;
885 p_sys->p_ports = &p_sys->in;
887 msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
888 (char *)&p_dec->fmt_out.i_codec);
890 /* Initialise the OMX core */
891 omx_error = omx_refcount > 0 ? OMX_ErrorNone : pf_init();
893 if(omx_error != OMX_ErrorNone)
895 msg_Warn( p_this, "OMX_Init failed (%x: %s)", omx_error,
896 ErrorToString(omx_error) );
897 vlc_mutex_unlock( &omx_core_mutex );
898 CloseGeneric(p_this);
901 p_sys->b_init = true;
902 vlc_mutex_unlock( &omx_core_mutex );
904 /* Enumerate components and build a list of the one we want to try */
905 if( !CreateComponentsList(p_dec,
906 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
907 p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
910 msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
911 (char *)&p_dec->fmt_in.i_codec );
912 CloseGeneric(p_this);
916 /* Try to load and initialise a component */
917 omx_error = OMX_ErrorUndefined;
918 for(i = 0; i < p_sys->components; i++)
921 /* ignore OpenCore software codecs */
922 if (!strncmp(p_sys->ppsz_components[i], "OMX.PV.", 7))
924 /* The same sw codecs, renamed in ICS (perhaps also in honeycomb) */
925 if (!strncmp(p_sys->ppsz_components[i], "OMX.google.", 11))
928 omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
930 if(omx_error == OMX_ErrorNone) break;
932 CHECK_ERROR(omx_error, "no component could be initialised" );
934 /* Move component to Idle then Executing state */
935 OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
936 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
938 /* Allocate omx buffers */
939 for(i = 0; i < p_sys->ports; i++)
941 OmxPort *p_port = &p_sys->p_ports[i];
943 for(j = 0; j < p_port->i_buffers; j++)
946 #define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
947 char *p_buf = malloc(p_port->definition.nBufferSize +
948 p_port->definition.nBufferAlignment);
949 p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
952 if(0 && p_port->b_direct)
954 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
955 p_port->i_port_index, 0,
956 p_port->definition.nBufferSize, (void*)1);
959 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
960 p_port->i_port_index, 0,
961 p_port->definition.nBufferSize);
963 if(omx_error != OMX_ErrorNone) break;
964 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
966 p_port->i_buffers = j;
967 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
968 omx_error, (int)p_port->i_port_index, j );
971 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
972 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
974 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
975 OMX_StateExecuting, 0);
976 CHECK_ERROR(omx_error, "OMX_CommandStateSet Executing failed (%x)", omx_error );
977 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
978 CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
980 /* Send codec configuration data */
981 if( p_dec->fmt_in.i_extra )
983 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
984 p_header->nFilledLen = p_dec->fmt_in.i_extra;
986 /* Convert H.264 NAL format to annex b */
987 if( p_sys->i_nal_size_length && !p_sys->in.b_direct )
989 p_header->nFilledLen = 0;
990 convert_sps_pps( p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
991 p_header->pBuffer, p_header->nAllocLen,
992 (uint32_t*) &p_header->nFilledLen, NULL );
994 else if(p_sys->in.b_direct)
996 p_header->pOutputPortPrivate = p_header->pBuffer;
997 p_header->pBuffer = p_dec->fmt_in.p_extra;
1001 if(p_header->nFilledLen > p_header->nAllocLen)
1003 msg_Dbg(p_dec, "buffer too small (%i,%i)", (int)p_header->nFilledLen,
1004 (int)p_header->nAllocLen);
1005 p_header->nFilledLen = p_header->nAllocLen;
1007 memcpy(p_header->pBuffer, p_dec->fmt_in.p_extra, p_header->nFilledLen);
1010 p_header->nOffset = 0;
1011 p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG;
1012 msg_Dbg(p_dec, "sending codec config data %p, %p, %i", p_header,
1013 p_header->pBuffer, (int)p_header->nFilledLen);
1014 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1017 /* Get back output port definition */
1018 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1019 if(omx_error != OMX_ErrorNone) goto error;
1021 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1022 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1024 if(p_sys->b_error) goto error;
1026 p_dec->b_need_packetized = true;
1030 CloseGeneric(p_this);
1031 return VLC_EGENERIC;
1034 /*****************************************************************************
1036 *****************************************************************************/
1037 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
1039 decoder_sys_t *p_sys = p_dec->p_sys;
1040 OMX_PARAM_PORTDEFINITIONTYPE definition;
1041 OMX_BUFFERHEADERTYPE *p_buffer;
1042 OMX_ERRORTYPE omx_error;
1045 /* Sanity checking */
1046 OMX_INIT_STRUCTURE(definition);
1047 definition.nPortIndex = p_port->i_port_index;
1048 omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1050 if(omx_error != OMX_ErrorNone || (p_dec->fmt_in.i_cat == VIDEO_ES &&
1051 (!definition.format.video.nFrameWidth ||
1052 !definition.format.video.nFrameHeight)) )
1053 return OMX_ErrorUndefined;
1055 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
1056 p_port->i_port_index, NULL);
1057 CHECK_ERROR(omx_error, "OMX_CommandPortDisable on %i failed (%x)",
1058 (int)p_port->i_port_index, omx_error );
1060 for(i = 0; i < p_port->i_buffers; i++)
1062 OMX_FIFO_GET(&p_port->fifo, p_buffer);
1063 if (p_buffer == &p_sys->sentinel_buffer)
1065 omx_error = OMX_FreeBuffer( p_sys->omx_handle,
1066 p_port->i_port_index, p_buffer );
1068 if(omx_error != OMX_ErrorNone) break;
1070 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
1071 omx_error, (int)p_port->i_port_index, i );
1073 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1074 CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
1076 /* Get the new port definition */
1077 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1078 if(omx_error != OMX_ErrorNone) goto error;
1080 if( p_dec->fmt_in.i_cat != AUDIO_ES )
1082 /* Don't explicitly set the new parameters that we got with
1083 * OMX_GetParameter above when using audio codecs.
1084 * That struct hasn't been changed since, so there should be
1085 * no need to set it here, unless some codec expects the
1086 * SetParameter call as a trigger event for some part of
1087 * the reconfiguration.
1088 * This fixes using audio decoders on Samsung Galaxy S II,
1090 * Only skipping this for audio codecs, to minimize the
1091 * change for current working configurations for video.
1093 omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1095 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
1096 omx_error, ErrorToString(omx_error));
1099 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
1100 p_port->i_port_index, NULL);
1101 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
1102 (int)p_port->i_port_index, omx_error );
1104 if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
1105 free(p_port->pp_buffers);
1106 p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
1107 if(!p_port->pp_buffers)
1109 omx_error = OMX_ErrorInsufficientResources;
1110 CHECK_ERROR(omx_error, "memory allocation failed");
1113 p_port->i_buffers = p_port->definition.nBufferCountActual;
1114 for(i = 0; i < p_port->i_buffers; i++)
1116 if(0 && p_port->b_direct)
1118 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1119 p_port->i_port_index, 0,
1120 p_port->definition.nBufferSize, (void*)1);
1123 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1124 p_port->i_port_index, 0,
1125 p_port->definition.nBufferSize);
1127 if(omx_error != OMX_ErrorNone) break;
1128 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1130 p_port->i_buffers = i;
1131 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1132 omx_error, (int)p_port->i_port_index, i );
1134 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1135 CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1137 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1138 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1144 /*****************************************************************************
1145 * DecodeVideo: Called to decode one frame
1146 *****************************************************************************/
1147 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1149 decoder_sys_t *p_sys = p_dec->p_sys;
1150 picture_t *p_pic = NULL, *p_next_pic;
1151 OMX_ERRORTYPE omx_error;
1154 OMX_BUFFERHEADERTYPE *p_header;
1157 if( !pp_block || !*pp_block )
1160 p_block = *pp_block;
1162 /* Check for errors from codec */
1165 msg_Dbg(p_dec, "error during decoding");
1166 block_Release( p_block );
1170 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1172 block_Release( p_block );
1173 if(!p_sys->in.b_flushed)
1175 msg_Dbg(p_dec, "flushing");
1176 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1177 p_sys->in.definition.nPortIndex, 0 );
1179 p_sys->in.b_flushed = true;
1183 /* Take care of decoded frames first */
1186 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1187 if(!p_header) break; /* No frame available */
1189 if(p_header->nFilledLen)
1191 p_pic = p_header->pAppPrivate;
1194 /* We're not in direct rendering mode.
1195 * Get a new picture and copy the content */
1196 p_pic = decoder_NewPicture( p_dec );
1197 if( !p_pic ) break; /* No picture available */
1199 CopyOmxPicture(p_dec, p_pic, p_header, p_sys->out.definition.format.video.nSliceHeight);
1202 p_pic->date = p_header->nTimeStamp;
1203 p_header->nFilledLen = 0;
1204 p_header->pAppPrivate = 0;
1207 /* Get a new picture */
1208 if(p_sys->in.b_direct && !p_header->pAppPrivate)
1210 p_next_pic = decoder_NewPicture( p_dec );
1211 if(!p_next_pic) break;
1213 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1214 p_header->pAppPrivate = p_next_pic;
1215 p_header->pInputPortPrivate = p_header->pBuffer;
1216 p_header->pBuffer = p_next_pic->p[0].p_pixels;
1220 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1223 #ifdef OMXIL_EXTRA_DEBUG
1224 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1226 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1229 /* Send the input buffer to the component */
1230 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1232 if (p_header && p_header->nFlags & OMX_BUFFERFLAG_EOS)
1237 p_header->nFilledLen = p_block->i_buffer;
1238 p_header->nOffset = 0;
1239 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1240 p_header->nTimeStamp = p_block->i_dts;
1242 /* In direct mode we pass the input pointer as is.
1243 * Otherwise we memcopy the data */
1244 if(p_sys->in.b_direct)
1246 p_header->pOutputPortPrivate = p_header->pBuffer;
1247 p_header->pBuffer = p_block->p_buffer;
1248 p_header->pAppPrivate = p_block;
1252 if(p_header->nFilledLen > p_header->nAllocLen)
1254 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1255 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1256 p_header->nFilledLen = p_header->nAllocLen;
1258 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1259 block_Release(p_block);
1262 /* Convert H.264 NAL format to annex b */
1263 if( p_sys->i_nal_size_length >= 3 && p_sys->i_nal_size_length <= 4 )
1265 /* This only works for NAL sizes 3-4 */
1266 int i_len = p_header->nFilledLen, i;
1267 uint8_t* ptr = p_header->pBuffer;
1268 while( i_len >= p_sys->i_nal_size_length )
1270 uint32_t nal_len = 0;
1271 for( i = 0; i < p_sys->i_nal_size_length; i++ ) {
1272 nal_len = (nal_len << 8) | ptr[i];
1275 ptr[p_sys->i_nal_size_length - 1] = 1;
1276 if( nal_len > INT_MAX || nal_len > (unsigned int) i_len )
1279 i_len -= nal_len + 4;
1282 #ifdef OMXIL_EXTRA_DEBUG
1283 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1284 (int)p_header->nFilledLen );
1286 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1287 p_sys->in.b_flushed = false;
1288 *pp_block = NULL; /* Avoid being fed the same packet again */
1292 /* Handle the PortSettingsChanged events */
1293 for(i = 0; i < p_sys->ports; i++)
1295 OmxPort *p_port = &p_sys->p_ports[i];
1296 if(!p_port->b_reconfigure) continue;
1297 p_port->b_reconfigure = 0;
1298 omx_error = PortReconfigure(p_dec, p_port);
1304 /*****************************************************************************
1305 * DecodeAudio: Called to decode one frame
1306 *****************************************************************************/
1307 aout_buffer_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1309 decoder_sys_t *p_sys = p_dec->p_sys;
1310 aout_buffer_t *p_buffer = 0;
1311 OMX_BUFFERHEADERTYPE *p_header;
1312 OMX_ERRORTYPE omx_error;
1316 if( !pp_block || !*pp_block ) return NULL;
1318 p_block = *pp_block;
1320 /* Check for errors from codec */
1323 msg_Dbg(p_dec, "error during decoding");
1324 block_Release( p_block );
1328 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1330 block_Release( p_block );
1331 date_Set( &p_sys->end_date, 0 );
1332 if(!p_sys->in.b_flushed)
1334 msg_Dbg(p_dec, "flushing");
1335 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1336 p_sys->in.definition.nPortIndex, 0 );
1338 p_sys->in.b_flushed = true;
1342 if( !date_Get( &p_sys->end_date ) )
1344 if( !p_block->i_pts )
1346 /* We've just started the stream, wait for the first PTS. */
1347 block_Release( p_block );
1350 date_Set( &p_sys->end_date, p_block->i_pts );
1353 /* Take care of decoded frames first */
1356 unsigned int i_samples;
1358 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1359 if(!p_header) break; /* No frame available */
1361 i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1364 p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1365 if( !p_buffer ) break; /* No audio buffer available */
1367 memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1368 p_header->nFilledLen = 0;
1370 if( p_header->nTimeStamp != 0 &&
1371 p_header->nTimeStamp != date_Get( &p_sys->end_date ) )
1372 date_Set( &p_sys->end_date, p_header->nTimeStamp );
1374 p_buffer->i_pts = date_Get( &p_sys->end_date );
1375 p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1379 #ifdef OMXIL_EXTRA_DEBUG
1380 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1382 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1383 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1387 /* Send the input buffer to the component */
1388 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1390 if (p_header && p_header->nFlags & OMX_BUFFERFLAG_EOS)
1395 p_header->nFilledLen = p_block->i_buffer;
1396 p_header->nOffset = 0;
1397 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1398 p_header->nTimeStamp = p_block->i_dts;
1400 /* In direct mode we pass the input pointer as is.
1401 * Otherwise we memcopy the data */
1402 if(p_sys->in.b_direct)
1404 p_header->pOutputPortPrivate = p_header->pBuffer;
1405 p_header->pBuffer = p_block->p_buffer;
1406 p_header->pAppPrivate = p_block;
1410 if(p_header->nFilledLen > p_header->nAllocLen)
1412 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1413 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1414 p_header->nFilledLen = p_header->nAllocLen;
1416 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1417 block_Release(p_block);
1420 #ifdef OMXIL_EXTRA_DEBUG
1421 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1422 (int)p_header->nFilledLen );
1424 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1425 p_sys->in.b_flushed = false;
1426 *pp_block = NULL; /* Avoid being fed the same packet again */
1430 /* Handle the PortSettingsChanged events */
1431 for(i = 0; i < p_sys->ports; i++)
1433 OmxPort *p_port = &p_sys->p_ports[i];
1434 if(!p_port->b_reconfigure) continue;
1435 p_port->b_reconfigure = 0;
1436 omx_error = PortReconfigure(p_dec, p_port);
1442 /*****************************************************************************
1443 * EncodeVideo: Called to encode one frame
1444 *****************************************************************************/
1445 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1447 decoder_t *p_dec = ( decoder_t *)p_enc;
1448 decoder_sys_t *p_sys = p_dec->p_sys;
1449 OMX_ERRORTYPE omx_error;
1452 OMX_BUFFERHEADERTYPE *p_header;
1453 block_t *p_block = 0;
1455 if( !p_pic ) return NULL;
1457 /* Check for errors from codec */
1460 msg_Dbg(p_dec, "error during encoding");
1464 /* Send the input buffer to the component */
1465 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1468 /* In direct mode we pass the input pointer as is.
1469 * Otherwise we memcopy the data */
1470 if(p_sys->in.b_direct)
1472 p_header->pOutputPortPrivate = p_header->pBuffer;
1473 p_header->pBuffer = p_pic->p[0].p_pixels;
1477 CopyVlcPicture(p_dec, p_header, p_pic);
1480 p_header->nFilledLen = p_sys->in.i_frame_size;
1481 p_header->nOffset = 0;
1482 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1483 p_header->nTimeStamp = p_pic->date;
1484 #ifdef OMXIL_EXTRA_DEBUG
1485 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1486 (int)p_header->nFilledLen );
1488 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1489 p_sys->in.b_flushed = false;
1492 /* Handle the PortSettingsChanged events */
1493 for(i = 0; i < p_sys->ports; i++)
1495 OmxPort *p_port = &p_sys->p_ports[i];
1496 if(!p_port->b_reconfigure) continue;
1497 p_port->b_reconfigure = 0;
1498 omx_error = PortReconfigure(p_dec, p_port);
1501 /* Wait for the decoded frame */
1504 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1506 if(p_header->nFilledLen)
1508 if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1510 /* TODO: need to store codec config */
1511 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1514 p_block = p_header->pAppPrivate;
1517 /* We're not in direct rendering mode.
1518 * Get a new block and copy the content */
1519 p_block = block_New( p_dec, p_header->nFilledLen );
1520 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1523 p_block->i_buffer = p_header->nFilledLen;
1524 p_block->i_pts = p_block->i_dts = p_header->nTimeStamp;
1525 p_header->nFilledLen = 0;
1526 p_header->pAppPrivate = 0;
1529 #ifdef OMXIL_EXTRA_DEBUG
1530 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1532 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1535 msg_Dbg(p_dec, "done");
1539 /*****************************************************************************
1540 * CloseGeneric: omxil decoder destruction
1541 *****************************************************************************/
1542 static void CloseGeneric( vlc_object_t *p_this )
1544 decoder_t *p_dec = (decoder_t *)p_this;
1545 decoder_sys_t *p_sys = p_dec->p_sys;
1547 if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1548 vlc_mutex_lock( &omx_core_mutex );
1550 if( omx_refcount == 0 )
1552 if( p_sys->b_init ) pf_deinit();
1553 dll_close( dll_handle );
1555 vlc_mutex_unlock( &omx_core_mutex );
1557 vlc_mutex_destroy (&p_sys->mutex);
1558 vlc_cond_destroy (&p_sys->cond);
1559 vlc_mutex_destroy (&p_sys->in.fifo.lock);
1560 vlc_cond_destroy (&p_sys->in.fifo.wait);
1561 vlc_mutex_destroy (&p_sys->out.fifo.lock);
1562 vlc_cond_destroy (&p_sys->out.fifo.wait);
1567 /*****************************************************************************
1569 *****************************************************************************/
1570 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1571 OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1572 OMX_U32 data_2, OMX_PTR event_data )
1574 decoder_t *p_dec = (decoder_t *)app_data;
1575 decoder_sys_t *p_sys = p_dec->p_sys;
1581 case OMX_EventCmdComplete:
1582 switch ((OMX_STATETYPE)data_1)
1584 case OMX_CommandStateSet:
1585 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %s)", EventToString(event),
1586 CommandToString(data_1), StateToString(data_2) );
1590 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u)", EventToString(event),
1591 CommandToString(data_1), (unsigned int)data_2 );
1596 case OMX_EventError:
1597 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u, %s)", EventToString(event),
1598 ErrorToString((OMX_ERRORTYPE)data_1), (unsigned int)data_2,
1599 (const char *)event_data);
1600 //p_sys->b_error = true;
1603 case OMX_EventPortSettingsChanged:
1604 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1605 (unsigned int)data_1, (unsigned int)data_2 );
1606 for(i = 0; i < p_sys->ports; i++)
1607 if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1608 p_sys->p_ports[i].b_reconfigure = true;
1609 memset(&p_sys->sentinel_buffer, 0, sizeof(p_sys->sentinel_buffer));
1610 p_sys->sentinel_buffer.nFlags = OMX_BUFFERFLAG_EOS;
1611 OMX_FIFO_PUT(&p_sys->in.fifo, &p_sys->sentinel_buffer);
1615 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1616 (unsigned int)data_1, (unsigned int)data_2 );
1620 PostOmxEvent(p_dec, event, data_1, data_2, event_data);
1621 return OMX_ErrorNone;
1624 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1625 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1627 decoder_t *p_dec = (decoder_t *)app_data;
1628 decoder_sys_t *p_sys = p_dec->p_sys;
1631 #ifdef OMXIL_EXTRA_DEBUG
1632 msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1635 if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1637 block_t *p_block = (block_t *)omx_header->pAppPrivate;
1638 omx_header->pBuffer = omx_header->pOutputPortPrivate;
1639 if(p_block) block_Release(p_block);
1640 omx_header->pAppPrivate = 0;
1642 OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1644 return OMX_ErrorNone;
1647 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1648 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1650 decoder_t *p_dec = (decoder_t *)app_data;
1651 decoder_sys_t *p_sys = p_dec->p_sys;
1654 #ifdef OMXIL_EXTRA_DEBUG
1655 msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
1656 (int)omx_header->nFilledLen );
1659 if(omx_header->pInputPortPrivate)
1661 omx_header->pBuffer = omx_header->pInputPortPrivate;
1663 OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1665 return OMX_ErrorNone;