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.

531 lines
24KB

  1. /*
  2. * H26L/H264/AVC/JVT/14496-10/... encoder/decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file h264data.h
  22. * @brief
  23. * H264 / AVC / MPEG4 part10 codec data table
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #define VERT_PRED 0
  27. #define HOR_PRED 1
  28. #define DC_PRED 2
  29. #define DIAG_DOWN_LEFT_PRED 3
  30. #define DIAG_DOWN_RIGHT_PRED 4
  31. #define VERT_RIGHT_PRED 5
  32. #define HOR_DOWN_PRED 6
  33. #define VERT_LEFT_PRED 7
  34. #define HOR_UP_PRED 8
  35. #define LEFT_DC_PRED 9
  36. #define TOP_DC_PRED 10
  37. #define DC_128_PRED 11
  38. #define DC_PRED8x8 0
  39. #define HOR_PRED8x8 1
  40. #define VERT_PRED8x8 2
  41. #define PLANE_PRED8x8 3
  42. #define LEFT_DC_PRED8x8 4
  43. #define TOP_DC_PRED8x8 5
  44. #define DC_128_PRED8x8 6
  45. #define EXTENDED_SAR 255
  46. static const AVRational pixel_aspect[14]={
  47. {0, 1},
  48. {1, 1},
  49. {12, 11},
  50. {10, 11},
  51. {16, 11},
  52. {40, 33},
  53. {24, 11},
  54. {20, 11},
  55. {32, 11},
  56. {80, 33},
  57. {18, 11},
  58. {15, 11},
  59. {64, 33},
  60. {160,99},
  61. };
  62. static const uint8_t golomb_to_pict_type[5]=
  63. {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  64. static const uint8_t pict_type_to_golomb[7]=
  65. {-1, 2, 0, 1, -1, 4, 3};
  66. static const uint8_t chroma_qp[52]={
  67. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,
  68. 12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,
  69. 28,29,29,30,31,32,32,33,34,34,35,35,36,36,37,37,
  70. 37,38,38,38,39,39,39,39
  71. };
  72. static const uint8_t golomb_to_intra4x4_cbp[48]={
  73. 47, 31, 15, 0, 23, 27, 29, 30, 7, 11, 13, 14, 39, 43, 45, 46,
  74. 16, 3, 5, 10, 12, 19, 21, 26, 28, 35, 37, 42, 44, 1, 2, 4,
  75. 8, 17, 18, 20, 24, 6, 9, 22, 25, 32, 33, 34, 36, 40, 38, 41
  76. };
  77. static const uint8_t golomb_to_inter_cbp[48]={
  78. 0, 16, 1, 2, 4, 8, 32, 3, 5, 10, 12, 15, 47, 7, 11, 13,
  79. 14, 6, 9, 31, 35, 37, 42, 44, 33, 34, 36, 40, 39, 43, 45, 46,
  80. 17, 18, 20, 24, 19, 21, 26, 28, 23, 27, 29, 30, 22, 25, 38, 41
  81. };
  82. static const uint8_t intra4x4_cbp_to_golomb[48]={
  83. 3, 29, 30, 17, 31, 18, 37, 8, 32, 38, 19, 9, 20, 10, 11, 2,
  84. 16, 33, 34, 21, 35, 22, 39, 4, 36, 40, 23, 5, 24, 6, 7, 1,
  85. 41, 42, 43, 25, 44, 26, 46, 12, 45, 47, 27, 13, 28, 14, 15, 0
  86. };
  87. static const uint8_t inter_cbp_to_golomb[48]={
  88. 0, 2, 3, 7, 4, 8, 17, 13, 5, 18, 9, 14, 10, 15, 16, 11,
  89. 1, 32, 33, 36, 34, 37, 44, 40, 35, 45, 38, 41, 39, 42, 43, 19,
  90. 6, 24, 25, 20, 26, 21, 46, 28, 27, 47, 22, 29, 23, 30, 31, 12
  91. };
  92. static const uint8_t chroma_dc_coeff_token_len[4*5]={
  93. 2, 0, 0, 0,
  94. 6, 1, 0, 0,
  95. 6, 6, 3, 0,
  96. 6, 7, 7, 6,
  97. 6, 8, 8, 7,
  98. };
  99. static const uint8_t chroma_dc_coeff_token_bits[4*5]={
  100. 1, 0, 0, 0,
  101. 7, 1, 0, 0,
  102. 4, 6, 1, 0,
  103. 3, 3, 2, 5,
  104. 2, 3, 2, 0,
  105. };
  106. static const uint8_t coeff_token_len[4][4*17]={
  107. {
  108. 1, 0, 0, 0,
  109. 6, 2, 0, 0, 8, 6, 3, 0, 9, 8, 7, 5, 10, 9, 8, 6,
  110. 11,10, 9, 7, 13,11,10, 8, 13,13,11, 9, 13,13,13,10,
  111. 14,14,13,11, 14,14,14,13, 15,15,14,14, 15,15,15,14,
  112. 16,15,15,15, 16,16,16,15, 16,16,16,16, 16,16,16,16,
  113. },
  114. {
  115. 2, 0, 0, 0,
  116. 6, 2, 0, 0, 6, 5, 3, 0, 7, 6, 6, 4, 8, 6, 6, 4,
  117. 8, 7, 7, 5, 9, 8, 8, 6, 11, 9, 9, 6, 11,11,11, 7,
  118. 12,11,11, 9, 12,12,12,11, 12,12,12,11, 13,13,13,12,
  119. 13,13,13,13, 13,14,13,13, 14,14,14,13, 14,14,14,14,
  120. },
  121. {
  122. 4, 0, 0, 0,
  123. 6, 4, 0, 0, 6, 5, 4, 0, 6, 5, 5, 4, 7, 5, 5, 4,
  124. 7, 5, 5, 4, 7, 6, 6, 4, 7, 6, 6, 4, 8, 7, 7, 5,
  125. 8, 8, 7, 6, 9, 8, 8, 7, 9, 9, 8, 8, 9, 9, 9, 8,
  126. 10, 9, 9, 9, 10,10,10,10, 10,10,10,10, 10,10,10,10,
  127. },
  128. {
  129. 6, 0, 0, 0,
  130. 6, 6, 0, 0, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6,
  131. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  132. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  133. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  134. }
  135. };
  136. static const uint8_t coeff_token_bits[4][4*17]={
  137. {
  138. 1, 0, 0, 0,
  139. 5, 1, 0, 0, 7, 4, 1, 0, 7, 6, 5, 3, 7, 6, 5, 3,
  140. 7, 6, 5, 4, 15, 6, 5, 4, 11,14, 5, 4, 8,10,13, 4,
  141. 15,14, 9, 4, 11,10,13,12, 15,14, 9,12, 11,10,13, 8,
  142. 15, 1, 9,12, 11,14,13, 8, 7,10, 9,12, 4, 6, 5, 8,
  143. },
  144. {
  145. 3, 0, 0, 0,
  146. 11, 2, 0, 0, 7, 7, 3, 0, 7,10, 9, 5, 7, 6, 5, 4,
  147. 4, 6, 5, 6, 7, 6, 5, 8, 15, 6, 5, 4, 11,14,13, 4,
  148. 15,10, 9, 4, 11,14,13,12, 8,10, 9, 8, 15,14,13,12,
  149. 11,10, 9,12, 7,11, 6, 8, 9, 8,10, 1, 7, 6, 5, 4,
  150. },
  151. {
  152. 15, 0, 0, 0,
  153. 15,14, 0, 0, 11,15,13, 0, 8,12,14,12, 15,10,11,11,
  154. 11, 8, 9,10, 9,14,13, 9, 8,10, 9, 8, 15,14,13,13,
  155. 11,14,10,12, 15,10,13,12, 11,14, 9,12, 8,10,13, 8,
  156. 13, 7, 9,12, 9,12,11,10, 5, 8, 7, 6, 1, 4, 3, 2,
  157. },
  158. {
  159. 3, 0, 0, 0,
  160. 0, 1, 0, 0, 4, 5, 6, 0, 8, 9,10,11, 12,13,14,15,
  161. 16,17,18,19, 20,21,22,23, 24,25,26,27, 28,29,30,31,
  162. 32,33,34,35, 36,37,38,39, 40,41,42,43, 44,45,46,47,
  163. 48,49,50,51, 52,53,54,55, 56,57,58,59, 60,61,62,63,
  164. }
  165. };
  166. static const uint8_t total_zeros_len[16][16]= {
  167. {1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9},
  168. {3,3,3,3,3,4,4,4,4,5,5,6,6,6,6},
  169. {4,3,3,3,4,4,3,3,4,5,5,6,5,6},
  170. {5,3,4,4,3,3,3,4,3,4,5,5,5},
  171. {4,4,4,3,3,3,3,3,4,5,4,5},
  172. {6,5,3,3,3,3,3,3,4,3,6},
  173. {6,5,3,3,3,2,3,4,3,6},
  174. {6,4,5,3,2,2,3,3,6},
  175. {6,6,4,2,2,3,2,5},
  176. {5,5,3,2,2,2,4},
  177. {4,4,3,3,1,3},
  178. {4,4,2,1,3},
  179. {3,3,1,2},
  180. {2,2,1},
  181. {1,1},
  182. };
  183. static const uint8_t total_zeros_bits[16][16]= {
  184. {1,3,2,3,2,3,2,3,2,3,2,3,2,3,2,1},
  185. {7,6,5,4,3,5,4,3,2,3,2,3,2,1,0},
  186. {5,7,6,5,4,3,4,3,2,3,2,1,1,0},
  187. {3,7,5,4,6,5,4,3,3,2,2,1,0},
  188. {5,4,3,7,6,5,4,3,2,1,1,0},
  189. {1,1,7,6,5,4,3,2,1,1,0},
  190. {1,1,5,4,3,3,2,1,1,0},
  191. {1,1,1,3,3,2,2,1,0},
  192. {1,0,1,3,2,1,1,1},
  193. {1,0,1,3,2,1,1},
  194. {0,1,1,2,1,3},
  195. {0,1,1,1,1},
  196. {0,1,1,1},
  197. {0,1,1},
  198. {0,1},
  199. };
  200. static const uint8_t chroma_dc_total_zeros_len[3][4]= {
  201. { 1, 2, 3, 3,},
  202. { 1, 2, 2, 0,},
  203. { 1, 1, 0, 0,},
  204. };
  205. static const uint8_t chroma_dc_total_zeros_bits[3][4]= {
  206. { 1, 1, 1, 0,},
  207. { 1, 1, 0, 0,},
  208. { 1, 0, 0, 0,},
  209. };
  210. static const uint8_t run_len[7][16]={
  211. {1,1},
  212. {1,2,2},
  213. {2,2,2,2},
  214. {2,2,2,3,3},
  215. {2,2,3,3,3,3},
  216. {2,3,3,3,3,3,3},
  217. {3,3,3,3,3,3,3,4,5,6,7,8,9,10,11},
  218. };
  219. static const uint8_t run_bits[7][16]={
  220. {1,0},
  221. {1,1,0},
  222. {3,2,1,0},
  223. {3,2,1,1,0},
  224. {3,2,3,2,1,0},
  225. {3,0,1,3,2,5,4},
  226. {7,6,5,4,3,2,1,1,1,1,1,1,1,1,1},
  227. };
  228. /*
  229. o-o o-o
  230. / / /
  231. o-o o-o
  232. ,---'
  233. o-o o-o
  234. / / /
  235. o-o o-o
  236. */
  237. static const uint8_t scan8[16 + 2*4]={
  238. 4+1*8, 5+1*8, 4+2*8, 5+2*8,
  239. 6+1*8, 7+1*8, 6+2*8, 7+2*8,
  240. 4+3*8, 5+3*8, 4+4*8, 5+4*8,
  241. 6+3*8, 7+3*8, 6+4*8, 7+4*8,
  242. 1+1*8, 2+1*8,
  243. 1+2*8, 2+2*8,
  244. 1+4*8, 2+4*8,
  245. 1+5*8, 2+5*8,
  246. };
  247. static const uint8_t zigzag_scan[16]={
  248. 0+0*4, 1+0*4, 0+1*4, 0+2*4,
  249. 1+1*4, 2+0*4, 3+0*4, 2+1*4,
  250. 1+2*4, 0+3*4, 1+3*4, 2+2*4,
  251. 3+1*4, 3+2*4, 2+3*4, 3+3*4,
  252. };
  253. static const uint8_t field_scan[16]={
  254. 0+0*4, 0+1*4, 1+0*4, 0+2*4,
  255. 0+3*4, 1+1*4, 1+2*4, 1+3*4,
  256. 2+0*4, 2+1*4, 2+2*4, 2+3*4,
  257. 3+0*4, 3+1*4, 3+2*4, 3+3*4,
  258. };
  259. static const uint8_t luma_dc_zigzag_scan[16]={
  260. 0*16 + 0*64, 1*16 + 0*64, 2*16 + 0*64, 0*16 + 2*64,
  261. 3*16 + 0*64, 0*16 + 1*64, 1*16 + 1*64, 2*16 + 1*64,
  262. 1*16 + 2*64, 2*16 + 2*64, 3*16 + 2*64, 0*16 + 3*64,
  263. 3*16 + 1*64, 1*16 + 3*64, 2*16 + 3*64, 3*16 + 3*64,
  264. };
  265. static const uint8_t luma_dc_field_scan[16]={
  266. 0*16 + 0*64, 2*16 + 0*64, 1*16 + 0*64, 0*16 + 2*64,
  267. 2*16 + 2*64, 3*16 + 0*64, 1*16 + 2*64, 3*16 + 2*64,
  268. 0*16 + 1*64, 2*16 + 1*64, 0*16 + 3*64, 2*16 + 3*64,
  269. 1*16 + 1*64, 3*16 + 1*64, 1*16 + 3*64, 3*16 + 3*64,
  270. };
  271. static const uint8_t chroma_dc_scan[4]={
  272. (0+0*2)*16, (1+0*2)*16,
  273. (0+1*2)*16, (1+1*2)*16, //FIXME
  274. };
  275. #define MB_TYPE_REF0 MB_TYPE_ACPRED //dirty but it fits in 16bit
  276. #define IS_REF0(a) ((a)&MB_TYPE_REF0)
  277. typedef struct IMbInfo{
  278. uint16_t type;
  279. uint8_t pred_mode;
  280. uint8_t cbp;
  281. } IMbInfo;
  282. static const IMbInfo i_mb_type_info[26]={
  283. {MB_TYPE_INTRA4x4 , -1, -1},
  284. {MB_TYPE_INTRA16x16, 2, 0},
  285. {MB_TYPE_INTRA16x16, 1, 0},
  286. {MB_TYPE_INTRA16x16, 0, 0},
  287. {MB_TYPE_INTRA16x16, 3, 0},
  288. {MB_TYPE_INTRA16x16, 2, 16},
  289. {MB_TYPE_INTRA16x16, 1, 16},
  290. {MB_TYPE_INTRA16x16, 0, 16},
  291. {MB_TYPE_INTRA16x16, 3, 16},
  292. {MB_TYPE_INTRA16x16, 2, 32},
  293. {MB_TYPE_INTRA16x16, 1, 32},
  294. {MB_TYPE_INTRA16x16, 0, 32},
  295. {MB_TYPE_INTRA16x16, 3, 32},
  296. {MB_TYPE_INTRA16x16, 2, 15+0},
  297. {MB_TYPE_INTRA16x16, 1, 15+0},
  298. {MB_TYPE_INTRA16x16, 0, 15+0},
  299. {MB_TYPE_INTRA16x16, 3, 15+0},
  300. {MB_TYPE_INTRA16x16, 2, 15+16},
  301. {MB_TYPE_INTRA16x16, 1, 15+16},
  302. {MB_TYPE_INTRA16x16, 0, 15+16},
  303. {MB_TYPE_INTRA16x16, 3, 15+16},
  304. {MB_TYPE_INTRA16x16, 2, 15+32},
  305. {MB_TYPE_INTRA16x16, 1, 15+32},
  306. {MB_TYPE_INTRA16x16, 0, 15+32},
  307. {MB_TYPE_INTRA16x16, 3, 15+32},
  308. {MB_TYPE_INTRA_PCM , -1, -1},
  309. };
  310. typedef struct PMbInfo{
  311. uint16_t type;
  312. uint8_t partition_count;
  313. } PMbInfo;
  314. static const PMbInfo p_mb_type_info[5]={
  315. {MB_TYPE_16x16|MB_TYPE_P0L0 , 1},
  316. {MB_TYPE_16x8 |MB_TYPE_P0L0|MB_TYPE_P1L0, 2},
  317. {MB_TYPE_8x16 |MB_TYPE_P0L0|MB_TYPE_P1L0, 2},
  318. {MB_TYPE_8x8 , 4},
  319. {MB_TYPE_8x8 |MB_TYPE_REF0 , 4},
  320. };
  321. static const PMbInfo p_sub_mb_type_info[4]={
  322. {MB_TYPE_16x16|MB_TYPE_P0L0 , 1},
  323. {MB_TYPE_16x8 |MB_TYPE_P0L0 , 2},
  324. {MB_TYPE_8x16 |MB_TYPE_P0L0 , 2},
  325. {MB_TYPE_8x8 |MB_TYPE_P0L0 , 4},
  326. };
  327. static const PMbInfo b_mb_type_info[23]={
  328. {MB_TYPE_DIRECT2 , 1, },
  329. {MB_TYPE_16x16|MB_TYPE_P0L0 , 1, },
  330. {MB_TYPE_16x16 |MB_TYPE_P0L1 , 1, },
  331. {MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1 , 1, },
  332. {MB_TYPE_16x8 |MB_TYPE_P0L0 |MB_TYPE_P1L0 , 2, },
  333. {MB_TYPE_8x16 |MB_TYPE_P0L0 |MB_TYPE_P1L0 , 2, },
  334. {MB_TYPE_16x8 |MB_TYPE_P0L1 |MB_TYPE_P1L1, 2, },
  335. {MB_TYPE_8x16 |MB_TYPE_P0L1 |MB_TYPE_P1L1, 2, },
  336. {MB_TYPE_16x8 |MB_TYPE_P0L0 |MB_TYPE_P1L1, 2, },
  337. {MB_TYPE_8x16 |MB_TYPE_P0L0 |MB_TYPE_P1L1, 2, },
  338. {MB_TYPE_16x8 |MB_TYPE_P0L1|MB_TYPE_P1L0 , 2, },
  339. {MB_TYPE_8x16 |MB_TYPE_P0L1|MB_TYPE_P1L0 , 2, },
  340. {MB_TYPE_16x8 |MB_TYPE_P0L0 |MB_TYPE_P1L0|MB_TYPE_P1L1, 2, },
  341. {MB_TYPE_8x16 |MB_TYPE_P0L0 |MB_TYPE_P1L0|MB_TYPE_P1L1, 2, },
  342. {MB_TYPE_16x8 |MB_TYPE_P0L1|MB_TYPE_P1L0|MB_TYPE_P1L1, 2, },
  343. {MB_TYPE_8x16 |MB_TYPE_P0L1|MB_TYPE_P1L0|MB_TYPE_P1L1, 2, },
  344. {MB_TYPE_16x8 |MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_P1L0 , 2, },
  345. {MB_TYPE_8x16 |MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_P1L0 , 2, },
  346. {MB_TYPE_16x8 |MB_TYPE_P0L0|MB_TYPE_P0L1 |MB_TYPE_P1L1, 2, },
  347. {MB_TYPE_8x16 |MB_TYPE_P0L0|MB_TYPE_P0L1 |MB_TYPE_P1L1, 2, },
  348. {MB_TYPE_16x8 |MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_P1L0|MB_TYPE_P1L1, 2, },
  349. {MB_TYPE_8x16 |MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_P1L0|MB_TYPE_P1L1, 2, },
  350. {MB_TYPE_8x8 , 4, },
  351. };
  352. static const PMbInfo b_sub_mb_type_info[13]={
  353. {MB_TYPE_DIRECT2 , 1, },
  354. {MB_TYPE_16x16|MB_TYPE_P0L0 , 1, },
  355. {MB_TYPE_16x16 |MB_TYPE_P0L1 , 1, },
  356. {MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1 , 1, },
  357. {MB_TYPE_16x8 |MB_TYPE_P0L0 |MB_TYPE_P1L0 , 2, },
  358. {MB_TYPE_8x16 |MB_TYPE_P0L0 |MB_TYPE_P1L0 , 2, },
  359. {MB_TYPE_16x8 |MB_TYPE_P0L1 |MB_TYPE_P1L1, 2, },
  360. {MB_TYPE_8x16 |MB_TYPE_P0L1 |MB_TYPE_P1L1, 2, },
  361. {MB_TYPE_16x8 |MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_P1L0|MB_TYPE_P1L1, 2, },
  362. {MB_TYPE_8x16 |MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_P1L0|MB_TYPE_P1L1, 2, },
  363. {MB_TYPE_8x8 |MB_TYPE_P0L0 |MB_TYPE_P1L0 , 4, },
  364. {MB_TYPE_8x8 |MB_TYPE_P0L1 |MB_TYPE_P1L1, 4, },
  365. {MB_TYPE_8x8 |MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_P1L0|MB_TYPE_P1L1, 4, },
  366. };
  367. static const uint8_t rem6[52]={
  368. 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
  369. };
  370. static const uint8_t div6[52]={
  371. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
  372. };
  373. static const uint16_t dequant_coeff[52][16]={
  374. { 10, 13, 10, 13, 13, 16, 13, 16, 10, 13, 10, 13, 13, 16, 13, 16, },
  375. { 11, 14, 11, 14, 14, 18, 14, 18, 11, 14, 11, 14, 14, 18, 14, 18, },
  376. { 13, 16, 13, 16, 16, 20, 16, 20, 13, 16, 13, 16, 16, 20, 16, 20, },
  377. { 14, 18, 14, 18, 18, 23, 18, 23, 14, 18, 14, 18, 18, 23, 18, 23, },
  378. { 16, 20, 16, 20, 20, 25, 20, 25, 16, 20, 16, 20, 20, 25, 20, 25, },
  379. { 18, 23, 18, 23, 23, 29, 23, 29, 18, 23, 18, 23, 23, 29, 23, 29, },
  380. { 20, 26, 20, 26, 26, 32, 26, 32, 20, 26, 20, 26, 26, 32, 26, 32, },
  381. { 22, 28, 22, 28, 28, 36, 28, 36, 22, 28, 22, 28, 28, 36, 28, 36, },
  382. { 26, 32, 26, 32, 32, 40, 32, 40, 26, 32, 26, 32, 32, 40, 32, 40, },
  383. { 28, 36, 28, 36, 36, 46, 36, 46, 28, 36, 28, 36, 36, 46, 36, 46, },
  384. { 32, 40, 32, 40, 40, 50, 40, 50, 32, 40, 32, 40, 40, 50, 40, 50, },
  385. { 36, 46, 36, 46, 46, 58, 46, 58, 36, 46, 36, 46, 46, 58, 46, 58, },
  386. { 40, 52, 40, 52, 52, 64, 52, 64, 40, 52, 40, 52, 52, 64, 52, 64, },
  387. { 44, 56, 44, 56, 56, 72, 56, 72, 44, 56, 44, 56, 56, 72, 56, 72, },
  388. { 52, 64, 52, 64, 64, 80, 64, 80, 52, 64, 52, 64, 64, 80, 64, 80, },
  389. { 56, 72, 56, 72, 72, 92, 72, 92, 56, 72, 56, 72, 72, 92, 72, 92, },
  390. { 64, 80, 64, 80, 80, 100, 80, 100, 64, 80, 64, 80, 80, 100, 80, 100, },
  391. { 72, 92, 72, 92, 92, 116, 92, 116, 72, 92, 72, 92, 92, 116, 92, 116, },
  392. { 80, 104, 80, 104, 104, 128, 104, 128, 80, 104, 80, 104, 104, 128, 104, 128, },
  393. { 88, 112, 88, 112, 112, 144, 112, 144, 88, 112, 88, 112, 112, 144, 112, 144, },
  394. { 104, 128, 104, 128, 128, 160, 128, 160, 104, 128, 104, 128, 128, 160, 128, 160, },
  395. { 112, 144, 112, 144, 144, 184, 144, 184, 112, 144, 112, 144, 144, 184, 144, 184, },
  396. { 128, 160, 128, 160, 160, 200, 160, 200, 128, 160, 128, 160, 160, 200, 160, 200, },
  397. { 144, 184, 144, 184, 184, 232, 184, 232, 144, 184, 144, 184, 184, 232, 184, 232, },
  398. { 160, 208, 160, 208, 208, 256, 208, 256, 160, 208, 160, 208, 208, 256, 208, 256, },
  399. { 176, 224, 176, 224, 224, 288, 224, 288, 176, 224, 176, 224, 224, 288, 224, 288, },
  400. { 208, 256, 208, 256, 256, 320, 256, 320, 208, 256, 208, 256, 256, 320, 256, 320, },
  401. { 224, 288, 224, 288, 288, 368, 288, 368, 224, 288, 224, 288, 288, 368, 288, 368, },
  402. { 256, 320, 256, 320, 320, 400, 320, 400, 256, 320, 256, 320, 320, 400, 320, 400, },
  403. { 288, 368, 288, 368, 368, 464, 368, 464, 288, 368, 288, 368, 368, 464, 368, 464, },
  404. { 320, 416, 320, 416, 416, 512, 416, 512, 320, 416, 320, 416, 416, 512, 416, 512, },
  405. { 352, 448, 352, 448, 448, 576, 448, 576, 352, 448, 352, 448, 448, 576, 448, 576, },
  406. { 416, 512, 416, 512, 512, 640, 512, 640, 416, 512, 416, 512, 512, 640, 512, 640, },
  407. { 448, 576, 448, 576, 576, 736, 576, 736, 448, 576, 448, 576, 576, 736, 576, 736, },
  408. { 512, 640, 512, 640, 640, 800, 640, 800, 512, 640, 512, 640, 640, 800, 640, 800, },
  409. { 576, 736, 576, 736, 736, 928, 736, 928, 576, 736, 576, 736, 736, 928, 736, 928, },
  410. { 640, 832, 640, 832, 832,1024, 832,1024, 640, 832, 640, 832, 832,1024, 832,1024, },
  411. { 704, 896, 704, 896, 896,1152, 896,1152, 704, 896, 704, 896, 896,1152, 896,1152, },
  412. { 832,1024, 832,1024, 1024,1280,1024,1280, 832,1024, 832,1024, 1024,1280,1024,1280, },
  413. { 896,1152, 896,1152, 1152,1472,1152,1472, 896,1152, 896,1152, 1152,1472,1152,1472, },
  414. {1024,1280,1024,1280, 1280,1600,1280,1600, 1024,1280,1024,1280, 1280,1600,1280,1600, },
  415. {1152,1472,1152,1472, 1472,1856,1472,1856, 1152,1472,1152,1472, 1472,1856,1472,1856, },
  416. {1280,1664,1280,1664, 1664,2048,1664,2048, 1280,1664,1280,1664, 1664,2048,1664,2048, },
  417. {1408,1792,1408,1792, 1792,2304,1792,2304, 1408,1792,1408,1792, 1792,2304,1792,2304, },
  418. {1664,2048,1664,2048, 2048,2560,2048,2560, 1664,2048,1664,2048, 2048,2560,2048,2560, },
  419. {1792,2304,1792,2304, 2304,2944,2304,2944, 1792,2304,1792,2304, 2304,2944,2304,2944, },
  420. {2048,2560,2048,2560, 2560,3200,2560,3200, 2048,2560,2048,2560, 2560,3200,2560,3200, },
  421. {2304,2944,2304,2944, 2944,3712,2944,3712, 2304,2944,2304,2944, 2944,3712,2944,3712, },
  422. {2560,3328,2560,3328, 3328,4096,3328,4096, 2560,3328,2560,3328, 3328,4096,3328,4096, },
  423. {2816,3584,2816,3584, 3584,4608,3584,4608, 2816,3584,2816,3584, 3584,4608,3584,4608, },
  424. {3328,4096,3328,4096, 4096,5120,4096,5120, 3328,4096,3328,4096, 4096,5120,4096,5120, },
  425. {3584,4608,3584,4608, 4608,5888,4608,5888, 3584,4608,3584,4608, 4608,5888,4608,5888, },
  426. //{4096,5120,4096,5120, 5120,6400,5120,6400, 4096,5120,4096,5120, 5120,6400,5120,6400, },
  427. //{4608,5888,4608,5888, 5888,7424,5888,7424, 4608,5888,4608,5888, 5888,7424,5888,7424, },
  428. };
  429. #define QUANT_SHIFT 22
  430. static const int quant_coeff[52][16]={
  431. { 419430,258111,419430,258111,258111,167772,258111,167772,419430,258111,419430,258111,258111,167772,258111,167772,},
  432. { 381300,239675,381300,239675,239675,149131,239675,149131,381300,239675,381300,239675,239675,149131,239675,149131,},
  433. { 322639,209715,322639,209715,209715,134218,209715,134218,322639,209715,322639,209715,209715,134218,209715,134218,},
  434. { 299593,186414,299593,186414,186414,116711,186414,116711,299593,186414,299593,186414,186414,116711,186414,116711,},
  435. { 262144,167772,262144,167772,167772,107374,167772,107374,262144,167772,262144,167772,167772,107374,167772,107374,},
  436. { 233017,145889,233017,145889,145889, 92564,145889, 92564,233017,145889,233017,145889,145889, 92564,145889, 92564,},
  437. { 209715,129056,209715,129056,129056, 83886,129056, 83886,209715,129056,209715,129056,129056, 83886,129056, 83886,},
  438. { 190650,119837,190650,119837,119837, 74565,119837, 74565,190650,119837,190650,119837,119837, 74565,119837, 74565,},
  439. { 161319,104858,161319,104858,104858, 67109,104858, 67109,161319,104858,161319,104858,104858, 67109,104858, 67109,},
  440. { 149797, 93207,149797, 93207, 93207, 58356, 93207, 58356,149797, 93207,149797, 93207, 93207, 58356, 93207, 58356,},
  441. { 131072, 83886,131072, 83886, 83886, 53687, 83886, 53687,131072, 83886,131072, 83886, 83886, 53687, 83886, 53687,},
  442. { 116508, 72944,116508, 72944, 72944, 46282, 72944, 46282,116508, 72944,116508, 72944, 72944, 46282, 72944, 46282,},
  443. { 104858, 64528,104858, 64528, 64528, 41943, 64528, 41943,104858, 64528,104858, 64528, 64528, 41943, 64528, 41943,},
  444. { 95325, 59919, 95325, 59919, 59919, 37283, 59919, 37283, 95325, 59919, 95325, 59919, 59919, 37283, 59919, 37283,},
  445. { 80660, 52429, 80660, 52429, 52429, 33554, 52429, 33554, 80660, 52429, 80660, 52429, 52429, 33554, 52429, 33554,},
  446. { 74898, 46603, 74898, 46603, 46603, 29178, 46603, 29178, 74898, 46603, 74898, 46603, 46603, 29178, 46603, 29178,},
  447. { 65536, 41943, 65536, 41943, 41943, 26844, 41943, 26844, 65536, 41943, 65536, 41943, 41943, 26844, 41943, 26844,},
  448. { 58254, 36472, 58254, 36472, 36472, 23141, 36472, 23141, 58254, 36472, 58254, 36472, 36472, 23141, 36472, 23141,},
  449. { 52429, 32264, 52429, 32264, 32264, 20972, 32264, 20972, 52429, 32264, 52429, 32264, 32264, 20972, 32264, 20972,},
  450. { 47663, 29959, 47663, 29959, 29959, 18641, 29959, 18641, 47663, 29959, 47663, 29959, 29959, 18641, 29959, 18641,},
  451. { 40330, 26214, 40330, 26214, 26214, 16777, 26214, 16777, 40330, 26214, 40330, 26214, 26214, 16777, 26214, 16777,},
  452. { 37449, 23302, 37449, 23302, 23302, 14589, 23302, 14589, 37449, 23302, 37449, 23302, 23302, 14589, 23302, 14589,},
  453. { 32768, 20972, 32768, 20972, 20972, 13422, 20972, 13422, 32768, 20972, 32768, 20972, 20972, 13422, 20972, 13422,},
  454. { 29127, 18236, 29127, 18236, 18236, 11570, 18236, 11570, 29127, 18236, 29127, 18236, 18236, 11570, 18236, 11570,},
  455. { 26214, 16132, 26214, 16132, 16132, 10486, 16132, 10486, 26214, 16132, 26214, 16132, 16132, 10486, 16132, 10486,},
  456. { 23831, 14980, 23831, 14980, 14980, 9321, 14980, 9321, 23831, 14980, 23831, 14980, 14980, 9321, 14980, 9321,},
  457. { 20165, 13107, 20165, 13107, 13107, 8389, 13107, 8389, 20165, 13107, 20165, 13107, 13107, 8389, 13107, 8389,},
  458. { 18725, 11651, 18725, 11651, 11651, 7294, 11651, 7294, 18725, 11651, 18725, 11651, 11651, 7294, 11651, 7294,},
  459. { 16384, 10486, 16384, 10486, 10486, 6711, 10486, 6711, 16384, 10486, 16384, 10486, 10486, 6711, 10486, 6711,},
  460. { 14564, 9118, 14564, 9118, 9118, 5785, 9118, 5785, 14564, 9118, 14564, 9118, 9118, 5785, 9118, 5785,},
  461. { 13107, 8066, 13107, 8066, 8066, 5243, 8066, 5243, 13107, 8066, 13107, 8066, 8066, 5243, 8066, 5243,},
  462. { 11916, 7490, 11916, 7490, 7490, 4660, 7490, 4660, 11916, 7490, 11916, 7490, 7490, 4660, 7490, 4660,},
  463. { 10082, 6554, 10082, 6554, 6554, 4194, 6554, 4194, 10082, 6554, 10082, 6554, 6554, 4194, 6554, 4194,},
  464. { 9362, 5825, 9362, 5825, 5825, 3647, 5825, 3647, 9362, 5825, 9362, 5825, 5825, 3647, 5825, 3647,},
  465. { 8192, 5243, 8192, 5243, 5243, 3355, 5243, 3355, 8192, 5243, 8192, 5243, 5243, 3355, 5243, 3355,},
  466. { 7282, 4559, 7282, 4559, 4559, 2893, 4559, 2893, 7282, 4559, 7282, 4559, 4559, 2893, 4559, 2893,},
  467. { 6554, 4033, 6554, 4033, 4033, 2621, 4033, 2621, 6554, 4033, 6554, 4033, 4033, 2621, 4033, 2621,},
  468. { 5958, 3745, 5958, 3745, 3745, 2330, 3745, 2330, 5958, 3745, 5958, 3745, 3745, 2330, 3745, 2330,},
  469. { 5041, 3277, 5041, 3277, 3277, 2097, 3277, 2097, 5041, 3277, 5041, 3277, 3277, 2097, 3277, 2097,},
  470. { 4681, 2913, 4681, 2913, 2913, 1824, 2913, 1824, 4681, 2913, 4681, 2913, 2913, 1824, 2913, 1824,},
  471. { 4096, 2621, 4096, 2621, 2621, 1678, 2621, 1678, 4096, 2621, 4096, 2621, 2621, 1678, 2621, 1678,},
  472. { 3641, 2280, 3641, 2280, 2280, 1446, 2280, 1446, 3641, 2280, 3641, 2280, 2280, 1446, 2280, 1446,},
  473. { 3277, 2016, 3277, 2016, 2016, 1311, 2016, 1311, 3277, 2016, 3277, 2016, 2016, 1311, 2016, 1311,},
  474. { 2979, 1872, 2979, 1872, 1872, 1165, 1872, 1165, 2979, 1872, 2979, 1872, 1872, 1165, 1872, 1165,},
  475. { 2521, 1638, 2521, 1638, 1638, 1049, 1638, 1049, 2521, 1638, 2521, 1638, 1638, 1049, 1638, 1049,},
  476. { 2341, 1456, 2341, 1456, 1456, 912, 1456, 912, 2341, 1456, 2341, 1456, 1456, 912, 1456, 912,},
  477. { 2048, 1311, 2048, 1311, 1311, 839, 1311, 839, 2048, 1311, 2048, 1311, 1311, 839, 1311, 839,},
  478. { 1820, 1140, 1820, 1140, 1140, 723, 1140, 723, 1820, 1140, 1820, 1140, 1140, 723, 1140, 723,},
  479. { 1638, 1008, 1638, 1008, 1008, 655, 1008, 655, 1638, 1008, 1638, 1008, 1008, 655, 1008, 655,},
  480. { 1489, 936, 1489, 936, 936, 583, 936, 583, 1489, 936, 1489, 936, 936, 583, 936, 583,},
  481. { 1260, 819, 1260, 819, 819, 524, 819, 524, 1260, 819, 1260, 819, 819, 524, 819, 524,},
  482. { 1170, 728, 1170, 728, 728, 456, 728, 456, 1170, 728, 1170, 728, 728, 456, 728, 456,},
  483. };