avformat/network: add union for avoiding strict aliassing violations with sockaddr*
[ffmpeg.git] / libavfilter / vf_stereo3d.c
index aaa74a3..2140120 100644 (file)
@@ -25,6 +25,7 @@
 #include "libavutil/parseutils.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
+#include "drawutils.h"
 #include "formats.h"
 #include "internal.h"
 #include "video.h"
@@ -56,6 +57,8 @@ enum StereoCode {
     ABOVE_BELOW_RL,     // above-below (right eye above, left eye below)
     ABOVE_BELOW_2_LR,   // above-below with half height resolution
     ABOVE_BELOW_2_RL,   // above-below with half height resolution
+    ALTERNATING_LR,     // alternating frames (left eye first, right eye second)
+    ALTERNATING_RL,     // alternating frames (right eye first, left eye second)
     STEREO_CODE_COUNT   // TODO: needs autodetection
 };
 
@@ -63,6 +66,7 @@ typedef struct StereoComponent {
     enum StereoCode format;
     int width, height;
     int off_left, off_right;
+    int off_lstep, off_rstep;
     int row_left, row_right;
 } StereoComponent;
 
@@ -130,18 +134,27 @@ typedef struct Stereo3DContext {
     StereoComponent in, out;
     int width, height;
     int row_step;
-    int ana_matrix[3][6];
+    const int *ana_matrix[3];
+    int nb_planes;
+    int linesize[4];
+    int pheight[4];
+    int hsub, vsub;
+    int pixstep[4];
+    AVFrame *prev;
+    double ts_unit;
 } Stereo3DContext;
 
 #define OFFSET(x) offsetof(Stereo3DContext, x)
 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
 static const AVOption stereo3d_options[] = {
-    { "in",    "set input format",  OFFSET(in.format),  AV_OPT_TYPE_INT, {.i64=SIDE_BY_SIDE_LR}, SIDE_BY_SIDE_LR, ABOVE_BELOW_2_RL, FLAGS, "in"},
+    { "in",    "set input format",  OFFSET(in.format),  AV_OPT_TYPE_INT, {.i64=SIDE_BY_SIDE_LR}, SIDE_BY_SIDE_LR, STEREO_CODE_COUNT-1, FLAGS, "in"},
     { "ab2l",  "above below half height left first",  0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR},  0, 0, FLAGS, "in" },
     { "ab2r",  "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL},  0, 0, FLAGS, "in" },
     { "abl",   "above below left first",              0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR},    0, 0, FLAGS, "in" },
     { "abr",   "above below right first",             0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL},    0, 0, FLAGS, "in" },
+    { "al",    "alternating frames left first",       0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_LR},    0, 0, FLAGS, "in" },
+    { "ar",    "alternating frames right first",      0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_RL},    0, 0, FLAGS, "in" },
     { "sbs2l", "side by side half width left first",  0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR}, 0, 0, FLAGS, "in" },
     { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL}, 0, 0, FLAGS, "in" },
     { "sbsl",  "side by side left first",             0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR},   0, 0, FLAGS, "in" },
@@ -155,6 +168,8 @@ static const AVOption stereo3d_options[] = {
     { "agmd",  "anaglyph green magenta dubois",       0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_DUBOIS}, 0, 0, FLAGS, "out" },
     { "agmg",  "anaglyph green magenta gray",         0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_GRAY},   0, 0, FLAGS, "out" },
     { "agmh",  "anaglyph green magenta half color",   0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_HALF},   0, 0, FLAGS, "out" },
+    { "al",    "alternating frames left first",       0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_LR},     0, 0, FLAGS, "out" },
+    { "ar",    "alternating frames right first",      0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_RL},     0, 0, FLAGS, "out" },
     { "arbg",  "anaglyph red blue gray",              0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RB_GRAY},   0, 0, FLAGS, "out" },
     { "arcc",  "anaglyph red cyan color",             0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_COLOR},  0, 0, FLAGS, "out" },
     { "arcd",  "anaglyph red cyan dubois",            0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_DUBOIS}, 0, 0, FLAGS, "out" },
@@ -173,16 +188,95 @@ static const AVOption stereo3d_options[] = {
     { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL},  0, 0, FLAGS, "out" },
     { "sbsl",  "side by side left first",             0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR},    0, 0, FLAGS, "out" },
     { "sbsr",  "side by side right first",            0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL},    0, 0, FLAGS, "out" },
