This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to change the demo h264encoderapp.c only for using H.264 encoder CODEC engine

Hello,

   We change the demo h264encoderapp.c only for using H.264 encoder CODEC engine, that is to say, a based and simple program for using H.264 Codec engine.
But we find the function
  if ((handle = H264VENC_create(&fxns, &params)) == NULL) in demo h264encoderapp.c, but this function hasnt a in port for handle_engine,


a)
    1)
 hEngine             = NULL;

 /* Initialize Codec Engine runtime */
 CERuntime_init();

 /* Initialize Davinci Multimedia Application Interface */
 Dmai_init();
 /* Reset, load, and start DSP Engine */

 hEngine = Engine_open("encode", NULL, NULL);
 if (hEngine == NULL) {
  printf("Failed to open codec engine \n");
  return -1;
  }
 printf("Encode demo started.\n");
2)
   ((handle = H264VENC_create(&fxns, &params)) == NULL

 The problem is how can we link H264VENC_create(&fxns, &params)) and hEngine = Engine_open("encode", NULL, NULL);,
and how  to write a  H264VENC_create() function link to Engine_open("encode", NULL, NULL);.

b)
 fxns = H264VENC_TI_IH264VENC;

 Does it links to h.264_encoder.cfg?


 Thank you very much,waiting for your answers,

 Zhou

The program is the H.264 encoder Codec engine foe modified  the demo h264encoderapp.c .

IH264VENC_Handle nhandle;

char   *algName = "h264enc1";

