stats_tools.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*Daala video codec
  2. Copyright (c) 2013 Daala project contributors. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. - Redistributions of source code must retain the above copyright notice, this
  6. list of conditions and the following disclaimer.
  7. - Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
  11. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  12. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  13. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  14. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  15. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  16. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  17. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  18. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
  20. #include <omp.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "stats_tools.h"
  24. #include "od_defs.h"
  25. #include "od_filter.h"
  26. #include "od_intra.h"
  27. #include "../src/dct.h"
  28. #include "../src/intra.h"
  29. #define PRINT_SCALE (0)
  30. void mode_data_init(mode_data *_md){
  31. int i;
  32. _md->n=0;
  33. _md->mean=0;
  34. _md->var=0;
  35. for(i=0;i<B_SZ*B_SZ;i++){
  36. _md->satd_avg[i]=0;
  37. }
  38. od_covmat_init(&_md->ref,B_SZ*B_SZ);
  39. od_covmat_init(&_md->res,B_SZ*B_SZ);
  40. }
  41. void mode_data_clear(mode_data *_md){
  42. od_covmat_clear(&_md->ref);
  43. od_covmat_clear(&_md->res);
  44. }
  45. void mode_data_reset(mode_data *_md){
  46. int i;
  47. _md->n=0;
  48. _md->mean=0;
  49. _md->var=0;
  50. for(i=0;i<B_SZ*B_SZ;i++){
  51. _md->satd_avg[i]=0;
  52. }
  53. od_covmat_reset(&_md->ref);
  54. od_covmat_reset(&_md->res);
  55. }
  56. /* update the input mean and variance */
  57. void mode_data_add_input(mode_data *_md,const unsigned char *_data,int _stride){
  58. int n;
  59. int i;
  60. int j;
  61. n=_md->n*B_SZ*B_SZ;
  62. for(j=0;j<B_SZ;j++){
  63. for(i=0;i<B_SZ;i++){
  64. double delta;
  65. double s;
  66. n++;
  67. s=1.0/n;
  68. delta=_data[_stride*j+i]*INPUT_SCALE-_md->mean;
  69. _md->mean+=delta*s;
  70. _md->var+=delta*delta*(n-1)*s;
  71. }
  72. }
  73. _md->n++;
  74. }
  75. void mode_data_add_block(mode_data *_md,const od_coeff *_block,int _stride,
  76. int _ref){
  77. int j;
  78. int i;
  79. double buf[B_SZ*B_SZ];
  80. for(j=0;j<B_SZ;j++){
  81. for(i=0;i<B_SZ;i++){
  82. buf[B_SZ*j+i]=_block[_stride*j+i];
  83. }
  84. }
  85. if(_ref){
  86. od_covmat_add(&_md->ref,buf,1);
  87. }
  88. else{
  89. od_covmat_add(&_md->res,buf,1);
  90. }
  91. }
  92. void mode_data_combine(mode_data *_a,const mode_data *_b){
  93. double s;
  94. double delta;
  95. int i;
  96. if(_b->n==0){
  97. return;
  98. }
  99. s=((double)_b->n)/(_a->n+_b->n);
  100. delta=_b->mean-_a->mean;
  101. _a->mean+=delta*s;
  102. for(i=0;i<B_SZ*B_SZ;i++){
  103. _a->satd_avg[i]+=(_b->satd_avg[i]-_a->satd_avg[i])*s;
  104. }
  105. s*=_a->n;
  106. _a->var+=_b->var+delta*s;
  107. od_covmat_combine(&_a->ref,&_b->ref);
  108. od_covmat_combine(&_a->res,&_b->res);
  109. _a->n+=_b->n;
  110. }
  111. void mode_data_correct(mode_data *_md){
  112. _md->var/=_md->n*B_SZ*B_SZ;
  113. od_covmat_correct(&_md->ref);
  114. od_covmat_correct(&_md->res);
  115. }
  116. void mode_data_print(mode_data *_md,const char *_label,double *_scale){
  117. double cg_ref;
  118. double cg_res;
  119. int v;
  120. int u;
  121. double satd_avg;
  122. double bits_avg;
  123. cg_ref=10*log10(_md->var);
  124. cg_res=10*log10(_md->var);
  125. satd_avg=0;
  126. bits_avg=0;
  127. for(v=0;v<B_SZ;v++){
  128. for(u=0;u<B_SZ;u++){
  129. int i;
  130. int ii;
  131. double b;
  132. i=B_SZ*v+u;
  133. ii=B_SZ*B_SZ*i+i;
  134. cg_ref-=10*log10(_md->ref.cov[ii]*_scale[v]*_scale[u])/(B_SZ*B_SZ);
  135. cg_res-=10*log10(_md->res.cov[ii]*_scale[v]*_scale[u])/(B_SZ*B_SZ);
  136. satd_avg+=sqrt(_scale[v]*_scale[u])*_md->satd_avg[i];
  137. b=sqrt(_scale[v]*_scale[u]*_md->res.cov[ii]/2);
  138. bits_avg+=1+OD_LOG2(b)+M_LOG2E/b*_md->satd_avg[i];
  139. }
  140. }
  141. printf("%s Blocks %5i SATD %G Bits %G Mean %G Var %G CgRef %G CgRes %G Pg %G\n",
  142. _label,_md->n,satd_avg,bits_avg,_md->mean,_md->var,cg_ref,cg_res,cg_res-cg_ref);
  143. }
  144. void mode_data_params(mode_data *_this,double _b[B_SZ*B_SZ],double *_scale){
  145. int v;
  146. int u;
  147. int i;
  148. int ii;
  149. for(v=0;v<B_SZ;v++){
  150. for(u=0;u<B_SZ;u++){
  151. i=(v*B_SZ+u);
  152. ii=B_SZ*B_SZ*i+i;
  153. _b[i]=sqrt(_scale[v]*_scale[u]*_this->res.cov[ii]/2);
  154. }
  155. }
  156. }
  157. void intra_stats_init(intra_stats *_this){
  158. int mode;
  159. mode_data_init(&_this->fr);
  160. for(mode=0;mode<OD_INTRA_NMODES;mode++){
  161. mode_data_init(&_this->md[mode]);
  162. }
  163. }
  164. void intra_stats_clear(intra_stats *_this){
  165. int i;
  166. mode_data_clear(&_this->fr);
  167. for(i=0;i<OD_INTRA_NMODES;i++){
  168. mode_data_clear(&_this->md[i]);
  169. }
  170. }
  171. void intra_stats_reset(intra_stats *_this){
  172. int i;
  173. mode_data_reset(&_this->fr);
  174. for(i=0;i<OD_INTRA_NMODES;i++){
  175. mode_data_reset(&_this->md[i]);
  176. }
  177. }
  178. void intra_stats_update(intra_stats *_this,const unsigned char *_data,
  179. int _stride,int _mode,const od_coeff *_ref,int _ref_stride,
  180. const double *_res,int _res_stride){
  181. mode_data *fr;
  182. mode_data *md;
  183. int j;
  184. int i;
  185. double buf[B_SZ*B_SZ];
  186. fr=&_this->fr;
  187. md=&_this->md[_mode];
  188. /* Update the input mean and variance. */
  189. mode_data_add_input(fr,_data,_stride);
  190. mode_data_add_input(md,_data,_stride);
  191. /* Update the reference mean and covariance. */
  192. for(j=0;j<B_SZ;j++){
  193. for(i=0;i<B_SZ;i++){
  194. buf[B_SZ*j+i]=_ref[_ref_stride*j+i];
  195. }
  196. }
  197. od_covmat_add(&fr->ref,buf,1);
  198. od_covmat_add(&md->ref,buf,1);
  199. /* Update the residual mean and covariance. */
  200. for(j=0;j<B_SZ;j++){
  201. for(i=0;i<B_SZ;i++){
  202. buf[B_SZ*j+i]=_res[_res_stride*j+i];
  203. }
  204. }
  205. od_covmat_add(&fr->res,buf,1);
  206. od_covmat_add(&md->res,buf,1);
  207. /* Update the average SATD. */
  208. for(j=0;j<B_SZ;j++){
  209. for(i=0;i<B_SZ;i++){
  210. double satd;
  211. satd=abs(buf[B_SZ*j+i]);
  212. fr->satd_avg[B_SZ*j+i]+=(satd-fr->satd_avg[B_SZ*j+i])/fr->n;
  213. md->satd_avg[B_SZ*j+i]+=(satd-md->satd_avg[B_SZ*j+i])/md->n;
  214. }
  215. }
  216. }
  217. void intra_stats_correct(intra_stats *_this){
  218. int mode;
  219. mode_data_correct(&_this->fr);
  220. for(mode=0;mode<OD_INTRA_NMODES;mode++){
  221. mode_data_correct(&_this->md[mode]);
  222. }
  223. }
  224. void intra_stats_print(intra_stats *_this,const char *_label,
  225. double *_scale){
  226. int mode;
  227. printf("%s\n",_label);
  228. for(mode=0;mode<OD_INTRA_NMODES;mode++){
  229. char label[16];
  230. sprintf(label,"Mode %i",mode);
  231. mode_data_print(&_this->md[mode],label,_scale);
  232. }
  233. mode_data_print(&_this->fr,"Pooled",_scale);
  234. }
  235. void intra_stats_combine(intra_stats *_this,const intra_stats *_that){
  236. int mode;
  237. mode_data_combine(&_this->fr,&_that->fr);
  238. for(mode=0;mode<OD_INTRA_NMODES;mode++){
  239. mode_data_combine(&_this->md[mode],&_that->md[mode]);
  240. }
  241. }
  242. /* compute the scale factors for the DCT and TDLT transforms */
  243. double VP8_SCALE[B_SZ];
  244. double OD_SCALE[B_SZ];
  245. #define SCALE_BITS (14)
  246. void vp8_scale_init(double _vp8_scale[B_SZ]){
  247. int j;
  248. int i;
  249. od_coeff buf[B_SZ];
  250. for(i=0;i<B_SZ;i++){
  251. for(j=0;j<B_SZ;j++){
  252. buf[j]=i!=j?0:(1<<SCALE_BITS);
  253. }
  254. #if B_SZ_LOG>=OD_LOG_BSIZE0&&B_SZ_LOG<OD_LOG_BSIZE0+OD_NBSIZES
  255. (*OD_IDCT_1D[B_SZ_LOG-OD_LOG_BSIZE0])(buf,1,buf);
  256. #else
  257. # error "Need an iDCT implementation for this block size."
  258. #endif
  259. _vp8_scale[i]=0;
  260. for(j=0;j<B_SZ;j++){
  261. double c=((double)buf[j])/(1<<SCALE_BITS);
  262. _vp8_scale[i]+=c*c;
  263. }
  264. #if PRINT_SCALE
  265. printf("%s%- 24.18G",i==0?"":" ",_vp8_scale[i]);
  266. #endif
  267. }
  268. #if PRINT_SCALE
  269. printf("\n");
  270. #endif
  271. }
  272. #define APPLY_PREFILTER (1)
  273. #define APPLY_POSTFILTER (1)
  274. void od_scale_init(double _od_scale[B_SZ]){
  275. int i;
  276. int j;
  277. od_coeff buf[2*B_SZ];
  278. for(i=0;i<B_SZ;i++){
  279. for(j=0;j<2*B_SZ;j++){
  280. buf[j]=(B_SZ>>1)+i!=j?0:(1<<SCALE_BITS);
  281. }
  282. #if B_SZ_LOG>=OD_LOG_BSIZE0&&B_SZ_LOG<OD_LOG_BSIZE0+OD_NBSIZES
  283. (*OD_IDCT_1D[B_SZ_LOG-OD_LOG_BSIZE0])(&buf[B_SZ>>1],1,&buf[B_SZ>>1]);
  284. #else
  285. # error "Need an iDCT implementation for this block size."
  286. #endif
  287. #if APPLY_POSTFILTER
  288. #if B_SZ_LOG>=OD_LOG_BSIZE0&&B_SZ_LOG<OD_LOG_BSIZE0+OD_NBSIZES
  289. (*NE_POST_FILTER[B_SZ_LOG-OD_LOG_BSIZE0])(buf,buf);
  290. (*NE_POST_FILTER[B_SZ_LOG-OD_LOG_BSIZE0])(&buf[B_SZ],&buf[B_SZ]);
  291. #else
  292. # error "Need a postfilter implementation for this block size."
  293. #endif
  294. #endif
  295. _od_scale[i]=0;
  296. for(j=0;j<2*B_SZ;j++){
  297. double c=((double)buf[j])/(1<<SCALE_BITS);
  298. _od_scale[i]+=c*c;
  299. }
  300. #if PRINT_SCALE
  301. printf("%s%- 24.18G",i==0?"":" ",_od_scale[i]);
  302. #endif
  303. }
  304. #if PRINT_SCALE
  305. printf("\n");
  306. #endif
  307. }
  308. #define SCALE_SATD (1)
  309. /* find the best vp8 mode */
  310. int vp8_select_mode(const unsigned char *_data,int _stride,double *_weight){
  311. double best_satd;
  312. double next_best_satd;
  313. int mode;
  314. int best_mode;
  315. best_mode=0;
  316. best_satd=UINT_MAX;
  317. next_best_satd=best_satd;
  318. for(mode=0;mode<OD_INTRA_NMODES;mode++){
  319. unsigned char block[B_SZ*B_SZ];
  320. od_coeff buf[B_SZ*B_SZ];
  321. int j;
  322. int i;
  323. double satd;
  324. memset(block,0,B_SZ*B_SZ);
  325. vp8_intra_predict(block,B_SZ,_data,_stride,mode);
  326. for(j=0;j<B_SZ;j++){
  327. for(i=0;i<B_SZ;i++){
  328. buf[B_SZ*j+i]=block[B_SZ*j+i]-_data[_stride*j+i];
  329. }
  330. }
  331. #if B_SZ_LOG>=OD_LOG_BSIZE0&&B_SZ_LOG<OD_LOG_BSIZE0+OD_NBSIZES
  332. (*OD_FDCT_2D[B_SZ_LOG-OD_LOG_BSIZE0])(buf,B_SZ,buf,B_SZ);
  333. #else
  334. # error "Need an fDCT implementation for this block size."
  335. #endif
  336. satd=0;
  337. for(j=0;j<B_SZ;j++){
  338. for(i=0;i<B_SZ;i++){
  339. #if SCALE_SATD
  340. satd+=sqrt(VP8_SCALE[j]*VP8_SCALE[i])*abs(buf[B_SZ*j+i]);
  341. #else
  342. satd+=abs(buf[B_SZ*j+i]);
  343. #endif
  344. }
  345. }
  346. if(satd<best_satd){
  347. next_best_satd=best_satd;
  348. best_satd=satd;
  349. best_mode=mode;
  350. }
  351. else{
  352. if(satd<next_best_satd){
  353. next_best_satd=satd;
  354. }
  355. }
  356. }
  357. if(_weight!=NULL){
  358. *_weight=best_mode!=0?next_best_satd-best_satd:1;
  359. }
  360. return best_mode;
  361. }
  362. int od_select_mode_bits(const od_coeff *_block,int _stride,double *_weight,
  363. double _b[OD_INTRA_NMODES][B_SZ*B_SZ]){
  364. int best_mode;
  365. double best_bits;
  366. double next_best_bits;
  367. int mode;
  368. best_mode=0;
  369. best_bits=UINT_MAX;
  370. next_best_bits=best_bits;
  371. for(mode=0;mode<OD_INTRA_NMODES;mode++){
  372. double p[B_SZ*B_SZ];
  373. double bits;
  374. int j;
  375. int i;
  376. #if B_SZ_LOG>=OD_LOG_BSIZE0&&B_SZ_LOG<OD_LOG_BSIZE0+OD_NBSIZES
  377. #if 0
  378. (*OD_INTRA_MULT[B_SZ_LOG-OD_LOG_BSIZE0])(p,_block,_stride,mode);
  379. #else
  380. (*NE_INTRA_MULT[B_SZ_LOG-OD_LOG_BSIZE0])(p,B_SZ,_block,_stride,mode);
  381. #endif
  382. #else
  383. # error "Need a predictor implementation for this block size."
  384. #endif
  385. bits=0;
  386. for(j=0;j<B_SZ;j++){
  387. for(i=0;i<B_SZ;i++){
  388. double res;
  389. res=sqrt(OD_SCALE[j]*OD_SCALE[i])*
  390. abs(_block[_stride*j+i]-(od_coeff)floor(p[B_SZ*j+i]+0.5));
  391. bits+=1+OD_LOG2(_b[mode][j*B_SZ+i])+M_LOG2E/_b[mode][j*B_SZ+i]*res;
  392. }
  393. }
  394. if(bits<best_bits){
  395. next_best_bits=best_bits;
  396. best_bits=bits;
  397. best_mode=mode;
  398. }
  399. else{
  400. if(bits<next_best_bits){
  401. next_best_bits=bits;
  402. }
  403. }
  404. }
  405. if(_weight!=NULL){
  406. *_weight=best_mode!=0?next_best_bits-best_bits:1;
  407. }
  408. return best_mode;
  409. }
  410. int od_select_mode_satd(const od_coeff *_block,int _stride,double *_weight){
  411. int best_mode;
  412. double best_satd;
  413. double next_best_satd;
  414. int mode;
  415. best_mode=0;
  416. best_satd=UINT_MAX;
  417. next_best_satd=best_satd;
  418. for(mode=0;mode<OD_INTRA_NMODES;mode++){
  419. double p[B_SZ*B_SZ];
  420. double satd;
  421. int j;
  422. int i;
  423. #if B_SZ_LOG>=OD_LOG_BSIZE0&&B_SZ_LOG<OD_LOG_BSIZE0+OD_NBSIZES
  424. #if 0
  425. (*OD_INTRA_MULT[B_SZ_LOG-OD_LOG_BSIZE0])(p,_block,_stride,mode);
  426. #else
  427. (*NE_INTRA_MULT[B_SZ_LOG-OD_LOG_BSIZE0])(p,B_SZ,_block,_stride,mode);
  428. #endif
  429. #else
  430. # error "Need a predictor implementation for this block size."
  431. #endif
  432. satd=0;
  433. for(j=0;j<B_SZ;j++){
  434. for(i=0;i<B_SZ;i++){
  435. #if SCALE_SATD
  436. satd+=sqrt(OD_SCALE[j]*OD_SCALE[i])*
  437. abs(_block[_stride*j+i]-(od_coeff)floor(p[B_SZ*j+i]+0.5));
  438. #else
  439. satd+=abs(_block[_stride*j+i]-(od_coeff)floor(p[B_SZ*j+i]+0.5));
  440. #endif
  441. }
  442. }
  443. if(satd<best_satd){
  444. next_best_satd=best_satd;
  445. best_mode=mode;
  446. best_satd=satd;
  447. }
  448. else{
  449. if(satd<next_best_satd){
  450. next_best_satd=satd;
  451. }
  452. }
  453. }
  454. if(_weight!=NULL){
  455. *_weight=best_mode!=0?next_best_satd-best_satd:1;
  456. }
  457. return best_mode;
  458. }
  459. int ne_apply_to_blocks(void *_ctx,int _ctx_sz,int _plmask,int _padding,
  460. plane_start_func _start,int _nfuncs,const block_func *_funcs,
  461. plane_finish_func _finish,int _argc,const char *_argv[]){
  462. int ai;
  463. #pragma omp parallel for schedule(dynamic)
  464. for(ai=1;ai<_argc;ai++){
  465. FILE *fin;
  466. video_input vid;
  467. th_info ti;
  468. th_ycbcr_buffer ycbcr;
  469. int pli;
  470. int tid;
  471. unsigned char *ctx;
  472. fin=fopen(_argv[ai],"rb");
  473. if(fin==NULL){
  474. fprintf(stderr,"Could not open '%s' for reading.\n",_argv[ai]);
  475. continue;
  476. }
  477. if(video_input_open(&vid,fin)<0){
  478. fprintf(stderr,"Error reading video info from '%s'.\n",_argv[ai]);
  479. continue;
  480. }
  481. video_input_get_info(&vid,&ti);
  482. if(video_input_fetch_frame(&vid,ycbcr,NULL)<0){
  483. fprintf(stderr,"Error reading first frame from '%s'.\n",_argv[ai]);
  484. continue;
  485. }
  486. tid=omp_get_thread_num();
  487. ctx=((unsigned char *)_ctx)+tid*_ctx_sz;
  488. for(pli=0;pli<3;pli++){
  489. if(_plmask&1<<pli){
  490. int x0;
  491. int y0;
  492. int nxblocks;
  493. int nyblocks;
  494. get_intra_dims(&ti,pli,_padding,&x0,&y0,&nxblocks,&nyblocks);
  495. if(_start!=NULL){
  496. (*_start)(ctx,_argv[ai],&ti,pli,nxblocks,nyblocks);
  497. }
  498. if(_funcs!=NULL){
  499. int f;
  500. for(f=0;f<_nfuncs;f++){
  501. if(_funcs[f]!=NULL){
  502. const unsigned char *data;
  503. int stride;
  504. int bj;
  505. int bi;
  506. data=ycbcr[pli].data;
  507. stride=ycbcr[pli].stride;
  508. for(bj=0;bj<nyblocks;bj++){
  509. int y;
  510. y=y0+B_SZ*bj;
  511. for(bi=0;bi<nxblocks;bi++){
  512. int x;
  513. x=x0+B_SZ*bi;
  514. (*_funcs[f])(ctx,&data[stride*y+x],stride,bi,bj);
  515. }
  516. }
  517. }
  518. }
  519. }
  520. if(_finish!=NULL){
  521. (*_finish)(ctx);
  522. }
  523. }
  524. }
  525. video_input_close(&vid);
  526. }
  527. return EXIT_SUCCESS;
  528. }