vf_stereo3d.c 45 KB

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