Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libheif/libheif/sequences/chunk.cc
Line
Count
Source
1
/*
2
 * HEIF image base codec.
3
 * Copyright (c) 2024 Dirk Farin <dirk.farin@gmail.com>
4
 *
5
 * This file is part of libheif.
6
 *
7
 * libheif is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as
9
 * published by the Free Software Foundation, either version 3 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * libheif is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with libheif.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "chunk.h"
22
#include "context.h"
23
#include "codecs/avc_enc.h"
24
#include "codecs/hevc_enc.h"
25
#include "codecs/avif_enc.h"
26
#include "codecs/vvc_enc.h"
27
#include "codecs/jpeg2000_enc.h"
28
#include "codecs/jpeg_enc.h"
29
30
#if WITH_UNCOMPRESSED_CODEC
31
#include "codecs/uncompressed/unc_enc.h"
32
#endif
33
34
35
Chunk::Chunk(HeifContext* ctx, uint32_t track_id, heif_compression_format format)
36
0
    : m_ctx(ctx),
37
0
      m_track_id(track_id),
38
0
      m_compression_format(format)
39
0
{
40
0
  switch (format) {
41
0
    case heif_compression_HEVC:
42
0
      m_encoder = std::make_shared<Encoder_HEVC>();
43
0
      break;
44
0
    case heif_compression_AV1:
45
0
      m_encoder = std::make_shared<Encoder_AVIF>();
46
0
      break;
47
0
    case heif_compression_VVC:
48
0
      m_encoder = std::make_shared<Encoder_VVC>();
49
0
      break;
50
0
    case heif_compression_AVC:
51
0
      m_encoder = std::make_shared<Encoder_AVC>();
52
0
      break;
53
0
    case heif_compression_JPEG2000:
54
0
      m_encoder = std::make_shared<Encoder_JPEG2000>();
55
0
      break;
56
0
    case heif_compression_HTJ2K:
57
0
      m_encoder = std::make_shared<Encoder_HTJ2K>();
58
0
      break;
59
0
    case heif_compression_JPEG:
60
0
      m_encoder = std::make_shared<Encoder_JPEG>();
61
0
      break;
62
0
#if WITH_UNCOMPRESSED_CODEC
63
0
    case heif_compression_uncompressed:
64
0
      m_encoder = std::make_shared<Encoder_uncompressed>();
65
0
      break;
66
0
#endif
67
0
    case heif_compression_undefined:
68
0
    default:
69
0
      m_encoder = nullptr;
70
0
      break;
71
0
  }
72
0
}
73
74
75
std::shared_ptr<Chunk> Chunk::create(HeifContext* ctx, uint32_t track_id,
76
                                     uint32_t first_sample, uint32_t num_samples,
77
                                     uint64_t file_offset,
78
                                     const std::shared_ptr<const Box_stsz>& stsz)
79
539k
{
80
539k
  bool success;
81
539k
  auto chunk = std::shared_ptr<Chunk>(new Chunk(ctx, track_id, first_sample,
82
539k
                                                num_samples, file_offset, stsz,
83
539k
                                                success));
84
539k
  if (!success) {
85
0
    return nullptr;
86
0
  }
87
539k
  return chunk;
88
539k
}
89
90
91
Chunk::Chunk(HeifContext* ctx, uint32_t track_id,
92
             uint32_t first_sample, uint32_t num_samples, uint64_t file_offset,
93
             const std::shared_ptr<const Box_stsz>& stsz, bool& success)
94
539k
{
95
539k
  success = false;
96
97
539k
  m_ctx = ctx;
98
539k
  m_track_id = track_id;
99
100
539k
  m_first_sample = first_sample;
101
539k
  m_last_sample = first_sample + num_samples - 1;
102
103
539k
  m_next_sample_to_be_decoded = first_sample;
104
105
  // Reserve the exact final size so the running offset check below is the only
106
  // growth concern (no geometric reallocation). The aggregate over all chunks is
107
  // accounted against max_total_memory by the owning Track before any chunk is
108
  // built (GHSA-xw34-mjcp-jqh8, variants V2/V3), so no per-chunk limit check here.
109
539k
  m_sample_ranges.reserve(num_samples);
110
111
281M
  for (uint32_t i=0;i<num_samples;i++) {
112
280M
    SampleFileRange range;
113
280M
    range.offset = file_offset;
114
280M
    if (stsz->has_fixed_sample_size()) {
115
280M
      range.size = stsz->get_fixed_sample_size();
116
280M
    }
117
535k
    else {
118
535k
      assert(first_sample + i < stsz->num_samples());
119
535k
      range.size = stsz->get_sample_sizes()[first_sample + i];
120
535k
    }
121
122
    // Detect uint64_t wrap when advancing the running offset. Cumulative chunk
123
    // size can exceed UINT64_MAX with a malformed stsz (uint32_t sample size ×
124
    // uint32_t sample count) starting from a large co64 chunk offset. Stop
125
    // building the chunk; create() will discard the partially-built object.
126
280M
    if (file_offset > UINT64_MAX - range.size) {
127
0
      return;
128
0
    }
129
130
280M
    m_sample_ranges.push_back(range);
131
132
280M
    file_offset += range.size;
133
280M
  }
134
135
539k
  success = true;
136
539k
}
137
138
139
size_t Chunk::sample_range_entry_size()
140
12.4k
{
141
12.4k
  return sizeof(SampleFileRange);
142
12.4k
}
143
144
145
DataExtent Chunk::get_data_extent_for_sample(uint32_t n) const
146
95.2k
{
147
95.2k
  assert(n>= m_first_sample);
148
95.2k
  assert(n<= m_last_sample);
149
150
95.2k
  DataExtent extent;
151
95.2k
  extent.set_file_range(m_ctx->get_heif_file(),
152
95.2k
                        m_sample_ranges[n - m_first_sample].offset,
153
95.2k
                        m_sample_ranges[n - m_first_sample].size);
154
95.2k
  return extent;
155
95.2k
}