]> git.sesse.net Git - ffmpeg/blob - libavcodec/psymodel.h
avcodec/ass: fix doxygen typo
[ffmpeg] / libavcodec / 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
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 #define AAC_CUTOFF(s) ((s)->bit_rate ? FFMIN3(4000 + (s)->bit_rate/8, 12000 + (s)->bit_rate/32, (s)->sample_rate / 2) : ((s)->sample_rate / 2))
33
34 /**
35  * single band psychoacoustic information
36  */
37 typedef struct FFPsyBand {
38     int   bits;
39     float energy;
40     float threshold;
41     float spread;    /* Energy spread over the band */
42 } FFPsyBand;
43
44 /**
45  * single channel psychoacoustic information
46  */
47 typedef struct FFPsyChannel {
48     FFPsyBand psy_bands[PSY_MAX_BANDS]; ///< channel bands information
49     float     entropy;                  ///< total PE for this channel
50 } FFPsyChannel;
51
52 /**
53  * psychoacoustic information for an arbitrary group of channels
54  */
55 typedef struct FFPsyChannelGroup {
56     FFPsyChannel *ch[PSY_MAX_CHANS];  ///< pointers to the individual channels in the group
57     uint8_t num_ch;                   ///< number of channels in this group
58     uint8_t coupling[PSY_MAX_BANDS];  ///< allow coupling for this band in the group
59 } FFPsyChannelGroup;
60
61 /**
62  * windowing related information
63  */
64 typedef struct FFPsyWindowInfo {
65     int window_type[3];               ///< window type (short/long/transitional, etc.) - current, previous and next
66     int window_shape;                 ///< window shape (sine/KBD/whatever)
67     int num_windows;                  ///< number of windows in a frame
68     int grouping[8];                  ///< window grouping (for e.g. AAC)
69     float clipping[8];                ///< maximum absolute normalized intensity in the given window for clip avoidance
70     int *window_sizes;                ///< sequence of window sizes inside one frame (for eg. WMA)
71 } FFPsyWindowInfo;
72
73 /**
74  * context used by psychoacoustic model
75  */
76 typedef struct FFPsyContext {
77     AVCodecContext *avctx;            ///< encoder context
78     const struct FFPsyModel *model;   ///< encoder-specific model functions
79
80     FFPsyChannel      *ch;            ///< single channel information
81     FFPsyChannelGroup *group;         ///< channel group information
82     int num_groups;                   ///< number of channel groups
83
84     uint8_t **bands;                  ///< scalefactor band sizes for possible frame sizes
85     int     *num_bands;               ///< number of scalefactor bands for possible frame sizes
86     int num_lens;                     ///< number of scalefactor band sets
87
88     struct {
89         int size;                     ///< size of the bitresevoir in bits
90         int bits;                     ///< number of bits used in the bitresevoir
91         int alloc;                    ///< number of bits allocated by the psy, or -1 if no allocation was done
92     } bitres;
93
94     void* model_priv_data;            ///< psychoacoustic model implementation private data
95 } FFPsyContext;
96
97 /**
98  * codec-specific psychoacoustic model implementation
99  */
100 typedef struct FFPsyModel {
101     const char *name;
102     int  (*init)   (FFPsyContext *apc);
103
104     /**
105      * Suggest window sequence for channel.
106      *
107      * @param ctx       model context
108      * @param audio     samples for the current frame
109      * @param la        lookahead samples (NULL when unavailable)
110      * @param channel   number of channel element to analyze
111      * @param prev_type previous window type
112      *
113      * @return suggested window information in a structure
114      */
115     FFPsyWindowInfo (*window)(FFPsyContext *ctx, const float *audio, const float *la, int channel, int prev_type);
116
117     /**
118      * Perform psychoacoustic analysis and set band info (threshold, energy) for a group of channels.
119      *
120      * @param ctx      model context
121      * @param channel  channel number of the first channel in the group to perform analysis on
122      * @param coeffs   array of pointers to the transformed coefficients
123      * @param wi       window information for the channels in the group
124      */
125     void (*analyze)(FFPsyContext *ctx, int channel, const float **coeffs, const FFPsyWindowInfo *wi);
126
127     void (*end)    (FFPsyContext *apc);
128 } FFPsyModel;
129
130 /**
131  * Initialize psychoacoustic model.
132  *
133  * @param ctx        model context
134  * @param avctx      codec context
135  * @param num_lens   number of possible frame lengths
136  * @param bands      scalefactor band lengths for all frame lengths
137  * @param num_bands  number of scalefactor bands for all frame lengths
138  * @param num_groups number of channel groups
139  * @param group_map  array with # of channels in group - 1, for each group
140  *
141  * @return zero if successful, a negative value if not
142  */
143 int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx, int num_lens,
144                 const uint8_t **bands, const int *num_bands,
145                 int num_groups, const uint8_t *group_map);
146
147 /**
148  * Determine what group a channel belongs to.
149  *
150  * @param ctx     psymodel context
151  * @param channel channel to locate the group for
152  *
153  * @return pointer to the FFPsyChannelGroup this channel belongs to
154  */
155 FFPsyChannelGroup *ff_psy_find_group(FFPsyContext *ctx, int channel);
156
157 /**
158  * Cleanup model context at the end.
159  *
160  * @param ctx model context
161  */
162 void ff_psy_end(FFPsyContext *ctx);
163
164
165 /**************************************************************************
166  *                       Audio preprocessing stuff.                       *
167  *       This should be moved into some audio filter eventually.          *
168  **************************************************************************/
169 struct FFPsyPreprocessContext;
170
171 /**
172  * psychoacoustic model audio preprocessing initialization
173  */
174 struct FFPsyPreprocessContext *ff_psy_preprocess_init(AVCodecContext *avctx);
175
176 /**
177  * Preprocess several channel in audio frame in order to compress it better.
178  *
179  * @param ctx      preprocessing context
180  * @param audio    samples to be filtered (in place)
181  * @param channels number of channel to preprocess
182  */
183 void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx, float **audio, int channels);
184
185 /**
186  * Cleanup audio preprocessing module.
187  */
188 void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx);
189
190 #endif /* AVCODEC_PSYMODEL_H */