xref: /aosp_15_r20/external/libopus/src/opus_private.h (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1 /* Copyright (c) 2012 Xiph.Org Foundation
2    Written by Jean-Marc Valin */
3 /*
4    Redistribution and use in source and binary forms, with or without
5    modification, are permitted provided that the following conditions
6    are met:
7 
8    - Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 
11    - Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14 
15    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 
29 #ifndef OPUS_PRIVATE_H
30 #define OPUS_PRIVATE_H
31 
32 #include "arch.h"
33 #include "opus.h"
34 #include "celt.h"
35 
36 #include <stdarg.h> /* va_list */
37 #include <stddef.h> /* offsetof */
38 
39 struct OpusRepacketizer {
40    unsigned char toc;
41    int nb_frames;
42    const unsigned char *frames[48];
43    opus_int16 len[48];
44    int framesize;
45    const unsigned char *paddings[48];
46    opus_int32 padding_len[48];
47 };
48 
49 typedef struct {
50    int id;
51    int frame;
52    const unsigned char *data;
53    opus_int32 len;
54 } opus_extension_data;
55 
56 typedef struct ChannelLayout {
57    int nb_channels;
58    int nb_streams;
59    int nb_coupled_streams;
60    unsigned char mapping[256];
61 } ChannelLayout;
62 
63 typedef enum {
64   MAPPING_TYPE_NONE,
65   MAPPING_TYPE_SURROUND,
66   MAPPING_TYPE_AMBISONICS
67 } MappingType;
68 
69 struct OpusMSEncoder {
70    ChannelLayout layout;
71    int arch;
72    int lfe_stream;
73    int application;
74    int variable_duration;
75    MappingType mapping_type;
76    opus_int32 bitrate_bps;
77    /* Encoder states go here */
78    /* then opus_val32 window_mem[channels*120]; */
79    /* then opus_val32 preemph_mem[channels]; */
80 };
81 
82 struct OpusMSDecoder {
83    ChannelLayout layout;
84    /* Decoder states go here */
85 };
86 
87 int opus_multistream_encoder_ctl_va_list(struct OpusMSEncoder *st, int request,
88   va_list ap);
89 int opus_multistream_decoder_ctl_va_list(struct OpusMSDecoder *st, int request,
90   va_list ap);
91 
92 int validate_layout(const ChannelLayout *layout);
93 int get_left_channel(const ChannelLayout *layout, int stream_id, int prev);
94 int get_right_channel(const ChannelLayout *layout, int stream_id, int prev);
95 int get_mono_channel(const ChannelLayout *layout, int stream_id, int prev);
96 
97 typedef void (*opus_copy_channel_in_func)(
98   opus_val16 *dst,
99   int dst_stride,
100   const void *src,
101   int src_stride,
102   int src_channel,
103   int frame_size,
104   void *user_data
105 );
106 
107 typedef void (*opus_copy_channel_out_func)(
108   void *dst,
109   int dst_stride,
110   int dst_channel,
111   const opus_val16 *src,
112   int src_stride,
113   int frame_size,
114   void *user_data
115 );
116 
117 #define MODE_SILK_ONLY          1000
118 #define MODE_HYBRID             1001
119 #define MODE_CELT_ONLY          1002
120 
121 #define OPUS_SET_VOICE_RATIO_REQUEST         11018
122 #define OPUS_GET_VOICE_RATIO_REQUEST         11019
123 
124 /** Configures the encoder's expected percentage of voice
125   * opposed to music or other signals.
126   *
127   * @note This interface is currently more aspiration than actuality. It's
128   * ultimately expected to bias an automatic signal classifier, but it currently
129   * just shifts the static bitrate to mode mapping around a little bit.
130   *
131   * @param[in] x <tt>int</tt>:   Voice percentage in the range 0-100, inclusive.
132   * @hideinitializer */
133 #define OPUS_SET_VOICE_RATIO(x) OPUS_SET_VOICE_RATIO_REQUEST, __opus_check_int(x)
134 /** Gets the encoder's configured voice ratio value, @see OPUS_SET_VOICE_RATIO
135   *
136   * @param[out] x <tt>int*</tt>:  Voice percentage in the range 0-100, inclusive.
137   * @hideinitializer */
138 #define OPUS_GET_VOICE_RATIO(x) OPUS_GET_VOICE_RATIO_REQUEST, __opus_check_int_ptr(x)
139 
140 
141 #define OPUS_SET_FORCE_MODE_REQUEST    11002
142 #define OPUS_SET_FORCE_MODE(x) OPUS_SET_FORCE_MODE_REQUEST, __opus_check_int(x)
143 
144 typedef void (*downmix_func)(const void *, opus_val32 *, int, int, int, int, int);
145 void downmix_float(const void *_x, opus_val32 *sub, int subframe, int offset, int c1, int c2, int C);
146 void downmix_int(const void *_x, opus_val32 *sub, int subframe, int offset, int c1, int c2, int C);
147 int is_digital_silence(const opus_val16* pcm, int frame_size, int channels, int lsb_depth);
148 
149 int encode_size(int size, unsigned char *data);
150 
151 opus_int32 frame_size_select(opus_int32 frame_size, int variable_duration, opus_int32 Fs);
152 
153 opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
154       unsigned char *data, opus_int32 out_data_bytes, int lsb_depth,
155       const void *analysis_pcm, opus_int32 analysis_size, int c1, int c2,
156       int analysis_channels, downmix_func downmix, int float_api);
157 
158 int opus_decode_native(OpusDecoder *st, const unsigned char *data, opus_int32 len,
159       opus_val16 *pcm, int frame_size, int decode_fec, int self_delimited,
160       opus_int32 *packet_offset, int soft_clip, const OpusDRED *dred, opus_int32 dred_offset);
161 
162 /* Make sure everything is properly aligned. */
align(int i)163 static OPUS_INLINE int align(int i)
164 {
165     struct foo {char c; union { void* p; opus_int32 i; opus_val32 v; } u;};
166 
167     unsigned int alignment = offsetof(struct foo, u);
168 
169     /* Optimizing compilers should optimize div and multiply into and
170        for all sensible alignment values. */
171     return ((i + alignment - 1) / alignment) * alignment;
172 }
173 
174 /* More than that is ridiculous for now (3 * max frames per packet)*/
175 opus_int32 skip_extension(const unsigned char **data, opus_int32 len, opus_int32 *header_size);
176 
177 int opus_packet_parse_impl(const unsigned char *data, opus_int32 len,
178       int self_delimited, unsigned char *out_toc,
179       const unsigned char *frames[48], opus_int16 size[48],
180       int *payload_offset, opus_int32 *packet_offset,
181       const unsigned char **padding, opus_int32 *padding_len);
182 
183 opus_int32 opus_repacketizer_out_range_impl(OpusRepacketizer *rp, int begin, int end,
184       unsigned char *data, opus_int32 maxlen, int self_delimited, int pad,
185       const opus_extension_data *extensions, int nb_extensions);
186 
187 int pad_frame(unsigned char *data, opus_int32 len, opus_int32 new_len);
188 
189 int opus_multistream_encode_native
190 (
191   struct OpusMSEncoder *st,
192   opus_copy_channel_in_func copy_channel_in,
193   const void *pcm,
194   int analysis_frame_size,
195   unsigned char *data,
196   opus_int32 max_data_bytes,
197   int lsb_depth,
198   downmix_func downmix,
199   int float_api,
200   void *user_data
201 );
202 
203 int opus_multistream_decode_native(
204   struct OpusMSDecoder *st,
205   const unsigned char *data,
206   opus_int32 len,
207   void *pcm,
208   opus_copy_channel_out_func copy_channel_out,
209   int frame_size,
210   int decode_fec,
211   int soft_clip,
212   void *user_data
213 );
214 
215 opus_int32 opus_packet_extensions_parse(const unsigned char *data, opus_int32 len, opus_extension_data *extensions, opus_int32 *nb_extensions);
216 
217 opus_int32 opus_packet_extensions_generate(unsigned char *data, opus_int32 len, const opus_extension_data  *extensions, int nb_extensions, int pad);
218 
219 opus_int32 opus_packet_extensions_count(const unsigned char *data, opus_int32 len);
220 
221 opus_int32 opus_packet_pad_impl(unsigned char *data, opus_int32 len, opus_int32 new_len, int pad, const opus_extension_data  *extensions, int nb_extensions);
222 
223 #endif /* OPUS_PRIVATE_H */
224