FFmpeg
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 #define USE_FIXED 1
51 #include "ac3dec.h"
52 #include "codec_internal.h"
53 #define IMDCT_TYPE AV_TX_INT32_MDCT
54 
55 #include "ac3dec.h"
56 
57 static const int end_freq_inv_tab[8] =
58 {
59  50529027, 44278013, 39403370, 32292987, 27356480, 23729101, 20951060, 18755316
60 };
61 
62 static void scale_coefs (
63  int32_t *dst,
64  const int32_t *src,
65  int dynrng,
66  int len)
67 {
68  int i, shift;
69  unsigned mul, round;
70  int temp, temp1, temp2, temp3, temp4, temp5, temp6, temp7;
71 
72  mul = (dynrng & 0x1f) + 0x20;
73  shift = 4 - (sign_extend(dynrng, 9) >> 5);
74  if (shift > 0 ) {
75  round = 1 << (shift-1);
76  for (i=0; i<len; i+=8) {
77 
78  temp = src[i] * mul;
79  temp1 = src[i+1] * mul;
80  temp = temp + round;
81  temp2 = src[i+2] * mul;
82 
83  temp1 = temp1 + round;
84  dst[i] = temp >> shift;
85  temp3 = src[i+3] * mul;
86  temp2 = temp2 + round;
87 
88  dst[i+1] = temp1 >> shift;
89  temp4 = src[i + 4] * mul;
90  temp3 = temp3 + round;
91  dst[i+2] = temp2 >> shift;
92 
93  temp5 = src[i+5] * mul;
94  temp4 = temp4 + round;
95  dst[i+3] = temp3 >> shift;
96  temp6 = src[i+6] * mul;
97 
98  dst[i+4] = temp4 >> shift;
99  temp5 = temp5 + round;
100  temp7 = src[i+7] * mul;
101  temp6 = temp6 + round;
102 
103  dst[i+5] = temp5 >> shift;
104  temp7 = temp7 + round;
105  dst[i+6] = temp6 >> shift;
106  dst[i+7] = temp7 >> shift;
107 
108  }
109  } else {
110  shift = -shift;
111  mul <<= shift;
112  for (i=0; i<len; i+=8) {
113 
114  dst[i] = src[i ] * mul;
115  dst[i+1] = src[i+1] * mul;
116  dst[i+2] = src[i+2] * mul;
117  dst[i+3] = src[i+3] * mul;
118  dst[i+4] = src[i+4] * mul;
119  dst[i+5] = src[i+5] * mul;
120  dst[i+6] = src[i+6] * mul;
121  dst[i+7] = src[i+7] * mul;
122  }
123  }
124 }
125 
126 /**
127  * Downmix samples from original signal to stereo or mono (this is for 16-bit samples
128  * and fixed point decoder - original (for 32-bit samples) is in ac3dsp.c).
129  */
130 static void ac3_downmix_c_fixed16(int16_t **samples, int16_t **matrix,
131  int out_ch, int in_ch, int len)
132 {
133  int i, j;
134  int v0, v1;
135  if (out_ch == 2) {
136  for (i = 0; i < len; i++) {
137  v0 = v1 = 0;
138  for (j = 0; j < in_ch; j++) {
139  v0 += samples[j][i] * matrix[0][j];
140  v1 += samples[j][i] * matrix[1][j];
141  }
142  samples[0][i] = (v0+2048)>>12;
143  samples[1][i] = (v1+2048)>>12;
144  }
145  } else if (out_ch == 1) {
146  for (i = 0; i < len; i++) {
147  v0 = 0;
148  for (j = 0; j < in_ch; j++)
149  v0 += samples[j][i] * matrix[0][j];
150  samples[0][i] = (v0+2048)>>12;
151  }
152  }
153 }
154 
155 #include "eac3dec.c"
156 #include "ac3dec.c"
157 
158 static const AVOption options[] = {
159  { "cons_noisegen", "enable consistent noise generation", OFFSET(consistent_noise_generation), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, PAR },
160  { "drc_scale", "percentage of dynamic range compression to apply", OFFSET(drc_scale), AV_OPT_TYPE_FLOAT, {.dbl = 1.0}, 0.0, 6.0, PAR },
161  { "heavy_compr", "enable heavy dynamic range compression", OFFSET(heavy_compression), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, PAR },
162  { "downmix", "Request a specific channel layout from the decoder", OFFSET(downmix_layout), AV_OPT_TYPE_CHLAYOUT, {.str = NULL}, .flags = PAR },
163  { NULL},
164 };
165 
166 static const AVClass ac3_decoder_class = {
167  .class_name = "Fixed-Point AC-3 Decoder",
168  .item_name = av_default_item_name,
169  .option = options,
170  .version = LIBAVUTIL_VERSION_INT,
171 };
172 
174  .p.name = "ac3_fixed",
175  CODEC_LONG_NAME("ATSC A/52A (AC-3)"),
176  .p.type = AVMEDIA_TYPE_AUDIO,
177  .p.id = AV_CODEC_ID_AC3,
178  .p.priv_class = &ac3_decoder_class,
179  .priv_data_size = sizeof (AC3DecodeContext),
181  .close = ac3_decode_end,
183  .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
185  .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
187  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
188 };
AV_CODEC_ID_AC3
@ AV_CODEC_ID_AC3
Definition: codec_id.h:443
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
matrix
Definition: vc1dsp.c:42
ac3_decoder_class
static const AVClass ac3_decoder_class
Definition: ac3dec_fixed.c:166
AVOption
AVOption.
Definition: opt.h:346
FFCodec
Definition: codec_internal.h:127
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
v0
#define v0
Definition: regdef.h:26
ac3dec.h
ac3_decode_frame
static int ac3_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Decode a single AC-3 frame.
Definition: ac3dec.c:1488
scale_coefs
static void scale_coefs(int32_t *dst, const int32_t *src, int dynrng, int len)
Definition: ac3dec_fixed.c:62
ac3dec.c
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:287
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ac3_decode_init
static av_cold int ac3_decode_init(AVCodecContext *avctx)
AVCodec initialization.
Definition: ac3dec.c:208
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
AV_OPT_TYPE_CHLAYOUT
@ AV_OPT_TYPE_CHLAYOUT
Definition: opt.h:252
eac3dec.c
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:106
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
codec_internal.h
shift
static int shift(int a, int b)
Definition: bonk.c:262
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
end_freq_inv_tab
static const int end_freq_inv_tab[8]
Definition: ac3dec_fixed.c:57
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:238
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
round
static av_always_inline av_const double round(double x)
Definition: libm.h:444
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
ac3_decode_end
static av_cold int ac3_decode_end(AVCodecContext *avctx)
Uninitialize the AC-3 decoder.
Definition: ac3dec.c:1856
options
static const AVOption options[]
Definition: ac3dec_fixed.c:158
ac3_downmix_c_fixed16
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...
Definition: ac3dec_fixed.c:130
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
len
int len
Definition: vorbis_enc_data.h:426
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
OFFSET
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option keep it simple and lowercase description are in without and describe what they for example set the foo of the bar offset is the offset of the field in your see the OFFSET() macro
sign_extend
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:133
temp
else temp
Definition: vf_mcdeint.c:263
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:251
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
int32_t
int32_t
Definition: audioconvert.c:56
ff_ac3_fixed_decoder
const FFCodec ff_ac3_fixed_decoder
Definition: ac3dec_fixed.c:173
PAR
@ PAR
Definition: af_afade.c:61