collect.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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-2011 *
  9. * by the Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: mode selection code
  13. last mod: $Id$
  14. ********************************************************************/
  15. #include <stdio.h>
  16. #include <limits.h>
  17. #include <math.h>
  18. #include <string.h>
  19. #include "collect.h"
  20. #if defined(OC_COLLECT_METRICS)
  21. int OC_HAS_MODE_METRICS;
  22. double OC_MODE_RD_WEIGHT_SATD[OC_LOGQ_BINS][3][2][OC_COMP_BINS];
  23. double OC_MODE_RD_WEIGHT_SAD[OC_LOGQ_BINS][3][2][OC_COMP_BINS];
  24. oc_mode_metrics OC_MODE_METRICS_SATD[OC_LOGQ_BINS-1][3][2][OC_COMP_BINS];
  25. oc_mode_metrics OC_MODE_METRICS_SAD[OC_LOGQ_BINS-1][3][2][OC_COMP_BINS];
  26. const char *OC_MODE_METRICS_FILENAME="modedec.stats";
  27. void oc_mode_metrics_add(oc_mode_metrics *_metrics,
  28. double _w,int _s,int _q,int _r,double _d){
  29. if(_metrics->w>0){
  30. double ds;
  31. double dq;
  32. double dr;
  33. double dd;
  34. double ds2;
  35. double dq2;
  36. double s2;
  37. double sq;
  38. double q2;
  39. double sr;
  40. double qr;
  41. double sd;
  42. double qd;
  43. double s2q;
  44. double sq2;
  45. double w;
  46. double wa;
  47. double rwa;
  48. double rwa2;
  49. double rwb;
  50. double rwb2;
  51. double rw2;
  52. double rw3;
  53. double rw4;
  54. wa=_metrics->w;
  55. ds=_s-_metrics->s/wa;
  56. dq=_q-_metrics->q/wa;
  57. dr=_r-_metrics->r/wa;
  58. dd=_d-_metrics->d/wa;
  59. ds2=ds*ds;
  60. dq2=dq*dq;
  61. s2=_metrics->s2;
  62. sq=_metrics->sq;
  63. q2=_metrics->q2;
  64. sr=_metrics->sr;
  65. qr=_metrics->qr;
  66. sd=_metrics->sd;
  67. qd=_metrics->qd;
  68. s2q=_metrics->s2q;
  69. sq2=_metrics->sq2;
  70. w=wa+_w;
  71. rwa=wa/w;
  72. rwb=_w/w;
  73. rwa2=rwa*rwa;
  74. rwb2=rwb*rwb;
  75. rw2=wa*rwb;
  76. rw3=rw2*(rwa2-rwb2);
  77. rw4=_w*rwa2*rwa2+wa*rwb2*rwb2;
  78. _metrics->s2q2+=-2*(ds*sq2+dq*s2q)*rwb
  79. +(ds2*q2+4*ds*dq*sq+dq2*s2)*rwb2+ds2*dq2*rw4;
  80. _metrics->s2q+=(-2*ds*sq-dq*s2)*rwb+ds2*dq*rw3;
  81. _metrics->sq2+=(-ds*q2-2*dq*sq)*rwb+ds*dq2*rw3;
  82. _metrics->sqr+=(-ds*qr-dq*sr-dr*sq)*rwb+ds*dq*dr*rw3;
  83. _metrics->sqd+=(-ds*qd-dq*sd-dd*sq)*rwb+ds*dq*dd*rw3;
  84. _metrics->s2+=ds2*rw2;
  85. _metrics->sq+=ds*dq*rw2;
  86. _metrics->q2+=dq2*rw2;
  87. _metrics->sr+=ds*dr*rw2;
  88. _metrics->qr+=dq*dr*rw2;
  89. _metrics->r2+=dr*dr*rw2;
  90. _metrics->sd+=ds*dd*rw2;
  91. _metrics->qd+=dq*dd*rw2;
  92. _metrics->d2+=dd*dd*rw2;
  93. }
  94. _metrics->w+=_w;
  95. _metrics->s+=_s*_w;
  96. _metrics->q+=_q*_w;
  97. _metrics->r+=_r*_w;
  98. _metrics->d+=_d*_w;
  99. }
  100. void oc_mode_metrics_merge(oc_mode_metrics *_dst,
  101. const oc_mode_metrics *_src,int _n){
  102. int i;
  103. /*Find a non-empty set of metrics.*/
  104. for(i=0;i<_n&&_src[i].w==0;i++);
  105. if(i>=_n){
  106. memset(_dst,0,sizeof(*_dst));
  107. return;
  108. }
  109. memcpy(_dst,_src+i,sizeof(*_dst));
  110. /*And iterate over the remaining non-empty sets of metrics.*/
  111. for(i++;i<_n;i++)if(_src[i].w!=0){
  112. double ds;
  113. double dq;
  114. double dr;
  115. double dd;
  116. double ds2;
  117. double dq2;
  118. double s2a;
  119. double s2b;
  120. double sqa;
  121. double sqb;
  122. double q2a;
  123. double q2b;
  124. double sra;
  125. double srb;
  126. double qra;
  127. double qrb;
  128. double sda;
  129. double sdb;
  130. double qda;
  131. double qdb;
  132. double s2qa;
  133. double s2qb;
  134. double sq2a;
  135. double sq2b;
  136. double w;
  137. double wa;
  138. double wb;
  139. double rwa;
  140. double rwb;
  141. double rwa2;
  142. double rwb2;
  143. double rw2;
  144. double rw3;
  145. double rw4;
  146. wa=_dst->w;
  147. wb=_src[i].w;
  148. ds=_src[i].s/wb-_dst->s/wa;
  149. dq=_src[i].q/wb-_dst->q/wa;
  150. dr=_src[i].r/wb-_dst->r/wa;
  151. dd=_src[i].d/wb-_dst->d/wa;
  152. ds2=ds*ds;
  153. dq2=dq*dq;
  154. s2a=_dst->s2;
  155. sqa=_dst->sq;
  156. q2a=_dst->q2;
  157. sra=_dst->sr;
  158. qra=_dst->qr;
  159. sda=_dst->sd;
  160. qda=_dst->qd;
  161. s2qa=_dst->s2q;
  162. sq2a=_dst->sq2;
  163. s2b=_src[i].s2;
  164. sqb=_src[i].sq;
  165. q2b=_src[i].q2;
  166. srb=_src[i].sr;
  167. qrb=_src[i].qr;
  168. sdb=_src[i].sd;
  169. qdb=_src[i].qd;
  170. s2qb=_src[i].s2q;
  171. sq2b=_src[i].sq2;
  172. w=wa+wb;
  173. if(w==0)rwa=rwb=0;
  174. else{
  175. rwa=wa/w;
  176. rwb=wb/w;
  177. }
  178. rwa2=rwa*rwa;
  179. rwb2=rwb*rwb;
  180. rw2=wa*rwb;
  181. rw3=rw2*(rwa2-rwb2);
  182. rw4=wb*rwa2*rwa2+wa*rwb2*rwb2;
  183. /*
  184. (1,1,1) ->
  185. (0,0,0)#
  186. (1,0,0) C(1,1)*C(1,0)*C(1,0)-> d^{1,0,0}*(rwa*B_{0,1,1}-rwb*A_{0,1,1})
  187. (0,1,0) C(1,0)*C(1,1)*C(1,0)-> d^{0,1,0}*(rwa*B_{1,0,1}-rwb*A_{1,0,1})
  188. (0,0,1) C(1,0)*C(1,0)*C(1,1)-> d^{0,0,1}*(rwa*B_{1,1,0}-rwb*A_{1,1,0})
  189. (1,1,0)*
  190. (1,0,1)*
  191. (0,1,1)*
  192. (1,1,1) C(1,1)*C(1,1)*C(1,1)-> d^{1,1,1}*(rwa^3*wb-rwb^3*wa)
  193. (2,1) ->
  194. (0,0)#
  195. (1,0) C(2,1)*C(1,1)->2*d^{1,0}*(rwa*B_{1,1}-rwb*A_{1,1})
  196. (0,1) C(2,0)*C(1,1)-> d^{0,1}*(rwa*B_{2,0}-rwb*A_{2,0})
  197. (2,0)*
  198. (1,1)*
  199. (2,1) C(2,2)*C(1,1)-> d^{2,1}*(rwa^3*wb-rwb^3*wa)
  200. (2,2) ->
  201. (0,0)#
  202. (1,0) C(2,1)*C(2,0)->2*d^{1,0}*(rwa*B_{1,2}-rwb*A_{1,2})
  203. (0,1) C(2,0)*C(2,1)->2*d^{0,1}*(rwa*B_{2,1}-rwb*A_{2,1})
  204. (2,0) C(2,2)*C(2,0)-> d^{2,0}*(rwa^2*B_{0,2}+rwb^2*A_{0,2})
  205. (1,1) C(2,1)*C(2,1)->4*d^{1,1}*(rwa^2*B_{1,1}+rwb^2*A_{1,1})
  206. (0,2) C(2,0)*C(2,2)-> d^{0,2}*(rwa^2*B_{2,0}+rwb^2*A_{2,0})
  207. (1,2)*
  208. (2,1)*
  209. (2,2) C(2,2)*C(2,2)*d^{2,2}*(rwa^4*wb+rwb^4*wa)
  210. */
  211. _dst->s2q2+=_src[i].s2q2+2*(ds*(rwa*sq2b-rwb*sq2a)+dq*(rwa*s2qb-rwb*s2qa))
  212. +ds2*(rwa2*q2b+rwb2*q2a)+4*ds*dq*(rwa2*sqb+rwb2*sqa)
  213. +dq2*(rwa2*s2b+rwb2*s2a)+ds2*dq2*rw4;
  214. _dst->s2q+=_src[i].s2q+2*ds*(rwa*sqb-rwb*sqa)
  215. +dq*(rwa*s2b-rwb*s2a)+ds2*dq*rw3;
  216. _dst->sq2+=_src[i].sq2+ds*(rwa*q2b-rwb*q2a)
  217. +2*dq*(rwa*sqb-rwb*sqa)+ds*dq2*rw3;
  218. _dst->sqr+=_src[i].sqr+ds*(rwa*qrb-rwb*qra)+dq*(rwa*srb-rwb*sra)
  219. +dr*(rwa*sqb-rwb*sqa)+ds*dq*dr*rw3;
  220. _dst->sqd+=_src[i].sqd+ds*(rwa*qdb-rwb*qda)+dq*(rwa*sdb-rwb*sda)
  221. +dd*(rwa*sqb-rwb*sqa)+ds*dq*dd*rw3;
  222. _dst->s2+=_src[i].s2+ds2*rw2;
  223. _dst->sq+=_src[i].sq+ds*dq*rw2;
  224. _dst->q2+=_src[i].q2+dq2*rw2;
  225. _dst->sr+=_src[i].sr+ds*dr*rw2;
  226. _dst->qr+=_src[i].qr+dq*dr*rw2;
  227. _dst->r2+=_src[i].r2+dr*dr*rw2;
  228. _dst->sd+=_src[i].sd+ds*dd*rw2;
  229. _dst->qd+=_src[i].qd+dq*dd*rw2;
  230. _dst->d2+=_src[i].d2+dd*dd*rw2;
  231. _dst->w+=_src[i].w;
  232. _dst->s+=_src[i].s;
  233. _dst->q+=_src[i].q;
  234. _dst->r+=_src[i].r;
  235. _dst->d+=_src[i].d;
  236. }
  237. }
  238. /*Adjust a single corner of a set of metric bins to minimize the squared
  239. prediction error of R and D.
  240. Each bin is assumed to cover a quad like so:
  241. (s0,q0) (s1,q0)
  242. A----------B
  243. | |
  244. | |
  245. | |
  246. | |
  247. C----------Z
  248. (s0,q1) (s1,q1)
  249. The values A, B, and C are fixed, and Z is the free parameter.
  250. Then, for example, R_i is predicted via bilinear interpolation as
  251. x_i=(s_i-s0)/(s1-s0)
  252. y_i=(q_i-q0)/(q1-q0)
  253. dRds1_i=A+(B-A)*x_i
  254. dRds2_i=C+(Z-C)*x_i
  255. R_i=dRds1_i+(dRds2_i-dRds1_i)*y_i
  256. To find the Z that minimizes the squared prediction error over i, this can
  257. be rewritten as
  258. R_i-(A+(B-A)*x_i+(C-A)*y_i+(A-B-C)*x_i*y_i)=x_i*y_i*Z
  259. Letting X={...,x_i*y_i,...}^T and
  260. Y={...,R_i-(A+(B-A)*x_i+(C-A)*y_i+(A-B-C)*x_i*y_i),...}^T,
  261. the optimal Z is given by Z=(X^T.Y)/(X^T.X).
  262. Now, we need to compute these dot products without actually storing data for
  263. each sample.
  264. Starting with X^T.X, we have
  265. X^T.X = sum(x_i^2*y_i^2) = sum((s_i-s0)^2*(q_i-q0)^2)/((s1-s0)^2*(q1-q0)^2).
  266. Expanding the interior of the sum in a monomial basis of s_i and q_i gives
  267. s0^2*q0^2 *(1)
  268. -2*s0*q0^2*(s_i)
  269. -2*s0^2*q0*(q_i)
  270. +q0^2 *(s_i^2)
  271. +4*s0*q0 *(s_i*q_i)
  272. +s0^2 *(q_i^2)
  273. -2*q0 *(s_i^2*q_i)
  274. -2*s0 *(s_i*q_i^2)
  275. +1 *(s_i^2*q_i^2).
  276. However, computing things directly in this basis leads to gross numerical
  277. errors, as most of the terms will have similar size and destructive
  278. cancellation results.
  279. A much better basis is the central (co-)moment basis:
  280. {1,s_i-sbar,q_i-qbar,(s_i-sbar)^2,(s_i-sbar)*(q_i-qbar),(q_i-qbar)^2,
  281. (s_i-sbar)^2*(q_i-qbar),(s_i-sbar)*(q_i-qbar)^2,(s_i-sbar)^2*(q_i-qbar)^2},
  282. where sbar and qbar are the average s and q values over the bin,
  283. respectively.
  284. In that basis, letting ds=sbar-s0 and dq=qbar-q0, (s_i-s0)^2*(q_i-q0)^2 is
  285. ds^2*dq^2*(1)
  286. +dq^2 *((s_i-sbar)^2)
  287. +4*ds*dq*((s_i-sbar)*(q_i-qbar))
  288. +ds^2 *((q_i-qbar)^2)
  289. +2*dq *((s_i-sbar)^2*(q_i-qbar))
  290. +2*ds *((s_i-sbar)*(q_i-qbar)^2)
  291. +1 *((s_i-sbar)^2*(q_i-qbar)^2).
  292. With these expressions in the central (co-)moment bases, all we need to do
  293. is compute sums over the (co-)moment terms, which can be done
  294. incrementally (see oc_mode_metrics_add() and oc_mode_metrics_merge()),
  295. with no need to store the individual samples.
  296. Now, for X^T.Y, we have
  297. X^T.Y = sum((R_i-A-((B-A)/(s1-s0))*(s_i-s0)-((C-A)/(q1-q0))*(q_i-q0)
  298. -((A-B-C)/((s1-s0)*(q1-q0)))*(s_i-s0)*(q_i-q0))*(s_i-s0)*(q_i-q0))/
  299. ((s1-s0)*(q1-q0)),
  300. or, rewriting the constants to simplify notation,
  301. X^T.Y = sum((C0+C1*(s_i-s0)+C2*(q_i-q0)
  302. +C3*(s_i-s0)*(q_i-q0)+R_i)*(s_i-s0)*(q_i-q0))/((s1-s0)*(q1-q0)).
  303. Again, converting to the central (co-)moment basis, the interior of the
  304. above sum is
  305. ds*dq*(rbar+C0+C1*ds+C2*dq+C3*ds*dq) *(1)
  306. +(C1*dq+C3*dq^2) *((s_i-sbar)^2)
  307. +(rbar+C0+2*C1*ds+2*C2*dq+4*C3*ds*dq)*((s_i-sbar)*(q_i-qbar))
  308. +(C2*ds+C3*ds^2) *((q_i-qbar)^2)
  309. +dq *((s_i-sbar)*(r_i-rbar))
  310. +ds *((q_i-qbar)*(r_i-rbar))
  311. +(C1+2*C3*dq) *((s_i-sbar)^2*(q_i-qbar))
  312. +(C2+2*C3*ds) *((s_i-sbar)*(q_i-qbar)^2)
  313. +1 *((s_i-sbar)*(q_i-qbar)*(r_i-rbar))
  314. +C3 *((s_i-sbar)^2*(q_i-qbar)^2).
  315. You might think it would be easier (if perhaps slightly less robust) to
  316. accumulate terms directly around s0 and q0.
  317. However, we update each corner of the bins in turn, so we would have to
  318. change basis to move the sums from corner to corner anyway.*/
  319. double oc_mode_metrics_solve(double *_r,double *_d,
  320. const oc_mode_metrics *_metrics,const int *_s0,const int *_s1,
  321. const int *_q0,const int *_q1,
  322. const double *_ra,const double *_rb,const double *_rc,
  323. const double *_da,const double *_db,const double *_dc,int _n){
  324. double xx;
  325. double rxy;
  326. double dxy;
  327. double wt;
  328. int i;
  329. xx=rxy=dxy=wt=0;
  330. for(i=0;i<_n;i++)if(_metrics[i].w>0){
  331. double s10;
  332. double q10;
  333. double sq10;
  334. double ds;
  335. double dq;
  336. double ds2;
  337. double dq2;
  338. double r;
  339. double d;
  340. double s2;
  341. double sq;
  342. double q2;
  343. double sr;
  344. double qr;
  345. double sd;
  346. double qd;
  347. double s2q;
  348. double sq2;
  349. double sqr;
  350. double sqd;
  351. double s2q2;
  352. double c0;
  353. double c1;
  354. double c2;
  355. double c3;
  356. double w;
  357. w=_metrics[i].w;
  358. wt+=w;
  359. s10=_s1[i]-_s0[i];
  360. q10=_q1[i]-_q0[i];
  361. sq10=s10*q10;
  362. ds=_metrics[i].s/w-_s0[i];
  363. dq=_metrics[i].q/w-_q0[i];
  364. ds2=ds*ds;
  365. dq2=dq*dq;
  366. s2=_metrics[i].s2;
  367. sq=_metrics[i].sq;
  368. q2=_metrics[i].q2;
  369. s2q=_metrics[i].s2q;
  370. sq2=_metrics[i].sq2;
  371. s2q2=_metrics[i].s2q2;
  372. xx+=(dq2*(ds2*w+s2)+4*ds*dq*sq+ds2*q2+2*(dq*s2q+ds*sq2)+s2q2)/(sq10*sq10);
  373. r=_metrics[i].r/w;
  374. sr=_metrics[i].sr;
  375. qr=_metrics[i].qr;
  376. sqr=_metrics[i].sqr;
  377. c0=-_ra[i];
  378. c1=-(_rb[i]-_ra[i])/s10;
  379. c2=-(_rc[i]-_ra[i])/q10;
  380. c3=-(_ra[i]-_rb[i]-_rc[i])/sq10;
  381. rxy+=(ds*dq*(r+c0+c1*ds+c2*dq+c3*ds*dq)*w+(c1*dq+c3*dq2)*s2
  382. +(r+c0+2*(c1*ds+(c2+2*c3*ds)*dq))*sq+(c2*ds+c3*ds2)*q2+dq*sr+ds*qr
  383. +(c1+2*c3*dq)*s2q+(c2+2*c3*ds)*sq2+sqr+c3*s2q2)/sq10;
  384. d=_metrics[i].d/w;
  385. sd=_metrics[i].sd;
  386. qd=_metrics[i].qd;
  387. sqd=_metrics[i].sqd;
  388. c0=-_da[i];
  389. c1=-(_db[i]-_da[i])/s10;
  390. c2=-(_dc[i]-_da[i])/q10;
  391. c3=-(_da[i]-_db[i]-_dc[i])/sq10;
  392. dxy+=(ds*dq*(d+c0+c1*ds+c2*dq+c3*ds*dq)*w+(c1*dq+c3*dq2)*s2
  393. +(d+c0+2*(c1*ds+(c2+2*c3*ds)*dq))*sq+(c2*ds+c3*ds2)*q2+dq*sd+ds*qd
  394. +(c1+2*c3*dq)*s2q+(c2+2*c3*ds)*sq2+sqd+c3*s2q2)/sq10;
  395. }
  396. if(xx>1E-3){
  397. *_r=rxy/xx;
  398. *_d=dxy/xx;
  399. }
  400. else{
  401. *_r=0;
  402. *_d=0;
  403. }
  404. return wt;
  405. }
  406. /*Compile collected SATD/logq/rate/RMSE metrics into a form that's immediately
  407. useful for mode decision.*/
  408. void oc_mode_metrics_update(oc_mode_metrics (*_metrics)[3][2][OC_COMP_BINS],
  409. int _niters_min,int _reweight,oc_mode_rd (*_table)[3][2][OC_COMP_BINS],
  410. int _shift,double (*_weight)[3][2][OC_COMP_BINS]){
  411. int niters;
  412. int prevdr;
  413. int prevdd;
  414. int dr;
  415. int dd;
  416. int pli;
  417. int qti;
  418. int qi;
  419. int si;
  420. dd=dr=INT_MAX;
  421. niters=0;
  422. /*The encoder interpolates rate and RMSE terms bilinearly from an
  423. OC_LOGQ_BINS by OC_COMP_BINS grid of sample points in _table.
  424. To find the sample values at the grid points that minimize the total
  425. squared prediction error actually requires solving a relatively sparse
  426. linear system with a number of variables equal to the number of grid
  427. points.
  428. Instead of writing a general sparse linear system solver, we just use
  429. Gauss-Seidel iteration, i.e., we update one grid point at time until
  430. they stop changing.*/
  431. do{
  432. prevdr=dr;
  433. prevdd=dd;
  434. dd=dr=0;
  435. for(pli=0;pli<3;pli++){
  436. for(qti=0;qti<2;qti++){
  437. for(qi=0;qi<OC_LOGQ_BINS;qi++){
  438. for(si=0;si<OC_COMP_BINS;si++){
  439. oc_mode_metrics m[4];
  440. int s0[4];
  441. int s1[4];
  442. int q0[4];
  443. int q1[4];
  444. double ra[4];
  445. double rb[4];
  446. double rc[4];
  447. double da[4];
  448. double db[4];
  449. double dc[4];
  450. double r;
  451. double d;
  452. int rate;
  453. int rmse;
  454. int ds;
  455. int n;
  456. n=0;
  457. /*Collect the statistics for the (up to) four bins grid point
  458. (si,qi) touches.*/
  459. if(qi>0&&si>0){
  460. q0[n]=OC_MODE_LOGQ[qi-1][pli][qti];
  461. q1[n]=OC_MODE_LOGQ[qi][pli][qti];
  462. s0[n]=si-1<<_shift;
  463. s1[n]=si<<_shift;
  464. ra[n]=ldexp(_table[qi-1][pli][qti][si-1].rate,-OC_BIT_SCALE);
  465. da[n]=ldexp(_table[qi-1][pli][qti][si-1].rmse,-OC_RMSE_SCALE);
  466. rb[n]=ldexp(_table[qi-1][pli][qti][si].rate,-OC_BIT_SCALE);
  467. db[n]=ldexp(_table[qi-1][pli][qti][si].rmse,-OC_RMSE_SCALE);
  468. rc[n]=ldexp(_table[qi][pli][qti][si-1].rate,-OC_BIT_SCALE);
  469. dc[n]=ldexp(_table[qi][pli][qti][si-1].rmse,-OC_RMSE_SCALE);
  470. *(m+n++)=*(_metrics[qi-1][pli][qti]+si-1);
  471. }
  472. if(qi>0){
  473. ds=si+1<OC_COMP_BINS?1:-1;
  474. q0[n]=OC_MODE_LOGQ[qi-1][pli][qti];
  475. q1[n]=OC_MODE_LOGQ[qi][pli][qti];
  476. s0[n]=si+ds<<_shift;
  477. s1[n]=si<<_shift;
  478. ra[n]=ldexp(_table[qi-1][pli][qti][si+ds].rate,-OC_BIT_SCALE);
  479. da[n]=
  480. ldexp(_table[qi-1][pli][qti][si+ds].rmse,-OC_RMSE_SCALE);
  481. rb[n]=ldexp(_table[qi-1][pli][qti][si].rate,-OC_BIT_SCALE);
  482. db[n]=ldexp(_table[qi-1][pli][qti][si].rmse,-OC_RMSE_SCALE);
  483. rc[n]=ldexp(_table[qi][pli][qti][si+ds].rate,-OC_BIT_SCALE);
  484. dc[n]=ldexp(_table[qi][pli][qti][si+ds].rmse,-OC_RMSE_SCALE);
  485. *(m+n++)=*(_metrics[qi-1][pli][qti]+si);
  486. }
  487. if(qi+1<OC_LOGQ_BINS&&si>0){
  488. q0[n]=OC_MODE_LOGQ[qi+1][pli][qti];
  489. q1[n]=OC_MODE_LOGQ[qi][pli][qti];
  490. s0[n]=si-1<<_shift;
  491. s1[n]=si<<_shift;
  492. ra[n]=ldexp(_table[qi+1][pli][qti][si-1].rate,-OC_BIT_SCALE);
  493. da[n]=ldexp(_table[qi+1][pli][qti][si-1].rmse,-OC_RMSE_SCALE);
  494. rb[n]=ldexp(_table[qi+1][pli][qti][si].rate,-OC_BIT_SCALE);
  495. db[n]=ldexp(_table[qi+1][pli][qti][si].rmse,-OC_RMSE_SCALE);
  496. rc[n]=ldexp(_table[qi][pli][qti][si-1].rate,-OC_BIT_SCALE);
  497. dc[n]=ldexp(_table[qi][pli][qti][si-1].rmse,-OC_RMSE_SCALE);
  498. *(m+n++)=*(_metrics[qi][pli][qti]+si-1);
  499. }
  500. if(qi+1<OC_LOGQ_BINS){
  501. ds=si+1<OC_COMP_BINS?1:-1;
  502. q0[n]=OC_MODE_LOGQ[qi+1][pli][qti];
  503. q1[n]=OC_MODE_LOGQ[qi][pli][qti];
  504. s0[n]=si+ds<<_shift;
  505. s1[n]=si<<_shift;
  506. ra[n]=ldexp(_table[qi+1][pli][qti][si+ds].rate,-OC_BIT_SCALE);
  507. da[n]=
  508. ldexp(_table[qi+1][pli][qti][si+ds].rmse,-OC_RMSE_SCALE);
  509. rb[n]=ldexp(_table[qi+1][pli][qti][si].rate,-OC_BIT_SCALE);
  510. db[n]=ldexp(_table[qi+1][pli][qti][si].rmse,-OC_RMSE_SCALE);
  511. rc[n]=ldexp(_table[qi][pli][qti][si+ds].rate,-OC_BIT_SCALE);
  512. dc[n]=ldexp(_table[qi][pli][qti][si+ds].rmse,-OC_RMSE_SCALE);
  513. *(m+n++)=*(_metrics[qi][pli][qti]+si);
  514. }
  515. /*On the first pass, initialize with a simple weighted average of
  516. the neighboring bins.*/
  517. if(!OC_HAS_MODE_METRICS&&niters==0){
  518. double w;
  519. w=r=d=0;
  520. while(n-->0){
  521. w+=m[n].w;
  522. r+=m[n].r;
  523. d+=m[n].d;
  524. }
  525. r=w>1E-3?r/w:0;
  526. d=w>1E-3?d/w:0;
  527. _weight[qi][pli][qti][si]=w;
  528. }
  529. else{
  530. /*Update the grid point and save the weight for later.*/
  531. _weight[qi][pli][qti][si]=
  532. oc_mode_metrics_solve(&r,&d,m,s0,s1,q0,q1,ra,rb,rc,da,db,dc,n);
  533. }
  534. rate=OC_CLAMPI(-32768,(int)(ldexp(r,OC_BIT_SCALE)+0.5),32767);
  535. rmse=OC_CLAMPI(-32768,(int)(ldexp(d,OC_RMSE_SCALE)+0.5),32767);
  536. dr+=abs(rate-_table[qi][pli][qti][si].rate);
  537. dd+=abs(rmse-_table[qi][pli][qti][si].rmse);
  538. _table[qi][pli][qti][si].rate=(ogg_int16_t)rate;
  539. _table[qi][pli][qti][si].rmse=(ogg_int16_t)rmse;
  540. }
  541. }
  542. }
  543. }
  544. }
  545. /*After a fixed number of initial iterations, only iterate so long as the
  546. total change is decreasing.
  547. This ensures we don't oscillate forever, which is a danger, as all of our
  548. results are rounded fairly coarsely.*/
  549. while((dr>0||dd>0)&&(niters++<_niters_min||(dr<prevdr&&dd<prevdd)));
  550. if(_reweight){
  551. /*Now, reduce the values of the optimal solution until we get enough
  552. samples in each bin to overcome the constant OC_ZWEIGHT factor.
  553. This encourages sampling under-populated bins and prevents a single large
  554. sample early on from discouraging coding in that bin ever again.*/
  555. for(pli=0;pli<3;pli++){
  556. for(qti=0;qti<2;qti++){
  557. for(qi=0;qi<OC_LOGQ_BINS;qi++){
  558. for(si=0;si<OC_COMP_BINS;si++){
  559. double wt;
  560. wt=_weight[qi][pli][qti][si];
  561. wt/=OC_ZWEIGHT+wt;
  562. _table[qi][pli][qti][si].rate=(ogg_int16_t)
  563. (_table[qi][pli][qti][si].rate*wt+0.5);
  564. _table[qi][pli][qti][si].rmse=(ogg_int16_t)
  565. (_table[qi][pli][qti][si].rmse*wt+0.5);
  566. }
  567. }
  568. }
  569. }
  570. }
  571. }
  572. /*Dump the in memory mode metrics to a file.
  573. Note this data format isn't portable between different platforms.*/
  574. void oc_mode_metrics_dump(void){
  575. FILE *fmetrics;
  576. fmetrics=fopen(OC_MODE_METRICS_FILENAME,"wb");
  577. if(fmetrics!=NULL){
  578. (void)fwrite(OC_MODE_LOGQ,sizeof(OC_MODE_LOGQ),1,fmetrics);
  579. (void)fwrite(OC_MODE_METRICS_SATD,sizeof(OC_MODE_METRICS_SATD),1,fmetrics);
  580. (void)fwrite(OC_MODE_METRICS_SAD,sizeof(OC_MODE_METRICS_SAD),1,fmetrics);
  581. fclose(fmetrics);
  582. }
  583. }
  584. void oc_mode_metrics_print_rd(FILE *_fout,const char *_table_name,
  585. #if !defined(OC_COLLECT_METRICS)
  586. const oc_mode_rd (*_mode_rd_table)[3][2][OC_COMP_BINS]){
  587. #else
  588. oc_mode_rd (*_mode_rd_table)[3][2][OC_COMP_BINS]){
  589. #endif
  590. int qii;
  591. fprintf(_fout,
  592. "# if !defined(OC_COLLECT_METRICS)\n"
  593. "static const\n"
  594. "# endif\n"
  595. "oc_mode_rd %s[OC_LOGQ_BINS][3][2][OC_COMP_BINS]={\n",_table_name);
  596. for(qii=0;qii<OC_LOGQ_BINS;qii++){
  597. int pli;
  598. fprintf(_fout," {\n");
  599. for(pli=0;pli<3;pli++){
  600. int qti;
  601. fprintf(_fout," {\n");
  602. for(qti=0;qti<2;qti++){
  603. int bin;
  604. int qi;
  605. static const char *pl_names[3]={"Y'","Cb","Cr"};
  606. static const char *qti_names[2]={"INTRA","INTER"};
  607. qi=(63*qii+(OC_LOGQ_BINS-1>>1))/(OC_LOGQ_BINS-1);
  608. fprintf(_fout," /*%s qi=%i %s*/\n",
  609. pl_names[pli],qi,qti_names[qti]);
  610. fprintf(_fout," {\n");
  611. fprintf(_fout," ");
  612. for(bin=0;bin<OC_COMP_BINS;bin++){
  613. if(bin&&!(bin&0x3))fprintf(_fout,"\n ");
  614. fprintf(_fout,"{%5i,%5i}",
  615. _mode_rd_table[qii][pli][qti][bin].rate,
  616. _mode_rd_table[qii][pli][qti][bin].rmse);
  617. if(bin+1<OC_COMP_BINS)fprintf(_fout,",");
  618. }
  619. fprintf(_fout,"\n }");
  620. if(qti<1)fprintf(_fout,",");
  621. fprintf(_fout,"\n");
  622. }
  623. fprintf(_fout," }");
  624. if(pli<2)fprintf(_fout,",");
  625. fprintf(_fout,"\n");
  626. }
  627. fprintf(_fout," }");
  628. if(qii+1<OC_LOGQ_BINS)fprintf(_fout,",");
  629. fprintf(_fout,"\n");
  630. }
  631. fprintf(_fout,
  632. "};\n"
  633. "\n");
  634. }
  635. void oc_mode_metrics_print(FILE *_fout){
  636. int qii;
  637. fprintf(_fout,
  638. "/*File generated by libtheora with OC_COLLECT_METRICS"
  639. " defined at compile time.*/\n"
  640. "#if !defined(_modedec_H)\n"
  641. "# define _modedec_H (1)\n"
  642. "# include \"encint.h\"\n"
  643. "\n"
  644. "\n"
  645. "\n"
  646. "/*The log of the average quantizer for each of the OC_MODE_RD table rows\n"
  647. " (e.g., for the represented qi's, and each pli and qti), in Q10 format.\n"
  648. " The actual statistics used by the encoder will be interpolated from\n"
  649. " that table based on log_plq for the actual quantization matrix used.*/\n"
  650. "# if !defined(OC_COLLECT_METRICS)\n"
  651. "static const\n"
  652. "# endif\n"
  653. "ogg_int16_t OC_MODE_LOGQ[OC_LOGQ_BINS][3][2]={\n");
  654. for(qii=0;qii<OC_LOGQ_BINS;qii++){
  655. fprintf(_fout," { {0x%04X,0x%04X},{0x%04X,0x%04X},{0x%04X,0x%04X} }%s\n",
  656. OC_MODE_LOGQ[qii][0][0],OC_MODE_LOGQ[qii][0][1],OC_MODE_LOGQ[qii][1][0],
  657. OC_MODE_LOGQ[qii][1][1],OC_MODE_LOGQ[qii][2][0],OC_MODE_LOGQ[qii][2][1],
  658. qii+1<OC_LOGQ_BINS?",":"");
  659. }
  660. fprintf(_fout,
  661. "};\n"
  662. "\n");
  663. oc_mode_metrics_print_rd(_fout,"OC_MODE_RD_SATD",OC_MODE_RD_SATD);
  664. oc_mode_metrics_print_rd(_fout,"OC_MODE_RD_SAD",OC_MODE_RD_SAD);
  665. fprintf(_fout,
  666. "#endif\n");
  667. }
  668. # if !defined(OC_COLLECT_NO_ENC_FUNCS)
  669. void oc_enc_mode_metrics_load(oc_enc_ctx *_enc){
  670. oc_restore_fpu(&_enc->state);
  671. /*Load any existing mode metrics if we haven't already.*/
  672. if(!OC_HAS_MODE_METRICS){
  673. FILE *fmetrics;
  674. memset(OC_MODE_METRICS_SATD,0,sizeof(OC_MODE_METRICS_SATD));
  675. memset(OC_MODE_METRICS_SAD,0,sizeof(OC_MODE_METRICS_SAD));
  676. fmetrics=fopen(OC_MODE_METRICS_FILENAME,"rb");
  677. if(fmetrics!=NULL){
  678. /*Read in the binary structures as written my oc_mode_metrics_dump().
  679. Note this format isn't portable between different platforms.*/
  680. (void)fread(OC_MODE_LOGQ,sizeof(OC_MODE_LOGQ),1,fmetrics);
  681. (void)fread(OC_MODE_METRICS_SATD,sizeof(OC_MODE_METRICS_SATD),1,fmetrics);
  682. (void)fread(OC_MODE_METRICS_SAD,sizeof(OC_MODE_METRICS_SAD),1,fmetrics);
  683. fclose(fmetrics);
  684. }
  685. else{
  686. int qii;
  687. int qi;
  688. int pli;
  689. int qti;
  690. for(qii=0;qii<OC_LOGQ_BINS;qii++){
  691. qi=(63*qii+(OC_LOGQ_BINS-1>>1))/(OC_LOGQ_BINS-1);
  692. for(pli=0;pli<3;pli++)for(qti=0;qti<2;qti++){
  693. OC_MODE_LOGQ[qii][pli][qti]=_enc->log_plq[qi][pli][qti];
  694. }
  695. }
  696. }
  697. oc_mode_metrics_update(OC_MODE_METRICS_SATD,100,1,
  698. OC_MODE_RD_SATD,OC_SATD_SHIFT,OC_MODE_RD_WEIGHT_SATD);
  699. oc_mode_metrics_update(OC_MODE_METRICS_SAD,100,1,
  700. OC_MODE_RD_SAD,OC_SAD_SHIFT,OC_MODE_RD_WEIGHT_SAD);
  701. OC_HAS_MODE_METRICS=1;
  702. }
  703. }
  704. /*The following token skipping code used to also be used in the decoder (and
  705. even at one point other places in the encoder).
  706. However, it was obsoleted by other optimizations, and is now only used here.
  707. It has been moved here to avoid generating the code when it's not needed.*/
  708. /*Determines the number of blocks or coefficients to be skipped for a given
  709. token value.
  710. _token: The token value to skip.
  711. _extra_bits: The extra bits attached to this token.
  712. Return: A positive value indicates that number of coefficients are to be
  713. skipped in the current block.
  714. Otherwise, the negative of the return value indicates that number of
  715. blocks are to be ended.*/
  716. typedef ptrdiff_t (*oc_token_skip_func)(int _token,int _extra_bits);
  717. /*Handles the simple end of block tokens.*/
  718. static ptrdiff_t oc_token_skip_eob(int _token,int _extra_bits){
  719. int nblocks_adjust;
  720. nblocks_adjust=OC_UNIBBLE_TABLE32(0,1,2,3,7,15,0,0,_token)+1;
  721. return -_extra_bits-nblocks_adjust;
  722. }
  723. /*The last EOB token has a special case, where an EOB run of size zero ends all
  724. the remaining blocks in the frame.*/
  725. static ptrdiff_t oc_token_skip_eob6(int _token,int _extra_bits){
  726. /*Note: We want to return -PTRDIFF_MAX, but that requires C99, which is not
  727. yet available everywhere; this should be equivalent.*/
  728. if(!_extra_bits)return -(~(size_t)0>>1);
  729. return -_extra_bits;
  730. }
  731. /*Handles the pure zero run tokens.*/
  732. static ptrdiff_t oc_token_skip_zrl(int _token,int _extra_bits){
  733. return _extra_bits+1;
  734. }
  735. /*Handles a normal coefficient value token.*/
  736. static ptrdiff_t oc_token_skip_val(void){
  737. return 1;
  738. }
  739. /*Handles a category 1A zero run/coefficient value combo token.*/
  740. static ptrdiff_t oc_token_skip_run_cat1a(int _token){
  741. return _token-OC_DCT_RUN_CAT1A+2;
  742. }
  743. /*Handles category 1b, 1c, 2a, and 2b zero run/coefficient value combo tokens.*/
  744. static ptrdiff_t oc_token_skip_run(int _token,int _extra_bits){
  745. int run_cati;
  746. int ncoeffs_mask;
  747. int ncoeffs_adjust;
  748. run_cati=_token-OC_DCT_RUN_CAT1B;
  749. ncoeffs_mask=OC_BYTE_TABLE32(3,7,0,1,run_cati);
  750. ncoeffs_adjust=OC_BYTE_TABLE32(7,11,2,3,run_cati);
  751. return (_extra_bits&ncoeffs_mask)+ncoeffs_adjust;
  752. }
  753. /*A jump table for computing the number of coefficients or blocks to skip for
  754. a given token value.
  755. This reduces all the conditional branches, etc., needed to parse these token
  756. values down to one indirect jump.*/
  757. static const oc_token_skip_func OC_TOKEN_SKIP_TABLE[TH_NDCT_TOKENS]={
  758. oc_token_skip_eob,
  759. oc_token_skip_eob,
  760. oc_token_skip_eob,
  761. oc_token_skip_eob,
  762. oc_token_skip_eob,
  763. oc_token_skip_eob,
  764. oc_token_skip_eob6,
  765. oc_token_skip_zrl,
  766. oc_token_skip_zrl,
  767. (oc_token_skip_func)oc_token_skip_val,
  768. (oc_token_skip_func)oc_token_skip_val,
  769. (oc_token_skip_func)oc_token_skip_val,
  770. (oc_token_skip_func)oc_token_skip_val,
  771. (oc_token_skip_func)oc_token_skip_val,
  772. (oc_token_skip_func)oc_token_skip_val,
  773. (oc_token_skip_func)oc_token_skip_val,
  774. (oc_token_skip_func)oc_token_skip_val,
  775. (oc_token_skip_func)oc_token_skip_val,
  776. (oc_token_skip_func)oc_token_skip_val,
  777. (oc_token_skip_func)oc_token_skip_val,
  778. (oc_token_skip_func)oc_token_skip_val,
  779. (oc_token_skip_func)oc_token_skip_val,
  780. (oc_token_skip_func)oc_token_skip_val,
  781. (oc_token_skip_func)oc_token_skip_run_cat1a,
  782. (oc_token_skip_func)oc_token_skip_run_cat1a,
  783. (oc_token_skip_func)oc_token_skip_run_cat1a,
  784. (oc_token_skip_func)oc_token_skip_run_cat1a,
  785. (oc_token_skip_func)oc_token_skip_run_cat1a,
  786. oc_token_skip_run,
  787. oc_token_skip_run,
  788. oc_token_skip_run,
  789. oc_token_skip_run
  790. };
  791. /*Determines the number of blocks or coefficients to be skipped for a given
  792. token value.
  793. _token: The token value to skip.
  794. _extra_bits: The extra bits attached to this token.
  795. Return: A positive value indicates that number of coefficients are to be
  796. skipped in the current block.
  797. Otherwise, the negative of the return value indicates that number of
  798. blocks are to be ended.
  799. 0 will never be returned, so that at least one coefficient in one
  800. block will always be decoded for every token.*/
  801. static ptrdiff_t oc_dct_token_skip(int _token,int _extra_bits){
  802. return (*OC_TOKEN_SKIP_TABLE[_token])(_token,_extra_bits);
  803. }
  804. void oc_enc_mode_metrics_collect(oc_enc_ctx *_enc){
  805. static const unsigned char OC_ZZI_HUFF_OFFSET[64]={
  806. 0,16,16,16,16,16,32,32,
  807. 32,32,32,32,32,32,32,48,
  808. 48,48,48,48,48,48,48,48,
  809. 48,48,48,48,64,64,64,64,
  810. 64,64,64,64,64,64,64,64,
  811. 64,64,64,64,64,64,64,64,
  812. 64,64,64,64,64,64,64,64
  813. };
  814. const oc_fragment *frags;
  815. const unsigned *frag_sad;
  816. const unsigned *frag_satd;
  817. const unsigned *frag_ssd;
  818. const ptrdiff_t *coded_fragis;
  819. ptrdiff_t ncoded_fragis;
  820. ptrdiff_t fragii;
  821. double fragw;
  822. int modelines[3][3][2];
  823. int qti;
  824. int qii;
  825. int qi;
  826. int pli;
  827. int zzi;
  828. int token;
  829. int eb;
  830. oc_restore_fpu(&_enc->state);
  831. /*Figure out which metric bins to use for this frame's quantizers.*/
  832. for(qii=0;qii<_enc->state.nqis;qii++){
  833. for(pli=0;pli<3;pli++){
  834. for(qti=0;qti<2;qti++){
  835. int log_plq;
  836. int modeline;
  837. log_plq=_enc->log_plq[_enc->state.qis[qii]][pli][qti];
  838. for(modeline=0;modeline<OC_LOGQ_BINS-1&&
  839. OC_MODE_LOGQ[modeline+1][pli][qti]>log_plq;modeline++);
  840. modelines[qii][pli][qti]=modeline;
  841. }
  842. }
  843. }
  844. qti=_enc->state.frame_type;
  845. frags=_enc->state.frags;
  846. frag_sad=_enc->frag_sad;
  847. frag_satd=_enc->frag_satd;
  848. frag_ssd=_enc->frag_ssd;
  849. coded_fragis=_enc->state.coded_fragis;
  850. ncoded_fragis=fragii=0;
  851. /*Weight the fragments by the inverse frame size; this prevents HD content
  852. from dominating the statistics.*/
  853. fragw=1.0/_enc->state.nfrags;
  854. for(pli=0;pli<3;pli++){
  855. ptrdiff_t ti[64];
  856. int eob_token[64];
  857. int eob_run[64];
  858. /*Set up token indices and eob run counts.
  859. We don't bother trying to figure out the real cost of the runs that span
  860. coefficients; instead we use the costs that were available when R-D
  861. token optimization was done.*/
  862. for(zzi=0;zzi<64;zzi++){
  863. ti[zzi]=_enc->dct_token_offs[pli][zzi];
  864. if(ti[zzi]>0){
  865. token=_enc->dct_tokens[pli][zzi][0];
  866. eb=_enc->extra_bits[pli][zzi][0];
  867. eob_token[zzi]=token;
  868. eob_run[zzi]=-oc_dct_token_skip(token,eb);
  869. }
  870. else{
  871. eob_token[zzi]=OC_NDCT_EOB_TOKEN_MAX;
  872. eob_run[zzi]=0;
  873. }
  874. }
  875. /*Scan the list of coded fragments for this plane.*/
  876. ncoded_fragis+=_enc->state.ncoded_fragis[pli];
  877. for(;fragii<ncoded_fragis;fragii++){
  878. ptrdiff_t fragi;
  879. int frag_bits;
  880. int huffi;
  881. int skip;
  882. int mb_mode;
  883. unsigned sad;
  884. unsigned satd;
  885. double sqrt_ssd;
  886. int bin;
  887. int qtj;
  888. fragi=coded_fragis[fragii];
  889. frag_bits=0;
  890. for(zzi=0;zzi<64;){
  891. if(eob_run[zzi]>0){
  892. /*We've reached the end of the block.*/
  893. eob_run[zzi]--;
  894. break;
  895. }
  896. huffi=_enc->huff_idxs[qti][zzi>0][pli+1>>1]
  897. +OC_ZZI_HUFF_OFFSET[zzi];
  898. if(eob_token[zzi]<OC_NDCT_EOB_TOKEN_MAX){
  899. /*This token caused an EOB run to be flushed.
  900. Therefore it gets the bits associated with it.*/
  901. frag_bits+=_enc->huff_codes[huffi][eob_token[zzi]].nbits
  902. +OC_DCT_TOKEN_EXTRA_BITS[eob_token[zzi]];
  903. eob_token[zzi]=OC_NDCT_EOB_TOKEN_MAX;
  904. }
  905. token=_enc->dct_tokens[pli][zzi][ti[zzi]];
  906. eb=_enc->extra_bits[pli][zzi][ti[zzi]];
  907. ti[zzi]++;
  908. skip=oc_dct_token_skip(token,eb);
  909. if(skip<0){
  910. eob_token[zzi]=token;
  911. eob_run[zzi]=-skip;
  912. }
  913. else{
  914. /*A regular DCT value token; accumulate the bits for it.*/
  915. frag_bits+=_enc->huff_codes[huffi][token].nbits
  916. +OC_DCT_TOKEN_EXTRA_BITS[token];
  917. zzi+=skip;
  918. }
  919. }
  920. mb_mode=frags[fragi].mb_mode;
  921. qii=frags[fragi].qii;
  922. qi=_enc->state.qis[qii];
  923. sad=frag_sad[fragi]<<(pli+1&2);
  924. satd=frag_satd[fragi]<<(pli+1&2);
  925. sqrt_ssd=sqrt(frag_ssd[fragi]);
  926. qtj=mb_mode!=OC_MODE_INTRA;
  927. /*Accumulate statistics.
  928. The rate (frag_bits) and RMSE (sqrt(frag_ssd)) are not scaled by
  929. OC_BIT_SCALE and OC_RMSE_SCALE; this lets us change the scale factor
  930. yet still use old data.*/
  931. bin=OC_MINI(satd>>OC_SATD_SHIFT,OC_COMP_BINS-1);
  932. oc_mode_metrics_add(
  933. OC_MODE_METRICS_SATD[modelines[qii][pli][qtj]][pli][qtj]+bin,
  934. fragw,satd,_enc->log_plq[qi][pli][qtj],frag_bits,sqrt_ssd);
  935. bin=OC_MINI(sad>>OC_SAD_SHIFT,OC_COMP_BINS-1);
  936. oc_mode_metrics_add(
  937. OC_MODE_METRICS_SAD[modelines[qii][pli][qtj]][pli][qtj]+bin,
  938. fragw,sad,_enc->log_plq[qi][pli][qtj],frag_bits,sqrt_ssd);
  939. }
  940. }
  941. /*Update global SA(T)D/logq/rate/RMSE estimation matrix.*/
  942. oc_mode_metrics_update(OC_MODE_METRICS_SATD,4,1,
  943. OC_MODE_RD_SATD,OC_SATD_SHIFT,OC_MODE_RD_WEIGHT_SATD);
  944. oc_mode_metrics_update(OC_MODE_METRICS_SAD,4,1,
  945. OC_MODE_RD_SAD,OC_SAD_SHIFT,OC_MODE_RD_WEIGHT_SAD);
  946. }
  947. # endif
  948. #endif