libavfilter/vf_fieldorder.c
aa0c515a
 /*
  * Copyright (c) 2011 Mark Himsley
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
  * @file
  * video field order filter, heavily influenced by vf_pad.c
  */
 
 #include "libavutil/imgutils.h"
1d9c2dc8
 #include "libavutil/internal.h"
a39c1540
 #include "libavutil/opt.h"
aa0c515a
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
b74a1da4
 #include "formats.h"
9d0bfc50
 #include "internal.h"
803391f7
 #include "video.h"
aa0c515a
 
58400ac1
 typedef struct FieldOrderContext {
aeac1dae
     const AVClass *class;
a39c1540
     int dst_tff;               ///< output bff/tff
aa0c515a
     int          line_size[4]; ///< bytes of pixel data per line for each plane
 } FieldOrderContext;
 
 static int query_formats(AVFilterContext *ctx)
 {
     AVFilterFormats  *formats;
716d413c
     enum AVPixelFormat pix_fmt;
aa0c515a
     int              ret;
 
     /** accept any input pixel format that is not hardware accelerated, not
      *  a bitstream format, and does not have vertically sub-sampled chroma */
     if (ctx->inputs[0]) {
a7d070ac
         const AVPixFmtDescriptor *desc = NULL;
aa0c515a
         formats = NULL;
a7d070ac
         while ((desc = av_pix_fmt_desc_next(desc))) {
             pix_fmt = av_pix_fmt_desc_get_id(desc);
e6c4ac7b
             if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
33dacda4
                   desc->flags & AV_PIX_FMT_FLAG_PAL     ||
e6c4ac7b
                   desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
59ee9f78
                 desc->nb_components && !desc->log2_chroma_h &&
6aaac24d
                 (ret = ff_add_format(&formats, pix_fmt)) < 0)
aa0c515a
                 return ret;
59ee9f78
         }
6aaac24d
         if ((ret = ff_formats_ref(formats, &ctx->inputs[0]->out_formats)) < 0 ||
             (ret = ff_formats_ref(formats, &ctx->outputs[0]->in_formats)) < 0)
             return ret;
aa0c515a
     }
 
     return 0;
 }
 
 static int config_input(AVFilterLink *inlink)
 {
d64cf54b
     AVFilterContext   *ctx = inlink->dst;
     FieldOrderContext *s   = ctx->priv;
aa0c515a
 
df7e3572
     return av_image_fill_linesizes(s->line_size, inlink->format, inlink->w);
aa0c515a
 }
 
7e350379
 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
aa0c515a
 {
e67fdbff
     AVFilterContext   *ctx     = inlink->dst;
     FieldOrderContext *s       = ctx->priv;
     AVFilterLink      *outlink = ctx->outputs[0];
6770fa4d
     int h, plane, src_line_step, dst_line_step, line_size, line;
     uint8_t *dst, *src;
     AVFrame *out;
aa0c515a
 
7e350379
     if (!frame->interlaced_frame ||
00d7369d
         frame->top_field_first == s->dst_tff) {
         av_log(ctx, AV_LOG_VERBOSE,
                "Skipping %s.\n",
                frame->interlaced_frame ?
                "frame with same field order" : "progressive frame");
e67fdbff
         return ff_filter_frame(outlink, frame);
00d7369d
     }
aa0c515a
 
6770fa4d
     if (av_frame_is_writable(frame)) {
         out = frame;
     } else {
         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
         if (!out) {
             av_frame_free(&frame);
             return AVERROR(ENOMEM);
         }
         av_frame_copy_props(out, frame);
     }
 
1a3eb042
     av_log(ctx, AV_LOG_TRACE,
49dd71a6
             "picture will move %s one line\n",
             s->dst_tff ? "up" : "down");
7e350379
     h = frame->height;
e43a0a23
     for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) {
6770fa4d
         dst_line_step = out->linesize[plane];
         src_line_step = frame->linesize[plane];
49dd71a6
         line_size = s->line_size[plane];
6770fa4d
         dst = out->data[plane];
         src = frame->data[plane];
49dd71a6
         if (s->dst_tff) {
             /** Move every line up one line, working from
              *  the top to the bottom of the frame.
              *  The original top line is lost.
              *  The new last line is created as a copy of the
              *  penultimate line from that field. */
             for (line = 0; line < h; line++) {
7e350379
                 if (1 + line < frame->height) {
6770fa4d
                     memcpy(dst, src + src_line_step, line_size);
49dd71a6
                 } else {
6770fa4d
                     memcpy(dst, src - 2 * src_line_step, line_size);
aa0c515a
                 }
6770fa4d
                 dst += dst_line_step;
                 src += src_line_step;
49dd71a6
             }
         } else {
             /** Move every line down one line, working from
              *  the bottom to the top of the frame.
              *  The original bottom line is lost.
              *  The new first line is created as a copy of the
              *  second line from that field. */
6770fa4d
             dst += (h - 1) * dst_line_step;
             src += (h - 1) * src_line_step;
49dd71a6
             for (line = h - 1; line >= 0 ; line--) {
                 if (line > 0) {
6770fa4d
                     memcpy(dst, src - src_line_step, line_size);
49dd71a6
                 } else {
6770fa4d
                     memcpy(dst, src + 2 * src_line_step, line_size);
aa0c515a
                 }
6770fa4d
                 dst -= dst_line_step;
                 src -= src_line_step;
aa0c515a
             }
         }
49dd71a6
     }
6770fa4d
     out->top_field_first = s->dst_tff;
aa0c515a
 
6770fa4d
     if (frame != out)
         av_frame_free(&frame);
     return ff_filter_frame(outlink, out);
aa0c515a
 }
 
a39c1540
 #define OFFSET(x) offsetof(FieldOrderContext, x)
b8a1f8b4
 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
 static const AVOption fieldorder_options[] = {
a39c1540
     { "order", "output field order", OFFSET(dst_tff), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "order" },
b8a1f8b4
         { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .flags=FLAGS, .unit = "order" },
         { "tff", "top field first",    0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .flags=FLAGS, .unit = "order" },
b211607b
     { NULL }
a39c1540
 };
 
b8a1f8b4
 AVFILTER_DEFINE_CLASS(fieldorder);
a39c1540
 
568c70e7
 static const AVFilterPad avfilter_vf_fieldorder_inputs[] = {
     {
b211607b
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
         .config_props = config_input,
         .filter_frame = filter_frame,
568c70e7
     },
     { NULL }
 };
 
 static const AVFilterPad avfilter_vf_fieldorder_outputs[] = {
     {
         .name = "default",
         .type = AVMEDIA_TYPE_VIDEO,
     },
     { NULL }
 };
 
cd43ca04
 AVFilter ff_vf_fieldorder = {
aa0c515a
     .name          = "fieldorder",
     .description   = NULL_IF_CONFIG_SMALL("Set the field order."),
     .priv_size     = sizeof(FieldOrderContext),
a39c1540
     .priv_class    = &fieldorder_class,
aa0c515a
     .query_formats = query_formats,
568c70e7
     .inputs        = avfilter_vf_fieldorder_inputs,
     .outputs       = avfilter_vf_fieldorder_outputs,
86cb986c
     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
aa0c515a
 };