FFmpeg
Loading...
Searching...
No Matches
ac3dec_fixed.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012
3 * MIPS Technologies, Inc., California.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the MIPS Technologies, Inc., nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE MIPS TECHNOLOGIES, INC. ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE MIPS TECHNOLOGIES, INC. BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * Author: Stanislav Ocovaj (socovaj@mips.com)
30 *
31 * AC3 fixed-point decoder for MIPS platforms
32 *
33 * This file is part of FFmpeg.
34 *
35 * FFmpeg is free software; you can redistribute it and/or
36 * modify it under the terms of the GNU Lesser General Public
37 * License as published by the Free Software Foundation; either
38 * version 2.1 of the License, or (at your option) any later version.
39 *
40 * FFmpeg is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
43 * Lesser General Public License for more details.
44 *
45 * You should have received a copy of the GNU Lesser General Public
46 * License along with FFmpeg; if not, write to the Free Software
47 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
48 */
49
50#include "config_components.h"
51#define USE_FIXED 1
52#include "ac3dec.h"
53#include "codec_internal.h"
54#define IMDCT_TYPE AV_TX_INT32_MDCT
55
56#include "ac3dec.h"
57
58/* Keep two fractional bits in fixed-point transform coefficients. */
59#define AC3_FIXED_COEFF_BITS 2
60#define AC3_FIXED_EXPONENT_MAX 24
61
62static av_always_inline int fixed_coeff_bits(const AC3DecodeContext *s)
63{
64 /* ac3_fixed normally decodes AC-3. Keep Q0 when it is explicitly forced
65 * to decode E-AC-3, whose AHT coefficient bounds are different. */
66 return s->eac3 ? 0 : AC3_FIXED_COEFF_BITS;
67}
68
69static const int end_freq_inv_tab[8] =
70{
71 50529027, 44278013, 39403370, 32292987, 27356480, 23729101, 20951060, 18755316
72};
73
74static void scale_coefs (
75 int32_t *dst,
76 const int32_t *src,
77 int dynrng,
78 int len)
79{
80 int i, shift;
81 unsigned mul, round;
82 int temp, temp1, temp2, temp3, temp4, temp5, temp6, temp7;
83
84 mul = (dynrng & 0x1f) + 0x20;
85 shift = 4 - (sign_extend(dynrng, 9) >> 5);
86 if (shift > 0 ) {
87 round = 1 << (shift-1);
88 for (i=0; i<len; i+=8) {
89
90 temp = src[i] * mul;
91 temp1 = src[i+1] * mul;
92 temp = temp + round;
93 temp2 = src[i+2] * mul;
94
95 temp1 = temp1 + round;
96 dst[i] = temp >> shift;
97 temp3 = src[i+3] * mul;
98 temp2 = temp2 + round;
99
100 dst[i+1] = temp1 >> shift;
101 temp4 = src[i + 4] * mul;
102 temp3 = temp3 + round;
103 dst[i+2] = temp2 >> shift;
104
105 temp5 = src[i+5] * mul;
106 temp4 = temp4 + round;
107 dst[i+3] = temp3 >> shift;
108 temp6 = src[i+6] * mul;
109
110 dst[i+4] = temp4 >> shift;
111 temp5 = temp5 + round;
112 temp7 = src[i+7] * mul;
113 temp6 = temp6 + round;
114
115 dst[i+5] = temp5 >> shift;
116 temp7 = temp7 + round;
117 dst[i+6] = temp6 >> shift;
118 dst[i+7] = temp7 >> shift;
119
120 }
121 } else {
122 shift = -shift;
123 mul <<= shift;
124 for (i=0; i<len; i+=8) {
125
126 dst[i] = src[i ] * mul;
127 dst[i+1] = src[i+1] * mul;
128 dst[i+2] = src[i+2] * mul;
129 dst[i+3] = src[i+3] * mul;
130 dst[i+4] = src[i+4] * mul;
131 dst[i+5] = src[i+5] * mul;
132 dst[i+6] = src[i+6] * mul;
133 dst[i+7] = src[i+7] * mul;
134 }
135 }
136}
137
138static void scale_coefs_q2(int32_t *dst, const int32_t *src, int dynrng,
139 int len)
140{
141 int i, shift;
142 int mul;
143
144 mul = (dynrng & 0x1f) + 0x20;
145 shift = 4 - (sign_extend(dynrng, 9) >> 5);
146
147 /* AC-3 mantissas have magnitude at most 2^23, hence Q2 coefficients
148 * have magnitude at most 2^25. Coupling uses MULH(coeff * 2^4, coord)
149 * with coord < 2^31, so coupled coefficients are below 2^28.
150 * Rematrixing can at most double them, keeping src below 2^29. */
151 if (dynrng == 32) {
152 for (i = 0; i < len; i++)
153 dst[i] = src[i] * 4;
154 return;
155 }
156
157 if (shift >= 4) {
158 const int round = 1 << (shift - 1);
159 const int unit = 1 << shift;
160
161 /* With shift >= 4, quotient * mul is below 2^25 * 63. Splitting
162 * quotient and remainder therefore keeps both products in int32_t. */
163 for (i = 0; i < len; i++) {
164 int quotient = src[i] >> shift;
165 int remainder = src[i] - quotient * unit;
166
167 dst[i] = quotient * mul + ((remainder * mul + round) >> shift);
168 }
169 } else if (shift > 0) {
170 const int round = 1 << (shift - 1);
171
172 for (i = 0; i < len; i++)
173 dst[i] = av_clipl_int32(((int64_t)src[i] * mul + round) >> shift);
174 } else {
175 mul <<= -shift;
176 for (i = 0; i < len; i++)
177 dst[i] = av_clipl_int32((int64_t)src[i] * mul);
178 }
179}
180
181/**
182 * Downmix samples from original signal to stereo or mono (this is for 16-bit samples
183 * and fixed point decoder - original (for 32-bit samples) is in ac3dsp.c).
184 */
185static void ac3_downmix_c_fixed16(int16_t **samples, int16_t **matrix,
186 int out_ch, int in_ch, int len)
187{
188 int i, j;
189 int v0, v1;
190 if (out_ch == 2) {
191 for (i = 0; i < len; i++) {
192 v0 = v1 = 0;
193 for (j = 0; j < in_ch; j++) {
194 v0 += samples[j][i] * matrix[0][j];
195 v1 += samples[j][i] * matrix[1][j];
196 }
197 samples[0][i] = (v0+2048)>>12;
198 samples[1][i] = (v1+2048)>>12;
199 }
200 } else if (out_ch == 1) {
201 for (i = 0; i < len; i++) {
202 v0 = 0;
203 for (j = 0; j < in_ch; j++)
204 v0 += samples[j][i] * matrix[0][j];
205 samples[0][i] = (v0+2048)>>12;
206 }
207 }
208}
209
210#if CONFIG_EAC3_DECODER
211#include "eac3dec.c"
212#endif
213#include "ac3dec.c"
214
215static const AVOption options[] = {
216 { "cons_noisegen", "enable consistent noise generation", OFFSET(consistent_noise_generation), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, PAR },
217 { "drc_scale", "percentage of dynamic range compression to apply", OFFSET(drc_scale), AV_OPT_TYPE_FLOAT, {.dbl = 1.0}, 0.0, 6.0, PAR },
218 { "heavy_compr", "enable heavy dynamic range compression", OFFSET(heavy_compression), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, PAR },
219 { "downmix", "Request a specific channel layout from the decoder", OFFSET(downmix_layout), AV_OPT_TYPE_CHLAYOUT, {.str = NULL}, .flags = PAR },
220 { NULL},
221};
222
224 .class_name = "Fixed-Point AC-3 Decoder",
225 .item_name = av_default_item_name,
226 .option = options,
227 .version = LIBAVUTIL_VERSION_INT,
228};
229
231 .p.name = "ac3_fixed",
232 CODEC_LONG_NAME("ATSC A/52A (AC-3)"),
233 .p.type = AVMEDIA_TYPE_AUDIO,
234 .p.id = AV_CODEC_ID_AC3,
235 .p.priv_class = &ac3_decoder_class,
236 .priv_data_size = sizeof (AC3DecodeContext),
241 .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
244 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
245};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
#define AC3_FIXED_COEFF_BITS
static void ac3_downmix_c_fixed16(int16_t **samples, int16_t **matrix, int out_ch, int in_ch, int len)
Downmix samples from original signal to stereo or mono (this is for 16-bit samples and fixed point de...
static void scale_coefs_q2(int32_t *dst, const int32_t *src, int dynrng, int len)
static const int end_freq_inv_tab[8]
static av_always_inline int fixed_coeff_bits(const AC3DecodeContext *s)
const FFCodec ff_ac3_fixed_decoder
static void scale_coefs(int32_t *dst, const int32_t *src, int dynrng, int len)
static const AVClass ac3_decoder_class
#define PAR
Definition apedec.c:1740
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
int32_t
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define CODEC_SAMPLEFMTS(...)
#define av_clipl_int32
Definition common.h:118
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
void(* flush)(AVBSFContext *ctx)
Definition dts2pts.c:610
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define OFFSET(x)
@ AV_OPT_TYPE_CHLAYOUT
Underlying C type is AVChannelLayout.
Definition opt.h:330
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
@ AV_CODEC_ID_AC3
Definition codec_id.h:456
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition samplefmt.h:64
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
static av_cold void ac3_decode_flush(AVCodecContext *avctx)
Definition ac3dec.c:157
static av_cold int ac3_decode_end(AVCodecContext *avctx)
Uninitialize the AC-3 decoder.
Definition ac3dec.c:1812
static int ac3_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Decode a single AC-3 frame.
Definition ac3dec.c:1408
static av_cold int ac3_decode_init(AVCodecContext *avctx)
AVCodec initialization.
Definition ac3dec.c:106
static int shift(int a, int b)
Definition bonk.c:261
#define av_always_inline
Definition attributes.h:72
static av_always_inline av_const double round(double x)
Definition libm.h:446
static av_const int sign_extend(int val, unsigned bits)
Definition mathops.h:135
Describe the class of an AVClass context structure.
Definition log.h:76
AVOption.
Definition opt.h:428
#define src
Definition vp8dsp.c:248
else temp
Definition vf_mcdeint.c:275
int len