[FFmpeg-user] Problem with playing audio

Anitha Susan Varghese anithasusan at tataelxsi.co.in
Tue Aug 20 16:04:42 CEST 2013


Hi

Myself trying to play an audio file using libavcodec in qt5 .While trying to play i am unable to play the the file.
while decoding the file using av_read_frame()
audioStream->index=0
and packet.stream_index is some higher nos .so avcodec_decode_audio4() is not getting executed.Why is it so?

This is my program. My output is also given below.Please help me in solving this.
-------------------------------------------------------------------------------------------------------------------------------------
#include <QApplication>
#include <QtDebug>
#include <ao/ao.h>

extern "C" {
#include "libavutil/samplefmt.h"
#include "libavcodec/avcodec.h"
#include "libavutil/mathematics.h"
#include "libavutil/avutil.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavutil/dict.h"
#include <ao/ao.h>
#include "libavformat/version.h"
}
#define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000
#define CODEC_TYPE_VIDEO AVMEDIA_TYPE_VIDEO



void die( const char* msg)
{
qDebug() <<  msg ;
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    const char* input_filename=argv[1];

    static int driver;
    static ao_device *audio_device;
    int i;
    int stream_id=-1;
    av_register_all();
    avcodec_register_all();
    AVFormatContext* container=NULL;
    AVStream* audioStream=NULL;
       //container=avformat_alloc_context();

     if(avformat_open_input(&container,input_filename,NULL,NULL)<0){
                die("Could not open file");
        }
       else
       die("File Opend");


    if(avformat_find_stream_info(container,NULL)<0){
                die("Could not find file info");
        }


    av_dump_format(container,0,input_filename,false);
    for(i=0; i < container->nb_streams; i++){

        if(container->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO){
                stream_id=i;
                audioStream=container->streams[i];

                qWarning() <<"audioStream"<<audioStream;
                qWarning() <<"Stream id:"<<stream_id;
                 break;
                 }
        }

    if(stream_id==-1){
        die("Could not find Audio Stream");
    }


    AVCodecContext *ctx=container->streams[stream_id]->codec;
    qWarning() <<"Codec ptr:"<< ctx;
    AVCodec *codec=avcodec_find_decoder(ctx->codec_id);
 if(codec==NULL){
        die("cannot find codec!");
   }

  if(avcodec_open2(ctx,codec,NULL)<0){
        die("Codec cannot be opended!");
   }
   else
        qWarning()<<"Codec Opened";


    ao_initialize();
    driver = ao_default_driver_id();
    qWarning()<<"DRIVER ID" <<driver;
   ao_sample_format sformat;
   AVPacket dummy_packet;
   av_read_frame(container,&dummy_packet);
   AVSampleFormat sfmt=ctx->sample_fmt;
//assign device sample rate depend on the input stream

   qWarning()<<"Sample format"<<sfmt;

   if(sfmt==AV_SAMPLE_FMT_U8){
        sformat.bits=8;
        qWarning()<<"Sample format"<<sformat.bits;
   }
   else if(sfmt==AV_SAMPLE_FMT_S16){
        sformat.bits=16;
        qWarning()<<"Sample format"<<sformat.bits;
  }
  else if(sfmt==AV_SAMPLE_FMT_S32){
        sformat.bits=32;
        qWarning()<<"Sample format:AV_SAMPLE_FMT_S32"<<sformat.bits;
  }
else if(sfmt==AV_SAMPLE_FMT_FLT){
        sformat.bits=32;
        qWarning()<<"Sample format:AV_SAMPLE_FMT_FLT"<<sformat.bits;
 else if(sfmt==AV_SAMPLE_FMT_DBL){
        sformat.bits=64;
        qWarning()<<"Sample format"<<sformat.bits;
  }
  else if(sfmt==AV_SAMPLE_FMT_U8P){
        sformat.bits=8;
        qWarning()<<"Sample format"<<sformat.bits;
  }
  else if(sfmt==AV_SAMPLE_FMT_S16P){
        sformat.bits=16;
        qWarning()<<"Sample format"<<sformat.bits;
  }

   else if(sfmt==AV_SAMPLE_FMT_S32P){
        sformat.bits=32;
        qWarning()<<"Sample format:AV_SAMPLE_FMT_S32P"<<sformat.bits;
  }
  else if(sfmt==AV_SAMPLE_FMT_FLTP){
        sformat.bits=32;
        qWarning()<<"Sample format:AV_SAMPLE_FMT_FLTP"<<sformat.bits;
  }
 else if(sfmt==AV_SAMPLE_FMT_DBLP){
        sformat.bits=64;
        qWarning()<<"Sample format"<<sformat.bits;
  }


   sformat.channels=ctx->channels;

  //sformat.channels=2;
  qWarning()<<"Sample format channels"<<sformat.channels;

  sformat.rate=ctx->sample_rate;
  qWarning()<<"Sample format rate"<<sformat.rate;

  sformat.byte_format=AO_FMT_NATIVE;
  qWarning()<<"Sample format "<<sformat.byte_format;
 sformat.matrix=0;

 //seek back to the beginning of stream
  av_seek_frame(container,stream_id,0,AVSEEK_FLAG_ANY);

  audio_device=ao_open_live(driver,&sformat,NULL);
//end of init AO LIB

//data packet read from the stream
  AVPacket packet;
  av_init_packet(&packet);

  int buffer_size=AVCODEC_MAX_AUDIO_FRAME_SIZE+ FF_INPUT_BUFFER_PADDING_SIZE;;

  qWarning()<<"buffersize "<<buffer_size;
  uint8_t buffer[buffer_size];
  packet.data=buffer;
  packet.size =buffer_size;

//frame ,where the decoded data will be written
  AVFrame *frame=avcodec_alloc_frame();
 int len;
  int frameFinished=0;

  qWarning()<<"CODEC"<<audioStream->codec;
  while(av_read_frame(container,&packet)>=0)
  {
//        qWarning()<<audioStream->index;
        if(packet.stream_index==audioStream->index){
        len=avcodec_decode_audio4(ctx,frame,&frameFinished,&packet);
        qWarning()<<"Enterd if loop";
      //  qWarning() <<len;
        if(frameFinished){
//play the decoded byteis
                qWarning()<<"PLAYING";
                ao_play(audio_device, (char*)frame->extended_data[0],frame->linesize[0] );
  }else{
}
}
 avformat_close_input(&container);
  ao_shutdown();
  return app.exec();
}
----------------------------------------------------------------------------------------------------------------------------------
My output
--------------------------------------------------------------------------------------------------------------------                     File Opend
Input #0, ogg, from '/home/tel/Downloads/desktop-login.ogg':
  Duration: 00:00:07.72, start: 0.000000, bitrate: 108 kb/s
    Stream #0:0: Audio: vorbis, 44100 Hz, stereo, fltp, 112 kb/s
audioStream 0x88b3f20
Stream id: 0
Codec ptr: 0x88b4100
Codec Opened
DRIVER ID 1
Sample format 3
Sample format:AV_SAMPLE_FMT_FLT 32
Sample format channels 0
Sample format rate 0
Sample format  4
buffersize  192008
CODEC 0x88b4100


--------------------------------------------------------------------------------------------------------------
Regards,
Anitha Susan Varghese



More information about the ffmpeg-user mailing list