vf_fieldorder.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2011 Mark Himsley
  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. /**
  21. * @file
  22. * video field order filter, heavily influenced by vf_pad.c
  23. */
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/internal.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "avfilter.h"
  29. #include "formats.h"
  30. #include "internal.h"
  31. #include "video.h"
  32. typedef struct FieldOrderContext {
  33. const AVClass *class;
  34. int dst_tff; ///< output bff/tff
  35. int line_size[4]; ///< bytes of pixel data per line for each plane
  36. } FieldOrderContext;
  37. static int query_formats(AVFilterContext *ctx)
  38. {
  39. AVFilterFormats *formats;
  40. enum AVPixelFormat pix_fmt;
  41. int ret;
  42. /** accept any input pixel format that is not hardware accelerated, not
  43. * a bitstream format, and does not have vertically sub-sampled chroma */
  44. if (ctx->inputs[0]) {
  45. const AVPixFmtDescriptor *desc = NULL;
  46. formats = NULL;
  47. while ((desc = av_pix_fmt_desc_next(desc))) {
  48. pix_fmt = av_pix_fmt_desc_get_id(desc);
  49. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
  50. desc->flags & AV_PIX_FMT_FLAG_PAL ||
  51. desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
  52. desc->nb_components && !desc->log2_chroma_h &&
  53. (ret = ff_add_format(&formats, pix_fmt)) < 0)
  54. return ret;
  55. }
  56. if ((ret = ff_formats_ref(formats, &ctx->inputs[0]->out_formats)) < 0 ||
  57. (ret = ff_formats_ref(formats, &ctx->outputs[0]->in_formats)) < 0)
  58. return ret;
  59. }
  60. return 0;
  61. }
  62. static int config_input(AVFilterLink *inlink)
  63. {
  64. AVFilterContext *ctx = inlink->dst;
  65. FieldOrderContext *s = ctx->priv;
  66. return av_image_fill_linesizes(s->line_size, inlink->format, inlink->w);
  67. }
  68. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  69. {
  70. AVFilterContext *ctx = inlink->dst;
  71. FieldOrderContext *s = ctx->priv;
  72. AVFilterLink *outlink = ctx->outputs[0];
  73. int h, plane, src_line_step, dst_line_step, line_size, line;
  74. uint8_t *dst, *src;
  75. AVFrame *out;
  76. if (!frame->interlaced_frame ||
  77. frame->top_field_first == s->dst_tff) {
  78. av_log(ctx, AV_LOG_VERBOSE,
  79. "Skipping %s.\n",
  80. frame->interlaced_frame ?
  81. "frame with same field order" : "progressive frame");
  82. return ff_filter_frame(outlink, frame);
  83. }
  84. if (av_frame_is_writable(frame)) {
  85. out = frame;
  86. } else {
  87. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  88. if (!out) {
  89. av_frame_free(&frame);
  90. return AVERROR(ENOMEM);
  91. }
  92. av_frame_copy_props(out, frame);
  93. }
  94. av_log(ctx, AV_LOG_TRACE,
  95. "picture will move %s one line\n",
  96. s->dst_tff ? "up" : "down");
  97. h = frame->height;
  98. for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) {
  99. dst_line_step = out->linesize[plane];
  100. src_line_step = frame->linesize[plane];
  101. line_size = s->line_size[plane];
  102. dst = out->data[plane];
  103. src = frame->data[plane];
  104. if (s->dst_tff) {
  105. /** Move every line up one line, working from
  106. * the top to the bottom of the frame.
  107. * The original top line is lost.
  108. * The new last line is created as a copy of the
  109. * penultimate line from that field. */
  110. for (line = 0; line < h; line++) {
  111. if (1 + line < frame->height) {
  112. memcpy(dst, src + src_line_step, line_size);
  113. } else {
  114. memcpy(dst, src - 2 * src_line_step, line_size);
  115. }
  116. dst += dst_line_step;
  117. src += src_line_step;
  118. }
  119. } else {
  120. /** Move every line down one line, working from
  121. * the bottom to the top of the frame.
  122. * The original bottom line is lost.
  123. * The new first line is created as a copy of the
  124. * second line from that field. */
  125. dst += (h - 1) * dst_line_step;
  126. src += (h - 1) * src_line_step;
  127. for (line = h - 1; line >= 0 ; line--) {
  128. if (line > 0) {
  129. memcpy(dst, src - src_line_step, line_size);
  130. } else {
  131. memcpy(dst, src + 2 * src_line_step, line_size);
  132. }
  133. dst -= dst_line_step;
  134. src -= src_line_step;
  135. }
  136. }
  137. }
  138. out->top_field_first = s->dst_tff;
  139. if (frame != out)
  140. av_frame_free(&frame);
  141. return ff_filter_frame(outlink, out);
  142. }
  143. #define OFFSET(x) offsetof(FieldOrderContext, x)
  144. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  145. static const AVOption fieldorder_options[] = {
  146. { "order", "output field order", OFFSET(dst_tff), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "order" },
  147. { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .flags=FLAGS, .unit = "order" },
  148. { "tff", "top field first", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .flags=FLAGS, .unit = "order" },
  149. { NULL }
  150. };
  151. AVFILTER_DEFINE_CLASS(fieldorder);
  152. static const AVFilterPad avfilter_vf_fieldorder_inputs[] = {
  153. {
  154. .name = "default",
  155. .type = AVMEDIA_TYPE_VIDEO,
  156. .config_props = config_input,
  157. .filter_frame = filter_frame,
  158. },
  159. { NULL }
  160. };
  161. static const AVFilterPad avfilter_vf_fieldorder_outputs[] = {
  162. {
  163. .name = "default",
  164. .type = AVMEDIA_TYPE_VIDEO,
  165. },
  166. { NULL }
  167. };
  168. AVFilter ff_vf_fieldorder = {
  169. .name = "fieldorder",
  170. .description = NULL_IF_CONFIG_SMALL("Set the field order."),
  171. .priv_size = sizeof(FieldOrderContext),
  172. .priv_class = &fieldorder_class,
  173. .query_formats = query_formats,
  174. .inputs = avfilter_vf_fieldorder_inputs,
  175. .outputs = avfilter_vf_fieldorder_outputs,
  176. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  177. };