diff mbox series

[FFmpeg-devel,RFC,GSoC,v2,5/6] avformat/utils: add av_packet_buffer_filter to filter packet_buffer

Message ID 20200716125116.187031-5-sj.hc_Zhong@sjtu.edu.cn
State New
Headers show
Series [FFmpeg-devel,RFC,GSoC,v2,1/6] avformat/abr: Adaptive Bitrate support
Related show

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Hongcheng Zhong July 16, 2020, 12:51 p.m. UTC
From: spartazhc <spartazhc@gmail.com>

Add av_packet_buffer_filter to remove AVPackets whose stream_index is not
in st_index list. st_index has length of AVMEDIA_TYPE_NB, contains
the stream_index of all these media types.

Generally s->internal->packet_buffer may have pkts from different
stream, and stream_index will be used to discard pkt that is not
needed. But in case of abr, several streams may pass the stream_index
check. So we need a function to remove AVPackets not needed in pktl
added by hls_read_header.

v1 fixed:
1. rename function name *_clean to *_filter
2. fix memory leak in ff_packet_buffer_filter
3. update the doc

Signed-off-by: spartazhc <spartazhc@gmail.com>
---
 libavformat/avformat.h | 12 ++++++++++
 libavformat/internal.h | 16 +++++++++++++
 libavformat/utils.c    | 54 ++++++++++++++++++++++++++++++++++++++++++
 libavformat/version.h  |  2 +-
 4 files changed, 83 insertions(+), 1 deletion(-)

Comments

James Almer July 16, 2020, 1:06 p.m. UTC | #1
On 7/16/2020 9:51 AM, Hongcheng Zhong wrote:
> From: spartazhc <spartazhc@gmail.com>
> 
> Add av_packet_buffer_filter to remove AVPackets whose stream_index is not
> in st_index list. st_index has length of AVMEDIA_TYPE_NB, contains
> the stream_index of all these media types.
> 
> Generally s->internal->packet_buffer may have pkts from different
> stream, and stream_index will be used to discard pkt that is not
> needed. But in case of abr, several streams may pass the stream_index
> check. So we need a function to remove AVPackets not needed in pktl
> added by hls_read_header.
> 
> v1 fixed:
> 1. rename function name *_clean to *_filter
> 2. fix memory leak in ff_packet_buffer_filter
> 3. update the doc
> 
> Signed-off-by: spartazhc <spartazhc@gmail.com>
> ---
>  libavformat/avformat.h | 12 ++++++++++
>  libavformat/internal.h | 16 +++++++++++++
>  libavformat/utils.c    | 54 ++++++++++++++++++++++++++++++++++++++++++
>  libavformat/version.h  |  2 +-
>  4 files changed, 83 insertions(+), 1 deletion(-)
> 
> diff --git a/libavformat/avformat.h b/libavformat/avformat.h
> index e91e7f1d33..c796fd0391 100644
> --- a/libavformat/avformat.h
> +++ b/libavformat/avformat.h
> @@ -2474,6 +2474,18 @@ int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts, int
>   */
>  int avformat_flush(AVFormatContext *s);
>  
> +/**
> + * Filter the packet buffer list of the AVFormatContext, remove the AVPackets
> + * do not need according to st_index.

"Not needed", here and below.

> + * Only filter the packet_buffer list.
> + *
> + * @param s media file handle
> + * @param st_index the stream_index list which is needed
> + *        st_index has length of AVMEDIA_TYPE_NB
> + *        in index AVMEDIA_TYPE_XXX contains the stream_index needed of type XXX

From an API user PoV, what does this function do? What is a "packet
buffer list"? How do i know what this does and how do i know when i need it?

> + */
> +int av_packet_buffer_filter(AVFormatContext *s, int *st_index);

No, av_packet_* is a libavcodec namespace for AVPacket helpers. You
can't use it here.

