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.

1727 lines
56 KiB

  1. /* File: sv_h263_putbits.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. /*
  19. #define _SLIBDEBUG_
  20. */
  21. #include "sv_h263.h"
  22. #include "proto.h"
  23. #ifdef _SLIBDEBUG_
  24. #include "sc_debug.h"
  25. #define _DEBUG_ 0 /* detailed debuging statements */
  26. #define _VERBOSE_ 1 /* show progress */
  27. #define _VERIFY_ 1 /* verify correct operation */
  28. #define _WARN_ 1 /* warnings about strange behavior */
  29. #endif
  30. #define H263_EHUFF struct Modified_Encoder_Huffman
  31. H263_EHUFF
  32. {
  33. int n;
  34. int *Hlen;
  35. int *Hcode;
  36. };
  37. /* from sactbls.h */
  38. int cumf_COD[3]={16383, 6849, 0};
  39. int cumf_MCBPC[22]={16383, 4105, 3088, 2367, 1988, 1621, 1612, 1609, 1608, 496, 353, 195, 77, 22, 17, 12, 5, 4, 3, 2, 1, 0};
  40. int cumf_MCBPC_intra[10]={16383, 7410, 6549, 5188, 442, 182, 181, 141, 1, 0};
  41. int cumf_MODB[4]={16383, 6062, 2130, 0};
  42. int cumf_YCBPB[3]={16383,6062,0};
  43. int cumf_UVCBPB[3]={16383,491,0};
  44. int cumf_CBPY[17]={16383, 14481, 13869, 13196, 12568, 11931, 11185, 10814, 9796, 9150, 8781, 7933, 6860, 6116, 4873, 3538, 0};
  45. int cumf_CBPY_intra[17]={16383, 13619, 13211, 12933, 12562, 12395, 11913, 11783, 11004, 10782, 10689, 9928, 9353, 8945, 8407, 7795, 0};
  46. int cumf_DQUANT[5]={16383, 12287, 8192, 4095, 0};
  47. int cumf_MVD[65]={16383, 16380, 16369, 16365, 16361, 16357, 16350, 16343, 16339, 16333, 16326, 16318, 16311, 16306, 16298, 16291, 16283, 16272, 16261, 16249, 16235, 16222, 16207, 16175, 16141, 16094, 16044, 15936, 15764, 15463, 14956, 13924, 11491, 4621, 2264, 1315, 854, 583, 420, 326, 273, 229, 196, 166, 148, 137, 123, 114, 101, 91, 82, 76, 66, 59, 53, 46, 36, 30, 26, 24, 18, 14, 10, 5, 0};
  48. int cumf_INTRADC[255]={16383, 16380, 16379, 16378, 16377, 16376, 16370, 16361, 16360, 16359, 16358, 16357, 16356, 16355, 16343, 16238, 16237, 16236, 16230, 16221, 16220, 16205, 16190, 16169, 16151, 16130, 16109, 16094, 16070, 16037, 16007, 15962, 15938, 15899, 15854, 15815, 15788, 15743, 15689, 15656, 15617, 15560, 15473, 15404, 15296, 15178, 15106, 14992, 14868, 14738, 14593, 14438, 14283, 14169, 14064, 14004, 13914, 13824, 13752, 13671, 13590, 13515, 13458, 13380, 13305, 13230, 13143, 13025, 12935, 12878, 12794, 12743, 12656, 12596, 12521, 12443, 12359, 12278, 12200, 12131, 12047, 12002, 11948, 11891, 11828, 11744, 11663, 11588, 11495, 11402, 11288, 11204, 11126, 11039, 10961, 10883, 10787, 10679, 10583, 10481, 10360, 10227, 10113, 9961, 9828, 9717, 9584, 9485, 9324, 9112, 9019, 8908, 8766, 8584, 8426, 8211, 7920, 7663, 7406, 7152, 6904, 6677, 6453, 6265, 6101, 5904, 5716, 5489, 5307, 5056, 4850, 4569, 4284, 3966, 3712, 3518, 3342, 3206, 3048, 2909, 2773, 2668, 2596, 2512, 2370, 2295, 2232, 2166, 2103, 2022, 1956, 1887, 1830, 1803, 1770, 1728, 1674, 1635, 1599, 1557, 1500, 1482, 1434, 1389, 1356, 1317, 1284, 1245, 1200, 1179, 1140, 1110, 1092, 1062, 1044, 1035, 1014, 1008, 993, 981, 954, 936, 912, 894, 876, 864, 849, 828, 816, 801, 792, 777, 756, 732, 690, 660, 642, 615, 597, 576, 555, 522, 489, 459, 435, 411, 405, 396, 387, 375, 360, 354, 345, 344, 329, 314, 293, 278, 251, 236, 230, 224, 215, 214, 208, 199, 193, 184, 178, 169, 154, 127, 100, 94, 73, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 20, 19, 18, 17, 16, 15, 9, 0};
  49. int cumf_TCOEF1[104]={16383, 13455, 12458, 12079, 11885, 11800, 11738, 11700, 11681, 11661, 11651, 11645, 11641, 10572, 10403, 10361, 10346, 10339, 10335, 9554, 9445, 9427, 9419, 9006, 8968, 8964, 8643, 8627, 8624, 8369, 8354, 8352, 8200, 8192, 8191, 8039, 8036, 7920, 7917, 7800, 7793, 7730, 7727, 7674, 7613, 7564, 7513, 7484, 7466, 7439, 7411, 7389, 7373, 7369, 7359, 7348, 7321, 7302, 7294, 5013, 4819, 4789, 4096, 4073, 3373, 3064, 2674, 2357, 2177, 1975, 1798, 1618, 1517, 1421, 1303, 1194, 1087, 1027, 960, 890, 819, 758, 707, 680, 656, 613, 566, 534, 505, 475, 465, 449, 430, 395, 358, 335, 324, 303, 295, 286, 272, 233, 215, 0};
  50. int cumf_TCOEF2[104]={16383, 13582, 12709, 12402, 12262, 12188, 12150, 12131, 12125, 12117, 12113, 12108, 12104, 10567, 10180, 10070, 10019, 9998, 9987, 9158, 9037, 9010, 9005, 8404, 8323, 8312, 7813, 7743, 7726, 7394, 7366, 7364, 7076, 7062, 7060, 6810, 6797, 6614, 6602, 6459, 6454, 6304, 6303, 6200, 6121, 6059, 6012, 5973, 5928, 5893, 5871, 5847, 5823, 5809, 5796, 5781, 5771, 5763, 5752, 4754, 4654, 4631, 3934, 3873, 3477, 3095, 2758, 2502, 2257, 2054, 1869, 1715, 1599, 1431, 1305, 1174, 1059, 983, 901, 839, 777, 733, 683, 658, 606, 565, 526, 488, 456, 434, 408, 380, 361, 327, 310, 296, 267, 259, 249, 239, 230, 221, 214, 0};
  51. int cumf_TCOEF3[104]={16383, 13532, 12677, 12342, 12195, 12112, 12059, 12034, 12020, 12008, 12003, 12002, 12001, 10586, 10297, 10224, 10202, 10195, 10191, 9223, 9046, 8999, 8987, 8275, 8148, 8113, 7552, 7483, 7468, 7066, 7003, 6989, 6671, 6642, 6631, 6359, 6327, 6114, 6103, 5929, 5918, 5792, 5785, 5672, 5580, 5507, 5461, 5414, 5382, 5354, 5330, 5312, 5288, 5273, 5261, 5247, 5235, 5227, 5219, 4357, 4277, 4272, 3847, 3819, 3455, 3119, 2829, 2550, 2313, 2104, 1881, 1711, 1565, 1366, 1219, 1068, 932, 866, 799, 750, 701, 662, 605, 559, 513, 471, 432, 403, 365, 336, 312, 290, 276, 266, 254, 240, 228, 223, 216, 206, 199, 192, 189, 0};
  52. int cumf_TCOEFr[104]={16383, 13216, 12233, 11931, 11822, 11776, 11758, 11748, 11743, 11742, 11741, 11740, 11739, 10203, 9822, 9725, 9691, 9677, 9674, 8759, 8609, 8576, 8566, 7901, 7787, 7770, 7257, 7185, 7168, 6716, 6653, 6639, 6276, 6229, 6220, 5888, 5845, 5600, 5567, 5348, 5327, 5160, 5142, 5004, 4900, 4798, 4743, 4708, 4685, 4658, 4641, 4622, 4610, 4598, 4589, 4582, 4578, 4570, 4566, 3824, 3757, 3748, 3360, 3338, 3068, 2835, 2592, 2359, 2179, 1984, 1804, 1614, 1445, 1234, 1068, 870, 739, 668, 616, 566, 532, 489, 453, 426, 385, 357, 335, 316, 297, 283, 274, 266, 259, 251, 241, 233, 226, 222, 217, 214, 211, 209, 208, 0};
  53. int cumf_TCOEF1_intra[104]={16383, 13383, 11498, 10201, 9207, 8528, 8099, 7768, 7546, 7368, 7167, 6994, 6869, 6005, 5474, 5220, 5084, 4964, 4862, 4672, 4591, 4570, 4543, 4397, 4337, 4326, 4272, 4240, 4239, 4212, 4196, 4185, 4158, 4157, 4156, 4140, 4139, 4138, 4137, 4136, 4125, 4124, 4123, 4112, 4111, 4110, 4109, 4108, 4107, 4106, 4105, 4104, 4103, 4102, 4101, 4100, 4099, 4098, 4097, 3043, 2897, 2843, 1974, 1790, 1677, 1552, 1416, 1379, 1331, 1288, 1251, 1250, 1249, 1248, 1247, 1236, 1225, 1224, 1223, 1212, 1201, 1200, 1199, 1198, 1197, 1196, 1195, 1194, 1193, 1192, 1191, 1190, 1189, 1188, 1187, 1186, 1185, 1184, 1183, 1182, 1181, 1180, 1179, 0};
  54. int cumf_TCOEF2_intra[104]={16383, 13242, 11417, 10134, 9254, 8507, 8012, 7556, 7273, 7062, 6924, 6839, 6741, 6108, 5851, 5785, 5719, 5687, 5655, 5028, 4917, 4864, 4845, 4416, 4159, 4074, 3903, 3871, 3870, 3765, 3752, 3751, 3659, 3606, 3580, 3541, 3540, 3514, 3495, 3494, 3493, 3474, 3473, 3441, 3440, 3439, 3438, 3425, 3424, 3423, 3422, 3421, 3420, 3401, 3400, 3399, 3398, 3397, 3396, 2530, 2419, 2360, 2241, 2228, 2017, 1687, 1576, 1478, 1320, 1281, 1242, 1229, 1197, 1178, 1152, 1133, 1114, 1101, 1088, 1087, 1086, 1085, 1072, 1071, 1070, 1069, 1068, 1067, 1066, 1065, 1064, 1063, 1062, 1061, 1060, 1059, 1058, 1057, 1056, 1055, 1054, 1053, 1052, 0};
  55. int cumf_TCOEF3_intra[104]={16383, 12741, 10950, 10071, 9493, 9008, 8685, 8516, 8385, 8239, 8209, 8179, 8141, 6628, 5980, 5634, 5503, 5396, 5327, 4857, 4642, 4550, 4481, 4235, 4166, 4151, 3967, 3922, 3907, 3676, 3500, 3324, 3247, 3246, 3245, 3183, 3168, 3084, 3069, 3031, 3030, 3029, 3014, 3013, 2990, 2975, 2974, 2973, 2958, 2943, 2928, 2927, 2926, 2925, 2924, 2923, 2922, 2921, 2920, 2397, 2298, 2283, 1891, 1799, 1591, 1445, 1338, 1145, 1068, 1006, 791, 768, 661, 631, 630, 615, 592, 577, 576, 561, 546, 523, 508, 493, 492, 491, 476, 475, 474, 473, 472, 471, 470, 469, 468, 453, 452, 451, 450, 449, 448, 447, 446, 0};
  56. int cumf_TCOEFr_intra[104]={16383, 12514, 10776, 9969, 9579, 9306, 9168, 9082, 9032, 9000, 8981, 8962, 8952, 7630, 7212, 7053, 6992, 6961, 6940, 6195, 5988, 5948, 5923, 5370, 5244, 5210, 4854, 4762, 4740, 4384, 4300, 4288, 4020, 3968, 3964, 3752, 3668, 3511, 3483, 3354, 3322, 3205, 3183, 3108, 3046, 2999, 2981, 2974, 2968, 2961, 2955, 2949, 2943, 2942, 2939, 2935, 2934, 2933, 2929, 2270, 2178, 2162, 1959, 1946, 1780, 1651, 1524, 1400, 1289, 1133, 1037, 942, 849, 763, 711, 591, 521, 503, 496, 474, 461, 449, 442, 436, 426, 417, 407, 394, 387, 377, 373, 370, 367, 366, 365, 364, 363, 362, 358, 355, 352, 351, 350, 0};
  57. int cumf_SIGN[3]={16383, 8416, 0};
  58. int cumf_LAST[3]={16383, 9469, 0};
  59. int cumf_LAST_intra[3]={16383, 2820, 0};
  60. int cumf_RUN[65]={16383, 15310, 14702, 13022, 11883, 11234, 10612, 10192, 9516, 9016, 8623, 8366, 7595, 7068, 6730, 6487, 6379, 6285, 6177, 6150, 6083, 5989, 5949, 5922, 5895, 5828, 5774, 5773, 5394, 5164, 5016, 4569, 4366, 4136, 4015, 3867, 3773, 3692, 3611, 3476, 3341, 3301, 2787, 2503, 2219, 1989, 1515, 1095, 934, 799, 691, 583, 435, 300, 246, 206, 125, 124, 97, 57, 30, 3, 2, 1, 0};
  61. int cumf_RUN_intra[65]={16383, 10884, 8242, 7124, 5173, 4745, 4246, 3984, 3034, 2749, 2607, 2298, 966, 681, 396, 349, 302, 255, 254, 253, 206, 159, 158, 157, 156, 155, 154, 153, 106, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
  62. int cumf_LEVEL[255]={16383, 16382, 16381, 16380, 16379, 16378, 16377, 16376, 16375, 16374, 16373, 16372, 16371, 16370, 16369, 16368, 16367, 16366, 16365, 16364, 16363, 16362, 16361, 16360, 16359, 16358, 16357, 16356, 16355, 16354, 16353, 16352, 16351, 16350, 16349, 16348, 16347, 16346, 16345, 16344, 16343, 16342, 16341, 16340, 16339, 16338, 16337, 16336, 16335, 16334, 16333, 16332, 16331, 16330, 16329, 16328, 16327, 16326, 16325, 16324, 16323, 16322, 16321, 16320, 16319, 16318, 16317, 16316, 16315, 16314, 16313, 16312, 16311, 16310, 16309, 16308, 16307, 16306, 16305, 16304, 16303, 16302, 16301, 16300, 16299, 16298, 16297, 16296, 16295, 16294, 16293, 16292, 16291, 16290, 16289, 16288, 16287, 16286, 16285, 16284, 16283, 16282, 16281, 16280, 16279, 16278, 16277, 16250, 16223, 16222, 16195, 16154, 16153, 16071, 15989, 15880, 15879, 15878, 15824, 15756, 15674, 15606, 15538, 15184, 14572, 13960, 10718, 7994, 5379, 2123, 1537, 992, 693, 611, 516, 448, 421, 380, 353, 352, 284, 257, 230, 203, 162, 161, 160, 133, 132, 105, 104, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
  63. int cumf_LEVEL_intra[255]={16383, 16379, 16378, 16377, 16376, 16375, 16374, 16373, 16372, 16371, 16370, 16369, 16368, 16367, 16366, 16365, 16364, 16363, 16362, 16361, 16360, 16359, 16358, 16357, 16356, 16355, 16354, 16353, 16352, 16351, 16350, 16349, 16348, 16347, 16346, 16345, 16344, 16343, 16342, 16341, 16340, 16339, 16338, 16337, 16336, 16335, 16334, 16333, 16332, 16331, 16330, 16329, 16328, 16327, 16326, 16325, 16324, 16323, 16322, 16321, 16320, 16319, 16318, 16317, 16316, 16315, 16314, 16313, 16312, 16311, 16268, 16267, 16224, 16223, 16180, 16179, 16136, 16135, 16134, 16133, 16132, 16131, 16130, 16129, 16128, 16127, 16126, 16061, 16018, 16017, 16016, 16015, 16014, 15971, 15970, 15969, 15968, 15925, 15837, 15794, 15751, 15750, 15749, 15661, 15618, 15508, 15376, 15288, 15045, 14913, 14781, 14384, 13965, 13502, 13083, 12509, 12289, 12135, 11892, 11738, 11429, 11010, 10812, 10371, 9664, 9113, 8117, 8116, 8028, 6855, 5883, 4710, 4401, 4203, 3740, 3453, 3343, 3189, 2946, 2881, 2661, 2352, 2132, 1867, 1558, 1382, 1250, 1162, 1097, 1032, 967, 835, 681, 549, 439, 351, 350, 307, 306, 305, 304, 303, 302, 301, 300, 299, 298, 255, 212, 211, 210, 167, 166, 165, 164, 163, 162, 161, 160, 159, 158, 115, 114, 113, 112, 111, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
  64. /* from indices.h */
  65. int codtab[2] = {0,1};
  66. int mcbpctab[21] = {0,16,32,48,1,17,33,49,2,18,34,50,3,19,35,51,4,20,36,52,255};
  67. int mcbpc_intratab[9] = {3,19,35,51,4,20,36,52,255};
  68. int cbpytab[16] = {15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};
  69. int cbpy_intratab[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  70. int dquanttab[4] = {1,0,3,4};
  71. int mvdtab[64] = {32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
  72. int intradctab[254] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,255,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254};
  73. int tcoeftab[103] = {1,2,3,4,5,6,7,8,9,10,11,12,17,18,19,20,21,22,33,34,35,36,49,50,51,65,66,67,81,82,83,97,98,99,113,114,129,130,145,146,161,162,177,193,209,225,241,257,273,289,305,321,337,353,369,385,401,417,4097,4098,4099,4113,4114,4129,4145,4161,4177,4193,4209,4225,4241,4257,4273,4289,4305,4321,4337,4353,4369,4385,4401,4417,4433,4449,4465,4481,4497,4513,4529,4545,4561,4577,4593,4609,4625,4641,4657,4673,4689,4705,4721,4737,7167};
  74. int signtab[2] = {0,1};
  75. int lasttab[2] = {0,1};
  76. int last_intratab[2] = {0,1};
  77. int runtab[64] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63};
  78. int leveltab[254] = {129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127};
  79. int arith_used = 0;
  80. int CodeCoeff(ScBitstream_t *BSOut, int Mode, short *qcoeff, int block, int ncoeffs);
  81. int Code_sac_Coeff(SvH263CompressInfo_t *H263Info, ScBitstream_t *BSOut, int Mode, short *qcoeff, int block, int ncoeffs);
  82. int CodeTCoef(SvH263CompressInfo_t *H263Info, ScBitstream_t *BSOut, int mod_index, int position, int intra);
  83. static int sv_H263HuffEncode(ScBitstream_t *BSOut, int val,H263_EHUFF *huff);
  84. /**********************************************************************
  85. *
  86. * Name: CountBitsMB
  87. * Description: counts bits used for MB info
  88. *
  89. * Input: Mode, COD, CBP, Picture and Bits structures
  90. * Returns:
  91. * Side effects:
  92. *
  93. ***********************************************************************/
  94. void sv_H263CountBitsMB(ScBitstream_t *BSOut, int Mode, int COD, int CBP,
  95. int CBPB, H263_Pict *pic, H263_Bits *bits)
  96. {
  97. extern H263_EHUFF *vlc_cbpy, *vlc_cbpcm, *vlc_cbpcm_intra;
  98. int cbpy, cbpcm, length;
  99. _SlibDebug(_VERBOSE_, ScDebugPrintf(NULL, "sv_H263CountBitsMB(CBP=0x%X) MB=%d COD=%d\n",
  100. CBP, pic->MB, COD) );
  101. if (pic->picture_coding_type == H263_PCT_INTER) {
  102. svH263mputv(1,COD);
  103. bits->COD++;
  104. }
  105. if (COD) return; /* not coded */
  106. /* CBPCM */
  107. cbpcm = Mode | ((CBP&3)<<4);
  108. _SlibDebug(_DEBUG_,
  109. ScDebugPrintf(NULL, "CBPCM (CBP=%d) (cbpcm=%d): \n", CBP, cbpcm) );
  110. if (pic->picture_coding_type == H263_PCT_INTRA)
  111. length = sv_H263HuffEncode(BSOut, cbpcm,vlc_cbpcm_intra);
  112. else
  113. length = sv_H263HuffEncode(BSOut, cbpcm,vlc_cbpcm);
  114. bits->CBPCM += length;
  115. /* MODB & CBPB */
  116. if (pic->PB) {
  117. switch (pic->MODB) {
  118. case H263_PBMODE_NORMAL:
  119. svH263mputv(1,0);
  120. bits->MODB += 1;
  121. break;
  122. case H263_PBMODE_MVDB:
  123. svH263mputv(2,2);
  124. bits->MODB += 2;
  125. break;
  126. case H263_PBMODE_CBPB_MVDB:
  127. svH263mputv(2,3);
  128. bits->MODB += 2;
  129. /* CBPB */
  130. svH263mputv(6,CBPB);
  131. bits->CBPB += 6;
  132. break;
  133. }
  134. _SlibDebug(_DEBUG_,
  135. ScDebugPrintf(NULL, "MODB: %d, CBPB: %d\n", pic->MODB, CBPB) );
  136. }
  137. /* CBPY */
  138. cbpy = CBP>>2;
  139. if (Mode == H263_MODE_INTRA || Mode == H263_MODE_INTRA_Q) /* Intra */
  140. cbpy = cbpy^15;
  141. _SlibDebug(_DEBUG_,
  142. ScDebugPrintf(NULL, "CBPY (CBP=%d) (cbpy=%d): \n",CBP,cbpy) );
  143. length = sv_H263HuffEncode(BSOut, cbpy, vlc_cbpy);
  144. bits->CBPY += length;
  145. /* DQUANT */
  146. if ((Mode == H263_MODE_INTER_Q) || (Mode == H263_MODE_INTRA_Q)) {
  147. switch (pic->DQUANT) {
  148. case -1:
  149. svH263mputv(2,0);
  150. break;
  151. case -2:
  152. svH263mputv(2,1);
  153. break;
  154. case 1:
  155. svH263mputv(2,2);
  156. break;
  157. case 2:
  158. svH263mputv(2,3);
  159. break;
  160. default:
  161. _SlibDebug(_WARN_,
  162. ScDebugPrintf(NULL, "sv_H263CountBitsMB() Invalid DQUANT: %d\n", pic->DQUANT) );
  163. return;
  164. }
  165. bits->DQUANT += 2;
  166. }
  167. return;
  168. }
  169. /**********************************************************************
  170. *
  171. * Name: Count_sac_BitsMB
  172. * Description: counts bits used for MB info using SAC models
  173. * modified from CountBitsMB
  174. *
  175. * Input: Mode, COD, CBP, Picture and Bits structures
  176. * Returns: none
  177. * Side effects: Updates Bits structure.
  178. *
  179. ***********************************************************************/
  180. void sv_H263CountSACBitsMB(SvH263CompressInfo_t *H263Info, ScBitstream_t *BSOut,
  181. int Mode,int COD,int CBP,int CBPB,H263_Pict *pic,H263_Bits *bits)
  182. {
  183. int cbpy, cbpcm, length, i;
  184. _SlibDebug(_VERBOSE_, ScDebugPrintf(NULL, "sv_H263CountSACBitsMB(CBP=0x%X) MB=%d COD=%d\n",
  185. CBP, pic->MB, COD) );
  186. arith_used = 1;
  187. /* COD */
  188. if (pic->picture_coding_type == H263_PCT_INTER)
  189. bits->COD+=sv_H263AREncode(H263Info, BSOut, COD, cumf_COD);
  190. if (COD) return; /* not coded */
  191. /* CBPCM */
  192. cbpcm = Mode | ((CBP&3)<<4);
  193. _SlibDebug(_DEBUG_,
  194. ScDebugPrintf(H263Info->dbg, "CBPCM (CBP=%d) (cbpcm=%d): \n",CBP,cbpcm) );
  195. if (pic->picture_coding_type == H263_PCT_INTRA)
  196. length = sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(cbpcm,mcbpc_intratab,9),cumf_MCBPC_intra);
  197. else
  198. length = sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(cbpcm,mcbpctab,21),cumf_MCBPC);
  199. bits->CBPCM += length;
  200. /* MODB & CBPB */
  201. if (pic->PB) {
  202. switch (pic->MODB) {
  203. case H263_PBMODE_NORMAL:
  204. bits->MODB += sv_H263AREncode(H263Info, BSOut, 0, cumf_MODB);
  205. break;
  206. case H263_PBMODE_MVDB:
  207. bits->MODB += sv_H263AREncode(H263Info, BSOut, 1, cumf_MODB);
  208. break;
  209. case H263_PBMODE_CBPB_MVDB:
  210. bits->MODB += sv_H263AREncode(H263Info, BSOut, 2, cumf_MODB);
  211. /* CBPB */
  212. for(i=5; i>1; i--)
  213. bits->CBPB += sv_H263AREncode(H263Info, BSOut, ((CBPB & 1<<i)>>i), cumf_YCBPB);
  214. for(i=1; i>-1; i--)
  215. bits->CBPB += sv_H263AREncode(H263Info, BSOut, ((CBPB & 1<<i)>>i), cumf_UVCBPB);
  216. break;
  217. }
  218. _SlibDebug(_VERBOSE_,
  219. ScDebugPrintf(H263Info->dbg, "MODB: %d, CBPB: %d\n", pic->MODB, CBPB) );
  220. }
  221. /* CBPY */
  222. cbpy = CBP>>2;
  223. if (Mode == H263_MODE_INTRA || Mode == H263_MODE_INTRA_Q) { /* Intra */
  224. length = sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(cbpy,cbpy_intratab,16),cumf_CBPY_intra);
  225. } else {
  226. length = sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(cbpy,cbpytab,16),cumf_CBPY);
  227. }
  228. _SlibDebug(_VERBOSE_,
  229. ScDebugPrintf(H263Info->dbg, "CBPY (CBP=%d) (cbpy=%d):\n",CBP,cbpy) );
  230. bits->CBPY += length;
  231. /* DQUANT */
  232. if ((Mode == H263_MODE_INTER_Q) || (Mode == H263_MODE_INTRA_Q)) {
  233. bits->DQUANT += sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(pic->DQUANT+2,dquanttab,4), cumf_DQUANT);
  234. }
  235. return;
  236. }
  237. /**********************************************************************
  238. *
  239. * Name: CountBitsSlice
  240. * Description: couonts bits used for slice (GOB) info
  241. *
  242. * Input: slice no., quantizer
  243. *
  244. ***********************************************************************/
  245. int sv_H263CountBitsSlice(SvH263CompressInfo_t *H263Info, ScBitstream_t *BSOut,
  246. int slice, int quant)
  247. {
  248. int bits = 0;
  249. _SlibDebug(_VERBOSE_, ScDebugPrintf(NULL, "sv_H263CountBitsSlice(slice=%d, quant=%d)\n",
  250. slice, quant) );
  251. if (arith_used) {
  252. bits+=sv_H263AREncoderFlush(H263Info, BSOut); /* Need to call before fixed length string output */
  253. arith_used = 0;
  254. }
  255. /* Picture Start Code */
  256. svH263mputv(H263_PSC_LENGTH,H263_PSC); /* PSC */
  257. bits += H263_PSC_LENGTH;
  258. /* Group Number */
  259. svH263mputv(5,slice);
  260. bits += 5;
  261. /* GOB Sub Bitstream Indicator */
  262. /* if CPM == 1: read 2 bits GSBI */
  263. /* not supported in this version */
  264. /* GOB Frame ID */
  265. svH263mputv(2, 0);
  266. /* NB: in error-prone environments this value should change if
  267. PTYPE in picture header changes. In this version of the encoder
  268. PTYPE only changes when PB-frames are used in the following cases:
  269. (i) after the first intra frame
  270. (ii) if the distance between two P-frames is very large
  271. Therefore I haven't implemented this GFID change */
  272. /* GFID is not allowed to change unless PTYPE changes */
  273. bits += 2;
  274. /* Gquant */
  275. svH263mputv(5,quant);
  276. bits += 5;
  277. return bits;
  278. }
  279. /**********************************************************************
  280. *
  281. * Name: CountBitsCoeff
  282. * Description: counts bits used for coeffs
  283. *
  284. * Input: qcoeff, coding mode CBP, bits structure, no. of
  285. * coeffs
  286. *
  287. * Returns: struct with no. of bits used
  288. * Side effects:
  289. *
  290. ***********************************************************************/
  291. void sv_H263CountBitsCoeff(ScBitstream_t *BSOut, short *qcoeff, int Mode,
  292. int CBP, H263_Bits *bits, int ncoeffs)
  293. {
  294. int i;
  295. _SlibDebug(_DEBUG_, ScDebugPrintf(NULL, "sv_H263CountBitsCoeff(CBP=%d, ncoeffs=%d)\n",
  296. CBP, ncoeffs) );
  297. if (Mode == H263_MODE_INTRA || Mode == H263_MODE_INTRA_Q) {
  298. for (i = 0; i < 4; i++)
  299. bits->Y += CodeCoeff(BSOut, Mode, qcoeff,i,ncoeffs);
  300. for (i = 4; i < 6; i++)
  301. bits->C += CodeCoeff(BSOut, Mode, qcoeff,i,ncoeffs);
  302. }
  303. else {
  304. for (i = 0; i < 4; i++)
  305. if ((i==0 && CBP&32) || (i==1 && CBP&16) || (i==2 && CBP&8) ||
  306. (i==3 && CBP&4))
  307. bits->Y += CodeCoeff(BSOut, Mode, qcoeff, i, ncoeffs);
  308. for (i = 4; i < 6; i++)
  309. if ((i==4 && CBP&2) || (i==5 && CBP&1))
  310. bits->C += CodeCoeff(BSOut, Mode, qcoeff, i, ncoeffs);
  311. }
  312. return;
  313. }
  314. int CodeCoeff(ScBitstream_t *BSOut, int Mode, short *qcoeff, int block, int ncoeffs)
  315. {
  316. int j, jj, bits;
  317. int prev_run, run, prev_level, level, first;
  318. int prev_ind, ind, prev_s, s, length;
  319. extern H263_EHUFF *vlc_3d;
  320. run = bits = 0;
  321. first = 1;
  322. prev_run = prev_level = prev_ind = level = s = prev_s = ind = 0;
  323. jj = (block + 1)*ncoeffs;
  324. for (j = block*ncoeffs; j< jj; j++) {
  325. /* Do this block's DC-coefficient first */
  326. if (!(j%ncoeffs) && (Mode == H263_MODE_INTRA || Mode == H263_MODE_INTRA_Q)) {
  327. /* DC coeff */
  328. if (qcoeff[block*ncoeffs] != 128)
  329. svH263mputv(8,qcoeff[block*ncoeffs]);
  330. else
  331. svH263mputv(8,255);
  332. bits += 8;
  333. }
  334. else {
  335. /* AC coeff */
  336. s = 0;
  337. /* Increment run if coeff is zero */
  338. if ((level = qcoeff[j]) == 0) run++;
  339. else {
  340. /* code run & level and count bits */
  341. if (level < 0) {
  342. s = 1;
  343. level = -level;
  344. }
  345. ind = level | run<<4;
  346. ind = ind | 0<<12; /* Not last coeff */
  347. if (!first) {
  348. /* Encode the previous ind */
  349. if (prev_level < 13 && prev_run < 64)
  350. length = sv_H263HuffEncode(BSOut, prev_ind,vlc_3d);
  351. else length = 0;
  352. if (length == 0) { /* Escape coding */
  353. if (prev_s == 1) {prev_level = (prev_level^0xff)+1;}
  354. sv_H263HuffEncode(BSOut, H263_ESCAPE,vlc_3d);
  355. svH263mputv(1,0);
  356. svH263mputv(6,prev_run);
  357. svH263mputv(8,prev_level);
  358. bits += 22;
  359. }
  360. else {
  361. svH263mputv(1,prev_s);
  362. bits += length + 1;
  363. }
  364. }
  365. prev_run = run; prev_s = s;
  366. prev_level = level; prev_ind = ind;
  367. run = first = 0;
  368. }
  369. }
  370. }
  371. /* Encode the last coeff */
  372. if (!first) {
  373. /* if (H263_trace) fprintf(H263_trace_file,"Last coeff: "); */
  374. prev_ind = prev_ind | 1<<12; /* last coeff */
  375. if (prev_level < 13 && prev_run < 64)
  376. length = sv_H263HuffEncode(BSOut, prev_ind,vlc_3d);
  377. else length = 0;
  378. if (length == 0) { /* Escape coding */
  379. if (prev_s == 1) {prev_level = (prev_level^0xff)+1;}
  380. sv_H263HuffEncode(BSOut, H263_ESCAPE,vlc_3d);
  381. svH263mputv(1,1);
  382. svH263mputv(6,prev_run);
  383. svH263mputv(8,prev_level);
  384. bits += 22;
  385. }
  386. else {
  387. svH263mputv(1,prev_s);
  388. bits += length + 1;
  389. }
  390. }
  391. return bits;
  392. }
  393. /**********************************************************************
  394. *
  395. * Name: Count_sac_BitsCoeff
  396. * counts bits using SAC models
  397. *
  398. * Input: qcoeff, coding mode CBP, bits structure, no. of
  399. * coeffs
  400. *
  401. * Returns: struct with no. of bits used
  402. * Side effects:
  403. *
  404. ***********************************************************************/
  405. void sv_H263CountSACBitsCoeff(SvH263CompressInfo_t *H263Info, ScBitstream_t *BSOut,
  406. short *qcoeff,int Mode,
  407. int CBP, H263_Bits *bits, int ncoeffs)
  408. {
  409. int i;
  410. _SlibDebug(_DEBUG_, ScDebugPrintf(NULL, "sv_H263CountSACBitsCoeff(CBP=%d, ncoeffs=%d)\n",
  411. CBP, ncoeffs) );
  412. arith_used = 1;
  413. if (Mode == H263_MODE_INTRA || Mode == H263_MODE_INTRA_Q) {
  414. for (i = 0; i < 4; i++) {
  415. bits->Y += Code_sac_Coeff(H263Info, BSOut, Mode, qcoeff,i,ncoeffs);
  416. }
  417. for (i = 4; i < 6; i++) {
  418. bits->C += Code_sac_Coeff(H263Info, BSOut, Mode, qcoeff,i,ncoeffs);
  419. }
  420. }
  421. else {
  422. for (i = 0; i < 4; i++) {
  423. if ((i==0 && CBP&32) ||
  424. (i==1 && CBP&16) ||
  425. (i==2 && CBP&8) ||
  426. (i==3 && CBP&4)) {
  427. bits->Y += Code_sac_Coeff(H263Info, BSOut, Mode, qcoeff, i, ncoeffs);
  428. }
  429. }
  430. for (i = 4; i < 6; i++) {
  431. if ((i==4 && CBP&2) || (i==5 && CBP&1))
  432. bits->C += Code_sac_Coeff(H263Info, BSOut, Mode, qcoeff, i, ncoeffs);
  433. }
  434. }
  435. return;
  436. }
  437. int Code_sac_Coeff(SvH263CompressInfo_t *H263Info, ScBitstream_t *BSOut,
  438. int Mode, short *qcoeff, int block, int ncoeffs)
  439. {
  440. int j, jj, bits, mod_index, intra;
  441. int prev_run, run, prev_level, level, first, prev_position, position;
  442. int prev_ind, ind, prev_s, s, length;
  443. run = bits = 0;
  444. first = 1; position = 0; intra = 0;
  445. level = s = ind = 0;
  446. prev_run = prev_level = prev_ind = prev_s = prev_position = 0;
  447. intra = (Mode == H263_MODE_INTRA || Mode == H263_MODE_INTRA_Q);
  448. jj = (block + 1)*ncoeffs;
  449. for (j = block*ncoeffs; j< jj; j++) {
  450. if (!(j%ncoeffs) && intra) {
  451. if (qcoeff[block*ncoeffs]!=128)
  452. mod_index = sv_H263IndexFN(qcoeff[block*ncoeffs],intradctab,254);
  453. else
  454. mod_index = sv_H263IndexFN(255,intradctab,254);
  455. bits += sv_H263AREncode(H263Info, BSOut, mod_index, cumf_INTRADC);
  456. }
  457. else {
  458. s = 0;
  459. /* Increment run if coeff is zero */
  460. if ((level = qcoeff[j]) == 0) {
  461. run++;
  462. }
  463. else {
  464. /* code run & level and count bits */
  465. if (level < 0) {
  466. s = 1;
  467. level = -level;
  468. }
  469. ind = level | run<<4;
  470. ind = ind | 0<<12; /* Not last coeff */
  471. position++;
  472. if (!first) {
  473. mod_index = sv_H263IndexFN(prev_ind, tcoeftab, 103);
  474. if (prev_level < 13 && prev_run < 64)
  475. length = CodeTCoef(H263Info, BSOut, mod_index, prev_position, intra);
  476. else
  477. length = -1;
  478. if (length == -1) { /* Escape coding */
  479. if (prev_s == 1) {prev_level = (prev_level^0xff)+1;}
  480. mod_index = sv_H263IndexFN(H263_ESCAPE, tcoeftab, 103);
  481. bits += CodeTCoef(H263Info, BSOut, mod_index, prev_position, intra);
  482. if (intra)
  483. bits += sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(0, lasttab, 2), cumf_LAST_intra);
  484. else
  485. bits += sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(0, lasttab, 2), cumf_LAST);
  486. if (intra)
  487. bits += sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(prev_run, runtab, 64), cumf_RUN_intra);
  488. else
  489. bits += sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(prev_run, runtab, 64), cumf_RUN);
  490. if (intra)
  491. bits += sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(prev_level, leveltab, 254),
  492. cumf_LEVEL_intra);
  493. else
  494. bits += sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(prev_level, leveltab, 254),
  495. cumf_LEVEL);
  496. }
  497. else {
  498. bits += sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(prev_s, signtab, 2), cumf_SIGN);
  499. bits += length;
  500. }
  501. }
  502. prev_run = run; prev_s = s;
  503. prev_level = level; prev_ind = ind;
  504. prev_position = position;
  505. run = first = 0;
  506. }
  507. }
  508. }
  509. /* Encode Last Coefficient */
  510. if (!first) {
  511. prev_ind = prev_ind | 1<<12; /* last coeff */
  512. mod_index = sv_H263IndexFN(prev_ind, tcoeftab, 103);
  513. if (prev_level < 13 && prev_run < 64)
  514. length = CodeTCoef(H263Info, BSOut, mod_index, prev_position, intra);
  515. else
  516. length = -1;
  517. if (length == -1) { /* Escape coding */
  518. if (prev_s == 1) {prev_level = (prev_level^0xff)+1;}
  519. mod_index = sv_H263IndexFN(H263_ESCAPE, tcoeftab, 103);
  520. bits += CodeTCoef(H263Info, BSOut, mod_index, prev_position, intra);
  521. if (intra)
  522. bits += sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(1, lasttab, 2), cumf_LAST_intra);
  523. else
  524. bits += sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(1, lasttab, 2), cumf_LAST);
  525. if (intra)
  526. bits += sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(prev_run, runtab, 64), cumf_RUN_intra);
  527. else
  528. bits += sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(prev_run, runtab, 64), cumf_RUN);
  529. if (intra)
  530. bits += sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(prev_level, leveltab, 254), cumf_LEVEL_intra);
  531. else
  532. bits += sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(prev_level, leveltab, 254), cumf_LEVEL);
  533. }
  534. else {
  535. bits += sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(prev_s, signtab, 2), cumf_SIGN);
  536. bits += length;
  537. }
  538. } /* last coeff */
  539. return bits;
  540. }
  541. /*********************************************************************
  542. *
  543. * Name: CodeTCoef
  544. *
  545. * Description: Encodes an AC Coefficient using the
  546. * relevant SAC model.
  547. *
  548. * Input: Model index, position in DCT block and intra/
  549. * inter flag.
  550. *
  551. * Returns: Number of bits used.
  552. *
  553. * Side Effects: None
  554. *
  555. *********************************************************************/
  556. int CodeTCoef(SvH263CompressInfo_t *H263Info, ScBitstream_t *BSOut, int mod_index, int position, int intra)
  557. {
  558. int length;
  559. switch (position) {
  560. case 1:
  561. {
  562. if (intra)
  563. length = sv_H263AREncode(H263Info, BSOut, mod_index, cumf_TCOEF1_intra);
  564. else
  565. length = sv_H263AREncode(H263Info, BSOut, mod_index, cumf_TCOEF1);
  566. break;
  567. }
  568. case 2:
  569. {
  570. if (intra)
  571. length = sv_H263AREncode(H263Info, BSOut, mod_index, cumf_TCOEF2_intra);
  572. else
  573. length = sv_H263AREncode(H263Info, BSOut, mod_index, cumf_TCOEF2);
  574. break;
  575. }
  576. case 3:
  577. {
  578. if (intra)
  579. length = sv_H263AREncode(H263Info, BSOut, mod_index, cumf_TCOEF3_intra);
  580. else
  581. length = sv_H263AREncode(H263Info, BSOut, mod_index, cumf_TCOEF3);
  582. break;
  583. }
  584. default:
  585. {
  586. if (intra)
  587. length = sv_H263AREncode(H263Info, BSOut, mod_index, cumf_TCOEFr_intra);
  588. else
  589. length = sv_H263AREncode(H263Info, BSOut, mod_index, cumf_TCOEFr);
  590. break;
  591. }
  592. }
  593. return length;
  594. }
  595. /**********************************************************************
  596. *
  597. * Name: FindCBP
  598. * Description: Finds the CBP for a macroblock
  599. *
  600. * Input: qcoeff and mode
  601. *
  602. * Returns: CBP
  603. * Side effects:
  604. *
  605. ***********************************************************************/
  606. #if 0 /* merged into Quantizer */
  607. int svH263FindCBP(short *qcoeff, int Mode, int ncoeffs)
  608. {
  609. int i,j,jj;
  610. int CBP = 0;
  611. int intra = (Mode == H263_MODE_INTRA || Mode == H263_MODE_INTRA_Q);
  612. register short *ptr;
  613. /* Set CBP for this Macroblock */
  614. jj = ncoeffs - intra;
  615. qcoeff += intra;
  616. for (i=0; i < 6; i++, qcoeff += ncoeffs) {
  617. ptr = qcoeff;
  618. for (j=0; j < jj; j++) if (*ptr++) {
  619. CBP |= (32 >> i);
  620. break;
  621. }
  622. }
  623. return CBP;
  624. }
  625. #endif
  626. void sv_H263CountBitsVectors(SvH263CompressInfo_t *H263Info, ScBitstream_t *BSOut,
  627. H263_MotionVector *MV[6][H263_MBR+1][H263_MBC+2], H263_Bits *bits,
  628. int x, int y, int Mode, int newgob, H263_Pict *pic)
  629. {
  630. int y_vec, x_vec;
  631. extern H263_EHUFF *vlc_mv;
  632. int pmv0, pmv1;
  633. int start,stop,block;
  634. _SlibDebug(_DEBUG_, ScDebugPrintf(NULL, "sv_H263CountBitsVectors(x=%d, y=%d)\n", x, y) );
  635. x++;y++;
  636. if (Mode == H263_MODE_INTER4V) {
  637. start = 1; stop = 4;
  638. }
  639. else {
  640. start = 0; stop = 0;
  641. }
  642. for (block = start; block <= stop; block++) {
  643. sv_H263FindPMV(MV,x,y,&pmv0,&pmv1, block, newgob, 1);
  644. x_vec = (2*MV[block][y][x]->x + MV[block][y][x]->x_half) - pmv0;
  645. y_vec = (2*MV[block][y][x]->y + MV[block][y][x]->y_half) - pmv1;
  646. if (!H263Info->long_vectors) {
  647. if (x_vec < -32) x_vec += 64;
  648. else if (x_vec > 31) x_vec -= 64;
  649. if (y_vec < -32) y_vec += 64;
  650. else if (y_vec > 31) y_vec -= 64;
  651. }
  652. else {
  653. if (pmv0 < -31 && x_vec < -63) x_vec += 64;
  654. else if (pmv0 > 32 && x_vec > 63) x_vec -= 64;
  655. if (pmv1 < -31 && y_vec < -63) y_vec += 64;
  656. else if (pmv1 > 32 && y_vec > 63) y_vec -= 64;
  657. }
  658. if (x_vec < 0) x_vec += 64;
  659. if (y_vec < 0) y_vec += 64;
  660. bits->vec += sv_H263HuffEncode(BSOut, x_vec,vlc_mv);
  661. bits->vec += sv_H263HuffEncode(BSOut, y_vec,vlc_mv);
  662. _SlibDebug(_DEBUG_,
  663. if (x_vec > 31) x_vec -= 64;
  664. if (y_vec > 31) y_vec -= 64;
  665. ScDebugPrintf(H263Info->dbg, "(x,y) = (%d,%d) - ",
  666. (2*MV[block][y][x]->x + MV[block][y][x]->x_half),
  667. (2*MV[block][y][x]->y + MV[block][y][x]->y_half));
  668. ScDebugPrintf(H263Info->dbg, "(Px,Py) = (%d,%d)\n", pmv0,pmv1);
  669. ScDebugPrintf(H263Info->dbg, "(x_diff,y_diff) = (%d,%d)\n",x_vec,y_vec)
  670. );
  671. }
  672. /* PB-frames delta vectors */
  673. if (pic->PB)
  674. if (pic->MODB == H263_PBMODE_MVDB || pic->MODB == H263_PBMODE_CBPB_MVDB) {
  675. x_vec = MV[5][y][x]->x;
  676. y_vec = MV[5][y][x]->y;
  677. /* x_vec and y_vec are the PB-delta vectors */
  678. if (x_vec < 0) x_vec += 64;
  679. if (y_vec < 0) y_vec += 64;
  680. bits->vec += sv_H263HuffEncode(BSOut, x_vec,vlc_mv);
  681. bits->vec += sv_H263HuffEncode(BSOut, y_vec,vlc_mv);
  682. _SlibDebug(_DEBUG_,
  683. if (x_vec > 31) x_vec -= 64;
  684. if (y_vec > 31) y_vec -= 64;
  685. ScDebugPrintf(H263Info->dbg, "MVDB (x,y) = (%d,%d)\n",x_vec,y_vec) );
  686. }
  687. return;
  688. }
  689. void sv_H263CountSACBitsVectors(SvH263CompressInfo_t *H263Info, ScBitstream_t *BSOut,
  690. H263_MotionVector *MV[6][H263_MBR+1][H263_MBC+2], H263_Bits *bits,
  691. int x, int y, int Mode, int newgob, H263_Pict *pic)
  692. {
  693. int y_vec, x_vec;
  694. int pmv0, pmv1;
  695. int start,stop,block;
  696. _SlibDebug(_DEBUG_, ScDebugPrintf(NULL, "sv_H263CountSACBitsVectors(x=%d, y=%d)", x, y) );
  697. arith_used = 1;
  698. x++;y++;
  699. if (Mode == H263_MODE_INTER4V) {
  700. start = 1; stop = 4;
  701. }
  702. else {
  703. start = 0; stop = 0;
  704. }
  705. for (block = start; block <= stop; block++) {
  706. sv_H263FindPMV(MV,x,y,&pmv0,&pmv1, block, newgob, 1);
  707. x_vec = (2*MV[block][y][x]->x + MV[block][y][x]->x_half) - pmv0;
  708. y_vec = (2*MV[block][y][x]->y + MV[block][y][x]->y_half) - pmv1;
  709. if (!H263Info->long_vectors) {
  710. if (x_vec < -32) x_vec += 64;
  711. else if (x_vec > 31) x_vec -= 64;
  712. if (y_vec < -32) y_vec += 64;
  713. else if (y_vec > 31) y_vec -= 64;
  714. }
  715. else {
  716. if (pmv0 < -31 && x_vec < -63) x_vec += 64;
  717. else if (pmv0 > 32 && x_vec > 63) x_vec -= 64;
  718. if (pmv1 < -31 && y_vec < -63) y_vec += 64;
  719. else if (pmv1 > 32 && y_vec > 63) y_vec -= 64;
  720. }
  721. if (x_vec < 0) x_vec += 64;
  722. if (y_vec < 0) y_vec += 64;
  723. bits->vec += sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(x_vec,mvdtab,64),cumf_MVD);
  724. bits->vec += sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(y_vec,mvdtab,64),cumf_MVD);
  725. _SlibDebug(_DEBUG_,
  726. if (x_vec > 31) x_vec -= 64;
  727. if (y_vec > 31) y_vec -= 64;
  728. ScDebugPrintf(H263Info->dbg, "(x,y) = (%d,%d) - ",
  729. (2*MV[block][y][x]->x + MV[block][y][x]->x_half),
  730. (2*MV[block][y][x]->y + MV[block][y][x]->y_half));
  731. ScDebugPrintf(H263Info->dbg, "(Px,Py) = (%d,%d)\n", pmv0,pmv1);
  732. ScDebugPrintf(H263Info->dbg, "(x_diff,y_diff) = (%d,%d)\n",x_vec,y_vec) );
  733. }
  734. /* PB-frames delta vectors */
  735. if (pic->PB)
  736. if (pic->MODB == H263_PBMODE_MVDB || pic->MODB == H263_PBMODE_CBPB_MVDB) {
  737. x_vec = MV[5][y][x]->x;
  738. y_vec = MV[5][y][x]->y;
  739. if (x_vec < -32)
  740. x_vec += 64;
  741. else if (x_vec > 31)
  742. x_vec -= 64;
  743. if (y_vec < -32)
  744. y_vec += 64;
  745. else if (y_vec > 31)
  746. y_vec -= 64;
  747. if (x_vec < 0) x_vec += 64;
  748. if (y_vec < 0) y_vec += 64;
  749. bits->vec += sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(x_vec,mvdtab,64),cumf_MVD);
  750. bits->vec += sv_H263AREncode(H263Info, BSOut, sv_H263IndexFN(y_vec,mvdtab,64),cumf_MVD);
  751. _SlibDebug(_DEBUG_,
  752. if (x_vec > 31) x_vec -= 64;
  753. if (y_vec > 31) y_vec -= 64;
  754. ScDebugPrintf(H263Info->dbg, "PB delta vectors: MVDB (x,y) = (%d,%d)\n",x_vec,y_vec)
  755. );
  756. }
  757. return;
  758. }
  759. void sv_H263FindPMV(H263_MotionVector *MV[6][H263_MBR+1][H263_MBC+2], int x, int y,
  760. int *pmv0, int *pmv1, int block, int newgob, int half_pel)
  761. {
  762. int p1,p2,p3;
  763. int xin1,xin2,xin3;
  764. int yin1,yin2,yin3;
  765. int vec1,vec2,vec3;
  766. int l8,o8,or8;
  767. l8 = o8 = or8 = 0;
  768. if (MV[0][y][x-1]->Mode == H263_MODE_INTER4V)
  769. l8 = 1;
  770. if (MV[0][y-1][x]->Mode == H263_MODE_INTER4V)
  771. o8 = 1;
  772. if (MV[0][y-1][x+1]->Mode == H263_MODE_INTER4V)
  773. or8 = 1;
  774. switch (block) {
  775. case 0:
  776. vec1 = (l8 ? 2 : 0) ; yin1 = y ; xin1 = x-1;
  777. vec2 = (o8 ? 3 : 0) ; yin2 = y-1; xin2 = x;
  778. vec3 = (or8? 3 : 0) ; yin3 = y-1; xin3 = x+1;
  779. break;
  780. case 1:
  781. vec1 = (l8 ? 2 : 0) ; yin1 = y ; xin1 = x-1;
  782. vec2 = (o8 ? 3 : 0) ; yin2 = y-1; xin2 = x;
  783. vec3 = (or8? 3 : 0) ; yin3 = y-1; xin3 = x+1;
  784. break;
  785. case 2:
  786. vec1 = 1 ; yin1 = y ; xin1 = x;
  787. vec2 = (o8 ? 4 : 0) ; yin2 = y-1; xin2 = x;
  788. vec3 = (or8? 3 : 0) ; yin3 = y-1; xin3 = x+1;
  789. break;
  790. case 3:
  791. vec1 = (l8 ? 4 : 0) ; yin1 = y ; xin1 = x-1;
  792. vec2 = 1 ; yin2 = y ; xin2 = x;
  793. vec3 = 2 ; yin3 = y ; xin3 = x;
  794. break;
  795. case 4:
  796. vec1 = 3 ; yin1 = y ; xin1 = x;
  797. vec2 = 1 ; yin2 = y ; xin2 = x;
  798. vec3 = 2 ; yin3 = y ; xin3 = x;
  799. break;
  800. default:
  801. _SlibDebug(_WARN_, ScDebugPrintf(NULL, "Illegal block number in FindPMV (countbit.c)\n") );
  802. return;
  803. }
  804. if (half_pel) {
  805. p1 = 2*MV[vec1][yin1][xin1]->x + MV[vec1][yin1][xin1]->x_half;
  806. p2 = 2*MV[vec2][yin2][xin2]->x + MV[vec2][yin2][xin2]->x_half;
  807. p3 = 2*MV[vec3][yin3][xin3]->x + MV[vec3][yin3][xin3]->x_half;
  808. }
  809. else {
  810. p1 = 2*MV[vec1][yin1][xin1]->x;
  811. p2 = 2*MV[vec2][yin2][xin2]->x;
  812. p3 = 2*MV[vec3][yin3][xin3]->x;
  813. }
  814. if (newgob && (block == 0 || block == 1 || block == 2))
  815. p2 = 2*H263_NO_VEC;
  816. if (p2 == 2*H263_NO_VEC) { p2 = p3 = p1; }
  817. *pmv0 = p1+p2+p3 - mmax(p1,mmax(p2,p3)) - mmin(p1,mmin(p2,p3));
  818. if (half_pel) {
  819. p1 = 2*MV[vec1][yin1][xin1]->y + MV[vec1][yin1][xin1]->y_half;
  820. p2 = 2*MV[vec2][yin2][xin2]->y + MV[vec2][yin2][xin2]->y_half;
  821. p3 = 2*MV[vec3][yin3][xin3]->y + MV[vec3][yin3][xin3]->y_half;
  822. }
  823. else {
  824. p1 = 2*MV[vec1][yin1][xin1]->y;
  825. p2 = 2*MV[vec2][yin2][xin2]->y;
  826. p3 = 2*MV[vec3][yin3][xin3]->y;
  827. }
  828. if (newgob && (block == 0 || block == 1 || block == 2))
  829. p2 = 2*H263_NO_VEC;
  830. if (p2 == 2*H263_NO_VEC) { p2 = p3 = p1; }
  831. *pmv1 = p1+p2+p3 - mmax(p1,mmax(p2,p3)) - mmin(p1,mmin(p2,p3));
  832. return;
  833. }
  834. void sv_H263ZeroBits(H263_Bits *bits)
  835. {
  836. bits->Y = 0;
  837. bits->C = 0;
  838. bits->vec = 0;
  839. bits->CBPY = 0;
  840. bits->CBPCM = 0;
  841. bits->MODB = 0;
  842. bits->CBPB = 0;
  843. bits->COD = 0;
  844. bits->DQUANT = 0;
  845. bits->header = 0;
  846. bits->total = 0;
  847. bits->no_inter = 0;
  848. bits->no_inter4v = 0;
  849. bits->no_intra = 0;
  850. return;
  851. }
  852. void sv_H263ZeroRes(H263_Results *res)
  853. {
  854. res->SNR_l = (float)0;
  855. res->SNR_Cr = (float)0;
  856. res->SNR_Cb = (float)0;
  857. res->QP_mean = (float)0;
  858. }
  859. void sv_H263AddBits(H263_Bits *total, H263_Bits *bits)
  860. {
  861. total->Y += bits->Y;
  862. total->C += bits->C;
  863. total->vec += bits->vec;
  864. total->CBPY += bits->CBPY;
  865. total->CBPCM += bits->CBPCM;
  866. total->MODB += bits->MODB;
  867. total->CBPB += bits->CBPB;
  868. total->COD += bits->COD;
  869. total->DQUANT += bits->DQUANT;
  870. total->header += bits->header;
  871. total->total += bits->total;
  872. total->no_inter += bits->no_inter;
  873. total->no_inter4v += bits->no_inter4v;
  874. total->no_intra += bits->no_intra;
  875. return;
  876. }
  877. void sv_H263AddRes(H263_Results *total, H263_Results *res, H263_Pict *pic)
  878. {
  879. total->SNR_l += res->SNR_l;
  880. total->SNR_Cr += res->SNR_Cr;
  881. total->SNR_Cb += res->SNR_Cb;
  882. total->QP_mean += pic->QP_mean;
  883. return;
  884. }
  885. void sv_H263AddBitsPicture(H263_Bits *bits)
  886. {
  887. bits->total =
  888. bits->Y +
  889. bits->C +
  890. bits->vec +
  891. bits->CBPY +
  892. bits->CBPCM +
  893. bits->MODB +
  894. bits->CBPB +
  895. bits->COD +
  896. bits->DQUANT +
  897. bits->header ;
  898. }
  899. void sv_H263ZeroVec(H263_MotionVector *MV)
  900. {
  901. MV->x = 0;
  902. MV->y = 0;
  903. MV->x_half = 0;
  904. MV->y_half = 0;
  905. return;
  906. }
  907. void sv_H263MarkVec(H263_MotionVector *MV)
  908. {
  909. MV->x = H263_NO_VEC;
  910. MV->y = H263_NO_VEC;
  911. MV->x_half = 0;
  912. MV->y_half = 0;
  913. return;
  914. }
  915. void svH263CopyVec(H263_MotionVector *MV2, H263_MotionVector *MV1)
  916. {
  917. MV2->x = MV1->x;
  918. MV2->x_half = MV1->x_half;
  919. MV2->y = MV1->y;
  920. MV2->y_half = MV1->y_half;
  921. return;
  922. }
  923. int sv_H263EqualVec(H263_MotionVector *MV2, H263_MotionVector *MV1)
  924. {
  925. if (MV1->x != MV2->x)
  926. return 0;
  927. if (MV1->y != MV2->y)
  928. return 0;
  929. if (MV1->x_half != MV2->x_half)
  930. return 0;
  931. if (MV1->y_half != MV2->y_half)
  932. return 0;
  933. return 1;
  934. }
  935. /**********************************************************************
  936. *
  937. * Name: CountBitsPicture(Pict *pic)
  938. * Description: counts the number of bits needed for picture
  939. * header
  940. *
  941. * Input: pointer to picture structure
  942. * Returns: number of bits
  943. * Side effects:
  944. *
  945. ***********************************************************************/
  946. int sv_H263CountBitsPicture(SvH263CompressInfo_t *H263Info, ScBitstream_t *BSOut, H263_Pict *pic)
  947. {
  948. int bits = 0;
  949. _SlibDebug(_VERBOSE_, ScDebugPrintf(NULL, "sv_H263CountBitsPicture(frames=%d)\n",
  950. H263Info->frames) );
  951. /* in case of arithmetic coding, encoder_flush() has been called before
  952. zeroflush() in main.c */
  953. /* Picture start code */
  954. #if 0
  955. if (H263_trace) { fprintf(tf,"picture_start_code: "); }
  956. #endif
  957. svH263mputv(H263_PSC_LENGTH,H263_PSC);
  958. bits += H263_PSC_LENGTH;
  959. /* Group number */
  960. #if 0
  961. if (H263_trace) { fprintf(tf,"Group number in picture header: ");}
  962. #endif
  963. svH263mputv(5,0);
  964. bits += 5;
  965. /* Time reference */
  966. _SlibDebug(_VERBOSE_, ScDebugPrintf(NULL, "Time reference: %d\n", pic->TR) );
  967. svH263mputv(8,pic->TR);
  968. bits += 8;
  969. /* bit 1 */
  970. _SlibDebug(_VERBOSE_, ScDebugPrintf(NULL, "Spare: %d\n", pic->spare) );
  971. pic->spare = 1; /* always 1 to avoid start code emulation */
  972. svH263mputv(1,pic->spare);
  973. bits += 1;
  974. /* bit 2 */
  975. #if 0
  976. if (H263_trace) { fprintf(tf,"always zero for distinction with H.261\n"); }
  977. #endif
  978. svH263mputv(1,0);
  979. bits += 1;
  980. /* bit 3 */
  981. #if 0
  982. if (H263_trace) { fprintf(tf,"split_screen_indicator: "); }
  983. #endif
  984. svH263mputv(1,0); /* no support for split-screen in this software */
  985. bits += 1;
  986. /* bit 4 */
  987. #if 0
  988. if (H263_trace) { fprintf(tf,"document_camera_indicator: "); }
  989. #endif
  990. svH263mputv(1,0);
  991. bits += 1;
  992. /* bit 5 */
  993. #if 0
  994. if (H263_trace) { fprintf(tf,"freeze_picture_release: "); }
  995. #endif
  996. svH263mputv(1,0);
  997. bits += 1;
  998. /* bit 6-8 */
  999. _SlibDebug(_VERBOSE_, ScDebugPrintf(NULL, "source_format: %d\n", pic->source_format) );
  1000. svH263mputv(3,pic->source_format);
  1001. bits += 3;
  1002. /* bit 9 */
  1003. _SlibDebug(_VERBOSE_, ScDebugPrintf(NULL, "picture_coding_type: %d\n",
  1004. pic->picture_coding_type) );
  1005. svH263mputv(1,pic->picture_coding_type);
  1006. bits += 1;
  1007. /* bit 10 */
  1008. _SlibDebug(_VERBOSE_, ScDebugPrintf(NULL, "mv_outside_frame: %d\n",
  1009. pic->unrestricted_mv_mode) );
  1010. svH263mputv(1,pic->unrestricted_mv_mode); /* Unrestricted Motion Vector mode */
  1011. bits += 1;
  1012. /* bit 11 */
  1013. _SlibDebug(_VERBOSE_, ScDebugPrintf(NULL, "sac_coding: %d\n",
  1014. H263Info->syntax_arith_coding) );
  1015. svH263mputv(1,H263Info->syntax_arith_coding); /* Syntax-based Arithmetic Coding mode */
  1016. bits += 1;
  1017. /* bit 12 */
  1018. _SlibDebug(_VERBOSE_, ScDebugPrintf(NULL, "adv_pred_mode: %d\n", H263Info->advanced) );
  1019. svH263mputv(1,H263Info->advanced); /* Advanced Prediction mode */
  1020. bits += 1;
  1021. /* bit 13 */
  1022. _SlibDebug(_VERBOSE_, ScDebugPrintf(NULL, "PB-coded: %d\n", pic->PB) );
  1023. svH263mputv(1,pic->PB);
  1024. bits += 1;
  1025. /* QUANT */
  1026. _SlibDebug(_VERBOSE_, ScDebugPrintf(NULL, "QUANT: %d\n", pic->QUANT) );
  1027. svH263mputv(5,pic->QUANT);
  1028. bits += 5;
  1029. /* Continuous Presence Multipoint (CPM) */
  1030. svH263mputv(1,0); /* CPM is not supported in this software */
  1031. bits += 1;
  1032. /* Picture Sub Bitstream Indicator (PSBI) */
  1033. /* if CPM == 1: 2 bits PSBI */
  1034. /* not supported */
  1035. /* extra information for PB-frames */
  1036. if (pic->PB) {
  1037. #if 0
  1038. if (H263_trace) { fprintf(tf,"TRB: "); }
  1039. #endif
  1040. svH263mputv(3,pic->TRB);
  1041. bits += 3;
  1042. #if 0
  1043. if (H263_trace) { fprintf(tf,"BQUANT: ");}
  1044. #endif
  1045. svH263mputv(2,pic->BQUANT);
  1046. bits += 2;
  1047. }
  1048. /* PEI (extra information) */
  1049. #if 0
  1050. if (H263_trace) { fprintf(tf,"PEI: "); }
  1051. #endif
  1052. /* "Encoders shall not insert PSPARE until specified by the ITU" */
  1053. svH263mputv(1,0);
  1054. bits += 1;
  1055. /* PSPARE */
  1056. /* if PEI == 1: 8 bits PSPARE + another PEI bit */
  1057. /* not supported */
  1058. return bits;
  1059. }
  1060. /*****************************************************************
  1061. *
  1062. * huffman.c, Huffman coder for H.263 encoder
  1063. * Wei-Lien Hsu
  1064. * Date: December 11, 1996
  1065. *
  1066. *****************************************************************/
  1067. /*
  1068. ************************************************************
  1069. huffman.c
  1070. This file contains the Huffman routines. They are constructed to use
  1071. no look-ahead in the stream.
  1072. ************************************************************
  1073. */
  1074. /* tables.h */
  1075. /* TMN Huffman tables */
  1076. /* Motion vectors */
  1077. int vlc_mv_coeff[] = {
  1078. 32,13,5,
  1079. 33,13,7,
  1080. 34,12,5,
  1081. 35,12,7,
  1082. 36,12,9,
  1083. 37,12,11,
  1084. 38,12,13,
  1085. 39,12,15,
  1086. 40,11,9,
  1087. 41,11,11,
  1088. 42,11,13,
  1089. 43,11,15,
  1090. 44,11,17,
  1091. 45,11,19,
  1092. 46,11,21,
  1093. 47,11,23,
  1094. 48,11,25,
  1095. 49,11,27,
  1096. 50,11,29,
  1097. 51,11,31,
  1098. 52,11,33,
  1099. 53,11,35,
  1100. 54,10,19,
  1101. 55,10,21,
  1102. 56,10,23,
  1103. 57,8,7,
  1104. 58,8,9,
  1105. 59,8,11,
  1106. 60,7,7,
  1107. 61,5,3,
  1108. 62,4,3,
  1109. 63,3,3,
  1110. 0,1,1,
  1111. 1,3,2,
  1112. 2,4,2,
  1113. 3,5,2,
  1114. 4,7,6,
  1115. 5,8,10,
  1116. 6,8,8,
  1117. 7,8,6,
  1118. 8,10,22,
  1119. 9,10,20,
  1120. 10,10,18,
  1121. 11,11,34,
  1122. 12,11,32,
  1123. 13,11,30,
  1124. 14,11,28,
  1125. 15,11,26,
  1126. 16,11,24,
  1127. 17,11,22,
  1128. 18,11,20,
  1129. 19,11,18,
  1130. 20,11,16,
  1131. 21,11,14,
  1132. 22,11,12,
  1133. 23,11,10,
  1134. 24,11,8,
  1135. 25,12,14,
  1136. 26,12,12,
  1137. 27,12,10,
  1138. 28,12,8,
  1139. 29,12,6,
  1140. 30,12,4,
  1141. 31,13,6,
  1142. -1,-1
  1143. };
  1144. /* CBPCM (MCBPC) */
  1145. int vlc_cbpcm_intra_coeff[] = {
  1146. 3,1,1,
  1147. 19,3,1,
  1148. 35,3,2,
  1149. 51,3,3,
  1150. 4,4,1,
  1151. 20,6,1,
  1152. 36,6,2,
  1153. 52,6,3,
  1154. 255,9,1,
  1155. -1,-1
  1156. };
  1157. int vlc_cbpcm_coeff[] = {
  1158. 0,1,1,
  1159. 16,4,3,
  1160. 32,4,2,
  1161. 48,6,5,
  1162. 1,3,3,
  1163. 17,7,7,
  1164. 33,7,6,
  1165. 49,9,5,
  1166. 2,3,2,
  1167. 18,7,5,
  1168. 34,7,4,
  1169. 50,8,5,
  1170. 3,5,3,
  1171. 19,8,4,
  1172. 35,8,3,
  1173. 51,7,3,
  1174. 4,6,4,
  1175. 20,9,4,
  1176. 36,9,3,
  1177. 52,9,2,
  1178. 255,9,1,
  1179. -1,-1
  1180. };
  1181. /* CBPY */
  1182. int vlc_cbpy_coeff[] = {
  1183. 0, 2,3,
  1184. 8, 4,11,
  1185. 4, 4,10,
  1186. 12, 4,9,
  1187. 2, 4,8,
  1188. 10, 4,7,
  1189. 6, 6,3,
  1190. 14, 5,5,
  1191. 1, 4,6,
  1192. 9, 6,2,
  1193. 5, 4,5,
  1194. 13, 5,4,
  1195. 3, 4,4,
  1196. 11, 5,3,
  1197. 7, 5,2,
  1198. 15, 4,3,
  1199. -1,-1
  1200. };
  1201. /* 3D VLC */
  1202. int vlc_3d_coeff[] = {
  1203. 1,2,2,
  1204. 2,4,15,
  1205. 3,6,21,
  1206. 4,7,23,
  1207. 5,8,31,
  1208. 6,9,37,
  1209. 7,9,36,
  1210. 8,10,33,
  1211. 9,10,32,
  1212. 10,11,7,
  1213. 11,11,6,
  1214. 12,11,32,
  1215. 17,3,6,
  1216. 18,6,20,
  1217. 19,8,30,
  1218. 20,10,15,
  1219. 21,11,33,
  1220. 22,12,80,
  1221. 33,4,14,
  1222. 34,8,29,
  1223. 35,10,14,
  1224. 36,12,81,
  1225. 49,5,13,
  1226. 50,9,35,
  1227. 51,10,13,
  1228. 65,5,12,
  1229. 66,9,34,
  1230. 67,12,82,
  1231. 81,5,11,
  1232. 82,10,12,
  1233. 83,12,83,
  1234. 97,6,19,
  1235. 98,10,11,
  1236. 99,12,84,
  1237. 113,6,18,
  1238. 114,10,10,
  1239. 129,6,17,
  1240. 130,10,9,
  1241. 145,6,16,
  1242. 146,10,8,
  1243. 161,7,22,
  1244. 162,12,85,
  1245. 177,7,21,
  1246. 193,7,20,
  1247. 209,8,28,
  1248. 225,8,27,
  1249. 241,9,33,
  1250. 257,9,32,
  1251. 273,9,31,
  1252. 289,9,30,
  1253. 305,9,29,
  1254. 321,9,28,
  1255. 337,9,27,
  1256. 353,9,26,
  1257. 369,11,34,
  1258. 385,11,35,
  1259. 401,12,86,
  1260. 417,12,87,
  1261. 4097,4,7, /* Table for last coeff */
  1262. 4098,9,25,
  1263. 4099,11,5,
  1264. 4113,6,15,
  1265. 4114,11,4,
  1266. 4129,6,14,
  1267. 4145,6,13,
  1268. 4161,6,12,
  1269. 4177,7,19,
  1270. 4193,7,18,
  1271. 4209,7,17,
  1272. 4225,7,16,
  1273. 4241,8,26,
  1274. 4257,8,25,
  1275. 4273,8,24,
  1276. 4289,8,23,
  1277. 4305,8,22,
  1278. 4321,8,21,
  1279. 4337,8,20,
  1280. 4353,8,19,
  1281. 4369,9,24,
  1282. 4385,9,23,
  1283. 4401,9,22,
  1284. 4417,9,21,
  1285. 4433,9,20,
  1286. 4449,9,19,
  1287. 4465,9,18,
  1288. 4481,9,17,
  1289. 4497,10,7,
  1290. 4513,10,6,
  1291. 4529,10,5,
  1292. 4545,10,4,
  1293. 4561,11,36,
  1294. 4577,11,37,
  1295. 4593,11,38,
  1296. 4609,11,39,
  1297. 4625,12,88,
  1298. 4641,12,89,
  1299. 4657,12,90,
  1300. 4673,12,91,
  1301. 4689,12,92,
  1302. 4705,12,93,
  1303. 4721,12,94,
  1304. 4737,12,95,
  1305. 7167,7,3, /* escape */
  1306. -1,-1
  1307. };
  1308. #define MakeStructure(S) (S *) ScAlloc(sizeof(S))
  1309. H263_EHUFF *vlc_3d;
  1310. H263_EHUFF *vlc_cbpcm;
  1311. H263_EHUFF *vlc_cbpcm_intra;
  1312. H263_EHUFF *vlc_cbpy;
  1313. H263_EHUFF *vlc_mv;
  1314. H263_EHUFF *MakeEhuff();
  1315. void FreeEhuff(H263_EHUFF *eh);
  1316. void LoadETable();
  1317. /**********************************************************************
  1318. *
  1319. * Name: InitHuff
  1320. * Description: Initializes vlc-tables
  1321. *
  1322. * Input:
  1323. * Returns:
  1324. * Side effects:
  1325. *
  1326. ***********************************************************************/
  1327. void sv_H263InitHuff(SvH263CompressInfo_t *H263Info)
  1328. {
  1329. _SlibDebug(_VERBOSE_, ScDebugPrintf(NULL, "sv_H263InitHuff()\n") );
  1330. vlc_3d = MakeEhuff(8192);
  1331. vlc_cbpcm = MakeEhuff(256);
  1332. vlc_cbpcm_intra = MakeEhuff(256);
  1333. vlc_cbpy = MakeEhuff(16);
  1334. vlc_mv = MakeEhuff(65);
  1335. LoadETable(vlc_3d_coeff,vlc_3d);
  1336. LoadETable(vlc_cbpcm_coeff,vlc_cbpcm);
  1337. LoadETable(vlc_cbpcm_intra_coeff,vlc_cbpcm_intra);
  1338. LoadETable(vlc_cbpy_coeff,vlc_cbpy);
  1339. LoadETable(vlc_mv_coeff,vlc_mv);
  1340. return;
  1341. }
  1342. /* FreeHuff(): Frees the VLC-tables */
  1343. void sv_H263FreeHuff(SvH263CompressInfo_t *H263Info)
  1344. {
  1345. _SlibDebug(_VERBOSE_, ScDebugPrintf(NULL, "sv_H263FreeHuff()\n") );
  1346. FreeEhuff(vlc_3d);
  1347. FreeEhuff(vlc_cbpcm);
  1348. FreeEhuff(vlc_cbpcm_intra);
  1349. FreeEhuff(vlc_cbpy);
  1350. FreeEhuff(vlc_mv);
  1351. }
  1352. /*
  1353. MakeEhuff() constructs an encoder huff with a designated table-size.
  1354. This table-size, n, is used for the lookup of Huffman values, and must
  1355. represent the largest positive Huffman value.
  1356. */
  1357. H263_EHUFF *MakeEhuff(int n)
  1358. {
  1359. int i;
  1360. H263_EHUFF *temp;
  1361. temp = MakeStructure(H263_EHUFF);
  1362. temp->n = n;
  1363. temp->Hlen = (int *) ScCalloc(n*sizeof(int));
  1364. temp->Hcode = (int *) ScCalloc(n*sizeof(int));
  1365. for(i=0;i<n;i++)
  1366. {
  1367. temp->Hlen[i] = -1;
  1368. temp->Hcode[i] = -1;
  1369. }
  1370. return(temp);
  1371. }
  1372. void FreeEhuff(H263_EHUFF *eh)
  1373. {
  1374. ScFree(eh->Hlen);
  1375. ScFree(eh->Hcode);
  1376. ScFree(eh);
  1377. }
  1378. /*
  1379. LoadETable() is used to load an array into an encoder table. The
  1380. array is grouped in triplets and the first negative value signals the
  1381. end of the table.
  1382. */
  1383. void LoadETable(int *array,H263_EHUFF *table)
  1384. {
  1385. while(*array>=0)
  1386. {
  1387. _SlibDebug(_WARN_ && *array>table->n,
  1388. ScDebugPrintf(NULL, "Table overflow.\n");
  1389. return );
  1390. table->Hlen[*array] = array[1];
  1391. table->Hcode[*array] = array[2];
  1392. array+=3;
  1393. }
  1394. }
  1395. /*
  1396. PrintEhuff() prints the encoder Huffman structure passed into it.
  1397. */
  1398. /*$void PrintEhuff(H263_EHUFF *huff)
  1399. {
  1400. int i;
  1401. printf("Modified Huffman Encoding Structure: %x\n",&huff);
  1402. printf("Number of values %d\n",huff->n);
  1403. for(i=0;i<huff->n;i++)
  1404. {
  1405. if (huff->Hlen[i]>=0)
  1406. {
  1407. printf("Value: %x Length: %d Code: %x\n",
  1408. i,huff->Hlen[i],huff->Hcode[i]);
  1409. }
  1410. }
  1411. }$*/
  1412. /*
  1413. PrintTable() prints out 256 elements in a nice byte ordered fashion.
  1414. */
  1415. #if 0
  1416. void PrintTable(int *table)
  1417. {
  1418. int i,j;
  1419. for(i=0;i<16;i++)
  1420. {
  1421. for(j=0;j<16;j++)
  1422. printf("%2x ",*(table++));
  1423. printf("\n");
  1424. }
  1425. }
  1426. #endif
  1427. /*
  1428. Encode() encodes a symbol according to a designated encoder Huffman
  1429. table out to the stream. It returns the number of bits written to the
  1430. stream and a zero on error.
  1431. */
  1432. static int sv_H263HuffEncode(ScBitstream_t *BSOut, int val,H263_EHUFF *huff)
  1433. {
  1434. if (val < 0)
  1435. {
  1436. _SlibDebug(_WARN_, ScDebugPrintf(NULL, "Out of bounds val:%d.\n",val) );
  1437. return(-1);
  1438. }
  1439. else if (val >= huff->n) {
  1440. return 0; /* No serious error, can occur with some values */
  1441. }
  1442. else if (huff->Hlen[val] < 0) {
  1443. return 0;
  1444. }
  1445. else {
  1446. svH263mputv(huff->Hlen[val],huff->Hcode[val]);
  1447. return(huff->Hlen[val]);
  1448. }
  1449. }
  1450. /*
  1451. char *BitPrint(int length, int val)
  1452. {
  1453. int m;
  1454. char *bit = (char *)ScAlloc(sizeof(char)*(length+3));
  1455. m = length;
  1456. bit[0] = '"';
  1457. while (m--)
  1458. bit[length-m] = (val & (1<<m)) ? '1' : '0';
  1459. bit[length+1] = '"';
  1460. bit[length+2] = '\0';
  1461. return bit;
  1462. }
  1463. */