Skip to content

Commit 401ee64

Browse files
committed
Use swr_alloc_set_opts2 function to set SwrContext options
1 parent 648b167 commit 401ee64

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

libraries/decoder_ffmpeg/src/main/jni/ffmpeg_jni.cc

+18-13
Original file line numberDiff line numberDiff line change
@@ -342,19 +342,24 @@ int decodePacket(AVCodecContext *context, AVPacket *packet,
342342
int sampleCount = frame->nb_samples;
343343
int dataSize = av_samples_get_buffer_size(NULL, channelCount, sampleCount,
344344
sampleFormat, 1);
345-
SwrContext *resampleContext;
346-
if (context->opaque) {
347-
resampleContext = (SwrContext *)context->opaque;
348-
} else {
349-
resampleContext = swr_alloc();
350-
av_opt_set_chlayout(resampleContext, "in_chlayout", &context->ch_layout, 0);
351-
av_opt_set_chlayout(resampleContext, "out_chlayout", &context->ch_layout, 0);
352-
av_opt_set_int(resampleContext, "in_sample_rate", sampleRate, 0);
353-
av_opt_set_int(resampleContext, "out_sample_rate", sampleRate, 0);
354-
av_opt_set_int(resampleContext, "in_sample_fmt", sampleFormat, 0);
355-
// The output format is always the requested format.
356-
av_opt_set_int(resampleContext, "out_sample_fmt",
357-
context->request_sample_fmt, 0);
345+
SwrContext *resampleContext = static_cast<SwrContext *>(context->opaque);
346+
if (!resampleContext) {
347+
result = swr_alloc_set_opts2(
348+
&resampleContext, // ps
349+
&context->ch_layout, // out_ch_layout
350+
context->request_sample_fmt, // out_sample_fmt
351+
sampleRate, // out_sample_rate
352+
&context->ch_layout, // in_ch_layout
353+
sampleFormat, // in_sample_fmt
354+
sampleRate, // in_sample_rate
355+
0, // log_offset
356+
NULL // log_ctx
357+
);
358+
if (result < 0) {
359+
logError("swr_alloc_set_opts2", result);
360+
av_frame_free(&frame);
361+
return transformError(result);
362+
}
358363
result = swr_init(resampleContext);
359364
if (result < 0) {
360365
logError("swr_init", result);

0 commit comments

Comments
 (0)