psymodel.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * audio encoder psychoacoustic model
  3. * Copyright (C) 2008 Konstantin Shishkov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #ifndef AVCODEC_PSYMODEL_H
  22. #define AVCODEC_PSYMODEL_H
  23. #include "avcodec.h"
  24. /** maximum possible number of bands */
  25. #define PSY_MAX_BANDS 128
  26. /** maximum number of channels */
  27. #define PSY_MAX_CHANS 20
  28. /* cutoff for VBR is purposely increased, since LP filtering actually
  29. * hinders VBR performance rather than the opposite
  30. */
  31. #define AAC_CUTOFF_FROM_BITRATE(bit_rate,channels,sample_rate) (bit_rate ? FFMIN3(FFMIN3( \
  32. FFMAX(bit_rate/channels/5, bit_rate/channels*15/32 - 5500), \
  33. 3000 + bit_rate/channels/4, \
  34. 12000 + bit_rate/channels/16), \
  35. 22000, \
  36. sample_rate / 2): (sample_rate / 2))
  37. #define AAC_CUTOFF(s) ( \
  38. (s->flags & AV_CODEC_FLAG_QSCALE) \
  39. ? s->sample_rate / 2 \
  40. : AAC_CUTOFF_FROM_BITRATE(s->bit_rate, s->channels, s->sample_rate) \
  41. )
  42. /**
  43. * single band psychoacoustic information
  44. */
  45. typedef struct FFPsyBand {
  46. int bits;
  47. float energy;
  48. float threshold;
  49. float spread; /* Energy spread over the band */
  50. } FFPsyBand;
  51. /**
  52. * single channel psychoacoustic information
  53. */
  54. typedef struct FFPsyChannel {
  55. FFPsyBand psy_bands[PSY_MAX_BANDS]; ///< channel bands information
  56. float entropy; ///< total PE for this channel
  57. } FFPsyChannel;
  58. /**
  59. * psychoacoustic information for an arbitrary group of channels
  60. */
  61. typedef struct FFPsyChannelGroup {
  62. FFPsyChannel *ch[PSY_MAX_CHANS]; ///< pointers to the individual channels in the group
  63. uint8_t num_ch; ///< number of channels in this group
  64. uint8_t coupling[PSY_MAX_BANDS]; ///< allow coupling for this band in the group
  65. } FFPsyChannelGroup;
  66. /**
  67. * windowing related information
  68. */
  69. typedef struct FFPsyWindowInfo {
  70. int window_type[3]; ///< window type (short/long/transitional, etc.) - current, previous and next
  71. int window_shape; ///< window shape (sine/KBD/whatever)
  72. int num_windows; ///< number of windows in a frame
  73. int grouping[8]; ///< window grouping (for e.g. AAC)
  74. float clipping[8]; ///< maximum absolute normalized intensity in the given window for clip avoidance
  75. int *window_sizes; ///< sequence of window sizes inside one frame (for eg. WMA)
  76. } FFPsyWindowInfo;
  77. /**
  78. * context used by psychoacoustic model
  79. */
  80. typedef struct FFPsyContext {
  81. AVCodecContext *avctx; ///< encoder context
  82. const struct FFPsyModel *model; ///< encoder-specific model functions
  83. FFPsyChannel *ch; ///< single channel information
  84. FFPsyChannelGroup *group; ///< channel group information
  85. int num_groups; ///< number of channel groups
  86. int cutoff; ///< lowpass frequency cutoff for analysis
  87. uint8_t **bands; ///< scalefactor band sizes for possible frame sizes
  88. int *num_bands; ///< number of scalefactor bands for possible frame sizes
  89. int num_lens; ///< number of scalefactor band sets
  90. struct {
  91. int size; ///< size of the bitresevoir in bits
  92. int bits; ///< number of bits used in the bitresevoir
  93. int alloc; ///< number of bits allocated by the psy, or -1 if no allocation was done
  94. } bitres;
  95. void* model_priv_data; ///< psychoacoustic model implementation private data
  96. } FFPsyContext;
  97. /**
  98. * codec-specific psychoacoustic model implementation
  99. */
  100. typedef struct FFPsyModel {
  101. const char *name;
  102. int (*init) (FFPsyContext *apc);
  103. /**
  104. * Suggest window sequence for channel.
  105. *
  106. * @param ctx model context
  107. * @param audio samples for the current frame
  108. * @param la lookahead samples (NULL when unavailable)
  109. * @param channel number of channel element to analyze
  110. * @param prev_type previous window type
  111. *
  112. * @return suggested window information in a structure
  113. */
  114. FFPsyWindowInfo (*window)(FFPsyContext *ctx, const float *audio, const float *la, int channel, int prev_type);
  115. /**
  116. * Perform psychoacoustic analysis and set band info (threshold, energy) for a group of channels.
  117. *
  118. * @param ctx model context
  119. * @param channel channel number of the first channel in the group to perform analysis on
  120. * @param coeffs array of pointers to the transformed coefficients
  121. * @param wi window information for the channels in the group
  122. */
  123. void (*analyze)(FFPsyContext *ctx, int channel, const float **coeffs, const FFPsyWindowInfo *wi);
  124. void (*end) (FFPsyContext *apc);
  125. } FFPsyModel;
  126. /**
  127. * Initialize psychoacoustic model.
  128. *
  129. * @param ctx model context
  130. * @param avctx codec context
  131. * @param num_lens number of possible frame lengths
  132. * @param bands scalefactor band lengths for all frame lengths
  133. * @param num_bands number of scalefactor bands for all frame lengths
  134. * @param num_groups number of channel groups
  135. * @param group_map array with # of channels in group - 1, for each group
  136. *
  137. * @return zero if successful, a negative value if not
  138. */
  139. int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx, int num_lens,
  140. const uint8_t **bands, const int *num_bands,
  141. int num_groups, const uint8_t *group_map);
  142. /**
  143. * Determine what group a channel belongs to.
  144. *
  145. * @param ctx psymodel context
  146. * @param channel channel to locate the group for
  147. *
  148. * @return pointer to the FFPsyChannelGroup this channel belongs to
  149. */
  150. FFPsyChannelGroup *ff_psy_find_group(FFPsyContext *ctx, int channel);
  151. /**
  152. * Cleanup model context at the end.
  153. *
  154. * @param ctx model context
  155. */
  156. void ff_psy_end(FFPsyContext *ctx);
  157. /**************************************************************************
  158. * Audio preprocessing stuff. *
  159. * This should be moved into some audio filter eventually. *
  160. **************************************************************************/
  161. struct FFPsyPreprocessContext;
  162. /**
  163. * psychoacoustic model audio preprocessing initialization
  164. */
  165. struct FFPsyPreprocessContext *ff_psy_preprocess_init(AVCodecContext *avctx);
  166. /**
  167. * Preprocess several channel in audio frame in order to compress it better.
  168. *
  169. * @param ctx preprocessing context
  170. * @param audio samples to be filtered (in place)
  171. * @param channels number of channel to preprocess
  172. */
  173. void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx, float **audio, int channels);
  174. /**
  175. * Cleanup audio preprocessing module.
  176. */
  177. void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx);
  178. #endif /* AVCODEC_PSYMODEL_H */