X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=src%2Fmain.c;h=8521a33ebd9791b174356c8cf24386f43c6205ea;hb=e1adc1783585421fdbd32d9a1d3edf5337e9af4f;hp=952cfbccc4b91fb92f416992bcdd7326c8b99e6f;hpb=f48bf1294c7fedb71ba8f3ad5706445b63073195;p=fdkaac.git diff --git a/src/main.c b/src/main.c index 952cfbc..8521a33 100644 --- a/src/main.c +++ b/src/main.c @@ -132,11 +132,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" -" -s, --sbr-signaling SBR signaling mode\n" -" 0: Implicit, backward compatible(default)\n" -" 1: Explicit SBR and implicit PS\n" -" 2: Explicit hierarchical signaling\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" @@ -153,7 +156,10 @@ PROGNAME " %s\n" " 0: iTunSMPB (default)\n" " 1: ISO standard (edts + sgpd)\n" " 2: Both\n" -" --ignorelength Ignore length of WAV header\n" +" --include-sbr-delay Count SBR decoder delay in encoder delay\n" +" This is not iTunes compatible, but is default\n" +" behavior of FDK library.\n" +" -I, --ignorelength Ignore length of WAV header\n" " -S, --silent Don't print progress messages\n" " --moov-before-mdat Place moov box before mdat box on m4a output\n" "\n" @@ -208,6 +214,7 @@ typedef struct aacenc_param_ex_t { char *output_filename; FILE *output_fp; unsigned gapless_mode; + unsigned include_sbr_delay; unsigned ignore_length; int silent; int moov_before_mdat; @@ -228,8 +235,9 @@ 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') #define OPT_RAW_CHANNELS M4AF_FOURCC('r','c','h','n') #define OPT_RAW_RATE M4AF_FOURCC('r','r','a','t') @@ -246,13 +254,14 @@ 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' }, - { "sbr-signaling", required_argument, 0, 's' }, + { "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' }, { "gapless-mode", required_argument, 0, 'G' }, + { "include-sbr-delay", no_argument, 0, OPT_INCLUDE_SBR_DELAY }, { "ignorelength", no_argument, 0, 'I' }, { "silent", no_argument, 0, 'S' }, { "moov-before-mdat", no_argument, 0, OPT_MOOV_BEFORE_MDAT }, @@ -324,14 +333,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-signaling\n"); + fprintf(stderr, "invalid arg for sbr-ratio\n"); return -1; } - params->sbr_signaling = n; + params->sbr_ratio = n; break; case 'f': if (sscanf(optarg, "%u", &n) != 1) { @@ -360,6 +373,9 @@ int parse_options(int argc, char **argv, aacenc_param_ex_t *params) } params->gapless_mode = n; break; + case OPT_INCLUDE_SBR_DELAY: + params->include_sbr_delay = 1; + break; case 'I': params->ignore_length = 1; break; @@ -475,16 +491,15 @@ int parse_options(int argc, char **argv, aacenc_param_ex_t *params) }; static -int write_sample(FILE *ofp, m4af_ctx_t *m4af, - const void *data, uint32_t size, uint32_t duration) +int write_sample(FILE *ofp, m4af_ctx_t *m4af, aacenc_frame_t *frame) { if (!m4af) { - fwrite(data, 1, size, ofp); + fwrite(frame->data, 1, frame->size, ofp); if (ferror(ofp)) { fprintf(stderr, "ERROR: fwrite(): %s\n", strerror(errno)); return -1; } - } else if (m4af_write_sample(m4af, 0, data, size, duration) < 0) { + } else if (m4af_write_sample(m4af, 0, frame->data, frame->size, 0) < 0) { fprintf(stderr, "ERROR: failed to write m4a sample\n"); return -1; } @@ -492,51 +507,76 @@ int write_sample(FILE *ofp, m4af_ctx_t *m4af, } static -int encode(pcm_reader_t *reader, HANDLE_AACENCODER encoder, - uint32_t frame_length, FILE *ofp, m4af_ctx_t *m4af, - int show_progress) +int encode(aacenc_param_ex_t *params, pcm_reader_t *reader, + HANDLE_AACENCODER encoder, uint32_t frame_length, + m4af_ctx_t *m4af) { - int16_t *ibuf = 0; - uint8_t *obuf = 0; - uint32_t olen; - uint32_t osize = 0; + int16_t *ibuf = 0, *ip; + aacenc_frame_t obuf[2] = {{ 0 }}, *obp; + unsigned flip = 0; int nread = 1; - int consumed; int rc = -1; - int frames_written = 0; + int remaining, consumed; + int frames_written = 0, encoded = 0; aacenc_progress_t progress = { 0 }; const pcm_sample_description_t *fmt = pcm_get_format(reader); ibuf = malloc(frame_length * fmt->bytes_per_frame); aacenc_progress_init(&progress, pcm_get_length(reader), fmt->sample_rate); - do { + + for (;;) { + /* + * Since we delay the write, we cannot just exit loop when interrupted. + * Instead, we regard it as EOF. + */ if (g_interrupted) nread = 0; - else if (nread) { + if (nread > 0) { if ((nread = pcm_read_frames(reader, ibuf, frame_length)) < 0) { fprintf(stderr, "ERROR: read failed\n"); goto END; } - if (show_progress) + if (!params->silent) aacenc_progress_update(&progress, pcm_get_position(reader), fmt->sample_rate * 2); } - if ((consumed = aac_encode_frame(encoder, fmt, ibuf, nread, - &obuf, &olen, &osize)) < 0) - goto END; - if (olen > 0) { - if (write_sample(ofp, m4af, obuf, olen, frame_length) < 0) + ip = ibuf; + remaining = nread; + do { + obp = &obuf[flip]; + consumed = aac_encode_frame(encoder, fmt, ip, remaining, obp); + if (consumed < 0) goto END; + if (consumed == 0 && obp->size == 0) goto DONE; + if (obp->size == 0) break; + + remaining -= consumed; + ip += consumed * fmt->channels_per_frame; + flip ^= 1; + /* + * As we pad 1 frame at beginning and ending by our extrapolator, + * we want to drop them. + * We delay output by 1 frame by double buffering, and discard + * second frame and final frame from the encoder. + * Since sbr_header is included in the first frame (in case of + * SBR), we cannot discard first frame. So we pick second instead. + */ + ++encoded; + if (encoded == 1 || encoded == 3) + continue; + obp = &obuf[flip]; + if (write_sample(params->output_fp, m4af, obp) < 0) goto END; ++frames_written; - } - } while (nread > 0 || olen > 0); - - if (show_progress) + } while (remaining > 0); + } +DONE: + if (!params->silent) aacenc_progress_finish(&progress, pcm_get_position(reader)); rc = frames_written; END: if (ibuf) free(ibuf); - if (obuf) free(obuf); + if (obuf[0].data) free(obuf[0].data); + if (obuf[1].data) free(obuf[1].data); return rc; } @@ -546,19 +586,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 @@ -610,7 +642,7 @@ char *generate_output_filename(const char *filename, const char *ext) const char *ext_org = strrchr(base, '.'); if (ext_org) ilen = ext_org - base; p = malloc(ilen + ext_len + 1); - sprintf(p, "%.*s%s", ilen, base, ext); + sprintf(p, "%.*s%s", (int)ilen, base, ext); } return p; } @@ -654,7 +686,7 @@ int parse_raw_spec(const char *spec, pcm_sample_description_t *desc) static pcm_io_vtbl_t pcm_io_vtbl = { read_callback, seek_callback, tell_callback }; -static pcm_io_vtbl_t pcm_io_vtbl_noseek = { read_callback, 0, 0 }; +static pcm_io_vtbl_t pcm_io_vtbl_noseek = { read_callback, 0, tell_callback }; static pcm_reader_t *open_input(aacenc_param_ex_t *params) @@ -712,10 +744,13 @@ pcm_reader_t *open_input(aacenc_param_ex_t *params) } break; default: + fprintf(stderr, "ERROR: unsupported input file\n"); goto END; } } - return pcm_open_sint16_converter(reader); + if ((reader = pcm_open_sint16_converter(reader)) != 0) + reader = extrapolater_open(reader); + return reader; END: return 0; } @@ -732,10 +767,13 @@ int main(int argc, char **argv) pcm_reader_t *reader = 0; HANDLE_AACENCODER encoder = 0; AACENC_InfoStruct aacinfo = { 0 }; + LIB_INFO lib_info = { 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); @@ -748,6 +786,26 @@ 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. + */ + 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 = (params.transport_format == TT_MP4_LOAS) ? 2 : 0; + if (sbr_mode && !scale_shift) + params.sbr_signaling = 2; + if (aacenc_init(&encoder, (aacenc_param_t*)¶ms, sample_format, &aacinfo) < 0) goto END; @@ -764,36 +822,50 @@ int main(int argc, char **argv) goto END; } handle_signals(); + if (!params.transport_format) { uint32_t scale; + uint8_t mp4asc[32]; + uint32_t ascsize = sizeof(mp4asc); unsigned framelen = aacinfo.frameLength; - int sbr_mode = aacenc_is_sbr_active((aacenc_param_t*)¶ms); - int sig_mode = aacEncoder_GetParam(encoder, AACENC_SIGNALING_MODE); - if (sbr_mode && !sig_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; - m4af_set_decoder_specific_info(m4af, 0, aacinfo.confBuf, - aacinfo.confSize); + 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); + framelen >> scale_shift); m4af_set_vbr_mode(m4af, 0, params.bitrate_mode); m4af_set_priming_mode(m4af, params.gapless_mode + 1); m4af_begin_write(m4af); } - frame_count = encode(reader, encoder, aacinfo.frameLength, - params.output_fp, m4af, !params.silent); + 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 + */ + int16_t zero[8] = { 0 }; + aacenc_frame_t frame = { 0 }; + aac_encode_frame(encoder, sample_format, zero, 1, &frame); + free(frame.data); + } + frame_count = encode(¶ms, reader, encoder, aacinfo.frameLength, m4af); if (frame_count < 0) goto END; if (m4af) { uint32_t delay = aacinfo.encoderDelay; + uint32_t padding; int64_t frames_read = pcm_get_position(reader); - uint32_t padding = frame_count * aacinfo.frameLength - - frames_read - aacinfo.encoderDelay; - m4af_set_priming(m4af, 0, delay >> downsampled_timescale, - padding >> downsampled_timescale); + + if (sbr_mode && params.profile != AOT_ER_AAC_ELD && + !params.include_sbr_delay) + delay -= 481 << scale_shift; + if (scale_shift && (delay & 1)) + ++delay; + padding = frame_count * aacinfo.frameLength - frames_read - delay; + m4af_set_priming(m4af, 0, delay >> scale_shift, padding >> scale_shift); if (finalize_m4a(m4af, ¶ms, encoder) < 0) goto END; }