📄 app.c
字号:
/* * Copyright 2006 by Texas Instruments Incorporated. * * All rights reserved. Property of Texas Instruments Incorporated. * Restricted rights to use, duplicate or disclose this code are * granted through contract. * *//* * ======== app.c ======== * * Non-Codec-Engine-using part of the Arm-side Linux application, that at * certain points talks to the Codec-Engine-using part of the Arm-side * Linux application. The latter module is in file ceapp.c. * * The call flow is: app.c functions -> ceapp.c functions -> CodecEngine libs */#include <stdio.h>#include <stdlib.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/ioctl.h>#include <fcntl.h>#include <errno.h>#include <linux/soundcard.h>#include <xdc/std.h>#include <ti/sdo/ce/Engine.h>#include <ti/sdo/ce/CERuntime.h>#include <ti/sdo/ce/osal/Memory.h>/* Definition of buffer sizes for input data, encoded data * * Our application reads a raw file one block at the time at the * input buffer. It calls ceapp to encodes that from the input buffer into the * "encoded data" buffer. Actually the codec does not do anything but makes * a copy of the input data. Then appliation writes the * output block to another file. This flow is illustrated here: * * <input file block> ==(app)==> [inBuf] ==> * ==(ceapp/encoder)==> [encodedBuf] ==> * ==(app)==> <output file block> * * For simplicity, below we define all blocks to be 1K, but those sizes * could be arbitrary (though they must match). * * Note: It is EXTREMELY important that the buffers that hold the data are not * simply malloc()ed, but are actually physically contiguous. This is so * because the buffers are passed on to the codecs running on the DSP * verbatim (there is no buffer copying involved). CodecEngine * (and the ceapp module) provides and API for allocating such buffers. */#define INFRAMESIZE (4096 * sizeof(char)) /* raw frame (input) */#define ENCODEDFRAMESIZE (4096 * sizeof(char)) /* encoded frame *//* prototypes of functions defined in ceapp.c: */extern int ceapp_init(); /* initialize the ceapp module */extern char *ceapp_allocContigBuf( /* allocate physically contiguous */ int bufSize, /* buffer of given size; description */ char *bufDescription /* is used only for print */ );extern int ceapp_validateBufSizes( /* confirm that the codec */ int inBufSize, /* support these buffer sizes */ int encodedBufSize /* for the raw input audio and encoded */ );extern int ceapp_encodeBuf( /* encode raw audio data in inBuf */ char *inBuf, /* and store result in encodedBuf */ int inBufSize, char *encodedBuf, int encodedBufSize );extern void ceapp_freeContigBuf( /* free the contiguous buffer */ char *buf, int bufSize );extern void ceapp_exit(); /* exit the ceapp module */#define SAMPLE_RATE 48000#define NUM_CHANNELS 2#define SOUND_DEVICE "/dev/dsp"/****************************************************************************** * setSampleRate ******************************************************************************/static int setSampleRate(int soundFd, int sampleRate) { static int fd = 0; if (!fd) { if (!soundFd) { printf("No valid sound filedescriptor available\n"); return -1; } fd = soundFd; } /* Set the sample rate */ if (ioctl(fd, SNDCTL_DSP_SPEED, &sampleRate) == -1) { printf("Could not set sample rate (%d)\n", sampleRate); return -1; } return 0;}/****************************************************************************** * initSoundDevice ******************************************************************************/static int initSoundDevice(void){ int channels = NUM_CHANNELS; int format = AFMT_S16_LE; int soundFd; /* Open the sound device for writing */ soundFd = open(SOUND_DEVICE, O_WRONLY); if (soundFd == -1) { printf("Failed to open the sound device (%s)\n", SOUND_DEVICE); return -1; } /* Set the sound format (only AFMT_S16_LE supported) */ if (ioctl(soundFd, SNDCTL_DSP_SETFMT, &format) == -1) { printf("Could not set format %d\n", format); return -1; } /* Set the number of channels */ if (ioctl(soundFd, SNDCTL_DSP_CHANNELS, &channels) == -1) { printf("Could not set mixer to %d channels\n", channels); return -1; } /* Set the sample rate */ if (setSampleRate(soundFd, SAMPLE_RATE) == -1) { return -1; } return soundFd;}/* * ======== encodeFile ======== * This function reads blocks of the input file one at the time, encodes * each block, verify whether the output is the same as input, and writes * the result to the output file. */static int encodeFile(char *inFileName, char *outFileName){ /* pointers to contiguous shared data buffers (shared b/w Arm and DSP) */ static char *inBuf = NULL; /* to contig-alloc for INFRAMESIZE */ static char *encodedBuf = NULL; /* to contig-alloc for ENCODEDFRAMESIZE */ FILE *inFile = NULL, *outFile = NULL; int outputFd = 0; int status, n; /* open file streams for input and output */ if ((inFile = fopen(inFileName, "rb")) == NULL) { printf("App-> ERROR: can't read file %s\n", inFileName); exit(1); } if ((outFile = fopen(outFileName, "wb")) == NULL) { printf("App-> ERROR: can't write to file %s\n", outFileName); fclose(inFile); exit(1); } /* Initialize the AIC33 audio codec for writing */ outputFd = initSoundDevice(); if ( outputFd == -1 ) { printf("App-> ERROR: can't open sound device\n"); fclose(inFile); fclose(outFile); exit(1); } /* initialize the ceapp which in turns initializes Codec Engine */ status = ceapp_init(); if (status != 0) { goto end; } /* Allocate 2 contiguous buffers. * The text description we pass alongside are used only in a printf. */ inBuf = ceapp_allocContigBuf(INFRAMESIZE, "input data"); encodedBuf = ceapp_allocContigBuf(ENCODEDFRAMESIZE, "encoded data"); if (inBuf == NULL || encodedBuf == NULL ) { printf("App-> Error allocating buffers (%p, %p)!\n", inBuf, encodedBuf); status = -1; goto end; } /* * Optional call: validate that the codec we'll use really supports the * sizes we specify here. There's no reason why they shouldn't, in this * particular case. */ status = ceapp_validateBufSizes( INFRAMESIZE, ENCODEDFRAMESIZE ); if (status != 0) { goto end; } /* * Read frames from the input file, and write * encoded data to an output file file */ for (n = 0 ;; n++) { // printf("App-> Processing frame %d...\n", n); if ( fread(inBuf, INFRAMESIZE, 1, inFile) == 0 ) { /* until EOF */ break; } /* Write samples to the device driver from contiguous buffer */ if ( write(outputFd, inBuf, INFRAMESIZE) == -1) { printf("Error writing data to %s (%s)\n", SOUND_DEVICE, strerror(errno)); break; } status = ceapp_encodeBuf(inBuf, INFRAMESIZE, encodedBuf, ENCODEDFRAMESIZE); if(status != 0) { printf("App-> ERROR: Encoding frame %d FAILED\n", n); goto end; } // write to file fwrite(encodedBuf, ENCODEDFRAMESIZE, 1, outFile); } printf("App-> Finished encoding %d frames\n", n); status = 0; /* success */end: /* close the files */ fclose(inFile); fclose(outFile); /* free the contiguous buffers */ if ( inBuf != NULL) { ceapp_freeContigBuf(inBuf, INFRAMESIZE); } if ( encodedBuf != NULL) { ceapp_freeContigBuf(encodedBuf, ENCODEDFRAMESIZE); } /* quit the CE application */ ceapp_exit(); /* and return success (0) or failure (non-0) */ return status;}/* * ======== main ======== */int main(int argc, char *argv[]){ char *inFileName, *outFileName; char *rawBuffer; if (argc <= 1) { inFileName = "davinciEffects.raw"; outFileName = "davinciEffects.copy"; } else if (argc != 3 ) { printf("Usage: %s [input-file] [output-file]\n", argv[0]); exit(1); } else { inFileName = argv[1]; outFileName = argv[2]; } CERuntime_init(); printf("App-> Application started.\n"); if (encodeFile(inFileName, outFileName) == 0) { printf("App-> Application finished successfully.\n"); } else { printf("App-> Application FAILED.\n"); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -