1 /*****************************************************************************
2 * omxil.c: Video decoder module making use of OpenMAX IL components.
3 *****************************************************************************
4 * Copyright (C) 2010 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #if defined(HAVE_DL_DLOPEN)
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_codec.h>
39 #include <vlc_block_helper.h>
44 //#define OMXIL_EXTRA_DEBUG
46 /*****************************************************************************
47 * List of OpenMAX IL core we will try in order
48 *****************************************************************************/
49 static const char *ppsz_dll_list[] =
51 "libOMX_Core.so", /* TI OMAP IL core */
52 "libomxil-bellagio.so", /* Bellagio IL core */
56 /*****************************************************************************
58 *****************************************************************************/
59 #if defined(HAVE_DL_DLOPEN)
60 # define dll_open(name) dlopen( name, RTLD_NOW )
61 # define dll_close(handle) dlclose(handle)
63 # define dll_open(name) (NULL)
64 # define dll_close(handle) (void)0
67 /*****************************************************************************
69 *****************************************************************************/
70 static int OpenDecoder( vlc_object_t * );
71 static int OpenEncoder( vlc_object_t * );
72 static int OpenGeneric( vlc_object_t *, bool b_encode );
73 static void CloseGeneric( vlc_object_t * );
75 static picture_t *DecodeVideo( decoder_t *, block_t ** );
76 static aout_buffer_t *DecodeAudio ( decoder_t *, block_t ** );
77 static block_t *EncodeVideo( encoder_t *, picture_t * );
79 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE, OMX_PTR, OMX_EVENTTYPE,
80 OMX_U32, OMX_U32, OMX_PTR );
81 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE, OMX_PTR,
82 OMX_BUFFERHEADERTYPE * );
83 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE, OMX_PTR,
84 OMX_BUFFERHEADERTYPE * );
86 /*****************************************************************************
88 *****************************************************************************/
90 set_description( N_("Audio/Video decoder (using OpenMAX IL)") )
91 set_category( CAT_INPUT )
92 set_subcategory( SUBCAT_INPUT_SCODEC )
93 set_section( N_("Decoding") , NULL )
95 set_capability( "decoder", 80 )
97 set_capability( "decoder", 0 )
99 set_callbacks( OpenDecoder, CloseGeneric )
102 set_section( N_("Encoding") , NULL )
103 set_description( N_("Video encoder (using OpenMAX IL)") )
104 set_capability( "encoder", 0 )
105 set_callbacks( OpenEncoder, CloseGeneric )
108 /*****************************************************************************
109 * CreateComponentsList: creates a list of components matching the given role
110 *****************************************************************************/
111 static int CreateComponentsList(decoder_t *p_dec, const char *psz_role)
113 decoder_sys_t *p_sys = p_dec->p_sys;
114 char psz_name[OMX_MAX_STRINGNAME_SIZE];
115 OMX_ERRORTYPE omx_error;
117 OMX_U8 **ppsz_roles = 0;
118 unsigned int i, j, len;
120 if(!psz_role) goto end;
121 len = strlen(psz_role);
125 bool b_found = false;
127 omx_error = p_sys->pf_component_enum(psz_name, OMX_MAX_STRINGNAME_SIZE, i);
128 if(omx_error != OMX_ErrorNone) break;
130 msg_Dbg(p_dec, "component %s", psz_name);
132 omx_error = p_sys->pf_get_roles_of_component(psz_name, &roles, 0);
133 if(omx_error != OMX_ErrorNone || !roles) continue;
135 ppsz_roles = malloc(roles * (sizeof(OMX_U8*) + OMX_MAX_STRINGNAME_SIZE));
136 if(!ppsz_roles) continue;
138 for( j = 0; j < roles; j++ )
139 ppsz_roles[j] = ((OMX_U8 *)(&ppsz_roles[roles])) +
140 j * OMX_MAX_STRINGNAME_SIZE;
142 omx_error = p_sys->pf_get_roles_of_component(psz_name, &roles, ppsz_roles);
143 if(omx_error != OMX_ErrorNone) roles = 0;
145 for(j = 0; j < roles; j++)
147 msg_Dbg(p_dec, " - role: %s", ppsz_roles[j]);
148 if(!strncmp((char *)ppsz_roles[j], psz_role, len)) b_found = true;
153 if(!b_found) continue;
155 if(p_sys->components >= MAX_COMPONENTS_LIST_SIZE)
157 msg_Dbg(p_dec, "too many matching components");
161 strncpy(p_sys->ppsz_components[p_sys->components], psz_name,
162 OMX_MAX_STRINGNAME_SIZE-1);
167 msg_Dbg(p_dec, "found %i matching components for role %s",
168 p_sys->components, psz_role);
169 for( i = 0; i < p_sys->components; i++ )
170 msg_Dbg(p_dec, "- %s", p_sys->ppsz_components[i]);
172 return p_sys->components;
175 /*****************************************************************************
176 * ImplementationSpecificWorkarounds: place-holder for implementation
177 * specific workarounds
178 *****************************************************************************/
179 static OMX_ERRORTYPE ImplementationSpecificWorkarounds(decoder_t *p_dec,
180 OmxPort *p_port, es_format_t *p_fmt)
182 decoder_sys_t *p_sys = p_dec->p_sys;
183 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
184 int i_profile = 0xFFFF, i_level = 0xFFFF;
186 /* Try to find out the profile of the video */
187 while(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
188 p_fmt->i_codec == VLC_CODEC_H264)
190 uint8_t *p = (uint8_t*)p_dec->fmt_in.p_extra;
191 if(!p || !p_dec->fmt_in.p_extra) break;
193 /* Check the profile / level */
194 if(p_dec->fmt_in.i_original_fourcc == VLC_FOURCC('a','v','c','1') &&
197 if(p_dec->fmt_in.i_extra < 12) break;
198 p_sys->i_nal_size_length = 1 + (p[4]&0x03);
199 if( !(p[5]&0x1f) ) break;
204 if(p_dec->fmt_in.i_extra < 8) break;
205 if(!p[0] && !p[1] && !p[2] && p[3] == 1) p += 4;
206 else if(!p[0] && !p[1] && p[2] == 1) p += 3;
210 if( ((*p++)&0x1f) != 7) break;
212 /* Get profile/level out of first SPS */
218 if(!strcmp(p_sys->psz_component, "OMX.TI.Video.Decoder"))
220 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
221 p_fmt->i_codec == VLC_CODEC_H264 &&
222 (i_profile != 66 || i_level > 30))
224 msg_Dbg(p_dec, "h264 profile/level not supported (0x%x, 0x%x)",
226 return OMX_ErrorNotImplemented;
229 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirOutput &&
230 p_fmt->i_codec == VLC_CODEC_I420)
232 /* I420 xvideo is slow on OMAP */
233 def->format.video.eColorFormat = OMX_COLOR_FormatCbYCrY;
234 GetVlcChromaFormat( def->format.video.eColorFormat,
235 &p_fmt->i_codec, 0 );
236 GetVlcChromaSizes( p_fmt->i_codec,
237 def->format.video.nFrameWidth,
238 def->format.video.nFrameHeight,
239 &p_port->i_frame_size, &p_port->i_frame_stride,
240 &p_port->i_frame_stride_chroma_div );
241 def->format.video.nStride = p_port->i_frame_stride;
242 def->nBufferSize = p_port->i_frame_size;
245 else if(!strcmp(p_sys->psz_component, "OMX.st.video_encoder"))
247 if(p_fmt->i_cat == VIDEO_ES)
249 /* Bellagio's encoder doesn't encode the framerate in Q16 */
250 def->format.video.xFramerate >>= 16;
254 return OMX_ErrorNone;
257 /*****************************************************************************
258 * SetPortDefinition: set definition of the omx port based on the vlc format
259 *****************************************************************************/
260 static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
263 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
264 OMX_ERRORTYPE omx_error;
266 omx_error = OMX_GetParameter(p_port->omx_handle,
267 OMX_IndexParamPortDefinition, def);
268 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
269 omx_error, ErrorToString(omx_error));
274 def->format.video.nFrameWidth = p_fmt->video.i_width;
275 def->format.video.nFrameHeight = p_fmt->video.i_height;
276 if(def->format.video.eCompressionFormat == OMX_VIDEO_CodingUnused)
277 def->format.video.nStride = def->format.video.nFrameWidth;
278 if( p_fmt->video.i_frame_rate > 0 &&
279 p_fmt->video.i_frame_rate_base > 0 )
280 def->format.video.xFramerate = (p_fmt->video.i_frame_rate << 16) /
281 p_fmt->video.i_frame_rate_base;
283 if(def->eDir == OMX_DirInput || p_dec->p_sys->b_enc)
285 def->nBufferSize = def->format.video.nFrameWidth *
286 def->format.video.nFrameHeight * 2;
287 p_port->i_frame_size = def->nBufferSize;
289 if(!GetOmxVideoFormat(p_fmt->i_codec,
290 &def->format.video.eCompressionFormat, 0) )
292 if(!GetOmxChromaFormat(p_fmt->i_codec,
293 &def->format.video.eColorFormat, 0) )
295 omx_error = OMX_ErrorNotImplemented;
296 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
297 (char *)&p_fmt->i_codec );
299 GetVlcChromaSizes( p_fmt->i_codec,
300 def->format.video.nFrameWidth,
301 def->format.video.nFrameHeight,
302 &p_port->i_frame_size, &p_port->i_frame_stride,
303 &p_port->i_frame_stride_chroma_div );
304 def->format.video.nStride = p_port->i_frame_stride;
305 def->nBufferSize = p_port->i_frame_size;
310 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
311 &p_fmt->i_codec, 0 ) )
313 omx_error = OMX_ErrorNotImplemented;
314 CHECK_ERROR(omx_error, "OMX color format %i not supported",
315 (int)def->format.video.eColorFormat );
317 GetVlcChromaSizes( p_fmt->i_codec,
318 def->format.video.nFrameWidth,
319 def->format.video.nFrameHeight,
320 &p_port->i_frame_size, &p_port->i_frame_stride,
321 &p_port->i_frame_stride_chroma_div );
322 def->format.video.nStride = p_port->i_frame_stride;
323 def->nBufferSize = p_port->i_frame_size;
328 p_port->i_frame_size = def->nBufferSize;
329 if(def->eDir == OMX_DirInput)
331 if(!GetOmxAudioFormat(p_fmt->i_codec,
332 &def->format.audio.eEncoding, 0) )
334 omx_error = OMX_ErrorNotImplemented;
335 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
336 (char *)&p_fmt->i_codec );
341 if( !GetVlcAudioFormat(def->format.audio.eEncoding,
342 &p_fmt->i_codec, 0 ) )
344 omx_error = OMX_ErrorNotImplemented;
345 CHECK_ERROR(omx_error, "OMX audio encoding %i not supported",
346 (int)def->format.audio.eEncoding );
351 default: return OMX_ErrorNotImplemented;
354 omx_error = ImplementationSpecificWorkarounds(p_dec, p_port, p_fmt);
355 CHECK_ERROR(omx_error, "ImplementationSpecificWorkarounds failed (%x : %s)",
356 omx_error, ErrorToString(omx_error));
358 omx_error = OMX_SetParameter(p_port->omx_handle,
359 OMX_IndexParamPortDefinition, def);
360 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
361 omx_error, ErrorToString(omx_error));
363 omx_error = OMX_GetParameter(p_port->omx_handle,
364 OMX_IndexParamPortDefinition, def);
365 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
366 omx_error, ErrorToString(omx_error));
368 if(p_port->i_frame_size > def->nBufferSize)
369 def->nBufferSize = p_port->i_frame_size;
370 p_port->i_frame_size = def->nBufferSize;
372 /* Deal with audio params */
373 if(p_fmt->i_cat == AUDIO_ES)
375 omx_error = SetAudioParameters(p_port->omx_handle,
376 &p_port->format_param, def->nPortIndex,
377 def->format.audio.eEncoding,
378 p_fmt->audio.i_channels,
381 p_fmt->audio.i_bitspersample,
382 p_fmt->audio.i_blockalign);
383 CHECK_ERROR(omx_error, "SetAudioParameters failed (%x : %s)",
384 omx_error, ErrorToString(omx_error));
391 /*****************************************************************************
392 * GetPortDefinition: set vlc format based on the definition of the omx port
393 *****************************************************************************/
394 static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
397 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
398 OMX_ERRORTYPE omx_error;
400 omx_error = OMX_GetParameter(p_port->omx_handle,
401 OMX_IndexParamPortDefinition, def);
402 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
403 omx_error, ErrorToString(omx_error));
408 p_fmt->video.i_width = def->format.video.nFrameWidth;
409 p_fmt->video.i_visible_width = def->format.video.nFrameWidth;
410 p_fmt->video.i_height = def->format.video.nFrameHeight;
411 p_fmt->video.i_visible_height = def->format.video.nFrameHeight;
412 p_fmt->video.i_frame_rate = p_dec->fmt_in.video.i_frame_rate;
413 p_fmt->video.i_frame_rate_base = p_dec->fmt_in.video.i_frame_rate_base;
415 if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
416 &p_fmt->i_codec, 0 ) )
418 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
419 &p_fmt->i_codec, 0 ) )
421 omx_error = OMX_ErrorNotImplemented;
422 CHECK_ERROR(omx_error, "OMX color format %i not supported",
423 (int)def->format.video.eColorFormat );
425 GetVlcChromaSizes( p_fmt->i_codec,
426 def->format.video.nFrameWidth,
427 def->format.video.nFrameHeight,
428 &p_port->i_frame_size, &p_port->i_frame_stride,
429 &p_port->i_frame_stride_chroma_div );
431 if(p_port->i_frame_size > def->nBufferSize)
432 def->nBufferSize = p_port->i_frame_size;
433 p_port->i_frame_size = def->nBufferSize;
435 if((int)p_port->i_frame_stride > def->format.video.nStride)
436 def->format.video.nStride = p_port->i_frame_stride;
438 p_port->i_frame_stride = def->format.video.nStride;
442 if( !GetVlcAudioFormat( def->format.audio.eEncoding,
443 &p_fmt->i_codec, 0 ) )
445 omx_error = OMX_ErrorNotImplemented;
446 CHECK_ERROR(omx_error, "OMX audio format %i not supported",
447 (int)def->format.audio.eEncoding );
450 omx_error = GetAudioParameters(p_port->omx_handle,
451 &p_port->format_param, def->nPortIndex,
452 def->format.audio.eEncoding,
453 &p_fmt->audio.i_channels,
454 &p_fmt->audio.i_rate,
456 &p_fmt->audio.i_bitspersample,
457 &p_fmt->audio.i_blockalign);
458 CHECK_ERROR(omx_error, "GetAudioParameters failed (%x : %s)",
459 omx_error, ErrorToString(omx_error));
461 if(p_fmt->audio.i_channels < 9)
463 static const int pi_channels_maps[9] =
465 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
466 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
467 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
468 | AOUT_CHAN_REARRIGHT,
469 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
470 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
471 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
472 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
473 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
474 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
475 | AOUT_CHAN_MIDDLERIGHT,
476 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
477 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
480 p_fmt->audio.i_physical_channels =
481 p_fmt->audio.i_original_channels =
482 pi_channels_maps[p_fmt->audio.i_channels];
485 date_Init( &p_dec->p_sys->end_date, p_fmt->audio.i_rate, 1 );
489 default: return OMX_ErrorNotImplemented;
496 /*****************************************************************************
497 * DeinitialiseComponent: Deinitialise and unload an OMX component
498 *****************************************************************************/
499 static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
500 OMX_HANDLETYPE omx_handle)
502 decoder_sys_t *p_sys = p_dec->p_sys;
503 OMX_ERRORTYPE omx_error;
507 if(!omx_handle) return OMX_ErrorNone;
509 omx_error = OMX_GetState(omx_handle, &state);
510 CHECK_ERROR(omx_error, "ONX_GetState failed (%x)", omx_error );
512 if(state == OMX_StateExecuting)
514 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
516 CHECK_ERROR(omx_error, "ONX_CommandStateSet Idle failed (%x)", omx_error );
517 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
518 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
521 omx_error = OMX_GetState(omx_handle, &state);
522 CHECK_ERROR(omx_error, "ONX_GetState failed (%x)", omx_error );
524 if(state == OMX_StateIdle)
526 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
527 OMX_StateLoaded, 0 );
528 CHECK_ERROR(omx_error, "ONX_CommandStateSet Loaded failed (%x)", omx_error );
530 for(i = 0; i < p_sys->ports; i++)
532 OmxPort *p_port = &p_sys->p_ports[i];
533 OMX_BUFFERHEADERTYPE *p_buffer;
535 for(j = 0; j < p_port->i_buffers; j++)
537 OMX_FIFO_GET(&p_port->fifo, p_buffer);
538 omx_error = OMX_FreeBuffer( omx_handle,
539 p_port->i_port_index, p_buffer );
541 if(omx_error != OMX_ErrorNone) break;
543 CHECK_ERROR(omx_error, "ONX_FreeBuffer failed (%x, %i, %i)",
544 omx_error, (int)p_port->i_port_index, j );
547 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
548 CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
552 for(i = 0; i < p_sys->ports; i++)
554 OmxPort *p_port = &p_sys->p_ports[i];
555 free(p_port->pp_buffers);
556 p_port->pp_buffers = 0;
558 omx_error = p_sys->pf_free_handle( omx_handle );
562 /*****************************************************************************
563 * InitialiseComponent: Load and initialise an OMX component
564 *****************************************************************************/
565 static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
566 OMX_STRING psz_component, OMX_HANDLETYPE *p_handle)
568 static OMX_CALLBACKTYPE callbacks =
569 { OmxEventHandler, OmxEmptyBufferDone, OmxFillBufferDone };
570 decoder_sys_t *p_sys = p_dec->p_sys;
571 OMX_HANDLETYPE omx_handle;
572 OMX_ERRORTYPE omx_error;
574 OMX_U8 psz_role[OMX_MAX_STRINGNAME_SIZE];
575 OMX_PARAM_COMPONENTROLETYPE role;
576 OMX_PARAM_PORTDEFINITIONTYPE definition;
577 OMX_PORT_PARAM_TYPE param;
580 omx_error = p_sys->pf_get_handle( &omx_handle, psz_component,
582 if(omx_error != OMX_ErrorNone)
584 msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
585 omx_error, ErrorToString(omx_error) );
588 strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
590 OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
591 msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
592 PrintOmx(p_dec, omx_handle, OMX_ALL);
594 /* Set component role */
595 OMX_INIT_STRUCTURE(role);
596 strcpy((char*)role.cRole,
597 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec : p_dec->fmt_in.i_codec,
598 p_dec->fmt_in.i_cat, p_sys->b_enc));
600 omx_error = OMX_SetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
602 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
604 if(omx_error == OMX_ErrorNone)
605 msg_Dbg(p_dec, "component standard role set to %s", role.cRole);
607 /* Find the input / output ports */
608 OMX_INIT_STRUCTURE(param);
609 OMX_INIT_STRUCTURE(definition);
610 omx_error = OMX_GetParameter(omx_handle, p_dec->fmt_in.i_cat == VIDEO_ES ?
611 OMX_IndexParamVideoInit : OMX_IndexParamAudioInit, ¶m);
612 if(omx_error != OMX_ErrorNone) param.nPorts = 0;
614 for(i = 0; i < param.nPorts; i++)
618 /* Get port definition */
619 definition.nPortIndex = param.nStartPortNumber + i;
620 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
622 if(omx_error != OMX_ErrorNone) continue;
624 if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
625 else p_port = &p_sys->out;
627 p_port->b_valid = true;
628 p_port->i_port_index = definition.nPortIndex;
629 p_port->definition = definition;
630 p_port->omx_handle = omx_handle;
633 if(!p_sys->in.b_valid || !p_sys->out.b_valid)
635 omx_error = OMX_ErrorInvalidComponent;
636 CHECK_ERROR(omx_error, "couldn't find an input and output port");
639 /* Set port definitions */
640 for(i = 0; i < p_sys->ports; i++)
642 omx_error = SetPortDefinition(p_dec, &p_sys->p_ports[i],
643 p_sys->p_ports[i].p_fmt);
644 if(omx_error != OMX_ErrorNone) goto error;
647 /* Allocate our array for the omx buffers and enable ports */
648 for(i = 0; i < p_sys->ports; i++)
650 OmxPort *p_port = &p_sys->p_ports[i];
653 malloc(p_port->definition.nBufferCountActual *
654 sizeof(OMX_BUFFERHEADERTYPE*));
655 if(!p_port->pp_buffers)
657 omx_error = OMX_ErrorInsufficientResources;
658 CHECK_ERROR(omx_error, "memory allocation failed");
660 p_port->i_buffers = p_port->definition.nBufferCountActual;
663 if(!p_port->definition.bEnabled)
665 omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
666 p_port->i_port_index, NULL);
667 CHECK_ERROR(omx_error, "ONX_CommandPortEnable on %i failed (%x)",
668 (int)p_port->i_port_index, omx_error );
669 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
670 CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
671 (int)p_port->i_port_index, omx_error );
675 *p_handle = omx_handle;
676 return OMX_ErrorNone;
679 DeinitialiseComponent(p_dec, omx_handle);
684 /*****************************************************************************
685 * OpenDecoder: Create the decoder instance
686 *****************************************************************************/
687 static int OpenDecoder( vlc_object_t *p_this )
689 decoder_t *p_dec = (decoder_t*)p_this;
692 if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
696 if( p_dec->fmt_in.i_cat != VIDEO_ES && !p_dec->b_force)
700 status = OpenGeneric( p_this, false );
701 if(status != VLC_SUCCESS) return status;
703 p_dec->pf_decode_video = DecodeVideo;
704 p_dec->pf_decode_audio = DecodeAudio;
709 /*****************************************************************************
710 * OpenEncoder: Create the encoder instance
711 *****************************************************************************/
712 static int OpenEncoder( vlc_object_t *p_this )
714 encoder_t *p_enc = (encoder_t*)p_this;
717 if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
720 status = OpenGeneric( p_this, true );
721 if(status != VLC_SUCCESS) return status;
723 p_enc->pf_encode_video = EncodeVideo;
728 /*****************************************************************************
729 * OpenGeneric: Create the generic decoder/encoder instance
730 *****************************************************************************/
731 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
733 void *dll_handle = 0, *pf_init = 0, *pf_deinit = 0;
734 void *pf_get_handle = 0, *pf_free_handle = 0, *pf_component_enum = 0;
735 void *pf_get_roles_of_component = 0;
736 decoder_t *p_dec = (decoder_t*)p_this;
737 decoder_sys_t *p_sys;
738 OMX_ERRORTYPE omx_error;
739 OMX_BUFFERHEADERTYPE *p_header;
742 /* Load the OMX core */
743 for( i = 0; ppsz_dll_list[i]; i++ )
745 dll_handle = dll_open( ppsz_dll_list[i] );
746 if( dll_handle ) break;
748 if( !dll_handle ) return VLC_EGENERIC;
750 pf_init = dlsym( dll_handle, "OMX_Init" );
751 pf_deinit = dlsym( dll_handle, "OMX_Deinit" );
752 pf_get_handle = dlsym( dll_handle, "OMX_GetHandle" );
753 pf_free_handle = dlsym( dll_handle, "OMX_FreeHandle" );
754 pf_component_enum = dlsym( dll_handle, "OMX_ComponentNameEnum" );
755 pf_get_roles_of_component = dlsym( dll_handle, "OMX_GetRolesOfComponent" );
756 if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle ||
757 !pf_component_enum || !pf_get_roles_of_component )
759 msg_Warn( p_this, "cannot find OMX_* symbols in `%s' (%s)",
760 ppsz_dll_list[i], dlerror() );
761 dll_close(dll_handle);
764 if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle || !pf_component_enum )
766 dll_close(dll_handle);
770 /* Allocate the memory needed to store the decoder's structure */
771 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
773 dll_close(dll_handle);
777 /* Initialise the thread properties */
780 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
781 p_dec->fmt_out.video = p_dec->fmt_in.video;
782 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
783 p_dec->fmt_out.i_codec = 0;
785 p_sys->b_enc = b_encode;
786 p_sys->dll_handle = dll_handle;
787 p_sys->pf_init = pf_init;
788 p_sys->pf_deinit = pf_deinit;
789 p_sys->pf_get_handle = pf_get_handle;
790 p_sys->pf_free_handle = pf_free_handle;
791 p_sys->pf_component_enum = pf_component_enum;
792 p_sys->pf_get_roles_of_component = pf_get_roles_of_component;
793 p_sys->pp_last_event = &p_sys->p_events;
794 vlc_mutex_init (&p_sys->mutex);
795 vlc_cond_init (&p_sys->cond);
796 vlc_mutex_init (&p_sys->lock);
797 vlc_mutex_init (&p_sys->in.fifo.lock);
798 vlc_cond_init (&p_sys->in.fifo.wait);
799 p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
800 p_sys->in.fifo.pp_last = &p_sys->in.fifo.p_first;
801 p_sys->in.b_direct = false;
802 p_sys->in.b_flushed = true;
803 p_sys->in.p_fmt = &p_dec->fmt_in;
804 vlc_mutex_init (&p_sys->out.fifo.lock);
805 vlc_cond_init (&p_sys->out.fifo.wait);
806 p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
807 p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
808 p_sys->out.b_direct = true;
809 p_sys->out.b_flushed = true;
810 p_sys->out.p_fmt = &p_dec->fmt_out;
812 p_sys->p_ports = &p_sys->in;
814 msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
815 (char *)&p_dec->fmt_out.i_codec);
817 /* Initialise the OMX core */
818 omx_error = p_sys->pf_init();
819 if(omx_error != OMX_ErrorNone)
821 msg_Warn( p_this, "OMX_Init failed (%x: %s)", omx_error,
822 ErrorToString(omx_error) );
823 CloseGeneric(p_this);
826 p_sys->b_init = true;
828 /* Enumerate components and build a list of the one we want to try */
829 if( !CreateComponentsList(p_dec,
830 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
831 p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
834 msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
835 (char *)&p_dec->fmt_in.i_codec );
836 CloseGeneric(p_this);
840 /* Try to load and initialise a component */
841 for(i = 0; i < p_sys->components; i++)
843 omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
845 if(omx_error == OMX_ErrorNone) break;
847 CHECK_ERROR(omx_error, "no component could be initialised" );
849 /* Move component to Idle then Executing state */
850 OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
851 CHECK_ERROR(omx_error, "ONX_CommandStateSet Idle failed (%x)", omx_error );
853 /* Allocate omx buffers */
854 for(i = 0; i < p_sys->ports; i++)
856 OmxPort *p_port = &p_sys->p_ports[i];
858 for(j = 0; j < p_port->i_buffers; j++)
861 #define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
862 char *p_buf = malloc(p_port->definition.nBufferSize +
863 p_port->definition.nBufferAlignment);
864 p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
867 if(0 && p_port->b_direct)
869 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
870 p_port->i_port_index, 0,
871 p_port->definition.nBufferSize, (void*)1);
874 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
875 p_port->i_port_index, 0,
876 p_port->definition.nBufferSize);
878 if(omx_error != OMX_ErrorNone) break;
879 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
881 p_port->i_buffers = j;
882 CHECK_ERROR(omx_error, "ONX_UseBuffer failed (%x, %i, %i)",
883 omx_error, (int)p_port->i_port_index, j );
886 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
887 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
889 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
890 OMX_StateExecuting, 0);
891 CHECK_ERROR(omx_error, "ONX_CommandStateSet Executing failed (%x)", omx_error );
892 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
893 CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
895 /* Send codec configuration data */
896 if( p_dec->fmt_in.i_extra )
898 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
899 p_header->nFilledLen = p_dec->fmt_in.i_extra;
901 if(p_sys->in.b_direct)
903 p_header->pOutputPortPrivate = p_header->pBuffer;
904 p_header->pBuffer = p_dec->fmt_in.p_extra;
908 if(p_header->nFilledLen > p_header->nAllocLen)
910 msg_Dbg(p_dec, "buffer too small (%i,%i)", (int)p_header->nFilledLen,
911 (int)p_header->nAllocLen);
912 p_header->nFilledLen = p_header->nAllocLen;
914 memcpy(p_header->pBuffer, p_dec->fmt_in.p_extra, p_header->nFilledLen);
917 p_header->nOffset = 0;
918 p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG;
919 msg_Dbg(p_dec, "sending codec config data %p, %p, %i", p_header,
920 p_header->pBuffer, (int)p_header->nFilledLen);
921 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
924 /* Get back output port definition */
925 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
926 if(omx_error != OMX_ErrorNone) goto error;
928 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
929 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
931 if(p_sys->b_error) goto error;
933 p_dec->b_need_packetized = true;
937 CloseGeneric(p_this);
941 /*****************************************************************************
943 *****************************************************************************/
944 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
946 decoder_sys_t *p_sys = p_dec->p_sys;
947 OMX_PARAM_PORTDEFINITIONTYPE definition;
948 OMX_BUFFERHEADERTYPE *p_buffer;
949 OMX_ERRORTYPE omx_error;
952 /* Sanity checking */
953 OMX_INIT_STRUCTURE(definition);
954 definition.nPortIndex = p_port->i_port_index;
955 omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
957 if(omx_error != OMX_ErrorNone ||
958 !definition.format.video.nFrameWidth ||
959 !definition.format.video.nFrameHeight )
960 return OMX_ErrorUndefined;
962 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
963 p_port->i_port_index, NULL);
964 CHECK_ERROR(omx_error, "ONX_CommandPortDisable on %i failed (%x)",
965 (int)p_port->i_port_index, omx_error );
967 for(i = 0; i < p_port->i_buffers; i++)
969 OMX_FIFO_GET(&p_port->fifo, p_buffer);
970 omx_error = OMX_FreeBuffer( p_sys->omx_handle,
971 p_port->i_port_index, p_buffer );
973 if(omx_error != OMX_ErrorNone) break;
975 CHECK_ERROR(omx_error, "ONX_FreeBuffer failed (%x, %i, %i)",
976 omx_error, (int)p_port->i_port_index, i );
978 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
979 CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
981 /* Get the new port definition */
982 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
983 if(omx_error != OMX_ErrorNone) goto error;
984 omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
986 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
987 omx_error, ErrorToString(omx_error));
989 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
990 p_port->i_port_index, NULL);
991 CHECK_ERROR(omx_error, "ONX_CommandPortEnable on %i failed (%x)",
992 (int)p_port->i_port_index, omx_error );
994 for(i = 0; i < p_port->i_buffers; i++)
996 if(0 && p_port->b_direct)
998 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
999 p_port->i_port_index, 0,
1000 p_port->definition.nBufferSize, (void*)1);
1003 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1004 p_port->i_port_index, 0,
1005 p_port->definition.nBufferSize);
1007 if(omx_error != OMX_ErrorNone) break;
1008 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1010 p_port->i_buffers = i;
1011 CHECK_ERROR(omx_error, "ONX_UseBuffer failed (%x, %i, %i)",
1012 omx_error, (int)p_port->i_port_index, i );
1014 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1015 CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1017 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1018 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1024 /*****************************************************************************
1025 * DecodeVideo: Called to decode one frame
1026 *****************************************************************************/
1027 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1029 decoder_sys_t *p_sys = p_dec->p_sys;
1030 picture_t *p_pic = NULL, *p_next_pic;
1031 OMX_ERRORTYPE omx_error;
1034 OMX_BUFFERHEADERTYPE *p_header;
1037 if( !pp_block || !*pp_block )
1040 p_block = *pp_block;
1042 /* Check for errors from codec */
1045 msg_Dbg(p_dec, "error during decoding");
1046 block_Release( p_block );
1050 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1052 block_Release( p_block );
1053 if(!p_sys->in.b_flushed)
1055 msg_Dbg(p_dec, "flushing");
1056 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1057 p_sys->in.definition.nPortIndex, 0 );
1059 p_sys->in.b_flushed = true;
1063 /* Take care of decoded frames first */
1066 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1067 if(!p_header) break; /* No frame available */
1069 if(p_header->nFilledLen)
1071 p_pic = p_header->pAppPrivate;
1074 /* We're not in direct rendering mode.
1075 * Get a new picture and copy the content */
1076 p_pic = decoder_NewPicture( p_dec );
1077 if( !p_pic ) break; /* No picture available */
1079 CopyOmxPicture(p_dec, p_pic, p_header);
1082 p_pic->date = p_header->nTimeStamp;
1083 p_header->nFilledLen = 0;
1084 p_header->pAppPrivate = 0;
1087 /* Get a new picture */
1088 if(p_sys->in.b_direct && !p_header->pAppPrivate)
1090 p_next_pic = decoder_NewPicture( p_dec );
1091 if(!p_next_pic) break;
1093 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1094 p_header->pAppPrivate = p_next_pic;
1095 p_header->pInputPortPrivate = p_header->pBuffer;
1096 p_header->pBuffer = p_next_pic->p[0].p_pixels;
1100 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1103 #ifdef OMXIL_EXTRA_DEBUG
1104 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1106 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1109 /* Send the input buffer to the component */
1110 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1113 p_header->nFilledLen = p_block->i_buffer;
1114 p_header->nOffset = 0;
1115 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1116 p_header->nTimeStamp = p_block->i_dts;
1118 /* In direct mode we pass the input pointer as is.
1119 * Otherwise we memcopy the data */
1120 if(p_sys->in.b_direct)
1122 p_header->pOutputPortPrivate = p_header->pBuffer;
1123 p_header->pBuffer = p_block->p_buffer;
1124 p_header->pAppPrivate = p_block;
1128 if(p_header->nFilledLen > p_header->nAllocLen)
1130 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1131 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1132 p_header->nFilledLen = p_header->nAllocLen;
1134 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1135 block_Release(p_block);
1138 #ifdef OMXIL_EXTRA_DEBUG
1139 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1140 (int)p_header->nFilledLen );
1142 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1143 p_sys->in.b_flushed = false;
1144 *pp_block = NULL; /* Avoid being fed the same packet again */
1147 /* Handle the PortSettingsChanged events */
1148 for(i = 0; i < p_sys->ports; i++)
1150 OmxPort *p_port = &p_sys->p_ports[i];
1151 if(!p_port->b_reconfigure) continue;
1152 p_port->b_reconfigure = 0;
1153 omx_error = PortReconfigure(p_dec, p_port);
1159 /*****************************************************************************
1160 * DecodeAudio: Called to decode one frame
1161 *****************************************************************************/
1162 aout_buffer_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1164 decoder_sys_t *p_sys = p_dec->p_sys;
1165 aout_buffer_t *p_buffer = 0;
1166 OMX_BUFFERHEADERTYPE *p_header;
1167 OMX_ERRORTYPE omx_error;
1171 if( !pp_block || !*pp_block ) return NULL;
1173 p_block = *pp_block;
1175 /* Check for errors from codec */
1178 msg_Dbg(p_dec, "error during decoding");
1179 block_Release( p_block );
1183 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1185 block_Release( p_block );
1186 date_Set( &p_sys->end_date, 0 );
1187 if(!p_sys->in.b_flushed)
1189 msg_Dbg(p_dec, "flushing");
1190 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1191 p_sys->in.definition.nPortIndex, 0 );
1193 p_sys->in.b_flushed = true;
1197 if( !date_Get( &p_sys->end_date ) )
1199 if( !p_block->i_pts )
1201 /* We've just started the stream, wait for the first PTS. */
1202 block_Release( p_block );
1205 date_Set( &p_sys->end_date, p_block->i_pts );
1208 /* Take care of decoded frames first */
1211 unsigned int i_samples;
1213 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1214 if(!p_header) break; /* No frame available */
1216 i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1219 p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1220 if( !p_buffer ) break; /* No audio buffer available */
1222 memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1223 p_header->nFilledLen = 0;
1225 if( p_header->nTimeStamp != 0 &&
1226 p_header->nTimeStamp != date_Get( &p_sys->end_date ) )
1227 date_Set( &p_sys->end_date, p_header->nTimeStamp );
1229 p_buffer->i_pts = date_Get( &p_sys->end_date );
1230 p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1234 #ifdef OMXIL_EXTRA_DEBUG
1235 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1237 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1238 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1242 /* Send the input buffer to the component */
1243 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1246 p_header->nFilledLen = p_block->i_buffer;
1247 p_header->nOffset = 0;
1248 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1249 p_header->nTimeStamp = p_block->i_dts;
1251 /* In direct mode we pass the input pointer as is.
1252 * Otherwise we memcopy the data */
1253 if(p_sys->in.b_direct)
1255 p_header->pOutputPortPrivate = p_header->pBuffer;
1256 p_header->pBuffer = p_block->p_buffer;
1257 p_header->pAppPrivate = p_block;
1261 if(p_header->nFilledLen > p_header->nAllocLen)
1263 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1264 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1265 p_header->nFilledLen = p_header->nAllocLen;
1267 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1268 block_Release(p_block);
1271 #ifdef OMXIL_EXTRA_DEBUG
1272 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1273 (int)p_header->nFilledLen );
1275 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1276 p_sys->in.b_flushed = false;
1277 *pp_block = NULL; /* Avoid being fed the same packet again */
1280 /* Handle the PortSettingsChanged events */
1281 for(i = 0; i < p_sys->ports; i++)
1283 OmxPort *p_port = &p_sys->p_ports[i];
1284 if(!p_port->b_reconfigure) continue;
1285 p_port->b_reconfigure = 0;
1286 omx_error = PortReconfigure(p_dec, p_port);
1292 /*****************************************************************************
1293 * EncodeVideo: Called to encode one frame
1294 *****************************************************************************/
1295 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1297 decoder_t *p_dec = ( decoder_t *)p_enc;
1298 decoder_sys_t *p_sys = p_dec->p_sys;
1299 OMX_ERRORTYPE omx_error;
1302 OMX_BUFFERHEADERTYPE *p_header;
1303 block_t *p_block = 0;
1305 if( !p_pic ) return NULL;
1307 /* Check for errors from codec */
1310 msg_Dbg(p_dec, "error during encoding");
1314 /* Send the input buffer to the component */
1315 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1318 /* In direct mode we pass the input pointer as is.
1319 * Otherwise we memcopy the data */
1320 if(p_sys->in.b_direct)
1322 p_header->pOutputPortPrivate = p_header->pBuffer;
1323 p_header->pBuffer = p_pic->p[0].p_pixels;
1327 CopyVlcPicture(p_dec, p_header, p_pic);
1330 p_header->nFilledLen = p_sys->in.i_frame_size;
1331 p_header->nOffset = 0;
1332 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1333 p_header->nTimeStamp = p_pic->date;
1334 #ifdef OMXIL_EXTRA_DEBUG
1335 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1336 (int)p_header->nFilledLen );
1338 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1339 p_sys->in.b_flushed = false;
1342 /* Handle the PortSettingsChanged events */
1343 for(i = 0; i < p_sys->ports; i++)
1345 OmxPort *p_port = &p_sys->p_ports[i];
1346 if(!p_port->b_reconfigure) continue;
1347 p_port->b_reconfigure = 0;
1348 omx_error = PortReconfigure(p_dec, p_port);
1351 /* Wait for the decoded frame */
1354 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1356 if(p_header->nFilledLen)
1358 if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1360 /* TODO: need to store codec config */
1361 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1364 p_block = p_header->pAppPrivate;
1367 /* We're not in direct rendering mode.
1368 * Get a new block and copy the content */
1369 p_block = block_New( p_dec, p_header->nFilledLen );
1370 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1373 p_block->i_buffer = p_header->nFilledLen;
1374 p_block->i_pts = p_block->i_dts = p_header->nTimeStamp;
1375 p_header->nFilledLen = 0;
1376 p_header->pAppPrivate = 0;
1379 #ifdef OMXIL_EXTRA_DEBUG
1380 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1382 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1385 msg_Dbg(p_dec, "done");
1389 /*****************************************************************************
1390 * CloseGeneric: omxil decoder destruction
1391 *****************************************************************************/
1392 static void CloseGeneric( vlc_object_t *p_this )
1394 decoder_t *p_dec = (decoder_t *)p_this;
1395 decoder_sys_t *p_sys = p_dec->p_sys;
1397 if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1398 if(p_sys->b_init) p_sys->pf_deinit();
1399 dll_close( p_sys->dll_handle );
1401 vlc_mutex_destroy (&p_sys->mutex);
1402 vlc_cond_destroy (&p_sys->cond);
1403 vlc_mutex_destroy (&p_sys->in.fifo.lock);
1404 vlc_cond_destroy (&p_sys->in.fifo.wait);
1405 vlc_mutex_destroy (&p_sys->out.fifo.lock);
1406 vlc_cond_destroy (&p_sys->out.fifo.wait);
1411 /*****************************************************************************
1413 *****************************************************************************/
1414 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1415 OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1416 OMX_U32 data_2, OMX_PTR event_data )
1418 decoder_t *p_dec = (decoder_t *)app_data;
1419 decoder_sys_t *p_sys = p_dec->p_sys;
1425 case OMX_EventCmdComplete:
1426 switch ((OMX_STATETYPE)data_1)
1428 case OMX_CommandStateSet:
1429 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %s)", EventToString(event),
1430 CommandToString(data_1), StateToString(data_2) );
1434 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u)", EventToString(event),
1435 CommandToString(data_1), (unsigned int)data_2 );
1440 case OMX_EventError:
1441 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u, %s)", EventToString(event),
1442 ErrorToString((OMX_ERRORTYPE)data_1), (unsigned int)data_2,
1443 (const char *)event_data);
1444 //p_sys->b_error = true;
1447 case OMX_EventPortSettingsChanged:
1448 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1449 (unsigned int)data_1, (unsigned int)data_2 );
1450 for(i = 0; i < p_sys->ports; i++)
1451 if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1452 p_sys->p_ports[i].b_reconfigure = true;
1456 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1457 (unsigned int)data_1, (unsigned int)data_2 );
1461 PostOmxEvent(p_dec, event, data_1, data_2, event_data);
1462 return OMX_ErrorNone;
1465 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1466 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1468 decoder_t *p_dec = (decoder_t *)app_data;
1469 decoder_sys_t *p_sys = p_dec->p_sys;
1472 #ifdef OMXIL_EXTRA_DEBUG
1473 msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1476 if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1478 block_t *p_block = (block_t *)omx_header->pAppPrivate;
1479 omx_header->pBuffer = omx_header->pOutputPortPrivate;
1480 if(p_block) block_Release(p_block);
1481 omx_header->pAppPrivate = 0;
1483 OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1485 return OMX_ErrorNone;
1488 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1489 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1491 decoder_t *p_dec = (decoder_t *)app_data;
1492 decoder_sys_t *p_sys = p_dec->p_sys;
1495 #ifdef OMXIL_EXTRA_DEBUG
1496 msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
1497 (int)omx_header->nFilledLen );
1500 if(omx_header->pInputPortPrivate)
1502 omx_header->pBuffer = omx_header->pInputPortPrivate;
1504 OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1506 return OMX_ErrorNone;