summaryrefslogtreecommitdiff
path: root/libavcodec/flac.c
diff options
context:
space:
mode:
authorMichael Niedermayer <michaelni@gmx.at>2004-02-18 04:10:52 +0000
committerMichael Niedermayer <michaelni@gmx.at>2004-02-18 04:10:52 +0000
commit9d656110966fbdde0fd1d2e685f3ed3633ba3596 (patch)
tree574755275cb21033ad240b88a96b4a1e9f99d13b /libavcodec/flac.c
parent4fd125984bae61992d965f53e5469dc876688a2d (diff)
crc8 checking, based upon a patch by (Miroslav Lichvar <lichvarm at phoenix dot inf dot upol dot cz>)
less spam/av_log fix synccode search channel_order -> decorrelation_type (cosmetic i know, but the old name is too meaningless IMHO) faster mid/side decorrelator based upon lifting transforms Originally committed as revision 2793 to svn://svn.ffmpeg.org/ffmpeg/trunk
Diffstat (limited to 'libavcodec/flac.c')
-rw-r--r--libavcodec/flac.c126
1 files changed, 88 insertions, 38 deletions
diff --git a/libavcodec/flac.c b/libavcodec/flac.c
index fbc44a3697..822ebc2dfb 100644
--- a/libavcodec/flac.c
+++ b/libavcodec/flac.c
@@ -34,7 +34,7 @@
#define MAX_CHANNELS 8
#define MAX_BLOCKSIZE 65535
-enum channel_order {
+enum decorrelation_type {
INDEPENDENT,
LEFT_SIDE,
RIGHT_SIDE,
@@ -50,7 +50,7 @@ typedef struct FLACContext {
int samplerate, channels;
int blocksize/*, last_blocksize*/;
int bps, curr_bps;
- enum channel_order order;
+ enum decorrelation_type decorrelation;
int32_t *decoded[MAX_CHANNELS];
uint8_t *bitstream;
@@ -69,6 +69,41 @@ static int sample_rate_table[] =
static int sample_size_table[] =
{ 0, 8, 12, 0, 16, 20, 24, 0 };
+static const uint8_t table_crc8[256] = {
+ 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,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 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
+};
+
static int64_t get_utf8(GetBitContext *gb)
{
uint64_t val;
@@ -93,6 +128,17 @@ static int64_t get_utf8(GetBitContext *gb)
return val;
}
+static int get_crc8(uint8_t *buf, int count){
+ int crc=0;
+ int i;
+
+ for(i=0; i<count; i++){
+ crc = table_crc8[crc ^ buf[i]];
+ }
+
+ return crc;
+}
+
static int flac_decode_init(AVCodecContext * avctx)
{
return 0;
@@ -199,10 +245,10 @@ static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order)
{
int i;
- av_log(s->avctx, AV_LOG_DEBUG, " SUBFRAME FIXED\n");
+// av_log(s->avctx, AV_LOG_DEBUG, " SUBFRAME FIXED\n");
/* warm up samples */
- av_log(s->avctx, AV_LOG_DEBUG, " warm up samples: %d\n", pred_order);
+// av_log(s->avctx, AV_LOG_DEBUG, " warm up samples: %d\n", pred_order);
for (i = 0; i < pred_order; i++)
{
@@ -270,10 +316,14 @@ static int decode_subframe_lpc(FLACContext *s, int channel, int pred_order)
av_log(s->avctx, AV_LOG_DEBUG, "invalid coeff precision\n");
return -1;
}
- av_log(s->avctx, AV_LOG_DEBUG, " qlp coeff prec: %d\n", coeff_prec);
+// av_log(s->avctx, AV_LOG_DEBUG, " qlp coeff prec: %d\n", coeff_prec);
qlevel = get_sbits(&s->gb, 5);
- av_log(s->avctx, AV_LOG_DEBUG, " quant level: %d\n", qlevel);
-assert(qlevel >= 0); //FIXME
+// av_log(s->avctx, AV_LOG_DEBUG, " quant level: %d\n", qlevel);
+ if(qlevel < 0){
+ av_log(s->avctx, AV_LOG_DEBUG, "qlevel %d not supported, maybe buggy stream\n", qlevel);
+ return -1;
+ }
+
for (i = 0; i < pred_order; i++)
{
coeffs[i] = get_sbits(&s->gb, coeff_prec);
@@ -301,10 +351,10 @@ static inline int decode_subframe(FLACContext *s, int channel)
s->curr_bps = s->bps;
if(channel == 0){
- if(s->order == RIGHT_SIDE)
+ if(s->decorrelation == RIGHT_SIDE)
s->curr_bps++;
}else{
- if(s->order == LEFT_SIDE || s->order == MID_SIDE)
+ if(s->decorrelation == LEFT_SIDE || s->decorrelation == MID_SIDE)
s->curr_bps++;
}
@@ -354,7 +404,7 @@ static inline int decode_subframe(FLACContext *s, int channel)
}
else if ((type >= 8) && (type <= 12))
{
- av_log(s->avctx, AV_LOG_DEBUG, "coding type: fixed\n");
+// av_log(s->avctx, AV_LOG_DEBUG, "coding type: fixed\n");
if (decode_subframe_fixed(s, channel, type & ~0x8) < 0)
return -1;
}
@@ -382,7 +432,7 @@ static inline int decode_subframe(FLACContext *s, int channel)
static int decode_frame(FLACContext *s)
{
- int blocksize_code, sample_rate_code, sample_size_code, assignment, i;
+ int blocksize_code, sample_rate_code, sample_size_code, assignment, i, crc8;
blocksize_code = get_bits(&s->gb, 4);
@@ -391,25 +441,14 @@ static int decode_frame(FLACContext *s)
assignment = get_bits(&s->gb, 4); /* channel assignment */
if (assignment < 8)
{
- s->order = INDEPENDENT;
+ s->decorrelation = INDEPENDENT;
if (s->channels != assignment+1)
av_log(s->avctx, AV_LOG_DEBUG, "channel number and number of assigned channels differ!\n");
- av_log(s->avctx, AV_LOG_DEBUG, "channels: %d\n", assignment+1);
+// av_log(s->avctx, AV_LOG_DEBUG, "channels: %d\n", assignment+1);
}
- else if (assignment == 8)
+ else if (assignment < 11)
{
- s->order = LEFT_SIDE;
- av_log(s->avctx, AV_LOG_DEBUG, "left/side\n");
- }
- else if (assignment == 9)
- {
- s->order = RIGHT_SIDE;
- av_log(s->avctx, AV_LOG_DEBUG, "right/side\n");
- }
- else if (assignment == 10)
- {
- s->order = MID_SIDE;
- av_log(s->avctx, AV_LOG_DEBUG, "mid/side\n");
+ s->decorrelation= LEFT_SIDE + assignment - 8;
}
else
{
@@ -484,17 +523,18 @@ static int decode_frame(FLACContext *s)
return -1;
}
- skip_bits(&s->gb, 8); /* header crc */
+ skip_bits(&s->gb, 8);
+ crc8= get_crc8(s->gb.buffer, get_bits_count(&s->gb)/8);
+ if(crc8){
+ av_log(s->avctx, AV_LOG_ERROR, "header crc missmatch crc=%2X\n", crc8);
+ return -1;
+ }
// dump_headers(s);
/* subframes */
for (i = 0; i < s->channels; i++)
{
-/* if (s->blocksize != s->last_blocksize)
- {
- s->decoded[i] = av_realloc(s->decoded[i], sizeof(uint32_t)*s->blocksize);
- }*/
// av_log(s->avctx, AV_LOG_DEBUG, "decoded: %x residual: %x\n", s->decoded[i], s->residual[i]);
if (decode_subframe(s, i) < 0)
return -1;
@@ -566,9 +606,6 @@ static int flac_decode_frame(AVCodecContext *avctx,
switch(metadata_type)
{
case METADATA_TYPE_STREAMINFO:
- if(metadata_size == 0)
- av_log(s->avctx, AV_LOG_DEBUG, "size= 0 WTF!?\n");
-
metadata_streaminfo(s);
dump_headers(s);
break;
@@ -582,7 +619,7 @@ static int flac_decode_frame(AVCodecContext *avctx,
else
{
- tmp = show_bits(&s->gb, 16);
+ tmp = show_bits(&s->gb, 16);
if(tmp != 0xFFF8){
av_log(s->avctx, AV_LOG_ERROR, "FRAME HEADER not here\n");
while(get_bits_count(&s->gb)/8+2 < buf_size && show_bits(&s->gb, 16) != 0xFFF8)
@@ -590,8 +627,12 @@ static int flac_decode_frame(AVCodecContext *avctx,
goto end; // we may not have enough bits left to decode a frame, so try next time
}
skip_bits(&s->gb, 16);
- if (decode_frame(s) < 0)
+ if (decode_frame(s) < 0){
+ av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
+ s->bitstream_size=0;
+ s->bitstream_index=0;
return -1;
+ }
}
@@ -637,7 +678,7 @@ static int flac_decode_frame(AVCodecContext *avctx,
}
}
#else
- switch(s->order)
+ switch(s->decorrelation)
{
case INDEPENDENT:
for (j = 0; j < s->blocksize; j++)
@@ -669,25 +710,34 @@ static int flac_decode_frame(AVCodecContext *avctx,
int mid, side;
mid = s->decoded[0][i];
side = s->decoded[1][i];
+
+#if 1 //needs to be checked but IMHO it should be binary identical
+ mid -= side>>1;
+ *(samples++) = mid + side;
+ *(samples++) = mid;
+#else
mid <<= 1;
if (side & 1)
mid++;
*(samples++) = (mid + side) >> 1;
*(samples++) = (mid - side) >> 1;
+#endif
}
break;
}
#endif
*data_size = (int8_t *)samples - (int8_t *)data;
- av_log(s->avctx, AV_LOG_DEBUG, "data size: %d\n", *data_size);
+// av_log(s->avctx, AV_LOG_DEBUG, "data size: %d\n", *data_size);
// s->last_blocksize = s->blocksize;
end:
i= (get_bits_count(&s->gb)+7)/8;;
if(i > buf_size){
av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
+ s->bitstream_size=0;
+ s->bitstream_index=0;
return -1;
}