intra_stats.c 9.3 KB

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