| 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 "libAACenc/include/aacenc_lib.h" |
| 24 | #include "wavreader.h" |
| 25 | |
| 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"); |
| 34 | } |
| 35 | |
| 36 | int main(int argc, char *argv[]) { |
| 37 | int bitrate = 64000; |
| 38 | int ch; |
| 39 | const char *infile, *outfile; |
| 40 | FILE *out; |
| 41 | void *wav; |
| 42 | int format, sample_rate, channels, bits_per_sample; |
| 43 | int input_size; |
| 44 | uint8_t* input_buf; |
| 45 | int16_t* convert_buf; |
| 46 | int aot = 2; |
| 47 | int afterburner = 1; |
| 48 | int eld_sbr = 0; |
| 49 | int vbr = 0; |
| 50 | HANDLE_AACENCODER handle; |
| 51 | CHANNEL_MODE mode; |
| 52 | AACENC_InfoStruct info = { 0 }; |
| 53 | while ((ch = getopt(argc, argv, "r:t:a:s:v:")) != -1) { |
| 54 | switch (ch) { |
| 55 | case 'r': |
| 56 | bitrate = atoi(optarg); |
| 57 | break; |
| 58 | case 't': |
| 59 | aot = atoi(optarg); |
| 60 | break; |
| 61 | case 'a': |
| 62 | afterburner = atoi(optarg); |
| 63 | break; |
| 64 | case 's': |
| 65 | eld_sbr = atoi(optarg); |
| 66 | break; |
| 67 | case 'v': |
| 68 | vbr = atoi(optarg); |
| 69 | break; |
| 70 | case '?': |
| 71 | default: |
| 72 | usage(argv[0]); |
| 73 | return 1; |
| 74 | } |
| 75 | } |
| 76 | if (argc - optind < 2) { |
| 77 | usage(argv[0]); |
| 78 | return 1; |
| 79 | } |
| 80 | infile = argv[optind]; |
| 81 | outfile = argv[optind + 1]; |
| 82 | |
| 83 | wav = wav_read_open(infile); |
| 84 | if (!wav) { |
| 85 | fprintf(stderr, "Unable to open wav file %s\n", infile); |
| 86 | return 1; |
| 87 | } |
| 88 | if (!wav_get_header(wav, &format, &channels, &sample_rate, &bits_per_sample, NULL)) { |
| 89 | fprintf(stderr, "Bad wav file %s\n", infile); |
| 90 | return 1; |
| 91 | } |
| 92 | if (format != 1) { |
| 93 | fprintf(stderr, "Unsupported WAV format %d\n", format); |
| 94 | return 1; |
| 95 | } |
| 96 | if (bits_per_sample != 16) { |
| 97 | fprintf(stderr, "Unsupported WAV sample depth %d\n", bits_per_sample); |
| 98 | return 1; |
| 99 | } |
| 100 | switch (channels) { |
| 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; |
| 107 | default: |
| 108 | fprintf(stderr, "Unsupported WAV channels %d\n", channels); |
| 109 | return 1; |
| 110 | } |
| 111 | if (aacEncOpen(&handle, 0, channels) != AACENC_OK) { |
| 112 | fprintf(stderr, "Unable to open encoder\n"); |
| 113 | return 1; |
| 114 | } |
| 115 | if (aacEncoder_SetParam(handle, AACENC_AOT, aot) != AACENC_OK) { |
| 116 | fprintf(stderr, "Unable to set the AOT\n"); |
| 117 | return 1; |
| 118 | } |
| 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"); |
| 122 | return 1; |
| 123 | } |
| 124 | } |
| 125 | if (aacEncoder_SetParam(handle, AACENC_SAMPLERATE, sample_rate) != AACENC_OK) { |
| 126 | fprintf(stderr, "Unable to set the AOT\n"); |
| 127 | return 1; |
| 128 | } |
| 129 | if (aacEncoder_SetParam(handle, AACENC_CHANNELMODE, mode) != AACENC_OK) { |
| 130 | fprintf(stderr, "Unable to set the channel mode\n"); |
| 131 | return 1; |
| 132 | } |
| 133 | if (aacEncoder_SetParam(handle, AACENC_CHANNELORDER, 1) != AACENC_OK) { |
| 134 | fprintf(stderr, "Unable to set the wav channel order\n"); |
| 135 | return 1; |
| 136 | } |
| 137 | if (vbr) { |
| 138 | if (aacEncoder_SetParam(handle, AACENC_BITRATEMODE, vbr) != AACENC_OK) { |
| 139 | fprintf(stderr, "Unable to set the VBR bitrate mode\n"); |
| 140 | return 1; |
| 141 | } |
| 142 | } else { |
| 143 | if (aacEncoder_SetParam(handle, AACENC_BITRATE, bitrate) != AACENC_OK) { |
| 144 | fprintf(stderr, "Unable to set the bitrate\n"); |
| 145 | return 1; |
| 146 | } |
| 147 | } |
| 148 | if (aacEncoder_SetParam(handle, AACENC_TRANSMUX, 2) != AACENC_OK) { |
| 149 | fprintf(stderr, "Unable to set the ADTS transmux\n"); |
| 150 | return 1; |
| 151 | } |
| 152 | if (aacEncoder_SetParam(handle, AACENC_AFTERBURNER, afterburner) != AACENC_OK) { |
| 153 | fprintf(stderr, "Unable to set the afterburner mode\n"); |
| 154 | return 1; |
| 155 | } |
| 156 | if (aacEncEncode(handle, NULL, NULL, NULL, NULL) != AACENC_OK) { |
| 157 | fprintf(stderr, "Unable to initialize the encoder\n"); |
| 158 | return 1; |
| 159 | } |
| 160 | if (aacEncInfo(handle, &info) != AACENC_OK) { |
| 161 | fprintf(stderr, "Unable to get the encoder info\n"); |
| 162 | return 1; |
| 163 | } |
| 164 | |
| 165 | out = fopen(outfile, "wb"); |
| 166 | if (!out) { |
| 167 | perror(outfile); |
| 168 | return 1; |
| 169 | } |
| 170 | |
| 171 | input_size = channels*2*info.frameLength; |
| 172 | input_buf = (uint8_t*) malloc(input_size); |
| 173 | convert_buf = (int16_t*) malloc(input_size); |
| 174 | |
| 175 | while (1) { |
| 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; |
| 183 | int read, i; |
| 184 | void *in_ptr, *out_ptr; |
| 185 | uint8_t outbuf[20480]; |
| 186 | AACENC_ERROR err; |
| 187 | |
| 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); |
| 192 | } |
| 193 | if (read <= 0) { |
| 194 | in_args.numInSamples = -1; |
| 195 | } else { |
| 196 | in_ptr = convert_buf; |
| 197 | in_size = read; |
| 198 | in_elem_size = 2; |
| 199 | |
| 200 | in_args.numInSamples = read/2; |
| 201 | in_buf.numBufs = 1; |
| 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; |
| 206 | } |
| 207 | out_ptr = outbuf; |
| 208 | out_size = sizeof(outbuf); |
| 209 | out_elem_size = 1; |
| 210 | out_buf.numBufs = 1; |
| 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; |
| 215 | |
| 216 | if ((err = aacEncEncode(handle, &in_buf, &out_buf, &in_args, &out_args)) != AACENC_OK) { |
| 217 | if (err == AACENC_ENCODE_EOF) |
| 218 | break; |
| 219 | fprintf(stderr, "Encoding failed\n"); |
| 220 | return 1; |
| 221 | } |
| 222 | if (out_args.numOutBytes == 0) |
| 223 | continue; |
| 224 | fwrite(outbuf, 1, out_args.numOutBytes, out); |
| 225 | } |
| 226 | free(input_buf); |
| 227 | free(convert_buf); |
| 228 | fclose(out); |
| 229 | wav_read_close(wav); |
| 230 | aacEncClose(&handle); |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |