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