VirtualBox

Changeset 35402 in vbox


Ignore:
Timestamp:
Jan 4, 2011 2:59:31 PM (14 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
69266
Message:

Devices/Audio: optimize allocation of temporary buffers for audio input filter.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Audio/audiosniffer.c

    r35353 r35402  
    141141    void *rate;
    142142
     143    /* Temporary buffer for st_sample_t representation of the input audio data. */
     144    void *pvSamplesBuffer;
     145    uint32_t cbSamplesBufferAllocated;
     146
     147    /* Temporary buffer for frequency conversion. */
     148    void *pvRateBuffer;
     149    uint32_t cbRateBufferAllocated;
     150
    143151} SnifferInputCtx;
     152
     153static void ictxDelete(SnifferInputCtx *pCtx)
     154{
     155    /* The caller will not use this context anymore. */
     156    if (pCtx->rate)
     157    {
     158        st_rate_stop (pCtx->rate);
     159    }
     160
     161    RTMemFree(pCtx->pvSamplesBuffer);
     162    RTMemFree(pCtx->pvRateBuffer);
     163
     164    memset(pCtx, 0, sizeof(*pCtx));
     165    RTMemFree(pCtx);
     166}
     167
     168static void ictxReallocSamplesBuffer(SnifferInputCtx *pCtx, uint32_t cs)
     169{
     170    uint32_t cbBuffer = cs * sizeof(st_sample_t);
     171
     172    if (cbBuffer > pCtx->cbSamplesBufferAllocated)
     173    {
     174        RTMemFree(pCtx->pvSamplesBuffer);
     175
     176        pCtx->pvSamplesBuffer = RTMemAlloc(cbBuffer);
     177        if (pCtx->pvSamplesBuffer)
     178        {
     179            pCtx->cbSamplesBufferAllocated = cbBuffer;
     180        }
     181        else
     182        {
     183            pCtx->cbSamplesBufferAllocated = 0;
     184        }
     185    }
     186}
     187
     188static void ictxReallocRateBuffer(SnifferInputCtx *pCtx, uint32_t cs)
     189{
     190    uint32_t cbBuffer = cs * sizeof(st_sample_t);
     191
     192    if (cbBuffer > pCtx->cbRateBufferAllocated)
     193    {
     194        RTMemFree(pCtx->pvRateBuffer);
     195
     196        pCtx->pvRateBuffer = RTMemAlloc(cbBuffer);
     197        if (pCtx->pvRateBuffer)
     198        {
     199            pCtx->cbRateBufferAllocated = cbBuffer;
     200        }
     201        else
     202        {
     203            pCtx->cbRateBufferAllocated = 0;
     204        }
     205    }
     206}
     207
    144208
    145209/*
     
    209273    pCtx->conv = NULL;
    210274    pCtx->rate = NULL;
     275    pCtx->pvSamplesBuffer = NULL;
     276    pCtx->cbSamplesBufferAllocated = 0;
     277    pCtx->pvRateBuffer = NULL;
     278    pCtx->cbRateBufferAllocated = 0;
    211279
    212280    rc = g_pData->pDrv->pfnAudioInputBegin (g_pData->pDrv,
     
    247315    if (c == 0)
    248316    {
    249         /* The caller will not use this context anymore. */
    250         if (pCtx->rate)
    251         {
    252             st_rate_stop (pCtx->rate);
    253         }
    254         RTMemFree(pCtx);
     317        ictxDelete(pCtx);
    255318        pCtx = NULL;
    256319    }
     
    352415    {
    353416        /* Convert PCM samples to st_sample_t.
    354          * And then apply rate convertion if necessary.
     417         * And then apply rate conversion if necessary.
    355418         */
    356         /* @todo Optimization: allocate ps buffer once per context and reallocate if cbData changes. */
    357         uint32_t cs = cbData / pCtx->cBytesPerFrame;
    358         st_sample_t *ps = (st_sample_t *)RTMemAlloc(cs * sizeof(st_sample_t));
     419
     420        /* Optimization: allocate 'ps' buffer once per context and reallocate if cbData changes.
     421         * Usually size of packets is constant.
     422         */
     423        st_sample_t *ps;
     424        uint32_t cs = cbData / pCtx->cBytesPerFrame; /* How many samples. */
     425
     426        ictxReallocSamplesBuffer(pCtx, cs);
     427
     428        ps = (st_sample_t *)pCtx->pvSamplesBuffer;
    359429        if (ps)
    360430        {
    361             void *pvSamplesRateDst = NULL;
    362 
    363431            void *pvSamples = NULL;
    364432            uint32_t cbSamples = 0;
    365433
     434            Assert(pCtx->cbSamplesBufferAllocated >= cs * sizeof(st_sample_t));
     435
    366436            pCtx->conv(ps, pvData, cs, &nominal_volume);
    367437
    368438            if (pCtx->rate)
    369439            {
     440                st_sample_t *psConverted;
    370441                uint32_t csConverted = (cs * pCtx->phw->info.freq) / pCtx->iFreq;
    371                 pvSamplesRateDst = RTMemAlloc(csConverted * sizeof(st_sample_t));
    372 
    373                 if (pvSamplesRateDst)
     442
     443                ictxReallocRateBuffer(pCtx, csConverted);
     444
     445                psConverted = (st_sample_t *)pCtx->pvRateBuffer;
     446                if (psConverted)
    374447                {
    375448                    int csSrc = cs;
    376449                    int csDst = csConverted;
    377450
     451                    Assert(pCtx->cbRateBufferAllocated >= csConverted * sizeof(st_sample_t));
     452
    378453                    st_rate_flow (pCtx->rate,
    379                                   ps, (st_sample_t *)pvSamplesRateDst,
     454                                  ps, psConverted,
    380455                                  &csSrc, &csDst);
    381456
    382                     pvSamples = pvSamplesRateDst;
    383                     cbSamples = csDst * sizeof(st_sample_t);
     457                    pvSamples = psConverted;
     458                    cbSamples = csDst * sizeof(st_sample_t); /* Use csDst as it may be != csConverted */
    384459                }
    385460                else
     
    398473                rc = pCtx->pfnFilterCallback(pCtx->pvFilterCallback, cbSamples, pvSamples);
    399474            }
    400 
    401             RTMemFree(pvSamplesRateDst);
    402             RTMemFree(ps);
    403475        }
    404476        else
     
    428500    if (c == 0)
    429501    {
    430         /* The caller will not use this context anymore. */
    431         if (pCtx->rate)
    432         {
    433             st_rate_stop (pCtx->rate);
    434         }
    435         RTMemFree(pCtx);
    436     }
    437 }
    438 
     502        ictxDelete(pCtx);
     503        pCtx = NULL;
     504    }
     505}
    439506
    440507static DECLCALLBACK(int) iface_Setup (PPDMIAUDIOSNIFFERPORT pInterface, bool fEnable, bool fKeepHostAudio)
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette