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.

332 lines
11KB

  1. /*
  2. * Copyright (c) 2004 Ville Saari
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/avassert.h"
  21. #include "libavutil/imgutils.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "libavutil/opt.h"
  24. #include "avfilter.h"
  25. #include "formats.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. enum PhaseMode {
  29. PROGRESSIVE,
  30. TOP_FIRST,
  31. BOTTOM_FIRST,
  32. TOP_FIRST_ANALYZE,
  33. BOTTOM_FIRST_ANALYZE,
  34. ANALYZE,
  35. FULL_ANALYZE,
  36. AUTO,
  37. AUTO_ANALYZE
  38. };
  39. typedef struct PhaseContext {
  40. const AVClass *class;
  41. int mode; ///<PhaseMode
  42. AVFrame *frame; /* previous frame */
  43. int nb_planes;
  44. int planeheight[4];
  45. int linesize[4];
  46. } PhaseContext;
  47. #define OFFSET(x) offsetof(PhaseContext, x)
  48. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  49. #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
  50. static const AVOption phase_options[] = {
  51. { "mode", "set phase mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=AUTO_ANALYZE}, PROGRESSIVE, AUTO_ANALYZE, FLAGS, "mode" },
  52. CONST("p", "progressive", PROGRESSIVE, "mode"),
  53. CONST("t", "top first", TOP_FIRST, "mode"),
  54. CONST("b", "bottom first", BOTTOM_FIRST, "mode"),
  55. CONST("T", "top first analyze", TOP_FIRST_ANALYZE, "mode"),
  56. CONST("B", "bottom first analyze", BOTTOM_FIRST_ANALYZE, "mode"),
  57. CONST("u", "analyze", ANALYZE, "mode"),
  58. CONST("U", "full analyze", FULL_ANALYZE, "mode"),
  59. CONST("a", "auto", AUTO, "mode"),
  60. CONST("A", "auto analyze", AUTO_ANALYZE, "mode"),
  61. { NULL }
  62. };
  63. AVFILTER_DEFINE_CLASS(phase);
  64. static int query_formats(AVFilterContext *ctx)
  65. {
  66. static const enum AVPixelFormat pix_fmts[] = {
  67. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
  68. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
  69. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  70. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
  71. };
  72. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  73. if (!fmts_list)
  74. return AVERROR(ENOMEM);
  75. return ff_set_common_formats(ctx, fmts_list);
  76. }
  77. static int config_input(AVFilterLink *inlink)
  78. {
  79. PhaseContext *s = inlink->dst->priv;
  80. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  81. int ret;
  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. return 0;
  88. }
  89. /*
  90. * This macro interpolates the value of both fields at a point halfway
  91. * between lines and takes the squared difference. In field resolution
  92. * the point is a quarter pixel below a line in one field and a quarter
  93. * pixel above a line in other.
  94. *
  95. * (The result is actually multiplied by 25)
  96. */
  97. #define DIFF(a, as, b, bs) ((t) = ((*(a) - (b)[bs]) << 2) + (a)[(as) << 1] - (b)[-(bs)], (t) * (t))
  98. /*
  99. * Find which field combination has the smallest average squared difference
  100. * between the fields.
  101. */
  102. static enum PhaseMode analyze_plane(void *ctx, enum PhaseMode mode, AVFrame *old, AVFrame *new)
  103. {
  104. double bdiff, tdiff, pdiff, scale;
  105. const int ns = new->linesize[0];
  106. const int os = old->linesize[0];
  107. const uint8_t *nptr = new->data[0];
  108. const uint8_t *optr = old->data[0];
  109. const int h = new->height;
  110. const int w = new->width;
  111. int bdif, tdif, pdif;
  112. if (mode == AUTO) {
  113. mode = new->interlaced_frame ? new->top_field_first ?
  114. TOP_FIRST : BOTTOM_FIRST : PROGRESSIVE;
  115. } else if (mode == AUTO_ANALYZE) {
  116. mode = new->interlaced_frame ? new->top_field_first ?
  117. TOP_FIRST_ANALYZE : BOTTOM_FIRST_ANALYZE : FULL_ANALYZE;
  118. }
  119. if (mode <= BOTTOM_FIRST) {
  120. bdiff = pdiff = tdiff = 65536.0;
  121. } else {
  122. int top = 0, t;
  123. const uint8_t *rend, *end = nptr + (h - 2) * ns;
  124. bdiff = pdiff = tdiff = 0.0;
  125. nptr += ns;
  126. optr += os;
  127. while (nptr < end) {
  128. pdif = tdif = bdif = 0;
  129. switch (mode) {
  130. case TOP_FIRST_ANALYZE:
  131. if (top) {
  132. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  133. pdif += DIFF(nptr, ns, nptr, ns);
  134. tdif += DIFF(nptr, ns, optr, os);
  135. }
  136. } else {
  137. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  138. pdif += DIFF(nptr, ns, nptr, ns);
  139. tdif += DIFF(optr, os, nptr, ns);
  140. }
  141. }
  142. break;
  143. case BOTTOM_FIRST_ANALYZE:
  144. if (top) {
  145. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  146. pdif += DIFF(nptr, ns, nptr, ns);
  147. bdif += DIFF(optr, os, nptr, ns);
  148. }
  149. } else {
  150. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  151. pdif += DIFF(nptr, ns, nptr, ns);
  152. bdif += DIFF(nptr, ns, optr, os);
  153. }
  154. }
  155. break;
  156. case ANALYZE:
  157. if (top) {
  158. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  159. tdif += DIFF(nptr, ns, optr, os);
  160. bdif += DIFF(optr, os, nptr, ns);
  161. }
  162. } else {
  163. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  164. bdif += DIFF(nptr, ns, optr, os);
  165. tdif += DIFF(optr, os, nptr, ns);
  166. }
  167. }
  168. break;
  169. case FULL_ANALYZE:
  170. if (top) {
  171. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  172. pdif += DIFF(nptr, ns, nptr, ns);
  173. tdif += DIFF(nptr, ns, optr, os);
  174. bdif += DIFF(optr, os, nptr, ns);
  175. }
  176. } else {
  177. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  178. pdif += DIFF(nptr, ns, nptr, ns);
  179. bdif += DIFF(nptr, ns, optr, os);
  180. tdif += DIFF(optr, os, nptr, ns);
  181. }
  182. }
  183. break;
  184. default:
  185. av_assert0(0);
  186. }
  187. pdiff += (double)pdif;
  188. tdiff += (double)tdif;
  189. bdiff += (double)bdif;
  190. nptr += ns - w;
  191. optr += os - w;
  192. top ^= 1;
  193. }
  194. scale = 1.0 / (w * (h - 3)) / 25.0;
  195. pdiff *= scale;
  196. tdiff *= scale;
  197. bdiff *= scale;
  198. if (mode == TOP_FIRST_ANALYZE) {
  199. bdiff = 65536.0;
  200. } else if (mode == BOTTOM_FIRST_ANALYZE) {
  201. tdiff = 65536.0;
  202. } else if (mode == ANALYZE) {
  203. pdiff = 65536.0;
  204. }
  205. if (bdiff < pdiff && bdiff < tdiff) {
  206. mode = BOTTOM_FIRST;
  207. } else if (tdiff < pdiff && tdiff < bdiff) {
  208. mode = TOP_FIRST;
  209. } else {
  210. mode = PROGRESSIVE;
  211. }
  212. }
  213. av_log(ctx, AV_LOG_DEBUG, "mode=%c tdiff=%f bdiff=%f pdiff=%f\n",
  214. mode == BOTTOM_FIRST ? 'b' : mode == TOP_FIRST ? 't' : 'p',
  215. tdiff, bdiff, pdiff);
  216. return mode;
  217. }
  218. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  219. {
  220. AVFilterContext *ctx = inlink->dst;
  221. AVFilterLink *outlink = ctx->outputs[0];
  222. PhaseContext *s = ctx->priv;
  223. enum PhaseMode mode;
  224. int plane, top, y;
  225. AVFrame *out;
  226. if (ctx->is_disabled) {
  227. av_frame_free(&s->frame);
  228. /* we keep a reference to the previous frame so the filter can start
  229. * being useful as soon as it's not disabled, avoiding the 1-frame
  230. * delay. */
  231. s->frame = av_frame_clone(in);
  232. return ff_filter_frame(outlink, in);
  233. }
  234. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  235. if (!out) {
  236. av_frame_free(&in);
  237. return AVERROR(ENOMEM);
  238. }
  239. av_frame_copy_props(out, in);
  240. if (!s->frame) {
  241. s->frame = in;
  242. mode = PROGRESSIVE;
  243. } else {
  244. mode = analyze_plane(ctx, s->mode, s->frame, in);
  245. }
  246. for (plane = 0; plane < s->nb_planes; plane++) {
  247. const uint8_t *buf = s->frame->data[plane];
  248. const uint8_t *from = in->data[plane];
  249. uint8_t *to = out->data[plane];
  250. for (y = 0, top = 1; y < s->planeheight[plane]; y++, top ^= 1) {
  251. memcpy(to, mode == (top ? BOTTOM_FIRST : TOP_FIRST) ? buf : from, s->linesize[plane]);
  252. buf += s->frame->linesize[plane];
  253. from += in->linesize[plane];
  254. to += out->linesize[plane];
  255. }
  256. }
  257. if (in != s->frame)
  258. av_frame_free(&s->frame);
  259. s->frame = in;
  260. return ff_filter_frame(outlink, out);
  261. }
  262. static av_cold void uninit(AVFilterContext *ctx)
  263. {
  264. PhaseContext *s = ctx->priv;
  265. av_frame_free(&s->frame);
  266. }
  267. static const AVFilterPad phase_inputs[] = {
  268. {
  269. .name = "default",
  270. .type = AVMEDIA_TYPE_VIDEO,
  271. .filter_frame = filter_frame,
  272. .config_props = config_input,
  273. },
  274. { NULL }
  275. };
  276. static const AVFilterPad phase_outputs[] = {
  277. {
  278. .name = "default",
  279. .type = AVMEDIA_TYPE_VIDEO,
  280. },
  281. { NULL }
  282. };
  283. AVFilter ff_vf_phase = {
  284. .name = "phase",
  285. .description = NULL_IF_CONFIG_SMALL("Phase shift fields."),
  286. .priv_size = sizeof(PhaseContext),
  287. .priv_class = &phase_class,
  288. .uninit = uninit,
  289. .query_formats = query_formats,
  290. .inputs = phase_inputs,
  291. .outputs = phase_outputs,
  292. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  293. };