Coverage Report

Created: 2025-12-03 07:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libheif/libheif/codecs/hevc_enc.cc
Line
Count
Source
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 "hevc_enc.h"
22
#include "hevc_boxes.h"
23
#include "error.h"
24
#include "context.h"
25
#include "api_structs.h"
26
27
#include <string>
28
29
#include "plugins/nalu_utils.h"
30
31
32
Result<Encoder::CodedImageData> Encoder_HEVC::encode(const std::shared_ptr<HeifPixelImage>& image,
33
                                                     heif_encoder* encoder,
34
                                                     const heif_encoding_options& options,
35
                                                     heif_image_input_class input_class)
36
0
{
37
0
  CodedImageData codedImage;
38
39
0
  auto hvcC = std::make_shared<Box_hvcC>();
40
41
0
  heif_image c_api_image;
42
0
  c_api_image.image = image;
43
44
0
  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, nullptr);
59
60
0
    if (data == nullptr) {
61
0
      break;
62
0
    }
63
64
65
0
    if ((data[0] >> 1) == NAL_UNIT_SPS_NUT) {
66
0
      parse_sps_for_hvcC_configuration(data, size, &hvcC->get_configuration(), &encoded_width, &encoded_height);
67
68
0
      codedImage.encoded_image_width = encoded_width;
69
0
      codedImage.encoded_image_height = encoded_height;
70
0
    }
71
72
0
    switch (data[0] >> 1) {
73
0
      case NAL_UNIT_VPS_NUT:
74
0
      case NAL_UNIT_SPS_NUT:
75
0
      case NAL_UNIT_PPS_NUT:
76
0
        hvcC->append_nal_data(data, size);
77
0
        break;
78
79
0
      default:
80
0
        codedImage.append_with_4bytes_size(data, size);
81
0
    }
82
0
  }
83
84
0
  if (!encoded_width || !encoded_height) {
85
0
    return Error(heif_error_Encoder_plugin_error,
86
0
                 heif_suberror_Invalid_image_size);
87
0
  }
88
89
0
  codedImage.properties.push_back(hvcC);
90
91
92
  // Make sure that the encoder plugin works correctly and the encoded image has the correct size.
93
94
0
  if (encoder->plugin->plugin_api_version >= 3 &&
95
0
      encoder->plugin->query_encoded_size != nullptr) {
96
0
    uint32_t check_encoded_width = image->get_width(), check_encoded_height = image->get_height();
97
98
0
    encoder->plugin->query_encoded_size(encoder->encoder,
99
0
                                        image->get_width(), image->get_height(),
100
0
                                        &check_encoded_width,
101
0
                                        &check_encoded_height);
102
103
0
    assert((int)check_encoded_width == encoded_width);
104
0
    assert((int)check_encoded_height == encoded_height);
105
0
  }
106
107
0
  codedImage.codingConstraints.intra_pred_used = true;
108
0
  codedImage.codingConstraints.all_ref_pics_intra = true; // TODO: change when we use predicted frames
109
110
0
  return codedImage;
111
0
}
112
113
114
std::shared_ptr<Box_VisualSampleEntry> Encoder_HEVC::get_sample_description_box(const CodedImageData& data) const
115
0
{
116
0
  auto hvc1 = std::make_shared<Box_hvc1>();
117
0
  hvc1->get_VisualSampleEntry().compressorname = "HEVC";
118
119
0
  for (auto prop : data.properties) {
120
0
    if (prop->get_short_type() == fourcc("hvcC")) {
121
0
      hvc1->append_child_box(prop);
122
0
      return hvc1;
123
0
    }
124
0
  }
125
126
0
  assert(false); // no hvcC generated
127
0
  return nullptr;
128
0
}