int h264enc_init(int max_w, int max_h)
{
    XDAS_Int32 iErrorFlag;    /* Local Variables to Keep track of errors     */
    XDAS_Int8  bSEIFlagOut;    /* Switch for adding SEI userdata */
    IRES_Status iresStatus;
    XDAS_Int8  lib_version[400] = "LIB";   /* Library version number */
    XDAS_Int32 configFileCount =0;
 hEngine             = NULL;

 /* Initialize Codec Engine runtime */
 CERuntime_init();

 /* Initialize Davinci Multimedia Application Interface */
 Dmai_init();
 /* Reset, load, and start DSP Engine */

 hEngine = Engine_open("encode", NULL, NULL);
 if (hEngine == NULL) {
  printf("Failed to open codec engine \n");
  return -1;
  }
 printf("Encode demo started.\n");

    LockMP_init();
    SemMP_init();
    Memory_init();

#ifdef ENABLE_CACHE
    ARM926_Set_MMU_Base();
    ARM926_Set_Domains();
    ARM926_Enable_MMU();
    ARM926_Icache_Enable();
    ARM926_Flush_Icache();
    ARM926_Cache_Set_RoundRobin();
    ARM926_Dcache_Enable();
    ARM926_CleanFlush_Dcache();
#else
    ARM926_Icache_Disable();
    ARM926_Dcache_Disable();
#endif //ENABLE_CACHE

    bSEIFlagOut = 0;

    /* Intialize function table pointer to the function table implementation for
     * H.264 Encoder by TI.
     */
    fxns = H264VENC_TI_IH264VENC;

    /* Initialization and allocation */
/*    CMEM_init();
    memParams.type=CMEM_POOL;
    memParams.flags=CMEM_CACHED;
    memParams.alignment=256;
    media=CMEM_alloc((EXTERNAL_DATA_MEM_SIZE), &memParams);
*/
    /*--------------------------------------------------------------------*/
    /* IRES related functions */
    /*--------------------------------------------------------------------*/
    iresStatus = (IRES_Status) RMAN_init();
    if (IRES_OK != iresStatus) {
        printf("RMAN initialization Failed \n");
        return -1;
    }
    {
    IRESMAN_Edma3ChanParams configParams;
    /*
     * Supply initialization information for the EDMA3 RESMAN while registering
     */
    configParams.baseConfig.allocFxn = RMAN_PARAMS.allocFxn;
    configParams.baseConfig.freeFxn = RMAN_PARAMS.freeFxn;
    configParams.baseConfig.size = sizeof(IRESMAN_Edma3ChanParams);

    /* Register the EDMA3CHAN protocol/resource manager with the
     * generic resource manager */

    iresStatus = (IRES_Status) RMAN_register(&IRESMAN_EDMA3CHAN,
                               (IRESMAN_Params *)&configParams);
    if (IRES_OK != iresStatus) {
        printf("EDMA3 Protocol Registration Failed \n");
        return -1;
    }
    }
    {
    IRESMAN_VicpParams configParams;
    /*
     * Supply initialization information for the RESMAN while registering
     */
    configParams.baseConfig.allocFxn = RMAN_PARAMS.allocFxn;
    configParams.baseConfig.freeFxn = RMAN_PARAMS.freeFxn;
    configParams.baseConfig.size = sizeof(IRESMAN_VicpParams);

    /* Register the VICP protocol/resource manager with the
     * generic resource manager */
   iresStatus = (IRES_Status)RMAN_register(&IRESMAN_VICP2,
                                            (IRESMAN_Params *)&configParams);

    if (IRES_OK != iresStatus) {
        printf("VICP Protocol Registration Failed \n");
        return -1;
    }
    }

    {
        IRESMAN_HdVicpParams configParams;
    /*
     * Supply initialization information for the RESMAN while registering
     */
    configParams.baseConfig.allocFxn = RMAN_PARAMS.allocFxn;
    configParams.baseConfig.freeFxn = RMAN_PARAMS.freeFxn;
    configParams.baseConfig.size = sizeof(IRESMAN_HdVicpParams);
    configParams.numResources = 1;
    /* Register the VICP protocol/resource manager with the
     * generic resource manager */
   iresStatus = (IRES_Status)RMAN_register(&IRESMAN_HDVICP,
                                            (IRESMAN_Params *)&configParams);

    if (IRES_OK != iresStatus) {
        printf("VICP Protocol Registration Failed \n");
        return -1;
    }
    }

 {
 IRESMAN_AddrSpaceParams addrspaceConfigParams;
    /*
     * Supply initialization information for the ADDRSPACE RESMAN while registering
     *           */
    addrspaceConfigParams.baseConfig.allocFxn = RMAN_PARAMS.allocFxn;
    addrspaceConfigParams.baseConfig.freeFxn = RMAN_PARAMS.freeFxn;
    addrspaceConfigParams.baseConfig.size = sizeof(IRESMAN_AddrSpaceParams);;

    iresStatus = RMAN_register(&IRESMAN_ADDRSPACE, (IRESMAN_Params *)&addrspaceConfigParams);

    if (IRES_OK != iresStatus) {
        printf("ADDRSPACE Protocol Registration Failed \n");
        return -1;
    }
    printf("ADDRSPACE Protocol Registration Success ****************************\n");
 }

 {
 IRESMAN_MemTcmParams memTcmConfigParams;
    /*
     * Supply initialization information for the ADDRSPACE RESMAN while registering
     *           */
    memTcmConfigParams.baseConfig.allocFxn = RMAN_PARAMS.allocFxn;
    memTcmConfigParams.baseConfig.freeFxn = RMAN_PARAMS.freeFxn;
    memTcmConfigParams.baseConfig.size = sizeof(IRESMAN_MemTcmParams);;

    iresStatus = RMAN_register(&IRESMAN_MEMTCM, (IRESMAN_Params *)&memTcmConfigParams);

    if (IRES_OK != iresStatus) {
        printf("MEMTCM Protocol Registration Failed \n");
        return -1;
    }
    printf("MEMTCM Protocol Registration Success \n");
 }

        /* Point the param pointer to default parameters from encoder */
        params = H264VENC_PARAMS;


        /*--------------------------------------------------------------------*/
        /*                        H.264 ENCODER INSTANCE CREATION             */
        /*--------------------------------------------------------------------*/
        /*
         * The process of instance creation has two parts:
         * - Setting up the creation time Parameters
         * - Calling the XDAIS Create function to create the instance
         */
        /*
         * Set the Create-Time parameters required for setting up the instance
         * of the H.264 Encoder.
         *
         * As standardized by XDAIS, the structure defining the create time
         * params is named as \b imod_Params \b and is defined in the file
         * \b imod.h \b.
         *
         * So for this module the create-time parameter structure is
         * \b iH264VENC_Params \b defined in \b ih264enc.h \b.
         * XDAIS also provides default create-time parameter in \b mod_PARAMS
         * (H264VENC_PARAMS here)\b defined in \b imod.c (ih264enc.c here) \b
         * which can be used for most scenarios.
         *
         * The code below uses the default create-time parameter set
         * H264VENC_PARAMS and then modifes few of the fields as necessary
         */

        /* Function for seeting initial parameters. Currently not defined, will
         * defined later.
         * H264VENC_setinitparmas(&params);
         */


        /*--------------------------------------------------------------------*/
        /*                       DYNAMIC PARAMETERS INTIALIZATION             */
        /*--------------------------------------------------------------------*/
        dynamicparams.videncDynamicParams.size = sizeof(IH264VENC_DynamicParams);
 bSEIFlagOut = bSEIFlagOut & 1;

        dynamicparams.videncDynamicParams.inputHeight = IN_HEIGHT;
        dynamicparams.videncDynamicParams.inputWidth = IN_WIDTH;

        dynamicparams.videncDynamicParams.refFrameRate = 30000;//
 dynamicparams.videncDynamicParams.targetFrameRate = 30000;
        dynamicparams.videncDynamicParams.targetBitRate = 8000000;

        dynamicparams.videncDynamicParams.intraFrameInterval = 30;
        dynamicparams.videncDynamicParams.generateHeader = 0;
        dynamicparams.videncDynamicParams.captureWidth = IN_WIDTH; //(uiFrmPitch << params.videncParams.inputContentType);

        dynamicparams.videncDynamicParams.forceFrame = IVIDEO_NA_FRAME;
        dynamicparams.videncDynamicParams.interFrameInterval = 0;
        dynamicparams.videncDynamicParams.mbDataFlag = 0;

        dynamicparams.intraSliceNum = 0;
 dynamicparams.enablePicTimSEI = 1;// dynamicparams.enableBufSEI;//EnableBufSEI
 dynamicparams.resetHDVICPeveryFrame = 0;
 dynamicparams.disableMVDCostFactor = 0;         numTicksPerFrame = ((params.timeScale/params.numUnitsInTicks) * 1000)/(dynamicparams.videncDynamicParams.targetFrameRate);
  /*!
   * Additional elements specific to H.264 Encoder - Extensions to base class
   */

 dynamicparams.sliceSize = 0;
 dynamicparams.airRate = 0;
 dynamicparams.intraFrameQP = 21;
 dynamicparams.interPFrameQP = 21;
 dynamicparams.initQ = 28;
 dynamicparams.rcQMax = 51;
 dynamicparams.rcQMin = 0;
 dynamicparams.rcAlgo = 0;
 dynamicparams.maxDelay = 1000;
 dynamicparams.intraSliceNum = 30;
 dynamicparams.lfDisableIdc = 0;
 dynamicparams.meMultiPart = 0;
 dynamicparams.enableBufSEI = 1;
 dynamicparams.enablePicTimSEI = 1;
 dynamicparams.intraThrQF = 0;//reserved
 dynamicparams.perceptualRC = 1;// This is set 0 for a better performance
 dynamicparams.idrFrameInterval = 300;// for syn.
 dynamicparams.mvSADoutFlag = 0;
 dynamicparams.resetHDVICPeveryFrame = 0;
 dynamicparams.disableMVDCostFactor = 0;

 configFileCount = 1;
  if(configFileCount == 1)
  {
  /*--------------------------------------------------------------------*/
  /*                       STATIC PARAMETERS INTIALIZATION              */
  /*--------------------------------------------------------------------*/
        params.videncParams.size = sizeof(IH264VENC_Params);
        params.videncParams.maxWidth = MAX_WIDTH;//IN_WIDTH;
        params.videncParams.maxHeight = MAX_HEIGHT;//IN_HEIGHT;
 params.videncParams.maxFrameRate = 30000; // 60 fps is not supported
 params.videncParams.maxBitRate = 10000000; //10M
 params.videncParams.dataEndianness = XDM_BYTE;// 1 is ok
 params.videncParams.maxInterFrameInterval = 1;

 params.videncParams.inputChromaFormat = XDM_YUV_420SP;
 params.videncParams.inputContentType = IVIDEO_PROGRESSIVE;
 params.videncParams.reconChromaFormat= XDM_YUV_420SP;


  /*!
   * Additional elements specific to H.264 Encoder - Extensions to base class
   */

  params.profileIdc = 100;
 params.levelIdc = 50;
 params.entropyMode = 1;
 params.transform8x8FlagIntraFrame = 1;
 params.transform8x8FlagInterFrame = 1;
 params.aspectRatioX = 1;
 params.aspectRatioY = 1;
 params.pixelRange = 1;
 params.timeScale = 60;
 params.numUnitsInTicks = 1;
 params.enableVUIparams = 1;
 params.disableHDVICPeveryFrame = 0;//reserved
 params.meAlgo = 0; 
 params.unrestrictedMV = 0;
 params.seqScalingFlag = 0;
 params.encQuality = 1;
 params.enableARM926Tcm = 0;

        /* Xena hardware provides only single VIMCOP which is indexed by 0 */
        //params.imcop_selector = 0; /* Allways 0 */

        /* DMA initialization parameter should be set before Encoder creation as
         * DMA will be configured based on these parameters during creation.
         * Setting of these parameters is done through the following function
         * defined in file testapp_idma.c. Application writer can update this
         * file
         */

        /* Encoder Create function - Calls ALG Framework function -
         * ALG_create(IALG_Fxns *fxns, IALG_Handle p, IALG_Params *params)
         */
      //= (IH264VENC_Handle) VIDENC1_create(ve->ceHdl, ve->h264enc1Name, (VIDENC1_Params*) &ve->venc1Params)))
        //if((handle = H264VENC_create(&fxns, &params)) == NULL)
        if((nhandle = VIDENC1_create(hEngine, algName, &params)) == NULL)
        {
            printf("\nERROR! - Encoder Creation Failed");
   fflush(stdout);
            return -1; /* Creation failed - exit the encoder */
        }
        printf("Encoder Creation Done\n");

    /* Create an instance of an algorithm that implements IALG and IRES_Fxns */

   if (IRES_OK != RMAN_assignResources((IALG_Handle)handle,
                                        &H264VENC_TI_IRES,
                                        1)) {
      printf( "\nFailed in assignign resources \
                 Exiting for this configuration..");
      goto FREE_ENCODER_IO_BUFFERS;
    }


  /**********************************************************************/
  /* Activate all IRES resources             */
  /**********************************************************************/
  H264VENC_TI_IRES.activateAllResources((IALG_Handle)handle);

        /*--------------------------------------------------------------------*/
        /*              Check for any warnings set at create time             */
        /*--------------------------------------------------------------------*/

  status.videncStatus.size = sizeof(IH264VENC_Status);
  status.videncStatus.data.bufSize = sizeof(lib_version);
  status.videncStatus.data.buf = lib_version;
        iErrorFlag = H264VENC_control(
            handle,         // Instance Handle
            XDM_GETSTATUS,  // Command
            &dynamicparams, // Pointer to Dynamic Params structure -Input
            &status         // Pointer to the status structure - Output
        );

        if(iErrorFlag == XDM_EFAIL)
        {
   printf("Get Status Info Command Failed\n");
   printErrorMsg(status.videncStatus.extendedError);
   goto FREE_ENCODER_OBJECT;
  }

  if(status.videncStatus.extendedError)
  printErrorMsg(status.videncStatus.extendedError);

  iErrorFlag = H264VENC_control(
            handle,         // Instance Handle
            XDM_GETVERSION, // Command
            &dynamicparams, // Pointer to Dynamic Params structure -Input
            &status         // Pointer to the status structure - Output
        );

        if(iErrorFlag == XDM_EFAIL)
        {
   printf("Get Status Info Command Failed\n");
   printErrorMsg(status.videncStatus.extendedError);
   goto FREE_ENCODER_OBJECT;
  }
  printf("Library Version %s\n",lib_version);

        /*--------------------------------------------------------------------*/
        /*            GET H.264 ENCODER INPUT/OUTPUT BUFFER INFORMATION       */
        /*--------------------------------------------------------------------*/
        /* The Number of Input output buffers and their sizes are alogrithm
         * specific. For a genric codec independent application like this, the
         * buffer information for a codec can be obatined by making a call to
         * the XDAIS control func - H264VENC_control() with the command
         * \b XDM_GETBUFINFO\b. The function will return back the number of
         * input and output arrays and the size of each array.
         */
  status.videncStatus.size = sizeof(IH264VENC_Status);
        iErrorFlag = H264VENC_control(
            handle,         // Instance Handle
            XDM_GETBUFINFO, // Command
            &dynamicparams, // Pointer to Dynamic Params structure -Input
            &status         // Pointer to the status structure - Output
        );
        if(iErrorFlag == XDM_EFAIL)
        {
   printf("Get Buffer Info Command Faile\n");
   goto FREE_ENCODER_OBJECT;
  }

        /*--------------------------------------------------------------------*/
        /*            GET H.264 ENCODER INPUT/OUTPUT BUFFER INFORMATION       */
        /*--------------------------------------------------------------------*/
        /* Based on the Num of buffers requested by the algorithm, the
         * application will allocate for the same here
         */
        if(AllocateH264IOBuffers(
            status, // status structure - Input
            &inobj, // Pointer to Input Buffer Descriptor - Output
            &outobj) // Pointer to Output Buffer Descriptor - Output
        )
        {
            printf("Not Enough Memory\n");
            FreeH264IOBuffers(
                &inobj, // Pointer to Input Buffer Descriptor - Input
                &outobj // Pointer to Output Buffer Descriptor - Input
            );
            return -1;
        }
  inobj.frameHeight = IN_HEIGHT;
  inobj.frameWidth = IN_WIDTH;
  inobj.framePitch = IN_WIDTH;
  if((dynamicparams.videncDynamicParams.captureWidth == 0) ||
     (dynamicparams.videncDynamicParams.captureWidth < IN_WIDTH))
  {
   inobj.framePitch = IN_WIDTH;
  }

        /*--------------------------------------------------------------------*/
        /*                     SET DYNAMIC INPUT PARAMETERS                   */
        /*--------------------------------------------------------------------*/

        iErrorFlag = H264VENC_control(
            handle,        // Instance Handle
            XDM_SETPARAMS, // Command
            &dynamicparams,// Pointer to Dynamic structure - Input
            &status        // Pointer to the status structure - Output
        );
        if(iErrorFlag == XDM_EFAIL)
        {
   printf("Set Encoder parameters Command Failed\n");
   printErrorMsg(status.videncStatus.extendedError);
   goto FREE_ENCODER_IO_BUFFERS;
  }

  if(status.videncStatus.extendedError)
  printErrorMsg(status.videncStatus.extendedError);


        inargs.videncInArgs.size = sizeof(IH264VENC_InArgs);
#ifdef SEI_USERDATA_INSERTION
  /* Input to the codec, to provide the space for adding SEI userdata unregistered
   * into the bitstream */
  inargs.insertUserData = (1 * bSEIFlagOut);
  inargs.lengthUserData = (60 * bSEIFlagOut);
#endif
        inargs.videncInArgs.inputID = 1;
        inargs.videncInArgs.topFieldFirstFlag = XDAS_TRUE;
 inargs.timeStamp = 0;


        outargs.videncOutArgs.size = sizeof(IH264VENC_OutArgs);
        outargs.videncOutArgs.extendedError = 0;
        outargs.videncOutArgs.bytesGenerated = 0;
        outargs.videncOutArgs.encodedFrameType = IVIDEO_NA_FRAME;
        outargs.videncOutArgs.inputFrameSkip = IVIDEO_FRAME_ENCODED;
        outargs.videncOutArgs.outputID = 1;
 outargs.numPackets = 0;
   }
 
 return 0;
        /*--------------------------------------------------------------------*/
        /*             FREE H.264 ENCODER INPUT/OUTPUT BUFFERS                */
        /*--------------------------------------------------------------------*/
FREE_ENCODER_IO_BUFFERS:
        FreeH264IOBuffers(
            &inobj, // Pointer to Input Buffer Descriptor - Input
            &outobj // Pointer to Output Buffer Descriptor - Input
        );

    if (IRES_OK != RMAN_freeResources((IALG_Handle)(handle),
                                        &H264VENC_TI_IRES, /* IRES_Fxns* */
                                        1 /* scratchId */)) {
        printf("Free Resource Failed \n");
        return -1;
    }

FREE_ENCODER_OBJECT:
        H264VENC_delete(handle);

    if (IRES_OK != RMAN_unregister(&IRESMAN_EDMA3CHAN)) {
            printf("Unregister Protocol Failed \n");
            return -1;
    }

    RMAN_exit();
 return -1;
}