You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

624 lines
24KB

  1. /*
  2. * Copyright (c) 2010 Gordon Schmidt <gordon.schmidt <at> s2000.tu-chemnitz.de>
  3. * Copyright (c) 2013 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. #include "libavutil/avassert.h"
  22. #include "libavutil/imgutils.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/parseutils.h"
  25. #include "libavutil/pixdesc.h"
  26. #include "avfilter.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. enum StereoCode {
  31. ANAGLYPH_RC_GRAY, // anaglyph red/cyan gray
  32. ANAGLYPH_RC_HALF, // anaglyph red/cyan half colored
  33. ANAGLYPH_RC_COLOR, // anaglyph red/cyan colored
  34. ANAGLYPH_RC_DUBOIS, // anaglyph red/cyan dubois
  35. ANAGLYPH_GM_GRAY, // anaglyph green/magenta gray
  36. ANAGLYPH_GM_HALF, // anaglyph green/magenta half colored
  37. ANAGLYPH_GM_COLOR, // anaglyph green/magenta colored
  38. ANAGLYPH_GM_DUBOIS, // anaglyph green/magenta dubois
  39. ANAGLYPH_YB_GRAY, // anaglyph yellow/blue gray
  40. ANAGLYPH_YB_HALF, // anaglyph yellow/blue half colored
  41. ANAGLYPH_YB_COLOR, // anaglyph yellow/blue colored
  42. ANAGLYPH_YB_DUBOIS, // anaglyph yellow/blue dubois
  43. ANAGLYPH_RB_GRAY, // anaglyph red/blue gray
  44. ANAGLYPH_RG_GRAY, // anaglyph red/green gray
  45. MONO_L, // mono output for debugging (left eye only)
  46. MONO_R, // mono output for debugging (right eye only)
  47. INTERLEAVE_ROWS_LR, // row-interleave (left eye has top row)
  48. INTERLEAVE_ROWS_RL, // row-interleave (right eye has top row)
  49. SIDE_BY_SIDE_LR, // side by side parallel (left eye left, right eye right)
  50. SIDE_BY_SIDE_RL, // side by side crosseye (right eye left, left eye right)
  51. SIDE_BY_SIDE_2_LR, // side by side parallel with half width resolution
  52. SIDE_BY_SIDE_2_RL, // side by side crosseye with half width resolution
  53. ABOVE_BELOW_LR, // above-below (left eye above, right eye below)
  54. ABOVE_BELOW_RL, // above-below (right eye above, left eye below)
  55. ABOVE_BELOW_2_LR, // above-below with half height resolution
  56. ABOVE_BELOW_2_RL, // above-below with half height resolution
  57. ALTERNATING_LR, // alternating frames (left eye first, right eye second)
  58. ALTERNATING_RL, // alternating frames (right eye first, left eye second)
  59. STEREO_CODE_COUNT // TODO: needs autodetection
  60. };
  61. typedef struct StereoComponent {
  62. enum StereoCode format;
  63. int width, height;
  64. int off_left, off_right;
  65. int off_lstep, off_rstep;
  66. int row_left, row_right;
  67. } StereoComponent;
  68. static const int ana_coeff[][3][6] = {
  69. [ANAGLYPH_RB_GRAY] =
  70. {{19595, 38470, 7471, 0, 0, 0},
  71. { 0, 0, 0, 0, 0, 0},
  72. { 0, 0, 0, 19595, 38470, 7471}},
  73. [ANAGLYPH_RG_GRAY] =
  74. {{19595, 38470, 7471, 0, 0, 0},
  75. { 0, 0, 0, 19595, 38470, 7471},
  76. { 0, 0, 0, 0, 0, 0}},
  77. [ANAGLYPH_RC_GRAY] =
  78. {{19595, 38470, 7471, 0, 0, 0},
  79. { 0, 0, 0, 19595, 38470, 7471},
  80. { 0, 0, 0, 19595, 38470, 7471}},
  81. [ANAGLYPH_RC_HALF] =
  82. {{19595, 38470, 7471, 0, 0, 0},
  83. { 0, 0, 0, 0, 65536, 0},
  84. { 0, 0, 0, 0, 0, 65536}},
  85. [ANAGLYPH_RC_COLOR] =
  86. {{65536, 0, 0, 0, 0, 0},
  87. { 0, 0, 0, 0, 65536, 0},
  88. { 0, 0, 0, 0, 0, 65536}},
  89. [ANAGLYPH_RC_DUBOIS] =
  90. {{29891, 32800, 11559, -2849, -5763, -102},
  91. {-2627, -2479, -1033, 24804, 48080, -1209},
  92. { -997, -1350, -358, -4729, -7403, 80373}},
  93. [ANAGLYPH_GM_GRAY] =
  94. {{ 0, 0, 0, 19595, 38470, 7471},
  95. {19595, 38470, 7471, 0, 0, 0},
  96. { 0, 0, 0, 19595, 38470, 7471}},
  97. [ANAGLYPH_GM_HALF] =
  98. {{ 0, 0, 0, 65536, 0, 0},
  99. {19595, 38470, 7471, 0, 0, 0},
  100. { 0, 0, 0, 0, 0, 65536}},
  101. [ANAGLYPH_GM_COLOR] =
  102. {{ 0, 0, 0, 65536, 0, 0},
  103. { 0, 65536, 0, 0, 0, 0},
  104. { 0, 0, 0, 0, 0, 65536}},
  105. [ANAGLYPH_GM_DUBOIS] =
  106. {{-4063,-10354, -2556, 34669, 46203, 1573},
  107. {18612, 43778, 9372, -1049, -983, -4260},
  108. { -983, -1769, 1376, 590, 4915, 61407}},
  109. [ANAGLYPH_YB_GRAY] =
  110. {{ 0, 0, 0, 19595, 38470, 7471},
  111. { 0, 0, 0, 19595, 38470, 7471},
  112. {19595, 38470, 7471, 0, 0, 0}},
  113. [ANAGLYPH_YB_HALF] =
  114. {{ 0, 0, 0, 65536, 0, 0},
  115. { 0, 0, 0, 0, 65536, 0},
  116. {19595, 38470, 7471, 0, 0, 0}},
  117. [ANAGLYPH_YB_COLOR] =
  118. {{ 0, 0, 0, 65536, 0, 0},
  119. { 0, 0, 0, 0, 65536, 0},
  120. { 0, 0, 65536, 0, 0, 0}},
  121. [ANAGLYPH_YB_DUBOIS] =
  122. {{65535,-12650,18451, -987, -7590, -1049},
  123. {-1604, 56032, 4196, 370, 3826, -1049},
  124. {-2345,-10676, 1358, 5801, 11416, 56217}},
  125. };
  126. typedef struct Stereo3DContext {
  127. const AVClass *class;
  128. StereoComponent in, out;
  129. int width, height;
  130. int row_step;
  131. int ana_matrix[3][6];
  132. int nb_planes;
  133. int linesize[4];
  134. int pixstep[4];
  135. AVFrame *prev;
  136. double ts_unit;
  137. } Stereo3DContext;
  138. #define OFFSET(x) offsetof(Stereo3DContext, x)
  139. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  140. static const AVOption stereo3d_options[] = {
  141. { "in", "set input format", OFFSET(in.format), AV_OPT_TYPE_INT, {.i64=SIDE_BY_SIDE_LR}, SIDE_BY_SIDE_LR, STEREO_CODE_COUNT-1, FLAGS, "in"},
  142. { "ab2l", "above below half height left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR}, 0, 0, FLAGS, "in" },
  143. { "ab2r", "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL}, 0, 0, FLAGS, "in" },
  144. { "abl", "above below left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR}, 0, 0, FLAGS, "in" },
  145. { "abr", "above below right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL}, 0, 0, FLAGS, "in" },
  146. { "al", "alternating frames left first", 0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_LR}, 0, 0, FLAGS, "in" },
  147. { "ar", "alternating frames right first", 0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_RL}, 0, 0, FLAGS, "in" },
  148. { "sbs2l", "side by side half width left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR}, 0, 0, FLAGS, "in" },
  149. { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL}, 0, 0, FLAGS, "in" },
  150. { "sbsl", "side by side left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR}, 0, 0, FLAGS, "in" },
  151. { "sbsr", "side by side right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL}, 0, 0, FLAGS, "in" },
  152. { "out", "set output format", OFFSET(out.format), AV_OPT_TYPE_INT, {.i64=ANAGLYPH_RC_DUBOIS}, 0, STEREO_CODE_COUNT-1, FLAGS, "out"},
  153. { "ab2l", "above below half height left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR}, 0, 0, FLAGS, "out" },
  154. { "ab2r", "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL}, 0, 0, FLAGS, "out" },
  155. { "abl", "above below left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR}, 0, 0, FLAGS, "out" },
  156. { "abr", "above below right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL}, 0, 0, FLAGS, "out" },
  157. { "agmc", "anaglyph green magenta color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_COLOR}, 0, 0, FLAGS, "out" },
  158. { "agmd", "anaglyph green magenta dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_DUBOIS}, 0, 0, FLAGS, "out" },
  159. { "agmg", "anaglyph green magenta gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_GRAY}, 0, 0, FLAGS, "out" },
  160. { "agmh", "anaglyph green magenta half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_HALF}, 0, 0, FLAGS, "out" },
  161. { "al", "alternating frames left first", 0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_LR}, 0, 0, FLAGS, "out" },
  162. { "ar", "alternating frames right first", 0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_RL}, 0, 0, FLAGS, "out" },
  163. { "arbg", "anaglyph red blue gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RB_GRAY}, 0, 0, FLAGS, "out" },
  164. { "arcc", "anaglyph red cyan color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_COLOR}, 0, 0, FLAGS, "out" },
  165. { "arcd", "anaglyph red cyan dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_DUBOIS}, 0, 0, FLAGS, "out" },
  166. { "arcg", "anaglyph red cyan gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_GRAY}, 0, 0, FLAGS, "out" },
  167. { "arch", "anaglyph red cyan half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_HALF}, 0, 0, FLAGS, "out" },
  168. { "argg", "anaglyph red green gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RG_GRAY}, 0, 0, FLAGS, "out" },
  169. { "aybc", "anaglyph yellow blue color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_COLOR}, 0, 0, FLAGS, "out" },
  170. { "aybd", "anaglyph yellow blue dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_DUBOIS}, 0, 0, FLAGS, "out" },
  171. { "aybg", "anaglyph yellow blue gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_GRAY}, 0, 0, FLAGS, "out" },
  172. { "aybh", "anaglyph yellow blue half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_HALF}, 0, 0, FLAGS, "out" },
  173. { "irl", "interleave rows left first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_LR}, 0, 0, FLAGS, "out" },
  174. { "irr", "interleave rows right first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_RL}, 0, 0, FLAGS, "out" },
  175. { "ml", "mono left", 0, AV_OPT_TYPE_CONST, {.i64=MONO_L}, 0, 0, FLAGS, "out" },
  176. { "mr", "mono right", 0, AV_OPT_TYPE_CONST, {.i64=MONO_R}, 0, 0, FLAGS, "out" },
  177. { "sbs2l", "side by side half width left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR}, 0, 0, FLAGS, "out" },
  178. { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL}, 0, 0, FLAGS, "out" },
  179. { "sbsl", "side by side left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR}, 0, 0, FLAGS, "out" },
  180. { "sbsr", "side by side right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL}, 0, 0, FLAGS, "out" },
  181. {NULL},
  182. };
  183. AVFILTER_DEFINE_CLASS(stereo3d);
  184. static const enum AVPixelFormat anaglyph_pix_fmts[] = { AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE };
  185. static const enum AVPixelFormat other_pix_fmts[] = {
  186. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  187. AV_PIX_FMT_RGB48BE, AV_PIX_FMT_BGR48BE,
  188. AV_PIX_FMT_RGB48LE, AV_PIX_FMT_BGR48LE,
  189. AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_BGRA64BE,
  190. AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_BGRA64LE,
  191. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  192. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  193. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  194. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
  195. AV_PIX_FMT_GBRP,
  196. AV_PIX_FMT_GBRP9BE, AV_PIX_FMT_GBRP9LE,
  197. AV_PIX_FMT_GBRP10BE, AV_PIX_FMT_GBRP10LE,
  198. AV_PIX_FMT_GBRP12BE, AV_PIX_FMT_GBRP12LE,
  199. AV_PIX_FMT_GBRP14BE, AV_PIX_FMT_GBRP14LE,
  200. AV_PIX_FMT_GBRP16BE, AV_PIX_FMT_GBRP16LE,
  201. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
  202. AV_PIX_FMT_YUVJ444P,
  203. AV_PIX_FMT_YUV444P9LE, AV_PIX_FMT_YUVA444P9LE,
  204. AV_PIX_FMT_YUV444P9BE, AV_PIX_FMT_YUVA444P9BE,
  205. AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUVA444P10LE,
  206. AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUVA444P10BE,
  207. AV_PIX_FMT_YUV444P12BE, AV_PIX_FMT_YUV444P12LE,
  208. AV_PIX_FMT_YUV444P14BE, AV_PIX_FMT_YUV444P14LE,
  209. AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUVA444P16LE,
  210. AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUVA444P16BE,
  211. AV_PIX_FMT_NONE
  212. };
  213. static int query_formats(AVFilterContext *ctx)
  214. {
  215. Stereo3DContext *s = ctx->priv;
  216. const enum AVPixelFormat *pix_fmts;
  217. switch (s->out.format) {
  218. case ANAGLYPH_GM_COLOR:
  219. case ANAGLYPH_GM_DUBOIS:
  220. case ANAGLYPH_GM_GRAY:
  221. case ANAGLYPH_GM_HALF:
  222. case ANAGLYPH_RB_GRAY:
  223. case ANAGLYPH_RC_COLOR:
  224. case ANAGLYPH_RC_DUBOIS:
  225. case ANAGLYPH_RC_GRAY:
  226. case ANAGLYPH_RC_HALF:
  227. case ANAGLYPH_RG_GRAY:
  228. case ANAGLYPH_YB_COLOR:
  229. case ANAGLYPH_YB_DUBOIS:
  230. case ANAGLYPH_YB_GRAY:
  231. case ANAGLYPH_YB_HALF:
  232. pix_fmts = anaglyph_pix_fmts;
  233. break;
  234. default:
  235. pix_fmts = other_pix_fmts;
  236. }
  237. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  238. return 0;
  239. }
  240. static int config_output(AVFilterLink *outlink)
  241. {
  242. AVFilterContext *ctx = outlink->src;
  243. AVFilterLink *inlink = ctx->inputs[0];
  244. Stereo3DContext *s = ctx->priv;
  245. AVRational aspect = inlink->sample_aspect_ratio;
  246. AVRational fps = inlink->frame_rate;
  247. AVRational tb = inlink->time_base;
  248. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
  249. int ret;
  250. switch (s->in.format) {
  251. case SIDE_BY_SIDE_2_LR:
  252. case SIDE_BY_SIDE_LR:
  253. case SIDE_BY_SIDE_2_RL:
  254. case SIDE_BY_SIDE_RL:
  255. if (inlink->w & 1) {
  256. av_log(ctx, AV_LOG_ERROR, "width must be even\n");
  257. return AVERROR_INVALIDDATA;
  258. }
  259. break;
  260. case ABOVE_BELOW_2_LR:
  261. case ABOVE_BELOW_LR:
  262. case ABOVE_BELOW_2_RL:
  263. case ABOVE_BELOW_RL:
  264. if (s->out.format == INTERLEAVE_ROWS_LR ||
  265. s->out.format == INTERLEAVE_ROWS_RL) {
  266. if (inlink->h & 3) {
  267. av_log(ctx, AV_LOG_ERROR, "height must be multiple of 4\n");
  268. return AVERROR_INVALIDDATA;
  269. }
  270. }
  271. if (inlink->h & 1) {
  272. av_log(ctx, AV_LOG_ERROR, "height must be even\n");
  273. return AVERROR_INVALIDDATA;
  274. }
  275. break;
  276. }
  277. s->in.width =
  278. s->width = inlink->w;
  279. s->in.height =
  280. s->height = inlink->h;
  281. s->row_step = 1;
  282. s->in.off_lstep =
  283. s->in.off_rstep =
  284. s->in.off_left =
  285. s->in.off_right =
  286. s->in.row_left =
  287. s->in.row_right = 0;
  288. switch (s->in.format) {
  289. case SIDE_BY_SIDE_2_LR:
  290. aspect.num *= 2;
  291. case SIDE_BY_SIDE_LR:
  292. s->width = inlink->w / 2;
  293. s->in.off_right = s->width;
  294. break;
  295. case SIDE_BY_SIDE_2_RL:
  296. aspect.num *= 2;
  297. case SIDE_BY_SIDE_RL:
  298. s->width = inlink->w / 2;
  299. s->in.off_left = s->width;
  300. break;
  301. case ABOVE_BELOW_2_LR:
  302. aspect.den *= 2;
  303. case ABOVE_BELOW_LR:
  304. s->in.row_right =
  305. s->height = inlink->h / 2;
  306. break;
  307. case ABOVE_BELOW_2_RL:
  308. aspect.den *= 2;
  309. case ABOVE_BELOW_RL:
  310. s->in.row_left =
  311. s->height = inlink->h / 2;
  312. break;
  313. case ALTERNATING_RL:
  314. case ALTERNATING_LR:
  315. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  316. fps.den *= 2;
  317. tb.num *= 2;
  318. break;
  319. default:
  320. av_log(ctx, AV_LOG_ERROR, "input format %d is not supported\n", s->in.format);
  321. return AVERROR(EINVAL);
  322. }
  323. s->out.width = s->width;
  324. s->out.height = s->height;
  325. s->out.off_lstep =
  326. s->out.off_rstep =
  327. s->out.off_left =
  328. s->out.off_right =
  329. s->out.row_left =
  330. s->out.row_right = 0;
  331. switch (s->out.format) {
  332. case ANAGLYPH_RB_GRAY:
  333. case ANAGLYPH_RG_GRAY:
  334. case ANAGLYPH_RC_GRAY:
  335. case ANAGLYPH_RC_HALF:
  336. case ANAGLYPH_RC_COLOR:
  337. case ANAGLYPH_RC_DUBOIS:
  338. case ANAGLYPH_GM_GRAY:
  339. case ANAGLYPH_GM_HALF:
  340. case ANAGLYPH_GM_COLOR:
  341. case ANAGLYPH_GM_DUBOIS:
  342. case ANAGLYPH_YB_GRAY:
  343. case ANAGLYPH_YB_HALF:
  344. case ANAGLYPH_YB_COLOR:
  345. case ANAGLYPH_YB_DUBOIS:
  346. memcpy(s->ana_matrix, ana_coeff[s->out.format], sizeof(s->ana_matrix));
  347. break;
  348. case SIDE_BY_SIDE_2_LR:
  349. aspect.den *= 2;
  350. case SIDE_BY_SIDE_LR:
  351. s->out.width = s->width * 2;
  352. s->out.off_right = s->width;
  353. break;
  354. case SIDE_BY_SIDE_2_RL:
  355. aspect.den *= 2;
  356. case SIDE_BY_SIDE_RL:
  357. s->out.width = s->width * 2;
  358. s->out.off_left = s->width;
  359. break;
  360. case ABOVE_BELOW_2_LR:
  361. aspect.num *= 2;
  362. case ABOVE_BELOW_LR:
  363. s->out.height = s->height * 2;
  364. s->out.row_right = s->height;
  365. break;
  366. case ABOVE_BELOW_2_RL:
  367. aspect.num *= 2;
  368. case ABOVE_BELOW_RL:
  369. s->out.height = s->height * 2;
  370. s->out.row_left = s->height;
  371. break;
  372. case INTERLEAVE_ROWS_LR:
  373. s->row_step = 2;
  374. s->height = s->height / 2;
  375. s->out.off_rstep =
  376. s->in.off_rstep = 1;
  377. break;
  378. case INTERLEAVE_ROWS_RL:
  379. s->row_step = 2;
  380. s->height = s->height / 2;
  381. s->out.off_lstep =
  382. s->in.off_lstep = 1;
  383. break;
  384. case MONO_R:
  385. s->in.off_left = s->in.off_right;
  386. s->in.row_left = s->in.row_right;
  387. case MONO_L:
  388. break;
  389. case ALTERNATING_RL:
  390. case ALTERNATING_LR:
  391. fps.num *= 2;
  392. tb.den *= 2;
  393. break;
  394. default:
  395. av_log(ctx, AV_LOG_ERROR, "output format %d is not supported\n", s->out.format);
  396. return AVERROR(EINVAL);
  397. }
  398. outlink->w = s->out.width;
  399. outlink->h = s->out.height;
  400. outlink->frame_rate = fps;
  401. outlink->time_base = tb;
  402. outlink->sample_aspect_ratio = aspect;
  403. if ((ret = av_image_fill_linesizes(s->linesize, outlink->format, s->width)) < 0)
  404. return ret;
  405. s->nb_planes = av_pix_fmt_count_planes(outlink->format);
  406. av_image_fill_max_pixsteps(s->pixstep, NULL, desc);
  407. s->ts_unit = av_q2d(av_inv_q(av_mul_q(outlink->frame_rate, outlink->time_base)));
  408. return 0;
  409. }
  410. static inline uint8_t ana_convert(const int *coeff, uint8_t *left, uint8_t *right)
  411. {
  412. int sum;
  413. sum = coeff[0] * left[0] + coeff[3] * right[0]; //red in
  414. sum += coeff[1] * left[1] + coeff[4] * right[1]; //green in
  415. sum += coeff[2] * left[2] + coeff[5] * right[2]; //blue in
  416. return av_clip_uint8(sum >> 16);
  417. }
  418. static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
  419. {
  420. AVFilterContext *ctx = inlink->dst;
  421. Stereo3DContext *s = ctx->priv;
  422. AVFilterLink *outlink = ctx->outputs[0];
  423. AVFrame *out, *oleft, *oright, *ileft, *iright;
  424. int out_off_left[4], out_off_right[4];
  425. int in_off_left[4], in_off_right[4];
  426. int i;
  427. switch (s->in.format) {
  428. case ALTERNATING_LR:
  429. case ALTERNATING_RL:
  430. if (!s->prev) {
  431. s->prev = inpicref;
  432. return 0;
  433. }
  434. ileft = s->prev;
  435. iright = inpicref;
  436. if (s->in.format == ALTERNATING_RL)
  437. FFSWAP(AVFrame *, ileft, iright);
  438. break;
  439. default:
  440. ileft = iright = inpicref;
  441. };
  442. out = oleft = oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  443. if (!out) {
  444. av_frame_free(&s->prev);
  445. av_frame_free(&inpicref);
  446. return AVERROR(ENOMEM);
  447. }
  448. av_frame_copy_props(out, inpicref);
  449. if (s->out.format == ALTERNATING_LR ||
  450. s->out.format == ALTERNATING_RL) {
  451. oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  452. if (!oright) {
  453. av_frame_free(&oleft);
  454. av_frame_free(&s->prev);
  455. av_frame_free(&inpicref);
  456. return AVERROR(ENOMEM);
  457. }
  458. av_frame_copy_props(oright, inpicref);
  459. }
  460. for (i = 0; i < 4; i++) {
  461. in_off_left[i] = (s->in.row_left + s->in.off_lstep) * ileft->linesize[i] + s->in.off_left * s->pixstep[i];
  462. in_off_right[i] = (s->in.row_right + s->in.off_rstep) * iright->linesize[i] + s->in.off_right * s->pixstep[i];
  463. out_off_left[i] = (s->out.row_left + s->out.off_lstep) * oleft->linesize[i] + s->out.off_left * s->pixstep[i];
  464. out_off_right[i] = (s->out.row_right + s->out.off_rstep) * oright->linesize[i] + s->out.off_right * s->pixstep[i];
  465. }
  466. switch (s->out.format) {
  467. case ALTERNATING_LR:
  468. case ALTERNATING_RL:
  469. case SIDE_BY_SIDE_LR:
  470. case SIDE_BY_SIDE_RL:
  471. case SIDE_BY_SIDE_2_LR:
  472. case SIDE_BY_SIDE_2_RL:
  473. case ABOVE_BELOW_LR:
  474. case ABOVE_BELOW_RL:
  475. case ABOVE_BELOW_2_LR:
  476. case ABOVE_BELOW_2_RL:
  477. case INTERLEAVE_ROWS_LR:
  478. case INTERLEAVE_ROWS_RL:
  479. for (i = 0; i < s->nb_planes; i++) {
  480. av_image_copy_plane(oleft->data[i] + out_off_left[i],
  481. oleft->linesize[i] * s->row_step,
  482. ileft->data[i] + in_off_left[i],
  483. ileft->linesize[i] * s->row_step,
  484. s->linesize[i], s->height);
  485. av_image_copy_plane(oright->data[i] + out_off_right[i],
  486. oright->linesize[i] * s->row_step,
  487. iright->data[i] + in_off_right[i],
  488. iright->linesize[i] * s->row_step,
  489. s->linesize[i], s->height);
  490. }
  491. break;
  492. case MONO_L:
  493. iright = ileft;
  494. case MONO_R:
  495. for (i = 0; i < s->nb_planes; i++) {
  496. av_image_copy_plane(out->data[i], out->linesize[i],
  497. iright->data[i] + in_off_left[i],
  498. iright->linesize[i],
  499. s->linesize[i], s->height);
  500. }
  501. break;
  502. case ANAGLYPH_RB_GRAY:
  503. case ANAGLYPH_RG_GRAY:
  504. case ANAGLYPH_RC_GRAY:
  505. case ANAGLYPH_RC_HALF:
  506. case ANAGLYPH_RC_COLOR:
  507. case ANAGLYPH_RC_DUBOIS:
  508. case ANAGLYPH_GM_GRAY:
  509. case ANAGLYPH_GM_HALF:
  510. case ANAGLYPH_GM_COLOR:
  511. case ANAGLYPH_GM_DUBOIS:
  512. case ANAGLYPH_YB_GRAY:
  513. case ANAGLYPH_YB_HALF:
  514. case ANAGLYPH_YB_COLOR:
  515. case ANAGLYPH_YB_DUBOIS: {
  516. int i, x, y, il, ir, o;
  517. uint8_t *lsrc = ileft->data[0];
  518. uint8_t *rsrc = iright->data[0];
  519. uint8_t *dst = out->data[0];
  520. int out_width = s->out.width;
  521. int *ana_matrix[3];
  522. for (i = 0; i < 3; i++)
  523. ana_matrix[i] = s->ana_matrix[i];
  524. for (y = 0; y < s->out.height; y++) {
  525. o = out->linesize[0] * y;
  526. il = in_off_left[0] + y * ileft->linesize[0];
  527. ir = in_off_right[0] + y * iright->linesize[0];
  528. for (x = 0; x < out_width; x++, il += 3, ir += 3, o+= 3) {
  529. dst[o ] = ana_convert(ana_matrix[0], lsrc + il, rsrc + ir);
  530. dst[o + 1] = ana_convert(ana_matrix[1], lsrc + il, rsrc + ir);
  531. dst[o + 2] = ana_convert(ana_matrix[2], lsrc + il, rsrc + ir);
  532. }
  533. }
  534. break;
  535. }
  536. default:
  537. av_assert0(0);
  538. }
  539. av_frame_free(&inpicref);
  540. av_frame_free(&s->prev);
  541. if (oright != oleft) {
  542. if (s->out.format == ALTERNATING_LR)
  543. FFSWAP(AVFrame *, oleft, oright);
  544. oright->pts = outlink->frame_count * s->ts_unit;
  545. ff_filter_frame(outlink, oright);
  546. out = oleft;
  547. oleft->pts = outlink->frame_count * s->ts_unit;
  548. } else if (s->in.format == ALTERNATING_LR ||
  549. s->in.format == ALTERNATING_RL) {
  550. out->pts = outlink->frame_count * s->ts_unit;
  551. }
  552. return ff_filter_frame(outlink, out);
  553. }
  554. static av_cold void uninit(AVFilterContext *ctx)
  555. {
  556. Stereo3DContext *s = ctx->priv;
  557. av_frame_free(&s->prev);
  558. }
  559. static const AVFilterPad stereo3d_inputs[] = {
  560. {
  561. .name = "default",
  562. .type = AVMEDIA_TYPE_VIDEO,
  563. .filter_frame = filter_frame,
  564. },
  565. { NULL }
  566. };
  567. static const AVFilterPad stereo3d_outputs[] = {
  568. {
  569. .name = "default",
  570. .type = AVMEDIA_TYPE_VIDEO,
  571. .config_props = config_output,
  572. },
  573. { NULL }
  574. };
  575. AVFilter avfilter_vf_stereo3d = {
  576. .name = "stereo3d",
  577. .description = NULL_IF_CONFIG_SMALL("Convert video stereoscopic 3D view."),
  578. .priv_size = sizeof(Stereo3DContext),
  579. .uninit = uninit,
  580. .query_formats = query_formats,
  581. .inputs = stereo3d_inputs,
  582. .outputs = stereo3d_outputs,
  583. .priv_class = &stereo3d_class,
  584. };