2010-09-20 05:32:05 +00:00
|
|
|
/**
|
|
|
|
|
* CUETools.FLACCL: FLAC audio encoder using OpenCL
|
|
|
|
|
* Copyright (c) 2009 Gregory S. Chudov
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef _FLACCL_KERNEL_H_
|
|
|
|
|
#define _FLACCL_KERNEL_H_
|
|
|
|
|
|
2010-10-10 23:28:38 +00:00
|
|
|
//#pragma OPENCL EXTENSION cl_amd_fp64 : enable
|
|
|
|
|
|
2010-09-20 05:32:05 +00:00
|
|
|
typedef enum
|
|
|
|
|
{
|
|
|
|
|
Constant = 0,
|
|
|
|
|
Verbatim = 1,
|
|
|
|
|
Fixed = 8,
|
|
|
|
|
LPC = 32
|
|
|
|
|
} SubframeType;
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
int residualOrder; // <= 32
|
|
|
|
|
int samplesOffs;
|
|
|
|
|
int shift;
|
|
|
|
|
int cbits;
|
|
|
|
|
int size;
|
|
|
|
|
int type;
|
|
|
|
|
int obits;
|
|
|
|
|
int blocksize;
|
|
|
|
|
int best_index;
|
|
|
|
|
int channel;
|
|
|
|
|
int residualOffs;
|
|
|
|
|
int wbits;
|
|
|
|
|
int abits;
|
|
|
|
|
int porder;
|
|
|
|
|
int reserved[2];
|
|
|
|
|
} FLACCLSubframeData;
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
FLACCLSubframeData data;
|
2010-09-25 19:53:48 +00:00
|
|
|
int coefs[32]; // fixme: should be short?
|
2010-09-20 05:32:05 +00:00
|
|
|
} FLACCLSubframeTask;
|
|
|
|
|
|
|
|
|
|
__kernel void cudaStereoDecorr(
|
|
|
|
|
__global int *samples,
|
|
|
|
|
__global short2 *src,
|
|
|
|
|
int offset
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
int pos = get_global_id(0);
|
|
|
|
|
if (pos < offset)
|
|
|
|
|
{
|
|
|
|
|
short2 s = src[pos];
|
|
|
|
|
samples[pos] = s.x;
|
|
|
|
|
samples[1 * offset + pos] = s.y;
|
|
|
|
|
samples[2 * offset + pos] = (s.x + s.y) >> 1;
|
|
|
|
|
samples[3 * offset + pos] = s.x - s.y;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__kernel void cudaChannelDecorr2(
|
|
|
|
|
__global int *samples,
|
|
|
|
|
__global short2 *src,
|
|
|
|
|
int offset
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
int pos = get_global_id(0);
|
|
|
|
|
if (pos < offset)
|
|
|
|
|
{
|
|
|
|
|
short2 s = src[pos];
|
|
|
|
|
samples[pos] = s.x;
|
|
|
|
|
samples[1 * offset + pos] = s.y;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//__kernel void cudaChannelDecorr(
|
|
|
|
|
// int *samples,
|
|
|
|
|
// short *src,
|
|
|
|
|
// int offset
|
|
|
|
|
//)
|
|
|
|
|
//{
|
|
|
|
|
// int pos = get_global_id(0);
|
|
|
|
|
// if (pos < offset)
|
|
|
|
|
// samples[get_group_id(1) * offset + pos] = src[pos * get_num_groups(1) + get_group_id(1)];
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
#define __ffs(a) (32 - clz(a & (-a)))
|
|
|
|
|
//#define __ffs(a) (33 - clz(~a & (a - 1)))
|
|
|
|
|
|
2010-09-25 19:53:48 +00:00
|
|
|
__kernel __attribute__((reqd_work_group_size(GROUP_SIZE, 1, 1)))
|
2010-09-20 05:32:05 +00:00
|
|
|
void cudaFindWastedBits(
|
|
|
|
|
__global FLACCLSubframeTask *tasks,
|
|
|
|
|
__global int *samples,
|
|
|
|
|
int tasksPerChannel
|
|
|
|
|
)
|
|
|
|
|
{
|
2010-09-25 19:53:48 +00:00
|
|
|
__local int abits[GROUP_SIZE];
|
|
|
|
|
__local int wbits[GROUP_SIZE];
|
2010-09-20 05:32:05 +00:00
|
|
|
__local FLACCLSubframeData task;
|
|
|
|
|
|
|
|
|
|
int tid = get_local_id(0);
|
|
|
|
|
if (tid < sizeof(task) / sizeof(int))
|
|
|
|
|
((__local int*)&task)[tid] = ((__global int*)(&tasks[get_group_id(0) * tasksPerChannel].data))[tid];
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
|
|
|
|
|
int w = 0, a = 0;
|
2010-10-10 23:28:38 +00:00
|
|
|
for (int pos = 0; pos < task.blocksize; pos += GROUP_SIZE)
|
2010-09-20 05:32:05 +00:00
|
|
|
{
|
|
|
|
|
int smp = pos + tid < task.blocksize ? samples[task.samplesOffs + pos + tid] : 0;
|
|
|
|
|
w |= smp;
|
|
|
|
|
a |= smp ^ (smp >> 31);
|
|
|
|
|
}
|
|
|
|
|
wbits[tid] = w;
|
|
|
|
|
abits[tid] = a;
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
|
2010-10-10 23:28:38 +00:00
|
|
|
for (int s = GROUP_SIZE / 2; s > 0; s >>= 1)
|
2010-09-20 05:32:05 +00:00
|
|
|
{
|
|
|
|
|
if (tid < s)
|
|
|
|
|
{
|
|
|
|
|
wbits[tid] |= wbits[tid + s];
|
|
|
|
|
abits[tid] |= abits[tid + s];
|
|
|
|
|
}
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-25 19:53:48 +00:00
|
|
|
w = max(0,__ffs(wbits[0]) - 1);
|
|
|
|
|
a = 32 - clz(abits[0]) - w;
|
2010-09-20 05:32:05 +00:00
|
|
|
if (tid < tasksPerChannel)
|
2010-09-25 19:53:48 +00:00
|
|
|
tasks[get_group_id(0) * tasksPerChannel + tid].data.wbits = w;
|
2010-09-20 05:32:05 +00:00
|
|
|
if (tid < tasksPerChannel)
|
2010-09-25 19:53:48 +00:00
|
|
|
tasks[get_group_id(0) * tasksPerChannel + tid].data.abits = a;
|
2010-09-20 05:32:05 +00:00
|
|
|
}
|
|
|
|
|
|
2010-09-25 19:53:48 +00:00
|
|
|
__kernel __attribute__((reqd_work_group_size(GROUP_SIZE, 1, 1)))
|
2010-09-20 05:32:05 +00:00
|
|
|
void cudaComputeAutocor(
|
|
|
|
|
__global float *output,
|
|
|
|
|
__global const int *samples,
|
|
|
|
|
__global const float *window,
|
|
|
|
|
__global FLACCLSubframeTask *tasks,
|
|
|
|
|
const int windowCount, // windows (log2: 0,1)
|
|
|
|
|
const int taskCount // tasks per block
|
|
|
|
|
)
|
|
|
|
|
{
|
2010-09-25 19:53:48 +00:00
|
|
|
__local float data[GROUP_SIZE * 2];
|
|
|
|
|
__local float product[GROUP_SIZE];
|
|
|
|
|
__local FLACCLSubframeData task;
|
|
|
|
|
const int tid = get_local_id(0);
|
2010-09-20 05:32:05 +00:00
|
|
|
// fetch task data
|
2010-09-25 19:53:48 +00:00
|
|
|
if (tid < sizeof(task) / sizeof(int))
|
|
|
|
|
((__local int*)&task)[tid] = ((__global int*)(tasks + taskCount * (get_group_id(1) >> windowCount)))[tid];
|
2010-09-20 05:32:05 +00:00
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
2010-09-25 19:53:48 +00:00
|
|
|
|
|
|
|
|
int bs = task.blocksize;
|
|
|
|
|
int windowOffs = (get_group_id(1) & ((1 << windowCount)-1)) * bs;
|
2010-09-20 05:32:05 +00:00
|
|
|
|
2010-09-25 19:53:48 +00:00
|
|
|
data[tid] = tid < bs ? samples[task.samplesOffs + tid] * window[windowOffs + tid] : 0.0f;
|
2010-09-20 05:32:05 +00:00
|
|
|
|
2010-09-25 19:53:48 +00:00
|
|
|
int tid0 = tid % (GROUP_SIZE >> 2);
|
|
|
|
|
int tid1 = tid / (GROUP_SIZE >> 2);
|
|
|
|
|
int lag0 = get_group_id(0) * 4;
|
|
|
|
|
__local float4 * dptr = ((__local float4 *)&data[0]) + tid0;
|
|
|
|
|
__local float4 * dptr1 = ((__local float4 *)&data[lag0 + tid1]) + tid0;
|
|
|
|
|
|
|
|
|
|
float prod = 0.0f;
|
|
|
|
|
for (int pos = 0; pos < bs; pos += GROUP_SIZE)
|
2010-09-20 05:32:05 +00:00
|
|
|
{
|
2010-09-25 19:53:48 +00:00
|
|
|
// fetch samples
|
|
|
|
|
float nextData = pos + tid + GROUP_SIZE < bs ? samples[task.samplesOffs + pos + tid + GROUP_SIZE] * window[windowOffs + pos + tid + GROUP_SIZE] : 0.0f;
|
|
|
|
|
data[tid + GROUP_SIZE] = nextData;
|
2010-09-20 05:32:05 +00:00
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
2010-09-25 19:53:48 +00:00
|
|
|
|
|
|
|
|
prod += dot(*dptr, *dptr1);
|
|
|
|
|
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
|
|
|
|
|
data[tid] = nextData;
|
|
|
|
|
}
|
|
|
|
|
product[tid] = prod;
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
for (int l = (GROUP_SIZE >> 3); l > 0; l >>= 1)
|
|
|
|
|
{
|
|
|
|
|
if (tid0 < l)
|
|
|
|
|
product[tid] = product[tid] + product[tid + l];
|
2010-09-20 05:32:05 +00:00
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
}
|
2010-09-25 19:53:48 +00:00
|
|
|
if (tid < 4 && tid + lag0 <= MAX_ORDER)
|
|
|
|
|
output[get_group_id(1) * (MAX_ORDER + 1) + tid + lag0] = product[tid * (GROUP_SIZE >> 2)];
|
2010-09-20 05:32:05 +00:00
|
|
|
}
|
|
|
|
|
|
2010-10-10 23:28:38 +00:00
|
|
|
//#define DEBUGPRINT
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUGPRINT
|
|
|
|
|
#pragma OPENCL EXTENSION cl_amd_printf : enable
|
|
|
|
|
#endif
|
|
|
|
|
|
2010-09-20 05:32:05 +00:00
|
|
|
__kernel __attribute__((reqd_work_group_size(32, 1, 1)))
|
|
|
|
|
void cudaComputeLPC(
|
|
|
|
|
__global FLACCLSubframeTask *tasks,
|
2010-09-25 19:53:48 +00:00
|
|
|
__global float *autoc,
|
2010-09-20 05:32:05 +00:00
|
|
|
__global float *lpcs,
|
2010-09-25 19:53:48 +00:00
|
|
|
int taskCount, // tasks per block
|
|
|
|
|
int windowCount
|
2010-09-20 05:32:05 +00:00
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
__local struct {
|
|
|
|
|
FLACCLSubframeData task;
|
|
|
|
|
volatile float ldr[32];
|
|
|
|
|
volatile float gen1[32];
|
|
|
|
|
volatile float error[32];
|
|
|
|
|
volatile float autoc[33];
|
|
|
|
|
volatile int lpcOffs;
|
|
|
|
|
volatile int autocOffs;
|
|
|
|
|
} shared;
|
|
|
|
|
const int tid = get_local_id(0);// + get_local_id(1) * 32;
|
|
|
|
|
|
|
|
|
|
// fetch task data
|
|
|
|
|
if (tid < sizeof(shared.task) / sizeof(int))
|
2010-09-25 19:53:48 +00:00
|
|
|
((__local int*)&shared.task)[tid] = ((__global int*)(tasks + get_group_id(1)))[tid];
|
2010-09-20 05:32:05 +00:00
|
|
|
if (tid == 0)
|
|
|
|
|
{
|
2010-09-25 19:53:48 +00:00
|
|
|
shared.lpcOffs = (get_group_id(0) + get_group_id(1) * windowCount) * (MAX_ORDER + 1) * 32;
|
|
|
|
|
shared.autocOffs = (get_group_id(0) + get_group_id(1) * get_num_groups(0)) * (MAX_ORDER + 1);
|
2010-09-20 05:32:05 +00:00
|
|
|
}
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
|
2010-09-25 19:53:48 +00:00
|
|
|
if (get_local_id(0) <= MAX_ORDER)
|
|
|
|
|
shared.autoc[get_local_id(0)] = autoc[shared.autocOffs + get_local_id(0)];
|
|
|
|
|
if (get_local_id(0) + get_local_size(0) <= MAX_ORDER)
|
|
|
|
|
shared.autoc[get_local_id(0) + get_local_size(0)] = autoc[shared.autocOffs + get_local_id(0) + get_local_size(0)];
|
2010-09-20 05:32:05 +00:00
|
|
|
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
|
|
|
|
|
// Compute LPC using Schur and Levinson-Durbin recursion
|
|
|
|
|
float gen0 = shared.gen1[get_local_id(0)] = shared.autoc[get_local_id(0)+1];
|
|
|
|
|
shared.ldr[get_local_id(0)] = 0.0f;
|
|
|
|
|
float error = shared.autoc[0];
|
2010-10-10 23:28:38 +00:00
|
|
|
|
|
|
|
|
#ifdef DEBUGPRINT
|
|
|
|
|
int magic = shared.autoc[0] == 177286873088.0f;
|
|
|
|
|
if (magic && get_local_id(0) <= MAX_ORDER)
|
|
|
|
|
printf("autoc[%d] == %f\n", get_local_id(0), shared.autoc[get_local_id(0)]);
|
|
|
|
|
#endif
|
|
|
|
|
|
2010-09-20 05:32:05 +00:00
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
2010-09-25 19:53:48 +00:00
|
|
|
for (int order = 0; order < MAX_ORDER; order++)
|
2010-09-20 05:32:05 +00:00
|
|
|
{
|
|
|
|
|
// Schur recursion
|
|
|
|
|
float reff = -shared.gen1[0] / error;
|
|
|
|
|
error += shared.gen1[0] * reff; // Equivalent to error *= (1 - reff * reff);
|
2010-10-10 23:28:38 +00:00
|
|
|
//error *= (1 - reff * reff);
|
2010-09-20 05:32:05 +00:00
|
|
|
float gen1;
|
2010-09-25 19:53:48 +00:00
|
|
|
if (get_local_id(0) < MAX_ORDER - 1 - order)
|
2010-09-20 05:32:05 +00:00
|
|
|
{
|
|
|
|
|
gen1 = shared.gen1[get_local_id(0) + 1] + reff * gen0;
|
|
|
|
|
gen0 += shared.gen1[get_local_id(0) + 1] * reff;
|
|
|
|
|
}
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
2010-09-25 19:53:48 +00:00
|
|
|
if (get_local_id(0) < MAX_ORDER - 1 - order)
|
2010-09-20 05:32:05 +00:00
|
|
|
shared.gen1[get_local_id(0)] = gen1;
|
2010-10-10 23:28:38 +00:00
|
|
|
#ifdef DEBUGPRINT
|
|
|
|
|
if (magic && get_local_id(0) == 0)
|
|
|
|
|
printf("order == %d, reff == %f, error = %f\n", order, reff, error);
|
|
|
|
|
if (magic && get_local_id(0) <= MAX_ORDER)
|
|
|
|
|
printf("gen[%d] == %f, %f\n", get_local_id(0), gen0, gen1);
|
|
|
|
|
#endif
|
2010-09-20 05:32:05 +00:00
|
|
|
|
|
|
|
|
// Store prediction error
|
|
|
|
|
if (get_local_id(0) == 0)
|
|
|
|
|
shared.error[order] = error;
|
|
|
|
|
|
|
|
|
|
// Levinson-Durbin recursion
|
|
|
|
|
float ldr =
|
|
|
|
|
select(0.0f, reff * shared.ldr[order - 1 - get_local_id(0)], get_local_id(0) < order) +
|
|
|
|
|
select(0.0f, reff, get_local_id(0) == order);
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
shared.ldr[get_local_id(0)] += ldr;
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
|
|
|
|
|
// Output coeffs
|
|
|
|
|
if (get_local_id(0) <= order)
|
|
|
|
|
lpcs[shared.lpcOffs + order * 32 + get_local_id(0)] = -shared.ldr[order - get_local_id(0)];
|
2010-10-10 23:28:38 +00:00
|
|
|
//if (get_local_id(0) <= order + 1 && fabs(-shared.ldr[0]) > 3000)
|
|
|
|
|
// printf("coef[%d] == %f, autoc == %f, error == %f\n", get_local_id(0), -shared.ldr[order - get_local_id(0)], shared.autoc[get_local_id(0)], shared.error[get_local_id(0)]);
|
2010-09-20 05:32:05 +00:00
|
|
|
}
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
// Output prediction error estimates
|
2010-09-25 19:53:48 +00:00
|
|
|
if (get_local_id(0) < MAX_ORDER)
|
|
|
|
|
lpcs[shared.lpcOffs + MAX_ORDER * 32 + get_local_id(0)] = shared.error[get_local_id(0)];
|
2010-09-20 05:32:05 +00:00
|
|
|
}
|
|
|
|
|
|
2010-10-06 11:16:41 +00:00
|
|
|
__kernel __attribute__((reqd_work_group_size(32, 1, 1)))
|
2010-09-20 05:32:05 +00:00
|
|
|
void cudaQuantizeLPC(
|
|
|
|
|
__global FLACCLSubframeTask *tasks,
|
2010-09-25 19:53:48 +00:00
|
|
|
__global float*lpcs,
|
2010-09-20 05:32:05 +00:00
|
|
|
int taskCount, // tasks per block
|
|
|
|
|
int taskCountLPC, // tasks per set of coeffs (<= 32)
|
|
|
|
|
int minprecision,
|
|
|
|
|
int precisions
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
__local struct {
|
|
|
|
|
FLACCLSubframeData task;
|
2010-10-06 11:16:41 +00:00
|
|
|
volatile int tmpi[32];
|
2010-09-20 05:32:05 +00:00
|
|
|
volatile int index[64];
|
|
|
|
|
volatile float error[64];
|
|
|
|
|
volatile int lpcOffs;
|
|
|
|
|
} shared;
|
|
|
|
|
|
2010-10-06 11:16:41 +00:00
|
|
|
const int tid = get_local_id(0);
|
2010-09-20 05:32:05 +00:00
|
|
|
|
|
|
|
|
// fetch task data
|
|
|
|
|
if (tid < sizeof(shared.task) / sizeof(int))
|
|
|
|
|
((__local int*)&shared.task)[tid] = ((__global int*)(tasks + get_group_id(1) * taskCount))[tid];
|
|
|
|
|
if (tid == 0)
|
2010-09-25 19:53:48 +00:00
|
|
|
shared.lpcOffs = (get_group_id(0) + get_group_id(1) * get_num_groups(0)) * (MAX_ORDER + 1) * 32;
|
2010-09-20 05:32:05 +00:00
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
|
2010-09-25 19:53:48 +00:00
|
|
|
// Select best orders based on Akaike's Criteria
|
2010-10-06 11:16:41 +00:00
|
|
|
shared.index[tid] = min(MAX_ORDER - 1, tid);
|
|
|
|
|
shared.error[tid] = shared.task.blocksize * 64 + tid;
|
2010-10-10 23:28:38 +00:00
|
|
|
shared.index[32 + tid] = MAX_ORDER - 1;
|
|
|
|
|
shared.error[32 + tid] = shared.task.blocksize * 64 + tid + 32;
|
2010-10-06 11:16:41 +00:00
|
|
|
|
|
|
|
|
// Load prediction error estimates
|
|
|
|
|
if (tid < MAX_ORDER)
|
2010-10-10 23:28:38 +00:00
|
|
|
shared.error[tid] = shared.task.blocksize * log(lpcs[shared.lpcOffs + MAX_ORDER * 32 + tid]) + tid * 4.12f * log(shared.task.blocksize);
|
2010-10-06 11:16:41 +00:00
|
|
|
//shared.error[get_local_id(0)] = shared.task.blocksize * log(lpcs[shared.lpcOffs + MAX_ORDER * 32 + get_local_id(0)]) + get_local_id(0) * 0.30f * (shared.task.abits + 1) * log(shared.task.blocksize);
|
2010-09-20 05:32:05 +00:00
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
|
|
|
|
|
// Sort using bitonic sort
|
|
|
|
|
for(int size = 2; size < 64; size <<= 1){
|
|
|
|
|
//Bitonic merge
|
2010-09-25 19:53:48 +00:00
|
|
|
int ddd = (tid & (size / 2)) == 0;
|
2010-09-20 05:32:05 +00:00
|
|
|
for(int stride = size / 2; stride > 0; stride >>= 1){
|
2010-09-25 19:53:48 +00:00
|
|
|
int pos = 2 * tid - (tid & (stride - 1));
|
2010-10-06 11:16:41 +00:00
|
|
|
float e0 = shared.error[pos];
|
|
|
|
|
float e1 = shared.error[pos + stride];
|
|
|
|
|
int i0 = shared.index[pos];
|
|
|
|
|
int i1 = shared.index[pos + stride];
|
2010-09-20 05:32:05 +00:00
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
2010-10-06 11:16:41 +00:00
|
|
|
if ((e0 >= e1) == ddd)
|
2010-09-20 05:32:05 +00:00
|
|
|
{
|
|
|
|
|
shared.error[pos] = e1;
|
|
|
|
|
shared.error[pos + stride] = e0;
|
|
|
|
|
shared.index[pos] = i1;
|
|
|
|
|
shared.index[pos + stride] = i0;
|
|
|
|
|
}
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//ddd == dir for the last bitonic merge step
|
|
|
|
|
{
|
|
|
|
|
for(int stride = 32; stride > 0; stride >>= 1){
|
|
|
|
|
//barrier(CLK_LOCAL_MEM_FENCE);
|
2010-09-25 19:53:48 +00:00
|
|
|
int pos = 2 * tid - (tid & (stride - 1));
|
2010-10-06 11:16:41 +00:00
|
|
|
float e0 = shared.error[pos];
|
|
|
|
|
float e1 = shared.error[pos + stride];
|
|
|
|
|
int i0 = shared.index[pos];
|
|
|
|
|
int i1 = shared.index[pos + stride];
|
2010-09-20 05:32:05 +00:00
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
2010-10-06 11:16:41 +00:00
|
|
|
if (e0 >= e1)
|
2010-09-20 05:32:05 +00:00
|
|
|
{
|
|
|
|
|
shared.error[pos] = e1;
|
|
|
|
|
shared.error[pos + stride] = e0;
|
|
|
|
|
shared.index[pos] = i1;
|
|
|
|
|
shared.index[pos + stride] = i0;
|
|
|
|
|
}
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-10 23:28:38 +00:00
|
|
|
//shared.index[tid] = MAX_ORDER - 1;
|
|
|
|
|
//barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
|
2010-09-20 05:32:05 +00:00
|
|
|
// Quantization
|
2010-10-06 11:16:41 +00:00
|
|
|
for (int i = 0; i < taskCountLPC; i ++)
|
2010-09-20 05:32:05 +00:00
|
|
|
{
|
|
|
|
|
int order = shared.index[i >> precisions];
|
2010-10-06 11:16:41 +00:00
|
|
|
float lpc = tid <= order ? lpcs[shared.lpcOffs + order * 32 + tid] : 0.0f;
|
2010-09-20 05:32:05 +00:00
|
|
|
// get 15 bits of each coeff
|
|
|
|
|
int coef = convert_int_rte(lpc * (1 << 15));
|
|
|
|
|
// remove sign bits
|
|
|
|
|
shared.tmpi[tid] = coef ^ (coef >> 31);
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
// OR reduction
|
|
|
|
|
for (int l = get_local_size(0) / 2; l > 1; l >>= 1)
|
|
|
|
|
{
|
2010-10-06 11:16:41 +00:00
|
|
|
if (tid < l)
|
2010-09-20 05:32:05 +00:00
|
|
|
shared.tmpi[tid] |= shared.tmpi[tid + l];
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
}
|
|
|
|
|
//SUM32(shared.tmpi,tid,|=);
|
|
|
|
|
// choose precision
|
|
|
|
|
//int cbits = max(3, min(10, 5 + (shared.task.abits >> 1))); // - convert_int_rte(shared.PE[order - 1])
|
|
|
|
|
int cbits = max(3, min(min(13 - minprecision + (i - ((i >> precisions) << precisions)) - (shared.task.blocksize <= 2304) - (shared.task.blocksize <= 1152) - (shared.task.blocksize <= 576), shared.task.abits), clz(order) + 1 - shared.task.abits));
|
|
|
|
|
// calculate shift based on precision and number of leading zeroes in coeffs
|
2010-10-06 11:16:41 +00:00
|
|
|
int shift = max(0,min(15, clz(shared.tmpi[0] | shared.tmpi[1]) - 18 + cbits));
|
2010-09-20 05:32:05 +00:00
|
|
|
|
|
|
|
|
//cbits = 13;
|
|
|
|
|
//shift = 15;
|
|
|
|
|
|
|
|
|
|
//if (shared.task.abits + 32 - clz(order) < shift
|
2010-10-06 11:16:41 +00:00
|
|
|
//int shift = max(0,min(15, (shared.task.abits >> 2) - 14 + clz(shared.tmpi[tid & ~31]) + ((32 - clz(order))>>1)));
|
2010-09-20 05:32:05 +00:00
|
|
|
// quantize coeffs with given shift
|
|
|
|
|
coef = convert_int_rte(clamp(lpc * (1 << shift), -1 << (cbits - 1), 1 << (cbits - 1)));
|
|
|
|
|
// error correction
|
2010-10-06 11:16:41 +00:00
|
|
|
//shared.tmp[tid] = (tid != 0) * (shared.arp[tid - 1]*(1 << shared.task.shift) - shared.task.coefs[tid - 1]);
|
|
|
|
|
//shared.task.coefs[tid] = max(-(1 << (shared.task.cbits - 1)), min((1 << (shared.task.cbits - 1))-1, convert_int_rte((shared.arp[tid]) * (1 << shared.task.shift) + shared.tmp[tid])));
|
2010-09-20 05:32:05 +00:00
|
|
|
// remove sign bits
|
|
|
|
|
shared.tmpi[tid] = coef ^ (coef >> 31);
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
// OR reduction
|
|
|
|
|
for (int l = get_local_size(0) / 2; l > 1; l >>= 1)
|
|
|
|
|
{
|
2010-10-06 11:16:41 +00:00
|
|
|
if (tid < l)
|
2010-09-20 05:32:05 +00:00
|
|
|
shared.tmpi[tid] |= shared.tmpi[tid + l];
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
}
|
|
|
|
|
//SUM32(shared.tmpi,tid,|=);
|
|
|
|
|
// calculate actual number of bits (+1 for sign)
|
2010-10-06 11:16:41 +00:00
|
|
|
cbits = 1 + 32 - clz(shared.tmpi[0] | shared.tmpi[1]);
|
2010-09-20 05:32:05 +00:00
|
|
|
|
|
|
|
|
// output shift, cbits and output coeffs
|
2010-10-10 23:28:38 +00:00
|
|
|
int taskNo = get_group_id(1) * taskCount + get_group_id(0) * taskCountLPC + i;
|
|
|
|
|
if (tid == 0)
|
|
|
|
|
tasks[taskNo].data.shift = shift;
|
|
|
|
|
if (tid == 0)
|
|
|
|
|
tasks[taskNo].data.cbits = cbits;
|
|
|
|
|
if (tid == 0)
|
|
|
|
|
tasks[taskNo].data.residualOrder = order + 1;
|
|
|
|
|
if (tid <= order)
|
|
|
|
|
tasks[taskNo].coefs[tid] = coef;
|
2010-09-20 05:32:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-10 23:28:38 +00:00
|
|
|
#define DONT_BEACCURATE
|
|
|
|
|
|
2010-09-25 19:53:48 +00:00
|
|
|
__kernel /*__attribute__(( vec_type_hint (int4)))*/ __attribute__((reqd_work_group_size(GROUP_SIZE, 1, 1)))
|
2010-09-20 05:32:05 +00:00
|
|
|
void cudaEstimateResidual(
|
|
|
|
|
__global int*output,
|
|
|
|
|
__global int*samples,
|
|
|
|
|
__global FLACCLSubframeTask *tasks
|
|
|
|
|
)
|
|
|
|
|
{
|
2010-10-10 23:28:38 +00:00
|
|
|
__local int data[GROUP_SIZE * 2];
|
2010-09-20 05:32:05 +00:00
|
|
|
__local FLACCLSubframeTask task;
|
2010-10-10 23:28:38 +00:00
|
|
|
#ifdef BEACCURATE
|
|
|
|
|
__local int residual[GROUP_SIZE];
|
|
|
|
|
__local int len[GROUP_SIZE / 16];
|
|
|
|
|
#else
|
|
|
|
|
__local float residual[GROUP_SIZE];
|
|
|
|
|
#endif
|
2010-09-20 05:32:05 +00:00
|
|
|
|
|
|
|
|
const int tid = get_local_id(0);
|
|
|
|
|
if (tid < sizeof(task)/sizeof(int))
|
2010-09-25 19:53:48 +00:00
|
|
|
((__local int*)&task)[tid] = ((__global int*)(&tasks[get_group_id(0)]))[tid];
|
2010-09-20 05:32:05 +00:00
|
|
|
barrier(CLK_GLOBAL_MEM_FENCE);
|
|
|
|
|
|
|
|
|
|
int ro = task.data.residualOrder;
|
|
|
|
|
int bs = task.data.blocksize;
|
|
|
|
|
|
2010-10-10 23:28:38 +00:00
|
|
|
if (tid < 32 && tid >= ro)
|
|
|
|
|
task.coefs[tid] = 0;
|
|
|
|
|
#ifdef BEACCURATE
|
|
|
|
|
if (tid < GROUP_SIZE / 16)
|
|
|
|
|
len[tid] = 0;
|
|
|
|
|
#else
|
|
|
|
|
float res = 0.0f;
|
|
|
|
|
#endif
|
|
|
|
|
data[tid] = tid < bs ? samples[task.data.samplesOffs + tid] >> task.data.wbits : 0;
|
2010-09-25 19:53:48 +00:00
|
|
|
for (int pos = 0; pos < bs; pos += GROUP_SIZE)
|
2010-09-20 05:32:05 +00:00
|
|
|
{
|
|
|
|
|
// fetch samples
|
2010-10-10 23:28:38 +00:00
|
|
|
int nextData = pos + tid + GROUP_SIZE < bs ? samples[task.data.samplesOffs + pos + tid + GROUP_SIZE] >> task.data.wbits : 0;
|
2010-09-25 19:53:48 +00:00
|
|
|
data[tid + GROUP_SIZE] = nextData;
|
2010-09-20 05:32:05 +00:00
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
|
|
|
|
|
// compute residual
|
2010-10-10 23:28:38 +00:00
|
|
|
__local int4 * dptr = (__local int4 *)&data[tid];
|
|
|
|
|
__local int4 * cptr = (__local int4 *)&task.coefs[0];
|
|
|
|
|
int4 sum = dptr[0] * cptr[0]
|
|
|
|
|
#if MAX_ORDER > 4
|
|
|
|
|
+ dptr[1] * cptr[1]
|
2010-09-20 05:32:05 +00:00
|
|
|
#if MAX_ORDER > 8
|
2010-10-10 23:28:38 +00:00
|
|
|
+ dptr[2] * cptr[2]
|
2010-09-20 05:32:05 +00:00
|
|
|
#if MAX_ORDER > 12
|
2010-10-10 23:28:38 +00:00
|
|
|
+ dptr[3] * cptr[3]
|
2010-09-20 05:32:05 +00:00
|
|
|
#if MAX_ORDER > 16
|
2010-10-10 23:28:38 +00:00
|
|
|
+ dptr[4] * cptr[4]
|
|
|
|
|
+ dptr[5] * cptr[5]
|
|
|
|
|
+ dptr[6] * cptr[6]
|
|
|
|
|
+ dptr[7] * cptr[7]
|
|
|
|
|
#endif
|
2010-09-20 05:32:05 +00:00
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
2010-10-10 23:28:38 +00:00
|
|
|
;
|
2010-09-20 05:32:05 +00:00
|
|
|
|
2010-10-10 23:28:38 +00:00
|
|
|
int t = select(0, data[tid + ro] - ((sum.x + sum.y + sum.z + sum.w) >> task.data.shift), pos + tid + ro < bs);
|
|
|
|
|
#ifdef BEACCURATE
|
|
|
|
|
residual[tid] = min((t << 1) ^ (t >> 31), 0x7fffff);
|
|
|
|
|
#else
|
|
|
|
|
res += fabs(t);
|
|
|
|
|
#endif
|
2010-09-20 05:32:05 +00:00
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
|
2010-10-10 23:28:38 +00:00
|
|
|
#ifdef BEACCURATE
|
|
|
|
|
if (tid < GROUP_SIZE / 16)
|
|
|
|
|
{
|
|
|
|
|
__local int4 * chunk = ((__local int4 *)residual) + tid * 4;
|
|
|
|
|
int4 sum = chunk[0] + chunk[1] + chunk[2] + chunk[3];
|
|
|
|
|
int res = sum.x + sum.y + sum.z + sum.w;
|
|
|
|
|
int k = clamp(clz(16) - clz(res), 0, 14);
|
|
|
|
|
len[tid] += 16 * k + (res >> k);
|
|
|
|
|
k = clamp(clz(16) - clz(res), 0, 14);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2010-09-20 05:32:05 +00:00
|
|
|
data[tid] = nextData;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-10 23:28:38 +00:00
|
|
|
#ifdef BEACCURATE
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
for (int l = GROUP_SIZE / 32; l > 0; l >>= 1)
|
|
|
|
|
{
|
|
|
|
|
if (tid < l)
|
|
|
|
|
len[tid] += len[tid + l];
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
}
|
|
|
|
|
if (tid == 0)
|
|
|
|
|
output[get_group_id(0)] = len[0] + (bs - ro);
|
|
|
|
|
#else
|
|
|
|
|
residual[tid] = res;
|
2010-09-20 05:32:05 +00:00
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
2010-09-25 19:53:48 +00:00
|
|
|
for (int l = GROUP_SIZE / 2; l > 0; l >>= 1)
|
2010-09-20 05:32:05 +00:00
|
|
|
{
|
|
|
|
|
if (tid < l)
|
|
|
|
|
residual[tid] += residual[tid + l];
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
}
|
|
|
|
|
if (tid == 0)
|
2010-10-10 23:28:38 +00:00
|
|
|
{
|
|
|
|
|
int residualLen = (bs - ro);
|
|
|
|
|
float sum = residual[0] * 2;// + residualLen / 2;
|
|
|
|
|
//int k = clamp(convert_int_rtn(log2((sum + 0.000001f) / (residualLen + 0.000001f))), 0, 14);
|
|
|
|
|
int k;
|
|
|
|
|
frexp((sum + 0.000001f) / residualLen, &k);
|
|
|
|
|
k = clamp(k - 1, 0, 14);
|
|
|
|
|
output[get_group_id(0)] = residualLen * (k + 1) + convert_int_rtn(min((float)0xffffff, sum / (1 << k)));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2010-09-20 05:32:05 +00:00
|
|
|
}
|
|
|
|
|
|
2010-10-06 11:16:41 +00:00
|
|
|
__kernel __attribute__((reqd_work_group_size(32, 1, 1)))
|
|
|
|
|
void cudaChooseBestMethod(
|
2010-09-20 05:32:05 +00:00
|
|
|
__global FLACCLSubframeTask *tasks,
|
|
|
|
|
__global int *residual,
|
|
|
|
|
int taskCount
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
__local struct {
|
2010-10-06 11:16:41 +00:00
|
|
|
volatile int index[32];
|
|
|
|
|
volatile int length[32];
|
2010-09-20 05:32:05 +00:00
|
|
|
} shared;
|
2010-10-06 11:16:41 +00:00
|
|
|
__local FLACCLSubframeData task;
|
|
|
|
|
const int tid = get_local_id(0);
|
2010-09-20 05:32:05 +00:00
|
|
|
|
|
|
|
|
shared.length[tid] = 0x7fffffff;
|
|
|
|
|
shared.index[tid] = tid;
|
2010-10-06 11:16:41 +00:00
|
|
|
for (int taskNo = 0; taskNo < taskCount; taskNo++)
|
|
|
|
|
{
|
|
|
|
|
// fetch task data
|
|
|
|
|
if (tid < sizeof(task) / sizeof(int))
|
|
|
|
|
((__local int*)&task)[tid] = ((__global int*)(&tasks[taskNo + taskCount * get_group_id(1)].data))[tid];
|
2010-09-20 05:32:05 +00:00
|
|
|
|
2010-10-06 11:16:41 +00:00
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
2010-09-20 05:32:05 +00:00
|
|
|
|
2010-10-06 11:16:41 +00:00
|
|
|
if (tid == 0)
|
|
|
|
|
{
|
|
|
|
|
// fetch part sum
|
|
|
|
|
int partLen = residual[taskNo + taskCount * get_group_id(1)];
|
|
|
|
|
//// calculate part size
|
|
|
|
|
//int residualLen = task[get_local_id(1)].data.blocksize - task[get_local_id(1)].data.residualOrder;
|
|
|
|
|
//residualLen = residualLen * (task[get_local_id(1)].data.type != Constant || psum != 0);
|
|
|
|
|
//// calculate rice parameter
|
|
|
|
|
//int k = max(0, min(14, convert_int_rtz(log2((psum + 0.000001f) / (residualLen + 0.000001f) + 0.5f))));
|
|
|
|
|
//// calculate part bit length
|
|
|
|
|
//int partLen = residualLen * (k + 1) + (psum >> k);
|
|
|
|
|
|
|
|
|
|
int obits = task.obits - task.wbits;
|
|
|
|
|
shared.length[taskNo] =
|
|
|
|
|
min(obits * task.blocksize,
|
|
|
|
|
task.type == Fixed ? task.residualOrder * obits + 6 + (4 * 1/2) + partLen :
|
|
|
|
|
task.type == LPC ? task.residualOrder * obits + 4 + 5 + task.residualOrder * task.cbits + 6 + (4 * 1/2)/* << porder */ + partLen :
|
|
|
|
|
task.type == Constant ? obits * (1 + task.blocksize * (partLen != 0)) :
|
|
|
|
|
obits * task.blocksize);
|
2010-09-20 05:32:05 +00:00
|
|
|
}
|
2010-10-06 11:16:41 +00:00
|
|
|
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
}
|
2010-09-20 05:32:05 +00:00
|
|
|
//shared.index[get_local_id(0)] = get_local_id(0);
|
|
|
|
|
//shared.length[get_local_id(0)] = (get_local_id(0) < taskCount) ? tasks[get_local_id(0) + taskCount * get_group_id(1)].size : 0x7fffffff;
|
|
|
|
|
|
|
|
|
|
if (tid < taskCount)
|
|
|
|
|
tasks[tid + taskCount * get_group_id(1)].data.size = shared.length[tid];
|
|
|
|
|
|
|
|
|
|
int l1 = shared.length[tid];
|
2010-10-06 11:16:41 +00:00
|
|
|
for (int sh = 4; sh > 0; sh --)
|
2010-09-20 05:32:05 +00:00
|
|
|
{
|
2010-10-06 11:16:41 +00:00
|
|
|
if (tid < (1 << sh))
|
2010-09-20 05:32:05 +00:00
|
|
|
{
|
|
|
|
|
int l2 = shared.length[tid + (1 << sh)];
|
|
|
|
|
shared.index[tid] = shared.index[tid + ((l2 < l1) << sh)];
|
|
|
|
|
shared.length[tid] = l1 = min(l1, l2);
|
|
|
|
|
}
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
}
|
|
|
|
|
if (tid == 0)
|
|
|
|
|
tasks[taskCount * get_group_id(1)].data.best_index = taskCount * get_group_id(1) + shared.index[shared.length[1] < shared.length[0]];
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-06 11:16:41 +00:00
|
|
|
__kernel __attribute__((reqd_work_group_size(64, 1, 1)))
|
|
|
|
|
void cudaCopyBestMethod(
|
2010-09-20 05:32:05 +00:00
|
|
|
__global FLACCLSubframeTask *tasks_out,
|
|
|
|
|
__global FLACCLSubframeTask *tasks,
|
|
|
|
|
int count
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
__local int best_index;
|
|
|
|
|
if (get_local_id(0) == 0)
|
|
|
|
|
best_index = tasks[count * get_group_id(1)].data.best_index;
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
if (get_local_id(0) < sizeof(FLACCLSubframeTask)/sizeof(int))
|
|
|
|
|
((__global int*)(tasks_out + get_group_id(1)))[get_local_id(0)] = ((__global int*)(tasks + best_index))[get_local_id(0)];
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-06 11:16:41 +00:00
|
|
|
__kernel __attribute__((reqd_work_group_size(64, 1, 1)))
|
|
|
|
|
void cudaCopyBestMethodStereo(
|
2010-09-20 05:32:05 +00:00
|
|
|
__global FLACCLSubframeTask *tasks_out,
|
|
|
|
|
__global FLACCLSubframeTask *tasks,
|
|
|
|
|
int count
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
__local struct {
|
|
|
|
|
int best_index[4];
|
|
|
|
|
int best_size[4];
|
|
|
|
|
int lr_index[2];
|
|
|
|
|
} shared;
|
|
|
|
|
if (get_local_id(0) < 4)
|
|
|
|
|
shared.best_index[get_local_id(0)] = tasks[count * (get_group_id(1) * 4 + get_local_id(0))].data.best_index;
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
if (get_local_id(0) < 4)
|
|
|
|
|
shared.best_size[get_local_id(0)] = tasks[shared.best_index[get_local_id(0)]].data.size;
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
if (get_local_id(0) == 0)
|
|
|
|
|
{
|
|
|
|
|
int bitsBest = shared.best_size[2] + shared.best_size[3]; // MidSide
|
|
|
|
|
shared.lr_index[0] = shared.best_index[2];
|
|
|
|
|
shared.lr_index[1] = shared.best_index[3];
|
|
|
|
|
if (bitsBest > shared.best_size[3] + shared.best_size[1]) // RightSide
|
|
|
|
|
{
|
|
|
|
|
bitsBest = shared.best_size[3] + shared.best_size[1];
|
|
|
|
|
shared.lr_index[0] = shared.best_index[3];
|
|
|
|
|
shared.lr_index[1] = shared.best_index[1];
|
|
|
|
|
}
|
|
|
|
|
if (bitsBest > shared.best_size[0] + shared.best_size[3]) // LeftSide
|
|
|
|
|
{
|
|
|
|
|
bitsBest = shared.best_size[0] + shared.best_size[3];
|
|
|
|
|
shared.lr_index[0] = shared.best_index[0];
|
|
|
|
|
shared.lr_index[1] = shared.best_index[3];
|
|
|
|
|
}
|
|
|
|
|
if (bitsBest > shared.best_size[0] + shared.best_size[1]) // LeftRight
|
|
|
|
|
{
|
|
|
|
|
bitsBest = shared.best_size[0] + shared.best_size[1];
|
|
|
|
|
shared.lr_index[0] = shared.best_index[0];
|
|
|
|
|
shared.lr_index[1] = shared.best_index[1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
if (get_local_id(0) < sizeof(FLACCLSubframeTask)/sizeof(int))
|
|
|
|
|
((__global int*)(tasks_out + 2 * get_group_id(1)))[get_local_id(0)] = ((__global int*)(tasks + shared.lr_index[0]))[get_local_id(0)];
|
|
|
|
|
if (get_local_id(0) == 0)
|
|
|
|
|
tasks_out[2 * get_group_id(1)].data.residualOffs = tasks[shared.best_index[0]].data.residualOffs;
|
|
|
|
|
if (get_local_id(0) < sizeof(FLACCLSubframeTask)/sizeof(int))
|
|
|
|
|
((__global int*)(tasks_out + 2 * get_group_id(1) + 1))[get_local_id(0)] = ((__global int*)(tasks + shared.lr_index[1]))[get_local_id(0)];
|
|
|
|
|
if (get_local_id(0) == 0)
|
|
|
|
|
tasks_out[2 * get_group_id(1) + 1].data.residualOffs = tasks[shared.best_index[1]].data.residualOffs;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-10 23:28:38 +00:00
|
|
|
// get_group_id(0) == task index
|
2010-10-06 11:16:41 +00:00
|
|
|
__kernel __attribute__((reqd_work_group_size(GROUP_SIZE, 1, 1)))
|
|
|
|
|
void cudaEncodeResidual(
|
|
|
|
|
__global int *output,
|
|
|
|
|
__global int *samples,
|
|
|
|
|
__global FLACCLSubframeTask *tasks
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
__local FLACCLSubframeTask task;
|
|
|
|
|
__local int data[GROUP_SIZE * 2];
|
|
|
|
|
const int tid = get_local_id(0);
|
|
|
|
|
if (get_local_id(0) < sizeof(task) / sizeof(int))
|
2010-10-10 23:28:38 +00:00
|
|
|
((__local int*)&task)[get_local_id(0)] = ((__global int*)(&tasks[get_group_id(0)]))[get_local_id(0)];
|
2010-10-06 11:16:41 +00:00
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
|
|
|
|
|
int bs = task.data.blocksize;
|
|
|
|
|
int ro = task.data.residualOrder;
|
|
|
|
|
|
|
|
|
|
data[tid] = tid < bs ? samples[task.data.samplesOffs + tid] >> task.data.wbits : 0;
|
|
|
|
|
for (int pos = 0; pos < bs; pos += GROUP_SIZE)
|
|
|
|
|
{
|
|
|
|
|
// fetch samples
|
|
|
|
|
float nextData = pos + tid + GROUP_SIZE < bs ? samples[task.data.samplesOffs + pos + tid + GROUP_SIZE] >> task.data.wbits : 0;
|
|
|
|
|
data[tid + GROUP_SIZE] = nextData;
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
|
|
|
|
|
// compute residual
|
|
|
|
|
int sum = 0;
|
|
|
|
|
for (int c = 0; c < ro; c++)
|
|
|
|
|
sum += data[tid + c] * task.coefs[c];
|
|
|
|
|
sum = data[tid + ro] - (sum >> task.data.shift);
|
|
|
|
|
if (pos + tid + ro < bs)
|
|
|
|
|
output[task.data.residualOffs + pos + tid + ro] = sum;
|
|
|
|
|
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
data[tid] = nextData;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-10 23:28:38 +00:00
|
|
|
// get_group_id(0) == partition index
|
|
|
|
|
// get_group_id(1) == task index
|
2010-10-06 11:16:41 +00:00
|
|
|
__kernel __attribute__((reqd_work_group_size(GROUP_SIZE, 1, 1)))
|
|
|
|
|
void cudaCalcPartition(
|
|
|
|
|
__global int *partition_lengths,
|
|
|
|
|
__global int *residual,
|
|
|
|
|
__global FLACCLSubframeTask *tasks,
|
|
|
|
|
int max_porder, // <= 8
|
|
|
|
|
int psize // == task.blocksize >> max_porder?
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
__local int data[GROUP_SIZE];
|
|
|
|
|
__local int length[GROUP_SIZE / 16][16];
|
|
|
|
|
__local FLACCLSubframeData task;
|
|
|
|
|
|
|
|
|
|
const int tid = get_local_id(0);
|
|
|
|
|
if (tid < sizeof(task) / sizeof(int))
|
|
|
|
|
((__local int*)&task)[tid] = ((__global int*)(&tasks[get_group_id(1)]))[tid];
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
|
2010-10-10 23:28:38 +00:00
|
|
|
int k = tid % 16;
|
|
|
|
|
int x = tid / 16;
|
2010-10-06 11:16:41 +00:00
|
|
|
|
|
|
|
|
int sum = 0;
|
|
|
|
|
for (int pos0 = 0; pos0 < psize; pos0 += GROUP_SIZE)
|
|
|
|
|
{
|
|
|
|
|
int offs = get_group_id(0) * psize + pos0 + tid;
|
|
|
|
|
// fetch residual
|
|
|
|
|
int s = (offs >= task.residualOrder && pos0 + tid < psize) ? residual[task.residualOffs + offs] : 0;
|
|
|
|
|
// convert to unsigned
|
2010-10-10 23:28:38 +00:00
|
|
|
data[tid] = min(0x7fffff, (s << 1) ^ (s >> 31));
|
2010-10-06 11:16:41 +00:00
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
|
|
|
|
|
// calc number of unary bits for each residual sample with each rice paramater
|
|
|
|
|
for (int pos = 0; pos < psize && pos < GROUP_SIZE; pos += GROUP_SIZE / 16)
|
|
|
|
|
sum += data[pos + x] >> k;
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-10 23:28:38 +00:00
|
|
|
length[x][k] = min(0x7fffff, sum);
|
2010-10-06 11:16:41 +00:00
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
|
|
|
|
|
if (x == 0)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 1; i < GROUP_SIZE / 16; i++)
|
|
|
|
|
length[0][k] += length[i][k];
|
|
|
|
|
// output length
|
|
|
|
|
const int pos = (15 << (max_porder + 1)) * get_group_id(1) + (k << (max_porder + 1));
|
|
|
|
|
if (k <= 14)
|
2010-10-10 23:28:38 +00:00
|
|
|
partition_lengths[pos + get_group_id(0)] = min(0x7fffff,length[0][k]) + (psize - task.residualOrder * (get_group_id(0) == 0)) * (k + 1);
|
2010-10-06 11:16:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-10 23:28:38 +00:00
|
|
|
// Sums partition lengths for a certain k == get_group_id(0)
|
|
|
|
|
// Requires 128 threads
|
|
|
|
|
// get_group_id(0) == k
|
|
|
|
|
// get_group_id(1) == task index
|
|
|
|
|
__kernel __attribute__((reqd_work_group_size(128, 1, 1)))
|
|
|
|
|
void cudaSumPartition(
|
|
|
|
|
__global int* partition_lengths,
|
|
|
|
|
int max_porder
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
__local int data[512]; // max_porder <= 8, data length <= 1 << 9.
|
|
|
|
|
const int pos = (15 << (max_porder + 1)) * get_group_id(1) + (get_group_id(0) << (max_porder + 1));
|
|
|
|
|
|
|
|
|
|
// fetch partition lengths
|
|
|
|
|
data[get_local_id(0)] = get_local_id(0) < (1 << max_porder) ? partition_lengths[pos + get_local_id(0)] : 0;
|
|
|
|
|
data[get_local_size(0) + get_local_id(0)] = get_local_size(0) + get_local_id(0) < (1 << max_porder) ? partition_lengths[pos + get_local_size(0) + get_local_id(0)] : 0;
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
|
|
|
|
|
int in_pos = (get_local_id(0) << 1);
|
|
|
|
|
int out_pos = (1 << max_porder) + get_local_id(0);
|
|
|
|
|
for (int bs = 1 << (max_porder - 1); bs > 0; bs >>= 1)
|
|
|
|
|
{
|
|
|
|
|
if (get_local_id(0) < bs) data[out_pos] = data[in_pos] + data[in_pos + 1];
|
|
|
|
|
in_pos += bs << 1;
|
|
|
|
|
out_pos += bs;
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
}
|
|
|
|
|
if (get_local_id(0) < (1 << max_porder))
|
|
|
|
|
partition_lengths[pos + (1 << max_porder) + get_local_id(0)] = data[(1 << max_porder) + get_local_id(0)];
|
|
|
|
|
if (get_local_size(0) + get_local_id(0) < (1 << max_porder))
|
|
|
|
|
partition_lengths[pos + (1 << max_porder) + get_local_size(0) + get_local_id(0)] = data[(1 << max_porder) + get_local_size(0) + get_local_id(0)];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Finds optimal rice parameter for several partitions at a time.
|
|
|
|
|
// get_group_id(0) == chunk index (chunk size is GROUP_SIZE / 8, so total task size is 8 * (2 << max_porder))
|
|
|
|
|
// get_group_id(1) == task index
|
|
|
|
|
__kernel __attribute__((reqd_work_group_size(GROUP_SIZE, 1, 1)))
|
|
|
|
|
void cudaFindRiceParameter(
|
|
|
|
|
__global int* rice_parameters,
|
|
|
|
|
__global int* partition_lengths,
|
|
|
|
|
int max_porder
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
__local struct {
|
|
|
|
|
volatile int length[GROUP_SIZE];
|
|
|
|
|
volatile int index[GROUP_SIZE];
|
|
|
|
|
} shared;
|
|
|
|
|
const int tid = get_local_id(0);
|
|
|
|
|
const int ws = GROUP_SIZE / 8;
|
|
|
|
|
const int parts = min(ws, 2 << max_porder);
|
|
|
|
|
const int p = tid % ws;
|
|
|
|
|
const int k = tid / ws; // 0..7
|
|
|
|
|
const int pos = (15 << (max_porder + 1)) * get_group_id(1) + (k << (max_porder + 1));
|
|
|
|
|
|
|
|
|
|
// read length for 32 partitions
|
|
|
|
|
int l1 = (p < parts) ? partition_lengths[pos + get_group_id(0) * ws + p] : 0xffffff;
|
|
|
|
|
int l2 = (k + 8 <= 14 && p < parts) ? partition_lengths[pos + (8 << (max_porder + 1)) + get_group_id(0) * ws + p] : 0xffffff;
|
|
|
|
|
// find best rice parameter
|
|
|
|
|
shared.index[tid] = k + ((l2 < l1) << 3);
|
|
|
|
|
shared.length[tid] = l1 = min(l1, l2);
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
2010-09-20 05:32:05 +00:00
|
|
|
//#pragma unroll 3
|
2010-10-10 23:28:38 +00:00
|
|
|
for (int lsh = GROUP_SIZE / 2; lsh >= ws; lsh >>= 1)
|
|
|
|
|
{
|
|
|
|
|
if (tid < lsh)
|
|
|
|
|
{
|
|
|
|
|
l2 = shared.length[tid + lsh];
|
|
|
|
|
shared.index[tid] = shared.index[tid + (l2 < l1) * lsh];
|
|
|
|
|
shared.length[tid] = l1 = min(l1, l2);
|
|
|
|
|
}
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
}
|
|
|
|
|
if (tid < parts)
|
|
|
|
|
{
|
|
|
|
|
// output rice parameter
|
|
|
|
|
rice_parameters[(get_group_id(1) << (max_porder + 2)) + get_group_id(0) * parts + tid] = shared.index[tid];
|
|
|
|
|
// output length
|
|
|
|
|
rice_parameters[(get_group_id(1) << (max_porder + 2)) + (1 << (max_porder + 1)) + get_group_id(0) * parts + tid] = shared.length[tid];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get_group_id(0) == task index
|
|
|
|
|
__kernel __attribute__((reqd_work_group_size(GROUP_SIZE, 1, 1)))
|
|
|
|
|
void cudaFindPartitionOrder(
|
|
|
|
|
__global int* best_rice_parameters,
|
|
|
|
|
__global FLACCLSubframeTask *tasks,
|
|
|
|
|
__global int* rice_parameters,
|
|
|
|
|
int max_porder
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
__local struct {
|
|
|
|
|
int length[32];
|
|
|
|
|
int index[32];
|
|
|
|
|
} shared;
|
|
|
|
|
__local int partlen[GROUP_SIZE];
|
|
|
|
|
__local FLACCLSubframeData task;
|
|
|
|
|
|
|
|
|
|
const int pos = (get_group_id(0) << (max_porder + 2)) + (2 << max_porder);
|
|
|
|
|
if (get_local_id(0) < sizeof(task) / sizeof(int))
|
|
|
|
|
((__local int*)&task)[get_local_id(0)] = ((__global int*)(&tasks[get_group_id(0)]))[get_local_id(0)];
|
|
|
|
|
// fetch partition lengths
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
|
|
|
|
|
for (int porder = max_porder; porder >= 0; porder--)
|
|
|
|
|
{
|
|
|
|
|
int len = 0;
|
|
|
|
|
for (int offs = 0; offs < (1 << porder); offs += GROUP_SIZE)
|
|
|
|
|
len += offs + get_local_id(0) < (1 << porder) ? rice_parameters[pos + (2 << max_porder) - (2 << porder) + offs + get_local_id(0)] : 0;
|
|
|
|
|
partlen[get_local_id(0)] = len;
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
for (int l = min(GROUP_SIZE, 1 << porder) / 2; l > 0; l >>= 1)
|
|
|
|
|
{
|
|
|
|
|
if (get_local_id(0) < l)
|
|
|
|
|
partlen[get_local_id(0)] += partlen[get_local_id(0) + l];
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
}
|
|
|
|
|
if (get_local_id(0) == 0)
|
|
|
|
|
shared.length[porder] = partlen[0] + (4 << porder);
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (get_local_id(0) < 32 && get_local_id(0) > max_porder)
|
|
|
|
|
shared.length[get_local_id(0)] = 0xfffffff;
|
|
|
|
|
if (get_local_id(0) < 32)
|
|
|
|
|
shared.index[get_local_id(0)] = get_local_id(0);
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
int l1 = get_local_id(0) <= max_porder ? shared.length[get_local_id(0)] : 0xfffffff;
|
|
|
|
|
for (int sh = 3; sh >= 0; sh --)
|
|
|
|
|
{
|
|
|
|
|
if (get_local_id(0) < (1 << sh))
|
|
|
|
|
{
|
|
|
|
|
int l2 = shared.length[get_local_id(0) + (1 << sh)];
|
|
|
|
|
shared.index[get_local_id(0)] = shared.index[get_local_id(0) + ((l2 < l1) << sh)];
|
|
|
|
|
shared.length[get_local_id(0)] = l1 = min(l1, l2);
|
|
|
|
|
}
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
}
|
|
|
|
|
if (get_local_id(0) == 0)
|
|
|
|
|
tasks[get_group_id(0)].data.porder = shared.index[0];
|
|
|
|
|
if (get_local_id(0) == 0)
|
|
|
|
|
{
|
|
|
|
|
int obits = task.obits - task.wbits;
|
|
|
|
|
tasks[get_group_id(0)].data.size =
|
|
|
|
|
task.type == Fixed ? task.residualOrder * obits + 6 + l1 :
|
|
|
|
|
task.type == LPC ? task.residualOrder * obits + 6 + l1 + 4 + 5 + task.residualOrder * task.cbits :
|
|
|
|
|
task.type == Constant ? obits : obits * task.blocksize;
|
|
|
|
|
}
|
|
|
|
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
int porder = shared.index[0];
|
|
|
|
|
for (int offs = 0; offs < (1 << porder); offs += GROUP_SIZE)
|
|
|
|
|
if (offs + get_local_id(0) < (1 << porder))
|
|
|
|
|
best_rice_parameters[(get_group_id(0) << max_porder) + offs + get_local_id(0)] = rice_parameters[pos - (2 << porder) + offs + get_local_id(0)];
|
|
|
|
|
// FIXME: should be bytes?
|
|
|
|
|
// if (get_local_id(0) < (1 << porder))
|
|
|
|
|
//shared.tmp[get_local_id(0)] = rice_parameters[pos - (2 << porder) + get_local_id(0)];
|
|
|
|
|
// barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
// if (get_local_id(0) < max(1, (1 << porder) >> 2))
|
|
|
|
|
// {
|
|
|
|
|
//char4 ch;
|
|
|
|
|
//ch.x = shared.tmp[(get_local_id(0) << 2)];
|
|
|
|
|
//ch.y = shared.tmp[(get_local_id(0) << 2) + 1];
|
|
|
|
|
//ch.z = shared.tmp[(get_local_id(0) << 2) + 2];
|
|
|
|
|
//ch.w = shared.tmp[(get_local_id(0) << 2) + 3];
|
|
|
|
|
//shared.ch[get_local_id(0)] = ch
|
|
|
|
|
// }
|
|
|
|
|
// barrier(CLK_LOCAL_MEM_FENCE);
|
|
|
|
|
// if (get_local_id(0) < max(1, (1 << porder) >> 2))
|
|
|
|
|
//best_rice_parameters[(get_group_id(1) << max_porder) + get_local_id(0)] = shared.ch[get_local_id(0)];
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-20 05:32:05 +00:00
|
|
|
//#endif
|
|
|
|
|
//
|
|
|
|
|
//#if 0
|
|
|
|
|
// if (get_local_id(0) < order)
|
|
|
|
|
// {
|
|
|
|
|
// for (int i = 0; i < order; i++)
|
|
|
|
|
// if (get_local_id(0) >= i)
|
|
|
|
|
// sum[get_local_id(0) - i] += coefs[get_local_id(0)] * sample[order - i - 1];
|
|
|
|
|
// fot (int i = order; i < blocksize; i++)
|
|
|
|
|
// {
|
|
|
|
|
// if (!get_local_id(0)) sample[order + i] = s = residual[order + i] + (sum[order + i] >> shift);
|
|
|
|
|
// sum[get_local_id(0) + i + 1] += coefs[get_local_id(0)] * s;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//#endif
|
|
|
|
|
#endif
|