PR #26160 opened by luoye-cpu
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26160
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26160.patch
## Summary
The iloc parser currently rejects still images (AVIF/HEIF) whose item data
is stored in more than one extent ("iloc: extent_count > 1 is not
implemented"). Real-world muxers may split item data across extents, and
the downstream consumers (image grid, overlay, EXIF, gain-map tmap, item
packet read) all assume a single contiguous run today.
This patch removes that limitation:
- HEIFItem stores an extent list (`HEIFItemExtent { offset, length }`) and
the iloc parser fills it with per-extent overflow checks (zero-length
extents are skipped).
- Contiguous extents are merged into one segment, so single-extent and
contiguous multi-extent inputs normalize to the existing single-segment
path (no behavior change for current files).
- A new helper `heif_read_item_data()` assembles all extents of an item
into one contiguous buffer (saving/restoring the file position), used by
the image grid, image overlay, EXIF and gain-map tmap parsers.
- mov_read_packet() gains a multi-extent branch: per-segment seek +
av_append_packet assembly, with the idat-relative offset applied per
segment.
Non-contiguous extents are fully supported; index_size != 0 remains
PATCHWELCOME and primary-item (pitm) semantics are unchanged.
## FATE
fate-mov-avif-demux-still-image-multiple-extents exercises a still AVIF
whose item data is split into two non-contiguous extents separated by a
free box. The sample is built from a single-extent AVIF and a script
reassembles the original bytes from the two extents (losslessness proof);
the unpatched decoder reports the removed PATCHWELCOME on this input.
A single-extent baseline file decodes identically before and after the
patch (three-way packet-crc comparison), and the assembled multi-extent
payload matches the original bytes exactly.
Related work: the mjpeg scan-output decoder series (PR #26092) uses the same
receive_frame multi-frame output contract family; this demuxer patch is
independent of that series but lands as part of the same progressive-image-
reading effort.
>From 09d068b23a4d0214d0e6a476623bc3de32d79d1c Mon Sep 17 00:00:00 2001
From: luoye-cpu <[email protected]>
Date: Wed, 18 Nov 2026 02:25:01 +0800
Subject: [PATCH] avformat/mov: support multiple extents in iloc for HEIF items
For still AVIF/HEIF items, parse the full extent list of each item
instead of rejecting extent_count > 1. Contiguous extents are merged
into one segment, so single-extent items and contiguous multi-extent
items normalize to one segment with no behavior change.
A new heif_read_item_data() helper assembles all extents of an item
into one contiguous buffer, and the grid/iovl/Exif/tmap readers are
converted to it. For video-like items, mov_parse_heif_items() records
the segment list in MOVStreamContext and mov_read_packet() assembles
packets with one seek per segment.
FATE: fate-mov-avif-demux-still-image-multiple-extents
---
libavformat/isom.h | 11 +-
libavformat/mov.c | 336 +++++++++++++-----
tests/fate/mov.mak | 5 +
...ov-avif-demux-still-image-multiple-extents | 7 +
4 files changed, 260 insertions(+), 99 deletions(-)
create mode 100644 tests/ref/fate/mov-avif-demux-still-image-multiple-extents
diff --git a/libavformat/isom.h b/libavformat/isom.h
index 6230218e37f..e046df5b0b8 100644
--- a/libavformat/isom.h
+++ b/libavformat/isom.h
@@ -289,8 +289,15 @@ typedef struct MOVStreamContext {
struct IAMFDemuxContext *iamf;
int iamf_stream_offset;
+ struct HEIFItemExtent *heif_extents;
+ int nb_heif_extents;
} MOVStreamContext;
+typedef struct HEIFItemExtent {
+ int64_t offset;
+ int64_t length;
+} HEIFItemExtent;
+
typedef struct HEIFItemRef {
uint32_t type;
int item_id;
@@ -306,8 +313,8 @@ typedef struct HEIFItem {
int flags;
char *name;
int item_id;
- int64_t extent_length;
- int64_t extent_offset;
+ HEIFItemExtent *extents;
+ int nb_extents;
int width;
int height;
int rotation;
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 96fcce1f8b0..407a27917a3 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -9510,6 +9510,7 @@ 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;
int64_t base_offset, extent_offset, extent_length;
+ int64_t atom_start, remaining;
uint8_t value;
if (c->found_iloc) {
@@ -9517,6 +9518,7 @@ static int mov_read_iloc(MOVContext *c, AVIOContext *pb,
MOVAtom atom)
return 0;
}
+ atom_start = avio_tell(pb);
version = avio_r8(pb);
avio_rb24(pb); // flags.
@@ -9562,15 +9564,13 @@ static int mov_read_iloc(MOVContext *c, AVIOContext
*pb, MOVAtom atom)
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.
- avpriv_report_missing_feature(c->fc, "iloc: extent_count > 1");
- return AVERROR_PATCHWELCOME;
- }
-
- if (rb_size(pb, &extent_offset, offset_size) < 0 ||
- rb_size(pb, &extent_length, length_size) < 0 ||
- base_offset > INT64_MAX - extent_offset)
+ // Each extent takes at least offset_size + length_size bytes; bound
+ // the count by the remaining box bytes so anomalous input cannot
+ // drive an unreasonably long extent loop. A zero total field size
+ // cannot carry any extent.
+ remaining = atom_start + atom.size - avio_tell(pb);
+ if (extent_count > (offset_size + length_size
+ ? remaining / (offset_size + length_size) : 0))
return AVERROR_INVALIDDATA;
for (j = 0; j < c->nb_heif_item; j++) {
@@ -9590,11 +9590,63 @@ static int mov_read_iloc(MOVContext *c, AVIOContext
*pb, MOVAtom atom)
if (offset_type == 1)
item->is_idat_relative = 1;
- item->extent_length = extent_length;
- item->extent_offset = base_offset + extent_offset;
- av_log(c->fc, AV_LOG_TRACE, "iloc: item_idx %d, item->item_id %d,
offset_type %d, "
- "extent_offset %"PRId64", extent_length
%"PRId64"\n",
- i, item->item_id, offset_type, item->extent_offset,
item->extent_length);
+
+ av_freep(&item->extents);
+ item->nb_extents = 0;
+
+ for (int k = 0; k < extent_count; k++) {
+ HEIFItemExtent *extents, *last;
+ int merged;
+
+ if (rb_size(pb, &extent_offset, offset_size) < 0 ||
+ rb_size(pb, &extent_length, length_size) < 0 ||
+ base_offset > INT64_MAX - extent_offset)
+ return AVERROR_INVALIDDATA;
+ extent_offset += base_offset;
+
+ if (!extent_length)
+ continue;
+
+ last = item->nb_extents ? &item->extents[item->nb_extents - 1] :
NULL;
+ merged = 0;
+ if (last) {
+ // The contiguity test below adds last->offset and
last->length;
+ // that sum must be validated for overflow first.
+ if (last->length > INT64_MAX - last->offset)
+ return AVERROR_INVALIDDATA;
+ merged = last->offset + last->length == extent_offset;
+ }
+
+ // Contiguous extents are merged so that a single-extent item and a
+ // contiguous multi-extent item both normalize to one segment.
+ if (merged) {
+ if (extent_length > INT64_MAX - last->length)
+ return AVERROR_INVALIDDATA;
+ last->length += extent_length;
+ } else {
+ extents = av_realloc_array(item->extents, item->nb_extents +
1, sizeof(*item->extents));
+ if (!extents)
+ return AVERROR(ENOMEM);
+ item->extents = extents;
+ item->extents[item->nb_extents].offset = extent_offset;
+ item->extents[item->nb_extents].length = extent_length;
+ item->nb_extents++;
+ }
+ av_log(c->fc, AV_LOG_TRACE, "iloc: item_idx %d, item->item_id %d,
offset_type %d, "
+ "extent_offset %"PRId64",
extent_length %"PRId64"\n",
+ i, item->item_id, offset_type, extent_offset,
extent_length);
+ }
+ if (!item->nb_extents) {
+ // Degenerate case (no extents or only zero-length ones): keep the
+ // pre-multi-extent behavior with a single zero-length extent
+ // anchored at base_offset.
+ item->extents = av_mallocz(sizeof(*item->extents));
+ if (!item->extents)
+ return AVERROR(ENOMEM);
+ item->extents[0].offset = base_offset;
+ item->extents[0].length = 0;
+ item->nb_extents = 1;
+ }
}
c->found_iloc = 1;
@@ -10932,6 +10984,7 @@ static void mov_free_stream_context(AVFormatContext *s,
AVStream *st)
}
av_freep(&sc->tts_data);
+ av_freep(&sc->heif_extents);
for (int i = 0; i < sc->drefs_count; i++) {
av_freep(&sc->drefs[i].path);
av_freep(&sc->drefs[i].dir);
@@ -11026,6 +11079,7 @@ static int mov_read_close(AVFormatContext *s)
for (i = 0; i < mov->nb_heif_item; i++) {
if (!mov->heif_item[i])
continue;
+ av_freep(&mov->heif_item[i]->extents);
av_freep(&mov->heif_item[i]->name);
av_freep(&mov->heif_item[i]->iref_from_list);
av_freep(&mov->heif_item[i]->iref_to_list);
@@ -11269,17 +11323,81 @@ static int
set_display_matrix_from_item(AVPacketSideData **coded_side_data, int
return 0;
}
+// Assemble all extents of a HEIF item, in order, into one contiguous buffer.
+// Restores the file position on return. Callers must have checked that the
+// input is seekable and that idat_offset is set when the item is
idat-relative.
+static int heif_read_item_data(AVFormatContext *s, const HEIFItem *item,
+ uint8_t **pbuf, int64_t *psize)
+{
+ MOVContext *c = s->priv_data;
+ int64_t idat_offset = 0, total = 0, read = 0, pos;
+ uint8_t *buf;
+ int ret = 0;
+
+ for (int i = 0; i < item->nb_extents; i++) {
+ if (item->extents[i].length > INT64_MAX - total)
+ return AVERROR_INVALIDDATA;
+ total += item->extents[i].length;
+ }
+ if (total > INT_MAX)
+ return AVERROR_INVALIDDATA;
+
+ if (item->is_idat_relative) {
+ if (!c->idat_offset)
+ return AVERROR_INVALIDDATA;
+ idat_offset = c->idat_offset;
+ }
+
+ buf = av_malloc(total ? total : 1);
+ if (!buf)
+ return AVERROR(ENOMEM);
+
+ pos = avio_tell(s->pb);
+ for (int i = 0; i < item->nb_extents; i++) {
+ int64_t offset = item->extents[i].offset;
+ int len = item->extents[i].length;
+ int err;
+
+ if (idat_offset > INT64_MAX - offset ||
+ avio_seek(s->pb, offset + idat_offset, SEEK_SET) < 0) {
+ ret = AVERROR_INVALIDDATA;
+ goto fail;
+ }
+ err = avio_read(s->pb, buf + read, len);
+ if (err != len) {
+ ret = err < 0 ? err : AVERROR_INVALIDDATA;
+ goto fail;
+ }
+ read += len;
+ }
+ avio_seek(s->pb, pos, SEEK_SET);
+
+ *pbuf = buf;
+ *psize = total;
+ return 0;
+
+fail:
+ av_free(buf);
+ avio_seek(s->pb, pos, SEEK_SET);
+
+ return ret;
+}
+
static int read_image_grid(AVFormatContext *s, const HEIFGroup *grid,
AVStreamGroupTileGrid *tile_grid)
{
MOVContext *c = s->priv_data;
const HEIFItem *item = grid->item;
int64_t coded_width = 0, coded_height = 0;
- int64_t offset = 0, pos = avio_tell(s->pb);
int64_t x = 0, y = 0;
+ int64_t buf_size;
int i = 0;
int tile_rows, tile_cols;
int flags, size;
+ FFIOContext pbctx;
+ AVIOContext *pb;
+ uint8_t *buf;
+ int ret;
if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
av_log(c->fc, AV_LOG_INFO, "grid box with non seekable input\n");
@@ -11290,27 +11408,28 @@ static int read_image_grid(AVFormatContext *s, const
HEIFGroup *grid,
av_log(c->fc, AV_LOG_ERROR, "missing idat box required by the
image grid\n");
return AVERROR_INVALIDDATA;
}
- offset = c->idat_offset;
}
- if (offset > INT64_MAX - item->extent_offset)
- return AVERROR_INVALIDDATA;
+ ret = heif_read_item_data(s, item, &buf, &buf_size);
+ if (ret < 0)
+ return ret;
- avio_seek(s->pb, item->extent_offset + offset, SEEK_SET);
+ pb = &pbctx.pub;
+ ffio_init_read_context(&pbctx, buf, buf_size);
- avio_r8(s->pb); /* version */
- flags = avio_r8(s->pb);
+ avio_r8(pb); /* version */
+ flags = avio_r8(pb);
- tile_rows = avio_r8(s->pb) + 1;
- tile_cols = avio_r8(s->pb) + 1;
+ tile_rows = avio_r8(pb) + 1;
+ tile_cols = avio_r8(pb) + 1;
/* actual width and height of output image */
- tile_grid->width = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
- tile_grid->height = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
+ tile_grid->width = (flags & 1) ? avio_rb32(pb) : avio_rb16(pb);
+ tile_grid->height = (flags & 1) ? avio_rb32(pb) : avio_rb16(pb);
av_log(c->fc, AV_LOG_TRACE, "grid: grid_rows %d grid_cols %d output_width
%d output_height %d\n",
tile_rows, tile_cols, tile_grid->width, tile_grid->height);
- avio_seek(s->pb, pos, SEEK_SET);
+ av_free(buf);
size = tile_rows * tile_cols;
tile_grid->nb_tiles = grid->nb_items;
@@ -11372,8 +11491,11 @@ static int read_image_iovl(AVFormatContext *s, const
HEIFGroup *grid,
MOVContext *c = s->priv_data;
const HEIFItem *item = grid->item;
uint16_t canvas_fill_value[4];
- int64_t offset = 0, pos = avio_tell(s->pb);
+ int64_t buf_size;
int ret = 0, flags;
+ FFIOContext pbctx;
+ AVIOContext *pb;
+ uint8_t *buf;
if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
av_log(c->fc, AV_LOG_INFO, "iovl box with non seekable input\n");
@@ -11384,19 +11506,20 @@ static int read_image_iovl(AVFormatContext *s, const
HEIFGroup *grid,
av_log(c->fc, AV_LOG_ERROR, "missing idat box required by the
image overlay\n");
return AVERROR_INVALIDDATA;
}
- offset = c->idat_offset;
}
- if (offset > INT64_MAX - item->extent_offset)
- return AVERROR_INVALIDDATA;
+ ret = heif_read_item_data(s, item, &buf, &buf_size);
+ if (ret < 0)
+ return ret;
- avio_seek(s->pb, item->extent_offset + offset, SEEK_SET);
+ pb = &pbctx.pub;
+ ffio_init_read_context(&pbctx, buf, buf_size);
- avio_r8(s->pb); /* version */
- flags = avio_r8(s->pb);
+ avio_r8(pb); /* version */
+ flags = avio_r8(pb);
for (int i = 0; i < 4; i++)
- canvas_fill_value[i] = avio_rb16(s->pb);
+ canvas_fill_value[i] = avio_rb16(pb);
av_log(c->fc, AV_LOG_TRACE, "iovl: canvas_fill_value { %u, %u, %u, %u }\n",
canvas_fill_value[0], canvas_fill_value[1],
canvas_fill_value[2], canvas_fill_value[3]);
@@ -11405,9 +11528,9 @@ static int read_image_iovl(AVFormatContext *s, const
HEIFGroup *grid,
/* actual width and height of output image */
tile_grid->width =
- tile_grid->coded_width = (flags & 1) ? avio_rb32(s->pb) :
avio_rb16(s->pb);
+ tile_grid->coded_width = (flags & 1) ? avio_rb32(pb) : avio_rb16(pb);
tile_grid->height =
- tile_grid->coded_height = (flags & 1) ? avio_rb32(s->pb) :
avio_rb16(s->pb);
+ tile_grid->coded_height = (flags & 1) ? avio_rb32(pb) : avio_rb16(pb);
av_log(c->fc, AV_LOG_TRACE, "iovl: output_width %d, output_height %d\n",
tile_grid->width, tile_grid->height);
@@ -11421,8 +11544,8 @@ static int read_image_iovl(AVFormatContext *s, const
HEIFGroup *grid,
for (int i = 0; i < tile_grid->nb_tiles; i++) {
tile_grid->offsets[i].idx = grid->item_idx_list[i];
- tile_grid->offsets[i].horizontal = (flags & 1) ? avio_rb32(s->pb) :
avio_rb16(s->pb);
- tile_grid->offsets[i].vertical = (flags & 1) ? avio_rb32(s->pb) :
avio_rb16(s->pb);
+ tile_grid->offsets[i].horizontal = (flags & 1) ? avio_rb32(pb) :
avio_rb16(pb);
+ tile_grid->offsets[i].vertical = (flags & 1) ? avio_rb32(pb) :
avio_rb16(pb);
av_log(c->fc, AV_LOG_TRACE, "iovl: stream_idx[%d] %u, "
"horizontal_offset[%d] %d,
vertical_offset[%d] %d\n",
i, tile_grid->offsets[i].idx,
@@ -11430,7 +11553,7 @@ static int read_image_iovl(AVFormatContext *s, const
HEIFGroup *grid,
}
fail:
- avio_seek(s->pb, pos, SEEK_SET);
+ av_free(buf);
return ret;
}
@@ -11440,7 +11563,7 @@ static int mov_parse_exif_item(AVFormatContext *s,
HEIFItem *item, const HEIFIte
MOVContext *c = s->priv_data;
AVExifMetadata *ifd = &item->ifd;
uint8_t *buf;
- int64_t offset = 0, pos = avio_tell(s->pb);
+ int64_t buf_size;
unsigned orientation_id = av_exif_get_tag_id("Orientation");
int err;
@@ -11453,30 +11576,17 @@ static int mov_parse_exif_item(AVFormatContext *s,
HEIFItem *item, const HEIFIte
av_log(c->fc, AV_LOG_ERROR, "missing idat box required by the Exif
metadata\n");
return AVERROR_INVALIDDATA;
}
- offset = c->idat_offset;
}
- buf = av_malloc(ref->extent_length);
- if (!buf)
- return AVERROR(ENOMEM);
-
- if (offset > INT64_MAX - ref->extent_offset) {
- err = AVERROR(ENOMEM);
- goto fail;
- }
-
- avio_seek(s->pb, ref->extent_offset + offset, SEEK_SET);
- err = avio_read(s->pb, buf, ref->extent_length);
- if (err != ref->extent_length) {
- if (err > 0)
- err = AVERROR_INVALIDDATA;
- goto fail;
- }
+ err = heif_read_item_data(s, ref, &buf, &buf_size);
+ if (err < 0)
+ return err;
// HEIF spec states that Exif metadata is informative. The irot item
property is
// the normative source of rotation information. So we remove any
Orientation tag
// present in the Exif buffer.
- err = av_exif_parse_buffer(s, buf, ref->extent_length, ifd, AV_EXIF_T_OFF);
+ err = av_exif_parse_buffer(s, buf, buf_size, ifd, AV_EXIF_T_OFF);
+ av_free(buf);
if (err < 0) {
av_log(s, AV_LOG_ERROR, "Unable to parse Exif metadata\n");
goto fail;
@@ -11486,12 +11596,9 @@ static int mov_parse_exif_item(AVFormatContext *s,
HEIFItem *item, const HEIFIte
if (err < 0)
goto fail;
- err = 0;
+ return 0;
fail:
- av_free(buf);
- if (err < 0)
- av_exif_free(ifd);
- avio_seek(s->pb, pos, SEEK_SET);
+ av_exif_free(ifd);
return err;
}
@@ -11658,9 +11765,8 @@ static int
mov_parse_heif_layered_video_tmap(AVFormatContext *s, const HEIFGroup
AVStreamGroupLayeredVideo *layered_video;
AVGainMapParams *gain = NULL;
uint8_t *payload = NULL;
- int64_t pos, offset = 0;
+ int64_t payload_size;
size_t gain_size;
- int64_t err64;
int err;
if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
@@ -11729,43 +11835,23 @@ static int
mov_parse_heif_layered_video_tmap(AVFormatContext *s, const HEIFGroup
av_log(s, AV_LOG_ERROR, "Missing idat box required by the tmap\n");
return AVERROR_INVALIDDATA;
}
- offset = mov->idat_offset;
}
- if (offset > INT64_MAX - tmap->item->extent_offset)
- return AVERROR_INVALIDDATA;
-
gain = av_gain_map_params_alloc(&gain_size);
if (!gain)
return AVERROR(ENOMEM);
- payload = av_malloc(tmap->item->extent_length);
- if (!payload) {
- err = AVERROR(ENOMEM);
+ err = heif_read_item_data(s, tmap->item, &payload, &payload_size);
+ if (err < 0)
+ goto fail;
+ if (payload_size < 1) {
+ err = AVERROR_INVALIDDATA;
goto fail;
}
- pos = avio_tell(s->pb);
- err64 = avio_seek(s->pb, tmap->item->extent_offset + offset, SEEK_SET);
- if (err64 != tmap->item->extent_offset + offset) {
- err64 = err64 < 0 ? err64 : AVERROR_INVALIDDATA;
- goto fail;
- }
- avio_r8(s->pb); /* version */
-
- err = avio_read(s->pb, payload, tmap->item->extent_length - 1);
- if (err == (tmap->item->extent_length - 1))
- err64 = avio_seek(s->pb, pos, SEEK_SET);
- else {
- err = err < 0 ? err : AVERROR_INVALIDDATA;
- goto fail;
- }
- if (err64 != pos) {
- err = err64 < 0 ? err64 : AVERROR_INVALIDDATA;
- goto fail;
- }
-
- err = av_gain_map_params_from_iso21496(gain, payload,
tmap->item->extent_length - 1);
+ // The first byte of the tmap item is the version, the ISO 21496-1 gain
+ // map payload starts right after it.
+ err = av_gain_map_params_from_iso21496(gain, payload + 1, payload_size -
1);
av_freep(&payload);
if (err < 0) {
av_log(s, AV_LOG_ERROR, "Invalid ISO 21496-1 gain map payload\n");
@@ -12090,7 +12176,7 @@ static int mov_parse_heif_items(AVFormatContext *s)
HEIFItem *item = mov->heif_item[i];
MOVStreamContext *sc;
AVStream *st;
- int64_t offset = 0;
+ int64_t offset = 0, extent_total = 0;
if (!item)
continue;
@@ -12110,17 +12196,38 @@ static int mov_parse_heif_items(AVFormatContext *s)
st->codecpar->width = item->width;
st->codecpar->height = item->height;
- sc->sample_size = sc->stsz_sample_size = item->extent_length;
+ for (int j = 0; j < item->nb_extents; j++) {
+ if (item->extents[j].length > INT64_MAX - extent_total)
+ return AVERROR_INVALIDDATA;
+ extent_total += item->extents[j].length;
+ }
+
+ sc->sample_size = sc->stsz_sample_size = extent_total;
sc->sample_count = 1;
err = sanity_checks(s, sc, st->index);
if (err)
return AVERROR_INVALIDDATA;
- if (offset > INT64_MAX - item->extent_offset)
+ if (item->nb_extents > 1) {
+ // The segments of a multi-extent item are not contiguous in the
+ // file, so mov_read_packet() assembles packets segment by segment.
+ sc->heif_extents = av_malloc_array(item->nb_extents,
sizeof(*sc->heif_extents));
+ if (!sc->heif_extents)
+ return AVERROR(ENOMEM);
+ for (int j = 0; j < item->nb_extents; j++) {
+ if (offset > INT64_MAX - item->extents[j].offset)
+ return AVERROR_INVALIDDATA;
+ sc->heif_extents[j].offset = item->extents[j].offset + offset;
+ sc->heif_extents[j].length = item->extents[j].length;
+ }
+ sc->nb_heif_extents = item->nb_extents;
+ }
+
+ if (offset > INT64_MAX - item->extents[0].offset)
return AVERROR_INVALIDDATA;
- sc->chunk_offsets[0] = item->extent_offset + offset;
+ sc->chunk_offsets[0] = item->extents[0].offset + offset;
err = mov_parse_heif_item_references(s, item,
&st->codecpar->coded_side_data,
&st->codecpar->nb_coded_side_data);
@@ -13047,6 +13154,41 @@ static int mov_read_packet(AVFormatContext *s,
AVPacket *pkt)
au_size = sample->size - 4;
}
ret = av_get_packet(sc->pb, pkt, au_size);
+ } else if (sc->nb_heif_extents > 1) {
+ // Multi-extent item: the segments are not contiguous in the file,
+ // so the packet is assembled with one seek per segment. The first
+ // segment is already seeked to and read via sample->pos above.
+ // Own error state; ret is not guaranteed to be set when entering
+ // this branch.
+ int64_t assembled = 0;
+ int err = 0;
+
+ for (int j = 0; j < sc->nb_heif_extents && assembled <
sample->size; j++) {
+ int64_t length = FFMIN(sc->heif_extents[j].length,
sample->size - assembled);
+ int seg;
+
+ if (j) {
+ int64_t err64 = avio_seek(sc->pb,
sc->heif_extents[j].offset, SEEK_SET);
+ if (err64 != sc->heif_extents[j].offset) {
+ err = err64 < 0 ? (int)err64 : AVERROR_INVALIDDATA;
+ break;
+ }
+ }
+ seg = assembled ? av_append_packet(sc->pb, pkt, length)
+ : av_get_packet(sc->pb, pkt, length);
+ // Each segment must be read exactly to the expected length;
+ // a short read would silently yield a truncated packet.
+ if (seg != length) {
+ err = seg < 0 ? seg : AVERROR_INVALIDDATA;
+ break;
+ }
+ assembled += seg;
+ }
+ if (err < 0) {
+ av_packet_unref(pkt);
+ ret = err;
+ } else
+ ret = assembled;
} else
ret = av_get_packet(sc->pb, pkt, sample->size);
if (ret < 0) {
diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index 1463ed841ac..bfa7b83704e 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -280,6 +280,11 @@ FATE_MOV_FFMPEG_SAMPLES-$(call FRAMECRC, MOV, AV1,
AV1_PARSER) \
+= fate-mov-avif-demux-still-image-multiple-items
fate-mov-avif-demux-still-image-multiple-items: CMD = framecrc -c:v av1 -i
$(TARGET_SAMPLES)/avif/still_image_exif.avif -c:v copy
+# avif demuxing - still image with a single item described by multiple extents.
+FATE_MOV_FFMPEG_SAMPLES-$(call FRAMECRC, MOV, AV1, AV1_PARSER) \
+ += fate-mov-avif-demux-still-image-multiple-extents
+fate-mov-avif-demux-still-image-multiple-extents: CMD = framecrc -c:v av1 -i
$(TARGET_SAMPLES)/avif/still_image_multiple_extents.avif -c:v copy
+
# avif demuxing - still image with multiple items forming two grids (color +
alpha), which themselves are handled as an Alpha derived image
FATE_MOV_FFMPEG_FFPROBE_SAMPLES-$(call FRAMECRC, MOV, AV1, AV1_PARSER) \
+= fate-mov-avif-demux-still-image-alpha-grid
diff --git a/tests/ref/fate/mov-avif-demux-still-image-multiple-extents
b/tests/ref/fate/mov-avif-demux-still-image-multiple-extents
new file mode 100644
index 00000000000..09355f211b5
--- /dev/null
+++ b/tests/ref/fate/mov-avif-demux-still-image-multiple-extents
@@ -0,0 +1,7 @@
+#extradata 0: 4, 0x021c008d
+#tb 0: 1/1
+#media_type 0: video
+#codec_id 0: av1
+#dimensions 0: 160x120
+#sar 0: 0/1
+0, 0, 0, 1, 947, 0xf176dba8
--
2.54.0
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]