X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=src%2Fmain.c;h=1eb9a0e827c6338c9dc1095ca8e988d2e0b0eaec;hb=9b7e1ca68c3a594f6657ad1d3d151cd74b3e0228;hp=ee07e35557b1378283ecb9795821f9ff7f20c526;hpb=3de0e22dcc176fb7f34be00d3406235870ae7db4;p=fdkaac.git diff --git a/src/main.c b/src/main.c index ee07e35..1eb9a0e 100644 --- a/src/main.c +++ b/src/main.c @@ -33,8 +33,7 @@ #include #endif #include "compat.h" -#include "wav_reader.h" -#include "caf_reader.h" +#include "pcm_reader.h" #include "aacenc.h" #include "m4af.h" #include "progress.h" @@ -54,7 +53,8 @@ static void handle_signals(void) { int i, sigs[] = { SIGINT, SIGHUP, SIGTERM }; for (i = 0; i < sizeof(sigs)/sizeof(sigs[0]); ++i) { - struct sigaction sa = { 0 }; + struct sigaction sa; + memset(&sa, 0, sizeof sa); sa.sa_handler = signal_handler; sa.sa_flags |= SA_RESTART; sigaction(sigs[i], &sa, 0); @@ -117,9 +117,6 @@ PROGNAME " %s\n" " 29: MPEG-4 HE-AAC v2 (SBR+PS)\n" " 23: MPEG-4 AAC LD\n" " 39: MPEG-4 AAC ELD\n" -" 129: MPEG-2 AAC LC\n" -" 132: MPEG-2 HE-AAC (SBR)\n" -" 156: MPEG-2 HE-AAC v2 (SBR+PS)\n" " -b, --bitrate Bitrate in bits per seconds (for CBR)\n" " -m, --bitrate-mode Bitrate configuration\n" " 0: CBR (default)\n" @@ -132,7 +129,14 @@ PROGNAME " %s\n" " -a, --afterburner Afterburner\n" " 0: Off\n" " 1: On(default)\n" -" -L, --lowdelay-sbr Enable ELD-SBR (AAC ELD only)\n" +" -L, --lowdelay-sbr <-1|0|1> Configure SBR activity on AAC ELD\n" +" -1: Use ELD SBR auto configurator\n" +" 0: Disable SBR on ELD (default)\n" +" 1: Enable SBR on ELD\n" +" -s, --sbr-ratio <0|1|2> Controls activation of downsampled SBR\n" +" 0: Use lib default (default)\n" +" 1: downsampled SBR (default for ELD+SBR)\n" +" 2: dual-rate SBR (default for HE-AAC)\n" " -f, --transport-format Transport format\n" " 0: RAW (default, muxed into M4A)\n" " 1: ADIF\n" @@ -228,7 +232,7 @@ static int parse_options(int argc, char **argv, aacenc_param_ex_t *params) { int ch; - unsigned n; + int n; #define OPT_INCLUDE_SBR_DELAY M4AF_FOURCC('s','d','l','y') #define OPT_MOOV_BEFORE_MDAT M4AF_FOURCC('m','o','o','v') @@ -247,7 +251,8 @@ int parse_options(int argc, char **argv, aacenc_param_ex_t *params) { "bitrate-mode", required_argument, 0, 'm' }, { "bandwidth", required_argument, 0, 'w' }, { "afterburner", required_argument, 0, 'a' }, - { "lowdelay-sbr", no_argument, 0, 'L' }, + { "lowdelay-sbr", required_argument, 0, 'L' }, + { "sbr-ratio", required_argument, 0, 's' }, { "transport-format", required_argument, 0, 'f' }, { "adts-crc-check", no_argument, 0, 'C' }, { "header-period", required_argument, 0, 'P' }, @@ -325,7 +330,18 @@ int parse_options(int argc, char **argv, aacenc_param_ex_t *params) params->afterburner = n; break; case 'L': - params->lowdelay_sbr = 1; + if (sscanf(optarg, "%d", &n) != 1 || n < -1 || n > 1) { + fprintf(stderr, "invalid arg for lowdelay-sbr\n"); + return -1; + } + params->lowdelay_sbr = n; + break; + case 's': + if (sscanf(optarg, "%u", &n) != 1 || n > 2) { + fprintf(stderr, "invalid arg for sbr-ratio\n"); + return -1; + } + params->sbr_ratio = n; break; case 'f': if (sscanf(optarg, "%u", &n) != 1) { @@ -544,11 +560,20 @@ int encode(aacenc_param_ex_t *params, pcm_reader_t *reader, ++encoded; if (encoded == 1 || encoded == 3) continue; - obp = &obuf[flip]; - if (write_sample(params->output_fp, m4af, obp) < 0) + + if (write_sample(params->output_fp, m4af, &obuf[flip]) < 0) goto END; ++frames_written; } while (remaining > 0); + /* + * When interrupted, we haven't pulled out last extrapolated frames + * from the reader. Therefore, we have to write the final outcome. + */ + if (g_interrupted) { + if (write_sample(params->output_fp, m4af, &obp[flip^1]) < 0) + goto END; + ++frames_written; + } } DONE: if (!params->silent) @@ -567,19 +592,11 @@ void put_tool_tag(m4af_ctx_t *m4af, const aacenc_param_ex_t *params, { char tool_info[256]; char *p = tool_info; - LIB_INFO *lib_info = 0; + LIB_INFO lib_info; p += sprintf(p, PROGNAME " %s, ", fdkaac_version); - - lib_info = calloc(FDK_MODULE_LAST, sizeof(LIB_INFO)); - if (aacEncGetLibInfo(lib_info) == AACENC_OK) { - int i; - for (i = 0; i < FDK_MODULE_LAST; ++i) - if (lib_info[i].module_id == FDK_AACENC) - break; - p += sprintf(p, "libfdk-aac %s, ", lib_info[i].versionStr); - } - free(lib_info); + aacenc_get_lib_info(&lib_info); + p += sprintf(p, "libfdk-aac %s, ", lib_info.versionStr); if (params->bitrate_mode) sprintf(p, "VBR mode %d", params->bitrate_mode); else @@ -682,16 +699,14 @@ pcm_reader_t *open_input(aacenc_param_ex_t *params) { pcm_io_context_t io = { 0 }; pcm_reader_t *reader = 0; - struct stat stb = { 0 }; if ((params->input_fp = aacenc_fopen(params->input_filename, "rb")) == 0) { aacenc_fprintf(stderr, "ERROR: %s: %s\n", params->input_filename, strerror(errno)); - goto END; + goto FAIL; } io.cookie = params->input_fp; - if (fstat(fileno(params->input_fp), &stb) == 0 - && (stb.st_mode & S_IFMT) == S_IFREG) + if (aacenc_seekable(params->input_fp)) io.vtbl = &pcm_io_vtbl; else io.vtbl = &pcm_io_vtbl_noseek; @@ -701,7 +716,7 @@ pcm_reader_t *open_input(aacenc_param_ex_t *params) pcm_sample_description_t desc = { 0 }; if (parse_raw_spec(params->raw_format, &desc) < 0) { fprintf(stderr, "ERROR: invalid raw-format spec\n"); - goto END; + goto FAIL; } desc.sample_rate = params->raw_rate; desc.channels_per_frame = params->raw_channels; @@ -709,7 +724,7 @@ pcm_reader_t *open_input(aacenc_param_ex_t *params) desc.bytes_per_frame = params->raw_channels * bytes_per_channel; if ((reader = raw_open(&io, &desc)) == 0) { fprintf(stderr, "ERROR: failed to open raw input\n"); - goto END; + goto FAIL; } } else { int c; @@ -719,7 +734,7 @@ pcm_reader_t *open_input(aacenc_param_ex_t *params) case 'R': if ((reader = wav_open(&io, params->ignore_length)) == 0) { fprintf(stderr, "ERROR: broken / unsupported input file\n"); - goto END; + goto FAIL; } break; case 'c': @@ -729,18 +744,21 @@ pcm_reader_t *open_input(aacenc_param_ex_t *params) aacenc_translate_generic_text_tag, ¶ms->source_tag_ctx)) == 0) { fprintf(stderr, "ERROR: broken / unsupported input file\n"); - goto END; + goto FAIL; } break; default: fprintf(stderr, "ERROR: unsupported input file\n"); - goto END; + goto FAIL; } } - if ((reader = pcm_open_sint16_converter(reader)) != 0) + reader = pcm_open_native_converter(reader); + if (reader && PCM_IS_FLOAT(pcm_get_format(reader))) + reader = limiter_open(reader); + if (reader && (reader = pcm_open_sint16_converter(reader)) != 0) reader = extrapolater_open(reader); return reader; -END: +FAIL: return 0; } @@ -758,9 +776,9 @@ int main(int argc, char **argv) AACENC_InfoStruct aacinfo = { 0 }; m4af_ctx_t *m4af = 0; const pcm_sample_description_t *sample_format; - int downsampled_timescale = 0; int frame_count = 0; int sbr_mode = 0; + unsigned scale_shift = 0; setlocale(LC_CTYPE, ""); setbuf(stderr, 0); @@ -773,17 +791,21 @@ int main(int argc, char **argv) sample_format = pcm_get_format(reader); - /* - * We use explicit/hierarchical signaling for LOAS. - * Other than that, we request implicit signaling to FDK library, then - * append explicit/backward-compatible signaling to ASC in case of MP4FF. - * - * Explicit/backward-compatible signaling of SBR is the most recommended - * way in MPEG4 part3 spec, and seems the only way supported by iTunes. - * Since FDK library does not support it, we have to do it on our side. - */ - params.sbr_signaling = (params.transport_format == TT_MP4_LOAS) ? 2 : 0; - + sbr_mode = aacenc_is_sbr_active((aacenc_param_t*)¶ms); + if (sbr_mode && !aacenc_is_sbr_ratio_available()) { + fprintf(stderr, "WARNING: Only dual-rate SBR is available " + "for this version\n"); + params.sbr_ratio = 2; + } + scale_shift = aacenc_is_dual_rate_sbr((aacenc_param_t*)¶ms); + params.sbr_signaling = 0; + if (sbr_mode) { + if (params.transport_format == TT_MP4_LOAS || !scale_shift) + params.sbr_signaling = 2; + if (params.transport_format == TT_MP4_RAW && + aacenc_is_explicit_bw_compatible_sbr_signaling_available()) + params.sbr_signaling = 1; + } if (aacenc_init(&encoder, (aacenc_param_t*)¶ms, sample_format, &aacinfo) < 0) goto END; @@ -800,28 +822,31 @@ int main(int argc, char **argv) goto END; } handle_signals(); - sbr_mode = aacenc_is_sbr_active((aacenc_param_t*)¶ms); + if (!params.transport_format) { uint32_t scale; - uint8_t mp4asc[32]; - uint32_t ascsize = sizeof(mp4asc); unsigned framelen = aacinfo.frameLength; - if (sbr_mode) - downsampled_timescale = 1; - scale = sample_format->sample_rate >> downsampled_timescale; + scale = sample_format->sample_rate >> scale_shift; if ((m4af = m4af_create(M4AF_CODEC_MP4A, scale, &m4af_io, params.output_fp)) < 0) goto END; - aacenc_mp4asc((aacenc_param_t*)¶ms, aacinfo.confBuf, - aacinfo.confSize, mp4asc, &ascsize); - m4af_set_decoder_specific_info(m4af, 0, mp4asc, ascsize); - m4af_set_fixed_frame_duration(m4af, 0, - framelen >> downsampled_timescale); + m4af_set_num_channels(m4af, 0, sample_format->channels_per_frame); + m4af_set_fixed_frame_duration(m4af, 0, framelen >> scale_shift); + if (aacenc_is_explicit_bw_compatible_sbr_signaling_available()) + m4af_set_decoder_specific_info(m4af, 0, + aacinfo.confBuf, aacinfo.confSize); + else { + uint8_t mp4asc[32]; + uint32_t ascsize = sizeof(mp4asc); + aacenc_mp4asc((aacenc_param_t*)¶ms, aacinfo.confBuf, + aacinfo.confSize, mp4asc, &ascsize); + m4af_set_decoder_specific_info(m4af, 0, mp4asc, ascsize); + } m4af_set_vbr_mode(m4af, 0, params.bitrate_mode); m4af_set_priming_mode(m4af, params.gapless_mode + 1); m4af_begin_write(m4af); } - if (sbr_mode && (aacinfo.encoderDelay & 1)) { + if (scale_shift && (aacinfo.encoderDelay & 1)) { /* * Since odd delay cannot be exactly expressed in downsampled scale, * we push one zero frame to the encoder here, to make delay even @@ -841,12 +866,11 @@ int main(int argc, char **argv) if (sbr_mode && params.profile != AOT_ER_AAC_ELD && !params.include_sbr_delay) - delay -= 481 << 1; - if (sbr_mode && (delay & 1)) + delay -= 481 << scale_shift; + if (scale_shift && (delay & 1)) ++delay; padding = frame_count * aacinfo.frameLength - frames_read - delay; - m4af_set_priming(m4af, 0, delay >> downsampled_timescale, - padding >> downsampled_timescale); + m4af_set_priming(m4af, 0, delay >> scale_shift, padding >> scale_shift); if (finalize_m4a(m4af, ¶ms, encoder) < 0) goto END; }