diff mbox series

[FFmpeg-devel,2/2] avformat/mux: return a pointer to the packet in ff_interleaved_peek()

Message ID 20210208222129.4695-2-jamrial@gmail.com
State Accepted
Headers show
Series [FFmpeg-devel,1/2] avformat/mux: add ff_get_muxer_ts_offset() | expand

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

James Almer Feb. 8, 2021, 10:21 p.m. UTC
And make it const, so the caller doesn't attempt to change it.
ff_get_muxer_ts_offset() should be used to get the muxer timestamp offset.

Signed-off-by: James Almer <jamrial@gmail.com>
---
Went with Andreas' idea, but splitting off the muxer ts offset part of the
process, so ff_interleaved_peek() doesn't need to use an extra output
argument for it.

 libavformat/internal.h |  8 ++------
 libavformat/movenc.c   | 17 ++++++++++-------
 libavformat/mux.c      | 20 +++-----------------
 3 files changed, 15 insertions(+), 30 deletions(-)

Comments

James Almer Feb. 12, 2021, 3:29 p.m. UTC | #1
On 2/8/2021 7:21 PM, James Almer wrote:
> And make it const, so the caller doesn't attempt to change it.
> ff_get_muxer_ts_offset() should be used to get the muxer timestamp offset.
> 
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
> Went with Andreas' idea, but splitting off the muxer ts offset part of the
> process, so ff_interleaved_peek() doesn't need to use an extra output
> argument for it.

Will apply the set soon.
diff mbox series

Patch

diff --git a/libavformat/internal.h b/libavformat/internal.h
index 33ece6b172..0ffdc87b6a 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -865,14 +865,10 @@  int ff_bprint_to_codecpar_extradata(AVCodecParameters *par, struct AVBPrint *buf
 
 /**
  * Find the next packet in the interleaving queue for the given stream.
- * The pkt parameter is filled in with the queued packet, including
- * references to the data (which the caller is not allowed to keep or
- * modify).
  *
- * @return 0 if a packet was found, a negative value if no packet was found
+ * @return a pointer to a packet if one was found, NULL otherwise.
  */
-int ff_interleaved_peek(AVFormatContext *s, int stream,
-                        AVPacket *pkt, int add_offset);
+const AVPacket *ff_interleaved_peek(AVFormatContext *s, int stream);
 
 int ff_get_muxer_ts_offset(AVFormatContext *s, int stream_index, int64_t *offset);
 
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 372c04295d..db9b6122e6 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -5303,15 +5303,18 @@  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) {
-            AVPacket pkt;
-            if (!ff_interleaved_peek(s, i, &pkt, 1)) {
+            const AVPacket *pkt = ff_interleaved_peek(s, i);
+            if (pkt) {
+                int64_t offset, dts = pkt->dts, pts = pkt->pts;
+                ff_get_muxer_ts_offset(s, i, &offset);
+                dts += offset;
                 if (track->dts_shift != AV_NOPTS_VALUE)
-                    pkt.dts += track->dts_shift;
-                track->track_duration = pkt.dts - track->start_dts;
-                if (pkt.pts != AV_NOPTS_VALUE)
-                    track->end_pts = pkt.pts;
+                    dts += track->dts_shift;
+                track->track_duration = dts - track->start_dts;
+                if (pts != AV_NOPTS_VALUE)
+                    track->end_pts = pts + offset;
                 else
-                    track->end_pts = pkt.dts;
+                    track->end_pts = dts;
             }
         }
     }
diff --git a/libavformat/mux.c b/libavformat/mux.c
index ae46844c66..062ba8d789 100644
--- a/libavformat/mux.c
+++ b/libavformat/mux.c
@@ -1062,30 +1062,16 @@  int ff_get_muxer_ts_offset(AVFormatContext *s, int stream_index, int64_t *offset
     return 0;
 }
 
-int ff_interleaved_peek(AVFormatContext *s, int stream,
-                        AVPacket *pkt, int add_offset)
+const AVPacket *ff_interleaved_peek(AVFormatContext *s, int stream)
 {
     AVPacketList *pktl = s->internal->packet_buffer;
     while (pktl) {
         if (pktl->pkt.stream_index == stream) {
-            *pkt = pktl->pkt;
-            if (add_offset) {
-                AVStream *st = s->streams[pkt->stream_index];
-                int64_t offset = st->internal->mux_ts_offset;
-
-                if (s->output_ts_offset)
-                    offset += av_rescale_q(s->output_ts_offset, AV_TIME_BASE_Q, st->time_base);
-
-                if (pkt->dts != AV_NOPTS_VALUE)
-                    pkt->dts += offset;
-                if (pkt->pts != AV_NOPTS_VALUE)
-                    pkt->pts += offset;
-            }
-            return 0;
+            return &pktl->pkt;
         }
         pktl = pktl->next;
     }
-    return AVERROR(ENOENT);
+    return NULL;
 }
 
 /**