Exemple #1
0
static void flush_buffer(Pager* pager, int default_fd, Error* error) {
    protect_buffer(pager, true, error);

    if (ERROR_HAS(error)) {
        return;
    }

    if (pager->fd == IO_INVALID_FD) {
        pager->fd = default_fd;
    }

    for (size_t i = 0; i < pager->buffers.length; ++i) {
        Buffer* buffer = (Buffer*) pager->buffers.data[i];

        io_write(pager->fd, buffer, error);
        Buffer_delete(buffer);

        if (ERROR_HAS(error)) {
            pager->buffers.data[i] = (intptr_t) NULL;
            return;
        }
    }

    pager->buffers.length = 0;
    protect_buffer(pager, false, error);
}
Exemple #2
0
// deallocate a Stream.
//
static void Stream_delete(Stream *s)
{
  assert(s && s->buffer);
  Buffer_delete(s->buffer);
  debugf("stream %p[%d] deleted\n", s, s->direction);
  free(s);
}
Exemple #3
0
void TabPage_delete(Object* super) {
   TabPage* this = (TabPage*) super;
   TabManager_releaseLock(this->name);
   free(this->name);
   if (this->buffer) {
      Buffer_delete(this->buffer);
   }
   free(this);
}
/******************************************************************************
 * gst_tidmaivideosink_exit_display
 *    Delete any display or Framecopy objects that were created.
 *
 * NOTE:  For now this is left as a gboolean return in case there is a reason
 *        in the future that this function will return FALSE.
*******************************************************************************/
static gboolean gst_tidmaivideosink_exit_display(GstTIDmaiVideoSink * sink)
{
    GST_DEBUG("Begin\n");

    if (sink->tempDmaiBuf) {
        GST_DEBUG("Freeing temporary DMAI buffer\n");
        Buffer_delete(sink->tempDmaiBuf);
        sink->tempDmaiBuf = NULL;
    }

    if (sink->hFc) {
        GST_DEBUG("closing Framecopy\n");
        Framecopy_delete(sink->hFc);
        sink->hFc = NULL;
    }

    if (sink->hDisplay) {
        GST_DEBUG("closing display\n");
        Display_delete(sink->hDisplay);
        sink->hDisplay = NULL;
    }

    if(sink->cleanBufCtrl){
        g_free(sink->cleanBufCtrl);
        sink->cleanBufCtrl = NULL;
    }

    if (sink->allocatedBuffers){
        g_free(sink->allocatedBuffers);
        sink->allocatedBuffers = NULL;
    }
    sink->lastAllocatedBuffer = NULL;
    sink->numAllocatedBuffers = 0;
    if (sink->unusedBuffers){
        g_free(sink->unusedBuffers);
        sink->unusedBuffers = NULL;
    }
    sink->numUnusedBuffers = 0;

    sink->capsAreSet = FALSE;

    GST_DEBUG("Finish\n");

    return TRUE;
}
/******************************************************************************
 * gst_ticircbuffer_finalize
 ******************************************************************************/
static void gst_ticircbuffer_finalize(GstTICircBuffer* circBuf)
{
    if (circBuf == NULL) {
        return;
    }

    GST_LOG("Maximum bytes consumed:  %lu\n", circBuf->maxConsumed);

    if (circBuf->hBuf) {
        Buffer_delete(circBuf->hBuf);
    }

    if (circBuf->waitOnProducer) {
        Rendezvous_delete(circBuf->waitOnProducer);
    }

    if (circBuf->waitOnConsumer) {
        Rendezvous_delete(circBuf->waitOnConsumer);
    }
}
Exemple #6
0
static void Pager_delete(Pager* pager, Error* error) {
    if (pager->has_timer) {
        if (ERROR_HAS(&pager->timer_error)) {
            // FIXME: don't discard errors
            Error_set(error, &pager->timer_error);
            return;
        }

        int error_nr = pthread_cancel(pager->timer_thread);

        if (error_nr && (error_nr != ESRCH)) {
            Error_add(error, strerror(error_nr));
            return;
        }

        error_nr = pthread_join(pager->timer_thread, NULL);

        if (error_nr) {
            Error_add(error, strerror(error_nr));
            return;
        }

        error_nr = pthread_mutex_destroy(&pager->timer_mutex);

        if (error_nr) {
            Error_add(error, strerror(error_nr));
            return;
        }
    }

    for (size_t i = 0; i < pager->buffers.length; ++i) {
        Buffer_delete((Buffer*) pager->buffers.data[i]);
    }

    Array_deinit(&pager->buffers);
    free(pager);
}
/*******************************************************************************
 * gst_tidmaivideosink_set_caps
 *
 * This is mainly a place holder function.
*******************************************************************************/
static gboolean gst_tidmaivideosink_set_caps(GstBaseSink * bsink,
                    GstCaps * caps)
{
    GstTIDmaiVideoSink *sink;
    guint32 fourcc;
    gint framerateDen, framerateNum;
    GstStructure *structure = NULL;

    sink = GST_TIDMAIVIDEOSINK(bsink);

    structure = gst_caps_get_structure(caps, 0);
    /* The width and height of the input buffer are collected here
     * so that it can be checked against the width and height of the
     * display buffer.
     */
    gst_structure_get_int(structure, "width", &sink->width);
    gst_structure_get_int(structure, "height", &sink->height);
    sink->dmaiElementUpstream = FALSE;
    gst_structure_get_boolean(structure,"dmaioutput",&sink->dmaiElementUpstream);

    /* Map input buffer fourcc to dmai color space  */
    gst_structure_get_fourcc(structure, "format", &fourcc);

    switch (fourcc) {
        case GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'):
            sink->colorSpace = ColorSpace_UYVY;
            break;
        case GST_MAKE_FOURCC('Y', '8', 'C', '8'):
            sink->colorSpace = ColorSpace_YUV422PSEMI;
            break;
        case GST_MAKE_FOURCC('N', 'V', '1', '2'):
            sink->colorSpace = ColorSpace_YUV420PSEMI;
            break;
        default:
            GST_ELEMENT_ERROR(sink,STREAM,FAILED,(NULL),
                ("unsupported fourcc"));
            return FALSE;
    }
    GST_DEBUG("Colorspace %d, width %d, height %d\n", sink->colorSpace, sink->width,
        sink->height);

    gst_structure_get_fraction(structure, "framerate",
        &framerateNum, &framerateDen);

    /* Set the input videostd attrs so that when the display is created
     * we can know what size it needs to be.
     */
    sink->iattrs.width  = sink->width;
    sink->iattrs.height = sink->height;

    /* Set the input frame rate.  Round to the nearest integer */
    sink->iattrs.framerate =
        (int)(((gdouble) framerateNum / framerateDen) + .5);

    GST_DEBUG("Frame rate numerator = %d\n", framerateNum);
    GST_DEBUG("Frame rate denominator = %d\n", framerateDen);
    GST_DEBUG("Frame rate rounded = %d\n", sink->iattrs.framerate);

    if (!gst_tidmaivideosink_init_display(sink, sink->colorSpace)) {
        GST_ELEMENT_ERROR(sink,STREAM,FAILED,(NULL),
            ("Failed to initialize display"));
        return FALSE;
    }

    if (sink->tempDmaiBuf) {
        GST_DEBUG("Freeing temporary DMAI buffer\n");
        Buffer_delete(sink->tempDmaiBuf);
        sink->tempDmaiBuf = NULL;
    }

    gst_tidmaivideosink_clean_DisplayBuf(sink);
    sink->capsAreSet = TRUE;

    return TRUE;
}
Exemple #8
0
/******************************************************************************
 * speechThrFxn
 ******************************************************************************/
