aacenc.h 5.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * AAC encoder
 * Copyright (C) 2008 Konstantin Shishkov
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef AVCODEC_AACENC_H
#define AVCODEC_AACENC_H

25
#include "libavutil/float_dsp.h"
26
#include "libavutil/lfg.h"
27 28 29 30
#include "avcodec.h"
#include "put_bits.h"

#include "aac.h"
31
#include "audio_frame_queue.h"
32 33
#include "psymodel.h"

34 35
#include "lpc.h"

36
typedef enum AACCoder {
37
    AAC_CODER_ANMR = 0,
38 39
    AAC_CODER_TWOLOOP,
    AAC_CODER_FAST,
40

41 42
    AAC_CODER_NB,
}AACCoder;
43

44
typedef struct AACEncOptions {
45
    int coder;
46
    int pns;
47
    int tns;
48
    int ltp;
49
    int pred;
50
    int mid_side;
51
    int intensity_stereo;
52 53
} AACEncOptions;

54 55
struct AACEncContext;

56
typedef struct AACCoefficientsEncoder {
57 58 59 60
    void (*search_for_quantizers)(AVCodecContext *avctx, struct AACEncContext *s,
                                  SingleChannelElement *sce, const float lambda);
    void (*encode_window_bands_info)(struct AACEncContext *s, SingleChannelElement *sce,
                                     int win, int group_len, const float lambda);
61
    void (*quantize_and_encode_band)(struct AACEncContext *s, PutBitContext *pb, const float *in, float *out, int size,
62
                                     int scale_idx, int cb, const float lambda, int rtz);
63
    void (*encode_tns_info)(struct AACEncContext *s, SingleChannelElement *sce);
64
    void (*encode_ltp_info)(struct AACEncContext *s, SingleChannelElement *sce, int common_window);
65
    void (*encode_main_pred)(struct AACEncContext *s, SingleChannelElement *sce);
66
    void (*adjust_common_pred)(struct AACEncContext *s, ChannelElement *cpe);
67
    void (*adjust_common_ltp)(struct AACEncContext *s, ChannelElement *cpe);
68
    void (*apply_main_pred)(struct AACEncContext *s, SingleChannelElement *sce);
69
    void (*apply_tns_filt)(struct AACEncContext *s, SingleChannelElement *sce);
70 71
    void (*update_ltp)(struct AACEncContext *s, SingleChannelElement *sce);
    void (*ltp_insert_new_frame)(struct AACEncContext *s);
72
    void (*set_special_band_scalefactors)(struct AACEncContext *s, SingleChannelElement *sce);
73
    void (*search_for_pns)(struct AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce);
74
    void (*mark_pns)(struct AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce);
75
    void (*search_for_tns)(struct AACEncContext *s, SingleChannelElement *sce);
76
    void (*search_for_ltp)(struct AACEncContext *s, SingleChannelElement *sce, int common_window);
77 78
    void (*search_for_ms)(struct AACEncContext *s, ChannelElement *cpe);
    void (*search_for_is)(struct AACEncContext *s, AVCodecContext *avctx, ChannelElement *cpe);
79
    void (*search_for_pred)(struct AACEncContext *s, SingleChannelElement *sce);
80
} AACCoefficientsEncoder;
81 82 83

extern AACCoefficientsEncoder ff_aac_coders[];

84 85 86
typedef struct AACQuantizeBandCostCacheEntry {
    float rd;
    float energy;
87
    int bits;
88 89
    char cb;
    char rtz;
90
    uint16_t generation;
91 92
} AACQuantizeBandCostCacheEntry;

93 94 95 96
/**
 * AAC encoder context
 */
typedef struct AACEncContext {
97 98
    AVClass *av_class;
    AACEncOptions options;                       ///< encoding options
99
    PutBitContext pb;
100 101
    FFTContext mdct1024;                         ///< long (1024 samples) frame transform context
    FFTContext mdct128;                          ///< short (128 samples) frame transform context
102
    AVFloatDSPContext *fdsp;
103
    AVLFG lfg;                                   ///< PRNG needed for PNS
104
    float *planar_samples[8];                    ///< saved preprocessed input
105

106
    int profile;                                 ///< copied from avctx
107
    LPCContext lpc;                              ///< used by TNS
108
    int samplerate_index;                        ///< MPEG-4 samplerate index
109
    int channels;                                ///< channel count
110
    const uint8_t *chan_map;                     ///< channel configuration map
111 112 113 114 115

    ChannelElement *cpe;                         ///< channel elements
    FFPsyContext psy;
    struct FFPsyPreprocessContext* psypp;
    AACCoefficientsEncoder *coder;
116
    int cur_channel;                             ///< current channel for coder context
117
    int last_frame;
118
    int random_state;
119
    float lambda;
120
    int last_frame_pb_count;                     ///< number of bits for the previous frame
121 122
    float lambda_sum;                            ///< sum(lambda), for Qvg reporting
    int lambda_count;                            ///< count(lambda), for Qvg reporting
123 124
    enum RawDataBlockType cur_type;              ///< channel group type cur_channel belongs to

125
    AudioFrameQueue afq;
126
    DECLARE_ALIGNED(16, int,   qcoefs)[96];      ///< quantized coefficients
127
    DECLARE_ALIGNED(32, float, scoefs)[1024];    ///< scaled coefficients
128

129
    uint16_t quantize_band_cost_cache_generation;
130 131
    AACQuantizeBandCostCacheEntry quantize_band_cost_cache[256][128]; ///< memoization area for quantize_band_cost

132 133 134
    struct {
        float *samples;
    } buffer;
135 136
} AACEncContext;

137
void ff_aac_coder_init_mips(AACEncContext *c);
138 139
void ff_quantize_band_cost_cache_init(struct AACEncContext *s);

140

141
#endif /* AVCODEC_AACENC_H */