1 /*****************************************************************************
2 * omxil.c: Video decoder module making use of OpenMAX IL components.
3 *****************************************************************************
4 * Copyright (C) 2010 VLC authors and VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_codec.h>
36 #include <vlc_block_helper.h>
38 #include "../h264_nal.h"
41 #include "omxil_core.h"
42 #include "OMX_Broadcom.h"
45 # define OMXIL_EXTRA_DEBUG
48 #define SENTINEL_FLAG 0x10000
50 /* Defined in the broadcom version of OMX_Index.h */
51 #define OMX_IndexConfigRequestCallback 0x7f000063
52 #define OMX_IndexParamBrcmPixelAspectRatio 0x7f00004d
54 /* Defined in the broadcom version of OMX_Core.h */
55 #define OMX_EventParamOrConfigChanged 0x7F000001
57 /*****************************************************************************
59 *****************************************************************************/
60 static int OpenDecoder( vlc_object_t * );
61 static int OpenEncoder( vlc_object_t * );
62 static int OpenGeneric( vlc_object_t *, bool b_encode );
63 static void CloseGeneric( vlc_object_t * );
65 static picture_t *DecodeVideo( decoder_t *, block_t ** );
66 static block_t *DecodeAudio ( decoder_t *, block_t ** );
67 static block_t *EncodeVideo( encoder_t *, picture_t * );
69 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE, OMX_PTR, OMX_EVENTTYPE,
70 OMX_U32, OMX_U32, OMX_PTR );
71 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE, OMX_PTR,
72 OMX_BUFFERHEADERTYPE * );
73 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE, OMX_PTR,
74 OMX_BUFFERHEADERTYPE * );
76 /*****************************************************************************
78 *****************************************************************************/
80 set_description( N_("Audio/Video decoder (using OpenMAX IL)") )
81 set_category( CAT_INPUT )
82 set_subcategory( SUBCAT_INPUT_VCODEC )
83 set_section( N_("Decoding") , NULL )
85 /* For IOMX, don't enable it automatically via priorities,
86 * enable it only via the --codec iomx command line parameter when
88 set_capability( "decoder", 0 )
90 set_capability( "decoder", 80 )
92 set_callbacks( OpenDecoder, CloseGeneric )
95 set_section( N_("Encoding") , NULL )
96 set_description( N_("Video encoder (using OpenMAX IL)") )
97 set_capability( "encoder", 0 )
98 set_callbacks( OpenEncoder, CloseGeneric )
101 /*****************************************************************************
102 * ImplementationSpecificWorkarounds: place-holder for implementation
103 * specific workarounds
104 *****************************************************************************/
105 static OMX_ERRORTYPE ImplementationSpecificWorkarounds(decoder_t *p_dec,
106 OmxPort *p_port, es_format_t *p_fmt)
108 decoder_sys_t *p_sys = p_dec->p_sys;
109 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
110 int i_profile = 0xFFFF, i_level = 0xFFFF;
112 /* Try to find out the profile of the video */
113 while(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
114 p_fmt->i_codec == VLC_CODEC_H264)
116 uint8_t *p = (uint8_t*)p_dec->fmt_in.p_extra;
117 if(!p || !p_dec->fmt_in.p_extra) break;
119 /* Check the profile / level */
120 if(p_dec->fmt_in.i_original_fourcc == VLC_FOURCC('a','v','c','1') &&
123 if(p_dec->fmt_in.i_extra < 12) break;
124 p_sys->i_nal_size_length = 1 + (p[4]&0x03);
125 if( !(p[5]&0x1f) ) break;
130 if(p_dec->fmt_in.i_extra < 8) break;
131 if(!p[0] && !p[1] && !p[2] && p[3] == 1) p += 4;
132 else if(!p[0] && !p[1] && p[2] == 1) p += 3;
136 if( ((*p++)&0x1f) != 7) break;
138 /* Get profile/level out of first SPS */
144 if(!strcmp(p_sys->psz_component, "OMX.TI.Video.Decoder"))
146 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
147 p_fmt->i_codec == VLC_CODEC_H264 &&
148 (i_profile != 66 || i_level > 30))
150 msg_Dbg(p_dec, "h264 profile/level not supported (0x%x, 0x%x)",
152 return OMX_ErrorNotImplemented;
155 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirOutput &&
156 p_fmt->i_codec == VLC_CODEC_I420)
158 /* I420 xvideo is slow on OMAP */
159 def->format.video.eColorFormat = OMX_COLOR_FormatCbYCrY;
160 GetVlcChromaFormat( def->format.video.eColorFormat,
161 &p_fmt->i_codec, 0 );
162 GetVlcChromaSizes( p_fmt->i_codec,
163 def->format.video.nFrameWidth,
164 def->format.video.nFrameHeight,
165 &p_port->i_frame_size, &p_port->i_frame_stride,
166 &p_port->i_frame_stride_chroma_div );
167 def->format.video.nStride = p_port->i_frame_stride;
168 def->nBufferSize = p_port->i_frame_size;
171 else if(!strcmp(p_sys->psz_component, "OMX.st.video_encoder"))
173 if(p_fmt->i_cat == VIDEO_ES)
175 /* Bellagio's encoder doesn't encode the framerate in Q16 */
176 def->format.video.xFramerate >>= 16;
179 #if 0 /* FIXME: doesn't apply for HP Touchpad */
180 else if (!strncmp(p_sys->psz_component, "OMX.qcom.video.decoder.",
181 strlen("OMX.qcom.video.decoder")))
183 /* qdsp6 refuses buffer size larger than 450K on input port */
184 if (def->nBufferSize > 450 * 1024)
186 def->nBufferSize = 450 * 1024;
187 p_port->i_frame_size = def->nBufferSize;
192 else if (!strcmp(p_sys->psz_component, "OMX.broadcom.video_decode"))
194 /* Clear these fields before setting parameters, to allow the codec
195 * fill in what it wants (instead of rejecting whatever happened to
197 def->format.video.nStride = def->format.video.nSliceHeight = 0;
201 return OMX_ErrorNone;
204 /*****************************************************************************
205 * SetPortDefinition: set definition of the omx port based on the vlc format
206 *****************************************************************************/
207 static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
210 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
211 OMX_ERRORTYPE omx_error;
213 omx_error = OMX_GetParameter(p_port->omx_handle,
214 OMX_IndexParamPortDefinition, def);
215 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
216 omx_error, ErrorToString(omx_error));
221 def->format.video.nFrameWidth = p_fmt->video.i_width;
222 def->format.video.nFrameHeight = p_fmt->video.i_height;
223 if(def->format.video.eCompressionFormat == OMX_VIDEO_CodingUnused)
224 def->format.video.nStride = def->format.video.nFrameWidth;
225 if( p_fmt->video.i_frame_rate > 0 &&
226 p_fmt->video.i_frame_rate_base > 0 )
227 def->format.video.xFramerate = (p_fmt->video.i_frame_rate << 16) /
228 p_fmt->video.i_frame_rate_base;
230 if(def->eDir == OMX_DirInput || p_dec->p_sys->b_enc)
232 if (def->eDir == OMX_DirInput && p_dec->p_sys->b_enc)
233 def->nBufferSize = def->format.video.nFrameWidth *
234 def->format.video.nFrameHeight * 2;
235 p_port->i_frame_size = def->nBufferSize;
237 if(!GetOmxVideoFormat(p_fmt->i_codec,
238 &def->format.video.eCompressionFormat, 0) )
240 if(!GetOmxChromaFormat(p_fmt->i_codec,
241 &def->format.video.eColorFormat, 0) )
243 omx_error = OMX_ErrorNotImplemented;
244 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
245 (char *)&p_fmt->i_codec );
247 GetVlcChromaSizes( p_fmt->i_codec,
248 def->format.video.nFrameWidth,
249 def->format.video.nFrameHeight,
250 &p_port->i_frame_size, &p_port->i_frame_stride,
251 &p_port->i_frame_stride_chroma_div );
252 def->format.video.nStride = p_port->i_frame_stride;
253 def->nBufferSize = p_port->i_frame_size;
258 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
259 &p_fmt->i_codec, 0 ) )
261 omx_error = OMX_ErrorNotImplemented;
262 CHECK_ERROR(omx_error, "OMX color format %i not supported",
263 (int)def->format.video.eColorFormat );
265 GetVlcChromaSizes( p_fmt->i_codec,
266 def->format.video.nFrameWidth,
267 def->format.video.nFrameHeight,
268 &p_port->i_frame_size, &p_port->i_frame_stride,
269 &p_port->i_frame_stride_chroma_div );
270 def->format.video.nStride = p_port->i_frame_stride;
271 if (p_port->i_frame_size > def->nBufferSize)
272 def->nBufferSize = p_port->i_frame_size;
277 p_port->i_frame_size = def->nBufferSize;
278 if(def->eDir == OMX_DirInput)
280 if(!GetOmxAudioFormat(p_fmt->i_codec,
281 &def->format.audio.eEncoding, 0) )
283 omx_error = OMX_ErrorNotImplemented;
284 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
285 (char *)&p_fmt->i_codec );
290 if( !OmxToVlcAudioFormat(def->format.audio.eEncoding,
291 &p_fmt->i_codec, 0 ) )
293 omx_error = OMX_ErrorNotImplemented;
294 CHECK_ERROR(omx_error, "OMX audio encoding %i not supported",
295 (int)def->format.audio.eEncoding );
300 default: return OMX_ErrorNotImplemented;
303 omx_error = ImplementationSpecificWorkarounds(p_dec, p_port, p_fmt);
304 CHECK_ERROR(omx_error, "ImplementationSpecificWorkarounds failed (%x : %s)",
305 omx_error, ErrorToString(omx_error));
307 omx_error = OMX_SetParameter(p_port->omx_handle,
308 OMX_IndexParamPortDefinition, def);
309 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
310 omx_error, ErrorToString(omx_error));
312 omx_error = OMX_GetParameter(p_port->omx_handle,
313 OMX_IndexParamPortDefinition, def);
314 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
315 omx_error, ErrorToString(omx_error));
317 if(p_port->i_frame_size > def->nBufferSize)
318 def->nBufferSize = p_port->i_frame_size;
319 p_port->i_frame_size = def->nBufferSize;
321 /* Deal with audio params */
322 if(p_fmt->i_cat == AUDIO_ES)
324 omx_error = SetAudioParameters(p_port->omx_handle,
325 &p_port->format_param, def->nPortIndex,
326 def->format.audio.eEncoding,
328 p_fmt->audio.i_channels,
331 p_fmt->audio.i_bitspersample,
332 p_fmt->audio.i_blockalign);
333 if (def->eDir == OMX_DirInput) {
334 CHECK_ERROR(omx_error, "SetAudioParameters failed (%x : %s)",
335 omx_error, ErrorToString(omx_error));
336 } else if (omx_error != OMX_ErrorNone) {
337 msg_Warn(p_dec, "SetAudioParameters failed (%x : %s) on output port",
338 omx_error, ErrorToString(omx_error));
339 omx_error = OMX_ErrorNone;
342 if (!strcmp(p_dec->p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER") &&
343 def->eDir == OMX_DirOutput)
345 /* When setting the output buffer size above, the decoder actually
346 * sets the buffer size to a lower value than what was chosen. If
347 * we try to allocate buffers of this size, it fails. Thus, forcibly
348 * use a larger buffer size. */
349 def->nBufferSize *= 2;
357 /*****************************************************************************
358 * UpdatePixelAspect: Update vlc pixel aspect based on the aspect reported on
359 * the omx port - NOTE: Broadcom specific
360 *****************************************************************************/
361 static OMX_ERRORTYPE UpdatePixelAspect(decoder_t *p_dec)
363 decoder_sys_t *p_sys = p_dec->p_sys;
364 OMX_CONFIG_POINTTYPE pixel_aspect;
365 OMX_INIT_STRUCTURE(pixel_aspect);
366 OMX_ERRORTYPE omx_err;
368 if (strncmp(p_sys->psz_component, "OMX.broadcom.", 13))
369 return OMX_ErrorNotImplemented;
371 pixel_aspect.nPortIndex = p_sys->out.i_port_index;
372 omx_err = OMX_GetParameter(p_sys->omx_handle,
373 OMX_IndexParamBrcmPixelAspectRatio, &pixel_aspect);
374 if (omx_err != OMX_ErrorNone) {
375 msg_Warn(p_dec, "Failed to retrieve aspect ratio");
377 p_dec->fmt_out.video.i_sar_num = pixel_aspect.nX;
378 p_dec->fmt_out.video.i_sar_den = pixel_aspect.nY;
384 /*****************************************************************************
385 * GetPortDefinition: set vlc format based on the definition of the omx port
386 *****************************************************************************/
387 static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
390 decoder_sys_t *p_sys = p_dec->p_sys;
391 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
392 OMX_ERRORTYPE omx_error;
393 OMX_CONFIG_RECTTYPE crop_rect;
395 omx_error = OMX_GetParameter(p_port->omx_handle,
396 OMX_IndexParamPortDefinition, def);
397 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
398 omx_error, ErrorToString(omx_error));
403 p_fmt->video.i_width = def->format.video.nFrameWidth;
404 p_fmt->video.i_visible_width = def->format.video.nFrameWidth;
405 p_fmt->video.i_height = def->format.video.nFrameHeight;
406 p_fmt->video.i_visible_height = def->format.video.nFrameHeight;
407 p_fmt->video.i_frame_rate = p_dec->fmt_in.video.i_frame_rate;
408 p_fmt->video.i_frame_rate_base = p_dec->fmt_in.video.i_frame_rate_base;
410 OMX_INIT_STRUCTURE(crop_rect);
411 crop_rect.nPortIndex = def->nPortIndex;
412 omx_error = OMX_GetConfig(p_port->omx_handle, OMX_IndexConfigCommonOutputCrop, &crop_rect);
413 if (omx_error == OMX_ErrorNone)
415 if (!def->format.video.nSliceHeight)
416 def->format.video.nSliceHeight = def->format.video.nFrameHeight;
417 if (!def->format.video.nStride)
418 def->format.video.nStride = def->format.video.nFrameWidth;
419 p_fmt->video.i_width = crop_rect.nWidth;
420 p_fmt->video.i_visible_width = crop_rect.nWidth;
421 p_fmt->video.i_height = crop_rect.nHeight;
422 p_fmt->video.i_visible_height = crop_rect.nHeight;
423 if (def->format.video.eColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
424 def->format.video.nSliceHeight -= crop_rect.nTop/2;
428 /* Don't pass the error back to the caller, this isn't mandatory */
429 omx_error = OMX_ErrorNone;
432 /* Hack: Nexus One (stock firmware with binary OMX driver blob)
433 * claims to output 420Planar even though it in in practice is
435 if(def->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar &&
436 !strncmp(p_sys->psz_component, "OMX.qcom.video.decoder",
437 strlen("OMX.qcom.video.decoder")))
438 def->format.video.eColorFormat = OMX_QCOM_COLOR_FormatYVU420SemiPlanar;
440 if (IgnoreOmxDecoderPadding(p_sys->psz_component)) {
441 def->format.video.nSliceHeight = 0;
442 def->format.video.nStride = p_fmt->video.i_width;
445 if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
446 &p_fmt->i_codec, 0 ) )
448 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
449 &p_fmt->i_codec, 0 ) )
451 omx_error = OMX_ErrorNotImplemented;
452 CHECK_ERROR(omx_error, "OMX color format %i not supported",
453 (int)def->format.video.eColorFormat );
455 GetVlcChromaSizes( p_fmt->i_codec,
456 def->format.video.nFrameWidth,
457 def->format.video.nFrameHeight,
458 &p_port->i_frame_size, &p_port->i_frame_stride,
459 &p_port->i_frame_stride_chroma_div );
461 if(p_port->i_frame_size > def->nBufferSize)
462 def->nBufferSize = p_port->i_frame_size;
463 p_port->i_frame_size = def->nBufferSize;
465 if((int)p_port->i_frame_stride > def->format.video.nStride)
466 def->format.video.nStride = p_port->i_frame_stride;
468 p_port->i_frame_stride = def->format.video.nStride;
469 UpdatePixelAspect(p_dec);
473 if( !OmxToVlcAudioFormat( def->format.audio.eEncoding,
474 &p_fmt->i_codec, 0 ) )
476 omx_error = OMX_ErrorNotImplemented;
477 CHECK_ERROR(omx_error, "OMX audio format %i not supported",
478 (int)def->format.audio.eEncoding );
481 omx_error = GetAudioParameters(p_port->omx_handle,
482 &p_port->format_param, def->nPortIndex,
483 def->format.audio.eEncoding,
484 &p_fmt->audio.i_channels,
485 &p_fmt->audio.i_rate,
487 &p_fmt->audio.i_bitspersample,
488 &p_fmt->audio.i_blockalign);
489 CHECK_ERROR(omx_error, "GetAudioParameters failed (%x : %s)",
490 omx_error, ErrorToString(omx_error));
492 if(p_fmt->audio.i_channels < 9)
494 static const int pi_channels_maps[9] =
496 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
497 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
498 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
499 | AOUT_CHAN_REARRIGHT,
500 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
501 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
502 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
503 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
504 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
505 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
506 | AOUT_CHAN_MIDDLERIGHT,
507 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
508 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
511 p_fmt->audio.i_physical_channels =
512 p_fmt->audio.i_original_channels =
513 pi_channels_maps[p_fmt->audio.i_channels];
516 date_Init( &p_dec->p_sys->end_date, p_fmt->audio.i_rate, 1 );
520 default: return OMX_ErrorNotImplemented;
527 /*****************************************************************************
528 * DeinitialiseComponent: Deinitialise and unload an OMX component
529 *****************************************************************************/
530 static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
531 OMX_HANDLETYPE omx_handle)
533 decoder_sys_t *p_sys = p_dec->p_sys;
534 OMX_ERRORTYPE omx_error;
538 if(!omx_handle) return OMX_ErrorNone;
540 omx_error = OMX_GetState(omx_handle, &state);
541 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
543 if(state == OMX_StateExecuting)
545 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
547 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
550 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, &cmd, &state, 0);
551 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
552 // The event queue can contain other OMX_EventCmdComplete items,
553 // such as for OMX_CommandFlush
554 if (cmd == OMX_CommandStateSet && state == OMX_StateIdle)
559 omx_error = OMX_GetState(omx_handle, &state);
560 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
562 if(state == OMX_StateIdle)
564 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
565 OMX_StateLoaded, 0 );
566 CHECK_ERROR(omx_error, "OMX_CommandStateSet Loaded failed (%x)", omx_error );
568 for(i = 0; i < p_sys->ports; i++)
570 OmxPort *p_port = &p_sys->p_ports[i];
571 OMX_BUFFERHEADERTYPE *p_buffer;
573 for(j = 0; j < p_port->i_buffers; j++)
575 OMX_FIFO_GET(&p_port->fifo, p_buffer);
576 if (p_buffer->nFlags & SENTINEL_FLAG) {
581 omx_error = OMX_FreeBuffer( omx_handle,
582 p_port->i_port_index, p_buffer );
584 if(omx_error != OMX_ErrorNone) break;
586 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
587 omx_error, (int)p_port->i_port_index, j );
589 OMX_FIFO_PEEK(&p_port->fifo, p_buffer);
590 if (!p_buffer) break;
592 OMX_FIFO_GET(&p_port->fifo, p_buffer);
593 if (p_buffer->nFlags & SENTINEL_FLAG) {
597 msg_Warn( p_dec, "Stray buffer left in fifo, %p", p_buffer );
601 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, 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) {
671 param.nStartPortNumber = 0;
677 for(i = 0; i < param.nPorts; i++)
681 /* Get port definition */
682 definition.nPortIndex = param.nStartPortNumber + i;
683 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
685 if(omx_error != OMX_ErrorNone) continue;
687 if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
688 else p_port = &p_sys->out;
690 p_port->b_valid = true;
691 p_port->i_port_index = definition.nPortIndex;
692 p_port->definition = definition;
693 p_port->omx_handle = omx_handle;
696 if(!p_sys->in.b_valid || !p_sys->out.b_valid)
698 omx_error = OMX_ErrorInvalidComponent;
699 CHECK_ERROR(omx_error, "couldn't find an input and output port");
702 if(!strncmp(p_sys->psz_component, "OMX.SEC.", 8) &&
703 p_dec->fmt_in.i_cat == VIDEO_ES)
706 omx_error = OMX_GetExtensionIndex(omx_handle, (OMX_STRING) "OMX.SEC.index.ThumbnailMode", &index);
707 if(omx_error == OMX_ErrorNone)
709 OMX_BOOL enable = OMX_TRUE;
710 omx_error = OMX_SetConfig(omx_handle, index, &enable);
711 CHECK_ERROR(omx_error, "Unable to set ThumbnailMode");
713 OMX_BOOL enable = OMX_TRUE;
714 /* Needed on Samsung Galaxy S II */
715 omx_error = OMX_SetConfig(omx_handle, OMX_IndexVendorSetYUV420pMode, &enable);
716 if (omx_error == OMX_ErrorNone)
717 msg_Dbg(p_dec, "Set OMX_IndexVendorSetYUV420pMode successfully");
719 msg_Dbg(p_dec, "Unable to set OMX_IndexVendorSetYUV420pMode: %x", omx_error);
723 if(!strncmp(p_sys->psz_component, "OMX.broadcom.", 13))
725 OMX_CONFIG_REQUESTCALLBACKTYPE notifications;
726 OMX_INIT_STRUCTURE(notifications);
728 notifications.nPortIndex = p_sys->out.i_port_index;
729 notifications.nIndex = OMX_IndexParamBrcmPixelAspectRatio;
730 notifications.bEnable = OMX_TRUE;
732 omx_error = OMX_SetParameter(omx_handle,
733 OMX_IndexConfigRequestCallback, ¬ifications);
734 if (omx_error == OMX_ErrorNone)
735 msg_Dbg(p_dec, "Enabled aspect ratio notifications");
737 msg_Dbg(p_dec, "Could not enable aspect ratio notifications");
740 /* Set port definitions */
741 for(i = 0; i < p_sys->ports; i++)
743 omx_error = SetPortDefinition(p_dec, &p_sys->p_ports[i],
744 p_sys->p_ports[i].p_fmt);
745 if(omx_error != OMX_ErrorNone) goto error;
748 /* Allocate our array for the omx buffers and enable ports */
749 for(i = 0; i < p_sys->ports; i++)
751 OmxPort *p_port = &p_sys->p_ports[i];
754 malloc(p_port->definition.nBufferCountActual *
755 sizeof(OMX_BUFFERHEADERTYPE*));
756 if(!p_port->pp_buffers)
758 omx_error = OMX_ErrorInsufficientResources;
759 CHECK_ERROR(omx_error, "memory allocation failed");
761 p_port->i_buffers = p_port->definition.nBufferCountActual;
764 if(!p_port->definition.bEnabled)
766 omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
767 p_port->i_port_index, NULL);
768 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
769 (int)p_port->i_port_index, omx_error );
770 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
771 CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
772 (int)p_port->i_port_index, omx_error );
776 *p_handle = omx_handle;
777 return OMX_ErrorNone;
780 DeinitialiseComponent(p_dec, omx_handle);
785 /*****************************************************************************
786 * OpenDecoder: Create the decoder instance
787 *****************************************************************************/
788 static int OpenDecoder( vlc_object_t *p_this )
790 decoder_t *p_dec = (decoder_t*)p_this;
793 if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
796 status = OpenGeneric( p_this, false );
797 if(status != VLC_SUCCESS) return status;
799 p_dec->pf_decode_video = DecodeVideo;
800 p_dec->pf_decode_audio = DecodeAudio;
805 /*****************************************************************************
806 * OpenEncoder: Create the encoder instance
807 *****************************************************************************/
808 static int OpenEncoder( vlc_object_t *p_this )
810 encoder_t *p_enc = (encoder_t*)p_this;
813 if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
816 status = OpenGeneric( p_this, true );
817 if(status != VLC_SUCCESS) return status;
819 p_enc->pf_encode_video = EncodeVideo;
824 /*****************************************************************************
825 * OpenGeneric: Create the generic decoder/encoder instance
826 *****************************************************************************/
827 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
829 decoder_t *p_dec = (decoder_t*)p_this;
830 decoder_sys_t *p_sys;
831 OMX_ERRORTYPE omx_error;
832 OMX_BUFFERHEADERTYPE *p_header;
835 if (InitOmxCore(p_this) != VLC_SUCCESS) {
839 /* Allocate the memory needed to store the decoder's structure */
840 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
846 /* Initialise the thread properties */
849 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
850 p_dec->fmt_out.video = p_dec->fmt_in.video;
851 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
852 p_dec->fmt_out.i_codec = 0;
854 /* set default aspect of 1, if parser did not set it */
855 if (p_dec->fmt_out.video.i_sar_num == 0)
856 p_dec->fmt_out.video.i_sar_num = 1;
857 if (p_dec->fmt_out.video.i_sar_den == 0)
858 p_dec->fmt_out.video.i_sar_den = 1;
860 p_sys->b_enc = b_encode;
861 InitOmxEventQueue(&p_sys->event_queue);
862 vlc_mutex_init (&p_sys->in.fifo.lock);
863 vlc_cond_init (&p_sys->in.fifo.wait);
864 p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
865 p_sys->in.fifo.pp_last = &p_sys->in.fifo.p_first;
866 p_sys->in.b_direct = false;
867 p_sys->in.b_flushed = true;
868 p_sys->in.p_fmt = &p_dec->fmt_in;
869 vlc_mutex_init (&p_sys->out.fifo.lock);
870 vlc_cond_init (&p_sys->out.fifo.wait);
871 p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
872 p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
873 p_sys->out.b_direct = false;
874 p_sys->out.b_flushed = true;
875 p_sys->out.p_fmt = &p_dec->fmt_out;
877 p_sys->p_ports = &p_sys->in;
878 p_sys->b_use_pts = 1;
880 msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
881 (char *)&p_dec->fmt_out.i_codec);
883 /* Enumerate components and build a list of the one we want to try */
885 CreateComponentsList(p_this,
886 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
887 p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
888 p_sys->b_enc), p_sys->ppsz_components);
889 if( !p_sys->components )
891 msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
892 (char *)&p_dec->fmt_in.i_codec );
893 CloseGeneric(p_this);
897 /* Try to load and initialise a component */
898 omx_error = OMX_ErrorUndefined;
899 for(i = 0; i < p_sys->components; i++)
902 /* ignore OpenCore software codecs */
903 if (!strncmp(p_sys->ppsz_components[i], "OMX.PV.", 7))
905 /* The same sw codecs, renamed in ICS (perhaps also in honeycomb) */
906 if (!strncmp(p_sys->ppsz_components[i], "OMX.google.", 11))
908 /* This one has been seen on HTC One V - it behaves like it works,
909 * but FillBufferDone returns buffers filled with 0 bytes. The One V
910 * has got a working OMX.qcom.video.decoder.avc instead though. */
911 if (!strncmp(p_sys->ppsz_components[i], "OMX.ARICENT.", 12))
913 /* Codecs with DRM, that don't output plain YUV data but only
914 * support direct rendering where the output can't be intercepted. */
915 if (strstr(p_sys->ppsz_components[i], ".secure"))
917 /* Use VC1 decoder for WMV3 for now */
918 if (!strcmp(p_sys->ppsz_components[i], "OMX.SEC.WMV.Decoder"))
920 /* This decoder does work, but has an insane latency (leading to errors
921 * about "main audio output playback way too late" and dropped frames).
922 * At least Samsung Galaxy S III (where this decoder is present) has
923 * got another one, OMX.SEC.mp3.dec, that works well and has a
924 * sensible latency. (Also, even if that one isn't found, in general,
925 * using SW codecs is usually more than fast enough for MP3.) */
926 if (!strcmp(p_sys->ppsz_components[i], "OMX.SEC.MP3.Decoder"))
928 /* This codec should be able to handle both VC1 and WMV3, but
929 * for VC1 it doesn't output any buffers at all (in the way we use
930 * it) and for WMV3 it outputs plain black buffers. Thus ignore
931 * it until we can make it work properly. */
932 if (!strcmp(p_sys->ppsz_components[i], "OMX.Nvidia.vc1.decode"))
935 omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
937 if(omx_error == OMX_ErrorNone) break;
939 CHECK_ERROR(omx_error, "no component could be initialised" );
941 /* Move component to Idle then Executing state */
942 OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
943 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
945 /* Allocate omx buffers */
946 for(i = 0; i < p_sys->ports; i++)
948 OmxPort *p_port = &p_sys->p_ports[i];
950 for(j = 0; j < p_port->i_buffers; j++)
953 #define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
954 char *p_buf = malloc(p_port->definition.nBufferSize +
955 p_port->definition.nBufferAlignment);
956 p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
961 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
962 p_port->i_port_index, 0,
963 p_port->definition.nBufferSize, (void*)1);
966 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
967 p_port->i_port_index, 0,
968 p_port->definition.nBufferSize);
970 if(omx_error != OMX_ErrorNone) break;
971 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
973 p_port->i_buffers = j;
974 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
975 omx_error, (int)p_port->i_port_index, j );
978 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
979 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
981 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
982 OMX_StateExecuting, 0);
983 CHECK_ERROR(omx_error, "OMX_CommandStateSet Executing failed (%x)", omx_error );
984 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
985 CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
987 /* Send codec configuration data */
988 if( p_dec->fmt_in.i_extra )
990 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
991 p_header->nFilledLen = p_dec->fmt_in.i_extra;
993 /* Convert H.264 NAL format to annex b */
994 if( p_sys->i_nal_size_length && !p_sys->in.b_direct )
996 p_header->nFilledLen = 0;
997 convert_sps_pps( p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
998 p_header->pBuffer, p_header->nAllocLen,
999 (uint32_t*) &p_header->nFilledLen, NULL );
1001 else if(p_sys->in.b_direct)
1003 p_header->pOutputPortPrivate = p_header->pBuffer;
1004 p_header->pBuffer = p_dec->fmt_in.p_extra;
1006 else if (p_dec->fmt_in.i_codec == VLC_CODEC_WMV3 &&
1007 p_dec->fmt_in.i_extra >= 4 &&
1008 p_header->nAllocLen >= 36)
1011 // According to OMX IL 1.2.0 spec (4.3.33.2), the codec config
1012 // data for VC-1 Main/Simple (aka WMV3) is according to table 265
1013 // in the VC-1 spec. Most of the fields are just set with placeholders
1014 // (like framerate, hrd_buffer/rate).
1015 static const uint8_t wmv3seq[] = {
1016 0xff, 0xff, 0xff, 0xc5, // numframes=ffffff, marker byte
1017 0x04, 0x00, 0x00, 0x00, // marker byte
1018 0x00, 0x00, 0x00, 0x00, // struct C, almost equal to p_extra
1019 0x00, 0x00, 0x00, 0x00, // struct A, vert size
1020 0x00, 0x00, 0x00, 0x00, // struct A, horiz size
1021 0x0c, 0x00, 0x00, 0x00, // marker byte
1022 0xff, 0xff, 0x00, 0x80, // struct B, level=4, cbr=0, hrd_buffer=ffff
1023 0xff, 0xff, 0x00, 0x00, // struct B, hrd_rate=ffff
1024 0xff, 0xff, 0xff, 0xff, // struct B, framerate=ffffffff
1026 p_header->nFilledLen = sizeof(wmv3seq);
1027 memcpy(p_header->pBuffer, wmv3seq, p_header->nFilledLen);
1028 // Struct C - almost equal to the extradata
1029 memcpy(&p_header->pBuffer[8], p_dec->fmt_in.p_extra, 4);
1030 // Expand profile from the highest 2 bits to the highest 4 bits
1031 profile = p_header->pBuffer[8] >> 6;
1032 p_header->pBuffer[8] = (p_header->pBuffer[8] & 0x0f) | (profile << 4);
1033 // Fill in the height/width for struct A
1034 SetDWLE(&p_header->pBuffer[12], p_dec->fmt_in.video.i_height);
1035 SetDWLE(&p_header->pBuffer[16], p_dec->fmt_in.video.i_width);
1039 if(p_header->nFilledLen > p_header->nAllocLen)
1041 msg_Dbg(p_dec, "buffer too small (%i,%i)", (int)p_header->nFilledLen,
1042 (int)p_header->nAllocLen);
1043 p_header->nFilledLen = p_header->nAllocLen;
1045 memcpy(p_header->pBuffer, p_dec->fmt_in.p_extra, p_header->nFilledLen);
1048 p_header->nOffset = 0;
1049 p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG | OMX_BUFFERFLAG_ENDOFFRAME;
1050 msg_Dbg(p_dec, "sending codec config data %p, %p, %i", p_header,
1051 p_header->pBuffer, (int)p_header->nFilledLen);
1052 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1055 /* Get back output port definition */
1056 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1057 if(omx_error != OMX_ErrorNone) goto error;
1059 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1060 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1062 if(p_sys->b_error) goto error;
1064 p_dec->b_need_packetized = true;
1066 if (!p_sys->b_use_pts)
1067 msg_Dbg( p_dec, "using dts timestamp mode for %s", p_sys->psz_component);
1072 CloseGeneric(p_this);
1073 return VLC_EGENERIC;
1076 /*****************************************************************************
1078 *****************************************************************************/
1079 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
1081 decoder_sys_t *p_sys = p_dec->p_sys;
1082 OMX_PARAM_PORTDEFINITIONTYPE definition;
1083 OMX_BUFFERHEADERTYPE *p_buffer;
1084 OMX_ERRORTYPE omx_error;
1087 /* Sanity checking */
1088 OMX_INIT_STRUCTURE(definition);
1089 definition.nPortIndex = p_port->i_port_index;
1090 omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1092 if(omx_error != OMX_ErrorNone || (p_dec->fmt_in.i_cat == VIDEO_ES &&
1093 (!definition.format.video.nFrameWidth ||
1094 !definition.format.video.nFrameHeight)) )
1095 return OMX_ErrorUndefined;
1097 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
1098 p_port->i_port_index, NULL);
1099 CHECK_ERROR(omx_error, "OMX_CommandPortDisable on %i failed (%x)",
1100 (int)p_port->i_port_index, omx_error );
1102 for(i = 0; i < p_port->i_buffers; i++)
1104 OMX_FIFO_GET(&p_port->fifo, p_buffer);
1105 if (p_buffer->pAppPrivate != NULL)
1106 decoder_DeletePicture( p_dec, p_buffer->pAppPrivate );
1107 if (p_buffer->nFlags & SENTINEL_FLAG) {
1112 omx_error = OMX_FreeBuffer( p_sys->omx_handle,
1113 p_port->i_port_index, p_buffer );
1115 if(omx_error != OMX_ErrorNone) break;
1117 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
1118 omx_error, (int)p_port->i_port_index, i );
1120 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
1121 CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
1123 /* Get the new port definition */
1124 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1125 if(omx_error != OMX_ErrorNone) goto error;
1127 if( p_dec->fmt_in.i_cat != AUDIO_ES )
1129 /* Don't explicitly set the new parameters that we got with
1130 * OMX_GetParameter above when using audio codecs.
1131 * That struct hasn't been changed since, so there should be
1132 * no need to set it here, unless some codec expects the
1133 * SetParameter call as a trigger event for some part of
1134 * the reconfiguration.
1135 * This fixes using audio decoders on Samsung Galaxy S II,
1137 * Only skipping this for audio codecs, to minimize the
1138 * change for current working configurations for video.
1140 omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1142 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
1143 omx_error, ErrorToString(omx_error));
1146 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
1147 p_port->i_port_index, NULL);
1148 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
1149 (int)p_port->i_port_index, omx_error );
1151 if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
1152 free(p_port->pp_buffers);
1153 p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
1154 if(!p_port->pp_buffers)
1156 omx_error = OMX_ErrorInsufficientResources;
1157 CHECK_ERROR(omx_error, "memory allocation failed");
1160 p_port->i_buffers = p_port->definition.nBufferCountActual;
1161 for(i = 0; i < p_port->i_buffers; i++)
1163 if(p_port->b_direct)
1165 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1166 p_port->i_port_index, 0,
1167 p_port->definition.nBufferSize, (void*)1);
1170 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1171 p_port->i_port_index, 0,
1172 p_port->definition.nBufferSize);
1174 if(omx_error != OMX_ErrorNone) break;
1175 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1177 p_port->i_buffers = i;
1178 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1179 omx_error, (int)p_port->i_port_index, i );
1181 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
1182 CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1184 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1185 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1191 /*****************************************************************************
1192 * DecodeVideo: Called to decode one frame
1193 *****************************************************************************/
1194 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1196 decoder_sys_t *p_sys = p_dec->p_sys;
1197 picture_t *p_pic = NULL, *p_next_pic;
1198 OMX_ERRORTYPE omx_error;
1201 OMX_BUFFERHEADERTYPE *p_header;
1203 int i_input_used = 0;
1204 struct H264ConvertState convert_state = { 0, 0 };
1206 if( !pp_block || !*pp_block )
1209 p_block = *pp_block;
1211 /* Check for errors from codec */
1214 msg_Dbg(p_dec, "error during decoding");
1215 block_Release( p_block );
1219 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1221 block_Release( p_block );
1222 if(!p_sys->in.b_flushed)
1224 msg_Dbg(p_dec, "flushing");
1225 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1226 p_sys->in.definition.nPortIndex, 0 );
1228 p_sys->in.b_flushed = true;
1232 /* Take care of decoded frames first */
1235 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1236 if(!p_header) break; /* No frame available */
1238 if(p_sys->out.b_update_def)
1240 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1241 p_sys->out.b_update_def = 0;
1244 if(p_header->nFilledLen)
1246 p_pic = p_header->pAppPrivate;
1249 /* We're not in direct rendering mode.
1250 * Get a new picture and copy the content */
1251 p_pic = decoder_NewPicture( p_dec );
1254 CopyOmxPicture(p_sys->out.definition.format.video.eColorFormat,
1255 p_pic, p_sys->out.definition.format.video.nSliceHeight,
1256 p_sys->out.i_frame_stride,
1257 p_header->pBuffer + p_header->nOffset,
1258 p_sys->out.i_frame_stride_chroma_div);
1262 p_pic->date = FromOmxTicks(p_header->nTimeStamp);
1263 p_header->nFilledLen = 0;
1264 p_header->pAppPrivate = 0;
1267 /* Get a new picture */
1268 if(p_sys->out.b_direct && !p_header->pAppPrivate)
1270 p_next_pic = decoder_NewPicture( p_dec );
1271 if(!p_next_pic) break;
1273 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1274 p_header->pAppPrivate = p_next_pic;
1275 p_header->pInputPortPrivate = p_header->pBuffer;
1276 p_header->pBuffer = p_next_pic->p[0].p_pixels;
1280 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1283 #ifdef OMXIL_EXTRA_DEBUG
1284 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1286 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1290 /* Send the input buffer to the component */
1291 OMX_FIFO_GET_TIMEOUT(&p_sys->in.fifo, p_header, 200000);
1293 if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1300 bool decode_more = false;
1301 p_header->nFilledLen = p_block->i_buffer - i_input_used;
1302 p_header->nOffset = 0;
1303 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1304 if (p_sys->b_use_pts && p_block->i_pts)
1305 p_header->nTimeStamp = ToOmxTicks(p_block->i_pts);
1307 p_header->nTimeStamp = ToOmxTicks(p_block->i_dts);
1309 /* In direct mode we pass the input pointer as is.
1310 * Otherwise we memcopy the data */
1311 if(p_sys->in.b_direct)
1313 p_header->pOutputPortPrivate = p_header->pBuffer;
1314 p_header->pBuffer = p_block->p_buffer;
1315 p_header->pAppPrivate = p_block;
1316 i_input_used = p_header->nFilledLen;
1320 if(p_header->nFilledLen > p_header->nAllocLen)
1322 p_header->nFilledLen = p_header->nAllocLen;
1324 memcpy(p_header->pBuffer, p_block->p_buffer + i_input_used, p_header->nFilledLen);
1325 i_input_used += p_header->nFilledLen;
1326 if (i_input_used == p_block->i_buffer)
1328 block_Release(p_block);
1333 p_header->nFlags &= ~OMX_BUFFERFLAG_ENDOFFRAME;
1337 /* Convert H.264 NAL format to annex b. Doesn't do anything if
1338 * i_nal_size_length == 0, which is the case for codecs other
1340 convert_h264_to_annexb( p_header->pBuffer, p_header->nFilledLen,
1341 p_sys->i_nal_size_length, &convert_state );
1342 #ifdef OMXIL_EXTRA_DEBUG
1343 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i, %"PRId64, p_header, p_header->pBuffer,
1344 (int)p_header->nFilledLen, FromOmxTicks(p_header->nTimeStamp) );
1346 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1347 p_sys->in.b_flushed = false;
1351 *pp_block = NULL; /* Avoid being fed the same packet again */
1355 /* Handle the PortSettingsChanged events */
1356 for(i = 0; i < p_sys->ports; i++)
1358 OmxPort *p_port = &p_sys->p_ports[i];
1359 if(p_port->b_reconfigure)
1361 omx_error = PortReconfigure(p_dec, p_port);
1362 p_port->b_reconfigure = 0;
1364 if(p_port->b_update_def)
1366 omx_error = GetPortDefinition(p_dec, p_port, p_port->p_fmt);
1367 p_port->b_update_def = 0;
1374 /*****************************************************************************
1375 * DecodeAudio: Called to decode one frame
1376 *****************************************************************************/
1377 block_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1379 decoder_sys_t *p_sys = p_dec->p_sys;
1380 block_t *p_buffer = NULL;
1381 OMX_BUFFERHEADERTYPE *p_header;
1382 OMX_ERRORTYPE omx_error;
1386 if( !pp_block || !*pp_block ) return NULL;
1388 p_block = *pp_block;
1390 /* Check for errors from codec */
1393 msg_Dbg(p_dec, "error during decoding");
1394 block_Release( p_block );
1398 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1400 block_Release( p_block );
1401 date_Set( &p_sys->end_date, 0 );
1402 if(!p_sys->in.b_flushed)
1404 msg_Dbg(p_dec, "flushing");
1405 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1406 p_sys->in.definition.nPortIndex, 0 );
1408 p_sys->in.b_flushed = true;
1412 if( !date_Get( &p_sys->end_date ) )
1414 if( !p_block->i_pts )
1416 /* We've just started the stream, wait for the first PTS. */
1417 block_Release( p_block );
1420 date_Set( &p_sys->end_date, p_block->i_pts );
1423 /* Take care of decoded frames first */
1426 unsigned int i_samples = 0;
1428 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1429 if(!p_header) break; /* No frame available */
1431 if (p_sys->out.p_fmt->audio.i_channels)
1432 i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1435 p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1436 if( !p_buffer ) break; /* No audio buffer available */
1438 memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1439 p_header->nFilledLen = 0;
1441 int64_t timestamp = FromOmxTicks(p_header->nTimeStamp);
1442 if( timestamp != 0 &&
1443 timestamp != date_Get( &p_sys->end_date ) )
1444 date_Set( &p_sys->end_date, timestamp );
1446 p_buffer->i_pts = date_Get( &p_sys->end_date );
1447 p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1451 #ifdef OMXIL_EXTRA_DEBUG
1452 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1454 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1455 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1459 /* Send the input buffer to the component */
1460 OMX_FIFO_GET_TIMEOUT(&p_sys->in.fifo, p_header, 200000);
1462 if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1469 p_header->nFilledLen = p_block->i_buffer;
1470 p_header->nOffset = 0;
1471 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1472 p_header->nTimeStamp = ToOmxTicks(p_block->i_dts);
1474 /* In direct mode we pass the input pointer as is.
1475 * Otherwise we memcopy the data */
1476 if(p_sys->in.b_direct)
1478 p_header->pOutputPortPrivate = p_header->pBuffer;
1479 p_header->pBuffer = p_block->p_buffer;
1480 p_header->pAppPrivate = p_block;
1484 if(p_header->nFilledLen > p_header->nAllocLen)
1486 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1487 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1488 p_header->nFilledLen = p_header->nAllocLen;
1490 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1491 block_Release(p_block);
1494 #ifdef OMXIL_EXTRA_DEBUG
1495 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1496 (int)p_header->nFilledLen );
1498 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1499 p_sys->in.b_flushed = false;
1500 *pp_block = NULL; /* Avoid being fed the same packet again */
1504 /* Handle the PortSettingsChanged events */
1505 for(i = 0; i < p_sys->ports; i++)
1507 OmxPort *p_port = &p_sys->p_ports[i];
1508 if(!p_port->b_reconfigure) continue;
1509 p_port->b_reconfigure = 0;
1510 omx_error = PortReconfigure(p_dec, p_port);
1516 /*****************************************************************************
1517 * EncodeVideo: Called to encode one frame
1518 *****************************************************************************/
1519 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1521 decoder_t *p_dec = ( decoder_t *)p_enc;
1522 decoder_sys_t *p_sys = p_dec->p_sys;
1523 OMX_ERRORTYPE omx_error;
1526 OMX_BUFFERHEADERTYPE *p_header;
1527 block_t *p_block = 0;
1529 if( !p_pic ) return NULL;
1531 /* Check for errors from codec */
1534 msg_Dbg(p_dec, "error during encoding");
1538 /* Send the input buffer to the component */
1539 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1542 /* In direct mode we pass the input pointer as is.
1543 * Otherwise we memcopy the data */
1544 if(p_sys->in.b_direct)
1546 p_header->pOutputPortPrivate = p_header->pBuffer;
1547 p_header->pBuffer = p_pic->p[0].p_pixels;
1551 CopyVlcPicture(p_dec, p_header, p_pic);
1554 p_header->nFilledLen = p_sys->in.i_frame_size;
1555 p_header->nOffset = 0;
1556 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1557 p_header->nTimeStamp = ToOmxTicks(p_pic->date);
1558 #ifdef OMXIL_EXTRA_DEBUG
1559 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1560 (int)p_header->nFilledLen );
1562 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1563 p_sys->in.b_flushed = false;
1566 /* Handle the PortSettingsChanged events */
1567 for(i = 0; i < p_sys->ports; i++)
1569 OmxPort *p_port = &p_sys->p_ports[i];
1570 if(!p_port->b_reconfigure) continue;
1571 p_port->b_reconfigure = 0;
1572 omx_error = PortReconfigure(p_dec, p_port);
1575 /* Wait for the decoded frame */
1578 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1580 if(p_header->nFilledLen)
1582 if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1584 /* TODO: need to store codec config */
1585 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1588 p_block = p_header->pAppPrivate;
1591 /* We're not in direct rendering mode.
1592 * Get a new block and copy the content */
1593 p_block = block_Alloc( p_header->nFilledLen );
1594 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1597 p_block->i_buffer = p_header->nFilledLen;
1598 p_block->i_pts = p_block->i_dts = FromOmxTicks(p_header->nTimeStamp);
1599 p_header->nFilledLen = 0;
1600 p_header->pAppPrivate = 0;
1603 #ifdef OMXIL_EXTRA_DEBUG
1604 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1606 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1609 msg_Dbg(p_dec, "done");
1613 /*****************************************************************************
1614 * CloseGeneric: omxil decoder destruction
1615 *****************************************************************************/
1616 static void CloseGeneric( vlc_object_t *p_this )
1618 decoder_t *p_dec = (decoder_t *)p_this;
1619 decoder_sys_t *p_sys = p_dec->p_sys;
1621 if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1625 DeinitOmxEventQueue(&p_sys->event_queue);
1626 vlc_mutex_destroy (&p_sys->in.fifo.lock);
1627 vlc_cond_destroy (&p_sys->in.fifo.wait);
1628 vlc_mutex_destroy (&p_sys->out.fifo.lock);
1629 vlc_cond_destroy (&p_sys->out.fifo.wait);
1634 /*****************************************************************************
1636 *****************************************************************************/
1637 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1638 OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1639 OMX_U32 data_2, OMX_PTR event_data )
1641 decoder_t *p_dec = (decoder_t *)app_data;
1642 decoder_sys_t *p_sys = p_dec->p_sys;
1646 PrintOmxEvent((vlc_object_t *) p_dec, event, data_1, data_2, event_data);
1649 case OMX_EventError:
1650 //p_sys->b_error = true;
1653 case OMX_EventPortSettingsChanged:
1654 if( data_2 == 0 || data_2 == OMX_IndexParamPortDefinition ||
1655 data_2 == OMX_IndexParamAudioPcm )
1657 OMX_BUFFERHEADERTYPE *sentinel;
1658 for(i = 0; i < p_sys->ports; i++)
1659 if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1660 p_sys->p_ports[i].b_reconfigure = true;
1661 sentinel = calloc(1, sizeof(*sentinel));
1663 sentinel->nFlags = SENTINEL_FLAG;
1664 OMX_FIFO_PUT(&p_sys->in.fifo, sentinel);
1667 else if( data_2 == OMX_IndexConfigCommonOutputCrop )
1669 for(i = 0; i < p_sys->ports; i++)
1670 if(p_sys->p_ports[i].definition.nPortIndex == data_1)
1671 p_sys->p_ports[i].b_update_def = true;
1675 msg_Dbg( p_dec, "Unhandled setting change %x", (unsigned int)data_2 );
1678 case OMX_EventParamOrConfigChanged:
1679 UpdatePixelAspect(p_dec);
1686 PostOmxEvent(&p_sys->event_queue, event, data_1, data_2, event_data);
1687 return OMX_ErrorNone;
1690 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1691 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1693 decoder_t *p_dec = (decoder_t *)app_data;
1694 decoder_sys_t *p_sys = p_dec->p_sys;
1697 #ifdef OMXIL_EXTRA_DEBUG
1698 msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1701 if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1703 block_t *p_block = (block_t *)omx_header->pAppPrivate;
1704 omx_header->pBuffer = omx_header->pOutputPortPrivate;
1705 if(p_block) block_Release(p_block);
1706 omx_header->pAppPrivate = 0;
1708 OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1710 return OMX_ErrorNone;
1713 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1714 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1716 decoder_t *p_dec = (decoder_t *)app_data;
1717 decoder_sys_t *p_sys = p_dec->p_sys;
1720 #ifdef OMXIL_EXTRA_DEBUG
1721 msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i, %"PRId64, omx_header, omx_header->pBuffer,
1722 (int)omx_header->nFilledLen, FromOmxTicks(omx_header->nTimeStamp) );
1725 if(omx_header->pInputPortPrivate)
1727 omx_header->pBuffer = omx_header->pInputPortPrivate;
1729 OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1731 return OMX_ErrorNone;