[PATCH 2/2] png parser

Peter Holik peter
Wed May 27 15:05:43 CEST 2009


Signed-off-by: Peter Holik <peter at holik.at>
---
 libavcodec/Makefile     |    1 +
 libavcodec/png_parser.c |   95 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 96 insertions(+), 0 deletions(-)
 create mode 100644 libavcodec/png_parser.c

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 14ef9f4..189231b 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -411,6 +411,7 @@ OBJS-$(CONFIG_MLP_PARSER)              += mlp_parser.o mlp.o
 OBJS-$(CONFIG_MPEG4VIDEO_PARSER)       += mpeg4video_parser.o h263.o mpeg12data.o mpegvideo.o error_resilience.o
 OBJS-$(CONFIG_MPEGAUDIO_PARSER)        += mpegaudio_parser.o mpegaudiodecheader.o mpegaudiodata.o
 OBJS-$(CONFIG_MPEGVIDEO_PARSER)        += mpegvideo_parser.o mpeg12.o mpeg12data.o mpegvideo.o error_resilience.o
+OBJS-$(CONFIG_PNG_PARSER)              += png_parser.o
 OBJS-$(CONFIG_PNM_PARSER)              += pnm_parser.o pnm.o
 OBJS-$(CONFIG_VC1_PARSER)              += vc1_parser.o
 OBJS-$(CONFIG_VP3_PARSER)              += vp3_parser.o
diff --git a/libavcodec/png_parser.c b/libavcodec/png_parser.c
new file mode 100644
index 0000000..1293ef2
--- /dev/null
+++ b/libavcodec/png_parser.c
@@ -0,0 +1,95 @@
+/*
+ * PNG parser
+ * Copyright (c) 2009 Peter Holik
+ *
+ * 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
+ */
+
+/**
+ * @file libavcodec/png_parser.c
+ * PNG parser
+ */
+
+#include "parser.h"
+
+#define PNGSIG 0x89504e470d0a1a0a
+#define MNGSIG 0x8a4d4e470d0a1a0a
+
+/**
+ * Finds the end of the current frame in the bitstream.
+ * @return the position of the first byte of the next frame, or -1
+ */
+static int find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
+{
+    int i = 0;
+    uint64_t state = pc->state64;
+
+    if (!pc->frame_start_found) {
+        for (i = 0; i < buf_size; i++) {
+            state = (state << 8) | buf[i];
+            if (state == PNGSIG || state == MNGSIG) {
+                i++;
+                pc->frame_start_found = 1;
+                break;
+            }
+        }
+    }
+
+    if (pc->frame_start_found) {
+        /* EOF considered as end of frame */
+        if (buf_size == 0)
+            return 0;
+        for(; i < buf_size; i++){
+            state = (state << 8) | buf[i];
+            if (state == PNGSIG || state == MNGSIG) {
+                pc->frame_start_found = 0;
+                pc->state64 = 0;
+                return i - 7;
+            }
+        }
+    }
+    pc->state64 = state;
+    return END_NOT_FOUND;
+}
+
+static int png_parse(AVCodecParserContext *s, AVCodecContext *avctx,
+                     const uint8_t **poutbuf, int *poutbuf_size,
+                     const uint8_t *buf, int buf_size)
+{
+    ParseContext *pc = s->priv_data;
+    int next;
+
+    next = find_frame_end(pc, buf, buf_size);
+
+    if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
+        *poutbuf = NULL;
+        *poutbuf_size = 0;
+        return buf_size;
+    }
+
+    *poutbuf = buf;
+    *poutbuf_size = buf_size;
+    return next;
+}
+
+AVCodecParser png_parser = {
+    { CODEC_ID_PNG },
+    sizeof(ParseContext),
+    NULL,
+    png_parse,
+    ff_parse_close,
+};
-- 
1.6.3.1

------=_20090527151626_51370--





More information about the ffmpeg-devel mailing list