From patchwork Thu Jun 30 21:04:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vignesh Venkat X-Patchwork-Id: 36557 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:8b27:b0:88:1bbf:7fd2 with SMTP id l39csp580034pzh; Thu, 30 Jun 2022 14:05:05 -0700 (PDT) X-Google-Smtp-Source: AGRyM1u6sn7Yqw3B7nqZnnoOUs98AuZ42MrSUjFEprcGC96X16dUc/R+ZxLhZjQ/33h9HAu18qv+ X-Received: by 2002:a17:907:1c09:b0:726:b834:1a21 with SMTP id nc9-20020a1709071c0900b00726b8341a21mr10871300ejc.518.1656623105463; Thu, 30 Jun 2022 14:05:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656623105; cv=none; d=google.com; s=arc-20160816; b=qs4cXB6A/ecIg3EN/Wtc58J1xhW8Sojj6tWx6FLpPsgN5yRO4BFXfLzgPfgBPzytaM uwW0kOOQdBQLaEjn/qprvanRBTfIANe42RiMnTnkm8JvqoAvn/Anj2e6JDp7LUmmSE+w U2TvCEQYVh4R/uaxVZVfgx1DJQIE77+FqKHe5Mt7LLjCsJhgD2dKCef6PbVC35NE0rYM wc7sr4NrucEuWRzD3G/iccfjmAWfzwacFBu2LCjZe/6lmgjCI7W9lbHc3GDLvRVeRQpe bvYrpWg+wA3fwMnbr2li5CpyNxGCH+iLuMruE6aLq02hWI/xfeH7LEpYH4AAG1xRs5UR j94g== 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:to:from:references:mime-version :message-id:in-reply-to:date:dkim-signature:delivered-to; bh=+RNOufFWs8vrv+jQtQ+XVnUcihN24K2ipHnpyuNAyJI=; b=ixQieFoMMbF86OOkFncxLjuVs7QN9EBRfhh/GFgiZaAJ3oOtuZDGLGln0eC2Z+QSVJ o0LN7Ep2qFNMk3TufotPeHIRfWehi30jySXyzAJQanwsyroyhIZIAGFbux+7JfBpgmT2 9iZ3ee/uJaRXCQ7xkB7pefowLwhXSjh390qbMRAynlw2Zt4PWiYSUNhzTlUFu9WuKe1N LXf1a8ZyX3zXFsZzs8CP6u/rIMv0B0VMrpm37ES+yofeuKdz1S6LT5Ivsj7/RsXf5+K9 YfyP+PFAOmt1NOs3NC9ED02QulkhVEczIgOvV1D0F/HNieR9EFwoNa0hLkjMBIImjoG0 llQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@google.com header.s=20210112 header.b="VOUucuj/"; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id nb28-20020a1709071c9c00b00707e0e3cd7csi2810553ejc.359.2022.06.30.14.05.05; Thu, 30 Jun 2022 14:05:05 -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=@google.com header.s=20210112 header.b="VOUucuj/"; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 72DE868B73B; Fri, 1 Jul 2022 00:04:56 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1A6EA68B73F for ; Fri, 1 Jul 2022 00:04:50 +0300 (EEST) Received: by mail-yb1-f202.google.com with SMTP id d132-20020a251d8a000000b0066c8dfb35f4so280892ybd.12 for ; Thu, 30 Jun 2022 14:04:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=7MTU060BMGaiWXGDRhJFaolWf0Cpc0UHRCj+OeTMdgg=; b=VOUucuj/DNC0RmZUkxs21rFgAF4c0vI1tuYa0HLlYcHvxOSxNIZFYq4eOX2GEl7gwn iaMXRkZsc3/P02GNuqzU8v+OuatwFqONTtaxU/D9MzpXx2jmFwTIIrAWSzF9QcB9mVQr aMFRJdxx+fHzPCwf54K1sdMb7OyNfHvBNq2D/XnPj+D5ASUgSzwAjh0gFrO106dhv620 kufnvTDw/NauquyL4XfiPMG4gTc79l+w/sTczNtmhriMWy2uA+anVJM7ifQbZ21lqmRL GCHyS0uLze5HNR0PxFy63SgHMO+IvuwYxy6ZjwhdpEXbctkJ+fjHKviPwDTtkDB9Zw22 23Eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=7MTU060BMGaiWXGDRhJFaolWf0Cpc0UHRCj+OeTMdgg=; b=mRxdrzhr9/GRObxrNkNfOeX+nmdH6575ECNHXheUGyBwzZ+BtzKNmV3vqRI8VSrAqw 8DypkZcQ60802gaucoyZLhaHUVrgUcKSqLWDYTMWDlWnBeABOWhzXRT1y+QzLeZtJgqH H/YpCkLsmDntb2lIMIq83w2QTYDM8u46ihwuGpZVIcyF4ypbkrrFwMZx2RALS0f7I4rL s9vj5IZ11wMFcMM6oKzDUlDVpv5QOOcZElDeahMinxC5EZa1I8uZFRROnHQqjvIhftz3 6ZDTXBMtDW68tRGc4h6u2cXPHHxOqAxBvrX3nFvtWfYrWPKrosCy38FNmuJIPoOrgMrA qQJw== X-Gm-Message-State: AJIora/Ldgchig391KjuBEy7lsDVyiWrrRo5n/1SN4fTGnUw6NbKv/lL BawrPvPkgyF5nn+Lc3yiPQOa3p7UqAR2M8laPj82gh+7zksrWR7tqMYslYktIviGTxugoErKoCi +mOHebQ1zQ8IIC/G7TOLCdvPwmGSkjJ3NN5bElG4Mrx7UKz2iYE5hPEHChd3li02ez7y1 X-Received: from vigneshv3.mtv.corp.google.com ([2620:0:1000:2511:f6b5:b6ce:7578:e219]) (user=vigneshv job=sendgmr) by 2002:a05:6902:1389:b0:668:39b3:63bc with SMTP id x9-20020a056902138900b0066839b363bcmr10725685ybu.577.1656623088840; Thu, 30 Jun 2022 14:04:48 -0700 (PDT) Date: Thu, 30 Jun 2022 14:04:34 -0700 In-Reply-To: <20220630210434.1551769-1-vigneshv@google.com> Message-Id: <20220630210434.1551769-2-vigneshv@google.com> Mime-Version: 1.0 References: <20220630210434.1551769-1-vigneshv@google.com> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog From: Vignesh Venkatasubramanian To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH 2/2] avformat/mov: Support parsing of still AVIF Alpha Channel 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: Vignesh Venkatasubramanian Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: DM8kNrZdUvHW Parse the alpha channel for still AVIF images and expose it as a separate track. This is the simplest way of supporting AVIF alpha channel in a codec independent manner (similar to how ffmpeg supports animated AVIF with alpha channel). One can use the alphamerge filter to get a transparent image with a single command. For example: ffmpeg -i image_with_alpha.avif -filter_complex alphamerge image_with_alpha.png Signed-off-by: Vignesh Venkatasubramanian --- libavformat/isom.h | 1 + libavformat/mov.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/libavformat/isom.h b/libavformat/isom.h index d8b262e915..62b95b40ff 100644 --- a/libavformat/isom.h +++ b/libavformat/isom.h @@ -318,6 +318,7 @@ typedef struct MOVContext { uint32_t max_stts_delta; int is_still_picture_avif; int primary_item_id; + int alpha_item_id; int *avif_item_ids; int avif_item_ids_size; int *avif_extent_lengths; diff --git a/libavformat/mov.c b/libavformat/mov.c index 9df5055d4e..72b17b618d 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -4758,6 +4758,7 @@ static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom) int ret; avio_seek(pb, -8, SEEK_CUR); atom.size += 8; + c->alpha_item_id = -1; ret = mov_read_default(c, pb, atom); if (ret < 0) return ret; @@ -4767,6 +4768,12 @@ static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom) ret = avif_add_stream(c, c->primary_item_id); if (ret) return ret; + if (c->alpha_item_id != -1) { + // Add a stream for the Alpha plane. + ret = avif_add_stream(c, c->alpha_item_id); + if (ret) + return ret; + } // For still AVIF images, the meta box contains all the // necessary information that would generally be provided by the // moov box. So simply mark that we have found the moov box so @@ -7556,6 +7563,64 @@ static int mov_read_pitm(MOVContext *c, AVIOContext *pb, MOVAtom atom) return atom.size; } +static int mov_read_iprp(MOVContext *c, AVIOContext *pb, MOVAtom atom) +{ + int entry_count, size, version, flags; + int index = 0, auxC_alpha_index = -1; + size = avio_rb32(pb); + if (avio_rl32(pb) != MKTAG('i','p','c','o')) + return AVERROR_INVALIDDATA; + size -= 8; + while (size > 0) { + int sub_size, sub_type; + sub_size = avio_rb32(pb); + sub_type = avio_rl32(pb); + sub_size -= 8; + size -= sub_size + 8; + index++; + if (sub_type == MKTAG('a','u','x','C')) { + const char *expected_alpha_urn = "urn:mpeg:mpegB:cicp:systems:auxiliary:alpha"; + avio_rb32(pb); // version & flags. + sub_size -= 4; + if (sub_size >= strlen(expected_alpha_urn) + 1) { + char alpha_urn[44]; + avio_read(pb, alpha_urn, 44); + sub_size -= 44; + if (!strncmp(alpha_urn, expected_alpha_urn, 44)) { + auxC_alpha_index = index; + } + } + } + avio_skip(pb, sub_size); + } + if (auxC_alpha_index == -1) + return atom.size; + + // ipma. + size = avio_rb32(pb); + if (avio_rl32(pb) != MKTAG('i','p','m','a')) + return AVERROR_INVALIDDATA; + version = avio_r8(pb); + flags = avio_rb24(pb); + entry_count = avio_rb32(pb); + for (int i = 0; i < entry_count; i++) { + int item_id, association_count; + item_id = (version < 1) ? avio_rb16(pb) : avio_rb32(pb); + association_count = avio_r8(pb); + for (int j = 0; j < association_count; j++) { + int property_index; + if (flags & 1) + property_index = avio_rb16(pb) & 0x7fff; + else + property_index = avio_r8(pb) & 0x7f; + if (property_index == auxC_alpha_index) { + c->alpha_item_id = item_id; + } + } + } + return atom.size; +} + static int mov_read_iloc(MOVContext *c, AVIOContext *pb, MOVAtom atom) { int version, offset_size, length_size, base_offset_size, index_size; @@ -7732,6 +7797,7 @@ static const MOVParseTableEntry mov_default_parse_table[] = { { MKTAG('i','l','o','c'), mov_read_iloc }, { MKTAG('p','c','m','C'), mov_read_pcmc }, /* PCM configuration box */ { MKTAG('p','i','t','m'), mov_read_pitm }, +{ MKTAG('i','p','r','p'), mov_read_iprp }, { 0, NULL } };