Exemplo n.º 1
0
/******************************************************************************
 * gst_ticircbuffer_shift_data
 *    Look for uncopied data in the last window and move it to the first one.
 ******************************************************************************/
static gboolean gst_ticircbuffer_shift_data(GstTICircBuffer *circBuf)
{
    Int8*     firstWindow   = Buffer_getUserPtr(circBuf->hBuf);
    Int32     lastWinOffset = Buffer_getSize(circBuf->hBuf) -
                              (circBuf->windowSize + circBuf->readAheadSize);
    Int8*     lastWindow    = firstWindow + lastWinOffset;
    Int32     bytesToCopy   = 0;
    gboolean  writePtrReset = FALSE;

    /* In fixedBlockSize mode, just wait until the write poitner reaches the
     * end of the buffer and then reset it to the beginning (no copying).
     */
    if (circBuf->fixedBlockSize && circBuf->writePtr >= circBuf->readPtr) {
        if (circBuf->writePtr == firstWindow + Buffer_getSize(circBuf->hBuf)) {
            GST_LOG("resetting write pointer (%lu->0)\n",
                (UInt32)(circBuf->writePtr - firstWindow));
            circBuf->writePtr       = Buffer_getUserPtr(circBuf->hBuf);
            circBuf->contiguousData = FALSE;
        }
        return TRUE;
    }

    /* Otherwise copy unconsumed data from the last window to the first one
     * and reset the write pointer back to the first window.
     */
    if (gst_ticircbuffer_first_window_free(circBuf) &&
        circBuf->writePtr >= circBuf->readPtr       &&
        circBuf->writePtr >= lastWindow + circBuf->windowSize)
    {

        bytesToCopy = circBuf->writePtr - lastWindow;

        GST_LOG("shifting %lu bytes of data from %lu to 0\n", bytesToCopy,
            (UInt32)(lastWindow - firstWindow));

        if (bytesToCopy > 0) {
            memcpy(firstWindow, lastWindow, bytesToCopy);
        }

        GST_LOG("resetting write pointer (%lu->%lu)\n",
            (UInt32)(circBuf->writePtr - firstWindow),
            (UInt32)(circBuf->writePtr - (lastWindow - firstWindow) -
                     firstWindow));
        circBuf->writePtr       -= (lastWindow - firstWindow);
        circBuf->contiguousData  = FALSE;
        writePtrReset            = TRUE;

        /* The queue function will not unblock the consumer until there is
         * at least windowSize + readAhead available, but if the read pointer
         * is toward the end of the buffer, we may never get more than just
         * windowSize available and will deadlock.  To avoid this situation,
         * wake the consumer after shifting data so it has an opportunity to
         * process the last window in the buffer and reset itself to the
         * beginning of the buffer.
         */
        gst_ticircbuffer_broadcast_producer(circBuf);
    }
    return writePtrReset;
}
Exemplo n.º 2
0
Arquivo: appMain.c Projeto: sv99/DVSDK
/******************************************************************************
 * readFrame420P
 ******************************************************************************/
Int readFrame420P(Buffer_Handle hBuf, FILE *outFile, Int imageHeight)
{
    Int8 *yPtr = Buffer_getUserPtr(hBuf);
    Int8 *cbcrPtr;
    Int y, x;

    BufferGfx_Dimensions dim;

    BufferGfx_getDimensions(hBuf, &dim);

    /* Write Y plane */
    for (y = 0; y < imageHeight; y++) {
        if (fread(yPtr, dim.width, 1, outFile) != 1) {
            fprintf(stderr,"Failed to read data from file\n");
            return -1;
        }

        yPtr += dim.lineLength;
    }

    /* Join Cb to CbCr interleaved */
    cbcrPtr = Buffer_getUserPtr(hBuf) + Buffer_getSize(hBuf) * 2 / 3;
    for (y = 0; y < imageHeight / 2; y++) {
        for (x = 0; x < dim.width; x += 2) {
            if (fread(&cbcrPtr[x], 1, 1, outFile) != 1) {
                fprintf(stderr,"Failed to read data from file\n");
                return -1;
            }
        }
        cbcrPtr += dim.lineLength;
    }

    /* Join Cr to CbCr interleaved */
    cbcrPtr = Buffer_getUserPtr(hBuf) + Buffer_getSize(hBuf) * 2 / 3;
    for (y = 0; y < imageHeight / 2; y++) {
        for (x = 1; x < dim.width; x += 2) {
            if (fread(&cbcrPtr[x], 1, 1, outFile) != 1) {
                fprintf(stderr,"Failed to read data from file\n");
                return -1;
            }
        }
        cbcrPtr += dim.lineLength;
    }

    printf("Read 420P frame size %d (%dx%d) from file\n",
           (Int) (dim.width * 3 / 2 * imageHeight),
           (Int) dim.width, (Int) imageHeight);

    Buffer_setNumBytesUsed(hBuf, Buffer_getSize(hBuf));

    return 0;
}
Exemplo n.º 3
0
/******************************************************************************
 * Resize_execute
 ******************************************************************************/
Int Resize_execute(Resize_Handle hResize,
                   Buffer_Handle hSrcBuf, Buffer_Handle hDstBuf)
{
#ifdef CONFIG_DM365_IPIPE
    struct imp_convert  rsz;
    BufferGfx_Dimensions srcDim;
    BufferGfx_Dimensions dstDim;
    UInt32               srcOffset;
    UInt32               dstOffset;
    
    assert(hResize);
    assert(hSrcBuf);
    assert(hDstBuf);

    Dmai_clear(rsz);

    BufferGfx_getDimensions(hSrcBuf, &srcDim);
    BufferGfx_getDimensions(hDstBuf, &dstDim);

    srcOffset = srcDim.y * srcDim.lineLength + (srcDim.x << 1);
    dstOffset = dstDim.y * dstDim.lineLength + (dstDim.x << 1);

    rsz.in_buff.index     = -1;
    rsz.in_buff.buf_type  = IMP_BUF_IN;
    rsz.in_buff.offset    = (UInt32) Buffer_getUserPtr(hSrcBuf) + srcOffset;
    rsz.in_buff.size      = Buffer_getSize(hSrcBuf);

    rsz.out_buff1.index    = -1;
    rsz.out_buff1.buf_type = IMP_BUF_OUT1;
    rsz.out_buff1.offset   = (UInt32) Buffer_getUserPtr(hDstBuf) + dstOffset;
    rsz.out_buff1.size     = Buffer_getSize(hDstBuf);
    
    /* 
     * The IPIPE requires that the memory offsets of the input and output
     * buffers start on 32-byte boundaries.
     */
    assert((rsz.in_buff.offset  & 0x1F) == 0);
    assert((rsz.out_buff1.offset & 0x1F) == 0);

    /* Start IPIPE operation */
    if (ioctl(hResize->fd, RSZ_RESIZE, &rsz) == -1) {
        Dmai_err0("Failed RSZ_RESIZE\n");
        return Dmai_EFAIL;
    }

    Buffer_setNumBytesUsed(hDstBuf, Buffer_getNumBytesUsed(hSrcBuf));
    return Dmai_EOK;
#else
    Dmai_err0("not implemented\n");
    return Dmai_ENOTIMPL;
#endif
}
Exemplo n.º 4
0
/*****************************************************************************
 * gst_tiprepencbuf_prepare_output_buffer
 *    Function is used to allocate output buffer
 *****************************************************************************/
static GstFlowReturn
gst_tiprepencbuf_prepare_output_buffer(GstBaseTransform * trans,
    GstBuffer * inBuf, gint size, GstCaps * caps, GstBuffer ** outBuf)
{
    GstTIPrepEncBuf *prepencbuf = GST_TIPREPENCBUF(trans);
    Buffer_Handle    hOutBuf;

    GST_LOG("begin prepare output buffer\n");

    /* Get free buffer from buftab */
    if (!(hOutBuf = gst_tidmaibuftab_get_buf(prepencbuf->hOutBufTab))) {
        GST_ELEMENT_ERROR(prepencbuf, RESOURCE, READ,
            ("failed to get free buffer\n"), (NULL));
        return GST_FLOW_ERROR;
    }

    /* Create a DMAI transport buffer object to carry a DMAI buffer to
     * the source pad.  The transport buffer knows how to release the
     * buffer for re-use in this element when the source pad calls
     * gst_buffer_unref().
     */
    GST_LOG("creating dmai transport buffer\n");
    *outBuf = gst_tidmaibuffertransport_new(hOutBuf, prepencbuf->hOutBufTab, NULL, NULL);
    gst_buffer_set_data(*outBuf, (guint8 *) Buffer_getUserPtr(hOutBuf),
        Buffer_getSize(hOutBuf));
    gst_buffer_set_caps(*outBuf, GST_PAD_CAPS(trans->srcpad));

    GST_LOG("end prepare output buffer\n");

    return GST_FLOW_OK;
}
Exemplo n.º 5
0
/******************************************************************************
 * Sound_alsa_read
 ******************************************************************************/
Int Sound_alsa_read(Sound_Handle hSound, Buffer_Handle hBuf)
{
    Int32 numSamples, readSamples;
    Int8 *bufPtr;

    assert(hSound);
    assert(hBuf);

    readSamples = Buffer_getNumBytesUsed(hBuf) / (2 * hSound->channels);
    bufPtr = Buffer_getUserPtr(hBuf);

    while (readSamples > 0) {
        numSamples = snd_pcm_readi(hSound->rcIn, bufPtr, readSamples);

        if (numSamples == -EAGAIN) 
            continue;

        if (numSamples < 0) {
            if (xrunRecovery(hSound->rcIn,numSamples) < 0) {
                Dmai_err2 ("Failed to read from %s (%s)\n",
                            AUDIO_DEVICE, strerror(numSamples));
                return Dmai_EFAIL;
            }
        }
        else {
            bufPtr += numSamples * 2 * hSound->channels;
            readSamples -= numSamples;
        }
    }

    Buffer_setNumBytesUsed(hBuf, Buffer_getSize(hBuf));

    return Dmai_EOK;
}
Exemplo n.º 6
0
/******************************************************************************
 * gst_ticircbuffer_write_space
 *    Return the free space available in the buffer for CONTIGUOUS WRITING at
 *    the writePtr location.
 ******************************************************************************/
