NOTICE: The Processors Wiki will End-of-Life on January 15, 2021. It is recommended to download any files or other content you may need that are hosted on processors.wiki.ti.com. The site is now set to read only.

Code

From Texas Instruments Wiki
Jump to: navigation, search

<syntaxhighlight lang="c"> /*

* Copyright (c) 2010, Texas Instruments Incorporated
* 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 Texas Instruments Incorporated 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 OWNER 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.
*/
/** file implements video decode IL client */
#include "viddec.h"
#include "h264parse.h"
#include "common.h"
#define COMPONENT_NAME_BASE "OMX.TI.DUCATI.VIDDEC"
#define BASE_ROLE ""
#define XPAD    32
#define YPAD    24
H264_ParsingCtx* H264Pc;
FILE *ifptr, *ofptr;
VIDDEC  *app;
int image_width, image_height, output_size;
char *tmpBuf;
int num_frames;
static int h264parse_read (H264_ParsingCtx* pc, OMX_BUFFERHEADERTYPE* pBuf)
{
   OMX_S32 nRead = 0;
   OMX_U32 framesizetoread = 0;
   pc->outBuf.ptr = pBuf->pBuffer;
   pc->outBuf.bufsize = pBuf->nAllocLen;
   pc->outBuf.bufused = 0;
   nRead = videdecGetNextFrameSize(pc);
   pBuf->nFilledLen = nRead;
   pBuf->nOffset = 0;
   return nRead;
}
OMX_ERRORTYPE EventHandler(
 OMX_OUT OMX_HANDLETYPE hComponent,
 OMX_OUT OMX_PTR pAppData,
 OMX_OUT OMX_EVENTTYPE eEvent,
 OMX_OUT OMX_U32 Data1,
 OMX_OUT OMX_U32 Data2,
 OMX_OUT OMX_PTR pEventData)
 {
   DLOG ("got event");
   if(eEvent == OMX_EventCmdComplete) {
       if (Data1 == OMX_CommandStateSet) {
           DLOG ("State changed to: ");
           switch ((int)Data2) {
               case OMX_StateInvalid:
                   DLOG ("OMX_StateInvalid");
                   break;
               case OMX_StateLoaded:
                   DLOG ("OMX_StateLoaded");
                   break;
               case OMX_StateIdle:
                   DLOG ("OMX_StateIdle");
                   break;
               case OMX_StateExecuting:
                   DLOG ("OMX_StateExecuting");
                   break;
               case OMX_StatePause:
                   DLOG ("OMX_StatePause");
                   break;
               case OMX_StateWaitForResources:
                   DLOG ("OMX_StateWaitForResources");
                   break;
           }
           tsem_up(app->done_sem);
       }
       else if (OMX_CommandPortEnable || OMX_CommandPortDisable) {
           DLOG ("Enable/Disable Event");
           tsem_up(app->port_sem);
       }
   }
   else if(eEvent == OMX_EventBufferFlag) {
       DLOG ("OMX_EventBufferFlag");
       if((int)Data2 == OMX_BUFFERFLAG_EOS) {
           printf ("got EOS event \n");
           tsem_up(app->eos);
       }
   }
   else if (eEvent == OMX_EventError) {
       printf ("*** unrecoverable error: %s (0x%lx) \n", omx_error_to_str (Data1), Data1);
       printf ("Press a key to proceed\n");
       getchar ();
   }
   else {
       DLOG ("unhandled event, param1 = %i, param2 = %i", (int)Data1, (int)Data2);
   }
   return OMX_ErrorNone;
}
static OMX_ERRORTYPE
FillBufferDone (OMX_HANDLETYPE omx_handle, OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_buffer)
{
   int size;
   static unsigned int num_frames_decoded = 0;
   DLOG ("FBD: address %p", omx_buffer);
   DLOG ("executing SW CSC: YUV420PSEMI -> YUV420P");
   size = convert_420psemi_to_420p (omx_buffer->pBuffer, XPAD, YPAD, image_width, image_height,
       omx_buffer->nFilledLen, tmpBuf);
   num_frames_decoded++;
   printf ("wrote %d bytes \n for %dth frame", size, num_frames_decoded);
   fwrite (tmpBuf, size, 1, ofptr);
   DLOG ("FTB: address %p", omx_buffer);
   OMX_FillThisBuffer (omx_handle, omx_buffer);
   return OMX_ErrorNone;
 }
 static OMX_ERRORTYPE
 EmptyBufferDone (OMX_HANDLETYPE omx_handle, OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_buffer)
 {
   int len;
   DLOG ("EBD: address %p, size %d", omx_buffer, omx_buffer->nFilledLen);
   omx_buffer->nFilledLen = h264parse_read (H264Pc, omx_buffer);
   if (omx_buffer->nFilledLen == 0)
       omx_buffer->nFlags = OMX_BUFFERFLAG_EOS;
   if (num_frames > 0)
   {
       num_frames --;
       if (num_frames == 0)
           omx_buffer->nFlags = OMX_BUFFERFLAG_EOS;
   }
   DLOG ("ETB: address %p, size %d", omx_buffer, omx_buffer->nFilledLen);
   OMX_EmptyThisBuffer (omx_handle, omx_buffer);
   return OMX_ErrorNone;
 }


