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 qmat = (vector signed int*)s->q_intra_matrix[qscale];
00271 biasAddr = &(s->intra_quant_bias);
00272 } else {
00273 qmat = (vector signed int*)s->q_inter_matrix[qscale];
00274 biasAddr = &(s->inter_quant_bias);
00275 }
00276
00277
00278
00279 {
00280 vector signed int biasInt;
00281 const vector float negOneFloat = (vector float)FOUROF(-1.0f);
00282 LOAD4(biasInt, biasAddr);
00283 bias = vec_ctf(biasInt, QUANT_BIAS_SHIFT);
00284 negBias = vec_madd(bias, negOneFloat, zero);
00285 }
00286
00287 {
00288 vector float q0, q1, q2, q3, q4, q5, q6, q7;
00289
00290 q0 = vec_ctf(qmat[0], QMAT_SHIFT);
00291 q1 = vec_ctf(qmat[2], QMAT_SHIFT);
00292 q2 = vec_ctf(qmat[4], QMAT_SHIFT);
00293 q3 = vec_ctf(qmat[6], QMAT_SHIFT);
00294 q4 = vec_ctf(qmat[8], QMAT_SHIFT);
00295 q5 = vec_ctf(qmat[10], QMAT_SHIFT);
00296 q6 = vec_ctf(qmat[12], QMAT_SHIFT);
00297 q7 = vec_ctf(qmat[14], QMAT_SHIFT);
00298
00299 row0 = vec_sel(vec_madd(row0, q0, negBias), vec_madd(row0, q0, bias),
00300 vec_cmpgt(row0, zero));
00301 row1 = vec_sel(vec_madd(row1, q1, negBias), vec_madd(row1, q1, bias),
00302 vec_cmpgt(row1, zero));
00303 row2 = vec_sel(vec_madd(row2, q2, negBias), vec_madd(row2, q2, bias),
00304 vec_cmpgt(row2, zero));
00305 row3 = vec_sel(vec_madd(row3, q3, negBias), vec_madd(row3, q3, bias),
00306 vec_cmpgt(row3, zero));
00307 row4 = vec_sel(vec_madd(row4, q4, negBias), vec_madd(row4, q4, bias),
00308 vec_cmpgt(row4, zero));
00309 row5 = vec_sel(vec_madd(row5, q5, negBias), vec_madd(row5, q5, bias),
00310 vec_cmpgt(row5, zero));
00311 row6 = vec_sel(vec_madd(row6, q6, negBias), vec_madd(row6, q6, bias),
00312 vec_cmpgt(row6, zero));
00313 row7 = vec_sel(vec_madd(row7, q7, negBias), vec_madd(row7, q7, bias),
00314 vec_cmpgt(row7, zero));
00315
00316 q0 = vec_ctf(qmat[1], QMAT_SHIFT);
00317 q1 = vec_ctf(qmat[3], QMAT_SHIFT);
00318 q2 = vec_ctf(qmat[5], QMAT_SHIFT);
00319 q3 = vec_ctf(qmat[7], QMAT_SHIFT);
00320 q4 = vec_ctf(qmat[9], QMAT_SHIFT);
00321 q5 = vec_ctf(qmat[11], QMAT_SHIFT);
00322 q6 = vec_ctf(qmat[13], QMAT_SHIFT);
00323 q7 = vec_ctf(qmat[15], QMAT_SHIFT);
00324
00325 alt0 = vec_sel(vec_madd(alt0, q0, negBias), vec_madd(alt0, q0, bias),
00326 vec_cmpgt(alt0, zero));
00327 alt1 = vec_sel(vec_madd(alt1, q1, negBias), vec_madd(alt1, q1, bias),
00328 vec_cmpgt(alt1, zero));
00329 alt2 = vec_sel(vec_madd(alt2, q2, negBias), vec_madd(alt2, q2, bias),
00330 vec_cmpgt(alt2, zero));
00331 alt3 = vec_sel(vec_madd(alt3, q3, negBias), vec_madd(alt3, q3, bias),
00332 vec_cmpgt(alt3, zero));
00333 alt4 = vec_sel(vec_madd(alt4, q4, negBias), vec_madd(alt4, q4, bias),
00334 vec_cmpgt(alt4, zero));
00335 alt5 = vec_sel(vec_madd(alt5, q5, negBias), vec_madd(alt5, q5, bias),
00336 vec_cmpgt(alt5, zero));
00337 alt6 = vec_sel(vec_madd(alt6, q6, negBias), vec_madd(alt6, q6, bias),
00338 vec_cmpgt(alt6, zero));
00339 alt7 = vec_sel(vec_madd(alt7, q7, negBias), vec_madd(alt7, q7, bias),
00340 vec_cmpgt(alt7, zero));
00341 }
00342
00343
00344 }
00345
00346
00347 {
00348 vector signed short data0, data1, data2, data3, data4, data5, data6, data7;
00349
00350 data0 = vec_pack(vec_cts(row0, 0), vec_cts(alt0, 0));
00351 data1 = vec_pack(vec_cts(row1, 0), vec_cts(alt1, 0));
00352 data2 = vec_pack(vec_cts(row2, 0), vec_cts(alt2, 0));
00353 data3 = vec_pack(vec_cts(row3, 0), vec_cts(alt3, 0));
00354 data4 = vec_pack(vec_cts(row4, 0), vec_cts(alt4, 0));
00355 data5 = vec_pack(vec_cts(row5, 0), vec_cts(alt5, 0));
00356 data6 = vec_pack(vec_cts(row6, 0), vec_cts(alt6, 0));
00357 data7 = vec_pack(vec_cts(row7, 0), vec_cts(alt7, 0));
00358
00359 {
00360
00361 vector signed int max_q_int, min_q_int;
00362 vector signed short max_q, min_q;
00363
00364 LOAD4(max_q_int, &(s->max_qcoeff));
00365 LOAD4(min_q_int, &(s->min_qcoeff));
00366
00367 max_q = vec_pack(max_q_int, max_q_int);
00368 min_q = vec_pack(min_q_int, min_q_int);
00369
00370 data0 = vec_max(vec_min(data0, max_q), min_q);
00371 data1 = vec_max(vec_min(data1, max_q), min_q);
00372 data2 = vec_max(vec_min(data2, max_q), min_q);
00373 data4 = vec_max(vec_min(data4, max_q), min_q);
00374 data5 = vec_max(vec_min(data5, max_q), min_q);
00375 data6 = vec_max(vec_min(data6, max_q), min_q);
00376 data7 = vec_max(vec_min(data7, max_q), min_q);
00377 }
00378
00379 {
00380 vector bool char zero_01, zero_23, zero_45, zero_67;
00381 vector signed char scanIndexes_01, scanIndexes_23, scanIndexes_45, scanIndexes_67;
00382 vector signed char negOne = vec_splat_s8(-1);
00383 vector signed char* scanPtr =
00384 (vector signed char*)(s->intra_scantable.inverse);
00385 signed char lastNonZeroChar;
00386
00387
00388 zero_01 = vec_pack(vec_cmpeq(data0, (vector signed short)zero),
00389 vec_cmpeq(data1, (vector signed short)zero));
00390 zero_23 = vec_pack(vec_cmpeq(data2, (vector signed short)zero),
00391 vec_cmpeq(data3, (vector signed short)zero));
00392 zero_45 = vec_pack(vec_cmpeq(data4, (vector signed short)zero),
00393 vec_cmpeq(data5, (vector signed short)zero));
00394 zero_67 = vec_pack(vec_cmpeq(data6, (vector signed short)zero),
00395 vec_cmpeq(data7, (vector signed short)zero));
00396
00397
00398 scanIndexes_01 = vec_sel(scanPtr[0], negOne, zero_01);
00399 scanIndexes_23 = vec_sel(scanPtr[1], negOne, zero_23);
00400 scanIndexes_45 = vec_sel(scanPtr[2], negOne, zero_45);
00401 scanIndexes_67 = vec_sel(scanPtr[3], negOne, zero_67);
00402
00403
00404 scanIndexes_01 = vec_max(scanIndexes_01, scanIndexes_23);
00405 scanIndexes_45 = vec_max(scanIndexes_45, scanIndexes_67);
00406
00407
00408 scanIndexes_01 = vec_max(scanIndexes_01, scanIndexes_45);
00409
00410
00411 scanIndexes_01 = vec_max(vec_mergeh(scanIndexes_01, negOne),
00412 vec_mergel(scanIndexes_01, negOne));
00413
00414
00415 scanIndexes_01 = vec_max(vec_mergeh(scanIndexes_01, negOne),
00416 vec_mergel(scanIndexes_01, negOne));
00417
00418
00419 scanIndexes_01 = vec_max(vec_mergeh(scanIndexes_01, negOne),
00420 vec_mergel(scanIndexes_01, negOne));
00421
00422
00423 scanIndexes_01 = vec_max(vec_mergeh(scanIndexes_01, negOne),
00424 vec_mergel(scanIndexes_01, negOne));
00425
00426 scanIndexes_01 = vec_splat(scanIndexes_01, 0);
00427
00428
00429 vec_ste(scanIndexes_01, 0, &lastNonZeroChar);
00430
00431 lastNonZero = lastNonZeroChar;
00432
00433
00434
00435
00436
00437 if ((lastNonZero > 0) && (s->dsp.idct_permutation_type == FF_TRANSPOSE_IDCT_PERM)) {
00438 TRANSPOSE8(data0, data1, data2, data3, data4, data5, data6, data7);
00439 }
00440
00441 vec_st(data0, 0, data);
00442 vec_st(data1, 16, data);
00443 vec_st(data2, 32, data);
00444 vec_st(data3, 48, data);
00445 vec_st(data4, 64, data);
00446 vec_st(data5, 80, data);
00447 vec_st(data6, 96, data);
00448 vec_st(data7, 112, data);
00449 }
00450 }
00451
00452
00453 if (s->mb_intra) {
00454 if (!s->h263_aic) {
00455 if (n < 4)
00456 oldBaseValue /= s->y_dc_scale;
00457 else
00458 oldBaseValue /= s->c_dc_scale;
00459 }
00460
00461
00462 data[0] = (oldBaseValue + 4) >> 3;
00463 }
00464
00465
00466
00467 if ((lastNonZero > 0) &&
00468 (s->dsp.idct_permutation_type != FF_TRANSPOSE_IDCT_PERM) &&
00469 (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)) {
00470 ff_block_permute(data, s->dsp.idct_permutation,
00471 s->intra_scantable.scantable, lastNonZero);
00472 }
00473
00474 return lastNonZero;
00475 }
00476
00477
00478
00479 static void dct_unquantize_h263_altivec(MpegEncContext *s,
00480 DCTELEM *block, int n, int qscale)
00481 {
00482 int i, level, qmul, qadd;
00483 int nCoeffs;
00484
00485 assert(s->block_last_index[n]>=0);
00486
00487 qadd = (qscale - 1) | 1;
00488 qmul = qscale << 1;
00489
00490 if (s->mb_intra) {
00491 if (!s->h263_aic) {
00492 if (n < 4)
00493 block[0] = block[0] * s->y_dc_scale;
00494 else
00495 block[0] = block[0] * s->c_dc_scale;
00496 }else
00497 qadd = 0;
00498 i = 1;
00499 nCoeffs= 63;
00500 } else {
00501 i = 0;
00502 nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
00503 }
00504
00505 {
00506 register const vector signed short vczero = (const vector signed short)vec_splat_s16(0);
00507 DECLARE_ALIGNED(16, short, qmul8) = qmul;
00508 DECLARE_ALIGNED(16, short, qadd8) = qadd;
00509 register vector signed short blockv, qmulv, qaddv, nqaddv, temp1;
00510 register vector bool short blockv_null, blockv_neg;
00511 register short backup_0 = block[0];
00512 register int j = 0;
00513
00514 qmulv = vec_splat((vec_s16)vec_lde(0, &qmul8), 0);
00515 qaddv = vec_splat((vec_s16)vec_lde(0, &qadd8), 0);
00516 nqaddv = vec_sub(vczero, qaddv);
00517
00518 #if 0 // block *is* 16 bytes-aligned, it seems.
00519
00520 for(j = 0; (j <= nCoeffs) && ((((unsigned long)block) + (j << 1)) & 0x0000000F) ; j++) {
00521 level = block[j];
00522 if (level) {
00523 if (level < 0) {
00524 level = level * qmul - qadd;
00525 } else {
00526 level = level * qmul + qadd;
00527 }
00528 block[j] = level;
00529 }
00530 }
00531 #endif
00532
00533
00534
00535 for(; (j + 7) <= nCoeffs ; j+=8) {
00536 blockv = vec_ld(j << 1, block);
00537 blockv_neg = vec_cmplt(blockv, vczero);
00538 blockv_null = vec_cmpeq(blockv, vczero);
00539
00540 temp1 = vec_sel(qaddv, nqaddv, blockv_neg);
00541
00542 temp1 = vec_mladd(blockv, qmulv, temp1);
00543
00544 blockv = vec_sel(temp1, blockv, blockv_null);
00545 vec_st(blockv, j << 1, block);
00546 }
00547
00548
00549
00550
00551
00552 for(; j <= nCoeffs ; j++) {
00553 level = block[j];
00554 if (level) {
00555 if (level < 0) {
00556 level = level * qmul - qadd;
00557 } else {
00558 level = level * qmul + qadd;
00559 }
00560 block[j] = level;
00561 }
00562 }
00563
00564 if (i == 1) {
00565
00566 block[0] = backup_0;
00567 }
00568 }
00569 }
00570
00571
00572 void MPV_common_init_altivec(MpegEncContext *s)
00573 {
00574 if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC)) return;
00575
00576 if (s->avctx->lowres==0) {
00577 if ((s->avctx->idct_algo == FF_IDCT_AUTO) ||
00578 (s->avctx->idct_algo == FF_IDCT_ALTIVEC)) {
00579 s->dsp.idct_put = idct_put_altivec;
00580 s->dsp.idct_add = idct_add_altivec;
00581 s->dsp.idct_permutation_type = FF_TRANSPOSE_IDCT_PERM;
00582 }
00583 }
00584
00585
00586 if ((((long)(s->q_intra_matrix) & 0x0f) != 0) ||
00587 (((long)(s->q_inter_matrix) & 0x0f) != 0)) {
00588 av_log(s->avctx, AV_LOG_INFO, "Internal Error: q-matrix blocks must be 16-byte aligned "
00589 "to use AltiVec DCT. Reverting to non-AltiVec version.\n");
00590 return;
00591 }
00592
00593 if (((long)(s->intra_scantable.inverse) & 0x0f) != 0) {
00594 av_log(s->avctx, AV_LOG_INFO, "Internal Error: scan table blocks must be 16-byte aligned "
00595 "to use AltiVec DCT. Reverting to non-AltiVec version.\n");
00596 return;
00597 }
00598
00599
00600 if ((s->avctx->dct_algo == FF_DCT_AUTO) ||
00601 (s->avctx->dct_algo == FF_DCT_ALTIVEC)) {
00602 #if 0
00603 s->dct_quantize = dct_quantize_altivec;
00604 #endif
00605 s->dct_unquantize_h263_intra = dct_unquantize_h263_altivec;
00606 s->dct_unquantize_h263_inter = dct_unquantize_h263_altivec;
00607 }
00608 }