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

📄 encodedecode.h

📁 采集视频信号进行H264压缩并通过UDP传送
💻 H
字号:
/*
 * encodedecode.h
 *
 * ============================================================================
 * Copyright (c) Texas Instruments Inc 2005
 *
 * Use of this software is controlled by the terms and conditions found in the
 * license agreement under which this software has been supplied or provided.
 * ============================================================================
 */

#ifndef _ENCODEDECODE_H
#define _ENCODEDECODE_H

/* Codec Engine headers */
#include <xdc/std.h>
#include <ti/sdo/ce/Engine.h>
#include <ti/sdo/ce/video/viddec.h>
#include <ti/sdo/ce/video/videnc.h>
#include <ti/sdo/ce/speech/sphdec.h>
#include <ti/sdo/ce/speech/sphenc.h>
#include <ti/sdo/ce/osal/Memory.h>
#include <ti/sdo/ce/CERuntime.h>

#include <ti/sdo/ce/trace/gt.h>

#include <stdio.h>
#include <pthread.h>

/* Enables or disables debug output */
#ifdef __DEBUG
#define DBG(fmt, args...) fprintf(stderr, "Encodedecode Debug: " fmt, ## args)
#define dspTraceDump(ce) Engine_fwriteTrace((ce), "[DSP] ", stderr)
#else
#define DBG printf
//#define DBG(fmt, args...)
#define dspTraceDump(ce)
#endif

#define ERR(fmt, args...) fprintf(stderr, "Encodedecode Error: " fmt, ## args)

/* Function error codes */
#define SUCCESS        0
#define FAILURE        -1

/* Thread error codes */
#define THREAD_SUCCESS (void *) 0
#define THREAD_FAILURE (void *) -1

/* The codec engine to use in this application */
#define ENGINE_NAME     "loopback"

/* Device parameters */
#define V4L2_DEVICE     "/dev/video0"
#define OSD_DEVICE      "/dev/fb/0"
#define ATTR_DEVICE     "/dev/fb/2"
#define FBVID_DEVICE    "/dev/fb/3"

/* True of false enumeration */
#define TRUE            1
#define FALSE           0

/* Defining infinite time */
#define FOREVER         -1

/* Scaling factors for the video standards */
#define PAL             12
#define NTSC            10

//#define MYPORT 5001 // the port usels yill be connectins to
#define PORT 5010
#define MAXDATASIZE 100

#define MYPORT 5000 // rhe pofi usors rill be connecting
#define BACKLOG 10 // hor MDy pending connecxions queue xlll hold

enum Resolution {
    D1,
    CIF,
    ZOOM
};
enum VideoEncoder {
    MPEG4_VIDEO_ENCODER,
    H264_VIDEO_ENCODER,
    NUM_VIDEO_ENCODERS
} VideoEncoder;

/* The demo environment passed to all threads */
typedef struct DemoEnv {
    int bitrate;
    char * address;
    int  time;
    int  interface;
    enum Resolution resolution;
    char *speechFile;
    char *videoFile;
    //enum SpeechEncoder speechEncoder;
    enum VideoEncoder videoEncoder;
    //enum SoundInput soundInput;
    int videoBitRate;
} DemoEnv;

/* Global data structure */
typedef struct GlobalData {
    int quit;                   /* Global quit flag */
    int play;                   /* Whether to play or pause */
    int frames;                 /* Video frame counter */
    int videoBytesEncoded;      /* Video bytes encoded counter */
    int yFactor;                /* Vertical scaling factor (PAL vs. NTSC) */
} GlobalData;

/* Thread main functions */
extern void *encodeThrFxn(void *arg);
extern void *decodeThrFxn(void *arg);
extern void *videoThrFxn(void *arg);
extern int server(void);
extern void *ctrlThrFxn(void *arg);
extern void *videosendThrFxn(void *arg);
extern void *videorecvThrFxn(void *arg);

/* Global data */
extern GlobalData      gbl;

extern pthread_cond_t videoInit;
extern pthread_cond_t encodeInit;
extern pthread_cond_t decodeInit;
extern pthread_cond_t  allInit;
extern pthread_cond_t networkInit;

