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 *****************************************************************************/
32 #define dll_open(name) dlopen( name, RTLD_NOW )
33 #define dll_close(handle) dlclose(handle)
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 static int OpenDecoder( vlc_object_t * );
60 static int OpenEncoder( vlc_object_t * );
61 static int OpenGeneric( vlc_object_t *, bool b_encode );
62 static void CloseGeneric( vlc_object_t * );
64 static picture_t *DecodeVideo( decoder_t *, block_t ** );
65 static aout_buffer_t *DecodeAudio ( decoder_t *, block_t ** );
66 static block_t *EncodeVideo( encoder_t *, picture_t * );
68 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE, OMX_PTR, OMX_EVENTTYPE,
69 OMX_U32, OMX_U32, OMX_PTR );
70 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE, OMX_PTR,
71 OMX_BUFFERHEADERTYPE * );
72 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE, OMX_PTR,
73 OMX_BUFFERHEADERTYPE * );
75 /*****************************************************************************
77 *****************************************************************************/
79 set_description( N_("Audio/Video decoder (using OpenMAX IL)") )
80 set_category( CAT_INPUT )
81 set_subcategory( SUBCAT_INPUT_VCODEC )
82 set_section( N_("Decoding") , NULL )
84 set_capability( "decoder", 80 )
86 set_capability( "decoder", 0 )
88 set_callbacks( OpenDecoder, CloseGeneric )
91 set_section( N_("Encoding") , NULL )
92 set_description( N_("Video encoder (using OpenMAX IL)") )
93 set_capability( "encoder", 0 )
94 set_callbacks( OpenEncoder, CloseGeneric )
97 /*****************************************************************************
98 * CreateComponentsList: creates a list of components matching the given role
99 *****************************************************************************/
100 static int CreateComponentsList(decoder_t *p_dec, const char *psz_role)
102 decoder_sys_t *p_sys = p_dec->p_sys;
103 char psz_name[OMX_MAX_STRINGNAME_SIZE];
104 OMX_ERRORTYPE omx_error;
106 OMX_U8 **ppsz_roles = 0;
107 unsigned int i, j, len;
109 if(!psz_role) goto end;
110 len = strlen(psz_role);
114 bool b_found = false;
116 omx_error = p_sys->pf_component_enum(psz_name, OMX_MAX_STRINGNAME_SIZE, i);
117 if(omx_error != OMX_ErrorNone) break;
119 msg_Dbg(p_dec, "component %s", psz_name);
121 omx_error = p_sys->pf_get_roles_of_component(psz_name, &roles, 0);
122 if(omx_error != OMX_ErrorNone || !roles) continue;
124 ppsz_roles = malloc(roles * (sizeof(OMX_U8*) + OMX_MAX_STRINGNAME_SIZE));
125 if(!ppsz_roles) continue;
127 for( j = 0; j < roles; j++ )
128 ppsz_roles[j] = ((OMX_U8 *)(&ppsz_roles[roles])) +
129 j * OMX_MAX_STRINGNAME_SIZE;
131 omx_error = p_sys->pf_get_roles_of_component(psz_name, &roles, ppsz_roles);
132 if(omx_error != OMX_ErrorNone) roles = 0;
134 for(j = 0; j < roles; j++)
136 msg_Dbg(p_dec, " - role: %s", ppsz_roles[j]);
137 if(!strncmp((char *)ppsz_roles[j], psz_role, len)) b_found = true;
142 if(!b_found) continue;
144 if(p_sys->components >= MAX_COMPONENTS_LIST_SIZE)
146 msg_Dbg(p_dec, "too many matching components");
150 strncpy(p_sys->ppsz_components[p_sys->components], psz_name,
151 OMX_MAX_STRINGNAME_SIZE-1);
156 msg_Dbg(p_dec, "found %i matching components for role %s",
157 p_sys->components, psz_role);
158 for( i = 0; i < p_sys->components; i++ )
159 msg_Dbg(p_dec, "- %s", p_sys->ppsz_components[i]);
161 return p_sys->components;
164 /*****************************************************************************
165 * ImplementationSpecificWorkarounds: place-holder for implementation
166 * specific workarounds
167 *****************************************************************************/
168 static OMX_ERRORTYPE ImplementationSpecificWorkarounds(decoder_t *p_dec,
169 OmxPort *p_port, es_format_t *p_fmt)
171 decoder_sys_t *p_sys = p_dec->p_sys;
172 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
173 int i_profile = 0xFFFF, i_level = 0xFFFF;
175 /* Try to find out the profile of the video */
176 while(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
177 p_fmt->i_codec == VLC_CODEC_H264)
179 uint8_t *p = (uint8_t*)p_dec->fmt_in.p_extra;
180 if(!p || !p_dec->fmt_in.p_extra) break;
182 /* Check the profile / level */
183 if(p_dec->fmt_in.i_original_fourcc == VLC_FOURCC('a','v','c','1') &&
186 if(p_dec->fmt_in.i_extra < 12) break;
187 p_sys->i_nal_size_length = 1 + (p[4]&0x03);
188 if( !(p[5]&0x1f) ) break;
193 if(p_dec->fmt_in.i_extra < 8) break;
194 if(!p[0] && !p[1] && !p[2] && p[3] == 1) p += 4;
195 else if(!p[0] && !p[1] && p[2] == 1) p += 3;
199 if( ((*p++)&0x1f) != 7) break;
201 /* Get profile/level out of first SPS */
207 if(!strcmp(p_sys->psz_component, "OMX.TI.Video.Decoder"))
209 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
210 p_fmt->i_codec == VLC_CODEC_H264 &&
211 (i_profile != 66 || i_level > 30))
213 msg_Dbg(p_dec, "h264 profile/level not supported (0x%x, 0x%x)",
215 return OMX_ErrorNotImplemented;
218 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirOutput &&
219 p_fmt->i_codec == VLC_CODEC_I420)
221 /* I420 xvideo is slow on OMAP */
222 def->format.video.eColorFormat = OMX_COLOR_FormatCbYCrY;
223 GetVlcChromaFormat( def->format.video.eColorFormat,
224 &p_fmt->i_codec, 0 );
225 GetVlcChromaSizes( p_fmt->i_codec,
226 def->format.video.nFrameWidth,
227 def->format.video.nFrameHeight,
228 &p_port->i_frame_size, &p_port->i_frame_stride,
229 &p_port->i_frame_stride_chroma_div );
230 def->format.video.nStride = p_port->i_frame_stride;
231 def->nBufferSize = p_port->i_frame_size;
234 else if(!strcmp(p_sys->psz_component, "OMX.st.video_encoder"))
236 if(p_fmt->i_cat == VIDEO_ES)
238 /* Bellagio's encoder doesn't encode the framerate in Q16 */
239 def->format.video.xFramerate >>= 16;
243 return OMX_ErrorNone;
246 /*****************************************************************************
247 * SetPortDefinition: set definition of the omx port based on the vlc format
248 *****************************************************************************/
249 static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
252 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
253 OMX_ERRORTYPE omx_error;
255 omx_error = OMX_GetParameter(p_port->omx_handle,
256 OMX_IndexParamPortDefinition, def);
257 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
258 omx_error, ErrorToString(omx_error));
263 def->format.video.nFrameWidth = p_fmt->video.i_width;
264 def->format.video.nFrameHeight = p_fmt->video.i_height;
265 if(def->format.video.eCompressionFormat == OMX_VIDEO_CodingUnused)
266 def->format.video.nStride = def->format.video.nFrameWidth;
267 if( p_fmt->video.i_frame_rate > 0 &&
268 p_fmt->video.i_frame_rate_base > 0 )
269 def->format.video.xFramerate = (p_fmt->video.i_frame_rate << 16) /
270 p_fmt->video.i_frame_rate_base;
272 if(def->eDir == OMX_DirInput || p_dec->p_sys->b_enc)
274 def->nBufferSize = def->format.video.nFrameWidth *
275 def->format.video.nFrameHeight * 2;
276 p_port->i_frame_size = def->nBufferSize;
278 if(!GetOmxVideoFormat(p_fmt->i_codec,
279 &def->format.video.eCompressionFormat, 0) )
281 if(!GetOmxChromaFormat(p_fmt->i_codec,
282 &def->format.video.eColorFormat, 0) )
284 omx_error = OMX_ErrorNotImplemented;
285 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
286 (char *)&p_fmt->i_codec );
288 GetVlcChromaSizes( p_fmt->i_codec,
289 def->format.video.nFrameWidth,
290 def->format.video.nFrameHeight,
291 &p_port->i_frame_size, &p_port->i_frame_stride,
292 &p_port->i_frame_stride_chroma_div );
293 def->format.video.nStride = p_port->i_frame_stride;
294 def->nBufferSize = p_port->i_frame_size;
299 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
300 &p_fmt->i_codec, 0 ) )
302 omx_error = OMX_ErrorNotImplemented;
303 CHECK_ERROR(omx_error, "OMX color format %i not supported",
304 (int)def->format.video.eColorFormat );
306 GetVlcChromaSizes( p_fmt->i_codec,
307 def->format.video.nFrameWidth,
308 def->format.video.nFrameHeight,
309 &p_port->i_frame_size, &p_port->i_frame_stride,
310 &p_port->i_frame_stride_chroma_div );
311 def->format.video.nStride = p_port->i_frame_stride;
312 def->nBufferSize = p_port->i_frame_size;
317 p_port->i_frame_size = def->nBufferSize;
318 if(def->eDir == OMX_DirInput)
320 if(!GetOmxAudioFormat(p_fmt->i_codec,
321 &def->format.audio.eEncoding, 0) )
323 omx_error = OMX_ErrorNotImplemented;
324 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
325 (char *)&p_fmt->i_codec );
330 if( !GetVlcAudioFormat(def->format.audio.eEncoding,
331 &p_fmt->i_codec, 0 ) )
333 omx_error = OMX_ErrorNotImplemented;
334 CHECK_ERROR(omx_error, "OMX audio encoding %i not supported",
335 (int)def->format.audio.eEncoding );
340 default: return OMX_ErrorNotImplemented;
343 omx_error = ImplementationSpecificWorkarounds(p_dec, p_port, p_fmt);
344 CHECK_ERROR(omx_error, "ImplementationSpecificWorkarounds failed (%x : %s)",
345 omx_error, ErrorToString(omx_error));
347 omx_error = OMX_SetParameter(p_port->omx_handle,
348 OMX_IndexParamPortDefinition, def);
349 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
350 omx_error, ErrorToString(omx_error));
352 omx_error = OMX_GetParameter(p_port->omx_handle,
353 OMX_IndexParamPortDefinition, def);
354 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
355 omx_error, ErrorToString(omx_error));
357 if(p_port->i_frame_size > def->nBufferSize)
358 def->nBufferSize = p_port->i_frame_size;
359 p_port->i_frame_size = def->nBufferSize;
361 /* Deal with audio params */
362 if(p_fmt->i_cat == AUDIO_ES)
364 omx_error = SetAudioParameters(p_port->omx_handle,
365 &p_port->format_param, def->nPortIndex,
366 def->format.audio.eEncoding,
367 p_fmt->audio.i_channels,
370 p_fmt->audio.i_bitspersample,
371 p_fmt->audio.i_blockalign);
372 CHECK_ERROR(omx_error, "SetAudioParameters failed (%x : %s)",
373 omx_error, ErrorToString(omx_error));
380 /*****************************************************************************
381 * GetPortDefinition: set vlc format based on the definition of the omx port
382 *****************************************************************************/
383 static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
386 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
387 OMX_ERRORTYPE omx_error;
389 omx_error = OMX_GetParameter(p_port->omx_handle,
390 OMX_IndexParamPortDefinition, def);
391 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
392 omx_error, ErrorToString(omx_error));
397 p_fmt->video.i_width = def->format.video.nFrameWidth;
398 p_fmt->video.i_visible_width = def->format.video.nFrameWidth;
399 p_fmt->video.i_height = def->format.video.nFrameHeight;
400 p_fmt->video.i_visible_height = def->format.video.nFrameHeight;
401 p_fmt->video.i_frame_rate = p_dec->fmt_in.video.i_frame_rate;
402 p_fmt->video.i_frame_rate_base = p_dec->fmt_in.video.i_frame_rate_base;
404 if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
405 &p_fmt->i_codec, 0 ) )
407 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
408 &p_fmt->i_codec, 0 ) )
410 omx_error = OMX_ErrorNotImplemented;
411 CHECK_ERROR(omx_error, "OMX color format %i not supported",
412 (int)def->format.video.eColorFormat );
414 GetVlcChromaSizes( p_fmt->i_codec,
415 def->format.video.nFrameWidth,
416 def->format.video.nFrameHeight,
417 &p_port->i_frame_size, &p_port->i_frame_stride,
418 &p_port->i_frame_stride_chroma_div );
420 if(p_port->i_frame_size > def->nBufferSize)
421 def->nBufferSize = p_port->i_frame_size;
422 p_port->i_frame_size = def->nBufferSize;
424 if((int)p_port->i_frame_stride > def->format.video.nStride)
425 def->format.video.nStride = p_port->i_frame_stride;
427 p_port->i_frame_stride = def->format.video.nStride;
431 if( !GetVlcAudioFormat( def->format.audio.eEncoding,
432 &p_fmt->i_codec, 0 ) )
434 omx_error = OMX_ErrorNotImplemented;
435 CHECK_ERROR(omx_error, "OMX audio format %i not supported",
436 (int)def->format.audio.eEncoding );
439 omx_error = GetAudioParameters(p_port->omx_handle,
440 &p_port->format_param, def->nPortIndex,
441 def->format.audio.eEncoding,
442 &p_fmt->audio.i_channels,
443 &p_fmt->audio.i_rate,
445 &p_fmt->audio.i_bitspersample,
446 &p_fmt->audio.i_blockalign);
447 CHECK_ERROR(omx_error, "GetAudioParameters failed (%x : %s)",
448 omx_error, ErrorToString(omx_error));
450 if(p_fmt->audio.i_channels < 9)
452 static const int pi_channels_maps[9] =
454 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
455 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
456 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
457 | AOUT_CHAN_REARRIGHT,
458 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
459 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
460 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
461 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
462 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
463 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
464 | AOUT_CHAN_MIDDLERIGHT,
465 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
466 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
469 p_fmt->audio.i_physical_channels =
470 p_fmt->audio.i_original_channels =
471 pi_channels_maps[p_fmt->audio.i_channels];
474 date_Init( &p_dec->p_sys->end_date, p_fmt->audio.i_rate, 1 );
478 default: return OMX_ErrorNotImplemented;
485 /*****************************************************************************
486 * DeinitialiseComponent: Deinitialise and unload an OMX component
487 *****************************************************************************/
488 static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
489 OMX_HANDLETYPE omx_handle)
491 decoder_sys_t *p_sys = p_dec->p_sys;
492 OMX_ERRORTYPE omx_error;
496 if(!omx_handle) return OMX_ErrorNone;
498 omx_error = OMX_GetState(omx_handle, &state);
499 CHECK_ERROR(omx_error, "ONX_GetState failed (%x)", omx_error );
501 if(state == OMX_StateExecuting)
503 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
505 CHECK_ERROR(omx_error, "ONX_CommandStateSet Idle failed (%x)", omx_error );
506 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
507 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
510 omx_error = OMX_GetState(omx_handle, &state);
511 CHECK_ERROR(omx_error, "ONX_GetState failed (%x)", omx_error );
513 if(state == OMX_StateIdle)
515 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
516 OMX_StateLoaded, 0 );
517 CHECK_ERROR(omx_error, "ONX_CommandStateSet Loaded failed (%x)", omx_error );
519 for(i = 0; i < p_sys->ports; i++)
521 OmxPort *p_port = &p_sys->p_ports[i];
522 OMX_BUFFERHEADERTYPE *p_buffer;
524 for(j = 0; j < p_port->i_buffers; j++)
526 OMX_FIFO_GET(&p_port->fifo, p_buffer);
527 omx_error = OMX_FreeBuffer( omx_handle,
528 p_port->i_port_index, p_buffer );
530 if(omx_error != OMX_ErrorNone) break;
532 CHECK_ERROR(omx_error, "ONX_FreeBuffer failed (%x, %i, %i)",
533 omx_error, (int)p_port->i_port_index, j );
536 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
537 CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
541 for(i = 0; i < p_sys->ports; i++)
543 OmxPort *p_port = &p_sys->p_ports[i];
544 free(p_port->pp_buffers);
545 p_port->pp_buffers = 0;
547 omx_error = p_sys->pf_free_handle( omx_handle );
551 /*****************************************************************************
552 * InitialiseComponent: Load and initialise an OMX component
553 *****************************************************************************/
554 static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
555 OMX_STRING psz_component, OMX_HANDLETYPE *p_handle)
557 static OMX_CALLBACKTYPE callbacks =
558 { OmxEventHandler, OmxEmptyBufferDone, OmxFillBufferDone };
559 decoder_sys_t *p_sys = p_dec->p_sys;
560 OMX_HANDLETYPE omx_handle;
561 OMX_ERRORTYPE omx_error;
563 OMX_U8 psz_role[OMX_MAX_STRINGNAME_SIZE];
564 OMX_PARAM_COMPONENTROLETYPE role;
565 OMX_PARAM_PORTDEFINITIONTYPE definition;
566 OMX_PORT_PARAM_TYPE param;
569 omx_error = p_sys->pf_get_handle( &omx_handle, psz_component,
571 if(omx_error != OMX_ErrorNone)
573 msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
574 omx_error, ErrorToString(omx_error) );
577 strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
579 OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
580 msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
581 PrintOmx(p_dec, omx_handle, OMX_ALL);
583 /* Set component role */
584 OMX_INIT_STRUCTURE(role);
585 strcpy((char*)role.cRole,
586 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec : p_dec->fmt_in.i_codec,
587 p_dec->fmt_in.i_cat, p_sys->b_enc));
589 omx_error = OMX_SetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
591 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
593 if(omx_error == OMX_ErrorNone)
594 msg_Dbg(p_dec, "component standard role set to %s", role.cRole);
596 /* Find the input / output ports */
597 OMX_INIT_STRUCTURE(param);
598 OMX_INIT_STRUCTURE(definition);
599 omx_error = OMX_GetParameter(omx_handle, p_dec->fmt_in.i_cat == VIDEO_ES ?
600 OMX_IndexParamVideoInit : OMX_IndexParamAudioInit, ¶m);
601 if(omx_error != OMX_ErrorNone) param.nPorts = 0;
603 for(i = 0; i < param.nPorts; i++)
607 /* Get port definition */
608 definition.nPortIndex = param.nStartPortNumber + i;
609 omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
611 if(omx_error != OMX_ErrorNone) continue;
613 if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
614 else p_port = &p_sys->out;
616 p_port->b_valid = true;
617 p_port->i_port_index = definition.nPortIndex;
618 p_port->definition = definition;
619 p_port->omx_handle = omx_handle;
622 if(!p_sys->in.b_valid || !p_sys->out.b_valid)
624 omx_error = OMX_ErrorInvalidComponent;
625 CHECK_ERROR(omx_error, "couldn't find an input and output port");
628 /* Set port definitions */
629 for(i = 0; i < p_sys->ports; i++)
631 omx_error = SetPortDefinition(p_dec, &p_sys->p_ports[i],
632 p_sys->p_ports[i].p_fmt);
633 if(omx_error != OMX_ErrorNone) goto error;
636 /* Allocate our array for the omx buffers and enable ports */
637 for(i = 0; i < p_sys->ports; i++)
639 OmxPort *p_port = &p_sys->p_ports[i];
642 malloc(p_port->definition.nBufferCountActual *
643 sizeof(OMX_BUFFERHEADERTYPE*));
644 if(!p_port->pp_buffers)
646 omx_error = OMX_ErrorInsufficientResources;
647 CHECK_ERROR(omx_error, "memory allocation failed");
649 p_port->i_buffers = p_port->definition.nBufferCountActual;
652 if(!p_port->definition.bEnabled)
654 omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
655 p_port->i_port_index, NULL);
656 CHECK_ERROR(omx_error, "ONX_CommandPortEnable on %i failed (%x)",
657 (int)p_port->i_port_index, omx_error );
658 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
659 CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
660 (int)p_port->i_port_index, omx_error );
664 *p_handle = omx_handle;
665 return OMX_ErrorNone;
668 DeinitialiseComponent(p_dec, omx_handle);
673 /*****************************************************************************
674 * OpenDecoder: Create the decoder instance
675 *****************************************************************************/
676 static int OpenDecoder( vlc_object_t *p_this )
678 decoder_t *p_dec = (decoder_t*)p_this;
681 if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
685 if( p_dec->fmt_in.i_cat != VIDEO_ES && !p_dec->b_force)
689 status = OpenGeneric( p_this, false );
690 if(status != VLC_SUCCESS) return status;
692 p_dec->pf_decode_video = DecodeVideo;
693 p_dec->pf_decode_audio = DecodeAudio;
698 /*****************************************************************************
699 * OpenEncoder: Create the encoder instance
700 *****************************************************************************/
701 static int OpenEncoder( vlc_object_t *p_this )
703 encoder_t *p_enc = (encoder_t*)p_this;
706 if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
709 status = OpenGeneric( p_this, true );
710 if(status != VLC_SUCCESS) return status;
712 p_enc->pf_encode_video = EncodeVideo;
717 /*****************************************************************************
718 * OpenGeneric: Create the generic decoder/encoder instance
719 *****************************************************************************/
720 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
722 void *dll_handle = 0, *pf_init = 0, *pf_deinit = 0;
723 void *pf_get_handle = 0, *pf_free_handle = 0, *pf_component_enum = 0;
724 void *pf_get_roles_of_component = 0;
725 decoder_t *p_dec = (decoder_t*)p_this;
726 decoder_sys_t *p_sys;
727 OMX_ERRORTYPE omx_error;
728 OMX_BUFFERHEADERTYPE *p_header;
731 /* Load the OMX core */
732 for( i = 0; ppsz_dll_list[i]; i++ )
734 dll_handle = dll_open( ppsz_dll_list[i] );
735 if( dll_handle ) break;
737 if( !dll_handle ) return VLC_EGENERIC;
739 pf_init = dlsym( dll_handle, "OMX_Init" );
740 pf_deinit = dlsym( dll_handle, "OMX_Deinit" );
741 pf_get_handle = dlsym( dll_handle, "OMX_GetHandle" );
742 pf_free_handle = dlsym( dll_handle, "OMX_FreeHandle" );
743 pf_component_enum = dlsym( dll_handle, "OMX_ComponentNameEnum" );
744 pf_get_roles_of_component = dlsym( dll_handle, "OMX_GetRolesOfComponent" );
745 if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle ||
746 !pf_component_enum || !pf_get_roles_of_component )
748 msg_Warn( p_this, "cannot find OMX_* symbols in `%s' (%s)",
749 ppsz_dll_list[i], dlerror() );
750 dll_close(dll_handle);
753 if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle || !pf_component_enum )
755 dll_close(dll_handle);
759 /* Allocate the memory needed to store the decoder's structure */
760 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
762 dll_close(dll_handle);
766 /* Initialise the thread properties */
769 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
770 p_dec->fmt_out.video = p_dec->fmt_in.video;
771 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
772 p_dec->fmt_out.i_codec = 0;
774 p_sys->b_enc = b_encode;
775 p_sys->dll_handle = dll_handle;
776 p_sys->pf_init = pf_init;
777 p_sys->pf_deinit = pf_deinit;
778 p_sys->pf_get_handle = pf_get_handle;
779 p_sys->pf_free_handle = pf_free_handle;
780 p_sys->pf_component_enum = pf_component_enum;
781 p_sys->pf_get_roles_of_component = pf_get_roles_of_component;
782 p_sys->pp_last_event = &p_sys->p_events;
783 vlc_mutex_init (&p_sys->mutex);
784 vlc_cond_init (&p_sys->cond);
785 vlc_mutex_init (&p_sys->lock);
786 vlc_mutex_init (&p_sys->in.fifo.lock);
787 vlc_cond_init (&p_sys->in.fifo.wait);
788 p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
789 p_sys->in.fifo.pp_last = &p_sys->in.fifo.p_first;
790 p_sys->in.b_direct = false;
791 p_sys->in.b_flushed = true;
792 p_sys->in.p_fmt = &p_dec->fmt_in;
793 vlc_mutex_init (&p_sys->out.fifo.lock);
794 vlc_cond_init (&p_sys->out.fifo.wait);
795 p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
796 p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
797 p_sys->out.b_direct = true;
798 p_sys->out.b_flushed = true;
799 p_sys->out.p_fmt = &p_dec->fmt_out;
801 p_sys->p_ports = &p_sys->in;
803 msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
804 (char *)&p_dec->fmt_out.i_codec);
806 /* Initialise the OMX core */
807 omx_error = p_sys->pf_init();
808 if(omx_error != OMX_ErrorNone)
810 msg_Warn( p_this, "OMX_Init failed (%x: %s)", omx_error,
811 ErrorToString(omx_error) );
812 CloseGeneric(p_this);
815 p_sys->b_init = true;
817 /* Enumerate components and build a list of the one we want to try */
818 if( !CreateComponentsList(p_dec,
819 GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
820 p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
823 msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
824 (char *)&p_dec->fmt_in.i_codec );
825 CloseGeneric(p_this);
829 /* Try to load and initialise a component */
830 for(i = 0; i < p_sys->components; i++)
832 omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
834 if(omx_error == OMX_ErrorNone) break;
836 CHECK_ERROR(omx_error, "no component could be initialised" );
838 /* Move component to Idle then Executing state */
839 OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
840 CHECK_ERROR(omx_error, "ONX_CommandStateSet Idle failed (%x)", omx_error );
842 /* Allocate omx buffers */
843 for(i = 0; i < p_sys->ports; i++)
845 OmxPort *p_port = &p_sys->p_ports[i];
847 for(j = 0; j < p_port->i_buffers; j++)
850 #define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
851 char *p_buf = malloc(p_port->definition.nBufferSize +
852 p_port->definition.nBufferAlignment);
853 p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
856 if(0 && p_port->b_direct)
858 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
859 p_port->i_port_index, 0,
860 p_port->definition.nBufferSize, (void*)1);
863 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
864 p_port->i_port_index, 0,
865 p_port->definition.nBufferSize);
867 if(omx_error != OMX_ErrorNone) break;
868 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
870 p_port->i_buffers = j;
871 CHECK_ERROR(omx_error, "ONX_UseBuffer failed (%x, %i, %i)",
872 omx_error, (int)p_port->i_port_index, j );
875 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
876 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
878 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
879 OMX_StateExecuting, 0);
880 CHECK_ERROR(omx_error, "ONX_CommandStateSet Executing failed (%x)", omx_error );
881 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
882 CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
884 /* Send codec configuration data */
885 if( p_dec->fmt_in.i_extra )
887 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
888 p_header->nFilledLen = p_dec->fmt_in.i_extra;
890 if(p_sys->in.b_direct)
892 p_header->pOutputPortPrivate = p_header->pBuffer;
893 p_header->pBuffer = p_dec->fmt_in.p_extra;
897 if(p_header->nFilledLen > p_header->nAllocLen)
899 msg_Dbg(p_dec, "buffer too small (%i,%i)", (int)p_header->nFilledLen,
900 (int)p_header->nAllocLen);
901 p_header->nFilledLen = p_header->nAllocLen;
903 memcpy(p_header->pBuffer, p_dec->fmt_in.p_extra, p_header->nFilledLen);
906 p_header->nOffset = 0;
907 p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG;
908 msg_Dbg(p_dec, "sending codec config data %p, %p, %i", p_header,
909 p_header->pBuffer, (int)p_header->nFilledLen);
910 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
913 /* Get back output port definition */
914 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
915 if(omx_error != OMX_ErrorNone) goto error;
917 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
918 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
920 if(p_sys->b_error) goto error;
922 p_dec->b_need_packetized = true;
926 CloseGeneric(p_this);
930 /*****************************************************************************
932 *****************************************************************************/
933 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
935 decoder_sys_t *p_sys = p_dec->p_sys;
936 OMX_PARAM_PORTDEFINITIONTYPE definition;
937 OMX_BUFFERHEADERTYPE *p_buffer;
938 OMX_ERRORTYPE omx_error;
941 /* Sanity checking */
942 OMX_INIT_STRUCTURE(definition);
943 definition.nPortIndex = p_port->i_port_index;
944 omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
946 if(omx_error != OMX_ErrorNone ||
947 !definition.format.video.nFrameWidth ||
948 !definition.format.video.nFrameHeight )
949 return OMX_ErrorUndefined;
951 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
952 p_port->i_port_index, NULL);
953 CHECK_ERROR(omx_error, "ONX_CommandPortDisable on %i failed (%x)",
954 (int)p_port->i_port_index, omx_error );
956 for(i = 0; i < p_port->i_buffers; i++)
958 OMX_FIFO_GET(&p_port->fifo, p_buffer);
959 omx_error = OMX_FreeBuffer( p_sys->omx_handle,
960 p_port->i_port_index, p_buffer );
962 if(omx_error != OMX_ErrorNone) break;
964 CHECK_ERROR(omx_error, "ONX_FreeBuffer failed (%x, %i, %i)",
965 omx_error, (int)p_port->i_port_index, i );
967 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
968 CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
970 /* Get the new port definition */
971 omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
972 if(omx_error != OMX_ErrorNone) goto error;
973 omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
975 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
976 omx_error, ErrorToString(omx_error));
978 omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
979 p_port->i_port_index, NULL);
980 CHECK_ERROR(omx_error, "ONX_CommandPortEnable on %i failed (%x)",
981 (int)p_port->i_port_index, omx_error );
983 for(i = 0; i < p_port->i_buffers; i++)
985 if(0 && p_port->b_direct)
987 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
988 p_port->i_port_index, 0,
989 p_port->definition.nBufferSize, (void*)1);
992 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
993 p_port->i_port_index, 0,
994 p_port->definition.nBufferSize);
996 if(omx_error != OMX_ErrorNone) break;
997 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
999 p_port->i_buffers = i;
1000 CHECK_ERROR(omx_error, "ONX_UseBuffer failed (%x, %i, %i)",
1001 omx_error, (int)p_port->i_port_index, i );
1003 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1004 CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1006 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1007 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1013 /*****************************************************************************
1014 * DecodeVideo: Called to decode one frame
1015 *****************************************************************************/
1016 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1018 decoder_sys_t *p_sys = p_dec->p_sys;
1019 picture_t *p_pic = NULL, *p_next_pic;
1020 OMX_ERRORTYPE omx_error;
1023 OMX_BUFFERHEADERTYPE *p_header;
1026 if( !pp_block || !*pp_block )
1029 p_block = *pp_block;
1031 /* Check for errors from codec */
1034 msg_Dbg(p_dec, "error during decoding");
1035 block_Release( p_block );
1039 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1041 block_Release( p_block );
1042 if(!p_sys->in.b_flushed)
1044 msg_Dbg(p_dec, "flushing");
1045 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1046 p_sys->in.definition.nPortIndex, 0 );
1048 p_sys->in.b_flushed = true;
1052 /* Take care of decoded frames first */
1055 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1056 if(!p_header) break; /* No frame available */
1058 if(p_header->nFilledLen)
1060 p_pic = p_header->pAppPrivate;
1063 /* We're not in direct rendering mode.
1064 * Get a new picture and copy the content */
1065 p_pic = decoder_NewPicture( p_dec );
1066 if( !p_pic ) break; /* No picture available */
1068 CopyOmxPicture(p_dec, p_pic, p_header);
1071 p_pic->date = p_header->nTimeStamp;
1072 p_header->nFilledLen = 0;
1073 p_header->pAppPrivate = 0;
1076 /* Get a new picture */
1077 if(p_sys->in.b_direct && !p_header->pAppPrivate)
1079 p_next_pic = decoder_NewPicture( p_dec );
1080 if(!p_next_pic) break;
1082 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1083 p_header->pAppPrivate = p_next_pic;
1084 p_header->pInputPortPrivate = p_header->pBuffer;
1085 p_header->pBuffer = p_next_pic->p[0].p_pixels;
1089 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1092 #ifdef OMXIL_EXTRA_DEBUG
1093 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1095 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1098 /* Send the input buffer to the component */
1099 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1102 p_header->nFilledLen = p_block->i_buffer;
1103 p_header->nOffset = 0;
1104 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1105 p_header->nTimeStamp = p_block->i_dts;
1107 /* In direct mode we pass the input pointer as is.
1108 * Otherwise we memcopy the data */
1109 if(p_sys->in.b_direct)
1111 p_header->pOutputPortPrivate = p_header->pBuffer;
1112 p_header->pBuffer = p_block->p_buffer;
1113 p_header->pAppPrivate = p_block;
1117 if(p_header->nFilledLen > p_header->nAllocLen)
1119 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1120 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1121 p_header->nFilledLen = p_header->nAllocLen;
1123 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1124 block_Release(p_block);
1127 #ifdef OMXIL_EXTRA_DEBUG
1128 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1129 (int)p_header->nFilledLen );
1131 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1132 p_sys->in.b_flushed = false;
1133 *pp_block = NULL; /* Avoid being fed the same packet again */
1136 /* Handle the PortSettingsChanged events */
1137 for(i = 0; i < p_sys->ports; i++)
1139 OmxPort *p_port = &p_sys->p_ports[i];
1140 if(!p_port->b_reconfigure) continue;
1141 p_port->b_reconfigure = 0;
1142 omx_error = PortReconfigure(p_dec, p_port);
1148 /*****************************************************************************
1149 * DecodeAudio: Called to decode one frame
1150 *****************************************************************************/
1151 aout_buffer_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1153 decoder_sys_t *p_sys = p_dec->p_sys;
1154 aout_buffer_t *p_buffer = 0;
1155 OMX_BUFFERHEADERTYPE *p_header;
1156 OMX_ERRORTYPE omx_error;
1160 if( !pp_block || !*pp_block ) return NULL;
1162 p_block = *pp_block;
1164 /* Check for errors from codec */
1167 msg_Dbg(p_dec, "error during decoding");
1168 block_Release( p_block );
1172 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1174 block_Release( p_block );
1175 date_Set( &p_sys->end_date, 0 );
1176 if(!p_sys->in.b_flushed)
1178 msg_Dbg(p_dec, "flushing");
1179 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1180 p_sys->in.definition.nPortIndex, 0 );
1182 p_sys->in.b_flushed = true;
1186 if( !date_Get( &p_sys->end_date ) )
1188 if( !p_block->i_pts )
1190 /* We've just started the stream, wait for the first PTS. */
1191 block_Release( p_block );
1194 date_Set( &p_sys->end_date, p_block->i_pts );
1197 /* Take care of decoded frames first */
1200 unsigned int i_samples;
1202 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1203 if(!p_header) break; /* No frame available */
1205 i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1208 p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1209 if( !p_buffer ) break; /* No audio buffer available */
1211 memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1212 p_header->nFilledLen = 0;
1214 if( p_header->nTimeStamp != 0 &&
1215 p_header->nTimeStamp != date_Get( &p_sys->end_date ) )
1216 date_Set( &p_sys->end_date, p_header->nTimeStamp );
1218 p_buffer->i_pts = date_Get( &p_sys->end_date );
1219 p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1223 #ifdef OMXIL_EXTRA_DEBUG
1224 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1226 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1227 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1231 /* Send the input buffer to the component */
1232 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1235 p_header->nFilledLen = p_block->i_buffer;
1236 p_header->nOffset = 0;
1237 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1238 p_header->nTimeStamp = p_block->i_dts;
1240 /* In direct mode we pass the input pointer as is.
1241 * Otherwise we memcopy the data */
1242 if(p_sys->in.b_direct)
1244 p_header->pOutputPortPrivate = p_header->pBuffer;
1245 p_header->pBuffer = p_block->p_buffer;
1246 p_header->pAppPrivate = p_block;
1250 if(p_header->nFilledLen > p_header->nAllocLen)
1252 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1253 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1254 p_header->nFilledLen = p_header->nAllocLen;
1256 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1257 block_Release(p_block);
1260 #ifdef OMXIL_EXTRA_DEBUG
1261 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1262 (int)p_header->nFilledLen );
1264 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1265 p_sys->in.b_flushed = false;
1266 *pp_block = NULL; /* Avoid being fed the same packet again */
1269 /* Handle the PortSettingsChanged events */
1270 for(i = 0; i < p_sys->ports; i++)
1272 OmxPort *p_port = &p_sys->p_ports[i];
1273 if(!p_port->b_reconfigure) continue;
1274 p_port->b_reconfigure = 0;
1275 omx_error = PortReconfigure(p_dec, p_port);
1281 /*****************************************************************************
1282 * EncodeVideo: Called to encode one frame
1283 *****************************************************************************/
1284 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1286 decoder_t *p_dec = ( decoder_t *)p_enc;
1287 decoder_sys_t *p_sys = p_dec->p_sys;
1288 OMX_ERRORTYPE omx_error;
1291 OMX_BUFFERHEADERTYPE *p_header;
1292 block_t *p_block = 0;
1294 if( !p_pic ) return NULL;
1296 /* Check for errors from codec */
1299 msg_Dbg(p_dec, "error during encoding");
1303 /* Send the input buffer to the component */
1304 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1307 /* In direct mode we pass the input pointer as is.
1308 * Otherwise we memcopy the data */
1309 if(p_sys->in.b_direct)
1311 p_header->pOutputPortPrivate = p_header->pBuffer;
1312 p_header->pBuffer = p_pic->p[0].p_pixels;
1316 CopyVlcPicture(p_dec, p_header, p_pic);
1319 p_header->nFilledLen = p_sys->in.i_frame_size;
1320 p_header->nOffset = 0;
1321 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1322 p_header->nTimeStamp = p_pic->date;
1323 #ifdef OMXIL_EXTRA_DEBUG
1324 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1325 (int)p_header->nFilledLen );
1327 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1328 p_sys->in.b_flushed = false;
1331 /* Handle the PortSettingsChanged events */
1332 for(i = 0; i < p_sys->ports; i++)
1334 OmxPort *p_port = &p_sys->p_ports[i];
1335 if(!p_port->b_reconfigure) continue;
1336 p_port->b_reconfigure = 0;
1337 omx_error = PortReconfigure(p_dec, p_port);
1340 /* Wait for the decoded frame */
1343 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1345 if(p_header->nFilledLen)
1347 if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1349 /* TODO: need to store codec config */
1350 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1353 p_block = p_header->pAppPrivate;
1356 /* We're not in direct rendering mode.
1357 * Get a new block and copy the content */
1358 p_block = block_New( p_dec, p_header->nFilledLen );
1359 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1362 p_block->i_buffer = p_header->nFilledLen;
1363 p_block->i_pts = p_block->i_dts = p_header->nTimeStamp;
1364 p_header->nFilledLen = 0;
1365 p_header->pAppPrivate = 0;
1368 #ifdef OMXIL_EXTRA_DEBUG
1369 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1371 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1374 msg_Dbg(p_dec, "done");
1378 /*****************************************************************************
1379 * CloseGeneric: omxil decoder destruction
1380 *****************************************************************************/
1381 static void CloseGeneric( vlc_object_t *p_this )
1383 decoder_t *p_dec = (decoder_t *)p_this;
1384 decoder_sys_t *p_sys = p_dec->p_sys;
1386 if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1387 if(p_sys->b_init) p_sys->pf_deinit();
1388 dll_close( p_sys->dll_handle );
1390 vlc_mutex_destroy (&p_sys->mutex);
1391 vlc_cond_destroy (&p_sys->cond);
1392 vlc_mutex_destroy (&p_sys->in.fifo.lock);
1393 vlc_cond_destroy (&p_sys->in.fifo.wait);
1394 vlc_mutex_destroy (&p_sys->out.fifo.lock);
1395 vlc_cond_destroy (&p_sys->out.fifo.wait);
1400 /*****************************************************************************
1402 *****************************************************************************/
1403 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1404 OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1405 OMX_U32 data_2, OMX_PTR event_data )
1407 decoder_t *p_dec = (decoder_t *)app_data;
1408 decoder_sys_t *p_sys = p_dec->p_sys;
1414 case OMX_EventCmdComplete:
1415 switch ((OMX_STATETYPE)data_1)
1417 case OMX_CommandStateSet:
1418 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %s)", EventToString(event),
1419 CommandToString(data_1), StateToString(data_2) );
1423 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u)", EventToString(event),
1424 CommandToString(data_1), (unsigned int)data_2 );
1429 case OMX_EventError:
1430 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u, %s)", EventToString(event),
1431 ErrorToString((OMX_ERRORTYPE)data_1), (unsigned int)data_2,
1432 (const char *)event_data);
1433 //p_sys->b_error = true;
1436 case OMX_EventPortSettingsChanged:
1437 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1438 (unsigned int)data_1, (unsigned int)data_2 );
1439 for(i = 0; i < p_sys->ports; i++)
1440 if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1441 p_sys->p_ports[i].b_reconfigure = true;
1445 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1446 (unsigned int)data_1, (unsigned int)data_2 );
1450 PostOmxEvent(p_dec, event, data_1, data_2, event_data);
1451 return OMX_ErrorNone;
1454 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1455 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1457 decoder_t *p_dec = (decoder_t *)app_data;
1458 decoder_sys_t *p_sys = p_dec->p_sys;
1461 #ifdef OMXIL_EXTRA_DEBUG
1462 msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1465 if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1467 block_t *p_block = (block_t *)omx_header->pAppPrivate;
1468 omx_header->pBuffer = omx_header->pOutputPortPrivate;
1469 if(p_block) block_Release(p_block);
1470 omx_header->pAppPrivate = 0;
1472 OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1474 return OMX_ErrorNone;
1477 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1478 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1480 decoder_t *p_dec = (decoder_t *)app_data;
1481 decoder_sys_t *p_sys = p_dec->p_sys;
1484 #ifdef OMXIL_EXTRA_DEBUG
1485 msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
1486 (int)omx_header->nFilledLen );
1489 if(omx_header->pInputPortPrivate)
1491 omx_header->pBuffer = omx_header->pInputPortPrivate;
1493 OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1495 return OMX_ErrorNone;