vf_tile.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * filter to tile a serie of image in a single, bigger, image
  3. *
  4. * The parameters are:
  5. *
  6. * xtile: number of tile on the x axis (5)
  7. * ytile: number of tile on the y axis (5)
  8. * xytile: when write the image, it can be different then xtile * ytile
  9. * (for example you can write 8 * 7 tile, writing the file every
  10. * 50 frame, to have one image every 2 seconds @ 25 fps ).
  11. * start: pixel at the start (x/y), default 2
  12. * delta: pixel between 2 tile, (x/y), default 4
  13. *
  14. * For example a valid command line is:
  15. * ... -vf tile=10:5:-1:4:8 ...
  16. * that make images of 10 * 5 tiles, with 4 pixel at the beginning and
  17. * 8 pixel between tiles.
  18. *
  19. * The default command is:
  20. * ... -vf tile=5:5:25:2:4
  21. *
  22. * If you omit a parameter or put a value less then 0, the default is used.
  23. * ... -vf tile=10:5::-1:10
  24. *
  25. * You can also stop when you're ok
  26. * ... -vf tile=10:5
  27. * (and this is probably the option you will use more often ...)
  28. *
  29. * Probably is good to put the scale filter before the tile :-)
  30. *
  31. * copyright (c) 2003 Daniele Forghieri ( guru@digitalfantasy.it )
  32. *
  33. * This file is part of MPlayer.
  34. *
  35. * MPlayer is free software; you can redistribute it and/or modify
  36. * it under the terms of the GNU General Public License as published by
  37. * the Free Software Foundation; either version 2 of the License, or
  38. * (at your option) any later version.
  39. *
  40. * MPlayer is distributed in the hope that it will be useful,
  41. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  42. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  43. * GNU General Public License for more details.
  44. *
  45. * You should have received a copy of the GNU General Public License along
  46. * with MPlayer; if not, write to the Free Software Foundation, Inc.,
  47. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  48. */
  49. // strtoi memcpy_pic
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include "config.h"
  54. #include "mp_msg.h"
  55. #include "help_mp.h"
  56. #include "cpudetect.h"
  57. #include "img_format.h"
  58. #include "mp_image.h"
  59. #include "vf.h"
  60. #include "libvo/fastmemcpy.h"
  61. /* private data */
  62. struct vf_priv_s {
  63. /* configuration data */
  64. /* Number on hor/ver tiles */
  65. int xtile;
  66. int ytile;
  67. /* When write the whole frame (default = xtile * ytile) */
  68. int xytile;
  69. /* pixel at start / end (default = 4) */
  70. int start;
  71. /* pixel between image (default = 2) */
  72. int delta;
  73. // /* Background color, in destination format */
  74. // int bkgSet;
  75. /* Work data */
  76. int frame_cur;
  77. };
  78. static int config(struct vf_instance *vf,
  79. int width, int height, int d_width, int d_height,
  80. unsigned int flags, unsigned int outfmt){
  81. struct vf_priv_s *priv;
  82. int xw;
  83. int yh;
  84. /* Calculate new destination size */
  85. priv = vf->priv;
  86. xw = priv->start * 2 +
  87. priv->xtile * width +
  88. (priv->xtile - 1) * priv->delta;
  89. yh = priv->start * 2 +
  90. priv->ytile * height +
  91. (priv->ytile - 1) * priv->delta;
  92. mp_msg(MSGT_VFILTER,MSGL_V,"vf_tile:config size set to %d * %d\n", xw, yh);
  93. return vf_next_config(vf, xw, yh, xw, yh, flags, outfmt);
  94. }
  95. /* Filter handler */
  96. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
  97. {
  98. mp_image_t *dmpi;
  99. struct vf_priv_s *priv;
  100. int t;
  101. int xw;
  102. int yh;
  103. int xi;
  104. int yi;
  105. int by;
  106. int dw;
  107. /* Calculate new size */
  108. priv = vf->priv;
  109. xw = priv->start * 2 +
  110. priv->xtile * mpi->w +
  111. (priv->xtile - 1) * priv->delta;
  112. yh = priv->start * 2 +
  113. priv->ytile * mpi->h+
  114. (priv->ytile - 1) * priv->delta;
  115. /* Get the big image! */
  116. dmpi=vf_get_image(vf->next, mpi->imgfmt,
  117. MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE,
  118. xw, yh);
  119. /* bytes x pixel & bytes x line */
  120. if (mpi->flags & MP_IMGFLAG_PLANAR) {
  121. by = 1;
  122. dw = mpi->w;
  123. }
  124. else {
  125. by = (mpi->bpp + 7) / 8;
  126. dw = mpi->w * by;
  127. }
  128. /* Index position */
  129. t = priv->frame_cur % priv->xytile;
  130. // if ((t == 0) && (bkg != 0)) {
  131. // /* First frame, delete the background */
  132. //
  133. // }
  134. /* Position of image */
  135. xi = priv->start + (mpi->w + priv->delta) * (t % priv->xtile);
  136. yi = priv->start + (mpi->h + priv->delta) * (t / priv->xtile);
  137. /* Copy first (or only) plane */
  138. memcpy_pic( dmpi->planes[0] + xi * by + yi * dmpi->stride[0],
  139. mpi->planes[0],
  140. dw,
  141. mpi->h,
  142. dmpi->stride[0],
  143. mpi->stride[0]);
  144. if (mpi->flags & MP_IMGFLAG_PLANAR) {
  145. /* Copy the other 2 planes */
  146. memcpy_pic( dmpi->planes[1] + (xi >> mpi->chroma_x_shift) + (yi >> mpi->chroma_y_shift) * dmpi->stride[1],
  147. mpi->planes[1],
  148. mpi->chroma_width,
  149. mpi->chroma_height,
  150. dmpi->stride[1],
  151. mpi->stride[1]);
  152. memcpy_pic( dmpi->planes[2] + (xi >> mpi->chroma_x_shift) + (yi >> mpi->chroma_y_shift) * dmpi->stride[2],
  153. mpi->planes[2],
  154. mpi->chroma_width,
  155. mpi->chroma_height,
  156. dmpi->stride[2],
  157. mpi->stride[2]);
  158. }
  159. /* Increment current frame */
  160. ++priv->frame_cur;
  161. if (t == priv->xytile - 1) {
  162. /* Display the composition */
  163. dmpi->width = xw;
  164. dmpi->height = yh;
  165. return vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
  166. }
  167. else {
  168. /* Skip the frame */
  169. return 0;
  170. }
  171. }
  172. static void uninit(struct vf_instance *vf)
  173. {
  174. /* free local data */
  175. free(vf->priv);
  176. }
  177. /* rgb/bgr 12...32 supported & some Yxxx */
  178. static int query_format(struct vf_instance *vf, unsigned int fmt)
  179. {
  180. switch (fmt) {
  181. /* rgb 12...32 bit */
  182. case IMGFMT_RGB12:
  183. case IMGFMT_RGB15:
  184. case IMGFMT_RGB16:
  185. case IMGFMT_RGB24:
  186. case IMGFMT_RGB32:
  187. /* bgr 12...32 bit */
  188. case IMGFMT_BGR12:
  189. case IMGFMT_BGR15:
  190. case IMGFMT_BGR16:
  191. case IMGFMT_BGR24:
  192. case IMGFMT_BGR32:
  193. /* Various Yxxx Formats */
  194. case IMGFMT_444P:
  195. case IMGFMT_422P:
  196. case IMGFMT_411P:
  197. case IMGFMT_YUY2:
  198. case IMGFMT_YV12:
  199. case IMGFMT_I420:
  200. case IMGFMT_YVU9:
  201. case IMGFMT_IF09:
  202. case IMGFMT_IYUV:
  203. return vf_next_query_format(vf, fmt);
  204. }
  205. return 0;
  206. }
  207. /* Get an integer from the string pointed by s, adjusting s.
  208. * If the value is less then 0 def_val is used.
  209. * Return 0 for ok
  210. *
  211. * Look below ( in vf_open(...) ) for a use ...
  212. */
  213. static int parse_int(char **s, int *rt, int def_val)
  214. {
  215. int t = 0;
  216. if (**s) {
  217. /* Get value (dec, hex or octal) */
  218. t = strtol( *s, s, 0 );
  219. /* Use default */
  220. if (t < 0) {
  221. t = def_val;
  222. }
  223. if (**s == ':') {
  224. /* Point to next character (problably a digit) */
  225. ++(*s);
  226. }
  227. else if (**s != '\0') {
  228. /* Error, we got some wrong char */
  229. return 1;
  230. }
  231. }
  232. else {
  233. t = def_val;
  234. }
  235. *rt = t;
  236. return 0;
  237. }
  238. /* Main entry funct for the filter */
  239. static int vf_open(vf_instance_t *vf, char *args)
  240. {
  241. struct vf_priv_s *p;
  242. int er;
  243. vf->put_image = put_image;
  244. vf->query_format = query_format;
  245. vf->config = config;
  246. vf->uninit = uninit;
  247. vf->default_reqs = VFCAP_ACCEPT_STRIDE;
  248. /* Private data */
  249. vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
  250. if (p == NULL) {
  251. return 0;
  252. }
  253. if (args == NULL) {
  254. /* Use the default */
  255. args = "";
  256. }
  257. /* Parse all the arguments */
  258. er = parse_int( &args, &p->xtile, 5 );
  259. er |= parse_int( &args, &p->ytile, 5 );
  260. er |= parse_int( &args, &p->xytile, 0 );
  261. er |= parse_int( &args, &p->start, 2 );
  262. er |= parse_int( &args, &p->delta, 4 );
  263. // er |= parse_int( &args, &p->bkgSet, 0 );
  264. if (er) {
  265. mp_msg(MSGT_VFILTER, MSGL_ERR, MSGTR_MPCODECS_ErrorParsingArgument);
  266. return 0;
  267. }
  268. /* Load some default */
  269. if ((p->xytile <= 0) || (p->xytile > p->xtile * p->ytile)) {
  270. p->xytile = p->xtile * p->ytile;
  271. }
  272. /* Say what happen: use mp_msg(...)? */
  273. if ( mp_msg_test(MSGT_VFILTER,MSGL_V) ) {
  274. printf("vf_tile: tiling %d * %d, output every %d frames\n",
  275. p->xtile,
  276. p->ytile,
  277. p->xytile);
  278. printf("vf_tile: start pixel %d, delta pixel %d\n",
  279. p->start,
  280. p->delta);
  281. // printf("vf_tile: background 0x%x\n",
  282. // p->bkgSet);
  283. }
  284. return 1;
  285. }
  286. const vf_info_t vf_info_tile = {
  287. "Make a single image tiling x/y images",
  288. "tile",
  289. "Daniele Forghieri",
  290. "",
  291. vf_open,
  292. NULL
  293. };