intra_stats.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #ifdef HAVE_CONFIG_H
  2. #include "config.h"
  3. #endif
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "od_defs.h"
  7. #include "od_filter.h"
  8. #include "od_intra.h"
  9. #include "image_tools.h"
  10. #include "stats_tools.h"
  11. #include "../src/dct.h"
  12. #include "../src/intra.h"
  13. #define WRITE_IMAGES (0)
  14. #define PRINT_PROGRESS (0)
  15. #define PRINT_BLOCKS (0)
  16. typedef struct intra_stats_ctx intra_stats_ctx;
  17. struct intra_stats_ctx{
  18. int n;
  19. intra_stats gb_vp8;
  20. intra_stats gb_od;
  21. intra_stats st_vp8;
  22. intra_stats st_od;
  23. image_data img;
  24. #if WRITE_IMAGES
  25. image_files files_vp8;
  26. image_files files_od;
  27. #endif
  28. };
  29. static void intra_stats_ctx_init(intra_stats_ctx *_this){
  30. _this->n=0;
  31. intra_stats_init(&_this->gb_vp8,B_SZ_LOG);
  32. intra_stats_init(&_this->gb_od,B_SZ_LOG);
  33. intra_stats_init(&_this->st_vp8,B_SZ_LOG);
  34. intra_stats_init(&_this->st_od,B_SZ_LOG);
  35. }
  36. static void intra_stats_ctx_clear(intra_stats_ctx *_this){
  37. intra_stats_clear(&_this->gb_vp8);
  38. intra_stats_clear(&_this->gb_od);
  39. intra_stats_clear(&_this->st_vp8);
  40. intra_stats_clear(&_this->st_od);
  41. }
  42. static void intra_stats_ctx_set_image(intra_stats_ctx *_this,const char *_name,
  43. int _nxblocks,int _nyblocks){
  44. _this->n++;
  45. intra_stats_reset(&_this->st_vp8);
  46. intra_stats_reset(&_this->st_od);
  47. image_data_init(&_this->img,_name,B_SZ_LOG,_nxblocks,_nyblocks);
  48. #if WRITE_IMAGES
  49. image_files_init(&_this->files_vp8,_nxblocks,_nyblocks);
  50. image_files_init(&_this->files_od,_nxblocks,_nyblocks);
  51. #endif
  52. }
  53. static void intra_stats_ctx_clear_image(intra_stats_ctx *_this){
  54. image_data_clear(&_this->img);
  55. #if WRITE_IMAGES
  56. image_files_clear(&_this->files_vp8);
  57. image_files_clear(&_this->files_od);
  58. #endif
  59. }
  60. static void intra_stats_ctx_combine(intra_stats_ctx *_a,intra_stats_ctx *_b){
  61. if(_b->n==0){
  62. return;
  63. }
  64. intra_stats_combine(&_a->gb_vp8,&_b->gb_vp8);
  65. intra_stats_combine(&_a->gb_od,&_b->gb_od);
  66. _a->n+=_b->n;
  67. }
  68. static void vp8_stats_block(intra_stats_ctx *_ctx,const unsigned char *_data,
  69. int _stride,int _bi,int _bj,int _mode,const unsigned char *_pred){
  70. int j;
  71. int i;
  72. od_coeff ref[B_SZ*B_SZ];
  73. od_coeff buf[B_SZ*B_SZ];
  74. double res[B_SZ*B_SZ];
  75. (void)_bi;
  76. (void)_bj;
  77. /* Compute reference transform coefficients. */
  78. for(j=0;j<B_SZ;j++){
  79. for(i=0;i<B_SZ;i++){
  80. ref[B_SZ*j+i]=(_data[_stride*j+i]-128)*INPUT_SCALE;
  81. }
  82. }
  83. #if B_SZ_LOG>=OD_LOG_BSIZE0&&B_SZ_LOG<OD_LOG_BSIZE0+OD_NBSIZES
  84. (*OD_FDCT_2D_C[B_SZ_LOG-OD_LOG_BSIZE0])(ref,B_SZ,ref,B_SZ);
  85. #else
  86. # error "Need an fDCT implementation for this block size."
  87. #endif
  88. /* Compute residual transform coefficients. */
  89. for(j=0;j<B_SZ;j++){
  90. for(i=0;i<B_SZ;i++){
  91. buf[B_SZ*j+i]=(_data[_stride*j+i]-_pred[B_SZ*j+i])*INPUT_SCALE;
  92. }
  93. }
  94. #if B_SZ_LOG>=OD_LOG_BSIZE0&&B_SZ_LOG<OD_LOG_BSIZE0+OD_NBSIZES
  95. (*OD_FDCT_2D_C[B_SZ_LOG-OD_LOG_BSIZE0])(buf,B_SZ,buf,B_SZ);
  96. #else
  97. # error "Need an fDCT implementation for this block size."
  98. #endif
  99. for(j=0;j<B_SZ;j++){
  100. for(i=0;i<B_SZ;i++){
  101. res[B_SZ*j+i]=buf[B_SZ*j+i];
  102. }
  103. }
  104. intra_stats_update(&_ctx->st_vp8,_data,_stride,_mode,ref,B_SZ,res,B_SZ);
  105. }
  106. #if WRITE_IMAGES
  107. static void vp8_files_block(intra_stats_ctx *_ctx,const unsigned char *_data,
  108. int _stride,int _bi,int _bj,int _mode,const unsigned char *_pred){
  109. int j;
  110. int i;
  111. unsigned char res[B_SZ*B_SZ];
  112. image_draw_block(&_ctx->files_vp8.raw,B_SZ*_bi,B_SZ*_bj,_data,_stride);
  113. od_rgba16_image_draw_point(&_ctx->files_vp8.map,_bi,_bj,COLORS[_mode]);
  114. image_draw_block(&_ctx->files_vp8.pred,B_SZ*_bi,B_SZ*_bj,_pred,B_SZ);
  115. for(j=0;j<B_SZ;j++){
  116. for(i=0;i<B_SZ;i++){
  117. res[j*B_SZ+i]=OD_CLAMP255(_data[_stride*j+i]-_pred[B_SZ*j+i]+128);
  118. }
  119. }
  120. image_draw_block(&_ctx->files_vp8.res,B_SZ*_bi,B_SZ*_bj,res,B_SZ);
  121. }
  122. #endif
  123. #if MASK_BLOCKS
  124. static void od_mask_block(void *_ctx,const unsigned char *_data,int _stride,
  125. int _bi,int _bj){
  126. #if PRINT_PROGRESS
  127. if(_bi==0&&_bj==0){
  128. print_progress(stdout,"od_mask_block");
  129. }
  130. #endif
  131. if(_bi==0&&_bj==0){
  132. intra_stats_ctx *ctx;
  133. ctx=(intra_stats_ctx *)_ctx;
  134. image_data_mask(&ctx->img,_data,_stride);
  135. }
  136. }
  137. #endif
  138. static void vp8_block(void *_ctx,const unsigned char *_data,int _stride,
  139. int _bi,int _bj){
  140. intra_stats_ctx *ctx;
  141. int mode;
  142. unsigned char pred[B_SZ*B_SZ];
  143. ctx=(intra_stats_ctx *)_ctx;
  144. mode=vp8_select_mode(_data,_stride,NULL);
  145. memset(pred,0,B_SZ*B_SZ);
  146. vp8_intra_predict(pred,B_SZ,_data,_stride,mode);
  147. #if MASK_BLOCKS
  148. if(!ctx->img.mask[ctx->img.nxblocks*_bj+_bi]){
  149. int j;
  150. int i;
  151. for(j=0;j<B_SZ;j++){
  152. for(i=0;i<B_SZ;i++){
  153. pred[j*B_SZ+i]=_data[_stride*j+i];
  154. }
  155. }
  156. }
  157. #endif
  158. #if WRITE_IMAGES
  159. vp8_files_block(ctx,_data,_stride,_bi,_bj,mode,pred);
  160. #endif
  161. ctx->img.mode[ctx->img.nxblocks*_bj+_bi]=mode;
  162. #if MASK_BLOCKS
  163. if(!ctx->img.mask[ctx->img.nxblocks*_bj+_bi]){
  164. return;
  165. }
  166. #endif
  167. vp8_stats_block(ctx,_data,_stride,_bi,_bj,mode,pred);
  168. }
  169. static void od_pre_block(void *_ctx,const unsigned char *_data,int _stride,
  170. int _bi,int _bj){
  171. intra_stats_ctx *ctx;
  172. (void)_data;
  173. (void)_stride;
  174. #if PRINT_PROGRESS
  175. if(_bi==0&&_bj==0){
  176. fprintf(stdout,"in od_pre_block\n");
  177. }
  178. #endif
  179. ctx=(intra_stats_ctx *)_ctx;
  180. image_data_pre_block(&ctx->img,_data,_stride,_bi,_bj);
  181. }
  182. static void od_fdct_block(void *_ctx,const unsigned char *_data,int _stride,
  183. int _bi,int _bj){
  184. intra_stats_ctx *ctx;
  185. (void)_data;
  186. (void)_stride;
  187. #if PRINT_PROGRESS
  188. if(_bi==0&&_bj==0){
  189. fprintf(stdout,"in od_fdct_block\n");
  190. }
  191. #endif
  192. ctx=(intra_stats_ctx *)_ctx;
  193. image_data_fdct_block(&ctx->img,_bi,_bj);
  194. }
  195. #if TF_BLOCKS
  196. static void od_tf_block(void *_ctx,const unsigned char *_data,int _stride,
  197. int _bi,int _bj){
  198. intra_stats_ctx *ctx;
  199. (void)_data;
  200. (void)_stride;
  201. #if PRINT_PROGRESS
  202. if(_bi==0&&_bj==0){
  203. fprintf(stdout,"in od_tf_block\n");
  204. }
  205. #endif
  206. ctx=(intra_stats_ctx *)_ctx;
  207. image_data_tf_block(&ctx->img,_bi,_bj);
  208. }
  209. #endif
  210. static void od_mode_block(void *_ctx,const unsigned char *_data,int _stride,
  211. int _bi,int _bj){
  212. intra_stats_ctx *ctx;
  213. od_coeff block[5*B_SZ*B_SZ];
  214. (void)_data;
  215. (void)_stride;
  216. #if PRINT_PROGRESS
  217. if(_bi==0&&_bj==0){
  218. fprintf(stdout,"in od_mode_block\n");
  219. }
  220. #endif
  221. ctx=(intra_stats_ctx *)_ctx;
  222. image_data_load_block(&ctx->img,_bi,_bj,block);
  223. ctx->img.mode[ctx->img.nxblocks*_bj+_bi]=
  224. od_select_mode_satd(block,NULL,ctx->img.b_sz_log);
  225. }
  226. #if PRINT_BLOCKS
  227. static void od_print_block(void *_ctx,const unsigned char *_data,int _stride,
  228. int _bi,int _bj){
  229. intra_stats_ctx *ctx;
  230. (void)_data;
  231. (void)_stride;
  232. #if PRINT_PROGRESS
  233. if(_bi==0&&_bj==0){
  234. print_progress(stdout,"od_print_block");
  235. }
  236. #endif
  237. ctx=(intra_stats_ctx *)_ctx;
  238. image_data_print_block(&ctx->img,_bi,_bj,stderr);
  239. }
  240. #endif
  241. static void od_pred_block(void *_ctx,const unsigned char *_data,int _stride,
  242. int _bi,int _bj){
  243. intra_stats_ctx *ctx;
  244. (void)_data;
  245. (void)_stride;
  246. #if PRINT_PROGRESS
  247. if(_bi==0&&_bj==0){
  248. fprintf(stdout,"in od_pred_block\n");
  249. }
  250. #endif
  251. ctx=(intra_stats_ctx *)_ctx;
  252. image_data_pred_block(&ctx->img,_bi,_bj);
  253. }
  254. #if WRITE_IMAGES
  255. static void od_idct_block(void *_ctx,const unsigned char *_data,int _stride,
  256. int _bi,int _bj){
  257. intra_stats_ctx *ctx;
  258. #if PRINT_PROGRESS
  259. if(_bi==0&&_bj==0){
  260. fprintf(stdout,"in od_idct_block\n");
  261. }
  262. #endif
  263. ctx=(intra_stats_ctx *)_ctx;
  264. image_data_idct_block(&ctx->img,_bi,_bj);
  265. }
  266. static void od_post_block(void *_ctx,const unsigned char *_data,int _stride,
  267. int _bi,int _bj){
  268. intra_stats_ctx *ctx;
  269. #if PRINT_PROGRESS
  270. if(_bi==0&&_bj==0){
  271. fprintf(stdout,"in od_post_block\n");
  272. }
  273. #endif
  274. ctx=(intra_stats_ctx *)_ctx;
  275. image_data_post_block(&ctx->img,_bi,_bj);
  276. }
  277. #endif
  278. static void od_stats_block(void *_ctx,const unsigned char *_data,int _stride,
  279. int _bi,int _bj){
  280. intra_stats_ctx *ctx;
  281. #if PRINT_PROGRESS
  282. if(_bi==0&&_bj==0){
  283. fprintf(stdout,"in od_stats_block\n");
  284. }
  285. #endif
  286. ctx=(intra_stats_ctx *)_ctx;
  287. image_data_stats_block(&ctx->img,_data,_stride,_bi,_bj,&ctx->st_od);
  288. }
  289. #if WRITE_IMAGES
  290. static void od_image_block(void *_ctx,const unsigned char *_data,int _stride,
  291. int _bi,int _bj){
  292. intra_stats_ctx *ctx;
  293. #if PRINT_PROGRESS
  294. if(_bi==0&&_bj==0){
  295. fprintf(stdout,"in od_files_block\n");
  296. }
  297. #endif
  298. ctx=(intra_stats_ctx *)_ctx;
  299. image_data_files_block(&ctx->img,_data,_stride,_bi,_bj,&ctx->files_od);
  300. }
  301. #endif
  302. static int stats_start(void *_ctx,const char *_name,
  303. const video_input_info *_info,int _pli, int _nxblocks,int _nyblocks){
  304. intra_stats_ctx *ctx;
  305. (void)_info;
  306. (void)_pli;
  307. fprintf(stdout,"%s\n",_name);
  308. ctx=(intra_stats_ctx *)_ctx;
  309. intra_stats_ctx_set_image(ctx,_name,_nxblocks,_nyblocks);
  310. return EXIT_SUCCESS;
  311. }
  312. static int stats_finish(void *_ctx){
  313. intra_stats_ctx *ctx;
  314. #if WRITE_IMAGES
  315. char name[8192];
  316. int eos;
  317. #endif
  318. ctx=(intra_stats_ctx *)_ctx;
  319. #if WRITE_IMAGES
  320. strcpy(name,ctx->img.name);
  321. eos=strlen(name)-4;
  322. sprintf(&name[eos],"%s","-vp8");
  323. image_files_write(&ctx->files_vp8,name,NULL);
  324. sprintf(&name[eos],"%s","-daala");
  325. image_files_write(&ctx->files_od,name,NULL);
  326. #endif
  327. intra_stats_combine(&ctx->gb_vp8,&ctx->st_vp8);
  328. intra_stats_correct(&ctx->st_vp8);
  329. intra_stats_print(&ctx->st_vp8,"VP8 Intra Predictors",VP8_SCALE[B_SZ_LOG-OD_LOG_BSIZE0]);
  330. intra_stats_combine(&ctx->gb_od,&ctx->st_od);
  331. intra_stats_correct(&ctx->st_od);
  332. intra_stats_print(&ctx->st_od,"Daala Intra Predictors",OD_SCALE[B_SZ_LOG-OD_LOG_BSIZE0]);
  333. intra_stats_ctx_clear_image(ctx);
  334. return EXIT_SUCCESS;
  335. }
  336. const block_func BLOCKS[]={
  337. #if MASK_BLOCKS
  338. od_mask_block,
  339. #endif
  340. vp8_block,
  341. od_pre_block,
  342. od_fdct_block,
  343. #if TF_BLOCKS
  344. od_tf_block,
  345. #endif
  346. #if PRINT_BLOCKS
  347. od_print_block,
  348. #endif
  349. od_mode_block,
  350. od_pred_block,
  351. #if WRITE_IMAGES
  352. od_idct_block,
  353. od_post_block,
  354. #endif
  355. od_stats_block,
  356. #if WRITE_IMAGES
  357. od_image_block,
  358. #endif
  359. };
  360. const int NBLOCKS=sizeof(BLOCKS)/sizeof(*BLOCKS);
  361. #define PADDING (4*B_SZ)
  362. #if PADDING<3*B_SZ
  363. # error "PADDING must be at least 3*B_SZ"
  364. #endif
  365. int main(int _argc,const char *_argv[]){
  366. intra_stats_ctx ctx[NUM_PROCS];
  367. int i;
  368. ne_filter_params_init();
  369. vp8_scale_init(VP8_SCALE[B_SZ_LOG-OD_LOG_BSIZE0],B_SZ_LOG);
  370. od_scale_init(OD_SCALE[B_SZ_LOG-OD_LOG_BSIZE0],B_SZ_LOG);
  371. #if WRITE_IMAGES
  372. intra_map_colors(COLORS,OD_INTRA_NMODES);
  373. #endif
  374. for(i=0;i<NUM_PROCS;i++){
  375. intra_stats_ctx_init(&ctx[i]);
  376. }
  377. od_intra_init();
  378. OD_OMP_SET_THREADS(NUM_PROCS);
  379. ne_apply_to_blocks(ctx,sizeof(*ctx),0x1,PADDING,stats_start,NBLOCKS,BLOCKS,
  380. stats_finish,_argc,_argv);
  381. for(i=1;i<NUM_PROCS;i++){
  382. intra_stats_ctx_combine(&ctx[0],&ctx[i]);
  383. }
  384. printf("Processed %i image(s)\n",ctx[0].n);
  385. if(ctx[0].n>0){
  386. intra_stats_correct(&ctx[0].gb_vp8);
  387. intra_stats_print(&ctx[0].gb_vp8,"VP8 Intra Predictors",
  388. VP8_SCALE[B_SZ_LOG-OD_LOG_BSIZE0]);
  389. intra_stats_correct(&ctx[0].gb_od);
  390. intra_stats_print(&ctx[0].gb_od,"Daala Intra Predictors",
  391. OD_SCALE[B_SZ_LOG-OD_LOG_BSIZE0]);
  392. }
  393. for(i=0;i<NUM_PROCS;i++){
  394. intra_stats_ctx_clear(&ctx[i]);
  395. }
  396. od_intra_clear();
  397. return EXIT_SUCCESS;
  398. }