vf_2xsai.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * This file is part of MPlayer.
  3. *
  4. * MPlayer is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * MPlayer is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with MPlayer; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <inttypes.h>
  22. #include "config.h"
  23. #include "mp_msg.h"
  24. #include "img_format.h"
  25. #include "mp_image.h"
  26. #include "vf.h"
  27. //===========================================================================//
  28. /* FIXME: these all belong in the context, not as globals! */
  29. static uint32_t colorMask = 0xF7DEF7DE;
  30. static uint32_t lowPixelMask = 0x08210821;
  31. static uint32_t qcolorMask = 0xE79CE79C;
  32. static uint32_t qlowpixelMask = 0x18631863;
  33. static uint32_t redblueMask = 0xF81F;
  34. static uint32_t greenMask = 0x7E0;
  35. static int PixelsPerMask = 2;
  36. #define makecol(r,g,b) (r+(g<<8)+(b<<16))
  37. #define makecol_depth(d,r,g,b) (r+(g<<8)+(b<<16))
  38. static int Init_2xSaI(int d)
  39. {
  40. int minr = 0, ming = 0, minb = 0;
  41. int i;
  42. // if (d != 15 && d != 16 && d != 24 && d != 32)
  43. // return -1;
  44. /* Get lowest color bit */
  45. for (i = 0; i < 255; i++) {
  46. if (!minr)
  47. minr = makecol(i, 0, 0);
  48. if (!ming)
  49. ming = makecol(0, i, 0);
  50. if (!minb)
  51. minb = makecol(0, 0, i);
  52. }
  53. colorMask = (makecol_depth(d, 255, 0, 0) - minr) | (makecol_depth(d, 0, 255, 0) - ming) | (makecol_depth(d, 0, 0, 255) - minb);
  54. lowPixelMask = minr | ming | minb;
  55. qcolorMask = (makecol_depth(d, 255, 0, 0) - 3 * minr) | (makecol_depth(d, 0, 255, 0) - 3 * ming) | (makecol_depth(d, 0, 0, 255) - 3 * minb);
  56. qlowpixelMask = (minr * 3) | (ming * 3) | (minb * 3);
  57. redblueMask = makecol_depth(d, 255, 0, 255);
  58. greenMask = makecol_depth(d, 0, 255, 0);
  59. PixelsPerMask = (d <= 16) ? 2 : 1;
  60. if (PixelsPerMask == 2) {
  61. colorMask |= (colorMask << 16);
  62. qcolorMask |= (qcolorMask << 16);
  63. lowPixelMask |= (lowPixelMask << 16);
  64. qlowpixelMask |= (qlowpixelMask << 16);
  65. }
  66. // TRACE("Color Mask: 0x%lX\n", colorMask);
  67. // TRACE("Low Pixel Mask: 0x%lX\n", lowPixelMask);
  68. // TRACE("QColor Mask: 0x%lX\n", qcolorMask);
  69. // TRACE("QLow Pixel Mask: 0x%lX\n", qlowpixelMask);
  70. return 0;
  71. }
  72. #define GET_RESULT(A, B, C, D) ((A != C || A != D) - (B != C || B != D))
  73. #define INTERPOLATE(A, B) (((A & colorMask) >> 1) + ((B & colorMask) >> 1) + (A & B & lowPixelMask))
  74. #define Q_INTERPOLATE(A, B, C, D) ((A & qcolorMask) >> 2) + ((B & qcolorMask) >> 2) + ((C & qcolorMask) >> 2) + ((D & qcolorMask) >> 2) \
  75. + ((((A & qlowpixelMask) + (B & qlowpixelMask) + (C & qlowpixelMask) + (D & qlowpixelMask)) >> 2) & qlowpixelMask)
  76. static void Super2xSaI_ex(uint8_t *src, uint32_t src_pitch,
  77. uint8_t *dst, uint32_t dst_pitch,
  78. uint32_t width, uint32_t height, int sbpp)
  79. {
  80. unsigned int x, y;
  81. uint32_t color[16];
  82. unsigned char *src_line[4];
  83. /* Point to the first 3 lines. */
  84. src_line[0] = src;
  85. src_line[1] = src;
  86. src_line[2] = src + src_pitch;
  87. src_line[3] = src + src_pitch * 2;
  88. x = 0, y = 0;
  89. if (PixelsPerMask == 2) {
  90. unsigned short *sbp;
  91. sbp = (unsigned short*)src_line[0];
  92. color[0] = *sbp; color[1] = color[0]; color[2] = color[0]; color[3] = color[0];
  93. color[4] = color[0]; color[5] = color[0]; color[6] = *(sbp + 1); color[7] = *(sbp + 2);
  94. sbp = (unsigned short*)src_line[2];
  95. color[8] = *sbp; color[9] = color[8]; color[10] = *(sbp + 1); color[11] = *(sbp + 2);
  96. sbp = (unsigned short*)src_line[3];
  97. color[12] = *sbp; color[13] = color[12]; color[14] = *(sbp + 1); color[15] = *(sbp + 2);
  98. }
  99. else {
  100. uint32_t *lbp;
  101. lbp = (uint32_t*)src_line[0];
  102. color[0] = *lbp; color[1] = color[0]; color[2] = color[0]; color[3] = color[0];
  103. color[4] = color[0]; color[5] = color[0]; color[6] = *(lbp + 1); color[7] = *(lbp + 2);
  104. lbp = (uint32_t*)src_line[2];
  105. color[8] = *lbp; color[9] = color[8]; color[10] = *(lbp + 1); color[11] = *(lbp + 2);
  106. lbp = (uint32_t*)src_line[3];
  107. color[12] = *lbp; color[13] = color[12]; color[14] = *(lbp + 1); color[15] = *(lbp + 2);
  108. }
  109. for (y = 0; y < height; y++) {
  110. unsigned char *dst_line[2];
  111. dst_line[0] = dst + dst_pitch*2*y;
  112. dst_line[1] = dst + dst_pitch*(2*y+1);
  113. /* Todo: x = width - 2, x = width - 1 */
  114. for (x = 0; x < width; x++) {
  115. uint32_t product1a, product1b, product2a, product2b;
  116. //--------------------------------------- B0 B1 B2 B3 0 1 2 3
  117. // 4 5* 6 S2 -> 4 5* 6 7
  118. // 1 2 3 S1 8 9 10 11
  119. // A0 A1 A2 A3 12 13 14 15
  120. //--------------------------------------
  121. if (color[9] == color[6] && color[5] != color[10]) {
  122. product2b = color[9];
  123. product1b = product2b;
  124. }
  125. else if (color[5] == color[10] && color[9] != color[6]) {
  126. product2b = color[5];
  127. product1b = product2b;
  128. }
  129. else if (color[5] == color[10] && color[9] == color[6]) {
  130. int r = 0;
  131. r += GET_RESULT(color[6], color[5], color[8], color[13]);
  132. r += GET_RESULT(color[6], color[5], color[4], color[1]);
  133. r += GET_RESULT(color[6], color[5], color[14], color[11]);
  134. r += GET_RESULT(color[6], color[5], color[2], color[7]);
  135. if (r > 0)
  136. product1b = color[6];
  137. else if (r < 0)
  138. product1b = color[5];
  139. else
  140. product1b = INTERPOLATE(color[5], color[6]);
  141. product2b = product1b;
  142. }
  143. else {
  144. if (color[6] == color[10] && color[10] == color[13] && color[9] != color[14] && color[10] != color[12])
  145. product2b = Q_INTERPOLATE(color[10], color[10], color[10], color[9]);
  146. else if (color[5] == color[9] && color[9] == color[14] && color[13] != color[10] && color[9] != color[15])
  147. product2b = Q_INTERPOLATE(color[9], color[9], color[9], color[10]);
  148. else
  149. product2b = INTERPOLATE(color[9], color[10]);
  150. if (color[6] == color[10] && color[6] == color[1] && color[5] != color[2] && color[6] != color[0])
  151. product1b = Q_INTERPOLATE(color[6], color[6], color[6], color[5]);
  152. else if (color[5] == color[9] && color[5] == color[2] && color[1] != color[6] && color[5] != color[3])
  153. product1b = Q_INTERPOLATE(color[6], color[5], color[5], color[5]);
  154. else
  155. product1b = INTERPOLATE(color[5], color[6]);
  156. }
  157. if (color[5] == color[10] && color[9] != color[6] && color[4] == color[5] && color[5] != color[14])
  158. product2a = INTERPOLATE(color[9], color[5]);
  159. else if (color[5] == color[8] && color[6] == color[5] && color[4] != color[9] && color[5] != color[12])
  160. product2a = INTERPOLATE(color[9], color[5]);
  161. else
  162. product2a = color[9];
  163. if (color[9] == color[6] && color[5] != color[10] && color[8] == color[9] && color[9] != color[2])
  164. product1a = INTERPOLATE(color[9], color[5]);
  165. else if (color[4] == color[9] && color[10] == color[9] && color[8] != color[5] && color[9] != color[0])
  166. product1a = INTERPOLATE(color[9], color[5]);
  167. else
  168. product1a = color[5];
  169. if (PixelsPerMask == 2) {
  170. *((uint32_t *) (&dst_line[0][x * 4])) = product1a | (product1b << 16);
  171. *((uint32_t *) (&dst_line[1][x * 4])) = product2a | (product2b << 16);
  172. }
  173. else {
  174. *((uint32_t *) (&dst_line[0][x * 8])) = product1a;
  175. *((uint32_t *) (&dst_line[0][x * 8 + 4])) = product1b;
  176. *((uint32_t *) (&dst_line[1][x * 8])) = product2a;
  177. *((uint32_t *) (&dst_line[1][x * 8 + 4])) = product2b;
  178. }
  179. /* Move color matrix forward */
  180. color[0] = color[1]; color[4] = color[5]; color[8] = color[9]; color[12] = color[13];
  181. color[1] = color[2]; color[5] = color[6]; color[9] = color[10]; color[13] = color[14];
  182. color[2] = color[3]; color[6] = color[7]; color[10] = color[11]; color[14] = color[15];
  183. if (x < width - 3) {
  184. x += 3;
  185. if (PixelsPerMask == 2) {
  186. color[3] = *(((unsigned short*)src_line[0]) + x);
  187. color[7] = *(((unsigned short*)src_line[1]) + x);
  188. color[11] = *(((unsigned short*)src_line[2]) + x);
  189. color[15] = *(((unsigned short*)src_line[3]) + x);
  190. }
  191. else {
  192. color[3] = *(((uint32_t*)src_line[0]) + x);
  193. color[7] = *(((uint32_t*)src_line[1]) + x);
  194. color[11] = *(((uint32_t*)src_line[2]) + x);
  195. color[15] = *(((uint32_t*)src_line[3]) + x);
  196. }
  197. x -= 3;
  198. }
  199. }
  200. /* We're done with one line, so we shift the source lines up */
  201. src_line[0] = src_line[1];
  202. src_line[1] = src_line[2];
  203. src_line[2] = src_line[3];
  204. /* Read next line */
  205. if (y + 3 >= height)
  206. src_line[3] = src_line[2];
  207. else
  208. src_line[3] = src_line[2] + src_pitch;
  209. /* Then shift the color matrix up */
  210. if (PixelsPerMask == 2) {
  211. unsigned short *sbp;
  212. sbp = (unsigned short*)src_line[0];
  213. color[0] = *sbp; color[1] = color[0]; color[2] = *(sbp + 1); color[3] = *(sbp + 2);
  214. sbp = (unsigned short*)src_line[1];
  215. color[4] = *sbp; color[5] = color[4]; color[6] = *(sbp + 1); color[7] = *(sbp + 2);
  216. sbp = (unsigned short*)src_line[2];
  217. color[8] = *sbp; color[9] = color[9]; color[10] = *(sbp + 1); color[11] = *(sbp + 2);
  218. sbp = (unsigned short*)src_line[3];
  219. color[12] = *sbp; color[13] = color[12]; color[14] = *(sbp + 1); color[15] = *(sbp + 2);
  220. }
  221. else {
  222. uint32_t *lbp;
  223. lbp = (uint32_t*)src_line[0];
  224. color[0] = *lbp; color[1] = color[0]; color[2] = *(lbp + 1); color[3] = *(lbp + 2);
  225. lbp = (uint32_t*)src_line[1];
  226. color[4] = *lbp; color[5] = color[4]; color[6] = *(lbp + 1); color[7] = *(lbp + 2);
  227. lbp = (uint32_t*)src_line[2];
  228. color[8] = *lbp; color[9] = color[9]; color[10] = *(lbp + 1); color[11] = *(lbp + 2);
  229. lbp = (uint32_t*)src_line[3];
  230. color[12] = *lbp; color[13] = color[12]; color[14] = *(lbp + 1); color[15] = *(lbp + 2);
  231. }
  232. } // y loop
  233. }
  234. //===========================================================================//
  235. static int config(struct vf_instance *vf,
  236. int width, int height, int d_width, int d_height,
  237. unsigned int flags, unsigned int outfmt){
  238. Init_2xSaI(outfmt&255);
  239. return vf_next_config(vf,2*width,2*height,2*d_width,2*d_height,flags,outfmt);
  240. }
  241. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
  242. mp_image_t *dmpi;
  243. // hope we'll get DR buffer:
  244. dmpi=vf_get_image(vf->next,mpi->imgfmt,
  245. MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
  246. 2*mpi->w, 2*mpi->h);
  247. Super2xSaI_ex(mpi->planes[0], mpi->stride[0],
  248. dmpi->planes[0], dmpi->stride[0],
  249. mpi->w, mpi->h, mpi->bpp/8);
  250. return vf_next_put_image(vf,dmpi, pts);
  251. }
  252. //===========================================================================//
  253. static int query_format(struct vf_instance *vf, unsigned int fmt){
  254. switch(fmt){
  255. // case IMGFMT_BGR15:
  256. // case IMGFMT_BGR16:
  257. case IMGFMT_BGR32:
  258. return vf_next_query_format(vf,fmt);
  259. }
  260. return 0;
  261. }
  262. static int vf_open(vf_instance_t *vf, char *args){
  263. vf->config=config;
  264. vf->put_image=put_image;
  265. vf->query_format=query_format;
  266. return 1;
  267. }
  268. const vf_info_t vf_info_2xsai = {
  269. "2xSai BGR bitmap 2x scaler",
  270. "2xsai",
  271. "A'rpi",
  272. "http://elektron.its.tudelft.nl/~dalikifa/",
  273. vf_open,
  274. NULL
  275. };
  276. //===========================================================================//