vf_field.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2003 Rich Felker
  3. * Copyright (c) 2012 Stefano Sabatini
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * field filter, based on libmpcodecs/vf_field.c by Rich Felker
  24. */
  25. #include "libavutil/opt.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "avfilter.h"
  28. #include "internal.h"
  29. enum FieldType { FIELD_TYPE_TOP = 0, FIELD_TYPE_BOTTOM };
  30. typedef struct {
  31. const AVClass *class;
  32. enum FieldType type;
  33. int nb_planes; ///< number of planes of the current format
  34. } FieldContext;
  35. #define OFFSET(x) offsetof(FieldContext, x)
  36. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  37. static const AVOption field_options[] = {
  38. {"type", "set field type (top or bottom)", OFFSET(type), AV_OPT_TYPE_INT, {.i64=FIELD_TYPE_TOP}, 0, 1, FLAGS, "field_type" },
  39. {"top", "select top field", 0, AV_OPT_TYPE_CONST, {.i64=FIELD_TYPE_TOP}, INT_MIN, INT_MAX, FLAGS, "field_type"},
  40. {"bottom", "select bottom field", 0, AV_OPT_TYPE_CONST, {.i64=FIELD_TYPE_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "field_type"},
  41. {NULL}
  42. };
  43. AVFILTER_DEFINE_CLASS(field);
  44. static int config_props_output(AVFilterLink *outlink)
  45. {
  46. AVFilterContext *ctx = outlink->src;
  47. FieldContext *field = ctx->priv;
  48. AVFilterLink *inlink = ctx->inputs[0];
  49. field->nb_planes = av_pix_fmt_count_planes(outlink->format);
  50. outlink->w = inlink->w;
  51. outlink->h = (inlink->h + (field->type == FIELD_TYPE_TOP)) / 2;
  52. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d type:%s -> w:%d h:%d\n",
  53. inlink->w, inlink->h, field->type == FIELD_TYPE_BOTTOM ? "bottom" : "top",
  54. outlink->w, outlink->h);
  55. return 0;
  56. }
  57. static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
  58. {
  59. FieldContext *field = inlink->dst->priv;
  60. AVFilterLink *outlink = inlink->dst->outputs[0];
  61. int i;
  62. inpicref->height = outlink->h;
  63. inpicref->interlaced_frame = 0;
  64. for (i = 0; i < field->nb_planes; i++) {
  65. if (field->type == FIELD_TYPE_BOTTOM)
  66. inpicref->data[i] = inpicref->data[i] + inpicref->linesize[i];
  67. inpicref->linesize[i] = 2 * inpicref->linesize[i];
  68. }
  69. return ff_filter_frame(outlink, inpicref);
  70. }
  71. static const AVFilterPad field_inputs[] = {
  72. {
  73. .name = "default",
  74. .type = AVMEDIA_TYPE_VIDEO,
  75. .filter_frame = filter_frame,
  76. },
  77. { NULL }
  78. };
  79. static const AVFilterPad field_outputs[] = {
  80. {
  81. .name = "default",
  82. .type = AVMEDIA_TYPE_VIDEO,
  83. .config_props = config_props_output,
  84. },
  85. { NULL }
  86. };
  87. AVFilter ff_vf_field = {
  88. .name = "field",
  89. .description = NULL_IF_CONFIG_SMALL("Extract a field from the input video."),
  90. .priv_size = sizeof(FieldContext),
  91. .inputs = field_inputs,
  92. .outputs = field_outputs,
  93. .priv_class = &field_class,
  94. };