cr88192

(Updated) Moderately optimized JPEG encoder.

Jun 13th, 2013
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 32.51 KB | None | 0 0
  1. /*
  2. Copyright (C) 2013 by Brendan G Bohannon
  3. Copying: http://pastebin.com/iJxtZHm6
  4.  */
  5.  
  6. /*
  7. From the header:
  8. #define BGBBTJ_JPG_RGBA         0
  9. #define BGBBTJ_JPG_RGB          1
  10. #define BGBBTJ_JPG_BGRA         3
  11. #define BGBBTJ_JPG_BGR          4
  12. #define BGBBTJ_JPG_YUVA         5
  13. #define BGBBTJ_JPG_YUV          6
  14. #define BGBBTJ_JPG_Y            7
  15. #define BGBBTJ_JPG_YA           8
  16. #define BGBBTJ_JPG_YUV422       9   //YYUV
  17. #define BGBBTJ_JPG_YUV420       10  //YYU/YYV
  18.  
  19. #define BGBBTJ_JPG_YYYA         11
  20. #define BGBBTJ_JPG_ARGB         12
  21. #define BGBBTJ_JPG_ABGR         13
  22.  
  23. struct BGBBTJ_JFE_Context_s
  24. {
  25. // byte *cs;    //current pos in bitstream (input)
  26. byte *ct;   //current pos in bitstream (output)
  27. uint win;   //bitstream window
  28. int pos;    //bitstream offset
  29.  
  30. byte qt[4][64];
  31. int qtfp[4][64];
  32.  
  33. short *ydb, *udb, *vdb;
  34. int lxs, lys, lqf;
  35. };
  36.  
  37. */
  38.  
  39. #include <bgbbtj.h>
  40. #include <math.h>
  41.  
  42. #define DCTSZ   8
  43. #define DCTSZ2  64
  44.  
  45. #define JPG_SOF0    0xC0
  46. #define JPG_SOF1    0xC1
  47. #define JPG_SOF2    0xC2
  48. #define JPG_SOF3    0xC3
  49. #define JPG_DHT     0xC4
  50. #define JPG_SOF5    0xC5
  51. #define JPG_SOF6    0xC6
  52. #define JPG_SOF7    0xC7
  53. #define JPG_JPG     0xC8
  54. #define JPG_SOF9    0xC9
  55. #define JPG_SOF10   0xCA
  56. #define JPG_SOF11   0xCB
  57. #define JPG_DAC     0xCC
  58. #define JPG_SOF13   0xCD
  59. #define JPG_SOF14   0xCE
  60. #define JPG_SOF15   0xCF
  61.  
  62. #define JPG_RST0    0xD0
  63. #define JPG_RST1    0xD1
  64. #define JPG_RST2    0xD2
  65. #define JPG_RST3    0xD3
  66. #define JPG_RST4    0xD4
  67. #define JPG_RST5    0xD5
  68. #define JPG_RST6    0xD6
  69. #define JPG_RST7    0xD7
  70.  
  71. #define JPG_SOI     0xD8
  72. #define JPG_EOI     0xD9
  73. #define JPG_SOS     0xDA
  74. #define JPG_DQT     0xDB
  75. #define JPG_DNL     0xDC
  76. #define JPG_DRI     0xDD
  77. #define JPG_DHP     0xDE
  78. #define JPG_EXP     0xDF
  79.  
  80. #define JPG_APP0    0xE0
  81. #define JPG_APP1    0xE1
  82. #define JPG_APP2    0xE2
  83. #define JPG_APP3    0xE3
  84. #define JPG_APP4    0xE4
  85. #define JPG_APP5    0xE5
  86. #define JPG_APP6    0xE6
  87. #define JPG_APP7    0xE7
  88. #define JPG_APP8    0xE8
  89. #define JPG_APP9    0xE9
  90. #define JPG_APP10   0xEA
  91. #define JPG_APP11   0xEB
  92. #define JPG_APP12   0xEC
  93. #define JPG_APP13   0xED
  94. #define JPG_APP14   0xEE
  95. #define JPG_APP15   0xEF
  96.  
  97. #define JPG_JPG0    0xF0
  98. #define JPG_JPG1    0xF1
  99. #define JPG_JPG2    0xF2
  100. #define JPG_JPG3    0xF3
  101. #define JPG_JPG4    0xF4
  102. #define JPG_JPG5    0xF5
  103. #define JPG_JPG6    0xF6
  104. #define JPG_JPG7    0xF7
  105. #define JPG_JPG8    0xF8
  106. #define JPG_JPG9    0xF9
  107. #define JPG_JPG10   0xFA
  108. #define JPG_JPG11   0xFB
  109. #define JPG_JPG12   0xFC
  110. #define JPG_JPG13   0xFD
  111. #define JPG_COM     0xFE
  112.  
  113. char *btj_jfe_marker[]={
  114. "SOF0", "SOF1", "SOF2", "SOF3", "DHT", "SOF5", "SOF6", "SOF7",
  115. "JPG", "SOF9", "SOF10", "SOF11", "DAC", "SOF13", "SOF14", "SOF15",
  116. "RST0", "RST1", "RST2", "RST3", "RST4", "RST5", "RST6", "RST7",
  117. "SOI", "EOI", "SOS", "DQT", "DNL", "DRI", "DHP", "EXP",
  118. "APP0", "APP1", "APP2", "APP3", "APP4", "APP5", "APP6", "APP7",
  119. "APP8", "APP9", "APP10", "APP11", "APP12", "APP13", "APP14", "APP15",
  120. "JPG0", "JPG1", "JPG2", "JPG3", "JPG4", "JPG5", "JPG6", "JPG7",
  121. "JPG8", "JPG9", "JPG10", "JPG11", "JPG12", "JPG13", "COM", ""
  122. };
  123.  
  124. static const byte btj_jfe_zigzag[64]={
  125.  0,  1,  5,  6, 14, 15, 27, 28,
  126.  2,  4,  7, 13, 16, 26, 29, 42,
  127.  3,  8, 12, 17, 25, 30, 41, 43,
  128.  9, 11, 18, 24, 31, 40, 44, 53,
  129. 10, 19, 23, 32, 39, 45, 52, 54,
  130. 20, 22, 33, 38, 46, 51, 55, 60,
  131. 21, 34, 37, 47, 50, 56, 59, 61,
  132. 35, 36, 48, 49, 57, 58, 62, 63
  133. };
  134.  
  135. static const byte btj_jfe_zigzag2[64]={
  136.  0,  1,  8, 16,  9,  2,  3, 10,
  137. 17, 24, 32, 25, 18, 11,  4,  5,
  138. 12, 19, 26, 33, 40, 48, 41, 34,
  139. 27, 20, 13,  6,  7, 14, 21, 28,
  140. 35, 42, 49, 56, 57, 50, 43, 36,
  141. 29, 22, 15, 23, 30, 37, 44, 51,
  142. 58, 59, 52, 45, 38, 31, 39, 46,
  143. 53, 60, 61, 54, 47, 55, 62, 63
  144. };
  145.  
  146. ushort btj_jfeh_code[4][256];
  147.  
  148. static const byte btj_jfeh_len[4][256]={
  149. {  6,  4,  3,  3,  2,  2,  3,  5,  7,  9, 10, 10, 10, 10, 10, 11,
  150.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  151.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  152.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  153.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  154.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  155.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  156.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  157.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  158.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  159.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  160.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  161.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  162.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  163.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  164.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  165. },
  166. {  4,  2,  4,  3,  4,  5,  6,  7,  8, 13, 14, 16, 16, 15, 15, 15,
  167. 15,  3,  5,  7,  9, 12, 13, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  168. 15,  5,  7, 10, 13, 14, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16,
  169. 16,  5,  8, 11, 13, 16, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  170. 11,  6, 10, 12, 15, 16, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  171. 11,  6, 10, 15, 14, 15, 15, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  172. 11,  7, 10, 13, 15, 15, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  173. 11,  7, 10, 15, 16, 15, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  174. 11,  7, 11, 14, 15, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  175. 11,  5, 13, 15, 15, 11, 11, 11, 11, 11, 11, 11, 11, 16, 16, 16,
  176. 16,  8, 12, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  177. 16,  9, 12, 13, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  178. 16,  9, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  179. 16, 10, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  180. 16, 11, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 15, 15,
  181. 10, 12, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16,
  182. },
  183. {  2,  2,  2,  3,  4,  5,  8,  8,  9,  9,  8,  8,  8,  8,  9, 10,
  184.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  185.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  186.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  187.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  188.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  189.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  190.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  191.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  192.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  193.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  194.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  195.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  196.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  197.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  198.  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  199. },
  200. {  3,  2,  3,  3,  5,  7, 11, 14, 15, 16, 16, 16, 16, 16, 16, 16,
  201. 16,  3,  5,  8, 11, 12, 16, 16, 15, 15, 15, 15, 15, 15, 15, 15,
  202. 15,  4,  7, 11, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  203. 15,  5,  9, 11, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  204. 15,  6, 10, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  205. 15,  6, 11, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  206. 15,  7, 13, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  207. 15,  7, 12, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  208. 15,  8, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  209. 15,  8, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  210. 15,  9, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  211. 15,  9, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  212. 15, 10, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  213. 15, 10, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  214. 15, 12, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  215. 11, 12, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16,
  216. }
  217. };
  218.  
  219. #if 1
  220. void BTJ_JFE_TransDCT_Horiz(short *iblk, int *oblk)
  221. {
  222.     int ib0, ib1, ib2, ib3, ib4, ib5, ib6, ib7;
  223.     int ib0p7, ib1p6, ib2p5, ib3p4;
  224.     int ib0n7, ib1n6, ib2n5, ib3n4;
  225.     int ib0p7n3n4, ib1p6n2n5;
  226.  
  227.     ib0=iblk[0]-128;    ib1=iblk[1]-128;
  228.     ib2=iblk[2]-128;    ib3=iblk[3]-128;
  229.     ib4=iblk[4]-128;    ib5=iblk[5]-128;
  230.     ib6=iblk[6]-128;    ib7=iblk[7]-128;
  231.  
  232.     ib0p7=ib0+ib7;  ib1p6=ib1+ib6;
  233.     ib2p5=ib2+ib5;  ib3p4=ib3+ib4;
  234.     ib0n7=ib0-ib7;  ib1n6=ib1-ib6;
  235.     ib2n5=ib2-ib5;  ib3n4=ib3-ib4;
  236.  
  237.     oblk[0]=(ib0p7+ib1p6+ib2p5+ib3p4)*91;
  238.     oblk[4]=(ib0p7-ib1p6-ib2p5+ib3p4)*91;
  239.  
  240.     ib0p7n3n4=ib0p7-ib3p4;
  241.     ib1p6n2n5=ib1p6-ib2p5;
  242.     oblk[2]=ib0p7n3n4*118 +ib1p6n2n5* 49;
  243.     oblk[6]=ib0p7n3n4* 49 -ib1p6n2n5*118;
  244.  
  245.     oblk[1]=ib0n7*126 +ib1n6*106 +ib2n5* 71 +ib3n4* 25;
  246.     oblk[3]=ib0n7*106 -ib1n6* 25 -ib2n5*126 -ib3n4* 71;
  247.     oblk[5]=ib0n7* 71 -ib1n6*126 +ib2n5* 25 +ib3n4*106;
  248.     oblk[7]=ib0n7* 25 -ib1n6* 71 +ib2n5*106 -ib3n4*126;
  249. }
  250.  
  251. void BTJ_JFE_TransDCT_Vert2(int *iblk, short *oblk)
  252. {
  253.     int ib0, ib1, ib2, ib3, ib4, ib5, ib6, ib7;
  254.     int ob0, ob1, ob2, ob3, ob4, ob5, ob6, ob7;
  255.     int ib0p7, ib1p6, ib2p5, ib3p4;
  256.     int ib0n7, ib1n6, ib2n5, ib3n4;
  257.     int ib0p7n3n4, ib1p6n2n5;
  258.  
  259.     ib0=iblk[ 0];   ib1=iblk[ 8];
  260.     ib2=iblk[16];   ib3=iblk[24];
  261.     ib4=iblk[32];   ib5=iblk[40];
  262.     ib6=iblk[48];   ib7=iblk[56];
  263.  
  264.     ib0p7=ib0+ib7;  ib1p6=ib1+ib6;
  265.     ib2p5=ib2+ib5;  ib3p4=ib3+ib4;
  266.     ib0n7=ib0-ib7;  ib1n6=ib1-ib6;
  267.     ib2n5=ib2-ib5;  ib3n4=ib3-ib4;
  268.  
  269.     ob0=(ib0p7+ib1p6+ib2p5+ib3p4)*91;
  270.     ob4=(ib0p7-ib1p6-ib2p5+ib3p4)*91;
  271.  
  272.     ib0p7n3n4=ib0p7-ib3p4;
  273.     ib1p6n2n5=ib1p6-ib2p5;
  274.     ob2=ib0p7n3n4*118 +ib1p6n2n5* 49;
  275.     ob6=ib0p7n3n4* 49 -ib1p6n2n5*118;
  276.  
  277.     ob1=ib0n7*126 +ib1n6*106 +ib2n5* 71 +ib3n4* 25;
  278.     ob3=ib0n7*106 -ib1n6* 25 -ib2n5*126 -ib3n4* 71;
  279.     ob5=ib0n7* 71 -ib1n6*126 +ib2n5* 25 +ib3n4*106;
  280.     ob7=ib0n7* 25 -ib1n6* 71 +ib2n5*106 -ib3n4*126;
  281.  
  282.     oblk[ 0]=(ob0+32768)>>16;   oblk[ 8]=(ob1+32768)>>16;
  283.     oblk[16]=(ob2+32768)>>16;   oblk[24]=(ob3+32768)>>16;
  284.     oblk[32]=(ob4+32768)>>16;   oblk[40]=(ob5+32768)>>16;
  285.     oblk[48]=(ob6+32768)>>16;   oblk[56]=(ob7+32768)>>16;
  286. }
  287.  
  288. void BTJ_JFE_TransDCT(short *iblk, short *oblk)
  289. {
  290.     int s[DCTSZ2];
  291.     int i, j;
  292.  
  293.     BTJ_JFE_TransDCT_Horiz(iblk+ 0, s+ 0);
  294.     BTJ_JFE_TransDCT_Horiz(iblk+ 8, s+ 8);
  295.     BTJ_JFE_TransDCT_Horiz(iblk+16, s+16);
  296.     BTJ_JFE_TransDCT_Horiz(iblk+24, s+24);
  297.     BTJ_JFE_TransDCT_Horiz(iblk+32, s+32);
  298.     BTJ_JFE_TransDCT_Horiz(iblk+40, s+40);
  299.     BTJ_JFE_TransDCT_Horiz(iblk+48, s+48);
  300.     BTJ_JFE_TransDCT_Horiz(iblk+56, s+56);
  301.  
  302.     BTJ_JFE_TransDCT_Vert2(s+0, oblk+0);
  303.     BTJ_JFE_TransDCT_Vert2(s+1, oblk+1);
  304.     BTJ_JFE_TransDCT_Vert2(s+2, oblk+2);
  305.     BTJ_JFE_TransDCT_Vert2(s+3, oblk+3);
  306.     BTJ_JFE_TransDCT_Vert2(s+4, oblk+4);
  307.     BTJ_JFE_TransDCT_Vert2(s+5, oblk+5);
  308.     BTJ_JFE_TransDCT_Vert2(s+6, oblk+6);
  309.     BTJ_JFE_TransDCT_Vert2(s+7, oblk+7);
  310. }
  311. #endif
  312.  
  313. #if 1
  314. void BTJ_JFEH_QuantBlock(BGBBTJ_JFE_Context *ctx,
  315.     short *ibuf, short *obuf, int qid)
  316. {
  317.     short tbuf[64];
  318.     short *cs, *ct;
  319.     int *qt;
  320.     byte *zt;
  321.     int i, j;
  322.  
  323. #if 1
  324.     qt=ctx->qtfp[qid];
  325.  
  326. //  0,  1,  5,  6, 14, 15, 27, 28,
  327.     tbuf[ 0]=((ibuf[ 0]*qt[ 0])+2048)>>12;
  328.     tbuf[ 1]=((ibuf[ 1]*qt[ 1])+2048)>>12;
  329.     tbuf[ 5]=((ibuf[ 2]*qt[ 2])+2048)>>12;
  330.     tbuf[ 6]=((ibuf[ 3]*qt[ 3])+2048)>>12;
  331.     tbuf[14]=((ibuf[ 4]*qt[ 4])+2048)>>12;
  332.     tbuf[15]=((ibuf[ 5]*qt[ 5])+2048)>>12;
  333.     tbuf[27]=((ibuf[ 6]*qt[ 6])+2048)>>12;
  334.     tbuf[28]=((ibuf[ 7]*qt[ 7])+2048)>>12;
  335.  
  336. //  2,  4,  7, 13, 16, 26, 29, 42,
  337.     tbuf[ 2]=((ibuf[ 8]*qt[ 8])+2048)>>12;
  338.     tbuf[ 4]=((ibuf[ 9]*qt[ 9])+2048)>>12;
  339.     tbuf[ 7]=((ibuf[10]*qt[10])+2048)>>12;
  340.     tbuf[13]=((ibuf[11]*qt[11])+2048)>>12;
  341.     tbuf[16]=((ibuf[12]*qt[12])+2048)>>12;
  342.     tbuf[26]=((ibuf[13]*qt[13])+2048)>>12;
  343.     tbuf[29]=((ibuf[14]*qt[14])+2048)>>12;
  344.     tbuf[42]=((ibuf[15]*qt[15])+2048)>>12;
  345.  
  346. //  3,  8, 12, 17, 25, 30, 41, 43,
  347.     tbuf[ 3]=((ibuf[16]*qt[16])+2048)>>12;
  348.     tbuf[ 8]=((ibuf[17]*qt[17])+2048)>>12;
  349.     tbuf[12]=((ibuf[18]*qt[18])+2048)>>12;
  350.     tbuf[17]=((ibuf[19]*qt[19])+2048)>>12;
  351.     tbuf[25]=((ibuf[20]*qt[20])+2048)>>12;
  352.     tbuf[30]=((ibuf[21]*qt[21])+2048)>>12;
  353.     tbuf[41]=((ibuf[22]*qt[22])+2048)>>12;
  354.     tbuf[43]=((ibuf[23]*qt[23])+2048)>>12;
  355.  
  356. //  9, 11, 18, 24, 31, 40, 44, 53,
  357.     tbuf[ 9]=((ibuf[24]*qt[24])+2048)>>12;
  358.     tbuf[11]=((ibuf[25]*qt[25])+2048)>>12;
  359.     tbuf[18]=((ibuf[26]*qt[26])+2048)>>12;
  360.     tbuf[24]=((ibuf[27]*qt[27])+2048)>>12;
  361.     tbuf[31]=((ibuf[28]*qt[28])+2048)>>12;
  362.     tbuf[40]=((ibuf[29]*qt[29])+2048)>>12;
  363.     tbuf[44]=((ibuf[30]*qt[30])+2048)>>12;
  364.     tbuf[53]=((ibuf[31]*qt[31])+2048)>>12;
  365.  
  366. //  10, 19, 23, 32, 39, 45, 52, 54,
  367.     tbuf[10]=((ibuf[32]*qt[32])+2048)>>12;
  368.     tbuf[19]=((ibuf[33]*qt[33])+2048)>>12;
  369.     tbuf[23]=((ibuf[34]*qt[34])+2048)>>12;
  370.     tbuf[32]=((ibuf[35]*qt[35])+2048)>>12;
  371.     tbuf[39]=((ibuf[36]*qt[36])+2048)>>12;
  372.     tbuf[45]=((ibuf[37]*qt[37])+2048)>>12;
  373.     tbuf[52]=((ibuf[38]*qt[38])+2048)>>12;
  374.     tbuf[54]=((ibuf[39]*qt[39])+2048)>>12;
  375.  
  376. //  20, 22, 33, 38, 46, 51, 55, 60,
  377.     tbuf[20]=((ibuf[40]*qt[40])+2048)>>12;
  378.     tbuf[22]=((ibuf[41]*qt[41])+2048)>>12;
  379.     tbuf[33]=((ibuf[42]*qt[42])+2048)>>12;
  380.     tbuf[38]=((ibuf[43]*qt[43])+2048)>>12;
  381.     tbuf[46]=((ibuf[44]*qt[44])+2048)>>12;
  382.     tbuf[51]=((ibuf[45]*qt[45])+2048)>>12;
  383.     tbuf[55]=((ibuf[46]*qt[46])+2048)>>12;
  384.     tbuf[60]=((ibuf[47]*qt[47])+2048)>>12;
  385.  
  386. //  21, 34, 37, 47, 50, 56, 59, 61,
  387.     tbuf[21]=((ibuf[48]*qt[48])+2048)>>12;
  388.     tbuf[34]=((ibuf[49]*qt[49])+2048)>>12;
  389.     tbuf[37]=((ibuf[50]*qt[50])+2048)>>12;
  390.     tbuf[47]=((ibuf[51]*qt[51])+2048)>>12;
  391.     tbuf[50]=((ibuf[52]*qt[52])+2048)>>12;
  392.     tbuf[56]=((ibuf[53]*qt[53])+2048)>>12;
  393.     tbuf[59]=((ibuf[54]*qt[54])+2048)>>12;
  394.     tbuf[61]=((ibuf[55]*qt[55])+2048)>>12;
  395.  
  396. //  35, 36, 48, 49, 57, 58, 62, 63
  397.     tbuf[35]=((ibuf[56]*qt[56])+2048)>>12;
  398.     tbuf[36]=((ibuf[57]*qt[57])+2048)>>12;
  399.     tbuf[48]=((ibuf[58]*qt[58])+2048)>>12;
  400.     tbuf[49]=((ibuf[59]*qt[59])+2048)>>12;
  401.     tbuf[57]=((ibuf[60]*qt[60])+2048)>>12;
  402.     tbuf[58]=((ibuf[61]*qt[61])+2048)>>12;
  403.     tbuf[62]=((ibuf[62]*qt[62])+2048)>>12;
  404.     tbuf[63]=((ibuf[63]*qt[63])+2048)>>12;
  405.  
  406.     memcpy(obuf, tbuf, 64*sizeof(short));
  407. #endif
  408.  
  409. #if 0
  410.     cs=ibuf; qt=ctx->qtfp[qid]; zt=btj_jfe_zigzag;
  411.     for(i=0; i<8; i++)
  412.     {
  413.         tbuf[zt[0]]=((cs[0]*qt[0])+2048)>>12;
  414.         tbuf[zt[1]]=((cs[1]*qt[1])+2048)>>12;
  415.         tbuf[zt[2]]=((cs[2]*qt[2])+2048)>>12;
  416.         tbuf[zt[3]]=((cs[3]*qt[3])+2048)>>12;
  417.         tbuf[zt[4]]=((cs[4]*qt[4])+2048)>>12;
  418.         tbuf[zt[5]]=((cs[5]*qt[5])+2048)>>12;
  419.         tbuf[zt[6]]=((cs[6]*qt[6])+2048)>>12;
  420.         tbuf[zt[7]]=((cs[7]*qt[7])+2048)>>12;
  421.         cs+=8; qt+=8; zt+=8;
  422.     }
  423.     memcpy(obuf, tbuf, 64*sizeof(short));
  424. #endif
  425. }
  426. #endif
  427.  
  428. void BTJ_JFEH_SetupQuantTabDivFP(BGBBTJ_JFE_Context *ctx, int qid)
  429. {
  430.     int i;
  431.    
  432.     for(i=0; i<64; i++)
  433.         ctx->qtfp[qid][i]=4096.0/ctx->qt[qid][i]+0.5;
  434. }
  435.  
  436. //Encoder
  437. #if 1
  438. void BTJ_JFEH_WriteBit(BGBBTJ_JFE_Context *ctx, int i)
  439. {
  440.     ctx->pos--;
  441.     ctx->win|=i<<ctx->pos;
  442.     if(ctx->pos<=24)
  443.     {
  444.         i=(ctx->win>>24)&0xFF;
  445.         *ctx->ct++=i;
  446.         if(i==0xFF)*ctx->ct++=0x00;
  447.         ctx->win<<=8;
  448.         ctx->pos+=8;
  449.     }
  450. }
  451.  
  452. void BTJ_JFEH_WriteNBits(BGBBTJ_JFE_Context *ctx, int i, int n)
  453. {
  454. //  i&=(1<<n)-1;
  455.  
  456.     ctx->pos-=n;
  457.     ctx->win|=i<<ctx->pos;
  458.     while(ctx->pos<=24)
  459.     {
  460.         i=(ctx->win>>24)&0xFF;
  461.         *ctx->ct++=i;
  462.         if(i==0xFF)*ctx->ct++=0x00;
  463.         ctx->win<<=8;
  464.         ctx->pos+=8;
  465.     }
  466. }
  467.  
  468. void BTJ_JFEH_FlushBits(BGBBTJ_JFE_Context *ctx)
  469. {
  470.     int i;
  471.     while(ctx->pos<32)
  472.     {
  473.         i=(ctx->win>>24)&0xFF;
  474.         *ctx->ct++=i;
  475.         if(i==0xFF)*ctx->ct++=0x00;
  476.         ctx->win<<=8;
  477.         ctx->pos+=8;
  478.     }
  479. }
  480. #endif
  481.  
  482. void BTJ_JFEH_EncodeSymbol(BGBBTJ_JFE_Context *ctx, int tab, int v)
  483. {
  484.     BTJ_JFEH_WriteNBits(ctx, btj_jfeh_code[tab][v], btj_jfeh_len[tab][v]);
  485. }
  486.  
  487. void BTJ_JFEH_EncodeVal(BGBBTJ_JFE_Context *ctx, int tab, int z, int v)
  488. {
  489.     int i, j, k;
  490.  
  491.     if(!v) { BTJ_JFEH_EncodeSymbol(ctx, tab, z<<4); return; }
  492.  
  493.     if(v>0)
  494.     {
  495.         i=1;
  496.         while(v>=(1<<i))i++;
  497.         BTJ_JFEH_EncodeSymbol(ctx, tab, (z<<4)|i);
  498.         BTJ_JFEH_WriteNBits(ctx, v, i);
  499.         return;
  500.     }
  501.  
  502.     i=1; j=-v; while(j>=(1<<i))i++;
  503.     BTJ_JFEH_EncodeSymbol(ctx, tab, (z<<4)|i);
  504.  
  505.     k=(1<<i)-(j+1);
  506.     BTJ_JFEH_WriteNBits(ctx, k, i);
  507.     return;
  508. }
  509.  
  510. void BTJ_JFEH_EncodeBlock(BGBBTJ_JFE_Context *ctx, short *buf, int dctab, int actab)
  511. {
  512.     int i, j, k;
  513.  
  514.     BTJ_JFEH_EncodeVal(ctx, dctab, 0, buf[0]);
  515.  
  516.     i=1;
  517.     while(i<64)
  518.     {
  519.         j=buf[i];
  520.         if(j)
  521.         {
  522.             BTJ_JFEH_EncodeVal(ctx, actab, 0, j);
  523.             i++;
  524.             continue;
  525.         }
  526.  
  527.         j=i+1;
  528.         while(1)
  529.         {
  530.             if(j>=64)
  531.             {
  532.                 BTJ_JFEH_EncodeSymbol(ctx, actab, 0);
  533.                 return;
  534.             }
  535.             if(buf[j])break;
  536.             j++;
  537.         }
  538.  
  539.         k=j-i;
  540. //      while((j-i)>15)
  541.         while(k>15)
  542.         {
  543.             BTJ_JFEH_EncodeSymbol(ctx, actab, 15<<4);
  544.             i+=16; k=j-i;
  545.         }
  546.  
  547. //      k=j-i;
  548.         BTJ_JFEH_EncodeVal(ctx, actab, k, buf[j]);
  549.         i=j+1;
  550.     }
  551. }
  552.  
  553. void BTJ_JFE_GetImgBlk2Y(short *blk, int xo, int yo,
  554.     byte *img, int xs, int ys, int xs2, int ys2, int ssz)
  555. {
  556.     byte *ib;
  557.     short *ob;
  558.     int sx0, sx1, sx2, sx3, sx4, sx5, sx6, sx7;
  559.     int i, j, k;
  560.  
  561.     if(((xo+8)>xs) || ((ys-yo-8)<0))
  562.     {
  563.         //goes off edge of image
  564.         for(i=0; i<8; i++)
  565.             for(j=0; j<8; j++)
  566.         {
  567.             k=(ys-(yo+i)-1);
  568.             if(((xo+j)>=xs) || (k<0))
  569.                 { blk[i*8+j]=0; continue; }
  570.             ib=img+((k*xs)+xo+j)*ssz;
  571.             blk[i*8+j]=(19595*ib[0] + 38470*ib[1] +
  572.                 7471*ib[2] + 32768)>>16;
  573.         }
  574.  
  575.         return;
  576.     }
  577.  
  578.     if(ssz==4)
  579.     {
  580.         for(i=0; i<8; i++)
  581.         {
  582.             ib=img+(((ys-(yo+i)-1)*xs)+xo)*ssz;
  583.             ob=blk+(i*8);
  584.             ob[0]=(19595*ib[ 0] + 38470*ib[ 1] + 7471*ib[ 2] + 32768)>>16;
  585.             ob[1]=(19595*ib[ 4] + 38470*ib[ 5] + 7471*ib[ 6] + 32768)>>16;
  586.             ob[2]=(19595*ib[ 8] + 38470*ib[ 9] + 7471*ib[10] + 32768)>>16;
  587.             ob[3]=(19595*ib[12] + 38470*ib[13] + 7471*ib[14] + 32768)>>16;
  588.             ob[4]=(19595*ib[16] + 38470*ib[17] + 7471*ib[18] + 32768)>>16;
  589.             ob[5]=(19595*ib[20] + 38470*ib[21] + 7471*ib[22] + 32768)>>16;
  590.             ob[6]=(19595*ib[24] + 38470*ib[25] + 7471*ib[26] + 32768)>>16;
  591.             ob[7]=(19595*ib[28] + 38470*ib[29] + 7471*ib[30] + 32768)>>16;
  592.         }
  593.     }else
  594.     {
  595.         sx0=0;     sx1=ssz;   sx2=2*ssz; sx3=3*ssz;
  596.         sx4=4*ssz; sx5=5*ssz; sx6=6*ssz; sx7=7*ssz;
  597.         for(i=0; i<8; i++)
  598.         {
  599.             ib=img+(((ys-(yo+i)-1)*xs)+xo)*ssz;
  600.             ob=blk+(i*8);
  601.             ob[0]=(19595*ib[sx0+0] + 38470*ib[sx0+1] + 7471*ib[sx0+2] + 32768)>>16;
  602.             ob[1]=(19595*ib[sx1+0] + 38470*ib[sx1+1] + 7471*ib[sx1+2] + 32768)>>16;
  603.             ob[2]=(19595*ib[sx2+0] + 38470*ib[sx2+1] + 7471*ib[sx2+2] + 32768)>>16;
  604.             ob[3]=(19595*ib[sx3+0] + 38470*ib[sx3+1] + 7471*ib[sx3+2] + 32768)>>16;
  605.             ob[4]=(19595*ib[sx4+0] + 38470*ib[sx4+1] + 7471*ib[sx4+2] + 32768)>>16;
  606.             ob[5]=(19595*ib[sx5+0] + 38470*ib[sx5+1] + 7471*ib[sx5+2] + 32768)>>16;
  607.             ob[6]=(19595*ib[sx6+0] + 38470*ib[sx6+1] + 7471*ib[sx6+2] + 32768)>>16;
  608.             ob[7]=(19595*ib[sx7+0] + 38470*ib[sx7+1] + 7471*ib[sx7+2] + 32768)>>16;
  609.         }
  610.     }
  611. }
  612.  
  613. void BTJ_JFE_GetImgBlk2UV_RGB(
  614.     short *ublk, short *vblk, int xo, int yo,
  615.     byte *img, int xs, int ys, int xs2, int ys2, int ssz)
  616. {
  617.     byte *ib0, *ib1;
  618.     short *obu, *obv;
  619.     int cr, cg, cb, cu, cv;
  620.     int i, j, k;
  621.  
  622. //  if(((xo+16)>xs) || ((2*ys2-yo-1)>ys))
  623.     if(((xo+16)>xs) || ((ys-yo-16)<0))
  624.     {
  625.         //goes off edge of image
  626.         for(i=0; i<8; i++)
  627.             for(j=0; j<8; j++)
  628.         {
  629.             if((xo+2*j+1)>=xs)
  630.                 { ublk[i*8+j]=0; vblk[i*8+j]=0; continue; }
  631.             k=(ys-(yo+i*2+1)-1);
  632. //          if(k>=ys)
  633.             if(k<0)
  634.                 { ublk[i*8+j]=0; vblk[i*8+j]=0; continue; }
  635.  
  636.             ib0=img+(((ys-(yo+i*2+0)-1)*xs)+xo+j*2)*ssz;
  637.             ib1=img+(((ys-(yo+i*2+1)-1)*xs)+xo+j*2)*ssz;
  638.             obu=ublk+(i*8)+j; obv=vblk+(i*8)+j;
  639.  
  640.             cr=(ib0[0]+ib0[ssz+0]+ib1[0]+ib1[ssz+0])>>2;
  641.             cg=(ib0[1]+ib0[ssz+1]+ib1[1]+ib1[ssz+1])>>2;
  642.             cb=(ib0[2]+ib0[ssz+2]+ib1[2]+ib1[ssz+2])>>2;
  643.             cu=-11056*cr -21712*cg +32768*cb;
  644.             cv= 32768*cr -27440*cg - 5328*cb;
  645.             obu[0]=((cu+32768)>>16)+128;
  646.             obv[0]=((cv+32768)>>16)+128;
  647.         }
  648.  
  649.         return;
  650.     }
  651.  
  652.     if(ssz==4)
  653.     {
  654.         for(i=0; i<8; i++)
  655.         {
  656.             ib0=img+(((ys-(yo+i*2+0)-1)*xs)+xo)*ssz;
  657.             ib1=img+(((ys-(yo+i*2+1)-1)*xs)+xo)*ssz;
  658.             obu=ublk+(i*8); obv=vblk+(i*8);
  659.  
  660.             for(j=0; j<2; j++)
  661.             {
  662.                 cr=(ib0[0]+ib0[4]+ib1[0]+ib1[4])>>2;
  663.                 cg=(ib0[1]+ib0[5]+ib1[1]+ib1[5])>>2;
  664.                 cb=(ib0[2]+ib0[6]+ib1[2]+ib1[6])>>2;
  665.                 cu=-11056*cr -21712*cg +32768*cb;
  666.                 cv= 32768*cr -27440*cg - 5328*cb;
  667.                 obu[0]=((cu+32768)>>16)+128;
  668.                 obv[0]=((cv+32768)>>16)+128;
  669.  
  670.                 cr=(ib0[ 8]+ib0[12]+ib1[ 8]+ib1[12])>>2;
  671.                 cg=(ib0[ 9]+ib0[13]+ib1[ 9]+ib1[13])>>2;
  672.                 cb=(ib0[10]+ib0[14]+ib1[10]+ib1[14])>>2;
  673.                 cu=-11056*cr -21712*cg +32768*cb;
  674.                 cv= 32768*cr -27440*cg - 5328*cb;
  675.                 obu[1]=((cu+32768)>>16)+128;
  676.                 obv[1]=((cv+32768)>>16)+128;
  677.  
  678.                 cr=(ib0[16]+ib0[20]+ib1[16]+ib1[20])>>2;
  679.                 cg=(ib0[17]+ib0[21]+ib1[17]+ib1[21])>>2;
  680.                 cb=(ib0[18]+ib0[22]+ib1[18]+ib1[22])>>2;
  681.                 cu=-11056*cr -21712*cg +32768*cb;
  682.                 cv= 32768*cr -27440*cg - 5328*cb;
  683.                 obu[2]=((cu+32768)>>16)+128;
  684.                 obv[2]=((cv+32768)>>16)+128;
  685.  
  686.                 cr=(ib0[24]+ib0[28]+ib1[24]+ib1[28])>>2;
  687.                 cg=(ib0[25]+ib0[29]+ib1[25]+ib1[29])>>2;
  688.                 cb=(ib0[26]+ib0[30]+ib1[26]+ib1[30])>>2;
  689.                 cu=-11056*cr -21712*cg +32768*cb;
  690.                 cv= 32768*cr -27440*cg - 5328*cb;
  691.                 obu[3]=((cu+32768)>>16)+128;
  692.                 obv[3]=((cv+32768)>>16)+128;
  693.  
  694.                 ib0+=32; ib1+=32; obu+=4; obv+=4;
  695.             }
  696.         }
  697.     }else
  698.     {
  699.         for(i=0; i<8; i++)
  700.         {
  701.             ib0=img+(((ys-(yo+i*2+0)-1)*xs)+xo)*ssz;
  702.             ib1=img+(((ys-(yo+i*2+1)-1)*xs)+xo)*ssz;
  703.             obu=ublk+(i*8); obv=vblk+(i*8);
  704.  
  705.             for(j=0; j<8; j++)
  706.             {
  707.                 cr=(ib0[0]+ib0[ssz+0]+ib1[0]+ib1[ssz+0])>>2;
  708.                 cg=(ib0[1]+ib0[ssz+1]+ib1[1]+ib1[ssz+1])>>2;
  709.                 cb=(ib0[2]+ib0[ssz+2]+ib1[2]+ib1[ssz+2])>>2;
  710.                 cu=-11056*cr -21712*cg +32768*cb;
  711.                 cv= 32768*cr -27440*cg - 5328*cb;
  712.                 obu[0]=((cu+32768)>>16)+128;
  713.                 obv[0]=((cv+32768)>>16)+128;
  714.                 ib0+=2*ssz; ib1+=2*ssz; obu++; obv++;
  715.             }
  716.         }
  717.     }
  718. }
  719.  
  720. void BTJ_JFE_GetImgBlk2UV_BGR(
  721.     short *ublk, short *vblk, int xo, int yo,
  722.     byte *img, int xs, int ys, int xs2, int ys2, int ssz)
  723. {
  724.     byte *ib0, *ib1;
  725.     short *obu, *obv;
  726.     int cr, cg, cb, cu, cv;
  727.     int i, j, k;
  728.  
  729. //  if(((xo+16)>xs) || ((2*ys2-yo-1)>ys))
  730.     if(((xo+16)>xs) || ((ys-yo-16)<0))
  731.     {
  732.         //goes off edge of image
  733.         for(i=0; i<8; i++)
  734.             for(j=0; j<8; j++)
  735.         {
  736.             k=(ys-(yo+i*2+1)-1);
  737.             if(((xo+2*j+1)>=xs) || (k<0))
  738.                 { ublk[i*8+j]=0; vblk[i*8+j]=0; continue; }
  739.  
  740.             ib0=img+(((ys-(yo+i*2+0)-1)*xs)+xo+j*2)*ssz;
  741.             ib1=img+(((ys-(yo+i*2+1)-1)*xs)+xo+j*2)*ssz;
  742.             obu=ublk+(i*8)+j; obv=vblk+(i*8)+j;
  743.  
  744.             cb=(ib0[0]+ib0[ssz+0]+ib1[0]+ib1[ssz+0])>>2;
  745.             cg=(ib0[1]+ib0[ssz+1]+ib1[1]+ib1[ssz+1])>>2;
  746.             cr=(ib0[2]+ib0[ssz+2]+ib1[2]+ib1[ssz+2])>>2;
  747.             cu=-11056*cr -21712*cg +32768*cb;
  748.             cv= 32768*cr -27440*cg - 5328*cb;
  749.             obu[0]=((cu+32768)>>16)+128;
  750.             obv[0]=((cv+32768)>>16)+128;
  751.         }
  752.  
  753.         return;
  754.     }
  755.  
  756.     if(ssz==4)
  757.     {
  758.         for(i=0; i<8; i++)
  759.         {
  760.             ib0=img+(((ys-(yo+i*2+0)-1)*xs)+xo)*ssz;
  761.             ib1=img+(((ys-(yo+i*2+1)-1)*xs)+xo)*ssz;
  762.             obu=ublk+(i*8); obv=vblk+(i*8);
  763.  
  764.             for(j=0; j<2; j++)
  765.             {
  766.                 cb=(ib0[0]+ib0[4]+ib1[0]+ib1[4])>>2;
  767.                 cg=(ib0[1]+ib0[5]+ib1[1]+ib1[5])>>2;
  768.                 cr=(ib0[2]+ib0[6]+ib1[2]+ib1[6])>>2;
  769.                 cu=-11056*cr -21712*cg +32768*cb;
  770.                 cv= 32768*cr -27440*cg - 5328*cb;
  771.                 obu[0]=((cu+32768)>>16)+128;
  772.                 obv[0]=((cv+32768)>>16)+128;
  773.  
  774.                 cb=(ib0[ 8]+ib0[12]+ib1[ 8]+ib1[12])>>2;
  775.                 cg=(ib0[ 9]+ib0[13]+ib1[ 9]+ib1[13])>>2;
  776.                 cr=(ib0[10]+ib0[14]+ib1[10]+ib1[14])>>2;
  777.                 cu=-11056*cr -21712*cg +32768*cb;
  778.                 cv= 32768*cr -27440*cg - 5328*cb;
  779.                 obu[1]=((cu+32768)>>16)+128;
  780.                 obv[1]=((cv+32768)>>16)+128;
  781.  
  782.                 cb=(ib0[16]+ib0[20]+ib1[16]+ib1[20])>>2;
  783.                 cg=(ib0[17]+ib0[21]+ib1[17]+ib1[21])>>2;
  784.                 cr=(ib0[18]+ib0[22]+ib1[18]+ib1[22])>>2;
  785.                 cu=-11056*cr -21712*cg +32768*cb;
  786.                 cv= 32768*cr -27440*cg - 5328*cb;
  787.                 obu[2]=((cu+32768)>>16)+128;
  788.                 obv[2]=((cv+32768)>>16)+128;
  789.  
  790.                 cb=(ib0[24]+ib0[28]+ib1[24]+ib1[28])>>2;
  791.                 cg=(ib0[25]+ib0[29]+ib1[25]+ib1[29])>>2;
  792.                 cr=(ib0[26]+ib0[30]+ib1[26]+ib1[30])>>2;
  793.                 cu=-11056*cr -21712*cg +32768*cb;
  794.                 cv= 32768*cr -27440*cg - 5328*cb;
  795.                 obu[3]=((cu+32768)>>16)+128;
  796.                 obv[3]=((cv+32768)>>16)+128;
  797.  
  798.                 ib0+=32; ib1+=32; obu+=4; obv+=4;
  799.             }
  800.         }
  801.     }else
  802.     {
  803.         for(i=0; i<8; i++)
  804.         {
  805.             ib0=img+(((ys-(yo+i*2+0)-1)*xs)+xo)*ssz;
  806.             ib1=img+(((ys-(yo+i*2+1)-1)*xs)+xo)*ssz;
  807.             obu=ublk+(i*8); obv=vblk+(i*8);
  808.  
  809.             for(j=0; j<8; j++)
  810.             {
  811.                 cb=(ib0[0]+ib0[ssz+0]+ib1[0]+ib1[ssz+0])>>2;
  812.                 cg=(ib0[1]+ib0[ssz+1]+ib1[1]+ib1[ssz+1])>>2;
  813.                 cr=(ib0[2]+ib0[ssz+2]+ib1[2]+ib1[ssz+2])>>2;
  814.                 cu=-11056*cr -21712*cg +32768*cb;
  815.                 cv= 32768*cr -27440*cg - 5328*cb;
  816.                 obu[0]=((cu+32768)>>16)+128;
  817.                 obv[0]=((cv+32768)>>16)+128;
  818.                 ib0+=2*ssz; ib1+=2*ssz; obu++; obv++;
  819.             }
  820.         }
  821.     }
  822. }
  823.  
  824. void BTJ_JFE_FilterImageDCT2(byte *ibuf,
  825.     short *ybuf, short *ubuf, short *vbuf, int xs, int ys,
  826.     int qf, int pf)
  827. {
  828.     short ublk[DCTSZ2], vblk[DCTSZ2];
  829.     short blk[DCTSZ2];
  830.     int xs2, ys2, xs3, ys3, ssz;
  831.     int i, j, k, l;
  832.  
  833.     xs2=((xs+7)/8)*8;
  834.     ys2=((ys+7)/8)*8;
  835.     xs3=((xs+15)/16)*8;
  836.     ys3=((ys+15)/16)*8;
  837.  
  838.     if((pf==BGBBTJ_JPG_RGBA) || (pf==BGBBTJ_JPG_BGRA) ||
  839.         (pf==BGBBTJ_JPG_RGB) || (pf==BGBBTJ_JPG_BGR))
  840.     {
  841.         ssz=4;
  842.         if((pf==BGBBTJ_JPG_RGB) || (pf==BGBBTJ_JPG_BGR))ssz=3;
  843.    
  844.         k=0;
  845.         for(i=0; i<(ys2/DCTSZ); i++)
  846.             for(j=0; j<(xs2/DCTSZ); j++)
  847.         {
  848.             BTJ_JFE_GetImgBlk2Y(blk, j*DCTSZ, i*DCTSZ,
  849.                 ibuf, xs, ys, xs2, ys2, ssz);
  850.             BTJ_JFE_TransDCT(blk, ybuf+k*DCTSZ2);
  851.             k++;
  852.         }
  853.     }else if((pf==BGBBTJ_JPG_ARGB) || (pf==BGBBTJ_JPG_ABGR))
  854.     {
  855.         ssz=4;
  856.         k=0;
  857.         for(i=0; i<(ys2/DCTSZ); i++)
  858.             for(j=0; j<(xs2/DCTSZ); j++)
  859.         {
  860.             BTJ_JFE_GetImgBlk2Y(blk, j*DCTSZ, i*DCTSZ,
  861.                 ibuf+1, xs, ys, xs2, ys2, ssz);
  862.             BTJ_JFE_TransDCT(blk, ybuf+k*DCTSZ2);
  863.             k++;
  864.         }
  865.     }
  866.  
  867.     if((pf==BGBBTJ_JPG_RGBA) || (pf==BGBBTJ_JPG_RGB))
  868.     {
  869.         ssz=4;
  870.         if(pf==BGBBTJ_JPG_RGB)ssz=3;
  871.  
  872.         k=0;
  873.         for(i=0; i<(ys3/DCTSZ); i++)
  874.             for(j=0; j<(xs3/DCTSZ); j++)
  875.         {
  876.             BTJ_JFE_GetImgBlk2UV_RGB(ublk, vblk,
  877.                 2*j*DCTSZ, 2*i*DCTSZ, ibuf, xs, ys, xs3, ys3, ssz);
  878.             BTJ_JFE_TransDCT(ublk, ubuf+k*DCTSZ2);
  879.             BTJ_JFE_TransDCT(vblk, vbuf+k*DCTSZ2);
  880.             k++;
  881.         }
  882.     }else if((pf==BGBBTJ_JPG_BGRA) || (pf==BGBBTJ_JPG_BGR))
  883.     {
  884.         ssz=4;
  885.         if(pf==BGBBTJ_JPG_BGR)ssz=3;
  886.  
  887.         k=0;
  888.         for(i=0; i<(ys3/DCTSZ); i++)
  889.             for(j=0; j<(xs3/DCTSZ); j++)
  890.         {
  891.             BTJ_JFE_GetImgBlk2UV_BGR(ublk, vblk,
  892.                 2*j*DCTSZ, 2*i*DCTSZ, ibuf, xs, ys, xs3, ys3, ssz);
  893.             BTJ_JFE_TransDCT(ublk, ubuf+k*DCTSZ2);
  894.             BTJ_JFE_TransDCT(vblk, vbuf+k*DCTSZ2);
  895.             k++;
  896.         }
  897.     }else if(pf==BGBBTJ_JPG_ARGB)
  898.     {
  899.         ssz=4; k=0;
  900.         for(i=0; i<(ys3/DCTSZ); i++)
  901.             for(j=0; j<(xs3/DCTSZ); j++)
  902.         {
  903.             BTJ_JFE_GetImgBlk2UV_RGB(ublk, vblk,
  904.                 2*j*DCTSZ, 2*i*DCTSZ, ibuf+1, xs, ys, xs3, ys3, ssz);
  905.             BTJ_JFE_TransDCT(ublk, ubuf+k*DCTSZ2);
  906.             BTJ_JFE_TransDCT(vblk, vbuf+k*DCTSZ2);
  907.             k++;
  908.         }
  909.     }else if(pf==BGBBTJ_JPG_ABGR)
  910.     {
  911.         ssz=4; k=0;
  912.         for(i=0; i<(ys3/DCTSZ); i++)
  913.             for(j=0; j<(xs3/DCTSZ); j++)
  914.         {
  915.             BTJ_JFE_GetImgBlk2UV_BGR(ublk, vblk,
  916.                 2*j*DCTSZ, 2*i*DCTSZ, ibuf+1, xs, ys, xs3, ys3, ssz);
  917.             BTJ_JFE_TransDCT(ublk, ubuf+k*DCTSZ2);
  918.             BTJ_JFE_TransDCT(vblk, vbuf+k*DCTSZ2);
  919.             k++;
  920.         }
  921.     }
  922. }
  923.  
  924.  
  925. void BTJ_JFE_EmitDQT(BGBBTJ_JFE_Context *ctx, int n)
  926. {
  927.     int i;
  928.  
  929.     *ctx->ct++=0xFF;
  930.     *ctx->ct++=JPG_DQT;
  931.  
  932.     i=64+3;
  933.     *ctx->ct++=i>>8;
  934.     *ctx->ct++=i&0xFF;
  935.  
  936.     *ctx->ct++=n;
  937.     for(i=0; i<64; i++)
  938.         *ctx->ct++=ctx->qt[n][btj_jfe_zigzag2[i]];
  939. }
  940.  
  941. void BTJ_JFE_EmitSOF(BGBBTJ_JFE_Context *ctx, int xs, int ys)
  942. {
  943.     int i;
  944.  
  945.     *ctx->ct++=0xFF;
  946.     *ctx->ct++=JPG_SOF0;
  947.  
  948.     i=8+3*3;
  949.     *ctx->ct++=i>>8;    //Lf
  950.     *ctx->ct++=i&0xFF;
  951.  
  952.     *ctx->ct++=8;   //P
  953.  
  954.     *ctx->ct++=ys>>8;   //Y
  955.     *ctx->ct++=ys&0xFF; //Y
  956.     *ctx->ct++=xs>>8;   //X
  957.     *ctx->ct++=xs&0xFF; //X
  958.  
  959.     *ctx->ct++=3;   //Nf
  960.  
  961.     *ctx->ct++=1;   //Ci
  962.     *ctx->ct++=0x22;    //Hi Vi
  963.     *ctx->ct++=0;   //Tqi
  964.     *ctx->ct++=2;   //Ci
  965.     *ctx->ct++=0x11;    //Hi Vi
  966.     *ctx->ct++=1;   //Tqi
  967.     *ctx->ct++=3;   //Ci
  968.     *ctx->ct++=0x11;    //Hi Vi
  969.     *ctx->ct++=1;   //Tqi
  970. }
  971.  
  972. void BTJ_JFE_EmitSOS(BGBBTJ_JFE_Context *ctx)
  973. {
  974.     int i;
  975.  
  976.     *ctx->ct++=0xFF;
  977.     *ctx->ct++=JPG_SOS;
  978.  
  979.     i=6+3*2;
  980.     *ctx->ct++=i>>8;    //Lf
  981.     *ctx->ct++=i&0xFF;
  982.  
  983.     *ctx->ct++=3;   //Ns
  984.  
  985.     *ctx->ct++=1;   //Csi
  986.     *ctx->ct++=0x00;    //Tdi Tai
  987.     *ctx->ct++=2;   //Csi
  988.     *ctx->ct++=0x11;    //Tdi Tai
  989.     *ctx->ct++=3;   //Csi
  990.     *ctx->ct++=0x11;    //Tdi Tai
  991.  
  992.     *ctx->ct++=0;   //Ss
  993.     *ctx->ct++=63;  //Se
  994.     *ctx->ct++=0x00;    //Ah Al
  995. }
  996.  
  997. void BTJ_JFE_EmitDHT(BGBBTJ_JFE_Context *ctx, int tab)
  998. {
  999.     byte *p;
  1000.     int i, j, k;
  1001.  
  1002.     *ctx->ct++=0xFF;
  1003.     *ctx->ct++=JPG_DHT;
  1004.  
  1005.     p=ctx->ct;
  1006.     *ctx->ct++=0;   //Lf
  1007.     *ctx->ct++=0;
  1008.  
  1009.     i=(tab/2)|((tab&1)<<4);
  1010.     *ctx->ct++=i;   //Tc Th
  1011.  
  1012. //  tab<<=8;
  1013.     for(i=1; i<=16; i++)
  1014.     {
  1015.         k=0;
  1016.         for(j=0; j<256; j++)
  1017.             if(btj_jfeh_len[tab][j]==i)
  1018.                 k++;
  1019.         *ctx->ct++=k;   //Li
  1020.     }
  1021.  
  1022.     k=0;
  1023.     for(i=1; i<=16; i++)
  1024.     {
  1025.         k<<=1;
  1026.         for(j=0; j<256; j++)
  1027.             if(btj_jfeh_len[tab][j]==i)
  1028.         {
  1029.             *ctx->ct++=j;   //Vi
  1030.             btj_jfeh_code[tab][j]=k++;
  1031.         }
  1032.     }
  1033.  
  1034. //  printf("DHT %04X\n", k);
  1035.  
  1036.     i=ctx->ct-p;
  1037.     p[0]=i>>8;  //Lf
  1038.     p[1]=i&0xFF;
  1039. }
  1040.  
  1041. void BTJ_JFE_EmitDHT2(BGBBTJ_JFE_Context *ctx)
  1042. {
  1043.     static int init=0;
  1044.     static byte *tab=NULL;
  1045.     static int tabsz;
  1046.     byte *ct0;
  1047.     int i;
  1048.    
  1049.     if(!init)
  1050.     {
  1051.         ct0=ctx->ct;
  1052.         BTJ_JFE_EmitDHT(ctx, 0);
  1053.         BTJ_JFE_EmitDHT(ctx, 1);
  1054.         BTJ_JFE_EmitDHT(ctx, 2);
  1055.         BTJ_JFE_EmitDHT(ctx, 3);
  1056.         i=ctx->ct-ct0;
  1057.         tab=malloc(i);
  1058.         tabsz=i;
  1059.         memcpy(tab, ct0, i);
  1060.         init=1;
  1061.         return;
  1062.     }
  1063.    
  1064.     memcpy(ctx->ct, tab, tabsz);
  1065.     ctx->ct+=tabsz;
  1066. }
  1067.  
  1068. static const int pdjpg_ijg_qtab_y[64] = {
  1069. 16, 11, 10, 16,  24,  40,  51,  61,
  1070. 12, 12, 14, 19,  26,  58,  60,  55,
  1071. 14, 13, 16, 24,  40,  57,  69,  56,
  1072. 14, 17, 22, 29,  51,  87,  80,  62,
  1073. 18, 22, 37, 56,  68, 109, 103,  77,
  1074. 24, 35, 55, 64,  81, 104, 113,  92,
  1075. 49, 64, 78, 87, 103, 121, 120, 101,
  1076. 72, 92, 95, 98, 112, 100, 103,  99};
  1077.  
  1078. static const int pdjpg_ijg_qtab_uv[64] = {
  1079. 99, 99, 99, 99, 99, 99, 99, 99,
  1080. 99, 99, 99, 99, 99, 99, 99, 99,
  1081. 99, 99, 99, 99, 99, 99, 99, 99,
  1082. 99, 99, 99, 99, 99, 99, 99, 99,
  1083. 47, 66, 99, 99, 99, 99, 99, 99,
  1084. 24, 26, 56, 99, 99, 99, 99, 99,
  1085. 18, 21, 26, 66, 99, 99, 99, 99,
  1086. 17, 18, 24, 47, 99, 99, 99, 99};
  1087.  
  1088. void BTJ_JFE_MakeQuantTabFastIJG_Y(byte *tab, float qf)
  1089. {
  1090.     double s, q;
  1091.     int i, j;
  1092.  
  1093.     q=(qf*100);
  1094.     s=(q<50)?5000/q:(200-2*q);
  1095.     for(i=0; i<64; i++)
  1096.     {
  1097.         j=(int)((s * pdjpg_ijg_qtab_y[i]+50)/100);
  1098.         j=(j<1)?1:((j<256)?j:255);
  1099.         tab[i]=j;
  1100.     }
  1101. }
  1102.  
  1103. void BTJ_JFE_MakeQuantTabFastIJG_UV(byte *tab, float qf)
  1104. {
  1105.     double s, q;
  1106.     int i, j;
  1107.  
  1108.     q=(qf*100);
  1109.     s=(q<50)?5000/q:(200-2*q);
  1110.     for(i=0; i<64; i++)
  1111.     {
  1112.         j=(int)((s*pdjpg_ijg_qtab_uv[i]+50)/100);
  1113.         j=(j<1)?1:((j<256)?j:255);
  1114.         tab[i]=j;
  1115.     }
  1116. }
  1117.  
  1118.  
  1119. BGBBTJ_API BGBBTJ_JFE_Context *BTJ_JFE_AllocContext(void)
  1120. {
  1121.     BGBBTJ_JFE_Context *ctx;
  1122.    
  1123.     ctx=gcalloc(sizeof(BGBBTJ_JFE_Context));
  1124.     return(ctx);
  1125. }
  1126.  
  1127. BGBBTJ_API void BTJ_JFE_FreeContext(
  1128.     BGBBTJ_JFE_Context *ctx)
  1129. {
  1130.     gcfree(ctx);
  1131. }
  1132.  
  1133. BGBBTJ_API void BTJ_JFE_FlushEncodeFast(
  1134.     BGBBTJ_JFE_Context *ctx)
  1135. {
  1136. }
  1137.  
  1138. BGBBTJ_API int BTJ_JFE_EncodeFast(byte *ibuf, byte *obuf,
  1139.     int xs, int ys, int qf, int pf)
  1140. {
  1141.     static BGBBTJ_JFE_Context cctx;
  1142.     return(BTJ_JFE_EncodeFastCtx(&cctx, ibuf, obuf,
  1143.         xs, ys, qf, pf));
  1144. }
  1145.  
  1146. BGBBTJ_API int BTJ_JFE_EncodeFastCtx(
  1147.     BGBBTJ_JFE_Context *ctx,
  1148.     byte *ibuf, byte *obuf,
  1149.     int xs, int ys, int qf, int pf)
  1150. {
  1151.     short *tp;
  1152.     int xs2, ys2, xs3, ys3;
  1153.     int xs2b, ys2b, xs3b, ys3b;
  1154.     int i, j, k, l, n;
  1155.  
  1156.     xs2=((xs+7)/8)*8;
  1157.     ys2=((ys+7)/8)*8;
  1158.     xs3=((xs+15)/16)*8;
  1159.     ys3=((ys+15)/16)*8;
  1160.  
  1161.     if(!ctx->ydb || (xs!=ctx->lxs) || (ys!=ctx->lys))
  1162.     {
  1163.         if(ctx->ydb)
  1164.         {
  1165.             free(ctx->ydb);
  1166.             free(ctx->udb);
  1167.             free(ctx->vdb);
  1168.         }
  1169.  
  1170.         ctx->ydb=malloc((xs2+8)*(ys2+16)*sizeof(short));
  1171.         ctx->udb=malloc((xs3+8)*(ys3+8)*sizeof(short));
  1172.         ctx->vdb=malloc((xs3+8)*(ys3+8)*sizeof(short));
  1173.  
  1174.         ctx->lxs=xs;
  1175.         ctx->lys=ys;
  1176.     }
  1177.  
  1178.     BTJ_JFE_FilterImageDCT2(ibuf,
  1179.         ctx->ydb, ctx->udb, ctx->vdb, xs, ys, qf, pf);
  1180.  
  1181.     if(qf!=ctx->lqf)
  1182.     {
  1183.         BTJ_JFE_MakeQuantTabFastIJG_Y(ctx->qt[0], (qf&255)/100.0);
  1184.         BTJ_JFE_MakeQuantTabFastIJG_UV(ctx->qt[1], (qf&255)/100.0);
  1185.         BTJ_JFEH_SetupQuantTabDivFP(ctx, 0);
  1186.         BTJ_JFEH_SetupQuantTabDivFP(ctx, 1);
  1187.         ctx->lqf=qf;
  1188.     }
  1189.  
  1190.     xs2b=xs2/8; ys2b=ys2/8;
  1191.     xs3b=xs3/8; ys3b=ys3/8;
  1192.  
  1193.     l=0;
  1194.     for(i=0; i<ys3b; i++)
  1195.         for(j=0; j<xs3b; j++)
  1196.     {
  1197.         tp=ctx->ydb+((i*2+0)*xs2b+j*2+0)*64;
  1198.         BTJ_JFEH_QuantBlock(ctx, tp, tp, 0);
  1199.         tp[0]-=l; l=tp[0]+l;
  1200.  
  1201.         tp=ctx->ydb+((i*2+0)*xs2b+j*2+1)*64;
  1202.         BTJ_JFEH_QuantBlock(ctx, tp, tp, 0);
  1203.         tp[0]-=l; l=tp[0]+l;
  1204.  
  1205.         tp=ctx->ydb+((i*2+1)*xs2b+j*2+0)*64;
  1206.         BTJ_JFEH_QuantBlock(ctx, tp, tp, 0);
  1207.         tp[0]-=l; l=tp[0]+l;
  1208.  
  1209.         tp=ctx->ydb+((i*2+1)*xs2b+j*2+1)*64;
  1210.         BTJ_JFEH_QuantBlock(ctx, tp, tp, 0);
  1211.         tp[0]-=l; l=tp[0]+l;
  1212.     }
  1213.  
  1214.     j=xs3b*ys3b; k=0; l=0;
  1215.     for(i=0; i<j; i++)
  1216.     {
  1217.         BTJ_JFEH_QuantBlock(ctx, ctx->udb+i*64, ctx->udb+i*64, 1);
  1218.         BTJ_JFEH_QuantBlock(ctx, ctx->vdb+i*64, ctx->vdb+i*64, 1);
  1219.         ctx->udb[i*64+0]-=k; k=ctx->udb[i*64+0]+k;
  1220.         ctx->vdb[i*64+0]-=l; l=ctx->vdb[i*64+0]+l;
  1221.     }
  1222.  
  1223.     ctx->ct=obuf;
  1224.     ctx->win=0;
  1225.     ctx->pos=32;
  1226.  
  1227.     *ctx->ct++=0xFF;
  1228.     *ctx->ct++=JPG_SOI;
  1229.  
  1230.     BTJ_JFE_EmitDQT(ctx, 0);
  1231.     BTJ_JFE_EmitDQT(ctx, 1);
  1232.  
  1233.     BTJ_JFE_EmitSOF(ctx, xs, ys);
  1234.  
  1235.     BTJ_JFE_EmitDHT2(ctx);
  1236.  
  1237. //  BTJ_JFE_EmitDHT(ctx, 0);
  1238. //  BTJ_JFE_EmitDHT(ctx, 1);
  1239. //  BTJ_JFE_EmitDHT(ctx, 2);
  1240. //  BTJ_JFE_EmitDHT(ctx, 3);
  1241.  
  1242.     BTJ_JFE_EmitSOS(ctx);
  1243.  
  1244.     for(i=0; i<ys3b; i++)
  1245.         for(j=0; j<xs3b; j++)
  1246.     {
  1247.         BTJ_JFEH_EncodeBlock(ctx, ctx->ydb+((i*2+0)*xs2b+j*2+0)*64, 0, 1);
  1248.         BTJ_JFEH_EncodeBlock(ctx, ctx->ydb+((i*2+0)*xs2b+j*2+1)*64, 0, 1);
  1249.         BTJ_JFEH_EncodeBlock(ctx, ctx->ydb+((i*2+1)*xs2b+j*2+0)*64, 0, 1);
  1250.         BTJ_JFEH_EncodeBlock(ctx, ctx->ydb+((i*2+1)*xs2b+j*2+1)*64, 0, 1);
  1251.  
  1252.         k=i*xs3b+j;
  1253.         BTJ_JFEH_EncodeBlock(ctx, ctx->udb+k*64, 2, 3);
  1254.         BTJ_JFEH_EncodeBlock(ctx, ctx->vdb+k*64, 2, 3);
  1255.     }
  1256.  
  1257.     BTJ_JFEH_FlushBits(ctx);
  1258.  
  1259.     *ctx->ct++=0xFF;
  1260.     *ctx->ct++=JPG_EOI;
  1261.  
  1262.     i=ctx->ct-obuf;
  1263.  
  1264.     return(i);
  1265. }
Advertisement
Add Comment
Please, Sign In to add comment