-    {NULL},
+    { NULL }
 };
 
 AVFILTER_DEFINE_CLASS(stereo3d);
 
+static const enum AVPixelFormat anaglyph_pix_fmts[] = {
+    AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
+    AV_PIX_FMT_NONE
+};
+
+static const enum AVPixelFormat other_pix_fmts[] = {
+    AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
+    AV_PIX_FMT_RGB48BE, AV_PIX_FMT_BGR48BE,
+    AV_PIX_FMT_RGB48LE, AV_PIX_FMT_BGR48LE,
+    AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_BGRA64BE,
+    AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_BGRA64LE,
+    AV_PIX_FMT_RGBA,  AV_PIX_FMT_BGRA,
+    AV_PIX_FMT_ARGB,  AV_PIX_FMT_ABGR,
+    AV_PIX_FMT_RGB0,  AV_PIX_FMT_BGR0,
+    AV_PIX_FMT_0RGB,  AV_PIX_FMT_0BGR,
+    AV_PIX_FMT_GBRP,
+    AV_PIX_FMT_GBRP9BE,  AV_PIX_FMT_GBRP9LE,
+    AV_PIX_FMT_GBRP10BE, AV_PIX_FMT_GBRP10LE,
+    AV_PIX_FMT_GBRP12BE, AV_PIX_FMT_GBRP12LE,
+    AV_PIX_FMT_GBRP14BE, AV_PIX_FMT_GBRP14LE,
+    AV_PIX_FMT_GBRP16BE, AV_PIX_FMT_GBRP16LE,
+    AV_PIX_FMT_YUV410P,
+    AV_PIX_FMT_YUV411P,
+    AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
+    AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P,
+    AV_PIX_FMT_YUV440P,
+    AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
+    AV_PIX_FMT_YUVJ411P,
+    AV_PIX_FMT_YUVJ420P,
+    AV_PIX_FMT_YUVJ422P,
+    AV_PIX_FMT_YUVJ440P,
+    AV_PIX_FMT_YUVJ444P,
+    AV_PIX_FMT_YUV420P9LE,  AV_PIX_FMT_YUVA420P9LE,
+    AV_PIX_FMT_YUV420P9BE,  AV_PIX_FMT_YUVA420P9BE,
+    AV_PIX_FMT_YUV422P9LE,  AV_PIX_FMT_YUVA422P9LE,
+    AV_PIX_FMT_YUV422P9BE,  AV_PIX_FMT_YUVA422P9BE,
+    AV_PIX_FMT_YUV444P9LE,  AV_PIX_FMT_YUVA444P9LE,
+    AV_PIX_FMT_YUV444P9BE,  AV_PIX_FMT_YUVA444P9BE,
+    AV_PIX_FMT_YUV420P10LE, AV_PIX_FMT_YUVA420P10LE,
+    AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUVA420P10BE,
+    AV_PIX_FMT_YUV422P10LE, AV_PIX_FMT_YUVA422P10LE,
+    AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUVA422P10BE,
+    AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUVA444P10LE,
+    AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUVA444P10BE,
+    AV_PIX_FMT_YUV420P12BE,  AV_PIX_FMT_YUV420P12LE,
+    AV_PIX_FMT_YUV422P12BE,  AV_PIX_FMT_YUV422P12LE,
+    AV_PIX_FMT_YUV444P12BE,  AV_PIX_FMT_YUV444P12LE,
+    AV_PIX_FMT_YUV420P14BE,  AV_PIX_FMT_YUV420P14LE,
+    AV_PIX_FMT_YUV422P14BE,  AV_PIX_FMT_YUV422P14LE,
+    AV_PIX_FMT_YUV444P14BE,  AV_PIX_FMT_YUV444P14LE,
+    AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUVA420P16LE,
+    AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUVA420P16BE,
+    AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUVA422P16LE,
+    AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUVA422P16BE,
+    AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUVA444P16LE,
+    AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUVA444P16BE,
+    AV_PIX_FMT_NONE
+};
+
 static int query_formats(AVFilterContext *ctx)
 {
-    static const enum AVPixelFormat pix_fmts[] = {
-        AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
-    };
+    Stereo3DContext *s = ctx->priv;
+    const enum AVPixelFormat *pix_fmts;
+
+    switch (s->out.format) {
+    case ANAGLYPH_GM_COLOR:
+    case ANAGLYPH_GM_DUBOIS:
+    case ANAGLYPH_GM_GRAY:
+    case ANAGLYPH_GM_HALF:
+    case ANAGLYPH_RB_GRAY:
+    case ANAGLYPH_RC_COLOR:
+    case ANAGLYPH_RC_DUBOIS:
+    case ANAGLYPH_RC_GRAY:
+    case ANAGLYPH_RC_HALF:
+    case ANAGLYPH_RG_GRAY:
+    case ANAGLYPH_YB_COLOR:
+    case ANAGLYPH_YB_DUBOIS:
+    case ANAGLYPH_YB_GRAY:
+    case ANAGLYPH_YB_HALF:
+        pix_fmts = anaglyph_pix_fmts;
+        break;
+    default:
+        pix_fmts = other_pix_fmts;
+    }
 
     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
 
@@ -195,12 +289,46 @@ static int config_output(AVFilterLink *outlink)
     AVFilterLink *inlink = ctx->inputs[0];
     Stereo3DContext *s = ctx->priv;
     AVRational aspect = inlink->sample_aspect_ratio;
+    AVRational fps = inlink->frame_rate;
+    AVRational tb = inlink->time_base;
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
+    int ret;
+
+    switch (s->in.format) {
+    case SIDE_BY_SIDE_2_LR:
+    case SIDE_BY_SIDE_LR:
+    case SIDE_BY_SIDE_2_RL:
+    case SIDE_BY_SIDE_RL:
+        if (inlink->w & 1) {
+            av_log(ctx, AV_LOG_ERROR, "width must be even\n");
+            return AVERROR_INVALIDDATA;
+        }
+        break;
+    case ABOVE_BELOW_2_LR:
+    case ABOVE_BELOW_LR:
+    case ABOVE_BELOW_2_RL:
+    case ABOVE_BELOW_RL:
+        if (s->out.format == INTERLEAVE_ROWS_LR ||
+            s->out.format == INTERLEAVE_ROWS_RL) {
+            if (inlink->h & 3) {
+                av_log(ctx, AV_LOG_ERROR, "height must be multiple of 4\n");
+                return AVERROR_INVALIDDATA;
+            }
+        }
+        if (inlink->h & 1) {
+            av_log(ctx, AV_LOG_ERROR, "height must be even\n");
+            return AVERROR_INVALIDDATA;
+        }
+        break;
+    }
 
     s->in.width     =
     s->width        = inlink->w;
     s->in.height    =
     s->height       = inlink->h;
     s->row_step     = 1;
+    s->in.off_lstep =
+    s->in.off_rstep =
     s->in.off_left  =
     s->in.off_right =
     s->in.row_left  =
@@ -211,13 +339,13 @@ static int config_output(AVFilterLink *outlink)
         aspect.num     *= 2;
     case SIDE_BY_SIDE_LR:
         s->width        = inlink->w / 2;
-        s->in.off_right = s->width * 3;
+        s->in.off_right = s->width;
         break;
     case SIDE_BY_SIDE_2_RL:
         aspect.num     *= 2;
     case SIDE_BY_SIDE_RL:
         s->width        = inlink->w / 2;
-        s->in.off_left  = s->width * 3;
+        s->in.off_left  = s->width;
         break;
     case ABOVE_BELOW_2_LR:
         aspect.den     *= 2;
@@ -231,6 +359,12 @@ static int config_output(AVFilterLink *outlink)
         s->in.row_left  =
         s->height       = inlink->h / 2;
         break;
+    case ALTERNATING_RL:
+    case ALTERNATING_LR:
+        outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
+        fps.den        *= 2;
+        tb.num         *= 2;
+        break;
     default:
         av_log(ctx, AV_LOG_ERROR, "input format %d is not supported\n", s->in.format);
         return AVERROR(EINVAL);
@@ -238,6 +372,8 @@ static int config_output(AVFilterLink *outlink)
 
     s->out.width     = s->width;
     s->out.height    = s->height;
+    s->out.off_lstep =
+    s->out.off_rstep =
     s->out.off_left  =
     s->out.off_right =
     s->out.row_left  =
@@ -257,29 +393,35 @@ static int config_output(AVFilterLink *outlink)
     case ANAGLYPH_YB_GRAY:
     case ANAGLYPH_YB_HALF:
     case ANAGLYPH_YB_COLOR:
-    case ANAGLYPH_YB_DUBOIS:
-        memcpy(s->ana_matrix, ana_coeff[s->out.format], sizeof(s->ana_matrix));
+    case ANAGLYPH_YB_DUBOIS: {
+        uint8_t rgba_map[4];
+
+        ff_fill_rgba_map(rgba_map, outlink->format);
+        s->ana_matrix[rgba_map[0]] = &ana_coeff[s->out.format][0][0];
+        s->ana_matrix[rgba_map[1]] = &ana_coeff[s->out.format][1][0];
+        s->ana_matrix[rgba_map[2]] = &ana_coeff[s->out.format][2][0];
         break;
+    }
     case SIDE_BY_SIDE_2_LR:
-        aspect.num      /= 2;
+        aspect.den      *= 2;
     case SIDE_BY_SIDE_LR:
-        s->out.width     =
-        s->out.off_right = s->width * 3;
+        s->out.width     = s->width * 2;
+        s->out.off_right = s->width;
         break;
     case SIDE_BY_SIDE_2_RL:
-        aspect.num      /= 2;
+        aspect.den      *= 2;
     case SIDE_BY_SIDE_RL:
         s->out.width     = s->width * 2;
-        s->out.off_left  = s->width * 3;
+        s->out.off_left  = s->width;
         break;
     case ABOVE_BELOW_2_LR:
-        aspect.den      /= 2;
+        aspect.num      *= 2;
     case ABOVE_BELOW_LR:
         s->out.height    = s->height * 2;
         s->out.row_right = s->height;
         break;
     case ABOVE_BELOW_2_RL:
-        aspect.den      /= 2;
+        aspect.num      *= 2;
     case ABOVE_BELOW_RL:
         s->out.height    = s->height * 2;
         s->out.row_left  = s->height;
@@ -287,33 +429,50 @@ static int config_output(AVFilterLink *outlink)
     case INTERLEAVE_ROWS_LR:
         s->row_step      = 2;
         s->height        = s->height / 2;
-        s->out.off_right = s->width * 3;
-        s->in.off_right += s->in.width * 3;
+        s->out.off_rstep =
+        s->in.off_rstep  = 1;
         break;
     case INTERLEAVE_ROWS_RL:
         s->row_step      = 2;
         s->height        = s->height / 2;
-        s->out.off_left  = s->width * 3;
-        s->in.off_left  += s->in.width * 3;
+        s->out.off_lstep =
+        s->in.off_lstep  = 1;
         break;
     case MONO_R:
         s->in.off_left   = s->in.off_right;
         s->in.row_left   = s->in.row_right;
     case MONO_L:
         break;
+    case ALTERNATING_RL:
+    case ALTERNATING_LR:
+        fps.num         *= 2;
+        tb.den          *= 2;
+        break;
     default:
-        av_log(ctx, AV_LOG_ERROR, "output format is not supported\n");
+        av_log(ctx, AV_LOG_ERROR, "output format %d is not supported\n", s->out.format);
         return AVERROR(EINVAL);
     }
 
     outlink->w = s->out.width;
     outlink->h = s->out.height;
