|
| 1 | +#include <benchmark/benchmark.h> |
| 2 | +#include "roc_audio/pcm_encoder.h" |
| 3 | +#include "roc_audio/pcm_decoder.h" |
| 4 | +#include "roc_audio/sample_spec.h" |
| 5 | +#include "roc_audio/channel_set.h" |
| 6 | +#include "roc_audio/channel_defs.h" |
| 7 | +#include <vector> |
| 8 | +#include <cstdint> |
| 9 | + |
| 10 | +using namespace roc::audio; |
| 11 | + |
| 12 | +static void bench_pcm_encoder(benchmark::State& s) { |
| 13 | + ChannelSet channels( |
| 14 | + ChanLayout_Surround, |
| 15 | + ChanOrder_Smpte, |
| 16 | + ChanMask_Surround_Stereo |
| 17 | + ); |
| 18 | + |
| 19 | + SampleSpec spec( |
| 20 | + 48000, |
| 21 | + PcmFormat_Float32, |
| 22 | + channels |
| 23 | + ); |
| 24 | + |
| 25 | + PcmEncoder enc(spec); |
| 26 | + |
| 27 | + const size_t samples_per_channel = 48000; |
| 28 | + const size_t num_channels = channels.num_channels(); |
| 29 | + const size_t total_samples = samples_per_channel * num_channels; |
| 30 | + |
| 31 | + std::vector<float> samples(total_samples, 0.0f); |
| 32 | + |
| 33 | + // encoded_byte_count expects samples per channel |
| 34 | + size_t frame_size = enc.encoded_byte_count(samples_per_channel); |
| 35 | + std::vector<uint8_t> frame(frame_size); |
| 36 | + |
| 37 | + for (auto _ : s) { |
| 38 | + enc.begin(frame.data(), frame_size); |
| 39 | + enc.write(samples.data(), total_samples); |
| 40 | + enc.end(); |
| 41 | + } |
| 42 | + |
| 43 | + s.SetItemsProcessed(int64_t(s.iterations()) * int64_t(total_samples)); |
| 44 | +} |
| 45 | + |
| 46 | +static void bench_pcm_decoder(benchmark::State& s) { |
| 47 | + ChannelSet channels( |
| 48 | + ChanLayout_Surround, |
| 49 | + ChanOrder_Smpte, |
| 50 | + ChanMask_Surround_Stereo |
| 51 | + ); |
| 52 | + |
| 53 | + SampleSpec spec( |
| 54 | + 48000, |
| 55 | + PcmFormat_Float32, |
| 56 | + channels |
| 57 | + ); |
| 58 | + |
| 59 | + PcmDecoder dec(spec); |
| 60 | + |
| 61 | + const size_t samples_per_channel = 48000; |
| 62 | + const size_t num_channels = channels.num_channels(); |
| 63 | + const size_t total_samples = samples_per_channel * num_channels; |
| 64 | + |
| 65 | + std::vector<float> samples(total_samples, 0.0f); |
| 66 | + |
| 67 | + // For PCM float32 encoded frame size (bytes) = samples_per_channel * num_channels * sizeof(float) |
| 68 | + size_t frame_size = samples_per_channel * num_channels * sizeof(float); |
| 69 | + std::vector<uint8_t> frame(frame_size); |
| 70 | + |
| 71 | + for (auto _ : s) { |
| 72 | + dec.begin(0, frame.data(), frame.size()); |
| 73 | + dec.read(samples.data(), total_samples); |
| 74 | + dec.end(); |
| 75 | + } |
| 76 | + |
| 77 | + s.SetItemsProcessed(int64_t(s.iterations()) * int64_t(total_samples)); |
| 78 | +} |
| 79 | + |
| 80 | +BENCHMARK(bench_pcm_encoder); |
| 81 | +BENCHMARK(bench_pcm_decoder); |
0 commit comments