xref: /aosp_15_r20/external/webrtc/test/fuzzers/rtp_packetizer_av1_fuzzer.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 #include <stddef.h>
11 #include <stdint.h>
12 
13 #include "api/video/video_frame_type.h"
14 #include "modules/rtp_rtcp/source/rtp_format.h"
15 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
16 #include "modules/rtp_rtcp/source/rtp_packetizer_av1.h"
17 #include "rtc_base/checks.h"
18 #include "test/fuzzers/fuzz_data_helper.h"
19 
20 namespace webrtc {
FuzzOneInput(const uint8_t * data,size_t size)21 void FuzzOneInput(const uint8_t* data, size_t size) {
22   test::FuzzDataHelper fuzz_input(rtc::MakeArrayView(data, size));
23 
24   RtpPacketizer::PayloadSizeLimits limits;
25   limits.max_payload_len = 1200;
26   // Read uint8_t to be sure reduction_lens are much smaller than
27   // max_payload_len and thus limits structure is valid.
28   limits.first_packet_reduction_len = fuzz_input.ReadOrDefaultValue<uint8_t>(0);
29   limits.last_packet_reduction_len = fuzz_input.ReadOrDefaultValue<uint8_t>(0);
30   limits.single_packet_reduction_len =
31       fuzz_input.ReadOrDefaultValue<uint8_t>(0);
32   const VideoFrameType kFrameTypes[] = {VideoFrameType::kVideoFrameKey,
33                                         VideoFrameType::kVideoFrameDelta};
34   VideoFrameType frame_type = fuzz_input.SelectOneOf(kFrameTypes);
35 
36   // Main function under test: RtpPacketizerAv1's constructor.
37   RtpPacketizerAv1 packetizer(fuzz_input.ReadByteArray(fuzz_input.BytesLeft()),
38                               limits, frame_type,
39                               /*is_last_frame_in_picture=*/true);
40 
41   size_t num_packets = packetizer.NumPackets();
42   if (num_packets == 0) {
43     return;
44   }
45   // When packetization was successful, validate NextPacket function too.
46   // While at it, check that packets respect the payload size limits.
47   RtpPacketToSend rtp_packet(nullptr);
48   // Single packet.
49   if (num_packets == 1) {
50     RTC_CHECK(packetizer.NextPacket(&rtp_packet));
51     RTC_CHECK_LE(rtp_packet.payload_size(),
52                  limits.max_payload_len - limits.single_packet_reduction_len);
53     return;
54   }
55   // First packet.
56   RTC_CHECK(packetizer.NextPacket(&rtp_packet));
57   RTC_CHECK_LE(rtp_packet.payload_size(),
58                limits.max_payload_len - limits.first_packet_reduction_len);
59   // Middle packets.
60   for (size_t i = 1; i < num_packets - 1; ++i) {
61     RTC_CHECK(packetizer.NextPacket(&rtp_packet))
62         << "Failed to get packet#" << i;
63     RTC_CHECK_LE(rtp_packet.payload_size(), limits.max_payload_len)
64         << "Packet #" << i << " exceeds it's limit";
65   }
66   // Last packet.
67   RTC_CHECK(packetizer.NextPacket(&rtp_packet));
68   RTC_CHECK_LE(rtp_packet.payload_size(),
69                limits.max_payload_len - limits.last_packet_reduction_len);
70 }
71 }  // namespace webrtc
72