Skip to content

Commit 7d06255

Browse files
Thomas Guillemmstorsjo
authored andcommitted
omxil: factorize FreeBuffers and AllocateBuffers
This also adds a missing call to decoder_DeletePicture when deinitializing the component. Signed-off-by: Martin Storsjö <[email protected]>
1 parent 8f31179 commit 7d06255

File tree

1 file changed

+132
-114
lines changed

1 file changed

+132
-114
lines changed

modules/codec/omxil/omxil.c

Lines changed: 132 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,111 @@ static OMX_ERRORTYPE UpdatePixelAspect(decoder_t *p_dec)
355355
return omx_err;
356356
}
357357

358+
/*****************************************************************************
359+
* AllocateBuffers: Allocate Omx buffers
360+
*****************************************************************************/
361+
static OMX_ERRORTYPE AllocateBuffers(decoder_t *p_dec, OmxPort *p_port)
362+
{
363+
decoder_sys_t *p_sys = p_dec->p_sys;
364+
OMX_ERRORTYPE omx_error = OMX_ErrorUndefined;
365+
OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
366+
unsigned int i;
367+
368+
#ifdef OMXIL_EXTRA_DEBUG
369+
msg_Dbg( p_dec, "AllocateBuffers(%d)", def->eDir );
370+
#endif
371+
372+
p_port->i_buffers = p_port->definition.nBufferCountActual;
373+
374+
p_port->pp_buffers = calloc(p_port->i_buffers, sizeof(OMX_BUFFERHEADERTYPE*));
375+
if( !p_port->pp_buffers )
376+
{
377+
return OMX_ErrorInsufficientResources;
378+
}
379+
380+
for(i = 0; i < p_port->i_buffers; i++)
381+
{
382+
#if 0
383+
#define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
384+
char *p_buf = malloc(p_port->definition.nBufferSize +
385+
p_port->definition.nBufferAlignment);
386+
p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
387+
#endif
388+
389+
if(p_port->b_direct)
390+
omx_error =
391+
OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
392+
p_port->i_port_index, 0,
393+
p_port->definition.nBufferSize, (void*)1);
394+
else
395+
omx_error =
396+
OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
397+
p_port->i_port_index, 0,
398+
p_port->definition.nBufferSize);
399+
400+
if(omx_error != OMX_ErrorNone)
401+
{
402+
p_port->i_buffers = i;
403+
break;
404+
}
405+
OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
406+
}
407+
408+
CHECK_ERROR(omx_error, "AllocateBuffers failed (%x, %i)",
409+
omx_error, (int)p_port->i_port_index );
410+
411+
412+
#ifdef OMXIL_EXTRA_DEBUG
413+
msg_Dbg( p_dec, "AllocateBuffers(%d)::done", def->eDir );
414+
#endif
415+
error:
416+
return omx_error;
417+
}
418+
419+
/*****************************************************************************
420+
* FreeBuffers: Free Omx buffers
421+
*****************************************************************************/
422+
static OMX_ERRORTYPE FreeBuffers(decoder_t *p_dec, OmxPort *p_port)
423+
{
424+
OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
425+
OMX_ERRORTYPE omx_error = OMX_ErrorNone;
426+
OMX_BUFFERHEADERTYPE *p_buffer;
427+
unsigned int i;
428+
429+
#ifdef OMXIL_EXTRA_DEBUG
430+
msg_Dbg( p_dec, "FreeBuffers(%d)", def->eDir );
431+
#endif
432+
433+
for(i = 0; i < p_port->i_buffers; i++)
434+
{
435+
OMX_FIFO_GET(&p_port->fifo, p_buffer);
436+
if (p_buffer->pAppPrivate != NULL)
437+
decoder_DeletePicture( p_dec, p_buffer->pAppPrivate );
438+
if (p_buffer->nFlags & SENTINEL_FLAG) {
439+
free(p_buffer);
440+
i--;
441+
continue;
442+
}
443+
omx_error = OMX_FreeBuffer( p_port->omx_handle,
444+
p_port->i_port_index, p_buffer );
445+
446+
if(omx_error != OMX_ErrorNone) break;
447+
}
448+
if( omx_error != OMX_ErrorNone )
449+
msg_Err( p_dec, "OMX_FreeBuffer failed (%x, %i, %i)",
450+
omx_error, (int)p_port->i_port_index, i );
451+
452+
p_port->i_buffers = 0;
453+
free( p_port->pp_buffers );
454+
p_port->pp_buffers = NULL;
455+
456+
#ifdef OMXIL_EXTRA_DEBUG
457+
msg_Dbg( p_dec, "FreeBuffers(%d)::done", def->eDir );
458+
#endif
459+
460+
return omx_error;
461+
}
462+
358463
/*****************************************************************************
359464
* GetPortDefinition: set vlc format based on the definition of the omx port
360465
*****************************************************************************/
@@ -505,9 +610,10 @@ static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
505610
OMX_HANDLETYPE omx_handle)
506611
{
507612
decoder_sys_t *p_sys = p_dec->p_sys;
613+
OMX_BUFFERHEADERTYPE *p_buffer;
508614
OMX_ERRORTYPE omx_error;
509615
OMX_STATETYPE state;
510-
unsigned int i, j;
616+
unsigned int i;
511617

512618
if(!omx_handle) return OMX_ErrorNone;
513619

@@ -542,34 +648,10 @@ static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
542648
for(i = 0; i < p_sys->ports; i++)
543649
{
544650
OmxPort *p_port = &p_sys->p_ports[i];
545-
OMX_BUFFERHEADERTYPE *p_buffer;
546-
547-
for(j = 0; j < p_port->i_buffers; j++)
548-
{
549-
OMX_FIFO_GET(&p_port->fifo, p_buffer);
550-
if (p_buffer->nFlags & SENTINEL_FLAG) {
551-
free(p_buffer);
552-
j--;
553-
continue;
554-
}
555-
omx_error = OMX_FreeBuffer( omx_handle,
556-
p_port->i_port_index, p_buffer );
557651

558-
if(omx_error != OMX_ErrorNone) break;
559-
}
560-
CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
561-
omx_error, (int)p_port->i_port_index, j );
562-
while (1) {
563-
OMX_FIFO_PEEK(&p_port->fifo, p_buffer);
564-
if (!p_buffer) break;
565-
566-
OMX_FIFO_GET(&p_port->fifo, p_buffer);
567-
if (p_buffer->nFlags & SENTINEL_FLAG) {
568-
free(p_buffer);
569-
continue;
570-
}
571-
msg_Warn( p_dec, "Stray buffer left in fifo, %p", p_buffer );
572-
}
652+
omx_error = FreeBuffers( p_dec, p_port );
653+
CHECK_ERROR(omx_error, "FreeBuffers failed (%x, %i)",
654+
omx_error, (int)p_port->i_port_index );
573655
}
574656

