flvmux: Add negative runtime DTS support
[platform/upstream/gst-plugins-good.git] / gst / flv / gstflvmux.c
index 2950a20..e13b24f 100644 (file)
@@ -26,8 +26,8 @@
  * <refsect2>
  * <title>Example launch line</title>
  * |[
- * gst-launch-1.0 -v filesrc location=/path/to/audio ! decodebin2 ! queue ! flvmux name=m ! filesink location=file.flv   filesrc location=/path/to/video ! decodebin2 ! queue ! m.
- * ]| This pipeline muxes an audio and video file into a single FLV file.
+ * gst-launch-1.0 -v flvmux name=mux ! filesink location=test.flv  audiotestsrc samplesperbuffer=44100 num-buffers=10 ! faac ! mux.  videotestsrc num-buffers=250 ! video/x-raw,framerate=25/1 ! x264enc ! mux.
+ * ]| This pipeline encodes a test audio and video stream and muxes both into an FLV file.
  * </refsect2>
  */
 
@@ -81,7 +81,7 @@ static GstStaticPadTemplate audiosink_templ = GST_STATIC_PAD_TEMPLATE ("audio",
         "audio/x-raw, format = (string) { U8, S16LE}, layout = (string) interleaved, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
         "audio/x-alaw, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
         "audio/x-mulaw, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
-        "audio/x-speex, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 };")
+        "audio/x-speex, channels = (int) 1, rate = (int) 16000;")
     );
 
 #define gst_flv_mux_parent_class parent_class
@@ -174,9 +174,7 @@ gst_flv_mux_class_init (GstFlvMuxClass * klass)
    *
    * If True, the output will be streaming friendly. (ie without indexes and
    * duration)
