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 "copy_block.h"
27 #include "bytestream.h"
29 #include "libavutil/bswap.h"
30 #include "libavutil/imgutils.h"
31 #include "sanm_data.h"
32 #include "libavutil/avassert.h"
37 AVCodecContext *avctx;
40 int version, subversion;
42 int16_t delta_pal[768];
46 int aligned_width, aligned_height;
50 uint16_t *frm0, *frm1, *frm2;
51 uint8_t *stored_frame;
52 uint32_t frm0_size, frm1_size, frm2_size;
53 uint32_t stored_frame_size;
56 unsigned int rle_buf_size;
60 long npixels, buf_size;
62 uint16_t codebook[256];
63 uint16_t small_codebook[4];
65 int8_t p4x4glyphs[NGLYPHS][16];
66 int8_t p8x8glyphs[NGLYPHS][64];
70 int seq_num, codec, rotate_code, rle_output_size;
73 uint32_t width, height;
93 * Return enum GlyphEdge of box where point (x, y) lies.
95 * @param x x point coordinate
96 * @param y y point coordinate
97 * @param edge_size box width/height.
99 static enum GlyphEdge which_edge(int x, int y, int edge_size)
101 const int edge_max = edge_size - 1;
105 } else if (y == edge_max) {
109 } else if (x == edge_max) {
116 static enum GlyphDir which_direction(enum GlyphEdge edge0, enum GlyphEdge edge1)
118 if ((edge0 == LEFT_EDGE && edge1 == RIGHT_EDGE) ||
119 (edge1 == LEFT_EDGE && edge0 == RIGHT_EDGE) ||
120 (edge0 == BOTTOM_EDGE && edge1 != TOP_EDGE) ||
121 (edge1 == BOTTOM_EDGE && edge0 != TOP_EDGE)) {
123 } else if ((edge0 == TOP_EDGE && edge1 != BOTTOM_EDGE) ||
124 (edge1 == TOP_EDGE && edge0 != BOTTOM_EDGE)) {
126 } else if ((edge0 == LEFT_EDGE && edge1 != RIGHT_EDGE) ||
127 (edge1 == LEFT_EDGE && edge0 != RIGHT_EDGE)) {
129 } else if ((edge0 == TOP_EDGE && edge1 == BOTTOM_EDGE) ||
130 (edge1 == TOP_EDGE && edge0 == BOTTOM_EDGE) ||
131 (edge0 == RIGHT_EDGE && edge1 != LEFT_EDGE) ||
132 (edge1 == RIGHT_EDGE && edge0 != LEFT_EDGE)) {
140 * Interpolate two points.
142 static void interp_point(int8_t *points, int x0, int y0, int x1, int y1,
143 int pos, int npoints)
146 points[0] = (x0 * pos + x1 * (npoints - pos) + (npoints >> 1)) / npoints;
147 points[1] = (y0 * pos + y1 * (npoints - pos) + (npoints >> 1)) / npoints;
155 * Construct glyphs by iterating through vectors coordinates.
157 * @param pglyphs pointer to table where glyphs are stored
158 * @param xvec pointer to x component of vectors coordinates
159 * @param yvec pointer to y component of vectors coordinates
160 * @param side_length glyph width/height.
162 static void make_glyphs(int8_t *pglyphs, const int8_t *xvec, const int8_t *yvec,
163 const int side_length)
165 const int glyph_size = side_length * side_length;
166 int8_t *pglyph = pglyphs;
169 for (i = 0; i < GLYPH_COORD_VECT_SIZE; i++) {
172 enum GlyphEdge edge0 = which_edge(x0, y0, side_length);
174 for (j = 0; j < GLYPH_COORD_VECT_SIZE; j++, pglyph += glyph_size) {
177 enum GlyphEdge edge1 = which_edge(x1, y1, side_length);
178 enum GlyphDir dir = which_direction(edge0, edge1);
179 int npoints = FFMAX(FFABS(x1 - x0), FFABS(y1 - y0));
182 for (ipoint = 0; ipoint <= npoints; ipoint++) {
186 interp_point(point, x0, y0, x1, y1, ipoint, npoints);
190 for (irow = point[1]; irow >= 0; irow--)
191 pglyph[point[0] + irow * side_length] = 1;
195 for (irow = point[1]; irow < side_length; irow++)
196 pglyph[point[0] + irow * side_length] = 1;
200 for (icol = point[0]; icol >= 0; icol--)
201 pglyph[icol + point[1] * side_length] = 1;
205 for (icol = point[0]; icol < side_length; icol++)
206 pglyph[icol + point[1] * side_length] = 1;
214 static void init_sizes(SANMVideoContext *ctx, int width, int height)
217 ctx->height = height;
218 ctx->npixels = width * height;
220 ctx->aligned_width = FFALIGN(width, 8);
221 ctx->aligned_height = FFALIGN(height, 8);
223 ctx->buf_size = ctx->aligned_width * ctx->aligned_height * sizeof(ctx->frm0[0]);
227 static void destroy_buffers(SANMVideoContext *ctx)
229 av_freep(&ctx->frm0);
230 av_freep(&ctx->frm1);
231 av_freep(&ctx->frm2);
232 av_freep(&ctx->stored_frame);
233 av_freep(&ctx->rle_buf);
239 static av_cold int init_buffers(SANMVideoContext *ctx)
241 av_fast_padded_malloc(&ctx->frm0, &ctx->frm0_size, ctx->buf_size);
242 av_fast_padded_malloc(&ctx->frm1, &ctx->frm1_size, ctx->buf_size);
243 av_fast_padded_malloc(&ctx->frm2, &ctx->frm2_size, ctx->buf_size);
245 av_fast_padded_malloc(&ctx->stored_frame, &ctx->stored_frame_size, ctx->buf_size);
247 if (!ctx->frm0 || !ctx->frm1 || !ctx->frm2 || (!ctx->stored_frame && !ctx->version)) {
248 destroy_buffers(ctx);
249 return AVERROR(ENOMEM);
255 static void rotate_bufs(SANMVideoContext *ctx, int rotate_code)
257 av_dlog(ctx->avctx, "rotate %d\n", rotate_code);
258 if (rotate_code == 2)
259 FFSWAP(uint16_t*, ctx->frm1, ctx->frm2);
260 FFSWAP(uint16_t*, ctx->frm2, ctx->frm0);
263 static av_cold int decode_init(AVCodecContext *avctx)
265 SANMVideoContext *ctx = avctx->priv_data;
268 ctx->version = !avctx->extradata_size;
270 avctx->pix_fmt = ctx->version ? AV_PIX_FMT_RGB565 : AV_PIX_FMT_PAL8;
272 init_sizes(ctx, avctx->width, avctx->height);
273 if (init_buffers(ctx)) {
274 av_log(avctx, AV_LOG_ERROR, "error allocating buffers\n");
275 return AVERROR(ENOMEM);
278 make_glyphs(ctx->p4x4glyphs[0], glyph4_x, glyph4_y, 4);
279 make_glyphs(ctx->p8x8glyphs[0], glyph8_x, glyph8_y, 8);
284 if (avctx->extradata_size < 1026) {
285 av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");
286 return AVERROR_INVALIDDATA;
289 ctx->subversion = AV_RL16(avctx->extradata);
290 for (i = 0; i < 256; i++)
291 ctx->pal[i] = 0xFFU << 24 | AV_RL32(avctx->extradata + 2 + i * 4);
297 static av_cold int decode_end(AVCodecContext *avctx)
299 SANMVideoContext *ctx = avctx->priv_data;
301 destroy_buffers(ctx);
306 static int rle_decode(SANMVideoContext *ctx, uint8_t *dst, const int out_size)
308 int opcode, color, run_len, left = out_size;
311 opcode = bytestream2_get_byte(&ctx->gb);
312 run_len = (opcode >> 1) + 1;
313 if (run_len > left || bytestream2_get_bytes_left(&ctx->gb) <= 0)
314 return AVERROR_INVALIDDATA;
317 color = bytestream2_get_byte(&ctx->gb);
318 memset(dst, color, run_len);
320 if (bytestream2_get_bytes_left(&ctx->gb) < run_len)
321 return AVERROR_INVALIDDATA;
322 bytestream2_get_bufferu(&ctx->gb, dst, run_len);
332 static int old_codec1(SANMVideoContext *ctx, int top,
333 int left, int width, int height)
335 uint8_t *dst = ((uint8_t*)ctx->frm0) + left + top * ctx->pitch;
336 int i, j, len, flag, code, val, pos, end;
338 for (i = 0; i < height; i++) {
341 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
342 return AVERROR_INVALIDDATA;
344 len = bytestream2_get_le16u(&ctx->gb);
345 end = bytestream2_tell(&ctx->gb) + len;
347 while (bytestream2_tell(&ctx->gb) < end) {
348 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
349 return AVERROR_INVALIDDATA;
351 code = bytestream2_get_byteu(&ctx->gb);
353 code = (code >> 1) + 1;
354 if (pos + code > width)
355 return AVERROR_INVALIDDATA;
357 val = bytestream2_get_byteu(&ctx->gb);
359 memset(dst + pos, val, code);
362 if (bytestream2_get_bytes_left(&ctx->gb) < code)
363 return AVERROR_INVALIDDATA;
364 for (j = 0; j < code; j++) {
365 val = bytestream2_get_byteu(&ctx->gb);
374 ctx->rotate_code = 0;
379 static inline void codec37_mv(uint8_t *dst, const uint8_t *src,
380 int height, int stride, int x, int y)
384 pos = x + y * stride;
385 for (j = 0; j < 4; j++) {
386 for (i = 0; i < 4; i++) {
387 if ((pos + i) < 0 || (pos + i) >= height * stride)
398 static int old_codec37(SANMVideoContext *ctx, int top,
399 int left, int width, int height)
401 int stride = ctx->pitch;
404 int compr, mvoff, seq, flags;
405 uint32_t decoded_size;
408 compr = bytestream2_get_byte(&ctx->gb);
409 mvoff = bytestream2_get_byte(&ctx->gb);
410 seq = bytestream2_get_le16(&ctx->gb);
411 decoded_size = bytestream2_get_le32(&ctx->gb);
412 bytestream2_skip(&ctx->gb, 4);
413 flags = bytestream2_get_byte(&ctx->gb);
414 bytestream2_skip(&ctx->gb, 3);
416 if (decoded_size > ctx->height * stride - left - top * stride) {
417 decoded_size = ctx->height * stride - left - top * stride;
418 av_log(ctx->avctx, AV_LOG_WARNING, "decoded size is too large\n");
421 ctx->rotate_code = 0;
423 if (((seq & 1) || !(flags & 1)) && (compr && compr != 2))
426 dst = ((uint8_t*)ctx->frm0) + left + top * stride;
427 prev = ((uint8_t*)ctx->frm2) + left + top * stride;
430 av_log(ctx->avctx, AV_LOG_ERROR, "invalid motion base value %d\n", mvoff);
431 return AVERROR_INVALIDDATA;
433 av_dlog(ctx->avctx, "compression %d\n", compr);
436 for (i = 0; i < height; i++) {
437 bytestream2_get_buffer(&ctx->gb, dst, width);
440 memset(ctx->frm1, 0, ctx->height * stride);
441 memset(ctx->frm2, 0, ctx->height * stride);
444 if (rle_decode(ctx, dst, decoded_size))
445 return AVERROR_INVALIDDATA;
446 memset(ctx->frm1, 0, ctx->frm1_size);
447 memset(ctx->frm2, 0, ctx->frm2_size);
452 for (j = 0; j < height; j += 4) {
453 for (i = 0; i < width; i += 4) {
457 copy_block4(dst + i, prev + i, stride, stride, 4);
460 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
461 return AVERROR_INVALIDDATA;
462 code = bytestream2_get_byteu(&ctx->gb);
465 if (bytestream2_get_bytes_left(&ctx->gb) < 16)
466 return AVERROR_INVALIDDATA;
467 for (k = 0; k < 4; k++)
468 bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
471 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
472 return AVERROR_INVALIDDATA;
473 for (k = 0; k < 4; k++)
474 memset(dst + i + k * stride, bytestream2_get_byteu(&ctx->gb), 4);
477 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
478 return AVERROR_INVALIDDATA;
479 t = bytestream2_get_byteu(&ctx->gb);
480 for (k = 0; k < 4; k++)
481 memset(dst + i + k * stride, t, 4);
484 if (compr == 4 && !code) {
485 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
486 return AVERROR_INVALIDDATA;
487 skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
492 mx = c37_mv[(mvoff * 255 + code) * 2 ];
493 my = c37_mv[(mvoff * 255 + code) * 2 + 1];
494 codec37_mv(dst + i, prev + i + mx + my * stride,
495 ctx->height, stride, i + mx, j + my);
503 for (j = 0; j < height; j += 4) {
504 for (i = 0; i < width; i += 4) {
508 copy_block4(dst + i, prev + i, stride, stride, 4);
511 code = bytestream2_get_byte(&ctx->gb);
513 if (bytestream2_get_bytes_left(&ctx->gb) < 16)
514 return AVERROR_INVALIDDATA;
515 for (k = 0; k < 4; k++)
516 bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
517 } else if (compr == 4 && !code) {
518 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
519 return AVERROR_INVALIDDATA;
520 skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
525 mx = c37_mv[(mvoff * 255 + code) * 2];
526 my = c37_mv[(mvoff * 255 + code) * 2 + 1];
527 codec37_mv(dst + i, prev + i + mx + my * stride,
528 ctx->height, stride, i + mx, j + my);
537 av_log(ctx->avctx, AV_LOG_ERROR,
538 "subcodec 37 compression %d not implemented\n", compr);
539 return AVERROR_PATCHWELCOME;
545 static int process_block(SANMVideoContext *ctx, uint8_t *dst, uint8_t *prev1,
546 uint8_t *prev2, int stride, int tbl, int size)
552 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
553 return AVERROR_INVALIDDATA;
555 code = bytestream2_get_byteu(&ctx->gb);
560 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
561 return AVERROR_INVALIDDATA;
562 dst[0] = bytestream2_get_byteu(&ctx->gb);
563 dst[1] = bytestream2_get_byteu(&ctx->gb);
564 dst[0+stride] = bytestream2_get_byteu(&ctx->gb);
565 dst[1+stride] = bytestream2_get_byteu(&ctx->gb);
568 if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
569 return AVERROR_INVALIDDATA;
570 if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
572 return AVERROR_INVALIDDATA;
573 dst += size * stride;
574 prev1 += size * stride;
575 prev2 += size * stride;
576 if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
577 return AVERROR_INVALIDDATA;
578 if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
580 return AVERROR_INVALIDDATA;
584 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
585 return AVERROR_INVALIDDATA;
587 t = bytestream2_get_byteu(&ctx->gb);
588 for (k = 0; k < size; k++)
589 memset(dst + k * stride, t, size);
592 if (bytestream2_get_bytes_left(&ctx->gb) < 3)
593 return AVERROR_INVALIDDATA;
595 code = bytestream2_get_byteu(&ctx->gb);
596 pglyph = (size == 8) ? ctx->p8x8glyphs[code] : ctx->p4x4glyphs[code];
597 bytestream2_get_bufferu(&ctx->gb, colors, 2);
599 for (k = 0; k < size; k++)
600 for (t = 0; t < size; t++)
601 dst[t + k * stride] = colors[!*pglyph++];
604 for (k = 0; k < size; k++)
605 memcpy(dst + k * stride, prev1 + k * stride, size);
608 k = bytestream2_tell(&ctx->gb);
609 bytestream2_seek(&ctx->gb, tbl + (code & 7), SEEK_SET);
610 t = bytestream2_get_byte(&ctx->gb);
611 bytestream2_seek(&ctx->gb, k, SEEK_SET);
612 for (k = 0; k < size; k++)
613 memset(dst + k * stride, t, size);
616 int mx = motion_vectors[code][0];
617 int my = motion_vectors[code][1];
618 int index = prev2 - (const uint8_t*)ctx->frm2;
620 av_assert2(index >= 0 && index < (ctx->buf_size>>1));
622 if (index < - mx - my*stride ||
623 (ctx->buf_size>>1) - index < mx + size + (my + size - 1)*stride) {
624 av_log(ctx->avctx, AV_LOG_ERROR, "MV is invalid \n");
625 return AVERROR_INVALIDDATA;
628 for (k = 0; k < size; k++)
629 memcpy(dst + k * stride, prev2 + mx + (my + k) * stride, size);
635 static int old_codec47(SANMVideoContext *ctx, int top,
636 int left, int width, int height)
638 int i, j, seq, compr, new_rot, tbl_pos, skip;
639 int stride = ctx->pitch;
640 uint8_t *dst = ((uint8_t*)ctx->frm0) + left + top * stride;
641 uint8_t *prev1 = (uint8_t*)ctx->frm1;
642 uint8_t *prev2 = (uint8_t*)ctx->frm2;
643 uint32_t decoded_size;
645 tbl_pos = bytestream2_tell(&ctx->gb);
646 seq = bytestream2_get_le16(&ctx->gb);
647 compr = bytestream2_get_byte(&ctx->gb);
648 new_rot = bytestream2_get_byte(&ctx->gb);
649 skip = bytestream2_get_byte(&ctx->gb);
650 bytestream2_skip(&ctx->gb, 9);
651 decoded_size = bytestream2_get_le32(&ctx->gb);
652 bytestream2_skip(&ctx->gb, 8);
654 if (decoded_size > ctx->height * stride - left - top * stride) {
655 decoded_size = ctx->height * stride - left - top * stride;
656 av_log(ctx->avctx, AV_LOG_WARNING, "decoded size is too large\n");
660 bytestream2_skip(&ctx->gb, 0x8080);
663 memset(prev1, 0, ctx->height * stride);
664 memset(prev2, 0, ctx->height * stride);
666 av_dlog(ctx->avctx, "compression %d\n", compr);
669 if (bytestream2_get_bytes_left(&ctx->gb) < width * height)
670 return AVERROR_INVALIDDATA;
671 for (j = 0; j < height; j++) {
672 bytestream2_get_bufferu(&ctx->gb, dst, width);
677 if (bytestream2_get_bytes_left(&ctx->gb) < ((width + 1) >> 1) * ((height + 1) >> 1))
678 return AVERROR_INVALIDDATA;
679 for (j = 0; j < height; j += 2) {
680 for (i = 0; i < width; i += 2) {
681 dst[i] = dst[i + 1] =
682 dst[stride + i] = dst[stride + i + 1] = bytestream2_get_byteu(&ctx->gb);
688 if (seq == ctx->prev_seq + 1) {
689 for (j = 0; j < height; j += 8) {
690 for (i = 0; i < width; i += 8) {
691 if (process_block(ctx, dst + i, prev1 + i, prev2 + i, stride,
693 return AVERROR_INVALIDDATA;
702 memcpy(ctx->frm0, ctx->frm2, ctx->pitch * ctx->height);
705 memcpy(ctx->frm0, ctx->frm1, ctx->pitch * ctx->height);
708 if (rle_decode(ctx, dst, decoded_size))
709 return AVERROR_INVALIDDATA;
712 av_log(ctx->avctx, AV_LOG_ERROR,
713 "subcodec 47 compression %d not implemented\n", compr);
714 return AVERROR_PATCHWELCOME;
716 if (seq == ctx->prev_seq + 1)
717 ctx->rotate_code = new_rot;
719 ctx->rotate_code = 0;
725 static int process_frame_obj(SANMVideoContext *ctx)
727 uint16_t codec, top, left, w, h;
729 codec = bytestream2_get_le16u(&ctx->gb);
730 left = bytestream2_get_le16u(&ctx->gb);
731 top = bytestream2_get_le16u(&ctx->gb);
732 w = bytestream2_get_le16u(&ctx->gb);
733 h = bytestream2_get_le16u(&ctx->gb);
736 av_log(ctx->avctx, AV_LOG_ERROR, "dimensions are invalid\n");
737 return AVERROR_INVALIDDATA;
740 if (ctx->width < left + w || ctx->height < top + h) {
741 int ret = ff_set_dimensions(ctx->avctx, FFMAX(left + w, ctx->width),
742 FFMAX(top + h, ctx->height));
745 init_sizes(ctx, FFMAX(left + w, ctx->width),
746 FFMAX(top + h, ctx->height));
747 if (init_buffers(ctx)) {
748 av_log(ctx->avctx, AV_LOG_ERROR, "error resizing buffers\n");
749 return AVERROR(ENOMEM);
752 bytestream2_skip(&ctx->gb, 4);
754 av_dlog(ctx->avctx, "subcodec %d\n", codec);
758 return old_codec1(ctx, top, left, w, h);
761 return old_codec37(ctx, top, left, w, h);
764 return old_codec47(ctx, top, left, w, h);
767 avpriv_request_sample(ctx->avctx, "unknown subcodec %d", codec);
768 return AVERROR_PATCHWELCOME;
772 static int decode_0(SANMVideoContext *ctx)
774 uint16_t *frm = ctx->frm0;
777 if (bytestream2_get_bytes_left(&ctx->gb) < ctx->width * ctx->height * 2) {
778 av_log(ctx->avctx, AV_LOG_ERROR, "insufficient data for raw frame\n");
779 return AVERROR_INVALIDDATA;
781 for (y = 0; y < ctx->height; y++) {
782 for (x = 0; x < ctx->width; x++)
783 frm[x] = bytestream2_get_le16u(&ctx->gb);
789 static int decode_nop(SANMVideoContext *ctx)
791 avpriv_request_sample(ctx->avctx, "unknown/unsupported compression type");
792 return AVERROR_PATCHWELCOME;
795 static void copy_block(uint16_t *pdest, uint16_t *psrc, int block_size, int pitch)
797 uint8_t *dst = (uint8_t *)pdest;
798 uint8_t *src = (uint8_t *)psrc;
799 int stride = pitch * 2;
801 switch (block_size) {
803 copy_block4(dst, src, stride, stride, 2);
806 copy_block8(dst, src, stride, stride, 4);
809 copy_block16(dst, src, stride, stride, 8);
814 static void fill_block(uint16_t *pdest, uint16_t color, int block_size, int pitch)
819 for (y = 0; y < block_size; y++, pdest += pitch)
820 for (x = 0; x < block_size; x++)
824 static int draw_glyph(SANMVideoContext *ctx, uint16_t *dst, int index, uint16_t fg_color,
825 uint16_t bg_color, int block_size, int pitch)
828 uint16_t colors[2] = { fg_color, bg_color };
831 if (index >= NGLYPHS) {
832 av_log(ctx->avctx, AV_LOG_ERROR, "ignoring nonexistent glyph #%u\n", index);
833 return AVERROR_INVALIDDATA;
836 pglyph = block_size == 8 ? ctx->p8x8glyphs[index] : ctx->p4x4glyphs[index];
839 for (y = 0; y < block_size; y++, dst += pitch)
840 for (x = 0; x < block_size; x++)
841 *dst++ = colors[*pglyph++];
845 static int opcode_0xf7(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
847 uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
849 if (block_size == 2) {
852 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
853 return AVERROR_INVALIDDATA;
855 indices = bytestream2_get_le32u(&ctx->gb);
856 dst[0] = ctx->codebook[indices & 0xFF]; indices >>= 8;
857 dst[1] = ctx->codebook[indices & 0xFF]; indices >>= 8;
858 dst[pitch] = ctx->codebook[indices & 0xFF]; indices >>= 8;
859 dst[pitch + 1] = ctx->codebook[indices & 0xFF];
861 uint16_t fgcolor, bgcolor;
864 if (bytestream2_get_bytes_left(&ctx->gb) < 3)
865 return AVERROR_INVALIDDATA;
867 glyph = bytestream2_get_byteu(&ctx->gb);
868 bgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
869 fgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
871 draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
876 static int opcode_0xf8(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
878 uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
880 if (block_size == 2) {
881 if (bytestream2_get_bytes_left(&ctx->gb) < 8)
882 return AVERROR_INVALIDDATA;
884 dst[0] = bytestream2_get_le16u(&ctx->gb);
885 dst[1] = bytestream2_get_le16u(&ctx->gb);
886 dst[pitch] = bytestream2_get_le16u(&ctx->gb);
887 dst[pitch + 1] = bytestream2_get_le16u(&ctx->gb);
889 uint16_t fgcolor, bgcolor;
892 if (bytestream2_get_bytes_left(&ctx->gb) < 5)
893 return AVERROR_INVALIDDATA;
895 glyph = bytestream2_get_byteu(&ctx->gb);
896 bgcolor = bytestream2_get_le16u(&ctx->gb);
897 fgcolor = bytestream2_get_le16u(&ctx->gb);
899 draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
904 static int good_mvec(SANMVideoContext *ctx, int cx, int cy, int mx, int my,
907 int start_pos = cx + mx + (cy + my) * ctx->pitch;
908 int end_pos = start_pos + (block_size - 1) * (ctx->pitch + 1);
910 int good = start_pos >= 0 && end_pos < (ctx->buf_size >> 1);
913 av_log(ctx->avctx, AV_LOG_ERROR, "ignoring invalid motion vector (%i, %i)->(%u, %u), block size = %u\n",
914 cx + mx, cy + my, cx, cy, block_size);
920 static int codec2subblock(SANMVideoContext *ctx, int cx, int cy, int blk_size)
922 int16_t mx, my, index;
925 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
926 return AVERROR_INVALIDDATA;
928 opcode = bytestream2_get_byteu(&ctx->gb);
930 av_dlog(ctx->avctx, "opcode 0x%0X cx %d cy %d blk %d\n", opcode, cx, cy, blk_size);
933 mx = motion_vectors[opcode][0];
934 my = motion_vectors[opcode][1];
936 if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
937 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
938 ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
939 blk_size, ctx->pitch);
943 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
944 return AVERROR_INVALIDDATA;
945 index = bytestream2_get_le16u(&ctx->gb);
947 mx = index % ctx->width;
948 my = index / ctx->width;
950 if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
951 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
952 ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
953 blk_size, ctx->pitch);
957 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
958 ctx->frm1 + cx + ctx->pitch * cy,
959 blk_size, ctx->pitch);
962 opcode_0xf7(ctx, cx, cy, blk_size, ctx->pitch);
966 opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
972 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
973 ctx->small_codebook[opcode - 0xf9], blk_size, ctx->pitch);
976 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
977 return AVERROR_INVALIDDATA;
978 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
979 ctx->codebook[bytestream2_get_byteu(&ctx->gb)], blk_size, ctx->pitch);
982 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
983 return AVERROR_INVALIDDATA;
984 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
985 bytestream2_get_le16u(&ctx->gb), blk_size, ctx->pitch);
989 opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
992 if (codec2subblock(ctx, cx , cy , blk_size))
993 return AVERROR_INVALIDDATA;
994 if (codec2subblock(ctx, cx + blk_size, cy , blk_size))
995 return AVERROR_INVALIDDATA;
996 if (codec2subblock(ctx, cx , cy + blk_size, blk_size))
997 return AVERROR_INVALIDDATA;
998 if (codec2subblock(ctx, cx + blk_size, cy + blk_size, blk_size))
999 return AVERROR_INVALIDDATA;
1006 static int decode_2(SANMVideoContext *ctx)
1010 for (cy = 0; cy < ctx->aligned_height; cy += 8) {
1011 for (cx = 0; cx < ctx->aligned_width; cx += 8) {
1012 if (ret = codec2subblock(ctx, cx, cy, 8))
1020 static int decode_3(SANMVideoContext *ctx)
1022 memcpy(ctx->frm0, ctx->frm2, ctx->frm2_size);
1026 static int decode_4(SANMVideoContext *ctx)
1028 memcpy(ctx->frm0, ctx->frm1, ctx->frm1_size);
1032 static int decode_5(SANMVideoContext *ctx)
1038 uint8_t *dst = (uint8_t*)ctx->frm0;
1040 if (rle_decode(ctx, dst, ctx->buf_size))
1041 return AVERROR_INVALIDDATA;
1044 npixels = ctx->npixels;
1047 *frm = av_bswap16(*frm);
1055 static int decode_6(SANMVideoContext *ctx)
1057 int npixels = ctx->npixels;
1058 uint16_t *frm = ctx->frm0;
1060 if (bytestream2_get_bytes_left(&ctx->gb) < npixels) {
1061 av_log(ctx->avctx, AV_LOG_ERROR, "insufficient data for frame\n");
1062 return AVERROR_INVALIDDATA;
1065 *frm++ = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
1070 static int decode_8(SANMVideoContext *ctx)
1072 uint16_t *pdest = ctx->frm0;
1074 long npixels = ctx->npixels;
1076 av_fast_malloc(&ctx->rle_buf, &ctx->rle_buf_size, npixels);
1077 if (!ctx->rle_buf) {
1078 av_log(ctx->avctx, AV_LOG_ERROR, "RLE buffer allocation failed\n");
1079 return AVERROR(ENOMEM);
1081 rsrc = ctx->rle_buf;
1083 if (rle_decode(ctx, rsrc, npixels))
1084 return AVERROR_INVALIDDATA;
1087 *pdest++ = ctx->codebook[*rsrc++];
1092 typedef int (*frm_decoder)(SANMVideoContext *ctx);
1094 static const frm_decoder v1_decoders[] = {
1095 decode_0, decode_nop, decode_2, decode_3, decode_4, decode_5,
1096 decode_6, decode_nop, decode_8
1099 static int read_frame_header(SANMVideoContext *ctx, SANMFrameHeader *hdr)
1103 if ((ret = bytestream2_get_bytes_left(&ctx->gb)) < 560) {
1104 av_log(ctx->avctx, AV_LOG_ERROR, "too short input frame (%d bytes)\n",
1106 return AVERROR_INVALIDDATA;
1108 bytestream2_skip(&ctx->gb, 8); // skip pad
1110 hdr->width = bytestream2_get_le32u(&ctx->gb);
1111 hdr->height = bytestream2_get_le32u(&ctx->gb);
1113 if (hdr->width != ctx->width || hdr->height != ctx->height) {
1114 av_log(ctx->avctx, AV_LOG_ERROR, "variable size frames are not implemented\n");
1115 return AVERROR_PATCHWELCOME;
1118 hdr->seq_num = bytestream2_get_le16u(&ctx->gb);
1119 hdr->codec = bytestream2_get_byteu(&ctx->gb);
1120 hdr->rotate_code = bytestream2_get_byteu(&ctx->gb);
1122 bytestream2_skip(&ctx->gb, 4); // skip pad
1124 for (i = 0; i < 4; i++)
1125 ctx->small_codebook[i] = bytestream2_get_le16u(&ctx->gb);
1126 hdr->bg_color = bytestream2_get_le16u(&ctx->gb);
1128 bytestream2_skip(&ctx->gb, 2); // skip pad
1130 hdr->rle_output_size = bytestream2_get_le32u(&ctx->gb);
1131 for (i = 0; i < 256; i++)
1132 ctx->codebook[i] = bytestream2_get_le16u(&ctx->gb);
1134 bytestream2_skip(&ctx->gb, 8); // skip pad
1136 av_dlog(ctx->avctx, "subcodec %d\n", hdr->codec);
1140 static void fill_frame(uint16_t *pbuf, int buf_size, uint16_t color)
1146 static int copy_output(SANMVideoContext *ctx, SANMFrameHeader *hdr)
1149 const uint8_t *src = (uint8_t*) ctx->frm0;
1150 int ret, dstpitch, height = ctx->height;
1151 int srcpitch = ctx->pitch * (hdr ? sizeof(ctx->frm0[0]) : 1);
1153 if ((ret = ff_get_buffer(ctx->avctx, ctx->frame, 0)) < 0)
1156 dst = ctx->frame->data[0];
1157 dstpitch = ctx->frame->linesize[0];
1160 memcpy(dst, src, srcpitch);
1168 static int decode_frame(AVCodecContext *avctx, void *data,
1169 int *got_frame_ptr, AVPacket *pkt)
1171 SANMVideoContext *ctx = avctx->priv_data;
1175 bytestream2_init(&ctx->gb, pkt->data, pkt->size);
1177 if (!ctx->version) {
1180 while (bytestream2_get_bytes_left(&ctx->gb) >= 8) {
1184 sig = bytestream2_get_be32u(&ctx->gb);
1185 size = bytestream2_get_be32u(&ctx->gb);
1186 pos = bytestream2_tell(&ctx->gb);
1188 if (bytestream2_get_bytes_left(&ctx->gb) < size) {
1189 av_log(avctx, AV_LOG_ERROR, "incorrect chunk size %d\n", size);
1193 case MKBETAG('N', 'P', 'A', 'L'):
1194 if (size != 256 * 3) {
1195 av_log(avctx, AV_LOG_ERROR, "incorrect palette block size %d\n",
1197 return AVERROR_INVALIDDATA;
1199 for (i = 0; i < 256; i++)
1200 ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
1202 case MKBETAG('F', 'O', 'B', 'J'):
1204 return AVERROR_INVALIDDATA;
1205 if (ret = process_frame_obj(ctx))
1208 case MKBETAG('X', 'P', 'A', 'L'):
1209 if (size == 6 || size == 4) {
1213 for (i = 0; i < 256; i++) {
1214 for (j = 0; j < 3; j++) {
1215 int t = (ctx->pal[i] >> (16 - j * 8)) & 0xFF;
1216 tmp[j] = av_clip_uint8((t * 129 + ctx->delta_pal[i * 3 + j]) >> 7);
1218 ctx->pal[i] = 0xFFU << 24 | AV_RB24(tmp);
1221 if (size < 768 * 2 + 4) {
1222 av_log(avctx, AV_LOG_ERROR, "incorrect palette change block size %d\n",
1224 return AVERROR_INVALIDDATA;
1226 bytestream2_skipu(&ctx->gb, 4);
1227 for (i = 0; i < 768; i++)
1228 ctx->delta_pal[i] = bytestream2_get_le16u(&ctx->gb);
1229 if (size >= 768 * 5 + 4) {
1230 for (i = 0; i < 256; i++)
1231 ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
1233 memset(ctx->pal, 0, sizeof(ctx->pal));
1237 case MKBETAG('S', 'T', 'O', 'R'):
1240 case MKBETAG('F', 'T', 'C', 'H'):
1241 memcpy(ctx->frm0, ctx->stored_frame, ctx->buf_size);
1244 bytestream2_skip(&ctx->gb, size);
1245 av_log(avctx, AV_LOG_DEBUG, "unknown/unsupported chunk %x\n", sig);
1249 bytestream2_seek(&ctx->gb, pos + size, SEEK_SET);
1251 bytestream2_skip(&ctx->gb, 1);
1254 memcpy(ctx->stored_frame, ctx->frm0, ctx->buf_size);
1255 if ((ret = copy_output(ctx, NULL)))
1257 memcpy(ctx->frame->data[1], ctx->pal, 1024);
1259 SANMFrameHeader header;
1261 if ((ret = read_frame_header(ctx, &header)))
1264 ctx->rotate_code = header.rotate_code;
1265 if ((ctx->frame->key_frame = !header.seq_num)) {
1266 ctx->frame->pict_type = AV_PICTURE_TYPE_I;
1267 fill_frame(ctx->frm1, ctx->npixels, header.bg_color);
1268 fill_frame(ctx->frm2, ctx->npixels, header.bg_color);
1270 ctx->frame->pict_type = AV_PICTURE_TYPE_P;
1273 if (header.codec < FF_ARRAY_ELEMS(v1_decoders)) {
1274 if ((ret = v1_decoders[header.codec](ctx))) {
1275 av_log(avctx, AV_LOG_ERROR,
1276 "subcodec %d: error decoding frame\n", header.codec);
1280 avpriv_request_sample(avctx, "subcodec %d",
1282 return AVERROR_PATCHWELCOME;
1285 if ((ret = copy_output(ctx, &header)))
1288 if (ctx->rotate_code)
1289 rotate_bufs(ctx, ctx->rotate_code);
1296 AVCodec ff_sanm_decoder = {
1298 .long_name = NULL_IF_CONFIG_SMALL("LucasArts SMUSH video"),
1299 .type = AVMEDIA_TYPE_VIDEO,
1300 .id = AV_CODEC_ID_SANM,
1301 .priv_data_size = sizeof(SANMVideoContext),
1302 .init = decode_init,
1303 .close = decode_end,
1304 .decode = decode_frame,
1305 .capabilities = CODEC_CAP_DR1,