[x265] [PATCH] BitCost: Remove the abs and new/delete operation in BitCost
Steve Borho
steve at borho.org
Fri Oct 9 17:53:24 CEST 2015
On 10/09, Ximing Cheng wrote:
> # HG changeset patch
> # User Ximing Cheng <ximingcheng at tencent.com>
> # Date 1444372125 -28800
> # Fri Oct 09 14:28:45 2015 +0800
> # Node ID 4176ffb657db00f173ba15a8d54c821f6811f514
> # Parent 55a4a9b920ff0385fd0b65c73c4c3f63b8a8cc65
> Remove new/delete in BitCost, double the size of s_bitsizes to remove the
> abs operation in bitcost
ah, now I see what you meant
> diff --git a/source/encoder/bitcost.cpp b/source/encoder/bitcost.cpp
> --- a/source/encoder/bitcost.cpp
> +++ b/source/encoder/bitcost.cpp
> @@ -41,7 +41,7 @@
> x265_emms(); // just to be safe
>
> CalculateLogs();
> - s_costs[qp] = new uint16_t[4 * BC_MAX_MV + 1] + 2 * BC_MAX_MV;
> + s_costs[qp] = X265_MALLOC(uint16_t, 4 * BC_MAX_MV + 1) + 2 *
> BC_MAX_MV;
> double lambda = x265_lambda_tab[qp];
>
> // estimate same cost for negative and positive MVD
> @@ -67,11 +67,11 @@
> {
> if (!s_bitsizes)
> {
> - s_bitsizes = new float[2 * BC_MAX_MV + 1];
> + s_bitsizes = X265_MALLOC(float, 4 * BC_MAX_MV + 1) + 2 * BC_MAX_MV;
in C++ new can never fail (it can throw an exception but it can't return
NULL) but malloc can, so this becomes a risk. We'll have to check for
malloc failures and abort the encode (and not try to free this pointer).
> s_bitsizes[0] = 0.718f;
> float log2_2 = 2.0f / log(2.0f); // 2 x 1/log(2)
> for (int i = 1; i <= 2 * BC_MAX_MV; i++)
> - s_bitsizes[i] = log((float)(i + 1)) * log2_2 + 1.718f;
> + s_bitsizes[i] = s_bitsizes[-i] = log((float)(i + 1)) * log2_2
> + 1.718f;
the patch was line mangled by the MTU
> }
> }
>
> @@ -81,12 +81,12 @@
> {
> if (s_costs[i])
> {
> - delete [] (s_costs[i] - 2 * BC_MAX_MV);
> + X265_FREE(s_costs[i] - 2 * BC_MAX_MV);
>
> s_costs[i] = 0;
> }
> }
>
> - delete [] s_bitsizes;
> + X265_FREE(s_bitsizes - 2 * BC_MAX_MV);
> s_bitsizes = 0;
> }
> diff --git a/source/encoder/bitcost.h b/source/encoder/bitcost.h
> --- a/source/encoder/bitcost.h
> +++ b/source/encoder/bitcost.h
> @@ -47,14 +47,14 @@
> // return bit cost of motion vector difference, without lambda
> inline uint32_t bitcost(const MV& mv) const
> {
> - return (uint32_t)(s_bitsizes[abs(mv.x - m_mvp.x)] +
> - s_bitsizes[abs(mv.y - m_mvp.y)] + 0.5f);
> + return (uint32_t)(s_bitsizes[mv.x - m_mvp.x] +
> + s_bitsizes[mv.y - m_mvp.y] + 0.5f);
> }
>
> static inline uint32_t bitcost(const MV& mv, const MV& mvp)
> {
> - return (uint32_t)(s_bitsizes[abs(mv.x - mvp.x)] +
> - s_bitsizes[abs(mv.y - mvp.y)] + 0.5f);
> + return (uint32_t)(s_bitsizes[mv.x - mvp.x] +
> + s_bitsizes[mv.y - mvp.y] + 0.5f);
yeah, this should be a reasonable speedup
> }
>
> static void destroy();
> _______________________________________________
> x265-devel mailing list
> x265-devel at videolan.org
> https://mailman.videolan.org/listinfo/x265-devel
--
Steve Borho
More information about the x265-devel
mailing list