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.

188 lines
6.5KB

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