mpegaudio.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 libavcodec/mpegaudio.h
  22. * mpeg audio declarations for both encoder and decoder.
  23. */
  24. #ifndef AVCODEC_MPEGAUDIO_H
  25. #define AVCODEC_MPEGAUDIO_H
  26. #include "avcodec.h"
  27. #include "bitstream.h"
  28. #include "dsputil.h"
  29. #define CONFIG_AUDIO_NONSHORT 0
  30. /* max frame size, in samples */
  31. #define MPA_FRAME_SIZE 1152
  32. /* max compressed frame size */
  33. #define MPA_MAX_CODED_FRAME_SIZE 1792
  34. #define MPA_MAX_CHANNELS 2
  35. #define SBLIMIT 32 /* number of subbands */
  36. #define MPA_STEREO 0
  37. #define MPA_JSTEREO 1
  38. #define MPA_DUAL 2
  39. #define MPA_MONO 3
  40. /* header + layer + bitrate + freq + lsf/mpeg25 */
  41. #define SAME_HEADER_MASK \
  42. (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
  43. #define MP3_MASK 0xFFFE0CCF
  44. #if CONFIG_MPEGAUDIO_HP
  45. #define FRAC_BITS 23 /* fractional bits for sb_samples and dct */
  46. #define WFRAC_BITS 16 /* fractional bits for window */
  47. #else
  48. #define FRAC_BITS 15 /* fractional bits for sb_samples and dct */
  49. #define WFRAC_BITS 14 /* fractional bits for window */
  50. #endif
  51. #define FRAC_ONE (1 << FRAC_BITS)
  52. #define FIX(a) ((int)((a) * FRAC_ONE))
  53. #if CONFIG_MPEGAUDIO_HP && CONFIG_AUDIO_NONSHORT
  54. typedef int32_t OUT_INT;
  55. #define OUT_MAX INT32_MAX
  56. #define OUT_MIN INT32_MIN
  57. #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 31)
  58. #define OUT_FMT SAMPLE_FMT_S32
  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 SAMPLE_FMT_S16
  65. #endif
  66. #if FRAC_BITS <= 15
  67. typedef int16_t MPA_INT;
  68. #else
  69. typedef int32_t MPA_INT;
  70. #endif
  71. #define BACKSTEP_SIZE 512
  72. #define EXTRABYTES 24
  73. struct GranuleDef;
  74. #define MPA_DECODE_HEADER \
  75. int frame_size; \
  76. int error_protection; \
  77. int layer; \
  78. int sample_rate; \
  79. int sample_rate_index; /* between 0 and 8 */ \
  80. int bit_rate; \
  81. int nb_channels; \
  82. int mode; \
  83. int mode_ext; \
  84. int lsf;
  85. typedef struct MPADecodeHeader {
  86. MPA_DECODE_HEADER
  87. } MPADecodeHeader;
  88. typedef struct MPADecodeContext {
  89. MPA_DECODE_HEADER
  90. DECLARE_ALIGNED_8(uint8_t, last_buf[2*BACKSTEP_SIZE + EXTRABYTES]);
  91. int last_buf_size;
  92. /* next header (used in free format parsing) */
  93. uint32_t free_format_next_header;
  94. GetBitContext gb;
  95. GetBitContext in_gb;
  96. DECLARE_ALIGNED_16(MPA_INT, synth_buf[MPA_MAX_CHANNELS][512 * 2]);
  97. int synth_buf_offset[MPA_MAX_CHANNELS];
  98. DECLARE_ALIGNED_16(int32_t, sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT]);
  99. int32_t mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
  100. #ifdef DEBUG
  101. int frame_count;
  102. #endif
  103. void (*compute_antialias)(struct MPADecodeContext *s, struct GranuleDef *g);
  104. int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
  105. int dither_state;
  106. int error_recognition;
  107. AVCodecContext* avctx;
  108. } MPADecodeContext;
  109. /* layer 3 huffman tables */
  110. typedef struct HuffTable {
  111. int xsize;
  112. const uint8_t *bits;
  113. const uint16_t *codes;
  114. } HuffTable;
  115. int ff_mpa_l2_select_table(int bitrate, int nb_channels, int freq, int lsf);
  116. int ff_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bitrate);
  117. void ff_mpa_synth_init(MPA_INT *window);
  118. void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
  119. MPA_INT *window, int *dither_state,
  120. OUT_INT *samples, int incr,
  121. int32_t sb_samples[SBLIMIT]);
  122. /* fast header check for resync */
  123. static inline int ff_mpa_check_header(uint32_t header){
  124. /* header */
  125. if ((header & 0xffe00000) != 0xffe00000)
  126. return -1;
  127. /* layer check */
  128. if ((header & (3<<17)) == 0)
  129. return -1;
  130. /* bit rate */
  131. if ((header & (0xf<<12)) == 0xf<<12)
  132. return -1;
  133. /* frequency */
  134. if ((header & (3<<10)) == 3<<10)
  135. return -1;
  136. return 0;
  137. }
  138. #endif /* AVCODEC_MPEGAUDIO_H */