shithub: aacenc

Download patch

ref: f19cd8500dd6759b0e0614ad904dc788106326d9
parent: 9a8b43fd079b71c647738612b6875a3c767dbf8f
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Thu Jul 15 08:46:04 EDT 2021

Plan 9 port

--- /dev/null
+++ b/frontend/aacenc.c
@@ -1,0 +1,120 @@
+/*
+ * 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(strcmp(s, "low") == 0)
+			type = LOW;
+		else if(strcmp(s, "main") == 0)
+			type = MAIN;
+		else if(strcmp(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);
+}
--- /dev/null
+++ b/frontend/mkfile
@@ -1,0 +1,12 @@
+</$objtype/mkfile
+
+TARG=\
+	aacenc\
+
+CFLAGS=$CFLAGS -I../include -p -D__plan9__
+BIN=/$objtype/bin/audio
+LIB=../libfaac/libfaac.$objtype.a
+
+default:V: all
+
+</sys/src/cmd/mkmany
--- a/include/faac.h
+++ b/include/faac.h
@@ -46,7 +46,6 @@
 }
 psymodellist_t;
 
-#include <stdint.h>
 #include "faaccfg.h"
 
 
@@ -80,7 +79,7 @@
 					  unsigned long *pSizeOfDecoderSpecificInfo);
 
 
-int FAACAPI faacEncEncode(faacEncHandle hEncoder, int32_t * inputBuffer, unsigned int samplesInput,
+int FAACAPI faacEncEncode(faacEncHandle hEncoder, void * inputBuffer, unsigned int samplesInput,
 			 unsigned char *outputBuffer,
 			 unsigned int bufferSize);
 
--- a/libfaac/bitstream.c
+++ b/libfaac/bitstream.c
@@ -24,15 +24,11 @@
 Copyright (c) 1997.
 **********************************************************************/
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
+#include "util.h"
 #include "coder.h"
 #include "channels.h"
 #include "huff2.h"
 #include "bitstream.h"
-#include "util.h"
 
 static int CountBitstream(faacEncStruct* hEncoder,
                           CoderInfo *coderInfo,
--- a/libfaac/blockswitch.c
+++ b/libfaac/blockswitch.c
@@ -18,10 +18,8 @@
  *
  * $Id: psychkni.c,v 1.19 2012/03/01 18:34:17 knik Exp $
  */
-#include <stdio.h>
-#include <stdlib.h>
-#include <math.h>
 
+#include "util.h"
 #include "blockswitch.h"
 #include "coder.h"
 #include "fft.h"
--- a/libfaac/blockswitch.h
+++ b/libfaac/blockswitch.h
@@ -81,4 +81,4 @@
 }
 #endif /* __cplusplus */
 
-#endif /* PSYCH_H */
\ No newline at end of file
+#endif /* PSYCH_H */
--- a/libfaac/fft.c
+++ b/libfaac/fft.c
@@ -19,12 +19,8 @@
  *
  */
 
-#include <math.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-#include "fft.h"
 #include "util.h"
+#include "fft.h"
 
 #define MAXLOGM 9
 #define MAXLOGR 8
--- a/libfaac/filtbank.c
+++ b/libfaac/filtbank.c
@@ -31,15 +31,11 @@
  *
  */
 
-#include <math.h>
-#include <stdio.h>
-#include <stdlib.h>
-
+#include "util.h"
 #include "coder.h"
 #include "filtbank.h"
 #include "frame.h"
 #include "fft.h"
-#include "util.h"
 
 #define  TWOPI       2*M_PI
 
--- a/libfaac/frame.c
+++ b/libfaac/frame.c
@@ -18,17 +18,12 @@
  *
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-#include <string.h>
-
+#include "util.h"
 #include "frame.h"
 #include "coder.h"
 #include "channels.h"
 #include "bitstream.h"
 #include "filtbank.h"
-#include "util.h"
 #include "tns.h"
 #include "stereo.h"
 
