[FFmpeg-cvslog] vp9: split packet parsing into an AVParser.

Ronald S. Bultje git at videolan.org
Sat Nov 30 22:40:04 CET 2013


ffmpeg | branch: master | Ronald S. Bultje <rsbultje at gmail.com> | Sat Nov 23 09:04:39 2013 -0500| [84d362f0209148ba86f438c41ca6c05da00ef298] | committer: Ronald S. Bultje

vp9: split packet parsing into an AVParser.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=84d362f0209148ba86f438c41ca6c05da00ef298
---

 libavcodec/Makefile     |    1 +
 libavcodec/allcodecs.c  |    1 +
 libavcodec/vp9.c        |   57 ++----------------------
 libavcodec/vp9_parser.c |  111 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 117 insertions(+), 53 deletions(-)

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 918013c..d947372 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -787,6 +787,7 @@ OBJS-$(CONFIG_VC1_PARSER)              += vc1_parser.o vc1.o vc1data.o \
 OBJS-$(CONFIG_VORBIS_PARSER)           += vorbis_parser.o xiph.o
 OBJS-$(CONFIG_VP3_PARSER)              += vp3_parser.o
 OBJS-$(CONFIG_VP8_PARSER)              += vp8_parser.o
+OBJS-$(CONFIG_VP9_PARSER)              += vp9_parser.o
 
 # bitstream filters
 OBJS-$(CONFIG_AAC_ADTSTOASC_BSF)          += aac_adtstoasc_bsf.o aacadtsdec.o \
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 49200a5..a47a85d 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -555,6 +555,7 @@ void avcodec_register_all(void)
     REGISTER_PARSER(VORBIS,             vorbis);
     REGISTER_PARSER(VP3,                vp3);
     REGISTER_PARSER(VP8,                vp8);
+    REGISTER_PARSER(VP9,                vp9);
 
     /* bitstream filters */
     REGISTER_BSF(AAC_ADTSTOASC,         aac_adtstoasc);
