aac-enc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. #include <stdio.h>
  19. #include <stdint.h>
  20. #if defined(_MSC_VER)
  21. #include <getopt.h>
  22. #else
  23. #include <unistd.h>
  24. #endif
  25. #include <stdlib.h>
  26. #include "libAACenc/include/aacenc_lib.h"
  27. #include "wavreader.h"
  28. void usage(const char* name) {
  29. fprintf(stderr, "%s [-r bitrate] [-t aot] [-a afterburner] [-s sbr] [-v vbr] in.wav out.aac\n", name);
  30. fprintf(stderr, "Supported AOTs:\n");
  31. fprintf(stderr, "\t2\tAAC-LC\n");
  32. fprintf(stderr, "\t5\tHE-AAC\n");
  33. fprintf(stderr, "\t29\tHE-AAC v2\n");
  34. fprintf(stderr, "\t23\tAAC-LD\n");
  35. fprintf(stderr, "\t39\tAAC-ELD\n");
  36. }
  37. int main(int argc, char *argv[]) {
  38. int bitrate = 64000;
  39. int ch;
  40. const char *infile, *outfile;
  41. FILE *out;
  42. void *wav;
  43. int format, sample_rate, channels, bits_per_sample;
  44. int input_size;
  45. uint8_t* input_buf;
  46. int16_t* convert_buf;
  47. int aot = 2;
  48. int afterburner = 1;
  49. int eld_sbr = 0;
  50. int vbr = 0;
  51. HANDLE_AACENCODER handle;
  52. CHANNEL_MODE mode;
  53. AACENC_InfoStruct info = { 0 };
  54. while ((ch = getopt(argc, argv, "r:t:a:s:v:")) != -1) {
  55. switch (ch) {
  56. case 'r':
  57. bitrate = atoi(optarg);
  58. break;
  59. case 't':
  60. aot = atoi(optarg);
  61. break;
  62. case 'a':
  63. afterburner = atoi(optarg);
  64. break;
  65. case 's':
  66. eld_sbr = atoi(optarg);
  67. break;
  68. case 'v':
  69. vbr = atoi(optarg);
  70. break;
  71. case '?':
  72. default:
  73. usage(argv[0]);
  74. return 1;
  75. }
  76. }
  77. if (argc - optind < 2) {
  78. usage(argv[0]);
  79. return 1;
  80. }
  81. infile = argv[optind];
  82. outfile = argv[optind + 1];
  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. out = fopen(outfile, "wb");
  165. if (!out) {
  166. perror(outfile);
  167. return 1;
  168. }
  169. input_size = channels*2*info.frameLength;
  170. input_buf = (uint8_t*) malloc(input_size);
  171. convert_buf = (int16_t*) malloc(input_size);
  172. while (1) {
  173. AACENC_BufDesc in_buf = { 0 }, out_buf = { 0 };
  174. AACENC_InArgs in_args = { 0 };
  175. AACENC_OutArgs out_args = { 0 };
  176. int in_identifier = IN_AUDIO_DATA;
  177. int in_size, in_elem_size;
  178. int out_identifier = OUT_BITSTREAM_DATA;
  179. int out_size, out_elem_size;
  180. int read, i;
  181. void *in_ptr, *out_ptr;
  182. uint8_t outbuf[20480];
  183. AACENC_ERROR err;
  184. read = wav_read_data(wav, input_buf, input_size);
  185. for (i = 0; i < read/2; i++) {
  186. const uint8_t* in = &input_buf[2*i];
  187. convert_buf[i] = in[0] | (in[1] << 8);
  188. }
  189. if (read <= 0) {
  190. in_args.numInSamples = -1;
  191. } else {
  192. in_ptr = convert_buf;
  193. in_size = read;
  194. in_elem_size = 2;
  195. in_args.numInSamples = read/2;
  196. in_buf.numBufs = 1;
  197. in_buf.bufs = &in_ptr;
  198. in_buf.bufferIdentifiers = &in_identifier;
  199. in_buf.bufSizes = &in_size;
  200. in_buf.bufElSizes = &in_elem_size;
  201. }
  202. out_ptr = outbuf;
  203. out_size = sizeof(outbuf);
  204. out_elem_size = 1;
  205. out_buf.numBufs = 1;
  206. out_buf.bufs = &out_ptr;
  207. out_buf.bufferIdentifiers = &out_identifier;
  208. out_buf.bufSizes = &out_size;
  209. out_buf.bufElSizes = &out_elem_size;
  210. if ((err = aacEncEncode(handle, &in_buf, &out_buf, &in_args, &out_args)) != AACENC_OK) {
  211. if (err == AACENC_ENCODE_EOF)
  212. break;
  213. fprintf(stderr, "Encoding failed\n");
  214. return 1;
  215. }
  216. if (out_args.numOutBytes == 0)
  217. continue;
  218. fwrite(outbuf, 1, out_args.numOutBytes, out);
  219. }
  220. free(input_buf);
  221. free(convert_buf);
  222. fclose(out);
  223. wav_read_close(wav);
  224. aacEncClose(&handle);
  225. return 0;
  226. }