2 * copyright (c) 2001 Fabrice Bellard
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * mpeg audio declarations for both encoder and decoder.
26 #ifndef AVCODEC_MPEGAUDIO_H
27 #define AVCODEC_MPEGAUDIO_H
30 # define CONFIG_FLOAT 0
37 #define CONFIG_AUDIO_NONSHORT 0
39 /* max frame size, in samples */
40 #define MPA_FRAME_SIZE 1152
42 /* max compressed frame size */
43 #define MPA_MAX_CODED_FRAME_SIZE 1792
45 #define MPA_MAX_CHANNELS 2
47 #define SBLIMIT 32 /* number of subbands */
54 /* header + layer + bitrate + freq + lsf/mpeg25 */
55 #define SAME_HEADER_MASK \
56 (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
58 #define MP3_MASK 0xFFFE0CCF
60 #if CONFIG_MPEGAUDIO_HP
61 #define FRAC_BITS 23 /* fractional bits for sb_samples and dct */
62 #define WFRAC_BITS 16 /* fractional bits for window */
64 #define FRAC_BITS 15 /* fractional bits for sb_samples and dct */
65 #define WFRAC_BITS 14 /* fractional bits for window */
68 #define FRAC_ONE (1 << FRAC_BITS)
70 #define FIX(a) ((int)((a) * FRAC_ONE))
73 typedef float OUT_INT;
74 #define OUT_FMT SAMPLE_FMT_FLT
75 #elif CONFIG_MPEGAUDIO_HP && CONFIG_AUDIO_NONSHORT
76 typedef int32_t OUT_INT;
77 #define OUT_MAX INT32_MAX
78 #define OUT_MIN INT32_MIN
79 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 31)
80 #define OUT_FMT SAMPLE_FMT_S32
82 typedef int16_t OUT_INT;
83 #define OUT_MAX INT16_MAX
84 #define OUT_MIN INT16_MIN
85 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
86 #define OUT_FMT SAMPLE_FMT_S16
90 # define INTFLOAT float
91 typedef float MPA_INT;
94 typedef int16_t MPA_INT;
97 typedef int32_t MPA_INT;
100 #define BACKSTEP_SIZE 512
101 #define EXTRABYTES 24
103 /* layer 3 "granule" */
104 typedef struct GranuleDef {
109 int scalefac_compress;
111 uint8_t switch_point;
113 int subblock_gain[3];
114 uint8_t scalefac_scale;
115 uint8_t count1table_select;
116 int region_size[3]; /* number of huffman codes in each region */
118 int short_start, long_end; /* long/short band indexes */
119 uint8_t scale_factors[40];
120 INTFLOAT sb_hybrid[SBLIMIT * 18]; /* 576 samples */
123 #define MPA_DECODE_HEADER \
125 int error_protection; \
128 int sample_rate_index; /* between 0 and 8 */ \
135 typedef struct MPADecodeHeader {
139 typedef struct MPADecodeContext {
141 uint8_t last_buf[2*BACKSTEP_SIZE + EXTRABYTES];
143 /* next header (used in free format parsing) */
144 uint32_t free_format_next_header;
147 DECLARE_ALIGNED(16, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512 * 2];
148 int synth_buf_offset[MPA_MAX_CHANNELS];
149 DECLARE_ALIGNED(16, INTFLOAT, sb_samples)[MPA_MAX_CHANNELS][36][SBLIMIT];
150 INTFLOAT mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
151 GranuleDef granules[2][2]; /* Used in Layer 3 */
155 int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
157 int error_recognition;
158 AVCodecContext* avctx;
159 void (*apply_window_mp3)(MPA_INT *synth_buf, MPA_INT *window,
160 int *dither_state, OUT_INT *samples, int incr);
163 /* layer 3 huffman tables */
164 typedef struct HuffTable {
167 const uint16_t *codes;
170 int ff_mpa_l2_select_table(int bitrate, int nb_channels, int freq, int lsf);
171 int ff_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bitrate);
172 extern MPA_INT ff_mpa_synth_window[];
173 void ff_mpa_synth_init(MPA_INT *window);
174 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
175 MPA_INT *window, int *dither_state,
176 OUT_INT *samples, int incr,
177 INTFLOAT sb_samples[SBLIMIT]);
179 void ff_mpa_synth_init_float(MPA_INT *window);
180 void ff_mpa_synth_filter_float(MPADecodeContext *s,
181 MPA_INT *synth_buf_ptr, int *synth_buf_offset,
182 MPA_INT *window, int *dither_state,
183 OUT_INT *samples, int incr,
184 INTFLOAT sb_samples[SBLIMIT]);
186 /* fast header check for resync */
187 static inline int ff_mpa_check_header(uint32_t header){
189 if ((header & 0xffe00000) != 0xffe00000)
192 if ((header & (3<<17)) == 0)
195 if ((header & (0xf<<12)) == 0xf<<12)
198 if ((header & (3<<10)) == 3<<10)
203 #endif /* AVCODEC_MPEGAUDIO_H */