Void *speechThrFxn(Void *arg)
{
    SpeechEnv              *envp                = (SpeechEnv *) arg;
    SPHDEC1_Params          defaultParams       = Sdec1_Params_DEFAULT;
    SPHDEC1_DynamicParams   defaultDynParams    = Sdec1_DynamicParams_DEFAULT;
    Void                   *status              = THREAD_SUCCESS;
    Sound_Attrs             sAttrs              = Sound_Attrs_MONO_DEFAULT;
    Loader_Attrs            lAttrs              = Loader_Attrs_DEFAULT;
    Buffer_Attrs            bAttrs              = Buffer_Attrs_DEFAULT;
    Sdec1_Handle            hSd1                = NULL;
    Sound_Handle            hSound              = NULL;
    Loader_Handle           hLoader             = NULL;
    Engine_Handle           hEngine             = NULL;
    Buffer_Handle           hOutBuf             = NULL;
    SPHDEC1_Params         *params;
    SPHDEC1_DynamicParams  *dynParams;
    Buffer_Handle           hInBuf;

    /* Open the codec engine */
    hEngine = Engine_open(envp->engineName, NULL, NULL);

    if (hEngine == NULL) {
        ERR("Failed to open codec engine %s\n", envp->engineName);
        cleanup(THREAD_FAILURE);
    }

    /* Create the sound device */
    sAttrs.sampleRate = 8000;
    sAttrs.mode = Sound_Mode_OUTPUT;
    sAttrs.leftGain  = 127;
    sAttrs.rightGain = 127;
    sAttrs.bufSize   = 128;
    hSound = Sound_create(&sAttrs);

    if (hSound == NULL) {
        ERR("Failed to create audio device\n");
        cleanup(THREAD_FAILURE);
    }

    /* Set the sample rate for the user interface */
    gblSetSamplingFrequency(sAttrs.sampleRate);

    /* Use supplied params if any, otherwise use defaults */
    params = envp->params ? envp->params : &defaultParams;
    dynParams = envp->dynParams ? envp->dynParams : &defaultDynParams;

    /* Create the speech decoder */
    hSd1 = Sdec1_create(hEngine, envp->speechDecoder, params, dynParams);

    if (hSd1 == NULL) {
        ERR("Failed to create speech decoder: %s\n", envp->speechDecoder);
        cleanup(THREAD_FAILURE);
    }

    /*
     * Make the output buffer size twice the size of what the codec needs
     * as the codec needs mono and the Sound module converts the decoded
     * mono samples to stereo before writing to the device driver.
     */
    hOutBuf = Buffer_create(OUTBUFSIZE, &bAttrs);

    if (hOutBuf == NULL) {
        ERR("Failed to allocate output buffer\n");
        cleanup(THREAD_FAILURE);
    }

    /* How much encoded data to feed the codec each process call */
    lAttrs.readSize = INBUFSIZE;

    /* Make the total ring buffer larger */
    lAttrs.readBufSize = lAttrs.readSize * 512;

    /* Create the file loader for reading encoded data */
    hLoader = Loader_create(envp->speechFile, &lAttrs);

    if (hLoader == NULL) {
        ERR("Failed to create loader\n");
        cleanup(THREAD_FAILURE);
    }

    /* Signal that initialization is done and wait for other threads */
    Rendezvous_meet(envp->hRendezvousInit);

    /* Prime the file loader */
    Loader_prime(hLoader, &hInBuf);

    while (!gblGetQuit()) {
        /* Pause processing? */
        Pause_test(envp->hPauseProcess);

        /* Decode the audio buffer */
        if (Sdec1_process(hSd1, hInBuf, hOutBuf) < 0) {
            ERR("Failed to decode audio buffer\n");
            cleanup(THREAD_FAILURE);
        }

        /* Increment statistics for user interface */
        gblIncSoundBytesProcessed(Buffer_getNumBytesUsed(hInBuf));

        /*
         * Force the output buffer size since we are forcing the size of the
         * output buffer allocated as opposed to asking the codec for a size.
         */
        Buffer_setNumBytesUsed(hOutBuf, OUTBUFSIZE);

        /* Write the decoded samples to the sound device */
        if (Sound_write(hSound, hOutBuf) < 0) {
            ERR("Failed to write audio buffer\n");
            cleanup(THREAD_FAILURE);
        }

        /* Load a new frame from the file system */
        if (Loader_getFrame(hLoader, hInBuf) < 0) {
            ERR("Failed to read a frame of encoded data\n");
            cleanup(THREAD_FAILURE);
        }

        /* Check if the clip has ended */
        if (Buffer_getUserPtr(hInBuf) == NULL) {
            /* Wait for the video clip to finish, if applicable */
            Rendezvous_meet(envp->hRendezvousLoop);

            /* If we are to loop the clip, start over */
            if (envp->loop) {
                /* Recreate the speech codec */
                Sdec1_delete(hSd1);
                hSd1 = Sdec1_create(hEngine, envp->speechDecoder,
                                    params, dynParams);

                if (hSd1 == NULL) {
                    ERR("Failed to create speech decoder: %s\n",
                        envp->speechDecoder);
                    cleanup(THREAD_FAILURE);
                }

                /* Re-prime the file loader */
                Loader_prime(hLoader, &hInBuf);
            }
            else {
                printf("End of clip reached, exiting..\n");
                cleanup(THREAD_SUCCESS);
            }
        }
    }

cleanup:
    /* Make sure the other threads aren't waiting for us */
    Rendezvous_force(envp->hRendezvousInit);
    Rendezvous_force(envp->hRendezvousLoop);
    Pause_off(envp->hPauseProcess);

    /* Meet up with other threads before cleaning up */
    Rendezvous_meet(envp->hRendezvousCleanup);

    /* Clean up the thread before exiting */
    if (hLoader) {
        Loader_delete(hLoader);
    }

    if (hSd1) {
        Sdec1_delete(hSd1);
    }

    if (hSound) {
        Sound_delete(hSound);
    }

    if (hOutBuf) {
        Buffer_delete(hOutBuf);
    }

    if (hEngine) {
        Engine_close(hEngine);
    }

    return status;
}
Exemple #9
0
/******************************************************************************
 * appMain
 ******************************************************************************/
