Coverage Report

Created: 2025-10-12 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libheif/libheif/codecs/avif_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 "avif_enc.h"
22
#include "avif_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_AVIF::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
  Box_av1C::configuration config;
38
39
  // Fill preliminary av1C in case we cannot parse the sequence_header() correctly in the code below.
40
  // TODO: maybe we can remove this later.
41
0
  fill_av1C_configuration(&config, image);
42
43
0
  heif_image c_api_image;
44
0
  c_api_image.image = image;
45
46
0
  struct heif_error err = encoder->plugin->encode_image(encoder->encoder, &c_api_image, input_class);
47
0
  if (err.code) {
48
0
    return Error(err.code,
49
0
                 err.subcode,
50
0
                 err.message);
51
0
  }
52
53
0
  for (;;) {
54
0
    uint8_t* data;
55
0
    int size;
56
57
0
    encoder->plugin->get_compressed_data(encoder->encoder, &data, &size, nullptr);
58
59
0
    bool found_config = fill_av1C_configuration_from_stream(&config, data, size);
60
0
    (void) found_config;
61
62
0
    if (data == nullptr) {
63
0
      break;
64
0
    }
65
66
0
    codedImage.append(data, size);
67
0
  }
68
69
0
  auto av1C = std::make_shared<Box_av1C>();
70
0
  av1C->set_configuration(config);
71
0
  codedImage.properties.push_back(av1C);
72
73
0
  codedImage.codingConstraints.intra_pred_used = true;
74
0
  codedImage.codingConstraints.all_ref_pics_intra = true; // TODO: change when we use predicted frames
75
76
0
  return codedImage;
77
0
}
78
79
80
std::shared_ptr<class Box_VisualSampleEntry> Encoder_AVIF::get_sample_description_box(const CodedImageData& data) const
81
0
{
82
0
  auto av01 = std::make_shared<Box_av01>();
83
0
  av01->get_VisualSampleEntry().compressorname = "AVIF";
84
85
0
  for (auto prop : data.properties) {
86
0
    if (prop->get_short_type() == fourcc("av1C")) {
87
0
      av01->append_child_box(prop);
88
0
      return av01;
89
0
    }
90
0
  }
91
92
0
  assert(false); // no av1C generated
93
0
  return nullptr;
94
0
}