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.

514 lines
17KB

  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/opt.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. #define PERM_RWP AV_PERM_WRITE | AV_PERM_PRESERVE | AV_PERM_REUSE
  32. #define CHECK(j)\
  33. { int score = FFABS(cur[mrefs + off_left + (j)] - cur[prefs + off_left - (j)])\
  34. + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
  35. + FFABS(cur[mrefs + off_right + (j)] - cur[prefs + off_right - (j)]);\
  36. if (score < spatial_score) {\
  37. spatial_score= score;\
  38. spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
  39. #define FILTER(start, end) \
  40. for (x = start; x < end; x++) { \
  41. int c = cur[mrefs]; \
  42. int d = (prev2[0] + next2[0])>>1; \
  43. int e = cur[prefs]; \
  44. int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
  45. int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
  46. int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
  47. int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
  48. int spatial_pred = (c+e) >> 1; \
  49. int off_right = (x < w - 1) ? 1 : -1;\
  50. int off_left = x ? -1 : 1;\
  51. int spatial_score = FFABS(cur[mrefs + off_left] - cur[prefs + off_left]) + FFABS(c-e) \
  52. + FFABS(cur[mrefs + off_right] - cur[prefs + off_right]) - 1; \
  53. \
  54. if (x > 2 && x < w - 3) {\
  55. CHECK(-1) CHECK(-2) }} }} \
  56. CHECK( 1) CHECK( 2) }} }} \
  57. }\
  58. \
  59. if (mode < 2) { \
  60. int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
  61. int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
  62. int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
  63. int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
  64. \
  65. diff = FFMAX3(diff, min, -max); \
  66. } \
  67. \
  68. if (spatial_pred > d + diff) \
  69. spatial_pred = d + diff; \
  70. else if (spatial_pred < d - diff) \
  71. spatial_pred = d - diff; \
  72. \
  73. dst[0] = spatial_pred; \
  74. \
  75. dst++; \
  76. cur++; \
  77. prev++; \
  78. next++; \
  79. prev2++; \
  80. next2++; \
  81. }
  82. static void filter_line_c(void *dst1,
  83. void *prev1, void *cur1, void *next1,
  84. int w, int prefs, int mrefs, int parity, int mode)
  85. {
  86. uint8_t *dst = dst1;
  87. uint8_t *prev = prev1;
  88. uint8_t *cur = cur1;
  89. uint8_t *next = next1;
  90. int x;
  91. uint8_t *prev2 = parity ? prev : cur ;
  92. uint8_t *next2 = parity ? cur : next;
  93. FILTER(0, w)
  94. }
  95. static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1,
  96. int w, int prefs, int mrefs, int parity, int mode,
  97. int l_edge)
  98. {
  99. uint8_t *dst = dst1;
  100. uint8_t *prev = prev1;
  101. uint8_t *cur = cur1;
  102. uint8_t *next = next1;
  103. int x;
  104. uint8_t *prev2 = parity ? prev : cur ;
  105. uint8_t *next2 = parity ? cur : next;
  106. FILTER(0, l_edge)
  107. dst = (uint8_t*)dst1 + w - 3;
  108. prev = (uint8_t*)prev1 + w - 3;
  109. cur = (uint8_t*)cur1 + w - 3;
  110. next = (uint8_t*)next1 + w - 3;
  111. prev2 = (uint8_t*)(parity ? prev : cur);
  112. next2 = (uint8_t*)(parity ? cur : next);
  113. FILTER(w - 3, w)
  114. }
  115. static void filter_line_c_16bit(void *dst1,
  116. void *prev1, void *cur1, void *next1,
  117. int w, int prefs, int mrefs, int parity,
  118. int mode)
  119. {
  120. uint16_t *dst = dst1;
  121. uint16_t *prev = prev1;
  122. uint16_t *cur = cur1;
  123. uint16_t *next = next1;
  124. int x;
  125. uint16_t *prev2 = parity ? prev : cur ;
  126. uint16_t *next2 = parity ? cur : next;
  127. mrefs /= 2;
  128. prefs /= 2;
  129. FILTER(0, w)
  130. }
  131. static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,
  132. int w, int prefs, int mrefs, int parity, int mode,
  133. int l_edge)
  134. {
  135. uint16_t *dst = dst1;
  136. uint16_t *prev = prev1;
  137. uint16_t *cur = cur1;
  138. uint16_t *next = next1;
  139. int x;
  140. uint16_t *prev2 = parity ? prev : cur ;
  141. uint16_t *next2 = parity ? cur : next;
  142. FILTER(0, l_edge)
  143. dst = (uint16_t*)dst1 + w - 3;
  144. prev = (uint16_t*)prev1 + w - 3;
  145. cur = (uint16_t*)cur1 + w - 3;
  146. next = (uint16_t*)next1 + w - 3;
  147. prev2 = (uint16_t*)(parity ? prev : cur);
  148. next2 = (uint16_t*)(parity ? cur : next);
  149. FILTER(w - 3, w)
  150. }
  151. static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
  152. int parity, int tff)
  153. {
  154. YADIFContext *yadif = ctx->priv;
  155. int y, i;
  156. for (i = 0; i < yadif->csp->nb_components; i++) {
  157. int w = dstpic->video->w;
  158. int h = dstpic->video->h;
  159. int refs = yadif->cur->linesize[i];
  160. int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
  161. int l_edge, l_edge_pix;
  162. if (i == 1 || i == 2) {
  163. /* Why is this not part of the per-plane description thing? */
  164. w >>= yadif->csp->log2_chroma_w;
  165. h >>= yadif->csp->log2_chroma_h;
  166. }
  167. /* filtering reads 3 pixels to the left/right; to avoid invalid reads,
  168. * we need to call the c variant which avoids this for border pixels
  169. */
  170. l_edge = yadif->req_align;
  171. l_edge_pix = l_edge / df;
  172. for (y = 0; y < h; y++) {
  173. if ((y ^ parity) & 1) {
  174. uint8_t *prev = &yadif->prev->data[i][y * refs];
  175. uint8_t *cur = &yadif->cur ->data[i][y * refs];
  176. uint8_t *next = &yadif->next->data[i][y * refs];
  177. uint8_t *dst = &dstpic->data[i][y * dstpic->linesize[i]];
  178. int mode = y == 1 || y + 2 == h ? 2 : yadif->mode;
  179. if (yadif->req_align) {
  180. yadif->filter_line(dst + l_edge, prev + l_edge, cur + l_edge,
  181. next + l_edge, w - l_edge_pix - 3,
  182. y + 1 < h ? refs : -refs,
  183. y ? -refs : refs,
  184. parity ^ tff, mode);
  185. yadif->filter_edges(dst, prev, cur, next, w,
  186. y + 1 < h ? refs : -refs,
  187. y ? -refs : refs,
  188. parity ^ tff, mode, l_edge_pix);
  189. } else {
  190. yadif->filter_line(dst, prev, cur, next + l_edge, w,
  191. y + 1 < h ? refs : -refs,
  192. y ? -refs : refs,
  193. parity ^ tff, mode);
  194. }
  195. } else {
  196. memcpy(&dstpic->data[i][y * dstpic->linesize[i]],
  197. &yadif->cur->data[i][y * refs], w * df);
  198. }
  199. }
  200. }
  201. emms_c();
  202. }
  203. static int return_frame(AVFilterContext *ctx, int is_second)
  204. {
  205. YADIFContext *yadif = ctx->priv;
  206. AVFilterLink *link = ctx->outputs[0];
  207. int tff, ret;
  208. if (yadif->parity == -1) {
  209. tff = yadif->cur->video->interlaced ?
  210. yadif->cur->video->top_field_first : 1;
  211. } else {
  212. tff = yadif->parity ^ 1;
  213. }
  214. if (is_second) {
  215. yadif->out = ff_get_video_buffer(link, PERM_RWP, link->w, link->h);
  216. if (!yadif->out)
  217. return AVERROR(ENOMEM);
  218. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  219. yadif->out->video->interlaced = 0;
  220. }
  221. filter(ctx, yadif->out, tff ^ !is_second, tff);
  222. if (is_second) {
  223. int64_t cur_pts = yadif->cur->pts;
  224. int64_t next_pts = yadif->next->pts;
  225. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  226. yadif->out->pts = cur_pts + next_pts;
  227. } else {
  228. yadif->out->pts = AV_NOPTS_VALUE;
  229. }
  230. }
  231. ret = ff_filter_frame(ctx->outputs[0], yadif->out);
  232. yadif->frame_pending = (yadif->mode&1) && !is_second;
  233. return ret;
  234. }
  235. static int filter_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  236. {
  237. AVFilterContext *ctx = link->dst;
  238. YADIFContext *yadif = ctx->priv;
  239. av_assert0(picref);
  240. if (yadif->frame_pending)
  241. return_frame(ctx, 1);
  242. if (yadif->prev)
  243. avfilter_unref_buffer(yadif->prev);
  244. yadif->prev = yadif->cur;
  245. yadif->cur = yadif->next;
  246. yadif->next = picref;
  247. if (!yadif->cur)
  248. return 0;
  249. if (yadif->deint && !yadif->cur->video->interlaced) {
  250. yadif->out = avfilter_ref_buffer(yadif->cur, ~AV_PERM_WRITE);
  251. if (!yadif->out)
  252. return AVERROR(ENOMEM);
  253. avfilter_unref_bufferp(&yadif->prev);
  254. if (yadif->out->pts != AV_NOPTS_VALUE)
  255. yadif->out->pts *= 2;
  256. return ff_filter_frame(ctx->outputs[0], yadif->out);
  257. }
  258. if (!yadif->prev &&
  259. !(yadif->prev = avfilter_ref_buffer(yadif->cur, ~AV_PERM_WRITE)))
  260. return AVERROR(ENOMEM);
  261. yadif->out = ff_get_video_buffer(ctx->outputs[0], PERM_RWP,
  262. link->w, link->h);
  263. if (!yadif->out)
  264. return AVERROR(ENOMEM);
  265. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  266. yadif->out->video->interlaced = 0;
  267. if (yadif->out->pts != AV_NOPTS_VALUE)
  268. yadif->out->pts *= 2;
  269. return return_frame(ctx, 0);
  270. }
  271. static int request_frame(AVFilterLink *link)
  272. {
  273. AVFilterContext *ctx = link->src;
  274. YADIFContext *yadif = ctx->priv;
  275. if (yadif->frame_pending) {
  276. return_frame(ctx, 1);
  277. return 0;
  278. }
  279. do {
  280. int ret;
  281. if (yadif->eof)
  282. return AVERROR_EOF;
  283. ret = ff_request_frame(link->src->inputs[0]);
  284. if (ret == AVERROR_EOF && yadif->cur) {
  285. AVFilterBufferRef *next = avfilter_ref_buffer(yadif->next, ~AV_PERM_WRITE);
  286. if (!next)
  287. return AVERROR(ENOMEM);
  288. next->pts = yadif->next->pts * 2 - yadif->cur->pts;
  289. filter_frame(link->src->inputs[0], next);
  290. yadif->eof = 1;
  291. } else if (ret < 0) {
  292. return ret;
  293. }
  294. } while (!yadif->cur);
  295. return 0;
  296. }
  297. #define OFFSET(x) offsetof(YADIFContext, x)
  298. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  299. #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
  300. static const AVOption yadif_options[] = {
  301. { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, "mode"},
  302. CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"),
  303. CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"),
  304. CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"),
  305. CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"),
  306. { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, "parity" },
  307. CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"),
  308. CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"),
  309. CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"),
  310. { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, "deint" },
  311. CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"),
  312. CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"),
  313. {NULL},
  314. };
  315. AVFILTER_DEFINE_CLASS(yadif);
  316. static av_cold void uninit(AVFilterContext *ctx)
  317. {
  318. YADIFContext *yadif = ctx->priv;
  319. avfilter_unref_bufferp(&yadif->prev);
  320. avfilter_unref_bufferp(&yadif->cur );
  321. avfilter_unref_bufferp(&yadif->next);
  322. av_opt_free(yadif);
  323. }
  324. static int query_formats(AVFilterContext *ctx)
  325. {
  326. static const enum AVPixelFormat pix_fmts[] = {
  327. AV_PIX_FMT_YUV420P,
  328. AV_PIX_FMT_YUV422P,
  329. AV_PIX_FMT_YUV444P,
  330. AV_PIX_FMT_YUV410P,
  331. AV_PIX_FMT_YUV411P,
  332. AV_PIX_FMT_GRAY8,
  333. AV_PIX_FMT_YUVJ420P,
  334. AV_PIX_FMT_YUVJ422P,
  335. AV_PIX_FMT_YUVJ444P,
  336. AV_NE( AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE ),
  337. AV_PIX_FMT_YUV440P,
  338. AV_PIX_FMT_YUVJ440P,
  339. AV_NE( AV_PIX_FMT_YUV420P9BE, AV_PIX_FMT_YUV420P9LE ),
  340. AV_NE( AV_PIX_FMT_YUV422P9BE, AV_PIX_FMT_YUV422P9LE ),
  341. AV_NE( AV_PIX_FMT_YUV444P9BE, AV_PIX_FMT_YUV444P9LE ),
  342. AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
  343. AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
  344. AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
  345. AV_NE( AV_PIX_FMT_YUV420P12BE, AV_PIX_FMT_YUV420P12LE ),
  346. AV_NE( AV_PIX_FMT_YUV422P12BE, AV_PIX_FMT_YUV422P12LE ),
  347. AV_NE( AV_PIX_FMT_YUV444P12BE, AV_PIX_FMT_YUV444P12LE ),
  348. AV_NE( AV_PIX_FMT_YUV420P14BE, AV_PIX_FMT_YUV420P14LE ),
  349. AV_NE( AV_PIX_FMT_YUV422P14BE, AV_PIX_FMT_YUV422P14LE ),
  350. AV_NE( AV_PIX_FMT_YUV444P14BE, AV_PIX_FMT_YUV444P14LE ),
  351. AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
  352. AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
  353. AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
  354. AV_PIX_FMT_YUVA420P,
  355. AV_PIX_FMT_YUVA422P,
  356. AV_PIX_FMT_YUVA444P,
  357. AV_PIX_FMT_NONE
  358. };
  359. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  360. return 0;
  361. }
  362. static av_cold int init(AVFilterContext *ctx, const char *args)
  363. {
  364. YADIFContext *yadif = ctx->priv;
  365. static const char *shorthand[] = { "mode", "parity", "deint", NULL };
  366. int ret;
  367. yadif->class = &yadif_class;
  368. av_opt_set_defaults(yadif);
  369. if ((ret = av_opt_set_from_string(yadif, args, shorthand, "=", ":")) < 0)
  370. return ret;
  371. av_log(ctx, AV_LOG_VERBOSE, "mode:%d parity:%d deint:%d\n",
  372. yadif->mode, yadif->parity, yadif->deint);
  373. return 0;
  374. }
  375. static int config_props(AVFilterLink *link)
  376. {
  377. AVFilterContext *ctx = link->src;
  378. YADIFContext *s = link->src->priv;
  379. link->time_base.num = link->src->inputs[0]->time_base.num;
  380. link->time_base.den = link->src->inputs[0]->time_base.den * 2;
  381. link->w = link->src->inputs[0]->w;
  382. link->h = link->src->inputs[0]->h;
  383. if(s->mode&1)
  384. link->frame_rate = av_mul_q(link->src->inputs[0]->frame_rate, (AVRational){2,1});
  385. if (link->w < 3 || link->h < 3) {
  386. av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
  387. return AVERROR(EINVAL);
  388. }
  389. s->csp = av_pix_fmt_desc_get(link->format);
  390. if (s->csp->comp[0].depth_minus1 / 8 == 1) {
  391. s->filter_line = filter_line_c_16bit;
  392. s->filter_edges = filter_edges_16bit;
  393. } else {
  394. s->filter_line = filter_line_c;
  395. s->filter_edges = filter_edges;
  396. if (ARCH_X86)
  397. ff_yadif_init_x86(s);
  398. }
  399. return 0;
  400. }
  401. static const AVFilterPad avfilter_vf_yadif_inputs[] = {
  402. {
  403. .name = "default",
  404. .type = AVMEDIA_TYPE_VIDEO,
  405. .filter_frame = filter_frame,
  406. .min_perms = AV_PERM_PRESERVE,
  407. },
  408. { NULL }
  409. };
  410. static const AVFilterPad avfilter_vf_yadif_outputs[] = {
  411. {
  412. .name = "default",
  413. .type = AVMEDIA_TYPE_VIDEO,
  414. .request_frame = request_frame,
  415. .config_props = config_props,
  416. },
  417. { NULL }
  418. };
  419. AVFilter avfilter_vf_yadif = {
  420. .name = "yadif",
  421. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
  422. .priv_size = sizeof(YADIFContext),
  423. .init = init,
  424. .uninit = uninit,
  425. .query_formats = query_formats,
  426. .inputs = avfilter_vf_yadif_inputs,
  427. .outputs = avfilter_vf_yadif_outputs,
  428. .priv_class = &yadif_class,
  429. };