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.

152 lines
4.7KB

  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/imgutils.h"
  21. #include "libavutil/opt.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "avfilter.h"
  24. #include "internal.h"
  25. typedef struct WeaveContext {
  26. const AVClass *class;
  27. int first_field;
  28. int nb_planes;
  29. int planeheight[4];
  30. int linesize[4];
  31. AVFrame *prev;
  32. } WeaveContext;
  33. #define OFFSET(x) offsetof(WeaveContext, x)
  34. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  35. static const AVOption weave_options[] = {
  36. { "first_field", "set first field", OFFSET(first_field), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "field"},
  37. { "top", "set top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
  38. { "t", "set top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
  39. { "bottom", "set bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
  40. { "b", "set bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
  41. { NULL }
  42. };
  43. AVFILTER_DEFINE_CLASS(weave);
  44. static int config_props_output(AVFilterLink *outlink)
  45. {
  46. AVFilterContext *ctx = outlink->src;
  47. WeaveContext *s = ctx->priv;
  48. AVFilterLink *inlink = ctx->inputs[0];
  49. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  50. int ret;
  51. outlink->time_base.num = inlink->time_base.num * 2;
  52. outlink->time_base.den = inlink->time_base.den;
  53. outlink->frame_rate.num = inlink->frame_rate.num;
  54. outlink->frame_rate.den = inlink->frame_rate.den * 2;
  55. outlink->w = inlink->w;
  56. outlink->h = inlink->h * 2;
  57. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  58. return ret;
  59. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  60. s->planeheight[0] = s->planeheight[3] = inlink->h;
  61. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  62. return 0;
  63. }
  64. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  65. {
  66. AVFilterContext *ctx = inlink->dst;
  67. WeaveContext *s = ctx->priv;
  68. AVFilterLink *outlink = ctx->outputs[0];
  69. AVFrame *out;
  70. int i;
  71. if (!s->prev) {
  72. s->prev = in;
  73. return 0;
  74. }
  75. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  76. if (!out) {
  77. av_frame_free(&in);
  78. av_frame_free(&s->prev);
  79. return AVERROR(ENOMEM);
  80. }
  81. av_frame_copy_props(out, in);
  82. for (i = 0; i < s->nb_planes; i++) {
  83. av_image_copy_plane(out->data[i] + out->linesize[i] * s->first_field,
  84. out->linesize[i] * 2,
  85. in->data[i], in->linesize[i],
  86. s->linesize[i], s->planeheight[i]);
  87. av_image_copy_plane(out->data[i] + out->linesize[i] * !s->first_field,
  88. out->linesize[i] * 2,
  89. s->prev->data[i], s->prev->linesize[i],
  90. s->linesize[i], s->planeheight[i]);
  91. }
  92. out->pts = in->pts / 2;
  93. out->interlaced_frame = 1;
  94. out->top_field_first = !s->first_field;
  95. av_frame_free(&in);
  96. av_frame_free(&s->prev);
  97. return ff_filter_frame(outlink, out);
  98. }
  99. static av_cold void uninit(AVFilterContext *ctx)
  100. {
  101. WeaveContext *s = ctx->priv;
  102. av_frame_free(&s->prev);
  103. }
  104. static const AVFilterPad weave_inputs[] = {
  105. {
  106. .name = "default",
  107. .type = AVMEDIA_TYPE_VIDEO,
  108. .filter_frame = filter_frame,
  109. },
  110. { NULL }
  111. };
  112. static const AVFilterPad weave_outputs[] = {
  113. {
  114. .name = "default",
  115. .type = AVMEDIA_TYPE_VIDEO,
  116. .config_props = config_props_output,
  117. },
  118. { NULL }
  119. };
  120. AVFilter ff_vf_weave = {
  121. .name = "weave",
  122. .description = NULL_IF_CONFIG_SMALL("Weave input video fields into frames."),
  123. .priv_size = sizeof(WeaveContext),
  124. .priv_class = &weave_class,
  125. .uninit = uninit,
  126. .inputs = weave_inputs,
  127. .outputs = weave_outputs,
  128. };