Skip to content

Commit 0ceb938

Browse files
committed
deque much more efficient if buffer ever >> frame_size, ~ same performance for defaults
1 parent db8a736 commit 0ceb938

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

system/loggerd/video_writer.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,11 @@ void VideoWriter::write_audio(uint8_t *data, int len, long long timestamp) {
151151
// convert s16le samples to fltp and add to buffer
152152
const int16_t *raw_samples = reinterpret_cast<const int16_t*>(data);
153153
int sample_count = len / sizeof(int16_t);
154-
audio_buffer.reserve(audio_buffer.size() + sample_count);
155154
constexpr float normalizer = 1.0f / 32768.0f;
156-
std::transform(raw_samples, raw_samples + sample_count, std::back_inserter(audio_buffer),
157-
[](int16_t sample) {
158-
return sample * normalizer;
159-
});
155+
const size_t original_size = audio_buffer.size();
156+
audio_buffer.resize(original_size + sample_count);
157+
std::transform(raw_samples, raw_samples + sample_count, audio_buffer.begin() + original_size,
158+
[](int16_t sample) { return sample * normalizer; });
160159

161160
while (audio_buffer.size() >= audio_codec_ctx->frame_size) {
162161
audio_frame->pts = next_audio_pts;

system/loggerd/video_writer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
#include <string>
4-
#include <vector>
4+
#include <deque>
55

66
extern "C" {
77
#include <libavformat/avformat.h>
@@ -30,7 +30,7 @@ class VideoWriter {
3030
AVFrame *audio_frame = nullptr;
3131
uint64_t next_audio_pts = 0;
3232
uint64_t first_audio_timestamp = 0;
33-
std::vector<float> audio_buffer;
33+
std::deque<float> audio_buffer;
3434

3535
bool remuxing;
3636
};

0 commit comments

Comments
 (0)