movenc: Allow getting the next packet from queues, with timestamps offset

Message ID 1466851953-6213-1-git-send-email-martin@martin.st
State Committed
Headers show

Commit Message

Martin Storsjö June 25, 2016, 10:52 a.m.
---
Added a comment about keeping the offsetting logic in sync, adjusted
the function signature as suggested by Anton.
---
 libavformat/internal.h |  8 +++-----
 libavformat/movenc.c   | 12 ++++++------
 libavformat/mux.c      | 22 ++++++++++++++++++----
 3 files changed, 27 insertions(+), 15 deletions(-)

Comments

Anton Khirnov June 28, 2016, 5:36 a.m. | #1
Quoting Martin Storsjö (2016-06-25 12:52:33)
> ---
> Added a comment about keeping the offsetting logic in sync, adjusted
> the function signature as suggested by Anton.
> ---
>  libavformat/internal.h |  8 +++-----
>  libavformat/movenc.c   | 12 ++++++------
>  libavformat/mux.c      | 22 ++++++++++++++++++----
>  3 files changed, 27 insertions(+), 15 deletions(-)
> 
> diff --git a/libavformat/internal.h b/libavformat/internal.h
> index bbdfd2f..e69c40c 100644
> --- a/libavformat/internal.h
> +++ b/libavformat/internal.h
> @@ -443,12 +443,10 @@ void ff_format_io_close(AVFormatContext *s, AVIOContext **pb);
>  
>  /**
>   * Find the next packet in the interleaving queue for the given stream.
> - * The packet is not removed from the interleaving queue, but only
> - * a pointer to it is returned.
>   *
> - * @return a pointer to the next packet, or NULL if no packet is queued
> - *         for this stream.
> + * @return 0 if a packet was found, a negative value if no packet was found
>   */
> -const AVPacket *ff_interleaved_peek(AVFormatContext *s, int stream);
> +int ff_interleaved_peek(AVFormatContext *s, int stream,
> +                        AVPacket *pkt, int add_offset);
>  
>  #endif /* AVFORMAT_INTERNAL_H */
> diff --git a/libavformat/movenc.c b/libavformat/movenc.c
> index aadfa06..393442c 100644
> --- a/libavformat/movenc.c
> +++ b/libavformat/movenc.c
> @@ -3262,13 +3262,13 @@ static int mov_flush_fragment(AVFormatContext *s, int force)
>      for (i = 0; i < s->nb_streams; i++) {
>          MOVTrack *track = &mov->tracks[i];
>          if (!track->end_reliable) {
> -            const AVPacket *next = ff_interleaved_peek(s, i);
> -            if (next) {
> -                track->track_duration = next->dts - track->start_dts;
> -                if (next->pts != AV_NOPTS_VALUE)
> -                    track->end_pts = next->pts;
> +            AVPacket pkt;
> +            if (!ff_interleaved_peek(s, i, &pkt, 1)) {
> +                track->track_duration = pkt.dts - track->start_dts;
> +                if (pkt.pts != AV_NOPTS_VALUE)
> +                    track->end_pts = pkt.pts;
>                  else
> -                    track->end_pts = next->dts;
> +                    track->end_pts = pkt.dts;
>              }
>          }
>      }
> diff --git a/libavformat/mux.c b/libavformat/mux.c
> index c41c477..37c4541 100644
> --- a/libavformat/mux.c
> +++ b/libavformat/mux.c
> @@ -357,6 +357,8 @@ FF_ENABLE_DEPRECATION_WARNINGS
>  static int write_packet(AVFormatContext *s, AVPacket *pkt)
>  {
>      int ret;
> +    // If the timestamp offsetting below is adjusted, adjust
> +    // ff_interleaved_peek similarly.
>      if (s->avoid_negative_ts > 0) {
>          AVRational time_base = s->streams[pkt->stream_index]->time_base;
>          int64_t offset = 0;
> @@ -614,15 +616,27 @@ int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
>      }
>  }
>  
> -const AVPacket *ff_interleaved_peek(AVFormatContext *s, int stream)
> +int ff_interleaved_peek(AVFormatContext *s, int stream,
> +                        AVPacket *pkt, int add_offset)

Drop the add_offset parameter if you prefer, otherwise should be ok.
Martin Storsjö June 28, 2016, 11:14 a.m. | #2
On Tue, 28 Jun 2016, Anton Khirnov wrote:

> Quoting Martin Storsjö (2016-06-25 12:52:33)
>> ---
>> Added a comment about keeping the offsetting logic in sync, adjusted
>> the function signature as suggested by Anton.
>> ---
>>  libavformat/internal.h |  8 +++-----
>>  libavformat/movenc.c   | 12 ++++++------
>>  libavformat/mux.c      | 22 ++++++++++++++++++----
>>  3 files changed, 27 insertions(+), 15 deletions(-)
>> 
>> diff --git a/libavformat/internal.h b/libavformat/internal.h
>> index bbdfd2f..e69c40c 100644
>> --- a/libavformat/internal.h
>> +++ b/libavformat/internal.h
>> @@ -443,12 +443,10 @@ void ff_format_io_close(AVFormatContext *s, AVIOContext **pb);
>>
>>  /**
>>   * Find the next packet in the interleaving queue for the given stream.
>> - * The packet is not removed from the interleaving queue, but only
>> - * a pointer to it is returned.
>>   *
>> - * @return a pointer to the next packet, or NULL if no packet is queued
>> - *         for this stream.
>> + * @return 0 if a packet was found, a negative value if no packet was found
>>   */
>> -const AVPacket *ff_interleaved_peek(AVFormatContext *s, int stream);
>> +int ff_interleaved_peek(AVFormatContext *s, int stream,
>> +                        AVPacket *pkt, int add_offset);
>>
>>  #endif /* AVFORMAT_INTERNAL_H */
>> diff --git a/libavformat/movenc.c b/libavformat/movenc.c
>> index aadfa06..393442c 100644
>> --- a/libavformat/movenc.c
>> +++ b/libavformat/movenc.c
>> @@ -3262,13 +3262,13 @@ static int mov_flush_fragment(AVFormatContext *s, int force)
>>      for (i = 0; i < s->nb_streams; i++) {
>>          MOVTrack *track = &mov->tracks[i];
>>          if (!track->end_reliable) {
>> -            const AVPacket *next = ff_interleaved_peek(s, i);
>> -            if (next) {
>> -                track->track_duration = next->dts - track->start_dts;
>> -                if (next->pts != AV_NOPTS_VALUE)
>> -                    track->end_pts = next->pts;
>> +            AVPacket pkt;
>> +            if (!ff_interleaved_peek(s, i, &pkt, 1)) {
>> +                track->track_duration = pkt.dts - track->start_dts;
>> +                if (pkt.pts != AV_NOPTS_VALUE)
>> +                    track->end_pts = pkt.pts;
>>                  else
>> -                    track->end_pts = next->dts;
>> +                    track->end_pts = pkt.dts;
>>              }
>>          }
>>      }
>> diff --git a/libavformat/mux.c b/libavformat/mux.c
>> index c41c477..37c4541 100644
>> --- a/libavformat/mux.c
>> +++ b/libavformat/mux.c
>> @@ -357,6 +357,8 @@ FF_ENABLE_DEPRECATION_WARNINGS
>>  static int write_packet(AVFormatContext *s, AVPacket *pkt)
>>  {
>>      int ret;
>> +    // If the timestamp offsetting below is adjusted, adjust
>> +    // ff_interleaved_peek similarly.
>>      if (s->avoid_negative_ts > 0) {
>>          AVRational time_base = s->streams[pkt->stream_index]->time_base;
>>          int64_t offset = 0;
>> @@ -614,15 +616,27 @@ int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
>>      }
>>  }
>> 
>> -const AVPacket *ff_interleaved_peek(AVFormatContext *s, int stream)
>> +int ff_interleaved_peek(AVFormatContext *s, int stream,
>> +                        AVPacket *pkt, int add_offset)
>
> Drop the add_offset parameter if you prefer, otherwise should be ok.

I think I'll rather keep it for now, it's easy to drop later if wanted 
though. Will push in a little while.

// Martin

Patch

diff --git a/libavformat/internal.h b/libavformat/internal.h
index bbdfd2f..e69c40c 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -443,12 +443,10 @@  void ff_format_io_close(AVFormatContext *s, AVIOContext **pb);
 
 /**
  * Find the next packet in the interleaving queue for the given stream.
- * The packet is not removed from the interleaving queue, but only
- * a pointer to it is returned.
  *
- * @return a pointer to the next packet, or NULL if no packet is queued
- *         for this stream.
+ * @return 0 if a packet was found, a negative value if no packet was found
  */
-const AVPacket *ff_interleaved_peek(AVFormatContext *s, int stream);
+int ff_interleaved_peek(AVFormatContext *s, int stream,
+                        AVPacket *pkt, int add_offset);
 
 #endif /* AVFORMAT_INTERNAL_H */
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index aadfa06..393442c 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -3262,13 +3262,13 @@  static int mov_flush_fragment(AVFormatContext *s, int force)
     for (i = 0; i < s->nb_streams; i++) {
         MOVTrack *track = &mov->tracks[i];
         if (!track->end_reliable) {
-            const AVPacket *next = ff_interleaved_peek(s, i);
-            if (next) {
-                track->track_duration = next->dts - track->start_dts;
-                if (next->pts != AV_NOPTS_VALUE)
-                    track->end_pts = next->pts;
+            AVPacket pkt;
+            if (!ff_interleaved_peek(s, i, &pkt, 1)) {
+                track->track_duration = pkt.dts - track->start_dts;
+                if (pkt.pts != AV_NOPTS_VALUE)
+                    track->end_pts = pkt.pts;
                 else
-                    track->end_pts = next->dts;
+                    track->end_pts = pkt.dts;
             }
         }
     }
diff --git a/libavformat/mux.c b/libavformat/mux.c
index c41c477..37c4541 100644
--- a/libavformat/mux.c
+++ b/libavformat/mux.c
@@ -357,6 +357,8 @@  FF_ENABLE_DEPRECATION_WARNINGS
 static int write_packet(AVFormatContext *s, AVPacket *pkt)
 {
     int ret;
+    // If the timestamp offsetting below is adjusted, adjust
+    // ff_interleaved_peek similarly.
     if (s->avoid_negative_ts > 0) {
         AVRational time_base = s->streams[pkt->stream_index]->time_base;
         int64_t offset = 0;
@@ -614,15 +616,27 @@  int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
     }
 }
 
-const AVPacket *ff_interleaved_peek(AVFormatContext *s, int stream)
+int ff_interleaved_peek(AVFormatContext *s, int stream,
+                        AVPacket *pkt, int add_offset)
 {
     AVPacketList *pktl = s->internal->packet_buffer;
     while (pktl) {
-        if (pktl->pkt.stream_index == stream)
-            return &pktl->pkt;
+        if (pktl->pkt.stream_index == stream) {
+            *pkt = pktl->pkt;
+            if (add_offset && s->internal->offset != AV_NOPTS_VALUE) {
+                int64_t offset = av_rescale_q(s->internal->offset,
+                                              s->internal->offset_timebase,
+                                              s->streams[stream]->time_base);
+                if (pkt->dts != AV_NOPTS_VALUE)
+                    pkt->dts += offset;
+                if (pkt->pts != AV_NOPTS_VALUE)
+                    pkt->pts += offset;
+            }
+            return 0;
+        }
         pktl = pktl->next;
     }
-    return NULL;
+    return AVERROR(ENOENT);
 }
 
 /**