[01/12] movenc: Add a separate start_pts

Message ID 1327065643-34065-1-git-send-email-martin@martin.st
State Committed
Commit 6ca3856894c8838096595f7e147df8f73169dfe2
Headers show

Commit Message

Martin Storsjö Jan. 20, 2012, 1:20 p.m.
This fixes calculation of trackDuration if the MOVIentry array
is cleared. This is required by the fragmentation support in the
next patch.
---
 libavformat/movenc.c |    7 +++++--
 libavformat/movenc.h |    1 +
 2 files changed, 6 insertions(+), 2 deletions(-)

Comments

Martin Storsjö Jan. 25, 2012, 9:28 a.m. | #1
On Fri, 20 Jan 2012, Martin Storsjö wrote:

> This fixes calculation of trackDuration if the MOVIentry array
> is cleared. This is required by the fragmentation support in the
> next patch.
> ---
> libavformat/movenc.c |    7 +++++--
> libavformat/movenc.h |    1 +
> 2 files changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/libavformat/movenc.c b/libavformat/movenc.c
> index 85b5667..d113bff 100644
> --- a/libavformat/movenc.c
> +++ b/libavformat/movenc.c
> @@ -987,7 +987,7 @@ static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
>         stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
>         for (i=0; i<track->entry; i++) {
>             int64_t duration = i + 1 == track->entry ?
> -                track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
> +                track->trackDuration - track->cluster[i].dts + track->start_dts : /* readjusting */
>                 track->cluster[i+1].dts - track->cluster[i].dts;
>             if (i && duration == stts_entries[entries].duration) {
>                 stts_entries[entries].count++; /* compress */
> @@ -2104,7 +2104,9 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
>     trk->cluster[trk->entry].size = size;
>     trk->cluster[trk->entry].entries = samplesInChunk;
>     trk->cluster[trk->entry].dts = pkt->dts;
> -    trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
> +    if (trk->start_dts == AV_NOPTS_VALUE)
> +        trk->start_dts = pkt->dts;
> +    trk->trackDuration = pkt->dts - trk->start_dts + pkt->duration;
>
>     if (pkt->pts == AV_NOPTS_VALUE) {
>         av_log(s, AV_LOG_WARNING, "pts has no value\n");
> @@ -2253,6 +2255,7 @@ static int mov_write_header(AVFormatContext *s)
>         /* If hinting of this track is enabled by a later hint track,
>          * this is updated. */
>         track->hint_track = -1;
> +        track->start_dts = AV_NOPTS_VALUE;
>         if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
>             if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
>                 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
> diff --git a/libavformat/movenc.h b/libavformat/movenc.h
> index 5ca5631..4d8d11a 100644
> --- a/libavformat/movenc.h
> +++ b/libavformat/movenc.h
> @@ -89,6 +89,7 @@ typedef struct MOVIndex {
>     int         height; ///< active picture (w/o VBI) height for D-10/IMX
>     uint32_t    tref_tag;
>     int         tref_id; ///< trackID of the referenced track
> +    int64_t     start_dts;
>
>     int         hint_track;   ///< the track that hints this track, -1 if no hint track is set
>     int         src_track;    ///< the track that this hint track describes
> -- 
> 1.7.3.1

I think this one was OKd by Ronald on irc the other day, but I'm not sure 
of this one - review/ok wanted.

// Martin
Ronald Bultje Jan. 25, 2012, 9:31 a.m. | #2
Hi,

On Wed, Jan 25, 2012 at 5:28 PM, Martin Storsjö <martin@martin.st> wrote:
> On Fri, 20 Jan 2012, Martin Storsjö wrote:
>
>> This fixes calculation of trackDuration if the MOVIentry array
>> is cleared. This is required by the fragmentation support in the
>> next patch.
>> ---
>> libavformat/movenc.c |    7 +++++--
>> libavformat/movenc.h |    1 +
>> 2 files changed, 6 insertions(+), 2 deletions(-)
>>
>> diff --git a/libavformat/movenc.c b/libavformat/movenc.c
>> index 85b5667..d113bff 100644
>> --- a/libavformat/movenc.c
>> +++ b/libavformat/movenc.c
>> @@ -987,7 +987,7 @@ static int mov_write_stts_tag(AVIOContext *pb,
>> MOVTrack *track)
>>        stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /*
>> worst case */
>>        for (i=0; i<track->entry; i++) {
>>            int64_t duration = i + 1 == track->entry ?
>> -                track->trackDuration - track->cluster[i].dts +
>> track->cluster[0].dts : /* readjusting */
>> +                track->trackDuration - track->cluster[i].dts +
>> track->start_dts : /* readjusting */
>>                track->cluster[i+1].dts - track->cluster[i].dts;
>>            if (i && duration == stts_entries[entries].duration) {
>>                stts_entries[entries].count++; /* compress */
>> @@ -2104,7 +2104,9 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket
>> *pkt)
>>    trk->cluster[trk->entry].size = size;
>>    trk->cluster[trk->entry].entries = samplesInChunk;
>>    trk->cluster[trk->entry].dts = pkt->dts;
>> -    trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
>> +    if (trk->start_dts == AV_NOPTS_VALUE)
>> +        trk->start_dts = pkt->dts;
>> +    trk->trackDuration = pkt->dts - trk->start_dts + pkt->duration;
>>
>>    if (pkt->pts == AV_NOPTS_VALUE) {
>>        av_log(s, AV_LOG_WARNING, "pts has no value\n");
>> @@ -2253,6 +2255,7 @@ static int mov_write_header(AVFormatContext *s)
>>        /* If hinting of this track is enabled by a later hint track,
>>         * this is updated. */
>>        track->hint_track = -1;
>> +        track->start_dts = AV_NOPTS_VALUE;
>>        if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
>>            if (track->tag == MKTAG('m','x','3','p') || track->tag ==
>> MKTAG('m','x','3','n') ||
>>                track->tag == MKTAG('m','x','4','p') || track->tag ==
>> MKTAG('m','x','4','n') ||
>> diff --git a/libavformat/movenc.h b/libavformat/movenc.h
>> index 5ca5631..4d8d11a 100644
>> --- a/libavformat/movenc.h
>> +++ b/libavformat/movenc.h
>> @@ -89,6 +89,7 @@ typedef struct MOVIndex {
>>    int         height; ///< active picture (w/o VBI) height for D-10/IMX
>>    uint32_t    tref_tag;
>>    int         tref_id; ///< trackID of the referenced track
>> +    int64_t     start_dts;
>>
>>    int         hint_track;   ///< the track that hints this track, -1 if
>> no hint track is set
>>    int         src_track;    ///< the track that this hint track describes
>> --
>> 1.7.3.1
>
>
> I think this one was OKd by Ronald on irc the other day, but I'm not sure of
> this one - review/ok wanted.

Yeah I said OK.

Ronald

Patch

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 85b5667..d113bff 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -987,7 +987,7 @@  static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
         stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
         for (i=0; i<track->entry; i++) {
             int64_t duration = i + 1 == track->entry ?
-                track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
+                track->trackDuration - track->cluster[i].dts + track->start_dts : /* readjusting */
                 track->cluster[i+1].dts - track->cluster[i].dts;
             if (i && duration == stts_entries[entries].duration) {
                 stts_entries[entries].count++; /* compress */
@@ -2104,7 +2104,9 @@  int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
     trk->cluster[trk->entry].size = size;
     trk->cluster[trk->entry].entries = samplesInChunk;
     trk->cluster[trk->entry].dts = pkt->dts;
-    trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
+    if (trk->start_dts == AV_NOPTS_VALUE)
+        trk->start_dts = pkt->dts;
+    trk->trackDuration = pkt->dts - trk->start_dts + pkt->duration;
 
     if (pkt->pts == AV_NOPTS_VALUE) {
         av_log(s, AV_LOG_WARNING, "pts has no value\n");
@@ -2253,6 +2255,7 @@  static int mov_write_header(AVFormatContext *s)
         /* If hinting of this track is enabled by a later hint track,
          * this is updated. */
         track->hint_track = -1;
+        track->start_dts = AV_NOPTS_VALUE;
         if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
             if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
                 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
diff --git a/libavformat/movenc.h b/libavformat/movenc.h
index 5ca5631..4d8d11a 100644
--- a/libavformat/movenc.h
+++ b/libavformat/movenc.h
@@ -89,6 +89,7 @@  typedef struct MOVIndex {
     int         height; ///< active picture (w/o VBI) height for D-10/IMX
     uint32_t    tref_tag;
     int         tref_id; ///< trackID of the referenced track
+    int64_t     start_dts;
 
     int         hint_track;   ///< the track that hints this track, -1 if no hint track is set
     int         src_track;    ///< the track that this hint track describes