vf_w3fdif.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. typedef struct W3FDIFContext {
  32. const AVClass *class;
  33. int filter; ///< 0 is simple, 1 is more complex
  34. int deint; ///< which frames to deinterlace
  35. int linesize[4]; ///< bytes of pixel data per line for each plane
  36. int planeheight[4]; ///< height of each plane
  37. int field; ///< which field are we on, 0 or 1
  38. int eof;
  39. int nb_planes;
  40. AVFrame *prev, *cur, *next; ///< previous, current, next frames
  41. int32_t *work_line; ///< line we are calculating
  42. } W3FDIFContext;
  43. #define OFFSET(x) offsetof(W3FDIFContext, x)
  44. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  45. #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
  46. static const AVOption w3fdif_options[] = {
  47. { "filter", "specify the filter", OFFSET(filter), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "filter" },
  48. CONST("simple", NULL, 0, "filter"),
  49. CONST("complex", NULL, 1, "filter"),
  50. { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "deint" },
  51. CONST("all", "deinterlace all frames", 0, "deint"),
  52. CONST("interlaced", "only deinterlace frames marked as interlaced", 1, "deint"),
  53. { NULL }
  54. };
  55. AVFILTER_DEFINE_CLASS(w3fdif);
  56. static int query_formats(AVFilterContext *ctx)
  57. {
  58. static const enum AVPixelFormat pix_fmts[] = {
  59. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  60. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  61. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  62. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  63. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  64. AV_PIX_FMT_YUVJ411P,
  65. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  66. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  67. AV_PIX_FMT_GRAY8,
  68. AV_PIX_FMT_NONE
  69. };
  70. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  71. return 0;
  72. }
  73. static int config_input(AVFilterLink *inlink)
  74. {
  75. W3FDIFContext *s = inlink->dst->priv;
  76. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  77. int ret;
  78. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  79. return ret;
  80. s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  81. s->planeheight[0] = s->planeheight[3] = inlink->h;
  82. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  83. s->work_line = av_calloc(s->linesize[0], sizeof(*s->work_line));
  84. if (!s->work_line)
  85. return AVERROR(ENOMEM);
  86. return 0;
  87. }
  88. static int config_output(AVFilterLink *outlink)
  89. {
  90. AVFilterLink *inlink = outlink->src->inputs[0];
  91. outlink->time_base.num = inlink->time_base.num;
  92. outlink->time_base.den = inlink->time_base.den * 2;
  93. outlink->frame_rate.num = inlink->frame_rate.num * 2;
  94. outlink->frame_rate.den = inlink->frame_rate.den;
  95. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  96. return 0;
  97. }
  98. /*
  99. * Filter coefficients from PH-2071, scaled by 256 * 256.
  100. * Each set of coefficients has a set for low-frequencies and high-frequencies.
  101. * n_coef_lf[] and n_coef_hf[] are the number of coefs for simple and more-complex.
  102. * It is important for later that n_coef_lf[] is even and n_coef_hf[] is odd.
  103. * coef_lf[][] and coef_hf[][] are the coefficients for low-frequencies
  104. * and high-frequencies for simple and more-complex mode.
  105. */
  106. static const int8_t n_coef_lf[2] = { 2, 4 };
  107. static const int32_t coef_lf[2][4] = {{ 32768, 32768, 0, 0},
  108. { -1704, 34472, 34472, -1704}};
  109. static const int8_t n_coef_hf[2] = { 3, 5 };
  110. static const int32_t coef_hf[2][5] = {{ -4096, 8192, -4096, 0, 0},
  111. { 2032, -7602, 11140, -7602, 2032}};
  112. static void deinterlace_plane(AVFilterContext *ctx, AVFrame *out,
  113. const AVFrame *cur, const AVFrame *adj,
  114. const int filter, const int plane)
  115. {
  116. W3FDIFContext *s = ctx->priv;
  117. uint8_t *in_line, *in_lines_cur[5], *in_lines_adj[5];
  118. uint8_t *out_line, *out_pixel;
  119. int32_t *work_line, *work_pixel;
  120. uint8_t *cur_data = cur->data[plane];
  121. uint8_t *adj_data = adj->data[plane];
  122. uint8_t *dst_data = out->data[plane];
  123. const int linesize = s->linesize[plane];
  124. const int height = s->planeheight[plane];
  125. const int cur_line_stride = cur->linesize[plane];
  126. const int adj_line_stride = adj->linesize[plane];
  127. const int dst_line_stride = out->linesize[plane];
  128. int i, j, y_in, y_out;
  129. /* copy unchanged the lines of the field */
  130. y_out = s->field == cur->top_field_first;
  131. in_line = cur_data + (y_out * cur_line_stride);
  132. out_line = dst_data + (y_out * dst_line_stride);
  133. while (y_out < height) {
  134. memcpy(out_line, in_line, linesize);
  135. y_out += 2;
  136. in_line += cur_line_stride * 2;
  137. out_line += dst_line_stride * 2;
  138. }
  139. /* interpolate other lines of the field */
  140. y_out = s->field != cur->top_field_first;
  141. out_line = dst_data + (y_out * dst_line_stride);
  142. while (y_out < height) {
  143. /* clear workspace */
  144. memset(s->work_line, 0, sizeof(*s->work_line) * linesize);
  145. /* get low vertical frequencies from current field */
  146. for (j = 0; j < n_coef_lf[filter]; j++) {
  147. y_in = (y_out + 1) + (j * 2) - n_coef_lf[filter];
  148. while (y_in < 0)
  149. y_in += 2;
  150. while (y_in >= height)
  151. y_in -= 2;
  152. in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
  153. }
  154. work_line = s->work_line;
  155. switch (n_coef_lf[filter]) {
  156. case 2:
  157. for (i = 0; i < linesize; i++) {
  158. *work_line += *in_lines_cur[0]++ * coef_lf[filter][0];
  159. *work_line++ += *in_lines_cur[1]++ * coef_lf[filter][1];
  160. }
  161. break;
  162. case 4:
  163. for (i = 0; i < linesize; i++) {
  164. *work_line += *in_lines_cur[0]++ * coef_lf[filter][0];
  165. *work_line += *in_lines_cur[1]++ * coef_lf[filter][1];
  166. *work_line += *in_lines_cur[2]++ * coef_lf[filter][2];
  167. *work_line++ += *in_lines_cur[3]++ * coef_lf[filter][3];
  168. }
  169. }
  170. /* get high vertical frequencies from adjacent fields */
  171. for (j = 0; j < n_coef_hf[filter]; j++) {
  172. y_in = (y_out + 1) + (j * 2) - n_coef_hf[filter];
  173. while (y_in < 0)
  174. y_in += 2;
  175. while (y_in >= height)
  176. y_in -= 2;
  177. in_lines_cur[j] = cur_data + (y_in * cur_line_stride);
  178. in_lines_adj[j] = adj_data + (y_in * adj_line_stride);
  179. }
  180. work_line = s->work_line;
  181. switch (n_coef_hf[filter]) {
  182. case 3:
  183. for (i = 0; i < linesize; i++) {
  184. *work_line += *in_lines_cur[0]++ * coef_hf[filter][0];
  185. *work_line += *in_lines_adj[0]++ * coef_hf[filter][0];
  186. *work_line += *in_lines_cur[1]++ * coef_hf[filter][1];
  187. *work_line += *in_lines_adj[1]++ * coef_hf[filter][1];
  188. *work_line += *in_lines_cur[2]++ * coef_hf[filter][2];
  189. *work_line++ += *in_lines_adj[2]++ * coef_hf[filter][2];
  190. }
  191. break;
  192. case 5:
  193. for (i = 0; i < linesize; i++) {
  194. *work_line += *in_lines_cur[0]++ * coef_hf[filter][0];
  195. *work_line += *in_lines_adj[0]++ * coef_hf[filter][0];
  196. *work_line += *in_lines_cur[1]++ * coef_hf[filter][1];
  197. *work_line += *in_lines_adj[1]++ * coef_hf[filter][1];
  198. *work_line += *in_lines_cur[2]++ * coef_hf[filter][2];
  199. *work_line += *in_lines_adj[2]++ * coef_hf[filter][2];
  200. *work_line += *in_lines_cur[3]++ * coef_hf[filter][3];
  201. *work_line += *in_lines_adj[3]++ * coef_hf[filter][3];
  202. *work_line += *in_lines_cur[4]++ * coef_hf[filter][4];
  203. *work_line++ += *in_lines_adj[4]++ * coef_hf[filter][4];
  204. }
  205. }
  206. /* save scaled result to the output frame, scaling down by 256 * 256 */
  207. work_pixel = s->work_line;
  208. out_pixel = out_line;
  209. for (j = 0; j < linesize; j++, out_pixel++, work_pixel++)
  210. *out_pixel = av_clip(*work_pixel, 0, 255 * 256 * 256) >> 16;
  211. /* move on to next line */
  212. y_out += 2;
  213. out_line += dst_line_stride * 2;
  214. }
  215. }
  216. static int filter(AVFilterContext *ctx, int is_second)
  217. {
  218. W3FDIFContext *s = ctx->priv;
  219. AVFilterLink *outlink = ctx->outputs[0];
  220. AVFrame *out, *adj;
  221. int plane;
  222. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  223. if (!out)
  224. return AVERROR(ENOMEM);
  225. av_frame_copy_props(out, s->cur);
  226. out->interlaced_frame = 0;
  227. if (!is_second) {
  228. if (out->pts != AV_NOPTS_VALUE)
  229. out->pts *= 2;
  230. } else {
  231. int64_t cur_pts = s->cur->pts;
  232. int64_t next_pts = s->next->pts;
  233. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  234. out->pts = cur_pts + next_pts;
  235. } else {
  236. out->pts = AV_NOPTS_VALUE;
  237. }
  238. }
  239. adj = s->field ? s->next : s->prev;
  240. for (plane = 0; plane < s->nb_planes; plane++)
  241. deinterlace_plane(ctx, out, s->cur, adj, s->filter, plane);
  242. s->field = !s->field;
  243. return ff_filter_frame(outlink, out);
  244. }
  245. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  246. {
  247. AVFilterContext *ctx = inlink->dst;
  248. W3FDIFContext *s = ctx->priv;
  249. int ret;
  250. av_frame_free(&s->prev);
  251. s->prev = s->cur;
  252. s->cur = s->next;
  253. s->next = frame;
  254. if (!s->cur) {
  255. s->cur = av_frame_clone(s->next);
  256. if (!s->cur)
  257. return AVERROR(ENOMEM);
  258. }
  259. if ((s->deint && !s->cur->interlaced_frame) || ctx->is_disabled) {
  260. AVFrame *out = av_frame_clone(s->cur);
  261. if (!out)
  262. return AVERROR(ENOMEM);
  263. av_frame_free(&s->prev);
  264. if (out->pts != AV_NOPTS_VALUE)
  265. out->pts *= 2;
  266. return ff_filter_frame(ctx->outputs[0], out);
  267. }
  268. if (!s->prev)
  269. return 0;
  270. ret = filter(ctx, 0);
  271. if (ret < 0)
  272. return ret;
  273. return filter(ctx, 1);
  274. }
  275. static int request_frame(AVFilterLink *outlink)
  276. {
  277. AVFilterContext *ctx = outlink->src;
  278. W3FDIFContext *s = ctx->priv;
  279. do {
  280. int ret;
  281. if (s->eof)
  282. return AVERROR_EOF;
  283. ret = ff_request_frame(ctx->inputs[0]);
  284. if (ret == AVERROR_EOF && s->cur) {
  285. AVFrame *next = av_frame_clone(s->next);
  286. if (!next)
  287. return AVERROR(ENOMEM);
  288. next->pts = s->next->pts * 2 - s->cur->pts;
  289. filter_frame(ctx->inputs[0], next);
  290. s->eof = 1;
  291. } else if (ret < 0) {
  292. return ret;
  293. }
  294. } while (!s->cur);
  295. return 0;
  296. }
  297. static av_cold void uninit(AVFilterContext *ctx)
  298. {
  299. W3FDIFContext *s = ctx->priv;
  300. av_frame_free(&s->prev);
  301. av_frame_free(&s->cur );
  302. av_frame_free(&s->next);
  303. av_freep(&s->work_line);
  304. }
  305. static const AVFilterPad w3fdif_inputs[] = {
  306. {
  307. .name = "default",
  308. .type = AVMEDIA_TYPE_VIDEO,
  309. .filter_frame = filter_frame,
  310. .config_props = config_input,
  311. },
  312. { NULL }
  313. };
  314. static const AVFilterPad w3fdif_outputs[] = {
  315. {
  316. .name = "default",
  317. .type = AVMEDIA_TYPE_VIDEO,
  318. .config_props = config_output,
  319. .request_frame = request_frame,
  320. },
  321. { NULL }
  322. };
  323. AVFilter ff_vf_w3fdif = {
  324. .name = "w3fdif",
  325. .description = NULL_IF_CONFIG_SMALL("Apply Martin Weston three field deinterlace."),
  326. .priv_size = sizeof(W3FDIFContext),
  327. .priv_class = &w3fdif_class,
  328. .uninit = uninit,
  329. .query_formats = query_formats,
  330. .inputs = w3fdif_inputs,
  331. .outputs = w3fdif_outputs,
  332. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  333. };