1 // Copyright 2019 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.
4 
5 #ifndef MEDIA_GPU_VAAPI_VAAPI_JPEG_DECODER_H_
6 #define MEDIA_GPU_VAAPI_VAAPI_JPEG_DECODER_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 
12 #include "base/macros.h"
13 #include "media/gpu/vaapi/vaapi_image_decoder.h"
14 
15 namespace media {
16 namespace fuzzing {
17 class VaapiJpegDecoderWrapper;
18 }  // namespace fuzzing
19 
20 struct JpegFrameHeader;
21 struct JpegParseResult;
22 class ScopedVAImage;
23 
24 // Returns the internal format required for a JPEG image given its parsed
25 // |frame_header|. If the image's subsampling format is not one of 4:2:0, 4:2:2,
26 // or 4:4:4, returns kInvalidVaRtFormat.
27 unsigned int VaSurfaceFormatForJpeg(const JpegFrameHeader& frame_header);
28 
29 class VaapiJpegDecoder : public VaapiImageDecoder {
30  public:
31   VaapiJpegDecoder();
32   ~VaapiJpegDecoder() override;
33 
34   // VaapiImageDecoder implementation.
35   gpu::ImageDecodeAcceleratorType GetType() const override;
36   SkYUVColorSpace GetYUVColorSpace() const override;
37 
38   // Get the decoded data from the last Decode() call as a ScopedVAImage. The
39   // VAImage's format will be either |preferred_image_fourcc| if the conversion
40   // from the internal format is supported or a fallback FOURCC (see
41   // VaapiWrapper::GetJpegDecodeSuitableImageFourCC() for details). Returns
42   // nullptr on failure and sets *|status| to the reason for failure.
43   std::unique_ptr<ScopedVAImage> GetImage(uint32_t preferred_image_fourcc,
44                                           VaapiImageDecodeStatus* status);
45 
46  private:
47   friend class fuzzing::VaapiJpegDecoderWrapper;
48 
49   // VaapiImageDecoder implementation.
50   VaapiImageDecodeStatus AllocateVASurfaceAndSubmitVABuffers(
51       base::span<const uint8_t> encoded_image) override;
52 
53   // AllocateVASurfaceAndSubmitVABuffers() is implemented by calling the
54   // following methods. They are here so that a fuzzer can inject (almost)
55   // arbitrary data into libva by skipping the parsing and image support checks
56   // in AllocateVASurfaceAndSubmitVABuffers().
57   bool MaybeCreateSurface(unsigned int picture_va_rt_format,
58                           const gfx::Size& new_coded_size,
59                           const gfx::Size& new_visible_size);
60   bool SubmitBuffers(const JpegParseResult& parse_result);
61 
62   DISALLOW_COPY_AND_ASSIGN(VaapiJpegDecoder);
63 };
64 
65 }  // namespace media
66 
67 #endif  // MEDIA_GPU_VAAPI_VAAPI_JPEG_DECODER_H_
68