mpegvideo_mmx_template.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:16k
源码类别:

Windows CE

开发平台:

C/C++

  1. /*
  2.  * MPEG video MMX templates
  3.  *
  4.  * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  */
  20. #undef SPREADW
  21. #undef PMAXW
  22. #ifdef HAVE_MMX2
  23. #define SPREADW(a) "pshufw $0, " #a ", " #a " nt"
  24. #define PMAXW(a,b) "pmaxsw " #a ", " #b " nt"
  25. #else
  26. #define SPREADW(a) 
  27. "punpcklwd " #a ", " #a " nt"
  28. "punpcklwd " #a ", " #a " nt"
  29. #define PMAXW(a,b) 
  30. "psubusw " #a ", " #b " nt"
  31. "paddw " #a ", " #b " nt"
  32. #endif
  33. static int RENAME(dct_quantize)(MpegEncContext *s,
  34.                             DCTELEM *block, int n,
  35.                             int qscale, int *overflow)
  36. {
  37.     long last_non_zero_p1;
  38.     int level=0, q; //=0 is cuz gcc says uninitalized ...
  39.     const uint16_t *qmat, *bias;
  40.     __align8 int16_t temp_block[64];
  41.     
  42.     assert((7&(int)(&temp_block[0])) == 0); //did gcc align it correctly?
  43.     //s->fdct (block);
  44.     RENAMEl(ff_fdct) (block); //cant be anything else ...
  45.     if(s->dct_error_sum)
  46.         s->denoise_dct(s, block);
  47.     if (s->mb_intra) {
  48.         int dummy;
  49.         if (n < 4)
  50.             q = s->y_dc_scale;
  51.         else
  52.             q = s->c_dc_scale;
  53.         /* note: block[0] is assumed to be positive */
  54.         if (!s->h263_aic) {
  55. #if 1
  56.         asm volatile (
  57.          "mul %%ecx nt"
  58.          : "=d" (level), "=a"(dummy)
  59.          : "a" ((block[0]>>2) + q), "c" (inverse[q<<1])
  60.         );
  61. #else
  62.         asm volatile (
  63.          "xorl %%edx, %%edx nt"
  64.          "divw %%cx nt"
  65.          "movzwl %%ax, %%eax nt"
  66.          : "=a" (level)
  67.          : "a" ((block[0]>>2) + q), "c" (q<<1)
  68.          : "%edx"
  69.         );
  70. #endif
  71.         } else
  72.             /* For AIC we skip quant/dequant of INTRADC */
  73.             level = (block[0] + 4)>>3;
  74.             
  75.         block[0]=0; //avoid fake overflow
  76. //        temp_block[0] = (block[0] + (q >> 1)) / q;
  77.         last_non_zero_p1 = 1;
  78.         bias = s->q_intra_matrix16[qscale][1];
  79.         qmat = s->q_intra_matrix16[qscale][0];
  80.     } else {
  81.         last_non_zero_p1 = 0;
  82.         bias = s->q_inter_matrix16[qscale][1];
  83.         qmat = s->q_inter_matrix16[qscale][0];
  84.     }
  85.     if((s->out_format == FMT_H263 || s->out_format == FMT_H261) && s->mpeg_quant==0){
  86.     
  87.         asm volatile(
  88.             "movd %%"REG_a", %%mm3 nt" // last_non_zero_p1
  89.             SPREADW(%%mm3)
  90.             "pxor %%mm7, %%mm7 nt" // 0
  91.             "pxor %%mm4, %%mm4 nt" // 0
  92.             "movq (%2), %%mm5 nt" // qmat[0]
  93.             "pxor %%mm6, %%mm6 nt"
  94.             "psubw (%3), %%mm6 nt" // -bias[0]
  95.             "mov $-128, %%"REG_a" nt"
  96.             ".balign 16 nt"
  97.             "1: nt"
  98.             "pxor %%mm1, %%mm1 nt" // 0
  99.             "movq (%1, %%"REG_a"), %%mm0 nt" // block[i]
  100.             "pcmpgtw %%mm0, %%mm1 nt" // block[i] <= 0 ? 0xFF : 0x00
  101.             "pxor %%mm1, %%mm0 nt" 
  102.             "psubw %%mm1, %%mm0 nt" // ABS(block[i])
  103.             "psubusw %%mm6, %%mm0 nt" // ABS(block[i]) + bias[0]
  104.             "pmulhw %%mm5, %%mm0 nt" // (ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16
  105.             "por %%mm0, %%mm4 nt" 
  106.             "pxor %%mm1, %%mm0 nt" 
  107.             "psubw %%mm1, %%mm0 nt" // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i])
  108.             "movq %%mm0, (%5, %%"REG_a") nt"
  109.             "pcmpeqw %%mm7, %%mm0 nt" // out==0 ? 0xFF : 0x00
  110.             "movq (%4, %%"REG_a"), %%mm1 nt" 
  111.             "movq %%mm7, (%1, %%"REG_a") nt" // 0
  112.             "pandn %%mm1, %%mm0 nt"
  113.     PMAXW(%%mm0, %%mm3)
  114.             "add $8, %%"REG_a" nt"
  115.             " js 1b nt"
  116.             "movq %%mm3, %%mm0 nt"
  117.             "psrlq $32, %%mm3 nt"
  118.     PMAXW(%%mm0, %%mm3)
  119.             "movq %%mm3, %%mm0 nt"
  120.             "psrlq $16, %%mm3 nt"
  121.     PMAXW(%%mm0, %%mm3)
  122.             "movd %%mm3, %%"REG_a" nt"
  123.             "movzb %%al, %%"REG_a" nt" // last_non_zero_p1
  124.     : "+a" (last_non_zero_p1)
  125.             : "r" (block+64), "r" (qmat), "r" (bias),
  126.               "r" (inv_zigzag_direct16+64), "r" (temp_block+64)
  127.         );
  128.         // note the asm is split cuz gcc doesnt like that many operands ...
  129.         asm volatile(
  130.             "movd %1, %%mm1 nt" // max_qcoeff
  131.     SPREADW(%%mm1)
  132.             "psubusw %%mm1, %%mm4 nt" 
  133.             "packuswb %%mm4, %%mm4 nt"
  134.             "movd %%mm4, %0 nt" // *overflow
  135.         : "=g" (*overflow)
  136.         : "g" (s->max_qcoeff)
  137.         );
  138.     }else{ // FMT_H263
  139.         asm volatile(
  140.             "movd %%"REG_a", %%mm3 nt" // last_non_zero_p1
  141.             SPREADW(%%mm3)
  142.             "pxor %%mm7, %%mm7 nt" // 0
  143.             "pxor %%mm4, %%mm4 nt" // 0
  144.             "mov $-128, %%"REG_a" nt"
  145.             ".balign 16 nt"
  146.             "1: nt"
  147.             "pxor %%mm1, %%mm1 nt" // 0
  148.             "movq (%1, %%"REG_a"), %%mm0 nt" // block[i]
  149.             "pcmpgtw %%mm0, %%mm1 nt" // block[i] <= 0 ? 0xFF : 0x00
  150.             "pxor %%mm1, %%mm0 nt" 
  151.             "psubw %%mm1, %%mm0 nt" // ABS(block[i])
  152.             "movq (%3, %%"REG_a"), %%mm6 nt" // bias[0]
  153.             "paddusw %%mm6, %%mm0 nt" // ABS(block[i]) + bias[0]
  154.             "movq (%2, %%"REG_a"), %%mm5 nt" // qmat[i]
  155.             "pmulhw %%mm5, %%mm0 nt" // (ABS(block[i])*qmat[0] + bias[0]*qmat[0])>>16
  156.             "por %%mm0, %%mm4 nt" 
  157.             "pxor %%mm1, %%mm0 nt" 
  158.             "psubw %%mm1, %%mm0 nt" // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i])
  159.             "movq %%mm0, (%5, %%"REG_a") nt"
  160.             "pcmpeqw %%mm7, %%mm0 nt" // out==0 ? 0xFF : 0x00
  161.             "movq (%4, %%"REG_a"), %%mm1 nt" 
  162.             "movq %%mm7, (%1, %%"REG_a") nt" // 0
  163.             "pandn %%mm1, %%mm0 nt"
  164.     PMAXW(%%mm0, %%mm3)
  165.             "add $8, %%"REG_a" nt"
  166.             " js 1b nt"
  167.             "movq %%mm3, %%mm0 nt"
  168.             "psrlq $32, %%mm3 nt"
  169.     PMAXW(%%mm0, %%mm3)
  170.             "movq %%mm3, %%mm0 nt"
  171.             "psrlq $16, %%mm3 nt"
  172.     PMAXW(%%mm0, %%mm3)
  173.             "movd %%mm3, %%"REG_a" nt"
  174.             "movzb %%al, %%"REG_a" nt" // last_non_zero_p1
  175.     : "+a" (last_non_zero_p1)
  176.             : "r" (block+64), "r" (qmat+64), "r" (bias+64),
  177.               "r" (inv_zigzag_direct16+64), "r" (temp_block+64)
  178.         );
  179.         // note the asm is split cuz gcc doesnt like that many operands ...
  180.         asm volatile(
  181.             "movd %1, %%mm1 nt" // max_qcoeff
  182.     SPREADW(%%mm1)
  183.             "psubusw %%mm1, %%mm4 nt" 
  184.             "packuswb %%mm4, %%mm4 nt"
  185.             "movd %%mm4, %0 nt" // *overflow
  186.         : "=g" (*overflow)
  187.         : "g" (s->max_qcoeff)
  188.         );
  189.     }
  190.     if(s->mb_intra) block[0]= level;
  191.     else            block[0]= temp_block[0];
  192.     if(s->dsp.idct_permutation_type == FF_SIMPLE_IDCT_PERM){
  193.         if(last_non_zero_p1 <= 1) goto end;
  194.         block[0x08] = temp_block[0x01]; block[0x10] = temp_block[0x08]; 
  195.         block[0x20] = temp_block[0x10]; 
  196.         if(last_non_zero_p1 <= 4) goto end;
  197.         block[0x18] = temp_block[0x09]; block[0x04] = temp_block[0x02]; 
  198.         block[0x09] = temp_block[0x03]; 
  199.         if(last_non_zero_p1 <= 7) goto end;
  200.         block[0x14] = temp_block[0x0A]; block[0x28] = temp_block[0x11]; 
  201.         block[0x12] = temp_block[0x18]; block[0x02] = temp_block[0x20]; 
  202.         if(last_non_zero_p1 <= 11) goto end;
  203.         block[0x1A] = temp_block[0x19]; block[0x24] = temp_block[0x12]; 
  204.         block[0x19] = temp_block[0x0B]; block[0x01] = temp_block[0x04]; 
  205.         block[0x0C] = temp_block[0x05]; 
  206.         if(last_non_zero_p1 <= 16) goto end;
  207.         block[0x11] = temp_block[0x0C]; block[0x29] = temp_block[0x13]; 
  208.         block[0x16] = temp_block[0x1A]; block[0x0A] = temp_block[0x21]; 
  209.         block[0x30] = temp_block[0x28]; block[0x22] = temp_block[0x30]; 
  210.         block[0x38] = temp_block[0x29]; block[0x06] = temp_block[0x22]; 
  211.         if(last_non_zero_p1 <= 24) goto end;
  212.         block[0x1B] = temp_block[0x1B]; block[0x21] = temp_block[0x14]; 
  213.         block[0x1C] = temp_block[0x0D]; block[0x05] = temp_block[0x06]; 
  214.         block[0x0D] = temp_block[0x07]; block[0x15] = temp_block[0x0E]; 
  215.         block[0x2C] = temp_block[0x15]; block[0x13] = temp_block[0x1C]; 
  216.         if(last_non_zero_p1 <= 32) goto end;
  217.         block[0x0B] = temp_block[0x23]; block[0x34] = temp_block[0x2A]; 
  218.         block[0x2A] = temp_block[0x31]; block[0x32] = temp_block[0x38]; 
  219.         block[0x3A] = temp_block[0x39]; block[0x26] = temp_block[0x32]; 
  220.         block[0x39] = temp_block[0x2B]; block[0x03] = temp_block[0x24]; 
  221.         if(last_non_zero_p1 <= 40) goto end;
  222.         block[0x1E] = temp_block[0x1D]; block[0x25] = temp_block[0x16]; 
  223.         block[0x1D] = temp_block[0x0F]; block[0x2D] = temp_block[0x17]; 
  224.         block[0x17] = temp_block[0x1E]; block[0x0E] = temp_block[0x25]; 
  225.         block[0x31] = temp_block[0x2C]; block[0x2B] = temp_block[0x33]; 
  226.         if(last_non_zero_p1 <= 48) goto end;
  227.         block[0x36] = temp_block[0x3A]; block[0x3B] = temp_block[0x3B]; 
  228.         block[0x23] = temp_block[0x34]; block[0x3C] = temp_block[0x2D]; 
  229.         block[0x07] = temp_block[0x26]; block[0x1F] = temp_block[0x1F]; 
  230.         block[0x0F] = temp_block[0x27]; block[0x35] = temp_block[0x2E]; 
  231.         if(last_non_zero_p1 <= 56) goto end;
  232.         block[0x2E] = temp_block[0x35]; block[0x33] = temp_block[0x3C]; 
  233.         block[0x3E] = temp_block[0x3D]; block[0x27] = temp_block[0x36]; 
  234.         block[0x3D] = temp_block[0x2F]; block[0x2F] = temp_block[0x37]; 
  235.         block[0x37] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
  236.     }else if(s->dsp.idct_permutation_type == FF_LIBMPEG2_IDCT_PERM){
  237.         if(last_non_zero_p1 <= 1) goto end;
  238.         block[0x04] = temp_block[0x01]; 
  239.         block[0x08] = temp_block[0x08]; block[0x10] = temp_block[0x10]; 
  240.         if(last_non_zero_p1 <= 4) goto end;
  241.         block[0x0C] = temp_block[0x09]; block[0x01] = temp_block[0x02]; 
  242.         block[0x05] = temp_block[0x03]; 
  243.         if(last_non_zero_p1 <= 7) goto end;
  244.         block[0x09] = temp_block[0x0A]; block[0x14] = temp_block[0x11]; 
  245.         block[0x18] = temp_block[0x18]; block[0x20] = temp_block[0x20]; 
  246.         if(last_non_zero_p1 <= 11) goto end;
  247.         block[0x1C] = temp_block[0x19]; 
  248.         block[0x11] = temp_block[0x12]; block[0x0D] = temp_block[0x0B]; 
  249.         block[0x02] = temp_block[0x04]; block[0x06] = temp_block[0x05]; 
  250.         if(last_non_zero_p1 <= 16) goto end;
  251.         block[0x0A] = temp_block[0x0C]; block[0x15] = temp_block[0x13]; 
  252.         block[0x19] = temp_block[0x1A]; block[0x24] = temp_block[0x21]; 
  253.         block[0x28] = temp_block[0x28]; block[0x30] = temp_block[0x30]; 
  254.         block[0x2C] = temp_block[0x29]; block[0x21] = temp_block[0x22]; 
  255.         if(last_non_zero_p1 <= 24) goto end;
  256.         block[0x1D] = temp_block[0x1B]; block[0x12] = temp_block[0x14]; 
  257.         block[0x0E] = temp_block[0x0D]; block[0x03] = temp_block[0x06]; 
  258.         block[0x07] = temp_block[0x07]; block[0x0B] = temp_block[0x0E]; 
  259.         block[0x16] = temp_block[0x15]; block[0x1A] = temp_block[0x1C]; 
  260.         if(last_non_zero_p1 <= 32) goto end;
  261.         block[0x25] = temp_block[0x23]; block[0x29] = temp_block[0x2A]; 
  262.         block[0x34] = temp_block[0x31]; block[0x38] = temp_block[0x38]; 
  263.         block[0x3C] = temp_block[0x39]; block[0x31] = temp_block[0x32]; 
  264.         block[0x2D] = temp_block[0x2B]; block[0x22] = temp_block[0x24]; 
  265.         if(last_non_zero_p1 <= 40) goto end;
  266.         block[0x1E] = temp_block[0x1D]; block[0x13] = temp_block[0x16]; 
  267.         block[0x0F] = temp_block[0x0F]; block[0x17] = temp_block[0x17]; 
  268.         block[0x1B] = temp_block[0x1E]; block[0x26] = temp_block[0x25]; 
  269.         block[0x2A] = temp_block[0x2C]; block[0x35] = temp_block[0x33]; 
  270.         if(last_non_zero_p1 <= 48) goto end;
  271.         block[0x39] = temp_block[0x3A]; block[0x3D] = temp_block[0x3B]; 
  272.         block[0x32] = temp_block[0x34]; block[0x2E] = temp_block[0x2D]; 
  273.             block[0x23] = temp_block[0x26]; block[0x1F] = temp_block[0x1F]; 
  274.         block[0x27] = temp_block[0x27]; block[0x2B] = temp_block[0x2E]; 
  275.         if(last_non_zero_p1 <= 56) goto end;
  276.         block[0x36] = temp_block[0x35]; block[0x3A] = temp_block[0x3C]; 
  277.         block[0x3E] = temp_block[0x3D]; block[0x33] = temp_block[0x36]; 
  278.         block[0x2F] = temp_block[0x2F]; block[0x37] = temp_block[0x37]; 
  279.         block[0x3B] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
  280.     }else{
  281.         if(last_non_zero_p1 <= 1) goto end;
  282.         block[0x01] = temp_block[0x01]; 
  283.         block[0x08] = temp_block[0x08]; block[0x10] = temp_block[0x10]; 
  284.         if(last_non_zero_p1 <= 4) goto end;
  285.         block[0x09] = temp_block[0x09]; block[0x02] = temp_block[0x02]; 
  286.         block[0x03] = temp_block[0x03]; 
  287.         if(last_non_zero_p1 <= 7) goto end;
  288.         block[0x0A] = temp_block[0x0A]; block[0x11] = temp_block[0x11]; 
  289.         block[0x18] = temp_block[0x18]; block[0x20] = temp_block[0x20]; 
  290.         if(last_non_zero_p1 <= 11) goto end;
  291.         block[0x19] = temp_block[0x19]; 
  292.         block[0x12] = temp_block[0x12]; block[0x0B] = temp_block[0x0B]; 
  293.         block[0x04] = temp_block[0x04]; block[0x05] = temp_block[0x05]; 
  294.         if(last_non_zero_p1 <= 16) goto end;
  295.         block[0x0C] = temp_block[0x0C]; block[0x13] = temp_block[0x13]; 
  296.         block[0x1A] = temp_block[0x1A]; block[0x21] = temp_block[0x21]; 
  297.         block[0x28] = temp_block[0x28]; block[0x30] = temp_block[0x30]; 
  298.         block[0x29] = temp_block[0x29]; block[0x22] = temp_block[0x22]; 
  299.         if(last_non_zero_p1 <= 24) goto end;
  300.         block[0x1B] = temp_block[0x1B]; block[0x14] = temp_block[0x14]; 
  301.         block[0x0D] = temp_block[0x0D]; block[0x06] = temp_block[0x06]; 
  302.         block[0x07] = temp_block[0x07]; block[0x0E] = temp_block[0x0E]; 
  303.         block[0x15] = temp_block[0x15]; block[0x1C] = temp_block[0x1C]; 
  304.         if(last_non_zero_p1 <= 32) goto end;
  305.         block[0x23] = temp_block[0x23]; block[0x2A] = temp_block[0x2A]; 
  306.         block[0x31] = temp_block[0x31]; block[0x38] = temp_block[0x38]; 
  307.         block[0x39] = temp_block[0x39]; block[0x32] = temp_block[0x32]; 
  308.         block[0x2B] = temp_block[0x2B]; block[0x24] = temp_block[0x24]; 
  309.         if(last_non_zero_p1 <= 40) goto end;
  310.         block[0x1D] = temp_block[0x1D]; block[0x16] = temp_block[0x16]; 
  311.         block[0x0F] = temp_block[0x0F]; block[0x17] = temp_block[0x17]; 
  312.         block[0x1E] = temp_block[0x1E]; block[0x25] = temp_block[0x25]; 
  313.         block[0x2C] = temp_block[0x2C]; block[0x33] = temp_block[0x33]; 
  314.         if(last_non_zero_p1 <= 48) goto end;
  315.         block[0x3A] = temp_block[0x3A]; block[0x3B] = temp_block[0x3B]; 
  316.         block[0x34] = temp_block[0x34]; block[0x2D] = temp_block[0x2D]; 
  317.         block[0x26] = temp_block[0x26]; block[0x1F] = temp_block[0x1F]; 
  318.         block[0x27] = temp_block[0x27]; block[0x2E] = temp_block[0x2E]; 
  319.         if(last_non_zero_p1 <= 56) goto end;
  320.         block[0x35] = temp_block[0x35]; block[0x3C] = temp_block[0x3C]; 
  321.         block[0x3D] = temp_block[0x3D]; block[0x36] = temp_block[0x36]; 
  322.         block[0x2F] = temp_block[0x2F]; block[0x37] = temp_block[0x37]; 
  323.         block[0x3E] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
  324.     }
  325.     end:
  326. /*
  327.     for(i=0; i<last_non_zero_p1; i++)
  328.     {
  329.        int j= zigzag_direct_noperm[i];
  330.        block[block_permute_op(j)]= temp_block[j];
  331.     }
  332. */
  333.     return last_non_zero_p1 - 1;
  334. }