Advertisement
cr88192

moderately optimized JPEG encoder.

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