00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "avcodec.h"
00020 #include "common.h"
00021 #include "dsputil.h"
00022
00023 #include "rangecoder.h"
00024 #define MID_STATE 128
00025
00026 #include "mpegvideo.h"
00027
00028 #undef NDEBUG
00029 #include <assert.h>
00030
00031 #define MAX_DECOMPOSITIONS 8
00032 #define MAX_PLANES 4
00033 #define DWTELEM int
00034 #define QSHIFT 5
00035 #define QROOT (1<<QSHIFT)
00036 #define LOSSLESS_QLOG -128
00037 #define FRAC_BITS 8
00038
00039 static const int8_t quant3[256]={
00040 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00041 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00042 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00043 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00044 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00045 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00046 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00047 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00048 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00049 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00050 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00051 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00052 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00053 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00054 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00055 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0,
00056 };
00057 static const int8_t quant3b[256]={
00058 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00059 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00060 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00061 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00062 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00063 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00064 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00065 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00066 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00067 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00068 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00069 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00070 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00071 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00072 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00073 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00074 };
00075 static const int8_t quant3bA[256]={
00076 0, 0, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
00077 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
00078 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
00079 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
00080 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
00081 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
00082 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
00083 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
00084 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
00085 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
00086 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
00087 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
00088 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
00089 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
00090 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
00091 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
00092 };
00093 static const int8_t quant5[256]={
00094 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00095 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00096 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00097 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00098 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00099 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00100 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00101 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00102 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00103 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00104 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00105 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00106 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00107 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00108 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00109 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,-1,
00110 };
00111 static const int8_t quant7[256]={
00112 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00113 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00114 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
00115 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
00116 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
00117 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
00118 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
00119 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
00120 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
00121 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
00122 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
00123 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
00124 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
00125 -3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-2,-2,-2,
00126 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00127 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,
00128 };
00129 static const int8_t quant9[256]={
00130 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
00131 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00132 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00133 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00134 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00135 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00136 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00137 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00138 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
00139 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
00140 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
00141 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
00142 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
00143 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
00144 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,
00145 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-1,-1,
00146 };
00147 static const int8_t quant11[256]={
00148 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
00149 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00150 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00151 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00152 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00153 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00154 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00155 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00156 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00157 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00158 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00159 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00160 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00161 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-4,-4,
00162 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
00163 -4,-4,-4,-4,-4,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-1,
00164 };
00165 static const int8_t quant13[256]={
00166 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
00167 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00168 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00169 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
00170 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
00171 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
00172 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
00173 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
00174 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
00175 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
00176 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
00177 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
00178 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5,
00179 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00180 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00181 -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
00182 };
00183
00184 #define LOG2_OBMC_MAX 6
00185 #define OBMC_MAX (1<<(LOG2_OBMC_MAX))
00186 #if 0 //64*cubic
00187 static const uint8_t obmc32[1024]={
00188 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00189 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
00190 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0,
00191 0, 0, 1, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 8, 8, 8, 7, 7, 6, 6, 5, 4, 4, 3, 2, 2, 1, 1, 0, 0,
00192 0, 0, 1, 2, 2, 3, 4, 6, 7, 8, 9,10,11,12,12,12,12,12,12,11,10, 9, 8, 7, 6, 4, 3, 2, 2, 1, 0, 0,
00193 0, 1, 1, 2, 3, 5, 6, 8,10,11,13,14,15,16,17,18,18,17,16,15,14,13,11,10, 8, 6, 5, 3, 2, 1, 1, 0,
00194 0, 1, 1, 3, 4, 6, 8,10,13,15,17,19,20,22,22,23,23,22,22,20,19,17,15,13,10, 8, 6, 4, 3, 1, 1, 0,
00195 0, 1, 2, 4, 6, 8,10,13,16,19,21,23,25,27,28,29,29,28,27,25,23,21,19,16,13,10, 8, 6, 4, 2, 1, 0,
00196 0, 1, 2, 4, 7,10,13,16,19,22,25,28,31,33,34,35,35,34,33,31,28,25,22,19,16,13,10, 7, 4, 2, 1, 0,
00197 0, 1, 3, 5, 8,11,15,19,22,26,30,33,36,38,40,41,41,40,38,36,33,30,26,22,19,15,11, 8, 5, 3, 1, 0,
00198 0, 1, 3, 6, 9,12,17,21,25,30,34,38,41,44,45,46,46,45,44,41,38,34,30,25,21,17,12, 9, 6, 3, 1, 0,
00199 0, 1, 3, 6,10,14,19,23,28,33,38,42,45,48,51,52,52,51,48,45,42,38,33,28,23,19,14,10, 6, 3, 1, 0,
00200 0, 1, 4, 7,11,15,20,25,31,36,41,45,49,52,55,56,56,55,52,49,45,41,36,31,25,20,15,11, 7, 4, 1, 0,
00201 0, 2, 4, 7,12,16,22,27,33,38,44,48,52,56,58,60,60,58,56,52,48,44,38,33,27,22,16,12, 7, 4, 2, 0,
00202 0, 1, 4, 8,12,17,22,28,34,40,45,51,55,58,61,62,62,61,58,55,51,45,40,34,28,22,17,12, 8, 4, 1, 0,
00203 0, 2, 4, 8,12,18,23,29,35,41,46,52,56,60,62,64,64,62,60,56,52,46,41,35,29,23,18,12, 8, 4, 2, 0,
00204 0, 2, 4, 8,12,18,23,29,35,41,46,52,56,60,62,64,64,62,60,56,52,46,41,35,29,23,18,12, 8, 4, 2, 0,
00205 0, 1, 4, 8,12,17,22,28,34,40,45,51,55,58,61,62,62,61,58,55,51,45,40,34,28,22,17,12, 8, 4, 1, 0,
00206 0, 2, 4, 7,12,16,22,27,33,38,44,48,52,56,58,60,60,58,56,52,48,44,38,33,27,22,16,12, 7, 4, 2, 0,
00207 0, 1, 4, 7,11,15,20,25,31,36,41,45,49,52,55,56,56,55,52,49,45,41,36,31,25,20,15,11, 7, 4, 1, 0,
00208 0, 1, 3, 6,10,14,19,23,28,33,38,42,45,48,51,52,52,51,48,45,42,38,33,28,23,19,14,10, 6, 3, 1, 0,
00209 0, 1, 3, 6, 9,12,17,21,25,30,34,38,41,44,45,46,46,45,44,41,38,34,30,25,21,17,12, 9, 6, 3, 1, 0,
00210 0, 1, 3, 5, 8,11,15,19,22,26,30,33,36,38,40,41,41,40,38,36,33,30,26,22,19,15,11, 8, 5, 3, 1, 0,
00211 0, 1, 2, 4, 7,10,13,16,19,22,25,28,31,33,34,35,35,34,33,31,28,25,22,19,16,13,10, 7, 4, 2, 1, 0,
00212 0, 1, 2, 4, 6, 8,10,13,16,19,21,23,25,27,28,29,29,28,27,25,23,21,19,16,13,10, 8, 6, 4, 2, 1, 0,
00213 0, 1, 1, 3, 4, 6, 8,10,13,15,17,19,20,22,22,23,23,22,22,20,19,17,15,13,10, 8, 6, 4, 3, 1, 1, 0,
00214 0, 1, 1, 2, 3, 5, 6, 8,10,11,13,14,15,16,17,18,18,17,16,15,14,13,11,10, 8, 6, 5, 3, 2, 1, 1, 0,
00215 0, 0, 1, 2, 2, 3, 4, 6, 7, 8, 9,10,11,12,12,12,12,12,12,11,10, 9, 8, 7, 6, 4, 3, 2, 2, 1, 0, 0,
00216 0, 0, 1, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 8, 8, 8, 7, 7, 6, 6, 5, 4, 4, 3, 2, 2, 1, 1, 0, 0,
00217 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0,
00218 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
00219 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00220
00221 };
00222 static const uint8_t obmc16[256]={
00223 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
00224 0, 1, 1, 2, 4, 5, 5, 6, 6, 5, 5, 4, 2, 1, 1, 0,
00225 0, 1, 4, 6, 9,11,13,15,15,13,11, 9, 6, 4, 1, 0,
00226 0, 2, 6,11,15,20,24,26,26,24,20,15,11, 6, 2, 0,
00227 0, 4, 9,15,23,29,34,38,38,34,29,23,15, 9, 4, 0,
00228 0, 5,11,20,29,38,45,49,49,45,38,29,20,11, 5, 0,
00229 1, 5,13,24,34,45,53,57,57,53,45,34,24,13, 5, 1,
00230 1, 6,15,26,38,49,57,62,62,57,49,38,26,15, 6, 1,
00231 1, 6,15,26,38,49,57,62,62,57,49,38,26,15, 6, 1,
00232 1, 5,13,24,34,45,53,57,57,53,45,34,24,13, 5, 1,
00233 0, 5,11,20,29,38,45,49,49,45,38,29,20,11, 5, 0,
00234 0, 4, 9,15,23,29,34,38,38,34,29,23,15, 9, 4, 0,
00235 0, 2, 6,11,15,20,24,26,26,24,20,15,11, 6, 2, 0,
00236 0, 1, 4, 6, 9,11,13,15,15,13,11, 9, 6, 4, 1, 0,
00237 0, 1, 1, 2, 4, 5, 5, 6, 6, 5, 5, 4, 2, 1, 1, 0,
00238 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
00239
00240 };
00241 #elif 1 // 64*linear
00242 static const uint8_t obmc32[1024]={
00243 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
00244 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 5, 5, 5, 4, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0,
00245 0, 1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 8, 9,10,10, 9, 8, 8, 7, 7, 6, 5, 5, 4, 3, 3, 2, 2, 1, 0,
00246 0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9,10,11,12,13,14,14,13,12,11,10, 9, 8, 7, 7, 6, 5, 4, 3, 2, 1, 0,
00247 1, 2, 3, 4, 5, 6, 7, 8,10,11,12,13,14,15,16,17,17,16,15,14,13,12,11,10, 8, 7, 6, 5, 4, 3, 2, 1,
00248 1, 2, 3, 5, 6, 8, 9,10,12,13,14,16,17,19,20,21,21,20,19,17,16,14,13,12,10, 9, 8, 6, 5, 3, 2, 1,
00249 1, 2, 4, 6, 7, 9,11,12,14,15,17,19,20,22,24,25,25,24,22,20,19,17,15,14,12,11, 9, 7, 6, 4, 2, 1,
00250 1, 3, 5, 7, 8,10,12,14,16,18,20,22,23,25,27,29,29,27,25,23,22,20,18,16,14,12,10, 8, 7, 5, 3, 1,
00251 1, 3, 5, 7,10,12,14,16,18,20,22,24,27,29,31,33,33,31,29,27,24,22,20,18,16,14,12,10, 7, 5, 3, 1,
00252 1, 4, 6, 8,11,13,15,18,20,23,25,27,30,32,34,37,37,34,32,30,27,25,23,20,18,15,13,11, 8, 6, 4, 1,
00253 1, 4, 7, 9,12,14,17,20,22,25,28,30,33,35,38,41,41,38,35,33,30,28,25,22,20,17,14,12, 9, 7, 4, 1,
00254 1, 4, 7,10,13,16,19,22,24,27,30,33,36,39,42,45,45,42,39,36,33,30,27,24,22,19,16,13,10, 7, 4, 1,
00255 2, 5, 8,11,14,17,20,23,27,30,33,36,39,42,45,48,48,45,42,39,36,33,30,27,23,20,17,14,11, 8, 5, 2,
00256 2, 5, 8,12,15,19,22,25,29,32,35,39,42,46,49,52,52,49,46,42,39,35,32,29,25,22,19,15,12, 8, 5, 2,
00257 2, 5, 9,13,16,20,24,27,31,34,38,42,45,49,53,56,56,53,49,45,42,38,34,31,27,24,20,16,13, 9, 5, 2,
00258 2, 6,10,14,17,21,25,29,33,37,41,45,48,52,56,60,60,56,52,48,45,41,37,33,29,25,21,17,14,10, 6, 2,
00259 2, 6,10,14,17,21,25,29,33,37,41,45,48,52,56,60,60,56,52,48,45,41,37,33,29,25,21,17,14,10, 6, 2,
00260 2, 5, 9,13,16,20,24,27,31,34,38,42,45,49,53,56,56,53,49,45,42,38,34,31,27,24,20,16,13, 9, 5, 2,
00261 2, 5, 8,12,15,19,22,25,29,32,35,39,42,46,49,52,52,49,46,42,39,35,32,29,25,22,19,15,12, 8, 5, 2,
00262 2, 5, 8,11,14,17,20,23,27,30,33,36,39,42,45,48,48,45,42,39,36,33,30,27,23,20,17,14,11, 8, 5, 2,
00263 1, 4, 7,10,13,16,19,22,24,27,30,33,36,39,42,45,45,42,39,36,33,30,27,24,22,19,16,13,10, 7, 4, 1,
00264 1, 4, 7, 9,12,14,17,20,22,25,28,30,33,35,38,41,41,38,35,33,30,28,25,22,20,17,14,12, 9, 7, 4, 1,
00265 1, 4, 6, 8,11,13,15,18,20,23,25,27,30,32,34,37,37,34,32,30,27,25,23,20,18,15,13,11, 8, 6, 4, 1,
00266 1, 3, 5, 7,10,12,14,16,18,20,22,24,27,29,31,33,33,31,29,27,24,22,20,18,16,14,12,10, 7, 5, 3, 1,
00267 1, 3, 5, 7, 8,10,12,14,16,18,20,22,23,25,27,29,29,27,25,23,22,20,18,16,14,12,10, 8, 7, 5, 3, 1,
00268 1, 2, 4, 6, 7, 9,11,12,14,15,17,19,20,22,24,25,25,24,22,20,19,17,15,14,12,11, 9, 7, 6, 4, 2, 1,
00269 1, 2, 3, 5, 6, 8, 9,10,12,13,14,16,17,19,20,21,21,20,19,17,16,14,13,12,10, 9, 8, 6, 5, 3, 2, 1,
00270 1, 2, 3, 4, 5, 6, 7, 8,10,11,12,13,14,15,16,17,17,16,15,14,13,12,11,10, 8, 7, 6, 5, 4, 3, 2, 1,
00271 0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9,10,11,12,13,14,14,13,12,11,10, 9, 8, 7, 7, 6, 5, 4, 3, 2, 1, 0,
00272 0, 1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 8, 9,10,10, 9, 8, 8, 7, 7, 6, 5, 5, 4, 3, 3, 2, 2, 1, 0,
00273 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 5, 5, 5, 4, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0,
00274 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
00275
00276 };
00277 static const uint8_t obmc16[256]={
00278 0, 1, 1, 2, 2, 3, 3, 4, 4, 3, 3, 2, 2, 1, 1, 0,
00279 1, 2, 4, 5, 7, 8,10,11,11,10, 8, 7, 5, 4, 2, 1,
00280 1, 4, 6, 9,11,14,16,19,19,16,14,11, 9, 6, 4, 1,
00281 2, 5, 9,12,16,19,23,26,26,23,19,16,12, 9, 5, 2,
00282 2, 7,11,16,20,25,29,34,34,29,25,20,16,11, 7, 2,
00283 3, 8,14,19,25,30,36,41,41,36,30,25,19,14, 8, 3,
00284 3,10,16,23,29,36,42,49,49,42,36,29,23,16,10, 3,
00285 4,11,19,26,34,41,49,56,56,49,41,34,26,19,11, 4,
00286 4,11,19,26,34,41,49,56,56,49,41,34,26,19,11, 4,
00287 3,10,16,23,29,36,42,49,49,42,36,29,23,16,10, 3,
00288 3, 8,14,19,25,30,36,41,41,36,30,25,19,14, 8, 3,
00289 2, 7,11,16,20,25,29,34,34,29,25,20,16,11, 7, 2,
00290 2, 5, 9,12,16,19,23,26,26,23,19,16,12, 9, 5, 2,
00291 1, 4, 6, 9,11,14,16,19,19,16,14,11, 9, 6, 4, 1,
00292 1, 2, 4, 5, 7, 8,10,11,11,10, 8, 7, 5, 4, 2, 1,
00293 0, 1, 1, 2, 2, 3, 3, 4, 4, 3, 3, 2, 2, 1, 1, 0,
00294
00295 };
00296 #else //64*cos
00297 static const uint8_t obmc32[1024]={
00298 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00299 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
00300 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0,
00301 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0,
00302 0, 0, 1, 1, 2, 3, 4, 5, 6, 7, 9,10,11,11,12,12,12,12,11,11,10, 9, 7, 6, 5, 4, 3, 2, 1, 1, 0, 0,
00303 0, 0, 1, 2, 3, 5, 6, 8, 9,11,12,14,15,16,17,17,17,17,16,15,14,12,11, 9, 8, 6, 5, 3, 2, 1, 0, 0,
00304 0, 1, 1, 2, 4, 6, 8,10,12,15,17,19,20,21,22,23,23,22,21,20,19,17,15,12,10, 8, 6, 4, 2, 1, 1, 0,
00305 0, 1, 2, 3, 5, 8,10,13,16,19,21,24,26,27,28,29,29,28,27,26,24,21,19,16,13,10, 8, 5, 3, 2, 1, 0,
00306 0, 1, 2, 4, 6, 9,12,16,19,23,26,29,31,33,34,35,35,34,33,31,29,26,23,19,16,12, 9, 6, 4, 2, 1, 0,
00307 0, 1, 3, 5, 7,11,15,19,23,26,30,34,37,39,40,41,41,40,39,37,34,30,26,23,19,15,11, 7, 5, 3, 1, 0,
00308 0, 1, 3, 5, 9,12,17,21,26,30,35,38,42,44,46,47,47,46,44,42,38,35,30,26,21,17,12, 9, 5, 3, 1, 0,
00309 0, 1, 3, 6, 9,14,19,24,29,34,38,43,46,49,51,52,52,51,49,46,43,38,34,29,24,19,14, 9, 6, 3, 1, 0,
00310 0, 1, 3, 6,11,15,20,26,31,37,42,46,50,53,56,57,57,56,53,50,46,42,37,31,26,20,15,11, 6, 3, 1, 0,
00311 0, 1, 3, 7,11,16,21,27,33,39,44,49,53,57,59,60,60,59,57,53,49,44,39,33,27,21,16,11, 7, 3, 1, 0,
00312 0, 1, 4, 7,12,17,22,28,34,40,46,51,56,59,61,63,63,61,59,56,51,46,40,34,28,22,17,12, 7, 4, 1, 0,
00313 0, 1, 4, 7,12,17,23,29,35,41,47,52,57,60,63,64,64,63,60,57,52,47,41,35,29,23,17,12, 7, 4, 1, 0,
00314 0, 1, 4, 7,12,17,23,29,35,41,47,52,57,60,63,64,64,63,60,57,52,47,41,35,29,23,17,12, 7, 4, 1, 0,
00315 0, 1, 4, 7,12,17,22,28,34,40,46,51,56,59,61,63,63,61,59,56,51,46,40,34,28,22,17,12, 7, 4, 1, 0,
00316 0, 1, 3, 7,11,16,21,27,33,39,44,49,53,57,59,60,60,59,57,53,49,44,39,33,27,21,16,11, 7, 3, 1, 0,
00317 0, 1, 3, 6,11,15,20,26,31,37,42,46,50,53,56,57,57,56,53,50,46,42,37,31,26,20,15,11, 6, 3, 1, 0,
00318 0, 1, 3, 6, 9,14,19,24,29,34,38,43,46,49,51,52,52,51,49,46,43,38,34,29,24,19,14, 9, 6, 3, 1, 0,
00319 0, 1, 3, 5, 9,12,17,21,26,30,35,38,42,44,46,47,47,46,44,42,38,35,30,26,21,17,12, 9, 5, 3, 1, 0,
00320 0, 1, 3, 5, 7,11,15,19,23,26,30,34,37,39,40,41,41,40,39,37,34,30,26,23,19,15,11, 7, 5, 3, 1, 0,
00321 0, 1, 2, 4, 6, 9,12,16,19,23,26,29,31,33,34,35,35,34,33,31,29,26,23,19,16,12, 9, 6, 4, 2, 1, 0,
00322 0, 1, 2, 3, 5, 8,10,13,16,19,21,24,26,27,28,29,29,28,27,26,24,21,19,16,13,10, 8, 5, 3, 2, 1, 0,
00323 0, 1, 1, 2, 4, 6, 8,10,12,15,17,19,20,21,22,23,23,22,21,20,19,17,15,12,10, 8, 6, 4, 2, 1, 1, 0,
00324 0, 0, 1, 2, 3, 5, 6, 8, 9,11,12,14,15,16,17,17,17,17,16,15,14,12,11, 9, 8, 6, 5, 3, 2, 1, 0, 0,
00325 0, 0, 1, 1, 2, 3, 4, 5, 6, 7, 9,10,11,11,12,12,12,12,11,11,10, 9, 7, 6, 5, 4, 3, 2, 1, 1, 0, 0,
00326 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0,
00327 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0,
00328 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
00329 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00330
00331 };
00332 static const uint8_t obmc16[256]={
00333 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
00334 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0,
00335 0, 1, 3, 6, 8,11,13,14,14,13,11, 8, 6, 3, 1, 0,
00336 0, 2, 6,10,15,20,24,26,26,24,20,15,10, 6, 2, 0,
00337 0, 3, 8,16,23,30,35,38,38,35,30,23,16, 8, 3, 0,
00338 1, 4,11,20,30,39,46,49,49,46,39,30,20,11, 4, 1,
00339 1, 5,13,24,35,46,54,58,58,54,46,35,24,13, 5, 1,
00340 0, 5,14,26,38,49,58,63,63,58,49,38,26,14, 5, 0,
00341 0, 5,14,26,38,49,58,63,63,58,49,38,26,14, 5, 0,
00342 1, 5,13,24,35,46,54,58,58,54,46,35,24,13, 5, 1,
00343 1, 4,11,20,30,39,46,49,49,46,39,30,20,11, 4, 1,
00344 0, 3, 8,16,23,30,35,38,38,35,30,23,16, 8, 3, 0,
00345 0, 2, 6,10,15,20,24,26,26,24,20,15,10, 6, 2, 0,
00346 0, 1, 3, 6, 8,11,13,14,14,13,11, 8, 6, 3, 1, 0,
00347 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0,
00348 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
00349
00350 };
00351 #endif
00352
00353
00354 static const uint8_t obmc8[64]={
00355 1, 3, 5, 7, 7, 5, 3, 1,
00356 3, 9,15,21,21,15, 9, 3,
00357 5,15,25,35,35,25,15, 5,
00358 7,21,35,49,49,35,21, 7,
00359 7,21,35,49,49,35,21, 7,
00360 5,15,25,35,35,25,15, 5,
00361 3, 9,15,21,21,15, 9, 3,
00362 1, 3, 5, 7, 7, 5, 3, 1,
00363
00364 };
00365
00366
00367 static const uint8_t obmc4[16]={
00368 4,12,12, 4,
00369 12,36,36,12,
00370 12,36,36,12,
00371 4,12,12, 4,
00372
00373 };
00374
00375 static const uint8_t *obmc_tab[4]={
00376 obmc32, obmc16, obmc8, obmc4
00377 };
00378
00379 typedef struct BlockNode{
00380 int16_t mx;
00381 int16_t my;
00382 uint8_t color[3];
00383 uint8_t type;
00384
00385 #define BLOCK_INTRA 1
00386
00387 uint8_t level;
00388 }BlockNode;
00389
00390 #define LOG2_MB_SIZE 4
00391 #define MB_SIZE (1<<LOG2_MB_SIZE)
00392
00393 typedef struct x_and_coeff{
00394 int16_t x;
00395 uint16_t coeff;
00396 } x_and_coeff;
00397
00398 typedef struct SubBand{
00399 int level;
00400 int stride;
00401 int width;
00402 int height;
00403 int qlog;
00404 DWTELEM *buf;
00405 int buf_x_offset;
00406 int buf_y_offset;
00407 int stride_line;
00408 x_and_coeff * x_coeff;
00409 struct SubBand *parent;
00410 uint8_t state[ 7 + 512][32];
00411 }SubBand;
00412
00413 typedef struct Plane{
00414 int width;
00415 int height;
00416 SubBand band[MAX_DECOMPOSITIONS][4];
00417 }Plane;
00418
00420 typedef struct {
00421 DWTELEM * * line;
00422 DWTELEM * * data_stack;
00423 int data_stack_top;
00424 int line_count;
00425 int line_width;
00426 int data_count;
00427 DWTELEM * base_buffer;
00428 } slice_buffer;
00429
00430 typedef struct SnowContext{
00431
00432
00433 AVCodecContext *avctx;
00434 RangeCoder c;
00435 DSPContext dsp;
00436 AVFrame input_picture;
00437 AVFrame current_picture;
00438 AVFrame last_picture;
00439 AVFrame mconly_picture;
00440
00441 uint8_t header_state[32];
00442 uint8_t block_state[128 + 32*128];
00443 int keyframe;
00444 int always_reset;
00445 int version;
00446 int spatial_decomposition_type;
00447 int temporal_decomposition_type;
00448 int spatial_decomposition_count;
00449 int temporal_decomposition_count;
00450 DWTELEM *spatial_dwt_buffer;
00451 int colorspace_type;
00452 int chroma_h_shift;
00453 int chroma_v_shift;
00454 int spatial_scalability;
00455 int qlog;
00456 int lambda;
00457 int lambda2;
00458 int mv_scale;
00459 int qbias;
00460 #define QBIAS_SHIFT 3
00461 int b_width;
00462 int b_height;
00463 int block_max_depth;
00464 Plane plane[MAX_PLANES];
00465 BlockNode *block;
00466 slice_buffer sb;
00467
00468 MpegEncContext m;
00469 }SnowContext;
00470
00471 typedef struct {
00472 DWTELEM *b0;
00473 DWTELEM *b1;
00474 DWTELEM *b2;
00475 DWTELEM *b3;
00476 int y;
00477 } dwt_compose_t;
00478
00479 #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)))
00480
00481
00482 static void slice_buffer_init(slice_buffer * buf, int line_count, int max_allocated_lines, int line_width, DWTELEM * base_buffer)
00483 {
00484 int i;
00485
00486 buf->base_buffer = base_buffer;
00487 buf->line_count = line_count;
00488 buf->line_width = line_width;
00489 buf->data_count = max_allocated_lines;
00490 buf->line = (DWTELEM * *) av_mallocz (sizeof(DWTELEM *) * line_count);
00491 buf->data_stack = (DWTELEM * *) av_malloc (sizeof(DWTELEM *) * max_allocated_lines);
00492
00493 for (i = 0; i < max_allocated_lines; i++)
00494 {
00495 buf->data_stack[i] = (DWTELEM *) av_malloc (sizeof(DWTELEM) * line_width);
00496 }
00497
00498 buf->data_stack_top = max_allocated_lines - 1;
00499 }
00500
00501 static DWTELEM * slice_buffer_load_line(slice_buffer * buf, int line)
00502 {
00503 int i;
00504 int offset;
00505 DWTELEM * buffer;
00506
00507
00508
00509 assert(buf->data_stack_top >= 0);
00510
00511 if (buf->line[line])
00512 return buf->line[line];
00513
00514 offset = buf->line_width * line;
00515 buffer = buf->data_stack[buf->data_stack_top];
00516 buf->data_stack_top--;
00517 buf->line[line] = buffer;
00518
00519
00520
00521 return buffer;
00522 }
00523
00524 static void slice_buffer_release(slice_buffer * buf, int line)
00525 {
00526 int i;
00527 int offset;
00528 DWTELEM * buffer;
00529
00530 assert(line >= 0 && line < buf->line_count);
00531 assert(buf->line[line]);
00532
00533 offset = buf->line_width * line;
00534 buffer = buf->line[line];
00535 buf->data_stack_top++;
00536 buf->data_stack[buf->data_stack_top] = buffer;
00537 buf->line[line] = NULL;
00538
00539
00540 }
00541
00542 static void slice_buffer_flush(slice_buffer * buf)
00543 {
00544 int i;
00545 for (i = 0; i < buf->line_count; i++)
00546 {
00547 if (buf->line[i])
00548 {
00549
00550 slice_buffer_release(buf, i);
00551 }
00552 }
00553 }
00554
00555 static void slice_buffer_destroy(slice_buffer * buf)
00556 {
00557 int i;
00558 slice_buffer_flush(buf);
00559
00560 for (i = buf->data_count - 1; i >= 0; i--)
00561 {
00562 assert(buf->data_stack[i]);
00563 av_free(buf->data_stack[i]);
00564 }
00565 assert(buf->data_stack);
00566 av_free(buf->data_stack);
00567 assert(buf->line);
00568 av_free(buf->line);
00569 }
00570
00571 #ifdef __sgi
00572
00573 #undef qexp
00574 #endif
00575 #define QEXPSHIFT (7-FRAC_BITS+8) //FIXME try to change this to 0
00576 static uint8_t qexp[QROOT];
00577
00578 static inline int mirror(int v, int m){
00579 if (v<0) return -v;
00580 else if(v>m) return 2*m-v;
00581 else return v;
00582 }
00583
00584 static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
00585 int i;
00586
00587 if(v){
00588 const int a= ABS(v);
00589 const int e= av_log2(a);
00590 #if 1
00591 const int el= FFMIN(e, 10);
00592 put_rac(c, state+0, 0);
00593
00594 for(i=0; i<el; i++){
00595 put_rac(c, state+1+i, 1);
00596 }
00597 for(; i<e; i++){
00598 put_rac(c, state+1+9, 1);
00599 }
00600 put_rac(c, state+1+FFMIN(i,9), 0);
00601
00602 for(i=e-1; i>=el; i--){
00603 put_rac(c, state+22+9, (a>>i)&1);
00604 }
00605 for(; i>=0; i--){
00606 put_rac(c, state+22+i, (a>>i)&1);
00607 }
00608
00609 if(is_signed)
00610 put_rac(c, state+11 + el, v < 0);
00611 #else
00612
00613 put_rac(c, state+0, 0);
00614 if(e<=9){
00615 for(i=0; i<e; i++){
00616 put_rac(c, state+1+i, 1);
00617 }
00618 put_rac(c, state+1+i, 0);
00619
00620 for(i=e-1; i>=0; i--){
00621 put_rac(c, state+22+i, (a>>i)&1);
00622 }
00623
00624 if(is_signed)
00625 put_rac(c, state+11 + e, v < 0);
00626 }else{
00627 for(i=0; i<e; i++){
00628 put_rac(c, state+1+FFMIN(i,9), 1);
00629 }
00630 put_rac(c, state+1+FFMIN(i,9), 0);
00631
00632 for(i=e-1; i>=0; i--){
00633 put_rac(c, state+22+FFMIN(i,9), (a>>i)&1);
00634 }
00635
00636 if(is_signed)
00637 put_rac(c, state+11 + FFMIN(e,10), v < 0);
00638 }
00639 #endif
00640 }else{
00641 put_rac(c, state+0, 1);
00642 }
00643 }
00644
00645 static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
00646 if(get_rac(c, state+0))
00647 return 0;
00648 else{
00649 int i, e, a;
00650 e= 0;
00651 while(get_rac(c, state+1 + FFMIN(e,9))){
00652 e++;
00653 }
00654
00655 a= 1;
00656 for(i=e-1; i>=0; i--){
00657 a += a + get_rac(c, state+22 + FFMIN(i,9));
00658 }
00659
00660 if(is_signed && get_rac(c, state+11 + FFMIN(e,10)))
00661 return -a;
00662 else
00663 return a;
00664 }
00665 }
00666
00667 static inline void put_symbol2(RangeCoder *c, uint8_t *state, int v, int log2){
00668 int i;
00669 int r= log2>=0 ? 1<<log2 : 1;
00670
00671 assert(v>=0);
00672 assert(log2>=-4);
00673
00674 while(v >= r){
00675 put_rac(c, state+4+log2, 1);
00676 v -= r;
00677 log2++;
00678 if(log2>0) r+=r;
00679 }
00680 put_rac(c, state+4+log2, 0);
00681
00682 for(i=log2-1; i>=0; i--){
00683 put_rac(c, state+31-i, (v>>i)&1);
00684 }
00685 }
00686
00687 static inline int get_symbol2(RangeCoder *c, uint8_t *state, int log2){
00688 int i;
00689 int r= log2>=0 ? 1<<log2 : 1;
00690 int v=0;
00691
00692 assert(log2>=-4);
00693
00694 while(get_rac(c, state+4+log2)){
00695 v+= r;
00696 log2++;
00697 if(log2>0) r+=r;
00698 }
00699
00700 for(i=log2-1; i>=0; i--){
00701 v+= get_rac(c, state+31-i)<<i;
00702 }
00703
00704 return v;
00705 }
00706
00707 static always_inline void lift(DWTELEM *dst, DWTELEM *src, DWTELEM *ref, int dst_step, int src_step, int ref_step, int width, int mul, int add, int shift, int highpass, int inverse){
00708 const int mirror_left= !highpass;
00709 const int mirror_right= (width&1) ^ highpass;
00710 const int w= (width>>1) - 1 + (highpass & width);
00711 int i;
00712
00713 #define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
00714 if(mirror_left){
00715 dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
00716 dst += dst_step;
00717 src += src_step;
00718 }
00719
00720 for(i=0; i<w; i++){
00721 dst[i*dst_step] = LIFT(src[i*src_step], ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift), inverse);
00722 }
00723
00724 if(mirror_right){
00725 dst[w*dst_step] = LIFT(src[w*src_step], ((mul*2*ref[w*ref_step]+add)>>shift), inverse);
00726 }
00727 }
00728
00729 static always_inline void lift5(DWTELEM *dst, DWTELEM *src, DWTELEM *ref, int dst_step, int src_step, int ref_step, int width, int mul, int add, int shift, int highpass, int inverse){
00730 const int mirror_left= !highpass;
00731 const int mirror_right= (width&1) ^ highpass;
00732 const int w= (width>>1) - 1 + (highpass & width);
00733 int i;
00734
00735 if(mirror_left){
00736 int r= 3*2*ref[0];
00737 r += r>>4;
00738 r += r>>8;
00739 dst[0] = LIFT(src[0], ((r+add)>>shift), inverse);
00740 dst += dst_step;
00741 src += src_step;
00742 }
00743
00744 for(i=0; i<w; i++){
00745 int r= 3*(ref[i*ref_step] + ref[(i+1)*ref_step]);
00746 r += r>>4;
00747 r += r>>8;
00748 dst[i*dst_step] = LIFT(src[i*src_step], ((r+add)>>shift), inverse);
00749 }
00750
00751 if(mirror_right){
00752 int r= 3*2*ref[w*ref_step];
00753 r += r>>4;
00754 r += r>>8;
00755 dst[w*dst_step] = LIFT(src[w*src_step], ((r+add)>>shift), inverse);
00756 }
00757 }
00758
00759 static always_inline void liftS(DWTELEM *dst, DWTELEM *src, DWTELEM *ref, int dst_step, int src_step, int ref_step, int width, int mul, int add, int shift, int highpass, int inverse){
00760 const int mirror_left= !highpass;
00761 const int mirror_right= (width&1) ^ highpass;
00762 const int w= (width>>1) - 1 + (highpass & width);
00763 int i;
00764
00765 assert(shift == 4);
00766 #define LIFTS(src, ref, inv) ((inv) ? (src) - (((ref) - 4*(src))>>shift): (16*4*(src) + 4*(ref) + 8 + (5<<27))/(5*16) - (1<<23))
00767 if(mirror_left){
00768 dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse);
00769 dst += dst_step;
00770 src += src_step;
00771 }
00772
00773 for(i=0; i<w; i++){
00774 dst[i*dst_step] = LIFTS(src[i*src_step], mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add, inverse);
00775 }
00776
00777 if(mirror_right){
00778 dst[w*dst_step] = LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse);
00779 }
00780 }
00781
00782
00783 static void inplace_lift(DWTELEM *dst, int width, int *coeffs, int n, int shift, int start, int inverse){
00784 int x, i;
00785
00786 for(x=start; x<width; x+=2){
00787 int64_t sum=0;
00788
00789 for(i=0; i<n; i++){
00790 int x2= x + 2*i - n + 1;
00791 if (x2< 0) x2= -x2;
00792 else if(x2>=width) x2= 2*width-x2-2;
00793 sum += coeffs[i]*(int64_t)dst[x2];
00794 }
00795 if(inverse) dst[x] -= (sum + (1<<shift)/2)>>shift;
00796 else dst[x] += (sum + (1<<shift)/2)>>shift;
00797 }
00798 }
00799
00800 static void inplace_liftV(DWTELEM *dst, int width, int height, int stride, int *coeffs, int n, int shift, int start, int inverse){
00801 int x, y, i;
00802 for(y=start; y<height; y+=2){
00803 for(x=0; x<width; x++){
00804 int64_t sum=0;
00805
00806 for(i=0; i<n; i++){
00807 int y2= y + 2*i - n + 1;
00808 if (y2< 0) y2= -y2;
00809 else if(y2>=height) y2= 2*height-y2-2;
00810 sum += coeffs[i]*(int64_t)dst[x + y2*stride];
00811 }
00812 if(inverse) dst[x + y*stride] -= (sum + (1<<shift)/2)>>shift;
00813 else dst[x + y*stride] += (sum + (1<<shift)/2)>>shift;
00814 }
00815 }
00816 }
00817
00818 #define SCALEX 1
00819 #define LX0 0
00820 #define LX1 1
00821
00822 #if 0 // more accurate 9/7
00823 #define N1 2
00824 #define SHIFT1 14
00825 #define COEFFS1 (int[]){-25987,-25987}
00826 #define N2 2
00827 #define SHIFT2 19
00828 #define COEFFS2 (int[]){-27777,-27777}
00829 #define N3 2
00830 #define SHIFT3 15
00831 #define COEFFS3 (int[]){28931,28931}
00832 #define N4 2
00833 #define SHIFT4 15
00834 #define COEFFS4 (int[]){14533,14533}
00835 #elif 1 // 13/7 CRF
00836 #define N1 4
00837 #define SHIFT1 4
00838 #define COEFFS1 (int[]){1,-9,-9,1}
00839 #define N2 4
00840 #define SHIFT2 4
00841 #define COEFFS2 (int[]){-1,5,5,-1}
00842 #define N3 0
00843 #define SHIFT3 1
00844 #define COEFFS3 NULL
00845 #define N4 0
00846 #define SHIFT4 1
00847 #define COEFFS4 NULL
00848 #elif 1 // 3/5
00849 #define LX0 1
00850 #define LX1 0
00851 #define SCALEX 0.5
00852 #define N1 2
00853 #define SHIFT1 1
00854 #define COEFFS1 (int[]){1,1}
00855 #define N2 2
00856 #define SHIFT2 2
00857 #define COEFFS2 (int[]){-1,-1}
00858 #define N3 0
00859 #define SHIFT3 0
00860 #define COEFFS3 NULL
00861 #define N4 0
00862 #define SHIFT4 0
00863 #define COEFFS4 NULL
00864 #elif 1 // 11/5
00865 #define N1 0
00866 #define SHIFT1 1
00867 #define COEFFS1 NULL
00868 #define N2 2
00869 #define SHIFT2 2
00870 #define COEFFS2 (int[]){-1,-1}
00871 #define N3 2
00872 #define SHIFT3 0
00873 #define COEFFS3 (int[]){-1,-1}
00874 #define N4 4
00875 #define SHIFT4 7
00876 #define COEFFS4 (int[]){-5,29,29,-5}
00877 #define SCALEX 4
00878 #elif 1 // 9/7 CDF
00879 #define N1 2
00880 #define SHIFT1 7
00881 #define COEFFS1 (int[]){-203,-203}
00882 #define N2 2
00883 #define SHIFT2 12
00884 #define COEFFS2 (int[]){-217,-217}
00885 #define N3 2
00886 #define SHIFT3 7
00887 #define COEFFS3 (int[]){113,113}
00888 #define N4 2
00889 #define SHIFT4 9
00890 #define COEFFS4 (int[]){227,227}
00891 #define SCALEX 1
00892 #elif 1 // 7/5 CDF
00893 #define N1 0
00894 #define SHIFT1 1
00895 #define COEFFS1 NULL
00896 #define N2 2
00897 #define SHIFT2 2
00898 #define COEFFS2 (int[]){-1,-1}
00899 #define N3 2
00900 #define SHIFT3 0
00901 #define COEFFS3 (int[]){-1,-1}
00902 #define N4 2
00903 #define SHIFT4 4
00904 #define COEFFS4 (int[]){3,3}
00905 #elif 1 // 9/7 MN
00906 #define N1 4
00907 #define SHIFT1 4
00908 #define COEFFS1 (int[]){1,-9,-9,1}
00909 #define N2 2
00910 #define SHIFT2 2
00911 #define COEFFS2 (int[]){1,1}
00912 #define N3 0
00913 #define SHIFT3 1
00914 #define COEFFS3 NULL
00915 #define N4 0
00916 #define SHIFT4 1
00917 #define COEFFS4 NULL
00918 #else // 13/7 CRF
00919 #define N1 4
00920 #define SHIFT1 4
00921 #define COEFFS1 (int[]){1,-9,-9,1}
00922 #define N2 4
00923 #define SHIFT2 4
00924 #define COEFFS2 (int[]){-1,5,5,-1}
00925 #define N3 0
00926 #define SHIFT3 1
00927 #define COEFFS3 NULL
00928 #define N4 0
00929 #define SHIFT4 1
00930 #define COEFFS4 NULL
00931 #endif
00932 static void horizontal_decomposeX(DWTELEM *b, int width){
00933 DWTELEM temp[width];
00934 const int width2= width>>1;
00935 const int w2= (width+1)>>1;
00936 int A1,A2,A3,A4, x;
00937
00938 inplace_lift(b, width, COEFFS1, N1, SHIFT1, LX1, 0);
00939 inplace_lift(b, width, COEFFS2, N2, SHIFT2, LX0, 0);
00940 inplace_lift(b, width, COEFFS3, N3, SHIFT3, LX1, 0);
00941 inplace_lift(b, width, COEFFS4, N4, SHIFT4, LX0, 0);
00942
00943 for(x=0; x<width2; x++){
00944 temp[x ]= b[2*x ];
00945 temp[x+w2]= b[2*x + 1];
00946 }
00947 if(width&1)
00948 temp[x ]= b[2*x ];
00949 memcpy(b, temp, width*sizeof(int));
00950 }
00951
00952 static void horizontal_composeX(DWTELEM *b, int width){
00953 DWTELEM temp[width];
00954 const int width2= width>>1;
00955 int A1,A2,A3,A4, x;
00956 const int w2= (width+1)>>1;
00957
00958 memcpy(temp, b, width*sizeof(int));
00959 for(x=0; x<width2; x++){
00960 b[2*x ]= temp[x ];
00961 b[2*x + 1]= temp[x+w2];
00962 }
00963 if(width&1)
00964 b[2*x ]= temp[x ];
00965
00966 inplace_lift(b, width, COEFFS4, N4, SHIFT4, LX0, 1);
00967 inplace_lift(b, width, COEFFS3, N3, SHIFT3, LX1, 1);
00968 inplace_lift(b, width, COEFFS2, N2, SHIFT2, LX0, 1);
00969 inplace_lift(b, width, COEFFS1, N1, SHIFT1, LX1, 1);
00970 }
00971
00972 static void spatial_decomposeX(DWTELEM *buffer, int width, int height, int stride){
00973 int x, y;
00974
00975 for(y=0; y<height; y++){
00976 for(x=0; x<width; x++){
00977 buffer[y*stride + x] *= SCALEX;
00978 }
00979 }
00980
00981 for(y=0; y<height; y++){
00982 horizontal_decomposeX(buffer + y*stride, width);
00983 }
00984
00985 inplace_liftV(buffer, width, height, stride, COEFFS1, N1, SHIFT1, LX1, 0);
00986 inplace_liftV(buffer, width, height, stride, COEFFS2, N2, SHIFT2, LX0, 0);
00987 inplace_liftV(buffer, width, height, stride, COEFFS3, N3, SHIFT3, LX1, 0);
00988 inplace_liftV(buffer, width, height, stride, COEFFS4, N4, SHIFT4, LX0, 0);
00989 }
00990
00991 static void spatial_composeX(DWTELEM *buffer, int width, int height, int stride){
00992 int x, y;
00993
00994 inplace_liftV(buffer, width, height, stride, COEFFS4, N4, SHIFT4, LX0, 1);
00995 inplace_liftV(buffer, width, height, stride, COEFFS3, N3, SHIFT3, LX1, 1);
00996 inplace_liftV(buffer, width, height, stride, COEFFS2, N2, SHIFT2, LX0, 1);
00997 inplace_liftV(buffer, width, height, stride, COEFFS1, N1, SHIFT1, LX1, 1);
00998
00999 for(y=0; y<height; y++){
01000 horizontal_composeX(buffer + y*stride, width);
01001 }
01002
01003 for(y=0; y<height; y++){
01004 for(x=0; x<width; x++){
01005 buffer[y*stride + x] /= SCALEX;
01006 }
01007 }
01008 }
01009
01010 static void horizontal_decompose53i(DWTELEM *b, int width){
01011 DWTELEM temp[width];
01012 const int width2= width>>1;
01013 int A1,A2,A3,A4, x;
01014 const int w2= (width+1)>>1;
01015
01016 for(x=0; x<width2; x++){
01017 temp[x ]= b[2*x ];
01018 temp[x+w2]= b[2*x + 1];
01019 }
01020 if(width&1)
01021 temp[x ]= b[2*x ];
01022 #if 0
01023 A2= temp[1 ];
01024 A4= temp[0 ];
01025 A1= temp[0+width2];
01026 A1 -= (A2 + A4)>>1;
01027 A4 += (A1 + 1)>>1;
01028 b[0+width2] = A1;
01029 b[0 ] = A4;
01030 for(x=1; x+1<width2; x+=2){
01031 A3= temp[x+width2];
01032 A4= temp[x+1 ];
01033 A3 -= (A2 + A4)>>1;
01034 A2 += (A1 + A3 + 2)>>2;
01035 b[x+width2] = A3;
01036 b[x ] = A2;
01037
01038 A1= temp[x+1+width2];
01039 A2= temp[x+2 ];
01040 A1 -= (A2 + A4)>>1;
01041 A4 += (A1 + A3 + 2)>>2;
01042 b[x+1+width2] = A1;
01043 b[x+1 ] = A4;
01044 }
01045 A3= temp[width-1];
01046 A3 -= A2;
01047 A2 += (A1 + A3 + 2)>>2;
01048 b[width -1] = A3;
01049 b[width2-1] = A2;
01050 #else
01051 lift(b+w2, temp+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
01052 lift(b , temp , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 0);
01053 #endif
01054 }
01055
01056 static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
01057 int i;
01058
01059 for(i=0; i<width; i++){
01060 b1[i] -= (b0[i] + b2[i])>>1;
01061 }
01062 }
01063
01064 static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
01065 int i;
01066
01067 for(i=0; i<width; i++){
01068 b1[i] += (b0[i] + b2[i] + 2)>>2;
01069 }
01070 }
01071
01072 static void spatial_decompose53i(DWTELEM *buffer, int width, int height, int stride){
01073 int y;
01074 DWTELEM *b0= buffer + mirror(-2-1, height-1)*stride;
01075 DWTELEM *b1= buffer + mirror(-2 , height-1)*stride;
01076
01077 for(y=-2; y<height; y+=2){
01078 DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
01079 DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
01080
01081 {START_TIMER
01082 if(b1 <= b3) horizontal_decompose53i(b2, width);
01083 if(y+2 < height) horizontal_decompose53i(b3, width);
01084 STOP_TIMER("horizontal_decompose53i")}
01085
01086 {START_TIMER
01087 if(b1 <= b3) vertical_decompose53iH0(b1, b2, b3, width);
01088 if(b0 <= b2) vertical_decompose53iL0(b0, b1, b2, width);
01089 STOP_TIMER("vertical_decompose53i*")}
01090
01091 b0=b2;
01092 b1=b3;
01093 }
01094 }
01095
01096 #define liftS lift
01097 #define lift5 lift
01098 #if 1
01099 #define W_AM 3
01100 #define W_AO 0
01101 #define W_AS 1
01102
01103 #undef liftS
01104 #define W_BM 1
01105 #define W_BO 8
01106 #define W_BS 4
01107
01108 #define W_CM 1
01109 #define W_CO 0
01110 #define W_CS 0
01111
01112 #define W_DM 3
01113 #define W_DO 4
01114 #define W_DS 3
01115 #elif 0
01116 #define W_AM 55
01117 #define W_AO 16
01118 #define W_AS 5
01119
01120 #define W_BM 3
01121 #define W_BO 32
01122 #define W_BS 6
01123
01124 #define W_CM 127
01125 #define W_CO 64
01126 #define W_CS 7
01127
01128 #define W_DM 7
01129 #define W_DO 8
01130 #define W_DS 4
01131 #elif 0
01132 #define W_AM 97
01133 #define W_AO 32
01134 #define W_AS 6
01135
01136 #define W_BM 63
01137 #define W_BO 512
01138 #define W_BS 10
01139
01140 #define W_CM 13
01141 #define W_CO 8
01142 #define W_CS 4
01143
01144 #define W_DM 15
01145 #define W_DO 16
01146 #define W_DS 5
01147
01148 #else
01149
01150 #define W_AM 203
01151 #define W_AO 64
01152 #define W_AS 7
01153
01154 #define W_BM 217
01155 #define W_BO 2048
01156 #define W_BS 12
01157
01158 #define W_CM 113
01159 #define W_CO 64
01160 #define W_CS 7
01161
01162 #define W_DM 227
01163 #define W_DO 128
01164 #define W_DS 9
01165 #endif
01166 static void horizontal_decompose97i(DWTELEM *b, int width){
01167 DWTELEM temp[width];
01168 const int w2= (width+1)>>1;
01169
01170 lift (temp+w2, b +1, b , 1, 2, 2, width, -W_AM, W_AO, W_AS, 1, 0);
01171 liftS(temp , b , temp+w2, 1, 2, 1, width, -W_BM, W_BO, W_BS, 0, 0);
01172 lift5(b +w2, temp+w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 0);
01173 lift (b , temp , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 0);
01174 }
01175
01176
01177 static void vertical_decompose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
01178 int i;
01179
01180 for(i=0; i<width; i++){
01181 b1[i] -= (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
01182 }
01183 }
01184
01185 static void vertical_decompose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
01186 int i;
01187
01188 for(i=0; i<width; i++){
01189 #ifdef lift5
01190 b1[i] += (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
01191 #else
01192 int r= 3*(b0[i] + b2[i]);
01193 r+= r>>4;
01194 r+= r>>8;
01195 b1[i] += (r+W_CO)>>W_CS;
01196 #endif
01197 }
01198 }
01199
01200 static void vertical_decompose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
01201 int i;
01202
01203 for(i=0; i<width; i++){
01204 #ifdef liftS
01205 b1[i] -= (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
01206 #else
01207 b1[i] = (16*4*b1[i] - 4*(b0[i] + b2[i]) + 8*5 + (5<<27)) / (5*16) - (1<<23);
01208 #endif
01209 }
01210 }
01211
01212 static void vertical_decompose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
01213 int i;
01214
01215 for(i=0; i<width; i++){
01216 b1[i] += (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
01217 }
01218 }
01219
01220 static void spatial_decompose97i(DWTELEM *buffer, int width, int height, int stride){
01221 int y;
01222 DWTELEM *b0= buffer + mirror(-4-1, height-1)*stride;
01223 DWTELEM *b1= buffer + mirror(-4 , height-1)*stride;
01224 DWTELEM *b2= buffer + mirror(-4+1, height-1)*stride;
01225 DWTELEM *b3= buffer + mirror(-4+2, height-1)*stride;
01226
01227 for(y=-4; y<height; y+=2){
01228 DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
01229 DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
01230
01231 {START_TIMER
01232 if(b3 <= b5) horizontal_decompose97i(b4, width);
01233 if(y+4 < height) horizontal_decompose97i(b5, width);
01234 if(width>400){
01235 STOP_TIMER("horizontal_decompose97i")
01236 }}
01237
01238 {START_TIMER
01239 if(b3 <= b5) vertical_decompose97iH0(b3, b4, b5, width);
01240 if(b2 <= b4) vertical_decompose97iL0(b2, b3, b4, width);
01241 if(b1 <= b3) vertical_decompose97iH1(b1, b2, b3, width);
01242 if(b0 <= b2) vertical_decompose97iL1(b0, b1, b2, width);
01243
01244 if(width>400){
01245 STOP_TIMER("vertical_decompose97i")
01246 }}
01247
01248 b0=b2;
01249 b1=b3;
01250 b2=b4;
01251 b3=b5;
01252 }
01253 }
01254
01255 void ff_spatial_dwt(DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
01256 int level;
01257
01258 for(level=0; level<decomposition_count; level++){
01259 switch(type){
01260 case 0: spatial_decompose97i(buffer, width>>level, height>>level, stride<<level); break;
01261 case 1: spatial_decompose53i(buffer, width>>level, height>>level, stride<<level); break;
01262 case 2: spatial_decomposeX (buffer, width>>level, height>>level, stride<<level); break;
01263 }
01264 }
01265 }
01266
01267 static void horizontal_compose53i(DWTELEM *b, int width){
01268 DWTELEM temp[width];
01269 const int width2= width>>1;
01270 const int w2= (width+1)>>1;
01271 int A1,A2,A3,A4, x;
01272
01273 #if 0
01274 A2= temp[1 ];
01275 A4= temp[0 ];
01276 A1= temp[0+width2];
01277 A1 -= (A2 + A4)>>1;
01278 A4 += (A1 + 1)>>1;
01279 b[0+width2] = A1;
01280 b[0 ] = A4;
01281 for(x=1; x+1<width2; x+=2){
01282 A3= temp[x+width2];
01283 A4= temp[x+1 ];
01284 A3 -= (A2 + A4)>>1;
01285 A2 += (A1 + A3 + 2)>>2;
01286 b[x+width2] = A3;
01287 b[x ] = A2;
01288
01289 A1= temp[x+1+width2];
01290 A2= temp[x+2 ];
01291 A1 -= (A2 + A4)>>1;
01292 A4 += (A1 + A3 + 2)>>2;
01293 b[x+1+width2] = A1;
01294 b[x+1 ] = A4;
01295 }
01296 A3= temp[width-1];
01297 A3 -= A2;
01298 A2 += (A1 + A3 + 2)>>2;
01299 b[width -1] = A3;
01300 b[width2-1] = A2;
01301 #else
01302 lift(temp , b , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 1);
01303 lift(temp+w2, b+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 1);
01304 #endif
01305 for(x=0; x<width2; x++){
01306 b[2*x ]= temp[x ];
01307 b[2*x + 1]= temp[x+w2];
01308 }
01309 if(width&1)
01310 b[2*x ]= temp[x ];
01311 }
01312
01313 static void vertical_compose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
01314 int i;
01315
01316 for(i=0; i<width; i++){
01317 b1[i] += (b0[i] + b2[i])>>1;
01318 }
01319 }
01320
01321 static void vertical_compose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
01322 int i;
01323
01324 for(i=0; i<width; i++){
01325 b1[i] -= (b0[i] + b2[i] + 2)>>2;
01326 }
01327 }
01328
01329 static void spatial_compose53i_buffered_init(dwt_compose_t *cs, slice_buffer * sb, int height, int stride_line){
01330 cs->b0 = slice_buffer_get_line(sb, mirror(-1-1, height-1) * stride_line);
01331 cs->b1 = slice_buffer_get_line(sb, mirror(-1 , height-1) * stride_line);
01332 cs->y = -1;
01333 }
01334
01335 static void spatial_compose53i_init(dwt_compose_t *cs, DWTELEM *buffer, int height, int stride){
01336 cs->b0 = buffer + mirror(-1-1, height-1)*stride;
01337 cs->b1 = buffer + mirror(-1 , height-1)*stride;
01338 cs->y = -1;
01339 }
01340
01341 static void spatial_compose53i_dy_buffered(dwt_compose_t *cs, slice_buffer * sb, int width, int height, int stride_line){
01342 int y= cs->y;
01343 int mirror0 = mirror(y-1, height-1);
01344 int mirror1 = mirror(y , height-1);
01345 int mirror2 = mirror(y+1, height-1);
01346 int mirror3 = mirror(y+2, height-1);
01347
01348 DWTELEM *b0= cs->b0;
01349 DWTELEM *b1= cs->b1;
01350 DWTELEM *b2= slice_buffer_get_line(sb, mirror2 * stride_line);
01351 DWTELEM *b3= slice_buffer_get_line(sb, mirror3 * stride_line);
01352
01353 {START_TIMER
01354 if(mirror1 <= mirror3) vertical_compose53iL0(b1, b2, b3, width);
01355 if(mirror0 <= mirror2) vertical_compose53iH0(b0, b1, b2, width);
01356 STOP_TIMER("vertical_compose53i*")}
01357
01358 {START_TIMER
01359 if(y-1 >= 0) horizontal_compose53i(b0, width);
01360 if(mirror0 <= mirror2) horizontal_compose53i(b1, width);
01361 STOP_TIMER("horizontal_compose53i")}
01362
01363 cs->b0 = b2;
01364 cs->b1 = b3;
01365 cs->y += 2;
01366 }
01367
01368 static void spatial_compose53i_dy(dwt_compose_t *cs, DWTELEM *buffer, int width, int height, int stride){
01369 int y= cs->y;
01370 DWTELEM *b0= cs->b0;
01371 DWTELEM *b1= cs->b1;
01372 DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
01373 DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
01374
01375 {START_TIMER
01376 if(b1 <= b3) vertical_compose53iL0(b1, b2, b3, width);
01377 if(b0 <= b2) vertical_compose53iH0(b0, b1, b2, width);
01378 STOP_TIMER("vertical_compose53i*")}
01379
01380 {START_TIMER
01381 if(y-1 >= 0) horizontal_compose53i(b0, width);
01382 if(b0 <= b2) horizontal_compose53i(b1, width);
01383 STOP_TIMER("horizontal_compose53i")}
01384
01385 cs->b0 = b2;
01386 cs->b1 = b3;
01387 cs->y += 2;
01388 }
01389
01390 static void spatial_compose53i(DWTELEM *buffer, int width, int height, int stride){
01391 dwt_compose_t cs;
01392 spatial_compose53i_init(&cs, buffer, height, stride);
01393 while(cs.y <= height)
01394 spatial_compose53i_dy(&cs, buffer, width, height, stride);
01395 }
01396
01397
01398 static void horizontal_compose97i(DWTELEM *b, int width){
01399 DWTELEM temp[width];
01400 const int w2= (width+1)>>1;
01401
01402 lift (temp , b , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 1);
01403 lift5(temp+w2, b +w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 1);
01404 liftS(b , temp , temp+w2, 2, 1, 1, width, -W_BM, W_BO, W_BS, 0, 1);
01405 lift (b+1 , temp+w2, b , 2, 1, 2, width, -W_AM, W_AO, W_AS, 1, 1);
01406 }
01407
01408 static void vertical_compose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
01409 int i;
01410
01411 for(i=0; i<width; i++){
01412 b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
01413 }
01414 }
01415
01416 static void vertical_compose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
01417 int i;
01418
01419 for(i=0; i<width; i++){
01420 #ifdef lift5
01421 b1[i] -= (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
01422 #else
01423 int r= 3*(b0[i] + b2[i]);
01424 r+= r>>4;
01425 r+= r>>8;
01426 b1[i] -= (r+W_CO)>>W_CS;
01427 #endif
01428 }
01429 }
01430
01431 static void vertical_compose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
01432 int i;
01433
01434 for(i=0; i<width; i++){
01435 #ifdef liftS
01436 b1[i] += (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
01437 #else
01438 b1[i] += (W_BM*(b0[i] + b2[i])+4*b1[i]+W_BO)>>W_BS;
01439 #endif
01440 }
01441 }
01442
01443 static void vertical_compose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
01444 int i;
01445
01446 for(i=0; i<width; i++){
01447 b1[i] -= (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
01448 }
01449 }
01450
01451 static void vertical_compose97i(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, DWTELEM *b3, DWTELEM *b4, DWTELEM *b5, int width){
01452 int i;
01453
01454 for(i=0; i<width; i++){
01455 int r;
01456 b4[i] -= (W_DM*(b3[i] + b5[i])+W_DO)>>W_DS;
01457 #ifdef lift5
01458 b3[i] -= (W_CM*(b2[i] + b4[i])+W_CO)>>W_CS;
01459 #else
01460 r= 3*(b2[i] + b4[i]);
01461 r+= r>>4;
01462 r+= r>>8;
01463 b3[i] -= (r+W_CO)>>W_CS;
01464 #endif
01465 #ifdef liftS
01466 b2[i] += (W_BM*(b1[i] + b3[i])+W_BO)>>W_BS;
01467 #else
01468 b2[i] += (W_BM*(b1[i] + b3[i])+4*b2[i]+W_BO)>>W_BS;
01469 #endif
01470 b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
01471 }
01472 }
01473
01474 static void spatial_compose97i_buffered_init(dwt_compose_t *cs, slice_buffer * sb, int height, int stride_line){
01475 cs->b0 = slice_buffer_get_line(sb, mirror(-3-1, height-1) * stride_line);
01476 cs->b1 = slice_buffer_get_line(sb, mirror(-3 , height-1) * stride_line);
01477 cs->b2 = slice_buffer_get_line(sb, mirror(-3+1, height-1) * stride_line);
01478 cs->b3 = slice_buffer_get_line(sb, mirror(-3+2, height-1) * stride_line);
01479 cs->y = -3;
01480 }
01481
01482 static void spatial_compose97i_init(dwt_compose_t *cs, DWTELEM *buffer, int height, int stride){
01483 cs->b0 = buffer + mirror(-3-1, height-1)*stride;
01484 cs->b1 = buffer + mirror(-3 , height-1)*stride;
01485 cs->b2 = buffer + mirror(-3+1, height-1)*stride;
01486 cs->b3 = buffer + mirror(-3+2, height-1)*stride;
01487 cs->y = -3;
01488 }
01489
01490 static void spatial_compose97i_dy_buffered(dwt_compose_t *cs, slice_buffer * sb, int width, int height, int stride_line){
01491 int y = cs->y;
01492
01493 int mirror0 = mirror(y - 1, height - 1);
01494 int mirror1 = mirror(y + 0, height - 1);
01495 int mirror2 = mirror(y + 1, height - 1);
01496 int mirror3 = mirror(y + 2, height - 1);
01497 int mirror4 = mirror(y + 3, height - 1);
01498 int mirror5 = mirror(y + 4, height - 1);
01499 DWTELEM *b0= cs->b0;
01500 DWTELEM *b1= cs->b1;
01501 DWTELEM *b2= cs->b2;
01502 DWTELEM *b3= cs->b3;
01503 DWTELEM *b4= slice_buffer_get_line(sb, mirror4 * stride_line);
01504 DWTELEM *b5= slice_buffer_get_line(sb, mirror5 * stride_line);
01505
01506 {START_TIMER
01507 if(y>0 && y+4<height){
01508 vertical_compose97i(b0, b1, b2, b3, b4, b5, width);
01509 }else{
01510 if(mirror3 <= mirror5) vertical_compose97iL1(b3, b4, b5, width);
01511 if(mirror2 <= mirror4) vertical_compose97iH1(b2, b3, b4, width);
01512 if(mirror1 <= mirror3) vertical_compose97iL0(b1, b2, b3, width);
01513 if(mirror0 <= mirror2) vertical_compose97iH0(b0, b1, b2, width);
01514 }
01515 if(width>400){
01516 STOP_TIMER("vertical_compose97i")}}
01517
01518 {START_TIMER
01519 if(y-1>= 0) horizontal_compose97i(b0, width);
01520 if(mirror0 <= mirror2) horizontal_compose97i(b1, width);
01521 if(width>400 && mirror0 <= mirror2){
01522 STOP_TIMER("horizontal_compose97i")}}
01523
01524 cs->b0=b2;
01525 cs->b1=b3;
01526 cs->b2=b4;
01527 cs->b3=b5;
01528 cs->y += 2;
01529 }
01530
01531 static void spatial_compose97i_dy(dwt_compose_t *cs, DWTELEM *buffer, int width, int height, int stride){
01532 int y = cs->y;
01533 DWTELEM *b0= cs->b0;
01534 DWTELEM *b1= cs->b1;
01535 DWTELEM *b2= cs->b2;
01536 DWTELEM *b3= cs->b3;
01537 DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
01538 DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
01539
01540 if(stride == width && y+4 < height && 0){
01541 int x;
01542 for(x=0; x<width/2; x++)
01543 b5[x] += 64*2;
01544 for(; x<width; x++)
01545 b5[x] += 169*2;
01546 }
01547
01548 {START_TIMER
01549 if(b3 <= b5) vertical_compose97iL1(b3, b4, b5, width);
01550 if(b2 <= b4) vertical_compose97iH1(b2, b3, b4, width);
01551 if(b1 <= b3) vertical_compose97iL0(b1, b2, b3, width);
01552 if(b0 <= b2) vertical_compose97iH0(b0, b1, b2, width);
01553 if(width>400){
01554 STOP_TIMER("vertical_compose97i")}}
01555
01556 {START_TIMER
01557 if(y-1>= 0) horizontal_compose97i(b0, width);
01558 if(b0 <= b2) horizontal_compose97i(b1, width);
01559 if(width>400 && b0 <= b2){
01560 STOP_TIMER("horizontal_compose97i")}}
01561
01562 cs->b0=b2;
01563 cs->b1=b3;
01564 cs->b2=b4;
01565 cs->b3=b5;
01566 cs->y += 2;
01567 }
01568
01569 static void spatial_compose97i(DWTELEM *buffer, int width, int height, int stride){
01570 dwt_compose_t cs;
01571 spatial_compose97i_init(&cs, buffer, height, stride);
01572 while(cs.y <= height)
01573 spatial_compose97i_dy(&cs, buffer, width, height, stride);
01574 }
01575
01576 void ff_spatial_idwt_buffered_init(dwt_compose_t *cs, slice_buffer * sb, int width, int height, int stride_line, int type, int decomposition_count){
01577 int level;
01578 for(level=decomposition_count-1; level>=0; level--){
01579 switch(type){
01580 case 0: spatial_compose97i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
01581 case 1: spatial_compose53i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
01582
01583 case 2:
01584 av_log(NULL, AV_LOG_ERROR, "spatial_composeX neither buffered nor slicified yet.\n"); break;
01585 }
01586 }
01587 }
01588
01589 void ff_spatial_idwt_init(dwt_compose_t *cs, DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
01590 int level;
01591 for(level=decomposition_count-1; level>=0; level--){
01592 switch(type){
01593 case 0: spatial_compose97i_init(cs+level, buffer, height>>level, stride<<level); break;
01594 case 1: spatial_compose53i_init(cs+level, buffer, height>>level, stride<<level); break;
01595
01596 case 2: spatial_composeX(buffer, width>>level, height>>level, stride<<level); break;
01597 }
01598 }
01599 }
01600
01601 void ff_spatial_idwt_slice(dwt_compose_t *cs, DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count, int y){
01602 const int support = type==1 ? 3 : 5;
01603 int level;
01604 if(type==2) return;
01605
01606 for(level=decomposition_count-1; level>=0; level--){
01607 while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
01608 switch(type){
01609 case 0: spatial_compose97i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
01610 break;
01611 case 1: spatial_compose53i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
01612 break;
01613 case 2: break;
01614 }
01615 }
01616 }
01617 }
01618
01619 void ff_spatial_idwt_buffered_slice(dwt_compose_t *cs, slice_buffer * slice_buf, int width, int height, int stride_line, int type, int decomposition_count, int y){
01620 const int support = type==1 ? 3 : 5;
01621 int level;
01622 if(type==2) return;
01623
01624 for(level=decomposition_count-1; level>=0; level--){
01625 while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
01626 switch(type){
01627 case 0: spatial_compose97i_dy_buffered(cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
01628 break;
01629 case 1: spatial_compose53i_dy_buffered(cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
01630 break;
01631 case 2: break;
01632 }
01633 }
01634 }
01635 }
01636
01637 void ff_spatial_idwt(DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
01638 if(type==2){
01639 int level;
01640 for(level=decomposition_count-1; level>=0; level--)
01641 spatial_composeX (buffer, width>>level, height>>level, stride<<level);
01642 }else{
01643 dwt_compose_t cs[MAX_DECOMPOSITIONS];
01644 int y;
01645 ff_spatial_idwt_init(cs, buffer, width, height, stride, type, decomposition_count);
01646 for(y=0; y<height; y+=4)
01647 ff_spatial_idwt_slice(cs, buffer, width, height, stride, type, decomposition_count, y);
01648 }
01649 }
01650
01651 static int encode_subband_c0run(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
01652 const int w= b->width;
01653 const int h= b->height;
01654 int x, y;
01655
01656 if(1){
01657 int run=0;
01658 int runs[w*h];
01659 int run_index=0;
01660 int max_index;
01661
01662 for(y=0; y<h; y++){
01663 for(x=0; x<w; x++){
01664 int v, p=0;
01665 int l=0, lt=0, t=0, rt=0;
01666 v= src[x + y*stride];
01667
01668 if(y){
01669 t= src[x + (y-1)*stride];
01670 if(x){
01671 lt= src[x - 1 + (y-1)*stride];
01672 }
01673 if(x + 1 < w){
01674 rt= src[x + 1 + (y-1)*stride];
01675 }
01676 }
01677 if(x){
01678 l= src[x - 1 + y*stride];
01679
01680
01681
01682
01683 }
01684 if(parent){
01685 int px= x>>1;
01686 int py= y>>1;
01687 if(px<b->parent->width && py<b->parent->height)
01688 p= parent[px + py*2*stride];
01689 }
01690 if(!(l|lt|t|rt|p)){
01691 if(v){
01692 runs[run_index++]= run;
01693 run=0;
01694 }else{
01695 run++;
01696 }
01697 }
01698 }
01699 }
01700 max_index= run_index;
01701 runs[run_index++]= run;
01702 run_index=0;
01703 run= runs[run_index++];
01704
01705 put_symbol2(&s->c, b->state[30], max_index, 0);
01706 if(run_index <= max_index)
01707 put_symbol2(&s->c, b->state[1], run, 3);
01708
01709 for(y=0; y<h; y++){
01710 if(s->c.bytestream_end - s->c.bytestream < w*40){
01711 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
01712 return -1;
01713 }
01714 for(x=0; x<w; x++){
01715 int v, p=0;
01716 int l=0, lt=0, t=0, rt=0;
01717 v= src[x + y*stride];
01718
01719 if(y){
01720 t= src[x + (y-1)*stride];
01721 if(x){
01722 lt= src[x - 1 + (y-1)*stride];
01723 }
01724 if(x + 1 < w){
01725 rt= src[x + 1 + (y-1)*stride];
01726 }
01727 }
01728 if(x){
01729 l= src[x - 1 + y*stride];
01730
01731
01732
01733
01734 }
01735 if(parent){
01736 int px= x>>1;
01737 int py= y>>1;
01738 if(px<b->parent->width && py<b->parent->height)
01739 p= parent[px + py*2*stride];
01740 }
01741 if(l|lt|t|rt|p){
01742 int context= av_log2(3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
01743
01744 put_rac(&s->c, &b->state[0][context], !!v);
01745 }else{
01746 if(!run){
01747 run= runs[run_index++];
01748
01749 if(run_index <= max_index)
01750 put_symbol2(&s->c, b->state[1], run, 3);
01751 assert(v);
01752 }else{
01753 run--;
01754 assert(!v);
01755 }
01756 }
01757 if(v){
01758 int context= av_log2(3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
01759 int l2= 2*ABS(l) + (l<0);
01760 int t2= 2*ABS(t) + (t<0);
01761
01762 put_symbol2(&s->c, b->state[context + 2], ABS(v)-1, context-4);
01763 put_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3bA[l2&0xFF] + 3*quant3bA[t2&0xFF]], v<0);
01764 }
01765 }
01766 }
01767 }
01768 return 0;
01769 }
01770
01771 static int encode_subband(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
01772
01773
01774 return encode_subband_c0run(s, b, src, parent, stride, orientation);
01775
01776 }
01777
01778 static inline void unpack_coeffs(SnowContext *s, SubBand *b, SubBand * parent, int orientation){
01779 const int w= b->width;
01780 const int h= b->height;
01781 int x,y;
01782
01783 if(1){
01784 int run, runs;
01785 x_and_coeff *xc= b->x_coeff;
01786 x_and_coeff *prev_xc= NULL;
01787 x_and_coeff *prev2_xc= xc;
01788 x_and_coeff *parent_xc= parent ? parent->x_coeff : NULL;
01789 x_and_coeff *prev_parent_xc= parent_xc;
01790
01791 runs= get_symbol2(&s->c, b->state[30], 0);
01792 if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
01793 else run= INT_MAX;
01794
01795 for(y=0; y<h; y++){
01796 int v=0;
01797 int lt=0, t=0, rt=0;
01798
01799 if(y && prev_xc->x == 0){
01800 rt= prev_xc->coeff;
01801 }
01802 for(x=0; x<w; x++){
01803 int p=0;
01804 const int l= v;
01805
01806 lt= t; t= rt;
01807
01808 if(y){
01809 if(prev_xc->x <= x)
01810 prev_xc++;
01811 if(prev_xc->x == x + 1)
01812 rt= prev_xc->coeff;
01813 else
01814 rt=0;
01815 }
01816 if(parent_xc){
01817 if(x>>1 > parent_xc->x){
01818 parent_xc++;
01819 }
01820 if(x>>1 == parent_xc->x){
01821 p= parent_xc->coeff;
01822 }
01823 }
01824 if(l|lt|t|rt|p){
01825 int context= av_log2(3*(l>>1) + (lt>>1) + (t&~1) + (rt>>1) + (p>>1));
01826
01827 v=get_rac(&s->c, &b->state[0][context]);
01828 if(v){
01829 v= 2*(get_symbol2(&s->c, b->state[context + 2], context-4) + 1);
01830 v+=get_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3bA[l&0xFF] + 3*quant3bA[t&0xFF]]);
01831
01832 xc->x=x;
01833 (xc++)->coeff= v;
01834 }
01835 }else{
01836 if(!run){
01837 if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
01838 else run= INT_MAX;
01839 v= 2*(get_symbol2(&s->c, b->state[0 + 2], 0-4) + 1);
01840 v+=get_rac(&s->c, &b->state[0][16 + 1 + 3]);
01841
01842 xc->x=x;
01843 (xc++)->coeff= v;
01844 }else{
01845 int max_run;
01846 run--;
01847 v=0;
01848
01849 if(y) max_run= FFMIN(run, prev_xc->x - x - 2);
01850 else max_run= FFMIN(run, w-x-1);
01851 if(parent_xc)
01852 max_run= FFMIN(max_run, 2*parent_xc->x - x - 1);
01853 x+= max_run;
01854 run-= max_run;
01855 }
01856 }
01857 }
01858 (xc++)->x= w+1;
01859 prev_xc= prev2_xc;
01860 prev2_xc= xc;
01861
01862 if(parent_xc){
01863 if(y&1){
01864 while(parent_xc->x != parent->width+1)
01865 parent_xc++;
01866 parent_xc++;
01867 prev_parent_xc= parent_xc;
01868 }else{
01869 parent_xc= prev_parent_xc;
01870 }
01871 }
01872 }
01873
01874 (xc++)->x= w+1;
01875 }
01876 }
01877
01878 static inline void decode_subband_slice_buffered(SnowContext *s, SubBand *b, slice_buffer * sb, int start_y, int h, int save_state[1]){
01879 const int w= b->width;
01880 int x,y;
01881 const int qlog= clip(s->qlog + b->qlog, 0, QROOT*16);
01882 int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
01883 int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
01884 int new_index = 0;
01885
01886 START_TIMER
01887
01888 if(b->buf == s->spatial_dwt_buffer || s->qlog == LOSSLESS_QLOG){
01889 qadd= 0;
01890 qmul= 1<<QEXPSHIFT;
01891 }
01892
01893
01894 if (start_y != 0)
01895 new_index = save_state[0];
01896
01897
01898 for(y=start_y; y<h; y++){
01899 int x = 0;
01900 int v;
01901 DWTELEM * line = slice_buffer_get_line(sb, y * b->stride_line + b->buf_y_offset) + b->buf_x_offset;
01902 memset(line, 0, b->width*sizeof(DWTELEM));
01903 v = b->x_coeff[new_index].coeff;
01904 x = b->x_coeff[new_index++].x;
01905 while(x < w)
01906 {
01907 register int t= ( (v>>1)*qmul + qadd)>>QEXPSHIFT;
01908 register int u= -(v&1);
01909 line[x] = (t^u) - u;
01910
01911 v = b->x_coeff[new_index].coeff;
01912 x = b->x_coeff[new_index++].x;
01913 }
01914 }
01915 if(w > 200 && start_y != 0){
01916 STOP_TIMER("decode_subband")
01917 }
01918
01919
01920 save_state[0] = new_index;
01921
01922 return;
01923 }
01924
01925 static void reset_contexts(SnowContext *s){
01926 int plane_index, level, orientation;
01927
01928 for(plane_index=0; plane_index<3; plane_index++){
01929 for(level=0; level<s->spatial_decomposition_count; level++){
01930 for(orientation=level ? 1:0; orientation<4; orientation++){
01931 memset(s->plane[plane_index].band[level][orientation].state, MID_STATE, sizeof(s->plane[plane_index].band[level][orientation].state));
01932 }
01933 }
01934 }
01935 memset(s->header_state, MID_STATE, sizeof(s->header_state));
01936 memset(s->block_state, MID_STATE, sizeof(s->block_state));
01937 }
01938
01939 static int alloc_blocks(SnowContext *s){
01940 int w= -((-s->avctx->width )>>LOG2_MB_SIZE);
01941 int h= -((-s->avctx->height)>>LOG2_MB_SIZE);
01942
01943 s->b_width = w;
01944 s->b_height= h;
01945
01946 s->block= av_mallocz(w * h * sizeof(BlockNode) << (s->block_max_depth*2));
01947 return 0;
01948 }
01949
01950 static inline void copy_rac_state(RangeCoder *d, RangeCoder *s){
01951 uint8_t *bytestream= d->bytestream;
01952 uint8_t *bytestream_start= d->bytestream_start;
01953 *d= *s;
01954 d->bytestream= bytestream;
01955 d->bytestream_start= bytestream_start;
01956 }
01957
01958
01959 static int pix_sum(uint8_t * pix, int line_size, int w)
01960 {
01961 int s, i, j;
01962
01963 s = 0;
01964 for (i = 0; i < w; i++) {
01965 for (j = 0; j < w; j++) {
01966 s += pix[0];
01967 pix ++;
01968 }
01969 pix += line_size - w;
01970 }
01971 return s;
01972 }
01973
01974
01975 static int pix_norm1(uint8_t * pix, int line_size, int w)
01976 {
01977 int s, i, j;
01978 uint32_t *sq = squareTbl + 256;
01979
01980 s = 0;
01981 for (i = 0; i < w; i++) {
01982 for (j = 0; j < w; j ++) {
01983 s += sq[pix[0]];
01984 pix ++;
01985 }
01986 pix += line_size - w;
01987 }
01988 return s;
01989 }
01990
01991 static inline void set_blocks(SnowContext *s, int level, int x, int y, int l, int cb, int cr, int mx, int my, int type){
01992 const int w= s->b_width << s->block_max_depth;
01993 const int rem_depth= s->block_max_depth - level;
01994 const int index= (x + y*w) << rem_depth;
01995 const int block_w= 1<<rem_depth;
01996 BlockNode block;
01997 int i,j;
01998
01999 block.color[0]= l;
02000 block.color[1]= cb;
02001 block.color[2]= cr;
02002 block.mx= mx;
02003 block.my= my;
02004 block.type= type;
02005 block.level= level;
02006
02007 for(j=0; j<block_w; j++){
02008 for(i=0; i<block_w; i++){
02009 s->block[index + i + j*w]= block;
02010 }
02011 }
02012 }
02013
02014 static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){
02015 const int offset[3]= {
02016 y*c-> stride + x,
02017 ((y*c->uvstride + x)>>1),
02018 ((y*c->uvstride + x)>>1),
02019 };
02020 int i;
02021 for(i=0; i<3; i++){
02022 c->src[0][i]= src [i];
02023 c->ref[0][i]= ref [i] + offset[i];
02024 }
02025 assert(!ref_index);
02026 }
02027
02028
02029 #define P_LEFT P[1]
02030 #define P_TOP P[2]
02031 #define P_TOPRIGHT P[3]
02032 #define P_MEDIAN P[4]
02033 #define P_MV1 P[9]
02034 #define FLAG_QPEL 1 //must be 1
02035
02036 static int encode_q_branch(SnowContext *s, int level, int x, int y){
02037 uint8_t p_buffer[1024];
02038 uint8_t i_buffer[1024];
02039 uint8_t p_state[sizeof(s->block_state)];
02040 uint8_t i_state[sizeof(s->block_state)];
02041 RangeCoder pc, ic;
02042 uint8_t *pbbak= s->c.bytestream;
02043 uint8_t *pbbak_start= s->c.bytestream_start;
02044 int score, score2, iscore, i_len, p_len, block_s, sum;
02045 const int w= s->b_width << s->block_max_depth;
02046 const int h= s->b_height << s->block_max_depth;
02047 const int rem_depth= s->block_max_depth - level;
02048 const int index= (x + y*w) << rem_depth;
02049 const int block_w= 1<<(LOG2_MB_SIZE - level);
02050 static BlockNode null_block= {
02051 .color= {128,128,128},
02052 .mx= 0,
02053 .my= 0,
02054 .type= 0,
02055 .level= 0,
02056 };
02057 int trx= (x+1)<<rem_depth;
02058 int try= (y+1)<<rem_depth;
02059 BlockNode *left = x ? &s->block[index-1] : &null_block;
02060 BlockNode *top = y ? &s->block[index-w] : &null_block;
02061 BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
02062 BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
02063 BlockNode *tl = y && x ? &s->block[index-w-1] : left;
02064 BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl;
02065 int pl = left->color[0];
02066 int pcb= left->color[1];
02067 int pcr= left->color[2];
02068 int pmx= mid_pred(left->mx, top->mx, tr->mx);
02069 int pmy= mid_pred(left->my, top->my, tr->my);
02070 int mx=0, my=0;
02071 int l,cr,cb, i;
02072 const int stride= s->current_picture.linesize[0];
02073 const int uvstride= s->current_picture.linesize[1];
02074 const int instride= s->input_picture.linesize[0];
02075 const int uvinstride= s->input_picture.linesize[1];
02076 uint8_t *new_l = s->input_picture.data[0] + (x + y* instride)*block_w;
02077 uint8_t *new_cb= s->input_picture.data[1] + (x + y*uvinstride)*block_w/2;
02078 uint8_t *new_cr= s->input_picture.data[2] + (x + y*uvinstride)*block_w/2;
02079 uint8_t current_mb[3][stride*block_w];
02080 uint8_t *current_data[3]= {¤t_mb[0][0], ¤t_mb[1][0], ¤t_mb[2][0]};
02081 int P[10][2];
02082 int16_t last_mv[3][2];
02083 int qpel= !!(s->avctx->flags & CODEC_FLAG_QPEL);
02084 const int shift= 1+qpel;
02085 MotionEstContext *c= &s->m.me;
02086 int mx_context= av_log2(2*ABS(left->mx - top->mx));
02087 int my_context= av_log2(2*ABS(left->my - top->my));
02088 int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
02089
02090 assert(sizeof(s->block_state) >= 256);
02091 if(s->keyframe){
02092 set_blocks(s, level, x, y, pl, pcb, pcr, pmx, pmy, BLOCK_INTRA);
02093 return 0;
02094 }
02095
02096
02097 for(i=0; i<block_w; i++)
02098 memcpy(¤t_mb[0][0] + stride*i, new_l + instride*i, block_w);
02099 for(i=0; i<block_w>>1; i++)
02100 memcpy(¤t_mb[1][0] + uvstride*i, new_cb + uvinstride*i, block_w>>1);
02101 for(i=0; i<block_w>>1; i++)
02102 memcpy(¤t_mb[2][0] + uvstride*i, new_cr + uvinstride*i, block_w>>1);
02103
02104
02105
02106 P_LEFT[0]= left->mx;
02107 P_LEFT[1]= left->my;
02108 P_TOP [0]= top->mx;
02109 P_TOP [1]= top->my;
02110 P_TOPRIGHT[0]= tr->mx;
02111 P_TOPRIGHT[1]= tr->my;
02112
02113 last_mv[0][0]= s->block[index].mx;
02114 last_mv[0][1]= s->block[index].my;
02115 last_mv[1][0]= right->mx;
02116 last_mv[1][1]= right->my;
02117 last_mv[2][0]= bottom->mx;
02118 last_mv[2][1]= bottom->my;
02119
02120 s->m.mb_stride=2;
02121 s->m.mb_x=
02122 s->m.mb_y= 0;
02123 s->m.me.skip= 0;
02124
02125 init_ref(c, current_data, s->last_picture.data, NULL, block_w*x, block_w*y, 0);
02126
02127 assert(s->m.me. stride == stride);
02128 assert(s->m.me.uvstride == uvstride);
02129
02130 c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
02131 c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
02132 c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
02133 c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_MV;
02134
02135 c->xmin = - x*block_w - 16+2;
02136 c->ymin = - y*block_w - 16+2;
02137 c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-2;
02138 c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-2;
02139
02140 if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
02141 if(P_LEFT[1] > (c->ymax<<shift)) P_LEFT[1] = (c->ymax<<shift);
02142 if(P_TOP[0] > (c->xmax<<shift)) P_TOP[0] = (c->xmax<<shift);
02143 if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
02144 if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
02145 if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
02146 if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
02147
02148 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
02149 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
02150
02151 if (!y) {
02152 c->pred_x= P_LEFT[0];
02153 c->pred_y= P_LEFT[1];
02154 } else {
02155 c->pred_x = P_MEDIAN[0];
02156 c->pred_y = P_MEDIAN[1];
02157 }
02158
02159 score= ff_epzs_motion_search(&s->m, &mx, &my, P, 0, 0, last_mv,
02160 (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
02161
02162 assert(mx >= c->xmin);
02163 assert(mx <= c->xmax);
02164 assert(my >= c->ymin);
02165 assert(my <= c->ymax);
02166
02167 score= s->m.me.sub_motion_search(&s->m, &mx, &my, score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
02168 score= ff_get_mb_score(&s->m, mx, my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
02169
02170
02171
02172 pc= s->c;
02173 pc.bytestream_start=
02174 pc.bytestream= p_buffer;
02175 memcpy(p_state, s->block_state, sizeof(s->block_state));
02176
02177 if(level!=s->block_max_depth)
02178 put_rac(&pc, &p_state[4 + s_context], 1);
02179 put_rac(&pc, &p_state[1 + left->type + top->type], 0);
02180 put_symbol(&pc, &p_state[128 + 32*mx_context], mx - pmx, 1);
02181 put_symbol(&pc, &p_state[128 + 32*my_context], my - pmy, 1);
02182 p_len= pc.bytestream - pc.bytestream_start;
02183 score += (s->lambda2*(p_len*8
02184 + (pc.outstanding_count - s->c.outstanding_count)*8
02185 + (-av_log2(pc.range) + av_log2(s->c.range))
02186 ))>>FF_LAMBDA_SHIFT;
02187
02188 block_s= block_w*block_w;
02189 sum = pix_sum(¤t_mb[0][0], stride, block_w);
02190 l= (sum + block_s/2)/block_s;
02191 iscore = pix_norm1(¤t_mb[0][0], stride, block_w) - 2*l*sum + l*l*block_s;
02192
02193 block_s= block_w*block_w>>2;
02194 sum = pix_sum(¤t_mb[1][0], uvstride, block_w>>1);
02195 cb= (sum + block_s/2)/block_s;
02196
02197 sum = pix_sum(¤t_mb[2][0], uvstride, block_w>>1);
02198 cr= (sum + block_s/2)/block_s;
02199
02200
02201 ic= s->c;
02202 ic.bytestream_start=
02203 ic.bytestream= i_buffer;
02204 memcpy(i_state, s->block_state, sizeof(s->block_state));
02205 if(level!=s->block_max_depth)
02206 put_rac(&ic, &i_state[4 + s_context], 1);
02207 put_rac(&ic, &i_state[1 + left->type + top->type], 1);
02208 put_symbol(&ic, &i_state[32], l-pl , 1);
02209 put_symbol(&ic, &i_state[64], cb-pcb, 1);
02210 put_symbol(&ic, &i_state[96], cr-pcr, 1);
02211 i_len= ic.bytestream - ic.bytestream_start;
02212 iscore += (s->lambda2*(i_len*8
02213 + (ic.outstanding_count - s->c.outstanding_count)*8
02214 + (-av_log2(ic.range) + av_log2(s->c.range))
02215 ))>>FF_LAMBDA_SHIFT;
02216
02217
02218 assert(iscore < 255*255*256 + s->lambda2*10);
02219 assert(iscore >= 0);
02220 assert(l>=0 && l<=255);
02221 assert(pl>=0 && pl<=255);
02222
02223 if(level==0){
02224 int varc= iscore >> 8;
02225 int vard= score >> 8;
02226 if (vard <= 64 || vard < varc)
02227 c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
02228 else
02229 c->scene_change_score+= s->m.qscale;
02230 }
02231
02232 if(level!=s->block_max_depth){
02233 put_rac(&s->c, &s->block_state[4 + s_context], 0);
02234 score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0);
02235 score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0);
02236 score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1);
02237 score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1);
02238 score2+= s->lambda2>>FF_LAMBDA_SHIFT;
02239
02240 if(score2 < score && score2 < iscore)
02241 return score2;
02242 }
02243
02244 if(iscore < score){
02245 memcpy(pbbak, i_buffer, i_len);
02246 s->c= ic;
02247 s->c.bytestream_start= pbbak_start;
02248 s->c.bytestream= pbbak + i_len;
02249 set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, BLOCK_INTRA);
02250 memcpy(s->block_state, i_state, sizeof(s->block_state));
02251 return iscore;
02252 }else{
02253 memcpy(pbbak, p_buffer, p_len);
02254 s->c= pc;
02255 s->c.bytestream_start= pbbak_start;
02256 s->c.bytestream= pbbak + p_len;
02257 set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, 0);
02258 memcpy(s->block_state, p_state, sizeof(s->block_state));
02259 return score;
02260 }
02261 }
02262
02263 static void decode_q_branch(SnowContext *s, int level, int x, int y){
02264 const int w= s->b_width << s->block_max_depth;
02265 const int rem_depth= s->block_max_depth - level;
02266 const int index= (x + y*w) << rem_depth;
02267 static BlockNode null_block= {
02268 .color= {128,128,128},
02269 .mx= 0,
02270 .my= 0,
02271 .type= 0,
02272 .level= 0,
02273 };
02274 int trx= (x+1)<<rem_depth;
02275 BlockNode *left = x ? &s->block[index-1] : &null_block;
02276 BlockNode *top = y ? &s->block[index-w] : &null_block;
02277 BlockNode *tl = y && x ? &s->block[index-w-1] : left;
02278 BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl;
02279 int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
02280
02281 if(s->keyframe){
02282 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);
02283 return;
02284 }
02285
02286 if(level==s->block_max_depth || get_rac(&s->c, &s->block_state[4 + s_context])){
02287 int type;
02288 int l = left->color[0];
02289 int cb= left->color[1];
02290 int cr= left->color[2];
02291 int mx= mid_pred(left->mx, top->mx, tr->mx);
02292 int my= mid_pred(left->my, top->my, tr->my);
02293 int mx_context= av_log2(2*ABS(left->mx - top->mx)) + 0*av_log2(2*ABS(tr->mx - top->mx));
02294 int my_context= av_log2(2*ABS(left->my - top->my)) + 0*av_log2(2*ABS(tr->my - top->my));
02295
02296 type= get_rac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0;
02297
02298 if(type){
02299 l += get_symbol(&s->c, &s->block_state[32], 1);
02300 cb+= get_symbol(&s->c, &s->block_state[64], 1);
02301 cr+= get_symbol(&s->c, &s->block_state[96], 1);
02302 }else{
02303 mx+= get_symbol(&s->c, &s->block_state[128 + 32*mx_context], 1);
02304 my+= get_symbol(&s->c, &s->block_state[128 + 32*my_context], 1);
02305 }
02306 set_blocks(s, level, x, y, l, cb, cr, mx, my, type);
02307 }else{
02308 decode_q_branch(s, level+1, 2*x+0, 2*y+0);
02309 decode_q_branch(s, level+1, 2*x+1, 2*y+0);
02310 decode_q_branch(s, level+1, 2*x+0, 2*y+1);
02311 decode_q_branch(s, level+1, 2*x+1, 2*y+1);
02312 }
02313 }
02314
02315 static void encode_blocks(SnowContext *s){
02316 int x, y;
02317 int w= s->b_width;
02318 int h= s->b_height;
02319
02320 for(y=0; y<h; y++){
02321 if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){
02322 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
02323 return;
02324 }
02325 for(x=0; x<w; x++){
02326 encode_q_branch(s, 0, x, y);
02327 }
02328 }
02329 }
02330
02331 static void decode_blocks(SnowContext *s){
02332 int x, y;
02333 int w= s->b_width;
02334 int h= s->b_height;
02335
02336 for(y=0; y<h; y++){
02337 for(x=0; x<w; x++){
02338 decode_q_branch(s, 0, x, y);
02339 }
02340 }
02341 }
02342
02343 static void mc_block(uint8_t *dst, uint8_t *src, uint8_t *tmp, int stride, int b_w, int b_h, int dx, int dy){
02344 int x, y;
02345 START_TIMER
02346 for(y=0; y < b_h+5; y++){
02347 for(x=0; x < b_w; x++){
02348 int a0= src[x ];
02349 int a1= src[x + 1];
02350 int a2= src[x + 2];
02351 int a3= src[x + 3];
02352 int a4= src[x + 4];
02353 int a5= src[x + 5];
02354
02355 int am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
02356
02357
02358
02359
02360
02361
02362 if(dx<8) am = (32*a2*( 8-dx) + am* dx + 128)>>8;
02363 else am = ( am*(16-dx) + 32*a3*(dx-8) + 128)>>8;
02364
02365
02366 if(am&(~255)) am= ~(am>>31);
02367
02368 tmp[x] = am;
02369
02370
02371
02372
02373
02374 }
02375 tmp += stride;
02376 src += stride;
02377 }
02378 tmp -= (b_h+5)*stride;
02379
02380 for(y=0; y < b_h; y++){
02381 for(x=0; x < b_w; x++){
02382 int a0= tmp[x + 0*stride];
02383 int a1= tmp[x + 1*stride];
02384 int a2= tmp[x + 2*stride];
02385 int a3= tmp[x + 3*stride];
02386 int a4= tmp[x + 4*stride];
02387 int a5= tmp[x + 5*stride];
02388 int am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
02389
02390
02391
02392
02393
02394
02395 if(dy<8) am = (32*a2*( 8-dy) + am* dy + 128)>>8;
02396 else am = ( am*(16-dy) + 32*a3*(dy-8) + 128)>>8;
02397
02398 if(am&(~255)) am= ~(am>>31);
02399
02400 dst[x] = am;
02401
02402
02403
02404
02405 }
02406 dst += stride;
02407 tmp += stride;
02408 }
02409 STOP_TIMER("mc_block")
02410 }
02411
02412 #define mca(dx,dy,b_w)\
02413 static void mc_block_hpel ## dx ## dy ## b_w(uint8_t *dst, uint8_t *src, int stride, int h){\
02414 uint8_t tmp[stride*(b_w+5)];\
02415 assert(h==b_w);\
02416 mc_block(dst, src-2-2*stride, tmp, stride, b_w, b_w, dx, dy);\
02417 }
02418
02419 mca( 0, 0,16)
02420 mca( 8, 0,16)
02421 mca( 0, 8,16)
02422 mca( 8, 8,16)
02423 mca( 0, 0,8)
02424 mca( 8, 0,8)
02425 mca( 0, 8,8)
02426 mca( 8, 8,8)
02427
02428 static void pred_block(SnowContext *s, uint8_t *dst, uint8_t *src, uint8_t *tmp, int stride, int sx, int sy, int b_w, int b_h, BlockNode *block, int plane_index, int w, int h){
02429 if(block->type){
02430 int x, y;
02431 const int color= block->color[plane_index];
02432 for(y=0; y < b_h; y++){
02433 for(x=0; x < b_w; x++){
02434 dst[x + y*stride]= color;
02435 }
02436 }
02437 }else{
02438 const int scale= plane_index ? s->mv_scale : 2*s->mv_scale;
02439 int mx= block->mx*scale;
02440 int my= block->my*scale;
02441 const int dx= mx&15;
02442 const int dy= my&15;
02443 sx += (mx>>4) - 2;
02444 sy += (my>>4) - 2;
02445 src += sx + sy*stride;
02446 if( (unsigned)sx >= w - b_w - 4
02447 || (unsigned)sy >= h - b_h - 4){
02448 ff_emulated_edge_mc(tmp + MB_SIZE, src, stride, b_w+5, b_h+5, sx, sy, w, h);
02449 src= tmp + MB_SIZE;
02450 }
02451 if((dx&3) || (dy&3) || b_w!=b_h || (b_w!=4 && b_w!=8 && b_w!=16))
02452 mc_block(dst, src, tmp, stride, b_w, b_h, dx, dy);
02453 else
02454 s->dsp.put_h264_qpel_pixels_tab[2-(b_w>>3)][dy+(dx>>2)](dst,src + 2 + 2*stride,stride);
02455 }
02456 }
02457
02458 static always_inline int same_block(BlockNode *a, BlockNode *b){
02459 return !((a->mx - b->mx) | (a->my - b->my) | a->type | b->type);
02460 }
02461
02462
02463 static always_inline void add_yblock_buffered(SnowContext *s, slice_buffer * sb, DWTELEM *old_dst, uint8_t *dst8, uint8_t *src, uint8_t *obmc, int src_x, int src_y, int b_w, int b_h, int w, int h, int dst_stride, int src_stride, int obmc_stride, int b_x, int b_y, int add, int plane_index){
02464 DWTELEM * dst = NULL;
02465 const int b_width = s->b_width << s->block_max_depth;
02466 const int b_height= s->b_height << s->block_max_depth;
02467 const int b_stride= b_width;
02468 BlockNode *lt= &s->block[b_x + b_y*b_stride];
02469 BlockNode *rt= lt+1;
02470 BlockNode *lb= lt+b_stride;
02471 BlockNode *rb= lb+1;
02472 uint8_t *block[4];
02473 uint8_t tmp[src_stride*(b_h+5)];
02474 int x,y;
02475
02476 if(b_x<0){
02477 lt= rt;
02478 lb= rb;
02479 }else if(b_x + 1 >= b_width){
02480 rt= lt;
02481 rb= lb;
02482 }
02483 if(b_y<0){
02484 lt= lb;
02485 rt= rb;
02486 }else if(b_y + 1 >= b_height){
02487 lb= lt;
02488 rb= rt;
02489 }
02490
02491 if(src_x<0){
02492 obmc -= src_x;
02493 b_w += src_x;
02494 src_x=0;
02495 }else if(src_x + b_w > w){
02496 b_w = w - src_x;
02497 }
02498 if(src_y<0){
02499 obmc -= src_y*obmc_stride;
02500 b_h += src_y;
02501 src_y=0;
02502 }else if(src_y + b_h> h){
02503 b_h = h - src_y;
02504 }
02505
02506 if(b_w<=0 || b_h<=0) return;
02507
02508 assert(src_stride > 7*MB_SIZE);
02509
02510 dst8+= src_x + src_y*src_stride;
02511
02512
02513 block[0]= tmp+3*MB_SIZE;
02514 pred_block(s, block[0], src, tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
02515
02516 if(same_block(lt, rt)){
02517 block[1]= block[0];
02518 }else{
02519 block[1]= tmp + 4*MB_SIZE;
02520 pred_block(s, block[1], src, tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
02521 }
02522
02523 if(same_block(lt, lb)){
02524 block[2]= block[0];
02525 }else if(same_block(rt, lb)){
02526 block[2]= block[1];
02527 }else{
02528 block[2]= tmp+5*MB_SIZE;
02529 pred_block(s, block[2], src, tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
02530 }
02531
02532 if(same_block(lt, rb) ){
02533 block[3]= block[0];
02534 }else if(same_block(rt, rb)){
02535 block[3]= block[1];
02536 }else if(same_block(lb, rb)){
02537 block[3]= block[2];
02538 }else{
02539 block[3]= tmp+6*MB_SIZE;
02540 pred_block(s, block[3], src, tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
02541 }
02542 #if 0
02543 for(y=0; y<b_h; y++){
02544 for(x=0; x<b_w; x++){
02545 int v= obmc [x + y*obmc_stride] * block[3][x + y*src_stride] * (256/OBMC_MAX);
02546 if(add) dst[x + y*dst_stride] += v;
02547 else dst[x + y*dst_stride] -= v;
02548 }
02549 }
02550 for(y=0; y<b_h; y++){
02551 uint8_t *obmc2= obmc + (obmc_stride>>1);
02552 for(x=0; x<b_w; x++){
02553 int v= obmc2[x + y*obmc_stride] * block[2][x + y*src_stride] * (256/OBMC_MAX);
02554 if(add) dst[x + y*dst_stride] += v;
02555 else dst[x + y*dst_stride] -= v;
02556 }
02557 }
02558 for(y=0; y<b_h; y++){
02559 uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
02560 for(x=0; x<b_w; x++){
02561 int v= obmc3[x + y*obmc_stride] * block[1][x + y*src_stride] * (256/OBMC_MAX);
02562 if(add) dst[x + y*dst_stride] += v;
02563 else dst[x + y*dst_stride] -= v;
02564 }
02565 }
02566 for(y=0; y<b_h; y++){
02567 uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
02568 uint8_t *obmc4= obmc3+ (obmc_stride>>1);
02569 for(x=0; x<b_w; x++){
02570 int v= obmc4[x + y*obmc_stride] * block[0][x + y*src_stride] * (256/OBMC_MAX);
02571 if(add) dst[x + y*dst_stride] += v;
02572 else dst[x + y*dst_stride] -= v;
02573 }
02574 }
02575 #else
02576 {
02577
02578 START_TIMER
02579
02580 int block_index = 0;
02581 for(y=0; y<b_h; y++){
02582
02583 uint8_t *obmc1= obmc + y*obmc_stride;
02584 uint8_t *obmc2= obmc1+ (obmc_stride>>1);
02585 uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
02586 uint8_t *obmc4= obmc3+ (obmc_stride>>1);
02587 dst = slice_buffer_get_line(sb, src_y + y);
02588 for(x=0; x<b_w; x++){
02589 int v= obmc1[x] * block[3][x + y*src_stride]
02590 +obmc2[x] * block[2][x + y*src_stride]
02591 +obmc3[x] * block[1][x + y*src_stride]
02592 +obmc4[x] * block[0][x + y*src_stride];
02593
02594 v <<= 8 - LOG2_OBMC_MAX;
02595 if(FRAC_BITS != 8){
02596 v += 1<<(7 - FRAC_BITS);
02597 v >>= 8 - FRAC_BITS;
02598 }
02599 if(add){
02600
02601 v += dst[x + src_x];
02602 v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
02603 if(v&(~255)) v= ~(v>>31);
02604 dst8[x + y*src_stride] = v;
02605 }else{
02606
02607 dst[x + src_x] -= v;
02608 }
02609 }
02610 }
02611 STOP_TIMER("Inner add y block")
02612 }
02613 #endif
02614 }
02615
02616
02617 static always_inline void add_yblock(SnowContext *s, DWTELEM *dst, uint8_t *dst8, uint8_t *src, uint8_t *obmc, int src_x, int src_y, int b_w, int b_h, int w, int h, int dst_stride, int src_stride, int obmc_stride, int b_x, int b_y, int add, int plane_index){
02618 const int b_width = s->b_width << s->block_max_depth;
02619 const int b_height= s->b_height << s->block_max_depth;
02620 const int b_stride= b_width;
02621 BlockNode *lt= &s->block[b_x + b_y*b_stride];
02622 BlockNode *rt= lt+1;
02623 BlockNode *lb= lt+b_stride;
02624 BlockNode *rb= lb+1;
02625 uint8_t *block[4];
02626 uint8_t tmp[src_stride*(b_h+5)];
02627 int x,y;
02628
02629 if(b_x<0){
02630 lt= rt;
02631 lb= rb;
02632 }else if(b_x + 1 >= b_width){
02633 rt= lt;
02634 rb= lb;
02635 }
02636 if(b_y<0){
02637 lt= lb;
02638 rt= rb;
02639 }else if(b_y + 1 >= b_height){
02640 lb= lt;
02641 rb= rt;
02642 }
02643
02644 if(src_x<0){
02645 obmc -= src_x;
02646 b_w += src_x;
02647 src_x=0;
02648 }else if(src_x + b_w > w){
02649 b_w = w - src_x;
02650 }
02651 if(src_y<0){
02652 obmc -= src_y*obmc_stride;
02653 b_h += src_y;
02654 src_y=0;
02655 }else if(src_y + b_h> h){
02656 b_h = h - src_y;
02657 }
02658
02659 if(b_w<=0 || b_h<=0) return;
02660
02661 assert(src_stride > 7*MB_SIZE);
02662 dst += src_x + src_y*dst_stride;
02663 dst8+= src_x + src_y*src_stride;
02664
02665
02666 block[0]= tmp+3*MB_SIZE;
02667 pred_block(s, block[0], src, tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
02668
02669 if(same_block(lt, rt)){
02670 block[1]= block[0];
02671 }else{
02672 block[1]= tmp + 4*MB_SIZE;
02673 pred_block(s, block[1], src, tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
02674 }
02675
02676 if(same_block(lt, lb)){
02677 block[2]= block[0];
02678 }else if(same_block(rt, lb)){
02679 block[2]= block[1];
02680 }else{
02681 block[2]= tmp+5*MB_SIZE;
02682 pred_block(s, block[2], src, tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
02683 }
02684
02685 if(same_block(lt, rb) ){
02686 block[3]= block[0];
02687 }else if(same_block(rt, rb)){
02688 block[3]= block[1];
02689 }else if(same_block(lb, rb)){
02690 block[3]= block[2];
02691 }else{
02692 block[3]= tmp+6*MB_SIZE;
02693 pred_block(s, block[3], src, tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
02694 }
02695 #if 0
02696 for(y=0; y<b_h; y++){
02697 for(x=0; x<b_w; x++){
02698 int v= obmc [x + y*obmc_stride] * block[3][x + y*src_stride] * (256/OBMC_MAX);
02699 if(add) dst[x + y*dst_stride] += v;
02700 else dst[x + y*dst_stride] -= v;
02701 }
02702 }
02703 for(y=0; y<b_h; y++){
02704 uint8_t *obmc2= obmc + (obmc_stride>>1);
02705 for(x=0; x<b_w; x++){
02706 int v= obmc2[x + y*obmc_stride] * block[2][x + y*src_stride] * (256/OBMC_MAX);
02707 if(add) dst[x + y*dst_stride] += v;
02708 else dst[x + y*dst_stride] -= v;
02709 }
02710 }
02711 for(y=0; y<b_h; y++){
02712 uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
02713 for(x=0; x<b_w; x++){
02714 int v= obmc3[x + y*obmc_stride] * block[1][x + y*src_stride] * (256/OBMC_MAX);
02715 if(add) dst[x + y*dst_stride] += v;
02716 else dst[x + y*dst_stride] -= v;
02717 }
02718 }
02719 for(y=0; y<b_h; y++){
02720 uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
02721 uint8_t *obmc4= obmc3+ (obmc_stride>>1);
02722 for(x=0; x<b_w; x++){
02723 int v= obmc4[x + y*obmc_stride] * block[0][x + y*src_stride] * (256/OBMC_MAX);
02724 if(add) dst[x + y*dst_stride] += v;
02725 else dst[x + y*dst_stride] -= v;
02726 }
02727 }
02728 #else
02729 for(y=0; y<b_h; y++){
02730
02731 uint8_t *obmc1= obmc + y*obmc_stride;
02732 uint8_t *obmc2= obmc1+ (obmc_stride>>1);
02733 uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
02734 uint8_t *obmc4= obmc3+ (obmc_stride>>1);
02735 for(x=0; x<b_w; x++){
02736 int v= obmc1[x] * block[3][x + y*src_stride]
02737 +obmc2[x] * block[2][x + y*src_stride]
02738 +obmc3[x] * block[1][x + y*src_stride]
02739 +obmc4[x] * block[0][x + y*src_stride];
02740
02741 v <<= 8 - LOG2_OBMC_MAX;
02742 if(FRAC_BITS != 8){
02743 v += 1<<(7 - FRAC_BITS);
02744 v >>= 8 - FRAC_BITS;
02745 }
02746 if(add){
02747 v += dst[x + y*dst_stride];
02748 v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
02749 if(v&(~255)) v= ~(v>>31);
02750 dst8[x + y*src_stride] = v;
02751 }else{
02752 dst[x + y*dst_stride] -= v;
02753 }
02754 }
02755 }
02756 #endif
02757 }
02758
02759 static always_inline void predict_slice_buffered(SnowContext *s, slice_buffer * sb, DWTELEM * old_buffer, int plane_index, int add, int mb_y){
02760 Plane *p= &s->plane[plane_index];
02761 const int mb_w= s->b_width << s->block_max_depth;
02762 const int mb_h= s->b_height << s->block_max_depth;
02763 int x, y, mb_x;
02764 int block_size = MB_SIZE >> s->block_max_depth;
02765 int block_w = plane_index ? block_size/2 : block_size;
02766 const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
02767 int obmc_stride= plane_index ? block_size : 2*block_size;
02768 int ref_stride= s->current_picture.linesize[plane_index];
02769 uint8_t *ref = s->last_picture.data[plane_index];
02770 uint8_t *dst8= s->current_picture.data[plane_index];
02771 int w= p->width;
02772 int h= p->height;
02773 START_TIMER
02774
02775 if(s->keyframe || (s->avctx->debug&512)){
02776 if(mb_y==mb_h)
02777 return;
02778
02779 if(add){
02780 for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++)
02781 {
02782
02783 DWTELEM * line = sb->line[y];
02784 for(x=0; x<w; x++)
02785 {
02786
02787 int v= line[x] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
02788 v >>= FRAC_BITS;
02789 if(v&(~255)) v= ~(v>>31);
02790 dst8[x + y*ref_stride]= v;
02791 }
02792 }
02793 }else{
02794 for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++)
02795 {
02796
02797 DWTELEM * line = sb->line[y];
02798 for(x=0; x<w; x++)
02799 {
02800 line[x] -= 128 << FRAC_BITS;
02801
02802 }
02803 }
02804 }
02805
02806 return;
02807 }
02808
02809 for(mb_x=0; mb_x<=mb_w; mb_x++){
02810 START_TIMER
02811
02812 add_yblock_buffered(s, sb, old_buffer, dst8, ref, obmc,
02813 block_w*mb_x - block_w/2,
02814 block_w*mb_y - block_w/2,
02815 block_w, block_w,
02816 w, h,
02817 w, ref_stride, obmc_stride,
02818 mb_x - 1, mb_y - 1,
02819 add, plane_index);
02820
02821 STOP_TIMER("add_yblock")
02822 }
02823
02824 STOP_TIMER("predict_slice")
02825 }
02826
02827 static always_inline void predict_slice(SnowContext *s, DWTELEM *buf, int plane_index, int add, int mb_y){
02828 Plane *p= &s->plane[plane_index];
02829 const int mb_w= s->b_width << s->block_max_depth;
02830 const int mb_h= s->b_height << s->block_max_depth;
02831 int x, y, mb_x;
02832 int block_size = MB_SIZE >> s->block_max_depth;
02833 int block_w = plane_index ? block_size/2 : block_size;
02834 const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
02835 int obmc_stride= plane_index ? block_size : 2*block_size;
02836 int ref_stride= s->current_picture.linesize[plane_index];
02837 uint8_t *ref = s->last_picture.data[plane_index];
02838 uint8_t *dst8= s->current_picture.data[plane_index];
02839 int w= p->width;
02840 int h= p->height;
02841 START_TIMER
02842
02843 if(s->keyframe || (s->avctx->debug&512)){
02844 if(mb_y==mb_h)
02845 return;
02846
02847 if(add){
02848 for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
02849 for(x=0; x<w; x++){
02850 int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
02851 v >>= FRAC_BITS;
02852 if(v&(~255)) v= ~(v>>31);
02853 dst8[x + y*ref_stride]= v;
02854 }
02855 }
02856 }else{
02857 for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
02858 for(x=0; x<w; x++){
02859 buf[x + y*w]-= 128<<FRAC_BITS;
02860 }
02861 }
02862 }
02863
02864 return;
02865 }
02866
02867 for(mb_x=0; mb_x<=mb_w; mb_x++){
02868 START_TIMER
02869
02870 add_yblock(s, buf, dst8, ref, obmc,
02871 block_w*mb_x - block_w/2,
02872 block_w*mb_y - block_w/2,
02873 block_w, block_w,
02874 w, h,
02875 w, ref_stride, obmc_stride,
02876 mb_x - 1, mb_y - 1,
02877 add, plane_index);
02878
02879 STOP_TIMER("add_yblock")
02880 }
02881
02882 STOP_TIMER("predict_slice")
02883 }
02884
02885 static always_inline void predict_plane(SnowContext *s, DWTELEM *buf, int plane_index, int add){
02886 const int mb_h= s->b_height << s->block_max_depth;
02887 int mb_y;
02888 for(mb_y=0; mb_y<=mb_h; mb_y++)
02889 predict_slice(s, buf, plane_index, add, mb_y);
02890 }
02891
02892 static void quantize(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int bias){
02893 const int level= b->level;
02894 const int w= b->width;
02895 const int h= b->height;
02896 const int qlog= clip(s->qlog + b->qlog, 0, QROOT*16);
02897 const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
02898 int x,y, thres1, thres2;
02899 START_TIMER
02900
02901 if(s->qlog == LOSSLESS_QLOG) return;
02902
02903 bias= bias ? 0 : (3*qmul)>>3;
02904 thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
02905 thres2= 2*thres1;
02906
02907 if(!bias){
02908 for(y=0; y<h; y++){
02909 for(x=0; x<w; x++){
02910 int i= src[x + y*stride];
02911
02912 if((unsigned)(i+thres1) > thres2){
02913 if(i>=0){
02914 i<<= QEXPSHIFT;
02915 i/= qmul;
02916 src[x + y*stride]= i;
02917 }else{
02918 i= -i;
02919 i<<= QEXPSHIFT;
02920 i/= qmul;
02921 src[x + y*stride]= -i;
02922 }
02923 }else
02924 src[x + y*stride]= 0;
02925 }
02926 }
02927 }else{
02928 for(y=0; y<h; y++){
02929 for(x=0; x<w; x++){
02930 int i= src[x + y*stride];
02931
02932 if((unsigned)(i+thres1) > thres2){
02933 if(i>=0){
02934 i<<= QEXPSHIFT;
02935 i= (i + bias) / qmul;
02936 src[x + y*stride]= i;
02937 }else{
02938 i= -i;
02939 i<<= QEXPSHIFT;
02940 i= (i + bias) / qmul;
02941 src[x + y*stride]= -i;
02942 }
02943 }else
02944 src[x + y*stride]= 0;
02945 }
02946 }
02947 }
02948 if(level+1 == s->spatial_decomposition_count){
02949
02950 }
02951 }
02952
02953 static void dequantize_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, DWTELEM *src, int stride, int start_y, int end_y){
02954 const int w= b->width;
02955 const int h= b->height;
02956 const int qlog= clip(s->qlog + b->qlog, 0, QROOT*16);
02957 const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
02958 const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
02959 int x,y;
02960 START_TIMER
02961
02962 if(s->qlog == LOSSLESS_QLOG) return;
02963
02964 for(y=start_y; y<end_y; y++){
02965
02966 DWTELEM * line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
02967 for(x=0; x<w; x++){
02968 int i= line[x];
02969 if(i<0){
02970 line[x]= -((-i*qmul + qadd)>>(QEXPSHIFT));
02971 }else if(i>0){
02972 line[x]= (( i*qmul + qadd)>>(QEXPSHIFT));
02973 }
02974 }
02975 }
02976 if(w > 200 ){
02977 STOP_TIMER("dquant")
02978 }
02979 }
02980
02981 static void dequantize(SnowContext *s, SubBand *b, DWTELEM *src, int stride){
02982 const int w= b->width;
02983 const int h= b->height;
02984 const int qlog= clip(s->qlog + b->qlog, 0, QROOT*16);
02985 const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
02986 const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
02987 int x,y;
02988 START_TIMER
02989
02990 if(s->qlog == LOSSLESS_QLOG) return;
02991
02992 for(y=0; y<h; y++){
02993 for(x=0; x<w; x++){
02994 int i= src[x + y*stride];
02995 if(i<0){
02996 src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT));
02997 }else if(i>0){
02998 src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
02999 }
03000 }
03001 }
03002 if(w > 200 ){
03003 STOP_TIMER("dquant")
03004 }
03005 }
03006
03007 static void decorrelate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
03008 const int w= b->width;
03009 const int h= b->height;
03010 int x,y;
03011
03012 for(y=h-1; y>=0; y--){
03013 for(x=w-1; x>=0; x--){
03014 int i= x + y*stride;
03015
03016 if(x){
03017 if(use_median){
03018 if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
03019 else src[i] -= src[i - 1];
03020 }else{
03021 if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
03022 else src[i] -= src[i - 1];
03023 }
03024 }else{
03025 if(y) src[i] -= src[i - stride];
03026 }
03027 }
03028 }
03029 }
03030
03031 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){
03032 const int w= b->width;
03033 const int h= b->height;
03034 int x,y;
03035
03036
03037
03038 DWTELEM * line;
03039 DWTELEM * prev;
03040
03041 if (start_y != 0)
03042 line = slice_buffer_get_line(sb, ((start_y - 1) * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
03043
03044 for(y=start_y; y<end_y; y++){
03045 prev = line;
03046
03047 line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
03048 for(x=0; x<w; x++){
03049 if(x){
03050 if(use_median){
03051 if(y && x+1<w) line[x] += mid_pred(line[x - 1], prev[x], prev[x + 1]);
03052 else line[x] += line[x - 1];
03053 }else{
03054 if(y) line[x] += mid_pred(line[x - 1], prev[x], line[x - 1] + prev[x] - prev[x - 1]);
03055 else line[x] += line[x - 1];
03056 }
03057 }else{
03058 if(y) line[x] += prev[x];
03059 }
03060 }
03061 }
03062
03063
03064 }
03065
03066 static void correlate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
03067 const int w= b->width;
03068 const int h= b->height;
03069 int x,y;
03070
03071 for(y=0; y<h; y++){
03072 for(x=0; x<w; x++){
03073 int i= x + y*stride;
03074
03075 if(x){
03076 if(use_median){
03077 if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
03078 else src[i] += src[i - 1];
03079 }else{
03080 if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
03081 else src[i] += src[i - 1];
03082 }
03083 }else{
03084 if(y) src[i] += src[i - stride];
03085 }
03086 }
03087 }
03088 }
03089
03090 static void encode_header(SnowContext *s){
03091 int plane_index, level, orientation;
03092 uint8_t kstate[32];
03093
03094 memset(kstate, MID_STATE, sizeof(kstate));
03095
03096 put_rac(&s->c, kstate, s->keyframe);
03097 if(s->keyframe || s->always_reset)
03098 reset_contexts(s);
03099 if(s->keyframe){
03100 put_symbol(&s->c, s->header_state, s->version, 0);
03101 put_rac(&s->c, s->header_state, s->always_reset);
03102 put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
03103 put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
03104 put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
03105 put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
03106 put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
03107 put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
03108 put_rac(&s->c, s->header_state, s->spatial_scalability);
03109
03110
03111 for(plane_index=0; plane_index<2; plane_index++){
03112 for(level=0; level<s->spatial_decomposition_count; level++){
03113 for(orientation=level ? 1:0; orientation<4; orientation++){
03114 if(orientation==2) continue;
03115 put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
03116 }
03117 }
03118 }
03119 }
03120 put_symbol(&s->c, s->header_state, s->spatial_decomposition_type, 0);
03121 put_symbol(&s->c, s->header_state, s->qlog, 1);
03122 put_symbol(&s->c, s->header_state, s->mv_scale, 0);
03123 put_symbol(&s->c, s->header_state, s->qbias, 1);
03124 put_symbol(&s->c, s->header_state, s->block_max_depth, 0);
03125 }
03126
03127 static int decode_header(SnowContext *s){
03128 int plane_index, level, orientation;
03129 uint8_t kstate[32];
03130
03131 memset(kstate, MID_STATE, sizeof(kstate));
03132
03133 s->keyframe= get_rac(&s->c, kstate);
03134 if(s->keyframe || s->always_reset)
03135 reset_contexts(s);
03136 if(s->keyframe){
03137 s->version= get_symbol(&s->c, s->header_state, 0);
03138 if(s->version>0){
03139 av_log(s->avctx, AV_LOG_ERROR, "version %d not supported", s->version);
03140 return -1;
03141 }
03142 s->always_reset= get_rac(&s->c, s->header_state);
03143 s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0);
03144 s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0);
03145 s->spatial_decomposition_count= get_symbol(&s->c, s->header_state, 0);
03146 s->colorspace_type= get_symbol(&s->c, s->header_state, 0);
03147 s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0);
03148 s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0);
03149 s->spatial_scalability= get_rac(&s->c, s->header_state);
03150
03151
03152 for(plane_index=0; plane_index<3; plane_index++){
03153 for(level=0; level<s->spatial_decomposition_count; level++){
03154 for(orientation=level ? 1:0; orientation<4; orientation++){
03155 int q;
03156 if (plane_index==2) q= s->plane[1].band[level][orientation].qlog;
03157 else if(orientation==2) q= s->plane[plane_index].band[level][1].qlog;
03158 else q= get_symbol(&s->c, s->header_state, 1);
03159 s->plane[plane_index].band[level][orientation].qlog= q;
03160 }
03161 }
03162 }
03163 }
03164
03165 s->spatial_decomposition_type= get_symbol(&s->c, s->header_state, 0);
03166 if(s->spatial_decomposition_type > 2){
03167 av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported", s->spatial_decomposition_type);
03168 return -1;
03169 }
03170
03171 s->qlog= get_symbol(&s->c, s->header_state, 1);
03172 s->mv_scale= get_symbol(&s->c, s->header_state, 0);
03173 s->qbias= get_symbol(&s->c, s->header_state, 1);
03174 s->block_max_depth= get_symbol(&s->c, s->header_state, 0);
03175
03176 return 0;
03177 }
03178
03179 static void init_qexp(){
03180 int i;
03181 double v=128;
03182
03183 for(i=0; i<QROOT; i++){
03184 qexp[i]= lrintf(v);
03185 v *= pow(2, 1.0 / QROOT);
03186 }
03187 }
03188
03189 static int common_init(AVCodecContext *avctx){
03190 SnowContext *s = avctx->priv_data;
03191 int width, height;
03192 int level, orientation, plane_index, dec;
03193
03194 s->avctx= avctx;
03195
03196 dsputil_init(&s->dsp, avctx);
03197
03198 #define mcf(dx,dy)\
03199 s->dsp.put_qpel_pixels_tab [0][dy+dx/4]=\
03200 s->dsp.put_no_rnd_qpel_pixels_tab[0][dy+dx/4]=\
03201 s->dsp.put_h264_qpel_pixels_tab[0][dy+dx/4];\
03202 s->dsp.put_qpel_pixels_tab [1][dy+dx/4]=\
03203 s->dsp.put_no_rnd_qpel_pixels_tab[1][dy+dx/4]=\
03204 s->dsp.put_h264_qpel_pixels_tab[1][dy+dx/4];
03205
03206 mcf( 0, 0)
03207 mcf( 4, 0)
03208 mcf( 8, 0)
03209 mcf(12, 0)
03210 mcf( 0, 4)
03211 mcf( 4, 4)
03212 mcf( 8, 4)
03213 mcf(12, 4)
03214 mcf( 0, 8)
03215 mcf( 4, 8)
03216 mcf( 8, 8)
03217 mcf(12, 8)
03218 mcf( 0,12)
03219 mcf( 4,12)
03220 mcf( 8,12)
03221 mcf(12,12)
03222
03223 #define mcfh(dx,dy)\
03224 s->dsp.put_pixels_tab [0][dy/4+dx/8]=\
03225 s->dsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
03226 mc_block_hpel ## dx ## dy ## 16;\
03227 s->dsp.put_pixels_tab [1][dy/4+dx/8]=\
03228 s->dsp.put_no_rnd_pixels_tab[1][dy/4+dx/8]=\
03229 mc_block_hpel ## dx ## dy ## 8;
03230
03231 mcfh(0, 0)
03232 mcfh(8, 0)
03233 mcfh(0, 8)
03234 mcfh(8, 8)
03235
03236 if(!qexp[0])
03237 init_qexp();
03238
03239 dec= s->spatial_decomposition_count= 5;
03240 s->spatial_decomposition_type= avctx->prediction_method;
03241
03242 s->chroma_h_shift= 1;
03243 s->chroma_v_shift= 1;
03244
03245
03246
03247 width= s->avctx->width;
03248 height= s->avctx->height;
03249
03250 s->spatial_dwt_buffer= av_mallocz(width*height*sizeof(DWTELEM));
03251
03252 s->mv_scale= (s->avctx->flags & CODEC_FLAG_QPEL) ? 2 : 4;
03253 s->block_max_depth= (s->avctx->flags & CODEC_FLAG_4MV) ? 1 : 0;
03254
03255 for(plane_index=0; plane_index<3; plane_index++){
03256 int w= s->avctx->width;
03257 int h= s->avctx->height;
03258
03259 if(plane_index){
03260 w>>= s->chroma_h_shift;
03261 h>>= s->chroma_v_shift;
03262 }
03263 s->plane[plane_index].width = w;
03264 s->plane[plane_index].height= h;
03265
03266 for(level=s->spatial_decomposition_count-1; level>=0; level--){
03267 for(orientation=level ? 1 : 0; orientation<4; orientation++){
03268 SubBand *b= &s->plane[plane_index].band[level][orientation];
03269
03270 b->buf= s->spatial_dwt_buffer;
03271 b->level= level;
03272 b->stride= s->plane[plane_index].width << (s->spatial_decomposition_count - level);
03273 b->width = (w + !(orientation&1))>>1;
03274 b->height= (h + !(orientation>1))>>1;
03275
03276 b->stride_line = 1 << (s->spatial_decomposition_count - level);
03277 b->buf_x_offset = 0;
03278 b->buf_y_offset = 0;
03279
03280 if(orientation&1){
03281 b->buf += (w+1)>>1;
03282 b->buf_x_offset = (w+1)>>1;
03283 }
03284 if(orientation>1){
03285 b->buf += b->stride>>1;
03286 b->buf_y_offset = b->stride_line >> 1;
03287 }
03288
03289 if(level)
03290 b->parent= &s->plane[plane_index].band[level-1][orientation];
03291 b->x_coeff=av_mallocz(((b->width+1) * b->height+1)*sizeof(x_and_coeff));
03292 }
03293 w= (w+1)>>1;
03294 h= (h+1)>>1;
03295 }
03296 }
03297
03298 reset_contexts(s);
03299
03300
03301
03302
03303
03304
03305 s->avctx->get_buffer(s->avctx, &s->mconly_picture);
03306
03307 return 0;
03308 }
03309
03310
03311 static void calculate_vissual_weight(SnowContext *s, Plane *p){
03312 int width = p->width;
03313 int height= p->height;
03314 int level, orientation, x, y;
03315
03316 for(level=0; level<s->spatial_decomposition_count; level++){
03317 for(orientation=level ? 1 : 0; orientation<4; orientation++){
03318 SubBand *b= &p->band[level][orientation];
03319 DWTELEM *buf= b->buf;
03320 int64_t error=0;
03321
03322 memset(s->spatial_dwt_buffer, 0, sizeof(int)*width*height);
03323 buf[b->width/2 + b->height/2*b->stride]= 256*256;
03324 ff_spatial_idwt(s->spatial_dwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
03325 for(y=0; y<height; y++){
03326 for(x=0; x<width; x++){
03327 int64_t d= s->spatial_dwt_buffer[x + y*width];
03328 error += d*d;
03329 }
03330 }
03331
03332 b->qlog= (int)(log(352256.0/sqrt(error)) / log(pow(2.0, 1.0/QROOT))+0.5);
03333
03334 }
03335 }
03336 }
03337
03338 static int encode_init(AVCodecContext *avctx)
03339 {
03340 SnowContext *s = avctx->priv_data;
03341 int plane_index;
03342
03343 if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
03344 av_log(avctx, AV_LOG_ERROR, "this codec is under development, files encoded with it may not be decodable with future versions!!!\n"
03345 "use vstrict=-2 / -strict -2 to use it anyway\n");
03346 return -1;
03347 }
03348
03349 common_init(avctx);
03350 alloc_blocks(s);
03351
03352 s->version=0;
03353
03354 s->m.avctx = avctx;
03355 s->m.flags = avctx->flags;
03356 s->m.bit_rate= avctx->bit_rate;
03357
03358 s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
03359 s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
03360 s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
03361 h263_encode_init(&s->m);
03362
03363 if(avctx->flags&CODEC_FLAG_PASS1){
03364 if(!avctx->stats_out)
03365 avctx->stats_out = av_mallocz(256);
03366 }
03367 if(avctx->flags&CODEC_FLAG_PASS2){
03368 if(ff_rate_control_init(&s->m) < 0)
03369 return -1;
03370 }
03371
03372 for(plane_index=0; plane_index<3; plane_index++){
03373 calculate_vissual_weight(s, &s->plane[plane_index]);
03374 }
03375
03376
03377 avctx->coded_frame= &s->current_picture;
03378 switch(avctx->pix_fmt){
03379
03380
03381 case PIX_FMT_YUV420P:
03382 case PIX_FMT_GRAY8:
03383
03384
03385 s->colorspace_type= 0;
03386 break;
03387
03388
03389
03390 default:
03391 av_log(avctx, AV_LOG_ERROR, "format not supported\n");
03392 return -1;
03393 }
03394
03395 s->chroma_h_shift= 1;
03396 s->chroma_v_shift= 1;
03397 return 0;
03398 }
03399
03400 static int frame_start(SnowContext *s){
03401 AVFrame tmp;
03402 int w= s->avctx->width;
03403 int h= s->avctx->height;
03404
03405 if(s->current_picture.data[0]){
03406 draw_edges(s->current_picture.data[0], s->current_picture.linesize[0], w , h , EDGE_WIDTH );
03407 draw_edges(s->current_picture.data[1], s->current_picture.linesize[1], w>>1, h>>1, EDGE_WIDTH/2);
03408 draw_edges(s->current_picture.data[2], s->current_picture.linesize[2], w>>1, h>>1, EDGE_WIDTH/2);
03409 }
03410
03411 tmp= s->last_picture;
03412 s->last_picture= s->current_picture;
03413 s->current_picture= tmp;
03414
03415 s->current_picture.reference= 1;
03416 if(s->avctx->get_buffer(s->avctx, &s->current_picture) < 0){
03417 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
03418 return -1;
03419 }
03420
03421 return 0;
03422 }
03423
03424 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
03425 SnowContext *s = avctx->priv_data;
03426 RangeCoder * const c= &s->c;
03427 AVFrame *pict = data;
03428 const int width= s->avctx->width;
03429 const int height= s->avctx->height;
03430 int level, orientation, plane_index;
03431
03432 ff_init_range_encoder(c, buf, buf_size);
03433 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
03434
03435 s->input_picture = *pict;
03436
03437 if(avctx->flags&CODEC_FLAG_PASS2){
03438 s->m.pict_type =
03439 pict->pict_type= s->m.rc_context.entry[avctx->frame_number].new_pict_type;
03440 s->keyframe= pict->pict_type==FF_I_TYPE;
03441 s->m.picture_number= avctx->frame_number;
03442 pict->quality= ff_rate_estimate_qscale(&s->m);
03443 }else{
03444 s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
03445 pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE;
03446 }
03447
03448 if(pict->quality){
03449 s->qlog= rint(QROOT*log(pict->quality / (float)FF_QP2LAMBDA)/log(2));
03450
03451 s->qlog += 61*QROOT/8;
03452 }else{
03453 s->qlog= LOSSLESS_QLOG;
03454 }
03455
03456 frame_start(s);
03457 s->current_picture.key_frame= s->keyframe;
03458
03459 s->m.current_picture_ptr= &s->m.current_picture;
03460 if(pict->pict_type == P_TYPE){
03461 int block_width = (width +15)>>4;
03462 int block_height= (height+15)>>4;
03463 int stride= s->current_picture.linesize[0];
03464
03465 assert(s->current_picture.data[0]);
03466 assert(s->last_picture.data[0]);
03467
03468 s->m.avctx= s->avctx;
03469 s->m.current_picture.data[0]= s->current_picture.data[0];
03470 s->m. last_picture.data[0]= s-> last_picture.data[0];
03471 s->m. new_picture.data[0]= s-> input_picture.data[0];
03472 s->m. last_picture_ptr= &s->m. last_picture;
03473 s->m.linesize=
03474 s->m. last_picture.linesize[0]=
03475 s->m. new_picture.linesize[0]=
03476 s->m.current_picture.linesize[0]= stride;
03477 s->m.uvlinesize= s->current_picture.linesize[1];
03478 s->m.width = width;
03479 s->m.height= height;
03480 s->m.mb_width = block_width;
03481 s->m.mb_height= block_height;
03482 s->m.mb_stride= s->m.mb_width+1;
03483 s->m.b8_stride= 2*s->m.mb_width+1;
03484 s->m.f_code=1;
03485 s->m.pict_type= pict->pict_type;
03486 s->m.me_method= s->avctx->me_method;
03487 s->m.me.scene_change_score=0;
03488 s->m.flags= s->avctx->flags;
03489 s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0;
03490 s->m.out_format= FMT_H263;
03491 s->m.unrestricted_mv= 1;
03492
03493 s->lambda = s->m.lambda= pict->quality * 3/2;
03494 s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
03495 s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
03496
03497 s->m.dsp= s->dsp;
03498 ff_init_me(&s->m);
03499 }
03500
03501 redo_frame:
03502
03503 s->qbias= pict->pict_type == P_TYPE ? 2 : 0;
03504
03505 encode_header(s);
03506 s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start);
03507 encode_blocks(s);
03508 s->m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits;
03509
03510 for(plane_index=0; plane_index<3; plane_index++){
03511 Plane *p= &s->plane[plane_index];
03512 int w= p->width;
03513 int h= p->height;
03514 int x, y;
03515
03516
03517
03518 if(pict->data[plane_index])
03519 for(y=0; y<h; y++){
03520 for(x=0; x<w; x++){
03521 s->spatial_dwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
03522 }
03523 }
03524 predict_plane(s, s->spatial_dwt_buffer, plane_index, 0);
03525
03526 if( plane_index==0
03527 && pict->pict_type == P_TYPE
03528 && s->m.me.scene_change_score > s->avctx->scenechange_threshold){
03529 ff_init_range_encoder(c, buf, buf_size);
03530 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
03531 pict->pict_type= FF_I_TYPE;
03532 s->keyframe=1;
03533 reset_contexts(s);
03534 goto redo_frame;
03535 }
03536
03537 if(s->qlog == LOSSLESS_QLOG){
03538 for(y=0; y<h; y++){
03539 for(x=0; x<w; x++){
03540 s->spatial_dwt_buffer[y*w + x]= (s->spatial_dwt_buffer[y*w + x] + (1<<(FRAC_BITS-1))-1)>>FRAC_BITS;
03541 }
03542 }
03543 }
03544
03545 ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
03546
03547 for(level=0; level<s->spatial_decomposition_count; level++){
03548 for(orientation=level ? 1 : 0; orientation<4; orientation++){
03549 SubBand *b= &p->band[level][orientation];
03550
03551 quantize(s, b, b->buf, b->stride, s->qbias);
03552 if(orientation==0)
03553 decorrelate(s, b, b->buf, b->stride, pict->pict_type == P_TYPE, 0);
03554 encode_subband(s, b, b->buf, b->parent ? b->parent->buf : NULL, b->stride, orientation);
03555 assert(b->parent==NULL || b->parent->stride == b->stride*2);
03556 if(orientation==0)
03557 correlate(s, b, b->buf, b->stride, 1, 0);
03558 }
03559 }
03560
03561
03562 for(level=0; level<s->spatial_decomposition_count; level++){
03563 for(orientation=level ? 1 : 0; orientation<4; orientation++){
03564 SubBand *b= &p->band[level][orientation];
03565
03566 dequantize(s, b, b->buf, b->stride);
03567 }
03568 }
03569
03570 ff_spatial_idwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
03571 if(s->qlog == LOSSLESS_QLOG){
03572 for(y=0; y<h; y++){
03573 for(x=0; x<w; x++){
03574 s->spatial_dwt_buffer[y*w + x]<<=FRAC_BITS;
03575 }
03576 }
03577 }
03578 {START_TIMER
03579 predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
03580 STOP_TIMER("pred-conv")}
03581 if(s->avctx->flags&CODEC_FLAG_PSNR){
03582 int64_t error= 0;
03583
03584 if(pict->data[plane_index])
03585 for(y=0; y<h; y++){
03586 for(x=0; x<w; x++){
03587 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];
03588 error += d*d;
03589 }
03590 }
03591 s->avctx->error[plane_index] += error;
03592 s->current_picture.error[plane_index] = error;
03593 }
03594 }
03595
03596 if(s->last_picture.data[0])
03597 avctx->release_buffer(avctx, &s->last_picture);
03598
03599 s->current_picture.coded_picture_number = avctx->frame_number;
03600 s->current_picture.pict_type = pict->pict_type;
03601 s->current_picture.quality = pict->quality;
03602 if(avctx->flags&CODEC_FLAG_PASS1){
03603 s->m.p_tex_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits - s->m.mv_bits;
03604 s->m.current_picture.display_picture_number =
03605 s->m.current_picture.coded_picture_number = avctx->frame_number;
03606 s->m.pict_type = pict->pict_type;
03607 s->m.current_picture.quality = pict->quality;
03608 ff_write_pass1_stats(&s->m);
03609 }
03610 if(avctx->flags&CODEC_FLAG_PASS2){
03611 s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
03612 }
03613
03614 emms_c();
03615
03616 return ff_rac_terminate(c);
03617 }
03618
03619 static void common_end(SnowContext *s){
03620 int plane_index, level, orientation;
03621
03622 av_freep(&s->spatial_dwt_buffer);
03623
03624 av_freep(&s->m.me.scratchpad);
03625 av_freep(&s->m.me.map);
03626 av_freep(&s->m.me.score_map);
03627
03628 av_freep(&s->block);
03629
03630 for(plane_index=0; plane_index<3; plane_index++){
03631 for(level=s->spatial_decomposition_count-1; level>=0; level--){
03632 for(orientation=level ? 1 : 0; orientation<4; orientation++){
03633 SubBand *b= &s->plane[plane_index].band[level][orientation];
03634
03635 av_freep(&b->x_coeff);
03636 }
03637 }
03638 }
03639 }
03640
03641 static int encode_end(AVCodecContext *avctx)
03642 {
03643 SnowContext *s = avctx->priv_data;
03644
03645 common_end(s);
03646 av_free(avctx->stats_out);
03647
03648 return 0;
03649 }
03650
03651 static int decode_init(AVCodecContext *avctx)
03652 {
03653 SnowContext *s = avctx->priv_data;
03654 int block_size;
03655
03656 avctx->pix_fmt= PIX_FMT_YUV420P;
03657
03658 common_init(avctx);
03659
03660 block_size = MB_SIZE >> s->block_max_depth;
03661 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);
03662
03663 return 0;
03664 }
03665
03666 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
03667 SnowContext *s = avctx->priv_data;
03668 RangeCoder * const c= &s->c;
03669 int bytes_read;
03670 AVFrame *picture = data;
03671 int level, orientation, plane_index;
03672
03673 ff_init_range_decoder(c, buf, buf_size);
03674 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
03675
03676 s->current_picture.pict_type= FF_I_TYPE;
03677 decode_header(s);
03678 if(!s->block) alloc_blocks(s);
03679
03680 frame_start(s);
03681
03682 if(avctx->debug&FF_DEBUG_PICT_INFO)
03683 av_log(avctx, AV_LOG_ERROR, "keyframe:%d qlog:%d\n", s->keyframe, s->qlog);
03684
03685 decode_blocks(s);
03686
03687 for(plane_index=0; plane_index<3; plane_index++){
03688 Plane *p= &s->plane[plane_index];
03689 int w= p->width;
03690 int h= p->height;
03691 int x, y;
03692 int decode_state[MAX_DECOMPOSITIONS][4][1];
03693
03694 if(s->avctx->debug&2048){
03695 memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
03696 predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
03697
03698 for(y=0; y<h; y++){
03699 for(x=0; x<w; x++){
03700 int v= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x];
03701 s->mconly_picture.data[plane_index][y*s->mconly_picture.linesize[plane_index] + x]= v;
03702 }
03703 }
03704 }
03705
03706 { START_TIMER
03707 for(level=0; level<s->spatial_decomposition_count; level++){
03708 for(orientation=level ? 1 : 0; orientation<4; orientation++){
03709 SubBand *b= &p->band[level][orientation];
03710 unpack_coeffs(s, b, b->parent, orientation);
03711 }
03712 }
03713 STOP_TIMER("unpack coeffs");
03714 }
03715
03716 {START_TIMER
03717 const int mb_h= s->b_height << s->block_max_depth;
03718 const int block_size = MB_SIZE >> s->block_max_depth;
03719 const int block_w = plane_index ? block_size/2 : block_size;
03720 int mb_y;
03721 dwt_compose_t cs[MAX_DECOMPOSITIONS];
03722 int yd=0, yq=0;
03723 int y;
03724 int end_y;
03725
03726 ff_spatial_idwt_buffered_init(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count);
03727 for(mb_y=0; mb_y<=mb_h; mb_y++){
03728
03729 int slice_starty = block_w*mb_y;
03730 int slice_h = block_w*(mb_y+1);
03731 if (!(s->keyframe || s->avctx->debug&512)){
03732 slice_starty = FFMAX(0, slice_starty - (block_w >> 1));
03733 slice_h -= (block_w >> 1);
03734 }
03735
03736 {
03737 START_TIMER
03738 for(level=0; level<s->spatial_decomposition_count; level++){
03739 for(orientation=level ? 1 : 0; orientation<4; orientation++){
03740 SubBand *b= &p->band[level][orientation];
03741 int start_y;
03742 int end_y;
03743 int our_mb_start = mb_y;
03744 int our_mb_end = (mb_y + 1);
03745 start_y = (mb_y ? ((block_w * our_mb_start) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + 2: 0);
03746 end_y = (((block_w * our_mb_end) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + 2);
03747 if (!(s->keyframe || s->avctx->debug&512)){
03748 start_y = FFMAX(0, start_y - (block_w >> (1+s->spatial_decomposition_count - level)));
03749 end_y = FFMAX(0, end_y - (block_w >> (1+s->spatial_decomposition_count - level)));
03750 }
03751 start_y = FFMIN(b->height, start_y);
03752 end_y = FFMIN(b->height, end_y);
03753
03754 if (start_y != end_y){
03755 if (orientation == 0){
03756 SubBand * correlate_band = &p->band[0][0];
03757 int correlate_end_y = FFMIN(b->height, end_y + 1);
03758 int correlate_start_y = FFMIN(b->height, (start_y ? start_y + 1 : 0));
03759 decode_subband_slice_buffered(s, correlate_band, &s->sb, correlate_start_y, correlate_end_y, decode_state[0][0]);
03760 correlate_slice_buffered(s, &s->sb, correlate_band, correlate_band->buf, correlate_band->stride, 1, 0, correlate_start_y, correlate_end_y);
03761 dequantize_slice_buffered(s, &s->sb, correlate_band, correlate_band->buf, correlate_band->stride, start_y, end_y);
03762 }
03763 else
03764 decode_subband_slice_buffered(s, b, &s->sb, start_y, end_y, decode_state[level][orientation]);
03765 }
03766 }
03767 }
03768 STOP_TIMER("decode_subband_slice");
03769 }
03770
03771 { START_TIMER
03772 for(; yd<slice_h; yd+=4){
03773 ff_spatial_idwt_buffered_slice(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count, yd);
03774 }
03775 STOP_TIMER("idwt slice");}
03776
03777
03778 if(s->qlog == LOSSLESS_QLOG){
03779 for(; yq<slice_h && yq<h; yq++){
03780 DWTELEM * line = slice_buffer_get_line(&s->sb, yq);
03781 for(x=0; x<w; x++){
03782 line[x] <<= FRAC_BITS;
03783 }
03784 }
03785 }
03786
03787 predict_slice_buffered(s, &s->sb, s->spatial_dwt_buffer, plane_index, 1, mb_y);
03788
03789 y = FFMIN(p->height, slice_starty);
03790 end_y = FFMIN(p->height, slice_h);
03791 while(y < end_y)
03792 slice_buffer_release(&s->sb, y++);
03793 }
03794
03795 slice_buffer_flush(&s->sb);
03796
03797 STOP_TIMER("idwt + predict_slices")}
03798 }
03799
03800 emms_c();
03801
03802 if(s->last_picture.data[0])
03803 avctx->release_buffer(avctx, &s->last_picture);
03804
03805 if(!(s->avctx->debug&2048))
03806 *picture= s->current_picture;
03807 else
03808 *picture= s->mconly_picture;
03809
03810 *data_size = sizeof(AVFrame);
03811
03812 bytes_read= c->bytestream - c->bytestream_start;
03813 if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n");
03814
03815 return bytes_read;
03816 }
03817
03818 static int decode_end(AVCodecContext *avctx)
03819 {
03820 SnowContext *s = avctx->priv_data;
03821
03822 slice_buffer_destroy(&s->sb);
03823
03824 common_end(s);
03825
03826 return 0;
03827 }
03828
03829 AVCodec snow_decoder = {
03830 "snow",
03831 CODEC_TYPE_VIDEO,
03832 CODEC_ID_SNOW,
03833 sizeof(SnowContext),
03834 decode_init,
03835 NULL,
03836 decode_end,
03837 decode_frame,
03838 0 ,
03839 NULL
03840 };
03841
03842 #ifdef CONFIG_ENCODERS
03843 AVCodec snow_encoder = {
03844 "snow",
03845 CODEC_TYPE_VIDEO,
03846 CODEC_ID_SNOW,
03847 sizeof(SnowContext),
03848 encode_init,
03849 encode_frame,
03850 encode_end,
03851 };
03852 #endif
03853
03854
03855 #if 0
03856 #undef malloc
03857 #undef free
03858 #undef printf
03859
03860 int main(){
03861 int width=256;
03862 int height=256;
03863 int buffer[2][width*height];
03864 SnowContext s;
03865 int i;
03866 s.spatial_decomposition_count=6;
03867 s.spatial_decomposition_type=1;
03868
03869 printf("testing 5/3 DWT\n");
03870 for(i=0; i<width*height; i++)
03871 buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
03872
03873 ff_spatial_dwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
03874 ff_spatial_idwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
03875
03876 for(i=0; i<width*height; i++)
03877 if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
03878
03879 printf("testing 9/7 DWT\n");
03880 s.spatial_decomposition_type=0;
03881 for(i=0; i<width*height; i++)
03882 buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
03883
03884 ff_spatial_dwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
03885 ff_spatial_idwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
03886
03887 for(i=0; i<width*height; i++)
03888 if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
03889
03890 printf("testing AC coder\n");
03891 memset(s.header_state, 0, sizeof(s.header_state));
03892 ff_init_range_encoder(&s.c, buffer[0], 256*256);
03893 ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
03894
03895 for(i=-256; i<256; i++){
03896 START_TIMER
03897 put_symbol(&s.c, s.header_state, i*i*i/3*ABS(i), 1);
03898 STOP_TIMER("put_symbol")
03899 }
03900 ff_rac_terminate(&s.c);
03901
03902 memset(s.header_state, 0, sizeof(s.header_state));
03903 ff_init_range_decoder(&s.c, buffer[0], 256*256);
03904 ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
03905
03906 for(i=-256; i<256; i++){
03907 int j;
03908 START_TIMER
03909 j= get_symbol(&s.c, s.header_state, 1);
03910 STOP_TIMER("get_symbol")
03911 if(j!=i*i*i/3*ABS(i)) printf("fsck: %d != %d\n", i, j);
03912 }
03913 {
03914 int level, orientation, x, y;
03915 int64_t errors[8][4];
03916 int64_t g=0;
03917
03918 memset(errors, 0, sizeof(errors));
03919 s.spatial_decomposition_count=3;
03920 s.spatial_decomposition_type=0;
03921 for(level=0; level<s.spatial_decomposition_count; level++){
03922 for(orientation=level ? 1 : 0; orientation<4; orientation++){
03923 int w= width >> (s.spatial_decomposition_count-level);
03924 int h= height >> (s.spatial_decomposition_count-level);
03925 int stride= width << (s.spatial_decomposition_count-level);
03926 DWTELEM *buf= buffer[0];
03927 int64_t error=0;
03928
03929 if(orientation&1) buf+=w;
03930 if(orientation>1) buf+=stride>>1;
03931
03932 memset(buffer[0], 0, sizeof(int)*width*height);
03933 buf[w/2 + h/2*stride]= 256*256;
03934 ff_spatial_idwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
03935 for(y=0; y<height; y++){
03936 for(x=0; x<width; x++){
03937 int64_t d= buffer[0][x + y*width];
03938 error += d*d;
03939 if(ABS(width/2-x)<9 && ABS(height/2-y)<9 && level==2) printf("%8lld ", d);
03940 }
03941 if(ABS(height/2-y)<9 && level==2) printf("\n");
03942 }
03943 error= (int)(sqrt(error)+0.5);
03944 errors[level][orientation]= error;
03945 if(g) g=ff_gcd(g, error);
03946 else g= error;
03947 }
03948 }
03949 printf("static int const visual_weight[][4]={\n");
03950 for(level=0; level<s.spatial_decomposition_count; level++){
03951 printf(" {");
03952 for(orientation=0; orientation<4; orientation++){
03953 printf("%8lld,", errors[level][orientation]/g);
03954 }
03955 printf("},\n");
03956 }
03957 printf("};\n");
03958 {
03959 int level=2;
03960 int orientation=3;
03961 int w= width >> (s.spatial_decomposition_count-level);
03962 int h= height >> (s.spatial_decomposition_count-level);
03963 int stride= width << (s.spatial_decomposition_count-level);
03964 DWTELEM *buf= buffer[0];
03965 int64_t error=0;
03966
03967 buf+=w;
03968 buf+=stride>>1;
03969
03970 memset(buffer[0], 0, sizeof(int)*width*height);
03971 #if 1
03972 for(y=0; y<height; y++){
03973 for(x=0; x<width; x++){
03974 int tab[4]={0,2,3,1};
03975 buffer[0][x+width*y]= 256*256*tab[(x&1) + 2*(y&1)];
03976 }
03977 }
03978 ff_spatial_dwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
03979 #else
03980 for(y=0; y<h; y++){
03981 for(x=0; x<w; x++){
03982 buf[x + y*stride ]=169;
03983 buf[x + y*stride-w]=64;
03984 }
03985 }
03986 ff_spatial_idwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
03987 #endif
03988 for(y=0; y<height; y++){
03989 for(x=0; x<width; x++){
03990 int64_t d= buffer[0][x + y*width];
03991 error += d*d;
03992 if(ABS(width/2-x)<9 && ABS(height/2-y)<9) printf("%8lld ", d);
03993 }
03994 if(ABS(height/2-y)<9) printf("\n");
03995 }
03996 }
03997
03998 }
03999 return 0;
04000 }
04001 #endif
04002