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.

510 lines
16KB

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