@@ -359,7 +354,7 @@
 }
 
 int FAACAPI faacEncEncode(faacEncHandle hpEncoder,
-                          int32_t *inputBuffer,
+                          void *inputBuffer,
                           unsigned int samplesInput,
                           unsigned char *outputBuffer,
                           unsigned int bufferSize
--- a/libfaac/huff2.c
+++ b/libfaac/huff2.c
@@ -17,8 +17,7 @@
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
 
-#include <stdio.h>
-#include <stdlib.h>
+#include "util.h"
 #include "coder.h"
 #include "huffdata.h"
 #include "huff2.h"
--- a/libfaac/huffdata.c
+++ b/libfaac/huffdata.c
@@ -15,6 +15,7 @@
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
 
+#include "util.h"
 #include "huffdata.h"
 
 hcode16_t book01[81] = {
--- a/libfaac/huffdata.h
+++ b/libfaac/huffdata.h
@@ -15,8 +15,6 @@
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
 
-#include <stdint.h>
-
 typedef struct {
     const uint16_t len;
     const uint16_t data;
--- a/libfaac/kiss_fft/kiss_fft.h
+++ b/libfaac/kiss_fft/kiss_fft.h
@@ -1,10 +1,14 @@
 #ifndef KISS_FFT_H
 #define KISS_FFT_H
 
+#ifdef __plan9__
+#include "util.h"
+#else
 #include <stdlib.h>
 #include <stdio.h>
 #include <math.h>
 #include <memory.h>
+#endif
 
 #ifdef __cplusplus
 extern "C" {
--- /dev/null
+++ b/libfaac/mkfile
@@ -1,0 +1,44 @@
+</$objtype/mkfile
+
+CFLAGS=$CFLAGS -p -I../include -D__plan9__ -DPACKAGE_VERSION="1.30"
+LIB=libfaac.$objtype.a
+TARG=aac
+
+HFILES=\
+	bitstream.h\
+	blockswitch.h\
+	channels.h\
+	coder.h\
+	fft.h\
+	filtbank.h\
+	frame.h\
+	huff2.h\
+	huffdata.h\
+	quantize.h\
+	stereo.h\
+	tns.h\
+	util.h\
+	kiss_fft/_kiss_fft_guts.h\
+	kiss_fft/kiss_fft.h\
+	kiss_fft/kiss_fftr.h\
+
+OFILES=\
+	bitstream.$O\
+	blockswitch.$O\
+	channels.$O\
+	fft.$O\
+	filtbank.$O\
+	frame.$O\
+	huff2.$O\
+	huffdata.$O\
+	quantize.$O\
+	stereo.$O\
+	tns.$O\
+	util.$O\
+	kiss_fft.$O\
+	kiss_fftr.$O\
+
+</sys/src/cmd/mklib
+
+(kiss_.*)\.$O:R: kiss_fft/\1.c
+	$CC $CFLAGS -I. -Ikiss_fft -o $target kiss_fft/$stem1.c
--- a/libfaac/quantize.c
+++ b/libfaac/quantize.c
@@ -18,8 +18,7 @@
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
 
-#include <math.h>
-#include <stdio.h>
+#include "util.h"
 #include "quantize.h"
 #include "huff2.h"
 
--- a/libfaac/stereo.c
+++ b/libfaac/stereo.c
@@ -17,7 +17,7 @@
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
 
-#include <math.h>
+#include "util.h"
 #include "stereo.h"
 #include "huff2.h"
 
--- a/libfaac/tns.c
+++ b/libfaac/tns.c
@@ -27,12 +27,11 @@
  * $Id: tns.c,v 1.11 2012/03/01 18:34:17 knik Exp $
  */
 
-#include <math.h>
+#include "util.h"
 #include "frame.h"
 #include "coder.h"
 #include "bitstream.h"
 #include "tns.h"
-#include "util.h"
 
 /***********************************************/
 /* TNS Profile/Frequency Dependent Parameters  */
--- a/libfaac/util.c
+++ b/libfaac/util.c
@@ -18,8 +18,6 @@
  *
  */
 
-#include <math.h>
-
 #include "util.h"
 #include "coder.h"  // FRAME_LEN
 
--- a/libfaac/util.h
+++ b/libfaac/util.h
@@ -26,8 +26,32 @@
 extern "C" {
 #endif /* __cplusplus */
 
+#ifdef __plan9__
+#include <u.h>
+#include <libc.h>
+#include <stdio.h>
+static long
+lrint(double d)
+{
+	long long l;
+	*((double*)&l) = d + 6755399441055744.0;
+	return l;
+}
+#define M_SQRT2 1.41421356237309504880
+#define exit(x) do{ exits(x ? "error" : nil); }while(0)
+typedef u16int uint16_t;
+typedef u32int uint32_t;
+typedef s32int int32_t;
+typedef usize size_t;
+#else
+#include <stdint.h>
 #include <stdlib.h>
+#include <math.h>
 #include <memory.h>
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#endif
 
 #ifndef max
 #define max(a, b) (((a) > (b)) ? (a) : (b))
--- /dev/null
+++ b/mkfile
@@ -1,0 +1,5 @@
+%:V:
+	cd libfaac && mk $target && cd ../
+	cd frontend && mk $target
+
+default:V: all