Otherwise ffmpeg's rate control algorithm will not work correctly as
it is based on the absolute PTS values.
Fixes https://gitlab.freedesktop.org/gstreamer/gst-libav/-/issues/91
Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/3718>
+ ffmpegenc->pts_offset = GST_CLOCK_TIME_NONE;
ffmpegenc->opened = TRUE;
return TRUE;
ffmpegenc->opened = TRUE;
return TRUE;
picture->width = GST_VIDEO_FRAME_WIDTH (&buffer_info->vframe);
picture->height = GST_VIDEO_FRAME_HEIGHT (&buffer_info->vframe);
picture->width = GST_VIDEO_FRAME_WIDTH (&buffer_info->vframe);
picture->height = GST_VIDEO_FRAME_HEIGHT (&buffer_info->vframe);
- picture->pts =
- gst_ffmpeg_time_gst_to_ff (frame->pts /
- ffmpegenc->context->ticks_per_frame, ffmpegenc->context->time_base);
+ if (ffmpegenc->pts_offset == GST_CLOCK_TIME_NONE) {
+ ffmpegenc->pts_offset = frame->pts;
+ }
+
+ if (frame->pts == GST_CLOCK_TIME_NONE) {
+ picture->pts = AV_NOPTS_VALUE;
+ } else if (frame->pts < ffmpegenc->pts_offset) {
+ GST_ERROR_OBJECT (ffmpegenc, "PTS is going backwards");
+ picture->pts = AV_NOPTS_VALUE;
+ } else {
+ picture->pts =
+ gst_ffmpeg_time_gst_to_ff ((frame->pts - ffmpegenc->pts_offset) /
+ ffmpegenc->context->ticks_per_frame, ffmpegenc->context->time_base);
+ }
send_frame:
if (!picture) {
send_frame:
if (!picture) {
GST_VIDEO_CODEC_FRAME_UNSET_SYNC_POINT (frame);
}
GST_VIDEO_CODEC_FRAME_UNSET_SYNC_POINT (frame);
}
- frame->dts =
- gst_ffmpeg_time_ff_to_gst (pkt->dts, ffmpegenc->context->time_base);
+ if (pkt->dts != AV_NOPTS_VALUE) {
+ frame->dts =
+ gst_ffmpeg_time_ff_to_gst (pkt->dts + ffmpegenc->pts_offset,
+ ffmpegenc->context->time_base);
+ }
/* This will lose some precision compared to setting the PTS from the input
* buffer directly, but that way we're sure PTS and DTS are consistent, in
* particular DTS should always be <= PTS
*/
/* This will lose some precision compared to setting the PTS from the input
* buffer directly, but that way we're sure PTS and DTS are consistent, in
* particular DTS should always be <= PTS
*/
- frame->pts =
- gst_ffmpeg_time_ff_to_gst (pkt->pts, ffmpegenc->context->time_base);
+ if (pkt->pts != AV_NOPTS_VALUE) {
+ frame->pts =
+ gst_ffmpeg_time_ff_to_gst (pkt->pts + ffmpegenc->pts_offset,
+ ffmpegenc->context->time_base);
+ }
ret = gst_video_encoder_finish_frame (GST_VIDEO_ENCODER (ffmpegenc), frame);
ret = gst_video_encoder_finish_frame (GST_VIDEO_ENCODER (ffmpegenc), frame);
break;
} while (got_packet);
avcodec_flush_buffers (ffmpegenc->context);
break;
} while (got_packet);
avcodec_flush_buffers (ffmpegenc->context);
+ ffmpegenc->pts_offset = GST_CLOCK_TIME_NONE;
done:
/* FFMpeg will return AVERROR_EOF if it's internal was fully drained
done:
/* FFMpeg will return AVERROR_EOF if it's internal was fully drained
{
GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) encoder;
{
GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) encoder;
+ if (ffmpegenc->opened) {
avcodec_flush_buffers (ffmpegenc->context);
avcodec_flush_buffers (ffmpegenc->context);
+ ffmpegenc->pts_offset = GST_CLOCK_TIME_NONE;
+ }
AVCodecContext *context;
AVFrame *picture;
AVCodecContext *context;
AVFrame *picture;
+ GstClockTime pts_offset;
gboolean opened;
gboolean need_reopen;
gboolean discont;
gboolean opened;
gboolean need_reopen;
gboolean discont;