intra_fit_tools.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*Daala video codec
  2. Copyright (c) 2012 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 <stdlib.h>
  21. #include <string.h>
  22. #include "intra_fit_tools.h"
  23. /*Computes the starting offset and number of blocks which can be intra
  24. predicted with full context (i.e., all of their neighbors) available.*/
  25. void get_intra_dims(const th_info *_ti,int _pli,int _padding,
  26. int *_x0,int *_y0,int *_nxblocks,int *_nyblocks){
  27. int xshift;
  28. int yshift;
  29. xshift=_pli!=0&&!(_ti->pixel_fmt&1);
  30. yshift=_pli!=0&&!(_ti->pixel_fmt&2);
  31. /*An offset of 1 would be fine to provide enough context for VP8-style intra
  32. prediction, but for frequency-domain prediction, we'll want a full block,
  33. plus overlap.*/
  34. *_x0=(_ti->pic_x>>xshift)+(_padding>>1);
  35. *_y0=(_ti->pic_y>>yshift)+(_padding>>1);
  36. /*We take an extra block off the end to give enough context for above-right
  37. intra prediction.*/
  38. *_nxblocks=_ti->pic_width-(_padding<<xshift)>>B_SZ_LOG+xshift;
  39. *_nyblocks=_ti->pic_height-(_padding<<yshift)>>B_SZ_LOG+yshift;
  40. }
  41. char *get_map_filename(const char *_name,int _pli,int _nxblocks,int _nyblocks){
  42. char *ret;
  43. char fname[8192];
  44. size_t fname_len;
  45. sprintf(fname,"%s-pli%i-%i-%ix%i.intra.map",
  46. _name,_pli,B_SZ,_nxblocks,_nyblocks);
  47. fname_len=strlen(fname);
  48. ret=(char *)malloc(fname_len+1);
  49. memcpy(ret,fname,fname_len+1);
  50. return ret;
  51. }
  52. char *get_weights_filename(const char *_name,
  53. int _pli,int _nxblocks,int _nyblocks){
  54. char *ret;
  55. char fname[8192];
  56. size_t fname_len;
  57. sprintf(fname,"%s-pli%i-%i-%ix%i.intra.weights",
  58. _name,_pli,B_SZ,_nxblocks,_nyblocks);
  59. fname_len=strlen(fname);
  60. ret=(char *)malloc(fname_len+1);
  61. memcpy(ret,fname,fname_len+1);
  62. return ret;
  63. }
  64. int apply_to_blocks(void *_ctx,plane_start_func _start,block_func _block,
  65. plane_finish_func _finish,int _argc,const char **_argv){
  66. block_func blocks[1];
  67. blocks[0]=_block;
  68. return apply_to_blocks2(_ctx,4*B_SZ,_start,blocks,1,_finish,0x7,_argc,_argv);
  69. }
  70. int apply_to_blocks2(void *_ctx,int _padding,plane_start_func _start,
  71. const block_func *_blocks,int _nfuncs,plane_finish_func _finish,int _plmask,
  72. int _argc,const char **_argv){
  73. int ai;
  74. for(ai=1;ai<_argc;ai++){
  75. video_input vid;
  76. th_info ti;
  77. th_ycbcr_buffer ycbcr;
  78. FILE *fin;
  79. int pli;
  80. fin=fopen(_argv[ai],"rb");
  81. if(fin==NULL){
  82. fprintf(stderr,"Could not open '%s' for reading.\n",_argv[ai]);
  83. return EXIT_FAILURE;
  84. }
  85. if(video_input_open(&vid,fin)<0){
  86. fprintf(stderr,"Error reading video info from '%s'.\n",_argv[ai]);
  87. return EXIT_FAILURE;
  88. }
  89. video_input_get_info(&vid,&ti);
  90. if(video_input_fetch_frame(&vid,ycbcr,NULL)<0){
  91. fprintf(stderr,"Error reading first frame from '%s'.\n",_argv[ai]);
  92. return EXIT_FAILURE;
  93. }
  94. for(pli=0;pli<3;pli++){
  95. if(_plmask&1<<pli){
  96. int x0;
  97. int y0;
  98. int nxblocks;
  99. int nyblocks;
  100. int ret;
  101. get_intra_dims(&ti,pli,_padding,&x0,&y0,&nxblocks,&nyblocks);
  102. if(_start!=NULL){
  103. ret=(*_start)(_ctx,_argv[ai],&ti,pli,nxblocks,nyblocks);
  104. if(ret)return ret;
  105. }
  106. if(_blocks!=NULL){
  107. int f;
  108. for(f=0;f<_nfuncs;f++){
  109. if(_blocks[f]!=NULL){
  110. const unsigned char *data;
  111. int stride;
  112. int bj;
  113. int bi;
  114. data=ycbcr[pli].data;
  115. stride=ycbcr[pli].stride;
  116. for(bj=0;bj<nyblocks;bj++){
  117. int y;
  118. y=y0+B_SZ*bj;
  119. for(bi=0;bi<nxblocks;bi++){
  120. int x;
  121. x=x0+B_SZ*bi;
  122. (*_blocks[f])(_ctx,&data[stride*y+x],stride,bi,bj);
  123. }
  124. }
  125. }
  126. }
  127. }
  128. if(_finish!=NULL){
  129. ret=(*_finish)(_ctx);
  130. if(ret)return ret;
  131. }
  132. }
  133. }
  134. video_input_close(&vid);
  135. }
  136. return EXIT_SUCCESS;
  137. }
  138. void vp8_intra_predict(unsigned char *_dst,int _dst_stride,
  139. const unsigned char *_src,int _src_stride,int _mode){
  140. const unsigned char *above;
  141. unsigned char *left;
  142. unsigned char p[4*B_SZ];
  143. int x;
  144. int y;
  145. above=_src-_src_stride;
  146. p[2*B_SZ-1]=*(above-1);
  147. left=p+2*B_SZ;
  148. for(y=0;y<B_SZ;y++)left[y]=*(_src+_src_stride*y-1);
  149. switch(_mode){
  150. case OD_INTRA_DC:{
  151. int dc;
  152. dc=0;
  153. for(x=0;x<B_SZ;x++)dc+=above[x];
  154. for(y=0;y<B_SZ;y++)dc+=left[y];
  155. dc=dc+B_SZ>>B_SZ_LOG+1;
  156. for(y=0;y<B_SZ;y++){
  157. for(x=0;x<B_SZ;x++){
  158. *(_dst+y*_dst_stride+x)=(unsigned char)dc;
  159. }
  160. }
  161. }break;
  162. case OD_INTRA_TM:{
  163. for(y=0;y<B_SZ;y++){
  164. for(x=0;x<B_SZ;x++){
  165. *(_dst+y*_dst_stride+x)=OD_CLAMP255(above[x]+left[y]-*(above-1));
  166. }
  167. }
  168. }break;
  169. case OD_INTRA_HU:{
  170. for(y=B_SZ;y<B_SZ+(B_SZ>>1)+1;y++)left[y]=left[B_SZ-1];
  171. for(y=0;y<B_SZ;y++){
  172. for(x=0;x<(B_SZ>>1);x++){
  173. *(_dst+y*_dst_stride+2*x)=
  174. (unsigned char)(left[y+x]+left[y+x+1]+1>>1);
  175. *(_dst+y*_dst_stride+2*x+1)=
  176. (unsigned char)(left[y+x]+2*left[y+x+1]+left[x+y+2]+2>>2);
  177. }
  178. }
  179. }break;
  180. case OD_INTRA_HE:{
  181. unsigned char q[B_SZ];
  182. p[2*B_SZ-1]=*(above-1);
  183. left[B_SZ]=left[B_SZ-1];
  184. for(y=0;y<B_SZ;y++){
  185. q[y]=(unsigned char)(p[2*B_SZ+y-1]+2*p[2*B_SZ+y]+p[2*B_SZ+y+1]+2>>2);
  186. }
  187. for(y=0;y<B_SZ;y++)for(x=0;x<B_SZ;x++)*(_dst+y*_dst_stride+x)=q[y];
  188. }break;
  189. case OD_INTRA_HD:{
  190. for(x=0;x<B_SZ;x++)p[2*B_SZ-x-1]=*(above+x-1);
  191. for(y=0;y<B_SZ;y++){
  192. for(x=0;x<(B_SZ>>1);x++){
  193. if(y<x){
  194. *(_dst+y*_dst_stride+2*x)=(unsigned char)(
  195. p[2*B_SZ+y-2*x+1]+2*p[2*B_SZ+y-2*x]+p[2*B_SZ+y-2*x-1]+2>>2);
  196. *(_dst+y*_dst_stride+2*x+1)=(unsigned char)(
  197. p[2*B_SZ+y-2*x]+2*p[2*B_SZ+y-2*x-1]+p[2*B_SZ+y-2*x-2]+2>>2);
  198. }
  199. else{
  200. *(_dst+y*_dst_stride+2*x)=
  201. (unsigned char)(p[2*B_SZ+y-x-1]+p[2*B_SZ+y-x]+1>>1);
  202. *(_dst+y*_dst_stride+2*x+1)=(unsigned char)(
  203. p[2*B_SZ+y-x]+2*p[2*B_SZ+y-x-1]+p[2*B_SZ+y-x-2]+2>>2);
  204. }
  205. }
  206. }
  207. }break;
  208. case OD_INTRA_RD:{
  209. for(x=0;x<=B_SZ;x++)p[2*B_SZ-x-1]=*(above+x-1);
  210. for(y=0;y<B_SZ;y++){
  211. for(x=0;x<B_SZ;x++){
  212. *(_dst+y*_dst_stride+x)=(unsigned char)(
  213. p[2*B_SZ+y-x]+2*p[2*B_SZ+y-x-1]+p[2*B_SZ+y-x-2]+2>>2);
  214. }
  215. }
  216. }break;
  217. case OD_INTRA_VR:{
  218. for(x=0;x<=B_SZ;x++)p[2*B_SZ-x-1]=*(above+x-1);
  219. for(y=0;y<(B_SZ>>1);y++){
  220. for(x=0;x<B_SZ;x++){
  221. if(x<y){
  222. *(_dst+2*y*_dst_stride+x)=(unsigned char)(
  223. p[2*B_SZ+2*y-x-1]+2*p[2*B_SZ+2*y-x-2]+p[2*B_SZ+2*y-x-3]+2>>2);
  224. *(_dst+(2*y+1)*_dst_stride+x)=(unsigned char)(
  225. p[2*B_SZ+2*y-x]+2*p[2*B_SZ+2*y-x-1]+p[2*B_SZ+2*y-x-2]+2>>2);
  226. }
  227. else{
  228. *(_dst+2*y*_dst_stride+x)=
  229. (unsigned char)(p[2*B_SZ+y-x-1]+p[2*B_SZ+y-x-2]+1>>1);
  230. *(_dst+(2*y+1)*_dst_stride+x)=(unsigned char)(
  231. p[2*B_SZ+y-x]+2*p[2*B_SZ+y-x-1]+p[2*B_SZ+y-x-2]+2>>2);
  232. }
  233. }
  234. }
  235. }break;
  236. case OD_INTRA_VE:{
  237. unsigned char q[B_SZ];
  238. for(x=0;x<B_SZ;x++){
  239. q[x]=(unsigned char)(*(above+x-1)+2*above[x]+above[x+1]+2>>2);
  240. }
  241. for(y=0;y<B_SZ;y++)for(x=0;x<B_SZ;x++)*(_dst+y*_dst_stride+x)=q[x];
  242. }break;
  243. case OD_INTRA_VL:{
  244. for(y=0;y<(B_SZ>>1);y++){
  245. /*This ignores the pattern mis-match for the last few values of the
  246. VP8 predictor.*/
  247. for(x=0;x<B_SZ;x++){
  248. *(_dst+2*y*_dst_stride+x)=
  249. (unsigned char)(above[y+x]+above[y+x+1]+1>>1);
  250. *(_dst+(2*y+1)*_dst_stride+x)=
  251. (unsigned char)(above[y+x]+2*above[y+x+1]+above[y+x+2]+2>>2);
  252. }
  253. }
  254. }break;
  255. case OD_INTRA_LD:{
  256. for(y=0;y<B_SZ;y++){
  257. for(x=0;x<B_SZ;x++){
  258. *(_dst+y*_dst_stride+x)=(unsigned char)(above[y+x]+2*above[y+x+1]
  259. +above[OD_MINI(y+x+2,2*B_SZ-1)]+2>>2);
  260. }
  261. }
  262. }break;
  263. }
  264. }