00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #undef MMREG_WIDTH
00024 #undef MM
00025 #undef MOVQ
00026 #undef SPREADW
00027 #undef PMAXW
00028 #undef PMAX
00029 #undef SAVE_SIGN
00030 #undef RESTORE_SIGN
00031
00032 #if HAVE_SSE2
00033 #define MMREG_WIDTH "16"
00034 #define MM "%%xmm"
00035 #define MOVQ "movdqa"
00036 #define SPREADW(a) \
00037 "pshuflw $0, "a", "a" \n\t"\
00038 "punpcklwd "a", "a" \n\t"
00039 #define PMAXW(a,b) "pmaxsw "a", "b" \n\t"
00040 #define PMAX(a,b) \
00041 "movhlps "a", "b" \n\t"\
00042 PMAXW(b, a)\
00043 "pshuflw $0x0E, "a", "b" \n\t"\
00044 PMAXW(b, a)\
00045 "pshuflw $0x01, "a", "b" \n\t"\
00046 PMAXW(b, a)
00047 #else
00048 #define MMREG_WIDTH "8"
00049 #define MM "%%mm"
00050 #define MOVQ "movq"
00051 #if HAVE_MMX2
00052 #define SPREADW(a) "pshufw $0, "a", "a" \n\t"
00053 #define PMAXW(a,b) "pmaxsw "a", "b" \n\t"
00054 #define PMAX(a,b) \
00055 "pshufw $0x0E, "a", "b" \n\t"\
00056 PMAXW(b, a)\
00057 "pshufw $0x01, "a", "b" \n\t"\
00058 PMAXW(b, a)
00059 #else
00060 #define SPREADW(a) \
00061 "punpcklwd "a", "a" \n\t"\
00062 "punpcklwd "a", "a" \n\t"
00063 #define PMAXW(a,b) \
00064 "psubusw "a", "b" \n\t"\
00065 "paddw "a", "b" \n\t"
00066 #define PMAX(a,b) \
00067 "movq "a", "b" \n\t"\
00068 "psrlq $32, "a" \n\t"\
00069 PMAXW(b, a)\
00070 "movq "a", "b" \n\t"\
00071 "psrlq $16, "a" \n\t"\
00072 PMAXW(b, a)
00073
00074 #endif
00075 #endif
00076
00077 #if HAVE_SSSE3
00078 #define SAVE_SIGN(a,b) \
00079 "movdqa "b", "a" \n\t"\
00080 "pabsw "b", "b" \n\t"
00081 #define RESTORE_SIGN(a,b) \
00082 "psignw "a", "b" \n\t"
00083 #else
00084 #define SAVE_SIGN(a,b) \
00085 "pxor "a", "a" \n\t"\
00086 "pcmpgtw "b", "a" \n\t" \
00087 "pxor "a", "b" \n\t"\
00088 "psubw "a", "b" \n\t"
00089 #define RESTORE_SIGN(a,b) \
00090 "pxor "a", "b" \n\t"\
00091 "psubw "a", "b" \n\t" // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i])
00092 #endif
00093
00094 static int RENAME(dct_quantize)(MpegEncContext *s,
00095 DCTELEM *block, int n,
00096 int qscale, int *overflow)
00097 {
00098 x86_reg last_non_zero_p1;
00099 int level=0, q;
00100 const uint16_t *qmat, *bias;
00101 DECLARE_ALIGNED(16, int16_t, temp_block)[64];
00102
00103 assert((7&(int)(&temp_block[0])) == 0);
00104
00105
00106 RENAMEl(ff_fdct) (block);
00107
00108 if(s->dct_error_sum)
00109 s->denoise_dct(s, block);
00110
00111 if (s->mb_intra) {
00112 int dummy;
00113 if (n < 4)
00114 q = s->y_dc_scale;
00115 else
00116 q = s->c_dc_scale;
00117
00118 if (!s->h263_aic) {
00119 #if 1
00120 __asm__ volatile (
00121 "mul %%ecx \n\t"
00122 : "=d" (level), "=a"(dummy)
00123 : "a" ((block[0]>>2) + q), "c" (ff_inverse[q<<1])
00124 );
00125 #else
00126 __asm__ volatile (
00127 "xorl %%edx, %%edx \n\t"
00128 "divw %%cx \n\t"
00129 "movzwl %%ax, %%eax \n\t"
00130 : "=a" (level)
00131 : "a" ((block[0]>>2) + q), "c" (q<<1)
00132 : "%edx"
00133 );
00134 #endif
00135 } else
00136
00137 level = (block[0] + 4)>>3;
00138
00139 block[0]=0;
00140
00141 last_non_zero_p1 = 1;
00142 bias = s->q_intra_matrix16[qscale][1];
00143 qmat = s->q_intra_matrix16[qscale][0];
00144 } else {
00145 last_non_zero_p1 = 0;
00146 bias = s->q_inter_matrix16[qscale][1];
00147 qmat = s->q_inter_matrix16[qscale][0];
00148 }
00149
00150 if((s->out_format == FMT_H263 || s->out_format == FMT_H261) && s->mpeg_quant==0){
00151
00152 __asm__ volatile(
00153 "movd %%"REG_a", "MM"3 \n\t"
00154 SPREADW(MM"3")
00155 "pxor "MM"7, "MM"7 \n\t"
00156 "pxor "MM"4, "MM"4 \n\t"
00157 MOVQ" (%2), "MM"5 \n\t"
00158 "pxor "MM"6, "MM"6 \n\t"
00159 "psubw (%3), "MM"6 \n\t"
00160 "mov $-128, %%"REG_a" \n\t"
00161 ASMALIGN(4)
00162 "1: \n\t"
00163 MOVQ" (%1, %%"REG_a"), "MM"0 \n\t"
00164 SAVE_SIGN(MM"1", MM"0")
00165 "psubusw "MM"6, "MM"0 \n\t"
00166 "pmulhw "MM"5, "MM"0 \n\t"
00167 "por "MM"0, "MM"4 \n\t"
00168 RESTORE_SIGN(MM"1", MM"0")
00169 MOVQ" "MM"0, (%5, %%"REG_a") \n\t"
00170 "pcmpeqw "MM"7, "MM"0 \n\t"
00171 MOVQ" (%4, %%"REG_a"), "MM"1 \n\t"
00172 MOVQ" "MM"7, (%1, %%"REG_a") \n\t"
00173 "pandn "MM"1, "MM"0 \n\t"
00174 PMAXW(MM"0", MM"3")
00175 "add $"MMREG_WIDTH", %%"REG_a" \n\t"
00176 " js 1b \n\t"
00177 PMAX(MM"3", MM"0")
00178 "movd "MM"3, %%"REG_a" \n\t"
00179 "movzb %%al, %%"REG_a" \n\t"
00180 : "+a" (last_non_zero_p1)
00181 : "r" (block+64), "r" (qmat), "r" (bias),
00182 "r" (inv_zigzag_direct16+64), "r" (temp_block+64)
00183 );
00184 }else{
00185 __asm__ volatile(
00186 "movd %%"REG_a", "MM"3 \n\t"
00187 SPREADW(MM"3")
00188 "pxor "MM"7, "MM"7 \n\t"
00189 "pxor "MM"4, "MM"4 \n\t"
00190 "mov $-128, %%"REG_a" \n\t"
00191 ASMALIGN(4)
00192 "1: \n\t"
00193 MOVQ" (%1, %%"REG_a"), "MM"0 \n\t"
00194 SAVE_SIGN(MM"1", MM"0")
00195 MOVQ" (%3, %%"REG_a"), "MM"6 \n\t"
00196 "paddusw "MM"6, "MM"0 \n\t"
00197 MOVQ" (%2, %%"REG_a"), "MM"5 \n\t"
00198 "pmulhw "MM"5, "MM"0 \n\t"
00199 "por "MM"0, "MM"4 \n\t"
00200 RESTORE_SIGN(MM"1", MM"0")
00201 MOVQ" "MM"0, (%5, %%"REG_a") \n\t"
00202 "pcmpeqw "MM"7, "MM"0 \n\t"
00203 MOVQ" (%4, %%"REG_a"), "MM"1 \n\t"
00204 MOVQ" "MM"7, (%1, %%"REG_a") \n\t"
00205 "pandn "MM"1, "MM"0 \n\t"
00206 PMAXW(MM"0", MM"3")
00207 "add $"MMREG_WIDTH", %%"REG_a" \n\t"
00208 " js 1b \n\t"
00209 PMAX(MM"3", MM"0")
00210 "movd "MM"3, %%"REG_a" \n\t"
00211 "movzb %%al, %%"REG_a" \n\t"
00212 : "+a" (last_non_zero_p1)
00213 : "r" (block+64), "r" (qmat+64), "r" (bias+64),
00214 "r" (inv_zigzag_direct16+64), "r" (temp_block+64)
00215 );
00216 }
00217 __asm__ volatile(
00218 "movd %1, "MM"1 \n\t"
00219 SPREADW(MM"1")
00220 "psubusw "MM"1, "MM"4 \n\t"
00221 "packuswb "MM"4, "MM"4 \n\t"
00222 #if HAVE_SSE2
00223 "packuswb "MM"4, "MM"4 \n\t"
00224 #endif
00225 "movd "MM"4, %0 \n\t"
00226 : "=g" (*overflow)
00227 : "g" (s->max_qcoeff)
00228 );
00229
00230 if(s->mb_intra) block[0]= level;
00231 else block[0]= temp_block[0];
00232
00233 if(s->dsp.idct_permutation_type == FF_SIMPLE_IDCT_PERM){
00234 if(last_non_zero_p1 <= 1) goto end;
00235 block[0x08] = temp_block[0x01]; block[0x10] = temp_block[0x08];
00236 block[0x20] = temp_block[0x10];
00237 if(last_non_zero_p1 <= 4) goto end;
00238 block[0x18] = temp_block[0x09]; block[0x04] = temp_block[0x02];
00239 block[0x09] = temp_block[0x03];
00240 if(last_non_zero_p1 <= 7) goto end;
00241 block[0x14] = temp_block[0x0A]; block[0x28] = temp_block[0x11];
00242 block[0x12] = temp_block[0x18]; block[0x02] = temp_block[0x20];
00243 if(last_non_zero_p1 <= 11) goto end;
00244 block[0x1A] = temp_block[0x19]; block[0x24] = temp_block[0x12];
00245 block[0x19] = temp_block[0x0B]; block[0x01] = temp_block[0x04];
00246 block[0x0C] = temp_block[0x05];
00247 if(last_non_zero_p1 <= 16) goto end;
00248 block[0x11] = temp_block[0x0C]; block[0x29] = temp_block[0x13];
00249 block[0x16] = temp_block[0x1A]; block[0x0A] = temp_block[0x21];
00250 block[0x30] = temp_block[0x28]; block[0x22] = temp_block[0x30];
00251 block[0x38] = temp_block[0x29]; block[0x06] = temp_block[0x22];
00252 if(last_non_zero_p1 <= 24) goto end;
00253 block[0x1B] = temp_block[0x1B]; block[0x21] = temp_block[0x14];
00254 block[0x1C] = temp_block[0x0D]; block[0x05] = temp_block[0x06];
00255 block[0x0D] = temp_block[0x07]; block[0x15] = temp_block[0x0E];
00256 block[0x2C] = temp_block[0x15]; block[0x13] = temp_block[0x1C];
00257 if(last_non_zero_p1 <= 32) goto end;
00258 block[0x0B] = temp_block[0x23]; block[0x34] = temp_block[0x2A];
00259 block[0x2A] = temp_block[0x31]; block[0x32] = temp_block[0x38];
00260 block[0x3A] = temp_block[0x39]; block[0x26] = temp_block[0x32];
00261 block[0x39] = temp_block[0x2B]; block[0x03] = temp_block[0x24];
00262 if(last_non_zero_p1 <= 40) goto end;
00263 block[0x1E] = temp_block[0x1D]; block[0x25] = temp_block[0x16];
00264 block[0x1D] = temp_block[0x0F]; block[0x2D] = temp_block[0x17];
00265 block[0x17] = temp_block[0x1E]; block[0x0E] = temp_block[0x25];
00266 block[0x31] = temp_block[0x2C]; block[0x2B] = temp_block[0x33];
00267 if(last_non_zero_p1 <= 48) goto end;
00268 block[0x36] = temp_block[0x3A]; block[0x3B] = temp_block[0x3B];
00269 block[0x23] = temp_block[0x34]; block[0x3C] = temp_block[0x2D];
00270 block[0x07] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
00271 block[0x0F] = temp_block[0x27]; block[0x35] = temp_block[0x2E];
00272 if(last_non_zero_p1 <= 56) goto end;
00273 block[0x2E] = temp_block[0x35]; block[0x33] = temp_block[0x3C];
00274 block[0x3E] = temp_block[0x3D]; block[0x27] = temp_block[0x36];
00275 block[0x3D] = temp_block[0x2F]; block[0x2F] = temp_block[0x37];
00276 block[0x37] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
00277 }else if(s->dsp.idct_permutation_type == FF_LIBMPEG2_IDCT_PERM){
00278 if(last_non_zero_p1 <= 1) goto end;
00279 block[0x04] = temp_block[0x01];
00280 block[0x08] = temp_block[0x08]; block[0x10] = temp_block[0x10];
00281 if(last_non_zero_p1 <= 4) goto end;
00282 block[0x0C] = temp_block[0x09]; block[0x01] = temp_block[0x02];
00283 block[0x05] = temp_block[0x03];
00284 if(last_non_zero_p1 <= 7) goto end;
00285 block[0x09] = temp_block[0x0A]; block[0x14] = temp_block[0x11];
00286 block[0x18] = temp_block[0x18]; block[0x20] = temp_block[0x20];
00287 if(last_non_zero_p1 <= 11) goto end;
00288 block[0x1C] = temp_block[0x19];
00289 block[0x11] = temp_block[0x12]; block[0x0D] = temp_block[0x0B];
00290 block[0x02] = temp_block[0x04]; block[0x06] = temp_block[0x05];
00291 if(last_non_zero_p1 <= 16) goto end;
00292 block[0x0A] = temp_block[0x0C]; block[0x15] = temp_block[0x13];
00293 block[0x19] = temp_block[0x1A]; block[0x24] = temp_block[0x21];
00294 block[0x28] = temp_block[0x28]; block[0x30] = temp_block[0x30];
00295 block[0x2C] = temp_block[0x29]; block[0x21] = temp_block[0x22];
00296 if(last_non_zero_p1 <= 24) goto end;
00297 block[0x1D] = temp_block[0x1B]; block[0x12] = temp_block[0x14];
00298 block[0x0E] = temp_block[0x0D]; block[0x03] = temp_block[0x06];
00299 block[0x07] = temp_block[0x07]; block[0x0B] = temp_block[0x0E];
00300 block[0x16] = temp_block[0x15]; block[0x1A] = temp_block[0x1C];
00301 if(last_non_zero_p1 <= 32) goto end;
00302 block[0x25] = temp_block[0x23]; block[0x29] = temp_block[0x2A];
00303 block[0x34] = temp_block[0x31]; block[0x38] = temp_block[0x38];
00304 block[0x3C] = temp_block[0x39]; block[0x31] = temp_block[0x32];
00305 block[0x2D] = temp_block[0x2B]; block[0x22] = temp_block[0x24];
00306 if(last_non_zero_p1 <= 40) goto end;
00307 block[0x1E] = temp_block[0x1D]; block[0x13] = temp_block[0x16];
00308 block[0x0F] = temp_block[0x0F]; block[0x17] = temp_block[0x17];
00309 block[0x1B] = temp_block[0x1E]; block[0x26] = temp_block[0x25];
00310 block[0x2A] = temp_block[0x2C]; block[0x35] = temp_block[0x33];
00311 if(last_non_zero_p1 <= 48) goto end;
00312 block[0x39] = temp_block[0x3A]; block[0x3D] = temp_block[0x3B];
00313 block[0x32] = temp_block[0x34]; block[0x2E] = temp_block[0x2D];
00314 block[0x23] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
00315 block[0x27] = temp_block[0x27]; block[0x2B] = temp_block[0x2E];
00316 if(last_non_zero_p1 <= 56) goto end;
00317 block[0x36] = temp_block[0x35]; block[0x3A] = temp_block[0x3C];
00318 block[0x3E] = temp_block[0x3D]; block[0x33] = temp_block[0x36];
00319 block[0x2F] = temp_block[0x2F]; block[0x37] = temp_block[0x37];
00320 block[0x3B] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
00321 }else{
00322 if(last_non_zero_p1 <= 1) goto end;
00323 block[0x01] = temp_block[0x01];
00324 block[0x08] = temp_block[0x08]; block[0x10] = temp_block[0x10];
00325 if(last_non_zero_p1 <= 4) goto end;
00326 block[0x09] = temp_block[0x09]; block[0x02] = temp_block[0x02];
00327 block[0x03] = temp_block[0x03];
00328 if(last_non_zero_p1 <= 7) goto end;
00329 block[0x0A] = temp_block[0x0A]; block[0x11] = temp_block[0x11];
00330 block[0x18] = temp_block[0x18]; block[0x20] = temp_block[0x20];
00331 if(last_non_zero_p1 <= 11) goto end;
00332 block[0x19] = temp_block[0x19];
00333 block[0x12] = temp_block[0x12]; block[0x0B] = temp_block[0x0B];
00334 block[0x04] = temp_block[0x04]; block[0x05] = temp_block[0x05];
00335 if(last_non_zero_p1 <= 16) goto end;
00336 block[0x0C] = temp_block[0x0C]; block[0x13] = temp_block[0x13];
00337 block[0x1A] = temp_block[0x1A]; block[0x21] = temp_block[0x21];
00338 block[0x28] = temp_block[0x28]; block[0x30] = temp_block[0x30];
00339 block[0x29] = temp_block[0x29]; block[0x22] = temp_block[0x22];
00340 if(last_non_zero_p1 <= 24) goto end;
00341 block[0x1B] = temp_block[0x1B]; block[0x14] = temp_block[0x14];
00342 block[0x0D] = temp_block[0x0D]; block[0x06] = temp_block[0x06];
00343 block[0x07] = temp_block[0x07]; block[0x0E] = temp_block[0x0E];
00344 block[0x15] = temp_block[0x15]; block[0x1C] = temp_block[0x1C];
00345 if(last_non_zero_p1 <= 32) goto end;
00346 block[0x23] = temp_block[0x23]; block[0x2A] = temp_block[0x2A];
00347 block[0x31] = temp_block[0x31]; block[0x38] = temp_block[0x38];
00348 block[0x39] = temp_block[0x39]; block[0x32] = temp_block[0x32];
00349 block[0x2B] = temp_block[0x2B]; block[0x24] = temp_block[0x24];
00350 if(last_non_zero_p1 <= 40) goto end;
00351 block[0x1D] = temp_block[0x1D]; block[0x16] = temp_block[0x16];
00352 block[0x0F] = temp_block[0x0F]; block[0x17] = temp_block[0x17];
00353 block[0x1E] = temp_block[0x1E]; block[0x25] = temp_block[0x25];
00354 block[0x2C] = temp_block[0x2C]; block[0x33] = temp_block[0x33];
00355 if(last_non_zero_p1 <= 48) goto end;
00356 block[0x3A] = temp_block[0x3A]; block[0x3B] = temp_block[0x3B];
00357 block[0x34] = temp_block[0x34]; block[0x2D] = temp_block[0x2D];
00358 block[0x26] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
00359 block[0x27] = temp_block[0x27]; block[0x2E] = temp_block[0x2E];
00360 if(last_non_zero_p1 <= 56) goto end;
00361 block[0x35] = temp_block[0x35]; block[0x3C] = temp_block[0x3C];
00362 block[0x3D] = temp_block[0x3D]; block[0x36] = temp_block[0x36];
00363 block[0x2F] = temp_block[0x2F]; block[0x37] = temp_block[0x37];
00364 block[0x3E] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
00365 }
00366 end:
00367
00368
00369
00370
00371
00372
00373
00374
00375 return last_non_zero_p1 - 1;
00376 }