Revision 05aec7bb libavcodec/snow.c

View differences:

libavcodec/snow.c
21 21
#include "libavutil/intmath.h"
22 22
#include "avcodec.h"
23 23
#include "dsputil.h"
24
#include "dwt.h"
24 25
#include "snow.h"
25 26

  
26 27
#include "rangecoder.h"
......
440 441
    AVCodecContext *avctx;
441 442
    RangeCoder c;
442 443
    DSPContext dsp;
444
    DWTContext dwt;
443 445
    AVFrame new_picture;
444 446
    AVFrame input_picture;              ///< new_picture with the internal linesizes
445 447
    AVFrame current_picture;
......
494 496
    uint8_t *scratchbuf;
495 497
}SnowContext;
496 498

  
497
typedef struct {
498
    IDWTELEM *b0;
499
    IDWTELEM *b1;
500
    IDWTELEM *b2;
501
    IDWTELEM *b3;
502
    int y;
503
} DWTCompose;
504

  
505
#define slice_buffer_get_line(slice_buf, line_num) ((slice_buf)->line[line_num] ? (slice_buf)->line[line_num] : slice_buffer_load_line((slice_buf), (line_num)))
506
//#define slice_buffer_get_line(slice_buf, line_num) (slice_buffer_load_line((slice_buf), (line_num)))
507

  
508
static void slice_buffer_init(slice_buffer * buf, int line_count, int max_allocated_lines, int line_width, IDWTELEM * base_buffer)
509
{
510
    int i;
511

  
512
    buf->base_buffer = base_buffer;
513
    buf->line_count = line_count;
514
    buf->line_width = line_width;
515
    buf->data_count = max_allocated_lines;
516
    buf->line = av_mallocz (sizeof(IDWTELEM *) * line_count);
517
    buf->data_stack = av_malloc (sizeof(IDWTELEM *) * max_allocated_lines);
518

  
519
    for(i = 0; i < max_allocated_lines; i++){
520
        buf->data_stack[i] = av_malloc (sizeof(IDWTELEM) * line_width);
521
    }
522

  
523
    buf->data_stack_top = max_allocated_lines - 1;
524
}
525

  
526
static IDWTELEM * slice_buffer_load_line(slice_buffer * buf, int line)
527
{
528
    IDWTELEM * buffer;
529

  
530
    assert(buf->data_stack_top >= 0);
531
//  assert(!buf->line[line]);
532
    if (buf->line[line])
533
        return buf->line[line];
534

  
535
    buffer = buf->data_stack[buf->data_stack_top];
536
    buf->data_stack_top--;
537
    buf->line[line] = buffer;
538

  
539
    return buffer;
540
}
541

  
542
static void slice_buffer_release(slice_buffer * buf, int line)
543
{
544
    IDWTELEM * buffer;
545

  
546
    assert(line >= 0 && line < buf->line_count);
547
    assert(buf->line[line]);
548

  
549
    buffer = buf->line[line];
550
    buf->data_stack_top++;
551
    buf->data_stack[buf->data_stack_top] = buffer;
552
    buf->line[line] = NULL;
553
}
554

  
555
static void slice_buffer_flush(slice_buffer * buf)
556
{
557
    int i;
558
    for(i = 0; i < buf->line_count; i++){
559
        if (buf->line[i])
560
            slice_buffer_release(buf, i);
561
    }
562
}
563

  
564
static void slice_buffer_destroy(slice_buffer * buf)
565
{
566
    int i;
567
    slice_buffer_flush(buf);
568

  
569
    for(i = buf->data_count - 1; i >= 0; i--){
570
        av_freep(&buf->data_stack[i]);
571
    }
572
    av_freep(&buf->data_stack);
573
    av_freep(&buf->line);
574
}
575

  
576 499
#ifdef __sgi
577 500
// Avoid a name clash on SGI IRIX
578 501
#undef qexp
......
580 503
#define QEXPSHIFT (7-FRAC_BITS+8) //FIXME try to change this to 0
581 504
static uint8_t qexp[QROOT];
582 505

  
583
static inline int mirror(int v, int m){
584
    while((unsigned)v > (unsigned)m){
585
        v=-v;
586
        if(v<0) v+= 2*m;
587
    }
588
    return v;
589
}
590

  
591 506
static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
592 507
    int i;
