FFmpeg
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 
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  OPUS_MODE_NB
68 };
69 
70 enum OpusBandwidth {
71  OPUS_BANDWIDTH_NARROWBAND,
72  OPUS_BANDWIDTH_MEDIUMBAND,
73  OPUS_BANDWIDTH_WIDEBAND,
74  OPUS_BANDWIDTH_SUPERWIDEBAND,
75  OPUS_BANDWIDTH_FULLBAND,
76 
77  OPUS_BANDWITH_NB
78 };
79 
80 typedef struct SilkContext SilkContext;
81 
82 typedef struct CeltFrame CeltFrame;
83 
84 typedef struct OpusPacket {
86  int data_size;
87  int code;
88  int stereo;
89  int vbr;
90  int config;
96  enum OpusMode mode;
97  enum OpusBandwidth bandwidth;
98 } OpusPacket;
99 
100 typedef struct OpusStreamContext {
101  AVCodecContext *avctx;
102  int output_channels;
103 
104  OpusRangeCoder rc;
105  OpusRangeCoder redundancy_rc;
106  SilkContext *silk;
107  CeltFrame *celt;
108  AVFloatDSPContext *fdsp;
109 
110  float silk_buf[2][960];
111  float *silk_output[2];
112  DECLARE_ALIGNED(32, float, celt_buf)[2][960];
113  float *celt_output[2];
114 
115  float redundancy_buf[2][960];
116  float *redundancy_output[2];
117 
118  /* data buffers for the final output data */
119  float *out[2];
120  int out_size;
121 
122  float *out_dummy;
123  int out_dummy_allocated_size;
124 
125  SwrContext *swr;
126  AVAudioFifo *celt_delay;
127  int silk_samplerate;
128  /* number of samples we still want to get from the resampler */
129  int delayed_samples;
130 
131  OpusPacket packet;
132 
133  int redundancy_idx;
135 
136 // a mapping between an opus stream and an output channel
137 typedef struct ChannelMap {
138  int stream_idx;
139  int channel_idx;
140 
141  // when a single decoded channel is mapped to multiple output channels, we
142  // write to the first output directly and copy from it to the others
143  // this field is set to 1 for those copied output channels
144  int copy;
145  // this is the index of the output channel to copy from
146  int copy_idx;
147 
148  // this channel is silent
149  int silence;
150 } ChannelMap;
151 
152 typedef struct OpusContext {
153  AVClass *av_class;
154  OpusStreamContext *streams;
155  int apply_phase_inv;
156 
157  /* current output buffers for each streams */
158  float **out;
159  int *out_size;
160  /* Buffers for synchronizing the streams when they have different
161  * resampling delays */
162  AVAudioFifo **sync_buffers;
163  /* number of decoded samples for each stream */
164  int *decoded_samples;
165 
166  int nb_streams;
167  int nb_stereo_streams;
168 
169  AVFloatDSPContext *fdsp;
170  int16_t gain_i;
171  float gain;
172 
173  ChannelMap *channel_maps;
174 } OpusContext;
175 
176 int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
177  int self_delimited);
178 
179 int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s);
180 
181 int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels);
182 void ff_silk_free(SilkContext **ps);
183 void ff_silk_flush(SilkContext *s);
184 
190  float *output[2],
191  enum OpusBandwidth bandwidth, int coded_channels,
192  int duration_ms);
193 
194 /* Encode or decode CELT bands */
195 void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc);
196 
197 /* Encode or decode CELT bitallocation */
198 void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode);
199 
200 #endif /* AVCODEC_OPUS_H */
int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size, int self_delimiting)
Parse Opus packet info from raw packet data.
Definition: opus.c:90
int frame_count
frame count
Definition: opus.h:92
int vbr
vbr flag
Definition: opus.h:89
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
Definition: mem.h:120
Definition: opus.h:100
libswresample public header
Definition: swresample_internal.h:95
reference-counted frame API
Definition: audio_fifo.c:34
int frame_size[MAX_FRAMES]
frame sizes
Definition: opus.h:94
int frame_duration
frame duration, in samples @ 48kHz
Definition: opus.h:95
Definition: float_dsp.h:24
Definition: opus.h:84
int ff_silk_decode_superframe(SilkContext *s, OpusRangeCoder *rc, float *output[2], enum OpusBandwidth bandwidth, int coded_channels, int duration_ms)
Decode the LP layer of one Opus frame (which may correspond to several SILK frames).
Definition: opus_silk.c:781
Definition: opus_rc.h:40
Definition: opus.h:137
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1518
int config
configuration: tells the audio mode, bandwidth, and frame duration
Definition: opus.h:90
Describe the class of an AVClass context structure.
Definition: log.h:67
enum OpusMode mode
mode
Definition: opus.h:96
Definition: opus.h:152
int stereo
whether this packet is mono or stereo
Definition: opus.h:88
int data_size
size of the useful data – packet size - padding
Definition: opus.h:86
Definition: opus_silk.c:45
Definition: opus_celt.h:92
int packet_size
packet size
Definition: opus.h:85
Audio FIFO Buffer.
int frame_offset[MAX_FRAMES]
frame offsets
Definition: opus.h:93
enum OpusBandwidth bandwidth
bandwidth
Definition: opus.h:97
#define MAX_FRAMES
maximum number of frames per superframe
Definition: wmavoice.c:51
int code
packet code: specifies the frame layout
Definition: opus.h:87