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.

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