static Int32 gst_ticircbuffer_write_space(GstTICircBuffer *circBuf)
{
    if (circBuf->contiguousData) {
        return (Buffer_getUserPtr(circBuf->hBuf) +
                Buffer_getSize(circBuf->hBuf)) - circBuf->writePtr;
    }

    return circBuf->readPtr - circBuf->writePtr;
}
Exemplo n.º 7
0
/******************************************************************************
 * gst_ticircbuffer_data_size
 *    Return the total amount of data currently in the buffer (may not all be
 *    contiguous in memory).
 ******************************************************************************/
static Int32 gst_ticircbuffer_data_size(GstTICircBuffer *circBuf)
{
    if (gst_ticircbuffer_is_empty(circBuf)) {
        return 0;
    }

    return (circBuf->contiguousData) ?
        (circBuf->writePtr - circBuf->readPtr) :
        Buffer_getSize(circBuf->hBuf) - (circBuf->readPtr - circBuf->writePtr);
}
Exemplo n.º 8
0
void dmaiaccel_release_cb(gpointer data,
    GstTIDmaiBufferTransport *buf){
    Buffer_Handle hBuf = GST_TIDMAIBUFFERTRANSPORT_DMAIBUF(buf);
    GstBuffer *inBuf = (GstBuffer *)data;

    GST_DEBUG("Release callback for dmaiaccel allocated buffer");

    Memory_unregisterContigBuf((UInt32)Buffer_getUserPtr(hBuf),
        Buffer_getSize(hBuf));

    /* Now we can release our input buffer */
    gst_buffer_unref(inBuf);
}
Exemplo n.º 9
0
/******************************************************************************
 * gst_ticircbuffer_reset_read_pointer
 *    Reset the read pointer back to the beginning of the buffer.
 ******************************************************************************/
static Int32 gst_ticircbuffer_reset_read_pointer(GstTICircBuffer *circBuf)
{
    Int8  *circBufStart  = Buffer_getUserPtr(circBuf->hBuf);
    Int32  lastWinOffset = Buffer_getSize(circBuf->hBuf) -
                            (circBuf->windowSize + circBuf->readAheadSize);
    Int8  *lastWindow    = circBufStart + lastWinOffset;
    Int32  resetDelta    = lastWindow - circBufStart;

    /* In fixedBlockSize mode, just wait until the read poitner reaches the
     * end of the buffer and then reset it to the beginning.
     */
    if (circBuf->fixedBlockSize && !circBuf->contiguousData) {
        if (circBuf->readPtr == circBufStart + Buffer_getSize(circBuf->hBuf)) {
            GST_LOG("resetting read pointer (%lu->0)\n",
                (UInt32)(circBuf->readPtr - circBufStart));
            circBuf->readPtr        = Buffer_getUserPtr(circBuf->hBuf);
            circBuf->contiguousData = TRUE;
        }
        return 0;
    }

    /* Otherwise, reset it when the read pointer reaches the last window and
     * the last window has already been copied back to the first window.
     */
    if (!circBuf->contiguousData                           &&
         circBuf->readPtr              >  lastWindow       &&
         circBuf->readPtr - resetDelta <= circBuf->writePtr) {

        GST_LOG("resetting read pointer (%lu->%lu)\n",
            (UInt32)(circBuf->readPtr - circBufStart),
            (UInt32)(circBuf->readPtr - resetDelta - circBufStart));
        circBuf->readPtr        -= resetDelta;
        circBuf->contiguousData  = TRUE;
        return TRUE;
    }

    return FALSE;
}
Exemplo n.º 10
0
/******************************************************************************
 * cleanup
 ******************************************************************************/
static Int cleanup(Display_Handle hDisplay)
{
    Int                   ret     = Dmai_EOK;
    BufTab_Handle         hBufTab = hDisplay->hBufTab;
    enum v4l2_buf_type    type;
    Int                   bufIdx;
    Buffer_Handle         hDispBuf;

    if (hDisplay->fd != -1) {
        if (hDisplay->started) {
            /* Shut off the video display */
            type = V4L2_BUF_TYPE_VIDEO_OUTPUT;

            if (ioctl(hDisplay->fd, VIDIOC_STREAMOFF, &type) == -1) {
                Dmai_err1("VIDIOC_STREAMOFF failed (%s)\n", strerror(errno));
                ret = Dmai_EFAIL;
            }
        }

        if (close(hDisplay->fd) == -1) {
            Dmai_err1("Failed to close capture device (%s)\n", strerror(errno));
            ret = Dmai_EIO;
        }

        if (hDisplay->userAlloc == FALSE) {
            if (hBufTab) {
                for (bufIdx = 0;
                     bufIdx < BufTab_getNumBufs(hBufTab);
                     bufIdx++) {

                    hDispBuf = BufTab_getBuf(hBufTab, bufIdx);

                    if (munmap(Buffer_getUserPtr(hDispBuf),
                               Buffer_getSize(hDispBuf)) == -1) {
                        Dmai_err1("Failed to unmap capture buffer%d\n", bufIdx);
                        ret = Dmai_EFAIL;
                    }
                }
            }
        }
    }

    if (hDisplay->bufDescs) {
        free(hDisplay->bufDescs);
    }

    free(hDisplay);

    return ret;
}
Exemplo n.º 11
0
/******************************************************************************
 * cleanup
 ******************************************************************************/
static Int cleanup(Capture_Handle hCapture)
{
    BufTab_Handle         hBufTab    = hCapture->hBufTab;
    Int                   ret        = Dmai_EOK;
    Int8                 *capBufPtr;
    enum v4l2_buf_type    type;
    Uns                   bufIdx;
    Buffer_Handle         hCapBuf;

    if (hCapture->fd != -1) {
        if (hCapture->started) {
            /* Shut off the video capture */
            type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
            if (ioctl(hCapture->fd, VIDIOC_STREAMOFF, &type) == -1) {
                Dmai_err1("VIDIOC_STREAMOFF failed (%s)\n", strerror(errno));
                ret = Dmai_EFAIL;
            }
        }

        if (close(hCapture->fd) == -1) {
            Dmai_err1("Failed to close capture device (%s)\n", strerror(errno));
            ret = Dmai_EIO;
        }

        if (hCapture->userAlloc == FALSE && hBufTab) {
            for (bufIdx = 0;
                 bufIdx < BufTab_getNumBufs(hBufTab);
                 bufIdx++) {

                hCapBuf = BufTab_getBuf(hBufTab, bufIdx);
                capBufPtr = Buffer_getUserPtr(hCapBuf);

                if (munmap(capBufPtr - hCapture->topOffset,
                           Buffer_getSize(hCapBuf)) == -1) {
                    Dmai_err1("Failed to unmap capture buffer%d\n", bufIdx);
                    ret = Dmai_EFAIL;
                }
            }
        }

        if (hCapture->bufDescs) {
            free(hCapture->bufDescs);
        }
    }

    free(hCapture);

    return ret;
}
Exemplo n.º 12
0
Arquivo: ui.c Projeto: ChrisX34/stuff
/******************************************************************************
 * setOsdTransparency
 ******************************************************************************/
static Int setOsdTransparency(UI_Handle hUI, Char trans)
{
    Buffer_Handle hBuf;

    if (Display_get(hUI->hAttr, &hBuf) < 0) {
        ERR("Failed to get attribute window buffer\n");
        return FAILURE;
    }

    memset(Buffer_getUserPtr(hBuf), trans, Buffer_getSize(hBuf));

    if (Display_put(hUI->hAttr, hBuf) < 0) {
        ERR("Failed to put display buffer\n");
        return FAILURE;
    }

    return SUCCESS;
}
Exemplo n.º 13
0
/******************************************************************************
 * Sound_alsa_write
 ******************************************************************************/
Int Sound_alsa_write(Sound_Handle hSound, Buffer_Handle hBuf)
{
    Int32 numSamples, writeSamples;
    Int8 *bufPtr;

    assert(hSound);
    assert(hBuf);

    writeSamples = Buffer_getNumBytesUsed(hBuf) / (2 * hSound->channels);
    bufPtr = Buffer_getUserPtr(hBuf);

    while (writeSamples > 0) {
        /* start by doing a blocking wait for free space. */
        snd_pcm_wait (hSound->rcOut, PCM_TIMEOUT);
        numSamples = snd_pcm_writei(hSound->rcOut, bufPtr, writeSamples);

        if (numSamples == -EAGAIN) 
            continue;

        if (numSamples < 0) {
            if (xrunRecovery(hSound->rcOut,numSamples) < 0) {
                Dmai_err2 ("Failed to write to %s (%s)\n",
                            AUDIO_DEVICE, strerror(-numSamples));
                return Dmai_EFAIL;
            }
        }
        else {
            bufPtr += numSamples * 2 * hSound->channels;
            writeSamples -= numSamples;
        }
    }

    Buffer_setNumBytesUsed(hBuf, Buffer_getSize(hBuf));

    return Dmai_EOK;
}
Exemplo n.º 14
0
/******************************************************************************
 * gst_ticircbuffer_data_available
 *    Return how much contiguous data is available at the read pointer.
 ******************************************************************************/
