comparison snow.c @ 2967:ef2149182f1c libavcodec

COSMETICS: Remove all trailing whitespace.
author diego
date Sat, 17 Dec 2005 18:14:38 +0000
parents d7be13d3ef00
children 18e7ecf83aa2
comparison
equal deleted inserted replaced
2966:564788471dd4 2967:ef2149182f1c
480 //#define slice_buffer_get_line(slice_buf, line_num) (slice_buffer_load_line((slice_buf), (line_num))) 480 //#define slice_buffer_get_line(slice_buf, line_num) (slice_buffer_load_line((slice_buf), (line_num)))
481 481
482 static void slice_buffer_init(slice_buffer * buf, int line_count, int max_allocated_lines, int line_width, DWTELEM * base_buffer) 482 static void slice_buffer_init(slice_buffer * buf, int line_count, int max_allocated_lines, int line_width, DWTELEM * base_buffer)
483 { 483 {
484 int i; 484 int i;
485 485
486 buf->base_buffer = base_buffer; 486 buf->base_buffer = base_buffer;
487 buf->line_count = line_count; 487 buf->line_count = line_count;
488 buf->line_width = line_width; 488 buf->line_width = line_width;
489 buf->data_count = max_allocated_lines; 489 buf->data_count = max_allocated_lines;
490 buf->line = (DWTELEM * *) av_mallocz (sizeof(DWTELEM *) * line_count); 490 buf->line = (DWTELEM * *) av_mallocz (sizeof(DWTELEM *) * line_count);
491 buf->data_stack = (DWTELEM * *) av_malloc (sizeof(DWTELEM *) * max_allocated_lines); 491 buf->data_stack = (DWTELEM * *) av_malloc (sizeof(DWTELEM *) * max_allocated_lines);
492 492
493 for (i = 0; i < max_allocated_lines; i++) 493 for (i = 0; i < max_allocated_lines; i++)
494 { 494 {
495 buf->data_stack[i] = (DWTELEM *) av_malloc (sizeof(DWTELEM) * line_width); 495 buf->data_stack[i] = (DWTELEM *) av_malloc (sizeof(DWTELEM) * line_width);
496 } 496 }
497 497
498 buf->data_stack_top = max_allocated_lines - 1; 498 buf->data_stack_top = max_allocated_lines - 1;
499 } 499 }
500 500
501 static DWTELEM * slice_buffer_load_line(slice_buffer * buf, int line) 501 static DWTELEM * slice_buffer_load_line(slice_buffer * buf, int line)
502 { 502 {
503 int offset; 503 int offset;
504 DWTELEM * buffer; 504 DWTELEM * buffer;
505 505
506 // av_log(NULL, AV_LOG_DEBUG, "Cache hit: %d\n", line); 506 // av_log(NULL, AV_LOG_DEBUG, "Cache hit: %d\n", line);
507 507
508 assert(buf->data_stack_top >= 0); 508 assert(buf->data_stack_top >= 0);
509 // assert(!buf->line[line]); 509 // assert(!buf->line[line]);
510 if (buf->line[line]) 510 if (buf->line[line])
511 return buf->line[line]; 511 return buf->line[line];
512 512
513 offset = buf->line_width * line; 513 offset = buf->line_width * line;
514 buffer = buf->data_stack[buf->data_stack_top]; 514 buffer = buf->data_stack[buf->data_stack_top];
515 buf->data_stack_top--; 515 buf->data_stack_top--;
516 buf->line[line] = buffer; 516 buf->line[line] = buffer;
517 517
518 // av_log(NULL, AV_LOG_DEBUG, "slice_buffer_load_line: line: %d remaining: %d\n", line, buf->data_stack_top + 1); 518 // av_log(NULL, AV_LOG_DEBUG, "slice_buffer_load_line: line: %d remaining: %d\n", line, buf->data_stack_top + 1);
519 519
520 return buffer; 520 return buffer;
521 } 521 }
522 522
523 static void slice_buffer_release(slice_buffer * buf, int line) 523 static void slice_buffer_release(slice_buffer * buf, int line)
524 { 524 {
531 offset = buf->line_width * line; 531 offset = buf->line_width * line;
532 buffer = buf->line[line]; 532 buffer = buf->line[line];
533 buf->data_stack_top++; 533 buf->data_stack_top++;
534 buf->data_stack[buf->data_stack_top] = buffer; 534 buf->data_stack[buf->data_stack_top] = buffer;
535 buf->line[line] = NULL; 535 buf->line[line] = NULL;
536 536
537 // av_log(NULL, AV_LOG_DEBUG, "slice_buffer_release: line: %d remaining: %d\n", line, buf->data_stack_top + 1); 537 // av_log(NULL, AV_LOG_DEBUG, "slice_buffer_release: line: %d remaining: %d\n", line, buf->data_stack_top + 1);
538 } 538 }
539 539
540 static void slice_buffer_flush(slice_buffer * buf) 540 static void slice_buffer_flush(slice_buffer * buf)
541 { 541 {
552 552
553 static void slice_buffer_destroy(slice_buffer * buf) 553 static void slice_buffer_destroy(slice_buffer * buf)
554 { 554 {
555 int i; 555 int i;
556 slice_buffer_flush(buf); 556 slice_buffer_flush(buf);
557 557
558 for (i = buf->data_count - 1; i >= 0; i--) 558 for (i = buf->data_count - 1; i >= 0; i--)
559 { 559 {
560 assert(buf->data_stack[i]); 560 assert(buf->data_stack[i]);
561 av_free(buf->data_stack[i]); 561 av_free(buf->data_stack[i]);
562 } 562 }
584 584
585 if(v){ 585 if(v){
586 const int a= ABS(v); 586 const int a= ABS(v);
587 const int e= av_log2(a); 587 const int e= av_log2(a);
588 #if 1 588 #if 1
589 const int el= FFMIN(e, 10); 589 const int el= FFMIN(e, 10);
590 put_rac(c, state+0, 0); 590 put_rac(c, state+0, 0);
591 591
592 for(i=0; i<el; i++){ 592 for(i=0; i<el; i++){
593 put_rac(c, state+1+i, 1); //1..10 593 put_rac(c, state+1+i, 1); //1..10
594 } 594 }
605 } 605 }
606 606
607 if(is_signed) 607 if(is_signed)
608 put_rac(c, state+11 + el, v < 0); //11..21 608 put_rac(c, state+11 + el, v < 0); //11..21
609 #else 609 #else
610 610
611 put_rac(c, state+0, 0); 611 put_rac(c, state+0, 0);
612 if(e<=9){ 612 if(e<=9){
613 for(i=0; i<e; i++){ 613 for(i=0; i<e; i++){
614 put_rac(c, state+1+i, 1); //1..10 614 put_rac(c, state+1+i, 1); //1..10
615 } 615 }
674 v -= r; 674 v -= r;
675 log2++; 675 log2++;
676 if(log2>0) r+=r; 676 if(log2>0) r+=r;
677 } 677 }
678 put_rac(c, state+4+log2, 0); 678 put_rac(c, state+4+log2, 0);
679 679
680 for(i=log2-1; i>=0; i--){ 680 for(i=log2-1; i>=0; i--){
681 put_rac(c, state+31-i, (v>>i)&1); 681 put_rac(c, state+31-i, (v>>i)&1);
682 } 682 }
683 } 683 }
684 684
692 while(get_rac(c, state+4+log2)){ 692 while(get_rac(c, state+4+log2)){
693 v+= r; 693 v+= r;
694 log2++; 694 log2++;
695 if(log2>0) r+=r; 695 if(log2>0) r+=r;
696 } 696 }
697 697
698 for(i=log2-1; i>=0; i--){ 698 for(i=log2-1; i>=0; i--){
699 v+= get_rac(c, state+31-i)<<i; 699 v+= get_rac(c, state+31-i)<<i;
700 } 700 }
701 701
702 return v; 702 return v;
712 if(mirror_left){ 712 if(mirror_left){
713 dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse); 713 dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
714 dst += dst_step; 714 dst += dst_step;
715 src += src_step; 715 src += src_step;
716 } 716 }
717 717
718 for(i=0; i<w; i++){ 718 for(i=0; i<w; i++){
719 dst[i*dst_step] = LIFT(src[i*src_step], ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift), inverse); 719 dst[i*dst_step] = LIFT(src[i*src_step], ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift), inverse);
720 } 720 }
721 721
722 if(mirror_right){ 722 if(mirror_right){
723 dst[w*dst_step] = LIFT(src[w*src_step], ((mul*2*ref[w*ref_step]+add)>>shift), inverse); 723 dst[w*dst_step] = LIFT(src[w*src_step], ((mul*2*ref[w*ref_step]+add)>>shift), inverse);
724 } 724 }
725 } 725 }
726 726
736 r += r>>8; 736 r += r>>8;
737 dst[0] = LIFT(src[0], ((r+add)>>shift), inverse); 737 dst[0] = LIFT(src[0], ((r+add)>>shift), inverse);
738 dst += dst_step; 738 dst += dst_step;
739 src += src_step; 739 src += src_step;
740 } 740 }
741 741
742 for(i=0; i<w; i++){ 742 for(i=0; i<w; i++){
743 int r= 3*(ref[i*ref_step] + ref[(i+1)*ref_step]); 743 int r= 3*(ref[i*ref_step] + ref[(i+1)*ref_step]);
744 r += r>>4; 744 r += r>>4;
745 r += r>>8; 745 r += r>>8;
746 dst[i*dst_step] = LIFT(src[i*src_step], ((r+add)>>shift), inverse); 746 dst[i*dst_step] = LIFT(src[i*src_step], ((r+add)>>shift), inverse);
747 } 747 }
748 748
749 if(mirror_right){ 749 if(mirror_right){
750 int r= 3*2*ref[w*ref_step]; 750 int r= 3*2*ref[w*ref_step];
751 r += r>>4; 751 r += r>>4;
752 r += r>>8; 752 r += r>>8;
753 dst[w*dst_step] = LIFT(src[w*src_step], ((r+add)>>shift), inverse); 753 dst[w*dst_step] = LIFT(src[w*src_step], ((r+add)>>shift), inverse);
765 if(mirror_left){ 765 if(mirror_left){
766 dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse); 766 dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse);
767 dst += dst_step; 767 dst += dst_step;
768 src += src_step; 768 src += src_step;
769 } 769 }
770 770
771 for(i=0; i<w; i++){ 771 for(i=0; i<w; i++){
772 dst[i*dst_step] = LIFTS(src[i*src_step], mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add, inverse); 772 dst[i*dst_step] = LIFTS(src[i*src_step], mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add, inverse);
773 } 773 }
774 774
775 if(mirror_right){ 775 if(mirror_right){
776 dst[w*dst_step] = LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse); 776 dst[w*dst_step] = LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse);
777 } 777 }
778 } 778 }
779 779
780 780
781 static void inplace_lift(DWTELEM *dst, int width, int *coeffs, int n, int shift, int start, int inverse){ 781 static void inplace_lift(DWTELEM *dst, int width, int *coeffs, int n, int shift, int start, int inverse){
782 int x, i; 782 int x, i;
783 783
784 for(x=start; x<width; x+=2){ 784 for(x=start; x<width; x+=2){
785 int64_t sum=0; 785 int64_t sum=0;
786 786
787 for(i=0; i<n; i++){ 787 for(i=0; i<n; i++){
788 int x2= x + 2*i - n + 1; 788 int x2= x + 2*i - n + 1;
798 static void inplace_liftV(DWTELEM *dst, int width, int height, int stride, int *coeffs, int n, int shift, int start, int inverse){ 798 static void inplace_liftV(DWTELEM *dst, int width, int height, int stride, int *coeffs, int n, int shift, int start, int inverse){
799 int x, y, i; 799 int x, y, i;
800 for(y=start; y<height; y+=2){ 800 for(y=start; y<height; y+=2){
801 for(x=0; x<width; x++){ 801 for(x=0; x<width; x++){
802 int64_t sum=0; 802 int64_t sum=0;
803 803
804 for(i=0; i<n; i++){ 804 for(i=0; i<n; i++){
805 int y2= y + 2*i - n + 1; 805 int y2= y + 2*i - n + 1;
806 if (y2< 0) y2= -y2; 806 if (y2< 0) y2= -y2;
807 else if(y2>=height) y2= 2*height-y2-2; 807 else if(y2>=height) y2= 2*height-y2-2;
808 sum += coeffs[i]*(int64_t)dst[x + y2*stride]; 808 sum += coeffs[i]*(int64_t)dst[x + y2*stride];
857 #define SHIFT3 0 857 #define SHIFT3 0
858 #define COEFFS3 NULL 858 #define COEFFS3 NULL
859 #define N4 0 859 #define N4 0
860 #define SHIFT4 0 860 #define SHIFT4 0
861 #define COEFFS4 NULL 861 #define COEFFS4 NULL
862 #elif 1 // 11/5 862 #elif 1 // 11/5
863 #define N1 0 863 #define N1 0
864 #define SHIFT1 1 864 #define SHIFT1 1
865 #define COEFFS1 NULL 865 #define COEFFS1 NULL
866 #define N2 2 866 #define N2 2
867 #define SHIFT2 2 867 #define SHIFT2 2
935 935
936 inplace_lift(b, width, COEFFS1, N1, SHIFT1, LX1, 0); 936 inplace_lift(b, width, COEFFS1, N1, SHIFT1, LX1, 0);
937 inplace_lift(b, width, COEFFS2, N2, SHIFT2, LX0, 0); 937 inplace_lift(b, width, COEFFS2, N2, SHIFT2, LX0, 0);
938 inplace_lift(b, width, COEFFS3, N3, SHIFT3, LX1, 0); 938 inplace_lift(b, width, COEFFS3, N3, SHIFT3, LX1, 0);
939 inplace_lift(b, width, COEFFS4, N4, SHIFT4, LX0, 0); 939 inplace_lift(b, width, COEFFS4, N4, SHIFT4, LX0, 0);
940 940
941 for(x=0; x<width2; x++){ 941 for(x=0; x<width2; x++){
942 temp[x ]= b[2*x ]; 942 temp[x ]= b[2*x ];
943 temp[x+w2]= b[2*x + 1]; 943 temp[x+w2]= b[2*x + 1];
944 } 944 }
945 if(width&1) 945 if(width&1)
967 inplace_lift(b, width, COEFFS1, N1, SHIFT1, LX1, 1); 967 inplace_lift(b, width, COEFFS1, N1, SHIFT1, LX1, 1);
968 } 968 }
969 969
970 static void spatial_decomposeX(DWTELEM *buffer, int width, int height, int stride){ 970 static void spatial_decomposeX(DWTELEM *buffer, int width, int height, int stride){
971 int x, y; 971 int x, y;
972 972
973 for(y=0; y<height; y++){ 973 for(y=0; y<height; y++){
974 for(x=0; x<width; x++){ 974 for(x=0; x<width; x++){
975 buffer[y*stride + x] *= SCALEX; 975 buffer[y*stride + x] *= SCALEX;
976 } 976 }
977 } 977 }
978 978
979 for(y=0; y<height; y++){ 979 for(y=0; y<height; y++){
980 horizontal_decomposeX(buffer + y*stride, width); 980 horizontal_decomposeX(buffer + y*stride, width);
981 } 981 }
982 982
983 inplace_liftV(buffer, width, height, stride, COEFFS1, N1, SHIFT1, LX1, 0); 983 inplace_liftV(buffer, width, height, stride, COEFFS1, N1, SHIFT1, LX1, 0);
984 inplace_liftV(buffer, width, height, stride, COEFFS2, N2, SHIFT2, LX0, 0); 984 inplace_liftV(buffer, width, height, stride, COEFFS2, N2, SHIFT2, LX0, 0);
985 inplace_liftV(buffer, width, height, stride, COEFFS3, N3, SHIFT3, LX1, 0); 985 inplace_liftV(buffer, width, height, stride, COEFFS3, N3, SHIFT3, LX1, 0);
986 inplace_liftV(buffer, width, height, stride, COEFFS4, N4, SHIFT4, LX0, 0); 986 inplace_liftV(buffer, width, height, stride, COEFFS4, N4, SHIFT4, LX0, 0);
987 } 987 }
988 988
989 static void spatial_composeX(DWTELEM *buffer, int width, int height, int stride){ 989 static void spatial_composeX(DWTELEM *buffer, int width, int height, int stride){
990 int x, y; 990 int x, y;
991 991
992 inplace_liftV(buffer, width, height, stride, COEFFS4, N4, SHIFT4, LX0, 1); 992 inplace_liftV(buffer, width, height, stride, COEFFS4, N4, SHIFT4, LX0, 1);
993 inplace_liftV(buffer, width, height, stride, COEFFS3, N3, SHIFT3, LX1, 1); 993 inplace_liftV(buffer, width, height, stride, COEFFS3, N3, SHIFT3, LX1, 1);
994 inplace_liftV(buffer, width, height, stride, COEFFS2, N2, SHIFT2, LX0, 1); 994 inplace_liftV(buffer, width, height, stride, COEFFS2, N2, SHIFT2, LX0, 1);
995 inplace_liftV(buffer, width, height, stride, COEFFS1, N1, SHIFT1, LX1, 1); 995 inplace_liftV(buffer, width, height, stride, COEFFS1, N1, SHIFT1, LX1, 1);
996 996
1046 A3 -= A2; 1046 A3 -= A2;
1047 A2 += (A1 + A3 + 2)>>2; 1047 A2 += (A1 + A3 + 2)>>2;
1048 b[width -1] = A3; 1048 b[width -1] = A3;
1049 b[width2-1] = A2; 1049 b[width2-1] = A2;
1050 } 1050 }
1051 #else 1051 #else
1052 lift(b+w2, temp+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0); 1052 lift(b+w2, temp+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
1053 lift(b , temp , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 0); 1053 lift(b , temp , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 0);
1054 #endif 1054 #endif
1055 } 1055 }
1056 1056
1057 static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){ 1057 static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1058 int i; 1058 int i;
1059 1059
1060 for(i=0; i<width; i++){ 1060 for(i=0; i<width; i++){
1061 b1[i] -= (b0[i] + b2[i])>>1; 1061 b1[i] -= (b0[i] + b2[i])>>1;
1062 } 1062 }
1063 } 1063 }
1064 1064
1065 static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){ 1065 static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1066 int i; 1066 int i;
1067 1067
1068 for(i=0; i<width; i++){ 1068 for(i=0; i<width; i++){
1069 b1[i] += (b0[i] + b2[i] + 2)>>2; 1069 b1[i] += (b0[i] + b2[i] + 2)>>2;
1070 } 1070 }
1071 } 1071 }
1072 1072
1073 static void spatial_decompose53i(DWTELEM *buffer, int width, int height, int stride){ 1073 static void spatial_decompose53i(DWTELEM *buffer, int width, int height, int stride){
1074 int y; 1074 int y;
1075 DWTELEM *b0= buffer + mirror(-2-1, height-1)*stride; 1075 DWTELEM *b0= buffer + mirror(-2-1, height-1)*stride;
1076 DWTELEM *b1= buffer + mirror(-2 , height-1)*stride; 1076 DWTELEM *b1= buffer + mirror(-2 , height-1)*stride;
1077 1077
1078 for(y=-2; y<height; y+=2){ 1078 for(y=-2; y<height; y+=2){
1079 DWTELEM *b2= buffer + mirror(y+1, height-1)*stride; 1079 DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
1080 DWTELEM *b3= buffer + mirror(y+2, height-1)*stride; 1080 DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
1081 1081
1082 {START_TIMER 1082 {START_TIMER
1083 if(b1 <= b3) horizontal_decompose53i(b2, width); 1083 if(b1 <= b3) horizontal_decompose53i(b2, width);
1084 if(y+2 < height) horizontal_decompose53i(b3, width); 1084 if(y+2 < height) horizontal_decompose53i(b3, width);
1085 STOP_TIMER("horizontal_decompose53i")} 1085 STOP_TIMER("horizontal_decompose53i")}
1086 1086
1087 {START_TIMER 1087 {START_TIMER
1088 if(b1 <= b3) vertical_decompose53iH0(b1, b2, b3, width); 1088 if(b1 <= b3) vertical_decompose53iH0(b1, b2, b3, width);
1089 if(b0 <= b2) vertical_decompose53iL0(b0, b1, b2, width); 1089 if(b0 <= b2) vertical_decompose53iL0(b0, b1, b2, width);
1090 STOP_TIMER("vertical_decompose53i*")} 1090 STOP_TIMER("vertical_decompose53i*")}
1091 1091
1092 b0=b2; 1092 b0=b2;
1093 b1=b3; 1093 b1=b3;
1094 } 1094 }
1095 } 1095 }
1096 1096
1175 } 1175 }
1176 1176
1177 1177
1178 static void vertical_decompose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){ 1178 static void vertical_decompose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1179 int i; 1179 int i;
1180 1180
1181 for(i=0; i<width; i++){ 1181 for(i=0; i<width; i++){
1182 b1[i] -= (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS; 1182 b1[i] -= (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
1183 } 1183 }
1184 } 1184 }
1185 1185
1186 static void vertical_decompose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){ 1186 static void vertical_decompose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1187 int i; 1187 int i;
1188 1188
1189 for(i=0; i<width; i++){ 1189 for(i=0; i<width; i++){
1190 #ifdef lift5 1190 #ifdef lift5
1191 b1[i] += (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS; 1191 b1[i] += (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
1192 #else 1192 #else
1193 int r= 3*(b0[i] + b2[i]); 1193 int r= 3*(b0[i] + b2[i]);
1198 } 1198 }
1199 } 1199 }
1200 1200
1201 static void vertical_decompose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){ 1201 static void vertical_decompose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1202 int i; 1202 int i;
1203 1203
1204 for(i=0; i<width; i++){ 1204 for(i=0; i<width; i++){
1205 #ifdef liftS 1205 #ifdef liftS
1206 b1[i] -= (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS; 1206 b1[i] -= (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
1207 #else 1207 #else
1208 b1[i] = (16*4*b1[i] - 4*(b0[i] + b2[i]) + 8*5 + (5<<27)) / (5*16) - (1<<23); 1208 b1[i] = (16*4*b1[i] - 4*(b0[i] + b2[i]) + 8*5 + (5<<27)) / (5*16) - (1<<23);
1210 } 1210 }
1211 } 1211 }
1212 1212
1213 static void vertical_decompose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){ 1213 static void vertical_decompose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1214 int i; 1214 int i;
1215 1215
1216 for(i=0; i<width; i++){ 1216 for(i=0; i<width; i++){
1217 b1[i] += (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS; 1217 b1[i] += (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
1218 } 1218 }
1219 } 1219 }
1220 1220
1222 int y; 1222 int y;
1223 DWTELEM *b0= buffer + mirror(-4-1, height-1)*stride; 1223 DWTELEM *b0= buffer + mirror(-4-1, height-1)*stride;
1224 DWTELEM *b1= buffer + mirror(-4 , height-1)*stride; 1224 DWTELEM *b1= buffer + mirror(-4 , height-1)*stride;
1225 DWTELEM *b2= buffer + mirror(-4+1, height-1)*stride; 1225 DWTELEM *b2= buffer + mirror(-4+1, height-1)*stride;
1226 DWTELEM *b3= buffer + mirror(-4+2, height-1)*stride; 1226 DWTELEM *b3= buffer + mirror(-4+2, height-1)*stride;
1227 1227
1228 for(y=-4; y<height; y+=2){ 1228 for(y=-4; y<height; y+=2){
1229 DWTELEM *b4= buffer + mirror(y+3, height-1)*stride; 1229 DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
1230 DWTELEM *b5= buffer + mirror(y+4, height-1)*stride; 1230 DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
1231 1231
1232 {START_TIMER 1232 {START_TIMER
1233 if(b3 <= b5) horizontal_decompose97i(b4, width); 1233 if(b3 <= b5) horizontal_decompose97i(b4, width);
1234 if(y+4 < height) horizontal_decompose97i(b5, width); 1234 if(y+4 < height) horizontal_decompose97i(b5, width);
1235 if(width>400){ 1235 if(width>400){
1236 STOP_TIMER("horizontal_decompose97i") 1236 STOP_TIMER("horizontal_decompose97i")
1237 }} 1237 }}
1238 1238
1239 {START_TIMER 1239 {START_TIMER
1240 if(b3 <= b5) vertical_decompose97iH0(b3, b4, b5, width); 1240 if(b3 <= b5) vertical_decompose97iH0(b3, b4, b5, width);
1241 if(b2 <= b4) vertical_decompose97iL0(b2, b3, b4, width); 1241 if(b2 <= b4) vertical_decompose97iL0(b2, b3, b4, width);
1242 if(b1 <= b3) vertical_decompose97iH1(b1, b2, b3, width); 1242 if(b1 <= b3) vertical_decompose97iH1(b1, b2, b3, width);
1243 if(b0 <= b2) vertical_decompose97iL1(b0, b1, b2, width); 1243 if(b0 <= b2) vertical_decompose97iL1(b0, b1, b2, width);
1244 1244
1245 if(width>400){ 1245 if(width>400){
1246 STOP_TIMER("vertical_decompose97i") 1246 STOP_TIMER("vertical_decompose97i")
1247 }} 1247 }}
1248 1248
1249 b0=b2; 1249 b0=b2;
1250 b1=b3; 1250 b1=b3;
1251 b2=b4; 1251 b2=b4;
1252 b3=b5; 1252 b3=b5;
1253 } 1253 }
1254 } 1254 }
1255 1255
1256 void ff_spatial_dwt(DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){ 1256 void ff_spatial_dwt(DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
1257 int level; 1257 int level;
1258 1258
1259 for(level=0; level<decomposition_count; level++){ 1259 for(level=0; level<decomposition_count; level++){
1260 switch(type){ 1260 switch(type){
1261 case 0: spatial_decompose97i(buffer, width>>level, height>>level, stride<<level); break; 1261 case 0: spatial_decompose97i(buffer, width>>level, height>>level, stride<<level); break;
1262 case 1: spatial_decompose53i(buffer, width>>level, height>>level, stride<<level); break; 1262 case 1: spatial_decompose53i(buffer, width>>level, height>>level, stride<<level); break;
1263 case 2: spatial_decomposeX (buffer, width>>level, height>>level, stride<<level); break; 1263 case 2: spatial_decomposeX (buffer, width>>level, height>>level, stride<<level); break;
1298 A3= temp[width-1]; 1298 A3= temp[width-1];
1299 A3 -= A2; 1299 A3 -= A2;
1300 A2 += (A1 + A3 + 2)>>2; 1300 A2 += (A1 + A3 + 2)>>2;
1301 b[width -1] = A3; 1301 b[width -1] = A3;
1302 b[width2-1] = A2; 1302 b[width2-1] = A2;
1303 #else 1303 #else
1304 lift(temp , b , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 1); 1304 lift(temp , b , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 1);
1305 lift(temp+w2, b+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 1); 1305 lift(temp+w2, b+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 1);
1306 #endif 1306 #endif
1307 for(x=0; x<width2; x++){ 1307 for(x=0; x<width2; x++){
1308 b[2*x ]= temp[x ]; 1308 b[2*x ]= temp[x ];
1312 b[2*x ]= temp[x ]; 1312 b[2*x ]= temp[x ];
1313 } 1313 }
1314 1314
1315 static void vertical_compose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){ 1315 static void vertical_compose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1316 int i; 1316 int i;
1317 1317
1318 for(i=0; i<width; i++){ 1318 for(i=0; i<width; i++){
1319 b1[i] += (b0[i] + b2[i])>>1; 1319 b1[i] += (b0[i] + b2[i])>>1;
1320 } 1320 }
1321 } 1321 }
1322 1322
1323 static void vertical_compose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){ 1323 static void vertical_compose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1324 int i; 1324 int i;
1325 1325
1326 for(i=0; i<width; i++){ 1326 for(i=0; i<width; i++){
1327 b1[i] -= (b0[i] + b2[i] + 2)>>2; 1327 b1[i] -= (b0[i] + b2[i] + 2)>>2;
1328 } 1328 }
1329 } 1329 }
1330 1330
1344 int y= cs->y; 1344 int y= cs->y;
1345 int mirror0 = mirror(y-1, height-1); 1345 int mirror0 = mirror(y-1, height-1);
1346 int mirror1 = mirror(y , height-1); 1346 int mirror1 = mirror(y , height-1);
1347 int mirror2 = mirror(y+1, height-1); 1347 int mirror2 = mirror(y+1, height-1);
1348 int mirror3 = mirror(y+2, height-1); 1348 int mirror3 = mirror(y+2, height-1);
1349 1349
1350 DWTELEM *b0= cs->b0; 1350 DWTELEM *b0= cs->b0;
1351 DWTELEM *b1= cs->b1; 1351 DWTELEM *b1= cs->b1;
1352 DWTELEM *b2= slice_buffer_get_line(sb, mirror2 * stride_line); 1352 DWTELEM *b2= slice_buffer_get_line(sb, mirror2 * stride_line);
1353 DWTELEM *b3= slice_buffer_get_line(sb, mirror3 * stride_line); 1353 DWTELEM *b3= slice_buffer_get_line(sb, mirror3 * stride_line);
1354 1354
1392 static void spatial_compose53i(DWTELEM *buffer, int width, int height, int stride){ 1392 static void spatial_compose53i(DWTELEM *buffer, int width, int height, int stride){
1393 dwt_compose_t cs; 1393 dwt_compose_t cs;
1394 spatial_compose53i_init(&cs, buffer, height, stride); 1394 spatial_compose53i_init(&cs, buffer, height, stride);
1395 while(cs.y <= height) 1395 while(cs.y <= height)
1396 spatial_compose53i_dy(&cs, buffer, width, height, stride); 1396 spatial_compose53i_dy(&cs, buffer, width, height, stride);
1397 } 1397 }
1398 1398
1399 1399
1400 static void horizontal_compose97i(DWTELEM *b, int width){ 1400 static void horizontal_compose97i(DWTELEM *b, int width){
1401 DWTELEM temp[width]; 1401 DWTELEM temp[width];
1402 const int w2= (width+1)>>1; 1402 const int w2= (width+1)>>1;
1403 1403
1404 lift (temp , b , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 1); 1404 lift (temp , b , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 1);
1407 lift (b+1 , temp+w2, b , 2, 1, 2, width, -W_AM, W_AO, W_AS, 1, 1); 1407 lift (b+1 , temp+w2, b , 2, 1, 2, width, -W_AM, W_AO, W_AS, 1, 1);
1408 } 1408 }
1409 1409
1410 static void vertical_compose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){ 1410 static void vertical_compose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1411 int i; 1411 int i;
1412 1412
1413 for(i=0; i<width; i++){ 1413 for(i=0; i<width; i++){
1414 b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS; 1414 b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
1415 } 1415 }
1416 } 1416 }
1417 1417
1418 static void vertical_compose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){ 1418 static void vertical_compose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1419 int i; 1419 int i;
1420 1420
1421 for(i=0; i<width; i++){ 1421 for(i=0; i<width; i++){
1422 #ifdef lift5 1422 #ifdef lift5
1423 b1[i] -= (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS; 1423 b1[i] -= (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
1424 #else 1424 #else
1425 int r= 3*(b0[i] + b2[i]); 1425 int r= 3*(b0[i] + b2[i]);
1430 } 1430 }
1431 } 1431 }
1432 1432
1433 static void vertical_compose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){ 1433 static void vertical_compose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1434 int i; 1434 int i;
1435 1435
1436 for(i=0; i<width; i++){ 1436 for(i=0; i<width; i++){
1437 #ifdef liftS 1437 #ifdef liftS
1438 b1[i] += (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS; 1438 b1[i] += (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
1439 #else 1439 #else
1440 b1[i] += (W_BM*(b0[i] + b2[i])+4*b1[i]+W_BO)>>W_BS; 1440 b1[i] += (W_BM*(b0[i] + b2[i])+4*b1[i]+W_BO)>>W_BS;
1442 } 1442 }
1443 } 1443 }
1444 1444
1445 static void vertical_compose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){ 1445 static void vertical_compose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1446 int i; 1446 int i;
1447 1447
1448 for(i=0; i<width; i++){ 1448 for(i=0; i<width; i++){
1449 b1[i] -= (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS; 1449 b1[i] -= (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
1450 } 1450 }
1451 } 1451 }
1452 1452
1453 static void vertical_compose97i(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, DWTELEM *b3, DWTELEM *b4, DWTELEM *b5, int width){ 1453 static void vertical_compose97i(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, DWTELEM *b3, DWTELEM *b4, DWTELEM *b5, int width){
1454 int i; 1454 int i;
1455 1455
1456 for(i=0; i<width; i++){ 1456 for(i=0; i<width; i++){
1457 #ifndef lift5 1457 #ifndef lift5
1458 int r; 1458 int r;
1459 #endif 1459 #endif
1460 b4[i] -= (W_DM*(b3[i] + b5[i])+W_DO)>>W_DS; 1460 b4[i] -= (W_DM*(b3[i] + b5[i])+W_DO)>>W_DS;
1491 cs->y = -3; 1491 cs->y = -3;
1492 } 1492 }
1493 1493
1494 static void spatial_compose97i_dy_buffered(dwt_compose_t *cs, slice_buffer * sb, int width, int height, int stride_line){ 1494 static void spatial_compose97i_dy_buffered(dwt_compose_t *cs, slice_buffer * sb, int width, int height, int stride_line){
1495 int y = cs->y; 1495 int y = cs->y;
1496 1496
1497 int mirror0 = mirror(y - 1, height - 1); 1497 int mirror0 = mirror(y - 1, height - 1);
1498 int mirror1 = mirror(y + 0, height - 1); 1498 int mirror1 = mirror(y + 0, height - 1);
1499 int mirror2 = mirror(y + 1, height - 1); 1499 int mirror2 = mirror(y + 1, height - 1);
1500 int mirror3 = mirror(y + 2, height - 1); 1500 int mirror3 = mirror(y + 2, height - 1);
1501 int mirror4 = mirror(y + 3, height - 1); 1501 int mirror4 = mirror(y + 3, height - 1);
1504 DWTELEM *b1= cs->b1; 1504 DWTELEM *b1= cs->b1;
1505 DWTELEM *b2= cs->b2; 1505 DWTELEM *b2= cs->b2;
1506 DWTELEM *b3= cs->b3; 1506 DWTELEM *b3= cs->b3;
1507 DWTELEM *b4= slice_buffer_get_line(sb, mirror4 * stride_line); 1507 DWTELEM *b4= slice_buffer_get_line(sb, mirror4 * stride_line);
1508 DWTELEM *b5= slice_buffer_get_line(sb, mirror5 * stride_line); 1508 DWTELEM *b5= slice_buffer_get_line(sb, mirror5 * stride_line);
1509 1509
1510 {START_TIMER 1510 {START_TIMER
1511 if(y>0 && y+4<height){ 1511 if(y>0 && y+4<height){
1512 vertical_compose97i(b0, b1, b2, b3, b4, b5, width); 1512 vertical_compose97i(b0, b1, b2, b3, b4, b5, width);
1513 }else{ 1513 }else{
1514 if(mirror3 <= mirror5) vertical_compose97iL1(b3, b4, b5, width); 1514 if(mirror3 <= mirror5) vertical_compose97iL1(b3, b4, b5, width);
1539 DWTELEM *b2= cs->b2; 1539 DWTELEM *b2= cs->b2;
1540 DWTELEM *b3= cs->b3; 1540 DWTELEM *b3= cs->b3;
1541 DWTELEM *b4= buffer + mirror(y+3, height-1)*stride; 1541 DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
1542 DWTELEM *b5= buffer + mirror(y+4, height-1)*stride; 1542 DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
1543 1543
1544 if(stride == width && y+4 < height && 0){ 1544 if(stride == width && y+4 < height && 0){
1545 int x; 1545 int x;
1546 for(x=0; x<width/2; x++) 1546 for(x=0; x<width/2; x++)
1547 b5[x] += 64*2; 1547 b5[x] += 64*2;
1548 for(; x<width; x++) 1548 for(; x<width; x++)
1549 b5[x] += 169*2; 1549 b5[x] += 169*2;
1550 } 1550 }
1551 1551
1552 {START_TIMER 1552 {START_TIMER
1553 if(b3 <= b5) vertical_compose97iL1(b3, b4, b5, width); 1553 if(b3 <= b5) vertical_compose97iL1(b3, b4, b5, width);
1554 if(b2 <= b4) vertical_compose97iH1(b2, b3, b4, width); 1554 if(b2 <= b4) vertical_compose97iH1(b2, b3, b4, width);
1555 if(b1 <= b3) vertical_compose97iL0(b1, b2, b3, width); 1555 if(b1 <= b3) vertical_compose97iL0(b1, b2, b3, width);
1556 if(b0 <= b2) vertical_compose97iH0(b0, b1, b2, width); 1556 if(b0 <= b2) vertical_compose97iH0(b0, b1, b2, width);
1660 if(1){ 1660 if(1){
1661 int run=0; 1661 int run=0;
1662 int runs[w*h]; 1662 int runs[w*h];
1663 int run_index=0; 1663 int run_index=0;
1664 int max_index; 1664 int max_index;
1665 1665
1666 for(y=0; y<h; y++){ 1666 for(y=0; y<h; y++){
1667 for(x=0; x<w; x++){ 1667 for(x=0; x<w; x++){
1668 int v, p=0; 1668 int v, p=0;
1669 int /*ll=0, */l=0, lt=0, t=0, rt=0; 1669 int /*ll=0, */l=0, lt=0, t=0, rt=0;
1670 v= src[x + y*stride]; 1670 v= src[x + y*stride];
1686 }*/ 1686 }*/
1687 } 1687 }
1688 if(parent){ 1688 if(parent){
1689 int px= x>>1; 1689 int px= x>>1;
1690 int py= y>>1; 1690 int py= y>>1;
1691 if(px<b->parent->width && py<b->parent->height) 1691 if(px<b->parent->width && py<b->parent->height)
1692 p= parent[px + py*2*stride]; 1692 p= parent[px + py*2*stride];
1693 } 1693 }
1694 if(!(/*ll|*/l|lt|t|rt|p)){ 1694 if(!(/*ll|*/l|lt|t|rt|p)){
1695 if(v){ 1695 if(v){
1696 runs[run_index++]= run; 1696 runs[run_index++]= run;
1707 run= runs[run_index++]; 1707 run= runs[run_index++];
1708 1708
1709 put_symbol2(&s->c, b->state[30], max_index, 0); 1709 put_symbol2(&s->c, b->state[30], max_index, 0);
1710 if(run_index <= max_index) 1710 if(run_index <= max_index)
1711 put_symbol2(&s->c, b->state[1], run, 3); 1711 put_symbol2(&s->c, b->state[1], run, 3);
1712 1712
1713 for(y=0; y<h; y++){ 1713 for(y=0; y<h; y++){
1714 if(s->c.bytestream_end - s->c.bytestream < w*40){ 1714 if(s->c.bytestream_end - s->c.bytestream < w*40){
1715 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); 1715 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
1716 return -1; 1716 return -1;
1717 } 1717 }
1737 }*/ 1737 }*/
1738 } 1738 }
1739 if(parent){ 1739 if(parent){
1740 int px= x>>1; 1740 int px= x>>1;
1741 int py= y>>1; 1741 int py= y>>1;
1742 if(px<b->parent->width && py<b->parent->height) 1742 if(px<b->parent->width && py<b->parent->height)
1743 p= parent[px + py*2*stride]; 1743 p= parent[px + py*2*stride];
1744 } 1744 }
1745 if(/*ll|*/l|lt|t|rt|p){ 1745 if(/*ll|*/l|lt|t|rt|p){
1746 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p)); 1746 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
1747 1747
1770 } 1770 }
1771 } 1771 }
1772 return 0; 1772 return 0;
1773 } 1773 }
1774 1774
1775 static int encode_subband(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){ 1775 static int encode_subband(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
1776 // encode_subband_qtree(s, b, src, parent, stride, orientation); 1776 // encode_subband_qtree(s, b, src, parent, stride, orientation);
1777 // encode_subband_z0run(s, b, src, parent, stride, orientation); 1777 // encode_subband_z0run(s, b, src, parent, stride, orientation);
1778 return encode_subband_c0run(s, b, src, parent, stride, orientation); 1778 return encode_subband_c0run(s, b, src, parent, stride, orientation);
1779 // encode_subband_dzr(s, b, src, parent, stride, orientation); 1779 // encode_subband_dzr(s, b, src, parent, stride, orientation);
1780 } 1780 }
1781 1781
1782 static inline void unpack_coeffs(SnowContext *s, SubBand *b, SubBand * parent, int orientation){ 1782 static inline void unpack_coeffs(SnowContext *s, SubBand *b, SubBand * parent, int orientation){
1783 const int w= b->width; 1783 const int w= b->width;
1784 const int h= b->height; 1784 const int h= b->height;
1785 int x,y; 1785 int x,y;
1786 1786
1787 if(1){ 1787 if(1){
1788 int run, runs; 1788 int run, runs;
1789 x_and_coeff *xc= b->x_coeff; 1789 x_and_coeff *xc= b->x_coeff;
1790 x_and_coeff *prev_xc= NULL; 1790 x_and_coeff *prev_xc= NULL;
1791 x_and_coeff *prev2_xc= xc; 1791 x_and_coeff *prev2_xc= xc;
1804 rt= prev_xc->coeff; 1804 rt= prev_xc->coeff;
1805 } 1805 }
1806 for(x=0; x<w; x++){ 1806 for(x=0; x<w; x++){
1807 int p=0; 1807 int p=0;
1808 const int l= v; 1808 const int l= v;
1809 1809
1810 lt= t; t= rt; 1810 lt= t; t= rt;
1811 1811
1812 if(y){ 1812 if(y){
1813 if(prev_xc->x <= x) 1813 if(prev_xc->x <= x)
1814 prev_xc++; 1814 prev_xc++;
1830 1830
1831 v=get_rac(&s->c, &b->state[0][context]); 1831 v=get_rac(&s->c, &b->state[0][context]);
1832 if(v){ 1832 if(v){
1833 v= 2*(get_symbol2(&s->c, b->state[context + 2], context-4) + 1); 1833 v= 2*(get_symbol2(&s->c, b->state[context + 2], context-4) + 1);
1834 v+=get_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3bA[l&0xFF] + 3*quant3bA[t&0xFF]]); 1834 v+=get_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3bA[l&0xFF] + 3*quant3bA[t&0xFF]]);
1835 1835
1836 xc->x=x; 1836 xc->x=x;
1837 (xc++)->coeff= v; 1837 (xc++)->coeff= v;
1838 } 1838 }
1839 }else{ 1839 }else{
1840 if(!run){ 1840 if(!run){
1841 if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3); 1841 if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
1842 else run= INT_MAX; 1842 else run= INT_MAX;
1843 v= 2*(get_symbol2(&s->c, b->state[0 + 2], 0-4) + 1); 1843 v= 2*(get_symbol2(&s->c, b->state[0 + 2], 0-4) + 1);
1844 v+=get_rac(&s->c, &b->state[0][16 + 1 + 3]); 1844 v+=get_rac(&s->c, &b->state[0][16 + 1 + 3]);
1845 1845
1846 xc->x=x; 1846 xc->x=x;
1847 (xc++)->coeff= v; 1847 (xc++)->coeff= v;
1848 }else{ 1848 }else{
1849 int max_run; 1849 int max_run;
1850 run--; 1850 run--;
1860 } 1860 }
1861 } 1861 }
1862 (xc++)->x= w+1; //end marker 1862 (xc++)->x= w+1; //end marker
1863 prev_xc= prev2_xc; 1863 prev_xc= prev2_xc;
1864 prev2_xc= xc; 1864 prev2_xc= xc;
1865 1865
1866 if(parent_xc){ 1866 if(parent_xc){
1867 if(y&1){ 1867 if(y&1){
1868 while(parent_xc->x != parent->width+1) 1868 while(parent_xc->x != parent->width+1)
1869 parent_xc++; 1869 parent_xc++;
1870 parent_xc++; 1870 parent_xc++;
1884 int y; 1884 int y;
1885 const int qlog= clip(s->qlog + b->qlog, 0, QROOT*16); 1885 const int qlog= clip(s->qlog + b->qlog, 0, QROOT*16);
1886 int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT); 1886 int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
1887 int qadd= (s->qbias*qmul)>>QBIAS_SHIFT; 1887 int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
1888 int new_index = 0; 1888 int new_index = 0;
1889 1889
1890 START_TIMER 1890 START_TIMER
1891 1891
1892 if(b->buf == s->spatial_dwt_buffer || s->qlog == LOSSLESS_QLOG){ 1892 if(b->buf == s->spatial_dwt_buffer || s->qlog == LOSSLESS_QLOG){
1893 qadd= 0; 1893 qadd= 0;
1894 qmul= 1<<QEXPSHIFT; 1894 qmul= 1<<QEXPSHIFT;
1896 1896
1897 /* If we are on the second or later slice, restore our index. */ 1897 /* If we are on the second or later slice, restore our index. */
1898 if (start_y != 0) 1898 if (start_y != 0)
1899 new_index = save_state[0]; 1899 new_index = save_state[0];
1900 1900
1901 1901
1902 for(y=start_y; y<h; y++){ 1902 for(y=start_y; y<h; y++){
1903 int x = 0; 1903 int x = 0;
1904 int v; 1904 int v;
1905 DWTELEM * line = slice_buffer_get_line(sb, y * b->stride_line + b->buf_y_offset) + b->buf_x_offset; 1905 DWTELEM * line = slice_buffer_get_line(sb, y * b->stride_line + b->buf_y_offset) + b->buf_x_offset;
1906 memset(line, 0, b->width*sizeof(DWTELEM)); 1906 memset(line, 0, b->width*sizeof(DWTELEM));
1917 } 1917 }
1918 } 1918 }
1919 if(w > 200 && start_y != 0/*level+1 == s->spatial_decomposition_count*/){ 1919 if(w > 200 && start_y != 0/*level+1 == s->spatial_decomposition_count*/){
1920 STOP_TIMER("decode_subband") 1920 STOP_TIMER("decode_subband")
1921 } 1921 }
1922 1922
1923 /* Save our variables for the next slice. */ 1923 /* Save our variables for the next slice. */
1924 save_state[0] = new_index; 1924 save_state[0] = new_index;
1925 1925
1926 return; 1926 return;
1927 } 1927 }
1928 1928
1929 static void reset_contexts(SnowContext *s){ 1929 static void reset_contexts(SnowContext *s){
1930 int plane_index, level, orientation; 1930 int plane_index, level, orientation;
1941 } 1941 }
1942 1942
1943 static int alloc_blocks(SnowContext *s){ 1943 static int alloc_blocks(SnowContext *s){
1944 int w= -((-s->avctx->width )>>LOG2_MB_SIZE); 1944 int w= -((-s->avctx->width )>>LOG2_MB_SIZE);
1945 int h= -((-s->avctx->height)>>LOG2_MB_SIZE); 1945 int h= -((-s->avctx->height)>>LOG2_MB_SIZE);
1946 1946
1947 s->b_width = w; 1947 s->b_width = w;
1948 s->b_height= h; 1948 s->b_height= h;
1949 1949
1950 s->block= av_mallocz(w * h * sizeof(BlockNode) << (s->block_max_depth*2)); 1950 s->block= av_mallocz(w * h * sizeof(BlockNode) << (s->block_max_depth*2));
1951 return 0; 1951 return 0;
1952 } 1952 }
1953 1953
1954 static inline void copy_rac_state(RangeCoder *d, RangeCoder *s){ 1954 static inline void copy_rac_state(RangeCoder *d, RangeCoder *s){
1997 const int rem_depth= s->block_max_depth - level; 1997 const int rem_depth= s->block_max_depth - level;
1998 const int index= (x + y*w) << rem_depth; 1998 const int index= (x + y*w) << rem_depth;
1999 const int block_w= 1<<rem_depth; 1999 const int block_w= 1<<rem_depth;
2000 BlockNode block; 2000 BlockNode block;
2001 int i,j; 2001 int i,j;
2002 2002
2003 block.color[0]= l; 2003 block.color[0]= l;
2004 block.color[1]= cb; 2004 block.color[1]= cb;
2005 block.color[2]= cr; 2005 block.color[2]= cr;
2006 block.mx= mx; 2006 block.mx= mx;
2007 block.my= my; 2007 block.my= my;
2111 P_LEFT[1]= left->my; 2111 P_LEFT[1]= left->my;
2112 P_TOP [0]= top->mx; 2112 P_TOP [0]= top->mx;
2113 P_TOP [1]= top->my; 2113 P_TOP [1]= top->my;
2114 P_TOPRIGHT[0]= tr->mx; 2114 P_TOPRIGHT[0]= tr->mx;
2115 P_TOPRIGHT[1]= tr->my; 2115 P_TOPRIGHT[1]= tr->my;
2116 2116
2117 last_mv[0][0]= s->block[index].mx; 2117 last_mv[0][0]= s->block[index].mx;
2118 last_mv[0][1]= s->block[index].my; 2118 last_mv[0][1]= s->block[index].my;
2119 last_mv[1][0]= right->mx; 2119 last_mv[1][0]= right->mx;
2120 last_mv[1][1]= right->my; 2120 last_mv[1][1]= right->my;
2121 last_mv[2][0]= bottom->mx; 2121 last_mv[2][0]= bottom->mx;
2122 last_mv[2][1]= bottom->my; 2122 last_mv[2][1]= bottom->my;
2123 2123
2124 s->m.mb_stride=2; 2124 s->m.mb_stride=2;
2125 s->m.mb_x= 2125 s->m.mb_x=
2126 s->m.mb_y= 0; 2126 s->m.mb_y= 0;
2127 s->m.me.skip= 0; 2127 s->m.me.skip= 0;
2128 2128
2129 init_ref(c, current_data, s->last_picture.data, NULL, block_w*x, block_w*y, 0); 2129 init_ref(c, current_data, s->last_picture.data, NULL, block_w*x, block_w*y, 0);
2130 2130
2131 assert(s->m.me. stride == stride); 2131 assert(s->m.me. stride == stride);
2132 assert(s->m.me.uvstride == uvstride); 2132 assert(s->m.me.uvstride == uvstride);
2133 2133
2134 c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp); 2134 c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
2135 c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp); 2135 c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
2136 c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp); 2136 c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
2137 c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_MV; 2137 c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_MV;
2138 2138
2139 c->xmin = - x*block_w - 16+2; 2139 c->xmin = - x*block_w - 16+2;
2140 c->ymin = - y*block_w - 16+2; 2140 c->ymin = - y*block_w - 16+2;
2141 c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-2; 2141 c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-2;
2142 c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-2; 2142 c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-2;
2143 2143
2144 if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift); 2144 if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
2145 if(P_LEFT[1] > (c->ymax<<shift)) P_LEFT[1] = (c->ymax<<shift); 2145 if(P_LEFT[1] > (c->ymax<<shift)) P_LEFT[1] = (c->ymax<<shift);
2146 if(P_TOP[0] > (c->xmax<<shift)) P_TOP[0] = (c->xmax<<shift); 2146 if(P_TOP[0] > (c->xmax<<shift)) P_TOP[0] = (c->xmax<<shift);
2147 if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift); 2147 if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
2148 if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift); 2148 if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
2149 if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); //due to pmx no clip 2149 if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); //due to pmx no clip
2150 if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift); 2150 if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
2158 } else { 2158 } else {
2159 c->pred_x = P_MEDIAN[0]; 2159 c->pred_x = P_MEDIAN[0];
2160 c->pred_y = P_MEDIAN[1]; 2160 c->pred_y = P_MEDIAN[1];
2161 } 2161 }
2162 2162
2163 score= ff_epzs_motion_search(&s->m, &mx, &my, P, 0, /*ref_index*/ 0, last_mv, 2163 score= ff_epzs_motion_search(&s->m, &mx, &my, P, 0, /*ref_index*/ 0, last_mv,
2164 (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w); 2164 (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
2165 2165
2166 assert(mx >= c->xmin); 2166 assert(mx >= c->xmin);
2167 assert(mx <= c->xmax); 2167 assert(mx <= c->xmax);
2168 assert(my >= c->ymin); 2168 assert(my >= c->ymin);
2169 assert(my <= c->ymax); 2169 assert(my <= c->ymax);
2170 2170
2171 score= s->m.me.sub_motion_search(&s->m, &mx, &my, score, 0, 0, level-LOG2_MB_SIZE+4, block_w); 2171 score= s->m.me.sub_motion_search(&s->m, &mx, &my, score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
2172 score= ff_get_mb_score(&s->m, mx, my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0); 2172 score= ff_get_mb_score(&s->m, mx, my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
2173 //FIXME if mb_cmp != SSE then intra cant be compared currently and mb_penalty vs. lambda2 2173 //FIXME if mb_cmp != SSE then intra cant be compared currently and mb_penalty vs. lambda2
2174 2174
2175 // subpel search 2175 // subpel search
2176 pc= s->c; 2176 pc= s->c;
2177 pc.bytestream_start= 2177 pc.bytestream_start=
2178 pc.bytestream= p_buffer; //FIXME end/start? and at the other stoo 2178 pc.bytestream= p_buffer; //FIXME end/start? and at the other stoo
2179 memcpy(p_state, s->block_state, sizeof(s->block_state)); 2179 memcpy(p_state, s->block_state, sizeof(s->block_state));
2191 2191
2192 block_s= block_w*block_w; 2192 block_s= block_w*block_w;
2193 sum = pix_sum(&current_mb[0][0], stride, block_w); 2193 sum = pix_sum(&current_mb[0][0], stride, block_w);
2194 l= (sum + block_s/2)/block_s; 2194 l= (sum + block_s/2)/block_s;
2195 iscore = pix_norm1(&current_mb[0][0], stride, block_w) - 2*l*sum + l*l*block_s; 2195 iscore = pix_norm1(&current_mb[0][0], stride, block_w) - 2*l*sum + l*l*block_s;
2196 2196
2197 block_s= block_w*block_w>>2; 2197 block_s= block_w*block_w>>2;
2198 sum = pix_sum(&current_mb[1][0], uvstride, block_w>>1); 2198 sum = pix_sum(&current_mb[1][0], uvstride, block_w>>1);
2199 cb= (sum + block_s/2)/block_s; 2199 cb= (sum + block_s/2)/block_s;
2200 // iscore += pix_norm1(&current_mb[1][0], uvstride, block_w>>1) - 2*cb*sum + cb*cb*block_s; 2200 // iscore += pix_norm1(&current_mb[1][0], uvstride, block_w>>1) - 2*cb*sum + cb*cb*block_s;
2201 sum = pix_sum(&current_mb[2][0], uvstride, block_w>>1); 2201 sum = pix_sum(&current_mb[2][0], uvstride, block_w>>1);
2230 if (vard <= 64 || vard < varc) 2230 if (vard <= 64 || vard < varc)
2231 c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc); 2231 c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
2232 else 2232 else
2233 c->scene_change_score+= s->m.qscale; 2233 c->scene_change_score+= s->m.qscale;
2234 } 2234 }
2235 2235
2236 if(level!=s->block_max_depth){ 2236 if(level!=s->block_max_depth){
2237 put_rac(&s->c, &s->block_state[4 + s_context], 0); 2237 put_rac(&s->c, &s->block_state[4 + s_context], 0);
2238 score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0); 2238 score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0);
2239 score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0); 2239 score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0);
2240 score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1); 2240 score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1);
2241 score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1); 2241 score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1);
2242 score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead 2242 score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead
2243 2243
2244 if(score2 < score && score2 < iscore) 2244 if(score2 < score && score2 < iscore)
2245 return score2; 2245 return score2;
2246 } 2246 }
2247 2247
2248 if(iscore < score){ 2248 if(iscore < score){
2249 memcpy(pbbak, i_buffer, i_len); 2249 memcpy(pbbak, i_buffer, i_len);
2250 s->c= ic; 2250 s->c= ic;
2251 s->c.bytestream_start= pbbak_start; 2251 s->c.bytestream_start= pbbak_start;
2252 s->c.bytestream= pbbak + i_len; 2252 s->c.bytestream= pbbak + i_len;
2279 BlockNode *left = x ? &s->block[index-1] : &null_block; 2279 BlockNode *left = x ? &s->block[index-1] : &null_block;
2280 BlockNode *top = y ? &s->block[index-w] : &null_block; 2280 BlockNode *top = y ? &s->block[index-w] : &null_block;
2281 BlockNode *tl = y && x ? &s->block[index-w-1] : left; 2281 BlockNode *tl = y && x ? &s->block[index-w-1] : left;
2282 BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt 2282 BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
2283 int s_context= 2*left->level + 2*top->level + tl->level + tr->level; 2283 int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
2284 2284
2285 if(s->keyframe){ 2285 if(s->keyframe){
2286 set_blocks(s, level, x, y, null_block.color[0], null_block.color[1], null_block.color[2], null_block.mx, null_block.my, BLOCK_INTRA); 2286 set_blocks(s, level, x, y, null_block.color[0], null_block.color[1], null_block.color[2], null_block.mx, null_block.my, BLOCK_INTRA);
2287 return; 2287 return;
2288 } 2288 }
2289 2289
2294 int cr= left->color[2]; 2294 int cr= left->color[2];
2295 int mx= mid_pred(left->mx, top->mx, tr->mx); 2295 int mx= mid_pred(left->mx, top->mx, tr->mx);
2296 int my= mid_pred(left->my, top->my, tr->my); 2296 int my= mid_pred(left->my, top->my, tr->my);
2297 int mx_context= av_log2(2*ABS(left->mx - top->mx)) + 0*av_log2(2*ABS(tr->mx - top->mx)); 2297 int mx_context= av_log2(2*ABS(left->mx - top->mx)) + 0*av_log2(2*ABS(tr->mx - top->mx));
2298 int my_context= av_log2(2*ABS(left->my - top->my)) + 0*av_log2(2*ABS(tr->my - top->my)); 2298 int my_context= av_log2(2*ABS(left->my - top->my)) + 0*av_log2(2*ABS(tr->my - top->my));
2299 2299
2300 type= get_rac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0; 2300 type= get_rac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0;
2301 2301
2302 if(type){ 2302 if(type){
2303 l += get_symbol(&s->c, &s->block_state[32], 1); 2303 l += get_symbol(&s->c, &s->block_state[32], 1);
2304 cb+= get_symbol(&s->c, &s->block_state[64], 1); 2304 cb+= get_symbol(&s->c, &s->block_state[64], 1);
2363 2363
2364 // if(b_w==16) am= 8*(a1+a2); 2364 // if(b_w==16) am= 8*(a1+a2);
2365 2365
2366 if(dx<8) am = (32*a2*( 8-dx) + am* dx + 128)>>8; 2366 if(dx<8) am = (32*a2*( 8-dx) + am* dx + 128)>>8;
2367 else am = ( am*(16-dx) + 32*a3*(dx-8) + 128)>>8; 2367 else am = ( am*(16-dx) + 32*a3*(dx-8) + 128)>>8;
2368 2368
2369 /* FIXME Try increasing tmp buffer to 16 bits and not clipping here. Should give marginally better results. - Robert*/ 2369 /* FIXME Try increasing tmp buffer to 16 bits and not clipping here. Should give marginally better results. - Robert*/
2370 if(am&(~255)) am= ~(am>>31); 2370 if(am&(~255)) am= ~(am>>31);
2371 2371
2372 tmp[x] = am; 2372 tmp[x] = am;
2373 2373
2374 /* if (dx< 4) tmp[x + y*stride]= (16*a1*( 4-dx) + aL* dx + 32)>>6; 2374 /* if (dx< 4) tmp[x + y*stride]= (16*a1*( 4-dx) + aL* dx + 32)>>6;
2375 else if(dx< 8) tmp[x + y*stride]= ( aL*( 8-dx) + am*(dx- 4) + 32)>>6; 2375 else if(dx< 8) tmp[x + y*stride]= ( aL*( 8-dx) + am*(dx- 4) + 32)>>6;
2376 else if(dx<12) tmp[x + y*stride]= ( am*(12-dx) + aR*(dx- 8) + 32)>>6; 2376 else if(dx<12) tmp[x + y*stride]= ( am*(12-dx) + aR*(dx- 8) + 32)>>6;
2378 } 2378 }
2379 tmp += stride; 2379 tmp += stride;
2380 src += stride; 2380 src += stride;
2381 } 2381 }
2382 tmp -= (b_h+5)*stride; 2382 tmp -= (b_h+5)*stride;
2383 2383
2384 for(y=0; y < b_h; y++){ 2384 for(y=0; y < b_h; y++){
2385 for(x=0; x < b_w; x++){ 2385 for(x=0; x < b_w; x++){
2386 int a0= tmp[x + 0*stride]; 2386 int a0= tmp[x + 0*stride];
2387 int a1= tmp[x + 1*stride]; 2387 int a1= tmp[x + 1*stride];
2388 int a2= tmp[x + 2*stride]; 2388 int a2= tmp[x + 2*stride];
2391 int a5= tmp[x + 5*stride]; 2391 int a5= tmp[x + 5*stride];
2392 int am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5); 2392 int am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
2393 // int am= 18*(a2+a3) - 2*(a1+a4); 2393 // int am= 18*(a2+a3) - 2*(a1+a4);
2394 /* int aL= (-7*a0 + 105*a1 + 35*a2 - 5*a3)>>3; 2394 /* int aL= (-7*a0 + 105*a1 + 35*a2 - 5*a3)>>3;
2395 int aR= (-7*a3 + 105*a2 + 35*a1 - 5*a0)>>3;*/ 2395 int aR= (-7*a3 + 105*a2 + 35*a1 - 5*a0)>>3;*/
2396 2396
2397 // if(b_w==16) am= 8*(a1+a2); 2397 // if(b_w==16) am= 8*(a1+a2);
2398 2398
2399 if(dy<8) am = (32*a2*( 8-dy) + am* dy + 128)>>8; 2399 if(dy<8) am = (32*a2*( 8-dy) + am* dy + 128)>>8;
2400 else am = ( am*(16-dy) + 32*a3*(dy-8) + 128)>>8; 2400 else am = ( am*(16-dy) + 32*a3*(dy-8) + 128)>>8;
2401 2401
2402 if(am&(~255)) am= ~(am>>31); 2402 if(am&(~255)) am= ~(am>>31);
2403 2403
2404 dst[x] = am; 2404 dst[x] = am;
2405 /* if (dy< 4) tmp[x + y*stride]= (16*a1*( 4-dy) + aL* dy + 32)>>6; 2405 /* if (dy< 4) tmp[x + y*stride]= (16*a1*( 4-dy) + aL* dy + 32)>>6;
2406 else if(dy< 8) tmp[x + y*stride]= ( aL*( 8-dy) + am*(dy- 4) + 32)>>6; 2406 else if(dy< 8) tmp[x + y*stride]= ( aL*( 8-dy) + am*(dy- 4) + 32)>>6;
2407 else if(dy<12) tmp[x + y*stride]= ( am*(12-dy) + aR*(dy- 8) + 32)>>6; 2407 else if(dy<12) tmp[x + y*stride]= ( am*(12-dy) + aR*(dy- 8) + 32)>>6;
2408 else tmp[x + y*stride]= ( aR*(16-dy) + 16*a2*(dy-12) + 32)>>6;*/ 2408 else tmp[x + y*stride]= ( aR*(16-dy) + 16*a2*(dy-12) + 32)>>6;*/
2471 const int b_stride= b_width; 2471 const int b_stride= b_width;
2472 BlockNode *lt= &s->block[b_x + b_y*b_stride]; 2472 BlockNode *lt= &s->block[b_x + b_y*b_stride];
2473 BlockNode *rt= lt+1; 2473 BlockNode *rt= lt+1;
2474 BlockNode *lb= lt+b_stride; 2474 BlockNode *lb= lt+b_stride;
2475 BlockNode *rb= lb+1; 2475 BlockNode *rb= lb+1;
2476 uint8_t *block[4]; 2476 uint8_t *block[4];
2477 int tmp_step= src_stride >= 7*MB_SIZE ? MB_SIZE : MB_SIZE*src_stride; 2477 int tmp_step= src_stride >= 7*MB_SIZE ? MB_SIZE : MB_SIZE*src_stride;
2478 uint8_t tmp[src_stride*7*MB_SIZE]; //FIXME align 2478 uint8_t tmp[src_stride*7*MB_SIZE]; //FIXME align
2479 uint8_t *ptmp; 2479 uint8_t *ptmp;
2480 int x,y; 2480 int x,y;
2481 2481
2491 rt= rb; 2491 rt= rb;
2492 }else if(b_y + 1 >= b_height){ 2492 }else if(b_y + 1 >= b_height){
2493 lb= lt; 2493 lb= lt;
2494 rb= rt; 2494 rb= rt;
2495 } 2495 }
2496 2496
2497 if(src_x<0){ //FIXME merge with prev & always round internal width upto *16 2497 if(src_x<0){ //FIXME merge with prev & always round internal width upto *16
2498 obmc -= src_x; 2498 obmc -= src_x;
2499 b_w += src_x; 2499 b_w += src_x;
2500 src_x=0; 2500 src_x=0;
2501 }else if(src_x + b_w > w){ 2501 }else if(src_x + b_w > w){
2506 b_h += src_y; 2506 b_h += src_y;
2507 src_y=0; 2507 src_y=0;
2508 }else if(src_y + b_h> h){ 2508 }else if(src_y + b_h> h){
2509 b_h = h - src_y; 2509 b_h = h - src_y;
2510 } 2510 }
2511 2511
2512 if(b_w<=0 || b_h<=0) return; 2512 if(b_w<=0 || b_h<=0) return;
2513 2513
2514 assert(src_stride > 2*MB_SIZE + 5); 2514 assert(src_stride > 2*MB_SIZE + 5);
2515 // old_dst += src_x + src_y*dst_stride; 2515 // old_dst += src_x + src_y*dst_stride;
2516 dst8+= src_x + src_y*src_stride; 2516 dst8+= src_x + src_y*src_stride;
2517 // src += src_x + src_y*src_stride; 2517 // src += src_x + src_y*src_stride;
2518 2518
2519 ptmp= tmp + 3*tmp_step; 2519 ptmp= tmp + 3*tmp_step;
2520 block[0]= ptmp; 2520 block[0]= ptmp;
2521 ptmp+=tmp_step; 2521 ptmp+=tmp_step;
2522 pred_block(s, block[0], src, tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h); 2522 pred_block(s, block[0], src, tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
2523 2523
2524 if(same_block(lt, rt)){ 2524 if(same_block(lt, rt)){
2525 block[1]= block[0]; 2525 block[1]= block[0];
2526 }else{ 2526 }else{
2527 block[1]= ptmp; 2527 block[1]= ptmp;
2528 ptmp+=tmp_step; 2528 ptmp+=tmp_step;
2529 pred_block(s, block[1], src, tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h); 2529 pred_block(s, block[1], src, tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
2530 } 2530 }
2531 2531
2532 if(same_block(lt, lb)){ 2532 if(same_block(lt, lb)){
2533 block[2]= block[0]; 2533 block[2]= block[0];
2534 }else if(same_block(rt, lb)){ 2534 }else if(same_block(rt, lb)){
2535 block[2]= block[1]; 2535 block[2]= block[1];
2536 }else{ 2536 }else{
2584 } 2584 }
2585 #else 2585 #else
2586 { 2586 {
2587 2587
2588 START_TIMER 2588 START_TIMER
2589 2589
2590 for(y=0; y<b_h; y++){ 2590 for(y=0; y<b_h; y++){
2591 //FIXME ugly missue of obmc_stride 2591 //FIXME ugly missue of obmc_stride
2592 uint8_t *obmc1= obmc + y*obmc_stride; 2592 uint8_t *obmc1= obmc + y*obmc_stride;
2593 uint8_t *obmc2= obmc1+ (obmc_stride>>1); 2593 uint8_t *obmc2= obmc1+ (obmc_stride>>1);
2594 uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1); 2594 uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
2629 const int b_stride= b_width; 2629 const int b_stride= b_width;
2630 BlockNode *lt= &s->block[b_x + b_y*b_stride]; 2630 BlockNode *lt= &s->block[b_x + b_y*b_stride];
2631 BlockNode *rt= lt+1; 2631 BlockNode *rt= lt+1;
2632 BlockNode *lb= lt+b_stride; 2632 BlockNode *lb= lt+b_stride;
2633 BlockNode *rb= lb+1; 2633 BlockNode *rb= lb+1;
2634 uint8_t *block[4]; 2634 uint8_t *block[4];
2635 int tmp_step= src_stride >= 7*MB_SIZE ? MB_SIZE : MB_SIZE*src_stride; 2635 int tmp_step= src_stride >= 7*MB_SIZE ? MB_SIZE : MB_SIZE*src_stride;
2636 uint8_t tmp[src_stride*7*MB_SIZE]; //FIXME align 2636 uint8_t tmp[src_stride*7*MB_SIZE]; //FIXME align
2637 uint8_t *ptmp; 2637 uint8_t *ptmp;
2638 int x,y; 2638 int x,y;
2639 2639
2649 rt= rb; 2649 rt= rb;
2650 }else if(b_y + 1 >= b_height){ 2650 }else if(b_y + 1 >= b_height){
2651 lb= lt; 2651 lb= lt;
2652 rb= rt; 2652 rb= rt;
2653 } 2653 }
2654 2654
2655 if(src_x<0){ //FIXME merge with prev & always round internal width upto *16 2655 if(src_x<0){ //FIXME merge with prev & always round internal width upto *16
2656 obmc -= src_x; 2656 obmc -= src_x;
2657 b_w += src_x; 2657 b_w += src_x;
2658 src_x=0; 2658 src_x=0;
2659 }else if(src_x + b_w > w){ 2659 }else if(src_x + b_w > w){
2664 b_h += src_y; 2664 b_h += src_y;
2665 src_y=0; 2665 src_y=0;
2666 }else if(src_y + b_h> h){ 2666 }else if(src_y + b_h> h){
2667 b_h = h - src_y; 2667 b_h = h - src_y;
2668 } 2668 }
2669 2669
2670 if(b_w<=0 || b_h<=0) return; 2670 if(b_w<=0 || b_h<=0) return;
2671 2671
2672 assert(src_stride > 2*MB_SIZE + 5); 2672 assert(src_stride > 2*MB_SIZE + 5);
2673 dst += src_x + src_y*dst_stride; 2673 dst += src_x + src_y*dst_stride;
2674 dst8+= src_x + src_y*src_stride; 2674 dst8+= src_x + src_y*src_stride;
2675 // src += src_x + src_y*src_stride; 2675 // src += src_x + src_y*src_stride;
2676 2676
2677 ptmp= tmp + 3*tmp_step; 2677 ptmp= tmp + 3*tmp_step;
2678 block[0]= ptmp; 2678 block[0]= ptmp;
2679 ptmp+=tmp_step; 2679 ptmp+=tmp_step;
2680 pred_block(s, block[0], src, tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h); 2680 pred_block(s, block[0], src, tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
2681 2681
2682 if(same_block(lt, rt)){ 2682 if(same_block(lt, rt)){
2683 block[1]= block[0]; 2683 block[1]= block[0];
2684 }else{ 2684 }else{
2685 block[1]= ptmp; 2685 block[1]= ptmp;
2686 ptmp+=tmp_step; 2686 ptmp+=tmp_step;
2687 pred_block(s, block[1], src, tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h); 2687 pred_block(s, block[1], src, tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
2688 } 2688 }
2689 2689
2690 if(same_block(lt, lb)){ 2690 if(same_block(lt, lb)){
2691 block[2]= block[0]; 2691 block[2]= block[0];
2692 }else if(same_block(rt, lb)){ 2692 }else if(same_block(rt, lb)){
2693 block[2]= block[1]; 2693 block[2]= block[1];
2694 }else{ 2694 }else{
2750 for(x=0; x<b_w; x++){ 2750 for(x=0; x<b_w; x++){
2751 int v= obmc1[x] * block[3][x + y*src_stride] 2751 int v= obmc1[x] * block[3][x + y*src_stride]
2752 +obmc2[x] * block[2][x + y*src_stride] 2752 +obmc2[x] * block[2][x + y*src_stride]
2753 +obmc3[x] * block[1][x + y*src_stride] 2753 +obmc3[x] * block[1][x + y*src_stride]
2754 +obmc4[x] * block[0][x + y*src_stride]; 2754 +obmc4[x] * block[0][x + y*src_stride];
2755 2755
2756 v <<= 8 - LOG2_OBMC_MAX; 2756 v <<= 8 - LOG2_OBMC_MAX;
2757 if(FRAC_BITS != 8){ 2757 if(FRAC_BITS != 8){
2758 v += 1<<(7 - FRAC_BITS); 2758 v += 1<<(7 - FRAC_BITS);
2759 v >>= 8 - FRAC_BITS; 2759 v >>= 8 - FRAC_BITS;
2760 } 2760 }
2784 uint8_t *ref = s->last_picture.data[plane_index]; 2784 uint8_t *ref = s->last_picture.data[plane_index];
2785 uint8_t *dst8= s->current_picture.data[plane_index]; 2785 uint8_t *dst8= s->current_picture.data[plane_index];
2786 int w= p->width; 2786 int w= p->width;
2787 int h= p->height; 2787 int h= p->height;
2788 START_TIMER 2788 START_TIMER
2789 2789
2790 if(s->keyframe || (s->avctx->debug&512)){ 2790 if(s->keyframe || (s->avctx->debug&512)){
2791 if(mb_y==mb_h) 2791 if(mb_y==mb_h)
2792 return; 2792 return;
2793 2793
2794 if(add){ 2794 if(add){
2818 } 2818 }
2819 } 2819 }
2820 2820
2821 return; 2821 return;
2822 } 2822 }
2823 2823
2824 for(mb_x=0; mb_x<=mb_w; mb_x++){ 2824 for(mb_x=0; mb_x<=mb_w; mb_x++){
2825 START_TIMER 2825 START_TIMER
2826 2826
2827 add_yblock_buffered(s, sb, old_buffer, dst8, ref, obmc, 2827 add_yblock_buffered(s, sb, old_buffer, dst8, ref, obmc,
2828 block_w*mb_x - block_w/2, 2828 block_w*mb_x - block_w/2,
2829 block_w*mb_y - block_w/2, 2829 block_w*mb_y - block_w/2,
2830 block_w, block_w, 2830 block_w, block_w,
2831 w, h, 2831 w, h,
2832 w, ref_stride, obmc_stride, 2832 w, ref_stride, obmc_stride,
2833 mb_x - 1, mb_y - 1, 2833 mb_x - 1, mb_y - 1,
2834 add, plane_index); 2834 add, plane_index);
2835 2835
2836 STOP_TIMER("add_yblock") 2836 STOP_TIMER("add_yblock")
2837 } 2837 }
2838 2838
2839 STOP_TIMER("predict_slice") 2839 STOP_TIMER("predict_slice")
2840 } 2840 }
2841 2841
2842 static always_inline void predict_slice(SnowContext *s, DWTELEM *buf, int plane_index, int add, int mb_y){ 2842 static always_inline void predict_slice(SnowContext *s, DWTELEM *buf, int plane_index, int add, int mb_y){
2843 Plane *p= &s->plane[plane_index]; 2843 Plane *p= &s->plane[plane_index];
2852 uint8_t *ref = s->last_picture.data[plane_index]; 2852 uint8_t *ref = s->last_picture.data[plane_index];
2853 uint8_t *dst8= s->current_picture.data[plane_index]; 2853 uint8_t *dst8= s->current_picture.data[plane_index];
2854 int w= p->width; 2854 int w= p->width;
2855 int h= p->height; 2855 int h= p->height;
2856 START_TIMER 2856 START_TIMER
2857 2857
2858 if(s->keyframe || (s->avctx->debug&512)){ 2858 if(s->keyframe || (s->avctx->debug&512)){
2859 if(mb_y==mb_h) 2859 if(mb_y==mb_h)
2860 return; 2860 return;
2861 2861
2862 if(add){ 2862 if(add){
2876 } 2876 }
2877 } 2877 }
2878 2878
2879 return; 2879 return;
2880 } 2880 }
2881 2881
2882 for(mb_x=0; mb_x<=mb_w; mb_x++){ 2882 for(mb_x=0; mb_x<=mb_w; mb_x++){
2883 START_TIMER 2883 START_TIMER
2884 2884
2885 add_yblock(s, buf, dst8, ref, obmc, 2885 add_yblock(s, buf, dst8, ref, obmc,
2886 block_w*mb_x - block_w/2, 2886 block_w*mb_x - block_w/2,
2887 block_w*mb_y - block_w/2, 2887 block_w*mb_y - block_w/2,
2888 block_w, block_w, 2888 block_w, block_w,
2889 w, h, 2889 w, h,
2890 w, ref_stride, obmc_stride, 2890 w, ref_stride, obmc_stride,
2891 mb_x - 1, mb_y - 1, 2891 mb_x - 1, mb_y - 1,
2892 add, plane_index); 2892 add, plane_index);
2893 2893
2894 STOP_TIMER("add_yblock") 2894 STOP_TIMER("add_yblock")
2895 } 2895 }
2896 2896
2897 STOP_TIMER("predict_slice") 2897 STOP_TIMER("predict_slice")
2898 } 2898 }
2899 2899
2900 static always_inline void predict_plane(SnowContext *s, DWTELEM *buf, int plane_index, int add){ 2900 static always_inline void predict_plane(SnowContext *s, DWTELEM *buf, int plane_index, int add){
2901 const int mb_h= s->b_height << s->block_max_depth; 2901 const int mb_h= s->b_height << s->block_max_depth;
2912 const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT); 2912 const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
2913 int x,y, thres1, thres2; 2913 int x,y, thres1, thres2;
2914 // START_TIMER 2914 // START_TIMER
2915 2915
2916 if(s->qlog == LOSSLESS_QLOG) return; 2916 if(s->qlog == LOSSLESS_QLOG) return;
2917 2917
2918 bias= bias ? 0 : (3*qmul)>>3; 2918 bias= bias ? 0 : (3*qmul)>>3;
2919 thres1= ((qmul - bias)>>QEXPSHIFT) - 1; 2919 thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
2920 thres2= 2*thres1; 2920 thres2= 2*thres1;
2921 2921
2922 if(!bias){ 2922 if(!bias){
2923 for(y=0; y<h; y++){ 2923 for(y=0; y<h; y++){
2924 for(x=0; x<w; x++){ 2924 for(x=0; x<w; x++){
2925 int i= src[x + y*stride]; 2925 int i= src[x + y*stride];
2926 2926
2927 if((unsigned)(i+thres1) > thres2){ 2927 if((unsigned)(i+thres1) > thres2){
2928 if(i>=0){ 2928 if(i>=0){
2929 i<<= QEXPSHIFT; 2929 i<<= QEXPSHIFT;
2930 i/= qmul; //FIXME optimize 2930 i/= qmul; //FIXME optimize
2931 src[x + y*stride]= i; 2931 src[x + y*stride]= i;
2940 } 2940 }
2941 } 2941 }
2942 }else{ 2942 }else{
2943 for(y=0; y<h; y++){ 2943 for(y=0; y<h; y++){
2944 for(x=0; x<w; x++){ 2944 for(x=0; x<w; x++){
2945 int i= src[x + y*stride]; 2945 int i= src[x + y*stride];
2946 2946
2947 if((unsigned)(i+thres1) > thres2){ 2947 if((unsigned)(i+thres1) > thres2){
2948 if(i>=0){ 2948 if(i>=0){
2949 i<<= QEXPSHIFT; 2949 i<<= QEXPSHIFT;
2950 i= (i + bias) / qmul; //FIXME optimize 2950 i= (i + bias) / qmul; //FIXME optimize
2951 src[x + y*stride]= i; 2951 src[x + y*stride]= i;
2970 const int qlog= clip(s->qlog + b->qlog, 0, QROOT*16); 2970 const int qlog= clip(s->qlog + b->qlog, 0, QROOT*16);
2971 const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT); 2971 const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
2972 const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT; 2972 const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
2973 int x,y; 2973 int x,y;
2974 START_TIMER 2974 START_TIMER
2975 2975
2976 if(s->qlog == LOSSLESS_QLOG) return; 2976 if(s->qlog == LOSSLESS_QLOG) return;
2977 2977
2978 for(y=start_y; y<end_y; y++){ 2978 for(y=start_y; y<end_y; y++){
2979 // DWTELEM * line = slice_buffer_get_line_from_address(sb, src + (y * stride)); 2979 // DWTELEM * line = slice_buffer_get_line_from_address(sb, src + (y * stride));
2980 DWTELEM * line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset; 2980 DWTELEM * line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
2981 for(x=0; x<w; x++){ 2981 for(x=0; x<w; x++){
2982 int i= line[x]; 2982 int i= line[x];
2998 const int qlog= clip(s->qlog + b->qlog, 0, QROOT*16); 2998 const int qlog= clip(s->qlog + b->qlog, 0, QROOT*16);
2999 const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT); 2999 const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
3000 const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT; 3000 const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
3001 int x,y; 3001 int x,y;
3002 START_TIMER 3002 START_TIMER
3003 3003
3004 if(s->qlog == LOSSLESS_QLOG) return; 3004 if(s->qlog == LOSSLESS_QLOG) return;
3005 3005
3006 for(y=0; y<h; y++){ 3006 for(y=0; y<h; y++){
3007 for(x=0; x<w; x++){ 3007 for(x=0; x<w; x++){
3008 int i= src[x + y*stride]; 3008 int i= src[x + y*stride];
3009 if(i<0){ 3009 if(i<0){
3010 src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias 3010 src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
3020 3020
3021 static void decorrelate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){ 3021 static void decorrelate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
3022 const int w= b->width; 3022 const int w= b->width;
3023 const int h= b->height; 3023 const int h= b->height;
3024 int x,y; 3024 int x,y;
3025 3025
3026 for(y=h-1; y>=0; y--){ 3026 for(y=h-1; y>=0; y--){
3027 for(x=w-1; x>=0; x--){ 3027 for(x=w-1; x>=0; x--){
3028 int i= x + y*stride; 3028 int i= x + y*stride;
3029 3029
3030 if(x){ 3030 if(x){
3031 if(use_median){ 3031 if(use_median){
3032 if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]); 3032 if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
3033 else src[i] -= src[i - 1]; 3033 else src[i] -= src[i - 1];
3034 }else{ 3034 }else{
3043 } 3043 }
3044 3044
3045 static void correlate_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median, int start_y, int end_y){ 3045 static void correlate_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median, int start_y, int end_y){
3046 const int w= b->width; 3046 const int w= b->width;
3047 int x,y; 3047 int x,y;
3048 3048
3049 // START_TIMER 3049 // START_TIMER
3050 3050
3051 DWTELEM * line; 3051 DWTELEM * line;
3052 DWTELEM * prev; 3052 DWTELEM * prev;
3053 3053
3054 if (start_y != 0) 3054 if (start_y != 0)
3055 line = slice_buffer_get_line(sb, ((start_y - 1) * b->stride_line) + b->buf_y_offset) + b->buf_x_offset; 3055 line = slice_buffer_get_line(sb, ((start_y - 1) * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
3056 3056
3057 for(y=start_y; y<end_y; y++){ 3057 for(y=start_y; y<end_y; y++){
3058 prev = line; 3058 prev = line;
3059 // line = slice_buffer_get_line_from_address(sb, src + (y * stride)); 3059 // line = slice_buffer_get_line_from_address(sb, src + (y * stride));
3060 line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset; 3060 line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
3061 for(x=0; x<w; x++){ 3061 for(x=0; x<w; x++){
3070 }else{ 3070 }else{
3071 if(y) line[x] += prev[x]; 3071 if(y) line[x] += prev[x];
3072 } 3072 }
3073 } 3073 }
3074 } 3074 }
3075 3075
3076 // STOP_TIMER("correlate") 3076 // STOP_TIMER("correlate")
3077 } 3077 }
3078 3078
3079 static void correlate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){ 3079 static void correlate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
3080 const int w= b->width; 3080 const int w= b->width;
3081 const int h= b->height; 3081 const int h= b->height;
3082 int x,y; 3082 int x,y;
3083 3083
3084 for(y=0; y<h; y++){ 3084 for(y=0; y<h; y++){
3085 for(x=0; x<w; x++){ 3085 for(x=0; x<w; x++){
3086 int i= x + y*stride; 3086 int i= x + y*stride;
3087 3087
3088 if(x){ 3088 if(x){
3089 if(use_median){ 3089 if(use_median){
3090 if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]); 3090 if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
3091 else src[i] += src[i - 1]; 3091 else src[i] += src[i - 1];
3092 }else{ 3092 }else{
3100 } 3100 }
3101 } 3101 }
3102 3102
3103 static void encode_header(SnowContext *s){ 3103 static void encode_header(SnowContext *s){
3104 int plane_index, level, orientation; 3104 int plane_index, level, orientation;
3105 uint8_t kstate[32]; 3105 uint8_t kstate[32];
3106 3106
3107 memset(kstate, MID_STATE, sizeof(kstate)); 3107 memset(kstate, MID_STATE, sizeof(kstate));
3108 3108
3109 put_rac(&s->c, kstate, s->keyframe); 3109 put_rac(&s->c, kstate, s->keyframe);
3110 if(s->keyframe || s->always_reset) 3110 if(s->keyframe || s->always_reset)
3111 reset_contexts(s); 3111 reset_contexts(s);
3112 if(s->keyframe){ 3112 if(s->keyframe){
3129 } 3129 }
3130 } 3130 }
3131 } 3131 }
3132 } 3132 }
3133 put_symbol(&s->c, s->header_state, s->spatial_decomposition_type, 0); 3133 put_symbol(&s->c, s->header_state, s->spatial_decomposition_type, 0);
3134 put_symbol(&s->c, s->header_state, s->qlog, 1); 3134 put_symbol(&s->c, s->header_state, s->qlog, 1);
3135 put_symbol(&s->c, s->header_state, s->mv_scale, 0); 3135 put_symbol(&s->c, s->header_state, s->mv_scale, 0);
3136 put_symbol(&s->c, s->header_state, s->qbias, 1); 3136 put_symbol(&s->c, s->header_state, s->qbias, 1);
3137 put_symbol(&s->c, s->header_state, s->block_max_depth, 0); 3137 put_symbol(&s->c, s->header_state, s->block_max_depth, 0);
3138 } 3138 }
3139 3139
3140 static int decode_header(SnowContext *s){ 3140 static int decode_header(SnowContext *s){
3141 int plane_index, level, orientation; 3141 int plane_index, level, orientation;
3142 uint8_t kstate[32]; 3142 uint8_t kstate[32];
3143 3143
3144 memset(kstate, MID_STATE, sizeof(kstate)); 3144 memset(kstate, MID_STATE, sizeof(kstate));
3145 3145
3146 s->keyframe= get_rac(&s->c, kstate); 3146 s->keyframe= get_rac(&s->c, kstate);
3147 if(s->keyframe || s->always_reset) 3147 if(s->keyframe || s->always_reset)
3148 reset_contexts(s); 3148 reset_contexts(s);
3149 if(s->keyframe){ 3149 if(s->keyframe){
3172 s->plane[plane_index].band[level][orientation].qlog= q; 3172 s->plane[plane_index].band[level][orientation].qlog= q;
3173 } 3173 }
3174 } 3174 }
3175 } 3175 }
3176 } 3176 }
3177 3177
3178 s->spatial_decomposition_type= get_symbol(&s->c, s->header_state, 0); 3178 s->spatial_decomposition_type= get_symbol(&s->c, s->header_state, 0);
3179 if(s->spatial_decomposition_type > 2){ 3179 if(s->spatial_decomposition_type > 2){
3180 av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported", s->spatial_decomposition_type); 3180 av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported", s->spatial_decomposition_type);
3181 return -1; 3181 return -1;
3182 } 3182 }
3183 3183
3184 s->qlog= get_symbol(&s->c, s->header_state, 1); 3184 s->qlog= get_symbol(&s->c, s->header_state, 1);
3185 s->mv_scale= get_symbol(&s->c, s->header_state, 0); 3185 s->mv_scale= get_symbol(&s->c, s->header_state, 0);
3186 s->qbias= get_symbol(&s->c, s->header_state, 1); 3186 s->qbias= get_symbol(&s->c, s->header_state, 1);
3187 s->block_max_depth= get_symbol(&s->c, s->header_state, 0); 3187 s->block_max_depth= get_symbol(&s->c, s->header_state, 0);
3188 if(s->block_max_depth > 1){ 3188 if(s->block_max_depth > 1){
3198 int i; 3198 int i;
3199 double v=128; 3199 double v=128;
3200 3200
3201 for(i=0; i<QROOT; i++){ 3201 for(i=0; i<QROOT; i++){
3202 qexp[i]= lrintf(v); 3202 qexp[i]= lrintf(v);
3203 v *= pow(2, 1.0 / QROOT); 3203 v *= pow(2, 1.0 / QROOT);
3204 } 3204 }
3205 } 3205 }
3206 3206
3207 static int common_init(AVCodecContext *avctx){ 3207 static int common_init(AVCodecContext *avctx){
3208 SnowContext *s = avctx->priv_data; 3208 SnowContext *s = avctx->priv_data;
3209 int width, height; 3209 int width, height;
3210 int level, orientation, plane_index, dec; 3210 int level, orientation, plane_index, dec;
3211 3211
3212 s->avctx= avctx; 3212 s->avctx= avctx;
3213 3213
3214 dsputil_init(&s->dsp, avctx); 3214 dsputil_init(&s->dsp, avctx);
3215 3215
3216 #define mcf(dx,dy)\ 3216 #define mcf(dx,dy)\
3217 s->dsp.put_qpel_pixels_tab [0][dy+dx/4]=\ 3217 s->dsp.put_qpel_pixels_tab [0][dy+dx/4]=\
3218 s->dsp.put_no_rnd_qpel_pixels_tab[0][dy+dx/4]=\ 3218 s->dsp.put_no_rnd_qpel_pixels_tab[0][dy+dx/4]=\
3254 if(!qexp[0]) 3254 if(!qexp[0])
3255 init_qexp(); 3255 init_qexp();
3256 3256
3257 dec= s->spatial_decomposition_count= 5; 3257 dec= s->spatial_decomposition_count= 5;
3258 s->spatial_decomposition_type= avctx->prediction_method; //FIXME add decorrelator type r transform_type 3258 s->spatial_decomposition_type= avctx->prediction_method; //FIXME add decorrelator type r transform_type
3259 3259
3260 s->chroma_h_shift= 1; //FIXME XXX 3260 s->chroma_h_shift= 1; //FIXME XXX
3261 s->chroma_v_shift= 1; 3261 s->chroma_v_shift= 1;
3262 3262
3263 // dec += FFMAX(s->chroma_h_shift, s->chroma_v_shift); 3263 // dec += FFMAX(s->chroma_h_shift, s->chroma_v_shift);
3264 3264
3265 width= s->avctx->width; 3265 width= s->avctx->width;
3266 height= s->avctx->height; 3266 height= s->avctx->height;
3267 3267
3268 s->spatial_dwt_buffer= av_mallocz(width*height*sizeof(DWTELEM)); 3268 s->spatial_dwt_buffer= av_mallocz(width*height*sizeof(DWTELEM));
3269 3269
3270 s->mv_scale= (s->avctx->flags & CODEC_FLAG_QPEL) ? 2 : 4; 3270 s->mv_scale= (s->avctx->flags & CODEC_FLAG_QPEL) ? 2 : 4;
3271 s->block_max_depth= (s->avctx->flags & CODEC_FLAG_4MV) ? 1 : 0; 3271 s->block_max_depth= (s->avctx->flags & CODEC_FLAG_4MV) ? 1 : 0;
3272 3272
3273 for(plane_index=0; plane_index<3; plane_index++){ 3273 for(plane_index=0; plane_index<3; plane_index++){
3274 int w= s->avctx->width; 3274 int w= s->avctx->width;
3275 int h= s->avctx->height; 3275 int h= s->avctx->height;
3276 3276
3277 if(plane_index){ 3277 if(plane_index){
3278 w>>= s->chroma_h_shift; 3278 w>>= s->chroma_h_shift;
3282 s->plane[plane_index].height= h; 3282 s->plane[plane_index].height= h;
3283 //av_log(NULL, AV_LOG_DEBUG, "%d %d\n", w, h); 3283 //av_log(NULL, AV_LOG_DEBUG, "%d %d\n", w, h);
3284 for(level=s->spatial_decomposition_count-1; level>=0; level--){ 3284 for(level=s->spatial_decomposition_count-1; level>=0; level--){
3285 for(orientation=level ? 1 : 0; orientation<4; orientation++){ 3285 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3286 SubBand *b= &s->plane[plane_index].band[level][orientation]; 3286 SubBand *b= &s->plane[plane_index].band[level][orientation];
3287 3287
3288 b->buf= s->spatial_dwt_buffer; 3288 b->buf= s->spatial_dwt_buffer;
3289 b->level= level; 3289 b->level= level;
3290 b->stride= s->plane[plane_index].width << (s->spatial_decomposition_count - level); 3290 b->stride= s->plane[plane_index].width << (s->spatial_decomposition_count - level);
3291 b->width = (w + !(orientation&1))>>1; 3291 b->width = (w + !(orientation&1))>>1;
3292 b->height= (h + !(orientation>1))>>1; 3292 b->height= (h + !(orientation>1))>>1;
3293 3293
3294 b->stride_line = 1 << (s->spatial_decomposition_count - level); 3294 b->stride_line = 1 << (s->spatial_decomposition_count - level);
3295 b->buf_x_offset = 0; 3295 b->buf_x_offset = 0;
3296 b->buf_y_offset = 0; 3296 b->buf_y_offset = 0;
3297 3297
3298 if(orientation&1){ 3298 if(orientation&1){
3299 b->buf += (w+1)>>1; 3299 b->buf += (w+1)>>1;
3300 b->buf_x_offset = (w+1)>>1; 3300 b->buf_x_offset = (w+1)>>1;
3301 } 3301 }
3302 if(orientation>1){ 3302 if(orientation>1){
3303 b->buf += b->stride>>1; 3303 b->buf += b->stride>>1;
3304 b->buf_y_offset = b->stride_line >> 1; 3304 b->buf_y_offset = b->stride_line >> 1;
3305 } 3305 }
3306 3306
3307 if(level) 3307 if(level)
3308 b->parent= &s->plane[plane_index].band[level-1][orientation]; 3308 b->parent= &s->plane[plane_index].band[level-1][orientation];
3309 b->x_coeff=av_mallocz(((b->width+1) * b->height+1)*sizeof(x_and_coeff)); 3309 b->x_coeff=av_mallocz(((b->width+1) * b->height+1)*sizeof(x_and_coeff));
3310 } 3310 }
3311 w= (w+1)>>1; 3311 w= (w+1)>>1;
3312 h= (h+1)>>1; 3312 h= (h+1)>>1;
3313 } 3313 }
3314 } 3314 }
3315 3315
3316 reset_contexts(s); 3316 reset_contexts(s);
3317 /* 3317 /*
3318 width= s->width= avctx->width; 3318 width= s->width= avctx->width;
3319 height= s->height= avctx->height; 3319 height= s->height= avctx->height;
3320 3320
3321 assert(width && height); 3321 assert(width && height);
3322 */ 3322 */
3323 s->avctx->get_buffer(s->avctx, &s->mconly_picture); 3323 s->avctx->get_buffer(s->avctx, &s->mconly_picture);
3324 3324
3325 return 0; 3325 return 0;
3326 } 3326 }
3327 3327
3328 3328
3329 static void calculate_vissual_weight(SnowContext *s, Plane *p){ 3329 static void calculate_vissual_weight(SnowContext *s, Plane *p){
3334 for(level=0; level<s->spatial_decomposition_count; level++){ 3334 for(level=0; level<s->spatial_decomposition_count; level++){
3335 for(orientation=level ? 1 : 0; orientation<4; orientation++){ 3335 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3336 SubBand *b= &p->band[level][orientation]; 3336 SubBand *b= &p->band[level][orientation];
3337 DWTELEM *buf= b->buf; 3337 DWTELEM *buf= b->buf;
3338 int64_t error=0; 3338 int64_t error=0;
3339 3339
3340 memset(s->spatial_dwt_buffer, 0, sizeof(int)*width*height); 3340 memset(s->spatial_dwt_buffer, 0, sizeof(int)*width*height);
3341 buf[b->width/2 + b->height/2*b->stride]= 256*256; 3341 buf[b->width/2 + b->height/2*b->stride]= 256*256;
3342 ff_spatial_idwt(s->spatial_dwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count); 3342 ff_spatial_idwt(s->spatial_dwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
3343 for(y=0; y<height; y++){ 3343 for(y=0; y<height; y++){
3344 for(x=0; x<width; x++){ 3344 for(x=0; x<width; x++){
3361 if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){ 3361 if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
3362 av_log(avctx, AV_LOG_ERROR, "this codec is under development, files encoded with it may not be decodable with future versions!!!\n" 3362 av_log(avctx, AV_LOG_ERROR, "this codec is under development, files encoded with it may not be decodable with future versions!!!\n"
3363 "use vstrict=-2 / -strict -2 to use it anyway\n"); 3363 "use vstrict=-2 / -strict -2 to use it anyway\n");
3364 return -1; 3364 return -1;
3365 } 3365 }
3366 3366
3367 common_init(avctx); 3367 common_init(avctx);
3368 alloc_blocks(s); 3368 alloc_blocks(s);
3369 3369
3370 s->version=0; 3370 s->version=0;
3371 3371
3372 s->m.avctx = avctx; 3372 s->m.avctx = avctx;
3373 s->m.flags = avctx->flags; 3373 s->m.flags = avctx->flags;
3374 s->m.bit_rate= avctx->bit_rate; 3374 s->m.bit_rate= avctx->bit_rate;
3375 3375
3376 s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t)); 3376 s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
3388 } 3388 }
3389 3389
3390 for(plane_index=0; plane_index<3; plane_index++){ 3390 for(plane_index=0; plane_index<3; plane_index++){
3391 calculate_vissual_weight(s, &s->plane[plane_index]); 3391 calculate_vissual_weight(s, &s->plane[plane_index]);
3392 } 3392 }
3393 3393
3394 3394
3395 avctx->coded_frame= &s->current_picture; 3395 avctx->coded_frame= &s->current_picture;
3396 switch(avctx->pix_fmt){ 3396 switch(avctx->pix_fmt){
3397 // case PIX_FMT_YUV444P: 3397 // case PIX_FMT_YUV444P:
3398 // case PIX_FMT_YUV422P: 3398 // case PIX_FMT_YUV422P:
3399 case PIX_FMT_YUV420P: 3399 case PIX_FMT_YUV420P:
3427 } 3427 }
3428 3428
3429 tmp= s->last_picture; 3429 tmp= s->last_picture;
3430 s->last_picture= s->current_picture; 3430 s->last_picture= s->current_picture;
3431 s->current_picture= tmp; 3431 s->current_picture= tmp;
3432 3432
3433 s->current_picture.reference= 1; 3433 s->current_picture.reference= 1;
3434 if(s->avctx->get_buffer(s->avctx, &s->current_picture) < 0){ 3434 if(s->avctx->get_buffer(s->avctx, &s->current_picture) < 0){
3435 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 3435 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
3436 return -1; 3436 return -1;
3437 } 3437 }
3438 3438
3439 return 0; 3439 return 0;
3440 } 3440 }
3441 3441
3442 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ 3442 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
3443 SnowContext *s = avctx->priv_data; 3443 SnowContext *s = avctx->priv_data;
3447 const int height= s->avctx->height; 3447 const int height= s->avctx->height;
3448 int level, orientation, plane_index; 3448 int level, orientation, plane_index;
3449 3449
3450 ff_init_range_encoder(c, buf, buf_size); 3450 ff_init_range_encoder(c, buf, buf_size);
3451 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8); 3451 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
3452 3452
3453 s->input_picture = *pict; 3453 s->input_picture = *pict;
3454 3454
3455 if(avctx->flags&CODEC_FLAG_PASS2){ 3455 if(avctx->flags&CODEC_FLAG_PASS2){
3456 s->m.pict_type = 3456 s->m.pict_type =
3457 pict->pict_type= s->m.rc_context.entry[avctx->frame_number].new_pict_type; 3457 pict->pict_type= s->m.rc_context.entry[avctx->frame_number].new_pict_type;
3460 pict->quality= ff_rate_estimate_qscale(&s->m); 3460 pict->quality= ff_rate_estimate_qscale(&s->m);
3461 }else{ 3461 }else{
3462 s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0; 3462 s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
3463 pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE; 3463 pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE;
3464 } 3464 }
3465 3465
3466 if(pict->quality){ 3466 if(pict->quality){
3467 s->qlog= rint(QROOT*log(pict->quality / (float)FF_QP2LAMBDA)/log(2)); 3467 s->qlog= rint(QROOT*log(pict->quality / (float)FF_QP2LAMBDA)/log(2));
3468 //<64 >60 3468 //<64 >60
3469 s->qlog += 61*QROOT/8; 3469 s->qlog += 61*QROOT/8;
3470 }else{ 3470 }else{
3477 s->m.current_picture_ptr= &s->m.current_picture; 3477 s->m.current_picture_ptr= &s->m.current_picture;
3478 if(pict->pict_type == P_TYPE){ 3478 if(pict->pict_type == P_TYPE){
3479 int block_width = (width +15)>>4; 3479 int block_width = (width +15)>>4;
3480 int block_height= (height+15)>>4; 3480 int block_height= (height+15)>>4;
3481 int stride= s->current_picture.linesize[0]; 3481 int stride= s->current_picture.linesize[0];
3482 3482
3483 assert(s->current_picture.data[0]); 3483 assert(s->current_picture.data[0]);
3484 assert(s->last_picture.data[0]); 3484 assert(s->last_picture.data[0]);
3485 3485
3486 s->m.avctx= s->avctx; 3486 s->m.avctx= s->avctx;
3487 s->m.current_picture.data[0]= s->current_picture.data[0]; 3487 s->m.current_picture.data[0]= s->current_picture.data[0];
3488 s->m. last_picture.data[0]= s-> last_picture.data[0]; 3488 s->m. last_picture.data[0]= s-> last_picture.data[0];
3489 s->m. new_picture.data[0]= s-> input_picture.data[0]; 3489 s->m. new_picture.data[0]= s-> input_picture.data[0];
3490 s->m. last_picture_ptr= &s->m. last_picture; 3490 s->m. last_picture_ptr= &s->m. last_picture;
3513 s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT; 3513 s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
3514 3514
3515 s->m.dsp= s->dsp; //move 3515 s->m.dsp= s->dsp; //move
3516 ff_init_me(&s->m); 3516 ff_init_me(&s->m);
3517 } 3517 }
3518 3518
3519 redo_frame: 3519 redo_frame:
3520 3520
3521 s->qbias= pict->pict_type == P_TYPE ? 2 : 0; 3521 s->qbias= pict->pict_type == P_TYPE ? 2 : 0;
3522 3522
3523 encode_header(s); 3523 encode_header(s);
3524 s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start); 3524 s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start);
3525 encode_blocks(s); 3525 encode_blocks(s);
3526 s->m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits; 3526 s->m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits;
3527 3527
3528 for(plane_index=0; plane_index<3; plane_index++){ 3528 for(plane_index=0; plane_index<3; plane_index++){
3529 Plane *p= &s->plane[plane_index]; 3529 Plane *p= &s->plane[plane_index];
3530 int w= p->width; 3530 int w= p->width;
3531 int h= p->height; 3531 int h= p->height;
3532 int x, y; 3532 int x, y;
3538 for(x=0; x<w; x++){ 3538 for(x=0; x<w; x++){
3539 s->spatial_dwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS; 3539 s->spatial_dwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
3540 } 3540 }
3541 } 3541 }
3542 predict_plane(s, s->spatial_dwt_buffer, plane_index, 0); 3542 predict_plane(s, s->spatial_dwt_buffer, plane_index, 0);
3543 3543
3544 if( plane_index==0 3544 if( plane_index==0
3545 && pict->pict_type == P_TYPE 3545 && pict->pict_type == P_TYPE
3546 && s->m.me.scene_change_score > s->avctx->scenechange_threshold){ 3546 && s->m.me.scene_change_score > s->avctx->scenechange_threshold){
3547 ff_init_range_encoder(c, buf, buf_size); 3547 ff_init_range_encoder(c, buf, buf_size);
3548 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8); 3548 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
3549 pict->pict_type= FF_I_TYPE; 3549 pict->pict_type= FF_I_TYPE;
3550 s->keyframe=1; 3550 s->keyframe=1;
3551 reset_contexts(s); 3551 reset_contexts(s);
3552 goto redo_frame; 3552 goto redo_frame;
3553 } 3553 }
3554 3554
3555 if(s->qlog == LOSSLESS_QLOG){ 3555 if(s->qlog == LOSSLESS_QLOG){
3556 for(y=0; y<h; y++){ 3556 for(y=0; y<h; y++){
3557 for(x=0; x<w; x++){ 3557 for(x=0; x<w; x++){
3558 s->spatial_dwt_buffer[y*w + x]= (s->spatial_dwt_buffer[y*w + x] + (1<<(FRAC_BITS-1))-1)>>FRAC_BITS; 3558 s->spatial_dwt_buffer[y*w + x]= (s->spatial_dwt_buffer[y*w + x] + (1<<(FRAC_BITS-1))-1)>>FRAC_BITS;
3559 } 3559 }
3560 } 3560 }
3561 } 3561 }
3562 3562
3563 ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count); 3563 ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
3564 3564
3565 for(level=0; level<s->spatial_decomposition_count; level++){ 3565 for(level=0; level<s->spatial_decomposition_count; level++){
3566 for(orientation=level ? 1 : 0; orientation<4; orientation++){ 3566 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3567 SubBand *b= &p->band[level][orientation]; 3567 SubBand *b= &p->band[level][orientation];
3568 3568
3569 quantize(s, b, b->buf, b->stride, s->qbias); 3569 quantize(s, b, b->buf, b->stride, s->qbias);
3570 if(orientation==0) 3570 if(orientation==0)
3571 decorrelate(s, b, b->buf, b->stride, pict->pict_type == P_TYPE, 0); 3571 decorrelate(s, b, b->buf, b->stride, pict->pict_type == P_TYPE, 0);
3572 encode_subband(s, b, b->buf, b->parent ? b->parent->buf : NULL, b->stride, orientation); 3572 encode_subband(s, b, b->buf, b->parent ? b->parent->buf : NULL, b->stride, orientation);
3573 assert(b->parent==NULL || b->parent->stride == b->stride*2); 3573 assert(b->parent==NULL || b->parent->stride == b->stride*2);
3596 {START_TIMER 3596 {START_TIMER
3597 predict_plane(s, s->spatial_dwt_buffer, plane_index, 1); 3597 predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
3598 STOP_TIMER("pred-conv")} 3598 STOP_TIMER("pred-conv")}
3599 if(s->avctx->flags&CODEC_FLAG_PSNR){ 3599 if(s->avctx->flags&CODEC_FLAG_PSNR){
3600 int64_t error= 0; 3600 int64_t error= 0;
3601 3601
3602 if(pict->data[plane_index]) //FIXME gray hack 3602 if(pict->data[plane_index]) //FIXME gray hack
3603 for(y=0; y<h; y++){ 3603 for(y=0; y<h; y++){
3604 for(x=0; x<w; x++){ 3604 for(x=0; x<w; x++){
3605 int d= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x] - pict->data[plane_index][y*pict->linesize[plane_index] + x]; 3605 int d= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x] - pict->data[plane_index][y*pict->linesize[plane_index] + x];
3606 error += d*d; 3606 error += d*d;
3628 if(avctx->flags&CODEC_FLAG_PASS2){ 3628 if(avctx->flags&CODEC_FLAG_PASS2){
3629 s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start); 3629 s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
3630 } 3630 }
3631 3631
3632 emms_c(); 3632 emms_c();
3633 3633
3634 return ff_rac_terminate(c); 3634 return ff_rac_terminate(c);
3635 } 3635 }
3636 3636
3637 static void common_end(SnowContext *s){ 3637 static void common_end(SnowContext *s){
3638 int plane_index, level, orientation; 3638 int plane_index, level, orientation;
3639 3639
3640 av_freep(&s->spatial_dwt_buffer); 3640 av_freep(&s->spatial_dwt_buffer);
3641 3641
3642 av_freep(&s->m.me.scratchpad); 3642 av_freep(&s->m.me.scratchpad);
3643 av_freep(&s->m.me.map); 3643 av_freep(&s->m.me.map);
3644 av_freep(&s->m.me.score_map); 3644 av_freep(&s->m.me.score_map);
3645 3645
3646 av_freep(&s->block); 3646 av_freep(&s->block);
3647 3647
3648 for(plane_index=0; plane_index<3; plane_index++){ 3648 for(plane_index=0; plane_index<3; plane_index++){
3649 for(level=s->spatial_decomposition_count-1; level>=0; level--){ 3649 for(level=s->spatial_decomposition_count-1; level>=0; level--){
3650 for(orientation=level ? 1 : 0; orientation<4; orientation++){ 3650 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3651 SubBand *b= &s->plane[plane_index].band[level][orientation]; 3651 SubBand *b= &s->plane[plane_index].band[level][orientation];
3652 3652
3653 av_freep(&b->x_coeff); 3653 av_freep(&b->x_coeff);
3654 } 3654 }
3655 } 3655 }
3656 } 3656 }
3657 } 3657 }
3668 3668
3669 static int decode_init(AVCodecContext *avctx) 3669 static int decode_init(AVCodecContext *avctx)
3670 { 3670 {
3671 SnowContext *s = avctx->priv_data; 3671 SnowContext *s = avctx->priv_data;
3672 int block_size; 3672 int block_size;
3673 3673
3674 avctx->pix_fmt= PIX_FMT_YUV420P; 3674 avctx->pix_fmt= PIX_FMT_YUV420P;
3675 3675
3676 common_init(avctx); 3676 common_init(avctx);
3677 3677
3678 block_size = MB_SIZE >> s->block_max_depth; 3678 block_size = MB_SIZE >> s->block_max_depth;
3679 slice_buffer_init(&s->sb, s->plane[0].height, (block_size) + (s->spatial_decomposition_count * (s->spatial_decomposition_count + 2)) + 1, s->plane[0].width, s->spatial_dwt_buffer); 3679 slice_buffer_init(&s->sb, s->plane[0].height, (block_size) + (s->spatial_decomposition_count * (s->spatial_decomposition_count + 2)) + 1, s->plane[0].width, s->spatial_dwt_buffer);
3680 3680
3681 return 0; 3681 return 0;
3682 } 3682 }
3683 3683
3684 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){ 3684 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
3685 SnowContext *s = avctx->priv_data; 3685 SnowContext *s = avctx->priv_data;
3697 3697
3698 frame_start(s); 3698 frame_start(s);
3699 //keyframe flag dupliaction mess FIXME 3699 //keyframe flag dupliaction mess FIXME
3700 if(avctx->debug&FF_DEBUG_PICT_INFO) 3700 if(avctx->debug&FF_DEBUG_PICT_INFO)
3701 av_log(avctx, AV_LOG_ERROR, "keyframe:%d qlog:%d\n", s->keyframe, s->qlog); 3701 av_log(avctx, AV_LOG_ERROR, "keyframe:%d qlog:%d\n", s->keyframe, s->qlog);
3702 3702
3703 decode_blocks(s); 3703 decode_blocks(s);
3704 3704
3705 for(plane_index=0; plane_index<3; plane_index++){ 3705 for(plane_index=0; plane_index<3; plane_index++){
3706 Plane *p= &s->plane[plane_index]; 3706 Plane *p= &s->plane[plane_index];
3707 int w= p->width; 3707 int w= p->width;
3708 int h= p->height; 3708 int h= p->height;
3709 int x, y; 3709 int x, y;
3710 int decode_state[MAX_DECOMPOSITIONS][4][1]; /* Stored state info for unpack_coeffs. 1 variable per instance. */ 3710 int decode_state[MAX_DECOMPOSITIONS][4][1]; /* Stored state info for unpack_coeffs. 1 variable per instance. */
3711 3711
3712 if(s->avctx->debug&2048){ 3712 if(s->avctx->debug&2048){
3713 memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h); 3713 memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
3714 predict_plane(s, s->spatial_dwt_buffer, plane_index, 1); 3714 predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
3715 3715
3716 for(y=0; y<h; y++){ 3716 for(y=0; y<h; y++){
3741 int y; 3741 int y;
3742 int end_y; 3742 int end_y;
3743 3743
3744 ff_spatial_idwt_buffered_init(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count); 3744 ff_spatial_idwt_buffered_init(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count);
3745 for(mb_y=0; mb_y<=mb_h; mb_y++){ 3745 for(mb_y=0; mb_y<=mb_h; mb_y++){
3746 3746
3747 int slice_starty = block_w*mb_y; 3747 int slice_starty = block_w*mb_y;
3748 int slice_h = block_w*(mb_y+1); 3748 int slice_h = block_w*(mb_y+1);
3749 if (!(s->keyframe || s->avctx->debug&512)){ 3749 if (!(s->keyframe || s->avctx->debug&512)){
3750 slice_starty = FFMAX(0, slice_starty - (block_w >> 1)); 3750 slice_starty = FFMAX(0, slice_starty - (block_w >> 1));
3751 slice_h -= (block_w >> 1); 3751 slice_h -= (block_w >> 1);
3752 } 3752 }
3753 3753
3754 { 3754 {
3755 START_TIMER 3755 START_TIMER
3756 for(level=0; level<s->spatial_decomposition_count; level++){ 3756 for(level=0; level<s->spatial_decomposition_count; level++){
3757 for(orientation=level ? 1 : 0; orientation<4; orientation++){ 3757 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3758 SubBand *b= &p->band[level][orientation]; 3758 SubBand *b= &p->band[level][orientation];
3759 int start_y; 3759 int start_y;
3766 start_y = FFMAX(0, start_y - (block_w >> (1+s->spatial_decomposition_count - level))); 3766 start_y = FFMAX(0, start_y - (block_w >> (1+s->spatial_decomposition_count - level)));
3767 end_y = FFMAX(0, end_y - (block_w >> (1+s->spatial_decomposition_count - level))); 3767 end_y = FFMAX(0, end_y - (block_w >> (1+s->spatial_decomposition_count - level)));
3768 } 3768 }
3769 start_y = FFMIN(b->height, start_y); 3769 start_y = FFMIN(b->height, start_y);
3770 end_y = FFMIN(b->height, end_y); 3770 end_y = FFMIN(b->height, end_y);
3771 3771
3772 if (start_y != end_y){ 3772 if (start_y != end_y){
3773 if (orientation == 0){ 3773 if (orientation == 0){
3774 SubBand * correlate_band = &p->band[0][0]; 3774 SubBand * correlate_band = &p->band[0][0];
3775 int correlate_end_y = FFMIN(b->height, end_y + 1); 3775 int correlate_end_y = FFMIN(b->height, end_y + 1);
3776 int correlate_start_y = FFMIN(b->height, (start_y ? start_y + 1 : 0)); 3776 int correlate_start_y = FFMIN(b->height, (start_y ? start_y + 1 : 0));
3783 } 3783 }
3784 } 3784 }
3785 } 3785 }
3786 STOP_TIMER("decode_subband_slice"); 3786 STOP_TIMER("decode_subband_slice");
3787 } 3787 }
3788 3788
3789 { START_TIMER 3789 { START_TIMER
3790 for(; yd<slice_h; yd+=4){ 3790 for(; yd<slice_h; yd+=4){
3791 ff_spatial_idwt_buffered_slice(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count, yd); 3791 ff_spatial_idwt_buffered_slice(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count, yd);
3792 } 3792 }
3793 STOP_TIMER("idwt slice");} 3793 STOP_TIMER("idwt slice");}
3794 3794
3795 3795
3796 if(s->qlog == LOSSLESS_QLOG){ 3796 if(s->qlog == LOSSLESS_QLOG){
3797 for(; yq<slice_h && yq<h; yq++){ 3797 for(; yq<slice_h && yq<h; yq++){
3798 DWTELEM * line = slice_buffer_get_line(&s->sb, yq); 3798 DWTELEM * line = slice_buffer_get_line(&s->sb, yq);
3799 for(x=0; x<w; x++){ 3799 for(x=0; x<w; x++){
3800 line[x] <<= FRAC_BITS; 3800 line[x] <<= FRAC_BITS;
3801 } 3801 }
3802 } 3802 }
3803 } 3803 }
3804 3804
3805 predict_slice_buffered(s, &s->sb, s->spatial_dwt_buffer, plane_index, 1, mb_y); 3805 predict_slice_buffered(s, &s->sb, s->spatial_dwt_buffer, plane_index, 1, mb_y);
3806 3806
3807 y = FFMIN(p->height, slice_starty); 3807 y = FFMIN(p->height, slice_starty);
3808 end_y = FFMIN(p->height, slice_h); 3808 end_y = FFMIN(p->height, slice_h);
3809 while(y < end_y) 3809 while(y < end_y)
3810 slice_buffer_release(&s->sb, y++); 3810 slice_buffer_release(&s->sb, y++);
3811 } 3811 }
3812 3812
3813 slice_buffer_flush(&s->sb); 3813 slice_buffer_flush(&s->sb);
3814 3814
3815 STOP_TIMER("idwt + predict_slices")} 3815 STOP_TIMER("idwt + predict_slices")}
3816 } 3816 }
3817 3817
3818 emms_c(); 3818 emms_c();
3819 3819
3820 if(s->last_picture.data[0]) 3820 if(s->last_picture.data[0])
3821 avctx->release_buffer(avctx, &s->last_picture); 3821 avctx->release_buffer(avctx, &s->last_picture);
3822 3822
3823 if(!(s->avctx->debug&2048)) 3823 if(!(s->avctx->debug&2048))
3824 *picture= s->current_picture; 3824 *picture= s->current_picture;
3825 else 3825 else
3826 *picture= s->mconly_picture; 3826 *picture= s->mconly_picture;
3827 3827
3828 *data_size = sizeof(AVFrame); 3828 *data_size = sizeof(AVFrame);
3829 3829
3830 bytes_read= c->bytestream - c->bytestream_start; 3830 bytes_read= c->bytestream - c->bytestream_start;
3831 if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n"); //FIXME 3831 if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n"); //FIXME
3832 3832
3833 return bytes_read; 3833 return bytes_read;
3834 } 3834 }
3836 static int decode_end(AVCodecContext *avctx) 3836 static int decode_end(AVCodecContext *avctx)
3837 { 3837 {
3838 SnowContext *s = avctx->priv_data; 3838 SnowContext *s = avctx->priv_data;
3839 3839
3840 slice_buffer_destroy(&s->sb); 3840 slice_buffer_destroy(&s->sb);
3841 3841
3842 common_end(s); 3842 common_end(s);
3843 3843
3844 return 0; 3844 return 0;
3845 } 3845 }
3846 3846
3881 int buffer[2][width*height]; 3881 int buffer[2][width*height];
3882 SnowContext s; 3882 SnowContext s;
3883 int i; 3883 int i;
3884 s.spatial_decomposition_count=6; 3884 s.spatial_decomposition_count=6;
3885 s.spatial_decomposition_type=1; 3885 s.spatial_decomposition_type=1;
3886 3886
3887 printf("testing 5/3 DWT\n"); 3887 printf("testing 5/3 DWT\n");
3888 for(i=0; i<width*height; i++) 3888 for(i=0; i<width*height; i++)
3889 buffer[0][i]= buffer[1][i]= random()%54321 - 12345; 3889 buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
3890 3890
3891 ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count); 3891 ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
3892 ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count); 3892 ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
3893 3893
3894 for(i=0; i<width*height; i++) 3894 for(i=0; i<width*height; i++)
3895 if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]); 3895 if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
3896 3896
3897 printf("testing 9/7 DWT\n"); 3897 printf("testing 9/7 DWT\n");
3898 s.spatial_decomposition_type=0; 3898 s.spatial_decomposition_type=0;
3899 for(i=0; i<width*height; i++) 3899 for(i=0; i<width*height; i++)
3900 buffer[0][i]= buffer[1][i]= random()%54321 - 12345; 3900 buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
3901 3901
3902 ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count); 3902 ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
3903 ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count); 3903 ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
3904 3904
3905 for(i=0; i<width*height; i++) 3905 for(i=0; i<width*height; i++)
3906 if(ABS(buffer[0][i] - buffer[1][i])>20) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]); 3906 if(ABS(buffer[0][i] - buffer[1][i])>20) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
3907 3907
3908 #if 0 3908 #if 0
3909 printf("testing AC coder\n"); 3909 printf("testing AC coder\n");
3910 memset(s.header_state, 0, sizeof(s.header_state)); 3910 memset(s.header_state, 0, sizeof(s.header_state));
3911 ff_init_range_encoder(&s.c, buffer[0], 256*256); 3911 ff_init_range_encoder(&s.c, buffer[0], 256*256);
3912 ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64); 3912 ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
3913 3913
3914 for(i=-256; i<256; i++){ 3914 for(i=-256; i<256; i++){
3915 START_TIMER 3915 START_TIMER
3916 put_symbol(&s.c, s.header_state, i*i*i/3*ABS(i), 1); 3916 put_symbol(&s.c, s.header_state, i*i*i/3*ABS(i), 1);
3917 STOP_TIMER("put_symbol") 3917 STOP_TIMER("put_symbol")
3918 } 3918 }
3919 ff_rac_terminate(&s.c); 3919 ff_rac_terminate(&s.c);
3920 3920
3921 memset(s.header_state, 0, sizeof(s.header_state)); 3921 memset(s.header_state, 0, sizeof(s.header_state));
3922 ff_init_range_decoder(&s.c, buffer[0], 256*256); 3922 ff_init_range_decoder(&s.c, buffer[0], 256*256);
3923 ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64); 3923 ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
3924 3924
3925 for(i=-256; i<256; i++){ 3925 for(i=-256; i<256; i++){
3926 int j; 3926 int j;
3927 START_TIMER 3927 START_TIMER
3928 j= get_symbol(&s.c, s.header_state, 1); 3928 j= get_symbol(&s.c, s.header_state, 1);
3929 STOP_TIMER("get_symbol") 3929 STOP_TIMER("get_symbol")
3946 DWTELEM *buf= buffer[0]; 3946 DWTELEM *buf= buffer[0];
3947 int64_t error=0; 3947 int64_t error=0;
3948 3948
3949 if(orientation&1) buf+=w; 3949 if(orientation&1) buf+=w;
3950 if(orientation>1) buf+=stride>>1; 3950 if(orientation>1) buf+=stride>>1;
3951 3951
3952 memset(buffer[0], 0, sizeof(int)*width*height); 3952 memset(buffer[0], 0, sizeof(int)*width*height);
3953 buf[w/2 + h/2*stride]= 256*256; 3953 buf[w/2 + h/2*stride]= 256*256;
3954 ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count); 3954 ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
3955 for(y=0; y<height; y++){ 3955 for(y=0; y<height; y++){
3956 for(x=0; x<width; x++){ 3956 for(x=0; x<width; x++){
3984 DWTELEM *buf= buffer[0]; 3984 DWTELEM *buf= buffer[0];
3985 int64_t error=0; 3985 int64_t error=0;
3986 3986
3987 buf+=w; 3987 buf+=w;
3988 buf+=stride>>1; 3988 buf+=stride>>1;
3989 3989
3990 memset(buffer[0], 0, sizeof(int)*width*height); 3990 memset(buffer[0], 0, sizeof(int)*width*height);
3991 #if 1 3991 #if 1
3992 for(y=0; y<height; y++){ 3992 for(y=0; y<height; y++){
3993 for(x=0; x<width; x++){ 3993 for(x=0; x<width; x++){
3994 int tab[4]={0,2,3,1}; 3994 int tab[4]={0,2,3,1};