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.

190 lines
6.7KB

  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. #define AAC_CUTOFF(s) (s->bit_rate ? FFMIN3(4000 + s->bit_rate/8, 12000 + s->bit_rate/32, s->sample_rate / 2) : (s->sample_rate / 2))
  29. /**
  30. * single band psychoacoustic information
  31. */
  32. typedef struct FFPsyBand {
  33. int bits;
  34. float energy;
  35. float threshold;
  36. float distortion;
  37. float perceptual_weight;
  38. } FFPsyBand;
  39. /**
  40. * single channel psychoacoustic information
  41. */
  42. typedef struct FFPsyChannel {
  43. FFPsyBand psy_bands[PSY_MAX_BANDS]; ///< channel bands information
  44. float entropy; ///< total PE for this channel
  45. } FFPsyChannel;
  46. /**
  47. * psychoacoustic information for an arbitrary group of channels
  48. */
  49. typedef struct FFPsyChannelGroup {
  50. FFPsyChannel *ch[PSY_MAX_CHANS]; ///< pointers to the individual channels in the group
  51. uint8_t num_ch; ///< number of channels in this group
  52. uint8_t coupling[PSY_MAX_BANDS]; ///< allow coupling for this band in the group
  53. } FFPsyChannelGroup;
  54. /**
  55. * windowing related information
  56. */
  57. typedef struct FFPsyWindowInfo {
  58. int window_type[3]; ///< window type (short/long/transitional, etc.) - current, previous and next
  59. int window_shape; ///< window shape (sine/KBD/whatever)
  60. int num_windows; ///< number of windows in a frame
  61. int grouping[8]; ///< window grouping (for e.g. AAC)
  62. int *window_sizes; ///< sequence of window sizes inside one frame (for eg. WMA)
  63. } FFPsyWindowInfo;
  64. /**
  65. * context used by psychoacoustic model
  66. */
  67. typedef struct FFPsyContext {
  68. AVCodecContext *avctx; ///< encoder context
  69. const struct FFPsyModel *model; ///< encoder-specific model functions
  70. FFPsyChannel *ch; ///< single channel information
  71. FFPsyChannelGroup *group; ///< channel group information
  72. int num_groups; ///< number of channel groups
  73. uint8_t **bands; ///< scalefactor band sizes for possible frame sizes
  74. int *num_bands; ///< number of scalefactor bands for possible frame sizes
  75. int num_lens; ///< number of scalefactor band sets
  76. struct {
  77. int size; ///< size of the bitresevoir in bits
  78. int bits; ///< number of bits used in the bitresevoir
  79. } bitres;
  80. void* model_priv_data; ///< psychoacoustic model implementation private data
  81. } FFPsyContext;
  82. /**
  83. * codec-specific psychoacoustic model implementation
  84. */
  85. typedef struct FFPsyModel {
  86. const char *name;
  87. int (*init) (FFPsyContext *apc);
  88. /**
  89. * Suggest window sequence for channel.
  90. *
  91. * @param ctx model context
  92. * @param audio samples for the current frame
  93. * @param la lookahead samples (NULL when unavailable)
  94. * @param channel number of channel element to analyze
  95. * @param prev_type previous window type
  96. *
  97. * @return suggested window information in a structure
  98. */
  99. FFPsyWindowInfo (*window)(FFPsyContext *ctx, const float *audio, const float *la, int channel, int prev_type);
  100. /**
  101. * Perform psychoacoustic analysis and set band info (threshold, energy) for a group of channels.
  102. *
  103. * @param ctx model context
  104. * @param channel channel number of the first channel in the group to perform analysis on
  105. * @param coeffs array of pointers to the transformed coefficients
  106. * @param wi window information for the channels in the group
  107. */
  108. void (*analyze)(FFPsyContext *ctx, int channel, const float **coeffs, const FFPsyWindowInfo *wi);
  109. void (*end) (FFPsyContext *apc);
  110. } FFPsyModel;
  111. /**
  112. * Initialize psychoacoustic model.
  113. *
  114. * @param ctx model context
  115. * @param avctx codec context
  116. * @param num_lens number of possible frame lengths
  117. * @param bands scalefactor band lengths for all frame lengths
  118. * @param num_bands number of scalefactor bands for all frame lengths
  119. * @param num_groups number of channel groups
  120. * @param group_map array with # of channels in group - 1, for each group
  121. *
  122. * @return zero if successful, a negative value if not
  123. */
  124. av_cold int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx, int num_lens,
  125. const uint8_t **bands, const int* num_bands,
  126. int num_groups, const uint8_t *group_map);
  127. /**
  128. * Determine what group a channel belongs to.
  129. *
  130. * @param ctx psymodel context
  131. * @param channel channel to locate the group for
  132. *
  133. * @return pointer to the FFPsyChannelGroup this channel belongs to
  134. */
  135. FFPsyChannelGroup *ff_psy_find_group(FFPsyContext *ctx, int channel);
  136. /**
  137. * Cleanup model context at the end.
  138. *
  139. * @param ctx model context
  140. */
  141. av_cold void ff_psy_end(FFPsyContext *ctx);
  142. /**************************************************************************
  143. * Audio preprocessing stuff. *
  144. * This should be moved into some audio filter eventually. *
  145. **************************************************************************/
  146. struct FFPsyPreprocessContext;
  147. /**
  148. * psychoacoustic model audio preprocessing initialization
  149. */
  150. av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *avctx);
  151. /**
  152. * Preprocess several channel in audio frame in order to compress it better.
  153. *
  154. * @param ctx preprocessing context
  155. * @param audio samples to be filtered (in place)
  156. * @param channels number of channel to preprocess
  157. */
  158. void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx, float **audio, int channels);
  159. /**
  160. * Cleanup audio preprocessing module.
  161. */
  162. av_cold void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx);
  163. #endif /* AVCODEC_PSYMODEL_H */