vf_fieldorder.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 PixelFormat 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 < PIX_FMT_NB; pix_fmt++)
  70. if (!( av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL
  71. || av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_BITSTREAM)
  72. && av_pix_fmt_descriptors[pix_fmt].nb_components
  73. && !av_pix_fmt_descriptors[pix_fmt].log2_chroma_h
  74. && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
  75. ff_formats_unref(&formats);
  76. return ret;
  77. }
  78. ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
  79. ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
  80. }
  81. return 0;
  82. }
  83. static int config_input(AVFilterLink *inlink)
  84. {
  85. AVFilterContext *ctx = inlink->dst;
  86. FieldOrderContext *fieldorder = ctx->priv;
  87. int plane;
  88. /** full an array with the number of bytes that the video
  89. * data occupies per line for each plane of the input video */
  90. for (plane = 0; plane < 4; plane++) {
  91. fieldorder->line_size[plane] = av_image_get_linesize(
  92. inlink->format,
  93. inlink->w,
  94. plane);
  95. }
  96. return 0;
  97. }
  98. static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
  99. {
  100. AVFilterContext *ctx = inlink->dst;
  101. AVFilterLink *outlink = ctx->outputs[0];
  102. return ff_get_video_buffer(outlink, perms, w, h);
  103. }
  104. static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  105. {
  106. AVFilterContext *ctx = inlink->dst;
  107. AVFilterLink *outlink = ctx->outputs[0];
  108. AVFilterBufferRef *outpicref, *for_next_filter;
  109. int ret = 0;
  110. outpicref = avfilter_ref_buffer(inpicref, ~0);
  111. if (!outpicref)
  112. return AVERROR(ENOMEM);
  113. for_next_filter = avfilter_ref_buffer(outpicref, ~0);
  114. if (!for_next_filter) {
  115. avfilter_unref_bufferp(&outpicref);
  116. return AVERROR(ENOMEM);
  117. }
  118. ret = ff_start_frame(outlink, for_next_filter);
  119. if (ret < 0) {
  120. avfilter_unref_bufferp(&outpicref);
  121. return ret;
  122. }
  123. outlink->out_buf = outpicref;
  124. return 0;
  125. }
  126. static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  127. {
  128. AVFilterContext *ctx = inlink->dst;
  129. FieldOrderContext *fieldorder = ctx->priv;
  130. AVFilterLink *outlink = ctx->outputs[0];
  131. AVFilterBufferRef *inpicref = inlink->cur_buf;
  132. /** can only currently do slices if this filter is doing nothing
  133. * because this filter is moving picture content, the output
  134. * slice will contain different video lines than the input slice
  135. * and that complexity will be added later */
  136. if ( !inpicref->video->interlaced
  137. || inpicref->video->top_field_first == fieldorder->dst_tff) {
  138. return ff_draw_slice(outlink, y, h, slice_dir);
  139. }
  140. return 0;
  141. }
  142. static int end_frame(AVFilterLink *inlink)
  143. {
  144. AVFilterContext *ctx = inlink->dst;
  145. FieldOrderContext *fieldorder = ctx->priv;
  146. AVFilterLink *outlink = ctx->outputs[0];
  147. AVFilterBufferRef *inpicref = inlink->cur_buf;
  148. AVFilterBufferRef *outpicref = outlink->out_buf;
  149. int h, plane, line_step, line_size, line;
  150. uint8_t *cpy_src, *cpy_dst;
  151. if ( inpicref->video->interlaced
  152. && inpicref->video->top_field_first != fieldorder->dst_tff) {
  153. av_dlog(ctx,
  154. "picture will move %s one line\n",
  155. fieldorder->dst_tff ? "up" : "down");
  156. h = inpicref->video->h;
  157. for (plane = 0; plane < 4 && inpicref->data[plane]; plane++) {
  158. line_step = inpicref->linesize[plane];
  159. line_size = fieldorder->line_size[plane];
  160. cpy_src = inpicref->data[plane];
  161. cpy_dst = outpicref->data[plane];
  162. if (fieldorder->dst_tff) {
  163. /** Move every line up one line, working from
  164. * the top to the bottom of the frame.
  165. * The original top line is lost.
  166. * The new last line is created as a copy of the
  167. * penultimate line from that field. */
  168. for (line = 0; line < h; line++) {
  169. if (1 + line < outpicref->video->h) {
  170. memcpy(cpy_dst, cpy_src + line_step, line_size);
  171. } else {
  172. memcpy(cpy_dst, cpy_src - line_step - line_step, line_size);
  173. }
  174. cpy_src += line_step;
  175. cpy_dst += line_step;
  176. }
  177. } else {
  178. /** Move every line down one line, working from
  179. * the bottom to the top of the frame.
  180. * The original bottom line is lost.
  181. * The new first line is created as a copy of the
  182. * second line from that field. */
  183. cpy_src += (h - 1) * line_step;
  184. cpy_dst += (h - 1) * line_step;
  185. for (line = h - 1; line >= 0 ; line--) {
  186. if (line > 0) {
  187. memcpy(cpy_dst, cpy_src - line_step, line_size);
  188. } else {
  189. memcpy(cpy_dst, cpy_src + line_step + line_step, line_size);
  190. }
  191. cpy_src -= line_step;
  192. cpy_dst -= line_step;
  193. }
  194. }
  195. }
  196. outpicref->video->top_field_first = fieldorder->dst_tff;
  197. ff_draw_slice(outlink, 0, h, 1);
  198. } else {
  199. av_dlog(ctx,
  200. "not interlaced or field order already correct\n");
  201. }
  202. return ff_end_frame(outlink);
  203. }
  204. AVFilter avfilter_vf_fieldorder = {
  205. .name = "fieldorder",
  206. .description = NULL_IF_CONFIG_SMALL("Set the field order."),
  207. .init = init,
  208. .priv_size = sizeof(FieldOrderContext),
  209. .query_formats = query_formats,
  210. .inputs = (const AVFilterPad[]) {{ .name = "default",
  211. .type = AVMEDIA_TYPE_VIDEO,
  212. .config_props = config_input,
  213. .start_frame = start_frame,
  214. .get_video_buffer = get_video_buffer,
  215. .draw_slice = draw_slice,
  216. .end_frame = end_frame,
  217. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE },
  218. { .name = NULL}},
  219. .outputs = (const AVFilterPad[]) {{ .name = "default",
  220. .type = AVMEDIA_TYPE_VIDEO, },
  221. { .name = NULL}},
  222. };