+    outlink->frame_rate = fps;
+    outlink->time_base = tb;
     outlink->sample_aspect_ratio = aspect;
 
+    if ((ret = av_image_fill_linesizes(s->linesize, outlink->format, s->width)) < 0)
+        return ret;
+    s->nb_planes = av_pix_fmt_count_planes(outlink->format);
+    av_image_fill_max_pixsteps(s->pixstep, NULL, desc);
+    s->ts_unit = av_q2d(av_inv_q(av_mul_q(outlink->frame_rate, outlink->time_base)));
+    s->pheight[1] = s->pheight[2] = FF_CEIL_RSHIFT(s->height, desc->log2_chroma_h);
+    s->pheight[0] = s->pheight[3] = s->height;
+    s->hsub = desc->log2_chroma_w;
+    s->vsub = desc->log2_chroma_h;
+
     return 0;
 }
 
-static inline uint8_t ana_convert(const int *coeff, uint8_t *left, uint8_t *right)
+static inline uint8_t ana_convert(const int *coeff, const uint8_t *left, const uint8_t *right)
 {
     int sum;
 
@@ -329,23 +488,59 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
     AVFilterContext *ctx  = inlink->dst;
     Stereo3DContext *s = ctx->priv;
     AVFilterLink *outlink = ctx->outputs[0];
-    AVFrame *out;
-    int out_off_left, out_off_right;
-    int in_off_left, in_off_right;
+    AVFrame *out, *oleft, *oright, *ileft, *iright;
+    int out_off_left[4], out_off_right[4];
+    int in_off_left[4], in_off_right[4];
+    int i;
+
+    switch (s->in.format) {
+    case ALTERNATING_LR:
+    case ALTERNATING_RL:
+        if (!s->prev) {
+            s->prev = inpicref;
+            return 0;
+        }
+        ileft  = s->prev;
+        iright = inpicref;
+        if (s->in.format == ALTERNATING_RL)
+            FFSWAP(AVFrame *, ileft, iright);
+        break;
+    default:
+        ileft = iright = inpicref;
+    };
 
-    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+    out = oleft = oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!out) {
+        av_frame_free(&s->prev);
         av_frame_free(&inpicref);
         return AVERROR(ENOMEM);
     }
     av_frame_copy_props(out, inpicref);
 
