Skip to content

Commit 03b35a3

Browse files
committed
Support Widevine
Signed-off-by: kkkuangzh <[email protected]>
1 parent 69cb8ae commit 03b35a3

11 files changed

+333
-12
lines changed

c2_buffers/include/mfx_c2_bitstream_in.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class MfxC2BitstreamIn
4040
// Maps c2 linear block and can leave it in mapped state until
4141
// frame_view freed or frame_view->Release is called.
4242
virtual c2_status_t AppendFrame(const C2FrameData& buf_pack, c2_nsecs_t timeout,
43-
std::unique_ptr<C2ReadView>* view);
43+
std::unique_ptr<C2ReadView>* view, bool header);
4444

4545
virtual bool IsInReset();
4646
protected: // variables

c2_buffers/src/mfx_c2_bitstream_in.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,33 @@ c2_status_t MfxC2BitstreamIn::Unload()
8282

8383

8484
c2_status_t MfxC2BitstreamIn::AppendFrame(const C2FrameData& buf_pack, c2_nsecs_t timeout,
85-
std::unique_ptr<C2ReadView>* frame_view)
85+
std::unique_ptr<C2ReadView>* frame_view, bool header)
8686
{
8787
MFX_DEBUG_TRACE_FUNC;
8888

8989
c2_status_t res = C2_OK;
9090
const mfxU8* data = nullptr;
9191
mfxU32 filled_len = 0;
92+
const mfxU8* infobuffer = nullptr;
93+
std::unique_ptr<C2ReadView> bs_read_view;
9294

95+
MFX_DEBUG_TRACE_I32(header);
9396
do {
9497
if (!frame_view) {
9598
res = C2_BAD_VALUE;
9699
break;
97100
}
98101

102+
for (auto &infoBuffer: buf_pack.infoBuffers) {
103+
if (infoBuffer.index().typeIndex() == kParamIndexEncryptedBuffer) {
104+
const C2BufferData& buf_data = infoBuffer.data();
105+
C2ConstLinearBlock encryptedBlock = buf_data.linearBlocks().front();
106+
MapConstLinearBlock(encryptedBlock, timeout, &bs_read_view);
107+
infobuffer = bs_read_view->data() + encryptedBlock.offset();
108+
MFX_DEBUG_TRACE_STREAM("c2 infobuffer data: " << FormatHex(infobuffer, encryptedBlock.size()));
109+
}
110+
}
111+
99112
if (buf_pack.buffers.size() == 0) {
100113
m_frameConstructor->SetEosMode(buf_pack.flags & C2FrameData::FLAG_END_OF_STREAM);
101114
break;
@@ -118,16 +131,28 @@ c2_status_t MfxC2BitstreamIn::AppendFrame(const C2FrameData& buf_pack, c2_nsecs_
118131

119132
m_frameConstructor->SetEosMode(buf_pack.flags & C2FrameData::FLAG_END_OF_STREAM);
120133

121-
mfxStatus mfx_res = m_frameConstructor->Load(data,
122-
filled_len,
123-
buf_pack.ordinal.timestamp.peeku(), // pass pts
124-
buf_pack.flags & C2FrameData::FLAG_CODEC_CONFIG,
125-
true);
134+
mfxStatus mfx_res;
135+
HUCVideoBuffer *hucBuffer = NULL;
136+
hucBuffer = (HUCVideoBuffer *) data;
137+
if (hucBuffer->pr_magic == PROTECTED_DATA_BUFFER_MAGIC) {
138+
mfx_res = m_frameConstructor->LoadSecure(hucBuffer,
139+
infobuffer,
140+
filled_len,
141+
buf_pack.ordinal.timestamp.peeku(), // pass pts
142+
header,
143+
true);
144+
*frame_view = std::move(bs_read_view);
145+
} else {
146+
mfx_res = m_frameConstructor->Load(data,
147+
filled_len,
148+
buf_pack.ordinal.timestamp.peeku(), // pass pts
149+
header,
150+
true);
151+
*frame_view = std::move(read_view);
152+
}
126153
res = MfxStatusToC2(mfx_res);
127154
if(C2_OK != res) break;
128155

129-
*frame_view = std::move(read_view);
130-
131156
} while(false);
132157

133158
MFX_DEBUG_TRACE__android_c2_status_t(res);

c2_components/include/mfx_c2_decoder_component.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class MfxC2DecoderComponent : public MfxC2Component
4343
DECODER_VP8,
4444
DECODER_MPEG2,
4545
DECODER_AV1,
46+
DECODER_H264_SECURE,
47+
DECODER_H265_SECURE
4648
};
4749

4850
enum class OperationState {
@@ -267,6 +269,9 @@ class MfxC2DecoderComponent : public MfxC2Component
267269
uint32_t m_file_num = 0;
268270
bool m_needCpuAccess = false;
269271

272+
protected:
273+
bool m_secure;
274+
270275
/* -----------------------C2Parameters--------------------------- */
271276
std::shared_ptr<C2ComponentNameSetting> m_name;
272277
std::shared_ptr<C2ComponentKindSetting> m_kind;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2017-2024 Intel Corporation
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in all
11+
// copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
// SOFTWARE.
20+
21+
#pragma once
22+
23+
#include "mfx_c2_component.h"
24+
#include "mfx_c2_decoder_component.h"
25+
#include "mfx_c2_components_registry.h"
26+
#include "mfx_dev.h"
27+
#include "mfx_c2_setters.h"
28+
#include <cutils/properties.h>
29+
30+
class MfxC2SecureDecoderComponent : public MfxC2DecoderComponent
31+
{
32+
public:
33+
MfxC2SecureDecoderComponent(const C2String name, const CreateConfig& config,
34+
std::shared_ptr<C2ReflectorHelper> reflector, DecoderType decoder_type);
35+
36+
virtual ~MfxC2SecureDecoderComponent();
37+
38+
static void RegisterClass(MfxC2ComponentsRegistry& registry);
39+
40+
MFX_CLASS_NO_COPY(MfxC2SecureDecoderComponent)
41+
42+
private:
43+
/* -----------------------C2Parameters--------------------------- */
44+
std::shared_ptr<C2SecureModeTuning> m_secureMode;
45+
};
46+

c2_components/src/mfx_c2_components_registry.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#else
3232
#include "mfx_c2_decoder_component.h"
3333
#include "mfx_c2_encoder_component.h"
34+
#include "mfx_c2_secure_decoder_component.h"
3435
#endif
3536

3637
using namespace android;
@@ -65,6 +66,7 @@ MfxC2ComponentsRegistry::MfxC2ComponentsRegistry()
6566
#else
6667
MfxC2DecoderComponent::RegisterClass(*this);
6768
MfxC2EncoderComponent::RegisterClass(*this);
69+
MfxC2SecureDecoderComponent::RegisterClass(*this);
6870
#endif
6971
}
7072

c2_components/src/mfx_c2_decoder_component.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ constexpr c2_nsecs_t TIMEOUT_NS = MFX_SECOND_NS;
4444
constexpr uint64_t kMinInputBufferSize = 1 * WIDTH_1K * HEIGHT_1K;
4545
constexpr uint64_t kDefaultConsumerUsage =
4646
(GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_COMPOSER);
47+
constexpr uint64_t kProtectedUsage = C2MemoryUsage::READ_PROTECTED;
4748

4849
// Android S declared VP8 profile
4950
#if PLATFORM_SDK_VERSION <= 30 // Android 11(R)
@@ -174,7 +175,8 @@ MfxC2DecoderComponent::MfxC2DecoderComponent(const C2String name, const CreateCo
174175
m_bInitialized(false),
175176
m_uSyncedPointsCount(0),
176177
m_bSetHdrStatic(false),
177-
m_surfaceNum(0)
178+
m_surfaceNum(0),
179+
m_secure(false)
178180
{
179181
MFX_DEBUG_TRACE_FUNC;
180182
const unsigned int SINGLE_STREAM_ID = 0u;
@@ -247,6 +249,7 @@ MfxC2DecoderComponent::MfxC2DecoderComponent(const C2String name, const CreateCo
247249
);
248250

249251
switch(m_decoderType) {
252+
case DECODER_H264_SECURE:
250253
case DECODER_H264: {
251254
m_uOutputDelay = /*max_dpb_size*/16 + /*for async depth*/1 + /*for msdk unref in sync part*/1;
252255

@@ -302,6 +305,7 @@ MfxC2DecoderComponent::MfxC2DecoderComponent(const C2String name, const CreateCo
302305
.build());
303306
break;
304307
}
308+
case DECODER_H265_SECURE:
305309
case DECODER_H265: {
306310
m_uOutputDelay = /*max_dpb_size*/16 + /*for async depth*/1 + /*for msdk unref in sync part*/1;
307311

@@ -934,9 +938,11 @@ void MfxC2DecoderComponent::InitFrameConstructor()
934938
MfxC2FrameConstructorType fc_type;
935939
switch (m_decoderType)
936940
{
941+
case DECODER_H264_SECURE:
937942
case DECODER_H264:
938943
fc_type = MfxC2FC_AVC;
939944
break;
945+
case DECODER_H265_SECURE:
940946
case DECODER_H265:
941947
fc_type = MfxC2FC_HEVC;
942948
break;
@@ -1096,9 +1102,11 @@ mfxStatus MfxC2DecoderComponent::ResetSettings()
10961102

10971103
switch (m_decoderType)
10981104
{
1105+
case DECODER_H264_SECURE:
10991106
case DECODER_H264:
11001107
m_mfxVideoParams.mfx.CodecId = MFX_CODEC_AVC;
11011108
break;
1109+
case DECODER_H265_SECURE:
11021110
case DECODER_H265:
11031111
m_mfxVideoParams.mfx.CodecId = MFX_CODEC_HEVC;
11041112
break;
@@ -1180,6 +1188,10 @@ mfxStatus MfxC2DecoderComponent::InitDecoder(std::shared_ptr<C2BlockPool> c2_all
11801188
}
11811189
}
11821190

1191+
if (m_secure) {
1192+
m_consumerUsage |= kProtectedUsage;
1193+
}
1194+
11831195
if (MFX_ERR_NONE == mfx_res) {
11841196
// saving parameters
11851197
mfxVideoParam oldParams = m_mfxVideoParams;
@@ -2129,7 +2141,7 @@ void MfxC2DecoderComponent::DoWork(std::unique_ptr<C2Work>&& work)
21292141
bool encounterResolutionChanged = false;
21302142
do {
21312143
std::unique_ptr<C2ReadView> read_view;
2132-
res = m_c2Bitstream->AppendFrame(work->input, TIMEOUT_NS, &read_view);
2144+
res = m_c2Bitstream->AppendFrame(work->input, TIMEOUT_NS, &read_view, codecConfig | !m_bInitialized);
21332145
if (C2_OK != res) break;
21342146

21352147
{
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) 2017-2024 Intel Corporation
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in all
11+
// copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
// SOFTWARE.
20+
21+
#include "mfx_c2_secure_decoder_component.h"
22+
23+
#include "mfx_defs.h"
24+
#include "mfx_c2_utils.h"
25+
#include "mfx_debug.h"
26+
#include "mfx_msdk_debug.h"
27+
#include "mfx_c2_debug.h"
28+
#include "mfx_c2_components_registry.h"
29+
#include "mfx_defaults.h"
30+
#include "C2PlatformSupport.h"
31+
32+
#undef MFX_DEBUG_MODULE_NAME
33+
#define MFX_DEBUG_MODULE_NAME "mfx_c2_secure_decoder_component"
34+
35+
MfxC2SecureDecoderComponent::MfxC2SecureDecoderComponent(const C2String name, const CreateConfig& config,
36+
std::shared_ptr<C2ReflectorHelper> reflector, DecoderType decoder_type) :
37+
MfxC2DecoderComponent(name, config, std::move(reflector), decoder_type)
38+
{
39+
MFX_DEBUG_TRACE_FUNC;
40+
m_secure = true;
41+
42+
addParameter(
43+
DefineParam(m_secureMode, C2_PARAMKEY_SECURE_MODE)
44+
.withConstValue(new C2SecureModeTuning(C2Config::secure_mode_t::SM_READ_PROTECTED_WITH_ENCRYPTED))
45+
.build());
46+
}
47+
48+
MfxC2SecureDecoderComponent::~MfxC2SecureDecoderComponent()
49+
{
50+
MFX_DEBUG_TRACE_FUNC;
51+
52+
MfxC2DecoderComponent::Release();
53+
}
54+
55+
void MfxC2SecureDecoderComponent::RegisterClass(MfxC2ComponentsRegistry& registry)
56+
{
57+
MFX_DEBUG_TRACE_FUNC;
58+
59+
registry.RegisterMfxC2Component("c2.intel.avc.decoder.secure",
60+
&MfxC2Component::Factory<MfxC2SecureDecoderComponent, DecoderType>::Create<DECODER_H264_SECURE>);
61+
62+
registry.RegisterMfxC2Component("c2.intel.hevc.decoder.secure",
63+
&MfxC2Component::Factory<MfxC2SecureDecoderComponent, DecoderType>::Create<DECODER_H265_SECURE>);
64+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) 2017-2024 Intel Corporation
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in all
11+
// copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
// SOFTWARE.
20+
21+
#pragma once
22+
#include <array>
23+
#include <mfxstructures.h>
24+
25+
#define PROTECTED_DATA_BUFFER_MAGIC (0UL | ('E' << 24) | ('B' << 16) | ('D' << 8) | 'P')
26+
using IV = std::array<uint8_t, 16>;
27+
28+
typedef unsigned int VAGenericID;
29+
typedef VAGenericID VAContextID;
30+
31+
typedef enum OEMCryptoCipherMode {
32+
OEMCrypto_CipherMode_CTR,
33+
OEMCrypto_CipherMode_CBC,
34+
OEMCrypto_CipherMode_MaxValue = OEMCrypto_CipherMode_CBC,
35+
} OEMCryptoCipherMode;
36+
37+
typedef struct {
38+
size_t block_offset;
39+
IV current_iv;
40+
size_t data_length;
41+
size_t clear_bytes;
42+
size_t encrypted_bytes;
43+
size_t pattern_clear;
44+
size_t pattern_encrypted;
45+
} packet_info;
46+
47+
typedef struct {
48+
uint32_t pr_magic;
49+
uint32_t app_id;
50+
uint32_t session_id;
51+
size_t num_packet_data;
52+
size_t sample_size;
53+
OEMCryptoCipherMode cipher_mode;
54+
uint8_t hw_key_id[16];
55+
packet_info* packet_data;
56+
} HUCVideoBuffer;
57+
58+
inline EncryptionScheme GetEncryptionScheme(OEMCryptoCipherMode mode) {
59+
switch (mode) {
60+
case OEMCrypto_CipherMode_CTR:
61+
return EncryptionScheme::kCenc;
62+
case OEMCrypto_CipherMode_CBC:
63+
return EncryptionScheme::kCbcs;
64+
default:
65+
return EncryptionScheme::kUnencrypted;
66+
}
67+
}

0 commit comments

Comments
 (0)