48e2f01c |
1 | /* |
2 | * Copyright (C) 2013 nu774 |
3 | * For conditions of distribution and use, see copyright notice in COPYING |
4 | */ |
5 | #if HAVE_CONFIG_H |
6 | # include "config.h" |
7 | #endif |
8 | #if HAVE_STDINT_H |
9 | # include <stdint.h> |
10 | #endif |
11 | #include <stdio.h> |
12 | #include <stdlib.h> |
13 | #include <string.h> |
68543176 |
14 | #include <ctype.h> |
48e2f01c |
15 | #include <locale.h> |
16 | #include <errno.h> |
7222b0b5 |
17 | #include <sys/stat.h> |
48e2f01c |
18 | #include <getopt.h> |
7222b0b5 |
19 | #if HAVE_UNISTD_H |
20 | #include <unistd.h> |
21 | #endif |
22 | #ifdef _WIN32 |
23 | #include <io.h> |
24 | #endif |
48e2f01c |
25 | #include "compat.h" |
26 | #include "wav_reader.h" |
27 | #include "aacenc.h" |
28 | #include "m4af.h" |
29 | #include "progress.h" |
30 | #include "version.h" |
31 | |
32 | #define PROGNAME "fdkaac" |
33 | |
34 | static |
35 | int read_callback(void *cookie, void *data, uint32_t size) |
36 | { |
5e1168a4 |
37 | size_t rc = fread(data, 1, size, (FILE*)cookie); |
38 | return ferror((FILE*)cookie) ? -1 : (int)rc; |
48e2f01c |
39 | } |
40 | |
41 | static |
42 | int write_callback(void *cookie, const void *data, uint32_t size) |
43 | { |
5e1168a4 |
44 | size_t rc = fwrite(data, 1, size, (FILE*)cookie); |
45 | return ferror((FILE*)cookie) ? -1 : (int)rc; |
48e2f01c |
46 | } |
47 | |
48 | static |
49 | int seek_callback(void *cookie, int64_t off, int whence) |
50 | { |
51 | return fseeko((FILE*)cookie, off, whence); |
52 | } |
53 | |
54 | static |
55 | int64_t tell_callback(void *cookie) |
56 | { |
57 | return ftello((FILE*)cookie); |
58 | } |
59 | |
60 | static |
61 | void usage(void) |
62 | { |
63 | printf( |
64 | PROGNAME " %s\n" |
65 | "Usage: " PROGNAME " [options] input_file\n" |
66 | "Options:\n" |
67 | " -h, --help Print this help message\n" |
68 | " -p, --profile <n> Profile (audio object type)\n" |
69 | " 2: MPEG-4 AAC LC (default)\n" |
70 | " 5: MPEG-4 HE-AAC (SBR)\n" |
71 | " 29: MPEG-4 HE-AAC v2 (SBR+PS)\n" |
72 | " 23: MPEG-4 AAC LD\n" |
73 | " 39: MPEG-4 AAC ELD\n" |
74 | " 129: MPEG-2 AAC LC\n" |
75 | " 132: MPEG-2 HE-AAC (SBR)\n" |
76 | " 156: MPEG-2 HE-AAC v2 (SBR+PS)\n" |
77 | " -b, --bitrate <n> Bitrate in bits per seconds (for CBR)\n" |
78 | " -m, --bitrate-mode <n> Bitrate configuration\n" |
79 | " 0: CBR (default)\n" |
80 | " 1-5: VBR\n" |
81 | " (VBR mode is not officially supported, and\n" |
82 | " works only on a certain combination of\n" |
83 | " parameter settings, sample rate, and\n" |
84 | " channel configuration)\n" |
85 | " -w, --bandwidth <n> Frequency bandwidth in Hz (AAC LC only)\n" |
097ef9a8 |
86 | " -a, --afterburner <n> Afterburner\n" |
48e2f01c |
87 | " 0: Off\n" |
88 | " 1: On(default)\n" |
89 | " -L, --lowdelay-sbr Enable ELD-SBR (AAC ELD only)\n" |
90 | " -s, --sbr-signaling <n> SBR signaling mode\n" |
91 | " 0: Implicit, backward compatible(default)\n" |
92 | " 1: Explicit SBR and implicit PS\n" |
93 | " 2: Explicit hierarchical signaling\n" |
94 | " -f, --transport-format <n> Transport format\n" |
95 | " 0: RAW (default, muxed into M4A)\n" |
96 | " 1: ADIF\n" |
97 | " 2: ADTS\n" |
98 | " 6: LATM MCP=1\n" |
99 | " 7: LATM MCP=0\n" |
100 | " 10: LOAS/LATM (LATM within LOAS)\n" |
68543176 |
101 | " -C, --adts-crc-check Add CRC protection on ADTS header\n" |
48e2f01c |
102 | " -h, --header-period <n> StreamMuxConfig/PCE repetition period in\n" |
103 | " transport layer\n" |
104 | "\n" |
105 | " -o <filename> Output filename\n" |
106 | " --ignore-length Ignore length of WAV header\n" |
107 | "\n" |
68543176 |
108 | "Options for raw (headerless) input:\n" |
109 | " -R, --raw Treat input as raw (by default WAV is\n" |
110 | " assumed)\n" |
111 | " --raw-channels <n> Number of channels (default: 2)\n" |
112 | " --raw-rate <n> Sample rate (default: 44100)\n" |
113 | " --raw-format <spec> Sample format, default is \"S16L\".\n" |
114 | " Spec is as follows:\n" |
115 | " 1st char: S(igned)|U(nsigned)|F(loat)\n" |
116 | " 2nd part: bits per channel\n" |
117 | " Last char: L(ittle)|B(ig)\n" |
118 | " Last char can be omitted, in which case L is\n" |
119 | " assumed. Spec is case insensitive, therefore\n" |
120 | " \"u16b\" is same as \"U16B\".\n" |
121 | "\n" |
48e2f01c |
122 | "Tagging options:\n" |
123 | " --title <string>\n" |
124 | " --artist <string>\n" |
125 | " --album <string>\n" |
126 | " --genre <string>\n" |
127 | " --date <string>\n" |
128 | " --composer <string>\n" |
129 | " --grouping <string>\n" |
130 | " --comment <string>\n" |
131 | " --album-artist <string>\n" |
132 | " --track <number[/total]>\n" |
133 | " --disk <number[/total]>\n" |
134 | " --tempo <n>\n" |
135 | , fdkaac_version); |
136 | } |
137 | |
138 | typedef struct aacenc_tag_entry_t { |
139 | uint32_t tag; |
140 | const char *data; |
141 | } aacenc_tag_entry_t; |
142 | |
143 | typedef struct aacenc_param_ex_t { |
144 | AACENC_PARAMS |
145 | |
146 | char *input_filename; |
147 | char *output_filename; |
148 | unsigned ignore_length; |
149 | |
68543176 |
150 | int is_raw; |
151 | unsigned raw_channels; |
152 | unsigned raw_rate; |
153 | const char *raw_format; |
154 | |
48e2f01c |
155 | aacenc_tag_entry_t *tag_table; |
156 | unsigned tag_count; |
157 | unsigned tag_table_capacity; |
158 | } aacenc_param_ex_t; |
159 | |
160 | static |
161 | int parse_options(int argc, char **argv, aacenc_param_ex_t *params) |
162 | { |
163 | int ch; |
164 | unsigned n; |
165 | aacenc_tag_entry_t *tag; |
166 | |
68543176 |
167 | #define OPT_RAW_CHANNELS M4AF_FOURCC('r','c','h','n') |
168 | #define OPT_RAW_RATE M4AF_FOURCC('r','r','a','t') |
169 | #define OPT_RAW_FORMAT M4AF_FOURCC('r','f','m','t') |
170 | |
48e2f01c |
171 | static struct option long_options[] = { |
172 | { "help", no_argument, 0, 'h' }, |
173 | { "profile", required_argument, 0, 'p' }, |
174 | { "bitrate", required_argument, 0, 'b' }, |
34b319e0 |
175 | { "bitrate-mode", required_argument, 0, 'm' }, |
48e2f01c |
176 | { "bandwidth", required_argument, 0, 'w' }, |
177 | { "afterburner", required_argument, 0, 'a' }, |
178 | { "lowdelay-sbr", no_argument, 0, 'L' }, |
179 | { "sbr-signaling", required_argument, 0, 's' }, |
180 | { "transport-format", required_argument, 0, 'f' }, |
68543176 |
181 | { "adts-crc-check", no_argument, 0, 'C' }, |
48e2f01c |
182 | { "header-period", required_argument, 0, 'P' }, |
183 | |
184 | { "ignore-length", no_argument, 0, 'I' }, |
185 | |
68543176 |
186 | { "raw", no_argument, 0, 'R' }, |
187 | { "raw-channels", required_argument, 0, OPT_RAW_CHANNELS }, |
188 | { "raw-rate", required_argument, 0, OPT_RAW_RATE }, |
189 | { "raw-format", required_argument, 0, OPT_RAW_FORMAT }, |
190 | |
191 | { "title", required_argument, 0, M4AF_TAG_TITLE }, |
192 | { "artist", required_argument, 0, M4AF_TAG_ARTIST }, |
193 | { "album", required_argument, 0, M4AF_TAG_ALBUM }, |
194 | { "genre", required_argument, 0, M4AF_TAG_GENRE }, |
195 | { "date", required_argument, 0, M4AF_TAG_DATE }, |
196 | { "composer", required_argument, 0, M4AF_TAG_COMPOSER }, |
197 | { "grouping", required_argument, 0, M4AF_TAG_GROUPING }, |
198 | { "comment", required_argument, 0, M4AF_TAG_COMMENT }, |
199 | { "album-artist", required_argument, 0, M4AF_TAG_ALBUM_ARTIST }, |
200 | { "track", required_argument, 0, M4AF_TAG_TRACK }, |
201 | { "disk", required_argument, 0, M4AF_TAG_DISK }, |
202 | { "tempo", required_argument, 0, M4AF_TAG_TEMPO }, |
48e2f01c |
203 | }; |
204 | params->afterburner = 1; |
205 | |
206 | aacenc_getmainargs(&argc, &argv); |
68543176 |
207 | while ((ch = getopt_long(argc, argv, "hp:b:m:w:a:Ls:f:CP:Io:R", |
48e2f01c |
208 | long_options, 0)) != EOF) { |
209 | switch (ch) { |
210 | case 'h': |
211 | return usage(), -1; |
212 | case 'p': |
213 | if (sscanf(optarg, "%u", &n) != 1) { |
214 | fprintf(stderr, "invalid arg for profile\n"); |
215 | return -1; |
216 | } |
217 | params->profile = n; |
218 | break; |
219 | case 'b': |
220 | if (sscanf(optarg, "%u", &n) != 1) { |
221 | fprintf(stderr, "invalid arg for bitrate\n"); |
222 | return -1; |
223 | } |
224 | params->bitrate = n; |
225 | break; |
226 | case 'm': |
227 | if (sscanf(optarg, "%u", &n) != 1 || n > 5) { |
228 | fprintf(stderr, "invalid arg for bitrate-mode\n"); |
229 | return -1; |
230 | } |
231 | params->bitrate_mode = n; |
232 | break; |
233 | case 'w': |
234 | if (sscanf(optarg, "%u", &n) != 1) { |
235 | fprintf(stderr, "invalid arg for bandwidth\n"); |
236 | return -1; |
237 | } |
238 | params->bandwidth = n; |
239 | break; |
240 | case 'a': |
241 | if (sscanf(optarg, "%u", &n) != 1 || n > 1) { |
242 | fprintf(stderr, "invalid arg for afterburner\n"); |
243 | return -1; |
244 | } |
245 | params->afterburner = n; |
246 | break; |
247 | case 'L': |
248 | params->lowdelay_sbr = 1; |
249 | break; |
250 | case 's': |
251 | if (sscanf(optarg, "%u", &n) != 1 || n > 2) { |
252 | fprintf(stderr, "invalid arg for sbr-signaling\n"); |
253 | return -1; |
254 | } |
255 | params->sbr_signaling = n; |
256 | break; |
257 | case 'f': |
258 | if (sscanf(optarg, "%u", &n) != 1) { |
259 | fprintf(stderr, "invalid arg for transport-format\n"); |
260 | return -1; |
261 | } |
262 | params->transport_format = n; |
263 | break; |
264 | case 'c': |
265 | params->adts_crc_check = 1; |
266 | break; |
267 | case 'P': |
268 | if (sscanf(optarg, "%u", &n) != 1) { |
269 | fprintf(stderr, "invalid arg for header-period\n"); |
270 | return -1; |
271 | } |
272 | params->header_period = n; |
273 | break; |
274 | case 'o': |
275 | params->output_filename = optarg; |
276 | break; |
277 | case 'I': |
278 | params->ignore_length = 1; |
279 | break; |
68543176 |
280 | case 'R': |
281 | params->is_raw = 1; |
282 | break; |
283 | case OPT_RAW_CHANNELS: |
284 | if (sscanf(optarg, "%u", &n) != 1) { |
285 | fprintf(stderr, "invalid arg for raw-channels\n"); |
286 | return -1; |
287 | } |
288 | params->raw_channels = n; |
289 | break; |
290 | case OPT_RAW_RATE: |
291 | if (sscanf(optarg, "%u", &n) != 1) { |
292 | fprintf(stderr, "invalid arg for raw-rate\n"); |
293 | return -1; |
294 | } |
295 | params->raw_rate = n; |
296 | break; |
297 | case OPT_RAW_FORMAT: |
298 | params->raw_format = optarg; |
299 | break; |
48e2f01c |
300 | case M4AF_TAG_TITLE: |
301 | case M4AF_TAG_ARTIST: |
302 | case M4AF_TAG_ALBUM: |
303 | case M4AF_TAG_GENRE: |
304 | case M4AF_TAG_DATE: |
305 | case M4AF_TAG_COMPOSER: |
306 | case M4AF_TAG_GROUPING: |
307 | case M4AF_TAG_COMMENT: |
308 | case M4AF_TAG_ALBUM_ARTIST: |
309 | case M4AF_TAG_TRACK: |
310 | case M4AF_TAG_DISK: |
311 | case M4AF_TAG_TEMPO: |
312 | if (params->tag_count == params->tag_table_capacity) { |
313 | unsigned newsize = params->tag_table_capacity; |
314 | newsize = newsize ? newsize * 2 : 1; |
315 | params->tag_table = |
316 | realloc(params->tag_table, |
317 | newsize * sizeof(aacenc_tag_entry_t)); |
318 | params->tag_table_capacity = newsize; |
319 | } |
320 | tag = params->tag_table + params->tag_count; |
321 | tag->tag = ch; |
322 | tag->data = optarg; |
323 | params->tag_count++; |
324 | break; |
325 | default: |
326 | return usage(), -1; |
327 | } |
328 | } |
329 | if (argc == optind) |
330 | return usage(), -1; |
68543176 |
331 | |
48e2f01c |
332 | if (!params->bitrate && !params->bitrate_mode) { |
333 | fprintf(stderr, "bitrate or bitrate-mode is mandatory\n"); |
334 | return -1; |
335 | } |
336 | if (params->output_filename && !strcmp(params->output_filename, "-") && |
337 | !params->transport_format) { |
338 | fprintf(stderr, "stdout streaming is not available on M4A output\n"); |
339 | return -1; |
340 | } |
341 | if (params->bitrate && params->bitrate < 10000) |
342 | params->bitrate *= 1000; |
68543176 |
343 | |
344 | if (params->is_raw) { |
345 | if (!params->raw_channels) |
346 | params->raw_channels = 2; |
347 | if (!params->raw_rate) |
348 | params->raw_rate = 44100; |
349 | if (!params->raw_format) |
350 | params->raw_format = "S16L"; |
351 | } |
48e2f01c |
352 | params->input_filename = argv[optind]; |
353 | return 0; |
354 | }; |
355 | |
356 | static |
357 | int write_sample(FILE *ofp, m4af_writer_t *m4af, |
358 | const void *data, uint32_t size, uint32_t duration) |
359 | { |
360 | if (!m4af) { |
5e1168a4 |
361 | fwrite(data, 1, size, ofp); |
362 | if (ferror(ofp)) { |
48e2f01c |
363 | fprintf(stderr, "ERROR: fwrite(): %s\n", strerror(errno)); |
364 | return -1; |
365 | } |
366 | } else if (m4af_write_sample(m4af, 0, data, size, duration) < 0) { |
367 | fprintf(stderr, "ERROR: failed to write m4a sample\n"); |
368 | return -1; |
369 | } |
370 | return 0; |
371 | } |
372 | |
373 | static |
374 | int encode(wav_reader_t *wavf, HANDLE_AACENCODER encoder, |
375 | uint32_t frame_length, FILE *ofp, m4af_writer_t *m4af) |
376 | { |
377 | uint8_t *ibuf = 0; |
378 | int16_t *pcmbuf = 0; |
379 | uint32_t pcmsize = 0; |
380 | uint8_t *obuf = 0; |
381 | uint32_t olen; |
382 | uint32_t osize = 0; |
383 | int nread = 1; |
384 | int consumed; |
385 | int rc = -1; |
386 | int frames_written = 0; |
387 | aacenc_progress_t progress = { 0 }; |
388 | const pcm_sample_description_t *format = wav_get_format(wavf); |
389 | |
390 | ibuf = malloc(frame_length * format->bytes_per_frame); |
391 | aacenc_progress_init(&progress, wav_get_length(wavf), format->sample_rate); |
392 | do { |
393 | if (nread) { |
394 | if ((nread = wav_read_frames(wavf, ibuf, frame_length)) < 0) { |
395 | fprintf(stderr, "ERROR: read failed\n"); |
396 | goto END; |
397 | } else if (nread > 0) { |
398 | if (pcm_convert_to_native_sint16(format, ibuf, nread, |
399 | &pcmbuf, &pcmsize) < 0) { |
400 | fprintf(stderr, "ERROR: unsupported sample format\n"); |
401 | goto END; |
402 | } |
403 | } |
404 | aacenc_progress_update(&progress, wav_get_position(wavf), |
405 | format->sample_rate * 2); |
406 | } |
407 | if ((consumed = aac_encode_frame(encoder, format, pcmbuf, nread, |
408 | &obuf, &olen, &osize)) < 0) |
409 | goto END; |
410 | if (olen > 0) { |
411 | if (write_sample(ofp, m4af, obuf, olen, frame_length) < 0) |
412 | goto END; |
413 | ++frames_written; |
414 | } |
415 | } while (nread > 0 || olen > 0); |
416 | aacenc_progress_finish(&progress, wav_get_position(wavf)); |
417 | rc = frames_written; |
418 | END: |
419 | if (ibuf) free(ibuf); |
420 | if (pcmbuf) free(pcmbuf); |
421 | if (obuf) free(obuf); |
422 | return rc; |
423 | } |
424 | |
425 | static |
426 | int finalize_m4a(m4af_writer_t *m4af, const aacenc_param_ex_t *params, |
427 | HANDLE_AACENCODER encoder) |
428 | { |
429 | unsigned i; |
430 | aacenc_tag_entry_t *tag = params->tag_table; |
431 | |
432 | for (i = 0; i < params->tag_count; ++i, ++tag) { |
433 | switch (tag->tag) { |
434 | case M4AF_TAG_TRACK: |
435 | { |
436 | unsigned m, n = 0; |
437 | if (sscanf(tag->data, "%u/%u", &m, &n) >= 1) |
438 | m4af_add_itmf_track_tag(m4af, m, n); |
439 | break; |
440 | } |
441 | case M4AF_TAG_DISK: |
442 | { |
443 | unsigned m, n = 0; |
444 | if (sscanf(tag->data, "%u/%u", &m, &n) >= 1) |
445 | m4af_add_itmf_disk_tag(m4af, m, n); |
446 | break; |
447 | } |
448 | case M4AF_TAG_TEMPO: |
449 | { |
450 | unsigned n; |
451 | if (sscanf(tag->data, "%u", &n) == 1) |
452 | m4af_add_itmf_int16_tag(m4af, tag->tag, n); |
453 | break; |
454 | } |
455 | default: |
456 | { |
457 | char *u8 = aacenc_to_utf8(tag->data); |
458 | m4af_add_itmf_string_tag(m4af, tag->tag, u8); |
459 | free(u8); |
460 | } |
461 | } |
462 | } |
463 | { |
464 | char tool_info[256]; |
465 | char *p = tool_info; |
466 | LIB_INFO *lib_info = 0; |
467 | |
468 | p += sprintf(p, PROGNAME " %s, ", fdkaac_version); |
469 | |
d1ce2536 |
470 | lib_info = calloc(FDK_MODULE_LAST, sizeof(LIB_INFO)); |
48e2f01c |
471 | if (aacEncGetLibInfo(lib_info) == AACENC_OK) { |
472 | for (i = 0; i < FDK_MODULE_LAST; ++i) |
473 | if (lib_info[i].module_id == FDK_AACENC) |
474 | break; |
475 | p += sprintf(p, "libfdk-aac %s, ", lib_info[i].versionStr); |
476 | } |
477 | free(lib_info); |
478 | if (params->bitrate_mode) |
479 | sprintf(p, "VBR mode %d", params->bitrate_mode); |
480 | else |
112fdaf4 |
481 | sprintf(p, "CBR %dkbps", |
482 | aacEncoder_GetParam(encoder, AACENC_BITRATE) / 1000); |
48e2f01c |
483 | |
484 | m4af_add_itmf_string_tag(m4af, M4AF_TAG_TOOL, tool_info); |
485 | } |
486 | if (m4af_finalize(m4af) < 0) { |
487 | fprintf(stderr, "ERROR: failed to finalize m4a\n"); |
488 | return -1; |
489 | } |
490 | return 0; |
491 | } |
492 | |
af8fa38d |
493 | static |
494 | const char *basename(const char *filename) |
495 | { |
496 | char *p = strrchr(filename, '/'); |
497 | #ifdef _WIN32 |
498 | char *q = strrchr(filename, '\\'); |
499 | if (p < q) p = q; |
500 | #endif |
501 | return p ? p + 1 : filename; |
502 | } |
503 | |
504 | static |
505 | char *generate_output_filename(const char *filename, const char *ext) |
506 | { |
507 | char *p = 0; |
508 | size_t ext_len = strlen(ext); |
509 | |
510 | if (strcmp(filename, "-") == 0) { |
511 | p = malloc(ext_len + 6); |
512 | sprintf(p, "stdin%s", ext); |
513 | } else { |
514 | const char *base = basename(filename); |
515 | size_t ilen = strlen(base); |
516 | const char *ext_org = strrchr(base, '.'); |
517 | if (ext_org) ilen = ext_org - base; |
518 | p = malloc(ilen + ext_len + 1); |
519 | sprintf(p, "%.*s%s", ilen, base, ext); |
520 | } |
521 | return p; |
522 | } |
523 | |
68543176 |
524 | static |
525 | int parse_raw_spec(const char *spec, pcm_sample_description_t *desc) |
526 | { |
527 | unsigned bits; |
528 | unsigned char c_type, c_endian = 'L'; |
529 | int type; |
530 | |
531 | if (sscanf(spec, "%c%u%c", &c_type, &bits, &c_endian) < 2) |
532 | return -1; |
533 | c_type = toupper(c_type); |
534 | c_endian = toupper(c_endian); |
535 | |
536 | if (c_type == 'S') |
537 | type = 1; |
538 | else if (c_type == 'U') |
539 | type = 2; |
540 | else if (c_type == 'F') |
541 | type = 4; |
542 | else |
543 | return -1; |
544 | |
545 | if (c_endian == 'B') |
546 | type |= 8; |
547 | else if (c_endian != 'L') |
548 | return -1; |
549 | |
550 | if (c_type == 'F' && bits != 32 && bits != 64) |
551 | return -1; |
552 | if (c_type != 'F' && (bits < 8 || bits > 32)) |
553 | return -1; |
554 | |
555 | desc->sample_type = type; |
556 | desc->bits_per_channel = bits; |
557 | return 0; |
558 | } |
559 | |
48e2f01c |
560 | int main(int argc, char **argv) |
561 | { |
68543176 |
562 | wav_io_context_t wav_io = { read_callback, seek_callback, tell_callback }; |
48e2f01c |
563 | m4af_io_callbacks_t m4af_io = { |
564 | write_callback, seek_callback, tell_callback }; |
565 | aacenc_param_ex_t params = { 0 }; |
566 | |
567 | int result = 2; |
568 | FILE *ifp = 0; |
569 | FILE *ofp = 0; |
570 | char *output_filename = 0; |
571 | wav_reader_t *wavf = 0; |
572 | HANDLE_AACENCODER encoder = 0; |
573 | AACENC_InfoStruct aacinfo = { 0 }; |
574 | m4af_writer_t *m4af = 0; |
575 | const pcm_sample_description_t *sample_format; |
576 | int downsampled_timescale = 0; |
577 | int frame_count = 0; |
7222b0b5 |
578 | struct stat stb = { 0 }; |
48e2f01c |
579 | |
580 | setlocale(LC_CTYPE, ""); |
581 | setbuf(stderr, 0); |
582 | |
583 | if (parse_options(argc, argv, ¶ms) < 0) |
584 | return 1; |
585 | |
586 | if ((ifp = aacenc_fopen(params.input_filename, "rb")) == 0) { |
587 | aacenc_fprintf(stderr, "ERROR: %s: %s\n", params.input_filename, |
588 | strerror(errno)); |
589 | goto END; |
590 | } |
68543176 |
591 | if (fstat(fileno(ifp), &stb) == 0 && (stb.st_mode & S_IFMT) != S_IFREG) { |
48e2f01c |
592 | wav_io.seek = 0; |
68543176 |
593 | wav_io.tell = 0; |
594 | } |
595 | if (!params.is_raw) { |
596 | if ((wavf = wav_open(&wav_io, ifp, params.ignore_length)) == 0) { |
597 | fprintf(stderr, "ERROR: broken / unsupported input file\n"); |
598 | goto END; |
599 | } |
600 | } else { |
601 | int bytes_per_channel; |
602 | pcm_sample_description_t desc = { 0 }; |
603 | if (parse_raw_spec(params.raw_format, &desc) < 0) { |
604 | fprintf(stderr, "ERROR: invalid raw-format spec\n"); |
605 | goto END; |
606 | } |
607 | desc.sample_rate = params.raw_rate; |
608 | desc.channels_per_frame = params.raw_channels; |
609 | bytes_per_channel = (desc.bits_per_channel + 7) / 8; |
610 | desc.bytes_per_frame = params.raw_channels * bytes_per_channel; |
611 | if ((wavf = raw_open(&wav_io, ifp, &desc)) == 0) { |
612 | fprintf(stderr, "ERROR: failed to open raw input\n"); |
613 | goto END; |
614 | } |
48e2f01c |
615 | } |
616 | sample_format = wav_get_format(wavf); |
617 | |
618 | if (aacenc_init(&encoder, (aacenc_param_t*)¶ms, sample_format, |
619 | &aacinfo) < 0) |
620 | goto END; |
621 | |
622 | if (!params.output_filename) { |
af8fa38d |
623 | const char *ext = params.transport_format ? ".aac" : ".m4a"; |
624 | output_filename = generate_output_filename(params.input_filename, ext); |
48e2f01c |
625 | params.output_filename = output_filename; |
626 | } |
627 | |
628 | if ((ofp = aacenc_fopen(params.output_filename, "wb")) == 0) { |
629 | aacenc_fprintf(stderr, "ERROR: %s: %s\n", params.output_filename, |
630 | strerror(errno)); |
631 | goto END; |
632 | } |
633 | if (!params.transport_format) { |
634 | uint32_t scale; |
635 | unsigned framelen = aacinfo.frameLength; |
636 | int sbr_mode = aacenc_is_sbr_active((aacenc_param_t*)¶ms); |
637 | int sig_mode = aacEncoder_GetParam(encoder, AACENC_SIGNALING_MODE); |
638 | if (sbr_mode && !sig_mode) |
639 | downsampled_timescale = 1; |
640 | scale = sample_format->sample_rate >> downsampled_timescale; |
641 | if ((m4af = m4af_create(M4AF_CODEC_MP4A, scale, &m4af_io, ofp)) < 0) |
642 | goto END; |
643 | m4af_decoder_specific_info(m4af, 0, aacinfo.confBuf, aacinfo.confSize); |
644 | m4af_set_fixed_frame_duration(m4af, 0, |
645 | framelen >> downsampled_timescale); |
646 | m4af_begin_write(m4af); |
647 | } |
648 | frame_count = encode(wavf, encoder, aacinfo.frameLength, ofp, m4af); |
649 | if (frame_count < 0) |
650 | goto END; |
651 | if (m4af) { |
652 | uint32_t delay = aacinfo.encoderDelay; |
653 | int64_t frames_read = wav_get_position(wavf); |
654 | uint32_t padding = frame_count * aacinfo.frameLength |
655 | - frames_read - aacinfo.encoderDelay; |
656 | m4af_set_priming(m4af, 0, delay >> downsampled_timescale, |
657 | padding >> downsampled_timescale); |
658 | if (finalize_m4a(m4af, ¶ms, encoder) < 0) |
659 | goto END; |
660 | } |
661 | result = 0; |
662 | END: |
663 | if (wavf) wav_teardown(&wavf); |
664 | if (ifp) fclose(ifp); |
665 | if (m4af) m4af_teardown(&m4af); |
666 | if (ofp) fclose(ofp); |
667 | if (encoder) aacEncClose(&encoder); |
668 | if (output_filename) free(output_filename); |
669 | if (params.tag_table) free(params.tag_table); |
670 | |
671 | return result; |
672 | } |