Coverage Report

Created: 2026-01-17 06:10

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