vf_fieldorder.c 8.75 KB
Newer Older
Mark Himsley's avatar
Mark Himsley committed
1 2 3
/*
 * Copyright (c) 2011 Mark Himsley
 *
4
 * This file is part of Libav.
Mark Himsley's avatar
Mark Himsley committed
5
 *
6
 * Libav is free software; you can redistribute it and/or
Mark Himsley's avatar
Mark Himsley committed
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,
Mark Himsley's avatar
Mark Himsley committed
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
Mark Himsley's avatar
Mark Himsley committed
18 19 20 21 22 23 24 25 26 27
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

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

/* #define DEBUG */

28 29 30
#include <stdio.h>
#include <string.h>

Mark Himsley's avatar
Mark Himsley committed
31
#include "libavutil/imgutils.h"
32
#include "libavutil/internal.h"
Mark Himsley's avatar
Mark Himsley committed
33 34
#include "libavutil/pixdesc.h"
#include "avfilter.h"
35
#include "formats.h"
36
#include "internal.h"
37
#include "video.h"
Mark Himsley's avatar
Mark Himsley committed
38 39 40 41 42 43 44

typedef struct
{
    unsigned int dst_tff;      ///< output bff/tff
    int          line_size[4]; ///< bytes of pixel data per line for each plane
} FieldOrderContext;

45
static av_cold int init(AVFilterContext *ctx, const char *args)
Mark Himsley's avatar
Mark Himsley committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
{
    FieldOrderContext *fieldorder = ctx->priv;

    const char *tff = "tff";
    const char *bff = "bff";

    if (!args) {
        fieldorder->dst_tff = 1;
    } else if (sscanf(args, "%u", &fieldorder->dst_tff) == 1) {
        fieldorder->dst_tff = !!fieldorder->dst_tff;
    } else if (!strcmp(tff, args)) {
        fieldorder->dst_tff = 1;
    } else if (!strcmp(bff, args)) {
        fieldorder->dst_tff = 0;
    } else {
        av_log(ctx, AV_LOG_ERROR, "Invalid argument '%s'.\n", args);
        return AVERROR(EINVAL);
    }

65
    av_log(ctx, AV_LOG_VERBOSE, "output field order: %s\n",
Mark Himsley's avatar
Mark Himsley committed
66 67 68 69 70 71 72 73
            fieldorder->dst_tff ? tff : bff);

    return 0;
}

static int query_formats(AVFilterContext *ctx)
{
    AVFilterFormats  *formats;
74
    enum AVPixelFormat pix_fmt;
Mark Himsley's avatar
Mark Himsley committed
75 76 77 78 79 80
    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]) {
        formats = NULL;
81 82 83 84 85 86
        for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++) {
            const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
            if (!(desc->flags & PIX_FMT_HWACCEL ||
                  desc->flags & PIX_FMT_BITSTREAM) &&
                desc->nb_components && !desc->log2_chroma_h &&
                (ret = ff_add_format(&formats, pix_fmt)) < 0) {
87
                ff_formats_unref(&formats);
Mark Himsley's avatar
Mark Himsley committed
88 89
                return ret;
            }
90
        }
91 92
        ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
        ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
Mark Himsley's avatar
Mark Himsley committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    }

    return 0;
}

static int config_input(AVFilterLink *inlink)
{
    AVFilterContext   *ctx        = inlink->dst;
    FieldOrderContext *fieldorder = ctx->priv;
    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++) {
        fieldorder->line_size[plane] = av_image_get_linesize(
                inlink->format,
                inlink->w,
                plane);
    }

    return 0;
}

static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
{
    AVFilterContext   *ctx        = inlink->dst;
    AVFilterLink      *outlink    = ctx->outputs[0];

121
    return ff_get_video_buffer(outlink, perms, w, h);
Mark Himsley's avatar
Mark Himsley committed
122 123
}

124
static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
Mark Himsley's avatar
Mark Himsley committed
125 126 127 128
{
    AVFilterContext   *ctx        = inlink->dst;
    AVFilterLink      *outlink    = ctx->outputs[0];

129
    AVFilterBufferRef *outpicref, *for_next_filter;
130
    int ret = 0;
Mark Himsley's avatar
Mark Himsley committed
131 132

    outpicref = avfilter_ref_buffer(inpicref, ~0);
133 134
    if (!outpicref)
        return AVERROR(ENOMEM);
Mark Himsley's avatar
Mark Himsley committed
135

136
    for_next_filter = avfilter_ref_buffer(outpicref, ~0);
137 138 139 140 141 142 143 144 145 146 147 148 149
    if (!for_next_filter) {
        avfilter_unref_bufferp(&outpicref);
        return AVERROR(ENOMEM);
    }

    ret = ff_start_frame(outlink, for_next_filter);
    if (ret < 0) {
        avfilter_unref_bufferp(&outpicref);
        return ret;
    }

    outlink->out_buf = outpicref;
    return 0;
Mark Himsley's avatar
Mark Himsley committed
150 151
}

