File: sample_vpp_frc_adv.cpp

package info (click to toggle)
libvpl-tools 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,652 kB
  • sloc: cpp: 107,469; python: 4,303; ansic: 3,202; sh: 159; lisp: 52; makefile: 13
file content (182 lines) | stat: -rw-r--r-- 5,955 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*############################################################################
  # Copyright (C) 2005 Intel Corporation
  #
  # SPDX-License-Identifier: MIT
  ############################################################################*/

#include "sample_vpp_frc_adv.h"
#include <math.h>
#include <algorithm>
#include "vm/strings_defs.h"

#ifndef MFX_VERSION
    #error MFX_VERSION not defined
#endif

static const mfxU32 MFX_TIME_STAMP_FREQUENCY = 90000; // will go to mfxdefs.h

bool FRCAdvancedChecker::IsTimeStampsNear(mfxU64 timeStampRef, mfxU64 timeStampTst, mfxU64 eps) {
    mfxU32 absDiff = abs((mfxI32)(timeStampTst - (timeStampRef)));
    if (absDiff <= eps) {
        return true;
    }
    else {
        printf("\n\nError in FRC Advanced algorithm. \n");

        printf("Output frame number is %d\n", m_numOutputFrames - 1);

        int iPTS_Ref = (int)timeStampRef;
        int iPTS_Tst = (int)timeStampTst;
        int iAbsDiff = (int)absDiff;
        int iEps     = (int)eps;

        printf("Error: refTimeStamp, tstTimeStamp, Diff, Delta are: %d %d %u %d\n",
               iPTS_Ref,
               iPTS_Tst,
               iAbsDiff,
               iEps);

        return false;
    }

} // bool IsTimeStampsNear( mfxU64 timeStampTst, mfxU64 timeStampRef,  mfxU64 eps)

FRCAdvancedChecker::FRCAdvancedChecker()
        : m_minDeltaTime(0),
          m_bIsSetTimeOffset(false),
          m_timeOffset(0),
          m_expectedTimeStamp(0),
          m_timeStampJump(0),
          m_numOutputFrames(0),
          m_bReadyOutput(false),
          m_defferedInputTimeStamp(0),
          m_videoParam({ 0 }),
          m_ptsList() {} // FRCAdvancedChecker::FRCAdvancedChecker()

mfxStatus FRCAdvancedChecker::Init(mfxVideoParam* par, mfxU32 /*asyncDeep*/) {
    m_videoParam = *par;

    m_minDeltaTime = std::min(
        ((uint64_t)m_videoParam.vpp.In.FrameRateExtD * (uint64_t)MFX_TIME_STAMP_FREQUENCY) /
            (2 * (uint64_t)m_videoParam.vpp.In.FrameRateExtN),
        ((uint64_t)m_videoParam.vpp.Out.FrameRateExtD * (uint64_t)MFX_TIME_STAMP_FREQUENCY) /
            (2 * (uint64_t)m_videoParam.vpp.Out.FrameRateExtN));

    return MFX_ERR_NONE;

} // mfxStatus FRCAdvancedChecker::Init(mfxVideoParam *par, mfxU32 asyncDeep)

bool FRCAdvancedChecker::PutInputFrameAndCheck(mfxFrameSurface1* pSurface) {
    if (pSurface) {
        m_ptsList.push_back(pSurface->Data.TimeStamp);
    }

    return true;

} // bool FRCAdvancedChecker::PutInputFrameAndCheck(mfxFrameSurface1* pSurface)

bool FRCAdvancedChecker::PutOutputFrameAndCheck(mfxFrameSurface1* pSurface) {
    bool res;

    if (NULL == pSurface) {
        return false;
    }

    mfxU64 timeStampTst = pSurface->Data.TimeStamp;

    bool bRepeatAnalysis = false;
    do {
        //------------------------------------------------
        //           ReadyOutput
        //------------------------------------------------
        if (m_bReadyOutput) {
            m_expectedTimeStamp = GetExpectedPTS(m_numOutputFrames, m_timeOffset, m_timeStampJump);

            m_numOutputFrames++;

            res = IsTimeStampsNear(m_expectedTimeStamp, timeStampTst, m_minDeltaTime);

            return res;
        }
        else {
            //------------------------------------------------
            //           standard processing
            //------------------------------------------------
            if (0 == m_ptsList.size()) {
                if (m_numOutputFrames > 0) // last frame processing
                {
                    m_expectedTimeStamp =
                        GetExpectedPTS(m_numOutputFrames, m_timeOffset, m_timeStampJump);

                    m_numOutputFrames++;

                    res = IsTimeStampsNear(m_expectedTimeStamp, timeStampTst, m_minDeltaTime);

                    return res;
                }
                else {
                    return false; //?
                }
            }

            mfxU64 inputTimeStamp = m_ptsList.front();
            m_ptsList.pop_front();

            if (false == m_bIsSetTimeOffset) {
                m_bIsSetTimeOffset = true;
                m_timeOffset       = inputTimeStamp;
            }

            m_expectedTimeStamp = GetExpectedPTS(m_numOutputFrames, m_timeOffset, m_timeStampJump);

            mfxU32 timeStampDifference = abs((mfxI32)(inputTimeStamp - m_expectedTimeStamp));

            // process irregularity
            if (m_minDeltaTime > timeStampDifference) {
                inputTimeStamp = m_expectedTimeStamp;
            }

            if (inputTimeStamp < m_expectedTimeStamp) {
                m_bReadyOutput = false;

                // skip frame
                // request new one input surface
                //return MFX_ERR_MORE_DATA;
                bRepeatAnalysis = true;
            }
            else if (inputTimeStamp == m_expectedTimeStamp) // see above (minDelta)
            {
                m_bReadyOutput = false;

                m_numOutputFrames++;

                res = IsTimeStampsNear(m_expectedTimeStamp, timeStampTst, m_minDeltaTime);

                return res;
            }
            else // inputTimeStampParam > ptr->expectedTimeStamp
            {
                m_numOutputFrames++;

                res = IsTimeStampsNear(m_expectedTimeStamp, timeStampTst, m_minDeltaTime);

                return res;
            }
        }
    } while (bRepeatAnalysis);

    return false;

} // bool  FRCAdvancedChecker::PutOutputFrameAndCheck(mfxFrameSurface1* pSurface)

mfxU64 FRCAdvancedChecker::GetExpectedPTS(mfxU32 frameNumber, mfxU64 timeOffset, mfxU64 timeJump) {
    mfxU64 expectedPTS =
        (((mfxU64)frameNumber * m_videoParam.vpp.Out.FrameRateExtD * MFX_TIME_STAMP_FREQUENCY) /
             m_videoParam.vpp.Out.FrameRateExtN +
         timeOffset + timeJump);

    return expectedPTS;

} // mfxU64  FRCAdvancedChecker::GetExpectedPTS( mfxU32 frameNumber, mfxU64 timeOffset, mfxU64 timeJump )

/* EOF */