comparison snow.c @ 2160:11e1425b3a66 libavcodec

cleanup
author michael
date Mon, 02 Aug 2004 18:01:03 +0000
parents 7f42295c1517
children ec7789e19e43
comparison
equal deleted inserted replaced
2159:7f42295c1517 2160:11e1425b3a66
1847 } 1847 }
1848 } 1848 }
1849 } 1849 }
1850 } 1850 }
1851 1851
1852 static void encode_subband_dzr(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
1853 const int level= b->level;
1854 const int w= b->width;
1855 const int h= b->height;
1856 int x, y;
1857
1858 if(1){
1859 int run[16]={0};
1860 int runs[16][w*h]; //FIXME do something about the size
1861 int run_index[16]={0};
1862 int positions[2][w];
1863 int distances[2][w];
1864 int dist_count=0;
1865 int i;
1866
1867 for(y=0; y<h; y++){
1868 int * pos = positions[ y&1];
1869 int *last_pos = positions[(y&1)^1];
1870 int * dist= distances[ y&1];
1871 int *last_dist= distances[(y&1)^1];
1872 int dist_index=0;
1873 int last_dist_index=0;
1874
1875 for(x=0; x<w; x++){
1876 int p=0, l=0, lt=0, t=0, rt=0;
1877 int v= src[x + y*stride];
1878
1879 if(y){
1880 t= src[x + (y-1)*stride];
1881 if(x){
1882 lt= src[x - 1 + (y-1)*stride];
1883 }
1884 if(x + 1 < w){
1885 rt= src[x + 1 + (y-1)*stride];
1886 }
1887 }
1888 if(x){
1889 l= src[x - 1 + y*stride];
1890 }
1891 if(parent){
1892 int px= x>>1;
1893 int py= y>>1;
1894 if(px<b->parent->width && py<b->parent->height)
1895 p= parent[px + py*2*stride];
1896 }
1897 if(last_dist_index < dist_count && last_pos[last_dist_index] == x){
1898 if(dist_index==0 || x - pos[dist_index-1] > dist[dist_index-1] - last_dist[last_dist_index]){
1899 pos[dist_index]= x;
1900 dist[dist_index++]= last_dist[last_dist_index];
1901 }
1902 last_dist_index++;
1903 }
1904
1905 if(!(l|lt|t|rt|p)){
1906 int cur_dist=w>>1;
1907 int run_class;
1908
1909 if(last_dist_index < dist_count)
1910 cur_dist= last_pos[last_dist_index] - x + y - last_dist[last_dist_index];
1911 if(dist_index)
1912 cur_dist= FFMIN(cur_dist, x - pos[dist_index-1] + y - dist[dist_index-1]);
1913 assert(cur_dist>=2);
1914 run_class= av_log2(cur_dist+62);
1915
1916 if(v){
1917 runs[run_class][run_index[run_class]++]= run[run_class];
1918 run[run_class]=0;
1919 }else{
1920 run[run_class]++;
1921 }
1922 }
1923 if(v){
1924 while(dist_index && x - pos[dist_index-1] <= y - dist[dist_index-1])
1925 dist_index--;
1926 pos[dist_index]= x;
1927 dist[dist_index++]= y;
1928 }
1929 }
1930 dist_count= dist_index;
1931 }
1932 for(i=0; i<12; i++){
1933 runs[i][run_index[i]++]= run[i];
1934 run_index[i]=0;
1935 run[i]=0;
1936 }
1937
1938 dist_count=0;
1939
1940 for(y=0; y<h; y++){
1941 int * pos = positions[ y&1];
1942 int *last_pos = positions[(y&1)^1];
1943 int * dist= distances[ y&1];
1944 int *last_dist= distances[(y&1)^1];
1945 int dist_index=0;
1946 int last_dist_index=0;
1947
1948 for(x=0; x<w; x++){
1949 int p=0, l=0, lt=0, t=0, rt=0;
1950 int v= src[x + y*stride];
1951
1952 if(y){
1953 t= src[x + (y-1)*stride];
1954 if(x){
1955 lt= src[x - 1 + (y-1)*stride];
1956 }
1957 if(x + 1 < w){
1958 rt= src[x + 1 + (y-1)*stride];
1959 }
1960 }
1961 if(x){
1962 l= src[x - 1 + y*stride];
1963 }
1964 if(parent){
1965 int px= x>>1;
1966 int py= y>>1;
1967 if(px<b->parent->width && py<b->parent->height)
1968 p= parent[px + py*2*stride];
1969 }
1970 if(last_dist_index < dist_count && last_pos[last_dist_index] == x){
1971 if(dist_index==0 || x - pos[dist_index-1] > dist[dist_index-1] - last_dist[last_dist_index]){
1972 pos[dist_index]= x;
1973 dist[dist_index++]= last_dist[last_dist_index];
1974 }
1975 last_dist_index++;
1976 }
1977 if(l|lt|t|rt|p){
1978 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
1979
1980 put_cabac(&s->c, &b->state[0][context], !!v);
1981 }else{
1982 int cur_dist=w>>1;
1983 int run_class;
1984
1985 if(last_dist_index < dist_count)
1986 cur_dist= last_pos[last_dist_index] - x + y - last_dist[last_dist_index];
1987 if(dist_index)
1988 cur_dist= FFMIN(cur_dist, x - pos[dist_index-1] + y - dist[dist_index-1]);
1989 assert(cur_dist>=2);
1990 assert(!dist_index || (pos[dist_index-1] >= 0 && pos[dist_index-1] <w));
1991 assert(last_dist_index >= dist_count || (last_pos[last_dist_index] >= 0 && last_pos[last_dist_index] <w));
1992 assert(!dist_index || dist[dist_index-1] <= y);
1993 assert(last_dist_index >= dist_count || last_dist[last_dist_index] < y);
1994 assert(cur_dist <= y + FFMAX(x, w-x-1));
1995 run_class= av_log2(cur_dist+62);
1996
1997 if(!run_index[run_class]){
1998 run[run_class]= runs[run_class][run_index[run_class]++];
1999 put_symbol(&s->c, b->state[run_class+1], run[run_class], 0);
2000 }
2001 if(!run[run_class]){
2002 run[run_class]= runs[run_class][run_index[run_class]++];
2003 put_symbol(&s->c, b->state[run_class+1], run[run_class], 0);
2004 assert(v);
2005 }else{
2006 run[run_class]--;
2007 assert(!v);
2008 }
2009 }
2010 if(v){
2011 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
2012
2013 put_symbol(&s->c, b->state[context + 16], ABS(v)-1, 0);
2014 put_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]], v<0);
2015
2016 while(dist_index && x - pos[dist_index-1] <= y - dist[dist_index-1])
2017 dist_index--;
2018 pos[dist_index]= x;
2019 dist[dist_index++]= y;
2020 }
2021 }
2022 dist_count= dist_index;
2023 }
2024 }
2025 }
2026
2027 static void encode_subband(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){ 1852 static void encode_subband(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
2028 // encode_subband_qtree(s, b, src, parent, stride, orientation); 1853 // encode_subband_qtree(s, b, src, parent, stride, orientation);
2029 // encode_subband_z0run(s, b, src, parent, stride, orientation); 1854 // encode_subband_z0run(s, b, src, parent, stride, orientation);
2030 encode_subband_c0run(s, b, src, parent, stride, orientation); 1855 encode_subband_c0run(s, b, src, parent, stride, orientation);
2031 // encode_subband_dzr(s, b, src, parent, stride, orientation); 1856 // encode_subband_dzr(s, b, src, parent, stride, orientation);
2122 } 1947 }
2123 } 1948 }
2124 } 1949 }
2125 return; 1950 return;
2126 #endif 1951 #endif
2127 #if 0
2128 int tree[10][w*h]; //FIXME space waste ...
2129 int treedim[10][2];
2130 int lev;
2131 const int max_level= av_log2(2*FFMAX(w,h)-1);
2132 int w2=w, h2=h;
2133 memset(tree, 0, sizeof(tree));
2134
2135 // assert(w%2==0 && h%2==0);
2136
2137 for(lev=max_level; lev>=0; lev--){
2138 treedim[lev][0]= w2;
2139 treedim[lev][1]= h2;
2140 w2= (w2+1)>>1;
2141 h2= (h2+1)>>1;
2142 }
2143
2144 for(lev=0; lev<=max_level; lev++){
2145 w2= treedim[lev][0];
2146 h2= treedim[lev][1];
2147 for(y=0; y<h2; y++){
2148 for(x=0; x<w2; x++){
2149 int l= 0, t=0;
2150 int context;
2151 if(lev && !tree[lev-1][x/2 + y/2*w])
2152 continue;
2153
2154 if(x) l= tree[lev][x - 1 + y*w];
2155 if(y) t= tree[lev][x + (y-1)*w];
2156
2157 context= lev + 8*(!!l) + 16*(!!t);
2158 tree[lev][x + y*w]= get_cabac(&s->c, &b->state[98][context]);
2159 }
2160 }
2161 }
2162 if(1){
2163 for(y=0; y<b->height; y++)
2164 memset(&src[y*stride], 0, b->width*sizeof(DWTELEM));
2165
2166 for(y=0; y<h; y++){
2167 for(x=0; x<w; x++){
2168 int v, p=0;
2169 int /*ll=0, */l=0, lt=0, t=0, rt=0;
2170
2171 if(y){
2172 t= src[x + (y-1)*stride];
2173 if(x){
2174 lt= src[x - 1 + (y-1)*stride];
2175 }
2176 if(x + 1 < w){
2177 rt= src[x + 1 + (y-1)*stride];
2178 }
2179 }
2180 if(x){
2181 l= src[x - 1 + y*stride];
2182 /*if(x > 1){
2183 if(orientation==1) ll= src[y + (x-2)*stride];
2184 else ll= src[x - 2 + y*stride];
2185 }*/
2186 }
2187 if(parent){
2188 int px= x>>1;
2189 int py= y>>1;
2190 if(px<b->parent->width && py<b->parent->height)
2191 p= parent[px + py*2*stride];
2192 }
2193 if(tree[max_level][x + y*w]){
2194 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
2195 v= get_symbol(&s->c, b->state[context + 2], 0) + 1;
2196 if(get_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]]))
2197 v= -v;
2198 src[x + y*stride]= v;
2199 }
2200 }
2201 }
2202 if(level+1 == s->spatial_decomposition_count){
2203 STOP_TIMER("decode_subband")
2204 }
2205
2206 return;
2207 }
2208 #endif
2209 if(1){ 1952 if(1){
2210 int run; 1953 int run;
2211 1954
2212 for(y=0; y<b->height; y++) 1955 for(y=0; y<b->height; y++)
2213 memset(&src[y*stride], 0, b->width*sizeof(DWTELEM)); 1956 memset(&src[y*stride], 0, b->width*sizeof(DWTELEM));
2768 w>>= s->chroma_h_shift; 2511 w>>= s->chroma_h_shift;
2769 h>>= s->chroma_v_shift; 2512 h>>= s->chroma_v_shift;
2770 } 2513 }
2771 s->plane[plane_index].width = w; 2514 s->plane[plane_index].width = w;
2772 s->plane[plane_index].height= h; 2515 s->plane[plane_index].height= h;
2773 av_log(NULL, AV_LOG_DEBUG, "%d %d\n", w, h); 2516 //av_log(NULL, AV_LOG_DEBUG, "%d %d\n", w, h);
2774 for(level=s->spatial_decomposition_count-1; level>=0; level--){ 2517 for(level=s->spatial_decomposition_count-1; level>=0; level--){
2775 for(orientation=level ? 1 : 0; orientation<4; orientation++){ 2518 for(orientation=level ? 1 : 0; orientation<4; orientation++){
2776 SubBand *b= &s->plane[plane_index].band[level][orientation]; 2519 SubBand *b= &s->plane[plane_index].band[level][orientation];
2777 2520
2778 b->buf= s->spatial_dwt_buffer; 2521 b->buf= s->spatial_dwt_buffer;
3125 int h= p->height; 2868 int h= p->height;
3126 int x, y; 2869 int x, y;
3127 int bits= put_bits_count(&s->c.pb); 2870 int bits= put_bits_count(&s->c.pb);
3128 2871
3129 //FIXME optimize 2872 //FIXME optimize
3130 #if QPRED
3131 memset(s->pred_buffer, 0, sizeof(DWTELEM)*w*h);
3132 predict_plane(s, s->pred_buffer, plane_index, 1);
3133 spatial_dwt(s, s->pred_buffer, w, h, w);
3134 for(level=0; level<s->spatial_decomposition_count; level++){
3135 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3136 SubBand *b= &p->band[level][orientation];
3137 int delta= ((int)s->pred_buffer - (int)s->spatial_dwt_buffer)/sizeof(DWTELEM);
3138
3139 quantize (s, b, b->buf + delta, b->stride, s->qbias);
3140 dequantize(s, b, b->buf + delta, b->stride);
3141 }
3142 }
3143 for(y=0; y<h; y++){
3144 for(x=0; x<w; x++){
3145 s->spatial_dwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<8;
3146 }
3147 }
3148 spatial_dwt(s, s->spatial_dwt_buffer, w, h, w);
3149 for(y=0; y<h; y++){
3150 for(x=0; x<w; x++){
3151 s->spatial_dwt_buffer[y*w + x]-= s->pred_buffer[y*w + x];
3152 }
3153 }
3154 #else
3155 if(pict->data[plane_index]) //FIXME gray hack 2873 if(pict->data[plane_index]) //FIXME gray hack
3156 for(y=0; y<h; y++){ 2874 for(y=0; y<h; y++){
3157 for(x=0; x<w; x++){ 2875 for(x=0; x<w; x++){
3158 s->spatial_dwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<8; 2876 s->spatial_dwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<8;
3159 } 2877 }
3160 } 2878 }
3161 predict_plane(s, s->spatial_dwt_buffer, plane_index, 0); 2879 predict_plane(s, s->spatial_dwt_buffer, plane_index, 0);
3162 spatial_dwt(s, s->spatial_dwt_buffer, w, h, w); 2880 spatial_dwt(s, s->spatial_dwt_buffer, w, h, w);
3163 #endif
3164 2881
3165 for(level=0; level<s->spatial_decomposition_count; level++){ 2882 for(level=0; level<s->spatial_decomposition_count; level++){
3166 for(orientation=level ? 1 : 0; orientation<4; orientation++){ 2883 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3167 SubBand *b= &p->band[level][orientation]; 2884 SubBand *b= &p->band[level][orientation];
3168 2885
3183 2900
3184 dequantize(s, b, b->buf, b->stride); 2901 dequantize(s, b, b->buf, b->stride);
3185 } 2902 }
3186 } 2903 }
3187 2904
3188 #if QPRED
3189 for(y=0; y<h; y++){
3190 for(x=0; x<w; x++){
3191 s->spatial_dwt_buffer[y*w + x]+= s->pred_buffer[y*w + x];
3192 }
3193 }
3194 spatial_idwt(s, s->spatial_dwt_buffer, w, h, w);
3195 #else
3196 spatial_idwt(s, s->spatial_dwt_buffer, w, h, w); 2905 spatial_idwt(s, s->spatial_dwt_buffer, w, h, w);
3197 predict_plane(s, s->spatial_dwt_buffer, plane_index, 1); 2906 predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
3198 #endif
3199 //FIXME optimize 2907 //FIXME optimize
3200 for(y=0; y<h; y++){ 2908 for(y=0; y<h; y++){
3201 for(x=0; x<w; x++){ 2909 for(x=0; x<w; x++){
3202 int v= (s->spatial_dwt_buffer[y*w + x]+128)>>8; 2910 int v= (s->spatial_dwt_buffer[y*w + x]+128)>>8;
3203 if(v&(~255)) v= ~(v>>31); 2911 if(v&(~255)) v= ~(v>>31);
3330 3038
3331 dequantize(s, b, b->buf, b->stride); 3039 dequantize(s, b, b->buf, b->stride);
3332 } 3040 }
3333 } 3041 }
3334 3042
3335 #if QPRED
3336 memset(s->pred_buffer, 0, sizeof(DWTELEM)*w*h);
3337 predict_plane(s, s->pred_buffer, plane_index, 1);
3338 spatial_dwt(s, s->pred_buffer, w, h, w);
3339 for(level=0; level<s->spatial_decomposition_count; level++){
3340 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3341 SubBand *b= &p->band[level][orientation];
3342 int delta= ((int)s->pred_buffer - (int)s->spatial_dwt_buffer)/sizeof(DWTELEM);
3343
3344 quantize (s, b, b->buf + delta, b->stride, s->qbias);
3345 dequantize(s, b, b->buf + delta, b->stride);
3346 }
3347 }
3348 for(y=0; y<h; y++){
3349 for(x=0; x<w; x++){
3350 s->spatial_dwt_buffer[y*w + x]+= s->pred_buffer[y*w + x];
3351 }
3352 }
3353 spatial_idwt(s, s->spatial_dwt_buffer, w, h, w);
3354 #else
3355 spatial_idwt(s, s->spatial_dwt_buffer, w, h, w); 3043 spatial_idwt(s, s->spatial_dwt_buffer, w, h, w);
3356 predict_plane(s, s->spatial_dwt_buffer, plane_index, 1); 3044 predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
3357 #endif
3358 3045
3359 //FIXME optimize 3046 //FIXME optimize
3360 for(y=0; y<h; y++){ 3047 for(y=0; y<h; y++){
3361 for(x=0; x<w; x++){ 3048 for(x=0; x<w; x++){
3362 int v= (s->spatial_dwt_buffer[y*w + x]+128)>>8; 3049 int v= (s->spatial_dwt_buffer[y*w + x]+128)>>8;