Commit | Line | Data |
---|---|---|
72b9787e JB |
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 "common.h" | |
25 | #include "primitives.h" | |
26 | #include "bitcost.h" | |
27 | ||
28 | using namespace x265; | |
29 | ||
30 | void BitCost::setQP(unsigned int qp) | |
31 | { | |
32 | if (!s_costs[qp]) | |
33 | { | |
34 | ScopedLock s(s_costCalcLock); | |
35 | ||
36 | // Now that we have acquired the lock, check again if another thread calculated | |
37 | // this row while we were blocked | |
38 | if (!s_costs[qp]) | |
39 | { | |
40 | x265_emms(); // just to be safe | |
41 | ||
42 | CalculateLogs(); | |
43 | s_costs[qp] = new uint16_t[4 * BC_MAX_MV + 1] + 2 * BC_MAX_MV; | |
44 | double lambda = x265_lambda_tab[qp]; | |
45 | ||
46 | // estimate same cost for negative and positive MVD | |
47 | for (int i = 0; i <= 2 * BC_MAX_MV; i++) | |
48 | s_costs[qp][i] = s_costs[qp][-i] = (uint16_t)X265_MIN(s_bitsizes[i] * lambda + 0.5f, (1 << 16) - 1); | |
49 | } | |
50 | } | |
51 | ||
52 | m_cost = s_costs[qp]; | |
53 | } | |
54 | ||
55 | /*** | |
56 | * Class static data and methods | |
57 | */ | |
58 | ||
59 | uint16_t *BitCost::s_costs[BC_MAX_QP]; | |
60 | ||
61 | float *BitCost::s_bitsizes; | |
62 | ||
63 | Lock BitCost::s_costCalcLock; | |
64 | ||
65 | void BitCost::CalculateLogs() | |
66 | { | |
67 | if (!s_bitsizes) | |
68 | { | |
69 | s_bitsizes = new float[2 * BC_MAX_MV + 1]; | |
70 | s_bitsizes[0] = 0.718f; | |
71 | float log2_2 = 2.0f / log(2.0f); // 2 x 1/log(2) | |
72 | for (int i = 1; i <= 2 * BC_MAX_MV; i++) | |
73 | s_bitsizes[i] = log((float)(i + 1)) * log2_2 + 1.718f; | |
74 | } | |
75 | } | |
76 | ||
77 | void BitCost::destroy() | |
78 | { | |
79 | for (int i = 0; i < BC_MAX_QP; i++) | |
80 | { | |
81 | if (s_costs[i]) | |
82 | { | |
83 | delete [] (s_costs[i] - 2 * BC_MAX_MV); | |
84 | ||
85 | s_costs[i] = 0; | |
86 | } | |
87 | } | |
88 | ||
89 | delete [] s_bitsizes; | |
90 | s_bitsizes = 0; | |
91 | } |