diff mbox series

[FFmpeg-devel,2/3] avformat/dashenc: write a capture time Producer Reference Time element when none is provided by the encoder

Message ID 20200218161336.1804-2-jamrial@gmail.com
State Accepted
Headers show
Series [FFmpeg-devel,1/3] avformat/dashenc: write the styp box when the first frame of a segment is ready | expand

Checks

Context Check Description
andriy/ffmpeg-patchwork success Make fate finished

Commit Message

James Almer Feb. 18, 2020, 4:13 p.m. UTC
This way, the element will be present in any scenario when the write_prft option is used.

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavformat/dashenc.c | 46 +++++++++++++++++++++++++++++++++----------
 1 file changed, 36 insertions(+), 10 deletions(-)

Comments

Anton Khirnov Feb. 19, 2020, 1:20 p.m. UTC | #1
Quoting James Almer (2020-02-18 17:13:35)
> +static int dash_parse_prft(DASHContext *c, AVPacket *pkt)
> +{
> +    OutputStream *os = &c->streams[pkt->stream_index];
> +    int side_data_size;
> +    AVProducerReferenceTime *prft;
> +
> +    if (!c->write_prft)
> +        return 0;
> +
> +    prft = (AVProducerReferenceTime *)av_packet_get_side_data(pkt, AV_PKT_DATA_PRFT, &side_data_size);
> +    if (!prft || side_data_size != sizeof(AVProducerReferenceTime) || prft->flags) {
> +        // No encoder generated AVProducerReferenceTime side data. Instead of letting the mov muxer
> +        // generate a capture-time one for the first packet, do it here so we can also use it for the
> +        // manifest.
> +        prft = (AVProducerReferenceTime *)av_packet_new_side_data(pkt, AV_PKT_DATA_PRFT,
> +                                                                  sizeof(AVProducerReferenceTime));
> +        if (!prft)
> +            return AVERROR(ENOMEM);
> +        prft->wallclock = av_gettime();
> +        prft->flags = 24;

It's pretty unclear what does this magic number mean.
The flags field in AVProducerReferenceTime is also undocumented.
James Almer Feb. 19, 2020, 1:34 p.m. UTC | #2
On 2/19/2020 10:20 AM, Anton Khirnov wrote:
> Quoting James Almer (2020-02-18 17:13:35)
>> +static int dash_parse_prft(DASHContext *c, AVPacket *pkt)
>> +{
>> +    OutputStream *os = &c->streams[pkt->stream_index];
>> +    int side_data_size;
>> +    AVProducerReferenceTime *prft;
>> +
>> +    if (!c->write_prft)
>> +        return 0;
>> +
>> +    prft = (AVProducerReferenceTime *)av_packet_get_side_data(pkt, AV_PKT_DATA_PRFT, &side_data_size);
>> +    if (!prft || side_data_size != sizeof(AVProducerReferenceTime) || prft->flags) {
>> +        // No encoder generated AVProducerReferenceTime side data. Instead of letting the mov muxer
>> +        // generate a capture-time one for the first packet, do it here so we can also use it for the
>> +        // manifest.
>> +        prft = (AVProducerReferenceTime *)av_packet_new_side_data(pkt, AV_PKT_DATA_PRFT,
>> +                                                                  sizeof(AVProducerReferenceTime));
>> +        if (!prft)
>> +            return AVERROR(ENOMEM);
>> +        prft->wallclock = av_gettime();
>> +        prft->flags = 24;
> 
> It's pretty unclear what does this magic number mean.
> The flags field in AVProducerReferenceTime is also undocumented.

It's the flags field in the isobmff spec for the prft box. I can add a
comment mentioning that.

0 and 24 are the only values accepted by the dash-ll spec. The former
signals the time a frame was feed to an encoder, and the latter signals
an arbitrary but consistent time (in this case, the time a packet was
feed to a packetizer).
diff mbox series

Patch

diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c
index 5c1d24d3e2..b910cc22d0 100644
--- a/libavformat/dashenc.c
+++ b/libavformat/dashenc.c
@@ -129,6 +129,7 @@  typedef struct OutputStream {
     char temp_path[1024];
     double availability_time_offset;
     int64_t producer_reference_time;
+    int producer_reference_time_flags;
     char producer_reference_time_str[100];
     int total_pkt_size;
     int64_t total_pkt_duration;
@@ -864,8 +865,8 @@  static int write_adaptation_set(AVFormatContext *s, AVIOContext *out, int as_ind
                 s->streams[i]->codecpar->channels);
         }
         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<ProducerReferenceTime id=\"%d\" inband=\"true\" type=\"%s\" wallclockTime=\"%s\" presentationTime=\"%"PRId64"\">\n",
+                        i, os->producer_reference_time_flags ? "captured" : "encoder", 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");
         }
@@ -2002,6 +2003,35 @@  static int dash_flush(AVFormatContext *s, int final, int stream)
     return ret;
 }
 
+static int dash_parse_prft(DASHContext *c, AVPacket *pkt)
+{
+    OutputStream *os = &c->streams[pkt->stream_index];
+    int side_data_size;
+    AVProducerReferenceTime *prft;
+
+    if (!c->write_prft)
+        return 0;
+
+    prft = (AVProducerReferenceTime *)av_packet_get_side_data(pkt, AV_PKT_DATA_PRFT, &side_data_size);
+    if (!prft || side_data_size != sizeof(AVProducerReferenceTime) || prft->flags) {
+        // No encoder generated AVProducerReferenceTime side data. Instead of letting the mov muxer
+        // generate a capture-time one for the first packet, do it here so we can also use it for the
+        // manifest.
+        prft = (AVProducerReferenceTime *)av_packet_new_side_data(pkt, AV_PKT_DATA_PRFT,
+                                                                  sizeof(AVProducerReferenceTime));
+        if (!prft)
+            return AVERROR(ENOMEM);
+        prft->wallclock = av_gettime();
+        prft->flags = 24;
+    }
+    os->producer_reference_time = prft->wallclock;
+    os->producer_reference_time_flags = prft->flags;
+    if (c->target_latency_refid < 0)
+        c->target_latency_refid = pkt->stream_index;
+
+    return 0;
+}
+
 static int dash_write_packet(AVFormatContext *s, AVPacket *pkt)
 {
     DASHContext *c = s->priv_data;
@@ -2034,15 +2064,11 @@  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;
-            if (c->target_latency_refid < 0)
-                c->target_latency_refid = pkt->stream_index;
-        }
         os->first_pts = pkt->pts;
+
+        ret = dash_parse_prft(c, pkt);
+        if (ret > 0)
+            return ret;
     }
     os->last_pts = pkt->pts;