dsputil_align.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * aligned/packed access motion
  3. *
  4. * Copyright (c) 2001-2003 BERO <bero@geocities.co.jp>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavcodec/avcodec.h"
  23. #include "libavcodec/dsputil.h"
  24. #define LP(p) *(uint32_t*)(p)
  25. #define UNPACK(ph,pl,tt0,tt1) do { \
  26. uint32_t t0,t1; t0=tt0;t1=tt1; \
  27. ph = ( (t0 & ~BYTE_VEC32(0x03))>>2) + ( (t1 & ~BYTE_VEC32(0x03))>>2); \
  28. pl = (t0 & BYTE_VEC32(0x03)) + (t1 & BYTE_VEC32(0x03)); } while(0)
  29. #define rnd_PACK(ph,pl,nph,npl) ph + nph + (((pl + npl + BYTE_VEC32(0x02))>>2) & BYTE_VEC32(0x03))
  30. #define no_rnd_PACK(ph,pl,nph,npl) ph + nph + (((pl + npl + BYTE_VEC32(0x01))>>2) & BYTE_VEC32(0x03))
  31. /* little endian */
  32. #define MERGE1(a,b,ofs) (ofs==0)?a:( ((a)>>(8*ofs))|((b)<<(32-8*ofs)) )
  33. #define MERGE2(a,b,ofs) (ofs==3)?b:( ((a)>>(8*(ofs+1)))|((b)<<(32-8*(ofs+1))) )
  34. /* big
  35. #define MERGE1(a,b,ofs) (ofs==0)?a:( ((a)<<(8*ofs))|((b)>>(32-8*ofs)) )
  36. #define MERGE2(a,b,ofs) (ofs==3)?b:( ((a)<<(8+8*ofs))|((b)>>(32-8-8*ofs)) )
  37. */
  38. #define put(d,s) d = s
  39. #define avg(d,s) d = rnd_avg32(s,d)
  40. #define OP_C4(ofs) \
  41. ref-=ofs; \
  42. do { \
  43. OP(LP(dest),MERGE1(LP(ref),LP(ref+4),ofs)); \
  44. ref+=stride; \
  45. dest+=stride; \
  46. } while(--height)
  47. #define OP_C40() \
  48. do { \
  49. OP(LP(dest),LP(ref)); \
  50. ref+=stride; \
  51. dest+=stride; \
  52. } while(--height)
  53. #define OP put
  54. static void put_pixels4_c(uint8_t *dest,const uint8_t *ref, const int stride,int height)
  55. {
  56. switch((int)ref&3){
  57. case 0: OP_C40(); return;
  58. case 1: OP_C4(1); return;
  59. case 2: OP_C4(2); return;
  60. case 3: OP_C4(3); return;
  61. }
  62. }
  63. #undef OP
  64. #define OP avg
  65. static void avg_pixels4_c(uint8_t *dest,const uint8_t *ref, const int stride,int height)
  66. {
  67. switch((int)ref&3){
  68. case 0: OP_C40(); return;
  69. case 1: OP_C4(1); return;
  70. case 2: OP_C4(2); return;
  71. case 3: OP_C4(3); return;
  72. }
  73. }
  74. #undef OP
  75. #define OP_C(ofs,sz,avg2) \
  76. { \
  77. ref-=ofs; \
  78. do { \
  79. uint32_t t0,t1; \
  80. t0 = LP(ref+0); \
  81. t1 = LP(ref+4); \
  82. OP(LP(dest+0), MERGE1(t0,t1,ofs)); \
  83. t0 = LP(ref+8); \
  84. OP(LP(dest+4), MERGE1(t1,t0,ofs)); \
  85. if (sz==16) { \
  86. t1 = LP(ref+12); \
  87. OP(LP(dest+8), MERGE1(t0,t1,ofs)); \
  88. t0 = LP(ref+16); \
  89. OP(LP(dest+12), MERGE1(t1,t0,ofs)); \
  90. } \
  91. ref+=stride; \
  92. dest+= stride; \
  93. } while(--height); \
  94. }
  95. /* aligned */
  96. #define OP_C0(sz,avg2) \
  97. { \
  98. do { \
  99. OP(LP(dest+0), LP(ref+0)); \
  100. OP(LP(dest+4), LP(ref+4)); \
  101. if (sz==16) { \
  102. OP(LP(dest+8), LP(ref+8)); \
  103. OP(LP(dest+12), LP(ref+12)); \
  104. } \
  105. ref+=stride; \
  106. dest+= stride; \
  107. } while(--height); \
  108. }
  109. #define OP_X(ofs,sz,avg2) \
  110. { \
  111. ref-=ofs; \
  112. do { \
  113. uint32_t t0,t1; \
  114. t0 = LP(ref+0); \
  115. t1 = LP(ref+4); \
  116. OP(LP(dest+0), avg2(MERGE1(t0,t1,ofs),MERGE2(t0,t1,ofs))); \
  117. t0 = LP(ref+8); \
  118. OP(LP(dest+4), avg2(MERGE1(t1,t0,ofs),MERGE2(t1,t0,ofs))); \
  119. if (sz==16) { \
  120. t1 = LP(ref+12); \
  121. OP(LP(dest+8), avg2(MERGE1(t0,t1,ofs),MERGE2(t0,t1,ofs))); \
  122. t0 = LP(ref+16); \
  123. OP(LP(dest+12), avg2(MERGE1(t1,t0,ofs),MERGE2(t1,t0,ofs))); \
  124. } \
  125. ref+=stride; \
  126. dest+= stride; \
  127. } while(--height); \
  128. }
  129. /* aligned */
  130. #define OP_Y0(sz,avg2) \
  131. { \
  132. uint32_t t0,t1,t2,t3,t; \
  133. \
  134. t0 = LP(ref+0); \
  135. t1 = LP(ref+4); \
  136. if (sz==16) { \
  137. t2 = LP(ref+8); \
  138. t3 = LP(ref+12); \
  139. } \
  140. do { \
  141. ref += stride; \
  142. \
  143. t = LP(ref+0); \
  144. OP(LP(dest+0), avg2(t0,t)); t0 = t; \
  145. t = LP(ref+4); \
  146. OP(LP(dest+4), avg2(t1,t)); t1 = t; \
  147. if (sz==16) { \
  148. t = LP(ref+8); \
  149. OP(LP(dest+8), avg2(t2,t)); t2 = t; \
  150. t = LP(ref+12); \
  151. OP(LP(dest+12), avg2(t3,t)); t3 = t; \
  152. } \
  153. dest+= stride; \
  154. } while(--height); \
  155. }
  156. #define OP_Y(ofs,sz,avg2) \
  157. { \
  158. uint32_t t0,t1,t2,t3,t,w0,w1; \
  159. \
  160. ref-=ofs; \
  161. w0 = LP(ref+0); \
  162. w1 = LP(ref+4); \
  163. t0 = MERGE1(w0,w1,ofs); \
  164. w0 = LP(ref+8); \
  165. t1 = MERGE1(w1,w0,ofs); \
  166. if (sz==16) { \
  167. w1 = LP(ref+12); \
  168. t2 = MERGE1(w0,w1,ofs); \
  169. w0 = LP(ref+16); \
  170. t3 = MERGE1(w1,w0,ofs); \
  171. } \
  172. do { \
  173. ref += stride; \
  174. \
  175. w0 = LP(ref+0); \
  176. w1 = LP(ref+4); \
  177. t = MERGE1(w0,w1,ofs); \
  178. OP(LP(dest+0), avg2(t0,t)); t0 = t; \
  179. w0 = LP(ref+8); \
  180. t = MERGE1(w1,w0,ofs); \
  181. OP(LP(dest+4), avg2(t1,t)); t1 = t; \
  182. if (sz==16) { \
  183. w1 = LP(ref+12); \
  184. t = MERGE1(w0,w1,ofs); \
  185. OP(LP(dest+8), avg2(t2,t)); t2 = t; \
  186. w0 = LP(ref+16); \
  187. t = MERGE1(w1,w0,ofs); \
  188. OP(LP(dest+12), avg2(t3,t)); t3 = t; \
  189. } \
  190. dest+=stride; \
  191. } while(--height); \
  192. }
  193. #define OP_X0(sz,avg2) OP_X(0,sz,avg2)
  194. #define OP_XY0(sz,PACK) OP_XY(0,sz,PACK)
  195. #define OP_XY(ofs,sz,PACK) \
  196. { \
  197. uint32_t t2,t3,w0,w1; \
  198. uint32_t a0,a1,a2,a3,a4,a5,a6,a7; \
  199. \
  200. ref -= ofs; \
  201. w0 = LP(ref+0); \
  202. w1 = LP(ref+4); \
  203. UNPACK(a0,a1,MERGE1(w0,w1,ofs),MERGE2(w0,w1,ofs)); \
  204. w0 = LP(ref+8); \
  205. UNPACK(a2,a3,MERGE1(w1,w0,ofs),MERGE2(w1,w0,ofs)); \
  206. if (sz==16) { \
  207. w1 = LP(ref+12); \
  208. UNPACK(a4,a5,MERGE1(w0,w1,ofs),MERGE2(w0,w1,ofs)); \
  209. w0 = LP(ref+16); \
  210. UNPACK(a6,a7,MERGE1(w1,w0,ofs),MERGE2(w1,w0,ofs)); \
  211. } \
  212. do { \
  213. ref+=stride; \
  214. w0 = LP(ref+0); \
  215. w1 = LP(ref+4); \
  216. UNPACK(t2,t3,MERGE1(w0,w1,ofs),MERGE2(w0,w1,ofs)); \
  217. OP(LP(dest+0),PACK(a0,a1,t2,t3)); \
  218. a0 = t2; a1 = t3; \
  219. w0 = LP(ref+8); \
  220. UNPACK(t2,t3,MERGE1(w1,w0,ofs),MERGE2(w1,w0,ofs)); \
  221. OP(LP(dest+4),PACK(a2,a3,t2,t3)); \
  222. a2 = t2; a3 = t3; \
  223. if (sz==16) { \
  224. w1 = LP(ref+12); \
  225. UNPACK(t2,t3,MERGE1(w0,w1,ofs),MERGE2(w0,w1,ofs)); \
  226. OP(LP(dest+8),PACK(a4,a5,t2,t3)); \
  227. a4 = t2; a5 = t3; \
  228. w0 = LP(ref+16); \
  229. UNPACK(t2,t3,MERGE1(w1,w0,ofs),MERGE2(w1,w0,ofs)); \
  230. OP(LP(dest+12),PACK(a6,a7,t2,t3)); \
  231. a6 = t2; a7 = t3; \
  232. } \
  233. dest+=stride; \
  234. } while(--height); \
  235. }
  236. #define DEFFUNC(op,rnd,xy,sz,OP_N,avgfunc) \
  237. static void op##_##rnd##_pixels##sz##_##xy (uint8_t * dest, const uint8_t * ref, \
  238. const int stride, int height) \
  239. { \
  240. switch((int)ref&3) { \
  241. case 0:OP_N##0(sz,rnd##_##avgfunc); return; \
  242. case 1:OP_N(1,sz,rnd##_##avgfunc); return; \
  243. case 2:OP_N(2,sz,rnd##_##avgfunc); return; \
  244. case 3:OP_N(3,sz,rnd##_##avgfunc); return; \
  245. } \
  246. }
  247. #define OP put
  248. DEFFUNC(put, rnd,o,8,OP_C,avg32)
  249. DEFFUNC(put, rnd,x,8,OP_X,avg32)
  250. DEFFUNC(put,no_rnd,x,8,OP_X,avg32)
  251. DEFFUNC(put, rnd,y,8,OP_Y,avg32)
  252. DEFFUNC(put,no_rnd,y,8,OP_Y,avg32)
  253. DEFFUNC(put, rnd,xy,8,OP_XY,PACK)
  254. DEFFUNC(put,no_rnd,xy,8,OP_XY,PACK)
  255. DEFFUNC(put, rnd,o,16,OP_C,avg32)
  256. DEFFUNC(put, rnd,x,16,OP_X,avg32)
  257. DEFFUNC(put,no_rnd,x,16,OP_X,avg32)
  258. DEFFUNC(put, rnd,y,16,OP_Y,avg32)
  259. DEFFUNC(put,no_rnd,y,16,OP_Y,avg32)
  260. DEFFUNC(put, rnd,xy,16,OP_XY,PACK)
  261. DEFFUNC(put,no_rnd,xy,16,OP_XY,PACK)
  262. #undef OP
  263. #define OP avg
  264. DEFFUNC(avg, rnd,o,8,OP_C,avg32)
  265. DEFFUNC(avg, rnd,x,8,OP_X,avg32)
  266. DEFFUNC(avg,no_rnd,x,8,OP_X,avg32)
  267. DEFFUNC(avg, rnd,y,8,OP_Y,avg32)
  268. DEFFUNC(avg,no_rnd,y,8,OP_Y,avg32)
  269. DEFFUNC(avg, rnd,xy,8,OP_XY,PACK)
  270. DEFFUNC(avg,no_rnd,xy,8,OP_XY,PACK)
  271. DEFFUNC(avg, rnd,o,16,OP_C,avg32)
  272. DEFFUNC(avg, rnd,x,16,OP_X,avg32)
  273. DEFFUNC(avg,no_rnd,x,16,OP_X,avg32)
  274. DEFFUNC(avg, rnd,y,16,OP_Y,avg32)
  275. DEFFUNC(avg,no_rnd,y,16,OP_Y,avg32)
  276. DEFFUNC(avg, rnd,xy,16,OP_XY,PACK)
  277. DEFFUNC(avg,no_rnd,xy,16,OP_XY,PACK)
  278. #undef OP
  279. #define put_no_rnd_pixels8_o put_rnd_pixels8_o
  280. #define put_no_rnd_pixels16_o put_rnd_pixels16_o
  281. #define avg_no_rnd_pixels8_o avg_rnd_pixels8_o
  282. #define avg_no_rnd_pixels16_o avg_rnd_pixels16_o
  283. #define put_pixels8_c put_rnd_pixels8_o
  284. #define put_pixels16_c put_rnd_pixels16_o
  285. #define avg_pixels8_c avg_rnd_pixels8_o
  286. #define avg_pixels16_c avg_rnd_pixels16_o
  287. #define put_no_rnd_pixels8_c put_rnd_pixels8_o
  288. #define put_no_rnd_pixels16_c put_rnd_pixels16_o
  289. #define avg_no_rnd_pixels8_c avg_rnd_pixels8_o
  290. #define avg_no_rnd_pixels16_c avg_rnd_pixels16_o
  291. #define QPEL
  292. #ifdef QPEL
  293. #include "qpel.c"
  294. #endif
  295. void dsputil_init_align(DSPContext* c, AVCodecContext *avctx)
  296. {
  297. c->put_pixels_tab[0][0] = put_rnd_pixels16_o;
  298. c->put_pixels_tab[0][1] = put_rnd_pixels16_x;
  299. c->put_pixels_tab[0][2] = put_rnd_pixels16_y;
  300. c->put_pixels_tab[0][3] = put_rnd_pixels16_xy;
  301. c->put_pixels_tab[1][0] = put_rnd_pixels8_o;
  302. c->put_pixels_tab[1][1] = put_rnd_pixels8_x;
  303. c->put_pixels_tab[1][2] = put_rnd_pixels8_y;
  304. c->put_pixels_tab[1][3] = put_rnd_pixels8_xy;
  305. c->put_no_rnd_pixels_tab[0][0] = put_no_rnd_pixels16_o;
  306. c->put_no_rnd_pixels_tab[0][1] = put_no_rnd_pixels16_x;
  307. c->put_no_rnd_pixels_tab[0][2] = put_no_rnd_pixels16_y;
  308. c->put_no_rnd_pixels_tab[0][3] = put_no_rnd_pixels16_xy;
  309. c->put_no_rnd_pixels_tab[1][0] = put_no_rnd_pixels8_o;
  310. c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels8_x;
  311. c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels8_y;
  312. c->put_no_rnd_pixels_tab[1][3] = put_no_rnd_pixels8_xy;
  313. c->avg_pixels_tab[0][0] = avg_rnd_pixels16_o;
  314. c->avg_pixels_tab[0][1] = avg_rnd_pixels16_x;
  315. c->avg_pixels_tab[0][2] = avg_rnd_pixels16_y;
  316. c->avg_pixels_tab[0][3] = avg_rnd_pixels16_xy;
  317. c->avg_pixels_tab[1][0] = avg_rnd_pixels8_o;
  318. c->avg_pixels_tab[1][1] = avg_rnd_pixels8_x;
  319. c->avg_pixels_tab[1][2] = avg_rnd_pixels8_y;
  320. c->avg_pixels_tab[1][3] = avg_rnd_pixels8_xy;
  321. c->avg_no_rnd_pixels_tab[0][0] = avg_no_rnd_pixels16_o;
  322. c->avg_no_rnd_pixels_tab[0][1] = avg_no_rnd_pixels16_x;
  323. c->avg_no_rnd_pixels_tab[0][2] = avg_no_rnd_pixels16_y;
  324. c->avg_no_rnd_pixels_tab[0][3] = avg_no_rnd_pixels16_xy;
  325. c->avg_no_rnd_pixels_tab[1][0] = avg_no_rnd_pixels8_o;
  326. c->avg_no_rnd_pixels_tab[1][1] = avg_no_rnd_pixels8_x;
  327. c->avg_no_rnd_pixels_tab[1][2] = avg_no_rnd_pixels8_y;
  328. c->avg_no_rnd_pixels_tab[1][3] = avg_no_rnd_pixels8_xy;
  329. #ifdef QPEL
  330. #define dspfunc(PFX, IDX, NUM) \
  331. c->PFX ## _pixels_tab[IDX][ 0] = PFX ## NUM ## _mc00_sh4; \
  332. c->PFX ## _pixels_tab[IDX][ 1] = PFX ## NUM ## _mc10_sh4; \
  333. c->PFX ## _pixels_tab[IDX][ 2] = PFX ## NUM ## _mc20_sh4; \
  334. c->PFX ## _pixels_tab[IDX][ 3] = PFX ## NUM ## _mc30_sh4; \
  335. c->PFX ## _pixels_tab[IDX][ 4] = PFX ## NUM ## _mc01_sh4; \
  336. c->PFX ## _pixels_tab[IDX][ 5] = PFX ## NUM ## _mc11_sh4; \
  337. c->PFX ## _pixels_tab[IDX][ 6] = PFX ## NUM ## _mc21_sh4; \
  338. c->PFX ## _pixels_tab[IDX][ 7] = PFX ## NUM ## _mc31_sh4; \
  339. c->PFX ## _pixels_tab[IDX][ 8] = PFX ## NUM ## _mc02_sh4; \
  340. c->PFX ## _pixels_tab[IDX][ 9] = PFX ## NUM ## _mc12_sh4; \
  341. c->PFX ## _pixels_tab[IDX][10] = PFX ## NUM ## _mc22_sh4; \
  342. c->PFX ## _pixels_tab[IDX][11] = PFX ## NUM ## _mc32_sh4; \
  343. c->PFX ## _pixels_tab[IDX][12] = PFX ## NUM ## _mc03_sh4; \
  344. c->PFX ## _pixels_tab[IDX][13] = PFX ## NUM ## _mc13_sh4; \
  345. c->PFX ## _pixels_tab[IDX][14] = PFX ## NUM ## _mc23_sh4; \
  346. c->PFX ## _pixels_tab[IDX][15] = PFX ## NUM ## _mc33_sh4
  347. dspfunc(put_qpel, 0, 16);
  348. dspfunc(put_no_rnd_qpel, 0, 16);
  349. dspfunc(avg_qpel, 0, 16);
  350. /* dspfunc(avg_no_rnd_qpel, 0, 16); */
  351. dspfunc(put_qpel, 1, 8);
  352. dspfunc(put_no_rnd_qpel, 1, 8);
  353. dspfunc(avg_qpel, 1, 8);
  354. /* dspfunc(avg_no_rnd_qpel, 1, 8); */
  355. dspfunc(put_h264_qpel, 0, 16);
  356. dspfunc(put_h264_qpel, 1, 8);
  357. dspfunc(put_h264_qpel, 2, 4);
  358. dspfunc(avg_h264_qpel, 0, 16);
  359. dspfunc(avg_h264_qpel, 1, 8);
  360. dspfunc(avg_h264_qpel, 2, 4);
  361. #undef dspfunc
  362. c->put_h264_chroma_pixels_tab[0]= put_h264_chroma_mc8_sh4;
  363. c->put_h264_chroma_pixels_tab[1]= put_h264_chroma_mc4_sh4;
  364. c->put_h264_chroma_pixels_tab[2]= put_h264_chroma_mc2_sh4;
  365. c->avg_h264_chroma_pixels_tab[0]= avg_h264_chroma_mc8_sh4;
  366. c->avg_h264_chroma_pixels_tab[1]= avg_h264_chroma_mc4_sh4;
  367. c->avg_h264_chroma_pixels_tab[2]= avg_h264_chroma_mc2_sh4;
  368. c->put_mspel_pixels_tab[0]= put_mspel8_mc00_sh4;
  369. c->put_mspel_pixels_tab[1]= put_mspel8_mc10_sh4;
  370. c->put_mspel_pixels_tab[2]= put_mspel8_mc20_sh4;
  371. c->put_mspel_pixels_tab[3]= put_mspel8_mc30_sh4;
  372. c->put_mspel_pixels_tab[4]= put_mspel8_mc02_sh4;
  373. c->put_mspel_pixels_tab[5]= put_mspel8_mc12_sh4;
  374. c->put_mspel_pixels_tab[6]= put_mspel8_mc22_sh4;
  375. c->put_mspel_pixels_tab[7]= put_mspel8_mc32_sh4;
  376. c->gmc1 = gmc1_c;
  377. c->gmc = gmc_c;
  378. #endif
  379. }