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 *****************************************************************************/
34 #define dll_open(name) iomx_dlopen(name)
35 #define dll_close(handle) iomx_dlclose(handle)
36 #define dlsym(handle, name) iomx_dlsym(handle, name)
38 #define dll_open(name) dlopen( name, RTLD_NOW )
39 #define dll_close(handle) dlclose(handle)
42 #include <vlc_common.h>
43 #include <vlc_plugin.h>
44 #include <vlc_codec.h>
46 #include <vlc_block_helper.h>
51 //#define OMXIL_EXTRA_DEBUG
53 /*****************************************************************************
54 * List of OpenMAX IL core we will try in order
55 *****************************************************************************/
56 static const char *ppsz_dll_list[] =
58 "libOMX_Core.so", /* TI OMAP IL core */
59 "libOmxCore.so", /* Qualcomm IL core */
60 "libomxil-bellagio.so", /* Bellagio IL core */
64 /*****************************************************************************
66 *****************************************************************************/
67 static int OpenDecoder( vlc_object_t * );
68 static int OpenEncoder( vlc_object_t * );
69 static int OpenGeneric( vlc_object_t *, bool b_encode );
70 static void CloseGeneric( vlc_object_t * );
72 static picture_t *DecodeVideo( decoder_t *, block_t ** );
73 static aout_buffer_t *DecodeAudio ( decoder_t *, block_t ** );
74 static block_t *EncodeVideo( encoder_t *, picture_t * );
76 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE, OMX_PTR, OMX_EVENTTYPE,
77 OMX_U32, OMX_U32, OMX_PTR );
78 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE, OMX_PTR,
79 OMX_BUFFERHEADERTYPE * );
80 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE, OMX_PTR,
81 OMX_BUFFERHEADERTYPE * );
83 /*****************************************************************************
85 *****************************************************************************/
87 set_description( N_("Audio/Video decoder (using OpenMAX IL)") )
88 set_category( CAT_INPUT )
89 set_subcategory( SUBCAT_INPUT_VCODEC )
90 set_section( N_("Decoding") , NULL )
92 set_capability( "decoder", 80 )
94 set_capability( "decoder", 0 )
96 set_callbacks( OpenDecoder, CloseGeneric )
99 set_section( N_("Encoding") , NULL )
100 set_description( N_("Video encoder (using OpenMAX IL)") )
101 set_capability( "encoder", 0 )
102 set_callbacks( OpenEncoder, CloseGeneric )
105 /*****************************************************************************
106 * CreateComponentsList: creates a list of components matching the given role
107 *****************************************************************************/
108 static int CreateComponentsList(decoder_t *p_dec, const char *psz_role)
110 decoder_sys_t *p_sys = p_dec->p_sys;
111 char psz_name[OMX_MAX_STRINGNAME_SIZE];
112 OMX_ERRORTYPE omx_error;
114 OMX_U8 **ppsz_roles = 0;
115 unsigned int i, j, len;
117 if(!psz_role) goto end;
118 len = strlen(psz_role);
122 bool b_found = false;
124 omx_error = p_sys->pf_component_enum(psz_name, OMX_MAX_STRINGNAME_SIZE, i);
125 if(omx_error != OMX_ErrorNone) break;
127 msg_Dbg(p_dec, "component %s", psz_name);
129 omx_error = p_sys->pf_get_roles_of_component(psz_name, &roles, 0);
130 if(omx_error != OMX_ErrorNone || !roles) continue;
132 ppsz_roles = malloc(roles * (sizeof(OMX_U8*) + OMX_MAX_STRINGNAME_SIZE));
133 if(!ppsz_roles) continue;
135 for( j = 0; j < roles; j++ )
136 ppsz_roles[j] = ((OMX_U8 *)(&ppsz_roles[roles])) +
137 j * OMX_MAX_STRINGNAME_SIZE;
139 omx_error = p_sys->pf_get_roles_of_component(psz_name, &roles, ppsz_roles);
140 if(omx_error != OMX_ErrorNone) roles = 0;
142 for(j = 0; j < roles; j++)
144 msg_Dbg(p_dec, " - role: %s", ppsz_roles[j]);
145 if(!strncmp((char *)ppsz_roles[j], psz_role, len)) b_found = true;
150 if(!b_found) continue;
152 if(p_sys->components >= MAX_COMPONENTS_LIST_SIZE)
154 msg_Dbg(p_dec, "too many matching components");
158 strncpy(p_sys->ppsz_components[p_sys->components], psz_name,
159 OMX_MAX_STRINGNAME_SIZE-1);
164 msg_Dbg(p_dec, "found %i matching components for role %s",
165 p_sys->components, psz_role);
166 for( i = 0; i < p_sys->components; i++ )
167 msg_Dbg(p_dec, "- %s", p_sys->ppsz_components[i]);
169 return p_sys->components;
172 /*****************************************************************************
173 * ImplementationSpecificWorkarounds: place-holder for implementation
174 * specific workarounds
175 *****************************************************************************/
176 static OMX_ERRORTYPE ImplementationSpecificWorkarounds(decoder_t *p_dec,
177 OmxPort *p_port, es_format_t *p_fmt)
179 decoder_sys_t *p_sys = p_dec->p_sys;
180 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
181 int i_profile = 0xFFFF, i_level = 0xFFFF;
183 /* Try to find out the profile of the video */
184 while(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
185 p_fmt->i_codec == VLC_CODEC_H264)
187 uint8_t *p = (uint8_t*)p_dec->fmt_in.p_extra;
188 if(!p || !p_dec->fmt_in.p_extra) break;
190 /* Check the profile / level */
191 if(p_dec->fmt_in.i_original_fourcc == VLC_FOURCC('a','v','c','1') &&
194 if(p_dec->fmt_in.i_extra < 12) break;
195 p_sys->i_nal_size_length = 1 + (p[4]&0x03);
196 if( !(p[5]&0x1f) ) break;
201 if(p_dec->fmt_in.i_extra < 8) break;
202 if(!p[0] && !p[1] && !p[2] && p[3] == 1) p += 4;
203 else if(!p[0] && !p[1] && p[2] == 1) p += 3;
207 if( ((*p++)&0x1f) != 7) break;
209 /* Get profile/level out of first SPS */
215 if(!strcmp(p_sys->psz_component, "OMX.TI.Video.Decoder"))
217 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
218 p_fmt->i_codec == VLC_CODEC_H264 &&
219 (i_profile != 66 || i_level > 30))
221 msg_Dbg(p_dec, "h264 profile/level not supported (0x%x, 0x%x)",
223 return OMX_ErrorNotImplemented;
226 if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirOutput &&
227 p_fmt->i_codec == VLC_CODEC_I420)
229 /* I420 xvideo is slow on OMAP */
230 def->format.video.eColorFormat = OMX_COLOR_FormatCbYCrY;
231 GetVlcChromaFormat( def->format.video.eColorFormat,
232 &p_fmt->i_codec, 0 );
233 GetVlcChromaSizes( p_fmt->i_codec,
234 def->format.video.nFrameWidth,
235 def->format.video.nFrameHeight,
236 &p_port->i_frame_size, &p_port->i_frame_stride,
237 &p_port->i_frame_stride_chroma_div );
238 def->format.video.nStride = p_port->i_frame_stride;
239 def->nBufferSize = p_port->i_frame_size;
242 else if(!strcmp(p_sys->psz_component, "OMX.st.video_encoder"))
244 if(p_fmt->i_cat == VIDEO_ES)
246 /* Bellagio's encoder doesn't encode the framerate in Q16 */
247 def->format.video.xFramerate >>= 16;
251 return OMX_ErrorNone;
254 /*****************************************************************************
255 * SetPortDefinition: set definition of the omx port based on the vlc format
256 *****************************************************************************/
257 static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
260 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
261 OMX_ERRORTYPE omx_error;
263 omx_error = OMX_GetParameter(p_port->omx_handle,
264 OMX_IndexParamPortDefinition, def);
265 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
266 omx_error, ErrorToString(omx_error));
271 def->format.video.nFrameWidth = p_fmt->video.i_width;
272 def->format.video.nFrameHeight = p_fmt->video.i_height;
273 if(def->format.video.eCompressionFormat == OMX_VIDEO_CodingUnused)
274 def->format.video.nStride = def->format.video.nFrameWidth;
275 if( p_fmt->video.i_frame_rate > 0 &&
276 p_fmt->video.i_frame_rate_base > 0 )
277 def->format.video.xFramerate = (p_fmt->video.i_frame_rate << 16) /
278 p_fmt->video.i_frame_rate_base;
280 if(def->eDir == OMX_DirInput || p_dec->p_sys->b_enc)
282 def->nBufferSize = def->format.video.nFrameWidth *
283 def->format.video.nFrameHeight * 2;
284 p_port->i_frame_size = def->nBufferSize;
286 if(!GetOmxVideoFormat(p_fmt->i_codec,
287 &def->format.video.eCompressionFormat, 0) )
289 if(!GetOmxChromaFormat(p_fmt->i_codec,
290 &def->format.video.eColorFormat, 0) )
292 omx_error = OMX_ErrorNotImplemented;
293 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
294 (char *)&p_fmt->i_codec );
296 GetVlcChromaSizes( p_fmt->i_codec,
297 def->format.video.nFrameWidth,
298 def->format.video.nFrameHeight,
299 &p_port->i_frame_size, &p_port->i_frame_stride,
300 &p_port->i_frame_stride_chroma_div );
301 def->format.video.nStride = p_port->i_frame_stride;
302 def->nBufferSize = p_port->i_frame_size;
307 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
308 &p_fmt->i_codec, 0 ) )
310 omx_error = OMX_ErrorNotImplemented;
311 CHECK_ERROR(omx_error, "OMX color format %i not supported",
312 (int)def->format.video.eColorFormat );
314 GetVlcChromaSizes( p_fmt->i_codec,
315 def->format.video.nFrameWidth,
316 def->format.video.nFrameHeight,
317 &p_port->i_frame_size, &p_port->i_frame_stride,
318 &p_port->i_frame_stride_chroma_div );
319 def->format.video.nStride = p_port->i_frame_stride;
320 def->nBufferSize = p_port->i_frame_size;
325 p_port->i_frame_size = def->nBufferSize;
326 if(def->eDir == OMX_DirInput)
328 if(!GetOmxAudioFormat(p_fmt->i_codec,
329 &def->format.audio.eEncoding, 0) )
331 omx_error = OMX_ErrorNotImplemented;
332 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
333 (char *)&p_fmt->i_codec );
338 if( !GetVlcAudioFormat(def->format.audio.eEncoding,
339 &p_fmt->i_codec, 0 ) )
341 omx_error = OMX_ErrorNotImplemented;
342 CHECK_ERROR(omx_error, "OMX audio encoding %i not supported",
343 (int)def->format.audio.eEncoding );
348 default: return OMX_ErrorNotImplemented;
351 omx_error = ImplementationSpecificWorkarounds(p_dec, p_port, p_fmt);
352 CHECK_ERROR(omx_error, "ImplementationSpecificWorkarounds failed (%x : %s)",
353 omx_error, ErrorToString(omx_error));
355 omx_error = OMX_SetParameter(p_port->omx_handle,
356 OMX_IndexParamPortDefinition, def);
357 CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
358 omx_error, ErrorToString(omx_error));
360 omx_error = OMX_GetParameter(p_port->omx_handle,
361 OMX_IndexParamPortDefinition, def);
362 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
363 omx_error, ErrorToString(omx_error));
365 if(p_port->i_frame_size > def->nBufferSize)
366 def->nBufferSize = p_port->i_frame_size;
367 p_port->i_frame_size = def->nBufferSize;
369 /* Deal with audio params */
370 if(p_fmt->i_cat == AUDIO_ES)
372 omx_error = SetAudioParameters(p_port->omx_handle,
373 &p_port->format_param, def->nPortIndex,
374 def->format.audio.eEncoding,
375 p_fmt->audio.i_channels,
378 p_fmt->audio.i_bitspersample,
379 p_fmt->audio.i_blockalign);
380 CHECK_ERROR(omx_error, "SetAudioParameters failed (%x : %s)",
381 omx_error, ErrorToString(omx_error));
388 /*****************************************************************************
389 * GetPortDefinition: set vlc format based on the definition of the omx port
390 *****************************************************************************/
391 static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
394 OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
395 OMX_ERRORTYPE omx_error;
397 omx_error = OMX_GetParameter(p_port->omx_handle,
398 OMX_IndexParamPortDefinition, def);
399 CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
400 omx_error, ErrorToString(omx_error));
405 p_fmt->video.i_width = def->format.video.nFrameWidth;
406 p_fmt->video.i_visible_width = def->format.video.nFrameWidth;
407 p_fmt->video.i_height = def->format.video.nFrameHeight;
408 p_fmt->video.i_visible_height = def->format.video.nFrameHeight;
409 p_fmt->video.i_frame_rate = p_dec->fmt_in.video.i_frame_rate;
410 p_fmt->video.i_frame_rate_base = p_dec->fmt_in.video.i_frame_rate_base;
412 if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
413 &p_fmt->i_codec, 0 ) )
415 if( !GetVlcChromaFormat( def->format.video.eColorFormat,
416 &p_fmt->i_codec, 0 ) )
418 omx_error = OMX_ErrorNotImplemented;
419 CHECK_ERROR(omx_error, "OMX color format %i not supported",
420 (int)def->format.video.eColorFormat );
422 GetVlcChromaSizes( p_fmt->i_codec,
423 def->format.video.nFrameWidth,
424 def->format.video.nFrameHeight,
425 &p_port->i_frame_size, &p_port->i_frame_stride,
426 &p_port->i_frame_stride_chroma_div );
428 if(p_port->i_frame_size > def->nBufferSize)
429 def->nBufferSize = p_port->i_frame_size;
430 p_port->i_frame_size = def->nBufferSize;
432 if((int)p_port->i_frame_stride > def->format.video.nStride)
433 def->format.video.nStride = p_port->i_frame_stride;
435 p_port->i_frame_stride = def->format.video.nStride;
439 if( !GetVlcAudioFormat( def->format.audio.eEncoding,
440 &p_fmt->i_codec, 0 ) )
442 omx_error = OMX_ErrorNotImplemented;
443 CHECK_ERROR(omx_error, "OMX audio format %i not supported",
444 (int)def->format.audio.eEncoding );
447 omx_error = GetAudioParameters(p_port->omx_handle,
448 &p_port->format_param, def->nPortIndex,
449 def->format.audio.eEncoding,
450 &p_fmt->audio.i_channels,
451 &p_fmt->audio.i_rate,
453 &p_fmt->audio.i_bitspersample,
454 &p_fmt->audio.i_blockalign);
455 CHECK_ERROR(omx_error, "GetAudioParameters failed (%x : %s)",
456 omx_error, ErrorToString(omx_error));
458 if(p_fmt->audio.i_channels < 9)
460 static const int pi_channels_maps[9] =
462 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
463 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
464 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
465 | AOUT_CHAN_REARRIGHT,
466 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
467 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
468 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
469 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
470 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
471 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
472 | AOUT_CHAN_MIDDLERIGHT,
473 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
474 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
477 p_fmt->audio.i_physical_channels =
478 p_fmt->audio.i_original_channels =
479 pi_channels_maps[p_fmt->audio.i_channels];
482 date_Init( &p_dec->p_sys->end_date, p_fmt->audio.i_rate, 1 );
486 default: return OMX_ErrorNotImplemented;
493 /*****************************************************************************
494 * DeinitialiseComponent: Deinitialise and unload an OMX component
495 *****************************************************************************/
496 static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
497 OMX_HANDLETYPE omx_handle)
499 decoder_sys_t *p_sys = p_dec->p_sys;
500 OMX_ERRORTYPE omx_error;
504 if(!omx_handle) return OMX_ErrorNone;
506 omx_error = OMX_GetState(omx_handle, &state);
507 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
509 if(state == OMX_StateExecuting)
511 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
513 CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
514 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
515 CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
518 omx_error = OMX_GetState(omx_handle, &state);
519 CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
521 if(state == OMX_StateIdle)
523 omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
524 OMX_StateLoaded, 0 );
525 CHECK_ERROR(omx_error, "OMX_CommandStateSet Loaded failed (%x)", omx_error );
527 for(i = 0; i < p_sys->ports; i++)
529 OmxPort *p_port = &p_sys->p_ports[i];
530 OMX_BUFFERHEADERTYPE *p_buffer;
532 for(j = 0; j < p_port->i_buffers; j++)
534 OMX_FIFO_GET(&p_port->fifo, p_buffer);
535 omx_error = OMX_FreeBuffer( omx_handle,
536 p_port->i_port_index, p_buffer );
538 if(omx_error != OMX_ErrorNone) break;
540 CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
541 omx_error, (int)p_port->i_port_index, j );
544 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
545 CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
549 for(i = 0; i < p_sys->ports; i++)
551 OmxPort *p_port = &p_sys->p_ports[i];
552 free(p_port->pp_buffers);
553 p_port->pp_buffers = 0;
555 omx_error = p_sys->pf_free_handle( omx_handle );
559 /*****************************************************************************
560 * InitialiseComponent: Load and initialise an OMX component
561 *****************************************************************************/
562 static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
563 OMX_STRING psz_component, OMX_HANDLETYPE *p_handle)
565 static OMX_CALLBACKTYPE callbacks =
566 { OmxEventHandler, OmxEmptyBufferDone, OmxFillBufferDone };
567 decoder_sys_t *p_sys = p_dec->p_sys;
568 OMX_HANDLETYPE omx_handle;
569 OMX_ERRORTYPE omx_error;
571 OMX_U8 psz_role[OMX_MAX_STRINGNAME_SIZE];
572 OMX_PARAM_COMPONENTROLETYPE role;
573 OMX_PARAM_PORTDEFINITIONTYPE definition;
574 OMX_PORT_PARAM_TYPE param;
577 omx_error = p_sys->pf_get_handle( &omx_handle, psz_component,
579 if(omx_error != OMX_ErrorNone)
581 msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
582 omx_error, ErrorToString(omx_error) );
585 strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
587 omx_error = OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
588 if(omx_error == OMX_ErrorNone)
589 msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
591 msg_Dbg(p_dec, "loaded component %s", psz_component);
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, "OMX_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, "OMX_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, "OMX_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, "OMX_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, "OMX_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, "OMX_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, "OMX_CommandPortEnable on %i failed (%x)",
992 (int)p_port->i_port_index, omx_error );
994 if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
995 free(p_port->pp_buffers);
996 p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
997 if(!p_port->pp_buffers)
999 omx_error = OMX_ErrorInsufficientResources;
1000 CHECK_ERROR(omx_error, "memory allocation failed");
1003 p_port->i_buffers = p_port->definition.nBufferCountActual;
1004 for(i = 0; i < p_port->i_buffers; i++)
1006 if(0 && p_port->b_direct)
1008 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1009 p_port->i_port_index, 0,
1010 p_port->definition.nBufferSize, (void*)1);
1013 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1014 p_port->i_port_index, 0,
1015 p_port->definition.nBufferSize);
1017 if(omx_error != OMX_ErrorNone) break;
1018 OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1020 p_port->i_buffers = i;
1021 CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1022 omx_error, (int)p_port->i_port_index, i );
1024 omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1025 CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1027 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1028 PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1034 /*****************************************************************************
1035 * DecodeVideo: Called to decode one frame
1036 *****************************************************************************/
1037 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1039 decoder_sys_t *p_sys = p_dec->p_sys;
1040 picture_t *p_pic = NULL, *p_next_pic;
1041 OMX_ERRORTYPE omx_error;
1044 OMX_BUFFERHEADERTYPE *p_header;
1047 if( !pp_block || !*pp_block )
1050 p_block = *pp_block;
1052 /* Check for errors from codec */
1055 msg_Dbg(p_dec, "error during decoding");
1056 block_Release( p_block );
1060 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1062 block_Release( p_block );
1063 if(!p_sys->in.b_flushed)
1065 msg_Dbg(p_dec, "flushing");
1066 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1067 p_sys->in.definition.nPortIndex, 0 );
1069 p_sys->in.b_flushed = true;
1073 /* Take care of decoded frames first */
1076 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1077 if(!p_header) break; /* No frame available */
1079 if(p_header->nFilledLen)
1081 p_pic = p_header->pAppPrivate;
1084 /* We're not in direct rendering mode.
1085 * Get a new picture and copy the content */
1086 p_pic = decoder_NewPicture( p_dec );
1087 if( !p_pic ) break; /* No picture available */
1089 CopyOmxPicture(p_dec, p_pic, p_header, p_sys->out.definition.format.video.nSliceHeight);
1092 p_pic->date = p_header->nTimeStamp;
1093 p_header->nFilledLen = 0;
1094 p_header->pAppPrivate = 0;
1097 /* Get a new picture */
1098 if(p_sys->in.b_direct && !p_header->pAppPrivate)
1100 p_next_pic = decoder_NewPicture( p_dec );
1101 if(!p_next_pic) break;
1103 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1104 p_header->pAppPrivate = p_next_pic;
1105 p_header->pInputPortPrivate = p_header->pBuffer;
1106 p_header->pBuffer = p_next_pic->p[0].p_pixels;
1110 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1113 #ifdef OMXIL_EXTRA_DEBUG
1114 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1116 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1119 /* Send the input buffer to the component */
1120 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1122 if (p_header && p_header->nFlags & OMX_BUFFERFLAG_EOS)
1127 p_header->nFilledLen = p_block->i_buffer;
1128 p_header->nOffset = 0;
1129 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1130 p_header->nTimeStamp = p_block->i_dts;
1132 /* In direct mode we pass the input pointer as is.
1133 * Otherwise we memcopy the data */
1134 if(p_sys->in.b_direct)
1136 p_header->pOutputPortPrivate = p_header->pBuffer;
1137 p_header->pBuffer = p_block->p_buffer;
1138 p_header->pAppPrivate = p_block;
1142 if(p_header->nFilledLen > p_header->nAllocLen)
1144 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1145 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1146 p_header->nFilledLen = p_header->nAllocLen;
1148 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1149 block_Release(p_block);
1152 #ifdef OMXIL_EXTRA_DEBUG
1153 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1154 (int)p_header->nFilledLen );
1156 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1157 p_sys->in.b_flushed = false;
1158 *pp_block = NULL; /* Avoid being fed the same packet again */
1162 /* Handle the PortSettingsChanged events */
1163 for(i = 0; i < p_sys->ports; i++)
1165 OmxPort *p_port = &p_sys->p_ports[i];
1166 if(!p_port->b_reconfigure) continue;
1167 p_port->b_reconfigure = 0;
1168 omx_error = PortReconfigure(p_dec, p_port);
1174 /*****************************************************************************
1175 * DecodeAudio: Called to decode one frame
1176 *****************************************************************************/
1177 aout_buffer_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1179 decoder_sys_t *p_sys = p_dec->p_sys;
1180 aout_buffer_t *p_buffer = 0;
1181 OMX_BUFFERHEADERTYPE *p_header;
1182 OMX_ERRORTYPE omx_error;
1186 if( !pp_block || !*pp_block ) return NULL;
1188 p_block = *pp_block;
1190 /* Check for errors from codec */
1193 msg_Dbg(p_dec, "error during decoding");
1194 block_Release( p_block );
1198 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1200 block_Release( p_block );
1201 date_Set( &p_sys->end_date, 0 );
1202 if(!p_sys->in.b_flushed)
1204 msg_Dbg(p_dec, "flushing");
1205 OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1206 p_sys->in.definition.nPortIndex, 0 );
1208 p_sys->in.b_flushed = true;
1212 if( !date_Get( &p_sys->end_date ) )
1214 if( !p_block->i_pts )
1216 /* We've just started the stream, wait for the first PTS. */
1217 block_Release( p_block );
1220 date_Set( &p_sys->end_date, p_block->i_pts );
1223 /* Take care of decoded frames first */
1226 unsigned int i_samples;
1228 OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1229 if(!p_header) break; /* No frame available */
1231 i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1234 p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1235 if( !p_buffer ) break; /* No audio buffer available */
1237 memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1238 p_header->nFilledLen = 0;
1240 if( p_header->nTimeStamp != 0 &&
1241 p_header->nTimeStamp != date_Get( &p_sys->end_date ) )
1242 date_Set( &p_sys->end_date, p_header->nTimeStamp );
1244 p_buffer->i_pts = date_Get( &p_sys->end_date );
1245 p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1249 #ifdef OMXIL_EXTRA_DEBUG
1250 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1252 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1253 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1257 /* Send the input buffer to the component */
1258 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1261 p_header->nFilledLen = p_block->i_buffer;
1262 p_header->nOffset = 0;
1263 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1264 p_header->nTimeStamp = p_block->i_dts;
1266 /* In direct mode we pass the input pointer as is.
1267 * Otherwise we memcopy the data */
1268 if(p_sys->in.b_direct)
1270 p_header->pOutputPortPrivate = p_header->pBuffer;
1271 p_header->pBuffer = p_block->p_buffer;
1272 p_header->pAppPrivate = p_block;
1276 if(p_header->nFilledLen > p_header->nAllocLen)
1278 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1279 (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1280 p_header->nFilledLen = p_header->nAllocLen;
1282 memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1283 block_Release(p_block);
1286 #ifdef OMXIL_EXTRA_DEBUG
1287 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1288 (int)p_header->nFilledLen );
1290 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1291 p_sys->in.b_flushed = false;
1292 *pp_block = NULL; /* Avoid being fed the same packet again */
1295 /* Handle the PortSettingsChanged events */
1296 for(i = 0; i < p_sys->ports; i++)
1298 OmxPort *p_port = &p_sys->p_ports[i];
1299 if(!p_port->b_reconfigure) continue;
1300 p_port->b_reconfigure = 0;
1301 omx_error = PortReconfigure(p_dec, p_port);
1307 /*****************************************************************************
1308 * EncodeVideo: Called to encode one frame
1309 *****************************************************************************/
1310 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1312 decoder_t *p_dec = ( decoder_t *)p_enc;
1313 decoder_sys_t *p_sys = p_dec->p_sys;
1314 OMX_ERRORTYPE omx_error;
1317 OMX_BUFFERHEADERTYPE *p_header;
1318 block_t *p_block = 0;
1320 if( !p_pic ) return NULL;
1322 /* Check for errors from codec */
1325 msg_Dbg(p_dec, "error during encoding");
1329 /* Send the input buffer to the component */
1330 OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1333 /* In direct mode we pass the input pointer as is.
1334 * Otherwise we memcopy the data */
1335 if(p_sys->in.b_direct)
1337 p_header->pOutputPortPrivate = p_header->pBuffer;
1338 p_header->pBuffer = p_pic->p[0].p_pixels;
1342 CopyVlcPicture(p_dec, p_header, p_pic);
1345 p_header->nFilledLen = p_sys->in.i_frame_size;
1346 p_header->nOffset = 0;
1347 p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1348 p_header->nTimeStamp = p_pic->date;
1349 #ifdef OMXIL_EXTRA_DEBUG
1350 msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1351 (int)p_header->nFilledLen );
1353 OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1354 p_sys->in.b_flushed = false;
1357 /* Handle the PortSettingsChanged events */
1358 for(i = 0; i < p_sys->ports; i++)
1360 OmxPort *p_port = &p_sys->p_ports[i];
1361 if(!p_port->b_reconfigure) continue;
1362 p_port->b_reconfigure = 0;
1363 omx_error = PortReconfigure(p_dec, p_port);
1366 /* Wait for the decoded frame */
1369 OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1371 if(p_header->nFilledLen)
1373 if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1375 /* TODO: need to store codec config */
1376 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1379 p_block = p_header->pAppPrivate;
1382 /* We're not in direct rendering mode.
1383 * Get a new block and copy the content */
1384 p_block = block_New( p_dec, p_header->nFilledLen );
1385 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1388 p_block->i_buffer = p_header->nFilledLen;
1389 p_block->i_pts = p_block->i_dts = p_header->nTimeStamp;
1390 p_header->nFilledLen = 0;
1391 p_header->pAppPrivate = 0;
1394 #ifdef OMXIL_EXTRA_DEBUG
1395 msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1397 OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1400 msg_Dbg(p_dec, "done");
1404 /*****************************************************************************
1405 * CloseGeneric: omxil decoder destruction
1406 *****************************************************************************/
1407 static void CloseGeneric( vlc_object_t *p_this )
1409 decoder_t *p_dec = (decoder_t *)p_this;
1410 decoder_sys_t *p_sys = p_dec->p_sys;
1412 if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1413 if(p_sys->b_init) p_sys->pf_deinit();
1414 dll_close( p_sys->dll_handle );
1416 vlc_mutex_destroy (&p_sys->mutex);
1417 vlc_cond_destroy (&p_sys->cond);
1418 vlc_mutex_destroy (&p_sys->in.fifo.lock);
1419 vlc_cond_destroy (&p_sys->in.fifo.wait);
1420 vlc_mutex_destroy (&p_sys->out.fifo.lock);
1421 vlc_cond_destroy (&p_sys->out.fifo.wait);
1426 /*****************************************************************************
1428 *****************************************************************************/
1429 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1430 OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1431 OMX_U32 data_2, OMX_PTR event_data )
1433 decoder_t *p_dec = (decoder_t *)app_data;
1434 decoder_sys_t *p_sys = p_dec->p_sys;
1440 case OMX_EventCmdComplete:
1441 switch ((OMX_STATETYPE)data_1)
1443 case OMX_CommandStateSet:
1444 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %s)", EventToString(event),
1445 CommandToString(data_1), StateToString(data_2) );
1449 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u)", EventToString(event),
1450 CommandToString(data_1), (unsigned int)data_2 );
1455 case OMX_EventError:
1456 msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u, %s)", EventToString(event),
1457 ErrorToString((OMX_ERRORTYPE)data_1), (unsigned int)data_2,
1458 (const char *)event_data);
1459 //p_sys->b_error = true;
1462 case OMX_EventPortSettingsChanged:
1463 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1464 (unsigned int)data_1, (unsigned int)data_2 );
1465 for(i = 0; i < p_sys->ports; i++)
1466 if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1467 p_sys->p_ports[i].b_reconfigure = true;
1468 memset(&p_sys->sentinel_buffer, 0, sizeof(p_sys->sentinel_buffer));
1469 p_sys->sentinel_buffer.nFlags = OMX_BUFFERFLAG_EOS;
1470 OMX_FIFO_PUT(&p_sys->in.fifo, &p_sys->sentinel_buffer);
1474 msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1475 (unsigned int)data_1, (unsigned int)data_2 );
1479 PostOmxEvent(p_dec, event, data_1, data_2, event_data);
1480 return OMX_ErrorNone;
1483 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1484 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1486 decoder_t *p_dec = (decoder_t *)app_data;
1487 decoder_sys_t *p_sys = p_dec->p_sys;
1490 #ifdef OMXIL_EXTRA_DEBUG
1491 msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1494 if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1496 block_t *p_block = (block_t *)omx_header->pAppPrivate;
1497 omx_header->pBuffer = omx_header->pOutputPortPrivate;
1498 if(p_block) block_Release(p_block);
1499 omx_header->pAppPrivate = 0;
1501 OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1503 return OMX_ErrorNone;
1506 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1507 OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1509 decoder_t *p_dec = (decoder_t *)app_data;
1510 decoder_sys_t *p_sys = p_dec->p_sys;
1513 #ifdef OMXIL_EXTRA_DEBUG
1514 msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
1515 (int)omx_header->nFilledLen );
1518 if(omx_header->pInputPortPrivate)
1520 omx_header->pBuffer = omx_header->pInputPortPrivate;
1522 OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1524 return OMX_ErrorNone;