2 * audio encoder psychoacoustic model
3 * Copyright (C) 2008 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
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);
85 * Suggest window sequence for channel.
87 * @param ctx model context
88 * @param audio samples for the current frame
89 * @param la lookahead samples (NULL when unavailable)
90 * @param channel number of channel element to analyze
91 * @param prev_type previous window type
93 * @return suggested window information in a structure
95 FFPsyWindowInfo (*window)(FFPsyContext *ctx, const int16_t *audio, const int16_t *la, int channel, int prev_type);
98 * Perform psychoacoustic analysis and set band info (threshold, energy).
100 * @param ctx model context
101 * @param channel audio channel number
102 * @param coeffs pointer to the transformed coefficients
103 * @param wi window information
105 void (*analyze)(FFPsyContext *ctx, int channel, const float *coeffs, const FFPsyWindowInfo *wi);
107 void (*end) (FFPsyContext *apc);
111 * Initialize psychoacoustic model.
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
119 * @return zero if successful, a negative value if not
121 av_cold int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx,
123 const uint8_t **bands, const int* num_bands);
126 * Cleanup model context at the end.
128 * @param ctx model context
130 av_cold void ff_psy_end(FFPsyContext *ctx);
133 /**************************************************************************
134 * Audio preprocessing stuff. *
135 * This should be moved into some audio filter eventually. *
136 **************************************************************************/
137 struct FFPsyPreprocessContext;
140 * psychoacoustic model audio preprocessing initialization
142 av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *avctx);
145 * Preprocess several channel in audio frame in order to compress it better.
147 * @param ctx preprocessing context
148 * @param audio samples to preprocess
149 * @param dest place to put filtered samples
150 * @param tag channel number
151 * @param channels number of channel to preprocess (some additional work may be done on stereo pair)
153 void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx,
154 const int16_t *audio, int16_t *dest,
155 int tag, int channels);
158 * Cleanup audio preprocessing module.
160 av_cold void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx);
162 #endif /* AVCODEC_PSYMODEL_H */