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.

1478 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. int 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. int 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. int 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. break;
  829. case 37:
  830. return old_codec37(ctx, top, left, w, h);
  831. break;
  832. case 47:
  833. return old_codec47(ctx, top, left, w, h);
  834. break;
  835. default:
  836. avpriv_request_sample(ctx->avctx, "Subcodec %d", codec);
  837. return AVERROR_PATCHWELCOME;
  838. }
  839. }
  840. static int decode_0(SANMVideoContext *ctx)
  841. {
  842. uint16_t *frm = ctx->frm0;
  843. int x, y;
  844. if (bytestream2_get_bytes_left(&ctx->gb) < ctx->width * ctx->height * 2) {
  845. av_log(ctx->avctx, AV_LOG_ERROR, "Insufficient data for raw frame.\n");
  846. return AVERROR_INVALIDDATA;
  847. }
  848. for (y = 0; y < ctx->height; y++) {
  849. for (x = 0; x < ctx->width; x++)
  850. frm[x] = bytestream2_get_le16u(&ctx->gb);
  851. frm += ctx->pitch;
  852. }
  853. return 0;
  854. }
  855. static int decode_nop(SANMVideoContext *ctx)
  856. {
  857. avpriv_request_sample(ctx->avctx, "Unknown/unsupported compression type");
  858. return AVERROR_PATCHWELCOME;
  859. }
  860. static void copy_block(uint16_t *pdest, uint16_t *psrc, int block_size, int pitch)
  861. {
  862. uint8_t *dst = (uint8_t *)pdest;
  863. uint8_t *src = (uint8_t *)psrc;
  864. int stride = pitch * 2;
  865. switch (block_size) {
  866. case 2:
  867. copy_block4(dst, src, stride, stride, 2);
  868. break;
  869. case 4:
  870. copy_block8(dst, src, stride, stride, 4);
  871. break;
  872. case 8:
  873. copy_block16(dst, src, stride, stride, 8);
  874. break;
  875. }
  876. }
  877. static void fill_block(uint16_t *pdest, uint16_t color, int block_size, int pitch)
  878. {
  879. int x, y;
  880. pitch -= block_size;
  881. for (y = 0; y < block_size; y++, pdest += pitch)
  882. for (x = 0; x < block_size; x++)
  883. *pdest++ = color;
  884. }
  885. static int draw_glyph(SANMVideoContext *ctx, uint16_t *dst, int index,
  886. uint16_t fg_color, uint16_t bg_color, int block_size,
  887. int pitch)
  888. {
  889. int8_t *pglyph;
  890. uint16_t colors[2] = { fg_color, bg_color };
  891. int x, y;
  892. if (index >= NGLYPHS) {
  893. av_log(ctx->avctx, AV_LOG_ERROR, "Ignoring nonexistent glyph #%u.\n", index);
  894. return AVERROR_INVALIDDATA;
  895. }
  896. pglyph = block_size == 8 ? ctx->p8x8glyphs[index] : ctx->p4x4glyphs[index];
  897. pitch -= block_size;
  898. for (y = 0; y < block_size; y++, dst += pitch)
  899. for (x = 0; x < block_size; x++)
  900. *dst++ = colors[*pglyph++];
  901. return 0;
  902. }
  903. static int opcode_0xf7(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
  904. {
  905. uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
  906. if (block_size == 2) {
  907. uint32_t indices;
  908. if (bytestream2_get_bytes_left(&ctx->gb) < 4)
  909. return AVERROR_INVALIDDATA;
  910. indices = bytestream2_get_le32u(&ctx->gb);
  911. dst[0] = ctx->codebook[indices & 0xFF];
  912. indices >>= 8;
  913. dst[1] = ctx->codebook[indices & 0xFF];
  914. indices >>= 8;
  915. dst[pitch] = ctx->codebook[indices & 0xFF];
  916. indices >>= 8;
  917. dst[pitch + 1] = ctx->codebook[indices & 0xFF];
  918. } else {
  919. uint16_t fgcolor, bgcolor;
  920. int glyph;
  921. if (bytestream2_get_bytes_left(&ctx->gb) < 3)
  922. return AVERROR_INVALIDDATA;
  923. glyph = bytestream2_get_byteu(&ctx->gb);
  924. bgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
  925. fgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
  926. draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
  927. }
  928. return 0;
  929. }
  930. static int opcode_0xf8(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
  931. {
  932. uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
  933. if (block_size == 2) {
  934. if (bytestream2_get_bytes_left(&ctx->gb) < 8)
  935. return AVERROR_INVALIDDATA;
  936. dst[0] = bytestream2_get_le16u(&ctx->gb);
  937. dst[1] = bytestream2_get_le16u(&ctx->gb);
  938. dst[pitch] = bytestream2_get_le16u(&ctx->gb);
  939. dst[pitch + 1] = bytestream2_get_le16u(&ctx->gb);
  940. } else {
  941. uint16_t fgcolor, bgcolor;
  942. int glyph;
  943. if (bytestream2_get_bytes_left(&ctx->gb) < 5)
  944. return AVERROR_INVALIDDATA;
  945. glyph = bytestream2_get_byteu(&ctx->gb);
  946. bgcolor = bytestream2_get_le16u(&ctx->gb);
  947. fgcolor = bytestream2_get_le16u(&ctx->gb);
  948. draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
  949. }
  950. return 0;
  951. }
  952. static int good_mvec(SANMVideoContext *ctx, int cx, int cy, int mx, int my,
  953. int block_size)
  954. {
  955. int start_pos = cx + mx + (cy + my) * ctx->pitch;
  956. int end_pos = start_pos + (block_size - 1) * (ctx->pitch + 1);
  957. int good = start_pos >= 0 && end_pos < (ctx->buf_size >> 1);
  958. if (!good)
  959. av_log(ctx->avctx, AV_LOG_ERROR,
  960. "Ignoring invalid motion vector (%i, %i)->(%u, %u), block size = %u\n",
  961. cx + mx, cy + my, cx, cy, block_size);
  962. return good;
  963. }
  964. static int codec2subblock(SANMVideoContext *ctx, int cx, int cy, int blk_size)
  965. {
  966. int16_t mx, my, index;
  967. int opcode;
  968. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  969. return AVERROR_INVALIDDATA;
  970. opcode = bytestream2_get_byteu(&ctx->gb);
  971. switch (opcode) {
  972. default:
  973. mx = motion_vectors[opcode][0];
  974. my = motion_vectors[opcode][1];
  975. if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
  976. copy_block(ctx->frm0 + cx + ctx->pitch * cy,
  977. ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
  978. blk_size, ctx->pitch);
  979. }
  980. break;
  981. case 0xF5:
  982. if (bytestream2_get_bytes_left(&ctx->gb) < 2)
  983. return AVERROR_INVALIDDATA;
  984. index = bytestream2_get_le16u(&ctx->gb);
  985. mx = index % ctx->width;
  986. my = index / ctx->width;
  987. if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
  988. copy_block(ctx->frm0 + cx + ctx->pitch * cy,
  989. ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
  990. blk_size, ctx->pitch);
  991. }
  992. break;
  993. case 0xF6:
  994. copy_block(ctx->frm0 + cx + ctx->pitch * cy,
  995. ctx->frm1 + cx + ctx->pitch * cy,
  996. blk_size, ctx->pitch);
  997. break;
  998. case 0xF7:
  999. opcode_0xf7(ctx, cx, cy, blk_size, ctx->pitch);
  1000. break;
  1001. case 0xF8:
  1002. opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
  1003. break;
  1004. case 0xF9:
  1005. case 0xFA:
  1006. case 0xFB:
  1007. case 0xFC:
  1008. fill_block(ctx->frm0 + cx + cy * ctx->pitch,
  1009. ctx->small_codebook[opcode - 0xf9], blk_size, ctx->pitch);
  1010. break;
  1011. case 0xFD:
  1012. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  1013. return AVERROR_INVALIDDATA;
  1014. fill_block(ctx->frm0 + cx + cy * ctx->pitch,
  1015. ctx->codebook[bytestream2_get_byteu(&ctx->gb)], blk_size, ctx->pitch);
  1016. break;
  1017. case 0xFE:
  1018. if (bytestream2_get_bytes_left(&ctx->gb) < 2)
  1019. return AVERROR_INVALIDDATA;
  1020. fill_block(ctx->frm0 + cx + cy * ctx->pitch,
  1021. bytestream2_get_le16u(&ctx->gb), blk_size, ctx->pitch);
  1022. break;
  1023. case 0xFF:
  1024. if (blk_size == 2) {
  1025. opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
  1026. } else {
  1027. blk_size >>= 1;
  1028. if (codec2subblock(ctx, cx, cy, blk_size))
  1029. return AVERROR_INVALIDDATA;
  1030. if (codec2subblock(ctx, cx + blk_size, cy, blk_size))
  1031. return AVERROR_INVALIDDATA;
  1032. if (codec2subblock(ctx, cx, cy + blk_size, blk_size))
  1033. return AVERROR_INVALIDDATA;
  1034. if (codec2subblock(ctx, cx + blk_size, cy + blk_size, blk_size))
  1035. return AVERROR_INVALIDDATA;
  1036. }
  1037. break;
  1038. }
  1039. return 0;
  1040. }
  1041. static int decode_2(SANMVideoContext *ctx)
  1042. {
  1043. int cx, cy, ret;
  1044. for (cy = 0; cy < ctx->aligned_height; cy += 8)
  1045. for (cx = 0; cx < ctx->aligned_width; cx += 8)
  1046. if (ret = codec2subblock(ctx, cx, cy, 8))
  1047. return ret;
  1048. return 0;
  1049. }
  1050. static int decode_3(SANMVideoContext *ctx)
  1051. {
  1052. memcpy(ctx->frm0, ctx->frm2, ctx->frm2_size);
  1053. return 0;
  1054. }
  1055. static int decode_4(SANMVideoContext *ctx)
  1056. {
  1057. memcpy(ctx->frm0, ctx->frm1, ctx->frm1_size);
  1058. return 0;
  1059. }
  1060. static int decode_5(SANMVideoContext *ctx)
  1061. {
  1062. #if HAVE_BIGENDIAN
  1063. uint16_t *frm;
  1064. int npixels;
  1065. #endif
  1066. uint8_t *dst = (uint8_t*)ctx->frm0;
  1067. if (rle_decode(ctx, dst, ctx->buf_size))
  1068. return AVERROR_INVALIDDATA;
  1069. #if HAVE_BIGENDIAN
  1070. npixels = ctx->npixels;
  1071. frm = ctx->frm0;
  1072. while (npixels--) {
  1073. *frm = av_bswap16(*frm);
  1074. frm++;
  1075. }
  1076. #endif
  1077. return 0;
  1078. }
  1079. static int decode_6(SANMVideoContext *ctx)
  1080. {
  1081. int npixels = ctx->npixels;
  1082. uint16_t *frm = ctx->frm0;
  1083. if (bytestream2_get_bytes_left(&ctx->gb) < npixels) {
  1084. av_log(ctx->avctx, AV_LOG_ERROR, "Insufficient data for frame.\n");
  1085. return AVERROR_INVALIDDATA;
  1086. }
  1087. while (npixels--)
  1088. *frm++ = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
  1089. return 0;
  1090. }
  1091. static int decode_8(SANMVideoContext *ctx)
  1092. {
  1093. uint16_t *pdest = ctx->frm0;
  1094. uint8_t *rsrc;
  1095. long npixels = ctx->npixels;
  1096. av_fast_malloc(&ctx->rle_buf, &ctx->rle_buf_size, npixels);
  1097. if (!ctx->rle_buf) {
  1098. av_log(ctx->avctx, AV_LOG_ERROR, "RLE buffer allocation failed.\n");
  1099. return AVERROR(ENOMEM);
  1100. }
  1101. rsrc = ctx->rle_buf;
  1102. if (rle_decode(ctx, rsrc, npixels))
  1103. return AVERROR_INVALIDDATA;
  1104. while (npixels--)
  1105. *pdest++ = ctx->codebook[*rsrc++];
  1106. return 0;
  1107. }
  1108. typedef int (*frm_decoder)(SANMVideoContext *ctx);
  1109. static const frm_decoder v1_decoders[] = {
  1110. decode_0, decode_nop, decode_2, decode_3, decode_4, decode_5,
  1111. decode_6, decode_nop, decode_8
  1112. };
  1113. static int read_frame_header(SANMVideoContext *ctx, SANMFrameHeader *hdr)
  1114. {
  1115. int i, ret;
  1116. if ((ret = bytestream2_get_bytes_left(&ctx->gb)) < 560) {
  1117. av_log(ctx->avctx, AV_LOG_ERROR, "Input frame too short (%d bytes).\n",
  1118. ret);
  1119. return AVERROR_INVALIDDATA;
  1120. }
  1121. bytestream2_skip(&ctx->gb, 8); // skip pad
  1122. hdr->width = bytestream2_get_le32u(&ctx->gb);
  1123. hdr->height = bytestream2_get_le32u(&ctx->gb);
  1124. if (hdr->width != ctx->width || hdr->height != ctx->height) {
  1125. avpriv_report_missing_feature(ctx->avctx, "Variable size frames");
  1126. return AVERROR_PATCHWELCOME;
  1127. }
  1128. hdr->seq_num = bytestream2_get_le16u(&ctx->gb);
  1129. hdr->codec = bytestream2_get_byteu(&ctx->gb);
  1130. hdr->rotate_code = bytestream2_get_byteu(&ctx->gb);
  1131. bytestream2_skip(&ctx->gb, 4); // skip pad
  1132. for (i = 0; i < 4; i++)
  1133. ctx->small_codebook[i] = bytestream2_get_le16u(&ctx->gb);
  1134. hdr->bg_color = bytestream2_get_le16u(&ctx->gb);
  1135. bytestream2_skip(&ctx->gb, 2); // skip pad
  1136. hdr->rle_output_size = bytestream2_get_le32u(&ctx->gb);
  1137. for (i = 0; i < 256; i++)
  1138. ctx->codebook[i] = bytestream2_get_le16u(&ctx->gb);
  1139. bytestream2_skip(&ctx->gb, 8); // skip pad
  1140. return 0;
  1141. }
  1142. static void fill_frame(uint16_t *pbuf, int buf_size, uint16_t color)
  1143. {
  1144. while (buf_size--)
  1145. *pbuf++ = color;
  1146. }
  1147. static int copy_output(SANMVideoContext *ctx, SANMFrameHeader *hdr)
  1148. {
  1149. uint8_t *dst;
  1150. const uint8_t *src = (uint8_t*) ctx->frm0;
  1151. int ret, dstpitch, height = ctx->height;
  1152. int srcpitch = ctx->pitch * (hdr ? sizeof(ctx->frm0[0]) : 1);
  1153. if ((ret = ff_get_buffer(ctx->avctx, ctx->frame, 0)) < 0)
  1154. return ret;
  1155. dst = ctx->frame->data[0];
  1156. dstpitch = ctx->frame->linesize[0];
  1157. while (height--) {
  1158. memcpy(dst, src, srcpitch);
  1159. src += srcpitch;
  1160. dst += dstpitch;
  1161. }
  1162. return 0;
  1163. }
  1164. static int decode_frame(AVCodecContext *avctx, void *data,
  1165. int *got_frame_ptr, AVPacket *pkt)
  1166. {
  1167. SANMVideoContext *ctx = avctx->priv_data;
  1168. int i, ret;
  1169. ctx->frame = data;
  1170. bytestream2_init(&ctx->gb, pkt->data, pkt->size);
  1171. if (!ctx->version) {
  1172. int to_store = 0;
  1173. while (bytestream2_get_bytes_left(&ctx->gb) >= 8) {
  1174. uint32_t sig, size;
  1175. int pos;
  1176. sig = bytestream2_get_be32u(&ctx->gb);
  1177. size = bytestream2_get_be32u(&ctx->gb);
  1178. pos = bytestream2_tell(&ctx->gb);
  1179. if (bytestream2_get_bytes_left(&ctx->gb) < size) {
  1180. av_log(avctx, AV_LOG_ERROR, "Incorrect chunk size %"PRIu32".\n", size);
  1181. break;
  1182. }
  1183. switch (sig) {
  1184. case MKBETAG('N', 'P', 'A', 'L'):
  1185. if (size != PALETTE_SIZE * 3) {
  1186. av_log(avctx, AV_LOG_ERROR,
  1187. "Incorrect palette block size %"PRIu32".\n", size);
  1188. return AVERROR_INVALIDDATA;
  1189. }
  1190. for (i = 0; i < PALETTE_SIZE; i++)
  1191. ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
  1192. break;
  1193. case MKBETAG('F', 'O', 'B', 'J'):
  1194. if (size < 16)
  1195. return AVERROR_INVALIDDATA;
  1196. if (ret = process_frame_obj(ctx))
  1197. return ret;
  1198. break;
  1199. case MKBETAG('X', 'P', 'A', 'L'):
  1200. if (size == 6 || size == 4) {
  1201. uint8_t tmp[3];
  1202. int j;
  1203. for (i = 0; i < PALETTE_SIZE; i++) {
  1204. for (j = 0; j < 3; j++) {
  1205. int t = (ctx->pal[i] >> (16 - j * 8)) & 0xFF;
  1206. tmp[j] = av_clip_uint8((t * 129 + ctx->delta_pal[i * 3 + j]) >> 7);
  1207. }
  1208. ctx->pal[i] = 0xFFU << 24 | AV_RB24(tmp);
  1209. }
  1210. } else {
  1211. if (size < PALETTE_DELTA * 2 + 4) {
  1212. av_log(avctx, AV_LOG_ERROR,
  1213. "Incorrect palette change block size %"PRIu32".\n",
  1214. size);
  1215. return AVERROR_INVALIDDATA;
  1216. }
  1217. bytestream2_skipu(&ctx->gb, 4);
  1218. for (i = 0; i < PALETTE_DELTA; i++)
  1219. ctx->delta_pal[i] = bytestream2_get_le16u(&ctx->gb);
  1220. if (size >= PALETTE_DELTA * 5 + 4) {
  1221. for (i = 0; i < PALETTE_SIZE; i++)
  1222. ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
  1223. } else {
  1224. memset(ctx->pal, 0, sizeof(ctx->pal));
  1225. }
  1226. }
  1227. break;
  1228. case MKBETAG('S', 'T', 'O', 'R'):
  1229. to_store = 1;
  1230. break;
  1231. case MKBETAG('F', 'T', 'C', 'H'):
  1232. memcpy(ctx->frm0, ctx->stored_frame, ctx->buf_size);
  1233. break;
  1234. default:
  1235. bytestream2_skip(&ctx->gb, size);
  1236. av_log(avctx, AV_LOG_DEBUG,
  1237. "Unknown/unsupported chunk %"PRIx32".\n", sig);
  1238. break;
  1239. }
  1240. bytestream2_seek(&ctx->gb, pos + size, SEEK_SET);
  1241. if (size & 1)
  1242. bytestream2_skip(&ctx->gb, 1);
  1243. }
  1244. if (to_store)
  1245. memcpy(ctx->stored_frame, ctx->frm0, ctx->buf_size);
  1246. if ((ret = copy_output(ctx, NULL)))
  1247. return ret;
  1248. memcpy(ctx->frame->data[1], ctx->pal, 1024);
  1249. } else {
  1250. SANMFrameHeader header;
  1251. if ((ret = read_frame_header(ctx, &header)))
  1252. return ret;
  1253. ctx->rotate_code = header.rotate_code;
  1254. if ((ctx->frame->key_frame = !header.seq_num)) {
  1255. ctx->frame->pict_type = AV_PICTURE_TYPE_I;
  1256. fill_frame(ctx->frm1, ctx->npixels, header.bg_color);
  1257. fill_frame(ctx->frm2, ctx->npixels, header.bg_color);
  1258. } else {
  1259. ctx->frame->pict_type = AV_PICTURE_TYPE_P;
  1260. }
  1261. if (header.codec < FF_ARRAY_ELEMS(v1_decoders)) {
  1262. if ((ret = v1_decoders[header.codec](ctx))) {
  1263. av_log(avctx, AV_LOG_ERROR,
  1264. "Subcodec %d: error decoding frame.\n", header.codec);
  1265. return ret;
  1266. }
  1267. } else {
  1268. avpriv_request_sample(avctx, "Subcodec %d", header.codec);
  1269. return AVERROR_PATCHWELCOME;
  1270. }
  1271. if ((ret = copy_output(ctx, &header)))
  1272. return ret;
  1273. }
  1274. if (ctx->rotate_code)
  1275. rotate_bufs(ctx, ctx->rotate_code);
  1276. *got_frame_ptr = 1;
  1277. return pkt->size;
  1278. }
  1279. AVCodec ff_sanm_decoder = {
  1280. .name = "sanm",
  1281. .long_name = NULL_IF_CONFIG_SMALL("LucasArts SANM video"),
  1282. .type = AVMEDIA_TYPE_VIDEO,
  1283. .id = AV_CODEC_ID_SANM,
  1284. .priv_data_size = sizeof(SANMVideoContext),
  1285. .init = decode_init,
  1286. .close = decode_end,
  1287. .decode = decode_frame,
  1288. .capabilities = AV_CODEC_CAP_DR1,
  1289. };