]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vf_stereo3d.c
avfilter/avf_showspectrum: add yet another color map
[ffmpeg] / libavfilter / vf_stereo3d.c
index 8e9cc83af11eda17aa2ce0c12d6abe665d4cf9af..85a52586164f405f876154509267a50a798ecf58 100644 (file)
@@ -148,7 +148,6 @@ typedef struct Stereo3DContext {
     int hsub, vsub;
     int pixstep[4];
     AVFrame *prev;
-    double ts_unit;
     int blanks;
     int in_off_left[4], in_off_right[4];
     Stereo3DDSPContext dsp;
@@ -171,6 +170,8 @@ static const AVOption stereo3d_options[] = {
     { "sbsr",  "side by side right first",            0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL},    0, 0, FLAGS, "in" },
     { "irl",   "interleave rows left first",          0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_LR}, 0, 0, FLAGS, "in" },
     { "irr",   "interleave rows right first",         0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_RL}, 0, 0, FLAGS, "in" },
+    { "icl",   "interleave columns left first",       0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_COLS_LR}, 0, 0, FLAGS, "in" },
+    { "icr",   "interleave columns right first",      0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_COLS_RL}, 0, 0, FLAGS, "in" },
     { "out",   "set output format", OFFSET(out.format),  AV_OPT_TYPE_INT,   {.i64=ANAGLYPH_RC_DUBOIS}, 0, STEREO_CODE_COUNT-1, FLAGS, "out"},
     { "ab2l",  "above below half height left first",  0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR},   0, 0, FLAGS, "out" },
     { "ab2r",  "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL},   0, 0, FLAGS, "out" },
@@ -313,6 +314,26 @@ static inline uint8_t ana_convert(const int *coeff, const uint8_t *left, const u
     return av_clip_uint8(sum >> 16);
 }
 
+static void anaglyph_ic(uint8_t *dst, uint8_t *lsrc, uint8_t *rsrc,
+                        ptrdiff_t dst_linesize, ptrdiff_t l_linesize, ptrdiff_t r_linesize,
+                        int width, int height,
+                        const int *ana_matrix_r, const int *ana_matrix_g, const int *ana_matrix_b)
+{
+    int x, y, o;
+
+    for (y = 0; y < height; y++) {
+        for (o = 0, x = 0; x < width; x++, o+= 3) {
+            dst[o    ] = ana_convert(ana_matrix_r, lsrc + o * 2, rsrc + o * 2);
+            dst[o + 1] = ana_convert(ana_matrix_g, lsrc + o * 2, rsrc + o * 2);
+            dst[o + 2] = ana_convert(ana_matrix_b, lsrc + o * 2, rsrc + o * 2);
+        }
+
+        dst  += dst_linesize;
+        lsrc += l_linesize;
+        rsrc += r_linesize;
+    }
+}
+
 static void anaglyph(uint8_t *dst, uint8_t *lsrc, uint8_t *rsrc,
                      ptrdiff_t dst_linesize, ptrdiff_t l_linesize, ptrdiff_t r_linesize,
                      int width, int height,
@@ -345,6 +366,8 @@ static int config_output(AVFilterLink *outlink)
     int ret;
 
     switch (s->in.format) {
+    case INTERLEAVE_COLS_LR:
+    case INTERLEAVE_COLS_RL:
     case SIDE_BY_SIDE_2_LR:
     case SIDE_BY_SIDE_LR:
     case SIDE_BY_SIDE_2_RL:
@@ -409,6 +432,10 @@ static int config_output(AVFilterLink *outlink)
         fps.den        *= 2;
         tb.num         *= 2;
         break;
+    case INTERLEAVE_COLS_RL:
+    case INTERLEAVE_COLS_LR:
+        s->width        = inlink->w / 2;
+        break;
     case INTERLEAVE_ROWS_LR:
     case INTERLEAVE_ROWS_RL:
         s->in.row_step  = 2;
@@ -497,18 +524,18 @@ static int config_output(AVFilterLink *outlink)
         s->out.row_step  = 2;
         s->out.height    = s->height * 2;
         s->out.off_rstep = 1;
-        s->in.off_rstep  = s->in.format != INTERLEAVE_ROWS_RL;
         break;
     case INTERLEAVE_ROWS_RL:
         s->in.row_step   = 1 + (s->in.format == INTERLEAVE_ROWS_LR);
         s->out.row_step  = 2;
         s->out.height    = s->height * 2;
         s->out.off_lstep = 1;
-        s->in.off_lstep  = s->in.format != INTERLEAVE_ROWS_LR;
         break;
     case MONO_R:
-        s->in.off_left   = s->in.off_right;
-        s->in.row_left   = s->in.row_right;
+        if (s->in.format != INTERLEAVE_COLS_LR) {
+            s->in.off_left = s->in.off_right;
+            s->in.row_left = s->in.row_right;
+        }
         if (s->in.format == INTERLEAVE_ROWS_LR)
             FFSWAP(int, s->in.off_lstep, s->in.off_rstep);
         break;
@@ -534,6 +561,17 @@ static int config_output(AVFilterLink *outlink)
         return AVERROR(EINVAL);
     }
 
+    if (s->in.format == INTERLEAVE_COLS_LR || s->in.format == INTERLEAVE_COLS_RL) {
+        if ((s->in.format & 1) != (s->out.format & 1)) {
+            FFSWAP(int, s->in.row_left,   s->in.row_right);
+            FFSWAP(int, s->in.off_lstep,  s->in.off_rstep);
+            FFSWAP(int, s->in.off_left,   s->in.off_right);
+            FFSWAP(int, s->out.row_left,  s->out.row_right);
+            FFSWAP(int, s->out.off_lstep, s->out.off_rstep);
+            FFSWAP(int, s->out.off_left,  s->out.off_right);
+        }
+    }
+
     outlink->w = s->out.width;
     outlink->h = s->out.height;
     outlink->frame_rate = fps;
@@ -544,7 +582,6 @@ static int config_output(AVFilterLink *outlink)
         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;
@@ -586,6 +623,43 @@ static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
     return 0;
 }
 
+static void interleave_cols_to_any(Stereo3DContext *s, int *out_off, int p, AVFrame *in, AVFrame *out, int d)
+{
+    int y, x;
+
+    for (y = 0; y < s->pheight[p]; y++) {
+        const uint8_t *src = (const uint8_t*)in->data[p] + y * in->linesize[p] + d * s->pixstep[p];
+        uint8_t *dst = out->data[p] + out_off[p] + y * out->linesize[p] * s->out.row_step;
+
+        switch (s->pixstep[p]) {
+        case 1:
+            for (x = 0; x < s->linesize[p]; x++)
+                dst[x] = src[x * 2];
+            break;
+        case 2:
+            for (x = 0; x < s->linesize[p]; x+=2)
+                AV_WN16(&dst[x], AV_RN16(&src[x * 2]));
+            break;
+        case 3:
+            for (x = 0; x < s->linesize[p]; x+=3)
+                AV_WB24(&dst[x], AV_RB24(&src[x * 2]));
+            break;
+        case 4:
+            for (x = 0; x < s->linesize[p]; x+=4)
+                AV_WN32(&dst[x], AV_RN32(&src[x * 2]));
+            break;
+        case 6:
+            for (x = 0; x < s->linesize[p]; x+=6)
+                AV_WB48(&dst[x], AV_RB48(&src[x * 2]));
+            break;
+        case 8:
+            for (x = 0; x < s->linesize[p]; x+=8)
+                AV_WN64(&dst[x], AV_RN64(&src[x * 2]));
+            break;
+        }
+    }
+}
+
 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
 {
     AVFilterContext *ctx  = inlink->dst;
@@ -593,11 +667,21 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
     AVFilterLink *outlink = ctx->outputs[0];
     AVFrame *out, *oleft, *oright, *ileft, *iright;
     int out_off_left[4], out_off_right[4];
-    int i;
+    int i, ret;
 
     if (s->in.format == s->out.format)
         return ff_filter_frame(outlink, inpicref);
 
+    switch (s->out.format) {
+    case ALTERNATING_LR:
+    case ALTERNATING_RL:
+        if (!s->prev) {
+            s->prev = inpicref;
+            return 0;
+        }
+        break;
+    };
+
     switch (s->in.format) {
     case ALTERNATING_LR:
     case ALTERNATING_RL:
@@ -614,24 +698,84 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
         ileft = iright = inpicref;
     };
 
-    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);
-
-    if (s->out.format == ALTERNATING_LR ||
-        s->out.format == ALTERNATING_RL) {
-        oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
-        if (!oright) {
+    if ((s->out.format == ALTERNATING_LR ||
+         s->out.format == ALTERNATING_RL) &&
+        (s->in.format == SIDE_BY_SIDE_LR ||
+         s->in.format == SIDE_BY_SIDE_RL ||
+         s->in.format == SIDE_BY_SIDE_2_LR ||
+         s->in.format == SIDE_BY_SIDE_2_RL ||
+         s->in.format == ABOVE_BELOW_LR ||
+         s->in.format == ABOVE_BELOW_RL ||
+         s->in.format == ABOVE_BELOW_2_LR ||
+         s->in.format == ABOVE_BELOW_2_RL ||
+         s->in.format == INTERLEAVE_ROWS_LR ||
+         s->in.format == INTERLEAVE_ROWS_RL)) {
+        oright = av_frame_clone(s->prev);
+        oleft  = av_frame_clone(s->prev);
+        if (!oright || !oleft) {
+            av_frame_free(&oright);
             av_frame_free(&oleft);
             av_frame_free(&s->prev);
             av_frame_free(&inpicref);
             return AVERROR(ENOMEM);
         }
-        av_frame_copy_props(oright, inpicref);
+    } else if ((s->out.format == MONO_L ||
+                s->out.format == MONO_R) &&
+        (s->in.format == SIDE_BY_SIDE_LR ||
+         s->in.format == SIDE_BY_SIDE_RL ||
+         s->in.format == SIDE_BY_SIDE_2_LR ||
+         s->in.format == SIDE_BY_SIDE_2_RL ||
+         s->in.format == ABOVE_BELOW_LR ||
+         s->in.format == ABOVE_BELOW_RL ||
+         s->in.format == ABOVE_BELOW_2_LR ||
+         s->in.format == ABOVE_BELOW_2_RL ||
+         s->in.format == INTERLEAVE_ROWS_LR ||
+         s->in.format == INTERLEAVE_ROWS_RL)) {
+        out = oleft = oright = av_frame_clone(inpicref);
+        if (!out) {
+            av_frame_free(&s->prev);
+            av_frame_free(&inpicref);
+            return AVERROR(ENOMEM);
+        }
+    } else if ((s->out.format == MONO_L && s->in.format == ALTERNATING_LR) ||
+               (s->out.format == MONO_R && s->in.format == ALTERNATING_RL)) {
+        s->prev->pts /= 2;
+        ret = ff_filter_frame(outlink, s->prev);
+        av_frame_free(&inpicref);
+        s->prev = NULL;
+        return ret;
+    } else if ((s->out.format == MONO_L && s->in.format == ALTERNATING_RL) ||
+               (s->out.format == MONO_R && s->in.format == ALTERNATING_LR)) {
+        av_frame_free(&s->prev);
+        inpicref->pts /= 2;
+        return ff_filter_frame(outlink, inpicref);
+    } else if ((s->out.format == ALTERNATING_LR && s->in.format == ALTERNATING_RL) ||
+               (s->out.format == ALTERNATING_RL && s->in.format == ALTERNATING_LR)) {
+        FFSWAP(int64_t, s->prev->pts, inpicref->pts);
+        ff_filter_frame(outlink, inpicref);
+        ret = ff_filter_frame(outlink, s->prev);
+        s->prev = NULL;
+        return ret;
+    } else {
+        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);
+
+        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, s->prev);
+        }
     }
 
     for (i = 0; i < 4; i++) {
@@ -644,6 +788,38 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
     }
 
     switch (s->out.format) {
+    case ALTERNATING_LR:
+    case ALTERNATING_RL:
+        switch (s->in.format) {
+        case INTERLEAVE_ROWS_LR:
+        case INTERLEAVE_ROWS_RL:
+            for (i = 0; i < s->nb_planes; i++) {
+                oleft->linesize[i]  *= 2;
+                oright->linesize[i] *= 2;
+            }
+        case ABOVE_BELOW_LR:
+        case ABOVE_BELOW_RL:
+        case ABOVE_BELOW_2_LR:
+        case ABOVE_BELOW_2_RL:
+        case SIDE_BY_SIDE_LR:
+        case SIDE_BY_SIDE_RL:
+        case SIDE_BY_SIDE_2_LR:
+        case SIDE_BY_SIDE_2_RL:
+            oleft->width   = outlink->w;
+            oright->width  = outlink->w;
+            oleft->height  = outlink->h;
+            oright->height = outlink->h;
+
+            for (i = 0; i < s->nb_planes; i++) {
+                oleft->data[i]  += s->in_off_left[i];
+                oright->data[i] += s->in_off_right[i];
+            }
+            break;
+        default:
+            goto copy;
+            break;
+        }
+        break;
     case HDMI:
         for (i = 0; i < s->nb_planes; i++) {
             int j, h = s->height >> ((i == 1 || i == 2) ? s->vsub : 0);
@@ -652,8 +828,6 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
             for (j = h; j < h + b; j++)
                 memset(oleft->data[i] + j * s->linesize[i], 0, s->linesize[i]);
         }
-    case ALTERNATING_LR:
-    case ALTERNATING_RL:
     case SIDE_BY_SIDE_LR:
     case SIDE_BY_SIDE_RL:
     case SIDE_BY_SIDE_2_LR:
@@ -664,27 +838,70 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
     case ABOVE_BELOW_2_RL:
     case INTERLEAVE_ROWS_LR:
     case INTERLEAVE_ROWS_RL:
-        for (i = 0; i < s->nb_planes; i++) {
-            av_image_copy_plane(oleft->data[i] + out_off_left[i],
-                                oleft->linesize[i] * s->out.row_step,
-                                ileft->data[i] + s->in_off_left[i],
-                                ileft->linesize[i] * s->in.row_step,
-                                s->linesize[i], s->pheight[i]);
-            av_image_copy_plane(oright->data[i] + out_off_right[i],
-                                oright->linesize[i] * s->out.row_step,
-                                iright->data[i] + s->in_off_right[i],
-                                iright->linesize[i] * s->in.row_step,
-                                s->linesize[i], s->pheight[i]);
+copy:
+        if (s->in.format == INTERLEAVE_COLS_LR ||
+            s->in.format == INTERLEAVE_COLS_RL) {
+            for (i = 0; i < s->nb_planes; i++) {
+                int d = (s->in.format & 1) != (s->out.format & 1);
+
+                interleave_cols_to_any(s, out_off_left,  i, ileft,  oleft,   d);
+                interleave_cols_to_any(s, out_off_right, i, iright, oright, !d);
+            }
+        } else {
+            for (i = 0; i < s->nb_planes; i++) {
+                av_image_copy_plane(oleft->data[i] + out_off_left[i],
+                                    oleft->linesize[i] * s->out.row_step,
+                                    ileft->data[i] + s->in_off_left[i],
+                                    ileft->linesize[i] * s->in.row_step,
+                                    s->linesize[i], s->pheight[i]);
+                av_image_copy_plane(oright->data[i] + out_off_right[i],
+                                    oright->linesize[i] * s->out.row_step,
+                                    iright->data[i] + s->in_off_right[i],
+                                    iright->linesize[i] * s->in.row_step,
+                                    s->linesize[i], s->pheight[i]);
+            }
         }
         break;
     case MONO_L:
         iright = ileft;
     case MONO_R:
-        for (i = 0; i < s->nb_planes; i++) {
-            av_image_copy_plane(out->data[i], out->linesize[i],
-                                iright->data[i] + s->in_off_left[i],
-                                iright->linesize[i] * s->in.row_step,
-                                s->linesize[i], s->pheight[i]);
+        switch (s->in.format) {
+        case INTERLEAVE_ROWS_LR:
+        case INTERLEAVE_ROWS_RL:
+            for (i = 0; i < s->nb_planes; i++) {
+                out->linesize[i] *= 2;
+            }
+        case ABOVE_BELOW_LR:
+        case ABOVE_BELOW_RL:
+        case ABOVE_BELOW_2_LR:
+        case ABOVE_BELOW_2_RL:
+        case SIDE_BY_SIDE_LR:
+        case SIDE_BY_SIDE_RL:
+        case SIDE_BY_SIDE_2_LR:
+        case SIDE_BY_SIDE_2_RL:
+            out->width  = outlink->w;
+            out->height = outlink->h;
+
+            for (i = 0; i < s->nb_planes; i++) {
+                out->data[i] += s->in_off_left[i];
+            }
+            break;
+        case INTERLEAVE_COLS_LR:
+        case INTERLEAVE_COLS_RL:
+            for (i = 0; i < s->nb_planes; i++) {
+                const int d = (s->in.format & 1) != (s->out.format & 1);
+
+                interleave_cols_to_any(s, out_off_right, i, iright, out, d);
+            }
+            break;
+        default:
+            for (i = 0; i < s->nb_planes; i++) {
+                av_image_copy_plane(out->data[i], out->linesize[i],
+                                    iright->data[i] + s->in_off_left[i],
+                                    iright->linesize[i] * s->in.row_step,
+                                    s->linesize[i], s->pheight[i]);
+            }
+            break;
         }
         break;
     case ANAGLYPH_RB_GRAY:
@@ -701,11 +918,25 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
     case ANAGLYPH_YB_HALF:
     case ANAGLYPH_YB_COLOR:
     case ANAGLYPH_YB_DUBOIS: {
-        ThreadData td;
+        if (s->in.format == INTERLEAVE_COLS_LR ||
+            s->in.format == INTERLEAVE_COLS_RL) {
+            const int d = (s->in.format & 1);
 
-        td.ileft = ileft; td.iright = iright; td.out = out;
-        ctx->internal->execute(ctx, filter_slice, &td, NULL,
-                               FFMIN(s->out.height, ctx->graph->nb_threads));
+            anaglyph_ic(out->data[0],
+                ileft ->data[0] + s->in_off_left [0] +   d  * 3,
+                iright->data[0] + s->in_off_right[0] + (!d) * 3,
+                out->linesize[0],
+                ileft->linesize[0] * s->in.row_step,
+                iright->linesize[0] * s->in.row_step,
+                s->out.width, s->out.height,
+                s->ana_matrix[0], s->ana_matrix[1], s->ana_matrix[2]);
+        } else {
+            ThreadData td;
+
+            td.ileft = ileft; td.iright = iright; td.out = out;
+            ctx->internal->execute(ctx, filter_slice, &td, NULL,
+                                   FFMIN(s->out.height, ctx->graph->nb_threads));
+        }
         break;
     }
     case CHECKERBOARD_RL:
@@ -715,47 +946,50 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
 
             for (y = 0; y < s->pheight[i]; y++) {
                 uint8_t *dst = out->data[i] + out->linesize[i] * y;
-                uint8_t *left = ileft->data[i] + ileft->linesize[i] * y + s->in_off_left[i];
-                uint8_t *right = iright->data[i] + iright->linesize[i] * y + s->in_off_right[i];
+                const int d1 = (s->in.format == INTERLEAVE_COLS_LR || s->in.format == INTERLEAVE_COLS_RL) && (s->in.format & 1) != (s->out.format & 1);
+                const int d2 = (s->in.format == INTERLEAVE_COLS_LR || s->in.format == INTERLEAVE_COLS_RL) ? !d1 : 0;
+                const int m = 1 + (s->in.format == INTERLEAVE_COLS_LR || s->in.format == INTERLEAVE_COLS_RL);
+                uint8_t *left  = ileft->data[i]  + ileft->linesize[i]  * y + s->in_off_left[i]  + d1 * s->pixstep[i];
+                uint8_t *right = iright->data[i] + iright->linesize[i] * y + s->in_off_right[i] + d2 * s->pixstep[i];
                 int p, b;
 
-                if (s->out.format == CHECKERBOARD_RL)
+                if (s->out.format == CHECKERBOARD_RL && s->in.format != INTERLEAVE_COLS_LR && s->in.format != INTERLEAVE_COLS_RL)
                     FFSWAP(uint8_t*, left, right);
                 switch (s->pixstep[i]) {
                 case 1:
-                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=2, p++, b++) {
-                        dst[x  ] = (b&1) == (y&1) ? left[p] : right[p];
-                        dst[x+1] = (b&1) != (y&1) ? left[p] : right[p];
+                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=2, p++, b+=2) {
+                        dst[x  ] = (b&1) == (y&1) ? left[p*m] : right[p*m];
+                        dst[x+1] = (b&1) != (y&1) ? left[p*m] : right[p*m];
                     }
                     break;
                 case 2:
-                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=4, p+=2, b++) {
-                        AV_WN16(&dst[x  ], (b&1) == (y&1) ? AV_RN16(&left[p]) : AV_RN16(&right[p]));
-                        AV_WN16(&dst[x+2], (b&1) != (y&1) ? AV_RN16(&left[p]) : AV_RN16(&right[p]));
+                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=4, p+=2, b+=2) {
+                        AV_WN16(&dst[x  ], (b&1) == (y&1) ? AV_RN16(&left[p*m]) : AV_RN16(&right[p*m]));
+                        AV_WN16(&dst[x+2], (b&1) != (y&1) ? AV_RN16(&left[p*m]) : AV_RN16(&right[p*m]));
                     }
                     break;
                 case 3:
-                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=6, p+=3, b++) {
-                        AV_WB24(&dst[x  ], (b&1) == (y&1) ? AV_RB24(&left[p]) : AV_RB24(&right[p]));
-                        AV_WB24(&dst[x+3], (b&1) != (y&1) ? AV_RB24(&left[p]) : AV_RB24(&right[p]));
+                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=6, p+=3, b+=2) {
+                        AV_WB24(&dst[x  ], (b&1) == (y&1) ? AV_RB24(&left[p*m]) : AV_RB24(&right[p*m]));
+                        AV_WB24(&dst[x+3], (b&1) != (y&1) ? AV_RB24(&left[p*m]) : AV_RB24(&right[p*m]));
                     }
                     break;
                 case 4:
-                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=8, p+=4, b++) {
-                        AV_WN32(&dst[x  ], (b&1) == (y&1) ? AV_RN32(&left[p]) : AV_RN32(&right[p]));
-                        AV_WN32(&dst[x+4], (b&1) != (y&1) ? AV_RN32(&left[p]) : AV_RN32(&right[p]));
+                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=8, p+=4, b+=2) {
+                        AV_WN32(&dst[x  ], (b&1) == (y&1) ? AV_RN32(&left[p*m]) : AV_RN32(&right[p*m]));
+                        AV_WN32(&dst[x+4], (b&1) != (y&1) ? AV_RN32(&left[p*m]) : AV_RN32(&right[p*m]));
                     }
                     break;
                 case 6:
-                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=12, p+=6, b++) {
-                        AV_WB48(&dst[x  ], (b&1) == (y&1) ? AV_RB48(&left[p]) : AV_RB48(&right[p]));
-                        AV_WB48(&dst[x+6], (b&1) != (y&1) ? AV_RB48(&left[p]) : AV_RB48(&right[p]));
+                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=12, p+=6, b+=2) {
+                        AV_WB48(&dst[x  ], (b&1) == (y&1) ? AV_RB48(&left[p*m]) : AV_RB48(&right[p*m]));
+                        AV_WB48(&dst[x+6], (b&1) != (y&1) ? AV_RB48(&left[p*m]) : AV_RB48(&right[p*m]));
                     }
                     break;
                 case 8:
-                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=16, p+=8, b++) {
-                        AV_WN64(&dst[x  ], (b&1) == (y&1) ? AV_RN64(&left[p]) : AV_RN64(&right[p]));
-                        AV_WN64(&dst[x+8], (b&1) != (y&1) ? AV_RN64(&left[p]) : AV_RN64(&right[p]));
+                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=16, p+=8, b+=2) {
+                        AV_WN64(&dst[x  ], (b&1) == (y&1) ? AV_RN64(&left[p*m]) : AV_RN64(&right[p*m]));
+                        AV_WN64(&dst[x+8], (b&1) != (y&1) ? AV_RN64(&left[p*m]) : AV_RN64(&right[p*m]));
                     }
                     break;
                 }
@@ -765,11 +999,13 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
     case INTERLEAVE_COLS_LR:
     case INTERLEAVE_COLS_RL:
         for (i = 0; i < s->nb_planes; i++) {
+            const int d = (s->in.format == INTERLEAVE_COLS_LR || s->in.format == INTERLEAVE_COLS_RL);
+            const int m = 1 + d;
             int x, y;
 
             for (y = 0; y < s->pheight[i]; y++) {
                 uint8_t *dst = out->data[i] + out->linesize[i] * y;
-                uint8_t *left = ileft->data[i] + ileft->linesize[i] * y * s->in.row_step + s->in_off_left[i];
+                uint8_t *left = ileft->data[i] + ileft->linesize[i] * y * s->in.row_step + s->in_off_left[i] + d * s->pixstep[i];
                 uint8_t *right = iright->data[i] + iright->linesize[i] * y * s->in.row_step + s->in_off_right[i];
                 int p, b;
 
@@ -778,39 +1014,39 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
 
                 switch (s->pixstep[i]) {
                 case 1:
-                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=2, p++, b++) {
-                        dst[x  ] =   b&1  ? left[p] : right[p];
-                        dst[x+1] = !(b&1) ? left[p] : right[p];
+                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=2, p++, b+=2) {
+                        dst[x  ] =   b&1  ? left[p*m] : right[p*m];
+                        dst[x+1] = !(b&1) ? left[p*m] : right[p*m];
                     }
                     break;
                 case 2:
-                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=4, p+=2, b++) {
-                        AV_WN16(&dst[x  ],   b&1  ? AV_RN16(&left[p]) : AV_RN16(&right[p]));
-                        AV_WN16(&dst[x+2], !(b&1) ? AV_RN16(&left[p]) : AV_RN16(&right[p]));
+                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=4, p+=2, b+=2) {
+                        AV_WN16(&dst[x  ],   b&1  ? AV_RN16(&left[p*m]) : AV_RN16(&right[p*m]));
+                        AV_WN16(&dst[x+2], !(b&1) ? AV_RN16(&left[p*m]) : AV_RN16(&right[p*m]));
                     }
                     break;
                 case 3:
-                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=6, p+=3, b++) {
-                        AV_WB24(&dst[x  ],   b&1  ? AV_RB24(&left[p]) : AV_RB24(&right[p]));
-                        AV_WB24(&dst[x+3], !(b&1) ? AV_RB24(&left[p]) : AV_RB24(&right[p]));
+                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=6, p+=3, b+=2) {
+                        AV_WB24(&dst[x  ],   b&1  ? AV_RB24(&left[p*m]) : AV_RB24(&right[p*m]));
+                        AV_WB24(&dst[x+3], !(b&1) ? AV_RB24(&left[p*m]) : AV_RB24(&right[p*m]));
                     }
                     break;
                 case 4:
-                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=8, p+=4, b++) {
-                        AV_WN32(&dst[x  ],   b&1  ? AV_RN32(&left[p]) : AV_RN32(&right[p]));
-                        AV_WN32(&dst[x+4], !(b&1) ? AV_RN32(&left[p]) : AV_RN32(&right[p]));
+                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=8, p+=4, b+=2) {
+                        AV_WN32(&dst[x  ],   b&1  ? AV_RN32(&left[p*m]) : AV_RN32(&right[p*m]));
+                        AV_WN32(&dst[x+4], !(b&1) ? AV_RN32(&left[p*m]) : AV_RN32(&right[p*m]));
                     }
                     break;
                 case 6:
-                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=12, p+=6, b++) {
-                        AV_WB48(&dst[x  ],   b&1  ? AV_RB48(&left[p]) : AV_RB48(&right[p]));
-                        AV_WB48(&dst[x+6], !(b&1) ? AV_RB48(&left[p]) : AV_RB48(&right[p]));
+                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=12, p+=6, b+=2) {
+                        AV_WB48(&dst[x  ],   b&1  ? AV_RB48(&left[p*m]) : AV_RB48(&right[p*m]));
+                        AV_WB48(&dst[x+6], !(b&1) ? AV_RB48(&left[p*m]) : AV_RB48(&right[p*m]));
                     }
                     break;
                 case 8:
-                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=16, p+=8, b++) {
-                        AV_WN64(&dst[x  ],   b&1 ?  AV_RN64(&left[p]) : AV_RN64(&right[p]));
-                        AV_WN64(&dst[x+8], !(b&1) ? AV_RN64(&left[p]) : AV_RN64(&right[p]));
+                    for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=16, p+=8, b+=2) {
+                        AV_WN64(&dst[x  ],   b&1 ?  AV_RN64(&left[p*m]) : AV_RN64(&right[p*m]));
+                        AV_WN64(&dst[x+8], !(b&1) ? AV_RN64(&left[p*m]) : AV_RN64(&right[p*m]));
                     }
                     break;
                 }
@@ -821,18 +1057,23 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
         av_assert0(0);
     }
 
-    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;
+        oright->pts = s->prev->pts * 2;
         ff_filter_frame(outlink, oright);
         out = oleft;
-        oleft->pts = outlink->frame_count * s->ts_unit;
+        oleft->pts = s->prev->pts + inpicref->pts;
+        av_frame_free(&s->prev);
+        s->prev = inpicref;
     } else if (s->in.format == ALTERNATING_LR ||
                s->in.format == ALTERNATING_RL) {
-        out->pts = outlink->frame_count * s->ts_unit;
+        out->pts = s->prev->pts / 2;
+        av_frame_free(&s->prev);
+        av_frame_free(&inpicref);
+    } else {
+        av_frame_free(&s->prev);
+        av_frame_free(&inpicref);
     }
     return ff_filter_frame(outlink, out);
 }