Int appMain(Args * args)
{
    VIDENC1_Params         params    = Venc1_Params_DEFAULT;
    VIDENC1_DynamicParams  dynParams = Venc1_DynamicParams_DEFAULT;
    BufferGfx_Attrs        gfxAttrs  = BufferGfx_Attrs_DEFAULT;
    Buffer_Attrs           bAttrs    = Buffer_Attrs_DEFAULT;
    Time_Attrs             tAttrs    = Time_Attrs_DEFAULT;
    Venc1_Handle           hVe1      = NULL;
    FILE                  *outFile   = NULL;
    FILE                  *reconFile = NULL;
    FILE                  *inFile    = NULL;
    Engine_Handle          hEngine   = NULL;
    Time_Handle            hTime     = NULL;
    Bool                   flushed   = FALSE;
    Bool                   mustExit  = FALSE;
    BufTab_Handle          hBufTab   = NULL;
    Buffer_Handle          hOutBuf   = NULL;
    Buffer_Handle          hFreeBuf  = NULL;
    Buffer_Handle          hInBuf    = NULL;
    Buffer_Handle          hReconBuf = NULL;
    Int                    numFrame  = 0;
    Int                    flushCntr = 1;
    Int                    bufIdx;
    Int                    inBufSize, outBufSize;
    Cpu_Device             device;
    Int                    numBufs;
    ColorSpace_Type        colorSpace;
    UInt32                 time;
    Int                    ret = Dmai_EOK;

    printf("Starting application...\n");

    /* Initialize the codec engine run time */
    CERuntime_init();

    /* Initialize DMAI */
    Dmai_init();

    /* Determine which device the application is running on */
    if (Cpu_getDevice(NULL, &device) < 0) {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Failed to determine target board\n");
        goto cleanup;
    }

    if (args->benchmark) {
        hTime = Time_create(&tAttrs);

        if (hTime == NULL) {
            ret = Dmai_EFAIL;
            fprintf(stderr,"Failed to create Time object\n");
            goto cleanup;
        }
    }

    /* Open the input file with raw yuv data */
    inFile = fopen(args->inFile, "rb");

    if (inFile == NULL) {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Failed to open input file %s\n", args->inFile);
        goto cleanup;
    }

    /* Using a larger vbuf to enhance performance of file i/o */
    if (setvbuf(inFile, vbufferIn, _IOFBF, sizeof(vbufferIn)) != 0) {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Failed to setvbuf on input file descriptor\n");
        goto cleanup;
    }

    /* Open the output file where to put encoded data */
    outFile = fopen(args->outFile, "wb");

    if (outFile == NULL) {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Failed to open output file %s\n", args->outFile);
        goto cleanup;
    }

    /* Using a larger vbuf to enhance performance of file i/o */
    if (setvbuf(outFile, vbufferOut, _IOFBF, sizeof(vbufferOut)) != 0) {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Failed to setvbuf on output file descriptor\n");
        goto cleanup;
    }

    /* Open the output file where to put reconstructed frames */
    if (args->writeReconFrames) {
        reconFile = fopen(args->reconFile, "wb");

        if (reconFile == NULL) {
            ret = Dmai_EFAIL;
            fprintf(stderr,"Failed to open output file %s\n", args->reconFile);
            goto cleanup;
        }

        /* Using a larger vbuf to enhance performance of file i/o */
        if (setvbuf(reconFile, vbufferRecon, _IOFBF,
                    sizeof(vbufferRecon)) != 0) {
            ret = Dmai_EFAIL;
            fprintf(stderr,"Failed to setvbuf on output file descriptor\n");
            goto cleanup;
        }
    }

    /* Open the codec engine */
    hEngine = Engine_open(args->engineName, NULL, NULL);

    if (hEngine == NULL) {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Failed to open codec engine: %s\n", args->engineName);
        goto cleanup;
    }

    /* Set up codec parameters depending on bit rate */
    if (args->bitRate < 0) {
        /* Variable bit rate */
        params.rateControlPreset = IVIDEO_NONE;

        /*
         * If variable bit rate use a bogus bit rate value (> 0)
         * since it will be ignored.
         */
        params.maxBitRate        = 2000000;
    }
    else {
        /* Constant bit rate */
        params.rateControlPreset = IVIDEO_LOW_DELAY;
        params.maxBitRate        = args->bitRate;
    }

    /* Set up codec parameters depending on device */
    switch (device) {
    case Cpu_Device_DM6467:
        params.inputChromaFormat = XDM_YUV_420SP;
        params.reconChromaFormat = XDM_CHROMA_NA;
        break;
    case Cpu_Device_DM355:
        params.inputChromaFormat = XDM_YUV_422ILE;
        params.reconChromaFormat = XDM_YUV_420P;
        break;
    case Cpu_Device_DM365:
    case Cpu_Device_DM368:
        params.inputChromaFormat = XDM_YUV_420SP;
        params.reconChromaFormat = XDM_YUV_420SP;
        break;
    case Cpu_Device_DM3730:
        params.rateControlPreset = IVIDEO_STORAGE;
        params.inputChromaFormat = XDM_YUV_422ILE;
        break;
    default:
        params.inputChromaFormat = XDM_YUV_422ILE;
        break;
    }

    params.maxWidth              = args->width;
    params.maxHeight             = args->height;

    /* Workaround for SDOCM00068944: h264fhdvenc fails
       to create codec when params.dataEndianness is
       set as XDM_BYTE */
    if(device == Cpu_Device_DM6467) {
        if (!strcmp(args->codecName, "h264fhdvenc")) {
            params.dataEndianness        = XDM_LE_32;
        }
    }

    params.maxInterFrameInterval = 1;
    dynParams.targetBitRate      = params.maxBitRate;
    dynParams.inputWidth         = params.maxWidth;
    dynParams.inputHeight        = params.maxHeight;

    /* Create the video encoder */
    hVe1 = Venc1_create(hEngine, args->codecName, &params, &dynParams);

    if (hVe1 == NULL) {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Failed to create video encoder: %s\n", args->codecName);
        goto cleanup;
    }

    /* Ask the codec how much input data it needs */
    inBufSize = Venc1_getInBufSize(hVe1);

    /* Ask the codec how much space it needs for output data */
    outBufSize = Venc1_getOutBufSize(hVe1);

    /* Which color space to use in the graphics buffers depends on the device */
    colorSpace = ((device == Cpu_Device_DM6467)||
                  (device == Cpu_Device_DM365) ||
                  (device == Cpu_Device_DM368)) ? ColorSpace_YUV420PSEMI :
                 ColorSpace_UYVY;

    /* Align buffers to cache line boundary */
    gfxAttrs.bAttrs.memParams.align = bAttrs.memParams.align = BUFSIZEALIGN;

    /* Use cached buffers if requested */
    if (args->cache) {
        gfxAttrs.bAttrs.memParams.flags = bAttrs.memParams.flags
                                          = Memory_CACHED;
    }

    gfxAttrs.dim.width      = args->width;
    gfxAttrs.dim.height     = args->height;
    if ((device == Cpu_Device_DM6467) || (device == Cpu_Device_DM365)
            || (device == Cpu_Device_DM368)) {
        gfxAttrs.dim.height = Dmai_roundUp(gfxAttrs.dim.height, CODECHEIGHTALIGN);
    }
    gfxAttrs.dim.lineLength = BufferGfx_calcLineLength(args->width, colorSpace);
    gfxAttrs.colorSpace     = colorSpace;

    if (inBufSize < 0) {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Failed to calculate buffer attributes\n");
        goto cleanup;
    }

    /* Number of input buffers required */
    if(params.maxInterFrameInterval>1) {
        /* B frame support */
        numBufs = params.maxInterFrameInterval;
    }
    else {
        numBufs = 1;
    }

    /* Create a table of input buffers of the size requested by the codec */
    hBufTab =
        BufTab_create(numBufs, Dmai_roundUp(inBufSize, BUFSIZEALIGN),
                      BufferGfx_getBufferAttrs(&gfxAttrs));

    if (hBufTab == NULL) {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Failed to allocate contiguous buffers\n");
        goto cleanup;
    }

    /* Set input buffer table */
    Venc1_setBufTab(hVe1, hBufTab);

    /* Create the reconstructed frame buffer for raw yuv data */
    if (args->writeReconFrames) {
        hReconBuf =
            Buffer_create(Dmai_roundUp(inBufSize, BUFSIZEALIGN),
                          BufferGfx_getBufferAttrs(&gfxAttrs));

        if (hReconBuf == NULL) {
            ret = Dmai_EFAIL;
            fprintf(stderr,"Failed to allocate contiguous buffer\n");
            goto cleanup;
        }
    }

    /* Create the output buffer for encoded video data */
    hOutBuf = Buffer_create(Dmai_roundUp(outBufSize, BUFSIZEALIGN), &bAttrs);

    if (hOutBuf == NULL) {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Failed to create contiguous buffer\n");
        goto cleanup;
    }

    while (1) {

        /* Get a buffer for input */
        hInBuf = BufTab_getFreeBuf(hBufTab);

        if (hInBuf == NULL) {
            ret = Dmai_EFAIL;
            fprintf(stderr,"Failed to get a free contiguous buffer from BufTab\n");
            BufTab_print(hBufTab);
            goto cleanup;
        }

        if (args->benchmark) {
            if (Time_reset(hTime) < 0) {
                ret = Dmai_EFAIL;
                fprintf(stderr,"Failed to reset timer\n");
                goto cleanup;
            }
        }

        /* Read a yuv input frame */
        printf("\n Frame %d: ", numFrame);
        if ((device == Cpu_Device_DM6467)||
                (device == Cpu_Device_DM365) ||
                (device == Cpu_Device_DM368)) {
            if(args->sp) {
                if (readFrame420SP(hInBuf, inFile, args->height) < 0) {
                    ret = Dmai_EFAIL;
                    goto cleanup;
                }
            } else {
                if (readFrame420P(hInBuf, inFile, args->height) < 0) {
                    ret = Dmai_EFAIL;
                    goto cleanup;
                }
            }
        }
        else {
            if (readFrameUYVY(hInBuf, inFile) < 0) {
                ret = Dmai_EFAIL;
                mustExit = TRUE;
            }
        }

        if (++numFrame == args->numFrames||mustExit == TRUE) {
            if(!(params.maxInterFrameInterval>1)) {
                /* No B-frame support */
                printf("... exiting \n");
                goto cleanup;
            }

            /*
             * When encoding a stream with B-frames, ending the processing
             * requires to free the buffer held by the encoder. This is done by
             * flushing the encoder and performing a last process() call
             * with a dummy input buffer.
             */
            printf("\n... exiting with flush (B-frame stream) \n");
            flushCntr = params.maxInterFrameInterval-1;
            flushed = TRUE;
            Venc1_flush(hVe1);
        }

        if (args->benchmark) {
            if (Time_delta(hTime, &time) < 0) {
                ret = Dmai_EFAIL;
                fprintf(stderr,"Failed to get timer delta\n");
                goto cleanup;
            }

            printf("Read time: %uus\n", (Uns)time);
        }

        /*
         * Following flushing loop will iterate more than one time only
         * when the encoder completes processing by flushing the frames
         * held by the encoder. All flushed frames will be encoded as P
         * or I frames.
         */

        for(bufIdx = 0; bufIdx < flushCntr; bufIdx++) {

            if (args->cache) {
                /*
                *  To meet xDAIS DMA Rule 7, when input buffers are cached, we
                *  must writeback the cache into physical memory.  Also, per DMA
                *  Rule 7, we must invalidate the output buffer from
                *  cache before providing it to any xDAIS algorithm.
                */
                Memory_cacheWbInv(Buffer_getUserPtr(hInBuf),
                                  Buffer_getSize(hInBuf));

                /* Per DMA Rule 7, our output buffer cache lines must be cleaned */
                Memory_cacheInv(Buffer_getUserPtr(hOutBuf),
                                Buffer_getSize(hOutBuf));

                if (args->benchmark) {
                    if (Time_delta(hTime, &time) < 0) {
                        ret = Dmai_EFAIL;
                        fprintf(stderr,"Failed to get timer delta\n");
                        goto cleanup;
                    }

                    printf("Pre-process cache maintenance: %uus \n", (Uns) time);
                }
            }

            /* Make sure the whole buffer is used for input */
            BufferGfx_resetDimensions(hInBuf);

            /* Encode the video buffer */
            if (Venc1_process(hVe1, hInBuf, hOutBuf) < 0) {
                ret = Dmai_EFAIL;
                fprintf(stderr,"Failed to encode video buffer\n");
                goto cleanup;
            }

            /* if encoder generated output content, free released buffer */
            if (Buffer_getNumBytesUsed(hOutBuf)>0) {
                /* Get free buffer */
                hFreeBuf = Venc1_getFreeBuf(hVe1);
                /* Free buffer */
                BufTab_freeBuf(hFreeBuf);
            }
            /* if encoder did not generate output content */
            else {
                /* if non B frame sequence */
                /* encoder skipped frame probably exceeding target bitrate */
                if (params.maxInterFrameInterval<=1) {
                    /* free buffer */
                    printf(" Encoder generated 0 size frame\n");
                    BufTab_freeBuf(hInBuf);
                }
            }

            if (args->benchmark) {
                if (Time_delta(hTime, &time) < 0) {
                    ret = Dmai_EFAIL;
                    fprintf(stderr,"Failed to get encode time\n");
                    goto cleanup;
                }

                printf("[%d] Encode: %uus\n", numFrame, (Uns)time);
            }

            if (args->cache) {
                /* Writeback the outBuf. */
                Memory_cacheWb(Buffer_getUserPtr(hOutBuf),
                               Buffer_getSize(hOutBuf));

                if (args->benchmark) {
                    if (Time_delta(hTime, &time) < 0) {
                        ret = Dmai_EFAIL;
                        fprintf(stderr,"Failed to get timer delta\n");
                        goto cleanup;
                    }

                    printf("Post-process cache write back: %uus \n", (Uns) time);
                }
            }

            /* Write the encoded frame to the file system */
            if (Buffer_getNumBytesUsed(hOutBuf)) {
                if (fwrite(Buffer_getUserPtr(hOutBuf),
                           Buffer_getNumBytesUsed(hOutBuf), 1, outFile) != 1) {
                    ret = Dmai_EFAIL;
                    fprintf(stderr,"Failed to write encoded video data to file\n");
                    goto cleanup;
                }
            }

            /* Write the reconstructed frame to the file system */
            if (args->writeReconFrames) {
                processReconData(Venc1_getReconBufs(hVe1), hInBuf, hReconBuf);

                if (Buffer_getNumBytesUsed(hReconBuf)) {
                    if (fwrite(Buffer_getUserPtr(hReconBuf),
                               Buffer_getNumBytesUsed(hReconBuf), 1, reconFile) != 1) {
                        ret = Dmai_EFAIL;
                        fprintf(stderr,"Failed to write reconstructed frame to file\n");
                        goto cleanup;
                    }
                }
            }

            if (args->benchmark) {
                if (Time_delta(hTime, &time) < 0) {
                    ret = Dmai_EFAIL;
                    printf("Failed to get timer delta\n");
                    goto cleanup;
                }

                printf("File write time: %uus\n", (Uns)time);

                if (Time_total(hTime, &time) < 0) {
                    ret = Dmai_EFAIL;
                    fprintf(stderr,"Failed to get timer total\n");
                    goto cleanup;
                }

                printf("Total: %uus\n", (Uns)time);
            }
        }

        /* If the codec flushing completed, exit main thread */
        if (flushed) {
            /* Free dummy input buffer used for flushing process() calls */
            printf("freeing dummy input buffer ... \n");
            BufTab_freeBuf(hInBuf);
            break;
        }
    }

cleanup:
    /* Clean up the application */
    if (hOutBuf) {
        Buffer_delete(hOutBuf);
    }

    if (hReconBuf) {
        Buffer_delete(hReconBuf);
    }

    if (hVe1) {
        Venc1_delete(hVe1);
    }

    if (hBufTab) {
        BufTab_delete(hBufTab);
    }

    if (hEngine) {
        Engine_close(hEngine);
    }

    if (inFile) {
        fclose(inFile);
    }

    if (outFile) {
        fclose(outFile);
    }

    if (reconFile) {
        fclose(reconFile);
    }

    if (hTime) {
        Time_delete(hTime);
    }

    printf("End of application.\n");

    if (ret == Dmai_EFAIL)
        return 1;
    else
        return 0;
}
Exemple #10
0
/******************************************************************************
 * appMain
 ******************************************************************************/
