Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SampleCorrection.cpp « src - github.com/mpc-hc/sanear.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8aafcbf000c7b77e5ca64c649d136d5b376e2b8a (plain)
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
#include "pch.h"
#include "SampleCorrection.h"

namespace SaneAudioRenderer
{
    void SampleCorrection::NewFormat(SharedWaveFormat format)
    {
        assert(format);
        assert(format->nSamplesPerSec > 0);

        if (m_format)
        {
            m_segmentTimeInPreviousFormats += FramesToTime(m_segmentFramesInCurrentFormat);
            m_segmentFramesInCurrentFormat = 0;
        }

        m_format = format;
        m_bitstream = (DspFormatFromWaveFormat(*m_format) == DspFormat::Unknown);
    }

    void SampleCorrection::NewSegment(double rate)
    {
        assert(rate > 0.0);

        m_rate = rate;

        m_segmentTimeInPreviousFormats = 0;
        m_segmentFramesInCurrentFormat = 0;

        m_lastFrameEnd = 0;

        m_timeDivergence = 0;
    }

    void SampleCorrection::NewDeviceBuffer()
    {
        m_freshBuffer = true;
    }

    DspChunk SampleCorrection::ProcessSample(IMediaSample* pSample, AM_SAMPLE2_PROPERTIES& sampleProps, bool realtimeDevice)
    {
        assert(m_format);

        DspChunk chunk(pSample, sampleProps, *m_format);

        if (m_bitstream)
        {
            if (m_freshBuffer && !(sampleProps.dwSampleFlags & AM_SAMPLE_SPLICEPOINT))
            {
                // Drop the sample.
                DebugOut("SampleCorrection drop [", sampleProps.tStart, sampleProps.tStop, "]");
                chunk = DspChunk();
                assert(chunk.IsEmpty());
            }
        }
        else if (!realtimeDevice && (m_lastFrameEnd == 0 || (sampleProps.dwSampleFlags & AM_SAMPLE_TIMEDISCONTINUITY)))
        {
            if ((sampleProps.dwSampleFlags & AM_SAMPLE_STOPVALID) && sampleProps.tStop <= m_lastFrameEnd)
            {
                // Drop the sample.
                DebugOut("SampleCorrection drop [", sampleProps.tStart, sampleProps.tStop, "]");
                chunk = DspChunk();
                assert(chunk.IsEmpty());
            }
            else if ((sampleProps.dwSampleFlags & AM_SAMPLE_TIMEVALID) && sampleProps.tStart < m_lastFrameEnd)
            {
                // Crop the sample.
                const size_t cropFrames = (size_t)TimeToFrames(m_lastFrameEnd - sampleProps.tStart);

                if (cropFrames > 0)
                {
                    DebugOut("SampleCorrection crop", cropFrames, "frames from [",
                             sampleProps.tStart, sampleProps.tStop, "]");

                    chunk.ShrinkHead(chunk.GetFrameCount() > cropFrames ? chunk.GetFrameCount() - cropFrames : 0);
                }
            }
            else if ((sampleProps.dwSampleFlags & AM_SAMPLE_TIMEVALID) && sampleProps.tStart > m_lastFrameEnd)
            {
                // Zero-pad the sample.
                const size_t padFrames = (size_t)TimeToFrames(sampleProps.tStart - m_lastFrameEnd);

                if (padFrames > 0 &&
                    FramesToTime(padFrames) < 10 * OneSecond)
                {
                    DebugOut("SampleCorrection pad", padFrames, "frames before [",
                             sampleProps.tStart, sampleProps.tStop, "]");

                    DspChunk tempChunk(chunk.GetFormat(), chunk.GetChannelCount(),
                                       chunk.GetFrameCount() + padFrames, chunk.GetRate());

                    const size_t padBytes = padFrames * chunk.GetFrameSize();
                    sampleProps.pbBuffer = nullptr;
                    sampleProps.lActual += (int32_t)padBytes;
                    sampleProps.tStart -= FramesToTime(padFrames);

                    assert(tempChunk.GetSize() == chunk.GetSize() + padBytes);
                    ZeroMemory(tempChunk.GetData(), padBytes);
                    memcpy(tempChunk.GetData() + padBytes, chunk.GetData(), chunk.GetSize());

                    chunk = std::move(tempChunk);
                }
            }
        }

        AccumulateTimings(sampleProps, chunk.GetFrameCount());

        return chunk;
    }

    uint64_t SampleCorrection::TimeToFrames(REFERENCE_TIME time)
    {
        assert(m_format);
        assert(m_rate > 0.0);
        return (size_t)(llMulDiv(time, m_format->nSamplesPerSec, OneSecond, 0) * m_rate);
    }

    REFERENCE_TIME SampleCorrection::FramesToTime(uint64_t frames)
    {
        assert(m_format);
        assert(m_rate > 0.0);
        return (REFERENCE_TIME)(llMulDiv(frames, OneSecond, m_format->nSamplesPerSec, 0) / m_rate);
    }

    void SampleCorrection::AccumulateTimings(AM_SAMPLE2_PROPERTIES& sampleProps, size_t frames)
    {
        assert(m_format);
        assert(m_rate > 0.0);

        if (frames == 0)
            return;

        if (sampleProps.dwSampleFlags & AM_SAMPLE_TIMEVALID)
            m_timeDivergence = sampleProps.tStart - m_lastFrameEnd;

        m_segmentFramesInCurrentFormat += frames;

        m_lastFrameEnd = m_segmentTimeInPreviousFormats + FramesToTime(m_segmentFramesInCurrentFormat);

        m_freshBuffer = false;
    }
}