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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
media / gpu / mac / video_toolbox_h265_accelerator_unittest.cc [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.
#include "media/gpu/mac/video_toolbox_h265_accelerator.h"
#include <memory>
#include "base/containers/span.h"
#include "media/base/media_util.h"
#include "media/gpu/codec_picture.h"
#include "media/parsers/h265_parser.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::ElementsAre;
using testing::SaveArg;
namespace media {
namespace {
// Configuration from buck1080p60_hevc.mp4
constexpr uint8_t kVPS0[] = {0x40, 0x01, 0x0c, 0x01, 0xff, 0xff, 0x01, 0x60,
0x00, 0x00, 0x03, 0x00, 0x90, 0x00, 0x00, 0x03,
0x00, 0x00, 0x03, 0x00, 0x7b, 0x95, 0x98, 0x09};
constexpr uint8_t kSPS0[] = {
0x42, 0x01, 0x01, 0x01, 0x60, 0x00, 0x00, 0x03, 0x00, 0x90, 0x00,
0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x7b, 0xa0, 0x03, 0xc0, 0x80,
0x10, 0xe5, 0x96, 0x56, 0x69, 0x24, 0xca, 0xf0, 0x16, 0x9c, 0x20,
0x00, 0x00, 0x03, 0x00, 0x20, 0x00, 0x00, 0x07, 0x81};
constexpr uint8_t kPPS0[] = {0x44, 0x01, 0xc1, 0x72, 0xb4, 0x62, 0x40};
// Configuration from bear-1280x720-hevc-10bit-hdr10.mp4
constexpr uint8_t kVPS1[] = {0x40, 0x01, 0x0c, 0x01, 0xff, 0xff, 0x02, 0x20,
0x00, 0x00, 0x03, 0x00, 0x90, 0x00, 0x00, 0x03,
0x00, 0x00, 0x03, 0x00, 0x5d, 0x95, 0x98, 0x09};
constexpr uint8_t kSPS1[] = {
0x42, 0x01, 0x01, 0x02, 0x20, 0x00, 0x00, 0x03, 0x00, 0x90, 0x00, 0x00,
0x03, 0x00, 0x00, 0x03, 0x00, 0x5d, 0xa0, 0x02, 0x80, 0x80, 0x2d, 0x13,
0x65, 0x95, 0x9a, 0x49, 0x32, 0xbc, 0x05, 0xa8, 0x48, 0x80, 0x4f, 0x08,
0x00, 0x00, 0x1f, 0x48, 0x00, 0x03, 0xa9, 0x80, 0x40};
constexpr uint8_t kPPS1[] = {0x44, 0x01, 0xc1, 0x72, 0xb4, 0x62, 0x40};
constexpr uint8_t kSliceData[] = {0x02};
} // namespace
class VideoToolboxH265AcceleratorTest : public testing::Test {
public:
VideoToolboxH265AcceleratorTest() = default;
~VideoToolboxH265AcceleratorTest() override = default;
protected:
MOCK_METHOD3(OnDecode,
void(base::apple::ScopedCFTypeRef<CMSampleBufferRef>,
VideoToolboxDecompressionSessionMetadata,
scoped_refptr<CodecPicture>));
MOCK_METHOD1(OnOutput, void(scoped_refptr<CodecPicture>));
std::unique_ptr<VideoToolboxH265Accelerator> accelerator_{
std::make_unique<VideoToolboxH265Accelerator>(
std::make_unique<NullMediaLog>(),
base::BindRepeating(&VideoToolboxH265AcceleratorTest::OnDecode,
base::Unretained(this)),
base::BindRepeating(&VideoToolboxH265AcceleratorTest::OnOutput,
base::Unretained(this)))};
};
TEST_F(VideoToolboxH265AcceleratorTest, Construct) {}
TEST_F(VideoToolboxH265AcceleratorTest, DecodeOne) {
scoped_refptr<H265Picture> pic = accelerator_->CreateH265Picture();
H265VPS vps;
H265SPS sps;
H265PPS pps;
H265SliceHeader slice_hdr;
H265Picture::Vector ref_pic_list;
std::vector<SubsampleEntry> subsamples;
// Decode frame.
accelerator_->ProcessVPS(&vps, base::span(kVPS0));
accelerator_->ProcessSPS(&sps, base::span(kSPS0));
accelerator_->ProcessPPS(&pps, base::span(kPPS0));
accelerator_->SubmitFrameMetadata(&sps, &pps, &slice_hdr, ref_pic_list,
ref_pic_list, ref_pic_list, ref_pic_list,
pic);
accelerator_->SubmitSlice(&sps, &pps, &slice_hdr, ref_pic_list, ref_pic_list,
ref_pic_list, ref_pic_list, ref_pic_list, pic,
kSliceData, sizeof(kSliceData), subsamples);
// Save the resulting sample.
base::apple::ScopedCFTypeRef<CMSampleBufferRef> sample;
EXPECT_CALL(*this, OnDecode(_, _, _)).WillOnce(SaveArg<0>(&sample));
accelerator_->SubmitDecode(pic);
// Verify sample.
CMBlockBufferRef buf = CMSampleBufferGetDataBuffer(sample.get());
std::vector<uint8_t> data(CMBlockBufferGetDataLength(buf));
CMBlockBufferCopyDataBytes(buf, 0, CMBlockBufferGetDataLength(buf),
data.data());
EXPECT_THAT(data, ElementsAre(0x00, 0x00, 0x00, 0x01, // length
0x02 // kSliceData
));
// Check that OutputPicture() works.
EXPECT_CALL(*this, OnOutput(_));
accelerator_->OutputPicture(pic);
}
TEST_F(VideoToolboxH265AcceleratorTest, DecodeTwo) {
scoped_refptr<H265Picture> pic0 = accelerator_->CreateH265Picture();
scoped_refptr<H265Picture> pic1 = accelerator_->CreateH265Picture();
H265VPS vps;
H265SPS sps;
H265PPS pps;
H265SliceHeader slice_hdr;
H265Picture::Vector ref_pic_list;
std::vector<SubsampleEntry> subsamples;
// First frame.
accelerator_->ProcessVPS(&vps, base::span(kVPS0));
accelerator_->ProcessSPS(&sps, base::span(kSPS0));
accelerator_->ProcessPPS(&pps, base::span(kPPS0));
accelerator_->SubmitFrameMetadata(&sps, &pps, &slice_hdr, ref_pic_list,
ref_pic_list, ref_pic_list, ref_pic_list,
pic0);
accelerator_->SubmitSlice(&sps, &pps, &slice_hdr, ref_pic_list, ref_pic_list,
ref_pic_list, ref_pic_list, ref_pic_list, pic0,
kSliceData, sizeof(kSliceData), subsamples);
// Save the resulting sample.
base::apple::ScopedCFTypeRef<CMSampleBufferRef> sample0;
EXPECT_CALL(*this, OnDecode(_, _, _)).WillOnce(SaveArg<0>(&sample0));
accelerator_->SubmitDecode(pic0);
// Second frame.
accelerator_->ProcessVPS(&vps, base::span(kVPS0));
accelerator_->ProcessSPS(&sps, base::span(kSPS0));
accelerator_->ProcessPPS(&pps, base::span(kPPS0));
accelerator_->SubmitFrameMetadata(&sps, &pps, &slice_hdr, ref_pic_list,
ref_pic_list, ref_pic_list, ref_pic_list,
pic1);
accelerator_->SubmitSlice(&sps, &pps, &slice_hdr, ref_pic_list, ref_pic_list,
ref_pic_list, ref_pic_list, ref_pic_list, pic1,
kSliceData, sizeof(kSliceData), subsamples);
// Save the resulting sample.
base::apple::ScopedCFTypeRef<CMSampleBufferRef> sample1;
EXPECT_CALL(*this, OnDecode(_, _, _)).WillOnce(SaveArg<0>(&sample1));
accelerator_->SubmitDecode(pic1);
// The two samples should have the same configuration.
EXPECT_EQ(CMSampleBufferGetFormatDescription(sample0.get()),
CMSampleBufferGetFormatDescription(sample1.get()));
}
TEST_F(VideoToolboxH265AcceleratorTest, DecodeTwo_Reset) {
scoped_refptr<H265Picture> pic0 = accelerator_->CreateH265Picture();
scoped_refptr<H265Picture> pic1 = accelerator_->CreateH265Picture();
H265VPS vps;
H265SPS sps;
H265PPS pps;
H265SliceHeader slice_hdr;
H265Picture::Vector ref_pic_list;
std::vector<SubsampleEntry> subsamples;
// First frame.
accelerator_->ProcessVPS(&vps, base::span(kVPS0));
accelerator_->ProcessSPS(&sps, base::span(kSPS0));
accelerator_->ProcessPPS(&pps, base::span(kPPS0));
accelerator_->SubmitFrameMetadata(&sps, &pps, &slice_hdr, ref_pic_list,
ref_pic_list, ref_pic_list, ref_pic_list,
pic0);
accelerator_->SubmitSlice(&sps, &pps, &slice_hdr, ref_pic_list, ref_pic_list,
ref_pic_list, ref_pic_list, ref_pic_list, pic0,
kSliceData, sizeof(kSliceData), subsamples);
// Save the resulting sample.
base::apple::ScopedCFTypeRef<CMSampleBufferRef> sample0;
EXPECT_CALL(*this, OnDecode(_, _, _)).WillOnce(SaveArg<0>(&sample0));
accelerator_->SubmitDecode(pic0);
// Reset.
accelerator_->Reset();
// Second frame.
accelerator_->ProcessVPS(&vps, base::span(kVPS0));
accelerator_->ProcessSPS(&sps, base::span(kSPS0));
accelerator_->ProcessPPS(&pps, base::span(kPPS0));
accelerator_->SubmitFrameMetadata(&sps, &pps, &slice_hdr, ref_pic_list,
ref_pic_list, ref_pic_list, ref_pic_list,
pic1);
accelerator_->SubmitSlice(&sps, &pps, &slice_hdr, ref_pic_list, ref_pic_list,
ref_pic_list, ref_pic_list, ref_pic_list, pic1,
kSliceData, sizeof(kSliceData), subsamples);
// Save the resulting sample.
base::apple::ScopedCFTypeRef<CMSampleBufferRef> sample1;
EXPECT_CALL(*this, OnDecode(_, _, _)).WillOnce(SaveArg<0>(&sample1));
accelerator_->SubmitDecode(pic1);
// The accelerator should have made a new configuration. (Technically it
// should be fine to reuse the old one because the parameter sets did not
// change.)
EXPECT_NE(CMSampleBufferGetFormatDescription(sample0.get()),
CMSampleBufferGetFormatDescription(sample1.get()));
}
TEST_F(VideoToolboxH265AcceleratorTest, DecodeTwo_ConfigChange) {
scoped_refptr<H265Picture> pic0 = accelerator_->CreateH265Picture();
scoped_refptr<H265Picture> pic1 = accelerator_->CreateH265Picture();
H265VPS vps;
H265SPS sps;
H265PPS pps;
H265SliceHeader slice_hdr;
H265Picture::Vector ref_pic_list;
std::vector<SubsampleEntry> subsamples;
// First frame.
accelerator_->ProcessVPS(&vps, base::span(kVPS0));
accelerator_->ProcessSPS(&sps, base::span(kSPS0));
accelerator_->ProcessPPS(&pps, base::span(kPPS0));
accelerator_->SubmitFrameMetadata(&sps, &pps, &slice_hdr, ref_pic_list,
ref_pic_list, ref_pic_list, ref_pic_list,
pic0);
accelerator_->SubmitSlice(&sps, &pps, &slice_hdr, ref_pic_list, ref_pic_list,
ref_pic_list, ref_pic_list, ref_pic_list, pic0,
kSliceData, sizeof(kSliceData), subsamples);
// Save the resulting sample.
base::apple::ScopedCFTypeRef<CMSampleBufferRef> sample0;
EXPECT_CALL(*this, OnDecode(_, _, _)).WillOnce(SaveArg<0>(&sample0));
accelerator_->SubmitDecode(pic0);
// Second frame.
accelerator_->ProcessVPS(&vps, base::span(kVPS1));
accelerator_->ProcessSPS(&sps, base::span(kSPS1));
accelerator_->ProcessPPS(&pps, base::span(kPPS1));
accelerator_->SubmitFrameMetadata(&sps, &pps, &slice_hdr, ref_pic_list,
ref_pic_list, ref_pic_list, ref_pic_list,
pic1);
accelerator_->SubmitSlice(&sps, &pps, &slice_hdr, ref_pic_list, ref_pic_list,
ref_pic_list, ref_pic_list, ref_pic_list, pic1,
kSliceData, sizeof(kSliceData), subsamples);
// Save the resulting sample.
base::apple::ScopedCFTypeRef<CMSampleBufferRef> sample1;
EXPECT_CALL(*this, OnDecode(_, _, _)).WillOnce(SaveArg<0>(&sample1));
accelerator_->SubmitDecode(pic1);
// The two samples should still have the same configurations.
EXPECT_EQ(CMSampleBufferGetFormatDescription(sample0.get()),
CMSampleBufferGetFormatDescription(sample1.get()));
}
} // namespace media