diff mbox

[FFmpeg-devel] movenc: calculate track_duration without packet duration

Message ID c91cbebd-beb6-2f4c-3420-1e62e43a13f1@gmail.com
State Superseded
Headers show

Commit Message

Alfred E. Heggestad June 17, 2019, 8:07 a.m. UTC
From c69b63a7af5531257753754e64ac33b7ef530e75 Mon Sep 17 00:00:00 2001
From: "Alfred E. Heggestad" <alfred.heggestad@gmail.com>
Date: Mon, 17 Jun 2019 10:04:08 +0200
Subject: [PATCH] movenc: calculate track_duration without packet duration

---
  libavformat/movenc.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

      if (pkt->pts == AV_NOPTS_VALUE) {

Comments

Gyan Doshi June 17, 2019, 8:23 a.m. UTC | #1
On 17-06-2019 01:37 PM, Alfred E. Heggestad wrote:
> From c69b63a7af5531257753754e64ac33b7ef530e75 Mon Sep 17 00:00:00 2001
> From: "Alfred E. Heggestad" <alfred.heggestad@gmail.com>
> Date: Mon, 17 Jun 2019 10:04:08 +0200
> Subject: [PATCH] movenc: calculate track_duration without packet duration
>
> ---
>  libavformat/movenc.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/libavformat/movenc.c b/libavformat/movenc.c
> index 46d314ff17..fa5833962b 100644
> --- a/libavformat/movenc.c
> +++ b/libavformat/movenc.c
> @@ -5486,7 +5486,7 @@ int ff_mov_write_packet(AVFormatContext *s, 
> AVPacket *pkt)
>                     "this case.\n",
>                     pkt->stream_index, pkt->dts);
>      }
> -    trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
> +    trk->track_duration = pkt->dts - trk->start_dts;

Why?

