diff mbox series

[FFmpeg-devel,06/18] avformat: port AVFormatInternal->packet_buffer to the new packet list API

Message ID 20201118165247.4130-7-jamrial@gmail.com
State New
Headers show
Series AVPacketList public API
Related show

Checks

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

Commit Message

James Almer Nov. 18, 2020, 4:52 p.m. UTC
Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavformat/internal.h |  3 +-
 libavformat/mux.c      | 53 +++++++++++++++++++-----------
 libavformat/mxfenc.c   | 19 +++++++----
 libavformat/options.c  |  6 ++++
 libavformat/utils.c    | 74 ++++++++++++++++++++++++++++++------------
 5 files changed, 107 insertions(+), 48 deletions(-)
diff mbox series

Patch

diff --git a/libavformat/internal.h b/libavformat/internal.h
index d366bdc452..3182409dfb 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -74,8 +74,7 @@  struct AVFormatInternal {
      * not decoded, for example to get the codec parameters in MPEG
      * streams.
      */
-    struct PacketListEntry *packet_buffer;
-    struct PacketListEntry *packet_buffer_end;
+    struct AVPacketList *packet_buffer;
 
     /* av_seek_frame() support */
     int64_t data_offset; /**< offset of the first packet */
diff --git a/libavformat/mux.c b/libavformat/mux.c
index cf52de5366..6991f81f9f 100644
--- a/libavformat/mux.c
+++ b/libavformat/mux.c
@@ -816,6 +816,11 @@  int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
     PacketListEntry **next_point, *this_pktl;
     AVStream *st = s->streams[pkt->stream_index];
     int chunked  = s->max_chunk_size || s->max_chunk_duration;
+#if FF_API_PACKET_LIST
+    struct PacketList *packet_buffer = (struct PacketList *)s->internal->packet_buffer;
+#else
+    AVPacketList *packet_buffer = s->internal->packet_buffer;
+#endif
 
     this_pktl    = av_malloc(sizeof(PacketListEntry));
     if (!this_pktl) {
@@ -834,7 +839,7 @@  int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
     if (st->internal->last_in_packet_buffer) {
         next_point = &(st->internal->last_in_packet_buffer->next);
     } else {
-        next_point = &s->internal->packet_buffer;
+        next_point = &packet_buffer->head;
     }
 
     if (chunked) {
@@ -858,7 +863,7 @@  int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
         if (chunked && !(pkt->flags & CHUNK_START))
             goto next_non_null;
 
-        if (compare(s, &s->internal->packet_buffer_end->pkt, pkt)) {
+        if (compare(s, &packet_buffer->tail->pkt, pkt)) {
             while (   *next_point
                    && ((chunked && !((*next_point)->pkt.flags&CHUNK_START))
                        || !compare(s, &(*next_point)->pkt, pkt)))
@@ -866,12 +871,12 @@  int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
             if (*next_point)
                 goto next_non_null;
         } else {
-            next_point = &(s->internal->packet_buffer_end->next);
+            next_point = &(packet_buffer->tail->next);
         }
     }
     av_assert1(!*next_point);
 
-    s->internal->packet_buffer_end = this_pktl;
+    packet_buffer->tail = this_pktl;
 next_non_null:
 
     this_pktl->next = *next_point;
@@ -920,6 +925,11 @@  int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
     int noninterleaved_count = 0;
     int i, ret;
     int eof = flush;
+#if FF_API_PACKET_LIST
+    struct PacketList *packet_buffer = (struct PacketList *)s->internal->packet_buffer;
+#else
+    AVPacketList *packet_buffer = s->internal->packet_buffer;
+#endif
 
     if (pkt) {
         if ((ret = ff_interleave_add_packet(s, pkt, interleave_compare_dts)) < 0)
@@ -940,11 +950,11 @@  int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
         flush = 1;
 
     if (s->max_interleave_delta > 0 &&
-        s->internal->packet_buffer &&
+        packet_buffer->head &&
         !flush &&
         s->internal->nb_interleaved_streams == stream_count+noninterleaved_count
     ) {
-        AVPacket *top_pkt = &s->internal->packet_buffer->pkt;
+        AVPacket *top_pkt = &packet_buffer->head->pkt;
         int64_t delta_dts = INT64_MIN;
         int64_t top_dts = av_rescale_q(top_pkt->dts,
                                        s->streams[top_pkt->stream_index]->time_base,
@@ -972,11 +982,11 @@  int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
         }
     }
 
-    if (s->internal->packet_buffer &&
+    if (packet_buffer->head &&
         eof &&
         (s->flags & AVFMT_FLAG_SHORTEST) &&
         s->internal->shortest_end == AV_NOPTS_VALUE) {
-        AVPacket *top_pkt = &s->internal->packet_buffer->pkt;
+        AVPacket *top_pkt = &packet_buffer->head->pkt;
 
         s->internal->shortest_end = av_rescale_q(top_pkt->dts,
                                        s->streams[top_pkt->stream_index]->time_base,
@@ -984,8 +994,8 @@  int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
     }
 
     if (s->internal->shortest_end != AV_NOPTS_VALUE) {
-        while (s->internal->packet_buffer) {
-            AVPacket *top_pkt = &s->internal->packet_buffer->pkt;
+        while (packet_buffer->head) {
+            AVPacket *top_pkt = &packet_buffer->head->pkt;
             AVStream *st;
             int64_t top_dts = av_rescale_q(top_pkt->dts,
                                         s->streams[top_pkt->stream_index]->time_base,
@@ -994,12 +1004,12 @@  int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
             if (s->internal->shortest_end + 1 >= top_dts)
                 break;
 
-            pktl = s->internal->packet_buffer;
+            pktl = packet_buffer->head;
             st   = s->streams[pktl->pkt.stream_index];
 
-            s->internal->packet_buffer = pktl->next;
-            if (!s->internal->packet_buffer)
-                s->internal->packet_buffer_end = NULL;
+            packet_buffer->head = pktl->next;
+            if (!packet_buffer->head)
+                packet_buffer->tail = NULL;
 
             if (st->internal->last_in_packet_buffer == pktl)
                 st->internal->last_in_packet_buffer = NULL;
@@ -1012,13 +1022,13 @@  int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
 
     if (stream_count && flush) {
         AVStream *st;
-        pktl = s->internal->packet_buffer;
+        pktl = packet_buffer->head;
         *out = pktl->pkt;
         st   = s->streams[out->stream_index];
 
-        s->internal->packet_buffer = pktl->next;
-        if (!s->internal->packet_buffer)
-            s->internal->packet_buffer_end = NULL;
+        packet_buffer->head = pktl->next;
+        if (!packet_buffer->head)
+            packet_buffer->tail = NULL;
 
         if (st->internal->last_in_packet_buffer == pktl)
             st->internal->last_in_packet_buffer = NULL;
@@ -1033,7 +1043,12 @@  int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
 int ff_interleaved_peek(AVFormatContext *s, int stream,
                         AVPacket *pkt, int add_offset)
 {
-    PacketListEntry *pktl = s->internal->packet_buffer;
+#if FF_API_PACKET_LIST
+    struct PacketList *packet_buffer = (struct PacketList *)s->internal->packet_buffer;
+#else
+    AVPacketList *packet_buffer = s->internal->packet_buffer;
+#endif
+    PacketListEntry *pktl = packet_buffer->head;
     while (pktl) {
         if (pktl->pkt.stream_index == stream) {
             *pkt = pktl->pkt;
diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c
index 8f2649c31f..55a53af5e8 100644
--- a/libavformat/mxfenc.c
+++ b/libavformat/mxfenc.c
@@ -3053,7 +3053,12 @@  static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket
         stream_count += !!s->streams[i]->internal->last_in_packet_buffer;
 
     if (stream_count && (s->nb_streams == stream_count || flush)) {
-        PacketListEntry *pktl = s->internal->packet_buffer;
+#if FF_API_PACKET_LIST
+        struct PacketList *packet_buffer = (struct PacketList *)s->internal->packet_buffer;
+#else
+        AVPacketList *packet_buffer = s->internal->packet_buffer;
+#endif
+        PacketListEntry *pktl = packet_buffer->head;
         if (s->nb_streams != stream_count) {
             PacketListEntry *last = NULL;
             // find last packet in edit unit
@@ -3077,20 +3082,20 @@  static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket
             if (last)
                 last->next = NULL;
             else {
-                s->internal->packet_buffer = NULL;
-                s->internal->packet_buffer_end= NULL;
+                packet_buffer->head = NULL;
+                packet_buffer->tail= NULL;
                 goto out;
             }
-            pktl = s->internal->packet_buffer;
+            pktl = packet_buffer->head;
         }
 
         *out = pktl->pkt;
         av_log(s, AV_LOG_TRACE, "out st:%d dts:%"PRId64"\n", (*out).stream_index, (*out).dts);
-        s->internal->packet_buffer = pktl->next;
+        packet_buffer->head = pktl->next;
         if(s->streams[pktl->pkt.stream_index]->internal->last_in_packet_buffer == pktl)
             s->streams[pktl->pkt.stream_index]->internal->last_in_packet_buffer= NULL;
-        if(!s->internal->packet_buffer)
-            s->internal->packet_buffer_end= NULL;
+        if(!packet_buffer->head)
+            packet_buffer->tail = NULL;
         av_freep(&pktl);
         return 1;
     } else {
diff --git a/libavformat/options.c b/libavformat/options.c
index 59e0389815..9fe1500cee 100644
--- a/libavformat/options.c
+++ b/libavformat/options.c
@@ -220,6 +220,12 @@  AVFormatContext *avformat_alloc_context(void)
         av_free(ic);
         return NULL;
     }
+    internal->packet_buffer = av_packet_list_alloc();
+    if (!internal->packet_buffer) {
+        av_free(internal);
+        av_free(ic);
+        return NULL;
+    }
     avformat_get_context_defaults(ic);
     ic->internal = internal;
     ic->internal->offset = AV_NOPTS_VALUE;
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 20bf723a55..88ce64496d 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -1009,9 +1009,15 @@  static int has_decode_delay_been_guessed(AVStream *st)
 
 static PacketListEntry *get_next_pkt(AVFormatContext *s, AVStream *st, PacketListEntry *pktl)
 {
+#if FF_API_PACKET_LIST
+    struct PacketList *packet_buffer = (struct PacketList *)s->internal->packet_buffer;
+#else
+    AVPacketList *packet_buffer = s->internal->packet_buffer;
+#endif
+
     if (pktl->next)
         return pktl->next;
-    if (pktl == s->internal->packet_buffer_end)
+    if (pktl == packet_buffer->tail)
         return s->internal->parse_queue;
     return NULL;
 }
@@ -1092,7 +1098,14 @@  static void update_initial_timestamps(AVFormatContext *s, int stream_index,
                                       int64_t dts, int64_t pts, AVPacket *pkt)
 {
     AVStream *st       = s->streams[stream_index];
-    PacketListEntry *pktl = s->internal->packet_buffer ? s->internal->packet_buffer : s->internal->parse_queue;
+#if FF_API_PACKET_LIST
+    struct PacketList *packet_buffer = (struct PacketList *)s->internal->packet_buffer;
+#else
+    AVPacketList *packet_buffer = s->internal->packet_buffer;
+#endif
+    PacketListEntry *pktl = packet_buffer->head ?
+                            packet_buffer->head :
+                            s->internal->parse_queue;
     PacketListEntry *pktl_it;
 
     uint64_t shift;
@@ -1143,7 +1156,14 @@  static void update_initial_timestamps(AVFormatContext *s, int stream_index,
 static void update_initial_durations(AVFormatContext *s, AVStream *st,
                                      int stream_index, int64_t duration)
 {
-    PacketListEntry *pktl = s->internal->packet_buffer ? s->internal->packet_buffer : s->internal->parse_queue;
+#if FF_API_PACKET_LIST
+    struct PacketList *packet_buffer = (struct PacketList *)s->internal->packet_buffer;
+#else
+    AVPacketList *packet_buffer = s->internal->packet_buffer;
+#endif
+    PacketListEntry *pktl = packet_buffer->head ?
+                            packet_buffer->head :
+                            s->internal->parse_queue;
     int64_t cur_dts    = RELATIVE_TS_BASE;
 
     if (st->first_dts != AV_NOPTS_VALUE) {
@@ -1169,7 +1189,9 @@  static void update_initial_durations(AVFormatContext *s, AVStream *st,
             av_log(s, AV_LOG_DEBUG, "first_dts %s but no packet with dts in the queue\n", av_ts2str(st->first_dts));
             return;
         }
-        pktl          = s->internal->packet_buffer ? s->internal->packet_buffer : s->internal->parse_queue;
+        pktl          = packet_buffer->head ?
+                        packet_buffer->head :
+                        s->internal->parse_queue;
         st->first_dts = cur_dts;
     } else if (st->cur_dts != RELATIVE_TS_BASE)
         return;
@@ -1204,6 +1226,11 @@  static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
     AVRational duration;
     int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
                        st->codecpar->codec_id != AV_CODEC_ID_HEVC;
+#if FF_API_PACKET_LIST
+    struct PacketList *packet_buffer = (struct PacketList *)s->internal->packet_buffer;
+#else
+    AVPacketList *packet_buffer = s->internal->packet_buffer;
+#endif
 
     if (s->flags & AVFMT_FLAG_NOFILLIN)
         return;
@@ -1281,7 +1308,7 @@  static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
         }
     }
 
-    if (pkt->duration > 0 && (s->internal->packet_buffer || s->internal->parse_queue))
+    if (pkt->duration > 0 && (packet_buffer->head || s->internal->parse_queue))
         update_initial_durations(s, st, pkt->stream_index, pkt->duration);
 
     /* Correct timestamps with byte offset if demuxers only have timestamps
@@ -1715,11 +1742,15 @@  int av_read_frame(AVFormatContext *s, AVPacket *pkt)
     int eof = 0;
     int ret;
     AVStream *st;
+#if FF_API_PACKET_LIST
+    struct PacketList *packet_buffer = (struct PacketList *)s->internal->packet_buffer;
+#else
+    AVPacketList *packet_buffer = s->internal->packet_buffer;
+#endif
 
     if (!genpts) {
-        ret = s->internal->packet_buffer
-              ? avpriv_packet_list_get(&s->internal->packet_buffer,
-                                        &s->internal->packet_buffer_end, pkt)
+        ret = packet_buffer->head
+              ? av_packet_list_get(s->internal->packet_buffer, pkt, 0)
               : read_frame_internal(s, pkt);
         if (ret < 0)
             return ret;
@@ -1727,7 +1758,7 @@  int av_read_frame(AVFormatContext *s, AVPacket *pkt)
     }
 
     for (;;) {
-        PacketListEntry *pktl = s->internal->packet_buffer;
+        PacketListEntry *pktl = packet_buffer->head;
 
         if (pktl) {
             AVPacket *next_pkt = &pktl->pkt;
@@ -1760,15 +1791,14 @@  int av_read_frame(AVFormatContext *s, AVPacket *pkt)
                     // 3. the packets for this stream at the end of the files had valid dts.
                     next_pkt->pts = last_dts + next_pkt->duration;
                 }
-                pktl = s->internal->packet_buffer;
+                pktl = packet_buffer->head;
             }
 
             /* read packet from packet buffer, if there is data */
             st = s->streams[next_pkt->stream_index];
             if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL &&
                   next_pkt->dts != AV_NOPTS_VALUE && !eof)) {
-                ret = avpriv_packet_list_get(&s->internal->packet_buffer,
-                                               &s->internal->packet_buffer_end, pkt);
+                ret = av_packet_list_get(s->internal->packet_buffer, pkt, 0);
                 goto return_packet;
             }
         }
@@ -1782,8 +1812,7 @@  int av_read_frame(AVFormatContext *s, AVPacket *pkt)
                 return ret;
         }
 
-        ret = avpriv_packet_list_put(&s->internal->packet_buffer,
-                                 &s->internal->packet_buffer_end,
+        ret = av_packet_list_put(s->internal->packet_buffer,
                                  pkt, NULL, 0);
         if (ret < 0) {
             av_packet_unref(pkt);
@@ -1813,7 +1842,7 @@  static void flush_packet_queue(AVFormatContext *s)
     if (!s->internal)
         return;
     avpriv_packet_list_free(&s->internal->parse_queue,       &s->internal->parse_queue_end);
-    avpriv_packet_list_free(&s->internal->packet_buffer,     &s->internal->packet_buffer_end);
+    av_packet_list_flush(s->internal->packet_buffer);
     avpriv_packet_list_free(&s->internal->raw_packet_buffer, &s->internal->raw_packet_buffer_end);
 
     s->internal->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
@@ -3581,6 +3610,11 @@  int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
     int64_t probesize = ic->probesize;
     int eof_reached = 0;
     int *missing_streams = av_opt_ptr(ic->iformat->priv_class, ic->priv_data, "missing_streams");
+#if FF_API_PACKET_LIST
+    struct PacketList *packet_buffer = (struct PacketList *)ic->internal->packet_buffer;
+#else
+    AVPacketList *packet_buffer = ic->internal->packet_buffer;
+#endif
 
     flush_codecs = probesize > 0;
 
@@ -3784,13 +3818,12 @@  FF_ENABLE_DEPRECATION_WARNINGS
         }
 
         if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
-            ret = avpriv_packet_list_put(&ic->internal->packet_buffer,
-                                     &ic->internal->packet_buffer_end,
+            ret = av_packet_list_put(ic->internal->packet_buffer,
                                      &pkt1, NULL, 0);
             if (ret < 0)
                 goto unref_then_goto_end;
 
-            pkt = &ic->internal->packet_buffer_end->pkt;
+            pkt = &packet_buffer->tail->pkt;
         } else {
             pkt = &pkt1;
         }
@@ -3933,8 +3966,8 @@  FF_ENABLE_DEPRECATION_WARNINGS
 
             // EOF already reached while reading the stream above.
             // So continue with reoordering DTS with whatever delay we have.
-            if (ic->internal->packet_buffer && !has_decode_delay_been_guessed(st)) {
-                update_dts_from_pts(ic, stream_index, ic->internal->packet_buffer);
+            if (packet_buffer->head && !has_decode_delay_been_guessed(st)) {
+                update_dts_from_pts(ic, stream_index, packet_buffer->head);
             }
         }
     }
@@ -4437,6 +4470,7 @@  void avformat_free_context(AVFormatContext *s)
     av_dict_free(&s->internal->id3v2_meta);
     av_freep(&s->streams);
     flush_packet_queue(s);
+    av_packet_list_free(&s->internal->packet_buffer);
     av_freep(&s->internal);
     av_freep(&s->url);
     av_free(s);