📄 main.c
字号:
/* * main.c * * ============================================================================ * 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. * ============================================================================ *//* Standard Linux headers */#include <stdio.h>#include <errno.h>#include <fcntl.h>#include <stdlib.h>#include <getopt.h>#include <unistd.h>#include <string.h>#include <pthread.h>#include <sys/ioctl.h>#include <sys/resource.h>/* Davinci specific kernel headers */#include <video/davincifb.h>/* Codec Engine headers */#include <xdc/std.h>#include <ti/sdo/ce/Engine.h>#include <ti/sdo/ce/trace/gt.h>#include <ti/sdo/ce/CERuntime.h>#include <ti/sdo/ce/utils/trace/TraceUtil.h>/* Demo headers */#include <rendezvous.h>#include "encode.h"#include "video.h"#include "speech.h"#include "ctrl.h"/* The levels of initialization */#define LOGSINITIALIZED 0x1#define RENDEZVOUSOPENED 0x2#define VIDEOTHREADCREATED 0x4#define SPEECHTHREADCREATED 0x8typedef struct Args { SpeechEncoder speechEncoder; VideoEncoder videoEncoder; SoundInput soundInput; char *speechFile; char *videoFile; int imageWidth; int imageHeight; int videoBitRate; int svideoInput; int keyboard; int time; int interface;} Args;/* Global variable declarations for this application */GlobalData gbl = { 0, 1, 0, 0, 0, NOSTD };/****************************************************************************** * usage ******************************************************************************/static void usage(void){ printf("Usage: encode [options]\n\n" "Options:\n" "-s | --speechfile Speech file to record to\n" "-v | --videofile Video file to record to\n" "-r | --resolution Video resolution ('width'x'height') [720x480]\n" "-b | --bitrate Bit rate to encode video at [variable]\n" "-l | --linein Use line in for encoding sound instead of mic\n" "-x | --svideo Use s-video instead of composite video input\n" "-k | --keyboard Enable keyboard interface\n" "-t | --time Number of seconds to run the demo [infinite]\n" "-i | --interface Launch the demo interface when exiting [off]\n" "-h | --help Print this message\n\n" "You must supply at least a video or a speech file or both\n" "with appropriate extensions for the file formats.\n\n");}/****************************************************************************** * parseArgs ******************************************************************************/static void parseArgs(int argc, char *argv[], Args *argsp){ const char shortOptions[] = "s:v:r:b:kxt:lih"; const struct option longOptions[] = { {"speechfile", required_argument, NULL, 's'}, {"videofile", required_argument, NULL, 'v'}, {"resolution", required_argument, NULL, 'r'}, {"bitrate", required_argument, NULL, 'b'}, {"svideo", no_argument, NULL, 'x'}, {"keyboard", no_argument, NULL, 'k'}, {"time", required_argument, NULL, 't'}, {"linein", no_argument, NULL, 'l'}, {"interface", no_argument, NULL, 'i'}, {"help", no_argument, NULL, 'h'}, {0, 0, 0, 0} }; int index; int c; char *extension; int imageWidth; int imageHeight; for (;;) { c = getopt_long(argc, argv, shortOptions, longOptions, &index); if (c == -1) { break; } switch (c) { case 0: break; case 's': extension = rindex(optarg, '.'); if (extension == NULL) { fprintf(stderr, "Speech file without extension: %s\n", optarg); exit(EXIT_FAILURE); } if (strcmp(extension, ".g711") == 0) { argsp->speechEncoder = G711_SPEECH_ENCODER; } else { fprintf(stderr, "Unknown speech file extension: %s\n", extension); exit(EXIT_FAILURE); } argsp->speechFile = optarg; break; case 'v': extension = rindex(optarg, '.'); if (extension == NULL) { fprintf(stderr, "Video file without extension: %s\n", optarg); exit(EXIT_FAILURE); } if (strcmp(extension, ".264") == 0) { argsp->videoEncoder = H264_VIDEO_ENCODER; } else if (strcmp(extension, ".mpeg4") == 0) { argsp->videoEncoder = MPEG4_VIDEO_ENCODER; } else { fprintf(stderr, "Unknown video file extension: %s\n", extension); exit(EXIT_FAILURE); } argsp->videoFile = optarg; break; case 'r': if (sscanf(optarg, "%dx%d", &imageWidth, &imageHeight) != 2) { fprintf(stderr, "Invalid resolution supplied (%s)\n", optarg); usage(); exit(EXIT_FAILURE); } /* Sanity check resolution */ if (imageWidth <= 0 || imageHeight <= 0 || imageWidth > D1_WIDTH || imageHeight > D1_HEIGHT) { fprintf(stderr, "Video resolution must be maximum %dx%d\n", D1_WIDTH, D1_HEIGHT); exit(EXIT_FAILURE); } /* Only use multiples of 16 */ argsp->imageWidth = imageWidth & ~0xf; argsp->imageHeight = imageHeight & ~0xf; break; case 'b': argsp->videoBitRate = atoi(optarg); break; case 'x': argsp->svideoInput = TRUE; break; case 'k': argsp->keyboard = TRUE; break; case 't': argsp->time = atoi(optarg); break; case 'l': argsp->soundInput = LINEIN_SOUND_INPUT; break; case 'i': argsp->interface = TRUE; break; case 'h': usage(); exit(EXIT_SUCCESS); default: usage(); exit(EXIT_FAILURE); } } /* Need at least one file to encode to */ if (!argsp->videoFile && !argsp->speechFile) { usage(); exit(EXIT_FAILURE); }}/****************************************************************************** * detectVideoStandard ******************************************************************************/static int detectVideoStandard(void){ int fd; int std; /* Open video display device */ fd = open(FBVID_DEVICE, O_RDWR); if (fd == -1) { ERR("Failed to open fb device %s (%s)\n", FBVID_DEVICE, strerror(errno)); return FAILURE; } /* Query the display device driver for video standard chosen */ if (ioctl(fd, FBIO_GETSTD, &std) == -1) { ERR("Failed to get video standard from display device driver\n"); return FAILURE; } if ((std >> 16) == 0x1) { DBG("NTSC selected\n"); gblSetYFactor(NTSCSTD); } else { DBG("PAL selected\n"); gblSetYFactor(PALSTD); } close(fd); return SUCCESS;}/****************************************************************************** * main ******************************************************************************/int main(int argc, char *argv[]){ unsigned int initMask = 0; int status = EXIT_SUCCESS; int numThreads; struct sched_param schedParam; Rendezvous_Obj rendezvous; pthread_t videoThread; pthread_t speechThread; VideoEnv videoEnv; SpeechEnv speechEnv; CtrlEnv ctrlEnv; pthread_attr_t attr; void *ret; Args args = { 0, 0, MIC_SOUND_INPUT, NULL, NULL, 720, 480, -1, FALSE, FALSE, FOREVER, FALSE }; /* Detect PAL or NTSC */ if (detectVideoStandard() == FAILURE) { cleanup(EXIT_FAILURE); } /* Parse the arguments given to the app and set the app environment */ parseArgs(argc, argv, &args); printf("Encode demo started.\n"); /* Initialize the mutex which protects the global data */ pthread_mutex_init(&gbl.mutex, NULL); /* Set the priority of this whole process to max (requires root) */ setpriority(PRIO_PROCESS, 0, -20); /* Initialize Codec Engine runtime */ CERuntime_init(); DBG("Codec Engine initialized\n"); /* Initialize the logs. Must be done after CERuntime_init() */ TraceUtil_start(ENGINE_NAME); initMask |= LOGSINITIALIZED; DBG("Logging initialized\n"); /* Open the object which synchronizes the thread initialization */ numThreads = 1; if (args.videoFile) { numThreads += 3; } if (args.speechFile) { numThreads += 1; } Rendezvous_open(&rendezvous, numThreads); initMask |= RENDEZVOUSOPENED; DBG("Rendezvous opened for %d threads\n", numThreads); /* Initialize the thread attributes */ if (pthread_attr_init(&attr)) { ERR("Failed to initialize thread attrs\n"); cleanup(EXIT_FAILURE); } /* Force the thread to use custom scheduling attributes */ if (pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED)) { ERR("Failed to set schedule inheritance attribute\n"); cleanup(EXIT_FAILURE); } /* Set the thread to be fifo real time scheduled */ if (pthread_attr_setschedpolicy(&attr, SCHED_FIFO)) { ERR("Failed to set FIFO scheduling policy\n"); cleanup(EXIT_FAILURE); } /* Start the video thread if a file name is supplied */ if (args.videoFile) { /* Set the thread priority */ schedParam.sched_priority = sched_get_priority_max(SCHED_FIFO); if (pthread_attr_setschedparam(&attr, &schedParam)) { ERR("Failed to set scheduler parameters\n"); cleanup(EXIT_FAILURE); } /* Create the thread */ videoEnv.hRendezvous = &rendezvous; videoEnv.videoFile = args.videoFile; videoEnv.svideoInput = args.svideoInput; videoEnv.videoEncoder = args.videoEncoder; videoEnv.videoBitRate = args.videoBitRate; videoEnv.imageWidth = args.imageWidth; videoEnv.imageHeight = args.imageHeight; if (pthread_create(&videoThread, &attr, videoThrFxn, &videoEnv)) { ERR("Failed to create video thread\n"); cleanup(EXIT_FAILURE); } initMask |= VIDEOTHREADCREATED; } /* Start the speech thread if a file name is supplied */ if (args.speechFile) { /* Set the thread priority */ schedParam.sched_priority = sched_get_priority_max(SCHED_FIFO) - 2; if (pthread_attr_setschedparam(&attr, &schedParam)) { ERR("Failed to set scheduler parameters\n"); cleanup(EXIT_FAILURE); } /* Create the thread */ speechEnv.hRendezvous = &rendezvous; speechEnv.speechFile = args.speechFile; speechEnv.speechEncoder = args.speechEncoder; speechEnv.soundInput = args.soundInput; if (pthread_create(&speechThread, &attr, speechThrFxn, &speechEnv)) { ERR("Failed to create speech thread\n"); cleanup(EXIT_FAILURE); } initMask |= SPEECHTHREADCREATED; } /* Main thread becomes the control thread */ ctrlEnv.hRendezvous = &rendezvous; ctrlEnv.keyboard = args.keyboard; ctrlEnv.time = args.time; ctrlEnv.imageWidth = args.imageWidth; ctrlEnv.imageHeight = args.imageHeight; ctrlEnv.videoFile = args.videoFile; ctrlEnv.videoEncoder = args.videoEncoder; ctrlEnv.speechFile = args.speechFile; ctrlEnv.speechEncoder = args.speechEncoder; ret = ctrlThrFxn(&ctrlEnv); if (ret == THREAD_FAILURE) { status = EXIT_FAILURE; }cleanup: /* Make sure the other threads aren't waiting for init to complete */ Rendezvous_force(&rendezvous); /* Wait until the other threads terminate */ if (initMask & SPEECHTHREADCREATED) { if (pthread_join(speechThread, &ret) == 0) { if (ret == THREAD_FAILURE) { status = EXIT_FAILURE; } } } if (initMask & VIDEOTHREADCREATED) { if (pthread_join(videoThread, &ret) == 0) { if (ret == THREAD_FAILURE) { status = EXIT_FAILURE; } } } if (initMask & RENDEZVOUSOPENED) { Rendezvous_close(&rendezvous); } if (initMask & LOGSINITIALIZED) { TraceUtil_stop(); } /* Destroy the global mutex */ pthread_mutex_destroy(&gbl.mutex); if (status == EXIT_SUCCESS && args.interface) { if (execl("./interface", "interface", "-l 3", NULL) == -1) { status = EXIT_FAILURE; } } exit(status);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -