On 3/24/16, Paul B Mahol <one...@gmail.com> wrote:
> Hi,
>
> patch attached.
>

Fixed version attached.
From 23088e3cf4fef14c8d344403c4124e426db1493b Mon Sep 17 00:00:00 2001
From: Paul B Mahol <one...@gmail.com>
Date: Thu, 24 Mar 2016 12:35:54 +0100
Subject: [PATCH] avcodec: add jpeg2000 parser

Signed-off-by: Paul B Mahol <one...@gmail.com>
---
 libavcodec/Makefile          |   1 +
 libavcodec/allcodecs.c       |   1 +
 libavcodec/jpeg2000_parser.c | 127 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 129 insertions(+)
 create mode 100644 libavcodec/jpeg2000_parser.c

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index ef9eb98..50a24ac 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -898,6 +898,7 @@ OBJS-$(CONFIG_H261_PARSER)             += h261_parser.o
 OBJS-$(CONFIG_H263_PARSER)             += h263_parser.o
 OBJS-$(CONFIG_H264_PARSER)             += h264_parser.o
 OBJS-$(CONFIG_HEVC_PARSER)             += hevc_parser.o hevc_parse.o hevc_ps.o hevc_data.o
+OBJS-$(CONFIG_JPEG2000_PARSER)         += jpeg2000_parser.o
 OBJS-$(CONFIG_MJPEG_PARSER)            += mjpeg_parser.o
 OBJS-$(CONFIG_MLP_PARSER)              += mlp_parser.o mlp.o
 OBJS-$(CONFIG_MPEG4VIDEO_PARSER)       += mpeg4video_parser.o h263.o \
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index a953a16..0a907ce 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -650,6 +650,7 @@ void avcodec_register_all(void)
     REGISTER_PARSER(H263,               h263);
     REGISTER_PARSER(H264,               h264);
     REGISTER_PARSER(HEVC,               hevc);
+    REGISTER_PARSER(JPEG2000,           jpeg2000);
     REGISTER_PARSER(MJPEG,              mjpeg);
     REGISTER_PARSER(MLP,                mlp);
     REGISTER_PARSER(MPEG4VIDEO,         mpeg4video);
diff --git a/libavcodec/jpeg2000_parser.c b/libavcodec/jpeg2000_parser.c
new file mode 100644
index 0000000..3c11a88
--- /dev/null
+++ b/libavcodec/jpeg2000_parser.c
@@ -0,0 +1,127 @@
+/*
+ * JPEG2000 parser
+ * Copyright (c) 2016 Paul B Mahol
+ *
+ * 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 "parser.h"
+#include "jpeg2000.h"
+
+typedef struct J2KParseContext {
+    ParseContext pc;
+    uint32_t index;
+    uint32_t length;
+    uint32_t remaining_size;
+    uint32_t data_size;
+    int inside_sot;
+} J2KParseContext;
+
+static int j2k_parse(AVCodecParserContext *s, AVCodecContext *avctx,
+                     const uint8_t **poutbuf, int *poutbuf_size,
+                     const uint8_t *buf, int buf_size)
+{
+    J2KParseContext *ppc = s->priv_data;
+    int next = END_NOT_FOUND;
+    int i = 0;
+
+    s->pict_type = AV_PICTURE_TYPE_I;
+
+    *poutbuf_size = 0;
+
+    if (!ppc->pc.frame_start_found) {
+        uint64_t state64 = ppc->pc.state64;
+        for (; i < buf_size; i++) {
+            state64 = (state64 << 8) | buf[i];
+            if ((state64 & 0xFFFF) == JPEG2000_SOC) {
+                ppc->pc.frame_start_found = 1;
+                ppc->index = 0;
+                break;
+            }
+        }
+        ppc->pc.state64 = state64;
+    } else if (ppc->remaining_size) {
+        i = FFMIN(ppc->remaining_size, buf_size);
+        ppc->remaining_size -= i;
+        if (ppc->remaining_size)
+            goto flush;
+    }
+
+    for (; ppc->pc.frame_start_found && i < buf_size; i++) {
+        ppc->pc.state64 = (ppc->pc.state64 << 8) | buf[i];
+        if (ppc->inside_sot) {
+            ppc->inside_sot++;
+            if (ppc->inside_sot >= 11) {
+                ppc->data_size = (ppc->pc.state64 >> 16) & 0xFFFFFFFF;
+                if (ppc->data_size <= 14) {
+                    ppc->index = ppc->inside_sot = ppc->pc.frame_start_found = 0;
+                    break;
+                }
+                ppc->data_size -= 14;
+                ppc->inside_sot = 0;
+            }
+        } else if ((ppc->pc.state64 & 0xFFFF) == JPEG2000_EOC) {
+            next = i + 1;
+            break;
+        } else if ((ppc->pc.state64 & 0xFFFF) == JPEG2000_SOT) {
+            ppc->inside_sot = 1;
+            ppc->remaining_size = 0;
+            ppc->index = 0;
+        } else if ((ppc->pc.state64 & 0xFFFF) == JPEG2000_SOD) {
+            if (ppc->data_size >= buf_size - i) {
+                ppc->remaining_size = ppc->data_size - buf_size + i;
+                break;
+            } else {
+                i += ppc->data_size;
+            }
+        } else {
+            if (ppc->index >= 4) {
+                ppc->index = 0;
+                ppc->length = ppc->pc.state64 & 0xFFFF;
+                if (ppc->length < 2) {
+                    ppc->index = ppc->pc.frame_start_found = 0;
+                    break;
+                }
+                ppc->length -= 2;
+                if (ppc->length >= buf_size - i) {
+                    ppc->remaining_size = ppc->length - buf_size + i;
+                    break;
+                } else {
+                    i += ppc->length;
+                }
+            }
+            ppc->index++;
+        }
+    }
+
+flush:
+    if (ff_combine_frame(&ppc->pc, next, &buf, &buf_size) < 0)
+        return buf_size;
+
+    ppc->index = ppc->pc.frame_start_found = 0;
+
+    *poutbuf      = buf;
+    *poutbuf_size = buf_size;
+    return next;
+}
+
+AVCodecParser ff_jpeg2000_parser = {
+    .codec_ids      = { AV_CODEC_ID_JPEG2000 },
+    .priv_data_size = sizeof(J2KParseContext),
+    .parser_parse   = j2k_parse,
+    .parser_close   = ff_parse_close,
+};
-- 
1.9.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel

Reply via email to