⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 votest.c

📁 用于TM1300/PNX1300系列DSP(主要用于视频处理)的外部设备的源码
💻 C
字号:
/*
 * Copyright (c) 1995-1999 by TriMedia Technologies. 
 *
 * +------------------------------------------------------------------+
 * | This software is furnished under a license and may only be used  |
 * | and copied in accordance with the terms and conditions of  such  |
 * | a license and with the inclusion of this copyright notice. This  |
 * | software or any other copies of this software may not be provided|
 * | or otherwise made available to any other person.  The ownership  |
 * | and title of this software is not transferred.                   |
 * |                                                                  |
 * | The information in this software is subject  to change without   |
 * | any  prior notice and should not be construed as a commitment by |
 * | TriMedia Technologies.                                           |
 * |                                                                  |
 * | this code and information is provided "as is" without any        |
 * | warranty of any kind, either expressed or implied, including but |
 * | not limited to the implied warranties of merchantability and/or  |
 * | fitness for any particular purpose.                              |
 * +------------------------------------------------------------------+
 *
 *  Module name              : votest.c    1.38
 *
 *  Last update              : 17:38:05 - 00/11/09
 *
 *  Description              :
 *
 *  This program demonstrates video-out 1X and 2X scaling.
 *
 *  Revision                 :
 *        Built for the TCS 2.0 release
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <tmlib/AppModel.h>
#include <tm1/tmVO.h>
#include <tm1/tmInterrupts.h>
#include <ops/custom_defs.h>
#include <tm1/tmProcessor.h>
#include <tmlib/tmlibc.h>
#include <tmlib/dprintf.h>

#include <tm1/tmHelp.h>

#define                YUV422        4
#define                YUV420        2
#define                NUM_VO_BUF    3

#define             CACHE_LINE_SIZE    64

typedef struct _videoBuf {    /* video buffer structure example */
    UInt32      Y;            /* buffer pointer for Y */
    UInt32      U;            /* buffer pointer for U */
    UInt32      V;            /* buffer pointer for V */
    UInt32      flag;         /* flags used in buffer management */
}           vBuf, *pvBuf;

static vBuf voBuf[NUM_VO_BUF];

static Int  displayNum = 0;
static Int  yFieldStride = 0;
static Int  uvFieldStride = 0;
static volatile UInt32 voISRCount = 0;
static Int  bufWidth = 768;
static Int  bufHeight = 576;

static tmVideoAnalogStandard_t videoStandard = vasNTSC;
static tmVideoAnalogAdapter_t  adapterType = vaaNone;
static Int  voInst;
static voInstanceSetup_t voInstSup;
static voYUVSetup_t voYUVSup;

static Int  BIGNUMBER = 600;

static Int  tmLogoWidth = 720;
static Int  tmLogoHeight = 480;
static Int  tmLogoStride = 720;

static Int  clownWidth = 360;
static Int  clownHeight = 288;
static Int  clownStride = 384;

static Int  sclownWidth = 120;
static Int  sclownHeight = 100;
static Int  sclownStride = 120;

static char *Header = "\nTriMedia Video-Out Example Program Version 2.0\n\n";

void 
voUsage(void)
{
    printf("usage: votest [ntsc|pal] \n");
}

static void 
my_abort(Char * name, Int err)
{
    assert(name != Null);
    fprintf(stderr, "%s failed, error code %x\n", name, err);
    exit(-1);
}

/***** Command Line Interface **********************************************/
void 
voCheckArgcv(int argc, char **argv)
{
    int         mark = 1;

    argc--;
    while (argc > 0) {
        if (strcmp(argv[mark], "ntsc") == 0) {       /* NTSC MODE */
            videoStandard = vasNTSC;
            argc--;
            mark++;
        }
        else if (strcmp(argv[mark], "pal") == 0) {    /* PAL MODE */
            videoStandard = vasPAL;
            argc--;
            mark++;
        }
        else {
            voUsage();
            argc--;
            mark++;
        }
    }
}

/***** File Reading ******************************************/
int 
readYUVFiles(char *baseName, int hsize, int vsize,
         UInt32 yBuf, UInt32 uBuf, UInt32 vBuf, int fm)
{
    int         count, ySize, uvSize;
    char        fn[80];
    FILE       *fp;

    ySize = hsize * vsize;
    if (fm == YUV422) {
        uvSize = ySize >> 1;
    }
    else if (fm == YUV420) {
        uvSize = ySize >> 2;
    }
    if (!yBuf)
        return (1);
    if (!uBuf)
        return (2);
    if (!vBuf)
        return (3);

    printf("reading %s.[yuv]\n", baseName);
    /* read Y file: binary */
    sprintf(fn, "%s.y", baseName);
    /* printf("File %s: ", fn); */
    fp = fopen(fn, "rb");
    if (!fp)
        return (4);
    count = fread((char *) yBuf, 1, ySize, fp);
    if (count != ySize)
        return 4;
    /* printf("Read %d bytes \n", count); */
    fclose(fp);
    _cache_copyback((void*)yBuf, ySize);

    /* read U file: binary */
    sprintf(fn, "%s.u", baseName);
    /* printf("File %s: ", fn); */
    fp = fopen(fn, "rb");
    if (!fp)
        return (4);
    count = fread((char *) uBuf, 1, uvSize, fp);
    if (count != uvSize)
        return 4;
    /* printf("Read %d bytes \n", count); */
    fclose(fp);
    _cache_copyback((void*)uBuf, uvSize);

    /* read V file: binary */
    sprintf(fn, "%s.v", baseName);
    /* printf("File %s: ", fn); */
    fp = fopen(fn, "rb");
    if (!fp)
        return (4);
    count = fread((char *) vBuf, 1, uvSize, fp);
    if (count != uvSize)
        return 4;
    /* printf("Read %d bytes \n", count); */
    fclose(fp);
    _cache_copyback((void*)vBuf, uvSize);

    return (0);
}

