BvcEncoder::BvcEncoder() : TransmitterModule(FT_YUV_420)
{
	bs_ = es_ = 0;
	quant_ = 0;

	quantizer(0, 0);
	quantizer(1, 0);
	quantizer(2, 0);
	quantizer(3, 2);
	quantizer(4, 2);
	quantizer(5, 2);
	quantizer(6, 3);
	quantizer(7, 3);
	quantizer(8, 4);
	quantizer(9, 4);
}
Exemple #2
0
TEST(TestGpuIndexIVFFlat, UnifiedMemory) {
  // Construct on a random device to test multi-device, if we have
  // multiple devices
  int device = faiss::gpu::randVal(0, faiss::gpu::getNumDevices() - 1);

  if (!faiss::gpu::getFullUnifiedMemSupport(device)) {
    return;
  }

  int dim = 256;

  int numCentroids = 1024;
  // 24 GB of vecs should be enough to test unified memory in
  // oversubscription mode
  size_t numAdd =
    (size_t) 1024 * 1024 * 1024 * 24 / ((size_t) dim * sizeof(float));
  size_t numTrain = numCentroids * 40;
  int numQuery = 10;
  int k = 10;
  int nprobe = 8;

  LOG(INFO) << "generating vecs";
  std::vector<float> trainVecs = faiss::gpu::randVecs(numTrain, dim);
  std::vector<float> addVecs = faiss::gpu::randVecs(numAdd, dim);

  LOG(INFO) << "train CPU";
  faiss::IndexFlatL2 quantizer(dim);
  faiss::IndexIVFFlat cpuIndex(&quantizer, dim, numCentroids, faiss::METRIC_L2);
  LOG(INFO) << "train CPU";
  cpuIndex.train(numTrain, trainVecs.data());
  LOG(INFO) << "add CPU";
  cpuIndex.add(numAdd, addVecs.data());
  cpuIndex.nprobe = nprobe;

  faiss::gpu::StandardGpuResources res;
  res.noTempMemory();

  faiss::gpu::GpuIndexIVFFlatConfig config;
  config.device = device;
  config.memorySpace = faiss::gpu::MemorySpace::Unified;

  faiss::gpu::GpuIndexIVFFlat gpuIndex(&res,
                                       dim,
                                       numCentroids,
                                       faiss::METRIC_L2,
                                       config);
  LOG(INFO) << "copy from CPU";
  gpuIndex.copyFrom(&cpuIndex);
  gpuIndex.setNumProbes(nprobe);

  LOG(INFO) << "compare";

  faiss::gpu::compareIndices(cpuIndex, gpuIndex,
                             numQuery, dim, k, "Unified Memory",
                             kF32MaxRelErr,
                             0.1f,
                             0.015f);
}
static void
create_8bit_images(const char* logoBaseName, int logoWidth, int logoHeight,
	png_bytep* logoRowPtrs, const char* iconsBaseName, int iconsWidth,
	int iconsHeight, png_bytep* iconsRowPtrs)
{
	// Generate 8-bit palette
	BColorQuantizer quantizer(256, 8);
	quantizer.ProcessImage(logoRowPtrs, logoWidth, logoHeight);
	quantizer.ProcessImage(iconsRowPtrs, iconsWidth, iconsHeight);

	RGBA palette[256];
	quantizer.GetColorTable(palette);

	// convert 24-bit logo image to 8-bit indexed color
	uint8* logoIndexedImageRows[logoHeight];
	for (int y = 0; y < logoHeight; y++) {
		logoIndexedImageRows[y] = new uint8[logoWidth];
		for (int x = 0; x < logoWidth; x++)	{
			logoIndexedImageRows[y][x] = nearest_color(&logoRowPtrs[y][x*3],
				palette);
		}
	}

	// convert 24-bit icons image to 8-bit indexed color
	uint8* iconsIndexedImageRows[iconsHeight];
	for (int y = 0; y < iconsHeight; y++) {
		iconsIndexedImageRows[y] = new uint8[iconsWidth];
		for (int x = 0; x < iconsWidth; x++)	{
			iconsIndexedImageRows[y][x] = nearest_color(&iconsRowPtrs[y][x*3],
				palette);
		}
	}


	fprintf(sOutput, "#ifndef __BOOTSPLASH_KERNEL__\n");

	// write the 8-bit color palette
	fprintf(sOutput, "static const uint8 k8BitPalette[] = {\n");
	for (int c = 0; c < 256; c++) {
		fprintf(sOutput, "\t0x%x, 0x%x, 0x%x,\n",
			palette[c].r, palette[c].g, palette[c].b);
	}
	fprintf(sOutput, "\t};\n\n");

	// write the 8-bit images
	write_8bit_image(logoBaseName, logoWidth, logoHeight, logoIndexedImageRows);
	write_8bit_image(iconsBaseName, iconsWidth, iconsHeight,
		iconsIndexedImageRows);

	fprintf(sOutput, "#endif\n\n");

	// free memory
	for (int y = 0; y < logoHeight; y++)
		delete[] logoIndexedImageRows[y];

	for (int y = 0; y < iconsHeight; y++)
		delete[] iconsIndexedImageRows[y];
}
/*
 * encoder q $n $q
 */
