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.

319 lines
10KB

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