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"
44 # define OMXIL_EXTRA_DEBUG
47 #define SENTINEL_FLAG 0x10000
49 /*****************************************************************************
51 *****************************************************************************/
52 static int OpenDecoder( vlc_object_t * );
53 static int OpenEncoder( vlc_object_t * );
54 static int OpenGeneric( vlc_object_t *, bool b_encode );
55 static void CloseGeneric( vlc_object_t * );
57 static picture_t *DecodeVideo( decoder_t *, block_t ** );
58 static block_t *DecodeAudio ( decoder_t *, block_t ** );
59 static block_t *EncodeVideo( encoder_t *, picture_t * );
61 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE, OMX_PTR, OMX_EVENTTYPE,
62 OMX_U32, OMX_U32, OMX_PTR );
63 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE, OMX_PTR,
64 OMX_BUFFERHEADERTYPE * );
65 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE, OMX_PTR,
66 OMX_BUFFERHEADERTYPE * );
68 /*****************************************************************************
70 *****************************************************************************/
72 set_description( N_("Audio/Video decoder (using OpenMAX IL)") )
73 set_category( CAT_INPUT )
74 set_subcategory( SUBCAT_INPUT_VCODEC )
75 set_section( N_("Decoding") , NULL )
77 /* For IOMX, don't enable it automatically via priorities,
78 * enable it only via the --codec iomx command line parameter when
80 set_capability( "decoder", 0 )
82 set_capability( "decoder", 80 )
84 set_callbacks( OpenDecoder, CloseGeneric )
87 set_section( N_("Encoding") , NULL )
88 set_description( N_("Video encoder (using OpenMAX IL)") )
89 set_capability( "encoder", 0 )
90 set_callbacks( OpenEncoder, CloseGeneric )
93 /*****************************************************************************
94 * ImplementationSpecificWorkarounds: place-holder for implementation
95 * specific workarounds
96 *****************************************************************************/
97 static OMX_ERRORTYPE ImplementationSpecificWorkarounds(decoder_t *p_dec,
98 OmxPort *p_port, es_format_t *p_fmt)
100 decoder_sys_t *p_sys = p_dec->p_sys;
101 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
102 int i_profile = 0xFFFF, i_level = 0xFFFF;
104 /* Try to find out the profile of the video */
105 while(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
106 p_fmt->i_codec == VLC_CODEC_H264)
108 uint8_t *p = (uint8_t*)p_dec->fmt_in.p_extra;
109 if(!p || !p_dec->fmt_in.p_extra) break;
111 /* Check the profile / level */
112 if(p_dec->fmt_in.i_original_fourcc == VLC_FOURCC('a','v','c','1') &&
115 if(p_dec->fmt_in.i_extra < 12) break;
116 p_sys->i_nal_size_length = 1 + (p[4]&0x03);
117 if( !(p[5]&0x1f) ) break;
122 if(p_dec->fmt_in.i_extra < 8) break;
123 if(!p[0] && !p[1] && !p[2] && p[3] == 1) p += 4;
124 else if(!p[0] && !p[1] && p[2] == 1) p += 3;
128 if( ((*p++)&0x1f) != 7) break;
130 /* Get profile/level out of first SPS */
136 if(!strcmp(p_sys->psz_component, "OMX.TI.Video.Decoder"))
138 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
139 p_fmt->i_codec == VLC_CODEC_H264 &&
140 (i_profile != 66 || i_level > 30))
142 msg_Dbg(p_dec, "h264 profile/level not supported (0x%x, 0x%x)",
144 return OMX_ErrorNotImplemented;
147 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirOutput &&
148 p_fmt->i_codec == VLC_CODEC_I420)
150 /* I420 xvideo is slow on OMAP */
151 def->format.video.eColorFormat = OMX_COLOR_FormatCbYCrY;
152 GetVlcChromaFormat( def->format.video.eColorFormat,
153 &p_fmt->i_codec, 0 );
154 GetVlcChromaSizes( p_fmt->i_codec,
155 def->format.video.nFrameWidth,
156 def->format.video.nFrameHeight,
157 &p_port->i_frame_size, &p_port->i_frame_stride,
158 &p_port->i_frame_stride_chroma_div );
159 def->format.video.nStride = p_port->i_frame_stride;
160 def->nBufferSize = p_port->i_frame_size;
163 else if(!strcmp(p_sys->psz_component, "OMX.st.video_encoder"))
165 if(p_fmt->i_cat == VIDEO_ES)
167 /* Bellagio's encoder doesn't encode the framerate in Q16 */
168 def->format.video.xFramerate >>= 16;
171 #if 0 /* FIXME: doesn't apply for HP Touchpad */
172 else if (!strncmp(p_sys->psz_component, "OMX.qcom.video.decoder.",
173 strlen("OMX.qcom.video.decoder")))
175 /* qdsp6 refuses buffer size larger than 450K on input port */
176 if (def->nBufferSize > 450 * 1024)
178 def->nBufferSize = 450 * 1024;
179 p_port->i_frame_size = def->nBufferSize;
184 else if (!strcmp(p_sys->psz_component, "OMX.broadcom.video_decode"))
186 /* Clear these fields before setting parameters, to allow the codec
187 * fill in what it wants (instead of rejecting whatever happened to
189 def->format.video.nStride = def->format.video.nSliceHeight = 0;
193 return OMX_ErrorNone;
196 /*****************************************************************************
197 * SetPortDefinition: set definition of the omx port based on the vlc format
198 *****************************************************************************/
199 static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
202 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
203 OMX_ERRORTYPE omx_error;
205 omx_error = OMX_GetParameter(p_port->omx_handle,
206 OMX_IndexParamPortDefinition, def);
207 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
208 omx_error, ErrorToString(omx_error));
213 def->format.video.nFrameWidth = p_fmt->video.i_width;
214 def->format.video.nFrameHeight = p_fmt->video.i_height;
215 if(def->format.video.eCompressionFormat == OMX_VIDEO_CodingUnused)
216 def->format.video.nStride = def->format.video.nFrameWidth;
217 if( p_fmt->video.i_frame_rate > 0 &&
218 p_fmt->video.i_frame_rate_base > 0 )
219 def->format.video.xFramerate = (p_fmt->video.i_frame_rate << 16) /
220 p_fmt->video.i_frame_rate_base;
222 if(def->eDir == OMX_DirInput || p_dec->p_sys->b_enc)
224 if (def->eDir == OMX_DirInput && p_dec->p_sys->b_enc)
225 def->nBufferSize = def->format.video.nFrameWidth *
226 def->format.video.nFrameHeight * 2;
227 p_port->i_frame_size = def->nBufferSize;
229 if(!GetOmxVideoFormat(p_fmt->i_codec,
230 &def->format.video.eCompressionFormat, 0) )
232 if(!GetOmxChromaFormat(p_fmt->i_codec,
233 &def->format.video.eColorFormat, 0) )
235 omx_error = OMX_ErrorNotImplemented;
236 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
237 (char *)&p_fmt->i_codec );
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 def->nBufferSize = p_port->i_frame_size;
250 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
251 &p_fmt->i_codec, 0 ) )
253 omx_error = OMX_ErrorNotImplemented;
254 CHECK_ERROR(omx_error, "OMX color format %i not supported",
255 (int)def->format.video.eColorFormat );
257 GetVlcChromaSizes( p_fmt->i_codec,
258 def->format.video.nFrameWidth,
259 def->format.video.nFrameHeight,
260 &p_port->i_frame_size, &p_port->i_frame_stride,
261 &p_port->i_frame_stride_chroma_div );
262 def->format.video.nStride = p_port->i_frame_stride;
263 if (p_port->i_frame_size > def->nBufferSize)
264 def->nBufferSize = p_port->i_frame_size;
269 p_port->i_frame_size = def->nBufferSize;
270 if(def->eDir == OMX_DirInput)
272 if(!GetOmxAudioFormat(p_fmt->i_codec,
273 &def->format.audio.eEncoding, 0) )
275 omx_error = OMX_ErrorNotImplemented;
276 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
277 (char *)&p_fmt->i_codec );
282 if( !OmxToVlcAudioFormat(def->format.audio.eEncoding,
283 &p_fmt->i_codec, 0 ) )
285 omx_error = OMX_ErrorNotImplemented;
286 CHECK_ERROR(omx_error, "OMX audio encoding %i not supported",
287 (int)def->format.audio.eEncoding );
292 default: return OMX_ErrorNotImplemented;
295 omx_error = ImplementationSpecificWorkarounds(p_dec, p_port, p_fmt);
296 CHECK_ERROR(omx_error, "ImplementationSpecificWorkarounds failed (%x : %s)",
297 omx_error, ErrorToString(omx_error));
299 omx_error = OMX_SetParameter(p_port->omx_handle,
300 OMX_IndexParamPortDefinition, def);
301 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
302 omx_error, ErrorToString(omx_error));
304 omx_error = OMX_GetParameter(p_port->omx_handle,
305 OMX_IndexParamPortDefinition, def);
306 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
307 omx_error, ErrorToString(omx_error));
309 if(p_port->i_frame_size > def->nBufferSize)
310 def->nBufferSize = p_port->i_frame_size;
311 p_port->i_frame_size = def->nBufferSize;
313 /* Deal with audio params */
314 if(p_fmt->i_cat == AUDIO_ES)
316 omx_error = SetAudioParameters(p_port->omx_handle,
317 &p_port->format_param, def->nPortIndex,
318 def->format.audio.eEncoding,
320 p_fmt->audio.i_channels,
323 p_fmt->audio.i_bitspersample,
324 p_fmt->audio.i_blockalign);
325 if (def->eDir == OMX_DirInput) {
326 CHECK_ERROR(omx_error, "SetAudioParameters failed (%x : %s)",
327 omx_error, ErrorToString(omx_error));
328 } else if (omx_error != OMX_ErrorNone) {
329 msg_Warn(p_dec, "SetAudioParameters failed (%x : %s) on output port",
330 omx_error, ErrorToString(omx_error));
331 omx_error = OMX_ErrorNone;
334 if (!strcmp(p_dec->p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER") &&
335 def->eDir == OMX_DirOutput)
337 /* When setting the output buffer size above, the decoder actually
338 * sets the buffer size to a lower value than what was chosen. If
339 * we try to allocate buffers of this size, it fails. Thus, forcibly
340 * use a larger buffer size. */
341 def->nBufferSize *= 2;
348 /*****************************************************************************
349 * GetPortDefinition: set vlc format based on the definition of the omx port
350 *****************************************************************************/
351 static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
354 decoder_sys_t *p_sys = p_dec->p_sys;
355 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
356 OMX_ERRORTYPE omx_error;
357 OMX_CONFIG_RECTTYPE crop_rect;
359 omx_error = OMX_GetParameter(p_port->omx_handle,
360 OMX_IndexParamPortDefinition, def);
361 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
362 omx_error, ErrorToString(omx_error));
367 p_fmt->video.i_width = def->format.video.nFrameWidth;
368 p_fmt->video.i_visible_width = def->format.video.nFrameWidth;
369 p_fmt->video.i_height = def->format.video.nFrameHeight;
370 p_fmt->video.i_visible_height = def->format.video.nFrameHeight;
371 p_fmt->video.i_frame_rate = p_dec->fmt_in.video.i_frame_rate;
372 p_fmt->video.i_frame_rate_base = p_dec->fmt_in.video.i_frame_rate_base;
374 OMX_INIT_STRUCTURE(crop_rect);
375 crop_rect.nPortIndex = def->nPortIndex;
376 omx_error = OMX_GetConfig(p_port->omx_handle, OMX_IndexConfigCommonOutputCrop, &crop_rect);
377 if (omx_error == OMX_ErrorNone)
379 if (!def->format.video.nSliceHeight)
380 def->format.video.nSliceHeight = def->format.video.nFrameHeight;
381 if (!def->format.video.nStride)
382 def->format.video.nStride = def->format.video.nFrameWidth;
383 p_fmt->video.i_width = crop_rect.nWidth;
384 p_fmt->video.i_visible_width = crop_rect.nWidth;
385 p_fmt->video.i_height = crop_rect.nHeight;
386 p_fmt->video.i_visible_height = crop_rect.nHeight;
387 if (def->format.video.eColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
388 def->format.video.nSliceHeight -= crop_rect.nTop/2;
392 /* Don't pass the error back to the caller, this isn't mandatory */
393 omx_error = OMX_ErrorNone;
396 /* Hack: Nexus One (stock firmware with binary OMX driver blob)
397 * claims to output 420Planar even though it in in practice is
399 if(def->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar &&
400 !strncmp(p_sys->psz_component, "OMX.qcom.video.decoder",
401 strlen("OMX.qcom.video.decoder")))
402 def->format.video.eColorFormat = OMX_QCOM_COLOR_FormatYVU420SemiPlanar;
404 if (IgnoreOmxDecoderPadding(p_sys->psz_component)) {
405 def->format.video.nSliceHeight = 0;
406 def->format.video.nStride = p_fmt->video.i_width;
409 if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
410 &p_fmt->i_codec, 0 ) )
412 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
413 &p_fmt->i_codec, 0 ) )
415 omx_error = OMX_ErrorNotImplemented;
416 CHECK_ERROR(omx_error, "OMX color format %i not supported",
417 (int)def->format.video.eColorFormat );
419 GetVlcChromaSizes( p_fmt->i_codec,
420 def->format.video.nFrameWidth,
421 def->format.video.nFrameHeight,
422 &p_port->i_frame_size, &p_port->i_frame_stride,
423 &p_port->i_frame_stride_chroma_div );
425 if(p_port->i_frame_size > def->nBufferSize)
426 def->nBufferSize = p_port->i_frame_size;
427 p_port->i_frame_size = def->nBufferSize;
429 if((int)p_port->i_frame_stride > def->format.video.nStride)
430 def->format.video.nStride = p_port->i_frame_stride;
432 p_port->i_frame_stride = def->format.video.nStride;
436 if( !OmxToVlcAudioFormat( def->format.audio.eEncoding,
437 &p_fmt->i_codec, 0 ) )
439 omx_error = OMX_ErrorNotImplemented;
440 CHECK_ERROR(omx_error, "OMX audio format %i not supported",
441 (int)def->format.audio.eEncoding );
444 omx_error = GetAudioParameters(p_port->omx_handle,
445 &p_port->format_param, def->nPortIndex,
446 def->format.audio.eEncoding,
447 &p_fmt->audio.i_channels,
448 &p_fmt->audio.i_rate,
450 &p_fmt->audio.i_bitspersample,
451 &p_fmt->audio.i_blockalign);
452 CHECK_ERROR(omx_error, "GetAudioParameters failed (%x : %s)",
453 omx_error, ErrorToString(omx_error));
455 if(p_fmt->audio.i_channels < 9)
457 static const int pi_channels_maps[9] =
459 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
460 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
461 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
462 | AOUT_CHAN_REARRIGHT,
463 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
464 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
465 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
466 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
467 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
468 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
469 | AOUT_CHAN_MIDDLERIGHT,
470 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
471 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
474 p_fmt->audio.i_physical_channels =
475 p_fmt->audio.i_original_channels =
476 pi_channels_maps[p_fmt->audio.i_channels];
479 date_Init( &p_dec->p_sys->end_date, p_fmt->audio.i_rate, 1 );
483 default: return OMX_ErrorNotImplemented;
490 /*****************************************************************************
491 * DeinitialiseComponent: Deinitialise and unload an OMX component
492 *****************************************************************************/
493 static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
494 OMX_HANDLETYPE omx_handle)
496 decoder_sys_t *p_sys = p_dec->p_sys;
497 OMX_ERRORTYPE omx_error;
501 if(!omx_handle) return OMX_ErrorNone;
503 omx_error = OMX_GetState(omx_handle, &state);
504 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
506 if(state == OMX_StateExecuting)
508 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
510 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
513 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, &cmd, &state, 0);
514 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
515 // The event queue can contain other OMX_EventCmdComplete items,
516 // such as for OMX_CommandFlush
517 if (cmd == OMX_CommandStateSet && state == OMX_StateIdle)
522 omx_error = OMX_GetState(omx_handle, &state);
523 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
525 if(state == OMX_StateIdle)
527 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
528 OMX_StateLoaded, 0 );
529 CHECK_ERROR(omx_error, "OMX_CommandStateSet Loaded failed (%x)", omx_error );
531 for(i = 0; i < p_sys->ports; i++)
533 OmxPort *p_port = &p_sys->p_ports[i];
534 OMX_BUFFERHEADERTYPE *p_buffer;
536 for(j = 0; j < p_port->i_buffers; j++)
538 OMX_FIFO_GET(&p_port->fifo, p_buffer);
539 if (p_buffer->nFlags & SENTINEL_FLAG) {
544 omx_error = OMX_FreeBuffer( omx_handle,
545 p_port->i_port_index, p_buffer );
547 if(omx_error != OMX_ErrorNone) break;
549 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
550 omx_error, (int)p_port->i_port_index, j );
552 OMX_FIFO_PEEK(&p_port->fifo, p_buffer);
553 if (!p_buffer) break;
555 OMX_FIFO_GET(&p_port->fifo, p_buffer);
556 if (p_buffer->nFlags & SENTINEL_FLAG) {
560 msg_Warn( p_dec, "Stray buffer left in fifo, %p", p_buffer );
564 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
565 CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
569 for(i = 0; i < p_sys->ports; i++)
571 OmxPort *p_port = &p_sys->p_ports[i];
572 free(p_port->pp_buffers);
573 p_port->pp_buffers = 0;
575 omx_error = pf_free_handle( omx_handle );
579 /*****************************************************************************
580 * InitialiseComponent: Load and initialise an OMX component
581 *****************************************************************************/
582 static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
583 OMX_STRING psz_component, OMX_HANDLETYPE *p_handle)
585 static OMX_CALLBACKTYPE callbacks =
586 { OmxEventHandler, OmxEmptyBufferDone, OmxFillBufferDone };
587 decoder_sys_t *p_sys = p_dec->p_sys;
588 OMX_HANDLETYPE omx_handle;
589 OMX_ERRORTYPE omx_error;
591 OMX_U8 psz_role[OMX_MAX_STRINGNAME_SIZE];
592 OMX_PARAM_COMPONENTROLETYPE role;
593 OMX_PARAM_PORTDEFINITIONTYPE definition;
594 OMX_PORT_PARAM_TYPE param;
597 omx_error = pf_get_handle( &omx_handle, psz_component, p_dec, &callbacks );
598 if(omx_error != OMX_ErrorNone)
600 msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
601 omx_error, ErrorToString(omx_error) );
604 strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
606 omx_error = OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
607 if(omx_error == OMX_ErrorNone)
608 msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
610 msg_Dbg(p_dec, "loaded component %s", psz_component);
611 PrintOmx(p_dec, omx_handle, OMX_ALL);
613 /* Set component role */
614 OMX_INIT_STRUCTURE(role);
615 strcpy((char*)role.cRole,
616 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec : p_dec->fmt_in.i_codec,
617 p_dec->fmt_in.i_cat, p_sys->b_enc));
619 omx_error = OMX_SetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
621 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
623 if(omx_error == OMX_ErrorNone)
624 msg_Dbg(p_dec, "component standard role set to %s", role.cRole);
626 /* Find the input / output ports */
627 OMX_INIT_STRUCTURE(param);
628 OMX_INIT_STRUCTURE(definition);
629 omx_error = OMX_GetParameter(omx_handle, p_dec->fmt_in.i_cat == VIDEO_ES ?
630 OMX_IndexParamVideoInit : OMX_IndexParamAudioInit, ¶m);
631 if(omx_error != OMX_ErrorNone) {
634 param.nStartPortNumber = 0;
640 for(i = 0; i < param.nPorts; i++)
644 /* Get port definition */
645 definition.nPortIndex = param.nStartPortNumber + i;
646 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
648 if(omx_error != OMX_ErrorNone) continue;
650 if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
651 else p_port = &p_sys->out;
653 p_port->b_valid = true;
654 p_port->i_port_index = definition.nPortIndex;
655 p_port->definition = definition;
656 p_port->omx_handle = omx_handle;
659 if(!p_sys->in.b_valid || !p_sys->out.b_valid)
661 omx_error = OMX_ErrorInvalidComponent;
662 CHECK_ERROR(omx_error, "couldn't find an input and output port");
665 if(!strncmp(p_sys->psz_component, "OMX.SEC.", 8) &&
666 p_dec->fmt_in.i_cat == VIDEO_ES)
669 omx_error = OMX_GetExtensionIndex(omx_handle, (OMX_STRING) "OMX.SEC.index.ThumbnailMode", &index);
670 if(omx_error == OMX_ErrorNone)
672 OMX_BOOL enable = OMX_TRUE;
673 omx_error = OMX_SetConfig(omx_handle, index, &enable);
674 CHECK_ERROR(omx_error, "Unable to set ThumbnailMode");
676 OMX_BOOL enable = OMX_TRUE;
677 /* Needed on Samsung Galaxy S II */
678 omx_error = OMX_SetConfig(omx_handle, OMX_IndexVendorSetYUV420pMode, &enable);
679 if (omx_error == OMX_ErrorNone)
680 msg_Dbg(p_dec, "Set OMX_IndexVendorSetYUV420pMode successfully");
682 msg_Dbg(p_dec, "Unable to set OMX_IndexVendorSetYUV420pMode: %x", omx_error);
686 /* Set port definitions */
687 for(i = 0; i < p_sys->ports; i++)
689 omx_error = SetPortDefinition(p_dec, &p_sys->p_ports[i],
690 p_sys->p_ports[i].p_fmt);
691 if(omx_error != OMX_ErrorNone) goto error;
694 /* Allocate our array for the omx buffers and enable ports */
695 for(i = 0; i < p_sys->ports; i++)
697 OmxPort *p_port = &p_sys->p_ports[i];
700 malloc(p_port->definition.nBufferCountActual *
701 sizeof(OMX_BUFFERHEADERTYPE*));
702 if(!p_port->pp_buffers)
704 omx_error = OMX_ErrorInsufficientResources;
705 CHECK_ERROR(omx_error, "memory allocation failed");
707 p_port->i_buffers = p_port->definition.nBufferCountActual;
710 if(!p_port->definition.bEnabled)
712 omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
713 p_port->i_port_index, NULL);
714 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
715 (int)p_port->i_port_index, omx_error );
716 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
717 CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
718 (int)p_port->i_port_index, omx_error );
722 *p_handle = omx_handle;
723 return OMX_ErrorNone;
726 DeinitialiseComponent(p_dec, omx_handle);
731 /*****************************************************************************
732 * OpenDecoder: Create the decoder instance
733 *****************************************************************************/
734 static int OpenDecoder( vlc_object_t *p_this )
736 decoder_t *p_dec = (decoder_t*)p_this;
739 if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
742 status = OpenGeneric( p_this, false );
743 if(status != VLC_SUCCESS) return status;
745 p_dec->pf_decode_video = DecodeVideo;
746 p_dec->pf_decode_audio = DecodeAudio;
751 /*****************************************************************************
752 * OpenEncoder: Create the encoder instance
753 *****************************************************************************/
754 static int OpenEncoder( vlc_object_t *p_this )
756 encoder_t *p_enc = (encoder_t*)p_this;
759 if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
762 status = OpenGeneric( p_this, true );
763 if(status != VLC_SUCCESS) return status;
765 p_enc->pf_encode_video = EncodeVideo;
770 /*****************************************************************************
771 * OpenGeneric: Create the generic decoder/encoder instance
772 *****************************************************************************/
773 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
775 decoder_t *p_dec = (decoder_t*)p_this;
776 decoder_sys_t *p_sys;
777 OMX_ERRORTYPE omx_error;
778 OMX_BUFFERHEADERTYPE *p_header;
781 if (InitOmxCore(p_this) != VLC_SUCCESS) {
785 /* Allocate the memory needed to store the decoder's structure */
786 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
792 /* Initialise the thread properties */
795 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
796 p_dec->fmt_out.video = p_dec->fmt_in.video;
797 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
798 p_dec->fmt_out.i_codec = 0;
800 p_sys->b_enc = b_encode;
801 InitOmxEventQueue(&p_sys->event_queue);
802 vlc_mutex_init (&p_sys->in.fifo.lock);
803 vlc_cond_init (&p_sys->in.fifo.wait);
804 p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
805 p_sys->in.fifo.pp_last = &p_sys->in.fifo.p_first;
806 p_sys->in.b_direct = false;
807 p_sys->in.b_flushed = true;
808 p_sys->in.p_fmt = &p_dec->fmt_in;
809 vlc_mutex_init (&p_sys->out.fifo.lock);
810 vlc_cond_init (&p_sys->out.fifo.wait);
811 p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
812 p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
813 p_sys->out.b_direct = false;
814 p_sys->out.b_flushed = true;
815 p_sys->out.p_fmt = &p_dec->fmt_out;
817 p_sys->p_ports = &p_sys->in;
818 p_sys->b_use_pts = 0;
820 msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
821 (char *)&p_dec->fmt_out.i_codec);
823 /* Enumerate components and build a list of the one we want to try */
825 CreateComponentsList(p_this,
826 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
827 p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
828 p_sys->b_enc), p_sys->ppsz_components);
829 if( !p_sys->components )
831 msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
832 (char *)&p_dec->fmt_in.i_codec );
833 CloseGeneric(p_this);
837 /* Try to load and initialise a component */
838 omx_error = OMX_ErrorUndefined;
839 for(i = 0; i < p_sys->components; i++)
842 /* ignore OpenCore software codecs */
843 if (!strncmp(p_sys->ppsz_components[i], "OMX.PV.", 7))
845 /* The same sw codecs, renamed in ICS (perhaps also in honeycomb) */
846 if (!strncmp(p_sys->ppsz_components[i], "OMX.google.", 11))
848 /* This one has been seen on HTC One V - it behaves like it works,
849 * but FillBufferDone returns buffers filled with 0 bytes. The One V
850 * has got a working OMX.qcom.video.decoder.avc instead though. */
851 if (!strncmp(p_sys->ppsz_components[i], "OMX.ARICENT.", 12))
853 /* Codecs with DRM, that don't output plain YUV data but only
854 * support direct rendering where the output can't be intercepted. */
855 if (strstr(p_sys->ppsz_components[i], ".secure"))
857 /* Use VC1 decoder for WMV3 for now */
858 if (!strcmp(p_sys->ppsz_components[i], "OMX.SEC.WMV.Decoder"))
860 /* This decoder does work, but has an insane latency (leading to errors
861 * about "main audio output playback way too late" and dropped frames).
862 * At least Samsung Galaxy S III (where this decoder is present) has
863 * got another one, OMX.SEC.mp3.dec, that works well and has a
864 * sensible latency. (Also, even if that one isn't found, in general,
865 * using SW codecs is usually more than fast enough for MP3.) */
866 if (!strcmp(p_sys->ppsz_components[i], "OMX.SEC.MP3.Decoder"))
868 /* This codec should be able to handle both VC1 and WMV3, but
869 * for VC1 it doesn't output any buffers at all (in the way we use
870 * it) and for WMV3 it outputs plain black buffers. Thus ignore
871 * it until we can make it work properly. */
872 if (!strcmp(p_sys->ppsz_components[i], "OMX.Nvidia.vc1.decode"))
875 omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
877 if(omx_error == OMX_ErrorNone) break;
879 CHECK_ERROR(omx_error, "no component could be initialised" );
881 /* Move component to Idle then Executing state */
882 OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
883 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
885 /* Allocate omx buffers */
886 for(i = 0; i < p_sys->ports; i++)
888 OmxPort *p_port = &p_sys->p_ports[i];
890 for(j = 0; j < p_port->i_buffers; j++)
893 #define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
894 char *p_buf = malloc(p_port->definition.nBufferSize +
895 p_port->definition.nBufferAlignment);
896 p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
901 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
902 p_port->i_port_index, 0,
903 p_port->definition.nBufferSize, (void*)1);
906 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
907 p_port->i_port_index, 0,
908 p_port->definition.nBufferSize);
910 if(omx_error != OMX_ErrorNone) break;
911 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
913 p_port->i_buffers = j;
914 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
915 omx_error, (int)p_port->i_port_index, j );
918 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
919 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
921 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
922 OMX_StateExecuting, 0);
923 CHECK_ERROR(omx_error, "OMX_CommandStateSet Executing failed (%x)", omx_error );
924 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
925 CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
927 /* Send codec configuration data */
928 if( p_dec->fmt_in.i_extra )
930 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
931 p_header->nFilledLen = p_dec->fmt_in.i_extra;
933 /* Convert H.264 NAL format to annex b */
934 if( p_sys->i_nal_size_length && !p_sys->in.b_direct )
936 p_header->nFilledLen = 0;
937 convert_sps_pps( p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
938 p_header->pBuffer, p_header->nAllocLen,
939 (uint32_t*) &p_header->nFilledLen, NULL );
941 else if(p_sys->in.b_direct)
943 p_header->pOutputPortPrivate = p_header->pBuffer;
944 p_header->pBuffer = p_dec->fmt_in.p_extra;
946 else if (p_dec->fmt_in.i_codec == VLC_CODEC_WMV3 &&
947 p_dec->fmt_in.i_extra >= 4 &&
948 p_header->nAllocLen >= 36)
951 // According to OMX IL 1.2.0 spec (4.3.33.2), the codec config
952 // data for VC-1 Main/Simple (aka WMV3) is according to table 265
953 // in the VC-1 spec. Most of the fields are just set with placeholders
954 // (like framerate, hrd_buffer/rate).
955 static const uint8_t wmv3seq[] = {
956 0xff, 0xff, 0xff, 0xc5, // numframes=ffffff, marker byte
957 0x04, 0x00, 0x00, 0x00, // marker byte
958 0x00, 0x00, 0x00, 0x00, // struct C, almost equal to p_extra
959 0x00, 0x00, 0x00, 0x00, // struct A, vert size
960 0x00, 0x00, 0x00, 0x00, // struct A, horiz size
961 0x0c, 0x00, 0x00, 0x00, // marker byte
962 0xff, 0xff, 0x00, 0x80, // struct B, level=4, cbr=0, hrd_buffer=ffff
963 0xff, 0xff, 0x00, 0x00, // struct B, hrd_rate=ffff
964 0xff, 0xff, 0xff, 0xff, // struct B, framerate=ffffffff
966 p_header->nFilledLen = sizeof(wmv3seq);
967 memcpy(p_header->pBuffer, wmv3seq, p_header->nFilledLen);
968 // Struct C - almost equal to the extradata
969 memcpy(&p_header->pBuffer[8], p_dec->fmt_in.p_extra, 4);
970 // Expand profile from the highest 2 bits to the highest 4 bits
971 profile = p_header->pBuffer[8] >> 6;
972 p_header->pBuffer[8] = (p_header->pBuffer[8] & 0x0f) | (profile << 4);
973 // Fill in the height/width for struct A
974 SetDWLE(&p_header->pBuffer[12], p_dec->fmt_in.video.i_height);
975 SetDWLE(&p_header->pBuffer[16], p_dec->fmt_in.video.i_width);
979 if(p_header->nFilledLen > p_header->nAllocLen)
981 msg_Dbg(p_dec, "buffer too small (%i,%i)", (int)p_header->nFilledLen,
982 (int)p_header->nAllocLen);
983 p_header->nFilledLen = p_header->nAllocLen;
985 memcpy(p_header->pBuffer, p_dec->fmt_in.p_extra, p_header->nFilledLen);
988 p_header->nOffset = 0;
989 p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG | OMX_BUFFERFLAG_ENDOFFRAME;
990 msg_Dbg(p_dec, "sending codec config data %p, %p, %i", p_header,
991 p_header->pBuffer, (int)p_header->nFilledLen);
992 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
995 /* Get back output port definition */
996 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
997 if(omx_error != OMX_ErrorNone) goto error;
999 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1000 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1002 if(p_sys->b_error) goto error;
1004 p_dec->b_need_packetized = true;
1005 if (!strcmp(p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER"))
1006 p_sys->b_use_pts = 1;
1008 if (!strcmp(p_sys->psz_component, "OMX.STM.Video.Decoder"))
1009 p_sys->b_use_pts = 1;
1011 if (p_sys->b_use_pts)
1012 msg_Dbg( p_dec, "using pts timestamp mode for %s", p_sys->psz_component);
1017 CloseGeneric(p_this);
1018 return VLC_EGENERIC;
1021 /*****************************************************************************
1023 *****************************************************************************/
1024 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
1026 decoder_sys_t *p_sys = p_dec->p_sys;
1027 OMX_PARAM_PORTDEFINITIONTYPE definition;
1028 OMX_BUFFERHEADERTYPE *p_buffer;
1029 OMX_ERRORTYPE omx_error;
1032 /* Sanity checking */
1033 OMX_INIT_STRUCTURE(definition);
1034 definition.nPortIndex = p_port->i_port_index;
1035 omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1037 if(omx_error != OMX_ErrorNone || (p_dec->fmt_in.i_cat == VIDEO_ES &&
1038 (!definition.format.video.nFrameWidth ||
1039 !definition.format.video.nFrameHeight)) )
1040 return OMX_ErrorUndefined;
1042 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
1043 p_port->i_port_index, NULL);
1044 CHECK_ERROR(omx_error, "OMX_CommandPortDisable on %i failed (%x)",
1045 (int)p_port->i_port_index, omx_error );
1047 for(i = 0; i < p_port->i_buffers; i++)
1049 OMX_FIFO_GET(&p_port->fifo, p_buffer);
1050 if (p_buffer->nFlags & SENTINEL_FLAG) {
1055 omx_error = OMX_FreeBuffer( p_sys->omx_handle,
1056 p_port->i_port_index, p_buffer );
1058 if(omx_error != OMX_ErrorNone) break;
1060 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
1061 omx_error, (int)p_port->i_port_index, i );
1063 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
1064 CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
1066 /* Get the new port definition */
1067 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1068 if(omx_error != OMX_ErrorNone) goto error;
1070 if( p_dec->fmt_in.i_cat != AUDIO_ES )
1072 /* Don't explicitly set the new parameters that we got with
1073 * OMX_GetParameter above when using audio codecs.
1074 * That struct hasn't been changed since, so there should be
1075 * no need to set it here, unless some codec expects the
1076 * SetParameter call as a trigger event for some part of
1077 * the reconfiguration.
1078 * This fixes using audio decoders on Samsung Galaxy S II,
1080 * Only skipping this for audio codecs, to minimize the
1081 * change for current working configurations for video.
1083 omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1085 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
1086 omx_error, ErrorToString(omx_error));
1089 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
1090 p_port->i_port_index, NULL);
1091 CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
1092 (int)p_port->i_port_index, omx_error );
1094 if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
1095 free(p_port->pp_buffers);
1096 p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
1097 if(!p_port->pp_buffers)
1099 omx_error = OMX_ErrorInsufficientResources;
1100 CHECK_ERROR(omx_error, "memory allocation failed");
1103 p_port->i_buffers = p_port->definition.nBufferCountActual;
1104 for(i = 0; i < p_port->i_buffers; i++)
1106 if(p_port->b_direct)
1108 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1109 p_port->i_port_index, 0,
1110 p_port->definition.nBufferSize, (void*)1);
1113 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1114 p_port->i_port_index, 0,
1115 p_port->definition.nBufferSize);
1117 if(omx_error != OMX_ErrorNone) break;
1118 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1120 p_port->i_buffers = i;
1121 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1122 omx_error, (int)p_port->i_port_index, i );
1124 omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
1125 CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1127 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1128 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1134 /*****************************************************************************
1135 * DecodeVideo: Called to decode one frame
1136 *****************************************************************************/
1137 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1139 decoder_sys_t *p_sys = p_dec->p_sys;
1140 picture_t *p_pic = NULL, *p_next_pic;
1141 OMX_ERRORTYPE omx_error;
1144 OMX_BUFFERHEADERTYPE *p_header;
1146 int i_input_used = 0;
1147 struct H264ConvertState convert_state = { 0, 0 };
1149 if( !pp_block || !*pp_block )
1152 p_block = *pp_block;
1154 /* Check for errors from codec */
1157 msg_Dbg(p_dec, "error during decoding");
1158 block_Release( p_block );
1162 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1164 block_Release( p_block );
1165 if(!p_sys->in.b_flushed)
1167 msg_Dbg(p_dec, "flushing");
1168 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1169 p_sys->in.definition.nPortIndex, 0 );
1171 p_sys->in.b_flushed = true;
1175 /* Take care of decoded frames first */
1178 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1179 if(!p_header) break; /* No frame available */
1181 if(p_sys->out.b_update_def)
1183 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1184 p_sys->out.b_update_def = 0;
1187 if(p_header->nFilledLen)
1189 p_pic = p_header->pAppPrivate;
1192 /* We're not in direct rendering mode.
1193 * Get a new picture and copy the content */
1194 p_pic = decoder_NewPicture( p_dec );
1197 CopyOmxPicture(p_sys->out.definition.format.video.eColorFormat,
1198 p_pic, p_sys->out.definition.format.video.nSliceHeight,
1199 p_sys->out.i_frame_stride,
1200 p_header->pBuffer + p_header->nOffset,
1201 p_sys->out.i_frame_stride_chroma_div);
1205 p_pic->date = FromOmxTicks(p_header->nTimeStamp);
1206 p_header->nFilledLen = 0;
1207 p_header->pAppPrivate = 0;
1210 /* Get a new picture */
1211 if(p_sys->out.b_direct && !p_header->pAppPrivate)
1213 p_next_pic = decoder_NewPicture( p_dec );
1214 if(!p_next_pic) break;
1216 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1217 p_header->pAppPrivate = p_next_pic;
1218 p_header->pInputPortPrivate = p_header->pBuffer;
1219 p_header->pBuffer = p_next_pic->p[0].p_pixels;
1223 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1226 #ifdef OMXIL_EXTRA_DEBUG
1227 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1229 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1233 /* Send the input buffer to the component */
1234 OMX_FIFO_GET_TIMEOUT(&p_sys->in.fifo, p_header, 200000);
1236 if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1243 bool decode_more = false;
1244 p_header->nFilledLen = p_block->i_buffer - i_input_used;
1245 p_header->nOffset = 0;
1246 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1247 if (p_sys->b_use_pts && p_block->i_pts)
1248 p_header->nTimeStamp = ToOmxTicks(p_block->i_pts);
1250 p_header->nTimeStamp = ToOmxTicks(p_block->i_dts);
1252 /* In direct mode we pass the input pointer as is.
1253 * Otherwise we memcopy the data */
1254 if(p_sys->in.b_direct)
1256 p_header->pOutputPortPrivate = p_header->pBuffer;
1257 p_header->pBuffer = p_block->p_buffer;
1258 p_header->pAppPrivate = p_block;
1259 i_input_used = p_header->nFilledLen;
1263 if(p_header->nFilledLen > p_header->nAllocLen)
1265 p_header->nFilledLen = p_header->nAllocLen;
1267 memcpy(p_header->pBuffer, p_block->p_buffer + i_input_used, p_header->nFilledLen);
1268 i_input_used += p_header->nFilledLen;
1269 if (i_input_used == p_block->i_buffer)
1271 block_Release(p_block);
1276 p_header->nFlags &= ~OMX_BUFFERFLAG_ENDOFFRAME;
1280 /* Convert H.264 NAL format to annex b. Doesn't do anything if
1281 * i_nal_size_length == 0, which is the case for codecs other
1283 convert_h264_to_annexb( p_header->pBuffer, p_header->nFilledLen,
1284 p_sys->i_nal_size_length, &convert_state );
1285 #ifdef OMXIL_EXTRA_DEBUG
1286 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1287 (int)p_header->nFilledLen );
1289 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1290 p_sys->in.b_flushed = false;
1294 *pp_block = NULL; /* Avoid being fed the same packet again */
1298 /* Handle the PortSettingsChanged events */
1299 for(i = 0; i < p_sys->ports; i++)
1301 OmxPort *p_port = &p_sys->p_ports[i];
1302 if(p_port->b_reconfigure)
1304 omx_error = PortReconfigure(p_dec, p_port);
1305 p_port->b_reconfigure = 0;
1307 if(p_port->b_update_def)
1309 omx_error = GetPortDefinition(p_dec, p_port, p_port->p_fmt);
1310 p_port->b_update_def = 0;
1317 /*****************************************************************************
1318 * DecodeAudio: Called to decode one frame
1319 *****************************************************************************/
1320 block_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1322 decoder_sys_t *p_sys = p_dec->p_sys;
1323 block_t *p_buffer = NULL;
1324 OMX_BUFFERHEADERTYPE *p_header;
1325 OMX_ERRORTYPE omx_error;
1329 if( !pp_block || !*pp_block ) return NULL;
1331 p_block = *pp_block;
1333 /* Check for errors from codec */
1336 msg_Dbg(p_dec, "error during decoding");
1337 block_Release( p_block );
1341 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1343 block_Release( p_block );
1344 date_Set( &p_sys->end_date, 0 );
1345 if(!p_sys->in.b_flushed)
1347 msg_Dbg(p_dec, "flushing");
1348 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1349 p_sys->in.definition.nPortIndex, 0 );
1351 p_sys->in.b_flushed = true;
1355 if( !date_Get( &p_sys->end_date ) )
1357 if( !p_block->i_pts )
1359 /* We've just started the stream, wait for the first PTS. */
1360 block_Release( p_block );
1363 date_Set( &p_sys->end_date, p_block->i_pts );
1366 /* Take care of decoded frames first */
1369 unsigned int i_samples = 0;
1371 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1372 if(!p_header) break; /* No frame available */
1374 if (p_sys->out.p_fmt->audio.i_channels)
1375 i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1378 p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1379 if( !p_buffer ) break; /* No audio buffer available */
1381 memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1382 p_header->nFilledLen = 0;
1384 int64_t timestamp = FromOmxTicks(p_header->nTimeStamp);
1385 if( timestamp != 0 &&
1386 timestamp != date_Get( &p_sys->end_date ) )
1387 date_Set( &p_sys->end_date, timestamp );
1389 p_buffer->i_pts = date_Get( &p_sys->end_date );
1390 p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1394 #ifdef OMXIL_EXTRA_DEBUG
1395 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1397 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1398 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1402 /* Send the input buffer to the component */
1403 OMX_FIFO_GET_TIMEOUT(&p_sys->in.fifo, p_header, 200000);
1405 if (p_header && p_header->nFlags & SENTINEL_FLAG) {
1412 p_header->nFilledLen = p_block->i_buffer;
1413 p_header->nOffset = 0;
1414 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1415 p_header->nTimeStamp = ToOmxTicks(p_block->i_dts);
1417 /* In direct mode we pass the input pointer as is.
1418 * Otherwise we memcopy the data */
1419 if(p_sys->in.b_direct)
1421 p_header->pOutputPortPrivate = p_header->pBuffer;
1422 p_header->pBuffer = p_block->p_buffer;
1423 p_header->pAppPrivate = p_block;
1427 if(p_header->nFilledLen > p_header->nAllocLen)
1429 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1430 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1431 p_header->nFilledLen = p_header->nAllocLen;
1433 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1434 block_Release(p_block);
1437 #ifdef OMXIL_EXTRA_DEBUG
1438 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1439 (int)p_header->nFilledLen );
1441 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1442 p_sys->in.b_flushed = false;
1443 *pp_block = NULL; /* Avoid being fed the same packet again */
1447 /* Handle the PortSettingsChanged events */
1448 for(i = 0; i < p_sys->ports; i++)
1450 OmxPort *p_port = &p_sys->p_ports[i];
1451 if(!p_port->b_reconfigure) continue;
1452 p_port->b_reconfigure = 0;
1453 omx_error = PortReconfigure(p_dec, p_port);
1459 /*****************************************************************************
1460 * EncodeVideo: Called to encode one frame
1461 *****************************************************************************/
1462 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1464 decoder_t *p_dec = ( decoder_t *)p_enc;
1465 decoder_sys_t *p_sys = p_dec->p_sys;
1466 OMX_ERRORTYPE omx_error;
1469 OMX_BUFFERHEADERTYPE *p_header;
1470 block_t *p_block = 0;
1472 if( !p_pic ) return NULL;
1474 /* Check for errors from codec */
1477 msg_Dbg(p_dec, "error during encoding");
1481 /* Send the input buffer to the component */
1482 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1485 /* In direct mode we pass the input pointer as is.
1486 * Otherwise we memcopy the data */
1487 if(p_sys->in.b_direct)
1489 p_header->pOutputPortPrivate = p_header->pBuffer;
1490 p_header->pBuffer = p_pic->p[0].p_pixels;
1494 CopyVlcPicture(p_dec, p_header, p_pic);
1497 p_header->nFilledLen = p_sys->in.i_frame_size;
1498 p_header->nOffset = 0;
1499 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1500 p_header->nTimeStamp = ToOmxTicks(p_pic->date);
1501 #ifdef OMXIL_EXTRA_DEBUG
1502 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1503 (int)p_header->nFilledLen );
1505 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1506 p_sys->in.b_flushed = false;
1509 /* Handle the PortSettingsChanged events */
1510 for(i = 0; i < p_sys->ports; i++)
1512 OmxPort *p_port = &p_sys->p_ports[i];
1513 if(!p_port->b_reconfigure) continue;
1514 p_port->b_reconfigure = 0;
1515 omx_error = PortReconfigure(p_dec, p_port);
1518 /* Wait for the decoded frame */
1521 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1523 if(p_header->nFilledLen)
1525 if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1527 /* TODO: need to store codec config */
1528 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1531 p_block = p_header->pAppPrivate;
1534 /* We're not in direct rendering mode.
1535 * Get a new block and copy the content */
1536 p_block = block_Alloc( p_header->nFilledLen );
1537 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1540 p_block->i_buffer = p_header->nFilledLen;
1541 p_block->i_pts = p_block->i_dts = FromOmxTicks(p_header->nTimeStamp);
1542 p_header->nFilledLen = 0;
1543 p_header->pAppPrivate = 0;
1546 #ifdef OMXIL_EXTRA_DEBUG
1547 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1549 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1552 msg_Dbg(p_dec, "done");
1556 /*****************************************************************************
1557 * CloseGeneric: omxil decoder destruction
1558 *****************************************************************************/
1559 static void CloseGeneric( vlc_object_t *p_this )
1561 decoder_t *p_dec = (decoder_t *)p_this;
1562 decoder_sys_t *p_sys = p_dec->p_sys;
1564 if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1568 DeinitOmxEventQueue(&p_sys->event_queue);
1569 vlc_mutex_destroy (&p_sys->in.fifo.lock);
1570 vlc_cond_destroy (&p_sys->in.fifo.wait);
1571 vlc_mutex_destroy (&p_sys->out.fifo.lock);
1572 vlc_cond_destroy (&p_sys->out.fifo.wait);
1577 /*****************************************************************************
1579 *****************************************************************************/
1580 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1581 OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1582 OMX_U32 data_2, OMX_PTR event_data )
1584 decoder_t *p_dec = (decoder_t *)app_data;
1585 decoder_sys_t *p_sys = p_dec->p_sys;
1589 PrintOmxEvent((vlc_object_t *) p_dec, event, data_1, data_2, event_data);
1592 case OMX_EventError:
1593 //p_sys->b_error = true;
1596 case OMX_EventPortSettingsChanged:
1597 if( data_2 == 0 || data_2 == OMX_IndexParamPortDefinition )
1599 OMX_BUFFERHEADERTYPE *sentinel;
1600 for(i = 0; i < p_sys->ports; i++)
1601 if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1602 p_sys->p_ports[i].b_reconfigure = true;
1603 sentinel = calloc(1, sizeof(*sentinel));
1605 sentinel->nFlags = SENTINEL_FLAG;
1606 OMX_FIFO_PUT(&p_sys->in.fifo, sentinel);
1609 else if( data_2 == OMX_IndexConfigCommonOutputCrop )
1611 for(i = 0; i < p_sys->ports; i++)
1612 if(p_sys->p_ports[i].definition.nPortIndex == data_1)
1613 p_sys->p_ports[i].b_update_def = true;
1617 msg_Dbg( p_dec, "Unhandled setting change %x", (unsigned int)data_2 );
1625 PostOmxEvent(&p_sys->event_queue, event, data_1, data_2, event_data);
1626 return OMX_ErrorNone;
1629 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1630 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1632 decoder_t *p_dec = (decoder_t *)app_data;
1633 decoder_sys_t *p_sys = p_dec->p_sys;
1636 #ifdef OMXIL_EXTRA_DEBUG
1637 msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1640 if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1642 block_t *p_block = (block_t *)omx_header->pAppPrivate;
1643 omx_header->pBuffer = omx_header->pOutputPortPrivate;
1644 if(p_block) block_Release(p_block);
1645 omx_header->pAppPrivate = 0;
1647 OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1649 return OMX_ErrorNone;
1652 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1653 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1655 decoder_t *p_dec = (decoder_t *)app_data;
1656 decoder_sys_t *p_sys = p_dec->p_sys;
1659 #ifdef OMXIL_EXTRA_DEBUG
1660 msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
1661 (int)omx_header->nFilledLen );
1664 if(omx_header->pInputPortPrivate)
1666 omx_header->pBuffer = omx_header->pInputPortPrivate;
1668 OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1670 return OMX_ErrorNone;