ImfTimeCode.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. //-----------------------------------------------------------------------------
  35. //
  36. // class TimeCode
  37. //
  38. //-----------------------------------------------------------------------------
  39. #include <ImfTimeCode.h>
  40. #include "Iex.h"
  41. #include "ImfNamespace.h"
  42. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  43. TimeCode::TimeCode ()
  44. {
  45. _time = 0;
  46. _user = 0;
  47. }
  48. TimeCode::TimeCode
  49. (int hours,
  50. int minutes,
  51. int seconds,
  52. int frame,
  53. bool dropFrame,
  54. bool colorFrame,
  55. bool fieldPhase,
  56. bool bgf0,
  57. bool bgf1,
  58. bool bgf2,
  59. int binaryGroup1,
  60. int binaryGroup2,
  61. int binaryGroup3,
  62. int binaryGroup4,
  63. int binaryGroup5,
  64. int binaryGroup6,
  65. int binaryGroup7,
  66. int binaryGroup8)
  67. {
  68. setHours (hours);
  69. setMinutes (minutes);
  70. setSeconds (seconds);
  71. setFrame (frame);
  72. setDropFrame (dropFrame);
  73. setColorFrame (colorFrame);
  74. setFieldPhase (fieldPhase);
  75. setBgf0 (bgf0);
  76. setBgf1 (bgf1);
  77. setBgf2 (bgf2);
  78. setBinaryGroup (1, binaryGroup1);
  79. setBinaryGroup (2, binaryGroup2);
  80. setBinaryGroup (3, binaryGroup3);
  81. setBinaryGroup (4, binaryGroup4);
  82. setBinaryGroup (5, binaryGroup5);
  83. setBinaryGroup (6, binaryGroup6);
  84. setBinaryGroup (7, binaryGroup7);
  85. setBinaryGroup (8, binaryGroup8);
  86. }
  87. TimeCode::TimeCode
  88. (unsigned int timeAndFlags,
  89. unsigned int userData,
  90. Packing packing)
  91. {
  92. setTimeAndFlags (timeAndFlags, packing);
  93. setUserData (userData);
  94. }
  95. TimeCode::TimeCode (const TimeCode &other)
  96. {
  97. _time = other._time;
  98. _user = other._user;
  99. }
  100. TimeCode &
  101. TimeCode::operator = (const TimeCode &other)
  102. {
  103. _time = other._time;
  104. _user = other._user;
  105. return *this;
  106. }
  107. bool
  108. TimeCode::operator == (const TimeCode & c) const
  109. {
  110. return (_time == c._time && _user == c._user);
  111. }
  112. bool
  113. TimeCode::operator != (const TimeCode & c) const
  114. {
  115. return (_time != c._time || _user != c._user);
  116. }
  117. namespace {
  118. unsigned int
  119. bitField (unsigned int value, int minBit, int maxBit)
  120. {
  121. int shift = minBit;
  122. unsigned int mask = (~(~0U << (maxBit - minBit + 1)) << minBit);
  123. return (value & mask) >> shift;
  124. }
  125. void
  126. setBitField (unsigned int &value, int minBit, int maxBit, unsigned int field)
  127. {
  128. int shift = minBit;
  129. unsigned int mask = (~(~0U << (maxBit - minBit + 1)) << minBit);
  130. value = ((value & ~mask) | ((field << shift) & mask));
  131. }
  132. int
  133. bcdToBinary (unsigned int bcd)
  134. {
  135. return int ((bcd & 0x0f) + 10 * ((bcd >> 4) & 0x0f));
  136. }
  137. unsigned int
  138. binaryToBcd (int binary)
  139. {
  140. int units = binary % 10;
  141. int tens = (binary / 10) % 10;
  142. return (unsigned int) (units | (tens << 4));
  143. }
  144. } // namespace
  145. int
  146. TimeCode::hours () const
  147. {
  148. return bcdToBinary (bitField (_time, 24, 29));
  149. }
  150. void
  151. TimeCode::setHours (int value)
  152. {
  153. if (value < 0 || value > 23)
  154. throw IEX_NAMESPACE::ArgExc ("Cannot set hours field in time code. "
  155. "New value is out of range.");
  156. setBitField (_time, 24, 29, binaryToBcd (value));
  157. }
  158. int
  159. TimeCode::minutes () const
  160. {
  161. return bcdToBinary (bitField (_time, 16, 22));
  162. }
  163. void
  164. TimeCode::setMinutes (int value)
  165. {
  166. if (value < 0 || value > 59)
  167. throw IEX_NAMESPACE::ArgExc ("Cannot set minutes field in time code. "
  168. "New value is out of range.");
  169. setBitField (_time, 16, 22, binaryToBcd (value));
  170. }
  171. int
  172. TimeCode::seconds () const
  173. {
  174. return bcdToBinary (bitField (_time, 8, 14));
  175. }
  176. void
  177. TimeCode::setSeconds (int value)
  178. {
  179. if (value < 0 || value > 59)
  180. throw IEX_NAMESPACE::ArgExc ("Cannot set seconds field in time code. "
  181. "New value is out of range.");
  182. setBitField (_time, 8, 14, binaryToBcd (value));
  183. }
  184. int
  185. TimeCode::frame () const
  186. {
  187. return bcdToBinary (bitField (_time, 0, 5));
  188. }
  189. void
  190. TimeCode::setFrame (int value)
  191. {
  192. if (value < 0 || value > 59)
  193. throw IEX_NAMESPACE::ArgExc ("Cannot set frame field in time code. "
  194. "New value is out of range.");
  195. setBitField (_time, 0, 5, binaryToBcd (value));
  196. }
  197. bool
  198. TimeCode::dropFrame () const
  199. {
  200. return !!bitField (_time, 6, 6);
  201. }
  202. void
  203. TimeCode::setDropFrame (bool value)
  204. {
  205. setBitField (_time, 6, 6, (unsigned int) !!value);
  206. }
  207. bool
  208. TimeCode::colorFrame () const
  209. {
  210. return !!bitField (_time, 7, 7);
  211. }
  212. void
  213. TimeCode::setColorFrame (bool value)
  214. {
  215. setBitField (_time, 7, 7, (unsigned int) !!value);
  216. }
  217. bool
  218. TimeCode::fieldPhase () const
  219. {
  220. return !!bitField (_time, 15, 15);
  221. }
  222. void
  223. TimeCode::setFieldPhase (bool value)
  224. {
  225. setBitField (_time, 15, 15, (unsigned int) !!value);
  226. }
  227. bool
  228. TimeCode::bgf0 () const
  229. {
  230. return !!bitField (_time, 23, 23);
  231. }
  232. void
  233. TimeCode::setBgf0 (bool value)
  234. {
  235. setBitField (_time, 23, 23, (unsigned int) !!value);
  236. }
  237. bool
  238. TimeCode::bgf1 () const
  239. {
  240. return!!bitField (_time, 30, 30);
  241. }
  242. void
  243. TimeCode::setBgf1 (bool value)
  244. {
  245. setBitField (_time, 30, 30, (unsigned int) !!value);
  246. }
  247. bool
  248. TimeCode::bgf2 () const
  249. {
  250. return !!bitField (_time, 31, 31);
  251. }
  252. void
  253. TimeCode::setBgf2 (bool value)
  254. {
  255. setBitField (_time, 31, 31, (unsigned int) !!value);
  256. }
  257. int
  258. TimeCode::binaryGroup (int group) const
  259. {
  260. if (group < 1 || group > 8)
  261. throw IEX_NAMESPACE::ArgExc ("Cannot extract binary group from time code "
  262. "user data. Group number is out of range.");
  263. int minBit = 4 * (group - 1);
  264. int maxBit = minBit + 3;
  265. return int (bitField (_user, minBit, maxBit));
  266. }
  267. void
  268. TimeCode::setBinaryGroup (int group, int value)
  269. {
  270. if (group < 1 || group > 8)
  271. throw IEX_NAMESPACE::ArgExc ("Cannot extract binary group from time code "
  272. "user data. Group number is out of range.");
  273. int minBit = 4 * (group - 1);
  274. int maxBit = minBit + 3;
  275. setBitField (_user, minBit, maxBit, (unsigned int) value);
  276. }
  277. unsigned int
  278. TimeCode::timeAndFlags (Packing packing) const
  279. {
  280. if (packing == TV50_PACKING)
  281. {
  282. unsigned int t = _time;
  283. t &= ~((1 << 6) | (1 << 15) | (1 << 23) | (1 << 30) | (1 << 31));
  284. t |= ((unsigned int) bgf0() << 15);
  285. t |= ((unsigned int) bgf2() << 23);
  286. t |= ((unsigned int) bgf1() << 30);
  287. t |= ((unsigned int) fieldPhase() << 31);
  288. return t;
  289. }
  290. if (packing == FILM24_PACKING)
  291. {
  292. return _time & ~((1 << 6) | (1 << 7));
  293. }
  294. else // packing == TV60_PACKING
  295. {
  296. return _time;
  297. }
  298. }
  299. void
  300. TimeCode::setTimeAndFlags (unsigned int value, Packing packing)
  301. {
  302. if (packing == TV50_PACKING)
  303. {
  304. _time = value &
  305. ~((1 << 6) | (1 << 15) | (1 << 23) | (1 << 30) | (1 << 31));
  306. if (value & (1 << 15))
  307. setBgf0 (true);
  308. if (value & (1 << 23))
  309. setBgf2 (true);
  310. if (value & (1 << 30))
  311. setBgf1 (true);
  312. if (value & (1 << 31))
  313. setFieldPhase (true);
  314. }
  315. else if (packing == FILM24_PACKING)
  316. {
  317. _time = value & ~((1 << 6) | (1 << 7));
  318. }
  319. else // packing == TV60_PACKING
  320. {
  321. _time = value;
  322. }
  323. }
  324. unsigned int
  325. TimeCode::userData () const
  326. {
  327. return _user;
  328. }
  329. void
  330. TimeCode::setUserData (unsigned int value)
  331. {
  332. _user = value;
  333. }
  334. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT