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.

205 lines
5.8KB

  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 "dct.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. #ifndef FRAC_BITS
  49. #define FRAC_BITS 23 /* fractional bits for sb_samples and dct */
  50. #define WFRAC_BITS 16 /* fractional bits for window */
  51. #endif
  52. #define FRAC_ONE (1 << FRAC_BITS)
  53. #define FIX(a) ((int)((a) * FRAC_ONE))
  54. #if CONFIG_FLOAT
  55. typedef float OUT_INT;
  56. #define OUT_FMT AV_SAMPLE_FMT_FLT
  57. #elif CONFIG_MPEGAUDIO_HP && CONFIG_AUDIO_NONSHORT
  58. typedef int32_t OUT_INT;
  59. #define OUT_MAX INT32_MAX
  60. #define OUT_MIN INT32_MIN
  61. #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 31)
  62. #define OUT_FMT AV_SAMPLE_FMT_S32
  63. #else
  64. typedef int16_t OUT_INT;
  65. #define OUT_MAX INT16_MAX
  66. #define OUT_MIN INT16_MIN
  67. #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
  68. #define OUT_FMT AV_SAMPLE_FMT_S16
  69. #endif
  70. #if CONFIG_FLOAT
  71. # define INTFLOAT float
  72. typedef float MPA_INT;
  73. #elif FRAC_BITS <= 15
  74. # define INTFLOAT int
  75. typedef int16_t MPA_INT;
  76. #else
  77. # define INTFLOAT int
  78. typedef int32_t MPA_INT;
  79. #endif
  80. #define BACKSTEP_SIZE 512
  81. #define EXTRABYTES 24
  82. /* layer 3 "granule" */
  83. typedef struct GranuleDef {
  84. uint8_t scfsi;
  85. int part2_3_length;
  86. int big_values;
  87. int global_gain;
  88. int scalefac_compress;
  89. uint8_t block_type;
  90. uint8_t switch_point;
  91. int table_select[3];
  92. int subblock_gain[3];
  93. uint8_t scalefac_scale;
  94. uint8_t count1table_select;
  95. int region_size[3]; /* number of huffman codes in each region */
  96. int preflag;
  97. int short_start, long_end; /* long/short band indexes */
  98. uint8_t scale_factors[40];
  99. INTFLOAT sb_hybrid[SBLIMIT * 18]; /* 576 samples */
  100. } GranuleDef;
  101. #define MPA_DECODE_HEADER \
  102. int frame_size; \
  103. int error_protection; \
  104. int layer; \
  105. int sample_rate; \
  106. int sample_rate_index; /* between 0 and 8 */ \
  107. int bit_rate; \
  108. int nb_channels; \
  109. int mode; \
  110. int mode_ext; \
  111. int lsf;
  112. typedef struct MPADecodeHeader {
  113. MPA_DECODE_HEADER
  114. } MPADecodeHeader;
  115. typedef struct MPADecodeContext {
  116. MPA_DECODE_HEADER
  117. uint8_t last_buf[2*BACKSTEP_SIZE + EXTRABYTES];
  118. int last_buf_size;
  119. /* next header (used in free format parsing) */
  120. uint32_t free_format_next_header;
  121. GetBitContext gb;
  122. GetBitContext in_gb;
  123. DECLARE_ALIGNED(16, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512 * 2];
  124. int synth_buf_offset[MPA_MAX_CHANNELS];
  125. DECLARE_ALIGNED(16, INTFLOAT, sb_samples)[MPA_MAX_CHANNELS][36][SBLIMIT];
  126. INTFLOAT mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
  127. GranuleDef granules[2][2]; /* Used in Layer 3 */
  128. int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
  129. int dither_state;
  130. int error_recognition;
  131. AVCodecContext* avctx;
  132. #if CONFIG_FLOAT
  133. DCTContext dct;
  134. #endif
  135. void (*apply_window_mp3)(MPA_INT *synth_buf, MPA_INT *window,
  136. int *dither_state, OUT_INT *samples, int incr);
  137. } MPADecodeContext;
  138. /* layer 3 huffman tables */
  139. typedef struct HuffTable {
  140. int xsize;
  141. const uint8_t *bits;
  142. const uint16_t *codes;
  143. } HuffTable;
  144. int ff_mpa_l2_select_table(int bitrate, int nb_channels, int freq, int lsf);
  145. int ff_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bitrate);
  146. extern MPA_INT ff_mpa_synth_window[];
  147. void ff_mpa_synth_init(MPA_INT *window);
  148. void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
  149. MPA_INT *window, int *dither_state,
  150. OUT_INT *samples, int incr,
  151. INTFLOAT sb_samples[SBLIMIT]);
  152. void ff_mpa_synth_init_float(MPA_INT *window);
  153. void ff_mpa_synth_filter_float(MPADecodeContext *s,
  154. 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_mpegaudiodec_init_mmx(MPADecodeContext *s);
  159. void ff_mpegaudiodec_init_altivec(MPADecodeContext *s);
  160. /* fast header check for resync */
  161. static inline int ff_mpa_check_header(uint32_t header){
  162. /* header */
  163. if ((header & 0xffe00000) != 0xffe00000)
  164. return -1;
  165. /* layer check */
  166. if ((header & (3<<17)) == 0)
  167. return -1;
  168. /* bit rate */
  169. if ((header & (0xf<<12)) == 0xf<<12)
  170. return -1;
  171. /* frequency */
  172. if ((header & (3<<10)) == 3<<10)
  173. return -1;
  174. return 0;
  175. }
  176. #endif /* AVCODEC_MPEGAUDIO_H */