enquant.c 14 KB

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