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.

168 lines
5.8KB

  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. * windowing related information
  40. */
  41. typedef struct FFPsyWindowInfo {
  42. int window_type[3]; ///< window type (short/long/transitional, etc.) - current, previous and next
  43. int window_shape; ///< window shape (sine/KBD/whatever)
  44. int num_windows; ///< number of windows in a frame
  45. int grouping[8]; ///< window grouping (for e.g. AAC)
  46. int *window_sizes; ///< sequence of window sizes inside one frame (for eg. WMA)
  47. } FFPsyWindowInfo;
  48. /**
  49. * context used by psychoacoustic model
  50. */
  51. typedef struct FFPsyContext {
  52. AVCodecContext *avctx; ///< encoder context
  53. const struct FFPsyModel *model; ///< encoder-specific model functions
  54. FFPsyBand *psy_bands; ///< frame bands information
  55. uint8_t **bands; ///< scalefactor band sizes for possible frame sizes
  56. int *num_bands; ///< number of scalefactor bands for possible frame sizes
  57. int num_lens; ///< number of scalefactor band sets
  58. float pe[PSY_MAX_CHANS]; ///< total PE for each channel in the frame
  59. struct {
  60. int size; ///< size of the bitresevoir in bits
  61. int bits; ///< number of bits used in the bitresevoir
  62. } bitres;
  63. void* model_priv_data; ///< psychoacoustic model implementation private data
  64. } FFPsyContext;
  65. /**
  66. * codec-specific psychoacoustic model implementation
  67. */
  68. typedef struct FFPsyModel {
  69. const char *name;
  70. int (*init) (FFPsyContext *apc);
  71. FFPsyWindowInfo (*window)(FFPsyContext *ctx, const int16_t *audio, const int16_t *la, int channel, int prev_type);
  72. void (*analyze)(FFPsyContext *ctx, int channel, const float *coeffs, const FFPsyWindowInfo *wi);
  73. void (*end) (FFPsyContext *apc);
  74. } FFPsyModel;
  75. /**
  76. * Initialize psychoacoustic model.
  77. *
  78. * @param ctx model context
  79. * @param avctx codec context
  80. * @param num_lens number of possible frame lengths
  81. * @param bands scalefactor band lengths for all frame lengths
  82. * @param num_bands number of scalefactor bands for all frame lengths
  83. *
  84. * @return zero if successful, a negative value if not
  85. */
  86. av_cold int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx,
  87. int num_lens,
  88. const uint8_t **bands, const int* num_bands);
  89. /**
  90. * Suggest window sequence for channel.
  91. *
  92. * @param ctx model context
  93. * @param audio samples for the current frame
  94. * @param la lookahead samples (NULL when unavailable)
  95. * @param channel number of channel element to analyze
  96. * @param prev_type previous window type
  97. *
  98. * @return suggested window information in a structure
  99. */
  100. FFPsyWindowInfo ff_psy_suggest_window(FFPsyContext *ctx,
  101. const int16_t *audio, const int16_t *la,
  102. int channel, int prev_type);
  103. /**
  104. * Perform psychoacoustic analysis and set band info (threshold, energy).
  105. *
  106. * @param ctx model context
  107. * @param channel audio channel number
  108. * @param coeffs pointer to the transformed coefficients
  109. * @param wi window information
  110. */
  111. void ff_psy_set_band_info(FFPsyContext *ctx, int channel, const float *coeffs,
  112. const FFPsyWindowInfo *wi);
  113. /**
  114. * Cleanup model context at the end.
  115. *
  116. * @param ctx model context
  117. */
  118. av_cold void ff_psy_end(FFPsyContext *ctx);
  119. /**************************************************************************
  120. * Audio preprocessing stuff. *
  121. * This should be moved into some audio filter eventually. *
  122. **************************************************************************/
  123. struct FFPsyPreprocessContext;
  124. /**
  125. * psychoacoustic model audio preprocessing initialization
  126. */
  127. av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *avctx);
  128. /**
  129. * Preprocess several channel in audio frame in order to compress it better.
  130. *
  131. * @param ctx preprocessing context
  132. * @param audio samples to preprocess
  133. * @param dest place to put filtered samples
  134. * @param tag channel number
  135. * @param channels number of channel to preprocess (some additional work may be done on stereo pair)
  136. */
  137. void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx,
  138. const int16_t *audio, int16_t *dest,
  139. int tag, int channels);
  140. /**
  141. * Cleanup audio preprocessing module.
  142. */
  143. av_cold void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx);
  144. #endif /* AVCODEC_PSYMODEL_H */