Coverage Report

Created: 2026-04-01 07:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libheif/libheif/codecs/avc_enc.cc
Line
Count
Source
1
/*
2
 * HEIF codec.
3
 * Copyright (c) 2025 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 "avc_enc.h"
22
#include "avc_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
// TODO: can we use the new sequences interface for this to avoid duplicate code.
33
Result<Encoder::CodedImageData> Encoder_AVC::encode(const std::shared_ptr<HeifPixelImage>& image,
34
                                                    heif_encoder* encoder,
35
                                                    const heif_encoding_options& options,
36
                                                    heif_image_input_class input_class)
37
0
{
38
0
  CodedImageData codedImage;
39
40
0
  auto avcC = std::make_shared<Box_avcC>();
41
42
0
  heif_image c_api_image;
43
0
  c_api_image.image = image;
44
45
0
  heif_error err = encoder->plugin->encode_image(encoder->encoder, &c_api_image, input_class);
46
0
  if (err.code) {
47
0
    return Error(err.code,
48
0
                 err.subcode,
49
0
                 err.message);
50
0
  }
51
52
0
  int encoded_width = 0;
53
0
  int encoded_height = 0;
54
55
0
  for (;;) {
56
0
    uint8_t* data;
57
0
    int size;
58
59
0
    err = encoder->plugin->get_compressed_data(encoder->encoder, &data, &size, nullptr);
60
0
    if (err.code) {
61
0
      return Error(err.code, err.subcode, err.message);
62
0
    }
63
64
0
    if (data == nullptr) {
65
0
      break;
66
0
    }
67
68
0
    uint8_t nal_type = data[0] & 0x1f;
69
70
0
    if (nal_type == AVC_NAL_UNIT_SPS_NUT) {
71
0
      parse_sps_for_avcC_configuration(data, size, &avcC->get_configuration(), &encoded_width, &encoded_height);
72
73
0
      codedImage.encoded_image_width = encoded_width;
74
0
      codedImage.encoded_image_height = encoded_height;
75
0
    }
76
77
0
    switch (nal_type) {
78
0
      case AVC_NAL_UNIT_SPS_NUT:
79
0
        avcC->append_sps_nal(data, size);
80
0
        break;
81
0
      case AVC_NAL_UNIT_SPS_EXT_NUT:
82
0
        avcC->append_sps_ext_nal(data, size);
83
0
        break;
84
0
      case AVC_NAL_UNIT_PPS_NUT:
85
0
        avcC->append_pps_nal(data, size);
86
0
        break;
87
88
0
      default:
89
0
        codedImage.append_with_4bytes_size(data, size);
90
0
    }
91
0
  }
92
93
0
  if (!encoded_width || !encoded_height) {
94
0
    return Error(heif_error_Encoder_plugin_error,
95
0
                 heif_suberror_Invalid_image_size);
96
0
  }
97
98
0
  codedImage.properties.push_back(avcC);
99
100
101
  // Make sure that the encoder plugin works correctly and the encoded image has the correct size.
102
103
0
  if (encoder->plugin->plugin_api_version >= 3 &&
104
0
      encoder->plugin->query_encoded_size != nullptr) {
105
0
    uint32_t check_encoded_width = image->get_width(), check_encoded_height = image->get_height();
106
107
0
    encoder->plugin->query_encoded_size(encoder->encoder,
108
0
                                        image->get_width(), image->get_height(),
109
0
                                        &check_encoded_width,
110
0
                                        &check_encoded_height);
111
112
0
    assert((int)check_encoded_width == encoded_width);
113
0
    assert((int)check_encoded_height == encoded_height);
114
0
  }
115
116
0
  codedImage.codingConstraints.intra_pred_used = true;
117
0
  codedImage.codingConstraints.all_ref_pics_intra = true; // TODO: change when we use predicted frames
118
119
0
  return codedImage;
120
0
}
121
122
123
Error Encoder_AVC::encode_sequence_frame(const std::shared_ptr<HeifPixelImage>& image,
124
                                         heif_encoder* encoder,
125
                                         const heif_sequence_encoding_options& options,
126
                                         heif_image_input_class input_class,
127
                                         uint32_t framerate_num, uint32_t framerate_denom,
128
                                         uintptr_t frame_number)
129
0
{
130
0
  heif_image c_api_image;
131
0
  c_api_image.image = image;
132
133
0
  if (!m_encoder_active) {
134
0
    heif_error err = encoder->plugin->start_sequence_encoding(encoder->encoder, &c_api_image,
135
0
                                                              input_class,
136
0
                                                              framerate_num, framerate_denom,
137
0
                                                              &options);
138
0
    if (err.code) {
139
0
      return {
140
0
        err.code,
141
0
        err.subcode,
142
0
        err.message
143
0
      };
144
0
    }
145
146
0
    m_avcC = std::make_shared<Box_avcC>();
147
0
    m_encoder_active = true;
148
0
  }
149
150
0
  Error dataErr = get_data(encoder);
151
0
  if (dataErr) {
152
0
    return dataErr;
153
0
  }
154
155
0
  heif_error err = encoder->plugin->encode_sequence_frame(encoder->encoder, &c_api_image, frame_number);
156
0
  if (err.code) {
157
0
    return {
158
0
      err.code,
159
0
      err.subcode,
160
0
      err.message
161
0
    };
162
0
  }
163
164
0
  return get_data(encoder);
165
0
}
166
167
168
Error Encoder_AVC::encode_sequence_flush(heif_encoder* encoder)
169
0
{
170
0
  encoder->plugin->end_sequence_encoding(encoder->encoder);
171
0
  m_encoder_active = false;
172
0
  m_end_of_sequence_reached = true;
173
174
0
  return get_data(encoder);
175
0
}
176
177
178
std::optional<Encoder::CodedImageData> Encoder_AVC::encode_sequence_get_data()
179
0
{
180
0
  if (m_output_image_complete) {
181
0
    m_output_image_complete = false;
182
0
    return std::move(m_current_output_data);
183
0
  }
184
0
  else {
185
0
    return std::nullopt;
186
0
  }
187
0
}
188
189
Error Encoder_AVC::get_data(heif_encoder* encoder)
190
0
{
191
  //CodedImageData codedImage;
192
193
0
  for (;;) {
194
0
    uint8_t* data;
195
0
    int size;
196
197
0
    uintptr_t frameNr=0;
198
0
    int more_frame_packets = 1;
199
0
    struct heif_error err = encoder->plugin->get_compressed_data2(encoder->encoder, &data, &size, &frameNr, nullptr, &more_frame_packets);
200
0
    if (err.code) {
201
0
      return Error(err.code, err.subcode, err.message);
202
0
    }
203
204
0
    if (data == nullptr) {
205
0
      break;
206
0
    }
207
208
0
    const uint8_t nal_type = (data[0] & 0x1f);
209
0
    const bool is_sync = (nal_type == 5);
210
0
    const bool is_image_data = (nal_type > 0 && nal_type <= AVC_NAL_UNIT_MAX_VCL);
211
212
0
    m_output_image_complete |= is_image_data;
213
214
    // std::cout << "received frameNr=" << frameNr << " nal_type:" << ((int)nal_type) << " size: " << size << "\n";
215
216
0
    if (nal_type == AVC_NAL_UNIT_SPS_NUT && m_avcC) {
217
0
      parse_sps_for_avcC_configuration(data, size,
218
0
                                       &m_avcC->get_configuration(),
219
0
                                       &m_encoded_image_width, &m_encoded_image_height);
220
0
    }
221
222
0
    if (is_image_data) {
223
      // more_frame_packets = 0;
224
0
    }
225
226
0
    switch (nal_type) {
227
0
      case AVC_NAL_UNIT_SPS_NUT:
228
0
        if (m_avcC && !m_avcC_has_SPS) m_avcC->append_sps_nal(data, size);
229
0
        m_avcC_has_SPS = true;
230
0
        break;
231
232
0
      case AVC_NAL_UNIT_SPS_EXT_NUT:
233
0
        if (m_avcC /*&& !m_avcC_has_SPS*/) m_avcC->append_sps_ext_nal(data, size);
