vf_w3fdif.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * Copyright (C) 2012 British Broadcasting Corporation, All Rights Reserved
  3. * Author of de-interlace algorithm: Jim Easterbrook for BBC R&D
  4. * Based on the process described by Martin Weston for BBC R&D
  5. * Author of FFmpeg filter: Mark Himsley for BBC Broadcast Systems Development
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "libavutil/common.h"
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "internal.h"
  30. #include "video.h"
  31. #include "w3fdif.h"
  32. typedef struct W3FDIFContext {
  33. const AVClass *class;
  34. int filter; ///< 0 is simple, 1 is more complex
  35. int deint; ///< which frames to deinterlace
  36. int linesize[4]; ///< bytes of pixel data per line for each plane
  37. int planeheight[4]; ///< height of each plane
  38. int field; ///< which field are we on, 0 or 1
  39. int eof;
  40. int nb_planes;
  41. AVFrame *prev, *cur, *next; ///< previous, current, next frames
  42. int32_t **work_line; ///< lines we are calculating
  43. int nb_threads;
  44. W3FDIFDSPContext dsp;
  45. } W3FDIFContext;
  46. #define OFFSET(x) offsetof(W3FDIFContext, x)
  47. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  48. #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
  49. static const AVOption w3fdif_options[] = {
  50. { "filter", "specify the filter", OFFSET(filter), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "filter" },
  51. CONST("simple", NULL, 0, "filter"),
  52. CONST("complex", NULL, 1, "filter"),
  53. { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "deint" },
  54. CONST("all", "deinterlace all frames", 0, "deint"),
  55. CONST("interlaced", "only deinterlace frames marked as interlaced", 1, "deint"),
  56. { NULL }
  57. };
  58. AVFILTER_DEFINE_CLASS(w3fdif);
  59. static int query_formats(AVFilterContext *ctx)
  60. {
  61. static const enum AVPixelFormat pix_fmts[] = {
  62. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  63. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  64. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  65. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  66. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  67. AV_PIX_FMT_YUVJ411P,
  68. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  69. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  70. AV_PIX_FMT_GRAY8,
  71. AV_PIX_FMT_NONE
  72. };
  73. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  74. if (!fmts_list)
  75. return AVERROR(ENOMEM);
  76. return ff_set_common_formats(ctx, fmts_list);
  77. }
  78. static void filter_simple_low(int32_t *work_line,
  79. uint8_t *in_lines_cur[2],
  80. const int16_t *coef, int linesize)
  81. {
  82. int i;
  83. for (i = 0; i < linesize; i++) {
  84. *work_line = *in_lines_cur[0]++ * coef[0];
  85. *work_line++ += *in_lines_cur[1]++ * coef[1];
  86. }
  87. }
  88. static void filter_complex_low(int32_t *work_line,
  89. uint8_t *in_lines_cur[4],
  90. const int16_t *coef, int linesize)
  91. {
  92. int i;
  93. for (i = 0; i < linesize; i++) {
  94. *work_line = *in_lines_cur[0]++ * coef[0];
  95. *work_line += *in_lines_cur[1]++ * coef[1];
  96. *work_line += *in_lines_cur[2]++ * coef[2];
  97. *work_line++ += *in_lines_cur[3]++ * coef[3];
  98. }
  99. }
  100. static void filter_simple_high(int32_t *work_line,
  101. uint8_t *in_lines_cur[3],
  102. uint8_t *in_lines_adj[3],
  103. const int16_t *coef, int linesize)
  104. {
  105. int i;
  106. for (i = 0; i < linesize; i++) {
  107. *work_line += *in_lines_cur[0]++ * coef[0];
  108. *work_line += *in_lines_adj[0]++ * coef[0];
  109. *work_line += *in_lines_cur[1]++ * coef[1];
  110. *work_line += *in_lines_adj[1]++ * coef[1];
  111. *work_line += *in_lines_cur[2]++ * coef[2];
  112. *work_line++ += *in_lines_adj[2]++ * coef[2];
  113. }
  114. }
  115. static void filter_complex_high(int32_t *work_line,
  116. uint8_t *in_lines_cur[5],
  117. uint8_t *in_lines_adj[5],
  118. const int16_t *coef, int linesize)
  119. {
  120. int i;
  121. for (i = 0; i < linesize; i++) {
  122. *work_line += *in_lines_cur[0]++ * coef[0];
  123. *work_line += *in_lines_adj[0]++ * coef[0];
  124. *work_line += *in_lines_cur[1]++ * coef[1];
  125. *work_line += *in_lines_adj[1]++ * coef[1];
  126. *work_line += *in_lines_cur[2]++ * coef[2];
  127. *work_line += *in_lines_adj[2]++ * coef[2];
  128. *work_line += *in_lines_cur[3]++ * coef[3];
  129. *work_line += *in_lines_adj[3]++ * coef[3];
  130. *work_line += *in_lines_cur[4]++ * coef[4];
  131. *work_line++ += *in_lines_adj[4]++ * coef[4];
  132. }
  133. }
  134. static void filter_scale(uint8_t *out_pixel, const int32_t *work_pixel, int linesize)
  135. {
  136. int j;
  137. for (j = 0; j < linesize; j++, out_pixel++, work_pixel++)
  138. *out_pixel = av_clip(*work_pixel, 0, 255 * 256 * 128) >> 15;
  139. }
  140. static int config_input(AVFilterLink *inlink)
  141. {
  142. AVFilterContext *ctx = inlink->dst;
  143. W3FDIFContext *s = ctx->priv;
  144. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  145. int ret, i;
  146. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  147. return ret;
  148. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  149. s->planeheight[0] = s->planeheight[3] = inlink->h;
  150. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  151. s->nb_threads = ctx->graph->nb_threads;
  152. s->work_line = av_calloc(s->nb_threads, sizeof(*s->work_line));
  153. if (!s->work_line)
  154. return AVERROR(ENOMEM);
  155. for (i = 0; i < s->nb_threads; i++) {
  156. s->work_line[i] = av_calloc(FFALIGN(s->linesize[0], 32), sizeof(*s->work_line[0]));
  157. if (!s->work_line[i])
  158. return AVERROR(ENOMEM);
  159. }
  160. s->dsp.filter_simple_low = filter_simple_low;
  161. s->dsp.filter_complex_low = filter_complex_low;
  162. s->dsp.filter_simple_high = filter_simple_high;
  163. s->dsp.filter_complex_high = filter_complex_high;
  164. s->dsp.filter_scale = filter_scale;
  165. if (ARCH_X86)
  166. ff_w3fdif_init_x86(&s->dsp);
  167. return 0;
  168. }
  169. static int config_output(AVFilterLink *outlink)
  170. {
  171. AVFilterLink *inlink = outlink->src->inputs[0];
  172. outlink->time_base.num = inlink->time_base.num;
  173. outlink->time_base.den = inlink->time_base.den * 2;
  174. outlink->frame_rate.num = inlink->frame_rate.num * 2;
  175. outlink->frame_rate.den = inlink->frame_rate.den;
  176. return 0;
  177. }
  178. /*
  179. * Filter coefficients from PH-2071, scaled by 256 * 128.
  180. * Each set of coefficients has a set for low-frequencies and high-frequencies.
  181. * n_coef_lf[] and n_coef_hf[] are the number of coefs for simple and more-complex.
  182. * It is important for later that n_coef_lf[] is even and n_coef_hf[] is odd.
  183. * coef_lf[][] and coef_hf[][] are the coefficients for low-frequencies
  184. * and high-frequencies for simple and more-complex mode.
  185. */
  186. static const int8_t n_coef_lf[2] = { 2, 4 };
  187. static const int16_t coef_lf[2][4] = {{ 16384, 16384, 0, 0},
  188. { -852, 17236, 17236, -852}};
  189. static const int8_t n_coef_hf[2] = { 3, 5 };
  190. static const int16_t coef_hf[2][5] = {{ -2048, 4096, -2048, 0, 0},
  191. { 1016, -3801, 5570, -3801, 1016}};
  192. typedef struct ThreadData {
  193. AVFrame *out, *cur, *adj;
  194. int plane;
  195. } ThreadData;
  196. static int deinterlace_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  197. {
  198. W3FDIFContext *s = ctx->priv;
  199. ThreadData *td = arg;
  200. AVFrame *out = td->out;
  201. AVFrame *cur = td->cur;
  202. AVFrame *adj = td->adj;
  203. const int plane = td->plane;
  204. const int filter = s->filter;
  205. uint8_t *in_line, *in_lines_cur[5], *in_lines_adj[5];
  206. uint8_t *out_line, *out_pixel;
  207. int32_t *work_line, *work_pixel;
  208. uint8_t *cur_data = cur->data[plane];
  209. uint8_t *adj_data = adj->data[plane];
  210. uint8_t *dst_data = out->data[plane];
  211. const int linesize = s->linesize[plane];
  212. const int height = s->planeheight[plane];
  213. const int cur_line_stride = cur->linesize[plane];
  214. const int adj_line_stride = adj->linesize[plane];
  215. const int dst_line_stride = out->linesize[plane];
  216. const int start = (height * jobnr) / nb_jobs;
  217. const int end = (height * (jobnr+1)) / nb_jobs;
  218. int j, y_in, y_out;
  219. /* copy unchanged the lines of the field */
  220. y_out = start + (s->field == cur->top_field_first) - (start & 1);
  221. in_line = cur_data + (y_out * cur_line_stride);
  222. out_line = dst_data + (y_out * dst_line_stride);
  223. while (y_out < end) {
  224. memcpy(out_line, in_line, linesize);
  225. y_out += 2;
  226. in_line += cur_line_stride * 2;
  227. out_line += dst_line_stride * 2;
  228. }
  229. /* interpolate other lines of the field */
  230. y_out = start + (s->field != cur->top_field_first) - (start & 1);
  231. out_line = dst_data + (y_out * dst_line_stride);
  232. while (y_out < end) {
  233. /* get low vertical frequencies from current field */
  234. for (j = 0; j < n_coef_lf[filter]; j++) {
  235. y_in = (y_out + 1) + (j * 2) - n_coef_lf[filter];
  236. while (y_in < 0)
  237. y_in += 2;
  238. while (y_in >= height)
  239. y_in -= 2;
  240. in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
  241. }
  242. work_line = s->work_line[jobnr];
  243. switch (n_coef_lf[filter]) {
  244. case 2:
  245. s->dsp.filter_simple_low(work_line, in_lines_cur,
  246. coef_lf[filter], linesize);
  247. break;
  248. case 4:
  249. s->dsp.filter_complex_low(work_line, in_lines_cur,
  250. coef_lf[filter], linesize);
  251. }
  252. /* get high vertical frequencies from adjacent fields */
  253. for (j = 0; j < n_coef_hf[filter]; j++) {
  254. y_in = (y_out + 1) + (j * 2) - n_coef_hf[filter];
  255. while (y_in < 0)
  256. y_in += 2;
  257. while (y_in >= height)
  258. y_in -= 2;
  259. in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
  260. in_lines_adj[j] = adj_data + (y_in * adj_line_stride);
  261. }
  262. work_line = s->work_line[jobnr];
  263. switch (n_coef_hf[filter]) {
  264. case 3:
  265. s->dsp.filter_simple_high(work_line, in_lines_cur, in_lines_adj,
  266. coef_hf[filter], linesize);
  267. break;
  268. case 5:
  269. s->dsp.filter_complex_high(work_line, in_lines_cur, in_lines_adj,
  270. coef_hf[filter], linesize);
  271. }
  272. /* save scaled result to the output frame, scaling down by 256 * 128 */
  273. work_pixel = s->work_line[jobnr];
  274. out_pixel = out_line;
  275. s->dsp.filter_scale(out_pixel, work_pixel, linesize);
  276. /* move on to next line */
  277. y_out += 2;
  278. out_line += dst_line_stride * 2;
  279. }
  280. return 0;
  281. }
  282. static int filter(AVFilterContext *ctx, int is_second)
  283. {
  284. W3FDIFContext *s = ctx->priv;
  285. AVFilterLink *outlink = ctx->outputs[0];
  286. AVFrame *out, *adj;
  287. ThreadData td;
  288. int plane;
  289. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  290. if (!out)
  291. return AVERROR(ENOMEM);
  292. av_frame_copy_props(out, s->cur);
  293. out->interlaced_frame = 0;
  294. if (!is_second) {
  295. if (out->pts != AV_NOPTS_VALUE)
  296. out->pts *= 2;
  297. } else {
  298. int64_t cur_pts = s->cur->pts;
  299. int64_t next_pts = s->next->pts;
  300. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  301. out->pts = cur_pts + next_pts;
  302. } else {
  303. out->pts = AV_NOPTS_VALUE;
  304. }
  305. }
  306. adj = s->field ? s->next : s->prev;
  307. td.out = out; td.cur = s->cur; td.adj = adj;
  308. for (plane = 0; plane < s->nb_planes; plane++) {
  309. td.plane = plane;
  310. ctx->internal->execute(ctx, deinterlace_slice, &td, NULL, FFMIN(s->planeheight[plane], s->nb_threads));
  311. }
  312. s->field = !s->field;
  313. return ff_filter_frame(outlink, out);
  314. }
  315. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  316. {
  317. AVFilterContext *ctx = inlink->dst;
  318. W3FDIFContext *s = ctx->priv;
  319. int ret;
  320. av_frame_free(&s->prev);
  321. s->prev = s->cur;
  322. s->cur = s->next;
  323. s->next = frame;
  324. if (!s->cur) {
  325. s->cur = av_frame_clone(s->next);
  326. if (!s->cur)
  327. return AVERROR(ENOMEM);
  328. }
  329. if ((s->deint && !s->cur->interlaced_frame) || ctx->is_disabled) {
  330. AVFrame *out = av_frame_clone(s->cur);
  331. if (!out)
  332. return AVERROR(ENOMEM);
  333. av_frame_free(&s->prev);
  334. if (out->pts != AV_NOPTS_VALUE)
  335. out->pts *= 2;
  336. return ff_filter_frame(ctx->outputs[0], out);
  337. }
  338. if (!s->prev)
  339. return 0;
  340. ret = filter(ctx, 0);
  341. if (ret < 0)
  342. return ret;
  343. return filter(ctx, 1);
  344. }
  345. static int request_frame(AVFilterLink *outlink)
  346. {
  347. AVFilterContext *ctx = outlink->src;
  348. W3FDIFContext *s = ctx->priv;
  349. int ret;
  350. if (s->eof)
  351. return AVERROR_EOF;
  352. ret = ff_request_frame(ctx->inputs[0]);
  353. if (ret == AVERROR_EOF && s->cur) {
  354. AVFrame *next = av_frame_clone(s->next);
  355. if (!next)
  356. return AVERROR(ENOMEM);
  357. next->pts = s->next->pts * 2 - s->cur->pts;
  358. filter_frame(ctx->inputs[0], next);
  359. s->eof = 1;
  360. } else if (ret < 0) {
  361. return ret;
  362. }
  363. return 0;
  364. }
  365. static av_cold void uninit(AVFilterContext *ctx)
  366. {
  367. W3FDIFContext *s = ctx->priv;
  368. int i;
  369. av_frame_free(&s->prev);
  370. av_frame_free(&s->cur );
  371. av_frame_free(&s->next);
  372. for (i = 0; i < s->nb_threads; i++)
  373. av_freep(&s->work_line[i]);
  374. av_freep(&s->work_line);
  375. }
  376. static const AVFilterPad w3fdif_inputs[] = {
  377. {
  378. .name = "default",
  379. .type = AVMEDIA_TYPE_VIDEO,
  380. .filter_frame = filter_frame,
  381. .config_props = config_input,
  382. },
  383. { NULL }
  384. };
  385. static const AVFilterPad w3fdif_outputs[] = {
  386. {
  387. .name = "default",
  388. .type = AVMEDIA_TYPE_VIDEO,
  389. .config_props = config_output,
  390. .request_frame = request_frame,
  391. },
  392. { NULL }
  393. };
  394. AVFilter ff_vf_w3fdif = {
  395. .name = "w3fdif",
  396. .description = NULL_IF_CONFIG_SMALL("Apply Martin Weston three field deinterlace."),
  397. .priv_size = sizeof(W3FDIFContext),
  398. .priv_class = &w3fdif_class,
  399. .uninit = uninit,
  400. .query_formats = query_formats,
  401. .inputs = w3fdif_inputs,
  402. .outputs = w3fdif_outputs,
  403. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  404. };