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 #include "libavutil/mem_internal.h"
32 
34 
35 #include "avcodec.h"
36 #include "opus_rc.h"
37 
38 #define MAX_FRAME_SIZE 1275
39 #define MAX_FRAMES 48
40 #define MAX_PACKET_DUR 5760
41 
42 #define CELT_SHORT_BLOCKSIZE 120
43 #define CELT_OVERLAP CELT_SHORT_BLOCKSIZE
44 #define CELT_MAX_LOG_BLOCKS 3
45 #define CELT_MAX_FRAME_SIZE (CELT_SHORT_BLOCKSIZE * (1 << CELT_MAX_LOG_BLOCKS))
46 #define CELT_MAX_BANDS 21
47 
48 #define SILK_HISTORY 322
49 #define SILK_MAX_LPC 16
50 
51 #define ROUND_MULL(a,b,s) (((MUL64(a, b) >> ((s) - 1)) + 1) >> 1)
52 #define ROUND_MUL16(a,b) ((MUL16(a, b) + 16384) >> 15)
53 
54 #define OPUS_TS_HEADER 0x7FE0 // 0x3ff (11 bits)
55 #define OPUS_TS_MASK 0xFFE0 // top 11 bits
56 
57 static const uint8_t opus_default_extradata[30] = {
58  'O', 'p', 'u', 's', 'H', 'e', 'a', 'd',
59  1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61 };
62 
63 enum OpusMode {
67 
69 };
70 
77 
79 };
80 
81 typedef struct SilkContext SilkContext;
82 
83 typedef struct CeltFrame CeltFrame;
84 
85 typedef struct OpusPacket {
86  int packet_size; /**< packet size */
87  int data_size; /**< size of the useful data -- packet size - padding */
88  int code; /**< packet code: specifies the frame layout */
89  int stereo; /**< whether this packet is mono or stereo */
90  int vbr; /**< vbr flag */
91  int config; /**< configuration: tells the audio mode,
92  ** bandwidth, and frame duration */
93  int frame_count; /**< frame count */
94  int frame_offset[MAX_FRAMES]; /**< frame offsets */
95  int frame_size[MAX_FRAMES]; /**< frame sizes */
96  int frame_duration; /**< frame duration, in samples @ 48kHz */
97  enum OpusMode mode; /**< mode */
98  enum OpusBandwidth bandwidth; /**< bandwidth */
99 } OpusPacket;
100 
101 typedef struct OpusStreamContext {
104 
105  /* number of decoded samples for this stream */
107  /* current output buffers for this stream */
108  float *out[2];
109  int out_size;
110  /* Buffer with samples from this stream for synchronizing
111  * the streams when they have different resampling delays */
113 
119 
120  float silk_buf[2][960];
121  float *silk_output[2];
122  DECLARE_ALIGNED(32, float, celt_buf)[2][960];
123  float *celt_output[2];
124 
125  DECLARE_ALIGNED(32, float, redundancy_buf)[2][960];
126  float *redundancy_output[2];
127 
128  /* buffers for the next samples to be decoded */
129  float *cur_out[2];
131 
132  float *out_dummy;
134 
138  /* number of samples we still want to get from the resampler */
140 
142 
145 
146 // a mapping between an opus stream and an output channel
147 typedef struct ChannelMap {
150 
151  // when a single decoded channel is mapped to multiple output channels, we
152  // write to the first output directly and copy from it to the others
153  // this field is set to 1 for those copied output channels
154  int copy;
155  // this is the index of the output channel to copy from
156  int copy_idx;
157 
158  // this channel is silent
159  int silence;
160 } ChannelMap;
161 
162 typedef struct OpusContext {
166 
169 
171  int16_t gain_i;
172  float gain;
173 
175 } OpusContext;
176 
177 int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
178  int self_delimited);
179 
181 
182 int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels);
183 void ff_silk_free(SilkContext **ps);
185 
186 /**
187  * Decode the LP layer of one Opus frame (which may correspond to several SILK
188  * frames).
189  */
191  float *output[2],
192  enum OpusBandwidth bandwidth, int coded_channels,
193  int duration_ms);
194 
195 /* Encode or decode CELT bands */
197 
198 /* Encode or decode CELT bitallocation */
200 
201 #endif /* AVCODEC_OPUS_H */
int frame_count
frame count
Definition: opus.h:93
int nb_stereo_streams
Definition: opus.h:168
int output_channels
Definition: opus.h:103
int delayed_samples
Definition: opus.h:139
float gain
Definition: opus.h:172
int remaining_out_size
Definition: opus.h:130
AVPacket * pkt
Definition: movenc.c:59
int vbr
vbr flag
Definition: opus.h:90
int16_t gain_i
Definition: opus.h:171
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
void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc)
Definition: opus.c:446
int apply_phase_inv
Definition: opus.h:165
uint8_t
#define f(width, name)
Definition: cbs_vp9.c:255
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
int copy
Definition: opus.h:154
SilkContext * silk
Definition: opus.h:116
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
Definition: mem.h:117
AVFloatDSPContext * fdsp
Definition: opus.h:118
ChannelMap * channel_maps
Definition: opus.h:174
libswresample public header
int nb_streams
Definition: opus.h:167
static const uint8_t opus_default_extradata[30]
Definition: opus.h:57
int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s)
Definition: opus.c:292
The libswresample context.
AVFloatDSPContext * fdsp
Definition: opus.h:170
reference-counted frame API
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:34
void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode)
Definition: opus.c:555
int frame_size[MAX_FRAMES]
frame sizes
Definition: opus.h:95
int frame_duration
frame duration, in samples @ 48kHz
Definition: opus.h:96
SwrContext * swr
Definition: opus.h:135
#define s(width, name)
Definition: cbs_vp9.c:257
int out_dummy_allocated_size
Definition: opus.h:133
int silence
Definition: opus.h:159
CeltFrame * celt
Definition: opus.h:117
float * out_dummy
Definition: opus.h:132
Libavcodec external API header.
OpusPacket packet
Definition: opus.h:141
void ff_silk_flush(SilkContext *s)
Definition: opus_silk.c:866
main external API structure.
Definition: avcodec.h:536
int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels)
Definition: opus_silk.c:874
static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, FILE *output)
Definition: encode_audio.c:95
int config
configuration: tells the audio mode, bandwidth, and frame duration
Definition: opus.h:91
void ff_silk_free(SilkContext **ps)
Definition: opus_silk.c:861
Describe the class of an AVClass context structure.
Definition: log.h:67
enum OpusMode mode
mode
Definition: opus.h:97
int copy_idx
Definition: opus.h:156
int stereo
whether this packet is mono or stereo
Definition: opus.h:89
AVCodecContext * avctx
Definition: opus.h:102
int data_size
size of the useful data – packet size - padding
Definition: opus.h:87
int channel_idx
Definition: opus.h:149
int redundancy_idx
Definition: opus.h:143
int decoded_samples
Definition: opus.h:106
OpusRangeCoder rc
Definition: opus.h:114
int stream_idx
Definition: opus.h:148
OpusBandwidth
Definition: opus.h:71
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 * av_class
Definition: opus.h:163
OpusStreamContext * streams
Definition: opus.h:164
int packet_size
packet size
Definition: opus.h:86
OpusRangeCoder redundancy_rc
Definition: opus.h:115
Audio FIFO Buffer.
OpusMode
Definition: opus.h:63
int frame_offset[MAX_FRAMES]
frame offsets
Definition: opus.h:94
enum OpusBandwidth bandwidth
bandwidth
Definition: opus.h:98
AVAudioFifo * celt_delay
Definition: opus.h:136
FILE * out
Definition: movenc.c:54
int silk_samplerate
Definition: opus.h:137
int code
packet code: specifies the frame layout
Definition: opus.h:88
AVAudioFifo * sync_buffer
Definition: opus.h:112
#define MAX_FRAMES
Definition: opus.h:39