libavcodec/ac3enc_fixed.c
ad6b2c1f
 /*
  * The simplest AC-3 encoder
  * Copyright (c) 2000 Fabrice Bellard
  * Copyright (c) 2006-2010 Justin Ruggles <justin.ruggles@gmail.com>
  * Copyright (c) 2006-2010 Prakash Punnoor <prakash@punnoor.de>
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
  * @file
  * fixed-point AC-3 encoder.
  */
 
79997def
 #define CONFIG_FFT_FLOAT 0
6fd96d1a
 #undef CONFIG_AC3ENC_FLOAT
aa872af5
 #include "internal.h"
e0cc66df
 #include "ac3enc.h"
ae264bb2
 #include "eac3enc.h"
e0cc66df
 
 #define AC3ENC_TYPE AC3ENC_TYPE_AC3_FIXED
 #include "ac3enc_opts_template.c"
e9d5c052
 
 static const AVClass ac3enc_class = {
     .class_name = "Fixed-Point AC-3 Encoder",
     .item_name  = av_default_item_name,
9023de34
     .option     = ac3_options,
e9d5c052
     .version    = LIBAVUTIL_VERSION_INT,
 };
e0cc66df
 
 #include "ac3enc_template.c"
ad6b2c1f
 
 
 /**
  * Finalize MDCT and free allocated memory.
c2d9a65b
  *
  * @param s  AC-3 encoder private context
ad6b2c1f
  */
b5849f77
 av_cold void AC3_NAME(mdct_end)(AC3EncodeContext *s)
ad6b2c1f
 {
b5849f77
     ff_mdct_end(&s->mdct);
ad6b2c1f
 }
 
 
 /**
  * Initialize MDCT tables.
c2d9a65b
  *
  * @param s  AC-3 encoder private context
  * @return   0 on success, negative error code on failure
ad6b2c1f
  */
b5849f77
 av_cold int AC3_NAME(mdct_init)(AC3EncodeContext *s)
ad6b2c1f
 {
b5849f77
     int ret = ff_mdct_init(&s->mdct, 9, 0, -1.0);
     s->mdct_window = ff_ac3_window;
79997def
     return ret;
ad6b2c1f
 }
 
 
c2d9a65b
 /*
ad6b2c1f
  * Apply KBD window to input samples prior to MDCT.
  */
d5a7229b
 static void apply_window(void *dsp, int16_t *output, const int16_t *input,
8683c6a6
                          const int16_t *window, unsigned int len)
ad6b2c1f
 {
d5a7229b
     DSPContext *dsp0 = dsp;
     dsp0->apply_window_int16(output, input, window, len);
ad6b2c1f
 }
 
 
c2d9a65b
 /*
ad6b2c1f
  * Normalize the input samples to use the maximum available precision.
323e6fea
  * This assumes signed 16-bit input samples.
ad6b2c1f
  */
8683c6a6
 static int normalize_samples(AC3EncodeContext *s)
ad6b2c1f
 {
668afae4
     int v = s->ac3dsp.ac3_max_msb_abs_int16(s->windowed_samples, AC3_WINDOW_SIZE);
     v = 14 - av_log2(v);
f1efbca5
     if (v > 0)
         s->ac3dsp.ac3_lshift_int16(s->windowed_samples, AC3_WINDOW_SIZE, v);
323e6fea
     /* +6 to right-shift from 31-bit to 25-bit */
     return v + 6;
ad6b2c1f
 }
 
 
c2d9a65b
 /*
323e6fea
  * Scale MDCT coefficients to 25-bit signed fixed-point.
ac05f903
  */
8683c6a6
 static void scale_coefficients(AC3EncodeContext *s)
ac05f903
 {
323e6fea
     int blk, ch;
 
be7bd626
     for (blk = 0; blk < s->num_blocks; blk++) {
323e6fea
         AC3Block *block = &s->blocks[blk];
7f3a7b5c
         for (ch = 1; ch <= s->channels; ch++) {
f1efbca5
             s->ac3dsp.ac3_rshift_int32(block->mdct_coef[ch], AC3_MAX_COEFS,
                                        block->coeff_shift[ch]);
323e6fea
         }
     }
ac05f903
 }
 
f507a9fe
 static void sum_square_butterfly(AC3EncodeContext *s, int64_t sum[4],
                                  const int32_t *coef0, const int32_t *coef1,
                                  int len)
 {
     s->ac3dsp.sum_square_butterfly_int32(sum, coef0, coef1, len);
 }
ac05f903
 
c2d9a65b
 /*
523b7eba
  * Clip MDCT coefficients to allowable range.
  */
 static void clip_coefficients(DSPContext *dsp, int32_t *coef, unsigned int len)
 {
     dsp->vector_clip_int32(coef, coef, COEF_MIN, COEF_MAX, len);
 }
 
 
c2d9a65b
 /*
ae264bb2
  * Calculate a single coupling coordinate.
  */
 static CoefType calc_cpl_coord(CoefSumType energy_ch, CoefSumType energy_cpl)
 {
     if (energy_cpl <= COEF_MAX) {
         return 1048576;
     } else {
         uint64_t coord   = energy_ch / (energy_cpl >> 24);
         uint32_t coord32 = FFMIN(coord, 1073741824);
         coord32          = ff_sqrt(coord32) << 9;
         return FFMIN(coord32, COEF_MAX);
     }
 }
 
 
e0cc66df
 static av_cold int ac3_fixed_encode_init(AVCodecContext *avctx)
 {
     AC3EncodeContext *s = avctx->priv_data;
     s->fixed_point = 1;
     return ff_ac3_encode_init(avctx);
 }
 
 
d36beb3f
 AVCodec ff_ac3_fixed_encoder = {
00c3b67b
     .name            = "ac3_fixed",
     .type            = AVMEDIA_TYPE_AUDIO,
36ef5369
     .id              = AV_CODEC_ID_AC3,
00c3b67b
     .priv_data_size  = sizeof(AC3EncodeContext),
     .init            = ac3_fixed_encode_init,
     .encode2         = ff_ac3_fixed_encode_frame,
     .close           = ff_ac3_encode_close,
b1540fc8
     .sample_fmts     = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16P,
00c3b67b
                                                       AV_SAMPLE_FMT_NONE },
     .long_name       = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"),
     .priv_class      = &ac3enc_class,
1aeb88b7
     .channel_layouts = ff_ac3_channel_layouts,
4e99501f
     .defaults        = ac3_defaults,
ad6b2c1f
 };