I am trying to decode an wma audio stream to PCM 16 in Wave container by
following this code

http://www.mail-archive.com/[email protected]/msg04404.html

However it runs without any error but the end result i.e. WAV file is
missing some frames as far as I can diagnose.

I have attached my sample code file with this.

Any help will be great.

Thank You 


#include <stdio.h>
#include "libmms/mmsx.h"
#include "libmms/mms.h"
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"



#define MAX_AUDIO_PACKET_SIZE (128 * 1024)
const char *url = "mms://live.cumulusstreaming.com/KPLX-FM";

void my_log_callback(void *ptr, int level, const char *fmt, va_list vargs)
{
  printf(fmt, vargs);
}

int64_t seek_data(void *opaque, int64_t offset, int whence)
{
  printf("SEEK DATA\n");
  return -1;
}

static AVStream *add_audio_stream(AVFormatContext *oc, int codec_id, 
AVCodecContext *pCodecCtx)
{
    AVCodecContext *c;
    AVStream *st;

    st = av_new_stream(oc, 0);
    if (!st) 
    {
        printf("Could not alloc new stream\n");
    }
    c = st->codec;
    c->codec_id = codec_id;
    c->codec_type = CODEC_TYPE_AUDIO;
    c->bit_rate = 64000;
    c->sample_rate = pCodecCtx->sample_rate;
    c->channels = pCodecCtx->channels;
    return st;
}

