@ -19,7 +19,9 @@
# include "avcodec.h"
# include "avcodec.h"
# include "common.h"
# include "common.h"
# include "dsputil.h"
# include "dsputil.h"
# include "cabac.h"
# include "rangecoder.h"
# define MID_STATE 128
# include "mpegvideo.h"
# include "mpegvideo.h"
@ -392,7 +394,7 @@ typedef struct SnowContext{
// MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to make the motion estimation eventually independant of MpegEncContext, so this will be removed then (FIXME/XXX)
// MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to make the motion estimation eventually independant of MpegEncContext, so this will be removed then (FIXME/XXX)
AVCodecContext * avctx ;
AVCodecContext * avctx ;
CABACContext c ;
RangeCoder c ;
DSPContext dsp ;
DSPContext dsp ;
AVFrame input_picture ;
AVFrame input_picture ;
AVFrame current_picture ;
AVFrame current_picture ;
@ -443,7 +445,7 @@ static inline int mirror(int v, int m){
else return v ;
else return v ;
}
}
static inline void put_symbol ( CABACContext * c , uint8_t * state , int v , int is_signed ) {
static inline void put_symbol ( RangeCoder * c , uint8_t * state , int v , int is_signed ) {
int i ;
int i ;
if ( v ) {
if ( v ) {
@ -451,82 +453,82 @@ static inline void put_symbol(CABACContext *c, uint8_t *state, int v, int is_sig
const int e = av_log2 ( a ) ;
const int e = av_log2 ( a ) ;
# if 1
# if 1
const int el = FFMIN ( e , 10 ) ;
const int el = FFMIN ( e , 10 ) ;
put_cab ac ( c , state + 0 , 0 ) ;
put_r ac ( c , state + 0 , 0 ) ;
for ( i = 0 ; i < el ; i + + ) {
for ( i = 0 ; i < el ; i + + ) {
put_cab ac ( c , state + 1 + i , 1 ) ; //1..10
put_r ac ( c , state + 1 + i , 1 ) ; //1..10
}
}
for ( ; i < e ; i + + ) {
for ( ; i < e ; i + + ) {
put_cab ac ( c , state + 1 + 9 , 1 ) ; //1..10
put_r ac ( c , state + 1 + 9 , 1 ) ; //1..10
}
}
put_cab ac ( c , state + 1 + FFMIN ( i , 9 ) , 0 ) ;
put_r ac ( c , state + 1 + FFMIN ( i , 9 ) , 0 ) ;
for ( i = e - 1 ; i > = el ; i - - ) {
for ( i = e - 1 ; i > = el ; i - - ) {
put_cab ac ( c , state + 22 + 9 , ( a > > i ) & 1 ) ; //22..31
put_r ac ( c , state + 22 + 9 , ( a > > i ) & 1 ) ; //22..31
}
}
for ( ; i > = 0 ; i - - ) {
for ( ; i > = 0 ; i - - ) {
put_cab ac ( c , state + 22 + i , ( a > > i ) & 1 ) ; //22..31
put_r ac ( c , state + 22 + i , ( a > > i ) & 1 ) ; //22..31
}
}
if ( is_signed )
if ( is_signed )
put_cab ac ( c , state + 11 + el , v < 0 ) ; //11..21
put_r ac ( c , state + 11 + el , v < 0 ) ; //11..21
# else
# else
put_cab ac ( c , state + 0 , 0 ) ;
put_r ac ( c , state + 0 , 0 ) ;
if ( e < = 9 ) {
if ( e < = 9 ) {
for ( i = 0 ; i < e ; i + + ) {
for ( i = 0 ; i < e ; i + + ) {
put_cab ac ( c , state + 1 + i , 1 ) ; //1..10
put_r ac ( c , state + 1 + i , 1 ) ; //1..10
}
}
put_cab ac ( c , state + 1 + i , 0 ) ;
put_r ac ( c , state + 1 + i , 0 ) ;
for ( i = e - 1 ; i > = 0 ; i - - ) {
for ( i = e - 1 ; i > = 0 ; i - - ) {
put_cab ac ( c , state + 22 + i , ( a > > i ) & 1 ) ; //22..31
put_r ac ( c , state + 22 + i , ( a > > i ) & 1 ) ; //22..31
}
}
if ( is_signed )
if ( is_signed )
put_cab ac ( c , state + 11 + e , v < 0 ) ; //11..21
put_r ac ( c , state + 11 + e , v < 0 ) ; //11..21
} else {
} else {
for ( i = 0 ; i < e ; i + + ) {
for ( i = 0 ; i < e ; i + + ) {
put_cab ac ( c , state + 1 + FFMIN ( i , 9 ) , 1 ) ; //1..10
put_r ac ( c , state + 1 + FFMIN ( i , 9 ) , 1 ) ; //1..10
}
}
put_cab ac ( c , state + 1 + FFMIN ( i , 9 ) , 0 ) ;
put_r ac ( c , state + 1 + FFMIN ( i , 9 ) , 0 ) ;
for ( i = e - 1 ; i > = 0 ; i - - ) {
for ( i = e - 1 ; i > = 0 ; i - - ) {
put_cab ac ( c , state + 22 + FFMIN ( i , 9 ) , ( a > > i ) & 1 ) ; //22..31
put_r ac ( c , state + 22 + FFMIN ( i , 9 ) , ( a > > i ) & 1 ) ; //22..31
}
}
if ( is_signed )
if ( is_signed )
put_cab ac ( c , state + 11 + FFMIN ( e , 10 ) , v < 0 ) ; //11..21
put_r ac ( c , state + 11 + FFMIN ( e , 10 ) , v < 0 ) ; //11..21
}
}
# endif
# endif
} else {
} else {
put_cab ac ( c , state + 0 , 1 ) ;
put_r ac ( c , state + 0 , 1 ) ;
}
}
}
}
static inline int get_symbol ( CABACContext * c , uint8_t * state , int is_signed ) {
static inline int get_symbol ( RangeCoder * c , uint8_t * state , int is_signed ) {
if ( get_cab ac ( c , state + 0 ) )
if ( get_r ac ( c , state + 0 ) )
return 0 ;
return 0 ;
else {
else {
int i , e , a ;
int i , e , a ;
e = 0 ;
e = 0 ;
while ( get_cab ac ( c , state + 1 + FFMIN ( e , 9 ) ) ) { //1..10
while ( get_r ac ( c , state + 1 + FFMIN ( e , 9 ) ) ) { //1..10
e + + ;
e + + ;
}
}
a = 1 ;
a = 1 ;
for ( i = e - 1 ; i > = 0 ; i - - ) {
for ( i = e - 1 ; i > = 0 ; i - - ) {
a + = a + get_cab ac ( c , state + 22 + FFMIN ( i , 9 ) ) ; //22..31
a + = a + get_r ac ( c , state + 22 + FFMIN ( i , 9 ) ) ; //22..31
}
}
if ( is_signed & & get_cab ac ( c , state + 11 + FFMIN ( e , 10 ) ) ) //11..21
if ( is_signed & & get_r ac ( c , state + 11 + FFMIN ( e , 10 ) ) ) //11..21
return - a ;
return - a ;
else
else
return a ;
return a ;
}
}
}
}
static inline void put_symbol2 ( CABACContext * c , uint8_t * state , int v , int log2 ) {
static inline void put_symbol2 ( RangeCoder * c , uint8_t * state , int v , int log2 ) {
int i ;
int i ;
int r = log2 > = 0 ? 1 < < log2 : 1 ;
int r = log2 > = 0 ? 1 < < log2 : 1 ;
@ -534,33 +536,33 @@ static inline void put_symbol2(CABACContext *c, uint8_t *state, int v, int log2)
assert ( log2 > = - 4 ) ;
assert ( log2 > = - 4 ) ;
while ( v > = r ) {
while ( v > = r ) {
put_cab ac ( c , state + 4 + log2 , 1 ) ;
put_r ac ( c , state + 4 + log2 , 1 ) ;
v - = r ;
v - = r ;
log2 + + ;
log2 + + ;
if ( log2 > 0 ) r + = r ;
if ( log2 > 0 ) r + = r ;
}
}
put_cab ac ( c , state + 4 + log2 , 0 ) ;
put_r ac ( c , state + 4 + log2 , 0 ) ;
for ( i = log2 - 1 ; i > = 0 ; i - - ) {
for ( i = log2 - 1 ; i > = 0 ; i - - ) {
put_cab ac ( c , state + 31 - i , ( v > > i ) & 1 ) ;
put_r ac ( c , state + 31 - i , ( v > > i ) & 1 ) ;
}
}
}
}
static inline int get_symbol2 ( CABACContext * c , uint8_t * state , int log2 ) {
static inline int get_symbol2 ( RangeCoder * c , uint8_t * state , int log2 ) {
int i ;
int i ;
int r = log2 > = 0 ? 1 < < log2 : 1 ;
int r = log2 > = 0 ? 1 < < log2 : 1 ;
int v = 0 ;
int v = 0 ;
assert ( log2 > = - 4 ) ;
assert ( log2 > = - 4 ) ;
while ( get_cab ac ( c , state + 4 + log2 ) ) {
while ( get_r ac ( c , state + 4 + log2 ) ) {
v + = r ;
v + = r ;
log2 + + ;
log2 + + ;
if ( log2 > 0 ) r + = r ;
if ( log2 > 0 ) r + = r ;
}
}
for ( i = log2 - 1 ; i > = 0 ; i - - ) {
for ( i = log2 - 1 ; i > = 0 ; i - - ) {
v + = get_cab ac ( c , state + 31 - i ) < < i ;
v + = get_r ac ( c , state + 31 - i ) < < i ;
}
}
return v ;
return v ;
@ -1371,7 +1373,7 @@ static void encode_subband_c0run(SnowContext *s, SubBand *b, DWTELEM *src, DWTEL
if ( /*ll|*/ l | lt | t | rt | p ) {
if ( /*ll|*/ l | lt | t | rt | p ) {
int context = av_log2 ( /*ABS(ll) + */ 3 * ABS ( l ) + ABS ( lt ) + 2 * ABS ( t ) + ABS ( rt ) + ABS ( p ) ) ;
int context = av_log2 ( /*ABS(ll) + */ 3 * ABS ( l ) + ABS ( lt ) + 2 * ABS ( t ) + ABS ( rt ) + ABS ( p ) ) ;
put_cab ac ( & s - > c , & b - > state [ 0 ] [ context ] , ! ! v ) ;
put_r ac ( & s - > c , & b - > state [ 0 ] [ context ] , ! ! v ) ;
} else {
} else {
if ( ! run ) {
if ( ! run ) {
run = runs [ run_index + + ] ;
run = runs [ run_index + + ] ;
@ -1387,7 +1389,7 @@ static void encode_subband_c0run(SnowContext *s, SubBand *b, DWTELEM *src, DWTEL
int context = av_log2 ( /*ABS(ll) + */ 3 * ABS ( l ) + ABS ( lt ) + 2 * ABS ( t ) + ABS ( rt ) + ABS ( p ) ) ;
int context = av_log2 ( /*ABS(ll) + */ 3 * ABS ( l ) + ABS ( lt ) + 2 * ABS ( t ) + ABS ( rt ) + ABS ( p ) ) ;
put_symbol2 ( & s - > c , b - > state [ context + 2 ] , ABS ( v ) - 1 , context - 4 ) ;
put_symbol2 ( & s - > c , b - > state [ context + 2 ] , ABS ( v ) - 1 , context - 4 ) ;
put_cab ac ( & s - > c , & b - > state [ 0 ] [ 16 + 1 + 3 + quant3b [ l & 0xFF ] + 3 * quant3b [ t & 0xFF ] ] , v < 0 ) ;
put_r ac ( & s - > c , & b - > state [ 0 ] [ 16 + 1 + 3 + quant3b [ l & 0xFF ] + 3 * quant3b [ t & 0xFF ] ] , v < 0 ) ;
}
}
}
}
}
}
@ -1460,7 +1462,7 @@ static inline void decode_subband(SnowContext *s, SubBand *b, DWTELEM *src, DWTE
if ( /*ll|*/ l | lt | t | rt | p ) {
if ( /*ll|*/ l | lt | t | rt | p ) {
int context = av_log2 ( /*ABS(ll) + */ 3 * ABS ( l ) + ABS ( lt ) + 2 * ABS ( t ) + ABS ( rt ) + ABS ( p ) ) ;
int context = av_log2 ( /*ABS(ll) + */ 3 * ABS ( l ) + ABS ( lt ) + 2 * ABS ( t ) + ABS ( rt ) + ABS ( p ) ) ;
v = get_cab ac ( & s - > c , & b - > state [ 0 ] [ context ] ) ;
v = get_r ac ( & s - > c , & b - > state [ 0 ] [ context ] ) ;
} else {
} else {
if ( ! run ) {
if ( ! run ) {
run = get_symbol2 ( & s - > c , b - > state [ 1 ] , 3 ) ;
run = get_symbol2 ( & s - > c , b - > state [ 1 ] , 3 ) ;
@ -1482,7 +1484,7 @@ static inline void decode_subband(SnowContext *s, SubBand *b, DWTELEM *src, DWTE
if ( v ) {
if ( v ) {
int context = av_log2 ( /*ABS(ll) + */ 3 * ABS ( l ) + ABS ( lt ) + 2 * ABS ( t ) + ABS ( rt ) + ABS ( p ) ) ;
int context = av_log2 ( /*ABS(ll) + */ 3 * ABS ( l ) + ABS ( lt ) + 2 * ABS ( t ) + ABS ( rt ) + ABS ( p ) ) ;
v = get_symbol2 ( & s - > c , b - > state [ context + 2 ] , context - 4 ) + 1 ;
v = get_symbol2 ( & s - > c , b - > state [ context + 2 ] , context - 4 ) + 1 ;
if ( get_cab ac ( & s - > c , & b - > state [ 0 ] [ 16 + 1 + 3 + quant3b [ l & 0xFF ] + 3 * quant3b [ t & 0xFF ] ] ) ) {
if ( get_r ac ( & s - > c , & b - > state [ 0 ] [ 16 + 1 + 3 + quant3b [ l & 0xFF ] + 3 * quant3b [ t & 0xFF ] ] ) ) {
src [ x + y * stride ] = - ( ( v * qmul + qadd ) > > ( QEXPSHIFT ) ) ;
src [ x + y * stride ] = - ( ( v * qmul + qadd ) > > ( QEXPSHIFT ) ) ;
v = - v ;
v = - v ;
} else {
} else {
@ -1522,12 +1524,12 @@ static void reset_contexts(SnowContext *s){
for ( plane_index = 0 ; plane_index < 3 ; plane_index + + ) {
for ( plane_index = 0 ; plane_index < 3 ; plane_index + + ) {
for ( level = 0 ; level < s - > spatial_decomposition_count ; level + + ) {
for ( level = 0 ; level < s - > spatial_decomposition_count ; level + + ) {
for ( orientation = level ? 1 : 0 ; orientation < 4 ; orientation + + ) {
for ( orientation = level ? 1 : 0 ; orientation < 4 ; orientation + + ) {
memset ( s - > plane [ plane_index ] . band [ level ] [ orientation ] . state , 0 , sizeof ( s - > plane [ plane_index ] . band [ level ] [ orientation ] . state ) ) ;
memset ( s - > plane [ plane_index ] . band [ level ] [ orientation ] . state , MID_STATE , sizeof ( s - > plane [ plane_index ] . band [ level ] [ orientation ] . state ) ) ;
}
}
}
}
}
}
memset ( s - > header_state , 0 , sizeof ( s - > header_state ) ) ;
memset ( s - > header_state , MID_STATE , sizeof ( s - > header_state ) ) ;
memset ( s - > block_state , 0 , sizeof ( s - > block_state ) ) ;
memset ( s - > block_state , MID_STATE , sizeof ( s - > block_state ) ) ;
}
}
static int alloc_blocks ( SnowContext * s ) {
static int alloc_blocks ( SnowContext * s ) {
@ -1541,10 +1543,12 @@ static int alloc_blocks(SnowContext *s){
return 0 ;
return 0 ;
}
}
static inline void copy_cabac_state ( CABACContext * d , CABACContext * s ) {
static inline void copy_rac_state ( RangeCoder * d , RangeCoder * s ) {
PutBitContext bak = d - > pb ;
uint8_t * bytestream = d - > bytestream ;
uint8_t * bytestream_start = d - > bytestream_start ;
* d = * s ;
* d = * s ;
d - > pb = bak ;
d - > bytestream = bytestream ;
d - > bytestream_start = bytestream_start ;
}
}
//near copy & paste from dsputil, FIXME
//near copy & paste from dsputil, FIXME
@ -1630,8 +1634,9 @@ static int encode_q_branch(SnowContext *s, int level, int x, int y){
uint8_t i_buffer [ 1024 ] ;
uint8_t i_buffer [ 1024 ] ;
uint8_t p_state [ sizeof ( s - > block_state ) ] ;
uint8_t p_state [ sizeof ( s - > block_state ) ] ;
uint8_t i_state [ sizeof ( s - > block_state ) ] ;
uint8_t i_state [ sizeof ( s - > block_state ) ] ;
CABACContext pc , ic ;
RangeCoder pc , ic ;
PutBitContext pbbak = s - > c . pb ;
uint8_t * pbbak = s - > c . bytestream ;
uint8_t * pbbak_start = s - > c . bytestream_start ;
int score , score2 , iscore , i_len , p_len , block_s , sum ;
int score , score2 , iscore , i_len , p_len , block_s , sum ;
const int w = s - > b_width < < s - > block_max_depth ;
const int w = s - > b_width < < s - > block_max_depth ;
const int h = s - > b_height < < s - > block_max_depth ;
const int h = s - > b_height < < s - > block_max_depth ;
@ -1761,16 +1766,20 @@ static int encode_q_branch(SnowContext *s, int level, int x, int y){
// subpel search
// subpel search
pc = s - > c ;
pc = s - > c ;
init_put_bits ( & pc . pb , p_buffer , sizeof ( p_buffer ) ) ;
pc . bytestream_start =
pc . bytestream = p_buffer ; //FIXME end/start? and at the other stoo
memcpy ( p_state , s - > block_state , sizeof ( s - > block_state ) ) ;
memcpy ( p_state , s - > block_state , sizeof ( s - > block_state ) ) ;
if ( level ! = s - > block_max_depth )
if ( level ! = s - > block_max_depth )
put_cab ac ( & pc , & p_state [ 4 + s_context ] , 1 ) ;
put_r ac ( & pc , & p_state [ 4 + s_context ] , 1 ) ;
put_cab ac ( & pc , & p_state [ 1 + left - > type + top - > type ] , 0 ) ;
put_r ac ( & pc , & p_state [ 1 + left - > type + top - > type ] , 0 ) ;
put_symbol ( & pc , & p_state [ 128 + 32 * mx_context ] , mx - pmx , 1 ) ;
put_symbol ( & pc , & p_state [ 128 + 32 * mx_context ] , mx - pmx , 1 ) ;
put_symbol ( & pc , & p_state [ 128 + 32 * my_context ] , my - pmy , 1 ) ;
put_symbol ( & pc , & p_state [ 128 + 32 * my_context ] , my - pmy , 1 ) ;
p_len = put_bits_count ( & pc . pb ) ;
p_len = pc . bytestream - pc . bytestream_start ;
score + = ( s - > lambda2 * ( p_len + pc . outstanding_count - s - > c . outstanding_count ) ) > > FF_LAMBDA_SHIFT ;
score + = ( s - > lambda2 * ( p_len * 8
+ ( pc . outstanding_count - s - > c . outstanding_count ) * 8
+ ( - av_log2 ( pc . range ) + av_log2 ( s - > c . range ) )
) ) > > FF_LAMBDA_SHIFT ;
block_s = block_w * block_w ;
block_s = block_w * block_w ;
sum = pix_sum ( & current_mb [ 0 ] [ 0 ] , stride , block_w ) ;
sum = pix_sum ( & current_mb [ 0 ] [ 0 ] , stride , block_w ) ;
@ -1786,16 +1795,20 @@ static int encode_q_branch(SnowContext *s, int level, int x, int y){
// iscore += pix_norm1(¤t_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s;
// iscore += pix_norm1(¤t_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s;
ic = s - > c ;
ic = s - > c ;
init_put_bits ( & ic . pb , i_buffer , sizeof ( i_buffer ) ) ;
ic . bytestream_start =
ic . bytestream = i_buffer ; //FIXME end/start? and at the other stoo
memcpy ( i_state , s - > block_state , sizeof ( s - > block_state ) ) ;
memcpy ( i_state , s - > block_state , sizeof ( s - > block_state ) ) ;
if ( level ! = s - > block_max_depth )
if ( level ! = s - > block_max_depth )
put_cab ac ( & ic , & i_state [ 4 + s_context ] , 1 ) ;
put_r ac ( & ic , & i_state [ 4 + s_context ] , 1 ) ;
put_cab ac ( & ic , & i_state [ 1 + left - > type + top - > type ] , 1 ) ;
put_r ac ( & ic , & i_state [ 1 + left - > type + top - > type ] , 1 ) ;
put_symbol ( & ic , & i_state [ 32 ] , l - pl , 1 ) ;
put_symbol ( & ic , & i_state [ 32 ] , l - pl , 1 ) ;
put_symbol ( & ic , & i_state [ 64 ] , cb - pcb , 1 ) ;
put_symbol ( & ic , & i_state [ 64 ] , cb - pcb , 1 ) ;
put_symbol ( & ic , & i_state [ 96 ] , cr - pcr , 1 ) ;
put_symbol ( & ic , & i_state [ 96 ] , cr - pcr , 1 ) ;
i_len = put_bits_count ( & ic . pb ) ;
i_len = ic . bytestream - ic . bytestream_start ;
iscore + = ( s - > lambda2 * ( i_len + ic . outstanding_count - s - > c . outstanding_count ) ) > > FF_LAMBDA_SHIFT ;
iscore + = ( s - > lambda2 * ( i_len * 8
+ ( ic . outstanding_count - s - > c . outstanding_count ) * 8
+ ( - av_log2 ( ic . range ) + av_log2 ( s - > c . range ) )
) ) > > FF_LAMBDA_SHIFT ;
// assert(score==256*256*256*64-1);
// assert(score==256*256*256*64-1);
assert ( iscore < 255 * 255 * 256 + s - > lambda2 * 10 ) ;
assert ( iscore < 255 * 255 * 256 + s - > lambda2 * 10 ) ;
@ -1813,7 +1826,7 @@ static int encode_q_branch(SnowContext *s, int level, int x, int y){
}
}
if ( level ! = s - > block_max_depth ) {
if ( level ! = s - > block_max_depth ) {
put_cab ac ( & s - > c , & s - > block_state [ 4 + s_context ] , 0 ) ;
put_r ac ( & s - > c , & s - > block_state [ 4 + s_context ] , 0 ) ;
score2 = encode_q_branch ( s , level + 1 , 2 * x + 0 , 2 * y + 0 ) ;
score2 = encode_q_branch ( s , level + 1 , 2 * x + 0 , 2 * y + 0 ) ;
score2 + = encode_q_branch ( s , level + 1 , 2 * x + 1 , 2 * y + 0 ) ;
score2 + = encode_q_branch ( s , level + 1 , 2 * x + 1 , 2 * y + 0 ) ;
score2 + = encode_q_branch ( s , level + 1 , 2 * x + 0 , 2 * y + 1 ) ;
score2 + = encode_q_branch ( s , level + 1 , 2 * x + 0 , 2 * y + 1 ) ;
@ -1825,18 +1838,18 @@ static int encode_q_branch(SnowContext *s, int level, int x, int y){
}
}
if ( iscore < score ) {
if ( iscore < score ) {
flush_put_bits ( & ic . pb ) ;
memcpy ( pbbak , i_buffer , i_len ) ;
ff_copy_bits ( & pbbak , i_buffer , i_len ) ;
s - > c = ic ;
s - > c = ic ;
s - > c . pb = pbbak ;
s - > c . bytestream_start = pbbak_start ;
s - > c . bytestream = pbbak + i_len ;
set_blocks ( s , level , x , y , l , cb , cr , pmx , pmy , BLOCK_INTRA ) ;
set_blocks ( s , level , x , y , l , cb , cr , pmx , pmy , BLOCK_INTRA ) ;
memcpy ( s - > block_state , i_state , sizeof ( s - > block_state ) ) ;
memcpy ( s - > block_state , i_state , sizeof ( s - > block_state ) ) ;
return iscore ;
return iscore ;
} else {
} else {
flush_put_bits ( & pc . pb ) ;
memcpy ( pbbak , p_buffer , p_len ) ;
ff_copy_bits ( & pbbak , p_buffer , p_len ) ;
s - > c = pc ;
s - > c = pc ;
s - > c . pb = pbbak ;
s - > c . bytestream_start = pbbak_start ;
s - > c . bytestream = pbbak + p_len ;
set_blocks ( s , level , x , y , pl , pcb , pcr , mx , my , 0 ) ;
set_blocks ( s , level , x , y , pl , pcb , pcr , mx , my , 0 ) ;
memcpy ( s - > block_state , p_state , sizeof ( s - > block_state ) ) ;
memcpy ( s - > block_state , p_state , sizeof ( s - > block_state ) ) ;
return score ;
return score ;
@ -1866,7 +1879,7 @@ static void decode_q_branch(SnowContext *s, int level, int x, int y){
return ;
return ;
}
}
if ( level = = s - > block_max_depth | | get_cab ac ( & s - > c , & s - > block_state [ 4 + s_context ] ) ) {
if ( level = = s - > block_max_depth | | get_r ac ( & s - > c , & s - > block_state [ 4 + s_context ] ) ) {
int type ;
int type ;
int l = left - > color [ 0 ] ;
int l = left - > color [ 0 ] ;
int cb = left - > color [ 1 ] ;
int cb = left - > color [ 1 ] ;
@ -1876,7 +1889,7 @@ static void decode_q_branch(SnowContext *s, int level, int x, int y){
int mx_context = av_log2 ( 2 * ABS ( left - > mx - top - > mx ) ) + 0 * av_log2 ( 2 * ABS ( tr - > mx - top - > mx ) ) ;
int mx_context = av_log2 ( 2 * ABS ( left - > mx - top - > mx ) ) + 0 * av_log2 ( 2 * ABS ( tr - > mx - top - > mx ) ) ;
int my_context = av_log2 ( 2 * ABS ( left - > my - top - > my ) ) + 0 * av_log2 ( 2 * ABS ( tr - > my - top - > my ) ) ;
int my_context = av_log2 ( 2 * ABS ( left - > my - top - > my ) ) + 0 * av_log2 ( 2 * ABS ( tr - > my - top - > my ) ) ;
type = get_cab ac ( & s - > c , & s - > block_state [ 1 + left - > type + top - > type ] ) ? BLOCK_INTRA : 0 ;
type = get_r ac ( & s - > c , & s - > block_state [ 1 + left - > type + top - > type ] ) ? BLOCK_INTRA : 0 ;
if ( type ) {
if ( type ) {
l + = get_symbol ( & s - > c , & s - > block_state [ 32 ] , 1 ) ;
l + = get_symbol ( & s - > c , & s - > block_state [ 32 ] , 1 ) ;
@ -2371,22 +2384,24 @@ static void correlate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int
static void encode_header ( SnowContext * s ) {
static void encode_header ( SnowContext * s ) {
int plane_index , level , orientation ;
int plane_index , level , orientation ;
uint8_t kstate [ 32 ] = { 0 } ;
uint8_t kstate [ 32 ] ;
memset ( kstate , MID_STATE , sizeof ( kstate ) ) ;
put_cabac ( & s - > c , kstate , s - > keyframe ) ;
put_r ac ( & s - > c , kstate , s - > keyframe ) ;
if ( s - > keyframe | | s - > always_reset )
if ( s - > keyframe | | s - > always_reset )
reset_contexts ( s ) ;
reset_contexts ( s ) ;
if ( s - > keyframe ) {
if ( s - > keyframe ) {
put_symbol ( & s - > c , s - > header_state , s - > version , 0 ) ;
put_symbol ( & s - > c , s - > header_state , s - > version , 0 ) ;
put_cab ac ( & s - > c , s - > header_state , s - > always_reset ) ;
put_r ac ( & s - > c , s - > header_state , s - > always_reset ) ;
put_symbol ( & s - > c , s - > header_state , s - > temporal_decomposition_type , 0 ) ;
put_symbol ( & s - > c , s - > header_state , s - > temporal_decomposition_type , 0 ) ;
put_symbol ( & s - > c , s - > header_state , s - > temporal_decomposition_count , 0 ) ;
put_symbol ( & s - > c , s - > header_state , s - > temporal_decomposition_count , 0 ) ;
put_symbol ( & s - > c , s - > header_state , s - > spatial_decomposition_count , 0 ) ;
put_symbol ( & s - > c , s - > header_state , s - > spatial_decomposition_count , 0 ) ;
put_symbol ( & s - > c , s - > header_state , s - > colorspace_type , 0 ) ;
put_symbol ( & s - > c , s - > header_state , s - > colorspace_type , 0 ) ;
put_symbol ( & s - > c , s - > header_state , s - > chroma_h_shift , 0 ) ;
put_symbol ( & s - > c , s - > header_state , s - > chroma_h_shift , 0 ) ;
put_symbol ( & s - > c , s - > header_state , s - > chroma_v_shift , 0 ) ;
put_symbol ( & s - > c , s - > header_state , s - > chroma_v_shift , 0 ) ;
put_cab ac ( & s - > c , s - > header_state , s - > spatial_scalability ) ;
put_r ac ( & s - > c , s - > header_state , s - > spatial_scalability ) ;
// put_cab ac(&s->c, s->header_state, s->rate_scalability);
// put_r ac(&s->c, s->header_state, s->rate_scalability);
for ( plane_index = 0 ; plane_index < 2 ; plane_index + + ) {
for ( plane_index = 0 ; plane_index < 2 ; plane_index + + ) {
for ( level = 0 ; level < s - > spatial_decomposition_count ; level + + ) {
for ( level = 0 ; level < s - > spatial_decomposition_count ; level + + ) {
@ -2406,9 +2421,11 @@ static void encode_header(SnowContext *s){
static int decode_header ( SnowContext * s ) {
static int decode_header ( SnowContext * s ) {
int plane_index , level , orientation ;
int plane_index , level , orientation ;
uint8_t kstate [ 32 ] = { 0 } ;
uint8_t kstate [ 32 ] ;
memset ( kstate , MID_STATE , sizeof ( kstate ) ) ;
s - > keyframe = get_cabac ( & s - > c , kstate ) ;
s - > keyframe = get_r ac ( & s - > c , kstate ) ;
if ( s - > keyframe | | s - > always_reset )
if ( s - > keyframe | | s - > always_reset )
reset_contexts ( s ) ;
reset_contexts ( s ) ;
if ( s - > keyframe ) {
if ( s - > keyframe ) {
@ -2417,15 +2434,15 @@ static int decode_header(SnowContext *s){
av_log ( s - > avctx , AV_LOG_ERROR , " version %d not supported " , s - > version ) ;
av_log ( s - > avctx , AV_LOG_ERROR , " version %d not supported " , s - > version ) ;
return - 1 ;
return - 1 ;
}
}
s - > always_reset = get_cab ac ( & s - > c , s - > header_state ) ;
s - > always_reset = get_r ac ( & s - > c , s - > header_state ) ;
s - > temporal_decomposition_type = get_symbol ( & s - > c , s - > header_state , 0 ) ;
s - > temporal_decomposition_type = get_symbol ( & s - > c , s - > header_state , 0 ) ;
s - > temporal_decomposition_count = get_symbol ( & s - > c , s - > header_state , 0 ) ;
s - > temporal_decomposition_count = get_symbol ( & s - > c , s - > header_state , 0 ) ;
s - > spatial_decomposition_count = get_symbol ( & s - > c , s - > header_state , 0 ) ;
s - > spatial_decomposition_count = get_symbol ( & s - > c , s - > header_state , 0 ) ;
s - > colorspace_type = get_symbol ( & s - > c , s - > header_state , 0 ) ;
s - > colorspace_type = get_symbol ( & s - > c , s - > header_state , 0 ) ;
s - > chroma_h_shift = get_symbol ( & s - > c , s - > header_state , 0 ) ;
s - > chroma_h_shift = get_symbol ( & s - > c , s - > header_state , 0 ) ;
s - > chroma_v_shift = get_symbol ( & s - > c , s - > header_state , 0 ) ;
s - > chroma_v_shift = get_symbol ( & s - > c , s - > header_state , 0 ) ;
s - > spatial_scalability = get_cab ac ( & s - > c , s - > header_state ) ;
s - > spatial_scalability = get_r ac ( & s - > c , s - > header_state ) ;
// s->rate_scalability= get_cab ac(&s->c, s->header_state);
// s->rate_scalability= get_r ac(&s->c, s->header_state);
for ( plane_index = 0 ; plane_index < 3 ; plane_index + + ) {
for ( plane_index = 0 ; plane_index < 3 ; plane_index + + ) {
for ( level = 0 ; level < s - > spatial_decomposition_count ; level + + ) {
for ( level = 0 ; level < s - > spatial_decomposition_count ; level + + ) {
@ -2666,14 +2683,14 @@ static int frame_start(SnowContext *s){
static int encode_frame ( AVCodecContext * avctx , unsigned char * buf , int buf_size , void * data ) {
static int encode_frame ( AVCodecContext * avctx , unsigned char * buf , int buf_size , void * data ) {
SnowContext * s = avctx - > priv_data ;
SnowContext * s = avctx - > priv_data ;
CABACContext * const c = & s - > c ;
RangeCoder * const c = & s - > c ;
AVFrame * pict = data ;
AVFrame * pict = data ;
const int width = s - > avctx - > width ;
const int width = s - > avctx - > width ;
const int height = s - > avctx - > height ;
const int height = s - > avctx - > height ;
int level , orientation , plane_index ;
int level , orientation , plane_index ;
ff_init_cabac _encoder ( c , buf , buf_size ) ;
ff_init_range _encoder ( c , buf , buf_size ) ;
ff_init_cab ac_states ( c , ff_h264_lps_range , ff_h264_mps_state , ff_h264_lps_state , 64 ) ;
ff_build_r ac_states ( c , 0.05 * ( 1LL < < 32 ) , 256 - 8 ) ;
s - > input_picture = * pict ;
s - > input_picture = * pict ;
@ -2759,8 +2776,8 @@ redo_frame:
if ( plane_index = = 0
if ( plane_index = = 0
& & pict - > pict_type = = P_TYPE
& & pict - > pict_type = = P_TYPE
& & s - > m . me . scene_change_score > s - > avctx - > scenechange_threshold ) {
& & s - > m . me . scene_change_score > s - > avctx - > scenechange_threshold ) {
ff_init_cabac _encoder ( c , buf , buf_size ) ;
ff_init_range _encoder ( c , buf , buf_size ) ;
ff_init_cab ac_states ( c , ff_h264_lps_range , ff_h264_mps_state , ff_h264_lps_state , 64 ) ;
ff_build_r ac_states ( c , 0.05 * ( 1LL < < 32 ) , 256 - 8 ) ;
pict - > pict_type = FF_I_TYPE ;
pict - > pict_type = FF_I_TYPE ;
s - > keyframe = 1 ;
s - > keyframe = 1 ;
reset_contexts ( s ) ;
reset_contexts ( s ) ;
@ -2831,7 +2848,7 @@ STOP_TIMER("pred-conv")}
emms_c ( ) ;
emms_c ( ) ;
return put_cab ac_terminate( c , 1 ) ;
return ff_r ac_terminate( c ) ;
}
}
static void common_end ( SnowContext * s ) {
static void common_end ( SnowContext * s ) {
@ -2877,7 +2894,7 @@ static int decode_init(AVCodecContext *avctx)
static int decode_frame ( AVCodecContext * avctx , void * data , int * data_size , uint8_t * buf , int buf_size ) {
static int decode_frame ( AVCodecContext * avctx , void * data , int * data_size , uint8_t * buf , int buf_size ) {
SnowContext * s = avctx - > priv_data ;
SnowContext * s = avctx - > priv_data ;
CABACContext * const c = & s - > c ;
RangeCoder * const c = & s - > c ;
int bytes_read ;
int bytes_read ;
AVFrame * picture = data ;
AVFrame * picture = data ;
int level , orientation , plane_index ;
int level , orientation , plane_index ;
@ -2887,8 +2904,8 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8
if ( buf_size = = 0 )
if ( buf_size = = 0 )
return 0 ;
return 0 ;
ff_init_cabac _decoder ( c , buf , buf_size ) ;
ff_init_range _decoder ( c , buf , buf_size ) ;
ff_init_cab ac_states ( c , ff_h264_lps_range , ff_h264_mps_state , ff_h264_lps_state , 64 ) ;
ff_build_r ac_states ( c , 0.05 * ( 1LL < < 32 ) , 256 - 8 ) ;
s - > current_picture . pict_type = FF_I_TYPE ; //FIXME I vs. P
s - > current_picture . pict_type = FF_I_TYPE ; //FIXME I vs. P
decode_header ( s ) ;
decode_header ( s ) ;
@ -2956,8 +2973,8 @@ else
* data_size = sizeof ( AVFrame ) ;
* data_size = sizeof ( AVFrame ) ;
bytes_read = get_cabac_terminate ( c ) ;
bytes_read = c - > bytestream - c - > bytestream_start ;
if ( bytes_read = = 0 ) av_log ( s - > avctx , AV_LOG_ERROR , " error at end of frame \n " ) ;
if ( bytes_read = = 0 ) av_log ( s - > avctx , AV_LOG_ERROR , " error at end of frame \n " ) ; //FIXME
return bytes_read ;
return bytes_read ;
}
}
@ -3032,7 +3049,7 @@ int main(){
printf ( " testing AC coder \n " ) ;
printf ( " testing AC coder \n " ) ;
memset ( s . header_state , 0 , sizeof ( s . header_state ) ) ;
memset ( s . header_state , 0 , sizeof ( s . header_state ) ) ;
ff_init_cabac _encoder ( & s . c , buffer [ 0 ] , 256 * 256 ) ;
ff_init_range _encoder ( & s . c , buffer [ 0 ] , 256 * 256 ) ;
ff_init_cabac_states ( & s . c , ff_h264_lps_range , ff_h264_mps_state , ff_h264_lps_state , 64 ) ;
ff_init_cabac_states ( & s . c , ff_h264_lps_range , ff_h264_mps_state , ff_h264_lps_state , 64 ) ;
for ( i = - 256 ; i < 256 ; i + + ) {
for ( i = - 256 ; i < 256 ; i + + ) {
@ -3040,10 +3057,10 @@ START_TIMER
put_symbol ( & s . c , s . header_state , i * i * i / 3 * ABS ( i ) , 1 ) ;
put_symbol ( & s . c , s . header_state , i * i * i / 3 * ABS ( i ) , 1 ) ;
STOP_TIMER ( " put_symbol " )
STOP_TIMER ( " put_symbol " )
}
}
put_cab ac_terminate( & s . c , 1 ) ;
ff_r ac_terminate( & s . c ) ;
memset ( s . header_state , 0 , sizeof ( s . header_state ) ) ;
memset ( s . header_state , 0 , sizeof ( s . header_state ) ) ;
ff_init_cabac _decoder ( & s . c , buffer [ 0 ] , 256 * 256 ) ;
ff_init_range _decoder ( & s . c , buffer [ 0 ] , 256 * 256 ) ;
ff_init_cabac_states ( & s . c , ff_h264_lps_range , ff_h264_mps_state , ff_h264_lps_state , 64 ) ;
ff_init_cabac_states ( & s . c , ff_h264_lps_range , ff_h264_mps_state , ff_h264_lps_state , 64 ) ;
for ( i = - 256 ; i < 256 ; i + + ) {
for ( i = - 256 ; i < 256 ; i + + ) {