2 * Copyright (C) 2013 nu774
3 * For conditions of distribution and use, see copyright notice in COPYING
12 # include <inttypes.h>
13 #elif defined(_MSC_VER)
14 # define SCNd64 "I64d"
32 #define WIN32_LEAN_AND_MEAN
36 #include "wav_reader.h"
42 #define PROGNAME "fdkaac"
44 static volatile g_interrupted
= 0;
47 static void signal_handler(int signum
)
51 static void handle_signals(void)
53 int i
, sigs
[] = { SIGINT
, SIGHUP
, SIGTERM
};
54 for (i
= 0; i
< sizeof(sigs
)/sizeof(sigs
[0]); ++i
) {
55 struct sigaction sa
= { 0 };
56 sa
.sa_handler
= signal_handler
;
57 sa
.sa_flags
|= SA_RESTART
;
58 sigaction(sigs
[i
], &sa
, 0);
62 static BOOL WINAPI
signal_handler(DWORD type
)
68 static void handle_signals(void)
70 SetConsoleCtrlHandler(signal_handler
, TRUE
);
73 static void handle_signals(void)
79 int read_callback(void *cookie
, void *data
, uint32_t size
)
81 size_t rc
= fread(data
, 1, size
, (FILE*)cookie
);
82 return ferror((FILE*)cookie
) ? -1 : (int)rc
;
86 int write_callback(void *cookie
, const void *data
, uint32_t size
)
88 size_t rc
= fwrite(data
, 1, size
, (FILE*)cookie
);
89 return ferror((FILE*)cookie
) ? -1 : (int)rc
;
93 int seek_callback(void *cookie
, int64_t off
, int whence
)
95 return fseeko((FILE*)cookie
, off
, whence
);
99 int64_t tell_callback(void *cookie
)
101 return ftello((FILE*)cookie
);
109 "Usage: " PROGNAME
" [options] input_file\n"
111 " -h, --help Print this help message\n"
112 " -p, --profile <n> Profile (audio object type)\n"
113 " 2: MPEG-4 AAC LC (default)\n"
114 " 5: MPEG-4 HE-AAC (SBR)\n"
115 " 29: MPEG-4 HE-AAC v2 (SBR+PS)\n"
116 " 23: MPEG-4 AAC LD\n"
117 " 39: MPEG-4 AAC ELD\n"
118 " 129: MPEG-2 AAC LC\n"
119 " 132: MPEG-2 HE-AAC (SBR)\n"
120 " 156: MPEG-2 HE-AAC v2 (SBR+PS)\n"
121 " -b, --bitrate <n> Bitrate in bits per seconds (for CBR)\n"
122 " -m, --bitrate-mode <n> Bitrate configuration\n"
123 " 0: CBR (default)\n"
125 " (VBR mode is not officially supported, and\n"
126 " works only on a certain combination of\n"
127 " parameter settings, sample rate, and\n"
128 " channel configuration)\n"
129 " -w, --bandwidth <n> Frequency bandwidth in Hz (AAC LC only)\n"
130 " -a, --afterburner <n> Afterburner\n"
133 " -L, --lowdelay-sbr Enable ELD-SBR (AAC ELD only)\n"
134 " -s, --sbr-signaling <n> SBR signaling mode\n"
135 " 0: Implicit, backward compatible(default)\n"
136 " 1: Explicit SBR and implicit PS\n"
137 " 2: Explicit hierarchical signaling\n"
138 " -f, --transport-format <n> Transport format\n"
139 " 0: RAW (default, muxed into M4A)\n"
144 " 10: LOAS/LATM (LATM within LOAS)\n"
145 " -C, --adts-crc-check Add CRC protection on ADTS header\n"
146 " -h, --header-period <n> StreamMuxConfig/PCE repetition period in\n"
149 " -o <filename> Output filename\n"
150 " --ignorelength Ignore length of WAV header\n"
151 " -S, --silent Don't print progress messages\n"
153 "Options for raw (headerless) input:\n"
154 " -R, --raw Treat input as raw (by default WAV is\n"
156 " --raw-channels <n> Number of channels (default: 2)\n"
157 " --raw-rate <n> Sample rate (default: 44100)\n"
158 " --raw-format <spec> Sample format, default is \"S16L\".\n"
159 " Spec is as follows:\n"
160 " 1st char: S(igned)|U(nsigned)|F(loat)\n"
161 " 2nd part: bits per channel\n"
162 " Last char: L(ittle)|B(ig)\n"
163 " Last char can be omitted, in which case L is\n"
164 " assumed. Spec is case insensitive, therefore\n"
165 " \"u16b\" is same as \"U16B\".\n"
168 " --title <string>\n"
169 " --artist <string>\n"
170 " --album <string>\n"
171 " --genre <string>\n"
173 " --composer <string>\n"
174 " --grouping <string>\n"
175 " --comment <string>\n"
176 " --album-artist <string>\n"
177 " --track <number[/total]>\n"
178 " --disk <number[/total]>\n"
180 " --tag <fcc>:<value> Set iTunes predefined tag with four char code.\n"
181 " --tag-from-file <fcc>:<filename>\n"
182 " Same as above, but value is read from file.\n"
183 " --long-tag <name>:<value> Set arbitrary tag as iTunes custom metadata.\n"
187 typedef struct aacenc_tag_entry_t
{
193 } aacenc_tag_entry_t
;
195 typedef struct aacenc_param_ex_t
{
198 char *input_filename
;
199 char *output_filename
;
200 unsigned ignore_length
;
204 unsigned raw_channels
;
206 const char *raw_format
;
208 aacenc_tag_entry_t
*tag_table
;
210 unsigned tag_table_capacity
;
214 void param_add_itmf_entry(aacenc_param_ex_t
*params
, uint32_t tag
,
215 const char *key
, const char *value
, uint32_t size
,
218 aacenc_tag_entry_t
*entry
;
219 if (params
->tag_count
== params
->tag_table_capacity
) {
220 unsigned newsize
= params
->tag_table_capacity
;
221 newsize
= newsize
? newsize
* 2 : 1;
223 realloc(params
->tag_table
, newsize
* sizeof(aacenc_tag_entry_t
));
224 params
->tag_table_capacity
= newsize
;
226 entry
= params
->tag_table
+ params
->tag_count
;
228 if (tag
== M4AF_FOURCC('-','-','-','-'))
231 entry
->data_size
= size
;
232 entry
->is_file_name
= is_file_name
;
237 int parse_options(int argc
, char **argv
, aacenc_param_ex_t
*params
)
242 #define OPT_RAW_CHANNELS M4AF_FOURCC('r','c','h','n')
243 #define OPT_RAW_RATE M4AF_FOURCC('r','r','a','t')
244 #define OPT_RAW_FORMAT M4AF_FOURCC('r','f','m','t')
245 #define OPT_SHORT_TAG M4AF_FOURCC('s','t','a','g')
246 #define OPT_SHORT_TAG_FILE M4AF_FOURCC('s','t','g','f')
247 #define OPT_LONG_TAG M4AF_FOURCC('l','t','a','g')
249 static struct option long_options
[] = {
250 { "help", no_argument
, 0, 'h' },
251 { "profile", required_argument
, 0, 'p' },
252 { "bitrate", required_argument
, 0, 'b' },
253 { "bitrate-mode", required_argument
, 0, 'm' },
254 { "bandwidth", required_argument
, 0, 'w' },
255 { "afterburner", required_argument
, 0, 'a' },
256 { "lowdelay-sbr", no_argument
, 0, 'L' },
257 { "sbr-signaling", required_argument
, 0, 's' },
258 { "transport-format", required_argument
, 0, 'f' },
259 { "adts-crc-check", no_argument
, 0, 'C' },
260 { "header-period", required_argument
, 0, 'P' },
262 { "ignorelength", no_argument
, 0, 'I' },
263 { "silent", no_argument
, 0, 'S' },
265 { "raw", no_argument
, 0, 'R' },
266 { "raw-channels", required_argument
, 0, OPT_RAW_CHANNELS
},
267 { "raw-rate", required_argument
, 0, OPT_RAW_RATE
},
268 { "raw-format", required_argument
, 0, OPT_RAW_FORMAT
},
270 { "title", required_argument
, 0, M4AF_TAG_TITLE
},
271 { "artist", required_argument
, 0, M4AF_TAG_ARTIST
},
272 { "album", required_argument
, 0, M4AF_TAG_ALBUM
},
273 { "genre", required_argument
, 0, M4AF_TAG_GENRE
},
274 { "date", required_argument
, 0, M4AF_TAG_DATE
},
275 { "composer", required_argument
, 0, M4AF_TAG_COMPOSER
},
276 { "grouping", required_argument
, 0, M4AF_TAG_GROUPING
},
277 { "comment", required_argument
, 0, M4AF_TAG_COMMENT
},
278 { "album-artist", required_argument
, 0, M4AF_TAG_ALBUM_ARTIST
},
279 { "track", required_argument
, 0, M4AF_TAG_TRACK
},
280 { "disk", required_argument
, 0, M4AF_TAG_DISK
},
281 { "tempo", required_argument
, 0, M4AF_TAG_TEMPO
},
282 { "tag", required_argument
, 0, OPT_SHORT_TAG
},
283 { "tag-from-file", required_argument
, 0, OPT_SHORT_TAG_FILE
},
284 { "long-tag", required_argument
, 0, OPT_LONG_TAG
},
287 params
->afterburner
= 1;
289 aacenc_getmainargs(&argc
, &argv
);
290 while ((ch
= getopt_long(argc
, argv
, "hp:b:m:w:a:Ls:f:CP:Io:SR",
291 long_options
, 0)) != EOF
) {
296 if (sscanf(optarg
, "%u", &n
) != 1) {
297 fprintf(stderr
, "invalid arg for profile\n");
303 if (sscanf(optarg
, "%u", &n
) != 1) {
304 fprintf(stderr
, "invalid arg for bitrate\n");
310 if (sscanf(optarg
, "%u", &n
) != 1 || n
> 5) {
311 fprintf(stderr
, "invalid arg for bitrate-mode\n");
314 params
->bitrate_mode
= n
;
317 if (sscanf(optarg
, "%u", &n
) != 1) {
318 fprintf(stderr
, "invalid arg for bandwidth\n");
321 params
->bandwidth
= n
;
324 if (sscanf(optarg
, "%u", &n
) != 1 || n
> 1) {
325 fprintf(stderr
, "invalid arg for afterburner\n");
328 params
->afterburner
= n
;
331 params
->lowdelay_sbr
= 1;
334 if (sscanf(optarg
, "%u", &n
) != 1 || n
> 2) {
335 fprintf(stderr
, "invalid arg for sbr-signaling\n");
338 params
->sbr_signaling
= n
;
341 if (sscanf(optarg
, "%u", &n
) != 1) {
342 fprintf(stderr
, "invalid arg for transport-format\n");
345 params
->transport_format
= n
;
348 params
->adts_crc_check
= 1;
351 if (sscanf(optarg
, "%u", &n
) != 1) {
352 fprintf(stderr
, "invalid arg for header-period\n");
355 params
->header_period
= n
;
358 params
->output_filename
= optarg
;
361 params
->ignore_length
= 1;
369 case OPT_RAW_CHANNELS
:
370 if (sscanf(optarg
, "%u", &n
) != 1) {
371 fprintf(stderr
, "invalid arg for raw-channels\n");
374 params
->raw_channels
= n
;
377 if (sscanf(optarg
, "%u", &n
) != 1) {
378 fprintf(stderr
, "invalid arg for raw-rate\n");
381 params
->raw_rate
= n
;
384 params
->raw_format
= optarg
;
387 case M4AF_TAG_ARTIST
:
391 case M4AF_TAG_COMPOSER
:
392 case M4AF_TAG_GROUPING
:
393 case M4AF_TAG_COMMENT
:
394 case M4AF_TAG_ALBUM_ARTIST
:
398 param_add_itmf_entry(params
, ch
, 0, optarg
, strlen(optarg
), 0);
401 case OPT_SHORT_TAG_FILE
:
406 unsigned fcc
= M4AF_FOURCC('-','-','-','-');
408 if ((val
= strchr(optarg
, ':')) == 0) {
409 fprintf(stderr
, "invalid arg for tag\n");
413 if (ch
== OPT_SHORT_TAG
|| ch
== OPT_SHORT_TAG_FILE
) {
415 * take care of U+00A9(COPYRIGHT SIGN).
416 * 1) if length of fcc is 3, we prepend '\xa9'.
417 * 2) U+00A9 becomes "\xc2\xa9" in UTF-8. Therefore
418 * we remove first '\xc2'.
420 if (optarg
[0] == '\xc2')
422 if ((klen
= strlen(optarg
))== 3)
424 else if (klen
!= 4) {
425 fprintf(stderr
, "invalid arg for tag\n");
428 for (; *optarg
; ++optarg
)
429 fcc
= ((fcc
<< 8) | (*optarg
& 0xff));
431 param_add_itmf_entry(params
, fcc
, optarg
, val
, strlen(val
),
432 ch
== OPT_SHORT_TAG_FILE
);
442 if (!params
->bitrate
&& !params
->bitrate_mode
) {
443 fprintf(stderr
, "bitrate or bitrate-mode is mandatory\n");
446 if (params
->output_filename
&& !strcmp(params
->output_filename
, "-") &&
447 !params
->transport_format
) {
448 fprintf(stderr
, "stdout streaming is not available on M4A output\n");
451 if (params
->bitrate
&& params
->bitrate
< 10000)
452 params
->bitrate
*= 1000;
454 if (params
->is_raw
) {
455 if (!params
->raw_channels
)
456 params
->raw_channels
= 2;
457 if (!params
->raw_rate
)
458 params
->raw_rate
= 44100;
459 if (!params
->raw_format
)
460 params
->raw_format
= "S16L";
462 params
->input_filename
= argv
[optind
];
467 int write_sample(FILE *ofp
, m4af_writer_t
*m4af
,
468 const void *data
, uint32_t size
, uint32_t duration
)
471 fwrite(data
, 1, size
, ofp
);
473 fprintf(stderr
, "ERROR: fwrite(): %s\n", strerror(errno
));
476 } else if (m4af_write_sample(m4af
, 0, data
, size
, duration
) < 0) {
477 fprintf(stderr
, "ERROR: failed to write m4a sample\n");
484 int encode(wav_reader_t
*wavf
, HANDLE_AACENCODER encoder
,
485 uint32_t frame_length
, FILE *ofp
, m4af_writer_t
*m4af
,
490 uint32_t pcmsize
= 0;
497 int frames_written
= 0;
498 aacenc_progress_t progress
= { 0 };
499 const pcm_sample_description_t
*format
= wav_get_format(wavf
);
501 ibuf
= malloc(frame_length
* format
->bytes_per_frame
);
502 aacenc_progress_init(&progress
, wav_get_length(wavf
), format
->sample_rate
);
507 if ((nread
= wav_read_frames(wavf
, ibuf
, frame_length
)) < 0) {
508 fprintf(stderr
, "ERROR: read failed\n");
510 } else if (nread
> 0) {
511 if (pcm_convert_to_native_sint16(format
, ibuf
, nread
,
512 &pcmbuf
, &pcmsize
) < 0) {
513 fprintf(stderr
, "ERROR: unsupported sample format\n");
518 aacenc_progress_update(&progress
, wav_get_position(wavf
),
519 format
->sample_rate
* 2);
521 if ((consumed
= aac_encode_frame(encoder
, format
, pcmbuf
, nread
,
522 &obuf
, &olen
, &osize
)) < 0)
525 if (write_sample(ofp
, m4af
, obuf
, olen
, frame_length
) < 0)
529 } while (nread
> 0 || olen
> 0);
532 aacenc_progress_finish(&progress
, wav_get_position(wavf
));
535 if (ibuf
) free(ibuf
);
536 if (pcmbuf
) free(pcmbuf
);
537 if (obuf
) free(obuf
);
542 char *load_tag_from_file(const char *path
, uint32_t *data_size
)
548 if ((fp
= aacenc_fopen(path
, "rb")) == NULL
) {
549 aacenc_fprintf(stderr
, "WARNING: %s: %s\n", path
, strerror(errno
));
552 fseeko(fp
, 0, SEEK_END
);
554 if (size
> 5*1024*1024) {
555 aacenc_fprintf(stderr
, "WARNING: %s: size too large\n", path
);
558 fseeko(fp
, 0, SEEK_SET
);
559 data
= malloc(size
+ 1);
560 if (data
) fread(data
, 1, size
, fp
);
562 *data_size
= (uint32_t)size
;
569 void put_tag_entry(m4af_writer_t
*m4af
, const aacenc_tag_entry_t
*tag
)
572 const char *data
= tag
->data
;
573 uint32_t data_size
= tag
->data_size
;
574 char *file_contents
= 0;
576 if (tag
->is_file_name
) {
577 data
= file_contents
= load_tag_from_file(tag
->data
, &data_size
);
582 if (sscanf(data
, "%u/%u", &m
, &n
) >= 1)
583 m4af_add_itmf_track_tag(m4af
, m
, n
);
586 if (sscanf(data
, "%u/%u", &m
, &n
) >= 1)
587 m4af_add_itmf_disk_tag(m4af
, m
, n
);
589 case M4AF_TAG_GENRE_ID3
:
590 if (sscanf(data
, "%u", &n
) == 1)
591 m4af_add_itmf_genre_tag(m4af
, n
);
594 if (sscanf(data
, "%u", &n
) == 1)
595 m4af_add_itmf_int16_tag(m4af
, tag
->tag
, n
);
597 case M4AF_TAG_COMPILATION
:
598 case M4AF_FOURCC('a','k','I','D'):
599 case M4AF_FOURCC('h','d','v','d'):
600 case M4AF_FOURCC('p','c','s','t'):
601 case M4AF_FOURCC('p','g','a','p'):
602 case M4AF_FOURCC('r','t','n','g'):
603 case M4AF_FOURCC('s','t','i','k'):
604 if (sscanf(data
, "%u", &n
) == 1)
605 m4af_add_itmf_int8_tag(m4af
, tag
->tag
, n
);
607 case M4AF_FOURCC('a','t','I','D'):
608 case M4AF_FOURCC('c','m','I','D'):
609 case M4AF_FOURCC('c','n','I','D'):
610 case M4AF_FOURCC('g','e','I','D'):
611 case M4AF_FOURCC('s','f','I','D'):
612 case M4AF_FOURCC('t','v','s','n'):
613 case M4AF_FOURCC('t','v','s','s'):
614 if (sscanf(data
, "%u", &n
) == 1)
615 m4af_add_itmf_int32_tag(m4af
, tag
->tag
, n
);
617 case M4AF_FOURCC('p','l','I','D'):
620 if (sscanf(data
, "%" SCNd64
, &qn
) == 1)
621 m4af_add_itmf_int64_tag(m4af
, tag
->tag
, qn
);
624 case M4AF_TAG_ARTWORK
:
627 if (!memcmp(data
, "GIF", 3))
628 data_type
= M4AF_GIF
;
629 else if (!memcmp(data
, "\xff\xd8\xff", 3))
630 data_type
= M4AF_JPEG
;
631 else if (!memcmp(data
, "\x89PNG", 4))
632 data_type
= M4AF_PNG
;
634 m4af_add_itmf_short_tag(m4af
, tag
->tag
, data_type
,
638 case M4AF_FOURCC('-','-','-','-'):
640 char *u8
= aacenc_to_utf8(data
);
641 m4af_add_itmf_long_tag(m4af
, tag
->name
, u8
);
646 case M4AF_TAG_ARTIST
:
650 case M4AF_TAG_COMPOSER
:
651 case M4AF_TAG_GROUPING
:
652 case M4AF_TAG_COMMENT
:
653 case M4AF_TAG_LYRICS
:
655 case M4AF_TAG_ALBUM_ARTIST
:
656 case M4AF_TAG_DESCRIPTION
:
657 case M4AF_TAG_LONG_DESCRIPTION
:
658 case M4AF_TAG_COPYRIGHT
:
659 case M4AF_FOURCC('a','p','I','D'):
660 case M4AF_FOURCC('c','a','t','g'):
661 case M4AF_FOURCC('k','e','y','w'):
662 case M4AF_FOURCC('p','u','r','d'):
663 case M4AF_FOURCC('p','u','r','l'):
664 case M4AF_FOURCC('s','o','a','a'):
665 case M4AF_FOURCC('s','o','a','l'):
666 case M4AF_FOURCC('s','o','a','r'):
667 case M4AF_FOURCC('s','o','c','o'):
668 case M4AF_FOURCC('s','o','n','m'):
669 case M4AF_FOURCC('s','o','s','n'):
670 case M4AF_FOURCC('t','v','e','n'):
671 case M4AF_FOURCC('t','v','n','n'):
672 case M4AF_FOURCC('t','v','s','h'):
673 case M4AF_FOURCC('x','i','d',' '):
674 case M4AF_FOURCC('\xa9','e','n','c'):
675 case M4AF_FOURCC('\xa9','s','t','3'):
677 char *u8
= aacenc_to_utf8(data
);
678 m4af_add_itmf_string_tag(m4af
, tag
->tag
, u8
);
683 fprintf(stderr
, "WARNING: unknown/unsupported tag: %c%c%c%c\n",
684 tag
->tag
>> 24, (tag
->tag
>> 16) & 0xff,
685 (tag
->tag
>> 8) & 0xff, tag
->tag
& 0xff);
687 if (file_contents
) free(file_contents
);
691 void put_tool_tag(m4af_writer_t
*m4af
, const aacenc_param_ex_t
*params
,
692 HANDLE_AACENCODER encoder
)
696 LIB_INFO
*lib_info
= 0;
698 p
+= sprintf(p
, PROGNAME
" %s, ", fdkaac_version
);
700 lib_info
= calloc(FDK_MODULE_LAST
, sizeof(LIB_INFO
));
701 if (aacEncGetLibInfo(lib_info
) == AACENC_OK
) {
703 for (i
= 0; i
< FDK_MODULE_LAST
; ++i
)
704 if (lib_info
[i
].module_id
== FDK_AACENC
)
706 p
+= sprintf(p
, "libfdk-aac %s, ", lib_info
[i
].versionStr
);
709 if (params
->bitrate_mode
)
710 sprintf(p
, "VBR mode %d", params
->bitrate_mode
);
712 sprintf(p
, "CBR %dkbps",
713 aacEncoder_GetParam(encoder
, AACENC_BITRATE
) / 1000);
715 m4af_add_itmf_string_tag(m4af
, M4AF_TAG_TOOL
, tool_info
);
719 int finalize_m4a(m4af_writer_t
*m4af
, const aacenc_param_ex_t
*params
,
720 HANDLE_AACENCODER encoder
)
723 aacenc_tag_entry_t
*tag
= params
->tag_table
;
724 for (i
= 0; i
< params
->tag_count
; ++i
, ++tag
)
725 put_tag_entry(m4af
, tag
);
727 put_tool_tag(m4af
, params
, encoder
);
729 if (m4af_finalize(m4af
) < 0) {
730 fprintf(stderr
, "ERROR: failed to finalize m4a\n");
737 char *generate_output_filename(const char *filename
, const char *ext
)
740 size_t ext_len
= strlen(ext
);
742 if (strcmp(filename
, "-") == 0) {
743 p
= malloc(ext_len
+ 6);
744 sprintf(p
, "stdin%s", ext
);
746 const char *base
= aacenc_basename(filename
);
747 size_t ilen
= strlen(base
);
748 const char *ext_org
= strrchr(base
, '.');
749 if (ext_org
) ilen
= ext_org
- base
;
750 p
= malloc(ilen
+ ext_len
+ 1);
751 sprintf(p
, "%.*s%s", ilen
, base
, ext
);
757 int parse_raw_spec(const char *spec
, pcm_sample_description_t
*desc
)
760 unsigned char c_type
, c_endian
= 'L';
763 if (sscanf(spec
, "%c%u%c", &c_type
, &bits
, &c_endian
) < 2)
765 c_type
= toupper(c_type
);
766 c_endian
= toupper(c_endian
);
770 else if (c_type
== 'U')
772 else if (c_type
== 'F')
779 else if (c_endian
!= 'L')
782 if (c_type
== 'F' && bits
!= 32 && bits
!= 64)
784 if (c_type
!= 'F' && (bits
< 8 || bits
> 32))
787 desc
->sample_type
= type
;
788 desc
->bits_per_channel
= bits
;
792 int main(int argc
, char **argv
)
794 wav_io_context_t wav_io
= { read_callback
, seek_callback
, tell_callback
};
795 m4af_io_callbacks_t m4af_io
= {
796 write_callback
, seek_callback
, tell_callback
};
797 aacenc_param_ex_t params
= { 0 };
802 char *output_filename
= 0;
803 wav_reader_t
*wavf
= 0;
804 HANDLE_AACENCODER encoder
= 0;
805 AACENC_InfoStruct aacinfo
= { 0 };
806 m4af_writer_t
*m4af
= 0;
807 const pcm_sample_description_t
*sample_format
;
808 int downsampled_timescale
= 0;
810 struct stat stb
= { 0 };
812 setlocale(LC_CTYPE
, "");
815 if (parse_options(argc
, argv
, ¶ms
) < 0)
818 if ((ifp
= aacenc_fopen(params
.input_filename
, "rb")) == 0) {
819 aacenc_fprintf(stderr
, "ERROR: %s: %s\n", params
.input_filename
,
823 if (fstat(fileno(ifp
), &stb
) == 0 && (stb
.st_mode
& S_IFMT
) != S_IFREG
) {
827 if (!params
.is_raw
) {
828 if ((wavf
= wav_open(&wav_io
, ifp
, params
.ignore_length
)) == 0) {
829 fprintf(stderr
, "ERROR: broken / unsupported input file\n");
833 int bytes_per_channel
;
834 pcm_sample_description_t desc
= { 0 };
835 if (parse_raw_spec(params
.raw_format
, &desc
) < 0) {
836 fprintf(stderr
, "ERROR: invalid raw-format spec\n");
839 desc
.sample_rate
= params
.raw_rate
;
840 desc
.channels_per_frame
= params
.raw_channels
;
841 bytes_per_channel
= (desc
.bits_per_channel
+ 7) / 8;
842 desc
.bytes_per_frame
= params
.raw_channels
* bytes_per_channel
;
843 if ((wavf
= raw_open(&wav_io
, ifp
, &desc
)) == 0) {
844 fprintf(stderr
, "ERROR: failed to open raw input\n");
848 sample_format
= wav_get_format(wavf
);
850 if (aacenc_init(&encoder
, (aacenc_param_t
*)¶ms
, sample_format
,
854 if (!params
.output_filename
) {
855 const char *ext
= params
.transport_format
? ".aac" : ".m4a";
856 output_filename
= generate_output_filename(params
.input_filename
, ext
);
857 params
.output_filename
= output_filename
;
860 if ((ofp
= aacenc_fopen(params
.output_filename
, "wb")) == 0) {
861 aacenc_fprintf(stderr
, "ERROR: %s: %s\n", params
.output_filename
,
866 if (!params
.transport_format
) {
868 unsigned framelen
= aacinfo
.frameLength
;
869 int sbr_mode
= aacenc_is_sbr_active((aacenc_param_t
*)¶ms
);
870 int sig_mode
= aacEncoder_GetParam(encoder
, AACENC_SIGNALING_MODE
);
871 if (sbr_mode
&& !sig_mode
)
872 downsampled_timescale
= 1;
873 scale
= sample_format
->sample_rate
>> downsampled_timescale
;
874 if ((m4af
= m4af_create(M4AF_CODEC_MP4A
, scale
, &m4af_io
, ofp
)) < 0)
876 m4af_decoder_specific_info(m4af
, 0, aacinfo
.confBuf
, aacinfo
.confSize
);
877 m4af_set_fixed_frame_duration(m4af
, 0,
878 framelen
>> downsampled_timescale
);
879 m4af_begin_write(m4af
);
881 frame_count
= encode(wavf
, encoder
, aacinfo
.frameLength
, ofp
, m4af
,
886 uint32_t delay
= aacinfo
.encoderDelay
;
887 int64_t frames_read
= wav_get_position(wavf
);
888 uint32_t padding
= frame_count
* aacinfo
.frameLength
889 - frames_read
- aacinfo
.encoderDelay
;
890 m4af_set_priming(m4af
, 0, delay
>> downsampled_timescale
,
891 padding
>> downsampled_timescale
);
892 if (finalize_m4a(m4af
, ¶ms
, encoder
) < 0)
897 if (wavf
) wav_teardown(&wavf
);
898 if (ifp
) fclose(ifp
);
899 if (m4af
) m4af_teardown(&m4af
);
900 if (ofp
) fclose(ofp
);
901 if (encoder
) aacEncClose(&encoder
);
902 if (output_filename
) free(output_filename
);
903 if (params
.tag_table
) free(params
.tag_table
);