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.

516 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. typedef struct ThreadData {
  32. AVFrame *frame;
  33. int plane;
  34. int w, h;
  35. int parity;
  36. int tff;
  37. } ThreadData;
  38. #define CHECK(j)\
  39. { int score = FFABS(cur[mrefs - 1 + (j)] - cur[prefs - 1 - (j)])\
  40. + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
  41. + FFABS(cur[mrefs + 1 + (j)] - cur[prefs + 1 - (j)]);\
  42. if (score < spatial_score) {\
  43. spatial_score= score;\
  44. spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
  45. /* The is_not_edge argument here controls when the code will enter a branch
  46. * which reads up to and including x-3 and x+3. */
  47. #define FILTER(start, end, is_not_edge) \
  48. for (x = start; x < end; x++) { \
  49. int c = cur[mrefs]; \
  50. int d = (prev2[0] + next2[0])>>1; \
  51. int e = cur[prefs]; \
  52. int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
  53. int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
  54. int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
  55. int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
  56. int spatial_pred = (c+e) >> 1; \
  57. \
  58. if (is_not_edge) {\
  59. int spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
  60. + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
  61. CHECK(-1) CHECK(-2) }} }} \
  62. CHECK( 1) CHECK( 2) }} }} \
  63. }\
  64. \
  65. if (mode < 2) { \
  66. int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
  67. int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
  68. int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
  69. int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
  70. \
  71. diff = FFMAX3(diff, min, -max); \
  72. } \
  73. \
  74. if (spatial_pred > d + diff) \
  75. spatial_pred = d + diff; \
  76. else if (spatial_pred < d - diff) \
  77. spatial_pred = d - diff; \
  78. \
  79. dst[0] = spatial_pred; \
  80. \
  81. dst++; \
  82. cur++; \
  83. prev++; \
  84. next++; \
  85. prev2++; \
  86. next2++; \
  87. }
  88. static void filter_line_c(void *dst1,
  89. void *prev1, void *cur1, void *next1,
  90. int w, int prefs, int mrefs, int parity, int mode)
  91. {
  92. uint8_t *dst = dst1;
  93. uint8_t *prev = prev1;
  94. uint8_t *cur = cur1;
  95. uint8_t *next = next1;
  96. int x;
  97. uint8_t *prev2 = parity ? prev : cur ;
  98. uint8_t *next2 = parity ? cur : next;
  99. /* The function is called with the pointers already pointing to data[3] and
  100. * with 6 subtracted from the width. This allows the FILTER macro to be
  101. * called so that it processes all the pixels normally. A constant value of
  102. * true for is_not_edge lets the compiler ignore the if statement. */
  103. FILTER(0, w, 1)
  104. }
  105. #define MAX_ALIGN 8
  106. static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1,
  107. int w, int prefs, int mrefs, int parity, int mode)
  108. {
  109. uint8_t *dst = dst1;
  110. uint8_t *prev = prev1;
  111. uint8_t *cur = cur1;
  112. uint8_t *next = next1;
  113. int x;
  114. uint8_t *prev2 = parity ? prev : cur ;
  115. uint8_t *next2 = parity ? cur : next;
  116. /* Only edge pixels need to be processed here. A constant value of false
  117. * for is_not_edge should let the compiler ignore the whole branch. */
  118. FILTER(0, 3, 0)
  119. dst = (uint8_t*)dst1 + w - (MAX_ALIGN-1);
  120. prev = (uint8_t*)prev1 + w - (MAX_ALIGN-1);
  121. cur = (uint8_t*)cur1 + w - (MAX_ALIGN-1);
  122. next = (uint8_t*)next1 + w - (MAX_ALIGN-1);
  123. prev2 = (uint8_t*)(parity ? prev : cur);
  124. next2 = (uint8_t*)(parity ? cur : next);
  125. FILTER(w - (MAX_ALIGN-1), w - 3, 1)
  126. FILTER(w - 3, w, 0)
  127. }
  128. static void filter_line_c_16bit(void *dst1,
  129. void *prev1, void *cur1, void *next1,
  130. int w, int prefs, int mrefs, int parity,
  131. int mode)
  132. {
  133. uint16_t *dst = dst1;
  134. uint16_t *prev = prev1;
  135. uint16_t *cur = cur1;
  136. uint16_t *next = next1;
  137. int x;
  138. uint16_t *prev2 = parity ? prev : cur ;
  139. uint16_t *next2 = parity ? cur : next;
  140. mrefs /= 2;
  141. prefs /= 2;
  142. FILTER(0, w, 1)
  143. }
  144. static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,
  145. int w, int prefs, int mrefs, int parity, int mode)
  146. {
  147. uint16_t *dst = dst1;
  148. uint16_t *prev = prev1;
  149. uint16_t *cur = cur1;
  150. uint16_t *next = next1;
  151. int x;
  152. uint16_t *prev2 = parity ? prev : cur ;
  153. uint16_t *next2 = parity ? cur : next;
  154. mrefs /= 2;
  155. prefs /= 2;
  156. FILTER(0, 3, 0)
  157. dst = (uint16_t*)dst1 + w - (MAX_ALIGN/2-1);
  158. prev = (uint16_t*)prev1 + w - (MAX_ALIGN/2-1);
  159. cur = (uint16_t*)cur1 + w - (MAX_ALIGN/2-1);
  160. next = (uint16_t*)next1 + w - (MAX_ALIGN/2-1);
  161. prev2 = (uint16_t*)(parity ? prev : cur);
  162. next2 = (uint16_t*)(parity ? cur : next);
  163. FILTER(w - (MAX_ALIGN/2-1), w - 3, 1)
  164. FILTER(w - 3, w, 0)
  165. }
  166. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  167. {
  168. YADIFContext *s = ctx->priv;
  169. ThreadData *td = arg;
  170. int refs = s->cur->linesize[td->plane];
  171. int df = (s->csp->comp[td->plane].depth_minus1 + 8) / 8;
  172. int pix_3 = 3 * df;
  173. int slice_start = (td->h * jobnr ) / nb_jobs;
  174. int slice_end = (td->h * (jobnr+1)) / nb_jobs;
  175. int y;
  176. /* filtering reads 3 pixels to the left/right; to avoid invalid reads,
  177. * we need to call the c variant which avoids this for border pixels
  178. */
  179. for (y = slice_start; y < slice_end; y++) {
  180. if ((y ^ td->parity) & 1) {
  181. uint8_t *prev = &s->prev->data[td->plane][y * refs];
  182. uint8_t *cur = &s->cur ->data[td->plane][y * refs];
  183. uint8_t *next = &s->next->data[td->plane][y * refs];
  184. uint8_t *dst = &td->frame->data[td->plane][y * td->frame->linesize[td->plane]];
  185. int mode = y == 1 || y + 2 == td->h ? 2 : s->mode;
  186. s->filter_line(dst + pix_3, prev + pix_3, cur + pix_3,
  187. next + pix_3, td->w - (3 + MAX_ALIGN/df-1),
  188. y + 1 < td->h ? refs : -refs,
  189. y ? -refs : refs,
  190. td->parity ^ td->tff, mode);
  191. s->filter_edges(dst, prev, cur, next, td->w,
  192. y + 1 < td->h ? refs : -refs,
  193. y ? -refs : refs,
  194. td->parity ^ td->tff, mode);
  195. } else {
  196. memcpy(&td->frame->data[td->plane][y * td->frame->linesize[td->plane]],
  197. &s->cur->data[td->plane][y * refs], td->w * df);
  198. }
  199. }
  200. return 0;
  201. }
  202. static void filter(AVFilterContext *ctx, AVFrame *dstpic,
  203. int parity, int tff)
  204. {
  205. YADIFContext *yadif = ctx->priv;
  206. ThreadData td = { .frame = dstpic, .parity = parity, .tff = tff };
  207. int i;
  208. for (i = 0; i < yadif->csp->nb_components; i++) {
  209. int w = dstpic->width;
  210. int h = dstpic->height;
  211. if (i == 1 || i == 2) {
  212. w = FF_CEIL_RSHIFT(w, yadif->csp->log2_chroma_w);
  213. h = FF_CEIL_RSHIFT(h, yadif->csp->log2_chroma_h);
  214. }
  215. td.w = w;
  216. td.h = h;
  217. td.plane = i;
  218. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h, ctx->graph->nb_threads));
  219. }
  220. emms_c();
  221. }
  222. static int return_frame(AVFilterContext *ctx, int is_second)
  223. {
  224. YADIFContext *yadif = ctx->priv;
  225. AVFilterLink *link = ctx->outputs[0];
  226. int tff, ret;
  227. if (yadif->parity == -1) {
  228. tff = yadif->cur->interlaced_frame ?
  229. yadif->cur->top_field_first : 1;
  230. } else {
  231. tff = yadif->parity ^ 1;
  232. }
  233. if (is_second) {
  234. yadif->out = ff_get_video_buffer(link, link->w, link->h);
  235. if (!yadif->out)
  236. return AVERROR(ENOMEM);
  237. av_frame_copy_props(yadif->out, yadif->cur);
  238. yadif->out->interlaced_frame = 0;
  239. }
  240. filter(ctx, yadif->out, tff ^ !is_second, tff);
  241. if (is_second) {
  242. int64_t cur_pts = yadif->cur->pts;
  243. int64_t next_pts = yadif->next->pts;
  244. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  245. yadif->out->pts = cur_pts + next_pts;
  246. } else {
  247. yadif->out->pts = AV_NOPTS_VALUE;
  248. }
  249. }
  250. ret = ff_filter_frame(ctx->outputs[0], yadif->out);
  251. yadif->frame_pending = (yadif->mode&1) && !is_second;
  252. return ret;
  253. }
  254. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  255. {
  256. AVFilterContext *ctx = link->dst;
  257. YADIFContext *yadif = ctx->priv;
  258. av_assert0(frame);
  259. if (yadif->frame_pending)
  260. return_frame(ctx, 1);
  261. if (yadif->prev)
  262. av_frame_free(&yadif->prev);
  263. yadif->prev = yadif->cur;
  264. yadif->cur = yadif->next;
  265. yadif->next = frame;
  266. if (!yadif->cur)
  267. return 0;
  268. if ((yadif->deint && !yadif->cur->interlaced_frame) || ctx->is_disabled) {
  269. yadif->out = av_frame_clone(yadif->cur);
  270. if (!yadif->out)
  271. return AVERROR(ENOMEM);
  272. av_frame_free(&yadif->prev);
  273. if (yadif->out->pts != AV_NOPTS_VALUE)
  274. yadif->out->pts *= 2;
  275. return ff_filter_frame(ctx->outputs[0], yadif->out);
  276. }
  277. if (!yadif->prev &&
  278. !(yadif->prev = av_frame_clone(yadif->cur)))
  279. return AVERROR(ENOMEM);
  280. yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
  281. if (!yadif->out)
  282. return AVERROR(ENOMEM);
  283. av_frame_copy_props(yadif->out, yadif->cur);
  284. yadif->out->interlaced_frame = 0;
  285. if (yadif->out->pts != AV_NOPTS_VALUE)
  286. yadif->out->pts *= 2;
  287. return return_frame(ctx, 0);
  288. }
  289. static int request_frame(AVFilterLink *link)
  290. {
  291. AVFilterContext *ctx = link->src;
  292. YADIFContext *yadif = ctx->priv;
  293. if (yadif->frame_pending) {
  294. return_frame(ctx, 1);
  295. return 0;
  296. }
  297. do {
  298. int ret;
  299. if (yadif->eof)
  300. return AVERROR_EOF;
  301. ret = ff_request_frame(link->src->inputs[0]);
  302. if (ret == AVERROR_EOF && yadif->cur) {
  303. AVFrame *next = av_frame_clone(yadif->next);
  304. if (!next)
  305. return AVERROR(ENOMEM);
  306. next->pts = yadif->next->pts * 2 - yadif->cur->pts;
  307. filter_frame(link->src->inputs[0], next);
  308. yadif->eof = 1;
  309. } else if (ret < 0) {
  310. return ret;
  311. }
  312. } while (!yadif->cur);
  313. return 0;
  314. }
  315. static av_cold void uninit(AVFilterContext *ctx)
  316. {
  317. YADIFContext *yadif = ctx->priv;
  318. av_frame_free(&yadif->prev);
  319. av_frame_free(&yadif->cur );
  320. av_frame_free(&yadif->next);
  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 int config_props(AVFilterLink *link)
  361. {
  362. AVFilterContext *ctx = link->src;
  363. YADIFContext *s = link->src->priv;
  364. link->time_base.num = link->src->inputs[0]->time_base.num;
  365. link->time_base.den = link->src->inputs[0]->time_base.den * 2;
  366. link->w = link->src->inputs[0]->w;
  367. link->h = link->src->inputs[0]->h;
  368. if(s->mode&1)
  369. link->frame_rate = av_mul_q(link->src->inputs[0]->frame_rate, (AVRational){2,1});
  370. if (link->w < 3 || link->h < 3) {
  371. av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
  372. return AVERROR(EINVAL);
  373. }
  374. s->csp = av_pix_fmt_desc_get(link->format);
  375. if (s->csp->comp[0].depth_minus1 / 8 == 1) {
  376. s->filter_line = filter_line_c_16bit;
  377. s->filter_edges = filter_edges_16bit;
  378. } else {
  379. s->filter_line = filter_line_c;
  380. s->filter_edges = filter_edges;
  381. }
  382. if (ARCH_X86)
  383. ff_yadif_init_x86(s);
  384. return 0;
  385. }
  386. #define OFFSET(x) offsetof(YADIFContext, x)
  387. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  388. #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
  389. static const AVOption yadif_options[] = {
  390. { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, "mode"},
  391. CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"),
  392. CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"),
  393. CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"),
  394. CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"),
  395. { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, "parity" },
  396. CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"),
  397. CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"),
  398. CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"),
  399. { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, "deint" },
  400. CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"),
  401. CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"),
  402. {NULL},
  403. };
  404. AVFILTER_DEFINE_CLASS(yadif);
  405. static const AVFilterPad avfilter_vf_yadif_inputs[] = {
  406. {
  407. .name = "default",
  408. .type = AVMEDIA_TYPE_VIDEO,
  409. .filter_frame = filter_frame,
  410. },
  411. { NULL }
  412. };
  413. static const AVFilterPad avfilter_vf_yadif_outputs[] = {
  414. {
  415. .name = "default",
  416. .type = AVMEDIA_TYPE_VIDEO,
  417. .request_frame = request_frame,
  418. .config_props = config_props,
  419. },
  420. { NULL }
  421. };
  422. AVFilter avfilter_vf_yadif = {
  423. .name = "yadif",
  424. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
  425. .priv_size = sizeof(YADIFContext),
  426. .priv_class = &yadif_class,
  427. .uninit = uninit,
  428. .query_formats = query_formats,
  429. .inputs = avfilter_vf_yadif_inputs,
  430. .outputs = avfilter_vf_yadif_outputs,
  431. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  432. };