LCOV - code coverage report
Current view: top level - libavfilter - vf_stereo3d.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 264 557 47.4 %
Date: 2017-12-14 08:27:08 Functions: 7 9 77.8 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2010 Gordon Schmidt <gordon.schmidt <at> s2000.tu-chemnitz.de>
       3             :  * Copyright (c) 2013-2015 Paul B Mahol
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :  * GNU General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU General Public License along
      18             :  * with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include "libavutil/avassert.h"
      23             : #include "libavutil/imgutils.h"
      24             : #include "libavutil/intreadwrite.h"
      25             : #include "libavutil/opt.h"
      26             : #include "libavutil/parseutils.h"
      27             : #include "libavutil/pixdesc.h"
      28             : #include "avfilter.h"
      29             : #include "drawutils.h"
      30             : #include "formats.h"
      31             : #include "internal.h"
      32             : #include "video.h"
      33             : #include "stereo3d.h"
      34             : 
      35             : enum StereoCode {
      36             :     ANAGLYPH_RC_GRAY,   // anaglyph red/cyan gray
      37             :     ANAGLYPH_RC_HALF,   // anaglyph red/cyan half colored
      38             :     ANAGLYPH_RC_COLOR,  // anaglyph red/cyan colored
      39             :     ANAGLYPH_RC_DUBOIS, // anaglyph red/cyan dubois
      40             :     ANAGLYPH_GM_GRAY,   // anaglyph green/magenta gray
      41             :     ANAGLYPH_GM_HALF,   // anaglyph green/magenta half colored
      42             :     ANAGLYPH_GM_COLOR,  // anaglyph green/magenta colored
      43             :     ANAGLYPH_GM_DUBOIS, // anaglyph green/magenta dubois
      44             :     ANAGLYPH_YB_GRAY,   // anaglyph yellow/blue gray
      45             :     ANAGLYPH_YB_HALF,   // anaglyph yellow/blue half colored
      46             :     ANAGLYPH_YB_COLOR,  // anaglyph yellow/blue colored
      47             :     ANAGLYPH_YB_DUBOIS, // anaglyph yellow/blue dubois
      48             :     ANAGLYPH_RB_GRAY,   // anaglyph red/blue gray
      49             :     ANAGLYPH_RG_GRAY,   // anaglyph red/green gray
      50             :     MONO_L,             // mono output for debugging (left eye only)
      51             :     MONO_R,             // mono output for debugging (right eye only)
      52             :     INTERLEAVE_ROWS_LR, // row-interleave (left eye has top row)
      53             :     INTERLEAVE_ROWS_RL, // row-interleave (right eye has top row)
      54             :     SIDE_BY_SIDE_LR,    // side by side parallel (left eye left, right eye right)
      55             :     SIDE_BY_SIDE_RL,    // side by side crosseye (right eye left, left eye right)
      56             :     SIDE_BY_SIDE_2_LR,  // side by side parallel with half width resolution
      57             :     SIDE_BY_SIDE_2_RL,  // side by side crosseye with half width resolution
      58             :     ABOVE_BELOW_LR,     // above-below (left eye above, right eye below)
      59             :     ABOVE_BELOW_RL,     // above-below (right eye above, left eye below)
      60             :     ABOVE_BELOW_2_LR,   // above-below with half height resolution
      61             :     ABOVE_BELOW_2_RL,   // above-below with half height resolution
      62             :     ALTERNATING_LR,     // alternating frames (left eye first, right eye second)
      63             :     ALTERNATING_RL,     // alternating frames (right eye first, left eye second)
      64             :     CHECKERBOARD_LR,    // checkerboard pattern (left eye first, right eye second)
      65             :     CHECKERBOARD_RL,    // checkerboard pattern (right eye first, left eye second)
      66             :     INTERLEAVE_COLS_LR, // column-interleave (left eye first, right eye second)
      67             :     INTERLEAVE_COLS_RL, // column-interleave (right eye first, left eye second)
      68             :     HDMI,               // HDMI frame pack (left eye first, right eye second)
      69             :     STEREO_CODE_COUNT   // TODO: needs autodetection
      70             : };
      71             : 
      72             : typedef struct StereoComponent {
      73             :     int format;                 ///< StereoCode
      74             :     int width, height;
      75             :     int off_left, off_right;
      76             :     int off_lstep, off_rstep;
      77             :     int row_left, row_right;
      78             :     int row_step;
      79             : } StereoComponent;
      80             : 
      81             : static const int ana_coeff[][3][6] = {
      82             :   [ANAGLYPH_RB_GRAY]   =
      83             :     {{19595, 38470,  7471,     0,     0,     0},
      84             :      {    0,     0,     0,     0,     0,     0},
      85             :      {    0,     0,     0, 19595, 38470,  7471}},
      86             :   [ANAGLYPH_RG_GRAY]   =
      87             :     {{19595, 38470,  7471,     0,     0,     0},
      88             :      {    0,     0,     0, 19595, 38470,  7471},
      89             :      {    0,     0,     0,     0,     0,     0}},
      90             :   [ANAGLYPH_RC_GRAY]   =
      91             :     {{19595, 38470,  7471,     0,     0,     0},
      92             :      {    0,     0,     0, 19595, 38470,  7471},
      93             :      {    0,     0,     0, 19595, 38470,  7471}},
      94             :   [ANAGLYPH_RC_HALF]   =
      95             :     {{19595, 38470,  7471,     0,     0,     0},
      96             :      {    0,     0,     0,     0, 65536,     0},
      97             :      {    0,     0,     0,     0,     0, 65536}},
      98             :   [ANAGLYPH_RC_COLOR]  =
      99             :     {{65536,     0,     0,     0,     0,     0},
     100             :      {    0,     0,     0,     0, 65536,     0},
     101             :      {    0,     0,     0,     0,     0, 65536}},
     102             :   [ANAGLYPH_RC_DUBOIS] =
     103             :     {{29891, 32800, 11559, -2849, -5763,  -102},
     104             :      {-2627, -2479, -1033, 24804, 48080, -1209},
     105             :      { -997, -1350,  -358, -4729, -7403, 80373}},
     106             :   [ANAGLYPH_GM_GRAY]   =
     107             :     {{    0,     0,     0, 19595, 38470,  7471},
     108             :      {19595, 38470,  7471,     0,     0,     0},
     109             :      {    0,     0,     0, 19595, 38470,  7471}},
     110             :   [ANAGLYPH_GM_HALF]   =
     111             :     {{    0,     0,     0, 65536,     0,     0},
     112             :      {19595, 38470,  7471,     0,     0,     0},
     113             :      {    0,     0,     0,     0,     0, 65536}},
     114             :   [ANAGLYPH_GM_COLOR]  =
     115             :     {{    0,     0,     0, 65536,     0,     0},
     116             :      {    0, 65536,     0,     0,     0,     0},
     117             :      {    0,     0,     0,     0,     0, 65536}},
     118             :   [ANAGLYPH_GM_DUBOIS]  =
     119             :     {{-4063,-10354, -2556, 34669, 46203,  1573},
     120             :      {18612, 43778,  9372, -1049,  -983, -4260},
     121             :      { -983, -1769,  1376,   590,  4915, 61407}},
     122             :   [ANAGLYPH_YB_GRAY]   =
     123             :     {{    0,     0,     0, 19595, 38470,  7471},
     124             :      {    0,     0,     0, 19595, 38470,  7471},
     125             :      {19595, 38470,  7471,     0,     0,     0}},
     126             :   [ANAGLYPH_YB_HALF]   =
     127             :     {{    0,     0,     0, 65536,     0,     0},
     128             :      {    0,     0,     0,     0, 65536,     0},
     129             :      {19595, 38470,  7471,     0,     0,     0}},
     130             :   [ANAGLYPH_YB_COLOR]  =
     131             :     {{    0,     0,     0, 65536,     0,     0},
     132             :      {    0,     0,     0,     0, 65536,     0},
     133             :      {    0,     0, 65536,     0,     0,     0}},
     134             :   [ANAGLYPH_YB_DUBOIS] =
     135             :     {{65535,-12650,18451,   -987, -7590, -1049},
     136             :      {-1604, 56032, 4196,    370,  3826, -1049},
     137             :      {-2345,-10676, 1358,   5801, 11416, 56217}},
     138             : };
     139             : 
     140             : typedef struct Stereo3DContext {
     141             :     const AVClass *class;
     142             :     StereoComponent in, out;
     143             :     int width, height;
     144             :     const int *ana_matrix[3];
     145             :     int nb_planes;
     146             :     int linesize[4];
     147             :     int pheight[4];
     148             :     int hsub, vsub;
     149             :     int pixstep[4];
     150             :     AVFrame *prev;
     151             :     int blanks;
     152             :     int in_off_left[4], in_off_right[4];
     153             :     AVRational aspect;
     154             :     Stereo3DDSPContext dsp;
     155             : } Stereo3DContext;
     156             : 
     157             : #define OFFSET(x) offsetof(Stereo3DContext, x)
     158             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
     159             : 
     160             : static const AVOption stereo3d_options[] = {
     161             :     { "in",    "set input format",  OFFSET(in.format),   AV_OPT_TYPE_INT,   {.i64=SIDE_BY_SIDE_LR}, INTERLEAVE_ROWS_LR, STEREO_CODE_COUNT-1, FLAGS, "in"},
     162             :     { "ab2l",  "above below half height left first",  0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR},   0, 0, FLAGS, "in" },
     163             :     { "ab2r",  "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL},   0, 0, FLAGS, "in" },
     164             :     { "abl",   "above below left first",              0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR},     0, 0, FLAGS, "in" },
     165             :     { "abr",   "above below right first",             0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL},     0, 0, FLAGS, "in" },
     166             :     { "al",    "alternating frames left first",       0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_LR},     0, 0, FLAGS, "in" },
     167             :     { "ar",    "alternating frames right first",      0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_RL},     0, 0, FLAGS, "in" },
     168             :     { "sbs2l", "side by side half width left first",  0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR},  0, 0, FLAGS, "in" },
     169             :     { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL},  0, 0, FLAGS, "in" },
     170             :     { "sbsl",  "side by side left first",             0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR},    0, 0, FLAGS, "in" },
     171             :     { "sbsr",  "side by side right first",            0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL},    0, 0, FLAGS, "in" },
     172             :     { "irl",   "interleave rows left first",          0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_LR}, 0, 0, FLAGS, "in" },
     173             :     { "irr",   "interleave rows right first",         0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_RL}, 0, 0, FLAGS, "in" },
     174             :     { "icl",   "interleave columns left first",       0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_COLS_LR}, 0, 0, FLAGS, "in" },
     175             :     { "icr",   "interleave columns right first",      0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_COLS_RL}, 0, 0, FLAGS, "in" },
     176             :     { "out",   "set output format", OFFSET(out.format),  AV_OPT_TYPE_INT,   {.i64=ANAGLYPH_RC_DUBOIS}, 0, STEREO_CODE_COUNT-1, FLAGS, "out"},
     177             :     { "ab2l",  "above below half height left first",  0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR},   0, 0, FLAGS, "out" },
     178             :     { "ab2r",  "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL},   0, 0, FLAGS, "out" },
     179             :     { "abl",   "above below left first",              0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR},     0, 0, FLAGS, "out" },
     180             :     { "abr",   "above below right first",             0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL},     0, 0, FLAGS, "out" },
     181             :     { "agmc",  "anaglyph green magenta color",        0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_COLOR},  0, 0, FLAGS, "out" },
     182             :     { "agmd",  "anaglyph green magenta dubois",       0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_DUBOIS}, 0, 0, FLAGS, "out" },
     183             :     { "agmg",  "anaglyph green magenta gray",         0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_GRAY},   0, 0, FLAGS, "out" },
     184             :     { "agmh",  "anaglyph green magenta half color",   0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_HALF},   0, 0, FLAGS, "out" },
     185             :     { "al",    "alternating frames left first",       0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_LR},     0, 0, FLAGS, "out" },
     186             :     { "ar",    "alternating frames right first",      0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_RL},     0, 0, FLAGS, "out" },
     187             :     { "arbg",  "anaglyph red blue gray",              0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RB_GRAY},   0, 0, FLAGS, "out" },
     188             :     { "arcc",  "anaglyph red cyan color",             0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_COLOR},  0, 0, FLAGS, "out" },
     189             :     { "arcd",  "anaglyph red cyan dubois",            0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_DUBOIS}, 0, 0, FLAGS, "out" },
     190             :     { "arcg",  "anaglyph red cyan gray",              0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_GRAY},   0, 0, FLAGS, "out" },
     191             :     { "arch",  "anaglyph red cyan half color",        0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_HALF},   0, 0, FLAGS, "out" },
     192             :     { "argg",  "anaglyph red green gray",             0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RG_GRAY},   0, 0, FLAGS, "out" },
     193             :     { "aybc",  "anaglyph yellow blue color",          0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_COLOR},  0, 0, FLAGS, "out" },
     194             :     { "aybd",  "anaglyph yellow blue dubois",         0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_DUBOIS}, 0, 0, FLAGS, "out" },
     195             :     { "aybg",  "anaglyph yellow blue gray",           0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_GRAY},   0, 0, FLAGS, "out" },
     196             :     { "aybh",  "anaglyph yellow blue half color",     0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_HALF},   0, 0, FLAGS, "out" },
     197             :     { "irl",   "interleave rows left first",          0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_LR}, 0, 0, FLAGS, "out" },
     198             :     { "irr",   "interleave rows right first",         0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_RL}, 0, 0, FLAGS, "out" },
     199             :     { "ml",    "mono left",                           0, AV_OPT_TYPE_CONST, {.i64=MONO_L},             0, 0, FLAGS, "out" },
     200             :     { "mr",    "mono right",                          0, AV_OPT_TYPE_CONST, {.i64=MONO_R},             0, 0, FLAGS, "out" },
     201             :     { "sbs2l", "side by side half width left first",  0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR},  0, 0, FLAGS, "out" },
     202             :     { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL},  0, 0, FLAGS, "out" },
     203             :     { "sbsl",  "side by side left first",             0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR},    0, 0, FLAGS, "out" },
     204             :     { "sbsr",  "side by side right first",            0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL},    0, 0, FLAGS, "out" },
     205             :     { "chl",   "checkerboard left first",             0, AV_OPT_TYPE_CONST, {.i64=CHECKERBOARD_LR},    0, 0, FLAGS, "out" },
     206             :     { "chr",   "checkerboard right first",            0, AV_OPT_TYPE_CONST, {.i64=CHECKERBOARD_RL},    0, 0, FLAGS, "out" },
     207             :     { "icl",   "interleave columns left first",       0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_COLS_LR}, 0, 0, FLAGS, "out" },
     208             :     { "icr",   "interleave columns right first",      0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_COLS_RL}, 0, 0, FLAGS, "out" },
     209             :     { "hdmi",  "HDMI frame pack",                     0, AV_OPT_TYPE_CONST, {.i64=HDMI},               0, 0, FLAGS, "out" },
     210             :     { NULL }
     211             : };
     212             : 
     213             : AVFILTER_DEFINE_CLASS(stereo3d);
     214             : 
     215             : static const enum AVPixelFormat anaglyph_pix_fmts[] = {
     216             :     AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
     217             :     AV_PIX_FMT_NONE
     218             : };
     219             : 
     220             : static const enum AVPixelFormat other_pix_fmts[] = {
     221             :     AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
     222             :     AV_PIX_FMT_RGB48BE, AV_PIX_FMT_BGR48BE,
     223             :     AV_PIX_FMT_RGB48LE, AV_PIX_FMT_BGR48LE,
     224             :     AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_BGRA64BE,
     225             :     AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_BGRA64LE,
     226             :     AV_PIX_FMT_RGBA,  AV_PIX_FMT_BGRA,
     227             :     AV_PIX_FMT_ARGB,  AV_PIX_FMT_ABGR,
     228             :     AV_PIX_FMT_RGB0,  AV_PIX_FMT_BGR0,
     229             :     AV_PIX_FMT_0RGB,  AV_PIX_FMT_0BGR,
     230             :     AV_PIX_FMT_GBRP,
     231             :     AV_PIX_FMT_GBRP9BE,  AV_PIX_FMT_GBRP9LE,
     232             :     AV_PIX_FMT_GBRP10BE, AV_PIX_FMT_GBRP10LE,
     233             :     AV_PIX_FMT_GBRP12BE, AV_PIX_FMT_GBRP12LE,
     234             :     AV_PIX_FMT_GBRP14BE, AV_PIX_FMT_GBRP14LE,
     235             :     AV_PIX_FMT_GBRP16BE, AV_PIX_FMT_GBRP16LE,
     236             :     AV_PIX_FMT_YUV410P,
     237             :     AV_PIX_FMT_YUV411P,
     238             :     AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
     239             :     AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P,
     240             :     AV_PIX_FMT_YUV440P,
     241             :     AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
     242             :     AV_PIX_FMT_YUVJ411P,
     243             :     AV_PIX_FMT_YUVJ420P,
     244             :     AV_PIX_FMT_YUVJ422P,
     245             :     AV_PIX_FMT_YUVJ440P,
     246             :     AV_PIX_FMT_YUVJ444P,
     247             :     AV_PIX_FMT_YUV420P9LE,  AV_PIX_FMT_YUVA420P9LE,
     248             :     AV_PIX_FMT_YUV420P9BE,  AV_PIX_FMT_YUVA420P9BE,
     249             :     AV_PIX_FMT_YUV422P9LE,  AV_PIX_FMT_YUVA422P9LE,
     250             :     AV_PIX_FMT_YUV422P9BE,  AV_PIX_FMT_YUVA422P9BE,
     251             :     AV_PIX_FMT_YUV444P9LE,  AV_PIX_FMT_YUVA444P9LE,
     252             :     AV_PIX_FMT_YUV444P9BE,  AV_PIX_FMT_YUVA444P9BE,
     253             :     AV_PIX_FMT_YUV420P10LE, AV_PIX_FMT_YUVA420P10LE,
     254             :     AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUVA420P10BE,
     255             :     AV_PIX_FMT_YUV422P10LE, AV_PIX_FMT_YUVA422P10LE,
     256             :     AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUVA422P10BE,
     257             :     AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUVA444P10LE,
     258             :     AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUVA444P10BE,
     259             :     AV_PIX_FMT_YUV420P12BE,  AV_PIX_FMT_YUV420P12LE,
     260             :     AV_PIX_FMT_YUV422P12BE,  AV_PIX_FMT_YUV422P12LE,
     261             :     AV_PIX_FMT_YUV444P12BE,  AV_PIX_FMT_YUV444P12LE,
     262             :     AV_PIX_FMT_YUV420P14BE,  AV_PIX_FMT_YUV420P14LE,
     263             :     AV_PIX_FMT_YUV422P14BE,  AV_PIX_FMT_YUV422P14LE,
     264             :     AV_PIX_FMT_YUV444P14BE,  AV_PIX_FMT_YUV444P14LE,
     265             :     AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUVA420P16LE,
     266             :     AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUVA420P16BE,
     267             :     AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUVA422P16LE,
     268             :     AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUVA422P16BE,
     269             :     AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUVA444P16LE,
     270             :     AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUVA444P16BE,
     271             :     AV_PIX_FMT_NONE
     272             : };
     273             : 
     274          22 : static int query_formats(AVFilterContext *ctx)
     275             : {
     276          22 :     Stereo3DContext *s = ctx->priv;
     277             :     const enum AVPixelFormat *pix_fmts;
     278             :     AVFilterFormats *fmts_list;
     279             : 
     280          22 :     switch (s->out.format) {
     281          14 :     case ANAGLYPH_GM_COLOR:
     282             :     case ANAGLYPH_GM_DUBOIS:
     283             :     case ANAGLYPH_GM_GRAY:
     284             :     case ANAGLYPH_GM_HALF:
     285             :     case ANAGLYPH_RB_GRAY:
     286             :     case ANAGLYPH_RC_COLOR:
     287             :     case ANAGLYPH_RC_DUBOIS:
     288             :     case ANAGLYPH_RC_GRAY:
     289             :     case ANAGLYPH_RC_HALF:
     290             :     case ANAGLYPH_RG_GRAY:
     291             :     case ANAGLYPH_YB_COLOR:
     292             :     case ANAGLYPH_YB_DUBOIS:
     293             :     case ANAGLYPH_YB_GRAY:
     294             :     case ANAGLYPH_YB_HALF:
     295          14 :         pix_fmts = anaglyph_pix_fmts;
     296          14 :         break;
     297           8 :     default:
     298           8 :         pix_fmts = other_pix_fmts;
     299             :     }
     300             : 
     301          22 :     fmts_list = ff_make_format_list(pix_fmts);
     302          22 :     if (!fmts_list)
     303           0 :         return AVERROR(ENOMEM);
     304          22 :     return ff_set_common_formats(ctx, fmts_list);
     305             : }
     306             : 
     307    10644480 : static inline uint8_t ana_convert(const int *coeff, const uint8_t *left, const uint8_t *right)
     308             : {
     309             :     int sum;
     310             : 
     311    10644480 :     sum  = coeff[0] * left[0] + coeff[3] * right[0]; //red in
     312    10644480 :     sum += coeff[1] * left[1] + coeff[4] * right[1]; //green in
     313    10644480 :     sum += coeff[2] * left[2] + coeff[5] * right[2]; //blue in
     314             : 
     315    10644480 :     return av_clip_uint8(sum >> 16);
     316             : }
     317             : 
     318           0 : static void anaglyph_ic(uint8_t *dst, uint8_t *lsrc, uint8_t *rsrc,
     319             :                         ptrdiff_t dst_linesize, ptrdiff_t l_linesize, ptrdiff_t r_linesize,
     320             :                         int width, int height,
     321             :                         const int *ana_matrix_r, const int *ana_matrix_g, const int *ana_matrix_b)
     322             : {
     323             :     int x, y, o;
     324             : 
     325           0 :     for (y = 0; y < height; y++) {
     326           0 :         for (o = 0, x = 0; x < width; x++, o+= 3) {
     327           0 :             dst[o    ] = ana_convert(ana_matrix_r, lsrc + o * 2, rsrc + o * 2);
     328           0 :             dst[o + 1] = ana_convert(ana_matrix_g, lsrc + o * 2, rsrc + o * 2);
     329           0 :             dst[o + 2] = ana_convert(ana_matrix_b, lsrc + o * 2, rsrc + o * 2);
     330             :         }
     331             : 
     332           0 :         dst  += dst_linesize;
     333           0 :         lsrc += l_linesize;
     334           0 :         rsrc += r_linesize;
     335             :     }
     336           0 : }
     337             : 
     338         630 : static void anaglyph(uint8_t *dst, uint8_t *lsrc, uint8_t *rsrc,
     339             :                      ptrdiff_t dst_linesize, ptrdiff_t l_linesize, ptrdiff_t r_linesize,
     340             :                      int width, int height,
     341             :                      const int *ana_matrix_r, const int *ana_matrix_g, const int *ana_matrix_b)
     342             : {
     343             :     int x, y, o;
     344             : 
     345       20790 :     for (y = 0; y < height; y++) {
     346     3568320 :         for (o = 0, x = 0; x < width; x++, o+= 3) {
     347     3548160 :             dst[o    ] = ana_convert(ana_matrix_r, lsrc + o, rsrc + o);
     348     3548160 :             dst[o + 1] = ana_convert(ana_matrix_g, lsrc + o, rsrc + o);
     349     3548160 :             dst[o + 2] = ana_convert(ana_matrix_b, lsrc + o, rsrc + o);
     350             :         }
     351             : 
     352       20160 :         dst  += dst_linesize;
     353       20160 :         lsrc += l_linesize;
     354       20160 :         rsrc += r_linesize;
     355             :     }
     356         630 : }
     357             : 
     358          22 : static int config_output(AVFilterLink *outlink)
     359             : {
     360          22 :     AVFilterContext *ctx = outlink->src;
     361          22 :     AVFilterLink *inlink = ctx->inputs[0];
     362          22 :     Stereo3DContext *s = ctx->priv;
     363          22 :     AVRational fps = inlink->frame_rate;
     364          22 :     AVRational tb = inlink->time_base;
     365          22 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
     366             :     int ret;
     367          22 :     s->aspect = inlink->sample_aspect_ratio;
     368             : 
     369          22 :     switch (s->in.format) {
     370          18 :     case INTERLEAVE_COLS_LR:
     371             :     case INTERLEAVE_COLS_RL:
     372             :     case SIDE_BY_SIDE_2_LR:
     373             :     case SIDE_BY_SIDE_LR:
     374             :     case SIDE_BY_SIDE_2_RL:
     375             :     case SIDE_BY_SIDE_RL:
     376          18 :         if (inlink->w & 1) {
     377           0 :             av_log(ctx, AV_LOG_ERROR, "width must be even\n");
     378           0 :             return AVERROR_INVALIDDATA;
     379             :         }
     380          18 :         break;
     381           2 :     case INTERLEAVE_ROWS_LR:
     382             :     case INTERLEAVE_ROWS_RL:
     383             :     case ABOVE_BELOW_2_LR:
     384             :     case ABOVE_BELOW_LR:
     385             :     case ABOVE_BELOW_2_RL:
     386             :     case ABOVE_BELOW_RL:
     387           2 :         if (inlink->h & 1) {
     388           0 :             av_log(ctx, AV_LOG_ERROR, "height must be even\n");
     389           0 :             return AVERROR_INVALIDDATA;
     390             :         }
     391           2 :         break;
     392             :     }
     393             : 
     394          22 :     s->in.width     =
     395          22 :     s->width        = inlink->w;
     396          22 :     s->in.height    =
     397          22 :     s->height       = inlink->h;
     398          22 :     s->in.off_lstep =
     399          22 :     s->in.off_rstep =
     400          22 :     s->in.off_left  =
     401          22 :     s->in.off_right =
     402          22 :     s->in.row_left  =
     403          22 :     s->in.row_right = 0;
     404          22 :     s->in.row_step  = 1;
     405             : 
     406          22 :     switch (s->in.format) {
     407           0 :     case SIDE_BY_SIDE_2_LR:
     408           0 :         s->aspect.num  *= 2;
     409          18 :     case SIDE_BY_SIDE_LR:
     410          18 :         s->width        = inlink->w / 2;
     411          18 :         s->in.off_right = s->width;
     412          18 :         break;
     413           0 :     case SIDE_BY_SIDE_2_RL:
     414           0 :         s->aspect.num  *= 2;
     415           0 :     case SIDE_BY_SIDE_RL:
     416           0 :         s->width        = inlink->w / 2;
     417           0 :         s->in.off_left  = s->width;
     418           0 :         break;
     419           0 :     case ABOVE_BELOW_2_LR:
     420           0 :         s->aspect.den  *= 2;
     421           0 :     case ABOVE_BELOW_LR:
     422           0 :         s->in.row_right =
     423           0 :         s->height       = inlink->h / 2;
     424           0 :         break;
     425           0 :     case ABOVE_BELOW_2_RL:
     426           0 :         s->aspect.den  *= 2;
     427           2 :     case ABOVE_BELOW_RL:
     428           2 :         s->in.row_left  =
     429           2 :         s->height       = inlink->h / 2;
     430           2 :         break;
     431           2 :     case ALTERNATING_RL:
     432             :     case ALTERNATING_LR:
     433           2 :         fps.den        *= 2;
     434           2 :         tb.num         *= 2;
     435           2 :         break;
     436           0 :     case INTERLEAVE_COLS_RL:
     437             :     case INTERLEAVE_COLS_LR:
     438           0 :         s->width        = inlink->w / 2;
     439           0 :         break;
     440           0 :     case INTERLEAVE_ROWS_LR:
     441             :     case INTERLEAVE_ROWS_RL:
     442           0 :         s->in.row_step  = 2;
     443           0 :         if (s->in.format == INTERLEAVE_ROWS_RL)
     444           0 :             s->in.off_lstep = 1;
     445             :         else
     446           0 :             s->in.off_rstep = 1;
     447           0 :         if (s->out.format != CHECKERBOARD_LR &&
     448           0 :             s->out.format != CHECKERBOARD_RL)
     449           0 :             s->height   = inlink->h / 2;
     450           0 :         break;
     451           0 :     default:
     452           0 :         av_log(ctx, AV_LOG_ERROR, "input format %d is not supported\n", s->in.format);
     453           0 :         return AVERROR(EINVAL);
     454             :     }
     455             : 
     456          22 :     s->out.width     = s->width;
     457          22 :     s->out.height    = s->height;
     458          22 :     s->out.off_lstep =
     459          22 :     s->out.off_rstep =
     460          22 :     s->out.off_left  =
     461          22 :     s->out.off_right =
     462          22 :     s->out.row_left  =
     463          22 :     s->out.row_right = 0;
     464          22 :     s->out.row_step  = 1;
     465             : 
     466          22 :     switch (s->out.format) {
     467          14 :     case ANAGLYPH_RB_GRAY:
     468             :     case ANAGLYPH_RG_GRAY:
     469             :     case ANAGLYPH_RC_GRAY:
     470             :     case ANAGLYPH_RC_HALF:
     471             :     case ANAGLYPH_RC_COLOR:
     472             :     case ANAGLYPH_RC_DUBOIS:
     473             :     case ANAGLYPH_GM_GRAY:
     474             :     case ANAGLYPH_GM_HALF:
     475             :     case ANAGLYPH_GM_COLOR:
     476             :     case ANAGLYPH_GM_DUBOIS:
     477             :     case ANAGLYPH_YB_GRAY:
     478             :     case ANAGLYPH_YB_HALF:
     479             :     case ANAGLYPH_YB_COLOR:
     480             :     case ANAGLYPH_YB_DUBOIS: {
     481             :         uint8_t rgba_map[4];
     482             : 
     483          14 :         ff_fill_rgba_map(rgba_map, outlink->format);
     484          14 :         s->ana_matrix[rgba_map[0]] = &ana_coeff[s->out.format][0][0];
     485          14 :         s->ana_matrix[rgba_map[1]] = &ana_coeff[s->out.format][1][0];
     486          14 :         s->ana_matrix[rgba_map[2]] = &ana_coeff[s->out.format][2][0];
     487          14 :         break;
     488             :     }
     489           0 :     case SIDE_BY_SIDE_2_LR:
     490           0 :         s->aspect.den   *= 2;
     491           1 :     case SIDE_BY_SIDE_LR:
     492           1 :         s->out.width     = s->width * 2;
     493           1 :         s->out.off_right = s->width;
     494           1 :         break;
     495           0 :     case SIDE_BY_SIDE_2_RL:
     496           0 :         s->aspect.den   *= 2;
     497           1 :     case SIDE_BY_SIDE_RL:
     498           1 :         s->out.width     = s->width * 2;
     499           1 :         s->out.off_left  = s->width;
     500           1 :         break;
     501           0 :     case ABOVE_BELOW_2_LR:
     502           0 :         s->aspect.num   *= 2;
     503           2 :     case ABOVE_BELOW_LR:
     504           2 :         s->out.height    = s->height * 2;
     505           2 :         s->out.row_right = s->height;
     506           2 :         break;
     507           0 :     case HDMI:
     508           0 :         if (s->height != 720 && s->height != 1080) {
     509           0 :             av_log(ctx, AV_LOG_ERROR, "Only 720 and 1080 height supported\n");
     510           0 :             return AVERROR(EINVAL);
     511             :         }
     512             : 
     513           0 :         s->blanks = s->height / 24;
     514           0 :         s->out.height    = s->height * 2 + s->blanks;
     515           0 :         s->out.row_right = s->height + s->blanks;
     516           0 :         break;
     517           0 :     case ABOVE_BELOW_2_RL:
     518           0 :         s->aspect.num   *= 2;
     519           1 :     case ABOVE_BELOW_RL:
     520           1 :         s->out.height    = s->height * 2;
     521           1 :         s->out.row_left  = s->height;
     522           1 :         break;
     523           0 :     case INTERLEAVE_ROWS_LR:
     524           0 :         s->in.row_step   = 1 + (s->in.format == INTERLEAVE_ROWS_RL);
     525           0 :         s->out.row_step  = 2;
     526           0 :         s->out.height    = s->height * 2;
     527           0 :         s->out.off_rstep = 1;
     528           0 :         break;
     529           0 :     case INTERLEAVE_ROWS_RL:
     530           0 :         s->in.row_step   = 1 + (s->in.format == INTERLEAVE_ROWS_LR);
     531           0 :         s->out.row_step  = 2;
     532           0 :         s->out.height    = s->height * 2;
     533           0 :         s->out.off_lstep = 1;
     534           0 :         break;
     535           1 :     case MONO_R:
     536           1 :         if (s->in.format != INTERLEAVE_COLS_LR) {
     537           1 :             s->in.off_left = s->in.off_right;
     538           1 :             s->in.row_left = s->in.row_right;
     539             :         }
     540           1 :         if (s->in.format == INTERLEAVE_ROWS_LR)
     541           0 :             FFSWAP(int, s->in.off_lstep, s->in.off_rstep);
     542           1 :         break;
     543           1 :     case MONO_L:
     544           1 :         if (s->in.format == INTERLEAVE_ROWS_RL)
     545           0 :             FFSWAP(int, s->in.off_lstep, s->in.off_rstep);
     546           1 :         break;
     547           1 :     case ALTERNATING_RL:
     548             :     case ALTERNATING_LR:
     549           1 :         fps.num         *= 2;
     550           1 :         tb.den          *= 2;
     551           1 :         break;
     552           0 :     case CHECKERBOARD_LR:
     553             :     case CHECKERBOARD_RL:
     554           0 :         s->out.width     = s->width * 2;
     555           0 :         break;
     556           0 :     case INTERLEAVE_COLS_LR:
     557             :     case INTERLEAVE_COLS_RL:
     558           0 :         s->out.width     = s->width * 2;
     559           0 :         break;
     560           0 :     default:
     561           0 :         av_log(ctx, AV_LOG_ERROR, "output format %d is not supported\n", s->out.format);
     562           0 :         return AVERROR(EINVAL);
     563             :     }
     564             : 
     565          22 :     if (s->in.format == INTERLEAVE_COLS_LR || s->in.format == INTERLEAVE_COLS_RL) {
     566           0 :         if ((s->in.format & 1) != (s->out.format & 1)) {
     567           0 :             FFSWAP(int, s->in.row_left,   s->in.row_right);
     568           0 :             FFSWAP(int, s->in.off_lstep,  s->in.off_rstep);
     569           0 :             FFSWAP(int, s->in.off_left,   s->in.off_right);
     570           0 :             FFSWAP(int, s->out.row_left,  s->out.row_right);
     571           0 :             FFSWAP(int, s->out.off_lstep, s->out.off_rstep);
     572           0 :             FFSWAP(int, s->out.off_left,  s->out.off_right);
     573             :         }
     574             :     }
     575             : 
     576          22 :     outlink->w = s->out.width;
     577          22 :     outlink->h = s->out.height;
     578          22 :     outlink->frame_rate = fps;
     579          22 :     outlink->time_base = tb;
     580          22 :     outlink->sample_aspect_ratio = s->aspect;
     581             : 
     582          22 :     if ((ret = av_image_fill_linesizes(s->linesize, outlink->format, s->width)) < 0)
     583           0 :         return ret;
     584          22 :     s->nb_planes = av_pix_fmt_count_planes(outlink->format);
     585          22 :     av_image_fill_max_pixsteps(s->pixstep, NULL, desc);
     586          22 :     s->pheight[1] = s->pheight[2] = AV_CEIL_RSHIFT(s->height, desc->log2_chroma_h);
     587          22 :     s->pheight[0] = s->pheight[3] = s->height;
     588          22 :     s->hsub = desc->log2_chroma_w;
     589          22 :     s->vsub = desc->log2_chroma_h;
     590             : 
     591          22 :     s->dsp.anaglyph = anaglyph;
     592             :     if (ARCH_X86)
     593          22 :         ff_stereo3d_init_x86(&s->dsp);
     594             : 
     595          22 :     return 0;
     596             : }
     597             : 
     598             : typedef struct ThreadData {
     599             :     AVFrame *ileft, *iright;
     600             :     AVFrame *out;
     601             : } ThreadData;
     602             : 
     603         630 : static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
     604             : {
     605         630 :     Stereo3DContext *s = ctx->priv;
     606         630 :     ThreadData *td = arg;
     607         630 :     AVFrame *ileft = td->ileft;
     608         630 :     AVFrame *iright = td->iright;
     609         630 :     AVFrame *out = td->out;
     610         630 :     int height = s->out.height;
     611         630 :     int start = (height *  jobnr   ) / nb_jobs;
     612         630 :     int end   = (height * (jobnr+1)) / nb_jobs;
     613         630 :     const int **ana_matrix = s->ana_matrix;
     614             : 
     615        5040 :     s->dsp.anaglyph(out->data[0] + out->linesize[0] * start,
     616         630 :              ileft ->data[0] + s->in_off_left [0]  + ileft->linesize[0] * start * s->in.row_step,
     617         630 :              iright->data[0] + s->in_off_right[0] + iright->linesize[0] * start * s->in.row_step,
     618         630 :              out->linesize[0],
     619         630 :              ileft->linesize[0] * s->in.row_step,
     620         630 :              iright->linesize[0] * s->in.row_step,
     621             :              s->out.width, end - start,
     622        1260 :              ana_matrix[0], ana_matrix[1], ana_matrix[2]);
     623             : 
     624         630 :     return 0;
     625             : }
     626             : 
     627           0 : static void interleave_cols_to_any(Stereo3DContext *s, int *out_off, int p, AVFrame *in, AVFrame *out, int d)
     628             : {
     629             :     int y, x;
     630             : 
     631           0 :     for (y = 0; y < s->pheight[p]; y++) {
     632           0 :         const uint8_t *src = (const uint8_t*)in->data[p] + y * in->linesize[p] + d * s->pixstep[p];
     633           0 :         uint8_t *dst = out->data[p] + out_off[p] + y * out->linesize[p] * s->out.row_step;
     634             : 
     635           0 :         switch (s->pixstep[p]) {
     636           0 :         case 1:
     637           0 :             for (x = 0; x < s->linesize[p]; x++)
     638           0 :                 dst[x] = src[x * 2];
     639           0 :             break;
     640           0 :         case 2:
     641           0 :             for (x = 0; x < s->linesize[p]; x+=2)
     642           0 :                 AV_WN16(&dst[x], AV_RN16(&src[x * 2]));
     643           0 :             break;
     644           0 :         case 3:
     645           0 :             for (x = 0; x < s->linesize[p]; x+=3)
     646           0 :                 AV_WB24(&dst[x], AV_RB24(&src[x * 2]));
     647           0 :             break;
     648           0 :         case 4:
     649           0 :             for (x = 0; x < s->linesize[p]; x+=4)
     650           0 :                 AV_WN32(&dst[x], AV_RN32(&src[x * 2]));
     651           0 :             break;
     652           0 :         case 6:
     653           0 :             for (x = 0; x < s->linesize[p]; x+=6)
     654           0 :                 AV_WB48(&dst[x], AV_RB48(&src[x * 2]));
     655           0 :             break;
     656           0 :         case 8:
     657           0 :             for (x = 0; x < s->linesize[p]; x+=8)
     658           0 :                 AV_WN64(&dst[x], AV_RN64(&src[x * 2]));
     659           0 :             break;
     660             :         }
     661             :     }
     662           0 : }
     663             : 
     664         119 : static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
     665             : {
     666         119 :     AVFilterContext *ctx  = inlink->dst;
     667         119 :     Stereo3DContext *s = ctx->priv;
     668         119 :     AVFilterLink *outlink = ctx->outputs[0];
     669             :     AVFrame *out, *oleft, *oright, *ileft, *iright;
     670             :     int out_off_left[4], out_off_right[4];
     671             :     int i, ret;
     672             : 
     673         119 :     if (s->in.format == s->out.format)
     674           0 :         return ff_filter_frame(outlink, inpicref);
     675             : 
     676         119 :     switch (s->out.format) {
     677           4 :     case ALTERNATING_LR:
     678             :     case ALTERNATING_RL:
     679           4 :         if (!s->prev) {
     680           1 :             s->prev = inpicref;
     681           1 :             return 0;
     682             :         }
     683           3 :         break;
     684             :     };
     685             : 
     686         118 :     switch (s->in.format) {
     687          20 :     case ALTERNATING_LR:
     688             :     case ALTERNATING_RL:
     689          20 :         if (!s->prev) {
     690          10 :             s->prev = inpicref;
     691          10 :             return 0;
     692             :         }
     693          10 :         ileft  = s->prev;
     694          10 :         iright = inpicref;
     695          10 :         if (s->in.format == ALTERNATING_RL)
     696           5 :             FFSWAP(AVFrame *, ileft, iright);
     697          10 :         break;
     698          98 :     default:
     699          98 :         ileft = iright = inpicref;
     700             :     };
     701             : 
     702         213 :     if ((s->out.format == ALTERNATING_LR ||
     703         108 :          s->out.format == ALTERNATING_RL) &&
     704           3 :         (s->in.format == SIDE_BY_SIDE_LR ||
     705           0 :          s->in.format == SIDE_BY_SIDE_RL ||
     706           0 :          s->in.format == SIDE_BY_SIDE_2_LR ||
     707           0 :          s->in.format == SIDE_BY_SIDE_2_RL ||
     708           0 :          s->in.format == ABOVE_BELOW_LR ||
     709           0 :          s->in.format == ABOVE_BELOW_RL ||
     710           0 :          s->in.format == ABOVE_BELOW_2_LR ||
     711           0 :          s->in.format == ABOVE_BELOW_2_RL ||
     712           0 :          s->in.format == INTERLEAVE_ROWS_LR ||
     713           0 :          s->in.format == INTERLEAVE_ROWS_RL)) {
     714           3 :         oright = av_frame_clone(s->prev);
     715           3 :         oleft  = av_frame_clone(s->prev);
     716           6 :         if (!oright || !oleft) {
     717           0 :             av_frame_free(&oright);
     718           0 :             av_frame_free(&oleft);
     719           0 :             av_frame_free(&s->prev);
     720           0 :             av_frame_free(&inpicref);
     721           0 :             return AVERROR(ENOMEM);
     722             :         }
     723         205 :     } else if ((s->out.format == MONO_L ||
     724         110 :                 s->out.format == MONO_R) &&
     725          20 :         (s->in.format == SIDE_BY_SIDE_LR ||
     726          20 :          s->in.format == SIDE_BY_SIDE_RL ||
     727          20 :          s->in.format == SIDE_BY_SIDE_2_LR ||
     728          20 :          s->in.format == SIDE_BY_SIDE_2_RL ||
     729          20 :          s->in.format == ABOVE_BELOW_LR ||
     730          10 :          s->in.format == ABOVE_BELOW_RL ||
     731           0 :          s->in.format == ABOVE_BELOW_2_LR ||
     732           0 :          s->in.format == ABOVE_BELOW_2_RL ||
     733           0 :          s->in.format == INTERLEAVE_ROWS_LR ||
     734           0 :          s->in.format == INTERLEAVE_ROWS_RL)) {
     735          10 :         out = oleft = oright = av_frame_clone(inpicref);
     736          20 :         if (!out) {
     737           0 :             av_frame_free(&s->prev);
     738           0 :             av_frame_free(&inpicref);
     739           0 :             return AVERROR(ENOMEM);
     740             :         }
     741         190 :     } else if ((s->out.format == MONO_L && s->in.format == ALTERNATING_LR) ||
     742          95 :                (s->out.format == MONO_R && s->in.format == ALTERNATING_RL)) {
     743           0 :         s->prev->pts /= 2;
     744           0 :         ret = ff_filter_frame(outlink, s->prev);
     745           0 :         av_frame_free(&inpicref);
     746           0 :         s->prev = NULL;
     747           0 :         return ret;
     748         190 :     } else if ((s->out.format == MONO_L && s->in.format == ALTERNATING_RL) ||
     749          95 :                (s->out.format == MONO_R && s->in.format == ALTERNATING_LR)) {
     750           0 :         av_frame_free(&s->prev);
     751           0 :         inpicref->pts /= 2;
     752           0 :         return ff_filter_frame(outlink, inpicref);
     753         190 :     } else if ((s->out.format == ALTERNATING_LR && s->in.format == ALTERNATING_RL) ||
     754          95 :                (s->out.format == ALTERNATING_RL && s->in.format == ALTERNATING_LR)) {
     755           0 :         FFSWAP(int64_t, s->prev->pts, inpicref->pts);
     756           0 :         ff_filter_frame(outlink, inpicref);
     757           0 :         ret = ff_filter_frame(outlink, s->prev);
     758           0 :         s->prev = NULL;
     759           0 :         return ret;
     760             :     } else {
     761          95 :         out = oleft = oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     762          95 :         if (!out) {
     763           0 :             av_frame_free(&s->prev);
     764           0 :             av_frame_free(&inpicref);
     765           0 :             return AVERROR(ENOMEM);
     766             :         }
     767          95 :         av_frame_copy_props(out, inpicref);
     768             : 
     769         190 :         if (s->out.format == ALTERNATING_LR ||
     770          95 :             s->out.format == ALTERNATING_RL) {
     771           0 :             oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     772           0 :             if (!oright) {
     773           0 :                 av_frame_free(&oleft);
     774           0 :                 av_frame_free(&s->prev);
     775           0 :                 av_frame_free(&inpicref);
     776           0 :                 return AVERROR(ENOMEM);
     777             :             }
     778           0 :             av_frame_copy_props(oright, s->prev);
     779             :         }
     780             :     }
     781             : 
     782         540 :     for (i = 0; i < 4; i++) {
     783         432 :         int hsub = i == 1 || i == 2 ? s->hsub : 0;
     784         432 :         int vsub = i == 1 || i == 2 ? s->vsub : 0;
     785         432 :         s->in_off_left[i]   = (AV_CEIL_RSHIFT(s->in.row_left,   vsub) + s->in.off_lstep)  * ileft->linesize[i]  + AV_CEIL_RSHIFT(s->in.off_left   * s->pixstep[i], hsub);
     786         432 :         s->in_off_right[i]  = (AV_CEIL_RSHIFT(s->in.row_right,  vsub) + s->in.off_rstep)  * iright->linesize[i] + AV_CEIL_RSHIFT(s->in.off_right  * s->pixstep[i], hsub);
     787         432 :         out_off_left[i]  = (AV_CEIL_RSHIFT(s->out.row_left,  vsub) + s->out.off_lstep) * oleft->linesize[i]  + AV_CEIL_RSHIFT(s->out.off_left  * s->pixstep[i], hsub);
     788         432 :         out_off_right[i] = (AV_CEIL_RSHIFT(s->out.row_right, vsub) + s->out.off_rstep) * oright->linesize[i] + AV_CEIL_RSHIFT(s->out.off_right * s->pixstep[i], hsub);
     789             :     }
     790             : 
     791         108 :     switch (s->out.format) {
     792           3 :     case ALTERNATING_LR:
     793             :     case ALTERNATING_RL:
     794           3 :         switch (s->in.format) {
     795           0 :         case INTERLEAVE_ROWS_LR:
     796             :         case INTERLEAVE_ROWS_RL:
     797           0 :             for (i = 0; i < s->nb_planes; i++) {
     798           0 :                 oleft->linesize[i]  *= 2;
     799           0 :                 oright->linesize[i] *= 2;
     800             :             }
     801             :         case ABOVE_BELOW_LR:
     802             :         case ABOVE_BELOW_RL:
     803             :         case ABOVE_BELOW_2_LR:
     804             :         case ABOVE_BELOW_2_RL:
     805             :         case SIDE_BY_SIDE_LR:
     806             :         case SIDE_BY_SIDE_RL:
     807             :         case SIDE_BY_SIDE_2_LR:
     808             :         case SIDE_BY_SIDE_2_RL:
     809           3 :             oleft->width   = outlink->w;
     810           3 :             oright->width  = outlink->w;
     811           3 :             oleft->height  = outlink->h;
     812           3 :             oright->height = outlink->h;
     813             : 
     814          12 :             for (i = 0; i < s->nb_planes; i++) {
     815           9 :                 oleft->data[i]  += s->in_off_left[i];
     816           9 :                 oright->data[i] += s->in_off_right[i];
     817             :             }
     818           3 :             break;
     819           0 :         default:
     820           0 :             goto copy;
     821             :             break;
     822             :         }
     823           3 :         break;
     824           0 :     case HDMI:
     825           0 :         for (i = 0; i < s->nb_planes; i++) {
     826           0 :             int j, h = s->height >> ((i == 1 || i == 2) ? s->vsub : 0);
     827           0 :             int b = (s->blanks) >> ((i == 1 || i == 2) ? s->vsub : 0);
     828             : 
     829           0 :             for (j = h; j < h + b; j++)
     830           0 :                 memset(oleft->data[i] + j * s->linesize[i], 0, s->linesize[i]);
     831             :         }
     832             :     case SIDE_BY_SIDE_LR:
     833             :     case SIDE_BY_SIDE_RL:
     834             :     case SIDE_BY_SIDE_2_LR:
     835             :     case SIDE_BY_SIDE_2_RL:
     836             :     case ABOVE_BELOW_LR:
     837             :     case ABOVE_BELOW_RL:
     838             :     case ABOVE_BELOW_2_LR:
     839             :     case ABOVE_BELOW_2_RL:
     840             :     case INTERLEAVE_ROWS_LR:
     841             :     case INTERLEAVE_ROWS_RL:
     842           0 : copy:
     843          50 :         if (s->in.format == INTERLEAVE_COLS_LR ||
     844          25 :             s->in.format == INTERLEAVE_COLS_RL) {
     845           0 :             for (i = 0; i < s->nb_planes; i++) {
     846           0 :                 int d = (s->in.format & 1) != (s->out.format & 1);
     847             : 
     848           0 :                 interleave_cols_to_any(s, out_off_left,  i, ileft,  oleft,   d);
     849           0 :                 interleave_cols_to_any(s, out_off_right, i, iright, oright, !d);
     850             :             }
     851             :         } else {
     852         100 :             for (i = 0; i < s->nb_planes; i++) {
     853         225 :                 av_image_copy_plane(oleft->data[i] + out_off_left[i],
     854          75 :                                     oleft->linesize[i] * s->out.row_step,
     855          75 :                                     ileft->data[i] + s->in_off_left[i],
     856          75 :                                     ileft->linesize[i] * s->in.row_step,
     857             :                                     s->linesize[i], s->pheight[i]);
     858         225 :                 av_image_copy_plane(oright->data[i] + out_off_right[i],
     859          75 :                                     oright->linesize[i] * s->out.row_step,
     860          75 :                                     iright->data[i] + s->in_off_right[i],
     861          75 :                                     iright->linesize[i] * s->in.row_step,
     862             :                                     s->linesize[i], s->pheight[i]);
     863             :             }
     864             :         }
     865          25 :         break;
     866           5 :     case MONO_L:
     867           5 :         iright = ileft;
     868          10 :     case MONO_R:
     869          10 :         switch (s->in.format) {
     870           0 :         case INTERLEAVE_ROWS_LR:
     871             :         case INTERLEAVE_ROWS_RL:
     872           0 :             for (i = 0; i < s->nb_planes; i++) {
     873           0 :                 out->linesize[i] *= 2;
     874             :             }
     875             :         case ABOVE_BELOW_LR:
     876             :         case ABOVE_BELOW_RL:
     877             :         case ABOVE_BELOW_2_LR:
     878             :         case ABOVE_BELOW_2_RL:
     879             :         case SIDE_BY_SIDE_LR:
     880             :         case SIDE_BY_SIDE_RL:
     881             :         case SIDE_BY_SIDE_2_LR:
     882             :         case SIDE_BY_SIDE_2_RL:
     883          10 :             out->width  = outlink->w;
     884          10 :             out->height = outlink->h;
     885             : 
     886          40 :             for (i = 0; i < s->nb_planes; i++) {
     887          30 :                 out->data[i] += s->in_off_left[i];
     888             :             }
     889          10 :             break;
     890           0 :         case INTERLEAVE_COLS_LR:
     891             :         case INTERLEAVE_COLS_RL:
     892           0 :             for (i = 0; i < s->nb_planes; i++) {
     893           0 :                 const int d = (s->in.format & 1) != (s->out.format & 1);
     894             : 
     895           0 :                 interleave_cols_to_any(s, out_off_right, i, iright, out, d);
     896             :             }
     897           0 :             break;
     898           0 :         default:
     899           0 :             for (i = 0; i < s->nb_planes; i++) {
     900           0 :                 av_image_copy_plane(out->data[i], out->linesize[i],
     901           0 :                                     iright->data[i] + s->in_off_left[i],
     902           0 :                                     iright->linesize[i] * s->in.row_step,
     903             :                                     s->linesize[i], s->pheight[i]);
     904             :             }
     905           0 :             break;
     906             :         }
     907          10 :         break;
     908          70 :     case ANAGLYPH_RB_GRAY:
     909             :     case ANAGLYPH_RG_GRAY:
     910             :     case ANAGLYPH_RC_GRAY:
     911             :     case ANAGLYPH_RC_HALF:
     912             :     case ANAGLYPH_RC_COLOR:
     913             :     case ANAGLYPH_RC_DUBOIS:
     914             :     case ANAGLYPH_GM_GRAY:
     915             :     case ANAGLYPH_GM_HALF:
     916             :     case ANAGLYPH_GM_COLOR:
     917             :     case ANAGLYPH_GM_DUBOIS:
     918             :     case ANAGLYPH_YB_GRAY:
     919             :     case ANAGLYPH_YB_HALF:
     920             :     case ANAGLYPH_YB_COLOR:
     921             :     case ANAGLYPH_YB_DUBOIS: {
     922         140 :         if (s->in.format == INTERLEAVE_COLS_LR ||
     923          70 :             s->in.format == INTERLEAVE_COLS_RL) {
     924           0 :             const int d = (s->in.format & 1);
     925             : 
     926           0 :             anaglyph_ic(out->data[0],
     927           0 :                 ileft ->data[0] + s->in_off_left [0] +   d  * 3,
     928           0 :                 iright->data[0] + s->in_off_right[0] + (!d) * 3,
     929           0 :                 out->linesize[0],
     930           0 :                 ileft->linesize[0] * s->in.row_step,
     931           0 :                 iright->linesize[0] * s->in.row_step,
     932             :                 s->out.width, s->out.height,
     933             :                 s->ana_matrix[0], s->ana_matrix[1], s->ana_matrix[2]);
     934             :         } else {
     935             :             ThreadData td;
     936             : 
     937          70 :             td.ileft = ileft; td.iright = iright; td.out = out;
     938         140 :             ctx->internal->execute(ctx, filter_slice, &td, NULL,
     939          70 :                                    FFMIN(s->out.height, ff_filter_get_nb_threads(ctx)));
     940             :         }
     941          70 :         break;
     942             :     }
     943           0 :     case CHECKERBOARD_RL:
     944             :     case CHECKERBOARD_LR:
     945           0 :         for (i = 0; i < s->nb_planes; i++) {
     946             :             int x, y;
     947             : 
     948           0 :             for (y = 0; y < s->pheight[i]; y++) {
     949           0 :                 uint8_t *dst = out->data[i] + out->linesize[i] * y;
     950           0 :                 const int d1 = (s->in.format == INTERLEAVE_COLS_LR || s->in.format == INTERLEAVE_COLS_RL) && (s->in.format & 1) != (s->out.format & 1);
     951           0 :                 const int d2 = (s->in.format == INTERLEAVE_COLS_LR || s->in.format == INTERLEAVE_COLS_RL) ? !d1 : 0;
     952           0 :                 const int m = 1 + (s->in.format == INTERLEAVE_COLS_LR || s->in.format == INTERLEAVE_COLS_RL);
     953           0 :                 uint8_t *left  = ileft->data[i]  + ileft->linesize[i]  * y + s->in_off_left[i]  + d1 * s->pixstep[i];
     954           0 :                 uint8_t *right = iright->data[i] + iright->linesize[i] * y + s->in_off_right[i] + d2 * s->pixstep[i];
     955             :                 int p, b;
     956             : 
     957           0 :                 if (s->out.format == CHECKERBOARD_RL && s->in.format != INTERLEAVE_COLS_LR && s->in.format != INTERLEAVE_COLS_RL)
     958           0 :                     FFSWAP(uint8_t*, left, right);
     959           0 :                 switch (s->pixstep[i]) {
     960           0 :                 case 1:
     961           0 :                     for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=2, p++, b+=2) {
     962           0 :                         dst[x  ] = (b&1) == (y&1) ? left[p*m] : right[p*m];
     963           0 :                         dst[x+1] = (b&1) != (y&1) ? left[p*m] : right[p*m];
     964             :                     }
     965           0 :                     break;
     966           0 :                 case 2:
     967           0 :                     for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=4, p+=2, b+=2) {
     968           0 :                         AV_WN16(&dst[x  ], (b&1) == (y&1) ? AV_RN16(&left[p*m]) : AV_RN16(&right[p*m]));
     969           0 :                         AV_WN16(&dst[x+2], (b&1) != (y&1) ? AV_RN16(&left[p*m]) : AV_RN16(&right[p*m]));
     970             :                     }
     971           0 :                     break;
     972           0 :                 case 3:
     973           0 :                     for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=6, p+=3, b+=2) {
     974           0 :                         AV_WB24(&dst[x  ], (b&1) == (y&1) ? AV_RB24(&left[p*m]) : AV_RB24(&right[p*m]));
     975           0 :                         AV_WB24(&dst[x+3], (b&1) != (y&1) ? AV_RB24(&left[p*m]) : AV_RB24(&right[p*m]));
     976             :                     }
     977           0 :                     break;
     978           0 :                 case 4:
     979           0 :                     for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=8, p+=4, b+=2) {
     980           0 :                         AV_WN32(&dst[x  ], (b&1) == (y&1) ? AV_RN32(&left[p*m]) : AV_RN32(&right[p*m]));
     981           0 :                         AV_WN32(&dst[x+4], (b&1) != (y&1) ? AV_RN32(&left[p*m]) : AV_RN32(&right[p*m]));
     982             :                     }
     983           0 :                     break;
     984           0 :                 case 6:
     985           0 :                     for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=12, p+=6, b+=2) {
     986           0 :                         AV_WB48(&dst[x  ], (b&1) == (y&1) ? AV_RB48(&left[p*m]) : AV_RB48(&right[p*m]));
     987           0 :                         AV_WB48(&dst[x+6], (b&1) != (y&1) ? AV_RB48(&left[p*m]) : AV_RB48(&right[p*m]));
     988             :                     }
     989           0 :                     break;
     990           0 :                 case 8:
     991           0 :                     for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=16, p+=8, b+=2) {
     992           0 :                         AV_WN64(&dst[x  ], (b&1) == (y&1) ? AV_RN64(&left[p*m]) : AV_RN64(&right[p*m]));
     993           0 :                         AV_WN64(&dst[x+8], (b&1) != (y&1) ? AV_RN64(&left[p*m]) : AV_RN64(&right[p*m]));
     994             :                     }
     995           0 :                     break;
     996             :                 }
     997             :             }
     998             :         }
     999           0 :         break;
    1000           0 :     case INTERLEAVE_COLS_LR:
    1001             :     case INTERLEAVE_COLS_RL:
    1002           0 :         for (i = 0; i < s->nb_planes; i++) {
    1003           0 :             const int d = (s->in.format == INTERLEAVE_COLS_LR || s->in.format == INTERLEAVE_COLS_RL);
    1004           0 :             const int m = 1 + d;
    1005             :             int x, y;
    1006             : 
    1007           0 :             for (y = 0; y < s->pheight[i]; y++) {
    1008           0 :                 uint8_t *dst = out->data[i] + out->linesize[i] * y;
    1009           0 :                 uint8_t *left = ileft->data[i] + ileft->linesize[i] * y * s->in.row_step + s->in_off_left[i] + d * s->pixstep[i];
    1010           0 :                 uint8_t *right = iright->data[i] + iright->linesize[i] * y * s->in.row_step + s->in_off_right[i];
    1011             :                 int p, b;
    1012             : 
    1013           0 :                 if (s->out.format == INTERLEAVE_COLS_LR)
    1014           0 :                     FFSWAP(uint8_t*, left, right);
    1015             : 
    1016           0 :                 switch (s->pixstep[i]) {
    1017           0 :                 case 1:
    1018           0 :                     for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=2, p++, b+=2) {
    1019           0 :                         dst[x  ] =   b&1  ? left[p*m] : right[p*m];
    1020           0 :                         dst[x+1] = !(b&1) ? left[p*m] : right[p*m];
    1021             :                     }
    1022           0 :                     break;
    1023           0 :                 case 2:
    1024           0 :                     for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=4, p+=2, b+=2) {
    1025           0 :                         AV_WN16(&dst[x  ],   b&1  ? AV_RN16(&left[p*m]) : AV_RN16(&right[p*m]));
    1026           0 :                         AV_WN16(&dst[x+2], !(b&1) ? AV_RN16(&left[p*m]) : AV_RN16(&right[p*m]));
    1027             :                     }
    1028           0 :                     break;
    1029           0 :                 case 3:
    1030           0 :                     for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=6, p+=3, b+=2) {
    1031           0 :                         AV_WB24(&dst[x  ],   b&1  ? AV_RB24(&left[p*m]) : AV_RB24(&right[p*m]));
    1032           0 :                         AV_WB24(&dst[x+3], !(b&1) ? AV_RB24(&left[p*m]) : AV_RB24(&right[p*m]));
    1033             :                     }
    1034           0 :                     break;
    1035           0 :                 case 4:
    1036           0 :                     for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=8, p+=4, b+=2) {
    1037           0 :                         AV_WN32(&dst[x  ],   b&1  ? AV_RN32(&left[p*m]) : AV_RN32(&right[p*m]));
    1038           0 :                         AV_WN32(&dst[x+4], !(b&1) ? AV_RN32(&left[p*m]) : AV_RN32(&right[p*m]));
    1039             :                     }
    1040           0 :                     break;
    1041           0 :                 case 6:
    1042           0 :                     for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=12, p+=6, b+=2) {
    1043           0 :                         AV_WB48(&dst[x  ],   b&1  ? AV_RB48(&left[p*m]) : AV_RB48(&right[p*m]));
    1044           0 :                         AV_WB48(&dst[x+6], !(b&1) ? AV_RB48(&left[p*m]) : AV_RB48(&right[p*m]));
    1045             :                     }
    1046           0 :                     break;
    1047           0 :                 case 8:
    1048           0 :                     for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=16, p+=8, b+=2) {
    1049           0 :                         AV_WN64(&dst[x  ],   b&1 ?  AV_RN64(&left[p*m]) : AV_RN64(&right[p*m]));
    1050           0 :                         AV_WN64(&dst[x+8], !(b&1) ? AV_RN64(&left[p*m]) : AV_RN64(&right[p*m]));
    1051             :                     }
    1052           0 :                     break;
    1053             :                 }
    1054             :             }
    1055             :         }
    1056           0 :         break;
    1057           0 :     default:
    1058           0 :         av_assert0(0);
    1059             :     }
    1060             : 
    1061         108 :     if (oright != oleft) {
    1062           3 :         if (s->out.format == ALTERNATING_LR)
    1063           3 :             FFSWAP(AVFrame *, oleft, oright);
    1064           3 :         oright->pts = s->prev->pts * 2;
    1065           3 :         ff_filter_frame(outlink, oright);
    1066           3 :         out = oleft;
    1067           3 :         oleft->pts = s->prev->pts + inpicref->pts;
    1068           3 :         av_frame_free(&s->prev);
    1069           3 :         s->prev = inpicref;
    1070         205 :     } else if (s->in.format == ALTERNATING_LR ||
    1071         100 :                s->in.format == ALTERNATING_RL) {
    1072          10 :         out->pts = s->prev->pts / 2;
    1073          10 :         av_frame_free(&s->prev);
    1074          10 :         av_frame_free(&inpicref);
    1075             :     } else {
    1076          95 :         av_frame_free(&s->prev);
    1077          95 :         av_frame_free(&inpicref);
    1078             :     }
    1079         108 :     out->sample_aspect_ratio = s->aspect;
    1080         108 :     return ff_filter_frame(outlink, out);
    1081             : }
    1082             : 
    1083          22 : static av_cold void uninit(AVFilterContext *ctx)
    1084             : {
    1085          22 :     Stereo3DContext *s = ctx->priv;
    1086             : 
    1087          22 :     av_frame_free(&s->prev);
    1088          22 : }
    1089             : 
    1090             : static const AVFilterPad stereo3d_inputs[] = {
    1091             :     {
    1092             :         .name         = "default",
    1093             :         .type         = AVMEDIA_TYPE_VIDEO,
    1094             :         .filter_frame = filter_frame,
    1095             :     },
    1096             :     { NULL }
    1097             : };
    1098             : 
    1099             : static const AVFilterPad stereo3d_outputs[] = {
    1100             :     {
    1101             :         .name         = "default",
    1102             :         .type         = AVMEDIA_TYPE_VIDEO,
    1103             :         .config_props = config_output,
    1104             :     },
    1105             :     { NULL }
    1106             : };
    1107             : 
    1108             : AVFilter ff_vf_stereo3d = {
    1109             :     .name          = "stereo3d",
    1110             :     .description   = NULL_IF_CONFIG_SMALL("Convert video stereoscopic 3D view."),
    1111             :     .priv_size     = sizeof(Stereo3DContext),
    1112             :     .uninit        = uninit,
    1113             :     .query_formats = query_formats,
    1114             :     .inputs        = stereo3d_inputs,
    1115             :     .outputs       = stereo3d_outputs,
    1116             :     .priv_class    = &stereo3d_class,
    1117             :     .flags         = AVFILTER_FLAG_SLICE_THREADS,
    1118             : };

Generated by: LCOV version 1.13