Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base for C2 hucbuffer dev #188

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions c2_buffers/src/mfx_c2_bitstream_in.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ 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 @@ -90,6 +92,25 @@ 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("ZHDEBUG: encryptedBlock: " << FormatHex(infobuffer, encryptedBlock->size()));
} else {
MFX_DEBUG_TRACE_STREAM("ZHDEBUG: encryptedBlock is null");
}

do {
if (!frame_view) {
res = C2_BAD_VALUE;
Expand All @@ -114,15 +135,30 @@ c2_status_t MfxC2BitstreamIn::AppendFrame(const C2FrameData& buf_pack, c2_nsecs_
data = read_view->data() + c_linear_block->offset();
filled_len = c_linear_block->size();

MFX_DEBUG_TRACE_I64(filled_len);
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_DEBUG_TRACE_STREAM("ZHDEBUG: 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_DEBUG_TRACE_STREAM("ZHDEBUG: hucBuffer->pr_magic != PROTECTED_DATA_BUFFER_MAGIC");
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 Down
51 changes: 30 additions & 21 deletions c2_components/include/mfx_c2_decoder_component.h
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,16 @@ 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 {};

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

Expand All @@ -183,12 +197,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 +207,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 +216,6 @@ 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 Expand Up @@ -246,6 +252,9 @@ class MfxC2DecoderComponent : public MfxC2Component
unsigned int m_uOutputDelay = 8u;
unsigned int m_uInputDelay = 0u;

protected:
bool m_secure;

#if MFX_DEBUG_DUMP_FRAME == MFX_DEBUG_YES
int m_count = 0;
std::mutex m_count_lock;
Expand Down
47 changes: 47 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,47 @@
// 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_c2_setters.h"
#include <cutils/properties.h>

class MfxC2SecureDecoderComponent : public MfxC2DecoderComponent
{
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)


private:
/* -----------------------C2Parameters--------------------------- */
std::shared_ptr<C2SecureModeTuning> m_secureMode;
};

2 changes: 2 additions & 0 deletions c2_components/src/mfx_c2_components_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#else
#include "mfx_c2_decoder_component.h"
#include "mfx_c2_encoder_component.h"
#include "mfx_c2_secure_decoder_component.h"
#endif

using namespace android;
Expand Down Expand Up @@ -65,6 +66,7 @@ MfxC2ComponentsRegistry::MfxC2ComponentsRegistry()
#else
MfxC2DecoderComponent::RegisterClass(*this);
MfxC2EncoderComponent::RegisterClass(*this);
MfxC2SecureDecoderComponent::RegisterClass(*this);
#endif
}

Expand Down
22 changes: 20 additions & 2 deletions c2_components/src/mfx_c2_decoder_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ constexpr uint64_t kMinInputBufferSize = 1 * WIDTH_1K * HEIGHT_1K;
constexpr uint64_t kDefaultConsumerUsage =
(GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_COMPOSER);

constexpr uint64_t kProtectedUsage = C2MemoryUsage::READ_PROTECTED;

// Android S declared VP8 profile
#if PLATFORM_SDK_VERSION <= 30 // Android 11(R)
Expand Down Expand Up @@ -169,7 +170,8 @@ MfxC2DecoderComponent::MfxC2DecoderComponent(const C2String name, const CreateCo
m_bInitialized(false),
m_uSyncedPointsCount(0),
m_bSetHdrStatic(false),
m_surfaceNum(0)
m_surfaceNum(0),
m_secure(false)
{
MFX_DEBUG_TRACE_FUNC;
const unsigned int SINGLE_STREAM_ID = 0u;
Expand Down Expand Up @@ -222,6 +224,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 +280,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 @@ -883,6 +887,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 @@ -1028,9 +1040,11 @@ mfxStatus MfxC2DecoderComponent::ResetSettings()
switch (m_decoderType)
{
case DECODER_H264:
case DECODER_H264_SECURE:
m_mfxVideoParams.mfx.CodecId = MFX_CODEC_AVC;
break;
case DECODER_H265:
case DECODER_H265_SECURE:
m_mfxVideoParams.mfx.CodecId = MFX_CODEC_HEVC;
break;
case DECODER_VP9:
Expand Down Expand Up @@ -1107,6 +1121,10 @@ mfxStatus MfxC2DecoderComponent::InitDecoder(std::shared_ptr<C2BlockPool> c2_all
mfxU16 cropW = 0, cropH = 0;
std::lock_guard<std::mutex> lock(m_initDecoderMutex);

MFX_DEBUG_TRACE_I32(m_secure);
if (m_secure)
m_consumerUsage |= kProtectedUsage;

{
MFX_DEBUG_TRACE_MSG("InitDecoder: DecodeHeader");

Expand Down Expand Up @@ -1971,7 +1989,7 @@ void MfxC2DecoderComponent::DoWork(std::unique_ptr<C2Work>&& work)
const auto incoming_flags = work->input.flags;

MFX_DEBUG_TRACE_STREAM("work: " << work.get() << "; index: " << incoming_frame_index.peeku() <<
" flags: " << std::hex << incoming_flags);
" flags: " << std::hex << incoming_flags << " infoBuffers: " << work->input.infoBuffers.size());

bool expect_output = false;
bool flushing = false;
Expand Down
Loading