Coverage Report

Created: 2025-07-23 08:18

/src/x265/source/common/wavefront.cpp
Line
Count
Source (jump to first uncovered line)
1
/*****************************************************************************
2
 * Copyright (C) 2013-2020 MulticoreWare, Inc
3
 *
4
 * Authors: Steve Borho <steve@borho.org>
5
 *          Min Chen <chenm003@163.com>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
20
 *
21
 * This program is also available under a commercial proprietary license.
22
 * For more information, contact us at license @ x265.com
23
 *****************************************************************************/
24
25
#include "threadpool.h"
26
#include "threading.h"
27
#include "wavefront.h"
28
#include "common.h"
29
30
namespace X265_NS {
31
// x265 private namespace
32
33
bool WaveFront::init(int numRows)
34
0
{
35
0
    m_numRows = numRows;
36
37
0
    m_numWords = (numRows + 31) >> 5;
38
0
    m_internalDependencyBitmap = X265_MALLOC(uint32_t, m_numWords);
39
0
    if (m_internalDependencyBitmap)
40
0
        memset((void*)m_internalDependencyBitmap, 0, sizeof(uint32_t) * m_numWords);
41
42
0
    m_externalDependencyBitmap = X265_MALLOC(uint32_t, m_numWords);
43
0
    if (m_externalDependencyBitmap)
44
0
        memset((void*)m_externalDependencyBitmap, 0, sizeof(uint32_t) * m_numWords);
45
46
0
    m_row_to_idx = X265_MALLOC(uint32_t, m_numRows);
47
0
    m_idx_to_row = X265_MALLOC(uint32_t, m_numRows);
48
49
0
    return m_internalDependencyBitmap && m_externalDependencyBitmap;
50
0
}
51
52
WaveFront::~WaveFront()
53
0
{
54
0
    x265_free((void*)m_row_to_idx);
55
0
    x265_free((void*)m_idx_to_row);
56
57
0
    x265_free((void*)m_internalDependencyBitmap);
58
0
    x265_free((void*)m_externalDependencyBitmap);
59
0
}
60
61
void WaveFront::setLayerId(int layer)
62
0
{
63
0
    m_sLayerId = layer;
64
0
}
65
66
void WaveFront::clearEnabledRowMask()
67
0
{
68
0
    memset((void*)m_externalDependencyBitmap, 0, sizeof(uint32_t) * m_numWords);
69
0
    memset((void*)m_internalDependencyBitmap, 0, sizeof(uint32_t) * m_numWords);
70
0
}
71
72
void WaveFront::enqueueRow(int row)
73
0
{
74
0
    uint32_t bit = 1 << (row & 31);
75
0
    ATOMIC_OR(&m_internalDependencyBitmap[row >> 5], bit);
76
0
}
77
78
void WaveFront::enableRow(int row)
79
0
{
80
0
    uint32_t bit = 1 << (row & 31);
81
0
    ATOMIC_OR(&m_externalDependencyBitmap[row >> 5], bit);
82
0
}
83
84
void WaveFront::enableAllRows()
85
0
{
86
0
    memset((void*)m_externalDependencyBitmap, ~0, sizeof(uint32_t) * m_numWords);
87
0
}
88
89
bool WaveFront::dequeueRow(int row)
90
0
{
91
0
    uint32_t bit = 1 << (row & 31);
92
0
    return !!(ATOMIC_AND(&m_internalDependencyBitmap[row >> 5], ~bit) & bit);
93
0
}
94
95
void WaveFront::findJob(int threadId)
96
0
{
97
0
    unsigned long id;
98
99
    /* Loop over each word until all available rows are finished */
100
0
    for (int w = 0; w < m_numWords; w++)
101
0
    {
102
0
        uint32_t oldval = m_internalDependencyBitmap[w] & m_externalDependencyBitmap[w];
103
0
        while (oldval)
104
0
        {
105
0
            BSF(id, oldval);
106
107
0
            uint32_t bit = 1 << id;
108
0
            if (ATOMIC_AND(&m_internalDependencyBitmap[w], ~bit) & bit)
109
0
            {
110
                /* we cleared the bit, we get to process the row */
111
0
                processRow(w * 32 + id, threadId, m_sLayerId);
112
0
                m_helpWanted = true;
113
0
                return; /* check for a higher priority task */
114
0
            }
115
116
0
            oldval = m_internalDependencyBitmap[w] & m_externalDependencyBitmap[w];
117
0
        }
118
0
    }
119
120
0
    m_helpWanted = false;
121
0
}
122
}