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
53 #define OMX_IndexParamBrcmVideoDecodeErrorConcealment 0x7f000080
55 /* Defined in the broadcom version of OMX_Core.h */
56 #define OMX_EventParamOrConfigChanged 0x7F000001
58 /*****************************************************************************
60 *****************************************************************************/
61 static int OpenDecoder( vlc_object_t * );
62 static int OpenEncoder( vlc_object_t * );
63 static int OpenGeneric( vlc_object_t *, bool b_encode );
64 static void CloseGeneric( vlc_object_t * );
66 static picture_t *DecodeVideo( decoder_t *, block_t ** );
67 static block_t *DecodeAudio ( decoder_t *, block_t ** );
68 static block_t *EncodeVideo( encoder_t *, picture_t * );
70 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE, OMX_PTR, OMX_EVENTTYPE,
71 OMX_U32, OMX_U32, OMX_PTR );
72 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE, OMX_PTR,
73 OMX_BUFFERHEADERTYPE * );
74 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE, OMX_PTR,
75 OMX_BUFFERHEADERTYPE * );
77 /*****************************************************************************
79 *****************************************************************************/
81 set_description( N_("Audio/Video decoder (using OpenMAX IL)") )
82 set_category( CAT_INPUT )
83 set_subcategory( SUBCAT_INPUT_VCODEC )
84 set_section( N_("Decoding") , NULL )
86 /* For IOMX, don't enable it automatically via priorities,
87 * enable it only via the --codec iomx command line parameter when
89 set_capability( "decoder", 0 )
91 set_capability( "decoder", 80 )
93 set_callbacks( OpenDecoder, CloseGeneric )
96 set_section( N_("Encoding") , NULL )
97 set_description( N_("Video encoder (using OpenMAX IL)") )
98 set_capability( "encoder", 0 )
99 set_callbacks( OpenEncoder, CloseGeneric )
102 /*****************************************************************************
103 * ImplementationSpecificWorkarounds: place-holder for implementation
104 * specific workarounds
105 *****************************************************************************/
106 static OMX_ERRORTYPE ImplementationSpecificWorkarounds(decoder_t *p_dec,
107 OmxPort *p_port, es_format_t *p_fmt)
109 decoder_sys_t *p_sys = p_dec->p_sys;
110 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
111 size_t i_profile = 0xFFFF, i_level = 0xFFFF;
113 /* Try to find out the profile of the video */
114 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
115 p_fmt->i_codec == VLC_CODEC_H264)
116 h264_get_profile_level(&p_dec->fmt_in, &i_profile, &i_level, &p_sys->i_nal_size_length);
118 if(!strcmp(p_sys->psz_component, "OMX.TI.Video.Decoder"))
120 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
121 p_fmt->i_codec == VLC_CODEC_H264 &&
122 (i_profile != 66 || i_level > 30))
124 msg_Dbg(p_dec, "h264 profile/level not supported (0x%x, 0x%x)",
126 return OMX_ErrorNotImplemented;
129 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirOutput &&
130 p_fmt->i_codec == VLC_CODEC_I420)
132 /* I420 xvideo is slow on OMAP */
133 def->format.video.eColorFormat = OMX_COLOR_FormatCbYCrY;
134 GetVlcChromaFormat( def->format.video.eColorFormat,
135 &p_fmt->i_codec, 0 );
136 GetVlcChromaSizes( p_fmt->i_codec,
137 def->format.video.nFrameWidth,
138 def->format.video.nFrameHeight,
139 &p_port->i_frame_size, &p_port->i_frame_stride,
140 &p_port->i_frame_stride_chroma_div );
141 def->format.video.nStride = p_port->i_frame_stride;
142 def->nBufferSize = p_port->i_frame_size;
145 else if(!strcmp(p_sys->psz_component, "OMX.st.video_encoder"))
147 if(p_fmt->i_cat == VIDEO_ES)
149 /* Bellagio's encoder doesn't encode the framerate in Q16 */
150 def->format.video.xFramerate >>= 16;
153 #if 0 /* FIXME: doesn't apply for HP Touchpad */
154 else if (!strncmp(p_sys->psz_component, "OMX.qcom.video.decoder.",
155 strlen("OMX.qcom.video.decoder")))
157 /* qdsp6 refuses buffer size larger than 450K on input port */
158 if (def->nBufferSize > 450 * 1024)
160 def->nBufferSize = 450 * 1024;
161 p_port->i_frame_size = def->nBufferSize;
166 else if (!strcmp(p_sys->psz_component, "OMX.broadcom.video_decode"))
168 /* Clear these fields before setting parameters, to allow the codec
169 * fill in what it wants (instead of rejecting whatever happened to
171 def->format.video.nStride = def->format.video.nSliceHeight = 0;
175 return OMX_ErrorNone;
178 /*****************************************************************************
179 * SetPortDefinition: set definition of the omx port based on the vlc format
180 *****************************************************************************/
181 static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
184 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
185 OMX_ERRORTYPE omx_error;
187 omx_error = OMX_GetParameter(p_port->omx_handle,
188 OMX_IndexParamPortDefinition, def);
189 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
190 omx_error, ErrorToString(omx_error));
195 def->format.video.nFrameWidth = p_fmt->video.i_width;
196 def->format.video.nFrameHeight = p_fmt->video.i_height;
197 if(def->format.video.eCompressionFormat == OMX_VIDEO_CodingUnused)
198 def->format.video.nStride = def->format.video.nFrameWidth;
199 if( p_fmt->video.i_frame_rate > 0 &&
200 p_fmt->video.i_frame_rate_base > 0 )
201 def->format.video.xFramerate = (p_fmt->video.i_frame_rate << 16) /
202 p_fmt->video.i_frame_rate_base;
204 if(def->eDir == OMX_DirInput || p_dec->p_sys->b_enc)
206 if (def->eDir == OMX_DirInput && p_dec->p_sys->b_enc)
207 def->nBufferSize = def->format.video.nFrameWidth *
208 def->format.video.nFrameHeight * 2;
209 p_port->i_frame_size = def->nBufferSize;
211 if(!GetOmxVideoFormat(p_fmt->i_codec,
212 &def->format.video.eCompressionFormat, 0) )
214 if(!GetOmxChromaFormat(p_fmt->i_codec,
215 &def->format.video.eColorFormat, 0) )
217 omx_error = OMX_ErrorNotImplemented;
218 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
219 (char *)&p_fmt->i_codec );
221 GetVlcChromaSizes( p_fmt->i_codec,
222 def->format.video.nFrameWidth,
223 def->format.video.nFrameHeight,
224 &p_port->i_frame_size, &p_port->i_frame_stride,
225 &p_port->i_frame_stride_chroma_div );
226 def->format.video.nStride = p_port->i_frame_stride;
227 def->nBufferSize = p_port->i_frame_size;
232 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
233 &p_fmt->i_codec, 0 ) )
235 omx_error = OMX_ErrorNotImplemented;
236 CHECK_ERROR(omx_error, "OMX color format %i not supported",
237 (int)def->format.video.eColorFormat );
239 GetVlcChromaSizes( p_fmt->i_codec,
240 def->format.video.nFrameWidth,
241 def->format.video.nFrameHeight,
242 &p_port->i_frame_size, &p_port->i_frame_stride,
243 &p_port->i_frame_stride_chroma_div );
244 def->format.video.nStride = p_port->i_frame_stride;
245 if (p_port->i_frame_size > def->nBufferSize)
246 def->nBufferSize = p_port->i_frame_size;
251 p_port->i_frame_size = def->nBufferSize;
252 if(def->eDir == OMX_DirInput)
254 if(!GetOmxAudioFormat(p_fmt->i_codec,
255 &def->format.audio.eEncoding, 0) )
257 omx_error = OMX_ErrorNotImplemented;
258 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
259 (char *)&p_fmt->i_codec );
264 if( !OmxToVlcAudioFormat(def->format.audio.eEncoding,
265 &p_fmt->i_codec, 0 ) )
267 omx_error = OMX_ErrorNotImplemented;
268 CHECK_ERROR(omx_error, "OMX audio encoding %i not supported",
269 (int)def->format.audio.eEncoding );
274 default: return OMX_ErrorNotImplemented;
277 omx_error = ImplementationSpecificWorkarounds(p_dec, p_port, p_fmt);
278 CHECK_ERROR(omx_error, "ImplementationSpecificWorkarounds failed (%x : %s)",
279 omx_error, ErrorToString(omx_error));
281 omx_error = OMX_SetParameter(p_port->omx_handle,
282 OMX_IndexParamPortDefinition, def);
283 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
284 omx_error, ErrorToString(omx_error));
286 omx_error = OMX_GetParameter(p_port->omx_handle,
287 OMX_IndexParamPortDefinition, def);
288 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
289 omx_error, ErrorToString(omx_error));
291 if(p_port->i_frame_size > def->nBufferSize)
292 def->nBufferSize = p_port->i_frame_size;
293 p_port->i_frame_size = def->nBufferSize;
295 /* Deal with audio params */
296 if(p_fmt->i_cat == AUDIO_ES)
298 omx_error = SetAudioParameters(p_port->omx_handle,
299 &p_port->format_param, def->nPortIndex,
300 def->format.audio.eEncoding,
302 p_fmt->audio.i_channels,
305 p_fmt->audio.i_bitspersample,
306 p_fmt->audio.i_blockalign);
307 if (def->eDir == OMX_DirInput) {
308 CHECK_ERROR(omx_error, "SetAudioParameters failed (%x : %s)",
309 omx_error, ErrorToString(omx_error));
310 } else if (omx_error != OMX_ErrorNone) {
311 msg_Warn(p_dec, "SetAudioParameters failed (%x : %s) on output port",
312 omx_error, ErrorToString(omx_error));
313 omx_error = OMX_ErrorNone;
316 if (!strcmp(p_dec->p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER") &&
317 def->eDir == OMX_DirOutput)
319 /* When setting the output buffer size above, the decoder actually
320 * sets the buffer size to a lower value than what was chosen. If
321 * we try to allocate buffers of this size, it fails. Thus, forcibly
322 * use a larger buffer size. */
323 def->nBufferSize *= 2;
331 /*****************************************************************************
332 * UpdatePixelAspect: Update vlc pixel aspect based on the aspect reported on
333 * the omx port - NOTE: Broadcom specific
334 *****************************************************************************/
335 static OMX_ERRORTYPE UpdatePixelAspect(decoder_t *p_dec)
337 decoder_sys_t *p_sys = p_dec->p_sys;
338 OMX_CONFIG_POINTTYPE pixel_aspect;
339 OMX_INIT_STRUCTURE(pixel_aspect);
340 OMX_ERRORTYPE omx_err;
342 if (strncmp(p_sys->psz_component, "OMX.broadcom.", 13))
343 return OMX_ErrorNotImplemented;
345 pixel_aspect.nPortIndex = p_sys->out.i_port_index;
346 omx_err = OMX_GetParameter(p_sys->omx_handle,
347 OMX_IndexParamBrcmPixelAspectRatio, &pixel_aspect);
348 if (omx_err != OMX_ErrorNone) {
349 msg_Warn(p_dec, "Failed to retrieve aspect ratio");
351 p_dec->fmt_out.video.i_sar_num = pixel_aspect.nX;
352 p_dec->fmt_out.video.i_sar_den = pixel_aspect.nY;
358 /*****************************************************************************
359 * GetPortDefinition: set vlc format based on the definition of the omx port
360 *****************************************************************************/
361 static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
364 decoder_sys_t *p_sys = p_dec->p_sys;
365 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
366 OMX_ERRORTYPE omx_error;
367 OMX_CONFIG_RECTTYPE crop_rect;
369 omx_error = OMX_GetParameter(p_port->omx_handle,
370 OMX_IndexParamPortDefinition, def);
371 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
372 omx_error, ErrorToString(omx_error));
377 p_fmt->video.i_width = def->format.video.nFrameWidth;
378 p_fmt->video.i_visible_width = def->format.video.nFrameWidth;
379 p_fmt->video.i_height = def->format.video.nFrameHeight;
380 p_fmt->video.i_visible_height = def->format.video.nFrameHeight;
381 p_fmt->video.i_frame_rate = p_dec->fmt_in.video.i_frame_rate;
382 p_fmt->video.i_frame_rate_base = p_dec->fmt_in.video.i_frame_rate_base;
384 OMX_INIT_STRUCTURE(crop_rect);
385 crop_rect.nPortIndex = def->nPortIndex;
386 omx_error = OMX_GetConfig(p_port->omx_handle, OMX_IndexConfigCommonOutputCrop, &crop_rect);
387 if (omx_error == OMX_ErrorNone)
389 if (!def->format.video.nSliceHeight)
390 def->format.video.nSliceHeight = def->format.video.nFrameHeight;
391 if (!def->format.video.nStride)
392 def->format.video.nStride = def->format.video.nFrameWidth;
393 p_fmt->video.i_width = crop_rect.nWidth;
394 p_fmt->video.i_visible_width = crop_rect.nWidth;
395 p_fmt->video.i_height = crop_rect.nHeight;
396 p_fmt->video.i_visible_height = crop_rect.nHeight;
397 if (def->format.video.eColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
398 def->format.video.nSliceHeight -= crop_rect.nTop/2;
402 /* Don't pass the error back to the caller, this isn't mandatory */
403 omx_error = OMX_ErrorNone;
406 /* Hack: Nexus One (stock firmware with binary OMX driver blob)
407 * claims to output 420Planar even though it in in practice is
409 if(def->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar &&
410 !strncmp(p_sys->psz_component, "OMX.qcom.video.decoder",
411 strlen("OMX.qcom.video.decoder")))
412 def->format.video.eColorFormat = OMX_QCOM_COLOR_FormatYVU420SemiPlanar;
414 if (IgnoreOmxDecoderPadding(p_sys->psz_component)) {
415 def->format.video.nSliceHeight = 0;
416 def->format.video.nStride = p_fmt->video.i_width;
419 if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
420 &p_fmt->i_codec, 0 ) )
422 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
423 &p_fmt->i_codec, 0 ) )
425 omx_error = OMX_ErrorNotImplemented;
426 CHECK_ERROR(omx_error, "OMX color format %i not supported",
427 (int)def->format.video.eColorFormat );
429 GetVlcChromaSizes( p_fmt->i_codec,
430 def->format.video.nFrameWidth,
431 def->format.video.nFrameHeight,
432 &p_port->i_frame_size, &p_port->i_frame_stride,
433 &p_port->i_frame_stride_chroma_div );
435 if(p_port->i_frame_size > def->nBufferSize)
436 def->nBufferSize = p_port->i_frame_size;
437 p_port->i_frame_size = def->nBufferSize;
439 if((int)p_port->i_frame_stride > def->format.video.nStride)
440 def->format.video.nStride = p_port->i_frame_stride;
442 p_port->i_frame_stride = def->format.video.nStride;
443 UpdatePixelAspect(p_dec);
447 if( !OmxToVlcAudioFormat( def->format.audio.eEncoding,
448 &p_fmt->i_codec, 0 ) )
450 omx_error = OMX_ErrorNotImplemented;
451 CHECK_ERROR(omx_error, "OMX audio format %i not supported",
452 (int)def->format.audio.eEncoding );
455 omx_error = GetAudioParameters(p_port->omx_handle,
456 &p_port->format_param, def->nPortIndex,
457 def->format.audio.eEncoding,
458 &p_fmt->audio.i_channels,
459 &p_fmt->audio.i_rate,
461 &p_fmt->audio.i_bitspersample,
462 &p_fmt->audio.i_blockalign);
463 CHECK_ERROR(omx_error, "GetAudioParameters failed (%x : %s)",
464 omx_error, ErrorToString(omx_error));
466 if(p_fmt->audio.i_channels < 9)
468 static const int pi_channels_maps[9] =
470 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
471 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
472 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
473 | AOUT_CHAN_REARRIGHT,
474 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
475 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
476 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
477 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
478 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
479 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
480 | AOUT_CHAN_MIDDLERIGHT,
481 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
482 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
485 p_fmt->audio.i_physical_channels =
486 p_fmt->audio.i_original_channels =
487 pi_channels_maps[p_fmt->audio.i_channels];
490 date_Init( &p_dec->p_sys->end_date, p_fmt->audio.i_rate, 1 );
494 default: return OMX_ErrorNotImplemented;
501 /*****************************************************************************
502 * DeinitialiseComponent: Deinitialise and unload an OMX component
503 *****************************************************************************/
504 static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
505 OMX_HANDLETYPE omx_handle)
507 decoder_sys_t *p_sys = p_dec->p_sys;
508 OMX_ERRORTYPE omx_error;
512 if(!omx_handle) return OMX_ErrorNone;
514 omx_error = OMX_GetState(omx_handle, &state);
515 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
517 if(state == OMX_StateExecuting)
519 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
521 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
524 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, &cmd, &state, 0);
525 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
526 // The event queue can contain other OMX_EventCmdComplete items,
527 // such as for OMX_CommandFlush
528 if (cmd == OMX_CommandStateSet && state == OMX_StateIdle)
533 omx_error = OMX_GetState(omx_handle, &state);
534 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
536 if(state == OMX_StateIdle)
538 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
539 OMX_StateLoaded, 0 );
540 CHECK_ERROR(omx_error, "OMX_CommandStateSet Loaded failed (%x)", omx_error );
542 for(i = 0; i < p_sys->ports; i++)
544 OmxPort *p_port = &p_sys->p_ports[i];
545 OMX_BUFFERHEADERTYPE *p_buffer;
547 for(j = 0; j < p_port->i_buffers; j++)
549 OMX_FIFO_GET(&p_port->fifo, p_buffer);
550 if (p_buffer->nFlags & SENTINEL_FLAG) {
555 omx_error = OMX_FreeBuffer( omx_handle,
556 p_port->i_port_index, p_buffer );
558 if(omx_error != OMX_ErrorNone) break;
560 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
561 omx_error, (int)p_port->i_port_index, j );
563 OMX_FIFO_PEEK(&p_port->fifo, p_buffer);
564 if (!p_buffer) break;
566 OMX_FIFO_GET(&p_port->fifo, p_buffer);
567 if (p_buffer->nFlags & SENTINEL_FLAG) {
571 msg_Warn( p_dec, "Stray buffer left in fifo, %p", p_buffer );
575 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
576 CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
580 for(i = 0; i < p_sys->ports; i++)
582 OmxPort *p_port = &p_sys->p_ports[i];
583 free(p_port->pp_buffers);
584 p_port->pp_buffers = 0;
586 omx_error = pf_free_handle( omx_handle );
590 /*****************************************************************************
591 * InitialiseComponent: Load and initialise an OMX component
592 *****************************************************************************/
593 static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
594 OMX_STRING psz_component, OMX_HANDLETYPE *p_handle)
596 static OMX_CALLBACKTYPE callbacks =
597 { OmxEventHandler, OmxEmptyBufferDone, OmxFillBufferDone };
598 decoder_sys_t *p_sys = p_dec->p_sys;
599 OMX_HANDLETYPE omx_handle;
600 OMX_ERRORTYPE omx_error;
602 OMX_U8 psz_role[OMX_MAX_STRINGNAME_SIZE];
603 OMX_PARAM_COMPONENTROLETYPE role;
604 OMX_PARAM_PORTDEFINITIONTYPE definition;
605 OMX_PORT_PARAM_TYPE param;
608 omx_error = pf_get_handle( &omx_handle, psz_component, p_dec, &callbacks );
609 if(omx_error != OMX_ErrorNone)
611 msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
612 omx_error, ErrorToString(omx_error) );
615 strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
617 omx_error = OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
618 if(omx_error == OMX_ErrorNone)
619 msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
621 msg_Dbg(p_dec, "loaded component %s", psz_component);
622 PrintOmx(p_dec, omx_handle, OMX_ALL);
624 /* Set component role */
625 OMX_INIT_STRUCTURE(role);
626 strcpy((char*)role.cRole,
627 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec : p_dec->fmt_in.i_codec,
628 p_dec->fmt_in.i_cat, p_sys->b_enc));
630 omx_error = OMX_SetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
632 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
634 if(omx_error == OMX_ErrorNone)
635 msg_Dbg(p_dec, "component standard role set to %s", role.cRole);
637 /* Find the input / output ports */
638 OMX_INIT_STRUCTURE(param);
639 OMX_INIT_STRUCTURE(definition);
640 omx_error = OMX_GetParameter(omx_handle, p_dec->fmt_in.i_cat == VIDEO_ES ?
641 OMX_IndexParamVideoInit : OMX_IndexParamAudioInit, ¶m);
642 if(omx_error != OMX_ErrorNone) {
645 param.nStartPortNumber = 0;
651 for(i = 0; i < param.nPorts; i++)
655 /* Get port definition */
656 definition.nPortIndex = param.nStartPortNumber + i;
657 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
659 if(omx_error != OMX_ErrorNone) continue;
661 if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
662 else p_port = &p_sys->out;
664 p_port->b_valid = true;
665 p_port->i_port_index = definition.nPortIndex;
666 p_port->definition = definition;
667 p_port->omx_handle = omx_handle;
670 if(!p_sys->in.b_valid || !p_sys->out.b_valid)
672 omx_error = OMX_ErrorInvalidComponent;
673 CHECK_ERROR(omx_error, "couldn't find an input and output port");
676 if(!strncmp(p_sys->psz_component, "OMX.SEC.", 8) &&
677 p_dec->fmt_in.i_cat == VIDEO_ES)
680 omx_error = OMX_GetExtensionIndex(omx_handle, (OMX_STRING) "OMX.SEC.index.ThumbnailMode", &index);
681 if(omx_error == OMX_ErrorNone)
683 OMX_BOOL enable = OMX_TRUE;
684 omx_error = OMX_SetConfig(omx_handle, index, &enable);
685 CHECK_ERROR(omx_error, "Unable to set ThumbnailMode");
687 OMX_BOOL enable = OMX_TRUE;
688 /* Needed on Samsung Galaxy S II */
689 omx_error = OMX_SetConfig(omx_handle, OMX_IndexVendorSetYUV420pMode, &enable);
690 if (omx_error == OMX_ErrorNone)
691 msg_Dbg(p_dec, "Set OMX_IndexVendorSetYUV420pMode successfully");
693 msg_Dbg(p_dec, "Unable to set OMX_IndexVendorSetYUV420pMode: %x", omx_error);
697 if(!strncmp(p_sys->psz_component, "OMX.broadcom.", 13))
699 OMX_CONFIG_REQUESTCALLBACKTYPE notifications;
700 OMX_INIT_STRUCTURE(notifications);
702 notifications.nPortIndex = p_sys->out.i_port_index;
703 notifications.nIndex = OMX_IndexParamBrcmPixelAspectRatio;
704 notifications.bEnable = OMX_TRUE;
706 omx_error = OMX_SetParameter(omx_handle,
707 OMX_IndexConfigRequestCallback, ¬ifications);
708 if (omx_error == OMX_ErrorNone) {
709 msg_Dbg(p_dec, "Enabled aspect ratio notifications");
710 p_sys->b_aspect_ratio_handled = true;
712 msg_Dbg(p_dec, "Could not enable aspect ratio notifications");
715 /* Set port definitions */
716 for(i = 0; i < p_sys->ports; i++)
718 omx_error = SetPortDefinition(p_dec, &p_sys->p_ports[i],
719 p_sys->p_ports[i].p_fmt);
720 if(omx_error != OMX_ErrorNone) goto error;
723 if(!strncmp(p_sys->psz_component, "OMX.broadcom.", 13) &&
724 p_sys->in.p_fmt->i_codec == VLC_CODEC_H264)
726 OMX_PARAM_BRCMVIDEODECODEERRORCONCEALMENTTYPE concanParam;
727 OMX_INIT_STRUCTURE(concanParam);
728 concanParam.bStartWithValidFrame = OMX_FALSE;
730 omx_error = OMX_SetParameter(omx_handle,
731 OMX_IndexParamBrcmVideoDecodeErrorConcealment, &concanParam);
732 if (omx_error == OMX_ErrorNone)
733 msg_Dbg(p_dec, "StartWithValidFrame disabled.");
735 msg_Dbg(p_dec, "Could not disable StartWithValidFrame.");
738 /* Allocate our array for the omx buffers and enable ports */
739 for(i = 0; i < p_sys->ports; i++)
741 OmxPort *p_port = &p_sys->p_ports[i];
744 malloc(p_port->definition.nBufferCountActual *
745 sizeof(OMX_BUFFERHEADERTYPE*));
746 if(!p_port->pp_buffers)
748 omx_error = OMX_ErrorInsufficientResources;
749 CHECK_ERROR(omx_error, "memory allocation failed");
751 p_port->i_buffers = p_port->definition.nBufferCountActual;
754 if(!p_port->definition.bEnabled)
756 omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
757 p_port->i_port_index, NULL);
758 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
759 (int)p_port->i_port_index, omx_error );
760 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
761 CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
762 (int)p_port->i_port_index, omx_error );
766 *p_handle = omx_handle;
767 return OMX_ErrorNone;
770 DeinitialiseComponent(p_dec, omx_handle);
775 /*****************************************************************************
776 * OpenDecoder: Create the decoder instance
777 *****************************************************************************/
778 static int OpenDecoder( vlc_object_t *p_this )
780 decoder_t *p_dec = (decoder_t*)p_this;
783 if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
786 status = OpenGeneric( p_this, false );
787 if(status != VLC_SUCCESS) return status;
789 p_dec->pf_decode_video = DecodeVideo;
790 p_dec->pf_decode_audio = DecodeAudio;
795 /*****************************************************************************
796 * OpenEncoder: Create the encoder instance
797 *****************************************************************************/
798 static int OpenEncoder( vlc_object_t *p_this )
800 encoder_t *p_enc = (encoder_t*)p_this;
803 if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
806 status = OpenGeneric( p_this, true );
807 if(status != VLC_SUCCESS) return status;
809 p_enc->pf_encode_video = EncodeVideo;
814 /*****************************************************************************
815 * OpenGeneric: Create the generic decoder/encoder instance
816 *****************************************************************************/
817 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
819 decoder_t *p_dec = (decoder_t*)p_this;
820 decoder_sys_t *p_sys;
821 OMX_ERRORTYPE omx_error;
822 OMX_BUFFERHEADERTYPE *p_header;
825 if (InitOmxCore(p_this) != VLC_SUCCESS) {
829 /* Allocate the memory needed to store the decoder's structure */
830 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
836 /* Initialise the thread properties */
839 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
840 p_dec->fmt_out.video = p_dec->fmt_in.video;
841 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
842 p_dec->fmt_out.i_codec = 0;
844 /* set default aspect of 1, if parser did not set it */
845 if (p_dec->fmt_out.video.i_sar_num == 0)
846 p_dec->fmt_out.video.i_sar_num = 1;
847 if (p_dec->fmt_out.video.i_sar_den == 0)
848 p_dec->fmt_out.video.i_sar_den = 1;
850 p_sys->b_enc = b_encode;
851 InitOmxEventQueue(&p_sys->event_queue);
852 vlc_mutex_init (&p_sys->in.fifo.lock);
853 vlc_cond_init (&p_sys->in.fifo.wait);
854 p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
855 p_sys->in.fifo.pp_last = &p_sys->in.fifo.p_first;
856 p_sys->in.b_direct = false;
857 p_sys->in.b_flushed = true;
858 p_sys->in.p_fmt = &p_dec->fmt_in;
859 vlc_mutex_init (&p_sys->out.fifo.lock);
860 vlc_cond_init (&p_sys->out.fifo.wait);
861 p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
862 p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
863 p_sys->out.b_direct = false;
864 p_sys->out.b_flushed = true;
865 p_sys->out.p_fmt = &p_dec->fmt_out;
867 p_sys->p_ports = &p_sys->in;
868 p_sys->b_use_pts = 1;
870 msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
871 (char *)&p_dec->fmt_out.i_codec);
873 /* Enumerate components and build a list of the one we want to try */
875 CreateComponentsList(p_this,
876 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
877 p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
878 p_sys->b_enc), p_sys->ppsz_components);
879 if( !p_sys->components )
881 msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
882 (char *)&p_dec->fmt_in.i_codec );
883 CloseGeneric(p_this);
887 /* Try to load and initialise a component */
888 omx_error = OMX_ErrorUndefined;
889 for(i = 0; i < p_sys->components; i++)
892 /* ignore OpenCore software codecs */
893 if (!strncmp(p_sys->ppsz_components[i], "OMX.PV.", 7))
895 /* The same sw codecs, renamed in ICS (perhaps also in honeycomb) */
896 if (!strncmp(p_sys->ppsz_components[i], "OMX.google.", 11))
898 /* This one has been seen on HTC One V - it behaves like it works,
899 * but FillBufferDone returns buffers filled with 0 bytes. The One V
900 * has got a working OMX.qcom.video.decoder.avc instead though. */
901 if (!strncmp(p_sys->ppsz_components[i], "OMX.ARICENT.", 12))
903 /* Codecs with DRM, that don't output plain YUV data but only
904 * support direct rendering where the output can't be intercepted. */
905 if (strstr(p_sys->ppsz_components[i], ".secure"))
907 /* Use VC1 decoder for WMV3 for now */
908 if (!strcmp(p_sys->ppsz_components[i], "OMX.SEC.WMV.Decoder"))
910 /* This decoder does work, but has an insane latency (leading to errors
911 * about "main audio output playback way too late" and dropped frames).
912 * At least Samsung Galaxy S III (where this decoder is present) has
913 * got another one, OMX.SEC.mp3.dec, that works well and has a
914 * sensible latency. (Also, even if that one isn't found, in general,
915 * using SW codecs is usually more than fast enough for MP3.) */
916 if (!strcmp(p_sys->ppsz_components[i], "OMX.SEC.MP3.Decoder"))
918 /* This codec should be able to handle both VC1 and WMV3, but
919 * for VC1 it doesn't output any buffers at all (in the way we use
920 * it) and for WMV3 it outputs plain black buffers. Thus ignore
921 * it until we can make it work properly. */
922 if (!strcmp(p_sys->ppsz_components[i], "OMX.Nvidia.vc1.decode"))
925 omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
927 if(omx_error == OMX_ErrorNone) break;
929 CHECK_ERROR(omx_error, "no component could be initialised" );
931 /* Move component to Idle then Executing state */
932 OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
933 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
935 /* Allocate omx buffers */
936 for(i = 0; i < p_sys->ports; i++)
938 OmxPort *p_port = &p_sys->p_ports[i];
940 for(j = 0; j < p_port->i_buffers; j++)
943 #define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
944 char *p_buf = malloc(p_port->definition.nBufferSize +
945 p_port->definition.nBufferAlignment);
946 p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
951 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
952 p_port->i_port_index, 0,
953 p_port->definition.nBufferSize, (void*)1);
956 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
957 p_port->i_port_index, 0,
958 p_port->definition.nBufferSize);
960 if(omx_error != OMX_ErrorNone) break;
961 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
963 p_port->i_buffers = j;
964 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
965 omx_error, (int)p_port->i_port_index, j );
968 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
969 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
971 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
972 OMX_StateExecuting, 0);
973 CHECK_ERROR(omx_error, "OMX_CommandStateSet Executing failed (%x)", omx_error );
974 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
975 CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
977 /* Send codec configuration data */
978 if( p_dec->fmt_in.i_extra )
980 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
981 p_header->nFilledLen = p_dec->fmt_in.i_extra;
983 /* Convert H.264 NAL format to annex b */
984 if( p_sys->i_nal_size_length && !p_sys->in.b_direct )
986 p_header->nFilledLen = 0;
987 convert_sps_pps( p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
988 p_header->pBuffer, p_header->nAllocLen,
989 (uint32_t*) &p_header->nFilledLen, NULL );
991 else if(p_sys->in.b_direct)
993 p_header->pOutputPortPrivate = p_header->pBuffer;
994 p_header->pBuffer = p_dec->fmt_in.p_extra;
996 else if (p_dec->fmt_in.i_codec == VLC_CODEC_WMV3 &&
997 p_dec->fmt_in.i_extra >= 4 &&
998 p_header->nAllocLen >= 36)
1001 // According to OMX IL 1.2.0 spec (4.3.33.2), the codec config
1002 // data for VC-1 Main/Simple (aka WMV3) is according to table 265
1003 // in the VC-1 spec. Most of the fields are just set with placeholders
1004 // (like framerate, hrd_buffer/rate).
1005 static const uint8_t wmv3seq[] = {
1006 0xff, 0xff, 0xff, 0xc5, // numframes=ffffff, marker byte
1007 0x04, 0x00, 0x00, 0x00, // marker byte
1008 0x00, 0x00, 0x00, 0x00, // struct C, almost equal to p_extra
1009 0x00, 0x00, 0x00, 0x00, // struct A, vert size
1010 0x00, 0x00, 0x00, 0x00, // struct A, horiz size
1011 0x0c, 0x00, 0x00, 0x00, // marker byte
1012 0xff, 0xff, 0x00, 0x80, // struct B, level=4, cbr=0, hrd_buffer=ffff
1013 0xff, 0xff, 0x00, 0x00, // struct B, hrd_rate=ffff
1014 0xff, 0xff, 0xff, 0xff, // struct B, framerate=ffffffff
1016 p_header->nFilledLen = sizeof(wmv3seq);
1017 memcpy(p_header->pBuffer, wmv3seq, p_header->nFilledLen);
1018 // Struct C - almost equal to the extradata
1019 memcpy(&p_header->pBuffer[8], p_dec->fmt_in.p_extra, 4);
1020 // Expand profile from the highest 2 bits to the highest 4 bits
1021 profile = p_header->pBuffer[8] >> 6;
1022 p_header->pBuffer[8] = (p_header->pBuffer[8] & 0x0f) | (profile << 4);
1023 // Fill in the height/width for struct A
1024 SetDWLE(&p_header->pBuffer[12], p_dec->fmt_in.video.i_height);
1025 SetDWLE(&p_header->pBuffer[16], p_dec->fmt_in.video.i_width);
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 | OMX_BUFFERFLAG_ENDOFFRAME;
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;
1056 if (!p_sys->b_use_pts)
1057 msg_Dbg( p_dec, "using dts timestamp mode for %s", p_sys->psz_component);
1062 CloseGeneric(p_this);
1063 return VLC_EGENERIC;
1066 /*****************************************************************************
1068 *****************************************************************************/
1069 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
1071 decoder_sys_t *p_sys = p_dec->p_sys;
1072 OMX_PARAM_PORTDEFINITIONTYPE definition;
1073 OMX_BUFFERHEADERTYPE *p_buffer;
1074 OMX_ERRORTYPE omx_error;
1077 /* Sanity checking */
1078 OMX_INIT_STRUCTURE(definition);
1079 definition.nPortIndex = p_port->i_port_index;
1080 omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1082 if(omx_error != OMX_ErrorNone || (p_dec->fmt_in.i_cat == VIDEO_ES &&
1083 (!definition.format.video.nFrameWidth ||
1084 !definition.format.video.nFrameHeight)) )
1085 return OMX_ErrorUndefined;
1087 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
1088 p_port->i_port_index, NULL);
1089 CHECK_ERROR(omx_error, "OMX_CommandPortDisable on %i failed (%x)",
1090 (int)p_port->i_port_index, omx_error );
1092 for(i = 0; i < p_port->i_buffers; i++)
1094 OMX_FIFO_GET(&p_port->fifo, p_buffer);
1095 if (p_buffer->pAppPrivate != NULL)
1096 decoder_DeletePicture( p_dec, p_buffer->pAppPrivate );
1097 if (p_buffer->nFlags & SENTINEL_FLAG) {
1102 omx_error = OMX_FreeBuffer( p_sys->omx_handle,
1103 p_port->i_port_index, p_buffer );
1105 if(omx_error != OMX_ErrorNone) break;
1107 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
1108 omx_error, (int)p_port->i_port_index, i );
1110 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
1111 CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
1113 /* Get the new port definition */
1114 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1115 if(omx_error != OMX_ErrorNone) goto error;
1117 if( p_dec->fmt_in.i_cat != AUDIO_ES )
1119 /* Don't explicitly set the new parameters that we got with
1120 * OMX_GetParameter above when using audio codecs.
1121 * That struct hasn't been changed since, so there should be
1122 * no need to set it here, unless some codec expects the
1123 * SetParameter call as a trigger event for some part of
1124 * the reconfiguration.
1125 * This fixes using audio decoders on Samsung Galaxy S II,
1127 * Only skipping this for audio codecs, to minimize the
1128 * change for current working configurations for video.
1130 omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1132 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
1133 omx_error, ErrorToString(omx_error));
1136 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
1137 p_port->i_port_index, NULL);
1138 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
1139 (int)p_port->i_port_index, omx_error );
1141 if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
1142 free(p_port->pp_buffers);
1143 p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
1144 if(!p_port->pp_buffers)
1146 omx_error = OMX_ErrorInsufficientResources;
1147 CHECK_ERROR(omx_error, "memory allocation failed");
1150 p_port->i_buffers = p_port->definition.nBufferCountActual;
1151 for(i = 0; i < p_port->i_buffers; i++)
1153 if(p_port->b_direct)
1155 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1156 p_port->i_port_index, 0,
1157 p_port->definition.nBufferSize, (void*)1);
1160 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1161 p_port->i_port_index, 0,
1162 p_port->definition.nBufferSize);
1164 if(omx_error != OMX_ErrorNone) break;
1165 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1167 p_port->i_buffers = i;
1168 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1169 omx_error, (int)p_port->i_port_index, i );
1171 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
1172 CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1174 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1175 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1181 /*****************************************************************************
1182 * DecodeVideo: Called to decode one frame
1183 *****************************************************************************/
1184 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1186 decoder_sys_t *p_sys = p_dec->p_sys;
1187 picture_t *p_pic = NULL, *p_next_pic;
1188 OMX_ERRORTYPE omx_error;
1191 OMX_BUFFERHEADERTYPE *p_header;
1193 int i_input_used = 0;
1194 struct H264ConvertState convert_state = { 0, 0 };
1196 if( !pp_block || !*pp_block )
1199 p_block = *pp_block;
1201 /* Check for errors from codec */
1204 msg_Dbg(p_dec, "error during decoding");
1205 block_Release( p_block );
1209 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1211 block_Release( p_block );
1212 if(!p_sys->in.b_flushed)
1214 msg_Dbg(p_dec, "flushing");
1215 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1216 p_sys->in.definition.nPortIndex, 0 );
1218 p_sys->in.b_flushed = true;
1222 /* Use the aspect ratio provided by the input (ie read from packetizer).
1223 * In case the we get aspect ratio info from the decoder (as in the
1224 * broadcom OMX implementation on RPi), don't let the packetizer values
1225 * override what the decoder says, if anything - otherwise always update
1226 * even if it already is set (since it can change within a stream). */
1227 if((p_dec->fmt_in.video.i_sar_num != 0 && p_dec->fmt_in.video.i_sar_den != 0) &&
1228 (p_dec->fmt_out.video.i_sar_num == 0 || p_dec->fmt_out.video.i_sar_den == 0 ||
1229 !p_sys->b_aspect_ratio_handled))
1231 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_in.video.i_sar_num;
1232 p_dec->fmt_out.video.i_sar_den = p_dec->fmt_in.video.i_sar_den;
1235 /* Take care of decoded frames first */
1238 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1239 if(!p_header) break; /* No frame available */
1241 if(p_sys->out.b_update_def)
1243 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1244 p_sys->out.b_update_def = 0;
1247 if(p_header->nFilledLen)
1249 p_pic = p_header->pAppPrivate;
1252 /* We're not in direct rendering mode.
1253 * Get a new picture and copy the content */
1254 p_pic = decoder_NewPicture( p_dec );
1257 CopyOmxPicture(p_sys->out.definition.format.video.eColorFormat,
1258 p_pic, p_sys->out.definition.format.video.nSliceHeight,
1259 p_sys->out.i_frame_stride,
1260 p_header->pBuffer + p_header->nOffset,
1261 p_sys->out.i_frame_stride_chroma_div, NULL);
1265 p_pic->date = FromOmxTicks(p_header->nTimeStamp);
1266 p_header->nFilledLen = 0;
1267 p_header->pAppPrivate = 0;
1270 /* Get a new picture */
1271 if(p_sys->out.b_direct && !p_header->pAppPrivate)
1273 p_next_pic = decoder_NewPicture( p_dec );
1274 if(!p_next_pic) break;
1276 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1277 p_header->pAppPrivate = p_next_pic;
1278 p_header->pInputPortPrivate = p_header->pBuffer;
1279 p_header->pBuffer = p_next_pic->p[0].p_pixels;
1283 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1286 #ifdef OMXIL_EXTRA_DEBUG
1287 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1289 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1293 /* Send the input buffer to the component */
1294 OMX_FIFO_GET_TIMEOUT(&p_sys->in.fifo, p_header, 200000);
1296 if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1303 bool decode_more = false;
1304 p_header->nFilledLen = p_block->i_buffer - i_input_used;
1305 p_header->nOffset = 0;
1306 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1307 if (p_sys->b_use_pts && p_block->i_pts)
1308 p_header->nTimeStamp = ToOmxTicks(p_block->i_pts);
1310 p_header->nTimeStamp = ToOmxTicks(p_block->i_dts);
1312 /* In direct mode we pass the input pointer as is.
1313 * Otherwise we memcopy the data */
1314 if(p_sys->in.b_direct)
1316 p_header->pOutputPortPrivate = p_header->pBuffer;
1317 p_header->pBuffer = p_block->p_buffer;
1318 p_header->pAppPrivate = p_block;
1319 i_input_used = p_header->nFilledLen;
1323 if(p_header->nFilledLen > p_header->nAllocLen)
1325 p_header->nFilledLen = p_header->nAllocLen;
1327 memcpy(p_header->pBuffer, p_block->p_buffer + i_input_used, p_header->nFilledLen);
1328 i_input_used += p_header->nFilledLen;
1329 if (i_input_used == p_block->i_buffer)
1331 block_Release(p_block);
1336 p_header->nFlags &= ~OMX_BUFFERFLAG_ENDOFFRAME;
1340 /* Convert H.264 NAL format to annex b. Doesn't do anything if
1341 * i_nal_size_length == 0, which is the case for codecs other
1343 convert_h264_to_annexb( p_header->pBuffer, p_header->nFilledLen,
1344 p_sys->i_nal_size_length, &convert_state );
1345 #ifdef OMXIL_EXTRA_DEBUG
1346 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i, %"PRId64, p_header, p_header->pBuffer,
1347 (int)p_header->nFilledLen, FromOmxTicks(p_header->nTimeStamp) );
1349 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1350 p_sys->in.b_flushed = false;
1354 *pp_block = NULL; /* Avoid being fed the same packet again */
1358 /* Handle the PortSettingsChanged events */
1359 for(i = 0; i < p_sys->ports; i++)
1361 OmxPort *p_port = &p_sys->p_ports[i];
1362 if(p_port->b_reconfigure)
1364 omx_error = PortReconfigure(p_dec, p_port);
1365 p_port->b_reconfigure = 0;
1367 if(p_port->b_update_def)
1369 omx_error = GetPortDefinition(p_dec, p_port, p_port->p_fmt);
1370 p_port->b_update_def = 0;
1377 /*****************************************************************************
1378 * DecodeAudio: Called to decode one frame
1379 *****************************************************************************/
1380 block_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1382 decoder_sys_t *p_sys = p_dec->p_sys;
1383 block_t *p_buffer = NULL;
1384 OMX_BUFFERHEADERTYPE *p_header;
1385 OMX_ERRORTYPE omx_error;
1389 if( !pp_block || !*pp_block ) return NULL;
1391 p_block = *pp_block;
1393 /* Check for errors from codec */
1396 msg_Dbg(p_dec, "error during decoding");
1397 block_Release( p_block );
1401 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1403 block_Release( p_block );
1404 date_Set( &p_sys->end_date, 0 );
1405 if(!p_sys->in.b_flushed)
1407 msg_Dbg(p_dec, "flushing");
1408 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1409 p_sys->in.definition.nPortIndex, 0 );
1411 p_sys->in.b_flushed = true;
1415 if( !date_Get( &p_sys->end_date ) )
1417 if( !p_block->i_pts )
1419 /* We've just started the stream, wait for the first PTS. */
1420 block_Release( p_block );
1423 date_Set( &p_sys->end_date, p_block->i_pts );
1426 /* Take care of decoded frames first */
1429 unsigned int i_samples = 0;
1431 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1432 if(!p_header) break; /* No frame available */
1434 if (p_sys->out.p_fmt->audio.i_channels)
1435 i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1438 p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1439 if( !p_buffer ) break; /* No audio buffer available */
1441 memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1442 p_header->nFilledLen = 0;
1444 int64_t timestamp = FromOmxTicks(p_header->nTimeStamp);
1445 if( timestamp != 0 &&
1446 timestamp != date_Get( &p_sys->end_date ) )
1447 date_Set( &p_sys->end_date, timestamp );
1449 p_buffer->i_pts = date_Get( &p_sys->end_date );
1450 p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1454 #ifdef OMXIL_EXTRA_DEBUG
1455 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1457 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1458 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1462 /* Send the input buffer to the component */
1463 OMX_FIFO_GET_TIMEOUT(&p_sys->in.fifo, p_header, 200000);
1465 if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1472 p_header->nFilledLen = p_block->i_buffer;
1473 p_header->nOffset = 0;
1474 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1475 p_header->nTimeStamp = ToOmxTicks(p_block->i_dts);
1477 /* In direct mode we pass the input pointer as is.
1478 * Otherwise we memcopy the data */
1479 if(p_sys->in.b_direct)
1481 p_header->pOutputPortPrivate = p_header->pBuffer;
1482 p_header->pBuffer = p_block->p_buffer;
1483 p_header->pAppPrivate = p_block;
1487 if(p_header->nFilledLen > p_header->nAllocLen)
1489 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1490 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1491 p_header->nFilledLen = p_header->nAllocLen;
1493 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1494 block_Release(p_block);
1497 #ifdef OMXIL_EXTRA_DEBUG
1498 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1499 (int)p_header->nFilledLen );
1501 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1502 p_sys->in.b_flushed = false;
1503 *pp_block = NULL; /* Avoid being fed the same packet again */
1507 /* Handle the PortSettingsChanged events */
1508 for(i = 0; i < p_sys->ports; i++)
1510 OmxPort *p_port = &p_sys->p_ports[i];
1511 if(!p_port->b_reconfigure) continue;
1512 p_port->b_reconfigure = 0;
1513 omx_error = PortReconfigure(p_dec, p_port);
1519 /*****************************************************************************
1520 * EncodeVideo: Called to encode one frame
1521 *****************************************************************************/
1522 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1524 decoder_t *p_dec = ( decoder_t *)p_enc;
1525 decoder_sys_t *p_sys = p_dec->p_sys;
1526 OMX_ERRORTYPE omx_error;
1529 OMX_BUFFERHEADERTYPE *p_header;
1530 block_t *p_block = 0;
1532 if( !p_pic ) return NULL;
1534 /* Check for errors from codec */
1537 msg_Dbg(p_dec, "error during encoding");
1541 /* Send the input buffer to the component */
1542 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1545 /* In direct mode we pass the input pointer as is.
1546 * Otherwise we memcopy the data */
1547 if(p_sys->in.b_direct)
1549 p_header->pOutputPortPrivate = p_header->pBuffer;
1550 p_header->pBuffer = p_pic->p[0].p_pixels;
1554 CopyVlcPicture(p_dec, p_header, p_pic);
1557 p_header->nFilledLen = p_sys->in.i_frame_size;
1558 p_header->nOffset = 0;
1559 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1560 p_header->nTimeStamp = ToOmxTicks(p_pic->date);
1561 #ifdef OMXIL_EXTRA_DEBUG
1562 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1563 (int)p_header->nFilledLen );
1565 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1566 p_sys->in.b_flushed = false;
1569 /* Handle the PortSettingsChanged events */
1570 for(i = 0; i < p_sys->ports; i++)
1572 OmxPort *p_port = &p_sys->p_ports[i];
1573 if(!p_port->b_reconfigure) continue;
1574 p_port->b_reconfigure = 0;
1575 omx_error = PortReconfigure(p_dec, p_port);
1578 /* Wait for the decoded frame */
1581 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1583 if(p_header->nFilledLen)
1585 if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1587 /* TODO: need to store codec config */
1588 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1591 p_block = p_header->pAppPrivate;
1594 /* We're not in direct rendering mode.
1595 * Get a new block and copy the content */
1596 p_block = block_Alloc( p_header->nFilledLen );
1597 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1600 p_block->i_buffer = p_header->nFilledLen;
1601 p_block->i_pts = p_block->i_dts = FromOmxTicks(p_header->nTimeStamp);
1602 p_header->nFilledLen = 0;
1603 p_header->pAppPrivate = 0;
1606 #ifdef OMXIL_EXTRA_DEBUG
1607 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1609 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1612 msg_Dbg(p_dec, "done");
1616 /*****************************************************************************
1617 * CloseGeneric: omxil decoder destruction
1618 *****************************************************************************/
1619 static void CloseGeneric( vlc_object_t *p_this )
1621 decoder_t *p_dec = (decoder_t *)p_this;
1622 decoder_sys_t *p_sys = p_dec->p_sys;
1624 if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1628 DeinitOmxEventQueue(&p_sys->event_queue);
1629 vlc_mutex_destroy (&p_sys->in.fifo.lock);
1630 vlc_cond_destroy (&p_sys->in.fifo.wait);
1631 vlc_mutex_destroy (&p_sys->out.fifo.lock);
1632 vlc_cond_destroy (&p_sys->out.fifo.wait);
1637 /*****************************************************************************
1639 *****************************************************************************/
1640 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1641 OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1642 OMX_U32 data_2, OMX_PTR event_data )
1644 decoder_t *p_dec = (decoder_t *)app_data;
1645 decoder_sys_t *p_sys = p_dec->p_sys;
1649 PrintOmxEvent((vlc_object_t *) p_dec, event, data_1, data_2, event_data);
1652 case OMX_EventError:
1653 //p_sys->b_error = true;
1656 case OMX_EventPortSettingsChanged:
1657 if( data_2 == 0 || data_2 == OMX_IndexParamPortDefinition ||
1658 data_2 == OMX_IndexParamAudioPcm )
1660 OMX_BUFFERHEADERTYPE *sentinel;
1661 for(i = 0; i < p_sys->ports; i++)
1662 if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1663 p_sys->p_ports[i].b_reconfigure = true;
1664 sentinel = calloc(1, sizeof(*sentinel));
1666 sentinel->nFlags = SENTINEL_FLAG;
1667 OMX_FIFO_PUT(&p_sys->in.fifo, sentinel);
1670 else if( data_2 == OMX_IndexConfigCommonOutputCrop )
1672 for(i = 0; i < p_sys->ports; i++)
1673 if(p_sys->p_ports[i].definition.nPortIndex == data_1)
1674 p_sys->p_ports[i].b_update_def = true;
1678 msg_Dbg( p_dec, "Unhandled setting change %x", (unsigned int)data_2 );
1681 case OMX_EventParamOrConfigChanged:
1682 UpdatePixelAspect(p_dec);
1689 PostOmxEvent(&p_sys->event_queue, event, data_1, data_2, event_data);
1690 return OMX_ErrorNone;
1693 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1694 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1696 decoder_t *p_dec = (decoder_t *)app_data;
1697 decoder_sys_t *p_sys = p_dec->p_sys;
1700 #ifdef OMXIL_EXTRA_DEBUG
1701 msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1704 if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1706 block_t *p_block = (block_t *)omx_header->pAppPrivate;
1707 omx_header->pBuffer = omx_header->pOutputPortPrivate;
1708 if(p_block) block_Release(p_block);
1709 omx_header->pAppPrivate = 0;
1711 OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1713 return OMX_ErrorNone;
1716 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1717 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1719 decoder_t *p_dec = (decoder_t *)app_data;
1720 decoder_sys_t *p_sys = p_dec->p_sys;
1723 #ifdef OMXIL_EXTRA_DEBUG
1724 msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i, %"PRId64, omx_header, omx_header->pBuffer,
1725 (int)omx_header->nFilledLen, FromOmxTicks(omx_header->nTimeStamp) );
1728 if(omx_header->pInputPortPrivate)
1730 omx_header->pBuffer = omx_header->pInputPortPrivate;
1732 OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1734 return OMX_ErrorNone;