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.

657 lines
26KB

  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 pheight[4];
  135. int hsub, vsub;
  136. int pixstep[4];
  137. AVFrame *prev;
  138. double ts_unit;
  139. } Stereo3DContext;
  140. #define OFFSET(x) offsetof(Stereo3DContext, x)
  141. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  142. static const AVOption stereo3d_options[] = {
  143. { "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"},
  144. { "ab2l", "above below half height left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR}, 0, 0, FLAGS, "in" },
  145. { "ab2r", "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL}, 0, 0, FLAGS, "in" },
  146. { "abl", "above below left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR}, 0, 0, FLAGS, "in" },
  147. { "abr", "above below right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL}, 0, 0, FLAGS, "in" },
  148. { "al", "alternating frames left first", 0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_LR}, 0, 0, FLAGS, "in" },
  149. { "ar", "alternating frames right first", 0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_RL}, 0, 0, FLAGS, "in" },
  150. { "sbs2l", "side by side half width left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR}, 0, 0, FLAGS, "in" },
  151. { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL}, 0, 0, FLAGS, "in" },
  152. { "sbsl", "side by side left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR}, 0, 0, FLAGS, "in" },
  153. { "sbsr", "side by side right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL}, 0, 0, FLAGS, "in" },
  154. { "out", "set output format", OFFSET(out.format), AV_OPT_TYPE_INT, {.i64=ANAGLYPH_RC_DUBOIS}, 0, STEREO_CODE_COUNT-1, FLAGS, "out"},
  155. { "ab2l", "above below half height left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR}, 0, 0, FLAGS, "out" },
  156. { "ab2r", "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL}, 0, 0, FLAGS, "out" },
  157. { "abl", "above below left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR}, 0, 0, FLAGS, "out" },
  158. { "abr", "above below right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL}, 0, 0, FLAGS, "out" },
  159. { "agmc", "anaglyph green magenta color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_COLOR}, 0, 0, FLAGS, "out" },
  160. { "agmd", "anaglyph green magenta dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_DUBOIS}, 0, 0, FLAGS, "out" },
  161. { "agmg", "anaglyph green magenta gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_GRAY}, 0, 0, FLAGS, "out" },
  162. { "agmh", "anaglyph green magenta half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_HALF}, 0, 0, FLAGS, "out" },
  163. { "al", "alternating frames left first", 0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_LR}, 0, 0, FLAGS, "out" },
  164. { "ar", "alternating frames right first", 0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_RL}, 0, 0, FLAGS, "out" },
  165. { "arbg", "anaglyph red blue gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RB_GRAY}, 0, 0, FLAGS, "out" },
  166. { "arcc", "anaglyph red cyan color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_COLOR}, 0, 0, FLAGS, "out" },
  167. { "arcd", "anaglyph red cyan dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_DUBOIS}, 0, 0, FLAGS, "out" },
  168. { "arcg", "anaglyph red cyan gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_GRAY}, 0, 0, FLAGS, "out" },
  169. { "arch", "anaglyph red cyan half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_HALF}, 0, 0, FLAGS, "out" },
  170. { "argg", "anaglyph red green gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RG_GRAY}, 0, 0, FLAGS, "out" },
  171. { "aybc", "anaglyph yellow blue color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_COLOR}, 0, 0, FLAGS, "out" },
  172. { "aybd", "anaglyph yellow blue dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_DUBOIS}, 0, 0, FLAGS, "out" },
  173. { "aybg", "anaglyph yellow blue gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_GRAY}, 0, 0, FLAGS, "out" },
  174. { "aybh", "anaglyph yellow blue half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_HALF}, 0, 0, FLAGS, "out" },
  175. { "irl", "interleave rows left first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_LR}, 0, 0, FLAGS, "out" },
  176. { "irr", "interleave rows right first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_RL}, 0, 0, FLAGS, "out" },
  177. { "ml", "mono left", 0, AV_OPT_TYPE_CONST, {.i64=MONO_L}, 0, 0, FLAGS, "out" },
  178. { "mr", "mono right", 0, AV_OPT_TYPE_CONST, {.i64=MONO_R}, 0, 0, FLAGS, "out" },
  179. { "sbs2l", "side by side half width left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR}, 0, 0, FLAGS, "out" },
  180. { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL}, 0, 0, FLAGS, "out" },
  181. { "sbsl", "side by side left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR}, 0, 0, FLAGS, "out" },
  182. { "sbsr", "side by side right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL}, 0, 0, FLAGS, "out" },
  183. {NULL},
  184. };
  185. AVFILTER_DEFINE_CLASS(stereo3d);
  186. static const enum AVPixelFormat anaglyph_pix_fmts[] = { AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE };
  187. static const enum AVPixelFormat other_pix_fmts[] = {
  188. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  189. AV_PIX_FMT_RGB48BE, AV_PIX_FMT_BGR48BE,
  190. AV_PIX_FMT_RGB48LE, AV_PIX_FMT_BGR48LE,
  191. AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_BGRA64BE,
  192. AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_BGRA64LE,
  193. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  194. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  195. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  196. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
  197. AV_PIX_FMT_GBRP,
  198. AV_PIX_FMT_GBRP9BE, AV_PIX_FMT_GBRP9LE,
  199. AV_PIX_FMT_GBRP10BE, AV_PIX_FMT_GBRP10LE,
  200. AV_PIX_FMT_GBRP12BE, AV_PIX_FMT_GBRP12LE,
  201. AV_PIX_FMT_GBRP14BE, AV_PIX_FMT_GBRP14LE,
  202. AV_PIX_FMT_GBRP16BE, AV_PIX_FMT_GBRP16LE,
  203. AV_PIX_FMT_YUV410P,
  204. AV_PIX_FMT_YUV411P,
  205. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
  206. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P,
  207. AV_PIX_FMT_YUV440P,
  208. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
  209. AV_PIX_FMT_YUVJ411P,
  210. AV_PIX_FMT_YUVJ420P,
  211. AV_PIX_FMT_YUVJ422P,
  212. AV_PIX_FMT_YUVJ440P,
  213. AV_PIX_FMT_YUVJ444P,
  214. AV_PIX_FMT_YUV420P9LE, AV_PIX_FMT_YUVA420P9LE,
  215. AV_PIX_FMT_YUV420P9BE, AV_PIX_FMT_YUVA420P9BE,
  216. AV_PIX_FMT_YUV422P9LE, AV_PIX_FMT_YUVA422P9LE,
  217. AV_PIX_FMT_YUV422P9BE, AV_PIX_FMT_YUVA422P9BE,
  218. AV_PIX_FMT_YUV444P9LE, AV_PIX_FMT_YUVA444P9LE,
  219. AV_PIX_FMT_YUV444P9BE, AV_PIX_FMT_YUVA444P9BE,
  220. AV_PIX_FMT_YUV420P10LE, AV_PIX_FMT_YUVA420P10LE,
  221. AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUVA420P10BE,
  222. AV_PIX_FMT_YUV422P10LE, AV_PIX_FMT_YUVA422P10LE,
  223. AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUVA422P10BE,
  224. AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUVA444P10LE,
  225. AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUVA444P10BE,
  226. AV_PIX_FMT_YUV420P12BE, AV_PIX_FMT_YUV420P12LE,
  227. AV_PIX_FMT_YUV422P12BE, AV_PIX_FMT_YUV422P12LE,
  228. AV_PIX_FMT_YUV444P12BE, AV_PIX_FMT_YUV444P12LE,
  229. AV_PIX_FMT_YUV420P14BE, AV_PIX_FMT_YUV420P14LE,
  230. AV_PIX_FMT_YUV422P14BE, AV_PIX_FMT_YUV422P14LE,
  231. AV_PIX_FMT_YUV444P14BE, AV_PIX_FMT_YUV444P14LE,
  232. AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUVA420P16LE,
  233. AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUVA420P16BE,
  234. AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUVA422P16LE,
  235. AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUVA422P16BE,
  236. AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUVA444P16LE,
  237. AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUVA444P16BE,
  238. AV_PIX_FMT_NONE
  239. };
  240. static int query_formats(AVFilterContext *ctx)
  241. {
  242. Stereo3DContext *s = ctx->priv;
  243. const enum AVPixelFormat *pix_fmts;
  244. switch (s->out.format) {
  245. case ANAGLYPH_GM_COLOR:
  246. case ANAGLYPH_GM_DUBOIS:
  247. case ANAGLYPH_GM_GRAY:
  248. case ANAGLYPH_GM_HALF:
  249. case ANAGLYPH_RB_GRAY:
  250. case ANAGLYPH_RC_COLOR:
  251. case ANAGLYPH_RC_DUBOIS:
  252. case ANAGLYPH_RC_GRAY:
  253. case ANAGLYPH_RC_HALF:
  254. case ANAGLYPH_RG_GRAY:
  255. case ANAGLYPH_YB_COLOR:
  256. case ANAGLYPH_YB_DUBOIS:
  257. case ANAGLYPH_YB_GRAY:
  258. case ANAGLYPH_YB_HALF:
  259. pix_fmts = anaglyph_pix_fmts;
  260. break;
  261. default:
  262. pix_fmts = other_pix_fmts;
  263. }
  264. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  265. return 0;
  266. }
  267. static int config_output(AVFilterLink *outlink)
  268. {
  269. AVFilterContext *ctx = outlink->src;
  270. AVFilterLink *inlink = ctx->inputs[0];
  271. Stereo3DContext *s = ctx->priv;
  272. AVRational aspect = inlink->sample_aspect_ratio;
  273. AVRational fps = inlink->frame_rate;
  274. AVRational tb = inlink->time_base;
  275. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
  276. int ret;
  277. switch (s->in.format) {
  278. case SIDE_BY_SIDE_2_LR:
  279. case SIDE_BY_SIDE_LR:
  280. case SIDE_BY_SIDE_2_RL:
  281. case SIDE_BY_SIDE_RL:
  282. if (inlink->w & 1) {
  283. av_log(ctx, AV_LOG_ERROR, "width must be even\n");
  284. return AVERROR_INVALIDDATA;
  285. }
  286. break;
  287. case ABOVE_BELOW_2_LR:
  288. case ABOVE_BELOW_LR:
  289. case ABOVE_BELOW_2_RL:
  290. case ABOVE_BELOW_RL:
  291. if (s->out.format == INTERLEAVE_ROWS_LR ||
  292. s->out.format == INTERLEAVE_ROWS_RL) {
  293. if (inlink->h & 3) {
  294. av_log(ctx, AV_LOG_ERROR, "height must be multiple of 4\n");
  295. return AVERROR_INVALIDDATA;
  296. }
  297. }
  298. if (inlink->h & 1) {
  299. av_log(ctx, AV_LOG_ERROR, "height must be even\n");
  300. return AVERROR_INVALIDDATA;
  301. }
  302. break;
  303. }
  304. s->in.width =
  305. s->width = inlink->w;
  306. s->in.height =
  307. s->height = inlink->h;
  308. s->row_step = 1;
  309. s->in.off_lstep =
  310. s->in.off_rstep =
  311. s->in.off_left =
  312. s->in.off_right =
  313. s->in.row_left =
  314. s->in.row_right = 0;
  315. switch (s->in.format) {
  316. case SIDE_BY_SIDE_2_LR:
  317. aspect.num *= 2;
  318. case SIDE_BY_SIDE_LR:
  319. s->width = inlink->w / 2;
  320. s->in.off_right = s->width;
  321. break;
  322. case SIDE_BY_SIDE_2_RL:
  323. aspect.num *= 2;
  324. case SIDE_BY_SIDE_RL:
  325. s->width = inlink->w / 2;
  326. s->in.off_left = s->width;
  327. break;
  328. case ABOVE_BELOW_2_LR:
  329. aspect.den *= 2;
  330. case ABOVE_BELOW_LR:
  331. s->in.row_right =
  332. s->height = inlink->h / 2;
  333. break;
  334. case ABOVE_BELOW_2_RL:
  335. aspect.den *= 2;
  336. case ABOVE_BELOW_RL:
  337. s->in.row_left =
  338. s->height = inlink->h / 2;
  339. break;
  340. case ALTERNATING_RL:
  341. case ALTERNATING_LR:
  342. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  343. fps.den *= 2;
  344. tb.num *= 2;
  345. break;
  346. default:
  347. av_log(ctx, AV_LOG_ERROR, "input format %d is not supported\n", s->in.format);
  348. return AVERROR(EINVAL);
  349. }
  350. s->out.width = s->width;
  351. s->out.height = s->height;
  352. s->out.off_lstep =
  353. s->out.off_rstep =
  354. s->out.off_left =
  355. s->out.off_right =
  356. s->out.row_left =
  357. s->out.row_right = 0;
  358. switch (s->out.format) {
  359. case ANAGLYPH_RB_GRAY:
  360. case ANAGLYPH_RG_GRAY:
  361. case ANAGLYPH_RC_GRAY:
  362. case ANAGLYPH_RC_HALF:
  363. case ANAGLYPH_RC_COLOR:
  364. case ANAGLYPH_RC_DUBOIS:
  365. case ANAGLYPH_GM_GRAY:
  366. case ANAGLYPH_GM_HALF:
  367. case ANAGLYPH_GM_COLOR:
  368. case ANAGLYPH_GM_DUBOIS:
  369. case ANAGLYPH_YB_GRAY:
  370. case ANAGLYPH_YB_HALF:
  371. case ANAGLYPH_YB_COLOR:
  372. case ANAGLYPH_YB_DUBOIS:
  373. memcpy(s->ana_matrix, ana_coeff[s->out.format], sizeof(s->ana_matrix));
  374. break;
  375. case SIDE_BY_SIDE_2_LR:
  376. aspect.den *= 2;
  377. case SIDE_BY_SIDE_LR:
  378. s->out.width = s->width * 2;
  379. s->out.off_right = s->width;
  380. break;
  381. case SIDE_BY_SIDE_2_RL:
  382. aspect.den *= 2;
  383. case SIDE_BY_SIDE_RL:
  384. s->out.width = s->width * 2;
  385. s->out.off_left = s->width;
  386. break;
  387. case ABOVE_BELOW_2_LR:
  388. aspect.num *= 2;
  389. case ABOVE_BELOW_LR:
  390. s->out.height = s->height * 2;
  391. s->out.row_right = s->height;
  392. break;
  393. case ABOVE_BELOW_2_RL:
  394. aspect.num *= 2;
  395. case ABOVE_BELOW_RL:
  396. s->out.height = s->height * 2;
  397. s->out.row_left = s->height;
  398. break;
  399. case INTERLEAVE_ROWS_LR:
  400. s->row_step = 2;
  401. s->height = s->height / 2;
  402. s->out.off_rstep =
  403. s->in.off_rstep = 1;
  404. break;
  405. case INTERLEAVE_ROWS_RL:
  406. s->row_step = 2;
  407. s->height = s->height / 2;
  408. s->out.off_lstep =
  409. s->in.off_lstep = 1;
  410. break;
  411. case MONO_R:
  412. s->in.off_left = s->in.off_right;
  413. s->in.row_left = s->in.row_right;
  414. case MONO_L:
  415. break;
  416. case ALTERNATING_RL:
  417. case ALTERNATING_LR:
  418. fps.num *= 2;
  419. tb.den *= 2;
  420. break;
  421. default:
  422. av_log(ctx, AV_LOG_ERROR, "output format %d is not supported\n", s->out.format);
  423. return AVERROR(EINVAL);
  424. }
  425. outlink->w = s->out.width;
  426. outlink->h = s->out.height;
  427. outlink->frame_rate = fps;
  428. outlink->time_base = tb;
  429. outlink->sample_aspect_ratio = aspect;
  430. if ((ret = av_image_fill_linesizes(s->linesize, outlink->format, s->width)) < 0)
  431. return ret;
  432. s->nb_planes = av_pix_fmt_count_planes(outlink->format);
  433. av_image_fill_max_pixsteps(s->pixstep, NULL, desc);
  434. s->ts_unit = av_q2d(av_inv_q(av_mul_q(outlink->frame_rate, outlink->time_base)));
  435. s->pheight[1] = s->pheight[2] = FF_CEIL_RSHIFT(s->height, desc->log2_chroma_h);
  436. s->pheight[0] = s->pheight[3] = s->height;
  437. s->hsub = desc->log2_chroma_w;
  438. s->vsub = desc->log2_chroma_h;
  439. return 0;
  440. }
  441. static inline uint8_t ana_convert(const int *coeff, uint8_t *left, uint8_t *right)
  442. {
  443. int sum;
  444. sum = coeff[0] * left[0] + coeff[3] * right[0]; //red in
  445. sum += coeff[1] * left[1] + coeff[4] * right[1]; //green in
  446. sum += coeff[2] * left[2] + coeff[5] * right[2]; //blue in
  447. return av_clip_uint8(sum >> 16);
  448. }
  449. static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
  450. {
  451. AVFilterContext *ctx = inlink->dst;
  452. Stereo3DContext *s = ctx->priv;
  453. AVFilterLink *outlink = ctx->outputs[0];
  454. AVFrame *out, *oleft, *oright, *ileft, *iright;
  455. int out_off_left[4], out_off_right[4];
  456. int in_off_left[4], in_off_right[4];
  457. int i;
  458. switch (s->in.format) {
  459. case ALTERNATING_LR:
  460. case ALTERNATING_RL:
  461. if (!s->prev) {
  462. s->prev = inpicref;
  463. return 0;
  464. }
  465. ileft = s->prev;
  466. iright = inpicref;
  467. if (s->in.format == ALTERNATING_RL)
  468. FFSWAP(AVFrame *, ileft, iright);
  469. break;
  470. default:
  471. ileft = iright = inpicref;
  472. };
  473. out = oleft = oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  474. if (!out) {
  475. av_frame_free(&s->prev);
  476. av_frame_free(&inpicref);
  477. return AVERROR(ENOMEM);
  478. }
  479. av_frame_copy_props(out, inpicref);
  480. if (s->out.format == ALTERNATING_LR ||
  481. s->out.format == ALTERNATING_RL) {
  482. oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  483. if (!oright) {
  484. av_frame_free(&oleft);
  485. av_frame_free(&s->prev);
  486. av_frame_free(&inpicref);
  487. return AVERROR(ENOMEM);
  488. }
  489. av_frame_copy_props(oright, inpicref);
  490. }
  491. for (i = 0; i < 4; i++) {
  492. int hsub = i == 1 || i == 2 ? s->hsub : 0;
  493. int vsub = i == 1 || i == 2 ? s->vsub : 0;
  494. in_off_left[i] = (FF_CEIL_RSHIFT(s->in.row_left, vsub) + s->in.off_lstep) * ileft->linesize[i] + FF_CEIL_RSHIFT(s->in.off_left * s->pixstep[i], hsub);
  495. in_off_right[i] = (FF_CEIL_RSHIFT(s->in.row_right, vsub) + s->in.off_rstep) * iright->linesize[i] + FF_CEIL_RSHIFT(s->in.off_right * s->pixstep[i], hsub);
  496. out_off_left[i] = (FF_CEIL_RSHIFT(s->out.row_left, vsub) + s->out.off_lstep) * oleft->linesize[i] + FF_CEIL_RSHIFT(s->out.off_left * s->pixstep[i], hsub);
  497. out_off_right[i] = (FF_CEIL_RSHIFT(s->out.row_right, vsub) + s->out.off_rstep) * oright->linesize[i] + FF_CEIL_RSHIFT(s->out.off_right * s->pixstep[i], hsub);
  498. }
  499. switch (s->out.format) {
  500. case ALTERNATING_LR:
  501. case ALTERNATING_RL:
  502. case SIDE_BY_SIDE_LR:
  503. case SIDE_BY_SIDE_RL:
  504. case SIDE_BY_SIDE_2_LR:
  505. case SIDE_BY_SIDE_2_RL:
  506. case ABOVE_BELOW_LR:
  507. case ABOVE_BELOW_RL:
  508. case ABOVE_BELOW_2_LR:
  509. case ABOVE_BELOW_2_RL:
  510. case INTERLEAVE_ROWS_LR:
  511. case INTERLEAVE_ROWS_RL:
  512. for (i = 0; i < s->nb_planes; i++) {
  513. av_image_copy_plane(oleft->data[i] + out_off_left[i],
  514. oleft->linesize[i] * s->row_step,
  515. ileft->data[i] + in_off_left[i],
  516. ileft->linesize[i] * s->row_step,
  517. s->linesize[i], s->pheight[i]);
  518. av_image_copy_plane(oright->data[i] + out_off_right[i],
  519. oright->linesize[i] * s->row_step,
  520. iright->data[i] + in_off_right[i],
  521. iright->linesize[i] * s->row_step,
  522. s->linesize[i], s->pheight[i]);
  523. }
  524. break;
  525. case MONO_L:
  526. iright = ileft;
  527. case MONO_R:
  528. for (i = 0; i < s->nb_planes; i++) {
  529. av_image_copy_plane(out->data[i], out->linesize[i],
  530. iright->data[i] + in_off_left[i],
  531. iright->linesize[i],
  532. s->linesize[i], s->pheight[i]);
  533. }
  534. break;
  535. case ANAGLYPH_RB_GRAY:
  536. case ANAGLYPH_RG_GRAY:
  537. case ANAGLYPH_RC_GRAY:
  538. case ANAGLYPH_RC_HALF:
  539. case ANAGLYPH_RC_COLOR:
  540. case ANAGLYPH_RC_DUBOIS:
  541. case ANAGLYPH_GM_GRAY:
  542. case ANAGLYPH_GM_HALF:
  543. case ANAGLYPH_GM_COLOR:
  544. case ANAGLYPH_GM_DUBOIS:
  545. case ANAGLYPH_YB_GRAY:
  546. case ANAGLYPH_YB_HALF:
  547. case ANAGLYPH_YB_COLOR:
  548. case ANAGLYPH_YB_DUBOIS: {
  549. int i, x, y, il, ir, o;
  550. uint8_t *lsrc = ileft->data[0];
  551. uint8_t *rsrc = iright->data[0];
  552. uint8_t *dst = out->data[0];
  553. int out_width = s->out.width;
  554. int *ana_matrix[3];
  555. for (i = 0; i < 3; i++)
  556. ana_matrix[i] = s->ana_matrix[i];
  557. for (y = 0; y < s->out.height; y++) {
  558. o = out->linesize[0] * y;
  559. il = in_off_left[0] + y * ileft->linesize[0];
  560. ir = in_off_right[0] + y * iright->linesize[0];
  561. for (x = 0; x < out_width; x++, il += 3, ir += 3, o+= 3) {
  562. dst[o ] = ana_convert(ana_matrix[0], lsrc + il, rsrc + ir);
  563. dst[o + 1] = ana_convert(ana_matrix[1], lsrc + il, rsrc + ir);
  564. dst[o + 2] = ana_convert(ana_matrix[2], lsrc + il, rsrc + ir);
  565. }
  566. }
  567. break;
  568. }
  569. default:
  570. av_assert0(0);
  571. }
  572. av_frame_free(&inpicref);
  573. av_frame_free(&s->prev);
  574. if (oright != oleft) {
  575. if (s->out.format == ALTERNATING_LR)
  576. FFSWAP(AVFrame *, oleft, oright);
  577. oright->pts = outlink->frame_count * s->ts_unit;
  578. ff_filter_frame(outlink, oright);
  579. out = oleft;
  580. oleft->pts = outlink->frame_count * s->ts_unit;
  581. } else if (s->in.format == ALTERNATING_LR ||
  582. s->in.format == ALTERNATING_RL) {
  583. out->pts = outlink->frame_count * s->ts_unit;
  584. }
  585. return ff_filter_frame(outlink, out);
  586. }
  587. static av_cold void uninit(AVFilterContext *ctx)
  588. {
  589. Stereo3DContext *s = ctx->priv;
  590. av_frame_free(&s->prev);
  591. }
  592. static const AVFilterPad stereo3d_inputs[] = {
  593. {
  594. .name = "default",
  595. .type = AVMEDIA_TYPE_VIDEO,
  596. .filter_frame = filter_frame,
  597. },
  598. { NULL }
  599. };
  600. static const AVFilterPad stereo3d_outputs[] = {
  601. {
  602. .name = "default",
  603. .type = AVMEDIA_TYPE_VIDEO,
  604. .config_props = config_output,
  605. },
  606. { NULL }
  607. };
  608. AVFilter avfilter_vf_stereo3d = {
  609. .name = "stereo3d",
  610. .description = NULL_IF_CONFIG_SMALL("Convert video stereoscopic 3D view."),
  611. .priv_size = sizeof(Stereo3DContext),
  612. .uninit = uninit,
  613. .query_formats = query_formats,
  614. .inputs = stereo3d_inputs,
  615. .outputs = stereo3d_outputs,
  616. .priv_class = &stereo3d_class,
  617. };