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 "pcm_reader.h"
43 #define PROGNAME "fdkaac"
45 static volatile int g_interrupted
= 0;
48 static void signal_handler(int signum
)
52 static void handle_signals(void)
54 int i
, sigs
[] = { SIGINT
, SIGHUP
, SIGTERM
};
55 for (i
= 0; i
< sizeof(sigs
)/sizeof(sigs
[0]); ++i
) {
57 memset(&sa
, 0, sizeof sa
);
58 sa
.sa_handler
= signal_handler
;
59 sa
.sa_flags
|= SA_RESTART
;
60 sigaction(sigs
[i
], &sa
, 0);
64 static BOOL WINAPI
signal_handler(DWORD type
)
70 static void handle_signals(void)
72 SetConsoleCtrlHandler(signal_handler
, TRUE
);
75 static void handle_signals(void)
81 int read_callback(void *cookie
, void *data
, uint32_t size
)
83 size_t rc
= fread(data
, 1, size
, (FILE*)cookie
);
84 return ferror((FILE*)cookie
) ? -1 : (int)rc
;
88 int write_callback(void *cookie
, const void *data
, uint32_t size
)
90 size_t rc
= fwrite(data
, 1, size
, (FILE*)cookie
);
91 return ferror((FILE*)cookie
) ? -1 : (int)rc
;
95 int seek_callback(void *cookie
, int64_t off
, int whence
)
97 return fseeko((FILE*)cookie
, off
, whence
);
101 int64_t tell_callback(void *cookie
)
103 return ftello((FILE*)cookie
);
111 "Usage: " PROGNAME
" [options] input_file\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"
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"
135 " -L, --lowdelay-sbr <-1|0|1> Configure SBR activity on AAC ELD\n"
136 " -1: Use ELD SBR auto configurator\n"
137 " 0: Disable SBR on ELD (default)\n"
138 " 1: Enable SBR on ELD\n"
139 " -s, --sbr-ratio <0|1|2> Controls activation of downsampled SBR\n"
140 " 0: Use lib default (default)\n"
141 " 1: downsampled SBR (default for ELD+SBR)\n"
142 " 2: dual-rate SBR (default for HE-AAC)\n"
143 " -f, --transport-format <n> Transport format\n"
144 " 0: RAW (default, muxed into M4A)\n"
149 " 10: LOAS/LATM (LATM within LOAS)\n"
150 " -C, --adts-crc-check Add CRC protection on ADTS header\n"
151 " -h, --header-period <n> StreamMuxConfig/PCE repetition period in\n"
154 " -o <filename> Output filename\n"
155 " -G, --gapless-mode <n> Encoder delay signaling for gapless playback\n"
156 " 0: iTunSMPB (default)\n"
157 " 1: ISO standard (edts + sgpd)\n"
159 " --include-sbr-delay Count SBR decoder delay in encoder delay\n"
160 " This is not iTunes compatible, but is default\n"
161 " behavior of FDK library.\n"
162 " -I, --ignorelength Ignore length of WAV header\n"
163 " -S, --silent Don't print progress messages\n"
164 " --moov-before-mdat Place moov box before mdat box on m4a output\n"
166 "Options for raw (headerless) input:\n"
167 " -R, --raw Treat input as raw (by default WAV is\n"
169 " --raw-channels <n> Number of channels (default: 2)\n"
170 " --raw-rate <n> Sample rate (default: 44100)\n"
171 " --raw-format <spec> Sample format, default is \"S16L\".\n"
172 " Spec is as follows:\n"
173 " 1st char: S(igned)|U(nsigned)|F(loat)\n"
174 " 2nd part: bits per channel\n"
175 " Last char: L(ittle)|B(ig)\n"
176 " Last char can be omitted, in which case L is\n"
177 " assumed. Spec is case insensitive, therefore\n"
178 " \"u16b\" is same as \"U16B\".\n"
181 " --title <string>\n"
182 " --artist <string>\n"
183 " --album <string>\n"
184 " --genre <string>\n"
186 " --composer <string>\n"
187 " --grouping <string>\n"
188 " --comment <string>\n"
189 " --album-artist <string>\n"
190 " --track <number[/total]>\n"
191 " --disk <number[/total]>\n"
193 " --tag <fcc>:<value> Set iTunes predefined tag with four char code.\n"
194 " --tag-from-file <fcc>:<filename>\n"
195 " Same as above, but value is read from file.\n"
196 " --long-tag <name>:<value> Set arbitrary tag as iTunes custom metadata.\n"
197 " --tag-from-json <filename[?dot_notation]>\n"
198 " Read tags from JSON. By default, tags are\n"
199 " assumed to be direct children of the root\n"
200 " object(dictionary).\n"
201 " Optionally, position of the dictionary\n"
202 " that contains tags can be specified with\n"
203 " dotted notation.\n"
205 " --tag-from-json /path/to/json?format.tags\n"
209 typedef struct aacenc_param_ex_t
{
212 char *input_filename
;
214 char *output_filename
;
216 unsigned gapless_mode
;
217 unsigned include_sbr_delay
;
218 unsigned ignore_length
;
220 int moov_before_mdat
;
223 unsigned raw_channels
;
225 const char *raw_format
;
227 aacenc_tag_store_t tags
;
228 aacenc_tag_store_t source_tags
;
229 aacenc_translate_generic_text_tag_ctx_t source_tag_ctx
;
235 int parse_options(int argc
, char **argv
, aacenc_param_ex_t
*params
)
240 #define OPT_INCLUDE_SBR_DELAY M4AF_FOURCC('s','d','l','y')
241 #define OPT_MOOV_BEFORE_MDAT M4AF_FOURCC('m','o','o','v')
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')
248 #define OPT_TAG_FROM_JSON M4AF_FOURCC('t','f','j','s')
250 static struct option long_options
[] = {
251 { "help", no_argument
, 0, 'h' },
252 { "profile", required_argument
, 0, 'p' },
253 { "bitrate", required_argument
, 0, 'b' },
254 { "bitrate-mode", required_argument
, 0, 'm' },
255 { "bandwidth", required_argument
, 0, 'w' },
256 { "afterburner", required_argument
, 0, 'a' },
257 { "lowdelay-sbr", required_argument
, 0, 'L' },
258 { "sbr-ratio", required_argument
, 0, 's' },
259 { "transport-format", required_argument
, 0, 'f' },
260 { "adts-crc-check", no_argument
, 0, 'C' },
261 { "header-period", required_argument
, 0, 'P' },
263 { "gapless-mode", required_argument
, 0, 'G' },
264 { "include-sbr-delay", no_argument
, 0, OPT_INCLUDE_SBR_DELAY
},
265 { "ignorelength", no_argument
, 0, 'I' },
266 { "silent", no_argument
, 0, 'S' },
267 { "moov-before-mdat", no_argument
, 0, OPT_MOOV_BEFORE_MDAT
},
269 { "raw", no_argument
, 0, 'R' },
270 { "raw-channels", required_argument
, 0, OPT_RAW_CHANNELS
},
271 { "raw-rate", required_argument
, 0, OPT_RAW_RATE
},
272 { "raw-format", required_argument
, 0, OPT_RAW_FORMAT
},
274 { "title", required_argument
, 0, M4AF_TAG_TITLE
},
275 { "artist", required_argument
, 0, M4AF_TAG_ARTIST
},
276 { "album", required_argument
, 0, M4AF_TAG_ALBUM
},
277 { "genre", required_argument
, 0, M4AF_TAG_GENRE
},
278 { "date", required_argument
, 0, M4AF_TAG_DATE
},
279 { "composer", required_argument
, 0, M4AF_TAG_COMPOSER
},
280 { "grouping", required_argument
, 0, M4AF_TAG_GROUPING
},
281 { "comment", required_argument
, 0, M4AF_TAG_COMMENT
},
282 { "album-artist", required_argument
, 0, M4AF_TAG_ALBUM_ARTIST
},
283 { "track", required_argument
, 0, M4AF_TAG_TRACK
},
284 { "disk", required_argument
, 0, M4AF_TAG_DISK
},
285 { "tempo", required_argument
, 0, M4AF_TAG_TEMPO
},
286 { "tag", required_argument
, 0, OPT_SHORT_TAG
},
287 { "tag-from-file", required_argument
, 0, OPT_SHORT_TAG_FILE
},
288 { "long-tag", required_argument
, 0, OPT_LONG_TAG
},
289 { "tag-from-json", required_argument
, 0, OPT_TAG_FROM_JSON
},
292 params
->afterburner
= 1;
294 aacenc_getmainargs(&argc
, &argv
);
295 while ((ch
= getopt_long(argc
, argv
, "hp:b:m:w:a:Ls:f:CP:G:Io:SR",
296 long_options
, 0)) != EOF
) {
301 if (sscanf(optarg
, "%u", &n
) != 1) {
302 fprintf(stderr
, "invalid arg for profile\n");
308 if (sscanf(optarg
, "%u", &n
) != 1) {
309 fprintf(stderr
, "invalid arg for bitrate\n");
315 if (sscanf(optarg
, "%u", &n
) != 1 || n
> 5) {
316 fprintf(stderr
, "invalid arg for bitrate-mode\n");
319 params
->bitrate_mode
= n
;
322 if (sscanf(optarg
, "%u", &n
) != 1) {
323 fprintf(stderr
, "invalid arg for bandwidth\n");
326 params
->bandwidth
= n
;
329 if (sscanf(optarg
, "%u", &n
) != 1 || n
> 1) {
330 fprintf(stderr
, "invalid arg for afterburner\n");
333 params
->afterburner
= n
;
336 if (sscanf(optarg
, "%d", &n
) != 1 || n
< -1 || n
> 1) {
337 fprintf(stderr
, "invalid arg for lowdelay-sbr\n");
340 params
->lowdelay_sbr
= n
;
343 if (sscanf(optarg
, "%u", &n
) != 1 || n
> 2) {
344 fprintf(stderr
, "invalid arg for sbr-ratio\n");
347 params
->sbr_ratio
= n
;
350 if (sscanf(optarg
, "%u", &n
) != 1) {
351 fprintf(stderr
, "invalid arg for transport-format\n");
354 params
->transport_format
= n
;
357 params
->adts_crc_check
= 1;
360 if (sscanf(optarg
, "%u", &n
) != 1) {
361 fprintf(stderr
, "invalid arg for header-period\n");
364 params
->header_period
= n
;
367 params
->output_filename
= optarg
;
370 if (sscanf(optarg
, "%u", &n
) != 1 || n
> 2) {
371 fprintf(stderr
, "invalid arg for gapless-mode\n");
374 params
->gapless_mode
= n
;
376 case OPT_INCLUDE_SBR_DELAY
:
377 params
->include_sbr_delay
= 1;
380 params
->ignore_length
= 1;
385 case OPT_MOOV_BEFORE_MDAT
:
386 params
->moov_before_mdat
= 1;
391 case OPT_RAW_CHANNELS
:
392 if (sscanf(optarg
, "%u", &n
) != 1) {
393 fprintf(stderr
, "invalid arg for raw-channels\n");
396 params
->raw_channels
= n
;
399 if (sscanf(optarg
, "%u", &n
) != 1) {
400 fprintf(stderr
, "invalid arg for raw-rate\n");
403 params
->raw_rate
= n
;
406 params
->raw_format
= optarg
;
409 case M4AF_TAG_ARTIST
:
413 case M4AF_TAG_COMPOSER
:
414 case M4AF_TAG_GROUPING
:
415 case M4AF_TAG_COMMENT
:
416 case M4AF_TAG_ALBUM_ARTIST
:
420 aacenc_add_tag_to_store(¶ms
->tags
, ch
, 0, optarg
,
424 case OPT_SHORT_TAG_FILE
:
429 unsigned fcc
= M4AF_FOURCC('-','-','-','-');
431 if ((val
= strchr(optarg
, ':')) == 0) {
432 fprintf(stderr
, "invalid arg for tag\n");
436 if (ch
== OPT_SHORT_TAG
|| ch
== OPT_SHORT_TAG_FILE
) {
438 * take care of U+00A9(COPYRIGHT SIGN).
439 * 1) if length of fcc is 3, we prepend '\xa9'.
440 * 2) U+00A9 becomes "\xc2\xa9" in UTF-8. Therefore
441 * we remove first '\xc2'.
443 if (optarg
[0] == '\xc2')
445 if ((klen
= strlen(optarg
))== 3)
447 else if (klen
!= 4) {
448 fprintf(stderr
, "invalid arg for tag\n");
451 for (; *optarg
; ++optarg
)
452 fcc
= ((fcc
<< 8) | (*optarg
& 0xff));
454 aacenc_add_tag_to_store(¶ms
->tags
, fcc
, optarg
,
456 ch
== OPT_SHORT_TAG_FILE
);
459 case OPT_TAG_FROM_JSON
:
460 params
->json_filename
= optarg
;
469 if (!params
->bitrate
&& !params
->bitrate_mode
) {
470 fprintf(stderr
, "bitrate or bitrate-mode is mandatory\n");
473 if (params
->output_filename
&& !strcmp(params
->output_filename
, "-") &&
474 !params
->transport_format
) {
475 fprintf(stderr
, "stdout streaming is not available on M4A output\n");
478 if (params
->bitrate
&& params
->bitrate
< 10000)
479 params
->bitrate
*= 1000;
481 if (params
->is_raw
) {
482 if (!params
->raw_channels
)
483 params
->raw_channels
= 2;
484 if (!params
->raw_rate
)
485 params
->raw_rate
= 44100;
486 if (!params
->raw_format
)
487 params
->raw_format
= "S16L";
489 params
->input_filename
= argv
[optind
];
494 int write_sample(FILE *ofp
, m4af_ctx_t
*m4af
, aacenc_frame_t
*frame
)
497 fwrite(frame
->data
, 1, frame
->size
, ofp
);
499 fprintf(stderr
, "ERROR: fwrite(): %s\n", strerror(errno
));
502 } else if (m4af_write_sample(m4af
, 0, frame
->data
, frame
->size
, 0) < 0) {
503 fprintf(stderr
, "ERROR: failed to write m4a sample\n");
510 int encode(aacenc_param_ex_t
*params
, pcm_reader_t
*reader
,
511 HANDLE_AACENCODER encoder
, uint32_t frame_length
,
514 int16_t *ibuf
= 0, *ip
;
515 aacenc_frame_t obuf
[2] = {{ 0 }}, *obp
;
519 int remaining
, consumed
;
520 int frames_written
= 0, encoded
= 0;
521 aacenc_progress_t progress
= { 0 };
522 const pcm_sample_description_t
*fmt
= pcm_get_format(reader
);
524 ibuf
= malloc(frame_length
* fmt
->bytes_per_frame
);
525 aacenc_progress_init(&progress
, pcm_get_length(reader
), fmt
->sample_rate
);
529 * Since we delay the write, we cannot just exit loop when interrupted.
530 * Instead, we regard it as EOF.
535 if ((nread
= pcm_read_frames(reader
, ibuf
, frame_length
)) < 0) {
536 fprintf(stderr
, "ERROR: read failed\n");
540 aacenc_progress_update(&progress
, pcm_get_position(reader
),
541 fmt
->sample_rate
* 2);
547 consumed
= aac_encode_frame(encoder
, fmt
, ip
, remaining
, obp
);
548 if (consumed
< 0) goto END
;
549 if (consumed
== 0 && obp
->size
== 0) goto DONE
;
550 if (obp
->size
== 0) break;
552 remaining
-= consumed
;
553 ip
+= consumed
* fmt
->channels_per_frame
;
556 * As we pad 1 frame at beginning and ending by our extrapolator,
557 * we want to drop them.
558 * We delay output by 1 frame by double buffering, and discard
559 * second frame and final frame from the encoder.
560 * Since sbr_header is included in the first frame (in case of
561 * SBR), we cannot discard first frame. So we pick second instead.
564 if (encoded
== 1 || encoded
== 3)
567 if (write_sample(params
->output_fp
, m4af
, &obuf
[flip
]) < 0)
570 } while (remaining
> 0);
572 * When interrupted, we haven't pulled out last extrapolated frames
573 * from the reader. Therefore, we have to write the final outcome.
576 if (write_sample(params
->output_fp
, m4af
, &obp
[flip
^1]) < 0)
583 aacenc_progress_finish(&progress
, pcm_get_position(reader
));
586 if (ibuf
) free(ibuf
);
587 if (obuf
[0].data
) free(obuf
[0].data
);
588 if (obuf
[1].data
) free(obuf
[1].data
);
593 void put_tool_tag(m4af_ctx_t
*m4af
, const aacenc_param_ex_t
*params
,
594 HANDLE_AACENCODER encoder
)
600 p
+= sprintf(p
, PROGNAME
" %s, ", fdkaac_version
);
601 aacenc_get_lib_info(&lib_info
);
602 p
+= sprintf(p
, "libfdk-aac %s, ", lib_info
.versionStr
);
603 if (params
->bitrate_mode
)
604 sprintf(p
, "VBR mode %d", params
->bitrate_mode
);
606 sprintf(p
, "CBR %dkbps",
607 aacEncoder_GetParam(encoder
, AACENC_BITRATE
) / 1000);
609 m4af_add_itmf_string_tag(m4af
, M4AF_TAG_TOOL
, tool_info
);
613 int finalize_m4a(m4af_ctx_t
*m4af
, const aacenc_param_ex_t
*params
,
614 HANDLE_AACENCODER encoder
)
617 aacenc_tag_entry_t
*tag
;
619 tag
= params
->source_tags
.tag_table
;
620 for (i
= 0; i
< params
->source_tags
.tag_count
; ++i
, ++tag
)
621 aacenc_write_tag_entry(m4af
, tag
);
623 if (params
->json_filename
)
624 aacenc_write_tags_from_json(m4af
, params
->json_filename
);
626 tag
= params
->tags
.tag_table
;
627 for (i
= 0; i
< params
->tags
.tag_count
; ++i
, ++tag
)
628 aacenc_write_tag_entry(m4af
, tag
);
630 put_tool_tag(m4af
, params
, encoder
);
632 if (m4af_finalize(m4af
, params
->moov_before_mdat
) < 0) {
633 fprintf(stderr
, "ERROR: failed to finalize m4a\n");
640 char *generate_output_filename(const char *filename
, const char *ext
)
643 size_t ext_len
= strlen(ext
);
645 if (strcmp(filename
, "-") == 0) {
646 p
= malloc(ext_len
+ 6);
647 sprintf(p
, "stdin%s", ext
);
649 const char *base
= aacenc_basename(filename
);
650 size_t ilen
= strlen(base
);
651 const char *ext_org
= strrchr(base
, '.');
652 if (ext_org
) ilen
= ext_org
- base
;
653 p
= malloc(ilen
+ ext_len
+ 1);
654 sprintf(p
, "%.*s%s", (int)ilen
, base
, ext
);
660 int parse_raw_spec(const char *spec
, pcm_sample_description_t
*desc
)
663 unsigned char c_type
, c_endian
= 'L';
666 if (sscanf(spec
, "%c%u%c", &c_type
, &bits
, &c_endian
) < 2)
668 c_type
= toupper(c_type
);
669 c_endian
= toupper(c_endian
);
673 else if (c_type
== 'U')
675 else if (c_type
== 'F')
682 else if (c_endian
!= 'L')
685 if (c_type
== 'F' && bits
!= 32 && bits
!= 64)
687 if (c_type
!= 'F' && (bits
< 8 || bits
> 32))
690 desc
->sample_type
= type
;
691 desc
->bits_per_channel
= bits
;
695 static pcm_io_vtbl_t pcm_io_vtbl
= {
696 read_callback
, seek_callback
, tell_callback
698 static pcm_io_vtbl_t pcm_io_vtbl_noseek
= { read_callback
, 0, tell_callback
};
701 pcm_reader_t
*open_input(aacenc_param_ex_t
*params
)
703 pcm_io_context_t io
= { 0 };
704 pcm_reader_t
*reader
= 0;
705 struct stat stb
= { 0 };
707 if ((params
->input_fp
= aacenc_fopen(params
->input_filename
, "rb")) == 0) {
708 aacenc_fprintf(stderr
, "ERROR: %s: %s\n", params
->input_filename
,
712 io
.cookie
= params
->input_fp
;
713 if (fstat(fileno(params
->input_fp
), &stb
) == 0
714 && (stb
.st_mode
& S_IFMT
) == S_IFREG
)
715 io
.vtbl
= &pcm_io_vtbl
;
717 io
.vtbl
= &pcm_io_vtbl_noseek
;
719 if (params
->is_raw
) {
720 int bytes_per_channel
;
721 pcm_sample_description_t desc
= { 0 };
722 if (parse_raw_spec(params
->raw_format
, &desc
) < 0) {
723 fprintf(stderr
, "ERROR: invalid raw-format spec\n");
726 desc
.sample_rate
= params
->raw_rate
;
727 desc
.channels_per_frame
= params
->raw_channels
;
728 bytes_per_channel
= (desc
.bits_per_channel
+ 7) / 8;
729 desc
.bytes_per_frame
= params
->raw_channels
* bytes_per_channel
;
730 if ((reader
= raw_open(&io
, &desc
)) == 0) {
731 fprintf(stderr
, "ERROR: failed to open raw input\n");
736 ungetc(c
= getc(params
->input_fp
), params
->input_fp
);
740 if ((reader
= wav_open(&io
, params
->ignore_length
)) == 0) {
741 fprintf(stderr
, "ERROR: broken / unsupported input file\n");
746 params
->source_tag_ctx
.add
= aacenc_add_tag_entry_to_store
;
747 params
->source_tag_ctx
.add_ctx
= ¶ms
->source_tags
;
748 if ((reader
= caf_open(&io
,
749 aacenc_translate_generic_text_tag
,
750 ¶ms
->source_tag_ctx
)) == 0) {
751 fprintf(stderr
, "ERROR: broken / unsupported input file\n");
756 fprintf(stderr
, "ERROR: unsupported input file\n");
760 reader
= pcm_open_native_converter(reader
);
761 if (reader
&& PCM_IS_FLOAT(pcm_get_format(reader
)))
762 reader
= limiter_open(reader
);
763 if (reader
&& (reader
= pcm_open_sint16_converter(reader
)) != 0)
764 reader
= extrapolater_open(reader
);
770 int main(int argc
, char **argv
)
772 static m4af_io_callbacks_t m4af_io
= {
773 read_callback
, write_callback
, seek_callback
, tell_callback
775 aacenc_param_ex_t params
= { 0 };
778 char *output_filename
= 0;
779 pcm_reader_t
*reader
= 0;
780 HANDLE_AACENCODER encoder
= 0;
781 AACENC_InfoStruct aacinfo
= { 0 };
782 m4af_ctx_t
*m4af
= 0;
783 const pcm_sample_description_t
*sample_format
;
786 unsigned scale_shift
= 0;
788 setlocale(LC_CTYPE
, "");
791 if (parse_options(argc
, argv
, ¶ms
) < 0)
794 if ((reader
= open_input(¶ms
)) == 0)
797 sample_format
= pcm_get_format(reader
);
799 sbr_mode
= aacenc_is_sbr_active((aacenc_param_t
*)¶ms
);
800 if (sbr_mode
&& !aacenc_is_sbr_ratio_available()) {
801 fprintf(stderr
, "WARNING: Only dual-rate SBR is available "
802 "for this version\n");
803 params
.sbr_ratio
= 2;
805 scale_shift
= aacenc_is_dual_rate_sbr((aacenc_param_t
*)¶ms
);
806 params
.sbr_signaling
=
807 (params
.transport_format
== TT_MP4_LOAS
) ? 2 :
808 (params
.transport_format
== TT_MP4_RAW
) ? 1 : 0;
809 if (sbr_mode
&& !scale_shift
)
810 params
.sbr_signaling
= 2;
812 if (aacenc_init(&encoder
, (aacenc_param_t
*)¶ms
, sample_format
,
816 if (!params
.output_filename
) {
817 const char *ext
= params
.transport_format
? ".aac" : ".m4a";
818 output_filename
= generate_output_filename(params
.input_filename
, ext
);
819 params
.output_filename
= output_filename
;
822 if ((params
.output_fp
= aacenc_fopen(params
.output_filename
, "wb+")) == 0) {
823 aacenc_fprintf(stderr
, "ERROR: %s: %s\n", params
.output_filename
,
829 if (!params
.transport_format
) {
831 unsigned framelen
= aacinfo
.frameLength
;
832 scale
= sample_format
->sample_rate
>> scale_shift
;
833 if ((m4af
= m4af_create(M4AF_CODEC_MP4A
, scale
, &m4af_io
,
834 params
.output_fp
)) < 0)
836 m4af_set_num_channels(m4af
, 0, sample_format
->channels_per_frame
);
837 m4af_set_decoder_specific_info(m4af
, 0,
838 aacinfo
.confBuf
, aacinfo
.confSize
);
839 m4af_set_fixed_frame_duration(m4af
, 0, framelen
>> scale_shift
);
840 m4af_set_vbr_mode(m4af
, 0, params
.bitrate_mode
);
841 m4af_set_priming_mode(m4af
, params
.gapless_mode
+ 1);
842 m4af_begin_write(m4af
);
844 if (scale_shift
&& (aacinfo
.encoderDelay
& 1)) {
846 * Since odd delay cannot be exactly expressed in downsampled scale,
847 * we push one zero frame to the encoder here, to make delay even
849 int16_t zero
[8] = { 0 };
850 aacenc_frame_t frame
= { 0 };
851 aac_encode_frame(encoder
, sample_format
, zero
, 1, &frame
);
854 frame_count
= encode(¶ms
, reader
, encoder
, aacinfo
.frameLength
, m4af
);
858 uint32_t delay
= aacinfo
.encoderDelay
;
860 int64_t frames_read
= pcm_get_position(reader
);
862 if (sbr_mode
&& params
.profile
!= AOT_ER_AAC_ELD
&&
863 !params
.include_sbr_delay
)
864 delay
-= 481 << scale_shift
;
865 if (scale_shift
&& (delay
& 1))
867 padding
= frame_count
* aacinfo
.frameLength
- frames_read
- delay
;
868 m4af_set_priming(m4af
, 0, delay
>> scale_shift
, padding
>> scale_shift
);
869 if (finalize_m4a(m4af
, ¶ms
, encoder
) < 0)
874 if (reader
) pcm_teardown(&reader
);
875 if (params
.input_fp
) fclose(params
.input_fp
);
876 if (m4af
) m4af_teardown(&m4af
);
877 if (params
.output_fp
) fclose(params
.output_fp
);
878 if (encoder
) aacEncClose(&encoder
);
879 if (output_filename
) free(output_filename
);
880 if (params
.tags
.tag_table
)
881 aacenc_free_tag_store(¶ms
.tags
);
882 if (params
.source_tags
.tag_table
)
883 aacenc_free_tag_store(¶ms
.source_tags
);