vf_phase.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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;
  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(AVFilterContext *ctx, PhaseContext *s,
  101. AVFrame *old, AVFrame *new)
  102. {
  103. double bdiff, tdiff, pdiff, scale;
  104. const int ns = new->linesize[0];
  105. const int os = old->linesize[0];
  106. uint8_t *nptr = new->data[0];
  107. uint8_t *optr = old->data[0];
  108. const int h = new->height;
  109. const int w = new->width;
  110. int bdif, tdif, pdif;
  111. enum PhaseMode mode = s->mode;
  112. uint8_t *end, *rend;
  113. int top, t;
  114. if (mode == AUTO) {
  115. mode = new->interlaced_frame ? new->top_field_first ?
  116. TOP_FIRST : BOTTOM_FIRST : PROGRESSIVE;
  117. } else if (mode == AUTO_ANALYZE) {
  118. mode = new->interlaced_frame ? new->top_field_first ?
  119. TOP_FIRST_ANALYZE : BOTTOM_FIRST_ANALYZE : FULL_ANALYZE;
  120. }
  121. if (mode <= BOTTOM_FIRST) {
  122. bdiff = pdiff = tdiff = 65536.0;
  123. } else {
  124. bdiff = pdiff = tdiff = 0.0;
  125. for (end = nptr + (h - 2) * ns, nptr += ns, optr += os, top = 0;
  126. nptr < end; nptr += ns - w, optr += os - w, top ^= 1) {
  127. pdif = tdif = bdif = 0;
  128. switch (mode) {
  129. case TOP_FIRST_ANALYZE:
  130. if (top) {
  131. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  132. pdif += DIFF(nptr, ns, nptr, ns);
  133. tdif += DIFF(nptr, ns, optr, os);
  134. }
  135. } else {
  136. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  137. pdif += DIFF(nptr, ns, nptr, ns);
  138. tdif += DIFF(optr, os, nptr, ns);
  139. }
  140. }
  141. break;
  142. case BOTTOM_FIRST_ANALYZE:
  143. if (top) {
  144. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  145. pdif += DIFF(nptr, ns, nptr, ns);
  146. bdif += DIFF(optr, os, nptr, ns);
  147. }
  148. } else {
  149. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  150. pdif += DIFF(nptr, ns, nptr, ns);
  151. bdif += DIFF(nptr, ns, optr, os);
  152. }
  153. }
  154. break;
  155. case ANALYZE:
  156. if (top) {
  157. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  158. tdif += DIFF(nptr, ns, optr, os);
  159. bdif += DIFF(optr, os, nptr, ns);
  160. }
  161. } else {
  162. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  163. bdif += DIFF(nptr, ns, optr, os);
  164. tdif += DIFF(optr, os, nptr, ns);
  165. }
  166. }
  167. break;
  168. case FULL_ANALYZE:
  169. if (top) {
  170. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  171. pdif += DIFF(nptr, ns, nptr, ns);
  172. tdif += DIFF(nptr, ns, optr, os);
  173. bdif += DIFF(optr, os, nptr, ns);
  174. }
  175. } else {
  176. for (rend = nptr + w; nptr < rend; nptr++, optr++) {
  177. pdif += DIFF(nptr, ns, nptr, ns);
  178. bdif += DIFF(nptr, ns, optr, os);
  179. tdif += DIFF(optr, os, nptr, ns);
  180. }
  181. }
  182. break;
  183. default:
  184. av_assert0(0);
  185. }
  186. pdiff += (double)pdif;
  187. tdiff += (double)tdif;
  188. bdiff += (double)bdif;
  189. }
  190. scale = 1.0 / (w * (h - 3)) / 25.0;
  191. pdiff *= scale;
  192. tdiff *= scale;
  193. bdiff *= scale;
  194. if (mode == TOP_FIRST_ANALYZE) {
  195. bdiff = 65536.0;
  196. } else if (mode == BOTTOM_FIRST_ANALYZE) {
  197. tdiff = 65536.0;
  198. } else if (mode == ANALYZE) {
  199. pdiff = 65536.0;
  200. }
  201. if (bdiff < pdiff && bdiff < tdiff) {
  202. mode = BOTTOM_FIRST;
  203. } else if (tdiff < pdiff && tdiff < bdiff) {
  204. mode = TOP_FIRST;
  205. } else {
  206. mode = PROGRESSIVE;
  207. }
  208. }
  209. av_log(ctx, AV_LOG_DEBUG, "mode=%c tdiff=%f bdiff=%f pdiff=%f\n",
  210. mode == BOTTOM_FIRST ? 'b' : mode == TOP_FIRST ? 't' : 'p',
  211. tdiff, bdiff, pdiff);
  212. return mode;
  213. }
  214. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  215. {
  216. AVFilterContext *ctx = inlink->dst;
  217. AVFilterLink *outlink = ctx->outputs[0];
  218. PhaseContext *s = ctx->priv;
  219. enum PhaseMode mode;
  220. int plane, top, y;
  221. AVFrame *out;
  222. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  223. if (!out) {
  224. av_frame_free(&in);
  225. return AVERROR(ENOMEM);
  226. }
  227. av_frame_copy_props(out, in);
  228. if (!s->frame) {
  229. mode = PROGRESSIVE;
  230. s->frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  231. if (!s->frame) {
  232. av_frame_free(&in);
  233. av_frame_free(&out);
  234. return AVERROR(ENOMEM);
  235. }
  236. } else {
  237. mode = analyze_plane(ctx, s, s->frame, in);
  238. }
  239. for (plane = 0; plane < s->nb_planes; plane++) {
  240. uint8_t *buf = s->frame->data[plane];
  241. uint8_t *from = in->data[plane];
  242. uint8_t *to = out->data[plane];
  243. for (y = 0, top = 1; y < s->planeheight[plane]; y++, top ^= 1) {
  244. memcpy(to, mode == (top ? BOTTOM_FIRST : TOP_FIRST) ? buf : from, s->linesize[plane]);
  245. memcpy(buf, from, s->linesize[plane]);
  246. buf += s->frame->linesize[plane];
  247. from += in->linesize[plane];
  248. to += out->linesize[plane];
  249. }
  250. }
  251. av_frame_free(&in);
  252. return ff_filter_frame(outlink, out);
  253. }
  254. static av_cold void uninit(AVFilterContext *ctx)
  255. {
  256. PhaseContext *s = ctx->priv;
  257. av_frame_free(&s->frame);
  258. }
  259. static const AVFilterPad phase_inputs[] = {
  260. {
  261. .name = "default",
  262. .type = AVMEDIA_TYPE_VIDEO,
  263. .filter_frame = filter_frame,
  264. .config_props = config_input,
  265. },
  266. { NULL }
  267. };
  268. static const AVFilterPad phase_outputs[] = {
  269. {
  270. .name = "default",
  271. .type = AVMEDIA_TYPE_VIDEO,
  272. },
  273. { NULL }
  274. };
  275. AVFilter ff_vf_phase = {
  276. .name = "phase",
  277. .description = NULL_IF_CONFIG_SMALL("Phase shift fields."),
  278. .priv_size = sizeof(PhaseContext),
  279. .priv_class = &phase_class,
  280. .uninit = uninit,
  281. .query_formats = query_formats,
  282. .inputs = phase_inputs,
  283. .outputs = phase_outputs,
  284. };