593 508

  
......
709 624
    return v;
710 625
}
711 626

  
712
static av_always_inline void
713
lift(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
714
     int dst_step, int src_step, int ref_step,
715
     int width, int mul, int add, int shift,
716
     int highpass, int inverse){
717
    const int mirror_left= !highpass;
718
    const int mirror_right= (width&1) ^ highpass;
719
    const int w= (width>>1) - 1 + (highpass & width);
720
    int i;
721

  
722
#define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
723
    if(mirror_left){
724
        dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
725
        dst += dst_step;
726
        src += src_step;
727
    }
728

  
729
    for(i=0; i<w; i++){
730
        dst[i*dst_step] =
731
            LIFT(src[i*src_step],
732
                 ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift),
733
                 inverse);
734
    }
735

  
736
    if(mirror_right){
737
        dst[w*dst_step] =
738
            LIFT(src[w*src_step],
739
                 ((mul*2*ref[w*ref_step]+add)>>shift),
740
                 inverse);
741
    }
742
}
743

  
744
static av_always_inline void
745
inv_lift(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *ref,
746
         int dst_step, int src_step, int ref_step,
747
         int width, int mul, int add, int shift,
748
         int highpass, int inverse){
749
    const int mirror_left= !highpass;
750
    const int mirror_right= (width&1) ^ highpass;
751
    const int w= (width>>1) - 1 + (highpass & width);
752
    int i;
753

  
754
#define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
755
    if(mirror_left){
756
        dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
757
        dst += dst_step;
758
        src += src_step;
759
    }
760

  
761
    for(i=0; i<w; i++){
762
        dst[i*dst_step] =
763
            LIFT(src[i*src_step],
764
                 ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift),
765
                 inverse);
766
    }
767

  
768
    if(mirror_right){
769
        dst[w*dst_step] =
770
            LIFT(src[w*src_step],
771
                 ((mul*2*ref[w*ref_step]+add)>>shift),
772
                 inverse);
773
    }
774
}
775

  
776
#ifndef liftS
777
static av_always_inline void
778
liftS(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
779
      int dst_step, int src_step, int ref_step,
780
      int width, int mul, int add, int shift,
781
      int highpass, int inverse){
782
    const int mirror_left= !highpass;
783
    const int mirror_right= (width&1) ^ highpass;
784
    const int w= (width>>1) - 1 + (highpass & width);
785
    int i;
786

  
787
    assert(shift == 4);
788
#define LIFTS(src, ref, inv) \
789
        ((inv) ? \
790
            (src) + (((ref) + 4*(src))>>shift): \
791
            -((-16*(src) + (ref) + add/4 + 1 + (5<<25))/(5*4) - (1<<23)))
792
    if(mirror_left){
793
        dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse);
794
        dst += dst_step;
795
        src += src_step;
796
    }
797

  
798
    for(i=0; i<w; i++){
799
        dst[i*dst_step] =
800
            LIFTS(src[i*src_step],
801
                  mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add,
802
                  inverse);
803
    }
804

  
805
    if(mirror_right){
806
        dst[w*dst_step] =
807
            LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse);
808
    }
809
}
810
static av_always_inline void
811
inv_liftS(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *ref,
812
          int dst_step, int src_step, int ref_step,
813
          int width, int mul, int add, int shift,
814
          int highpass, int inverse){
815
    const int mirror_left= !highpass;
816
    const int mirror_right= (width&1) ^ highpass;
817
    const int w= (width>>1) - 1 + (highpass & width);
818
    int i;
819

  
820
    assert(shift == 4);
821
#define LIFTS(src, ref, inv) \
822
    ((inv) ? \
823
        (src) + (((ref) + 4*(src))>>shift): \
824
        -((-16*(src) + (ref) + add/4 + 1 + (5<<25))/(5*4) - (1<<23)))
825
    if(mirror_left){
826
        dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse);
827
        dst += dst_step;
828
        src += src_step;
829
    }
