1 /* ------------------------------------------------------------------
2 * Copyright (C) 2011 Martin Storsjo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
23 #include "libAACenc/include/aacenc_lib.h"
24 #include "wavreader.h"
26 void usage(const char* name
) {
27 fprintf(stderr
, "%s [-r bitrate] [-t aot] [-a afterburner] [-s sbr] [-v vbr] in.wav out.aac\n", name
);
28 fprintf(stderr
, "Supported AOTs:\n");
29 fprintf(stderr
, "\t2\tAAC-LC\n");
30 fprintf(stderr
, "\t5\tHE-AAC\n");
31 fprintf(stderr
, "\t29\tHE-AAC v2\n");
32 fprintf(stderr
, "\t23\tAAC-LD\n");
33 fprintf(stderr
, "\t39\tAAC-ELD\n");
36 int main(int argc
, char *argv
[]) {
39 const char *infile
, *outfile
;
42 int format
, sample_rate
, channels
, bits_per_sample
;
50 HANDLE_AACENCODER handle
;
52 AACENC_InfoStruct info
= { 0 };
53 while ((ch
= getopt(argc
, argv
, "r:t:a:s:v:")) != -1) {
56 bitrate
= atoi(optarg
);
62 afterburner
= atoi(optarg
);
65 eld_sbr
= atoi(optarg
);
76 if (argc
- optind
< 2) {
80 infile
= argv
[optind
];
81 outfile
= argv
[optind
+ 1];
83 wav
= wav_read_open(infile
);
85 fprintf(stderr
, "Unable to open wav file %s\n", infile
);
88 if (!wav_get_header(wav
, &format
, &channels
, &sample_rate
, &bits_per_sample
, NULL
)) {
89 fprintf(stderr
, "Bad wav file %s\n", infile
);
93 fprintf(stderr
, "Unsupported WAV format %d\n", format
);
96 if (bits_per_sample
!= 16) {
97 fprintf(stderr
, "Unsupported WAV sample depth %d\n", bits_per_sample
);
101 case 1: mode
= MODE_1
; break;
102 case 2: mode
= MODE_2
; break;
103 case 3: mode
= MODE_1_2
; break;
104 case 4: mode
= MODE_1_2_1
; break;
105 case 5: mode
= MODE_1_2_2
; break;
106 case 6: mode
= MODE_1_2_2_1
; break;
108 fprintf(stderr
, "Unsupported WAV channels %d\n", channels
);
111 if (aacEncOpen(&handle
, 0, channels
) != AACENC_OK
) {
112 fprintf(stderr
, "Unable to open encoder\n");
115 if (aacEncoder_SetParam(handle
, AACENC_AOT
, aot
) != AACENC_OK
) {
116 fprintf(stderr
, "Unable to set the AOT\n");
119 if (aot
== 39 && eld_sbr
) {
120 if (aacEncoder_SetParam(handle
, AACENC_SBR_MODE
, 1) != AACENC_OK
) {
121 fprintf(stderr
, "Unable to set SBR mode for ELD\n");
125 if (aacEncoder_SetParam(handle
, AACENC_SAMPLERATE
, sample_rate
) != AACENC_OK
) {
126 fprintf(stderr
, "Unable to set the AOT\n");
129 if (aacEncoder_SetParam(handle
, AACENC_CHANNELMODE
, mode
) != AACENC_OK
) {
130 fprintf(stderr
, "Unable to set the channel mode\n");
133 if (aacEncoder_SetParam(handle
, AACENC_CHANNELORDER
, 1) != AACENC_OK
) {
134 fprintf(stderr
, "Unable to set the wav channel order\n");
138 if (aacEncoder_SetParam(handle
, AACENC_BITRATEMODE
, vbr
) != AACENC_OK
) {
139 fprintf(stderr
, "Unable to set the VBR bitrate mode\n");
143 if (aacEncoder_SetParam(handle
, AACENC_BITRATE
, bitrate
) != AACENC_OK
) {
144 fprintf(stderr
, "Unable to set the bitrate\n");
148 if (aacEncoder_SetParam(handle
, AACENC_TRANSMUX
, 2) != AACENC_OK
) {
149 fprintf(stderr
, "Unable to set the ADTS transmux\n");
152 if (aacEncoder_SetParam(handle
, AACENC_AFTERBURNER
, afterburner
) != AACENC_OK
) {
153 fprintf(stderr
, "Unable to set the afterburner mode\n");
156 if (aacEncEncode(handle
, NULL
, NULL
, NULL
, NULL
) != AACENC_OK
) {
157 fprintf(stderr
, "Unable to initialize the encoder\n");
160 if (aacEncInfo(handle
, &info
) != AACENC_OK
) {
161 fprintf(stderr
, "Unable to get the encoder info\n");
165 out
= fopen(outfile
, "wb");
171 input_size
= channels
*2*info
.frameLength
;
172 input_buf
= (uint8_t*) malloc(input_size
);
173 convert_buf
= (int16_t*) malloc(input_size
);
176 AACENC_BufDesc in_buf
= { 0 }, out_buf
= { 0 };
177 AACENC_InArgs in_args
= { 0 };
178 AACENC_OutArgs out_args
= { 0 };
179 int in_identifier
= IN_AUDIO_DATA
;
180 int in_size
, in_elem_size
;
181 int out_identifier
= OUT_BITSTREAM_DATA
;
182 int out_size
, out_elem_size
;
184 void *in_ptr
, *out_ptr
;
185 uint8_t outbuf
[20480];
188 read
= wav_read_data(wav
, input_buf
, input_size
);
189 for (i
= 0; i
< read
/2; i
++) {
190 const uint8_t* in
= &input_buf
[2*i
];
191 convert_buf
[i
] = in
[0] | (in
[1] << 8);
194 in_args
.numInSamples
= -1;
196 in_ptr
= convert_buf
;
200 in_args
.numInSamples
= read
/2;
202 in_buf
.bufs
= &in_ptr
;
203 in_buf
.bufferIdentifiers
= &in_identifier
;
204 in_buf
.bufSizes
= &in_size
;
205 in_buf
.bufElSizes
= &in_elem_size
;
208 out_size
= sizeof(outbuf
);
211 out_buf
.bufs
= &out_ptr
;
212 out_buf
.bufferIdentifiers
= &out_identifier
;
213 out_buf
.bufSizes
= &out_size
;
214 out_buf
.bufElSizes
= &out_elem_size
;
216 if ((err
= aacEncEncode(handle
, &in_buf
, &out_buf
, &in_args
, &out_args
)) != AACENC_OK
) {
217 if (err
== AACENC_ENCODE_EOF
)
219 fprintf(stderr
, "Encoding failed\n");
222 if (out_args
.numOutBytes
== 0)
224 fwrite(outbuf
, 1, out_args
.numOutBytes
, out
);
230 aacEncClose(&handle
);