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"
37 #include "caf_reader.h"
44 #define PROGNAME "fdkaac"
46 static volatile int g_interrupted
= 0;
49 static void signal_handler(int signum
)
53 static void handle_signals(void)
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);
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 Enable ELD-SBR (AAC ELD only)\n"
136 " -f, --transport-format <n> Transport format\n"
137 " 0: RAW (default, muxed into M4A)\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"
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"
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"
159 "Options for raw (headerless) input:\n"
160 " -R, --raw Treat input as raw (by default WAV is\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"
174 " --title <string>\n"
175 " --artist <string>\n"
176 " --album <string>\n"
177 " --genre <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"
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"
198 " --tag-from-json /path/to/json?format.tags\n"
202 typedef struct aacenc_param_ex_t
{
205 char *input_filename
;
207 char *output_filename
;
209 unsigned gapless_mode
;
210 unsigned include_sbr_delay
;
211 unsigned ignore_length
;
213 int moov_before_mdat
;
216 unsigned raw_channels
;
218 const char *raw_format
;
220 aacenc_tag_store_t tags
;
221 aacenc_tag_store_t source_tags
;
222 aacenc_translate_generic_text_tag_ctx_t source_tag_ctx
;
228 int parse_options(int argc
, char **argv
, aacenc_param_ex_t
*params
)
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')
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' },
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
},
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
},
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
},
284 params
->afterburner
= 1;
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
) {
293 if (sscanf(optarg
, "%u", &n
) != 1) {
294 fprintf(stderr
, "invalid arg for profile\n");
300 if (sscanf(optarg
, "%u", &n
) != 1) {
301 fprintf(stderr
, "invalid arg for bitrate\n");
307 if (sscanf(optarg
, "%u", &n
) != 1 || n
> 5) {
308 fprintf(stderr
, "invalid arg for bitrate-mode\n");
311 params
->bitrate_mode
= n
;
314 if (sscanf(optarg
, "%u", &n
) != 1) {
315 fprintf(stderr
, "invalid arg for bandwidth\n");
318 params
->bandwidth
= n
;
321 if (sscanf(optarg
, "%u", &n
) != 1 || n
> 1) {
322 fprintf(stderr
, "invalid arg for afterburner\n");
325 params
->afterburner
= n
;
328 params
->lowdelay_sbr
= 1;
331 if (sscanf(optarg
, "%u", &n
) != 1) {
332 fprintf(stderr
, "invalid arg for transport-format\n");
335 params
->transport_format
= n
;
338 params
->adts_crc_check
= 1;
341 if (sscanf(optarg
, "%u", &n
) != 1) {
342 fprintf(stderr
, "invalid arg for header-period\n");
345 params
->header_period
= n
;
348 params
->output_filename
= optarg
;
351 if (sscanf(optarg
, "%u", &n
) != 1 || n
> 2) {
352 fprintf(stderr
, "invalid arg for gapless-mode\n");
355 params
->gapless_mode
= n
;
357 case OPT_INCLUDE_SBR_DELAY
:
358 params
->include_sbr_delay
= 1;
361 params
->ignore_length
= 1;
366 case OPT_MOOV_BEFORE_MDAT
:
367 params
->moov_before_mdat
= 1;
372 case OPT_RAW_CHANNELS
:
373 if (sscanf(optarg
, "%u", &n
) != 1) {
374 fprintf(stderr
, "invalid arg for raw-channels\n");
377 params
->raw_channels
= n
;
380 if (sscanf(optarg
, "%u", &n
) != 1) {
381 fprintf(stderr
, "invalid arg for raw-rate\n");
384 params
->raw_rate
= n
;
387 params
->raw_format
= optarg
;
390 case M4AF_TAG_ARTIST
:
394 case M4AF_TAG_COMPOSER
:
395 case M4AF_TAG_GROUPING
:
396 case M4AF_TAG_COMMENT
:
397 case M4AF_TAG_ALBUM_ARTIST
:
401 aacenc_add_tag_to_store(¶ms
->tags
, ch
, 0, optarg
,
405 case OPT_SHORT_TAG_FILE
:
410 unsigned fcc
= M4AF_FOURCC('-','-','-','-');
412 if ((val
= strchr(optarg
, ':')) == 0) {
413 fprintf(stderr
, "invalid arg for tag\n");
417 if (ch
== OPT_SHORT_TAG
|| ch
== OPT_SHORT_TAG_FILE
) {
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'.
424 if (optarg
[0] == '\xc2')
426 if ((klen
= strlen(optarg
))== 3)
428 else if (klen
!= 4) {
429 fprintf(stderr
, "invalid arg for tag\n");
432 for (; *optarg
; ++optarg
)
433 fcc
= ((fcc
<< 8) | (*optarg
& 0xff));
435 aacenc_add_tag_to_store(¶ms
->tags
, fcc
, optarg
,
437 ch
== OPT_SHORT_TAG_FILE
);
440 case OPT_TAG_FROM_JSON
:
441 params
->json_filename
= optarg
;
450 if (!params
->bitrate
&& !params
->bitrate_mode
) {
451 fprintf(stderr
, "bitrate or bitrate-mode is mandatory\n");
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");
459 if (params
->bitrate
&& params
->bitrate
< 10000)
460 params
->bitrate
*= 1000;
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";
470 params
->input_filename
= argv
[optind
];
475 int write_sample(FILE *ofp
, m4af_ctx_t
*m4af
,
476 const void *data
, uint32_t size
, uint32_t duration
)
479 fwrite(data
, 1, size
, ofp
);
481 fprintf(stderr
, "ERROR: fwrite(): %s\n", strerror(errno
));
484 } else if (m4af_write_sample(m4af
, 0, data
, size
, duration
) < 0) {
485 fprintf(stderr
, "ERROR: failed to write m4a sample\n");
492 int encode(aacenc_param_ex_t
*params
, pcm_reader_t
*reader
,
493 HANDLE_AACENCODER encoder
, uint32_t frame_length
,
496 int16_t *ibuf
= 0, *ip
;
497 aacenc_result_t obuf
[2] = {{ 0 }}, *obp
;
501 int remaining
, consumed
;
502 int frames_written
= 0, encoded
= 0;
503 aacenc_progress_t progress
= { 0 };
504 const pcm_sample_description_t
*fmt
= pcm_get_format(reader
);
506 ibuf
= malloc(frame_length
* fmt
->bytes_per_frame
);
507 aacenc_progress_init(&progress
, pcm_get_length(reader
), fmt
->sample_rate
);
511 * Since we delay the write, we cannot just exit loop when interrupted.
512 * Instead, we regard it as EOF.
517 if ((nread
= pcm_read_frames(reader
, ibuf
, frame_length
)) < 0) {
518 fprintf(stderr
, "ERROR: read failed\n");
522 aacenc_progress_update(&progress
, pcm_get_position(reader
),
523 fmt
->sample_rate
* 2);
529 consumed
= aac_encode_frame(encoder
, fmt
, ip
, remaining
, obp
);
530 if (consumed
< 0) goto END
;
531 if (consumed
== 0 && obp
->len
== 0) goto DONE
;
532 if (obp
->len
== 0) break;
534 remaining
-= consumed
;
535 ip
+= consumed
* fmt
->channels_per_frame
;
538 * As we pad 1 frame at beginning and ending by our extrapolator,
539 * we want to drop them.
540 * We delay output by 1 frame by double buffering, and discard
541 * second frame and final frame from the encoder.
542 * Since sbr_header is included in the first frame (in case of
543 * SBR), we cannot discard first frame. So we pick second instead.
546 if (encoded
== 1 || encoded
== 3)
549 if (write_sample(params
->output_fp
, m4af
, obp
->data
, obp
->len
,
553 } while (remaining
> 0);
557 aacenc_progress_finish(&progress
, pcm_get_position(reader
));
560 if (ibuf
) free(ibuf
);
561 if (obuf
[0].data
) free(obuf
[0].data
);
562 if (obuf
[1].data
) free(obuf
[1].data
);
567 void put_tool_tag(m4af_ctx_t
*m4af
, const aacenc_param_ex_t
*params
,
568 HANDLE_AACENCODER encoder
)
572 LIB_INFO
*lib_info
= 0;
574 p
+= sprintf(p
, PROGNAME
" %s, ", fdkaac_version
);
576 lib_info
= calloc(FDK_MODULE_LAST
, sizeof(LIB_INFO
));
577 if (aacEncGetLibInfo(lib_info
) == AACENC_OK
) {
579 for (i
= 0; i
< FDK_MODULE_LAST
; ++i
)
580 if (lib_info
[i
].module_id
== FDK_AACENC
)
582 p
+= sprintf(p
, "libfdk-aac %s, ", lib_info
[i
].versionStr
);
585 if (params
->bitrate_mode
)
586 sprintf(p
, "VBR mode %d", params
->bitrate_mode
);
588 sprintf(p
, "CBR %dkbps",
589 aacEncoder_GetParam(encoder
, AACENC_BITRATE
) / 1000);
591 m4af_add_itmf_string_tag(m4af
, M4AF_TAG_TOOL
, tool_info
);
595 int finalize_m4a(m4af_ctx_t
*m4af
, const aacenc_param_ex_t
*params
,
596 HANDLE_AACENCODER encoder
)
599 aacenc_tag_entry_t
*tag
;
601 tag
= params
->source_tags
.tag_table
;
602 for (i
= 0; i
< params
->source_tags
.tag_count
; ++i
, ++tag
)
603 aacenc_write_tag_entry(m4af
, tag
);
605 if (params
->json_filename
)
606 aacenc_write_tags_from_json(m4af
, params
->json_filename
);
608 tag
= params
->tags
.tag_table
;
609 for (i
= 0; i
< params
->tags
.tag_count
; ++i
, ++tag
)
610 aacenc_write_tag_entry(m4af
, tag
);
612 put_tool_tag(m4af
, params
, encoder
);
614 if (m4af_finalize(m4af
, params
->moov_before_mdat
) < 0) {
615 fprintf(stderr
, "ERROR: failed to finalize m4a\n");
622 char *generate_output_filename(const char *filename
, const char *ext
)
625 size_t ext_len
= strlen(ext
);
627 if (strcmp(filename
, "-") == 0) {
628 p
= malloc(ext_len
+ 6);
629 sprintf(p
, "stdin%s", ext
);
631 const char *base
= aacenc_basename(filename
);
632 size_t ilen
= strlen(base
);
633 const char *ext_org
= strrchr(base
, '.');
634 if (ext_org
) ilen
= ext_org
- base
;
635 p
= malloc(ilen
+ ext_len
+ 1);
636 sprintf(p
, "%.*s%s", (int)ilen
, base
, ext
);
642 int parse_raw_spec(const char *spec
, pcm_sample_description_t
*desc
)
645 unsigned char c_type
, c_endian
= 'L';
648 if (sscanf(spec
, "%c%u%c", &c_type
, &bits
, &c_endian
) < 2)
650 c_type
= toupper(c_type
);
651 c_endian
= toupper(c_endian
);
655 else if (c_type
== 'U')
657 else if (c_type
== 'F')
664 else if (c_endian
!= 'L')
667 if (c_type
== 'F' && bits
!= 32 && bits
!= 64)
669 if (c_type
!= 'F' && (bits
< 8 || bits
> 32))
672 desc
->sample_type
= type
;
673 desc
->bits_per_channel
= bits
;
677 static pcm_io_vtbl_t pcm_io_vtbl
= {
678 read_callback
, seek_callback
, tell_callback
680 static pcm_io_vtbl_t pcm_io_vtbl_noseek
= { read_callback
, 0, 0 };
683 pcm_reader_t
*open_input(aacenc_param_ex_t
*params
)
685 pcm_io_context_t io
= { 0 };
686 pcm_reader_t
*reader
= 0;
687 struct stat stb
= { 0 };
689 if ((params
->input_fp
= aacenc_fopen(params
->input_filename
, "rb")) == 0) {
690 aacenc_fprintf(stderr
, "ERROR: %s: %s\n", params
->input_filename
,
694 io
.cookie
= params
->input_fp
;
695 if (fstat(fileno(params
->input_fp
), &stb
) == 0
696 && (stb
.st_mode
& S_IFMT
) == S_IFREG
)
697 io
.vtbl
= &pcm_io_vtbl
;
699 io
.vtbl
= &pcm_io_vtbl_noseek
;
701 if (params
->is_raw
) {
702 int bytes_per_channel
;
703 pcm_sample_description_t desc
= { 0 };
704 if (parse_raw_spec(params
->raw_format
, &desc
) < 0) {
705 fprintf(stderr
, "ERROR: invalid raw-format spec\n");
708 desc
.sample_rate
= params
->raw_rate
;
709 desc
.channels_per_frame
= params
->raw_channels
;
710 bytes_per_channel
= (desc
.bits_per_channel
+ 7) / 8;
711 desc
.bytes_per_frame
= params
->raw_channels
* bytes_per_channel
;
712 if ((reader
= raw_open(&io
, &desc
)) == 0) {
713 fprintf(stderr
, "ERROR: failed to open raw input\n");
718 ungetc(c
= getc(params
->input_fp
), params
->input_fp
);
722 if ((reader
= wav_open(&io
, params
->ignore_length
)) == 0) {
723 fprintf(stderr
, "ERROR: broken / unsupported input file\n");
728 params
->source_tag_ctx
.add
= aacenc_add_tag_entry_to_store
;
729 params
->source_tag_ctx
.add_ctx
= ¶ms
->source_tags
;
730 if ((reader
= caf_open(&io
,
731 aacenc_translate_generic_text_tag
,
732 ¶ms
->source_tag_ctx
)) == 0) {
733 fprintf(stderr
, "ERROR: broken / unsupported input file\n");
738 fprintf(stderr
, "ERROR: unsupported input file\n");
742 if ((reader
= pcm_open_sint16_converter(reader
)) != 0)
743 reader
= extrapolater_open(reader
);
749 int main(int argc
, char **argv
)
751 static m4af_io_callbacks_t m4af_io
= {
752 read_callback
, write_callback
, seek_callback
, tell_callback
754 aacenc_param_ex_t params
= { 0 };
757 char *output_filename
= 0;
758 pcm_reader_t
*reader
= 0;
759 HANDLE_AACENCODER encoder
= 0;
760 AACENC_InfoStruct aacinfo
= { 0 };
761 m4af_ctx_t
*m4af
= 0;
762 const pcm_sample_description_t
*sample_format
;
763 int downsampled_timescale
= 0;
767 setlocale(LC_CTYPE
, "");
770 if (parse_options(argc
, argv
, ¶ms
) < 0)
773 if ((reader
= open_input(¶ms
)) == 0)
776 sample_format
= pcm_get_format(reader
);
779 * We use explicit/hierarchical signaling for LOAS.
780 * Other than that, we request implicit signaling to FDK library, then
781 * append explicit/backward-compatible signaling to ASC in case of MP4FF.
783 * Explicit/backward-compatible signaling of SBR is the most recommended
784 * way in MPEG4 part3 spec, and seems the only way supported by iTunes.
785 * Since FDK library does not support it, we have to do it on our side.
787 params
.sbr_signaling
= (params
.transport_format
== TT_MP4_LOAS
) ? 2 : 0;
789 if (aacenc_init(&encoder
, (aacenc_param_t
*)¶ms
, sample_format
,
793 if (!params
.output_filename
) {
794 const char *ext
= params
.transport_format
? ".aac" : ".m4a";
795 output_filename
= generate_output_filename(params
.input_filename
, ext
);
796 params
.output_filename
= output_filename
;
799 if ((params
.output_fp
= aacenc_fopen(params
.output_filename
, "wb+")) == 0) {
800 aacenc_fprintf(stderr
, "ERROR: %s: %s\n", params
.output_filename
,
805 sbr_mode
= aacenc_is_sbr_active((aacenc_param_t
*)¶ms
);
806 if (!params
.transport_format
) {
809 uint32_t ascsize
= sizeof(mp4asc
);
810 unsigned framelen
= aacinfo
.frameLength
;
812 downsampled_timescale
= 1;
813 scale
= sample_format
->sample_rate
>> downsampled_timescale
;
814 if ((m4af
= m4af_create(M4AF_CODEC_MP4A
, scale
, &m4af_io
,
815 params
.output_fp
)) < 0)
817 aacenc_mp4asc((aacenc_param_t
*)¶ms
, aacinfo
.confBuf
,
818 aacinfo
.confSize
, mp4asc
, &ascsize
);
819 m4af_set_decoder_specific_info(m4af
, 0, mp4asc
, ascsize
);
820 m4af_set_fixed_frame_duration(m4af
, 0,
821 framelen
>> downsampled_timescale
);
822 m4af_set_vbr_mode(m4af
, 0, params
.bitrate_mode
);
823 m4af_set_priming_mode(m4af
, params
.gapless_mode
+ 1);
824 m4af_begin_write(m4af
);
826 frame_count
= encode(¶ms
, reader
, encoder
, aacinfo
.frameLength
, m4af
);
830 uint32_t delay
= aacinfo
.encoderDelay
;
832 int64_t frames_read
= pcm_get_position(reader
);
834 if (sbr_mode
&& params
.profile
!= AOT_ER_AAC_ELD
&&
835 !params
.include_sbr_delay
)
837 if (sbr_mode
&& (delay
& 1))
839 padding
= frame_count
* aacinfo
.frameLength
- frames_read
- delay
;
840 m4af_set_priming(m4af
, 0, delay
>> downsampled_timescale
,
841 padding
>> downsampled_timescale
);
842 if (finalize_m4a(m4af
, ¶ms
, encoder
) < 0)
847 if (reader
) pcm_teardown(&reader
);
848 if (params
.input_fp
) fclose(params
.input_fp
);
849 if (m4af
) m4af_teardown(&m4af
);
850 if (params
.output_fp
) fclose(params
.output_fp
);
851 if (encoder
) aacEncClose(&encoder
);
852 if (output_filename
) free(output_filename
);
853 if (params
.tags
.tag_table
)
854 aacenc_free_tag_store(¶ms
.tags
);
855 if (params
.source_tags
.tag_table
)
856 aacenc_free_tag_store(¶ms
.source_tags
);