830

  
831
    for(i=0; i<w; i++){
832
        dst[i*dst_step] =
833
            LIFTS(src[i*src_step],
834
                  mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add,
835
                  inverse);
836
    }
837

  
838
    if(mirror_right){
839
        dst[w*dst_step] =
840
            LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse);
841
    }
842
}
843
#endif /* ! liftS */
844

  
845
static void horizontal_decompose53i(DWTELEM *b, int width){
846
    DWTELEM temp[width];
847
    const int width2= width>>1;
848
    int x;
849
    const int w2= (width+1)>>1;
850

  
851
    for(x=0; x<width2; x++){
852
        temp[x   ]= b[2*x    ];
853
        temp[x+w2]= b[2*x + 1];
854
    }
855
    if(width&1)
856
        temp[x   ]= b[2*x    ];
857
#if 0
858
    {
859
    int A1,A2,A3,A4;
860
    A2= temp[1       ];
861
    A4= temp[0       ];
862
    A1= temp[0+width2];
863
    A1 -= (A2 + A4)>>1;
864
    A4 += (A1 + 1)>>1;
865
    b[0+width2] = A1;
866
    b[0       ] = A4;
867
    for(x=1; x+1<width2; x+=2){
868
        A3= temp[x+width2];
869
        A4= temp[x+1     ];
870
        A3 -= (A2 + A4)>>1;
871
        A2 += (A1 + A3 + 2)>>2;
872
        b[x+width2] = A3;
873
        b[x       ] = A2;
874

  
875
        A1= temp[x+1+width2];
876
        A2= temp[x+2       ];
877
        A1 -= (A2 + A4)>>1;
878
        A4 += (A1 + A3 + 2)>>2;
879
        b[x+1+width2] = A1;
880
        b[x+1       ] = A4;
881
    }
882
    A3= temp[width-1];
883
    A3 -= A2;
884
    A2 += (A1 + A3 + 2)>>2;
885
    b[width -1] = A3;
886
    b[width2-1] = A2;
887
    }
888
#else
889
    lift(b+w2, temp+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
890
    lift(b   , temp   , b+w2, 1, 1, 1, width,  1, 2, 2, 0, 0);
891
#endif /* 0 */
892
}
893

  
894
static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
895
    int i;
896

  
897
    for(i=0; i<width; i++){
898
        b1[i] -= (b0[i] + b2[i])>>1;
899
    }
900
}
901

  
902
static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
903
    int i;
904

  
905
    for(i=0; i<width; i++){
906
        b1[i] += (b0[i] + b2[i] + 2)>>2;
907
    }
908
}
909

  
910
static void spatial_decompose53i(DWTELEM *buffer, int width, int height, int stride){
911
    int y;
912
    DWTELEM *b0= buffer + mirror(-2-1, height-1)*stride;
913
    DWTELEM *b1= buffer + mirror(-2  , height-1)*stride;
914

  
915
    for(y=-2; y<height; y+=2){
916
        DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
917
        DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
918

  
919
        if(y+1<(unsigned)height) horizontal_decompose53i(b2, width);
920
        if(y+2<(unsigned)height) horizontal_decompose53i(b3, width);
921

  
922
        if(y+1<(unsigned)height) vertical_decompose53iH0(b1, b2, b3, width);
923
        if(y+0<(unsigned)height) vertical_decompose53iL0(b0, b1, b2, width);
924

  
925
        b0=b2;
926
        b1=b3;
927
    }
928
}
929

  
930
static void horizontal_decompose97i(DWTELEM *b, int width){
931
    DWTELEM temp[width];
932
    const int w2= (width+1)>>1;
933

  
934
    lift (temp+w2, b    +1, b      , 1, 2, 2, width,  W_AM, W_AO, W_AS, 1, 1);
935
    liftS(temp   , b      , temp+w2, 1, 2, 1, width,  W_BM, W_BO, W_BS, 0, 0);
936
    lift (b   +w2, temp+w2, temp   , 1, 1, 1, width,  W_CM, W_CO, W_CS, 1, 0);
937
    lift (b      , temp   , b   +w2, 1, 1, 1, width,  W_DM, W_DO, W_DS, 0, 0);
938
}
939

  
940

  
941
static void vertical_decompose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
942
    int i;
