Coverage Report

Created: 2026-12-01 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libheif/libheif/codecs/encoder.h
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
#ifndef HEIF_ENCODER_H
22
#define HEIF_ENCODER_H
23
24
#include "libheif/heif.h"
25
#include "error.h"
26
#include "libheif/heif_plugin.h"
27
28
#include <memory>
29
#include <string>
30
#include <utility>
31
#include <vector>
32
#include "sequences/seq_boxes.h"
33
34
class HeifPixelImage;
35
36
class Box;
37
38
39
class Encoder {
40
public:
41
0
  virtual ~Encoder() = default;
42
43
  struct CodedImageData {
44
    std::vector<std::shared_ptr<Box>> properties;
45
    std::vector<uint8_t> bitstream;
46
    CodingConstraints codingConstraints;
47
48
    // If 0, the encoded size is unknown.
49
    uint32_t encoded_image_width = 0;
50
    uint32_t encoded_image_height = 0;
51
52
    bool is_sync_frame = true; // TODO: set in encoder
53
    uintptr_t frame_nr = 0;
54
55
    void append(const uint8_t* data, size_t size);
56
57
    void append_with_4bytes_size(const uint8_t* data, size_t size);
58
  };
59
60
  // If the output format requires a specific nclx (like JPEG), return this. Otherwise, return NULL.
61
0
  virtual const heif_color_profile_nclx* get_forced_output_nclx() const { return nullptr; }
62
63
  // Checks that an image handed to this encoder has a plane layout the encoder plugins can rely
64
  // on: exactly the planes of its colorspace and chroma format, each once and at the size its
65
  // channel implies (HeifPixelImage::check_plane_layout()). Planes with channel
66
  // heif_channel_unknown are tolerated. Images with a custom colorspace pass unchecked: they
67
  // have no defined layout, and the uncompressed encoder writes their components as they are.
68
  // This is the libheif-side check, made before the image is colour-converted for the plugin
69
  // (or handed over unchanged when no conversion is needed). A plugin that supports only a
70
  // subset of the layouts checks its input itself (see plugins/encoder_input_check.h).
71
  virtual Error check_input_image_layout(const HeifPixelImage& image) const;
72
73
  Result<std::shared_ptr<HeifPixelImage>> convert_colorspace_for_encoding(const std::shared_ptr<HeifPixelImage>& image,
74
                                                                          heif_encoder* encoder,
75
                                                                          const heif_color_profile_nclx* user_requested_output_nclx,
76
                                                                          const heif_color_conversion_options* color_conversion_options,
77
                                                                          const heif_security_limits* security_limits);
78
79
  virtual Result<CodedImageData> encode(const std::shared_ptr<HeifPixelImage>& image,
80
                                        heif_encoder* encoder,
81
                                        const heif_encoding_options& options,
82
0
                                        heif_image_input_class input_class) { return {}; }
83
84
  // --- encode sequence
85
86
0
  virtual bool encode_sequence_started() const { return false; }
87
88
  virtual Error encode_sequence_frame(const std::shared_ptr<HeifPixelImage>& image,
89
                                      heif_encoder* encoder,
90
                                      const heif_sequence_encoding_options& options,
91
                                      heif_image_input_class input_class,
92
                                      uint32_t framerate_num, uint32_t framerate_denom,
93
0
                                      uintptr_t frame_number) { return {}; }
94
95
0
  virtual Error encode_sequence_flush(heif_encoder* encoder) { return {}; }
96
97
  // Hands out the coded data collected since the previous call and leaves nothing pending
98
  // in the encoder, so that the next call returns std::nullopt until new data has arrived.
99
  // The result may carry only properties (e.g. the codec configuration box that some
100
  // encoders attach at the end of the sequence) and an empty bitstream.
101
0
  virtual std::optional<CodedImageData> encode_sequence_extract_data() { return std::nullopt; }
102
103
0
  virtual std::shared_ptr<Box_VisualSampleEntry> get_sample_description_box(const CodedImageData&) const { return {}; }
104
};
105
106
107
#endif