Skip to content

Commit

Permalink
Base code for secure decoder plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
kkkuangzh committed Jul 4, 2024
1 parent d3aa084 commit ecae1b9
Show file tree
Hide file tree
Showing 23 changed files with 3,780 additions and 142 deletions.
50 changes: 29 additions & 21 deletions c2_components/include/mfx_c2_decoder_component.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class MfxC2DecoderComponent : public MfxC2Component
DECODER_VP8,
DECODER_MPEG2,
DECODER_AV1,
#ifdef ENABLE_WIDEVINE
DECODER_H264_SECURE,
DECODER_H265_SECURE
#endif
};

enum class OperationState {
Expand Down Expand Up @@ -103,7 +107,10 @@ class MfxC2DecoderComponent : public MfxC2Component

c2_status_t Flush(std::list<std::unique_ptr<C2Work>>* const flushedWork) override;

private:
// Work routines
c2_status_t ValidateWork(const std::unique_ptr<C2Work>& work);

protected:
c2_status_t UpdateC2Param(const mfxVideoParam* src, C2Param::Index index) const;

void DoUpdateMfxParam(const std::vector<C2Param*> &params,
Expand Down Expand Up @@ -137,11 +144,6 @@ class MfxC2DecoderComponent : public MfxC2Component

mfxU16 GetAsyncDepth();

// Work routines
c2_status_t ValidateWork(const std::unique_ptr<C2Work>& work);

void DoWork(std::unique_ptr<C2Work>&& work);

void ReleaseReadViews(uint64_t incoming_frame_index);

void EmptyReadViews(uint64_t timestamp, uint64_t frame_index);
Expand All @@ -152,17 +154,19 @@ class MfxC2DecoderComponent : public MfxC2Component

void FillEmptyWork(std::unique_ptr<C2Work>&& work, c2_status_t res);

void Drain(std::unique_ptr<C2Work>&& work);
// waits for the sync_point and update work with decoder output then
void WaitWork(MfxC2FrameOut&& frame_out, mfxSyncPoint sync_point);

void PushPending(std::unique_ptr<C2Work>&& work);

void UpdateHdrStaticInfo();

void UpdateColorAspectsFromBitstream(const mfxExtVideoSignalInfo &signalInfo);

private:
protected:
void DoWork(std::unique_ptr<C2Work>&& work);

void PushPending(std::unique_ptr<C2Work>&& work);

void Drain(std::unique_ptr<C2Work>&& work);
// waits for the sync_point and update work with decoder output then
void WaitWork(MfxC2FrameOut&& frame_out, mfxSyncPoint sync_point);

DecoderType m_decoderType;

std::unique_ptr<MfxDev> m_device;
Expand All @@ -173,6 +177,17 @@ class MfxC2DecoderComponent : public MfxC2Component
MFXVideoSession m_mfxSession;
#endif

std::atomic<bool> m_bEosReceived {};

MfxCmdQueue m_workingQueue;
MFX_TRACEABLE(m_workingQueue);
MfxCmdQueue m_waitingQueue;
MFX_TRACEABLE(m_waitingQueue);

std::unique_ptr<MfxC2BitstreamIn> m_c2Bitstream;
mfxVideoParam m_mfxVideoParams {};

protected:
// Accessed from working thread or stop method when working thread is stopped.
std::unique_ptr<MFXVideoDECODE> m_mfxDecoder;

Expand All @@ -183,12 +198,6 @@ class MfxC2DecoderComponent : public MfxC2Component

OperationState m_OperationState { OperationState::INITIALIZATION };

MfxCmdQueue m_workingQueue;
MFX_TRACEABLE(m_workingQueue);
MfxCmdQueue m_waitingQueue;
MFX_TRACEABLE(m_waitingQueue);

mfxVideoParam m_mfxVideoParams {};
std::vector<mfxExtBuffer*> m_extBuffers;
mfxExtVideoSignalInfo m_signalInfo;

Expand All @@ -199,7 +208,6 @@ class MfxC2DecoderComponent : public MfxC2Component
mfxU16 m_uMaxWidth {};
mfxU16 m_uMaxHeight {};

std::atomic<bool> m_bEosReceived {};
// Members handling MFX_WRN_DEVICE_BUSY.
// Active sync points got from DecodeFrameAsync for waiting on.
std::atomic_uint m_uSyncedPointsCount;
Expand All @@ -209,7 +217,7 @@ class MfxC2DecoderComponent : public MfxC2Component
// Even atomic type needs to be mutex protected.
std::mutex m_devBusyMutex;

std::unique_ptr<MfxC2BitstreamIn> m_c2Bitstream;

// Store raw pointers there as don't want to keep objects by shared_ptr
std::map<uint64_t, std::shared_ptr<mfxFrameSurface1>> m_surfaces; // all ever send to Decoder
// Store all surfaces used for system memory
Expand Down
84 changes: 84 additions & 0 deletions c2_components/include/mfx_c2_secure_decoder_component.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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.

#pragma once

#include "mfx_c2_component.h"
#include "mfx_c2_decoder_component.h"
#include "mfx_c2_components_registry.h"
#include "mfx_dev.h"
#include "mfx_cmd_queue.h"
#include "mfx_c2_frame_out.h"
#include "mfx_c2_bitstream_in.h"
#include "mfx_frame_pool_allocator.h"
#include "mfx_gralloc_instance.h"
#include "mfx_c2_setters.h"
#include <cutils/properties.h>

class MfxC2SecureDecoderComponent : public MfxC2DecoderComponent
{
public:
struct DecryptionTask
{
mfxU16 usStatusReportFeedbackNumber {0};
VASurfaceID surfaceId {0};
};

public:
MfxC2SecureDecoderComponent(const C2String name, const CreateConfig& config,
std::shared_ptr<C2ReflectorHelper> reflector, DecoderType decoder_type);

virtual ~MfxC2SecureDecoderComponent();

static void RegisterClass(MfxC2ComponentsRegistry& registry);

MFX_CLASS_NO_COPY(MfxC2SecureDecoderComponent)

protected:
c2_status_t Init() override;

c2_status_t Queue(std::list<std::unique_ptr<C2Work>>* const items) override;

c2_status_t DoStart() override;

c2_status_t DoStop(bool abort) override;

void Decrypt(std::unique_ptr<C2Work>&& work);

void Dowork(std::unique_ptr<C2Work>&& work, DecryptionTask&& decryptionTask, mfxU32 feedbackNumber);

bool CheckBitstream();

mfxStatus SubmitDecryptionTask(VAContextID contextId,
mfxU16 PESPacketCounter,
DecryptionTask& decryptionTask);

mfxStatus WaitUtilDecryptionDone(DecryptionTask& decryptionTask, mfxU32 feedbackNumber);

private:
MfxCmdQueue m_decryptionQueue;
MFX_TRACEABLE(m_decryptionQueue);

mfxU32 m_decrytedFeedbackNumber = 0;
mfxU16 m_uDecrptingCount = 0;
/* -----------------------C2Parameters--------------------------- */
std::shared_ptr<C2SecureModeTuning> m_secureMode;
};

8 changes: 8 additions & 0 deletions c2_components/src/mfx_c2_components_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
#include "mfx_c2_encoder_component.h"
#endif

#ifdef ENABLE_WIDEVINE
#include "mfx_c2_secure_decoder_component.h"
#endif

using namespace android;


Expand Down Expand Up @@ -66,6 +70,10 @@ MfxC2ComponentsRegistry::MfxC2ComponentsRegistry()
MfxC2DecoderComponent::RegisterClass(*this);
MfxC2EncoderComponent::RegisterClass(*this);
#endif

#ifdef ENABLE_WIDEVINE
MfxC2SecureDecoderComponent::RegisterClass(*this);
#endif
}

MfxC2ComponentsRegistry& MfxC2ComponentsRegistry::getInstance()
Expand Down
38 changes: 17 additions & 21 deletions c2_components/src/mfx_c2_decoder_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ MfxC2DecoderComponent::MfxC2DecoderComponent(const C2String name, const CreateCo
.build());

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

Expand Down Expand Up @@ -277,6 +278,7 @@ MfxC2DecoderComponent::MfxC2DecoderComponent(const C2String name, const CreateCo
.build());
break;
}
case DECODER_H265_SECURE:
case DECODER_H265: {
m_uOutputDelay = /*max_dpb_size*/16 + /*for async depth*/1 + /*for msdk unref in sync part*/1;

Expand Down Expand Up @@ -370,9 +372,8 @@ MfxC2DecoderComponent::MfxC2DecoderComponent(const C2String name, const CreateCo
.oneOf({
PROFILE_VP9_0,
PROFILE_VP9_1,
// TODO: support 10-bit HDR
// PROFILE_VP9_2,
// PROFILE_VP9_3,
PROFILE_VP9_2,
PROFILE_VP9_3,
}),
C2F(m_profileLevel, C2ProfileLevelStruct::level)
.oneOf({
Expand Down Expand Up @@ -523,15 +524,9 @@ MfxC2DecoderComponent::MfxC2DecoderComponent(const C2String name, const CreateCo
C2F(m_profileLevel, C2ProfileLevelStruct::level)
.oneOf({
LEVEL_AV1_2, LEVEL_AV1_2_1,
LEVEL_AV1_2_2, LEVEL_AV1_2_3,
LEVEL_AV1_2_1, LEVEL_AV1_2_3,
LEVEL_AV1_3, LEVEL_AV1_3_1,
LEVEL_AV1_3_2, LEVEL_AV1_3_3,
LEVEL_AV1_4, LEVEL_AV1_4_1,
LEVEL_AV1_4_2, LEVEL_AV1_4_3,
LEVEL_AV1_5, LEVEL_AV1_5_1,
LEVEL_AV1_5_2, LEVEL_AV1_5_3,
LEVEL_AV1_6, LEVEL_AV1_6_1,
LEVEL_AV1_6_2, LEVEL_AV1_6_3,
LEVEL_AV1_3_2,
}),})
.withSetter(ProfileLevelSetter, m_size)
.build());
Expand Down Expand Up @@ -882,6 +877,14 @@ void MfxC2DecoderComponent::InitFrameConstructor()
case DECODER_AV1:
fc_type = MfxC2FC_AV1;
break;
#ifdef ENABLE_WIDEVINE
case DECODER_H264_SECURE:
fc_type = MfxC2FC_SEC_AVC;
break;
case DECODER_H265_SECURE:
fc_type = MfxC2FC_SEC_HEVC;
break;
#endif
default:
MFX_DEBUG_TRACE_MSG("unhandled codec type: BUG in plug-ins registration");
fc_type = MfxC2FC_None;
Expand Down Expand Up @@ -1026,9 +1029,11 @@ mfxStatus MfxC2DecoderComponent::ResetSettings()

switch (m_decoderType)
{
case DECODER_H264_SECURE:
case DECODER_H264:
m_mfxVideoParams.mfx.CodecId = MFX_CODEC_AVC;
break;
case DECODER_H265_SECURE:
case DECODER_H265:
m_mfxVideoParams.mfx.CodecId = MFX_CODEC_HEVC;
break;
Expand Down Expand Up @@ -1913,7 +1918,7 @@ void MfxC2DecoderComponent::EmptyReadViews(uint64_t timestamp, uint64_t frame_in

auto it = m_duplicatedTimeStamp.begin();
for (; it != m_duplicatedTimeStamp.end(); it++) {
if (it->first < timestamp) {
if (it->first <= timestamp) {
ReleaseReadViews(it->second);
}
}
Expand Down Expand Up @@ -1968,15 +1973,6 @@ void MfxC2DecoderComponent::DoWork(std::unique_ptr<C2Work>&& work)
}
}
return;
} else if (DECODER_AV1 == m_decoderType && m_c2Bitstream->IsInReset()) {
if (true == m_bInitialized) {
mfxStatus format_change_sts = HandleFormatChange();
MFX_DEBUG_TRACE__mfxStatus(format_change_sts);
mfx_sts = format_change_sts;
if (MFX_ERR_NONE != mfx_sts) {
FreeDecoder();
}
}
}

do {
Expand Down
Loading

0 comments on commit ecae1b9

Please sign in to comment.