943

  
944
    for(i=0; i<width; i++){
945
        b1[i] -= (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
946
    }
947
}
948

  
949
static void vertical_decompose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
950
    int i;
951

  
952
    for(i=0; i<width; i++){
953
        b1[i] += (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
954
    }
955
}
956

  
957
static void vertical_decompose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
958
    int i;
959

  
960
    for(i=0; i<width; i++){
961
#ifdef liftS
962
        b1[i] -= (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
963
#else
964
        b1[i] = (16*4*b1[i] - 4*(b0[i] + b2[i]) + W_BO*5 + (5<<27)) / (5*16) - (1<<23);
965
#endif
966
    }
967
}
968

  
969
static void vertical_decompose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
970
    int i;
971

  
972
    for(i=0; i<width; i++){
973
        b1[i] += (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
974
    }
975
}
976

  
977
static void spatial_decompose97i(DWTELEM *buffer, int width, int height, int stride){
978
    int y;
979
    DWTELEM *b0= buffer + mirror(-4-1, height-1)*stride;
980
    DWTELEM *b1= buffer + mirror(-4  , height-1)*stride;
981
    DWTELEM *b2= buffer + mirror(-4+1, height-1)*stride;
982
    DWTELEM *b3= buffer + mirror(-4+2, height-1)*stride;
983

  
984
    for(y=-4; y<height; y+=2){
985
        DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
986
        DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
987

  
988
        if(y+3<(unsigned)height) horizontal_decompose97i(b4, width);
989
        if(y+4<(unsigned)height) horizontal_decompose97i(b5, width);
990

  
991
        if(y+3<(unsigned)height) vertical_decompose97iH0(b3, b4, b5, width);
992
        if(y+2<(unsigned)height) vertical_decompose97iL0(b2, b3, b4, width);
993
        if(y+1<(unsigned)height) vertical_decompose97iH1(b1, b2, b3, width);
994
        if(y+0<(unsigned)height) vertical_decompose97iL1(b0, b1, b2, width);
995

  
996
        b0=b2;
997
        b1=b3;
998
        b2=b4;
999
        b3=b5;
1000
    }
1001
}
1002

  
1003
void ff_spatial_dwt(DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
1004
    int level;
1005

  
1006
    for(level=0; level<decomposition_count; level++){
1007
        switch(type){
1008
        case DWT_97: spatial_decompose97i(buffer, width>>level, height>>level, stride<<level); break;
1009
        case DWT_53: spatial_decompose53i(buffer, width>>level, height>>level, stride<<level); break;
1010
        }
1011
    }
1012
}
1013

  
1014
static void horizontal_compose53i(IDWTELEM *b, int width){
1015
    IDWTELEM temp[width];
1016
    const int width2= width>>1;
1017
    const int w2= (width+1)>>1;
1018
    int x;
1019

  
1020
    for(x=0; x<width2; x++){
1021
        temp[2*x    ]= b[x   ];
1022
        temp[2*x + 1]= b[x+w2];
1023
    }
1024
    if(width&1)
1025
        temp[2*x    ]= b[x   ];
1026

  
1027
    b[0] = temp[0] - ((temp[1]+1)>>1);
1028
    for(x=2; x<width-1; x+=2){
1029
        b[x  ] = temp[x  ] - ((temp[x-1] + temp[x+1]+2)>>2);
1030
        b[x-1] = temp[x-1] + ((b   [x-2] + b   [x  ]+1)>>1);
1031
    }
1032
    if(width&1){
1033
        b[x  ] = temp[x  ] - ((temp[x-1]+1)>>1);
1034
        b[x-1] = temp[x-1] + ((b   [x-2] + b  [x  ]+1)>>1);
1035
    }else
1036
        b[x-1] = temp[x-1] + b[x-2];
1037
}
1038

  
1039
static void vertical_compose53iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
1040
    int i;
