New upstream version 1.0.0
[fdkaac.git] / src / main.c
index ef2fa87b8b60931c19d02930eacb972185b7447f..49051307ebaf7631db909ebab4f3d504aa6403dc 100644 (file)
@@ -33,8 +33,7 @@
 #include <windows.h>
 #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"
@@ -118,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 <n>             Bitrate in bits per seconds (for CBR)\n"
 " -m, --bitrate-mode <n>        Bitrate configuration\n"
 "                                 0: CBR (default)\n"
@@ -293,7 +289,7 @@ int parse_options(int argc, char **argv, aacenc_param_ex_t *params)
     params->afterburner = 1;
 
     aacenc_getmainargs(&argc, &argv);
-    while ((ch = getopt_long(argc, argv, "hp:b:m:w:a:Ls:f:CP:G:Io:SR",
+    while ((ch = getopt_long(argc, argv, "hp:b:m:w:a:L:s:f:CP:G:Io:SR",
                              long_options, 0)) != EOF) {
         switch (ch) {
         case 'h':
@@ -507,6 +503,11 @@ int write_sample(FILE *ofp, m4af_ctx_t *m4af, aacenc_frame_t *frame)
     return 0;
 }
 
+static int do_smart_padding(int profile)
+{
+    return profile == 2 || profile == 5 || profile == 29;
+}
+
 static
 int encode(aacenc_param_ex_t *params, pcm_reader_t *reader,
            HANDLE_AACENCODER encoder, uint32_t frame_length, 
@@ -521,15 +522,12 @@ int encode(aacenc_param_ex_t *params, pcm_reader_t *reader,
     int frames_written = 0, encoded = 0;
     aacenc_progress_t progress = { 0 };
     const pcm_sample_description_t *fmt = pcm_get_format(reader);
+    const int is_padding = do_smart_padding(params->profile);
 
     ibuf = malloc(frame_length * fmt->bytes_per_frame);
     aacenc_progress_init(&progress, pcm_get_length(reader), fmt->sample_rate);
 
     for (;;) {
-        /*
-         * Since we delay the write, we cannot just exit loop when interrupted.
-         * Instead, we regard it as EOF.
-         */
         if (g_interrupted)
             nread = 0;
         if (nread > 0) {
@@ -552,7 +550,7 @@ int encode(aacenc_param_ex_t *params, pcm_reader_t *reader,
 
             remaining -= consumed;
             ip += consumed * fmt->channels_per_frame;
-            flip ^= 1;
+            if (is_padding) {
             /*
              * As we pad 1 frame at beginning and ending by our extrapolator,
              * we want to drop them.
@@ -561,16 +559,26 @@ int encode(aacenc_param_ex_t *params, pcm_reader_t *reader,
              * 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)
+                flip ^= 1;
+                ++encoded;
+                if (encoded == 1 || encoded == 3)
+                    continue;
+            }
+            if (write_sample(params->output_fp, m4af, &obuf[flip]) < 0)
                 goto END;
             ++frames_written;
         } while (remaining > 0);
     }
 DONE:
+    /*
+     * 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;
+    }
     if (!params->silent)
         aacenc_progress_finish(&progress, pcm_get_position(reader));
     rc = frames_written;
@@ -694,16 +702,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;
@@ -713,7 +719,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;
@@ -721,7 +727,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;
@@ -731,7 +737,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':
@@ -741,18 +747,23 @@ pcm_reader_t *open_input(aacenc_param_ex_t *params)
                                    aacenc_translate_generic_text_tag,
                                    &params->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 = extrapolater_open(reader);
+    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) {
+        if (do_smart_padding(params->profile))
+            reader = extrapolater_open(reader);
+    }
     return reader;
-END:
+FAIL:
     return 0;
 }
 
@@ -792,12 +803,14 @@ int main(int argc, char **argv)
         params.sbr_ratio = 2;
     }
     scale_shift = aacenc_is_dual_rate_sbr((aacenc_param_t*)&params);
-    params.sbr_signaling =
-        (params.transport_format == TT_MP4_LOAS) ? 2 :
-        (params.transport_format == TT_MP4_RAW)  ? 1 : 0;
-    if (sbr_mode && !scale_shift)
-        params.sbr_signaling = 2;
-
+    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*)&params, sample_format,
                     &aacinfo) < 0)
         goto END;
@@ -822,37 +835,38 @@ int main(int argc, char **argv)
         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);
-        m4af_set_fixed_frame_duration(m4af, 0,
-                                      framelen >> scale_shift);
+        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*)&params, 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 (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(&params, reader, encoder, aacinfo.frameLength, m4af);
     if (frame_count < 0)
         goto END;
     if (m4af) {
-        uint32_t delay = aacinfo.encoderDelay;
         uint32_t padding;
+#if AACENCODER_LIB_VL0 < 4
+        uint32_t delay = aacinfo.encoderDelay;
+        if (sbr_mode && params.profile != AOT_ER_AAC_ELD
+            && !params.include_sbr_delay)
+            delay -= 481 << scale_shift;
+#else
+        uint32_t delay = params.include_sbr_delay ? aacinfo.nDelay
+                                                  : aacinfo.nDelayCore;
+#endif
         int64_t frames_read = pcm_get_position(reader);
 
-        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, &params, encoder) < 0)
This page took 0.014519 seconds and 4 git commands to generate.