Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

430 lines
13 KiB

  1. /* File: sv_h263_getvlc.c */
  2. /*****************************************************************************
  3. ** Copyright (c) Digital Equipment Corporation, 1995, 1997 **
  4. ** **
  5. ** All Rights Reserved. Unpublished rights reserved under the copyright **
  6. ** laws of the United States. **
  7. ** **
  8. ** The software contained on this media is proprietary to and embodies **
  9. ** the confidential technology of Digital Equipment Corporation. **
  10. ** Possession, use, duplication or dissemination of the software and **
  11. ** media is authorized only pursuant to a valid written license from **
  12. ** Digital Equipment Corporation. **
  13. ** **
  14. ** RESTRICTED RIGHTS LEGEND Use, duplication, or disclosure by the U.S. **
  15. ** Government is subject to restrictions as set forth in Subparagraph **
  16. ** (c)(1)(ii) of DFARS 252.227-7013, or in FAR 52.227-19, as applicable. **
  17. ******************************************************************************/
  18. #include <stdio.h>
  19. #include "sv_h263.h"
  20. #include "sv_intrn.h"
  21. #include "SC_err.h"
  22. #include "proto.h"
  23. /* From getvlc.h */
  24. typedef struct {
  25. int val, len;
  26. } VLCtab;
  27. typedef struct {
  28. char run, level, len;
  29. } DCTtab;
  30. static VLCtab TMNMVtab0[] = {
  31. {3,4}, {61,4}, {2,3}, {2,3}, {62,3}, {62,3},
  32. {1,2}, {1,2}, {1,2}, {1,2}, {63,2}, {63,2}, {63,2}, {63,2}
  33. };
  34. static VLCtab TMNMVtab1[] = {
  35. {12,10}, {52,10}, {11,10}, {53,10}, {10,9}, {10,9},
  36. {54,9}, {54,9}, {9,9}, {9,9}, {55,9}, {55,9},
  37. {8,9}, {8,9}, {56,9}, {56,9}, {7,7}, {7,7},
  38. {7,7}, {7,7}, {7,7}, {7,7}, {7,7}, {7,7},
  39. {57,7}, {57,7}, {57,7}, {57,7}, {57,7}, {57,7},
  40. {57,7}, {57,7}, {6,7}, {6,7}, {6,7}, {6,7},
  41. {6,7}, {6,7}, {6,7}, {6,7}, {58,7}, {58,7},
  42. {58,7}, {58,7}, {58,7}, {58,7}, {58,7}, {58,7},
  43. {5,7}, {5,7}, {5,7}, {5,7}, {5,7}, {5,7},
  44. {5,7}, {5,7}, {59,7}, {59,7}, {59,7}, {59,7},
  45. {59,7}, {59,7}, {59,7}, {59,7}, {4,6}, {4,6},
  46. {4,6}, {4,6}, {4,6}, {4,6}, {4,6}, {4,6},
  47. {4,6}, {4,6}, {4,6}, {4,6}, {4,6}, {4,6},
  48. {4,6}, {4,6}, {60,6}, {60,6},{60,6},{60,6},
  49. {60,6},{60,6},{60,6},{60,6},{60,6},{60,6},
  50. {60,6},{60,6},{60,6},{60,6},{60,6},{60,6}
  51. };
  52. static VLCtab TMNMVtab2[] = {
  53. {32,12}, {31,12}, {33,12}, {30,11}, {30,11}, {34,11},
  54. {34,11}, {29,11}, {29,11}, {35,11}, {35,11}, {28,11},
  55. {28,11}, {36,11}, {36,11}, {27,11}, {27,11}, {37,11},
  56. {37,11}, {26,11}, {26,11}, {38,11}, {38,11}, {25,11},
  57. {25,11}, {39,11}, {39,11}, {24,10}, {24,10}, {24,10},
  58. {24,10}, {40,10}, {40,10}, {40,10}, {40,10}, {23,10},
  59. {23,10}, {23,10}, {23,10}, {41,10}, {41,10}, {41,10},
  60. {41,10}, {22,10}, {22,10}, {22,10}, {22,10}, {42,10},
  61. {42,10}, {42,10}, {42,10}, {21,10}, {21,10}, {21,10},
  62. {21,10}, {43,10}, {43,10}, {43,10}, {43,10}, {20,10},
  63. {20,10}, {20,10}, {20,10}, {44,10}, {44,10}, {44,10},
  64. {44,10}, {19,10}, {19,10}, {19,10}, {19,10}, {45,10},
  65. {45,10}, {45,10}, {45,10}, {18,10}, {18,10}, {18,10},
  66. {18,10}, {46,10}, {46,10}, {46,10}, {46,10}, {17,10},
  67. {17,10}, {17,10}, {17,10}, {47,10}, {47,10}, {47,10},
  68. {47,10}, {16,10}, {16,10}, {16,10}, {16,10}, {48,10},
  69. {48,10}, {48,10}, {48,10}, {15,10}, {15,10}, {15,10},
  70. {15,10}, {49,10}, {49,10}, {49,10}, {49,10}, {14,10},
  71. {14,10}, {14,10}, {14,10}, {50,10}, {50,10}, {50,10},
  72. {50,10}, {13,10}, {13,10}, {13,10}, {13,10}, {51,10},
  73. {51,10}, {51,10}, {51,10}
  74. };
  75. static VLCtab MCBPCtab[] = {
  76. {-1,0},
  77. {255,9}, {52,9}, {36,9}, {20,9}, {49,9}, {35,8}, {35,8}, {19,8}, {19,8},
  78. {50,8}, {50,8}, {51,7}, {51,7}, {51,7}, {51,7}, {34,7}, {34,7}, {34,7},
  79. {34,7}, {18,7}, {18,7}, {18,7}, {18,7}, {33,7}, {33,7}, {33,7}, {33,7},
  80. {17,7}, {17,7}, {17,7}, {17,7}, {4,6}, {4,6}, {4,6}, {4,6}, {4,6},
  81. {4,6}, {4,6}, {4,6}, {48,6}, {48,6}, {48,6}, {48,6}, {48,6}, {48,6},
  82. {48,6}, {48,6}, {3,5}, {3,5}, {3,5}, {3,5}, {3,5}, {3,5}, {3,5},
  83. {3,5}, {3,5}, {3,5}, {3,5}, {3,5}, {3,5}, {3,5}, {3,5}, {3,5},
  84. {32,4}, {32,4}, {32,4}, {32,4}, {32,4}, {32,4}, {32,4}, {32,4}, {32,4},
  85. {32,4}, {32,4}, {32,4}, {32,4}, {32,4}, {32,4}, {32,4}, {32,4}, {32,4},
  86. {32,4}, {32,4}, {32,4}, {32,4}, {32,4}, {32,4}, {32,4}, {32,4}, {32,4},
  87. {32,4}, {32,4}, {32,4}, {32,4}, {32,4}, {16,4}, {16,4}, {16,4}, {16,4},
  88. {16,4}, {16,4}, {16,4}, {16,4}, {16,4}, {16,4}, {16,4}, {16,4}, {16,4},
  89. {16,4}, {16,4}, {16,4}, {16,4}, {16,4}, {16,4}, {16,4}, {16,4}, {16,4},
  90. {16,4}, {16,4}, {16,4}, {16,4}, {16,4}, {16,4}, {16,4}, {16,4}, {16,4},
  91. {16,4}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3},
  92. {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3},
  93. {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3},
  94. {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3},
  95. {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3},
  96. {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3},
  97. {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3},
  98. {2,3}, {2,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3},
  99. {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3},
  100. {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3},
  101. {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3},
  102. {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3},
  103. {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3},
  104. {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3},
  105. {1,3}, {1,3}, {1,3},
  106. };
  107. static VLCtab MCBPCtabintra[] = {
  108. {-1,0},
  109. {20,6}, {36,6}, {52,6}, {4,4}, {4,4}, {4,4},
  110. {4,4}, {19,3}, {19,3}, {19,3}, {19,3}, {19,3},
  111. {19,3}, {19,3}, {19,3}, {35,3}, {35,3}, {35,3},
  112. {35,3}, {35,3}, {35,3}, {35,3}, {35,3}, {51,3},
  113. {51,3}, {51,3}, {51,3}, {51,3}, {51,3}, {51,3},
  114. {51,3},
  115. };
  116. static VLCtab CBPYtab[48] =
  117. { {-1,0}, {-1,0}, {9,6}, {6,6}, {7,5}, {7,5}, {11,5}, {11,5},
  118. {13,5}, {13,5}, {14,5}, {14,5}, {15,4}, {15,4}, {15,4}, {15,4},
  119. {3,4}, {3,4}, {3,4}, {3,4}, {5,4},{5,4},{5,4},{5,4},
  120. {1,4}, {1,4}, {1,4}, {1,4}, {10,4}, {10,4}, {10,4}, {10,4},
  121. {2,4}, {2,4}, {2,4}, {2,4}, {12,4}, {12,4}, {12,4}, {12,4},
  122. {4,4}, {4,4}, {4,4}, {4,4}, {8,4}, {8,4}, {8,4}, {8,4},
  123. };
  124. VLCtab DCT3Dtab0[] = {
  125. {4225,7}, {4209,7}, {4193,7}, {4177,7}, {193,7}, {177,7},
  126. {161,7}, {4,7}, {4161,6}, {4161,6}, {4145,6}, {4145,6},
  127. {4129,6}, {4129,6}, {4113,6}, {4113,6}, {145,6}, {145,6},
  128. {129,6}, {129,6}, {113,6}, {113,6}, {97,6}, {97,6},
  129. {18,6}, {18,6}, {3,6}, {3,6}, {81,5}, {81,5},
  130. {81,5}, {81,5}, {65,5}, {65,5}, {65,5}, {65,5},
  131. {49,5}, {49,5}, {49,5}, {49,5}, {4097,4}, {4097,4},
  132. {4097,4}, {4097,4}, {4097,4}, {4097,4}, {4097,4}, {4097,4},
  133. {1,2}, {1,2}, {1,2}, {1,2}, {1,2}, {1,2},
  134. {1,2}, {1,2}, {1,2}, {1,2}, {1,2}, {1,2},
  135. {1,2}, {1,2}, {1,2}, {1,2}, {1,2}, {1,2},
  136. {1,2}, {1,2}, {1,2}, {1,2}, {1,2}, {1,2},
  137. {1,2}, {1,2}, {1,2}, {1,2}, {1,2}, {1,2},
  138. {1,2}, {1,2}, {17,3}, {17,3}, {17,3}, {17,3},
  139. {17,3}, {17,3}, {17,3}, {17,3}, {17,3}, {17,3},
  140. {17,3}, {17,3}, {17,3}, {17,3}, {17,3}, {17,3},
  141. {33,4}, {33,4}, {33,4}, {33,4}, {33,4}, {33,4},
  142. {33,4}, {33,4}, {2,4}, {2,4},{2,4},{2,4},
  143. {2,4}, {2,4},{2,4},{2,4},
  144. };
  145. VLCtab DCT3Dtab1[] = {
  146. {9,10}, {8,10}, {4481,9}, {4481,9}, {4465,9}, {4465,9},
  147. {4449,9}, {4449,9}, {4433,9}, {4433,9}, {4417,9}, {4417,9},
  148. {4401,9}, {4401,9}, {4385,9}, {4385,9}, {4369,9}, {4369,9},
  149. {4098,9}, {4098,9}, {353,9}, {353,9}, {337,9}, {337,9},
  150. {321,9}, {321,9}, {305,9}, {305,9}, {289,9}, {289,9},
  151. {273,9}, {273,9}, {257,9}, {257,9}, {241,9}, {241,9},
  152. {66,9}, {66,9}, {50,9}, {50,9}, {7,9}, {7,9},
  153. {6,9}, {6,9}, {4353,8}, {4353,8}, {4353,8}, {4353,8},
  154. {4337,8}, {4337,8}, {4337,8}, {4337,8}, {4321,8}, {4321,8},
  155. {4321,8}, {4321,8}, {4305,8}, {4305,8}, {4305,8}, {4305,8},
  156. {4289,8}, {4289,8}, {4289,8}, {4289,8}, {4273,8}, {4273,8},
  157. {4273,8}, {4273,8}, {4257,8}, {4257,8}, {4257,8}, {4257,8},
  158. {4241,8}, {4241,8}, {4241,8}, {4241,8}, {225,8}, {225,8},
  159. {225,8}, {225,8}, {209,8}, {209,8}, {209,8}, {209,8},
  160. {34,8}, {34,8}, {34,8}, {34,8}, {19,8}, {19,8},
  161. {19,8}, {19,8}, {5,8}, {5,8}, {5,8}, {5,8},
  162. };
  163. VLCtab DCT3Dtab2[] = {
  164. {4114,11}, {4114,11}, {4099,11}, {4099,11}, {11,11}, {11,11},
  165. {10,11}, {10,11}, {4545,10}, {4545,10}, {4545,10}, {4545,10},
  166. {4529,10}, {4529,10}, {4529,10}, {4529,10}, {4513,10}, {4513,10},
  167. {4513,10}, {4513,10}, {4497,10}, {4497,10}, {4497,10}, {4497,10},
  168. {146,10}, {146,10}, {146,10}, {146,10}, {130,10}, {130,10},
  169. {130,10}, {130,10}, {114,10}, {114,10}, {114,10}, {114,10},
  170. {98,10}, {98,10}, {98,10}, {98,10}, {82,10}, {82,10},
  171. {82,10}, {82,10}, {51,10}, {51,10}, {51,10}, {51,10},
  172. {35,10}, {35,10}, {35,10}, {35,10}, {20,10}, {20,10},
  173. {20,10}, {20,10}, {12,11}, {12,11}, {21,11}, {21,11},
  174. {369,11}, {369,11}, {385,11}, {385,11}, {4561,11}, {4561,11},
  175. {4577,11}, {4577,11}, {4593,11}, {4593,11}, {4609,11}, {4609,11},
  176. {22,12}, {36,12}, {67,12}, {83,12}, {99,12}, {162,12},
  177. {401,12}, {417,12}, {4625,12}, {4641,12}, {4657,12}, {4673,12},
  178. {4689,12}, {4705,12}, {4721,12}, {4737,12}, {7167,7},
  179. {7167,7}, {7167,7}, {7167,7}, {7167,7}, {7167,7}, {7167,7},
  180. {7167,7}, {7167,7}, {7167,7}, {7167,7}, {7167,7}, {7167,7},
  181. {7167,7}, {7167,7}, {7167,7}, {7167,7}, {7167,7}, {7167,7},
  182. {7167,7}, {7167,7}, {7167,7}, {7167,7}, {7167,7}, {7167,7},
  183. {7167,7}, {7167,7}, {7167,7}, {7167,7}, {7167,7}, {7167,7},
  184. {7167,7}, };
  185. int sv_H263GetTMNMV(SvH263DecompressInfo_t *H263Info, ScBitstream_t *BSIn)
  186. {
  187. int code;
  188. if (H263Info->trace)
  189. printf("motion_code (");
  190. if (ScBSGetBit(BSIn))
  191. {
  192. if (H263Info->trace)
  193. printf("1): 0\n");
  194. return 0;
  195. }
  196. if ((code = (int)ScBSPeekBits(BSIn, 12))>=512)
  197. {
  198. code = (code>>8) - 2;
  199. ScBSSkipBits(BSIn, TMNMVtab0[code].len);
  200. #if 0
  201. if (H263Info->trace)
  202. {
  203. printf("0");
  204. svH263Printbits(code+2,4,TMNMVtab0[code].len);
  205. printf("): %d\n", TMNMVtab0[code].val);
  206. }
  207. #endif
  208. return TMNMVtab0[code].val;
  209. }
  210. if (code>=128)
  211. {
  212. code = (code>>2) -32;
  213. ScBSSkipBits(BSIn, TMNMVtab1[code].len);
  214. #if 0
  215. if (H263Info->trace)
  216. {
  217. printf("0");
  218. svH263Printbits(code+32,10,TMNMVtab1[code].len);
  219. printf("): %d\n",TMNMVtab1[code].val);
  220. }
  221. #endif
  222. return TMNMVtab1[code].val;
  223. }
  224. if ((code-=5)<0)
  225. {
  226. if (!H263Info->quiet)
  227. fprintf(stderr,"Invalid motion_vector code\n");
  228. H263Info->expand=1;
  229. return 0;
  230. }
  231. ScBSSkipBits(BSIn, TMNMVtab2[code].len);
  232. #if 0
  233. if (H263Info->trace)
  234. {
  235. printf("0");
  236. svH263Printbits(code+5,12,TMNMVtab2[code].len);
  237. printf("): %d\n",TMNMVtab2[code].val);
  238. }
  239. #endif
  240. return TMNMVtab2[code].val;
  241. }
  242. int sv_H263GetMCBPC(SvH263DecompressInfo_t *H263Info, ScBitstream_t *BSIn)
  243. {
  244. int code;
  245. if (H263Info->trace)
  246. printf("MCBPC (");
  247. code = (int)ScBSPeekBits(BSIn, 9);
  248. if (code == 1) {
  249. /* macroblock stuffing */
  250. if (H263Info->trace)
  251. printf("000000001): stuffing\n");
  252. ScBSSkipBits(BSIn, 9);
  253. return 255;
  254. }
  255. if (code == 0) {
  256. if (!H263Info->quiet)
  257. fprintf(stderr,"Invalid MCBPC code\n");
  258. H263Info->expand = 1;
  259. return 0;
  260. }
  261. if (code>=256)
  262. {
  263. ScBSSkipBits(BSIn, 1);
  264. if (H263Info->trace)
  265. printf("1): %d\n",0);
  266. return 0;
  267. }
  268. ScBSSkipBits(BSIn, MCBPCtab[code].len);
  269. #if 0
  270. if (H263Info->trace)
  271. {
  272. svH263Printbits(code,9,MCBPCtab[code].len);
  273. printf("): %d\n",MCBPCtab[code].val);
  274. }
  275. #endif
  276. return MCBPCtab[code].val;
  277. }
  278. int sv_H263GetMODB(SvH263DecompressInfo_t *H263Info, ScBitstream_t *BSIn)
  279. {
  280. int code;
  281. int MODB;
  282. if (H263Info->trace)
  283. printf("MODB (");
  284. code = (int)ScBSPeekBits(BSIn, 2);
  285. if (code < 2) {
  286. if (H263Info->trace)
  287. printf("0): MODB = 0\n");
  288. MODB = 0;
  289. ScBSSkipBits(BSIn, 1);
  290. }
  291. else if (code == 2) {
  292. if (H263Info->trace)
  293. printf("10): MODB = 1\n");
  294. MODB = 1;
  295. ScBSSkipBits(BSIn, 2);
  296. }
  297. else { /* code == 3 */
  298. if (H263Info->trace)
  299. printf("11): MODB = 2\n");
  300. MODB = 2;
  301. ScBSSkipBits(BSIn, 2);
  302. }
  303. return MODB;
  304. }
  305. int sv_H263GetMCBPCintra(SvH263DecompressInfo_t *H263Info, ScBitstream_t *BSIn)
  306. {
  307. int code;
  308. if (H263Info->trace)
  309. printf("MCBPCintra (");
  310. code = (int)ScBSPeekBits(BSIn, 9);
  311. if (code == 1) {
  312. /* macroblock stuffing */
  313. if (H263Info->trace)
  314. printf("000000001): stuffing\n");
  315. ScBSSkipBits(BSIn, 9);
  316. return 255;
  317. }
  318. if (code < 8) {
  319. if (!H263Info->quiet)
  320. fprintf(stderr,"Invalid MCBPCintra code\n");
  321. H263Info->expand = 1;
  322. return 0;
  323. }
  324. code >>= 3;
  325. if (code>=32)
  326. {
  327. ScBSSkipBits(BSIn, 1);
  328. if (H263Info->trace)
  329. printf("1): %d\n",3);
  330. return 3;
  331. }
  332. ScBSSkipBits(BSIn, MCBPCtabintra[code].len);
  333. #if 0
  334. if (H263Info->trace)
  335. {
  336. svH263Printbits(code,6,MCBPCtabintra[code].len);
  337. printf("): %d\n",MCBPCtabintra[code].val);
  338. }
  339. #endif
  340. return MCBPCtabintra[code].val;
  341. }
  342. int sv_H263GetCBPY(SvH263DecompressInfo_t *H263Info, ScBitstream_t *BSIn)
  343. {
  344. int code;
  345. if (H263Info->trace)
  346. printf("CBPY (");
  347. code = (int)ScBSPeekBits(BSIn, 6);
  348. if (code < 2) {
  349. if (!H263Info->quiet)
  350. fprintf(stderr,"Invalid CBPY code\n");
  351. H263Info->expand = 1;
  352. return -1;
  353. }
  354. if (code>=48)
  355. {
  356. ScBSSkipBits(BSIn, 2);
  357. if (H263Info->trace)
  358. printf("11): %d\n",0);
  359. return 0;
  360. }
  361. ScBSSkipBits(BSIn, CBPYtab[code].len);
  362. #if 0
  363. if (H263Info->trace)
  364. {
  365. svH263Printbits(code,6,CBPYtab[code].len);
  366. printf("): %d\n",CBPYtab[code].val);
  367. }
  368. #endif
  369. return CBPYtab[code].val;
  370. }