/***** Interrupt Service Routine *******************************************/
void 
_voTestISR(void)
{
    unsigned long vo_status = MMIO(VO_STATUS);

    voISRCount++;

    if (voYTR(vo_status))
        voAckYTR_ACK();
    if (voURUN(vo_status))
        voAckURUN_ACK();
    if (voHBE(vo_status))
        voAckHBE_ACK();
    if (voBUF2EMPTY(vo_status))
        voAckBFR2_ACK();

    if (vo_status & VO_BUF1EMPTY) {    /* bfr1 empty */
        if (!(vo_status & VO_FIELD2)) {
            voYUVChangeBuffer(voInst,
                      voBuf[displayNum].Y,
                      voBuf[displayNum].U,
                      voBuf[displayNum].V);
            voAckBFR1_ACK();
        } else {
            /* from f1 to f2 */
            voYUVChangeBuffer(voInst,
                      voBuf[displayNum].Y + (yFieldStride),
                      voBuf[displayNum].U + (uvFieldStride),
                      voBuf[displayNum].V + (uvFieldStride));
            voAckBFR1_ACK();
	}
        return;
    }
}                /* end of vo ISR */

void 
voTestISR(void)
{
#pragma TCS_handler
    AppModel_suspend_scheduling();
    AppModel_run_on_sstack((AppModel_Fun)_voTestISR, Null);
    AppModel_resume_scheduling();
}


void 
voReadFiles()
{
    int         retVal = 0;

#ifndef __TCS_nohost__
    if (retVal = readYUVFiles("clown120x100", 120, 100,
                  voBuf[0].Y, voBuf[0].U, voBuf[0].V, YUV422)) {
        my_abort("readYUVFiles clown120x100", retVal);
    }
    if (retVal = readYUVFiles("clown", 384, 288,
                  voBuf[1].Y, voBuf[1].U, voBuf[1].V, YUV420)) {
        my_abort("readYUVFiles clown", retVal);
    }
    if (retVal = readYUVFiles("tmlogo", 720, 480,
                  voBuf[2].Y, voBuf[2].U, voBuf[2].V, YUV420)) {
        my_abort("readYUVFiles tmlogo", retVal);
    }
#endif
}

void 
voOpenInstAPI()
{
    tmLibdevErr_t retVal;

    if (retVal = voOpen(&voInst))
        my_abort("voOpen", retVal);

    memset((char *) (&voInstSup), 0, sizeof (voInstanceSetup_t));
    voInstSup.interruptPriority = intPRIO_3;
    voInstSup.isr = voTestISR;
    voInstSup.videoStandard = videoStandard;
    voInstSup.adapterType = adapterType;

    voFrequencyToDDS(27000000.0, &voInstSup.ddsFrequency);
        
    voInstSup.hbeEnable = True;
    voInstSup.underrunEnable = True;
    if (retVal = voInstanceSetup(voInst, &voInstSup))
        my_abort("voInstanceSetup", retVal);
}

void 
voYUVAPI(voYUVModes_t mode,
     int imageWidth, int imageHeight, int imageStride,
     int imageVertOffset, int imageHorzOffset,
     Pointer yBase, Pointer uBase, Pointer vBase)
{
    tmLibdevErr_t retVal;

    memset((char *) (&voYUVSup), 0, sizeof (voYUVSetup_t));
    voYUVSup.mode = mode;
    voYUVSup.buf1emptyEnable = True;
    voYUVSup.yThresholdEnable = False;
    voYUVSup.yThreshold = False;
    voYUVSup.yBase = yBase;
    voYUVSup.uBase = uBase;
    voYUVSup.vBase = vBase;

    voYUVSup.imageVertOffset = imageVertOffset;
    voYUVSup.imageHorzOffset = imageHorzOffset;
    voYUVSup.imageHeight = (imageHeight >> 1);
    voYUVSup.yStride = (2 * imageStride);
    voYUVSup.uStride = imageStride;
    voYUVSup.vStride = imageStride;

    switch (mode) {
    case vo422_COSITED_UNSCALED:
    case vo422_INTERSPERSED_UNSCALED:
    case vo420_UNSCALED:
        voYUVSup.imageWidth = imageWidth;
        break;
    case vo422_COSITED_SCALED:
    case vo422_INTERSPERSED_SCALED:
    case vo420_SCALED:
    default:
        voYUVSup.imageWidth = imageWidth << 1;
        break;
    }

    if (retVal = voYUVSetup(voInst, &voYUVSup))
        my_abort("viYUVSetup", retVal);
}

