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