deblock.c
上传用户:lctgjx
上传日期:2022-06-04
资源大小:8887k
文件大小:17k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

Visual C++

  1. /*****************************************************************************
  2. * deblock.c: Altivec-accelerated deblocking for h264 encoder
  3. *****************************************************************************
  4. * Copyright (C) 2007-2008 Guillaume Poirier <gpoirier@mplayerhq.hu>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program 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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
  19. *****************************************************************************/
  20. #include "common/common.h"
  21. #include "ppccommon.h"
  22. #define transpose4x16(r0, r1, r2, r3) {      
  23.     register vec_u8_t r4;                    
  24.     register vec_u8_t r5;                    
  25.     register vec_u8_t r6;                    
  26.     register vec_u8_t r7;                    
  27.                                              
  28.     r4 = vec_mergeh(r0, r2);  /*0, 2 set 0*/ 
  29.     r5 = vec_mergel(r0, r2);  /*0, 2 set 1*/ 
  30.     r6 = vec_mergeh(r1, r3);  /*1, 3 set 0*/ 
  31.     r7 = vec_mergel(r1, r3);  /*1, 3 set 1*/ 
  32.                                              
  33.     r0 = vec_mergeh(r4, r6);  /*all set 0*/  
  34.     r1 = vec_mergel(r4, r6);  /*all set 1*/  
  35.     r2 = vec_mergeh(r5, r7);  /*all set 2*/  
  36.     r3 = vec_mergel(r5, r7);  /*all set 3*/  
  37. }
  38. static inline void write16x4(uint8_t *dst, int dst_stride,
  39.                              register vec_u8_t r0, register vec_u8_t r1,
  40.                              register vec_u8_t r2, register vec_u8_t r3) {
  41.     ALIGNED_16(unsigned char result[64]);
  42.     uint32_t *src_int = (uint32_t *)result, *dst_int = (uint32_t *)dst;
  43.     int int_dst_stride = dst_stride/4;
  44.     vec_st(r0, 0, result);
  45.     vec_st(r1, 16, result);
  46.     vec_st(r2, 32, result);
  47.     vec_st(r3, 48, result);
  48.     /* FIXME: there has to be a better way!!!! */
  49.     *dst_int = *src_int;
  50.     *(dst_int+   int_dst_stride) = *(src_int + 1);
  51.     *(dst_int+ 2*int_dst_stride) = *(src_int + 2);
  52.     *(dst_int+ 3*int_dst_stride) = *(src_int + 3);
  53.     *(dst_int+ 4*int_dst_stride) = *(src_int + 4);
  54.     *(dst_int+ 5*int_dst_stride) = *(src_int + 5);
  55.     *(dst_int+ 6*int_dst_stride) = *(src_int + 6);
  56.     *(dst_int+ 7*int_dst_stride) = *(src_int + 7);
  57.     *(dst_int+ 8*int_dst_stride) = *(src_int + 8);
  58.     *(dst_int+ 9*int_dst_stride) = *(src_int + 9);
  59.     *(dst_int+10*int_dst_stride) = *(src_int + 10);
  60.     *(dst_int+11*int_dst_stride) = *(src_int + 11);
  61.     *(dst_int+12*int_dst_stride) = *(src_int + 12);
  62.     *(dst_int+13*int_dst_stride) = *(src_int + 13);
  63.     *(dst_int+14*int_dst_stride) = *(src_int + 14);
  64.     *(dst_int+15*int_dst_stride) = *(src_int + 15);
  65. }
  66. /** brief performs a 6x16 transpose of data in src, and stores it to dst */
  67. #define readAndTranspose16x6(src, src_stride, r8, r9, r10, r11, r12, r13) {
  68.     register vec_u8_t r0, r1, r2, r3, r4, r5, r6, r7, r14, r15;
  69.     VEC_LOAD(src,                  r0, 16, vec_u8_t, pix );    
  70.     VEC_LOAD(src +    src_stride,  r1, 16, vec_u8_t, pix );    
  71.     VEC_LOAD(src +  2*src_stride,  r2, 16, vec_u8_t, pix );    
  72.     VEC_LOAD(src +  3*src_stride,  r3, 16, vec_u8_t, pix );    
  73.     VEC_LOAD(src +  4*src_stride,  r4, 16, vec_u8_t, pix );    
  74.     VEC_LOAD(src +  5*src_stride,  r5, 16, vec_u8_t, pix );    
  75.     VEC_LOAD(src +  6*src_stride,  r6, 16, vec_u8_t, pix );    
  76.     VEC_LOAD(src +  7*src_stride,  r7, 16, vec_u8_t, pix );    
  77.     VEC_LOAD(src + 14*src_stride, r14, 16, vec_u8_t, pix );    
  78.     VEC_LOAD(src + 15*src_stride, r15, 16, vec_u8_t, pix );    
  79.                                                                
  80.     VEC_LOAD(src + 8*src_stride,   r8, 16, vec_u8_t, pix );    
  81.     VEC_LOAD(src + 9*src_stride,   r9, 16, vec_u8_t, pix );    
  82.     VEC_LOAD(src + 10*src_stride, r10, 16, vec_u8_t, pix );    
  83.     VEC_LOAD(src + 11*src_stride, r11, 16, vec_u8_t, pix );    
  84.     VEC_LOAD(src + 12*src_stride, r12, 16, vec_u8_t, pix );    
  85.     VEC_LOAD(src + 13*src_stride, r13, 16, vec_u8_t, pix );    
  86.                                                                
  87.     /*Merge first pairs*/                                      
  88.     r0 = vec_mergeh(r0, r8);    /*0, 8*/                       
  89.     r1 = vec_mergeh(r1, r9);    /*1, 9*/                       
  90.     r2 = vec_mergeh(r2, r10);   /*2,10*/                       
  91.     r3 = vec_mergeh(r3, r11);   /*3,11*/                       
  92.     r4 = vec_mergeh(r4, r12);   /*4,12*/                       
  93.     r5 = vec_mergeh(r5, r13);   /*5,13*/                       
  94.     r6 = vec_mergeh(r6, r14);   /*6,14*/                       
  95.     r7 = vec_mergeh(r7, r15);   /*7,15*/                       
  96.                                                                
  97.     /*Merge second pairs*/                                     
  98.     r8  = vec_mergeh(r0, r4);   /*0,4, 8,12 set 0*/            
  99.     r9  = vec_mergel(r0, r4);   /*0,4, 8,12 set 1*/            
  100.     r10 = vec_mergeh(r1, r5);   /*1,5, 9,13 set 0*/            
  101.     r11 = vec_mergel(r1, r5);   /*1,5, 9,13 set 1*/            
  102.     r12 = vec_mergeh(r2, r6);   /*2,6,10,14 set 0*/            
  103.     r13 = vec_mergel(r2, r6);   /*2,6,10,14 set 1*/            
  104.     r14 = vec_mergeh(r3, r7);   /*3,7,11,15 set 0*/            
  105.     r15 = vec_mergel(r3, r7);   /*3,7,11,15 set 1*/            
  106.                                                                
  107.     /*Third merge*/                                            
  108.     r0 = vec_mergeh(r8, r12);   /*0,2,4,6,8,10,12,14 set 0*/   
  109.     r1 = vec_mergel(r8, r12);   /*0,2,4,6,8,10,12,14 set 1*/   
  110.     r2 = vec_mergeh(r9, r13);   /*0,2,4,6,8,10,12,14 set 2*/   
  111.     r4 = vec_mergeh(r10, r14);  /*1,3,5,7,9,11,13,15 set 0*/   
  112.     r5 = vec_mergel(r10, r14);  /*1,3,5,7,9,11,13,15 set 1*/   
  113.     r6 = vec_mergeh(r11, r15);  /*1,3,5,7,9,11,13,15 set 2*/   
  114.     /* Don't need to compute 3 and 7*/                         
  115.                                                                
  116.     /*Final merge*/                                            
  117.     r8  = vec_mergeh(r0, r4);   /*all set 0*/                  
  118.     r9  = vec_mergel(r0, r4);   /*all set 1*/                  
  119.     r10 = vec_mergeh(r1, r5);   /*all set 2*/                  
  120.     r11 = vec_mergel(r1, r5);   /*all set 3*/                  
  121.     r12 = vec_mergeh(r2, r6);   /*all set 4*/                  
  122.     r13 = vec_mergel(r2, r6);   /*all set 5*/                  
  123.     /* Don't need to compute 14 and 15*/                       
  124.                                                                
  125. }
  126. // out: o = |x-y| < a
  127. static inline vec_u8_t diff_lt_altivec ( register vec_u8_t x,
  128.                                          register vec_u8_t y,
  129.                                          register vec_u8_t a) {
  130.     register vec_u8_t diff = vec_subs(x, y);
  131.     register vec_u8_t diffneg = vec_subs(y, x);
  132.     register vec_u8_t o = vec_or(diff, diffneg); /* |x-y| */
  133.     o = (vec_u8_t)vec_cmplt(o, a);
  134.     return o;
  135. }
  136. static inline vec_u8_t h264_deblock_mask ( register vec_u8_t p0,
  137.                                            register vec_u8_t p1,
  138.                                            register vec_u8_t q0,
  139.                                            register vec_u8_t q1,
  140.                                            register vec_u8_t alpha,
  141.                                            register vec_u8_t beta) {
  142.     register vec_u8_t mask;
  143.     register vec_u8_t tempmask;
  144.     mask = diff_lt_altivec(p0, q0, alpha);
  145.     tempmask = diff_lt_altivec(p1, p0, beta);
  146.     mask = vec_and(mask, tempmask);
  147.     tempmask = diff_lt_altivec(q1, q0, beta);
  148.     mask = vec_and(mask, tempmask);
  149.     return mask;
  150. }
  151. // out: newp1 = clip((p2 + ((p0 + q0 + 1) >> 1)) >> 1, p1-tc0, p1+tc0)
  152. static inline vec_u8_t h264_deblock_q1(register vec_u8_t p0,
  153.                                        register vec_u8_t p1,
  154.                                        register vec_u8_t p2,
  155.                                        register vec_u8_t q0,
  156.                                        register vec_u8_t tc0) {
  157.     register vec_u8_t average = vec_avg(p0, q0);
  158.     register vec_u8_t temp;
  159.     register vec_u8_t uncliped;
  160.     register vec_u8_t ones;
  161.     register vec_u8_t max;
  162.     register vec_u8_t min;
  163.     register vec_u8_t newp1;
  164.     temp = vec_xor(average, p2);
  165.     average = vec_avg(average, p2);     /*avg(p2, avg(p0, q0)) */
  166.     ones = vec_splat_u8(1);
  167.     temp = vec_and(temp, ones);         /*(p2^avg(p0, q0)) & 1 */
  168.     uncliped = vec_subs(average, temp); /*(p2+((p0+q0+1)>>1))>>1 */
  169.     max = vec_adds(p1, tc0);
  170.     min = vec_subs(p1, tc0);
  171.     newp1 = vec_max(min, uncliped);
  172.     newp1 = vec_min(max, newp1);
  173.     return newp1;
  174. }
  175. #define h264_deblock_p0_q0(p0, p1, q0, q1, tc0masked) {                                         
  176.                                                                                                 
  177.     const vec_u8_t A0v = vec_sl(vec_splat_u8(10), vec_splat_u8(4));                             
  178.                                                                                                 
  179.     register vec_u8_t pq0bit = vec_xor(p0,q0);                                                  
  180.     register vec_u8_t q1minus;                                                                  
  181.     register vec_u8_t p0minus;                                                                  
  182.     register vec_u8_t stage1;                                                                   
  183.     register vec_u8_t stage2;                                                                   
  184.     register vec_u8_t vec160;                                                                   
  185.     register vec_u8_t delta;                                                                    
  186.     register vec_u8_t deltaneg;                                                                 
  187.                                                                                                 
  188.     q1minus = vec_nor(q1, q1);                /* 255 - q1 */                                    
  189.     stage1 = vec_avg(p1, q1minus);            /* (p1 - q1 + 256)>>1 */                          
  190.     stage2 = vec_sr(stage1, vec_splat_u8(1)); /* (p1 - q1 + 256)>>2 = 64 + (p1 - q1) >> 2 */    
  191.     p0minus = vec_nor(p0, p0);                /* 255 - p0 */                                    
  192.     stage1 = vec_avg(q0, p0minus);            /* (q0 - p0 + 256)>>1 */                          
  193.     pq0bit = vec_and(pq0bit, vec_splat_u8(1));                                                  
  194.     stage2 = vec_avg(stage2, pq0bit);         /* 32 + ((q0 - p0)&1 + (p1 - q1) >> 2 + 1) >> 1 */
  195.     stage2 = vec_adds(stage2, stage1);        /* 160 + ((p0 - q0) + (p1 - q1) >> 2 + 1) >> 1 */ 
  196.     vec160 = vec_ld(0, &A0v);                                                                   
  197.     deltaneg = vec_subs(vec160, stage2);      /* -d */                                          
  198.     delta = vec_subs(stage2, vec160);         /*  d */                                          
  199.     deltaneg = vec_min(tc0masked, deltaneg);                                                    
  200.     delta = vec_min(tc0masked, delta);                                                          
  201.     p0 = vec_subs(p0, deltaneg);                                                                
  202.     q0 = vec_subs(q0, delta);                                                                   
  203.     p0 = vec_adds(p0, delta);                                                                   
  204.     q0 = vec_adds(q0, deltaneg);                                                                
  205. }
  206. #define h264_loop_filter_luma_altivec(p2, p1, p0, q0, q1, q2, alpha, beta, tc0) {            
  207.     ALIGNED_16(unsigned char temp[16]);                                              
  208.     register vec_u8_t alphavec;                                                              
  209.     register vec_u8_t betavec;                                                               
  210.     register vec_u8_t mask;                                                                  
  211.     register vec_u8_t p1mask;                                                                
  212.     register vec_u8_t q1mask;                                                                
  213.     register vec_s8_t tc0vec;                                                                
  214.     register vec_u8_t finaltc0;                                                              
  215.     register vec_u8_t tc0masked;                                                             
  216.     register vec_u8_t newp1;                                                                 
  217.     register vec_u8_t newq1;                                                                 
  218.                                                                                              
  219.     temp[0] = alpha;                                                                         
  220.     temp[1] = beta;                                                                          
  221.     alphavec = vec_ld(0, temp);                                                              
  222.     betavec = vec_splat(alphavec, 0x1);                                                      
  223.     alphavec = vec_splat(alphavec, 0x0);                                                     
  224.     mask = h264_deblock_mask(p0, p1, q0, q1, alphavec, betavec); /*if in block */            
  225.                                                                                              
  226.     *((int *)temp) = *((int *)tc0);                                                          
  227.     tc0vec = vec_ld(0, (signed char*)temp);                                                  
  228.     tc0vec = vec_mergeh(tc0vec, tc0vec);                                                     
  229.     tc0vec = vec_mergeh(tc0vec, tc0vec);                                                     
  230.     mask = vec_and(mask, vec_cmpgt(tc0vec, vec_splat_s8(-1)));  /* if tc0[i] >= 0 */         
  231.     finaltc0 = vec_and((vec_u8_t)tc0vec, mask);                 /* tc = tc0 */               
  232.                                                                                              
  233.     p1mask = diff_lt_altivec(p2, p0, betavec);                                               
  234.     p1mask = vec_and(p1mask, mask);                             /* if( |p2 - p0| < beta) */  
  235.     tc0masked = vec_and(p1mask, (vec_u8_t)tc0vec);                                           
  236.     finaltc0 = vec_sub(finaltc0, p1mask);                       /* tc++ */                   
  237.     newp1 = h264_deblock_q1(p0, p1, p2, q0, tc0masked);                                      
  238.     /*end if*/                                                                               
  239.                                                                                              
  240.     q1mask = diff_lt_altivec(q2, q0, betavec);                                               
  241.     q1mask = vec_and(q1mask, mask);                             /* if ( |q2 - q0| < beta ) */
  242.     tc0masked = vec_and(q1mask, (vec_u8_t)tc0vec);                                           
  243.     finaltc0 = vec_sub(finaltc0, q1mask);                       /* tc++ */                   
  244.     newq1 = h264_deblock_q1(p0, q1, q2, q0, tc0masked);                                      
  245.     /*end if*/                                                                               
  246.                                                                                              
  247.     h264_deblock_p0_q0(p0, p1, q0, q1, finaltc0);                                            
  248.     p1 = newp1;                                                                              
  249.     q1 = newq1;                                                                              
  250. }
  251. void x264_deblock_v_luma_altivec(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0) {
  252.     if((tc0[0] & tc0[1] & tc0[2] & tc0[3]) >= 0) {
  253.         register vec_u8_t p2 = vec_ld(-3*stride, pix);
  254.         register vec_u8_t p1 = vec_ld(-2*stride, pix);
  255.         register vec_u8_t p0 = vec_ld(-1*stride, pix);
  256.         register vec_u8_t q0 = vec_ld(0, pix);
  257.         register vec_u8_t q1 = vec_ld(stride, pix);
  258.         register vec_u8_t q2 = vec_ld(2*stride, pix);
  259.         h264_loop_filter_luma_altivec(p2, p1, p0, q0, q1, q2, alpha, beta, tc0);
  260.         vec_st(p1, -2*stride, pix);
  261.         vec_st(p0, -1*stride, pix);
  262.         vec_st(q0, 0, pix);
  263.         vec_st(q1, stride, pix);
  264.     }
  265. }
  266. void x264_deblock_h_luma_altivec(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0) {
  267.     register vec_u8_t line0, line1, line2, line3, line4, line5;
  268.     if((tc0[0] & tc0[1] & tc0[2] & tc0[3]) < 0)
  269.         return;
  270.     PREP_LOAD;
  271.     vec_u8_t _pix_ = vec_lvsl(0, pix-3);
  272.     readAndTranspose16x6(pix-3, stride, line0, line1, line2, line3, line4, line5);
  273.     h264_loop_filter_luma_altivec(line0, line1, line2, line3, line4, line5, alpha, beta, tc0);
  274.     transpose4x16(line1, line2, line3, line4);
  275.     write16x4(pix-2, stride, line1, line2, line3, line4);
  276. }