]> git.sesse.net Git - ffmpeg/blob - libavcodec/opus.h
Merge commit '7d7355aa92bb36ca0765c49a569a999bcb96f332'
[ffmpeg] / 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 #define CELT_VECTORS                 11
47 #define CELT_ALLOC_STEPS             6
48 #define CELT_FINE_OFFSET             21
49 #define CELT_MAX_FINE_BITS           8
50 #define CELT_NORM_SCALE              16384
51 #define CELT_QTHETA_OFFSET           4
52 #define CELT_QTHETA_OFFSET_TWOPHASE  16
53 #define CELT_DEEMPH_COEFF            0.85000610f
54 #define CELT_POSTFILTER_MINPERIOD    15
55 #define CELT_ENERGY_SILENCE          (-28.0f)
56
57 #define SILK_HISTORY                 322
58 #define SILK_MAX_LPC                 16
59
60 #define ROUND_MULL(a,b,s) (((MUL64(a, b) >> ((s) - 1)) + 1) >> 1)
61 #define ROUND_MUL16(a,b)  ((MUL16(a, b) + 16384) >> 15)
62
63 #define OPUS_TS_HEADER     0x7FE0        // 0x3ff (11 bits)
64 #define OPUS_TS_MASK       0xFFE0        // top 11 bits
65
66 static const uint8_t opus_default_extradata[30] = {
67     'O', 'p', 'u', 's', 'H', 'e', 'a', 'd',
68     1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70 };
71
72 enum OpusMode {
73     OPUS_MODE_SILK,
74     OPUS_MODE_HYBRID,
75     OPUS_MODE_CELT
76 };
77
78 enum OpusBandwidth {
79     OPUS_BANDWIDTH_NARROWBAND,
80     OPUS_BANDWIDTH_MEDIUMBAND,
81     OPUS_BANDWIDTH_WIDEBAND,
82     OPUS_BANDWIDTH_SUPERWIDEBAND,
83     OPUS_BANDWIDTH_FULLBAND
84 };
85
86 typedef struct SilkContext SilkContext;
87
88 typedef struct CeltContext CeltContext;
89
90 typedef struct OpusPacket {
91     int packet_size;                /**< packet size */
92     int data_size;                  /**< size of the useful data -- packet size - padding */
93     int code;                       /**< packet code: specifies the frame layout */
94     int stereo;                     /**< whether this packet is mono or stereo */
95     int vbr;                        /**< vbr flag */
96     int config;                     /**< configuration: tells the audio mode,
97                                      **                bandwidth, and frame duration */
98     int frame_count;                /**< frame count */
99     int frame_offset[MAX_FRAMES];   /**< frame offsets */
100     int frame_size[MAX_FRAMES];     /**< frame sizes */
101     int frame_duration;             /**< frame duration, in samples @ 48kHz */
102     enum OpusMode mode;             /**< mode */
103     enum OpusBandwidth bandwidth;   /**< bandwidth */
104 } OpusPacket;
105
106 typedef struct OpusStreamContext {
107     AVCodecContext *avctx;
108     int output_channels;
109
110     OpusRangeCoder rc;
111     OpusRangeCoder redundancy_rc;
112     SilkContext *silk;
113     CeltContext *celt;
114     AVFloatDSPContext *fdsp;
115
116     float silk_buf[2][960];
117     float *silk_output[2];
118     DECLARE_ALIGNED(32, float, celt_buf)[2][960];
119     float *celt_output[2];
120
121     float redundancy_buf[2][960];
122     float *redundancy_output[2];
123
124     /* data buffers for the final output data */
125     float *out[2];
126     int out_size;
127
128     float *out_dummy;
129     int    out_dummy_allocated_size;
130
131     SwrContext *swr;
132     AVAudioFifo *celt_delay;
133     int silk_samplerate;
134     /* number of samples we still want to get from the resampler */
135     int delayed_samples;
136
137     OpusPacket packet;
138
139     int redundancy_idx;
140 } OpusStreamContext;
141
142 // a mapping between an opus stream and an output channel
143 typedef struct ChannelMap {
144     int stream_idx;
145     int channel_idx;
146
147     // when a single decoded channel is mapped to multiple output channels, we
148     // write to the first output directly and copy from it to the others
149     // this field is set to 1 for those copied output channels
150     int copy;
151     // this is the index of the output channel to copy from
152     int copy_idx;
153
154     // this channel is silent
155     int silence;
156 } ChannelMap;
157
158 typedef struct OpusContext {
159     OpusStreamContext *streams;
160
161     /* current output buffers for each streams */
162     float **out;
163     int   *out_size;
164     /* Buffers for synchronizing the streams when they have different
165      * resampling delays */
166     AVAudioFifo **sync_buffers;
167     /* number of decoded samples for each stream */
168     int         *decoded_samples;
169
170     int             nb_streams;
171     int      nb_stereo_streams;
172
173     AVFloatDSPContext *fdsp;
174     int16_t gain_i;
175     float   gain;
176
177     ChannelMap *channel_maps;
178 } OpusContext;
179
180 int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
181                          int self_delimited);
182
183 int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s);
184
185 int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels);
186 void ff_silk_free(SilkContext **ps);
187 void ff_silk_flush(SilkContext *s);
188
189 /**
190  * Decode the LP layer of one Opus frame (which may correspond to several SILK
191  * frames).
192  */
193 int ff_silk_decode_superframe(SilkContext *s, OpusRangeCoder *rc,
194                               float *output[2],
195                               enum OpusBandwidth bandwidth, int coded_channels,
196                               int duration_ms);
197
198 int ff_celt_init(AVCodecContext *avctx, CeltContext **s, int output_channels);
199
200 void ff_celt_free(CeltContext **s);
201
202 void ff_celt_flush(CeltContext *s);
203
204 int ff_celt_decode_frame(CeltContext *s, OpusRangeCoder *rc,
205                          float **output, int coded_channels, int frame_size,
206                          int startband,  int endband);
207
208 #endif /* AVCODEC_OPUS_H */