avfilter/vf_stereo3d: add interleave columns input support
authorPaul B Mahol <onemda@gmail.com>
Fri, 18 Dec 2015 21:00:31 +0000 (22:00 +0100)
committerPaul B Mahol <onemda@gmail.com>
Sat, 19 Dec 2015 18:23:47 +0000 (19:23 +0100)
Signed-off-by: Paul B Mahol <onemda@gmail.com>
doc/filters.texi
libavfilter/vf_stereo3d.c

index b8047ab..ba81dc7 100644 (file)
@@ -10784,6 +10784,12 @@ interleaved rows (left eye has top row, right eye starts on next row)
 @item irr
 interleaved rows (right eye has top row, left eye starts on next row)
 
+@item icl
+interleaved columns, left eye first
+
+@item icr
+interleaved columns, right eye first
+
 Default value is @samp{sbsl}.
 @end table
 
index de884a8..85a5258 100644 (file)
@@ -170,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" },
@@ -312,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,
@@ -344,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:
@@ -408,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;
@@ -504,8 +532,10 @@ static int config_output(AVFilterLink *outlink)
         s->out.off_lstep = 1;
         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;
@@ -531,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;
@@ -582,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;
@@ -761,17 +839,27 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
     case INTERLEAVE_ROWS_LR:
     case INTERLEAVE_ROWS_RL:
 copy:
-        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]);
+        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:
@@ -798,6 +886,14 @@ copy:
                 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],
@@ -822,11 +918,25 @@ copy:
     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);
+
+            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));
+            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:
@@ -836,47 +946,50 @@ copy:
 
             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;
                 }
@@ -886,11 +999,13 @@ copy:
     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;
 
@@ -900,38 +1015,38 @@ copy:
                 switch (s->pixstep[i]) {
                 case 1:
                     for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=2, p++, b+=2) {
-                        dst[x  ] =   b&1  ? left[p] : right[p];
-                        dst[x+1] = !(b&1) ? left[p] : right[p];
+                        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+=2) {
-                        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]));
+                        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+=2) {
-                        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]));
+                        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+=2) {
-                        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]));
+                        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+=2) {
-                        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]));
+                        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+=2) {
-                        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]));
+                        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;
                 }