Guest User

Untitled

a guest
Jun 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. #include "vbx.h"
  2.  
  3. void filter(uint8_t* in_vid,uint8_t* out_vid,int rows, int cols,int pitch){
  4. int rowlen=cols;
  5. // save scratchpad allocator state
  6. vbx_sp_push();
  7. vbx_ubyte_t* line_in= vbx_sp_malloc(rowlen*3);
  8.  
  9. vbx_word_t* v_tmp0=vbx_sp_malloc(rowlen*sizeof(vbx_word_t));
  10. vbx_word_t* v_tmp1=vbx_sp_malloc(rowlen*sizeof(vbx_word_t));
  11. vbx_word_t* v_tmp2=vbx_sp_malloc(rowlen*sizeof(vbx_word_t));
  12. vbx_word_t* luma_top=vbx_sp_malloc(rowlen*sizeof(vbx_word_t));
  13. vbx_word_t* luma_mid=vbx_sp_malloc(rowlen*sizeof(vbx_word_t));
  14. vbx_word_t* luma_bot=vbx_sp_malloc(rowlen*sizeof(vbx_word_t));
  15. vbx_word_t* sobel_top=vbx_sp_malloc(rowlen*sizeof(vbx_word_t));
  16. vbx_word_t* sobel_mid=vbx_sp_malloc(rowlen*sizeof(vbx_word_t));
  17. vbx_word_t* sobel_bot=vbx_sp_malloc(rowlen*sizeof(vbx_word_t));
  18. vbx_word_t* grad_x=vbx_sp_malloc(rowlen*sizeof(vbx_word_t));
  19. vbx_word_t* grad_y=vbx_sp_malloc(rowlen*sizeof(vbx_word_t));
  20.  
  21.  
  22. int row;
  23. for(row=0;row<rows;row++){
  24. //load a row into scratchpad
  25. vbx_dma_to_vector(line_in,in_vid+row*pitch*3,rowlen*3);
  26.  
  27. //extract each channel into seperate vectors
  28. //multiply by a facto for combining into luma
  29. vbx_set_vl(1);
  30. vbx_set_2D(rowlen,sizeof(vbx_word_t),3,3);
  31. vbx_2D(SVBWU,VMUL,(vbx_uword_t*)v_tmp0,66,line_in+0);
  32. vbx_2D(SVBWU,VMUL,(vbx_uword_t*)v_tmp1,129,line_in+1);
  33. vbx_2D(SVBWU,VMUL,(vbx_uword_t*)v_tmp2,25,line_in+2);
  34.  
  35. //convert to luma
  36. vbx_set_vl(rowlen);
  37. vbx(VVW,VADD,luma_bot,v_tmp0,v_tmp1);
  38. vbx(VVW,VADD,luma_bot,luma_bot,v_tmp2);
  39. vbx(SVW,VSHR,luma_bot,8,luma_bot);
  40.  
  41. /// Apply [1 2 1] to input luma row
  42. vbx_set_vl(rowlen-1);
  43. vbx(VVW,VADD,sobel_bot,luma_bot,luma_bot+1);
  44. vbx_set_vl(rowlen-2);
  45. vbx(VVW,VADD,sobel_bot,sobel_bot,sobel_bot+1);
  46.  
  47. if (row>1){
  48.  
  49. // |grad_y| = |sobel_top - sobel_bot|;
  50. vbx(VVW,VABSDIFF,grad_y,sobel_top,sobel_bot);
  51.  
  52. //grad_x = | 1 0 -1 |
  53. // | 2 0 -2 | * luma
  54. // | 1 - -1 |
  55. vbx_set_vl(rowlen);
  56. vbx(VVW,VADD,v_tmp0,luma_top,luma_mid);
  57. vbx(VVW,VADD,v_tmp1,luma_bot,luma_mid);
  58. vbx(VVW,VADD,v_tmp0,v_tmp0,v_tmp1);
  59.  
  60. vbx_set_vl(rowlen-2);
  61. vbx(VVW,VABSDIFF,grad_x,v_tmp0,v_tmp0+2);
  62.  
  63. //sum gradiaents and threshold at 255
  64. vbx_word_t* row_out=grad_x;
  65. vbx(VVW,VADD,row_out,grad_x,grad_y);
  66. vbx(SVW,VSUB,v_tmp0,255,row_out);
  67. vbx(SVW,VCMV_LTZ,row_out,255,v_tmp0);
  68.  
  69. //copy to other bytes of word
  70. vbx(SVW,VMUL,row_out,0x010101,row_out);
  71.  
  72. //pack into RGB
  73. vbx_set_vl(3);
  74. vbx_set_2D(rowlen-2,3,sizeof(vbx_word_t),0);
  75. vbx_2D(VVB,VMOV,(vbx_byte_t*)row_out,(vbx_byte_t*)row_out,0);
  76. vbx_byte_t *out=out_vid+((row-1)*pitch + 1)*3;
  77. vbx_dma_to_host(out,row_out,(rowlen-2)*3);
  78. }
  79.  
  80. //rotate rows
  81. {
  82. vbx_word_t* aa;
  83. aa=luma_top;
  84. luma_top=luma_mid;
  85. luma_mid=luma_bot;
  86. luma_bot=aa;
  87.  
  88. aa=sobel_top;
  89. sobel_top=sobel_mid;
  90. sobel_mid=sobel_bot;
  91. sobel_bot=aa;
  92. }
  93.  
  94. }
  95.  
  96. //blackout outside pixels
  97. vbx_set_vl(rowlen*3);
  98. vbx(SVBU,VMOV,line_in,0,0);
  99.  
  100. //top row
  101. vbx_dma_to_host(out_vid,line_in,rowlen*3);
  102. //bottom row
  103. vbx_dma_to_host(out_vid+(rows-1)*rowlen*3,line_in,rowlen*3);
  104. //left column
  105. vbx_dma_to_host_2D(out_vid,line_in,3,cols,pitch*3,0);
  106. //right column
  107. vbx_dma_to_host_2D(out_vid+(rowlen-1)*3,line_in,3,cols,rowlen*3,0);
  108.  
  109. //restore scratchpad allocator state
  110. vbx_sp_pop();
  111.  
  112. //make sure all dma operations are complete before exiting
  113. vbx_sync();
  114. }
Add Comment
Please, Sign In to add comment