[Libav-user] Audio encoding using avcodec_fill_audio_frame() and memory leaks

Pradeep Karosiya praks411 at gmail.com
Fri May 17 10:25:01 CEST 2013


Hi,

As a part of encoding decoded audio packets, I'm using
avcodec_fill_audio_frame(). I'm passing allocated AVFrame pointer to along
with buffer containing the decoded samples and other parameters number of
channels, sample format, buffer size. Though the encoding is working fine
I'm not able to completely eliminate the memory leaks. I've taken care of
most of things but still I'm not able detect the leakage.
Below is the function which I'm using for encoding. Please suggest
something.
AudioSample contains decoded data and it is completely managed in different
class(free in class destructor). I'm freeing the AVFrame in FFmpegEncoder
destructor and AVPacket is freed every time using av_free_packet() with
av_packet_destruct enabled. What more do I need to free?


void FfmpegEncoder::WriteAudioSample(AudioSample *audS)
{


    int num_audio_frame = 0;
    AVCodecContext *c = NULL;
   // AVFrame *frame;
    AVPacket pkt;

    av_init_packet(&pkt);
    pkt.destruct = av_destruct_packet;
    pkt.data = NULL;
    pkt.size = 0;
    int ret = 0, got_packet = 0;
    c = m_out_aud_strm->codec;
     static int64_t aud_pts_in = -1;

    if((audS != NULL) && (audS->GetSampleLength() > 0) )
    {
        int byte_per_sample = av_get_bytes_per_sample(c->sample_fmt);
       
        PRINT_VAL("Byte Per Sample ", byte_per_sample)
        m_frame->nb_samples =
(audS->GetSampleLength())/(c->channels*av_get_bytes_per_sample(c->sample_fmt));
        if(m_frame->nb_samples == c->frame_size)
        {   
            
#if 1
        if(m_need_resample && (c->channels >= 2))
           {
               uint8_t * t_buff1 = new uint8_t[audS->GetSampleLength()];
               if(t_buff1 != NULL)
                {
                     for(int64_t i = 0; i< m_frame->nb_samples; i++)
                    {
                        memcpy(t_buff1 + i*byte_per_sample,
(uint8_t*)((uint8_t*)audS->GetAudioSampleData() +
i*byte_per_sample*c->channels), byte_per_sample);
                        memcpy(t_buff1 + (audS->GetSampleLength())/2 +
i*byte_per_sample, (uint8_t*)((uint8_t*)audS->GetAudioSampleData() +
i*byte_per_sample*c->channels+ byte_per_sample), byte_per_sample);
                    }
                    audS->FillAudioSample(t_buff1, audS->GetSampleLength());
                    delete[] t_buff1;
                }
            }
#endif
            ret = avcodec_fill_audio_frame(m_frame, c->channels,
c->sample_fmt,
(uint8_t*)audS->GetAudioSampleData(),m_frame->nb_samples*byte_per_sample*c->channels,
0);
            //ret = avcodec_fill_audio_frame(&frame, c->channels,
c->sample_fmt, t_buff,frame.nb_samples*byte_per_sample*c->channels, 0);
            if(ret != 0)
            {
                PRINT_MSG("Avcodec Fill Audio Failed ")
            }
            else
            {
                got_packet = 0;
                ret = avcodec_encode_audio2(c, &pkt, m_frame, &got_packet);
                if(ret < 0 || got_packet == 0)
                {
                    PRINT_MSG("failed to encode audio ")     
                }
                else
                {
                    PRINT_MSG("Audio Packet Encoded ");
                    aud_pts_in++;
                    pkt.pts = aud_pts_in;
                    pkt.dts = pkt.pts;
                    pkt.stream_index = m_out_aud_strm->index;
                   
                    ret = av_interleaved_write_frame(oc, &pkt);
                    if(ret != 0)
                    {
                        PRINT_MSG("Error Write Audio PKT ")    
                    }
                    else
                    {
                        PRINT_MSG("Audio PKT Writen ")    
                    }
                }
             }
        }
        avcodec_flush_buffers(c);
     //   avcodec_free_frame(&frame);
     }

     av_free_packet(&pkt);
}

Thanks 
Pradeep



--
View this message in context: http://libav-users.943685.n4.nabble.com/Audio-encoding-using-avcodec-fill-audio-frame-and-memory-leaks-tp4657623.html
Sent from the libav-users mailing list archive at Nabble.com.


More information about the Libav-user mailing list