/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 | 0 | { |
80 | 0 | bool success; |
81 | 0 | auto chunk = std::shared_ptr<Chunk>(new Chunk(ctx, track_id, first_sample, |
82 | 0 | num_samples, file_offset, stsz, |
83 | 0 | success)); |
84 | 0 | if (!success) { |
85 | 0 | return nullptr; |
86 | 0 | } |
87 | 0 | return chunk; |
88 | 0 | } |
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 | 0 | { |
95 | 0 | success = false; |
96 | |
|
97 | 0 | m_ctx = ctx; |
98 | 0 | m_track_id = track_id; |
99 | |
|
100 | 0 | m_first_sample = first_sample; |
101 | 0 | m_last_sample = first_sample + num_samples - 1; |
102 | |
|
103 | 0 | m_next_sample_to_be_decoded = first_sample; |
104 | |
|
105 | 0 | for (uint32_t i=0;i<num_samples;i++) { |
106 | 0 | SampleFileRange range; |
107 | 0 | range.offset = file_offset; |
108 | 0 | if (stsz->has_fixed_sample_size()) { |
109 | 0 | range.size = stsz->get_fixed_sample_size(); |
110 | 0 | } |
111 | 0 | else { |
112 | 0 | assert(first_sample + i < stsz->num_samples()); |
113 | 0 | range.size = stsz->get_sample_sizes()[first_sample + i]; |
114 | 0 | } |
115 | | |
116 | | // Detect uint64_t wrap when advancing the running offset. Cumulative chunk |
117 | | // size can exceed UINT64_MAX with a malformed stsz (uint32_t sample size × |
118 | | // uint32_t sample count) starting from a large co64 chunk offset. Stop |
119 | | // building the chunk; create() will discard the partially-built object. |
120 | 0 | if (file_offset > UINT64_MAX - range.size) { |
121 | 0 | return; |
122 | 0 | } |
123 | | |
124 | 0 | m_sample_ranges.push_back(range); |
125 | |
|
126 | 0 | file_offset += range.size; |
127 | 0 | } |
128 | | |
129 | 0 | success = true; |
130 | 0 | } |
131 | | |
132 | | |
133 | | DataExtent Chunk::get_data_extent_for_sample(uint32_t n) const |
134 | 0 | { |
135 | 0 | assert(n>= m_first_sample); |
136 | 0 | assert(n<= m_last_sample); |
137 | | |
138 | 0 | DataExtent extent; |
139 | 0 | extent.set_file_range(m_ctx->get_heif_file(), |
140 | 0 | m_sample_ranges[n - m_first_sample].offset, |
141 | 0 | m_sample_ranges[n - m_first_sample].size); |
142 | 0 | return extent; |
143 | 0 | } |