static Int32 gst_ticircbuffer_data_available(GstTICircBuffer *circBuf)
{
    /* First check if the buffer is empty, in which case return 0. */
    if (gst_ticircbuffer_is_empty(circBuf)) {
        return 0;
    }

    /* If the write pointer is now ahead of the read pointer, make sure there
     * is a window available.
     */
    if (circBuf->contiguousData) {
        return (circBuf->writePtr - circBuf->readPtr);
    }

    /* Otherwise, there needs to be enough data between the read pointer and
     * the end of the buffer.
     */
    else {
       return (Buffer_getUserPtr(circBuf->hBuf) +
               Buffer_getSize(circBuf->hBuf)) - circBuf->readPtr;
    }

    return 0;
}
Exemplo n.º 15
0
Void CapBuf_blackFill(Buffer_Handle hBuf)
{
    switch (BufferGfx_getColorSpace(hBuf)) {
        case ColorSpace_YUV422PSEMI:
        {
            Int8  *yPtr     = Buffer_getUserPtr(hBuf);
            Int32  ySize    = Buffer_getSize(hBuf) / 2;
            Int8  *cbcrPtr  = yPtr + ySize;
            Int    bpp = ColorSpace_getBpp(ColorSpace_YUV422PSEMI);
            Int    y;
            BufferGfx_Dimensions dim;
            UInt32 offset;

            BufferGfx_getDimensions(hBuf, &dim);
            offset = dim.y * dim.lineLength + dim.x * bpp / 8;
            for (y = 0; y < dim.height; y++) {
                memset(yPtr + offset, 0x0, dim.width * bpp / 8);
                yPtr += dim.lineLength;
            }

            for (y = 0; y < dim.height; y++) {
                memset(cbcrPtr + offset, 0x80, dim.width * bpp / 8);
                cbcrPtr += dim.lineLength;
            }

            break;
        }

        case ColorSpace_YUV420PSEMI:
        {
            Int8  *yPtr     = Buffer_getUserPtr(hBuf);
            Int32  ySize    = Buffer_getSize(hBuf) * 2 / 3;
            Int8  *cbcrPtr  = yPtr + ySize;
            Int    bpp = ColorSpace_getBpp(ColorSpace_YUV420PSEMI);
            Int    y;
            BufferGfx_Dimensions dim;

            BufferGfx_getDimensions(hBuf, &dim);
            yPtr += dim.y * dim.lineLength + dim.x * bpp / 8;
            for (y = 0; y < dim.height; y++) {
                memset(yPtr, 0x0, dim.width * bpp / 8);
                yPtr += dim.lineLength;
            }

            cbcrPtr += dim.y * dim.lineLength / 2 + dim.x * bpp / 8;
            for (y = 0; y < dim.height / 2; y++) {
                memset(cbcrPtr, 0x80, dim.width * bpp / 8);
                cbcrPtr += dim.lineLength;
            }
            
            break;
        }

        case ColorSpace_UYVY:
        {
            Int32 *bufPtr = (Int32*)Buffer_getUserPtr(hBuf);
            Int    bpp    = ColorSpace_getBpp(ColorSpace_UYVY);
            Int    i, j;
            BufferGfx_Dimensions dim;

            BufferGfx_getDimensions(hBuf, &dim);
            bufPtr += (dim.y * dim.lineLength + dim.x * bpp / 8) / sizeof(Int32);

            /* Make sure display buffer is 4-byte aligned */
            assert((((UInt32) bufPtr) & 0x3) == 0);

            for (i = 0; i < dim.height; i++) {
                for (j = 0; j < dim.width / 2; j++) {
                    bufPtr[j] = UYVY_BLACK;
                }
                bufPtr += dim.lineLength / sizeof(Int32);
            }

            break;
        }

        case ColorSpace_RGB565:
        {
            memset(Buffer_getUserPtr(hBuf), 0, Buffer_getSize(hBuf));
            break;
        }

        default:
        {
                ERR("Unsupported color space (%d) for _Dmai_blackFill\n",
                BufferGfx_getColorSpace(hBuf));
            break;
        }
    }
}
Exemplo n.º 16
0
/******************************************************************************
 * Framecopy_accel_execute
 ******************************************************************************/
Int Framecopy_accel_execute(Framecopy_Handle hFc,
                             Buffer_Handle hSrcBuf, Buffer_Handle hDstBuf)
{
    vdce_address_start_t params;
    BufferGfx_Dimensions srcDim;
    BufferGfx_Dimensions dstDim;
    UInt32 srcOffset, dstOffset;

    assert(hFc);
    assert(hSrcBuf);
    assert(hDstBuf);

    /* VDCE addresses must be a multiple of 8 */
    assert((((UInt32) Buffer_getUserPtr(hDstBuf)) & 0x7) == 0);
    assert((((UInt32) Buffer_getUserPtr(hSrcBuf)) & 0x7) == 0);

    /* Buffer sizes / 4 must be a multiple of 8 */
    assert(((Buffer_getSize(hSrcBuf) / 4) & 0x7) == 0);
    assert(((Buffer_getSize(hDstBuf) / 4) & 0x7) == 0);

    BufferGfx_getDimensions(hSrcBuf, &srcDim);
    BufferGfx_getDimensions(hDstBuf, &dstDim);

    assert(srcDim.x >= 0);
    assert(srcDim.y >= 0);
    assert(dstDim.x >= 0);
    assert(dstDim.y >= 0);
    assert((srcDim.x & 0x7) == 0);
    assert((dstDim.x & 0x7) == 0);

    srcOffset = srcDim.y * srcDim.lineLength + srcDim.x;
    dstOffset = dstDim.y * dstDim.lineLength + dstDim.x;

    params.buffers[VDCE_BUF_IN].index     = -1;
    params.buffers[VDCE_BUF_IN].buf_type  = VDCE_BUF_IN;
    params.buffers[VDCE_BUF_IN].virt_ptr  = ((Int32) Buffer_getUserPtr(hSrcBuf)+
                                                     srcOffset);
    params.buffers[VDCE_BUF_IN].size      = Buffer_getSize(hSrcBuf);

    params.buffers[VDCE_BUF_OUT].index    = -1;
    params.buffers[VDCE_BUF_OUT].buf_type = VDCE_BUF_OUT;
    params.buffers[VDCE_BUF_OUT].virt_ptr = ((Int32) Buffer_getUserPtr(hDstBuf)+
                                                     dstOffset);
    params.buffers[VDCE_BUF_OUT].size     = Buffer_getSize(hDstBuf);

    params.src_horz_pitch = srcDim.lineLength;
    params.res_horz_pitch = dstDim.lineLength;

    if (ioctl(hFc->fd, VDCE_START, &params) < 0) {
        Dmai_err0("Failed VDCE_START\n");
        return Dmai_EFAIL;
    }

    if (BufferGfx_getColorSpace(hDstBuf) == ColorSpace_YUV420PSEMI) {
        Buffer_setNumBytesUsed(hDstBuf, dstDim.width * dstDim.height * 3 / 2);
    }
    else {
        Buffer_setNumBytesUsed(hDstBuf, dstDim.width * dstDim.height * 2);
    }

    return Dmai_EOK;
}
Exemplo n.º 17
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;
}
Exemplo n.º 18
0
/******************************************************************************
 * _Dmai_blackFill
 ******************************************************************************/
Void _Dmai_blackFill(Buffer_Handle hBuf)
{
    switch (BufferGfx_getColorSpace(hBuf)) {
        case ColorSpace_YUV422PSEMI:
        {
            Int8  *yPtr     = Buffer_getUserPtr(hBuf);
            Int32  ySize    = Buffer_getSize(hBuf) / 2;
            Int8  *cbcrPtr  = yPtr + ySize;
            Int32  cbCrSize = Buffer_getSize(hBuf) - ySize;
            Int    i;

            /* Fill the Y plane */
            for (i = 0; i < ySize; i++) {
                yPtr[i] = 0x0;
            }

            for (i = 0; i < cbCrSize; i++) {
                cbcrPtr[i] = 0x80;
            }
            break;
        }

        case ColorSpace_YUV420PSEMI:
        {
            Int8  *bufPtr = Buffer_getUserPtr(hBuf);
            Int    y;
            Int    bpp = ColorSpace_getBpp(ColorSpace_YUV420PSEMI);
            BufferGfx_Dimensions dim;
            
            BufferGfx_getDimensions(hBuf, &dim);

            for (y = 0; y < dim.height; y++) {
                memset(bufPtr, 0x0, dim.width * bpp / 8);
                bufPtr += dim.lineLength;
            }

            for (y = 0; y < (dim.height / 2); y++) {
                memset(bufPtr, 0x80, dim.width * bpp / 8);
                bufPtr += dim.lineLength;
            }
            
            break;
        }

        case ColorSpace_UYVY:
        {
            Int32 *bufPtr  = (Int32*)Buffer_getUserPtr(hBuf);
            Int32  bufSize = Buffer_getSize(hBuf) / sizeof(Int32);
            Int    i;

            /* Make sure display buffer is 4-byte aligned */
            assert((((UInt32) bufPtr) & 0x3) == 0);

            for (i = 0; i < bufSize; i++) {
                bufPtr[i] = UYVY_BLACK;
            }
            break;
        }

        case ColorSpace_RGB565:
        {
            memset(Buffer_getUserPtr(hBuf), 0, Buffer_getSize(hBuf));
            break;
        }

        default:
            Dmai_err0("Unsupported color space for _Dmai_blackFill\n");
            break;
    }
}
Exemplo n.º 19
0
/******************************************************************************
 * _Dmai_v4l2UserAlloc
 ******************************************************************************/
