[Libav-user] Raw video encoding using AV_CODEC_ID_RAWVIDEO

Pradeep Karosiya praks411 at gmail.com
Tue Jul 9 14:21:37 CEST 2013


I've used av_interleaved_write_frame() for writing to .avi file. I'm facing
this problem only for raw video.
For AV_CODEC_ID_MPEG4 it works fine.

Here are the two functions which I'm using for writing video and audio
frames.

//Member function to video frame
void FfmpegEncoder::WriteFrameInt(void)
{
    static int num_call = 0;
    static int64_t vid_pts = -1;
    AVCodecContext * c = video_st->codec;
    int ret = 0;
    AVPacket pkt = {0};
    av_init_packet(&pkt);
    int got_frame = 0;
    ret = avcodec_encode_video2(c, &pkt, m_pic_frame, &got_frame);
    if (got_frame > 0 && ret == 0) 
    {
        pkt.stream_index= video_st->index;
        vid_pts++;
        pkt.pts = vid_pts;
        std::cout<<"Video Pts "<<vid_pts<<std::endl;
        pkt.dts = pkt.pts;
        if((ret = av_interleaved_write_frame(oc, &pkt)) < 0)
        {
            PRINT_MSG("FAILED to WRITE Video Frame")
        }
        av_free_packet(&pkt);
     } 
}


//Member function to write audio frame.
void FfmpegEncoder::WriteAudioSample(AudioSample *audS)
{
    int num_audio_frame = 0;
    AVCodecContext *c = NULL;
    AVPacket pkt;
    av_init_packet(&pkt);
    pkt.destruct = NULL;
    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) )
    {
        AVFrame *frame = avcodec_alloc_frame();
        int byte_per_sample = av_get_bytes_per_sample(c->sample_fmt);
        PRINT_VAL("Byte Per Sample ", byte_per_sample)
        frame->nb_samples =
(audS->GetSampleLength())/(c->channels*av_get_bytes_per_sample(c->sample_fmt));
        frame->channel_layout = c->channel_layout;
        frame->format = c->sample_fmt;
        assert(frame->nb_samples == c->frame_size);
        if(frame->nb_samples == c->frame_size)
        {   
            assert(audS->GetSampleLength() == m_buffer_size);
            memcpy(m_samples,(const uint8_t*)audS->GetAudioSampleData(),
m_buffer_size);
            ret = avcodec_fill_audio_frame(frame, 
m_out_aud_strm->codec->channels, m_out_aud_strm->codec->sample_fmt, (const
uint8_t*)m_samples,m_buffer_size, 0);
           
            if(ret != 0)
            {
                PRINT_MSG("Avcodec Fill Audio Failed ")
            }
            else
            {
                got_packet = 0;
                if(frame->pts == AV_NOPTS_VALUE)
                {
                    frame->pts = m_syn_opts;
                }
                m_syn_opts = frame->pts + frame->nb_samples;
                ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet);
                if(ret < 0 || got_packet == 0)
                {
                    PRINT_MSG("FAILED to ENCODE audio ")     
                }
                else
                {
                    PRINT_MSG("Audio Packet Encoded ");
                    #if 1
                    aud_pts_in++;
                    pkt.pts = aud_pts_in;
                    std::cout<<"Audio Pts "<<aud_pts_in<<std::endl;
                    pkt.dts = pkt.pts;
                    #endif
                    pkt.stream_index = m_out_aud_strm->index;
                     fwrite(pkt.data, 1, pkt.size, m_fp);
                    ret = av_interleaved_write_frame(oc, &pkt);
                    if(ret != 0)
                    {
                        PRINT_MSG("FAILED to WRITE Audio PKT ")
                    }
                    else
                    {
                        PRINT_MSG("Audio PKT Writen ")    
                    }

                }
                avcodec_free_frame(&frame);
             }
             
        }
       
     }
     av_free_packet(&pkt);
}


Thanks,
Pradeep



--
View this message in context: http://libav-users.943685.n4.nabble.com/Raw-video-encoding-using-AV-CODEC-ID-RAWVIDEO-tp4658117p4658121.html
Sent from the libav-users mailing list archive at Nabble.com.


More information about the Libav-user mailing list