initial commit from tarball
This commit is contained in:
9
frontend/Makefile.am
Normal file
9
frontend/Makefile.am
Normal file
@@ -0,0 +1,9 @@
|
||||
bin_PROGRAMS = aacplusenc
|
||||
man_MANS = ../docs/aacplusenc.1
|
||||
|
||||
aacplusenc_SOURCES = main.c
|
||||
|
||||
INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/src
|
||||
LDADD = $(top_builddir)/src/libaacplus.la -lm
|
||||
|
||||
aacplusenc_LDFLAGS = @FFTW3_LDFLAGS@
|
||||
66
frontend/au_channel.h
Normal file
66
frontend/au_channel.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define WAV_HEADER_SIZE 64
|
||||
|
||||
#define WAV_FORMAT_FLOAT 0xFFFE
|
||||
|
||||
typedef struct {
|
||||
int sampleRate;
|
||||
int nChannels;
|
||||
long nSamples;
|
||||
int aFmt;
|
||||
} WavInfo;
|
||||
|
||||
inline FILE* AuChannelOpen (const char* filename, WavInfo* info)
|
||||
{
|
||||
unsigned char header[12];
|
||||
unsigned char data[WAV_HEADER_SIZE];
|
||||
FILE *handle;
|
||||
unsigned int chunksize;
|
||||
|
||||
|
||||
if (!strcmp(filename,"-"))
|
||||
handle = stdin;
|
||||
else
|
||||
handle = fopen(filename, "rb");
|
||||
|
||||
if(!handle) return NULL;
|
||||
|
||||
if(fread(header, 1, 12, handle) != 12) return NULL;
|
||||
info->nSamples = (header[4] | (header[5] << 8) | (header[6] << 16) | (header[7] << 24)) + 8;
|
||||
|
||||
while (memcmp(header, "data", 4) != 0){
|
||||
if(fread(header, 1, 8, handle) != 8) return NULL;
|
||||
chunksize = (header[4] | (header[5] << 8) | (header[6] << 16) | (header[7] << 24));
|
||||
//fprintf(stderr, "%c%c%c%c %d", header[0], header[1], header[2], header[3], chunksize);
|
||||
if(!memcmp(header, "fmt ", 4)) {
|
||||
if(chunksize > WAV_HEADER_SIZE) return NULL;
|
||||
if(fread(data, 1, chunksize, handle) != chunksize) return NULL;
|
||||
info->aFmt = data[0] | data[1] << 8;
|
||||
info->nChannels = data[2] | data[3] << 8;
|
||||
info->sampleRate = data[4] | data[5] << 8 | data[6] << 12 | data[7] << 16;
|
||||
} else if(memcmp(header, "data", 4) != 0) {
|
||||
if(fseek(handle, chunksize, SEEK_CUR) != 0) return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
inline void AuChannelClose (FILE *audioChannel)
|
||||
{
|
||||
fclose(audioChannel);
|
||||
}
|
||||
|
||||
inline size_t AuChannelReadShort(FILE *audioChannel, short *samples, int nSamples, int *readed)
|
||||
{
|
||||
*readed = fread(samples, 2, nSamples, audioChannel);
|
||||
return *readed <= 0;
|
||||
}
|
||||
|
||||
inline size_t AuChannelReadFloat(FILE *audioChannel, float *samples, int nSamples, int *readed)
|
||||
{
|
||||
*readed = fread(samples, 4, nSamples, audioChannel);
|
||||
return *readed <= 0;
|
||||
}
|
||||
141
frontend/main.c
Normal file
141
frontend/main.c
Normal file
@@ -0,0 +1,141 @@
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "au_channel.h"
|
||||
#include "aacplus.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
WavInfo inputInfo;
|
||||
FILE *inputFile = NULL;
|
||||
FILE *hADTSFile;
|
||||
|
||||
int error;
|
||||
int bEncodeMono = 0;
|
||||
int frmCnt = 0;
|
||||
|
||||
/*
|
||||
* parse command line arguments
|
||||
*/
|
||||
if (argc != 5) {
|
||||
fprintf(stderr, "\nUsage: %s <wav_file> <bitstream_file> <bitrate> <(m)ono/(s)tereo>\n", argv[0]);
|
||||
fprintf(stderr, "\nExample: %s input.wav out.aac 24000 s\n", argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ( strcmp (argv[4],"m") == 0 ) {
|
||||
bEncodeMono = 1;
|
||||
}
|
||||
else {
|
||||
if ( strcmp (argv[4],"s") != 0 ) {
|
||||
fprintf(stderr, "\nWrong mode %s, use either (m)ono or (s)tereo\n", argv[4]);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
fflush(stdout);
|
||||
|
||||
inputFile = AuChannelOpen (argv[1], &inputInfo);
|
||||
|
||||
if(inputFile == NULL){
|
||||
fprintf(stderr,"could not open %s\n",argv[1]);
|
||||
exit(10);
|
||||
}
|
||||
|
||||
if (inputInfo.nChannels==1 && !bEncodeMono) {
|
||||
fprintf(stderr,"Need stereo input for stereo coding mode !\n");
|
||||
exit(10);
|
||||
}
|
||||
|
||||
if (strcmp(argv[2],"-")==0)
|
||||
hADTSFile=stdout;
|
||||
else
|
||||
hADTSFile = fopen(argv[2], "wb");
|
||||
|
||||
if(!hADTSFile) {
|
||||
fprintf(stderr, "\nFailed to create ADTS file\n") ;
|
||||
exit(10);
|
||||
}
|
||||
|
||||
/*
|
||||
Be verbose
|
||||
*/
|
||||
unsigned long inputSamples=0;
|
||||
unsigned long maxOutputBytes=0;
|
||||
aacplusEncHandle hEncoder = aacplusEncOpen(inputInfo.sampleRate,
|
||||
inputInfo.nChannels,
|
||||
&inputSamples,
|
||||
&maxOutputBytes);
|
||||
|
||||
aacplusEncConfiguration *cfg = aacplusEncGetCurrentConfiguration(hEncoder);
|
||||
cfg->bitRate = atoi(argv[3]);
|
||||
cfg->bandWidth = 0;
|
||||
cfg->outputFormat = 1;
|
||||
cfg->nChannelsOut = bEncodeMono ? 1 : inputInfo.nChannels;
|
||||
if(inputInfo.aFmt == WAV_FORMAT_FLOAT){
|
||||
cfg->inputFormat = AACPLUS_INPUT_FLOAT;
|
||||
}
|
||||
|
||||
fprintf(stdout,"input file %s: \nsr = %d, nc = %d fmt = %d\n\n",
|
||||
argv[1], inputInfo.sampleRate, inputInfo.nChannels, inputInfo.aFmt);
|
||||
fprintf(stdout,"output file %s: \nbr = %d inputSamples = %lu maxOutputBytes = %lu nc = %d m = %d\n\n",
|
||||
argv[2], cfg->bitRate, inputSamples, maxOutputBytes, cfg->nChannelsOut, bEncodeMono);
|
||||
fflush(stdout);
|
||||
|
||||
int ret = 0;
|
||||
if((ret = aacplusEncSetConfiguration(hEncoder, cfg)) == 0) {
|
||||
fprintf(stdout,"setting cfg failed\n", ret);
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint8_t *outputBuffer = malloc(maxOutputBytes);
|
||||
int32_t *TimeDataPcm;
|
||||
if(inputInfo.aFmt == WAV_FORMAT_FLOAT) {
|
||||
TimeDataPcm = calloc(inputSamples, sizeof(float));
|
||||
} else {
|
||||
TimeDataPcm = calloc(inputSamples, sizeof(short));
|
||||
}
|
||||
|
||||
int stopLoop = 0;
|
||||
int bytes = 0;
|
||||
do {
|
||||
int numSamplesRead = 0;
|
||||
if(inputInfo.aFmt == WAV_FORMAT_FLOAT) {
|
||||
if ( AuChannelReadFloat(inputFile, (float *) TimeDataPcm, inputSamples, &numSamplesRead) > 0) {
|
||||
stopLoop = 1;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if ( AuChannelReadShort(inputFile, (short *) TimeDataPcm, inputSamples, &numSamplesRead) > 0) {
|
||||
stopLoop = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(numSamplesRead < inputSamples) {
|
||||
stopLoop = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
bytes = aacplusEncEncode(hEncoder, (int32_t *) TimeDataPcm, numSamplesRead,
|
||||
outputBuffer,
|
||||
maxOutputBytes);
|
||||
|
||||
if(bytes > 0) fwrite(outputBuffer, bytes, 1, hADTSFile);
|
||||
|
||||
frmCnt++;
|
||||
fprintf(stderr,"[%d]\r",frmCnt); fflush(stderr);
|
||||
} while (!stopLoop && bytes >= 0);
|
||||
|
||||
fprintf(stderr,"\n");
|
||||
fflush(stderr);
|
||||
|
||||
printf("\nencoding finished\n");
|
||||
aacplusEncClose(hEncoder);
|
||||
fclose(hADTSFile);
|
||||
free(outputBuffer);
|
||||
free(TimeDataPcm);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user