From patchwork Sun Jun 12 16:42:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhao Zhili X-Patchwork-Id: 36175 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp83168pzb; Sun, 12 Jun 2022 09:42:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw85sNLPPbUM+okLJue1qGm9wmBzCz6yOqEb8ocmFn92+FUh/KxNYTeI/a4Z6p59lIZh/xv X-Received: by 2002:a17:906:730f:b0:711:db1a:fdb0 with SMTP id di15-20020a170906730f00b00711db1afdb0mr28319045ejc.54.1655052179349; Sun, 12 Jun 2022 09:42:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655052179; cv=none; d=google.com; s=arc-20160816; b=CAhPD9UGnSoLCaZvYR4Kr7EzpkSrLAiW5UcwdsbPFhoLlg9or2gUV5bFxkNhqfYpTE WyAavgknH43YkNSwDc5mhfSsW+Pxn84r+Qt8zq+ZJqhpQZ2E5fVLBWN2FJlNWKg4ElOe 3W2aB1E+Y3XjObAvmmzHgBB+5+AM+a69nk2NolDIZbNqYcwUGSezgDKnawOPsmMIif9v 2gzsxskIS/tNh5VAfc+/JIls+5ywkdy3STkzl9NMaCbRKbiyZy6srqoYl7NbRwd0F6X0 UCLslF6CKdVM+Mn8gSo0BBaIT/GBaaZtFgUr+FKLdGO3oZVwoAl8pjd6C0Om46OLrWo5 +87Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to:date :to:from:message-id:dkim-signature:delivered-to; bh=ZxnI4MhUlhtKyFuWhE2EbRYeGpxLk0zOSItAsejR+BM=; b=Cs5bttkGKww7rt+TIQ7s/QLFMmwgcV78RrnIgY5D1rXLW+Zg57EuHB63K+/mWnI9vw Ey+XaRuIhiGxT/WNOzsQZDmeGPpT5sXtu7SmInM/BiZNO83dW5BZCRyENJP0E58eU1rs eCkoxEEngF5IUOUMLCmWwlpjnVtdWCmZ/7jk/eN/fPo5FY7RBrZLfnazHNju4SYZWuYS L2epwcEZ7sbG/Z0I8vCfDTvwYhqC9b4r3ZCoDQKqwO9KBZY6vB/N2+fl8sHB8a6ReBkM 4jv4QSTSBvryCKsVdz9CqfCmQIzwMQpeA7rXD1tILo79IxM88hZ+vzq9Rn7YGLBNo4rG qPWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b="tZjffm/2"; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=foxmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id e11-20020a170906648b00b00711d61d0e1csi5202837ejm.109.2022.06.12.09.42.58; Sun, 12 Jun 2022 09:42:59 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b="tZjffm/2"; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=foxmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8658E68B42F; Sun, 12 Jun 2022 19:42:54 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from out203-205-221-173.mail.qq.com (out203-205-221-173.mail.qq.com [203.205.221.173]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 76C026881AC for ; Sun, 12 Jun 2022 19:42:45 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1655052161; bh=349p3LTqFNeMqx2i6imE/kpXrhUVCaWEkV/OIUugxbU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=tZjffm/2u7agVMBGzzkwLpqYewb2mrJYekR5xlFpMFoF6DoxE6zXC4RW4YwwcC44Y C22VNFQMhJd5qohabx1qaFqRnwiACLRJqdtsvUR18pMTTxESZuATkUQzT5Qw1PTeyR xENTenGfk9/ldNSmOd3T/VPWAF3dR4/N3DpaxXoE= Received: from ZHILIZHAO-MB2.tencent.com ([121.35.187.221]) by newxmesmtplogicsvrszc8.qq.com (NewEsmtp) with SMTP id AA7A6090; Mon, 13 Jun 2022 00:42:39 +0800 X-QQ-mid: xmsmtpt1655052159tfsy3ga3x Message-ID: X-QQ-XMAILINFO: NMmJpeSXIGQNDg2tHfvZaaY5keJ0IXh6o/WoibRanHCFx24ihFB5PZCq4x42lS UxlCpbGPbEsqi1k50bZu4rPjoia2EM/ZbfdJN05uw3FdQtt6/zQzbT6OHh3PGSC8C1roeGKbAuOq z7z3o54ZqTpJExM0h/g0i5cEG/d55UQCIxmCfHzinBqsZmlsB3Zkvy7Km8jvTPuLfTSbFPoITJUL vITPDGCtX2nyTA2FU1hhneBU33JCbmmFQbWDU3vmQbjcLtWGzgOdcvDd32ReDDwRoCDQfTLyMH09 DmR6sS7Pp+0RyYIcaICcYodpiCB155Jo5JiKDgYfjhFjQFWUMHWCrBSj8Prwij3bVD8m30sWHRFg Vk3ugNxLIVXhianfaiEAKiDStJZSWgL+yUqKnZ009cDj/sfkBPCPgutdmo0ULSqn9NOpdXAl2gJ8 ptUN946tmbxt6DQXLFnkcIpOZ9XvaSV+BaLf+XFuenVqPmYyexneIqO5YJGSUMboZ08o/EqvKBbB /AskYxSVDpZyNcbz2GLjD81jkslwTTdnYjeWkLaLCkf8RpDGrsyVXVzQ7FDOOr0gqwH7vUiy3Hhk 5dOwLtjqfSidae5sMrku5jGmbdO2BZyHVdhomoQ9wjmet2AEJzhbwQRFjD0a37TMJrNObN8VbX6U MMPeftpAGi9f9Fqowwj5IzGguGX/Flp2UE0t/ihKME78553PwUukOlU5zSPwth+BpsSBuRS89ifp fUtL49AQxdTyqUk9vBR0gTLUnD8ybEGlnEzTH+1WPZ3NOhJF6Y5Ro5TndZTmXJKwSWCx21VNdH4g VDhDLy41JsyarsLdvoRS/lCAnxJjp/KqXaeo6Nf/ElfURIuAiDlO78VG49XXlkLFc+TTwnfMBltn 1siXD8xe521nlzT6uqm6yDDQld3t5y9qOsYZAMMnVIqtaRF656ov+HcB56INkSeXRIqjj4DQYN From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Jun 2022 00:42:37 +0800 X-OQ-MSGID: <20220612164237.64154-1-quinkblack@foxmail.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 3/3] avcodec/avs2_parser: parse more info X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: Zhao Zhili Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: JGKSfMdYFRmC Including video resolution, framerate and picture type, etc. Signed-off-by: Zhao Zhili --- v2: fix unused variable warning libavcodec/avs2.h | 27 ++++++++++ libavcodec/avs2_parser.c | 103 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/libavcodec/avs2.h b/libavcodec/avs2.h index f342ba52a0..e961c7d517 100644 --- a/libavcodec/avs2.h +++ b/libavcodec/avs2.h @@ -23,6 +23,8 @@ #ifndef AVCODEC_AVS2_H #define AVCODEC_AVS2_H +#include "libavutil/rational.h" + #define AVS2_SLICE_MAX_START_CODE 0x000001AF enum { @@ -38,4 +40,29 @@ enum { #define AVS2_ISPIC(x) ((x) == AVS2_INTRA_PIC_START_CODE || (x) == AVS2_INTER_PIC_START_CODE) #define AVS2_ISUNIT(x) ((x) == AVS2_SEQ_START_CODE || AVS2_ISPIC(x)) +enum AVS2Profile { + AVS2_PROFILE_MAIN_PIC = 0x12, + AVS2_PROFILE_MAIN = 0x20, + AVS2_PROFILE_MAIN10 = 0x22, +}; + +static const AVRational ff_avs2_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 +}; + #endif diff --git a/libavcodec/avs2_parser.c b/libavcodec/avs2_parser.c index 71cf442903..18d7729768 100644 --- a/libavcodec/avs2_parser.c +++ b/libavcodec/avs2_parser.c @@ -19,7 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "libavutil/avutil.h" #include "avs2.h" +#include "get_bits.h" #include "parser.h" static int avs2_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size) @@ -58,6 +60,105 @@ static int avs2_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_siz return END_NOT_FOUND; } +static void parse_avs2_seq_header(AVCodecParserContext *s, const uint8_t *buf, + int buf_size, AVCodecContext *avctx) +{ + GetBitContext gb; + int profile, level; + int width, height; + int chroma, sample_precision, encoding_precision = 1; + // sample_precision and encoding_precision is 3 bits + static uint8_t precision[8] = { 0, 8, 10 }; + unsigned aspect_ratio; + unsigned frame_rate_code; + int low_delay; + + if (buf_size < 15) + return; + + init_get_bits8(&gb, buf, buf_size); + + s->key_frame = 1; + s->pict_type = AV_PICTURE_TYPE_I; + + profile = get_bits(&gb, 8); + level = get_bits(&gb, 8); + + // progressive_sequence u(1) + // field_coded_sequence u(1) + skip_bits(&gb, 2); + + width = get_bits(&gb, 14); + height = get_bits(&gb, 14); + + chroma = get_bits(&gb, 2); + sample_precision = get_bits(&gb, 3); + if (profile == AVS2_PROFILE_MAIN10) + encoding_precision = get_bits(&gb, 3); + + aspect_ratio = get_bits(&gb, 4); + frame_rate_code = get_bits(&gb, 4); + + // bit_rate_lower u(18) + // marker_bit f(1) + // bit_rate_upper u(12) + skip_bits(&gb, 31); + + low_delay = get_bits(&gb, 1); + + s->width = width; + s->height = height; + s->coded_width = FFALIGN(width, 8); + s->coded_height = FFALIGN(height, 8); + avctx->framerate.num = avctx->time_base.den = + ff_avs2_frame_rate_tab[frame_rate_code].num; + avctx->framerate.den = avctx->time_base.num = + ff_avs2_frame_rate_tab[frame_rate_code].den; + avctx->has_b_frames = FFMAX(avctx->has_b_frames, !low_delay); + + av_log(avctx, AV_LOG_DEBUG, + "AVS2 parse seq HDR: profile %x, level %x, " + "width %d, height %d, " + "chroma %d, sample_precision %d bits, encoding_precision %d bits, " + "aspect_ratio 0x%x, framerate %d/%d, low_delay %d\n", + profile, level, + width, height, + chroma, precision[sample_precision], precision[encoding_precision], + aspect_ratio, avctx->framerate.num, avctx->framerate.den, low_delay); +} + +static void parse_avs2_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)) + return; + + switch (buf[3]) { + case AVS2_SEQ_START_CODE: + parse_avs2_seq_header(s, buf + 4, buf_size - 4, avctx); + return; + case AVS2_INTRA_PIC_START_CODE: + s->key_frame = 1; + s->pict_type = AV_PICTURE_TYPE_I; + return; + case AVS2_INTER_PIC_START_CODE: + s->key_frame = 0; + if (buf_size > 9) { + int pic_code_type = buf[8] & 0x3; + if (pic_code_type == 1) + s->pict_type = AV_PICTURE_TYPE_P; + else if (pic_code_type == 3) + s->pict_type = AV_PICTURE_TYPE_S; + else + s->pict_type = AV_PICTURE_TYPE_B; + } + return; + } +} + static int avs2_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size) @@ -76,6 +177,8 @@ static int avs2_parse(AVCodecParserContext *s, AVCodecContext *avctx, } } + parse_avs2_units(s, buf, buf_size, avctx); + *poutbuf = buf; *poutbuf_size = buf_size;