<div dir="rtl"><div style="text-align:left" dir="ltr"><pre>Hi

<font face="arial,sans-serif">I am</font> trying to play an audio udp (HE-AAC) using libavcodec and ao player.<br>While trying to play I hear a very noisy channel.<br><br></pre><pre>The av_dump_format(container,0,input_filename,0) is:<br><br><span style="background-color:rgb(204,204,204)">Input #0, mpegts, from 'udp://<a href="http://224.10.0.15:1234">224.10.0.15:1234</a>':<br>  Duration: N/A, start: 70866.349700, bitrate: N/A<br>  Program 29 <br>    Metadata:<br>      service_name    : Galatz<br>      service_provider: Idan +<br>    Stream #0:0[0xb81]: Audio: aac_latm (HE-AAC) ([17][0][0][0] / 0x0011), 48000 Hz, stereo, fltp</span><br></pre></div><div dir="ltr"><br></div><div dir="ltr"><span style="background-color:rgb(204,204,204)">And the ao_sample_format sformat content is: <br></span><div style="margin-left:40px"><span style="background-color:rgb(204,204,204)">Bits: 8, Channels:0<br>Rate: 8, byte_format:1 matrix:L,R<br></span></div><br></div><div dir="ltr">The c code:<br></div><div dir="ltr">---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br>#include <libavformat/avformat.h><br>#include <libavcodec/avcodec.h><br>//#include "libavcodec/lpc.h"<br>//#include <libavcodec/aacsbr.c><br>#include <libavutil/avutil.h><br>#include <libavutil/rational.h><br>#include <libavutil/samplefmt.h><br>#include <ao/ao.h><br><br>#include <stdio.h><br><br><br>#define AVCODEC_MAX_AUDIO_FRAME_SIZE 19200<br><br>void die(const char *msg)<br>{<br>    fprintf(stderr,"%s\n",msg);<br>    exit(1);<br>}<br><br>int main(int argc, char** argv)<br>{<br>    //SpectralBandReplication *sbr; <br>    //int id_aac;<br>//    sbr_dequant(sbr, id_aac);<br>    av_register_all();<br>   avformat_network_init();<br>   <br>   <br>    const char* input_filename=argv[1];<br>    <br>   AVFormatContext* container=avformat_alloc_context();<br>    if(avformat_open_input(&container,input_filename,NULL,NULL)<0)<br>        die("Could not open file");<br>    <br>    if(avformat_find_stream_info(container,NULL)<0)<br>        die("Could not find file info");<br>    <br>   <br>   av_dump_format(container,0,input_filename,0);<br><br>    int stream_id=-1;<br>    int i;<br>    for(i=0;i<container->nb_streams;i++)<br>    {<br>       if(container->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO)<br>        {<br>            stream_id=i;//audio stream index<br>            break;<br>        }<br>    }<br>    if(stream_id==-1){<br>        die("Could not find Audio Stream");<br>    }<br><br>  //  AVDictionary *metadata=container->metadata;<br><br>    AVCodecContext *ctx=container->streams[stream_id]->codec;<br>    AVCodec *codec=avcodec_find_decoder(ctx->codec_id);<br>  <br>    ctx=avcodec_alloc_context3(codec);<br>    if(codec==NULL){<br>        die("cannot find codec!");<br>    }<br><br>    <br>    if(avcodec_open2(ctx,codec, NULL)<0){<br>        die("Codec cannot be found");<br>    }<br>    <br>    //initialize AO lib<br>    ao_initialize();<br><br>    int driver = ao_default_driver_id();//ao_driver_id("wav");<br>    <br>    ao_sample_format sformat;<br>    <br>    enum AVSampleFormat sfmt=ctx->sample_fmt;<br>   memset(&sformat, 0, sizeof(sformat)); <br>    if(sfmt==AV_SAMPLE_FMT_U8){<br>        printf("U8\n");<br><br>        sformat.bits=8;<br>    }else if(sfmt==AV_SAMPLE_FMT_S16){<br>        printf("S16\n");<br>        sformat.bits=16;<br>    }else if(sfmt==AV_SAMPLE_FMT_S32){<br>        printf("S32\n");<br>        sformat.bits=32;<br>    }<br>   else if(sfmt==AV_SAMPLE_FMT_FLTP){<br>        sformat.bits=32;<br>   }<br>    //sformat.bits = sformat.bits;<br>    sformat.channels = 2;//ctx->channels;<br>    sformat.rate = 48000;//ctx->sample_rate;<br>    sformat.byte_format = AO_FMT_LITTLE; // Samples are in little-endian order<br>    sformat.matrix = "L,R";<br>    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);<br>    <br>    ao_device *adevice = ao_open_live(driver,&sformat,NULL);<br>    //end of init AO LIB<br><br>    // prepare to read data<br>    AVPacket packet;<br>    av_init_packet(&packet);<br><br>    AVFrame *frame=av_frame_alloc();<br>    if (!frame) <br>    {<br>        fprintf(stderr, "Error allocating the frame\n");<br>        return -1;<br>    }<br>    <br>    int buffer_size=AVCODEC_MAX_AUDIO_FRAME_SIZE+ FF_INPUT_BUFFER_PADDING_SIZE;<br>    <br>    uint8_t buffer[buffer_size];<br>    packet.data=buffer;<br>    packet.size =buffer_size;<br>    packet.pos = 0; //add from <a href="https://lists.ffmpeg.org/pipermail/ffmpeg-user/2013-August/017040.html">https://lists.ffmpeg.org/pipermail/ffmpeg-user/2013-August/017040.html</a><br>    <br>    int len;<br>    int frameFinished=0;<br>    i = 0;<br>     while(av_read_frame(container,&packet)>=0)<br>    {<br><br>        if(packet.stream_index==stream_id){<br>            //printf("Audio Frame read  \n");<br>            int len=avcodec_decode_audio4(ctx,frame,&frameFinished,&packet);<br>            //frame-><br>            if(frameFinished){<br>              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]);<br>                ao_play(adevice, (char*)frame->extended_data[0],frame->linesize[0] );<br>                if (i == 0)<br>                {<br>                    printf("play\n");<br>                    i++;<br>                }<br>            }else<br>            {<br>                printf("Not Finished\n");<br>            }<br><br>        }else {<br>            printf("Some other packet possibly Video\n");<br>        }<br><br><br>    }<br><br>    avformat_close_input(&container);<br>    ao_shutdown();<br>    return 0;<br>}<br>    <br> <br clear="all"><br>-- <br></div><div class="gmail_signature" dir="ltr" style="text-align:center"><div>Yosef Alon<br></div></div>
</div>