vf_fieldorder.c 6.38 KB
Newer Older
1 2 3
/*
 * Copyright (c) 2011 Mark Himsley
 *
4
 * This file is part of Libav.
5
 *
6
 * Libav is free software; you can redistribute it and/or
7 8 9 10
 * 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.
 *
11
 * Libav is distributed in the hope that it will be useful,
12 13 14 15 16
 * 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
17
 * License along with Libav; if not, write to the Free Software
18 19 20 21 22 23 24 25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * video field order filter, heavily influenced by vf_pad.c
 */

26 27 28
#include <stdio.h>
#include <string.h>

29
#include "libavutil/imgutils.h"
30
#include "libavutil/internal.h"
31
#include "libavutil/opt.h"
32 33
#include "libavutil/pixdesc.h"
#include "avfilter.h"
34
#include "formats.h"
35
#include "internal.h"
36
#include "video.h"
37

38
typedef struct FieldOrderContext {
39 40
    const AVClass *class;
    int dst_tff;               ///< output bff/tff
41 42 43 44 45 46
    int          line_size[4]; ///< bytes of pixel data per line for each plane
} FieldOrderContext;

static int query_formats(AVFilterContext *ctx)
{
    AVFilterFormats  *formats;
47
    enum AVPixelFormat pix_fmt;
48 49 50 51 52
    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]) {
53
        const AVPixFmtDescriptor *desc = NULL;
54
        formats = NULL;
55 56
        while ((desc = av_pix_fmt_desc_next(desc))) {
            pix_fmt = av_pix_fmt_desc_get_id(desc);
57 58
            if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
                  desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
59 60
                desc->nb_components && !desc->log2_chroma_h &&
                (ret = ff_add_format(&formats, pix_fmt)) < 0) {
61
                ff_formats_unref(&formats);
62 63
                return ret;
            }
64
        }
65 66
        ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
        ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
67 68 69 70 71 72 73
    }

    return 0;
}

static int config_input(AVFilterLink *inlink)
{
74 75
    AVFilterContext   *ctx = inlink->dst;
    FieldOrderContext *s   = ctx->priv;
76 77 78 79 80
    int               plane;

    /** full an array with the number of bytes that the video
     *  data occupies per line for each plane of the input video */
    for (plane = 0; plane < 4; plane++) {
81 82
        s->line_size[plane] = av_image_get_linesize(inlink->format, inlink->w,
                                                    plane);
83 84 85 86 87
    }

    return 0;
}

88
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
89
{
90 91 92 93 94
    AVFilterContext   *ctx     = inlink->dst;
    FieldOrderContext *s       = ctx->priv;
    AVFilterLink      *outlink = ctx->outputs[0];
    int h, plane, line_step, line_size, line;
    uint8_t *data;
95

96
    if (!frame->interlaced_frame ||
97 98 99 100 101
        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");
102
        return ff_filter_frame(outlink, frame);
103
    }
104

Anton Khirnov's avatar
Anton Khirnov committed
105 106 107
    av_dlog(ctx,
            "picture will move %s one line\n",
            s->dst_tff ? "up" : "down");
108
    h = frame->height;
Anton Khirnov's avatar
Anton Khirnov committed
109 110 111 112 113 114 115 116 117 118 119
    for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
        line_step = frame->linesize[plane];
        line_size = s->line_size[plane];
        data = frame->data[plane];
        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++) {
120
                if (1 + line < frame->height) {
Anton Khirnov's avatar
Anton Khirnov committed
121 122 123
                    memcpy(data, data + line_step, line_size);
                } else {
                    memcpy(data, data - line_step - line_step, line_size);
124
                }
Anton Khirnov's avatar
Anton Khirnov committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138
                data += line_step;
            }
        } 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. */
            data += (h - 1) * line_step;
            for (line = h - 1; line >= 0 ; line--) {
                if (line > 0) {
                    memcpy(data, data - line_step, line_size);
                } else {
                    memcpy(data, data + line_step + line_step, line_size);
139
                }
Anton Khirnov's avatar
Anton Khirnov committed
140
                data -= line_step;
141 142
            }
        }
Anton Khirnov's avatar
Anton Khirnov committed
143
    }
144
    frame->top_field_first = s->dst_tff;
145

146
    return ff_filter_frame(outlink, frame);
147 148
}

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
#define OFFSET(x) offsetof(FieldOrderContext, x)
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
static const AVOption options[] = {
    { "order", "output field order", OFFSET(dst_tff), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "order" },
        { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .unit = "order" },
        { "tff", "top field first",    0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .unit = "order" },
    { NULL },
};

static const AVClass fieldorder_class = {
    .class_name = "fieldorder",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};

165 166 167 168 169
static const AVFilterPad avfilter_vf_fieldorder_inputs[] = {
    {
        .name             = "default",
        .type             = AVMEDIA_TYPE_VIDEO,
        .config_props     = config_input,
170
        .filter_frame     = filter_frame,
171
        .needs_writable   = 1,
172 173 174 175 176 177 178 179 180 181 182 183
    },
    { NULL }
};

static const AVFilterPad avfilter_vf_fieldorder_outputs[] = {
    {
        .name = "default",
        .type = AVMEDIA_TYPE_VIDEO,
    },
    { NULL }
};

184
AVFilter ff_vf_fieldorder = {
185 186 187
    .name          = "fieldorder",
    .description   = NULL_IF_CONFIG_SMALL("Set the field order."),
    .priv_size     = sizeof(FieldOrderContext),
188
    .priv_class    = &fieldorder_class,
189
    .query_formats = query_formats,
190 191
    .inputs        = avfilter_vf_fieldorder_inputs,
    .outputs       = avfilter_vf_fieldorder_outputs,
192
};