00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <stdlib.h>
00025 #include <stdio.h>
00026 #include "libavutil/cpu.h"
00027 #include "libavcodec/dsputil.h"
00028 #include "libavcodec/mpegvideo.h"
00029
00030 #include "util_altivec.h"
00031 #include "types_altivec.h"
00032 #include "dsputil_altivec.h"
00033
00034
00035 #define SWAP(a,b) \
00036 do { \
00037 __typeof__(a) swap_temp=a; \
00038 a=b; \
00039 b=swap_temp; \
00040 } while (0)
00041
00042
00043 #define TRANSPOSE4(a,b,c,d) \
00044 do { \
00045 __typeof__(a) _trans_ach = vec_mergeh(a, c); \
00046 __typeof__(a) _trans_acl = vec_mergel(a, c); \
00047 __typeof__(a) _trans_bdh = vec_mergeh(b, d); \
00048 __typeof__(a) _trans_bdl = vec_mergel(b, d); \
00049 \
00050 a = vec_mergeh(_trans_ach, _trans_bdh); \
00051 b = vec_mergel(_trans_ach, _trans_bdh); \
00052 c = vec_mergeh(_trans_acl, _trans_bdl); \
00053 d = vec_mergel(_trans_acl, _trans_bdl); \
00054 } while (0)
00055
00056
00057
00058
00059
00060 #define LOAD4(vec, address) \
00061 { \
00062 __typeof__(vec)* _load_addr = (__typeof__(vec)*)(address); \
00063 vector unsigned char _perm_vec = vec_lvsl(0,(address)); \
00064 vec = vec_ld(0, _load_addr); \
00065 vec = vec_perm(vec, vec, _perm_vec); \
00066 vec = vec_splat(vec, 0); \
00067 }
00068
00069
00070 #define FOUROF(a) {a,a,a,a}
00071
00072 static int dct_quantize_altivec(MpegEncContext* s,
00073 DCTELEM* data, int n,
00074 int qscale, int* overflow)
00075 {
00076 int lastNonZero;
00077 vector float row0, row1, row2, row3, row4, row5, row6, row7;
00078 vector float alt0, alt1, alt2, alt3, alt4, alt5, alt6, alt7;
00079 const vector float zero = (const vector float)FOUROF(0.);
00080
00081 int oldBaseValue = 0;
00082
00083
00084 {
00085 vector signed short data0, data1, data2, data3, data4, data5, data6, data7;
00086
00087 data0 = vec_ld(0, data);
00088 data1 = vec_ld(16, data);
00089 data2 = vec_ld(32, data);
00090 data3 = vec_ld(48, data);
00091 data4 = vec_ld(64, data);
00092 data5 = vec_ld(80, data);
00093 data6 = vec_ld(96, data);
00094 data7 = vec_ld(112, data);
00095
00096
00097 TRANSPOSE8(data0, data1, data2, data3, data4, data5, data6, data7);
00098
00099
00100
00101
00102 row0 = vec_ctf(vec_unpackh(data0), 0);
00103 alt0 = vec_ctf(vec_unpackl(data0), 0);
00104 row1 = vec_ctf(vec_unpackh(data1), 0);
00105 alt1 = vec_ctf(vec_unpackl(data1), 0);
00106 row2 = vec_ctf(vec_unpackh(data2), 0);
00107 alt2 = vec_ctf(vec_unpackl(data2), 0);
00108 row3 = vec_ctf(vec_unpackh(data3), 0);
00109 alt3 = vec_ctf(vec_unpackl(data3), 0);
00110 row4 = vec_ctf(vec_unpackh(data4), 0);
00111 alt4 = vec_ctf(vec_unpackl(data4), 0);
00112 row5 = vec_ctf(vec_unpackh(data5), 0);
00113 alt5 = vec_ctf(vec_unpackl(data5), 0);
00114 row6 = vec_ctf(vec_unpackh(data6), 0);
00115 alt6 = vec_ctf(vec_unpackl(data6), 0);
00116 row7 = vec_ctf(vec_unpackh(data7), 0);
00117 alt7 = vec_ctf(vec_unpackl(data7), 0);
00118 }
00119
00120
00121
00122
00123
00124 {
00125 const vector float vec_0_298631336 = (vector float)FOUROF(0.298631336f);
00126 const vector float vec_0_390180644 = (vector float)FOUROF(-0.390180644f);
00127 const vector float vec_0_541196100 = (vector float)FOUROF(0.541196100f);
00128 const vector float vec_0_765366865 = (vector float)FOUROF(0.765366865f);
00129 const vector float vec_0_899976223 = (vector float)FOUROF(-0.899976223f);
00130 const vector float vec_1_175875602 = (vector float)FOUROF(1.175875602f);
00131 const vector float vec_1_501321110 = (vector float)FOUROF(1.501321110f);
00132 const vector float vec_1_847759065 = (vector float)FOUROF(-1.847759065f);
00133 const vector float vec_1_961570560 = (vector float)FOUROF(-1.961570560f);
00134 const vector float vec_2_053119869 = (vector float)FOUROF(2.053119869f);
00135 const vector float vec_2_562915447 = (vector float)FOUROF(-2.562915447f);
00136 const vector float vec_3_072711026 = (vector float)FOUROF(3.072711026f);
00137
00138
00139 int whichPass, whichHalf;
00140
00141 for(whichPass = 1; whichPass<=2; whichPass++) {
00142 for(whichHalf = 1; whichHalf<=2; whichHalf++) {
00143 vector float tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
00144 vector float tmp10, tmp11, tmp12, tmp13;
00145 vector float z1, z2, z3, z4, z5;
00146
00147 tmp0 = vec_add(row0, row7);
00148 tmp7 = vec_sub(row0, row7);
00149 tmp3 = vec_add(row3, row4);
00150 tmp4 = vec_sub(row3, row4);
00151 tmp1 = vec_add(row1, row6);
00152 tmp6 = vec_sub(row1, row6);
00153 tmp2 = vec_add(row2, row5);
00154 tmp5 = vec_sub(row2, row5);
00155
00156 tmp10 = vec_add(tmp0, tmp3);
00157 tmp13 = vec_sub(tmp0, tmp3);
00158 tmp11 = vec_add(tmp1, tmp2);
00159 tmp12 = vec_sub(tmp1, tmp2);
00160
00161
00162
00163 row0 = vec_add(tmp10, tmp11);
00164
00165
00166 row4 = vec_sub(tmp10, tmp11);
00167
00168
00169
00170 z1 = vec_madd(vec_add(tmp12, tmp13), vec_0_541196100, (vector float)zero);
00171
00172
00173
00174 row2 = vec_madd(tmp13, vec_0_765366865, z1);
00175
00176
00177
00178 row6 = vec_madd(tmp12, vec_1_847759065, z1);
00179
00180 z1 = vec_add(tmp4, tmp7);
00181 z2 = vec_add(tmp5, tmp6);
00182 z3 = vec_add(tmp4, tmp6);
00183 z4 = vec_add(tmp5, tmp7);
00184
00185
00186 z5 = vec_madd(vec_add(z3, z4), vec_1_175875602, (vector float)zero);
00187
00188
00189 z3 = vec_madd(z3, vec_1_961570560, z5);
00190
00191
00192 z4 = vec_madd(z4, vec_0_390180644, z5);
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 row7 = vec_madd(tmp4, vec_0_298631336, vec_madd(z1, vec_0_899976223, z3));
00210
00211
00212
00213 row5 = vec_madd(tmp5, vec_2_053119869, vec_madd(z2, vec_2_562915447, z4));
00214
00215
00216
00217 row3 = vec_madd(tmp6, vec_3_072711026, vec_madd(z2, vec_2_562915447, z3));
00218
00219
00220
00221 row1 = vec_madd(z1, vec_0_899976223, vec_madd(tmp7, vec_1_501321110, z4));
00222
00223
00224
00225
00226
00227 SWAP(row0, alt0);
00228 SWAP(row1, alt1);
00229 SWAP(row2, alt2);
00230 SWAP(row3, alt3);
00231 SWAP(row4, alt4);
00232 SWAP(row5, alt5);
00233 SWAP(row6, alt6);
00234 SWAP(row7, alt7);
00235 }
00236
00237 if (whichPass == 1) {
00238
00239
00240
00241 SWAP(row4, alt0);
00242 SWAP(row5, alt1);
00243 SWAP(row6, alt2);
00244 SWAP(row7, alt3);
00245
00246
00247 TRANSPOSE4(row0, row1, row2, row3);
00248 TRANSPOSE4(row4, row5, row6, row7);
00249 TRANSPOSE4(alt0, alt1, alt2, alt3);
00250 TRANSPOSE4(alt4, alt5, alt6, alt7);
00251 }
00252 }
00253 }
00254
00255
00256
00257 {
00258 const int* biasAddr;
00259 const vector signed int* qmat;
00260 vector float bias, negBias;
00261
00262 if (s->mb_intra) {
00263 vector signed int baseVector;
00264
00265
00266
00267 baseVector = vec_cts(vec_splat(row0, 0), 0);
00268 vec_ste(baseVector, 0, &oldBaseValue);
00269
00270 if(n<4){
00271 qmat = (vector signed int*)s->q_intra_matrix[qscale];
00272 biasAddr = &s->intra_quant_bias;
00273 }else{
00274 qmat = (vector signed int*)s->q_chroma_intra_matrix[qscale];
00275 biasAddr = &s->intra_quant_bias;
00276 }
00277 } else {
00278 qmat = (vector signed int*)s->q_inter_matrix[qscale];
00279 biasAddr = &s->inter_quant_bias;
00280 }
00281
00282
00283
00284 {
00285 vector signed int biasInt;
00286 const vector float negOneFloat = (vector float)FOUROF(-1.0f);
00287 LOAD4(biasInt, biasAddr);
00288 bias = vec_ctf(biasInt, QUANT_BIAS_SHIFT);
00289 negBias = vec_madd(bias, negOneFloat, zero);
00290 }
00291
00292 {
00293 vector float q0, q1, q2, q3, q4, q5, q6, q7;
00294
00295 q0 = vec_ctf(qmat[0], QMAT_SHIFT);
00296 q1 = vec_ctf(qmat[2], QMAT_SHIFT);
00297 q2 = vec_ctf(qmat[4], QMAT_SHIFT);
00298 q3 = vec_ctf(qmat[6], QMAT_SHIFT);
00299 q4 = vec_ctf(qmat[8], QMAT_SHIFT);
00300 q5 = vec_ctf(qmat[10], QMAT_SHIFT);
00301 q6 = vec_ctf(qmat[12], QMAT_SHIFT);
00302 q7 = vec_ctf(qmat[14], QMAT_SHIFT);
00303
00304 row0 = vec_sel(vec_madd(row0, q0, negBias), vec_madd(row0, q0, bias),
00305 vec_cmpgt(row0, zero));
00306 row1 = vec_sel(vec_madd(row1, q1, negBias), vec_madd(row1, q1, bias),
00307 vec_cmpgt(row1, zero));
00308 row2 = vec_sel(vec_madd(row2, q2, negBias), vec_madd(row2, q2, bias),
00309 vec_cmpgt(row2, zero));
00310 row3 = vec_sel(vec_madd(row3, q3, negBias), vec_madd(row3, q3, bias),
00311 vec_cmpgt(row3, zero));
00312 row4 = vec_sel(vec_madd(row4, q4, negBias), vec_madd(row4, q4, bias),
00313 vec_cmpgt(row4, zero));
00314 row5 = vec_sel(vec_madd(row5, q5, negBias), vec_madd(row5, q5, bias),
00315 vec_cmpgt(row5, zero));
00316 row6 = vec_sel(vec_madd(row6, q6, negBias), vec_madd(row6, q6, bias),
00317 vec_cmpgt(row6, zero));
00318 row7 = vec_sel(vec_madd(row7, q7, negBias), vec_madd(row7, q7, bias),
00319 vec_cmpgt(row7, zero));
00320
00321 q0 = vec_ctf(qmat[1], QMAT_SHIFT);
00322 q1 = vec_ctf(qmat[3], QMAT_SHIFT);
00323 q2 = vec_ctf(qmat[5], QMAT_SHIFT);
00324 q3 = vec_ctf(qmat[7], QMAT_SHIFT);
00325 q4 = vec_ctf(qmat[9], QMAT_SHIFT);
00326 q5 = vec_ctf(qmat[11], QMAT_SHIFT);
00327 q6 = vec_ctf(qmat[13], QMAT_SHIFT);
00328 q7 = vec_ctf(qmat[15], QMAT_SHIFT);
00329
00330 alt0 = vec_sel(vec_madd(alt0, q0, negBias), vec_madd(alt0, q0, bias),
00331 vec_cmpgt(alt0, zero));
00332 alt1 = vec_sel(vec_madd(alt1, q1, negBias), vec_madd(alt1, q1, bias),
00333 vec_cmpgt(alt1, zero));
00334 alt2 = vec_sel(vec_madd(alt2, q2, negBias), vec_madd(alt2, q2, bias),
00335 vec_cmpgt(alt2, zero));
00336 alt3 = vec_sel(vec_madd(alt3, q3, negBias), vec_madd(alt3, q3, bias),
00337 vec_cmpgt(alt3, zero));
00338 alt4 = vec_sel(vec_madd(alt4, q4, negBias), vec_madd(alt4, q4, bias),
00339 vec_cmpgt(alt4, zero));
00340 alt5 = vec_sel(vec_madd(alt5, q5, negBias), vec_madd(alt5, q5, bias),
00341 vec_cmpgt(alt5, zero));
00342 alt6 = vec_sel(vec_madd(alt6, q6, negBias), vec_madd(alt6, q6, bias),
00343 vec_cmpgt(alt6, zero));
00344 alt7 = vec_sel(vec_madd(alt7, q7, negBias), vec_madd(alt7, q7, bias),
00345 vec_cmpgt(alt7, zero));
00346 }
00347
00348
00349 }
00350
00351
00352 {
00353 vector signed short data0, data1, data2, data3, data4, data5, data6, data7;
00354
00355 data0 = vec_pack(vec_cts(row0, 0), vec_cts(alt0, 0));
00356 data1 = vec_pack(vec_cts(row1, 0), vec_cts(alt1, 0));
00357 data2 = vec_pack(vec_cts(row2, 0), vec_cts(alt2, 0));
00358 data3 = vec_pack(vec_cts(row3, 0), vec_cts(alt3, 0));
00359 data4 = vec_pack(vec_cts(row4, 0), vec_cts(alt4, 0));
00360 data5 = vec_pack(vec_cts(row5, 0), vec_cts(alt5, 0));
00361 data6 = vec_pack(vec_cts(row6, 0), vec_cts(alt6, 0));
00362 data7 = vec_pack(vec_cts(row7, 0), vec_cts(alt7, 0));
00363
00364 {
00365
00366 vector signed int max_q_int, min_q_int;
00367 vector signed short max_q, min_q;
00368
00369 LOAD4(max_q_int, &s->max_qcoeff);
00370 LOAD4(min_q_int, &s->min_qcoeff);
00371
00372 max_q = vec_pack(max_q_int, max_q_int);
00373 min_q = vec_pack(min_q_int, min_q_int);
00374
00375 data0 = vec_max(vec_min(data0, max_q), min_q);
00376 data1 = vec_max(vec_min(data1, max_q), min_q);
00377 data2 = vec_max(vec_min(data2, max_q), min_q);
00378 data4 = vec_max(vec_min(data4, max_q), min_q);
00379 data5 = vec_max(vec_min(data5, max_q), min_q);
00380 data6 = vec_max(vec_min(data6, max_q), min_q);
00381 data7 = vec_max(vec_min(data7, max_q), min_q);
00382 }
00383
00384 {
00385 vector bool char zero_01, zero_23, zero_45, zero_67;
00386 vector signed char scanIndexes_01, scanIndexes_23, scanIndexes_45, scanIndexes_67;
00387 vector signed char negOne = vec_splat_s8(-1);
00388 vector signed char* scanPtr =
00389 (vector signed char*)(s->intra_scantable.inverse);
00390 signed char lastNonZeroChar;
00391
00392
00393 zero_01 = vec_pack(vec_cmpeq(data0, (vector signed short)zero),
00394 vec_cmpeq(data1, (vector signed short)zero));
00395 zero_23 = vec_pack(vec_cmpeq(data2, (vector signed short)zero),
00396 vec_cmpeq(data3, (vector signed short)zero));
00397 zero_45 = vec_pack(vec_cmpeq(data4, (vector signed short)zero),
00398 vec_cmpeq(data5, (vector signed short)zero));
00399 zero_67 = vec_pack(vec_cmpeq(data6, (vector signed short)zero),
00400 vec_cmpeq(data7, (vector signed short)zero));
00401
00402
00403 scanIndexes_01 = vec_sel(scanPtr[0], negOne, zero_01);
00404 scanIndexes_23 = vec_sel(scanPtr[1], negOne, zero_23);
00405 scanIndexes_45 = vec_sel(scanPtr[2], negOne, zero_45);
00406 scanIndexes_67 = vec_sel(scanPtr[3], negOne, zero_67);
00407
00408
00409 scanIndexes_01 = vec_max(scanIndexes_01, scanIndexes_23);
00410 scanIndexes_45 = vec_max(scanIndexes_45, scanIndexes_67);
00411
00412
00413 scanIndexes_01 = vec_max(scanIndexes_01, scanIndexes_45);
00414
00415
00416 scanIndexes_01 = vec_max(vec_mergeh(scanIndexes_01, negOne),
00417 vec_mergel(scanIndexes_01, negOne));
00418
00419
00420 scanIndexes_01 = vec_max(vec_mergeh(scanIndexes_01, negOne),
00421 vec_mergel(scanIndexes_01, negOne));
00422
00423
00424 scanIndexes_01 = vec_max(vec_mergeh(scanIndexes_01, negOne),
00425 vec_mergel(scanIndexes_01, negOne));
00426
00427
00428 scanIndexes_01 = vec_max(vec_mergeh(scanIndexes_01, negOne),
00429 vec_mergel(scanIndexes_01, negOne));
00430
00431 scanIndexes_01 = vec_splat(scanIndexes_01, 0);
00432
00433
00434 vec_ste(scanIndexes_01, 0, &lastNonZeroChar);
00435
00436 lastNonZero = lastNonZeroChar;
00437
00438
00439
00440
00441
00442 if ((lastNonZero > 0) && (s->dsp.idct_permutation_type == FF_TRANSPOSE_IDCT_PERM)) {
00443 TRANSPOSE8(data0, data1, data2, data3, data4, data5, data6, data7);
00444 }
00445
00446 vec_st(data0, 0, data);
00447 vec_st(data1, 16, data);
00448 vec_st(data2, 32, data);
00449 vec_st(data3, 48, data);
00450 vec_st(data4, 64, data);
00451 vec_st(data5, 80, data);
00452 vec_st(data6, 96, data);
00453 vec_st(data7, 112, data);
00454 }
00455 }
00456
00457
00458 if (s->mb_intra) {
00459 if (!s->h263_aic) {
00460 if (n < 4)
00461 oldBaseValue /= s->y_dc_scale;
00462 else
00463 oldBaseValue /= s->c_dc_scale;
00464 }
00465
00466
00467 data[0] = (oldBaseValue + 4) >> 3;
00468 }
00469
00470
00471
00472 if ((lastNonZero > 0) &&
00473 (s->dsp.idct_permutation_type != FF_TRANSPOSE_IDCT_PERM) &&
00474 (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)) {
00475 ff_block_permute(data, s->dsp.idct_permutation,
00476 s->intra_scantable.scantable, lastNonZero);
00477 }
00478
00479 return lastNonZero;
00480 }
00481
00482
00483
00484 static void dct_unquantize_h263_altivec(MpegEncContext *s,
00485 DCTELEM *block, int n, int qscale)
00486 {
00487 int i, level, qmul, qadd;
00488 int nCoeffs;
00489
00490 assert(s->block_last_index[n]>=0);
00491
00492 qadd = (qscale - 1) | 1;
00493 qmul = qscale << 1;
00494
00495 if (s->mb_intra) {
00496 if (!s->h263_aic) {
00497 if (n < 4)
00498 block[0] = block[0] * s->y_dc_scale;
00499 else
00500 block[0] = block[0] * s->c_dc_scale;
00501 }else
00502 qadd = 0;
00503 i = 1;
00504 nCoeffs= 63;
00505 } else {
00506 i = 0;
00507 nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
00508 }
00509
00510 {
00511 register const vector signed short vczero = (const vector signed short)vec_splat_s16(0);
00512 DECLARE_ALIGNED(16, short, qmul8) = qmul;
00513 DECLARE_ALIGNED(16, short, qadd8) = qadd;
00514 register vector signed short blockv, qmulv, qaddv, nqaddv, temp1;
00515 register vector bool short blockv_null, blockv_neg;
00516 register short backup_0 = block[0];
00517 register int j = 0;
00518
00519 qmulv = vec_splat((vec_s16)vec_lde(0, &qmul8), 0);
00520 qaddv = vec_splat((vec_s16)vec_lde(0, &qadd8), 0);
00521 nqaddv = vec_sub(vczero, qaddv);
00522
00523
00524
00525 for(; (j + 7) <= nCoeffs ; j+=8) {
00526 blockv = vec_ld(j << 1, block);
00527 blockv_neg = vec_cmplt(blockv, vczero);
00528 blockv_null = vec_cmpeq(blockv, vczero);
00529
00530 temp1 = vec_sel(qaddv, nqaddv, blockv_neg);
00531
00532 temp1 = vec_mladd(blockv, qmulv, temp1);
00533
00534 blockv = vec_sel(temp1, blockv, blockv_null);
00535 vec_st(blockv, j << 1, block);
00536 }
00537
00538
00539
00540
00541
00542 for(; j <= nCoeffs ; j++) {
00543 level = block[j];
00544 if (level) {
00545 if (level < 0) {
00546 level = level * qmul - qadd;
00547 } else {
00548 level = level * qmul + qadd;
00549 }
00550 block[j] = level;
00551 }
00552 }
00553
00554 if (i == 1) {
00555
00556 block[0] = backup_0;
00557 }
00558 }
00559 }
00560
00561
00562 void MPV_common_init_altivec(MpegEncContext *s)
00563 {
00564 if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC)) return;
00565
00566
00567 if ((((long)(s->q_intra_matrix) & 0x0f) != 0) ||
00568 (((long)(s->q_inter_matrix) & 0x0f) != 0)) {
00569 av_log(s->avctx, AV_LOG_INFO, "Internal Error: q-matrix blocks must be 16-byte aligned "
00570 "to use AltiVec DCT. Reverting to non-AltiVec version.\n");
00571 return;
00572 }
00573
00574 if (((long)(s->intra_scantable.inverse) & 0x0f) != 0) {
00575 av_log(s->avctx, AV_LOG_INFO, "Internal Error: scan table blocks must be 16-byte aligned "
00576 "to use AltiVec DCT. Reverting to non-AltiVec version.\n");
00577 return;
00578 }
00579
00580
00581 if ((s->avctx->dct_algo == FF_DCT_AUTO) ||
00582 (s->avctx->dct_algo == FF_DCT_ALTIVEC)) {
00583 s->dct_unquantize_h263_intra = dct_unquantize_h263_altivec;
00584 s->dct_unquantize_h263_inter = dct_unquantize_h263_altivec;
00585 }
00586 }