On Sat, Apr 09, 2011 at 10:24:53PM +0800, can wu wrote:
> On Sat, Apr 9, 2011 at 9:59 PM, Kostya Shishkov
> <[email protected]>wrote:
> 
> > This implementation should work fine with multiple side information types
> > present.
> >
> > ---
> >  libavcodec/avcodec.h  |   41 +++++++++++++++++
> >  libavcodec/avpacket.c |  117
> > ++++++++++++++++++++++++++++++++++++++++++++----
> >  libavcodec/version.h  |    2 +-
> >  3 files changed, 149 insertions(+), 11 deletions(-)
> >
> > diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
> > index 7eb8b52..98eb0b0 100644
> > --- a/libavcodec/avcodec.h
> > +++ b/libavcodec/avcodec.h
> > @@ -1057,6 +1057,15 @@ typedef struct AVPacket {
> >     int   stream_index;
> >     int   flags;
> >     /**
> > +     * Additional packet data that can be provided by the container.
> > +     * Packet can contain several types of side information.
> > +     */
> > +    uint8_t **side_data;
> > +    int      *side_data_sizes;
> > +    int      *side_data_types;
> > +    int       side_data_elems;
> > +
> > +    /**
> >      * Duration of this packet in AVStream->time_base units, 0 if unknown.
> >      * Equals next_pts - this_pts in presentation order.
> >      */
> > @@ -1089,6 +1098,10 @@ typedef struct AVPacket {
> >  #define PKT_FLAG_KEY AV_PKT_FLAG_KEY
> >  #endif
> >
> > +enum AVPacketSideDataType {
> > +    AV_PKT_DATA_PALETTE,
> > +};
> > +
> >
> should be in another patch
 
indeed, moved to the second patch
 
> >  /**
> >  * Audio Video Frame.
> >  * New fields can be added to the end of FF_COMMON_FRAME with minor version
> > @@ -3202,6 +3215,34 @@ int av_dup_packet(AVPacket *pkt);
> >  */
> >  void av_free_packet(AVPacket *pkt);
> >
> > +/**
> > + * Allocate new information of a packet.
> > + *
> > + * @param pkt packet
> > + * @param type side information type
> > + * @param size side information size
> > + * @return 0 if OK, AVERROR_xxx otherwise
> > + */
> > +int av_packet_new_side_data(AVPacket *pkt, int type, int size);
> > +
> > +/**
> > + * Get side information from packet.
> > + *
> > + * @param pkt packet
> > + * @param type desired side information type
> > + * @return pointer to data if present or NULL otherwise
> > + */
> > +uint8_t* av_packet_get_side_data(AVPacket *pkt, int type);
> > +
> > +/**
> > + * Get side information size from packet.
> > + *
> > + * @param pkt packet
> > + * @param type desired side information type
> > + * @return side information data size if present or 0 otherwise
> > + */
> > +int av_packet_get_side_data_size(AVPacket *pkt, int type);
> > +
> >
> why not one call to get the side data and it's size, as this:
> int av_packet_get_side_data(AVPacket *pkt, int type, void **side_data);

I thought about it but decided against (it's minor issue anyway). I presume
that side information will be mostly of fixed size so both container and codec
don't need to query size.

[...]
> > +
> > +int av_packet_new_side_data(AVPacket *pkt, int type, int size)
> > +{
> > +    int elems = pkt->side_data_elems;
> > +
> > +    if ((unsigned)elems + 1 > INT_MAX / FFMAX(sizeof(*pkt->side_data),
> > +
> >  sizeof(*pkt->side_data_sizes)))
> > +        return AVERROR(ENOMEM);
> > +    if ((unsigned)size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
> > +        return AVERROR(ENOMEM);
> > +
> > +    pkt->side_data = av_realloc(pkt->side_data, (elems + 1) *
> > sizeof(*pkt->side_data));
> > +    if (!pkt->side_data)
> > +        return AVERROR(ENOMEM);
> > +    pkt->side_data_sizes = av_realloc(pkt->side_data_sizes,
> > +                                      (elems + 1) *
> > sizeof(*pkt->side_data_sizes));
> > +    if (!pkt->side_data_sizes)
> > +        return AVERROR(ENOMEM);
> > +    pkt->side_data_types = av_realloc(pkt->side_data_types,
> > +                                      (elems + 1) *
> > sizeof(*pkt->side_data_types));
> > +    if (!pkt->side_data_types)
> > +        return AVERROR(ENOMEM);
> > +
> > +    pkt->side_data[elems] = av_malloc(size +
> > FF_INPUT_BUFFER_PADDING_SIZE);
> > +    if (!pkt->side_data[elems])
> > +        return AVERROR(ENOMEM);
> > +    pkt->side_data_sizes[elems] = size;
> > +    pkt->side_data_types[elems] = type;
> > +    pkt->side_data_elems++;
> > +    return 0;
> > +}
> > +
> >
> not cleanup allocated data when error out, but since the deconstruct will
> free these, maybe it's ok

I don't want to delve into details myself
>From 6fb8045b0a197edaf8b96ac1f26a6f82ad7db8e6 Mon Sep 17 00:00:00 2001
From: Kostya Shishkov <[email protected]>
Date: Sat, 9 Apr 2011 15:31:39 +0200
Subject: [PATCH 1/2] introduce side information for AVPacket

---
 libavcodec/avcodec.h  |   37 +++++++++++++++
 libavcodec/avpacket.c |  117 ++++++++++++++++++++++++++++++++++++++++++++----
 libavcodec/version.h  |    2 +-
 3 files changed, 145 insertions(+), 11 deletions(-)

diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 7eb8b52..dc21549 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -1057,6 +1057,15 @@ typedef struct AVPacket {
     int   stream_index;
     int   flags;
     /**
+     * Additional packet data that can be provided by the container.
+     * Packet can contain several types of side information.
+     */
+    uint8_t **side_data;
+    int      *side_data_sizes;
+    int      *side_data_types;
+    int       side_data_elems;
+
+    /**
      * Duration of this packet in AVStream->time_base units, 0 if unknown.
      * Equals next_pts - this_pts in presentation order.
      */
@@ -3202,6 +3211,34 @@ int av_dup_packet(AVPacket *pkt);
  */
 void av_free_packet(AVPacket *pkt);
 
+/**
+ * Allocate new information of a packet.
+ *
+ * @param pkt packet
+ * @param type side information type
+ * @param size side information size
+ * @return 0 if OK, AVERROR_xxx otherwise
+ */
+int av_packet_new_side_data(AVPacket *pkt, int type, int size);
+
+/**
+ * Get side information from packet.
+ *
+ * @param pkt packet
+ * @param type desired side information type
+ * @return pointer to data if present or NULL otherwise
+ */
+uint8_t* av_packet_get_side_data(AVPacket *pkt, int type);
+
+/**
+ * Get side information size from packet.
+ *
+ * @param pkt packet
+ * @param type desired side information type
+ * @return side information data size if present or 0 otherwise
+ */
+int av_packet_get_side_data_size(AVPacket *pkt, int type);
+
 /* resample.c */
 
 struct ReSampleContext;
diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c
index f6aef20..bbf5978 100644
--- a/libavcodec/avpacket.c
+++ b/libavcodec/avpacket.c
@@ -26,12 +26,25 @@
 void av_destruct_packet_nofree(AVPacket *pkt)
 {
     pkt->data = NULL; pkt->size = 0;
+    pkt->side_data       = NULL;
+    pkt->side_data_sizes = NULL;
+    pkt->side_data_types = NULL;
+    pkt->side_data_elems = 0;
 }
 
 void av_destruct_packet(AVPacket *pkt)
 {
+    int i;
+
     av_free(pkt->data);
     pkt->data = NULL; pkt->size = 0;
+
+    for (i = 0; i < pkt->side_data_elems; i++)
+        av_free(pkt->side_data[i]);
+    av_freep(&pkt->side_data);
+    av_freep(&pkt->side_data_sizes);
+    av_freep(&pkt->side_data_types);
+    pkt->side_data_elems = 0;
 }
 
 void av_init_packet(AVPacket *pkt)
@@ -44,6 +57,10 @@ void av_init_packet(AVPacket *pkt)
     pkt->flags = 0;
     pkt->stream_index = 0;
     pkt->destruct= NULL;
+    pkt->side_data       = NULL;
+    pkt->side_data_sizes = NULL;
+    pkt->side_data_types = NULL;
+    pkt->side_data_elems = 0;
 }
 
 int av_new_packet(AVPacket *pkt, int size)
@@ -89,21 +106,46 @@ int av_grow_packet(AVPacket *pkt, int grow_by)
     return 0;
 }
 
+#define DUP_DATA(dst, size, tmp, padding) \
+    do { \
+        if (padding) { \
+            if ((unsigned)(size) > (unsigned)(size) + FF_INPUT_BUFFER_PADDING_SIZE) \
+                return AVERROR(ENOMEM); \
+            tmp = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); \
+        } else { \
+            tmp = av_malloc(size); \
+        } \
+        if (!data) \
+            return AVERROR(ENOMEM); \
+        memcpy(tmp, dst, size); \
+        if (padding) \
+            memset(tmp + size, 0, FF_INPUT_BUFFER_PADDING_SIZE); \
+        dst = tmp; \
+    } while(0)
+
 int av_dup_packet(AVPacket *pkt)
 {
     if (((pkt->destruct == av_destruct_packet_nofree) || (pkt->destruct == NULL)) && pkt->data) {
         uint8_t *data;
-        /* We duplicate the packet and don't forget to add the padding again. */
-        if((unsigned)pkt->size > (unsigned)pkt->size + FF_INPUT_BUFFER_PADDING_SIZE)
-            return AVERROR(ENOMEM);
-        data = av_malloc(pkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
-        if (!data) {
-            return AVERROR(ENOMEM);
-        }
-        memcpy(data, pkt->data, pkt->size);
-        memset(data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
-        pkt->data = data;
+
+        DUP_DATA(pkt->data, pkt->size, data, 1);
         pkt->destruct = av_destruct_packet;
+
+        if (pkt->side_data_elems) {
+            int i;
+
+            DUP_DATA(pkt->side_data, pkt->side_data_elems * sizeof(*pkt->side_data),
+                     data, 0);
+            DUP_DATA(pkt->side_data_sizes,
+                     pkt->side_data_elems * sizeof(*pkt->side_data_sizes),
+                     data, 0);
+            DUP_DATA(pkt->side_data_types,
+                     pkt->side_data_elems * sizeof(*pkt->side_data_types),
+                     data, 0);
+            for (i = 0; i < pkt->side_data_elems; i++) {
+                DUP_DATA(pkt->side_data[i], pkt->side_data_sizes[i], data, 1);
+            }
+        }
     }
     return 0;
 }
@@ -113,5 +155,60 @@ void av_free_packet(AVPacket *pkt)
     if (pkt) {
         if (pkt->destruct) pkt->destruct(pkt);
         pkt->data = NULL; pkt->size = 0;
+        pkt->side_data       = NULL;
+        pkt->side_data_sizes = NULL;
+        pkt->side_data_types = NULL;
+        pkt->side_data_elems = 0;
     }
 }
+
+int av_packet_new_side_data(AVPacket *pkt, int type, int size)
+{
+    int elems = pkt->side_data_elems;
+
+    if ((unsigned)elems + 1 > INT_MAX / FFMAX(sizeof(*pkt->side_data),
+                                            sizeof(*pkt->side_data_sizes)))
+        return AVERROR(ENOMEM);
+    if ((unsigned)size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
+        return AVERROR(ENOMEM);
+
+    pkt->side_data = av_realloc(pkt->side_data, (elems + 1) * sizeof(*pkt->side_data));
+    if (!pkt->side_data)
+        return AVERROR(ENOMEM);
+    pkt->side_data_sizes = av_realloc(pkt->side_data_sizes,
+                                      (elems + 1) * sizeof(*pkt->side_data_sizes));
+    if (!pkt->side_data_sizes)
+        return AVERROR(ENOMEM);
+    pkt->side_data_types = av_realloc(pkt->side_data_types,
+                                      (elems + 1) * sizeof(*pkt->side_data_types));
+    if (!pkt->side_data_types)
+        return AVERROR(ENOMEM);
+
+    pkt->side_data[elems] = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
+    if (!pkt->side_data[elems])
+        return AVERROR(ENOMEM);
+    pkt->side_data_sizes[elems] = size;
+    pkt->side_data_types[elems] = type;
+    pkt->side_data_elems++;
+    return 0;
+}
+
+uint8_t* av_packet_get_side_data(AVPacket *pkt, int type)
+{
+    int i;
+
+    for (i = 0; i < pkt->side_data_elems; i++)
+        if (pkt->side_data_types[i] == type)
+            return pkt->side_data[i];
+    return NULL;
+}
+
+int av_packet_get_side_data_size(AVPacket *pkt, int type)
+{
+    int i;
+
+    for (i = 0; i < pkt->side_data_elems; i++)
+        if (pkt->side_data_types[i] == type)
+            return pkt->side_data_sizes[i];
+    return 0;
+}
diff --git a/libavcodec/version.h b/libavcodec/version.h
index b60e264..ac79d82 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -21,7 +21,7 @@
 #define AVCODEC_VERSION_H
 
 #define LIBAVCODEC_VERSION_MAJOR 52
-#define LIBAVCODEC_VERSION_MINOR 117
+#define LIBAVCODEC_VERSION_MINOR 118
 #define LIBAVCODEC_VERSION_MICRO  0
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
-- 
1.7.0.4

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to