加载中…
个人资料
南冠彤
南冠彤
  • 博客等级:
  • 博客积分:0
  • 博客访问:411,015
  • 关注人气:59
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

[转载]gstreamer1.0的mpegtsmux的pcr处理的补丁

(2014-10-09 23:40:12)
标签:

转载

分类: 音视频
mpegtsmux处理pcr过于粗糙,对其进行了一些修改,patch文件如下:


diff --git a/gst/mpegtsmux/mpegtsmux.c b/gst/mpegtsmux/mpegtsmux.c
index aebe23a..d092a18 100644
--- a/gst/mpegtsmux/mpegtsmux.c
+++ b/gst/mpegtsmux/mpegtsmux.c
@@ -1105,7 +1105,7 @@ mpegtsmux_collected_buffer (GstCollectPads * pads, GstCollectData * data,
     }
   }
 
-  if (G_UNLIKELY (prog->pcr_stream == NULL)) {
+  if (G_UNLIKELY (prog->pcr_stream == NULL) && best->stream->is_video_stream) {
     
     GST_DEBUG_OBJECT (COLLECT_DATA_PAD (best),
         "Use stream (pid=%d) from pad as PCR for program (prog_id = %d)",
@@ -1163,6 +1163,7 @@ mpegtsmux_collected_buffer (GstCollectPads * pads, GstCollectData * data,
     mux->last_ts =
         GST_CLOCK_TIME_IS_VALID (best->last_dts) ? best->last_dts : best->
         last_pts;
+    best->stream->cur_buffer_duration = GST_BUFFER_DURATION(buf);
   }
 
   mux->is_delta = delta;
diff --git a/gst/mpegtsmux/tsmux/tsmux.c b/gst/mpegtsmux/tsmux/tsmux.c
index e7edf57..86dfaf0 100644
--- a/gst/mpegtsmux/tsmux/tsmux.c
+++ b/gst/mpegtsmux/tsmux/tsmux.c
@@ -748,6 +748,11 @@ tsmux_write_ts_header (guint8 * buf, TsMuxPacketInfo * pi,
   return TRUE;
 }
 
+#define MPEGTIME_TO_GSTTIME(time) (gst_util_uint64_scale ((time), 
+                        GST_MSECOND/10, 9LL))
+
+#define GSTTIME_TO_MPEGTIME(time) (gst_util_uint64_scale ((time), 
+                        9LL, GST_MSECOND/10))
 
       cur_pts += CLOCK_BASE;
-      cur_pcr = (cur_pts - TSMUX_PCR_OFFSET) *
-          (TSMUX_SYS_CLOCK_FREQ / TSMUX_CLOCK_FREQ);
     }
 
+    if (stream->cur_pcr == -1)
+      stream->cur_pcr = MPEGTIME_TO_GSTTIME (CLOCK_BASE);
+
+    if (stream->last_buffer_pcr == -1)
+      stream->last_buffer_pcr = MPEGTIME_TO_GSTTIME (CLOCK_BASE);
+
+    cur_pcr = (GSTTIME_TO_MPEGTIME (stream->cur_pcr) - TSMUX_PCR_OFFSET) *
+          (TSMUX_SYS_CLOCK_FREQ / TSMUX_CLOCK_FREQ);
+
+      //GST_ERROR ("cur_pcr %" G_GINT64_FORMAT " stream->cur_pcr %" G_GINT64_FORMAT, cur_pcr, stream->cur_pcr);
     
     if (stream->last_pcr == -1 ||
-        (cur_pcr - stream->last_pcr >
+        (cur_pcr - stream->last_pcr >=
             (TSMUX_SYS_CLOCK_FREQ / TSMUX_DEFAULT_PCR_FREQ))) {
 
+      //GST_ERROR ("cur_pcr %" G_GINT64_FORMAT "cur-last %" G_GINT64_FORMAT, cur_pcr, cur_pcr - stream->last_pcr);
       stream->pi.flags |=
           TSMUX_PACKET_FLAG_ADAPTATION | TSMUX_PACKET_FLAG_WRITE_PCR;
       stream->pi.pcr = cur_pcr;
@@ -866,6 +879,16 @@ tsmux_write_stream_packet (TsMux * mux, TsMuxStream * stream)
   
   stream->pi.flags &= TSMUX_PACKET_FLAG_PES_FULL_HEADER;
 
+  if (tsmux_stream_is_pcr (stream)) {
+    if (stream->bytes_avail == 0) {
+      stream->last_buffer_pcr += stream->cur_buffer_duration;
+      stream->cur_pcr = stream->last_buffer_pcr;
+      //GST_ERROR ("cur_pcr %" G_GINT64_FORMAT, stream->cur_pcr);
+    } else
+      stream->cur_pcr = stream->last_buffer_pcr +
+          (stream->cur_buffer_duration * stream->cur_buffer_consumed) / (stream->bytes_avail + stream->cur_buffer_consumed);
+  }
+
   return res;
 
   
diff --git a/gst/mpegtsmux/tsmux/tsmuxstream.c b/gst/mpegtsmux/tsmux/tsmuxstream.c
index 20378b5..89c0bf9 100644
--- a/gst/mpegtsmux/tsmux/tsmuxstream.c
+++ b/gst/mpegtsmux/tsmux/tsmuxstream.c
@@ -197,6 +197,8 @@ tsmux_stream_new (guint16 pid, TsMuxStreamType stream_type)
 
   stream->pcr_ref = 0;
   stream->last_pcr = -1;
+  stream->cur_pcr = -1;
+  stream->last_buffer_pcr = -1;
 
   return stream;
 }
diff --git a/gst/mpegtsmux/tsmux/tsmuxstream.h b/gst/mpegtsmux/tsmux/tsmuxstream.h
index 27c0e79..401ba74 100644
--- a/gst/mpegtsmux/tsmux/tsmuxstream.h
+++ b/gst/mpegtsmux/tsmux/tsmuxstream.h
@@ -173,6 +173,7 @@ struct TsMuxStream {
    * and amount already consumed */
   TsMuxStreamBuffer *cur_buffer;
   guint32 cur_buffer_consumed;
+  gint64 cur_buffer_duration;
 
   
   TsMuxStreamBufferReleaseFunc buffer_release;
@@ -197,6 +198,9 @@ struct TsMuxStream {
   gint   pcr_ref;
   
   gint64 last_pcr;
+  
+  gint64 last_buffer_pcr;
+  gint64 cur_pcr;
 
   



用如下命令打补丁:
patch -p 1 < gstreamer-plugin-mpegtsmux-pcr.diff

0

  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有