vf_phase.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. enum PhaseMode mode;
  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. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  73. return 0;
  74. }
  75. static int config_input(AVFilterLink *inlink)
  76. {
  77. PhaseContext *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. return 0;
  86. }
  87. /*
  88. * This macro interpolates the value of both fields at a point halfway
  89. * between lines and takes the squared difference. In field resolution
  90. * the point is a quarter pixel below a line in one field and a quarter
  91. * pixel above a line in other.
  92. *
  93. * (The result is actually multiplied by 25)
  94. */
  95. #define DIFF(a, as, b, bs) (t = ((*a - b[bs]) << 2) + a[as << 1] - b[-bs], t * t)
  96. /*
  97. * Find which field combination has the smallest average squared difference
  98. * between the fields.
  99. */
  100. static enum PhaseMode analyze_plane(void *ctx, enum PhaseMode mode, AVFrame *old, AVFrame *new)
  101. {
  102. double bdiff, tdiff, pdiff, scale;
  103. const int ns = new->linesize[0];
  104. const int os = old->linesize[0];
  105. const uint8_t *nptr = new->data[0];
  106. const uint8_t *optr = old->data[0];
  107. const int h = new->height;
  108. const int w = new->width;
  109. int bdif, tdif, pdif;
  110. if (mode == AUTO) {
  111. mode = new->interlaced_frame ? new->top_field_first ?
  112. TOP_FIRST : BOTTOM_FIRST : PROGRESSIVE;
  113. } else if (mode == AUTO_ANALYZE) {
  114. mode = new->interlaced_frame ? new->top_field_first ?
  115. TOP_FIRST_ANALYZE : BOTTOM_FIRST_ANALYZE : FULL_ANALYZE;
  116. }
  117. if (mode <= BOTTOM_FIRST) {
  118. bdiff = pdiff = tdiff = 65536.0;
  119. } else {
  120. int top = 0, t;
  121. const uint8_t *rend, *end = nptr + (h - 2) * ns;
  122. bdiff = pdiff = tdiff = 0.0;
  123. nptr += ns;
  124. optr += os;
  125. while (nptr < end) {
  126. pdif = tdif = bdif = 0;
  127. switch (mode) {
  128. case TOP_FIRST_ANALYZE:
  129. if (top) {
  130. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  131. pdif += DIFF(nptr, ns, nptr, ns);
  132. tdif += DIFF(nptr, ns, optr, os);
  133. }
  134. } else {
  135. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  136. pdif += DIFF(nptr, ns, nptr, ns);
  137. tdif += DIFF(optr, os, nptr, ns);
  138. }
  139. }
  140. break;
  141. case BOTTOM_FIRST_ANALYZE:
  142. if (top) {
  143. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  144. pdif += DIFF(nptr, ns, nptr, ns);
  145. bdif += DIFF(optr, os, nptr, ns);
  146. }
  147. } else {
  148. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  149. pdif += DIFF(nptr, ns, nptr, ns);
  150. bdif += DIFF(nptr, ns, optr, os);
  151. }
  152. }
  153. break;
  154. case ANALYZE:
  155. if (top) {
  156. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  157. tdif += DIFF(nptr, ns, optr, os);
  158. bdif += DIFF(optr, os, nptr, ns);
  159. }
  160. } else {
  161. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  162. bdif += DIFF(nptr, ns, optr, os);
  163. tdif += DIFF(optr, os, nptr, ns);
  164. }
  165. }
  166. break;
  167. case FULL_ANALYZE:
  168. if (top) {
  169. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  170. pdif += DIFF(nptr, ns, nptr, ns);
  171. tdif += DIFF(nptr, ns, optr, os);
  172. bdif += DIFF(optr, os, nptr, ns);
  173. }
  174. } else {
  175. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  176. pdif += DIFF(nptr, ns, nptr, ns);
  177. bdif += DIFF(nptr, ns, optr, os);
  178. tdif += DIFF(optr, os, nptr, ns);
  179. }
  180. }
  181. break;
  182. default:
  183. av_assert0(0);
  184. }
  185. pdiff += (double)pdif;
  186. tdiff += (double)tdif;
  187. bdiff += (double)bdif;
  188. nptr += ns - w;
  189. optr += os - w;
  190. top ^= 1;
  191. }
  192. scale = 1.0 / (w * (h - 3)) / 25.0;
  193. pdiff *= scale;
  194. tdiff *= scale;
  195. bdiff *= scale;
  196. if (mode == TOP_FIRST_ANALYZE) {
  197. bdiff = 65536.0;
  198. } else if (mode == BOTTOM_FIRST_ANALYZE) {
  199. tdiff = 65536.0;
  200. } else if (mode == ANALYZE) {
  201. pdiff = 65536.0;
  202. }
  203. if (bdiff < pdiff && bdiff < tdiff) {
  204. mode = BOTTOM_FIRST;
  205. } else if (tdiff < pdiff && tdiff < bdiff) {
  206. mode = TOP_FIRST;
  207. } else {
  208. mode = PROGRESSIVE;
  209. }
  210. }
  211. av_log(ctx, AV_LOG_DEBUG, "mode=%c tdiff=%f bdiff=%f pdiff=%f\n",
  212. mode == BOTTOM_FIRST ? 'b' : mode == TOP_FIRST ? 't' : 'p',
  213. tdiff, bdiff, pdiff);
  214. return mode;
  215. }
  216. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  217. {
  218. AVFilterContext *ctx = inlink->dst;
  219. AVFilterLink *outlink = ctx->outputs[0];
  220. PhaseContext *s = ctx->priv;
  221. enum PhaseMode mode;
  222. int plane, top, y;
  223. AVFrame *out;
  224. if (ctx->is_disabled) {
  225. av_frame_free(&s->frame);
  226. /* we keep a reference to the previous frame so the filter can start
  227. * being useful as soon as it's not disabled, avoiding the 1-frame
  228. * delay. */
  229. s->frame = av_frame_clone(in);
  230. return ff_filter_frame(outlink, in);
  231. }
  232. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  233. if (!out) {
  234. av_frame_free(&in);
  235. return AVERROR(ENOMEM);
  236. }
  237. av_frame_copy_props(out, in);
  238. if (!s->frame) {
  239. s->frame = in;
  240. mode = PROGRESSIVE;
  241. } else {
  242. mode = analyze_plane(ctx, s->mode, s->frame, in);
  243. }
  244. for (plane = 0; plane < s->nb_planes; plane++) {
  245. const uint8_t *buf = s->frame->data[plane];
  246. const uint8_t *from = in->data[plane];
  247. uint8_t *to = out->data[plane];
  248. for (y = 0, top = 1; y < s->planeheight[plane]; y++, top ^= 1) {
  249. memcpy(to, mode == (top ? BOTTOM_FIRST : TOP_FIRST) ? buf : from, s->linesize[plane]);
  250. buf += s->frame->linesize[plane];
  251. from += in->linesize[plane];
  252. to += out->linesize[plane];
  253. }
  254. }
  255. if (in != s->frame)
  256. av_frame_free(&s->frame);
  257. s->frame = in;
  258. return ff_filter_frame(outlink, out);
  259. }
  260. static av_cold void uninit(AVFilterContext *ctx)
  261. {
  262. PhaseContext *s = ctx->priv;
  263. av_frame_free(&s->frame);
  264. }
  265. static const AVFilterPad phase_inputs[] = {
  266. {
  267. .name = "default",
  268. .type = AVMEDIA_TYPE_VIDEO,
  269. .filter_frame = filter_frame,
  270. .config_props = config_input,
  271. },
  272. { NULL }
  273. };
  274. static const AVFilterPad phase_outputs[] = {
  275. {
  276. .name = "default",
  277. .type = AVMEDIA_TYPE_VIDEO,
  278. },
  279. { NULL }
  280. };
  281. AVFilter ff_vf_phase = {
  282. .name = "phase",
  283. .description = NULL_IF_CONFIG_SMALL("Phase shift fields."),
  284. .priv_size = sizeof(PhaseContext),
  285. .priv_class = &phase_class,
  286. .uninit = uninit,
  287. .query_formats = query_formats,
  288. .inputs = phase_inputs,
  289. .outputs = phase_outputs,
  290. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  291. };