From patchwork Mon Jan 22 22:26:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 45730 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:120f:b0:199:de12:6fa6 with SMTP id v15csp263031pzf; Mon, 22 Jan 2024 14:26:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IFbxQmH7dURNE6xDkjIxEHz6kx1uZBo9R1W6TB6GhHWuP0JFJ16cZYJ3kabu2TPSh5g972r X-Received: by 2002:a05:600c:1f94:b0:40c:3555:e230 with SMTP id je20-20020a05600c1f9400b0040c3555e230mr2899485wmb.46.1705962411475; Mon, 22 Jan 2024 14:26:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1705962411; cv=none; d=google.com; s=arc-20160816; b=ZQqh4xkJXCKjD4Hk1wwM9hdEuLbuZzc54ZcSfH+QkYuiJYK6enaMp0WiLSEEDJ+tC7 7kpcIJ03sp1EH0Z3Tm9fkF3Vhs9T/Rdw2irJLM1vmw0pQ1GSjblFbET07hzIe9/wXpkw pQB1Fgxt7MgiMpgPCrUgttf+CV0/HJrNzqZXu8minWv7UtSQWNHfDBkn7oIvCZW6EQl+ OwH9AL1fVZWVbN3XZazYq7O2H/PDz4Me5tmEDTXt6YeDTX2vDJLGteLA7OtCBf1pzTjd kUeAx50ANAPV7r1T/9jeW+bFfRR/c4g7pyyNun5YYKnqMggTtKfwGEML4WJTQfIp1ama 0bJQ== 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:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=7Xc78dGvPvmDpMzYg6jq+sQ80IKp4QnqhZ6R6zYz460=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=IQJaOs7pb4Aeslo4MhCC3kybIkN1Y27ska13/WAsSniC8Y0zApmT3W6fwyHewM98JU sTDrqZFaZgk+ATWxlDACvgi1cRUheGXUMm3RGEv5twPWZDyBPDKBdMac4zMYimS/Z+52 HFPNQ8T3Rwk8Iv2mNgXoT1mA6C5r5LfDJmAFnjifklYxcQiDUaiobv/MRZdEGEVY7nP8 KrGCFcPPU4xzd6zUNFuCBsHkR+zpy3cWneVc4S8JvbN5rnxa5vyTOQ525MQeTGwZebE8 zam1/dB5SvQ28naygiEmrz8PJJ5iC762Wje6zW4UE20f2Z8fPbfu8nlfFKB8YTA+Re0Z x2hw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=mpCRsYNP; 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 fd12-20020a056402388c00b0055c45aa1380si1046838edb.431.2024.01.22.14.26.50; Mon, 22 Jan 2024 14:26:51 -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=20230601 header.b=mpCRsYNP; 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 ACA9A68CD10; Tue, 23 Jan 2024 00:26:47 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7FB0368CA64 for ; Tue, 23 Jan 2024 00:26:40 +0200 (EET) Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-5ce74ea4bf2so2334676a12.0 for ; Mon, 22 Jan 2024 14:26:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705962397; x=1706567197; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=VUUYz5qC5f01DGyKlbiH2KhNXw9+WICh/XbHpeKAGn8=; b=mpCRsYNPELO2DyjPXQkbyiWYEEQ4kiDOsLJUPu7tXhS7z+tKFJvzbTGIq8eD8+4AQP GkFZAG9OmEgwcFnJKRgmIashOjI9rK5j7tEhFeD60QdIiDN/bbTXoFCBmv65wF254rSl upfwhr1msGhJl6u679m+mPuB9V1adGVK3SaoCUlZa4UYfXE7XdWZBo9aYSOfTJBYDUo3 wZG9/krnBm0wBosYbbHHxZv9eVRD3QMrfiho7iPW4+QqQO5L0H6z58BKa2PR9XhR3S4/ I7DfBXVy0un6cVqJJqp2cYaQ21hZISOZHdlJrNeUbMJSel7ctWTuHuLc8YPcmazifZa+ mgjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705962397; x=1706567197; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VUUYz5qC5f01DGyKlbiH2KhNXw9+WICh/XbHpeKAGn8=; b=cIRLWH01it05z9VRxVV8qKAzqe5wmNvuhTICD9Pzwcm0inxIYLtErPacSd7Upg47+w 1IcNlwalvE8vlfvQKQYsPAxWd+q+Qg/pHALPDArsSPYs47bjR9Wjsz9QQhetVvQyOnSq jqpsquuMPItusYCP8PQLXzSE/zPKErJ8CqFOwzycpa/wvfIKXnP8MU/w7PTax9mxfMZo WoXOG22gU8lLbgkqS/wWlO25qs0ESZQ3oacXjJ68rCC1KVtBCAbpKS7YPxGHcfGbsfZY lAh+mOLUQECdYZCYeZNcQecL0LpkaQtYoEPZz9aRz+u73sVe5lYzEnAiLdpAEb+PPXmt 32tQ== X-Gm-Message-State: AOJu0Yxg5k0V8gkoLXOFMGmAaRm02W6G/wmoXTSXHMz8w2yO3H7Sadd5 +NZwoUW/wfnaKxwdbxQOrPM94hEEyWC3LSxCj6/fbmh7RUQtpA6hflpXKZxF X-Received: by 2002:a05:6a20:158d:b0:19c:51f2:3ff1 with SMTP id h13-20020a056a20158d00b0019c51f23ff1mr1208868pzj.11.1705962397020; Mon, 22 Jan 2024 14:26:37 -0800 (PST) Received: from localhost.localdomain (host197.190-225-105.telecom.net.ar. [190.225.105.197]) by smtp.gmail.com with ESMTPSA id c6-20020a633506000000b005cfbd89fd15sm5208106pga.20.2024.01.22.14.26.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 14:26:36 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Mon, 22 Jan 2024 19:26:32 -0300 Message-ID: <20240122222632.12685-1-jamrial@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240122180902.4555-2-jamrial@gmail.com> References: <20240122180902.4555-2-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/3 v2] avformat/mov: add support for tile HEIF still images 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: Qs4kmHsSLdFH Export each tile as its own stream, and the tiling information as a Stream Group of type TILE_GRID. This also enables exporting other stream items like thumbnails, which may be present in non tiled HEIF images too. For those, the primary stream will be tagged with the default disposition. Based on a patch by Swaraj Hota Signed-off-by: James Almer --- Fixed a bug setting tile dimensions in the stream group. Also, the grid info can now be read from mdat and not just idat. libavcodec/packet.h | 9 ++ libavformat/avformat.h | 6 + libavformat/isom.h | 8 +- libavformat/mov.c | 318 ++++++++++++++++++++++++++++++++++++----- 4 files changed, 304 insertions(+), 37 deletions(-) diff --git a/libavcodec/packet.h b/libavcodec/packet.h index 2c57d262c6..48ca799334 100644 --- a/libavcodec/packet.h +++ b/libavcodec/packet.h @@ -323,6 +323,15 @@ enum AVPacketSideDataType { */ AV_PKT_DATA_IAMF_RECON_GAIN_INFO_PARAM, + /** + * Tile info for image reconstruction, e.g HEIF. + * @code + * u32le tile number in row major order [0..nb_tiles-1] + * u32le nb_tiles + * @endcode + */ + AV_PKT_DATA_TILE_INFO, + /** * The number of side data types. * This is not part of the public API/ABI in the sense that it may diff --git a/libavformat/avformat.h b/libavformat/avformat.h index ab9a3fc6be..cf4e72e11d 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -811,6 +811,12 @@ typedef struct AVIndexEntry { * The video stream contains still images. */ #define AV_DISPOSITION_STILL_IMAGE (1 << 20) +/** + * The video stream is intended to be merged with another stream before + * presentation. + * Used for example to signal the stream contains a tile from a HEIF grid. + */ +#define AV_DISPOSITION_TILE (1 << 21) /** * @return The AV_DISPOSITION_* flag corresponding to disp or a negative error diff --git a/libavformat/isom.h b/libavformat/isom.h index 2cf456fee1..cd5478b61c 100644 --- a/libavformat/isom.h +++ b/libavformat/isom.h @@ -267,10 +267,10 @@ typedef struct HEIFItem { int item_id; int64_t extent_length; int64_t extent_offset; - int64_t size; int width; int height; int type; + int is_idat_relative; } HEIFItem; typedef struct MOVContext { @@ -335,6 +335,12 @@ typedef struct MOVContext { int cur_item_id; HEIFItem *heif_info; int heif_info_size; + int grid_item_id; + int thmb_item_id; + int16_t *tile_id_list; + int nb_tiles; + uint8_t *idat_buf; + int64_t idat_size; int interleaved_read; } MOVContext; diff --git a/libavformat/mov.c b/libavformat/mov.c index 5cb907e120..3b7d3e4285 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -184,6 +184,30 @@ static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len, return p - dst; } +static AVStream *get_curr_st(MOVContext *c) +{ + AVStream *st = NULL; + + if (c->fc->nb_streams < 1) + return NULL; + + for (int i = 0; i < c->heif_info_size; i++) { + HEIFItem *item = &c->heif_info[i]; + + if (!item->st) + continue; + if (item->st->id != c->cur_item_id) + continue; + + st = item->st; + break; + } + if (!st) + st = c->fc->streams[c->fc->nb_streams-1]; + + return st; +} + static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len) { AVStream *st; @@ -1766,9 +1790,9 @@ static int mov_read_colr(MOVContext *c, AVIOContext *pb, MOVAtom atom) uint16_t color_primaries, color_trc, color_matrix; int ret; - if (c->fc->nb_streams < 1) + st = get_curr_st(c); + if (!st) return 0; - st = c->fc->streams[c->fc->nb_streams - 1]; ret = ffio_read_size(pb, color_parameter_type, 4); if (ret < 0) @@ -2116,9 +2140,9 @@ static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom) AVStream *st; int ret; - if (c->fc->nb_streams < 1) + st = get_curr_st(c); + if (!st) return 0; - st = c->fc->streams[c->fc->nb_streams-1]; if ((uint64_t)atom.size > (1<<30)) return AVERROR_INVALIDDATA; @@ -4928,12 +4952,10 @@ static int heif_add_stream(MOVContext *c, HEIFItem *item) st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; st->codecpar->codec_id = mov_codec_id(st, item->type); 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; @@ -7761,15 +7783,75 @@ static int mov_read_pitm(MOVContext *c, AVIOContext *pb, MOVAtom atom) return atom.size; } +static int mov_read_idat(MOVContext *c, AVIOContext *pb, MOVAtom atom) +{ + av_log(c->fc, AV_LOG_TRACE, "idat: size %"PRId64"\n", atom.size); + + c->idat_buf = av_malloc(atom.size); + if (!c->idat_buf) + return AVERROR(ENOMEM); + + if (avio_read(pb, c->idat_buf, atom.size) != atom.size) { + av_freep(&c->idat_buf); + return AVERROR_INVALIDDATA; + } + c->idat_size = atom.size; + + return 0; +} + +static int read_image_grid(AVFormatContext *s, AVTileGrid *tile_grid, HEIFItem *item) { + MOVContext *c = s->priv_data; + FFIOContext b; + AVIOContext *pb = &b.pub; + int64_t pos = avio_tell(s->pb); + uint8_t flags; + + if (item->is_idat_relative) { + if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) { + av_log(c->fc, AV_LOG_INFO, "grid box in mdat with non seekable input\n"); + return AVERROR_PATCHWELCOME; + } + ffio_init_read_context(&b, c->idat_buf, c->idat_size); + } else if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) { + avio_seek(s->pb, item->extent_offset, SEEK_SET); + pb = s->pb; + } + + avio_r8(pb); /* version */ + flags = avio_r8(pb); + + tile_grid->tile_rows = avio_r8(pb) + 1; + tile_grid->tile_cols = avio_r8(pb) + 1; + /* actual width and height of output image */ + tile_grid->output_width = (flags & 1) ? avio_rb32(pb) : avio_rb16(pb); + tile_grid->output_height = (flags & 1) ? avio_rb32(pb) : avio_rb16(pb); + + tile_grid->tile_width = av_calloc(tile_grid->tile_rows * tile_grid->tile_cols, + sizeof(*tile_grid->tile_width)); + tile_grid->tile_height = av_calloc(tile_grid->tile_rows * tile_grid->tile_cols, + sizeof(*tile_grid->tile_height)); + if (!tile_grid->tile_width || !tile_grid->tile_width) + return AVERROR(ENOMEM); + + av_log(c->fc, AV_LOG_TRACE, "grid: grid_rows %d grid_cols %d output_width %d output_height %d\n", + tile_grid->tile_rows, tile_grid->tile_cols, tile_grid->output_width, tile_grid->output_height); + + if (!item->is_idat_relative && (s->pb->seekable & AVIO_SEEKABLE_NORMAL)) + avio_seek(s->pb, pos, SEEK_SET); + + return 0; +} + 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; + int64_t base_offset, extent_offset, extent_length; uint8_t value; if (c->found_iloc) { - av_log(c->fc, AV_LOG_INFO, "Duplicate iloc box found\n"); + av_log(c->fc, AV_LOG_WARNING, "Duplicate iloc box found\n"); return 0; } @@ -7811,6 +7893,7 @@ static int mov_read_iloc(MOVContext *c, AVIOContext *pb, MOVAtom atom) avio_rb16(pb); // data_reference_index. if (rb_size(pb, &base_offset, base_offset_size) < 0) return AVERROR_INVALIDDATA; + av_log(c->fc, AV_LOG_TRACE, "iloc: base_offset %"PRId64"\n", base_offset); extent_count = avio_rb16(pb); if (extent_count > 1) { // For still AVIF images, we only support one extent item. @@ -7821,6 +7904,8 @@ static int mov_read_iloc(MOVContext *c, AVIOContext *pb, MOVAtom atom) if (rb_size(pb, &extent_offset, offset_size) < 0 || rb_size(pb, &extent_length, length_size) < 0) return AVERROR_INVALIDDATA; + if (offset_type == 1) + c->heif_info[i].is_idat_relative = 1; c->heif_info[i].extent_length = extent_length; c->heif_info[i].extent_offset = base_offset + extent_offset; av_log(c->fc, AV_LOG_TRACE, "iloc: item_idx %d, offset_type %d, " @@ -7859,10 +7944,6 @@ static int mov_read_infe(MOVContext *c, AVIOContext *pb, MOVAtom atom) av_log(c->fc, AV_LOG_TRACE, "infe: item_id %d, item_type %s, item_name %s\n", item_id, av_fourcc2str(item_type), item_name); - // Skip all but the primary item until support is added - if (item_id != c->primary_item_id) - return 0; - if (size > 0) avio_skip(pb, size); @@ -7876,6 +7957,9 @@ static int mov_read_infe(MOVContext *c, AVIOContext *pb, MOVAtom atom) if (ret < 0) return ret; break; + case MKTAG('g','r','i','d'): + c->grid_item_id = item_id; + break; default: av_log(c->fc, AV_LOG_TRACE, "infe: ignoring item_type %s\n", av_fourcc2str(item_type)); break; @@ -7924,6 +8008,59 @@ static int mov_read_iref(MOVContext *c, AVIOContext *pb, MOVAtom atom) return mov_read_default(c, pb, atom); } +static int mov_read_dimg(MOVContext *c, AVIOContext *pb, MOVAtom atom) +{ + int entries, i; + int from_item_id = avio_rb16(pb); + + if (c->grid_item_id < 0) { + av_log(c->fc, AV_LOG_ERROR, "Missing grid information\n"); + return AVERROR_INVALIDDATA; + } + if (from_item_id != c->grid_item_id) { + avpriv_request_sample(c->fc, "Derived item of type other than 'grid'"); + return AVERROR_PATCHWELCOME; + } + entries = avio_rb16(pb); + c->tile_id_list = av_malloc_array(entries, sizeof(*c->tile_id_list)); + if (!c->tile_id_list) + return AVERROR(ENOMEM); + /* 'to' item ids */ + for (i = 0; i < entries; i++) + c->tile_id_list[i] = avio_rb16(pb); + c->nb_tiles = entries; + + av_log(c->fc, AV_LOG_TRACE, "dimg: from_item_id %d, entries %d\n", + from_item_id, entries); + + return 0; +} + +static int mov_read_thmb(MOVContext *c, AVIOContext *pb, MOVAtom atom) +{ + int entries; + int to_item_id, from_item_id = avio_rb16(pb); + + entries = avio_rb16(pb); + if (entries > 1) { + avpriv_request_sample(c->fc, "More than one thmb entry"); + return AVERROR_PATCHWELCOME; + } + /* 'to' item ids */ + to_item_id = avio_rb16(pb); + + if (to_item_id != c->primary_item_id || + to_item_id != c->grid_item_id) + return 0; + + c->thmb_item_id = from_item_id; + + av_log(c->fc, AV_LOG_TRACE, "thmb: from_item_id %d, entries %d\n", + from_item_id, entries); + + return 0; +} + static int mov_read_ispe(MOVContext *c, AVIOContext *pb, MOVAtom atom) { uint32_t width, height; @@ -7932,15 +8069,17 @@ static int mov_read_ispe(MOVContext *c, AVIOContext *pb, MOVAtom atom) width = avio_rb32(pb); height = avio_rb32(pb); - av_log(c->fc, AV_LOG_TRACE, "ispe: item_id %d, width %u, height %u\n", + av_log(c->fc, AV_LOG_TRACE, "ispe: cur_item_id %d, width %u, height %u\n", c->cur_item_id, width, height); for (int i = 0; i < c->heif_info_size; i++) { - if (c->heif_info[i].item_id == c->cur_item_id) { - c->heif_info[i].width = width; - c->heif_info[i].height = height; - break; - } + HEIFItem *item = &c->heif_info[i]; + if (item->item_id != c->cur_item_id) + continue; + + item->width = width; + item->height = height; + break; } return 0; @@ -8031,10 +8170,6 @@ static int mov_read_iprp(MOVContext *c, AVIOContext *pb, MOVAtom atom) av_log(c->fc, AV_LOG_TRACE, "ipma: property_index %d, item_id %d, item_type %s\n", index + 1, item_id, av_fourcc2str(ref->type)); - // Skip properties referencing items other than the primary item until support is added - if (item_id != c->primary_item_id) - continue; - c->cur_item_id = item_id; ret = mov_read_default(c, &ref->b.pub, @@ -8162,6 +8297,9 @@ static const MOVParseTableEntry mov_default_parse_table[] = { { MKTAG('p','c','m','C'), mov_read_pcmc }, /* PCM configuration box */ { MKTAG('p','i','t','m'), mov_read_pitm }, { MKTAG('e','v','c','C'), mov_read_glbl }, +{ MKTAG('d','i','m','g'), mov_read_dimg }, +{ MKTAG('t','h','m','b'), mov_read_thmb }, +{ MKTAG('i','d','a','t'), mov_read_idat }, { MKTAG('i','r','e','f'), mov_read_iref }, { MKTAG('i','s','p','e'), mov_read_ispe }, { MKTAG('i','p','r','p'), mov_read_iprp }, @@ -8664,6 +8802,8 @@ static int mov_read_close(AVFormatContext *s) av_freep(&mov->aes_decrypt); av_freep(&mov->chapter_tracks); av_freep(&mov->heif_info); + av_freep(&mov->idat_buf); + av_freep(&mov->tile_id_list); return 0; } @@ -8803,6 +8943,102 @@ fail: return ret; } +static int mov_parse_tiles(AVFormatContext *s) +{ + MOVContext *mov = s->priv_data; + AVStreamGroup *stg = avformat_stream_group_create(s, AV_STREAM_GROUP_PARAMS_TILE_GRID, NULL); + AVTileGrid *tile_grid; + int err; + + if (!stg) + return AVERROR(ENOMEM); + + tile_grid = stg->params.tile_grid; + + av_assert0(mov->grid_item_id >= 0); + for (int i = 0; i < mov->heif_info_size; i++) { + HEIFItem *item = &mov->heif_info[i]; + + if (item->item_id != mov->grid_item_id) + continue; + err = read_image_grid(s, tile_grid, item); + if (err < 0) + return err; + stg->id = item->item_id; + break; + } + + for (int i = 0; i < mov->nb_tiles; i++) { + int tile_id = mov->tile_id_list[i]; + + for (int j = 0; j < mov->heif_info_size; j++) { + HEIFItem *item = &mov->heif_info[j]; + AVStream *st = item->st; + AVPacketSideData *sd; + MOVStreamContext *sc; + + if (item->item_id != tile_id) + continue; + if (!st) { + av_log(s, AV_LOG_ERROR, "HEIF tile %d doesn't reference a stream\n", tile_id); + return AVERROR_INVALIDDATA; + } + + tile_grid->tile_width[i] = item->width; + tile_grid->tile_height[i] = item->height; + st->codecpar->width = item->width; + st->codecpar->height = item->height; + + err = avformat_stream_group_add_stream(stg, st); + if (err == AVERROR(EEXIST)) + return AVERROR_INVALIDDATA; + else if (err < 0) + return err; + + sc = st->priv_data; + sc->sample_sizes[0] = item->extent_length; + sc->chunk_offsets[0] = item->extent_offset; + + st->disposition |= AV_DISPOSITION_TILE; + + mov_build_index(mov, st); + + sd = av_packet_side_data_new(&st->codecpar->coded_side_data, + &st->codecpar->nb_coded_side_data, + AV_PKT_DATA_TILE_INFO, + sizeof(uint32_t) * 2, 0); + if (!sd) + return AVERROR(ENOMEM); + + AV_WL32(sd->data, i); + AV_WL32(sd->data + 4, mov->nb_tiles); + break; + } + } + + for (int i = 0; i < mov->heif_info_size; i++) { + HEIFItem *item = &mov->heif_info[i]; + AVStream *st = item->st; + MOVStreamContext *sc; + + if (item->item_id != mov->thmb_item_id) + continue; + + if (!st) { + av_log(s, AV_LOG_ERROR, "HEIF thumbnail doesn't reference a stream\n"); + return AVERROR_INVALIDDATA; + } + + sc = st->priv_data; + sc->sample_sizes[0] = item->extent_length; + sc->chunk_offsets[0] = item->extent_offset; + + mov_build_index(mov, st); + } + + return 0; +} + static int mov_read_header(AVFormatContext *s) { MOVContext *mov = s->priv_data; @@ -8819,6 +9055,9 @@ static int mov_read_header(AVFormatContext *s) mov->fc = s; mov->trak_index = -1; + mov->grid_item_id = -1; + mov->thmb_item_id = -1; + mov->primary_item_id = -1; /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */ if (pb->seekable & AVIO_SEEKABLE_NORMAL) atom.size = avio_size(pb); @@ -8841,23 +9080,30 @@ static int mov_read_header(AVFormatContext *s) av_log(mov->fc, AV_LOG_TRACE, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb)); if (mov->found_iloc) { - for (i = 0; i < mov->heif_info_size; i++) { - HEIFItem *item = &mov->heif_info[i]; - MOVStreamContext *sc; - AVStream *st; + if (mov->nb_tiles) { + err = mov_parse_tiles(s); + if (err < 0) + return err; + } else + for (i = 0; i < mov->heif_info_size; i++) { + HEIFItem *item = &mov->heif_info[i]; + AVStream *st = item->st; + MOVStreamContext *sc; - if (!item->st) - continue; + if (!st) + continue; - st = item->st; - sc = st->priv_data; - st->codecpar->width = item->width; - st->codecpar->height = item->height; - sc->sample_sizes[0] = item->extent_length; - sc->chunk_offsets[0] = item->extent_offset; + sc = st->priv_data; + st->codecpar->width = item->width; + st->codecpar->height = item->height; + sc->sample_sizes[0] = item->extent_length; + sc->chunk_offsets[0] = item->extent_offset; - mov_build_index(mov, st); - } + if (item->item_id == mov->primary_item_id) + st->disposition |= AV_DISPOSITION_DEFAULT; + + mov_build_index(mov, st); + } } if (pb->seekable & AVIO_SEEKABLE_NORMAL) {