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.

159 lines
5.4KB

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