From patchwork Fri Nov 11 17:17:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 39247 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:999a:b0:a4:2148:650a with SMTP id ve26csp1042693pzb; Fri, 11 Nov 2022 09:18:16 -0800 (PST) X-Google-Smtp-Source: AA0mqf6qUlaIGCC5RPDiSsD6ukGCxBjdJMxEH13+t8ZQOP3x/5RrMopriuol9Nw0ftJrlVkTcDRL X-Received: by 2002:a17:906:4e42:b0:781:e568:294f with SMTP id g2-20020a1709064e4200b00781e568294fmr2675687ejw.447.1668187095639; Fri, 11 Nov 2022 09:18:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668187095; cv=none; d=google.com; s=arc-20160816; b=kwy6R2MUmMYunIELZEFY7L5ORVC6vczVOFmItsuCQKfYjlFVx+4+JijLdM22DfEyMV LvEDQ/voAH843RkW1k7OfZ4yJlCGuadX++TqP7lmg8bhrSfwIQUrUW+PciGR+jAFTknl rYyNFDpfh+FwEj9FfB1xgBTbD2qoztkj8ntSORouKa8IXQVGrTY3EvFPz8hg6Ewkf6hi H5tAbChDNuAcoogrf84+5XZRKwEQSrhWdC4vktlIjXziSWxvOcIpxbfeICmNOScy/rJJ 4HQsGnQ5sqh7gt7s8TAOqGSk2KYPPVKGg5Y7FbNzRzgQQ7Mdf+IzVFNZ3QZUTGZBemY0 BglQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=44XVjS9/hyvdQMSMxI1KI8+xhb+Yf8q0UlWcbMnSKLc=; b=Nwl+JpXl/gF+sZxVmfnN5nQXRbJc7HUGMMlXL+UytWYwIMf9UczXazJvCZ9hTSkLX/ Lth0Q1BBZZYrEsaKd7Njqmvr5jeOgZcBvukHYCyst7+DWXVkT+q6HECtoxxltJtQOovi QfH1/qLZcQZq+EQqFZYZJ0bbGbYu2jf5oekjkz7YmelbYKNPw2vNJvYvBBA7JWzQddDv td65gPzSdq2O1qJ3nYm20/oOCcyioRTAGuWo/9Wub45bN0Ofs3DUUgdFEJUVAOvCVQBw kR1s0tmobbkB8mmJlovJ3DlcwvihfsA5h5H2jhmYcuNmGhsaSikI5yGoCmnJ+5emsMVe EXgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=RlBQaLJn; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id g22-20020a1709061e1600b007a641e472f0si2303278ejj.139.2022.11.11.09.17.56; Fri, 11 Nov 2022 09:18:15 -0800 (PST) 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=@gmail.com header.s=20210112 header.b=RlBQaLJn; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6C91368B752; Fri, 11 Nov 2022 19:17:53 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-oi1-f174.google.com (mail-oi1-f174.google.com [209.85.167.174]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 61DDD68B034 for ; Fri, 11 Nov 2022 19:17:46 +0200 (EET) Received: by mail-oi1-f174.google.com with SMTP id n186so5465201oih.7 for ; Fri, 11 Nov 2022 09:17:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=UZSw2Fq7JnmhIG3tf0IGzY2KPI6GnC4u06xrmU6YB5M=; b=RlBQaLJndHOjxbznn8Xd+Niuu+0rf48rdTEjXp96Mx9k2HqVQvKBIL774sOHz3Molh xr+Q5/xsYdSVnf2gW8rIXe9ER7DIaNx0PndG1G/9KDwae/a4ZmZptx6BxNaorfybw5Ym STnGw7GxFFIR7vMkJTp5SX1o+/4ynOdErsVi0h7yIMU5DugDf1dRNvO2yaeJ4R4nrC8c NveeM8eFvQBUBlRKo2fXfqb6PFXzqe75X3mpZHxMbg7WDCYMF0B/lBzc4wIV0LMQUx+T oOYK/7EkqtXK2EAkX3amz2rJMIxMFSXkRWiG+Ov6FqPZC5qfJ6t3qMJ2OzXNnC5MAFJL gTtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=UZSw2Fq7JnmhIG3tf0IGzY2KPI6GnC4u06xrmU6YB5M=; b=XPjXaCWtZrj1TbqZU4wDIq/N1/bkQ/m4nQpErKN3zNB2/XdcnoAB1qc6D1cTPerH9i yvimD2Vt3Xtt5MiTypVHvTUsRH2g2EJW+YlE2F2hWIfRFwG3qAstGzQjqDgz2weyfGPO rEzT5tabk46LtQgEN9+Zj5/RQUzUGiekzJVDtUin3QKIhQaBrNSZVwHgwcFsWxfL1mkG UpzG6N4Jy1JpagFWbxQzqMhBXtrPqVzxwd1ZjAa7Q2vCpz8KD9tV62Wy78RLXuB00T9f AlQdGlXQrVNj/jqG0ynsuf0MEFsvcvIjOavhNTTZNSxX601P6KdddC2K0C3DtgfjaLZQ 1DSg== X-Gm-Message-State: ANoB5pnS9n1v9+UkEe4qRewqlq/WVIU/N/ZlvLILdnyoJ20auWNmdJ1W NxlWvuJNPVRVdy16vhE7Ru2vVhYt3Ew= X-Received: by 2002:a05:6808:4f:b0:354:b7e5:8f8f with SMTP id v15-20020a056808004f00b00354b7e58f8fmr1231037oic.223.1668187064448; Fri, 11 Nov 2022 09:17:44 -0800 (PST) Received: from localhost.localdomain ([181.85.72.69]) by smtp.gmail.com with ESMTPSA id l4-20020a4ac604000000b0049f0671a23asm997451ooq.9.2022.11.11.09.17.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Nov 2022 09:17:43 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Fri, 11 Nov 2022 14:17:33 -0300 Message-Id: <20221111171733.4484-1-jamrial@gmail.com> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avformat/vpcc: parse bitstream data to get profile and bitdepth 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: U5a2o2tmU0Jb Profile can be derived from values codecpar pixel format only with software formats. For hardware formats, we're forced to parse a frame header to get the required information. Signed-off-by: James Almer --- libavformat/dashenc.c | 2 +- libavformat/movenc.c | 4 +++- libavformat/vpcc.c | 44 +++++++++++++++++++++++++++++++++++++++++-- libavformat/vpcc.h | 5 +++++ 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c index a0919f6f2d..9c1bcad9e3 100644 --- a/libavformat/dashenc.c +++ b/libavformat/dashenc.c @@ -338,7 +338,7 @@ static int init_segment_types(AVFormatContext *s) static void set_vp9_codec_str(AVFormatContext *s, AVCodecParameters *par, AVRational *frame_rate, char *str, int size) { VPCC vpcc; - int ret = ff_isom_get_vpcc_features(s, par, frame_rate, &vpcc); + int ret = ff_isom_get_vpcc_features(s, par, NULL, 0, frame_rate, &vpcc); if (ret == 0) { av_strlcatf(str, size, "vp09.%02d.%02d.%02d", vpcc.profile, vpcc.level, vpcc.bitdepth); diff --git a/libavformat/movenc.c b/libavformat/movenc.c index 754f95912a..064b541972 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -1375,7 +1375,8 @@ static int mov_write_vpcc_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *tra ffio_wfourcc(pb, "vpcC"); avio_w8(pb, 1); /* version */ avio_wb24(pb, 0); /* flags */ - ff_isom_write_vpcc(s, pb, track->par); + ff_isom_write_vpcc(s, pb, track->vos_data, track->vos_len, track->par); + return update_size(pb, pos); } @@ -6033,6 +6034,7 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt) if ((par->codec_id == AV_CODEC_ID_DNXHD || par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_HEVC || + par->codec_id == AV_CODEC_ID_VP9 || par->codec_id == AV_CODEC_ID_TRUEHD) && !trk->vos_len && !TAG_IS_AVCI(trk->tag)) { /* copy frame to create needed atoms */ diff --git a/libavformat/vpcc.c b/libavformat/vpcc.c index 8b4f913b5d..1fbde74fbb 100644 --- a/libavformat/vpcc.c +++ b/libavformat/vpcc.c @@ -22,8 +22,11 @@ #include "libavutil/pixdesc.h" #include "libavutil/pixfmt.h" #include "libavcodec/avcodec.h" +#include "libavcodec/get_bits.h" #include "vpcc.h" +#define VP9_SYNCCODE 0x498342 + enum VPX_CHROMA_SUBSAMPLING { VPX_SUBSAMPLING_420_VERTICAL = 0, @@ -114,7 +117,30 @@ static int get_vp9_level(AVCodecParameters *par, AVRational *frame_rate) { } } +static void parse_bitstream(GetBitContext *gb, int *profile, int *bit_depth) { + int keyframe; + + if (get_bits(gb, 2) != 0x2) // frame marker + return; + *profile = get_bits1(gb); + *profile |= get_bits1(gb) << 1; + if (*profile == 3) + *profile += get_bits1(gb); + + if (get_bits1(gb)) + return; + + keyframe = !get_bits1(gb); + get_bits(gb, 2); + + if (!keyframe || get_bits(gb, 24) != VP9_SYNCCODE) + return; + + *bit_depth = *profile <= 1 ? 8 : 10 + get_bits1(gb) * 2; +} + int ff_isom_get_vpcc_features(AVFormatContext *s, AVCodecParameters *par, + const uint8_t *data, int len, AVRational *frame_rate, VPCC *vpcc) { int profile = par->profile; @@ -129,7 +155,17 @@ int ff_isom_get_vpcc_features(AVFormatContext *s, AVCodecParameters *par, if (bit_depth < 0 || vpx_chroma_subsampling < 0) return AVERROR_INVALIDDATA; - if (profile == FF_PROFILE_UNKNOWN) { + if (len && (profile == FF_PROFILE_UNKNOWN || !bit_depth)) { + GetBitContext gb; + + int ret = init_get_bits8(&gb, data, len); + if (ret < 0) + return AVERROR_INVALIDDATA; + + parse_bitstream(&gb, &profile, &bit_depth); + } + + if (profile == FF_PROFILE_UNKNOWN && bit_depth) { if (vpx_chroma_subsampling == VPX_SUBSAMPLING_420_VERTICAL || vpx_chroma_subsampling == VPX_SUBSAMPLING_420_COLLOCATED_WITH_LUMA) { profile = (bit_depth == 8) ? FF_PROFILE_VP9_0 : FF_PROFILE_VP9_2; @@ -138,6 +174,9 @@ int ff_isom_get_vpcc_features(AVFormatContext *s, AVCodecParameters *par, } } + if (profile == FF_PROFILE_UNKNOWN || !bit_depth) + return AVERROR_INVALIDDATA; + vpcc->profile = profile; vpcc->level = level; vpcc->bitdepth = bit_depth; @@ -148,12 +187,13 @@ int ff_isom_get_vpcc_features(AVFormatContext *s, AVCodecParameters *par, } int ff_isom_write_vpcc(AVFormatContext *s, AVIOContext *pb, + const uint8_t *data, int len, AVCodecParameters *par) { VPCC vpcc; int ret; - ret = ff_isom_get_vpcc_features(s, par, NULL, &vpcc); + ret = ff_isom_get_vpcc_features(s, par, data, len, NULL, &vpcc); if (ret < 0) return ret; diff --git a/libavformat/vpcc.h b/libavformat/vpcc.h index f05b2f58b3..bad38c5615 100644 --- a/libavformat/vpcc.h +++ b/libavformat/vpcc.h @@ -45,14 +45,19 @@ typedef struct VPCC { * * @param s address of the AVFormatContext for the logging context. * @param pb address of the AVIOContext where the vpcC shall be written. + * @param data address of a data array which contains coded bitstream data from + * which codec information can be extracted. May be NULL. + * @param len length of the data array. * @param par address of the AVCodecParameters which contains codec information. * @return >=0 in case of success, a negative value corresponding to an AVERROR * code in case of failure */ int ff_isom_write_vpcc(AVFormatContext *s, AVIOContext *pb, + const uint8_t *data, int len, AVCodecParameters *par); int ff_isom_get_vpcc_features(AVFormatContext *s, AVCodecParameters *par, + const uint8_t *data, int len, AVRational *frame_rate, VPCC *vpcc); #endif /* AVFORMAT_VPCC_H */