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.

codec memory error

Hi,I add some code to videnc_copy.c under example/codec/videnc directory.Here is my code:

/*
 *  ======== VIDENCCOPY_TI_process ========
 */
XDAS_Int32 VIDENCCOPY_TI_process(IVIDENC_Handle h, XDM_BufDesc *inBufs,
    XDM_BufDesc *outBufs, IVIDENC_InArgs *inArgs, IVIDENC_OutArgs *outArgs)
{
    XDAS_Int32 curBuf;
    XDAS_UInt32 minSamples;
    int i;
    int j;
    unsigned char *m_Temp;
    int height=inArgs->height;
    int length =inArgs->length;


#ifdef USE_ACPY3
    const Uint32 maxTransferChunkSize       = 0xffff;
    Uint32       thisTransferChunkSize      = 0x0;
    Uint32       remainingTransferChunkSize;
    Uint32       thisTransferSrcAddr, thisTransferDstAddr;

    ACPY3_Params params;
    VIDENCCOPY_TI_Obj *videncObj = (VIDENCCOPY_TI_Obj *)h;
#endif

    GT_5trace(curTrace, GT_ENTER, "VIDENCCOPY_TI_process(0x%x, 0x%x, 0x%x, "
        "0x%x, 0x%x)\n", h, inBufs, outBufs, inArgs, outArgs);

    /* validate arguments - this codec only supports "base" xDM. */
    if ((inArgs->size != sizeof(*inArgs)) ||
        (outArgs->size != sizeof(*outArgs))) {

        GT_2trace(curTrace, GT_ENTER,
            "VIDENCCOPY_TI_process, unsupported size "
            "(0x%x, 0x%x)\n", inArgs->size, outArgs->size);

        return (IVIDENC_EFAIL);
    }

#ifdef USE_ACPY3
    /*
     * Activate Channel  scratch DMA channels.
     */
    ACPY3_activate(videncObj->dmaHandle1D1D8B);
#endif

    /* outArgs->bytesGenerated reports the total number of bytes generated */
    outArgs->bytesGenerated = 0;

    /*
     * A couple constraints for this simple "copy" codec:
     *    - Video encoding presumes a single input buffer, so only one input
     *      buffer will be encoded, regardless of inBufs->numBufs.
     *    - Given a different size of an input and output buffers, only
     *      encode (i.e., histogram) the lesser of the sizes.
     */

    for (curBuf = 0; (curBuf < inBufs->numBufs) &&
        (curBuf < outBufs->numBufs); curBuf++) {

        /* there's an available in and out buffer, how many samples? */
        minSamples = inBufs->bufSizes[curBuf] < outBufs->bufSizes[curBuf] ?
            inBufs->bufSizes[curBuf] : outBufs->bufSizes[curBuf];
 
        
            m_Temp = (unsigned char *)malloc(height*length);
            if(m_Temp==NULL) {
                GT_1trace(curTrace, GT_ENTER,"Fail to malloc memory (0x%x)\n",m_Temp);
                return (IVIDENC_EFAIL);}


       memcpy(m_Temp,inBufs->bufs[curBuf],inBufs->bufSizes[curBuf]);
      
      
        /* Change to grey image. */
       for(i=0;i< height;i++)
            for(j=0;j< length;j+=2)
            {
            m_Temp[i*length+j]=0x80; 
            }
          memcpy(inBufs->bufs[curBuf],m_Temp,inBufs->bufSizes[curBuf]);
          free(m_Temp);
 
#ifdef USE_ACPY3

        thisTransferSrcAddr        = (Uint32)inBufs->bufs[curBuf];

        thisTransferDstAddr        = (Uint32)outBufs->bufs[curBuf];
        remainingTransferChunkSize = minSamples;

        while (remainingTransferChunkSize > 0) {

            if (remainingTransferChunkSize > maxTransferChunkSize) {
               thisTransferChunkSize = maxTransferChunkSize;
            }
            else {
               thisTransferChunkSize = remainingTransferChunkSize;
            }

            /* Configure the logical channel */
            params.transferType = ACPY3_1D1D;
            params.srcAddr      = (void *)thisTransferSrcAddr;
            params.dstAddr      = (void *)thisTransferDstAddr;
            params.elementSize  = thisTransferChunkSize;
            params.numElements  = 1;
            params.waitId       = 0;
            params.numFrames    = 1;

            remainingTransferChunkSize -= thisTransferChunkSize;
            thisTransferSrcAddr += thisTransferChunkSize;
            thisTransferDstAddr += thisTransferChunkSize;

            /* Configure logical dma channel */
            ACPY3_configure(videncObj->dmaHandle1D1D8B, &params, 0);

            /* Use DMA to histogram data */
            ACPY3_start(videncObj->dmaHandle1D1D8B);

            /* wait for transfer to finish  */
            ACPY3_wait(videncObj->dmaHandle1D1D8B);
        }
        GT_1trace(curTrace, GT_2CLASS, "VIDENCHISTOGRAM_TI_process> "
               "ACPY3 Processed %d bytes.\n", minSamples);
#else
        GT_3trace(curTrace, GT_2CLASS, "VIDENCHISTOGRAM_TI_process> "
               "memcpy (0x%x, 0x%x, %d)\n",
               outBufs->bufs[curBuf], inBufs->bufs[curBuf], minSamples);

        /* process the data: read input, produce output */
        memcpy(outBufs->bufs[curBuf], inBufs->bufs[curBuf], minSamples);
#endif

        outArgs->bytesGenerated += minSamples;
    }
  
    /* Fill out the rest of the outArgs struct */
    outArgs->extendedError = 0;
    outArgs->encodedFrameType = 0;    /* TODO */
    outArgs->inputFrameSkip = IVIDEO_FRAME_ENCODED;
    outArgs->reconBufs.numBufs = 0;   /* important: indicate no reconBufs */

    return (IVIDENC_EOK);
}

