1 /* ------------------------------------------------------------------
2  * Copyright (C) 2011 Martin Storsjo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *	  http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 
19 #include <stdio.h>
20 #include <stdint.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <common/include/voAAC.h>
24 #include <common/include/cmnMemory.h>
25 #include "wavreader.h"
26 
usage(const char * name)27 void usage(const char* name) {
28 	fprintf(stderr, "%s [-r bitrate] in.wav out.aac\n", name);
29 }
30 
main(int argc,char * argv[])31 int main(int argc, char *argv[]) {
32 	int bitrate = 64000;
33 	int ch;
34 	const char *infile, *outfile;
35 	FILE *out;
36 	void *wav;
37 	int format, sampleRate, channels, bitsPerSample;
38 	int inputSize;
39 	uint8_t* inputBuf;
40 	int16_t* convertBuf;
41 	VO_AUDIO_CODECAPI codec_api = { 0 };
42 	VO_HANDLE handle = 0;
43 	VO_MEM_OPERATOR mem_operator = { 0 };
44 	VO_CODEC_INIT_USERDATA user_data;
45 	AACENC_PARAM params = { 0 };
46 	while ((ch = getopt(argc, argv, "r:")) != -1) {
47 		switch (ch) {
48 		case 'r':
49 			bitrate = atoi(optarg);
50 			break;
51 		case '?':
52 		default:
53 			usage(argv[0]);
54 			return 1;
55 		}
56 	}
57 	if (argc - optind < 2) {
58 		usage(argv[0]);
59 		return 1;
60 	}
61 	infile = argv[optind];
62 	outfile = argv[optind + 1];
63 
64 	wav = wav_read_open(infile);
65 	if (!wav) {
66 		fprintf(stderr, "Unable to open wav file %s\n", infile);
67 		return 1;
68 	}
69 	if (!wav_get_header(wav, &format, &channels, &sampleRate, &bitsPerSample, NULL)) {
70 		fprintf(stderr, "Bad wav file %s\n", infile);
71 		return 1;
72 	}
73 	if (format != 1) {
74 		fprintf(stderr, "Unsupported WAV format %d\n", format);
75 		return 1;
76 	}
77 	if (bitsPerSample != 16) {
78 		fprintf(stderr, "Unsupported WAV sample depth %d\n", bitsPerSample);
79 		return 1;
80 	}
81 	inputSize = channels*2*1024;
82 	inputBuf = (uint8_t*) malloc(inputSize);
83 	convertBuf = (int16_t*) malloc(inputSize);
84 
85 	voGetAACEncAPI(&codec_api);
86 
87 	mem_operator.Alloc = cmnMemAlloc;
88 	mem_operator.Copy = cmnMemCopy;
89 	mem_operator.Free = cmnMemFree;
90 	mem_operator.Set = cmnMemSet;
91 	mem_operator.Check = cmnMemCheck;
92 	user_data.memflag = VO_IMF_USERMEMOPERATOR;
93 	user_data.memData = &mem_operator;
94 	codec_api.Init(&handle, VO_AUDIO_CodingAAC, &user_data);
95 
96 	params.sampleRate = sampleRate;
97 	params.bitRate = bitrate;
98 	params.nChannels = channels;
99 	params.adtsUsed = 1;
100 	if (codec_api.SetParam(handle, VO_PID_AAC_ENCPARAM, &params) != VO_ERR_NONE) {
101 		fprintf(stderr, "Unable to set encoding parameters\n");
102 		return 1;
103 	}
104 
105 	out = fopen(outfile, "wb");
106 	if (!out) {
107 		perror(outfile);
108 		return 1;
109 	}
110 
111 	while (1) {
112 		VO_CODECBUFFER input = { 0 }, output = { 0 };
113 		VO_AUDIO_OUTPUTINFO output_info = { 0 };
114 		int read, i;
115 		uint8_t outbuf[20480];
116 
117 		read = wav_read_data(wav, inputBuf, inputSize);
118 		if (read < inputSize)
119 			break;
120 		for (i = 0; i < read/2; i++) {
121 			const uint8_t* in = &inputBuf[2*i];
122 			convertBuf[i] = in[0] | (in[1] << 8);
123 		}
124 		input.Buffer = (uint8_t*) convertBuf;
125 		input.Length = read;
126 		codec_api.SetInputData(handle, &input);
127 
128 		output.Buffer = outbuf;
129 		output.Length = sizeof(outbuf);
130 		if (codec_api.GetOutputData(handle, &output, &output_info) != VO_ERR_NONE) {
131 			fprintf(stderr, "Unable to encode frame\n");
132 			return 1;
133 		}
134 		fwrite(outbuf, 1, output.Length, out);
135 	}
136 	free(inputBuf);
137 	free(convertBuf);
138 	fclose(out);
139 	codec_api.Uninit(handle);
140 	wav_read_close(wav);
141 
142 	return 0;
143 }
144 
145