<div dir="ltr">I am trying to decode the audio and draw the waveform using ffmpeg,  and the input audio data is `AV_SAMPLE_FMT_S16P`, basically I am following the tutorial [here][ <a href="https://0xdeafc0de.wordpress.com/2013/12/19/ffmpeg-audio-playback-sample/">https://0xdeafc0de.wordpress.com/2013/12/19/ffmpeg-audio-playback-sample/</a>], and the audio is playing fine with [libao][<a href="https://xiph.org/ao/doc/overview.html">https://xiph.org/ao/doc/overview.html</a>]. Now I need to plot the waveform using decoded data, currently I am writing left and right channel to separate csv file and plotting on excel. But the waveform is something different from the waveform shown in Audacity using the same audio clip. When I analyzed the value written on csv most of the values are close to maximum of `uint16_t `(65535), but there are some other lower values, but majority is high peak. <br><br><br>Here is the source code,<br><br> <br><br>        const char* input_filename="/home/user/Music/Clip.mp3";<br>        av_register_all();<br>        AVFormatContext* container=avformat_alloc_context();<br>        if(avformat_open_input(&container,input_filename,NULL,NULL)<0){<br>            endApp("Could not open file");<br>        }<br>    <br>        if(avformat_find_stream_info(container, NULL)<0){<br>            endApp("Could not find file info");<br>        }<br>    <br>        av_dump_format(container,0,input_filename,false);<br>    <br>        int stream_id=-1;<br>        int i;<br>        for(i=0;i<container->nb_streams;i++){<br>            if(container->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO){<br>                stream_id=i;<br>                break;<br>            }<br>        }<br>        if(stream_id==-1){<br>            endApp("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>        if(codec==NULL){<br>            endApp("cannot find codec!");<br>        }<br>    <br>        if(avcodec_open2(ctx,codec,NULL)<0){<br>            endApp("Codec cannot be found");<br>        }<br>    <br>    <br>    <br>        AVPacket packet;<br>        av_init_packet(&packet);<br>    <br>        //AVFrame *frame=avcodec_alloc_frame();<br>        AVFrame *frame=av_frame_alloc();<br>    <br>        int buffer_size=AVCODEC_MAX_AUDIO_FRAME_SIZE+ FF_INPUT_BUFFER_PADDING_SIZE;<br>    <br>        // MSVC can't do variable size allocations on stack, ohgodwhy<br>        uint8_t *buffer = new uint8_t[buffer_size];<br>        packet.data=buffer;<br>        packet.size =buffer_size;<br>    <br>        int frameFinished=0;<br>    <br>        int plane_size;<br>    <br>        ofstream fileCh1,fileCh2;<br>        fileCh1.open ("ch1.csv");<br>        fileCh2.open ("ch2.csv");<br>    <br>        AVSampleFormat sfmt=ctx->sample_fmt;<br>    <br>        while(av_read_frame(container,&packet)>=0)<br>        {<br>    <br>            if(packet.stream_index==stream_id){<br>                int len=avcodec_decode_audio4(ctx,frame,&frameFinished,&packet);<br>                int data_size = av_samples_get_buffer_size(&plane_size, ctx->channels,<br>                                                    frame->nb_samples,<br>                                                    ctx->sample_fmt, 1);<br>    <br>    <br>                if(frameFinished){<br>                    int write_p=0;<br>                    // QTime t;<br>                    switch (sfmt){<br>    <br>                        case AV_SAMPLE_FMT_S16P:<br>    <br>                            for (int nb=0;nb<plane_size/sizeof(uint16_t);nb++){<br>                                for (int ch = 0; ch < ctx->channels; ch++) {<br>                                    if(ch==0)<br>                                        fileCh1 <<((uint16_t *) frame->extended_data[ch])[nb]<<"\n";<br>                                    else if(ch==1)<br>                                        fileCh2 <<((uint16_t *) frame->extended_data[ch])[nb]<<"\n";<br>                                }<br>                            }<br>    <br>                            break;<br>    <br>                    }<br>                } else {<br>                    DBG("frame failed");<br>                }<br>            }<br>    <br>    <br>            av_free_packet(&packet);<br>        }<br>        fileCh1.close();<br>        fileCh2.close();<br>        avcodec_close(ctx);<br>        avformat_close_input(&container);<br>        delete buffer;<br>        return 0;<br><br>        <br></div>