2 * LucasArts Smush video decoder
3 * Copyright (c) 2006 Cyril Zorin
4 * Copyright (c) 2011 Konstantin Shishkov
6 * This file is part of FFmpeg.
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.
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.
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
26 #include "bytestream.h"
28 #include "libavutil/bswap.h"
29 #include "libavcodec/dsputil.h"
30 #include "sanm_data.h"
35 AVCodecContext *avctx;
38 int version, subversion;
40 int16_t delta_pal[768];
44 int aligned_width, aligned_height;
47 AVFrame frame, *output;
48 uint16_t *frm0, *frm1, *frm2;
49 uint8_t *stored_frame;
50 uint32_t frm0_size, frm1_size, frm2_size;
51 uint32_t stored_frame_size;
54 unsigned int rle_buf_size;
58 long npixels, buf_size;
60 uint16_t codebook[256];
61 uint16_t small_codebook[4];
63 int8_t p4x4glyphs[NGLYPHS][16];
64 int8_t p8x8glyphs[NGLYPHS][64];
68 int seq_num, codec, rotate_code, rle_output_size;
71 uint32_t width, height;
91 * Return enum GlyphEdge of box where point (x, y) lies.
93 * @param x x point coordinate
94 * @param y y point coordinate
95 * @param edge_size box width/height.
97 static enum GlyphEdge which_edge(int x, int y, int edge_size)
99 const int edge_max = edge_size - 1;
103 } else if (y == edge_max) {
107 } else if (x == edge_max) {
114 static enum GlyphDir which_direction(enum GlyphEdge edge0, enum GlyphEdge edge1)
116 if ((edge0 == LEFT_EDGE && edge1 == RIGHT_EDGE) ||
117 (edge1 == LEFT_EDGE && edge0 == RIGHT_EDGE) ||
118 (edge0 == BOTTOM_EDGE && edge1 != TOP_EDGE) ||
119 (edge1 == BOTTOM_EDGE && edge0 != TOP_EDGE)) {
121 } else if ((edge0 == TOP_EDGE && edge1 != BOTTOM_EDGE) ||
122 (edge1 == TOP_EDGE && edge0 != BOTTOM_EDGE)) {
124 } else if ((edge0 == LEFT_EDGE && edge1 != RIGHT_EDGE) ||
125 (edge1 == LEFT_EDGE && edge0 != RIGHT_EDGE)) {
127 } else if ((edge0 == TOP_EDGE && edge1 == BOTTOM_EDGE) ||
128 (edge1 == TOP_EDGE && edge0 == BOTTOM_EDGE) ||
129 (edge0 == RIGHT_EDGE && edge1 != LEFT_EDGE) ||
130 (edge1 == RIGHT_EDGE && edge0 != LEFT_EDGE)) {
138 * Interpolate two points.
140 static void interp_point(int8_t *points, int x0, int y0, int x1, int y1,
141 int pos, int npoints)
144 points[0] = (x0 * pos + x1 * (npoints - pos) + (npoints >> 1)) / npoints;
145 points[1] = (y0 * pos + y1 * (npoints - pos) + (npoints >> 1)) / npoints;
153 * Construct glyphs by iterating through vectors coordinates.
155 * @param pglyphs pointer to table where glyphs are stored
156 * @param xvec pointer to x component of vectors coordinates
157 * @param yvec pointer to y component of vectors coordinates
158 * @param side_length glyph width/height.
160 static void make_glyphs(int8_t *pglyphs, const int8_t *xvec, const int8_t *yvec,
161 const int side_length)
163 const int glyph_size = side_length * side_length;
164 int8_t *pglyph = pglyphs;
167 for (i = 0; i < GLYPH_COORD_VECT_SIZE; i++) {
170 enum GlyphEdge edge0 = which_edge(x0, y0, side_length);
172 for (j = 0; j < GLYPH_COORD_VECT_SIZE; j++, pglyph += glyph_size) {
175 enum GlyphEdge edge1 = which_edge(x1, y1, side_length);
176 enum GlyphDir dir = which_direction(edge0, edge1);
177 int npoints = FFMAX(FFABS(x1 - x0), FFABS(y1 - y0));
180 for (ipoint = 0; ipoint <= npoints; ipoint++) {
184 interp_point(point, x0, y0, x1, y1, ipoint, npoints);
188 for (irow = point[1]; irow >= 0; irow--)
189 pglyph[point[0] + irow * side_length] = 1;
193 for (irow = point[1]; irow < side_length; irow++)
194 pglyph[point[0] + irow * side_length] = 1;
198 for (icol = point[0]; icol >= 0; icol--)
199 pglyph[icol + point[1] * side_length] = 1;
203 for (icol = point[0]; icol < side_length; icol++)
204 pglyph[icol + point[1] * side_length] = 1;
212 static void init_sizes(SANMVideoContext *ctx, int width, int height)
215 ctx->height = height;
216 ctx->npixels = width * height;
218 ctx->aligned_width = FFALIGN(width, 8);
219 ctx->aligned_height = FFALIGN(height, 8);
221 ctx->buf_size = ctx->aligned_width * ctx->aligned_height * sizeof(ctx->frm0[0]);
225 static void destroy_buffers(SANMVideoContext *ctx)
227 av_freep(&ctx->frm0);
228 av_freep(&ctx->frm1);
229 av_freep(&ctx->frm2);
230 av_freep(&ctx->stored_frame);
231 av_freep(&ctx->rle_buf);
234 static av_cold int init_buffers(SANMVideoContext *ctx)
236 av_fast_padded_malloc(&ctx->frm0, &ctx->frm0_size, ctx->buf_size);
237 av_fast_padded_malloc(&ctx->frm1, &ctx->frm1_size, ctx->buf_size);
238 av_fast_padded_malloc(&ctx->frm2, &ctx->frm2_size, ctx->buf_size);
240 av_fast_padded_malloc(&ctx->stored_frame, &ctx->stored_frame_size, ctx->buf_size);
242 if (!ctx->frm0 || !ctx->frm1 || !ctx->frm2 || (!ctx->stored_frame && !ctx->version)) {
243 destroy_buffers(ctx);
244 return AVERROR(ENOMEM);
250 static void rotate_bufs(SANMVideoContext *ctx, int rotate_code)
252 av_dlog(ctx->avctx, "rotate %d\n", rotate_code);
253 if (rotate_code == 2)
254 FFSWAP(uint16_t*, ctx->frm1, ctx->frm2);
255 FFSWAP(uint16_t*, ctx->frm2, ctx->frm0);
258 static av_cold int decode_init(AVCodecContext *avctx)
260 SANMVideoContext *ctx = avctx->priv_data;
263 ctx->version = !avctx->extradata_size;
265 avctx->pix_fmt = ctx->version ? AV_PIX_FMT_RGB565 : AV_PIX_FMT_PAL8;
267 init_sizes(ctx, avctx->width, avctx->height);
268 if (init_buffers(ctx)) {
269 av_log(avctx, AV_LOG_ERROR, "error allocating buffers\n");
270 return AVERROR(ENOMEM);
272 ctx->output = &ctx->frame;
273 ctx->output->data[0] = 0;
275 make_glyphs(ctx->p4x4glyphs[0], glyph4_x, glyph4_y, 4);
276 make_glyphs(ctx->p8x8glyphs[0], glyph8_x, glyph8_y, 8);
281 if (avctx->extradata_size < 1026) {
282 av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");
283 return AVERROR_INVALIDDATA;
286 ctx->subversion = AV_RL16(avctx->extradata);
287 for (i = 0; i < 256; i++)
288 ctx->pal[i] = 0xFFU << 24 | AV_RL32(avctx->extradata + 2 + i * 4);
294 static av_cold int decode_end(AVCodecContext *avctx)
296 SANMVideoContext *ctx = avctx->priv_data;
298 destroy_buffers(ctx);
300 if (ctx->frame.data[0]) {
301 avctx->release_buffer(avctx, &ctx->frame);
302 ctx->frame.data[0] = 0;
308 static int rle_decode(SANMVideoContext *ctx, uint8_t *dst, const int out_size)
310 int opcode, color, run_len, left = out_size;
313 opcode = bytestream2_get_byte(&ctx->gb);
314 run_len = (opcode >> 1) + 1;
315 if (run_len > left || bytestream2_get_bytes_left(&ctx->gb) <= 0)
316 return AVERROR_INVALIDDATA;
319 color = bytestream2_get_byte(&ctx->gb);
320 memset(dst, color, run_len);
322 if (bytestream2_get_bytes_left(&ctx->gb) < run_len)
323 return AVERROR_INVALIDDATA;
324 bytestream2_get_bufferu(&ctx->gb, dst, run_len);
334 static int old_codec1(SANMVideoContext *ctx, int top,
335 int left, int width, int height)
337 uint8_t *dst = ((uint8_t*)ctx->frm0) + left + top * ctx->pitch;
338 int i, j, len, flag, code, val, pos, end;
340 for (i = 0; i < height; i++) {
343 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
344 return AVERROR_INVALIDDATA;
346 len = bytestream2_get_le16u(&ctx->gb);
347 end = bytestream2_tell(&ctx->gb) + len;
349 while (bytestream2_tell(&ctx->gb) < end) {
350 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
351 return AVERROR_INVALIDDATA;
353 code = bytestream2_get_byteu(&ctx->gb);
355 code = (code >> 1) + 1;
356 if (pos + code > width)
357 return AVERROR_INVALIDDATA;
359 val = bytestream2_get_byteu(&ctx->gb);
361 memset(dst + pos, val, code);
364 if (bytestream2_get_bytes_left(&ctx->gb) < code)
365 return AVERROR_INVALIDDATA;
366 for (j = 0; j < code; j++) {
367 val = bytestream2_get_byteu(&ctx->gb);
376 ctx->rotate_code = 0;
381 static inline void codec37_mv(uint8_t *dst, const uint8_t *src,
382 int height, int stride, int x, int y)
386 pos = x + y * stride;
387 for (j = 0; j < 4; j++) {
388 for (i = 0; i < 4; i++) {
389 if ((pos + i) < 0 || (pos + i) >= height * stride)
400 static int old_codec37(SANMVideoContext *ctx, int top,
401 int left, int width, int height)
403 int stride = ctx->pitch;
406 int compr, mvoff, seq, flags;
407 uint32_t decoded_size;
410 compr = bytestream2_get_byte(&ctx->gb);
411 mvoff = bytestream2_get_byte(&ctx->gb);
412 seq = bytestream2_get_le16(&ctx->gb);
413 decoded_size = bytestream2_get_le32(&ctx->gb);
414 bytestream2_skip(&ctx->gb, 4);
415 flags = bytestream2_get_byte(&ctx->gb);
416 bytestream2_skip(&ctx->gb, 3);
418 ctx->rotate_code = 0;
420 if (((seq & 1) || !(flags & 1)) && (compr && compr != 2))
423 dst = ((uint8_t*)ctx->frm0) + left + top * stride;
424 prev = ((uint8_t*)ctx->frm2) + left + top * stride;
427 av_log(ctx->avctx, AV_LOG_ERROR, "invalid motion base value %d\n", mvoff);
428 return AVERROR_INVALIDDATA;
430 av_dlog(ctx->avctx, "compression %d\n", compr);
433 for (i = 0; i < height; i++) {
434 bytestream2_get_buffer(&ctx->gb, dst, width);
437 memset(ctx->frm1, 0, ctx->height * stride);
438 memset(ctx->frm2, 0, ctx->height * stride);
441 if (rle_decode(ctx, dst, decoded_size))
442 return AVERROR_INVALIDDATA;
443 memset(ctx->frm1, 0, ctx->frm1_size);
444 memset(ctx->frm2, 0, ctx->frm2_size);
449 for (j = 0; j < height; j += 4) {
450 for (i = 0; i < width; i += 4) {
454 copy_block4(dst + i, prev + i, stride, stride, 4);
457 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
458 return AVERROR_INVALIDDATA;
459 code = bytestream2_get_byteu(&ctx->gb);
462 if (bytestream2_get_bytes_left(&ctx->gb) < 16)
463 return AVERROR_INVALIDDATA;
464 for (k = 0; k < 4; k++)
465 bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
468 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
469 return AVERROR_INVALIDDATA;
470 for (k = 0; k < 4; k++)
471 memset(dst + i + k * stride, bytestream2_get_byteu(&ctx->gb), 4);
474 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
475 return AVERROR_INVALIDDATA;
476 t = bytestream2_get_byteu(&ctx->gb);
477 for (k = 0; k < 4; k++)
478 memset(dst + i + k * stride, t, 4);
481 if (compr == 4 && !code) {
482 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
483 return AVERROR_INVALIDDATA;
484 skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
489 mx = c37_mv[(mvoff * 255 + code) * 2 ];
490 my = c37_mv[(mvoff * 255 + code) * 2 + 1];
491 codec37_mv(dst + i, prev + i + mx + my * stride,
492 ctx->height, stride, i + mx, j + my);
500 for (j = 0; j < height; j += 4) {
501 for (i = 0; i < width; i += 4) {
505 copy_block4(dst + i, prev + i, stride, stride, 4);
508 code = bytestream2_get_byte(&ctx->gb);
510 if (bytestream2_get_bytes_left(&ctx->gb) < 16)
511 return AVERROR_INVALIDDATA;
512 for (k = 0; k < 4; k++)
513 bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
514 } else if (compr == 4 && !code) {
515 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
516 return AVERROR_INVALIDDATA;
517 skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
522 mx = c37_mv[(mvoff * 255 + code) * 2];
523 my = c37_mv[(mvoff * 255 + code) * 2 + 1];
524 codec37_mv(dst + i, prev + i + mx + my * stride,
525 ctx->height, stride, i + mx, j + my);
534 av_log(ctx->avctx, AV_LOG_ERROR,
535 "subcodec 37 compression %d not implemented\n", compr);
536 return AVERROR_PATCHWELCOME;
542 static int process_block(SANMVideoContext *ctx, uint8_t *dst, uint8_t *prev1,
543 uint8_t *prev2, int stride, int tbl, int size)
549 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
550 return AVERROR_INVALIDDATA;
552 code = bytestream2_get_byteu(&ctx->gb);
557 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
558 return AVERROR_INVALIDDATA;
559 dst[0] = bytestream2_get_byteu(&ctx->gb);
560 dst[1] = bytestream2_get_byteu(&ctx->gb);
561 dst[0+stride] = bytestream2_get_byteu(&ctx->gb);
562 dst[1+stride] = bytestream2_get_byteu(&ctx->gb);
565 if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
566 return AVERROR_INVALIDDATA;
567 if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
569 return AVERROR_INVALIDDATA;
570 dst += size * stride;
571 prev1 += size * stride;
572 prev2 += size * stride;
573 if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
574 return AVERROR_INVALIDDATA;
575 if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
577 return AVERROR_INVALIDDATA;
581 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
582 return AVERROR_INVALIDDATA;
584 t = bytestream2_get_byteu(&ctx->gb);
585 for (k = 0; k < size; k++)
586 memset(dst + k * stride, t, size);
589 if (bytestream2_get_bytes_left(&ctx->gb) < 3)
590 return AVERROR_INVALIDDATA;
592 code = bytestream2_get_byteu(&ctx->gb);
593 pglyph = (size == 8) ? ctx->p8x8glyphs[code] : ctx->p4x4glyphs[code];
594 bytestream2_get_bufferu(&ctx->gb, colors, 2);
596 for (k = 0; k < size; k++)
597 for (t = 0; t < size; t++)
598 dst[t + k * stride] = colors[!*pglyph++];
601 for (k = 0; k < size; k++)
602 memcpy(dst + k * stride, prev1 + k * stride, size);
605 k = bytestream2_tell(&ctx->gb);
606 bytestream2_seek(&ctx->gb, tbl + (code & 7), SEEK_SET);
607 t = bytestream2_get_byte(&ctx->gb);
608 bytestream2_seek(&ctx->gb, k, SEEK_SET);
609 for (k = 0; k < size; k++)
610 memset(dst + k * stride, t, size);
613 int mx = motion_vectors[code][0];
614 int my = motion_vectors[code][1];
615 for (k = 0; k < size; k++)
616 memcpy(dst + k * stride, prev2 + mx + (my + k) * stride, size);
622 static int old_codec47(SANMVideoContext *ctx, int top,
623 int left, int width, int height)
625 int i, j, seq, compr, new_rot, tbl_pos, skip;
626 int stride = ctx->pitch;
627 uint8_t *dst = ((uint8_t*)ctx->frm0) + left + top * stride;
628 uint8_t *prev1 = (uint8_t*)ctx->frm1;
629 uint8_t *prev2 = (uint8_t*)ctx->frm2;
630 uint32_t decoded_size;
632 tbl_pos = bytestream2_tell(&ctx->gb);
633 seq = bytestream2_get_le16(&ctx->gb);
634 compr = bytestream2_get_byte(&ctx->gb);
635 new_rot = bytestream2_get_byte(&ctx->gb);
636 skip = bytestream2_get_byte(&ctx->gb);
637 bytestream2_skip(&ctx->gb, 9);
638 decoded_size = bytestream2_get_le32(&ctx->gb);
639 bytestream2_skip(&ctx->gb, 8);
642 bytestream2_skip(&ctx->gb, 0x8080);
645 memset(prev1, 0, ctx->height * stride);
646 memset(prev2, 0, ctx->height * stride);
648 av_dlog(ctx->avctx, "compression %d\n", compr);
651 if (bytestream2_get_bytes_left(&ctx->gb) < width * height)
652 return AVERROR_INVALIDDATA;
653 for (j = 0; j < height; j++) {
654 for (i = 0; i < width; i++)
655 bytestream2_get_bufferu(&ctx->gb, dst, width);
660 if (bytestream2_get_bytes_left(&ctx->gb) < ((width + 1) >> 1) * ((height + 1) >> 1))
661 return AVERROR_INVALIDDATA;
662 for (j = 0; j < height; j += 2) {
663 for (i = 0; i < width; i += 2) {
664 dst[i] = dst[i + 1] =
665 dst[stride + i] = dst[stride + i + 1] = bytestream2_get_byteu(&ctx->gb);
671 if (seq == ctx->prev_seq + 1) {
672 for (j = 0; j < height; j += 8) {
673 for (i = 0; i < width; i += 8) {
674 if (process_block(ctx, dst + i, prev1 + i, prev2 + i, stride,
676 return AVERROR_INVALIDDATA;
685 memcpy(ctx->frm0, ctx->frm2, ctx->pitch * ctx->height);
688 memcpy(ctx->frm0, ctx->frm1, ctx->pitch * ctx->height);
691 if (rle_decode(ctx, dst, decoded_size))
692 return AVERROR_INVALIDDATA;
695 av_log(ctx->avctx, AV_LOG_ERROR,
696 "subcodec 47 compression %d not implemented\n", compr);
697 return AVERROR_PATCHWELCOME;
699 if (seq == ctx->prev_seq + 1)
700 ctx->rotate_code = new_rot;
702 ctx->rotate_code = 0;
708 static int process_frame_obj(SANMVideoContext *ctx)
710 uint16_t codec, top, left, w, h;
712 codec = bytestream2_get_le16u(&ctx->gb);
713 left = bytestream2_get_le16u(&ctx->gb);
714 top = bytestream2_get_le16u(&ctx->gb);
715 w = bytestream2_get_le16u(&ctx->gb);
716 h = bytestream2_get_le16u(&ctx->gb);
718 if (ctx->width < left + w || ctx->height < top + h) {
719 ctx->avctx->width = FFMAX(left + w, ctx->width);
720 ctx->avctx->height = FFMAX(top + h, ctx->height);
721 init_sizes(ctx, left + w, top + h);
722 if (init_buffers(ctx)) {
723 av_log(ctx->avctx, AV_LOG_ERROR, "error resizing buffers\n");
724 return AVERROR(ENOMEM);
727 bytestream2_skip(&ctx->gb, 4);
729 av_dlog(ctx->avctx, "subcodec %d\n", codec);
733 return old_codec1(ctx, top, left, w, h);
736 return old_codec37(ctx, top, left, w, h);
739 return old_codec47(ctx, top, left, w, h);
742 av_log_ask_for_sample(ctx->avctx, "unknown subcodec %d\n", codec);
743 return AVERROR_PATCHWELCOME;
747 static int decode_0(SANMVideoContext *ctx)
749 uint16_t *frm = ctx->frm0;
752 if (bytestream2_get_bytes_left(&ctx->gb) < ctx->width * ctx->height * 2) {
753 av_log(ctx->avctx, AV_LOG_ERROR, "insufficient data for raw frame\n");
754 return AVERROR_INVALIDDATA;
756 for (y = 0; y < ctx->height; y++) {
757 for (x = 0; x < ctx->width; x++)
758 frm[x] = bytestream2_get_le16u(&ctx->gb);
764 static int decode_nop(SANMVideoContext *ctx)
766 av_log_ask_for_sample(ctx->avctx, "unknown/unsupported compression type\n");
767 return AVERROR_PATCHWELCOME;
770 static void copy_block(uint16_t *pdest, uint16_t *psrc, int block_size, int pitch)
772 uint8_t *dst = (uint8_t *)pdest;
773 uint8_t *src = (uint8_t *)psrc;
774 int stride = pitch * 2;
776 switch (block_size) {
778 copy_block4(dst, src, stride, stride, 2);
781 copy_block8(dst, src, stride, stride, 4);
784 copy_block16(dst, src, stride, stride, 8);
789 static void fill_block(uint16_t *pdest, uint16_t color, int block_size, int pitch)
794 for (y = 0; y < block_size; y++, pdest += pitch)
795 for (x = 0; x < block_size; x++)
799 static int draw_glyph(SANMVideoContext *ctx, uint16_t *dst, int index, uint16_t fg_color,
800 uint16_t bg_color, int block_size, int pitch)
803 uint16_t colors[2] = { fg_color, bg_color };
806 if (index >= NGLYPHS) {
807 av_log(ctx->avctx, AV_LOG_ERROR, "ignoring nonexistent glyph #%u\n", index);
808 return AVERROR_INVALIDDATA;
811 pglyph = block_size == 8 ? ctx->p8x8glyphs[index] : ctx->p4x4glyphs[index];
814 for (y = 0; y < block_size; y++, dst += pitch)
815 for (x = 0; x < block_size; x++)
816 *dst++ = colors[*pglyph++];
820 static int opcode_0xf7(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
822 uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
824 if (block_size == 2) {
827 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
828 return AVERROR_INVALIDDATA;
830 indices = bytestream2_get_le32u(&ctx->gb);
831 dst[0] = ctx->codebook[indices & 0xFF]; indices >>= 8;
832 dst[1] = ctx->codebook[indices & 0xFF]; indices >>= 8;
833 dst[pitch] = ctx->codebook[indices & 0xFF]; indices >>= 8;
834 dst[pitch + 1] = ctx->codebook[indices & 0xFF];
836 uint16_t fgcolor, bgcolor;
839 if (bytestream2_get_bytes_left(&ctx->gb) < 3)
840 return AVERROR_INVALIDDATA;
842 glyph = bytestream2_get_byteu(&ctx->gb);
843 bgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
844 fgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
846 draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
851 static int opcode_0xf8(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
853 uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
855 if (block_size == 2) {
856 if (bytestream2_get_bytes_left(&ctx->gb) < 8)
857 return AVERROR_INVALIDDATA;
859 dst[0] = bytestream2_get_le16u(&ctx->gb);
860 dst[1] = bytestream2_get_le16u(&ctx->gb);
861 dst[pitch] = bytestream2_get_le16u(&ctx->gb);
862 dst[pitch + 1] = bytestream2_get_le16u(&ctx->gb);
864 uint16_t fgcolor, bgcolor;
867 if (bytestream2_get_bytes_left(&ctx->gb) < 5)
868 return AVERROR_INVALIDDATA;
870 glyph = bytestream2_get_byteu(&ctx->gb);
871 bgcolor = bytestream2_get_le16u(&ctx->gb);
872 fgcolor = bytestream2_get_le16u(&ctx->gb);
874 draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
879 static int good_mvec(SANMVideoContext *ctx, int cx, int cy, int mx, int my,
882 int start_pos = cx + mx + (cy + my) * ctx->pitch;
883 int end_pos = start_pos + (block_size - 1) * (ctx->pitch + 1);
885 int good = start_pos >= 0 && end_pos < (ctx->buf_size >> 1);
888 av_log(ctx->avctx, AV_LOG_ERROR, "ignoring invalid motion vector (%i, %i)->(%u, %u), block size = %u\n",
889 cx + mx, cy + my, cx, cy, block_size);
895 static int codec2subblock(SANMVideoContext *ctx, int cx, int cy, int blk_size)
897 int16_t mx, my, index;
900 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
901 return AVERROR_INVALIDDATA;
903 opcode = bytestream2_get_byteu(&ctx->gb);
905 av_dlog(ctx->avctx, "opcode 0x%0X cx %d cy %d blk %d\n", opcode, cx, cy, blk_size);
908 mx = motion_vectors[opcode][0];
909 my = motion_vectors[opcode][1];
911 if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
912 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
913 ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
914 blk_size, ctx->pitch);
918 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
919 return AVERROR_INVALIDDATA;
920 index = bytestream2_get_le16u(&ctx->gb);
922 mx = index % ctx->width;
923 my = index / ctx->width;
925 if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
926 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
927 ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
928 blk_size, ctx->pitch);
932 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
933 ctx->frm1 + cx + ctx->pitch * cy,
934 blk_size, ctx->pitch);
937 opcode_0xf7(ctx, cx, cy, blk_size, ctx->pitch);
941 opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
947 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
948 ctx->small_codebook[opcode - 0xf9], blk_size, ctx->pitch);
951 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
952 return AVERROR_INVALIDDATA;
953 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
954 ctx->codebook[bytestream2_get_byteu(&ctx->gb)], blk_size, ctx->pitch);
957 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
958 return AVERROR_INVALIDDATA;
959 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
960 bytestream2_get_le16u(&ctx->gb), blk_size, ctx->pitch);
964 opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
967 if (codec2subblock(ctx, cx , cy , blk_size))
968 return AVERROR_INVALIDDATA;
969 if (codec2subblock(ctx, cx + blk_size, cy , blk_size))
970 return AVERROR_INVALIDDATA;
971 if (codec2subblock(ctx, cx , cy + blk_size, blk_size))
972 return AVERROR_INVALIDDATA;
973 if (codec2subblock(ctx, cx + blk_size, cy + blk_size, blk_size))
974 return AVERROR_INVALIDDATA;
981 static int decode_2(SANMVideoContext *ctx)
985 for (cy = 0; cy < ctx->aligned_height; cy += 8) {
986 for (cx = 0; cx < ctx->aligned_width; cx += 8) {
987 if (ret = codec2subblock(ctx, cx, cy, 8))
995 static int decode_3(SANMVideoContext *ctx)
997 memcpy(ctx->frm0, ctx->frm2, ctx->frm2_size);
1001 static int decode_4(SANMVideoContext *ctx)
1003 memcpy(ctx->frm0, ctx->frm1, ctx->frm1_size);
1007 static int decode_5(SANMVideoContext *ctx)
1013 uint8_t *dst = (uint8_t*)ctx->frm0;
1015 if (rle_decode(ctx, dst, ctx->buf_size))
1016 return AVERROR_INVALIDDATA;
1019 npixels = ctx->npixels;
1022 *frm++ = av_bswap16(*frm);
1028 static int decode_6(SANMVideoContext *ctx)
1030 int npixels = ctx->npixels;
1031 uint16_t *frm = ctx->frm0;
1033 if (bytestream2_get_bytes_left(&ctx->gb) < npixels) {
1034 av_log(ctx->avctx, AV_LOG_ERROR, "insufficient data for frame\n");
1035 return AVERROR_INVALIDDATA;
1038 *frm++ = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
1043 static int decode_8(SANMVideoContext *ctx)
1045 uint16_t *pdest = ctx->frm0;
1047 long npixels = ctx->npixels;
1049 av_fast_malloc(&ctx->rle_buf, &ctx->rle_buf_size, npixels);
1050 if (!ctx->rle_buf) {
1051 av_log(ctx->avctx, AV_LOG_ERROR, "RLE buffer allocation failed\n");
1052 return AVERROR(ENOMEM);
1054 rsrc = ctx->rle_buf;
1056 if (rle_decode(ctx, rsrc, npixels))
1057 return AVERROR_INVALIDDATA;
1060 *pdest++ = ctx->codebook[*rsrc++];
1065 typedef int (*frm_decoder)(SANMVideoContext *ctx);
1067 static const frm_decoder v1_decoders[] = {
1068 decode_0, decode_nop, decode_2, decode_3, decode_4, decode_5,
1069 decode_6, decode_nop, decode_8
1072 static int read_frame_header(SANMVideoContext *ctx, SANMFrameHeader *hdr)
1076 if ((ret = bytestream2_get_bytes_left(&ctx->gb)) < 560) {
1077 av_log(ctx->avctx, AV_LOG_ERROR, "too short input frame (%d bytes)\n",
1079 return AVERROR_INVALIDDATA;
1081 bytestream2_skip(&ctx->gb, 8); // skip pad
1083 hdr->width = bytestream2_get_le32u(&ctx->gb);
1084 hdr->height = bytestream2_get_le32u(&ctx->gb);
1086 if (hdr->width != ctx->width || hdr->height != ctx->height) {
1087 av_log(ctx->avctx, AV_LOG_ERROR, "variable size frames are not implemented\n");
1088 return AVERROR_PATCHWELCOME;
1091 hdr->seq_num = bytestream2_get_le16u(&ctx->gb);
1092 hdr->codec = bytestream2_get_byteu(&ctx->gb);
1093 hdr->rotate_code = bytestream2_get_byteu(&ctx->gb);
1095 bytestream2_skip(&ctx->gb, 4); // skip pad
1097 for (i = 0; i < 4; i++)
1098 ctx->small_codebook[i] = bytestream2_get_le16u(&ctx->gb);
1099 hdr->bg_color = bytestream2_get_le16u(&ctx->gb);
1101 bytestream2_skip(&ctx->gb, 2); // skip pad
1103 hdr->rle_output_size = bytestream2_get_le32u(&ctx->gb);
1104 for (i = 0; i < 256; i++)
1105 ctx->codebook[i] = bytestream2_get_le16u(&ctx->gb);
1107 bytestream2_skip(&ctx->gb, 8); // skip pad
1109 av_dlog(ctx->avctx, "subcodec %d\n", hdr->codec);
1113 static void fill_frame(uint16_t *pbuf, int buf_size, uint16_t color)
1119 static int copy_output(SANMVideoContext *ctx, SANMFrameHeader *hdr)
1122 const uint8_t *src = (uint8_t*) ctx->frm0;
1123 int ret, dstpitch, height = ctx->height;
1124 int srcpitch = ctx->pitch * (hdr ? sizeof(ctx->frm0[0]) : 1);
1126 if ((ret = ff_get_buffer(ctx->avctx, ctx->output)) < 0) {
1127 av_log(ctx->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1131 dst = ctx->output->data[0];
1132 dstpitch = ctx->output->linesize[0];
1135 memcpy(dst, src, srcpitch);
1143 static int decode_frame(AVCodecContext *avctx, void *data,
1144 int *got_frame_ptr, AVPacket *pkt)
1146 SANMVideoContext *ctx = avctx->priv_data;
1149 bytestream2_init(&ctx->gb, pkt->data, pkt->size);
1150 if (ctx->output->data[0])
1151 avctx->release_buffer(avctx, ctx->output);
1153 if (!ctx->version) {
1156 while (bytestream2_get_bytes_left(&ctx->gb) >= 8) {
1160 sig = bytestream2_get_be32u(&ctx->gb);
1161 size = bytestream2_get_be32u(&ctx->gb);
1162 pos = bytestream2_tell(&ctx->gb);
1164 if (bytestream2_get_bytes_left(&ctx->gb) < size) {
1165 av_log(avctx, AV_LOG_ERROR, "incorrect chunk size %d\n", size);
1169 case MKBETAG('N', 'P', 'A', 'L'):
1170 if (size != 256 * 3) {
1171 av_log(avctx, AV_LOG_ERROR, "incorrect palette block size %d\n",
1173 return AVERROR_INVALIDDATA;
1175 for (i = 0; i < 256; i++)
1176 ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
1178 case MKBETAG('F', 'O', 'B', 'J'):
1180 return AVERROR_INVALIDDATA;
1181 if (ret = process_frame_obj(ctx))
1184 case MKBETAG('X', 'P', 'A', 'L'):
1185 if (size == 6 || size == 4) {
1189 for (i = 0; i < 256; i++) {
1190 for (j = 0; j < 3; j++) {
1191 int t = (ctx->pal[i] >> (16 - j * 8)) & 0xFF;
1192 tmp[j] = av_clip_uint8((t * 129 + ctx->delta_pal[i * 3 + j]) >> 7);
1194 ctx->pal[i] = 0xFFU << 24 | AV_RB24(tmp);
1197 if (size < 768 * 2 + 4) {
1198 av_log(avctx, AV_LOG_ERROR, "incorrect palette change block size %d\n",
1200 return AVERROR_INVALIDDATA;
1202 bytestream2_skipu(&ctx->gb, 4);
1203 for (i = 0; i < 768; i++)
1204 ctx->delta_pal[i] = bytestream2_get_le16u(&ctx->gb);
1205 if (size >= 768 * 5 + 4) {
1206 for (i = 0; i < 256; i++)
1207 ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
1209 memset(ctx->pal, 0, sizeof(ctx->pal));
1213 case MKBETAG('S', 'T', 'O', 'R'):
1216 case MKBETAG('F', 'T', 'C', 'H'):
1217 memcpy(ctx->frm0, ctx->stored_frame, ctx->buf_size);
1220 bytestream2_skip(&ctx->gb, size);
1221 av_log(avctx, AV_LOG_DEBUG, "unknown/unsupported chunk %x\n", sig);
1225 bytestream2_seek(&ctx->gb, pos + size, SEEK_SET);
1227 bytestream2_skip(&ctx->gb, 1);
1230 memcpy(ctx->stored_frame, ctx->frm0, ctx->buf_size);
1231 if ((ret = copy_output(ctx, NULL)))
1233 memcpy(ctx->output->data[1], ctx->pal, 1024);
1235 SANMFrameHeader header;
1237 if ((ret = read_frame_header(ctx, &header)))
1240 ctx->rotate_code = header.rotate_code;
1241 if ((ctx->output->key_frame = !header.seq_num)) {
1242 ctx->output->pict_type = AV_PICTURE_TYPE_I;
1243 fill_frame(ctx->frm1, ctx->npixels, header.bg_color);
1244 fill_frame(ctx->frm2, ctx->npixels, header.bg_color);
1246 ctx->output->pict_type = AV_PICTURE_TYPE_P;
1249 if (header.codec < FF_ARRAY_ELEMS(v1_decoders)) {
1250 if ((ret = v1_decoders[header.codec](ctx))) {
1251 av_log(avctx, AV_LOG_ERROR,
1252 "subcodec %d: error decoding frame\n", header.codec);
1256 av_log_ask_for_sample(avctx, "subcodec %d is not implemented\n",
1258 return AVERROR_PATCHWELCOME;
1261 if ((ret = copy_output(ctx, &header)))
1264 if (ctx->rotate_code)
1265 rotate_bufs(ctx, ctx->rotate_code);
1268 *(AVFrame*)data = *ctx->output;
1273 AVCodec ff_sanm_decoder = {
1275 .type = AVMEDIA_TYPE_VIDEO,
1276 .id = AV_CODEC_ID_SANM,
1277 .priv_data_size = sizeof(SANMVideoContext),
1278 .init = decode_init,
1279 .close = decode_end,
1280 .decode = decode_frame,
1281 .capabilities = CODEC_CAP_DR1,
1282 .long_name = NULL_IF_CONFIG_SMALL("LucasArts SMUSH video"),