📄 buffer.c
字号:
/* Copyright (C) 2007 ROCK-CHIPS FUZHOU . All Rights Reserved. */
/*
File : \Audio\buffer\
Desc : AAC解码。
Author : FSH
Date : 2007-06-01
Notes :
$Log :
* FSH 2007/06/01 建立此文件
*
* Vincent 2007/08/xx 少量改动
*/
/****************************************************************/
#include "buffer.h"
static short *LeftPCM;
static short *RightPCM;
unsigned char ucBufFull;
/******************************************************
Name: BufferInit
Desc: 初始化环形缓冲区
Param: psBuffer ->缓冲区状态指针
其余无意义
Return: 无
Global: 无
Note: 无
Author: FSH
Log:
******************************************************/
void
BufferInit(BufferState *psBuffer, long(*pfnIoctl)(unsigned long ulIoctl,
BufferState *psBuffer,
unsigned long ulParam1,
unsigned long ulParam2,
unsigned long ulParam3),
long lInstanceData)
{
psBuffer->lInstanceData = lInstanceData;
//
// Initialize the circular buffer.
//
psBuffer->psLeft = 0;
psBuffer->psRight = 0;
psBuffer->lLength = 0;
//
// Initialize the circular buffer read and write pointers.
//
psBuffer->lReadPtr = 0;
psBuffer->lWritePtr = 0;
}
/******************************************************
Name: BufferSetBuffer
Desc: 设置环形缓冲区的数据地址及长度
【BufferSetBuffer sets the address and length of the data buffer for this circular buffer.】
Param: psBuffer ->缓冲区状态指针
psLeft ->左声道缓冲区指针
psRight ->右声道缓冲区指针
lLength ->缓冲区长度 【必须为4的倍数】
Return: 1-成功 0-失败
Global: 无
Note: 无
Author: FSH
Log:
******************************************************/
long
BufferSetBuffer(BufferState *psBuffer, short *psLeft, short *psRight,
long lLength)
{
long lRet;
//
// Make sure that the size of the buffer is a multiple of four samples.
//
if (lLength & 3)
{
return(0);
}
{
//
// Remember the address and size of the buffer.
//
psBuffer->psLeft = psLeft;
psBuffer->psRight = psRight;
psBuffer->lLength = lLength;
//
// Reset the read and write pointers.
//
psBuffer->lReadPtr = 0;
psBuffer->lWritePtr = 0;
//
// Success.
//
lRet = 1;
}
//
// Return to the caller.
//
return(lRet);
}
/******************************************************
Name: BufferDataAvailable
Desc: 得到环形缓冲区可用数据量
【BufferDataAvailable determines the number of samples that are currently in the circular buffer.】
Param: psBuffer ->缓冲区状态指针
Return: 可用数据量
Global: 无
Note: 无
Author: FSH
Log:
******************************************************/
long
BufferDataAvailable(BufferState *psBuffer)
{
long lRet, lRead, lWrite;
//
// Get the current read and write pointers.
//
lRead = psBuffer->lReadPtr;
lWrite = psBuffer->lWritePtr;
//
// Determine the samples available.
//
if (lWrite >= lRead)
{
//
// The read pointer is before the write pointer in the bufer.
//
lRet = lWrite - lRead;
}
else
{
//
// The write pointer is before the read pointer in the buffer.
//
lRet = psBuffer->lLength - (lRead - lWrite);
}
//
// Return to the caller.
//
return(lRet);
}
/******************************************************
Name: BufferSpaceAvailable
Desc: 得到环形缓冲区可用空间大小
【BufferSpaceAvailable determines the number of samples that can be added to the circular buffer.】
Param: psBuffer ->缓冲区状态指针
skip -> ??
Return: 可用空间大小
Global: 无
Note: 无
Author: FSH
Log:
******************************************************/
long
BufferSpaceAvailable(BufferState *psBuffer, int skip)
{
long lRet, lRead, lWrite;
if (skip == 0)
{
//
// Get the current read and write pointers.
//
lRead = psBuffer->lReadPtr;
lWrite = psBuffer->lWritePtr;
//
// Determine the space available.
//
if (lWrite == lRead)
{
//
// The entire buffer is available (minus four samples).
//
lRet = psBuffer->lLength - 4;
}
else if (lWrite > lRead)
{
//
// The read pointer is before the write pointer in the buffer.
//
lRet = psBuffer->lLength - (lWrite - lRead) - 4;
}
else
{
//
// The write pointer is before the read pointer in the buffer.
//
lRet = lRead - lWrite - 4;
}
//
// Return to the caller.
//
}
else
{
lRead = psBuffer->lReadPtr;
lWrite = psBuffer->lWritePtr;
if (lWrite == lRead)
{
lRet = psBuffer->lLength - 4;
}
else if (lWrite > lRead)
{
lRet = psBuffer->lLength - (lWrite - lRead) - 4;
}
else
{
lRet = lRead - lWrite - 4;
}
}
return(lRet);
}
/******************************************************
Name: BufferIsEmpty
Desc: 判断环形缓冲区是否为空
【BufferIsEmpty determines if the circular buffer is empty.】
Param: psBuffer ->缓冲区状态指针
Return: 1-空 0-非空
Global: 无
Note: 无
Author: FSH
Log:
******************************************************/
long
BufferIsEmpty(BufferState *psBuffer)
{
long lRet;
//
// The circular buffer is empty if its read pointer and write pointer
// are identical.
//
if (psBuffer->lReadPtr == psBuffer->lWritePtr)
{
//
// The buffer is empty.
//
lRet = 1;
}
else
{
//
// The buffer is not empty.
//
lRet = 0;
}
//
// Return to the caller.
//
return(lRet);
}
/******************************************************
Name: BufferGetWritePointer
Desc: 得到环形缓冲区写入指针
【BufferGetWritePointer provides pointers into the circular buffer where data can be written.】
Param: psBuffer ->缓冲区状态指针
ppsLeft ->指向左声道缓冲区的指针 【回写】
ppsRight ->指向右声道缓冲区的指针 【回写】
plLength ->指向长度的指针 【回写】
skip -> ??
Return: 1-成功 0-失败
Global: 无
Note: 无
Author: FSH
Log:
******************************************************/
long
BufferGetWritePointer(BufferState *psBuffer, short **ppsLeft, short **ppsRight,
long *plLength, int skip)
{
long lRet, lRead, lWrite;
if (skip == 0)
{
//
// Get the current read pointer.
//
lRead = psBuffer->lReadPtr;
lWrite = psBuffer->lWritePtr;
//
// Get pointers to the next write position within the circular buffer.
//
*ppsLeft = psBuffer->psLeft + lWrite;
*ppsRight = psBuffer->psRight + lWrite;
//
// Determine the number of sample that can be stored at this offset.
//
if (lRead > lWrite)
{
//
// We can write up to the read pointer.
//
*plLength = lRead - lWrite - 4;
}
else if (lRead == 0)
{
//
// We can write almost to the end of the buffer.
//
*plLength = psBuffer->lLength - lWrite - 4;
}
else
{
//
// We can write up to the end of the buffer.
//
*plLength = psBuffer->lLength - lWrite;
}
//
// Success.
//
lRet = 1;
//
// Return to the caller.
//
}
else
{
lRead = psBuffer->lReadPtr;
lWrite = psBuffer->lWritePtr;
*ppsLeft = psBuffer->psLeft + lWrite;
*ppsRight = psBuffer->psRight + lWrite;
if (lRead > lWrite)
{
*plLength = lRead - lWrite - 4;
}
else if (lRead == 0)
{
*plLength = psBuffer->lLength - lWrite - 4;
}
else
{
*plLength = psBuffer->lLength - lWrite;
}
lRet = 1;
}
return(lRet);
}
/******************************************************
Name: BufferUpdateWritePointer
Desc: 更新环形缓冲区写入指针
【BufferUpdateWritePointer advances the write pointer for the buffer by the specified number of samples.】
Param: psBuffer ->缓冲区状态指针
lLength ->之前已经写入的长度
skip -> ??
Return: 1-成功 0-失败
Global: 无
Note: 无
Author: FSH
Log:
******************************************************/
long
BufferUpdateWritePointer(BufferState *psBuffer, long lLength, int skip)
{
long lRet, lRead, lMaxLength;
long lWrite;
lLength &= ~3; // 20041207
if (skip == 0)
{
//
// Get the current read pointer.
//
lRead = psBuffer->lReadPtr;
lWrite = psBuffer->lWritePtr;
//
// Determine the maximum number of samples we can write.
//
if (lRead > lWrite)
{
//
// The maximum we can write is up to the read pointer.
//
lMaxLength = lRead - lWrite - 4;
}
else if (lRead == 0)
{
//
// The maximum we can write is almost to the end of the buffer.
//
lMaxLength = psBuffer->lLength - lWrite - 4;
}
else
{
//
// The maximum we can write is to the end of the buffer.
//
lMaxLength = psBuffer->lLength - lWrite;
}
//
// See if the specified length is valid.
//
if (lLength > lMaxLength)
{
//
// An invalid length was specified.
//
lRet = 0;
}
else
{
//
// Update the write poitner.
//
lWrite += lLength;
if (lWrite == psBuffer->lLength)
{
lWrite = 0;
}
psBuffer->lWritePtr = lWrite ;
//
// Success.
//
lRet = 1;
}
}
else
{
lRead = psBuffer->lReadPtr;
lWrite = psBuffer->lWritePtr;
if (lRead > lWrite)
{
lMaxLength = lRead - lWrite - 4;
}
else if (lRead == 0)
{
lMaxLength = psBuffer->lLength - lWrite - 4;
}
else
{
lMaxLength = psBuffer->lLength - lWrite;
}
if (lLength > lMaxLength)
{
lRet = 0;
}
else
{
lWrite += lLength;
if (lWrite == psBuffer->lLength)
{
lWrite = 0;
}
psBuffer->lWritePtr = lWrite ;
lRet = 1;
}
}
//
// Return to the caller.
//
return(lRet);
}
/******************************************************
Name: BufferGetReadPointer
Desc: 得到环形缓冲区读取指针
【BufferGetReadPointer provides pointers into the circular buffer where data can be read.】
Param: psBuffer ->缓冲区状态指针
ppsLeft ->指向左声道缓冲区的指针 【回写】
ppsRight ->指向右声道缓冲区的指针 【回写】
plLength ->指向长度的指针 【回写】
Return: 1-成功 0-失败
Global: 无
Note: 无
Author: FSH
Log:
******************************************************/
long
BufferGetReadPointer(BufferState *psBuffer, short **ppsLeft, short **ppsRight,
long *plLength)
{
long lRet, lWrite;
//
// Get the current write pointer.
//
lWrite = psBuffer->lWritePtr;
//
// Get pointers to the next read position within the circular buffer.
//
*ppsLeft = psBuffer->psLeft + psBuffer->lReadPtr;
*ppsRight = psBuffer->psRight + psBuffer->lReadPtr;
//
// Determine the number of sample that can be read at this offset.
//
if (lWrite >= psBuffer->lReadPtr)
{
//
// We can read up to the write pointer.
//
*plLength = lWrite - psBuffer->lReadPtr;
}
else
{
//
// We can read up to the end of the buffer.
//
*plLength = psBuffer->lLength - psBuffer->lReadPtr;
}
//
// Success.
//
lRet = 1;
//
// Return to the caller.
//
return(lRet);
}
/******************************************************
Name: BufferUpdateReadPointer
Desc: 更新环形缓冲区读取指针
【BufferUpdateReadPointer advances the read pointer for the buffer by the specified number of samples.】
Param: psBuffer -> 缓冲区状态指针
lLength -> 之前已经读出的长度
Return: 1-成功 0-失败
Global: 无
Note: 无
Author: FSH
Log:
******************************************************/
long
BufferUpdateReadPointer(BufferState *psBuffer, long lLength)
{
long lRet = 0;
psBuffer->lReadPtr += lLength;
if (psBuffer->lReadPtr == psBuffer->lLength)
{
psBuffer->lReadPtr = 0;
}
//
// Success.
//
lRet = 1;
//
// Return to the caller.
//
return(lRet);
}
long
BufferUpdateReadPointerJS(BufferState *psBuffer, long lLength)
{
long lRet = 0;
//
// Update the read pointer.
//
psBuffer->lReadPtr += lLength;
if (psBuffer->lReadPtr > psBuffer->lLength)
{
psBuffer->lReadPtr = psBuffer->lReadPtr - psBuffer->lLength;
}
else if (psBuffer->lReadPtr == psBuffer->lLength)
{
psBuffer->lReadPtr = 0;
}
//
// Success.
//
lRet = 1;
//
// Return to the caller.
//
return(lRet);
}
//****************************************************************************
//
// BufferSetSampleRate specifies the sample rate of the data in the buffer.
//
//****************************************************************************
long
BufferSetSampleRate(BufferState *psBuffer, long lSampleRate)
{
long lRet;
//
// Success.
//
return(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -