/src/libultrahdr/third_party/libheif/libheif/image-items/avif.cc
Line | Count | Source |
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 "pixelimage.h" |
22 | | #include "avif.h" |
23 | | #include "codecs/avif_dec.h" |
24 | | #include "codecs/avif_boxes.h" |
25 | | #include "bitstream.h" |
26 | | #include "common_utils.h" |
27 | | #include "libheif/api_structs.h" |
28 | | #include "file.h" |
29 | | #include <iomanip> |
30 | | #include <limits> |
31 | | #include <string> |
32 | | #include <cstring> |
33 | | #include <utility> |
34 | | |
35 | | // https://aomediacodec.github.io/av1-spec/av1-spec.pdf |
36 | | |
37 | | |
38 | | |
39 | | Error ImageItem_AVIF::on_load_file() |
40 | 33 | { |
41 | 33 | auto av1C_box = get_property<Box_av1C>(); |
42 | 33 | if (!av1C_box) { |
43 | 25 | return Error{heif_error_Invalid_input, |
44 | 25 | heif_suberror_No_av1C_box}; |
45 | 25 | } |
46 | | |
47 | 8 | m_decoder = std::make_shared<Decoder_AVIF>(av1C_box); |
48 | | |
49 | 8 | DataExtent extent; |
50 | 8 | extent.set_from_image_item(get_context()->get_heif_file(), get_id()); |
51 | | |
52 | 8 | m_decoder->set_data_extent(std::move(extent)); |
53 | | |
54 | 8 | return Error::Ok; |
55 | 33 | } |
56 | | |
57 | | |
58 | | Result<ImageItem::CodedImageData> ImageItem_AVIF::encode(const std::shared_ptr<HeifPixelImage>& image, |
59 | | struct heif_encoder* encoder, |
60 | | const struct heif_encoding_options& options, |
61 | | enum heif_image_input_class input_class) |
62 | 0 | { |
63 | 0 | CodedImageData codedImage; |
64 | |
|
65 | 0 | Box_av1C::configuration config; |
66 | | |
67 | | // Fill preliminary av1C in case we cannot parse the sequence_header() correctly in the code below. |
68 | | // TODO: maybe we can remove this later. |
69 | 0 | fill_av1C_configuration(&config, image); |
70 | |
|
71 | 0 | heif_image c_api_image; |
72 | 0 | c_api_image.image = image; |
73 | |
|
74 | 0 | struct heif_error err = encoder->plugin->encode_image(encoder->encoder, &c_api_image, input_class); |
75 | 0 | if (err.code) { |
76 | 0 | return Error(err.code, |
77 | 0 | err.subcode, |
78 | 0 | err.message); |
79 | 0 | } |
80 | | |
81 | 0 | for (;;) { |
82 | 0 | uint8_t* data; |
83 | 0 | int size; |
84 | |
|
85 | 0 | encoder->plugin->get_compressed_data(encoder->encoder, &data, &size, nullptr); |
86 | |
|
87 | 0 | bool found_config = fill_av1C_configuration_from_stream(&config, data, size); |
88 | 0 | (void) found_config; |
89 | |
|
90 | 0 | if (data == nullptr) { |
91 | 0 | break; |
92 | 0 | } |
93 | | |
94 | 0 | codedImage.append(data, size); |
95 | 0 | } |
96 | |
|
97 | 0 | auto av1C = std::make_shared<Box_av1C>(); |
98 | 0 | av1C->set_configuration(config); |
99 | 0 | codedImage.properties.push_back(av1C); |
100 | |
|
101 | 0 | return codedImage; |
102 | 0 | } |
103 | | |
104 | | |
105 | | Result<std::vector<uint8_t>> ImageItem_AVIF::read_bitstream_configuration_data() const |
106 | 0 | { |
107 | 0 | return m_decoder->read_bitstream_configuration_data(); |
108 | 0 | } |
109 | | |
110 | | |
111 | | Result<std::shared_ptr<class Decoder>> ImageItem_AVIF::get_decoder() const |
112 | 0 | { |
113 | 0 | return {m_decoder}; |
114 | 0 | } |