xref: /aosp_15_r20/external/libaom/examples/av1_dec_fuzzer.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2019, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker /*
13*77c1e3ccSAndroid Build Coastguard Worker  * See build_av1_dec_fuzzer.sh for building instructions.
14*77c1e3ccSAndroid Build Coastguard Worker  */
15*77c1e3ccSAndroid Build Coastguard Worker 
16*77c1e3ccSAndroid Build Coastguard Worker #include <stddef.h>
17*77c1e3ccSAndroid Build Coastguard Worker #include <stdint.h>
18*77c1e3ccSAndroid Build Coastguard Worker #include <stdio.h>
19*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
20*77c1e3ccSAndroid Build Coastguard Worker #include <algorithm>
21*77c1e3ccSAndroid Build Coastguard Worker #include <memory>
22*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
23*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_decoder.h"
24*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aomdx.h"
25*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/mem_ops.h"
26*77c1e3ccSAndroid Build Coastguard Worker 
27*77c1e3ccSAndroid Build Coastguard Worker #define IVF_FRAME_HDR_SZ (4 + 8) /* 4 byte size + 8 byte timestamp */
28*77c1e3ccSAndroid Build Coastguard Worker #define IVF_FILE_HDR_SZ 32
29*77c1e3ccSAndroid Build Coastguard Worker 
usage_exit(void)30*77c1e3ccSAndroid Build Coastguard Worker extern "C" void usage_exit(void) { exit(EXIT_FAILURE); }
31*77c1e3ccSAndroid Build Coastguard Worker 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)32*77c1e3ccSAndroid Build Coastguard Worker extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33*77c1e3ccSAndroid Build Coastguard Worker   if (size <= IVF_FILE_HDR_SZ) {
34*77c1e3ccSAndroid Build Coastguard Worker     return 0;
35*77c1e3ccSAndroid Build Coastguard Worker   }
36*77c1e3ccSAndroid Build Coastguard Worker 
37*77c1e3ccSAndroid Build Coastguard Worker   // Abusing the four unused bytes at the end of the IVF file header as a source
38*77c1e3ccSAndroid Build Coastguard Worker   // of random bits.
39*77c1e3ccSAndroid Build Coastguard Worker   unsigned int tile_mode = (data[IVF_FILE_HDR_SZ - 1] & 2) != 0;
40*77c1e3ccSAndroid Build Coastguard Worker   unsigned int ext_tile_debug = (data[IVF_FILE_HDR_SZ - 1] & 4) != 0;
41*77c1e3ccSAndroid Build Coastguard Worker   unsigned int is_annexb = (data[IVF_FILE_HDR_SZ - 1] & 8) != 0;
42*77c1e3ccSAndroid Build Coastguard Worker   int output_all_layers = (data[IVF_FILE_HDR_SZ - 1] & 0x10) != 0;
43*77c1e3ccSAndroid Build Coastguard Worker   int operating_point = data[IVF_FILE_HDR_SZ - 2] & 0x1F;
44*77c1e3ccSAndroid Build Coastguard Worker 
45*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_iface_t *codec_interface = aom_codec_av1_dx();
46*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_ctx_t codec;
47*77c1e3ccSAndroid Build Coastguard Worker   // Set thread count in the range [1, 64].
48*77c1e3ccSAndroid Build Coastguard Worker   const unsigned int threads = (data[IVF_FILE_HDR_SZ] & 0x3f) + 1;
49*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_dec_cfg_t cfg = { threads, 0, 0, !FORCE_HIGHBITDEPTH_DECODING };
50*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_dec_init(&codec, codec_interface, &cfg, 0)) {
51*77c1e3ccSAndroid Build Coastguard Worker     return 0;
52*77c1e3ccSAndroid Build Coastguard Worker   }
53*77c1e3ccSAndroid Build Coastguard Worker   AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1_SET_TILE_MODE, tile_mode);
54*77c1e3ccSAndroid Build Coastguard Worker   AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1D_EXT_TILE_DEBUG, ext_tile_debug);
55*77c1e3ccSAndroid Build Coastguard Worker   AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1D_SET_IS_ANNEXB, is_annexb);
56*77c1e3ccSAndroid Build Coastguard Worker   AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1D_SET_OUTPUT_ALL_LAYERS,
57*77c1e3ccSAndroid Build Coastguard Worker                                 output_all_layers);
58*77c1e3ccSAndroid Build Coastguard Worker   AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1D_SET_OPERATING_POINT,
59*77c1e3ccSAndroid Build Coastguard Worker                                 operating_point);
60*77c1e3ccSAndroid Build Coastguard Worker 
61*77c1e3ccSAndroid Build Coastguard Worker   data += IVF_FILE_HDR_SZ;
62*77c1e3ccSAndroid Build Coastguard Worker   size -= IVF_FILE_HDR_SZ;
63*77c1e3ccSAndroid Build Coastguard Worker 
64*77c1e3ccSAndroid Build Coastguard Worker   while (size > IVF_FRAME_HDR_SZ) {
65*77c1e3ccSAndroid Build Coastguard Worker     size_t frame_size = mem_get_le32(data);
66*77c1e3ccSAndroid Build Coastguard Worker     size -= IVF_FRAME_HDR_SZ;
67*77c1e3ccSAndroid Build Coastguard Worker     data += IVF_FRAME_HDR_SZ;
68*77c1e3ccSAndroid Build Coastguard Worker     frame_size = std::min(size, frame_size);
69*77c1e3ccSAndroid Build Coastguard Worker 
70*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_stream_info_t stream_info;
71*77c1e3ccSAndroid Build Coastguard Worker     stream_info.is_annexb = is_annexb;
72*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_err_t err =
73*77c1e3ccSAndroid Build Coastguard Worker         aom_codec_peek_stream_info(codec_interface, data, size, &stream_info);
74*77c1e3ccSAndroid Build Coastguard Worker     static_cast<void>(err);
75*77c1e3ccSAndroid Build Coastguard Worker 
76*77c1e3ccSAndroid Build Coastguard Worker     err = aom_codec_decode(&codec, data, frame_size, nullptr);
77*77c1e3ccSAndroid Build Coastguard Worker     static_cast<void>(err);
78*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_iter_t iter = nullptr;
79*77c1e3ccSAndroid Build Coastguard Worker     aom_image_t *img = nullptr;
80*77c1e3ccSAndroid Build Coastguard Worker     while ((img = aom_codec_get_frame(&codec, &iter)) != nullptr) {
81*77c1e3ccSAndroid Build Coastguard Worker     }
82*77c1e3ccSAndroid Build Coastguard Worker     data += frame_size;
83*77c1e3ccSAndroid Build Coastguard Worker     size -= frame_size;
84*77c1e3ccSAndroid Build Coastguard Worker   }
85*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_destroy(&codec);
86*77c1e3ccSAndroid Build Coastguard Worker   return 0;
87*77c1e3ccSAndroid Build Coastguard Worker }
88