---
 Changelog                  |    1 +
 doc/filters.texi           |   19 ++
 libavfilter/Makefile       |    1 +
 libavfilter/allfilters.c   |    1 +
 libavfilter/version.h      |    4 +-
 libavfilter/vf_framepack.c |  488 ++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 512 insertions(+), 2 deletions(-)
 create mode 100644 libavfilter/vf_framepack.c

diff --git a/Changelog b/Changelog
index 78b3338..5a47b18 100644
--- a/Changelog
+++ b/Changelog
@@ -51,6 +51,7 @@ version 10:
 - support for decoding through VDPAU in avconv (the -hwaccel option)
 - remove mp3_header_(de)compress bitstream filters
 - codec level stereoscopic metadata handling
+- framepack filter
 
 
 version 9:
diff --git a/doc/filters.texi b/doc/filters.texi
index b32aad1..04779bc 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -1252,6 +1252,25 @@ frames with a negative PTS.
 
 @end table
 
+@section framepack
+
+Packs two different video streams into a stereoscopic video. The two videos
+should have the same size and framerate; you may conveniently adjust those
+values with the @ref{scale} and @ref{fps} filters. If one video lasts longer
+than the other, the last frame is repeated.
+
+This filter also sets proper signalling on supported codecs.
+
+This filter accepts the following named parameters:
+@table @option
+
+@item format
+Desired framepacking format. Supported values are @var{2d} (default),
+@var{sbs}, @var{sbsqnx}, @var{tab}, @var{lines}, @var{columns}, 
@var{frameseq}, @var{check}.
+See a detailed format description in @file{libavutil/stereo3d.h}.
+
+@end table
+
 @anchor{frei0r}
 @section frei0r
 
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 96fa8c0..92c1561 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -54,6 +54,7 @@ OBJS-$(CONFIG_FADE_FILTER)                   += vf_fade.o
 OBJS-$(CONFIG_FIELDORDER_FILTER)             += vf_fieldorder.o
 OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
 OBJS-$(CONFIG_FPS_FILTER)                    += vf_fps.o
+OBJS-$(CONFIG_FRAMEPACK_FILTER)              += vf_framepack.o
 OBJS-$(CONFIG_FREI0R_FILTER)                 += vf_frei0r.o
 OBJS-$(CONFIG_GRADFUN_FILTER)                += vf_gradfun.o
 OBJS-$(CONFIG_HFLIP_FILTER)                  += vf_hflip.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index f041f5c..9702a0a 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -74,6 +74,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(FIELDORDER,     fieldorder,     vf);
     REGISTER_FILTER(FORMAT,         format,         vf);
     REGISTER_FILTER(FPS,            fps,            vf);
+    REGISTER_FILTER(FRAMEPACK,      framepack,      vf);
     REGISTER_FILTER(FREI0R,         frei0r,         vf);
     REGISTER_FILTER(GRADFUN,        gradfun,        vf);
     REGISTER_FILTER(HFLIP,          hflip,          vf);
diff --git a/libavfilter/version.h b/libavfilter/version.h
index bc079b4..3f7d9dc 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -30,8 +30,8 @@
 #include "libavutil/avutil.h"
 
 #define LIBAVFILTER_VERSION_MAJOR  3
-#define LIBAVFILTER_VERSION_MINOR  11
-#define LIBAVFILTER_VERSION_MICRO  1
+#define LIBAVFILTER_VERSION_MINOR  12
+#define LIBAVFILTER_VERSION_MICRO  0
 
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
                                                LIBAVFILTER_VERSION_MINOR, \