575657
omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
@@ -582,6 +664,18 @@ static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
582664
OmxPort *p_port = &p_sys->p_ports[i];
583665
free(p_port->pp_buffers);
584666
p_port->pp_buffers = 0;
667+
668+
while (1) {
669+
OMX_FIFO_PEEK(&p_port->fifo, p_buffer);
670+
if (!p_buffer) break;
671+
672+
OMX_FIFO_GET(&p_port->fifo, p_buffer);
673+
if (p_buffer->nFlags & SENTINEL_FLAG) {
674+
free(p_buffer);
675+
continue;
676+
}
677+
msg_Warn( p_dec, "Stray buffer left in fifo, %p", p_buffer );
678+
}
585679
}
586680
omx_error = pf_free_handle( omx_handle );
587681
return omx_error;
@@ -740,16 +834,6 @@ static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
740834
{
741835
OmxPort *p_port = &p_sys->p_ports[i];
742836

743-
p_port->pp_buffers =
744-
malloc(p_port->definition.nBufferCountActual *
745-
sizeof(OMX_BUFFERHEADERTYPE*));
746-
if(!p_port->pp_buffers)
747-
{
748-
omx_error = OMX_ErrorInsufficientResources;
749-
CHECK_ERROR(omx_error, "memory allocation failed");
750-
}
751-
p_port->i_buffers = p_port->definition.nBufferCountActual;
752-
753837
/* Enable port */
754838
if(!p_port->definition.bEnabled)
755839
{
@@ -825,7 +909,7 @@ static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
825909
decoder_sys_t *p_sys;
826910
OMX_ERRORTYPE omx_error;
827911
OMX_BUFFERHEADERTYPE *p_header;
828-
unsigned int i, j;
912+
unsigned int i;
829913

830914
if (InitOmxCore(p_this) != VLC_SUCCESS) {
831915
return VLC_EGENERIC;
@@ -935,33 +1019,9 @@ static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
9351019
for(i = 0; i < p_sys->ports; i++)
9361020
{
9371021
OmxPort *p_port = &p_sys->p_ports[i];
938-
939-
for(j = 0; j < p_port->i_buffers; j++)
940-
{
941-
#if 0
942-
#define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
943-
char *p_buf = malloc(p_port->definition.nBufferSize +
944-
p_port->definition.nBufferAlignment);
945-
p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
946-
#endif
947-
948-
if(p_port->b_direct)
949-
omx_error =
950-
OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
951-
p_port->i_port_index, 0,
952-
p_port->definition.nBufferSize, (void*)1);
953-
else
954-
omx_error =
955-
OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
956-
p_port->i_port_index, 0,
957-
p_port->definition.nBufferSize);
958-
959-
if(omx_error != OMX_ErrorNone) break;
960-
OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
961-
}
962-
p_port->i_buffers = j;
963-
CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
964-
omx_error, (int)p_port->i_port_index, j );
1022+
omx_error = AllocateBuffers( p_dec, p_port );
1023+
CHECK_ERROR(omx_error, "AllocateBuffers failed (%x, %i)",
1024+
omx_error, (int)p_port->i_port_index );
9651025
}
9661026

