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.

203 lines
5.7KB

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