diff --git a/libavfilter/vf_framepack.c b/libavfilter/vf_framepack.c
new file mode 100644
index 0000000..35f5656
--- /dev/null
+++ b/libavfilter/vf_framepack.c
@@ -0,0 +1,488 @@
+/*
+ * Copyright (c) 2013 Vittorio Giovara
+ *
+ * This file is part of Libav.
+ *
+ * Libav 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.
+ *
+ * Libav 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 Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * Generate a frame packed video, by combining two views in a single surface.
+ */
+
+#include "libavutil/common.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/rational.h"
+#include "libavutil/stereo3d.h"
+
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+#define LEFT_VIEW  0
+#define RIGHT_VIEW 1
+
+typedef struct FramepackContext {
+    const AVClass *class;
+
+    const AVPixFmtDescriptor *pix_desc; ///< agreed pixel format
+
+    enum AVStereo3DType format;         ///< frame packed output
+
+    AVFrame *left;                      ///< left input frame
+    AVFrame *right;                     ///< right input frame
+} FramepackContext;
+
+static const enum AVPixelFormat formats_supported[] = {
+    AV_PIX_FMT_YUV420P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV444P,
+    AV_PIX_FMT_YUV410P,  AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVJ420P,
+    AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
+    AV_PIX_FMT_NONE
+};
+
+static int query_formats(AVFilterContext *ctx)
+{
+    ff_set_common_formats(ctx, ff_make_format_list(formats_supported));
+    return 0;
+}
+
+static av_cold void framepack_uninit(AVFilterContext *ctx)
+{
+    FramepackContext *s = ctx->priv;
+
+    // clean any leftover frame
+    av_frame_free(&s->left);
+    av_frame_free(&s->right);
+
+    av_opt_free(s);
+}
+
+static int config_input_left(AVFilterLink *inlink)
+{
+    FramepackContext *s = inlink->dst->priv;
+
+    // save pixel format which has to match the other one
+    s->pix_desc = av_pix_fmt_desc_get(inlink->format);
+
+    return 0;
+}
+
+static int config_input_right(AVFilterLink *inlink)
+{
+    FramepackContext *s = inlink->dst->priv;
+    const AVPixFmtDescriptor *right_desc = av_pix_fmt_desc_get(inlink->format);
+
+    if(!s->pix_desc || !right_desc)
+        return AVERROR_BUG;
+
+    // check input format
+    if (s->pix_desc != right_desc) {
+        av_log(inlink->dst, AV_LOG_ERROR,
+               "Left and right color spaces differ (%s vs %s).\n",
+               s->pix_desc->name, right_desc->name);
+        return AVERROR_INVALIDDATA;
+    }
+
+    return 0;
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    FramepackContext *s  = outlink->src->priv;
+
+    int width            = ctx->inputs[LEFT_VIEW]->w;
+    int height           = ctx->inputs[LEFT_VIEW]->h;
+    AVRational time_base = ctx->inputs[LEFT_VIEW]->time_base;
+
+    // check size and fps match on the other input
+    if (width  != ctx->inputs[RIGHT_VIEW]->w ||
+        height != ctx->inputs[RIGHT_VIEW]->h) {
+        av_log(ctx, AV_LOG_ERROR,
+               "Left and right sizes differ (%dx%d vs %dx%d).\n",
+               width, height,
+               ctx->inputs[RIGHT_VIEW]->w, ctx->inputs[RIGHT_VIEW]->h);
+        return AVERROR_INVALIDDATA;
+    } else if (av_cmp_q(time_base, ctx->inputs[RIGHT_VIEW]->time_base) != 0) {
+        av_log(ctx, AV_LOG_ERROR,
+               "Left and right framerates differ (%d/%d vs %d/%d).\n",
+               time_base.num, time_base.den,
+               ctx->inputs[RIGHT_VIEW]->time_base.num,
+               ctx->inputs[RIGHT_VIEW]->time_base.den);
+        return AVERROR_INVALIDDATA;
+    }
+
+    // modify output properties as needed
+    switch (s->format) {
+    case AV_STEREO3D_2D:
+        av_log(ctx, AV_LOG_WARNING,
+               "No frame packing mode selected, setting metadata only.\n");
+        break;
+    case AV_STEREO3D_FRAMESEQUENCE:
+        time_base.den *= 2;
+        break;
+    case AV_STEREO3D_COLUMNS:
+    case AV_STEREO3D_SIDEBYSIDE:
+    case AV_STEREO3D_SIDEBYSIDE_QUINCUNX:
+        width *= 2;
+        break;
+    case AV_STEREO3D_LINES:
+    case AV_STEREO3D_TOPBOTTOM:
+        height *= 2;
+        break;
+    case AV_STEREO3D_CHECKERBOARD:
+        // same w/h/tb
+        break;
+    }
+
+    outlink->w         = width;
+    outlink->h         = height;
+    outlink->time_base = time_base;
+
+    return 0;
+}
+
+static void pack_sidebyside_frame(FramepackContext *s,
+                                  AVFrame *dst,
+                                  int interleaved)
+{
+    int plane, i;
+    int length = s->left->width;
+    int lines  = s->left->height;
+
+    for (plane = 0; plane < s->pix_desc->nb_components; plane++) {
+        const uint8_t *leftp  = s->left->data[plane];
+        const uint8_t *rightp = s->right->data[plane];
+        uint8_t *dstp         = dst->data[plane];
+
+        if (plane == 1 || plane == 2) {
+            length = -(-s->left->width  >> s->pix_desc->log2_chroma_w);
+            lines  = -(-s->left->height >> s->pix_desc->log2_chroma_h);
+        }
+
+        if (!interleaved) {
+            for (i = 0; i < lines; i++) {
+                memcpy(dstp, leftp, length);
+                memcpy(dstp + length, rightp, length);
+
+                dstp   += dst->linesize[plane];
+                leftp  += s->left->linesize[plane];
+                rightp += s->right->linesize[plane];
+            }
+        } else {
+            for (i = 0; i < lines; i++) {
+                int j;
+                int k = 0;
+
+                for (j = 0; j < length; j++) {
+                    dstp[k++] = leftp[j];
+                    dstp[k++] = rightp[j];
+                }
+
+                dstp   += dst->linesize[plane];
+                leftp  += s->left->linesize[plane];
+                rightp += s->right->linesize[plane];
+            }
+        }
+    }
+
+    return;
+}
+
+static void pack_topbottom_frame(FramepackContext *s,
+                                 AVFrame *dst,
+                                 int interleaved)
+{
+    int plane, i;
+    int step   = interleaved ? 2 : 1;
+    int length = s->left->width;
+    int lines  = s->left->height;
+
+    for (plane = 0; plane < s->pix_desc->nb_components; plane++) {
+        const uint8_t *srcp;
+        uint8_t *dstp;
+
+        if (plane == 1 || plane == 2) {
+            length = -(-s->left->width  >> s->pix_desc->log2_chroma_w);
+            lines  = -(-s->left->height >> s->pix_desc->log2_chroma_h);
+        }
+
+        // copy first frame line by line
+        srcp = s->left->data[plane];
+        dstp = dst->data[plane];
+        for (i = 0; i < lines; i ++) {
+            memcpy(dstp, srcp, length);
+            dstp += dst->linesize[plane] * step;
+            srcp += s->left->linesize[plane];
+        }
+
+        // copy second frame line by line
+        srcp = s->right->data[plane];
+        if (interleaved) {
+            // reset destination pointer and offset one line
+            dstp = dst->data[plane] + dst->linesize[plane];
+        }
+        for (i = 0; i < lines; i ++) {
+            memcpy(dstp, srcp, length);
+            dstp += dst->linesize[plane] * step;
+            srcp += s->right->linesize[plane];
+        }
+    }
+
+    return;
+}
+
+static void pack_checkerboard_frame(FramepackContext *s, AVFrame *dst)
+{
+    int plane, i;
+    int length = s->left->width;
+    int lines  = s->left->height;
+
+    for (plane = 0; plane < s->pix_desc->nb_components; plane++) {
+        const uint8_t *leftp  = s->left->data[plane];
+        const uint8_t *rightp = s->right->data[plane];
+        uint8_t *dstp         = dst->data[plane];
+
+        if (plane == 1 || plane == 2) {
+            length = -(-s->left->width  >> s->pix_desc->log2_chroma_w);
+            lines  = -(-s->left->height >> s->pix_desc->log2_chroma_h);
+        }
+
+        // alternatively copy one pixel, changing source at each line
+        for (i = 0; i < lines; i++) {
+            int k;
+            for (k = 0; k < length; k += 2) {
+                dstp[k]     = i % 2 ? rightp[k] : leftp[k];
+                dstp[k + 1] = i % 2 ? leftp[k + 1] : rightp[k + 1];
+            }
+
+            dstp   += dst->linesize[plane];
+            leftp  += s->left->linesize[plane];
+            rightp += s->right->linesize[plane];
+        }
+    }
+
+    return;
+}
+
+static int set_side_data(AVFrame *out, enum AVStereo3DType format)
+{
+    AVStereo3D *stereo = av_stereo3d_create_side_data(out);
+    if (!stereo)
+        return AVERROR(ENOMEM);
+
+    stereo->type = format;
+
+    return 0;
+}
+
+
+static av_always_inline void pack_frame(FramepackContext *s, AVFrame *dst)
+{
+    switch (s->format) {
+    case AV_STEREO3D_COLUMNS:
+        pack_sidebyside_frame(s, dst, 1);
+        break;
+    case AV_STEREO3D_SIDEBYSIDE:
+    case AV_STEREO3D_SIDEBYSIDE_QUINCUNX:
+        pack_sidebyside_frame(s, dst, 0);
+        break;
+    case AV_STEREO3D_LINES:
+        pack_topbottom_frame(s, dst, 1);
+        break;
+    case AV_STEREO3D_TOPBOTTOM:
+        pack_topbottom_frame(s, dst, 0);
+        break;
+    case AV_STEREO3D_CHECKERBOARD:
+        pack_checkerboard_frame(s, dst);
+        break;
+    }
+
+    return;
+}
+
+static int filter_frame_left(AVFilterLink *inlink, AVFrame *frame)
+{
+    FramepackContext *s = inlink->dst->priv;
+
+    s->left = frame;
+
+    return 0;
+}
+
+static int filter_frame_right(AVFilterLink *inlink, AVFrame *frame)
+{
+    FramepackContext *s = inlink->dst->priv;
+
+    s->right = frame;
+
+    return 0;
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    FramepackContext *s = ctx->priv;
+    int ret, retl, retr;
+    AVFrame *out;
+    static uint64_t double_pts;
+
+    /* get a frame on the left input, on EOF reuse previous */
+    if (!s->left) {
+        retl = ff_request_frame(ctx->inputs[LEFT_VIEW]);
+        if (retl < 0 && retl != AVERROR_EOF)
+            return retl;
+    }
+    /* get a new frame on the right input, on EOF reuse previous */
+    if (!s->right) {
+        retr = ff_request_frame(ctx->inputs[RIGHT_VIEW]);
+        if (retr < 0 && retr != AVERROR_EOF)
+            return retr;
+    }
+    /* when both return values are EOF, it means that both
+     * videos are fully processed and we can stop */
+    if (retl == retr && retl == AVERROR_EOF)
+        return retl;
+
+    if (s->format == AV_STEREO3D_FRAMESEQUENCE) {
+        AVFrame *cur;
+        int i;
+        double_pts = s->left->pts;
+
+        for (i = 0; i < 2; i++) {
+            cur = i == 0 ? s->left : s->right;
+            out = av_frame_clone(cur);
+            if (!out)
+                return AVERROR(ENOMEM);
+
+            out->pts = double_pts++;
+            ret = set_side_data(out, s->format);
+            if (ret < 0)
+                return ret;
+
+            ret = ff_filter_frame(outlink, out);
+            if (ret < 0)
+                return ret;
+        }
+        if (retl != AVERROR_EOF)
+            av_frame_free(&s->left);
+        if (retr != AVERROR_EOF)
+            av_frame_free(&s->right);
+        return ret;
+    } else if (s->format == AV_STEREO3D_2D) {
+        out = av_frame_clone(s->left);
+        if (!out)
+            return AVERROR(ENOMEM);
+    } else {
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+        if (!out)
+            return AVERROR(ENOMEM);
+
+        pack_frame(s, out);
+    }
+
+    ret = av_frame_copy_props(out, s->left);
+    if (ret < 0)
+        return ret;
+
+    ret = set_side_data(out, s->format);
+    if (ret < 0)
+        return ret;
+
+    ret = ff_filter_frame(outlink, out);
+    if (ret < 0)
+        return ret;
+
+    // cleanup keeping a valid frame on EOF
+    if (retl != AVERROR_EOF)
+        av_frame_free(&s->left);
+    if (retr != AVERROR_EOF)
+        av_frame_free(&s->right);
+
+    return ret;
+}
+
+#define OFFSET(x) offsetof(FramepackContext, x)
+#define V AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption options[] = {
+    { "format", "Frame pack output format", OFFSET(format), AV_OPT_TYPE_INT,
+        { .i64 = AV_STEREO3D_2D }, 0, INT_MAX, .flags = V, .unit = "format" },
+    { "2d", "Views are not stereoscopic (right input ignored)", 0, 
AV_OPT_TYPE_CONST,
+        { .i64 = AV_STEREO3D_2D }, INT_MIN, INT_MAX, .flags = V, .unit = 
"format" },
+    { "sbs", "Views are packed next to each other", 0, AV_OPT_TYPE_CONST,
+        { .i64 = AV_STEREO3D_SIDEBYSIDE }, INT_MIN, INT_MAX, .flags = V, .unit 
= "format" },
+    { "tab", "Views are packed on top of each other", 0, AV_OPT_TYPE_CONST,
+        { .i64 = AV_STEREO3D_TOPBOTTOM }, INT_MIN, INT_MAX, .flags = V, .unit 
= "format" },
+    { "frameseq", "Views are one after the other", 0, AV_OPT_TYPE_CONST,
+        { .i64 = AV_STEREO3D_FRAMESEQUENCE }, INT_MIN, INT_MAX, .flags = V, 
.unit = "format" },
+    { "sbsqnx", "Views are packed next to each other, different upsampling 
needed", 0, AV_OPT_TYPE_CONST,
+        { .i64 = AV_STEREO3D_SIDEBYSIDE_QUINCUNX }, INT_MIN, INT_MAX, .flags = 
V, .unit = "format" },
+    { "lines", "Views are interleaved by lines", 0, AV_OPT_TYPE_CONST,
+        { .i64 = AV_STEREO3D_LINES }, INT_MIN, INT_MAX, .flags = V, .unit = 
"format" },
+    { "columns", "Views are interleaved by columns", 0, AV_OPT_TYPE_CONST,
+        { .i64 = AV_STEREO3D_COLUMNS }, INT_MIN, INT_MAX, .flags = V, .unit = 
"format" },
+    { "check", "Vies are interleaved by pixels as a checkerboard", 0, 
AV_OPT_TYPE_CONST,
+        { .i64 = AV_STEREO3D_CHECKERBOARD }, INT_MIN, INT_MAX, .flags = V, 
.unit = "format" },
+    { NULL },
+};
+
+static const AVClass framepack_class = {
+    .class_name = "framepack",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+static const AVFilterPad framepack_inputs[] = {
+    {
+        .name         = "left",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .config_props = config_input_left,
+        .filter_frame = filter_frame_left,
+        .needs_fifo   = 1,
+    },
+    {
+        .name         = "right",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .config_props = config_input_right,
+        .filter_frame = filter_frame_right,
+        .needs_fifo   = 1,
+    },
+    { NULL }
+};
+
+static const AVFilterPad framepack_outputs[] = {
+    {
+        .name          = "packed",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .config_props  = config_output,
+        .request_frame = request_frame,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_framepack = {
+    .name          = "framepack",
+    .description   = NULL_IF_CONFIG_SMALL("Generate a frame packed 
stereoscopic video."),
+    .priv_size     = sizeof(FramepackContext),
+    .priv_class    = &framepack_class,
+    .query_formats = query_formats,
+    .inputs        = framepack_inputs,
+    .outputs       = framepack_outputs,
+    .uninit        = framepack_uninit,
+};
-- 
1.7.9.5

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to