From patchwork Mon Aug 24 03:02:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: hwren X-Patchwork-Id: 21869 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 3ED0844BAEF for ; Mon, 24 Aug 2020 06:03:11 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 285A268AA60; Mon, 24 Aug 2020 06:03:11 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from m15114.mail.126.com (m15114.mail.126.com [220.181.15.114]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CA5EF68A2B6 for ; Mon, 24 Aug 2020 06:02:57 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=qlxih NxGsz06jrlO2JDxhezvAoqvAKpQ6S10ZVzeTQQ=; b=YBAMuLPnyUL3fbcynZ/ci 8UR9LyLTGyt4gRY2X3mz4CZWgh6ytHLqvczZy5OqaBblCxSDctjbTcr8sUMhohrP 8ho7FvWjpwTon/YlXcOZN2kC8XRRZbl/QpzLrreBPZRZfe08ov0C4C2PLVET18xl aE+EhGeyTEL11OCk3o4c1c= Received: from localhost.localdomain (unknown [111.34.131.254]) by smtp7 (Coremail) with SMTP id DsmowAAHQnTbLUNfMjBiIg--.55350S3; Mon, 24 Aug 2020 11:02:52 +0800 (CST) From: hwrenx@126.com To: ffmpeg-devel@ffmpeg.org Date: Mon, 24 Aug 2020 11:02:37 +0800 Message-Id: <20200824030241.1737-2-hwrenx@126.com> X-Mailer: git-send-email 2.23.0.windows.1 In-Reply-To: <20200824030241.1737-1-hwrenx@126.com> References: <20200820064359.800-1-hwrenx@126.com> <20200824030241.1737-1-hwrenx@126.com> MIME-Version: 1.0 X-CM-TRANSID: DsmowAAHQnTbLUNfMjBiIg--.55350S3 X-Coremail-Antispam: 1Uf129KBjvJXoW7Xr1xZw4Uuw15Gr1xCFW7Arb_yoW8Jr4Upr sIqr13KFWaqF9xA397Jr1UCF1jkFy3XF97W3sFqw12kr4Sq34UJryavrZrW3WxZrZ09F9r uwnxGw1kXw4UJaUanT9S1TB71UUUUUJqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jYNtxUUUUU= X-Originating-IP: [111.34.131.254] X-CM-SenderInfo: pkzuv0b06rjloofrz/1tbiJByK6VpEAhD8uQAAsS Subject: [FFmpeg-devel] [PATCH v7 1/5] lavc: add AVS3 codec id and desc 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: hwren , hbj Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: hwren Signed-off-by: hbj Signed-off-by: hwren --- libavcodec/codec_desc.c | 7 +++++++ libavcodec/codec_id.h | 1 + 2 files changed, 8 insertions(+) diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index 0ae6aee63b..61473178fc 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -1412,6 +1412,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .long_name = NULL_IF_CONFIG_SMALL("PGX (JPEG2000 Test Format)"), .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS, }, + { + .id = AV_CODEC_ID_AVS3, + .type = AVMEDIA_TYPE_VIDEO, + .name = "avs3", + .long_name = NULL_IF_CONFIG_SMALL("AVS3-P2/IEEE1857.10"), + .props = AV_CODEC_PROP_LOSSY, + }, { .id = AV_CODEC_ID_Y41P, .type = AVMEDIA_TYPE_VIDEO, diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h index 896ecb0ce0..47aa0787b6 100644 --- a/libavcodec/codec_id.h +++ b/libavcodec/codec_id.h @@ -242,6 +242,7 @@ enum AVCodecID { AV_CODEC_ID_RSCC, AV_CODEC_ID_AVS2, AV_CODEC_ID_PGX, + AV_CODEC_ID_AVS3, AV_CODEC_ID_Y41P = 0x8000, AV_CODEC_ID_AVRP, From patchwork Mon Aug 24 03:02:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: hwren X-Patchwork-Id: 21867 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 0EAD344BAEF for ; Mon, 24 Aug 2020 06:03:09 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EA52468A817; Mon, 24 Aug 2020 06:03:08 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from m15114.mail.126.com (m15114.mail.126.com [220.181.15.114]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 401D968A173 for ; Mon, 24 Aug 2020 06:02:57 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=sP4O0 g+KKGM9Cw2X7SBLuxQPT9H+hcciGXJzO/wRQ/s=; b=jitjl60zpf9hlnz8O/qAG hQiwESmeplEP1Smdc+Fo/bq/ON9iAcT8aCp6caetexB+6hsdEHpeWlKKJpWmqfKf Cz1fEWXAzxDPekeEhnkm5cmVzTid1AFYMarguTxsZfv5WDrSqSwV99xO3NgObgpM +puofEgFQxN4SSj+6EPxIQ= Received: from localhost.localdomain (unknown [111.34.131.254]) by smtp7 (Coremail) with SMTP id DsmowAAHQnTbLUNfMjBiIg--.55350S4; Mon, 24 Aug 2020 11:02:52 +0800 (CST) From: hwrenx@126.com To: ffmpeg-devel@ffmpeg.org Date: Mon, 24 Aug 2020 11:02:38 +0800 Message-Id: <20200824030241.1737-3-hwrenx@126.com> X-Mailer: git-send-email 2.23.0.windows.1 In-Reply-To: <20200824030241.1737-1-hwrenx@126.com> References: <20200820064359.800-1-hwrenx@126.com> <20200824030241.1737-1-hwrenx@126.com> MIME-Version: 1.0 X-CM-TRANSID: DsmowAAHQnTbLUNfMjBiIg--.55350S4 X-Coremail-Antispam: 1Uf129KBjvJXoWxuw1DJF1rKrWfCw13Jw4kXrb_yoW7ZFW7pF 47Cwn8GF1kJw4S9r93GF1jyF13G393XF1jga4Iqa12qw1Yvrs5Jrn7KFy8Ga1fXrn8uayY gr4q9w15Cr1kJw7anT9S1TB71UUUUUJqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UuuWLUUUUU= X-Originating-IP: [111.34.131.254] X-CM-SenderInfo: pkzuv0b06rjloofrz/1tbiTxyK6VpEBW+QQAAAsU Subject: [FFmpeg-devel] [PATCH v7 2/5] lavc/avs3: add AVS3 related definition 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: hwren Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: hwren Signed-off-by: hwren --- libavcodec/Makefile | 2 + libavcodec/avs3.c | 95 +++++++++++++++++++++++++++++++++++++++++++++ libavcodec/avs3.h | 52 +++++++++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 libavcodec/avs3.c create mode 100644 libavcodec/avs3.h diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 3431ba2dca..e1e0c4629d 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -6,6 +6,7 @@ HEADERS = ac3_parser.h \ avcodec.h \ avdct.h \ avfft.h \ + avs3.h \ bsf.h \ codec.h \ codec_desc.h \ @@ -32,6 +33,7 @@ OBJS = ac3_parser.o \ avdct.o \ avpacket.o \ avpicture.o \ + avs3.o \ bitstream.o \ bitstream_filter.o \ bitstream_filters.o \ diff --git a/libavcodec/avs3.c b/libavcodec/avs3.c new file mode 100644 index 0000000000..8587e36def --- /dev/null +++ b/libavcodec/avs3.c @@ -0,0 +1,95 @@ +/* + * AVS3 related definition + * + * Copyright (C) 2020 Huiwen Ren, + * + * 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 "avs3.h" + +const AVRational ff_avs3_frame_rate_tab[16] = { + { 0 , 0 }, // forbid + { 24000, 1001}, + { 24 , 1 }, + { 25 , 1 }, + { 30000, 1001}, + { 30 , 1 }, + { 50 , 1 }, + { 60000, 1001}, + { 60 , 1 }, + { 100 , 1 }, + { 120 , 1 }, + { 200 , 1 }, + { 240 , 1 }, + { 300 , 1 }, + { 0 , 0 }, // reserved + { 0 , 0 } // reserved +}; + +const int ff_avs3_color_primaries_tab[10] = { + AVCOL_PRI_RESERVED0 , // 0 + AVCOL_PRI_BT709 , // 1 + AVCOL_PRI_UNSPECIFIED , // 2 + AVCOL_PRI_RESERVED , // 3 + AVCOL_PRI_BT470M , // 4 + AVCOL_PRI_BT470BG , // 5 + AVCOL_PRI_SMPTE170M , // 6 + AVCOL_PRI_SMPTE240M , // 7 + AVCOL_PRI_FILM , // 8 + AVCOL_PRI_BT2020 // 9 +}; + +const int ff_avs3_color_transfer_tab[15] = { + AVCOL_TRC_RESERVED0 , // 0 + AVCOL_TRC_BT709 , // 1 + AVCOL_TRC_UNSPECIFIED , // 2 + AVCOL_TRC_RESERVED , // 3 + AVCOL_TRC_GAMMA22 , // 4 + AVCOL_TRC_GAMMA28 , // 5 + AVCOL_TRC_SMPTE170M , // 6 + AVCOL_TRC_SMPTE240M , // 7 + AVCOL_TRC_LINEAR , // 8 + AVCOL_TRC_LOG , // 9 + AVCOL_TRC_LOG_SQRT , // 10 + AVCOL_TRC_BT2020_12 , // 11 + AVCOL_TRC_SMPTE2084 , // 12 + AVCOL_TRC_UNSPECIFIED , // 13 + AVCOL_TRC_ARIB_STD_B67 // 14 +}; + +const int ff_avs3_color_matrix_tab[12] = { + AVCOL_SPC_RESERVED , // 0 + AVCOL_SPC_BT709 , // 1 + AVCOL_SPC_UNSPECIFIED , // 2 + AVCOL_SPC_RESERVED , // 3 + AVCOL_SPC_FCC , // 4 + AVCOL_SPC_BT470BG , // 5 + AVCOL_SPC_SMPTE170M , // 6 + AVCOL_SPC_SMPTE240M , // 7 + AVCOL_SPC_BT2020_NCL , // 8 + AVCOL_SPC_BT2020_CL , // 9 + AVCOL_SPC_UNSPECIFIED , // 10 + AVCOL_SPC_UNSPECIFIED // 11 +}; + +const enum AVPictureType ff_avs3_image_type[4] = { + AV_PICTURE_TYPE_NONE, + AV_PICTURE_TYPE_I, + AV_PICTURE_TYPE_P, + AV_PICTURE_TYPE_B +}; \ No newline at end of file diff --git a/libavcodec/avs3.h b/libavcodec/avs3.h new file mode 100644 index 0000000000..9f79adb7bf --- /dev/null +++ b/libavcodec/avs3.h @@ -0,0 +1,52 @@ +/* + * AVS3 related definition + * + * Copyright (C) 2020 Huiwen Ren, + * + * 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 + */ + +#ifndef AVCODEC_AVS3_H +#define AVCODEC_AVS3_H + +#define AVS3_NAL_START_CODE 0x010000 +#define AVS3_SEQ_START_CODE 0xB0 +#define AVS3_SEQ_END_CODE 0xB1 +#define AVS3_USER_DATA_START_CODE 0xB2 +#define AVS3_INTRA_PIC_START_CODE 0xB3 +#define AVS3_UNDEF_START_CODE 0xB4 +#define AVS3_EXTENSION_START_CODE 0xB5 +#define AVS3_INTER_PIC_START_CODE 0xB6 +#define AVS3_VIDEO_EDIT_CODE 0xB7 +#define AVS3_FIRST_SLICE_START_CODE 0x00 +#define AVS3_PROFILE_BASELINE_MAIN 0x20 +#define AVS3_PROFILE_BASELINE_MAIN10 0x22 + +#define ISPIC(x) ((x) == AVS3_INTRA_PIC_START_CODE || (x) == AVS3_INTER_PIC_START_CODE) +#define ISUNIT(x) ((x) == AVS3_SEQ_START_CODE || ISPIC(x)) + +#include "libavutil/avutil.h" +#include "libavutil/pixfmt.h" +#include "libavutil/rational.h" + +extern const AVRational ff_avs3_frame_rate_tab[16]; +extern const int ff_avs3_color_primaries_tab[10]; +extern const int ff_avs3_color_transfer_tab[15]; +extern const int ff_avs3_color_matrix_tab[12]; +extern const enum AVPictureType ff_avs3_image_type[4]; + +#endif /* AVCODEC_AVS3_H */ From patchwork Mon Aug 24 03:02:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: hwren X-Patchwork-Id: 21868 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 1CAA044BAEF for ; Mon, 24 Aug 2020 06:03:10 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 02FAB68A9BB; Mon, 24 Aug 2020 06:03:10 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from m15114.mail.126.com (m15114.mail.126.com [220.181.15.114]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3D02D687F6C for ; Mon, 24 Aug 2020 06:02:57 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=6RIRS bsmUrysQ3dkhE4iZKChvxK638vb7c0LInQdoIg=; b=DCi4+osT2BNO8RXY9WVYP 1wmp4hU0KLsk81yEO3t58UCP8EHJtbFZbeOHAO8sexMCqHOEm4QrJbWmbA7G1GGK Y2YttCCcj86cywDVEM3T4VosJJneWPN4Nz8EWLGWqCnrFA1iA/bMmdaqn06FemSa zGnTRMLEong9f4MoIow/fc= Received: from localhost.localdomain (unknown [111.34.131.254]) by smtp7 (Coremail) with SMTP id DsmowAAHQnTbLUNfMjBiIg--.55350S5; Mon, 24 Aug 2020 11:02:53 +0800 (CST) From: hwrenx@126.com To: ffmpeg-devel@ffmpeg.org Date: Mon, 24 Aug 2020 11:02:39 +0800 Message-Id: <20200824030241.1737-4-hwrenx@126.com> X-Mailer: git-send-email 2.23.0.windows.1 In-Reply-To: <20200824030241.1737-1-hwrenx@126.com> References: <20200820064359.800-1-hwrenx@126.com> <20200824030241.1737-1-hwrenx@126.com> MIME-Version: 1.0 X-CM-TRANSID: DsmowAAHQnTbLUNfMjBiIg--.55350S5 X-Coremail-Antispam: 1Uf129KBjvJXoWxuw1DJrWDWFy5Jrykuw1UAwb_yoWxGF15pF sxCr4rJr4xta1SkrZ7tr13G3W5WF9YqFy8tFWfur1rtws3KryDJ3Z7GFy2k3ySgr9avFy5 urs8XF1jkw4qq3DanT9S1TB71UUUUUDqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UMMKtUUUUU= X-Originating-IP: [111.34.131.254] X-CM-SenderInfo: pkzuv0b06rjloofrz/1tbi2x2K6V16IGYSKgAAs8 Subject: [FFmpeg-devel] [PATCH v7 3/5] lavc/avs3_parser: add avs3 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: hwren , hbj Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: hwren Signed-off-by: hbj Signed-off-by: hwren --- libavcodec/Makefile | 1 + libavcodec/avs3_parser.c | 180 +++++++++++++++++++++++++++++++++++++++ libavcodec/parsers.c | 1 + 3 files changed, 182 insertions(+) create mode 100644 libavcodec/avs3_parser.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index e1e0c4629d..4c596515ee 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1061,6 +1061,7 @@ OBJS-$(CONFIG_AC3_PARSER) += ac3tab.o aac_ac3_parser.o OBJS-$(CONFIG_ADX_PARSER) += adx_parser.o adx.o OBJS-$(CONFIG_AV1_PARSER) += av1_parser.o av1_parse.o OBJS-$(CONFIG_AVS2_PARSER) += avs2_parser.o +OBJS-$(CONFIG_AVS3_PARSER) += avs3_parser.o OBJS-$(CONFIG_BMP_PARSER) += bmp_parser.o OBJS-$(CONFIG_CAVSVIDEO_PARSER) += cavs_parser.o OBJS-$(CONFIG_COOK_PARSER) += cook_parser.o diff --git a/libavcodec/avs3_parser.c b/libavcodec/avs3_parser.c new file mode 100644 index 0000000000..36ca19797d --- /dev/null +++ b/libavcodec/avs3_parser.c @@ -0,0 +1,180 @@ +/* + * AVS3-P2/IEEE1857.10 video parser. + * Copyright (c) 2020 Zhenyu Wang + * Bingjie Han + * Huiwen Ren + * + * 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 "avs3.h" +#include "get_bits.h" +#include "parser.h" + +static int avs3_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size) +{ + int pic_found = pc->frame_start_found; + uint32_t state = pc->state; + int cur = 0; + + if (!pic_found) { + for (; cur < buf_size; ++cur) { + state = (state << 8) | buf[cur]; + if (ISPIC(buf[cur])){ + ++cur; + pic_found = 1; + break; + } + } + } + + if (pic_found) { + if (!buf_size) + return END_NOT_FOUND; + for (; cur < buf_size; ++cur) { + state = (state << 8) | buf[cur]; + if ((state & 0xFFFFFF00) == 0x100 && ISUNIT(state & 0xFF)) { + pc->frame_start_found = 0; + pc->state = -1; + return cur - 3; + } + } + } + + pc->frame_start_found = pic_found; + pc->state = state; + + return END_NOT_FOUND; +} + +static void parse_avs3_nal_units(AVCodecParserContext *s, const uint8_t *buf, + int buf_size, AVCodecContext *avctx) +{ + if (buf_size < 5) { + return; + } + + if (buf[0] == 0x0 && buf[1] == 0x0 && buf[2] == 0x1) { + if (buf[3] == AVS3_SEQ_START_CODE) { + GetBitContext gb; + int profile, ratecode; + + init_get_bits(&gb, buf + 4, buf_size - 4); + + s->key_frame = 1; + s->pict_type = AV_PICTURE_TYPE_I; + + profile = get_bits(&gb, 8); + // Skip bits: level(8) + // progressive(1) + // field(1) + // library(2) + // resv(1) + // width(14) + // resv(1) + // height(14) + // chroma(2) + // sampe_precision(3) + skip_bits(&gb, 47); + + if (profile == AVS3_PROFILE_BASELINE_MAIN10) { + int sample_precision = get_bits(&gb, 3); + if (sample_precision == 1) { + avctx->pix_fmt = AV_PIX_FMT_YUV420P; + } else if (sample_precision == 2) { + avctx->pix_fmt = AV_PIX_FMT_YUV420P10LE; + } else { + avctx->pix_fmt = AV_PIX_FMT_NONE; + } + } + + // Skip bits: resv(1) + // aspect(4) + skip_bits(&gb, 5); + + ratecode = get_bits(&gb, 4); + + // Skip bits: resv(1) + // bitrate_low(18) + // resv(1) + // bitrate_high(12) + // low_delay + skip_bits(&gb, 32); + + avctx->has_b_frames = !get_bits(&gb, 1); + + avctx->framerate.num = avctx->time_base.den = ff_avs3_frame_rate_tab[ratecode].num; + avctx->framerate.den = avctx->time_base.num = ff_avs3_frame_rate_tab[ratecode].den; + + s->width = s->coded_width = avctx->width; + s->height = s->coded_height = avctx->height; + + av_log(avctx, AV_LOG_DEBUG, + "avs3 parse seq hdr: profile %d; coded wxh: %dx%d; " + "frame_rate_code %d\n", profile, avctx->width, avctx->height, ratecode); + + } else if (buf[3] == AVS3_INTRA_PIC_START_CODE) { + s->key_frame = 1; + s->pict_type = AV_PICTURE_TYPE_I; + } else if (buf[3] == AVS3_INTER_PIC_START_CODE){ + s->key_frame = 0; + if (buf_size > 9) { + int pic_code_type = buf[8] & 0x3; + if (pic_code_type == 1 || pic_code_type == 3) { + s->pict_type = AV_PICTURE_TYPE_P; + } else { + s->pict_type = AV_PICTURE_TYPE_B; + } + } + } + } +} + + +static int avs3_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; + + if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { + next = buf_size; + } else { + next = avs3_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; + } + } + + parse_avs3_nal_units(s, buf, buf_size, avctx); + + *poutbuf = buf; + *poutbuf_size = buf_size; + + return next; +} + +AVCodecParser ff_avs3_parser = { + .codec_ids = { AV_CODEC_ID_AVS3 }, + .priv_data_size = sizeof(ParseContext), + .parser_parse = avs3_parse, + .parser_close = ff_parse_close, + .split = ff_mpeg4video_split, +}; diff --git a/libavcodec/parsers.c b/libavcodec/parsers.c index 7d75cea830..aab1d5e3e5 100644 --- a/libavcodec/parsers.c +++ b/libavcodec/parsers.c @@ -28,6 +28,7 @@ extern AVCodecParser ff_ac3_parser; extern AVCodecParser ff_adx_parser; extern AVCodecParser ff_av1_parser; extern AVCodecParser ff_avs2_parser; +extern AVCodecParser ff_avs3_parser; extern AVCodecParser ff_bmp_parser; extern AVCodecParser ff_cavsvideo_parser; extern AVCodecParser ff_cook_parser; From patchwork Mon Aug 24 03:02:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: hwren X-Patchwork-Id: 21866 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 24DE644BAEF for ; Mon, 24 Aug 2020 06:03:04 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 08D9868A2D7; Mon, 24 Aug 2020 06:03:04 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from m15114.mail.126.com (m15114.mail.126.com [220.181.15.114]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4554C68A188 for ; Mon, 24 Aug 2020 06:02:57 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=JZLvs 2h+S7iQmwhhPg/3+w7J9l7Z3YBU04OOsP5Ig1o=; b=WJYU7f0oOP4CDRBw9QyRw 1LbM5zkosc4Db/IEs/adBofp/GcIgT+LObboyRY23WhXqfNYTqjY+l3XiTG56H1w CZhoPKeK/n8TJHQurObB5EVpQdgnPfUyuQWouBlFlWDXi6WItgLZXi9YK+/OVYW1 5UfO1/yPQlDVGBS3sPEHTY= Received: from localhost.localdomain (unknown [111.34.131.254]) by smtp7 (Coremail) with SMTP id DsmowAAHQnTbLUNfMjBiIg--.55350S6; Mon, 24 Aug 2020 11:02:53 +0800 (CST) From: hwrenx@126.com To: ffmpeg-devel@ffmpeg.org Date: Mon, 24 Aug 2020 11:02:40 +0800 Message-Id: <20200824030241.1737-5-hwrenx@126.com> X-Mailer: git-send-email 2.23.0.windows.1 In-Reply-To: <20200824030241.1737-1-hwrenx@126.com> References: <20200820064359.800-1-hwrenx@126.com> <20200824030241.1737-1-hwrenx@126.com> MIME-Version: 1.0 X-CM-TRANSID: DsmowAAHQnTbLUNfMjBiIg--.55350S6 X-Coremail-Antispam: 1Uf129KBjvJXoWxJw48XFyDCF4fJryDAw1xGrg_yoWrZryrp3 Z3uw15Jr1kJa12yr97JFW5Jws5Can5KFyxXa47ur10kFnxWry8Xr9FkFW2yrs7W3srGF48 XF4Yy3WYkw15JaDanT9S1TB71UUUUUDqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jjJPiUUUUU= X-Originating-IP: [111.34.131.254] X-CM-SenderInfo: pkzuv0b06rjloofrz/1tbiDx2K6VpEBU+9DAAAsU Subject: [FFmpeg-devel] [PATCH v7 4/5] lavf/avs3dec: add raw avs3 demuxer 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: hwren , hbj Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: hwren Signed-off-by: hbj Signed-off-by: hwren --- libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/avs3dec.c | 68 ++++++++++++++++++++++++++++++++++++++++ libavformat/version.h | 2 +- 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 libavformat/avs3dec.c diff --git a/libavformat/Makefile b/libavformat/Makefile index cbb33fe37c..8ddab3aee5 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -122,6 +122,7 @@ OBJS-$(CONFIG_AVR_DEMUXER) += avr.o pcm.o OBJS-$(CONFIG_AVS_DEMUXER) += avs.o voc_packet.o vocdec.o voc.o OBJS-$(CONFIG_AVS2_DEMUXER) += davs2.o rawdec.o OBJS-$(CONFIG_AVS2_MUXER) += rawenc.o +OBJS-$(CONFIG_AVS3_DEMUXER) += avs3dec.o rawdec.o OBJS-$(CONFIG_BETHSOFTVID_DEMUXER) += bethsoftvid.o OBJS-$(CONFIG_BFI_DEMUXER) += bfi.o OBJS-$(CONFIG_BINK_DEMUXER) += bink.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 0aa9dd7198..7f41c12d7c 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -84,6 +84,7 @@ extern AVInputFormat ff_avr_demuxer; extern AVInputFormat ff_avs_demuxer; extern AVInputFormat ff_avs2_demuxer; extern AVOutputFormat ff_avs2_muxer; +extern AVInputFormat ff_avs3_demuxer; extern AVInputFormat ff_bethsoftvid_demuxer; extern AVInputFormat ff_bfi_demuxer; extern AVInputFormat ff_bintext_demuxer; diff --git a/libavformat/avs3dec.c b/libavformat/avs3dec.c new file mode 100644 index 0000000000..af14550b55 --- /dev/null +++ b/libavformat/avs3dec.c @@ -0,0 +1,68 @@ +/* + * RAW AVS3-P2/IEEE1857.10 video demuxer + * Copyright (c) 2020 Zhenyu Wang + * Bingjie Han + * Huiwen Ren + * + * 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 "libavcodec/avs3.h" +#include "libavcodec/internal.h" +#include "avformat.h" +#include "rawdec.h" + +static int avs3video_probe(const AVProbeData *p) +{ + const uint8_t *ptr = p->buf, *end = p->buf + p->buf_size; + uint32_t code = -1; + uint8_t state = 0; + int pic = 0, seq = 0, slice_pos = 0; + int ret = 0; + + while (ptr < end) { + ptr = avpriv_find_start_code(ptr, end, &code); + state = code & 0xFF; + if ((code & 0xFFFFFF00) == 0x100) { + if (state < AVS3_SEQ_START_CODE) { + if (code < slice_pos) + return 0; + slice_pos = code; + } else { + slice_pos = 0; + } + if (state == AVS3_SEQ_START_CODE) { + seq++; + if (*ptr != AVS3_PROFILE_BASELINE_MAIN && *ptr != AVS3_PROFILE_BASELINE_MAIN10) + return 0; + } else if (ISPIC(state)) { + pic++; + } else if ((state == AVS3_UNDEF_START_CODE) || + (state > AVS3_VIDEO_EDIT_CODE)) { + return 0; + } + } + } + + if (seq && pic && av_match_ext(p->filename, "avs3")) { + ret = AVPROBE_SCORE_MAX; + } + + return ret; +} + +FF_DEF_RAWVIDEO_DEMUXER(avs3, "raw AVS3-P2/IEEE1857.10", avs3video_probe, "avs3", AV_CODEC_ID_AVS3) \ No newline at end of file diff --git a/libavformat/version.h b/libavformat/version.h index aa309ecc77..146db09d1b 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -32,7 +32,7 @@ // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium) // Also please add any ticket numbers that you believe might be affected here #define LIBAVFORMAT_VERSION_MAJOR 58 -#define LIBAVFORMAT_VERSION_MINOR 51 +#define LIBAVFORMAT_VERSION_MINOR 52 #define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ From patchwork Mon Aug 24 03:02:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: hwren X-Patchwork-Id: 21871 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 07AF344BAEF for ; Mon, 24 Aug 2020 06:03:14 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E56C068A7D8; Mon, 24 Aug 2020 06:03:13 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from m15114.mail.126.com (m15114.mail.126.com [220.181.15.114]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C6A1668A285 for ; Mon, 24 Aug 2020 06:02:57 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=wjzdK Rh8+9g6UCFsPqIbyi3dEEw81L+0cFq3MlwXo5M=; b=dYIBMmZ48cF1xwvxi4aJ2 pNg4uZhemNqgJO1Gpuk/wu1+i6wd4Bvpu5u1RS195Cq9oyKHE2CXtJqtve2300Gg eQkYFcN0zYFPck5JBRIuqFT7rSjdlcGtWzkQQy1inpyUBthuat8DW7riwtYj4O/J WIQhnPtNxypOR3w31VY8d8= Received: from localhost.localdomain (unknown [111.34.131.254]) by smtp7 (Coremail) with SMTP id DsmowAAHQnTbLUNfMjBiIg--.55350S7; Mon, 24 Aug 2020 11:02:53 +0800 (CST) From: hwrenx@126.com To: ffmpeg-devel@ffmpeg.org Date: Mon, 24 Aug 2020 11:02:41 +0800 Message-Id: <20200824030241.1737-6-hwrenx@126.com> X-Mailer: git-send-email 2.23.0.windows.1 In-Reply-To: <20200824030241.1737-1-hwrenx@126.com> References: <20200820064359.800-1-hwrenx@126.com> <20200824030241.1737-1-hwrenx@126.com> MIME-Version: 1.0 X-CM-TRANSID: DsmowAAHQnTbLUNfMjBiIg--.55350S7 X-Coremail-Antispam: 1Uf129KBjvAXoW3tF4ftFWDurykJr15ZFWfuFg_yoW8Gr4kGo WUCryYyw1UAry2kF4v9r17W3y7Xr4a9rW3J393KayDGay5u343KryYvw15XwsrCrWFya4f AayIgr4UJFWUKw4rn29KB7ZKAUJUUUU5529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjxUD8nYDUUUU X-Originating-IP: [111.34.131.254] X-CM-SenderInfo: pkzuv0b06rjloofrz/1tbiZx6K6V16erJvdwAAst Subject: [FFmpeg-devel] [PATCH v7 5/5] lavc, doc: add libuavs3d video decoder wrapper 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: hwren , hbj Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: hwren Signed-off-by: hbj Signed-off-by: hwren --- Changelog | 1 + configure | 4 + doc/decoders.texi | 21 ++++ doc/general.texi | 8 ++ libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/libuavs3d.c | 248 +++++++++++++++++++++++++++++++++++++++++ libavcodec/version.h | 2 +- 8 files changed, 285 insertions(+), 1 deletion(-) create mode 100644 libavcodec/libuavs3d.c diff --git a/Changelog b/Changelog index 1efc768387..91794629e5 100644 --- a/Changelog +++ b/Changelog @@ -14,6 +14,7 @@ version : - ADPCM Argonaut Games encoder - Argonaut Games ASF muxer - AV1 Low overhead bitstream format demuxer +- AVS3 video decoder via libuavs3d version 4.3: diff --git a/configure b/configure index 6faff9bc7b..0fde821742 100755 --- a/configure +++ b/configure @@ -277,6 +277,7 @@ External library support: --enable-libtls enable LibreSSL (via libtls), needed for https support if openssl, gnutls or mbedtls is not used [no] --enable-libtwolame enable MP2 encoding via libtwolame [no] + --enable-libuavs3d enable AVS3 decoding via libuavs3d [no] --enable-libv4l2 enable libv4l2/v4l-utils [no] --enable-libvidstab enable video stabilization using vid.stab [no] --enable-libvmaf enable vmaf filter via libvmaf [no] @@ -1811,6 +1812,7 @@ EXTERNAL_LIBRARY_LIST=" libtesseract libtheora libtwolame + libuavs3d libv4l2 libvmaf libvorbis @@ -3253,6 +3255,7 @@ libspeex_encoder_select="audio_frame_queue" libsvtav1_encoder_deps="libsvtav1" libtheora_encoder_deps="libtheora" libtwolame_encoder_deps="libtwolame" +libuavs3d_decoder_deps="libuavs3d" libvo_amrwbenc_encoder_deps="libvo_amrwbenc" libvorbis_decoder_deps="libvorbis" libvorbis_encoder_deps="libvorbis libvorbisenc" @@ -6416,6 +6419,7 @@ enabled libtls && require_pkg_config libtls libtls tls.h tls_configur enabled libtwolame && require libtwolame twolame.h twolame_init -ltwolame && { check_lib libtwolame twolame.h twolame_encode_buffer_float32_interleaved -ltwolame || die "ERROR: libtwolame must be installed and version must be >= 0.3.10"; } +enabled libuavs3d && require_pkg_config libuavs3d "uavs3d >= 1.1.41" uavs3d.h uavs3d_decode enabled libv4l2 && require_pkg_config libv4l2 libv4l2 libv4l2.h v4l2_ioctl enabled libvidstab && require_pkg_config libvidstab "vidstab >= 0.98" vid.stab/libvidstab.h vsMotionDetectInit enabled libvmaf && require_pkg_config libvmaf "libvmaf >= 1.5.2" libvmaf.h compute_vmaf diff --git a/doc/decoders.texi b/doc/decoders.texi index 9005714e3c..42b46fe153 100644 --- a/doc/decoders.texi +++ b/doc/decoders.texi @@ -88,6 +88,27 @@ This decoder allows libavcodec to decode AVS2 streams with davs2 library. @c man end VIDEO DECODERS +@section libuavs3d + +AVS3-P2/IEEE1857.10 video decoder. + +libuavs3d allows libavcodec to decode AVS3 streams. +Requires the presence of the libuavs3d headers and library during configuration. +You need to explicitly configure the build with @code{--enable-libuavs3d}. + +@subsection Options + +The following option is supported by the libuavs3d wrapper. + +@table @option + +@item frame_threads +Set amount of frame threads to use during decoding. The default value is 0 (autodetect). + +@end table + +@c man end VIDEO DECODERS + @chapter Audio Decoders @c man begin AUDIO DECODERS diff --git a/doc/general.texi b/doc/general.texi index fac5377504..233f69d349 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -125,6 +125,14 @@ Go to @url{https://github.com/pkuvcl/davs2} and follow the instructions for installing the library. Then pass @code{--enable-libdavs2} to configure to enable it. +@section uavs3d + +FFmpeg can make use of the uavs3d library for AVS3-P2/IEEE1857.10 video decoding. + +Go to @url{https://github.com/uavs3/uavs3d} and follow the instructions for +installing the library. Then pass @code{--enable-libuavs3d} to configure to +enable it. + @float NOTE libdavs2 is under the GNU Public License Version 2 or later (see @url{http://www.gnu.org/licenses/old-licenses/gpl-2.0.html} for diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 4c596515ee..5cccc654b3 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1034,6 +1034,7 @@ OBJS-$(CONFIG_LIBSPEEX_ENCODER) += libspeexenc.o OBJS-$(CONFIG_LIBSVTAV1_ENCODER) += libsvtav1.o OBJS-$(CONFIG_LIBTHEORA_ENCODER) += libtheoraenc.o OBJS-$(CONFIG_LIBTWOLAME_ENCODER) += libtwolame.o +OBJS-$(CONFIG_LIBUAVS3D_DECODER) += libuavs3d.o OBJS-$(CONFIG_LIBVO_AMRWBENC_ENCODER) += libvo-amrwbenc.o OBJS-$(CONFIG_LIBVORBIS_DECODER) += libvorbisdec.o OBJS-$(CONFIG_LIBVORBIS_ENCODER) += libvorbisenc.o \ diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 4bd830e5d0..04b990dd37 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -732,6 +732,7 @@ extern AVCodec ff_libspeex_decoder; extern AVCodec ff_libsvtav1_encoder; extern AVCodec ff_libtheora_encoder; extern AVCodec ff_libtwolame_encoder; +extern AVCodec ff_libuavs3d_decoder; extern AVCodec ff_libvo_amrwbenc_encoder; extern AVCodec ff_libvorbis_encoder; extern AVCodec ff_libvorbis_decoder; diff --git a/libavcodec/libuavs3d.c b/libavcodec/libuavs3d.c new file mode 100644 index 0000000000..d9829f1077 --- /dev/null +++ b/libavcodec/libuavs3d.c @@ -0,0 +1,248 @@ +/* + * RAW AVS3-P2/IEEE1857.10 video demuxer + * Copyright (c) 2020 Zhenyu Wang + * Bingjie Han + * Huiwen Ren + * + * 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/avassert.h" +#include "libavutil/avutil.h" +#include "libavutil/common.h" +#include "libavutil/imgutils.h" +#include "libavutil/intreadwrite.h" +#include "libavutil/opt.h" +#include "avcodec.h" +#include "avs3.h" +#include "internal.h" +#include "uavs3d.h" + +typedef struct uavs3d_context { + AVCodecContext *avctx; + void *dec_handle; + int frame_threads; + int got_seqhdr; + uavs3d_io_frm_t dec_frame; +} uavs3d_context; + +#define UAVS3D_CHECK_START_CODE(data_ptr, PIC_START_CODE) \ + (AV_RL32(data_ptr) != (PIC_START_CODE << 24) + AVS3_NAL_START_CODE) +static int uavs3d_find_next_start_code(const unsigned char *bs_data, int bs_len, int *left) +{ + const unsigned char *data_ptr = bs_data + 4; + int count = bs_len - 4; + + while (count >= 4 && + UAVS3D_CHECK_START_CODE(data_ptr, AVS3_INTER_PIC_START_CODE) && + UAVS3D_CHECK_START_CODE(data_ptr, AVS3_INTRA_PIC_START_CODE) && + UAVS3D_CHECK_START_CODE(data_ptr, AVS3_SEQ_START_CODE) && + UAVS3D_CHECK_START_CODE(data_ptr, AVS3_FIRST_SLICE_START_CODE) && + UAVS3D_CHECK_START_CODE(data_ptr, AVS3_SEQ_END_CODE)) { + data_ptr++; + count--; + } + + if (count >= 4) { + *left = count; + return 1; + } + + return 0; +} + +static void uavs3d_output_callback(uavs3d_io_frm_t *dec_frame) { + uavs3d_io_frm_t frm_out; + AVFrame *frm = (AVFrame *)dec_frame->priv; + int i; + + if (!frm || !frm->data[0]) { + dec_frame->got_pic = 0; + av_log(NULL, AV_LOG_ERROR, "Invalid AVFrame in uavs3d output.\n"); + return; + } + + frm->pts = dec_frame->pts; + frm->pkt_dts = dec_frame->dts; + frm->pkt_pos = dec_frame->pkt_pos; + frm->pkt_size = dec_frame->pkt_size; + frm->coded_picture_number = dec_frame->dtr; + frm->display_picture_number = dec_frame->ptr; + + if (dec_frame->type < 0 || dec_frame->type >= 4) { + av_log(NULL, AV_LOG_WARNING, "Error frame type in uavs3d: %d.\n", dec_frame->type); + } + + frm->pict_type = ff_avs3_image_type[dec_frame->type]; + frm->key_frame = (frm->pict_type == AV_PICTURE_TYPE_I); + + for (i = 0; i < 3; i++) { + frm_out.width [i] = dec_frame->width[i]; + frm_out.height[i] = dec_frame->height[i]; + frm_out.stride[i] = frm->linesize[i]; + frm_out.buffer[i] = frm->data[i]; + } + + uavs3d_img_cpy_cvt(&frm_out, dec_frame, dec_frame->bit_depth); +} + +static av_cold int libuavs3d_init(AVCodecContext *avctx) +{ + uavs3d_context *h = avctx->priv_data; + uavs3d_cfg_t cdsc; + + cdsc.frm_threads = avctx->thread_count > 0 ? avctx->thread_count : av_cpu_count(); + cdsc.check_md5 = 0; + h->dec_handle = uavs3d_create(&cdsc, uavs3d_output_callback, NULL); + h->got_seqhdr = 0; + + if (!h->dec_handle) { + return AVERROR(ENOMEM); + } + + return 0; +} + +static av_cold int libuavs3d_end(AVCodecContext *avctx) +{ + uavs3d_context *h = avctx->priv_data; + + if (h->dec_handle) { + uavs3d_flush(h->dec_handle, NULL); + uavs3d_delete(h->dec_handle); + h->dec_handle = NULL; + } + h->got_seqhdr = 0; + + return 0; +} + +static void libuavs3d_flush(AVCodecContext * avctx) +{ + uavs3d_context *h = avctx->priv_data; + + if (h->dec_handle) { + uavs3d_reset(h->dec_handle); + } +} + +static int libuavs3d_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt) +{ + uavs3d_context *h = avctx->priv_data; + const uint8_t *buf = avpkt->data; + int buf_size = avpkt->size; + const uint8_t *buf_end; + const uint8_t *buf_ptr; + AVFrame *frm = data; + int left_bytes; + int ret, finish = 0; + + *got_frame = 0; + frm->pts = -1; + frm->pict_type = AV_PICTURE_TYPE_NONE; + + if (!buf_size) { + if (h->got_seqhdr) { + if (!frm->data[0] && (ret = ff_get_buffer(avctx, frm, 0)) < 0) { + return ret; + } + h->dec_frame.priv = data; // AVFrame + } + do { + ret = uavs3d_flush(h->dec_handle, &h->dec_frame); + } while (ret > 0 && !h->dec_frame.got_pic); + } else { + uavs3d_io_frm_t *frm_dec = &h->dec_frame; + + buf_ptr = buf; + buf_end = buf + buf_size; + frm_dec->pkt_pos = avpkt->pos; + frm_dec->pkt_size = avpkt->size; + + while (!finish) { + int bs_len; + + if (h->got_seqhdr) { + if (!frm->data[0] && (ret = ff_get_buffer(avctx, frm, 0)) < 0) { + return ret; + } + h->dec_frame.priv = data; // AVFrame + } + + if (uavs3d_find_next_start_code(buf_ptr, buf_end - buf_ptr, &left_bytes)) { + bs_len = buf_end - buf_ptr - left_bytes; + } else { + bs_len = buf_end - buf_ptr; + finish = 1; + } + frm_dec->bs = (unsigned char *)buf_ptr; + frm_dec->bs_len = bs_len; + frm_dec->pts = avpkt->pts; + frm_dec->dts = avpkt->dts; + uavs3d_decode(h->dec_handle, frm_dec); + buf_ptr += bs_len; + + if (frm_dec->nal_type == NAL_SEQ_HEADER) { + struct uavs3d_com_seqh_t *seqh = frm_dec->seqhdr; + if (seqh->frame_rate_code >= 16) { + av_log(avctx, AV_LOG_WARNING, "Invalid frame rate code: %d.\n", seqh->frame_rate_code); + seqh->frame_rate_code = 3; // default 25 fps + } + avctx->framerate.num = ff_avs3_frame_rate_tab[seqh->frame_rate_code].num; + avctx->framerate.den = ff_avs3_frame_rate_tab[seqh->frame_rate_code].den; + avctx->has_b_frames = !seqh->low_delay; + avctx->pix_fmt = seqh->bit_depth_internal == 8 ? AV_PIX_FMT_YUV420P : AV_PIX_FMT_YUV420P10LE; + ff_set_dimensions(avctx, seqh->horizontal_size, seqh->vertical_size); + h->got_seqhdr = 1; + + if (seqh->colour_description) { + avctx->color_primaries = ff_avs3_color_primaries_tab[seqh->colour_primaries]; + avctx->color_trc = ff_avs3_color_transfer_tab [seqh->transfer_characteristics]; + avctx->colorspace = ff_avs3_color_matrix_tab [seqh->matrix_coefficients]; + } + } + if (frm_dec->got_pic) { + break; + } + } + } + + *got_frame = h->dec_frame.got_pic; + + if (!(*got_frame)) { + av_frame_unref(frm); + } + + return buf_ptr - buf; +} + +AVCodec ff_libuavs3d_decoder = { + .name = "libuavs3d", + .long_name = NULL_IF_CONFIG_SMALL("libuavs3d AVS3-P2/IEEE1857.10"), + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_AVS3, + .priv_data_size = sizeof(uavs3d_context), + .init = libuavs3d_init, + .close = libuavs3d_end, + .decode = libuavs3d_decode_frame, + .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS, + .flush = libuavs3d_flush, + .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P, + AV_PIX_FMT_YUV420P10LE, + AV_PIX_FMT_NONE }, + .wrapper_name = "libuavs3d", +}; diff --git a/libavcodec/version.h b/libavcodec/version.h index a3f9f828ee..5bdfdce363 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -28,7 +28,7 @@ #include "libavutil/version.h" #define LIBAVCODEC_VERSION_MAJOR 58 -#define LIBAVCODEC_VERSION_MINOR 100 +#define LIBAVCODEC_VERSION_MINOR 101 #define LIBAVCODEC_VERSION_MICRO 100 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \