vf_fieldorder.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 "libavutil/imgutils.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "avfilter.h"
  28. typedef struct
  29. {
  30. unsigned int dst_tff; ///< output bff/tff
  31. int line_size[4]; ///< bytes of pixel data per line for each plane
  32. } FieldOrderContext;
  33. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  34. {
  35. FieldOrderContext *fieldorder = ctx->priv;
  36. const char *tff = "tff";
  37. const char *bff = "bff";
  38. if (!args) {
  39. fieldorder->dst_tff = 1;
  40. } else if (sscanf(args, "%u", &fieldorder->dst_tff) == 1) {
  41. fieldorder->dst_tff = !!fieldorder->dst_tff;
  42. } else if (!strcmp(tff, args)) {
  43. fieldorder->dst_tff = 1;
  44. } else if (!strcmp(bff, args)) {
  45. fieldorder->dst_tff = 0;
  46. } else {
  47. av_log(ctx, AV_LOG_ERROR, "Invalid argument '%s'.\n", args);
  48. return AVERROR(EINVAL);
  49. }
  50. av_log(ctx, AV_LOG_INFO, "output field order: %s\n",
  51. fieldorder->dst_tff ? tff : bff);
  52. return 0;
  53. }
  54. static int query_formats(AVFilterContext *ctx)
  55. {
  56. AVFilterFormats *formats;
  57. enum PixelFormat pix_fmt;
  58. int ret;
  59. /** accept any input pixel format that is not hardware accelerated, not
  60. * a bitstream format, and does not have vertically sub-sampled chroma */
  61. if (ctx->inputs[0]) {
  62. formats = NULL;
  63. for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
  64. if (!( av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL
  65. || av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_BITSTREAM)
  66. && av_pix_fmt_descriptors[pix_fmt].nb_components
  67. && !av_pix_fmt_descriptors[pix_fmt].log2_chroma_h
  68. && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
  69. avfilter_formats_unref(&formats);
  70. return ret;
  71. }
  72. avfilter_formats_ref(formats, &ctx->inputs[0]->out_formats);
  73. avfilter_formats_ref(formats, &ctx->outputs[0]->in_formats);
  74. }
  75. return 0;
  76. }
  77. static int config_input(AVFilterLink *inlink)
  78. {
  79. AVFilterContext *ctx = inlink->dst;
  80. FieldOrderContext *fieldorder = ctx->priv;
  81. int plane;
  82. /** full an array with the number of bytes that the video
  83. * data occupies per line for each plane of the input video */
  84. for (plane = 0; plane < 4; plane++) {
  85. fieldorder->line_size[plane] = av_image_get_linesize(
  86. inlink->format,
  87. inlink->w,
  88. plane);
  89. }
  90. return 0;
  91. }
  92. static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
  93. {
  94. AVFilterContext *ctx = inlink->dst;
  95. AVFilterLink *outlink = ctx->outputs[0];
  96. return avfilter_get_video_buffer(outlink, perms, w, h);
  97. }
  98. static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  99. {
  100. AVFilterContext *ctx = inlink->dst;
  101. AVFilterLink *outlink = ctx->outputs[0];
  102. AVFilterBufferRef *outpicref;
  103. outpicref = avfilter_ref_buffer(inpicref, ~0);
  104. outlink->out_buf = outpicref;
  105. avfilter_start_frame(outlink, outpicref);
  106. }
  107. static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  108. {
  109. AVFilterContext *ctx = inlink->dst;
  110. FieldOrderContext *fieldorder = ctx->priv;
  111. AVFilterLink *outlink = ctx->outputs[0];
  112. AVFilterBufferRef *inpicref = inlink->cur_buf;
  113. /** can only currently do slices if this filter is doing nothing
  114. * because this filter is moving picture content, the output
  115. * slice will contain different video lines than the input slice
  116. * and that complexity will be added later */
  117. if ( !inpicref->video->interlaced
  118. || inpicref->video->top_field_first == fieldorder->dst_tff) {
  119. avfilter_draw_slice(outlink, y, h, slice_dir);
  120. }
  121. }
  122. static void end_frame(AVFilterLink *inlink)
  123. {
  124. AVFilterContext *ctx = inlink->dst;
  125. FieldOrderContext *fieldorder = ctx->priv;
  126. AVFilterLink *outlink = ctx->outputs[0];
  127. AVFilterBufferRef *inpicref = inlink->cur_buf;
  128. AVFilterBufferRef *outpicref = outlink->out_buf;
  129. int h, plane, line_step, line_size, line;
  130. uint8_t *cpy_src, *cpy_dst;
  131. if ( inpicref->video->interlaced
  132. && inpicref->video->top_field_first != fieldorder->dst_tff) {
  133. av_dlog(ctx,
  134. "picture will move %s one line\n",
  135. fieldorder->dst_tff ? "up" : "down");
  136. h = inpicref->video->h;
  137. for (plane = 0; plane < 4 && inpicref->data[plane]; plane++) {
  138. line_step = inpicref->linesize[plane];
  139. line_size = fieldorder->line_size[plane];
  140. cpy_src = inpicref->data[plane];
  141. cpy_dst = outpicref->data[plane];
  142. if (fieldorder->dst_tff) {
  143. /** Move every line up one line, working from
  144. * the top to the bottom of the frame.
  145. * The original top line is lost.
  146. * The new last line is created as a copy of the
  147. * penultimate line from that field. */
  148. for (line = 0; line < h; line++) {
  149. if (1 + line < outpicref->video->h) {
  150. memcpy(cpy_dst, cpy_src + line_step, line_size);
  151. } else {
  152. memcpy(cpy_dst, cpy_src - line_step - line_step, line_size);
  153. }
  154. cpy_src += line_step;
  155. cpy_dst += line_step;
  156. }
  157. } else {
  158. /** Move every line down one line, working from
  159. * the bottom to the top of the frame.
  160. * The original bottom line is lost.
  161. * The new first line is created as a copy of the
  162. * second line from that field. */
  163. cpy_src += (h - 1) * line_step;
  164. cpy_dst += (h - 1) * line_step;
  165. for (line = h - 1; line >= 0 ; line--) {
  166. if (line > 0) {
  167. memcpy(cpy_dst, cpy_src - line_step, line_size);
  168. } else {
  169. memcpy(cpy_dst, cpy_src + line_step + line_step, line_size);
  170. }
  171. cpy_src -= line_step;
  172. cpy_dst -= line_step;
  173. }
  174. }
  175. }
  176. outpicref->video->top_field_first = fieldorder->dst_tff;
  177. avfilter_draw_slice(outlink, 0, h, 1);
  178. } else {
  179. av_dlog(ctx,
  180. "not interlaced or field order already correct\n");
  181. }
  182. avfilter_end_frame(outlink);
  183. avfilter_unref_buffer(inpicref);
  184. }
  185. AVFilter avfilter_vf_fieldorder = {
  186. .name = "fieldorder",
  187. .description = NULL_IF_CONFIG_SMALL("Set the field order."),
  188. .init = init,
  189. .priv_size = sizeof(FieldOrderContext),
  190. .query_formats = query_formats,
  191. .inputs = (const AVFilterPad[]) {{ .name = "default",
  192. .type = AVMEDIA_TYPE_VIDEO,
  193. .config_props = config_input,
  194. .start_frame = start_frame,
  195. .get_video_buffer = get_video_buffer,
  196. .draw_slice = draw_slice,
  197. .end_frame = end_frame,
  198. .min_perms = AV_PERM_READ,
  199. .rej_perms = AV_PERM_REUSE2|AV_PERM_PRESERVE,},
  200. { .name = NULL}},
  201. .outputs = (const AVFilterPad[]) {{ .name = "default",
  202. .type = AVMEDIA_TYPE_VIDEO, },
  203. { .name = NULL}},
  204. };