image_tools.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include <string.h>
  24. #include "od_defs.h"
  25. #include "od_filter.h"
  26. #include "od_intra.h"
  27. #include "image_tools.h"
  28. #include "../src/block_size_enc.h"
  29. #include "../src/dct.h"
  30. #include "../src/tf.h"
  31. #include <stdlib.h>
  32. od_rgba16_pixel COLORS[OD_INTRA_NMODES];
  33. void image_draw_block(od_rgba16_image *_image,int _x,int _y,
  34. const unsigned char *_block,int _stride){
  35. od_rgba16_pixel color;
  36. int i;
  37. int j;
  38. color[3]=(unsigned short)0xFFFFU;
  39. for(i=0;i<B_SZ;i++){
  40. for(j=0;j<B_SZ;j++){
  41. color[0]=color[1]=color[2]=_block[_stride*i+j]*0x101;
  42. od_rgba16_image_draw_point(_image,_x+j,_y+i,color);
  43. }
  44. }
  45. }
  46. int image_write_png(od_rgba16_image *_image,const char *_name){
  47. char fout_name[8192];
  48. FILE *fout;
  49. sprintf(fout_name,"%s.png",_name);
  50. fout=fopen(fout_name,"wb");
  51. if(fout==NULL){
  52. fprintf(stderr,"Could not open '%s' for reading.\n",fout_name);
  53. return EXIT_FAILURE;
  54. }
  55. od_rgba16_image_write_png(_image,fout);
  56. fclose(fout);
  57. return EXIT_SUCCESS;
  58. }
  59. void image_files_init(image_files *_this,int _nxblocks,int _nyblocks){
  60. od_rgba16_image_init(&_this->raw,B_SZ*_nxblocks,B_SZ*_nyblocks);
  61. od_rgba16_image_init(&_this->map,_nxblocks,_nyblocks);
  62. od_rgba16_image_init(&_this->pred,B_SZ*_nxblocks,B_SZ*_nyblocks);
  63. od_rgba16_image_init(&_this->res,B_SZ*_nxblocks,B_SZ*_nyblocks);
  64. }
  65. void image_files_clear(image_files *_this){
  66. od_rgba16_image_clear(&_this->raw);
  67. od_rgba16_image_clear(&_this->map);
  68. od_rgba16_image_clear(&_this->pred);
  69. od_rgba16_image_clear(&_this->res);
  70. }
  71. void image_files_write(image_files *_this,const char *_name,const char *_suf){
  72. char name[8192];
  73. sprintf(name,"%s-raw%s",_name,_suf==NULL?"":_suf);
  74. image_write_png(&_this->raw,name);
  75. sprintf(name,"%s-map%s",_name,_suf==NULL?"":_suf);
  76. image_write_png(&_this->map,name);
  77. sprintf(name,"%s-pred%s",_name,_suf==NULL?"":_suf);
  78. image_write_png(&_this->pred,name);
  79. sprintf(name,"%s-res%s",_name,_suf==NULL?"":_suf);
  80. image_write_png(&_this->res,name);
  81. }
  82. static void od_pre_blocks(od_coeff *_out,int _out_stride,od_coeff *_in,
  83. int _in_stride,int _bx,int _by,int _b_sz_log){
  84. int b_sz;
  85. int by;
  86. int bx;
  87. int j;
  88. int i;
  89. b_sz=1<<_b_sz_log;
  90. for(by=0;by<_by;by++){
  91. int y;
  92. y=by<<_b_sz_log;
  93. for(bx=0;bx<_bx;bx++){
  94. int x;
  95. x=bx<<_b_sz_log;
  96. for(i=0;i<b_sz;i++){
  97. od_coeff col[B_SZ_MAX];
  98. #if APPLY_FILTER
  99. for(j=0;j<b_sz;j++){
  100. col[j]=_in[_in_stride*(y+j)+x+i];
  101. }
  102. OD_ASSERT(_b_sz_log>=OD_LOG_BSIZE0&&_b_sz_log<=B_SZ_LOG_MAX);
  103. (*NE_PRE_FILTER[_b_sz_log-OD_LOG_BSIZE0])(col,col);
  104. for(j=0;j<b_sz;j++){
  105. _out[_out_stride*(y+j)+x+i]=col[j];
  106. }
  107. #else
  108. for(j=0;j<b_sz;j++){
  109. _out[_out_stride*(y+j)+x+i]=_in[_in_stride*(y+j)+x+i];
  110. }
  111. #endif
  112. }
  113. #if APPLY_FILTER
  114. for(j=0;j<b_sz;j++){
  115. od_coeff *row;
  116. row=&_out[_out_stride*(y+j)+x];
  117. OD_ASSERT(_b_sz_log>=OD_LOG_BSIZE0&&_b_sz_log<=B_SZ_LOG_MAX);
  118. (*NE_PRE_FILTER[_b_sz_log-OD_LOG_BSIZE0])(row,row);
  119. }
  120. #endif
  121. }
  122. }
  123. }
  124. static void od_post_blocks(od_coeff *_out,int _out_stride,od_coeff *_in,
  125. int _in_stride,int _bx,int _by,int _b_sz_log){
  126. int b_sz;
  127. int bx;
  128. int by;
  129. int j;
  130. int i;
  131. b_sz=1<<_b_sz_log;
  132. for(by=0;by<_by;by++){
  133. int y;
  134. y=by<<_b_sz_log;
  135. for(bx=0;bx<_bx;bx++){
  136. int x;
  137. x=bx<<_b_sz_log;
  138. for(j=0;j<b_sz;j++){
  139. od_coeff *row;
  140. for(i=0;i<b_sz;i++){
  141. _out[_out_stride*(y+j)+x+i]=_in[_in_stride*(y+j)+x+i];
  142. }
  143. #if APPLY_FILTER
  144. row=&_out[_out_stride*(y+j)+x];
  145. OD_ASSERT(_b_sz_log>=OD_LOG_BSIZE0&&_b_sz_log<=B_SZ_LOG_MAX);
  146. (*NE_POST_FILTER[_b_sz_log-OD_LOG_BSIZE0])(row,row);
  147. #endif
  148. }
  149. #if APPLY_FILTER
  150. for(i=0;i<b_sz;i++){
  151. od_coeff col[B_SZ_MAX];
  152. for(j=0;j<b_sz;j++){
  153. col[j]=_out[_out_stride*(y+j)+x+i];
  154. }
  155. OD_ASSERT(_b_sz_log>=OD_LOG_BSIZE0&&_b_sz_log<=B_SZ_LOG_MAX);
  156. (*NE_POST_FILTER[_b_sz_log-OD_LOG_BSIZE0])(col,col);
  157. for(j=0;j<b_sz;j++){
  158. _out[_out_stride*(j+y)+x+i]=col[j];
  159. }
  160. }
  161. #endif
  162. }
  163. }
  164. }
  165. static void od_fdct_blocks(od_coeff *_out,int _out_stride,od_coeff *_in,
  166. int _in_stride,int _bx,int _by,int _b_sz_log){
  167. int bx;
  168. int by;
  169. for(by=0;by<_by;by++){
  170. int y;
  171. y=by<<_b_sz_log;
  172. for(bx=0;bx<_bx;bx++){
  173. int x;
  174. x=bx<<_b_sz_log;
  175. OD_ASSERT(_b_sz_log>=OD_LOG_BSIZE0&&_b_sz_log<=B_SZ_LOG_MAX);
  176. (*OD_FDCT_2D[_b_sz_log-OD_LOG_BSIZE0])(&_out[_out_stride*y+x],_out_stride,
  177. &_in[_in_stride*y+x],_in_stride);
  178. }
  179. }
  180. }
  181. #if TF_BLOCKS
  182. static void od_tf_blocks_down(od_coeff *_out,int _out_stride,od_coeff *_in,
  183. int _in_stride,int _bx,int _by,int _b_sz_log){
  184. int b_sz;
  185. int by;
  186. int bx;
  187. b_sz=1<<_b_sz_log;
  188. for(by=0;by<_by;by++){
  189. int y;
  190. y=by<<_b_sz_log;
  191. for(bx=0;bx<_bx;bx++){
  192. int x;
  193. x=bx<<_b_sz_log;
  194. if(_b_sz_log==2){
  195. int j;
  196. int i;
  197. for(j=0;j<b_sz;j++){
  198. for(i=0;i<b_sz;i++){
  199. _out[_out_stride*(y+j)+x+i]=_in[_in_stride*(y+j)+x+i];
  200. }
  201. }
  202. }
  203. else {
  204. od_convert_block_down(&_out[_out_stride*y+x],_out_stride,
  205. &_in[_in_stride*y+x],_in_stride,_b_sz_log-2,0,0);
  206. }
  207. }
  208. }
  209. }
  210. #endif
  211. static void od_idct_blocks(od_coeff *_out,int _out_stride,od_coeff *_in,
  212. int _in_stride,int _bx,int _by,int _b_sz_log){
  213. int bx;
  214. int by;
  215. for(by=0;by<_by;by++){
  216. int y;
  217. y=by<<_b_sz_log;
  218. for(bx=0;bx<_bx;bx++){
  219. int x;
  220. x=bx<<_b_sz_log;
  221. OD_ASSERT(_b_sz_log>=OD_LOG_BSIZE0&&_b_sz_log<=B_SZ_LOG_MAX);
  222. (*OD_IDCT_2D[_b_sz_log-OD_LOG_BSIZE0])(&_out[_out_stride*y+x],_out_stride,
  223. &_in[_in_stride*y+x],_in_stride);
  224. }
  225. }
  226. }
  227. void image_data_init(image_data *_this,const char *_name,int _b_sz_log,
  228. int _nxblocks,int _nyblocks){
  229. int w;
  230. int h;
  231. _this->name=_name;
  232. _this->b_sz_log=_b_sz_log;
  233. _this->nxblocks=_nxblocks;
  234. _this->nyblocks=_nyblocks;
  235. _this->mask=(unsigned char *)malloc(sizeof(*_this->mask)*_nxblocks*_nyblocks);
  236. _this->mode=(unsigned char *)malloc(sizeof(*_this->mode)*_nxblocks*_nyblocks);
  237. _this->weight=(double *)malloc(sizeof(*_this->weight)*_nxblocks*_nyblocks);
  238. w=(_nxblocks+3)<<_b_sz_log;
  239. h=(_nyblocks+3)<<_b_sz_log;
  240. _this->pre=(od_coeff *)malloc(sizeof(*_this->pre)*w*h);
  241. _this->pre_stride=w;
  242. w=(_nxblocks+2)<<_b_sz_log;
  243. h=(_nyblocks+2)<<_b_sz_log;
  244. _this->fdct=(od_coeff *)malloc(sizeof(*_this->fdct)*w*h);
  245. _this->fdct_stride=w;
  246. #if TF_BLOCKS
  247. _this->tf=(od_coeff *)malloc(sizeof(*_this->fdct)*w*h);
  248. #endif
  249. w=(_nxblocks+0)<<_b_sz_log;
  250. h=(_nyblocks+0)<<_b_sz_log;
  251. _this->pred=(double *)malloc(sizeof(*_this->pred)*w*h);
  252. _this->pred_stride=w;
  253. w=(_nxblocks+2)<<_b_sz_log;
  254. h=(_nyblocks+2)<<_b_sz_log;
  255. _this->idct=(od_coeff *)malloc(sizeof(*_this->idct)*w*h);
  256. _this->idct_stride=w;
  257. w=(_nxblocks+1)<<_b_sz_log;
  258. h=(_nyblocks+1)<<_b_sz_log;
  259. _this->post=(od_coeff *)malloc(sizeof(*_this->post)*w*h);
  260. _this->post_stride=w;
  261. }
  262. void image_data_clear(image_data *_this){
  263. free(_this->mask);
  264. free(_this->mode);
  265. free(_this->weight);
  266. free(_this->pre);
  267. free(_this->fdct);
  268. #if TF_BLOCKS
  269. free(_this->tf);
  270. #endif
  271. free(_this->pred);
  272. free(_this->idct);
  273. free(_this->post);
  274. }
  275. void image_data_mask(image_data *_this,const unsigned char *_data,int _stride){
  276. int j;
  277. int i;
  278. memset(_this->mask,0,_this->nyblocks*_this->nxblocks);
  279. /* process_block_size32 needs 32x32 image data with 6 pixel padding */
  280. OD_ASSERT(((_stride-_this->nxblocks*B_SZ)/2)>=6);
  281. for(j=0;j<_this->nyblocks*B_SZ/32;j++){
  282. for(i=0;i<_this->nxblocks*B_SZ/32;i++){
  283. const unsigned char *b;
  284. BlockSizeComp bs;
  285. int dec[4][4];
  286. int k;
  287. int l;
  288. b=&_data[_stride*32*j+32*i];
  289. process_block_size32(&bs, b,_stride, b, _stride, dec, 21);
  290. for(l=0;l<32/B_SZ;l++){
  291. for(k=0;k<32/B_SZ;k++){
  292. /*printf("i=%i j=%i k=%i l=%i\n",i,j,k,l);
  293. printf("bx=%i by=%i\n",i*32/B_SZ+k*B_SZ,j*32/B_SZ+l*B_SZ);
  294. fflush(stdout);*/
  295. #if B_SZ==4
  296. _this->mask[_this->nxblocks*(j*32/B_SZ+l)+i*32/B_SZ+k]=
  297. (dec[l>>1][k>>1]==0);
  298. #elif B_SZ==8
  299. _this->mask[_this->nxblocks*(j*32/B_SZ+l)+i*32/B_SZ+k]=
  300. (dec[l][1]==1);
  301. #elif B_SZ==16
  302. _this->mask[_this->nxblocks*(j*32/B_SZ+l)+i*32/B_SZ+k]=
  303. (dec[l<<1][k<<1]==2||dec[l<<1][k<<1]==3);
  304. #else
  305. # error "Invalid block size."
  306. #endif
  307. }
  308. }
  309. }
  310. }
  311. }
  312. void image_data_pre_block(image_data *_this,const unsigned char *_data,
  313. int _stride,int _bi,int _bj){
  314. int b_sz;
  315. int x0;
  316. int y0;
  317. int bx;
  318. int by;
  319. int x;
  320. int y;
  321. int bi;
  322. int bj;
  323. int i;
  324. int j;
  325. od_coeff buf[B_SZ_MAX*B_SZ_MAX];
  326. b_sz=1<<_this->b_sz_log;
  327. x0=-(b_sz>>1);
  328. y0=-(b_sz>>1);
  329. bx=by=1;
  330. if(_bi==0){
  331. x0-=b_sz;
  332. bx++;
  333. }
  334. if(_bj==0){
  335. y0-=b_sz;
  336. by++;
  337. }
  338. if(_bi==_this->nxblocks-1){
  339. bx+=2;
  340. }
  341. if(_bj==_this->nyblocks-1){
  342. by+=2;
  343. }
  344. x=x0+_bi*b_sz+(3*b_sz>>1);
  345. y=y0+_bj*b_sz+(3*b_sz>>1);
  346. for(bj=0;bj<by;bj++){
  347. for(bi=0;bi<bx;bi++){
  348. for(j=0;j<b_sz;j++){
  349. for(i=0;i<b_sz;i++){
  350. buf[b_sz*j+i]=
  351. (_data[_stride*(y0+b_sz*bj+j)+x0+b_sz*bi+i]-128)*INPUT_SCALE;
  352. }
  353. }
  354. od_pre_blocks(&_this->pre[_this->pre_stride*(y+b_sz*bj)+x+b_sz*bi],
  355. _this->pre_stride,buf,b_sz,1,1,_this->b_sz_log);
  356. }
  357. }
  358. }
  359. void image_data_fdct_block(image_data *_this,int _bi,int _bj){
  360. int b_sz;
  361. int x0;
  362. int y0;
  363. int bx;
  364. int by;
  365. int x;
  366. int y;
  367. b_sz=1<<_this->b_sz_log;
  368. x0=_bi*b_sz+(3*b_sz>>1);
  369. y0=_bj*b_sz+(3*b_sz>>1);
  370. bx=by=1;
  371. if(_bi==0){
  372. x0-=b_sz;
  373. bx++;
  374. }
  375. if(_bj==0){
  376. y0-=b_sz;
  377. by++;
  378. }
  379. if(_bi==_this->nxblocks-1){
  380. bx++;
  381. }
  382. if(_bj==_this->nyblocks-1){
  383. by++;
  384. }
  385. x=x0-(b_sz>>1);
  386. y=y0-(b_sz>>1);
  387. od_fdct_blocks(&_this->fdct[_this->fdct_stride*y+x],_this->fdct_stride,
  388. &_this->pre[_this->pre_stride*y0+x0],_this->pre_stride,bx,by,
  389. _this->b_sz_log);
  390. }
  391. #if TF_BLOCKS
  392. void image_data_tf_block(image_data *_this,int _bi,int _bj){
  393. int b_sz;
  394. int x;
  395. int y;
  396. int bx;
  397. int by;
  398. b_sz=1<<_this->b_sz_log;
  399. x=_bi*b_sz+b_sz;
  400. y=_bj*b_sz+b_sz;
  401. bx=by=1;
  402. if(_bi==0){
  403. x-=b_sz;
  404. bx++;
  405. }
  406. if(_bj==0){
  407. y-=b_sz;
  408. by++;
  409. }
  410. if(_bi==_this->nxblocks-1){
  411. bx++;
  412. }
  413. if(_bj==_this->nyblocks-1){
  414. by++;
  415. }
  416. od_tf_blocks_down(&_this->tf[_this->fdct_stride*y+x],_this->fdct_stride,
  417. &_this->fdct[_this->fdct_stride*y+x],_this->fdct_stride,bx,by,
  418. _this->b_sz_log);
  419. }
  420. #endif
  421. void image_data_print_block(image_data *_this,int _bi,int _bj,FILE *_fp){
  422. int b_sz;
  423. int by;
  424. int bx;
  425. od_coeff *block;
  426. int j;
  427. int i;
  428. b_sz=1<<_this->b_sz_log;
  429. #if MASK_BLOCKS
  430. if(!_this->mask[_this->nxblocks*_bj+_bi]){
  431. return;
  432. }
  433. #endif
  434. fprintf(_fp,"%i",_this->mode[_this->nxblocks*_bj+_bi]);
  435. for(by=0;by<=1;by++){
  436. for(bx=0;bx<=(1-by)<<1;bx++){
  437. #if TF_BLOCKS
  438. block=&_this->tf[_this->fdct_stride*b_sz*(_bj+by)+b_sz*(_bi+bx)];
  439. #else
  440. block=&_this->fdct[_this->fdct_stride*b_sz*(_bj+by)+b_sz*(_bi+bx)];
  441. #endif
  442. for(j=0;j<b_sz;j++){
  443. for(i=0;i<b_sz;i++){
  444. fprintf(_fp," %i",block[_this->fdct_stride*j+i]);
  445. }
  446. }
  447. }
  448. }
  449. block=&_this->fdct[_this->fdct_stride*b_sz*(_bj+1)+b_sz*(_bi+1)];
  450. for(j=0;j<b_sz;j++){
  451. for(i=0;i<b_sz;i++){
  452. fprintf(_fp," %i",block[_this->fdct_stride*j+i]);
  453. }
  454. }
  455. fprintf(_fp,"\n");
  456. fflush(_fp);
  457. }
  458. void image_data_load_block(image_data *_this,int _bi,int _bj,
  459. od_coeff *_coeffs){
  460. int b_sz;
  461. od_coeff *block;
  462. int by;
  463. int bx;
  464. int y;
  465. int x;
  466. b_sz=1<<_this->b_sz_log;
  467. #if TF_BLOCKS
  468. block=&_this->tf[_this->fdct_stride*b_sz*_bj+b_sz*_bi];
  469. #else
  470. block=&_this->fdct[_this->fdct_stride*b_sz*_bj+b_sz*_bi];
  471. #endif
  472. for(by=0;by<=1;by++){
  473. for(bx=0;bx<=(1-by)<<1;bx++){
  474. for(y=0;y<b_sz;y++){
  475. for(x=0;x<b_sz;x++){
  476. (*_coeffs)=block[_this->fdct_stride*(b_sz*by+y)+b_sz*bx+x];
  477. _coeffs++;
  478. }
  479. }
  480. }
  481. }
  482. block=&_this->fdct[_this->fdct_stride*b_sz*(_bj+1)+b_sz*(_bi+1)];
  483. for(y=0;y<b_sz;y++){
  484. for(x=0;x<b_sz;x++){
  485. (*_coeffs)=block[_this->fdct_stride*y+x];
  486. _coeffs++;
  487. }
  488. }
  489. }
  490. void image_data_pred_block(image_data *_this,int _bi,int _bj){
  491. int b_sz;
  492. double *pred;
  493. int mode;
  494. od_coeff coeffs[5*B_SZ_MAX*B_SZ_MAX];
  495. b_sz=1<<_this->b_sz_log;
  496. pred=&_this->pred[_this->pred_stride*b_sz*_bj+b_sz*_bi];
  497. #if MASK_BLOCKS
  498. if(!_this->mask[_this->nxblocks*_bj+_bi]){
  499. od_coeff *fdct;
  500. int j;
  501. int i;
  502. fdct=&_this->fdct[_this->fdct_stride*b_sz*(_bj+1)+b_sz*(_bi+1)];
  503. for(j=0;j<b_sz;j++){
  504. for(i=0;i<b_sz;i++){
  505. pred[_this->pred_stride*j+i]=fdct[_this->fdct_stride*j+i];
  506. }
  507. }
  508. return;
  509. }
  510. #endif
  511. mode=_this->mode[_this->nxblocks*_bj+_bi];
  512. image_data_load_block(_this,_bi,_bj,coeffs);
  513. OD_ASSERT(_this->b_sz_log>=OD_LOG_BSIZE0&&_this->b_sz_log<=B_SZ_LOG_MAX);
  514. (*NE_INTRA_MULT[_this->b_sz_log-OD_LOG_BSIZE0])(pred,_this->pred_stride,
  515. coeffs,mode);
  516. }
  517. void image_data_stats_block(image_data *_this,const unsigned char *_data,
  518. int _stride,int _bi,int _bj,intra_stats *_stats){
  519. int b_sz;
  520. int mode;
  521. od_coeff *ref;
  522. double *pred;
  523. int j;
  524. int i;
  525. double buf[B_SZ_MAX*B_SZ_MAX];
  526. b_sz=1<<_this->b_sz_log;
  527. #if MASK_BLOCKS
  528. if(!_this->mask[_this->nxblocks*_bj+_bi]){
  529. return;
  530. }
  531. #endif
  532. mode=_this->mode[_this->nxblocks*_bj+_bi];
  533. ref=&_this->fdct[_this->fdct_stride*b_sz*(_bj+1)+b_sz*(_bi+1)];
  534. pred=&_this->pred[_this->pred_stride*b_sz*_bj+b_sz*_bi];
  535. for(j=0;j<b_sz;j++){
  536. for(i=0;i<b_sz;i++){
  537. buf[b_sz*j+i]=ref[_this->fdct_stride*j+i]-pred[_this->pred_stride*j+i];
  538. }
  539. }
  540. intra_stats_update(_stats,_data,_stride,mode,ref,_this->fdct_stride,buf,b_sz);
  541. }
  542. void image_data_idct_block(image_data *_this,int _bi,int _bj){
  543. int b_sz;
  544. int x0;
  545. int y0;
  546. int x;
  547. int y;
  548. int bx;
  549. int by;
  550. int j;
  551. int i;
  552. double *p;
  553. od_coeff buf[B_SZ_MAX*B_SZ_MAX];
  554. b_sz=1<<_this->b_sz_log;
  555. x0=b_sz*_bi;
  556. y0=b_sz*_bj;
  557. x=x0+b_sz;
  558. y=y0+b_sz;
  559. bx=by=1;
  560. if(_bi==0){
  561. x-=b_sz;
  562. bx++;
  563. }
  564. if(_bj==0){
  565. y-=b_sz;
  566. by++;
  567. }
  568. if(_bi==_this->nxblocks-1){
  569. bx++;
  570. }
  571. if(_bj==_this->nyblocks-1){
  572. by++;
  573. }
  574. /* TODO remove redundant computations here */
  575. if(bx!=1||by!=1){
  576. od_idct_blocks(&_this->idct[_this->idct_stride*y+x],_this->idct_stride,
  577. &_this->fdct[_this->fdct_stride*y+x],_this->fdct_stride,bx,by,
  578. _this->b_sz_log);
  579. }
  580. p=&_this->pred[_this->pred_stride*y0+x0];
  581. for(j=0;j<b_sz;j++){
  582. for(i=0;i<b_sz;i++){
  583. buf[j*b_sz+i]=(od_coeff)floor(p[_this->pred_stride*j+i]+0.5);
  584. }
  585. }
  586. x=x0+b_sz;
  587. y=y0+b_sz;
  588. od_idct_blocks(&_this->idct[_this->idct_stride*y+x],_this->idct_stride,
  589. buf,b_sz,1,1,_this->b_sz_log);
  590. }
  591. void image_data_post_block(image_data *_this,int _bi,int _bj){
  592. int b_sz;
  593. int x0;
  594. int y0;
  595. int x;
  596. int y;
  597. int bx;
  598. int by;
  599. b_sz=1<<_this->b_sz_log;
  600. x=b_sz*_bi;
  601. y=b_sz*_bj;
  602. x0=x+(b_sz>>1);
  603. y0=y+(b_sz>>1);
  604. bx=by=1;
  605. if(_bi==_this->nxblocks-1){
  606. bx++;
  607. }
  608. if(_bj==_this->nyblocks-1){
  609. by++;
  610. }
  611. od_post_blocks(&_this->post[_this->post_stride*y+x],_this->post_stride,
  612. &_this->idct[_this->idct_stride*y0+x0],_this->idct_stride,bx,by,
  613. _this->b_sz_log);
  614. }
  615. void image_data_files_block(image_data *_this,const unsigned char *_data,
  616. int _stride,int _bi,int _bj,image_files *_files){
  617. int mode;
  618. od_coeff *p;
  619. int j;
  620. int i;
  621. od_coeff v;
  622. unsigned char buf[B_SZ*B_SZ];
  623. mode=_this->mode[_bj*_this->nxblocks+_bi];
  624. od_rgba16_image_draw_point(&_files->map,_bi,_bj,COLORS[mode]);
  625. p=&_this->pre[_this->pre_stride*(B_SZ*_bj+(3*B_SZ>>1))+B_SZ*_bi+(3*B_SZ>>1)];
  626. for(j=0;j<B_SZ;j++){
  627. for(i=0;i<B_SZ;i++){
  628. v=(p[_this->pre_stride*j+i]+INPUT_SCALE*128+INPUT_SCALE/2)/INPUT_SCALE;
  629. buf[B_SZ*j+i]=OD_CLAMPI(0,v,255);
  630. }
  631. }
  632. image_draw_block(&_files->raw,B_SZ*_bi,B_SZ*_bj,buf,B_SZ);
  633. p=&_this->post[_this->post_stride*(B_SZ*_bj+(B_SZ>>1))+B_SZ*_bi+(B_SZ>>1)];
  634. for(j=0;j<B_SZ;j++){
  635. for(i=0;i<B_SZ;i++){
  636. v=(p[_this->post_stride*j+i]+INPUT_SCALE*128+INPUT_SCALE/2)/INPUT_SCALE;
  637. buf[B_SZ*j+i]=OD_CLAMPI(0,v,255);
  638. }
  639. }
  640. image_draw_block(&_files->pred,B_SZ*_bi,B_SZ*_bj,buf,B_SZ);
  641. for(j=0;j<B_SZ;j++){
  642. for(i=0;i<B_SZ;i++){
  643. buf[B_SZ*j+i]=OD_CLAMPI(0,_data[_stride*j+i]-buf[B_SZ*j+i]+128,255);
  644. }
  645. }
  646. image_draw_block(&_files->res,B_SZ*_bi,B_SZ*_bj,buf,B_SZ);
  647. }
  648. int image_data_save_map(image_data *_this){
  649. char name[8192];
  650. char *pos;
  651. int eos;
  652. FILE *fout;
  653. strcpy(name,_this->name);
  654. pos=strrchr(name,'.');
  655. if(!pos){
  656. eos=strlen(name);
  657. }
  658. else{
  659. eos=pos-name;
  660. }
  661. sprintf(&name[eos],".map");
  662. fout=fopen(name,"wb");
  663. if(fout==NULL){
  664. fprintf(stderr,"Error opening output file '%s'.\n",name);
  665. return EXIT_FAILURE;
  666. }
  667. if(fwrite(_this->mode,
  668. _this->nxblocks*(size_t)_this->nyblocks*sizeof(*_this->mode),1,fout)<1){
  669. fprintf(stderr,"Error writing to output file '%s'.\n",name);
  670. return EXIT_FAILURE;
  671. }
  672. if(fwrite(_this->weight,
  673. _this->nxblocks*(size_t)_this->nyblocks*sizeof(*_this->weight),1,fout)<1){
  674. fprintf(stderr,"Error writing to output file '%s'.\n",name);
  675. return EXIT_FAILURE;
  676. }
  677. fclose(fout);
  678. return EXIT_SUCCESS;
  679. }
  680. int image_data_load_map(image_data *_this){
  681. char name[8192];
  682. char *pos;
  683. int eos;
  684. FILE *fin;
  685. strcpy(name,_this->name);
  686. pos=strrchr(name,'.');
  687. if(!pos){
  688. eos=strlen(name);
  689. }
  690. else{
  691. eos=pos-name;
  692. }
  693. sprintf(&name[eos],".map");
  694. fin=fopen(name,"rb");
  695. if(fin==NULL){
  696. fprintf(stderr,"Error opening input file '%s'.\n",name);
  697. return EXIT_FAILURE;
  698. }
  699. if(fread(_this->mode,
  700. _this->nxblocks*(size_t)_this->nyblocks*sizeof(*_this->mode),1,fin)<1) {
  701. fprintf(stderr,"Error reading from input file '%s'.\n",name);
  702. return EXIT_FAILURE;
  703. }
  704. if(fread(_this->weight,
  705. _this->nxblocks*(size_t)_this->nyblocks*sizeof(*_this->weight),1,fin)<1) {
  706. fprintf(stderr,"Error reading from input file '%s'.\n",name);
  707. return EXIT_FAILURE;
  708. }
  709. fclose(fin);
  710. return EXIT_SUCCESS;
  711. }