OMX_CALLBACKTYPE videodeccallbacks = {
   .EventHandler = EventHandler,
   .EmptyBufferDone = EmptyBufferDone,
   .FillBufferDone = FillBufferDone
};



    int decode (Args *argsp) {
     OMX_ERRORTYPE err;
      OMX_PARAM_PORTDEFINITIONTYPE paramPort;
      int i;
      int input_port_index = 0, output_port_index = 1;/* initialize private data structure */
      ifptr = fopen (argsp->input_file, "rb");
     if (ifptr == NULL)
       ERROR ("failed to open %s", argsp->input_file);
     ofptr = fopen (argsp->output_file, "wb");
   if (ofptr == NULL)
       ERROR ("failed to open %s", argsp->output_file);
     app = malloc (sizeof(VIDDEC));
   app->eos = malloc (sizeof(tsem_t));
   tsem_init(app->eos, 0);
   app->done_sem = malloc (sizeof(tsem_t));
   tsem_init(app->done_sem, 0);
   app->port_sem = malloc (sizeof(tsem_t));
   tsem_init(app->port_sem, 0);
   image_width = argsp->width;
   image_height = argsp->height;
   output_size = image_width * image_height * 1.5;
   tmpBuf = malloc (output_size);
   num_frames = argsp->num_frames;
   /* initialize openmax core libs */
   OMX_Init();
   /* getting video decoder handle */
   err = OMX_GetHandle(&app->omx_handle, COMPONENT_NAME_BASE, NULL, &videodeccallbacks);
   if(err != OMX_ErrorNone)
       ERROR ("failed to get handle\n");
   printf ("found handle %p for component %s\n", app->omx_handle, COMPONENT_NAME_BASE);
   printf ("Press Key now!!\n");
   getchar ();
   /* get component roles */
   #if 0 /*  not supported in EZSDK 5.01.00.77 release */
   err = OMX_GetParameter(appPriv->omx_handle, OMX_IndexParamStandardComponentRole, &paramRole);
   #endif
   /* set output height/width and color format */
   OMX_PORT_GET_DEFINITION (app->omx_handle, output_port_index, &paramPort);
   paramPort.nPortIndex = output_port_index;
   paramPort.format.video.nFrameWidth = argsp->width;
   paramPort.format.video.nFrameHeight = argsp->height;
   paramPort.format.video.eCompressionFormat = OMX_VIDEO_CodingUnused;
   paramPort.format.video.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
   DLOG ("set output port params (width = %d, height = %d)", argsp->width, argsp->height);
   OMX_PORT_SET_DEFINITION (app->omx_handle, output_port_index, &paramPort);
   /* enable input and output port */
   /* FIXME: as per openmax specs all the ports should be enabled by default but EZSDK OMX
    * component does not enable it hence we manually need to enable it.
    */
   DLOG ("enable input port");
   OMX_SendCommand (app->omx_handle, OMX_CommandPortEnable, input_port_index, NULL);
   tsem_down (app->port_sem);
   DLOG ("enable output port");
   OMX_SendCommand (app->omx_handle, OMX_CommandPortEnable, output_port_index, NULL);
   tsem_down (app->port_sem);
   /* sending command to video decoder component to go to idle state */
   printf ("sending OMX_StateIdle command \n");
   err = OMX_SendCommand(app->omx_handle, OMX_CommandStateSet, OMX_StateIdle, NULL);
   if(err != OMX_ErrorNone)
       ERROR ("failed to send StateIdle command\n");
   /* allocate input buffer */
   printf ("allocating input buffers \n");
   OMX_PORT_GET_DEFINITION (app->omx_handle, input_port_index, &paramPort);
   app->num_input_buffer = paramPort.nBufferCountActual;
   app->input_buffer_size = paramPort.nBufferSize;
   app->input_buffer = malloc (app->num_input_buffer);
   if (app->input_buffer == NULL)
       ERROR ("failed to allocate memory");
   for (i=0; i < app->num_input_buffer; i++) {
      OMX_AllocateBuffer (app->omx_handle, &app->input_buffer[i], input_port_index, NULL,
           app->input_buffer_size);
   if (app->input_buffer[i] == NULL)
           ERROR ("failed to allocate input buffer, index = %d, size = %d", i, app->input_buffer_size);
   DLOG ("Allocated input buffer, index =  %d, size = %ld, address = %p", i,
           app->input_buffer_size, app->input_buffer[i]);
   }
   /* allocate output buffer */
   printf ("allocating output buffers\n");
   OMX_PORT_GET_DEFINITION (app->omx_handle, output_port_index, &paramPort);
   app->num_output_buffer = paramPort.nBufferCountActual;
   app->output_buffer_size = paramPort.nBufferSize;
   app->output_buffer = malloc (app->num_output_buffer);
   if (app->output_buffer == NULL)
       ERROR ("failed to allocate memory");
   for (i=0; i < app->num_output_buffer; i++) {
       OMX_AllocateBuffer (app->omx_handle, &app->output_buffer[i], output_port_index, NULL,
           app->output_buffer_size);
   if (app->output_buffer[i] == NULL)
           ERROR ("failed to allocate output buffer, index = %d, size = %d", i, app->output_buffer_size);
   DLOG ("Allocated output buffer, index =  %d, size = %ld, address = %p", i,
           app->output_buffer_size, app->output_buffer[i]);
   }
   /* waiting for idle state transition */
   tsem_down(app->done_sem);
   /* sending command to video decoder component to go to executing state */
   printf ("sending OMX_StateExecuting command\n");
   err = OMX_SendCommand(app->omx_handle, OMX_CommandStateSet, OMX_StateExecuting, NULL);
   if(err != OMX_ErrorNone)
       ERROR ("failed to send OMX_StateExecuting command\n");
   /* waiting for executing state transition */
   tsem_down(app->done_sem);
   /* queue the output buffers */
   for (i=0; i < app->num_output_buffer; i++) {
       DLOG ("FTB: address %p, size = %d", app->output_buffer[i], app->output_buffer_size);
       OMX_FillThisBuffer(app->omx_handle, app->output_buffer[i]);
   }
   H264Pc = (H264_ParsingCtx *)malloc (sizeof(H264_ParsingCtx));
   if (NULL == H264Pc)
       ERROR ("failed to malloc memory for Parser Context \n");
   DLOG ("init h264 parser");
   Decode_ParserInit (H264Pc, ifptr);
   /* parse input data read from file and queue the buffers */
   for (i=0; i < app->num_input_buffer; i++) {
       app->input_buffer[i]->nFilledLen = h264parse_read (H264Pc, app->input_buffer[i]);
       DLOG ("ETB: address %p, size = %d", app->input_buffer[i], app->input_buffer[i]->nFilledLen);
       OMX_EmptyThisBuffer(app->omx_handle, app->input_buffer[i]);
   }
   tsem_down(app->eos);
   printf ("executing freeing resources section ..\n");
}

