00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <cstdio>
00020 #include <cstdlib>
00021 #include <cerrno>
00022 #include <iostream>
00023 #include <sstream>
00024 #include "compat.h"
00025 #include "freesurround.h"
00026 #include "el_processor.h"
00027 #include <vector>
00028 #include <list>
00029 #include <map>
00030 #include <math.h>
00031
00032 #include <qstring.h>
00033 #include <qdatetime.h>
00034
00035 using namespace std;
00036
00037 #if 0
00038 #define VERBOSE(args...) \
00039 do { \
00040 QDateTime dtmp = QDateTime::currentDateTime(); \
00041 QString dtime = dtmp.toString("yyyy-MM-dd hh:mm:ss.zzz"); \
00042 ostringstream verbose_macro_tmp; \
00043 verbose_macro_tmp << dtime << " " << args; \
00044 cout << verbose_macro_tmp.str() << endl; \
00045 } while (0)
00046 #else
00047 #define VERBOSE(args...)
00048 #endif
00049 #if 0
00050 #define VERBOSE1(args...) \
00051 do { \
00052 QDateTime dtmp = QDateTime::currentDateTime(); \
00053 QString dtime = dtmp.toString("yyyy-MM-dd hh:mm:ss.zzz"); \
00054 ostringstream verbose_macro_tmp; \
00055 verbose_macro_tmp << dtime << " " << args; \
00056 cout << verbose_macro_tmp.str() << endl; \
00057 } while (0)
00058 #else
00059 #define VERBOSE1(args...)
00060 #endif
00061
00062
00063 const unsigned default_block_size = 8192;
00064
00065
00066
00067 #define MASTER_GAIN
00068
00069
00070
00071 #define INV_MASTER_GAIN
00072
00073 unsigned int block_size = default_block_size;
00074
00075
00076 template<class T> T pop_back(std::list<T> &l)
00077 {
00078 T result(l.back());
00079 l.pop_back();
00080 return result;
00081 }
00082
00083
00084 class object_pool
00085 {
00086 public:
00087 typedef void* (*callback)();
00088
00089 object_pool(callback cbf):construct(cbf) { }
00090 ~object_pool()
00091 {
00092 for (std::map<void*,void*>::iterator i=pool.begin(),e=pool.end();i!=e;i++)
00093 delete i->second;
00094 for (std::list<void*>::iterator i=freelist.begin(),e=freelist.end();i!=e;i++)
00095 delete *i;
00096 }
00097
00098 void *acquire(void *who)
00099 {
00100 std::map<void*,void*>::iterator i(pool.find(who));
00101 if (i != pool.end())
00102 return i->second;
00103 else
00104 if (!freelist.empty())
00105 return pool.insert(std::make_pair(who,pop_back(freelist))).first->second;
00106 else
00107 return pool.insert(std::make_pair(who,construct())).first->second;
00108 }
00109
00110 void release(void *who)
00111 {
00112 std::map<void*,void*>::iterator i(pool.find(who));
00113 if (i != pool.end()) {
00114 freelist.push_back(i->second);
00115 pool.erase(i);
00116 }
00117 }
00118 public:
00119 callback construct;
00120 std::list<void*> freelist;
00121 std::map<void*,void*> pool;
00122 };
00123
00124
00125 struct buffers
00126 {
00127 buffers(unsigned int s):
00128
00129 l(s),r(s),c(s),ls(s),rs(s),lfe(s) { }
00130 void resize(unsigned int s)
00131 {
00132
00133 l.resize(s); r.resize(s); lfe.resize(s);
00134 ls.resize(s); rs.resize(s); c.resize(s);
00135 }
00136 void clear()
00137 {
00138
00139 l.clear(); r.clear(); lfe.clear();
00140 ls.clear(); rs.clear(); c.clear();
00141 }
00142
00143 std::vector<float> l,r,c,ls,rs,lfe,cs,lcs,rcs;
00144 };
00145
00146 struct int16buffers
00147 {
00148 int16buffers(unsigned int s):
00149 l(s),r(s),c(s),ls(s),rs(s),lfe(s) { }
00150 void resize(unsigned int s)
00151 {
00152 l.resize(s); r.resize(s); lfe.resize(s);
00153 ls.resize(s); rs.resize(s); c.resize(s);
00154 }
00155 void clear()
00156 {
00157 l.clear(); r.clear();
00158 ls.clear(); rs.clear(); c.clear();
00159 lfe.clear();
00160 }
00161 std::vector<short> l,r,c,ls,rs,lfe;
00162 };
00163
00164
00165 void *new_decoder() { return new fsurround_decoder(block_size); }
00166 void *new_buffers() { return new buffers(block_size/2); }
00167 void *new_int16buffers() { return new int16buffers(block_size/2); }
00168
00169 object_pool dp(&new_decoder);
00170
00171 object_pool bp16(&new_int16buffers);
00172
00173
00174 #ifdef SPEAKERTEST
00175 int channel_select = -1;
00176 #endif
00177
00178 FreeSurround::FreeSurround(uint srate, bool moviemode, SurroundMode smode) :
00179 srate(srate),
00180 open_(false),
00181 initialized_(false),
00182
00183 int16bufs(NULL),
00184 decoder(0),
00185 in_count(0),
00186 out_count(0),
00187 processed(true),
00188 surround_mode(smode)
00189 {
00190 VERBOSE(QString("FreeSurround::FreeSurround rate %1 moviemode %2").arg(srate).arg(moviemode));
00191 if (moviemode)
00192 {
00193 params.phasemode = 1;
00194 params.center_width = 0;
00195 params.gain = 1.0;
00196 }
00197 else
00198 {
00199 params.center_width = 70;
00200
00201
00202 params.gain = 3.1;
00203 }
00204 switch (surround_mode)
00205 {
00206 case SurroundModeActiveSimple:
00207 params.steering = 0;
00208
00209 break;
00210 case SurroundModeActiveLinear:
00211 params.steering = 1;
00212
00213 break;
00214 default:
00215
00216 break;
00217 }
00218 int16bufs = (int16buffers*)bp16.acquire((void*)1);
00219 open();
00220 #ifdef SPEAKERTEST
00221 channel_select++;
00222 if (channel_select>=6)
00223 channel_select = 0;
00224 VERBOSE(QString("FreeSurround::FreeSurround channel_select %1").arg(channel_select));
00225 #endif
00226
00227 VERBOSE(QString("FreeSurround::FreeSurround done"));
00228 }
00229
00230 void FreeSurround::SetParams()
00231 {
00232 if (decoder)
00233 {
00234 decoder->steering_mode(params.steering);
00235 decoder->phase_mode(params.phasemode);
00236 decoder->surround_coefficients(params.coeff_a, params.coeff_b);
00237 decoder->separation(params.front_sep/100.0,params.rear_sep/100.0);
00238 decoder->gain(params.gain);
00239 }
00240 }
00241
00242 FreeSurround::fsurround_params::fsurround_params(
00243 int32_t center_width,
00244 int32_t dimension
00245 ) :
00246 center_width(center_width),
00247 dimension(dimension),
00248 coeff_a(0.8165),coeff_b(0.5774),
00249 phasemode(0),
00250 steering(1),
00251 front_sep(100),
00252 rear_sep(100),
00253 gain(1.0)
00254 {
00255 }
00256
00257 FreeSurround::~FreeSurround()
00258 {
00259 VERBOSE(QString("FreeSurround::~FreeSurround"));
00260 close();
00261
00262
00263
00264
00265
00266
00267
00268 if (int16bufs)
00269 {
00270 bp16.release((void*)1);
00271 int16bufs = NULL;
00272 }
00273 VERBOSE(QString("FreeSurround::~FreeSurround done"));
00274 }
00275
00276 uint FreeSurround::putSamples(short* samples, uint numSamples, uint numChannels, int step)
00277 {
00278 int i;
00279 int ic = in_count;
00280 int bs = block_size/2;
00281 bool process = true;
00282
00283 switch (surround_mode)
00284 {
00285 case SurroundModePassive:
00286 switch (numChannels)
00287 {
00288 case 1:
00289 for (i=0;(i<numSamples) && (ic < bs);i++,ic++)
00290 {
00291 int16bufs->l[ic] =
00292 int16bufs->c[ic] =
00293 int16bufs->r[ic] =
00294 samples[i] >> 1;
00295 }
00296 break;
00297 case 2:
00298 if (step>0)
00299 {
00300 for (i=0;(i<numSamples) && (ic < bs);i++,ic++)
00301 {
00302 short lt = samples[i] >> 1;
00303 short rt = samples[i+step] >> 1;
00304 int16bufs->l[ic] = lt;
00305 int16bufs->lfe[ic] =
00306 int16bufs->c[ic] = ((lt+rt)*23)>>5;
00307 int16bufs->r[ic] = rt;
00308 int16bufs->ls[ic] =
00309 int16bufs->rs[ic] = ((lt-rt)*23)>>5;
00310 }
00311 }
00312 else
00313 {
00314 for (i=0;(i<numSamples) && (ic < bs);i++,ic++)
00315 {
00316 short lt = samples[i*2] >> 1;
00317 short rt = samples[i*2+1] >> 1;
00318 int16bufs->l[ic] = lt;
00319 int16bufs->lfe[ic] =
00320 int16bufs->c[ic] = ((lt+rt)*23)>>5;
00321 int16bufs->r[ic] = rt;
00322 int16bufs->ls[ic] =
00323 int16bufs->rs[ic] = ((lt-rt)*23)>>5;
00324 }
00325 }
00326 break;
00327 case 6:
00328 for (i=0;(i<numSamples) && (ic < bs);i++,ic++)
00329 {
00330 int16bufs->l[ic] = *samples++ >> 1;
00331 int16bufs->c[ic] = *samples++ >> 1;
00332 int16bufs->r[ic] = *samples++ >> 1;
00333 int16bufs->ls[ic] = *samples++ >> 1;
00334 int16bufs->rs[ic] = *samples++ >> 1;
00335 int16bufs->lfe[ic] = *samples++ >> 1;
00336 }
00337 break;
00338 }
00339 in_count = 0;
00340 out_count = ic;
00341 processed_size = ic;
00342 processed = false;
00343 break;
00344
00345 default:
00346 {
00347 float** inputs = decoder->getInputBuffers();
00348 float * lt = &inputs[0][ic];
00349 float * rt = &inputs[1][ic];
00350 if ((ic+numSamples) > bs)
00351 numSamples = bs - ic;
00352 switch (numChannels)
00353 {
00354 case 1:
00355 for (i=0;i<numSamples;i++)
00356 {
00357 *lt++ =
00358 *rt++ =
00359 *samples++ MASTER_GAIN;
00360 }
00361 break;
00362 case 2:
00363 if (step>0)
00364 {
00365 for (i=0;i<numSamples;i++)
00366 {
00367 *lt++ = samples[0] MASTER_GAIN;
00368 *rt++ = samples[step] MASTER_GAIN;
00369 samples++;
00370 }
00371 }
00372 else
00373 {
00374 for (i=0;i<numSamples;i++)
00375 {
00376 *lt++ = *samples++ MASTER_GAIN;
00377 *rt++ = *samples++ MASTER_GAIN;
00378 }
00379 }
00380 break;
00381 case 6:
00382 {
00383 process = false;
00384 short * l = &int16bufs->l[ic];
00385 short * c = &int16bufs->c[ic];
00386 short * r = &int16bufs->r[ic];
00387 short * ls = &int16bufs->ls[ic];
00388 short * rs = &int16bufs->rs[ic];
00389 short * lfe = &int16bufs->lfe[ic];
00390 for (i=0;i<numSamples;i++)
00391 {
00392 *l++ = *samples++ >> 1;
00393 *c++ = *samples++ >> 1;
00394 *r++ = *samples++ >> 1;
00395 *ls++ = *samples++ >> 1;
00396 *rs++ = *samples++ >> 1;
00397 *lfe++ = *samples++ >> 1;
00398 }
00399 } break;
00400 }
00401 ic += numSamples;
00402 in_count = ic;
00403 processed = process;
00404 if (ic == bs)
00405 {
00406 in_count = 0;
00407 if (process)
00408 {
00409 process_block();
00410 }
00411 out_count = bs;
00412 processed_size = bs;
00413 }
00414 } break;
00415 }
00416 VERBOSE1(QString("FreeSurround::putSamples %1 %2 %3 used %4 generated %5")
00417 .arg(numSamples)
00418 .arg(numChannels)
00419 .arg(step)
00420 .arg(i)
00421 .arg(out_count)
00422 );
00423 return i;
00424 }
00425
00426 uint FreeSurround::putSamples(char* samples, uint numSamples, uint numChannels, int step)
00427 {
00428 int i;
00429 int ic = in_count;
00430 int bs = block_size/2;
00431 bool process = true;
00432
00433 switch (surround_mode)
00434 {
00435 case SurroundModePassive:
00436 switch (numChannels)
00437 {
00438 case 1:
00439 for (i=0;(i<numSamples) && (ic < bs);i++,ic++)
00440 {
00441 int16bufs->l[ic] =
00442 int16bufs->c[ic] =
00443 int16bufs->r[ic] =
00444 samples[i] << 7;
00445 }
00446 break;
00447 case 2:
00448 if (step>0)
00449 {
00450 for (i=0;(i<numSamples) && (ic < bs);i++,ic++)
00451 {
00452 short lt = samples[i] << 7;
00453 short rt = samples[i+step] << 7;
00454 int16bufs->l[ic] = lt;
00455 int16bufs->lfe[ic] =
00456 int16bufs->c[ic] = ((lt+rt)*23)>>5;
00457 int16bufs->r[ic] = rt;
00458 int16bufs->ls[ic] =
00459 int16bufs->rs[ic] = ((lt-rt)*23)>>5;
00460 }
00461 }
00462 else
00463 {
00464 for (i=0;(i<numSamples) && (ic < bs);i++,ic++)
00465 {
00466 short lt = samples[i*2] << 7;
00467 short rt = samples[i*2+1] << 7;
00468 int16bufs->l[ic] = lt;
00469 int16bufs->lfe[ic] =
00470 int16bufs->c[ic] = ((lt+rt)*23)>>5;
00471 int16bufs->r[ic] = rt;
00472 int16bufs->ls[ic] =
00473 int16bufs->rs[ic] = ((lt-rt)*23)>>5;
00474 }
00475 }
00476 break;
00477 case 6:
00478 for (i=0;(i<numSamples) && (ic < bs);i++,ic++)
00479 {
00480 int16bufs->l[ic] = *samples++ << 7;
00481 int16bufs->c[ic] = *samples++ << 7;
00482 int16bufs->r[ic] = *samples++ << 7;
00483 int16bufs->ls[ic] = *samples++ << 7;
00484 int16bufs->rs[ic] = *samples++ << 7;
00485 int16bufs->lfe[ic] = *samples++ << 7;
00486 }
00487 break;
00488 }
00489 in_count = 0;
00490 out_count = ic;
00491 processed_size = ic;
00492 processed = false;
00493 break;
00494
00495 default:
00496 {
00497 float** inputs = decoder->getInputBuffers();
00498 float * lt = &inputs[0][ic];
00499 float * rt = &inputs[1][ic];
00500 if ((ic+numSamples) > bs)
00501 numSamples = bs - ic;
00502 switch (numChannels)
00503 {
00504 case 1:
00505 for (i=0;i<numSamples;i++)
00506 {
00507 *lt++ =
00508 *rt++ =
00509 *samples++ MASTER_GAIN;
00510 }
00511 break;
00512 case 2:
00513 if (step>0)
00514 {
00515 for (i=0;i<numSamples;i++)
00516 {
00517 *lt++ = samples[0] MASTER_GAIN;
00518 *rt++ = samples[step] MASTER_GAIN;
00519 samples++;
00520 }
00521 }
00522 else
00523 {
00524 for (i=0;i<numSamples;i++)
00525 {
00526 *lt++ = *samples++ MASTER_GAIN;
00527 *rt++ = *samples++ MASTER_GAIN;
00528 }
00529 }
00530 break;
00531 case 6:
00532 {
00533 process = false;
00534 short * l = &int16bufs->l[ic];
00535 short * c = &int16bufs->c[ic];
00536 short * r = &int16bufs->r[ic];
00537 short * ls = &int16bufs->ls[ic];
00538 short * rs = &int16bufs->rs[ic];
00539 short * lfe = &int16bufs->lfe[ic];
00540 for (i=0;i<numSamples;i++)
00541 {
00542 *l++ = *samples++ << 7;
00543 *c++ = *samples++ << 7;
00544 *r++ = *samples++ << 7;
00545 *ls++ = *samples++ << 7;
00546 *rs++ = *samples++ << 7;
00547 *lfe++ = *samples++ << 7;
00548 }
00549 } break;
00550 }
00551 ic += numSamples;
00552 in_count = ic;
00553 processed = process;
00554 if (ic == bs)
00555 {
00556 in_count = 0;
00557 if (process)
00558 process_block();
00559 out_count = bs;
00560 processed_size = bs;
00561 }
00562 } break;
00563 }
00564 VERBOSE1(QString("FreeSurround::putSamples %1 %2 %3 used %4 generated %5")
00565 .arg(numSamples)
00566 .arg(numChannels)
00567 .arg(step)
00568 .arg(i)
00569 .arg(out_count)
00570 );
00571 return i;
00572 }
00573
00574 uint FreeSurround::receiveSamples(
00575 short *output,
00576 uint maxSamples
00577 )
00578 {
00579 uint i;
00580 uint oc = out_count;
00581 if (maxSamples>oc) maxSamples = oc;
00582 uint outindex = processed_size - oc;
00583 switch (surround_mode)
00584 {
00585 case SurroundModePassive:
00586 for (unsigned int i=0;i<maxSamples;i++)
00587 {
00588 *output++ = int16bufs->l[outindex];
00589 *output++ = int16bufs->r[outindex];
00590 *output++ = int16bufs->ls[outindex];
00591 *output++ = int16bufs->rs[outindex];
00592 *output++ = int16bufs->c[outindex];
00593 *output++ = int16bufs->lfe[outindex];
00594 oc--;
00595 outindex++;
00596 }
00597 break;
00598
00599 default:
00600 if (processed)
00601 {
00602 float** outputs = decoder->getOutputBuffers();
00603 float * l = &outputs[0][outindex];
00604 float * c = &outputs[1][outindex];
00605 float * r = &outputs[2][outindex];
00606 float * ls = &outputs[3][outindex];
00607 float * rs = &outputs[4][outindex];
00608 float * lfe = &outputs[5][outindex];
00609 for (unsigned int i=0;i<maxSamples;i++)
00610 {
00611 *output++ = lrintf(*l++ INV_MASTER_GAIN);
00612 *output++ = lrintf(*r++ INV_MASTER_GAIN);
00613 *output++ = lrintf(*ls++ INV_MASTER_GAIN);
00614 *output++ = lrintf(*rs++ INV_MASTER_GAIN);
00615 *output++ = lrintf(*c++ INV_MASTER_GAIN);
00616 *output++ = lrintf(*lfe++ INV_MASTER_GAIN);
00617 }
00618 oc -= maxSamples;
00619 outindex += maxSamples;
00620 }
00621 else
00622 {
00623 short * l = &int16bufs->l[outindex];
00624 short * c = &int16bufs->c[outindex];
00625 short * r = &int16bufs->r[outindex];
00626 short * ls = &int16bufs->ls[outindex];
00627 short * rs = &int16bufs->rs[outindex];
00628 short * lfe = &int16bufs->lfe[outindex];
00629 for (unsigned int i=0;i<maxSamples;i++)
00630 {
00631 *output++ = *l++;
00632 *output++ = *r++;
00633 *output++ = *ls++;
00634 *output++ = *rs++;
00635 *output++ = *c++;
00636 *output++ = *lfe++;
00637 }
00638 oc -= maxSamples;
00639 outindex += maxSamples;
00640 }
00641 break;
00642 }
00643 out_count = oc;
00644 VERBOSE1(QString("FreeSurround::receiveSamples %1")
00645 .arg(maxSamples)
00646 );
00647 return maxSamples;
00648 }
00649
00650 void FreeSurround::process_block()
00651 {
00652
00653 try
00654 {
00655 if (decoder)
00656 {
00657
00658 #if 0
00659 decoder->steering_mode(params.steering);
00660 decoder->phase_mode(params.phasemode);
00661 decoder->surround_coefficients(params.coeff_a, params.coeff_b);
00662 decoder->separation(params.front_sep/100.0,params.rear_sep/100.0);
00663 #endif
00664
00665
00666
00667 decoder->decode(params.center_width/100.0,params.dimension/100.0);
00668 }
00669 }
00670 catch(...)
00671 {
00672
00673 }
00674 }
00675
00676 long long FreeSurround::getLatency()
00677 {
00678
00679 if (surround_mode == SurroundModePassive)
00680 return 0;
00681 return decoder ? ((block_size/2 + in_count)*1000000)/(2*srate) : 0;
00682 }
00683
00684 void FreeSurround::flush()
00685 {
00686 if (decoder)
00687 decoder->flush();
00688 int16bufs->clear();
00689 }
00690
00691
00692 void FreeSurround::open()
00693 {
00694 if (!decoder)
00695 {
00696 decoder = (fsurround_decoder*)dp.acquire((void*)1);
00697 decoder->flush();
00698
00699
00700 if (int16bufs)
00701 int16bufs->clear();
00702 decoder->sample_rate(srate);
00703 }
00704 SetParams();
00705 }
00706
00707 void FreeSurround::close()
00708 {
00709 if (decoder)
00710 {
00711 dp.release(this);
00712 decoder = 0;
00713 }
00714 }
00715
00716 uint FreeSurround::numUnprocessedSamples()
00717 {
00718 return in_count;
00719 }
00720
00721 uint FreeSurround::numSamples()
00722 {
00723 return out_count;
00724 }
00725
00726 uint FreeSurround::sampleLatency()
00727 {
00728 if (processed)
00729 return in_count + out_count + (block_size/2);
00730 else
00731 return in_count + out_count;
00732 }
00733
00734 uint FreeSurround::samplesPerBlock()
00735 {
00736 return block_size/2;
00737 }
00738