From 353e4fcd7a64df0a26f56ed99a6e461ad9e7773d Mon Sep 17 00:00:00 2001 From: Dima Date: Thu, 19 Sep 2019 13:07:22 +0300 Subject: [PATCH] process 32-bit input if possible (i.e. respect aac:INT_PCM type) switch AAC-FDK from pcm16 to pcm32: typedef LONG INT_PCM; #define SAMPLE_BITS 32 --- src/aacenc.c | 6 +++--- src/aacenc.h | 2 +- src/main.c | 2 +- src/pcm_sint16_converter.c | 24 +++++++++++++++++++----- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/aacenc.c b/src/aacenc.c index 0dd47bb..8404f31 100644 --- a/src/aacenc.c +++ b/src/aacenc.c @@ -247,7 +247,7 @@ FAIL: int aac_encode_frame(HANDLE_AACENCODER encoder, const pcm_sample_description_t *format, - const int16_t *input, unsigned iframes, + const INT_PCM *input, unsigned iframes, aacenc_frame_t *output) { uint32_t ilen = iframes * format->channels_per_frame; @@ -258,9 +258,9 @@ int aac_encode_frame(HANDLE_AACENCODER encoder, void *obufs[1]; INT ibuf_ids[] = { IN_AUDIO_DATA }; INT obuf_ids[] = { OUT_BITSTREAM_DATA }; - INT ibuf_sizes[] = { ilen * sizeof(int16_t) }; + INT ibuf_sizes[] = { ilen * sizeof(INT_PCM) }; INT obuf_sizes[1]; - INT ibuf_el_sizes[] = { sizeof(int16_t) }; + INT ibuf_el_sizes[] = { sizeof(INT_PCM) }; INT obuf_el_sizes[] = { 1 }; AACENC_ERROR err; unsigned channel_mode, obytes; diff --git a/src/aacenc.h b/src/aacenc.h index 2b7d102..ebbe98a 100644 --- a/src/aacenc.h +++ b/src/aacenc.h @@ -50,7 +50,7 @@ int aacenc_init(HANDLE_AACENCODER *encoder, const aacenc_param_t *params, int aac_encode_frame(HANDLE_AACENCODER encoder, const pcm_sample_description_t *format, - const int16_t *input, unsigned iframes, + const INT_PCM *input, unsigned iframes, aacenc_frame_t *output); #endif diff --git a/src/main.c b/src/main.c index 88f9e23..6d9dce0 100644 --- a/src/main.c +++ b/src/main.c @@ -521,7 +521,7 @@ 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, *ip; + INT_PCM *ibuf = 0, *ip; aacenc_frame_t obuf[2] = {{ 0 }}, *obp; unsigned flip = 0; int nread = 1; diff --git a/src/pcm_sint16_converter.c b/src/pcm_sint16_converter.c index 052efc8..c2a6de2 100644 --- a/src/pcm_sint16_converter.c +++ b/src/pcm_sint16_converter.c @@ -10,6 +10,8 @@ #if HAVE_STDINT_H # include #endif +#include +#include #include "pcm_reader.h" typedef struct pcm_sint16_converter_t { @@ -57,12 +59,18 @@ static int read_frames(pcm_reader_t *reader, void *buffer, unsigned nframes) count = nframes * sfmt->channels_per_frame; if (PCM_IS_FLOAT(sfmt)) { float *ip = self->pivot; - int16_t *op = buffer; + INT_PCM *op = buffer; +#if SAMPLE_BITS == 16 for (i = 0; i < count; ++i) - op[i] = pcm_clip(ip[i] * 32768.0, -32768.0, 32767.0); + op[i] = (int16_t)pcm_clip(ip[i] * 32768.0, -32768.0, 32767.0); +#else + for (i = 0; i < count; ++i) + op[i] = (int32_t)pcm_clip(ip[i] * 2147483648.0, -2147483648.0, 2147483647.0); +#endif } else { int32_t *ip = self->pivot; - int16_t *op = buffer; + INT_PCM *op = buffer; +#if SAMPLE_BITS == 16 if (sfmt->bits_per_channel <= 16) { for (i = 0; i < count; ++i) op[i] = ip[i] >> 16; @@ -72,6 +80,10 @@ static int read_frames(pcm_reader_t *reader, void *buffer, unsigned nframes) op[i] = (n == 0x8000) ? 0x7fff : n; } } +#else + for (i = 0; i < count; ++i) + op[i] = ip[i]; +#endif } return nframes; } @@ -94,14 +106,16 @@ pcm_reader_t *pcm_open_sint16_converter(pcm_reader_t *reader) pcm_sint16_converter_t *self = 0; pcm_sample_description_t *fmt; + assert((SAMPLE_BITS>>3) == sizeof(INT_PCM)); + if ((self = calloc(1, sizeof(pcm_sint16_converter_t))) == 0) return 0; self->src = reader; self->vtbl = &my_vtable; memcpy(&self->format, pcm_get_format(reader), sizeof(self->format)); fmt = &self->format; - fmt->bits_per_channel = 16; + fmt->bits_per_channel = SAMPLE_BITS; fmt->sample_type = PCM_TYPE_SINT; - fmt->bytes_per_frame = 2 * fmt->channels_per_frame; + fmt->bytes_per_frame = sizeof(INT_PCM) * fmt->channels_per_frame; return (pcm_reader_t *)self; }