enquant.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 *
  9. * by the Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function:
  13. last mod: $Id: enquant.c 16503 2009-08-22 18:14:02Z giles $
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "encint.h"
  18. void oc_quant_params_pack(oggpack_buffer *_opb,const th_quant_info *_qinfo){
  19. const th_quant_ranges *qranges;
  20. const th_quant_base *base_mats[2*3*64];
  21. int indices[2][3][64];
  22. int nbase_mats;
  23. int nbits;
  24. int ci;
  25. int qi;
  26. int qri;
  27. int qti;
  28. int pli;
  29. int qtj;
  30. int plj;
  31. int bmi;
  32. int i;
  33. i=_qinfo->loop_filter_limits[0];
  34. for(qi=1;qi<64;qi++)i=OC_MAXI(i,_qinfo->loop_filter_limits[qi]);
  35. nbits=OC_ILOG_32(i);
  36. oggpackB_write(_opb,nbits,3);
  37. for(qi=0;qi<64;qi++){
  38. oggpackB_write(_opb,_qinfo->loop_filter_limits[qi],nbits);
  39. }
  40. /*580 bits for VP3.*/
  41. i=1;
  42. for(qi=0;qi<64;qi++)i=OC_MAXI(_qinfo->ac_scale[qi],i);
  43. nbits=OC_ILOGNZ_32(i);
  44. oggpackB_write(_opb,nbits-1,4);
  45. for(qi=0;qi<64;qi++)oggpackB_write(_opb,_qinfo->ac_scale[qi],nbits);
  46. /*516 bits for VP3.*/
  47. i=1;
  48. for(qi=0;qi<64;qi++)i=OC_MAXI(_qinfo->dc_scale[qi],i);
  49. nbits=OC_ILOGNZ_32(i);
  50. oggpackB_write(_opb,nbits-1,4);
  51. for(qi=0;qi<64;qi++)oggpackB_write(_opb,_qinfo->dc_scale[qi],nbits);
  52. /*Consolidate any duplicate base matrices.*/
  53. nbase_mats=0;
  54. for(qti=0;qti<2;qti++)for(pli=0;pli<3;pli++){
  55. qranges=_qinfo->qi_ranges[qti]+pli;
  56. for(qri=0;qri<=qranges->nranges;qri++){
  57. for(bmi=0;;bmi++){
  58. if(bmi>=nbase_mats){
  59. base_mats[bmi]=qranges->base_matrices+qri;
  60. indices[qti][pli][qri]=nbase_mats++;
  61. break;
  62. }
  63. else if(memcmp(base_mats[bmi][0],qranges->base_matrices[qri],
  64. sizeof(base_mats[bmi][0]))==0){
  65. indices[qti][pli][qri]=bmi;
  66. break;
  67. }
  68. }
  69. }
  70. }
  71. /*Write out the list of unique base matrices.
  72. 1545 bits for VP3 matrices.*/
  73. oggpackB_write(_opb,nbase_mats-1,9);
  74. for(bmi=0;bmi<nbase_mats;bmi++){
  75. for(ci=0;ci<64;ci++)oggpackB_write(_opb,base_mats[bmi][0][ci],8);
  76. }
  77. /*Now store quant ranges and their associated indices into the base matrix
  78. list.
  79. 46 bits for VP3 matrices.*/
  80. nbits=OC_ILOG_32(nbase_mats-1);
  81. for(i=0;i<6;i++){
  82. qti=i/3;
  83. pli=i%3;
  84. qranges=_qinfo->qi_ranges[qti]+pli;
  85. if(i>0){
  86. if(qti>0){
  87. if(qranges->nranges==_qinfo->qi_ranges[qti-1][pli].nranges&&
  88. memcmp(qranges->sizes,_qinfo->qi_ranges[qti-1][pli].sizes,
  89. qranges->nranges*sizeof(qranges->sizes[0]))==0&&
  90. memcmp(indices[qti][pli],indices[qti-1][pli],
  91. (qranges->nranges+1)*sizeof(indices[qti][pli][0]))==0){
  92. oggpackB_write(_opb,1,2);
  93. continue;
  94. }
  95. }
  96. qtj=(i-1)/3;
  97. plj=(i-1)%3;
  98. if(qranges->nranges==_qinfo->qi_ranges[qtj][plj].nranges&&
  99. memcmp(qranges->sizes,_qinfo->qi_ranges[qtj][plj].sizes,
  100. qranges->nranges*sizeof(qranges->sizes[0]))==0&&
  101. memcmp(indices[qti][pli],indices[qtj][plj],
  102. (qranges->nranges+1)*sizeof(indices[qti][pli][0]))==0){
  103. oggpackB_write(_opb,0,1+(qti>0));
  104. continue;
  105. }
  106. oggpackB_write(_opb,1,1);
  107. }
  108. oggpackB_write(_opb,indices[qti][pli][0],nbits);
  109. for(qi=qri=0;qi<63;qri++){
  110. oggpackB_write(_opb,qranges->sizes[qri]-1,OC_ILOG_32(62-qi));
  111. qi+=qranges->sizes[qri];
  112. oggpackB_write(_opb,indices[qti][pli][qri+1],nbits);
  113. }
  114. }
  115. }
  116. static void oc_iquant_init(oc_iquant *_this,ogg_uint16_t _d){
  117. ogg_uint32_t t;
  118. int l;
  119. _d<<=1;
  120. l=OC_ILOGNZ_32(_d)-1;
  121. t=1+((ogg_uint32_t)1<<16+l)/_d;
  122. _this->m=(ogg_int16_t)(t-0x10000);
  123. _this->l=l;
  124. }
  125. /*See comments at oc_dequant_tables_init() for how the quantization tables'
  126. storage should be initialized.*/
  127. void oc_enquant_tables_init(ogg_uint16_t *_dequant[64][3][2],
  128. oc_iquant *_enquant[64][3][2],const th_quant_info *_qinfo){
  129. int qi;
  130. int pli;
  131. int qti;
  132. /*Initialize the dequantization tables first.*/
  133. oc_dequant_tables_init(_dequant,NULL,_qinfo);
  134. /*Derive the quantization tables directly from the dequantization tables.*/
  135. for(qi=0;qi<64;qi++)for(qti=0;qti<2;qti++)for(pli=0;pli<3;pli++){
  136. int zzi;
  137. int plj;
  138. int qtj;
  139. int dupe;
  140. dupe=0;
  141. for(qtj=0;qtj<=qti;qtj++){
  142. for(plj=0;plj<(qtj<qti?3:pli);plj++){
  143. if(_dequant[qi][pli][qti]==_dequant[qi][plj][qtj]){
  144. dupe=1;
  145. break;
  146. }
  147. }
  148. if(dupe)break;
  149. }
  150. if(dupe){
  151. _enquant[qi][pli][qti]=_enquant[qi][plj][qtj];
  152. continue;
  153. }
  154. /*In the original VP3.2 code, the rounding offset and the size of the
  155. dead zone around 0 were controlled by a "sharpness" parameter.
  156. We now R-D optimize the tokens for each block after quantization,
  157. so the rounding offset should always be 1/2, and an explicit dead
  158. zone is unnecessary.
  159. Hence, all of that VP3.2 code is gone from here, and the remaining
  160. floating point code has been implemented as equivalent integer
  161. code with exact precision.*/
  162. for(zzi=0;zzi<64;zzi++){
  163. oc_iquant_init(_enquant[qi][pli][qti]+zzi,
  164. _dequant[qi][pli][qti][zzi]);
  165. }
  166. }
  167. }
  168. /*This table gives the square root of the fraction of the squared magnitude of
  169. each DCT coefficient relative to the total, scaled by 2**16, for both INTRA
  170. and INTER modes.
  171. These values were measured after motion-compensated prediction, before
  172. quantization, over a large set of test video (from QCIF to 1080p) encoded at
  173. all possible rates.
  174. The DC coefficient takes into account the DPCM prediction (using the
  175. quantized values from neighboring blocks, as the encoder does, but still
  176. before quantization of the coefficient in the current block).
  177. The results differ significantly from the expected variance (e.g., using an
  178. AR(1) model of the signal with rho=0.95, as is frequently done to compute
  179. the coding gain of the DCT).
  180. We use them to estimate an "average" quantizer for a given quantizer matrix,
  181. as this is used to parameterize a number of the rate control decisions.
  182. These values are themselves probably quantizer-matrix dependent, since the
  183. shape of the matrix affects the noise distribution in the reference frames,
  184. but they should at least give us _some_ amount of adaptivity to different
  185. matrices, as opposed to hard-coding a table of average Q values for the
  186. current set.
  187. The main features they capture are that a) only a few of the quantizers in
  188. the upper-left corner contribute anything significant at all (though INTER
  189. mode is significantly flatter) and b) the DPCM prediction of the DC
  190. coefficient gives a very minor improvement in the INTRA case and a quite
  191. significant one in the INTER case (over the expected variance).*/
  192. static const ogg_uint16_t OC_RPSD[2][64]={
  193. {
  194. 52725,17370,10399, 6867, 5115, 3798, 2942, 2076,
  195. 17370, 9900, 6948, 4994, 3836, 2869, 2229, 1619,
  196. 10399, 6948, 5516, 4202, 3376, 2573, 2015, 1461,
  197. 6867, 4994, 4202, 3377, 2800, 2164, 1718, 1243,
  198. 5115, 3836, 3376, 2800, 2391, 1884, 1530, 1091,
  199. 3798, 2869, 2573, 2164, 1884, 1495, 1212, 873,
  200. 2942, 2229, 2015, 1718, 1530, 1212, 1001, 704,
  201. 2076, 1619, 1461, 1243, 1091, 873, 704, 474
  202. },
  203. {
  204. 23411,15604,13529,11601,10683, 8958, 7840, 6142,
  205. 15604,11901,10718, 9108, 8290, 6961, 6023, 4487,
  206. 13529,10718, 9961, 8527, 7945, 6689, 5742, 4333,
  207. 11601, 9108, 8527, 7414, 7084, 5923, 5175, 3743,
  208. 10683, 8290, 7945, 7084, 6771, 5754, 4793, 3504,
  209. 8958, 6961, 6689, 5923, 5754, 4679, 3936, 2989,
  210. 7840, 6023, 5742, 5175, 4793, 3936, 3522, 2558,
  211. 6142, 4487, 4333, 3743, 3504, 2989, 2558, 1829
  212. }
  213. };
  214. /*The fraction of the squared magnitude of the residuals in each color channel
  215. relative to the total, scaled by 2**16, for each pixel format.
  216. These values were measured after motion-compensated prediction, before
  217. quantization, over a large set of test video encoded at all possible rates.
  218. TODO: These values are only from INTER frames; it should be re-measured for
  219. INTRA frames.*/
  220. static const ogg_uint16_t OC_PCD[4][3]={
  221. {59926, 3038, 2572},
  222. {55201, 5597, 4738},
  223. {55201, 5597, 4738},
  224. {47682, 9669, 8185}
  225. };
  226. /*Compute an "average" quantizer for each qi level.
  227. We do one for INTER and one for INTRA, since their behavior is very
  228. different, but average across chroma channels.
  229. The basic approach is to compute a harmonic average of the squared quantizer,
  230. weighted by the expected squared magnitude of the DCT coefficients.
  231. Under the (not quite true) assumption that DCT coefficients are
  232. Laplacian-distributed, this preserves the product Q*lambda, where
  233. lambda=sqrt(2/sigma**2) is the Laplacian distribution parameter (not to be
  234. confused with the lambda used in R-D optimization throughout most of the
  235. rest of the code).
  236. The value Q*lambda completely determines the entropy of the coefficients.*/
  237. void oc_enquant_qavg_init(ogg_int64_t _log_qavg[2][64],
  238. ogg_uint16_t *_dequant[64][3][2],int _pixel_fmt){
  239. int qi;
  240. int pli;
  241. int qti;
  242. int ci;
  243. for(qti=0;qti<2;qti++)for(qi=0;qi<64;qi++){
  244. ogg_int64_t q2;
  245. q2=0;
  246. for(pli=0;pli<3;pli++){
  247. ogg_uint32_t qp;
  248. qp=0;
  249. for(ci=0;ci<64;ci++){
  250. unsigned rq;
  251. unsigned qd;
  252. qd=_dequant[qi][pli][qti][OC_IZIG_ZAG[ci]];
  253. rq=(OC_RPSD[qti][ci]+(qd>>1))/qd;
  254. qp+=rq*(ogg_uint32_t)rq;
  255. }
  256. q2+=OC_PCD[_pixel_fmt][pli]*(ogg_int64_t)qp;
  257. }
  258. /*qavg=1.0/sqrt(q2).*/
  259. _log_qavg[qti][qi]=OC_Q57(48)-oc_blog64(q2)>>1;
  260. }
  261. }