/*
* FAAC - Freeware Advanced Audio Coder
* Copyright (C) 2001 Menno Bakker
* Copyright (C) 2002-2017 Krzysztof Nikiel
* Copyright (C) 2004 Dan Villiom P. Christiansen
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#include <signal.h>
#include <faac.h>
int main(int argc, char *argv[])
{
faacEncHandle hEncoder;
faacEncConfigurationPtr myFormat;
unsigned long samplesInput, maxBytesOutput;
char *audioFileName = NULL;
char *aacFileName = NULL;
int16_t *tmpbuf;
unsigned char *bitbuf;
FILE *f;
FILE *outfile = NULL;
int rawChans = 1; // disabled by default
int rawBits = 16;
int rawRate = 8000;
/* begin process command line */
if (argc < 3)
{
printf("Usage: %s input_file output_file\n", argv[0]);
return 1;
}
audioFileName = argv[1];
aacFileName = argv[2];
f = fopen(audioFileName,"rb");
/* open the encoder library */
hEncoder = faacEncOpen(rawRate, rawChans,
&samplesInput, &maxBytesOutput);
if (hEncoder == NULL)
{
fprintf(stderr, "Couldn't open encoder instance for input file %s\n",
audioFileName);
fclose(f);
return 1;
}
tmpbuf = (int16_t *) malloc(samplesInput);
bitbuf = (unsigned char *) malloc(maxBytesOutput * sizeof(unsigned char));
/* put the options in the configuration struct */
myFormat = faacEncGetCurrentConfiguration(hEncoder);
myFormat->aacObjectType = LOW;
myFormat->mpegVersion = MPEG2;
myFormat->useTns = 0;
myFormat->bitRate = 64000;
myFormat->bandWidth = rawRate/2;
myFormat->outputFormat = ADTS_STREAM;
myFormat->inputFormat = FAAC_INPUT_16BIT;
if (!faacEncSetConfiguration(hEncoder, myFormat))
{
fprintf(stderr, "Unsupported output format!\n");
return 1;
}
outfile = fopen(aacFileName, "wb");
if (!outfile)
{
fprintf(stderr, "Couldn't create output file %s\n", aacFileName);
return 1;
}
/* encoding loop */
while (1)
{
int bytesWritten, samplesRead;
samplesRead = fread(tmpbuf, 2, samplesInput, f);
if(samplesRead<=0)
{
perror(":");
break;
}
/* call the actual encoding routine */
bytesWritten = faacEncEncode(hEncoder,
(int32_t *) tmpbuf,
samplesRead, bitbuf, maxBytesOutput);
if (bytesWritten > 0)
{
fwrite(bitbuf, 1, bytesWritten, outfile);
}
}
// free(tmpbuf);
// free(bitbuf);
fclose(f);
fclose(outfile);
faacEncClose(hEncoder);
return 0;
}