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.

1274 lines
40KB

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