-    in_off_left   = s->in.row_left  * inpicref->linesize[0] + s->in.off_left;
-    in_off_right  = s->in.row_right * inpicref->linesize[0] + s->in.off_right;
-    out_off_left  = s->out.row_left  * out->linesize[0] + s->out.off_left;
-    out_off_right = s->out.row_right * out->linesize[0] + s->out.off_right;
+    if (s->out.format == ALTERNATING_LR ||
+        s->out.format == ALTERNATING_RL) {
+        oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+        if (!oright) {
+            av_frame_free(&oleft);
+            av_frame_free(&s->prev);
+            av_frame_free(&inpicref);
+            return AVERROR(ENOMEM);
+        }
+        av_frame_copy_props(oright, inpicref);
+    }
+
+    for (i = 0; i < 4; i++) {
+        int hsub = i == 1 || i == 2 ? s->hsub : 0;
+        int vsub = i == 1 || i == 2 ? s->vsub : 0;
+        in_off_left[i]   = (FF_CEIL_RSHIFT(s->in.row_left,   vsub) + s->in.off_lstep)  * ileft->linesize[i]  + FF_CEIL_RSHIFT(s->in.off_left   * s->pixstep[i], hsub);
+        in_off_right[i]  = (FF_CEIL_RSHIFT(s->in.row_right,  vsub) + s->in.off_rstep)  * iright->linesize[i] + FF_CEIL_RSHIFT(s->in.off_right  * s->pixstep[i], hsub);
+        out_off_left[i]  = (FF_CEIL_RSHIFT(s->out.row_left,  vsub) + s->out.off_lstep) * oleft->linesize[i]  + FF_CEIL_RSHIFT(s->out.off_left  * s->pixstep[i], hsub);
+        out_off_right[i] = (FF_CEIL_RSHIFT(s->out.row_right, vsub) + s->out.off_rstep) * oright->linesize[i] + FF_CEIL_RSHIFT(s->out.off_right * s->pixstep[i], hsub);
+    }
 
     switch (s->out.format) {
+    case ALTERNATING_LR:
+    case ALTERNATING_RL:
     case SIDE_BY_SIDE_LR:
     case SIDE_BY_SIDE_RL:
     case SIDE_BY_SIDE_2_LR:
@@ -356,23 +551,28 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
     case ABOVE_BELOW_2_RL:
     case INTERLEAVE_ROWS_LR:
     case INTERLEAVE_ROWS_RL:
-        av_image_copy_plane(out->data[0] + out_off_left,
-                            out->linesize[0] * s->row_step,
-                            inpicref->data[0] + in_off_left,
-                            inpicref->linesize[0] * s->row_step,
-                            3 * s->width, s->height);
-        av_image_copy_plane(out->data[0] + out_off_right,
-                            out->linesize[0] * s->row_step,
-                            inpicref->data[0] + in_off_right,
-                            inpicref->linesize[0] * s->row_step,
-                            3 * s->width, s->height);
+        for (i = 0; i < s->nb_planes; i++) {
+            av_image_copy_plane(oleft->data[i] + out_off_left[i],
+                                oleft->linesize[i] * s->row_step,
+                                ileft->data[i] + in_off_left[i],
+                                ileft->linesize[i] * s->row_step,
+                                s->linesize[i], s->pheight[i]);
+            av_image_copy_plane(oright->data[i] + out_off_right[i],
+                                oright->linesize[i] * s->row_step,
+                                iright->data[i] + in_off_right[i],
+                                iright->linesize[i] * s->row_step,
+                                s->linesize[i], s->pheight[i]);
+        }
         break;
     case MONO_L:
+        iright = ileft;
     case MONO_R:
-        av_image_copy_plane(out->data[0], out->linesize[0],
-                            inpicref->data[0] + in_off_left,
-                            inpicref->linesize[0],
-                            3 * s->width, s->height);
+        for (i = 0; i < s->nb_planes; i++) {
+            av_image_copy_plane(out->data[i], out->linesize[i],
+                                iright->data[i] + in_off_left[i],
+                                iright->linesize[i],
+                                s->linesize[i], s->pheight[i]);
+        }
         break;
     case ANAGLYPH_RB_GRAY:
     case ANAGLYPH_RG_GRAY:
@@ -388,23 +588,21 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
     case ANAGLYPH_YB_HALF:
     case ANAGLYPH_YB_COLOR:
     case ANAGLYPH_YB_DUBOIS: {
-        int i, x, y, il, ir, o;
-        uint8_t *src = inpicref->data[0];
+        int x, y, il, ir, o;
+        const uint8_t *lsrc = ileft->data[0];
+        const uint8_t *rsrc = iright->data[0];
         uint8_t *dst = out->data[0];
         int out_width = s->out.width;
-        int *ana_matrix[3];
-
-        for (i = 0; i < 3; i++)
-            ana_matrix[i] = s->ana_matrix[i];
+        const int **ana_matrix = s->ana_matrix;
 
         for (y = 0; y < s->out.height; y++) {
             o   = out->linesize[0] * y;
-            il  = in_off_left  + y * inpicref->linesize[0];
-            ir  = in_off_right + y * inpicref->linesize[0];
+            il  = in_off_left[0]  + y * ileft->linesize[0];
+            ir  = in_off_right[0] + y * iright->linesize[0];
             for (x = 0; x < out_width; x++, il += 3, ir += 3, o+= 3) {
-                dst[o    ] = ana_convert(ana_matrix[0], src + il, src + ir);
-                dst[o + 1] = ana_convert(ana_matrix[1], src + il, src + ir);
-                dst[o + 2] = ana_convert(ana_matrix[2], src + il, src + ir);
+                dst[o    ] = ana_convert(ana_matrix[0], lsrc + il, rsrc + ir);
+                dst[o + 1] = ana_convert(ana_matrix[1], lsrc + il, rsrc + ir);
+                dst[o + 2] = ana_convert(ana_matrix[2], lsrc + il, rsrc + ir);
             }
         }
         break;
@@ -414,14 +612,33 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
     }
 
     av_frame_free(&inpicref);
+    av_frame_free(&s->prev);
+    if (oright != oleft) {
+        if (s->out.format == ALTERNATING_LR)
+            FFSWAP(AVFrame *, oleft, oright);
+        oright->pts = outlink->frame_count * s->ts_unit;
+        ff_filter_frame(outlink, oright);
+        out = oleft;
+        oleft->pts = outlink->frame_count * s->ts_unit;
+    } else if (s->in.format == ALTERNATING_LR ||
+               s->in.format == ALTERNATING_RL) {
+        out->pts = outlink->frame_count * s->ts_unit;
+    }
     return ff_filter_frame(outlink, out);
 }
 
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    Stereo3DContext *s = ctx->priv;
+
+    av_frame_free(&s->prev);
+}
+
 static const AVFilterPad stereo3d_inputs[] = {
     {
-        .name             = "default",
-        .type             = AVMEDIA_TYPE_VIDEO,
-        .filter_frame     = filter_frame,
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
     },
     { NULL }
 };
@@ -435,10 +652,11 @@ static const AVFilterPad stereo3d_outputs[] = {
     { NULL }
 };
 
-AVFilter avfilter_vf_stereo3d = {
+AVFilter ff_vf_stereo3d = {
     .name          = "stereo3d",
     .description   = NULL_IF_CONFIG_SMALL("Convert video stereoscopic 3D view."),
     .priv_size     = sizeof(Stereo3DContext),
+    .uninit        = uninit,
     .query_formats = query_formats,
     .inputs        = stereo3d_inputs,
     .outputs       = stereo3d_outputs,