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.

230 lines
7.1KB

  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. typedef struct ThreadData {
  79. AVFrame *in, *out;
  80. } ThreadData;
  81. static int weave_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  82. {
  83. AVFilterLink *inlink = ctx->inputs[0];
  84. WeaveContext *s = ctx->priv;
  85. ThreadData *td = arg;
  86. AVFrame *in = td->in;
  87. AVFrame *out = td->out;
  88. const int weave = (s->double_weave && !(inlink->frame_count_out & 1));
  89. const int field1 = weave ? s->first_field : (!s->first_field);
  90. const int field2 = weave ? (!s->first_field) : s->first_field;
  91. for (int i = 0; i < s->nb_planes; i++) {
  92. const int height = s->planeheight[i];
  93. const int start = (height * jobnr) / nb_jobs;
  94. const int end = (height * (jobnr+1)) / nb_jobs;
  95. av_image_copy_plane(out->data[i] + out->linesize[i] * field1 +
  96. out->linesize[i] * start * 2,
  97. out->linesize[i] * 2,
  98. in->data[i] + start * in->linesize[i],
  99. in->linesize[i],
  100. s->linesize[i], end - start);
  101. av_image_copy_plane(out->data[i] + out->linesize[i] * field2 +
  102. out->linesize[i] * start * 2,
  103. out->linesize[i] * 2,
  104. s->prev->data[i] + start * s->prev->linesize[i],
  105. s->prev->linesize[i],
  106. s->linesize[i], end - start);
  107. }
  108. return 0;
  109. }
  110. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  111. {
  112. AVFilterContext *ctx = inlink->dst;
  113. WeaveContext *s = ctx->priv;
  114. AVFilterLink *outlink = ctx->outputs[0];
  115. ThreadData td;
  116. AVFrame *out;
  117. if (!s->prev) {
  118. s->prev = in;
  119. return 0;
  120. }
  121. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  122. if (!out) {
  123. av_frame_free(&in);
  124. av_frame_free(&s->prev);
  125. return AVERROR(ENOMEM);
  126. }
  127. av_frame_copy_props(out, in);
  128. td.out = out, td.in = in;
  129. ctx->internal->execute(ctx, weave_slice, &td, NULL, FFMIN(s->planeheight[1],
  130. ff_filter_get_nb_threads(ctx)));
  131. out->pts = s->double_weave ? s->prev->pts : in->pts / 2;
  132. out->interlaced_frame = 1;
  133. out->top_field_first = !s->first_field;
  134. if (!s->double_weave)
  135. av_frame_free(&in);
  136. av_frame_free(&s->prev);
  137. if (s->double_weave)
  138. s->prev = in;
  139. return ff_filter_frame(outlink, out);
  140. }
  141. static av_cold void uninit(AVFilterContext *ctx)
  142. {
  143. WeaveContext *s = ctx->priv;
  144. av_frame_free(&s->prev);
  145. }
  146. static const AVFilterPad weave_inputs[] = {
  147. {
  148. .name = "default",
  149. .type = AVMEDIA_TYPE_VIDEO,
  150. .filter_frame = filter_frame,
  151. },
  152. { NULL }
  153. };
  154. static const AVFilterPad weave_outputs[] = {
  155. {
  156. .name = "default",
  157. .type = AVMEDIA_TYPE_VIDEO,
  158. .config_props = config_props_output,
  159. },
  160. { NULL }
  161. };
  162. AVFilter ff_vf_weave = {
  163. .name = "weave",
  164. .description = NULL_IF_CONFIG_SMALL("Weave input video fields into frames."),
  165. .priv_size = sizeof(WeaveContext),
  166. .priv_class = &weave_class,
  167. .query_formats = query_formats,
  168. .uninit = uninit,
  169. .inputs = weave_inputs,
  170. .outputs = weave_outputs,
  171. .flags = AVFILTER_FLAG_SLICE_THREADS,
  172. };
  173. static av_cold int init(AVFilterContext *ctx)
  174. {
  175. WeaveContext *s = ctx->priv;
  176. if (!strcmp(ctx->filter->name, "doubleweave"))
  177. s->double_weave = 1;
  178. return 0;
  179. }
  180. #define doubleweave_options weave_options
  181. AVFILTER_DEFINE_CLASS(doubleweave);
  182. AVFilter ff_vf_doubleweave = {
  183. .name = "doubleweave",
  184. .description = NULL_IF_CONFIG_SMALL("Weave input video fields into double number of frames."),
  185. .priv_size = sizeof(WeaveContext),
  186. .priv_class = &doubleweave_class,
  187. .query_formats = query_formats,
  188. .init = init,
  189. .uninit = uninit,
  190. .inputs = weave_inputs,
  191. .outputs = weave_outputs,
  192. .flags = AVFILTER_FLAG_SLICE_THREADS,
  193. };