Void appMain(Args * args)
{
    Buffer_Attrs            bAttrs      = Buffer_Attrs_DEFAULT;
    Time_Attrs              tAttrs      = Time_Attrs_DEFAULT;
    SPHENC1_Params          params      = Senc1_Params_DEFAULT;
    SPHENC1_DynamicParams   dynParams   = Senc1_DynamicParams_DEFAULT;
    Senc1_Handle            hSe1        = NULL;
    Engine_Handle           hEngine     = NULL;
    Buffer_Handle           hOutBuf     = NULL;
    Buffer_Handle           hInBuf      = NULL;
    Time_Handle             hTime       = NULL;
    FILE                   *inFile      = NULL;
    FILE                   *outFile     = NULL;
    Int                     numFrame    = 0;
    Int32                   bytesRead;
    UInt32                  time;

   printf("Starting application...\n");

   if (args->benchmark) {
        hTime = Time_create(&tAttrs);

        if (hTime == NULL) {
            printf("Failed to create Time object\n");
            goto cleanup;
        }
    }

    /* Initialize the codec engine run time */
    CERuntime_init();

    /* Initialize DMAI */
    Dmai_init();

    /* Open the output file */
    outFile = fopen(args->outFile, "wb");

    if (outFile == NULL) {
        printf("Failed to create output file %s\n", args->outFile);
        goto cleanup;
    }

    /* Open the input file */
    inFile = fopen(args->inFile, "rb");

    if (inFile == NULL) {
        printf("Failed to open input file %s\n", args->inFile);
        goto cleanup;
    }

    /* Using a larger vbuf to enhance performance of file i/o */
        if (setvbuf(outFile, vbuffer, _IOFBF, sizeof(vbuffer)) != 0) {
            printf("Failed to setvbuf on file descriptor\n");
            goto cleanup;
    }

    /* Open the codec engine */
    hEngine = Engine_open(args->engineName, NULL, NULL);

    if (hEngine == NULL) {
        printf("Failed to open codec engine %s\n", args->engineName);
        goto cleanup;
    }
    
    params.compandingLaw = args->compandingLaw;

    /* Create the SPHENC1 based speech encoder */
    hSe1 = Senc1_create(hEngine, args->codecName, &params, &dynParams);

    if (hSe1 == NULL) {
        printf("Failed to create %s\n", args->codecName);
        goto cleanup;
    }

    /* Align buffers to cache line boundary */    
    bAttrs.memParams.align = BUFSIZEALIGN; 
    
    /* Use cached buffers if requested */  
    if (args->cache) {
        bAttrs.memParams.flags = Memory_CACHED;
    } 
    
    /* Create an output buffer for encoded data */
    hOutBuf = Buffer_create(
        Dmai_roundUp(Senc1_getOutBufSize(hSe1), BUFSIZEALIGN), &bAttrs); 

    if (hOutBuf == NULL) {
        printf("Failed to create contiguous buffer\n");
        goto cleanup;
    }

    /* Create an input buffer for input data */
    hInBuf = Buffer_create(Dmai_roundUp(Senc1_getInBufSize(hSe1), BUFSIZEALIGN),
                           &bAttrs);

    if (hInBuf == NULL) {
        printf("Failed to create contiguous buffer\n");
        goto cleanup;
    }

    while (numFrame++ < args->numFrames) {
        printf("Frame %d: ", numFrame);
        if (args->benchmark) {
            if (Time_reset(hTime) < 0) {
                printf("Failed to reset timer\n");
                goto cleanup;
            }
        }

        /* Read raw PCM data from input file */
        bytesRead = fread(Buffer_getUserPtr(hInBuf), 1,
                          Senc1_getInBufSize(hSe1), inFile);

        if (bytesRead < Senc1_getInBufSize(hSe1)) {
            if (ferror(inFile)) {
                printf("Failed to read data from input file\n");
                goto cleanup;
            }
             printf("Failed to read full frame %ld bytes, read %ld bytes\n",
                                    Senc1_getInBufSize(hSe1),bytesRead);
             goto cleanup;
        }

        Buffer_setNumBytesUsed(hInBuf, bytesRead);

        if (args->benchmark) {
            if (Time_delta(hTime, &time) < 0) {
                printf("Failed to get timer delta\n");
                goto cleanup;
            }
            printf("Read: %uus ", (Uns) time);
        }

        if (args->cache) {
            /*
             *  To meet xDAIS DMA Rule 7, when input buffers are cached, we
             *  must writeback the cache into physical memory.  Also, per DMA
             *  Rule 7, we must invalidate the output buffer from
             *  cache before providing it to any xDAIS algorithm.
             */
            Memory_cacheWbInv(Buffer_getUserPtr(hInBuf),
                              Buffer_getSize(hInBuf));

            /* Per DMA Rule 7, our output buffer cache lines must be cleaned */
            Memory_cacheInv(Buffer_getUserPtr(hOutBuf),
                            Buffer_getSize(hOutBuf));

            if (args->benchmark) {
                if (Time_delta(hTime, &time) < 0) {
                    printf("Failed to get timer delta\n");
                    goto cleanup;
                }
                printf("Pre-process cache maintenance: %uus ", (Uns) time);
            }
        }

        /* Encode the speech buffer */
        if (Senc1_process(hSe1, hInBuf, hOutBuf) < 0) {
            printf("Failed to encode speech buffer\n");
            goto cleanup;
        }

        if (args->benchmark) {
            if (Time_delta(hTime, &time) < 0) {
                printf("Failed to get timer delta\n");
                goto cleanup;
            }
            printf("Encode: %uus ", (Uns) time);
        }

        if (args->cache) {
            /* Writeback the outBuf. */
            Memory_cacheWb(Buffer_getUserPtr(hOutBuf), Buffer_getSize(hOutBuf));

            if (args->benchmark) {
                if (Time_delta(hTime, &time) < 0) {
                    printf("Failed to get timer delta\n");
                    goto cleanup;
                }
                printf("Post-process cache write back: %uus ", (Uns) time);
            }
        }

        printf("Write encoded speech data to output file \n");

        /* Write the encoded frame to the file system */
        if (Buffer_getNumBytesUsed(hOutBuf)) {
            if (fwrite(Buffer_getUserPtr(hOutBuf),
                       Buffer_getNumBytesUsed(hOutBuf), 1, outFile) != 1) {
                printf("Failed to write encoded speech data to file\n");
                goto cleanup;
            }
        }

        if (args->benchmark) {
            if (Time_delta(hTime, &time) < 0) {
                printf("Failed to get timer delta\n");
                goto cleanup;
            }
            printf("Write: %uus ", (Uns) time);

            if (Time_total(hTime, &time) < 0) {
                printf("Failed to get timer total\n");
                goto cleanup;
            }

            printf("Total: %uus\n", (unsigned int)time);
        }
    }
cleanup:
    /* Clean up the application */
    if (hSe1) {
        Senc1_delete(hSe1);
    }

    if (hInBuf) {
        Buffer_delete(hInBuf);
    }

    if (hOutBuf) {
        Buffer_delete(hOutBuf);
    }

    if (hEngine) {
        Engine_close(hEngine);
    }

    if (hTime) {
        Time_delete(hTime);
    }

    if (inFile) {
        fclose(inFile);
    }

    if (outFile) {
        fclose(outFile);
    }

    printf("End of application.\n");

    return;
}
Exemple #11
0
/******************************************************************************
 * main
 ******************************************************************************/