> trk->last_sample_is_subtitle_end = 0;
>
>      if (pkt->pts == AV_NOPTS_VALUE) {

Gyan
Alfred E. Heggestad June 18, 2019, 8:30 a.m. UTC | #2
On 17/06/2019 10:23, Gyan wrote:
> 
> 
> On 17-06-2019 01:37 PM, Alfred E. Heggestad wrote:
>> From c69b63a7af5531257753754e64ac33b7ef530e75 Mon Sep 17 00:00:00 2001
>> From: "Alfred E. Heggestad" <alfred.heggestad@gmail.com>
>> Date: Mon, 17 Jun 2019 10:04:08 +0200
>> Subject: [PATCH] movenc: calculate track_duration without packet duration
>>
>> ---
>>  libavformat/movenc.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/libavformat/movenc.c b/libavformat/movenc.c
>> index 46d314ff17..fa5833962b 100644
>> --- a/libavformat/movenc.c
>> +++ b/libavformat/movenc.c
>> @@ -5486,7 +5486,7 @@ int ff_mov_write_packet(AVFormatContext *s, 
>> AVPacket *pkt)
>>                     "this case.\n",
>>                     pkt->stream_index, pkt->dts);
>>      }
>> -    trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
>> +    trk->track_duration = pkt->dts - trk->start_dts;
> 
> Why?
> 
>> trk->last_sample_is_subtitle_end = 0;
>>
>>      if (pkt->pts == AV_NOPTS_VALUE) {
> 

the background for this change is the check done in check_pkt():

    if (trk->entry) {
         ref = trk->cluster[trk->entry - 1].dts;
     } else if (   trk->start_dts != AV_NOPTS_VALUE
                && !trk->frag_discont) {
         ref = trk->start_dts + trk->track_duration;
     } else
         ref = pkt->dts; // Skip tests for the first packet

     if (trk->dts_shift != AV_NOPTS_VALUE) {
         /* With negative CTS offsets we have set an offset to the DTS,
          * reverse this for the check. */
         ref -= trk->dts_shift;
     }

     duration = pkt->dts - ref;
     if (pkt->dts < ref || duration >= INT_MAX) {
         av_log(s, AV_LOG_ERROR, "Application provided duration: 
%"PRId64" / timestamp: %"PRId64" is out of range for mov/mp4 format\n",
             duration, pkt->dts
         );

         pkt->dts = ref + 1;
         pkt->pts = AV_NOPTS_VALUE;
     }



it requires that the DTS of the packet is larger than
"ref" which in some cases is equal to track_duration.


line 5015:

      track->track_duration = pkt.dts - track->start_dts;

line 5489:

      trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;


line 5615:

      trk->track_duration = pkt->dts - trk->start_dts;



setting track_duration is inconsistent; some times it includes
duration and some times not.


here is an example with a stream of AVPacket's that increment
the DTS by approx. 10 (and duration is set to 0):

pkt:  dts:
  1     0
  2    10
  3    20
  4    30
  5    40
  6    50


after packet 6 has arrived, the "ref" value in check_pkt will now
be calculated to 50 + 10 = 60. the code assumes a duration of 10.

when packet 7 arrives it has a DTS of 59 (which is valid).
but this packet will fail in check_pkt and print a warning,
because 59 < 60.






/alfred
Gyan Doshi June 19, 2019, 5:43 a.m. UTC | #3
On 18-06-2019 02:00 PM, Alfred E. Heggestad wrote:
> On 17/06/2019 10:23, Gyan wrote:
>>
>>
>> On 17-06-2019 01:37 PM, Alfred E. Heggestad wrote:
>>> From c69b63a7af5531257753754e64ac33b7ef530e75 Mon Sep 17 00:00:00 2001
>>> From: "Alfred E. Heggestad" <alfred.heggestad@gmail.com>
>>> Date: Mon, 17 Jun 2019 10:04:08 +0200
>>> Subject: [PATCH] movenc: calculate track_duration without packet 
>>> duration
>>>
>>> ---
>>>  libavformat/movenc.c | 2 +-
>>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/libavformat/movenc.c b/libavformat/movenc.c
>>> index 46d314ff17..fa5833962b 100644
>>> --- a/libavformat/movenc.c
>>> +++ b/libavformat/movenc.c
>>> @@ -5486,7 +5486,7 @@ int ff_mov_write_packet(AVFormatContext *s, 
>>> AVPacket *pkt)
>>>                     "this case.\n",
>>>                     pkt->stream_index, pkt->dts);
>>>      }
>>> -    trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
>>> +    trk->track_duration = pkt->dts - trk->start_dts;
>>
>> Why?
>>
>>> trk->last_sample_is_subtitle_end = 0;
>>>
>>>      if (pkt->pts == AV_NOPTS_VALUE) {
>>
>
> the background for this change is the check done in check_pkt():
>
>    if (trk->entry) {
>         ref = trk->cluster[trk->entry - 1].dts;
>     } else if (   trk->start_dts != AV_NOPTS_VALUE
>                && !trk->frag_discont) {
>         ref = trk->start_dts + trk->track_duration;
>     } else
>         ref = pkt->dts; // Skip tests for the first packet
>
>     if (trk->dts_shift != AV_NOPTS_VALUE) {
>         /* With negative CTS offsets we have set an offset to the DTS,
>          * reverse this for the check. */
>         ref -= trk->dts_shift;
>     }
>
>     duration = pkt->dts - ref;
>     if (pkt->dts < ref || duration >= INT_MAX) {
>         av_log(s, AV_LOG_ERROR, "Application provided duration: 
> %"PRId64" / timestamp: %"PRId64" is out of range for mov/mp4 format\n",
>             duration, pkt->dts
>         );
>
>         pkt->dts = ref + 1;
>         pkt->pts = AV_NOPTS_VALUE;
>     }
>
>
>
> it requires that the DTS of the packet is larger than
> "ref" which in some cases is equal to track_duration.
>
>
> line 5015:
>
>      track->track_duration = pkt.dts - track->start_dts;
>
> line 5489:
>
>      trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
>
>
> line 5615:
>
>      trk->track_duration = pkt->dts - trk->start_dts;
>
>
>
> setting track_duration is inconsistent; some times it includes
> duration and some times not.

It may be best to check the commits for these assignments to see if the 
inconsistency is deliberate.
The track duration is written into the media header box for the track. I 
also see it being used elsewhere to adjust dts. Do those roles remain 
intact?

Does FATE pass?

Gyan

> here is an example with a stream of AVPacket's that increment
> the DTS by approx. 10 (and duration is set to 0):
>
> pkt:  dts:
>  1     0
>  2    10
>  3    20
>  4    30
>  5    40
>  6    50
>
>
> after packet 6 has arrived, the "ref" value in check_pkt will now
> be calculated to 50 + 10 = 60. the code assumes a duration of 10.
>
> when packet 7 arrives it has a DTS of 59 (which is valid).
> but this packet will fail in check_pkt and print a warning,
> because 59 < 60.
Derek Buitenhuis June 19, 2019, 1:36 p.m. UTC | #4
On 19/06/2019 06:43, Gyan wrote:
>> setting track_duration is inconsistent; some times it includes
>> duration and some times not.
> It may be best to check the commits for these assignments to see if the 
> inconsistency is deliberate.
> The track duration is written into the media header box for the track. I 
> also see it being used elsewhere to adjust dts. Do those roles remain 
> intact?
> 
> Does FATE pass?

Wouldn't the correct fix be to fix the places where duration is *not*
used?

Writing the track duration without taking into account the actual
packet duration of the last packet is just wrong. That's not the
track's duration, and is in fact very problematic for low frame
rate files, such a slide shows; QuickTime will cut off at the
end of the media and track duration, dropping possibly a whole
slide, for example.

- Derek
Alfred E. Heggestad June 19, 2019, 1:51 p.m. UTC | #5
On 19/06/2019 15:36, Derek Buitenhuis wrote:
> On 19/06/2019 06:43, Gyan wrote:
>>> setting track_duration is inconsistent; some times it includes
>>> duration and some times not.
>> It may be best to check the commits for these assignments to see if the
>> inconsistency is deliberate.
>> The track duration is written into the media header box for the track. I
>> also see it being used elsewhere to adjust dts. Do those roles remain
>> intact?
>>
>> Does FATE pass?
> 
> Wouldn't the correct fix be to fix the places where duration is *not*
> used?
> 
> Writing the track duration without taking into account the actual
> packet duration of the last packet is just wrong. That's not the
> track's duration, and is in fact very problematic for low frame
> rate files, such a slide shows; QuickTime will cut off at the
> end of the media and track duration, dropping possibly a whole
> slide, for example.
> 

Hi,

thanks for your comments. I agree that track_duration should
include duration, I will take a look at this tomorrow.


but why do you require that dts > prev_dts + prev_dur when dur
is unknown ? I think this check is too strict. a better check
would be:


    dts > prev_dts


a very simple example of how to demonstrate this:

     $ ffmpeg -y -loglevel info \
	-listen 1 -i rtmp://127.0.0.1:1935/live \
	-f dash out.mpd

and then use ffmpeg to send FLV/RTMP stream to 127.0.0.1

after some seconds ffmpeg prints this warning:

[mp4 @ 0x7faffe065600] Application provided duration: -2 / timestamp: 
1761244 is out of range for mov/mp4 format




/alfred
diff mbox

Patch

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 46d314ff17..fa5833962b 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -5486,7 +5486,7 @@  int ff_mov_write_packet(AVFormatContext *s, 
AVPacket *pkt)
                     "this case.\n",
                     pkt->stream_index, pkt->dts);
      }
-    trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
+    trk->track_duration = pkt->dts - trk->start_dts;
      trk->last_sample_is_subtitle_end = 0;