2 * audio encoder psychoacoustic model
3 * Copyright (C) 2008 Konstantin Shishkov
5 * This file is part of Libav.
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.
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.
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
22 #ifndef AVCODEC_PSYMODEL_H
23 #define AVCODEC_PSYMODEL_H
27 /** maximum possible number of bands */
28 #define PSY_MAX_BANDS 128
29 /** maximum number of channels */
30 #define PSY_MAX_CHANS 20
33 * single band psychoacoustic information
35 typedef struct FFPsyBand {
40 float perceptual_weight;
44 * windowing related information
46 typedef struct FFPsyWindowInfo {
47 int window_type[3]; ///< window type (short/long/transitional, etc.) - current, previous and next
48 int window_shape; ///< window shape (sine/KBD/whatever)
49 int num_windows; ///< number of windows in a frame
50 int grouping[8]; ///< window grouping (for e.g. AAC)
51 int *window_sizes; ///< sequence of window sizes inside one frame (for eg. WMA)
55 * context used by psychoacoustic model
57 typedef struct FFPsyContext {
58 AVCodecContext *avctx; ///< encoder context
59 const struct FFPsyModel *model; ///< encoder-specific model functions
61 FFPsyBand *psy_bands; ///< frame bands information
63 uint8_t **bands; ///< scalefactor band sizes for possible frame sizes
64 int *num_bands; ///< number of scalefactor bands for possible frame sizes
65 int num_lens; ///< number of scalefactor band sets
67 float pe[PSY_MAX_CHANS]; ///< total PE for each channel in the frame
70 int size; ///< size of the bitresevoir in bits
71 int bits; ///< number of bits used in the bitresevoir
74 void* model_priv_data; ///< psychoacoustic model implementation private data
78 * codec-specific psychoacoustic model implementation
80 typedef struct FFPsyModel {
82 int (*init) (FFPsyContext *apc);
83 FFPsyWindowInfo (*window)(FFPsyContext *ctx, const int16_t *audio, const int16_t *la, int channel, int prev_type);
84 void (*analyze)(FFPsyContext *ctx, int channel, const float *coeffs, const FFPsyWindowInfo *wi);
85 void (*end) (FFPsyContext *apc);
89 * Initialize psychoacoustic model.
91 * @param ctx model context
92 * @param avctx codec context
93 * @param num_lens number of possible frame lengths
94 * @param bands scalefactor band lengths for all frame lengths
95 * @param num_bands number of scalefactor bands for all frame lengths
97 * @return zero if successful, a negative value if not
99 av_cold int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx,
101 const uint8_t **bands, const int* num_bands);
104 * Suggest window sequence for channel.
106 * @param ctx model context
107 * @param audio samples for the current frame
108 * @param la lookahead samples (NULL when unavailable)
109 * @param channel number of channel element to analyze
110 * @param prev_type previous window type
112 * @return suggested window information in a structure
114 FFPsyWindowInfo ff_psy_suggest_window(FFPsyContext *ctx,
115 const int16_t *audio, const int16_t *la,
116 int channel, int prev_type);
120 * Perform psychoacoustic analysis and set band info (threshold, energy).
122 * @param ctx model context
123 * @param channel audio channel number
124 * @param coeffs pointer to the transformed coefficients
125 * @param wi window information
127 void ff_psy_set_band_info(FFPsyContext *ctx, int channel, const float *coeffs,
128 const FFPsyWindowInfo *wi);
131 * Cleanup model context at the end.
133 * @param ctx model context
135 av_cold void ff_psy_end(FFPsyContext *ctx);
138 /**************************************************************************
139 * Audio preprocessing stuff. *
140 * This should be moved into some audio filter eventually. *
141 **************************************************************************/
142 struct FFPsyPreprocessContext;
145 * psychoacoustic model audio preprocessing initialization
147 av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *avctx);
150 * Preprocess several channel in audio frame in order to compress it better.
152 * @param ctx preprocessing context
153 * @param audio samples to preprocess
154 * @param dest place to put filtered samples
155 * @param tag channel number
156 * @param channels number of channel to preprocess (some additional work may be done on stereo pair)
158 void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx,
159 const int16_t *audio, int16_t *dest,
160 int tag, int channels);
163 * Cleanup audio preprocessing module.
165 av_cold void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx);
167 #endif /* AVCODEC_PSYMODEL_H */