[Libav-user] Seeking encoded video (.avi)

praks411 praks411 at gmail.com
Thu Apr 11 15:14:42 CEST 2013


No, the output file which i'm generating is not working. However it works
when I again encode it with 
ffmpeg. So I'm looking for some help what I'm doing wrong.

Please find below the snippet of code which I'm using

    av_register_all();

  
        //if ( av_open_input_file( &m_data->formatCtx, cfilename, 0, 0, 0)
!= 0 )
    if ( (ret = avformat_open_input( &informat, sourcefile, 0, 0)) != 0 )
    {
        av_strerror(ret,errbuf,sizeof(errbuf));
        PRINT_VAL("Not able to Open file;; ", errbuf)
        std::getchar();
        return -1;
    }
    else
    {
        PRINT_MSG("Opened File ")
    }


    if ((ret = avformat_find_stream_info(informat, 0)) < 0 )
    {
        
        av_strerror(ret,errbuf,sizeof(errbuf));
        PRINT_VAL("Not Able to find stream info:: ", errbuf)
        std::getchar();
        return -1;
    }
    else
    {
        PRINT_MSG("Got stream Info ")
    }
    av_dump_format(informat, 0, sourcefile, 0);

    for(unsigned int i = 0; i<informat->nb_streams; i++)
    {
        if(informat->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
        {
   
            PRINT_MSG("Found Video Stream ")
            in_vid_strm_idx = i;
            in_vid_strm = informat->streams[i];
        }

        if(informat->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
        {
            PRINT_MSG("Found Audio Stream ")
            in_aud_strm_idx = i;
            in_aud_strm = informat->streams[i];

        }
    }


    if(in_vid_strm != NULL)
    {
        in_vid_codec_ctx = in_vid_strm->codec;
        in_vid_codec = avcodec_find_decoder(in_vid_strm->codec->codec_id);
        if(in_vid_codec == NULL)
        {
            PRINT_MSG("Unable to find Video codec ")
        }
        else
        {
            PRINT_MSG("Found Vid Decoder Opening it ")
            if(need_decoding)
            {
                if((ret = avcodec_open2(in_vid_codec_ctx, in_vid_codec,
NULL))< 0)
                {
                    av_strerror(ret,errbuf,sizeof(errbuf));
                    PRINT_VAL("No Video Decoder:: ", errbuf)
                    std::getchar();
                    return -1;
                }
                else
                {
                    PRINT_MSG("Opened Video Decoder ")
                }
            }
        }
    }

    if(in_aud_strm != NULL)
    {
        in_aud_codec_ctx = in_aud_strm->codec;
        in_aud_codec = avcodec_find_decoder(in_aud_strm->codec->codec_id);
        if(in_aud_codec == NULL)
        {
            PRINT_MSG("Unable to find Audio Codec ")
        }
        else
        {
            PRINT_MSG("Found Aud Decoder Opening it ")
            if(need_decoding)
            {
                if((ret = avcodec_open2(in_aud_codec_ctx, in_aud_codec,
NULL))< 0)
                {
                    av_strerror(ret,errbuf,sizeof(errbuf));
                    PRINT_VAL("Not Able to find Audio Decoder:: ", errbuf)
                    std::getchar();
                    return -1;
                }
                else
                {
                    PRINT_MSG("Opened Audio Decoder ")
                }
            }
        }
    }

    if(need_encoding)
    {
        AVCodecContext * c1, *c2;
        outfmt = av_guess_format(NULL, (const char*)outfile, NULL);

        if(NULL == outfmt)
        {
            PRINT_MSG("Not able to Guess Output format ")
            std::getchar();
        }
        else
        {
            outformat = avformat_alloc_context();
            if(outformat)
            {
                PRINT_MSG("Got Output context ")
                outformat->oformat = outfmt;
                _snprintf(outformat->filename, sizeof(outformat->filename),
"%s", (const char*)outfile);

                if(outfmt->video_codec != AV_CODEC_ID_NONE)
                {
                    out_vid_codec =
avcodec_find_encoder(outfmt->video_codec);
                    if(NULL == out_vid_codec)
                    {
                        PRINT_MSG("Could Not Find Vid Encoder")
                    }
                    else
                    {
                        PRINT_MSG("Found Out Vid Encoder ")
                        out_vid_strm = avformat_new_stream(outformat,
out_vid_codec);
                        if(NULL == out_vid_strm)
                        {
                            PRINT_MSG("Failed to Allocate Output Vid Strm ")
                        }
                        else
                        {
                          
                            PRINT_MSG("Allocated Video Stream ")
 
                            if(avcodec_copy_context(out_vid_strm->codec,
in_vid_codec_ctx) != 0)
                            {
                                PRINT_MSG("Failed to Copy Context ")
                            }
                            else
                            {
                                PRINT_MSG("Copied Context ")
                                out_vid_strm->codec->codec_id =
in_vid_strm->codec->codec_id;
                                out_vid_strm->sample_aspect_ratio.den = 1;
                                out_vid_strm->sample_aspect_ratio.num = 1;
                                out_vid_strm->time_base.num =
in_vid_strm->time_base.num;
                                out_vid_strm->time_base.den =
in_vid_strm->time_base.den;
                                out_vid_strm->r_frame_rate.den =
in_vid_strm->r_frame_rate.den;
                                out_vid_strm->r_frame_rate.num =
in_vid_strm->r_frame_rate.num;
                                out_vid_strm->avg_frame_rate.den =
in_vid_strm->avg_frame_rate.den;
                                 out_vid_strm->avg_frame_rate.num =
in_vid_strm->avg_frame_rate.num;
                            }

                        }
                    }
                }



                if(outfmt->audio_codec != AV_CODEC_ID_NONE)
                {
                    out_aud_codec =
avcodec_find_encoder(outfmt->audio_codec);
                    if(NULL == out_aud_codec)
                    {
                        PRINT_MSG("Could Not Find Out Aud Encoder ")
                    }
                    else
                    {
                        PRINT_MSG("Found Out Aud Encoder ")
                        out_aud_strm = avformat_new_stream(outformat,
out_aud_codec);
                        if(NULL == out_aud_strm)
                        {
                            PRINT_MSG("Failed to Allocate Out Vid Strm ")
                        }
                        else
                        {
                            if(avcodec_copy_context(out_aud_strm->codec,
in_aud_codec_ctx) != 0)
                            {
                                PRINT_MSG("Failed to Copy Context ")
                            }
                            else
                            {
                                PRINT_MSG("Copied Context ")
                            //	out_aud_strm->id = 1;
                                out_aud_strm->codec->codec_id =
in_aud_strm->codec->codec_id;
                                out_aud_strm->pts = in_aud_strm->pts;
                                out_aud_strm->duration =
in_aud_strm->duration;
                                out_aud_strm->time_base.num =
in_aud_strm->time_base.num;
                                out_aud_strm->time_base.den =
in_aud_strm->time_base.den;

                                
                            }
                        }
                    }
                }


            }
        }

    }

    if(need_encoding)
    {
        //av_dump_format(outformat, 0, outfile, 0);
        if (!(outfmt->flags & AVFMT_NOFILE)) 
        {
    
            if (avio_open2(&outformat->pb, outfile, AVIO_FLAG_WRITE,NULL,
NULL) < 0) 
            {
                PRINT_VAL("Could Not Open File ", outfile)
                std::getchar();
                return 1;
            }
        }
        /* Write the stream header, if any. */
        if (avformat_write_header(outformat, NULL) < 0) 
        {
            PRINT_VAL("Error Occurred While Writing Header ", outfile)
            std::getchar();
            return 1;
        }
        else
        {
            PRINT_MSG("Written Output header ")
        }
    }

    AVFrame *pframeT, *pframe;
    pframeT = pframe = NULL;

    pframeT = avcodec_alloc_frame();
    pframe = avcodec_alloc_frame();
    uint8_t *buffer_pic = NULL;
    int num_bytes = 0, pic_sz = 0;

    num_bytes = avpicture_get_size(PIX_FMT_RGB24, in_vid_strm->codec->width,
in_vid_strm->codec->height);
    if(num_bytes != 0)
    {
        PRINT_VAL("Number of Bytes ", num_bytes)
        buffer_pic = (uint8_t*)av_malloc(num_bytes);
        if(buffer_pic == NULL)
        {
            PRINT_MSG("Failed to allocate Buffer ")
        }
    }
    
    pic_sz = avpicture_fill((AVPicture*)pframe,buffer_pic, PIX_FMT_RGB24,
in_vid_strm->codec->width, in_vid_strm->codec->height);
    if(0 == pic_sz)
    {
        PRINT_MSG("avpicture_fill failed ")
    }
    else
    {
        PRINT_VAL("Total Pic size ", pic_sz)
    }

   
PRINT_MSG("==============================================================")

    PRINT_VAL("In Vid Codec Time Base:: NUM :: ",
in_vid_strm->codec->time_base.num)
    PRINT_VAL("In Vid Codec Time Base:: DEN :: ",
in_vid_strm->codec->time_base.den)

    PRINT_VAL("In Vid Stream Time Base:: NUM :: ",
in_vid_strm->time_base.num)
    PRINT_VAL("In Vid Stream Time Base:: DEN :: ",
in_vid_strm->time_base.den)

    PRINT_VAL("In Aud Codec Time Base:: NUM :: ",
in_aud_strm->codec->time_base.num)
    PRINT_VAL("In Aud Codec Time Base:: DEN :: ",
in_aud_strm->codec->time_base.den)

    PRINT_VAL("In Aud Stream Time Base:: NUM :: ",
in_aud_strm->time_base.num)
    PRINT_VAL("In Aud Stream Time Base:: DEN :: ",
in_aud_strm->time_base.den)

   
PRINT_MSG("==============================================================")
    PRINT_VAL("Out Vid Codec Time Base:: NUM :: ",
out_vid_strm->codec->time_base.num)
    PRINT_VAL("Out Vid Codec Time Base:: DEN :: ",
out_vid_strm->codec->time_base.den)

    PRINT_VAL("Out Vid Stream Time Base:: NUM :: ",
out_vid_strm->time_base.num)
    PRINT_VAL("Out Vid Stream Time Base:: DEN :: ",
out_vid_strm->time_base.den)

    PRINT_VAL("Out Aud Codec Time Base:: NUM :: ",
out_aud_strm->codec->time_base.num)
    PRINT_VAL("Out Aud Codec Time Base:: DEN :: ",
out_aud_strm->codec->time_base.den)

    PRINT_VAL("Out Aud Stream Time Base:: NUM :: ",
out_aud_strm->time_base.num)
    PRINT_VAL("Out Aud Stream Time Base:: DEN :: ",
out_aud_strm->time_base.den)
   
PRINT_MSG("==============================================================")

    AVPacket pkt;
    AVPacket outpkt;
    //int64_t aud_pts = -1, vid_pts = -1;
    int num_aud_pkt, num_vid_pkt, num_total_pkt, num_unkwn_pkt;
    num_aud_pkt = num_vid_pkt = num_total_pkt = num_unkwn_pkt = 0;
    int got_vid_pkt = 0, num_vid_dec = 0, num_aud_dec = 0;
    int64_t aud_pts = -1, vid_pts = -1, aud_dts = -1, vid_dts = -1;


    while(av_read_frame(informat, &pkt) >= 0)
    {
        if(pkt.stream_index == in_vid_strm_idx)
        {
            //PRINT_MSG("Got Video Frame ")
            PRINT_VAL("VID Pkt PTS ", pkt.pts)
            PRINT_VAL("VID Pkt DTS ", pkt.dts)
            num_vid_pkt++;
            if(need_decoding)
            {
                avcodec_decode_video2(in_vid_codec_ctx, pframeT,
&got_vid_pkt, &pkt);
                if(got_vid_pkt)
                {
                   // PRINT_MSG("Decode Video Pkt ")
                    num_vid_dec++;
                }
                got_vid_pkt = 0;
            }
#if 1
            if(need_encoding)
            {
                av_init_packet(&outpkt);

                if(pkt.pts != AV_NOPTS_VALUE)
                {
                    outpkt.pts = av_rescale_q(pkt.pts,
in_vid_strm->time_base, out_vid_strm->time_base);
                    //outpkt.pts = av_rescale_q(pkt.pts,
out_vid_strm->time_base,out_vid_strm->codec->time_base);
                    PRINT_VAL("ReScaled VID Pts ", outpkt.pts)

                    vid_pts = outpkt.pts;
                }
                else
                {
                    outpkt.pts = AV_NOPTS_VALUE;
                }

                if(pkt.dts == AV_NOPTS_VALUE)
                {
                    //outpkt.dts = av_rescale_q(in_vid_strm->cur_dts,
AV_TIME_BASE_Q, out_vid_strm->time_base);
                    outpkt.dts = AV_NOPTS_VALUE;
                    
                }
                else
                {
                   outpkt.dts = av_rescale_q(pkt.dts,
in_vid_strm->time_base, out_vid_strm->time_base);
                    //outpkt.dts = av_rescale_q(pkt.dts,
out_vid_strm->time_base, out_vid_strm->codec->time_base);
                     PRINT_VAL("ReScaled VID Dts ", outpkt.dts)
                     PRINT_MSG("=======================================")

                    vid_dts = outpkt.dts;
                }
                outpkt.duration = av_rescale_q(pkt.duration,
in_vid_strm->time_base, out_vid_strm->time_base);
               // outpkt.duration =
av_rescale_q(pkt.duration,out_vid_strm->time_base,out_vid_strm->codec->time_base);

                outpkt.data = pkt.data;
                outpkt.size = pkt.size;
                outpkt.flags = pkt.flags;
                outpkt.stream_index = pkt.stream_index;
                if(av_interleaved_write_frame(outformat, &outpkt) < 0)
                {
                    PRINT_MSG("Failed Video Write ")
                }
                else
                {
                   // PRINT_MSG("Writted Video Frame ")
                    out_vid_strm->codec->frame_number++;
                }
            }
#endif

        }
        else if(pkt.stream_index == in_aud_strm_idx)
        {
            //PRINT_MSG("Got Audio Frame ")
            PRINT_VAL("AUD Pkt PTS ", pkt.pts)
            PRINT_VAL("AUD Pkt DTS ", pkt.dts)
            num_aud_pkt++;
            if(need_decoding)
            {
                avcodec_decode_audio4(in_aud_codec_ctx, pframeT,
&got_vid_pkt, &pkt);
                if(got_vid_pkt)
                {
                    //PRINT_MSG("Decoded Audio Pkt ")
                    num_aud_dec++;
                }
                got_vid_pkt = 0;
            }
#if 1
            if(need_encoding)
            {
                av_init_packet(&outpkt);
#if 1
                if(pkt.pts != AV_NOPTS_VALUE)
                {
                    //outpkt.pts = av_rescale_q(pkt.pts,
out_aud_strm->codec->time_base, out_aud_strm->time_base) ;
                    // outpkt.pts =
av_rescale_q(pkt.pts,out_aud_strm->time_base,out_aud_strm->codec->time_base)
;
                    outpkt.pts = av_rescale_q(pkt.pts,
in_aud_strm->time_base, out_aud_strm->time_base);
                    PRINT_VAL("ReScaled AUD PTS ", outpkt.pts)
                    //outpkt.pts = aud_pts++;
                    //PRINT_VAL("Aud PTS ", outpkt.dts)

                    aud_pts = outpkt.pts;
                    //PRINT_VAL("New Aud PTS ", outpkt.pts)
                }
                else
                {
                    outpkt.pts = AV_NOPTS_VALUE;
                }

                if(pkt.dts == AV_NOPTS_VALUE)
                {
                    outpkt.dts = AV_NOPTS_VALUE;
                }
                else
                {
                   // outpkt.dts = av_rescale_q(pkt.dts,
out_aud_strm->codec->time_base, out_aud_strm->time_base) ;
                   // outpkt.dts = av_rescale_q(pkt.dts,
out_aud_strm->time_base, out_aud_strm->codec->time_base) ;
                    outpkt.dts = av_rescale_q(pkt.dts,
in_aud_strm->time_base, out_aud_strm->time_base);
                    PRINT_VAL("ReScaled AUD DTS ", outpkt.dts)
                    PRINT_MSG("====================================")
                    //PRINT_VAL("Aud DTS ", outpkt.dts)

                    aud_dts = outpkt.dts;
                    //PRINT_VAL("New Aud DTS ", outpkt.dts)
                }

                //outpkt.duration = av_rescale_q(pkt.duration,
