1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
/*
* FAAC - Freeware Advanced Audio Coder
* Copyright (C) 2002 Krzysztof Nikiel
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: psychkni.c,v 1.19 2012/03/01 18:34:17 knik Exp $
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "blockswitch.h"
#include "coder.h"
#include "fft.h"
#include "util.h"
#include "filtbank.h"
#include <faac.h>
typedef float psyfloat;
typedef struct
{
/* bandwidth */
int bandS;
int lastband;
/* band volumes */
psyfloat *engPrev[8];
psyfloat *eng[8];
psyfloat *engNext[8];
psyfloat *engNext2[8];
}
psydata_t;
static void Hann(GlobalPsyInfo * gpsyInfo, faac_real *inSamples, int size)
{
int i;
/* Applying Hann window */
if (size == BLOCK_LEN_LONG * 2)
{
for (i = 0; i < size; i++)
inSamples[i] *= gpsyInfo->hannWindow[i];
}
else
{
for (i = 0; i < size; i++)
inSamples[i] *= gpsyInfo->hannWindowS[i];
}
}
#define PRINTSTAT 0
#if PRINTSTAT
static struct {
int tot;
int s;
} frames;
#endif
static void PsyCheckShort(PsyInfo * psyInfo, faac_real quality)
{
enum {PREVS = 2, NEXTS = 2};
psydata_t *psydata = psyInfo->data;
int lastband = psydata->lastband;
int firstband = 2;
int sfb, win;
psyfloat *lasteng;
psyInfo->block_type = ONLY_LONG_WINDOW;
lasteng = NULL;
for (win = 0; win < PREVS + 8 + NEXTS; win++)
{
psyfloat *eng;
if (win < PREVS)
eng = psydata->engPrev[win + 8 - PREVS];
else if (win < (PREVS + 8))
eng = psydata->eng[win - PREVS];
else
eng = psydata->engNext[win - PREVS - 8];
if (lasteng)
{
faac_real toteng = 0.0;
faac_real volchg = 0.0;
for (sfb = firstband; sfb < lastband; sfb++)
{
toteng += (eng[sfb] < lasteng[sfb]) ? eng[sfb] : lasteng[sfb];
volchg += FAAC_FABS(eng[sfb] - lasteng[sfb]);
}
if ((volchg / toteng * quality) > 3.0)
{
psyInfo->block_type = ONLY_SHORT_WINDOW;
break;
}
}
lasteng = eng;
}
#if PRINTSTAT
frames.tot++;
if (psyInfo->block_type == ONLY_SHORT_WINDOW)
frames.s++;
#endif
}
static void PsyInit(GlobalPsyInfo * gpsyInfo, PsyInfo * psyInfo, unsigned int numChannels,
unsigned int sampleRate, int *cb_width_long, int num_cb_long,
int *cb_width_short, int num_cb_short)
{
unsigned int channel;
int i, j, size;
gpsyInfo->hannWindow =
(faac_real *) AllocMemory(2 * BLOCK_LEN_LONG * sizeof(faac_real));
gpsyInfo->hannWindowS =
(faac_real *) AllocMemory(2 * BLOCK_LEN_SHORT * sizeof(faac_real));
for (i = 0; i < BLOCK_LEN_LONG * 2; i++)
gpsyInfo->hannWindow[i] = 0.5 * (1 - FAAC_COS(2.0 * M_PI * (i + 0.5) /
(BLOCK_LEN_LONG * 2)));
for (i = 0; i < BLOCK_LEN_SHORT * 2; i++)
gpsyInfo->hannWindowS[i] = 0.5 * (1 - FAAC_COS(2.0 * M_PI * (i + 0.5) /
(BLOCK_LEN_SHORT * 2)));
gpsyInfo->sampleRate = (faac_real) sampleRate;
for (channel = 0; channel < numChannels; channel++)
{
psydata_t *psydata = AllocMemory(sizeof(psydata_t));
psyInfo[channel].data = psydata;
}
size = BLOCK_LEN_LONG;
for (channel = 0; channel < numChannels; channel++)
{
psyInfo[channel].size = size;
psyInfo[channel].prevSamples =
(faac_real *) AllocMemory(size * sizeof(faac_real));
memset(psyInfo[channel].prevSamples, 0, size * sizeof(faac_real));
}
size = BLOCK_LEN_SHORT;
for (channel = 0; channel < numChannels; channel++)
{
psydata_t *psydata = psyInfo[channel].data;
psyInfo[channel].sizeS = size;
for (j = 0; j < 8; j++)
{
psydata->engPrev[j] =
(psyfloat *) AllocMemory(NSFB_SHORT * sizeof(psyfloat));
memset(psydata->engPrev[j], 0, NSFB_SHORT * sizeof(psyfloat));
psydata->eng[j] =
(psyfloat *) AllocMemory(NSFB_SHORT * sizeof(psyfloat));
memset(psydata->eng[j], 0, NSFB_SHORT * sizeof(psyfloat));
psydata->engNext[j] =
(psyfloat *) AllocMemory(NSFB_SHORT * sizeof(psyfloat));
memset(psydata->engNext[j], 0, NSFB_SHORT * sizeof(psyfloat));
psydata->engNext2[j] =
(psyfloat *) AllocMemory(NSFB_SHORT * sizeof(psyfloat));
memset(psydata->engNext2[j], 0, NSFB_SHORT * sizeof(psyfloat));
}
}
}
static void PsyEnd(GlobalPsyInfo * gpsyInfo, PsyInfo * psyInfo, unsigned int numChannels)
{
unsigned int channel;
int j;
if (gpsyInfo->hannWindow)
FreeMemory(gpsyInfo->hannWindow);
if (gpsyInfo->hannWindowS)
FreeMemory(gpsyInfo->hannWindowS);
for (channel = 0; channel < numChannels; channel++)
{
if (psyInfo[channel].prevSamples)
FreeMemory(psyInfo[channel].prevSamples);
}
for (channel = 0; channel < numChannels; channel++)
{
psydata_t *psydata = psyInfo[channel].data;
for (j = 0; j < 8; j++)
{
if (psydata->engPrev[j])
FreeMemory(psydata->engPrev[j]);
if (psydata->eng[j])
FreeMemory(psydata->eng[j]);
if (psydata->engNext[j])
FreeMemory(psydata->engNext[j]);
if (psydata->engNext2[j])
FreeMemory(psydata->engNext2[j]);
}
}
for (channel = 0; channel < numChannels; channel++)
{
if (psyInfo[channel].data)
FreeMemory(psyInfo[channel].data);
}
#if PRINTSTAT
printf("short frames: %d/%d (%.2f %%)\n", frames.s, frames.tot, 100.0*frames.s/frames.tot);
#endif
}
/* Do psychoacoustical analysis */
static void PsyCalculate(ChannelInfo * channelInfo, GlobalPsyInfo * gpsyInfo,
PsyInfo * psyInfo, int *cb_width_long, int
num_cb_long, int *cb_width_short,
int num_cb_short, unsigned int numChannels,
faac_real quality
)
{
unsigned int channel;
// limit switching threshold
if (quality < 0.4)
quality = 0.4;
for (channel = 0; channel < numChannels; channel++)
{
if (channelInfo[channel].present)
{
if (channelInfo[channel].cpe &&
channelInfo[channel].ch_is_left)
{ /* CPE */
int leftChan = channel;
int rightChan = channelInfo[channel].paired_ch;
PsyCheckShort(&psyInfo[leftChan], quality);
PsyCheckShort(&psyInfo[rightChan], quality);
}
else if (!channelInfo[channel].cpe &&
channelInfo[channel].lfe)
{ /* LFE */
// Only set block type and it should be OK
psyInfo[channel].block_type = ONLY_LONG_WINDOW;
}
else if (!channelInfo[channel].cpe)
{ /* SCE */
PsyCheckShort(&psyInfo[channel], quality);
}
}
}
}
static void PsyBufferUpdate( FFT_Tables *fft_tables, GlobalPsyInfo * gpsyInfo, PsyInfo * psyInfo,
faac_real *newSamples, unsigned int bandwidth,
int *cb_width_short, int num_cb_short)
{
int win;
faac_real *transBuff = gpsyInfo->sharedWorkBuffLong;
faac_real *transBuffS = gpsyInfo->sharedWorkBuffShort;
psydata_t *psydata = psyInfo->data;
psyfloat *tmp;
int sfb;
psydata->bandS = psyInfo->sizeS * bandwidth * 2 / gpsyInfo->sampleRate;
memcpy(transBuff, psyInfo->prevSamples, psyInfo->size * sizeof(faac_real));
memcpy(transBuff + psyInfo->size, newSamples, psyInfo->size * sizeof(faac_real));
for (win = 0; win < 8; win++)
{
int first = 0;
int last = 0;
memcpy(transBuffS, transBuff + (win * BLOCK_LEN_SHORT) + (BLOCK_LEN_LONG - BLOCK_LEN_SHORT) / 2,
2 * psyInfo->sizeS * sizeof(faac_real));
Hann(gpsyInfo, transBuffS, 2 * psyInfo->sizeS);
MDCT( fft_tables, transBuffS, 2 * psyInfo->sizeS, gpsyInfo->mdctXr, gpsyInfo->mdctXi);
// shift bufs
tmp = psydata->engPrev[win];
psydata->engPrev[win] = psydata->eng[win];
psydata->eng[win] = psydata->engNext[win];
psydata->engNext[win] = psydata->engNext2[win];
psydata->engNext2[win] = tmp;
for (sfb = 0; sfb < num_cb_short; sfb++)
{
faac_real e;
int l;
first = last;
last = first + cb_width_short[sfb];
if (first < 1)
first = 1;
if (first >= psydata->bandS) // band out of range
break;
e = 0.0;
for (l = first; l < last; l++)
e += transBuffS[l] * transBuffS[l];
psydata->engNext2[win][sfb] = e;
}
psydata->lastband = sfb;
for (; sfb < num_cb_short; sfb++)
{
psydata->engNext2[win][sfb] = 0;
}
}
memcpy(psyInfo->prevSamples, newSamples, psyInfo->size * sizeof(faac_real));
}
static void BlockSwitch(CoderInfo * coderInfo, PsyInfo * psyInfo, unsigned int numChannels)
{
unsigned int channel;
int desire = ONLY_LONG_WINDOW;
/* Use the same block type for all channels
If there is 1 channel that wants a short block,
use a short block on all channels.
*/
for (channel = 0; channel < numChannels; channel++)
{
if (psyInfo[channel].block_type == ONLY_SHORT_WINDOW)
desire = ONLY_SHORT_WINDOW;
}
for (channel = 0; channel < numChannels; channel++)
{
int lasttype = coderInfo[channel].block_type;
if (desire == ONLY_SHORT_WINDOW
|| coderInfo[channel].desired_block_type == ONLY_SHORT_WINDOW)
{
if (lasttype == ONLY_LONG_WINDOW || lasttype == SHORT_LONG_WINDOW)
coderInfo[channel].block_type = LONG_SHORT_WINDOW;
else
coderInfo[channel].block_type = ONLY_SHORT_WINDOW;
}
else
{
if (lasttype == ONLY_SHORT_WINDOW || lasttype == LONG_SHORT_WINDOW)
coderInfo[channel].block_type = SHORT_LONG_WINDOW;
else
coderInfo[channel].block_type = ONLY_LONG_WINDOW;
}
coderInfo[channel].desired_block_type = desire;
}
}
psymodel_t psymodel2 =
{
PsyInit,
PsyEnd,
PsyCalculate,
PsyBufferUpdate,
BlockSwitch
};
|