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