From patchwork Thu Jun 4 07:54:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gautam Ramakrishnan X-Patchwork-Id: 20145 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 94E6744BBC9 for ; Thu, 4 Jun 2020 11:02:21 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7070868AFFB; Thu, 4 Jun 2020 11:02:21 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl1-f195.google.com (mail-pl1-f195.google.com [209.85.214.195]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6A30968ACE4 for ; Thu, 4 Jun 2020 11:02:14 +0300 (EEST) Received: by mail-pl1-f195.google.com with SMTP id bh7so1883996plb.11 for ; Thu, 04 Jun 2020 01:02:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=Kz8MFfYchzf6K+KD3JyjpkAnnC5/ZR3oxiVo1ck25oo=; b=YMkga3YLRqA12sa1Qz/QycCGjQEWffohSqURofTwhDV3D7NrjDasalnAA8A09UOaUu w+ZYPLpUOwA9p9CfkV3/sowifqJf6Ymvadpva/1vllZpYjilzLzAPIjg61CSvR0dvuWA oa20Hr4Vx7jFan3UCAyky/U4VkUHFpTZPGaBhreMX73th6oZ1pyMIL5AilL+gNaiusve mpiUEBO7PE24Za0jHPjWVAPUG0O4oOzUglRryDgSZoTkPe1t86XqbSkZTcoIESD8o43c Xq1f8uc8ibnADUUsos7tHMRGRBvjct9QIqY1nLb3ZmhSK7fbB6NBwd7g29YQqeO+FdpR W7qQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=Kz8MFfYchzf6K+KD3JyjpkAnnC5/ZR3oxiVo1ck25oo=; b=aV85Eszgfuzthtz9AXJznrjPjttVN8NCDLI7E4dJoGIGmwngkk7d+MoVOOgx4l8S7d Gwb4Vs2YOZ+zjdNtJqZtpg2byqvOeMFgXU53Ycd0PbJgAgxt63MvlUvzBOc2KV6EqoU1 44NbQi21khOdcUt+wfQLs5dHHCkA9F7tbdUHggRCPhRyKm5r89w9Rfv5E3gm1yOpdDJS 9wUtqpOstXGXncxmMaVbA7fugf0m0MbqQ4dkYDqt6PI8+iMvf/lSDjEGJiTWy8dIJpC5 ZcietCOlFukNv8pxgrmrt5Np62iH9Rqx6fSSZJHCGzdXRSgf7S20ASxDhzd5xKwQxNKJ VSLg== X-Gm-Message-State: AOAM531BTxaRruwNHDXIrsTFw8+41VHGyGXCmDNN0X/Adq+uenG91BTf mPaslCREI86Ekt86R94aDkHTh59EQ9Q= X-Google-Smtp-Source: ABdhPJxCuwBLAE1DrtibW+xkpki68b5nywlh4RvCalxHOrMePdlrAj/AY5LvN1BNnx32aCKgzdDkow== X-Received: by 2002:aa7:9f11:: with SMTP id g17mr3071780pfr.278.1591257295479; Thu, 04 Jun 2020 00:54:55 -0700 (PDT) Received: from localhost.localdomain ([122.172.136.215]) by smtp.gmail.com with ESMTPSA id h3sm4444383pjk.10.2020.06.04.00.54.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jun 2020 00:54:54 -0700 (PDT) From: gautamramk@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Thu, 4 Jun 2020 13:24:42 +0530 Message-Id: <20200604075443.22767-1-gautamramk@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [RFC PATCH 1/2] libavcodec/jpeg2000_parser: Add jpeg2000 parser X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Gautam Ramakrishnan MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Gautam Ramakrishnan I have attempted to write a JPEG2000 Parser. Have tested by generating a file containing 14 frames, as mentioned by Micheal. Have also tried testing with various packet sizes by setting -frame_size option. --- libavcodec/Makefile | 1 + libavcodec/jpeg2000_parser.c | 190 +++++++++++++++++++++++++++++++++++ libavcodec/parsers.c | 1 + 3 files changed, 192 insertions(+) create mode 100644 libavcodec/jpeg2000_parser.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 0a3bbc7128..5a6ea59715 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1074,6 +1074,7 @@ OBJS-$(CONFIG_H261_PARSER) += h261_parser.o OBJS-$(CONFIG_H263_PARSER) += h263_parser.o OBJS-$(CONFIG_H264_PARSER) += h264_parser.o h264_sei.o h264data.o OBJS-$(CONFIG_HEVC_PARSER) += hevc_parser.o hevc_data.o +OBJS-$(CONFIG_JPEG2000_PARSER) += jpeg2000_parser.o OBJS-$(CONFIG_MJPEG_PARSER) += mjpeg_parser.o OBJS-$(CONFIG_MLP_PARSER) += mlp_parse.o mlp_parser.o mlp.o OBJS-$(CONFIG_MPEG4VIDEO_PARSER) += mpeg4video_parser.o h263.o \ diff --git a/libavcodec/jpeg2000_parser.c b/libavcodec/jpeg2000_parser.c new file mode 100644 index 0000000000..06571d30e3 --- /dev/null +++ b/libavcodec/jpeg2000_parser.c @@ -0,0 +1,190 @@ +/* + * JPEG2000 parser + * Copyright (c) 2020 Gautam Ramakrishnan + * + * 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 + * JPEG2000 parser. + */ + +#include "parser.h" + +/* Whether frame is jp2 file or codestream +*/ +enum frame_type { + jp2_file = 1, + j2k_cstream +}; + +typedef struct JPEG2000ParserContext{ + ParseContext pc; + uint64_t bytes_read; + uint64_t fheader_state; + uint32_t skip_bytes; // skip bytes inside codestream data + enum frame_type ft; // 1 if file, 2 if codestream + uint8_t fheader_read; // are we reading + uint8_t reading_file_header; + uint8_t skipped_codestream; + uint8_t codestream_frame_end; + uint8_t read_tp; + uint8_t in_codestream; +}JPEG2000ParserContext; + +static inline void reset_context(JPEG2000ParserContext *m) +{ + ParseContext *pc = &m->pc; + + pc->frame_start_found= 0; + pc->state = 0; + m->bytes_read = 0; + m->ft = 0; + m->skipped_codestream = 0; + m->fheader_read = 0; + m->codestream_frame_end = 0; + m->skip_bytes = 0; + m->read_tp = 0; + m->in_codestream = 0; +} + +/* Returns 1 if marker has any data which can be skipped +*/ +static uint8_t info_marker(uint16_t marker) +{ + if (marker == 0xFF92 || marker == 0xFF4F || + marker == 0xFF90 || marker == 0xFF93 || + marker == 0xFFD9) + return 0; + else + if (marker > 0xFF00) return 1; + return 0; +} + +/** + * Find 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(JPEG2000ParserContext *m, const uint8_t *buf, int buf_size) +{ + ParseContext *pc= &m->pc; + int i; + uint32_t state; + uint64_t state64; + state= pc->state; + state64 = pc->state64; + if (buf_size == 0) { + return 0; + } + + for (i = 0; i < buf_size; i++) { + state = state << 8 | buf[i]; + state64 = state64 << 8 | buf[i]; + m->bytes_read++; + if (m->skip_bytes) { + m->skip_bytes--; + continue; + } + if (m->codestream_frame_end) { + reset_context(m); + return i; + } + if (m->read_tp) { // Find out how many bytes inside Tile part codestream to skip. + if (m->read_tp == 1){ + m->skip_bytes = (state64 & 0xFFFFFFFF) - 10 > 0? + (state64 & 0xFFFFFFFF) - 10 : 0; + } + m->read_tp--; + } + if (m->fheader_read) { + if (m->fheader_read == 1) { + if (state64 == 0x6A5020200D0A870A) { // JP2 signature box value. + if (pc->frame_start_found) { + pc->frame_start_found = 0; + reset_context(m); + return i - 11; + } else { + pc->frame_start_found = 1; + m->ft = jp2_file; + } + } + } + m->fheader_read--; + } + if (state == 0x0000000C && m->bytes_read >= 3) { // Indicates start of JP2 file. Check signature next. + m->fheader_read = 8; + } else if ((state & 0xFFFF) == 0xFF4F) { + m->in_codestream = 1; + if (!pc->frame_start_found) { + pc->frame_start_found = 1; + m->ft = j2k_cstream; + } else if (pc->frame_start_found && m->ft == jp2_file && m->skipped_codestream) { + reset_context(m); + return i - 1; + } + } else if ((state & 0xFFFF) == 0xFFD9) { + if (pc->frame_start_found && m->ft == jp2_file) { + m->skipped_codestream = 1; + } else if (pc->frame_start_found && m->ft == j2k_cstream) { + m->codestream_frame_end = 1; + } + m->in_codestream = 0; + } else if (m->in_codestream && (state & 0xFFFF) == 0xFF90) { // Are we in tile part header? + m->read_tp = 8; + } else if (pc->frame_start_found && info_marker((state & 0xFFFF0000)>>16) && m->in_codestream) { + m->skip_bytes = (state & 0xFFFF) - 2; + } + } + + pc->state = state; + pc->state64 = state64; + return END_NOT_FOUND; +} + +static int jpeg2000_parse(AVCodecParserContext *s, + AVCodecContext *avctx, + const uint8_t **poutbuf, int *poutbuf_size, + const uint8_t *buf, int buf_size) +{ + JPEG2000ParserContext *m = s->priv_data; + ParseContext *pc = &m->pc; + int next; + + if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){ + next= buf_size; + }else{ + next= find_frame_end(m, 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 ff_jpeg2000_parser = { + .codec_ids = { AV_CODEC_ID_JPEG2000 }, + .priv_data_size = sizeof(JPEG2000ParserContext), + .parser_parse = jpeg2000_parse, + .parser_close = ff_parse_close, +}; \ No newline at end of file diff --git a/libavcodec/parsers.c b/libavcodec/parsers.c index 9fbf182594..7d75cea830 100644 --- a/libavcodec/parsers.c +++ b/libavcodec/parsers.c @@ -48,6 +48,7 @@ extern AVCodecParser ff_h261_parser; extern AVCodecParser ff_h263_parser; extern AVCodecParser ff_h264_parser; extern AVCodecParser ff_hevc_parser; +extern AVCodecParser ff_jpeg2000_parser; extern AVCodecParser ff_mjpeg_parser; extern AVCodecParser ff_mlp_parser; extern AVCodecParser ff_mpeg4video_parser; From patchwork Thu Jun 4 07:54:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gautam Ramakrishnan X-Patchwork-Id: 20144 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 09CAC4489E4 for ; Thu, 4 Jun 2020 10:55:07 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C153468A9F0; Thu, 4 Jun 2020 10:55:06 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id AD7E4688321 for ; Thu, 4 Jun 2020 10:54:59 +0300 (EEST) Received: by mail-pj1-f51.google.com with SMTP id m2so846686pjv.2 for ; Thu, 04 Jun 2020 00:54:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=6yEpW6rd0vr0MjCMRH1HyBBq0pm/kyVZ0Oog1ezS5mU=; b=ZxMJnyfzCoQ2Aw0u3r3506gJUQCwE73YLdHXQCzS7VpJSc9qgIGKdy4t6HxJvePwAO 3Tc6lXAyP5F5RD0LhkH0uEPHtxDHHoJBak8NJcZzbkxBBhdWkbYIINjb3nDAvuD1oNVJ d6f+BaTTQ94NQk0SOY4lxPPZV7G7uIy1dkTIYOMyyR5dD8NR4sTV/shrEWXH28/TYuxz a4EBKkrnt3afTRP+nQ7QBziL2eEI+1QaiaQ4yqnOA/tYGsSYzhOawRBdBcAKTS/n4KbN VPTVhd6rnl/4LZ+TwtL8oEXYvcbGW+zJUCdVm+XfUkcI8kW6snbMNnd8Fx0vYgx1wpWH Epeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=6yEpW6rd0vr0MjCMRH1HyBBq0pm/kyVZ0Oog1ezS5mU=; b=NW+xdzhcURy9ZEbywxk5hc4l3FXa+j1flUypo2V0eRs1NuE8jfjbOsz3HHuovZnQm6 ylvZvYTVFcBUVzNqArz4oYsospAQUc/PW1ba3R8jJ0UuNbW6Dfum5LeI1+1uoJ+FJKh9 WsVAVvs3uB/hUhfjM0zpc4X2nqKFa6haxWSfsAxb7iUD121LBAEkStE9+tXqsBB85IYq 5zq4gsTr0bFa/bUrZoeCVnpUO2s4M6EB+sLTbbLfjNsPzyizmelFnpXzcQ9C5Mr1xuaG xsBlOfUnjwT79VXwJW5hO5KLVWR9BCksw3JLpTiaB2qA9ZrObEO8a0yVAsj5g45Jz9Dk 1PDg== X-Gm-Message-State: AOAM533SwnMtFtZxKN2R62W5u526dgYtUTwUDedXuuAMbhoaiGj12OUT fhc9zHGXN2TqASsGlvUQNVz6Ekee22Y= X-Google-Smtp-Source: ABdhPJwpQMGPd+ro7CNDYXSkm/Mc4I9VHLSLwuQJCNhDs+/pIzkkSHRB/l9ghV7TYZfR8Ox7TvMWSw== X-Received: by 2002:a17:90b:4d10:: with SMTP id mw16mr4454925pjb.143.1591257297497; Thu, 04 Jun 2020 00:54:57 -0700 (PDT) Received: from localhost.localdomain ([122.172.136.215]) by smtp.gmail.com with ESMTPSA id h3sm4444383pjk.10.2020.06.04.00.54.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jun 2020 00:54:56 -0700 (PDT) From: gautamramk@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Thu, 4 Jun 2020 13:24:43 +0530 Message-Id: <20200604075443.22767-2-gautamramk@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200604075443.22767-1-gautamramk@gmail.com> References: <20200604075443.22767-1-gautamramk@gmail.com> Subject: [FFmpeg-devel] [RFC PATCH 2/2] fate/video.mak: fate test for jpeg2000 parser X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Gautam Ramakrishnan MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Gautam Ramakrishnan This test generates a rawvideo video of jpeg2000 frames using lavfi testsrc2 as input. The reference file as 25 frames. --- tests/fate-run.sh | 4 +++- tests/fate/video.mak | 3 +++ tests/ref/fate/jpeg2000-parser | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/ref/fate/jpeg2000-parser diff --git a/tests/fate-run.sh b/tests/fate-run.sh index 002944b010..28c74ecbc5 100755 --- a/tests/fate-run.sh +++ b/tests/fate-run.sh @@ -221,7 +221,9 @@ transcode(){ ffprobe_opts=$7 encfile="${outdir}/${test}.${enc_fmt}" test "$6" = -keep || cleanfiles="$cleanfiles $encfile" - tsrcfile=$(target_path $srcfile) + test $src_fmt = "lavfi" ||\ + tsrcfile=$(target_path $srcfile) &&\ + tsrcfile=$srcfile tencfile=$(target_path $encfile) ffmpeg -f $src_fmt $DEC_OPTS -i $tsrcfile $ENC_OPTS $enc_opt $FLAGS \ -f $enc_fmt -y $tencfile || return diff --git a/tests/fate/video.mak b/tests/fate/video.mak index d2d43e518d..c8d2e66b4a 100644 --- a/tests/fate/video.mak +++ b/tests/fate/video.mak @@ -192,6 +192,9 @@ fate-interplay-mve-16bit: CMD = framecrc -i $(TARGET_SAMPLES)/interplay-mve/desc FATE_VIDEO-$(call DEMDEC, MXF, JPEG2000) += fate-jpeg2000-dcinema fate-jpeg2000-dcinema: CMD = framecrc -flags +bitexact -c:v jpeg2000 -i $(TARGET_SAMPLES)/jpeg2000/chiens_dcinema2K.mxf -pix_fmt xyz12le +FATE_VIDEO-$(call DEMDEC, MXF, JPEG2000) += fate-jpeg2000-parser +fate-jpeg2000-parser: CMD = transcode lavfi "testsrc2=s=640x480" rawvideo "-t 1 -c:v jpeg2000" + FATE_VIDEO-$(call DEMDEC, JV, JV) += fate-jv fate-jv: CMD = framecrc -i $(TARGET_SAMPLES)/jv/intro.jv -an -pix_fmt rgb24 diff --git a/tests/ref/fate/jpeg2000-parser b/tests/ref/fate/jpeg2000-parser new file mode 100644 index 0000000000..f30e27a24e --- /dev/null +++ b/tests/ref/fate/jpeg2000-parser @@ -0,0 +1,32 @@ +d2ea224b9c7319e5240af18260e82c4d *tests/data/fate/jpeg2000-parser.rawvideo +1511061 tests/data/fate/jpeg2000-parser.rawvideo +#tb 0: 1/25 +#media_type 0: video +#codec_id 0: rawvideo +#dimensions 0: 640x480 +#sar 0: 0/1 +0, 0, 0, 1, 460800, 0x82d0c5fe +0, 1, 1, 1, 460800, 0x1bf42314 +0, 2, 2, 1, 460800, 0x2d5598d4 +0, 3, 3, 1, 460800, 0x159e4c0b +0, 4, 4, 1, 460800, 0x0652826a +0, 5, 5, 1, 460800, 0x23f718f4 +0, 6, 6, 1, 460800, 0xc56cb13c +0, 7, 7, 1, 460800, 0x2ba79edd +0, 8, 8, 1, 460800, 0x8b2f8adb +0, 9, 9, 1, 460800, 0x5e9d58e9 +0, 10, 10, 1, 460800, 0xf655492b +0, 11, 11, 1, 460800, 0xa590743b +0, 12, 12, 1, 460800, 0xbaa67b19 +0, 13, 13, 1, 460800, 0x4ca64768 +0, 14, 14, 1, 460800, 0x43978755 +0, 15, 15, 1, 460800, 0x173a7c5c +0, 16, 16, 1, 460800, 0x3135a88e +0, 17, 17, 1, 460800, 0x17cbea25 +0, 18, 18, 1, 460800, 0x21c222ab +0, 19, 19, 1, 460800, 0x7444efcf +0, 20, 20, 1, 460800, 0x860921d2 +0, 21, 21, 1, 460800, 0x9ea5a718 +0, 22, 22, 1, 460800, 0xb8c1a9d7 +0, 23, 23, 1, 460800, 0xe4214b2c +0, 24, 24, 1, 460800, 0x3f00181d