[FFmpeg-devel] [PATCH 09/11] avformat/dashenc: use AVProducerReferenceTime side data to write the Producer Reference Time element

James Almer jamrial at gmail.com
Thu Oct 17 21:59:14 EEST 2019


This way it will be in sync with the first wallclock value in the bitstream.

Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavformat/dashenc.c | 33 ++++++++++++++++++++-------------
 1 file changed, 20 insertions(+), 13 deletions(-)

diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c
index 63a3853110..2ef0461c88 100644
--- a/libavformat/dashenc.c
+++ b/libavformat/dashenc.c
@@ -116,11 +116,11 @@ typedef struct OutputStream {
     char full_path[1024];
     char temp_path[1024];
     double availability_time_offset;
-    char producer_reference_time[100];
+    int64_t producer_reference_time;
+    char producer_reference_time_str[100];
     int total_pkt_size;
     int total_pkt_duration;
     int muxer_overhead;
-    int64_t start_time_us;
 } OutputStream;
 
 typedef struct DASHContext {
@@ -816,9 +816,9 @@ static int write_adaptation_set(AVFormatContext *s, AVIOContext *out, int as_ind
             avio_printf(out, "\t\t\t\t<AudioChannelConfiguration schemeIdUri=\"urn:mpeg:dash:23003:3:audio_channel_configuration:2011\" value=\"%d\" />\n",
                 s->streams[i]->codecpar->channels);
         }
-        if (!final && c->write_prft && os->producer_reference_time[0]) {
-            avio_printf(out, "\t\t\t\t<ProducerReferenceTime id=\"%d\" inband=\"true\" type=\"captured\" wallclockTime=\"%s\" presentationTime=\"%"PRId64"\">\n",
-                        i, os->producer_reference_time, c->presentation_time_offset);
+        if (!final && c->write_prft && os->producer_reference_time_str[0]) {
+            avio_printf(out, "\t\t\t\t<ProducerReferenceTime id=\"%d\" inband=\"true\" type=\"encoder\" wallclockTime=\"%s\" presentationTime=\"%"PRId64"\">\n",
+                        i, os->producer_reference_time_str, c->presentation_time_offset);
             avio_printf(out, "\t\t\t\t\t<UTCTiming schemeIdUri=\"urn:mpeg:dash:utc:http-xsdate:2014\" value=\"%s\"/>\n", c->utc_timing_url);
             avio_printf(out, "\t\t\t\t</ProducerReferenceTime>\n");
         }
@@ -1402,6 +1402,8 @@ static int dash_init(AVFormatContext *s)
                 av_dict_set(&opts, "movflags", "+frag_custom", AV_DICT_APPEND);
             if (c->frag_type == FRAG_TYPE_DURATION && os->frag_duration)
                 av_dict_set_int(&opts, "frag_duration", os->frag_duration, 0);
+            if (c->write_prft)
+                av_dict_set(&opts, "write_prft", "wallclock", 0);
         } else {
             av_dict_set_int(&opts, "cluster_time_limit", c->seg_duration / 1000, 0);
             av_dict_set_int(&opts, "cluster_size_limit", 5 * 1024 * 1024, 0); // set a large cluster size limit
@@ -1816,15 +1818,20 @@ static int dash_write_packet(AVFormatContext *s, AVPacket *pkt)
     }
 
     if (os->first_pts == AV_NOPTS_VALUE) {
+        int side_data_size;
+        AVProducerReferenceTime *prft = (AVProducerReferenceTime *)av_packet_get_side_data(pkt, AV_PKT_DATA_PRFT,
+                                                                                           &side_data_size);
+        if (prft && side_data_size == sizeof(AVProducerReferenceTime) && !prft->flags)
+            os->producer_reference_time = prft->wallclock;
         os->first_pts = pkt->pts;
-        os->start_time_us = av_gettime();
     }
     os->last_pts = pkt->pts;
 
     if (!c->availability_start_time[0]) {
-        c->start_time_s = os->start_time_us / 1000000;
+        int64_t start_time_us = av_gettime();
+        c->start_time_s = start_time_us / 1000000;
         format_date(c->availability_start_time,
-                    sizeof(c->availability_start_time), os->start_time_us);
+                    sizeof(c->availability_start_time), start_time_us);
     }
 
     if (!os->packets_written)
@@ -1881,11 +1888,11 @@ static int dash_write_packet(AVFormatContext *s, AVPacket *pkt)
         }
         }
 
-        if (c->write_prft && !os->producer_reference_time[0])
-            format_date(os->producer_reference_time,
-                        sizeof(os->producer_reference_time),
-                        os->start_time_us + (av_rescale_q(os->first_pts, st->time_base, AV_TIME_BASE_Q) -
-                                                          c->presentation_time_offset));
+        if (c->write_prft && os->producer_reference_time && !os->producer_reference_time_str[0])
+            format_date(os->producer_reference_time_str,
+                        sizeof(os->producer_reference_time_str),
+                        os->producer_reference_time + (av_rescale_q(os->first_pts, st->time_base, AV_TIME_BASE_Q) -
+                                                                    c->presentation_time_offset));
 
         if ((ret = dash_flush(s, 0, pkt->stream_index)) < 0)
             return ret;
-- 
2.23.0



More information about the ffmpeg-devel mailing list