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