vf_detc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 "config.h"
  22. #include "mp_msg.h"
  23. #include "img_format.h"
  24. #include "mp_image.h"
  25. #include "vf.h"
  26. #include "libvo/fastmemcpy.h"
  27. struct metrics {
  28. int even;
  29. int odd;
  30. int noise;
  31. int temp;
  32. };
  33. struct vf_priv_s {
  34. int frame;
  35. int drop, lastdrop;
  36. struct metrics pm;
  37. int thres[5];
  38. int inframes, outframes;
  39. int mode;
  40. int (*analyze)(struct vf_priv_s *, mp_image_t *, mp_image_t *);
  41. int needread;
  42. };
  43. #define COMPE(a,b,e) (abs((a)-(b)) < (((a)+(b))>>(e)))
  44. #define COMPARABLE(a,b) COMPE((a),(b),2)
  45. #define VERYCLOSE(a,b) COMPE((a),(b),3)
  46. #define OUTER_TC_NBHD(s) ( \
  47. COMPARABLE((s)[-1].m.even,(s)[-1].m.odd) && \
  48. COMPARABLE((s)[1].m.even,(s)[0].m.odd) && \
  49. COMPARABLE((s)[2].m.even,(s)[1].m.odd) && \
  50. COMPARABLE((s)[-1].m.noise,(s)[0].m.temp) && \
  51. COMPARABLE((s)[2].m.noise,(s)[2].m.temp) )
  52. #define INNER_TC_NBHD(s,l,h) ( \
  53. COMPARABLE((s)[0].m.even,(l)) && \
  54. COMPARABLE((s)[2].m.odd,(l)) && ( \
  55. COMPARABLE((s)[0].m.noise,(h)) || \
  56. COMPARABLE((s)[1].m.noise,(h)) ) )
  57. enum {
  58. TC_DROP,
  59. TC_PROG,
  60. TC_IL1,
  61. TC_IL2
  62. };
  63. static void block_diffs(struct metrics *m, unsigned char *old, unsigned char *new, int os, int ns)
  64. {
  65. int x, y, even=0, odd=0, noise, temp;
  66. unsigned char *oldp, *newp;
  67. m->noise = m->temp = 0;
  68. for (x = 8; x; x--) {
  69. oldp = old++;
  70. newp = new++;
  71. noise = temp = 0;
  72. for (y = 4; y; y--) {
  73. even += abs(newp[0]-oldp[0]);
  74. odd += abs(newp[ns]-oldp[os]);
  75. noise += newp[ns]-newp[0];
  76. temp += oldp[os]-newp[0];
  77. oldp += os<<1;
  78. newp += ns<<1;
  79. }
  80. m->noise += abs(noise);
  81. m->temp += abs(temp);
  82. }
  83. m->even = even;
  84. m->odd = odd;
  85. }
  86. static void diff_planes(struct metrics *m, unsigned char *old, unsigned char *new, int w, int h, int os, int ns)
  87. {
  88. int x, y, me=0, mo=0, mn=0, mt=0;
  89. struct metrics l;
  90. for (y = 0; y < h-7; y += 8) {
  91. for (x = 0; x < w-7; x += 8) {
  92. block_diffs(&l, old+x+y*os, new+x+y*ns, os, ns);
  93. if (l.even > me) me = l.even;
  94. if (l.odd > mo) mo = l.odd;
  95. if (l.noise > mn) mn = l.noise;
  96. if (l.temp > mt) mt = l.temp;
  97. }
  98. }
  99. m->even = me;
  100. m->odd = mo;
  101. m->noise = mn;
  102. m->temp = mt;
  103. }
  104. static void diff_fields(struct metrics *metr, mp_image_t *old, mp_image_t *new)
  105. {
  106. struct metrics m, mu, mv;
  107. diff_planes(&m, old->planes[0], new->planes[0],
  108. new->w, new->h, old->stride[0], new->stride[0]);
  109. if (new->flags & MP_IMGFLAG_PLANAR) {
  110. diff_planes(&mu, old->planes[1], new->planes[1],
  111. new->chroma_width, new->chroma_height,
  112. old->stride[1], new->stride[1]);
  113. diff_planes(&mv, old->planes[2], new->planes[2],
  114. new->chroma_width, new->chroma_height,
  115. old->stride[2], new->stride[2]);
  116. if (mu.even > m.even) m.even = mu.even;
  117. if (mu.odd > m.odd) m.odd = mu.odd;
  118. if (mu.noise > m.noise) m.noise = mu.noise;
  119. if (mu.temp > m.temp) m.temp = mu.temp;
  120. if (mv.even > m.even) m.even = mv.even;
  121. if (mv.odd > m.odd) m.odd = mv.odd;
  122. if (mv.noise > m.noise) m.noise = mv.noise;
  123. if (mv.temp > m.temp) m.temp = mv.temp;
  124. }
  125. *metr = m;
  126. }
  127. static void status(int f, struct metrics *m)
  128. {
  129. mp_msg(MSGT_VFILTER, MSGL_V, "frame %d: e=%d o=%d n=%d t=%d\n",
  130. f, m->even, m->odd, m->noise, m->temp);
  131. }
  132. static int analyze_fixed_pattern(struct vf_priv_s *p, mp_image_t *new, mp_image_t *old)
  133. {
  134. if (p->frame >= 0) p->frame = (p->frame+1)%5;
  135. mp_msg(MSGT_VFILTER, MSGL_V, "frame %d\n", p->frame);
  136. switch (p->frame) {
  137. case -1: case 0: case 1: case 2:
  138. return TC_PROG;
  139. case 3:
  140. return TC_IL1;
  141. case 4:
  142. return TC_IL2;
  143. }
  144. return 0;
  145. }
  146. static int analyze_aggressive(struct vf_priv_s *p, mp_image_t *new, mp_image_t *old)
  147. {
  148. struct metrics m, pm;
  149. if (p->frame >= 0) p->frame = (p->frame+1)%5;
  150. diff_fields(&m, old, new);
  151. status(p->frame, &m);
  152. pm = p->pm;
  153. p->pm = m;
  154. if (p->frame == 4) {
  155. /* We need to break at scene changes, but is this a valid test? */
  156. if ((m.even > p->thres[2]) && (m.odd > p->thres[2]) && (m.temp > p->thres[3])
  157. && (m.temp > 5*pm.temp) && (m.temp*2 > m.noise)) {
  158. mp_msg(MSGT_VFILTER, MSGL_V, "scene change breaking telecine!\n");
  159. p->frame = -1;
  160. return TC_DROP;
  161. }
  162. /* Thres. is to compensate for quantization errors when noise is low */
  163. if (m.noise - m.temp > -p->thres[4]) {
  164. if (COMPARABLE(m.even, pm.odd)) {
  165. //mp_msg(MSGT_VFILTER, MSGL_V, "confirmed field match!\n");
  166. return TC_IL2;
  167. } else if ((m.even < p->thres[0]) && (m.odd < p->thres[0]) && VERYCLOSE(m.even, m.odd)
  168. && VERYCLOSE(m.noise,m.temp) && VERYCLOSE(m.noise,pm.noise)) {
  169. mp_msg(MSGT_VFILTER, MSGL_V, "interlaced frame appears in duplicate!!!\n");
  170. p->pm = pm; /* hack :) */
  171. p->frame = 3;
  172. return TC_IL1;
  173. }
  174. } else {
  175. mp_msg(MSGT_VFILTER, MSGL_V, "mismatched telecine fields!\n");
  176. p->frame = -1;
  177. }
  178. }
  179. if (2*m.even*m.temp < m.odd*m.noise) {
  180. mp_msg(MSGT_VFILTER, MSGL_V, "caught telecine sync!\n");
  181. p->frame = 3;
  182. return TC_IL1;
  183. }
  184. if (p->frame < 3) {
  185. if (m.noise > p->thres[3]) {
  186. if (m.noise > 2*m.temp) {
  187. mp_msg(MSGT_VFILTER, MSGL_V, "merging fields out of sequence!\n");
  188. return TC_IL2;
  189. }
  190. if ((m.noise > 2*pm.noise) && (m.even > p->thres[2]) && (m.odd > p->thres[2])) {
  191. mp_msg(MSGT_VFILTER, MSGL_V, "dropping horrible interlaced frame!\n");
  192. return TC_DROP;
  193. }
  194. }
  195. }
  196. switch (p->frame) {
  197. case -1:
  198. if (4*m.noise > 5*m.temp) {
  199. mp_msg(MSGT_VFILTER, MSGL_V, "merging fields out of sequence!\n");
  200. return TC_IL2;
  201. }
  202. case 0:
  203. case 1:
  204. case 2:
  205. return TC_PROG;
  206. case 3:
  207. if ((m.even > p->thres[1]) && (m.even > m.odd) && (m.temp > m.noise)) {
  208. mp_msg(MSGT_VFILTER, MSGL_V, "lost telecine tracking!\n");
  209. p->frame = -1;
  210. return TC_PROG;
  211. }
  212. return TC_IL1;
  213. case 4:
  214. return TC_IL2;
  215. }
  216. return 0;
  217. }
  218. static void copy_image(mp_image_t *dmpi, mp_image_t *mpi, int field)
  219. {
  220. switch (field) {
  221. case 0:
  222. my_memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h/2,
  223. dmpi->stride[0]*2, mpi->stride[0]*2);
  224. if (mpi->flags & MP_IMGFLAG_PLANAR) {
  225. my_memcpy_pic(dmpi->planes[1], mpi->planes[1],
  226. mpi->chroma_width, mpi->chroma_height/2,
  227. dmpi->stride[1]*2, mpi->stride[1]*2);
  228. my_memcpy_pic(dmpi->planes[2], mpi->planes[2],
  229. mpi->chroma_width, mpi->chroma_height/2,
  230. dmpi->stride[2]*2, mpi->stride[2]*2);
  231. }
  232. break;
  233. case 1:
  234. my_memcpy_pic(dmpi->planes[0]+dmpi->stride[0],
  235. mpi->planes[0]+mpi->stride[0], mpi->w, mpi->h/2,
  236. dmpi->stride[0]*2, mpi->stride[0]*2);
  237. if (mpi->flags & MP_IMGFLAG_PLANAR) {
  238. my_memcpy_pic(dmpi->planes[1]+dmpi->stride[1],
  239. mpi->planes[1]+mpi->stride[1],
  240. mpi->chroma_width, mpi->chroma_height/2,
  241. dmpi->stride[1]*2, mpi->stride[1]*2);
  242. my_memcpy_pic(dmpi->planes[2]+dmpi->stride[2],
  243. mpi->planes[2]+mpi->stride[2],
  244. mpi->chroma_width, mpi->chroma_height/2,
  245. dmpi->stride[2]*2, mpi->stride[2]*2);
  246. }
  247. break;
  248. case 2:
  249. memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h,
  250. dmpi->stride[0], mpi->stride[0]);
  251. if (mpi->flags & MP_IMGFLAG_PLANAR) {
  252. memcpy_pic(dmpi->planes[1], mpi->planes[1],
  253. mpi->chroma_width, mpi->chroma_height,
  254. dmpi->stride[1], mpi->stride[1]);
  255. memcpy_pic(dmpi->planes[2], mpi->planes[2],
  256. mpi->chroma_width, mpi->chroma_height,
  257. dmpi->stride[2], mpi->stride[2]);
  258. }
  259. break;
  260. }
  261. }
  262. static int do_put_image(struct vf_instance *vf, mp_image_t *dmpi)
  263. {
  264. struct vf_priv_s *p = vf->priv;
  265. int dropflag;
  266. switch (p->drop) {
  267. default:
  268. dropflag = 0;
  269. break;
  270. case 1:
  271. dropflag = (++p->lastdrop >= 5);
  272. break;
  273. case 2:
  274. dropflag = (++p->lastdrop >= 5) && (4*p->inframes <= 5*p->outframes);
  275. break;
  276. }
  277. if (dropflag) {
  278. mp_msg(MSGT_VFILTER, MSGL_V, "drop! [%d/%d=%g]\n",
  279. p->outframes, p->inframes, (float)p->outframes/p->inframes);
  280. p->lastdrop = 0;
  281. return 0;
  282. }
  283. p->outframes++;
  284. return vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
  285. }
  286. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
  287. {
  288. int ret=0;
  289. mp_image_t *dmpi;
  290. struct vf_priv_s *p = vf->priv;
  291. p->inframes++;
  292. if (p->needread) dmpi = vf_get_image(vf->next, mpi->imgfmt,
  293. MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
  294. MP_IMGFLAG_PRESERVE | MP_IMGFLAG_READABLE,
  295. mpi->width, mpi->height);
  296. /* FIXME: is there a good way to get rid of static type? */
  297. else dmpi = vf_get_image(vf->next, mpi->imgfmt,
  298. MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
  299. MP_IMGFLAG_PRESERVE, mpi->width, mpi->height);
  300. switch (p->analyze(p, mpi, dmpi)) {
  301. case TC_DROP:
  302. /* Don't copy anything unless we'll need to read it. */
  303. if (p->needread) copy_image(dmpi, mpi, 2);
  304. p->lastdrop = 0;
  305. break;
  306. case TC_PROG:
  307. /* Copy and display the whole frame. */
  308. copy_image(dmpi, mpi, 2);
  309. ret = do_put_image(vf, dmpi);
  310. break;
  311. case TC_IL1:
  312. /* Only copy bottom field unless we need to read. */
  313. if (p->needread) copy_image(dmpi, mpi, 2);
  314. else copy_image(dmpi, mpi, 1);
  315. p->lastdrop = 0;
  316. break;
  317. case TC_IL2:
  318. /* Copy top field and show frame, then copy bottom if needed. */
  319. copy_image(dmpi, mpi, 0);
  320. ret = do_put_image(vf, dmpi);
  321. if (p->needread) copy_image(dmpi, mpi, 1);
  322. break;
  323. }
  324. return ret;
  325. }
  326. static int query_format(struct vf_instance *vf, unsigned int fmt)
  327. {
  328. /* FIXME - figure out which other formats work */
  329. switch (fmt) {
  330. case IMGFMT_YV12:
  331. case IMGFMT_IYUV:
  332. case IMGFMT_I420:
  333. return vf_next_query_format(vf, fmt);
  334. }
  335. return 0;
  336. }
  337. static int config(struct vf_instance *vf,
  338. int width, int height, int d_width, int d_height,
  339. unsigned int flags, unsigned int outfmt)
  340. {
  341. return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
  342. }
  343. static void uninit(struct vf_instance *vf)
  344. {
  345. free(vf->priv);
  346. }
  347. static struct {
  348. const char *name;
  349. int (*func)(struct vf_priv_s *p, mp_image_t *new, mp_image_t *old);
  350. int needread;
  351. } anal_funcs[] = {
  352. { "fixed", analyze_fixed_pattern, 0 },
  353. { "aggressive", analyze_aggressive, 1 },
  354. { NULL, NULL, 0 }
  355. };
  356. #define STARTVARS if (0)
  357. #define GETVAR(str, name, out, func) \
  358. else if (!strncmp((str), name "=", sizeof(name))) \
  359. (out) = (func)((str) + sizeof(name))
  360. static void parse_var(struct vf_priv_s *p, char *var)
  361. {
  362. STARTVARS;
  363. GETVAR(var, "dr", p->drop, atoi);
  364. GETVAR(var, "t0", p->thres[0], atoi);
  365. GETVAR(var, "t1", p->thres[1], atoi);
  366. GETVAR(var, "t2", p->thres[2], atoi);
  367. GETVAR(var, "t3", p->thres[3], atoi);
  368. GETVAR(var, "t4", p->thres[4], atoi);
  369. GETVAR(var, "fr", p->frame, atoi);
  370. GETVAR(var, "am", p->mode, atoi);
  371. }
  372. static void parse_args(struct vf_priv_s *p, char *args)
  373. {
  374. char *next, *orig;
  375. for (args=orig=av_strdup(args); args; args=next) {
  376. next = strchr(args, ':');
  377. if (next) *next++ = 0;
  378. parse_var(p, args);
  379. }
  380. free(orig);
  381. }
  382. static int vf_open(vf_instance_t *vf, char *args)
  383. {
  384. struct vf_priv_s *p;
  385. vf->config = config;
  386. vf->put_image = put_image;
  387. vf->query_format = query_format;
  388. vf->uninit = uninit;
  389. vf->default_reqs = VFCAP_ACCEPT_STRIDE;
  390. vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
  391. p->frame = -1;
  392. p->thres[0] = 440;
  393. p->thres[1] = 720;
  394. p->thres[2] = 2500;
  395. p->thres[3] = 2500;
  396. p->thres[4] = 800;
  397. p->drop = 0;
  398. p->mode = 1;
  399. if (args) parse_args(p, args);
  400. p->analyze = anal_funcs[p->mode].func;
  401. p->needread = anal_funcs[p->mode].needread;
  402. return 1;
  403. }
  404. const vf_info_t vf_info_detc = {
  405. "de-telecine filter",
  406. "detc",
  407. "Rich Felker",
  408. "",
  409. vf_open,
  410. NULL
  411. };