FFmpeg
psymodel.h
Go to the documentation of this file.
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 
22 #ifndef AVCODEC_PSYMODEL_H
23 #define AVCODEC_PSYMODEL_H
24 
25 #include "avcodec.h"
26 
27 /** maximum possible number of bands */
28 #define PSY_MAX_BANDS 128
29 /** maximum number of channels */
30 #define PSY_MAX_CHANS 20
31 
32 /* cutoff for VBR is purposely increased, since LP filtering actually
33  * hinders VBR performance rather than the opposite
34  */
35 #define AAC_CUTOFF_FROM_BITRATE(bit_rate,channels,sample_rate) (bit_rate ? FFMIN3(FFMIN3( \
36  FFMAX(bit_rate/channels/5, bit_rate/channels*15/32 - 5500), \
37  3000 + bit_rate/channels/4, \
38  12000 + bit_rate/channels/16), \
39  22000, \
40  sample_rate / 2): (sample_rate / 2))
41 #define AAC_CUTOFF(s) ( \
42  (s->flags & AV_CODEC_FLAG_QSCALE) \
43  ? s->sample_rate / 2 \
44  : AAC_CUTOFF_FROM_BITRATE(s->bit_rate, s->ch_layout.nb_channels, s->sample_rate) \
45 )
46 
47 /**
48  * single band psychoacoustic information
49  */
50 typedef struct FFPsyBand {
51  int bits;
52  float energy;
53  float threshold;
54  float spread; /* Energy spread over the band */
55 } FFPsyBand;
56 
57 /**
58  * single channel psychoacoustic information
59  */
60 typedef struct FFPsyChannel {
61  FFPsyBand psy_bands[PSY_MAX_BANDS]; ///< channel bands information
62  float entropy; ///< total PE for this channel
63 } FFPsyChannel;
64 
65 /**
66  * psychoacoustic information for an arbitrary group of channels
67  */
68 typedef struct FFPsyChannelGroup {
69  FFPsyChannel *ch[PSY_MAX_CHANS]; ///< pointers to the individual channels in the group
70  uint8_t num_ch; ///< number of channels in this group
71  uint8_t coupling[PSY_MAX_BANDS]; ///< allow coupling for this band in the group
73 
74 /**
75  * windowing related information
76  */
77 typedef struct FFPsyWindowInfo {
78  int window_type[3]; ///< window type (short/long/transitional, etc.) - current, previous and next
79  int window_shape; ///< window shape (sine/KBD/whatever)
80  int num_windows; ///< number of windows in a frame
81  int grouping[8]; ///< window grouping (for e.g. AAC)
82  float clipping[8]; ///< maximum absolute normalized intensity in the given window for clip avoidance
83  int *window_sizes; ///< sequence of window sizes inside one frame (for eg. WMA)
85 
86 /**
87  * context used by psychoacoustic model
88  */
89 typedef struct FFPsyContext {
90  AVCodecContext *avctx; ///< encoder context
91  const struct FFPsyModel *model; ///< encoder-specific model functions
92 
93  FFPsyChannel *ch; ///< single channel information
94  FFPsyChannelGroup *group; ///< channel group information
95  int num_groups; ///< number of channel groups
96  int cutoff; ///< lowpass frequency cutoff for analysis
97 
98  uint8_t **bands; ///< scalefactor band sizes for possible frame sizes
99  int *num_bands; ///< number of scalefactor bands for possible frame sizes
100  int num_lens; ///< number of scalefactor band sets
101 
102  struct {
103  int size; ///< size of the bitresevoir in bits
104  int bits; ///< number of bits used in the bitresevoir
105  int alloc; ///< number of bits allocated by the psy, or -1 if no allocation was done
106  } bitres;
107 
108  void* model_priv_data; ///< psychoacoustic model implementation private data
109  float pair_joint[16]; ///< encoder-fed per-CPE joint-tool candidacy fraction EMA (bands M/S would adopt or I/S renders; ~0 = joint tools dead); 0 = unseeded
110  uint8_t pair_decoupled[16]; ///< Schmitt state over pair_joint: 1 = joint tools dead on this pair, joint windowing/M-S coupling suspended
111 } FFPsyContext;
112 
113 /**
114  * codec-specific psychoacoustic model implementation
115  */
116 typedef struct FFPsyModel {
117  const char *name;
118  int (*init) (FFPsyContext *apc);
119 
120  /**
121  * Suggest window sequence for channel.
122  *
123  * @param ctx model context
124  * @param audio samples for the current frame
125  * @param la lookahead samples (NULL when unavailable)
126  * @param channel number of channel element to analyze
127  * @param prev_type previous window type
128  *
129  * @return suggested window information in a structure
130  */
131  FFPsyWindowInfo (*window)(FFPsyContext *ctx, const float *audio, const float *la, int channel, int prev_type);
132 
133  /**
134  * Suggest window sequences for both channels of a CPE with block switching
135  * synchronized across the pair (either channel's attack switches both), so
136  * common_window and the joint stereo tools stay available. Optional; when
137  * NULL the encoder decides each channel independently via window().
138  */
139  void (*window_pair)(FFPsyContext *ctx, const float *audio0, const float *la0,
140  const float *audio1, const float *la1,
141  int channel0, int channel1,
142  int prev_type0, int prev_type1, FFPsyWindowInfo wi[2]);
143 
144  /**
145  * Perform psychoacoustic analysis and set band info (threshold, energy) for a group of channels.
146  *
147  * @param ctx model context
148  * @param channel channel number of the first channel in the group to perform analysis on
149  * @param coeffs array of pointers to the transformed coefficients
150  * @param wi window information for the channels in the group
151  */
152  void (*analyze)(FFPsyContext *ctx, int channel, const float **coeffs, const FFPsyWindowInfo *wi);
153 
154  void (*end) (FFPsyContext *apc);
155 } FFPsyModel;
156 
157 /**
158  * Initialize psychoacoustic model.
159  *
160  * @param ctx model context
161  * @param avctx codec context
162  * @param num_lens number of possible frame lengths
163  * @param bands scalefactor band lengths for all frame lengths
164  * @param num_bands number of scalefactor bands for all frame lengths
165  * @param num_groups number of channel groups
166  * @param group_map array with # of channels in group - 1, for each group
167  * @param cutoff analysis bandwidth in Hz, 0 to derive it from avctx
168  *
169  * @return zero if successful, a negative value if not
170  */
171 int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx, int num_lens,
172  const uint8_t **bands, const int *num_bands,
173  int num_groups, const uint8_t *group_map, int cutoff);
174 
175 /**
176  * Determine what group a channel belongs to.
177  *
178  * @param ctx psymodel context
179  * @param channel channel to locate the group for
180  *
181  * @return pointer to the FFPsyChannelGroup this channel belongs to
182  */
184 
185 /**
186  * Cleanup model context at the end.
187  *
188  * @param ctx model context
189  */
191 
192 
193 /**************************************************************************
194  * Audio preprocessing stuff. *
195  * This should be moved into some audio filter eventually. *
196  **************************************************************************/
197 struct FFPsyPreprocessContext;
198 
199 /**
200  * psychoacoustic model audio preprocessing initialization
201  */
202 struct FFPsyPreprocessContext *ff_psy_preprocess_init(AVCodecContext *avctx);
203 
204 /**
205  * Preprocess several channel in audio frame in order to compress it better.
206  *
207  * @param ctx preprocessing context
208  * @param audio samples to be filtered (in place)
209  * @param channels number of channel to preprocess
210  */
211 void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx, float **audio, int channels);
212 
213 /**
214  * Cleanup audio preprocessing module.
215  */
216 void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx);
217 
218 #endif /* AVCODEC_PSYMODEL_H */
ff_psy_preprocess_end
void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx)
Cleanup audio preprocessing module.
FFPsyContext::group
FFPsyChannelGroup * group
channel group information
Definition: psymodel.h:94
FFPsyChannel
single channel psychoacoustic information
Definition: psymodel.h:60
FFPsyModel::name
const char * name
Definition: psymodel.h:117
FFPsyContext::bits
int bits
number of bits used in the bitresevoir
Definition: psymodel.h:104
FFPsyContext::size
int size
size of the bitresevoir in bits
Definition: psymodel.h:103
FFPsyContext::num_groups
int num_groups
number of channel groups
Definition: psymodel.h:95
ff_psy_end
void ff_psy_end(FFPsyContext *ctx)
Cleanup model context at the end.
Definition: psymodel.c:77
FFPsyContext::bitres
struct FFPsyContext::@252 bitres
FFPsyWindowInfo::window_shape
int window_shape
window shape (sine/KBD/whatever)
Definition: psymodel.h:79
FFPsyModel::window_pair
void(* window_pair)(FFPsyContext *ctx, const float *audio0, const float *la0, const float *audio1, const float *la1, int channel0, int channel1, int prev_type0, int prev_type1, FFPsyWindowInfo wi[2])
Suggest window sequences for both channels of a CPE with block switching synchronized across the pair...
Definition: psymodel.h:139
ff_psy_preprocess
void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx, float **audio, int channels)
Preprocess several channel in audio frame in order to compress it better.
FFPsyChannelGroup::ch
FFPsyChannel * ch[PSY_MAX_CHANS]
pointers to the individual channels in the group
Definition: psymodel.h:69
ff_psy_preprocess_init
struct FFPsyPreprocessContext * ff_psy_preprocess_init(AVCodecContext *avctx)
psychoacoustic model audio preprocessing initialization
FFPsyModel::init
int(* init)(FFPsyContext *apc)
Definition: psymodel.h:118
FFPsyContext::num_bands
int * num_bands
number of scalefactor bands for possible frame sizes
Definition: psymodel.h:99
FFPsyWindowInfo
windowing related information
Definition: psymodel.h:77
FFPsyChannelGroup::num_ch
uint8_t num_ch
number of channels in this group
Definition: psymodel.h:70
FFPsyContext::pair_joint
float pair_joint[16]
encoder-fed per-CPE joint-tool candidacy fraction EMA (bands M/S would adopt or I/S renders; ~0 = joi...
Definition: psymodel.h:109
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
channels
channels
Definition: aptx.h:31
bands
static const float bands[]
Definition: af_superequalizer.c:56
FFPsyWindowInfo::window_type
int window_type[3]
window type (short/long/transitional, etc.) - current, previous and next
Definition: psymodel.h:78
FFPsyContext::num_lens
int num_lens
number of scalefactor band sets
Definition: psymodel.h:100
FFPsyBand::bits
int bits
Definition: psymodel.h:51
FFPsyBand
single band psychoacoustic information
Definition: psymodel.h:50
FFPsyContext::bands
uint8_t ** bands
scalefactor band sizes for possible frame sizes
Definition: psymodel.h:98
FFPsyWindowInfo::grouping
int grouping[8]
window grouping (for e.g. AAC)
Definition: psymodel.h:81
PSY_MAX_BANDS
#define PSY_MAX_BANDS
maximum possible number of bands
Definition: psymodel.h:28
FFPsyWindowInfo::clipping
float clipping[8]
maximum absolute normalized intensity in the given window for clip avoidance
Definition: psymodel.h:82
FFPsyChannelGroup::coupling
uint8_t coupling[PSY_MAX_BANDS]
allow coupling for this band in the group
Definition: psymodel.h:71
FFPsyChannel::psy_bands
FFPsyBand psy_bands[PSY_MAX_BANDS]
channel bands information
Definition: psymodel.h:61
FFPsyModel::window
FFPsyWindowInfo(* window)(FFPsyContext *ctx, const float *audio, const float *la, int channel, int prev_type)
Suggest window sequence for channel.
Definition: psymodel.h:131
ff_psy_find_group
FFPsyChannelGroup * ff_psy_find_group(FFPsyContext *ctx, int channel)
Determine what group a channel belongs to.
Definition: psymodel.c:67
ff_psy_init
int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx, int num_lens, const uint8_t **bands, const int *num_bands, int num_groups, const uint8_t *group_map, int cutoff)
Initialize psychoacoustic model.
Definition: psymodel.c:28
FFPsyBand::threshold
float threshold
Definition: psymodel.h:53
FFPsyContext::ch
FFPsyChannel * ch
single channel information
Definition: psymodel.h:93
PSY_MAX_CHANS
#define PSY_MAX_CHANS
maximum number of channels
Definition: psymodel.h:30
FFPsyWindowInfo::window_sizes
int * window_sizes
sequence of window sizes inside one frame (for eg. WMA)
Definition: psymodel.h:83
FFPsyContext::alloc
int alloc
number of bits allocated by the psy, or -1 if no allocation was done
Definition: psymodel.h:105
avcodec.h
FFPsyModel::end
void(* end)(FFPsyContext *apc)
Definition: psymodel.h:154
FFPsyChannelGroup
psychoacoustic information for an arbitrary group of channels
Definition: psymodel.h:68
FFPsyModel::analyze
void(* analyze)(FFPsyContext *ctx, int channel, const float **coeffs, const FFPsyWindowInfo *wi)
Perform psychoacoustic analysis and set band info (threshold, energy) for a group of channels.
Definition: psymodel.h:152
FFPsyContext::model_priv_data
void * model_priv_data
psychoacoustic model implementation private data
Definition: psymodel.h:108
FFPsyContext::pair_decoupled
uint8_t pair_decoupled[16]
Schmitt state over pair_joint: 1 = joint tools dead on this pair, joint windowing/M-S coupling suspen...
Definition: psymodel.h:110
FFPsyBand::energy
float energy
Definition: psymodel.h:52
AVCodecContext
main external API structure.
Definition: avcodec.h:443
FFPsyContext::avctx
AVCodecContext * avctx
encoder context
Definition: psymodel.h:90
FFPsyModel
codec-specific psychoacoustic model implementation
Definition: psymodel.h:116
FFPsyContext::cutoff
int cutoff
lowpass frequency cutoff for analysis
Definition: psymodel.h:96
FFPsyContext::model
const struct FFPsyModel * model
encoder-specific model functions
Definition: psymodel.h:91
FFPsyChannel::entropy
float entropy
total PE for this channel
Definition: psymodel.h:62
FFPsyBand::spread
float spread
Definition: psymodel.h:54
FFPsyContext
context used by psychoacoustic model
Definition: psymodel.h:89
channel
channel
Definition: ebur128.h:39
FFPsyWindowInfo::num_windows
int num_windows
number of windows in a frame
Definition: psymodel.h:80