-   *
-   * Since: 0.10.24
-   **/
+   */
   g_object_class_install_property (gobject_class, PROP_STREAMABLE,
       g_param_spec_boolean ("streamable", "streamable",
           "If set to true, the output should be as if it is to be streamed "
@@ -255,6 +253,7 @@ gst_flv_mux_reset (GstElement * element)
   mux->have_audio = mux->have_video = FALSE;
   mux->duration = GST_CLOCK_TIME_NONE;
   mux->new_tags = FALSE;
+  mux->first_timestamp = GST_CLOCK_STIME_NONE;
 
   mux->state = GST_FLV_MUX_STATE_HEADER;
 
@@ -466,7 +465,8 @@ gst_flv_mux_audio_pad_setcaps (GstPad * pad, GstCaps * caps)
       else if (rate == 8000 && (cpad->audio_codec == 5
               || cpad->audio_codec == 14))
         cpad->rate = 0;
-      else if (rate == 16000 && cpad->audio_codec == 4)
+      else if (rate == 16000 && (cpad->audio_codec == 4
+              || cpad->audio_codec == 11))
         cpad->rate = 0;
       else
         ret = FALSE;
@@ -478,7 +478,7 @@ gst_flv_mux_audio_pad_setcaps (GstPad * pad, GstCaps * caps)
 
     if (gst_structure_get_int (s, "channels", &channels)) {
       if (cpad->audio_codec == 4 || cpad->audio_codec == 5
-          || cpad->audio_codec == 6)
+          || cpad->audio_codec == 6 || cpad->audio_codec == 11)
         cpad->channels = 0;
       else if (cpad->audio_codec == 10)
         cpad->channels = 1;
@@ -531,6 +531,8 @@ gst_flv_mux_reset_pad (GstFlvMux * mux, GstFlvPad * cpad, gboolean video)
   cpad->video_codec_data = NULL;
   cpad->video_codec = G_MAXUINT;
   cpad->last_timestamp = 0;
+  cpad->pts = GST_CLOCK_STIME_NONE;
+  cpad->dts = GST_CLOCK_STIME_NONE;
 }
 
 static GstPad *
@@ -1002,12 +1004,31 @@ gst_flv_mux_buffer_to_tag_internal (GstFlvMux * mux, GstBuffer * buffer,
   GstBuffer *tag;
   GstMapInfo map;
   guint size;
-  guint32 timestamp =
-      (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) ? GST_BUFFER_TIMESTAMP (buffer) /
-      GST_MSECOND : cpad->last_timestamp / GST_MSECOND;
+  guint32 pts, dts, cts;
   guint8 *data, *bdata;
   gsize bsize;
 
+  if (GST_CLOCK_STIME_IS_VALID (cpad->dts)) {
+    pts = dts = cpad->last_timestamp / GST_MSECOND;
+  } else {
+    pts = cpad->pts / GST_MSECOND;
+    dts = cpad->dts / GST_MSECOND;
+  }
+
+  /* Be safe in case TS are buggy */
+  if (pts > dts)
+    cts = pts - dts;
+  else
+    cts = 0;
+
+  /* Timestamp must start at zero */
+  if (GST_CLOCK_STIME_IS_VALID (mux->first_timestamp)) {
+    dts -= mux->first_timestamp / GST_MSECOND;
+    pts = dts + cts;
+  }
+
+  GST_LOG_OBJECT (mux, "got pts %i dts %i cts %i\n", pts, dts, cts);
+
   gst_buffer_map (buffer, &map, GST_MAP_READ);
   bdata = map.data;
   bsize = map.size;
@@ -1029,7 +1050,6 @@ gst_flv_mux_buffer_to_tag_internal (GstFlvMux * mux, GstBuffer * buffer,
   size += 4;
 
   _gst_buffer_new_and_alloc (size, &tag, &data);
-  GST_BUFFER_TIMESTAMP (tag) = timestamp * GST_MSECOND;
   memset (data, 0, size);
 
   data[0] = (cpad->video) ? 9 : 8;
@@ -1038,12 +1058,8 @@ gst_flv_mux_buffer_to_tag_internal (GstFlvMux * mux, GstBuffer * buffer,
   data[2] = ((size - 11 - 4) >> 8) & 0xff;
   data[3] = ((size - 11 - 4) >> 0) & 0xff;
 
-  /* wrap the timestamp every G_MAXINT32 miliseconds */
-  timestamp &= 0x7fffffff;
-  data[4] = (timestamp >> 16) & 0xff;
-  data[5] = (timestamp >> 8) & 0xff;
-  data[6] = (timestamp >> 0) & 0xff;
-  data[7] = (timestamp >> 24) & 0xff;
+  GST_WRITE_UINT24_BE (data + 4, dts);
+  data[7] = (((guint) dts) >> 24) & 0xff;
 
   data[8] = data[9] = data[10] = 0;
 
@@ -1056,11 +1072,14 @@ gst_flv_mux_buffer_to_tag_internal (GstFlvMux * mux, GstBuffer * buffer,
     data[11] |= cpad->video_codec & 0x0f;
 
     if (cpad->video_codec == 7) {
-      data[12] = is_codec_data ? 0 : 1;
-
-      /* FIXME: what to do about composition time */
-      data[13] = data[14] = data[15] = 0;
-
+      if (is_codec_data) {
+        data[12] = 0;
+        GST_WRITE_UINT24_BE (data + 13, 0);
+      } else {
+        /* ACV NALU */
+        data[12] = 1;
+        GST_WRITE_UINT24_BE (data + 13, cts);
+      }
       memcpy (data + 11 + 1 + 4, bdata, bsize);
     } else {
       memcpy (data + 11 + 1, bdata, bsize);
@@ -1084,8 +1103,9 @@ gst_flv_mux_buffer_to_tag_internal (GstFlvMux * mux, GstBuffer * buffer,
 
   GST_WRITE_UINT32_BE (data + size - 4, size - 4);
 
-  GST_BUFFER_TIMESTAMP (tag) = GST_BUFFER_TIMESTAMP (buffer);
-  GST_BUFFER_DURATION (tag) = GST_BUFFER_DURATION (buffer);
+  GST_BUFFER_PTS (tag) = GST_CLOCK_TIME_NONE;
+  GST_BUFFER_DTS (tag) = GST_CLOCK_TIME_NONE;
+  GST_BUFFER_DURATION (tag) = GST_CLOCK_TIME_NONE;
   GST_BUFFER_OFFSET (tag) = GST_BUFFER_OFFSET (buffer);
   GST_BUFFER_OFFSET_END (tag) = GST_BUFFER_OFFSET_END (buffer);
 
@@ -1268,11 +1288,10 @@ gst_flv_mux_update_index (GstFlvMux * mux, GstBuffer * buffer, GstFlvPad * cpad)
           GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)))
     return;
 
-  if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
+  if (GST_BUFFER_PTS_IS_VALID (buffer)) {
     GstFlvMuxIndexEntry *entry = g_slice_new (GstFlvMuxIndexEntry);
     entry->position = mux->byte_count;
-    entry->time =
-        gst_guint64_to_gdouble (GST_BUFFER_TIMESTAMP (buffer)) / GST_SECOND;
+    entry->time = gst_guint64_to_gdouble (GST_BUFFER_PTS (buffer)) / GST_SECOND;
     mux->index = g_list_prepend (mux->index, entry);
   }
 }
@@ -1282,6 +1301,7 @@ gst_flv_mux_write_buffer (GstFlvMux * mux, GstFlvPad * cpad, GstBuffer * buffer)
 {
   GstBuffer *tag;
   GstFlowReturn ret;
+  GstClockTime dts = GST_BUFFER_DTS (buffer);
 
   /* clipping function arranged for running_time */
 
@@ -1294,8 +1314,9 @@ gst_flv_mux_write_buffer (GstFlvMux * mux, GstFlvPad * cpad, GstBuffer * buffer)
 
   ret = gst_flv_mux_push (mux, tag);
 
-  if (ret == GST_FLOW_OK && GST_BUFFER_TIMESTAMP_IS_VALID (tag))
-    cpad->last_timestamp = GST_BUFFER_TIMESTAMP (tag);
+  if (ret == GST_FLOW_OK && GST_CLOCK_TIME_IS_VALID (dts))
+    cpad->last_timestamp = dts;
+
 
   return ret;
 }
@@ -1467,7 +1488,7 @@ gst_flv_mux_handle_buffer (GstCollectPads * pads, GstCollectData * cdata,
 {
   GstFlvMux *mux = GST_FLV_MUX (user_data);
   GstFlvPad *best;
-  GstClockTime best_time;
+  gint64 best_time;
   GstFlowReturn ret;
 
   if (mux->state == GST_FLV_MUX_STATE_HEADER) {
@@ -1481,6 +1502,11 @@ gst_flv_mux_handle_buffer (GstCollectPads * pads, GstCollectData * cdata,
     if (ret != GST_FLOW_OK)
       return ret;
     mux->state = GST_FLV_MUX_STATE_DATA;
+
+    if (GST_COLLECT_PADS_DTS_IS_VALID (cdata))
+      mux->first_timestamp = GST_COLLECT_PADS_DTS (cdata);
+    else
+      mux->first_timestamp = 0;
   }
 
   if (mux->new_tags) {
@@ -1493,15 +1519,27 @@ gst_flv_mux_handle_buffer (GstCollectPads * pads, GstCollectData * cdata,
   best = (GstFlvPad *) cdata;
   if (best) {
     g_assert (buffer);
-    best_time = GST_BUFFER_TIMESTAMP (buffer);
+    best->dts = GST_COLLECT_PADS_DTS (cdata);
+
+    if (GST_CLOCK_STIME_IS_VALID (best->dts))
+      best_time = best->dts - mux->first_timestamp;
+
+    if (GST_BUFFER_PTS_IS_VALID (buffer))
+      best->pts = GST_BUFFER_PTS (buffer);
+    else
+      best->pts = best->dts;
+
+    GST_LOG_OBJECT (mux, "got buffer PTS %" GST_TIME_FORMAT " DTS %"
+        GST_STIME_FORMAT "\n", GST_TIME_ARGS (best->pts),
+        GST_STIME_ARGS (best->dts));
   } else {
-    best_time = GST_CLOCK_TIME_NONE;
+    best_time = GST_CLOCK_STIME_NONE;
   }
 
   /* The FLV timestamp is an int32 field. For non-live streams error out if a
      bigger timestamp is seen, for live the timestamp will get wrapped in
      gst_flv_mux_buffer_to_tag */
-  if (!mux->streamable && GST_CLOCK_TIME_IS_VALID (best_time)
+  if (!mux->streamable && (GST_CLOCK_STIME_IS_VALID (best_time))
       && best_time / GST_MSECOND > G_MAXINT32) {
     GST_WARNING_OBJECT (mux, "Timestamp larger than FLV supports - EOS");
     gst_buffer_unref (buffer);