You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

458 lines
14KB

  1. /*
  2. * Copyright (C) 2006-2010 Michael Niedermayer <michaelni@gmx.at>
  3. * 2010 James Darnley <james.darnley@gmail.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with Libav; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #include "libavutil/cpu.h"
  22. #include "libavutil/common.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "avfilter.h"
  25. #include "formats.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. #include "yadif.h"
  29. #undef NDEBUG
  30. #include <assert.h>
  31. typedef struct {
  32. /**
  33. * 0: send 1 frame for each frame
  34. * 1: send 1 frame for each field
  35. * 2: like 0 but skips spatial interlacing check
  36. * 3: like 1 but skips spatial interlacing check
  37. */
  38. int mode;
  39. /**
  40. * 0: top field first
  41. * 1: bottom field first
  42. * -1: auto-detection
  43. */
  44. int parity;
  45. int frame_pending;
  46. /**
  47. * 0: deinterlace all frames
  48. * 1: only deinterlace frames marked as interlaced
  49. */
  50. int auto_enable;
  51. AVFilterBufferRef *cur;
  52. AVFilterBufferRef *next;
  53. AVFilterBufferRef *prev;
  54. AVFilterBufferRef *out;
  55. void (*filter_line)(uint8_t *dst,
  56. uint8_t *prev, uint8_t *cur, uint8_t *next,
  57. int w, int prefs, int mrefs, int parity, int mode);
  58. const AVPixFmtDescriptor *csp;
  59. int eof;
  60. } YADIFContext;
  61. #define CHECK(j)\
  62. { int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
  63. + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
  64. + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
  65. if (score < spatial_score) {\
  66. spatial_score= score;\
  67. spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
  68. #define FILTER \
  69. for (x = 0; x < w; x++) { \
  70. int c = cur[mrefs]; \
  71. int d = (prev2[0] + next2[0])>>1; \
  72. int e = cur[prefs]; \
  73. int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
  74. int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
  75. int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
  76. int diff = FFMAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2); \
  77. int spatial_pred = (c+e)>>1; \
  78. int spatial_score = FFABS(cur[mrefs-1] - cur[prefs-1]) + FFABS(c-e) \
  79. + FFABS(cur[mrefs+1] - cur[prefs+1]) - 1; \
  80. \
  81. CHECK(-1) CHECK(-2) }} }} \
  82. CHECK( 1) CHECK( 2) }} }} \
  83. \
  84. if (mode < 2) { \
  85. int b = (prev2[2*mrefs] + next2[2*mrefs])>>1; \
  86. int f = (prev2[2*prefs] + next2[2*prefs])>>1; \
  87. int max = FFMAX3(d-e, d-c, FFMIN(b-c, f-e)); \
  88. int min = FFMIN3(d-e, d-c, FFMAX(b-c, f-e)); \
  89. \
  90. diff = FFMAX3(diff, min, -max); \
  91. } \
  92. \
  93. if (spatial_pred > d + diff) \
  94. spatial_pred = d + diff; \
  95. else if (spatial_pred < d - diff) \
  96. spatial_pred = d - diff; \
  97. \
  98. dst[0] = spatial_pred; \
  99. \
  100. dst++; \
  101. cur++; \
  102. prev++; \
  103. next++; \
  104. prev2++; \
  105. next2++; \
  106. }
  107. static void filter_line_c(uint8_t *dst,
  108. uint8_t *prev, uint8_t *cur, uint8_t *next,
  109. int w, int prefs, int mrefs, int parity, int mode)
  110. {
  111. int x;
  112. uint8_t *prev2 = parity ? prev : cur ;
  113. uint8_t *next2 = parity ? cur : next;
  114. FILTER
  115. }
  116. static void filter_line_c_16bit(uint16_t *dst,
  117. uint16_t *prev, uint16_t *cur, uint16_t *next,
  118. int w, int prefs, int mrefs, int parity, int mode)
  119. {
  120. int x;
  121. uint16_t *prev2 = parity ? prev : cur ;
  122. uint16_t *next2 = parity ? cur : next;
  123. mrefs /= 2;
  124. prefs /= 2;
  125. FILTER
  126. }
  127. static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
  128. int parity, int tff)
  129. {
  130. YADIFContext *yadif = ctx->priv;
  131. int y, i;
  132. for (i = 0; i < yadif->csp->nb_components; i++) {
  133. int w = dstpic->video->w;
  134. int h = dstpic->video->h;
  135. int refs = yadif->cur->linesize[i];
  136. int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
  137. if (i == 1 || i == 2) {
  138. /* Why is this not part of the per-plane description thing? */
  139. w >>= yadif->csp->log2_chroma_w;
  140. h >>= yadif->csp->log2_chroma_h;
  141. }
  142. for (y = 0; y < h; y++) {
  143. if ((y ^ parity) & 1) {
  144. uint8_t *prev = &yadif->prev->data[i][y*refs];
  145. uint8_t *cur = &yadif->cur ->data[i][y*refs];
  146. uint8_t *next = &yadif->next->data[i][y*refs];
  147. uint8_t *dst = &dstpic->data[i][y*dstpic->linesize[i]];
  148. int mode = y==1 || y+2==h ? 2 : yadif->mode;
  149. yadif->filter_line(dst, prev, cur, next, w, y+1<h ? refs : -refs, y ? -refs : refs, parity ^ tff, mode);
  150. } else {
  151. memcpy(&dstpic->data[i][y*dstpic->linesize[i]],
  152. &yadif->cur->data[i][y*refs], w*df);
  153. }
  154. }
  155. }
  156. #if HAVE_MMX
  157. __asm__ volatile("emms \n\t" : : : "memory");
  158. #endif
  159. }
  160. static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  161. {
  162. AVFilterBufferRef *picref;
  163. int width = FFALIGN(w, 32);
  164. int height= FFALIGN(h+2, 32);
  165. int i;
  166. picref = ff_default_get_video_buffer(link, perms, width, height);
  167. picref->video->w = w;
  168. picref->video->h = h;
  169. for (i = 0; i < 3; i++)
  170. picref->data[i] += picref->linesize[i];
  171. return picref;
  172. }
  173. static void return_frame(AVFilterContext *ctx, int is_second)
  174. {
  175. YADIFContext *yadif = ctx->priv;
  176. AVFilterLink *link= ctx->outputs[0];
  177. int tff;
  178. if (yadif->parity == -1) {
  179. tff = yadif->cur->video->interlaced ?
  180. yadif->cur->video->top_field_first : 1;
  181. } else {
  182. tff = yadif->parity^1;
  183. }
  184. if (is_second) {
  185. yadif->out = ff_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
  186. AV_PERM_REUSE, link->w, link->h);
  187. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  188. yadif->out->video->interlaced = 0;
  189. }
  190. if (!yadif->csp)
  191. yadif->csp = &av_pix_fmt_descriptors[link->format];
  192. if (yadif->csp->comp[0].depth_minus1 / 8 == 1)
  193. yadif->filter_line = filter_line_c_16bit;
  194. filter(ctx, yadif->out, tff ^ !is_second, tff);
  195. if (is_second) {
  196. int64_t cur_pts = yadif->cur->pts;
  197. int64_t next_pts = yadif->next->pts;
  198. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  199. yadif->out->pts = cur_pts + next_pts;
  200. } else {
  201. yadif->out->pts = AV_NOPTS_VALUE;
  202. }
  203. ff_start_frame(ctx->outputs[0], yadif->out);
  204. }
  205. ff_draw_slice(ctx->outputs[0], 0, link->h, 1);
  206. ff_end_frame(ctx->outputs[0]);
  207. yadif->frame_pending = (yadif->mode&1) && !is_second;
  208. }
  209. static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  210. {
  211. AVFilterContext *ctx = link->dst;
  212. YADIFContext *yadif = ctx->priv;
  213. if (yadif->frame_pending)
  214. return_frame(ctx, 1);
  215. if (yadif->prev)
  216. avfilter_unref_buffer(yadif->prev);
  217. yadif->prev = yadif->cur;
  218. yadif->cur = yadif->next;
  219. yadif->next = picref;
  220. if (!yadif->cur)
  221. return;
  222. if (yadif->auto_enable && !yadif->cur->video->interlaced) {
  223. yadif->out = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
  224. avfilter_unref_buffer(yadif->prev);
  225. yadif->prev = NULL;
  226. if (yadif->out->pts != AV_NOPTS_VALUE)
  227. yadif->out->pts *= 2;
  228. ff_start_frame(ctx->outputs[0], yadif->out);
  229. return;
  230. }
  231. if (!yadif->prev)
  232. yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
  233. yadif->out = ff_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
  234. AV_PERM_REUSE, link->w, link->h);
  235. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  236. yadif->out->video->interlaced = 0;
  237. if (yadif->out->pts != AV_NOPTS_VALUE)
  238. yadif->out->pts *= 2;
  239. ff_start_frame(ctx->outputs[0], yadif->out);
  240. }
  241. static void end_frame(AVFilterLink *link)
  242. {
  243. AVFilterContext *ctx = link->dst;
  244. YADIFContext *yadif = ctx->priv;
  245. if (!yadif->out)
  246. return;
  247. if (yadif->auto_enable && !yadif->cur->video->interlaced) {
  248. ff_draw_slice(ctx->outputs[0], 0, link->h, 1);
  249. ff_end_frame(ctx->outputs[0]);
  250. return;
  251. }
  252. return_frame(ctx, 0);
  253. }
  254. static int request_frame(AVFilterLink *link)
  255. {
  256. AVFilterContext *ctx = link->src;
  257. YADIFContext *yadif = ctx->priv;
  258. if (yadif->frame_pending) {
  259. return_frame(ctx, 1);
  260. return 0;
  261. }
  262. do {
  263. int ret;
  264. if (yadif->eof)
  265. return AVERROR_EOF;
  266. ret = ff_request_frame(link->src->inputs[0]);
  267. if (ret == AVERROR_EOF && yadif->next) {
  268. AVFilterBufferRef *next = avfilter_ref_buffer(yadif->next, AV_PERM_READ);
  269. next->pts = yadif->next->pts * 2 - yadif->cur->pts;
  270. start_frame(link->src->inputs[0], next);
  271. end_frame(link->src->inputs[0]);
  272. yadif->eof = 1;
  273. } else if (ret < 0) {
  274. return ret;
  275. }
  276. } while (!yadif->cur);
  277. return 0;
  278. }
  279. static int poll_frame(AVFilterLink *link)
  280. {
  281. YADIFContext *yadif = link->src->priv;
  282. int ret, val;
  283. if (yadif->frame_pending)
  284. return 1;
  285. val = ff_poll_frame(link->src->inputs[0]);
  286. if (val <= 0)
  287. return val;
  288. if (val==1 && !yadif->next) { //FIXME change API to not requre this red tape
  289. if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
  290. return ret;
  291. val = ff_poll_frame(link->src->inputs[0]);
  292. if (val <= 0)
  293. return val;
  294. }
  295. assert(yadif->next || !val);
  296. if (yadif->auto_enable && yadif->next && !yadif->next->video->interlaced)
  297. return val;
  298. return val * ((yadif->mode&1)+1);
  299. }
  300. static av_cold void uninit(AVFilterContext *ctx)
  301. {
  302. YADIFContext *yadif = ctx->priv;
  303. if (yadif->prev) avfilter_unref_buffer(yadif->prev);
  304. if (yadif->cur ) avfilter_unref_buffer(yadif->cur );
  305. if (yadif->next) avfilter_unref_buffer(yadif->next);
  306. }
  307. static int query_formats(AVFilterContext *ctx)
  308. {
  309. static const enum PixelFormat pix_fmts[] = {
  310. PIX_FMT_YUV420P,
  311. PIX_FMT_YUV422P,
  312. PIX_FMT_YUV444P,
  313. PIX_FMT_YUV410P,
  314. PIX_FMT_YUV411P,
  315. PIX_FMT_GRAY8,
  316. PIX_FMT_YUVJ420P,
  317. PIX_FMT_YUVJ422P,
  318. PIX_FMT_YUVJ444P,
  319. AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
  320. PIX_FMT_YUV440P,
  321. PIX_FMT_YUVJ440P,
  322. AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
  323. AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
  324. AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
  325. AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
  326. AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
  327. AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
  328. PIX_FMT_YUVA420P,
  329. PIX_FMT_NONE
  330. };
  331. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  332. return 0;
  333. }
  334. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  335. {
  336. YADIFContext *yadif = ctx->priv;
  337. int cpu_flags = av_get_cpu_flags();
  338. yadif->mode = 0;
  339. yadif->parity = -1;
  340. yadif->auto_enable = 0;
  341. yadif->csp = NULL;
  342. if (args) sscanf(args, "%d:%d:%d", &yadif->mode, &yadif->parity, &yadif->auto_enable);
  343. yadif->filter_line = filter_line_c;
  344. if (HAVE_SSSE3 && cpu_flags & AV_CPU_FLAG_SSSE3)
  345. yadif->filter_line = ff_yadif_filter_line_ssse3;
  346. else if (HAVE_SSE && cpu_flags & AV_CPU_FLAG_SSE2)
  347. yadif->filter_line = ff_yadif_filter_line_sse2;
  348. else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX)
  349. yadif->filter_line = ff_yadif_filter_line_mmx;
  350. av_log(ctx, AV_LOG_INFO, "mode:%d parity:%d auto_enable:%d\n", yadif->mode, yadif->parity, yadif->auto_enable);
  351. return 0;
  352. }
  353. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  354. static int config_props(AVFilterLink *link)
  355. {
  356. link->time_base.num = link->src->inputs[0]->time_base.num;
  357. link->time_base.den = link->src->inputs[0]->time_base.den * 2;
  358. link->w = link->src->inputs[0]->w;
  359. link->h = link->src->inputs[0]->h;
  360. return 0;
  361. }
  362. AVFilter avfilter_vf_yadif = {
  363. .name = "yadif",
  364. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
  365. .priv_size = sizeof(YADIFContext),
  366. .init = init,
  367. .uninit = uninit,
  368. .query_formats = query_formats,
  369. .inputs = (AVFilterPad[]) {{ .name = "default",
  370. .type = AVMEDIA_TYPE_VIDEO,
  371. .start_frame = start_frame,
  372. .get_video_buffer = get_video_buffer,
  373. .draw_slice = null_draw_slice,
  374. .end_frame = end_frame, },
  375. { .name = NULL}},
  376. .outputs = (AVFilterPad[]) {{ .name = "default",
  377. .type = AVMEDIA_TYPE_VIDEO,
  378. .poll_frame = poll_frame,
  379. .request_frame = request_frame,
  380. .config_props = config_props, },
  381. { .name = NULL}},
  382. };