2 * Copyright (C) 2013 nu774
3 * For conditions of distribution and use, see copyright notice in COPYING
26 #include "wav_reader.h"
32 #define PROGNAME "fdkaac"
35 int read_callback(void *cookie
, void *data
, uint32_t size
)
37 size_t rc
= fread(data
, 1, size
, (FILE*)cookie
);
38 return ferror((FILE*)cookie
) ? -1 : (int)rc
;
42 int write_callback(void *cookie
, const void *data
, uint32_t size
)
44 size_t rc
= fwrite(data
, 1, size
, (FILE*)cookie
);
45 return ferror((FILE*)cookie
) ? -1 : (int)rc
;
49 int seek_callback(void *cookie
, int64_t off
, int whence
)
51 return fseeko((FILE*)cookie
, off
, whence
);
55 int64_t tell_callback(void *cookie
)
57 return ftello((FILE*)cookie
);
65 "Usage: " PROGNAME
" [options] input_file\n"
67 " -h, --help Print this help message\n"
68 " -p, --profile <n> Profile (audio object type)\n"
69 " 2: MPEG-4 AAC LC (default)\n"
70 " 5: MPEG-4 HE-AAC (SBR)\n"
71 " 29: MPEG-4 HE-AAC v2 (SBR+PS)\n"
72 " 23: MPEG-4 AAC LD\n"
73 " 39: MPEG-4 AAC ELD\n"
74 " 129: MPEG-2 AAC LC\n"
75 " 132: MPEG-2 HE-AAC (SBR)\n"
76 " 156: MPEG-2 HE-AAC v2 (SBR+PS)\n"
77 " -b, --bitrate <n> Bitrate in bits per seconds (for CBR)\n"
78 " -m, --bitrate-mode <n> Bitrate configuration\n"
81 " (VBR mode is not officially supported, and\n"
82 " works only on a certain combination of\n"
83 " parameter settings, sample rate, and\n"
84 " channel configuration)\n"
85 " -w, --bandwidth <n> Frequency bandwidth in Hz (AAC LC only)\n"
86 " -a, --afterburner <n> Afterburner\n"
89 " -L, --lowdelay-sbr Enable ELD-SBR (AAC ELD only)\n"
90 " -s, --sbr-signaling <n> SBR signaling mode\n"
91 " 0: Implicit, backward compatible(default)\n"
92 " 1: Explicit SBR and implicit PS\n"
93 " 2: Explicit hierarchical signaling\n"
94 " -f, --transport-format <n> Transport format\n"
95 " 0: RAW (default, muxed into M4A)\n"
100 " 10: LOAS/LATM (LATM within LOAS)\n"
101 " -C, --adts-crc-check Add CRC protection on ADTS header\n"
102 " -h, --header-period <n> StreamMuxConfig/PCE repetition period in\n"
105 " -o <filename> Output filename\n"
106 " --ignore-length Ignore length of WAV header\n"
108 "Options for raw (headerless) input:\n"
109 " -R, --raw Treat input as raw (by default WAV is\n"
111 " --raw-channels <n> Number of channels (default: 2)\n"
112 " --raw-rate <n> Sample rate (default: 44100)\n"
113 " --raw-format <spec> Sample format, default is \"S16L\".\n"
114 " Spec is as follows:\n"
115 " 1st char: S(igned)|U(nsigned)|F(loat)\n"
116 " 2nd part: bits per channel\n"
117 " Last char: L(ittle)|B(ig)\n"
118 " Last char can be omitted, in which case L is\n"
119 " assumed. Spec is case insensitive, therefore\n"
120 " \"u16b\" is same as \"U16B\".\n"
123 " --title <string>\n"
124 " --artist <string>\n"
125 " --album <string>\n"
126 " --genre <string>\n"
128 " --composer <string>\n"
129 " --grouping <string>\n"
130 " --comment <string>\n"
131 " --album-artist <string>\n"
132 " --track <number[/total]>\n"
133 " --disk <number[/total]>\n"
138 typedef struct aacenc_tag_entry_t
{
141 } aacenc_tag_entry_t
;
143 typedef struct aacenc_param_ex_t
{
146 char *input_filename
;
147 char *output_filename
;
148 unsigned ignore_length
;
151 unsigned raw_channels
;
153 const char *raw_format
;
155 aacenc_tag_entry_t
*tag_table
;
157 unsigned tag_table_capacity
;
161 int parse_options(int argc
, char **argv
, aacenc_param_ex_t
*params
)
165 aacenc_tag_entry_t
*tag
;
167 #define OPT_RAW_CHANNELS M4AF_FOURCC('r','c','h','n')
168 #define OPT_RAW_RATE M4AF_FOURCC('r','r','a','t')
169 #define OPT_RAW_FORMAT M4AF_FOURCC('r','f','m','t')
171 static struct option long_options
[] = {
172 { "help", no_argument
, 0, 'h' },
173 { "profile", required_argument
, 0, 'p' },
174 { "bitrate", required_argument
, 0, 'b' },
175 { "bitrate-mode", required_argument
, 0, 'm' },
176 { "bandwidth", required_argument
, 0, 'w' },
177 { "afterburner", required_argument
, 0, 'a' },
178 { "lowdelay-sbr", no_argument
, 0, 'L' },
179 { "sbr-signaling", required_argument
, 0, 's' },
180 { "transport-format", required_argument
, 0, 'f' },
181 { "adts-crc-check", no_argument
, 0, 'C' },
182 { "header-period", required_argument
, 0, 'P' },
184 { "ignore-length", no_argument
, 0, 'I' },
186 { "raw", no_argument
, 0, 'R' },
187 { "raw-channels", required_argument
, 0, OPT_RAW_CHANNELS
},
188 { "raw-rate", required_argument
, 0, OPT_RAW_RATE
},
189 { "raw-format", required_argument
, 0, OPT_RAW_FORMAT
},
191 { "title", required_argument
, 0, M4AF_TAG_TITLE
},
192 { "artist", required_argument
, 0, M4AF_TAG_ARTIST
},
193 { "album", required_argument
, 0, M4AF_TAG_ALBUM
},
194 { "genre", required_argument
, 0, M4AF_TAG_GENRE
},
195 { "date", required_argument
, 0, M4AF_TAG_DATE
},
196 { "composer", required_argument
, 0, M4AF_TAG_COMPOSER
},
197 { "grouping", required_argument
, 0, M4AF_TAG_GROUPING
},
198 { "comment", required_argument
, 0, M4AF_TAG_COMMENT
},
199 { "album-artist", required_argument
, 0, M4AF_TAG_ALBUM_ARTIST
},
200 { "track", required_argument
, 0, M4AF_TAG_TRACK
},
201 { "disk", required_argument
, 0, M4AF_TAG_DISK
},
202 { "tempo", required_argument
, 0, M4AF_TAG_TEMPO
},
204 params
->afterburner
= 1;
206 aacenc_getmainargs(&argc
, &argv
);
207 while ((ch
= getopt_long(argc
, argv
, "hp:b:m:w:a:Ls:f:CP:Io:R",
208 long_options
, 0)) != EOF
) {
213 if (sscanf(optarg
, "%u", &n
) != 1) {
214 fprintf(stderr
, "invalid arg for profile\n");
220 if (sscanf(optarg
, "%u", &n
) != 1) {
221 fprintf(stderr
, "invalid arg for bitrate\n");
227 if (sscanf(optarg
, "%u", &n
) != 1 || n
> 5) {
228 fprintf(stderr
, "invalid arg for bitrate-mode\n");
231 params
->bitrate_mode
= n
;
234 if (sscanf(optarg
, "%u", &n
) != 1) {
235 fprintf(stderr
, "invalid arg for bandwidth\n");
238 params
->bandwidth
= n
;
241 if (sscanf(optarg
, "%u", &n
) != 1 || n
> 1) {
242 fprintf(stderr
, "invalid arg for afterburner\n");
245 params
->afterburner
= n
;
248 params
->lowdelay_sbr
= 1;
251 if (sscanf(optarg
, "%u", &n
) != 1 || n
> 2) {
252 fprintf(stderr
, "invalid arg for sbr-signaling\n");
255 params
->sbr_signaling
= n
;
258 if (sscanf(optarg
, "%u", &n
) != 1) {
259 fprintf(stderr
, "invalid arg for transport-format\n");
262 params
->transport_format
= n
;
265 params
->adts_crc_check
= 1;
268 if (sscanf(optarg
, "%u", &n
) != 1) {
269 fprintf(stderr
, "invalid arg for header-period\n");
272 params
->header_period
= n
;
275 params
->output_filename
= optarg
;
278 params
->ignore_length
= 1;
283 case OPT_RAW_CHANNELS
:
284 if (sscanf(optarg
, "%u", &n
) != 1) {
285 fprintf(stderr
, "invalid arg for raw-channels\n");
288 params
->raw_channels
= n
;
291 if (sscanf(optarg
, "%u", &n
) != 1) {
292 fprintf(stderr
, "invalid arg for raw-rate\n");
295 params
->raw_rate
= n
;
298 params
->raw_format
= optarg
;
301 case M4AF_TAG_ARTIST
:
305 case M4AF_TAG_COMPOSER
:
306 case M4AF_TAG_GROUPING
:
307 case M4AF_TAG_COMMENT
:
308 case M4AF_TAG_ALBUM_ARTIST
:
312 if (params
->tag_count
== params
->tag_table_capacity
) {
313 unsigned newsize
= params
->tag_table_capacity
;
314 newsize
= newsize
? newsize
* 2 : 1;
316 realloc(params
->tag_table
,
317 newsize
* sizeof(aacenc_tag_entry_t
));
318 params
->tag_table_capacity
= newsize
;
320 tag
= params
->tag_table
+ params
->tag_count
;
332 if (!params
->bitrate
&& !params
->bitrate_mode
) {
333 fprintf(stderr
, "bitrate or bitrate-mode is mandatory\n");
336 if (params
->output_filename
&& !strcmp(params
->output_filename
, "-") &&
337 !params
->transport_format
) {
338 fprintf(stderr
, "stdout streaming is not available on M4A output\n");
341 if (params
->bitrate
&& params
->bitrate
< 10000)
342 params
->bitrate
*= 1000;
344 if (params
->is_raw
) {
345 if (!params
->raw_channels
)
346 params
->raw_channels
= 2;
347 if (!params
->raw_rate
)
348 params
->raw_rate
= 44100;
349 if (!params
->raw_format
)
350 params
->raw_format
= "S16L";
352 params
->input_filename
= argv
[optind
];
357 int write_sample(FILE *ofp
, m4af_writer_t
*m4af
,
358 const void *data
, uint32_t size
, uint32_t duration
)
361 fwrite(data
, 1, size
, ofp
);
363 fprintf(stderr
, "ERROR: fwrite(): %s\n", strerror(errno
));
366 } else if (m4af_write_sample(m4af
, 0, data
, size
, duration
) < 0) {
367 fprintf(stderr
, "ERROR: failed to write m4a sample\n");
374 int encode(wav_reader_t
*wavf
, HANDLE_AACENCODER encoder
,
375 uint32_t frame_length
, FILE *ofp
, m4af_writer_t
*m4af
)
379 uint32_t pcmsize
= 0;
386 int frames_written
= 0;
387 aacenc_progress_t progress
= { 0 };
388 const pcm_sample_description_t
*format
= wav_get_format(wavf
);
390 ibuf
= malloc(frame_length
* format
->bytes_per_frame
);
391 aacenc_progress_init(&progress
, wav_get_length(wavf
), format
->sample_rate
);
394 if ((nread
= wav_read_frames(wavf
, ibuf
, frame_length
)) < 0) {
395 fprintf(stderr
, "ERROR: read failed\n");
397 } else if (nread
> 0) {
398 if (pcm_convert_to_native_sint16(format
, ibuf
, nread
,
399 &pcmbuf
, &pcmsize
) < 0) {
400 fprintf(stderr
, "ERROR: unsupported sample format\n");
404 aacenc_progress_update(&progress
, wav_get_position(wavf
),
405 format
->sample_rate
* 2);
407 if ((consumed
= aac_encode_frame(encoder
, format
, pcmbuf
, nread
,
408 &obuf
, &olen
, &osize
)) < 0)
411 if (write_sample(ofp
, m4af
, obuf
, olen
, frame_length
) < 0)
415 } while (nread
> 0 || olen
> 0);
416 aacenc_progress_finish(&progress
, wav_get_position(wavf
));
419 if (ibuf
) free(ibuf
);
420 if (pcmbuf
) free(pcmbuf
);
421 if (obuf
) free(obuf
);
426 int finalize_m4a(m4af_writer_t
*m4af
, const aacenc_param_ex_t
*params
,
427 HANDLE_AACENCODER encoder
)
430 aacenc_tag_entry_t
*tag
= params
->tag_table
;
432 for (i
= 0; i
< params
->tag_count
; ++i
, ++tag
) {
437 if (sscanf(tag
->data
, "%u/%u", &m
, &n
) >= 1)
438 m4af_add_itmf_track_tag(m4af
, m
, n
);
444 if (sscanf(tag
->data
, "%u/%u", &m
, &n
) >= 1)
445 m4af_add_itmf_disk_tag(m4af
, m
, n
);
451 if (sscanf(tag
->data
, "%u", &n
) == 1)
452 m4af_add_itmf_int16_tag(m4af
, tag
->tag
, n
);
457 char *u8
= aacenc_to_utf8(tag
->data
);
458 m4af_add_itmf_string_tag(m4af
, tag
->tag
, u8
);
466 LIB_INFO
*lib_info
= 0;
468 p
+= sprintf(p
, PROGNAME
" %s, ", fdkaac_version
);
470 lib_info
= calloc(FDK_MODULE_LAST
, sizeof(LIB_INFO
));
471 if (aacEncGetLibInfo(lib_info
) == AACENC_OK
) {
472 for (i
= 0; i
< FDK_MODULE_LAST
; ++i
)
473 if (lib_info
[i
].module_id
== FDK_AACENC
)
475 p
+= sprintf(p
, "libfdk-aac %s, ", lib_info
[i
].versionStr
);
478 if (params
->bitrate_mode
)
479 sprintf(p
, "VBR mode %d", params
->bitrate_mode
);
481 sprintf(p
, "CBR %dkbps",
482 aacEncoder_GetParam(encoder
, AACENC_BITRATE
) / 1000);
484 m4af_add_itmf_string_tag(m4af
, M4AF_TAG_TOOL
, tool_info
);
486 if (m4af_finalize(m4af
) < 0) {
487 fprintf(stderr
, "ERROR: failed to finalize m4a\n");
494 const char *basename(const char *filename
)
496 char *p
= strrchr(filename
, '/');
498 char *q
= strrchr(filename
, '\\');
501 return p
? p
+ 1 : filename
;
505 char *generate_output_filename(const char *filename
, const char *ext
)
508 size_t ext_len
= strlen(ext
);
510 if (strcmp(filename
, "-") == 0) {
511 p
= malloc(ext_len
+ 6);
512 sprintf(p
, "stdin%s", ext
);
514 const char *base
= basename(filename
);
515 size_t ilen
= strlen(base
);
516 const char *ext_org
= strrchr(base
, '.');
517 if (ext_org
) ilen
= ext_org
- base
;
518 p
= malloc(ilen
+ ext_len
+ 1);
519 sprintf(p
, "%.*s%s", ilen
, base
, ext
);
525 int parse_raw_spec(const char *spec
, pcm_sample_description_t
*desc
)
528 unsigned char c_type
, c_endian
= 'L';
531 if (sscanf(spec
, "%c%u%c", &c_type
, &bits
, &c_endian
) < 2)
533 c_type
= toupper(c_type
);
534 c_endian
= toupper(c_endian
);
538 else if (c_type
== 'U')
540 else if (c_type
== 'F')
547 else if (c_endian
!= 'L')
550 if (c_type
== 'F' && bits
!= 32 && bits
!= 64)
552 if (c_type
!= 'F' && (bits
< 8 || bits
> 32))
555 desc
->sample_type
= type
;
556 desc
->bits_per_channel
= bits
;
560 int main(int argc
, char **argv
)
562 wav_io_context_t wav_io
= { read_callback
, seek_callback
, tell_callback
};
563 m4af_io_callbacks_t m4af_io
= {
564 write_callback
, seek_callback
, tell_callback
};
565 aacenc_param_ex_t params
= { 0 };
570 char *output_filename
= 0;
571 wav_reader_t
*wavf
= 0;
572 HANDLE_AACENCODER encoder
= 0;
573 AACENC_InfoStruct aacinfo
= { 0 };
574 m4af_writer_t
*m4af
= 0;
575 const pcm_sample_description_t
*sample_format
;
576 int downsampled_timescale
= 0;
578 struct stat stb
= { 0 };
580 setlocale(LC_CTYPE
, "");
583 if (parse_options(argc
, argv
, ¶ms
) < 0)
586 if ((ifp
= aacenc_fopen(params
.input_filename
, "rb")) == 0) {
587 aacenc_fprintf(stderr
, "ERROR: %s: %s\n", params
.input_filename
,
591 if (fstat(fileno(ifp
), &stb
) == 0 && (stb
.st_mode
& S_IFMT
) != S_IFREG
) {
595 if (!params
.is_raw
) {
596 if ((wavf
= wav_open(&wav_io
, ifp
, params
.ignore_length
)) == 0) {
597 fprintf(stderr
, "ERROR: broken / unsupported input file\n");
601 int bytes_per_channel
;
602 pcm_sample_description_t desc
= { 0 };
603 if (parse_raw_spec(params
.raw_format
, &desc
) < 0) {
604 fprintf(stderr
, "ERROR: invalid raw-format spec\n");
607 desc
.sample_rate
= params
.raw_rate
;
608 desc
.channels_per_frame
= params
.raw_channels
;
609 bytes_per_channel
= (desc
.bits_per_channel
+ 7) / 8;
610 desc
.bytes_per_frame
= params
.raw_channels
* bytes_per_channel
;
611 if ((wavf
= raw_open(&wav_io
, ifp
, &desc
)) == 0) {
612 fprintf(stderr
, "ERROR: failed to open raw input\n");
616 sample_format
= wav_get_format(wavf
);
618 if (aacenc_init(&encoder
, (aacenc_param_t
*)¶ms
, sample_format
,
622 if (!params
.output_filename
) {
623 const char *ext
= params
.transport_format
? ".aac" : ".m4a";
624 output_filename
= generate_output_filename(params
.input_filename
, ext
);
625 params
.output_filename
= output_filename
;
628 if ((ofp
= aacenc_fopen(params
.output_filename
, "wb")) == 0) {
629 aacenc_fprintf(stderr
, "ERROR: %s: %s\n", params
.output_filename
,
633 if (!params
.transport_format
) {
635 unsigned framelen
= aacinfo
.frameLength
;
636 int sbr_mode
= aacenc_is_sbr_active((aacenc_param_t
*)¶ms
);
637 int sig_mode
= aacEncoder_GetParam(encoder
, AACENC_SIGNALING_MODE
);
638 if (sbr_mode
&& !sig_mode
)
639 downsampled_timescale
= 1;
640 scale
= sample_format
->sample_rate
>> downsampled_timescale
;
641 if ((m4af
= m4af_create(M4AF_CODEC_MP4A
, scale
, &m4af_io
, ofp
)) < 0)
643 m4af_decoder_specific_info(m4af
, 0, aacinfo
.confBuf
, aacinfo
.confSize
);
644 m4af_set_fixed_frame_duration(m4af
, 0,
645 framelen
>> downsampled_timescale
);
646 m4af_begin_write(m4af
);
648 frame_count
= encode(wavf
, encoder
, aacinfo
.frameLength
, ofp
, m4af
);
652 uint32_t delay
= aacinfo
.encoderDelay
;
653 int64_t frames_read
= wav_get_position(wavf
);
654 uint32_t padding
= frame_count
* aacinfo
.frameLength
655 - frames_read
- aacinfo
.encoderDelay
;
656 m4af_set_priming(m4af
, 0, delay
>> downsampled_timescale
,
657 padding
>> downsampled_timescale
);
658 if (finalize_m4a(m4af
, ¶ms
, encoder
) < 0)
663 if (wavf
) wav_teardown(&wavf
);
664 if (ifp
) fclose(ifp
);
665 if (m4af
) m4af_teardown(&m4af
);
666 if (ofp
) fclose(ofp
);
667 if (encoder
) aacEncClose(&encoder
);
668 if (output_filename
) free(output_filename
);
669 if (params
.tag_table
) free(params
.tag_table
);