decoder gets sample format from header

This commit is contained in:
badaix 2016-01-24 13:43:02 +01:00
parent 1d1ef239b2
commit eed7f287fb
10 changed files with 216 additions and 114 deletions

View file

@ -20,6 +20,8 @@
#define DECODER_H
#include "message/pcmChunk.h"
#include "message/header.h"
#include "message/sampleFormat.h"
class Decoder
{
@ -27,7 +29,7 @@ public:
Decoder() {};
virtual ~Decoder() {};
virtual bool decode(msg::PcmChunk* chunk) = 0;
virtual bool setHeader(msg::Header* chunk) = 0;
virtual msg::SampleFormat setHeader(msg::Header* chunk) = 0;
};

View file

@ -21,6 +21,7 @@
#include <cmath>
#include <FLAC/stream_decoder.h>
#include "flacDecoder.h"
#include "common/snapException.h"
#include "common/log.h"
@ -36,9 +37,11 @@ static void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecod
static msg::Header* flacHeader = NULL;
static msg::PcmChunk* flacChunk = NULL;
static msg::PcmChunk* pcmChunk = NULL;
static msg::SampleFormat sampleFormat;
static FLAC__StreamDecoder *decoder = NULL;
FlacDecoder::FlacDecoder() : Decoder()
{
flacChunk = new msg::PcmChunk();
@ -78,28 +81,25 @@ bool FlacDecoder::decode(msg::PcmChunk* chunk)
}
bool FlacDecoder::setHeader(msg::Header* chunk)
msg::SampleFormat FlacDecoder::setHeader(msg::Header* chunk)
{
flacHeader = chunk;
FLAC__bool ok = true;
FLAC__StreamDecoderInitStatus init_status;
if ((decoder = FLAC__stream_decoder_new()) == NULL)
{
logS(kLogErr) << "ERROR: allocating decoder\n";
return false;
}
throw SnapException("ERROR: allocating decoder");
// (void)FLAC__stream_decoder_set_md5_checking(decoder, true);
init_status = FLAC__stream_decoder_init_stream(decoder, read_callback, NULL, NULL, NULL, NULL, write_callback, metadata_callback, error_callback, this);
if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK)
{
logS(kLogErr) << "ERROR: initializing decoder: " << FLAC__StreamDecoderInitStatusString[init_status] << "\n";
ok = false;
}
FLAC__stream_decoder_process_until_end_of_metadata(decoder);
if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK)
throw SnapException("ERROR: initializing decoder: " + string(FLAC__StreamDecoderInitStatusString[init_status]));
return ok;
sampleFormat.rate = 0;
FLAC__stream_decoder_process_until_end_of_metadata(decoder);
if (sampleFormat.rate == 0)
throw SnapException("Sample format not found");
return sampleFormat;
}
@ -179,9 +179,10 @@ void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMet
if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO)
{
((FlacDecoder*)client_data)->cacheInfo_.sampleRate_ = metadata->data.stream_info.sample_rate;
logO << "sample rate : " << metadata->data.stream_info.sample_rate << "Hz\n";
logO << "bits per sample: " << metadata->data.stream_info.bits_per_sample << "\n";
logO << "channels : " << metadata->data.stream_info.channels << "\n";
sampleFormat.setFormat(
metadata->data.stream_info.sample_rate,
metadata->data.stream_info.bits_per_sample,
metadata->data.stream_info.channels);
}
}

View file

@ -46,7 +46,7 @@ public:
FlacDecoder();
virtual ~FlacDecoder();
virtual bool decode(msg::PcmChunk* chunk);
virtual bool setHeader(msg::Header* chunk);
virtual msg::SampleFormat setHeader(msg::Header* chunk);
CacheInfo cacheInfo_;
};

View file