> +
>  /**
>   * Start playing a network-based stream (e.g. RTSP stream) at the
>   * current position.
> diff --git a/libavformat/internal.h b/libavformat/internal.h
> index 17a6ab07d3..58ebcb2e35 100644
> --- a/libavformat/internal.h
> +++ b/libavformat/internal.h
> @@ -772,6 +772,22 @@ int ff_packet_list_get(AVPacketList **head, AVPacketList **tail,
>   */
>  void ff_packet_list_free(AVPacketList **head, AVPacketList **tail);
>  
> +/**
> + * Remove the AVPackets do not need in the packet buffer list.
> + * For each type in AVMediaType, at most keep one stream and
> + * the others will be removed.
> + *
> + * @param head List head element
> + * @param tail List tail element
> + * @param st_index the stream_index list which is needed
> + *        st_index has length of AVMEDIA_TYPE_NB
> + *        in index AVMEDIA_TYPE_XXX contains the stream_index needed of type XXX
> + * @return 0 on success. Success is guaranteed
> + *         if the packet list is not empty.
> + */
> +int ff_packet_buffer_filter(AVPacketList **head, AVPacketList **tail,
> +                            int *st_index);
> +
>  void avpriv_register_devices(const AVOutputFormat * const o[], const AVInputFormat * const i[]);
>  
>  #endif /* AVFORMAT_INTERNAL_H */
> diff --git a/libavformat/utils.c b/libavformat/utils.c
> index 807d9f10cb..7674e4ea3d 100644
> --- a/libavformat/utils.c
> +++ b/libavformat/utils.c
> @@ -1565,6 +1565,60 @@ int ff_packet_list_get(AVPacketList **pkt_buffer,
>      return 0;
>  }
>  
> +/**
> + * return 1 if needed
> + */
> +static int ff_check_st_index(int st, int *st_index)

Static functions don't use the ff_ prefix.

> +{
> +    for (int i = 0; i < AVMEDIA_TYPE_NB; ++i) {
> +        if (st_index[i] == st)
> +            return 1;
> +    }
> +    return 0;
> +}
> +
> +int ff_packet_buffer_filter(AVPacketList **pkt_buffer,
> +                         AVPacketList **pkt_buffer_end,
> +                         int           *st_index)
> +{
> +    AVPacketList *pktl, *pktn;
> +    av_assert0(*pkt_buffer);
> +    pktl = *pkt_buffer;
> +    pktn = pktl->next;
> +
> +    /* num >= 2 */
> +    while (pktn) {
> +        if (!ff_check_st_index(pktn->pkt.stream_index, st_index)) {
> +            av_packet_unref(&pktn->pkt);
> +            pktl->next = pktn->next;
> +            av_freep(&pktn);
> +            pktn = pktl->next;
> +        } else {
> +            pktl = pktn;
> +            pktn = pktn->next;
> +        }
> +    }
> +    *pkt_buffer_end = pktl;
> +    /* first one*/
> +    pktl = *pkt_buffer;
> +    if (!ff_check_st_index(pktl->pkt.stream_index, st_index)) {
> +        av_packet_unref(&pktl->pkt);
> +        *pkt_buffer = pktl->next;
> +        if (!pktl->next)
> +            *pkt_buffer_end = NULL;
> +        av_freep(&pktl);
> +    }

Why are you not using the existing packet list helpers for this?

> +
> +    return 0;
> +}
> +
> +int av_packet_buffer_filter(AVFormatContext *s, int *st_index)
> +{
> +    int ret = ff_packet_buffer_filter(&s->internal->packet_buffer,
> +                                   &s->internal->packet_buffer_end, st_index);
> +    return ret;
> +}
> +
>  static int64_t ts_to_samples(AVStream *st, int64_t ts)
>  {
>      return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den);
> diff --git a/libavformat/version.h b/libavformat/version.h
> index 75c03fde0a..c17727cf73 100644
> --- a/libavformat/version.h
> +++ b/libavformat/version.h
> @@ -33,7 +33,7 @@
>  // Also please add any ticket numbers that you believe might be affected here
>  #define LIBAVFORMAT_VERSION_MAJOR  58
>  #define LIBAVFORMAT_VERSION_MINOR  48
> -#define LIBAVFORMAT_VERSION_MICRO 100
> +#define LIBAVFORMAT_VERSION_MICRO 101
>  
>  #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
>                                                 LIBAVFORMAT_VERSION_MINOR, \
>
diff mbox series

Patch

diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index e91e7f1d33..c796fd0391 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -2474,6 +2474,18 @@  int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts, int
  */
 int avformat_flush(AVFormatContext *s);
 
