yading@10
|
1 /*
|
yading@10
|
2 * audio encoder psychoacoustic model
|
yading@10
|
3 * Copyright (C) 2008 Konstantin Shishkov
|
yading@10
|
4 *
|
yading@10
|
5 * This file is part of FFmpeg.
|
yading@10
|
6 *
|
yading@10
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
9 * License as published by the Free Software Foundation; either
|
yading@10
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
11 *
|
yading@10
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
15 * Lesser General Public License for more details.
|
yading@10
|
16 *
|
yading@10
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
20 */
|
yading@10
|
21
|
yading@10
|
22 #ifndef AVCODEC_PSYMODEL_H
|
yading@10
|
23 #define AVCODEC_PSYMODEL_H
|
yading@10
|
24
|
yading@10
|
25 #include "avcodec.h"
|
yading@10
|
26
|
yading@10
|
27 /** maximum possible number of bands */
|
yading@10
|
28 #define PSY_MAX_BANDS 128
|
yading@10
|
29 /** maximum number of channels */
|
yading@10
|
30 #define PSY_MAX_CHANS 20
|
yading@10
|
31
|
yading@10
|
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))
|
yading@10
|
33
|
yading@10
|
34 /**
|
yading@10
|
35 * single band psychoacoustic information
|
yading@10
|
36 */
|
yading@10
|
37 typedef struct FFPsyBand {
|
yading@10
|
38 int bits;
|
yading@10
|
39 float energy;
|
yading@10
|
40 float threshold;
|
yading@10
|
41 float distortion;
|
yading@10
|
42 float perceptual_weight;
|
yading@10
|
43 } FFPsyBand;
|
yading@10
|
44
|
yading@10
|
45 /**
|
yading@10
|
46 * single channel psychoacoustic information
|
yading@10
|
47 */
|
yading@10
|
48 typedef struct FFPsyChannel {
|
yading@10
|
49 FFPsyBand psy_bands[PSY_MAX_BANDS]; ///< channel bands information
|
yading@10
|
50 float entropy; ///< total PE for this channel
|
yading@10
|
51 } FFPsyChannel;
|
yading@10
|
52
|
yading@10
|
53 /**
|
yading@10
|
54 * psychoacoustic information for an arbitrary group of channels
|
yading@10
|
55 */
|
yading@10
|
56 typedef struct FFPsyChannelGroup {
|
yading@10
|
57 FFPsyChannel *ch[PSY_MAX_CHANS]; ///< pointers to the individual channels in the group
|
yading@10
|
58 uint8_t num_ch; ///< number of channels in this group
|
yading@10
|
59 uint8_t coupling[PSY_MAX_BANDS]; ///< allow coupling for this band in the group
|
yading@10
|
60 } FFPsyChannelGroup;
|
yading@10
|
61
|
yading@10
|
62 /**
|
yading@10
|
63 * windowing related information
|
yading@10
|
64 */
|
yading@10
|
65 typedef struct FFPsyWindowInfo {
|
yading@10
|
66 int window_type[3]; ///< window type (short/long/transitional, etc.) - current, previous and next
|
yading@10
|
67 int window_shape; ///< window shape (sine/KBD/whatever)
|
yading@10
|
68 int num_windows; ///< number of windows in a frame
|
yading@10
|
69 int grouping[8]; ///< window grouping (for e.g. AAC)
|
yading@10
|
70 int *window_sizes; ///< sequence of window sizes inside one frame (for eg. WMA)
|
yading@10
|
71 } FFPsyWindowInfo;
|
yading@10
|
72
|
yading@10
|
73 /**
|
yading@10
|
74 * context used by psychoacoustic model
|
yading@10
|
75 */
|
yading@10
|
76 typedef struct FFPsyContext {
|
yading@10
|
77 AVCodecContext *avctx; ///< encoder context
|
yading@10
|
78 const struct FFPsyModel *model; ///< encoder-specific model functions
|
yading@10
|
79
|
yading@10
|
80 FFPsyChannel *ch; ///< single channel information
|
yading@10
|
81 FFPsyChannelGroup *group; ///< channel group information
|
yading@10
|
82 int num_groups; ///< number of channel groups
|
yading@10
|
83
|
yading@10
|
84 uint8_t **bands; ///< scalefactor band sizes for possible frame sizes
|
yading@10
|
85 int *num_bands; ///< number of scalefactor bands for possible frame sizes
|
yading@10
|
86 int num_lens; ///< number of scalefactor band sets
|
yading@10
|
87
|
yading@10
|
88 struct {
|
yading@10
|
89 int size; ///< size of the bitresevoir in bits
|
yading@10
|
90 int bits; ///< number of bits used in the bitresevoir
|
yading@10
|
91 } bitres;
|
yading@10
|
92
|
yading@10
|
93 void* model_priv_data; ///< psychoacoustic model implementation private data
|
yading@10
|
94 } FFPsyContext;
|
yading@10
|
95
|
yading@10
|
96 /**
|
yading@10
|
97 * codec-specific psychoacoustic model implementation
|
yading@10
|
98 */
|
yading@10
|
99 typedef struct FFPsyModel {
|
yading@10
|
100 const char *name;
|
yading@10
|
101 int (*init) (FFPsyContext *apc);
|
yading@10
|
102
|
yading@10
|
103 /**
|
yading@10
|
104 * Suggest window sequence for channel.
|
yading@10
|
105 *
|
yading@10
|
106 * @param ctx model context
|
yading@10
|
107 * @param audio samples for the current frame
|
yading@10
|
108 * @param la lookahead samples (NULL when unavailable)
|
yading@10
|
109 * @param channel number of channel element to analyze
|
yading@10
|
110 * @param prev_type previous window type
|
yading@10
|
111 *
|
yading@10
|
112 * @return suggested window information in a structure
|
yading@10
|
113 */
|
yading@10
|
114 FFPsyWindowInfo (*window)(FFPsyContext *ctx, const float *audio, const float *la, int channel, int prev_type);
|
yading@10
|
115
|
yading@10
|
116 /**
|
yading@10
|
117 * Perform psychoacoustic analysis and set band info (threshold, energy) for a group of channels.
|
yading@10
|
118 *
|
yading@10
|
119 * @param ctx model context
|
yading@10
|
120 * @param channel channel number of the first channel in the group to perform analysis on
|
yading@10
|
121 * @param coeffs array of pointers to the transformed coefficients
|
yading@10
|
122 * @param wi window information for the channels in the group
|
yading@10
|
123 */
|
yading@10
|
124 void (*analyze)(FFPsyContext *ctx, int channel, const float **coeffs, const FFPsyWindowInfo *wi);
|
yading@10
|
125
|
yading@10
|
126 void (*end) (FFPsyContext *apc);
|
yading@10
|
127 } FFPsyModel;
|
yading@10
|
128
|
yading@10
|
129 /**
|
yading@10
|
130 * Initialize psychoacoustic model.
|
yading@10
|
131 *
|
yading@10
|
132 * @param ctx model context
|
yading@10
|
133 * @param avctx codec context
|
yading@10
|
134 * @param num_lens number of possible frame lengths
|
yading@10
|
135 * @param bands scalefactor band lengths for all frame lengths
|
yading@10
|
136 * @param num_bands number of scalefactor bands for all frame lengths
|
yading@10
|
137 * @param num_groups number of channel groups
|
yading@10
|
138 * @param group_map array with # of channels in group - 1, for each group
|
yading@10
|
139 *
|
yading@10
|
140 * @return zero if successful, a negative value if not
|
yading@10
|
141 */
|
yading@10
|
142 int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx, int num_lens,
|
yading@10
|
143 const uint8_t **bands, const int *num_bands,
|
yading@10
|
144 int num_groups, const uint8_t *group_map);
|
yading@10
|
145
|
yading@10
|
146 /**
|
yading@10
|
147 * Determine what group a channel belongs to.
|
yading@10
|
148 *
|
yading@10
|
149 * @param ctx psymodel context
|
yading@10
|
150 * @param channel channel to locate the group for
|
yading@10
|
151 *
|
yading@10
|
152 * @return pointer to the FFPsyChannelGroup this channel belongs to
|
yading@10
|
153 */
|
yading@10
|
154 FFPsyChannelGroup *ff_psy_find_group(FFPsyContext *ctx, int channel);
|
yading@10
|
155
|
yading@10
|
156 /**
|
yading@10
|
157 * Cleanup model context at the end.
|
yading@10
|
158 *
|
yading@10
|
159 * @param ctx model context
|
yading@10
|
160 */
|
yading@10
|
161 void ff_psy_end(FFPsyContext *ctx);
|
yading@10
|
162
|
yading@10
|
163
|
yading@10
|
164 /**************************************************************************
|
yading@10
|
165 * Audio preprocessing stuff. *
|
yading@10
|
166 * This should be moved into some audio filter eventually. *
|
yading@10
|
167 **************************************************************************/
|
yading@10
|
168 struct FFPsyPreprocessContext;
|
yading@10
|
169
|
yading@10
|
170 /**
|
yading@10
|
171 * psychoacoustic model audio preprocessing initialization
|
yading@10
|
172 */
|
yading@10
|
173 struct FFPsyPreprocessContext *ff_psy_preprocess_init(AVCodecContext *avctx);
|
yading@10
|
174
|
yading@10
|
175 /**
|
yading@10
|
176 * Preprocess several channel in audio frame in order to compress it better.
|
yading@10
|
177 *
|
yading@10
|
178 * @param ctx preprocessing context
|
yading@10
|
179 * @param audio samples to be filtered (in place)
|
yading@10
|
180 * @param channels number of channel to preprocess
|
yading@10
|
181 */
|
yading@10
|
182 void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx, float **audio, int channels);
|
yading@10
|
183
|
yading@10
|
184 /**
|
yading@10
|
185 * Cleanup audio preprocessing module.
|
yading@10
|
186 */
|
yading@10
|
187 void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx);
|
yading@10
|
188
|
yading@10
|
189 #endif /* AVCODEC_PSYMODEL_H */
|