Int _Dmai_v4l2UserAlloc(Int fd, Int numBufs, enum v4l2_buf_type type,
                        struct _VideoBufDesc **bufDescsPtr,
                        BufTab_Handle hBufTab, Int topOffset, 
                        ColorSpace_Type colorSpace, Int queueBuffers)
{
    struct v4l2_requestbuffers  req;
    struct v4l2_format          fmt;
    _VideoBufDesc              *bufDesc;
    Buffer_Handle               hBuf;
    Int                         bufIdx;

    Dmai_clear(fmt);
    fmt.type = type;
    if (ioctl(fd, VIDIOC_G_FMT, &fmt) == -1) {
        Dmai_err1("VIDIOC_G_FMT failed (%s)\n", strerror(errno));
        return Dmai_EFAIL;
    }

    if (Buffer_getSize(BufTab_getBuf(hBufTab, 0)) <
        fmt.fmt.pix.width * fmt.fmt.pix.height * 2) {
        Dmai_err0("Supplied buffers not large enough for video standard\n");
        return Dmai_EINVAL;
    }
    /* Set the actual size of the buffers allocated */
    fmt.type                 = type;
    fmt.fmt.pix.sizeimage    = Buffer_getSize(BufTab_getBuf(hBufTab, 0));
    fmt.fmt.pix.pixelformat  = V4L2_PIX_FMT_UYVY;
    fmt.fmt.pix.bytesperline = fmt.fmt.pix.width;

    if (ioctl(fd, VIDIOC_S_FMT, &fmt) == -1) {
        Dmai_err1("VIDIOC_S_FMT failed (%s)\n", strerror(errno));
        return Dmai_EIO;
    }
    /*
     * Tell the driver that we will use user allocated buffers, but don't
     * allocate any buffers in the driver (just the internal descriptors).
     */
    Dmai_clear(req);
    req.type   = type;
    req.count  = numBufs; 
    req.memory = V4L2_MEMORY_USERPTR;

    if (ioctl(fd, VIDIOC_REQBUFS, &req) == -1) {
        Dmai_err0("Could not allocate video display buffers\n");
        return Dmai_ENOMEM;
    }

    /* The driver may return less buffers than requested */
    if (req.count < numBufs || !req.count) {
        Dmai_err0("Insufficient device driver buffer memory\n");
        return Dmai_ENOMEM;
    }

    /* Allocate space for buffer descriptors */
    *bufDescsPtr = calloc(numBufs, sizeof(_VideoBufDesc));

    if (*bufDescsPtr == NULL) {
        Dmai_err0("Failed to allocate space for buffer descriptors\n");
        return Dmai_ENOMEM;
    }

    for (bufIdx = 0; bufIdx < numBufs; bufIdx++) {
        bufDesc = &(*bufDescsPtr)[bufIdx];
        hBuf = BufTab_getBuf(hBufTab, bufIdx);

        if (hBuf == NULL) {
            Dmai_err0("Failed to get buffer from BufTab for display\n");
            return Dmai_ENOMEM;
        }

        if (Buffer_getType(hBuf) != Buffer_Type_GRAPHICS) {
            Dmai_err0("Buffer supplied to Display not a Graphics buffer\n");
            return Dmai_EINVAL;
        }

        Dmai_clear(bufDesc->v4l2buf);
        bufDesc->v4l2buf.index     = bufIdx;
        bufDesc->v4l2buf.type      = type;
        bufDesc->v4l2buf.memory    = V4L2_MEMORY_USERPTR;
        bufDesc->v4l2buf.m.userptr = (UInt32) Buffer_getUserPtr(hBuf);
        bufDesc->v4l2buf.length    = (fmt.fmt.pix.sizeimage + 4096) & (~0xFFF);
        bufDesc->hBuf              = hBuf;
        bufDesc->used              = (queueBuffers == FALSE) ? TRUE : FALSE;

        /* If queueBuffers is TRUE, initialize the buffers to black and queue
         * them into the driver.
         */
        if (queueBuffers == TRUE) {
            Buffer_setNumBytesUsed(hBuf, Buffer_getSize(hBuf));
            _Dmai_blackFill(hBuf);

            /* Queue buffer in device driver */
            if (ioctl(fd, VIDIOC_QBUF, &bufDesc->v4l2buf) == -1) {
                Dmai_err1("VIODIC_QBUF failed (%s)\n", strerror(errno));
                return Dmai_EFAIL;
            }
        }
    }

    return Dmai_EOK;
}
Exemplo n.º 20
0
/******************************************************************************
 * writeFrameSemiPlanar
******************************************************************************/
Int writeFrameSemiPlanar(Buffer_Handle hBuf, FILE *outFile)
{
    Int8   *yPtr, *cbcrPtr;        
    UInt32  ySize;                /* Size of Luma data */
    Uint32  cbcrSize;             /* Size of Chroma (cb and cr) data */
    Char   *colorFmt;
    Int     y, x;

    BufferGfx_Dimensions dim;
    BufferGfx_getDimensions(hBuf, &dim);    

    /*  The three color planes are evenly spaced in the output buffer. Each 
     *  plane has been allocated a size equal to the input creation parameter 
     *  maxWidth x maxHeight.              
     */         
    ySize = dim.width * dim.height;
    
    switch(BufferGfx_getColorSpace(hBuf)){
       case ColorSpace_YUV420PSEMI:
           cbcrSize = dim.width * dim.height / 2;
           colorFmt = "420P";
           break;
       default:
           printf("Unsupported output color space %d.\n", 
            BufferGfx_getColorSpace(hBuf));
           return -1;        
    }    

    /* Write Y plane */
    yPtr = Buffer_getUserPtr(hBuf);
    if (fwrite(yPtr, 1, ySize, outFile) != ySize ) {
        fprintf(stderr,"Failed to write y data to disk\n");
        return -1;
    }

    /* Separate Cb from CbCr interleaved and save Cb plane */
    cbcrPtr = Buffer_getUserPtr(hBuf) + Buffer_getSize(hBuf) * 2/3;
    for (y = 0; y < dim.height / 2; y++) {
      for (x = 0; x < dim.width; x += 2) {
        if (fwrite(&cbcrPtr[x], 1, 1, outFile) != 1) {
            fprintf(stderr,"Failed to write data to disk\n");
            return -1;
        }
      }
      cbcrPtr += dim.lineLength;
    }

    /* Separate Cr from CbCr interleaved and save Cr plane */
    cbcrPtr = Buffer_getUserPtr(hBuf) + Buffer_getSize(hBuf) * 2 / 3;
    for (y = 0; y < dim.height / 2; y++) {
      for (x = 1; x < dim.width; x += 2) {
        if (fwrite(&cbcrPtr[x], 1, 1, outFile) != 1) {
            fprintf(stderr,"Failed to write data to disk\n");
            return -1;
        }
      }
      cbcrPtr += dim.lineLength;
    }

    printf("Wrote %s image size %d (%dx%d) to disk\n",
           colorFmt, 
           (Int) (ySize + cbcrSize),
           (Int) dim.width, (Int) dim.height);

    return 0;
}
Exemplo n.º 21
0
/******************************************************************************
 * Resize_execute
 ******************************************************************************/
