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.

1529 lines
52KB

  1. /*
  2. * LucasArts Smush video decoder
  3. * Copyright (c) 2006 Cyril Zorin
  4. * Copyright (c) 2011 Konstantin Shishkov
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/bswap.h"
  24. #include "libavutil/imgutils.h"
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. #include "copy_block.h"
  28. #include "internal.h"
  29. #define NGLYPHS 256
  30. #define GLYPH_COORD_VECT_SIZE 16
  31. #define PALETTE_SIZE 256
  32. #define PALETTE_DELTA 768
  33. static const int8_t glyph4_x[GLYPH_COORD_VECT_SIZE] = {
  34. 0, 1, 2, 3, 3, 3, 3, 2, 1, 0, 0, 0, 1, 2, 2, 1
  35. };
  36. static const int8_t glyph4_y[GLYPH_COORD_VECT_SIZE] = {
  37. 0, 0, 0, 0, 1, 2, 3, 3, 3, 3, 2, 1, 1, 1, 2, 2
  38. };
  39. static const int8_t glyph8_x[GLYPH_COORD_VECT_SIZE] = {
  40. 0, 2, 5, 7, 7, 7, 7, 7, 7, 5, 2, 0, 0, 0, 0, 0
  41. };
  42. static const int8_t glyph8_y[GLYPH_COORD_VECT_SIZE] = {
  43. 0, 0, 0, 0, 1, 3, 4, 6, 7, 7, 7, 7, 6, 4, 3, 1
  44. };
  45. static const int8_t motion_vectors[256][2] = {
  46. { 0, 0 }, { -1, -43 }, { 6, -43 }, { -9, -42 }, { 13, -41 },
  47. { -16, -40 }, { 19, -39 }, { -23, -36 }, { 26, -34 }, { -2, -33 },
  48. { 4, -33 }, { -29, -32 }, { -9, -32 }, { 11, -31 }, { -16, -29 },
  49. { 32, -29 }, { 18, -28 }, { -34, -26 }, { -22, -25 }, { -1, -25 },
  50. { 3, -25 }, { -7, -24 }, { 8, -24 }, { 24, -23 }, { 36, -23 },
  51. { -12, -22 }, { 13, -21 }, { -38, -20 }, { 0, -20 }, { -27, -19 },
  52. { -4, -19 }, { 4, -19 }, { -17, -18 }, { -8, -17 }, { 8, -17 },
  53. { 18, -17 }, { 28, -17 }, { 39, -17 }, { -12, -15 }, { 12, -15 },
  54. { -21, -14 }, { -1, -14 }, { 1, -14 }, { -41, -13 }, { -5, -13 },
  55. { 5, -13 }, { 21, -13 }, { -31, -12 }, { -15, -11 }, { -8, -11 },
  56. { 8, -11 }, { 15, -11 }, { -2, -10 }, { 1, -10 }, { 31, -10 },
  57. { -23, -9 }, { -11, -9 }, { -5, -9 }, { 4, -9 }, { 11, -9 },
  58. { 42, -9 }, { 6, -8 }, { 24, -8 }, { -18, -7 }, { -7, -7 },
  59. { -3, -7 }, { -1, -7 }, { 2, -7 }, { 18, -7 }, { -43, -6 },
  60. { -13, -6 }, { -4, -6 }, { 4, -6 }, { 8, -6 }, { -33, -5 },
  61. { -9, -5 }, { -2, -5 }, { 0, -5 }, { 2, -5 }, { 5, -5 },
  62. { 13, -5 }, { -25, -4 }, { -6, -4 }, { -3, -4 }, { 3, -4 },
  63. { 9, -4 }, { -19, -3 }, { -7, -3 }, { -4, -3 }, { -2, -3 },
  64. { -1, -3 }, { 0, -3 }, { 1, -3 }, { 2, -3 }, { 4, -3 },
  65. { 6, -3 }, { 33, -3 }, { -14, -2 }, { -10, -2 }, { -5, -2 },
  66. { -3, -2 }, { -2, -2 }, { -1, -2 }, { 0, -2 }, { 1, -2 },
  67. { 2, -2 }, { 3, -2 }, { 5, -2 }, { 7, -2 }, { 14, -2 },
  68. { 19, -2 }, { 25, -2 }, { 43, -2 }, { -7, -1 }, { -3, -1 },
  69. { -2, -1 }, { -1, -1 }, { 0, -1 }, { 1, -1 }, { 2, -1 },
  70. { 3, -1 }, { 10, -1 }, { -5, 0 }, { -3, 0 }, { -2, 0 },
  71. { -1, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 }, { 5, 0 },
  72. { 7, 0 }, { -10, 1 }, { -7, 1 }, { -3, 1 }, { -2, 1 },
  73. { -1, 1 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 3, 1 },
  74. { -43, 2 }, { -25, 2 }, { -19, 2 }, { -14, 2 }, { -5, 2 },
  75. { -3, 2 }, { -2, 2 }, { -1, 2 }, { 0, 2 }, { 1, 2 },
  76. { 2, 2 }, { 3, 2 }, { 5, 2 }, { 7, 2 }, { 10, 2 },
  77. { 14, 2 }, { -33, 3 }, { -6, 3 }, { -4, 3 }, { -2, 3 },
  78. { -1, 3 }, { 0, 3 }, { 1, 3 }, { 2, 3 }, { 4, 3 },
  79. { 19, 3 }, { -9, 4 }, { -3, 4 }, { 3, 4 }, { 7, 4 },
  80. { 25, 4 }, { -13, 5 }, { -5, 5 }, { -2, 5 }, { 0, 5 },
  81. { 2, 5 }, { 5, 5 }, { 9, 5 }, { 33, 5 }, { -8, 6 },
  82. { -4, 6 }, { 4, 6 }, { 13, 6 }, { 43, 6 }, { -18, 7 },
  83. { -2, 7 }, { 0, 7 }, { 2, 7 }, { 7, 7 }, { 18, 7 },
  84. { -24, 8 }, { -6, 8 }, { -42, 9 }, { -11, 9 }, { -4, 9 },
  85. { 5, 9 }, { 11, 9 }, { 23, 9 }, { -31, 10 }, { -1, 10 },
  86. { 2, 10 }, { -15, 11 }, { -8, 11 }, { 8, 11 }, { 15, 11 },
  87. { 31, 12 }, { -21, 13 }, { -5, 13 }, { 5, 13 }, { 41, 13 },
  88. { -1, 14 }, { 1, 14 }, { 21, 14 }, { -12, 15 }, { 12, 15 },
  89. { -39, 17 }, { -28, 17 }, { -18, 17 }, { -8, 17 }, { 8, 17 },
  90. { 17, 18 }, { -4, 19 }, { 0, 19 }, { 4, 19 }, { 27, 19 },
  91. { 38, 20 }, { -13, 21 }, { 12, 22 }, { -36, 23 }, { -24, 23 },
  92. { -8, 24 }, { 7, 24 }, { -3, 25 }, { 1, 25 }, { 22, 25 },
  93. { 34, 26 }, { -18, 28 }, { -32, 29 }, { 16, 29 }, { -11, 31 },
  94. { 9, 32 }, { 29, 32 }, { -4, 33 }, { 2, 33 }, { -26, 34 },
  95. { 23, 36 }, { -19, 39 }, { 16, 40 }, { -13, 41 }, { 9, 42 },
  96. { -6, 43 }, { 1, 43 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
  97. };
  98. static const int8_t c37_mv[] = {
  99. 0, 0, 1, 0, 2, 0, 3, 0, 5, 0,
  100. 8, 0, 13, 0, 21, 0, -1, 0, -2, 0,
  101. -3, 0, -5, 0, -8, 0, -13, 0, -17, 0,
  102. -21, 0, 0, 1, 1, 1, 2, 1, 3, 1,
  103. 5, 1, 8, 1, 13, 1, 21, 1, -1, 1,
  104. -2, 1, -3, 1, -5, 1, -8, 1, -13, 1,
  105. -17, 1, -21, 1, 0, 2, 1, 2, 2, 2,
  106. 3, 2, 5, 2, 8, 2, 13, 2, 21, 2,
  107. -1, 2, -2, 2, -3, 2, -5, 2, -8, 2,
  108. -13, 2, -17, 2, -21, 2, 0, 3, 1, 3,
  109. 2, 3, 3, 3, 5, 3, 8, 3, 13, 3,
  110. 21, 3, -1, 3, -2, 3, -3, 3, -5, 3,
  111. -8, 3, -13, 3, -17, 3, -21, 3, 0, 5,
  112. 1, 5, 2, 5, 3, 5, 5, 5, 8, 5,
  113. 13, 5, 21, 5, -1, 5, -2, 5, -3, 5,
  114. -5, 5, -8, 5, -13, 5, -17, 5, -21, 5,
  115. 0, 8, 1, 8, 2, 8, 3, 8, 5, 8,
  116. 8, 8, 13, 8, 21, 8, -1, 8, -2, 8,
  117. -3, 8, -5, 8, -8, 8, -13, 8, -17, 8,
  118. -21, 8, 0, 13, 1, 13, 2, 13, 3, 13,
  119. 5, 13, 8, 13, 13, 13, 21, 13, -1, 13,
  120. -2, 13, -3, 13, -5, 13, -8, 13, -13, 13,
  121. -17, 13, -21, 13, 0, 21, 1, 21, 2, 21,
  122. 3, 21, 5, 21, 8, 21, 13, 21, 21, 21,
  123. -1, 21, -2, 21, -3, 21, -5, 21, -8, 21,
  124. -13, 21, -17, 21, -21, 21, 0, -1, 1, -1,
  125. 2, -1, 3, -1, 5, -1, 8, -1, 13, -1,
  126. 21, -1, -1, -1, -2, -1, -3, -1, -5, -1,
  127. -8, -1, -13, -1, -17, -1, -21, -1, 0, -2,
  128. 1, -2, 2, -2, 3, -2, 5, -2, 8, -2,
  129. 13, -2, 21, -2, -1, -2, -2, -2, -3, -2,
  130. -5, -2, -8, -2, -13, -2, -17, -2, -21, -2,
  131. 0, -3, 1, -3, 2, -3, 3, -3, 5, -3,
  132. 8, -3, 13, -3, 21, -3, -1, -3, -2, -3,
  133. -3, -3, -5, -3, -8, -3, -13, -3, -17, -3,
  134. -21, -3, 0, -5, 1, -5, 2, -5, 3, -5,
  135. 5, -5, 8, -5, 13, -5, 21, -5, -1, -5,
  136. -2, -5, -3, -5, -5, -5, -8, -5, -13, -5,
  137. -17, -5, -21, -5, 0, -8, 1, -8, 2, -8,
  138. 3, -8, 5, -8, 8, -8, 13, -8, 21, -8,
  139. -1, -8, -2, -8, -3, -8, -5, -8, -8, -8,
  140. -13, -8, -17, -8, -21, -8, 0, -13, 1, -13,
  141. 2, -13, 3, -13, 5, -13, 8, -13, 13, -13,
  142. 21, -13, -1, -13, -2, -13, -3, -13, -5, -13,
  143. -8, -13, -13, -13, -17, -13, -21, -13, 0, -17,
  144. 1, -17, 2, -17, 3, -17, 5, -17, 8, -17,
  145. 13, -17, 21, -17, -1, -17, -2, -17, -3, -17,
  146. -5, -17, -8, -17, -13, -17, -17, -17, -21, -17,
  147. 0, -21, 1, -21, 2, -21, 3, -21, 5, -21,
  148. 8, -21, 13, -21, 21, -21, -1, -21, -2, -21,
  149. -3, -21, -5, -21, -8, -21, -13, -21, -17, -21,
  150. 0, 0, -8, -29, 8, -29, -18, -25, 17, -25,
  151. 0, -23, -6, -22, 6, -22, -13, -19, 12, -19,
  152. 0, -18, 25, -18, -25, -17, -5, -17, 5, -17,
  153. -10, -15, 10, -15, 0, -14, -4, -13, 4, -13,
  154. 19, -13, -19, -12, -8, -11, -2, -11, 0, -11,
  155. 2, -11, 8, -11, -15, -10, -4, -10, 4, -10,
  156. 15, -10, -6, -9, -1, -9, 1, -9, 6, -9,
  157. -29, -8, -11, -8, -8, -8, -3, -8, 3, -8,
  158. 8, -8, 11, -8, 29, -8, -5, -7, -2, -7,
  159. 0, -7, 2, -7, 5, -7, -22, -6, -9, -6,
  160. -6, -6, -3, -6, -1, -6, 1, -6, 3, -6,
  161. 6, -6, 9, -6, 22, -6, -17, -5, -7, -5,
  162. -4, -5, -2, -5, 0, -5, 2, -5, 4, -5,
  163. 7, -5, 17, -5, -13, -4, -10, -4, -5, -4,
  164. -3, -4, -1, -4, 0, -4, 1, -4, 3, -4,
  165. 5, -4, 10, -4, 13, -4, -8, -3, -6, -3,
  166. -4, -3, -3, -3, -2, -3, -1, -3, 0, -3,
  167. 1, -3, 2, -3, 4, -3, 6, -3, 8, -3,
  168. -11, -2, -7, -2, -5, -2, -3, -2, -2, -2,
  169. -1, -2, 0, -2, 1, -2, 2, -2, 3, -2,
  170. 5, -2, 7, -2, 11, -2, -9, -1, -6, -1,
  171. -4, -1, -3, -1, -2, -1, -1, -1, 0, -1,
  172. 1, -1, 2, -1, 3, -1, 4, -1, 6, -1,
  173. 9, -1, -31, 0, -23, 0, -18, 0, -14, 0,
  174. -11, 0, -7, 0, -5, 0, -4, 0, -3, 0,
  175. -2, 0, -1, 0, 0, -31, 1, 0, 2, 0,
  176. 3, 0, 4, 0, 5, 0, 7, 0, 11, 0,
  177. 14, 0, 18, 0, 23, 0, 31, 0, -9, 1,
  178. -6, 1, -4, 1, -3, 1, -2, 1, -1, 1,
  179. 0, 1, 1, 1, 2, 1, 3, 1, 4, 1,
  180. 6, 1, 9, 1, -11, 2, -7, 2, -5, 2,
  181. -3, 2, -2, 2, -1, 2, 0, 2, 1, 2,
  182. 2, 2, 3, 2, 5, 2, 7, 2, 11, 2,
  183. -8, 3, -6, 3, -4, 3, -2, 3, -1, 3,
  184. 0, 3, 1, 3, 2, 3, 3, 3, 4, 3,
  185. 6, 3, 8, 3, -13, 4, -10, 4, -5, 4,
  186. -3, 4, -1, 4, 0, 4, 1, 4, 3, 4,
  187. 5, 4, 10, 4, 13, 4, -17, 5, -7, 5,
  188. -4, 5, -2, 5, 0, 5, 2, 5, 4, 5,
  189. 7, 5, 17, 5, -22, 6, -9, 6, -6, 6,
  190. -3, 6, -1, 6, 1, 6, 3, 6, 6, 6,
  191. 9, 6, 22, 6, -5, 7, -2, 7, 0, 7,
  192. 2, 7, 5, 7, -29, 8, -11, 8, -8, 8,
  193. -3, 8, 3, 8, 8, 8, 11, 8, 29, 8,
  194. -6, 9, -1, 9, 1, 9, 6, 9, -15, 10,
  195. -4, 10, 4, 10, 15, 10, -8, 11, -2, 11,
  196. 0, 11, 2, 11, 8, 11, 19, 12, -19, 13,
  197. -4, 13, 4, 13, 0, 14, -10, 15, 10, 15,
  198. -5, 17, 5, 17, 25, 17, -25, 18, 0, 18,
  199. -12, 19, 13, 19, -6, 22, 6, 22, 0, 23,
  200. -17, 25, 18, 25, -8, 29, 8, 29, 0, 31,
  201. 0, 0, -6, -22, 6, -22, -13, -19, 12, -19,
  202. 0, -18, -5, -17, 5, -17, -10, -15, 10, -15,
  203. 0, -14, -4, -13, 4, -13, 19, -13, -19, -12,
  204. -8, -11, -2, -11, 0, -11, 2, -11, 8, -11,
  205. -15, -10, -4, -10, 4, -10, 15, -10, -6, -9,
  206. -1, -9, 1, -9, 6, -9, -11, -8, -8, -8,
  207. -3, -8, 0, -8, 3, -8, 8, -8, 11, -8,
  208. -5, -7, -2, -7, 0, -7, 2, -7, 5, -7,
  209. -22, -6, -9, -6, -6, -6, -3, -6, -1, -6,
  210. 1, -6, 3, -6, 6, -6, 9, -6, 22, -6,
  211. -17, -5, -7, -5, -4, -5, -2, -5, -1, -5,
  212. 0, -5, 1, -5, 2, -5, 4, -5, 7, -5,
  213. 17, -5, -13, -4, -10, -4, -5, -4, -3, -4,
  214. -2, -4, -1, -4, 0, -4, 1, -4, 2, -4,
  215. 3, -4, 5, -4, 10, -4, 13, -4, -8, -3,
  216. -6, -3, -4, -3, -3, -3, -2, -3, -1, -3,
  217. 0, -3, 1, -3, 2, -3, 3, -3, 4, -3,
  218. 6, -3, 8, -3, -11, -2, -7, -2, -5, -2,
  219. -4, -2, -3, -2, -2, -2, -1, -2, 0, -2,
  220. 1, -2, 2, -2, 3, -2, 4, -2, 5, -2,
  221. 7, -2, 11, -2, -9, -1, -6, -1, -5, -1,
  222. -4, -1, -3, -1, -2, -1, -1, -1, 0, -1,
  223. 1, -1, 2, -1, 3, -1, 4, -1, 5, -1,
  224. 6, -1, 9, -1, -23, 0, -18, 0, -14, 0,
  225. -11, 0, -7, 0, -5, 0, -4, 0, -3, 0,
  226. -2, 0, -1, 0, 0, -23, 1, 0, 2, 0,
  227. 3, 0, 4, 0, 5, 0, 7, 0, 11, 0,
  228. 14, 0, 18, 0, 23, 0, -9, 1, -6, 1,
  229. -5, 1, -4, 1, -3, 1, -2, 1, -1, 1,
  230. 0, 1, 1, 1, 2, 1, 3, 1, 4, 1,
  231. 5, 1, 6, 1, 9, 1, -11, 2, -7, 2,
  232. -5, 2, -4, 2, -3, 2, -2, 2, -1, 2,
  233. 0, 2, 1, 2, 2, 2, 3, 2, 4, 2,
  234. 5, 2, 7, 2, 11, 2, -8, 3, -6, 3,
  235. -4, 3, -3, 3, -2, 3, -1, 3, 0, 3,
  236. 1, 3, 2, 3, 3, 3, 4, 3, 6, 3,
  237. 8, 3, -13, 4, -10, 4, -5, 4, -3, 4,
  238. -2, 4, -1, 4, 0, 4, 1, 4, 2, 4,
  239. 3, 4, 5, 4, 10, 4, 13, 4, -17, 5,
  240. -7, 5, -4, 5, -2, 5, -1, 5, 0, 5,
  241. 1, 5, 2, 5, 4, 5, 7, 5, 17, 5,
  242. -22, 6, -9, 6, -6, 6, -3, 6, -1, 6,
  243. 1, 6, 3, 6, 6, 6, 9, 6, 22, 6,
  244. -5, 7, -2, 7, 0, 7, 2, 7, 5, 7,
  245. -11, 8, -8, 8, -3, 8, 0, 8, 3, 8,
  246. 8, 8, 11, 8, -6, 9, -1, 9, 1, 9,
  247. 6, 9, -15, 10, -4, 10, 4, 10, 15, 10,
  248. -8, 11, -2, 11, 0, 11, 2, 11, 8, 11,
  249. 19, 12, -19, 13, -4, 13, 4, 13, 0, 14,
  250. -10, 15, 10, 15, -5, 17, 5, 17, 0, 18,
  251. -12, 19, 13, 19, -6, 22, 6, 22, 0, 23,
  252. };
  253. typedef struct SANMVideoContext {
  254. AVCodecContext *avctx;
  255. GetByteContext gb;
  256. int version, subversion;
  257. uint32_t pal[PALETTE_SIZE];
  258. int16_t delta_pal[PALETTE_DELTA];
  259. int pitch;
  260. int width, height;
  261. int aligned_width, aligned_height;
  262. int prev_seq;
  263. AVFrame *frame;
  264. uint16_t *frm0, *frm1, *frm2;
  265. uint8_t *stored_frame;
  266. uint32_t frm0_size, frm1_size, frm2_size;
  267. uint32_t stored_frame_size;
  268. uint8_t *rle_buf;
  269. unsigned int rle_buf_size;
  270. int rotate_code;
  271. long npixels, buf_size;
  272. uint16_t codebook[256];
  273. uint16_t small_codebook[4];
  274. int8_t p4x4glyphs[NGLYPHS][16];
  275. int8_t p8x8glyphs[NGLYPHS][64];
  276. } SANMVideoContext;
  277. typedef struct SANMFrameHeader {
  278. int seq_num, codec, rotate_code, rle_output_size;
  279. uint16_t bg_color;
  280. uint32_t width, height;
  281. } SANMFrameHeader;
  282. enum GlyphEdge {
  283. LEFT_EDGE,
  284. TOP_EDGE,
  285. RIGHT_EDGE,
  286. BOTTOM_EDGE,
  287. NO_EDGE
  288. };
  289. enum GlyphDir {
  290. DIR_LEFT,
  291. DIR_UP,
  292. DIR_RIGHT,
  293. DIR_DOWN,
  294. NO_DIR
  295. };
  296. /**
  297. * Return enum GlyphEdge of box where point (x, y) lies.
  298. *
  299. * @param x x point coordinate
  300. * @param y y point coordinate
  301. * @param edge_size box width/height.
  302. */
  303. static enum GlyphEdge which_edge(int x, int y, int edge_size)
  304. {
  305. const int edge_max = edge_size - 1;
  306. if (!y)
  307. return BOTTOM_EDGE;
  308. else if (y == edge_max)
  309. return TOP_EDGE;
  310. else if (!x)
  311. return LEFT_EDGE;
  312. else if (x == edge_max)
  313. return RIGHT_EDGE;
  314. else
  315. return NO_EDGE;
  316. }
  317. static enum GlyphDir which_direction(enum GlyphEdge edge0, enum GlyphEdge edge1)
  318. {
  319. if ((edge0 == LEFT_EDGE && edge1 == RIGHT_EDGE) ||
  320. (edge1 == LEFT_EDGE && edge0 == RIGHT_EDGE) ||
  321. (edge0 == BOTTOM_EDGE && edge1 != TOP_EDGE) ||
  322. (edge1 == BOTTOM_EDGE && edge0 != TOP_EDGE))
  323. return DIR_UP;
  324. else if ((edge0 == TOP_EDGE && edge1 != BOTTOM_EDGE) ||
  325. (edge1 == TOP_EDGE && edge0 != BOTTOM_EDGE))
  326. return DIR_DOWN;
  327. else if ((edge0 == LEFT_EDGE && edge1 != RIGHT_EDGE) ||
  328. (edge1 == LEFT_EDGE && edge0 != RIGHT_EDGE))
  329. return DIR_LEFT;
  330. else if ((edge0 == TOP_EDGE && edge1 == BOTTOM_EDGE) ||
  331. (edge1 == TOP_EDGE && edge0 == BOTTOM_EDGE) ||
  332. (edge0 == RIGHT_EDGE && edge1 != LEFT_EDGE) ||
  333. (edge1 == RIGHT_EDGE && edge0 != LEFT_EDGE))
  334. return DIR_RIGHT;
  335. return NO_DIR;
  336. }
  337. /* Interpolate two points. */
  338. static void interp_point(int8_t *points, int x0, int y0, int x1, int y1,
  339. int pos, int npoints)
  340. {
  341. if (npoints) {
  342. points[0] = (x0 * pos + x1 * (npoints - pos) + (npoints >> 1)) / npoints;
  343. points[1] = (y0 * pos + y1 * (npoints - pos) + (npoints >> 1)) / npoints;
  344. } else {
  345. points[0] = x0;
  346. points[1] = y0;
  347. }
  348. }
  349. /**
  350. * Construct glyphs by iterating through vector coordinates.
  351. *
  352. * @param pglyphs pointer to table where glyphs are stored
  353. * @param xvec pointer to x component of vector coordinates
  354. * @param yvec pointer to y component of vector coordinates
  355. * @param side_length glyph width/height.
  356. */
  357. static void make_glyphs(int8_t *pglyphs, const int8_t *xvec, const int8_t *yvec,
  358. const int side_length)
  359. {
  360. const int glyph_size = side_length * side_length;
  361. int8_t *pglyph = pglyphs;
  362. int i, j;
  363. for (i = 0; i < GLYPH_COORD_VECT_SIZE; i++) {
  364. int x0 = xvec[i];
  365. int y0 = yvec[i];
  366. enum GlyphEdge edge0 = which_edge(x0, y0, side_length);
  367. for (j = 0; j < GLYPH_COORD_VECT_SIZE; j++, pglyph += glyph_size) {
  368. int x1 = xvec[j];
  369. int y1 = yvec[j];
  370. enum GlyphEdge edge1 = which_edge(x1, y1, side_length);
  371. enum GlyphDir dir = which_direction(edge0, edge1);
  372. int npoints = FFMAX(FFABS(x1 - x0), FFABS(y1 - y0));
  373. int ipoint;
  374. for (ipoint = 0; ipoint <= npoints; ipoint++) {
  375. int8_t point[2];
  376. int irow, icol;
  377. interp_point(point, x0, y0, x1, y1, ipoint, npoints);
  378. switch (dir) {
  379. case DIR_UP:
  380. for (irow = point[1]; irow >= 0; irow--)
  381. pglyph[point[0] + irow * side_length] = 1;
  382. break;
  383. case DIR_DOWN:
  384. for (irow = point[1]; irow < side_length; irow++)
  385. pglyph[point[0] + irow * side_length] = 1;
  386. break;
  387. case DIR_LEFT:
  388. for (icol = point[0]; icol >= 0; icol--)
  389. pglyph[icol + point[1] * side_length] = 1;
  390. break;
  391. case DIR_RIGHT:
  392. for (icol = point[0]; icol < side_length; icol++)
  393. pglyph[icol + point[1] * side_length] = 1;
  394. break;
  395. }
  396. }
  397. }
  398. }
  399. }
  400. static void init_sizes(SANMVideoContext *ctx, int width, int height)
  401. {
  402. ctx->width = width;
  403. ctx->height = height;
  404. ctx->npixels = width * height;
  405. ctx->aligned_width = FFALIGN(width, 8);
  406. ctx->aligned_height = FFALIGN(height, 8);
  407. ctx->buf_size = ctx->aligned_width * ctx->aligned_height * sizeof(ctx->frm0[0]);
  408. ctx->pitch = width;
  409. }
  410. static void destroy_buffers(SANMVideoContext *ctx)
  411. {
  412. av_freep(&ctx->frm0);
  413. av_freep(&ctx->frm1);
  414. av_freep(&ctx->frm2);
  415. av_freep(&ctx->stored_frame);
  416. av_freep(&ctx->rle_buf);
  417. ctx->frm0_size =
  418. ctx->frm1_size =
  419. ctx->frm2_size = 0;
  420. }
  421. static av_cold int init_buffers(SANMVideoContext *ctx)
  422. {
  423. av_fast_padded_malloc(&ctx->frm0, &ctx->frm0_size, ctx->buf_size);
  424. av_fast_padded_malloc(&ctx->frm1, &ctx->frm1_size, ctx->buf_size);
  425. av_fast_padded_malloc(&ctx->frm2, &ctx->frm2_size, ctx->buf_size);
  426. if (!ctx->version)
  427. av_fast_padded_malloc(&ctx->stored_frame,
  428. &ctx->stored_frame_size, ctx->buf_size);
  429. if (!ctx->frm0 || !ctx->frm1 || !ctx->frm2 ||
  430. (!ctx->stored_frame && !ctx->version)) {
  431. destroy_buffers(ctx);
  432. return AVERROR(ENOMEM);
  433. }
  434. return 0;
  435. }
  436. static void rotate_bufs(SANMVideoContext *ctx, int rotate_code)
  437. {
  438. if (rotate_code == 2)
  439. FFSWAP(uint16_t*, ctx->frm1, ctx->frm2);
  440. FFSWAP(uint16_t*, ctx->frm2, ctx->frm0);
  441. }
  442. static av_cold int decode_init(AVCodecContext *avctx)
  443. {
  444. SANMVideoContext *ctx = avctx->priv_data;
  445. ctx->avctx = avctx;
  446. ctx->version = !avctx->extradata_size;
  447. avctx->pix_fmt = ctx->version ? AV_PIX_FMT_RGB565 : AV_PIX_FMT_PAL8;
  448. init_sizes(ctx, avctx->width, avctx->height);
  449. if (init_buffers(ctx)) {
  450. av_log(avctx, AV_LOG_ERROR, "Error allocating buffers.\n");
  451. return AVERROR(ENOMEM);
  452. }
  453. make_glyphs(ctx->p4x4glyphs[0], glyph4_x, glyph4_y, 4);
  454. make_glyphs(ctx->p8x8glyphs[0], glyph8_x, glyph8_y, 8);
  455. if (!ctx->version) {
  456. int i;
  457. if (avctx->extradata_size < 1026) {
  458. av_log(avctx, AV_LOG_ERROR, "Not enough extradata.\n");
  459. return AVERROR_INVALIDDATA;
  460. }
  461. ctx->subversion = AV_RL16(avctx->extradata);
  462. for (i = 0; i < PALETTE_SIZE; i++)
  463. ctx->pal[i] = 0xFFU << 24 | AV_RL32(avctx->extradata + 2 + i * 4);
  464. }
  465. return 0;
  466. }
  467. static av_cold int decode_end(AVCodecContext *avctx)
  468. {
  469. SANMVideoContext *ctx = avctx->priv_data;
  470. destroy_buffers(ctx);
  471. return 0;
  472. }
  473. static int rle_decode(SANMVideoContext *ctx, uint8_t *dst, const int out_size)
  474. {
  475. int opcode, color, run_len, left = out_size;
  476. while (left > 0) {
  477. opcode = bytestream2_get_byte(&ctx->gb);
  478. run_len = (opcode >> 1) + 1;
  479. if (run_len > left || bytestream2_get_bytes_left(&ctx->gb) <= 0)
  480. return AVERROR_INVALIDDATA;
  481. if (opcode & 1) {
  482. color = bytestream2_get_byte(&ctx->gb);
  483. memset(dst, color, run_len);
  484. } else {
  485. if (bytestream2_get_bytes_left(&ctx->gb) < run_len)
  486. return AVERROR_INVALIDDATA;
  487. bytestream2_get_bufferu(&ctx->gb, dst, run_len);
  488. }
  489. dst += run_len;
  490. left -= run_len;
  491. }
  492. return 0;
  493. }
  494. static int old_codec1(SANMVideoContext *ctx, int top,
  495. int left, int width, int height)
  496. {
  497. uint8_t *dst = ((uint8_t *)ctx->frm0) + left + top * ctx->pitch;
  498. int i, j, len, flag, code, val, pos, end;
  499. for (i = 0; i < height; i++) {
  500. pos = 0;
  501. if (bytestream2_get_bytes_left(&ctx->gb) < 2)
  502. return AVERROR_INVALIDDATA;
  503. len = bytestream2_get_le16u(&ctx->gb);
  504. end = bytestream2_tell(&ctx->gb) + len;
  505. while (bytestream2_tell(&ctx->gb) < end) {
  506. if (bytestream2_get_bytes_left(&ctx->gb) < 2)
  507. return AVERROR_INVALIDDATA;
  508. code = bytestream2_get_byteu(&ctx->gb);
  509. flag = code & 1;
  510. code = (code >> 1) + 1;
  511. if (pos + code > width)
  512. return AVERROR_INVALIDDATA;
  513. if (flag) {
  514. val = bytestream2_get_byteu(&ctx->gb);
  515. if (val)
  516. memset(dst + pos, val, code);
  517. pos += code;
  518. } else {
  519. if (bytestream2_get_bytes_left(&ctx->gb) < code)
  520. return AVERROR_INVALIDDATA;
  521. for (j = 0; j < code; j++) {
  522. val = bytestream2_get_byteu(&ctx->gb);
  523. if (val)
  524. dst[pos] = val;
  525. pos++;
  526. }
  527. }
  528. }
  529. dst += ctx->pitch;
  530. }
  531. ctx->rotate_code = 0;
  532. return 0;
  533. }
  534. static inline void codec37_mv(uint8_t *dst, const uint8_t *src,
  535. int height, int stride, int x, int y)
  536. {
  537. int pos, i, j;
  538. pos = x + y * stride;
  539. for (j = 0; j < 4; j++) {
  540. for (i = 0; i < 4; i++) {
  541. if ((pos + i) < 0 || (pos + i) >= height * stride)
  542. dst[i] = 0;
  543. else
  544. dst[i] = src[i];
  545. }
  546. dst += stride;
  547. src += stride;
  548. pos += stride;
  549. }
  550. }
  551. static int old_codec37(SANMVideoContext *ctx, int top,
  552. int left, int width, int height)
  553. {
  554. int stride = ctx->pitch;
  555. int i, j, k, t;
  556. uint8_t *dst, *prev;
  557. int skip_run = 0;
  558. int compr = bytestream2_get_byte(&ctx->gb);
  559. int mvoff = bytestream2_get_byte(&ctx->gb);
  560. int seq = bytestream2_get_le16(&ctx->gb);
  561. uint32_t decoded_size = bytestream2_get_le32(&ctx->gb);
  562. int flags;
  563. bytestream2_skip(&ctx->gb, 4);
  564. flags = bytestream2_get_byte(&ctx->gb);
  565. bytestream2_skip(&ctx->gb, 3);
  566. if (decoded_size > ctx->height * stride - left - top * stride) {
  567. decoded_size = ctx->height * stride - left - top * stride;
  568. av_log(ctx->avctx, AV_LOG_WARNING, "Decoded size is too large.\n");
  569. }
  570. ctx->rotate_code = 0;
  571. if (((seq & 1) || !(flags & 1)) && (compr && compr != 2))
  572. rotate_bufs(ctx, 1);
  573. dst = ((uint8_t*)ctx->frm0) + left + top * stride;
  574. prev = ((uint8_t*)ctx->frm2) + left + top * stride;
  575. if (mvoff > 2) {
  576. av_log(ctx->avctx, AV_LOG_ERROR, "Invalid motion base value %d.\n", mvoff);
  577. return AVERROR_INVALIDDATA;
  578. }
  579. switch (compr) {
  580. case 0:
  581. for (i = 0; i < height; i++) {
  582. bytestream2_get_buffer(&ctx->gb, dst, width);
  583. dst += stride;
  584. }
  585. memset(ctx->frm1, 0, ctx->height * stride);
  586. memset(ctx->frm2, 0, ctx->height * stride);
  587. break;
  588. case 2:
  589. if (rle_decode(ctx, dst, decoded_size))
  590. return AVERROR_INVALIDDATA;
  591. memset(ctx->frm1, 0, ctx->frm1_size);
  592. memset(ctx->frm2, 0, ctx->frm2_size);
  593. break;
  594. case 3:
  595. case 4:
  596. if (flags & 4) {
  597. for (j = 0; j < height; j += 4) {
  598. for (i = 0; i < width; i += 4) {
  599. int code;
  600. if (skip_run) {
  601. skip_run--;
  602. copy_block4(dst + i, prev + i, stride, stride, 4);
  603. continue;
  604. }
  605. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  606. return AVERROR_INVALIDDATA;
  607. code = bytestream2_get_byteu(&ctx->gb);
  608. switch (code) {
  609. case 0xFF:
  610. if (bytestream2_get_bytes_left(&ctx->gb) < 16)
  611. return AVERROR_INVALIDDATA;
  612. for (k = 0; k < 4; k++)
  613. bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
  614. break;
  615. case 0xFE:
  616. if (bytestream2_get_bytes_left(&ctx->gb) < 4)
  617. return AVERROR_INVALIDDATA;
  618. for (k = 0; k < 4; k++)
  619. memset(dst + i + k * stride, bytestream2_get_byteu(&ctx->gb), 4);
  620. break;
  621. case 0xFD:
  622. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  623. return AVERROR_INVALIDDATA;
  624. t = bytestream2_get_byteu(&ctx->gb);
  625. for (k = 0; k < 4; k++)
  626. memset(dst + i + k * stride, t, 4);
  627. break;
  628. default:
  629. if (compr == 4 && !code) {
  630. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  631. return AVERROR_INVALIDDATA;
  632. skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
  633. i -= 4;
  634. } else {
  635. int mx, my;
  636. mx = c37_mv[(mvoff * 255 + code) * 2];
  637. my = c37_mv[(mvoff * 255 + code) * 2 + 1];
  638. codec37_mv(dst + i, prev + i + mx + my * stride,
  639. ctx->height, stride, i + mx, j + my);
  640. }
  641. }
  642. }
  643. dst += stride * 4;
  644. prev += stride * 4;
  645. }
  646. } else {
  647. for (j = 0; j < height; j += 4) {
  648. for (i = 0; i < width; i += 4) {
  649. int code;
  650. if (skip_run) {
  651. skip_run--;
  652. copy_block4(dst + i, prev + i, stride, stride, 4);
  653. continue;
  654. }
  655. code = bytestream2_get_byte(&ctx->gb);
  656. if (code == 0xFF) {
  657. if (bytestream2_get_bytes_left(&ctx->gb) < 16)
  658. return AVERROR_INVALIDDATA;
  659. for (k = 0; k < 4; k++)
  660. bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
  661. } else if (compr == 4 && !code) {
  662. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  663. return AVERROR_INVALIDDATA;
  664. skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
  665. i -= 4;
  666. } else {
  667. int mx, my;
  668. mx = c37_mv[(mvoff * 255 + code) * 2];
  669. my = c37_mv[(mvoff * 255 + code) * 2 + 1];
  670. codec37_mv(dst + i, prev + i + mx + my * stride,
  671. ctx->height, stride, i + mx, j + my);
  672. }
  673. }
  674. dst += stride * 4;
  675. prev += stride * 4;
  676. }
  677. }
  678. break;
  679. default:
  680. avpriv_report_missing_feature(ctx->avctx,
  681. "Subcodec 37 compression %d", compr);
  682. return AVERROR_PATCHWELCOME;
  683. }
  684. return 0;
  685. }
  686. static int process_block(SANMVideoContext *ctx, uint8_t *dst, uint8_t *prev1,
  687. uint8_t *prev2, int stride, int tbl, int size)
  688. {
  689. int code, k, t;
  690. uint8_t colors[2];
  691. int8_t *pglyph;
  692. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  693. return AVERROR_INVALIDDATA;
  694. code = bytestream2_get_byteu(&ctx->gb);
  695. if (code >= 0xF8) {
  696. switch (code) {
  697. case 0xFF:
  698. if (size == 2) {
  699. if (bytestream2_get_bytes_left(&ctx->gb) < 4)
  700. return AVERROR_INVALIDDATA;
  701. dst[0] = bytestream2_get_byteu(&ctx->gb);
  702. dst[1] = bytestream2_get_byteu(&ctx->gb);
  703. dst[0 + stride] = bytestream2_get_byteu(&ctx->gb);
  704. dst[1 + stride] = bytestream2_get_byteu(&ctx->gb);
  705. } else {
  706. size >>= 1;
  707. if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
  708. return AVERROR_INVALIDDATA;
  709. if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
  710. stride, tbl, size))
  711. return AVERROR_INVALIDDATA;
  712. dst += size * stride;
  713. prev1 += size * stride;
  714. prev2 += size * stride;
  715. if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
  716. return AVERROR_INVALIDDATA;
  717. if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
  718. stride, tbl, size))
  719. return AVERROR_INVALIDDATA;
  720. }
  721. break;
  722. case 0xFE:
  723. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  724. return AVERROR_INVALIDDATA;
  725. t = bytestream2_get_byteu(&ctx->gb);
  726. for (k = 0; k < size; k++)
  727. memset(dst + k * stride, t, size);
  728. break;
  729. case 0xFD:
  730. if (bytestream2_get_bytes_left(&ctx->gb) < 3)
  731. return AVERROR_INVALIDDATA;
  732. code = bytestream2_get_byteu(&ctx->gb);
  733. pglyph = (size == 8) ? ctx->p8x8glyphs[code] : ctx->p4x4glyphs[code];
  734. bytestream2_get_bufferu(&ctx->gb, colors, 2);
  735. for (k = 0; k < size; k++)
  736. for (t = 0; t < size; t++)
  737. dst[t + k * stride] = colors[!*pglyph++];
  738. break;
  739. case 0xFC:
  740. for (k = 0; k < size; k++)
  741. memcpy(dst + k * stride, prev1 + k * stride, size);
  742. break;
  743. default:
  744. k = bytestream2_tell(&ctx->gb);
  745. bytestream2_seek(&ctx->gb, tbl + (code & 7), SEEK_SET);
  746. t = bytestream2_get_byte(&ctx->gb);
  747. bytestream2_seek(&ctx->gb, k, SEEK_SET);
  748. for (k = 0; k < size; k++)
  749. memset(dst + k * stride, t, size);
  750. }
  751. } else {
  752. int mx = motion_vectors[code][0];
  753. int my = motion_vectors[code][1];
  754. int index = prev2 - (const uint8_t *)ctx->frm2;
  755. av_assert2(index >= 0 && index < (ctx->buf_size >> 1));
  756. if (index < -mx - my * stride ||
  757. (ctx->buf_size >> 1) - index < mx + size + (my + size - 1) * stride) {
  758. av_log(ctx->avctx, AV_LOG_ERROR, "MV is invalid.\n");
  759. return AVERROR_INVALIDDATA;
  760. }
  761. for (k = 0; k < size; k++)
  762. memcpy(dst + k * stride, prev2 + mx + (my + k) * stride, size);
  763. }
  764. return 0;
  765. }
  766. static int old_codec47(SANMVideoContext *ctx, int top,
  767. int left, int width, int height)
  768. {
  769. uint32_t decoded_size;
  770. int i, j;
  771. int stride = ctx->pitch;
  772. uint8_t *dst = (uint8_t *)ctx->frm0 + left + top * stride;
  773. uint8_t *prev1 = (uint8_t *)ctx->frm1;
  774. uint8_t *prev2 = (uint8_t *)ctx->frm2;
  775. int tbl_pos = bytestream2_tell(&ctx->gb);
  776. int seq = bytestream2_get_le16(&ctx->gb);
  777. int compr = bytestream2_get_byte(&ctx->gb);
  778. int new_rot = bytestream2_get_byte(&ctx->gb);
  779. int skip = bytestream2_get_byte(&ctx->gb);
  780. bytestream2_skip(&ctx->gb, 9);
  781. decoded_size = bytestream2_get_le32(&ctx->gb);
  782. bytestream2_skip(&ctx->gb, 8);
  783. if (decoded_size > ctx->height * stride - left - top * stride) {
  784. decoded_size = ctx->height * stride - left - top * stride;
  785. av_log(ctx->avctx, AV_LOG_WARNING, "Decoded size is too large.\n");
  786. }
  787. if (skip & 1)
  788. bytestream2_skip(&ctx->gb, 0x8080);
  789. if (!seq) {
  790. ctx->prev_seq = -1;
  791. memset(prev1, 0, ctx->height * stride);
  792. memset(prev2, 0, ctx->height * stride);
  793. }
  794. switch (compr) {
  795. case 0:
  796. if (bytestream2_get_bytes_left(&ctx->gb) < width * height)
  797. return AVERROR_INVALIDDATA;
  798. for (j = 0; j < height; j++) {
  799. bytestream2_get_bufferu(&ctx->gb, dst, width);
  800. dst += stride;
  801. }
  802. break;
  803. case 1:
  804. if (bytestream2_get_bytes_left(&ctx->gb) < ((width + 1) >> 1) * ((height + 1) >> 1))
  805. return AVERROR_INVALIDDATA;
  806. for (j = 0; j < height; j += 2) {
  807. for (i = 0; i < width; i += 2) {
  808. dst[i] =
  809. dst[i + 1] =
  810. dst[stride + i] =
  811. dst[stride + i + 1] = bytestream2_get_byteu(&ctx->gb);
  812. }
  813. dst += stride * 2;
  814. }
  815. break;
  816. case 2:
  817. if (seq == ctx->prev_seq + 1) {
  818. for (j = 0; j < height; j += 8) {
  819. for (i = 0; i < width; i += 8)
  820. if (process_block(ctx, dst + i, prev1 + i, prev2 + i, stride,
  821. tbl_pos + 8, 8))
  822. return AVERROR_INVALIDDATA;
  823. dst += stride * 8;
  824. prev1 += stride * 8;
  825. prev2 += stride * 8;
  826. }
  827. }
  828. break;
  829. case 3:
  830. memcpy(ctx->frm0, ctx->frm2, ctx->pitch * ctx->height);
  831. break;
  832. case 4:
  833. memcpy(ctx->frm0, ctx->frm1, ctx->pitch * ctx->height);
  834. break;
  835. case 5:
  836. if (rle_decode(ctx, dst, decoded_size))
  837. return AVERROR_INVALIDDATA;
  838. break;
  839. default:
  840. avpriv_report_missing_feature(ctx->avctx,
  841. "Subcodec 47 compression %d", compr);
  842. return AVERROR_PATCHWELCOME;
  843. }
  844. if (seq == ctx->prev_seq + 1)
  845. ctx->rotate_code = new_rot;
  846. else
  847. ctx->rotate_code = 0;
  848. ctx->prev_seq = seq;
  849. return 0;
  850. }
  851. static int process_frame_obj(SANMVideoContext *ctx)
  852. {
  853. uint16_t codec = bytestream2_get_le16u(&ctx->gb);
  854. uint16_t left = bytestream2_get_le16u(&ctx->gb);
  855. uint16_t top = bytestream2_get_le16u(&ctx->gb);
  856. uint16_t w = bytestream2_get_le16u(&ctx->gb);
  857. uint16_t h = bytestream2_get_le16u(&ctx->gb);
  858. if (!w || !h) {
  859. av_log(ctx->avctx, AV_LOG_ERROR, "Dimensions are invalid.\n");
  860. return AVERROR_INVALIDDATA;
  861. }
  862. if (ctx->width < left + w || ctx->height < top + h) {
  863. int ret = ff_set_dimensions(ctx->avctx, FFMAX(left + w, ctx->width),
  864. FFMAX(top + h, ctx->height));
  865. if (ret < 0)
  866. return ret;
  867. init_sizes(ctx, FFMAX(left + w, ctx->width),
  868. FFMAX(top + h, ctx->height));
  869. if (init_buffers(ctx)) {
  870. av_log(ctx->avctx, AV_LOG_ERROR, "Error resizing buffers.\n");
  871. return AVERROR(ENOMEM);
  872. }
  873. }
  874. bytestream2_skip(&ctx->gb, 4);
  875. switch (codec) {
  876. case 1:
  877. case 3:
  878. return old_codec1(ctx, top, left, w, h);
  879. break;
  880. case 37:
  881. return old_codec37(ctx, top, left, w, h);
  882. break;
  883. case 47:
  884. return old_codec47(ctx, top, left, w, h);
  885. break;
  886. default:
  887. avpriv_request_sample(ctx->avctx, "Subcodec %d", codec);
  888. return AVERROR_PATCHWELCOME;
  889. }
  890. }
  891. static int decode_0(SANMVideoContext *ctx)
  892. {
  893. uint16_t *frm = ctx->frm0;
  894. int x, y;
  895. if (bytestream2_get_bytes_left(&ctx->gb) < ctx->width * ctx->height * 2) {
  896. av_log(ctx->avctx, AV_LOG_ERROR, "Insufficient data for raw frame.\n");
  897. return AVERROR_INVALIDDATA;
  898. }
  899. for (y = 0; y < ctx->height; y++) {
  900. for (x = 0; x < ctx->width; x++)
  901. frm[x] = bytestream2_get_le16u(&ctx->gb);
  902. frm += ctx->pitch;
  903. }
  904. return 0;
  905. }
  906. static int decode_nop(SANMVideoContext *ctx)
  907. {
  908. avpriv_request_sample(ctx->avctx, "Unknown/unsupported compression type");
  909. return AVERROR_PATCHWELCOME;
  910. }
  911. static void copy_block(uint16_t *pdest, uint16_t *psrc, int block_size, int pitch)
  912. {
  913. uint8_t *dst = (uint8_t *)pdest;
  914. uint8_t *src = (uint8_t *)psrc;
  915. int stride = pitch * 2;
  916. switch (block_size) {
  917. case 2:
  918. copy_block4(dst, src, stride, stride, 2);
  919. break;
  920. case 4:
  921. copy_block8(dst, src, stride, stride, 4);
  922. break;
  923. case 8:
  924. copy_block16(dst, src, stride, stride, 8);
  925. break;
  926. }
  927. }
  928. static void fill_block(uint16_t *pdest, uint16_t color, int block_size, int pitch)
  929. {
  930. int x, y;
  931. pitch -= block_size;
  932. for (y = 0; y < block_size; y++, pdest += pitch)
  933. for (x = 0; x < block_size; x++)
  934. *pdest++ = color;
  935. }
  936. static int draw_glyph(SANMVideoContext *ctx, uint16_t *dst, int index,
  937. uint16_t fg_color, uint16_t bg_color, int block_size,
  938. int pitch)
  939. {
  940. int8_t *pglyph;
  941. uint16_t colors[2] = { fg_color, bg_color };
  942. int x, y;
  943. if (index >= NGLYPHS) {
  944. av_log(ctx->avctx, AV_LOG_ERROR, "Ignoring nonexistent glyph #%u.\n", index);
  945. return AVERROR_INVALIDDATA;
  946. }
  947. pglyph = block_size == 8 ? ctx->p8x8glyphs[index] : ctx->p4x4glyphs[index];
  948. pitch -= block_size;
  949. for (y = 0; y < block_size; y++, dst += pitch)
  950. for (x = 0; x < block_size; x++)
  951. *dst++ = colors[*pglyph++];
  952. return 0;
  953. }
  954. static int opcode_0xf7(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
  955. {
  956. uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
  957. if (block_size == 2) {
  958. uint32_t indices;
  959. if (bytestream2_get_bytes_left(&ctx->gb) < 4)
  960. return AVERROR_INVALIDDATA;
  961. indices = bytestream2_get_le32u(&ctx->gb);
  962. dst[0] = ctx->codebook[indices & 0xFF];
  963. indices >>= 8;
  964. dst[1] = ctx->codebook[indices & 0xFF];
  965. indices >>= 8;
  966. dst[pitch] = ctx->codebook[indices & 0xFF];
  967. indices >>= 8;
  968. dst[pitch + 1] = ctx->codebook[indices & 0xFF];
  969. } else {
  970. uint16_t fgcolor, bgcolor;
  971. int glyph;
  972. if (bytestream2_get_bytes_left(&ctx->gb) < 3)
  973. return AVERROR_INVALIDDATA;
  974. glyph = bytestream2_get_byteu(&ctx->gb);
  975. bgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
  976. fgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
  977. draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
  978. }
  979. return 0;
  980. }
  981. static int opcode_0xf8(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
  982. {
  983. uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
  984. if (block_size == 2) {
  985. if (bytestream2_get_bytes_left(&ctx->gb) < 8)
  986. return AVERROR_INVALIDDATA;
  987. dst[0] = bytestream2_get_le16u(&ctx->gb);
  988. dst[1] = bytestream2_get_le16u(&ctx->gb);
  989. dst[pitch] = bytestream2_get_le16u(&ctx->gb);
  990. dst[pitch + 1] = bytestream2_get_le16u(&ctx->gb);
  991. } else {
  992. uint16_t fgcolor, bgcolor;
  993. int glyph;
  994. if (bytestream2_get_bytes_left(&ctx->gb) < 5)
  995. return AVERROR_INVALIDDATA;
  996. glyph = bytestream2_get_byteu(&ctx->gb);
  997. bgcolor = bytestream2_get_le16u(&ctx->gb);
  998. fgcolor = bytestream2_get_le16u(&ctx->gb);
  999. draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
  1000. }
  1001. return 0;
  1002. }
  1003. static int good_mvec(SANMVideoContext *ctx, int cx, int cy, int mx, int my,
  1004. int block_size)
  1005. {
  1006. int start_pos = cx + mx + (cy + my) * ctx->pitch;
  1007. int end_pos = start_pos + (block_size - 1) * (ctx->pitch + 1);
  1008. int good = start_pos >= 0 && end_pos < (ctx->buf_size >> 1);
  1009. if (!good)
  1010. av_log(ctx->avctx, AV_LOG_ERROR,
  1011. "Ignoring invalid motion vector (%i, %i)->(%u, %u), block size = %u\n",
  1012. cx + mx, cy + my, cx, cy, block_size);
  1013. return good;
  1014. }
  1015. static int codec2subblock(SANMVideoContext *ctx, int cx, int cy, int blk_size)
  1016. {
  1017. int16_t mx, my, index;
  1018. int opcode;
  1019. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  1020. return AVERROR_INVALIDDATA;
  1021. opcode = bytestream2_get_byteu(&ctx->gb);
  1022. switch (opcode) {
  1023. default:
  1024. mx = motion_vectors[opcode][0];
  1025. my = motion_vectors[opcode][1];
  1026. if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
  1027. copy_block(ctx->frm0 + cx + ctx->pitch * cy,
  1028. ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
  1029. blk_size, ctx->pitch);
  1030. }
  1031. break;
  1032. case 0xF5:
  1033. if (bytestream2_get_bytes_left(&ctx->gb) < 2)
  1034. return AVERROR_INVALIDDATA;
  1035. index = bytestream2_get_le16u(&ctx->gb);
  1036. mx = index % ctx->width;
  1037. my = index / ctx->width;
  1038. if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
  1039. copy_block(ctx->frm0 + cx + ctx->pitch * cy,
  1040. ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
  1041. blk_size, ctx->pitch);
  1042. }
  1043. break;
  1044. case 0xF6:
  1045. copy_block(ctx->frm0 + cx + ctx->pitch * cy,
  1046. ctx->frm1 + cx + ctx->pitch * cy,
  1047. blk_size, ctx->pitch);
  1048. break;
  1049. case 0xF7:
  1050. opcode_0xf7(ctx, cx, cy, blk_size, ctx->pitch);
  1051. break;
  1052. case 0xF8:
  1053. opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
  1054. break;
  1055. case 0xF9:
  1056. case 0xFA:
  1057. case 0xFB:
  1058. case 0xFC:
  1059. fill_block(ctx->frm0 + cx + cy * ctx->pitch,
  1060. ctx->small_codebook[opcode - 0xf9], blk_size, ctx->pitch);
  1061. break;
  1062. case 0xFD:
  1063. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  1064. return AVERROR_INVALIDDATA;
  1065. fill_block(ctx->frm0 + cx + cy * ctx->pitch,
  1066. ctx->codebook[bytestream2_get_byteu(&ctx->gb)], blk_size, ctx->pitch);
  1067. break;
  1068. case 0xFE:
  1069. if (bytestream2_get_bytes_left(&ctx->gb) < 2)
  1070. return AVERROR_INVALIDDATA;
  1071. fill_block(ctx->frm0 + cx + cy * ctx->pitch,
  1072. bytestream2_get_le16u(&ctx->gb), blk_size, ctx->pitch);
  1073. break;
  1074. case 0xFF:
  1075. if (blk_size == 2) {
  1076. opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
  1077. } else {
  1078. blk_size >>= 1;
  1079. if (codec2subblock(ctx, cx, cy, blk_size))
  1080. return AVERROR_INVALIDDATA;
  1081. if (codec2subblock(ctx, cx + blk_size, cy, blk_size))
  1082. return AVERROR_INVALIDDATA;
  1083. if (codec2subblock(ctx, cx, cy + blk_size, blk_size))
  1084. return AVERROR_INVALIDDATA;
  1085. if (codec2subblock(ctx, cx + blk_size, cy + blk_size, blk_size))
  1086. return AVERROR_INVALIDDATA;
  1087. }
  1088. break;
  1089. }
  1090. return 0;
  1091. }
  1092. static int decode_2(SANMVideoContext *ctx)
  1093. {
  1094. int cx, cy, ret;
  1095. for (cy = 0; cy < ctx->aligned_height; cy += 8)
  1096. for (cx = 0; cx < ctx->aligned_width; cx += 8)
  1097. if (ret = codec2subblock(ctx, cx, cy, 8))
  1098. return ret;
  1099. return 0;
  1100. }
  1101. static int decode_3(SANMVideoContext *ctx)
  1102. {
  1103. memcpy(ctx->frm0, ctx->frm2, ctx->frm2_size);
  1104. return 0;
  1105. }
  1106. static int decode_4(SANMVideoContext *ctx)
  1107. {
  1108. memcpy(ctx->frm0, ctx->frm1, ctx->frm1_size);
  1109. return 0;
  1110. }
  1111. static int decode_5(SANMVideoContext *ctx)
  1112. {
  1113. #if HAVE_BIGENDIAN
  1114. uint16_t *frm;
  1115. int npixels;
  1116. #endif
  1117. uint8_t *dst = (uint8_t*)ctx->frm0;
  1118. if (rle_decode(ctx, dst, ctx->buf_size))
  1119. return AVERROR_INVALIDDATA;
  1120. #if HAVE_BIGENDIAN
  1121. npixels = ctx->npixels;
  1122. frm = ctx->frm0;
  1123. while (npixels--) {
  1124. *frm = av_bswap16(*frm);
  1125. frm++;
  1126. }
  1127. #endif
  1128. return 0;
  1129. }
  1130. static int decode_6(SANMVideoContext *ctx)
  1131. {
  1132. int npixels = ctx->npixels;
  1133. uint16_t *frm = ctx->frm0;
  1134. if (bytestream2_get_bytes_left(&ctx->gb) < npixels) {
  1135. av_log(ctx->avctx, AV_LOG_ERROR, "Insufficient data for frame.\n");
  1136. return AVERROR_INVALIDDATA;
  1137. }
  1138. while (npixels--)
  1139. *frm++ = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
  1140. return 0;
  1141. }
  1142. static int decode_8(SANMVideoContext *ctx)
  1143. {
  1144. uint16_t *pdest = ctx->frm0;
  1145. uint8_t *rsrc;
  1146. long npixels = ctx->npixels;
  1147. av_fast_malloc(&ctx->rle_buf, &ctx->rle_buf_size, npixels);
  1148. if (!ctx->rle_buf) {
  1149. av_log(ctx->avctx, AV_LOG_ERROR, "RLE buffer allocation failed.\n");
  1150. return AVERROR(ENOMEM);
  1151. }
  1152. rsrc = ctx->rle_buf;
  1153. if (rle_decode(ctx, rsrc, npixels))
  1154. return AVERROR_INVALIDDATA;
  1155. while (npixels--)
  1156. *pdest++ = ctx->codebook[*rsrc++];
  1157. return 0;
  1158. }
  1159. typedef int (*frm_decoder)(SANMVideoContext *ctx);
  1160. static const frm_decoder v1_decoders[] = {
  1161. decode_0, decode_nop, decode_2, decode_3, decode_4, decode_5,
  1162. decode_6, decode_nop, decode_8
  1163. };
  1164. static int read_frame_header(SANMVideoContext *ctx, SANMFrameHeader *hdr)
  1165. {
  1166. int i, ret;
  1167. if ((ret = bytestream2_get_bytes_left(&ctx->gb)) < 560) {
  1168. av_log(ctx->avctx, AV_LOG_ERROR, "Input frame too short (%d bytes).\n",
  1169. ret);
  1170. return AVERROR_INVALIDDATA;
  1171. }
  1172. bytestream2_skip(&ctx->gb, 8); // skip pad
  1173. hdr->width = bytestream2_get_le32u(&ctx->gb);
  1174. hdr->height = bytestream2_get_le32u(&ctx->gb);
  1175. if (hdr->width != ctx->width || hdr->height != ctx->height) {
  1176. avpriv_report_missing_feature(ctx->avctx, "Variable size frames");
  1177. return AVERROR_PATCHWELCOME;
  1178. }
  1179. hdr->seq_num = bytestream2_get_le16u(&ctx->gb);
  1180. hdr->codec = bytestream2_get_byteu(&ctx->gb);
  1181. hdr->rotate_code = bytestream2_get_byteu(&ctx->gb);
  1182. bytestream2_skip(&ctx->gb, 4); // skip pad
  1183. for (i = 0; i < 4; i++)
  1184. ctx->small_codebook[i] = bytestream2_get_le16u(&ctx->gb);
  1185. hdr->bg_color = bytestream2_get_le16u(&ctx->gb);
  1186. bytestream2_skip(&ctx->gb, 2); // skip pad
  1187. hdr->rle_output_size = bytestream2_get_le32u(&ctx->gb);
  1188. for (i = 0; i < 256; i++)
  1189. ctx->codebook[i] = bytestream2_get_le16u(&ctx->gb);
  1190. bytestream2_skip(&ctx->gb, 8); // skip pad
  1191. return 0;
  1192. }
  1193. static void fill_frame(uint16_t *pbuf, int buf_size, uint16_t color)
  1194. {
  1195. while (buf_size--)
  1196. *pbuf++ = color;
  1197. }
  1198. static int copy_output(SANMVideoContext *ctx, SANMFrameHeader *hdr)
  1199. {
  1200. uint8_t *dst;
  1201. const uint8_t *src = (uint8_t*) ctx->frm0;
  1202. int ret, dstpitch, height = ctx->height;
  1203. int srcpitch = ctx->pitch * (hdr ? sizeof(ctx->frm0[0]) : 1);
  1204. if ((ret = ff_get_buffer(ctx->avctx, ctx->frame, 0)) < 0)
  1205. return ret;
  1206. dst = ctx->frame->data[0];
  1207. dstpitch = ctx->frame->linesize[0];
  1208. while (height--) {
  1209. memcpy(dst, src, srcpitch);
  1210. src += srcpitch;
  1211. dst += dstpitch;
  1212. }
  1213. return 0;
  1214. }
  1215. static int decode_frame(AVCodecContext *avctx, void *data,
  1216. int *got_frame_ptr, AVPacket *pkt)
  1217. {
  1218. SANMVideoContext *ctx = avctx->priv_data;
  1219. int i, ret;
  1220. ctx->frame = data;
  1221. bytestream2_init(&ctx->gb, pkt->data, pkt->size);
  1222. if (!ctx->version) {
  1223. int to_store = 0;
  1224. while (bytestream2_get_bytes_left(&ctx->gb) >= 8) {
  1225. uint32_t sig, size;
  1226. int pos;
  1227. sig = bytestream2_get_be32u(&ctx->gb);
  1228. size = bytestream2_get_be32u(&ctx->gb);
  1229. pos = bytestream2_tell(&ctx->gb);
  1230. if (bytestream2_get_bytes_left(&ctx->gb) < size) {
  1231. av_log(avctx, AV_LOG_ERROR, "Incorrect chunk size %"PRIu32".\n", size);
  1232. break;
  1233. }
  1234. switch (sig) {
  1235. case MKBETAG('N', 'P', 'A', 'L'):
  1236. if (size != PALETTE_SIZE * 3) {
  1237. av_log(avctx, AV_LOG_ERROR,
  1238. "Incorrect palette block size %"PRIu32".\n", size);
  1239. return AVERROR_INVALIDDATA;
  1240. }
  1241. for (i = 0; i < PALETTE_SIZE; i++)
  1242. ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
  1243. break;
  1244. case MKBETAG('F', 'O', 'B', 'J'):
  1245. if (size < 16)
  1246. return AVERROR_INVALIDDATA;
  1247. if (ret = process_frame_obj(ctx))
  1248. return ret;
  1249. break;
  1250. case MKBETAG('X', 'P', 'A', 'L'):
  1251. if (size == 6 || size == 4) {
  1252. uint8_t tmp[3];
  1253. int j;
  1254. for (i = 0; i < PALETTE_SIZE; i++) {
  1255. for (j = 0; j < 3; j++) {
  1256. int t = (ctx->pal[i] >> (16 - j * 8)) & 0xFF;
  1257. tmp[j] = av_clip_uint8((t * 129 + ctx->delta_pal[i * 3 + j]) >> 7);
  1258. }
  1259. ctx->pal[i] = 0xFFU << 24 | AV_RB24(tmp);
  1260. }
  1261. } else {
  1262. if (size < PALETTE_DELTA * 2 + 4) {
  1263. av_log(avctx, AV_LOG_ERROR,
  1264. "Incorrect palette change block size %"PRIu32".\n",
  1265. size);
  1266. return AVERROR_INVALIDDATA;
  1267. }
  1268. bytestream2_skipu(&ctx->gb, 4);
  1269. for (i = 0; i < PALETTE_DELTA; i++)
  1270. ctx->delta_pal[i] = bytestream2_get_le16u(&ctx->gb);
  1271. if (size >= PALETTE_DELTA * 5 + 4) {
  1272. for (i = 0; i < PALETTE_SIZE; i++)
  1273. ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
  1274. } else {
  1275. memset(ctx->pal, 0, sizeof(ctx->pal));
  1276. }
  1277. }
  1278. break;
  1279. case MKBETAG('S', 'T', 'O', 'R'):
  1280. to_store = 1;
  1281. break;
  1282. case MKBETAG('F', 'T', 'C', 'H'):
  1283. memcpy(ctx->frm0, ctx->stored_frame, ctx->buf_size);
  1284. break;
  1285. default:
  1286. bytestream2_skip(&ctx->gb, size);
  1287. av_log(avctx, AV_LOG_DEBUG,
  1288. "Unknown/unsupported chunk %"PRIx32".\n", sig);
  1289. break;
  1290. }
  1291. bytestream2_seek(&ctx->gb, pos + size, SEEK_SET);
  1292. if (size & 1)
  1293. bytestream2_skip(&ctx->gb, 1);
  1294. }
  1295. if (to_store)
  1296. memcpy(ctx->stored_frame, ctx->frm0, ctx->buf_size);
  1297. if ((ret = copy_output(ctx, NULL)))
  1298. return ret;
  1299. memcpy(ctx->frame->data[1], ctx->pal, 1024);
  1300. } else {
  1301. SANMFrameHeader header;
  1302. if ((ret = read_frame_header(ctx, &header)))
  1303. return ret;
  1304. ctx->rotate_code = header.rotate_code;
  1305. if ((ctx->frame->key_frame = !header.seq_num)) {
  1306. ctx->frame->pict_type = AV_PICTURE_TYPE_I;
  1307. fill_frame(ctx->frm1, ctx->npixels, header.bg_color);
  1308. fill_frame(ctx->frm2, ctx->npixels, header.bg_color);
  1309. } else {
  1310. ctx->frame->pict_type = AV_PICTURE_TYPE_P;
  1311. }
  1312. if (header.codec < FF_ARRAY_ELEMS(v1_decoders)) {
  1313. if ((ret = v1_decoders[header.codec](ctx))) {
  1314. av_log(avctx, AV_LOG_ERROR,
  1315. "Subcodec %d: error decoding frame.\n", header.codec);
  1316. return ret;
  1317. }
  1318. } else {
  1319. avpriv_request_sample(avctx, "Subcodec %d", header.codec);
  1320. return AVERROR_PATCHWELCOME;
  1321. }
  1322. if ((ret = copy_output(ctx, &header)))
  1323. return ret;
  1324. }
  1325. if (ctx->rotate_code)
  1326. rotate_bufs(ctx, ctx->rotate_code);
  1327. *got_frame_ptr = 1;
  1328. return pkt->size;
  1329. }
  1330. AVCodec ff_sanm_decoder = {
  1331. .name = "sanm",
  1332. .long_name = NULL_IF_CONFIG_SMALL("LucasArts SANM/Smush video"),
  1333. .type = AVMEDIA_TYPE_VIDEO,
  1334. .id = AV_CODEC_ID_SANM,
  1335. .priv_data_size = sizeof(SANMVideoContext),
  1336. .init = decode_init,
  1337. .close = decode_end,
  1338. .decode = decode_frame,
  1339. .capabilities = CODEC_CAP_DR1,
  1340. };