Int appMain(Args * args)
{
    Buffer_Attrs            bAttrs      = Buffer_Attrs_DEFAULT;
    Loader_Attrs            lAttrs      = Loader_Attrs_DEFAULT;
    AUDDEC1_Params          params      = Adec1_Params_DEFAULT;
    AUDDEC1_DynamicParams   dynParams   = Adec1_DynamicParams_DEFAULT;
    Time_Attrs              tAttrs      = Time_Attrs_DEFAULT;
    Adec1_Handle            hAd1        = NULL;
    Loader_Handle           hLoader     = NULL;
    Engine_Handle           hEngine     = NULL;
    Buffer_Handle           hOutBuf     = NULL;
    Time_Handle             hTime       = NULL;
    Buffer_Handle           hInBuf      = NULL;
    FILE                   *outFile     = NULL;
    Int                     numFrame    = 0;
    UInt32                  time;
    Int                     ret         = Dmai_EOK;
    Cpu_Device              device;

    printf("Starting application...\n");
        
    if (args->benchmark) {
        hTime = Time_create(&tAttrs);

        if (hTime == NULL) {
            ret = Dmai_EFAIL;
            fprintf(stderr, "Failed to create Time object\n");
            goto cleanup;
        }
    }

    /* Initialize the codec engine run time */
    CERuntime_init();

    /* Initialize DMAI */
    Dmai_init();

    /* Determine which device the application is running on */
    if (Cpu_getDevice(NULL, &device) < 0) {
        ret = Dmai_EFAIL;
        fprintf(stderr, "Failed to determine target board\n");
        goto cleanup;
    }

    /* Open the output file */
    outFile = fopen(args->outFile, "wb");

    if (outFile == NULL) {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Failed to create output file %s\n", args->outFile);
        goto cleanup;
    }

    /* Using a larger vbuf to enhance performance of file i/o */
    if (setvbuf(outFile, vbuffer, _IOFBF, sizeof(vbuffer)) != 0) {
        ret = Dmai_EFAIL;
        fprintf(stderr, "Failed to setvbuf on file descriptor\n");
        goto cleanup;
    }

    /* Open the codec engine */
    hEngine = Engine_open(args->engineName, NULL, NULL);

    if (hEngine == NULL) {
        ret = Dmai_EFAIL;
        fprintf(stderr, "Failed to open codec engine %s\n", args->engineName);
        goto cleanup;
    }

    if (device == Cpu_Device_DM365 || device == Cpu_Device_OMAP3530 ||
        device == Cpu_Device_DM368 || device == Cpu_Device_DM3730) {
        params.dataEndianness = XDM_LE_16;
    }


    /* Create the AUDDEC1 based audio decoder */
    hAd1 = Adec1_create(hEngine, args->codecName, &params, &dynParams);

    if (hAd1 == NULL) {
        ret = Dmai_EFAIL;
        fprintf(stderr, "Failed to create audio decoder\n");
        goto cleanup;
    }

    /* Align buffers to cache line boundary */    
    bAttrs.memParams.align = lAttrs.mParams.align = BUFSIZEALIGN; 
    
    /* Use cached buffers if requested */    
    if (args->cache) {
        bAttrs.memParams.flags = lAttrs.mParams.flags = Memory_CACHED;
    } 
    
    /* Ask the codec how much input data it needs */
    lAttrs.readSize = Adec1_getInBufSize(hAd1);

    /* Make the total ring buffer larger */
    lAttrs.readBufSize = Dmai_roundUp(lAttrs.readSize * 10, BUFSIZEALIGN);

    /* Increase the stdio buffer size for loader for better RTDX performance */
    lAttrs.vBufSize = VBUFSIZE;

    /* Create the file loader */
    hLoader = Loader_create(args->inFile, &lAttrs);

    if (hLoader == NULL) {
        ret = Dmai_EFAIL;
        fprintf(stderr, "Failed to create loader\n");
        goto cleanup;
    }

    /* Create an output buffer for decoded data */
    hOutBuf = Buffer_create(
        Dmai_roundUp(Adec1_getOutBufSize(hAd1), BUFSIZEALIGN), &bAttrs);

    if (hOutBuf == NULL) {
        ret = Dmai_EFAIL;
        fprintf(stderr, "Failed to create contiguous buffers\n");
        goto cleanup;
    }

    /* Prime the file loader */
    Loader_prime(hLoader, &hInBuf);

    while (numFrame++ < args->numFrames) {
        if (args->benchmark) {
            if (Time_reset(hTime) < 0) {
                ret = Dmai_EFAIL;
                fprintf(stderr, "Failed to reset timer\n");
                goto cleanup;
            }
        }

        if (args->cache) {
            /*
             *  To meet xDAIS DMA Rule 7, when input buffers are cached, we
             *  must writeback the cache into physical memory.  Also, per DMA
             *  Rule 7, we must invalidate the output buffer from
             *  cache before providing it to any xDAIS algorithm.
             */
            Memory_cacheWbInv(Buffer_getUserPtr(hInBuf),Buffer_getSize(hInBuf));

            /* Per DMA Rule 7, our output buffer cache lines must be cleaned */
            Memory_cacheInv(Buffer_getUserPtr(hOutBuf),Buffer_getSize(hOutBuf));

            if (args->benchmark) {
                if (Time_delta(hTime, &time) < 0) {
                    ret = Dmai_EFAIL;
                    fprintf(stderr,"Failed to get timer delta\n");
                    goto cleanup;
                }

                printf("Pre-process cache maintenance: %uus ", (Uns) time);
            }
        }

        /* Decode the audio buffer */
        ret = Adec1_process(hAd1, hInBuf, hOutBuf);

        if ((ret == Dmai_EFAIL)||
            (ret == Dmai_EBITERROR && Buffer_getNumBytesUsed(hInBuf) == 0)) {
            ret = Dmai_EFAIL;
            fprintf(stderr, "Failed to decode audio buffer\n");
            goto cleanup;
        }

        if (args->benchmark) {
            if (Time_delta(hTime, &time) < 0) {
                ret = Dmai_EFAIL;
                fprintf(stderr, "Failed to get timer delta\n");
                goto cleanup;
            }

            printf("Decode: %uus ", (Uns) time);
        }

        if (args->cache) {
            /* Writeback the outBuf. */
            Memory_cacheWb(Buffer_getUserPtr(hOutBuf), Buffer_getSize(hOutBuf));

            if (args->benchmark) {
                if (Time_delta(hTime, &time) < 0) {
                    ret = Dmai_EFAIL;
                    fprintf(stderr, "Failed to get timer delta\n");
                    goto cleanup;
                }

                printf("Post-process cache write back: %uus ", (Uns) time);
            }
        }

        /* Load a new frame from the file system */
        Loader_getFrame(hLoader, hInBuf);

        if (args->benchmark) {
            if (Time_delta(hTime, &time) < 0) {
                ret = Dmai_EFAIL;
                fprintf(stderr,"Failed to get timer delta\n");
                goto cleanup;
            }

            printf("Loader: %uus\n", (Uns) time);
        }

        if (Buffer_getNumBytesUsed(hOutBuf)) {
            if (numFrame >= args->startFrame) {
                printf("Frame %d: ", numFrame);
                if (writeFrame(hOutBuf, outFile) < 0) {
                    ret = Dmai_EFAIL;
                    goto cleanup;
                }
            }
        }

        if (Buffer_getUserPtr(hInBuf) == NULL) {
            printf("Loader returned null, clip finished\n");
            break;
        }

        if (args->benchmark) {
            if (Time_total(hTime, &time) < 0) {
                ret = Dmai_EFAIL;
                fprintf(stderr,"Failed to get timer total\n");
                goto cleanup;
            }

            printf("Total: %uus\n", (unsigned int)time);
        }
    }

cleanup:
    /* Clean up the application */
    if (hLoader) {
        Loader_delete(hLoader);
    }

    if (hAd1) {
        Adec1_delete(hAd1);
    }

    if (hOutBuf) {
        Buffer_delete(hOutBuf);
    }

    if (hEngine) {
        Engine_close(hEngine);
    }

    if (hTime) {
        Time_delete(hTime);
    }

    if (outFile) {
        fclose(outFile);
    }

    printf("End of application.\n");

    if (ret == Dmai_EFAIL)
        return 1;
    else
        return 0;
}
Exemple #12
0
/******************************************************************************
 * appMain
 ******************************************************************************/
Int appMain(Args * args)
{
    IMGDEC1_Params          params       = Idec1_Params_DEFAULT;
    IMGDEC1_DynamicParams   dynParams    = Idec1_DynamicParams_DEFAULT;
    Buffer_Attrs            bAttrs       = Buffer_Attrs_DEFAULT;
    BufferGfx_Attrs         gfxAttrs     = BufferGfx_Attrs_DEFAULT;
    Time_Attrs              tAttrs       = Time_Attrs_DEFAULT;   
    Idec1_Handle            hId          = NULL;
    Engine_Handle           hEngine      = NULL;
    Time_Handle             hTime        = NULL;
    Buffer_Handle           hInBuf       = NULL;
    Buffer_Handle           hOutBuf      = NULL;
    FILE                   *outFile      = NULL;
    FILE                   *inFile       = NULL;
    Int                     numBytes     = 0;
    Int                     ret          = Dmai_EOK;
    Cpu_Device              device;
    UInt32                  time;

    printf("Starting application...\n");
     
    if (args->benchmark) {
        hTime = Time_create(&tAttrs);

        if (hTime == NULL) {
            ret = Dmai_EFAIL;
            fprintf(stderr,"Failed to create Time object\n");
            goto cleanup;
        }
    }

    /* Initialize the codec engine run time */
    CERuntime_init();

    /* Initialize DMAI */
    Dmai_init();

    /* Determine which device the application is running on */
    if (Cpu_getDevice(NULL, &device) < 0) {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Failed to determine target board\n");
        goto cleanup;
    }

    /* Open input file */
    inFile = fopen(args->inFile, "rb");

    if (inFile == NULL) {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Failed to open file %s\n", args->inFile);
        goto cleanup;
    }
    
    /* Open output file */
    outFile = fopen(args->outFile, "wb");
    
    if (outFile == NULL) {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Failed to create output file %s\n", args->outFile);
        goto cleanup;
    }

    /* Using a larger vbuf to enhance performance of file i/o */
    if (setvbuf(outFile, vbuffer, _IOFBF, sizeof(vbuffer)) != 0) {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Failed to setvbuf on file descriptor\n");
        goto cleanup;   
    }
    
    /* Open the codec engine */
    hEngine = Engine_open(args->engineName, NULL, NULL);

    if (hEngine == NULL) {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Failed to open codec engine %s\n", args->engineName);
        goto cleanup;
    }

    /*
     * Set output color format to UYVY or Planar output.
     * Here XDM_DEFUALT sets the output planar format to
     * the planar fromat of the encoded image.
     *           
     */
    switch (args->oColorSpace) {
        case ColorSpace_UYVY:
            params.forceChromaFormat = XDM_YUV_422ILE;
            break;
        case ColorSpace_NOTSET:
            params.forceChromaFormat = XDM_CHROMAFORMAT_DEFAULT;
            break;
        case ColorSpace_YUV444P:
            params.forceChromaFormat = XDM_YUV_444P;
            break;
        case ColorSpace_YUV422P:
            params.forceChromaFormat = XDM_YUV_422P;
            break;
        case ColorSpace_YUV420P:
            params.forceChromaFormat = XDM_YUV_420P;
            break;
        case ColorSpace_YUV420PSEMI:
            params.forceChromaFormat = XDM_YUV_420SP;
            break;
        case ColorSpace_GRAY:
            params.forceChromaFormat = ColorSpace_GRAY;
            break;
        default:
            ret = Dmai_EFAIL;
            fprintf(stderr,"Unsupported output color space %d.\n", args->oColorSpace);
            goto cleanup;
    }

    if ((device == Cpu_Device_DM365) || (device == Cpu_Device_DM368)) {
        params.maxHeight = VideoStd_720P_HEIGHT;
        params.maxWidth  = VideoStd_720P_WIDTH;
    }

    if (device == Cpu_Device_DM6467) {
        params.maxHeight = VideoStd_720P_HEIGHT;
        params.maxWidth  = VideoStd_720P_WIDTH;
    }

    /* Create the image decoder */
    hId = Idec1_create(hEngine, args->codecName, &params, &dynParams);

    if (hId == NULL) {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Failed to create image decoder: %s\n", args->codecName);
        goto cleanup;
    }

    /* Align buffers to cache line boundary */    
    gfxAttrs.bAttrs.memParams.align = bAttrs.memParams.align = BUFSIZEALIGN; 
    
    /* Use cached buffers if requested */    
    if (args->cache) {
        gfxAttrs.bAttrs.memParams.flags = bAttrs.memParams.flags 
            = Memory_CACHED;
    } 
    
    gfxAttrs.colorSpace     = args->oColorSpace;
    gfxAttrs.dim.width      = params.maxWidth;
    gfxAttrs.dim.height     = params.maxHeight;
    gfxAttrs.dim.lineLength = BufferGfx_calcLineLength(params.maxWidth,
                                                       gfxAttrs.colorSpace);
    
    /* Create an output buffer for decoded data */
    hOutBuf = Buffer_create(
        Dmai_roundUp(Idec1_getOutBufSize(hId), BUFSIZEALIGN), 
        BufferGfx_getBufferAttrs(&gfxAttrs));

    if (hOutBuf == NULL) {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Failed to create contiguous buffers\n");
        goto cleanup;
    }

    /* Create an input buffer for encoded data */
    hInBuf = Buffer_create(Dmai_roundUp(Idec1_getInBufSize(hId), BUFSIZEALIGN), 
        &bAttrs);

    if (hInBuf == NULL) {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Failed to create contiguous buffers\n");
        goto cleanup;
    }
   
    /* Read encoded image data */
    numBytes = fread(Buffer_getUserPtr(hInBuf), 1,
                     Idec1_getInBufSize(hId), inFile);
                     
    Buffer_setNumBytesUsed(hInBuf, numBytes);

    if (args->benchmark) {
        if (Time_reset(hTime) < 0) {
            ret = Dmai_EFAIL;
            fprintf(stderr,"Failed to reset timer\n");
            goto cleanup;
        }
    }

    if (args->cache) {
        /*  
         *  To meet xDAIS DMA Rule 7, when input buffers are cached, we 
         *  must writeback the cache into physical memory.  Also, per DMA 
         *  Rule 7, we must invalidate the output buffer from
         *  cache before providing it to any xDAIS algorithm.
         */
        Memory_cacheWbInv(Buffer_getUserPtr(hInBuf), Buffer_getSize(hInBuf));
        /* Per DMA Rule 7, our output buffer cache lines must be cleaned */
        Memory_cacheInv(Buffer_getUserPtr(hOutBuf), Buffer_getSize(hOutBuf));
        if (args->benchmark) {
            if (Time_delta(hTime, &time) < 0) {
                ret = Dmai_EFAIL;
                fprintf(stderr,"Failed to get timer delta\n");
                goto cleanup;
            }
    
            printf("Pre-process cache maintenance: %uus \n", (Uns) time);
        }
    }

    printf("Decoding image...\n");
        
    /* Decode the image frame */
    ret = Idec1_process(hId, hInBuf, hOutBuf);
    
    if (ret < 0) {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Failed to decode image buffer\n");
        goto cleanup;
    }
    
    if (args->benchmark) {
        if (Time_delta(hTime, &time) < 0) {
            ret = Dmai_EFAIL;
            fprintf(stderr,"Failed to get timer delta\n");
            goto cleanup;
        }
    
        printf("Frame - Decode: %uus \n", (unsigned int)time);
    }
    
    if (args->cache) {
        /* Writeback the outBuf. */
        Memory_cacheWb(Buffer_getUserPtr(hOutBuf), Buffer_getSize(hOutBuf));
    
        if (args->benchmark) {
            if (Time_delta(hTime, &time) < 0) {
                ret = Dmai_EFAIL;
                fprintf(stderr,"Failed to get timer delta\n");
                goto cleanup;
            }
    
            printf("Post-process cache write back: %uus ", (Uns) time);
        }
    }

    /* Write decoded image to a file */
   if (BufferGfx_getColorSpace(hOutBuf) == ColorSpace_UYVY){
        if (writeFrameUYVY(hOutBuf, outFile) < 0) {
           ret = Dmai_EFAIL;
           fprintf(stderr,"Failed to write image to file\n");
           goto cleanup; 
       }
    } else if (BufferGfx_getColorSpace(hOutBuf) == ColorSpace_YUV420PSEMI) {
        if (writeFrameSemiPlanar(hOutBuf, outFile) < 0) {
           ret = Dmai_EFAIL;
           fprintf(stderr,"Failed to write image to file\n");
           goto cleanup; 
       }
    }
    else if (BufferGfx_getColorSpace(hOutBuf) == ColorSpace_YUV420P
             || ColorSpace_YUV422P || ColorSpace_YUV444P || ColorSpace_GRAY){
        /* For XDM_GRAY ignoring the color planes */
        if (args->oColorSpace == ColorSpace_GRAY){
            BufferGfx_setColorSpace (hOutBuf, ColorSpace_GRAY); 
        }        
        if (writeFramePlanar(hOutBuf, outFile) < 0) {
           ret = Dmai_EFAIL;
           fprintf(stderr,"Failed to write image to file\n");
           goto cleanup; 
       }
    }
    else {
        ret = Dmai_EFAIL;
        fprintf(stderr,"Invalid output colorspace.\n");
        goto cleanup;     
    }

    if (args->benchmark) {
        if (Time_total(hTime, &time) < 0) {
            ret = Dmai_EFAIL;
            fprintf(stderr,"Failed to get timer total\n");
            goto cleanup;
        }
    
        printf("Total: %uus\n", (unsigned int)time);
    }
       
cleanup:
    /* Clean up the application */
    if (hId) {
        Idec1_delete(hId);
    }
    
    if (hInBuf) {
        Buffer_delete(hInBuf);
    }

    if (hOutBuf) {
        Buffer_delete(hOutBuf);
    }

    if (hEngine) {
        Engine_close(hEngine);
    }

    if (hTime) {
        Time_delete(hTime);
    }

    if (inFile) {
        fclose(inFile);
    }
 
    if (outFile) {
        fclose(outFile);
    }
    
    printf("End of application.\n");

    if (ret == Dmai_EFAIL) 
        return 1;
    else    
        return 0;
}
Exemple #13
0
/*****************************************************************************
 * gst_tiprepencbuf_copy_input
 *  Make the input data in src available in the physically contiguous memory
 *  in dst in the best way possible.  Preferably an accelerated copy or
 *  color conversion.
 ****************************************************************************/
