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.

547 lines
21KB

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