vf_fieldorder.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. /* #define DEBUG */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/internal.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
  35. {
  36. unsigned int dst_tff; ///< output bff/tff
  37. int line_size[4]; ///< bytes of pixel data per line for each plane
  38. } FieldOrderContext;
  39. static av_cold int init(AVFilterContext *ctx, const char *args)
  40. {
  41. FieldOrderContext *fieldorder = ctx->priv;
  42. const char *tff = "tff";
  43. const char *bff = "bff";
  44. if (!args) {
  45. fieldorder->dst_tff = 1;
  46. } else if (sscanf(args, "%u", &fieldorder->dst_tff) == 1) {
  47. fieldorder->dst_tff = !!fieldorder->dst_tff;
  48. } else if (!strcmp(tff, args)) {
  49. fieldorder->dst_tff = 1;
  50. } else if (!strcmp(bff, args)) {
  51. fieldorder->dst_tff = 0;
  52. } else {
  53. av_log(ctx, AV_LOG_ERROR, "Invalid argument '%s'.\n", args);
  54. return AVERROR(EINVAL);
  55. }
  56. av_log(ctx, AV_LOG_VERBOSE, "output field order: %s\n",
  57. fieldorder->dst_tff ? tff : bff);
  58. return 0;
  59. }
  60. static int query_formats(AVFilterContext *ctx)
  61. {
  62. AVFilterFormats *formats;
  63. enum AVPixelFormat pix_fmt;
  64. int ret;
  65. /** accept any input pixel format that is not hardware accelerated, not
  66. * a bitstream format, and does not have vertically sub-sampled chroma */
  67. if (ctx->inputs[0]) {
  68. formats = NULL;
  69. for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++) {
  70. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  71. if (!(desc->flags & PIX_FMT_HWACCEL ||
  72. desc->flags & PIX_FMT_BITSTREAM) &&
  73. desc->nb_components && !desc->log2_chroma_h &&
  74. (ret = ff_add_format(&formats, pix_fmt)) < 0) {
  75. ff_formats_unref(&formats);
  76. return ret;
  77. }
  78. }
  79. ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
  80. ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
  81. }
  82. return 0;
  83. }
  84. static int config_input(AVFilterLink *inlink)
  85. {
  86. AVFilterContext *ctx = inlink->dst;
  87. FieldOrderContext *fieldorder = ctx->priv;
  88. int plane;
  89. /** full an array with the number of bytes that the video
  90. * data occupies per line for each plane of the input video */
  91. for (plane = 0; plane < 4; plane++) {
  92. fieldorder->line_size[plane] = av_image_get_linesize(
  93. inlink->format,
  94. inlink->w,
  95. plane);
  96. }
  97. return 0;
  98. }
  99. static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
  100. {
  101. AVFilterContext *ctx = inlink->dst;
  102. AVFilterLink *outlink = ctx->outputs[0];
  103. return ff_get_video_buffer(outlink, perms, w, h);
  104. }
  105. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
  106. {
  107. AVFilterContext *ctx = inlink->dst;
  108. FieldOrderContext *s = ctx->priv;
  109. AVFilterLink *outlink = ctx->outputs[0];
  110. int h, plane, line_step, line_size, line;
  111. uint8_t *data;
  112. if (!frame->video->interlaced ||
  113. frame->video->top_field_first == s->dst_tff)
  114. return ff_filter_frame(outlink, frame);
  115. av_dlog(ctx,
  116. "picture will move %s one line\n",
  117. s->dst_tff ? "up" : "down");
  118. h = frame->video->h;
  119. for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
  120. line_step = frame->linesize[plane];
  121. line_size = s->line_size[plane];
  122. data = frame->data[plane];
  123. if (s->dst_tff) {
  124. /** Move every line up one line, working from
  125. * the top to the bottom of the frame.
  126. * The original top line is lost.
  127. * The new last line is created as a copy of the
  128. * penultimate line from that field. */
  129. for (line = 0; line < h; line++) {
  130. if (1 + line < frame->video->h) {
  131. memcpy(data, data + line_step, line_size);
  132. } else {
  133. memcpy(data, data - line_step - line_step, line_size);
  134. }
  135. data += line_step;
  136. }
  137. } else {
  138. /** Move every line down one line, working from
  139. * the bottom to the top of the frame.
  140. * The original bottom line is lost.
  141. * The new first line is created as a copy of the
  142. * second line from that field. */
  143. data += (h - 1) * line_step;
  144. for (line = h - 1; line >= 0 ; line--) {
  145. if (line > 0) {
  146. memcpy(data, data - line_step, line_size);
  147. } else {
  148. memcpy(data, data + line_step + line_step, line_size);
  149. }
  150. data -= line_step;
  151. }
  152. }
  153. }
  154. frame->video->top_field_first = s->dst_tff;
  155. return ff_filter_frame(outlink, frame);
  156. }
  157. static const AVFilterPad avfilter_vf_fieldorder_inputs[] = {
  158. {
  159. .name = "default",
  160. .type = AVMEDIA_TYPE_VIDEO,
  161. .config_props = config_input,
  162. .get_video_buffer = get_video_buffer,
  163. .filter_frame = filter_frame,
  164. .min_perms = AV_PERM_READ | AV_PERM_WRITE,
  165. },
  166. { NULL }
  167. };
  168. static const AVFilterPad avfilter_vf_fieldorder_outputs[] = {
  169. {
  170. .name = "default",
  171. .type = AVMEDIA_TYPE_VIDEO,
  172. },
  173. { NULL }
  174. };
  175. AVFilter avfilter_vf_fieldorder = {
  176. .name = "fieldorder",
  177. .description = NULL_IF_CONFIG_SMALL("Set the field order."),
  178. .init = init,
  179. .priv_size = sizeof(FieldOrderContext),
  180. .query_formats = query_formats,
  181. .inputs = avfilter_vf_fieldorder_inputs,
  182. .outputs = avfilter_vf_fieldorder_outputs,
  183. };