2011-04-10 19:18:03 +02:00
|
|
|
/*
|
|
|
|
* 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"
|
2012-08-06 15:49:32 +02:00
|
|
|
#include "libavutil/internal.h"
|
2013-02-25 21:21:29 +01:00
|
|
|
#include "libavutil/opt.h"
|
2011-04-10 19:18:03 +02:00
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
#include "avfilter.h"
|
2012-05-30 10:12:55 +02:00
|
|
|
#include "formats.h"
|
2012-06-12 20:12:42 +02:00
|
|
|
#include "internal.h"
|
2012-05-30 11:20:32 +02:00
|
|
|
#include "video.h"
|
2011-04-10 19:18:03 +02:00
|
|
|
|
2014-04-11 11:54:15 +02:00
|
|
|
typedef struct FieldOrderContext {
|
2013-03-10 17:36:29 +01:00
|
|
|
const AVClass *class;
|
2013-02-25 21:21:29 +01:00
|
|
|
int dst_tff; ///< output bff/tff
|
2011-04-10 19:18:03 +02:00
|
|
|
int line_size[4]; ///< bytes of pixel data per line for each plane
|
|
|
|
} FieldOrderContext;
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AVFilterFormats *formats;
|
2012-10-06 12:10:34 +02:00
|
|
|
enum AVPixelFormat pix_fmt;
|
2011-04-10 19:18:03 +02:00
|
|
|
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]) {
|
2014-05-24 11:15:15 +02:00
|
|
|
const AVPixFmtDescriptor *desc = NULL;
|
2011-04-10 19:18:03 +02:00
|
|
|
formats = NULL;
|
2014-05-24 11:15:15 +02:00
|
|
|
while ((desc = av_pix_fmt_desc_next(desc))) {
|
|
|
|
pix_fmt = av_pix_fmt_desc_get_id(desc);
|
2013-05-12 15:41:49 +02:00
|
|
|
if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
|
2013-09-06 20:32:25 +02:00
|
|
|
desc->flags & AV_PIX_FMT_FLAG_PAL ||
|
2013-05-12 15:41:49 +02:00
|
|
|
desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
|
2012-10-06 13:29:37 +02:00
|
|
|
desc->nb_components && !desc->log2_chroma_h &&
|
|
|
|
(ret = ff_add_format(&formats, pix_fmt)) < 0) {
|
2012-05-30 10:12:55 +02:00
|
|
|
ff_formats_unref(&formats);
|
2011-04-10 19:18:03 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2012-10-06 13:29:37 +02:00
|
|
|
}
|
2012-05-30 10:12:55 +02:00
|
|
|
ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
|
|
|
|
ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
|
2011-04-10 19:18:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_input(AVFilterLink *inlink)
|
|
|
|
{
|
2013-03-18 20:44:36 +01:00
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
FieldOrderContext *s = ctx->priv;
|
2011-04-10 19:18:03 +02:00
|
|
|
|
2013-09-06 16:04:48 +02:00
|
|
|
return av_image_fill_linesizes(s->line_size, inlink->format, inlink->w);
|
2011-04-10 19:18:03 +02:00
|
|
|
}
|
|
|
|
|
2012-11-28 08:41:07 +01:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
|
2011-04-10 19:18:03 +02:00
|
|
|
{
|
2012-11-28 18:50:44 +01:00
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
FieldOrderContext *s = ctx->priv;
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2013-09-06 16:34:28 +02:00
|
|
|
int h, plane, src_line_step, dst_line_step, line_size, line;
|
|
|
|
uint8_t *dst, *src;
|
|
|
|
AVFrame *out;
|
2011-04-10 19:18:03 +02:00
|
|
|
|
2012-11-28 08:41:07 +01:00
|
|
|
if (!frame->interlaced_frame ||
|
2013-10-22 09:21:18 +02:00
|
|
|
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");
|
2012-11-28 18:50:44 +01:00
|
|
|
return ff_filter_frame(outlink, frame);
|
2013-10-22 09:21:18 +02:00
|
|
|
}
|
2011-04-10 19:18:03 +02:00
|
|
|
|
2013-09-06 16:34:28 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-03-16 09:57:35 +01:00
|
|
|
av_log(ctx, AV_LOG_TRACE,
|
2012-11-27 08:59:22 +01:00
|
|
|
"picture will move %s one line\n",
|
|
|
|
s->dst_tff ? "up" : "down");
|
2012-11-28 08:41:07 +01:00
|
|
|
h = frame->height;
|
2013-08-03 18:54:43 +02:00
|
|
|
for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) {
|
2013-09-06 16:34:28 +02:00
|
|
|
dst_line_step = out->linesize[plane];
|
|
|
|
src_line_step = frame->linesize[plane];
|
2012-11-27 08:59:22 +01:00
|
|
|
line_size = s->line_size[plane];
|
2013-09-06 16:34:28 +02:00
|
|
|
dst = out->data[plane];
|
|
|
|
src = frame->data[plane];
|
2012-11-27 08:59:22 +01:00
|
|
|
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++) {
|
2012-11-28 08:41:07 +01:00
|
|
|
if (1 + line < frame->height) {
|
2013-09-06 16:34:28 +02:00
|
|
|
memcpy(dst, src + src_line_step, line_size);
|
2012-11-27 08:59:22 +01:00
|
|
|
} else {
|
2013-09-06 16:34:28 +02:00
|
|
|
memcpy(dst, src - 2 * src_line_step, line_size);
|
2011-04-10 19:18:03 +02:00
|
|
|
}
|
2013-09-06 16:34:28 +02:00
|
|
|
dst += dst_line_step;
|
|
|
|
src += src_line_step;
|
2012-11-27 08:59:22 +01:00
|
|
|
}
|
|
|
|
} 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. */
|
2013-09-06 16:34:28 +02:00
|
|
|
dst += (h - 1) * dst_line_step;
|
|
|
|
src += (h - 1) * src_line_step;
|
2012-11-27 08:59:22 +01:00
|
|
|
for (line = h - 1; line >= 0 ; line--) {
|
|
|
|
if (line > 0) {
|
2013-09-06 16:34:28 +02:00
|
|
|
memcpy(dst, src - src_line_step, line_size);
|
2012-11-27 08:59:22 +01:00
|
|
|
} else {
|
2013-09-06 16:34:28 +02:00
|
|
|
memcpy(dst, src + 2 * src_line_step, line_size);
|
2011-04-10 19:18:03 +02:00
|
|
|
}
|
2013-09-06 16:34:28 +02:00
|
|
|
dst -= dst_line_step;
|
|
|
|
src -= src_line_step;
|
2011-04-10 19:18:03 +02:00
|
|
|
}
|
|
|
|
}
|
2012-11-27 08:59:22 +01:00
|
|
|
}
|
2013-09-06 16:34:28 +02:00
|
|
|
out->top_field_first = s->dst_tff;
|
2011-04-10 19:18:03 +02:00
|
|
|
|
2013-09-06 16:34:28 +02:00
|
|
|
if (frame != out)
|
|
|
|
av_frame_free(&frame);
|
|
|
|
return ff_filter_frame(outlink, out);
|
2011-04-10 19:18:03 +02:00
|
|
|
}
|
|
|
|
|
2013-02-25 21:21:29 +01:00
|
|
|
#define OFFSET(x) offsetof(FieldOrderContext, x)
|
2013-04-10 19:02:13 +02:00
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
|
|
|
|
|
|
|
|
static const AVOption fieldorder_options[] = {
|
2013-02-25 21:21:29 +01:00
|
|
|
{ "order", "output field order", OFFSET(dst_tff), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "order" },
|
2013-04-10 19:02:13 +02:00
|
|
|
{ "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" },
|
2013-09-07 14:13:50 +02:00
|
|
|
{ NULL }
|
2013-02-25 21:21:29 +01:00
|
|
|
};
|
|
|
|
|
2013-04-10 19:02:13 +02:00
|
|
|
AVFILTER_DEFINE_CLASS(fieldorder);
|
2013-02-25 21:21:29 +01:00
|
|
|
|
2012-07-24 15:14:01 +02:00
|
|
|
static const AVFilterPad avfilter_vf_fieldorder_inputs[] = {
|
|
|
|
{
|
2013-09-07 14:13:50 +02:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_input,
|
|
|
|
.filter_frame = filter_frame,
|
2012-07-24 15:14:01 +02:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_vf_fieldorder_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-10-28 07:44:24 +01:00
|
|
|
AVFilter ff_vf_fieldorder = {
|
2011-04-10 19:18:03 +02:00
|
|
|
.name = "fieldorder",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Set the field order."),
|
|
|
|
.priv_size = sizeof(FieldOrderContext),
|
2013-02-25 21:21:29 +01:00
|
|
|
.priv_class = &fieldorder_class,
|
2011-04-10 19:18:03 +02:00
|
|
|
.query_formats = query_formats,
|
2012-07-24 15:14:01 +02:00
|
|
|
.inputs = avfilter_vf_fieldorder_inputs,
|
|
|
|
.outputs = avfilter_vf_fieldorder_outputs,
|
2013-09-06 16:07:57 +02:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
|
2011-04-10 19:18:03 +02:00
|
|
|
};
|