| 1 |
/* |
| 2 |
* HEIF codec. |
| 3 |
* Copyright (c) 2018 struktur AG, Joachim Bauch <[email protected]> |
| 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 <assert.h> |
| 22 |
#include <string.h> |
| 23 |
#include <stdlib.h> |
| 24 |
|
| 25 |
#include <memory> |
| 26 |
|
| 27 |
#include "libheif/heif.h" |
| 28 |
|
| 29 |
static void generate_plane(int width, int height, uint8_t* output, int stride) |
| 30 |
{ |
| 31 |
// TODO(fancycode): Fill with random data. |
| 32 |
if (width == stride) { |
| 33 |
memset(output, 0, width * height); |
| 34 |
} |
| 35 |
else { |
| 36 |
for (int y = 0; y < height; y++) { |
| 37 |
memset(output, 0, width); |
| 38 |
output += stride; |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
static size_t create_image(const uint8_t* data, size_t size, struct heif_image** image) |
| 44 |
{ |
| 45 |
if (size < 2) { |
| 46 |
return 0; |
| 47 |
} |
| 48 |
|
| 49 |
int width = data[0] + 16; |
| 50 |
int height = data[1] + 16; |
| 51 |
data += 2; |
| 52 |
size -= 2; |
| 53 |
// TODO(fancycode): Get colorspace/chroma from fuzzing input. |
| 54 |
heif_colorspace colorspace = heif_colorspace_YCbCr; |
| 55 |
heif_chroma chroma = heif_chroma_420; |
| 56 |
|
| 57 |
struct heif_error err = heif_image_create(width, height, colorspace, chroma, image); |
| 58 |
if (err.code != heif_error_Ok) { |
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
int chroma_width = (width+1)/2; |
| 63 |
int chroma_height = (height+1)/2; |
| 64 |
|
| 65 |
err = heif_image_add_plane(*image, heif_channel_Y, width, height, 8); |
| 66 |
assert(err.code == heif_error_Ok); |
| 67 |
err = heif_image_add_plane(*image, heif_channel_Cb, chroma_width, chroma_height, 8); |
| 68 |
assert(err.code == heif_error_Ok); |
| 69 |
err = heif_image_add_plane(*image, heif_channel_Cr, chroma_width, chroma_height, 8); |
| 70 |
assert(err.code == heif_error_Ok); |
| 71 |
|
| 72 |
int stride; |
| 73 |
uint8_t* plane; |
| 74 |
|
| 75 |
plane = heif_image_get_plane(*image, heif_channel_Y, &stride); |
| 76 |
generate_plane(width, height, plane, stride); |
| 77 |
|
| 78 |
plane = heif_image_get_plane(*image, heif_channel_Cb, &stride); |
| 79 |
generate_plane(chroma_width, chroma_height, plane, stride); |
| 80 |
|
| 81 |
plane = heif_image_get_plane(*image, heif_channel_Cr, &stride); |
| 82 |
generate_plane(chroma_width, chroma_height, plane, stride); |
| 83 |
|
| 84 |
return 2; |
| 85 |
} |
| 86 |
|
| 87 |
class MemoryWriter |
| 88 |
{ |
| 89 |
public: |
| 90 |
MemoryWriter() : data_(nullptr), size_(0), capacity_(0) |
| 91 |
{} |
| 92 |
|
| 93 |
~MemoryWriter() |
| 94 |
{ |
| 95 |
free(data_); |
| 96 |
} |
| 97 |
|
| 98 |
const uint8_t* data() const |
| 99 |
{ return data_; } |
| 100 |
|
| 101 |
size_t size() const |
| 102 |
{ return size_; } |
| 103 |
|
| 104 |
void write(const void* data, size_t size) |
| 105 |
{ |
| 106 |
if (capacity_ - size_ < size) { |
| 107 |
size_t new_capacity = capacity_ + size; |
| 108 |
uint8_t* new_data = static_cast<uint8_t*>(malloc(new_capacity)); |
| 109 |
assert(new_data); |
| 110 |
if (data_) { |
| 111 |
memcpy(new_data, data_, size_); |
| 112 |
free(data_); |
| 113 |
} |
| 114 |
data_ = new_data; |
| 115 |
capacity_ = new_capacity; |
| 116 |
} |
| 117 |
memcpy(&data_[size_], data, size); |
| 118 |
size_ += size; |
| 119 |
} |
| 120 |
|
| 121 |
public: |
| 122 |
uint8_t* data_; |
| 123 |
size_t size_; |
| 124 |
size_t capacity_; |
| 125 |
}; |
| 126 |
|
| 127 |
static struct heif_error writer_write(struct heif_context* ctx, const void* data, size_t size, void* userdata) |
| 128 |
{ |
| 129 |
MemoryWriter* writer = static_cast<MemoryWriter*>(userdata); |
| 130 |
writer->write(data, size); |
| 131 |
struct heif_error err{heif_error_Ok, heif_suberror_Unspecified, nullptr}; |
| 132 |
return err; |
| 133 |
} |
| 134 |
|
| 135 |
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) |
| 136 |
{ |
| 137 |
struct heif_error err; |
| 138 |
std::shared_ptr<heif_context> context(heif_context_alloc(), |
| 139 |
[](heif_context* c) { heif_context_free(c); }); |
| 140 |
assert(context); |
| 141 |
|
| 142 |
if (size < 2) { |
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
int quality = (data[0] & 0x7F) % 101; |
| 147 |
bool lossless = (data[1] & 0x80); |
| 148 |
bool use_avif = (data[1] & 0x40); |
| 149 |
data += 2; |
| 150 |
size -= 2; |
| 151 |
|
| 152 |
static const size_t kMaxEncoders = 5; |
| 153 |
const heif_encoder_descriptor* encoder_descriptors[kMaxEncoders]; |
| 154 |
int count = heif_get_encoder_descriptors(use_avif ? heif_compression_AV1 : heif_compression_HEVC, |
| 155 |
nullptr, |
| 156 |
encoder_descriptors, kMaxEncoders); |
| 157 |
assert(count >= 0); |
| 158 |
if (count == 0) { |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
heif_encoder* encoder; |
| 163 |
err = heif_context_get_encoder(context.get(), encoder_descriptors[0], &encoder); |
| 164 |
if (err.code != heif_error_Ok) { |
| 165 |
return 0; |
| 166 |
} |
| 167 |
|
| 168 |
heif_encoder_set_lossy_quality(encoder, quality); |
| 169 |
heif_encoder_set_lossless(encoder, lossless); |
| 170 |
|
| 171 |
struct heif_image* image = nullptr; |
| 172 |
size_t read = create_image(data, size, &image); |
| 173 |
assert(read <= size); |
| 174 |
if (!read) { |
| 175 |
heif_image_release(image); |
| 176 |
heif_encoder_release(encoder); |
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
data += read; |
| 181 |
size -= read; |
| 182 |
|
| 183 |
struct heif_image_handle* img; |
| 184 |
err = heif_context_encode_image(context.get(), image, encoder, nullptr, &img); |
| 185 |
heif_image_release(image); |
| 186 |
heif_encoder_release(encoder); |
| 187 |
heif_image_handle_release(img); |
| 188 |
if (err.code != heif_error_Ok) { |
| 189 |
return 0; |
| 190 |
} |
| 191 |
|
| 192 |
MemoryWriter writer; |
| 193 |
struct heif_writer w; |
| 194 |
w.writer_api_version = 1; |
| 195 |
w.write = writer_write; |
| 196 |
heif_context_write(context.get(), &w, &writer); |
| 197 |
assert(writer.size() > 0); |
| 198 |
return 0; |
| 199 |
} |