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