vf_fieldorder.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. ff_formats_unref(&formats);
  55. return ret;
  56. }
  57. }
  58. ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
  59. ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
  60. }
  61. return 0;
  62. }
  63. static int config_input(AVFilterLink *inlink)
  64. {
  65. AVFilterContext *ctx = inlink->dst;
  66. FieldOrderContext *s = ctx->priv;
  67. return av_image_fill_linesizes(s->line_size, inlink->format, inlink->w);
  68. }
  69. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  70. {
  71. AVFilterContext *ctx = inlink->dst;
  72. FieldOrderContext *s = ctx->priv;
  73. AVFilterLink *outlink = ctx->outputs[0];
  74. int h, plane, src_line_step, dst_line_step, line_size, line;
  75. uint8_t *dst, *src;
  76. AVFrame *out;
  77. if (!frame->interlaced_frame ||
  78. frame->top_field_first == s->dst_tff) {
  79. av_log(ctx, AV_LOG_VERBOSE,
  80. "Skipping %s.\n",
  81. frame->interlaced_frame ?
  82. "frame with same field order" : "progressive frame");
  83. return ff_filter_frame(outlink, frame);
  84. }
  85. if (av_frame_is_writable(frame)) {
  86. out = frame;
  87. } else {
  88. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  89. if (!out) {
  90. av_frame_free(&frame);
  91. return AVERROR(ENOMEM);
  92. }
  93. av_frame_copy_props(out, frame);
  94. }
  95. av_dlog(ctx,
  96. "picture will move %s one line\n",
  97. s->dst_tff ? "up" : "down");
  98. h = frame->height;
  99. for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) {
  100. dst_line_step = out->linesize[plane];
  101. src_line_step = frame->linesize[plane];
  102. line_size = s->line_size[plane];
  103. dst = out->data[plane];
  104. src = frame->data[plane];
  105. if (s->dst_tff) {
  106. /** Move every line up one line, working from
  107. * the top to the bottom of the frame.
  108. * The original top line is lost.
  109. * The new last line is created as a copy of the
  110. * penultimate line from that field. */
  111. for (line = 0; line < h; line++) {
  112. if (1 + line < frame->height) {
  113. memcpy(dst, src + src_line_step, line_size);
  114. } else {
  115. memcpy(dst, src - 2 * src_line_step, line_size);
  116. }
  117. dst += dst_line_step;
  118. src += src_line_step;
  119. }
  120. } else {
  121. /** Move every line down one line, working from
  122. * the bottom to the top of the frame.
  123. * The original bottom line is lost.
  124. * The new first line is created as a copy of the
  125. * second line from that field. */
  126. dst += (h - 1) * dst_line_step;
  127. src += (h - 1) * src_line_step;
  128. for (line = h - 1; line >= 0 ; line--) {
  129. if (line > 0) {
  130. memcpy(dst, src - src_line_step, line_size);
  131. } else {
  132. memcpy(dst, src + 2 * src_line_step, line_size);
  133. }
  134. dst -= dst_line_step;
  135. src -= src_line_step;
  136. }
  137. }
  138. }
  139. out->top_field_first = s->dst_tff;
  140. if (frame != out)
  141. av_frame_free(&frame);
  142. return ff_filter_frame(outlink, out);
  143. }
  144. #define OFFSET(x) offsetof(FieldOrderContext, x)
  145. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  146. static const AVOption fieldorder_options[] = {
  147. { "order", "output field order", OFFSET(dst_tff), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "order" },
  148. { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .flags=FLAGS, .unit = "order" },
  149. { "tff", "top field first", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .flags=FLAGS, .unit = "order" },
  150. { NULL }
  151. };
  152. AVFILTER_DEFINE_CLASS(fieldorder);
  153. static const AVFilterPad avfilter_vf_fieldorder_inputs[] = {
  154. {
  155. .name = "default",
  156. .type = AVMEDIA_TYPE_VIDEO,
  157. .config_props = config_input,
  158. .filter_frame = filter_frame,
  159. },
  160. { NULL }
  161. };
  162. static const AVFilterPad avfilter_vf_fieldorder_outputs[] = {
  163. {
  164. .name = "default",
  165. .type = AVMEDIA_TYPE_VIDEO,
  166. },
  167. { NULL }
  168. };
  169. AVFilter ff_vf_fieldorder = {
  170. .name = "fieldorder",
  171. .description = NULL_IF_CONFIG_SMALL("Set the field order."),
  172. .priv_size = sizeof(FieldOrderContext),
  173. .priv_class = &fieldorder_class,
  174. .query_formats = query_formats,
  175. .inputs = avfilter_vf_fieldorder_inputs,
  176. .outputs = avfilter_vf_fieldorder_outputs,
  177. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  178. };