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.

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