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
53KB

  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. ptrdiff_t 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. init_sizes(ctx, 0, 0);
  421. }
  422. static av_cold int init_buffers(SANMVideoContext *ctx)
  423. {
  424. av_fast_padded_mallocz(&ctx->frm0, &ctx->frm0_size, ctx->buf_size);
  425. av_fast_padded_mallocz(&ctx->frm1, &ctx->frm1_size, ctx->buf_size);
  426. av_fast_padded_mallocz(&ctx->frm2, &ctx->frm2_size, ctx->buf_size);
  427. if (!ctx->version)
  428. av_fast_padded_mallocz(&ctx->stored_frame,
  429. &ctx->stored_frame_size, ctx->buf_size);
  430. if (!ctx->frm0 || !ctx->frm1 || !ctx->frm2 ||
  431. (!ctx->stored_frame && !ctx->version)) {
  432. destroy_buffers(ctx);
  433. return AVERROR(ENOMEM);
  434. }
  435. return 0;
  436. }
  437. static void rotate_bufs(SANMVideoContext *ctx, int rotate_code)
  438. {
  439. if (rotate_code == 2)
  440. FFSWAP(uint16_t*, ctx->frm1, ctx->frm2);
  441. FFSWAP(uint16_t*, ctx->frm2, ctx->frm0);
  442. }
  443. static av_cold int decode_init(AVCodecContext *avctx)
  444. {
  445. SANMVideoContext *ctx = avctx->priv_data;
  446. ctx->avctx = avctx;
  447. ctx->version = !avctx->extradata_size;
  448. avctx->pix_fmt = ctx->version ? AV_PIX_FMT_RGB565 : AV_PIX_FMT_PAL8;
  449. init_sizes(ctx, avctx->width, avctx->height);
  450. if (init_buffers(ctx)) {
  451. av_log(avctx, AV_LOG_ERROR, "Error allocating buffers.\n");
  452. return AVERROR(ENOMEM);
  453. }
  454. make_glyphs(ctx->p4x4glyphs[0], glyph4_x, glyph4_y, 4);
  455. make_glyphs(ctx->p8x8glyphs[0], glyph8_x, glyph8_y, 8);
  456. if (!ctx->version) {
  457. int i;
  458. if (avctx->extradata_size < 1026) {
  459. av_log(avctx, AV_LOG_ERROR, "Not enough extradata.\n");
  460. return AVERROR_INVALIDDATA;
  461. }
  462. ctx->subversion = AV_RL16(avctx->extradata);
  463. for (i = 0; i < PALETTE_SIZE; i++)
  464. ctx->pal[i] = 0xFFU << 24 | AV_RL32(avctx->extradata + 2 + i * 4);
  465. }
  466. return 0;
  467. }
  468. static av_cold int decode_end(AVCodecContext *avctx)
  469. {
  470. SANMVideoContext *ctx = avctx->priv_data;
  471. destroy_buffers(ctx);
  472. return 0;
  473. }
  474. static int rle_decode(SANMVideoContext *ctx, uint8_t *dst, const int out_size)
  475. {
  476. int opcode, color, run_len, left = out_size;
  477. while (left > 0) {
  478. opcode = bytestream2_get_byte(&ctx->gb);
  479. run_len = (opcode >> 1) + 1;
  480. if (run_len > left || bytestream2_get_bytes_left(&ctx->gb) <= 0)
  481. return AVERROR_INVALIDDATA;
  482. if (opcode & 1) {
  483. color = bytestream2_get_byte(&ctx->gb);
  484. memset(dst, color, run_len);
  485. } else {
  486. if (bytestream2_get_bytes_left(&ctx->gb) < run_len)
  487. return AVERROR_INVALIDDATA;
  488. bytestream2_get_bufferu(&ctx->gb, dst, run_len);
  489. }
  490. dst += run_len;
  491. left -= run_len;
  492. }
  493. return 0;
  494. }
  495. static int old_codec1(SANMVideoContext *ctx, int top,
  496. int left, int width, int height)
  497. {
  498. uint8_t *dst = ((uint8_t *)ctx->frm0) + left + top * ctx->pitch;
  499. int i, j, len, flag, code, val, pos, end;
  500. for (i = 0; i < height; i++) {
  501. pos = 0;
  502. if (bytestream2_get_bytes_left(&ctx->gb) < 2)
  503. return AVERROR_INVALIDDATA;
  504. len = bytestream2_get_le16u(&ctx->gb);
  505. end = bytestream2_tell(&ctx->gb) + len;
  506. while (bytestream2_tell(&ctx->gb) < end) {
  507. if (bytestream2_get_bytes_left(&ctx->gb) < 2)
  508. return AVERROR_INVALIDDATA;
  509. code = bytestream2_get_byteu(&ctx->gb);
  510. flag = code & 1;
  511. code = (code >> 1) + 1;
  512. if (pos + code > width)
  513. return AVERROR_INVALIDDATA;
  514. if (flag) {
  515. val = bytestream2_get_byteu(&ctx->gb);
  516. if (val)
  517. memset(dst + pos, val, code);
  518. pos += code;
  519. } else {
  520. if (bytestream2_get_bytes_left(&ctx->gb) < code)
  521. return AVERROR_INVALIDDATA;
  522. for (j = 0; j < code; j++) {
  523. val = bytestream2_get_byteu(&ctx->gb);
  524. if (val)
  525. dst[pos] = val;
  526. pos++;
  527. }
  528. }
  529. }
  530. dst += ctx->pitch;
  531. }
  532. ctx->rotate_code = 0;
  533. return 0;
  534. }
  535. static inline void codec37_mv(uint8_t *dst, const uint8_t *src,
  536. int height, int stride, int x, int y)
  537. {
  538. int pos, i, j;
  539. pos = x + y * stride;
  540. for (j = 0; j < 4; j++) {
  541. for (i = 0; i < 4; i++) {
  542. if ((pos + i) < 0 || (pos + i) >= height * stride)
  543. dst[i] = 0;
  544. else
  545. dst[i] = src[i];
  546. }
  547. dst += stride;
  548. src += stride;
  549. pos += stride;
  550. }
  551. }
  552. static int old_codec37(SANMVideoContext *ctx, int top,
  553. int left, int width, int height)
  554. {
  555. ptrdiff_t stride = ctx->pitch;
  556. int i, j, k, t;
  557. uint8_t *dst, *prev;
  558. int skip_run = 0;
  559. int compr = bytestream2_get_byte(&ctx->gb);
  560. int mvoff = bytestream2_get_byte(&ctx->gb);
  561. int seq = bytestream2_get_le16(&ctx->gb);
  562. uint32_t decoded_size = bytestream2_get_le32(&ctx->gb);
  563. int flags;
  564. bytestream2_skip(&ctx->gb, 4);
  565. flags = bytestream2_get_byte(&ctx->gb);
  566. bytestream2_skip(&ctx->gb, 3);
  567. if (decoded_size > ctx->height * stride - left - top * stride) {
  568. decoded_size = ctx->height * stride - left - top * stride;
  569. av_log(ctx->avctx, AV_LOG_WARNING, "Decoded size is too large.\n");
  570. }
  571. ctx->rotate_code = 0;
  572. if (((seq & 1) || !(flags & 1)) && (compr && compr != 2))
  573. rotate_bufs(ctx, 1);
  574. dst = ((uint8_t*)ctx->frm0) + left + top * stride;
  575. prev = ((uint8_t*)ctx->frm2) + left + top * stride;
  576. if (mvoff > 2) {
  577. av_log(ctx->avctx, AV_LOG_ERROR, "Invalid motion base value %d.\n", mvoff);
  578. return AVERROR_INVALIDDATA;
  579. }
  580. switch (compr) {
  581. case 0:
  582. for (i = 0; i < height; i++) {
  583. bytestream2_get_buffer(&ctx->gb, dst, width);
  584. dst += stride;
  585. }
  586. memset(ctx->frm1, 0, ctx->height * stride);
  587. memset(ctx->frm2, 0, ctx->height * stride);
  588. break;
  589. case 2:
  590. if (rle_decode(ctx, dst, decoded_size))
  591. return AVERROR_INVALIDDATA;
  592. memset(ctx->frm1, 0, ctx->frm1_size);
  593. memset(ctx->frm2, 0, ctx->frm2_size);
  594. break;
  595. case 3:
  596. case 4:
  597. if (flags & 4) {
  598. for (j = 0; j < height; j += 4) {
  599. for (i = 0; i < width; i += 4) {
  600. int code;
  601. if (skip_run) {
  602. skip_run--;
  603. copy_block4(dst + i, prev + i, stride, stride, 4);
  604. continue;
  605. }
  606. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  607. return AVERROR_INVALIDDATA;
  608. code = bytestream2_get_byteu(&ctx->gb);
  609. switch (code) {
  610. case 0xFF:
  611. if (bytestream2_get_bytes_left(&ctx->gb) < 16)
  612. return AVERROR_INVALIDDATA;
  613. for (k = 0; k < 4; k++)
  614. bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
  615. break;
  616. case 0xFE:
  617. if (bytestream2_get_bytes_left(&ctx->gb) < 4)
  618. return AVERROR_INVALIDDATA;
  619. for (k = 0; k < 4; k++)
  620. memset(dst + i + k * stride, bytestream2_get_byteu(&ctx->gb), 4);
  621. break;
  622. case 0xFD:
  623. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  624. return AVERROR_INVALIDDATA;
  625. t = bytestream2_get_byteu(&ctx->gb);
  626. for (k = 0; k < 4; k++)
  627. memset(dst + i + k * stride, t, 4);
  628. break;
  629. default:
  630. if (compr == 4 && !code) {
  631. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  632. return AVERROR_INVALIDDATA;
  633. skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
  634. i -= 4;
  635. } else {
  636. int mx, my;
  637. mx = c37_mv[(mvoff * 255 + code) * 2];
  638. my = c37_mv[(mvoff * 255 + code) * 2 + 1];
  639. codec37_mv(dst + i, prev + i + mx + my * stride,
  640. ctx->height, stride, i + mx, j + my);
  641. }
  642. }
  643. }
  644. dst += stride * 4;
  645. prev += stride * 4;
  646. }
  647. } else {
  648. for (j = 0; j < height; j += 4) {
  649. for (i = 0; i < width; i += 4) {
  650. int code;
  651. if (skip_run) {
  652. skip_run--;
  653. copy_block4(dst + i, prev + i, stride, stride, 4);
  654. continue;
  655. }
  656. code = bytestream2_get_byte(&ctx->gb);
  657. if (code == 0xFF) {
  658. if (bytestream2_get_bytes_left(&ctx->gb) < 16)
  659. return AVERROR_INVALIDDATA;
  660. for (k = 0; k < 4; k++)
  661. bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
  662. } else if (compr == 4 && !code) {
  663. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  664. return AVERROR_INVALIDDATA;
  665. skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
  666. i -= 4;
  667. } else {
  668. int mx, my;
  669. mx = c37_mv[(mvoff * 255 + code) * 2];
  670. my = c37_mv[(mvoff * 255 + code) * 2 + 1];
  671. codec37_mv(dst + i, prev + i + mx + my * stride,
  672. ctx->height, stride, i + mx, j + my);
  673. }
  674. }
  675. dst += stride * 4;
  676. prev += stride * 4;
  677. }
  678. }
  679. break;
  680. default:
  681. avpriv_report_missing_feature(ctx->avctx,
  682. "Subcodec 37 compression %d", compr);
  683. return AVERROR_PATCHWELCOME;
  684. }
  685. return 0;
  686. }
  687. static int process_block(SANMVideoContext *ctx, uint8_t *dst, uint8_t *prev1,
  688. uint8_t *prev2, int stride, int tbl, int size)
  689. {
  690. int code, k, t;
  691. uint8_t colors[2];
  692. int8_t *pglyph;
  693. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  694. return AVERROR_INVALIDDATA;
  695. code = bytestream2_get_byteu(&ctx->gb);
  696. if (code >= 0xF8) {
  697. switch (code) {
  698. case 0xFF:
  699. if (size == 2) {
  700. if (bytestream2_get_bytes_left(&ctx->gb) < 4)
  701. return AVERROR_INVALIDDATA;
  702. dst[0] = bytestream2_get_byteu(&ctx->gb);
  703. dst[1] = bytestream2_get_byteu(&ctx->gb);
  704. dst[0 + stride] = bytestream2_get_byteu(&ctx->gb);
  705. dst[1 + stride] = bytestream2_get_byteu(&ctx->gb);
  706. } else {
  707. size >>= 1;
  708. if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
  709. return AVERROR_INVALIDDATA;
  710. if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
  711. stride, tbl, size))
  712. return AVERROR_INVALIDDATA;
  713. dst += size * stride;
  714. prev1 += size * stride;
  715. prev2 += size * stride;
  716. if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
  717. return AVERROR_INVALIDDATA;
  718. if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
  719. stride, tbl, size))
  720. return AVERROR_INVALIDDATA;
  721. }
  722. break;
  723. case 0xFE:
  724. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  725. return AVERROR_INVALIDDATA;
  726. t = bytestream2_get_byteu(&ctx->gb);
  727. for (k = 0; k < size; k++)
  728. memset(dst + k * stride, t, size);
  729. break;
  730. case 0xFD:
  731. if (bytestream2_get_bytes_left(&ctx->gb) < 3)
  732. return AVERROR_INVALIDDATA;
  733. code = bytestream2_get_byteu(&ctx->gb);
  734. pglyph = (size == 8) ? ctx->p8x8glyphs[code] : ctx->p4x4glyphs[code];
  735. bytestream2_get_bufferu(&ctx->gb, colors, 2);
  736. for (k = 0; k < size; k++)
  737. for (t = 0; t < size; t++)
  738. dst[t + k * stride] = colors[!*pglyph++];
  739. break;
  740. case 0xFC:
  741. for (k = 0; k < size; k++)
  742. memcpy(dst + k * stride, prev1 + k * stride, size);
  743. break;
  744. default:
  745. k = bytestream2_tell(&ctx->gb);
  746. bytestream2_seek(&ctx->gb, tbl + (code & 7), SEEK_SET);
  747. t = bytestream2_get_byte(&ctx->gb);
  748. bytestream2_seek(&ctx->gb, k, SEEK_SET);
  749. for (k = 0; k < size; k++)
  750. memset(dst + k * stride, t, size);
  751. }
  752. } else {
  753. int mx = motion_vectors[code][0];
  754. int my = motion_vectors[code][1];
  755. int index = prev2 - (const uint8_t *)ctx->frm2;
  756. av_assert2(index >= 0 && index < (ctx->buf_size >> 1));
  757. if (index < -mx - my * stride ||
  758. (ctx->buf_size >> 1) - index < mx + size + (my + size - 1) * stride) {
  759. av_log(ctx->avctx, AV_LOG_ERROR, "MV is invalid.\n");
  760. return AVERROR_INVALIDDATA;
  761. }
  762. for (k = 0; k < size; k++)
  763. memcpy(dst + k * stride, prev2 + mx + (my + k) * stride, size);
  764. }
  765. return 0;
  766. }
  767. static int old_codec47(SANMVideoContext *ctx, int top,
  768. int left, int width, int height)
  769. {
  770. uint32_t decoded_size;
  771. int i, j;
  772. ptrdiff_t stride = ctx->pitch;
  773. uint8_t *dst = (uint8_t *)ctx->frm0 + left + top * stride;
  774. uint8_t *prev1 = (uint8_t *)ctx->frm1;
  775. uint8_t *prev2 = (uint8_t *)ctx->frm2;
  776. int tbl_pos = bytestream2_tell(&ctx->gb);
  777. int seq = bytestream2_get_le16(&ctx->gb);
  778. int compr = bytestream2_get_byte(&ctx->gb);
  779. int new_rot = bytestream2_get_byte(&ctx->gb);
  780. int skip = bytestream2_get_byte(&ctx->gb);
  781. bytestream2_skip(&ctx->gb, 9);
  782. decoded_size = bytestream2_get_le32(&ctx->gb);
  783. bytestream2_skip(&ctx->gb, 8);
  784. if (decoded_size > ctx->height * stride - left - top * stride) {
  785. decoded_size = ctx->height * stride - left - top * stride;
  786. av_log(ctx->avctx, AV_LOG_WARNING, "Decoded size is too large.\n");
  787. }
  788. if (skip & 1)
  789. bytestream2_skip(&ctx->gb, 0x8080);
  790. if (!seq) {
  791. ctx->prev_seq = -1;
  792. memset(prev1, 0, ctx->height * stride);
  793. memset(prev2, 0, ctx->height * stride);
  794. }
  795. switch (compr) {
  796. case 0:
  797. if (bytestream2_get_bytes_left(&ctx->gb) < width * height)
  798. return AVERROR_INVALIDDATA;
  799. for (j = 0; j < height; j++) {
  800. bytestream2_get_bufferu(&ctx->gb, dst, width);
  801. dst += stride;
  802. }
  803. break;
  804. case 1:
  805. if (bytestream2_get_bytes_left(&ctx->gb) < ((width + 1) >> 1) * ((height + 1) >> 1))
  806. return AVERROR_INVALIDDATA;
  807. for (j = 0; j < height; j += 2) {
  808. for (i = 0; i < width; i += 2) {
  809. dst[i] =
  810. dst[i + 1] =
  811. dst[stride + i] =
  812. dst[stride + i + 1] = bytestream2_get_byteu(&ctx->gb);
  813. }
  814. dst += stride * 2;
  815. }
  816. break;
  817. case 2:
  818. if (seq == ctx->prev_seq + 1) {
  819. for (j = 0; j < height; j += 8) {
  820. for (i = 0; i < width; i += 8)
  821. if (process_block(ctx, dst + i, prev1 + i, prev2 + i, stride,
  822. tbl_pos + 8, 8))
  823. return AVERROR_INVALIDDATA;
  824. dst += stride * 8;
  825. prev1 += stride * 8;
  826. prev2 += stride * 8;
  827. }
  828. }
  829. break;
  830. case 3:
  831. memcpy(ctx->frm0, ctx->frm2, ctx->pitch * ctx->height);
  832. break;
  833. case 4:
  834. memcpy(ctx->frm0, ctx->frm1, ctx->pitch * ctx->height);
  835. break;
  836. case 5:
  837. if (rle_decode(ctx, dst, decoded_size))
  838. return AVERROR_INVALIDDATA;
  839. break;
  840. default:
  841. avpriv_report_missing_feature(ctx->avctx,
  842. "Subcodec 47 compression %d", compr);
  843. return AVERROR_PATCHWELCOME;
  844. }
  845. if (seq == ctx->prev_seq + 1)
  846. ctx->rotate_code = new_rot;
  847. else
  848. ctx->rotate_code = 0;
  849. ctx->prev_seq = seq;
  850. return 0;
  851. }
  852. static int process_frame_obj(SANMVideoContext *ctx)
  853. {
  854. uint16_t codec = bytestream2_get_le16u(&ctx->gb);
  855. uint16_t left = bytestream2_get_le16u(&ctx->gb);
  856. uint16_t top = bytestream2_get_le16u(&ctx->gb);
  857. uint16_t w = bytestream2_get_le16u(&ctx->gb);
  858. uint16_t h = bytestream2_get_le16u(&ctx->gb);
  859. if (!w || !h) {
  860. av_log(ctx->avctx, AV_LOG_ERROR, "Dimensions are invalid.\n");
  861. return AVERROR_INVALIDDATA;
  862. }
  863. if (ctx->width < left + w || ctx->height < top + h) {
  864. int ret = ff_set_dimensions(ctx->avctx, FFMAX(left + w, ctx->width),
  865. FFMAX(top + h, ctx->height));
  866. if (ret < 0)
  867. return ret;
  868. init_sizes(ctx, FFMAX(left + w, ctx->width),
  869. FFMAX(top + h, ctx->height));
  870. if (init_buffers(ctx)) {
  871. av_log(ctx->avctx, AV_LOG_ERROR, "Error resizing buffers.\n");
  872. return AVERROR(ENOMEM);
  873. }
  874. }
  875. bytestream2_skip(&ctx->gb, 4);
  876. switch (codec) {
  877. case 1:
  878. case 3:
  879. return old_codec1(ctx, top, left, w, h);
  880. case 37:
  881. return old_codec37(ctx, top, left, w, h);
  882. case 47:
  883. return old_codec47(ctx, top, left, w, h);
  884. default:
  885. avpriv_request_sample(ctx->avctx, "Subcodec %d", codec);
  886. return AVERROR_PATCHWELCOME;
  887. }
  888. }
  889. static int decode_0(SANMVideoContext *ctx)
  890. {
  891. uint16_t *frm = ctx->frm0;
  892. int x, y;
  893. if (bytestream2_get_bytes_left(&ctx->gb) < ctx->width * ctx->height * 2) {
  894. av_log(ctx->avctx, AV_LOG_ERROR, "Insufficient data for raw frame.\n");
  895. return AVERROR_INVALIDDATA;
  896. }
  897. for (y = 0; y < ctx->height; y++) {
  898. for (x = 0; x < ctx->width; x++)
  899. frm[x] = bytestream2_get_le16u(&ctx->gb);
  900. frm += ctx->pitch;
  901. }
  902. return 0;
  903. }
  904. static int decode_nop(SANMVideoContext *ctx)
  905. {
  906. avpriv_request_sample(ctx->avctx, "Unknown/unsupported compression type");
  907. return AVERROR_PATCHWELCOME;
  908. }
  909. static void copy_block(uint16_t *pdest, uint16_t *psrc, int block_size, ptrdiff_t pitch)
  910. {
  911. uint8_t *dst = (uint8_t *)pdest;
  912. uint8_t *src = (uint8_t *)psrc;
  913. ptrdiff_t stride = pitch * 2;
  914. switch (block_size) {
  915. case 2:
  916. copy_block4(dst, src, stride, stride, 2);
  917. break;
  918. case 4:
  919. copy_block8(dst, src, stride, stride, 4);
  920. break;
  921. case 8:
  922. copy_block16(dst, src, stride, stride, 8);
  923. break;
  924. }
  925. }
  926. static void fill_block(uint16_t *pdest, uint16_t color, int block_size, ptrdiff_t pitch)
  927. {
  928. int x, y;
  929. pitch -= block_size;
  930. for (y = 0; y < block_size; y++, pdest += pitch)
  931. for (x = 0; x < block_size; x++)
  932. *pdest++ = color;
  933. }
  934. static int draw_glyph(SANMVideoContext *ctx, uint16_t *dst, int index,
  935. uint16_t fg_color, uint16_t bg_color, int block_size,
  936. ptrdiff_t pitch)
  937. {
  938. int8_t *pglyph;
  939. uint16_t colors[2] = { fg_color, bg_color };
  940. int x, y;
  941. if (index >= NGLYPHS) {
  942. av_log(ctx->avctx, AV_LOG_ERROR, "Ignoring nonexistent glyph #%u.\n", index);
  943. return AVERROR_INVALIDDATA;
  944. }
  945. pglyph = block_size == 8 ? ctx->p8x8glyphs[index] : ctx->p4x4glyphs[index];
  946. pitch -= block_size;
  947. for (y = 0; y < block_size; y++, dst += pitch)
  948. for (x = 0; x < block_size; x++)
  949. *dst++ = colors[*pglyph++];
  950. return 0;
  951. }
  952. static int opcode_0xf7(SANMVideoContext *ctx, int cx, int cy, int block_size, ptrdiff_t pitch)
  953. {
  954. uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
  955. if (block_size == 2) {
  956. uint32_t indices;
  957. if (bytestream2_get_bytes_left(&ctx->gb) < 4)
  958. return AVERROR_INVALIDDATA;
  959. indices = bytestream2_get_le32u(&ctx->gb);
  960. dst[0] = ctx->codebook[indices & 0xFF];
  961. indices >>= 8;
  962. dst[1] = ctx->codebook[indices & 0xFF];
  963. indices >>= 8;
  964. dst[pitch] = ctx->codebook[indices & 0xFF];
  965. indices >>= 8;
  966. dst[pitch + 1] = ctx->codebook[indices & 0xFF];
  967. } else {
  968. uint16_t fgcolor, bgcolor;
  969. int glyph;
  970. if (bytestream2_get_bytes_left(&ctx->gb) < 3)
  971. return AVERROR_INVALIDDATA;
  972. glyph = bytestream2_get_byteu(&ctx->gb);
  973. bgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
  974. fgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
  975. draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
  976. }
  977. return 0;
  978. }
  979. static int opcode_0xf8(SANMVideoContext *ctx, int cx, int cy, int block_size, ptrdiff_t pitch)
  980. {
  981. uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
  982. if (block_size == 2) {
  983. if (bytestream2_get_bytes_left(&ctx->gb) < 8)
  984. return AVERROR_INVALIDDATA;
  985. dst[0] = bytestream2_get_le16u(&ctx->gb);
  986. dst[1] = bytestream2_get_le16u(&ctx->gb);
  987. dst[pitch] = bytestream2_get_le16u(&ctx->gb);
  988. dst[pitch + 1] = bytestream2_get_le16u(&ctx->gb);
  989. } else {
  990. uint16_t fgcolor, bgcolor;
  991. int glyph;
  992. if (bytestream2_get_bytes_left(&ctx->gb) < 5)
  993. return AVERROR_INVALIDDATA;
  994. glyph = bytestream2_get_byteu(&ctx->gb);
  995. bgcolor = bytestream2_get_le16u(&ctx->gb);
  996. fgcolor = bytestream2_get_le16u(&ctx->gb);
  997. draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
  998. }
  999. return 0;
  1000. }
  1001. static int good_mvec(SANMVideoContext *ctx, int cx, int cy, int mx, int my,
  1002. int block_size)
  1003. {
  1004. int start_pos = cx + mx + (cy + my) * ctx->pitch;
  1005. int end_pos = start_pos + (block_size - 1) * (ctx->pitch + 1);
  1006. int good = start_pos >= 0 && end_pos < (ctx->buf_size >> 1);
  1007. if (!good)
  1008. av_log(ctx->avctx, AV_LOG_ERROR,
  1009. "Ignoring invalid motion vector (%i, %i)->(%u, %u), block size = %u\n",
  1010. cx + mx, cy + my, cx, cy, block_size);
  1011. return good;
  1012. }
  1013. static int codec2subblock(SANMVideoContext *ctx, int cx, int cy, int blk_size)
  1014. {
  1015. int16_t mx, my, index;
  1016. int opcode;
  1017. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  1018. return AVERROR_INVALIDDATA;
  1019. opcode = bytestream2_get_byteu(&ctx->gb);
  1020. switch (opcode) {
  1021. default:
  1022. mx = motion_vectors[opcode][0];
  1023. my = motion_vectors[opcode][1];
  1024. if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
  1025. copy_block(ctx->frm0 + cx + ctx->pitch * cy,
  1026. ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
  1027. blk_size, ctx->pitch);
  1028. }
  1029. break;
  1030. case 0xF5:
  1031. if (bytestream2_get_bytes_left(&ctx->gb) < 2)
  1032. return AVERROR_INVALIDDATA;
  1033. index = bytestream2_get_le16u(&ctx->gb);
  1034. mx = index % ctx->width;
  1035. my = index / ctx->width;
  1036. if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
  1037. copy_block(ctx->frm0 + cx + ctx->pitch * cy,
  1038. ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
  1039. blk_size, ctx->pitch);
  1040. }
  1041. break;
  1042. case 0xF6:
  1043. copy_block(ctx->frm0 + cx + ctx->pitch * cy,
  1044. ctx->frm1 + cx + ctx->pitch * cy,
  1045. blk_size, ctx->pitch);
  1046. break;
  1047. case 0xF7:
  1048. opcode_0xf7(ctx, cx, cy, blk_size, ctx->pitch);
  1049. break;
  1050. case 0xF8:
  1051. opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
  1052. break;
  1053. case 0xF9:
  1054. case 0xFA:
  1055. case 0xFB:
  1056. case 0xFC:
  1057. fill_block(ctx->frm0 + cx + cy * ctx->pitch,
  1058. ctx->small_codebook[opcode - 0xf9], blk_size, ctx->pitch);
  1059. break;
  1060. case 0xFD:
  1061. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  1062. return AVERROR_INVALIDDATA;
  1063. fill_block(ctx->frm0 + cx + cy * ctx->pitch,
  1064. ctx->codebook[bytestream2_get_byteu(&ctx->gb)], blk_size, ctx->pitch);
  1065. break;
  1066. case 0xFE:
  1067. if (bytestream2_get_bytes_left(&ctx->gb) < 2)
  1068. return AVERROR_INVALIDDATA;
  1069. fill_block(ctx->frm0 + cx + cy * ctx->pitch,
  1070. bytestream2_get_le16u(&ctx->gb), blk_size, ctx->pitch);
  1071. break;
  1072. case 0xFF:
  1073. if (blk_size == 2) {
  1074. opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
  1075. } else {
  1076. blk_size >>= 1;
  1077. if (codec2subblock(ctx, cx, cy, blk_size))
  1078. return AVERROR_INVALIDDATA;
  1079. if (codec2subblock(ctx, cx + blk_size, cy, blk_size))
  1080. return AVERROR_INVALIDDATA;
  1081. if (codec2subblock(ctx, cx, cy + blk_size, blk_size))
  1082. return AVERROR_INVALIDDATA;
  1083. if (codec2subblock(ctx, cx + blk_size, cy + blk_size, blk_size))
  1084. return AVERROR_INVALIDDATA;
  1085. }
  1086. break;
  1087. }
  1088. return 0;
  1089. }
  1090. static int decode_2(SANMVideoContext *ctx)
  1091. {
  1092. int cx, cy, ret;
  1093. for (cy = 0; cy < ctx->aligned_height; cy += 8)
  1094. for (cx = 0; cx < ctx->aligned_width; cx += 8)
  1095. if (ret = codec2subblock(ctx, cx, cy, 8))
  1096. return ret;
  1097. return 0;
  1098. }
  1099. static int decode_3(SANMVideoContext *ctx)
  1100. {
  1101. memcpy(ctx->frm0, ctx->frm2, ctx->frm2_size);
  1102. return 0;
  1103. }
  1104. static int decode_4(SANMVideoContext *ctx)
  1105. {
  1106. memcpy(ctx->frm0, ctx->frm1, ctx->frm1_size);
  1107. return 0;
  1108. }
  1109. static int decode_5(SANMVideoContext *ctx)
  1110. {
  1111. #if HAVE_BIGENDIAN
  1112. uint16_t *frm;
  1113. int npixels;
  1114. #endif
  1115. uint8_t *dst = (uint8_t*)ctx->frm0;
  1116. if (rle_decode(ctx, dst, ctx->buf_size))
  1117. return AVERROR_INVALIDDATA;
  1118. #if HAVE_BIGENDIAN
  1119. npixels = ctx->npixels;
  1120. frm = ctx->frm0;
  1121. while (npixels--) {
  1122. *frm = av_bswap16(*frm);
  1123. frm++;
  1124. }
  1125. #endif
  1126. return 0;
  1127. }
  1128. static int decode_6(SANMVideoContext *ctx)
  1129. {
  1130. int npixels = ctx->npixels;
  1131. uint16_t *frm = ctx->frm0;
  1132. if (bytestream2_get_bytes_left(&ctx->gb) < npixels) {
  1133. av_log(ctx->avctx, AV_LOG_ERROR, "Insufficient data for frame.\n");
  1134. return AVERROR_INVALIDDATA;
  1135. }
  1136. while (npixels--)
  1137. *frm++ = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
  1138. return 0;
  1139. }
  1140. static int decode_8(SANMVideoContext *ctx)
  1141. {
  1142. uint16_t *pdest = ctx->frm0;
  1143. uint8_t *rsrc;
  1144. long npixels = ctx->npixels;
  1145. av_fast_malloc(&ctx->rle_buf, &ctx->rle_buf_size, npixels);
  1146. if (!ctx->rle_buf) {
  1147. av_log(ctx->avctx, AV_LOG_ERROR, "RLE buffer allocation failed.\n");
  1148. return AVERROR(ENOMEM);
  1149. }
  1150. rsrc = ctx->rle_buf;
  1151. if (rle_decode(ctx, rsrc, npixels))
  1152. return AVERROR_INVALIDDATA;
  1153. while (npixels--)
  1154. *pdest++ = ctx->codebook[*rsrc++];
  1155. return 0;
  1156. }
  1157. typedef int (*frm_decoder)(SANMVideoContext *ctx);
  1158. static const frm_decoder v1_decoders[] = {
  1159. decode_0, decode_nop, decode_2, decode_3, decode_4, decode_5,
  1160. decode_6, decode_nop, decode_8
  1161. };
  1162. static int read_frame_header(SANMVideoContext *ctx, SANMFrameHeader *hdr)
  1163. {
  1164. int i, ret;
  1165. if ((ret = bytestream2_get_bytes_left(&ctx->gb)) < 560) {
  1166. av_log(ctx->avctx, AV_LOG_ERROR, "Input frame too short (%d bytes).\n",
  1167. ret);
  1168. return AVERROR_INVALIDDATA;
  1169. }
  1170. bytestream2_skip(&ctx->gb, 8); // skip pad
  1171. hdr->width = bytestream2_get_le32u(&ctx->gb);
  1172. hdr->height = bytestream2_get_le32u(&ctx->gb);
  1173. if (hdr->width != ctx->width || hdr->height != ctx->height) {
  1174. avpriv_report_missing_feature(ctx->avctx, "Variable size frames");
  1175. return AVERROR_PATCHWELCOME;
  1176. }
  1177. hdr->seq_num = bytestream2_get_le16u(&ctx->gb);
  1178. hdr->codec = bytestream2_get_byteu(&ctx->gb);
  1179. hdr->rotate_code = bytestream2_get_byteu(&ctx->gb);
  1180. bytestream2_skip(&ctx->gb, 4); // skip pad
  1181. for (i = 0; i < 4; i++)
  1182. ctx->small_codebook[i] = bytestream2_get_le16u(&ctx->gb);
  1183. hdr->bg_color = bytestream2_get_le16u(&ctx->gb);
  1184. bytestream2_skip(&ctx->gb, 2); // skip pad
  1185. hdr->rle_output_size = bytestream2_get_le32u(&ctx->gb);
  1186. for (i = 0; i < 256; i++)
  1187. ctx->codebook[i] = bytestream2_get_le16u(&ctx->gb);
  1188. bytestream2_skip(&ctx->gb, 8); // skip pad
  1189. return 0;
  1190. }
  1191. static void fill_frame(uint16_t *pbuf, int buf_size, uint16_t color)
  1192. {
  1193. if (buf_size--) {
  1194. *pbuf++ = color;
  1195. av_memcpy_backptr((uint8_t*)pbuf, 2, 2*buf_size);
  1196. }
  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, height = ctx->height;
  1203. ptrdiff_t dstpitch, 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 = AV_CODEC_CAP_DR1,
  1340. };