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.

196 lines
5.5KB

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