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