enquant.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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$
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "encint.h"
  18. int oc_quant_params_clone(th_quant_info *_dst,const th_quant_info *_src){
  19. int i;
  20. memcpy(_dst,_src,sizeof(*_dst));
  21. memset(_dst->qi_ranges,0,sizeof(_dst->qi_ranges));
  22. for(i=0;i<6;i++){
  23. int nranges;
  24. int qti;
  25. int pli;
  26. int qtj;
  27. int plj;
  28. int pdup;
  29. int qdup;
  30. qti=i/3;
  31. pli=i%3;
  32. qtj=(i-1)/3;
  33. plj=(i-1)%3;
  34. nranges=_src->qi_ranges[qti][pli].nranges;
  35. /*Check for those duplicates that can be cleanly handled by
  36. oc_quant_params_clear().*/
  37. pdup=i>0&&nranges<=_src->qi_ranges[qtj][plj].nranges;
  38. qdup=qti>0&&nranges<=_src->qi_ranges[0][pli].nranges;
  39. _dst->qi_ranges[qti][pli].nranges=nranges;
  40. if(pdup&&_src->qi_ranges[qti][pli].sizes==_src->qi_ranges[qtj][plj].sizes){
  41. _dst->qi_ranges[qti][pli].sizes=_dst->qi_ranges[qtj][plj].sizes;
  42. }
  43. else if(qdup&&_src->qi_ranges[1][pli].sizes==_src->qi_ranges[0][pli].sizes){
  44. _dst->qi_ranges[1][pli].sizes=_dst->qi_ranges[0][pli].sizes;
  45. }
  46. else{
  47. int *sizes;
  48. sizes=(int *)_ogg_malloc(nranges*sizeof(*sizes));
  49. /*Note: The caller is responsible for cleaning up any partially
  50. constructed qinfo.*/
  51. if(sizes==NULL)return TH_EFAULT;
  52. memcpy(sizes,_src->qi_ranges[qti][pli].sizes,nranges*sizeof(*sizes));
  53. _dst->qi_ranges[qti][pli].sizes=sizes;
  54. }
  55. if(pdup&&_src->qi_ranges[qti][pli].base_matrices==
  56. _src->qi_ranges[qtj][plj].base_matrices){
  57. _dst->qi_ranges[qti][pli].base_matrices=
  58. _dst->qi_ranges[qtj][plj].base_matrices;
  59. }
  60. else if(qdup&&_src->qi_ranges[1][pli].base_matrices==
  61. _src->qi_ranges[0][pli].base_matrices){
  62. _dst->qi_ranges[1][pli].base_matrices=
  63. _dst->qi_ranges[0][pli].base_matrices;
  64. }
  65. else{
  66. th_quant_base *base_matrices;
  67. base_matrices=(th_quant_base *)_ogg_malloc(
  68. (nranges+1)*sizeof(*base_matrices));
  69. /*Note: The caller is responsible for cleaning up any partially
  70. constructed qinfo.*/
  71. if(base_matrices==NULL)return TH_EFAULT;
  72. memcpy(base_matrices,_src->qi_ranges[qti][pli].base_matrices,
  73. (nranges+1)*sizeof(*base_matrices));
  74. _dst->qi_ranges[qti][pli].base_matrices=
  75. (const th_quant_base *)base_matrices;
  76. }
  77. }
  78. return 0;
  79. }
  80. void oc_quant_params_pack(oggpack_buffer *_opb,const th_quant_info *_qinfo){
  81. const th_quant_ranges *qranges;
  82. const th_quant_base *base_mats[2*3*64];
  83. int indices[2][3][64];
  84. int nbase_mats;
  85. int nbits;
  86. int ci;
  87. int qi;
  88. int qri;
  89. int qti;
  90. int pli;
  91. int qtj;
  92. int plj;
  93. int bmi;
  94. int i;
  95. i=_qinfo->loop_filter_limits[0];
  96. for(qi=1;qi<64;qi++)i=OC_MAXI(i,_qinfo->loop_filter_limits[qi]);
  97. nbits=OC_ILOG_32(i);
  98. oggpackB_write(_opb,nbits,3);
  99. for(qi=0;qi<64;qi++){
  100. oggpackB_write(_opb,_qinfo->loop_filter_limits[qi],nbits);
  101. }
  102. /*580 bits for VP3.*/
  103. i=1;
  104. for(qi=0;qi<64;qi++)i=OC_MAXI(_qinfo->ac_scale[qi],i);
  105. nbits=OC_ILOGNZ_32(i);
  106. oggpackB_write(_opb,nbits-1,4);
  107. for(qi=0;qi<64;qi++)oggpackB_write(_opb,_qinfo->ac_scale[qi],nbits);
  108. /*516 bits for VP3.*/
  109. i=1;
  110. for(qi=0;qi<64;qi++)i=OC_MAXI(_qinfo->dc_scale[qi],i);
  111. nbits=OC_ILOGNZ_32(i);
  112. oggpackB_write(_opb,nbits-1,4);
  113. for(qi=0;qi<64;qi++)oggpackB_write(_opb,_qinfo->dc_scale[qi],nbits);
  114. /*Consolidate any duplicate base matrices.*/
  115. nbase_mats=0;
  116. for(qti=0;qti<2;qti++)for(pli=0;pli<3;pli++){
  117. qranges=_qinfo->qi_ranges[qti]+pli;
  118. for(qri=0;qri<=qranges->nranges;qri++){
  119. for(bmi=0;;bmi++){
  120. if(bmi>=nbase_mats){
  121. base_mats[bmi]=qranges->base_matrices+qri;
  122. indices[qti][pli][qri]=nbase_mats++;
  123. break;
  124. }
  125. else if(memcmp(base_mats[bmi][0],qranges->base_matrices[qri],
  126. sizeof(base_mats[bmi][0]))==0){
  127. indices[qti][pli][qri]=bmi;
  128. break;
  129. }
  130. }
  131. }
  132. }
  133. /*Write out the list of unique base matrices.
  134. 1545 bits for VP3 matrices.*/
  135. oggpackB_write(_opb,nbase_mats-1,9);
  136. for(bmi=0;bmi<nbase_mats;bmi++){
  137. for(ci=0;ci<64;ci++)oggpackB_write(_opb,base_mats[bmi][0][ci],8);
  138. }
  139. /*Now store quant ranges and their associated indices into the base matrix
  140. list.
  141. 46 bits for VP3 matrices.*/
  142. nbits=OC_ILOG_32(nbase_mats-1);
  143. for(i=0;i<6;i++){
  144. qti=i/3;
  145. pli=i%3;
  146. qranges=_qinfo->qi_ranges[qti]+pli;
  147. if(i>0){
  148. if(qti>0){
  149. if(qranges->nranges==_qinfo->qi_ranges[qti-1][pli].nranges&&
  150. memcmp(qranges->sizes,_qinfo->qi_ranges[qti-1][pli].sizes,
  151. qranges->nranges*sizeof(qranges->sizes[0]))==0&&
  152. memcmp(indices[qti][pli],indices[qti-1][pli],
  153. (qranges->nranges+1)*sizeof(indices[qti][pli][0]))==0){
  154. oggpackB_write(_opb,1,2);
  155. continue;
  156. }
  157. }
  158. qtj=(i-1)/3;
  159. plj=(i-1)%3;
  160. if(qranges->nranges==_qinfo->qi_ranges[qtj][plj].nranges&&
  161. memcmp(qranges->sizes,_qinfo->qi_ranges[qtj][plj].sizes,
  162. qranges->nranges*sizeof(qranges->sizes[0]))==0&&
  163. memcmp(indices[qti][pli],indices[qtj][plj],
  164. (qranges->nranges+1)*sizeof(indices[qti][pli][0]))==0){
  165. oggpackB_write(_opb,0,1+(qti>0));
  166. continue;
  167. }
  168. oggpackB_write(_opb,1,1);
  169. }
  170. oggpackB_write(_opb,indices[qti][pli][0],nbits);
  171. for(qi=qri=0;qi<63;qri++){
  172. oggpackB_write(_opb,qranges->sizes[qri]-1,OC_ILOG_32(62-qi));
  173. qi+=qranges->sizes[qri];
  174. oggpackB_write(_opb,indices[qti][pli][qri+1],nbits);
  175. }
  176. }
  177. }
  178. void oc_iquant_init(oc_iquant *_this,ogg_uint16_t _d){
  179. ogg_uint32_t t;
  180. int l;
  181. _d<<=1;
  182. l=OC_ILOGNZ_32(_d)-1;
  183. t=1+((ogg_uint32_t)1<<16+l)/_d;
  184. _this->m=(ogg_int16_t)(t-0x10000);
  185. _this->l=l;
  186. }
  187. void oc_enc_enquant_table_init_c(void *_enquant,
  188. const ogg_uint16_t _dequant[64]){
  189. oc_iquant *enquant;
  190. int zzi;
  191. /*In the original VP3.2 code, the rounding offset and the size of the
  192. dead zone around 0 were controlled by a "sharpness" parameter.
  193. We now R-D optimize the tokens for each block after quantization,
  194. so the rounding offset should always be 1/2, and an explicit dead
  195. zone is unnecessary.
  196. Hence, all of that VP3.2 code is gone from here, and the remaining
  197. floating point code has been implemented as equivalent integer
  198. code with exact precision.*/
  199. enquant=(oc_iquant *)_enquant;
  200. for(zzi=0;zzi<64;zzi++)oc_iquant_init(enquant+zzi,_dequant[zzi]);
  201. }
  202. void oc_enc_enquant_table_fixup_c(void *_enquant[3][3][2],int _nqis){
  203. int pli;
  204. int qii;
  205. int qti;
  206. for(pli=0;pli<3;pli++)for(qii=1;qii<_nqis;qii++)for(qti=0;qti<2;qti++){
  207. *((oc_iquant *)_enquant[pli][qii][qti])=
  208. *((oc_iquant *)_enquant[pli][0][qti]);
  209. }
  210. }
  211. int oc_enc_quantize_c(ogg_int16_t _qdct[64],const ogg_int16_t _dct[64],
  212. const ogg_uint16_t _dequant[64],const void *_enquant){
  213. const oc_iquant *enquant;
  214. int nonzero;
  215. int zzi;
  216. int val;
  217. int d;
  218. int s;
  219. enquant=(const oc_iquant *)_enquant;
  220. nonzero=0;
  221. for(zzi=0;zzi<64;zzi++){
  222. val=_dct[zzi];
  223. d=_dequant[zzi];
  224. val=val<<1;
  225. if(abs(val)>=d){
  226. s=OC_SIGNMASK(val);
  227. /*The bias added here rounds ties away from zero, since token
  228. optimization can only decrease the magnitude of the quantized
  229. value.*/
  230. val+=d+s^s;
  231. /*Note the arithmetic right shift is not guaranteed by ANSI C.
  232. Hopefully no one still uses ones-complement architectures.*/
  233. val=((enquant[zzi].m*(ogg_int32_t)val>>16)+val>>enquant[zzi].l)-s;
  234. _qdct[zzi]=(ogg_int16_t)val;
  235. nonzero=zzi;
  236. }
  237. else _qdct[zzi]=0;
  238. }
  239. return nonzero;
  240. }
  241. /*This table gives the square root of the fraction of the squared magnitude of
  242. each DCT coefficient relative to the total, scaled by 2**16, for both INTRA
  243. and INTER modes.
  244. These values were measured after motion-compensated prediction, before
  245. quantization, over a large set of test video (from QCIF to 1080p) encoded at
  246. all possible rates.
  247. The DC coefficient takes into account the DPCM prediction (using the
  248. quantized values from neighboring blocks, as the encoder does, but still
  249. before quantization of the coefficient in the current block).
  250. The results differ significantly from the expected variance (e.g., using an
  251. AR(1) model of the signal with rho=0.95, as is frequently done to compute
  252. the coding gain of the DCT).
  253. We use them to estimate an "average" quantizer for a given quantizer matrix,
  254. as this is used to parameterize a number of the rate control decisions.
  255. These values are themselves probably quantizer-matrix dependent, since the
  256. shape of the matrix affects the noise distribution in the reference frames,
  257. but they should at least give us _some_ amount of adaptivity to different
  258. matrices, as opposed to hard-coding a table of average Q values for the
  259. current set.
  260. The main features they capture are that a) only a few of the quantizers in
  261. the upper-left corner contribute anything significant at all (though INTER
  262. mode is significantly flatter) and b) the DPCM prediction of the DC
  263. coefficient gives a very minor improvement in the INTRA case and a quite
  264. significant one in the INTER case (over the expected variance).*/
  265. static const ogg_uint16_t OC_RPSD[2][64]={
  266. {
  267. 52725,17370,10399, 6867, 5115, 3798, 2942, 2076,
  268. 17370, 9900, 6948, 4994, 3836, 2869, 2229, 1619,
  269. 10399, 6948, 5516, 4202, 3376, 2573, 2015, 1461,
  270. 6867, 4994, 4202, 3377, 2800, 2164, 1718, 1243,
  271. 5115, 3836, 3376, 2800, 2391, 1884, 1530, 1091,
  272. 3798, 2869, 2573, 2164, 1884, 1495, 1212, 873,
  273. 2942, 2229, 2015, 1718, 1530, 1212, 1001, 704,
  274. 2076, 1619, 1461, 1243, 1091, 873, 704, 474
  275. },
  276. {
  277. 23411,15604,13529,11601,10683, 8958, 7840, 6142,
  278. 15604,11901,10718, 9108, 8290, 6961, 6023, 4487,
  279. 13529,10718, 9961, 8527, 7945, 6689, 5742, 4333,
  280. 11601, 9108, 8527, 7414, 7084, 5923, 5175, 3743,
  281. 10683, 8290, 7945, 7084, 6771, 5754, 4793, 3504,
  282. 8958, 6961, 6689, 5923, 5754, 4679, 3936, 2989,
  283. 7840, 6023, 5742, 5175, 4793, 3936, 3522, 2558,
  284. 6142, 4487, 4333, 3743, 3504, 2989, 2558, 1829
  285. }
  286. };
  287. /*The fraction of the squared magnitude of the residuals in each color channel
  288. relative to the total, scaled by 2**16, for each pixel format.
  289. These values were measured after motion-compensated prediction, before
  290. quantization, over a large set of test video encoded at all possible rates.
  291. TODO: These values are only from INTER frames; they should be re-measured for
  292. INTRA frames.*/
  293. static const ogg_uint16_t OC_PCD[4][3]={
  294. {59926, 3038, 2572},
  295. {55201, 5597, 4738},
  296. {55201, 5597, 4738},
  297. {47682, 9669, 8185}
  298. };
  299. /*Compute "average" quantizers for each qi level to use for rate control.
  300. We do one for each color channel, as well as an average across color
  301. channels, separately for INTER and INTRA, since their behavior is very
  302. different.
  303. The basic approach is to compute a harmonic average of the squared quantizer,
  304. weighted by the expected squared magnitude of the DCT coefficients.
  305. Under the (not quite true) assumption that DCT coefficients are
  306. Laplacian-distributed, this preserves the product Q*lambda, where
  307. lambda=sqrt(2/sigma**2) is the Laplacian distribution parameter (not to be
  308. confused with the lambda used in R-D optimization throughout most of the
  309. rest of the code), when the distributions from multiple coefficients are
  310. pooled.
  311. The value Q*lambda completely determines the entropy of coefficients drawn
  312. from a Laplacian distribution, and thus the expected bitrate.*/
  313. void oc_enquant_qavg_init(ogg_int64_t _log_qavg[2][64],
  314. ogg_int16_t _log_plq[64][3][2],ogg_uint16_t _chroma_rd_scale[2][64][2],
  315. ogg_uint16_t *_dequant[64][3][2],int _pixel_fmt){
  316. int qi;
  317. int pli;
  318. int qti;
  319. int ci;
  320. for(qti=0;qti<2;qti++)for(qi=0;qi<64;qi++){
  321. ogg_int64_t q2;
  322. ogg_uint32_t qp[3];
  323. ogg_uint32_t cqp;
  324. ogg_uint32_t d;
  325. q2=0;
  326. for(pli=0;pli<3;pli++){
  327. qp[pli]=0;
  328. for(ci=0;ci<64;ci++){
  329. unsigned rq;
  330. unsigned qd;
  331. qd=_dequant[qi][pli][qti][OC_IZIG_ZAG[ci]];
  332. rq=(OC_RPSD[qti][ci]+(qd>>1))/qd;
  333. qp[pli]+=rq*(ogg_uint32_t)rq;
  334. }
  335. q2+=OC_PCD[_pixel_fmt][pli]*(ogg_int64_t)qp[pli];
  336. /*plq=1.0/sqrt(qp)*/
  337. _log_plq[qi][pli][qti]=
  338. (ogg_int16_t)(OC_Q10(32)-oc_blog32_q10(qp[pli])>>1);
  339. }
  340. d=OC_PCD[_pixel_fmt][1]+OC_PCD[_pixel_fmt][2];
  341. cqp=(ogg_uint32_t)((OC_PCD[_pixel_fmt][1]*(ogg_int64_t)qp[1]+
  342. OC_PCD[_pixel_fmt][2]*(ogg_int64_t)qp[2]+(d>>1))/d);
  343. /*chroma_rd_scale=clamp(0.25,cqp/qp[0],4)*/
  344. d=OC_MAXI(qp[0]+(1<<OC_RD_SCALE_BITS-1)>>OC_RD_SCALE_BITS,1);
  345. d=OC_CLAMPI(1<<OC_RD_SCALE_BITS-2,(cqp+(d>>1))/d,4<<OC_RD_SCALE_BITS);
  346. _chroma_rd_scale[qti][qi][0]=(ogg_int16_t)d;
  347. /*chroma_rd_iscale=clamp(0.25,qp[0]/cqp,4)*/
  348. d=OC_MAXI(OC_RD_ISCALE(cqp,1),1);
  349. d=OC_CLAMPI(1<<OC_RD_ISCALE_BITS-2,(qp[0]+(d>>1))/d,4<<OC_RD_ISCALE_BITS);
  350. _chroma_rd_scale[qti][qi][1]=(ogg_int16_t)d;
  351. /*qavg=1.0/sqrt(q2).*/
  352. _log_qavg[qti][qi]=OC_Q57(48)-oc_blog64(q2)>>1;
  353. }
  354. }