vf_weave.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 double_weave;
  29. int nb_planes;
  30. int planeheight[4];
  31. int linesize[4];
  32. AVFrame *prev;
  33. } WeaveContext;
  34. #define OFFSET(x) offsetof(WeaveContext, x)
  35. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  36. static const AVOption weave_options[] = {
  37. { "first_field", "set first field", OFFSET(first_field), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "field"},
  38. { "top", "set top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
  39. { "t", "set top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
  40. { "bottom", "set bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
  41. { "b", "set bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
  42. { NULL }
  43. };
  44. AVFILTER_DEFINE_CLASS(weave);
  45. static int config_props_output(AVFilterLink *outlink)
  46. {
  47. AVFilterContext *ctx = outlink->src;
  48. WeaveContext *s = ctx->priv;
  49. AVFilterLink *inlink = ctx->inputs[0];
  50. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  51. int ret;
  52. if (!s->double_weave) {
  53. outlink->time_base.num = inlink->time_base.num * 2;
  54. outlink->time_base.den = inlink->time_base.den;
  55. outlink->frame_rate.num = inlink->frame_rate.num;
  56. outlink->frame_rate.den = inlink->frame_rate.den * 2;
  57. }
  58. outlink->w = inlink->w;
  59. outlink->h = inlink->h * 2;
  60. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  61. return ret;
  62. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  63. s->planeheight[0] = s->planeheight[3] = inlink->h;
  64. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  65. return 0;
  66. }
  67. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  68. {
  69. AVFilterContext *ctx = inlink->dst;
  70. WeaveContext *s = ctx->priv;
  71. AVFilterLink *outlink = ctx->outputs[0];
  72. AVFrame *out;
  73. int i;
  74. int weave;
  75. int field1, field2;
  76. if (!s->prev) {
  77. s->prev = in;
  78. return 0;
  79. }
  80. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  81. if (!out) {
  82. av_frame_free(&in);
  83. av_frame_free(&s->prev);
  84. return AVERROR(ENOMEM);
  85. }
  86. av_frame_copy_props(out, in);
  87. weave = (s->double_weave && !(inlink->frame_count_out & 1));
  88. field1 = weave ? s->first_field : (!s->first_field);
  89. field2 = weave ? (!s->first_field) : s->first_field;
  90. for (i = 0; i < s->nb_planes; i++) {
  91. av_image_copy_plane(out->data[i] + out->linesize[i] * field1,
  92. out->linesize[i] * 2,
  93. in->data[i], in->linesize[i],
  94. s->linesize[i], s->planeheight[i]);
  95. av_image_copy_plane(out->data[i] + out->linesize[i] * field2,
  96. out->linesize[i] * 2,
  97. s->prev->data[i], s->prev->linesize[i],
  98. s->linesize[i], s->planeheight[i]);
  99. }
  100. out->pts = s->double_weave ? s->prev->pts : in->pts / 2;
  101. out->interlaced_frame = 1;
  102. out->top_field_first = !s->first_field;
  103. if (!s->double_weave)
  104. av_frame_free(&in);
  105. av_frame_free(&s->prev);
  106. if (s->double_weave)
  107. s->prev = in;
  108. return ff_filter_frame(outlink, out);
  109. }
  110. static av_cold void uninit(AVFilterContext *ctx)
  111. {
  112. WeaveContext *s = ctx->priv;
  113. av_frame_free(&s->prev);
  114. }
  115. static const AVFilterPad weave_inputs[] = {
  116. {
  117. .name = "default",
  118. .type = AVMEDIA_TYPE_VIDEO,
  119. .filter_frame = filter_frame,
  120. },
  121. { NULL }
  122. };
  123. static const AVFilterPad weave_outputs[] = {
  124. {
  125. .name = "default",
  126. .type = AVMEDIA_TYPE_VIDEO,
  127. .config_props = config_props_output,
  128. },
  129. { NULL }
  130. };
  131. AVFilter ff_vf_weave = {
  132. .name = "weave",
  133. .description = NULL_IF_CONFIG_SMALL("Weave input video fields into frames."),
  134. .priv_size = sizeof(WeaveContext),
  135. .priv_class = &weave_class,
  136. .uninit = uninit,
  137. .inputs = weave_inputs,
  138. .outputs = weave_outputs,
  139. };
  140. static av_cold int init(AVFilterContext *ctx)
  141. {
  142. WeaveContext *s = ctx->priv;
  143. if (!strcmp(ctx->filter->name, "doubleweave"))
  144. s->double_weave = 1;
  145. return 0;
  146. }
  147. #define doubleweave_options weave_options
  148. AVFILTER_DEFINE_CLASS(doubleweave);
  149. AVFilter ff_vf_doubleweave = {
  150. .name = "doubleweave",
  151. .description = NULL_IF_CONFIG_SMALL("Weave input video fields into double number of frames."),
  152. .priv_size = sizeof(WeaveContext),
  153. .priv_class = &doubleweave_class,
  154. .init = init,
  155. .uninit = uninit,
  156. .inputs = weave_inputs,
  157. .outputs = weave_outputs,
  158. };