Skip to content

Commit fa0c457

Browse files
committed
Use swr_alloc_set_opts2 function to set SwrContext options
1 parent fb63e64 commit fa0c457

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
@@ -301,19 +301,24 @@ int decodePacket(AVCodecContext *context, AVPacket *packet,
301301
int sampleCount = frame->nb_samples;
302302
int dataSize = av_samples_get_buffer_size(NULL, channelCount, sampleCount,
303303
sampleFormat, 1);
304-
SwrContext *resampleContext;
305-
if (context->opaque) {
306-
resampleContext = (SwrContext *)context->opaque;
307-
} else {
308-
resampleContext = swr_alloc();
309-
av_opt_set_chlayout(resampleContext, "in_chlayout", &context->ch_layout, 0);
310-
av_opt_set_chlayout(resampleContext, "out_chlayout", &context->ch_layout, 0);
311-
av_opt_set_int(resampleContext, "in_sample_rate", sampleRate, 0);
312-
av_opt_set_int(resampleContext, "out_sample_rate", sampleRate, 0);
313-
av_opt_set_int(resampleContext, "in_sample_fmt", sampleFormat, 0);
314-
// The output format is always the requested format.
315-
av_opt_set_int(resampleContext, "out_sample_fmt",
316-
context->request_sample_fmt, 0);
304+
SwrContext *resampleContext = static_cast<SwrContext *>(context->opaque);
305+
if (!resampleContext) {
306+
result = swr_alloc_set_opts2(
307+
&resampleContext, // ps
308+
&context->ch_layout, // out_ch_layout
309+
context->request_sample_fmt, // out_sample_fmt
310+
sampleRate, // out_sample_rate
311+
&context->ch_layout, // in_ch_layout
312+
sampleFormat, // in_sample_fmt
313+
sampleRate, // in_sample_rate
314+
0, // log_offset
315+
NULL // log_ctx
316+
);
317+
if (result < 0) {
318+
logError("swr_alloc_set_opts2", result);
319+
av_frame_free(&frame);
320+
return transformError(result);
321+
}
317322
result = swr_init(resampleContext);
318323
if (result < 0) {
319324
logError("swr_init", result);

0 commit comments

Comments
 (0)