extern pthread_mutex_t videoMutex;
extern pthread_mutex_t encodeMutex;
extern pthread_mutex_t decodeMutex;
extern pthread_mutex_t allMutex;
extern pthread_mutex_t dataMutex;
extern pthread_mutex_t networkMutex;

/* Functions to protect the global data */
static inline int getQuit(void)
{
    int quit;

    pthread_mutex_lock(&dataMutex);
    quit = gbl.quit;
    pthread_mutex_unlock(&dataMutex);

    return quit;
}

static inline void setQuit()
{
    pthread_mutex_lock(&dataMutex);
    gbl.quit = TRUE;
    pthread_mutex_unlock(&dataMutex);
}

static inline int getPlay(void)
{
    int play;

    pthread_mutex_lock(&dataMutex);
    play = gbl.play;
    pthread_mutex_unlock(&dataMutex);

    return play;
}

static inline void setPlay(int play)
{
    pthread_mutex_lock(&dataMutex);
    gbl.play = play;
    pthread_mutex_unlock(&dataMutex);
}

static inline int getAndResetFrames(void)
{
    int frames;

    pthread_mutex_lock(&dataMutex);
    frames = gbl.frames;
    gbl.frames = 0;
    pthread_mutex_unlock(&dataMutex);

    return frames;
}

static inline void incFrames(void)
{
    pthread_mutex_lock(&dataMutex);
    gbl.frames++;
    pthread_mutex_unlock(&dataMutex);
}

static inline int getAndResetVideoBytesEncoded(void)
{
    int videoBytesEncoded;

    pthread_mutex_lock(&dataMutex);
    videoBytesEncoded = gbl.videoBytesEncoded;
    gbl.videoBytesEncoded = 0;
    pthread_mutex_unlock(&dataMutex);

    return videoBytesEncoded;
}

static inline void incVideoBytesEncoded(int videoBytesEncoded)
{
    pthread_mutex_lock(&dataMutex);
    gbl.videoBytesEncoded += videoBytesEncoded;
    pthread_mutex_unlock(&dataMutex);
}

static inline int getYFactor(void)
{
    int yFactor;

    pthread_mutex_lock(&dataMutex);
    yFactor = gbl.yFactor;
    pthread_mutex_unlock(&dataMutex);

    return yFactor;
}

static inline void setYFactor(int yFactor)
{
    pthread_mutex_lock(&dataMutex);
    gbl.yFactor = yFactor;
    pthread_mutex_unlock(&dataMutex);
}

/* Scale vertically depending on video standard */
#define YSCALE(x) (((x) * getYFactor()) / 10)

/* Waits for a given condition (with an assigned mutex) */
#define WAIT_CONDITION(mutex, condition)            \
    if (!getQuit()) {                               \
        pthread_mutex_lock(&(mutex));               \
        pthread_cond_wait(&(condition), &(mutex));  \
        pthread_mutex_unlock(&(mutex));             \
    }

/* Signals that a condition (with an assigned mutex) has been met */
#define MET_CONDITION(mutex, condition)             \
    pthread_mutex_lock(&(mutex));                   \
    pthread_cond_broadcast(&(condition));           \
    pthread_mutex_unlock(&(mutex));

/* Cleans up cleanly after a failure */
#define CLEANUP(x)                                  \
    status = (x);                                   \
    setQuit(TRUE);                                  \
    MET_CONDITION(allMutex, allInit);               \
    MET_CONDITION(encodeMutex, encodeInit);         \
    MET_CONDITION(decodeMutex, decodeInit);	    \
    goto cleanup

/* Breaks a processing loop for a clean exit */
#define BREAK_LOOP(x)                               \
    status = (x);                                   \
    setQuit(TRUE);                                  \
    MET_CONDITION(allMutex, allInit);               \
    MET_CONDITION(encodeMutex, encodeInit);         \
    MET_CONDITION(decodeMutex, decodeInit);         \
    break

#endif /* _ENCODEDECODE_H */

⌨️ 快捷键说明

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