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 #include "decode_huc_packet_creator_base.h"
33 
34 namespace decode {
35 
DecodeInputBitstream(DecodePipeline * pipeline,MediaTask * task,uint8_t numVdbox)36 DecodeInputBitstream::DecodeInputBitstream(DecodePipeline* pipeline, MediaTask* task, uint8_t numVdbox)
37     : DecodeSubPipeline(pipeline, task, numVdbox)
38 {}
39 
~DecodeInputBitstream()40 DecodeInputBitstream::~DecodeInputBitstream()
41 {
42     if (m_allocator)
43     {
44         m_allocator->Destroy(m_catenatedBuffer);
45     }
46 }
47 
Init(CodechalSetting & settings)48 MOS_STATUS DecodeInputBitstream::Init(CodechalSetting& settings)
49 {
50     DECODE_CHK_NULL(m_pipeline);
51 
52     CodechalHwInterfaceNext *hwInterface = m_pipeline->GetHwInterface();
53     DECODE_CHK_NULL(hwInterface);
54     PMOS_INTERFACE osInterface = hwInterface->GetOsInterface();
55     DECODE_CHK_NULL(osInterface);
56     InitScalabilityPars(osInterface);
57 
58     m_allocator = m_pipeline->GetDecodeAllocator();
59     DECODE_CHK_NULL(m_allocator);
60 
61     MediaFeatureManager* featureManager = m_pipeline->GetFeatureManager();
62     DECODE_CHK_NULL(featureManager);
63     m_basicFeature = dynamic_cast<DecodeBasicFeature*>(featureManager->GetFeature(FeatureIDs::basicFeature));
64     DECODE_CHK_NULL(m_basicFeature);
65 
66     HucPacketCreatorBase *hucPktCreator = dynamic_cast<HucPacketCreatorBase *>(m_pipeline);
67     DECODE_CHK_NULL(hucPktCreator);
68     m_concatPkt = hucPktCreator->CreateHucCopyPkt(m_pipeline, m_task, hwInterface);
69     DECODE_CHK_NULL(m_concatPkt);
70     MediaPacket *packet = dynamic_cast<MediaPacket *>(m_concatPkt);
71     DECODE_CHK_NULL(packet);
72     DECODE_CHK_STATUS(RegisterPacket(DecodePacketId(m_pipeline, hucCopyPacketId), *packet));
73     DECODE_CHK_STATUS(packet->Init());
74 
75     return MOS_STATUS_SUCCESS;
76 }
77 
Prepare(DecodePipelineParams & params)78 MOS_STATUS DecodeInputBitstream::Prepare(DecodePipelineParams& params)
79 {
80     if (params.m_pipeMode == decodePipeModeBegin)
81     {
82         DECODE_CHK_STATUS(Begin());
83     }
84     else if (params.m_pipeMode == decodePipeModeProcess)
85     {
86         DECODE_CHK_NULL(params.m_params);
87         CodechalDecodeParams *decodeParams = params.m_params;
88         DECODE_CHK_STATUS(Append(*decodeParams));
89     }
90 
91     return MOS_STATUS_SUCCESS;
92 }
93 
Begin()94 MOS_STATUS DecodeInputBitstream::Begin()
95 {
96     DECODE_CHK_STATUS(DecodeSubPipeline::Reset());
97 
98     m_segmentsTotalSize = 0;
99     return MOS_STATUS_SUCCESS;
100 }
101 
AllocateCatenatedBuffer()102 MOS_STATUS DecodeInputBitstream::AllocateCatenatedBuffer()
103 {
104     DECODE_CHK_NULL(m_allocator);
105 
106     uint32_t allocSize = MOS_ALIGN_CEIL(m_requiredSize, MHW_CACHELINE_SIZE);
107 
108     if (m_catenatedBuffer == nullptr)
109     {
110         m_catenatedBuffer = m_allocator->AllocateBuffer(
111             allocSize, "bitstream", resourceInputBitstream, notLockableVideoMem);
112         DECODE_CHK_NULL(m_catenatedBuffer);
113         return MOS_STATUS_SUCCESS;
114     }
115 
116     DECODE_CHK_STATUS(m_allocator->Resize(m_catenatedBuffer, allocSize, notLockableVideoMem));
117     return MOS_STATUS_SUCCESS;
118 }
119 
Append(const CodechalDecodeParams & decodeParams)120 MOS_STATUS DecodeInputBitstream::Append(const CodechalDecodeParams &decodeParams)
121 {
122     uint32_t segmentSize = decodeParams.m_dataSize;
123 
124     bool firstExecuteCall = (decodeParams.m_executeCallIndex == 0);
125     if (firstExecuteCall)
126     {
127         m_requiredSize = m_basicFeature->m_dataSize;
128         bool isIncompleteBitstream = (segmentSize < m_requiredSize);
129         if (isIncompleteBitstream)
130         {
131             DECODE_CHK_STATUS(AllocateCatenatedBuffer());
132             m_basicFeature->m_resDataBuffer = *m_catenatedBuffer;
133             m_basicFeature->m_dataOffset = 0;
134             DECODE_CHK_STATUS(ActivatePacket(DecodePacketId(m_pipeline, hucCopyPacketId), true, 0, 0));
135             AddNewSegment(*(decodeParams.m_dataBuffer), decodeParams.m_dataOffset, decodeParams.m_dataSize);
136         }
137     }
138     else
139     {
140         if(m_segmentsTotalSize + segmentSize > m_requiredSize)
141         {
142             DECODE_ASSERTMESSAGE("Bitstream size exceeds allocated buffer size!");
143             return MOS_STATUS_INVALID_PARAMETER;
144         }
145         DECODE_CHK_STATUS(ActivatePacket(DecodePacketId(m_pipeline, hucCopyPacketId), true, 0, 0));
146         AddNewSegment(*(decodeParams.m_dataBuffer), decodeParams.m_dataOffset, decodeParams.m_dataSize);
147     }
148 
149     m_segmentsTotalSize += MOS_ALIGN_CEIL(segmentSize, MHW_CACHELINE_SIZE);
150 
151     return MOS_STATUS_SUCCESS;
152 }
153 
AddNewSegment(MOS_RESOURCE & resource,uint32_t offset,uint32_t size)154 void DecodeInputBitstream::AddNewSegment(MOS_RESOURCE& resource, uint32_t offset, uint32_t size)
155 {
156     HucCopyPktItf::HucCopyParams copyParams;
157     copyParams.srcBuffer    = &resource;
158     copyParams.srcOffset    = offset;
159     copyParams.destBuffer   = &(m_catenatedBuffer->OsResource);
160     copyParams.destOffset   = m_segmentsTotalSize;
161     copyParams.copyLength   = size;
162     m_concatPkt->PushCopyParams(copyParams);
163 }
164 
IsComplete()165 bool DecodeInputBitstream::IsComplete()
166 {
167     return (m_segmentsTotalSize >= m_requiredSize);
168 }
169 
GetMediaFunction()170 MediaFunction DecodeInputBitstream::GetMediaFunction()
171 {
172     return VdboxDecodeWaFunc;
173 }
174 
InitScalabilityPars(PMOS_INTERFACE osInterface)175 void DecodeInputBitstream::InitScalabilityPars(PMOS_INTERFACE osInterface)
176 {
177     m_decodeScalabilityPars.disableScalability = true;
178     m_decodeScalabilityPars.disableRealTile = true;
179     m_decodeScalabilityPars.enableVE = MOS_VE_SUPPORTED(osInterface);
180     m_decodeScalabilityPars.numVdbox = m_numVdbox;
181 }
182 
183 }
184