Guest User

Untitled

a guest
Jun 13th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.98 KB | None | 0 0
  1. /*
  2. To resample audio frame by frame of 1.5 sec duration to frequency 5512 Hz
  3. */
  4. void resampling(AVFrame *frame){ //decoded frame from avcodec_decode_audio4()
  5.   //refer to article https://www.ffmpeg.org/doxygen/2.3/group__lswr.html                                                                                                  
  6.   int ret,in_count,out_count,i,src_nb_channels=0,dst_nb_channels=0;
  7.   int src_linesize=0,dst_linesize;
  8.   int src_nb_samples = 1024, dst_nb_samples;
  9.   enum AVSampleFormat src_sample_fmt;
  10.   src_sample_fmt = dec_ctx->sample_fmt;
  11.  
  12.   uint8_t *out=NULL,**in=NULL;
  13.   char err[128];
  14.   in = frame->data;
  15.   in_count = frame->nb_samples;
  16.   printf("Sample rate:%d\tNumber of samples:%d\n",frame->sample_rate,frame->nb_samples);
  17.  
  18.   if(fmt_ctx == NULL){
  19.     printf("No format context present(null)\n");
  20.     return;
  21.   }
  22.   swr = swr_alloc();  
  23. if(swr == NULL){
  24.     printf("SWR context not allocted\n");
  25.     return;
  26.   }
  27.   av_opt_set_channel_layout(swr, "in_channel_layout", frame->channel_layout, 0);
  28.   av_opt_set_channel_layout(swr, "out_channel_layout", AV_CH_LAYOUT_MONO, 0);
  29.   av_opt_set_int(swr, "in_sample_rate",frame->sample_rate, 0);
  30.   av_opt_set_int(swr, "out_sample_rate", 5512, 0);
  31.   av_opt_set_sample_fmt(swr, "in_sample_fmt", src_sample_fmt, 0);
  32.   av_opt_set_sample_fmt(swr, "out_sample_fmt", AV_SAMPLE_FMT_S16P, 0);
  33.   ret = swr_init(swr);
  34.   if(ret < 0){
  35.     printf("SWR not initialised\n");
  36.     return;
  37.   }
  38.  
  39.   out_count = av_rescale_rnd(swr_get_delay(swr, frame->sample_rate) + frame->nb_samples, 5512, frame->sample_rate, AV_ROUND_UP);
  40.   printf("Number of samples of output:%d\n",out_count);
  41.   av_samples_alloc(&out, NULL, 2, out_count, AV_SAMPLE_FMT_S16, 0);
  42.   ret = swr_convert(swr, &out, out_count,(const uint8_t **) in, in_count);
  43.   if( ret < 0){
  44.     printf("Not successfull swr_convert returned: %d\n",ret);
  45.     return;
  46.   }
  47.   for(i = 0; i < out_count; i++)
  48.     printf("in:%d\nout:%d\n",in[i],out[i]);
  49.   swr_free(&swr);
  50.   free(out);
  51.   exit(0);
  52. }
Add Comment
Please, Sign In to add comment