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