1 /*
2 * Copyright (c) 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 //! \file     decode_mpeg2_reference_frames.cpp
24 //! \brief    Defines reference list related logic for mpeg2 decode
25 //!
26 
27 #include "decode_mpeg2_basic_feature.h"
28 #include "decode_utils.h"
29 #include "codec_utilities_next.h"
30 #include "decode_mpeg2_reference_frames.h"
31 #include "codec_def_decode_mpeg2.h"
32 
33 namespace decode{
34 
Mpeg2ReferenceFrames()35 Mpeg2ReferenceFrames::Mpeg2ReferenceFrames()
36 {
37     MOS_ZeroMemory(m_refList, sizeof(m_refList));
38 }
39 
Init(Mpeg2BasicFeature * basicFeature,DecodeAllocator & allocator)40 MOS_STATUS Mpeg2ReferenceFrames::Init(Mpeg2BasicFeature *basicFeature, DecodeAllocator& allocator)
41 {
42     DECODE_FUNC_CALL();
43     DECODE_CHK_NULL(basicFeature);
44 
45     m_basicFeature = basicFeature;
46     m_allocator = &allocator;
47     DECODE_CHK_STATUS(CodecUtilities::CodecHalAllocateDataList(m_refList, CODECHAL_NUM_UNCOMPRESSED_SURFACE_MPEG2));
48 
49     //mark all reference res as invalid when they are no initialized
50     for (uint32_t i = 0; i < CODECHAL_NUM_UNCOMPRESSED_SURFACE_MPEG2; i++)
51     {
52         m_refList[i]->RefPic.PicFlags = PICTURE_INVALID;
53     }
54 
55     return MOS_STATUS_SUCCESS;
56 }
57 
~Mpeg2ReferenceFrames()58 Mpeg2ReferenceFrames::~Mpeg2ReferenceFrames()
59 {
60     DECODE_FUNC_CALL();
61     CodecUtilities::CodecHalFreeDataList(m_refList, CODECHAL_NUM_UNCOMPRESSED_SURFACE_MPEG2);
62     m_activeReferenceList.clear();
63 }
64 
UpdatePicture(CodecDecodeMpeg2PicParams & picParams)65 MOS_STATUS Mpeg2ReferenceFrames::UpdatePicture(CodecDecodeMpeg2PicParams &picParams)
66 {
67     DECODE_FUNC_CALL();
68 
69     DECODE_CHK_STATUS(UpdateCurFrame(picParams));
70     DECODE_CHK_STATUS(UpdateCurRefList(picParams));
71 
72     return MOS_STATUS_SUCCESS;
73 }
74 
UpdateCurResource(const CodecDecodeMpeg2PicParams & picParams)75 MOS_STATUS Mpeg2ReferenceFrames::UpdateCurResource(const CodecDecodeMpeg2PicParams &picParams)
76 {
77     DECODE_FUNC_CALL();
78 
79     PCODEC_REF_LIST destEntry = m_refList[picParams.m_currPic.FrameIdx];
80     destEntry->resRefPic = m_basicFeature->m_destSurface.OsResource;
81 
82     return MOS_STATUS_SUCCESS;
83 }
84 
UpdateCurFrame(const CodecDecodeMpeg2PicParams & picParams)85 MOS_STATUS Mpeg2ReferenceFrames::UpdateCurFrame(const CodecDecodeMpeg2PicParams &picParams)
86 {
87     DECODE_FUNC_CALL();
88 
89     CODEC_PICTURE currPic     = picParams.m_currPic;
90     PCODEC_REF_LIST destEntry = m_refList[currPic.FrameIdx];
91     destEntry->RefPic         = currPic;
92     DECODE_CHK_STATUS(UpdateCurResource(picParams));
93 
94     return MOS_STATUS_SUCCESS;
95 }
96 
UpdateCurRefList(const CodecDecodeMpeg2PicParams & picParams)97 MOS_STATUS Mpeg2ReferenceFrames::UpdateCurRefList(const CodecDecodeMpeg2PicParams &picParams)
98 {
99     DECODE_FUNC_CALL();
100 
101     // Do not use data that has not been initialized
102     if (CodecHal_PictureIsInvalid(m_refList[m_basicFeature->m_fwdRefIdx]->RefPic))
103     {
104         m_basicFeature->m_fwdRefIdx = picParams.m_currPic.FrameIdx;
105     }
106     if (CodecHal_PictureIsInvalid(m_refList[m_basicFeature->m_bwdRefIdx]->RefPic))
107     {
108         m_basicFeature->m_bwdRefIdx = picParams.m_currPic.FrameIdx;
109     }
110 
111     // Override reference list with ref surface passed from DDI if needed
112     uint8_t surfCount = 0;
113     uint8_t surfIndex = 0;
114     while (surfCount < m_basicFeature->m_refSurfaceNum && surfIndex < CODECHAL_NUM_UNCOMPRESSED_SURFACE_MPEG2)
115     {
116         if (!m_allocator->ResourceIsNull(&m_basicFeature->m_refFrameSurface[surfIndex].OsResource))
117         {
118             m_refList[surfIndex]->resRefPic = m_basicFeature->m_refFrameSurface[surfIndex].OsResource;
119             surfCount++;
120         }
121         surfIndex++;
122     }
123 
124     return MOS_STATUS_SUCCESS;
125 }
126 
127 }  // namespace decode
128