[FFmpeg-devel] [PATCH 1/5] vorbis: handle special packets in the middle of a stream

Ben Boeckel mathstuf at gmail.com
Sat Oct 19 15:40:42 CEST 2013


This allows for updating metadata from new metadata packets in the
middle of a stream (e.g., MPD streams). There still needs to be a signal
that there *is* new metadata, but this is at least gets the data into a
data structure.
---
 libavcodec/vorbis_parser.c   | 20 ++++++++++++++++++--
 libavcodec/vorbis_parser.h   |  6 +++++-
 libavformat/oggparsevorbis.c | 17 +++++++++++++----
 3 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/libavcodec/vorbis_parser.c b/libavcodec/vorbis_parser.c
index fcbecc8..d3d7ab7 100644
--- a/libavcodec/vorbis_parser.c
+++ b/libavcodec/vorbis_parser.c
@@ -202,7 +202,7 @@ int avpriv_vorbis_parse_extradata(AVCodecContext *avctx, VorbisParseContext *s)
 }
 
 int avpriv_vorbis_parse_frame(VorbisParseContext *s, const uint8_t *buf,
-                              int buf_size)
+                              int buf_size, int *flags)
 {
     int duration = 0;
 
@@ -211,6 +211,22 @@ int avpriv_vorbis_parse_frame(VorbisParseContext *s, const uint8_t *buf,
         int previous_blocksize = s->previous_blocksize;
 
         if (buf[0] & 1) {
+            /* If the user doesn't care about special packets, it's a bad one. */
+            if (!flags)
+                goto bad_packet;
+
+            /* Set the flag for which kind of special packet it is. */
+            if (buf[0] == 1)
+                *flags |= VORBIS_FLAG_HEADER;
+            else if (buf[0] == 3)
+                *flags |= VORBIS_FLAG_COMMENT;
+            else
+                goto bad_packet;
+
+            /* Special packets have no duration. */
+            return 0;
+
+bad_packet:
             av_log(s->avctx, AV_LOG_ERROR, "Invalid packet\n");
             return AVERROR_INVALIDDATA;
         }
@@ -252,7 +268,7 @@ static int vorbis_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
         if (avpriv_vorbis_parse_extradata(avctx, s))
             goto end;
 
-    if ((duration = avpriv_vorbis_parse_frame(s, buf, buf_size)) >= 0)
+    if ((duration = avpriv_vorbis_parse_frame(s, buf, buf_size, NULL)) >= 0)
         s1->duration = duration;
 
 end:
diff --git a/libavcodec/vorbis_parser.h b/libavcodec/vorbis_parser.h
index 101df5d..54b780b 100644
--- a/libavcodec/vorbis_parser.h
+++ b/libavcodec/vorbis_parser.h
@@ -50,6 +50,9 @@ typedef struct VorbisParseContext {
  */
 int avpriv_vorbis_parse_extradata(AVCodecContext *avctx, VorbisParseContext *s);
 
+#define VORBIS_FLAG_HEADER  0x00000001
+#define VORBIS_FLAG_COMMENT 0x00000002
+
 /**
  * Get the duration for a Vorbis packet.
  *
@@ -59,9 +62,10 @@ int avpriv_vorbis_parse_extradata(AVCodecContext *avctx, VorbisParseContext *s);
  * @param s        Vorbis parser context
  * @param buf      buffer containing a Vorbis frame
  * @param buf_size size of the buffer
+ * @param flags    flags for special frames (NULL for "don't care")
  */
 int avpriv_vorbis_parse_frame(VorbisParseContext *s, const uint8_t *buf,
-                              int buf_size);
+                              int buf_size, int *flags);
 
 void avpriv_vorbis_parse_reset(VorbisParseContext *s);
 
diff --git a/libavformat/oggparsevorbis.c b/libavformat/oggparsevorbis.c
index 36ad738..cca6de7 100644
--- a/libavformat/oggparsevorbis.c
+++ b/libavformat/oggparsevorbis.c
@@ -345,7 +345,7 @@ static int vorbis_packet(AVFormatContext *s, int idx)
     struct ogg *ogg = s->priv_data;
     struct ogg_stream *os = ogg->streams + idx;
     struct oggvorbis_private *priv = os->private;
-    int duration;
+    int duration, flags = 0;
 
     /* first packet handling
      * here we parse the duration of each packet in the first page and compare
@@ -359,19 +359,25 @@ static int vorbis_packet(AVFormatContext *s, int idx)
         avpriv_vorbis_parse_reset(&priv->vp);
         duration = 0;
         seg = os->segp;
-        d = avpriv_vorbis_parse_frame(&priv->vp, last_pkt, 1);
+        d = avpriv_vorbis_parse_frame(&priv->vp, last_pkt, 1, &flags);
         if (d < 0) {
             os->pflags |= AV_PKT_FLAG_CORRUPT;
             return 0;
+        } else if (flags) {
+            vorbis_header(s, idx);
+            flags = 0;
         }
         duration += d;
         last_pkt = next_pkt =  next_pkt + os->psize;
         for (; seg < os->nsegs; seg++) {
             if (os->segments[seg] < 255) {
-                int d = avpriv_vorbis_parse_frame(&priv->vp, last_pkt, 1);
+                int d = avpriv_vorbis_parse_frame(&priv->vp, last_pkt, 1, &flags);
                 if (d < 0) {
                     duration = os->granule;
                     break;
+                } else if (flags) {
+                    vorbis_header(s, idx);
+                    flags = 0;
                 }
                 duration += d;
                 last_pkt  = next_pkt + os->segments[seg];
@@ -391,10 +397,13 @@ static int vorbis_packet(AVFormatContext *s, int idx)
 
     /* parse packet duration */
     if (os->psize > 0) {
-        duration = avpriv_vorbis_parse_frame(&priv->vp, os->buf + os->pstart, 1);
+        duration = avpriv_vorbis_parse_frame(&priv->vp, os->buf + os->pstart, 1, &flags);
         if (duration < 0) {
             os->pflags |= AV_PKT_FLAG_CORRUPT;
             return 0;
+        } else if (!duration) {
+            vorbis_header(s, idx);
+            flags = 0;
         }
         os->pduration = duration;
     }
-- 
1.8.3.1



More information about the ffmpeg-devel mailing list