152
static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
Mark Himsley's avatar
Mark Himsley committed
153 154 155 156 157 158 159 160 161 162 163 164 165
{
    AVFilterContext   *ctx        = inlink->dst;
    FieldOrderContext *fieldorder = ctx->priv;
    AVFilterLink      *outlink    = ctx->outputs[0];

    AVFilterBufferRef *inpicref   = inlink->cur_buf;

    /** can only currently do slices if this filter is doing nothing
     *  because this filter is moving picture content, the output
     *  slice will contain different video lines than the input slice
     *  and that complexity will be added later */
    if (  !inpicref->video->interlaced
        || inpicref->video->top_field_first == fieldorder->dst_tff) {
166
        return ff_draw_slice(outlink, y, h, slice_dir);
Mark Himsley's avatar
Mark Himsley committed
167
    }
168
    return 0;
Mark Himsley's avatar
Mark Himsley committed
169 170
}

171
static int end_frame(AVFilterLink *inlink)
Mark Himsley's avatar
Mark Himsley committed
172 173 174 175 176 177 178 179
{
    AVFilterContext   *ctx        = inlink->dst;
    FieldOrderContext *fieldorder = ctx->priv;
    AVFilterLink      *outlink    = ctx->outputs[0];

    AVFilterBufferRef *inpicref   = inlink->cur_buf;
    AVFilterBufferRef *outpicref  = outlink->out_buf;

Mans Rullgard's avatar
Mans Rullgard committed
180
    int               h, plane, line_step, line_size, line;
Mark Himsley's avatar
Mark Himsley committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    uint8_t           *cpy_src, *cpy_dst;

    if (    inpicref->video->interlaced
         && inpicref->video->top_field_first != fieldorder->dst_tff) {
        av_dlog(ctx,
                "picture will move %s one line\n",
                fieldorder->dst_tff ? "up" : "down");
        h = inpicref->video->h;
        for (plane = 0; plane < 4 && inpicref->data[plane]; plane++) {
            line_step = inpicref->linesize[plane];
            line_size = fieldorder->line_size[plane];
            cpy_src = inpicref->data[plane];
            cpy_dst = outpicref->data[plane];
            if (fieldorder->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++) {
                    if (1 + line < outpicref->video->h) {
                        memcpy(cpy_dst, cpy_src + line_step, line_size);
                    } else {
                        memcpy(cpy_dst, cpy_src - line_step - line_step, line_size);
                    }
                    cpy_src += line_step;
                    cpy_dst += 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. */
                cpy_src += (h - 1) * line_step;
                cpy_dst += (h - 1) * line_step;
                for (line = h - 1; line >= 0 ; line--) {
                    if (line > 0) {
                        memcpy(cpy_dst, cpy_src - line_step, line_size);
                    } else {
                        memcpy(cpy_dst, cpy_src + line_step + line_step, line_size);
                    }
                    cpy_src -= line_step;
                    cpy_dst -= line_step;
                }
            }
        }
        outpicref->video->top_field_first = fieldorder->dst_tff;
229
        ff_draw_slice(outlink, 0, h, 1);
Mark Himsley's avatar
Mark Himsley committed
230 231 232 233 234
    } else {
        av_dlog(ctx,
                "not interlaced or field order already correct\n");
    }

235
    return ff_end_frame(outlink);
Mark Himsley's avatar
Mark Himsley committed
236 237
}

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
static const AVFilterPad avfilter_vf_fieldorder_inputs[] = {
    {
        .name             = "default",
        .type             = AVMEDIA_TYPE_VIDEO,
        .config_props     = config_input,
        .start_frame      = start_frame,
        .get_video_buffer = get_video_buffer,
        .draw_slice       = draw_slice,
        .end_frame        = end_frame,
        .min_perms        = AV_PERM_READ,
        .rej_perms        = AV_PERM_REUSE2 | AV_PERM_PRESERVE,
    },
    { NULL }
};

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

Mark Himsley's avatar
Mark Himsley committed
261 262 263 264 265 266
AVFilter avfilter_vf_fieldorder = {
    .name          = "fieldorder",
    .description   = NULL_IF_CONFIG_SMALL("Set the field order."),
    .init          = init,
    .priv_size     = sizeof(FieldOrderContext),
    .query_formats = query_formats,
267 268
    .inputs        = avfilter_vf_fieldorder_inputs,
    .outputs       = avfilter_vf_fieldorder_outputs,
Mark Himsley's avatar
Mark Himsley committed
269
};