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.

209 lines
6.3KB

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