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.

185 lines
4.9KB

  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 "filters.h"
  23. #include "internal.h"
  24. typedef struct SeparateFieldsContext {
  25. int nb_planes;
  26. AVFrame *second;
  27. } SeparateFieldsContext;
  28. static int config_props_output(AVFilterLink *outlink)
  29. {
  30. AVFilterContext *ctx = outlink->src;
  31. SeparateFieldsContext *s = ctx->priv;
  32. AVFilterLink *inlink = ctx->inputs[0];
  33. s->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->time_base.num = inlink->time_base.num;
  39. outlink->time_base.den = inlink->time_base.den * 2;
  40. outlink->frame_rate.num = inlink->frame_rate.num * 2;
  41. outlink->frame_rate.den = inlink->frame_rate.den;
  42. outlink->w = inlink->w;
  43. outlink->h = inlink->h / 2;
  44. return 0;
  45. }
  46. static void extract_field(AVFrame *frame, int nb_planes, int type)
  47. {
  48. int i;
  49. for (i = 0; i < nb_planes; i++) {
  50. if (type)
  51. frame->data[i] = frame->data[i] + frame->linesize[i];
  52. frame->linesize[i] *= 2;
  53. }
  54. }
  55. static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
  56. {
  57. AVFilterContext *ctx = inlink->dst;
  58. SeparateFieldsContext *s = ctx->priv;
  59. AVFilterLink *outlink = ctx->outputs[0];
  60. int ret;
  61. inpicref->height = outlink->h;
  62. inpicref->interlaced_frame = 0;
  63. if (!s->second) {
  64. goto clone;
  65. } else {
  66. AVFrame *second = s->second;
  67. extract_field(second, s->nb_planes, second->top_field_first);
  68. if (second->pts != AV_NOPTS_VALUE &&
  69. inpicref->pts != AV_NOPTS_VALUE)
  70. second->pts += inpicref->pts;
  71. else
  72. second->pts = AV_NOPTS_VALUE;
  73. ret = ff_filter_frame(outlink, second);
  74. if (ret < 0)
  75. return ret;
  76. clone:
  77. s->second = av_frame_clone(inpicref);
  78. if (!s->second)
  79. return AVERROR(ENOMEM);
  80. }
  81. extract_field(inpicref, s->nb_planes, !inpicref->top_field_first);
  82. if (inpicref->pts != AV_NOPTS_VALUE)
  83. inpicref->pts *= 2;
  84. return ff_filter_frame(outlink, inpicref);
  85. }
  86. static int flush_frame(AVFilterLink *outlink, int64_t pts, int64_t *out_pts)
  87. {
  88. AVFilterContext *ctx = outlink->src;
  89. SeparateFieldsContext *s = ctx->priv;
  90. int ret = 0;
  91. if (s->second) {
  92. *out_pts = s->second->pts += pts;
  93. extract_field(s->second, s->nb_planes, s->second->top_field_first);
  94. ret = ff_filter_frame(outlink, s->second);
  95. s->second = NULL;
  96. }
  97. return ret;
  98. }
  99. static int activate(AVFilterContext *ctx)
  100. {
  101. AVFilterLink *inlink = ctx->inputs[0];
  102. AVFilterLink *outlink = ctx->outputs[0];
  103. AVFrame *in;
  104. int64_t pts;
  105. int ret, status;
  106. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  107. ret = ff_inlink_consume_frame(inlink, &in);
  108. if (ret < 0)
  109. return ret;
  110. if (ret > 0)
  111. return filter_frame(inlink, in);
  112. if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
  113. if (status == AVERROR_EOF) {
  114. int64_t out_pts = pts;
  115. ret = flush_frame(outlink, pts, &out_pts);
  116. ff_outlink_set_status(outlink, status, out_pts);
  117. return ret;
  118. }
  119. }
  120. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  121. return FFERROR_NOT_READY;
  122. }
  123. static av_cold void uninit(AVFilterContext *ctx)
  124. {
  125. SeparateFieldsContext *s = ctx->priv;
  126. av_frame_free(&s->second);
  127. }
  128. static const AVFilterPad separatefields_inputs[] = {
  129. {
  130. .name = "default",
  131. .type = AVMEDIA_TYPE_VIDEO,
  132. },
  133. { NULL }
  134. };
  135. static const AVFilterPad separatefields_outputs[] = {
  136. {
  137. .name = "default",
  138. .type = AVMEDIA_TYPE_VIDEO,
  139. .config_props = config_props_output,
  140. },
  141. { NULL }
  142. };
  143. AVFilter ff_vf_separatefields = {
  144. .name = "separatefields",
  145. .description = NULL_IF_CONFIG_SMALL("Split input video frames into fields."),
  146. .priv_size = sizeof(SeparateFieldsContext),
  147. .activate = activate,
  148. .uninit = uninit,
  149. .inputs = separatefields_inputs,
  150. .outputs = separatefields_outputs,
  151. };