Skip to content

Commit

Permalink
Change SWR initialization
Browse files Browse the repository at this point in the history
The other function lead to segmentation faults on
linux. I also tested it with asan and it was not
happy about it.
  • Loading branch information
dmorn committed Nov 13, 2023
1 parent 0327ccb commit f0bd8db
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions c_src/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "libavutil/samplefmt.h"
#include "libswresample/swresample.h"
#include <decoder.h>
#include <libavutil/opt.h>

int is_planar(enum AVSampleFormat fmt) {
switch (fmt) {
Expand All @@ -20,18 +21,27 @@ int is_planar(enum AVSampleFormat fmt) {

int alloc_resampler(Decoder *ctx) {
AVCodecContext *codec_ctx;
SwrContext *swr_ctx;
int errn;

codec_ctx = ctx->codec_ctx;
if (!(swr_ctx = swr_alloc()))
return AVERROR(ENOMEM);

av_opt_set_chlayout(swr_ctx, "in_chlayout", &codec_ctx->ch_layout, 0);
av_opt_set_int(swr_ctx, "in_sample_rate", codec_ctx->sample_rate, 0);
av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", codec_ctx->sample_fmt, 0);

av_opt_set_chlayout(swr_ctx, "out_chlayout", ctx->output.ch_layout, 0);
av_opt_set_int(swr_ctx, "out_sample_rate", ctx->output.sample_rate, 0);
av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", ctx->output.sample_format,
0);

if ((errn = swr_alloc_set_opts2(&(ctx->resampler_ctx), ctx->output.ch_layout,
ctx->output.sample_format,
ctx->output.sample_rate,
&codec_ctx->ch_layout, codec_ctx->sample_fmt,
codec_ctx->sample_rate, 0, NULL)) != 0)
if ((errn = swr_init(swr_ctx)) < 0)
return errn;

return swr_init(ctx->resampler_ctx);
ctx->resampler_ctx = swr_ctx;
return errn;
}

int decoder_alloc(Decoder **ctx, DecoderOpts opts) {
Expand Down

0 comments on commit f0bd8db

Please sign in to comment.