int main(int argc, char *argv[])
{
  mmsx_t *this = NULL;
  mmsx_t *this2 = NULL;
  char buf[2048];
  char buf2[2048];
  int i, res, audiostream;
  FILE* f;
  AVFormatContext* pFormatCtx;

  av_log_set_callback(my_log_callback);
  av_log_set_level(AV_LOG_VERBOSE);


  av_register_all();

  int read_data(void *opaque, char *buf, int buf_size);  
  //static AVStream *add_audio_stream(AVFormatContext *oc, int codec_id, 
AVCodecContext *pCodecCtx);

  AVInputFormat* pAVInputFormat = av_find_input_format("asf");
  if(!pAVInputFormat) 
  { 
    printf("Probe not successful\n"); 
  }
  else
  {
    printf("Probe successfull------%s-------%s\n",pAVInputFormat->name, 
pAVInputFormat->long_name);
  }

  if((this = mmsx_connect(NULL, NULL, url, 1)))
    printf("Connect OK\n");

  //pAVInputFormat->flags |= AVFMT_NOFILE;

  ByteIOContext ByteIOCtx;
  int result = init_put_byte(&ByteIOCtx, buf, 2048, 0, this, read_data, NULL, 
seek_data);


  if(result < 0) 
  { 
    printf("init_put_byte not successful\n"); 
  }
  else
  {
    printf("init_put_byte successful\n");
  }
  ByteIOCtx.is_streamed = 1;


  int ires = av_open_input_stream(&pFormatCtx, &ByteIOCtx, "", 
pAVInputFormat,NULL);
  
  if(ires < 0)
  {
    printf("Open input stream not successful %d\n",ires);
  }
  else
  {
    printf("Open input stream successfull %d\n",ires);
  }

  if(av_find_stream_info(pFormatCtx) < 0)
  {
     printf("Could not find stream information\n");
  }
  else
  {
    printf("Stream information found\n");
  }

  printf("Number of streams=%d\n",pFormatCtx->nb_streams);

  audiostream = -1;
  for(i=0; i<pFormatCtx->nb_streams; i++)
  {
    if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO)
    {
      audiostream = i;
      break;
    }
  }

  if(audiostream == -1)
  {
    printf("No audio stream found in URL\n");
    return -1;
  }
  else
  {
    printf("Audio stream found at - %d\n",audiostream);
  }
  
  AVCodecContext *pCodecCtx;
  pCodecCtx = pFormatCtx->streams[audiostream]->codec;
  
  AVCodec *pCodec;
  pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
  if(pCodec == NULL)
  {
    printf("Unsupported codec\n");
  }
  else
  {
    printf("Codec for stream  = %s\n", pCodec->name);
  }
  
  if(avcodec_open(pCodecCtx, pCodec) < 0)
  {
    printf("Open codec not successful\n");
  }
  else
  {
    printf("Open codec successful\n");
  }

  char *wavebuffer;
  int ok;
  ok = posix_memalign(&wavebuffer, 16, 256);
 
  FILE *waveFile;
  char waveFilename[32];
  sprintf(waveFilename, "outmms.wave");
  waveFile=fopen(waveFilename, "wb");
  i=0;
  sprintf(wavebuffer, "RIFF");
  fwrite(wavebuffer,1,4,waveFile);
  fwrite(&i,4,1,waveFile);
  sprintf(wavebuffer, "WAVEfmt ");
  fwrite(wavebuffer,1,8,waveFile);
  i=16;
  fwrite(&i,4,1,waveFile);
  i=1;
  fwrite(&i,2,1,waveFile);
  i=2; //number of channels
  fwrite(&i,2,1,waveFile);
  i=48000; //sample rate
  fwrite(&i,4,1,waveFile);
  i=192000; //byte rate = samplerate * num channels * bits per sample / 8
  fwrite(&i,4,1,waveFile);
  i=4; //block align - number of bytes for one sample including all channels
  fwrite(&i,2,1,waveFile);
  i=16; //bits per sample
  fwrite(&i,2,1,waveFile);
  sprintf(wavebuffer, "data");
  fwrite(wavebuffer,1,4,waveFile);
  i=0; //data subchunk size - go back and fill this in later
  fwrite(&i,4,1,waveFile);
  fclose(waveFile);
 
  AVOutputFormat *Outfmt;
  AVFormatContext *oc;
  AVStream *audio_st;
  AVPacket pkt;
  char *outfile = "output.wav";
  static short *samples= NULL;
  static unsigned int samples_size= 0;
  int16_t *decode_buffer;
  ok = posix_memalign(&decode_buffer, 16, AVCODEC_MAX_AUDIO_FRAME_SIZE*5);
  Outfmt = guess_format("wav", NULL, NULL);
  if(!Outfmt)
  {
    printf("Output format not guessed successful\n");
  }
  else
  {
    printf("Output format guessed 
successfully------%s-------%s\n",Outfmt->name, Outfmt->long_name);
  }
  
  while(av_read_frame(pFormatCtx, &pkt)>=0)
  {
    int pkt_decoded_len = 0;
    int frame_decoded_len;
    int decode_buff_remain=AVCODEC_MAX_AUDIO_FRAME_SIZE * 5;

    if(pkt.stream_index==audiostream)
    {
      frame_decoded_len=decode_buff_remain;
      int16_t *decode_buff_ptr = decode_buffer;
      int decoded_tot_len=0;
      pkt_decoded_len = avcodec_decode_audio2(pCodecCtx, decode_buff_ptr, 
&frame_decoded_len, pkt.data, pkt.size);
      if (pkt_decoded_len <0) break;
      pkt.size -= pkt_decoded_len;
      pkt.data += pkt_decoded_len;
      decode_buff_ptr += frame_decoded_len;
      decode_buff_remain -= frame_decoded_len;
      decoded_tot_len += frame_decoded_len;
      f = fopen("outmms.wav", "a");
      fwrite(decode_buffer, 1, decoded_tot_len, f);
      fclose(f);
    }
  }

  printf("END OF PROGRAM\n");

  //dump_format(pFormatCtx, 0, 0, 0);


}

int read_data(void *opaque, char *buf, int buf_size)
{
  FILE* f;
  f = fopen("outmms.wma", "a");
  printf("Read DATA\n");
  fflush(stdin);
  mmsx_t *this22 = (mmsx_t *)opaque;
  int cnt = mmsx_read(NULL, opaque, buf, buf_size);
  fwrite(buf, 1, cnt, f);
  fclose(f);
  return cnt;
}

_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user

Reply via email to