vf_stereo3d.c 26 KB

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