1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/numerics/safe_conversions.h"
8 #include "media/video/h264_parser.h"
9 #include "third_party/abseil-cpp/absl/types/optional.h"
10 #include "ui/gfx/geometry/rect.h"
11 #include "ui/gfx/geometry/size.h"
13 static volatile size_t volatile_sink;
15 // Entry point for LibFuzzer.
16 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
20 media::H264Parser parser;
21 parser.SetStream(data, base::checked_cast<off_t>(size));
23 // Parse until the end of stream/unsupported stream/error in stream is
27 media::H264Parser::Result res = parser.AdvanceToNextNALU(&nalu);
28 if (res != media::H264Parser::kOk)
31 switch (nalu.nal_unit_type) {
32 case media::H264NALU::kIDRSlice:
33 case media::H264NALU::kNonIDRSlice: {
34 media::H264SliceHeader shdr;
35 res = parser.ParseSliceHeader(nalu, &shdr);
39 case media::H264NALU::kSPS: {
41 res = parser.ParseSPS(&id);
42 if (res != media::H264Parser::kOk)
44 const media::H264SPS* sps = parser.GetSPS(id);
47 // Also test the SPS helper methods. We make sure that the results are
48 // used so that the calls are not optimized away.
49 absl::optional<gfx::Size> coded_size = sps->GetCodedSize();
50 volatile_sink = coded_size.value_or(gfx::Size()).ToString().length();
51 absl::optional<gfx::Rect> visible_rect = sps->GetVisibleRect();
52 volatile_sink = visible_rect.value_or(gfx::Rect()).ToString().length();
56 case media::H264NALU::kPPS: {
58 res = parser.ParsePPS(&id);
62 case media::H264NALU::kSEIMessage: {
63 media::H264SEIMessage sei_msg;
64 res = parser.ParseSEI(&sei_msg);
69 // Skip any other NALU.
72 if (res != media::H264Parser::kOk)