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));
416 if (!strcmp(p_dec->p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER") &&
417 def->eDir == OMX_DirOutput)
419 /* When setting the output buffer size above, the decoder actually
420 * sets the buffer size to a lower value than what was chosen. If
421 * we try to allocate buffers of this size, it fails. Thus, forcibly
422 * use a larger buffer size. */
423 def->nBufferSize *= 2;
430 /*****************************************************************************
431 * GetPortDefinition: set vlc format based on the definition of the omx port
432 *****************************************************************************/
433 static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
436 decoder_sys_t *p_sys = p_dec->p_sys;
437 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
438 OMX_ERRORTYPE omx_error;
439 OMX_CONFIG_RECTTYPE crop_rect;
441 omx_error = OMX_GetParameter(p_port->omx_handle,
442 OMX_IndexParamPortDefinition, def);
443 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
444 omx_error, ErrorToString(omx_error));
449 p_fmt->video.i_width = def->format.video.nFrameWidth;
450 p_fmt->video.i_visible_width = def->format.video.nFrameWidth;
451 p_fmt->video.i_height = def->format.video.nFrameHeight;
452 p_fmt->video.i_visible_height = def->format.video.nFrameHeight;
453 p_fmt->video.i_frame_rate = p_dec->fmt_in.video.i_frame_rate;
454 p_fmt->video.i_frame_rate_base = p_dec->fmt_in.video.i_frame_rate_base;
456 OMX_INIT_STRUCTURE(crop_rect);
457 crop_rect.nPortIndex = def->nPortIndex;
458 omx_error = OMX_GetConfig(p_port->omx_handle, OMX_IndexConfigCommonOutputCrop, &crop_rect);
459 if (omx_error == OMX_ErrorNone)
461 p_fmt->video.i_width = crop_rect.nWidth;
462 p_fmt->video.i_visible_width = crop_rect.nWidth;
463 p_fmt->video.i_height = crop_rect.nHeight;
464 p_fmt->video.i_visible_height = crop_rect.nHeight;
465 if (def->format.video.eColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
466 def->format.video.nSliceHeight -= crop_rect.nTop/2;
470 /* Don't pass the error back to the caller, this isn't mandatory */
471 omx_error = OMX_ErrorNone;
474 /* Hack: Nexus One (stock firmware with binary OMX driver blob)
475 * claims to output 420Planar even though it in in practice is
477 if(def->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar &&
478 !strncmp(p_sys->psz_component, "OMX.qcom.video.decoder",
479 strlen("OMX.qcom.video.decoder")))
480 def->format.video.eColorFormat = OMX_QCOM_COLOR_FormatYVU420SemiPlanar;
482 /* Hack: Galaxy S II (stock firmware) gives a slice height larger
483 * than the video height, but this doesn't imply padding between
484 * the video planes. Nexus S also has a slice height larger than
485 * the video height, but there it actually is real padding, thus
486 * Galaxy S II is the buggy one. The Galaxy S II decoder is
487 * named OMX.SEC.avcdec while the one on Nexus S is
488 * OMX.SEC.AVC.Decoder. Thus do this for any OMX.SEC. that don't
489 * contain the string ".Decoder". */
490 if(!strncmp(p_sys->psz_component, "OMX.SEC.", strlen("OMX.SEC.")) &&
491 !strstr(p_sys->psz_component, ".Decoder"))
492 def->format.video.nSliceHeight = 0;
494 if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
495 &p_fmt->i_codec, 0 ) )
497 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
498 &p_fmt->i_codec, 0 ) )
500 omx_error = OMX_ErrorNotImplemented;
501 CHECK_ERROR(omx_error, "OMX color format %i not supported",
502 (int)def->format.video.eColorFormat );
504 GetVlcChromaSizes( p_fmt->i_codec,
505 def->format.video.nFrameWidth,
506 def->format.video.nFrameHeight,
507 &p_port->i_frame_size, &p_port->i_frame_stride,
508 &p_port->i_frame_stride_chroma_div );
510 if(p_port->i_frame_size > def->nBufferSize)
511 def->nBufferSize = p_port->i_frame_size;
512 p_port->i_frame_size = def->nBufferSize;
514 if((int)p_port->i_frame_stride > def->format.video.nStride)
515 def->format.video.nStride = p_port->i_frame_stride;
517 p_port->i_frame_stride = def->format.video.nStride;
521 if( !OmxToVlcAudioFormat( def->format.audio.eEncoding,
522 &p_fmt->i_codec, 0 ) )
524 omx_error = OMX_ErrorNotImplemented;
525 CHECK_ERROR(omx_error, "OMX audio format %i not supported",
526 (int)def->format.audio.eEncoding );
529 omx_error = GetAudioParameters(p_port->omx_handle,
530 &p_port->format_param, def->nPortIndex,
531 def->format.audio.eEncoding,
532 &p_fmt->audio.i_channels,
533 &p_fmt->audio.i_rate,
535 &p_fmt->audio.i_bitspersample,
536 &p_fmt->audio.i_blockalign);
537 CHECK_ERROR(omx_error, "GetAudioParameters failed (%x : %s)",
538 omx_error, ErrorToString(omx_error));
540 if(p_fmt->audio.i_channels < 9)
542 static const int pi_channels_maps[9] =
544 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
545 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
546 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
547 | AOUT_CHAN_REARRIGHT,
548 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
549 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
550 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
551 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
552 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
553 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
554 | AOUT_CHAN_MIDDLERIGHT,
555 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
556 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
559 p_fmt->audio.i_physical_channels =
560 p_fmt->audio.i_original_channels =
561 pi_channels_maps[p_fmt->audio.i_channels];
564 date_Init( &p_dec->p_sys->end_date, p_fmt->audio.i_rate, 1 );
568 default: return OMX_ErrorNotImplemented;
575 /*****************************************************************************
576 * DeinitialiseComponent: Deinitialise and unload an OMX component
577 *****************************************************************************/
578 static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
579 OMX_HANDLETYPE omx_handle)
581 decoder_sys_t *p_sys = p_dec->p_sys;
582 OMX_ERRORTYPE omx_error;
586 if(!omx_handle) return OMX_ErrorNone;
588 omx_error = OMX_GetState(omx_handle, &state);
589 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
591 if(state == OMX_StateExecuting)
593 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
595 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
596 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
597 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
600 omx_error = OMX_GetState(omx_handle, &state);
601 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
603 if(state == OMX_StateIdle)
605 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
606 OMX_StateLoaded, 0 );
607 CHECK_ERROR(omx_error, "OMX_CommandStateSet Loaded failed (%x)", omx_error );
609 for(i = 0; i < p_sys->ports; i++)
611 OmxPort *p_port = &p_sys->p_ports[i];
612 OMX_BUFFERHEADERTYPE *p_buffer;
614 for(j = 0; j < p_port->i_buffers; j++)
616 OMX_FIFO_GET(&p_port->fifo, p_buffer);
617 if (p_buffer == &p_sys->sentinel_buffer)
619 omx_error = OMX_FreeBuffer( omx_handle,
620 p_port->i_port_index, p_buffer );
622 if(omx_error != OMX_ErrorNone) break;
624 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
625 omx_error, (int)p_port->i_port_index, j );
628 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
629 CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
633 for(i = 0; i < p_sys->ports; i++)
635 OmxPort *p_port = &p_sys->p_ports[i];
636 free(p_port->pp_buffers);
637 p_port->pp_buffers = 0;
639 omx_error = pf_free_handle( omx_handle );
643 /*****************************************************************************
644 * InitialiseComponent: Load and initialise an OMX component
645 *****************************************************************************/
646 static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
647 OMX_STRING psz_component, OMX_HANDLETYPE *p_handle)
649 static OMX_CALLBACKTYPE callbacks =
650 { OmxEventHandler, OmxEmptyBufferDone, OmxFillBufferDone };
651 decoder_sys_t *p_sys = p_dec->p_sys;
652 OMX_HANDLETYPE omx_handle;
653 OMX_ERRORTYPE omx_error;
655 OMX_U8 psz_role[OMX_MAX_STRINGNAME_SIZE];
656 OMX_PARAM_COMPONENTROLETYPE role;
657 OMX_PARAM_PORTDEFINITIONTYPE definition;
658 OMX_PORT_PARAM_TYPE param;
661 omx_error = pf_get_handle( &omx_handle, psz_component, p_dec, &callbacks );
662 if(omx_error != OMX_ErrorNone)
664 msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
665 omx_error, ErrorToString(omx_error) );
668 strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
670 omx_error = OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
671 if(omx_error == OMX_ErrorNone)
672 msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
674 msg_Dbg(p_dec, "loaded component %s", psz_component);
675 PrintOmx(p_dec, omx_handle, OMX_ALL);
677 /* Set component role */
678 OMX_INIT_STRUCTURE(role);
679 strcpy((char*)role.cRole,
680 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec : p_dec->fmt_in.i_codec,
681 p_dec->fmt_in.i_cat, p_sys->b_enc));
683 omx_error = OMX_SetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
685 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
687 if(omx_error == OMX_ErrorNone)
688 msg_Dbg(p_dec, "component standard role set to %s", role.cRole);
690 /* Find the input / output ports */
691 OMX_INIT_STRUCTURE(param);
692 OMX_INIT_STRUCTURE(definition);
693 omx_error = OMX_GetParameter(omx_handle, p_dec->fmt_in.i_cat == VIDEO_ES ?
694 OMX_IndexParamVideoInit : OMX_IndexParamAudioInit, ¶m);
695 if(omx_error != OMX_ErrorNone) param.nPorts = 0;
697 for(i = 0; i < param.nPorts; i++)
701 /* Get port definition */
702 definition.nPortIndex = param.nStartPortNumber + i;
703 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
705 if(omx_error != OMX_ErrorNone) continue;
707 if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
708 else p_port = &p_sys->out;
710 p_port->b_valid = true;
711 p_port->i_port_index = definition.nPortIndex;
712 p_port->definition = definition;
713 p_port->omx_handle = omx_handle;
716 if(!p_sys->in.b_valid || !p_sys->out.b_valid)
718 omx_error = OMX_ErrorInvalidComponent;
719 CHECK_ERROR(omx_error, "couldn't find an input and output port");
722 if(!strncmp(p_sys->psz_component, "OMX.SEC.", 8))
725 omx_error = OMX_GetExtensionIndex(omx_handle, (OMX_STRING) "OMX.SEC.index.ThumbnailMode", &index);
726 if(omx_error == OMX_ErrorNone)
728 OMX_BOOL enable = OMX_TRUE;
729 omx_error = OMX_SetConfig(omx_handle, index, &enable);
730 CHECK_ERROR(omx_error, "Unable to set ThumbnailMode");
732 OMX_BOOL enable = OMX_TRUE;
733 /* Needed on Samsung Galaxy S II */
734 omx_error = OMX_SetConfig(omx_handle, OMX_IndexVendorSetYUV420pMode, &enable);
735 if (omx_error == OMX_ErrorNone)
736 msg_Dbg(p_dec, "Set OMX_IndexVendorSetYUV420pMode successfully");
738 msg_Dbg(p_dec, "Unable to set OMX_IndexVendorSetYUV420pMode: %x", omx_error);
742 /* Set port definitions */
743 for(i = 0; i < p_sys->ports; i++)
745 omx_error = SetPortDefinition(p_dec, &p_sys->p_ports[i],
746 p_sys->p_ports[i].p_fmt);
747 if(omx_error != OMX_ErrorNone) goto error;
750 /* Allocate our array for the omx buffers and enable ports */
751 for(i = 0; i < p_sys->ports; i++)
753 OmxPort *p_port = &p_sys->p_ports[i];
756 malloc(p_port->definition.nBufferCountActual *
757 sizeof(OMX_BUFFERHEADERTYPE*));
758 if(!p_port->pp_buffers)
760 omx_error = OMX_ErrorInsufficientResources;
761 CHECK_ERROR(omx_error, "memory allocation failed");
763 p_port->i_buffers = p_port->definition.nBufferCountActual;
766 if(!p_port->definition.bEnabled)
768 omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
769 p_port->i_port_index, NULL);
770 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
771 (int)p_port->i_port_index, omx_error );
772 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
773 CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
774 (int)p_port->i_port_index, omx_error );
778 *p_handle = omx_handle;
779 return OMX_ErrorNone;
782 DeinitialiseComponent(p_dec, omx_handle);
787 /*****************************************************************************
788 * OpenDecoder: Create the decoder instance
789 *****************************************************************************/
790 static int OpenDecoder( vlc_object_t *p_this )
792 decoder_t *p_dec = (decoder_t*)p_this;
795 if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
799 if( p_dec->fmt_in.i_cat != VIDEO_ES && !p_dec->b_force)
803 status = OpenGeneric( p_this, false );
804 if(status != VLC_SUCCESS) return status;
806 p_dec->pf_decode_video = DecodeVideo;
807 p_dec->pf_decode_audio = DecodeAudio;
812 /*****************************************************************************
813 * OpenEncoder: Create the encoder instance
814 *****************************************************************************/
815 static int OpenEncoder( vlc_object_t *p_this )
817 encoder_t *p_enc = (encoder_t*)p_this;
820 if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
823 status = OpenGeneric( p_this, true );
824 if(status != VLC_SUCCESS) return status;
826 p_enc->pf_encode_video = EncodeVideo;
831 /*****************************************************************************
832 * OpenGeneric: Create the generic decoder/encoder instance
833 *****************************************************************************/
834 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
836 decoder_t *p_dec = (decoder_t*)p_this;
837 decoder_sys_t *p_sys;
838 OMX_ERRORTYPE omx_error;
839 OMX_BUFFERHEADERTYPE *p_header;
842 vlc_mutex_lock( &omx_core_mutex );
843 if( omx_refcount > 0 )
846 /* Load the OMX core */
847 for( i = 0; ppsz_dll_list[i]; i++ )
849 dll_handle = dll_open( ppsz_dll_list[i] );
850 if( dll_handle ) break;
854 vlc_mutex_unlock( &omx_core_mutex );
858 pf_init = dlsym( dll_handle, "OMX_Init" );
859 pf_deinit = dlsym( dll_handle, "OMX_Deinit" );
860 pf_get_handle = dlsym( dll_handle, "OMX_GetHandle" );
861 pf_free_handle = dlsym( dll_handle, "OMX_FreeHandle" );
862 pf_component_enum = dlsym( dll_handle, "OMX_ComponentNameEnum" );
863 pf_get_roles_of_component = dlsym( dll_handle, "OMX_GetRolesOfComponent" );
864 if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle ||
865 !pf_component_enum || !pf_get_roles_of_component )
867 msg_Warn( p_this, "cannot find OMX_* symbols in `%s' (%s)",
868 ppsz_dll_list[i], dlerror() );
869 dll_close(dll_handle);
870 vlc_mutex_unlock( &omx_core_mutex );
875 /* Allocate the memory needed to store the decoder's structure */
876 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
878 if( omx_refcount == 0 )
879 dll_close(dll_handle);
880 vlc_mutex_unlock( &omx_core_mutex );
884 /* Initialise the thread properties */
887 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
888 p_dec->fmt_out.video = p_dec->fmt_in.video;
889 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
890 p_dec->fmt_out.i_codec = 0;
892 p_sys->b_enc = b_encode;
893 p_sys->pp_last_event = &p_sys->p_events;
894 vlc_mutex_init (&p_sys->mutex);
895 vlc_cond_init (&p_sys->cond);
896 vlc_mutex_init (&p_sys->lock);
897 vlc_mutex_init (&p_sys->in.fifo.lock);
898 vlc_cond_init (&p_sys->in.fifo.wait);
899 p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
900 p_sys->in.fifo.pp_last = &p_sys->in.fifo.p_first;
901 p_sys->in.b_direct = false;
902 p_sys->in.b_flushed = true;
903 p_sys->in.p_fmt = &p_dec->fmt_in;
904 vlc_mutex_init (&p_sys->out.fifo.lock);
905 vlc_cond_init (&p_sys->out.fifo.wait);
906 p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
907 p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
908 p_sys->out.b_direct = true;
909 p_sys->out.b_flushed = true;
910 p_sys->out.p_fmt = &p_dec->fmt_out;
912 p_sys->p_ports = &p_sys->in;
913 p_sys->b_use_pts = 0;
915 msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
916 (char *)&p_dec->fmt_out.i_codec);
918 /* Initialise the OMX core */
919 omx_error = omx_refcount > 0 ? OMX_ErrorNone : pf_init();
921 if(omx_error != OMX_ErrorNone)
923 msg_Warn( p_this, "OMX_Init failed (%x: %s)", omx_error,
924 ErrorToString(omx_error) );
925 vlc_mutex_unlock( &omx_core_mutex );
926 CloseGeneric(p_this);
929 p_sys->b_init = true;
930 vlc_mutex_unlock( &omx_core_mutex );
932 /* Enumerate components and build a list of the one we want to try */
933 if( !CreateComponentsList(p_dec,
934 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
935 p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
938 msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
939 (char *)&p_dec->fmt_in.i_codec );
940 CloseGeneric(p_this);
944 /* Try to load and initialise a component */
945 omx_error = OMX_ErrorUndefined;
946 for(i = 0; i < p_sys->components; i++)
949 /* ignore OpenCore software codecs */
950 if (!strncmp(p_sys->ppsz_components[i], "OMX.PV.", 7))
952 /* The same sw codecs, renamed in ICS (perhaps also in honeycomb) */
953 if (!strncmp(p_sys->ppsz_components[i], "OMX.google.", 11))
956 omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
958 if(omx_error == OMX_ErrorNone) break;
960 CHECK_ERROR(omx_error, "no component could be initialised" );
962 /* Move component to Idle then Executing state */
963 OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
964 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
966 /* Allocate omx buffers */
967 for(i = 0; i < p_sys->ports; i++)
969 OmxPort *p_port = &p_sys->p_ports[i];
971 for(j = 0; j < p_port->i_buffers; j++)
974 #define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
975 char *p_buf = malloc(p_port->definition.nBufferSize +
976 p_port->definition.nBufferAlignment);
977 p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
980 if(0 && p_port->b_direct)
982 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
983 p_port->i_port_index, 0,
984 p_port->definition.nBufferSize, (void*)1);
987 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
988 p_port->i_port_index, 0,
989 p_port->definition.nBufferSize);
991 if(omx_error != OMX_ErrorNone) break;
992 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
994 p_port->i_buffers = j;
995 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
996 omx_error, (int)p_port->i_port_index, j );
999 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1000 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
1002 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
1003 OMX_StateExecuting, 0);
1004 CHECK_ERROR(omx_error, "OMX_CommandStateSet Executing failed (%x)", omx_error );
1005 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1006 CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
1008 /* Send codec configuration data */
1009 if( p_dec->fmt_in.i_extra )
1011 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1012 p_header->nFilledLen = p_dec->fmt_in.i_extra;
1014 /* Convert H.264 NAL format to annex b */
1015 if( p_sys->i_nal_size_length && !p_sys->in.b_direct )
1017 p_header->nFilledLen = 0;
1018 convert_sps_pps( p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
1019 p_header->pBuffer, p_header->nAllocLen,
1020 (uint32_t*) &p_header->nFilledLen, NULL );
1022 else if(p_sys->in.b_direct)
1024 p_header->pOutputPortPrivate = p_header->pBuffer;
1025 p_header->pBuffer = p_dec->fmt_in.p_extra;
1029 if(p_header->nFilledLen > p_header->nAllocLen)
1031 msg_Dbg(p_dec, "buffer too small (%i,%i)", (int)p_header->nFilledLen,
1032 (int)p_header->nAllocLen);
1033 p_header->nFilledLen = p_header->nAllocLen;
1035 memcpy(p_header->pBuffer, p_dec->fmt_in.p_extra, p_header->nFilledLen);
1038 p_header->nOffset = 0;
1039 p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG;
1040 msg_Dbg(p_dec, "sending codec config data %p, %p, %i", p_header,
1041 p_header->pBuffer, (int)p_header->nFilledLen);
1042 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1045 /* Get back output port definition */
1046 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1047 if(omx_error != OMX_ErrorNone) goto error;
1049 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1050 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1052 if(p_sys->b_error) goto error;
1054 p_dec->b_need_packetized = true;
1055 if (!strcmp(p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER"))
1056 p_sys->b_use_pts = 1;
1060 CloseGeneric(p_this);
1061 return VLC_EGENERIC;
1064 /*****************************************************************************
1066 *****************************************************************************/
1067 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
1069 decoder_sys_t *p_sys = p_dec->p_sys;
1070 OMX_PARAM_PORTDEFINITIONTYPE definition;
1071 OMX_BUFFERHEADERTYPE *p_buffer;
1072 OMX_ERRORTYPE omx_error;
1075 /* Sanity checking */
1076 OMX_INIT_STRUCTURE(definition);
1077 definition.nPortIndex = p_port->i_port_index;
1078 omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1080 if(omx_error != OMX_ErrorNone || (p_dec->fmt_in.i_cat == VIDEO_ES &&
1081 (!definition.format.video.nFrameWidth ||
1082 !definition.format.video.nFrameHeight)) )
1083 return OMX_ErrorUndefined;
1085 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
1086 p_port->i_port_index, NULL);
1087 CHECK_ERROR(omx_error, "OMX_CommandPortDisable on %i failed (%x)",
1088 (int)p_port->i_port_index, omx_error );
1090 for(i = 0; i < p_port->i_buffers; i++)
1092 OMX_FIFO_GET(&p_port->fifo, p_buffer);
1093 if (p_buffer == &p_sys->sentinel_buffer)
1095 omx_error = OMX_FreeBuffer( p_sys->omx_handle,
1096 p_port->i_port_index, p_buffer );
1098 if(omx_error != OMX_ErrorNone) break;
1100 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
1101 omx_error, (int)p_port->i_port_index, i );
1103 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1104 CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
1106 /* Get the new port definition */
1107 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1108 if(omx_error != OMX_ErrorNone) goto error;
1110 if( p_dec->fmt_in.i_cat != AUDIO_ES )
1112 /* Don't explicitly set the new parameters that we got with
1113 * OMX_GetParameter above when using audio codecs.
1114 * That struct hasn't been changed since, so there should be
1115 * no need to set it here, unless some codec expects the
1116 * SetParameter call as a trigger event for some part of
1117 * the reconfiguration.
1118 * This fixes using audio decoders on Samsung Galaxy S II,
1120 * Only skipping this for audio codecs, to minimize the
1121 * change for current working configurations for video.
1123 omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1125 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
1126 omx_error, ErrorToString(omx_error));
1129 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
1130 p_port->i_port_index, NULL);
1131 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
1132 (int)p_port->i_port_index, omx_error );
1134 if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
1135 free(p_port->pp_buffers);
1136 p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
1137 if(!p_port->pp_buffers)
1139 omx_error = OMX_ErrorInsufficientResources;
1140 CHECK_ERROR(omx_error, "memory allocation failed");
1143 p_port->i_buffers = p_port->definition.nBufferCountActual;
1144 for(i = 0; i < p_port->i_buffers; i++)
1146 if(0 && p_port->b_direct)
1148 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1149 p_port->i_port_index, 0,
1150 p_port->definition.nBufferSize, (void*)1);
1153 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1154 p_port->i_port_index, 0,
1155 p_port->definition.nBufferSize);
1157 if(omx_error != OMX_ErrorNone) break;
1158 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1160 p_port->i_buffers = i;
1161 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1162 omx_error, (int)p_port->i_port_index, i );
1164 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1165 CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1167 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1168 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1174 /*****************************************************************************
1175 * DecodeVideo: Called to decode one frame
1176 *****************************************************************************/
1177 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1179 decoder_sys_t *p_sys = p_dec->p_sys;
1180 picture_t *p_pic = NULL, *p_next_pic;
1181 OMX_ERRORTYPE omx_error;
1184 OMX_BUFFERHEADERTYPE *p_header;
1187 if( !pp_block || !*pp_block )
1190 p_block = *pp_block;
1192 /* Check for errors from codec */
1195 msg_Dbg(p_dec, "error during decoding");
1196 block_Release( p_block );
1200 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1202 block_Release( p_block );
1203 if(!p_sys->in.b_flushed)
1205 msg_Dbg(p_dec, "flushing");
1206 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1207 p_sys->in.definition.nPortIndex, 0 );
1209 p_sys->in.b_flushed = true;
1213 /* Take care of decoded frames first */
1216 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1217 if(!p_header) break; /* No frame available */
1219 if(p_sys->out.b_update_def)
1221 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1222 p_sys->out.b_update_def = 0;
1225 if(p_header->nFilledLen)
1227 p_pic = p_header->pAppPrivate;
1230 /* We're not in direct rendering mode.
1231 * Get a new picture and copy the content */
1232 p_pic = decoder_NewPicture( p_dec );
1233 if( !p_pic ) break; /* No picture available */
1235 CopyOmxPicture(p_dec, p_pic, p_header, p_sys->out.definition.format.video.nSliceHeight);
1238 p_pic->date = p_header->nTimeStamp;
1239 p_header->nFilledLen = 0;
1240 p_header->pAppPrivate = 0;
1243 /* Get a new picture */
1244 if(p_sys->in.b_direct && !p_header->pAppPrivate)
1246 p_next_pic = decoder_NewPicture( p_dec );
1247 if(!p_next_pic) break;
1249 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1250 p_header->pAppPrivate = p_next_pic;
1251 p_header->pInputPortPrivate = p_header->pBuffer;
1252 p_header->pBuffer = p_next_pic->p[0].p_pixels;
1256 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1259 #ifdef OMXIL_EXTRA_DEBUG
1260 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1262 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1265 /* Send the input buffer to the component */
1266 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1268 if (p_header && p_header->nFlags & OMX_BUFFERFLAG_EOS)
1273 p_header->nFilledLen = p_block->i_buffer;
1274 p_header->nOffset = 0;
1275 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1276 if (p_sys->b_use_pts)
1277 p_header->nTimeStamp = p_block->i_pts;
1279 p_header->nTimeStamp = p_block->i_dts;
1281 /* In direct mode we pass the input pointer as is.
1282 * Otherwise we memcopy the data */
1283 if(p_sys->in.b_direct)
1285 p_header->pOutputPortPrivate = p_header->pBuffer;
1286 p_header->pBuffer = p_block->p_buffer;
1287 p_header->pAppPrivate = p_block;
1291 if(p_header->nFilledLen > p_header->nAllocLen)
1293 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1294 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1295 p_header->nFilledLen = p_header->nAllocLen;
1297 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1298 block_Release(p_block);
1301 /* Convert H.264 NAL format to annex b */
1302 if( p_sys->i_nal_size_length >= 3 && p_sys->i_nal_size_length <= 4 )
1304 /* This only works for NAL sizes 3-4 */
1305 int i_len = p_header->nFilledLen, i;
1306 uint8_t* ptr = p_header->pBuffer;
1307 while( i_len >= p_sys->i_nal_size_length )
1309 uint32_t nal_len = 0;
1310 for( i = 0; i < p_sys->i_nal_size_length; i++ ) {
1311 nal_len = (nal_len << 8) | ptr[i];
1314 ptr[p_sys->i_nal_size_length - 1] = 1;
1315 if( nal_len > INT_MAX || nal_len > (unsigned int) i_len )
1318 i_len -= nal_len + 4;
1321 #ifdef OMXIL_EXTRA_DEBUG
1322 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1323 (int)p_header->nFilledLen );
1325 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1326 p_sys->in.b_flushed = false;
1327 *pp_block = NULL; /* Avoid being fed the same packet again */
1331 /* Handle the PortSettingsChanged events */
1332 for(i = 0; i < p_sys->ports; i++)
1334 OmxPort *p_port = &p_sys->p_ports[i];
1335 if(p_port->b_reconfigure)
1337 omx_error = PortReconfigure(p_dec, p_port);
1338 p_port->b_reconfigure = 0;
1340 if(p_port->b_update_def)
1342 omx_error = GetPortDefinition(p_dec, p_port, p_port->p_fmt);
1343 p_port->b_update_def = 0;
1350 /*****************************************************************************
1351 * DecodeAudio: Called to decode one frame
1352 *****************************************************************************/
1353 aout_buffer_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1355 decoder_sys_t *p_sys = p_dec->p_sys;
1356 aout_buffer_t *p_buffer = 0;
1357 OMX_BUFFERHEADERTYPE *p_header;
1358 OMX_ERRORTYPE omx_error;
1362 if( !pp_block || !*pp_block ) return NULL;
1364 p_block = *pp_block;
1366 /* Check for errors from codec */
1369 msg_Dbg(p_dec, "error during decoding");
1370 block_Release( p_block );
1374 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1376 block_Release( p_block );
1377 date_Set( &p_sys->end_date, 0 );
1378 if(!p_sys->in.b_flushed)
1380 msg_Dbg(p_dec, "flushing");
1381 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1382 p_sys->in.definition.nPortIndex, 0 );
1384 p_sys->in.b_flushed = true;
1388 if( !date_Get( &p_sys->end_date ) )
1390 if( !p_block->i_pts )
1392 /* We've just started the stream, wait for the first PTS. */
1393 block_Release( p_block );
1396 date_Set( &p_sys->end_date, p_block->i_pts );
1399 /* Take care of decoded frames first */
1402 unsigned int i_samples;
1404 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1405 if(!p_header) break; /* No frame available */
1407 i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1410 p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1411 if( !p_buffer ) break; /* No audio buffer available */
1413 memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1414 p_header->nFilledLen = 0;
1416 if( p_header->nTimeStamp != 0 &&
1417 p_header->nTimeStamp != date_Get( &p_sys->end_date ) )
1418 date_Set( &p_sys->end_date, p_header->nTimeStamp );
1420 p_buffer->i_pts = date_Get( &p_sys->end_date );
1421 p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1425 #ifdef OMXIL_EXTRA_DEBUG
1426 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1428 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1429 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1433 /* Send the input buffer to the component */
1434 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1436 if (p_header && p_header->nFlags & OMX_BUFFERFLAG_EOS)
1441 p_header->nFilledLen = p_block->i_buffer;
1442 p_header->nOffset = 0;
1443 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1444 p_header->nTimeStamp = p_block->i_dts;
1446 /* In direct mode we pass the input pointer as is.
1447 * Otherwise we memcopy the data */
1448 if(p_sys->in.b_direct)
1450 p_header->pOutputPortPrivate = p_header->pBuffer;
1451 p_header->pBuffer = p_block->p_buffer;
1452 p_header->pAppPrivate = p_block;
1456 if(p_header->nFilledLen > p_header->nAllocLen)
1458 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1459 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1460 p_header->nFilledLen = p_header->nAllocLen;
1462 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1463 block_Release(p_block);
1466 #ifdef OMXIL_EXTRA_DEBUG
1467 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1468 (int)p_header->nFilledLen );
1470 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1471 p_sys->in.b_flushed = false;
1472 *pp_block = NULL; /* Avoid being fed the same packet again */
1476 /* Handle the PortSettingsChanged events */
1477 for(i = 0; i < p_sys->ports; i++)
1479 OmxPort *p_port = &p_sys->p_ports[i];
1480 if(!p_port->b_reconfigure) continue;
1481 p_port->b_reconfigure = 0;
1482 omx_error = PortReconfigure(p_dec, p_port);
1488 /*****************************************************************************
1489 * EncodeVideo: Called to encode one frame
1490 *****************************************************************************/
1491 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1493 decoder_t *p_dec = ( decoder_t *)p_enc;
1494 decoder_sys_t *p_sys = p_dec->p_sys;
1495 OMX_ERRORTYPE omx_error;
1498 OMX_BUFFERHEADERTYPE *p_header;
1499 block_t *p_block = 0;
1501 if( !p_pic ) return NULL;
1503 /* Check for errors from codec */
1506 msg_Dbg(p_dec, "error during encoding");
1510 /* Send the input buffer to the component */
1511 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1514 /* In direct mode we pass the input pointer as is.
1515 * Otherwise we memcopy the data */
1516 if(p_sys->in.b_direct)
1518 p_header->pOutputPortPrivate = p_header->pBuffer;
1519 p_header->pBuffer = p_pic->p[0].p_pixels;
1523 CopyVlcPicture(p_dec, p_header, p_pic);
1526 p_header->nFilledLen = p_sys->in.i_frame_size;
1527 p_header->nOffset = 0;
1528 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1529 p_header->nTimeStamp = p_pic->date;
1530 #ifdef OMXIL_EXTRA_DEBUG
1531 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1532 (int)p_header->nFilledLen );
1534 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1535 p_sys->in.b_flushed = false;
1538 /* Handle the PortSettingsChanged events */
1539 for(i = 0; i < p_sys->ports; i++)
1541 OmxPort *p_port = &p_sys->p_ports[i];
1542 if(!p_port->b_reconfigure) continue;
1543 p_port->b_reconfigure = 0;
1544 omx_error = PortReconfigure(p_dec, p_port);
1547 /* Wait for the decoded frame */
1550 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1552 if(p_header->nFilledLen)
1554 if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1556 /* TODO: need to store codec config */
1557 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1560 p_block = p_header->pAppPrivate;
1563 /* We're not in direct rendering mode.
1564 * Get a new block and copy the content */
1565 p_block = block_New( p_dec, p_header->nFilledLen );
1566 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1569 p_block->i_buffer = p_header->nFilledLen;
1570 p_block->i_pts = p_block->i_dts = p_header->nTimeStamp;
1571 p_header->nFilledLen = 0;
1572 p_header->pAppPrivate = 0;
1575 #ifdef OMXIL_EXTRA_DEBUG
1576 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1578 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1581 msg_Dbg(p_dec, "done");
1585 /*****************************************************************************
1586 * CloseGeneric: omxil decoder destruction
1587 *****************************************************************************/
1588 static void CloseGeneric( vlc_object_t *p_this )
1590 decoder_t *p_dec = (decoder_t *)p_this;
1591 decoder_sys_t *p_sys = p_dec->p_sys;
1593 if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1594 vlc_mutex_lock( &omx_core_mutex );
1596 if( omx_refcount == 0 )
1598 if( p_sys->b_init ) pf_deinit();
1599 dll_close( dll_handle );
1601 vlc_mutex_unlock( &omx_core_mutex );
1603 vlc_mutex_destroy (&p_sys->mutex);
1604 vlc_cond_destroy (&p_sys->cond);
1605 vlc_mutex_destroy (&p_sys->in.fifo.lock);
1606 vlc_cond_destroy (&p_sys->in.fifo.wait);
1607 vlc_mutex_destroy (&p_sys->out.fifo.lock);
1608 vlc_cond_destroy (&p_sys->out.fifo.wait);
1613 /*****************************************************************************
1615 *****************************************************************************/
1616 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1617 OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1618 OMX_U32 data_2, OMX_PTR event_data )
1620 decoder_t *p_dec = (decoder_t *)app_data;
1621 decoder_sys_t *p_sys = p_dec->p_sys;
1627 case OMX_EventCmdComplete:
1628 switch ((OMX_STATETYPE)data_1)
1630 case OMX_CommandStateSet:
1631 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %s)", EventToString(event),
1632 CommandToString(data_1), StateToString(data_2) );
1636 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u)", EventToString(event),
1637 CommandToString(data_1), (unsigned int)data_2 );
1642 case OMX_EventError:
1643 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u, %s)", EventToString(event),
1644 ErrorToString((OMX_ERRORTYPE)data_1), (unsigned int)data_2,
1645 (const char *)event_data);
1646 //p_sys->b_error = true;
1649 case OMX_EventPortSettingsChanged:
1650 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1651 (unsigned int)data_1, (unsigned int)data_2 );
1652 if( data_2 == 0 || data_2 == OMX_IndexParamPortDefinition )
1654 for(i = 0; i < p_sys->ports; i++)
1655 if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1656 p_sys->p_ports[i].b_reconfigure = true;
1657 memset(&p_sys->sentinel_buffer, 0, sizeof(p_sys->sentinel_buffer));
1658 p_sys->sentinel_buffer.nFlags = OMX_BUFFERFLAG_EOS;
1659 OMX_FIFO_PUT(&p_sys->in.fifo, &p_sys->sentinel_buffer);
1661 else if( data_2 == OMX_IndexConfigCommonOutputCrop )
1663 for(i = 0; i < p_sys->ports; i++)
1664 if(p_sys->p_ports[i].definition.nPortIndex == data_1)
1665 p_sys->p_ports[i].b_update_def = true;
1669 msg_Dbg( p_dec, "Unhandled setting change %x", (unsigned int)data_2 );
1674 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1675 (unsigned int)data_1, (unsigned int)data_2 );
1679 PostOmxEvent(p_dec, event, data_1, data_2, event_data);
1680 return OMX_ErrorNone;
1683 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1684 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1686 decoder_t *p_dec = (decoder_t *)app_data;
1687 decoder_sys_t *p_sys = p_dec->p_sys;
1690 #ifdef OMXIL_EXTRA_DEBUG
1691 msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1694 if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1696 block_t *p_block = (block_t *)omx_header->pAppPrivate;
1697 omx_header->pBuffer = omx_header->pOutputPortPrivate;
1698 if(p_block) block_Release(p_block);
1699 omx_header->pAppPrivate = 0;
1701 OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1703 return OMX_ErrorNone;
1706 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1707 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1709 decoder_t *p_dec = (decoder_t *)app_data;
1710 decoder_sys_t *p_sys = p_dec->p_sys;
1713 #ifdef OMXIL_EXTRA_DEBUG
1714 msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
1715 (int)omx_header->nFilledLen );
1718 if(omx_header->pInputPortPrivate)
1720 omx_header->pBuffer = omx_header->pInputPortPrivate;
1722 OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1724 return OMX_ErrorNone;