Debian Codegraph @

src/mpt/audio/span.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_AUDIO_SPAN_HPP
4 #define MPT_AUDIO_SPAN_HPP
5
6
7
8 #include "mpt/base/macros.hpp"
9 #include "mpt/base/namespace.hpp"
10
11 #include <utility>
12 #include <variant>
13
14 #include <cassert>
15 #include <cstddef>
16
17
18
19 namespace mpt {
20 inline namespace MPT_INLINE_NS {
21
22
23
24 // LRLRLRLRLR
25
26 template <typename SampleType>
27 struct audio_span_interleaved {
28 public:
29 using sample_type = SampleType;
30
31 private:
32 sample_type * const m_buffer;
33 std::size_t m_channels;
34 std::size_t m_frames;
35
36 public:
37 MPT_CONSTEXPRINLINE audio_span_interleaved(sample_type * buffer, std::size_t channels, std::size_t frames) noexcept
38 : m_buffer(buffer)
39 , m_channels(channels)
40 , m_frames(frames) {
41 return;
42 }
43 MPT_FORCEINLINE sample_type * const * data_planar() const noexcept {
44 return nullptr;
45 }
46 MPT_FORCEINLINE sample_type * data() const noexcept {
47 return m_buffer;
48 }
49 MPT_FORCEINLINE sample_type & operator()(std::size_t channel, std::size_t frame) const {
50 return m_buffer[m_channels * frame + channel];
51 }
52 MPT_FORCEINLINE bool is_contiguous() const noexcept {
53 return true;
54 }
55 MPT_FORCEINLINE bool channels_are_contiguous() const noexcept {
56 return false;
57 }
58 MPT_FORCEINLINE bool frames_are_contiguous() const noexcept {
59 return true;
60 }
61 MPT_FORCEINLINE std::size_t size_channels() const noexcept {
62 return m_channels;
63 }
64 MPT_FORCEINLINE std::size_t size_frames() const noexcept {
65 return m_frames;
66 }
67 MPT_FORCEINLINE std::size_t size_samples() const noexcept {
68 return m_channels * m_frames;
69 }
70 MPT_FORCEINLINE std::ptrdiff_t frame_stride() const noexcept {
71 return m_channels;
72 }
73 };
74
75 struct audio_span_frames_are_contiguous_t {
76 };
77
78 inline constexpr audio_span_frames_are_contiguous_t audio_span_frames_are_contiguous;
79
80
81
82 // LLLLLRRRRR
83
84 template <typename SampleType>
85 struct audio_span_contiguous {
86 public:
87 using sample_type = SampleType;
88
89 private:
90 sample_type * const m_buffer;
91 std::size_t m_channels;
92 std::size_t m_frames;
93
94 public:
95 MPT_CONSTEXPRINLINE audio_span_contiguous(sample_type * buffer, std::size_t channels, std::size_t frames) noexcept
96 : m_buffer(buffer)
97 , m_channels(channels)
98 , m_frames(frames) {
99 return;
100 }
101 MPT_FORCEINLINE sample_type * const * data_planar() const noexcept {
102 return nullptr;
103 }
104 MPT_FORCEINLINE sample_type * data() const noexcept {
105 return m_buffer;
106 }
107 MPT_FORCEINLINE sample_type & operator()(std::size_t channel, std::size_t frame) const {
108 return m_buffer[(m_frames * channel) + frame];
109 }
110 MPT_FORCEINLINE bool is_contiguous() const noexcept {
111 return true;
112 }
113 MPT_FORCEINLINE bool channels_are_contiguous() const noexcept {
114 return true;
115 }
116 MPT_FORCEINLINE bool frames_are_contiguous() const noexcept {
117 return false;
118 }
119 MPT_FORCEINLINE std::size_t size_channels() const noexcept {
120 return m_channels;
121 }
122 MPT_FORCEINLINE std::size_t size_frames() const noexcept {
123 return m_frames;
124 }
125 MPT_FORCEINLINE std::size_t size_samples() const noexcept {
126 return m_channels * m_frames;
127 }
128 MPT_FORCEINLINE std::ptrdiff_t frame_stride() const noexcept {
129 return 1;
130 }
131 };
132
133 struct audio_span_channels_are_contiguous_t {
134 };
135
136 inline constexpr audio_span_channels_are_contiguous_t audio_span_channels_are_contiguous;
137
138
139
140 // LLLLL RRRRR
141
142 template <typename SampleType>
143 struct audio_span_planar {
144 public:
145 using sample_type = SampleType;
146
147 private:
148 sample_type * const * m_buffers;
149 std::size_t m_channels;
150 std::size_t m_frames;
151
152 public:
153 MPT_CONSTEXPRINLINE audio_span_planar(sample_type * const * buffers, std::size_t channels, std::size_t frames) noexcept
154 : m_buffers(buffers)
155 , m_channels(channels)
156 , m_frames(frames) {
157 return;
158 }
159 MPT_FORCEINLINE sample_type * const * data_planar() const noexcept {
160 return m_buffers;
161 }
162 MPT_FORCEINLINE sample_type * data() const noexcept {
163 return nullptr;
164 }
165 MPT_FORCEINLINE sample_type & operator()(std::size_t channel, std::size_t frame) const {
166 return m_buffers[channel][frame];
167 }
168 MPT_FORCEINLINE bool is_contiguous() const noexcept {
169 return false;
170 }
171 MPT_FORCEINLINE bool channels_are_contiguous() const noexcept {
172 return true;
173 }
174 MPT_FORCEINLINE bool frames_are_contiguous() const noexcept {
175 return false;
176 }
177 MPT_FORCEINLINE std::size_t size_channels() const noexcept {
178 return m_channels;
179 }
180 MPT_FORCEINLINE std::size_t size_frames() const noexcept {
181 return m_frames;
182 }
183 MPT_FORCEINLINE std::size_t size_samples() const noexcept {
184 return m_channels * m_frames;
185 }
186 MPT_FORCEINLINE std::ptrdiff_t frame_stride() const noexcept {
187 return 1;
188 }
189 };
190
191 struct audio_span_channels_are_planar_t {
192 };
193
194 inline constexpr audio_span_channels_are_planar_t audio_span_channels_are_planar;
195
196
197
198 // LxxxLxxxLxxxLxxxLxxx xRxxxRxxxRxxxRxxxRxx
199 template <typename SampleType>
200 struct audio_span_planar_strided {
201 public:
202 using sample_type = SampleType;
203
204 private:
205 sample_type * const * m_buffers;
206 std::ptrdiff_t m_frame_stride;
207 std::size_t m_channels;
208 std::size_t m_frames;
209
210 public:
211 MPT_CONSTEXPRINLINE audio_span_planar_strided(sample_type * const * buffers, std::size_t channels, std::size_t frames, std::ptrdiff_t frame_stride) noexcept
212 : m_buffers(buffers)
213 , m_frame_stride(frame_stride)
214 , m_channels(channels)
215 , m_frames(frames) {
216 return;
217 }
218 MPT_FORCEINLINE sample_type * const * data_planar() const noexcept {
219 return m_buffers;
220 }
221 MPT_FORCEINLINE sample_type * data() const noexcept {
222 return nullptr;
223 }
224 MPT_FORCEINLINE sample_type & operator()(std::size_t channel, std::size_t frame) const {
225 return m_buffers[channel][static_cast<std::ptrdiff_t>(frame) * m_frame_stride];
226 }
227 MPT_FORCEINLINE bool is_contiguous() const noexcept {
228 return false;
229 }
230 MPT_FORCEINLINE bool channels_are_contiguous() const noexcept {
231 return false;
232 }
233 MPT_FORCEINLINE bool frames_are_contiguous() const noexcept {
234 return false;
235 }
236 MPT_FORCEINLINE std::size_t size_channels() const noexcept {
237 return m_channels;
238 }
239 MPT_FORCEINLINE std::size_t size_frames() const noexcept {
240 return m_frames;
241 }
242 MPT_FORCEINLINE std::size_t size_samples() const noexcept {
243 return m_channels * m_frames;
244 }
245 MPT_FORCEINLINE std::ptrdiff_t frame_stride() const noexcept {
246 return m_frame_stride;
247 }
248 };
249
250 struct audio_span_channels_are_planar_and_strided_t {
251 };
252
253 inline constexpr audio_span_channels_are_planar_and_strided_t audio_span_channels_are_planar_and_strided;
254
255
256
257 template <typename SampleType>
258 struct audio_span {
259 public:
260 using sample_type = SampleType;
261
262 private:
263 union {
264 sample_type * const contiguous;
265 sample_type * const * const planes;
266 } m_buffer;
267 std::ptrdiff_t m_frame_stride;
268 std::ptrdiff_t m_channel_stride;
269 std::size_t m_channels;
270 std::size_t m_frames;
271
272 public:
273 MPT_CONSTEXPRINLINE audio_span(audio_span_interleaved<sample_type> buffer) noexcept
274 : m_frame_stride(static_cast<std::ptrdiff_t>(buffer.size_channels()))
275 , m_channel_stride(1)
276 , m_channels(buffer.size_channels())
277 , m_frames(buffer.size_frames()) {
278 m_buffer.contiguous = buffer.data();
279 }
280 MPT_CONSTEXPRINLINE audio_span(sample_type * buffer, std::size_t channels, std::size_t frames, audio_span_frames_are_contiguous_t) noexcept
281 : m_frame_stride(static_cast<std::ptrdiff_t>(channels))
282 , m_channel_stride(1)
283 , m_channels(channels)
284 , m_frames(frames) {
285 m_buffer.contiguous = buffer;
286 }
287 MPT_CONSTEXPRINLINE audio_span(audio_span_contiguous<sample_type> buffer) noexcept
288 : m_frame_stride(1)
289 , m_channel_stride(buffer.size_frames())
290 , m_channels(buffer.size_channels())
291 , m_frames(buffer.size_frames()) {
292 m_buffer.contiguous = buffer.data();
293 }
294 MPT_CONSTEXPRINLINE audio_span(sample_type * buffer, std::size_t channels, std::size_t frames, audio_span_channels_are_contiguous_t) noexcept
295 : m_frame_stride(1)
296 , m_channel_stride(static_cast<std::ptrdiff_t>(frames))
297 , m_channels(channels)
298 , m_frames(frames) {
299 m_buffer.contiguous = buffer;
300 }
301 MPT_CONSTEXPRINLINE audio_span(audio_span_planar<sample_type> buffer) noexcept
302 : m_frame_stride(1)
303 , m_channel_stride(0)
304 , m_channels(buffer.size_channels())
305 , m_frames(buffer.size_frames()) {
306 m_buffer.planes = buffer.data_planar();
307 }
308 MPT_CONSTEXPRINLINE audio_span(sample_type * const * planes, std::size_t channels, std::size_t frames, audio_span_channels_are_planar_t) noexcept
309 : m_frame_stride(1)
310 , m_channel_stride(0)
311 , m_channels(channels)
312 , m_frames(frames) {
313 m_buffer.planes = planes;
314 }
315 MPT_CONSTEXPRINLINE audio_span(audio_span_planar_strided<sample_type> buffer) noexcept
316 : m_frame_stride(static_cast<std::ptrdiff_t>(buffer.frame_stride()))
317 , m_channel_stride(0)
318 , m_channels(buffer.size_channels())
319 , m_frames(buffer.size_frames()) {
320 m_buffer.planes = buffer.data_planar();
321 }
322 MPT_CONSTEXPRINLINE audio_span(sample_type * const * planes, std::size_t channels, std::size_t frames, std::ptrdiff_t frame_stride, audio_span_channels_are_planar_and_strided_t) noexcept
323 : m_frame_stride(frame_stride)
324 , m_channel_stride(0)
325 , m_channels(channels)
326 , m_frames(frames) {
327 m_buffer.planes = planes;
328 }
329 MPT_FORCEINLINE bool is_contiguous() const noexcept {
330 return (m_channel_stride != 0);
331 }
332 MPT_FORCEINLINE sample_type * const * data_planar() const noexcept {
333 return (!is_contiguous()) ? m_buffer.planes : nullptr;
334 }
335 MPT_FORCEINLINE sample_type * data() const noexcept {
336 return is_contiguous() ? m_buffer.contiguous : nullptr;
337 }
338 MPT_FORCEINLINE sample_type & operator()(std::size_t channel, std::size_t frame) const {
339 return is_contiguous() ? m_buffer.contiguous[(m_channel_stride * static_cast<std::ptrdiff_t>(channel)) + (m_frame_stride * static_cast<std::ptrdiff_t>(frame))] : m_buffer.planes[channel][frame * static_cast<std::ptrdiff_t>(m_frame_stride)];
340 }
341 MPT_FORCEINLINE bool channels_are_contiguous() const noexcept {
342 return (m_frame_stride == 1);
343 }
344 MPT_FORCEINLINE bool frames_are_contiguous() const noexcept {
345 return (m_channel_stride == 1);
346 }
347 MPT_FORCEINLINE std::size_t size_channels() const noexcept {
348 return m_channels;
349 }
350 MPT_FORCEINLINE std::size_t size_frames() const noexcept {
351 return m_frames;
352 }
353 MPT_FORCEINLINE std::size_t size_samples() const noexcept {
354 return m_channels * m_frames;
355 }
356 MPT_FORCEINLINE std::ptrdiff_t frame_stride() const noexcept {
357 return m_frame_stride;
358 }
359 };
360
361
362
363 template <typename Taudio_span>
364 struct audio_span_with_offset {
365 public:
366 using sample_type = typename Taudio_span::sample_type;
367
368 private:
369 Taudio_span m_buffer;
370 std::size_t m_offset;
371
372 public:
373 MPT_CONSTEXPRINLINE audio_span_with_offset(Taudio_span buffer, std::size_t offsetFrames) noexcept
374 : m_buffer(buffer)
375 , m_offset(offsetFrames) {
376 return;
377 }
378 MPT_FORCEINLINE sample_type * const * data_planar() const noexcept {
379 return nullptr;
380 }
381 MPT_FORCEINLINE sample_type * data() const noexcept {
382 if (!is_contiguous()) {
383 return nullptr;
384 }
385 return m_buffer.data() + (size_channels() * m_offset);
386 }
387 MPT_FORCEINLINE sample_type & operator()(std::size_t channel, std::size_t frame) const {
388 return m_buffer(channel, m_offset + frame);
389 }
390 MPT_FORCEINLINE bool is_contiguous() const noexcept {
391 return m_buffer.is_contiguous() && m_buffer.frames_are_contiguous();
392 }
393 MPT_FORCEINLINE bool channels_are_contiguous() const noexcept {
394 return m_buffer.channels_are_contiguous();
395 }
396 MPT_FORCEINLINE bool frames_are_contiguous() const noexcept {
397 return m_buffer.frames_are_contiguous();
398 }
399 MPT_FORCEINLINE std::size_t size_channels() const noexcept {
400 return m_buffer.size_channels();
401 }
402 MPT_FORCEINLINE std::size_t size_frames() const noexcept {
403 return m_buffer.size_frames() - m_offset;
404 }
405 MPT_FORCEINLINE std::size_t size_samples() const noexcept {
406 return size_channels() * size_frames();
407 }
408 MPT_FORCEINLINE std::ptrdiff_t frame_stride() const noexcept {
409 return m_buffer.frame_stride();
410 }
411 };
412
413
414
415 template <typename Tspan, typename Tfunc>
416 MPT_FORCEINLINE auto audio_span_optimized_visit(Tspan span, Tfunc && f) -> decltype(std::forward<Tfunc>(f)(span)) const {
417 using sample_type = typename Tspan::sample_type;
418 std::variant<Tspan, audio_span_interleaved<sample_type>, audio_span_contiguous<sample_type>, audio_span_planar<sample_type>, audio_span_planar_strided<sample_type>> v{span};
419 if (span.data() && span.is_contiguous() && span.frames_are_contiguous()) {
420 v = audio_span_interleaved<sample_type>{span.data(), span.size_channels(), span.size_frames()};
421 } else if (span.data() && span.is_contiguous() && span.channels_are_contiguous()) {
422 v = audio_span_contiguous<sample_type>{span.data(), span.size_channels(), span.size_frames()};
423 } else if (span.data_planar() && span.channels_are_contiguous()) {
424 v = audio_span_planar<sample_type>{span.data_planar(), span.size_channels(), span.size_frames()};
425 } else if (span.data_planar()) {
426 v = audio_span_planar_strided<sample_type>{span.data_planar(), span.size_channels(), span.size_frames(), span.frame_stride()};
427 }
428 return std::visit(std::forward<Tfunc>(f), v);
429 }
430
431
432
433 template <typename BufferType>
434 inline audio_span_with_offset<BufferType> make_audio_span_with_offset(BufferType buf, std::size_t offsetFrames) noexcept {
435 assert(offsetFrames <= buf.size_frames());
436 return audio_span_with_offset<BufferType>{buf, offsetFrames};
437 }
438
439
440
441 } // namespace MPT_INLINE_NS
442 } // namespace mpt
443
444
445
446 #endif // MPT_AUDIO_SPAN_HPP