Coverage Report

Created: 2026-05-16 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/x265/source/common/wavefront.cpp
Line
Count
Source
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
2.63k
{
35
2.63k
    m_numRows = numRows;
36
37
2.63k
    m_numWords = (numRows + 31) >> 5;
38
2.63k
    m_internalDependencyBitmap = X265_MALLOC(uint32_t, m_numWords);
39
2.63k
    if (m_internalDependencyBitmap)
40
2.63k
        memset((void*)m_internalDependencyBitmap, 0, sizeof(uint32_t) * m_numWords);
41
42
2.63k
    m_externalDependencyBitmap = X265_MALLOC(uint32_t, m_numWords);
43
2.63k
    if (m_externalDependencyBitmap)
44
2.63k
        memset((void*)m_externalDependencyBitmap, 0, sizeof(uint32_t) * m_numWords);
45
46
2.63k
    m_row_to_idx = X265_MALLOC(uint32_t, m_numRows);
47
2.63k
    m_idx_to_row = X265_MALLOC(uint32_t, m_numRows);
48
49
2.63k
    return m_internalDependencyBitmap && m_externalDependencyBitmap;
50
2.63k
}
51
52
WaveFront::~WaveFront()
53
2.63k
{
54
2.63k
    x265_free((void*)m_row_to_idx);
55
2.63k
    x265_free((void*)m_idx_to_row);
56
57
2.63k
    x265_free((void*)m_internalDependencyBitmap);
58
2.63k
    x265_free((void*)m_externalDependencyBitmap);
59
2.63k
}
60
61
void WaveFront::setLayerId(int layer)
62
605
{
63
605
    m_sLayerId = layer;
64
605
}
65
66
void WaveFront::clearEnabledRowMask()
67
605
{
68
605
    memset((void*)m_externalDependencyBitmap, 0, sizeof(uint32_t) * m_numWords);
69
605
    memset((void*)m_internalDependencyBitmap, 0, sizeof(uint32_t) * m_numWords);
70
605
}
71
72
void WaveFront::enqueueRow(int row)
73
5.82k
{
74
5.82k
    uint32_t bit = 1 << (row & 31);
75
5.82k
    ATOMIC_OR(&m_internalDependencyBitmap[row >> 5], bit);
76
5.82k
}
77
78
void WaveFront::enableRow(int row)
79
5.16k
{
80
5.16k
    uint32_t bit = 1 << (row & 31);
81
5.16k
    ATOMIC_OR(&m_externalDependencyBitmap[row >> 5], bit);
82
5.16k
}
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
14.8k
{
97
14.8k
    unsigned long id;
98
99
    /* Loop over each word until all available rows are finished */
100
25.0k
    for (int w = 0; w < m_numWords; w++)
101
16.0k
    {
102
16.0k
        uint32_t oldval = m_internalDependencyBitmap[w] & m_externalDependencyBitmap[w];
103
16.0k
        while (oldval)
104
5.83k
        {
105
5.83k
            BSF(id, oldval);
106
107
5.83k
            uint32_t bit = 1 << id;
108
5.83k
            if (ATOMIC_AND(&m_internalDependencyBitmap[w], ~bit) & bit)
109
5.82k
            {
110
                /* we cleared the bit, we get to process the row */
111
5.82k
                processRow(w * 32 + id, threadId, m_sLayerId);
112
5.82k
                m_helpWanted = true;
113
5.82k
                return; /* check for a higher priority task */
114
5.82k
            }
115
116
8
            oldval = m_internalDependencyBitmap[w] & m_externalDependencyBitmap[w];
117
8
        }
118
16.0k
    }
119
120
9.00k
    m_helpWanted = false;
121
9.00k
}
122
}