When I run my application,I got the error message below:

[DSP] @0x0040468f:[T:0x8fa215e4] codecs.videnc_histogram - Fail to malloc memory
 (0x0)

Why did I fail to malloc some memory in the codec? I know that when I use malloc to request memory ,the system will distribute it at DDRALGHEAP section,and my DDRALGHEAP's size  is 128M bytes.It's big enough. So please ,I want somebody to tell me where is my fault,and how to correct it .Thanks a lot. 

  • The XDAIS standard doesn't allow you to call malloc()/free().  You should request your memory during your algAlloc() fxn, and store the memory provided to you during algInit() in your alg object... then use that memory via the alg object passed to your process() method.

    As for why malloc() is failing, you'd likely have the same malloc() failure if you called it from main(), so I'm betting it's not really a codec issue (and therefore belongs maybe in the BIOS forum).  It's most likely a config-related issue, and the folks on the BIOS forum should be able to help.  (But again, you shouldn't chase that too far - don't call malloc() from your codec.)

    Chris

  • Can you give an example to illustrate how to use  algAlloc() and algInit() to request and store memory? I try to use it ,but it does not  works,

    /*
     *  Copyright 2010
     *  Hailing ,Xidian University
     *
     */
    /*
     *  =========== vidrnc_track.c ============
     *  Video Encoder "track" algorithm.
     *
     *  This file contains an implementation of the deprecated IVIDENC interface
     *  as defined by xDM.
     */
    #include <xdc/std.h>
    #include <string.h>
    #include<stdio.h>
    #include <stdlib.h>
     
    #include <ti/xdais/dm/ividenc.h>
    #include <ti/xdais/dm/xdm.h>
    #include <ti/sdo/ce/trace/gt.h>
     
    #include "videnc_track_ti.h"
    #include "videnc_track_ti_priv.h"
     

    # define SIZE 352*288*sizeof(unsigned char)
     
     
    /* buffer definitions */
    #define MININBUFS       1
    #define MINOUTBUFS      1
    #define MININBUFSIZE    1
    #define MINOUTBUFSIZE   1
     
    extern IALG_Fxns VIDENCTRACK_TI_IALG;
    #define IALGFXNS  \
        &VIDENCTRACK_TI_IALG,         /* module ID */                         \
        VIDENCTRACK_TI_activate,     /* activate */                          \
        VIDENCTRACK_TI_alloc,        /* alloc */                             \
        NULL,                       /* control (NULL => no control ops) */   \
        VIDENCTRACK_TI_deactivate,   /* deactivate */                        \
        VIDENCTRACK_TI_free,         /* free */                              \
        VIDENCTRACK_TI_initObj,      /* init */                              \
        NULL,                       /* moved */                              \
        NULL                        /* numAlloc (NULL => IALG_MAXMEMRECS) */
     
    /*
     *  ======== VIDENCTRACK_TI_IVIDENC ========
     *  This structure defines TI's implementation of the IVIDENC interface
     *  for the VIDENCTRACK_TI module.
     */
    IVIDENC_Fxns VIDENCTRACK_TI_VIDENCTRACK = {      /* module_vendor_interface */
         {IALGFXNS},
         VIDENCTRACK_TI_process,
         VIDENCTRACK_TI_control,
    };
     
    /*
     *  ======== VIDENCTRACK_TI_IALG ========
     *  This structure defines TI's implementation of the IALG interface
     *  for the VIDENCTRACK_TI module.
     */
    #ifdef _TI_
    /* satisfy xDAIS symbol requirement without any overhead */
    asm("_VIDENCTRACK_TI_IALG .set _VIDENCTRACK_TI_VIDENCTRACK");
     
    #else
    /*
     *  We duplicate the structure here to allow this code to be compiled and
     *  run non-DSP platforms at the expense of unnecessary data space
     *  consumed by the definition below.
     */
     IALG_Fxns VIDENCTRACK_TI_IALG = {      /* module_vendor_interface int *pFlagFG;*/ 
         IALGFXNS
    };
     
    #endif
     
    /* tracing information */
    #define GTNAME "ti.sdo.ce.examples.codecs.videnc_track"
    static GT_Mask curTrace = {NULL,NULL};
     
     
    /*
     *  ======== VIDENCTRACK_TI_activate ========
     */
    Void VIDENCTRACK_TI_activate(IALG_Handle handle)
    {
        GT_1trace(curTrace, GT_ENTER, "VIDENCTRACK_TI_activate(0x%x)\n", handle);
    }
     
     
    /*
     *  ======== VIDENCTRACK_TI_deactivate ========
     */
    Void VIDENCTRACK_TI_deactivate(IALG_Handle handle)
    {
        GT_1trace(curTrace, GT_ENTER, "VIDENCTRACK_TI_deactivate(0x%x)\n", handle);
    }
     
     
    /*
     *  ======== VIDENCTRACK_TI_alloc ========
     */
    Int VIDENCTRACK_TI_alloc(const IALG_Params *algParams,
        IALG_Fxns **pf, IALG_MemRec memTab[])
    {
        if (curTrace.modName == NULL) {   /* initialize GT (tracing) */
            GT_create(&curTrace, GTNAME);
        }
     
        GT_3trace(curTrace, GT_ENTER, "VIDENCTRACK_TI_alloc(0x%x, 0x%x, 0x%x)\n",
            algParams, pf, memTab);

             
         /* Request memory for my object */
         memTab[0].size = sizeof(VIDENCTRACK_TI_Obj);
         memTab[0].alignment = 0;
         memTab[0].space = IALG_EXTERNAL;
         memTab[0].attrs = IALG_PERSIST;

         memTab[1].size = 352*288*sizeof(unsigned char);
         memTab[1].alignment = 0;
         memTab[1].space = IALG_EXTERNAL;
         memTab[1].attrs = IALG_PERSIST;
    memTab[0].base=(void*)MEM_alloc(memTab[0].space,memTab[0].size,memTab[0].alignment);
    memTab[1].base=(unsigned char*)MEM_alloc(memTab[1].space,memTab[1].size,memTab[1].alignment);
         return (1);
    }
     
     
    /*
     *  ======== VIDENCTRACK_TI_free ========
     */
    Int VIDENCTRACK_TI_free(IALG_Handle handle, IALG_MemRec memTab[])
    {
        GT_2trace(curTrace, GT_ENTER, "VIDENCTRACK_TI_free(0x%lx, 0x%lx)\n",
            handle, memTab);

    //        VIDENCTRACK_TI_Obj *inst = (VIDENCTRACK_TI_Obj *)handle;
        VIDENCTRACK_TI_alloc(NULL, NULL, memTab);
       
    return (1);
    }
     
     
    /*
     *  ======== VIDENCTRACK_TI_initObj ========
     */
    Int VIDENCTRACK_TI_initObj(IALG_Handle handle,
        const IALG_MemRec memTab[], IALG_Handle p,
        const IALG_Params *algParams) 
    {   int i;
        VIDENCTRACK_TI_Obj *inst = (VIDENCTRACK_TI_Obj *)handle;
       
       

        inst->p=memTab[1].base;
      if(inst->p==NULL){
            return (IALG_EFAIL); }
       
    GT_4trace(curTrace, GT_ENTER,
            "VIDENCTRACK_TI_initObj(0x%x, 0x%x, 0x%x, 0x%x)\n", handle, memTab,
            p, algParams);
     
        return (IALG_EOK);
    }
     
     
    /* 
     *  ======== VIDENCTRACK_TI_process ========
     */
    XDAS_Int32 VIDENCTRACK_TI_process(IVIDENC_Handle h, XDM_BufDesc *inBufs,
        XDM_BufDesc *outBufs, IVIDENC_InArgs *inArgs, IVIDENC_OutArgs *outArgs)

      XDAS_Int32 curBuf;
      XDAS_Int32 minSamples;
      int i,j;

        VIDENCTRACK_TI_Obj *inst = (VIDENCTRACK_TI_Obj *)h;
        q=inst->p;
        
      
     GT_5trace(curTrace, GT_ENTER, "VIDENCTRACK_TI_process(0x%lx, 0x%lx, 0x%lx, "
           "0x%lx, 0x%lx)\n", h, inBufs, outBufs, inArgs, outArgs);

        /* validate arguments - this codec only supports "base" xDM. */
        if ((inArgs->size != sizeof(*inArgs)) ||
            (outArgs->size != sizeof(*outArgs))) {

            GT_2trace(curTrace, GT_ENTER, "VIDENCTRACK_TI_process, unsupported size "
                "(0x%lx, 0x%lx)\n", inArgs->size, outArgs->size);

            return (IVIDENC_EFAIL);
        }



      outArgs->bytesGenerated=0;
     
     
         minSamples = inBufs->bufSizes[0] < outBufs->bufSizes[0] ?
         inBufs->bufSizes[0] : outBufs->bufSizes[0];       
                
             memcpy(inst->p,inBufs->bufs[curBuf],inBufs->bufSizes[0]);
             for(i=0;i<288;i++)
                for(j=0;j<352;j+=2)
                {
                  inst->p[i*352+j]=0x80;
                inst->p[i*352+j+1]=0x10; 
                }
     
         memcpy(outBufs->bufs[0], inst->p, outBufs->bufSizes[0]);
     
         GT_1trace( curTrace, GT_2CLASS, "VIDENCTRACK_TI_process> "
                   "Processed %d bytes.\n", minSamples );
           outArgs->bytesGenerated += minSamples;
     
     
         /* Fill out the rest of the outArgs struct */
        outArgs->extendedError = 0;
        outArgs->encodedFrameType = 0;     /* TODO */
        outArgs->inputFrameSkip = IVIDEO_FRAME_ENCODED;
        outArgs->reconBufs.numBufs = 0;   /* important: indicate no reconBufs */

     
      return (IVIDENC_EOK);
    }
     
     
    /*
     *  ======== VIDENCTRACK_TI_control ========
     */
    XDAS_Int32 VIDENCTRACK_TI_control(IVIDENC_Handle handle, IVIDENC_Cmd id,
        IVIDENC_DynamicParams *params, IVIDENC_Status *status)
    {
         XDAS_Int32 retVal;
     
         GT_4trace(curTrace, GT_ENTER, "VIDENCTRACK_TI_control(0x%x, 0x%x, 0x%x, "
             "0x%x)\n", handle, id, params, status);
         
         /* validate arguments - this codec only supports "base" xDM. */
         if ((params->size != sizeof(*params)) ||
             (status->size != sizeof(*status))) {
     
            GT_2trace(curTrace, GT_ENTER,
                "VIDENCTRACK_TI_control, unsupported size "
                "(0x%x, 0x%x)\n", params->size, status->size);
     
            return (IVIDENC_EFAIL);
          }
     
         switch (id) {
             case XDM_GETSTATUS:
            case XDM_GETBUFINFO:
                status->extendedError = 0;
                status->bufInfo.minNumInBufs = MININBUFS;
                status->bufInfo.minNumOutBufs = MINOUTBUFS;
                status->bufInfo.minInBufSize[0] = MININBUFSIZE;
                status->bufInfo.minOutBufSize[0] = MINOUTBUFSIZE;
     
                retVal = IVIDENC_EOK;
     
                break;
            case XDM_SETPARAMS:
            case XDM_SETDEFAULT:
            case XDM_RESET:
            case XDM_FLUSH:
                /* TODO - for now just return success. */
                retVal = IVIDENC_EOK;
                break;
            default:
                /* unsupported cmd */
                retVal = IVIDENC_EFAIL;
                break;
            }
         return (retVal);

     
      

  • Can anyone explan the function and use of the API GT_4trace,and its 7parameters?