1 /*
2 * Copyright (c) 2019-2021, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 
23 //!
24 //! \file     decode_input_bitstream.cpp
25 //! \brief    Defines the common interface for decode input bitstream
26 //! \details  Defines the interface to handle the decode input bitstream in
27 //!           both single execution call mode and multiple excution call mode.
28 //!
29 #include "decode_input_bitstream.h"
30 #include "decode_basic_feature.h"
31 #include "decode_pipeline.h"
32 
33 #include "decode_huc_packet_creator_base.h"
34 
35 namespace decode {
36 
DecodeInputBitstream(DecodePipeline * pipeline,MediaTask * task,uint8_t numVdbox)37 DecodeInputBitstream::DecodeInputBitstream(DecodePipeline* pipeline, MediaTask* task, uint8_t numVdbox)
38     : DecodeSubPipeline(pipeline, task, numVdbox)
39 {}
40 
~DecodeInputBitstream()41 DecodeInputBitstream::~DecodeInputBitstream()
42 {
43     m_allocator->Destroy(m_catenatedBuffer);
44 }
45 
Init(CodechalSetting & settings)46 MOS_STATUS DecodeInputBitstream::Init(CodechalSetting& settings)
47 {
48     DECODE_CHK_NULL(m_pipeline);
49 
50     CodechalHwInterface* hwInterface = m_pipeline->GetHwInterface();
51     DECODE_CHK_NULL(hwInterface);
52     PMOS_INTERFACE osInterface = hwInterface->GetOsInterface();
53     DECODE_CHK_NULL(osInterface);
54     InitScalabilityPars(osInterface);
55 
56     m_allocator = m_pipeline->GetDecodeAllocator();
57     DECODE_CHK_NULL(m_allocator);
58 
59     MediaFeatureManager* featureManager = m_pipeline->GetFeatureManager();
60     DECODE_CHK_NULL(featureManager);
61     m_basicFeature = dynamic_cast<DecodeBasicFeature*>(featureManager->GetFeature(FeatureIDs::basicFeature));
62     DECODE_CHK_NULL(m_basicFeature);
63 
64     HucPacketCreatorBase *hucPktCreator = dynamic_cast<HucPacketCreatorBase *>(m_pipeline);
65     DECODE_CHK_NULL(hucPktCreator);
66     m_concatPkt = hucPktCreator->CreateHucCopyPkt(m_pipeline, m_task, hwInterface);
67     DECODE_CHK_NULL(m_concatPkt);
68     MediaPacket *packet = dynamic_cast<MediaPacket *>(m_concatPkt);
69     DECODE_CHK_NULL(packet);
70     DECODE_CHK_STATUS(RegisterPacket(DecodePacketId(m_pipeline, hucCopyPacketId), *packet));
71     DECODE_CHK_STATUS(packet->Init());
72 
73     return MOS_STATUS_SUCCESS;
74 }
75 
Prepare(DecodePipelineParams & params)76 MOS_STATUS DecodeInputBitstream::Prepare(DecodePipelineParams& params)
77 {
78     if (params.m_pipeMode == decodePipeModeBegin)
79     {
80         DECODE_CHK_STATUS(Begin());
81     }
82     else if (params.m_pipeMode == decodePipeModeProcess)
83     {
84         DECODE_CHK_NULL(params.m_params);
85         CodechalDecodeParams *decodeParams = params.m_params;
86         DECODE_CHK_STATUS(Append(*decodeParams));
87     }
88 
89     return MOS_STATUS_SUCCESS;
90 }
91 
Begin()92 MOS_STATUS DecodeInputBitstream::Begin()
93 {
94     DECODE_CHK_STATUS(DecodeSubPipeline::Reset());
95 
96     m_segmentsTotalSize = 0;
97     return MOS_STATUS_SUCCESS;
98 }
99 
AllocateCatenatedBuffer()100 MOS_STATUS DecodeInputBitstream::AllocateCatenatedBuffer()
101 {
102     DECODE_CHK_NULL(m_allocator);
103 
104     uint32_t allocSize = MOS_ALIGN_CEIL(m_requiredSize, MHW_CACHELINE_SIZE);
105 
106     if (m_catenatedBuffer == nullptr)
107     {
108         m_catenatedBuffer = m_allocator->AllocateBuffer(
109             allocSize, "bitstream", resourceInputBitstream, notLockableVideoMem);
110         DECODE_CHK_NULL(m_catenatedBuffer);
111         return MOS_STATUS_SUCCESS;
112     }
113 
114     DECODE_CHK_STATUS(m_allocator->Resize(m_catenatedBuffer, allocSize, notLockableVideoMem));
115     return MOS_STATUS_SUCCESS;
116 }
117 
Append(const CodechalDecodeParams & decodeParams)118 MOS_STATUS DecodeInputBitstream::Append(const CodechalDecodeParams &decodeParams)
119 {
120     uint32_t segmentSize = decodeParams.m_dataSize;
121 
122     bool firstExecuteCall = (decodeParams.m_executeCallIndex == 0);
123     if (firstExecuteCall)
124     {
125         m_requiredSize = m_basicFeature->m_dataSize;
126         bool isIncompleteBitstream = (segmentSize < m_requiredSize);
127         if (isIncompleteBitstream)
128         {
129             DECODE_CHK_STATUS(AllocateCatenatedBuffer());
130             m_basicFeature->m_resDataBuffer = *m_catenatedBuffer;
131             m_basicFeature->m_dataOffset = 0;
132             DECODE_CHK_STATUS(ActivatePacket(DecodePacketId(m_pipeline, hucCopyPacketId), true, 0, 0));
133             AddNewSegment(*(decodeParams.m_dataBuffer), decodeParams.m_dataOffset, decodeParams.m_dataSize);
134         }
135     }
136     else
137     {
138         if(m_segmentsTotalSize + segmentSize > m_requiredSize)
139         {
140             DECODE_ASSERTMESSAGE("Bitstream size exceeds allocated buffer size!");
141             return MOS_STATUS_INVALID_PARAMETER;
142         }
143         DECODE_CHK_STATUS(ActivatePacket(DecodePacketId(m_pipeline, hucCopyPacketId), true, 0, 0));
144         AddNewSegment(*(decodeParams.m_dataBuffer), decodeParams.m_dataOffset, decodeParams.m_dataSize);
145     }
146 
147     m_segmentsTotalSize += MOS_ALIGN_CEIL(segmentSize, MHW_CACHELINE_SIZE);
148 
149     return MOS_STATUS_SUCCESS;
150 }
151 
AddNewSegment(MOS_RESOURCE & resource,uint32_t offset,uint32_t size)152 void DecodeInputBitstream::AddNewSegment(MOS_RESOURCE& resource, uint32_t offset, uint32_t size)
153 {
154     HucCopyPktItf::HucCopyParams copyParams;
155     copyParams.srcBuffer    = &resource;
156     copyParams.srcOffset    = offset;
157     copyParams.destBuffer   = &(m_catenatedBuffer->OsResource);
158     copyParams.destOffset   = m_segmentsTotalSize;
159     copyParams.copyLength   = size;
160     m_concatPkt->PushCopyParams(copyParams);
161 }
162 
IsComplete()163 bool DecodeInputBitstream::IsComplete()
164 {
165     return (m_segmentsTotalSize >= m_requiredSize);
166 }
167 
GetMediaFunction()168 MediaFunction DecodeInputBitstream::GetMediaFunction()
169 {
170     return VdboxDecodeWaFunc;
171 }
172 
InitScalabilityPars(PMOS_INTERFACE osInterface)173 void DecodeInputBitstream::InitScalabilityPars(PMOS_INTERFACE osInterface)
174 {
175     m_decodeScalabilityPars.disableScalability = true;
176     m_decodeScalabilityPars.disableRealTile = true;
177     m_decodeScalabilityPars.enableVE = MOS_VE_SUPPORTED(osInterface);
178     m_decodeScalabilityPars.numVdbox = m_numVdbox;
179 }
180 
181 }
182