void 
voRunSclown()
{
    printf("Displaying clown (120x100) in 422 1X mode for ~10 seconds.\n");

    displayNum = 0;
    yFieldStride = sclownStride;
    uvFieldStride = (sclownStride >> 1);
    voYUVAPI(vo422_COSITED_UNSCALED, 
             sclownWidth, sclownHeight, sclownStride, 64, 192, 
             (Pointer)voBuf[0].Y, (Pointer)voBuf[0].U, (Pointer)voBuf[0].V);
    for (voISRCount = 0; voISRCount < BIGNUMBER;) {
        ;
    }

    printf("Displaying clown (120x100) in 422 2X mode for ~10 seconds.\n");
    voYUVAPI(vo422_COSITED_SCALED,
             sclownWidth, sclownHeight, sclownStride, 64, 192, 
             (Pointer) voBuf[0].Y, (Pointer) voBuf[0].U, (Pointer) voBuf[0].V);

    for (voISRCount = 0; voISRCount < BIGNUMBER;) {
        ;
    }
}

void 
voRunClown()
{
    tmLibdevErr_t retVal;

    printf("Displaying clown (360x288) in 420 1X mode for ~10 seconds.\n");
    displayNum = 1;
    yFieldStride = clownStride;
    uvFieldStride = 0;
    voYUVAPI(vo420_UNSCALED, 
            clownWidth, clownHeight, clownStride, 64, 128,
            (Pointer) voBuf[1].Y, (Pointer) voBuf[1].U, (Pointer) voBuf[1].V);
    if (retVal = voStart(voInst))
        my_abort("voStart", retVal);
    for (voISRCount = 0; voISRCount < BIGNUMBER;) {
        ;
    }

    printf("Displaying clown (360x288) in 420 2X mode for ~10 seconds.\n");
    voYUVAPI(vo420_SCALED, 
            clownWidth, clownHeight, clownStride, 64, 0,
            (Pointer) voBuf[1].Y, (Pointer) voBuf[1].U, (Pointer) voBuf[1].V);

    for (voISRCount = 0; voISRCount < BIGNUMBER;) {
        ;
    }
}

void 
voRunTMlogo()
{
    printf("Displaying tmLogo (720x480) in 420 1X mode for ~10 seconds.\n");
    displayNum = 2;
    yFieldStride = tmLogoStride;
    uvFieldStride = (tmLogoStride >> 1);

    voYUVAPI(vo420_UNSCALED, 
             tmLogoWidth, tmLogoHeight, tmLogoStride, 0, 0,
             (Pointer) voBuf[2].Y, (Pointer) voBuf[2].U, (Pointer) voBuf[2].V);

    for (voISRCount = 0; voISRCount < BIGNUMBER;) {
        ;
    }
}

void 
voRun()
{
    tmLibdevErr_t retVal;

    voOpenInstAPI();

    voRunClown();
    voRunSclown();
    voRunTMlogo();

    if (retVal = voStop(voInst))
        my_abort("voStop", retVal);

    voClose(voInst);
}

UInt32 
allocSz(int bufSz)
{
    UInt32      temp;
    int         i;

    if ((temp = (UInt32) _cache_malloc(bufSz, -1)) == Null)
        my_abort("_cache_malloc", 0);

    _cache_copyback((void*)temp, bufSz);

    return temp;
}

void 
voAllocBuf()
{
    int         i, szY, szUV;

    szY = (bufWidth * (bufHeight + 4));
    szUV = ((bufWidth >> 1) * (bufHeight + 4));
    for (i = 0; i < NUM_VO_BUF; i++) {
        voBuf[i].Y = allocSz(szY);
        voBuf[i].U = allocSz(szUV);
        voBuf[i].V = allocSz(szUV);
        voBuf[i].flag = 0;
        DP(("Allocate voBuf i %d Y %x U %x V %x\n",
            i, voBuf[i].Y, voBuf[i].U, voBuf[i].V));
    }
}

void 
voFreeBuf()
{
    int         i;

    for (i = 0; i < NUM_VO_BUF; i++) {
        _cache_free((Pointer) voBuf[i].Y);
        _cache_free((Pointer) voBuf[i].U);
        _cache_free((Pointer) voBuf[i].V);
    }
}

void 
voSetDP()
{
    DPsize(32000);    /* for debugging */
}

/***************************************************************************/
int 
main(int argc, char **argv)
{
    /* printf("TriMedia Video-Out Example Program Version 2.0 \n\n"); */

    voSetDP();

    DP((Header));
    printf(Header);

    DP(("Running on ")); tmHelpReportSystem(Null);
    printf("Running on "); tmHelpReportSystem(stdout);

#ifndef __TCS_nohost__
    voCheckArgcv(argc, argv);
#else
    argc = 0;
    argv = NULL;
#endif

    voAllocBuf();
    voReadFiles();
    voRun();
    voFreeBuf();
    exit(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -