Debian Codegraph @

Source/Core/AudioCommon/SurroundDecoder.cpp

raw symbols SCIP
1 // Copyright 2017 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "AudioCommon/SurroundDecoder.h"
5
6 #include <FreeSurround/FreeSurroundDecoder.h>
7 #include <limits>
8
9 namespace AudioCommon
10 {
11 constexpr size_t STEREO_CHANNELS = 2;
12 constexpr size_t SURROUND_CHANNELS = 6;
13
14 SurroundDecoder::SurroundDecoder(u32 sample_rate, u32 frame_block_size)
15 : m_sample_rate(sample_rate), m_frame_block_size(frame_block_size)
16 {
17 m_fsdecoder = std::make_unique<DPL2FSDecoder>();
18 m_fsdecoder->Init(cs_5point1, m_frame_block_size, m_sample_rate);
19 }
20
21 SurroundDecoder::~SurroundDecoder() = default;
22
23 void SurroundDecoder::Clear()
24 {
25 m_fsdecoder->flush();
26 m_decoded_fifo.clear();
27 }
28
29 // Currently only 6 channels are supported.
30 size_t SurroundDecoder::QueryFramesNeededForSurroundOutput(const size_t output_frames) const
31 {
32 if (m_decoded_fifo.size() < output_frames * SURROUND_CHANNELS)
33 {
34 // Output stereo frames needed to have at least the desired number of surround frames
35 const size_t frames_needed = output_frames - m_decoded_fifo.size() / SURROUND_CHANNELS;
36 return frames_needed + m_frame_block_size - frames_needed % m_frame_block_size;
37 }
38
39 return 0;
40 }
41
42 // Receive and decode samples
43 void SurroundDecoder::PutFrames(const short* in, const size_t num_frames_in)
44 {
45 // Maybe check if it is really power-of-2?
46 s64 remaining_frames = static_cast<s64>(num_frames_in);
47 size_t frame_index = 0;
48
49 while (remaining_frames > 0)
50 {
51 // Convert to float
52 for (size_t i = 0, end = m_frame_block_size * STEREO_CHANNELS; i < end; ++i)
53 {
54 m_float_conversion_buffer[i] = in[i + frame_index * STEREO_CHANNELS] /
55 static_cast<float>(std::numeric_limits<short>::max());
56 }
57
58 // Decode
59 const float* dpl2_fs = m_fsdecoder->decode(m_float_conversion_buffer.data());
60
61 // Add to ring buffer and fix channel mapping
62 // Maybe modify FreeSurround to output the correct mapping?
63 // FreeSurround:
64 // FL | FC | FR | BL | BR | LFE
65 // Most backends:
66 // FL | FR | FC | LFE | BL | BR
67 for (size_t i = 0; i < m_frame_block_size; ++i)
68 {
69 m_decoded_fifo.push(dpl2_fs[i * SURROUND_CHANNELS + 0]); // LEFTFRONT
70 m_decoded_fifo.push(dpl2_fs[i * SURROUND_CHANNELS + 2]); // RIGHTFRONT
71 m_decoded_fifo.push(dpl2_fs[i * SURROUND_CHANNELS + 1]); // CENTREFRONT
72 m_decoded_fifo.push(dpl2_fs[i * SURROUND_CHANNELS + 5]); // sub/lfe
73 m_decoded_fifo.push(dpl2_fs[i * SURROUND_CHANNELS + 3]); // LEFTREAR
74 m_decoded_fifo.push(dpl2_fs[i * SURROUND_CHANNELS + 4]); // RIGHTREAR
75 }
76
77 remaining_frames = remaining_frames - static_cast<int>(m_frame_block_size);
78 frame_index = frame_index + m_frame_block_size;
79 }
80 }
81
82 void SurroundDecoder::ReceiveFrames(float* out, const size_t num_frames_out)
83 {
84 // Copy to output array with desired num_frames_out
85 for (size_t i = 0, num_samples_output = num_frames_out * SURROUND_CHANNELS;
86 i < num_samples_output; ++i)
87 {
88 out[i] = m_decoded_fifo.pop_front();
89 }
90 }
91
92 } // namespace AudioCommon