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.

1475 lines
52KB

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