You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
6.0KB

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