FFmpeg
opus.h
Go to the documentation of this file.
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 {
66 
68 };
69 
76 
78 };
79 
80 typedef struct SilkContext SilkContext;
81 
82 typedef struct CeltFrame CeltFrame;
83 
84 typedef struct OpusPacket {
85  int packet_size; /**< packet size */
86  int data_size; /**< size of the useful data -- packet size - padding */
87  int code; /**< packet code: specifies the frame layout */
88  int stereo; /**< whether this packet is mono or stereo */
89  int vbr; /**< vbr flag */
90  int config; /**< configuration: tells the audio mode,
91  ** bandwidth, and frame duration */
92  int frame_count; /**< frame count */
93  int frame_offset[MAX_FRAMES]; /**< frame offsets */
94  int frame_size[MAX_FRAMES]; /**< frame sizes */
95  int frame_duration; /**< frame duration, in samples @ 48kHz */
96  enum OpusMode mode; /**< mode */
97  enum OpusBandwidth bandwidth; /**< bandwidth */
98 } OpusPacket;
99 
100 typedef struct OpusStreamContext {
103 
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  DECLARE_ALIGNED(32, 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;
124 
128  /* number of samples we still want to get from the resampler */
130 
132 
135 
136 // a mapping between an opus stream and an output channel
137 typedef struct ChannelMap {
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 {
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 */
163  /* number of decoded samples for each stream */
165 
168 
170  int16_t gain_i;
171  float gain;
172 
174 } OpusContext;
175 
176 int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
177  int self_delimited);
178 
180 
181 int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels);
182 void ff_silk_free(SilkContext **ps);
184 
185 /**
186  * Decode the LP layer of one Opus frame (which may correspond to several SILK
187  * frames).
188  */
190  float *output[2],
191  enum OpusBandwidth bandwidth, int coded_channels,
192  int duration_ms);
193 
194 /* Encode or decode CELT bands */
196 
197 /* Encode or decode CELT bitallocation */
199 
200 #endif /* AVCODEC_OPUS_H */
OpusPacket::bandwidth
enum OpusBandwidth bandwidth
bandwidth
Definition: opus.h:97
OpusStreamContext::avctx
AVCodecContext * avctx
Definition: opus.h:101
ff_celt_quant_bands
void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc)
Definition: opus.c:446
OPUS_BANDWIDTH_NARROWBAND
@ OPUS_BANDWIDTH_NARROWBAND
Definition: opus.h:71
OpusStreamContext::delayed_samples
int delayed_samples
Definition: opus.h:129
opus_default_extradata
static const uint8_t opus_default_extradata[30]
Definition: opus.h:56
OpusPacket::code
int code
packet code: specifies the frame layout
Definition: opus.h:87
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
OpusContext::out
float ** out
Definition: opus.h:158
OpusMode
OpusMode
Definition: opus.h:62
OPUS_BANDWIDTH_FULLBAND
@ OPUS_BANDWIDTH_FULLBAND
Definition: opus.h:75
OpusStreamContext::rc
OpusRangeCoder rc
Definition: opus.h:104
OpusContext::out_size
int * out_size
Definition: opus.h:159
ff_celt_bitalloc
void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode)
Definition: opus.c:555
OpusPacket::vbr
int vbr
vbr flag
Definition: opus.h:89
ChannelMap::copy
int copy
Definition: opus.h:144
OpusStreamContext::celt_delay
AVAudioFifo * celt_delay
Definition: opus.h:126
OpusStreamContext::swr
SwrContext * swr
Definition: opus.h:125
AVAudioFifo
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:34
OpusStreamContext::out_size
int out_size
Definition: opus.h:120
OpusContext::gain
float gain
Definition: opus.h:171
OpusStreamContext::celt
CeltFrame * celt
Definition: opus.h:107
OpusStreamContext::redundancy_buf
float redundancy_buf[2][960]
Definition: opus.h:115
OpusStreamContext::celt_output
float * celt_output[2]
Definition: opus.h:113
OpusStreamContext::out
float * out[2]
Definition: opus.h:119
OpusStreamContext::silk_output
float * silk_output[2]
Definition: opus.h:111
OpusStreamContext
Definition: opus.h:100
OpusContext::apply_phase_inv
int apply_phase_inv
Definition: opus.h:155
s
#define s(width, name)
Definition: cbs_vp9.c:257
OpusStreamContext::fdsp
AVFloatDSPContext * fdsp
Definition: opus.h:108
OpusStreamContext::redundancy_idx
int redundancy_idx
Definition: opus.h:133
ff_silk_init
int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels)
Definition: opus_silk.c:874
OpusPacket::frame_duration
int frame_duration
frame duration, in samples @ 48kHz
Definition: opus.h:95
OpusContext::av_class
AVClass * av_class
Definition: opus.h:153
ff_silk_flush
void ff_silk_flush(SilkContext *s)
Definition: opus_silk.c:866
SwrContext
The libswresample context.
Definition: swresample_internal.h:95
f
#define f(width, name)
Definition: cbs_vp9.c:255
SilkContext
Definition: opus_silk.c:45
OpusContext::nb_stereo_streams
int nb_stereo_streams
Definition: opus.h:167
OPUS_BANDWIDTH_WIDEBAND
@ OPUS_BANDWIDTH_WIDEBAND
Definition: opus.h:73
OPUS_MODE_NB
@ OPUS_MODE_NB
Definition: opus.h:67
OPUS_MODE_CELT
@ OPUS_MODE_CELT
Definition: opus.h:65
ff_silk_decode_superframe
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:785
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
ff_opus_parse_packet
int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size, int self_delimited)
Parse Opus packet info from raw packet data.
Definition: opus.c:91
OPUS_BANDWITH_NB
@ OPUS_BANDWITH_NB
Definition: opus.h:77
OpusContext::sync_buffers
AVAudioFifo ** sync_buffers
Definition: opus.h:162
OpusStreamContext::silk_samplerate
int silk_samplerate
Definition: opus.h:127
OpusStreamContext::packet
OpusPacket packet
Definition: opus.h:131
OpusContext::streams
OpusStreamContext * streams
Definition: opus.h:154
OpusStreamContext::out_dummy_allocated_size
int out_dummy_allocated_size
Definition: opus.h:123
OpusContext::gain_i
int16_t gain_i
Definition: opus.h:170
OpusStreamContext::redundancy_rc
OpusRangeCoder redundancy_rc
Definition: opus.h:105
OpusContext::fdsp
AVFloatDSPContext * fdsp
Definition: opus.h:169
OPUS_BANDWIDTH_SUPERWIDEBAND
@ OPUS_BANDWIDTH_SUPERWIDEBAND
Definition: opus.h:74
swresample.h
float_dsp.h
ff_opus_parse_extradata
int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s)
Definition: opus.c:292
OpusRangeCoder
Definition: opus_rc.h:40
OpusStreamContext::silk
SilkContext * silk
Definition: opus.h:106
AVFloatDSPContext
Definition: float_dsp.h:24
frame.h
OpusPacket::data_size
int data_size
size of the useful data – packet size - padding
Definition: opus.h:86
encode
static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, FILE *output)
Definition: encode_audio.c:95
OpusContext::channel_maps
ChannelMap * channel_maps
Definition: opus.h:173
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:112
ChannelMap::silence
int silence
Definition: opus.h:149
OpusContext::nb_streams
int nb_streams
Definition: opus.h:166
OpusPacket::frame_size
int frame_size[MAX_FRAMES]
frame sizes
Definition: opus.h:94
MAX_FRAMES
#define MAX_FRAMES
Definition: opus.h:38
OPUS_MODE_HYBRID
@ OPUS_MODE_HYBRID
Definition: opus.h:64
uint8_t
uint8_t
Definition: audio_convert.c:194
audio_fifo.h
avcodec.h
OpusPacket::mode
enum OpusMode mode
mode
Definition: opus.h:96
AVCodecContext
main external API structure.
Definition: avcodec.h:526
OpusPacket::frame_offset
int frame_offset[MAX_FRAMES]
frame offsets
Definition: opus.h:93
OpusStreamContext::silk_buf
float silk_buf[2][960]
Definition: opus.h:110
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
OpusPacket::config
int config
configuration: tells the audio mode, bandwidth, and frame duration
Definition: opus.h:90
OPUS_MODE_SILK
@ OPUS_MODE_SILK
Definition: opus.h:63
OpusPacket
Definition: opus.h:84
ChannelMap
Definition: opus.h:137
ChannelMap::stream_idx
int stream_idx
Definition: opus.h:138
ChannelMap::copy_idx
int copy_idx
Definition: opus.h:146
OpusStreamContext::out_dummy
float * out_dummy
Definition: opus.h:122
OPUS_BANDWIDTH_MEDIUMBAND
@ OPUS_BANDWIDTH_MEDIUMBAND
Definition: opus.h:72
ChannelMap::channel_idx
int channel_idx
Definition: opus.h:139
OpusPacket::stereo
int stereo
whether this packet is mono or stereo
Definition: opus.h:88
OpusStreamContext::celt_buf
float celt_buf[2][960]
Definition: opus.h:112
OpusContext::decoded_samples
int * decoded_samples
Definition: opus.h:164
OpusPacket::packet_size
int packet_size
packet size
Definition: opus.h:85
opus_rc.h
ff_silk_free
void ff_silk_free(SilkContext **ps)
Definition: opus_silk.c:861
OpusStreamContext::output_channels
int output_channels
Definition: opus.h:102
OpusStreamContext::redundancy_output
float * redundancy_output[2]
Definition: opus.h:116
OpusBandwidth
OpusBandwidth
Definition: opus.h:70
OpusPacket::frame_count
int frame_count
frame count
Definition: opus.h:92
CeltFrame
Definition: opus_celt.h:92
OpusContext
Definition: opus.h:152