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.

163 lines
5.5KB

  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. /**
  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. /**
  72. * Suggest window sequence for channel.
  73. *
  74. * @param ctx model context
  75. * @param audio samples for the current frame
  76. * @param la lookahead samples (NULL when unavailable)
  77. * @param channel number of channel element to analyze
  78. * @param prev_type previous window type
  79. *
  80. * @return suggested window information in a structure
  81. */
  82. FFPsyWindowInfo (*window)(FFPsyContext *ctx, const int16_t *audio, const int16_t *la, int channel, int prev_type);
  83. /**
  84. * Perform psychoacoustic analysis and set band info (threshold, energy).
  85. *
  86. * @param ctx model context
  87. * @param channel audio channel number
  88. * @param coeffs pointer to the transformed coefficients
  89. * @param wi window information
  90. */
  91. void (*analyze)(FFPsyContext *ctx, int channel, const float *coeffs, const FFPsyWindowInfo *wi);
  92. void (*end) (FFPsyContext *apc);
  93. } FFPsyModel;
  94. /**
  95. * Initialize psychoacoustic model.
  96. *
  97. * @param ctx model context
  98. * @param avctx codec context
  99. * @param num_lens number of possible frame lengths
  100. * @param bands scalefactor band lengths for all frame lengths
  101. * @param num_bands number of scalefactor bands for all frame lengths
  102. *
  103. * @return zero if successful, a negative value if not
  104. */
  105. av_cold int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx,
  106. int num_lens,
  107. const uint8_t **bands, const int* num_bands);
  108. /**
  109. * Cleanup model context at the end.
  110. *
  111. * @param ctx model context
  112. */
  113. av_cold void ff_psy_end(FFPsyContext *ctx);
  114. /**************************************************************************
  115. * Audio preprocessing stuff. *
  116. * This should be moved into some audio filter eventually. *
  117. **************************************************************************/
  118. struct FFPsyPreprocessContext;
  119. /**
  120. * psychoacoustic model audio preprocessing initialization
  121. */
  122. av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *avctx);
  123. /**
  124. * Preprocess several channel in audio frame in order to compress it better.
  125. *
  126. * @param ctx preprocessing context
  127. * @param audio samples to preprocess
  128. * @param dest place to put filtered samples
  129. * @param tag channel number
  130. * @param channels number of channel to preprocess (some additional work may be done on stereo pair)
  131. */
  132. void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx,
  133. const int16_t *audio, int16_t *dest,
  134. int tag, int channels);
  135. /**
  136. * Cleanup audio preprocessing module.
  137. */
  138. av_cold void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx);
  139. #endif /* AVCODEC_PSYMODEL_H */