9671027
omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
@@ -1069,9 +1129,7 @@ static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
10691129
{
10701130
decoder_sys_t *p_sys = p_dec->p_sys;
10711131
OMX_PARAM_PORTDEFINITIONTYPE definition;
1072-
OMX_BUFFERHEADERTYPE *p_buffer;
10731132
OMX_ERRORTYPE omx_error;
1074-
unsigned int i;
10751133

10761134
/* Sanity checking */
10771135
OMX_INIT_STRUCTURE(definition);
@@ -1088,23 +1146,9 @@ static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
10881146
CHECK_ERROR(omx_error, "OMX_CommandPortDisable on %i failed (%x)",
10891147
(int)p_port->i_port_index, omx_error );
10901148

1091-
for(i = 0; i < p_port->i_buffers; i++)
1092-
{
1093-
OMX_FIFO_GET(&p_port->fifo, p_buffer);
1094-
if (p_buffer->pAppPrivate != NULL)
1095-
decoder_DeletePicture( p_dec, p_buffer->pAppPrivate );
1096-
if (p_buffer->nFlags & SENTINEL_FLAG) {
1097-
free(p_buffer);
1098-
i--;
1099-
continue;
1100-
}
1101-
omx_error = OMX_FreeBuffer( p_sys->omx_handle,
1102-
p_port->i_port_index, p_buffer );
1103-
1104-
if(omx_error != OMX_ErrorNone) break;
1105-
}
1106-
CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
1107-
omx_error, (int)p_port->i_port_index, i );
1149+
omx_error = FreeBuffers( p_dec, p_port );
1150+
CHECK_ERROR(omx_error, "FreeBuffers failed (%x, %i)",
1151+
omx_error, (int)p_port->i_port_index );
11081152

11091153
omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
11101154
CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
@@ -1137,35 +1181,9 @@ static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
11371181
CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
11381182
(int)p_port->i_port_index, omx_error );
11391183

1140-
if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
1141-
free(p_port->pp_buffers);
1142-
p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
1143-
if(!p_port->pp_buffers)
1144-
{
1145-
omx_error = OMX_ErrorInsufficientResources;
1146-
CHECK_ERROR(omx_error, "memory allocation failed");
1147-
}
1148-
}
1149-
p_port->i_buffers = p_port->definition.nBufferCountActual;
1150-
for(i = 0; i < p_port->i_buffers; i++)
1151-
{
1152-
if(p_port->b_direct)
1153-
omx_error =
1154-
OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1155-
p_port->i_port_index, 0,
1156-
p_port->definition.nBufferSize, (void*)1);
1157-
else
1158-
omx_error =
1159-
OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1160-
p_port->i_port_index, 0,
1161-
p_port->definition.nBufferSize);
1162-
1163-
if(omx_error != OMX_ErrorNone) break;
1164-
OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1165-
}
1166-
p_port->i_buffers = i;
1167-
CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1168-
omx_error, (int)p_port->i_port_index, i );
1184+
omx_error = AllocateBuffers( p_dec, p_port );
1185+
CHECK_ERROR(omx_error, "OMX_AllocateBuffers failed (%x, %i)",
1186+
omx_error, (int)p_port->i_port_index );
11691187

11701188
omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
11711189
CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );

0 commit comments

Comments
 (0)