summaryrefslogtreecommitdiffstats
path: root/dom/media/webcodecs/DecoderTypes.h
blob: 4817a66f179042e5f43ae987ead779f99b7a07d0 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_DecoderTypes_h
#define mozilla_dom_DecoderTypes_h

#include "MediaData.h"
#include "mozilla/Maybe.h"
#include "mozilla/dom/AudioData.h"
#include "mozilla/dom/AudioDecoderBinding.h"
#include "mozilla/dom/EncodedAudioChunk.h"
#include "mozilla/dom/EncodedVideoChunk.h"
#include "mozilla/dom/VideoColorSpaceBinding.h"
#include "mozilla/dom/VideoDecoderBinding.h"
#include "mozilla/dom/VideoFrame.h"
#include "nsStringFwd.h"
#include "nsTLiteralString.h"

namespace mozilla {

class TrackInfo;
class MediaByteBuffer;

namespace dom {

struct VideoColorSpaceInternal {
  explicit VideoColorSpaceInternal(const VideoColorSpaceInit& aColorSpaceInit);
  VideoColorSpaceInternal() = default;
  VideoColorSpaceInit ToColorSpaceInit() const;

  bool operator==(const VideoColorSpaceInternal& aOther) const {
    return mFullRange == aOther.mFullRange && mMatrix == aOther.mMatrix &&
           mPrimaries == aOther.mPrimaries && mTransfer == aOther.mTransfer;
  }

  Maybe<bool> mFullRange;
  Maybe<VideoMatrixCoefficients> mMatrix;
  Maybe<VideoColorPrimaries> mPrimaries;
  Maybe<VideoTransferCharacteristics> mTransfer;
};

class VideoDecoderConfigInternal {
 public:
  static UniquePtr<VideoDecoderConfigInternal> Create(
      const VideoDecoderConfig& aConfig);
  VideoDecoderConfigInternal(const nsAString& aCodec,
                             Maybe<uint32_t>&& aCodedHeight,
                             Maybe<uint32_t>&& aCodedWidth,
                             Maybe<VideoColorSpaceInternal>&& aColorSpace,
                             already_AddRefed<MediaByteBuffer> aDescription,
                             Maybe<uint32_t>&& aDisplayAspectHeight,
                             Maybe<uint32_t>&& aDisplayAspectWidth,
                             const HardwareAcceleration& aHardwareAcceleration,
                             Maybe<bool>&& aOptimizeForLatency);
  ~VideoDecoderConfigInternal() = default;

  nsCString ToString() const;

  bool Equals(const VideoDecoderConfigInternal& aOther) const {
    if (mDescription != aOther.mDescription) {
      return false;
    }
    if (mDescription && aOther.mDescription) {
      auto lhs = mDescription;
      auto rhs = aOther.mDescription;
      if (lhs->Length() != rhs->Length()) {
        return false;
      }
      if (!ArrayEqual(lhs->Elements(), rhs->Elements(), lhs->Length())) {
        return false;
      }
    }
    return mCodec.Equals(aOther.mCodec) &&
           mCodedHeight == aOther.mCodedHeight &&
           mCodedWidth == aOther.mCodedWidth &&
           mColorSpace == aOther.mColorSpace &&
           mDisplayAspectHeight == aOther.mDisplayAspectWidth &&
           mHardwareAcceleration == aOther.mHardwareAcceleration &&
           mOptimizeForLatency == aOther.mOptimizeForLatency;
  }

  nsString mCodec;
  Maybe<uint32_t> mCodedHeight;
  Maybe<uint32_t> mCodedWidth;
  Maybe<VideoColorSpaceInternal> mColorSpace;
  RefPtr<MediaByteBuffer> mDescription;
  Maybe<uint32_t> mDisplayAspectHeight;
  Maybe<uint32_t> mDisplayAspectWidth;
  HardwareAcceleration mHardwareAcceleration;
  Maybe<bool> mOptimizeForLatency;
};

class VideoDecoderTraits {
 public:
  static constexpr nsLiteralCString Name = "VideoDecoder"_ns;
  using ConfigType = VideoDecoderConfig;
  using ConfigTypeInternal = VideoDecoderConfigInternal;
  using InputType = EncodedVideoChunk;
  using InputTypeInternal = EncodedVideoChunkData;
  using OutputType = VideoFrame;
  using OutputCallbackType = VideoFrameOutputCallback;

  static bool IsSupported(const ConfigTypeInternal& aConfig);
  static Result<UniquePtr<TrackInfo>, nsresult> CreateTrackInfo(
      const ConfigTypeInternal& aConfig);
  static bool Validate(const ConfigType& aConfig, nsCString& aErrorMessage);
  static UniquePtr<ConfigTypeInternal> CreateConfigInternal(
      const ConfigType& aConfig);
  static bool IsKeyChunk(const InputType& aInput);
  static UniquePtr<InputTypeInternal> CreateInputInternal(
      const InputType& aInput);
};

class AudioDecoderConfigInternal {
 public:
  AudioDecoderConfigInternal(const nsAString& aCodec, uint32_t aSampleRate,
                             uint32_t aNumberOfChannels,
                             already_AddRefed<MediaByteBuffer> aDescription);
  static UniquePtr<AudioDecoderConfigInternal> Create(
      const AudioDecoderConfig& aConfig);
  ~AudioDecoderConfigInternal() = default;

  bool Equals(const AudioDecoderConfigInternal& aOther) const {
    if (mDescription != aOther.mDescription) {
      return false;
    }
    if (mDescription && aOther.mDescription) {
      auto lhs = mDescription;
      auto rhs = aOther.mDescription;
      if (lhs->Length() != rhs->Length()) {
        return false;
      }
      if (!ArrayEqual(lhs->Elements(), rhs->Elements(), lhs->Length())) {
        return false;
      }
    }
    return mCodec.Equals(aOther.mCodec) && mSampleRate == aOther.mSampleRate &&
           mNumberOfChannels == aOther.mNumberOfChannels &&
           mOptimizeForLatency == aOther.mOptimizeForLatency;
  }
  nsCString ToString() const;

  nsString mCodec;
  uint32_t mSampleRate;
  uint32_t mNumberOfChannels;
  RefPtr<MediaByteBuffer> mDescription;
  // Compilation fix, should be abstracted by DecoderAgent since those are not
  // supported
  HardwareAcceleration mHardwareAcceleration =
      HardwareAcceleration::No_preference;
  Maybe<bool> mOptimizeForLatency;
};

class AudioDecoderTraits {
 public:
  static constexpr nsLiteralCString Name = "AudioDecoder"_ns;
  using ConfigType = AudioDecoderConfig;
  using ConfigTypeInternal = AudioDecoderConfigInternal;
  using InputType = EncodedAudioChunk;
  using InputTypeInternal = EncodedAudioChunkData;
  using OutputType = AudioData;
  using OutputCallbackType = AudioDataOutputCallback;

  static bool IsSupported(const ConfigTypeInternal& aConfig);
  static Result<UniquePtr<TrackInfo>, nsresult> CreateTrackInfo(
      const ConfigTypeInternal& aConfig);
  static bool Validate(const ConfigType& aConfig, nsCString& aErrorMessage);
  static UniquePtr<ConfigTypeInternal> CreateConfigInternal(
      const ConfigType& aConfig);
  static bool IsKeyChunk(const InputType& aInput);
  static UniquePtr<InputTypeInternal> CreateInputInternal(
      const InputType& aInput);
};

}  // namespace dom
}  // namespace mozilla

#endif  // mozilla_dom_DecoderTypes_h