1041

  
1042
    for(i=0; i<width; i++){
1043
        b1[i] += (b0[i] + b2[i])>>1;
1044
    }
1045
}
1046

  
1047
static void vertical_compose53iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
1048
    int i;
1049

  
1050
    for(i=0; i<width; i++){
1051
        b1[i] -= (b0[i] + b2[i] + 2)>>2;
1052
    }
1053
}
1054

  
1055
static void spatial_compose53i_buffered_init(DWTCompose *cs, slice_buffer * sb, int height, int stride_line){
1056
    cs->b0 = slice_buffer_get_line(sb, mirror(-1-1, height-1) * stride_line);
1057
    cs->b1 = slice_buffer_get_line(sb, mirror(-1  , height-1) * stride_line);
1058
    cs->y = -1;
1059
}
1060

  
1061
static void spatial_compose53i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride){
1062
    cs->b0 = buffer + mirror(-1-1, height-1)*stride;
1063
    cs->b1 = buffer + mirror(-1  , height-1)*stride;
1064
    cs->y = -1;
1065
}
1066

  
1067
static void spatial_compose53i_dy_buffered(DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line){
1068
    int y= cs->y;
1069

  
1070
    IDWTELEM *b0= cs->b0;
1071
    IDWTELEM *b1= cs->b1;
1072
    IDWTELEM *b2= slice_buffer_get_line(sb, mirror(y+1, height-1) * stride_line);
1073
    IDWTELEM *b3= slice_buffer_get_line(sb, mirror(y+2, height-1) * stride_line);
1074

  
1075
    if(y+1<(unsigned)height && y<(unsigned)height){
1076
        int x;
1077

  
1078
        for(x=0; x<width; x++){
1079
            b2[x] -= (b1[x] + b3[x] + 2)>>2;
1080
            b1[x] += (b0[x] + b2[x])>>1;
1081
        }
1082
    }else{
1083
        if(y+1<(unsigned)height) vertical_compose53iL0(b1, b2, b3, width);
1084
        if(y+0<(unsigned)height) vertical_compose53iH0(b0, b1, b2, width);
1085
    }
1086

  
1087
        if(y-1<(unsigned)height) horizontal_compose53i(b0, width);
1088
        if(y+0<(unsigned)height) horizontal_compose53i(b1, width);
1089

  
1090
    cs->b0 = b2;
1091
    cs->b1 = b3;
1092
    cs->y += 2;
1093
}
1094

  
1095
static void spatial_compose53i_dy(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride){
1096
    int y= cs->y;
1097
    IDWTELEM *b0= cs->b0;
1098
    IDWTELEM *b1= cs->b1;
1099
    IDWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
1100
    IDWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
1101

  
1102
        if(y+1<(unsigned)height) vertical_compose53iL0(b1, b2, b3, width);
1103
        if(y+0<(unsigned)height) vertical_compose53iH0(b0, b1, b2, width);
1104

  
1105
        if(y-1<(unsigned)height) horizontal_compose53i(b0, width);
1106
        if(y+0<(unsigned)height) horizontal_compose53i(b1, width);
1107

  
1108
    cs->b0 = b2;
1109
    cs->b1 = b3;
1110
    cs->y += 2;
1111
}
1112

  
1113
static void av_unused spatial_compose53i(IDWTELEM *buffer, int width, int height, int stride){
1114
    DWTCompose cs;
1115
    spatial_compose53i_init(&cs, buffer, height, stride);
1116
    while(cs.y <= height)
1117
        spatial_compose53i_dy(&cs, buffer, width, height, stride);
1118
}
1119

  
1120

  
1121
void ff_snow_horizontal_compose97i(IDWTELEM *b, int width){
1122
    IDWTELEM temp[width];
1123
    const int w2= (width+1)>>1;
1124

  
1125
#if 0 //maybe more understadable but slower
1126
    inv_lift (temp   , b      , b   +w2, 2, 1, 1, width,  W_DM, W_DO, W_DS, 0, 1);
1127
    inv_lift (temp+1 , b   +w2, temp   , 2, 1, 2, width,  W_CM, W_CO, W_CS, 1, 1);
1128

  
1129
    inv_liftS(b      , temp   , temp+1 , 2, 2, 2, width,  W_BM, W_BO, W_BS, 0, 1);
1130
    inv_lift (b+1    , temp+1 , b      , 2, 2, 2, width,  W_AM, W_AO, W_AS, 1, 0);
1131
#else
1132
    int x;
1133
    temp[0] = b[0] - ((3*b[w2]+2)>>2);
1134
    for(x=1; x<(width>>1); x++){
1135
        temp[2*x  ] = b[x     ] - ((3*(b   [x+w2-1] + b[x+w2])+4)>>3);
1136
        temp[2*x-1] = b[x+w2-1] - temp[2*x-2] - temp[2*x];
1137
    }
1138
    if(width&1){
1139
        temp[2*x  ] = b[x     ] - ((3*b   [x+w2-1]+2)>>2);
1140
        temp[2*x-1] = b[x+w2-1] - temp[2*x-2] - temp[2*x];
1141
    }else
1142
        temp[2*x-1] = b[x+w2-1] - 2*temp[2*x-2];
1143

  
1144
    b[0] = temp[0] + ((2*temp[0] + temp[1]+4)>>3);
1145
    for(x=2; x<width-1; x+=2){
1146
        b[x  ] = temp[x  ] + ((4*temp[x  ] + temp[x-1] + temp[x+1]+8)>>4);
1147
        b[x-1] = temp[x-1] + ((3*(b  [x-2] + b   [x  ] ))>>1);
1148
    }
1149
    if(width&1){
1150
        b[x  ] = temp[x  ] + ((2*temp[x  ] + temp[x-1]+4)>>3);
1151
        b[x-1] = temp[x-1] + ((3*(b  [x-2] + b   [x  ] ))>>1);
1152
    }else
1153
        b[x-1] = temp[x-1] + 3*b [x-2];
1154
#endif
1155
}
1156

  
1157
static void vertical_compose97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
1158
    int i;
