Skip to content

Commit aee0583

Browse files
committed
postprocess: fix preallocating audio_frame2
to 6 seconds - it was thought so always but written in a wrong way
1 parent 265bb56 commit aee0583

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

src/audio/postprocess.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ decode_audio_frame_postprocess(struct state_audio_postprocess *postprocess,
426426
audio_frame2_get_channel_count(decompressed), AC_PCM,
427427
audio_frame2_get_bps(decompressed),
428428
audio_frame2_get_sample_rate(decompressed));
429+
audio_frame2_reserve(postprocess->decoded, 6);
429430
}
430431
audio_frame2_append(postprocess->decoded, decompressed);
431432

@@ -455,6 +456,7 @@ decode_audio_frame_postprocess(struct state_audio_postprocess *postprocess,
455456
audio_frame2_get_channel_count(decompressed), AC_PCM,
456457
audio_frame2_get_bps(decompressed),
457458
audio_frame2_get_sample_rate(decompressed));
459+
audio_frame2_reserve(d->frame, 6);
458460
SWAP_PTR(d->frame, postprocess->decoded);
459461
d->seconds = NS_TO_SEC_DBL(postprocess->t0 - t);
460462
d->bytes_received = *received_bytes_cum;

src/audio/types.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,10 @@ void audio_frame2::replace(int channel, size_t offset, const char *data, size_t
174174
/**
175175
* Reserves data for every channel with the specified length.
176176
*/
177-
void audio_frame2::reserve(size_t length)
177+
void audio_frame2::reserve(size_t bytes)
178178
{
179179
for (size_t channel = 0; channel < channels.size(); ++channel) {
180-
reserve(channel, length);
180+
reserve(channel, bytes);
181181
}
182182
}
183183

@@ -450,7 +450,6 @@ audio_frame2_alloc(int ch_count, audio_codec_t codec, int bps, int sample_rate)
450450

451451
auto *ret = new audio_frame2();
452452
ret->init(ch_count, codec, bps, sample_rate);
453-
ret->reserve(bps * ch_count * 6); ///< @todo
454453
return ret;
455454
}
456455

@@ -559,6 +558,15 @@ audio_frame2_replace(struct audio_frame2 *dst, struct audio_frame2 **src)
559558
src = nullptr;
560559
}
561560

561+
void
562+
audio_frame2_reserve(struct audio_frame2 *frame, double seconds)
563+
{
564+
int bytes =
565+
(int) (seconds * frame->get_sample_rate()) * frame->get_bps();
566+
567+
frame->reserve(bytes);
568+
}
569+
562570
struct audio_frame2_resampler *
563571
audio_frame2_resampler_init()
564572
{

src/audio/types.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ struct audio_frame2
159159
void append(int channel, const char *data, size_t length);
160160
void append(audio_frame const &frame);
161161
void replace(int channel, size_t offset, const char *data, size_t length);
162-
void reserve(size_t len);
162+
void reserve(size_t bytes);
163163
void resize(int channel, size_t len);
164164
void reset();
165165
void set_timestamp(int64_t ts);
@@ -250,6 +250,8 @@ int audio_frame2_get_sample_count(const struct audio_frame2 *frame);
250250
int audio_frame2_get_sample_rate(const struct audio_frame2 *frame);
251251
int64_t audio_frame2_get_timestamp(const struct audio_frame2 *frame);
252252
void audio_frame2_replace(struct audio_frame2 *dst, struct audio_frame2 **src);
253+
void audio_frame2_reserve(struct audio_frame2 *frame, double seconds);
254+
253255
struct audio_frame2_resampler *audio_frame2_resampler_init();
254256
void delete_resampler(struct audio_frame2_resampler *resampler);
255257
bool audio_frame2_resample(struct audio_frame2_resampler *resampler,

0 commit comments

Comments
 (0)