Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
pcm_format.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 Roc Streaming authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_audio/pcm_format.h
10 //! @brief PCM format.
11 
12 #ifndef ROC_AUDIO_PCM_FORMAT_H_
13 #define ROC_AUDIO_PCM_FORMAT_H_
14 
15 #include "roc_core/stddefs.h"
16 
17 namespace roc {
18 namespace audio {
19 
20 //! PCM format.
21 //! Defines PCM sample coding and endian.
22 enum PcmFormat {
23  //! Invalid format.
25 
26  //! 8-bit signed integer, native endian.
28  //! 8-bit signed integer, big endian.
30  //! 8-bit signed integer, little endian.
32  //! 8-bit unsigned integer, native endian.
34  //! 8-bit unsigned integer, big endian.
36  //! 8-bit unsigned integer, little endian.
38 
39  //! 16-bit signed integer, native endian.
41  //! 16-bit signed integer, big endian.
43  //! 16-bit signed integer, little endian.
45  //! 16-bit unsigned integer, native endian.
47  //! 16-bit unsigned integer, big endian.
49  //! 16-bit unsigned integer, little endian.
51 
52  //! 18-bit signed integer (2.25 bytes), native endian.
54  //! 18-bit signed integer (2.25 bytes), big endian.
56  //! 18-bit signed integer (2.25 bytes), little endian.
58  //! 18-bit unsigned integer (2.25 bytes), native endian.
60  //! 18-bit unsigned integer (2.25 bytes), big endian.
62  //! 18-bit unsigned integer (2.25 bytes), little endian.
64 
65  //! 18-bit signed integer, in low bits of 3-byte container, native endian.
67  //! 18-bit signed integer, in low bits of 3-byte container, big endian.
69  //! 18-bit signed integer, in low bits of 3-byte container, little endian.
71  //! 18-bit unsigned integer, in low bits of 3-byte container, native endian.
73  //! 18-bit unsigned integer, in low bits of 3-byte container, big endian.
75  //! 18-bit unsigned integer, in low bits of 3-byte container, little endian.
77 
78  //! 18-bit signed integer, in low bits of 4-byte container, native endian.
80  //! 18-bit signed integer, in low bits of 4-byte container, big endian.
82  //! 18-bit signed integer, in low bits of 4-byte container, little endian.
84  //! 18-bit unsigned integer, in low bits of 4-byte container, native endian.
86  //! 18-bit unsigned integer, in low bits of 4-byte container, big endian.
88  //! 18-bit unsigned integer, in low bits of 4-byte container, little endian.
90 
91  //! 20-bit signed integer (2.5 bytes), native endian.
93  //! 20-bit signed integer (2.5 bytes), big endian.
95  //! 20-bit signed integer (2.5 bytes), little endian.
97  //! 20-bit unsigned integer (2.5 bytes), native endian.
99  //! 20-bit unsigned integer (2.5 bytes), big endian.
101  //! 20-bit unsigned integer (2.5 bytes), little endian.
103 
104  //! 20-bit signed integer, in low bits of 3-byte container, native endian.
106  //! 20-bit signed integer, in low bits of 3-byte container, big endian.
108  //! 20-bit signed integer, in low bits of 3-byte container, little endian.
110  //! 20-bit unsigned integer, in low bits of 3-byte container, native endian.
112  //! 20-bit unsigned integer, in low bits of 3-byte container, big endian.
114  //! 20-bit unsigned integer, in low bits of 3-byte container, little endian.
116 
117  //! 20-bit signed integer, in low bits of 4-byte container, native endian.
119  //! 20-bit signed integer, in low bits of 4-byte container, big endian.
121  //! 20-bit signed integer, in low bits of 4-byte container, little endian.
123  //! 20-bit unsigned integer, in low bits of 4-byte container, native endian.
125  //! 20-bit unsigned integer, in low bits of 4-byte container, big endian.
127  //! 20-bit unsigned integer, in low bits of 4-byte container, little endian.
129 
130  //! 24-bit signed integer (3 bytes), native endian.
132  //! 24-bit signed integer (3 bytes), big endian.
134  //! 24-bit signed integer (3 bytes), little endian.
136  //! 24-bit unsigned integer (3 bytes), native endian.
138  //! 24-bit unsigned integer (3 bytes), big endian.
140  //! 24-bit unsigned integer (3 bytes), little endian.
142 
143  //! 24-bit signed integer, in low bits of 4-byte container, native endian.
145  //! 24-bit signed integer, in low bits of 4-byte container, big endian.
147  //! 24-bit signed integer, in low bits of 4-byte container, little endian.
149  //! 24-bit unsigned integer, in low bits of 4-byte container, native endian.
151  //! 24-bit unsigned integer, in low bits of 4-byte container, big endian.
153  //! 24-bit unsigned integer, in low bits of 4-byte container, little endian.
155 
156  //! 32-bit signed integer, native endian.
158  //! 32-bit signed integer, big endian.
160  //! 32-bit signed integer, little endian.
162  //! 32-bit unsigned integer, native endian.
164  //! 32-bit unsigned integer, big endian.
166  //! 32-bit unsigned integer, little endian.
168 
169  //! 64-bit signed integer, native endian.
171  //! 64-bit signed integer, big endian.
173  //! 64-bit signed integer, little endian.
175  //! 64-bit unsigned integer, native endian.
177  //! 64-bit unsigned integer, big endian.
179  //! 64-bit unsigned integer, little endian.
181 
182  //! 32-bit IEEE-754 float in range [-1.0; +1.0], native endian.
184  //! 32-bit IEEE-754 float in range [-1.0; +1.0], big endian.
186  //! 32-bit IEEE-754 float in range [-1.0; +1.0], little endian.
188 
189  //! 64-bit IEEE-754 float in range [-1.0; +1.0], native endian.
191  //! 64-bit IEEE-754 float in range [-1.0; +1.0], big endian.
193  //! 64-bit IEEE-754 float in range [-1.0; +1.0], little endian.
195 
196  //! Maximum enum value.
198 };
199 
200 //! PCM format meta-information.
201 struct PcmTraits {
202  //! If traits are valid.
203  bool is_valid;
204 
205  //! True for integers, false for floating point.
207 
208  //! True for signed integers and floating point.
209  bool is_signed;
210 
211  //! True for little-endian formats.
212  bool is_little;
213 
214  //! Canonical identifier of the same format.
216 
217  //! Number of significant bits per sample.
218  size_t bit_depth;
219 
220  //! Number of stored bits per sample in packed form.
221  size_t bit_width;
222 
223  PcmTraits()
224  : is_valid(false)
225  , is_integer(false)
226  , is_signed(false)
227  , is_little(false)
229  , bit_depth(0)
230  , bit_width(0) {
231  }
232 };
233 
234 //! PCM mapping function.
235 typedef void (*PcmMapFn)(const uint8_t* in_data,
236  size_t& in_bit_off,
237  uint8_t* out_data,
238  size_t& out_bit_off,
239  size_t n_samples);
240 
241 //! Get mapping function for given PCM format pair.
243 
244 //! Get format traits for given PCM format.
246 
247 //! Get string name of PCM format.
248 const char* pcm_format_to_str(PcmFormat format);
249 
250 //! Get PCM format from string name.
252 
253 } // namespace audio
254 } // namespace roc
255 
256 #endif // ROC_AUDIO_PCM_FORMAT_H_
PcmMapFn pcm_format_mapfn(PcmFormat in_format, PcmFormat out_format)
Get mapping function for given PCM format pair.
PcmFormat pcm_format_from_str(const char *str)
Get PCM format from string name.
const char * pcm_format_to_str(PcmFormat format)
Get string name of PCM format.
void(* PcmMapFn)(const uint8_t *in_data, size_t &in_bit_off, uint8_t *out_data, size_t &out_bit_off, size_t n_samples)
PCM mapping function.
Definition: pcm_format.h:235
PcmFormat
PCM format. Defines PCM sample coding and endian.
Definition: pcm_format.h:22
@ PcmFormat_Float64
64-bit IEEE-754 float in range [-1.0; +1.0], native endian.
Definition: pcm_format.h:190
@ PcmFormat_SInt20_Le
20-bit signed integer (2.5 bytes), little endian.
Definition: pcm_format.h:96
@ PcmFormat_SInt8_Le
8-bit signed integer, little endian.
Definition: pcm_format.h:31
@ PcmFormat_SInt8
8-bit signed integer, native endian.
Definition: pcm_format.h:27
@ PcmFormat_UInt20_Le
20-bit unsigned integer (2.5 bytes), little endian.
Definition: pcm_format.h:102
@ PcmFormat_Float32
32-bit IEEE-754 float in range [-1.0; +1.0], native endian.
Definition: pcm_format.h:183
@ PcmFormat_SInt18_3_Be
18-bit signed integer, in low bits of 3-byte container, big endian.
Definition: pcm_format.h:68
@ PcmFormat_UInt18
18-bit unsigned integer (2.25 bytes), native endian.
Definition: pcm_format.h:59
@ PcmFormat_SInt18_Le
18-bit signed integer (2.25 bytes), little endian.
Definition: pcm_format.h:57
@ PcmFormat_UInt20_3_Be
20-bit unsigned integer, in low bits of 3-byte container, big endian.
Definition: pcm_format.h:113
@ PcmFormat_SInt18_4_Be
18-bit signed integer, in low bits of 4-byte container, big endian.
Definition: pcm_format.h:81
@ PcmFormat_Invalid
Invalid format.
Definition: pcm_format.h:24
@ PcmFormat_SInt20_3_Le
20-bit signed integer, in low bits of 3-byte container, little endian.
Definition: pcm_format.h:109
@ PcmFormat_SInt20
20-bit signed integer (2.5 bytes), native endian.
Definition: pcm_format.h:92
@ PcmFormat_UInt64_Le
64-bit unsigned integer, little endian.
Definition: pcm_format.h:180
@ PcmFormat_Float32_Le
32-bit IEEE-754 float in range [-1.0; +1.0], little endian.
Definition: pcm_format.h:187
@ PcmFormat_SInt18_3_Le
18-bit signed integer, in low bits of 3-byte container, little endian.
Definition: pcm_format.h:70
@ PcmFormat_UInt20
20-bit unsigned integer (2.5 bytes), native endian.
Definition: pcm_format.h:98
@ PcmFormat_SInt32_Be
32-bit signed integer, big endian.
Definition: pcm_format.h:159
@ PcmFormat_UInt18_3
18-bit unsigned integer, in low bits of 3-byte container, native endian.
Definition: pcm_format.h:72
@ PcmFormat_UInt24_Be
24-bit unsigned integer (3 bytes), big endian.
Definition: pcm_format.h:139
@ PcmFormat_SInt20_Be
20-bit signed integer (2.5 bytes), big endian.
Definition: pcm_format.h:94
@ PcmFormat_SInt24_4_Be
24-bit signed integer, in low bits of 4-byte container, big endian.
Definition: pcm_format.h:146
@ PcmFormat_Float64_Le
64-bit IEEE-754 float in range [-1.0; +1.0], little endian.
Definition: pcm_format.h:194
@ PcmFormat_SInt32_Le
32-bit signed integer, little endian.
Definition: pcm_format.h:161
@ PcmFormat_UInt16_Le
16-bit unsigned integer, little endian.
Definition: pcm_format.h:50
@ PcmFormat_UInt24_Le
24-bit unsigned integer (3 bytes), little endian.
Definition: pcm_format.h:141
@ PcmFormat_UInt32_Le
32-bit unsigned integer, little endian.
Definition: pcm_format.h:167
@ PcmFormat_UInt8_Be
8-bit unsigned integer, big endian.
Definition: pcm_format.h:35
@ PcmFormat_UInt18_4
18-bit unsigned integer, in low bits of 4-byte container, native endian.
Definition: pcm_format.h:85
@ PcmFormat_UInt20_4
20-bit unsigned integer, in low bits of 4-byte container, native endian.
Definition: pcm_format.h:124
@ PcmFormat_SInt24
24-bit signed integer (3 bytes), native endian.
Definition: pcm_format.h:131
@ PcmFormat_UInt24
24-bit unsigned integer (3 bytes), native endian.
Definition: pcm_format.h:137
@ PcmFormat_SInt18_Be
18-bit signed integer (2.25 bytes), big endian.
Definition: pcm_format.h:55
@ PcmFormat_Float64_Be
64-bit IEEE-754 float in range [-1.0; +1.0], big endian.
Definition: pcm_format.h:192
@ PcmFormat_UInt64_Be
64-bit unsigned integer, big endian.
Definition: pcm_format.h:178
@ PcmFormat_SInt24_Be
24-bit signed integer (3 bytes), big endian.
Definition: pcm_format.h:133
@ PcmFormat_SInt18
18-bit signed integer (2.25 bytes), native endian.
Definition: pcm_format.h:53
@ PcmFormat_SInt16_Be
16-bit signed integer, big endian.
Definition: pcm_format.h:42
@ PcmFormat_UInt64
64-bit unsigned integer, native endian.
Definition: pcm_format.h:176
@ PcmFormat_UInt32
32-bit unsigned integer, native endian.
Definition: pcm_format.h:163
@ PcmFormat_SInt24_4
24-bit signed integer, in low bits of 4-byte container, native endian.
Definition: pcm_format.h:144
@ PcmFormat_SInt32
32-bit signed integer, native endian.
Definition: pcm_format.h:157
@ PcmFormat_UInt24_4
24-bit unsigned integer, in low bits of 4-byte container, native endian.
Definition: pcm_format.h:150
@ PcmFormat_UInt18_3_Be
18-bit unsigned integer, in low bits of 3-byte container, big endian.
Definition: pcm_format.h:74
@ PcmFormat_UInt24_4_Be
24-bit unsigned integer, in low bits of 4-byte container, big endian.
Definition: pcm_format.h:152
@ PcmFormat_Max
Maximum enum value.
Definition: pcm_format.h:197
@ PcmFormat_SInt16_Le
16-bit signed integer, little endian.
Definition: pcm_format.h:44
@ PcmFormat_UInt20_3_Le
20-bit unsigned integer, in low bits of 3-byte container, little endian.
Definition: pcm_format.h:115
@ PcmFormat_UInt16
16-bit unsigned integer, native endian.
Definition: pcm_format.h:46
@ PcmFormat_UInt18_Be
18-bit unsigned integer (2.25 bytes), big endian.
Definition: pcm_format.h:61
@ PcmFormat_SInt20_4
20-bit signed integer, in low bits of 4-byte container, native endian.
Definition: pcm_format.h:118
@ PcmFormat_SInt20_4_Be
20-bit signed integer, in low bits of 4-byte container, big endian.
Definition: pcm_format.h:120
@ PcmFormat_SInt16
16-bit signed integer, native endian.
Definition: pcm_format.h:40
@ PcmFormat_SInt64_Be
64-bit signed integer, big endian.
Definition: pcm_format.h:172
@ PcmFormat_UInt18_Le
18-bit unsigned integer (2.25 bytes), little endian.
Definition: pcm_format.h:63
@ PcmFormat_SInt8_Be
8-bit signed integer, big endian.
Definition: pcm_format.h:29
@ PcmFormat_UInt20_4_Be
20-bit unsigned integer, in low bits of 4-byte container, big endian.
Definition: pcm_format.h:126
@ PcmFormat_SInt18_4
18-bit signed integer, in low bits of 4-byte container, native endian.
Definition: pcm_format.h:79
@ PcmFormat_UInt20_4_Le
20-bit unsigned integer, in low bits of 4-byte container, little endian.
Definition: pcm_format.h:128
@ PcmFormat_SInt20_3_Be
20-bit signed integer, in low bits of 3-byte container, big endian.
Definition: pcm_format.h:107
@ PcmFormat_SInt64
64-bit signed integer, native endian.
Definition: pcm_format.h:170
@ PcmFormat_SInt24_Le
24-bit signed integer (3 bytes), little endian.
Definition: pcm_format.h:135
@ PcmFormat_UInt8
8-bit unsigned integer, native endian.
Definition: pcm_format.h:33
@ PcmFormat_SInt18_4_Le
18-bit signed integer, in low bits of 4-byte container, little endian.
Definition: pcm_format.h:83
@ PcmFormat_SInt24_4_Le
24-bit signed integer, in low bits of 4-byte container, little endian.
Definition: pcm_format.h:148
@ PcmFormat_UInt20_Be
20-bit unsigned integer (2.5 bytes), big endian.
Definition: pcm_format.h:100
@ PcmFormat_UInt18_3_Le
18-bit unsigned integer, in low bits of 3-byte container, little endian.
Definition: pcm_format.h:76
@ PcmFormat_UInt24_4_Le
24-bit unsigned integer, in low bits of 4-byte container, little endian.
Definition: pcm_format.h:154
@ PcmFormat_UInt16_Be
16-bit unsigned integer, big endian.
Definition: pcm_format.h:48
@ PcmFormat_SInt64_Le
64-bit signed integer, little endian.
Definition: pcm_format.h:174
@ PcmFormat_SInt18_3
18-bit signed integer, in low bits of 3-byte container, native endian.
Definition: pcm_format.h:66
@ PcmFormat_UInt18_4_Le
18-bit unsigned integer, in low bits of 4-byte container, little endian.
Definition: pcm_format.h:89
@ PcmFormat_UInt20_3
20-bit unsigned integer, in low bits of 3-byte container, native endian.
Definition: pcm_format.h:111
@ PcmFormat_SInt20_3
20-bit signed integer, in low bits of 3-byte container, native endian.
Definition: pcm_format.h:105
@ PcmFormat_UInt32_Be
32-bit unsigned integer, big endian.
Definition: pcm_format.h:165
@ PcmFormat_Float32_Be
32-bit IEEE-754 float in range [-1.0; +1.0], big endian.
Definition: pcm_format.h:185
@ PcmFormat_SInt20_4_Le
20-bit signed integer, in low bits of 4-byte container, little endian.
Definition: pcm_format.h:122
@ PcmFormat_UInt18_4_Be
18-bit unsigned integer, in low bits of 4-byte container, big endian.
Definition: pcm_format.h:87
@ PcmFormat_UInt8_Le
8-bit unsigned integer, little endian.
Definition: pcm_format.h:37
PcmTraits pcm_format_traits(PcmFormat format)
Get format traits for given PCM format.
Root namespace.
Commonly used types and functions.
PCM format meta-information.
Definition: pcm_format.h:201
bool is_signed
True for signed integers and floating point.
Definition: pcm_format.h:209
size_t bit_width
Number of stored bits per sample in packed form.
Definition: pcm_format.h:221
size_t bit_depth
Number of significant bits per sample.
Definition: pcm_format.h:218
bool is_integer
True for integers, false for floating point.
Definition: pcm_format.h:206
PcmFormat canon_id
Canonical identifier of the same format.
Definition: pcm_format.h:215
bool is_little
True for little-endian formats.
Definition: pcm_format.h:212
bool is_valid
If traits are valid.
Definition: pcm_format.h:203