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.

427 lines
14KB

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