OSDN Git Service

lavc: remove the FF_API_AUDIO_OLD cruft.
[coroid/libav_saccubus.git] / 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  * windowing related information
45  */
46 typedef struct FFPsyWindowInfo {
47     int window_type[3];               ///< window type (short/long/transitional, etc.) - current, previous and next
48     int window_shape;                 ///< window shape (sine/KBD/whatever)
49     int num_windows;                  ///< number of windows in a frame
50     int grouping[8];                  ///< window grouping (for e.g. AAC)
51     int *window_sizes;                ///< sequence of window sizes inside one frame (for eg. WMA)
52 } FFPsyWindowInfo;
53
54 /**
55  * context used by psychoacoustic model
56  */
57 typedef struct FFPsyContext {
58     AVCodecContext *avctx;            ///< encoder context
59     const struct FFPsyModel *model;   ///< encoder-specific model functions
60
61     FFPsyBand *psy_bands;             ///< frame bands information
62
63     uint8_t **bands;                  ///< scalefactor band sizes for possible frame sizes
64     int     *num_bands;               ///< number of scalefactor bands for possible frame sizes
65     int num_lens;                     ///< number of scalefactor band sets
66
67     float pe[PSY_MAX_CHANS];          ///< total PE for each channel in the frame
68
69     struct {
70         int size;                     ///< size of the bitresevoir in bits
71         int bits;                     ///< number of bits used in the bitresevoir
72     } bitres;
73
74     void* model_priv_data;            ///< psychoacoustic model implementation private data
75 } FFPsyContext;
76
77 /**
78  * codec-specific psychoacoustic model implementation
79  */
80 typedef struct FFPsyModel {
81     const char *name;
82     int  (*init)   (FFPsyContext *apc);
83     FFPsyWindowInfo (*window)(FFPsyContext *ctx, const int16_t *audio, const int16_t *la, int channel, int prev_type);
84     void (*analyze)(FFPsyContext *ctx, int channel, const float *coeffs, const FFPsyWindowInfo *wi);
85     void (*end)    (FFPsyContext *apc);
86 } FFPsyModel;
87
88 /**
89  * Initialize psychoacoustic model.
90  *
91  * @param ctx        model context
92  * @param avctx      codec context
93  * @param num_lens   number of possible frame lengths
94  * @param bands      scalefactor band lengths for all frame lengths
95  * @param num_bands  number of scalefactor bands for all frame lengths
96  *
97  * @return zero if successful, a negative value if not
98  */
99 av_cold int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx,
100                         int num_lens,
101                         const uint8_t **bands, const int* num_bands);
102
103 /**
104  * Suggest window sequence for channel.
105  *
106  * @param ctx       model context
107  * @param audio     samples for the current frame
108  * @param la        lookahead samples (NULL when unavailable)
109  * @param channel   number of channel element to analyze
110  * @param prev_type previous window type
111  *
112  * @return suggested window information in a structure
113  */
114 FFPsyWindowInfo ff_psy_suggest_window(FFPsyContext *ctx,
115                                       const int16_t *audio, const int16_t *la,
116                                       int channel, int prev_type);
117
118
119 /**
120  * Perform psychoacoustic analysis and set band info (threshold, energy).
121  *
122  * @param ctx     model context
123  * @param channel audio channel number
124  * @param coeffs  pointer to the transformed coefficients
125  * @param wi      window information
126  */
127 void ff_psy_set_band_info(FFPsyContext *ctx, int channel, const float *coeffs,
128                           const FFPsyWindowInfo *wi);
129
130 /**
131  * Cleanup model context at the end.
132  *
133  * @param ctx model context
134  */
135 av_cold void ff_psy_end(FFPsyContext *ctx);
136
137
138 /**************************************************************************
139  *                       Audio preprocessing stuff.                       *
140  *       This should be moved into some audio filter eventually.          *
141  **************************************************************************/
142 struct FFPsyPreprocessContext;
143
144 /**
145  * psychoacoustic model audio preprocessing initialization
146  */
147 av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *avctx);
148
149 /**
150  * Preprocess several channel in audio frame in order to compress it better.
151  *
152  * @param ctx      preprocessing context
153  * @param audio    samples to preprocess
154  * @param dest     place to put filtered samples
155  * @param tag      channel number
156  * @param channels number of channel to preprocess (some additional work may be done on stereo pair)
157  */
158 void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx,
159                        const int16_t *audio, int16_t *dest,
160                        int tag, int channels);
161
162 /**
163  * Cleanup audio preprocessing module.
164  */
165 av_cold void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx);
166
167 #endif /* AVCODEC_PSYMODEL_H */