</syntaxhighlight>

E2e.jpg {{
  1. switchcategory:MultiCore=
  • For technical support on MultiCore devices, please post your questions in the C6000 MultiCore Forum
  • For questions related to the BIOS MultiCore SDK (MCSDK), please use the BIOS Forum

Please post only comments related to the article Code here.

Keystone=
  • For technical support on MultiCore devices, please post your questions in the C6000 MultiCore Forum
  • For questions related to the BIOS MultiCore SDK (MCSDK), please use the BIOS Forum

Please post only comments related to the article Code here.

C2000=For technical support on the C2000 please post your questions on The C2000 Forum. Please post only comments about the article Code here. DaVinci=For technical support on DaVincoplease post your questions on The DaVinci Forum. Please post only comments about the article Code here. MSP430=For technical support on MSP430 please post your questions on The MSP430 Forum. Please post only comments about the article Code here. OMAP35x=For technical support on OMAP please post your questions on The OMAP Forum. Please post only comments about the article Code here. OMAPL1=For technical support on OMAP please post your questions on The OMAP Forum. Please post only comments about the article Code here. MAVRK=For technical support on MAVRK please post your questions on The MAVRK Toolbox Forum. Please post only comments about the article Code here. For technical support please post your questions at http://e2e.ti.com. Please post only comments about the article Code here.

}}

Hyperlink blue.png Links

Amplifiers & Linear
Audio
Broadband RF/IF & Digital Radio
Clocks & Timers
Data Converters

DLP & MEMS
High-Reliability
Interface
Logic
Power Management

Processors

Switches & Multiplexers
Temperature Sensors & Control ICs
Wireless Connectivity