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.

1301 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 "copy_block.h"
  25. #include "bytestream.h"
  26. #include "internal.h"
  27. #include "libavutil/bswap.h"
  28. #include "libavutil/imgutils.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;
  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. ctx->frm0_size =
  201. ctx->frm1_size =
  202. ctx->frm2_size = 0;
  203. }
  204. static av_cold int init_buffers(SANMVideoContext *ctx)
  205. {
  206. av_fast_padded_malloc(&ctx->frm0, &ctx->frm0_size, ctx->buf_size);
  207. av_fast_padded_malloc(&ctx->frm1, &ctx->frm1_size, ctx->buf_size);
  208. av_fast_padded_malloc(&ctx->frm2, &ctx->frm2_size, ctx->buf_size);
  209. if (!ctx->version)
  210. av_fast_padded_malloc(&ctx->stored_frame, &ctx->stored_frame_size, ctx->buf_size);
  211. if (!ctx->frm0 || !ctx->frm1 || !ctx->frm2 || (!ctx->stored_frame && !ctx->version)) {
  212. destroy_buffers(ctx);
  213. return AVERROR(ENOMEM);
  214. }
  215. return 0;
  216. }
  217. static void rotate_bufs(SANMVideoContext *ctx, int rotate_code)
  218. {
  219. av_dlog(ctx->avctx, "rotate %d\n", rotate_code);
  220. if (rotate_code == 2)
  221. FFSWAP(uint16_t*, ctx->frm1, ctx->frm2);
  222. FFSWAP(uint16_t*, ctx->frm2, ctx->frm0);
  223. }
  224. static av_cold int decode_init(AVCodecContext *avctx)
  225. {
  226. SANMVideoContext *ctx = avctx->priv_data;
  227. ctx->avctx = avctx;
  228. ctx->version = !avctx->extradata_size;
  229. avctx->pix_fmt = ctx->version ? AV_PIX_FMT_RGB565 : AV_PIX_FMT_PAL8;
  230. init_sizes(ctx, avctx->width, avctx->height);
  231. if (init_buffers(ctx)) {
  232. av_log(avctx, AV_LOG_ERROR, "error allocating buffers\n");
  233. return AVERROR(ENOMEM);
  234. }
  235. make_glyphs(ctx->p4x4glyphs[0], glyph4_x, glyph4_y, 4);
  236. make_glyphs(ctx->p8x8glyphs[0], glyph8_x, glyph8_y, 8);
  237. if (!ctx->version) {
  238. int i;
  239. if (avctx->extradata_size < 1026) {
  240. av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");
  241. return AVERROR_INVALIDDATA;
  242. }
  243. ctx->subversion = AV_RL16(avctx->extradata);
  244. for (i = 0; i < 256; i++)
  245. ctx->pal[i] = 0xFFU << 24 | AV_RL32(avctx->extradata + 2 + i * 4);
  246. }
  247. return 0;
  248. }
  249. static av_cold int decode_end(AVCodecContext *avctx)
  250. {
  251. SANMVideoContext *ctx = avctx->priv_data;
  252. destroy_buffers(ctx);
  253. return 0;
  254. }
  255. static int rle_decode(SANMVideoContext *ctx, uint8_t *dst, const int out_size)
  256. {
  257. int opcode, color, run_len, left = out_size;
  258. while (left > 0) {
  259. opcode = bytestream2_get_byte(&ctx->gb);
  260. run_len = (opcode >> 1) + 1;
  261. if (run_len > left || bytestream2_get_bytes_left(&ctx->gb) <= 0)
  262. return AVERROR_INVALIDDATA;
  263. if (opcode & 1) {
  264. color = bytestream2_get_byte(&ctx->gb);
  265. memset(dst, color, run_len);
  266. } else {
  267. if (bytestream2_get_bytes_left(&ctx->gb) < run_len)
  268. return AVERROR_INVALIDDATA;
  269. bytestream2_get_bufferu(&ctx->gb, dst, run_len);
  270. }
  271. dst += run_len;
  272. left -= run_len;
  273. }
  274. return 0;
  275. }
  276. static int old_codec1(SANMVideoContext *ctx, int top,
  277. int left, int width, int height)
  278. {
  279. uint8_t *dst = ((uint8_t*)ctx->frm0) + left + top * ctx->pitch;
  280. int i, j, len, flag, code, val, pos, end;
  281. for (i = 0; i < height; i++) {
  282. pos = 0;
  283. if (bytestream2_get_bytes_left(&ctx->gb) < 2)
  284. return AVERROR_INVALIDDATA;
  285. len = bytestream2_get_le16u(&ctx->gb);
  286. end = bytestream2_tell(&ctx->gb) + len;
  287. while (bytestream2_tell(&ctx->gb) < end) {
  288. if (bytestream2_get_bytes_left(&ctx->gb) < 2)
  289. return AVERROR_INVALIDDATA;
  290. code = bytestream2_get_byteu(&ctx->gb);
  291. flag = code & 1;
  292. code = (code >> 1) + 1;
  293. if (pos + code > width)
  294. return AVERROR_INVALIDDATA;
  295. if (flag) {
  296. val = bytestream2_get_byteu(&ctx->gb);
  297. if (val)
  298. memset(dst + pos, val, code);
  299. pos += code;
  300. } else {
  301. if (bytestream2_get_bytes_left(&ctx->gb) < code)
  302. return AVERROR_INVALIDDATA;
  303. for (j = 0; j < code; j++) {
  304. val = bytestream2_get_byteu(&ctx->gb);
  305. if (val)
  306. dst[pos] = val;
  307. pos++;
  308. }
  309. }
  310. }
  311. dst += ctx->pitch;
  312. }
  313. ctx->rotate_code = 0;
  314. return 0;
  315. }
  316. static inline void codec37_mv(uint8_t *dst, const uint8_t *src,
  317. int height, int stride, int x, int y)
  318. {
  319. int pos, i, j;
  320. pos = x + y * stride;
  321. for (j = 0; j < 4; j++) {
  322. for (i = 0; i < 4; i++) {
  323. if ((pos + i) < 0 || (pos + i) >= height * stride)
  324. dst[i] = 0;
  325. else
  326. dst[i] = src[i];
  327. }
  328. dst += stride;
  329. src += stride;
  330. pos += stride;
  331. }
  332. }
  333. static int old_codec37(SANMVideoContext *ctx, int top,
  334. int left, int width, int height)
  335. {
  336. int stride = ctx->pitch;
  337. int i, j, k, t;
  338. int skip_run = 0;
  339. int compr, mvoff, seq, flags;
  340. uint32_t decoded_size;
  341. uint8_t *dst, *prev;
  342. compr = bytestream2_get_byte(&ctx->gb);
  343. mvoff = bytestream2_get_byte(&ctx->gb);
  344. seq = bytestream2_get_le16(&ctx->gb);
  345. decoded_size = bytestream2_get_le32(&ctx->gb);
  346. bytestream2_skip(&ctx->gb, 4);
  347. flags = bytestream2_get_byte(&ctx->gb);
  348. bytestream2_skip(&ctx->gb, 3);
  349. if (decoded_size > ctx->height * stride - left - top * stride) {
  350. decoded_size = ctx->height * stride - left - top * stride;
  351. av_log(ctx->avctx, AV_LOG_WARNING, "decoded size is too large\n");
  352. }
  353. ctx->rotate_code = 0;
  354. if (((seq & 1) || !(flags & 1)) && (compr && compr != 2))
  355. rotate_bufs(ctx, 1);
  356. dst = ((uint8_t*)ctx->frm0) + left + top * stride;
  357. prev = ((uint8_t*)ctx->frm2) + left + top * stride;
  358. if (mvoff > 2) {
  359. av_log(ctx->avctx, AV_LOG_ERROR, "invalid motion base value %d\n", mvoff);
  360. return AVERROR_INVALIDDATA;
  361. }
  362. av_dlog(ctx->avctx, "compression %d\n", compr);
  363. switch (compr) {
  364. case 0:
  365. for (i = 0; i < height; i++) {
  366. bytestream2_get_buffer(&ctx->gb, dst, width);
  367. dst += stride;
  368. }
  369. memset(ctx->frm1, 0, ctx->height * stride);
  370. memset(ctx->frm2, 0, ctx->height * stride);
  371. break;
  372. case 2:
  373. if (rle_decode(ctx, dst, decoded_size))
  374. return AVERROR_INVALIDDATA;
  375. memset(ctx->frm1, 0, ctx->frm1_size);
  376. memset(ctx->frm2, 0, ctx->frm2_size);
  377. break;
  378. case 3:
  379. case 4:
  380. if (flags & 4) {
  381. for (j = 0; j < height; j += 4) {
  382. for (i = 0; i < width; i += 4) {
  383. int code;
  384. if (skip_run) {
  385. skip_run--;
  386. copy_block4(dst + i, prev + i, stride, stride, 4);
  387. continue;
  388. }
  389. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  390. return AVERROR_INVALIDDATA;
  391. code = bytestream2_get_byteu(&ctx->gb);
  392. switch (code) {
  393. case 0xFF:
  394. if (bytestream2_get_bytes_left(&ctx->gb) < 16)
  395. return AVERROR_INVALIDDATA;
  396. for (k = 0; k < 4; k++)
  397. bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
  398. break;
  399. case 0xFE:
  400. if (bytestream2_get_bytes_left(&ctx->gb) < 4)
  401. return AVERROR_INVALIDDATA;
  402. for (k = 0; k < 4; k++)
  403. memset(dst + i + k * stride, bytestream2_get_byteu(&ctx->gb), 4);
  404. break;
  405. case 0xFD:
  406. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  407. return AVERROR_INVALIDDATA;
  408. t = bytestream2_get_byteu(&ctx->gb);
  409. for (k = 0; k < 4; k++)
  410. memset(dst + i + k * stride, t, 4);
  411. break;
  412. default:
  413. if (compr == 4 && !code) {
  414. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  415. return AVERROR_INVALIDDATA;
  416. skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
  417. i -= 4;
  418. } else {
  419. int mx, my;
  420. mx = c37_mv[(mvoff * 255 + code) * 2 ];
  421. my = c37_mv[(mvoff * 255 + code) * 2 + 1];
  422. codec37_mv(dst + i, prev + i + mx + my * stride,
  423. ctx->height, stride, i + mx, j + my);
  424. }
  425. }
  426. }
  427. dst += stride * 4;
  428. prev += stride * 4;
  429. }
  430. } else {
  431. for (j = 0; j < height; j += 4) {
  432. for (i = 0; i < width; i += 4) {
  433. int code;
  434. if (skip_run) {
  435. skip_run--;
  436. copy_block4(dst + i, prev + i, stride, stride, 4);
  437. continue;
  438. }
  439. code = bytestream2_get_byte(&ctx->gb);
  440. if (code == 0xFF) {
  441. if (bytestream2_get_bytes_left(&ctx->gb) < 16)
  442. return AVERROR_INVALIDDATA;
  443. for (k = 0; k < 4; k++)
  444. bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
  445. } else if (compr == 4 && !code) {
  446. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  447. return AVERROR_INVALIDDATA;
  448. skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
  449. i -= 4;
  450. } else {
  451. int mx, my;
  452. mx = c37_mv[(mvoff * 255 + code) * 2];
  453. my = c37_mv[(mvoff * 255 + code) * 2 + 1];
  454. codec37_mv(dst + i, prev + i + mx + my * stride,
  455. ctx->height, stride, i + mx, j + my);
  456. }
  457. }
  458. dst += stride * 4;
  459. prev += stride * 4;
  460. }
  461. }
  462. break;
  463. default:
  464. av_log(ctx->avctx, AV_LOG_ERROR,
  465. "subcodec 37 compression %d not implemented\n", compr);
  466. return AVERROR_PATCHWELCOME;
  467. }
  468. return 0;
  469. }
  470. static int process_block(SANMVideoContext *ctx, uint8_t *dst, uint8_t *prev1,
  471. uint8_t *prev2, int stride, int tbl, int size)
  472. {
  473. int code, k, t;
  474. uint8_t colors[2];
  475. int8_t *pglyph;
  476. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  477. return AVERROR_INVALIDDATA;
  478. code = bytestream2_get_byteu(&ctx->gb);
  479. if (code >= 0xF8) {
  480. switch (code) {
  481. case 0xFF:
  482. if (size == 2) {
  483. if (bytestream2_get_bytes_left(&ctx->gb) < 4)
  484. return AVERROR_INVALIDDATA;
  485. dst[0] = bytestream2_get_byteu(&ctx->gb);
  486. dst[1] = bytestream2_get_byteu(&ctx->gb);
  487. dst[0+stride] = bytestream2_get_byteu(&ctx->gb);
  488. dst[1+stride] = bytestream2_get_byteu(&ctx->gb);
  489. } else {
  490. size >>= 1;
  491. if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
  492. return AVERROR_INVALIDDATA;
  493. if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
  494. stride, tbl, size))
  495. return AVERROR_INVALIDDATA;
  496. dst += size * stride;
  497. prev1 += size * stride;
  498. prev2 += size * stride;
  499. if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
  500. return AVERROR_INVALIDDATA;
  501. if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
  502. stride, tbl, size))
  503. return AVERROR_INVALIDDATA;
  504. }
  505. break;
  506. case 0xFE:
  507. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  508. return AVERROR_INVALIDDATA;
  509. t = bytestream2_get_byteu(&ctx->gb);
  510. for (k = 0; k < size; k++)
  511. memset(dst + k * stride, t, size);
  512. break;
  513. case 0xFD:
  514. if (bytestream2_get_bytes_left(&ctx->gb) < 3)
  515. return AVERROR_INVALIDDATA;
  516. code = bytestream2_get_byteu(&ctx->gb);
  517. pglyph = (size == 8) ? ctx->p8x8glyphs[code] : ctx->p4x4glyphs[code];
  518. bytestream2_get_bufferu(&ctx->gb, colors, 2);
  519. for (k = 0; k < size; k++)
  520. for (t = 0; t < size; t++)
  521. dst[t + k * stride] = colors[!*pglyph++];
  522. break;
  523. case 0xFC:
  524. for (k = 0; k < size; k++)
  525. memcpy(dst + k * stride, prev1 + k * stride, size);
  526. break;
  527. default:
  528. k = bytestream2_tell(&ctx->gb);
  529. bytestream2_seek(&ctx->gb, tbl + (code & 7), SEEK_SET);
  530. t = bytestream2_get_byte(&ctx->gb);
  531. bytestream2_seek(&ctx->gb, k, SEEK_SET);
  532. for (k = 0; k < size; k++)
  533. memset(dst + k * stride, t, size);
  534. }
  535. } else {
  536. int mx = motion_vectors[code][0];
  537. int my = motion_vectors[code][1];
  538. int index = prev2 - (const uint8_t*)ctx->frm2;
  539. av_assert2(index >= 0 && index < (ctx->buf_size>>1));
  540. if (index < - mx - my*stride ||
  541. (ctx->buf_size>>1) - index < mx + size + (my + size - 1)*stride) {
  542. av_log(ctx->avctx, AV_LOG_ERROR, "MV is invalid \n");
  543. return AVERROR_INVALIDDATA;
  544. }
  545. for (k = 0; k < size; k++)
  546. memcpy(dst + k * stride, prev2 + mx + (my + k) * stride, size);
  547. }
  548. return 0;
  549. }
  550. static int old_codec47(SANMVideoContext *ctx, int top,
  551. int left, int width, int height)
  552. {
  553. int i, j, seq, compr, new_rot, tbl_pos, skip;
  554. int stride = ctx->pitch;
  555. uint8_t *dst = ((uint8_t*)ctx->frm0) + left + top * stride;
  556. uint8_t *prev1 = (uint8_t*)ctx->frm1;
  557. uint8_t *prev2 = (uint8_t*)ctx->frm2;
  558. uint32_t decoded_size;
  559. tbl_pos = bytestream2_tell(&ctx->gb);
  560. seq = bytestream2_get_le16(&ctx->gb);
  561. compr = bytestream2_get_byte(&ctx->gb);
  562. new_rot = bytestream2_get_byte(&ctx->gb);
  563. skip = bytestream2_get_byte(&ctx->gb);
  564. bytestream2_skip(&ctx->gb, 9);
  565. decoded_size = bytestream2_get_le32(&ctx->gb);
  566. bytestream2_skip(&ctx->gb, 8);
  567. if (decoded_size > ctx->height * stride - left - top * stride) {
  568. decoded_size = ctx->height * stride - left - top * stride;
  569. av_log(ctx->avctx, AV_LOG_WARNING, "decoded size is too large\n");
  570. }
  571. if (skip & 1)
  572. bytestream2_skip(&ctx->gb, 0x8080);
  573. if (!seq) {
  574. ctx->prev_seq = -1;
  575. memset(prev1, 0, ctx->height * stride);
  576. memset(prev2, 0, ctx->height * stride);
  577. }
  578. av_dlog(ctx->avctx, "compression %d\n", compr);
  579. switch (compr) {
  580. case 0:
  581. if (bytestream2_get_bytes_left(&ctx->gb) < width * height)
  582. return AVERROR_INVALIDDATA;
  583. for (j = 0; j < height; j++) {
  584. bytestream2_get_bufferu(&ctx->gb, dst, width);
  585. dst += stride;
  586. }
  587. break;
  588. case 1:
  589. if (bytestream2_get_bytes_left(&ctx->gb) < ((width + 1) >> 1) * ((height + 1) >> 1))
  590. return AVERROR_INVALIDDATA;
  591. for (j = 0; j < height; j += 2) {
  592. for (i = 0; i < width; i += 2) {
  593. dst[i] = dst[i + 1] =
  594. dst[stride + i] = dst[stride + i + 1] = bytestream2_get_byteu(&ctx->gb);
  595. }
  596. dst += stride * 2;
  597. }
  598. break;
  599. case 2:
  600. if (seq == ctx->prev_seq + 1) {
  601. for (j = 0; j < height; j += 8) {
  602. for (i = 0; i < width; i += 8) {
  603. if (process_block(ctx, dst + i, prev1 + i, prev2 + i, stride,
  604. tbl_pos + 8, 8))
  605. return AVERROR_INVALIDDATA;
  606. }
  607. dst += stride * 8;
  608. prev1 += stride * 8;
  609. prev2 += stride * 8;
  610. }
  611. }
  612. break;
  613. case 3:
  614. memcpy(ctx->frm0, ctx->frm2, ctx->pitch * ctx->height);
  615. break;
  616. case 4:
  617. memcpy(ctx->frm0, ctx->frm1, ctx->pitch * ctx->height);
  618. break;
  619. case 5:
  620. if (rle_decode(ctx, dst, decoded_size))
  621. return AVERROR_INVALIDDATA;
  622. break;
  623. default:
  624. av_log(ctx->avctx, AV_LOG_ERROR,
  625. "subcodec 47 compression %d not implemented\n", compr);
  626. return AVERROR_PATCHWELCOME;
  627. }
  628. if (seq == ctx->prev_seq + 1)
  629. ctx->rotate_code = new_rot;
  630. else
  631. ctx->rotate_code = 0;
  632. ctx->prev_seq = seq;
  633. return 0;
  634. }
  635. static int process_frame_obj(SANMVideoContext *ctx)
  636. {
  637. uint16_t codec, top, left, w, h;
  638. codec = bytestream2_get_le16u(&ctx->gb);
  639. left = bytestream2_get_le16u(&ctx->gb);
  640. top = bytestream2_get_le16u(&ctx->gb);
  641. w = bytestream2_get_le16u(&ctx->gb);
  642. h = bytestream2_get_le16u(&ctx->gb);
  643. if (ctx->width < left + w || ctx->height < top + h) {
  644. if (av_image_check_size(FFMAX(left + w, ctx->width),
  645. FFMAX(top + h, ctx->height), 0, ctx->avctx) < 0)
  646. return AVERROR_INVALIDDATA;
  647. avcodec_set_dimensions(ctx->avctx, FFMAX(left + w, ctx->width),
  648. FFMAX(top + h, ctx->height));
  649. init_sizes(ctx, FFMAX(left + w, ctx->width),
  650. FFMAX(top + h, ctx->height));
  651. if (init_buffers(ctx)) {
  652. av_log(ctx->avctx, AV_LOG_ERROR, "error resizing buffers\n");
  653. return AVERROR(ENOMEM);
  654. }
  655. }
  656. bytestream2_skip(&ctx->gb, 4);
  657. av_dlog(ctx->avctx, "subcodec %d\n", codec);
  658. switch (codec) {
  659. case 1:
  660. case 3:
  661. return old_codec1(ctx, top, left, w, h);
  662. break;
  663. case 37:
  664. return old_codec37(ctx, top, left, w, h);
  665. break;
  666. case 47:
  667. return old_codec47(ctx, top, left, w, h);
  668. break;
  669. default:
  670. avpriv_request_sample(ctx->avctx, "unknown subcodec %d", codec);
  671. return AVERROR_PATCHWELCOME;
  672. }
  673. }
  674. static int decode_0(SANMVideoContext *ctx)
  675. {
  676. uint16_t *frm = ctx->frm0;
  677. int x, y;
  678. if (bytestream2_get_bytes_left(&ctx->gb) < ctx->width * ctx->height * 2) {
  679. av_log(ctx->avctx, AV_LOG_ERROR, "insufficient data for raw frame\n");
  680. return AVERROR_INVALIDDATA;
  681. }
  682. for (y = 0; y < ctx->height; y++) {
  683. for (x = 0; x < ctx->width; x++)
  684. frm[x] = bytestream2_get_le16u(&ctx->gb);
  685. frm += ctx->pitch;
  686. }
  687. return 0;
  688. }
  689. static int decode_nop(SANMVideoContext *ctx)
  690. {
  691. avpriv_request_sample(ctx->avctx, "unknown/unsupported compression type");
  692. return AVERROR_PATCHWELCOME;
  693. }
  694. static void copy_block(uint16_t *pdest, uint16_t *psrc, int block_size, int pitch)
  695. {
  696. uint8_t *dst = (uint8_t *)pdest;
  697. uint8_t *src = (uint8_t *)psrc;
  698. int stride = pitch * 2;
  699. switch (block_size) {
  700. case 2:
  701. copy_block4(dst, src, stride, stride, 2);
  702. break;
  703. case 4:
  704. copy_block8(dst, src, stride, stride, 4);
  705. break;
  706. case 8:
  707. copy_block16(dst, src, stride, stride, 8);
  708. break;
  709. }
  710. }
  711. static void fill_block(uint16_t *pdest, uint16_t color, int block_size, int pitch)
  712. {
  713. int x, y;
  714. pitch -= block_size;
  715. for (y = 0; y < block_size; y++, pdest += pitch)
  716. for (x = 0; x < block_size; x++)
  717. *pdest++ = color;
  718. }
  719. static int draw_glyph(SANMVideoContext *ctx, uint16_t *dst, int index, uint16_t fg_color,
  720. uint16_t bg_color, int block_size, int pitch)
  721. {
  722. int8_t *pglyph;
  723. uint16_t colors[2] = { fg_color, bg_color };
  724. int x, y;
  725. if (index >= NGLYPHS) {
  726. av_log(ctx->avctx, AV_LOG_ERROR, "ignoring nonexistent glyph #%u\n", index);
  727. return AVERROR_INVALIDDATA;
  728. }
  729. pglyph = block_size == 8 ? ctx->p8x8glyphs[index] : ctx->p4x4glyphs[index];
  730. pitch -= block_size;
  731. for (y = 0; y < block_size; y++, dst += pitch)
  732. for (x = 0; x < block_size; x++)
  733. *dst++ = colors[*pglyph++];
  734. return 0;
  735. }
  736. static int opcode_0xf7(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
  737. {
  738. uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
  739. if (block_size == 2) {
  740. uint32_t indices;
  741. if (bytestream2_get_bytes_left(&ctx->gb) < 4)
  742. return AVERROR_INVALIDDATA;
  743. indices = bytestream2_get_le32u(&ctx->gb);
  744. dst[0] = ctx->codebook[indices & 0xFF]; indices >>= 8;
  745. dst[1] = ctx->codebook[indices & 0xFF]; indices >>= 8;
  746. dst[pitch] = ctx->codebook[indices & 0xFF]; indices >>= 8;
  747. dst[pitch + 1] = ctx->codebook[indices & 0xFF];
  748. } else {
  749. uint16_t fgcolor, bgcolor;
  750. int glyph;
  751. if (bytestream2_get_bytes_left(&ctx->gb) < 3)
  752. return AVERROR_INVALIDDATA;
  753. glyph = bytestream2_get_byteu(&ctx->gb);
  754. bgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
  755. fgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
  756. draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
  757. }
  758. return 0;
  759. }
  760. static int opcode_0xf8(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
  761. {
  762. uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
  763. if (block_size == 2) {
  764. if (bytestream2_get_bytes_left(&ctx->gb) < 8)
  765. return AVERROR_INVALIDDATA;
  766. dst[0] = bytestream2_get_le16u(&ctx->gb);
  767. dst[1] = bytestream2_get_le16u(&ctx->gb);
  768. dst[pitch] = bytestream2_get_le16u(&ctx->gb);
  769. dst[pitch + 1] = bytestream2_get_le16u(&ctx->gb);
  770. } else {
  771. uint16_t fgcolor, bgcolor;
  772. int glyph;
  773. if (bytestream2_get_bytes_left(&ctx->gb) < 5)
  774. return AVERROR_INVALIDDATA;
  775. glyph = bytestream2_get_byteu(&ctx->gb);
  776. bgcolor = bytestream2_get_le16u(&ctx->gb);
  777. fgcolor = bytestream2_get_le16u(&ctx->gb);
  778. draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
  779. }
  780. return 0;
  781. }
  782. static int good_mvec(SANMVideoContext *ctx, int cx, int cy, int mx, int my,
  783. int block_size)
  784. {
  785. int start_pos = cx + mx + (cy + my) * ctx->pitch;
  786. int end_pos = start_pos + (block_size - 1) * (ctx->pitch + 1);
  787. int good = start_pos >= 0 && end_pos < (ctx->buf_size >> 1);
  788. if (!good) {
  789. av_log(ctx->avctx, AV_LOG_ERROR, "ignoring invalid motion vector (%i, %i)->(%u, %u), block size = %u\n",
  790. cx + mx, cy + my, cx, cy, block_size);
  791. }
  792. return good;
  793. }
  794. static int codec2subblock(SANMVideoContext *ctx, int cx, int cy, int blk_size)
  795. {
  796. int16_t mx, my, index;
  797. int opcode;
  798. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  799. return AVERROR_INVALIDDATA;
  800. opcode = bytestream2_get_byteu(&ctx->gb);
  801. av_dlog(ctx->avctx, "opcode 0x%0X cx %d cy %d blk %d\n", opcode, cx, cy, blk_size);
  802. switch (opcode) {
  803. default:
  804. mx = motion_vectors[opcode][0];
  805. my = motion_vectors[opcode][1];
  806. if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
  807. copy_block(ctx->frm0 + cx + ctx->pitch * cy,
  808. ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
  809. blk_size, ctx->pitch);
  810. }
  811. break;
  812. case 0xF5:
  813. if (bytestream2_get_bytes_left(&ctx->gb) < 2)
  814. return AVERROR_INVALIDDATA;
  815. index = bytestream2_get_le16u(&ctx->gb);
  816. mx = index % ctx->width;
  817. my = index / ctx->width;
  818. if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
  819. copy_block(ctx->frm0 + cx + ctx->pitch * cy,
  820. ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
  821. blk_size, ctx->pitch);
  822. }
  823. break;
  824. case 0xF6:
  825. copy_block(ctx->frm0 + cx + ctx->pitch * cy,
  826. ctx->frm1 + cx + ctx->pitch * cy,
  827. blk_size, ctx->pitch);
  828. break;
  829. case 0xF7:
  830. opcode_0xf7(ctx, cx, cy, blk_size, ctx->pitch);
  831. break;
  832. case 0xF8:
  833. opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
  834. break;
  835. case 0xF9:
  836. case 0xFA:
  837. case 0xFB:
  838. case 0xFC:
  839. fill_block(ctx->frm0 + cx + cy * ctx->pitch,
  840. ctx->small_codebook[opcode - 0xf9], blk_size, ctx->pitch);
  841. break;
  842. case 0xFD:
  843. if (bytestream2_get_bytes_left(&ctx->gb) < 1)
  844. return AVERROR_INVALIDDATA;
  845. fill_block(ctx->frm0 + cx + cy * ctx->pitch,
  846. ctx->codebook[bytestream2_get_byteu(&ctx->gb)], blk_size, ctx->pitch);
  847. break;
  848. case 0xFE:
  849. if (bytestream2_get_bytes_left(&ctx->gb) < 2)
  850. return AVERROR_INVALIDDATA;
  851. fill_block(ctx->frm0 + cx + cy * ctx->pitch,
  852. bytestream2_get_le16u(&ctx->gb), blk_size, ctx->pitch);
  853. break;
  854. case 0xFF:
  855. if (blk_size == 2) {
  856. opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
  857. } else {
  858. blk_size >>= 1;
  859. if (codec2subblock(ctx, cx , cy , blk_size))
  860. return AVERROR_INVALIDDATA;
  861. if (codec2subblock(ctx, cx + blk_size, cy , blk_size))
  862. return AVERROR_INVALIDDATA;
  863. if (codec2subblock(ctx, cx , cy + blk_size, blk_size))
  864. return AVERROR_INVALIDDATA;
  865. if (codec2subblock(ctx, cx + blk_size, cy + blk_size, blk_size))
  866. return AVERROR_INVALIDDATA;
  867. }
  868. break;
  869. }
  870. return 0;
  871. }
  872. static int decode_2(SANMVideoContext *ctx)
  873. {
  874. int cx, cy, ret;
  875. for (cy = 0; cy < ctx->aligned_height; cy += 8) {
  876. for (cx = 0; cx < ctx->aligned_width; cx += 8) {
  877. if (ret = codec2subblock(ctx, cx, cy, 8))
  878. return ret;
  879. }
  880. }
  881. return 0;
  882. }
  883. static int decode_3(SANMVideoContext *ctx)
  884. {
  885. memcpy(ctx->frm0, ctx->frm2, ctx->frm2_size);
  886. return 0;
  887. }
  888. static int decode_4(SANMVideoContext *ctx)
  889. {
  890. memcpy(ctx->frm0, ctx->frm1, ctx->frm1_size);
  891. return 0;
  892. }
  893. static int decode_5(SANMVideoContext *ctx)
  894. {
  895. #if HAVE_BIGENDIAN
  896. uint16_t *frm;
  897. int npixels;
  898. #endif
  899. uint8_t *dst = (uint8_t*)ctx->frm0;
  900. if (rle_decode(ctx, dst, ctx->buf_size))
  901. return AVERROR_INVALIDDATA;
  902. #if HAVE_BIGENDIAN
  903. npixels = ctx->npixels;
  904. frm = ctx->frm0;
  905. while (npixels--)
  906. *frm++ = av_bswap16(*frm);
  907. #endif
  908. return 0;
  909. }
  910. static int decode_6(SANMVideoContext *ctx)
  911. {
  912. int npixels = ctx->npixels;
  913. uint16_t *frm = ctx->frm0;
  914. if (bytestream2_get_bytes_left(&ctx->gb) < npixels) {
  915. av_log(ctx->avctx, AV_LOG_ERROR, "insufficient data for frame\n");
  916. return AVERROR_INVALIDDATA;
  917. }
  918. while (npixels--)
  919. *frm++ = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
  920. return 0;
  921. }
  922. static int decode_8(SANMVideoContext *ctx)
  923. {
  924. uint16_t *pdest = ctx->frm0;
  925. uint8_t *rsrc;
  926. long npixels = ctx->npixels;
  927. av_fast_malloc(&ctx->rle_buf, &ctx->rle_buf_size, npixels);
  928. if (!ctx->rle_buf) {
  929. av_log(ctx->avctx, AV_LOG_ERROR, "RLE buffer allocation failed\n");
  930. return AVERROR(ENOMEM);
  931. }
  932. rsrc = ctx->rle_buf;
  933. if (rle_decode(ctx, rsrc, npixels))
  934. return AVERROR_INVALIDDATA;
  935. while (npixels--)
  936. *pdest++ = ctx->codebook[*rsrc++];
  937. return 0;
  938. }
  939. typedef int (*frm_decoder)(SANMVideoContext *ctx);
  940. static const frm_decoder v1_decoders[] = {
  941. decode_0, decode_nop, decode_2, decode_3, decode_4, decode_5,
  942. decode_6, decode_nop, decode_8
  943. };
  944. static int read_frame_header(SANMVideoContext *ctx, SANMFrameHeader *hdr)
  945. {
  946. int i, ret;
  947. if ((ret = bytestream2_get_bytes_left(&ctx->gb)) < 560) {
  948. av_log(ctx->avctx, AV_LOG_ERROR, "too short input frame (%d bytes)\n",
  949. ret);
  950. return AVERROR_INVALIDDATA;
  951. }
  952. bytestream2_skip(&ctx->gb, 8); // skip pad
  953. hdr->width = bytestream2_get_le32u(&ctx->gb);
  954. hdr->height = bytestream2_get_le32u(&ctx->gb);
  955. if (hdr->width != ctx->width || hdr->height != ctx->height) {
  956. av_log(ctx->avctx, AV_LOG_ERROR, "variable size frames are not implemented\n");
  957. return AVERROR_PATCHWELCOME;
  958. }
  959. hdr->seq_num = bytestream2_get_le16u(&ctx->gb);
  960. hdr->codec = bytestream2_get_byteu(&ctx->gb);
  961. hdr->rotate_code = bytestream2_get_byteu(&ctx->gb);
  962. bytestream2_skip(&ctx->gb, 4); // skip pad
  963. for (i = 0; i < 4; i++)
  964. ctx->small_codebook[i] = bytestream2_get_le16u(&ctx->gb);
  965. hdr->bg_color = bytestream2_get_le16u(&ctx->gb);
  966. bytestream2_skip(&ctx->gb, 2); // skip pad
  967. hdr->rle_output_size = bytestream2_get_le32u(&ctx->gb);
  968. for (i = 0; i < 256; i++)
  969. ctx->codebook[i] = bytestream2_get_le16u(&ctx->gb);
  970. bytestream2_skip(&ctx->gb, 8); // skip pad
  971. av_dlog(ctx->avctx, "subcodec %d\n", hdr->codec);
  972. return 0;
  973. }
  974. static void fill_frame(uint16_t *pbuf, int buf_size, uint16_t color)
  975. {
  976. while (buf_size--)
  977. *pbuf++ = color;
  978. }
  979. static int copy_output(SANMVideoContext *ctx, SANMFrameHeader *hdr)
  980. {
  981. uint8_t *dst;
  982. const uint8_t *src = (uint8_t*) ctx->frm0;
  983. int ret, dstpitch, height = ctx->height;
  984. int srcpitch = ctx->pitch * (hdr ? sizeof(ctx->frm0[0]) : 1);
  985. if ((ret = ff_get_buffer(ctx->avctx, ctx->frame, 0)) < 0)
  986. return ret;
  987. dst = ctx->frame->data[0];
  988. dstpitch = ctx->frame->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. ctx->frame = data;
  1002. bytestream2_init(&ctx->gb, pkt->data, pkt->size);
  1003. if (!ctx->version) {
  1004. int to_store = 0;
  1005. while (bytestream2_get_bytes_left(&ctx->gb) >= 8) {
  1006. uint32_t sig, size;
  1007. int pos;
  1008. sig = bytestream2_get_be32u(&ctx->gb);
  1009. size = bytestream2_get_be32u(&ctx->gb);
  1010. pos = bytestream2_tell(&ctx->gb);
  1011. if (bytestream2_get_bytes_left(&ctx->gb) < size) {
  1012. av_log(avctx, AV_LOG_ERROR, "incorrect chunk size %d\n", size);
  1013. break;
  1014. }
  1015. switch (sig) {
  1016. case MKBETAG('N', 'P', 'A', 'L'):
  1017. if (size != 256 * 3) {
  1018. av_log(avctx, AV_LOG_ERROR, "incorrect palette block size %d\n",
  1019. size);
  1020. return AVERROR_INVALIDDATA;
  1021. }
  1022. for (i = 0; i < 256; i++)
  1023. ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
  1024. break;
  1025. case MKBETAG('F', 'O', 'B', 'J'):
  1026. if (size < 16)
  1027. return AVERROR_INVALIDDATA;
  1028. if (ret = process_frame_obj(ctx))
  1029. return ret;
  1030. break;
  1031. case MKBETAG('X', 'P', 'A', 'L'):
  1032. if (size == 6 || size == 4) {
  1033. uint8_t tmp[3];
  1034. int j;
  1035. for (i = 0; i < 256; i++) {
  1036. for (j = 0; j < 3; j++) {
  1037. int t = (ctx->pal[i] >> (16 - j * 8)) & 0xFF;
  1038. tmp[j] = av_clip_uint8((t * 129 + ctx->delta_pal[i * 3 + j]) >> 7);
  1039. }
  1040. ctx->pal[i] = 0xFFU << 24 | AV_RB24(tmp);
  1041. }
  1042. } else {
  1043. if (size < 768 * 2 + 4) {
  1044. av_log(avctx, AV_LOG_ERROR, "incorrect palette change block size %d\n",
  1045. size);
  1046. return AVERROR_INVALIDDATA;
  1047. }
  1048. bytestream2_skipu(&ctx->gb, 4);
  1049. for (i = 0; i < 768; i++)
  1050. ctx->delta_pal[i] = bytestream2_get_le16u(&ctx->gb);
  1051. if (size >= 768 * 5 + 4) {
  1052. for (i = 0; i < 256; i++)
  1053. ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
  1054. } else {
  1055. memset(ctx->pal, 0, sizeof(ctx->pal));
  1056. }
  1057. }
  1058. break;
  1059. case MKBETAG('S', 'T', 'O', 'R'):
  1060. to_store = 1;
  1061. break;
  1062. case MKBETAG('F', 'T', 'C', 'H'):
  1063. memcpy(ctx->frm0, ctx->stored_frame, ctx->buf_size);
  1064. break;
  1065. default:
  1066. bytestream2_skip(&ctx->gb, size);
  1067. av_log(avctx, AV_LOG_DEBUG, "unknown/unsupported chunk %x\n", sig);
  1068. break;
  1069. }
  1070. bytestream2_seek(&ctx->gb, pos + size, SEEK_SET);
  1071. if (size & 1)
  1072. bytestream2_skip(&ctx->gb, 1);
  1073. }
  1074. if (to_store)
  1075. memcpy(ctx->stored_frame, ctx->frm0, ctx->buf_size);
  1076. if ((ret = copy_output(ctx, NULL)))
  1077. return ret;
  1078. memcpy(ctx->frame->data[1], ctx->pal, 1024);
  1079. } else {
  1080. SANMFrameHeader header;
  1081. if ((ret = read_frame_header(ctx, &header)))
  1082. return ret;
  1083. ctx->rotate_code = header.rotate_code;
  1084. if ((ctx->frame->key_frame = !header.seq_num)) {
  1085. ctx->frame->pict_type = AV_PICTURE_TYPE_I;
  1086. fill_frame(ctx->frm1, ctx->npixels, header.bg_color);
  1087. fill_frame(ctx->frm2, ctx->npixels, header.bg_color);
  1088. } else {
  1089. ctx->frame->pict_type = AV_PICTURE_TYPE_P;
  1090. }
  1091. if (header.codec < FF_ARRAY_ELEMS(v1_decoders)) {
  1092. if ((ret = v1_decoders[header.codec](ctx))) {
  1093. av_log(avctx, AV_LOG_ERROR,
  1094. "subcodec %d: error decoding frame\n", header.codec);
  1095. return ret;
  1096. }
  1097. } else {
  1098. avpriv_request_sample(avctx, "subcodec %d",
  1099. header.codec);
  1100. return AVERROR_PATCHWELCOME;
  1101. }
  1102. if ((ret = copy_output(ctx, &header)))
  1103. return ret;
  1104. }
  1105. if (ctx->rotate_code)
  1106. rotate_bufs(ctx, ctx->rotate_code);
  1107. *got_frame_ptr = 1;
  1108. return pkt->size;
  1109. }
  1110. AVCodec ff_sanm_decoder = {
  1111. .name = "sanm",
  1112. .type = AVMEDIA_TYPE_VIDEO,
  1113. .id = AV_CODEC_ID_SANM,
  1114. .priv_data_size = sizeof(SANMVideoContext),
  1115. .init = decode_init,
  1116. .close = decode_end,
  1117. .decode = decode_frame,
  1118. .capabilities = CODEC_CAP_DR1,
  1119. .long_name = NULL_IF_CONFIG_SMALL("LucasArts SMUSH video"),
  1120. };