Skip to content

Commit

Permalink
Move Widevine feature to separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
kkkuangzh committed Dec 15, 2024
1 parent a9b4641 commit 9c41de3
Show file tree
Hide file tree
Showing 9 changed files with 614 additions and 415 deletions.
35 changes: 35 additions & 0 deletions c2_buffers/include/mfx_c2_secure_bitstream_in.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2017-2024 Intel Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include "mfx_c2_bitstream_in.h"
#include "mfx_secure_frame_constructor.h"

class MfxC2SecureBitstreamIn : public MfxC2BitstreamIn
{
public:
MfxC2SecureBitstreamIn(MfxC2FrameConstructorType fc_type);
virtual ~MfxC2SecureBitstreamIn();

virtual c2_status_t AppendFrame(const C2FrameData& buf_pack, c2_nsecs_t timeout,
std::unique_ptr<C2ReadView>* view);

private:
MFX_CLASS_NO_COPY(MfxC2SecureBitstreamIn)
};
43 changes: 6 additions & 37 deletions c2_buffers/src/mfx_c2_bitstream_in.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ using namespace android;
#undef MFX_DEBUG_MODULE_NAME
#define MFX_DEBUG_MODULE_NAME "mfx_c2_bitstream_in"

constexpr c2_nsecs_t TIMEOUT_NS = MFX_SECOND_NS;