+/**
+ * Filter the packet buffer list of the AVFormatContext, remove the AVPackets
+ * do not need according to st_index.
+ * Only filter the packet_buffer list.
+ *
+ * @param s media file handle
+ * @param st_index the stream_index list which is needed
+ *        st_index has length of AVMEDIA_TYPE_NB
+ *        in index AVMEDIA_TYPE_XXX contains the stream_index needed of type XXX
+ */
+int av_packet_buffer_filter(AVFormatContext *s, int *st_index);
+
 /**
  * Start playing a network-based stream (e.g. RTSP stream) at the
  * current position.
diff --git a/libavformat/internal.h b/libavformat/internal.h
index 17a6ab07d3..58ebcb2e35 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -772,6 +772,22 @@  int ff_packet_list_get(AVPacketList **head, AVPacketList **tail,
  */
 void ff_packet_list_free(AVPacketList **head, AVPacketList **tail);
 
+/**
+ * Remove the AVPackets do not need in the packet buffer list.
+ * For each type in AVMediaType, at most keep one stream and
+ * the others will be removed.
+ *
+ * @param head List head element
+ * @param tail List tail element
+ * @param st_index the stream_index list which is needed
+ *        st_index has length of AVMEDIA_TYPE_NB
+ *        in index AVMEDIA_TYPE_XXX contains the stream_index needed of type XXX
+ * @return 0 on success. Success is guaranteed
+ *         if the packet list is not empty.
+ */
+int ff_packet_buffer_filter(AVPacketList **head, AVPacketList **tail,
+                            int *st_index);
+
 void avpriv_register_devices(const AVOutputFormat * const o[], const AVInputFormat * const i[]);
 
 #endif /* AVFORMAT_INTERNAL_H */
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 807d9f10cb..7674e4ea3d 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -1565,6 +1565,60 @@  int ff_packet_list_get(AVPacketList **pkt_buffer,
     return 0;
 }
 
+/**
+ * return 1 if needed
+ */
+static int ff_check_st_index(int st, int *st_index)
+{
+    for (int i = 0; i < AVMEDIA_TYPE_NB; ++i) {
+        if (st_index[i] == st)
+            return 1;
+    }
+    return 0;
+}
+
+int ff_packet_buffer_filter(AVPacketList **pkt_buffer,
+                         AVPacketList **pkt_buffer_end,
+                         int           *st_index)
+{
+    AVPacketList *pktl, *pktn;
+    av_assert0(*pkt_buffer);
+    pktl = *pkt_buffer;
+    pktn = pktl->next;
+
+    /* num >= 2 */
+    while (pktn) {
+        if (!ff_check_st_index(pktn->pkt.stream_index, st_index)) {
+            av_packet_unref(&pktn->pkt);
+            pktl->next = pktn->next;
+            av_freep(&pktn);
+            pktn = pktl->next;
+        } else {
+            pktl = pktn;
+            pktn = pktn->next;
+        }
+    }
+    *pkt_buffer_end = pktl;
+    /* first one*/
+    pktl = *pkt_buffer;
+    if (!ff_check_st_index(pktl->pkt.stream_index, st_index)) {
+        av_packet_unref(&pktl->pkt);
+        *pkt_buffer = pktl->next;
+        if (!pktl->next)
+            *pkt_buffer_end = NULL;
+        av_freep(&pktl);
+    }
+
+    return 0;
+}
+
+int av_packet_buffer_filter(AVFormatContext *s, int *st_index)
+{
+    int ret = ff_packet_buffer_filter(&s->internal->packet_buffer,
+                                   &s->internal->packet_buffer_end, st_index);
+    return ret;
+}
+
 static int64_t ts_to_samples(AVStream *st, int64_t ts)
 {
     return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den);
diff --git a/libavformat/version.h b/libavformat/version.h
index 75c03fde0a..c17727cf73 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -33,7 +33,7 @@ 
 // Also please add any ticket numbers that you believe might be affected here
 #define LIBAVFORMAT_VERSION_MAJOR  58
 #define LIBAVFORMAT_VERSION_MINOR  48
-#define LIBAVFORMAT_VERSION_MICRO 100
+#define LIBAVFORMAT_VERSION_MICRO 101
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \