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.

549 lines
16KB

  1. /*
  2. * Copyright (C) 2006-2010 Michael Niedermayer <michaelni@gmx.at>
  3. * 2010 James Darnley <james.darnley@gmail.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/cpu.h"
  22. #include "libavutil/common.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "formats.h"
  27. #include "internal.h"
  28. #include "video.h"
  29. #include "yadif.h"
  30. #undef NDEBUG
  31. #include <assert.h>
  32. typedef struct ThreadData {
  33. AVFrame *frame;
  34. int plane;
  35. int w, h;
  36. int parity;
  37. int tff;
  38. } ThreadData;
  39. #define MAX_ALIGN 8
  40. #define CHECK(j)\
  41. { int score = FFABS(cur[mrefs - 1 + (j)] - cur[prefs - 1 - (j)])\
  42. + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
  43. + FFABS(cur[mrefs + 1 + (j)] - cur[prefs + 1 - (j)]);\
  44. if (score < spatial_score) {\
  45. spatial_score= score;\
  46. spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
  47. /* The is_not_edge argument here controls when the code will enter a branch
  48. * which reads up to and including x-3 and x+3. */
  49. #define FILTER(start, end, is_not_edge) \
  50. for (x = start; x < end; x++) { \
  51. int c = cur[mrefs]; \
  52. int d = (prev2[0] + next2[0])>>1; \
  53. int e = cur[prefs]; \
  54. int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
  55. int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
  56. int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
  57. int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
  58. int spatial_pred = (c+e) >> 1; \
  59. \
  60. if (is_not_edge) {\
  61. int spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
  62. + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
  63. CHECK(-1) CHECK(-2) }} }} \
  64. CHECK( 1) CHECK( 2) }} }} \
  65. }\
  66. \
  67. if (mode < 2) { \
  68. int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
  69. int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
  70. int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
  71. int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
  72. \
  73. diff = FFMAX3(diff, min, -max); \
  74. } \
  75. \
  76. if (spatial_pred > d + diff) \
  77. spatial_pred = d + diff; \
  78. else if (spatial_pred < d - diff) \
  79. spatial_pred = d - diff; \
  80. \
  81. dst[0] = spatial_pred; \
  82. \
  83. dst++; \
  84. cur++; \
  85. prev++; \
  86. next++; \
  87. prev2++; \
  88. next2++; \
  89. }
  90. static void filter_line_c(void *dst1,
  91. void *prev1, void *cur1, void *next1,
  92. int w, int prefs, int mrefs, int parity, int mode)
  93. {
  94. uint8_t *dst = dst1;
  95. uint8_t *prev = prev1;
  96. uint8_t *cur = cur1;
  97. uint8_t *next = next1;
  98. int x;
  99. uint8_t *prev2 = parity ? prev : cur ;
  100. uint8_t *next2 = parity ? cur : next;
  101. /* The function is called with the pointers already pointing to data[3] and
  102. * with 6 subtracted from the width. This allows the FILTER macro to be
  103. * called so that it processes all the pixels normally. A constant value of
  104. * true for is_not_edge lets the compiler ignore the if statement. */
  105. FILTER(0, w, 1)
  106. }
  107. static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1,
  108. int w, int prefs, int mrefs, int parity, int mode)
  109. {
  110. uint8_t *dst = dst1;
  111. uint8_t *prev = prev1;
  112. uint8_t *cur = cur1;
  113. uint8_t *next = next1;
  114. int x;
  115. uint8_t *prev2 = parity ? prev : cur ;
  116. uint8_t *next2 = parity ? cur : next;
  117. const int edge = MAX_ALIGN - 1;
  118. /* Only edge pixels need to be processed here. A constant value of false
  119. * for is_not_edge should let the compiler ignore the whole branch. */
  120. FILTER(0, 3, 0)
  121. dst = (uint8_t*)dst1 + w - edge;
  122. prev = (uint8_t*)prev1 + w - edge;
  123. cur = (uint8_t*)cur1 + w - edge;
  124. next = (uint8_t*)next1 + w - edge;
  125. prev2 = (uint8_t*)(parity ? prev : cur);
  126. next2 = (uint8_t*)(parity ? cur : next);
  127. FILTER(w - edge, w - 3, 1)
  128. FILTER(w - 3, w, 0)
  129. }
  130. static void filter_line_c_16bit(void *dst1,
  131. void *prev1, void *cur1, void *next1,
  132. int w, int prefs, int mrefs, int parity,
  133. int mode)
  134. {
  135. uint16_t *dst = dst1;
  136. uint16_t *prev = prev1;
  137. uint16_t *cur = cur1;
  138. uint16_t *next = next1;
  139. int x;
  140. uint16_t *prev2 = parity ? prev : cur ;
  141. uint16_t *next2 = parity ? cur : next;
  142. mrefs /= 2;
  143. prefs /= 2;
  144. FILTER(0, w, 1)
  145. }
  146. static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,
  147. int w, int prefs, int mrefs, int parity, int mode)
  148. {
  149. uint16_t *dst = dst1;
  150. uint16_t *prev = prev1;
  151. uint16_t *cur = cur1;
  152. uint16_t *next = next1;
  153. int x;
  154. uint16_t *prev2 = parity ? prev : cur ;
  155. uint16_t *next2 = parity ? cur : next;
  156. const int edge = MAX_ALIGN / 2 - 1;
  157. mrefs /= 2;
  158. prefs /= 2;
  159. FILTER(0, 3, 0)
  160. dst = (uint16_t*)dst1 + w - edge;
  161. prev = (uint16_t*)prev1 + w - edge;
  162. cur = (uint16_t*)cur1 + w - edge;
  163. next = (uint16_t*)next1 + w - edge;
  164. prev2 = (uint16_t*)(parity ? prev : cur);
  165. next2 = (uint16_t*)(parity ? cur : next);
  166. FILTER(w - edge, w - 3, 1)
  167. FILTER(w - 3, w, 0)
  168. }
  169. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  170. {
  171. YADIFContext *s = ctx->priv;
  172. ThreadData *td = arg;
  173. int refs = s->cur->linesize[td->plane];
  174. int df = (s->csp->comp[td->plane].depth + 7) / 8;
  175. int pix_3 = 3 * df;
  176. int slice_h = td->h / nb_jobs;
  177. int slice_start = jobnr * slice_h;
  178. int slice_end = (jobnr == nb_jobs - 1) ? td->h : (jobnr + 1) * slice_h;
  179. int y;
  180. int edge = 3 + MAX_ALIGN / df - 1;
  181. /* filtering reads 3 pixels to the left/right; to avoid invalid reads,
  182. * we need to call the c variant which avoids this for border pixels
  183. */
  184. for (y = slice_start; y < slice_end; y++) {
  185. if ((y ^ td->parity) & 1) {
  186. uint8_t *prev = &s->prev->data[td->plane][y * refs];
  187. uint8_t *cur = &s->cur ->data[td->plane][y * refs];
  188. uint8_t *next = &s->next->data[td->plane][y * refs];
  189. uint8_t *dst = &td->frame->data[td->plane][y * td->frame->linesize[td->plane]];
  190. int mode = y == 1 || y + 2 == td->h ? 2 : s->mode;
  191. s->filter_line(dst + pix_3, prev + pix_3, cur + pix_3,
  192. next + pix_3, td->w - edge,
  193. y + 1 < td->h ? refs : -refs,
  194. y ? -refs : refs,
  195. td->parity ^ td->tff, mode);
  196. s->filter_edges(dst, prev, cur, next, td->w,
  197. y + 1 < td->h ? refs : -refs,
  198. y ? -refs : refs,
  199. td->parity ^ td->tff, mode);
  200. } else {
  201. memcpy(&td->frame->data[td->plane][y * td->frame->linesize[td->plane]],
  202. &s->cur->data[td->plane][y * refs], td->w * df);
  203. }
  204. }
  205. return 0;
  206. }
  207. static void filter(AVFilterContext *ctx, AVFrame *dstpic,
  208. int parity, int tff)
  209. {
  210. YADIFContext *yadif = ctx->priv;
  211. ThreadData td = { .frame = dstpic, .parity = parity, .tff = tff };
  212. int i;
  213. for (i = 0; i < yadif->csp->nb_components; i++) {
  214. int w = dstpic->width;
  215. int h = dstpic->height;
  216. if (i == 1 || i == 2) {
  217. w >>= yadif->csp->log2_chroma_w;
  218. h >>= yadif->csp->log2_chroma_h;
  219. }
  220. td.w = w;
  221. td.h = h;
  222. td.plane = i;
  223. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h, ctx->graph->nb_threads));
  224. }
  225. emms_c();
  226. }
  227. static AVFrame *get_video_buffer(AVFilterLink *link, int w, int h)
  228. {
  229. AVFrame *frame;
  230. int width = FFALIGN(w, 32);
  231. int height = FFALIGN(h + 2, 32);
  232. int i;
  233. frame = ff_default_get_video_buffer(link, width, height);
  234. frame->width = w;
  235. frame->height = h;
  236. for (i = 0; i < 3; i++)
  237. frame->data[i] += frame->linesize[i];
  238. return frame;
  239. }
  240. static int return_frame(AVFilterContext *ctx, int is_second)
  241. {
  242. YADIFContext *yadif = ctx->priv;
  243. AVFilterLink *link = ctx->outputs[0];
  244. int tff, ret;
  245. if (yadif->parity == -1) {
  246. tff = yadif->cur->interlaced_frame ?
  247. yadif->cur->top_field_first : 1;
  248. } else {
  249. tff = yadif->parity ^ 1;
  250. }
  251. if (is_second) {
  252. yadif->out = ff_get_video_buffer(link, link->w, link->h);
  253. if (!yadif->out)
  254. return AVERROR(ENOMEM);
  255. av_frame_copy_props(yadif->out, yadif->cur);
  256. yadif->out->interlaced_frame = 0;
  257. }
  258. filter(ctx, yadif->out, tff ^ !is_second, tff);
  259. if (is_second) {
  260. int64_t cur_pts = yadif->cur->pts;
  261. int64_t next_pts = yadif->next->pts;
  262. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  263. yadif->out->pts = cur_pts + next_pts;
  264. } else {
  265. yadif->out->pts = AV_NOPTS_VALUE;
  266. }
  267. }
  268. ret = ff_filter_frame(ctx->outputs[0], yadif->out);
  269. yadif->frame_pending = (yadif->mode&1) && !is_second;
  270. return ret;
  271. }
  272. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  273. {
  274. AVFilterContext *ctx = link->dst;
  275. YADIFContext *yadif = ctx->priv;
  276. if (yadif->frame_pending)
  277. return_frame(ctx, 1);
  278. if (yadif->prev)
  279. av_frame_free(&yadif->prev);
  280. yadif->prev = yadif->cur;
  281. yadif->cur = yadif->next;
  282. yadif->next = frame;
  283. if (!yadif->cur)
  284. return 0;
  285. if (yadif->auto_enable && !yadif->cur->interlaced_frame) {
  286. yadif->out = av_frame_clone(yadif->cur);
  287. if (!yadif->out)
  288. return AVERROR(ENOMEM);
  289. av_frame_free(&yadif->prev);
  290. if (yadif->out->pts != AV_NOPTS_VALUE)
  291. yadif->out->pts *= 2;
  292. return ff_filter_frame(ctx->outputs[0], yadif->out);
  293. }
  294. if (!yadif->prev &&
  295. !(yadif->prev = av_frame_clone(yadif->cur)))
  296. return AVERROR(ENOMEM);
  297. yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
  298. if (!yadif->out)
  299. return AVERROR(ENOMEM);
  300. av_frame_copy_props(yadif->out, yadif->cur);
  301. yadif->out->interlaced_frame = 0;
  302. if (yadif->out->pts != AV_NOPTS_VALUE)
  303. yadif->out->pts *= 2;
  304. return return_frame(ctx, 0);
  305. }
  306. static int request_frame(AVFilterLink *link)
  307. {
  308. AVFilterContext *ctx = link->src;
  309. YADIFContext *yadif = ctx->priv;
  310. if (yadif->frame_pending) {
  311. return_frame(ctx, 1);
  312. return 0;
  313. }
  314. do {
  315. int ret;
  316. if (yadif->eof)
  317. return AVERROR_EOF;
  318. ret = ff_request_frame(link->src->inputs[0]);
  319. if (ret == AVERROR_EOF && yadif->next) {
  320. AVFrame *next = av_frame_clone(yadif->next);
  321. if (!next)
  322. return AVERROR(ENOMEM);
  323. next->pts = yadif->next->pts * 2 - yadif->cur->pts;
  324. filter_frame(link->src->inputs[0], next);
  325. yadif->eof = 1;
  326. } else if (ret < 0) {
  327. return ret;
  328. }
  329. } while (!yadif->cur);
  330. return 0;
  331. }
  332. static int poll_frame(AVFilterLink *link)
  333. {
  334. YADIFContext *yadif = link->src->priv;
  335. int ret, val;
  336. if (yadif->frame_pending)
  337. return 1;
  338. val = ff_poll_frame(link->src->inputs[0]);
  339. if (val <= 0)
  340. return val;
  341. //FIXME change API to not require this red tape
  342. if (val == 1 && !yadif->next) {
  343. if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
  344. return ret;
  345. val = ff_poll_frame(link->src->inputs[0]);
  346. if (val <= 0)
  347. return val;
  348. }
  349. assert(yadif->next || !val);
  350. if (yadif->auto_enable && yadif->next && !yadif->next->interlaced_frame)
  351. return val;
  352. return val * ((yadif->mode&1)+1);
  353. }
  354. static av_cold void uninit(AVFilterContext *ctx)
  355. {
  356. YADIFContext *yadif = ctx->priv;
  357. if (yadif->prev) av_frame_free(&yadif->prev);
  358. if (yadif->cur ) av_frame_free(&yadif->cur );
  359. if (yadif->next) av_frame_free(&yadif->next);
  360. }
  361. static int query_formats(AVFilterContext *ctx)
  362. {
  363. static const enum AVPixelFormat pix_fmts[] = {
  364. AV_PIX_FMT_YUV420P,
  365. AV_PIX_FMT_YUV422P,
  366. AV_PIX_FMT_YUV444P,
  367. AV_PIX_FMT_YUV410P,
  368. AV_PIX_FMT_YUV411P,
  369. AV_PIX_FMT_GRAY8,
  370. AV_PIX_FMT_YUVJ420P,
  371. AV_PIX_FMT_YUVJ422P,
  372. AV_PIX_FMT_YUVJ444P,
  373. AV_NE( AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE ),
  374. AV_PIX_FMT_YUV440P,
  375. AV_PIX_FMT_YUVJ440P,
  376. AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
  377. AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
  378. AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
  379. AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
  380. AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
  381. AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
  382. AV_PIX_FMT_YUVA420P,
  383. AV_PIX_FMT_NONE
  384. };
  385. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  386. return 0;
  387. }
  388. static int config_props(AVFilterLink *link)
  389. {
  390. YADIFContext *s = link->src->priv;
  391. link->time_base.num = link->src->inputs[0]->time_base.num;
  392. link->time_base.den = link->src->inputs[0]->time_base.den * 2;
  393. link->w = link->src->inputs[0]->w;
  394. link->h = link->src->inputs[0]->h;
  395. if (s->mode & 1)
  396. link->frame_rate = av_mul_q(link->src->inputs[0]->frame_rate,
  397. (AVRational){2, 1});
  398. s->csp = av_pix_fmt_desc_get(link->format);
  399. if (s->csp->comp[0].depth > 8) {
  400. s->filter_line = filter_line_c_16bit;
  401. s->filter_edges = filter_edges_16bit;
  402. } else {
  403. s->filter_line = filter_line_c;
  404. s->filter_edges = filter_edges;
  405. if (ARCH_X86)
  406. ff_yadif_init_x86(s);
  407. }
  408. return 0;
  409. }
  410. #define OFFSET(x) offsetof(YADIFContext, x)
  411. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  412. static const AVOption options[] = {
  413. { "mode", NULL, OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 3, FLAGS },
  414. { "parity", NULL, OFFSET(parity), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, FLAGS, "parity" },
  415. { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, .unit = "parity" },
  416. { "tff", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .unit = "parity" },
  417. { "bff", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .unit = "parity" },
  418. { "auto", NULL, OFFSET(auto_enable), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
  419. { NULL },
  420. };
  421. static const AVClass yadif_class = {
  422. .class_name = "yadif",
  423. .item_name = av_default_item_name,
  424. .option = options,
  425. .version = LIBAVUTIL_VERSION_INT,
  426. };
  427. static const AVFilterPad avfilter_vf_yadif_inputs[] = {
  428. {
  429. .name = "default",
  430. .type = AVMEDIA_TYPE_VIDEO,
  431. .get_video_buffer = get_video_buffer,
  432. .filter_frame = filter_frame,
  433. },
  434. { NULL }
  435. };
  436. static const AVFilterPad avfilter_vf_yadif_outputs[] = {
  437. {
  438. .name = "default",
  439. .type = AVMEDIA_TYPE_VIDEO,
  440. .poll_frame = poll_frame,
  441. .request_frame = request_frame,
  442. .config_props = config_props,
  443. },
  444. { NULL }
  445. };
  446. AVFilter ff_vf_yadif = {
  447. .name = "yadif",
  448. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
  449. .priv_size = sizeof(YADIFContext),
  450. .priv_class = &yadif_class,
  451. .uninit = uninit,
  452. .query_formats = query_formats,
  453. .inputs = avfilter_vf_yadif_inputs,
  454. .outputs = avfilter_vf_yadif_outputs,
  455. .flags = AVFILTER_FLAG_SLICE_THREADS,
  456. };