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.

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