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.

566 lines
18KB

  1. /*
  2. * Copyright (C) 2006-2011 Michael Niedermayer <michaelni@gmx.at>
  3. * 2010 James Darnley <james.darnley@gmail.com>
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/avassert.h"
  21. #include "libavutil/cpu.h"
  22. #include "libavutil/common.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "libavutil/imgutils.h"
  26. #include "avfilter.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. #include "yadif.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. const int edge = MAX_ALIGN - 1;
  117. /* Only edge pixels need to be processed here. A constant value of false
  118. * for is_not_edge should let the compiler ignore the whole branch. */
  119. FILTER(0, 3, 0)
  120. dst = (uint8_t*)dst1 + w - edge;
  121. prev = (uint8_t*)prev1 + w - edge;
  122. cur = (uint8_t*)cur1 + w - edge;
  123. next = (uint8_t*)next1 + w - edge;
  124. prev2 = (uint8_t*)(parity ? prev : cur);
  125. next2 = (uint8_t*)(parity ? cur : next);
  126. FILTER(w - edge, w - 3, 1)
  127. FILTER(w - 3, w, 0)
  128. }
  129. static void filter_line_c_16bit(void *dst1,
  130. void *prev1, void *cur1, void *next1,
  131. int w, int prefs, int mrefs, int parity,
  132. int mode)
  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. mrefs /= 2;
  142. prefs /= 2;
  143. FILTER(0, w, 1)
  144. }
  145. static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,
  146. int w, int prefs, int mrefs, int parity, int mode)
  147. {
  148. uint16_t *dst = dst1;
  149. uint16_t *prev = prev1;
  150. uint16_t *cur = cur1;
  151. uint16_t *next = next1;
  152. int x;
  153. uint16_t *prev2 = parity ? prev : cur ;
  154. uint16_t *next2 = parity ? cur : next;
  155. const int edge = MAX_ALIGN / 2 - 1;
  156. mrefs /= 2;
  157. prefs /= 2;
  158. FILTER(0, 3, 0)
  159. dst = (uint16_t*)dst1 + w - edge;
  160. prev = (uint16_t*)prev1 + w - edge;
  161. cur = (uint16_t*)cur1 + w - edge;
  162. next = (uint16_t*)next1 + w - edge;
  163. prev2 = (uint16_t*)(parity ? prev : cur);
  164. next2 = (uint16_t*)(parity ? cur : next);
  165. FILTER(w - edge, w - 3, 1)
  166. FILTER(w - 3, w, 0)
  167. }
  168. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  169. {
  170. YADIFContext *s = ctx->priv;
  171. ThreadData *td = arg;
  172. int refs = s->cur->linesize[td->plane];
  173. int df = (s->csp->comp[td->plane].depth + 7) / 8;
  174. int pix_3 = 3 * df;
  175. int slice_start = (td->h * jobnr ) / nb_jobs;
  176. int slice_end = (td->h * (jobnr+1)) / nb_jobs;
  177. int y;
  178. int edge = 3 + MAX_ALIGN / df - 1;
  179. /* filtering reads 3 pixels to the left/right; to avoid invalid reads,
  180. * we need to call the c variant which avoids this for border pixels
  181. */
  182. for (y = slice_start; y < slice_end; y++) {
  183. if ((y ^ td->parity) & 1) {
  184. uint8_t *prev = &s->prev->data[td->plane][y * refs];
  185. uint8_t *cur = &s->cur ->data[td->plane][y * refs];
  186. uint8_t *next = &s->next->data[td->plane][y * refs];
  187. uint8_t *dst = &td->frame->data[td->plane][y * td->frame->linesize[td->plane]];
  188. int mode = y == 1 || y + 2 == td->h ? 2 : s->mode;
  189. s->filter_line(dst + pix_3, prev + pix_3, cur + pix_3,
  190. next + pix_3, td->w - edge,
  191. y + 1 < td->h ? refs : -refs,
  192. y ? -refs : refs,
  193. td->parity ^ td->tff, mode);
  194. s->filter_edges(dst, prev, cur, next, td->w,
  195. y + 1 < td->h ? refs : -refs,
  196. y ? -refs : refs,
  197. td->parity ^ td->tff, mode);
  198. } else {
  199. memcpy(&td->frame->data[td->plane][y * td->frame->linesize[td->plane]],
  200. &s->cur->data[td->plane][y * refs], td->w * df);
  201. }
  202. }
  203. return 0;
  204. }
  205. static void filter(AVFilterContext *ctx, AVFrame *dstpic,
  206. int parity, int tff)
  207. {
  208. YADIFContext *yadif = ctx->priv;
  209. ThreadData td = { .frame = dstpic, .parity = parity, .tff = tff };
  210. int i;
  211. for (i = 0; i < yadif->csp->nb_components; i++) {
  212. int w = dstpic->width;
  213. int h = dstpic->height;
  214. if (i == 1 || i == 2) {
  215. w = AV_CEIL_RSHIFT(w, yadif->csp->log2_chroma_w);
  216. h = AV_CEIL_RSHIFT(h, yadif->csp->log2_chroma_h);
  217. }
  218. td.w = w;
  219. td.h = h;
  220. td.plane = i;
  221. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h, ff_filter_get_nb_threads(ctx)));
  222. }
  223. emms_c();
  224. }
  225. static int return_frame(AVFilterContext *ctx, int is_second)
  226. {
  227. YADIFContext *yadif = ctx->priv;
  228. AVFilterLink *link = ctx->outputs[0];
  229. int tff, ret;
  230. if (yadif->parity == -1) {
  231. tff = yadif->cur->interlaced_frame ?
  232. yadif->cur->top_field_first : 1;
  233. } else {
  234. tff = yadif->parity ^ 1;
  235. }
  236. if (is_second) {
  237. yadif->out = ff_get_video_buffer(link, link->w, link->h);
  238. if (!yadif->out)
  239. return AVERROR(ENOMEM);
  240. av_frame_copy_props(yadif->out, yadif->cur);
  241. yadif->out->interlaced_frame = 0;
  242. }
  243. filter(ctx, yadif->out, tff ^ !is_second, tff);
  244. if (is_second) {
  245. int64_t cur_pts = yadif->cur->pts;
  246. int64_t next_pts = yadif->next->pts;
  247. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  248. yadif->out->pts = cur_pts + next_pts;
  249. } else {
  250. yadif->out->pts = AV_NOPTS_VALUE;
  251. }
  252. }
  253. ret = ff_filter_frame(ctx->outputs[0], yadif->out);
  254. yadif->frame_pending = (yadif->mode&1) && !is_second;
  255. return ret;
  256. }
  257. static int checkstride(YADIFContext *yadif, const AVFrame *a, const AVFrame *b)
  258. {
  259. int i;
  260. for (i = 0; i < yadif->csp->nb_components; i++)
  261. if (a->linesize[i] != b->linesize[i])
  262. return 1;
  263. return 0;
  264. }
  265. static void fixstride(AVFilterLink *link, AVFrame *f)
  266. {
  267. AVFrame *dst = ff_default_get_video_buffer(link, f->width, f->height);
  268. if(!dst)
  269. return;
  270. av_frame_copy_props(dst, f);
  271. av_image_copy(dst->data, dst->linesize,
  272. (const uint8_t **)f->data, f->linesize,
  273. dst->format, dst->width, dst->height);
  274. av_frame_unref(f);
  275. av_frame_move_ref(f, dst);
  276. av_frame_free(&dst);
  277. }
  278. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  279. {
  280. AVFilterContext *ctx = link->dst;
  281. YADIFContext *yadif = ctx->priv;
  282. av_assert0(frame);
  283. if (yadif->frame_pending)
  284. return_frame(ctx, 1);
  285. if (yadif->prev)
  286. av_frame_free(&yadif->prev);
  287. yadif->prev = yadif->cur;
  288. yadif->cur = yadif->next;
  289. yadif->next = frame;
  290. if (!yadif->cur &&
  291. !(yadif->cur = av_frame_clone(yadif->next)))
  292. return AVERROR(ENOMEM);
  293. if (checkstride(yadif, yadif->next, yadif->cur)) {
  294. av_log(ctx, AV_LOG_VERBOSE, "Reallocating frame due to differing stride\n");
  295. fixstride(link, yadif->next);
  296. }
  297. if (checkstride(yadif, yadif->next, yadif->cur))
  298. fixstride(link, yadif->cur);
  299. if (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))
  300. fixstride(link, yadif->prev);
  301. if (checkstride(yadif, yadif->next, yadif->cur) || (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))) {
  302. av_log(ctx, AV_LOG_ERROR, "Failed to reallocate frame\n");
  303. return -1;
  304. }
  305. if (!yadif->prev)
  306. return 0;
  307. if ((yadif->deint && !yadif->cur->interlaced_frame) ||
  308. ctx->is_disabled ||
  309. (yadif->deint && !yadif->prev->interlaced_frame && yadif->prev->repeat_pict) ||
  310. (yadif->deint && !yadif->next->interlaced_frame && yadif->next->repeat_pict)
  311. ) {
  312. yadif->out = av_frame_clone(yadif->cur);
  313. if (!yadif->out)
  314. return AVERROR(ENOMEM);
  315. av_frame_free(&yadif->prev);
  316. if (yadif->out->pts != AV_NOPTS_VALUE)
  317. yadif->out->pts *= 2;
  318. return ff_filter_frame(ctx->outputs[0], yadif->out);
  319. }
  320. yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
  321. if (!yadif->out)
  322. return AVERROR(ENOMEM);
  323. av_frame_copy_props(yadif->out, yadif->cur);
  324. yadif->out->interlaced_frame = 0;
  325. if (yadif->out->pts != AV_NOPTS_VALUE)
  326. yadif->out->pts *= 2;
  327. return return_frame(ctx, 0);
  328. }
  329. static int request_frame(AVFilterLink *link)
  330. {
  331. AVFilterContext *ctx = link->src;
  332. YADIFContext *yadif = ctx->priv;
  333. int ret;
  334. if (yadif->frame_pending) {
  335. return_frame(ctx, 1);
  336. return 0;
  337. }
  338. if (yadif->eof)
  339. return AVERROR_EOF;
  340. ret = ff_request_frame(ctx->inputs[0]);
  341. if (ret == AVERROR_EOF && yadif->cur) {
  342. AVFrame *next = av_frame_clone(yadif->next);
  343. if (!next)
  344. return AVERROR(ENOMEM);
  345. next->pts = yadif->next->pts * 2 - yadif->cur->pts;
  346. filter_frame(ctx->inputs[0], next);
  347. yadif->eof = 1;
  348. } else if (ret < 0) {
  349. return ret;
  350. }
  351. return 0;
  352. }
  353. static av_cold void uninit(AVFilterContext *ctx)
  354. {
  355. YADIFContext *yadif = ctx->priv;
  356. av_frame_free(&yadif->prev);
  357. av_frame_free(&yadif->cur );
  358. av_frame_free(&yadif->next);
  359. }
  360. static int query_formats(AVFilterContext *ctx)
  361. {
  362. static const enum AVPixelFormat pix_fmts[] = {
  363. AV_PIX_FMT_YUV420P,
  364. AV_PIX_FMT_YUV422P,
  365. AV_PIX_FMT_YUV444P,
  366. AV_PIX_FMT_YUV410P,
  367. AV_PIX_FMT_YUV411P,
  368. AV_PIX_FMT_GRAY8,
  369. AV_PIX_FMT_YUVJ420P,
  370. AV_PIX_FMT_YUVJ422P,
  371. AV_PIX_FMT_YUVJ444P,
  372. AV_PIX_FMT_GRAY16,
  373. AV_PIX_FMT_YUV440P,
  374. AV_PIX_FMT_YUVJ440P,
  375. AV_PIX_FMT_YUV420P9,
  376. AV_PIX_FMT_YUV422P9,
  377. AV_PIX_FMT_YUV444P9,
  378. AV_PIX_FMT_YUV420P10,
  379. AV_PIX_FMT_YUV422P10,
  380. AV_PIX_FMT_YUV444P10,
  381. AV_PIX_FMT_YUV420P12,
  382. AV_PIX_FMT_YUV422P12,
  383. AV_PIX_FMT_YUV444P12,
  384. AV_PIX_FMT_YUV420P14,
  385. AV_PIX_FMT_YUV422P14,
  386. AV_PIX_FMT_YUV444P14,
  387. AV_PIX_FMT_YUV420P16,
  388. AV_PIX_FMT_YUV422P16,
  389. AV_PIX_FMT_YUV444P16,
  390. AV_PIX_FMT_YUVA420P,
  391. AV_PIX_FMT_YUVA422P,
  392. AV_PIX_FMT_YUVA444P,
  393. AV_PIX_FMT_GBRP,
  394. AV_PIX_FMT_GBRP9,
  395. AV_PIX_FMT_GBRP10,
  396. AV_PIX_FMT_GBRP12,
  397. AV_PIX_FMT_GBRP14,
  398. AV_PIX_FMT_GBRP16,
  399. AV_PIX_FMT_GBRAP,
  400. AV_PIX_FMT_NONE
  401. };
  402. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  403. if (!fmts_list)
  404. return AVERROR(ENOMEM);
  405. return ff_set_common_formats(ctx, fmts_list);
  406. }
  407. static int config_props(AVFilterLink *link)
  408. {
  409. AVFilterContext *ctx = link->src;
  410. YADIFContext *s = ctx->priv;
  411. link->time_base.num = ctx->inputs[0]->time_base.num;
  412. link->time_base.den = ctx->inputs[0]->time_base.den * 2;
  413. link->w = ctx->inputs[0]->w;
  414. link->h = ctx->inputs[0]->h;
  415. if(s->mode & 1)
  416. link->frame_rate = av_mul_q(ctx->inputs[0]->frame_rate,
  417. (AVRational){2, 1});
  418. if (link->w < 3 || link->h < 3) {
  419. av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
  420. return AVERROR(EINVAL);
  421. }
  422. s->csp = av_pix_fmt_desc_get(link->format);
  423. if (s->csp->comp[0].depth > 8) {
  424. s->filter_line = filter_line_c_16bit;
  425. s->filter_edges = filter_edges_16bit;
  426. } else {
  427. s->filter_line = filter_line_c;
  428. s->filter_edges = filter_edges;
  429. }
  430. if (ARCH_X86)
  431. ff_yadif_init_x86(s);
  432. return 0;
  433. }
  434. #define OFFSET(x) offsetof(YADIFContext, x)
  435. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  436. #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
  437. static const AVOption yadif_options[] = {
  438. { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, "mode"},
  439. CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"),
  440. CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"),
  441. CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"),
  442. CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"),
  443. { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, "parity" },
  444. CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"),
  445. CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"),
  446. CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"),
  447. { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, "deint" },
  448. CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"),
  449. CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"),
  450. { NULL }
  451. };
  452. AVFILTER_DEFINE_CLASS(yadif);
  453. static const AVFilterPad avfilter_vf_yadif_inputs[] = {
  454. {
  455. .name = "default",
  456. .type = AVMEDIA_TYPE_VIDEO,
  457. .filter_frame = filter_frame,
  458. },
  459. { NULL }
  460. };
  461. static const AVFilterPad avfilter_vf_yadif_outputs[] = {
  462. {
  463. .name = "default",
  464. .type = AVMEDIA_TYPE_VIDEO,
  465. .request_frame = request_frame,
  466. .config_props = config_props,
  467. },
  468. { NULL }
  469. };
  470. AVFilter ff_vf_yadif = {
  471. .name = "yadif",
  472. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
  473. .priv_size = sizeof(YADIFContext),
  474. .priv_class = &yadif_class,
  475. .uninit = uninit,
  476. .query_formats = query_formats,
  477. .inputs = avfilter_vf_yadif_inputs,
  478. .outputs = avfilter_vf_yadif_outputs,
  479. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  480. };