234
        //m_avcC_has_SPS = true;
235
0
        break;
236
237
0
      case AVC_NAL_UNIT_PPS_NUT:
238
0
        if (m_avcC && !m_avcC_has_PPS) m_avcC->append_pps_nal(data, size);
239
0
        m_avcC_has_PPS = true;
240
0
        break;
241
242
0
      default:
243
0
        if (!m_current_output_data) {
244
0
          m_current_output_data = CodedImageData{};
245
0
        }
246
0
        m_current_output_data->append_with_4bytes_size(data, size);
247
248
0
        if (is_image_data) {
249
0
          m_current_output_data->is_sync_frame = is_sync;
250
0
          m_current_output_data->frame_nr = frameNr;
251
0
        }
252
0
    }
253
254
0
    if (!more_frame_packets) {
255
0
      break;
256
0
    }
257
0
  }
258
259
0
  if (!m_output_image_complete) {
260
0
    return {};
261
0
  }
262
263
0
  if (!m_encoded_image_width || !m_encoded_image_height) {
264
0
    return Error(heif_error_Encoder_plugin_error,
265
0
                 heif_suberror_Invalid_image_size);
266
0
  }
267
268
269
  // --- return avcC when all headers are included and it was not returned yet
270
  //     TODO: it's maybe better to return this at the end so that we are sure to have all headers
271
  //           and also complete codingConstraints.
272
273
0
  if (m_end_of_sequence_reached && m_avcC && !m_avcC_sent) {
274
0
    m_current_output_data->properties.push_back(m_avcC);
275
0
    m_avcC = nullptr;
276
0
    m_avcC_sent = true;
277
0
  }
278
279
0
  m_current_output_data->encoded_image_width = m_encoded_image_width;
280
0
  m_current_output_data->encoded_image_height = m_encoded_image_height;
281
282
0
  m_current_output_data->codingConstraints.intra_pred_used = true;
283
0
  m_current_output_data->codingConstraints.all_ref_pics_intra = true; // TODO: change when we use predicted frames
284
285
0
  return {};
286
0
}
287
288
289
std::shared_ptr<Box_VisualSampleEntry> Encoder_AVC::get_sample_description_box(const CodedImageData& data) const
290
0
{
291
0
  auto avc1 = std::make_shared<Box_avc1>();
292
0
  avc1->get_VisualSampleEntry().compressorname = "AVC";
293
294
0
  for (auto prop : data.properties) {
295
0
    if (prop->get_short_type() == fourcc("avcC")) {
296
0
      avc1->append_child_box(prop);
297
0
      return avc1;
298
0
    }
299
0
  }
300
301
  // box not yet available
302
0
  return nullptr;
303
0
}