1159

  
1160
    for(i=0; i<width; i++){
1161
        b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
1162
    }
1163
}
1164

  
1165
static void vertical_compose97iH1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
1166
    int i;
1167

  
1168
    for(i=0; i<width; i++){
1169
        b1[i] -= (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
1170
    }
1171
}
1172

  
1173
static void vertical_compose97iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
1174
    int i;
1175

  
1176
    for(i=0; i<width; i++){
1177
#ifdef liftS
1178
        b1[i] += (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
1179
#else
1180
        b1[i] += (W_BM*(b0[i] + b2[i])+4*b1[i]+W_BO)>>W_BS;
1181
#endif
1182
    }
1183
}
1184

  
1185
static void vertical_compose97iL1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
1186
    int i;
1187

  
1188
    for(i=0; i<width; i++){
1189
        b1[i] -= (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
1190
    }
1191
}
1192

  
1193
void ff_snow_vertical_compose97i(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5, int width){
1194
    int i;
1195

  
1196
    for(i=0; i<width; i++){
1197
        b4[i] -= (W_DM*(b3[i] + b5[i])+W_DO)>>W_DS;
1198
        b3[i] -= (W_CM*(b2[i] + b4[i])+W_CO)>>W_CS;
1199
#ifdef liftS
1200
        b2[i] += (W_BM*(b1[i] + b3[i])+W_BO)>>W_BS;
1201
#else
1202
        b2[i] += (W_BM*(b1[i] + b3[i])+4*b2[i]+W_BO)>>W_BS;
1203
#endif
1204
        b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
1205
    }
1206
}
1207

  
1208
static void spatial_compose97i_buffered_init(DWTCompose *cs, slice_buffer * sb, int height, int stride_line){
1209
    cs->b0 = slice_buffer_get_line(sb, mirror(-3-1, height-1) * stride_line);
1210
    cs->b1 = slice_buffer_get_line(sb, mirror(-3  , height-1) * stride_line);
1211
    cs->b2 = slice_buffer_get_line(sb, mirror(-3+1, height-1) * stride_line);
1212
    cs->b3 = slice_buffer_get_line(sb, mirror(-3+2, height-1) * stride_line);
1213
    cs->y = -3;
1214
}
1215

  
1216
static void spatial_compose97i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride){
1217
    cs->b0 = buffer + mirror(-3-1, height-1)*stride;
1218
    cs->b1 = buffer + mirror(-3  , height-1)*stride;
1219
    cs->b2 = buffer + mirror(-3+1, height-1)*stride;
1220
    cs->b3 = buffer + mirror(-3+2, height-1)*stride;
1221
    cs->y = -3;
1222
}
1223

  
1224
static void spatial_compose97i_dy_buffered(DSPContext *dsp, DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line){
1225
    int y = cs->y;
1226

  
1227
    IDWTELEM *b0= cs->b0;
1228
    IDWTELEM *b1= cs->b1;
1229
    IDWTELEM *b2= cs->b2;
1230
    IDWTELEM *b3= cs->b3;
1231
    IDWTELEM *b4= slice_buffer_get_line(sb, mirror(y + 3, height - 1) * stride_line);
1232
    IDWTELEM *b5= slice_buffer_get_line(sb, mirror(y + 4, height - 1) * stride_line);
1233

  
1234
    if(y>0 && y+4<height){
1235
        dsp->vertical_compose97i(b0, b1, b2, b3, b4, b5, width);
1236
    }else{
1237
        if(y+3<(unsigned)height) vertical_compose97iL1(b3, b4, b5, width);
1238
        if(y+2<(unsigned)height) vertical_compose97iH1(b2, b3, b4, width);
1239
        if(y+1<(unsigned)height) vertical_compose97iL0(b1, b2, b3, width);
1240
        if(y+0<(unsigned)height) vertical_compose97iH0(b0, b1, b2, width);
1241
    }
1242

  
1243
    if(y-1<(unsigned)height) dsp->horizontal_compose97i(b0, width);
1244
    if(y+0<(unsigned)height) dsp->horizontal_compose97i(b1, width);
1245

  
1246
    cs->b0=b2;
1247
    cs->b1=b3;
1248
    cs->b2=b4;
1249
    cs->b3=b5;
1250
    cs->y += 2;
1251
}
1252

  
1253
static void spatial_compose97i_dy(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride){
1254
    int y = cs->y;
1255
    IDWTELEM *b0= cs->b0;
1256
    IDWTELEM *b1= cs->b1;
1257
    IDWTELEM *b2= cs->b2;
1258
    IDWTELEM *b3= cs->b3;
1259
    IDWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
1260
    IDWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
1261

  
1262
    if(y+3<(unsigned)height) vertical_compose97iL1(b3, b4, b5, width);
1263
    if(y+2<(unsigned)height) vertical_compose97iH1(b2, b3, b4, width);
1264
    if(y+1<(unsigned)height) vertical_compose97iL0(b1, b2, b3, width);
1265
    if(y+0<(unsigned)height) vertical_compose97iH0(b0, b1, b2, width);
1266

  
1267
    if(y-1<(unsigned)height) ff_snow_horizontal_compose97i(b0, width);
1268
    if(y+0<(unsigned)height) ff_snow_horizontal_compose97i(b1, width);
1269

  
1270
    cs->b0=b2;
1271
    cs->b1=b3;
1272
    cs->b2=b4;
1273
    cs->b3=b5;
1274
    cs->y += 2;
1275
}
1276

  
1277
static void av_unused spatial_compose97i(IDWTELEM *buffer, int width, int height, int stride){
1278
    DWTCompose cs;
1279
    spatial_compose97i_init(&cs, buffer, height, stride);
1280
    while(cs.y <= height)
1281
        spatial_compose97i_dy(&cs, buffer, width, height, stride);
1282
}
1283

  
1284
static void ff_spatial_idwt_buffered_init(DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line, int type, int decomposition_count){
1285
    int level;
1286
    for(level=decomposition_count-1; level>=0; level--){
1287
        switch(type){
1288
        case DWT_97: spatial_compose97i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
1289
        case DWT_53: spatial_compose53i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
1290
        }
1291
    }
1292
}
1293

  
1294
static void ff_spatial_idwt_buffered_slice(DSPContext *dsp, DWTCompose *cs, slice_buffer * slice_buf, int width, int height, int stride_line, int type, int decomposition_count, int y){
1295
    const int support = type==1 ? 3 : 5;
1296
    int level;
1297
    if(type==2) return;
1298

  
1299
    for(level=decomposition_count-1; level>=0; level--){
1300
        while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
1301
            switch(type){
1302
            case DWT_97: spatial_compose97i_dy_buffered(dsp, cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
1303
                break;
1304
            case DWT_53: spatial_compose53i_dy_buffered(cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
1305
                break;
1306
            }
1307
        }
1308
    }
1309
}
1310

  
1311 627
static inline void unpack_coeffs(SnowContext *s, SubBand *b, SubBand * parent, int orientation){
1312 628
    const int w= b->width;
1313 629
    const int h= b->height;
......
2043 1359
    }
2044 1360
#else
2045 1361
    if(sliced){
2046
        s->dsp.inner_add_yblock(obmc, obmc_stride, block, b_w, b_h, src_x,src_y, src_stride, sb, add, dst8);
1362
        s->dwt.inner_add_yblock(obmc, obmc_stride, block, b_w, b_h, src_x,src_y, src_stride, sb, add, dst8);
2047 1363
    }else{
2048 1364
        for(y=0; y<b_h; y++){
2049 1365
            //FIXME ugly misuse of obmc_stride
......
2366 1682
    s->max_ref_frames=1; //just make sure its not an invalid value in case of no initial keyframe
2367 1683

  
2368 1684
    dsputil_init(&s->dsp, avctx);
1685
    ff_dwt_init(&s->dwt);
2369 1686

  
2370 1687
#define mcf(dx,dy)\
2371 1688
    s->dsp.put_qpel_pixels_tab       [0][dy+dx/4]=\
......
2865 2182
            }
2866 2183

  
2867 2184
            for(; yd<slice_h; yd+=4){
2868
                ff_spatial_idwt_buffered_slice(&s->dsp, cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count, yd);
2185
                ff_spatial_idwt_buffered_slice(&s->dwt, cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count, yd);
2869 2186
            }
2870 2187

  
2871 2188
            if(s->qlog == LOSSLESS_QLOG){
......
3607 2924
    return distortion + rate*penalty_factor;
3608 2925
}
3609 2926

  
3610
static void ff_spatial_idwt_init(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
3611
    int level;
3612
    for(level=decomposition_count-1; level>=0; level--){
3613
        switch(type){
3614
        case DWT_97: spatial_compose97i_init(cs+level, buffer, height>>level, stride<<level); break;
3615
        case DWT_53: spatial_compose53i_init(cs+level, buffer, height>>level, stride<<level); break;
3616
        }
3617
    }
3618
}
3619

  
3620
static void ff_spatial_idwt_slice(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count, int y){
3621
    const int support = type==1 ? 3 : 5;
3622
    int level;
3623
    if(type==2) return;
3624

  
3625
    for(level=decomposition_count-1; level>=0; level--){
3626
        while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
3627
            switch(type){
3628
            case DWT_97: spatial_compose97i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
3629
                break;
3630
            case DWT_53: spatial_compose53i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
3631
                break;
3632
            }
3633
        }
3634
    }
3635
}
3636

  
3637
static void ff_spatial_idwt(IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
3638
        DWTCompose cs[MAX_DECOMPOSITIONS];
3639
        int y;
3640
        ff_spatial_idwt_init(cs, buffer, width, height, stride, type, decomposition_count);
3641
        for(y=0; y<height; y+=4)
3642
            ff_spatial_idwt_slice(cs, buffer, width, height, stride, type, decomposition_count, y);
3643
}
3644

  
3645 2927
static int encode_subband_c0run(SnowContext *s, SubBand *b, IDWTELEM *src, IDWTELEM *parent, int stride, int orientation){
3646 2928
    const int w= b->width;
3647 2929
    const int h= b->height;

Also available in: Unified diff