<div dir="ltr"><div>Hello.<br></div>I stream video over udp and can't calculate pts/dts.<br><br><br clear="all"><div><div> outputStream_->sample_aspect_ratio.den = inputCodecContext_->sample_aspect_ratio.den;<br>
outputStream_->sample_aspect_ratio.num = inputCodecContext_->sample_aspect_ratio.num;<br> outputStream_->time_base.num = 1;<br> outputStream_->time_base.den = frame_rate_ * inputCodecContext_->ticks_per_frame;<br>
outputStream_->time_base.num = 1;<br> outputStream_->time_base.den = 1000;<br> outputStream_->r_frame_rate.num = frame_rate_;<br> outputStream_->r_frame_rate.den = 1;<br> outputStream_->avg_frame_rate.den = 1;<br>
outputStream_->avg_frame_rate.num = frame_rate_;<br> outputStream_->codec->bit_rate = inputCodecContext_->bit_rate;<br> //outputStream_->duration = (m_out_end_time - m_out_start_time) * 1000;<br> outputStream_->duration = inputFormatContext_->duration;<br>
outputFormatContext_->bit_rate = inputFormatContext_->bit_rate;<br><br> ret = avcodec_open2(outputStream_->codec, encoder_, NULL);<br><br> if (unlikely(ret < 0))<br> setErrorFromAvError(ret);<br>
<br><br> ret = avio_open(&outputFormatContext_->pb, url, AVIO_FLAG_WRITE);<br><br> if (unlikely(ret < 0))<br> setErrorFromAvError(ret);<br><br> is_open_ = true;<br><br> SwsContext* img_convert_context =<br>
sws_getCachedContext(NULL, inputCodecContext_->width, inputCodecContext_->height,<br> inputCodecContext_->pix_fmt,<br> outputStream_->codec->width, outputStream_->codec->height,<br>
AV_PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL);<br><br> if (img_convert_context != NULL)<br> swsContext_= boost::shared_ptr<SwsContext> (img_convert_context, sws_freeContext);<br>
<br> if (likely(is_open_)) {<br><br> AVFrame* frame = av_frame_alloc();<br> AVPacket packet, outputPacket;<br> outputPacket.size = 0;<br> outputPacket.data = NULL;<br> AVPicture pict;<br>
avpicture_alloc(&pict, AV_PIX_FMT_YUV420P, inputCodecContext_->width, inputCodecContext_->height);<br> //int vid_pts = 0;<br><br> while (av_read_frame(inputFormatContext_, &packet) >= 0) {<br>
outputStream_->pts = {0, 1, 1000};<br><br> if (packet.stream_index == 1) {<br> int frame_finished;<br> std::cout << "packet.dts " << packet.dts << " packet.pts " << packet.pts << std::endl;<br>
/*packet.dts = av_rescale_q_rnd(packet.dts,<br> inputFormatContext_->streams[inputVideoStreamIndex_]->time_base,<br> inputFormatContext_->streams[inputVideoStreamIndex_]->codec->time_base,<br>
static_cast<AVRounding> (AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));<br> packet.pts = av_rescale_q_rnd(packet.pts,<br> inputFormatContext_->streams[inputVideoStreamIndex_]->time_base,<br>
inputFormatContext_->streams[inputVideoStreamIndex_]->codec->time_base,<br> static_cast<AVRounding> (AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));*/<br> avcodec_decode_video2(inputCodecContext_.get(), frame, &frame_finished, &packet);<br>
<br> if (frame_finished) {<br> sws_scale(swsContext_.get(), frame->data, frame->linesize, 0, inputCodecContext_->height, pict.data, pict.linesize);<br> int got_packet = 0;<br>
int stream_index = 0;<br> outputPacket.stream_index = stream_index;<br> frame->pict_type = AV_PICTURE_TYPE_NONE;<br> av_init_packet(&outputPacket);<br>
outputPacket.stream_index = 0;<br> outputPacket.flags = AV_PKT_FLAG_KEY;<br> outputPacket.duration = packet.duration;<br><br> avcodec_encode_video2(outputStream_->codec, &outputPacket, frame, &got_packet);<br>
<br> std::cout << "packet.dts " << packet.dts << " packet.pts " << packet.pts << std::endl;<br> outputPacket.dts = av_rescale_q_rnd(outputPacket.dts,<br>
inputFormatContext_->streams[inputVideoStreamIndex_]->time_base,<br> inputFormatContext_->streams[inputVideoStreamIndex_]->codec->time_base,<br> static_cast<AVRounding> (AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));<br>
outputPacket.pts = av_rescale_q_rnd(outputPacket.pts,<br> inputFormatContext_->streams[inputVideoStreamIndex_]->time_base,<br> inputFormatContext_->streams[inputVideoStreamIndex_]->codec->time_base,<br>
static_cast<AVRounding> (AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));<br> outputPacket.duration = packet.duration;<br> //outputPacket.pts = vid_pts ++;<br>
<br> if (got_packet) {<br> outputPacket.stream_index = 0;<br> av_interleaved_write_frame(outputFormatContext_.get(), &outputPacket);<br> }<br>
<br> av_free_packet(&outputPacket);<br> outputPacket.size = 0;<br> outputPacket.data = NULL;<br> }<br> }<br> }<br><br> avpicture_free(&pict);<br>
<br> av_free_packet(&packet);<br> // Free the YUV frame<br> av_free(frame);<br> }<br><br></div><div>vlc shows video frames with losts order, ffplay shows file but not all<br></div><div><br>-- <br>
Regards,<br>Dmitry
</div></div></div>