[Libav-user] Video frames pts/dts

Dmitry Adjiev adjiev.dmitry at gmail.com
Tue Aug 5 04:36:58 CEST 2014


Hello.
I stream video over udp and can't calculate pts/dts.


 outputStream_->sample_aspect_ratio.den =
inputCodecContext_->sample_aspect_ratio.den;
    outputStream_->sample_aspect_ratio.num =
inputCodecContext_->sample_aspect_ratio.num;
    outputStream_->time_base.num = 1;
    outputStream_->time_base.den = frame_rate_ *
inputCodecContext_->ticks_per_frame;
    outputStream_->time_base.num = 1;
    outputStream_->time_base.den = 1000;
    outputStream_->r_frame_rate.num = frame_rate_;
    outputStream_->r_frame_rate.den = 1;
    outputStream_->avg_frame_rate.den = 1;
    outputStream_->avg_frame_rate.num = frame_rate_;
    outputStream_->codec->bit_rate = inputCodecContext_->bit_rate;
    //outputStream_->duration = (m_out_end_time - m_out_start_time) * 1000;
    outputStream_->duration = inputFormatContext_->duration;
    outputFormatContext_->bit_rate = inputFormatContext_->bit_rate;

    ret = avcodec_open2(outputStream_->codec, encoder_, NULL);

    if (unlikely(ret < 0))
        setErrorFromAvError(ret);


    ret = avio_open(&outputFormatContext_->pb, url, AVIO_FLAG_WRITE);

    if (unlikely(ret < 0))
        setErrorFromAvError(ret);

    is_open_ = true;

    SwsContext* img_convert_context =
                sws_getCachedContext(NULL, inputCodecContext_->width,
inputCodecContext_->height,
                                     inputCodecContext_->pix_fmt,
                                     outputStream_->codec->width,
outputStream_->codec->height,
                                     AV_PIX_FMT_YUV420P, SWS_FAST_BILINEAR,
NULL, NULL, NULL);

    if (img_convert_context != NULL)
        swsContext_= boost::shared_ptr<SwsContext> (img_convert_context,
sws_freeContext);

    if (likely(is_open_)) {

        AVFrame* frame = av_frame_alloc();
        AVPacket packet, outputPacket;
        outputPacket.size = 0;
        outputPacket.data = NULL;
        AVPicture pict;
        avpicture_alloc(&pict, AV_PIX_FMT_YUV420P,
inputCodecContext_->width, inputCodecContext_->height);
        //int vid_pts = 0;

        while (av_read_frame(inputFormatContext_, &packet) >= 0) {
            outputStream_->pts = {0, 1, 1000};

            if (packet.stream_index == 1) {
                int frame_finished;
                std::cout << "packet.dts " << packet.dts << " packet.pts "
<< packet.pts << std::endl;
                /*packet.dts = av_rescale_q_rnd(packet.dts,

inputFormatContext_->streams[inputVideoStreamIndex_]->time_base,

inputFormatContext_->streams[inputVideoStreamIndex_]->codec->time_base,
                        static_cast<AVRounding>
(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
                packet.pts = av_rescale_q_rnd(packet.pts,

inputFormatContext_->streams[inputVideoStreamIndex_]->time_base,

inputFormatContext_->streams[inputVideoStreamIndex_]->codec->time_base,
                        static_cast<AVRounding>
(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));*/
                avcodec_decode_video2(inputCodecContext_.get(), frame,
&frame_finished, &packet);

                if (frame_finished) {
                    sws_scale(swsContext_.get(), frame->data,
frame->linesize, 0, inputCodecContext_->height, pict.data, pict.linesize);
                    int got_packet = 0;
                    int stream_index = 0;
                    outputPacket.stream_index = stream_index;
                    frame->pict_type = AV_PICTURE_TYPE_NONE;
                    av_init_packet(&outputPacket);
                    outputPacket.stream_index = 0;
                    outputPacket.flags = AV_PKT_FLAG_KEY;
                    outputPacket.duration = packet.duration;

                    avcodec_encode_video2(outputStream_->codec,
&outputPacket, frame, &got_packet);

                    std::cout << "packet.dts " << packet.dts << "
packet.pts " << packet.pts << std::endl;
                    outputPacket.dts = av_rescale_q_rnd(outputPacket.dts,

inputFormatContext_->streams[inputVideoStreamIndex_]->time_base,

inputFormatContext_->streams[inputVideoStreamIndex_]->codec->time_base,
                            static_cast<AVRounding>
(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
                    outputPacket.pts = av_rescale_q_rnd(outputPacket.pts,

inputFormatContext_->streams[inputVideoStreamIndex_]->time_base,

inputFormatContext_->streams[inputVideoStreamIndex_]->codec->time_base,
                            static_cast<AVRounding>
(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
                    outputPacket.duration = packet.duration;
                    //outputPacket.pts = vid_pts ++;

                    if (got_packet) {
                        outputPacket.stream_index = 0;

av_interleaved_write_frame(outputFormatContext_.get(), &outputPacket);
                    }

                    av_free_packet(&outputPacket);
                    outputPacket.size = 0;
                    outputPacket.data = NULL;
                }
            }
        }

        avpicture_free(&pict);

        av_free_packet(&packet);
        // Free the YUV frame
        av_free(frame);
    }

vlc shows video frames with losts order, ffplay shows file but not all

-- 
Regards,
Dmitry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20140805/9ac12323/attachment.html>


More information about the Libav-user mailing list