out_aud_strm->codec->time_base, out_aud_strm->time_base) ;
              // outpkt.duration = av_rescale_q(pkt.duration,
out_aud_strm->codec->time_base,out_aud_strm->time_base ) ;
                outpkt.duration = av_rescale_q(pkt.duration,
in_aud_strm->time_base, out_aud_strm->time_base) ;

                outpkt.data = pkt.data;
                outpkt.size = pkt.size;
                outpkt.flags = pkt.flags;
                outpkt.stream_index = pkt.stream_index;

                if(av_interleaved_write_frame(outformat, &outpkt) < 0)
              //  if(av_write_frame(outformat, &pkt) < 0)
                {
                    PRINT_MSG("Faile Audio Write ")
                }
                else
                {
                   // PRINT_MSG("Written Audio Frame ")
                    out_aud_strm->codec->frame_number++;
                }
    
            }
    #endif
        }
        else
        {
            PRINT_MSG("Got Unknown Pkt ")
            num_unkwn_pkt++;
        }
        num_total_pkt++;
    }

    
    if(need_encoding)
    {
        av_write_trailer(outformat);
    }
    PRINT_VAL("Total Pkt Read ",num_total_pkt)
    PRINT_VAL("Total Vid Read ",num_vid_pkt)
    PRINT_VAL("Total Aud Read ",num_aud_pkt)
    PRINT_VAL("Total Unknown Read ",num_unkwn_pkt)

    PRINT_VAL("Total Vid Decoded ",num_vid_dec)
    PRINT_VAL("Total Aud Decoded ",num_aud_dec)
    
    av_free_packet(&pkt);
    av_close_input_file(informat);
    if(pframe)
    {
        avcodec_free_frame(&pframe);
    }
    if(pframeT)
    {
        avcodec_free_frame(&pframeT);
    }

    #if 0
    if(informat)
    {
        avformat_free_context(informat);
    }
    if(buffer_pic)
    {
        av_free(buffer_pic);
    }
    if(pframe)
    {
        avcodec_free_frame(&pframe);
    }
    if(pframeT)
    {
        avcodec_free_frame(&pframeT);
    }
    #endif



--
View this message in context: http://libav-users.943685.n4.nabble.com/Seeking-encoded-video-avi-tp4657245p4657254.html
Sent from the libav-users mailing list archive at Nabble.com.


More information about the Libav-user mailing list