OSDN Git Service

opus_celt: move quantization and band decoding to opus_pvq.c
[android-x86/external-ffmpeg.git] / libavcodec / opus.h
1 /*
2  * Opus decoder/demuxer common functions
3  * Copyright (c) 2012 Andrew D'Addesio
4  * Copyright (c) 2013-2014 Mozilla Corporation
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #ifndef AVCODEC_OPUS_H
24 #define AVCODEC_OPUS_H
25
26 #include <stdint.h>
27
28 #include "libavutil/audio_fifo.h"
29 #include "libavutil/float_dsp.h"
30 #include "libavutil/frame.h"
31
32 #include "libswresample/swresample.h"
33
34 #include "avcodec.h"
35 #include "opus_rc.h"
36
37 #define MAX_FRAME_SIZE               1275
38 #define MAX_FRAMES                   48
39 #define MAX_PACKET_DUR               5760
40
41 #define CELT_SHORT_BLOCKSIZE         120
42 #define CELT_OVERLAP                 CELT_SHORT_BLOCKSIZE
43 #define CELT_MAX_LOG_BLOCKS          3
44 #define CELT_MAX_FRAME_SIZE          (CELT_SHORT_BLOCKSIZE * (1 << CELT_MAX_LOG_BLOCKS))
45 #define CELT_MAX_BANDS               21
46
47 #define SILK_HISTORY                 322
48 #define SILK_MAX_LPC                 16
49
50 #define ROUND_MULL(a,b,s) (((MUL64(a, b) >> ((s) - 1)) + 1) >> 1)
51 #define ROUND_MUL16(a,b)  ((MUL16(a, b) + 16384) >> 15)
52
53 #define OPUS_TS_HEADER     0x7FE0        // 0x3ff (11 bits)
54 #define OPUS_TS_MASK       0xFFE0        // top 11 bits
55
56 static const uint8_t opus_default_extradata[30] = {
57     'O', 'p', 'u', 's', 'H', 'e', 'a', 'd',
58     1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60 };
61
62 enum OpusMode {
63     OPUS_MODE_SILK,
64     OPUS_MODE_HYBRID,
65     OPUS_MODE_CELT
66 };
67
68 enum OpusBandwidth {
69     OPUS_BANDWIDTH_NARROWBAND,
70     OPUS_BANDWIDTH_MEDIUMBAND,
71     OPUS_BANDWIDTH_WIDEBAND,
72     OPUS_BANDWIDTH_SUPERWIDEBAND,
73     OPUS_BANDWIDTH_FULLBAND
74 };
75
76 typedef struct SilkContext SilkContext;
77
78 typedef struct CeltContext CeltContext;
79
80 typedef struct OpusPacket {
81     int packet_size;                /**< packet size */
82     int data_size;                  /**< size of the useful data -- packet size - padding */
83     int code;                       /**< packet code: specifies the frame layout */
84     int stereo;                     /**< whether this packet is mono or stereo */
85     int vbr;                        /**< vbr flag */
86     int config;                     /**< configuration: tells the audio mode,
87                                      **                bandwidth, and frame duration */
88     int frame_count;                /**< frame count */
89     int frame_offset[MAX_FRAMES];   /**< frame offsets */
90     int frame_size[MAX_FRAMES];     /**< frame sizes */
91     int frame_duration;             /**< frame duration, in samples @ 48kHz */
92     enum OpusMode mode;             /**< mode */
93     enum OpusBandwidth bandwidth;   /**< bandwidth */
94 } OpusPacket;
95
96 typedef struct OpusStreamContext {
97     AVCodecContext *avctx;
98     int output_channels;
99
100     OpusRangeCoder rc;
101     OpusRangeCoder redundancy_rc;
102     SilkContext *silk;
103     CeltContext *celt;
104     AVFloatDSPContext *fdsp;
105
106     float silk_buf[2][960];
107     float *silk_output[2];
108     DECLARE_ALIGNED(32, float, celt_buf)[2][960];
109     float *celt_output[2];
110
111     float redundancy_buf[2][960];
112     float *redundancy_output[2];
113
114     /* data buffers for the final output data */
115     float *out[2];
116     int out_size;
117
118     float *out_dummy;
119     int    out_dummy_allocated_size;
120
121     SwrContext *swr;
122     AVAudioFifo *celt_delay;
123     int silk_samplerate;
124     /* number of samples we still want to get from the resampler */
125     int delayed_samples;
126
127     OpusPacket packet;
128
129     int redundancy_idx;
130 } OpusStreamContext;
131
132 // a mapping between an opus stream and an output channel
133 typedef struct ChannelMap {
134     int stream_idx;
135     int channel_idx;
136
137     // when a single decoded channel is mapped to multiple output channels, we
138     // write to the first output directly and copy from it to the others
139     // this field is set to 1 for those copied output channels
140     int copy;
141     // this is the index of the output channel to copy from
142     int copy_idx;
143
144     // this channel is silent
145     int silence;
146 } ChannelMap;
147
148 typedef struct OpusContext {
149     OpusStreamContext *streams;
150
151     /* current output buffers for each streams */
152     float **out;
153     int   *out_size;
154     /* Buffers for synchronizing the streams when they have different
155      * resampling delays */
156     AVAudioFifo **sync_buffers;
157     /* number of decoded samples for each stream */
158     int         *decoded_samples;
159
160     int             nb_streams;
161     int      nb_stereo_streams;
162
163     AVFloatDSPContext *fdsp;
164     int16_t gain_i;
165     float   gain;
166
167     ChannelMap *channel_maps;
168 } OpusContext;
169
170 int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
171                          int self_delimited);
172
173 int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s);
174
175 int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels);
176 void ff_silk_free(SilkContext **ps);
177 void ff_silk_flush(SilkContext *s);
178
179 /**
180  * Decode the LP layer of one Opus frame (which may correspond to several SILK
181  * frames).
182  */
183 int ff_silk_decode_superframe(SilkContext *s, OpusRangeCoder *rc,
184                               float *output[2],
185                               enum OpusBandwidth bandwidth, int coded_channels,
186                               int duration_ms);
187
188 int ff_celt_init(AVCodecContext *avctx, CeltContext **s, int output_channels);
189
190 void ff_celt_free(CeltContext **s);
191
192 void ff_celt_flush(CeltContext *s);
193
194 int ff_celt_decode_frame(CeltContext *s, OpusRangeCoder *rc,
195                          float **output, int coded_channels, int frame_size,
196                          int startband,  int endband);
197
198 #endif /* AVCODEC_OPUS_H */