[Libav-user] Problem with playing audio with ao

יוסף אלון yos104104 at gmail.com
Mon Apr 3 11:56:06 EEST 2017


Hi
I am trying to play an audio udp (HE-AAC) using libavcodec and ao player.
While trying to play I hear a very noisy channel.

The av_dump_format(container,0,input_filename,0) is:

Input #0, mpegts, from 'udp://224.10.0.15:1234':
  Duration: N/A, start: 70866.349700, bitrate: N/A
  Program 29
    Metadata:
      service_name    : Galatz
      service_provider: Idan +
    Stream #0:0[0xb81]: Audio: aac_latm (HE-AAC) ([17][0][0][0] /
0x0011), 48000 Hz, stereo, fltp


And the ao_sample_format sformat content is:
Bits: 8, Channels:0
Rate: 8, byte_format:1 matrix:L,R

The c code:
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
//#include "libavcodec/lpc.h"
//#include <libavcodec/aacsbr.c>
#include <libavutil/avutil.h>
#include <libavutil/rational.h>
#include <libavutil/samplefmt.h>
#include <ao/ao.h>

#include <stdio.h>


#define AVCODEC_MAX_AUDIO_FRAME_SIZE 19200

void die(const char *msg)
{
    fprintf(stderr,"%s\n",msg);
    exit(1);
}

int main(int argc, char** argv)
{
    //SpectralBandReplication *sbr;
    //int id_aac;
//    sbr_dequant(sbr, id_aac);
    av_register_all();
   avformat_network_init();


    const char* input_filename=argv[1];

   AVFormatContext* container=avformat_alloc_context();
    if(avformat_open_input(&container,input_filename,NULL,NULL)<0)
        die("Could not open file");

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


   av_dump_format(container,0,input_filename,0);

    int stream_id=-1;
    int i;
    for(i=0;i<container->nb_streams;i++)
    {
       if(container->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO)
        {
            stream_id=i;//audio stream index
            break;
        }
    }
    if(stream_id==-1){
        die("Could not find Audio Stream");
    }

  //  AVDictionary *metadata=container->metadata;

    AVCodecContext *ctx=container->streams[stream_id]->codec;
    AVCodec *codec=avcodec_find_decoder(ctx->codec_id);

    ctx=avcodec_alloc_context3(codec);
    if(codec==NULL){
        die("cannot find codec!");
    }


    if(avcodec_open2(ctx,codec, NULL)<0){
        die("Codec cannot be found");
    }

    //initialize AO lib
    ao_initialize();

    int driver = ao_default_driver_id();//ao_driver_id("wav");

    ao_sample_format sformat;

    enum AVSampleFormat sfmt=ctx->sample_fmt;
   memset(&sformat, 0, sizeof(sformat));
    if(sfmt==AV_SAMPLE_FMT_U8){
        printf("U8\n");

        sformat.bits=8;
    }else if(sfmt==AV_SAMPLE_FMT_S16){
        printf("S16\n");
        sformat.bits=16;
    }else if(sfmt==AV_SAMPLE_FMT_S32){
        printf("S32\n");
        sformat.bits=32;
    }
   else if(sfmt==AV_SAMPLE_FMT_FLTP){
        sformat.bits=32;
   }
    //sformat.bits = sformat.bits;
    sformat.channels = 2;//ctx->channels;
    sformat.rate = 48000;//ctx->sample_rate;
    sformat.byte_format = AO_FMT_LITTLE; // Samples are in little-endian
order
    sformat.matrix = "L,R";
    printf("Sformat:\nBits: %d, Channels:%d\nRate: %d, byte_format:%d
matrix:%s\n",sformat.bits, ctx->channels, sformat.rate,
sformat.byte_format,sformat.matrix);

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

    // prepare to read data
    AVPacket packet;
    av_init_packet(&packet);

    AVFrame *frame=av_frame_alloc();
    if (!frame)
    {
        fprintf(stderr, "Error allocating the frame\n");
        return -1;
    }

    int buffer_size=AVCODEC_MAX_AUDIO_FRAME_SIZE+
FF_INPUT_BUFFER_PADDING_SIZE;

    uint8_t buffer[buffer_size];
    packet.data=buffer;
    packet.size =buffer_size;
    packet.pos = 0; //add from
https://lists.ffmpeg.org/pipermail/ffmpeg-user/2013-August/017040.html

    int len;
    int frameFinished=0;
    i = 0;
     while(av_read_frame(container,&packet)>=0)
    {

        if(packet.stream_index==stream_id){
            //printf("Audio Frame read  \n");
            int len=avcodec_decode_audio4(ctx,frame,&frameFinished,&packet);
            //frame->
            if(frameFinished){
              printf("Finished reading Frame len : %d , nb_samples:%d
buffer_size:%d line size: %d
\n",len,frame->nb_samples,buffer_size,frame->linesize[0]);
                ao_play(adevice,
(char*)frame->extended_data[0],frame->linesize[0] );
                if (i == 0)
                {
                    printf("play\n");
                    i++;
                }
            }else
            {
                printf("Not Finished\n");
            }

        }else {
            printf("Some other packet possibly Video\n");
        }


    }

    avformat_close_input(&container);
    ao_shutdown();
    return 0;
}



-- 
Yosef Alon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20170403/90d7122d/attachment.html>


More information about the Libav-user mailing list