FLAC support:
- FLAC container input and output support - FLAC container analyser - FLAC codec support in EBML input and output
This commit is contained in:
parent
0f692233e8
commit
a7183aedc5
17 changed files with 948 additions and 1 deletions
|
@ -1,4 +1,5 @@
|
|||
#include <stddef.h>
|
||||
#include "defines.h"
|
||||
|
||||
namespace checksum{
|
||||
inline unsigned int crc32c(unsigned int crc, const char *data, size_t len){
|
||||
|
@ -143,4 +144,89 @@ namespace checksum{
|
|||
while (tmpData < end){crc = table[((unsigned char)crc) ^ *tmpData++] ^ (crc >> 8);}
|
||||
return crc;
|
||||
}
|
||||
|
||||
inline unsigned int crc16(unsigned int crc, const char *data, size_t len){
|
||||
static const unsigned short table[] = {
|
||||
0x0000, 0x8005, 0x800F, 0x000A, 0x801B, 0x001E, 0x0014, 0x8011,
|
||||
0x8033, 0x0036, 0x003C, 0x8039, 0x0028, 0x802D, 0x8027, 0x0022,
|
||||
0x8063, 0x0066, 0x006C, 0x8069, 0x0078, 0x807D, 0x8077, 0x0072,
|
||||
0x0050, 0x8055, 0x805F, 0x005A, 0x804B, 0x004E, 0x0044, 0x8041,
|
||||
0x80C3, 0x00C6, 0x00CC, 0x80C9, 0x00D8, 0x80DD, 0x80D7, 0x00D2,
|
||||
0x00F0, 0x80F5, 0x80FF, 0x00FA, 0x80EB, 0x00EE, 0x00E4, 0x80E1,
|
||||
0x00A0, 0x80A5, 0x80AF, 0x00AA, 0x80BB, 0x00BE, 0x00B4, 0x80B1,
|
||||
0x8093, 0x0096, 0x009C, 0x8099, 0x0088, 0x808D, 0x8087, 0x0082,
|
||||
0x8183, 0x0186, 0x018C, 0x8189, 0x0198, 0x819D, 0x8197, 0x0192,
|
||||
0x01B0, 0x81B5, 0x81BF, 0x01BA, 0x81AB, 0x01AE, 0x01A4, 0x81A1,
|
||||
0x01E0, 0x81E5, 0x81EF, 0x01EA, 0x81FB, 0x01FE, 0x01F4, 0x81F1,
|
||||
0x81D3, 0x01D6, 0x01DC, 0x81D9, 0x01C8, 0x81CD, 0x81C7, 0x01C2,
|
||||
0x0140, 0x8145, 0x814F, 0x014A, 0x815B, 0x015E, 0x0154, 0x8151,
|
||||
0x8173, 0x0176, 0x017C, 0x8179, 0x0168, 0x816D, 0x8167, 0x0162,
|
||||
0x8123, 0x0126, 0x012C, 0x8129, 0x0138, 0x813D, 0x8137, 0x0132,
|
||||
0x0110, 0x8115, 0x811F, 0x011A, 0x810B, 0x010E, 0x0104, 0x8101,
|
||||
0x8303, 0x0306, 0x030C, 0x8309, 0x0318, 0x831D, 0x8317, 0x0312,
|
||||
0x0330, 0x8335, 0x833F, 0x033A, 0x832B, 0x032E, 0x0324, 0x8321,
|
||||
0x0360, 0x8365, 0x836F, 0x036A, 0x837B, 0x037E, 0x0374, 0x8371,
|
||||
0x8353, 0x0356, 0x035C, 0x8359, 0x0348, 0x834D, 0x8347, 0x0342,
|
||||
0x03C0, 0x83C5, 0x83CF, 0x03CA, 0x83DB, 0x03DE, 0x03D4, 0x83D1,
|
||||
0x83F3, 0x03F6, 0x03FC, 0x83F9, 0x03E8, 0x83ED, 0x83E7, 0x03E2,
|
||||
0x83A3, 0x03A6, 0x03AC, 0x83A9, 0x03B8, 0x83BD, 0x83B7, 0x03B2,
|
||||
0x0390, 0x8395, 0x839F, 0x039A, 0x838B, 0x038E, 0x0384, 0x8381,
|
||||
0x0280, 0x8285, 0x828F, 0x028A, 0x829B, 0x029E, 0x0294, 0x8291,
|
||||
0x82B3, 0x02B6, 0x02BC, 0x82B9, 0x02A8, 0x82AD, 0x82A7, 0x02A2,
|
||||
0x82E3, 0x02E6, 0x02EC, 0x82E9, 0x02F8, 0x82FD, 0x82F7, 0x02F2,
|
||||
0x02D0, 0x82D5, 0x82DF, 0x02DA, 0x82CB, 0x02CE, 0x02C4, 0x82C1,
|
||||
0x8243, 0x0246, 0x024C, 0x8249, 0x0258, 0x825D, 0x8257, 0x0252,
|
||||
0x0270, 0x8275, 0x827F, 0x027A, 0x826B, 0x026E, 0x0264, 0x8261,
|
||||
0x0220, 0x8225, 0x822F, 0x022A, 0x823B, 0x023E, 0x0234, 0x8231,
|
||||
0x8213, 0x0216, 0x021C, 0x8219, 0x0208, 0x820D, 0x8207, 0x0202
|
||||
};
|
||||
|
||||
|
||||
const char *tmpData= data;
|
||||
const char *end = tmpData+len;
|
||||
while(tmpData < end){
|
||||
crc = (crc << 8) ^ table[((crc >> 8) ^ *(char*)tmpData++) & 0x00ff];
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
||||
|
||||
inline unsigned int crc8(unsigned int crc, const char *data, size_t len){
|
||||
|
||||
// crc table for x8+ x2+ x1+ x0 polynomial
|
||||
static const uint8_t crc_table[] = {
|
||||
0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15, 0x38, 0x3f, 0x36, 0x31,
|
||||
0x24, 0x23, 0x2a, 0x2d, 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65,
|
||||
0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d, 0xe0, 0xe7, 0xee, 0xe9,
|
||||
0xfc, 0xfb, 0xf2, 0xf5, 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3, 0xca, 0xcd,
|
||||
0x90, 0x97, 0x9e, 0x99, 0x8c, 0x8b, 0x82, 0x85, 0xa8, 0xaf, 0xa6, 0xa1,
|
||||
0xb4, 0xb3, 0xba, 0xbd, 0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc, 0xd5, 0xd2,
|
||||
0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4, 0xed, 0xea, 0xb7, 0xb0, 0xb9, 0xbe,
|
||||
0xab, 0xac, 0xa5, 0xa2, 0x8f, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9d, 0x9a,
|
||||
0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32, 0x1f, 0x18, 0x11, 0x16,
|
||||
0x03, 0x04, 0x0d, 0x0a, 0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42,
|
||||
0x6f, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d, 0x7a, 0x89, 0x8e, 0x87, 0x80,
|
||||
0x95, 0x92, 0x9b, 0x9c, 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa, 0xa3, 0xa4,
|
||||
0xf9, 0xfe, 0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec, 0xc1, 0xc6, 0xcf, 0xc8,
|
||||
0xdd, 0xda, 0xd3, 0xd4, 0x69, 0x6e, 0x67, 0x60, 0x75, 0x72, 0x7b, 0x7c,
|
||||
0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a, 0x43, 0x44, 0x19, 0x1e, 0x17, 0x10,
|
||||
0x05, 0x02, 0x0b, 0x0c, 0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a, 0x33, 0x34,
|
||||
0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b, 0x76, 0x71, 0x78, 0x7f,
|
||||
0x6a, 0x6d, 0x64, 0x63, 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b,
|
||||
0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13, 0xae, 0xa9, 0xa0, 0xa7,
|
||||
0xb2, 0xb5, 0xbc, 0xbb, 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83,
|
||||
0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb, 0xe6, 0xe1, 0xe8, 0xef,
|
||||
0xfa, 0xfd, 0xf4, 0xf3
|
||||
};
|
||||
|
||||
const char *tmpData = data;
|
||||
const char *end = tmpData + len;
|
||||
while (tmpData < end){
|
||||
crc = crc_table[(*tmpData++) ^ (unsigned char)crc] ^ ((crc << 8) & 0xff);
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}// namespace checksum
|
||||
|
|
|
@ -1851,9 +1851,13 @@ namespace DTSC{
|
|||
setInit(trackIdx, init.data(), init.size());
|
||||
}
|
||||
|
||||
/// Sets the given track's init data.setvod
|
||||
/// Sets the given track's init data.
|
||||
void Meta::setInit(size_t trackIdx, const char *init, size_t initLen){
|
||||
DTSC::Track &t = tracks.at(trackIdx);
|
||||
if (initLen > t.trackInitField.size){
|
||||
FAIL_MSG("Attempting to store %zu bytes of init data, but we only have room for %" PRIu32 " bytes!", initLen, t.trackInitField.size);
|
||||
initLen = t.trackInitField.size;
|
||||
}
|
||||
char *_init = t.track.getPointer(t.trackInitField);
|
||||
Bit::htobs(_init, initLen);
|
||||
memcpy(_init + 2, init, initLen);
|
||||
|
|
128
lib/flac.cpp
Normal file
128
lib/flac.cpp
Normal file
|
@ -0,0 +1,128 @@
|
|||
#include "flac.h"
|
||||
|
||||
/// Checks the first 4 bytes for the string "flaC". Implementing a basic FLAC header check,
|
||||
/// returning true if it is, false if not.
|
||||
bool FLAC::is_header(const char *header){
|
||||
if (header[0] != 'f') return false;
|
||||
if (header[1] != 'L') return false;
|
||||
if (header[2] != 'a') return false;
|
||||
if (header[3] != 'C') return false;
|
||||
return true;
|
||||
}// FLAC::is_header
|
||||
|
||||
size_t FLAC::utfBytes(char p){
|
||||
if ((p & 0x80) == 0x00){return 1;}
|
||||
if ((p & 0xE0) == 0xC0){return 2;}
|
||||
if ((p & 0xF0) == 0xE0){return 3;}
|
||||
if ((p & 0xF8) == 0xF0){return 4;}
|
||||
if ((p & 0xFC) == 0xF8){return 5;}
|
||||
if ((p & 0xFE) == 0xFC){return 6;}
|
||||
if ((p & 0xFF) == 0xFE){return 7;}
|
||||
return 9;
|
||||
}
|
||||
|
||||
uint32_t FLAC::utfVal(char *p){
|
||||
size_t bytes = utfBytes(*p);
|
||||
uint32_t ret = 0;
|
||||
|
||||
if (bytes == 1){
|
||||
ret = (uint32_t)*p;
|
||||
}else if (bytes == 2){
|
||||
ret = (uint32_t)(*p & 0x1F) << 6;
|
||||
ret = ret | (*(p + 1) & 0x3f);
|
||||
}else if (bytes == 3){
|
||||
ret = (uint32_t)(*p & 0x1F) << 6;
|
||||
ret = (ret | (*(p + 1) & 0x3f)) << 6;
|
||||
ret = ret | (*(p + 2) & 0x3f);
|
||||
}else if (bytes == 4){
|
||||
ret = (uint32_t)(*p & 0x1F) << 6;
|
||||
ret = (ret | (*(p + 1) & 0x3f)) << 6;
|
||||
ret = (ret | (*(p + 2) & 0x3f)) << 6;
|
||||
ret = ret | (*(p + 3) & 0x3f);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
FLAC::Frame::Frame(char *pkt){
|
||||
data = pkt;
|
||||
if (data[0] != 0xFF || (data[1] & 0xFC) != 0xF8){
|
||||
WARN_MSG("Sync code incorrect! Ignoring FLAC frame");
|
||||
FAIL_MSG("%x %x", data[0], data[1]);
|
||||
data = 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t FLAC::Frame::samples(){
|
||||
if (!data){return 0;}
|
||||
switch ((data[2] & 0xF0) >> 4){
|
||||
case 0: return 0; // reserved
|
||||
case 1: return 192;
|
||||
case 2: return 576;
|
||||
case 3: return 1152;
|
||||
case 4: return 2304;
|
||||
case 5: return 4608;
|
||||
case 6: return 1; // 1b at end
|
||||
case 7: return 2; // 2b at end
|
||||
default: return 256 << (((data[2] & 0xf0) >> 4) - 8);
|
||||
}
|
||||
}
|
||||
uint32_t FLAC::Frame::rate(){
|
||||
if (!data){return 0;}
|
||||
switch (data[2] & 0x0F){
|
||||
case 0: return 0; // get from STREAMINFO
|
||||
case 1: return 88200;
|
||||
case 2: return 176400;
|
||||
case 3: return 192000;
|
||||
case 4: return 8000;
|
||||
case 5: return 16000;
|
||||
case 6: return 22050;
|
||||
case 7: return 24000;
|
||||
case 8: return 32000;
|
||||
case 9: return 44100;
|
||||
case 10: return 48000;
|
||||
case 11: return 96000;
|
||||
case 12: return 1; // 1b at end, *1000
|
||||
case 13: return 2; // 2b at end
|
||||
case 14: return 3; // 2b at end, *10
|
||||
case 15: return 0; // invalid, get from STREAMINFO
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t FLAC::Frame::channels(){
|
||||
if (!data){return 0;}
|
||||
uint8_t ret = ((data[3] & 0xF0) >> 4) + 1;
|
||||
if (ret > 8 && ret < 12){return 2;}// special stereo
|
||||
return ret;
|
||||
}
|
||||
uint8_t FLAC::Frame::size(){
|
||||
if (!data){return 0;}
|
||||
switch (data[3] & 0x0E){
|
||||
case 0: return 0; // get from STREAMINFO
|
||||
case 1: return 8;
|
||||
case 2: return 12;
|
||||
case 3: return 0; // reserved
|
||||
case 4: return 16;
|
||||
case 5: return 20;
|
||||
case 6: return 24;
|
||||
case 7: return 0; // reserved
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t FLAC::Frame::utfVal(){
|
||||
return FLAC::utfVal(data + 4);
|
||||
}
|
||||
|
||||
std::string FLAC::Frame::toPrettyString(){
|
||||
if (!data){return "Invalid frame";}
|
||||
std::stringstream r;
|
||||
r << "FLAC frame" << std::endl;
|
||||
r << " Block size: " << ((data[1] & 0x1) ? "variable" : "fixed") << std::endl;
|
||||
r << " Samples: " << samples() << std::endl;
|
||||
r << " Rate: " << rate() << "Hz" << std::endl;
|
||||
r << " Channels: " << (int)channels() << std::endl;
|
||||
r << " Size: " << (int)size() << "-bit" << std::endl;
|
||||
return r.str();
|
||||
}
|
32
lib/flac.h
Normal file
32
lib/flac.h
Normal file
|
@ -0,0 +1,32 @@
|
|||
#pragma once
|
||||
#include <mist/defines.h>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <unistd.h> //for stat
|
||||
#include <util.h>
|
||||
|
||||
namespace FLAC{
|
||||
bool is_header(const char *header); ///< Checks the first 4 bytes for the string "flaC".
|
||||
size_t utfBytes(char p); // UTF encoding byte size
|
||||
uint32_t utfVal(char *p); // UTF encoding value
|
||||
|
||||
size_t rate();
|
||||
uint8_t channels();
|
||||
|
||||
class Frame{
|
||||
public:
|
||||
Frame(char *pkt);
|
||||
uint16_t samples();
|
||||
uint32_t rate();
|
||||
uint8_t channels();
|
||||
uint8_t size();
|
||||
|
||||
uint32_t utfVal(); // UTF encoding value
|
||||
std::string toPrettyString();
|
||||
|
||||
private:
|
||||
char *data;
|
||||
};
|
||||
|
||||
}// namespace FLAC
|
|
@ -57,6 +57,7 @@ headers = [
|
|||
'websocket.h',
|
||||
'url.h',
|
||||
'urireader.h',
|
||||
'flac.h',
|
||||
]
|
||||
|
||||
if have_srt
|
||||
|
@ -123,6 +124,7 @@ libmist = library('mist',
|
|||
'url.cpp',
|
||||
'urireader.cpp',
|
||||
'websocket.cpp',
|
||||
'flac.cpp',
|
||||
extra_code,
|
||||
include_directories: incroot,
|
||||
dependencies: mist_deps,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue