From patchwork Sat Jun 20 01:45:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: hwren X-Patchwork-Id: 20512 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 6EA0944B0E0 for ; Sat, 20 Jun 2020 04:46:19 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 56F7F68B779; Sat, 20 Jun 2020 04:46:19 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from m15112.mail.126.com (m15112.mail.126.com [220.181.15.112]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8A3C168B671 for ; Sat, 20 Jun 2020 04:46:10 +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=BipES aO7Q1ldRxVC6WrGjnLJVzT//WT+7p83JBb4DPM=; b=hMTwHUwYPOtMsueg1L50n FdxyIj4bpI6MyxB8rCB+/NIx5XFPGb6cqQe8yc9KMuBT0Ye0ZQUx1jcPreRsALBx PAhdgRYXZdNOhfzc1Popn69Vfn6/3uRp1mfyJhsUwtVBX4bK4H4EbUU6+9hdzSWQ xlEm4rmpUAMxkApVU4YjPc= Received: from localhost.localdomain (unknown [111.34.131.148]) by smtp2 (Coremail) with SMTP id DMmowACXzgBbau1ebM9FEg--.41208S3; Sat, 20 Jun 2020 09:46:05 +0800 (CST) From: hwrenx@126.com To: ffmpeg-devel@ffmpeg.org Date: Sat, 20 Jun 2020 09:45:49 +0800 Message-Id: <20200620014552.806-2-hwrenx@126.com> X-Mailer: git-send-email 2.23.0.windows.1 In-Reply-To: <20200620014552.806-1-hwrenx@126.com> References: <20200620014552.806-1-hwrenx@126.com> MIME-Version: 1.0 X-CM-TRANSID: DMmowACXzgBbau1ebM9FEg--.41208S3 X-Coremail-Antispam: 1Uf129KBjvdXoW7Xr1xZw4Uuw15Gr1xCFW7Arb_yoWkXrg_ta s8JFykuF98Za42qa1UKrWIyrn7ZFyrZa1Fq3Z0yFs7WFZIqr4UXrnxua1FyFW3W3s3Za4S ya48tw4Fkr1j9jkaLaAFLSUrUUUUbb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUvcSsGvfC2KfnxnUUI43ZEXa7IUjRnQUUUUUU== X-Originating-IP: [111.34.131.148] X-CM-SenderInfo: pkzuv0b06rjloofrz/1tbiDx1J6VpEA1w6ogAAsr Subject: [FFmpeg-devel] [PATCH v1 1/4] 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: hwrenx@126.com, 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 9f88475..ad55d99 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -1406,6 +1406,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .props = AV_CODEC_PROP_LOSSY, }, { + .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, .name = "y41p", diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h index d885962..7f7dee5 100644 --- a/libavcodec/codec_id.h +++ b/libavcodec/codec_id.h @@ -241,6 +241,7 @@ enum AVCodecID { AV_CODEC_ID_SCREENPRESSO, AV_CODEC_ID_RSCC, AV_CODEC_ID_AVS2, + AV_CODEC_ID_AVS3, AV_CODEC_ID_Y41P = 0x8000, AV_CODEC_ID_AVRP, From patchwork Sat Jun 20 01:45:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: hwren X-Patchwork-Id: 20511 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 81F4C44B0E0 for ; Sat, 20 Jun 2020 04:46:18 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 67F0468B77D; Sat, 20 Jun 2020 04:46:18 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from m15112.mail.126.com (m15112.mail.126.com [220.181.15.112]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E1EFA68B672 for ; Sat, 20 Jun 2020 04:46:09 +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=ILtig m4WUt67IppSQiMrBE3Ei1/JJ1fmdSycDfKgvjA=; b=k9OSFB6dyPM1dPfzFg3q/ Y7XgpqHspRLZ+clPzTTwBNbpf9Xxb7aFgJcqvdHrJZ7+tlPth61b/ZA2QwfW0vFY S5z+2fgeP3LZ9VI970eWGgF6+OzzJ0lzMNWSUoXxR1gmKV/umXMglHEEa6Sj/khb AmPk2mOP2dSOkDtXiuB+GQ= Received: from localhost.localdomain (unknown [111.34.131.148]) by smtp2 (Coremail) with SMTP id DMmowACXzgBbau1ebM9FEg--.41208S4; Sat, 20 Jun 2020 09:46:05 +0800 (CST) From: hwrenx@126.com To: ffmpeg-devel@ffmpeg.org Date: Sat, 20 Jun 2020 09:45:50 +0800 Message-Id: <20200620014552.806-3-hwrenx@126.com> X-Mailer: git-send-email 2.23.0.windows.1 In-Reply-To: <20200620014552.806-1-hwrenx@126.com> References: <20200620014552.806-1-hwrenx@126.com> MIME-Version: 1.0 X-CM-TRANSID: DMmowACXzgBbau1ebM9FEg--.41208S4 X-Coremail-Antispam: 1Uf129KBjvJXoWxuw1DJF1rCr13Cr43uFWkCrg_yoWxXFWrpF sxCF1rJr4xtana9rZ7tr13K3W5Wr9YqFyktFWxur1rt39agryDX3ZrGFyYk34fWr93ZFy5 urs8Xa4jkw1Dt3DanT9S1TB71UUUUUDqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UxnY7UUUUU= X-Originating-IP: [111.34.131.148] X-CM-SenderInfo: pkzuv0b06rjloofrz/1tbi2x1J6V16HnJfnwAAst Subject: [FFmpeg-devel] [PATCH v1 2/4] lavc/avs3_paeser: 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: hwrenx@126.com, 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 | 184 +++++++++++++++++++++++++++++++++++++++++++++++ libavcodec/parsers.c | 1 + 3 files changed, 186 insertions(+) create mode 100644 libavcodec/avs3_parser.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 5a6ea59..f151277 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1053,6 +1053,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 0000000..7a7f826 --- /dev/null +++ b/libavcodec/avs3_parser.c @@ -0,0 +1,184 @@ +/* + * 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 "parser.h" + +#define SLICE_MAX_START_CODE 0x000001af + +#define ISPIC(x) ((x) == 0xB3 || (x) == 0xB6) +#define ISUNIT(x) ((x) == 0xB0 || ISPIC(x)) + +static av_cold 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 unsigned int read_bits(const char** ppbuf, int *pidx, int bits) +{ + const char* p = *ppbuf; + int idx = *pidx; + unsigned int val = 0; + + while (bits) { + bits--; + val = (val << 1) | (((*p) >> idx) & 0x1); + if (--idx < 0) { + idx = 7; + p++; + } + } + + *ppbuf = p; + *pidx = idx; + + return val; +} + +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] == 0xB0) { + static const int avs3_fps_num[9] = {0, 240000, 24, 25, 30000, 30, 50, 60000, 60 }; + static const int avs3_fps_den[9] = {1, 1001, 1, 1, 1001, 1, 1, 1001, 1 }; + int profile,ratecode; + const char* p = buf + 4; + int idx = 7; + + s->key_frame = 1; + s->pict_type = AV_PICTURE_TYPE_I; + + profile = read_bits(&p, &idx, 8); + // level(8) + progressive(1) + field(1) + library(2) + resv(1) + width(14) + resv(1) + height(14) + chroma(2) + sampe_precision(3) + read_bits(&p, &idx, 47); + + if (profile == 0x22) { + avctx->pix_fmt = read_bits(&p, &idx, 3) == 1 ? AV_PIX_FMT_YUV420P : AV_PIX_FMT_YUV420P10LE; + } + + // resv(1) + aspect(4) + read_bits(&p, &idx, 5); + + ratecode = read_bits(&p, &idx, 4); + + // resv(1) + bitrate_low(18) + resv(1) + bitrate_high(12) + read_bits(&p, &idx, 32); + + avctx->has_b_frames = !read_bits(&p, &idx, 1); + + avctx->framerate.num = avctx->time_base.den = avs3_fps_num[ratecode]; + avctx->framerate.den = avctx->time_base.num = avs3_fps_den[ratecode]; + + 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] == 0xB3) { + s->key_frame = 1; + s->pict_type = AV_PICTURE_TYPE_I; + } else if (buf[3] == 0xB6){ + 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 7d75cea..aab1d5e 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 Sat Jun 20 01:45:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: hwren X-Patchwork-Id: 20510 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 95E1D44B0E0 for ; Sat, 20 Jun 2020 04:46:16 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 73A4068B6C7; Sat, 20 Jun 2020 04:46:16 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from m15112.mail.126.com (m15112.mail.126.com [220.181.15.112]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6E59168B671 for ; Sat, 20 Jun 2020 04:46:09 +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=h/8Bq 0lze6JmeDnwR0rZszWXgprxmZppANqAv40uRFU=; b=pNTsk+dK2Lxf1sqSvnM4l epfsP6YQmdsSU5xMM3N3Gqhsl1MHVYefD/qNI0GMPH9N4KxvPYE6tRJ5lpol//jZ +Vh5r0vEyQ8gk9Sdcwpp2ZCHB04dlAS7aTrO1Zj55v6PQHLfVGtGTeuAHfjWSPpu I4xBjlWBHfwCqWbqGaTOXQ= Received: from localhost.localdomain (unknown [111.34.131.148]) by smtp2 (Coremail) with SMTP id DMmowACXzgBbau1ebM9FEg--.41208S5; Sat, 20 Jun 2020 09:46:05 +0800 (CST) From: hwrenx@126.com To: ffmpeg-devel@ffmpeg.org Date: Sat, 20 Jun 2020 09:45:51 +0800 Message-Id: <20200620014552.806-4-hwrenx@126.com> X-Mailer: git-send-email 2.23.0.windows.1 In-Reply-To: <20200620014552.806-1-hwrenx@126.com> References: <20200620014552.806-1-hwrenx@126.com> MIME-Version: 1.0 X-CM-TRANSID: DMmowACXzgBbau1ebM9FEg--.41208S5 X-Coremail-Antispam: 1Uf129KBjvJXoWxJw1ktF4rtw45Jw13JFW7twb_yoWrWw48pa nxu3W5Jr1kJ3y29rZ3Jas5JwsYkanYqF17XF97ur1xuFnxXrykXr1kKrWjyryxW34DCrWx XF45ta4Skwn8J3DanT9S1TB71UUUUUJqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jbrcfUUUUU= X-Originating-IP: [111.34.131.148] X-CM-SenderInfo: pkzuv0b06rjloofrz/1tbi2x5J6V16HnJfpQAAsU Subject: [FFmpeg-devel] [PATCH v1 3/4] 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: hwrenx@126.com, 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 | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 libavformat/avs3dec.c diff --git a/libavformat/Makefile b/libavformat/Makefile index 26af859..b433a60 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -120,6 +120,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 97fd06d..2d768b8 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -82,6 +82,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 0000000..9758071 --- /dev/null +++ b/libavformat/avs3dec.c @@ -0,0 +1,79 @@ +/* + * 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 "avformat.h" +#include "rawdec.h" +#include "libavcodec/internal.h" + +#define AVS3_SEQ_START_CODE 0x000001b0 +#define AVS3_PIC_I_START_CODE 0x000001b3 +#define AVS3_UNDEF_START_CODE 0x000001b4 +#define AVS3_PIC_PB_START_CODE 0x000001b6 +#define AVS3_VIDEO_EDIT_CODE 0x000001b7 +#define AVS3_PROFILE_JIZHUN 0x20 +#define AVS3_PROFILE_JIZHUN10 0x22 + +static int avs3video_probe(const AVProbeData *p) +{ + uint32_t code= -1; + int pic=0, seq=0, slice_pos = 0; + const uint8_t *ptr = p->buf, *end = p->buf + p->buf_size; + int ret = 0; + + while (ptr < end) { + ptr = avpriv_find_start_code(ptr, end, &code); + if ((code & 0xffffff00) == 0x100) { + if (code < AVS3_SEQ_START_CODE) { + /* slices have to be consecutive */ + if (code < slice_pos) + return 0; + slice_pos = code; + } else { + slice_pos = 0; + } + if (code == AVS3_SEQ_START_CODE) { + seq++; + /* check for the only currently supported profile */ + if (*ptr != AVS3_PROFILE_JIZHUN && *ptr != AVS3_PROFILE_JIZHUN10) + return 0; + } else if ((code == AVS3_PIC_I_START_CODE) || + (code == AVS3_PIC_PB_START_CODE)) { + pic++; + } else if ((code == AVS3_UNDEF_START_CODE) || + (code > AVS3_VIDEO_EDIT_CODE)) { + return 0; + } + } + } + if (seq && pic) { + if (av_match_ext(p->filename, "avs3")) { + ret = AVPROBE_SCORE_MAX; + } else { + ret = AVPROBE_SCORE_EXTENSION + 1; + } + } + return ret; +} + +FF_DEF_RAWVIDEO_DEMUXER(avs3, "raw AVS3-P2/IEEE1857.10", avs3video_probe, "avs3", AV_CODEC_ID_AVS3) + From patchwork Sat Jun 20 01:45:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: hwren X-Patchwork-Id: 20513 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 2E42944B0E0 for ; Sat, 20 Jun 2020 04:46:20 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 18D1068B7A5; Sat, 20 Jun 2020 04:46:20 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from m15112.mail.126.com (m15112.mail.126.com [220.181.15.112]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B773568B743 for ; Sat, 20 Jun 2020 04:46:11 +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=IyuSw 7hRSW+KCS4IKLJGIHpi0UmcHXChgqZZzNgHBWQ=; b=kHm5wgPCLRJcp5Umgi7S/ 4UubwgT0PK7ii0TrwLsRLY8EbPZhg5uDCBhLBKQlg6cXV5crF6iaPZnKBn7HHWTi Hs1ChS/nbj+ZfDJ30UKfFdnde/6ausBVoVTQl0dB0oKsoNCSyFVbqpWj7wtYXfGh K70VqxRWo/tyfTksIPrd4E= Received: from localhost.localdomain (unknown [111.34.131.148]) by smtp2 (Coremail) with SMTP id DMmowACXzgBbau1ebM9FEg--.41208S6; Sat, 20 Jun 2020 09:46:06 +0800 (CST) From: hwrenx@126.com To: ffmpeg-devel@ffmpeg.org Date: Sat, 20 Jun 2020 09:45:52 +0800 Message-Id: <20200620014552.806-5-hwrenx@126.com> X-Mailer: git-send-email 2.23.0.windows.1 In-Reply-To: <20200620014552.806-1-hwrenx@126.com> References: <20200620014552.806-1-hwrenx@126.com> MIME-Version: 1.0 X-CM-TRANSID: DMmowACXzgBbau1ebM9FEg--.41208S6 X-Coremail-Antispam: 1Uf129KBjvAXoW3tF4DWr1ktw15ur48CFy7trb_yoW8GF1rAo Wjyryjvw18Jr929a1q9r1UW347XF4YkrW3Jwsakay5G398WFy3Kry5uw15Xw4DCrWFya4f ZayIgrW7tF48Kw4rn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjxUSdgAUUUUU X-Originating-IP: [111.34.131.148] X-CM-SenderInfo: pkzuv0b06rjloofrz/1tbiux5J6VpEA4In-QAAsA Subject: [FFmpeg-devel] [PATCH v1 4/4] 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: hwrenx@126.com, 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 | 283 +++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 319 insertions(+) create mode 100644 libavcodec/libuavs3d.c diff --git a/Changelog b/Changelog index a60e7d2..dfd56b3 100644 --- a/Changelog +++ b/Changelog @@ -4,6 +4,7 @@ releases are sorted from youngest to oldest. version : - AudioToolbox output device - MacCaption demuxer +- AVS3 video decoder via libuavs3d version 4.3: diff --git a/configure b/configure index 7495f35..459f3a6 100755 --- a/configure +++ b/configure @@ -274,6 +274,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] @@ -1807,6 +1808,7 @@ EXTERNAL_LIBRARY_LIST=" libtesseract libtheora libtwolame + libuavs3d libv4l2 libvorbis libvpx @@ -3242,6 +3244,7 @@ libspeex_encoder_deps="libspeex" libspeex_encoder_select="audio_frame_queue" 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" @@ -6379,6 +6382,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 "uavs3d > 1.0.0" 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.3.9" libvmaf.h compute_vmaf diff --git a/doc/decoders.texi b/doc/decoders.texi index 9005714..f1a0b3c 100644 --- a/doc/decoders.texi +++ b/doc/decoders.texi @@ -87,6 +87,27 @@ AVS2-P2/IEEE1857.4 video decoder wrapper. 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 9b0ee96..6d673b7 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 f151277..491485f 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1026,6 +1026,7 @@ OBJS-$(CONFIG_LIBSPEEX_DECODER) += libspeexdec.o OBJS-$(CONFIG_LIBSPEEX_ENCODER) += libspeexenc.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 80f128c..3d2d0af 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -730,6 +730,7 @@ extern AVCodec ff_libspeex_encoder; extern AVCodec ff_libspeex_decoder; 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 0000000..bee09b1 --- /dev/null +++ b/libavcodec/libuavs3d.c @@ -0,0 +1,283 @@ +/* + * 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/common.h" +#include "libavutil/avutil.h" +#include "libavutil/opt.h" +#include "avcodec.h" +#include "uavs3d.h" +#include "libavutil/imgutils.h" +#include "internal.h" + +#define UAVS3D_MAX_FRAME_THREADS 48 + +static const int 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 +}; + +static const int 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 +}; + +static const int 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 +}; + +static const enum AVPictureType IMGTYPE[8] = { + AV_PICTURE_TYPE_NONE, + AV_PICTURE_TYPE_I, + AV_PICTURE_TYPE_P, + AV_PICTURE_TYPE_B +}; + +typedef struct UAVS3DContext { + AVCodecContext *avctx; + void *dec_handle; + int frame_threads; + int got_seqhdr; + uavs3d_io_frm_t dec_frame; +} UAVS3DContext; + + +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 && + ((*(unsigned int *)data_ptr) != 0xB6010000) && /* P/B picture */ + ((*(unsigned int *)data_ptr) != 0xB3010000) && /* I picture */ + ((*(unsigned int *)data_ptr) != 0xB0010000) && /* sequence header */ + ((*(unsigned int *)data_ptr) != 0x00010000) && /* first slice */ + ((*(unsigned int *)data_ptr) != 0xB1010000)) { /* sequence end */ + 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) { + return; + } + + frm->pts = dec_frame->pts; + frm->pkt_dts = dec_frame->dts; + frm->pict_type = IMGTYPE[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 uavs3d_init(AVCodecContext *avctx) +{ + UAVS3DContext *h = avctx->priv_data; + uavs3d_cfg_t cdsc; + + cdsc.frm_threads = FFMIN(h->frame_threads > 0 ? h->frame_threads : av_cpu_count(), UAVS3D_MAX_FRAME_THREADS); + cdsc.check_md5 = 0; + h->dec_handle = uavs3d_create(&cdsc, uavs3d_output_callback, NULL); + h->got_seqhdr = 0; + + return 0; +} + +static av_cold int uavs3d_end(AVCodecContext *avctx) +{ + UAVS3DContext *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 uavs3d_flush(AVCodecContext * avctx) +{ + UAVS3DContext *h = avctx->priv_data; + uavs3d_cfg_t cdsc; + cdsc.frm_threads = FFMIN(h->frame_threads > 0 ? h->frame_threads : av_cpu_count(), UAVS3D_MAX_FRAME_THREADS); + cdsc.check_md5 = 0; + + if (h->dec_handle) { + uavs3d_flush(h->dec_handle, NULL); + uavs3d_delete(h->dec_handle); + } + + h->dec_handle = uavs3d_create(&cdsc, uavs3d_output_callback, NULL); + h->got_seqhdr = 0; +} + +static int uavs3d_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt) +{ + UAVS3DContext *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 = (AVFrame*)data; + int left_bytes; + int ret, finish = 0; + + *got_frame = 0; + frm->pts = -1; + frm->pict_type = AV_PICTURE_TYPE_NONE; + + 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 (!buf_size) { + do { + ret = uavs3d_flush(h->dec_handle, &h->dec_frame); + } while (ret > 0 && !h->dec_frame.got_pic); + } else { + buf_ptr = buf; + buf_end = buf + buf_size; + + while (!finish) { + int bs_len; + uavs3d_io_frm_t *frm_dec = &h->dec_frame; + + 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) { + static const int avs2_fps_num[9] = {0, 240000, 24, 25, 30000, 30, 50, 60000, 60 }; + static const int avs2_fps_den[9] = {1, 1001, 1, 1, 1001, 1, 1, 1001, 1 }; + avctx->framerate.num = avs2_fps_num[frm_dec->seqhdr->frame_rate_code]; + avctx->framerate.den = avs2_fps_den[frm_dec->seqhdr->frame_rate_code]; + avctx->has_b_frames = 1; + avctx->pix_fmt = frm_dec->seqhdr->bit_depth_internal == 8 ? AV_PIX_FMT_YUV420P : AV_PIX_FMT_YUV420P10LE; + ff_set_dimensions(avctx, frm_dec->seqhdr->horizontal_size, frm_dec->seqhdr->vertical_size); + h->got_seqhdr = 1; + } + if (frm_dec->got_pic) { + break; + } + } + } + + *got_frame = h->dec_frame.got_pic; + + return buf_ptr - buf; +} + +#define UAVS3D_OFFSET(x) offsetof(UAVS3DContext, x) +#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM +static const AVOption options[] = { + { "frame_threads", "number of frame-level threads ", UAVS3D_OFFSET(frame_threads), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, UAVS3D_MAX_FRAME_THREADS, VE }, + { NULL } +}; +static const AVClass libuavs3d_class = { + .class_name = "libuavs3d_class", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVCodec ff_libuavs3d_decoder = { + .name = "libuavs3d", + .long_name = NULL_IF_CONFIG_SMALL("uavs3d AVS3-P2/IEEE1857.10 decoder"), + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_AVS3, + .priv_data_size = sizeof(UAVS3DContext), + .priv_class = &libuavs3d_class, + .init = uavs3d_init, + .close = uavs3d_end, + .decode = uavs3d_decode_frame, + .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, + .flush = uavs3d_flush, + .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P10LE, AV_PIX_FMT_NONE }, + + .wrapper_name = "libuavs3d", +};