@ -16,12 +16,15 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "oggDecoder.h"
#include <iostream>
#include <cstring>
#include <cmath>
#include <vorbis/vorbisenc.h>
#include "oggDecoder.h"
#include "common/snapException.h"
#include "common/log.h"
using namespace std;
@ -54,24 +57,24 @@ bool OggDecoder::decode(msg::PcmChunk* chunk)
stream initial header) We need the first page to get the stream
serialno. */
bytes = chunk->payloadSize;
buffer=ogg_sync_buffer(&oy, bytes);
buffer = ogg_sync_buffer(&oy, bytes);
memcpy(buffer, chunk->payload, bytes);
ogg_sync_wrote(&oy,bytes);
chunk->payloadSize = 0;
convsize=4096;//bytes/vi.channels;
convsize = 4096;//bytes/vi.channels;
/* The rest is just a straight decode loop until end of stream */
// while(!eos){
while(true)
{
int result=ogg_sync_pageout(&oy,&og);
if (result==0)
int result = ogg_sync_pageout(&oy, &og);
if (result == 0)
break; /* need more data */
if(result<0)
if(result < 0)
{
/* missing or corrupt data at this page position */
fprintf(stderr,"Corrupt or missing data in bitstream; continuing...\n");
logE << "Corrupt or missing data in bitstream; continuing...\n";
continue;
}
@ -79,19 +82,19 @@ bool OggDecoder::decode(msg::PcmChunk* chunk)
this point */
while(1)
{
result=ogg_stream_packetout(&os,&op);
result = ogg_stream_packetout(&os, &op);
if(result==0)
if (result == 0)
break; /* need more data */
if(result<0)
if (result < 0)
continue; /* missing or corrupt data at this page position */
/* no reason to complain; already complained above */
/* we have a packet. Decode it */
float **pcm;
int samples;
if(vorbis_synthesis(&vb,&op)==0) /* test for success! */
vorbis_synthesis_blockin(&vd,&vb);
if (vorbis_synthesis(&vb,&op) == 0) /* test for success! */
vorbis_synthesis_blockin(&vd, &vb);
/*
**pcm is a multichannel float vector. In stereo, for
@ -99,17 +102,17 @@ bool OggDecoder::decode(msg::PcmChunk* chunk)
the size of each channel. Convert the float values
(-1.<=range<=1.) to whatever PCM format and write it out */
while((samples=vorbis_synthesis_pcmout(&vd,&pcm))>0)
while ((samples = vorbis_synthesis_pcmout(&vd, &pcm)) > 0)
{
int bout=(samples<convsize?samples:convsize);
int bout = (samples<convsize?samples:convsize);
//cout << "samples: " << samples << ", convsize: " << convsize << "\n";
/* convert floats to 16 bit signed ints (host order) and
interleave */
for(int i=0; i<vi.channels; i++)
{
ogg_int16_t *ptr=convbuffer+i;
float *mono=pcm[i];
for(int j=0; j<bout; j++)
float *mono=pcm[i];
for (int j=0; j<bout; j++)
{
int val=floor(mono[j]*32767.f+.5f);
/* might as well guard against clipping */
@ -140,73 +143,56 @@ bool OggDecoder::decode(msg::PcmChunk* chunk)
}
bool OggDecoder::setHeader(msg::Header* chunk)
msg::SampleFormat OggDecoder::setHeader(msg::Header* chunk)
{
bytes = chunk->payloadSize;
buffer=ogg_sync_buffer(&oy, bytes);
memcpy(buffer, chunk->payload, bytes);
ogg_sync_wrote(&oy,bytes);
ogg_sync_wrote(&oy, bytes);
if(ogg_sync_pageout(&oy,&og)!=1)
{
fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
return false;
}
if (ogg_sync_pageout(&oy, &og) != 1)
throw SnapException("Input does not appear to be an Ogg bitstream");
ogg_stream_init(&os,ogg_page_serialno(&og));
vorbis_info_init(&vi);
vorbis_comment_init(&vc);
if(ogg_stream_pagein(&os,&og)<0)
{
fprintf(stderr,"Error reading first page of Ogg bitstream data.\n");
return false;
}
if (ogg_stream_pagein(&os, &og) < 0)
throw SnapException("Error reading first page of Ogg bitstream data");
if(ogg_stream_packetout(&os,&op)!=1)
{
fprintf(stderr,"Error reading initial header packet.\n");
return false;
}
if(vorbis_synthesis_headerin(&vi,&vc,&op)<0)
{
fprintf(stderr,"This Ogg bitstream does not contain Vorbis audio data.\n");
return false;
}
if (ogg_stream_packetout(&os, &op) != 1)
throw SnapException("Error reading initial header packet");
if (vorbis_synthesis_headerin(&vi, &vc, &op) < 0)
throw SnapException("This Ogg bitstream does not contain Vorbis audio data");
int i(0);
while(i<2)
while (i < 2)
{
while(i<2)
while (i < 2)
{
int result=ogg_sync_pageout(&oy,&og);
if(result==0)
int result=ogg_sync_pageout(&oy, &og);
if (result == 0)
break; /* Need more data */
/* Don't complain about missing or corrupt data yet. We'll
catch it at the packet output phase */
if(result==1)
if (result == 1)
{
ogg_stream_pagein(&os,&og); /* we can ignore any errors here as they'll also become apparent at packetout */
while(i<2)
ogg_stream_pagein(&os, &og); /* we can ignore any errors here as they'll also become apparent at packetout */
while (i < 2)
{
result=ogg_stream_packetout(&os,&op);
if(result==0)
result=ogg_stream_packetout(&os, &op);
if (result == 0)
break;
if(result<0)
{
/* Uh oh; data at some point was corrupted or missing!
We can't tolerate that in a header. Die. */
fprintf(stderr,"Corrupt secondary header. Exiting.\n");
return false;
}
result=vorbis_synthesis_headerin(&vi,&vc,&op);
if(result<0)
{
fprintf(stderr,"Corrupt secondary header. Exiting.\n");
return false;
}
/// Uh oh; data at some point was corrupted or missing!
/// We can't tolerate that in a header. Die. */
if (result < 0)
throw SnapException("Corrupt secondary header. Exiting.");
result=vorbis_synthesis_headerin(&vi, &vc, &op);
if (result < 0)
throw SnapException("Corrupt secondary header. Exiting.");
i++;
}
}
@ -220,18 +206,19 @@ bool OggDecoder::setHeader(msg::Header* chunk)
fprintf(stderr,"%s\n",*ptr);
++ptr;
}
fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi.channels,vi.rate);
fprintf(stderr,"Encoded by: %s\n\n",vc.vendor);
/* OK, got and parsed all three headers. Initialize the Vorbis
packet->PCM decoder. */
if(vorbis_synthesis_init(&vd,&vi)==0) /* central decode state */
vorbis_block_init(&vd,&vb); /* local state for most of the decode
so multiple block decodes can
proceed in parallel. We could init
multiple vorbis_block structures
for vd here */
return false;
logE << "Encoded by: " << vc.vendor << "\n";
/// OK, got and parsed all three headers. Initialize the Vorbis packet->PCM decoder.
if (vorbis_synthesis_init(&vd, &vi) == 0)
vorbis_block_init(&vd, &vb);
/// central decode state
/// local state for most of the decode so multiple block decodes can proceed
/// in parallel. We could init multiple vorbis_block structures for vd here
msg::SampleFormat sampleFormat(vi.rate, 16, vi.channels);
return sampleFormat;
}

View file

@ -28,24 +28,22 @@ public:
OggDecoder();
virtual ~OggDecoder();
virtual bool decode(msg::PcmChunk* chunk);
virtual bool setHeader(msg::Header* chunk);
virtual msg::SampleFormat setHeader(msg::Header* chunk);
private:
bool decodePayload(msg::PcmChunk* chunk);
ogg_sync_state oy; /* sync and verify incoming physical bitstream */
ogg_stream_state os; /* take physical pages, weld into a logical
stream of packets */
ogg_page og; /* one Ogg bitstream page. Vorbis packets are inside */
ogg_packet op; /* one raw packet of data for decode */
ogg_sync_state oy; /// sync and verify incoming physical bitstream
ogg_stream_state os; /// take physical pages, weld into a logical stream of packets
ogg_page og; /// one Ogg bitstream page. Vorbis packets are inside
ogg_packet op; /// one raw packet of data for decode
vorbis_info vi; /* struct that stores all the static vorbis bitstream
settings */
vorbis_comment vc; /* struct that stores all the bitstream user comments */
vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
vorbis_block vb; /* local working space for packet->PCM decode */
vorbis_info vi; /// struct that stores all the static vorbis bitstream settings
vorbis_comment vc; /// struct that stores all the bitstream user comments
vorbis_dsp_state vd; /// central working state for the packet->PCM decoder
vorbis_block vb; /// local working space for packet->PCM decode
ogg_int16_t* convbuffer; /* take 8k out of the data segment, not the stack */
ogg_int16_t* convbuffer; /// take 8k out of the data segment, not the stack
int convsize;
char *buffer;

View file

@ -16,8 +16,42 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "common/snapException.h"
#include "common/log.h"
#include "pcmDecoder.h"
#define ID_RIFF 0x46464952
#define ID_WAVE 0x45564157
#define ID_FMT 0x20746d66
#define ID_DATA 0x61746164
struct riff_wave_header
{
uint32_t riff_id;
uint32_t riff_sz;
uint32_t wave_id;
};
struct chunk_header
{
uint32_t id;
uint32_t sz;
};
struct chunk_fmt
{
uint16_t audio_format;
uint16_t num_channels;
uint32_t sample_rate;
uint32_t byte_rate;
uint16_t block_align;
uint16_t bits_per_sample;
};
PcmDecoder::PcmDecoder() : Decoder()
{
}
@ -29,9 +63,61 @@ bool PcmDecoder::decode(msg::PcmChunk* chunk)
}
bool PcmDecoder::setHeader(msg::Header* chunk)
msg::SampleFormat PcmDecoder::setHeader(msg::Header* chunk)
{
return true;
if (chunk->payloadSize < 44)
throw SnapException("PCM header too small");
struct riff_wave_header riff_wave_header;
struct chunk_header chunk_header;
struct chunk_fmt chunk_fmt;
chunk_fmt.sample_rate = 0;
size_t pos(0);
memcpy(&riff_wave_header, chunk->payload + pos, sizeof(riff_wave_header));
pos += sizeof(riff_wave_header);
if ((riff_wave_header.riff_id != ID_RIFF) || (riff_wave_header.wave_id != ID_WAVE))
throw SnapException("Not a riff/wave header");
bool moreChunks(true);
do
{
if (pos + sizeof(chunk_header) > chunk->payloadSize)
throw SnapException("riff/wave header incomplete");
memcpy(&chunk_header, chunk->payload + pos, sizeof(chunk_header));
pos += sizeof(chunk_header);
switch (chunk_header.id)
{
case ID_FMT:
if (pos + sizeof(chunk_fmt) > chunk->payloadSize)
throw SnapException("riff/wave header incomplete");
memcpy(&chunk_fmt, chunk->payload + pos, sizeof(chunk_fmt));
pos += sizeof(chunk_fmt);
/// If the format header is larger, skip the rest
if (chunk_header.sz > sizeof(chunk_fmt))
pos += (chunk_header.sz - sizeof(chunk_fmt));
break;
case ID_DATA:
/// Stop looking for chunks
moreChunks = false;
break;
default:
/// Unknown chunk, skip bytes
pos += chunk_header.sz;
}
}
while (moreChunks);
if (chunk_fmt.sample_rate == 0)
throw SnapException("Sample format not found");
msg::SampleFormat sampleFormat(
chunk_fmt.sample_rate,
chunk_fmt.bits_per_sample,
chunk_fmt.num_channels);
return sampleFormat;
}

View file

@ -26,7 +26,7 @@ class PcmDecoder : public Decoder
public:
PcmDecoder();
virtual bool decode(msg::PcmChunk* chunk);
virtual bool setHeader(msg::Header* chunk);
virtual msg::SampleFormat setHeader(msg::Header* chunk);
};