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.

193 lines
6.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/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. if (!s->prev) {
  75. s->prev = in;
  76. return 0;
  77. }
  78. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  79. if (!out) {
  80. av_frame_free(&in);
  81. av_frame_free(&s->prev);
  82. return AVERROR(ENOMEM);
  83. }
  84. av_frame_copy_props(out, in);
  85. for (i = 0; i < s->nb_planes; i++) {
  86. if (s->double_weave && !(inlink->frame_count_out & 1)) {
  87. av_image_copy_plane(out->data[i] + out->linesize[i] * s->first_field,
  88. out->linesize[i] * 2,
  89. in->data[i], in->linesize[i],
  90. s->linesize[i], s->planeheight[i]);
  91. av_image_copy_plane(out->data[i] + out->linesize[i] * !s->first_field,
  92. out->linesize[i] * 2,
  93. s->prev->data[i], s->prev->linesize[i],
  94. s->linesize[i], s->planeheight[i]);
  95. } else {
  96. av_image_copy_plane(out->data[i] + out->linesize[i] * !s->first_field,
  97. out->linesize[i] * 2,
  98. in->data[i], in->linesize[i],
  99. s->linesize[i], s->planeheight[i]);
  100. av_image_copy_plane(out->data[i] + out->linesize[i] * s->first_field,
  101. out->linesize[i] * 2,
  102. s->prev->data[i], s->prev->linesize[i],
  103. s->linesize[i], s->planeheight[i]);
  104. }
  105. }
  106. out->pts = s->double_weave ? s->prev->pts : in->pts / 2;
  107. out->interlaced_frame = 1;
  108. out->top_field_first = !s->first_field;
  109. if (!s->double_weave)
  110. av_frame_free(&in);
  111. av_frame_free(&s->prev);
  112. if (s->double_weave)
  113. s->prev = in;
  114. return ff_filter_frame(outlink, out);
  115. }
  116. static av_cold void uninit(AVFilterContext *ctx)
  117. {
  118. WeaveContext *s = ctx->priv;
  119. av_frame_free(&s->prev);
  120. }
  121. static const AVFilterPad weave_inputs[] = {
  122. {
  123. .name = "default",
  124. .type = AVMEDIA_TYPE_VIDEO,
  125. .filter_frame = filter_frame,
  126. },
  127. { NULL }
  128. };
  129. static const AVFilterPad weave_outputs[] = {
  130. {
  131. .name = "default",
  132. .type = AVMEDIA_TYPE_VIDEO,
  133. .config_props = config_props_output,
  134. },
  135. { NULL }
  136. };
  137. AVFilter ff_vf_weave = {
  138. .name = "weave",
  139. .description = NULL_IF_CONFIG_SMALL("Weave input video fields into frames."),
  140. .priv_size = sizeof(WeaveContext),
  141. .priv_class = &weave_class,
  142. .uninit = uninit,
  143. .inputs = weave_inputs,
  144. .outputs = weave_outputs,
  145. };
  146. static av_cold int init(AVFilterContext *ctx)
  147. {
  148. WeaveContext *s = ctx->priv;
  149. if (!strcmp(ctx->filter->name, "doubleweave"))
  150. s->double_weave = 1;
  151. return 0;
  152. }
  153. #define doubleweave_options weave_options
  154. AVFILTER_DEFINE_CLASS(doubleweave);
  155. AVFilter ff_vf_doubleweave = {
  156. .name = "doubleweave",
  157. .description = NULL_IF_CONFIG_SMALL("Weave input video fields into double number of frames."),
  158. .priv_size = sizeof(WeaveContext),
  159. .priv_class = &doubleweave_class,
  160. .init = init,
  161. .uninit = uninit,
  162. .inputs = weave_inputs,
  163. .outputs = weave_outputs,
  164. };