Permalink
Browse files

avcodec: properly calculate DTS/PTS

So that we get proper timestamps.

Thank you GStreamer!

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
  • Loading branch information...
1 parent e0c34cd commit 4edaf96c468c240aee7f9311dd2d164af545fe1d @felipec committed Oct 20, 2012
Showing with 21 additions and 3 deletions.
  1. +14 −3 gstav_vdec.c
  2. +7 −0 gstav_vdec.h
View
@@ -139,7 +139,18 @@ static GstBuffer *convert_frame(struct obj *self, AVFrame *frame)
#if LIBAVCODEC_VERSION_MAJOR < 53
v = frame->reordered_opaque;
#else
- v = frame->pkt_pts;
+ if (frame->pkt_pts < self->last_pts)
+ self->bad_pts++;
+ if (frame->pkt_dts < self->last_dts)
+ self->bad_dts++;
+ self->last_pts = frame->pkt_pts;
+ self->last_dts = frame->pkt_dts;
+
+ /* Is GStreamer sending DTS or PTS? */
+ if (self->bad_pts <= self->bad_dts)
+ v = frame->pkt_pts;
+ else
+ v = frame->pkt_dts;
#endif
out_buf->timestamp = gstav_pts_to_timestamp(self->av_ctx, v);
@@ -209,9 +220,9 @@ pad_chain(GstPad *pad, GstBuffer *buf)
frame = avcodec_alloc_frame();
- pkt.pts = gstav_timestamp_to_pts(ctx, buf->timestamp);
+ pkt.dts = pkt.pts = gstav_timestamp_to_pts(ctx, buf->timestamp);
#if LIBAVCODEC_VERSION_MAJOR < 53
- ctx->reordered_opaque = pkt.pts;
+ ctx->reordered_opaque = pkt.dts;
#endif
g_mutex_lock(self->mutex);
View
@@ -26,6 +26,13 @@ struct gst_av_vdec {
bool initialized;
bool (*parse_func)(struct gst_av_vdec *vdec, GstBuffer *buf);
GMutex *mutex;
+
+ /* thank you GStreamer */
+ bool is_dts;
+ int64_t last_pts;
+ int64_t last_dts;
+ int bad_pts;
+ int bad_dts;
};
#endif /* GST_AV_VDEC_H */

0 comments on commit 4edaf96

Please sign in to comment.