vf_separatefields.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. AVFrame *second;
  26. } SeparateFieldsContext;
  27. static int config_props_output(AVFilterLink *outlink)
  28. {
  29. AVFilterContext *ctx = outlink->src;
  30. SeparateFieldsContext *s = ctx->priv;
  31. AVFilterLink *inlink = ctx->inputs[0];
  32. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  33. if (inlink->h & 1) {
  34. av_log(ctx, AV_LOG_ERROR, "height must be even\n");
  35. return AVERROR_INVALIDDATA;
  36. }
  37. outlink->time_base.num = inlink->time_base.num;
  38. outlink->time_base.den = inlink->time_base.den * 2;
  39. outlink->frame_rate.num = inlink->frame_rate.num * 2;
  40. outlink->frame_rate.den = inlink->frame_rate.den;
  41. outlink->w = inlink->w;
  42. outlink->h = inlink->h / 2;
  43. return 0;
  44. }
  45. static void extract_field(AVFrame *frame, int nb_planes, int type)
  46. {
  47. int i;
  48. for (i = 0; i < nb_planes; i++) {
  49. if (type)
  50. frame->data[i] = frame->data[i] + frame->linesize[i];
  51. frame->linesize[i] *= 2;
  52. }
  53. }
  54. static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
  55. {
  56. AVFilterContext *ctx = inlink->dst;
  57. SeparateFieldsContext *s = ctx->priv;
  58. AVFilterLink *outlink = ctx->outputs[0];
  59. int ret;
  60. inpicref->height = outlink->h;
  61. inpicref->interlaced_frame = 0;
  62. if (!s->second) {
  63. goto clone;
  64. } else {
  65. AVFrame *second = s->second;
  66. extract_field(second, s->nb_planes, second->top_field_first);
  67. if (second->pts != AV_NOPTS_VALUE &&
  68. inpicref->pts != AV_NOPTS_VALUE)
  69. second->pts += inpicref->pts;
  70. else
  71. second->pts = AV_NOPTS_VALUE;
  72. ret = ff_filter_frame(outlink, second);
  73. if (ret < 0)
  74. return ret;
  75. clone:
  76. s->second = av_frame_clone(inpicref);
  77. if (!s->second)
  78. return AVERROR(ENOMEM);
  79. }
  80. extract_field(inpicref, s->nb_planes, !inpicref->top_field_first);
  81. if (inpicref->pts != AV_NOPTS_VALUE)
  82. inpicref->pts *= 2;
  83. return ff_filter_frame(outlink, inpicref);
  84. }
  85. static int request_frame(AVFilterLink *outlink)
  86. {
  87. AVFilterContext *ctx = outlink->src;
  88. SeparateFieldsContext *s = ctx->priv;
  89. int ret;
  90. ret = ff_request_frame(ctx->inputs[0]);
  91. if (ret == AVERROR_EOF && s->second) {
  92. s->second->pts *= 2;
  93. extract_field(s->second, s->nb_planes, s->second->top_field_first);
  94. ret = ff_filter_frame(outlink, s->second);
  95. s->second = 0;
  96. }
  97. return ret;
  98. }
  99. static const AVFilterPad separatefields_inputs[] = {
  100. {
  101. .name = "default",
  102. .type = AVMEDIA_TYPE_VIDEO,
  103. .filter_frame = filter_frame,
  104. },
  105. { NULL }
  106. };
  107. static const AVFilterPad separatefields_outputs[] = {
  108. {
  109. .name = "default",
  110. .type = AVMEDIA_TYPE_VIDEO,
  111. .config_props = config_props_output,
  112. .request_frame = request_frame,
  113. },
  114. { NULL }
  115. };
  116. AVFilter ff_vf_separatefields = {
  117. .name = "separatefields",
  118. .description = NULL_IF_CONFIG_SMALL("Split input video frames into fields."),
  119. .priv_size = sizeof(SeparateFieldsContext),
  120. .inputs = separatefields_inputs,
  121. .outputs = separatefields_outputs,
  122. };