1 /*
2 * Copyright (c) 2017, 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     codechal_encode_tracked_buffer.cpp
24 //! \brief    Class to manage tracked buffer used in encoder
25 //!
26 
27 #include "codechal_encode_tracked_buffer_hevc.h"
28 #include "codechal_encoder_base.h"
29 
LookUpBufIndexMbCode()30 void CodechalEncodeTrackedBufferHevc::LookUpBufIndexMbCode()
31 {
32     // the value of current index will recycle in CODEC_NUM_REF_BUFFERS, CODEC_NUM_REF_BUFFERS + 2, CODEC_NUM_REF_BUFFERS + 1
33     m_mbCodeAnteIdx = m_mbCodePenuIdx;
34     m_mbCodePenuIdx = m_mbCodeCurrIdx;
35     m_mbCodeCurrIdx = m_mbCodeCurrIdx % CODEC_NUM_NON_REF_BUFFERS;
36     m_mbCodeCurrIdx += CODEC_NUM_REF_BUFFERS;
37 }
38 
AllocateMvTemporalBuffer()39 MOS_STATUS CodechalEncodeTrackedBufferHevc::AllocateMvTemporalBuffer()
40 {
41     CODECHAL_ENCODE_FUNCTION_ENTER;
42 
43     if ((m_trackedBufCurrMvTemporal = (MOS_RESOURCE*)m_allocator->GetResource(m_standard, mvTemporalBuffer, m_trackedBufCurrIdx)))
44     {
45         return MOS_STATUS_SUCCESS;
46     }
47 
48     CODECHAL_ENCODE_CHK_NULL_RETURN(m_hevcState);
49 
50     CODECHAL_ENCODE_CHK_NULL_RETURN(m_trackedBufCurrMvTemporal = (MOS_RESOURCE*)m_allocator->AllocateResource(
51         m_standard, m_hevcState->m_sizeOfMvTemporalBuffer, 1, mvTemporalBuffer, "mvTemporalBuffer", m_trackedBufCurrIdx, true));
52 
53     return MOS_STATUS_SUCCESS;
54 }
55 
DeferredDeallocateOnResChange()56 void CodechalEncodeTrackedBufferHevc::DeferredDeallocateOnResChange()
57 {
58     CodechalEncodeTrackedBuffer::DeferredDeallocateOnResChange();
59 
60     // release MbCode buffer
61     if ((m_mbCodeAnteIdx != m_mbCodePenuIdx) &&
62         (m_mbCodeAnteIdx != m_mbCodeCurrIdx))
63     {
64         ReleaseMbCode(m_mbCodeAnteIdx);
65     }
66 }
67 
CodechalEncodeTrackedBufferHevc(CodechalEncoderState * encoder)68 CodechalEncodeTrackedBufferHevc::CodechalEncodeTrackedBufferHevc(CodechalEncoderState* encoder)
69     : CodechalEncodeTrackedBuffer(encoder)
70 {
71     m_hevcState = dynamic_cast<CodechalEncodeHevcBase*>(encoder);
72 
73     // HEVC does not need to keep ref frame's PakObject, can use a size-3 ring buffer instead
74     m_mbCodeIsTracked = false;
75 
76     m_mbCodeAnteIdx = m_mbCodePenuIdx = m_mbCodeCurrIdx = 0;
77 }
78