/src/libheif/libheif/image-items/hevc.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * HEIF codec. |
3 | | * Copyright (c) 2017 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 "hevc.h" |
22 | | #include "codecs/hevc_boxes.h" |
23 | | #include "bitstream.h" |
24 | | #include "error.h" |
25 | | #include "file.h" |
26 | | #include "codecs/hevc_dec.h" |
27 | | |
28 | | #include <cassert> |
29 | | #include <cmath> |
30 | | #include <cstring> |
31 | | #include <iomanip> |
32 | | #include <string> |
33 | | #include <utility> |
34 | | #include "api_structs.h" |
35 | | #include "codecs/hevc_enc.h" |
36 | | |
37 | | |
38 | | ImageItem_HEVC::ImageItem_HEVC(HeifContext* ctx, heif_item_id id) |
39 | 727 | : ImageItem(ctx, id) |
40 | 727 | { |
41 | 727 | m_encoder = std::make_shared<Encoder_HEVC>(); |
42 | 727 | } |
43 | | |
44 | | |
45 | | ImageItem_HEVC::ImageItem_HEVC(HeifContext* ctx) |
46 | 0 | : ImageItem(ctx) |
47 | 0 | { |
48 | 0 | m_encoder = std::make_shared<Encoder_HEVC>(); |
49 | 0 | } |
50 | | |
51 | | |
52 | | Error ImageItem_HEVC::initialize_decoder() |
53 | 723 | { |
54 | 723 | auto hvcC_box = get_property<Box_hvcC>(); |
55 | 723 | if (!hvcC_box) { |
56 | 2 | return Error{heif_error_Invalid_input, |
57 | 2 | heif_suberror_No_hvcC_box}; |
58 | 2 | } |
59 | | |
60 | 721 | m_decoder = std::make_shared<Decoder_HEVC>(hvcC_box); |
61 | | |
62 | 721 | return Error::Ok; |
63 | 723 | } |
64 | | |
65 | | |
66 | | void ImageItem_HEVC::set_decoder_input_data() |
67 | 721 | { |
68 | 721 | DataExtent extent; |
69 | 721 | extent.set_from_image_item(get_context()->get_heif_file(), get_id()); |
70 | | |
71 | 721 | m_decoder->set_data_extent(std::move(extent)); |
72 | 721 | } |
73 | | |
74 | | |
75 | | heif_brand2 ImageItem_HEVC::get_compatible_brand() const |
76 | 0 | { |
77 | 0 | auto hvcC = get_property<Box_hvcC>(); |
78 | |
|
79 | 0 | if (has_essential_property_other_than(std::set{fourcc("hvcC"), |
80 | 0 | fourcc("irot"), |
81 | 0 | fourcc("imir"), |
82 | 0 | fourcc("clap")})) { |
83 | 0 | return 0; |
84 | 0 | } |
85 | | |
86 | 0 | const auto& config = hvcC->get_configuration(); |
87 | 0 | if (config.is_profile_compatibile(HEVCDecoderConfigurationRecord::Profile_Main) || |
88 | 0 | config.is_profile_compatibile(HEVCDecoderConfigurationRecord::Profile_MainStillPicture)) { |
89 | 0 | return heif_brand2_heic; |
90 | 0 | } |
91 | | |
92 | 0 | if (config.is_profile_compatibile(HEVCDecoderConfigurationRecord::Profile_Main10) || |
93 | 0 | config.is_profile_compatibile(HEVCDecoderConfigurationRecord::Profile_RExt)) { |
94 | 0 | return heif_brand2_heix; |
95 | 0 | } |
96 | | |
97 | | // TODO: what brand should we use for this case? |
98 | | |
99 | 0 | return heif_brand2_heix; |
100 | 0 | } |
101 | | |
102 | | |
103 | | Result<std::vector<uint8_t>> ImageItem_HEVC::read_bitstream_configuration_data() const |
104 | 0 | { |
105 | 0 | return m_decoder->read_bitstream_configuration_data(); |
106 | 0 | } |
107 | | |
108 | | |
109 | | Result<std::shared_ptr<class Decoder>> ImageItem_HEVC::get_decoder() const |
110 | 183 | { |
111 | 183 | return {m_decoder}; |
112 | 183 | } |
113 | | |
114 | | |
115 | | std::shared_ptr<class Encoder> ImageItem_HEVC::get_encoder() const |
116 | 0 | { |
117 | 0 | return m_encoder; |
118 | 0 | } |
119 | | |
120 | | |
121 | | void ImageItem_HEVC::set_preencoded_hevc_image(const std::vector<uint8_t>& data) |
122 | 0 | { |
123 | 0 | auto hvcC = std::make_shared<Box_hvcC>(); |
124 | | |
125 | | |
126 | | // --- parse the h265 stream and set hvcC headers and compressed image data |
127 | |
|
128 | 0 | int state = 0; |
129 | |
|
130 | 0 | bool first = true; |
131 | 0 | bool eof = false; |
132 | |
|
133 | 0 | int prev_start_code_start = -1; // init to an invalid value, will always be overwritten before use |
134 | 0 | int start_code_start; |
135 | 0 | int ptr = 0; |
136 | |
|
137 | 0 | for (;;) { |
138 | 0 | bool dump_nal = false; |
139 | |
|
140 | 0 | uint8_t c = data[ptr++]; |
141 | |
|
142 | 0 | if (state == 3) { |
143 | 0 | state = 0; |
144 | 0 | } |
145 | |
|
146 | 0 | if (c == 0 && state <= 1) { |
147 | 0 | state++; |
148 | 0 | } |
149 | 0 | else if (c == 0) { |
150 | | // NOP |
151 | 0 | } |
152 | 0 | else if (c == 1 && state == 2) { |
153 | 0 | start_code_start = ptr - 3; |
154 | 0 | dump_nal = true; |
155 | 0 | state = 3; |
156 | 0 | } |
157 | 0 | else { |
158 | 0 | state = 0; |
159 | 0 | } |
160 | |
|
161 | 0 | if (ptr == (int) data.size()) { |
162 | 0 | start_code_start = (int) data.size(); |
163 | 0 | dump_nal = true; |
164 | 0 | eof = true; |
165 | 0 | } |
166 | |
|
167 | 0 | if (dump_nal) { |
168 | 0 | if (first) { |
169 | 0 | first = false; |
170 | 0 | } |
171 | 0 | else { |
172 | 0 | std::vector<uint8_t> nal_data; |
173 | 0 | size_t length = start_code_start - (prev_start_code_start + 3); |
174 | |
|
175 | 0 | nal_data.resize(length); |
176 | |
|
177 | 0 | assert(prev_start_code_start >= 0); |
178 | 0 | memcpy(nal_data.data(), data.data() + prev_start_code_start + 3, length); |
179 | |
|
180 | 0 | int nal_type = (nal_data[0] >> 1); |
181 | |
|
182 | 0 | switch (nal_type) { |
183 | 0 | case 0x20: |
184 | 0 | case 0x21: |
185 | 0 | case 0x22: |
186 | 0 | hvcC->append_nal_data(nal_data); |
187 | 0 | break; |
188 | | |
189 | 0 | default: { |
190 | 0 | std::vector<uint8_t> nal_data_with_size; |
191 | 0 | nal_data_with_size.resize(nal_data.size() + 4); |
192 | |
|
193 | 0 | memcpy(nal_data_with_size.data() + 4, nal_data.data(), nal_data.size()); |
194 | 0 | nal_data_with_size[0] = ((nal_data.size() >> 24) & 0xFF); |
195 | 0 | nal_data_with_size[1] = ((nal_data.size() >> 16) & 0xFF); |
196 | 0 | nal_data_with_size[2] = ((nal_data.size() >> 8) & 0xFF); |
197 | 0 | nal_data_with_size[3] = ((nal_data.size() >> 0) & 0xFF); |
198 | |
|
199 | 0 | get_file()->append_iloc_data(get_id(), nal_data_with_size, 0); |
200 | 0 | } |
201 | 0 | break; |
202 | 0 | } |
203 | 0 | } |
204 | | |
205 | 0 | prev_start_code_start = start_code_start; |
206 | 0 | } |
207 | | |
208 | 0 | if (eof) { |
209 | 0 | break; |
210 | 0 | } |
211 | 0 | } |
212 | | |
213 | 0 | get_file()->add_property(get_id(), hvcC, true); |
214 | 0 | } |