MfxC2BitstreamIn::MfxC2BitstreamIn(MfxC2FrameConstructorType fc_type)
{
MFX_DEBUG_TRACE_FUNC;
Expand Down Expand Up @@ -92,23 +90,6 @@ c2_status_t MfxC2BitstreamIn::AppendFrame(const C2FrameData& buf_pack, c2_nsecs_
const mfxU8* data = nullptr;
mfxU32 filled_len = 0;

std::unique_ptr<C2ConstLinearBlock> encryptedBlock;
for (auto &infoBuffer: buf_pack.infoBuffers) {
if (infoBuffer.index().typeIndex() == kParamIndexEncryptedBuffer) {
const C2BufferData& buf_data = infoBuffer.data();
encryptedBlock = std::make_unique<C2ConstLinearBlock>(buf_data.linearBlocks().front());
}
}

const mfxU8* infobuffer = nullptr;
std::unique_ptr<C2ReadView> bs_read_view;

if (encryptedBlock != nullptr) {
MapConstLinearBlock(*encryptedBlock, TIMEOUT_NS, &bs_read_view);
infobuffer = bs_read_view->data() + encryptedBlock->offset();
MFX_DEBUG_TRACE_STREAM("c2 infobuffer data: " << FormatHex(infobuffer, encryptedBlock->size()));
}

do {
if (!frame_view) {
res = C2_BAD_VALUE;
Expand Down Expand Up @@ -137,23 +118,11 @@ c2_status_t MfxC2BitstreamIn::AppendFrame(const C2FrameData& buf_pack, c2_nsecs_

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

mfxStatus mfx_res;
HUCVideoBuffer *hucBuffer = NULL;
hucBuffer = (HUCVideoBuffer *) data;
if (hucBuffer->pr_magic == PROTECTED_DATA_BUFFER_MAGIC) {
mfx_res = m_frameConstructor->Load_data(data,
filled_len,
infobuffer,
buf_pack.ordinal.timestamp.peeku(), // pass pts
buf_pack.flags & C2FrameData::FLAG_CODEC_CONFIG,
true);
} else {
mfx_res = m_frameConstructor->Load(data,
filled_len,
buf_pack.ordinal.timestamp.peeku(), // pass pts
buf_pack.flags & C2FrameData::FLAG_CODEC_CONFIG,
true);
}
mfxStatus mfx_res = m_frameConstructor->Load(data,
filled_len,
buf_pack.ordinal.timestamp.peeku(), // pass pts
buf_pack.flags & C2FrameData::FLAG_CODEC_CONFIG,
true);
res = MfxStatusToC2(mfx_res);
if(C2_OK != res) break;

Expand All @@ -163,4 +132,4 @@ c2_status_t MfxC2BitstreamIn::AppendFrame(const C2FrameData& buf_pack, c2_nsecs_

MFX_DEBUG_TRACE__android_c2_status_t(res);
return res;
}
}
126 changes: 126 additions & 0 deletions c2_buffers/src/mfx_c2_secure_bitstream_in.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright (c) 2017-2024 Intel Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.


#include "mfx_c2_secure_bitstream_in.h"
#include "mfx_c2_utils.h"
#include "mfx_debug.h"
#include "mfx_c2_debug.h"
#include "mfx_msdk_debug.h"

#undef MFX_DEBUG_MODULE_NAME
#define MFX_DEBUG_MODULE_NAME "mfx_c2_secure_bitstream_in"

constexpr c2_nsecs_t TIMEOUT_NS = MFX_SECOND_NS;

MfxC2SecureBitstreamIn::MfxC2SecureBitstreamIn(MfxC2FrameConstructorType fc_type)
: MfxC2BitstreamIn(fc_type)
{
MFX_DEBUG_TRACE_FUNC;

m_frameConstructor = MfxC2SecureFrameConstructorFactory::CreateFrameConstructor(fc_type);
}

MfxC2SecureBitstreamIn::~MfxC2SecureBitstreamIn()
{
MFX_DEBUG_TRACE_FUNC;
}

c2_status_t MfxC2SecureBitstreamIn::AppendFrame(const C2FrameData& buf_pack, c2_nsecs_t timeout,
std::unique_ptr<C2ReadView>* frame_view)
{
MFX_DEBUG_TRACE_FUNC;

c2_status_t res = C2_OK;
const mfxU8* data = nullptr;
mfxU32 filled_len = 0;

std::unique_ptr<C2ConstLinearBlock> encryptedBlock;
for (auto &infoBuffer: buf_pack.infoBuffers) {
if (infoBuffer.index().typeIndex() == kParamIndexEncryptedBuffer) {
const C2BufferData& buf_data = infoBuffer.data();
encryptedBlock = std::make_unique<C2ConstLinearBlock>(buf_data.linearBlocks().front());
}
}

const mfxU8* infobuffer = nullptr;
std::unique_ptr<C2ReadView> bs_read_view;

if (encryptedBlock != nullptr) {
MapConstLinearBlock(*encryptedBlock, TIMEOUT_NS, &bs_read_view);
infobuffer = bs_read_view->data() + encryptedBlock->offset();
MFX_DEBUG_TRACE_STREAM("c2 infobuffer data: " << FormatHex(infobuffer, encryptedBlock->size()));
}

do {
if (!frame_view) {
res = C2_BAD_VALUE;
break;
}

if (buf_pack.buffers.size() == 0) {
m_frameConstructor->SetEosMode(buf_pack.flags & C2FrameData::FLAG_END_OF_STREAM);
break;
}

std::unique_ptr<C2ConstLinearBlock> c_linear_block;
res = GetC2ConstLinearBlock(buf_pack, &c_linear_block);
if(C2_OK != res) break;

std::unique_ptr<C2ReadView> read_view;
res = MapConstLinearBlock(*c_linear_block, timeout, &read_view);
if(C2_OK != res) break;

MFX_DEBUG_TRACE_I64(buf_pack.ordinal.timestamp.peeku());

data = read_view->data() + c_linear_block->offset();
filled_len = c_linear_block->size();

MFX_DEBUG_TRACE_STREAM("data: " << FormatHex(data, filled_len));

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

mfxStatus mfx_res;
HUCVideoBuffer *hucBuffer = NULL;
hucBuffer = (HUCVideoBuffer *) data;
if (hucBuffer->pr_magic == PROTECTED_DATA_BUFFER_MAGIC) {
mfx_res = m_frameConstructor->Load_data(data,
filled_len,
infobuffer,
buf_pack.ordinal.timestamp.peeku(), // pass pts
buf_pack.flags & C2FrameData::FLAG_CODEC_CONFIG,
true);
} else {
mfx_res = m_frameConstructor->Load(data,
filled_len,
buf_pack.ordinal.timestamp.peeku(), // pass pts
buf_pack.flags & C2FrameData::FLAG_CODEC_CONFIG,
true);
}
res = MfxStatusToC2(mfx_res);
if(C2_OK != res) break;

*frame_view = std::move(read_view);

} while(false);

MFX_DEBUG_TRACE__android_c2_status_t(res);
return res;
}
1 change: 1 addition & 0 deletions c2_components/include/mfx_c2_decoder_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "mfx_cmd_queue.h"
#include "mfx_c2_frame_out.h"
#include "mfx_c2_bitstream_in.h"
#include "mfx_c2_secure_bitstream_in.h"
#include "mfx_frame_pool_allocator.h"
#include "mfx_gralloc_instance.h"
#include "mfx_intel_device.h"
Expand Down
8 changes: 7 additions & 1 deletion c2_components/src/mfx_c2_decoder_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,13 @@ void MfxC2DecoderComponent::InitFrameConstructor()
fc_type = MfxC2FC_None;
break;
}
m_c2Bitstream = std::make_unique<MfxC2BitstreamIn>(fc_type);

if (fc_type == MfxC2FC_SEC_AVC || fc_type == MfxC2FC_SEC_HEVC)
{
m_c2Bitstream = std::make_unique<MfxC2SecureBitstreamIn>(fc_type);
} else {
m_c2Bitstream = std::make_unique<MfxC2BitstreamIn>(fc_type);
}
}

#ifdef USE_ONEVPL
Expand Down
67 changes: 0 additions & 67 deletions c2_utils/include/mfx_frame_constructor.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#include <vector>
#include <map>

#include "mfx_c2_widevine_crypto_defs.h"

enum MfxC2FrameConstructorType
{
MfxC2FC_None,
Expand Down Expand Up @@ -240,71 +238,6 @@ class MfxC2HEVCFrameConstructor : public MfxC2AVCFrameConstructor
MFX_CLASS_NO_COPY(MfxC2HEVCFrameConstructor)
};

class MfxC2SecureFrameConstructor
{
public:
MfxC2SecureFrameConstructor();
virtual ~MfxC2SecureFrameConstructor();
protected:
virtual mfxStatus Load(const mfxU8* data, mfxU32 size, mfxU64 pts, bool header, bool complete_frame);

// metadata of hucbuffer
HUCVideoBuffer* m_hucBuffer = nullptr;
// bs buffer used for WV L1
std::shared_ptr<mfxBitstream> m_bstEnc;
// ext buffer vector
std::vector<mfxExtBuffer*> m_extBufs;
// MFX_EXTBUFF_ENCRYPTION_PARAM
mfxExtEncryptionParam m_decryptParams;

private:
MFX_CLASS_NO_COPY(MfxC2SecureFrameConstructor)
};

class MfxC2AVCSecureFrameConstructor : public MfxC2HEVCFrameConstructor, public MfxC2SecureFrameConstructor
{
public:
MfxC2AVCSecureFrameConstructor();
virtual ~MfxC2AVCSecureFrameConstructor();

virtual mfxStatus Load(const mfxU8* data, mfxU32 size, mfxU64 pts, bool b_header, bool bCompleteFrame);
virtual mfxStatus Load_data(const mfxU8* data, mfxU32 size, const mfxU8* infobuffer, mfxU64 pts, bool header, bool complete_frame);

protected:
virtual StartCode ReadStartCode(const mfxU8** position, mfxU32* size_left);
virtual bool isSPS(mfxI32 code) {return MfxC2AVCFrameConstructor::isSPS(code);}
virtual bool isPPS(mfxI32 code) {return MfxC2AVCFrameConstructor::isPPS(code);}
virtual bool isIDR(mfxI32 code) {return NAL_UT_AVC_IDR_SLICE == code;}
virtual bool isRegularSlice(mfxI32 code) {return NAL_UT_AVC_SLICE == code;}

std::shared_ptr<mfxBitstream> GetMfxBitstream();

protected:
bool m_bNeedAttachSPSPPS;

const static mfxU32 NAL_UT_AVC_SLICE = 1;
const static mfxU32 NAL_UT_AVC_IDR_SLICE = 5;

private:
MFX_CLASS_NO_COPY(MfxC2AVCSecureFrameConstructor)
};

/*------------------------------------------------------------------------------*/

class MfxC2HEVCSecureFrameConstructor : public MfxC2AVCSecureFrameConstructor
{
public:
MfxC2HEVCSecureFrameConstructor();
virtual ~MfxC2HEVCSecureFrameConstructor();

protected:
virtual bool isSPS(mfxI32 code) {return MfxC2HEVCFrameConstructor::isSPS(code);}
virtual bool isPPS(mfxI32 code) {return MfxC2HEVCFrameConstructor::isPPS(code);}

private:
MFX_CLASS_NO_COPY(MfxC2HEVCSecureFrameConstructor)
};

class MfxC2FrameConstructorFactory
{
public:
Expand Down
Loading

0 comments on commit 9c41de3

Please sign in to comment.