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.

462 lines
14KB

  1. /*
  2. * Copyright (C) 2006-2011 Michael Niedermayer <michaelni@gmx.at>
  3. * 2010 James Darnley <james.darnley@gmail.com>
  4. *
  5. * FFmpeg is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * FFmpeg is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include "libavutil/avassert.h"
  20. #include "libavutil/cpu.h"
  21. #include "libavutil/common.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "avfilter.h"
  24. #include "formats.h"
  25. #include "internal.h"
  26. #include "video.h"
  27. #include "yadif.h"
  28. #undef NDEBUG
  29. #include <assert.h>
  30. typedef struct {
  31. /**
  32. * 0: send 1 frame for each frame
  33. * 1: send 1 frame for each field
  34. * 2: like 0 but skips spatial interlacing check
  35. * 3: like 1 but skips spatial interlacing check
  36. */
  37. int mode;
  38. /**
  39. * 0: top field first
  40. * 1: bottom field first
  41. * -1: auto-detection
  42. */
  43. int parity;
  44. int frame_pending;
  45. /**
  46. * 0: deinterlace all frames
  47. * 1: only deinterlace frames marked as interlaced
  48. */
  49. int auto_enable;
  50. AVFilterBufferRef *cur;
  51. AVFilterBufferRef *next;
  52. AVFilterBufferRef *prev;
  53. AVFilterBufferRef *out;
  54. void (*filter_line)(uint8_t *dst,
  55. uint8_t *prev, uint8_t *cur, uint8_t *next,
  56. int w, int prefs, int mrefs, int parity, int mode);
  57. const AVPixFmtDescriptor *csp;
  58. int eof;
  59. } YADIFContext;
  60. #define CHECK(j)\
  61. { int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
  62. + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
  63. + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
  64. if (score < spatial_score) {\
  65. spatial_score= score;\
  66. spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
  67. #define FILTER \
  68. for (x = 0; x < w; x++) { \
  69. int c = cur[mrefs]; \
  70. int d = (prev2[0] + next2[0])>>1; \
  71. int e = cur[prefs]; \
  72. int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
  73. int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
  74. int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
  75. int diff = FFMAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2); \
  76. int spatial_pred = (c+e)>>1; \
  77. int spatial_score = FFABS(cur[mrefs-1] - cur[prefs-1]) + FFABS(c-e) \
  78. + FFABS(cur[mrefs+1] - cur[prefs+1]) - 1; \
  79. \
  80. CHECK(-1) CHECK(-2) }} }} \
  81. CHECK( 1) CHECK( 2) }} }} \
  82. \
  83. if (mode < 2) { \
  84. int b = (prev2[2*mrefs] + next2[2*mrefs])>>1; \
  85. int f = (prev2[2*prefs] + next2[2*prefs])>>1; \
  86. int max = FFMAX3(d-e, d-c, FFMIN(b-c, f-e)); \
  87. int min = FFMIN3(d-e, d-c, FFMAX(b-c, f-e)); \
  88. \
  89. diff = FFMAX3(diff, min, -max); \
  90. } \
  91. \
  92. if (spatial_pred > d + diff) \
  93. spatial_pred = d + diff; \
  94. else if (spatial_pred < d - diff) \
  95. spatial_pred = d - diff; \
  96. \
  97. dst[0] = spatial_pred; \
  98. \
  99. dst++; \
  100. cur++; \
  101. prev++; \
  102. next++; \
  103. prev2++; \
  104. next2++; \
  105. }
  106. static void filter_line_c(uint8_t *dst,
  107. uint8_t *prev, uint8_t *cur, uint8_t *next,
  108. int w, int prefs, int mrefs, int parity, int mode)
  109. {
  110. int x;
  111. uint8_t *prev2 = parity ? prev : cur ;
  112. uint8_t *next2 = parity ? cur : next;
  113. FILTER
  114. }
  115. static void filter_line_c_16bit(uint16_t *dst,
  116. uint16_t *prev, uint16_t *cur, uint16_t *next,
  117. int w, int prefs, int mrefs, int parity, int mode)
  118. {
  119. int x;
  120. uint16_t *prev2 = parity ? prev : cur ;
  121. uint16_t *next2 = parity ? cur : next;
  122. mrefs /= 2;
  123. prefs /= 2;
  124. FILTER
  125. }
  126. static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
  127. int parity, int tff)
  128. {
  129. YADIFContext *yadif = ctx->priv;
  130. int y, i;
  131. for (i = 0; i < yadif->csp->nb_components; i++) {
  132. int w = dstpic->video->w;
  133. int h = dstpic->video->h;
  134. int refs = yadif->cur->linesize[i];
  135. int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
  136. if (i == 1 || i == 2) {
  137. /* Why is this not part of the per-plane description thing? */
  138. w >>= yadif->csp->log2_chroma_w;
  139. h >>= yadif->csp->log2_chroma_h;
  140. }
  141. for (y = 0; y < h; y++) {
  142. if ((y ^ parity) & 1) {
  143. uint8_t *prev = &yadif->prev->data[i][y*refs];
  144. uint8_t *cur = &yadif->cur ->data[i][y*refs];
  145. uint8_t *next = &yadif->next->data[i][y*refs];
  146. uint8_t *dst = &dstpic->data[i][y*dstpic->linesize[i]];
  147. int mode = y==1 || y+2==h ? 2 : yadif->mode;
  148. yadif->filter_line(dst, prev, cur, next, w, y+1<h ? refs : -refs, y ? -refs : refs, parity ^ tff, mode);
  149. } else {
  150. memcpy(&dstpic->data[i][y*dstpic->linesize[i]],
  151. &yadif->cur->data[i][y*refs], w*df);
  152. }
  153. }
  154. }
  155. #if HAVE_MMX
  156. __asm__ volatile("emms \n\t" : : : "memory");
  157. #endif
  158. }
  159. static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  160. {
  161. AVFilterBufferRef *picref;
  162. int width = FFALIGN(w, 32);
  163. int height= FFALIGN(h+2, 32);
  164. int i;
  165. picref = ff_default_get_video_buffer(link, perms, width, height);
  166. picref->video->w = w;
  167. picref->video->h = h;
  168. for (i = 0; i < 3; i++)
  169. picref->data[i] += picref->linesize[i];
  170. return picref;
  171. }
  172. static void return_frame(AVFilterContext *ctx, int is_second)
  173. {
  174. YADIFContext *yadif = ctx->priv;
  175. AVFilterLink *link= ctx->outputs[0];
  176. int tff;
  177. if (yadif->parity == -1) {
  178. tff = yadif->cur->video->interlaced ?
  179. yadif->cur->video->top_field_first : 1;
  180. } else {
  181. tff = yadif->parity^1;
  182. }
  183. if (is_second) {
  184. yadif->out = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
  185. AV_PERM_REUSE, link->w, link->h);
  186. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  187. yadif->out->video->interlaced = 0;
  188. }
  189. if (!yadif->csp)
  190. yadif->csp = &av_pix_fmt_descriptors[link->format];
  191. if (yadif->csp->comp[0].depth_minus1 / 8 == 1)
  192. yadif->filter_line = (void*)filter_line_c_16bit;
  193. filter(ctx, yadif->out, tff ^ !is_second, tff);
  194. if (is_second) {
  195. int64_t cur_pts = yadif->cur->pts;
  196. int64_t next_pts = yadif->next->pts;
  197. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  198. yadif->out->pts = cur_pts + next_pts;
  199. } else {
  200. yadif->out->pts = AV_NOPTS_VALUE;
  201. }
  202. ff_start_frame(ctx->outputs[0], yadif->out);
  203. }
  204. ff_draw_slice(ctx->outputs[0], 0, link->h, 1);
  205. ff_end_frame(ctx->outputs[0]);
  206. yadif->frame_pending = (yadif->mode&1) && !is_second;
  207. }
  208. static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  209. {
  210. AVFilterContext *ctx = link->dst;
  211. YADIFContext *yadif = ctx->priv;
  212. av_assert0(picref);
  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 = avfilter_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->cur) {
  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_YUVA422P,
  330. PIX_FMT_YUVA444P,
  331. PIX_FMT_NONE
  332. };
  333. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  334. return 0;
  335. }
  336. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  337. {
  338. YADIFContext *yadif = ctx->priv;
  339. int cpu_flags = av_get_cpu_flags();
  340. yadif->mode = 0;
  341. yadif->parity = -1;
  342. yadif->auto_enable = 0;
  343. yadif->csp = NULL;
  344. if (args) sscanf(args, "%d:%d:%d", &yadif->mode, &yadif->parity, &yadif->auto_enable);
  345. yadif->filter_line = filter_line_c;
  346. if (HAVE_SSSE3 && cpu_flags & AV_CPU_FLAG_SSSE3)
  347. yadif->filter_line = ff_yadif_filter_line_ssse3;
  348. else if (HAVE_SSE && cpu_flags & AV_CPU_FLAG_SSE2)
  349. yadif->filter_line = ff_yadif_filter_line_sse2;
  350. else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX)
  351. yadif->filter_line = ff_yadif_filter_line_mmx;
  352. av_log(ctx, AV_LOG_INFO, "mode:%d parity:%d auto_enable:%d\n", yadif->mode, yadif->parity, yadif->auto_enable);
  353. return 0;
  354. }
  355. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  356. static int config_props(AVFilterLink *link)
  357. {
  358. link->time_base.num = link->src->inputs[0]->time_base.num;
  359. link->time_base.den = link->src->inputs[0]->time_base.den * 2;
  360. link->w = link->src->inputs[0]->w;
  361. link->h = link->src->inputs[0]->h;
  362. return 0;
  363. }
  364. AVFilter avfilter_vf_yadif = {
  365. .name = "yadif",
  366. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
  367. .priv_size = sizeof(YADIFContext),
  368. .init = init,
  369. .uninit = uninit,
  370. .query_formats = query_formats,
  371. .inputs = (const AVFilterPad[]) {{ .name = "default",
  372. .type = AVMEDIA_TYPE_VIDEO,
  373. .start_frame = start_frame,
  374. .get_video_buffer = get_video_buffer,
  375. .draw_slice = null_draw_slice,
  376. .end_frame = end_frame,
  377. .rej_perms = AV_PERM_REUSE2, },
  378. { .name = NULL}},
  379. .outputs = (const AVFilterPad[]) {{ .name = "default",
  380. .type = AVMEDIA_TYPE_VIDEO,
  381. .poll_frame = poll_frame,
  382. .request_frame = request_frame,
  383. .config_props = config_props, },
  384. { .name = NULL}},
  385. };