FFmpeg
psymodel.h
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 
28 #define PSY_MAX_BANDS 128
29 
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->channels, s->sample_rate) \
45 )
46 
50 typedef struct FFPsyBand {
51  int bits;
52  float energy;
53  float threshold;
54  float spread; /* Energy spread over the band */
55 } FFPsyBand;
56 
60 typedef struct FFPsyChannel {
61  FFPsyBand psy_bands[PSY_MAX_BANDS];
62  float entropy;
63 } FFPsyChannel;
64 
68 typedef struct FFPsyChannelGroup {
69  FFPsyChannel *ch[PSY_MAX_CHANS];
70  uint8_t num_ch;
71  uint8_t coupling[PSY_MAX_BANDS];
73 
77 typedef struct FFPsyWindowInfo {
78  int window_type[3];
81  int grouping[8];
82  float clipping[8];
83  int *window_sizes;
85 
89 typedef struct FFPsyContext {
91  const struct FFPsyModel *model;
92 
95  int num_groups;
96  int cutoff;
97 
98  uint8_t **bands;
99  int *num_bands;
100  int num_lens;
101 
102  struct {
103  int size;
104  int bits;
105  int alloc;
106  } bitres;
107 
109 } FFPsyContext;
110 
114 typedef struct FFPsyModel {
115  const char *name;
116  int (*init) (FFPsyContext *apc);
117 
129  FFPsyWindowInfo (*window)(FFPsyContext *ctx, const float *audio, const float *la, int channel, int prev_type);
130 
139  void (*analyze)(FFPsyContext *ctx, int channel, const float **coeffs, const FFPsyWindowInfo *wi);
140 
141  void (*end) (FFPsyContext *apc);
142 } FFPsyModel;
143 
157 int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx, int num_lens,
158  const uint8_t **bands, const int *num_bands,
159  int num_groups, const uint8_t *group_map);
160 
169 FFPsyChannelGroup *ff_psy_find_group(FFPsyContext *ctx, int channel);
170 
176 void ff_psy_end(FFPsyContext *ctx);
177 
178 
179 /**************************************************************************
180  * Audio preprocessing stuff. *
181  * This should be moved into some audio filter eventually. *
182  **************************************************************************/
184 
188 struct FFPsyPreprocessContext *ff_psy_preprocess_init(AVCodecContext *avctx);
189 
197 void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx, float **audio, int channels);
198 
202 void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx);
203 
204 #endif /* AVCODEC_PSYMODEL_H */
int num_groups
number of channel groups
Definition: psymodel.h:95
uint8_t ** bands
scalefactor band sizes for possible frame sizes
Definition: psymodel.h:98
FFPsyChannelGroup * group
channel group information
Definition: psymodel.h:94
psychoacoustic information for an arbitrary group of channels
Definition: psymodel.h:68
int alloc
number of bits allocated by the psy, or -1 if no allocation was done
Definition: psymodel.h:105
Definition: psymodel.c:93
int * num_bands
number of scalefactor bands for possible frame sizes
Definition: psymodel.h:99
int * window_sizes
sequence of window sizes inside one frame (for eg. WMA)
Definition: psymodel.h:83
int size
size of the bitresevoir in bits
Definition: psymodel.h:103
context used by psychoacoustic model
Definition: psymodel.h:89
single band psychoacoustic information
Definition: psymodel.h:50
single channel psychoacoustic information
Definition: psymodel.h:60
int num_windows
number of windows in a frame
Definition: psymodel.h:80
codec-specific psychoacoustic model implementation
Definition: psymodel.h:114
uint8_t num_ch
number of channels in this group
Definition: psymodel.h:70
float entropy
total PE for this channel
Definition: psymodel.h:62
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1518
void * model_priv_data
psychoacoustic model implementation private data
Definition: psymodel.h:108
int bits
number of bits used in the bitresevoir
Definition: psymodel.h:104
int window_shape
window shape (sine/KBD/whatever)
Definition: psymodel.h:79
int cutoff
lowpass frequency cutoff for analysis
Definition: psymodel.h:96
const struct FFPsyModel * model
encoder-specific model functions
Definition: psymodel.h:91
windowing related information
Definition: psymodel.h:77
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition: ebur128.h:39
int num_lens
number of scalefactor band sets
Definition: psymodel.h:100
FFPsyChannel * ch
single channel information
Definition: psymodel.h:93
AVCodecContext * avctx
encoder context
Definition: psymodel.h:90