diff --git a/libavcodec/vp9.c b/libavcodec/vp9.c
index 28844b9..91f4eeb 100644
--- a/libavcodec/vp9.c
+++ b/libavcodec/vp9.c
@@ -3304,8 +3304,10 @@ static av_cold int vp9_decode_free(AVCodecContext *ctx)
 
 
 static int vp9_decode_frame(AVCodecContext *ctx, AVFrame *frame,
-                            int *got_frame, const uint8_t *data, int size)
+                            int *got_frame, AVPacket *pkt)
 {
+    const uint8_t *data = pkt->data;
+    int size = pkt->size;
     VP9Context *s = ctx->priv_data;
     int res, tile_row, tile_col, i, ref, row, col;
     ptrdiff_t yoff = 0, uvoff = 0;
@@ -3469,57 +3471,6 @@ static int vp9_decode_frame(AVCodecContext *ctx, AVFrame *frame,
     return 0;
 }
 
-static int vp9_decode_packet(AVCodecContext *avctx, AVFrame *frame,
-                             int *got_frame, AVPacket *avpkt)
-{
-    const uint8_t *data = avpkt->data;
-    int size = avpkt->size, marker, res;
-
-    // read superframe index - this is a collection of individual frames that
-    // together lead to one visible frame
-    av_assert1(size > 0); // without CODEC_CAP_DELAY, this is implied
-    marker = data[size - 1];
-    if ((marker & 0xe0) == 0xc0) {
-        int nbytes = 1 + ((marker >> 3) & 0x3);
-        int n_frames = 1 + (marker & 0x7), idx_sz = 2 + n_frames * nbytes;
-
-        if (size >= idx_sz && data[size - idx_sz] == marker) {
-            const uint8_t *idx = data + size + 1 - idx_sz;
-            switch (nbytes) {
-#define case_n(a, rd) \
-                case a: \
-                    while (n_frames--) { \
-                        int sz = rd; \
-                        idx += a; \
-                        if (sz > size) { \
-                            av_log(avctx, AV_LOG_ERROR, \
-                                   "Superframe packet size too big: %d > %d\n", \
-                                   sz, size); \
-                            return AVERROR_INVALIDDATA; \
-                        } \
-                        res = vp9_decode_frame(avctx, frame, got_frame, \
-                                               data, sz); \
-                        if (res < 0) \
-                            return res; \
-                        data += sz; \
-                        size -= sz; \
-                    } \
-                    break;
-                case_n(1, *idx);
-                case_n(2, AV_RL16(idx));
-                case_n(3, AV_RL24(idx));
-                case_n(4, AV_RL32(idx));
-            }
-            return avpkt->size;
-        }
-    }
-    // if we get here, there was no valid superframe index, i.e. this is just
-    // one whole single frame - decode it as such from the complete input buf
-    if ((res = vp9_decode_frame(avctx, frame, got_frame, data, size)) < 0)
-        return res;
-    return avpkt->size;
-}
-
 static void vp9_decode_flush(AVCodecContext *ctx)
 {
     VP9Context *s = ctx->priv_data;
@@ -3559,7 +3510,7 @@ AVCodec ff_vp9_decoder = {
   .priv_data_size        = sizeof(VP9Context),
   .init                  = vp9_decode_init,
   .close                 = vp9_decode_free,
-  .decode                = vp9_decode_packet,
   .capabilities          = CODEC_CAP_DR1,
   .flush                 = vp9_decode_flush,
+    .decode                = vp9_decode_frame,
 };
diff --git a/libavcodec/vp9_parser.c b/libavcodec/vp9_parser.c
new file mode 100644
index 0000000..6301936
--- /dev/null
+++ b/libavcodec/vp9_parser.c
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2008 Michael Niedermayer
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/intreadwrite.h"
+#include "parser.h"
+
+typedef struct VP9ParseContext {
+    int n_frames; // 1-8
+    int size[8];
+} VP9ParseContext;
+
+static void parse_frame(AVCodecParserContext *ctx, const uint8_t *buf, int size)
+{
+    if (buf[0] & 0x4) {
+        ctx->pict_type = AV_PICTURE_TYPE_P;
+        ctx->key_frame = 0;
+    } else {
+        ctx->pict_type = AV_PICTURE_TYPE_I;
+        ctx->key_frame = 1;
+    }
+}
+
+static int parse(AVCodecParserContext *ctx,
+                 AVCodecContext *avctx,
+                 const uint8_t **out_data, int *out_size,
+                 const uint8_t *data, int size)
+{
+    VP9ParseContext *s = ctx->priv_data;
+    int marker;
+
+    if (s->n_frames > 0) {
+        *out_data = data;
+        *out_size = s->size[--s->n_frames];
+        parse_frame(ctx, *out_data, *out_size);
+
+        return s->n_frames > 0 ? *out_size : size /* i.e. include idx tail */;
+    }
+
+    marker = data[size - 1];
+    if ((marker & 0xe0) == 0xc0) {
+        int nbytes = 1 + ((marker >> 3) & 0x3);
+        int n_frames = 1 + (marker & 0x7), idx_sz = 2 + n_frames * nbytes;
+
+        if (size >= idx_sz && data[size - idx_sz] == marker) {
+            const uint8_t *idx = data + size + 1 - idx_sz;
+            int first = 1;
+
+            switch (nbytes) {
+#define case_n(a, rd) \
+            case a: \
+                while (n_frames--) { \
+                    int sz = rd; \
+                    idx += a; \
+                    if (sz > size) { \
+                        s->n_frames = 0; \
+                        av_log(ctx, AV_LOG_ERROR, \
+                               "Superframe packet size too big: %d > %d\n", \
+                               sz, size); \
+                        return AVERROR_INVALIDDATA; \
+                    } \
+                    if (first) { \
+                        first = 0; \
+                        *out_data = data; \
+                        *out_size = sz; \
+                        s->n_frames = n_frames; \
+                    } else { \
+                        s->size[n_frames] = sz; \
+                    } \
+                    data += sz; \
+                    size -= sz; \
+                } \
+                parse_frame(ctx, *out_data, *out_size); \
+                return *out_size
+
+                case_n(1, *idx);
+                case_n(2, AV_RL16(idx));
+                case_n(3, AV_RL24(idx));
+                case_n(4, AV_RL32(idx));
+            }
+        }
+    }
+
+    *out_data = data;
+    *out_size = size;
+    parse_frame(ctx, data, size);
+
+    return size;
+}
+
+AVCodecParser ff_vp9_parser = {
+    .codec_ids      = { AV_CODEC_ID_VP9 },
+    .priv_data_size = sizeof(VP9ParseContext),
+    .parser_parse   = parse,
+};



More information about the ffmpeg-cvslog mailing list