ref: bde1d1cc6aab54c42dd2050b5dc3931cd3cd9a88
dir: /frontend/aacenc.c/
/*
* 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 <u.h>
#include <libc.h>
#include <bio.h>
#include <faac.h>
#define min(a,b) ((a)<=(b)?(a):(b))
static void
usage(void)
{
fprint(2, "usage: %s [-r RATE] [-c CHAN] [-t low|main|ltp] [-B BITRATE]\n", argv0);
exits("usage");
}
void
main(int argc, char **argv)
{
int nch, srate, type, brate, sz, n, r;
ulong insamples, outsz, insz;
faacEncConfigurationPtr fmt;
faacEncHandle e;
s16int *pcm;
u8int *obuf;
Biobuf out;
Biobuf in;
char *s;
brate = 0;
srate = 44100;
nch = 2;
type = LOW;
ARGBEGIN{
case 'B':
if((brate = atoi(EARGF(usage()))) < 0)
sysfatal("invalid bitrate %d", brate);
break;
case 'c':
if((nch = atoi(EARGF(usage()))) < 1 || nch > 64)
sysfatal("invalid number of channels %d", nch);
break;
case 'r':
if((srate = atoi(EARGF(usage()))) < 1)
sysfatal("invalid samplerate %d", srate);
break;
case 't':
s = EARGF(usage());
if(cistrcmp(s, "low") == 0)
type = LOW;
else if(cistrcmp(s, "main") == 0)
type = MAIN;
else if(cistrcmp(s, "ltp") == 0)
type = LTP;
else
sysfatal("invalid type %s", s);
break;
default:
usage();
}ARGEND
if(argc != 0)
usage();
if(Binit(&in, 0, OREAD) != 0 || Binit(&out, 1, OWRITE) != 0)
sysfatal("io init failed");
setfcr(getfcr() & ~(FPINVAL|FPOVFL));
if((e = faacEncOpen(srate, nch, &insamples, &outsz)) == nil)
sysfatal("faacEncOpen");
insz = insamples * sizeof(*pcm);
if((pcm = malloc(insz)) == nil)
sysfatal("memory");
if((obuf = malloc(outsz)) == nil)
sysfatal("memory");
fmt = faacEncGetCurrentConfiguration(e);
fmt->inputFormat = FAAC_INPUT_16BIT;
fmt->mpegVersion = MPEG2;
fmt->outputFormat = ADTS_STREAM;
fmt->aacObjectType = type;
if(brate > 0)
fmt->bitRate = brate / nch;
if(!faacEncSetConfiguration(e, fmt))
sysfatal("invalid encoder configuration");
for(;;){
for(n = 0; n == 0 || (n & (sizeof(*pcm)-1)) != 0; n += r){
if((r = Bread(&in, pcm+n, insz-n)) <= 0)
break;
}
if(n == 0)
break;
if((sz = faacEncEncode(e, pcm, n/sizeof(*pcm), obuf, outsz)) < 0)
sysfatal("faacEncEncode");
Bwrite(&out, obuf, sz);
}
Bflush(&out);
exits(nil);
}