static Int
gst_tiprepencbuf_copy_input(GstTIPrepEncBuf * prepencbuf,
    Buffer_Handle hDstBuf, GstBuffer * src)
{
    Framecopy_Attrs  fcAttrs = Framecopy_Attrs_DEFAULT;
    gboolean         accel   = FALSE;
    Buffer_Handle    hInBuf  = NULL;
    Int              ret     = -1;

#if defined(Platform_dm365) || defined(Platform_dm368)
    BufferGfx_Dimensions dim;
#endif

    /* Check to see if we need to execute ccv on dm6467 */
    if (prepencbuf->device == Cpu_Device_DM6467 &&
        prepencbuf->srcColorSpace == ColorSpace_YUV422PSEMI) {
        return gst_tiprepencbuf_422psemi_420psemi(hDstBuf, src, prepencbuf);
    }

    GST_LOG("gst_tiphyscontig_copy_input - begin\n");

    if (prepencbuf->hFc == NULL) {
        /* Enable the accel framecopy based on contiguousInputFrame.
         * If accel is set to FALSE then DMAI will use regular memcpy function
         * else will use HW accelerated framecopy.
         */

        /* If we are getting dmai transport buffer then enable HW 
         * acceleration */
        if (GST_IS_TIDMAIBUFFERTRANSPORT(src)) {
            accel = TRUE;
        }
        else {
            accel = prepencbuf->contiguousInputFrame;
        }

        fcAttrs.accel = prepencbuf->contiguousInputFrame;

        prepencbuf->hFc = Framecopy_create(&fcAttrs);
        if (prepencbuf->hFc == NULL) {
            GST_ERROR("failed to create framecopy handle\n");
            goto exit;
        }

        GST_INFO("HW accel framecopy: %s\n", accel ? "enabled" : "disabled");
    }

    /* Prepare input buffer */
    hInBuf = gst_tiprepencbuf_convert_gst_to_dmai(prepencbuf, src, TRUE);
    if (hInBuf == NULL) {
        GST_ERROR("failed to get dmai buffer\n");
        goto exit;
    }

#if defined(Platform_dm365) || defined(Platform_dm368)
    /* Handle resizer 32-byte issue on DM365 platform */
    if (prepencbuf->device == Cpu_Device_DM365 || prepencbuf->device == Cpu_Device_DM368) {
        if ((prepencbuf->srcColorSpace == ColorSpace_YUV420PSEMI)) {
            BufferGfx_getDimensions(hInBuf, &dim);
            dim.lineLength = Dmai_roundUp(dim.lineLength, 32);
            BufferGfx_setDimensions(hInBuf, &dim);
        }
    }
#endif

    /* Prepare output buffer */
    if (Framecopy_config(prepencbuf->hFc, hInBuf, hDstBuf) < 0) {
        GST_ERROR("failed to configure framecopy\n");
        goto exit;
    }

    if (Framecopy_execute(prepencbuf->hFc, hInBuf, hDstBuf) < 0) {
        GST_ERROR("failed to execute framecopy\n");
        goto exit;
    }

    ret = GST_BUFFER_SIZE(src);

exit:
    if (hInBuf) {
        Buffer_delete(hInBuf);
    }

    GST_LOG("gst_tiprepencbuf_copy_input - end\n");
    return ret;
}
Exemple #14
0
/******************************************************************************
 * gst_tiprepencbuf_422psemi_420psemi
 *  this function color convert YUV422PSEMI to YUV420PSEMI.
 *****************************************************************************/
static Int
gst_tiprepencbuf_422psemi_420psemi(Buffer_Handle hDstBuf, GstBuffer * src,
    GstTIPrepEncBuf * prepencbuf)
{
    Ccv_Attrs     ccvAttrs = Ccv_Attrs_DEFAULT;
    gboolean      accel    = FALSE;
    Buffer_Handle hInBuf   = NULL;
    Int           ret      = -1;

    GST_LOG("gst_tiprepencbuf_422psemi_420psemi - begin\n");

    /* create ccv handle */
    if (prepencbuf->hCcv == NULL) {
        /* Enable the accel ccv based on contiguousInputFrame.
         * If accel is set to FALSE then DMAI will use software ccv function
         * else will use HW accelerated ccv engine.
         */

        /* If we are getting dmai transport buffer then enable HW 
         * acceleration */
        if (GST_IS_TIDMAIBUFFERTRANSPORT(src)) {
            accel = TRUE;
        }
        else {
            accel = prepencbuf->contiguousInputFrame;
        }

        ccvAttrs.accel = prepencbuf->contiguousInputFrame;
        prepencbuf->hCcv = Ccv_create(&ccvAttrs);
        if (prepencbuf->hCcv == NULL) {
            GST_ERROR("failed to create CCV handle\n");
            goto exit;
        }

        GST_INFO("HW accel CCV: %s\n", accel ? "enabled" : "disabled");
    }

    /* Prepare input buffer */
    hInBuf = gst_tiprepencbuf_convert_gst_to_dmai(prepencbuf, src, TRUE);
    if (hInBuf == NULL) {
        GST_ERROR("failed to get dmai buffer\n");
        goto exit;
    }

    /* Prepare output buffer */
    if (Ccv_config(prepencbuf->hCcv, hInBuf, hDstBuf) < 0) {
        GST_ERROR("failed to config CCV handle\n");
        goto exit;
    }

    if (Ccv_execute(prepencbuf->hCcv, hInBuf, hDstBuf) < 0) {
        GST_ERROR("failed to execute Ccv handle\n");
        goto exit;
    }

    ret = GST_BUFFER_SIZE(src);

    GST_LOG("gst_tiprepencbuf_422psemi_420psemi - end\n");

exit:
    if (hInBuf) {
        Buffer_delete(hInBuf);
    }

    return ret;
}
Exemple #15
0
/******************************************************************************
 * appMain
 ******************************************************************************/
