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.

249 lines
7.8KB

  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. #define DEPTH 8
  40. #include "phase_template.c"
  41. #undef DEPTH
  42. #define DEPTH 9
  43. #include "phase_template.c"
  44. #undef DEPTH
  45. #define DEPTH 10
  46. #include "phase_template.c"
  47. #undef DEPTH
  48. #define DEPTH 12
  49. #include "phase_template.c"
  50. #undef DEPTH
  51. #define DEPTH 14
  52. #include "phase_template.c"
  53. #undef DEPTH
  54. #define DEPTH 16
  55. #include "phase_template.c"
  56. typedef struct PhaseContext {
  57. const AVClass *class;
  58. int mode; ///<PhaseMode
  59. AVFrame *frame; /* previous frame */
  60. int nb_planes;
  61. int planeheight[4];
  62. int linesize[4];
  63. enum PhaseMode (*analyze_plane)(void *ctx, enum PhaseMode mode, AVFrame *old, AVFrame *new);
  64. } PhaseContext;
  65. #define OFFSET(x) offsetof(PhaseContext, x)
  66. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  67. #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
  68. static const AVOption phase_options[] = {
  69. { "mode", "set phase mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=AUTO_ANALYZE}, PROGRESSIVE, AUTO_ANALYZE, FLAGS, "mode" },
  70. CONST("p", "progressive", PROGRESSIVE, "mode"),
  71. CONST("t", "top first", TOP_FIRST, "mode"),
  72. CONST("b", "bottom first", BOTTOM_FIRST, "mode"),
  73. CONST("T", "top first analyze", TOP_FIRST_ANALYZE, "mode"),
  74. CONST("B", "bottom first analyze", BOTTOM_FIRST_ANALYZE, "mode"),
  75. CONST("u", "analyze", ANALYZE, "mode"),
  76. CONST("U", "full analyze", FULL_ANALYZE, "mode"),
  77. CONST("a", "auto", AUTO, "mode"),
  78. CONST("A", "auto analyze", AUTO_ANALYZE, "mode"),
  79. { NULL }
  80. };
  81. AVFILTER_DEFINE_CLASS(phase);
  82. static int query_formats(AVFilterContext *ctx)
  83. {
  84. static const enum AVPixelFormat pix_fmts[] = {
  85. AV_PIX_FMT_GRAY8,
  86. AV_PIX_FMT_GRAY9,
  87. AV_PIX_FMT_GRAY10,
  88. AV_PIX_FMT_GRAY12,
  89. AV_PIX_FMT_GRAY14,
  90. AV_PIX_FMT_GRAY16,
  91. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  92. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  93. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  94. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  95. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  96. AV_PIX_FMT_YUVJ411P,
  97. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  98. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  99. AV_PIX_FMT_YUV440P10,
  100. AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
  101. AV_PIX_FMT_YUV440P12,
  102. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
  103. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  104. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  105. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  106. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  107. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
  108. AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
  109. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
  110. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  111. AV_PIX_FMT_NONE
  112. };
  113. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  114. if (!fmts_list)
  115. return AVERROR(ENOMEM);
  116. return ff_set_common_formats(ctx, fmts_list);
  117. }
  118. static int config_input(AVFilterLink *inlink)
  119. {
  120. PhaseContext *s = inlink->dst->priv;
  121. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  122. int ret;
  123. switch (desc->comp[0].depth) {
  124. case 8: s->analyze_plane = analyze_plane_8; break;
  125. case 9: s->analyze_plane = analyze_plane_9; break;
  126. case 10: s->analyze_plane = analyze_plane_10; break;
  127. case 12: s->analyze_plane = analyze_plane_12; break;
  128. case 14: s->analyze_plane = analyze_plane_14; break;
  129. case 16: s->analyze_plane = analyze_plane_16; break;
  130. default: av_assert0(0);
  131. };
  132. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  133. return ret;
  134. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  135. s->planeheight[0] = s->planeheight[3] = inlink->h;
  136. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  137. return 0;
  138. }
  139. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  140. {
  141. AVFilterContext *ctx = inlink->dst;
  142. AVFilterLink *outlink = ctx->outputs[0];
  143. PhaseContext *s = ctx->priv;
  144. enum PhaseMode mode;
  145. int plane, top, y;
  146. AVFrame *out;
  147. if (ctx->is_disabled) {
  148. av_frame_free(&s->frame);
  149. /* we keep a reference to the previous frame so the filter can start
  150. * being useful as soon as it's not disabled, avoiding the 1-frame
  151. * delay. */
  152. s->frame = av_frame_clone(in);
  153. return ff_filter_frame(outlink, in);
  154. }
  155. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  156. if (!out) {
  157. av_frame_free(&in);
  158. return AVERROR(ENOMEM);
  159. }
  160. av_frame_copy_props(out, in);
  161. if (!s->frame) {
  162. s->frame = in;
  163. mode = PROGRESSIVE;
  164. } else {
  165. mode = s->analyze_plane(ctx, s->mode, s->frame, in);
  166. }
  167. for (plane = 0; plane < s->nb_planes; plane++) {
  168. const uint8_t *buf = s->frame->data[plane];
  169. const uint8_t *from = in->data[plane];
  170. uint8_t *to = out->data[plane];
  171. for (y = 0, top = 1; y < s->planeheight[plane]; y++, top ^= 1) {
  172. memcpy(to, mode == (top ? BOTTOM_FIRST : TOP_FIRST) ? buf : from, s->linesize[plane]);
  173. buf += s->frame->linesize[plane];
  174. from += in->linesize[plane];
  175. to += out->linesize[plane];
  176. }
  177. }
  178. if (in != s->frame)
  179. av_frame_free(&s->frame);
  180. s->frame = in;
  181. return ff_filter_frame(outlink, out);
  182. }
  183. static av_cold void uninit(AVFilterContext *ctx)
  184. {
  185. PhaseContext *s = ctx->priv;
  186. av_frame_free(&s->frame);
  187. }
  188. static const AVFilterPad phase_inputs[] = {
  189. {
  190. .name = "default",
  191. .type = AVMEDIA_TYPE_VIDEO,
  192. .filter_frame = filter_frame,
  193. .config_props = config_input,
  194. },
  195. { NULL }
  196. };
  197. static const AVFilterPad phase_outputs[] = {
  198. {
  199. .name = "default",
  200. .type = AVMEDIA_TYPE_VIDEO,
  201. },
  202. { NULL }
  203. };
  204. AVFilter ff_vf_phase = {
  205. .name = "phase",
  206. .description = NULL_IF_CONFIG_SMALL("Phase shift fields."),
  207. .priv_size = sizeof(PhaseContext),
  208. .priv_class = &phase_class,
  209. .uninit = uninit,
  210. .query_formats = query_formats,
  211. .inputs = phase_inputs,
  212. .outputs = phase_outputs,
  213. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  214. };