intra_pred.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. #ifdef HAVE_CONFIG_H
  2. #include "config.h"
  3. #endif
  4. #include <float.h>
  5. #include <stdlib.h>
  6. #include <sys/time.h>
  7. /* for fallback to the otherwise obsolete ftime on windows */
  8. #if ! HAVE_GETTIMEOFDAY && HAVE_FTIME
  9. #include <sys/timeb.h>
  10. #endif
  11. #include "cholesky.h"
  12. #include "od_defs.h"
  13. #include "od_covmat.h"
  14. #include "od_filter.h"
  15. #include "od_intra.h"
  16. #include "image_tools.h"
  17. #include "stats_tools.h"
  18. #include "svd.h"
  19. #define USE_SVD (0)
  20. #define BITS_SELECT (0)
  21. #define USE_WEIGHTS (0)
  22. #define SUBTRACT_DC (0)
  23. #define POOLED_COV (1)
  24. #define MAKE_SPARSE (1)
  25. #define DROP_BY_MAG (0)
  26. #define TF_MASKING (1)
  27. #define WRITE_IMAGES (0)
  28. #define PRINT_PROGRESS (0)
  29. #define PRINT_BLOCKS (0)
  30. #define PRINT_COMP (0)
  31. #define PRINT_DROPS (0)
  32. #define PRINT_BETAS (0)
  33. static void od_gettime(struct timeval *tv){
  34. #if HAVE_GETTIMEOFDAY
  35. gettimeofday(tv,NULL);
  36. #elif HAVE_FTIME
  37. struct timeb ft;
  38. ftime(&ft);
  39. tv->tv_sec=ft.time;
  40. tv->tv_usec=ft.millitm*1000;
  41. #else
  42. #error No suitable wall time function available.
  43. #endif
  44. }
  45. typedef struct classify_ctx classify_ctx;
  46. struct classify_ctx{
  47. int n;
  48. intra_stats st;
  49. intra_stats gb;
  50. od_covmat pd[OD_INTRA_NMODES];
  51. image_data img;
  52. #if WRITE_IMAGES
  53. image_files files;
  54. #endif
  55. double bits;
  56. };
  57. static void classify_ctx_init(classify_ctx *_this){
  58. int i;
  59. _this->n=0;
  60. intra_stats_init(&_this->st,B_SZ_LOG);
  61. intra_stats_init(&_this->gb,B_SZ_LOG);
  62. for(i=0;i<OD_INTRA_NMODES;i++){
  63. od_covmat_init(&_this->pd[i],5*B_SZ*B_SZ);
  64. }
  65. }
  66. static void classify_ctx_clear(classify_ctx *_this){
  67. int i;
  68. intra_stats_clear(&_this->st);
  69. intra_stats_clear(&_this->gb);
  70. for(i=0;i<OD_INTRA_NMODES;i++){
  71. od_covmat_clear(&_this->pd[i]);
  72. }
  73. }
  74. static void classify_ctx_reset(classify_ctx *_this){
  75. int i;
  76. _this->n=0;
  77. intra_stats_reset(&_this->gb);
  78. for(i=0;i<OD_INTRA_NMODES;i++){
  79. od_covmat_reset(&_this->pd[i]);
  80. }
  81. _this->bits=0;
  82. }
  83. static void classify_ctx_set_image(classify_ctx *_this,const char *_name,
  84. int _nxblocks,int _nyblocks){
  85. _this->n++;
  86. intra_stats_reset(&_this->st);
  87. image_data_init(&_this->img,_name,B_SZ_LOG,_nxblocks,_nyblocks);
  88. #if WRITE_IMAGES
  89. image_files_init(&_this->files,_nxblocks,_nyblocks);
  90. #endif
  91. }
  92. static void classify_ctx_clear_image(classify_ctx *_this){
  93. image_data_clear(&_this->img);
  94. #if WRITE_IMAGES
  95. image_files_clear(&_this->files);
  96. #endif
  97. }
  98. typedef struct prob_ctx prob_ctx;
  99. struct prob_ctx{
  100. double *scale;
  101. double *xtx;
  102. double *xty;
  103. const double *mean;
  104. const double *cov;
  105. double *ete;
  106. };
  107. static void prob_ctx_init(prob_ctx *_this){
  108. _this->scale=(double *)malloc(sizeof(*_this->scale)*5*B_SZ*B_SZ);
  109. _this->xtx=(double *)malloc(sizeof(*_this->xtx)*4*B_SZ*B_SZ*4*B_SZ*B_SZ);
  110. _this->xty=(double *)malloc(sizeof(*_this->xty)*4*B_SZ*B_SZ*B_SZ*B_SZ);
  111. _this->mean=NULL;
  112. _this->cov=NULL;
  113. _this->ete=(double *)malloc(sizeof(*_this->ete)*B_SZ*B_SZ*B_SZ*B_SZ);
  114. }
  115. static void prob_ctx_clear(prob_ctx *_this){
  116. free(_this->scale);
  117. free(_this->xtx);
  118. free(_this->xty);
  119. _this->mean=NULL;
  120. _this->cov=NULL;
  121. free(_this->ete);
  122. }
  123. static void prob_ctx_load(prob_ctx *_this,od_covmat *_mat){
  124. int i;
  125. int j;
  126. /* compute the scale factors */
  127. for(i=0;i<5*B_SZ*B_SZ;i++){
  128. _this->scale[i]=sqrt(_mat->cov[i*5*B_SZ*B_SZ+i]);
  129. }
  130. /* normalize X^T*X and X^T*Y */
  131. for(j=0;j<4*B_SZ*B_SZ;j++){
  132. for(i=0;i<4*B_SZ*B_SZ;i++){
  133. _this->xtx[4*B_SZ*B_SZ*j+i]=
  134. _mat->cov[5*B_SZ*B_SZ*j+i]/(_this->scale[j]*_this->scale[i]);
  135. }
  136. for(i=0;i<B_SZ*B_SZ;i++){
  137. _this->xty[B_SZ*B_SZ*j+i]=_mat->cov[5*B_SZ*B_SZ*j+4*B_SZ*B_SZ+i];
  138. _this->xty[B_SZ*B_SZ*j+i]/=_this->scale[j]*_this->scale[4*B_SZ*B_SZ+i];
  139. }
  140. }
  141. _this->mean=_mat->mean;
  142. _this->cov=_mat->cov;
  143. }
  144. static void prob_ctx_comp_error(prob_ctx *_this,od_covmat *_mat,double *_beta_1){
  145. int j;
  146. int i;
  147. for(j=0;j<B_SZ*B_SZ;j++){
  148. for(i=0;i<B_SZ*B_SZ;i++){
  149. int ji;
  150. int l;
  151. int k;
  152. ji=B_SZ*B_SZ*j+i;
  153. l=5*B_SZ*B_SZ*(4*B_SZ*B_SZ+j);
  154. /* E^T*E = Y^T*Y - Y^T*X * beta_1 */
  155. _this->ete[ji]=_mat->cov[l+4*B_SZ*B_SZ+i];
  156. for(k=0;k<4*B_SZ*B_SZ;k++){
  157. _this->ete[ji]-=_mat->cov[l+k]*_beta_1[4*B_SZ*B_SZ*i+k];
  158. }
  159. }
  160. }
  161. #if PRINT_COMP
  162. fprintf(stderr,"ete=[");
  163. for(j=0;j<B_SZ*B_SZ;j++){
  164. fprintf(stderr,"%s",j!=0?";":"");
  165. for(i=0;i<B_SZ*B_SZ;i++){
  166. fprintf(stderr,"%s%- 24.18G",i!=0?",":"",_this->ete[B_SZ*B_SZ*j+i]);
  167. }
  168. }
  169. fprintf(stderr,"];\n");
  170. #endif
  171. }
  172. static void update_diversity(const double *_ete,double _b[B_SZ*B_SZ],
  173. const double *_scale){
  174. int v;
  175. int u;
  176. for(v=0;v<B_SZ;v++){
  177. for(u=0;u<B_SZ;u++){
  178. int i;
  179. int ii;
  180. i=B_SZ*v+u;
  181. ii=B_SZ*B_SZ*i+i;
  182. _b[i]=sqrt(_scale[v]*_scale[u]*_ete[ii]/2);
  183. }
  184. }
  185. }
  186. #if PRINT_BETAS && POOLED_COV
  187. static void print_diversity(FILE *_fp,double _b[B_SZ*B_SZ],
  188. const double *_scale){
  189. int j;
  190. int i;
  191. fprintf(_fp,"const ogg_int16_t OD_SATD_WEIGHTS_%dx%d[%d*%d] = {\n",B_SZ,B_SZ,B_SZ,B_SZ);
  192. for(j=0;j<B_SZ;j++){
  193. for(i=0;i<B_SZ;i++){
  194. double b;
  195. b=M_LOG2E/(_b[j*B_SZ+i]/(sqrt(_scale[i]*_scale[j])*INPUT_SCALE));
  196. fprintf(_fp,"%s%3i%s",
  197. i>0?" ":" ",(int)(b*256+0.5),j==B_SZ-1&&i==B_SZ-1?"":",");
  198. }
  199. fprintf(_fp,"\n");
  200. }
  201. fprintf(_fp,"};\n");
  202. }
  203. #endif
  204. typedef struct solve_ctx solve_ctx;
  205. struct solve_ctx{
  206. #if USE_SVD
  207. double *xtx;
  208. double **xtxp;
  209. double *s;
  210. #else
  211. double *r;
  212. int *pivot;
  213. double *tau;
  214. double *b;
  215. double *work;
  216. #endif
  217. double *beta_0;
  218. double *beta_1;
  219. };
  220. static void solve_ctx_init(solve_ctx *_this){
  221. #if USE_SVD
  222. _this->xtx=(double *)malloc(sizeof(*_this->xtx)*2*4*B_SZ*B_SZ*4*B_SZ*B_SZ);
  223. _this->xtxp=(double **)malloc(sizeof(*_this->xtxp)*2*4*B_SZ*B_SZ);
  224. _this->s=(double *)malloc(sizeof(*_this->s)*4*B_SZ*B_SZ);
  225. #else
  226. _this->r=(double *)malloc(sizeof(*_this->r)*UT_SZ(4*B_SZ*B_SZ,4*B_SZ*B_SZ));
  227. _this->pivot=(int *)malloc(sizeof(*_this->pivot)*4*B_SZ*B_SZ);
  228. _this->tau=(double *)malloc(sizeof(*_this->tau)*4*B_SZ*B_SZ);
  229. _this->b=(double *)malloc(sizeof(*_this->b)*4*B_SZ*B_SZ);
  230. _this->work=(double *)malloc(sizeof(*_this->work)*4*B_SZ*B_SZ);
  231. #endif
  232. _this->beta_0=(double *)malloc(sizeof(*_this->beta_0)*B_SZ*B_SZ);
  233. _this->beta_1=(double *)malloc(sizeof(*_this->beta_1)*B_SZ*B_SZ*4*B_SZ*B_SZ);
  234. }
  235. static void solve_ctx_clear(solve_ctx *_this){
  236. #if USE_SVD
  237. free(_this->xtx);
  238. free(_this->xtxp);
  239. free(_this->s);
  240. #else
  241. free(_this->r);
  242. free(_this->pivot);
  243. free(_this->tau);
  244. free(_this->b);
  245. free(_this->work);
  246. #endif
  247. free(_this->beta_0);
  248. free(_this->beta_1);
  249. }
  250. /* solve for beta_0[_y] and beta_1[_y] */
  251. static void solve(const prob_ctx *_prob,solve_ctx *_sol,int _y,int *_mask,
  252. double *_beta_0,double *_beta_1){
  253. int nmi;
  254. int mi[4*B_SZ*B_SZ];
  255. int i;
  256. int j;
  257. #if !USE_SVD
  258. int rank;
  259. #endif
  260. nmi=0;
  261. for(i=0;i<4*B_SZ*B_SZ;i++){
  262. if(_mask[i]){
  263. mi[nmi]=i;
  264. nmi++;
  265. }
  266. _beta_1[_y*4*B_SZ*B_SZ+i]=0;
  267. }
  268. #if USE_SVD
  269. for(j=0;j<nmi;j++){
  270. for(i=0;i<nmi;i++){
  271. _sol->xtx[4*B_SZ*B_SZ*j+i]=_prob->xtx[4*B_SZ*B_SZ*mi[j]+mi[i]];
  272. }
  273. }
  274. for(i=0;i<2*nmi;i++){
  275. _sol->xtxp[i]=&_sol->xtx[4*B_SZ*B_SZ*i];
  276. }
  277. svd_pseudoinverse(_sol->xtxp,_sol->s,nmi,nmi);
  278. #else
  279. for(j=0;j<nmi;j++){
  280. for(i=j;i<nmi;i++){
  281. _sol->r[UT_IDX(j,i,nmi)]=_prob->xtx[4*B_SZ*B_SZ*mi[j]+mi[i]];
  282. }
  283. _sol->b[j]=_prob->xty[B_SZ*B_SZ*mi[j]+_y];
  284. }
  285. rank=cholesky(_sol->r,_sol->pivot,DBL_EPSILON,nmi);
  286. chdecomp(_sol->r,_sol->tau,rank,nmi);
  287. chsolve(_sol->r,_sol->pivot,_sol->tau,_sol->b,_sol->b,_sol->work,rank,nmi);
  288. #endif
  289. /* compute beta_1 = (X^T*X)^-1 * X^T*Y and beta_0 = Ym - Xm * beta_1 */
  290. _beta_0[_y]=_prob->mean[4*B_SZ*B_SZ+_y];
  291. for(j=0;j<nmi;j++){
  292. int yj;
  293. yj=_y*4*B_SZ*B_SZ+mi[j];
  294. #if USE_SVD
  295. _beta_1[yj]=0;
  296. for(i=0;i<nmi;i++){
  297. _beta_1[yj]+=_sol->xtx[4*B_SZ*B_SZ*j+i]*_prob->xty[B_SZ*B_SZ*mi[i]+_y];
  298. }
  299. #else
  300. _beta_1[yj]=_sol->b[j];
  301. #endif
  302. _beta_1[yj]*=_prob->scale[4*B_SZ*B_SZ+_y]/_prob->scale[mi[j]];
  303. _beta_0[_y]-=_prob->mean[mi[j]]*_beta_1[yj];
  304. }
  305. }
  306. static double comp_error(const prob_ctx *_prob,solve_ctx *_sol,int _y,
  307. int *_mask){
  308. double err;
  309. int j;
  310. int i;
  311. solve(_prob,_sol,_y,_mask,_sol->beta_0,_sol->beta_1);
  312. j=4*B_SZ*B_SZ+_y;
  313. err=_prob->cov[5*B_SZ*B_SZ*j+j];
  314. for(i=0;i<4*B_SZ*B_SZ;i++){
  315. if(_mask[i]){
  316. err-=_prob->cov[5*B_SZ*B_SZ*j+i]*_sol->beta_1[4*B_SZ*B_SZ*_y+i];
  317. }
  318. }
  319. return(err);
  320. }
  321. static int comp_delta_pg(const prob_ctx *_prob,solve_ctx _sol[NUM_PROCS],int _y,
  322. int _mask[4*B_SZ*B_SZ],double *_delta_pg){
  323. double s;
  324. int i;
  325. int j;
  326. int nmi;
  327. int mi[4*B_SZ*B_SZ];
  328. int mask[NUM_PROCS][4*B_SZ*B_SZ];
  329. double delta_pg[4*B_SZ*B_SZ];
  330. nmi=0;
  331. for(i=0;i<4*B_SZ*B_SZ;i++){
  332. if(_mask[i]){
  333. mi[nmi]=i;
  334. nmi++;
  335. }
  336. for(j=0;j<NUM_PROCS;j++){
  337. mask[j][i]=_mask[i];
  338. }
  339. }
  340. s=1/comp_error(_prob,&_sol[0],_y,_mask);
  341. #pragma omp parallel for schedule(dynamic)
  342. for(i=0;i<nmi;i++){
  343. int tid;
  344. tid=OD_OMP_GET_THREAD;
  345. mask[tid][mi[i]]=0;
  346. delta_pg[i]=comp_error(_prob,&_sol[tid],_y,mask[tid])*s;
  347. mask[tid][mi[i]]=1;
  348. }
  349. #if SUBTRACT_DC
  350. if(_y==0) {
  351. for(i=0;i<nmi;i++){
  352. if(mi[i]%(B_SZ*B_SZ)==0){
  353. delta_pg[i]=UINT_MAX;
  354. }
  355. }
  356. }
  357. #endif
  358. j=-1;
  359. for(i=0;i<nmi;i++){
  360. if(j==-1||delta_pg[i]<delta_pg[j]){
  361. j=i;
  362. }
  363. }
  364. if(j==-1){
  365. return j;
  366. }
  367. *_delta_pg=delta_pg[j];
  368. return(mi[j]);
  369. }
  370. static long timing(const struct timeval *_start,const struct timeval *_stop){
  371. long ms;
  372. ms=(_stop->tv_sec-_start->tv_sec)*1000;
  373. ms+=(_stop->tv_usec-_start->tv_usec)/1000;
  374. return ms;
  375. }
  376. static void comp_predictors(const prob_ctx *_prob,solve_ctx _sol[NUM_PROCS],
  377. int _drop,int _mask[B_SZ*B_SZ*4*B_SZ*B_SZ]){
  378. int i;
  379. int j;
  380. #if PRINT_COMP
  381. fprintf(stderr,"xtx=[");
  382. for(j=0;j<4*B_SZ*B_SZ;j++){
  383. fprintf(stderr,"%s",j!=0?";":"");
  384. for(i=0;i<4*B_SZ*B_SZ;i++){
  385. fprintf(stderr,"%s%- 24.18G",i!=0?",":"",_prob->xtx[4*B_SZ*B_SZ*j+i]);
  386. }
  387. }
  388. fprintf(stderr,"];\n");
  389. fprintf(stderr,"xty=[");
  390. for(j=0;j<4*B_SZ*B_SZ;j++){
  391. fprintf(stderr,"%s",j!=0?";":"");
  392. for(i=0;i<B_SZ*B_SZ;i++){
  393. fprintf(stderr,"%s%- 24.18G",i!=0?",":"",_prob->xty[B_SZ*B_SZ*j+i]);
  394. }
  395. }
  396. fprintf(stderr,"];\n");
  397. #endif
  398. if(_drop>0){
  399. #if DROP_BY_MAG
  400. double min[B_SZ*B_SZ];
  401. int idx[B_SZ*B_SZ];
  402. #pragma omp parallel for schedule(dynamic)
  403. for(j=0;j<B_SZ*B_SZ;j++){
  404. int tid;
  405. tid=OD_OMP_GET_THREAD;
  406. solve(_prob,&_sol[tid],j,&_mask[j*4*B_SZ*B_SZ],_sol->beta_0,_sol->beta_1);
  407. }
  408. for(j=0;j<B_SZ*B_SZ;j++){
  409. idx[j]=-1;
  410. min[j]=UINT_MAX;
  411. for(i=0;i<4*B_SZ*B_SZ;i++){
  412. if(_mask[4*B_SZ*B_SZ*j+i]&&fabs(_sol->beta_1[4*B_SZ*B_SZ*j+i])<min[j]){
  413. idx[j]=i;
  414. min[j]=fabs(_sol->beta_1[4*B_SZ*B_SZ*j+i]);
  415. }
  416. }
  417. }
  418. while(_drop-->0){
  419. j=-1;
  420. for(i=0;i<B_SZ*B_SZ;i++){
  421. if(idx[i]!=-1&&(j==-1||min[i]<min[j])){
  422. j=i;
  423. }
  424. }
  425. #if PRINT_DROPS
  426. printf("Dropping (%2i,%2i) with beta_1=%g\n",j,idx[j],min[j]);
  427. fflush(stdout);
  428. #endif
  429. _mask[4*B_SZ*B_SZ*j+idx[j]]=0;
  430. idx[j]=-1;
  431. min[j]=UINT_MAX;
  432. for(i=0;i<4*B_SZ*B_SZ;i++){
  433. if(_mask[4*B_SZ*B_SZ*j+i]&&fabs(_sol->beta_1[4*B_SZ*B_SZ*j+i])<min[j]){
  434. idx[j]=i;
  435. min[j]=fabs(_sol->beta_1[4*B_SZ*B_SZ*j+i]);
  436. }
  437. }
  438. }
  439. #else
  440. double delta_pg[B_SZ*B_SZ];
  441. int idx[B_SZ*B_SZ];
  442. for(j=0;j<B_SZ*B_SZ;j++){
  443. idx[j]=comp_delta_pg(_prob,_sol,j,&_mask[j*4*B_SZ*B_SZ],&delta_pg[j]);
  444. }
  445. while(_drop-->0){
  446. j=-1;
  447. for(i=0;i<B_SZ*B_SZ;i++){
  448. if(idx[i]!=-1&&(j==-1||delta_pg[i]<delta_pg[j])){
  449. j=i;
  450. }
  451. }
  452. #if PRINT_DROPS
  453. printf("Dropping (%2i,%2i) cost Pg=%g\n",j,idx[j],10*log10(delta_pg[j]));
  454. fflush(stdout);
  455. #endif
  456. _mask[j*4*B_SZ*B_SZ+idx[j]]=0;
  457. idx[j]=comp_delta_pg(_prob,_sol,j,&_mask[j*4*B_SZ*B_SZ],&delta_pg[j]);
  458. }
  459. #endif
  460. }
  461. #pragma omp parallel for schedule(dynamic)
  462. for(j=0;j<B_SZ*B_SZ;j++){
  463. int tid;
  464. tid=OD_OMP_GET_THREAD;
  465. solve(_prob,&_sol[tid],j,&_mask[j*4*B_SZ*B_SZ],_sol->beta_0,_sol->beta_1);
  466. }
  467. #if PRINT_COMP
  468. fprintf(stderr,"beta_1=[");
  469. for(j=0;j<4*B_SZ*B_SZ;j++){
  470. fprintf(stderr,"%s",j!=0?";":"");
  471. for(i=0;i<B_SZ*B_SZ;i++){
  472. fprintf(stderr,"%s%- 24.18G",i!=0?",":"",_sol->beta_1[4*B_SZ*B_SZ*i+j]);
  473. }
  474. }
  475. fprintf(stderr,"];\n");
  476. fprintf(stderr,"beta_0=[");
  477. for(i=0;i<B_SZ*B_SZ;i++){
  478. fprintf(stderr,"%s%- 24.18G",i!=0?",":"",_sol->beta_0[i]);
  479. }
  480. fprintf(stderr,"];\n");
  481. #endif
  482. }
  483. #if PRINT_PROGRESS
  484. static void print_progress(FILE *_fp,const char *_proc){
  485. int tid;
  486. tid=OD_OMP_GET_THREAD;
  487. fprintf(_fp,"thread %i in %s\n",tid,_proc);
  488. }
  489. #endif
  490. #if MASK_BLOCKS
  491. static void ip_mask_block(void *_ctx,const unsigned char *_data,int _stride,
  492. int _bi,int _bj){
  493. #if PRINT_PROGRESS
  494. if(_bi==0&&_bj==0){
  495. print_progress(stdout,"ip_mask_block");
  496. }
  497. #endif
  498. if(_bi==0&&_bj==0){
  499. classify_ctx *ctx;
  500. ctx=(classify_ctx *)_ctx;
  501. image_data_mask(&ctx->img,_data,_stride);
  502. }
  503. }
  504. #endif
  505. static void ip_pre_block(void *_ctx,const unsigned char *_data,int _stride,
  506. int _bi,int _bj){
  507. classify_ctx *ctx;
  508. #if PRINT_PROGRESS
  509. if(_bi==0&&_bj==0){
  510. print_progress(stdout,"ip_pre_block");
  511. }
  512. #endif
  513. ctx=(classify_ctx *)_ctx;
  514. image_data_pre_block(&ctx->img,_data,_stride,_bi,_bj);
  515. }
  516. static void ip_fdct_block(void *_ctx,const unsigned char *_data,int _stride,
  517. int _bi,int _bj){
  518. classify_ctx *ctx;
  519. (void)_data;
  520. (void)_stride;
  521. #if PRINT_PROGRESS
  522. if(_bi==0&&_bj==0){
  523. print_progress(stdout,"ip_fdct_block");
  524. }
  525. #endif
  526. ctx=(classify_ctx *)_ctx;
  527. image_data_fdct_block(&ctx->img,_bi,_bj);
  528. }
  529. #if TF_BLOCKS
  530. static void ip_tf_block(void *_ctx,const unsigned char *_data,int _stride,
  531. int _bi,int _bj){
  532. classify_ctx *ctx;
  533. (void)_data;
  534. (void)_stride;
  535. #if PRINT_PROGRESS
  536. if(_bi==0&&_bj==0){
  537. print_progress(stdout,"ip_tf_block");
  538. }
  539. #endif
  540. ctx=(classify_ctx *)_ctx;
  541. image_data_tf_block(&ctx->img,_bi,_bj);
  542. }
  543. #endif
  544. static void ip_add_block(void *_ctx,const unsigned char *_data,int _stride,
  545. int _bi,int _bj){
  546. classify_ctx *ctx;
  547. od_coeff *block;
  548. int by;
  549. int bx;
  550. int j;
  551. int i;
  552. double buf[5*B_SZ*B_SZ];
  553. int mode;
  554. double weight;
  555. (void)_data;
  556. (void)_stride;
  557. #if PRINT_PROGRESS
  558. if(_bi==0&&_bj==0){
  559. print_progress(stdout,"ip_add_block");
  560. }
  561. #endif
  562. ctx=(classify_ctx *)_ctx;
  563. #if MASK_BLOCKS
  564. if(!ctx->img.mask[ctx->img.nxblocks*_bj+_bi]){
  565. return;
  566. }
  567. #endif
  568. for(by=0;by<=1;by++){
  569. for(bx=0;bx<=(1-by)<<1;bx++){
  570. #if TF_BLOCKS
  571. block=&ctx->img.tf[ctx->img.fdct_stride*B_SZ*(_bj+by)+B_SZ*(_bi+bx)];
  572. #else
  573. block=&ctx->img.fdct[ctx->img.fdct_stride*B_SZ*(_bj+by)+B_SZ*(_bi+bx)];
  574. #endif
  575. for(j=0;j<B_SZ;j++){
  576. for(i=0;i<B_SZ;i++){
  577. buf[B_SZ*B_SZ*(3*by+bx)+j*B_SZ+i]=block[ctx->img.fdct_stride*j+i];
  578. }
  579. }
  580. }
  581. }
  582. block=&ctx->img.fdct[ctx->img.fdct_stride*B_SZ*(_bj+1)+B_SZ*(_bi+1)];
  583. for(j=0;j<B_SZ;j++){
  584. for(i=0;i<B_SZ;i++){
  585. buf[B_SZ*B_SZ*4+j*B_SZ+i]=block[ctx->img.fdct_stride*j+i];
  586. }
  587. }
  588. #if SUBTRACT_DC
  589. for(i=0;i<4;i++){
  590. buf[4*B_SZ*B_SZ]-=0.25*buf[i*B_SZ*B_SZ];
  591. }
  592. #endif
  593. mode=ctx->img.mode[ctx->img.nxblocks*_bj+_bi];
  594. weight=ctx->img.weight[ctx->img.nxblocks*_bj+_bi];
  595. od_covmat_add(&ctx->pd[mode],buf,weight);
  596. }
  597. #if PRINT_BLOCKS
  598. static void ip_print_block(void *_ctx,const unsigned char *_data,int _stride,
  599. int _bi,int _bj){
  600. classify_ctx *ctx;
  601. (void)_data;
  602. (void)_stride;
  603. #if PRINT_PROGRESS
  604. if(_bi==0&&_bj==0){
  605. print_progress(stdout,"ip_print_block");
  606. }
  607. #endif
  608. ctx=(classify_ctx *)_ctx;
  609. image_data_print_block(&ctx->img,_bi,_bj,stderr);
  610. }
  611. #endif
  612. static void ip_pred_block(void *_ctx,const unsigned char *_data,int _stride,
  613. int _bi,int _bj){
  614. classify_ctx *ctx;
  615. (void)_data;
  616. (void)_stride;
  617. #if PRINT_PROGRESS
  618. if(_bi==0&&_bj==0){
  619. print_progress("ip_pred_block");
  620. }
  621. #endif
  622. ctx=(classify_ctx *)_ctx;
  623. image_data_pred_block(&ctx->img,_bi,_bj);
  624. }
  625. static void ip_idct_block(void *_ctx,const unsigned char *_data,int _stride,
  626. int _bi,int _bj){
  627. classify_ctx *ctx;
  628. (void)_data;
  629. (void)_stride;
  630. #if PRINT_PROGRESS
  631. if(_bi==0&&_bj==0){
  632. print_progress("ip_idct_block");
  633. }
  634. #endif
  635. ctx=(classify_ctx *)_ctx;
  636. image_data_idct_block(&ctx->img,_bi,_bj);
  637. }
  638. static void ip_post_block(void *_ctx,const unsigned char *_data,int _stride,
  639. int _bi,int _bj){
  640. classify_ctx *ctx;
  641. (void)_data;
  642. (void)_stride;
  643. #if PRINT_PROGRESS
  644. if(_bi==0&&_bj==0){
  645. print_progress("ip_post_block");
  646. }
  647. #endif
  648. ctx=(classify_ctx *)_ctx;
  649. image_data_post_block(&ctx->img,_bi,_bj);
  650. }
  651. double b[OD_INTRA_NMODES][B_SZ*B_SZ];
  652. static void ip_stats_block(void *_ctx,const unsigned char *_data,int _stride,
  653. int _bi,int _bj){
  654. classify_ctx *ctx;
  655. #if PRINT_PROGRESS
  656. if(_bi==0&&_bj==0){
  657. print_progress("ip_stats_block");
  658. }
  659. #endif
  660. ctx=(classify_ctx *)_ctx;
  661. #if MASK_BLOCKS
  662. if(!ctx->img.mask[ctx->img.nxblocks*_bj+_bi]){
  663. return;
  664. }
  665. #endif
  666. image_data_stats_block(&ctx->img,_data,_stride,_bi,_bj,&ctx->st);
  667. {
  668. od_coeff *block;
  669. int bstride;
  670. double *pred;
  671. int pstride;
  672. int mode;
  673. double *od_scale;
  674. int j;
  675. int i;
  676. block=&ctx->img.fdct[ctx->img.fdct_stride*B_SZ*(_bj+1)+B_SZ*(_bi+1)];
  677. bstride=ctx->img.fdct_stride;
  678. pred=&ctx->img.pred[ctx->img.pred_stride*B_SZ*_bj+B_SZ*_bi];
  679. pstride=ctx->img.pred_stride;
  680. mode=ctx->img.mode[ctx->img.nxblocks*_bj+_bi];
  681. od_scale=OD_SCALE[B_SZ_LOG-OD_LOG_BSIZE0];
  682. for(j=0;j<B_SZ;j++){
  683. for(i=0;i<B_SZ;i++){
  684. double res;
  685. res=sqrt(od_scale[j]*od_scale[i])*
  686. abs(block[bstride*j+i]-(od_coeff)floor(pred[pstride*j+i]+0.5));
  687. ctx->bits+=1+OD_LOG2(b[mode][j*B_SZ+i])+M_LOG2E/b[mode][j*B_SZ+i]*res;
  688. }
  689. }
  690. }
  691. }
  692. #if WRITE_IMAGES
  693. static void ip_files_block(void *_ctx,const unsigned char *_data,int _stride,
  694. int _bi,int _bj){
  695. classify_ctx *ctx;
  696. #if PRINT_PROGRESS
  697. if(_bi==0&&_bj==0){
  698. print_progress(stdout,"ip_files_block");
  699. }
  700. #endif
  701. ctx=(classify_ctx *)_ctx;
  702. image_data_files_block(&ctx->img,_data,_stride,_bi,_bj,&ctx->files);
  703. }
  704. #endif
  705. int step;
  706. static void vp8_mode_block(void *_ctx,const unsigned char *_data,int _stride,
  707. int _bi,int _bj){
  708. classify_ctx *ctx;
  709. unsigned char *mode;
  710. double *weight;
  711. #if PRINT_PROGRESS
  712. if(_bi==0&&_bj==0){
  713. print_progress(stdout,"ip_vp8_mode_block");
  714. }
  715. #endif
  716. ctx=(classify_ctx *)_ctx;
  717. mode=&ctx->img.mode[ctx->img.nxblocks*_bj+_bi];
  718. weight=&ctx->img.weight[ctx->img.nxblocks*_bj+_bi];
  719. *mode=vp8_select_mode(_data,_stride,weight);
  720. #if USE_WEIGHTS
  721. if(*mode==0){
  722. *weight=1;
  723. }
  724. #else
  725. *weight=1;
  726. #endif
  727. }
  728. static void od_mode_block(void *_ctx,const unsigned char *_data,int _stride,
  729. int _bi,int _bj){
  730. classify_ctx *ctx;
  731. unsigned char *mode;
  732. od_coeff block[5*B_SZ*B_SZ];
  733. double *weight;
  734. (void)_data;
  735. (void)_stride;
  736. #if PRINT_PROGRESS
  737. if(_bi==0&&_bj==0){
  738. print_progress("od_mode_block");
  739. }
  740. #endif
  741. ctx=(classify_ctx *)_ctx;
  742. mode=&ctx->img.mode[ctx->img.nxblocks*_bj+_bi];
  743. image_data_load_block(&ctx->img,_bi,_bj,block);
  744. weight=&ctx->img.weight[ctx->img.nxblocks*_bj+_bi];
  745. #if BITS_SELECT
  746. if(step==1){
  747. *mode=od_select_mode_satd(block,weight,ctx->img.b_sz_log);
  748. }
  749. else{
  750. *mode=od_select_mode_bits(block,weight,b);
  751. }
  752. #else
  753. *mode=od_select_mode_satd(block,weight,ctx->img.b_sz_log);
  754. #endif
  755. #if USE_WEIGHTS
  756. if(*mode==0){
  757. *weight=1;
  758. }
  759. #else
  760. *weight=1;
  761. #endif
  762. }
  763. static int init_start(void *_ctx,const char *_name,
  764. const video_input_info *_info,int _pli,int _nxblocks,int _nyblocks){
  765. classify_ctx *ctx;
  766. (void)_info;
  767. (void)_pli;
  768. #if PRINT_PROGRESS
  769. print_progress(stdout,"init_start");
  770. #endif
  771. fprintf(stdout,"%s\n",_name);
  772. fflush(stdout);
  773. ctx=(classify_ctx *)_ctx;
  774. classify_ctx_set_image(ctx,_name,_nxblocks,_nyblocks);
  775. return EXIT_SUCCESS;
  776. }
  777. static int init_finish(void *_ctx){
  778. classify_ctx *ctx;
  779. #if PRINT_PROGRESS
  780. print_progress(stdout,"init_finish");
  781. #endif
  782. ctx=(classify_ctx *)_ctx;
  783. /*intra_stats_combine(&ctx->gb,&ctx->st);
  784. intra_stats_correct(&ctx->st);
  785. fprintf(stdout,"%s\n",ctx->img.name);
  786. intra_stats_print(&ctx->st,"Daala Intra Predictors",OD_SCALE);
  787. fflush(stdout);*/
  788. image_data_save_map(&ctx->img);
  789. classify_ctx_clear_image(ctx);
  790. return EXIT_SUCCESS;
  791. }
  792. const block_func INIT[]={
  793. #if MASK_BLOCKS
  794. ip_mask_block,
  795. #endif
  796. ip_pre_block,
  797. ip_fdct_block,
  798. #if TF_BLOCKS
  799. ip_tf_block,
  800. #endif
  801. vp8_mode_block,
  802. ip_add_block,
  803. #if PRINT_BLOCKS
  804. ip_print_block,
  805. #endif
  806. };
  807. const int NINIT=sizeof(INIT)/sizeof(*INIT);
  808. static int pred_start(void *_ctx,const char *_name,
  809. const video_input_info *_info,int _pli,int _nxblocks,int _nyblocks){
  810. classify_ctx *ctx;
  811. (void)_info;
  812. (void)_pli;
  813. #if PRINT_PROGRESS
  814. print_progress(stdout,"pred_start");
  815. #endif
  816. ctx=(classify_ctx *)_ctx;
  817. classify_ctx_set_image(ctx,_name,_nxblocks,_nyblocks);
  818. image_data_load_map(&ctx->img);
  819. return EXIT_SUCCESS;
  820. }
  821. static int pred_finish(void *_ctx){
  822. classify_ctx *ctx;
  823. #if WRITE_IMAGES
  824. char suffix[16];
  825. #endif
  826. #if PRINT_PROGRESS
  827. print_progress(stdout,"pred_finish");
  828. #endif
  829. ctx=(classify_ctx *)_ctx;
  830. intra_stats_combine(&ctx->gb,&ctx->st);
  831. intra_stats_correct(&ctx->st);
  832. fprintf(stdout,"%s\n",ctx->img.name);
  833. intra_stats_print(&ctx->st,"Daala Intra Predictors",
  834. OD_SCALE[B_SZ_LOG-OD_LOG_BSIZE0]);
  835. fflush(stdout);
  836. #if WRITE_IMAGES
  837. sprintf(suffix,"-step%02i",step);
  838. image_files_write(&ctx->files,ctx->img.name,suffix);
  839. #endif
  840. image_data_save_map(&ctx->img);
  841. classify_ctx_clear_image(ctx);
  842. return EXIT_SUCCESS;
  843. }
  844. const block_func PRED[]={
  845. #if MASK_BLOCKS
  846. ip_mask_block,
  847. #endif
  848. ip_pre_block,
  849. ip_fdct_block,
  850. #if TF_BLOCKS
  851. ip_tf_block,
  852. #endif
  853. od_mode_block,
  854. ip_add_block,
  855. ip_pred_block,
  856. ip_stats_block,
  857. ip_idct_block,
  858. ip_post_block,
  859. #if WRITE_IMAGES
  860. ip_files_block,
  861. #endif
  862. };
  863. const int NPRED=sizeof(PRED)/sizeof(*PRED);
  864. #define PADDING (4*B_SZ)
  865. #if PADDING<3*B_SZ
  866. # error "PADDING must be at least 3*B_SZ"
  867. #endif
  868. #define INIT_STEPS (10)
  869. #if B_SZ==4
  870. # define DROPS_PER_STEP (16)
  871. #elif B_SZ==8
  872. # define DROPS_PER_STEP (64)
  873. #elif B_SZ==16
  874. # define DROPS_PER_STEP (256)
  875. #else
  876. # error "Unsupported block size."
  877. #endif
  878. int main(int _argc,const char *_argv[]){
  879. classify_ctx *cls;
  880. int i;
  881. int j;
  882. ne_filter_params_init();
  883. vp8_scale_init(VP8_SCALE[B_SZ_LOG-OD_LOG_BSIZE0],B_SZ_LOG);
  884. od_scale_init(OD_SCALE[B_SZ_LOG-OD_LOG_BSIZE0],B_SZ_LOG);
  885. #if WRITE_IMAGES
  886. intra_map_colors(COLORS,OD_INTRA_NMODES);
  887. #endif
  888. cls=(classify_ctx *)malloc(sizeof(*cls)*NUM_PROCS);
  889. for(i=0;i<NUM_PROCS;i++){
  890. classify_ctx_init(&cls[i]);
  891. }
  892. od_intra_init();
  893. OD_OMP_SET_THREADS(NUM_PROCS);
  894. /* First pass across images uses VP8 mode selection. */
  895. ne_apply_to_blocks(cls,sizeof(*cls),0x1,PADDING,init_start,NINIT,INIT,
  896. init_finish,_argc,_argv);
  897. for(i=1;i<NUM_PROCS;i++){
  898. cls[0].n+=cls[i].n;
  899. }
  900. if(cls[0].n>0){
  901. prob_ctx prob;
  902. solve_ctx sol[NUM_PROCS];
  903. od_covmat ete;
  904. int *mask;
  905. struct timeval start;
  906. struct timeval stop;
  907. prob_ctx_init(&prob);
  908. for(i=0;i<NUM_PROCS;i++){
  909. solve_ctx_init(&sol[i]);
  910. }
  911. od_covmat_init(&ete,B_SZ*B_SZ);
  912. mask=(int *)malloc(sizeof(*mask)*OD_INTRA_NMODES*B_SZ*B_SZ*4*B_SZ*B_SZ);
  913. #if TF_BLOCKS && TF_MASKING
  914. for(j=0;j<OD_INTRA_NMODES;j++){
  915. int *mode_mask;
  916. int *coeff_mask;
  917. int u;
  918. int v;
  919. mode_mask=&mask[B_SZ*B_SZ*4*B_SZ*B_SZ*j];
  920. for(i=0;i<B_SZ*B_SZ;i++){
  921. coeff_mask=&mode_mask[4*B_SZ*B_SZ*i];
  922. for(v=0;v<B_SZ;v++){
  923. for(u=0;u<B_SZ;u++){
  924. /* UL */
  925. coeff_mask[0*B_SZ*B_SZ+B_SZ*v+u]=v>=B_SZ-4&&u>=B_SZ-4;
  926. /* U */
  927. coeff_mask[1*B_SZ*B_SZ+B_SZ*v+u]=v>=B_SZ-4;
  928. /* UR */
  929. coeff_mask[2*B_SZ*B_SZ+B_SZ*v+u]=v>=B_SZ-4;
  930. /* L */
  931. coeff_mask[3*B_SZ*B_SZ+B_SZ*v+u]=u>=B_SZ-4;
  932. }
  933. }
  934. }
  935. }
  936. #else
  937. for(i=0;i<OD_INTRA_NMODES*B_SZ*B_SZ*4*B_SZ*B_SZ;i++){
  938. mask[i]=1;
  939. }
  940. #endif
  941. od_gettime(&start);
  942. /* Each k-means step uses Daala mode selection. */
  943. for(step=1;;step++){
  944. int mults;
  945. int drops;
  946. #if TF_BLOCKS && TF_MASKING
  947. mults=(B_SZ/4*3+1)*16*B_SZ*B_SZ;
  948. #else
  949. mults=B_SZ*B_SZ*4*B_SZ*B_SZ;
  950. #endif
  951. drops=0;
  952. if(step>INIT_STEPS){
  953. #if !MAKE_SPARSE
  954. break;
  955. #endif
  956. mults-=DROPS_PER_STEP*(step-INIT_STEPS);
  957. drops=DROPS_PER_STEP;
  958. }
  959. printf("Starting Step %02i (%i mults / block)\n",step,mults);
  960. for(j=0;j<OD_INTRA_NMODES;j++){
  961. int *mode_mask;
  962. /* Combine the gathered prediction data. */
  963. for(i=1;i<NUM_PROCS;i++){
  964. od_covmat_combine(&cls[0].pd[j],&cls[i].pd[j]);
  965. }
  966. prob_ctx_load(&prob,&cls[0].pd[j]);
  967. /* Update predictor model based on mults and drops. */
  968. #if PRINT_DROPS
  969. if(drops>0){
  970. printf("Mode %i\n",j);
  971. fflush(stdout);
  972. }
  973. #endif
  974. mode_mask=&mask[B_SZ*B_SZ*4*B_SZ*B_SZ*j];
  975. comp_predictors(&prob,sol,drops,mode_mask);
  976. /* Compute residual covariance for each mode. */
  977. prob_ctx_comp_error(&prob,&cls[0].pd[j],sol->beta_1);
  978. #if ZERO_MEAN
  979. {
  980. double mean[B_SZ*B_SZ];
  981. for(i=0;i<B_SZ*B_SZ;i++){
  982. mean[i]=0;
  983. }
  984. od_covmat_update(&ete,prob.ete,mean,cls[0].pd[j].w);
  985. }
  986. #else
  987. od_covmat_update(&ete,prob.ete,sol->beta_0,cls[0].pd[j].w);
  988. #endif
  989. #if !POOLED_COV
  990. od_covmat_correct(&ete);
  991. update_diversity(ete.cov,b[j],OD_SCALE[B_SZ_LOG-OD_LOG_BSIZE0]);
  992. od_covmat_reset(&ete);
  993. #endif
  994. #if SUBTRACT_DC
  995. for(i=0;i<4;i++){
  996. OD_ASSERT(mode_mask[i*B_SZ*B_SZ]);
  997. sol->beta_1[i*B_SZ*B_SZ]+=0.25;
  998. }
  999. #endif
  1000. update_predictors(j,sol->beta_0,sol->beta_1,mode_mask);
  1001. }
  1002. #if POOLED_COV
  1003. od_covmat_correct(&ete);
  1004. for(j=0;j<OD_INTRA_NMODES;j++){
  1005. update_diversity(ete.cov,b[j],OD_SCALE[B_SZ_LOG-OD_LOG_BSIZE0]);
  1006. }
  1007. od_covmat_reset(&ete);
  1008. #endif
  1009. #if PRINT_BETAS
  1010. fprintf(stderr,"Finished Step %02i\n",step);
  1011. print_predictors(stderr);
  1012. #if POOLED_COV
  1013. print_diversity(stderr,b[0],OD_SCALE[B_SZ_LOG-OD_LOG_BSIZE0]);
  1014. #endif
  1015. #endif
  1016. /* Reset the prediction data. */
  1017. for(i=0;i<NUM_PROCS;i++){
  1018. classify_ctx_reset(&cls[i]);
  1019. }
  1020. /* Reclassify based on the new model. */
  1021. ne_apply_to_blocks(cls,sizeof(*cls),0x1,PADDING,pred_start,NPRED,PRED,
  1022. pred_finish,_argc,_argv);
  1023. od_gettime(&stop);
  1024. printf("Finished Step %02i (%lims)\n",step,timing(&start,&stop));
  1025. start=stop;
  1026. /* Combine the gathered intra stats. */
  1027. for(i=1;i<NUM_PROCS;i++){
  1028. intra_stats_combine(&cls[0].gb,&cls[i].gb);
  1029. cls[0].bits+=cls[i].bits;
  1030. }
  1031. printf("Step %02i Total Bits %-24.18G\n",step,cls[0].bits);
  1032. intra_stats_correct(&cls[0].gb);
  1033. intra_stats_print(&cls[0].gb,"Daala Intra Predictors",
  1034. OD_SCALE[B_SZ_LOG-OD_LOG_BSIZE0]);
  1035. if (mults==4*B_SZ*B_SZ) {
  1036. break;
  1037. }
  1038. }
  1039. prob_ctx_clear(&prob);
  1040. for(i=0;i<NUM_PROCS;i++){
  1041. solve_ctx_clear(&sol[i]);
  1042. }
  1043. od_covmat_clear(&ete);
  1044. free(mask);
  1045. }
  1046. for(i=0;i<NUM_PROCS;i++){
  1047. classify_ctx_clear(&cls[i]);
  1048. }
  1049. free(cls);
  1050. od_intra_clear();
  1051. return EXIT_SUCCESS;
  1052. }