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