]> git.sesse.net Git - ffmpeg/blob - libavcodec/ac3dec_fixed.c
Merge commit 'd4ae8ac92f619507aadd021bb67b517d39d3a36f'
[ffmpeg] / libavcodec / ac3dec_fixed.c
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 #define FFT_FLOAT 0
51 #define USE_FIXED 1
52 #define FFT_FIXED_32 1
53 #include "ac3dec.h"
54
55
56 /**
57  * Table for center mix levels
58  * reference: Section 5.4.2.4 cmixlev
59  */
60 static const uint8_t center_levels[4] = { 4, 5, 6, 5 };
61
62 /**
63  * Table for surround mix levels
64  * reference: Section 5.4.2.5 surmixlev
65  */
66 static const uint8_t surround_levels[4] = { 4, 6, 7, 6 };
67
68 int end_freq_inv_tab[8] =
69 {
70     50529027, 44278013, 39403370, 32292987, 27356480, 23729101, 20951060, 18755316
71 };
72
73 static void scale_coefs (
74     int32_t *dst,
75     const int32_t *src,
76     int dynrng,
77     int len)
78 {
79     int i, shift, round;
80     int16_t mul;
81     int temp, temp1, temp2, temp3, temp4, temp5, temp6, temp7;
82
83     mul = (dynrng & 0x1f) + 0x20;
84     shift = 4 - ((dynrng << 23) >> 28);
85     if (shift > 0 ) {
86       round = 1 << (shift-1);
87       for (i=0; i<len; i+=8) {
88
89           temp = src[i] * mul;
90           temp1 = src[i+1] * mul;
91           temp = temp + round;
92           temp2 = src[i+2] * mul;
93
94           temp1 = temp1 + round;
95           dst[i] = temp >> shift;
96           temp3 = src[i+3] * mul;
97           temp2 = temp2 + round;
98
99           dst[i+1] = temp1 >> shift;
100           temp4 = src[i + 4] * mul;
101           temp3 = temp3 + round;
102           dst[i+2] = temp2 >> shift;
103
104           temp5 = src[i+5] * mul;
105           temp4 = temp4 + round;
106           dst[i+3] = temp3 >> shift;
107           temp6 = src[i+6] * mul;
108
109           dst[i+4] = temp4 >> shift;
110           temp5 = temp5 + round;
111           temp7 = src[i+7] * mul;
112           temp6 = temp6 + round;
113
114           dst[i+5] = temp5 >> shift;
115           temp7 = temp7 + round;
116           dst[i+6] = temp6 >> shift;
117           dst[i+7] = temp7 >> shift;
118
119       }
120     } else {
121       shift = -shift;
122       for (i=0; i<len; i+=8) {
123
124           temp = src[i] * mul;
125           temp1 = src[i+1] * mul;
126           temp2 = src[i+2] * mul;
127
128           dst[i] = temp << shift;
129           temp3 = src[i+3] * mul;
130
131           dst[i+1] = temp1 << shift;
132           temp4 = src[i + 4] * mul;
133           dst[i+2] = temp2 << shift;
134
135           temp5 = src[i+5] * mul;
136           dst[i+3] = temp3 << shift;
137           temp6 = src[i+6] * mul;
138
139           dst[i+4] = temp4 << shift;
140           temp7 = src[i+7] * mul;
141
142           dst[i+5] = temp5 << shift;
143           dst[i+6] = temp6 << shift;
144           dst[i+7] = temp7 << shift;
145
146       }
147     }
148 }
149
150 /**
151  * Downmix samples from original signal to stereo or mono (this is for 16-bit samples
152  * and fixed point decoder - original (for 32-bit samples) is in ac3dsp.c).
153  */
154 static void ac3_downmix_c_fixed16(int16_t **samples, int16_t (*matrix)[2],
155                                   int out_ch, int in_ch, int len)
156 {
157     int i, j;
158     int v0, v1;
159     if (out_ch == 2) {
160         for (i = 0; i < len; i++) {
161             v0 = v1 = 0;
162             for (j = 0; j < in_ch; j++) {
163                 v0 += samples[j][i] * matrix[j][0];
164                 v1 += samples[j][i] * matrix[j][1];
165             }
166             samples[0][i] = (v0+2048)>>12;
167             samples[1][i] = (v1+2048)>>12;
168         }
169     } else if (out_ch == 1) {
170         for (i = 0; i < len; i++) {
171             v0 = 0;
172             for (j = 0; j < in_ch; j++)
173                 v0 += samples[j][i] * matrix[j][0];
174             samples[0][i] = (v0+2048)>>12;
175         }
176     }
177 }
178
179 #include "ac3dec.c"
180
181 static const AVOption options[] = {
182     { "drc_scale", "percentage of dynamic range compression to apply", OFFSET(drc_scale), AV_OPT_TYPE_FLOAT, {.dbl = 1.0}, 0.0, 6.0, PAR },
183     { "heavy_compr", "heavy dynamic range compression enabled", OFFSET(heavy_compression), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, PAR },
184     { NULL},
185 };
186
187 static const AVClass ac3_decoder_class = {
188     .class_name = "Fixed-Point AC-3 Decoder",
189     .item_name  = av_default_item_name,
190     .option     = options,
191     .version    = LIBAVUTIL_VERSION_INT,
192 };
193
194 AVCodec ff_ac3_fixed_decoder = {
195     .name           = "ac3_fixed",
196     .type           = AVMEDIA_TYPE_AUDIO,
197     .id             = AV_CODEC_ID_AC3,
198     .priv_data_size = sizeof (AC3DecodeContext),
199     .init           = ac3_decode_init,
200     .close          = ac3_decode_end,
201     .decode         = ac3_decode_frame,
202     .capabilities   = CODEC_CAP_DR1,
203     .long_name      = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"),
204     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
205                                                       AV_SAMPLE_FMT_NONE },
206     .priv_class     = &ac3_decoder_class,
207 };