vf_fieldorder.c 6.4 KB

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