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.

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