Coverage Report

Created: 2026-01-20 07:37

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
Chunk::Chunk(HeifContext* ctx, uint32_t track_id,
76
             uint32_t first_sample, uint32_t num_samples, uint64_t file_offset, const std::shared_ptr<const Box_stsz>& stsz)
77
91
{
78
91
  m_ctx = ctx;
79
91
  m_track_id = track_id;
80
81
91
  m_first_sample = first_sample;
82
91
  m_last_sample = first_sample + num_samples - 1;
83
84
91
  m_next_sample_to_be_decoded = first_sample;
85
86
1.26k
  for (uint32_t i=0;i<num_samples;i++) {
87
1.16k
    SampleFileRange range;
88
1.16k
    range.offset = file_offset;
89
1.16k
    if (stsz->has_fixed_sample_size()) {
90
903
      range.size = stsz->get_fixed_sample_size();
91
903
    }
92
266
    else {
93
266
      assert(first_sample + i < stsz->num_samples());
94
266
      range.size = stsz->get_sample_sizes()[first_sample + i];
95
266
    }
96
97
1.16k
    m_sample_ranges.push_back(range);
98
99
1.16k
    file_offset += range.size;
100
1.16k
  }
101
91
}
102
103
104
DataExtent Chunk::get_data_extent_for_sample(uint32_t n) const
105
0
{
106
0
  assert(n>= m_first_sample);
107
0
  assert(n<= m_last_sample);
108
109
0
  DataExtent extent;
110
0
  extent.set_file_range(m_ctx->get_heif_file(),
111
0
                        m_sample_ranges[n - m_first_sample].offset,
112
0
                        m_sample_ranges[n - m_first_sample].size);
113
0
  return extent;
114
0
}