@ -33,8 +33,8 @@
# include "avcodec.h"
# include "get_bits.h"
# define FORMAT_INT 1
# define FORMAT_FLOAT 3
# define FORMAT_SIMPLE 1
# define FORMAT_ENCRYPTED 2
# define MAX_ORDER 16
typedef struct TTAFilter {
@ -58,7 +58,7 @@ typedef struct TTAContext {
AVCodecContext * avctx ;
GetBitContext gb ;
int flags , channels , bps , is_float , data_length ;
int format , channels , bps , data_length ;
int frame_length , last_frame_length , total_frames ;
int32_t * decode_buffer ;
@ -182,7 +182,7 @@ static int tta_get_unary(GetBitContext *gb)
int ret = 0 ;
// count ones
while ( get_bits1 ( gb ) )
while ( get_bits_left ( gb ) > 0 & & get_bits1 ( gb ) )
ret + + ;
return ret ;
}
@ -213,58 +213,54 @@ static av_cold int tta_decode_init(AVCodecContext * avctx)
{
/* signature */
skip_bits ( & s - > gb , 32 ) ;
// if (get_bits_long(&s->gb, 32) != av_bswap32(AV_RL32("TTA1"))) {
// av_log(s->avctx, AV_LOG_ERROR, "Missing magic\n");
// return -1;
// }
s - > flags = get_bits ( & s - > gb , 16 ) ;
if ( s - > flags ! = 1 & & s - > flags ! = 3 )
{
av_log ( s - > avctx , AV_LOG_ERROR , " Invalid flags \n " ) ;
s - > format = get_bits ( & s - > gb , 16 ) ;
if ( s - > format > 2 ) {
av_log ( s - > avctx , AV_LOG_ERROR , " Invalid format \n " ) ;
return - 1 ;
}
s - > is_float = ( s - > flags = = FORMAT_FLOAT ) ;
if ( s - > format = = FORMAT_ENCRYPTED ) {
av_log_missing_feature ( s - > avctx , " Encrypted TTA " , 0 ) ;
return AVERROR ( EINVAL ) ;
}
avctx - > channels = s - > channels = get_bits ( & s - > gb , 16 ) ;
if ( s - > channels > 1 & & s - > channels < 9 )
avctx - > channel_layout = tta_channel_layouts [ s - > channels - 2 ] ;
avctx - > bits_per_coded_sample = get_bits ( & s - > gb , 16 ) ;
s - > bps = ( avctx - > bits_per_coded_sample + 7 ) / 8 ;
avctx - > sample_rate = get_bits_long ( & s - > gb , 32 ) ;
if ( avctx - > sample_rate > 1000000 ) { //prevent FRAME_TIME * avctx->sample_rate from overflowing and sanity check
av_log ( avctx , AV_LOG_ERROR , " sample_rate too large \n " ) ;
return - 1 ;
}
s - > data_length = get_bits_long ( & s - > gb , 32 ) ;
skip_bits ( & s - > gb , 32 ) ; // CRC32 of header
if ( s - > is_float )
{
avctx - > sample_fmt = AV_SAMPLE_FMT_FLT ;
av_log_ask_for_sample ( s - > avctx , " Unsupported sample format. \n " ) ;
return - 1 ;
}
else switch ( s - > bps ) {
case 1 : avctx - > sample_fmt = AV_SAMPLE_FMT_U8 ; break ;
case 2 : avctx - > sample_fmt = AV_SAMPLE_FMT_S16 ; break ;
case 3 : avctx - > bits_per_coded_sample = 24 ;
case 4 : avctx - > sample_fmt = AV_SAMPLE_FMT_S32 ; break ;
default :
av_log_ask_for_sample ( s - > avctx ,
" Invalid/unsupported sample format. \n " ) ;
return - 1 ;
switch ( s - > bps ) {
case 1 : avctx - > sample_fmt = AV_SAMPLE_FMT_U8 ; break ;
case 2 :
avctx - > sample_fmt = AV_SAMPLE_FMT_S16 ;
avctx - > bits_per_raw_sample = 16 ;
break ;
case 3 :
avctx - > sample_fmt = AV_SAMPLE_FMT_S32 ;
avctx - > bits_per_raw_sample = 24 ;
break ;
//case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
default :
av_log ( avctx , AV_LOG_ERROR , " Invalid/unsupported sample format. \n " ) ;
return AVERROR_INVALIDDATA ;
}
// FIXME: horribly broken, but directly from reference source
# define FRAME_TIME 1.04489795918367346939
s - > frame_length = ( int ) ( FRAME_TIME * avctx - > sample_rate ) ;
// prevent overflow
if ( avctx - > sample_rate > 0x7FFFFF ) {
av_log ( avctx , AV_LOG_ERROR , " sample_rate too large \n " ) ;
return AVERROR ( EINVAL ) ;
}
s - > frame_length = 256 * avctx - > sample_rate / 245 ;
s - > last_frame_length = s - > data_length % s - > frame_length ;
s - > total_frames = s - > data_length / s - > frame_length +
( s - > last_frame_length ? 1 : 0 ) ;
av_log ( s - > avctx , AV_LOG_DEBUG , " flags: %x chans: %d bps: %d rate: %d block: %d \n " ,
s - > flags , avctx - > channels , avctx - > bits_per_coded_sample , avctx - > sample_rate ,
av_log ( s - > avctx , AV_LOG_DEBUG , " format: %d chans: %d bps: %d rate: %d block: %d \n " ,
s - > format , avctx - > channels , avctx - > bits_per_coded_sample , avctx - > sample_rate ,
avctx - > block_align ) ;
av_log ( s - > avctx , AV_LOG_DEBUG , " data_length: %d frame_length: %d last: %d total: %d \n " ,
s - > data_length , s - > frame_length , s - > last_frame_length , s - > total_frames ) ;
@ -283,8 +279,10 @@ static av_cold int tta_decode_init(AVCodecContext * avctx)
if ( ! s - > decode_buffer )
return AVERROR ( ENOMEM ) ;
s - > ch_ctx = av_malloc ( avctx - > channels * sizeof ( * s - > ch_ctx ) ) ;
if ( ! s - > ch_ctx )
if ( ! s - > ch_ctx ) {
av_freep ( & s - > decode_buffer ) ;
return AVERROR ( ENOMEM ) ;
}
} else {
av_log ( avctx , AV_LOG_ERROR , " Wrong extradata present \n " ) ;
return - 1 ;
@ -300,114 +298,113 @@ static int tta_decode_frame(AVCodecContext *avctx,
const uint8_t * buf = avpkt - > data ;
int buf_size = avpkt - > size ;
TTAContext * s = avctx - > priv_data ;
int i ;
int i , out_size ;
int cur_chan = 0 , framelen = s - > frame_length ;
int32_t * p ;
init_get_bits ( & s - > gb , buf , buf_size * 8 ) ;
{
int cur_chan = 0 , framelen = s - > frame_length ;
int32_t * p ;
if ( * data_size < ( framelen * s - > channels * av_get_bits_per_sample_fmt ( avctx - > sample_fmt ) / 8 ) ) {
av_log ( avctx , AV_LOG_ERROR , " Output buffer size is too small. \n " ) ;
return - 1 ;
}
// FIXME: seeking
s - > total_frames - - ;
if ( ! s - > total_frames & & s - > last_frame_length )
framelen = s - > last_frame_length ;
// init per channel states
for ( i = 0 ; i < s - > channels ; i + + ) {
s - > ch_ctx [ i ] . predictor = 0 ;
ttafilter_init ( & s - > ch_ctx [ i ] . filter , ttafilter_configs [ s - > bps - 1 ] [ 0 ] , ttafilter_configs [ s - > bps - 1 ] [ 1 ] ) ;
rice_init ( & s - > ch_ctx [ i ] . rice , 10 , 10 ) ;
// FIXME: seeking
s - > total_frames - - ;
if ( ! s - > total_frames & & s - > last_frame_length )
framelen = s - > last_frame_length ;
out_size = framelen * s - > channels * av_get_bytes_per_sample ( avctx - > sample_fmt ) ;
if ( * data_size < out_size ) {
av_log ( avctx , AV_LOG_ERROR , " Output buffer size is too small. \n " ) ;
return - 1 ;
}
// decode directly to output buffer for 24-bit sample format
if ( s - > bps = = 3 )
s - > decode_buffer = data ;
// init per channel states
for ( i = 0 ; i < s - > channels ; i + + ) {
s - > ch_ctx [ i ] . predictor = 0 ;
ttafilter_init ( & s - > ch_ctx [ i ] . filter , ttafilter_configs [ s - > bps - 1 ] [ 0 ] , ttafilter_configs [ s - > bps - 1 ] [ 1 ] ) ;
rice_init ( & s - > ch_ctx [ i ] . rice , 10 , 10 ) ;
}
for ( p = s - > decode_buffer ; p < s - > decode_buffer + ( framelen * s - > channels ) ; p + + ) {
int32_t * predictor = & s - > ch_ctx [ cur_chan ] . predictor ;
TTAFilter * filter = & s - > ch_ctx [ cur_chan ] . filter ;
TTARice * rice = & s - > ch_ctx [ cur_chan ] . rice ;
uint32_t unary , depth , k ;
int32_t value ;
unary = tta_get_unary ( & s - > gb ) ;
if ( unary = = 0 ) {
depth = 0 ;
k = rice - > k0 ;
} else {
depth = 1 ;
k = rice - > k1 ;
unary - - ;
}
for ( p = s - > decode_buffer ; p < s - > decode_buffer + ( framelen * s - > channels ) ; p + + ) {
int32_t * predictor = & s - > ch_ctx [ cur_chan ] . predictor ;
TTAFilter * filter = & s - > ch_ctx [ cur_chan ] . filter ;
TTARice * rice = & s - > ch_ctx [ cur_chan ] . rice ;
uint32_t unary , depth , k ;
int32_t value ;
unary = tta_get_unary ( & s - > gb ) ;
if ( unary = = 0 ) {
depth = 0 ;
k = rice - > k0 ;
} else {
depth = 1 ;
k = rice - > k1 ;
unary - - ;
}
if ( get_bits_left ( & s - > gb ) < k )
return - 1 ;
if ( get_bits_left ( & s - > gb ) < k )
if ( k ) {
if ( k > MIN_CACHE_BITS )
return - 1 ;
value = ( unary < < k ) + get_bits ( & s - > gb , k ) ;
} else
value = unary ;
// FIXME: copy paste from original
switch ( depth ) {
case 1 :
rice - > sum1 + = value - ( rice - > sum1 > > 4 ) ;
if ( rice - > k1 > 0 & & rice - > sum1 < shift_16 [ rice - > k1 ] )
rice - > k1 - - ;
else if ( rice - > sum1 > shift_16 [ rice - > k1 + 1 ] )
rice - > k1 + + ;
value + = shift_1 [ rice - > k0 ] ;
default :
rice - > sum0 + = value - ( rice - > sum0 > > 4 ) ;
if ( rice - > k0 > 0 & & rice - > sum0 < shift_16 [ rice - > k0 ] )
rice - > k0 - - ;
else if ( rice - > sum0 > shift_16 [ rice - > k0 + 1 ] )
rice - > k0 + + ;
}
if ( k ) {
if ( k > MIN_CACHE_BITS )
return - 1 ;
value = ( unary < < k ) + get_bits ( & s - > gb , k ) ;
} else
value = unary ;
// FIXME: copy paste from original
switch ( depth ) {
case 1 :
rice - > sum1 + = value - ( rice - > sum1 > > 4 ) ;
if ( rice - > k1 > 0 & & rice - > sum1 < shift_16 [ rice - > k1 ] )
rice - > k1 - - ;
else if ( rice - > sum1 > shift_16 [ rice - > k1 + 1 ] )
rice - > k1 + + ;
value + = shift_1 [ rice - > k0 ] ;
default :
rice - > sum0 + = value - ( rice - > sum0 > > 4 ) ;
if ( rice - > k0 > 0 & & rice - > sum0 < shift_16 [ rice - > k0 ] )
rice - > k0 - - ;
else if ( rice - > sum0 > shift_16 [ rice - > k0 + 1 ] )
rice - > k0 + + ;
}
// extract coded value
// extract coded value
# define UNFOLD(x) (((x)&1) ? (++(x)>>1) : (-(x)>>1))
* p = UNFOLD ( value ) ;
* p = UNFOLD ( value ) ;
// run hybrid filter
ttafilter_process ( filter , p , 0 ) ;
// run hybrid filter
ttafilter_process ( filter , p , 0 ) ;
// fixed order prediction
// fixed order prediction
# define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
switch ( s - > bps ) {
case 1 : * p + = PRED ( * predictor , 4 ) ; break ;
case 2 :
case 3 : * p + = PRED ( * predictor , 5 ) ; break ;
case 4 : * p + = * predictor ; break ;
}
* predictor = * p ;
/*if ((get_bits_count(&s->gb)+7)/8 > buf_size)
{
av_log ( NULL , AV_LOG_INFO , " overread!! \n " ) ;
break ;
} */
// flip channels
if ( cur_chan < ( s - > channels - 1 ) )
cur_chan + + ;
else {
// decorrelate in case of stereo integer
if ( ! s - > is_float & & ( s - > channels > 1 ) ) {
int32_t * r = p - 1 ;
for ( * p + = * r / 2 ; r > p - s - > channels ; r - - )
* r = * ( r + 1 ) - * r ;
}
cur_chan = 0 ;
switch ( s - > bps ) {
case 1 : * p + = PRED ( * predictor , 4 ) ; break ;
case 2 :
case 3 : * p + = PRED ( * predictor , 5 ) ; break ;
case 4 : * p + = * predictor ; break ;
}
* predictor = * p ;
// flip channels
if ( cur_chan < ( s - > channels - 1 ) )
cur_chan + + ;
else {
// decorrelate in case of stereo integer
if ( s - > channels > 1 ) {
int32_t * r = p - 1 ;
for ( * p + = * r / 2 ; r > p - s - > channels ; r - - )
* r = * ( r + 1 ) - * r ;
}
cur_chan = 0 ;
}
}
if ( get_bits_left ( & s - > gb ) < 32 )
return - 1 ;
skip_bits ( & s - > gb , 32 ) ; // frame crc
if ( get_bits_left ( & s - > gb ) < 32 )
return - 1 ;
skip_bits ( & s - > gb , 32 ) ; // frame crc
// convert to output buffer
switch ( s - > bps ) {
@ -415,32 +412,29 @@ static int tta_decode_frame(AVCodecContext *avctx,
uint8_t * samples = data ;
for ( p = s - > decode_buffer ; p < s - > decode_buffer + ( framelen * s - > channels ) ; p + + )
* samples + + = * p + 0x80 ;
* data_size = samples - ( uint8_t * ) data ;
break ;
}
case 2 : {
uint16_t * samples = data ;
for ( p = s - > decode_buffer ; p < s - > decode_buffer + ( framelen * s - > channels ) ; p + + ) {
// *samples++ = (unsigned char)*p;
// *samples++ = (unsigned char)(*p >> 8);
for ( p = s - > decode_buffer ; p < s - > decode_buffer + ( framelen * s - > channels ) ; p + + )
* samples + + = * p ;
}
* data_size = ( uint8_t * ) samples - ( uint8_t * ) data ;
break ;
}
case 3 : {
// shift samples for 24-bit sample format
int32_t * samples = data ;
for ( p = s - > decode_buffer ; p < s - > decode_buffer + ( framelen * s - > channels ) ; p + + )
* samples + + = AV_RN32 ( p ) < < 8 ;
* data_size = ( uint8_t * ) samples - ( uint8_t * ) data ;
* samples + + < < = 8 ;
// reset decode buffer
s - > decode_buffer = NULL ;
break ;
}
default :
av_log ( s - > avctx , AV_LOG_ERROR , " Error, only 16bit samples supported! \n " ) ;
}
}
// return get_bits_count(&s->gb)+7)/8;
* data_size = out_size ;
return buf_size ;
}