Update to version 2
This commit is contained in:
@@ -32,163 +32,174 @@ namespace CAPS {
|
||||
|
||||
|
||||
template<typename T> Steim2Encoder<T>::~Steim2Encoder() {
|
||||
if ( format != NULL ) delete format;
|
||||
if ( _format ) {
|
||||
delete _format;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T> void Steim2Encoder<T>::update_spw(int bp) {
|
||||
assert(bp < 7);
|
||||
template<typename T> void Steim2Encoder<T>::updateSpw(int bp) {
|
||||
assert(_bp < 7);
|
||||
|
||||
if(buf[bp] < -536870912) {
|
||||
if ( _buf[_bp] < -536870912 ) {
|
||||
CAPS_WARNING("%s.%s.%s.%s: value %d is too large for Steim2 encoding",
|
||||
format->networkCode.c_str(), format->stationCode.c_str(),
|
||||
format->locationCode.c_str(), format->channelCode.c_str(),
|
||||
buf[bp]);
|
||||
buf[bp] = -536870912;
|
||||
spw = 1;
|
||||
_format->networkCode.c_str(), _format->stationCode.c_str(),
|
||||
_format->locationCode.c_str(), _format->channelCode.c_str(),
|
||||
_buf[_bp]);
|
||||
_buf[_bp] = -536870912;
|
||||
_spw = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
if(buf[bp] > 536870911) {
|
||||
if ( _buf[_bp] > 536870911 ) {
|
||||
CAPS_WARNING("%s.%s.%s.%s: value %d is too large for Steim2 encoding",
|
||||
format->networkCode.c_str(), format->stationCode.c_str(),
|
||||
format->locationCode.c_str(), format->channelCode.c_str(),
|
||||
buf[bp]);
|
||||
buf[bp] = 536870911;
|
||||
spw = 1;
|
||||
_format->networkCode.c_str(), _format->stationCode.c_str(),
|
||||
_format->locationCode.c_str(), _format->channelCode.c_str(),
|
||||
_buf[_bp]);
|
||||
_buf[_bp] = 536870911;
|
||||
_spw = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
int spw1 = 7;
|
||||
if(buf[bp] < -16384 || buf[bp] > 16383) spw1 = 1;
|
||||
else if(buf[bp] < -512 || buf[bp] > 511) spw1 = 2;
|
||||
else if(buf[bp] < -128 || buf[bp] > 127) spw1 = 3;
|
||||
else if(buf[bp] < -32 || buf[bp] > 31) spw1 = 4;
|
||||
else if(buf[bp] < -16 || buf[bp] > 15) spw1 = 5;
|
||||
else if(buf[bp] < -8 || buf[bp] > 7) spw1 = 6;
|
||||
if(spw1 < spw) spw = spw1;
|
||||
if ( _buf[_bp] < -16384 || _buf[_bp] > 16383 ) spw1 = 1;
|
||||
else if ( _buf[_bp] < -512 || _buf[_bp] > 511 ) spw1 = 2;
|
||||
else if ( _buf[_bp] < -128 || _buf[_bp] > 127 ) spw1 = 3;
|
||||
else if ( _buf[_bp] < -32 || _buf[_bp] > 31 ) spw1 = 4;
|
||||
else if ( _buf[_bp] < -16 || _buf[_bp] > 15 ) spw1 = 5;
|
||||
else if ( _buf[_bp] < -8 || _buf[_bp] > 7 ) spw1 = 6;
|
||||
if ( spw1 < _spw ) _spw = spw1;
|
||||
}
|
||||
|
||||
template<typename T> void Steim2Encoder<T>::store(int32_t value) {
|
||||
assert(bp < 7);
|
||||
buf[bp] = value - last_sample;
|
||||
last_sample = value;
|
||||
update_spw(bp);
|
||||
++bp;
|
||||
assert(_bp < 7);
|
||||
_buf[_bp] = value - _lastSample;
|
||||
_lastSample = value;
|
||||
updateSpw(_bp);
|
||||
++_bp;
|
||||
}
|
||||
|
||||
template<typename T> void Steim2Encoder<T>::init_packet() {
|
||||
template<typename T> void Steim2Encoder<T>::initPacket() {
|
||||
int i;
|
||||
int32_t begin_sample = last_sample;
|
||||
int32_t begin_sample = _lastSample;
|
||||
|
||||
for(i = 1; i < bp; ++i) {
|
||||
begin_sample -= buf[i];
|
||||
for ( i = 1; i < _bp; ++i ) {
|
||||
begin_sample -= _buf[i];
|
||||
}
|
||||
|
||||
reset();
|
||||
current_packet.data[0].sample_word[0] = htonl(begin_sample);
|
||||
frame_count = 0;
|
||||
nibble_word = 0;
|
||||
fp = 2;
|
||||
_currentPacket.data[0].sampleWord[0] = htonl(begin_sample);
|
||||
_frameCount = 0;
|
||||
_nibbleWord = 0;
|
||||
_fp = 2;
|
||||
}
|
||||
|
||||
template<typename T> void Steim2Encoder<T>::finish_packet() {
|
||||
template<typename T> void Steim2Encoder<T>::finishPacket() {
|
||||
int i;
|
||||
int32_t end_sample = last_sample;
|
||||
int32_t endSample = _lastSample;
|
||||
|
||||
for(i = 0; i < bp; ++i) {
|
||||
end_sample -= buf[i];
|
||||
for ( i = 0; i < _bp; ++i ) {
|
||||
endSample -= _buf[i];
|
||||
}
|
||||
|
||||
current_packet.data[0].sample_word[1] = htonl(end_sample);
|
||||
_currentPacket.data[0].sampleWord[1] = htonl(endSample);
|
||||
}
|
||||
|
||||
template<typename T> void Steim2Encoder<T>::update_packet() {
|
||||
template<typename T> void Steim2Encoder<T>::updatePacket() {
|
||||
unsigned int nibble = 0;
|
||||
u_int32_t sample_word = 0;
|
||||
u_int32_t sampleWord = 0;
|
||||
|
||||
assert(bp < 8);
|
||||
assert(_bp < 8);
|
||||
|
||||
int used = bp;
|
||||
int used = _bp;
|
||||
|
||||
while(used > spw) {
|
||||
while ( used > _spw ) {
|
||||
--used;
|
||||
spw = 7;
|
||||
for(int i = 0; i < used; ++i) update_spw(i);
|
||||
_spw = 7;
|
||||
|
||||
for ( int i = 0; i < used; ++i ) {
|
||||
updateSpw(i);
|
||||
}
|
||||
}
|
||||
|
||||
spw = used;
|
||||
_spw = used;
|
||||
|
||||
switch(spw) {
|
||||
switch ( _spw ) {
|
||||
case 7:
|
||||
nibble = 3;
|
||||
sample_word = (2U << 30) | ((buf[0] & 0xf) << 24) |
|
||||
((buf[1] & 0xf) << 20) | ((buf[2] & 0xf) << 16) |
|
||||
((buf[3] & 0xf) << 12) | ((buf[4] & 0xf) << 8) |
|
||||
((buf[5] & 0xf) << 4) | (buf[6] & 0xf);
|
||||
sampleWord = (2U << 30) | ((_buf[0] & 0xf) << 24) |
|
||||
((_buf[1] & 0xf) << 20) | ((_buf[2] & 0xf) << 16) |
|
||||
((_buf[3] & 0xf) << 12) | ((_buf[4] & 0xf) << 8) |
|
||||
((_buf[5] & 0xf) << 4) | (_buf[6] & 0xf);
|
||||
break;
|
||||
case 6:
|
||||
nibble = 3;
|
||||
sample_word = (1U << 30) | ((buf[0] & 0x1f) << 25) |
|
||||
((buf[1] & 0x1f) << 20) | ((buf[2] & 0x1f) << 15) |
|
||||
((buf[3] & 0x1f) << 10) | ((buf[4] & 0x1f) << 5) |
|
||||
(buf[5] & 0x1f);
|
||||
sampleWord = (1U << 30) | ((_buf[0] & 0x1f) << 25) |
|
||||
((_buf[1] & 0x1f) << 20) | ((_buf[2] & 0x1f) << 15) |
|
||||
((_buf[3] & 0x1f) << 10) | ((_buf[4] & 0x1f) << 5) |
|
||||
(_buf[5] & 0x1f);
|
||||
break;
|
||||
case 5:
|
||||
nibble = 3;
|
||||
sample_word = ((buf[0] & 0x3f) << 24) | ((buf[1] & 0x3f) << 18) |
|
||||
((buf[2] & 0x3f) << 12) | ((buf[3] & 0x3f) << 6) |
|
||||
(buf[4] & 0x3f);
|
||||
sampleWord = ((_buf[0] & 0x3f) << 24) | ((_buf[1] & 0x3f) << 18) |
|
||||
((_buf[2] & 0x3f) << 12) | ((_buf[3] & 0x3f) << 6) |
|
||||
(_buf[4] & 0x3f);
|
||||
break;
|
||||
case 4:
|
||||
nibble = 1;
|
||||
sample_word = ((buf[0] & 0xff) << 24) | ((buf[1] & 0xff) << 16) |
|
||||
((buf[2] & 0xff) << 8) | (buf[3] & 0xff);
|
||||
sampleWord = ((_buf[0] & 0xff) << 24) | ((_buf[1] & 0xff) << 16) |
|
||||
((_buf[2] & 0xff) << 8) | (_buf[3] & 0xff);
|
||||
break;
|
||||
case 3:
|
||||
nibble = 2;
|
||||
sample_word = (3U << 30) | ((buf[0] & 0x3ff) << 20) |
|
||||
((buf[1] & 0x3ff) << 10) | (buf[2] & 0x3ff);
|
||||
sampleWord = (3U << 30) | ((_buf[0] & 0x3ff) << 20) |
|
||||
((_buf[1] & 0x3ff) << 10) | (_buf[2] & 0x3ff);
|
||||
break;
|
||||
case 2:
|
||||
nibble = 2;
|
||||
sample_word = (2U << 30) | ((buf[0] & 0x7fff) << 15) |
|
||||
(buf[1] & 0x7fff);
|
||||
sampleWord = (2U << 30) | ((_buf[0] & 0x7fff) << 15) |
|
||||
(_buf[1] & 0x7fff);
|
||||
break;
|
||||
case 1:
|
||||
nibble = 2;
|
||||
sample_word = (1U << 30) | (buf[0] & 0x3fffffff);
|
||||
sampleWord = (1U << 30) | (_buf[0] & 0x3fffffff);
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
break;
|
||||
}
|
||||
|
||||
nibble_word |= (nibble << (30 - ((fp + 1) << 1)));
|
||||
_nibbleWord |= (nibble << (30 - ((_fp + 1) << 1)));
|
||||
|
||||
spw = 7;
|
||||
for(int i = 0; i < bp - used; ++i) {
|
||||
buf[i] = buf[i + used];
|
||||
update_spw(i);
|
||||
_spw = 7;
|
||||
for ( int i = 0; i < _bp - used; ++i ) {
|
||||
_buf[i] = _buf[i + used];
|
||||
updateSpw(i);
|
||||
}
|
||||
|
||||
bp -= used;
|
||||
_bp -= used;
|
||||
_sampleCount += used;
|
||||
|
||||
current_packet.data[frame_count].nibble_word = htonl(nibble_word);
|
||||
current_packet.data[frame_count].sample_word[fp] = htonl(sample_word);
|
||||
if(++fp < 15) return;
|
||||
_currentPacket.data[_frameCount].nibbleWord = htonl(_nibbleWord);
|
||||
_currentPacket.data[_frameCount].sampleWord[_fp] = htonl(sampleWord);
|
||||
|
||||
if ( ++_fp < 15 ) {
|
||||
return;
|
||||
}
|
||||
|
||||
_nibbleWord = 0;
|
||||
_fp = 0;
|
||||
++_frameCount;
|
||||
|
||||
nibble_word = 0;
|
||||
fp = 0;
|
||||
++frame_count;
|
||||
return;
|
||||
}
|
||||
|
||||
template<typename T> void Steim2Encoder<T>::queue_packet(MSEEDEncoderPacket<Steim2Frame> &pckt) {
|
||||
format->updateBuffer(pckt.record, _sampleCount, frame_count + (fp > 0));
|
||||
template<typename T> void Steim2Encoder<T>::queuePacket(MSEEDEncoderPacket<Steim2Frame> &pckt) {
|
||||
_format->updateBuffer(pckt.record, _sampleCount, _frameCount + (_fp > 0));
|
||||
|
||||
Packet *packet = new Packet(DataRecordPtr(pckt.record), format->networkCode, format->stationCode,
|
||||
format->locationCode, format->channelCode);
|
||||
Packet *packet = new Packet(DataRecordPtr(pckt.record), _format->networkCode,
|
||||
_format->stationCode,
|
||||
_format->locationCode,
|
||||
_format->channelCode);
|
||||
_packetQueue.push_back(PacketPtr(packet));
|
||||
pckt.reset();
|
||||
reset();
|
||||
@@ -200,37 +211,37 @@ template<typename T> void Steim2Encoder<T>::push(void *value) {
|
||||
store(sample_val);
|
||||
_clk.tick();
|
||||
|
||||
while ( bp >= spw ) {
|
||||
if( !current_packet.valid() ) {
|
||||
current_packet = get_packet();
|
||||
init_packet();
|
||||
while ( _bp >= _spw ) {
|
||||
if ( !_currentPacket.valid() ) {
|
||||
_currentPacket = getPacket();
|
||||
initPacket();
|
||||
}
|
||||
|
||||
update_packet();
|
||||
if ( frame_count == number_of_frames(current_packet) ) {
|
||||
finish_packet();
|
||||
queue_packet(current_packet);
|
||||
updatePacket();
|
||||
if ( _frameCount == numberOfFrames(_currentPacket) ) {
|
||||
finishPacket();
|
||||
queuePacket(_currentPacket);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T> void Steim2Encoder<T>::flush() {
|
||||
while ( bp ) {
|
||||
if ( !current_packet.valid() ) {
|
||||
current_packet = get_packet();
|
||||
init_packet();
|
||||
while ( _bp ) {
|
||||
if ( !_currentPacket.valid() ) {
|
||||
_currentPacket = getPacket();
|
||||
initPacket();
|
||||
}
|
||||
|
||||
update_packet();
|
||||
if( frame_count == number_of_frames(current_packet) ) {
|
||||
finish_packet();
|
||||
queue_packet(current_packet);
|
||||
updatePacket();
|
||||
if ( _frameCount == numberOfFrames(_currentPacket) ) {
|
||||
finishPacket();
|
||||
queuePacket(_currentPacket);
|
||||
}
|
||||
}
|
||||
|
||||
if ( current_packet.valid() ) {
|
||||
finish_packet();
|
||||
queue_packet(current_packet);
|
||||
if ( _currentPacket.valid() ) {
|
||||
finishPacket();
|
||||
queuePacket(_currentPacket);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user