Int Resize_execute(Resize_Handle hResize,
                   Buffer_Handle hSrcBuf, Buffer_Handle hDstBuf, Buffer_Handle hsDstBuf)
{
    struct imp_convert  rsz;
	struct rsz_channel_config rsz_chan_config;
	struct rsz_single_shot_config rsz_ss_config;
    BufferGfx_Dimensions srcDim;
    BufferGfx_Dimensions dstDim;
    BufferGfx_Dimensions sdstDim;
    UInt32               srcOffset;
    UInt32               dstOffset;
    UInt32               sdstOffset;
    UInt32               srcSize;
    UInt32               dstSize;
    
    assert(hResize);
    assert(hSrcBuf);
    assert(hDstBuf);

    Dmai_clear(rsz);

    BufferGfx_getDimensions(hSrcBuf, &srcDim);
    BufferGfx_getDimensions(hDstBuf, &dstDim);
    hsDstBuf?BufferGfx_getDimensions(hsDstBuf, &sdstDim):NULL;
    srcSize = srcDim.width * srcDim.height;
    dstSize = dstDim.width * dstDim.height;

    /* the resize operation to ColorSpace_UYVY is different from ColorSpace_YUV420PSEMI*/
    if (BufferGfx_getColorSpace(hSrcBuf) == ColorSpace_UYVY) {
        srcOffset = srcDim.y * srcDim.lineLength + (srcDim.x << 1);
        dstOffset = dstDim.y * dstDim.lineLength + (dstDim.x << 1);
        sdstOffset = hsDstBuf?(sdstDim.y * sdstDim.lineLength + (sdstDim.x << 1)):0;

        rsz.in_buff.index     = -1;
        rsz.in_buff.buf_type  = IMP_BUF_IN;
        rsz.in_buff.offset    = Buffer_getPhysicalPtr(hSrcBuf) + srcOffset;
        rsz.in_buff.size      = Buffer_getSize(hSrcBuf);

        rsz.out_buff1.index    = -1;
        rsz.out_buff1.buf_type = IMP_BUF_OUT1;
        rsz.out_buff1.offset   = Buffer_getPhysicalPtr(hDstBuf) + dstOffset;
        rsz.out_buff1.size     = Buffer_getSize(hDstBuf);
        
        rsz.out_buff2.index    = -1;
        rsz.out_buff2.buf_type = IMP_BUF_OUT2;
        rsz.out_buff2.offset   = hsDstBuf?(Buffer_getPhysicalPtr(hsDstBuf) + sdstOffset):0;
        rsz.out_buff2.size     = hsDstBuf?Buffer_getSize(hsDstBuf):0;
        
        /* 
         * The IPIPE requires that the memory offsets of the input and output
         * buffers start on 32-byte boundaries.
         */
        assert((rsz.in_buff.offset  & 0x1F) == 0);
        assert((rsz.out_buff1.offset & 0x1F) == 0);
        assert((rsz.out_buff2.offset & 0x1F) == 0);

        /* Start IPIPE operation */
        if (ioctl(hResize->fd, RSZ_RESIZE, &rsz) == -1) {
            Dmai_err0("Failed RSZ_RESIZE\n");
            return Dmai_EFAIL;
        }

        Buffer_setNumBytesUsed(hDstBuf, Buffer_getSize(hDstBuf));
        hsDstBuf?Buffer_setNumBytesUsed(hsDstBuf, Buffer_getSize(hsDstBuf)):NULL;
    } else {
        /* configure for the ColorSpace_YUV420PSEMI*/
	    Dmai_clear(rsz_ss_config);
	    rsz_chan_config.oper_mode = IMP_MODE_SINGLE_SHOT;
	    rsz_chan_config.chain = 0;
	    rsz_chan_config.len = 0;
	    rsz_chan_config.config = NULL; /* to set defaults in driver */
	    if (ioctl(hResize->fd, RSZ_S_CONFIG, &rsz_chan_config) < 0) {
	    	Dmai_err0("Error in setting default configuration for single shot mode\n");
            return Dmai_EFAIL;
	    }

	    /* default configuration setting in Resizer successfull */
	    Dmai_clear(rsz_ss_config);
	    rsz_chan_config.oper_mode = IMP_MODE_SINGLE_SHOT;
	    rsz_chan_config.chain = 0;
	    rsz_chan_config.len = sizeof(struct rsz_single_shot_config);
	    rsz_chan_config.config = &rsz_ss_config;

	    if (ioctl(hResize->fd, RSZ_G_CONFIG, &rsz_chan_config) < 0) {
	    	Dmai_err0("Error in getting configuration from driver\n");
            return Dmai_EFAIL;
	    }

	    /* input params are set at the resizer */
	    rsz_ss_config.input.image_width  = srcDim.width;
	    rsz_ss_config.input.image_height = srcDim.height;
	    rsz_ss_config.input.ppln = rsz_ss_config.input.image_width + 8;
	    rsz_ss_config.input.lpfr = rsz_ss_config.input.image_height + 10;
	    rsz_ss_config.input.pix_fmt = IPIPE_420SP_Y;
	    rsz_ss_config.output1.pix_fmt = pixFormatConversion(BufferGfx_getColorSpace(hDstBuf));
        rsz_ss_config.output1.enable = 1;
	    rsz_ss_config.output1.width = dstDim.width;
	    rsz_ss_config.output1.height = dstDim.height;
	    rsz_ss_config.output2.enable = 0;

	    rsz_chan_config.oper_mode = IMP_MODE_SINGLE_SHOT;
	    rsz_chan_config.chain = 0;
	    rsz_chan_config.len = sizeof(struct rsz_single_shot_config);
	    if (ioctl(hResize->fd, RSZ_S_CONFIG, &rsz_chan_config) < 0) {
	    	Dmai_err0("Error in setting default configuration for single shot mode\n");
            return Dmai_EFAIL;
	    }
	    rsz_chan_config.oper_mode = IMP_MODE_SINGLE_SHOT;
	    rsz_chan_config.chain = 0;
	    rsz_chan_config.len = sizeof(struct rsz_single_shot_config);

	    /* read again and verify */
	    if (ioctl(hResize->fd, RSZ_G_CONFIG, &rsz_chan_config) < 0) {
	    	Dmai_err0("Error in getting configuration from driver\n");
            return Dmai_EFAIL;
	    }

        /* execute for ColorSpace_YUV420PSEMI*/
        Dmai_clear(rsz);
        //srcOffset = srcDim.y * srcDim.lineLength + (srcDim.x << 1);
        //dstOffset = dstDim.y * dstDim.lineLength + (dstDim.x << 1);
        rsz.in_buff.buf_type  = IMP_BUF_IN;
        rsz.in_buff.index     = -1;
        rsz.in_buff.offset    = Buffer_getPhysicalPtr(hSrcBuf);
        //rsz.in_buff.size      = Buffer_getSize(hSrcBuf);
        rsz.in_buff.size      = srcSize;

        rsz.out_buff1.buf_type = IMP_BUF_OUT1;
        rsz.out_buff1.index    = -1;
        rsz.out_buff1.offset    = Buffer_getPhysicalPtr(hDstBuf);
        //rsz.out_buff1.size     = Buffer_getSize(hDstBuf);
        rsz.out_buff1.size     = dstSize;
        
        /* 
         * The IPIPE requires that the memory offsets of the input and output
         * buffers start on 32-byte boundaries.
         */
        assert((rsz.in_buff.offset  & 0x1F) == 0);
        assert((rsz.out_buff1.offset & 0x1F) == 0);
        /* Start IPIPE operation */
        if (ioctl(hResize->fd, RSZ_RESIZE, &rsz) == -1) {
            Dmai_err0("Failed RSZ_RESIZE\n");
            return Dmai_EFAIL;
        }

	    /* input params are set at the resizer */
	    rsz_ss_config.input.image_width  = srcDim.width;
	    rsz_ss_config.input.image_height = srcDim.height>>1;
	    rsz_ss_config.input.ppln = rsz_ss_config.input.image_width + 8;
	    rsz_ss_config.input.lpfr = rsz_ss_config.input.image_height + 10;
	    rsz_ss_config.input.pix_fmt = IPIPE_420SP_C;
        rsz_ss_config.output1.pix_fmt = pixFormatConversion(BufferGfx_getColorSpace(hDstBuf));
	    rsz_ss_config.output1.enable = 1;
	    rsz_ss_config.output1.width = dstDim.width;
	    rsz_ss_config.output1.height = dstDim.height;
        rsz_ss_config.output2.enable = 0;

	    rsz_chan_config.oper_mode = IMP_MODE_SINGLE_SHOT;
	    rsz_chan_config.chain = 0;
	    rsz_chan_config.len = sizeof(struct rsz_single_shot_config);
	    if (ioctl(hResize->fd, RSZ_S_CONFIG, &rsz_chan_config) < 0) {
	    	Dmai_err0("Error in setting default configuration for single shot mode\n");
            return Dmai_EFAIL;
	    }
	    rsz_chan_config.oper_mode = IMP_MODE_SINGLE_SHOT;
	    rsz_chan_config.chain = 0;
	    rsz_chan_config.len = sizeof(struct rsz_single_shot_config);

	    /* read again and verify */
	    if (ioctl(hResize->fd, RSZ_G_CONFIG, &rsz_chan_config) < 0) {
	    	Dmai_err0("Error in getting configuration from driver\n");
            return Dmai_EFAIL;
	    }

        Dmai_clear(rsz);
        //srcOffset = srcDim.y * srcDim.lineLength + (srcDim.x << 1);
        //dstOffset = dstDim.y * dstDim.lineLength + (dstDim.x << 1);
        rsz.in_buff.buf_type  = IMP_BUF_IN;
        rsz.in_buff.index     = -1;
        rsz.in_buff.offset    = Buffer_getPhysicalPtr(hSrcBuf)  + srcSize;
        rsz.in_buff.size      = srcSize>>1;

        rsz.out_buff1.buf_type = IMP_BUF_OUT1;
        rsz.out_buff1.index    = -1;
        rsz.out_buff1.offset   = Buffer_getPhysicalPtr(hDstBuf)  + dstSize;
        rsz.out_buff1.size     = dstSize>>1;
        
        /* 
         * The IPIPE requires that the memory offsets of the input and output
         * buffers start on 32-byte boundaries.
         */
        assert((rsz.in_buff.offset  & 0x1F) == 0);
        assert((rsz.out_buff1.offset & 0x1F) == 0);
        /* Start IPIPE operation */
        if (ioctl(hResize->fd, RSZ_RESIZE, &rsz) == -1) {
            Dmai_err0("Failed RSZ_RESIZE\n");
            return Dmai_EFAIL;
        }

        Buffer_setNumBytesUsed(hDstBuf, Buffer_getNumBytesUsed(hSrcBuf));

    }
    return Dmai_EOK;
}
Exemplo n.º 22
0
static GstFlowReturn
create(GstPushSrc *base, GstBuffer **buf)
{
    GstTICaptureSrc *src = (GstTICaptureSrc *)base;
    Buffer_Handle       hDstBuf;
    GstBuffer   *outBuf;
    gint    ret = GST_FLOW_OK;
    BufferGfx_Attrs  gfxAttrs = BufferGfx_Attrs_DEFAULT;
    Int32   width, height;

    GST_LOG("create begin");

    /* create capture device */
    if (src->hCapture == NULL) {

        /* set framerate based on video standard */
        switch(dmai_video_std(src->video_standard)) {
            case VideoStd_D1_NTSC:
                    gst_value_set_fraction(&src->framerate,30000,1001);
                break;
            case VideoStd_D1_PAL:
                    gst_value_set_fraction(&src->framerate,25,1);
                break;
            default:
                    gst_value_set_fraction(&src->framerate,30,1);
                break;
        }

        /* set width & height based on video standard */

        src->cAttrs.videoStd = dmai_video_std(src->video_standard);

        VideoStd_getResolution(src->cAttrs.videoStd, &width, &height);
				width = 720;
				height = 576;
				GST_WARNING("force video size to %dx%d", src->width, src->height);

        src->width = width;
        src->height = height;
        
        gfxAttrs.dim.height = src->height;
        gfxAttrs.dim.width = src->width;
        src->cAttrs.captureDimension = &gfxAttrs.dim;

        if (!capture_create(src))
            return GST_FLOW_ERROR;
    }

    /* Get buffer from driver */
    if (Capture_get(src->hCapture, &hDstBuf)) {
        GST_ELEMENT_ERROR(src, RESOURCE, FAILED,
        ("Failed to allocate buffer\n"), (NULL));
        return GST_FLOW_ERROR;
    }

    /* Create a DMAI transport buffer object to carry a DMAI buffer to
     * the source pad.  The transport buffer knows how to release the
     * buffer for re-use in this element when the source pad calls
     * gst_buffer_unref().
     */
    outBuf = gst_tidmaibuffertransport_new(hDstBuf, src->hBufTab, capture_buffer_finalize, (void*)src);
    gst_buffer_set_data(outBuf, GST_BUFFER_DATA(outBuf), Buffer_getSize(hDstBuf));

    *buf = outBuf;

    /* set buffer metadata */
    if (G_LIKELY (ret == GST_FLOW_OK && *buf)) {
        GstClock *clock;
        GstClockTime timestamp;

        GST_BUFFER_OFFSET (*buf) = src->offset++;
        GST_BUFFER_OFFSET_END (*buf) = src->offset;

        /* timestamps, LOCK to get clock and base time. */
        GST_OBJECT_LOCK (src);
        if ((clock = GST_ELEMENT_CLOCK (src))) {
            /* we have a clock, get base time and ref clock */
            timestamp = GST_ELEMENT (src)->base_time;
            gst_object_ref (clock);
        } else {
            /* no clock, can't set timestamps */
            timestamp = GST_CLOCK_TIME_NONE;
        }
        GST_OBJECT_UNLOCK (src);

        if (G_LIKELY (clock)) {
            /* the time now is the time of the clock minus the base time */
            timestamp = gst_clock_get_time (clock) - timestamp;
            gst_object_unref (clock);

            /* if we have a framerate adjust timestamp for frame latency */
            if (GST_CLOCK_TIME_IS_VALID (src->duration)) {
                if (timestamp > src->duration)
                    timestamp -= src->duration;
                else
                    timestamp = 0;
            }

        }

        /* FIXME: use the timestamp from the buffer itself! */
        GST_BUFFER_TIMESTAMP (*buf) = timestamp;
        GST_BUFFER_DURATION (*buf) = src->duration;
    }

    /* Create caps for buffer */
    GstCaps *mycaps;
    GstStructure        *structure;

    mycaps = gst_caps_new_empty();

    if (src->cAttrs.colorSpace == ColorSpace_UYVY) {
        structure = gst_structure_new( "video/x-raw-yuv",
            "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'),
            "framerate", GST_TYPE_FRACTION,
                gst_value_get_fraction_numerator(&src->framerate),
                gst_value_get_fraction_denominator(&src->framerate),
            "width", G_TYPE_INT,    src->width,
            "height", G_TYPE_INT,   src->height,
            (gchar*) NULL);

    }
    else if(src->cAttrs.colorSpace == ColorSpace_YUV420PSEMI) {
        structure = gst_structure_new( "video/x-raw-yuv",
            "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC('N', 'V', '1', '2'),
            "framerate", GST_TYPE_FRACTION,
                gst_value_get_fraction_numerator(&src->framerate),
                gst_value_get_fraction_denominator(&src->framerate),
            "width", G_TYPE_INT,    src->width,
            "height", G_TYPE_INT,   src->height,
            (gchar*) NULL);
    }
    else {
        GST_ERROR("unsupported fourcc\n");
        return FALSE;
    }

    gst_caps_append_structure(mycaps, gst_structure_copy (structure));
    gst_structure_free(structure);
    gst_buffer_set_caps(*buf, mycaps);
    gst_caps_unref(mycaps);

		{
			static int fn;
			fn++;
			GST_INFO("capture frame %d", fn);
		}

    GST_LOG("create end");
    return GST_FLOW_OK;
}
Exemplo n.º 23
0
/******************************************************************************
 * gst_ticircbuffer_display
 *     Print out a nice visual of the buffer.  Legend:
 *         W = write pointer
 *         R = read pointer
 *         B = write pointer and read pointer when they are at the same place
 *         | = end of window
 *         = = active data when there is at least one window in the buffer
 *         - = active data when there is less than a window in the buffer
 *
 *     Examples:
 *        Write pointer after read pointer (contiguousData == TRUE):
 *            [  R=====================|==================================W   ]
 *        Read pointer after write pointer (contiguousData == FALSE):
 *            [==============================W  R=====================|=======]
 *
 ******************************************************************************/
static void gst_ticircbuffer_display(GstTICircBuffer *circBuf)
{
    static Char   buffer[DISP_SIZE + 3];
    static Int32  lastReadBufOffset  = -1;
    static Int32  lastWriteBufOffset = -1;
    Int8*         circBufStart       = Buffer_getUserPtr(circBuf->hBuf);
    Int32         readOffset         = circBuf->readPtr  - circBufStart;
    Int32         writeOffset        = circBuf->writePtr - circBufStart;
    Int32         winBufOffset       = ((double)circBuf->windowSize /
                                        (double)Buffer_getSize(circBuf->hBuf) *
                                        DISP_SIZE);
    Int32         readBufOffset     = ((double)readOffset /
                                       (double)Buffer_getSize(circBuf->hBuf) *
                                        DISP_SIZE) + 1;
    Int32         writeBufOffset    = ((double)writeOffset /
                                       (double)Buffer_getSize(circBuf->hBuf) *
                                        DISP_SIZE) + 1;
    Char          dataChar          = '=';
    Int           i;

    /* If the read or write pointers are past the end of the buffer, make sure
     * they get printed in the last position.
     */
    if (readBufOffset == DISP_SIZE+1) {
        readBufOffset--;
    }
    if (writeBufOffset == DISP_SIZE+1) {
        writeBufOffset--;
    }

    /* Don't print anything out if the buffer hasn't changed significantly */
    if (lastReadBufOffset > -1) {
        if (lastReadBufOffset == readBufOffset &&
            lastWriteBufOffset == writeBufOffset) {
            return;
        }
    }
    lastReadBufOffset  = readBufOffset;
    lastWriteBufOffset = writeBufOffset;

    /* Create a border on the ends of the buffer, and terminate the string */
    buffer[0]             = '[';
    buffer[DISP_SIZE + 1] = ']';
    buffer[DISP_SIZE + 2] = '\0';

    /* If less than a full window is available, represent data with "-" instead
     * of "=".
     */
    if (!gst_ticircbuffer_window_available(circBuf)) {
        dataChar = '-';
    }

    /* Fill in each character of the array representing the buffer */
    for (i = 1; i <= DISP_SIZE; i++) {

        /* If we are at a read or write pointer location, generate the
         * character representing the pointer.
         */
        if (i == readBufOffset && i == writeBufOffset) {
           buffer[i] = 'B';
        }
        else if (i == readBufOffset) {
           buffer[i] = 'R';
        }
        else if (i == writeBufOffset) {
           buffer[i] = 'W';
        }

        /* Case 1:
         *
         * [  B                     |                                      ] or
         * [==B=====================|======================================]
         *
         * If the read and write pointers are at the same location, we need
         * to fill all other locations with either a ' ' or an '=', depending
         * on if the buffer is full or empty.  The only exception is the
         * location of the end of the window, represented by '|'.
         */
        else if (readBufOffset == writeBufOffset) {
           if (i == readBufOffset + winBufOffset) {
               buffer[i] = '|';
           }
           else if (!circBuf->contiguousData) {
               buffer[i] = dataChar;
           }
           else {
               buffer[i] = ' ';
           }
        }

        /* Case 2:
         *
         * [  R=====================|==================================W   ]
         *
         * Mark active data when the write pointer is after the read pointer
         * If the current location is between the two pointers, we get a '=',
         * otherwise we get a ' '.
         */
        else if (circBuf->contiguousData) {
           if (i > readBufOffset && i == readBufOffset + winBufOffset) {
               buffer[i] = '|';
           }
           else if (i > readBufOffset && i < writeBufOffset) {
               buffer[i] = dataChar;
           }
           else {
               buffer[i] = ' ';
           }
        }

        /* Case 3:
         *
         * [==============================W  R=====================|=======]
         *
         * Mark active data when the read pointer is after the write pointer
         * If the current location is between the two pointers, we get a ' ',
         * otherwise we get a '='.
         */
        else {
           if (i > readBufOffset && i == readBufOffset + winBufOffset) {
               buffer[i] = '|';
           }
           else if (i > readBufOffset || i < writeBufOffset) {
               buffer[i] = dataChar;
           }
           else {
               buffer[i] = ' ';
           }
        }
    }
    printf("%s\n", buffer);
}
Exemplo n.º 24
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;
}
Exemplo n.º 25
0
Arquivo: appMain.c Projeto: sv99/DVSDK
/******************************************************************************
 * 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;
}
Exemplo n.º 26
0
Void *deiThrFxn(Void *arg)
{
	DeiEnv *envp = (DeiEnv *) arg;
	Void *status = THREAD_SUCCESS;

	Uint32 sysRegBase = 0;

	VIDENC1_Handle hDei = NULL;
	IDEI_Params deiParams;

	Uint16 frame_width = 0, frame_height = 0;
	Uint16 threshold_low = 0, threshold_high = 0;

	IVIDEO1_BufDescIn inBufDesc;	
	XDM_BufDesc outBufDesc;	
	XDAS_Int8 *outbufs[2];	
	XDAS_Int32 outBufSizeArray[2];
	VIDENC1_InArgs inArgs;
	IDEI_OutArgs outArgs;	
	Uint32 bufferSize;

	CMEM_AllocParams cmemPrm;
	Uint32 prevBufAddr, outBufAddr;

	Buffer_Handle cBuf, dBuf;

	Int ret = 0;

	int fd = -1;
	dm365mmap_params_t dm365mmap_params;
	pthread_mutex_t Dmai_DM365dmaLock = PTHREAD_MUTEX_INITIALIZER;

	/* ▼▼▼▼▼ Initialization ▼▼▼▼▼ */
	DM365MM_init(); printf("\n"); /* dm365mm issue */
	sysRegBase = DM365MM_mmap(0x01C40000, 0x4000);

	CMEM_init();
	cmemPrm.type = CMEM_HEAP;
	cmemPrm.flags = CMEM_NONCACHED;
	cmemPrm.alignment = 32;
	prevBufAddr = (Uint32)CMEM_alloc(IN_OUT_BUF_SIZE, &cmemPrm);
	outBufAddr = (Uint32)CMEM_alloc(IN_OUT_BUF_SIZE, &cmemPrm);

	frame_width = 720;
	frame_height = 576;
	threshold_low = 16;
	threshold_high = 20;

	/* Create DEI instance */	
	deiParams.videncParams.size = sizeof(IDEI_Params);
	deiParams.frameHeight = frame_height;
	deiParams.frameWidth = frame_width; 
	deiParams.inLineOffset = frame_width;
	deiParams.outLineOffset = (frame_width - 8);	
	deiParams.threshold_low = threshold_low;
	deiParams.threshold_high = threshold_high;

	deiParams.inputFormat = XDM_YUV_422ILE;	
	deiParams.outputFormat = XDM_YUV_420SP;
	
	deiParams.q_num = 1;
	deiParams.askIMCOPRes = 0; 
	deiParams.sysBaseAddr = sysRegBase;

	hDei = VIDENC1_create(envp->hEngine, "dei", (VIDENC1_Params *)&deiParams);						  
    if(hDei == NULL)
	{
		ERR("DEI alg creation failed\n");
        cleanup(THREAD_FAILURE);		
	}

	fd = open("/dev/dm365mmap", O_RDWR | O_SYNC);

	

	Rendezvous_meet(envp->hRendezvousInit);
	/* ▲▲▲▲▲ Initialization ▲▲▲▲▲ */
	while (1) {

		if (Fifo_get(envp->hFromCaptureFifo, &cBuf) < 0) {
			ERR("Failed to get buffer from capture thread\n");
			cleanup(THREAD_FAILURE);
		}

		if (Fifo_get(envp->hFromDisplayFifo, &dBuf) < 0) {
			ERR("Failed to get buffer from display thread\n");
			cleanup(THREAD_FAILURE);
		}

		inBufDesc.numBufs = 4;

		bufferSize = (frame_width * frame_height);
		
		inBufDesc.bufDesc[0].bufSize = bufferSize;
		inBufDesc.bufDesc[0].buf = (XDAS_Int8 *)Buffer_getUserPtr(cBuf);
		inBufDesc.bufDesc[0].accessMask = 0;

		inBufDesc.bufDesc[1].bufSize = bufferSize;
		inBufDesc.bufDesc[1].buf = (XDAS_Int8 *)(Buffer_getUserPtr(cBuf) + bufferSize);
		inBufDesc.bufDesc[1].accessMask = 0;

		inBufDesc.bufDesc[2].bufSize = bufferSize;
		inBufDesc.bufDesc[2].buf = (XDAS_Int8 *)prevBufAddr;
		inBufDesc.bufDesc[2].accessMask = 0;

		inBufDesc.bufDesc[3].bufSize = bufferSize;
		inBufDesc.bufDesc[3].buf = (XDAS_Int8 *)(prevBufAddr + bufferSize);
		inBufDesc.bufDesc[3].accessMask = 0;	
		
		/* Output buffers */
		outBufDesc.numBufs = 2;
		outbufs[0] = (XDAS_Int8*)outBufAddr;
		outbufs[1] = (XDAS_Int8*)(outBufAddr + bufferSize);
		outBufSizeArray[0] = bufferSize;
		outBufSizeArray[1] = bufferSize / 2;

		outBufDesc.bufSizes = outBufSizeArray;
		outBufDesc.bufs = outbufs;

		inArgs.size = sizeof(VIDENC1_InArgs);
		outArgs.videncOutArgs.size = sizeof(IDEI_OutArgs);

		ret = VIDENC1_process((VIDENC1_Handle)hDei,
								 &inBufDesc,
								 &outBufDesc,
								 &inArgs,
								 (IVIDENC1_OutArgs *)&outArgs);
		if (ret != VIDENC1_EOK) {
			ERR("DEI process failed\n");
			cleanup(THREAD_FAILURE);
		}

		dm365mmap_params.src = CMEM_getPhys(outbufs[0]);
		dm365mmap_params.srcmode = 0;
		dm365mmap_params.dst = Buffer_getPhysicalPtr(dBuf);
		dm365mmap_params.dstmode = 0;
		dm365mmap_params.srcbidx = 712;
		dm365mmap_params.dstbidx = 704;
		dm365mmap_params.acnt = 704;
		dm365mmap_params.bcnt = 576;
		dm365mmap_params.ccnt = 1;
		dm365mmap_params.bcntrld = dm365mmap_params.bcnt;
		dm365mmap_params.syncmode = 1;

		pthread_mutex_lock(&Dmai_DM365dmaLock);
		if (ioctl(fd, DM365MMAP_IOCMEMCPY, &dm365mmap_params) == -1) {
        	ERR("memcpy: Failed to do memcpy\n");
        	cleanup(THREAD_FAILURE);
    	}
		pthread_mutex_unlock(&Dmai_DM365dmaLock);

    	dm365mmap_params.src = CMEM_getPhys(outbufs[1]);
    	dm365mmap_params.srcmode = 0;
    	dm365mmap_params.dst = Buffer_getPhysicalPtr(dBuf) + (Buffer_getSize(dBuf) * 2 / 3);
    	dm365mmap_params.dstmode = 0;
    	dm365mmap_params.srcbidx = 712;
		dm365mmap_params.dstbidx = 704;
    	dm365mmap_params.acnt = 712;
    	dm365mmap_params.bcnt = 570 / 2;
    	dm365mmap_params.ccnt = 1;
    	dm365mmap_params.bcntrld = dm365mmap_params.bcnt;
    	dm365mmap_params.syncmode = 1;

		pthread_mutex_lock(&Dmai_DM365dmaLock);
		if (ioctl(fd, DM365MMAP_IOCMEMCPY, &dm365mmap_params) == -1) {
        	ERR("memcpy: Failed to do memcpy\n");
        	cleanup(THREAD_FAILURE);
    	}
		pthread_mutex_unlock(&Dmai_DM365dmaLock);
    	Buffer_setNumBytesUsed(dBuf, 704 * 576 * 3 / 2);

    	dm365mmap_params.src = Buffer_getPhysicalPtr(cBuf);
    	dm365mmap_params.srcmode = 0;
    	dm365mmap_params.dst = prevBufAddr;
    	dm365mmap_params.dstmode = 0;
    	dm365mmap_params.srcbidx = 1440;
		dm365mmap_params.dstbidx = 1440;
    	dm365mmap_params.acnt = 1440;
    	dm365mmap_params.bcnt = 576;
    	dm365mmap_params.ccnt = 1;
    	dm365mmap_params.bcntrld = dm365mmap_params.bcnt;
    	dm365mmap_params.syncmode = 1;

    	pthread_mutex_lock(&Dmai_DM365dmaLock);
		if (ioctl(fd, DM365MMAP_IOCMEMCPY, &dm365mmap_params) == -1) {
        	ERR("memcpy: Failed to do memcpy\n");
        	cleanup(THREAD_FAILURE);
    	}
		pthread_mutex_unlock(&Dmai_DM365dmaLock);

		/* Send buffer to display thread */
		if (Fifo_put(envp->hToDisplayFifo, dBuf) < 0) {
			ERR("Failed to send buffer to dei thread\n");
			cleanup(THREAD_FAILURE);
		}

		/* Send buffer to display thread */
		if (Fifo_put(envp->hToCaptureFifo, cBuf) < 0) {
			ERR("Failed to send buffer to dei thread\n");
			cleanup(THREAD_FAILURE);
		}

	}

cleanup:
	Rendezvous_force(envp->hRendezvousInit);
	Rendezvous_meet(envp->hRendezvousCleanup);

	/* Delete DEI ALG instance */
	VIDENC1_delete(hDei);

	DM365MM_ummap(sysRegBase,0x4000);

	DM365MM_exit();

	if (fd > 0) {   
        close(fd);
    }

	return status;
}
Exemplo n.º 27
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;
}
Exemplo n.º 28
0
/******************************************************************************
 * writeFramePlanar
******************************************************************************/
Int writeFramePlanar(Buffer_Handle hBuf, FILE *outFile)
{
    Int8   *yPtr, *cbcrPtr;        
    UInt32  ySize;                /* Size of Luma data */
    Uint32  cbcrSize;             /* Size of Chroma (cb and cr) data */
    Uint32  cbOffset, crOffSet;   
    Char   *colorFmt;
 
    BufferGfx_Dimensions dim;
    BufferGfx_getDimensions(hBuf, &dim);    

    /*  The three color planes are evenly spaced in the output buffer. Each 
     *  plane has been allocated a size equal to the input creation parameter 
     *  maxWidth x maxHeight.              
     */     
    cbOffset = Buffer_getSize(hBuf) * 1/3;
    crOffSet = Buffer_getSize(hBuf) * 2/3;
    
    ySize = dim.width * dim.height;
    
    switch(BufferGfx_getColorSpace(hBuf)){
       case ColorSpace_YUV444P:
           cbcrSize = dim.width * dim.height * 2;
           colorFmt = "444P";
           break;
       case ColorSpace_YUV422P:
           cbcrSize = dim.width * dim.height;
           colorFmt = "422P";
           break;
       case ColorSpace_YUV420P:
           cbcrSize = dim.width * dim.height / 2;
           colorFmt = "420P";
           break;
       case ColorSpace_GRAY:
           cbcrSize = 0;
           colorFmt = "GRAY";
           break;
       default:
           printf("Unsupported output color space %d.\n", 
            BufferGfx_getColorSpace(hBuf));
           return -1;        
    }    

    /* Write Y plane */
    yPtr = Buffer_getUserPtr(hBuf);
    if (fwrite(yPtr, 1, ySize, outFile) != ySize ) {
        fprintf(stderr,"Failed to write y data to disk\n");
        return -1;
    }

    /* Write Cb plane */
    cbcrPtr = Buffer_getUserPtr(hBuf) + cbOffset;   
    if (fwrite(cbcrPtr, 1, cbcrSize / 2 , outFile) != cbcrSize / 2 ) {
        fprintf(stderr,"Failed to write y data to disk\n");
        return -1;
    }

    /* Write Cr plane */
    cbcrPtr = Buffer_getUserPtr(hBuf) + crOffSet;
    if (fwrite(cbcrPtr, 1, cbcrSize / 2  , outFile) != cbcrSize / 2 ) {
        fprintf(stderr,"Failed to write y data to disk\n");
        return -1;
    }

    printf("Wrote %s image size %d (%dx%d) to disk\n",
           colorFmt, 
           (Int) (ySize + cbcrSize),
           (Int) dim.width, (Int) dim.height);

    return 0;
}
Exemplo n.º 29
0
/******************************************************************************
 * Idec1_process
 ******************************************************************************/
