1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86

media / gpu / mac / video_toolbox_av1_accelerator.h [blame]

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_GPU_MAC_VIDEO_TOOLBOX_AV1_ACCELERATOR_H_
#define MEDIA_GPU_MAC_VIDEO_TOOLBOX_AV1_ACCELERATOR_H_

#include <CoreMedia/CoreMedia.h>
#include <stdint.h>

#include <memory>
#include <optional>

#include "base/apple/scoped_cftyperef.h"
#include "base/functional/callback.h"
#include "base/memory/scoped_refptr.h"
#include "base/sequence_checker.h"
#include "media/base/video_codecs.h"
#include "media/base/video_color_space.h"
#include "media/gpu/av1_decoder.h"
#include "media/gpu/mac/video_toolbox_decompression_metadata.h"
#include "media/gpu/media_gpu_export.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/hdr_metadata.h"

namespace media {

class MediaLog;

class MEDIA_GPU_EXPORT VideoToolboxAV1Accelerator
    : public AV1Decoder::AV1Accelerator {
 public:
  using DecodeCB = base::RepeatingCallback<void(
      base::apple::ScopedCFTypeRef<CMSampleBufferRef>,
      VideoToolboxDecompressionSessionMetadata,
      scoped_refptr<CodecPicture>)>;
  using OutputCB = base::RepeatingCallback<void(scoped_refptr<CodecPicture>)>;

  VideoToolboxAV1Accelerator(std::unique_ptr<MediaLog> media_log,
                             std::optional<gfx::HDRMetadata> hdr_metadata,
                             DecodeCB decode_cb,
                             OutputCB output_cb);
  ~VideoToolboxAV1Accelerator() override;

  // AV1Accelerator implementation.
  scoped_refptr<AV1Picture> CreateAV1Picture(bool apply_grain) override;
  Status SubmitDecode(const AV1Picture& pic,
                      const libgav1::ObuSequenceHeader& sequence_header,
                      const AV1ReferenceFrameVector& ref_frames,
                      const libgav1::Vector<libgav1::TileBuffer>& tile_buffers,
                      base::span<const uint8_t> data) override;
  bool OutputPicture(const AV1Picture& pic) override;
  Status SetStream(base::span<const uint8_t> stream,
                   const DecryptConfig* decrypt_config) override;

 private:
  bool ProcessFormat(const AV1Picture& pic,
                     const libgav1::ObuSequenceHeader& sequence_header,
                     base::span<const uint8_t> data);

  std::unique_ptr<MediaLog> media_log_;
  std::optional<gfx::HDRMetadata> hdr_metadata_;

  // Callbacks are called synchronously, which is always re-entrant.
  DecodeCB decode_cb_;
  OutputCB output_cb_;

  // Parameters of the active format.
  VideoColorSpace active_color_space_;
  VideoCodecProfile active_profile_ = VIDEO_CODEC_PROFILE_UNKNOWN;
  std::optional<gfx::HDRMetadata> active_hdr_metadata_;
  gfx::Size active_coded_size_;

  base::apple::ScopedCFTypeRef<CMFormatDescriptionRef> active_format_;
  VideoToolboxDecompressionSessionMetadata session_metadata_;

  // Data for the current frame.
  base::apple::ScopedCFTypeRef<CMBlockBufferRef> temporal_unit_data_;
  bool format_processed_ = false;

  SEQUENCE_CHECKER(sequence_checker_);
};

}  // namespace media

#endif  // MEDIA_GPU_MAC_VIDEO_TOOLBOX_AV1_ACCELERATOR_H_