@@ -30,7 +30,7 @@ int audio_to_pcm(uint8_t* audio_data, int data_len, cb_codec callback, void* use
3030 }
3131
3232 printf (" DEBUG: number of streams found: %d\n " , format_context->nb_streams );
33- int stream_index = av_find_best_stream (format_context, AVMEDIA_TYPE_AUDIO,-1 , -1 , nullptr , 0 );
33+ const int stream_index = av_find_best_stream (format_context, AVMEDIA_TYPE_AUDIO, -1 , -1 , nullptr , 0 );
3434 if (stream_index < 0 ) {
3535 fprintf (stderr, " ERROR: no audio stream found\n " );
3636 return -1 ;
@@ -53,7 +53,9 @@ int audio_to_pcm(uint8_t* audio_data, int data_len, cb_codec callback, void* use
5353 fprintf (stderr, " ERROR: failed to open the decoder\n " );
5454 return -1 ;
5555 }
56-
56+ if (decoder_ctx->channel_layout == 0 ) {
57+ decoder_ctx->channel_layout = av_get_default_channel_layout (decoder_ctx->channels );
58+ }
5759 printf (
5860 " DEBUG: Setting up decoder - sample format: %s, sample rate: %d Hz, "
5961 " channels: %d \n " ,
@@ -64,31 +66,38 @@ int audio_to_pcm(uint8_t* audio_data, int data_len, cb_codec callback, void* use
6466 AVPacket *packet = av_packet_alloc ();
6567 AVFrame *frame = av_frame_alloc ();
6668
67- SwrContext *swr_context = swr_alloc_set_opts (nullptr ,
68- AV_CH_LAYOUT_MONO, AV_SAMPLE_FMT_S16,24000 ,
69- stream->codecpar ->channel_layout ,static_cast <AVSampleFormat>(stream->codecpar ->format ), stream->codecpar ->sample_rate ,
70- 0 , nullptr );
69+ SwrContext *swr_context = swr_alloc_set_opts (
70+ nullptr ,
71+ AV_CH_LAYOUT_MONO,
72+ AV_SAMPLE_FMT_S16,
73+ 24000 ,
74+ av_get_default_channel_layout (decoder_ctx->channels ),
75+ decoder_ctx->sample_fmt ,
76+ decoder_ctx->sample_rate ,
77+ 0 ,
78+ nullptr
79+ );
7180
72- while (av_read_frame (format_context, packet) == 0 ) {
73- if (packet->stream_index != stream_index) continue ;
81+ ret = swr_init (swr_context);
7482
75- ret = avcodec_send_packet (decoder_ctx, packet);
76- if (ret < 0 && (ret != AVERROR (EAGAIN))) {
77- fprintf (stderr, " ERROR: failed to decode a frame\n " );
83+ while (av_read_frame (format_context, packet) == 0 ) {
84+ if (packet->stream_index != stream_index) {
85+ av_packet_unref (packet);
86+ continue ;
7887 }
88+ ret = avcodec_send_packet (decoder_ctx, packet);
7989 while ((ret = avcodec_receive_frame (decoder_ctx, frame)) == 0 ) {
80- AVFrame *resampled_frame = av_frame_alloc ();
81- resampled_frame->sample_rate = 24000 ;
82- resampled_frame->channel_layout = AV_CH_LAYOUT_MONO;
83- resampled_frame->channels = 1 ;
84- resampled_frame->format = AV_SAMPLE_FMT_S16;
85-
86- ret = swr_convert_frame (swr_context, resampled_frame, frame);
87-
88- callback (userdata, resampled_frame->data [0 ], resampled_frame->nb_samples * resampled_frame->channels * 2 );
90+ AVFrame *out = av_frame_alloc ();
91+ out->sample_rate = 24000 ;
92+ out->channel_layout = AV_CH_LAYOUT_MONO;
93+ out->channels = 1 ;
94+ out->format = AV_SAMPLE_FMT_S16;
95+ ret = swr_convert_frame (swr_context, out, frame);
96+ callback (userdata, out->data [0 ], out->nb_samples * out->channels * 2 );
8997 av_frame_unref (frame);
90- av_frame_free (&resampled_frame );
98+ av_frame_free (&out );
9199 }
100+ av_packet_unref (packet);
92101 }
93102
94103 swr_free (&swr_context);
0 commit comments