Int Idec1_process(Idec1_Handle hId, Buffer_Handle hInBuf,
                 Buffer_Handle hOutBuf)
{
    BufferGfx_Dimensions    dim;
    IMGDEC1_DynamicParams   dynParams;
    IMGDEC1_InArgs          inArgs;
    IMGDEC1_OutArgs         outArgs;
    IMGDEC1_Status          decStatus;
    XDM1_BufDesc            inBufDesc;
    XDM1_BufDesc            outBufDesc;
    XDAS_Int32              status;
    XDAS_Int8 *             inPtr;
    XDAS_Int8 *             outPtr;
    UInt32                  offset = 0;
    UInt32                  i;
    
    assert(hId);
    assert(hInBuf);
    assert(hOutBuf);
    assert(Buffer_getSize(hInBuf));   
    assert(Buffer_getUserPtr(hInBuf)); 
    assert(Buffer_getUserPtr(hOutBuf));
    assert(Buffer_getNumBytesUsed(hInBuf));
    assert(Buffer_getSize(hOutBuf));
    assert(Buffer_getType(hOutBuf) == Buffer_Type_GRAPHICS);

    BufferGfx_getDimensions(hOutBuf, &dim);
    
    inPtr  = Buffer_getUserPtr(hInBuf);
    outPtr = Buffer_getUserPtr(hOutBuf);

    inArgs.size             = sizeof(IMGDEC1_InArgs);
    inArgs.numBytes         = Buffer_getNumBytesUsed(hInBuf);

    outArgs.size            = sizeof(IMGDEC1_OutArgs);

    inBufDesc.numBufs       = 1;
    outBufDesc.numBufs      = hId->minNumOutBufs;
    
    inBufDesc.descs[0].buf = inPtr;
    inBufDesc.descs[0].bufSize = Buffer_getSize(hInBuf);

    for(i = 0; i < hId->minNumOutBufs; i++)
    {
        outBufDesc.descs[i].buf = (XDAS_Int8 *)((unsigned int)outPtr + offset);
        offset +=  hId->minOutBufSize[i];
        outBufDesc.descs[i].bufSize = hId->minOutBufSize[i];
    }   
        
    /* Decode image buffer */
    status = IMGDEC1_process(hId->hDecode, &inBufDesc, &outBufDesc, &inArgs,
                            &outArgs);

    Buffer_setNumBytesUsed(hInBuf, outArgs.bytesConsumed);

    if (status != IMGDEC1_EOK) {
        if (XDM_ISFATALERROR(outArgs.extendedError)) {
            Dmai_err2("IMGDEC1_process() failed with error (%d ext: 0x%x)\n",
                      (Int)status, (Uns) outArgs.extendedError);
            return Dmai_EFAIL;
        }
        else {
            Dmai_dbg1("IMGDEC1_process() non-fatal error 0x%x\n",
                      (Uns) outArgs.extendedError);
            return Dmai_EBITERROR;
        }
    }

    /* Get the dynamic codec status */
    decStatus.data.buf = NULL;
    decStatus.size = sizeof(IMGDEC1_Status);
    dynParams.size = sizeof(IMGDEC1_DynamicParams);
    status = IMGDEC1_control(hId->hDecode, XDM_GETSTATUS, &dynParams,
                            &decStatus);

    if (status != IMGDEC1_EOK) {
        Dmai_err1("XDM_GETSTATUS failed, status=%d\n", status);
        return Dmai_EFAIL;
    }

    /* Set output Color Format */
    switch (decStatus.outputChromaFormat) {
        case XDM_YUV_422ILE:
            BufferGfx_setColorSpace (hOutBuf, ColorSpace_UYVY); 
            break;
        case XDM_YUV_420P:
            BufferGfx_setColorSpace (hOutBuf, ColorSpace_YUV420P); 
            break;
        case XDM_YUV_422P:
            BufferGfx_setColorSpace (hOutBuf, ColorSpace_YUV422P); 
            break;
        case XDM_YUV_444P:
            BufferGfx_setColorSpace (hOutBuf, ColorSpace_YUV444P); 
            break;
        case XDM_GRAY:
            BufferGfx_setColorSpace (hOutBuf, ColorSpace_GRAY); 
            break;
        default:
            printf("Unsupported output color space.\n");
            return Dmai_EFAIL;        
    }
       
    dim.x = dim.y = 0;
    dim.width = decStatus.outputWidth;
    dim.height = decStatus.outputHeight;
    dim.lineLength = decStatus.outputWidth *
                      ColorSpace_getBpp(BufferGfx_getColorSpace(hOutBuf)) / 8;
    
    if (BufferGfx_setDimensions(hOutBuf, &dim) < 0) {
        Dmai_err0("Frame does not fit in allocated buffer\n");
        return Dmai_EFAIL;
    }

    return Dmai_EOK;
}
Exemplo n.º 30
0
/******************************************************************************
 * Sdec_process
 ******************************************************************************/
Int Sdec_process(Sdec_Handle hSd, Buffer_Handle hInBuf, Buffer_Handle hOutBuf)
{
    SPHDEC_DynamicParams    dynamicParams;
    SPHDEC_Status           decStatus;
    XDM_BufDesc             inBufDesc;
    XDM_BufDesc             outBufDesc;
    XDAS_Int32              inBufSizeArray[1];
    XDAS_Int32              outBufSizeArray[1];
    XDAS_Int32              status;
    SPHDEC_InArgs           inArgs;
    SPHDEC_OutArgs          outArgs;
    XDAS_Int8              *inPtr;
    XDAS_Int8              *outPtr;

    assert(hSd);
    assert(hInBuf);
    assert(hOutBuf);
    assert(Buffer_getUserPtr(hInBuf));
    assert(Buffer_getUserPtr(hOutBuf));
    assert(Buffer_getNumBytesUsed(hInBuf));
    assert(Buffer_getSize(hOutBuf));

    inPtr = Buffer_getUserPtr(hInBuf);
    outPtr = Buffer_getUserPtr(hOutBuf);

    inBufSizeArray[0]          = Buffer_getNumBytesUsed(hInBuf);
    outBufSizeArray[0]         = Buffer_getSize(hOutBuf);

    inBufDesc.bufSizes         = inBufSizeArray;
    inBufDesc.bufs             = &inPtr;
    inBufDesc.numBufs          = 1;

    outBufDesc.bufSizes        = outBufSizeArray;
    outBufDesc.bufs            = &outPtr;
    outBufDesc.numBufs         = 1;

    inArgs.size                = sizeof(SPHDEC_InArgs);
    inArgs.inBufferSize        = Buffer_getNumBytesUsed(hInBuf);

    outArgs.size               = sizeof(SPHDEC_OutArgs);

    /* Decode the speech buffer */
    status = SPHDEC_process(hSd->hDecode, &inBufDesc, &outBufDesc, &inArgs,
                            &outArgs);

    if (status != SPHDEC_EOK) {
        decStatus.size = sizeof(SPHDEC_Status);
        dynamicParams.size = sizeof(SPHDEC_DynamicParams);
        status = SPHDEC_control(hSd->hDecode, XDM_GETSTATUS, &dynamicParams,
                                &decStatus);
        if (status != SPHDEC_EOK) {
            Dmai_err1("XDM_GETSTATUS failed, status=%d\n", status);
            return Dmai_EFAIL;
        }

        if (status == SPHDEC_ERUNTIME ||
            XDM_ISFATALERROR(decStatus.extendedError)) {

            Dmai_err2("SPHDEC_process() failed with error (%d ext: 0x%x)\n",
                      (Int)status, (Uns) decStatus.extendedError);
            return Dmai_EFAIL;
        }
        else {
            Dmai_dbg1("SPHDEC_process() non-fatal error 0x%x\n",
                      (Uns) decStatus.extendedError);
            return Dmai_EBITERROR;
        }
    }

    /* A fixed x2 decompression ratio, only works for g711 */
    Buffer_setNumBytesUsed(hOutBuf, Buffer_getNumBytesUsed(hInBuf) * 2);

    return Dmai_EOK;
}