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