Void appMain(Args * args)
{
    VIDENC_Params           params      = Venc_Params_DEFAULT;
    VIDENC_DynamicParams    dynParams   = Venc_DynamicParams_DEFAULT;
    BufferGfx_Attrs         gfxAttrs    = BufferGfx_Attrs_DEFAULT;
    Buffer_Attrs            bAttrs      = Buffer_Attrs_DEFAULT;
    Time_Attrs              tAttrs      = Time_Attrs_DEFAULT;
    Venc_Handle             hVe         = NULL;
    FILE                   *outFile     = NULL;
    FILE                   *inFile      = NULL;
    Engine_Handle           hEngine     = NULL;
    Time_Handle             hTime       = NULL;
    Int                     numFrame    = 0;
    Buffer_Handle           hOutBuf     = NULL;
    Buffer_Handle           hInBuf      = NULL;
    ColorSpace_Type         colorSpace;
    UInt32                  time;

    printf("Starting application...\n");
    
    /* Initialize the codec engine run time */
    CERuntime_init();
    
    /* Initialize DMAI */
    Dmai_init();
    
    if (args->benchmark) {
        hTime = Time_create(&tAttrs);

        if (hTime == NULL) {
            printf("Failed to create Time object\n");
            goto cleanup;
        }
    }

    /* Open input file */
    inFile = fopen(args->inFile, "rb");
    
    if (inFile == NULL) {
        printf("Failed to open input file %s\n", args->inFile);
        goto cleanup;
    }

    /* Using a larger vbuf to enhance performance of file i/o */
    if (setvbuf(inFile, vbufferIn, _IOFBF, sizeof(vbufferIn)) != 0) {
        printf("Failed to setvbuf on input file descriptor\n");
        goto cleanup;   
    }   

    
    /* Open output file */
    outFile = fopen(args->outFile, "wb");

    if (outFile == NULL) {
        printf("Failed to open output file %s\n", args->outFile);
        goto cleanup;
    }

    /* Using a larger vbuf to enhance performance of file i/o */
    if (setvbuf(outFile, vbufferOut, _IOFBF, sizeof(vbufferOut)) != 0) {
        printf("Failed to setvbuf on output file descriptor\n");
        goto cleanup;   
    }
    
    /* Open the codec engine */
    hEngine = Engine_open(args->engineName, NULL, NULL);

    if (hEngine == NULL) {
        printf("Failed to open codec engine: %s\n", args->engineName);
        goto cleanup;
    }

    params.maxWidth = args->width;
    params.maxHeight = args->height;

    /* Set up codec parameters depending on bit rate */
    if (args->bitRate < 0) {
        /* Variable bit rate */
        params.rateControlPreset = IVIDEO_NONE;

        /*
         * If variable bit rate use a bogus bit rate value (> 0)
         * since it will be ignored.
         */
        params.maxBitRate        = 2000000;
    }
    else {
        /* Constant bit rate */
        params.rateControlPreset = IVIDEO_LOW_DELAY;
        params.maxBitRate        = args->bitRate;
    }

    params.inputChromaFormat = XDM_YUV_422ILE;

    dynParams.targetBitRate = params.maxBitRate;
    dynParams.inputWidth = params.maxWidth;
    dynParams.inputHeight = params.maxHeight;
    
    /* Create the video encoder */
    hVe = Venc_create(hEngine, args->codecName, &params, &dynParams);

    if (hVe == NULL) {
        printf("Failed to create video encoder: %s\n", args->codecName);
        goto cleanup;
    }

    /* Only the UYVY colorspace is supported in this application */
    colorSpace = ColorSpace_UYVY;

    /* Align buffers to cache line boundary */    
    gfxAttrs.bAttrs.memParams.align = bAttrs.memParams.align = BUFSIZEALIGN; 
    
    /* Use cached buffers if requested */    
    if (args->cache) {
        gfxAttrs.bAttrs.memParams.flags = bAttrs.memParams.flags 
            = Memory_CACHED;
    } 
    
    /* Calculate the buffer attributes */
    gfxAttrs.dim.width = args->width;
    gfxAttrs.dim.height = args->height;
    gfxAttrs.dim.lineLength = BufferGfx_calcLineLength(args->width, colorSpace);
    gfxAttrs.colorSpace = colorSpace;

    /* Create input buffer */
    hInBuf = Buffer_create(Dmai_roundUp(Venc_getInBufSize(hVe), BUFSIZEALIGN),
                           BufferGfx_getBufferAttrs(&gfxAttrs));

    if (hInBuf == NULL) {
        printf("Failed to allocate contiguous buffer\n");
        goto cleanup;
    }

    /* Create output buffer */
    hOutBuf = Buffer_create(Dmai_roundUp(Venc_getOutBufSize(hVe), BUFSIZEALIGN),
        &bAttrs);

    if (hOutBuf == NULL) {
        printf("Failed to create contiguous buffer\n");
        goto cleanup;
    }

    while (numFrame++ < args->numFrames) {
        /* Read a yuv input frame */
        printf("Frame %d: ", numFrame);
        if (readFrameUYVY(hInBuf, inFile) < 0) {
            goto cleanup;        
        }
        
        if (args->benchmark) {
            if (Time_reset(hTime) < 0) {
                printf("Failed to reset timer\n");
                goto cleanup;
            }
        }

        if (args->cache) {
            /*  
             *  To meet xDAIS DMA Rule 7, when input buffers are cached, we 
             *  must writeback the cache into physical memory.  Also, per DMA 
             *  Rule 7, we must invalidate the output buffer from
             *  cache before providing it to any xDAIS algorithm.
             */
            Memory_cacheWbInv(Buffer_getUserPtr(hInBuf), Buffer_getSize(hInBuf));
    
            /* Per DMA Rule 7, our output buffer cache lines must be cleaned */
            Memory_cacheInv(Buffer_getUserPtr(hOutBuf), Buffer_getSize(hOutBuf));
    
            if (args->benchmark) {
                if (Time_delta(hTime, &time) < 0) {
                    printf("Failed to get timer delta\n");
                    goto cleanup;
                }
    
                printf("Pre-process cache maintenance: %uus \n", (Uns) time);
            }
        }

        /* Encode the video buffer */
        if (Venc_process(hVe, hInBuf, hOutBuf) < 0) {
            printf("Failed to encode video buffer\n");
            goto cleanup;
        }

        if (args->benchmark) {
            if (Time_delta(hTime, &time) < 0) {
                printf("Failed to get encode time\n");
                goto cleanup;
            }

            printf("[%d] Encode: %uus\n", numFrame, (Uns)time);
        }

        if (args->cache) {
            /* Writeback the outBuf. */
            Memory_cacheWb(Buffer_getUserPtr(hOutBuf), Buffer_getSize(hOutBuf));
    
            if (args->benchmark) {
                if (Time_delta(hTime, &time) < 0) {
                    printf("Failed to get timer delta\n");
                    goto cleanup;
                }
    
                printf("Post-process cache write back: %uus \n", (Uns) time);
            }
        }
        
        /* Write the encoded frame to the file system */
        if (Buffer_getNumBytesUsed(hOutBuf)) {
            if (fwrite(Buffer_getUserPtr(hOutBuf),
                       Buffer_getNumBytesUsed(hOutBuf), 1, outFile) != 1) {
                printf("Failed to write encoded video data to file\n");
                goto cleanup;
            }
        }
        
        if (args->benchmark) {
            if (Time_total(hTime, &time) < 0) {
                printf("Failed to get timer total\n");
                goto cleanup;
            }

            printf("Total: %uus\n", (unsigned int)time);
        }
    }

cleanup:
    /* Clean up the application */
    if (hOutBuf) {
        Buffer_delete(hOutBuf);
    }

    if (hInBuf) {
        Buffer_delete(hInBuf);
    }

    if (hVe) {
        Venc_delete(hVe);
    }

    if (hEngine) {
        Engine_close(hEngine);
    }

    if (inFile) {
        fclose(inFile);
    }
   
    if (outFile) {
        fclose(outFile);
    }

    if (hTime) {
        Time_delete(hTime);
    }

    printf("End of application.\n");
    
    return;
}