int BvcEncoder::command(int argc, const char*const* argv)
{
	if (argc == 4) {
		if (strcmp(argv[1], "q") == 0) {
			int n = atoi(argv[2]);
			int q = atoi(argv[3]);
			quantizer(n, q);
			return (TCL_OK);
		}
	}
	return (TransmitterModule::command(argc, argv));
}
Exemple #5
0
// Greedily compute a set of nonintersecting edges in a point cloud for testing purposes
// Warning: Takes O(n^3) time.
static Array<Vector<int,2>> greedy_nonintersecting_edges(RawArray<const Vector<real,2>> X, const int limit) {
  const auto E = amap(quantizer(bounding_box(X)),X).copy();
  const int n = E.size();
  Array<Vector<int,2>> edges;
  IntervalScope scope;
  for (const int i : range(n*n)) {
    const int pi = int(random_permute(n*n,key+14,i));
    const int a = pi/n,
              b = pi-n*a;
    if (a < b) {
      const auto Ea = Perturbed2(a,E[a]),
                 Eb = Perturbed2(b,E[b]);
      for (const auto e : edges)
        if (!e.contains(a) && !e.contains(b) && segments_intersect(Ea,Eb,Perturbed2(e.x,E[e.x]),Perturbed2(e.y,E[e.y])))
          goto skip;
      edges.append(vec(a,b));
      if (edges.size()==limit || edges.size()==3*n-6)
        break;
      skip:;
    }
  }
  return edges;
}
Exemple #6
0
int main(int argc, char** argv)
{
    EBOFConfig cfg;

    if(init(argc, argv, cfg)) return 1; // If help, fnc returns 1
    logParameters(cfg);

    // Buffers
    auto eventQueue = std::make_shared<QueueT<Event>>();
    auto eventSliceQueue = std::make_shared<QueueT<EventSlice::Ptr>>();
    auto flowSliceQueue = std::make_shared<QueueT<FlowSlice::Ptr>>();

    // Startup
    EventReader<QueueT<Event>> eventReader;
    eventReader.setOutputBuffer(eventQueue);
    eventReader.setURI(cfg.fn_input);

    Quantizer<QueueT> quantizer(cfg.timeSliceDuration);
    quantizer.setInputBuffer(eventQueue);
    quantizer.setOutputBuffer(eventSliceQueue);

    auto factory = std::make_unique<FilterFactory>(cfg.t0, cfg.tk, cfg.timeResolution, cfg.spatialRange, cfg.spatialRange);
    auto padder = std::make_unique<FourierPadder>(cfg.dataSize, cfg.filterSize);
    auto transformer = std::make_unique<FourierTransformerFFTW>(padder->fourierSizeRows_,
                       padder->fourierSizeCols_);

    FilteringEngineCPU<QueueT, QueueT> engine(std::move(factory), std::move(padder), std::move(transformer));

    engine.setInputBuffer(eventSliceQueue);
    engine.setOutputBuffer(flowSliceQueue);
    for(auto angle : cfg.filterAngles) {
        engine.addFilter(angle);
    }

    FlowSinkProcessor<QueueT> sink;
    sink.setInputBuffer(flowSliceQueue);
    auto ebfloWriter = std::make_unique<TaskWriteFlowSlice<OutputPolicyBinary> >();
    ebfloWriter->setFilePath(cfg.fn_path);
    sink.addTask(std::move(ebfloWriter));

    // Start Processing
    LOG(INFO) << "Initialization completed";
    LOG(INFO) << "Processing...";

    auto timer = std::make_shared<boost::timer::auto_cpu_timer>();
    if(eventReader.startPublishing())
    {
        // TODO handle keyboard interrupts
        while(eventReader.isPublishing() || !eventQueue->empty())
        {
            quantizer.process();
            engine.process();
        }
    }
    timer.reset();//trigger time printing
    LOG(INFO) << "Processing finished. Completed " << flowSliceQueue->size() << " FlowSlices!";

    if(!cfg.fn_path.empty())
    {
        FlowSinkProcessor<QueueT> sink;
        sink.setInputBuffer(flowSliceQueue);
        auto ebfloWriter = std::make_unique<TaskWriteFlowSlice<OutputPolicyBinary> >();
        ebfloWriter->setFilePath(cfg.fn_path);
        sink.addTask(std::move(ebfloWriter));
        sink.start();
        while(!flowSliceQueue->empty())
        {
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
        sink.stop();
    }

    eventReader.stopPublishing();

    return 0;
}
Exemple #7
0
Ref<TriangleTopology> delaunay_points(RawArray<const Vector<real,2>> X, RawArray<const Vector<int,2>> edges,
                                      const bool validate) {
  return exact_delaunay_points(amap(quantizer(bounding_box(X)),X).copy(),edges,validate);
}
Exemple #8
0
// ---------------------------------------------------------------------------
//  synthesize
// ---------------------------------------------------------------------------
//! Synthesize a bandwidth-enhanced sinusoidal Partial. Zero-amplitude
//! Breakpoints are inserted at either end of the Partial to reduce
//! turn-on and turn-off artifacts, as described above. The synthesizer
//! will resize the buffer as necessary to accommodate all the samples,
//! including the fade out. Previous contents of the buffer are not
//! overwritten. Partials with start times earlier than the Partial fade
//! time will have shorter onset fades. Partials are not rendered at
//! frequencies above the half-sample rate. 
//!
//! \param  p The Partial to synthesize.
//! \return Nothing.
//! \pre    The partial must have non-negative start time.
//! \post   This Synthesizer's sample buffer (vector) has been 
//!         resized to accommodate the entire duration of the 
//!         Partial, p, including fade out at the end.
//! \throw  InvalidPartial if the Partial has negative start time.
//  
void
Synthesizer::synthesize( Partial p ) 
{
    if ( p.numBreakpoints() == 0 )
    {
        debugger << "Synthesizer ignoring a partial that contains no Breakpoints" << endl;
        return;
    }
    
    if ( p.startTime() < 0 )
    {
        Throw( InvalidPartial, "Tried to synthesize a Partial having start time less than 0." );
    }

    debugger << "synthesizing Partial from " << p.startTime() * m_srateHz 
             << " to " << p.endTime() * m_srateHz << " starting phase "
             << p.initialPhase() << " starting frequency " 
             << p.first().frequency() << endl;
             
    //  better to compute this only once:
    const double OneOverSrate = 1. / m_srateHz;
    
             
    //  use a Resampler to quantize the Breakpoint times and 
    //  correct the phases:
    Resampler quantizer( OneOverSrate );
    quantizer.setPhaseCorrect( true );
    quantizer.quantize( p );
    

    //  resize the sample buffer if necessary:
    typedef unsigned long index_type;
    index_type endSamp = index_type( ( p.endTime() + m_fadeTimeSec ) * m_srateHz );
    if ( endSamp+1 > m_sampleBuffer->size() )
    {
        //  pad by one sample:
        m_sampleBuffer->resize( endSamp+1 );
    }
    
    //  compute the starting time for synthesis of this Partial,
    //  m_fadeTimeSec before the Partial's startTime, but not before 0:
    double itime = ( m_fadeTimeSec < p.startTime() ) ? ( p.startTime() - m_fadeTimeSec ) : 0.;
    index_type currentSamp = index_type( (itime * m_srateHz) + 0.5 );   //  cheap rounding
    
    //  reset the oscillator:
    //  all that really needs to happen here is setting the frequency
    //  correctly, the phase will be reset again in the loop over 
    //  Breakpoints below, and the amp and bw can start at 0.
    m_osc.resetEnvelopes( BreakpointUtils::makeNullBefore( p.first(), p.startTime() - itime ), m_srateHz );

    //  cache the previous frequency (in Hz) so that it
    //  can be used to reset the phase when necessary
    //  in the sample computation loop below (this saves
    //  having to recompute from the oscillator's radian
    //  frequency):
    double prevFrequency = p.first().frequency();   
    
    //  synthesize linear-frequency segments until 
    //  there aren't any more Breakpoints to make segments:
    double * bufferBegin = &( m_sampleBuffer->front() );
    for ( Partial::const_iterator it = p.begin(); it != p.end(); ++it )
    {
        index_type tgtSamp = index_type( (it.time() * m_srateHz) + 0.5 );   //  cheap rounding
        Assert( tgtSamp >= currentSamp );
        
        //  if the current oscillator amplitude is
        //  zero, and the target Breakpoint amplitude
        //  is not, reset the oscillator phase so that
        //  it matches exactly the target Breakpoint 
        //  phase at tgtSamp:
        if ( m_osc.amplitude() == 0. )
        {
            //  recompute the phase so that it is correct
            //  at the target Breakpoint (need to do this
            //  because the null Breakpoint phase was computed
            //  from an interval in seconds, not samples, so
            //  it might be inaccurate):
            //
            //  double favg = 0.5 * ( prevFrequency + it.breakpoint().frequency() );
            //  double dphase = 2 * Pi * favg * ( tgtSamp - currentSamp ) / m_srateHz;
            //
            double dphase = Pi * ( prevFrequency + it.breakpoint().frequency() ) 
                               * ( tgtSamp - currentSamp ) * OneOverSrate;
            m_osc.setPhase( it.breakpoint().phase() - dphase );
        }

        m_osc.oscillate( bufferBegin + currentSamp, bufferBegin + tgtSamp,
                         it.breakpoint(), m_srateHz );
        
        currentSamp = tgtSamp;
        
        //  remember the frequency, may need it to reset the 
        //  phase if a Null Breakpoint is encountered:
        prevFrequency = it.breakpoint().frequency();
    }

    //  render a fade out segment:  
    m_osc.oscillate( bufferBegin + currentSamp, bufferBegin + endSamp,
                     BreakpointUtils::makeNullAfter( p.last(), m_fadeTimeSec ), m_srateHz );
    
}
void median_cut_32bit_quantize(Image& image, PNGWriter& writer, bool hasAlphaChannel)
{
    MedianCut32bitQuantizer quantizer(image.width(), image.height());
    quantizer.process(image.image(), hasAlphaChannel);
    writer.process(quantizer.buffer(), quantizer.palette(), quantizer.trans(), true);
}