2011-04-10 17:18:03 +00: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
|
|
|
|
*/
|
|
|
|
|
2013-03-10 16:36:29 +00:00
|
|
|
#include "libavutil/opt.h"
|
2011-04-10 17:18:03 +00:00
|
|
|
#include "libavutil/imgutils.h"
|
2012-08-06 13:49:32 +00:00
|
|
|
#include "libavutil/internal.h"
|
2011-04-10 17:18:03 +00:00
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
#include "avfilter.h"
|
2012-05-30 08:12:55 +00:00
|
|
|
#include "formats.h"
|
2012-06-12 18:12:42 +00:00
|
|
|
#include "internal.h"
|
2012-05-30 09:20:32 +00:00
|
|
|
#include "video.h"
|
2011-04-10 17:18:03 +00:00
|
|
|
|
2013-03-10 16:36:29 +00:00
|
|
|
enum FieldOrder {
|
|
|
|
ORDER_TFF,
|
|
|
|
ORDER_BFF,
|
|
|
|
ORDER_NB,
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const AVClass *class;
|
|
|
|
enum FieldOrder order;
|
2011-04-10 17:18:03 +00:00
|
|
|
unsigned int dst_tff; ///< output bff/tff
|
|
|
|
int line_size[4]; ///< bytes of pixel data per line for each plane
|
|
|
|
} FieldOrderContext;
|
|
|
|
|
2013-03-10 16:36:29 +00:00
|
|
|
#define OFFSET(x) offsetof(FieldOrderContext, x)
|
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
|
|
|
|
|
|
|
|
static const AVOption fieldorder_options[] = {
|
|
|
|
{ "order", "set output field order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=ORDER_TFF}, 0, ORDER_NB-1, FLAGS, "order" },
|
|
|
|
{ "tff", "set top field first", 0, AV_OPT_TYPE_CONST, {.i64=ORDER_TFF}, .flags=FLAGS, .unit="order" },
|
|
|
|
{ "bff", "set bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=ORDER_BFF}, .flags=FLAGS, .unit="order" },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(fieldorder);
|
|
|
|
|
2012-06-21 05:55:56 +00:00
|
|
|
static av_cold int init(AVFilterContext *ctx, const char *args)
|
2011-04-10 17:18:03 +00:00
|
|
|
{
|
|
|
|
FieldOrderContext *fieldorder = ctx->priv;
|
2013-03-10 16:36:29 +00:00
|
|
|
int ret;
|
|
|
|
static const char *shorthand[] = { "order", NULL };
|
2011-04-10 17:18:03 +00:00
|
|
|
|
2013-03-10 16:36:29 +00:00
|
|
|
fieldorder->class = &fieldorder_class;
|
|
|
|
av_opt_set_defaults(fieldorder);
|
|
|
|
|
|
|
|
if ((ret = av_opt_set_from_string(fieldorder, args, shorthand, "=", ":")) < 0)
|
|
|
|
return ret;
|
2011-04-10 17:18:03 +00:00
|
|
|
|
2013-03-10 16:36:29 +00:00
|
|
|
fieldorder->dst_tff = fieldorder->order == ORDER_TFF;
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "tff:%d\n", fieldorder->dst_tff);
|
2011-04-10 17:18:03 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AVFilterFormats *formats;
|
2012-10-06 10:10:34 +00:00
|
|
|
enum AVPixelFormat pix_fmt;
|
2011-04-10 17:18:03 +00: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]) {
|
|
|
|
formats = NULL;
|
2012-10-06 11:29:37 +00:00
|
|
|
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) {
|
2012-05-30 08:12:55 +00:00
|
|
|
ff_formats_unref(&formats);
|
2011-04-10 17:18:03 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2012-10-06 11:29:37 +00:00
|
|
|
}
|
2012-05-30 08:12:55 +00:00
|
|
|
ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
|
|
|
|
ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
|
2011-04-10 17:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-11-28 07:41:07 +00:00
|
|
|
static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
|
2011-04-10 17:18:03 +00:00
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
|
2012-11-28 07:41:07 +00:00
|
|
|
return ff_get_video_buffer(outlink, w, h);
|
2011-04-10 17:18:03 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 07:41:07 +00:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
|
2011-04-10 17:18:03 +00:00
|
|
|
{
|
2012-11-28 17:50:44 +00:00
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
FieldOrderContext *s = ctx->priv;
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
int h, plane, line_step, line_size, line;
|
|
|
|
uint8_t *data;
|
2011-04-10 17:18:03 +00:00
|
|
|
|
2012-11-28 07:41:07 +00:00
|
|
|
if (!frame->interlaced_frame ||
|
|
|
|
frame->top_field_first == s->dst_tff)
|
2012-11-28 17:50:44 +00:00
|
|
|
return ff_filter_frame(outlink, frame);
|
2011-04-10 17:18:03 +00:00
|
|
|
|
2012-11-27 07:59:22 +00:00
|
|
|
av_dlog(ctx,
|
|
|
|
"picture will move %s one line\n",
|
|
|
|
s->dst_tff ? "up" : "down");
|
2012-11-28 07:41:07 +00:00
|
|
|
h = frame->height;
|
2012-11-27 07:59:22 +00:00
|
|
|
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++) {
|
2012-11-28 07:41:07 +00:00
|
|
|
if (1 + line < frame->height) {
|
2012-11-27 07:59:22 +00:00
|
|
|
memcpy(data, data + line_step, line_size);
|
|
|
|
} else {
|
|
|
|
memcpy(data, data - line_step - line_step, line_size);
|
2011-04-10 17:18:03 +00:00
|
|
|
}
|
2012-11-27 07:59:22 +00:00
|
|
|
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);
|
2011-04-10 17:18:03 +00:00
|
|
|
}
|
2012-11-27 07:59:22 +00:00
|
|
|
data -= line_step;
|
2011-04-10 17:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-27 07:59:22 +00:00
|
|
|
}
|
2012-11-28 07:41:07 +00:00
|
|
|
frame->top_field_first = s->dst_tff;
|
2011-04-10 17:18:03 +00:00
|
|
|
|
2012-11-28 17:50:44 +00:00
|
|
|
return ff_filter_frame(outlink, frame);
|
2011-04-10 17:18:03 +00:00
|
|
|
}
|
|
|
|
|
2012-07-24 13:14:01 +00:00
|
|
|
static const AVFilterPad avfilter_vf_fieldorder_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_input,
|
|
|
|
.get_video_buffer = get_video_buffer,
|
2012-11-28 17:50:44 +00:00
|
|
|
.filter_frame = filter_frame,
|
2012-11-28 07:41:07 +00:00
|
|
|
.needs_writable = 1,
|
2012-07-24 13:14:01 +00:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_vf_fieldorder_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2011-04-10 17:18:03 +00:00
|
|
|
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,
|
2012-07-24 13:14:01 +00:00
|
|
|
.inputs = avfilter_vf_fieldorder_inputs,
|
|
|
|
.outputs = avfilter_vf_fieldorder_outputs,
|
2013-03-10 16:36:29 +00:00
|
|
|
.priv_class = &fieldorder_class,
|
2011-04-10 17:18:03 +00:00
|
|
|
};
|