/* Copyright (c) 2012, Broadcom Europe Ltd Copyright (c) 2012, OtherCrashOverride Copyright (C) 2014, Fluendo S.A. @author: Josep Torra All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* Video decode demo using OpenMAX IL though the ilcient helper library */ #include #include #include #include #include "bcm_host.h" #include "ilclient.h" #include "triangle.h" static OMX_BUFFERHEADERTYPE *eglBuffer = NULL; static COMPONENT_T *egl_render = NULL; static void *eglImage = 0; static gboolean running = FALSE; void my_fill_buffer_done (void *data, COMPONENT_T * comp) { if (OMX_FillThisBuffer (ilclient_get_handle (egl_render), eglBuffer) != OMX_ErrorNone) { g_print ("WARNING: OMX_FillThisBuffer failed in callback\n"); } } void * video_decode_test (void *arg) { const char *filename = "/opt/vc/src/hello_pi/hello_video/test.h264"; IPC_T *ipc = (IPC_T *) arg; eglImage = ipc->eglImage; running = TRUE; if (eglImage == 0) { g_print ("eglImage is null.\n"); exit (1); } OMX_VIDEO_PARAM_PORTFORMATTYPE format; COMPONENT_T *video_decode = NULL; COMPONENT_T *list[5]; TUNNEL_T tunnel[4]; ILCLIENT_T *client; FILE *in; int status = 0; unsigned int data_len = 0; memset (list, 0, sizeof (list)); memset (tunnel, 0, sizeof (tunnel)); if ((in = fopen (filename, "rb")) == NULL) return (void *) -2; if ((client = ilclient_init ()) == NULL) { fclose (in); return (void *) -3; } if (OMX_Init () != OMX_ErrorNone) { ilclient_destroy (client); fclose (in); return (void *) -4; } /* callback */ ilclient_set_fill_buffer_done_callback (client, my_fill_buffer_done, 0); /* create video_decode */ if (ilclient_create_component (client, &video_decode, "video_decode", ILCLIENT_DISABLE_ALL_PORTS | ILCLIENT_ENABLE_INPUT_BUFFERS) != 0) status = -14; list[0] = video_decode; /* create egl_render */ if (status == 0 && ilclient_create_component (client, &egl_render, "egl_render", ILCLIENT_DISABLE_ALL_PORTS | ILCLIENT_ENABLE_OUTPUT_BUFFERS) != 0) status = -14; list[1] = egl_render; set_tunnel (tunnel, video_decode, 131, egl_render, 220); if (status == 0) ilclient_change_component_state (video_decode, OMX_StateIdle); memset (&format, 0, sizeof (OMX_VIDEO_PARAM_PORTFORMATTYPE)); format.nSize = sizeof (OMX_VIDEO_PARAM_PORTFORMATTYPE); format.nVersion.nVersion = OMX_VERSION; format.nPortIndex = 130; format.eCompressionFormat = OMX_VIDEO_CodingAVC; if (status == 0 && OMX_SetParameter (ILC_GET_HANDLE (video_decode), OMX_IndexParamVideoPortFormat, &format) == OMX_ErrorNone && ilclient_enable_port_buffers (video_decode, 130, NULL, NULL, NULL) == 0) { OMX_BUFFERHEADERTYPE *buf; int port_settings_changed = 0; int first_packet = 1; ilclient_change_component_state (video_decode, OMX_StateExecuting); while ((buf = ilclient_get_input_buffer (video_decode, 130, 1)) != NULL) { /* feed data and wait until we get port settings changed */ unsigned char *dest = buf->pBuffer; /* loop if at end */ if (feof (in) || !running) { g_print ("EOS\n"); break; } data_len += fread (dest, 1, buf->nAllocLen - data_len, in); if (port_settings_changed == 0 && ((data_len > 0 && ilclient_remove_event (video_decode, OMX_EventPortSettingsChanged, 131, 0, 0, 1) == 0) || (data_len == 0 && ilclient_wait_for_event (video_decode, OMX_EventPortSettingsChanged, 131, 0, 0, 1, ILCLIENT_EVENT_ERROR | ILCLIENT_PARAMETER_CHANGED, 10000) == 0))) { port_settings_changed = 1; { #define OMX_IndexParamBrcmVideoEGLRenderDiscardMode 0x7f0000db OMX_CONFIG_PORTBOOLEANTYPE discardMode; memset (&discardMode, 0, sizeof (discardMode)); discardMode.nSize = sizeof (discardMode); discardMode.nPortIndex = 220; discardMode.nVersion.nVersion = OMX_VERSION; discardMode.bEnabled = OMX_FALSE; if (OMX_SetParameter (ILC_GET_HANDLE (egl_render), OMX_IndexParamBrcmVideoEGLRenderDiscardMode, &discardMode) != OMX_ErrorNone) { status = -7; break; } #undef OMX_IndexParamBrcmVideoEGLRenderDiscardMode } if (ilclient_setup_tunnel (tunnel, 0, 0) != 0) { status = -7; break; } /* Set egl_render to idle */ ilclient_change_component_state (egl_render, OMX_StateIdle); /* Enable the output port and tell egl_render to use the texture as a buffer * ilclient_enable_port(egl_render, 221); THIS BLOCKS SO CANT BE USED */ if (OMX_SendCommand (ILC_GET_HANDLE (egl_render), OMX_CommandPortEnable, 221, NULL) != OMX_ErrorNone) { g_print ("OMX_CommandPortEnable failed.\n"); exit (1); } if (OMX_UseEGLImage (ILC_GET_HANDLE (egl_render), &eglBuffer, 221, NULL, eglImage) != OMX_ErrorNone) { g_print ("OMX_UseEGLImage failed.\n"); exit (1); } /* Set egl_render to executing */ ilclient_change_component_state (egl_render, OMX_StateExecuting); /* Request egl_render to write data to the texture buffer */ if (OMX_FillThisBuffer (ILC_GET_HANDLE (egl_render), eglBuffer) != OMX_ErrorNone) { g_print ("OMX_FillThisBuffer failed.\n"); exit (1); } } if (!data_len) break; buf->nFilledLen = data_len; data_len = 0; buf->nOffset = 0; if (first_packet) { buf->nFlags = OMX_BUFFERFLAG_STARTTIME; first_packet = 0; } else buf->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN; if (OMX_EmptyThisBuffer (ILC_GET_HANDLE (video_decode), buf) != OMX_ErrorNone) { status = -6; break; } } g_print ("Sending EOS buffer\n"); buf->nFilledLen = 0; buf->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN | OMX_BUFFERFLAG_EOS; if (OMX_EmptyThisBuffer (ILC_GET_HANDLE (video_decode), buf) != OMX_ErrorNone) status = -20; g_print ("Pause media processing pipeline\n"); ilclient_state_transition (list, OMX_StatePause); g_print ("Flushing tunnel\n"); /* need to flush the renderer to allow video_decode to disable its input port */ ilclient_flush_tunnels (tunnel, 0); g_print ("Disable video decoder input port\n"); ilclient_disable_port_buffers (video_decode, 130, NULL, NULL, NULL); } fclose (in); g_print ("change media processing pipeline to idle state\n"); ilclient_state_transition (list, OMX_StateIdle); g_print ("Disable tunnels\n"); ilclient_disable_tunnel (tunnel); ilclient_teardown_tunnels (tunnel); g_print ("release components\n"); ilclient_cleanup_components (list); g_print ("deinit OpenMAX\n"); OMX_Deinit (); g_print ("destroy IL client\n"); ilclient_destroy (client); return (void *) status; } void video_decode_stop () { running = FALSE; }