]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpegaudio.h
mpegaudio: remove CONFIG_AUDIO_NONSHORT
[ffmpeg] / libavcodec / mpegaudio.h
1 /*
2  * copyright (c) 2001 Fabrice Bellard
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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.
10  *
11  * Libav 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.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * mpeg audio declarations for both encoder and decoder.
24  */
25
26 #ifndef AVCODEC_MPEGAUDIO_H
27 #define AVCODEC_MPEGAUDIO_H
28
29 #ifndef CONFIG_FLOAT
30 #   define CONFIG_FLOAT 0
31 #endif
32
33 #include "avcodec.h"
34 #include "get_bits.h"
35 #include "dsputil.h"
36 #include "dct.h"
37
38 /* max frame size, in samples */
39 #define MPA_FRAME_SIZE 1152
40
41 /* max compressed frame size */
42 #define MPA_MAX_CODED_FRAME_SIZE 1792
43
44 #define MPA_MAX_CHANNELS 2
45
46 #define SBLIMIT 32 /* number of subbands */
47
48 #define MPA_STEREO  0
49 #define MPA_JSTEREO 1
50 #define MPA_DUAL    2
51 #define MPA_MONO    3
52
53 /* header + layer + bitrate + freq + lsf/mpeg25 */
54 #define SAME_HEADER_MASK \
55    (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
56
57 #define MP3_MASK 0xFFFE0CCF
58
59 #if CONFIG_MPEGAUDIO_HP
60 #define FRAC_BITS   23   /* fractional bits for sb_samples and dct */
61 #define WFRAC_BITS  16   /* fractional bits for window */
62 #else
63 #define FRAC_BITS   15   /* fractional bits for sb_samples and dct */
64 #define WFRAC_BITS  14   /* fractional bits for window */
65 #endif
66
67 #define FRAC_ONE    (1 << FRAC_BITS)
68
69 #define FIX(a)   ((int)((a) * FRAC_ONE))
70
71 #if CONFIG_FLOAT
72 typedef float OUT_INT;
73 #define OUT_FMT AV_SAMPLE_FMT_FLT
74 #else
75 typedef int16_t OUT_INT;
76 #define OUT_MAX INT16_MAX
77 #define OUT_MIN INT16_MIN
78 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
79 #define OUT_FMT AV_SAMPLE_FMT_S16
80 #endif
81
82 #if CONFIG_FLOAT
83 #   define INTFLOAT float
84 typedef float MPA_INT;
85 #elif FRAC_BITS <= 15
86 #   define INTFLOAT int
87 typedef int16_t MPA_INT;
88 #else
89 #   define INTFLOAT int
90 typedef int32_t MPA_INT;
91 #endif
92
93 #define BACKSTEP_SIZE 512
94 #define EXTRABYTES 24
95
96 /* layer 3 "granule" */
97 typedef struct GranuleDef {
98     uint8_t scfsi;
99     int part2_3_length;
100     int big_values;
101     int global_gain;
102     int scalefac_compress;
103     uint8_t block_type;
104     uint8_t switch_point;
105     int table_select[3];
106     int subblock_gain[3];
107     uint8_t scalefac_scale;
108     uint8_t count1table_select;
109     int region_size[3]; /* number of huffman codes in each region */
110     int preflag;
111     int short_start, long_end; /* long/short band indexes */
112     uint8_t scale_factors[40];
113     INTFLOAT sb_hybrid[SBLIMIT * 18]; /* 576 samples */
114 } GranuleDef;
115
116 #define MPA_DECODE_HEADER \
117     int frame_size; \
118     int error_protection; \
119     int layer; \
120     int sample_rate; \
121     int sample_rate_index; /* between 0 and 8 */ \
122     int bit_rate; \
123     int nb_channels; \
124     int mode; \
125     int mode_ext; \
126     int lsf;
127
128 typedef struct MPADecodeHeader {
129   MPA_DECODE_HEADER
130 } MPADecodeHeader;
131
132 typedef struct MPADecodeContext {
133     MPA_DECODE_HEADER
134     uint8_t last_buf[2*BACKSTEP_SIZE + EXTRABYTES];
135     int last_buf_size;
136     /* next header (used in free format parsing) */
137     uint32_t free_format_next_header;
138     GetBitContext gb;
139     GetBitContext in_gb;
140     DECLARE_ALIGNED(16, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512 * 2];
141     int synth_buf_offset[MPA_MAX_CHANNELS];
142     DECLARE_ALIGNED(16, INTFLOAT, sb_samples)[MPA_MAX_CHANNELS][36][SBLIMIT];
143     INTFLOAT mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
144     GranuleDef granules[2][2]; /* Used in Layer 3 */
145 #ifdef DEBUG
146     int frame_count;
147 #endif
148     int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
149     int dither_state;
150     int error_recognition;
151     AVCodecContext* avctx;
152 #if CONFIG_FLOAT
153     DCTContext dct;
154 #endif
155     void (*apply_window_mp3)(MPA_INT *synth_buf, MPA_INT *window,
156                              int *dither_state, OUT_INT *samples, int incr);
157 } MPADecodeContext;
158
159 /* layer 3 huffman tables */
160 typedef struct HuffTable {
161     int xsize;
162     const uint8_t *bits;
163     const uint16_t *codes;
164 } HuffTable;
165
166 int ff_mpa_l2_select_table(int bitrate, int nb_channels, int freq, int lsf);
167 int ff_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bitrate);
168 extern MPA_INT ff_mpa_synth_window[];
169 void ff_mpa_synth_init(MPA_INT *window);
170 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
171                          MPA_INT *window, int *dither_state,
172                          OUT_INT *samples, int incr,
173                          INTFLOAT sb_samples[SBLIMIT]);
174
175 void ff_mpa_synth_init_float(MPA_INT *window);
176 void ff_mpa_synth_filter_float(MPADecodeContext *s,
177                          MPA_INT *synth_buf_ptr, int *synth_buf_offset,
178                          MPA_INT *window, int *dither_state,
179                          OUT_INT *samples, int incr,
180                          INTFLOAT sb_samples[SBLIMIT]);
181
182 void ff_mpegaudiodec_init_mmx(MPADecodeContext *s);
183 void ff_mpegaudiodec_init_altivec(MPADecodeContext *s);
184
185 /* fast header check for resync */
186 static inline int ff_mpa_check_header(uint32_t header){
187     /* header */
188     if ((header & 0xffe00000) != 0xffe00000)
189         return -1;
190     /* layer check */
191     if ((header & (3<<17)) == 0)
192         return -1;
193     /* bit rate */
194     if ((header & (0xf<<12)) == 0xf<<12)
195         return -1;
196     /* frequency */
197     if ((header & (3<<10)) == 3<<10)
198         return -1;
199     return 0;
200 }
201
202 #endif /* AVCODEC_MPEGAUDIO_H */