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 |
c5c45908 |
11 | #if HAVE_INTTYPES_H |
12 | # include <inttypes.h> |
13 | #elif defined(_MSC_VER) |
14 | # define SCNd64 "I64d" |
15 | #endif |
48e2f01c |
16 | #include <stdio.h> |
17 | #include <stdlib.h> |
18 | #include <string.h> |
68543176 |
19 | #include <ctype.h> |
48e2f01c |
20 | #include <locale.h> |
21 | #include <errno.h> |
7222b0b5 |
22 | #include <sys/stat.h> |
48e2f01c |
23 | #include <getopt.h> |
7222b0b5 |
24 | #if HAVE_UNISTD_H |
25 | #include <unistd.h> |
26 | #endif |
bd3b4b34 |
27 | #if HAVE_SIGACTION |
28 | #include <signal.h> |
29 | #endif |
7222b0b5 |
30 | #ifdef _WIN32 |
31 | #include <io.h> |
bd3b4b34 |
32 | #define WIN32_LEAN_AND_MEAN |
33 | #include <windows.h> |
7222b0b5 |
34 | #endif |
48e2f01c |
35 | #include "compat.h" |
36 | #include "wav_reader.h" |
37 | #include "aacenc.h" |
38 | #include "m4af.h" |
39 | #include "progress.h" |
40 | #include "version.h" |
41 | |
42 | #define PROGNAME "fdkaac" |
43 | |
721d977f |
44 | static volatile int g_interrupted = 0; |
bd3b4b34 |
45 | |
46 | #if HAVE_SIGACTION |
47 | static void signal_handler(int signum) |
48 | { |
49 | g_interrupted = 1; |
50 | } |
51 | static void handle_signals(void) |
52 | { |
53 | int i, sigs[] = { SIGINT, SIGHUP, SIGTERM }; |
54 | for (i = 0; i < sizeof(sigs)/sizeof(sigs[0]); ++i) { |
55 | struct sigaction sa = { 0 }; |
56 | sa.sa_handler = signal_handler; |
57 | sa.sa_flags |= SA_RESTART; |
58 | sigaction(sigs[i], &sa, 0); |
59 | } |
60 | } |
61 | #elif defined(_WIN32) |
62 | static BOOL WINAPI signal_handler(DWORD type) |
63 | { |
64 | g_interrupted = 1; |
65 | return TRUE; |
66 | } |
67 | |
68 | static void handle_signals(void) |
69 | { |
70 | SetConsoleCtrlHandler(signal_handler, TRUE); |
71 | } |
72 | #else |
73 | static void handle_signals(void) |
74 | { |
75 | } |
76 | #endif |
77 | |
48e2f01c |
78 | static |
79 | int read_callback(void *cookie, void *data, uint32_t size) |
80 | { |
5e1168a4 |
81 | size_t rc = fread(data, 1, size, (FILE*)cookie); |
82 | return ferror((FILE*)cookie) ? -1 : (int)rc; |
48e2f01c |
83 | } |
84 | |
85 | static |
86 | int write_callback(void *cookie, const void *data, uint32_t size) |
87 | { |
5e1168a4 |
88 | size_t rc = fwrite(data, 1, size, (FILE*)cookie); |
89 | return ferror((FILE*)cookie) ? -1 : (int)rc; |
48e2f01c |
90 | } |
91 | |
92 | static |
93 | int seek_callback(void *cookie, int64_t off, int whence) |
94 | { |
95 | return fseeko((FILE*)cookie, off, whence); |
96 | } |
97 | |
98 | static |
99 | int64_t tell_callback(void *cookie) |
100 | { |
101 | return ftello((FILE*)cookie); |
102 | } |
103 | |
104 | static |
105 | void usage(void) |
106 | { |
107 | printf( |
108 | PROGNAME " %s\n" |
109 | "Usage: " PROGNAME " [options] input_file\n" |
110 | "Options:\n" |
111 | " -h, --help Print this help message\n" |
112 | " -p, --profile <n> Profile (audio object type)\n" |
113 | " 2: MPEG-4 AAC LC (default)\n" |
114 | " 5: MPEG-4 HE-AAC (SBR)\n" |
115 | " 29: MPEG-4 HE-AAC v2 (SBR+PS)\n" |
116 | " 23: MPEG-4 AAC LD\n" |
117 | " 39: MPEG-4 AAC ELD\n" |
118 | " 129: MPEG-2 AAC LC\n" |
119 | " 132: MPEG-2 HE-AAC (SBR)\n" |
120 | " 156: MPEG-2 HE-AAC v2 (SBR+PS)\n" |
121 | " -b, --bitrate <n> Bitrate in bits per seconds (for CBR)\n" |
122 | " -m, --bitrate-mode <n> Bitrate configuration\n" |
123 | " 0: CBR (default)\n" |
124 | " 1-5: VBR\n" |
125 | " (VBR mode is not officially supported, and\n" |
126 | " works only on a certain combination of\n" |
127 | " parameter settings, sample rate, and\n" |
128 | " channel configuration)\n" |
129 | " -w, --bandwidth <n> Frequency bandwidth in Hz (AAC LC only)\n" |
097ef9a8 |
130 | " -a, --afterburner <n> Afterburner\n" |
48e2f01c |
131 | " 0: Off\n" |
132 | " 1: On(default)\n" |
133 | " -L, --lowdelay-sbr Enable ELD-SBR (AAC ELD only)\n" |
134 | " -s, --sbr-signaling <n> SBR signaling mode\n" |
135 | " 0: Implicit, backward compatible(default)\n" |
136 | " 1: Explicit SBR and implicit PS\n" |
137 | " 2: Explicit hierarchical signaling\n" |
138 | " -f, --transport-format <n> Transport format\n" |
139 | " 0: RAW (default, muxed into M4A)\n" |
140 | " 1: ADIF\n" |
141 | " 2: ADTS\n" |
142 | " 6: LATM MCP=1\n" |
143 | " 7: LATM MCP=0\n" |
144 | " 10: LOAS/LATM (LATM within LOAS)\n" |
68543176 |
145 | " -C, --adts-crc-check Add CRC protection on ADTS header\n" |
48e2f01c |
146 | " -h, --header-period <n> StreamMuxConfig/PCE repetition period in\n" |
147 | " transport layer\n" |
148 | "\n" |
149 | " -o <filename> Output filename\n" |
5c534696 |
150 | " --ignorelength Ignore length of WAV header\n" |
aa2ca1e3 |
151 | " -S, --silent Don't print progress messages\n" |
48e2f01c |
152 | "\n" |
68543176 |
153 | "Options for raw (headerless) input:\n" |
154 | " -R, --raw Treat input as raw (by default WAV is\n" |
155 | " assumed)\n" |
156 | " --raw-channels <n> Number of channels (default: 2)\n" |
157 | " --raw-rate <n> Sample rate (default: 44100)\n" |
158 | " --raw-format <spec> Sample format, default is \"S16L\".\n" |
159 | " Spec is as follows:\n" |
160 | " 1st char: S(igned)|U(nsigned)|F(loat)\n" |
161 | " 2nd part: bits per channel\n" |
162 | " Last char: L(ittle)|B(ig)\n" |
163 | " Last char can be omitted, in which case L is\n" |
164 | " assumed. Spec is case insensitive, therefore\n" |
165 | " \"u16b\" is same as \"U16B\".\n" |
166 | "\n" |
48e2f01c |
167 | "Tagging options:\n" |
168 | " --title <string>\n" |
169 | " --artist <string>\n" |
170 | " --album <string>\n" |
171 | " --genre <string>\n" |
172 | " --date <string>\n" |
173 | " --composer <string>\n" |
174 | " --grouping <string>\n" |
175 | " --comment <string>\n" |
176 | " --album-artist <string>\n" |
177 | " --track <number[/total]>\n" |
178 | " --disk <number[/total]>\n" |
179 | " --tempo <n>\n" |
c5c45908 |
180 | " --tag <fcc>:<value> Set iTunes predefined tag with four char code.\n" |
a56831da |
181 | " --tag-from-file <fcc>:<filename>\n" |
182 | " Same as above, but value is read from file.\n" |
c5c45908 |
183 | " --long-tag <name>:<value> Set arbitrary tag as iTunes custom metadata.\n" |
48e2f01c |
184 | , fdkaac_version); |
185 | } |
186 | |
187 | typedef struct aacenc_tag_entry_t { |
188 | uint32_t tag; |
c5c45908 |
189 | const char *name; |
48e2f01c |
190 | const char *data; |
c5c45908 |
191 | uint32_t data_size; |
a56831da |
192 | int is_file_name; |
48e2f01c |
193 | } aacenc_tag_entry_t; |
194 | |
195 | typedef struct aacenc_param_ex_t { |
196 | AACENC_PARAMS |
197 | |
198 | char *input_filename; |
199 | char *output_filename; |
200 | unsigned ignore_length; |
aa2ca1e3 |
201 | int silent; |
48e2f01c |
202 | |
68543176 |
203 | int is_raw; |
204 | unsigned raw_channels; |
205 | unsigned raw_rate; |
206 | const char *raw_format; |
207 | |
48e2f01c |
208 | aacenc_tag_entry_t *tag_table; |
209 | unsigned tag_count; |
210 | unsigned tag_table_capacity; |
211 | } aacenc_param_ex_t; |
212 | |
c5c45908 |
213 | static |
214 | void param_add_itmf_entry(aacenc_param_ex_t *params, uint32_t tag, |
a56831da |
215 | const char *key, const char *value, uint32_t size, |
216 | int is_file_name) |
c5c45908 |
217 | { |
218 | aacenc_tag_entry_t *entry; |
219 | if (params->tag_count == params->tag_table_capacity) { |
220 | unsigned newsize = params->tag_table_capacity; |
221 | newsize = newsize ? newsize * 2 : 1; |
222 | params->tag_table = |
223 | realloc(params->tag_table, newsize * sizeof(aacenc_tag_entry_t)); |
224 | params->tag_table_capacity = newsize; |
225 | } |
226 | entry = params->tag_table + params->tag_count; |
227 | entry->tag = tag; |
228 | if (tag == M4AF_FOURCC('-','-','-','-')) |
229 | entry->name = key; |
230 | entry->data = value; |
231 | entry->data_size = size; |
a56831da |
232 | entry->is_file_name = is_file_name; |
c5c45908 |
233 | params->tag_count++; |
234 | } |
235 | |
48e2f01c |
236 | static |
237 | int parse_options(int argc, char **argv, aacenc_param_ex_t *params) |
238 | { |
239 | int ch; |
240 | unsigned n; |
48e2f01c |
241 | |
a56831da |
242 | #define OPT_RAW_CHANNELS M4AF_FOURCC('r','c','h','n') |
243 | #define OPT_RAW_RATE M4AF_FOURCC('r','r','a','t') |
244 | #define OPT_RAW_FORMAT M4AF_FOURCC('r','f','m','t') |
245 | #define OPT_SHORT_TAG M4AF_FOURCC('s','t','a','g') |
246 | #define OPT_SHORT_TAG_FILE M4AF_FOURCC('s','t','g','f') |
247 | #define OPT_LONG_TAG M4AF_FOURCC('l','t','a','g') |
68543176 |
248 | |
48e2f01c |
249 | static struct option long_options[] = { |
250 | { "help", no_argument, 0, 'h' }, |
251 | { "profile", required_argument, 0, 'p' }, |
252 | { "bitrate", required_argument, 0, 'b' }, |
34b319e0 |
253 | { "bitrate-mode", required_argument, 0, 'm' }, |
48e2f01c |
254 | { "bandwidth", required_argument, 0, 'w' }, |
255 | { "afterburner", required_argument, 0, 'a' }, |
256 | { "lowdelay-sbr", no_argument, 0, 'L' }, |
257 | { "sbr-signaling", required_argument, 0, 's' }, |
258 | { "transport-format", required_argument, 0, 'f' }, |
68543176 |
259 | { "adts-crc-check", no_argument, 0, 'C' }, |
48e2f01c |
260 | { "header-period", required_argument, 0, 'P' }, |
261 | |
5c534696 |
262 | { "ignorelength", no_argument, 0, 'I' }, |
aa2ca1e3 |
263 | { "silent", no_argument, 0, 'S' }, |
48e2f01c |
264 | |
68543176 |
265 | { "raw", no_argument, 0, 'R' }, |
266 | { "raw-channels", required_argument, 0, OPT_RAW_CHANNELS }, |
267 | { "raw-rate", required_argument, 0, OPT_RAW_RATE }, |
268 | { "raw-format", required_argument, 0, OPT_RAW_FORMAT }, |
269 | |
270 | { "title", required_argument, 0, M4AF_TAG_TITLE }, |
271 | { "artist", required_argument, 0, M4AF_TAG_ARTIST }, |
272 | { "album", required_argument, 0, M4AF_TAG_ALBUM }, |
273 | { "genre", required_argument, 0, M4AF_TAG_GENRE }, |
274 | { "date", required_argument, 0, M4AF_TAG_DATE }, |
275 | { "composer", required_argument, 0, M4AF_TAG_COMPOSER }, |
276 | { "grouping", required_argument, 0, M4AF_TAG_GROUPING }, |
277 | { "comment", required_argument, 0, M4AF_TAG_COMMENT }, |
278 | { "album-artist", required_argument, 0, M4AF_TAG_ALBUM_ARTIST }, |
279 | { "track", required_argument, 0, M4AF_TAG_TRACK }, |
280 | { "disk", required_argument, 0, M4AF_TAG_DISK }, |
281 | { "tempo", required_argument, 0, M4AF_TAG_TEMPO }, |
c5c45908 |
282 | { "tag", required_argument, 0, OPT_SHORT_TAG }, |
a56831da |
283 | { "tag-from-file", required_argument, 0, OPT_SHORT_TAG_FILE }, |
c5c45908 |
284 | { "long-tag", required_argument, 0, OPT_LONG_TAG }, |
5c534696 |
285 | { 0, 0, 0, 0 }, |
48e2f01c |
286 | }; |
287 | params->afterburner = 1; |
288 | |
289 | aacenc_getmainargs(&argc, &argv); |
aa2ca1e3 |
290 | while ((ch = getopt_long(argc, argv, "hp:b:m:w:a:Ls:f:CP:Io:SR", |
48e2f01c |
291 | long_options, 0)) != EOF) { |
292 | switch (ch) { |
293 | case 'h': |
294 | return usage(), -1; |
295 | case 'p': |
296 | if (sscanf(optarg, "%u", &n) != 1) { |
297 | fprintf(stderr, "invalid arg for profile\n"); |
298 | return -1; |
299 | } |
300 | params->profile = n; |
301 | break; |
302 | case 'b': |
303 | if (sscanf(optarg, "%u", &n) != 1) { |
304 | fprintf(stderr, "invalid arg for bitrate\n"); |
305 | return -1; |
306 | } |
307 | params->bitrate = n; |
308 | break; |
309 | case 'm': |
310 | if (sscanf(optarg, "%u", &n) != 1 || n > 5) { |
311 | fprintf(stderr, "invalid arg for bitrate-mode\n"); |
312 | return -1; |
313 | } |
314 | params->bitrate_mode = n; |
315 | break; |
316 | case 'w': |
317 | if (sscanf(optarg, "%u", &n) != 1) { |
318 | fprintf(stderr, "invalid arg for bandwidth\n"); |
319 | return -1; |
320 | } |
321 | params->bandwidth = n; |
322 | break; |
323 | case 'a': |
324 | if (sscanf(optarg, "%u", &n) != 1 || n > 1) { |
325 | fprintf(stderr, "invalid arg for afterburner\n"); |
326 | return -1; |
327 | } |
328 | params->afterburner = n; |
329 | break; |
330 | case 'L': |
331 | params->lowdelay_sbr = 1; |
332 | break; |
333 | case 's': |
334 | if (sscanf(optarg, "%u", &n) != 1 || n > 2) { |
335 | fprintf(stderr, "invalid arg for sbr-signaling\n"); |
336 | return -1; |
337 | } |
338 | params->sbr_signaling = n; |
339 | break; |
340 | case 'f': |
341 | if (sscanf(optarg, "%u", &n) != 1) { |
342 | fprintf(stderr, "invalid arg for transport-format\n"); |
343 | return -1; |
344 | } |
345 | params->transport_format = n; |
346 | break; |
347 | case 'c': |
348 | params->adts_crc_check = 1; |
349 | break; |
350 | case 'P': |
351 | if (sscanf(optarg, "%u", &n) != 1) { |
352 | fprintf(stderr, "invalid arg for header-period\n"); |
353 | return -1; |
354 | } |
355 | params->header_period = n; |
356 | break; |
357 | case 'o': |
358 | params->output_filename = optarg; |
359 | break; |
360 | case 'I': |
361 | params->ignore_length = 1; |
362 | break; |
aa2ca1e3 |
363 | case 'S': |
364 | params->silent = 1; |
365 | break; |
68543176 |
366 | case 'R': |
367 | params->is_raw = 1; |
368 | break; |
369 | case OPT_RAW_CHANNELS: |
370 | if (sscanf(optarg, "%u", &n) != 1) { |
371 | fprintf(stderr, "invalid arg for raw-channels\n"); |
372 | return -1; |
373 | } |
374 | params->raw_channels = n; |
375 | break; |
376 | case OPT_RAW_RATE: |
377 | if (sscanf(optarg, "%u", &n) != 1) { |
378 | fprintf(stderr, "invalid arg for raw-rate\n"); |
379 | return -1; |
380 | } |
381 | params->raw_rate = n; |
382 | break; |
383 | case OPT_RAW_FORMAT: |
384 | params->raw_format = optarg; |
385 | break; |
48e2f01c |
386 | case M4AF_TAG_TITLE: |
387 | case M4AF_TAG_ARTIST: |
388 | case M4AF_TAG_ALBUM: |
389 | case M4AF_TAG_GENRE: |
390 | case M4AF_TAG_DATE: |
391 | case M4AF_TAG_COMPOSER: |
392 | case M4AF_TAG_GROUPING: |
393 | case M4AF_TAG_COMMENT: |
394 | case M4AF_TAG_ALBUM_ARTIST: |
395 | case M4AF_TAG_TRACK: |
396 | case M4AF_TAG_DISK: |
397 | case M4AF_TAG_TEMPO: |
a56831da |
398 | param_add_itmf_entry(params, ch, 0, optarg, strlen(optarg), 0); |
c5c45908 |
399 | break; |
400 | case OPT_SHORT_TAG: |
a56831da |
401 | case OPT_SHORT_TAG_FILE: |
c5c45908 |
402 | case OPT_LONG_TAG: |
403 | { |
404 | char *val; |
405 | size_t klen; |
406 | unsigned fcc = M4AF_FOURCC('-','-','-','-'); |
407 | |
408 | if ((val = strchr(optarg, ':')) == 0) { |
409 | fprintf(stderr, "invalid arg for tag\n"); |
410 | return -1; |
411 | } |
412 | *val++ = '\0'; |
a56831da |
413 | if (ch == OPT_SHORT_TAG || ch == OPT_SHORT_TAG_FILE) { |
360cf7dc |
414 | /* |
415 | * take care of U+00A9(COPYRIGHT SIGN). |
416 | * 1) if length of fcc is 3, we prepend '\xa9'. |
417 | * 2) U+00A9 becomes "\xc2\xa9" in UTF-8. Therefore |
418 | * we remove first '\xc2'. |
419 | */ |
420 | if (optarg[0] == '\xc2') |
421 | ++optarg; |
c5c45908 |
422 | if ((klen = strlen(optarg))== 3) |
423 | fcc = 0xa9; |
424 | else if (klen != 4) { |
425 | fprintf(stderr, "invalid arg for tag\n"); |
426 | return -1; |
427 | } |
428 | for (; *optarg; ++optarg) |
429 | fcc = ((fcc << 8) | (*optarg & 0xff)); |
430 | } |
a56831da |
431 | param_add_itmf_entry(params, fcc, optarg, val, strlen(val), |
432 | ch == OPT_SHORT_TAG_FILE); |
48e2f01c |
433 | } |
48e2f01c |
434 | break; |
435 | default: |
436 | return usage(), -1; |
437 | } |
438 | } |
439 | if (argc == optind) |
440 | return usage(), -1; |
68543176 |
441 | |
48e2f01c |
442 | if (!params->bitrate && !params->bitrate_mode) { |
443 | fprintf(stderr, "bitrate or bitrate-mode is mandatory\n"); |
444 | return -1; |
445 | } |
446 | if (params->output_filename && !strcmp(params->output_filename, "-") && |
447 | !params->transport_format) { |
448 | fprintf(stderr, "stdout streaming is not available on M4A output\n"); |
449 | return -1; |
450 | } |
451 | if (params->bitrate && params->bitrate < 10000) |
452 | params->bitrate *= 1000; |
68543176 |
453 | |
454 | if (params->is_raw) { |
455 | if (!params->raw_channels) |
456 | params->raw_channels = 2; |
457 | if (!params->raw_rate) |
458 | params->raw_rate = 44100; |
459 | if (!params->raw_format) |
460 | params->raw_format = "S16L"; |
461 | } |
48e2f01c |
462 | params->input_filename = argv[optind]; |
463 | return 0; |
464 | }; |
465 | |
466 | static |
e4bbeeb0 |
467 | int write_sample(FILE *ofp, m4af_ctx_t *m4af, |
48e2f01c |
468 | const void *data, uint32_t size, uint32_t duration) |
469 | { |
470 | if (!m4af) { |
5e1168a4 |
471 | fwrite(data, 1, size, ofp); |
472 | if (ferror(ofp)) { |
48e2f01c |
473 | fprintf(stderr, "ERROR: fwrite(): %s\n", strerror(errno)); |
474 | return -1; |
475 | } |
476 | } else if (m4af_write_sample(m4af, 0, data, size, duration) < 0) { |
477 | fprintf(stderr, "ERROR: failed to write m4a sample\n"); |
478 | return -1; |
479 | } |
480 | return 0; |
481 | } |
482 | |
483 | static |
484 | int encode(wav_reader_t *wavf, HANDLE_AACENCODER encoder, |
e4bbeeb0 |
485 | uint32_t frame_length, FILE *ofp, m4af_ctx_t *m4af, |
aa2ca1e3 |
486 | int show_progress) |
48e2f01c |
487 | { |
488 | uint8_t *ibuf = 0; |
489 | int16_t *pcmbuf = 0; |
490 | uint32_t pcmsize = 0; |
491 | uint8_t *obuf = 0; |
492 | uint32_t olen; |
493 | uint32_t osize = 0; |
494 | int nread = 1; |
495 | int consumed; |
496 | int rc = -1; |
497 | int frames_written = 0; |
498 | aacenc_progress_t progress = { 0 }; |
499 | const pcm_sample_description_t *format = wav_get_format(wavf); |
500 | |
501 | ibuf = malloc(frame_length * format->bytes_per_frame); |
502 | aacenc_progress_init(&progress, wav_get_length(wavf), format->sample_rate); |
503 | do { |
bd3b4b34 |
504 | if (g_interrupted) |
505 | nread = 0; |
506 | else if (nread) { |
48e2f01c |
507 | if ((nread = wav_read_frames(wavf, ibuf, frame_length)) < 0) { |
508 | fprintf(stderr, "ERROR: read failed\n"); |
509 | goto END; |
510 | } else if (nread > 0) { |
511 | if (pcm_convert_to_native_sint16(format, ibuf, nread, |
512 | &pcmbuf, &pcmsize) < 0) { |
513 | fprintf(stderr, "ERROR: unsupported sample format\n"); |
514 | goto END; |
515 | } |
516 | } |
aa2ca1e3 |
517 | if (show_progress) |
518 | aacenc_progress_update(&progress, wav_get_position(wavf), |
519 | format->sample_rate * 2); |
48e2f01c |
520 | } |
521 | if ((consumed = aac_encode_frame(encoder, format, pcmbuf, nread, |
522 | &obuf, &olen, &osize)) < 0) |
523 | goto END; |
524 | if (olen > 0) { |
525 | if (write_sample(ofp, m4af, obuf, olen, frame_length) < 0) |
526 | goto END; |
527 | ++frames_written; |
528 | } |
529 | } while (nread > 0 || olen > 0); |
aa2ca1e3 |
530 | |
531 | if (show_progress) |
532 | aacenc_progress_finish(&progress, wav_get_position(wavf)); |
48e2f01c |
533 | rc = frames_written; |
534 | END: |
535 | if (ibuf) free(ibuf); |
536 | if (pcmbuf) free(pcmbuf); |
537 | if (obuf) free(obuf); |
538 | return rc; |
539 | } |
540 | |
a56831da |
541 | static |
542 | char *load_tag_from_file(const char *path, uint32_t *data_size) |
543 | { |
544 | FILE *fp = 0; |
545 | char *data = 0; |
546 | int64_t size; |
547 | |
548 | if ((fp = aacenc_fopen(path, "rb")) == NULL) { |
549 | aacenc_fprintf(stderr, "WARNING: %s: %s\n", path, strerror(errno)); |
550 | goto END; |
551 | } |
552 | fseeko(fp, 0, SEEK_END); |
553 | size = ftello(fp); |
554 | if (size > 5*1024*1024) { |
555 | aacenc_fprintf(stderr, "WARNING: %s: size too large\n", path); |
556 | goto END; |
557 | } |
558 | fseeko(fp, 0, SEEK_SET); |
559 | data = malloc(size + 1); |
560 | if (data) fread(data, 1, size, fp); |
561 | data[size] = 0; |
562 | *data_size = (uint32_t)size; |
563 | END: |
564 | if (fp) fclose(fp); |
565 | return data; |
566 | } |
567 | |
c5c45908 |
568 | static |
e4bbeeb0 |
569 | void put_tag_entry(m4af_ctx_t *m4af, const aacenc_tag_entry_t *tag) |
c5c45908 |
570 | { |
571 | unsigned m, n = 0; |
a56831da |
572 | const char *data = tag->data; |
573 | uint32_t data_size = tag->data_size; |
574 | char *file_contents = 0; |
c5c45908 |
575 | |
a56831da |
576 | if (tag->is_file_name) { |
577 | data = file_contents = load_tag_from_file(tag->data, &data_size); |
578 | if (!data) return; |
579 | } |
c5c45908 |
580 | switch (tag->tag) { |
581 | case M4AF_TAG_TRACK: |
a56831da |
582 | if (sscanf(data, "%u/%u", &m, &n) >= 1) |
c5c45908 |
583 | m4af_add_itmf_track_tag(m4af, m, n); |
584 | break; |
585 | case M4AF_TAG_DISK: |
a56831da |
586 | if (sscanf(data, "%u/%u", &m, &n) >= 1) |
c5c45908 |
587 | m4af_add_itmf_disk_tag(m4af, m, n); |
588 | break; |
589 | case M4AF_TAG_GENRE_ID3: |
a56831da |
590 | if (sscanf(data, "%u", &n) == 1) |
c5c45908 |
591 | m4af_add_itmf_genre_tag(m4af, n); |
592 | break; |
593 | case M4AF_TAG_TEMPO: |
a56831da |
594 | if (sscanf(data, "%u", &n) == 1) |
c5c45908 |
595 | m4af_add_itmf_int16_tag(m4af, tag->tag, n); |
596 | break; |
597 | case M4AF_TAG_COMPILATION: |
598 | case M4AF_FOURCC('a','k','I','D'): |
599 | case M4AF_FOURCC('h','d','v','d'): |
600 | case M4AF_FOURCC('p','c','s','t'): |
601 | case M4AF_FOURCC('p','g','a','p'): |
602 | case M4AF_FOURCC('r','t','n','g'): |
603 | case M4AF_FOURCC('s','t','i','k'): |
a56831da |
604 | if (sscanf(data, "%u", &n) == 1) |
c5c45908 |
605 | m4af_add_itmf_int8_tag(m4af, tag->tag, n); |
606 | break; |
607 | case M4AF_FOURCC('a','t','I','D'): |
608 | case M4AF_FOURCC('c','m','I','D'): |
609 | case M4AF_FOURCC('c','n','I','D'): |
610 | case M4AF_FOURCC('g','e','I','D'): |
611 | case M4AF_FOURCC('s','f','I','D'): |
612 | case M4AF_FOURCC('t','v','s','n'): |
613 | case M4AF_FOURCC('t','v','s','s'): |
a56831da |
614 | if (sscanf(data, "%u", &n) == 1) |
c5c45908 |
615 | m4af_add_itmf_int32_tag(m4af, tag->tag, n); |
616 | break; |
617 | case M4AF_FOURCC('p','l','I','D'): |
618 | { |
619 | int64_t qn; |
a56831da |
620 | if (sscanf(data, "%" SCNd64, &qn) == 1) |
c5c45908 |
621 | m4af_add_itmf_int64_tag(m4af, tag->tag, qn); |
622 | break; |
623 | } |
624 | case M4AF_TAG_ARTWORK: |
625 | { |
626 | int data_type = 0; |
a56831da |
627 | if (!memcmp(data, "GIF", 3)) |
c5c45908 |
628 | data_type = M4AF_GIF; |
a56831da |
629 | else if (!memcmp(data, "\xff\xd8\xff", 3)) |
c5c45908 |
630 | data_type = M4AF_JPEG; |
a56831da |
631 | else if (!memcmp(data, "\x89PNG", 4)) |
c5c45908 |
632 | data_type = M4AF_PNG; |
633 | if (data_type) |
634 | m4af_add_itmf_short_tag(m4af, tag->tag, data_type, |
a56831da |
635 | data, data_size); |
c5c45908 |
636 | break; |
637 | } |
638 | case M4AF_FOURCC('-','-','-','-'): |
639 | { |
a56831da |
640 | char *u8 = aacenc_to_utf8(data); |
c5c45908 |
641 | m4af_add_itmf_long_tag(m4af, tag->name, u8); |
642 | free(u8); |
643 | break; |
644 | } |
645 | case M4AF_TAG_TITLE: |
646 | case M4AF_TAG_ARTIST: |
647 | case M4AF_TAG_ALBUM: |
648 | case M4AF_TAG_GENRE: |
649 | case M4AF_TAG_DATE: |
650 | case M4AF_TAG_COMPOSER: |
651 | case M4AF_TAG_GROUPING: |
652 | case M4AF_TAG_COMMENT: |
653 | case M4AF_TAG_LYRICS: |
654 | case M4AF_TAG_TOOL: |
655 | case M4AF_TAG_ALBUM_ARTIST: |
656 | case M4AF_TAG_DESCRIPTION: |
657 | case M4AF_TAG_LONG_DESCRIPTION: |
658 | case M4AF_TAG_COPYRIGHT: |
659 | case M4AF_FOURCC('a','p','I','D'): |
660 | case M4AF_FOURCC('c','a','t','g'): |
661 | case M4AF_FOURCC('k','e','y','w'): |
662 | case M4AF_FOURCC('p','u','r','d'): |
663 | case M4AF_FOURCC('p','u','r','l'): |
664 | case M4AF_FOURCC('s','o','a','a'): |
665 | case M4AF_FOURCC('s','o','a','l'): |
666 | case M4AF_FOURCC('s','o','a','r'): |
667 | case M4AF_FOURCC('s','o','c','o'): |
668 | case M4AF_FOURCC('s','o','n','m'): |
669 | case M4AF_FOURCC('s','o','s','n'): |
670 | case M4AF_FOURCC('t','v','e','n'): |
671 | case M4AF_FOURCC('t','v','n','n'): |
672 | case M4AF_FOURCC('t','v','s','h'): |
e476728e |
673 | case M4AF_FOURCC('x','i','d',' '): |
c5c45908 |
674 | case M4AF_FOURCC('\xa9','e','n','c'): |
675 | case M4AF_FOURCC('\xa9','s','t','3'): |
676 | { |
a56831da |
677 | char *u8 = aacenc_to_utf8(data); |
c5c45908 |
678 | m4af_add_itmf_string_tag(m4af, tag->tag, u8); |
679 | free(u8); |
680 | break; |
681 | } |
682 | default: |
683 | fprintf(stderr, "WARNING: unknown/unsupported tag: %c%c%c%c\n", |
684 | tag->tag >> 24, (tag->tag >> 16) & 0xff, |
685 | (tag->tag >> 8) & 0xff, tag->tag & 0xff); |
686 | } |
a56831da |
687 | if (file_contents) free(file_contents); |
c5c45908 |
688 | } |
689 | |
690 | static |
e4bbeeb0 |
691 | void put_tool_tag(m4af_ctx_t *m4af, const aacenc_param_ex_t *params, |
c5c45908 |
692 | HANDLE_AACENCODER encoder) |
693 | { |
694 | char tool_info[256]; |
695 | char *p = tool_info; |
696 | LIB_INFO *lib_info = 0; |
697 | |
698 | p += sprintf(p, PROGNAME " %s, ", fdkaac_version); |
699 | |
700 | lib_info = calloc(FDK_MODULE_LAST, sizeof(LIB_INFO)); |
701 | if (aacEncGetLibInfo(lib_info) == AACENC_OK) { |
702 | int i; |
703 | for (i = 0; i < FDK_MODULE_LAST; ++i) |
704 | if (lib_info[i].module_id == FDK_AACENC) |
705 | break; |
706 | p += sprintf(p, "libfdk-aac %s, ", lib_info[i].versionStr); |
707 | } |
708 | free(lib_info); |
709 | if (params->bitrate_mode) |
710 | sprintf(p, "VBR mode %d", params->bitrate_mode); |
711 | else |
712 | sprintf(p, "CBR %dkbps", |
713 | aacEncoder_GetParam(encoder, AACENC_BITRATE) / 1000); |
714 | |
715 | m4af_add_itmf_string_tag(m4af, M4AF_TAG_TOOL, tool_info); |
716 | } |
717 | |
48e2f01c |
718 | static |
e4bbeeb0 |
719 | int finalize_m4a(m4af_ctx_t *m4af, const aacenc_param_ex_t *params, |
48e2f01c |
720 | HANDLE_AACENCODER encoder) |
721 | { |
722 | unsigned i; |
723 | aacenc_tag_entry_t *tag = params->tag_table; |
c5c45908 |
724 | for (i = 0; i < params->tag_count; ++i, ++tag) |
725 | put_tag_entry(m4af, tag); |
726 | |
727 | put_tool_tag(m4af, params, encoder); |
48e2f01c |
728 | |
48e2f01c |
729 | if (m4af_finalize(m4af) < 0) { |
730 | fprintf(stderr, "ERROR: failed to finalize m4a\n"); |
731 | return -1; |
732 | } |
733 | return 0; |
734 | } |
735 | |
af8fa38d |
736 | static |
737 | char *generate_output_filename(const char *filename, const char *ext) |
738 | { |
739 | char *p = 0; |
740 | size_t ext_len = strlen(ext); |
741 | |
742 | if (strcmp(filename, "-") == 0) { |
743 | p = malloc(ext_len + 6); |
744 | sprintf(p, "stdin%s", ext); |
745 | } else { |
5888fddc |
746 | const char *base = aacenc_basename(filename); |
af8fa38d |
747 | size_t ilen = strlen(base); |
748 | const char *ext_org = strrchr(base, '.'); |
749 | if (ext_org) ilen = ext_org - base; |
750 | p = malloc(ilen + ext_len + 1); |
751 | sprintf(p, "%.*s%s", ilen, base, ext); |
752 | } |
753 | return p; |
754 | } |
755 | |
68543176 |
756 | static |
757 | int parse_raw_spec(const char *spec, pcm_sample_description_t *desc) |
758 | { |
759 | unsigned bits; |
760 | unsigned char c_type, c_endian = 'L'; |
761 | int type; |
762 | |
763 | if (sscanf(spec, "%c%u%c", &c_type, &bits, &c_endian) < 2) |
764 | return -1; |
765 | c_type = toupper(c_type); |
766 | c_endian = toupper(c_endian); |
767 | |
768 | if (c_type == 'S') |
769 | type = 1; |
770 | else if (c_type == 'U') |
771 | type = 2; |
772 | else if (c_type == 'F') |
773 | type = 4; |
774 | else |
775 | return -1; |
776 | |
777 | if (c_endian == 'B') |
778 | type |= 8; |
779 | else if (c_endian != 'L') |
780 | return -1; |
781 | |
782 | if (c_type == 'F' && bits != 32 && bits != 64) |
783 | return -1; |
784 | if (c_type != 'F' && (bits < 8 || bits > 32)) |
785 | return -1; |
786 | |
787 | desc->sample_type = type; |
788 | desc->bits_per_channel = bits; |
789 | return 0; |
790 | } |
791 | |
48e2f01c |
792 | int main(int argc, char **argv) |
793 | { |
68543176 |
794 | wav_io_context_t wav_io = { read_callback, seek_callback, tell_callback }; |
e4bbeeb0 |
795 | m4af_io_callbacks_t |
796 | m4af_io = { 0, write_callback, seek_callback, tell_callback }; |
48e2f01c |
797 | aacenc_param_ex_t params = { 0 }; |
798 | |
799 | int result = 2; |
800 | FILE *ifp = 0; |
801 | FILE *ofp = 0; |
802 | char *output_filename = 0; |
803 | wav_reader_t *wavf = 0; |
804 | HANDLE_AACENCODER encoder = 0; |
805 | AACENC_InfoStruct aacinfo = { 0 }; |
e4bbeeb0 |
806 | m4af_ctx_t *m4af = 0; |
48e2f01c |
807 | const pcm_sample_description_t *sample_format; |
808 | int downsampled_timescale = 0; |
809 | int frame_count = 0; |
7222b0b5 |
810 | struct stat stb = { 0 }; |
48e2f01c |
811 | |
812 | setlocale(LC_CTYPE, ""); |
813 | setbuf(stderr, 0); |
814 | |
815 | if (parse_options(argc, argv, ¶ms) < 0) |
816 | return 1; |
817 | |
818 | if ((ifp = aacenc_fopen(params.input_filename, "rb")) == 0) { |
819 | aacenc_fprintf(stderr, "ERROR: %s: %s\n", params.input_filename, |
820 | strerror(errno)); |
821 | goto END; |
822 | } |
68543176 |
823 | if (fstat(fileno(ifp), &stb) == 0 && (stb.st_mode & S_IFMT) != S_IFREG) { |
48e2f01c |
824 | wav_io.seek = 0; |
68543176 |
825 | wav_io.tell = 0; |
826 | } |
827 | if (!params.is_raw) { |
828 | if ((wavf = wav_open(&wav_io, ifp, params.ignore_length)) == 0) { |
829 | fprintf(stderr, "ERROR: broken / unsupported input file\n"); |
830 | goto END; |
831 | } |
832 | } else { |
833 | int bytes_per_channel; |
834 | pcm_sample_description_t desc = { 0 }; |
835 | if (parse_raw_spec(params.raw_format, &desc) < 0) { |
836 | fprintf(stderr, "ERROR: invalid raw-format spec\n"); |
837 | goto END; |
838 | } |
839 | desc.sample_rate = params.raw_rate; |
840 | desc.channels_per_frame = params.raw_channels; |
841 | bytes_per_channel = (desc.bits_per_channel + 7) / 8; |
842 | desc.bytes_per_frame = params.raw_channels * bytes_per_channel; |
843 | if ((wavf = raw_open(&wav_io, ifp, &desc)) == 0) { |
844 | fprintf(stderr, "ERROR: failed to open raw input\n"); |
845 | goto END; |
846 | } |
48e2f01c |
847 | } |
848 | sample_format = wav_get_format(wavf); |
849 | |
850 | if (aacenc_init(&encoder, (aacenc_param_t*)¶ms, sample_format, |
851 | &aacinfo) < 0) |
852 | goto END; |
853 | |
854 | if (!params.output_filename) { |
af8fa38d |
855 | const char *ext = params.transport_format ? ".aac" : ".m4a"; |
856 | output_filename = generate_output_filename(params.input_filename, ext); |
48e2f01c |
857 | params.output_filename = output_filename; |
858 | } |
859 | |
860 | if ((ofp = aacenc_fopen(params.output_filename, "wb")) == 0) { |
861 | aacenc_fprintf(stderr, "ERROR: %s: %s\n", params.output_filename, |
862 | strerror(errno)); |
863 | goto END; |
864 | } |
bd3b4b34 |
865 | handle_signals(); |
48e2f01c |
866 | if (!params.transport_format) { |
867 | uint32_t scale; |
868 | unsigned framelen = aacinfo.frameLength; |
7b1f2136 |
869 | int sbr_mode = aacenc_is_sbr_active((aacenc_param_t*)¶ms); |
870 | int sig_mode = aacEncoder_GetParam(encoder, AACENC_SIGNALING_MODE); |
871 | if (sbr_mode && !sig_mode) |
872 | downsampled_timescale = 1; |
48e2f01c |
873 | scale = sample_format->sample_rate >> downsampled_timescale; |
874 | if ((m4af = m4af_create(M4AF_CODEC_MP4A, scale, &m4af_io, ofp)) < 0) |
875 | goto END; |
e4bbeeb0 |
876 | m4af_set_decoder_specific_info(m4af, 0, aacinfo.confBuf, |
877 | aacinfo.confSize); |
48e2f01c |
878 | m4af_set_fixed_frame_duration(m4af, 0, |
879 | framelen >> downsampled_timescale); |
880 | m4af_begin_write(m4af); |
881 | } |
aa2ca1e3 |
882 | frame_count = encode(wavf, encoder, aacinfo.frameLength, ofp, m4af, |
883 | !params.silent); |
48e2f01c |
884 | if (frame_count < 0) |
885 | goto END; |
886 | if (m4af) { |
887 | uint32_t delay = aacinfo.encoderDelay; |
7b1f2136 |
888 | int64_t frames_read = wav_get_position(wavf); |
48e2f01c |
889 | uint32_t padding = frame_count * aacinfo.frameLength |
890 | - frames_read - aacinfo.encoderDelay; |
891 | m4af_set_priming(m4af, 0, delay >> downsampled_timescale, |
892 | padding >> downsampled_timescale); |
893 | if (finalize_m4a(m4af, ¶ms, encoder) < 0) |
894 | goto END; |
895 | } |
896 | result = 0; |
897 | END: |
898 | if (wavf) wav_teardown(&wavf); |
899 | if (ifp) fclose(ifp); |
900 | if (m4af) m4af_teardown(&m4af); |
901 | if (ofp) fclose(ofp); |
902 | if (encoder) aacEncClose(&encoder); |
903 | if (output_filename) free(output_filename); |
904 | if (params.tag_table) free(params.tag_table); |
905 | |
906 | return result; |
907 | } |