Statistics
| Branch: | Revision:

ffmpeg / libavcodec / psymodel.h @ b58e2985

History | View | Annotate | Download (5.5 KB)

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

    
84
    /**
85
     * Suggest window sequence for channel.
86
     *
87
     * @param ctx       model context
88
     * @param audio     samples for the current frame
89
     * @param la        lookahead samples (NULL when unavailable)
90
     * @param channel   number of channel element to analyze
91
     * @param prev_type previous window type
92
     *
93
     * @return suggested window information in a structure
94
     */
95
    FFPsyWindowInfo (*window)(FFPsyContext *ctx, const int16_t *audio, const int16_t *la, int channel, int prev_type);
96

    
97
    /**
98
     * Perform psychoacoustic analysis and set band info (threshold, energy).
99
     *
100
     * @param ctx     model context
101
     * @param channel audio channel number
102
     * @param coeffs  pointer to the transformed coefficients
103
     * @param wi      window information
104
     */
105
    void (*analyze)(FFPsyContext *ctx, int channel, const float *coeffs, const FFPsyWindowInfo *wi);
106

    
107
    void (*end)    (FFPsyContext *apc);
108
} FFPsyModel;
109

    
110
/**
111
 * Initialize psychoacoustic model.
112
 *
113
 * @param ctx        model context
114
 * @param avctx      codec context
115
 * @param num_lens   number of possible frame lengths
116
 * @param bands      scalefactor band lengths for all frame lengths
117
 * @param num_bands  number of scalefactor bands for all frame lengths
118
 *
119
 * @return zero if successful, a negative value if not
120
 */
121
av_cold int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx,
122
                        int num_lens,
123
                        const uint8_t **bands, const int* num_bands);
124

    
125
/**
126
 * Cleanup model context at the end.
127
 *
128
 * @param ctx model context
129
 */
130
av_cold void ff_psy_end(FFPsyContext *ctx);
131

    
132

    
133
/**************************************************************************
134
 *                       Audio preprocessing stuff.                       *
135
 *       This should be moved into some audio filter eventually.          *
136
 **************************************************************************/
137
struct FFPsyPreprocessContext;
138

    
139
/**
140
 * psychoacoustic model audio preprocessing initialization
141
 */
142
av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *avctx);
143

    
144
/**
145
 * Preprocess several channel in audio frame in order to compress it better.
146
 *
147
 * @param ctx      preprocessing context
148
 * @param audio    samples to preprocess
149
 * @param dest     place to put filtered samples
150
 * @param tag      channel number
151
 * @param channels number of channel to preprocess (some additional work may be done on stereo pair)
152
 */
153
void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx,
154
                       const int16_t *audio, int16_t *dest,
155
                       int tag, int channels);
156

    
157
/**
158
 * Cleanup audio preprocessing module.
159
 */
160
av_cold void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx);
161

    
162
#endif /* AVCODEC_PSYMODEL_H */