From patchwork Fri Apr 22 18:59:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vignesh Venkat X-Patchwork-Id: 35375 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b9e:b0:7d:cfb5:dc7c with SMTP id b30csp551732pzh; Fri, 22 Apr 2022 11:59:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwI5kzO+XnkwnfHUNdQVc6U8anincxliAR06OEvkhgLDheVVWqCsZzlTYtBqZGeRmr0tfHB X-Received: by 2002:a17:906:fc05:b0:6e8:89e7:52e7 with SMTP id ov5-20020a170906fc0500b006e889e752e7mr5384350ejb.360.1650653965924; Fri, 22 Apr 2022 11:59:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650653965; cv=none; d=google.com; s=arc-20160816; b=FPd+3rexD3m/u65uOd5HrYWlqhKj83jgfrkb/FnbCCB9XTMVtgGpuujgWPdTy1xjy/ rbtGpC4+qbRhJRYzfyeP7HdWpmVtyP9xZ9/lnmvjkyueidSpKLcvDy4eh8BCV1f30iZY /fVE8gc1p9KcQXzUcbPvLUlFaWpbVnoyOng6gnReGGAy2X+fPUHt6DTYyfLT1Ee7OXGI W8nBqPTEnjVqWVZjyiYecshHTv4lGuPuiY9j7BYXZKktT4PxJt8/QYWgBcUUb5QPl0yl gMVuiCBuAQch6ECslcrwhyvl+dOIg4AJPe2ppY4KiP9ffbKnkBPa1lDUsKMBFjMFLmd1 Sh9w== 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=zWD0ndygo2zQkO1QehdzW2pTlGHeySrtxdqLhx1zb9U=; b=T6CSMKQLqHk3pQ/w3s/utcY/ih2b01nBYj+VMa0SghEbJvyrrftSyo+BrEwec64GGL KnoKwC8r5MskpE/ytpse44v//SjX4PNLpsDJSZVW0ImatPlg2v0V9oChXTQ7iAILPBoy 5Ppf1iZ6cI0ZKYCY6587DFZIj80UQ2uSG/8QXheo+CdXZrflL7CV+6v3dzvf6uDbDLyr vF6mr7uVQHtleGB72YUA8AV61Q6a6j/GwX5Jf7EpIuWaSKHeKp/TC+oSgj6bPjdGdy5G ESIIvNewzp3Inv5KPhX1IOOhXEkSkdXthY8q4G58pSaVN9q+J+wfDLEDMrB+GaNLmxK8 D/1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@google.com header.s=20210112 header.b=WUJiHfJV; 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 r10-20020a1709064d0a00b006df76385c1esi7549296eju.190.2022.04.22.11.59.25; Fri, 22 Apr 2022 11:59:25 -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=WUJiHfJV; 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 E675D68B393; Fri, 22 Apr 2022 21:59:22 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 14F6168B21B for ; Fri, 22 Apr 2022 21:59:17 +0300 (EEST) Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-2ef53391dbaso78127027b3.11 for ; Fri, 22 Apr 2022 11:59:16 -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=Bmoh8UdEC1Q0RujqjGEkQ/+mLIm6JZ8HNCGoGglgx9o=; b=WUJiHfJVDEu1bSMm6b/OzK5FUKyt+K2zkTWYhp0GQmO0WSrxBZq9agjee25BrkTlvK DWaMnLuBe7PZtIPQjYiNiin1opRrW1h4AWRrkXWpzdF7PGYY2EPZe2YLVW2Bi22D+dQd 39VVAw6NA8q6xh3O1zw6fd5XArNznO+022hp3m9NrBD0lh4tBaRAKMfC8hDq4mLZEmPc KWIfMbhUAdo3z4DvfZrqr2PQHFm65yOpUoHUK8dYZWukv+H9kWUCbxoTlgMxPoz7abuk 1HX84I9wMDgHva8g7ILds/xh7cgwhNEGUw02rkAkMW/Ki5ZqiWMKxQJp/kDE5HXwxZCF 1XOQ== 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=Bmoh8UdEC1Q0RujqjGEkQ/+mLIm6JZ8HNCGoGglgx9o=; b=uEzHj+7+r1hANvRp4Jw/lYXKK7dUlDsa6c/elhnf/g927DDQGhi/MaM5KwHN+8kxpk 7bTQFuKS19IAoRwqHGD4+bykAB1FdIj8eS4nh6Mg3bhR3EnktrAJTqUjBt5V7Psqfxxn pvb+eXWBdHsgUltrYMqxXmP4pJ54twm0lkmJrfF0xeZ16pzr+xmgqgQxjh/u27iQpurB JFF1RLvlzdy2FG0wpFoKPF5EEgp6GSvQbV6a1M0mT80CAanKvBQcd8V7rkWOZoxQDiJY B8K5qwV0Lgc8gFUMUKF39UDdsfEQhsVHDT1e23SYA+jRsQ5vw7L/K+WWe8SGuJZJ6CwQ LudA== X-Gm-Message-State: AOAM530e22kDrLGA3z7Lfpv21SXmxwI0v0FmZYREFqc9LnZtQCCQugG7 GpznYbnDImc0ZGZwX7PrCJEM51UsTDp2B+dpvwGM5ZPsYaHANYUJOdwq4Z2S/pN3Nv3sSpv7OA5 XZ0xhExjU4CbHy0lsyQeAZbGye4oKAhFOAMl6X8HMnFL7ahV9a1U/lo7jYafvvvwVPGUK X-Received: from vigneshv3.mtv.corp.google.com ([2620:0:1000:2511:5019:abb5:2603:e59]) (user=vigneshv job=sendgmr) by 2002:a81:4ed7:0:b0:2f5:d7d7:5afc with SMTP id c206-20020a814ed7000000b002f5d7d75afcmr4351490ywb.65.1650653955215; Fri, 22 Apr 2022 11:59:15 -0700 (PDT) Date: Fri, 22 Apr 2022 11:59:11 -0700 In-Reply-To: Message-Id: <20220422185911.3086062-1-vigneshv@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog From: Vignesh Venkatasubramanian To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH] avformat/mov: Add support for still image AVIF parsing 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: rl23VHamrMTP Add support for parsing AVIF still images. This patches supports AVIF still images that have exactly 1 item (i.e.) no alpha channel. Essentially, we will have to parse the "iloc" box and populate the mov index. With this patch, we can decode still AVIF images like so: ffmpeg -i image.avif image.png Partially fixes trac ticket #7621 Signed-off-by: Vignesh Venkatasubramanian --- libavformat/isom.h | 1 + libavformat/mov.c | 141 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) diff --git a/libavformat/isom.h b/libavformat/isom.h index 5caf42b15d..02d681e3ae 100644 --- a/libavformat/isom.h +++ b/libavformat/isom.h @@ -315,6 +315,7 @@ typedef struct MOVContext { int have_read_mfra_size; uint32_t mfra_size; uint32_t max_stts_delta; + int is_still_picture_avif; } MOVContext; int ff_mp4_read_descr_len(AVIOContext *pb); diff --git a/libavformat/mov.c b/libavformat/mov.c index 6c847de164..39feb9fba6 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -1136,6 +1136,7 @@ static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom) c->isom = 1; av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type); av_dict_set(&c->fc->metadata, "major_brand", type, 0); + c->is_still_picture_avif = !strncmp(type, "avif", 4); minor_ver = avio_rb32(pb); /* minor version */ av_dict_set_int(&c->fc->metadata, "minor_version", minor_ver, 0); @@ -7430,6 +7431,145 @@ static int mov_read_SAND(MOVContext *c, AVIOContext *pb, MOVAtom atom) return 0; } +static int rb_size(AVIOContext *pb, uint64_t* value, int size) +{ + if (size == 0) + *value = 0; + else if (size == 1) + *value = avio_r8(pb); + else if (size == 2) + *value = avio_rb16(pb); + else if (size == 4) + *value = avio_rb32(pb); + else if (size == 8) + *value = avio_rb64(pb); + else + return -1; + return size; +} + +static int mov_read_iloc(MOVContext *c, AVIOContext *pb, MOVAtom atom) +{ + int version, offset_size, length_size, base_offset_size, index_size; + int item_count, extent_count; + uint64_t base_offset, extent_offset, extent_length; + uint8_t value; + AVStream *st; + MOVStreamContext *sc; + + if (!c->is_still_picture_avif) { + // * For non-avif, we simply ignore the iloc box. + // * For animated avif, we don't care about the iloc box as all the + // necessary information can be found in the moov box. + return 0; + } + + if (c->fc->nb_streams) { + av_log(c->fc, AV_LOG_INFO, "Duplicate iloc box found\n"); + return 0; + } + + st = avformat_new_stream(c->fc, NULL); + if (!st) + return AVERROR(ENOMEM); + st->id = c->fc->nb_streams; + sc = av_mallocz(sizeof(MOVStreamContext)); + if (!sc) + return AVERROR(ENOMEM); + + st->priv_data = sc; + st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; + st->codecpar->codec_id = AV_CODEC_ID_AV1; + sc->ffindex = st->index; + c->trak_index = st->index; + st->avg_frame_rate.num = st->avg_frame_rate.den = 1; + st->time_base.num = st->time_base.den = 1; + st->nb_frames = 1; + sc->time_scale = 1; + sc = st->priv_data; + sc->pb = c->fc->pb; + sc->pb_is_copied = 1; + + version = avio_r8(pb); + avio_rb24(pb); // flags. + + value = avio_r8(pb); + offset_size = (value >> 4) & 0xF; + length_size = value & 0xF; + value = avio_r8(pb); + base_offset_size = (value >> 4) & 0xF; + index_size = !version ? 0 : (value & 0xF); + if (index_size) { + av_log(c->fc, AV_LOG_ERROR, "iloc: index_size != 0 not supported.\n"); + return AVERROR_PATCHWELCOME; + } + item_count = (version < 2) ? avio_rb16(pb) : avio_rb32(pb); + if (item_count > 1) { + // For still AVIF images, we only support one item. Second item will + // generally be found for AVIF images with alpha channel. We don't + // support them as of now. + av_log(c->fc, AV_LOG_ERROR, "iloc: item_count > 1 not supported.\n"); + return AVERROR_PATCHWELCOME; + } + + // Populate the necessary fields used by mov_build_index. + sc->stsc_count = item_count; + sc->stsc_data = av_malloc_array(item_count, sizeof(*sc->stsc_data)); + if (!sc->stsc_data) + return AVERROR(ENOMEM); + sc->stsc_data[0].first = 1; + sc->stsc_data[0].count = 1; + sc->stsc_data[0].id = 1; + sc->chunk_count = item_count; + sc->chunk_offsets = + av_malloc_array(item_count, sizeof(*sc->chunk_offsets)); + if (!sc->chunk_offsets) + return AVERROR(ENOMEM); + sc->sample_count = item_count; + sc->sample_sizes = + av_malloc_array(item_count, sizeof(*sc->sample_sizes)); + if (!sc->sample_sizes) + return AVERROR(ENOMEM); + sc->stts_count = item_count; + sc->stts_data = av_malloc_array(item_count, sizeof(*sc->stts_data)); + if (!sc->stts_data) + return AVERROR(ENOMEM); + sc->stts_data[0].count = 1; + // Not used for still images. But needed by mov_build_index. + sc->stts_data[0].duration = 0; + + for (int i = 0; i < item_count; i++) { + (version < 2) ? avio_rb16(pb) : avio_rb32(pb); // item_id; + if (version > 0) + avio_rb16(pb); // construction_method. + avio_rb16(pb); // data_reference_index. + if (rb_size(pb, &base_offset, base_offset_size) < 0) + return AVERROR_INVALIDDATA; + extent_count = avio_rb16(pb); + if (extent_count > 1) { + // For still AVIF images, we only support one extent item. + av_log(c->fc, AV_LOG_ERROR, "iloc: extent_count > 1 not supported.\n"); + return AVERROR_PATCHWELCOME; + } + for (int j = 0; j < extent_count; j++) { + if (rb_size(pb, &extent_offset, offset_size) < 0 || + rb_size(pb, &extent_length, length_size) < 0) + return AVERROR_INVALIDDATA; + sc->sample_sizes[0] = extent_length; + sc->chunk_offsets[0] = base_offset + extent_offset; + } + } + + mov_build_index(c, st); + + // For still AVIF images, the iloc 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 that parsing can continue. + c->found_moov = 1; + + return atom.size; +} + static const MOVParseTableEntry mov_default_parse_table[] = { { MKTAG('A','C','L','R'), mov_read_aclr }, { MKTAG('A','P','R','G'), mov_read_avid }, @@ -7532,6 +7672,7 @@ static const MOVParseTableEntry mov_default_parse_table[] = { { MKTAG('k','i','n','d'), mov_read_kind }, { MKTAG('S','A','3','D'), mov_read_SA3D }, /* ambisonic audio box */ { MKTAG('S','A','N','D'), mov_read_SAND }, /* non diegetic audio box */ +{ MKTAG('i','l','o','c'), mov_read_iloc }, { 0, NULL } };