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.

1303 lines
41KB

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