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.

111 lines
3.2KB

  1. /*
  2. * Copyright (c) 2013 Paul B Mahol
  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 Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along 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/pixdesc.h"
  21. #include "avfilter.h"
  22. #include "internal.h"
  23. typedef struct {
  24. int nb_planes;
  25. int64_t frame_count;
  26. double ts_unit;
  27. } SeparateFieldsContext;
  28. static int config_props_output(AVFilterLink *outlink)
  29. {
  30. AVFilterContext *ctx = outlink->src;
  31. SeparateFieldsContext *sf = ctx->priv;
  32. AVFilterLink *inlink = ctx->inputs[0];
  33. sf->nb_planes = av_pix_fmt_count_planes(inlink->format);;
  34. if (inlink->h & 1) {
  35. av_log(ctx, AV_LOG_ERROR, "height must be even\n");
  36. return AVERROR_INVALIDDATA;
  37. }
  38. outlink->frame_rate.num = inlink->frame_rate.num * 2;
  39. outlink->frame_rate.den = inlink->frame_rate.den;
  40. outlink->w = inlink->w;
  41. outlink->h = inlink->h / 2;
  42. sf->ts_unit = av_q2d(av_inv_q(av_mul_q(outlink->frame_rate, inlink->time_base)));
  43. return 0;
  44. }
  45. static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
  46. {
  47. AVFilterContext *ctx = inlink->dst;
  48. SeparateFieldsContext *sf = ctx->priv;
  49. AVFilterLink *outlink = ctx->outputs[0];
  50. AVFrame *second;
  51. int i, ret;
  52. inpicref->height = outlink->h;
  53. inpicref->interlaced_frame = 0;
  54. second = av_frame_clone(inpicref);
  55. if (!second)
  56. return AVERROR(ENOMEM);
  57. for (i = 0; i < sf->nb_planes; i++) {
  58. if (!inpicref->top_field_first)
  59. inpicref->data[i] = inpicref->data[i] + inpicref->linesize[i];
  60. else
  61. second->data[i] = second->data[i] + second->linesize[i];
  62. inpicref->linesize[i] *= 2;
  63. second->linesize[i] *= 2;
  64. }
  65. inpicref->pts = sf->frame_count++ * sf->ts_unit;
  66. second->pts = sf->frame_count++ * sf->ts_unit;
  67. ret = ff_filter_frame(outlink, inpicref);
  68. if (ret < 0)
  69. return ret;
  70. return ff_filter_frame(outlink, second);
  71. }
  72. static const AVFilterPad separatefields_inputs[] = {
  73. {
  74. .name = "default",
  75. .type = AVMEDIA_TYPE_VIDEO,
  76. .filter_frame = filter_frame,
  77. },
  78. { NULL }
  79. };
  80. static const AVFilterPad separatefields_outputs[] = {
  81. {
  82. .name = "default",
  83. .type = AVMEDIA_TYPE_VIDEO,
  84. .config_props = config_props_output,
  85. },
  86. { NULL }
  87. };
  88. AVFilter avfilter_vf_separatefields = {
  89. .name = "separatefields",
  90. .description = NULL_IF_CONFIG_SMALL("Split input video frames into fields."),
  91. .priv_size = sizeof(SeparateFieldsContext),
  92. .inputs = separatefields_inputs,
  93. .outputs = separatefields_outputs,
  94. };