I am trying to encode a wma stream which is downloaded via libmms to mp3.

 

I am getting this error when I try to write frame

 

error, non monotone timestamps 4887518128590291004 >= 7216676395666

error, non monotone timestamps 4846352658059493436 >= 7216677804686

error, non monotone timestamps 4859273513118728252 >= 7216678673811

error, non monotone timestamps 4886046458046316604 >= 7216680462839

 

and many more repeating.

 

Can anyone guide me what is causing this ?

 

Thanks in advance

 

Here is my program

 

#include <stdio.h>

#include "libmms/mmsx.h"

#include "libmms/mms.h"

#include "libavcodec/avcodec.h"

#include "libavformat/avformat.h"

#include "libavutil/fifo.h"

 

#define MAX_AUDIO_PACKET_SIZE (4 * 2048)

const char *url = "mms://live.cumulusstreaming.com/KPLX-FM";

 

typedef struct PacketQueue {

  AVPacketList *first_pkt, *last_pkt;

  int nb_packets;

  int size;

} PacketQueue;

 

PacketQueue audioq;

 

void packet_queue_init(PacketQueue *q) {

  memset(q, 0, sizeof(PacketQueue));

}

int packet_queue_put(PacketQueue *q, AVPacket *pkt) {

 

  AVPacketList *pkt1;

  if(av_dup_packet(pkt) < 0) {

    return -1;

  }

  pkt1 = av_malloc(sizeof(AVPacketList));

  if (!pkt1)

    return -1;

  pkt1->pkt = *pkt;

  pkt1->next = NULL;

 

  if (!q->last_pkt)

    q->first_pkt = pkt1;

  else

    q->last_pkt->next = pkt1;

  q->last_pkt = pkt1;

  q->nb_packets++;

  q->size += pkt1->pkt.size;

  return 0;

}

static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)

{

  AVPacketList *pkt1;

  int ret;

 

  pkt1 = q->first_pkt;

  if (pkt1)

  {

      q->first_pkt = pkt1->next;

      if (!q->first_pkt)

        q->last_pkt = NULL;

      q->nb_packets--;

      q->size -= pkt1->pkt.size;

      *pkt = pkt1->pkt;

      av_free(pkt1);

      ret = 1;

  }

  else

  {

    ret = -1;

  }

  return ret;

}

 

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;

}

 

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;

  if(init_put_byte(&ByteIOCtx, buf, 2048, 0, this, read_data, NULL,
seek_data) < 0)

  {

    printf("init_put_byte not 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");

  }

 

 

 

////OUTPUT FILE

  AVOutputFormat *Outfmt;

  AVFormatContext *oc;

  AVStream *audio_st;

  char *outfile = "output.mp3";

  Outfmt = guess_format("mp3", 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);

  }

 

  oc = av_alloc_format_context();

  if(!oc)

  {

    printf("Allocation of output media context not successful\n");

  }

  else

  {

    printf("Allocation of output media context successful\n");

  }

  oc->oformat = Outfmt;

 

  if (av_set_parameters(oc, NULL) < 0)

  {

    printf("Invalid output format parameters\n");

  }

  else

  {

    printf("Output format parameters set successfully\n");

  }

  audio_st = add_audio_stream(oc, Outfmt->audio_codec, pCodecCtx);

 

 

 

  AVCodecContext *outc;

  AVCodec *outcodec;

 

  outc = audio_st->codec;

 

  outcodec = avcodec_find_encoder(outc->codec_id);

 

  if(!outcodec)

  {

    printf("Could not find codec\n");

  }

  else

  {

    printf("Codec for out stream  = %s\n", outcodec->name);

  }

 

  if (avcodec_open(outc, outcodec) < 0)

  {

    printf("Could not open codec\n");

  }

  else

  {

    printf("Codec opened successfully\n");

  }

 

  if(!(Outfmt->flags & AVFMT_NOFILE))

  {

    if(url_fopen(&oc->pb, outfile, URL_WRONLY) < 0)

    {

      printf("Could not open %s\n",outfile);

    }

    else

    {

      printf("Output file opened successfully\n");

    }

  }

 

  av_write_header(oc);

 

 

  AVPacket pkt, packet;

  av_init_packet(&pkt);

  av_init_packet(&packet);

 

 

  ReSampleContext *rs;

 

  static short *samples= NULL;

  short *mp3buffer = (short*) malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);

  uint8_t *outbuf = (uint8_t*) malloc(2*MAX_AUDIO_PACKET_SIZE);

  uint8_t *data_buf = (uint8_t*)av_malloc(2*MAX_AUDIO_PACKET_SIZE);

  uint8_t *audio_out = (uint8_t*)av_malloc(4*MAX_AUDIO_PACKET_SIZE);

  uint8_t *buffer_resample = (uint8_t*)av_malloc(2*MAX_AUDIO_PACKET_SIZE);

 

  static unsigned int samples_size= 0;

 

  if(pCodecCtx->sample_rate != outc->sample_rate || pCodecCtx->channels !=
outc->channels)

      rs = audio_resample_init(outc->channels,pCodecCtx->channels,
outc->sample_rate, pCodecCtx->sample_rate);

 

  int finalPTS;

 

  while(av_read_frame(pFormatCtx, &packet)>=0)

  {

    printf("Read FRAME\n");

    fflush(stdin);

    if(packet.stream_index==audiostream)

    {

      int bufSize = AVCODEC_MAX_AUDIO_FRAME_SIZE;

      int result = avcodec_decode_audio2(pCodecCtx, mp3buffer, &bufSize,
packet.data, packet.size);

      if(result > 0)

      {

        pkt.size= avcodec_encode_audio(outc, outbuf,
(2*MAX_AUDIO_PACKET_SIZE), mp3buffer);

        pkt.pts = packet.pts;

        pkt.data= outbuf;

        if (av_interleaved_write_frame(oc, &pkt) != 0)

        {

          printf("Could not write audio frame\n");

        }

        else

        {

          printf("Audio frame written successfully\n");

        }

      }

    }

    av_free_packet(&packet);

    av_free_packet(&pkt);

  }

  printf("END OF PROGRAM\n");

 

  

}

 

int read_data(void *opaque, char *buf, int buf_size)

{

  printf("Read DATA\n");

  fflush(stdin);

  mmsx_t *this22 = (mmsx_t *)opaque;

  int cnt = mmsx_read(NULL, opaque, buf, buf_size);

  return cnt;

}

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;

}

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

Reply via email to