/src/libheif/libheif/codecs/vvc_enc.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * HEIF 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 "vvc_enc.h" |
22 | | #include "vvc_boxes.h" |
23 | | #include "error.h" |
24 | | #include "context.h" |
25 | | #include "api_structs.h" |
26 | | |
27 | | #include <string> |
28 | | |
29 | | |
30 | | Result<Encoder::CodedImageData> Encoder_VVC::encode(const std::shared_ptr<HeifPixelImage>& image, |
31 | | struct heif_encoder* encoder, |
32 | | const struct heif_encoding_options& options, |
33 | | enum heif_image_input_class input_class) |
34 | 0 | { |
35 | 0 | Encoder::CodedImageData codedImage; |
36 | |
|
37 | 0 | auto vvcC = std::make_shared<Box_vvcC>(); |
38 | 0 | codedImage.properties.push_back(vvcC); |
39 | | |
40 | |
|
41 | 0 | heif_image c_api_image; |
42 | 0 | c_api_image.image = image; |
43 | |
|
44 | 0 | struct heif_error err = encoder->plugin->encode_image(encoder->encoder, &c_api_image, input_class); |
45 | 0 | if (err.code) { |
46 | 0 | return Error(err.code, |
47 | 0 | err.subcode, |
48 | 0 | err.message); |
49 | 0 | } |
50 | | |
51 | 0 | int encoded_width = 0; |
52 | 0 | int encoded_height = 0; |
53 | |
|
54 | 0 | for (;;) { |
55 | 0 | uint8_t* data; |
56 | 0 | int size; |
57 | |
|
58 | 0 | encoder->plugin->get_compressed_data(encoder->encoder, &data, &size, NULL); |
59 | |
|
60 | 0 | if (data == NULL) { |
61 | 0 | break; |
62 | 0 | } |
63 | | |
64 | | |
65 | 0 | const uint8_t NAL_SPS = 15; |
66 | |
|
67 | 0 | uint8_t nal_type = 0; |
68 | 0 | if (size>=2) { |
69 | 0 | nal_type = (data[1] >> 3) & 0x1F; |
70 | 0 | } |
71 | |
|
72 | 0 | if (nal_type == NAL_SPS) { |
73 | 0 | Box_vvcC::configuration config; |
74 | |
|
75 | 0 | parse_sps_for_vvcC_configuration(data, size, &config, &encoded_width, &encoded_height); |
76 | |
|
77 | 0 | vvcC->set_configuration(config); |
78 | 0 | } |
79 | |
|
80 | 0 | switch (nal_type) { |
81 | 0 | case 14: // VPS |
82 | 0 | case 15: // SPS |
83 | 0 | case 16: // PPS |
84 | 0 | vvcC->append_nal_data(data, size); |
85 | 0 | break; |
86 | | |
87 | 0 | default: |
88 | 0 | codedImage.append_with_4bytes_size(data, size); |
89 | 0 | } |
90 | 0 | } |
91 | | |
92 | 0 | codedImage.codingConstraints.intra_pred_used = true; |
93 | 0 | codedImage.codingConstraints.all_ref_pics_intra = true; // TODO: change when we use predicted frames |
94 | 0 | codedImage.codingConstraints.max_ref_per_pic = 0; |
95 | |
|
96 | 0 | return codedImage; |
97 | 0 | } |
98 | | |
99 | | |
100 | | std::shared_ptr<class Box_VisualSampleEntry> Encoder_VVC::get_sample_description_box(const CodedImageData& data) const |
101 | 0 | { |
102 | 0 | auto vvc1 = std::make_shared<Box_vvc1>(); |
103 | 0 | vvc1->get_VisualSampleEntry().compressorname = "VVC"; |
104 | |
|
105 | 0 | for (auto prop : data.properties) { |
106 | 0 | if (prop->get_short_type() == fourcc("vvcC")) { |
107 | 0 | vvc1->append_child_box(prop); |
108 | 0 | return vvc1; |
109 | 0 | } |
110 | 0 | } |
111 | | |
112 | 0 | assert(false); // no hvcC generated |
113 | 0 | return nullptr; |
114 | 0 | } |