Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Vignesh Venkatasubramanian <vigneshv-at-google.com@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: Vignesh Venkatasubramanian <vigneshv@google.com>
Subject: [FFmpeg-devel] [PATCH 2/2] avformat/mov: Support parsing of still AVIF Alpha Channel
Date: Thu, 30 Jun 2022 14:04:34 -0700
Message-ID: <20220630210434.1551769-2-vigneshv@google.com> (raw)
In-Reply-To: <20220630210434.1551769-1-vigneshv@google.com>

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 <vigneshv@google.com>
---
 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 }
 };
 
-- 
2.37.0.rc0.161.g10f37bed90-goog

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

  reply	other threads:[~2022-06-30 21:05 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-30 21:04 [FFmpeg-devel] [PATCH 1/2] avformat/mov: Rework the AVIF parser to handle multiple items Vignesh Venkatasubramanian
2022-06-30 21:04 ` Vignesh Venkatasubramanian [this message]
2022-07-02  9:34   ` [FFmpeg-devel] [PATCH 2/2] avformat/mov: Support parsing of still AVIF Alpha Channel Anton Khirnov
2022-07-02 16:32     ` Vignesh Venkatasubramanian
2022-07-02 19:34       ` Jan Ekström
2022-07-02 21:15         ` Vignesh Venkatasubramanian
2022-07-03 12:17           ` Jan Ekström
2022-07-03 16:52             ` Vignesh Venkatasubramanian
2022-07-05 16:53           ` Anton Khirnov
2022-07-12 15:17             ` Vignesh Venkatasubramanian
2022-07-11 22:25 ` [FFmpeg-devel] [PATCH 1/2] avformat/mov: Rework the AVIF parser to handle multiple items James Zern
2022-07-13 16:11   ` Vignesh Venkatasubramanian
2022-07-13 16:12   ` Vignesh Venkatasubramanian
2022-07-22 18:20     ` Vignesh Venkatasubramanian
2022-07-26 19:02       ` James Zern
2022-07-27 16:12         ` Vignesh Venkatasubramanian
2022-07-27 19:37           ` James Zern
2022-07-27 19:40           ` Andreas Rheinhardt
2022-07-28 18:25             ` Vignesh Venkatasubramanian
2022-07-28 18:25             ` Vignesh Venkatasubramanian
2022-08-02 16:54               ` James Zern
2022-08-09 20:20                 ` James Zern
2022-07-27 16:12         ` Vignesh Venkatasubramanian

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220630210434.1551769-2-vigneshv@google.com \
    --to=vigneshv-at-google.com@ffmpeg.org \
    --cc=ffmpeg-devel@ffmpeg.org \
    --cc=vigneshv@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git