Move some mpegaudio functions to new mpegaudiodsp subsystem
[libav.git] / 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
37 /* max frame size, in samples */
38 #define MPA_FRAME_SIZE 1152
39
40 /* max compressed frame size */
41 #define MPA_MAX_CODED_FRAME_SIZE 1792
42
43 #define MPA_MAX_CHANNELS 2
44
45 #define SBLIMIT 32 /* number of subbands */
46
47 #define MPA_STEREO 0
48 #define MPA_JSTEREO 1
49 #define MPA_DUAL 2
50 #define MPA_MONO 3
51
52 /* header + layer + bitrate + freq + lsf/mpeg25 */
53 #define SAME_HEADER_MASK \
54 (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
55
56 #define MP3_MASK 0xFFFE0CCF
57
58 #ifndef FRAC_BITS
59 #define FRAC_BITS 23 /* fractional bits for sb_samples and dct */
60 #define WFRAC_BITS 16 /* fractional bits for window */
61 #endif
62
63 #define FRAC_ONE (1 << FRAC_BITS)
64
65 #define FIX(a) ((int)((a) * FRAC_ONE))
66
67 #if CONFIG_FLOAT
68 typedef float OUT_INT;
69 #else
70 typedef int16_t OUT_INT;
71 #endif
72
73 #if CONFIG_FLOAT
74 # define INTFLOAT float
75 typedef float MPA_INT;
76 #elif FRAC_BITS <= 15
77 # define INTFLOAT int
78 typedef int16_t MPA_INT;
79 #else
80 # define INTFLOAT int
81 typedef int32_t MPA_INT;
82 #endif
83
84 #define BACKSTEP_SIZE 512
85 #define EXTRABYTES 24
86
87 /* layer 3 "granule" */
88 typedef struct GranuleDef {
89 uint8_t scfsi;
90 int part2_3_length;
91 int big_values;
92 int global_gain;
93 int scalefac_compress;
94 uint8_t block_type;
95 uint8_t switch_point;
96 int table_select[3];
97 int subblock_gain[3];
98 uint8_t scalefac_scale;
99 uint8_t count1table_select;
100 int region_size[3]; /* number of huffman codes in each region */
101 int preflag;
102 int short_start, long_end; /* long/short band indexes */
103 uint8_t scale_factors[40];
104 INTFLOAT sb_hybrid[SBLIMIT * 18]; /* 576 samples */
105 } GranuleDef;
106
107 #define MPA_DECODE_HEADER \
108 int frame_size; \
109 int error_protection; \
110 int layer; \
111 int sample_rate; \
112 int sample_rate_index; /* between 0 and 8 */ \
113 int bit_rate; \
114 int nb_channels; \
115 int mode; \
116 int mode_ext; \
117 int lsf;
118
119 typedef struct MPADecodeHeader {
120 MPA_DECODE_HEADER
121 } MPADecodeHeader;
122
123 typedef struct MPADecodeContext {
124 MPA_DECODE_HEADER
125 uint8_t last_buf[2*BACKSTEP_SIZE + EXTRABYTES];
126 int last_buf_size;
127 /* next header (used in free format parsing) */
128 uint32_t free_format_next_header;
129 GetBitContext gb;
130 GetBitContext in_gb;
131 DECLARE_ALIGNED(16, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512 * 2];
132 int synth_buf_offset[MPA_MAX_CHANNELS];
133 DECLARE_ALIGNED(16, INTFLOAT, sb_samples)[MPA_MAX_CHANNELS][36][SBLIMIT];
134 INTFLOAT mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
135 GranuleDef granules[2][2]; /* Used in Layer 3 */
136 #ifdef DEBUG
137 int frame_count;
138 #endif
139 int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
140 int dither_state;
141 int error_recognition;
142 AVCodecContext* avctx;
143 MPADSPContext mpadsp;
144 } MPADecodeContext;
145
146 /* layer 3 huffman tables */
147 typedef struct HuffTable {
148 int xsize;
149 const uint8_t *bits;
150 const uint16_t *codes;
151 } HuffTable;
152
153 int ff_mpa_l2_select_table(int bitrate, int nb_channels, int freq, int lsf);
154 int ff_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bitrate);
155
156 /* fast header check for resync */
157 static inline int ff_mpa_check_header(uint32_t header){
158 /* header */
159 if ((header & 0xffe00000) != 0xffe00000)
160 return -1;
161 /* layer check */
162 if ((header & (3<<17)) == 0)
163 return -1;
164 /* bit rate */
165 if ((header & (0xf<<12)) == 0xf<<12)
166 return -1;
167 /* frequency */
168 if ((header & (3<<10)) == 3<<10)
169 return -1;
170 return 0;
171 }
172
173 #endif /* AVCODEC_MPEGAUDIO_H */