Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- To resample audio frame by frame of 1.5 sec duration to frequency 5512 Hz
- */
- void resampling(AVFrame *frame){ //decoded frame from avcodec_decode_audio4()
- //refer to article https://www.ffmpeg.org/doxygen/2.3/group__lswr.html
- int ret,in_count,out_count,i,src_nb_channels=0,dst_nb_channels=0;
- int src_linesize=0,dst_linesize;
- int src_nb_samples = 1024, dst_nb_samples;
- enum AVSampleFormat src_sample_fmt;
- src_sample_fmt = dec_ctx->sample_fmt;
- uint8_t *out=NULL,**in=NULL;
- char err[128];
- in = frame->data;
- in_count = frame->nb_samples;
- printf("Sample rate:%d\tNumber of samples:%d\n",frame->sample_rate,frame->nb_samples);
- if(fmt_ctx == NULL){
- printf("No format context present(null)\n");
- return;
- }
- swr = swr_alloc();
- if(swr == NULL){
- printf("SWR context not allocted\n");
- return;
- }
- av_opt_set_channel_layout(swr, "in_channel_layout", frame->channel_layout, 0);
- av_opt_set_channel_layout(swr, "out_channel_layout", AV_CH_LAYOUT_MONO, 0);
- av_opt_set_int(swr, "in_sample_rate",frame->sample_rate, 0);
- av_opt_set_int(swr, "out_sample_rate", 5512, 0);
- av_opt_set_sample_fmt(swr, "in_sample_fmt", src_sample_fmt, 0);
- av_opt_set_sample_fmt(swr, "out_sample_fmt", AV_SAMPLE_FMT_S16P, 0);
- ret = swr_init(swr);
- if(ret < 0){
- printf("SWR not initialised\n");
- return;
- }
- out_count = av_rescale_rnd(swr_get_delay(swr, frame->sample_rate) + frame->nb_samples, 5512, frame->sample_rate, AV_ROUND_UP);
- printf("Number of samples of output:%d\n",out_count);
- av_samples_alloc(&out, NULL, 2, out_count, AV_SAMPLE_FMT_S16, 0);
- ret = swr_convert(swr, &out, out_count,(const uint8_t **) in, in_count);
- if( ret < 0){
- printf("Not successfull swr_convert returned: %d\n",ret);
- return;
- }
- for(i = 0; i < out_count; i++)
- printf("in:%d\nout:%d\n",in[i],out[i]);
- swr_free(&swr);
- free(out);
- exit(0);
- }
Add Comment
Please, Sign In to add comment