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.

396 lines
13KB

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