/*
-Copyright (c) 2005, John Hurst
+Copyright (c) 2005-2009, John Hurst
All rights reserved.
Redistribution and use in source and binary forms, with or without
\brief Wave file common elements
*/
-#include <Wav.h>
+#include "Wav.h"
#include <assert.h>
+#include <KM_log.h>
+using Kumu::DefaultLogSink;
-const ui32_t SimpleHeaderLength = 46;
+const ui32_t SimpleWavHeaderLength = 46;
//
ASDCP::Wav::SimpleWaveHeader::SimpleWaveHeader(ASDCP::PCM::AudioDescriptor& ADesc)
nchannels = ADesc.ChannelCount;
bitspersample = ADesc.QuantizationBits;
samplespersec = (ui32_t)ceil(ADesc.AudioSamplingRate.Quotient());
- avgbps = samplespersec * nchannels * ((bitspersample + 7) / 8);
- blockalign = nchannels * ((bitspersample + 7) / 8);
+ blockalign = nchannels * (bitspersample / 8);
+ avgbps = samplespersec * blockalign;
cbsize = 0;
data_len = ASDCP::PCM::CalcFrameBufferSize(ADesc) * ADesc.ContainerDuration;
}
void
ASDCP::Wav::SimpleWaveHeader::FillADesc(ASDCP::PCM::AudioDescriptor& ADesc, ASDCP::Rational PictureRate) const
{
- ADesc.SampleRate = PictureRate;
+ ADesc.EditRate = PictureRate;
ADesc.LinkedTrackID = 0;
ADesc.Locked = 0;
ADesc.QuantizationBits = bitspersample;
ui32_t FrameBufferSize = ASDCP::PCM::CalcFrameBufferSize(ADesc);
ADesc.ContainerDuration = data_len / FrameBufferSize;
+ ADesc.ChannelFormat = PCM::CF_NONE;
}
//
ASDCP::Result_t
-ASDCP::Wav::SimpleWaveHeader::WriteToFile(ASDCP::FileWriter& OutFile) const
+ASDCP::Wav::SimpleWaveHeader::WriteToFile(Kumu::FileWriter& OutFile) const
{
ui32_t write_count;
- byte_t tmp_header[SimpleHeaderLength];
+ byte_t tmp_header[SimpleWavHeaderLength];
byte_t* p = tmp_header;
static ui32_t fmt_len =
+ sizeof(bitspersample)
+ sizeof(cbsize);
- ui32_t RIFF_len = data_len + SimpleHeaderLength - 8;
+ ui32_t RIFF_len = data_len + SimpleWavHeaderLength - 8;
memcpy(p, &FCC_RIFF, sizeof(fourcc)); p += 4;
- *((ui32_t*)p) = ASDCP_i32_LE(RIFF_len); p += 4;
+ *((ui32_t*)p) = KM_i32_LE(RIFF_len); p += 4;
memcpy(p, &FCC_WAVE, sizeof(fourcc)); p += 4;
memcpy(p, &FCC_fmt_, sizeof(fourcc)); p += 4;
- *((ui32_t*)p) = ASDCP_i32_LE(fmt_len); p += 4;
- *((ui16_t*)p) = ASDCP_i16_LE(format); p += 2;
- *((ui16_t*)p) = ASDCP_i16_LE(nchannels); p += 2;
- *((ui32_t*)p) = ASDCP_i32_LE(samplespersec); p += 4;
- *((ui32_t*)p) = ASDCP_i32_LE(avgbps); p += 4;
- *((ui16_t*)p) = ASDCP_i16_LE(blockalign); p += 2;
- *((ui16_t*)p) = ASDCP_i16_LE(bitspersample); p += 2;
- *((ui16_t*)p) = ASDCP_i16_LE(cbsize); p += 2;
+ *((ui32_t*)p) = KM_i32_LE(fmt_len); p += 4;
+ *((ui16_t*)p) = KM_i16_LE(format); p += 2;
+ *((ui16_t*)p) = KM_i16_LE(nchannels); p += 2;
+ *((ui32_t*)p) = KM_i32_LE(samplespersec); p += 4;
+ *((ui32_t*)p) = KM_i32_LE(avgbps); p += 4;
+ *((ui16_t*)p) = KM_i16_LE(blockalign); p += 2;
+ *((ui16_t*)p) = KM_i16_LE(bitspersample); p += 2;
+ *((ui16_t*)p) = KM_i16_LE(cbsize); p += 2;
memcpy(p, &FCC_data, sizeof(fourcc)); p += 4;
- *((ui32_t*)p) = ASDCP_i32_LE(data_len); p += 4;
+ *((ui32_t*)p) = KM_i32_LE(data_len); p += 4;
- return OutFile.Write(tmp_header, SimpleHeaderLength, &write_count);
+ return OutFile.Write(tmp_header, SimpleWavHeaderLength, &write_count);
}
//
ASDCP::Result_t
-ASDCP::Wav::SimpleWaveHeader::ReadFromFile(const ASDCP::FileReader& InFile, ui32_t* data_start)
+ASDCP::Wav::SimpleWaveHeader::ReadFromFile(const Kumu::FileReader& InFile, ui32_t* data_start)
{
ui32_t read_count = 0;
ui32_t local_data_start = 0;
ASDCP::Result_t
ASDCP::Wav::SimpleWaveHeader::ReadFromBuffer(const byte_t* buf, ui32_t buf_len, ui32_t* data_start)
{
- if ( buf_len < SimpleHeaderLength )
+ if ( buf_len < SimpleWavHeaderLength )
return RESULT_SMALLBUF;
*data_start = 0;
fourcc test_RIFF(p); p += 4;
if ( test_RIFF != FCC_RIFF )
{
- DefaultLogSink().Error("Files does not begin with RIFF header\n");
+ // DefaultLogSink().Debug("File does not begin with RIFF header\n");
return RESULT_RAW_FORMAT;
}
- ui32_t RIFF_len = ASDCP_i32_LE(*(ui32_t*)p); p += 4;
+ ui32_t RIFF_len = KM_i32_LE(*(ui32_t*)p); p += 4;
fourcc test_WAVE(p); p += 4;
if ( test_WAVE != FCC_WAVE )
{
- DefaultLogSink().Error("Files does not begin with WAVE header\n");
+ DefaultLogSink().Debug("File does not contain a WAVE header\n");
return RESULT_RAW_FORMAT;
}
while ( p < end_p )
{
test_fcc = fourcc(p); p += 4;
- ui32_t chunk_size = ASDCP_i32_LE(*(ui32_t*)p); p += 4;
+ ui32_t chunk_size = KM_i32_LE(*(ui32_t*)p); p += 4;
if ( test_fcc == FCC_data )
{
if ( chunk_size > RIFF_len )
{
- DefaultLogSink().Error("Chunk size %lu larger than file: %lu\n", chunk_size, RIFF_len);
+ DefaultLogSink().Error("Chunk size %u larger than file: %u\n", chunk_size, RIFF_len);
return RESULT_RAW_FORMAT;
}
if ( test_fcc == FCC_fmt_ )
{
- ui16_t format = ASDCP_i16_LE(*(ui16_t*)p); p += 2;
+ ui16_t format = KM_i16_LE(*(ui16_t*)p); p += 2;
- if ( format != 1 )
+ if ( format != WAVE_FORMAT_PCM && format != WAVE_FORMAT_EXTENSIBLE )
{
- DefaultLogSink().Error("Expecting uncompressed essence, got format type %hu\n", format);
+ DefaultLogSink().Error("Expecting uncompressed PCM data, got format type %hd\n", format);
return RESULT_RAW_FORMAT;
}
- nchannels = ASDCP_i16_LE(*(ui16_t*)p); p += 2;
- samplespersec = ASDCP_i32_LE(*(ui32_t*)p); p += 4;
- avgbps = ASDCP_i32_LE(*(ui32_t*)p); p += 4;
- blockalign = ASDCP_i16_LE(*(ui16_t*)p); p += 2;
- bitspersample = ASDCP_i16_LE(*(ui16_t*)p); p += 2;
- p += chunk_size - 16;
+ nchannels = KM_i16_LE(*(ui16_t*)p); p += 2;
+ samplespersec = KM_i32_LE(*(ui32_t*)p); p += 4;
+ avgbps = KM_i32_LE(*(ui32_t*)p); p += 4;
+ blockalign = KM_i16_LE(*(ui16_t*)p); p += 2;
+ bitspersample = KM_i16_LE(*(ui16_t*)p); p += 2;
+ p += chunk_size - 16; // 16 is the number of bytes read in this block
+ }
+ else
+ {
+ p += chunk_size;
+ }
+ }
+
+ if ( *data_start == 0 ) // can't have no data!
+ {
+ DefaultLogSink().Error("No data chunk found, file contains no essence\n");
+ return RESULT_RAW_FORMAT;
+ }
+
+ return RESULT_OK;
+}
+
+//------------------------------------------------------------------------------------------
+// conversion algorithms from http://www.borg.com/~jglatt/tech/aiff.htm
+
+//
+void
+Rat_to_extended(ASDCP::Rational rate, byte_t* buf)
+{
+ memset(buf, 0, 10);
+ ui32_t value = (ui32_t)ceil(rate.Quotient());
+ ui32_t exp = value;
+ exp >>= 1;
+ ui8_t i = 0;
+
+ for ( ; i < 32; i++ )
+ {
+ exp >>= 1;
+ if ( ! exp )
+ break;
+ }
+
+ *(buf+1) = i;
+
+ for ( i = 32; i != 0 ; i-- )
+ {
+ if ( value & 0x80000000 )
+ break;
+ value <<= 1;
+ }
+
+ *(ui32_t*)(buf+2) = KM_i32_BE(value);
+}
+
+//
+ASDCP::Rational
+extended_to_Rat(const byte_t* buf)
+{
+ ui32_t last = 0;
+ ui32_t mantissa = KM_i32_BE(*(ui32_t*)(buf+2));
+
+ byte_t exp = 30 - *(buf+1);
+
+ while ( exp-- )
+ {
+ last = mantissa;
+ mantissa >>= 1;
+ }
+
+ if ( last & 0x00000001 )
+ mantissa++;
+
+ return ASDCP::Rational(mantissa, 1);
+}
+
+//
+void
+ASDCP::AIFF::SimpleAIFFHeader::FillADesc(ASDCP::PCM::AudioDescriptor& ADesc, ASDCP::Rational PictureRate) const
+{
+ ADesc.EditRate = PictureRate;
+
+ ADesc.ChannelCount = numChannels;
+ ADesc.AudioSamplingRate = extended_to_Rat(sampleRate);
+ ADesc.QuantizationBits = sampleSize;
+ ADesc.BlockAlign = sampleSize / 8;
+ ADesc.AvgBps = (ui32_t) (ADesc.BlockAlign * ADesc.AudioSamplingRate.Quotient());
+ ui32_t FrameBufferSize = ASDCP::PCM::CalcFrameBufferSize(ADesc);
+ ADesc.ContainerDuration = data_len / FrameBufferSize;
+ ADesc.ChannelFormat = PCM::CF_NONE;
+}
+
+//
+ASDCP::Result_t
+ASDCP::AIFF::SimpleAIFFHeader::ReadFromFile(const Kumu::FileReader& InFile, ui32_t* data_start)
+{
+ ui32_t read_count = 0;
+ ui32_t local_data_start = 0;
+ ASDCP::PCM::FrameBuffer TmpBuffer(Wav::MaxWavHeader);
+
+ if ( data_start == 0 )
+ data_start = &local_data_start;
+
+ Result_t result = InFile.Read(TmpBuffer.Data(), TmpBuffer.Capacity(), &read_count);
+
+ if ( ASDCP_SUCCESS(result) )
+ result = ReadFromBuffer(TmpBuffer.RoData(), read_count, data_start);
+
+ return result;
+}
+
+//
+ASDCP::Result_t
+ASDCP::AIFF::SimpleAIFFHeader::ReadFromBuffer(const byte_t* buf, ui32_t buf_len, ui32_t* data_start)
+{
+ if ( buf_len < 32 )
+ return RESULT_SMALLBUF;
+
+ *data_start = 0;
+ const byte_t* p = buf;
+ const byte_t* end_p = p + buf_len;
+
+ fourcc test_FORM(p); p += 4;
+ if ( test_FORM != FCC_FORM )
+ {
+ // DefaultLogSink().Debug("File does not begin with FORM header\n");
+ return RESULT_RAW_FORMAT;
+ }
+
+ ui32_t RIFF_len = KM_i32_BE(*(ui32_t*)p); p += 4;
+
+ fourcc test_AIFF(p); p += 4;
+ if ( test_AIFF != FCC_AIFF )
+ {
+ DefaultLogSink().Debug("File does not contain an AIFF header\n");
+ return RESULT_RAW_FORMAT;
+ }
+
+ fourcc test_fcc;
+
+ while ( p < end_p )
+ {
+ test_fcc = fourcc(p); p += 4;
+ ui32_t chunk_size = KM_i32_BE(*(ui32_t*)p); p += 4;
+
+ if ( test_fcc == FCC_COMM )
+ {
+ numChannels = KM_i16_BE(*(ui16_t*)p); p += 2;
+ numSampleFrames = KM_i32_BE(*(ui32_t*)p); p += 4;
+ sampleSize = KM_i16_BE(*(ui16_t*)p); p += 2;
+ memcpy(sampleRate, p, 10);
+ p += 10;
+ }
+ else if ( test_fcc == FCC_SSND )
+ {
+ if ( chunk_size > RIFF_len )
+ {
+ DefaultLogSink().Error("Chunk size %u larger than file: %u\n", chunk_size, RIFF_len);
+ return RESULT_RAW_FORMAT;
+ }
+
+ ui32_t offset = KM_i32_BE(*(ui32_t*)p); p += 4;
+ p += 4; // blockSize;
+
+ data_len = chunk_size - 8;
+ *data_start = (p - buf) + offset;
+ break;
}
else
{