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"
27 #include "libavutil/bswap.h"
28 #include "libavcodec/dsputil.h"
29 #include "sanm_data.h"
34 AVCodecContext *avctx;
37 int version, subversion;
39 int16_t delta_pal[768];
43 int aligned_width, aligned_height;
46 AVFrame frame, *output;
47 uint16_t *frm0, *frm1, *frm2;
48 uint8_t *stored_frame;
49 uint32_t frm0_size, frm1_size, frm2_size;
50 uint32_t stored_frame_size;
53 unsigned int rle_buf_size;
57 long npixels, buf_size;
59 uint16_t codebook[256];
60 uint16_t small_codebook[4];
62 int8_t p4x4glyphs[NGLYPHS][16];
63 int8_t p8x8glyphs[NGLYPHS][64];
67 int seq_num, codec, rotate_code, rle_output_size;
70 uint32_t width, height;
90 * Return enum GlyphEdge of box where point (x, y) lies.
92 * @param x x point coordinate
93 * @param y y point coordinate
94 * @param edge_size box width/height.
96 static enum GlyphEdge which_edge(int x, int y, int edge_size)
98 const int edge_max = edge_size - 1;
102 } else if (y == edge_max) {
106 } else if (x == edge_max) {
113 static enum GlyphDir which_direction(enum GlyphEdge edge0, enum GlyphEdge edge1)
115 if ((edge0 == LEFT_EDGE && edge1 == RIGHT_EDGE) ||
116 (edge1 == LEFT_EDGE && edge0 == RIGHT_EDGE) ||
117 (edge0 == BOTTOM_EDGE && edge1 != TOP_EDGE) ||
118 (edge1 == BOTTOM_EDGE && edge0 != TOP_EDGE)) {
120 } else if ((edge0 == TOP_EDGE && edge1 != BOTTOM_EDGE) ||
121 (edge1 == TOP_EDGE && edge0 != BOTTOM_EDGE)) {
123 } else if ((edge0 == LEFT_EDGE && edge1 != RIGHT_EDGE) ||
124 (edge1 == LEFT_EDGE && edge0 != RIGHT_EDGE)) {
126 } else if ((edge0 == TOP_EDGE && edge1 == BOTTOM_EDGE) ||
127 (edge1 == TOP_EDGE && edge0 == BOTTOM_EDGE) ||
128 (edge0 == RIGHT_EDGE && edge1 != LEFT_EDGE) ||
129 (edge1 == RIGHT_EDGE && edge0 != LEFT_EDGE)) {
137 * Interpolate two points.
139 static void interp_point(int8_t *points, int x0, int y0, int x1, int y1,
140 int pos, int npoints)
143 points[0] = (x0 * pos + x1 * (npoints - pos) + (npoints >> 1)) / npoints;
144 points[1] = (y0 * pos + y1 * (npoints - pos) + (npoints >> 1)) / npoints;
152 * Construct glyphs by iterating through vectors coordinates.
154 * @param pglyphs pointer to table where glyphs are stored
155 * @param xvec pointer to x component of vectors coordinates
156 * @param yvec pointer to y component of vectors coordinates
157 * @param side_length glyph width/height.
159 static void make_glyphs(int8_t *pglyphs, const int8_t *xvec, const int8_t *yvec,
160 const int side_length)
162 const int glyph_size = side_length * side_length;
163 int8_t *pglyph = pglyphs;
166 for (i = 0; i < GLYPH_COORD_VECT_SIZE; i++) {
169 enum GlyphEdge edge0 = which_edge(x0, y0, side_length);
171 for (j = 0; j < GLYPH_COORD_VECT_SIZE; j++, pglyph += glyph_size) {
174 enum GlyphEdge edge1 = which_edge(x1, y1, side_length);
175 enum GlyphDir dir = which_direction(edge0, edge1);
176 int npoints = FFMAX(FFABS(x1 - x0), FFABS(y1 - y0));
179 for (ipoint = 0; ipoint <= npoints; ipoint++) {
183 interp_point(point, x0, y0, x1, y1, ipoint, npoints);
187 for (irow = point[1]; irow >= 0; irow--)
188 pglyph[point[0] + irow * side_length] = 1;
192 for (irow = point[1]; irow < side_length; irow++)
193 pglyph[point[0] + irow * side_length] = 1;
197 for (icol = point[0]; icol >= 0; icol--)
198 pglyph[icol + point[1] * side_length] = 1;
202 for (icol = point[0]; icol < side_length; icol++)
203 pglyph[icol + point[1] * side_length] = 1;
211 static void init_sizes(SANMVideoContext *ctx, int width, int height)
214 ctx->height = height;
215 ctx->npixels = width * height;
217 ctx->aligned_width = FFALIGN(width, 8);
218 ctx->aligned_height = FFALIGN(height, 8);
220 ctx->buf_size = ctx->aligned_width * ctx->aligned_height * sizeof(ctx->frm0[0]);
224 static void destroy_buffers(SANMVideoContext *ctx)
226 av_freep(&ctx->frm0);
227 av_freep(&ctx->frm1);
228 av_freep(&ctx->frm2);
229 av_freep(&ctx->stored_frame);
230 av_freep(&ctx->rle_buf);
233 static av_cold int init_buffers(SANMVideoContext *ctx)
235 av_fast_padded_malloc(&ctx->frm0, &ctx->frm0_size, ctx->buf_size);
236 av_fast_padded_malloc(&ctx->frm1, &ctx->frm1_size, ctx->buf_size);
237 av_fast_padded_malloc(&ctx->frm2, &ctx->frm2_size, ctx->buf_size);
239 av_fast_padded_malloc(&ctx->stored_frame, &ctx->stored_frame_size, ctx->buf_size);
241 if (!ctx->frm0 || !ctx->frm1 || !ctx->frm2 || (!ctx->stored_frame && !ctx->version)) {
242 destroy_buffers(ctx);
243 return AVERROR(ENOMEM);
249 static void rotate_bufs(SANMVideoContext *ctx, int rotate_code)
251 av_dlog(ctx->avctx, "rotate %d\n", rotate_code);
252 if (rotate_code == 2)
253 FFSWAP(uint16_t*, ctx->frm1, ctx->frm2);
254 FFSWAP(uint16_t*, ctx->frm2, ctx->frm0);
257 static av_cold int decode_init(AVCodecContext *avctx)
259 SANMVideoContext *ctx = avctx->priv_data;
262 ctx->version = !avctx->extradata_size;
264 avctx->pix_fmt = ctx->version ? AV_PIX_FMT_RGB565 : AV_PIX_FMT_PAL8;
266 init_sizes(ctx, avctx->width, avctx->height);
267 if (init_buffers(ctx)) {
268 av_log(avctx, AV_LOG_ERROR, "error allocating buffers\n");
269 return AVERROR(ENOMEM);
271 ctx->output = &ctx->frame;
272 ctx->output->data[0] = 0;
274 make_glyphs(ctx->p4x4glyphs[0], glyph4_x, glyph4_y, 4);
275 make_glyphs(ctx->p8x8glyphs[0], glyph8_x, glyph8_y, 8);
280 if (avctx->extradata_size < 1026) {
281 av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");
282 return AVERROR_INVALIDDATA;
285 ctx->subversion = AV_RL16(avctx->extradata);
286 for (i = 0; i < 256; i++)
287 ctx->pal[i] = 0xFF << 24 | AV_RL32(avctx->extradata + 2 + i * 4);
293 static av_cold int decode_end(AVCodecContext *avctx)
295 SANMVideoContext *ctx = avctx->priv_data;
297 destroy_buffers(ctx);
299 if (ctx->frame.data[0]) {
300 avctx->release_buffer(avctx, &ctx->frame);
301 ctx->frame.data[0] = 0;
307 static int rle_decode(SANMVideoContext *ctx, uint8_t *dst, const int out_size)
309 int opcode, color, run_len, left = out_size;
312 opcode = bytestream2_get_byte(&ctx->gb);
313 run_len = (opcode >> 1) + 1;
314 if (run_len > left || bytestream2_get_bytes_left(&ctx->gb) <= 0)
315 return AVERROR_INVALIDDATA;
318 color = bytestream2_get_byte(&ctx->gb);
319 memset(dst, color, run_len);
321 if (bytestream2_get_bytes_left(&ctx->gb) < run_len)
322 return AVERROR_INVALIDDATA;
323 bytestream2_get_bufferu(&ctx->gb, dst, run_len);
333 static int old_codec1(SANMVideoContext *ctx, int top,
334 int left, int width, int height)
336 uint8_t *dst = ((uint8_t*)ctx->frm0) + left + top * ctx->pitch;
337 int i, j, len, flag, code, val, pos, end;
339 for (i = 0; i < height; i++) {
342 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
343 return AVERROR_INVALIDDATA;
345 len = bytestream2_get_le16u(&ctx->gb);
346 end = bytestream2_tell(&ctx->gb) + len;
348 while (bytestream2_tell(&ctx->gb) < end) {
349 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
350 return AVERROR_INVALIDDATA;
352 code = bytestream2_get_byteu(&ctx->gb);
354 code = (code >> 1) + 1;
355 if (pos + code > width)
356 return AVERROR_INVALIDDATA;
358 val = bytestream2_get_byteu(&ctx->gb);
360 memset(dst + pos, val, code);
363 if (bytestream2_get_bytes_left(&ctx->gb) < code)
364 return AVERROR_INVALIDDATA;
365 for (j = 0; j < code; j++) {
366 val = bytestream2_get_byteu(&ctx->gb);
375 ctx->rotate_code = 0;
380 static inline void codec37_mv(uint8_t *dst, const uint8_t *src,
381 int height, int stride, int x, int y)
385 pos = x + y * stride;
386 for (j = 0; j < 4; j++) {
387 for (i = 0; i < 4; i++) {
388 if ((pos + i) < 0 || (pos + i) >= height * stride)
399 static int old_codec37(SANMVideoContext *ctx, int top,
400 int left, int width, int height)
402 int stride = ctx->pitch;
405 int compr, mvoff, seq, flags;
406 uint32_t decoded_size;
409 compr = bytestream2_get_byte(&ctx->gb);
410 mvoff = bytestream2_get_byte(&ctx->gb);
411 seq = bytestream2_get_le16(&ctx->gb);
412 decoded_size = bytestream2_get_le32(&ctx->gb);
413 bytestream2_skip(&ctx->gb, 4);
414 flags = bytestream2_get_byte(&ctx->gb);
415 bytestream2_skip(&ctx->gb, 3);
417 ctx->rotate_code = 0;
419 if (((seq & 1) || !(flags & 1)) && (compr && compr != 2))
422 dst = ((uint8_t*)ctx->frm0) + left + top * stride;
423 prev = ((uint8_t*)ctx->frm2) + left + top * stride;
426 av_log(ctx->avctx, AV_LOG_ERROR, "invalid motion base value %d\n", mvoff);
427 return AVERROR_INVALIDDATA;
429 av_dlog(ctx->avctx, "compression %d\n", compr);
432 for (i = 0; i < height; i++) {
433 bytestream2_get_buffer(&ctx->gb, dst, width);
436 memset(ctx->frm1, 0, ctx->height * stride);
437 memset(ctx->frm2, 0, ctx->height * stride);
440 if (rle_decode(ctx, dst, decoded_size))
441 return AVERROR_INVALIDDATA;
442 memset(ctx->frm1, 0, ctx->frm1_size);
443 memset(ctx->frm2, 0, ctx->frm2_size);
448 for (j = 0; j < height; j += 4) {
449 for (i = 0; i < width; i += 4) {
453 copy_block4(dst + i, prev + i, stride, stride, 4);
456 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
457 return AVERROR_INVALIDDATA;
458 code = bytestream2_get_byteu(&ctx->gb);
461 if (bytestream2_get_bytes_left(&ctx->gb) < 16)
462 return AVERROR_INVALIDDATA;
463 for (k = 0; k < 4; k++)
464 bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
467 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
468 return AVERROR_INVALIDDATA;
469 for (k = 0; k < 4; k++)
470 memset(dst + i + k * stride, bytestream2_get_byteu(&ctx->gb), 4);
473 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
474 return AVERROR_INVALIDDATA;
475 t = bytestream2_get_byteu(&ctx->gb);
476 for (k = 0; k < 4; k++)
477 memset(dst + i + k * stride, t, 4);
480 if (compr == 4 && !code) {
481 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
482 return AVERROR_INVALIDDATA;
483 skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
488 mx = c37_mv[(mvoff * 255 + code) * 2 ];
489 my = c37_mv[(mvoff * 255 + code) * 2 + 1];
490 codec37_mv(dst + i, prev + i + mx + my * stride,
491 ctx->height, stride, i + mx, j + my);
499 for (j = 0; j < height; j += 4) {
500 for (i = 0; i < width; i += 4) {
504 copy_block4(dst + i, prev + i, stride, stride, 4);
507 code = bytestream2_get_byte(&ctx->gb);
509 if (bytestream2_get_bytes_left(&ctx->gb) < 16)
510 return AVERROR_INVALIDDATA;
511 for (k = 0; k < 4; k++)
512 bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
513 } else if (compr == 4 && !code) {
514 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
515 return AVERROR_INVALIDDATA;
516 skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
521 mx = c37_mv[(mvoff * 255 + code) * 2];
522 my = c37_mv[(mvoff * 255 + code) * 2 + 1];
523 codec37_mv(dst + i, prev + i + mx + my * stride,
524 ctx->height, stride, i + mx, j + my);
533 av_log(ctx->avctx, AV_LOG_ERROR,
534 "subcodec 37 compression %d not implemented\n", compr);
535 return AVERROR_PATCHWELCOME;
541 static int process_block(SANMVideoContext *ctx, uint8_t *dst, uint8_t *prev1,
542 uint8_t *prev2, int stride, int tbl, int size)
548 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
549 return AVERROR_INVALIDDATA;
551 code = bytestream2_get_byteu(&ctx->gb);
556 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
557 return AVERROR_INVALIDDATA;
558 dst[0] = bytestream2_get_byteu(&ctx->gb);
559 dst[1] = bytestream2_get_byteu(&ctx->gb);
560 dst[0+stride] = bytestream2_get_byteu(&ctx->gb);
561 dst[1+stride] = bytestream2_get_byteu(&ctx->gb);
564 if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
565 return AVERROR_INVALIDDATA;
566 if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
568 return AVERROR_INVALIDDATA;
569 dst += size * stride;
570 prev1 += size * stride;
571 prev2 += size * stride;
572 if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
573 return AVERROR_INVALIDDATA;
574 if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
576 return AVERROR_INVALIDDATA;
580 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
581 return AVERROR_INVALIDDATA;
583 t = bytestream2_get_byteu(&ctx->gb);
584 for (k = 0; k < size; k++)
585 memset(dst + k * stride, t, size);
588 if (bytestream2_get_bytes_left(&ctx->gb) < 3)
589 return AVERROR_INVALIDDATA;
591 code = bytestream2_get_byteu(&ctx->gb);
592 pglyph = (size == 8) ? ctx->p8x8glyphs[code] : ctx->p4x4glyphs[code];
593 bytestream2_get_bufferu(&ctx->gb, colors, 2);
595 for (k = 0; k < size; k++)
596 for (t = 0; t < size; t++)
597 dst[t + k * stride] = colors[!*pglyph++];
600 for (k = 0; k < size; k++)
601 memcpy(dst + k * stride, prev1 + k * stride, size);
604 k = bytestream2_tell(&ctx->gb);
605 bytestream2_seek(&ctx->gb, tbl + (code & 7), SEEK_SET);
606 t = bytestream2_get_byte(&ctx->gb);
607 bytestream2_seek(&ctx->gb, k, SEEK_SET);
608 for (k = 0; k < size; k++)
609 memset(dst + k * stride, t, size);
612 int mx = motion_vectors[code][0];
613 int my = motion_vectors[code][1];
614 for (k = 0; k < size; k++)
615 memcpy(dst + k * stride, prev2 + mx + (my + k) * stride, size);
621 static int old_codec47(SANMVideoContext *ctx, int top,
622 int left, int width, int height)
624 int i, j, seq, compr, new_rot, tbl_pos, skip;
625 int stride = ctx->pitch;
626 uint8_t *dst = ((uint8_t*)ctx->frm0) + left + top * stride;
627 uint8_t *prev1 = (uint8_t*)ctx->frm1;
628 uint8_t *prev2 = (uint8_t*)ctx->frm2;
629 uint32_t decoded_size;
631 tbl_pos = bytestream2_tell(&ctx->gb);
632 seq = bytestream2_get_le16(&ctx->gb);
633 compr = bytestream2_get_byte(&ctx->gb);
634 new_rot = bytestream2_get_byte(&ctx->gb);
635 skip = bytestream2_get_byte(&ctx->gb);
636 bytestream2_skip(&ctx->gb, 9);
637 decoded_size = bytestream2_get_le32(&ctx->gb);
638 bytestream2_skip(&ctx->gb, 8);
641 bytestream2_skip(&ctx->gb, 0x8080);
644 memset(prev1, 0, ctx->height * stride);
645 memset(prev2, 0, ctx->height * stride);
647 av_dlog(ctx->avctx, "compression %d\n", compr);
650 if (bytestream2_get_bytes_left(&ctx->gb) < width * height)
651 return AVERROR_INVALIDDATA;
652 for (j = 0; j < height; j++) {
653 for (i = 0; i < width; i++)
654 bytestream2_get_bufferu(&ctx->gb, dst, width);
659 if (bytestream2_get_bytes_left(&ctx->gb) < ((width + 1) >> 1) * ((height + 1) >> 1))
660 return AVERROR_INVALIDDATA;
661 for (j = 0; j < height; j += 2) {
662 for (i = 0; i < width; i += 2) {
663 dst[i] = dst[i + 1] =
664 dst[stride + i] = dst[stride + i + 1] = bytestream2_get_byteu(&ctx->gb);
670 if (seq == ctx->prev_seq + 1) {
671 for (j = 0; j < height; j += 8) {
672 for (i = 0; i < width; i += 8) {
673 if (process_block(ctx, dst + i, prev1 + i, prev2 + i, stride,
675 return AVERROR_INVALIDDATA;
684 memcpy(ctx->frm0, ctx->frm2, ctx->pitch * ctx->height);
687 memcpy(ctx->frm0, ctx->frm1, ctx->pitch * ctx->height);
690 if (rle_decode(ctx, dst, decoded_size))
691 return AVERROR_INVALIDDATA;
694 av_log(ctx->avctx, AV_LOG_ERROR,
695 "subcodec 47 compression %d not implemented\n", compr);
696 return AVERROR_PATCHWELCOME;
698 if (seq == ctx->prev_seq + 1)
699 ctx->rotate_code = new_rot;
701 ctx->rotate_code = 0;
707 static int process_frame_obj(SANMVideoContext *ctx)
709 uint16_t codec, top, left, w, h;
711 codec = bytestream2_get_le16u(&ctx->gb);
712 left = bytestream2_get_le16u(&ctx->gb);
713 top = bytestream2_get_le16u(&ctx->gb);
714 w = bytestream2_get_le16u(&ctx->gb);
715 h = bytestream2_get_le16u(&ctx->gb);
717 if (ctx->width < left + w || ctx->height < top + h) {
718 ctx->avctx->width = FFMAX(left + w, ctx->width);
719 ctx->avctx->height = FFMAX(top + h, ctx->height);
720 init_sizes(ctx, left + w, top + h);
721 if (init_buffers(ctx)) {
722 av_log(ctx->avctx, AV_LOG_ERROR, "error resizing buffers\n");
723 return AVERROR(ENOMEM);
726 bytestream2_skip(&ctx->gb, 4);
728 av_dlog(ctx->avctx, "subcodec %d\n", codec);
732 return old_codec1(ctx, top, left, w, h);
735 return old_codec37(ctx, top, left, w, h);
738 return old_codec47(ctx, top, left, w, h);
741 av_log_ask_for_sample(ctx->avctx, "unknown subcodec %d\n", codec);
742 return AVERROR_PATCHWELCOME;
746 static int decode_0(SANMVideoContext *ctx)
748 uint16_t *frm = ctx->frm0;
751 if (bytestream2_get_bytes_left(&ctx->gb) < ctx->width * ctx->height * 2) {
752 av_log(ctx->avctx, AV_LOG_ERROR, "insufficient data for raw frame\n");
753 return AVERROR_INVALIDDATA;
755 for (y = 0; y < ctx->height; y++) {
756 for (x = 0; x < ctx->width; x++)
757 frm[x] = bytestream2_get_le16u(&ctx->gb);
763 static int decode_nop(SANMVideoContext *ctx)
765 av_log_ask_for_sample(ctx->avctx, "unknown/unsupported compression type\n");
766 return AVERROR_PATCHWELCOME;
769 static void copy_block(uint16_t *pdest, uint16_t *psrc, int block_size, int pitch)
771 uint8_t *dst = (uint8_t *)pdest;
772 uint8_t *src = (uint8_t *)psrc;
773 int stride = pitch * 2;
775 switch (block_size) {
777 copy_block4(dst, src, stride, stride, 2);
780 copy_block8(dst, src, stride, stride, 4);
783 copy_block16(dst, src, stride, stride, 8);
788 static void fill_block(uint16_t *pdest, uint16_t color, int block_size, int pitch)
793 for (y = 0; y < block_size; y++, pdest += pitch)
794 for (x = 0; x < block_size; x++)
798 static int draw_glyph(SANMVideoContext *ctx, uint16_t *dst, int index, uint16_t fg_color,
799 uint16_t bg_color, int block_size, int pitch)
802 uint16_t colors[2] = { fg_color, bg_color };
805 if (index > NGLYPHS) {
806 av_log(ctx->avctx, AV_LOG_ERROR, "ignoring nonexistent glyph #%u\n", index);
807 return AVERROR_INVALIDDATA;
810 pglyph = block_size == 8 ? ctx->p8x8glyphs[index] : ctx->p4x4glyphs[index];
813 for (y = 0; y < block_size; y++, dst += pitch)
814 for (x = 0; x < block_size; x++)
815 *dst++ = colors[*pglyph++];
819 static int opcode_0xf7(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
821 uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
823 if (block_size == 2) {
826 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
827 return AVERROR_INVALIDDATA;
829 indices = bytestream2_get_le32u(&ctx->gb);
830 dst[0] = ctx->codebook[indices & 0xFF]; indices >>= 8;
831 dst[1] = ctx->codebook[indices & 0xFF]; indices >>= 8;
832 dst[pitch] = ctx->codebook[indices & 0xFF]; indices >>= 8;
833 dst[pitch + 1] = ctx->codebook[indices & 0xFF];
835 uint16_t fgcolor, bgcolor;
838 if (bytestream2_get_bytes_left(&ctx->gb) < 3)
839 return AVERROR_INVALIDDATA;
841 glyph = bytestream2_get_byteu(&ctx->gb);
842 bgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
843 fgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
845 draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
850 static int opcode_0xf8(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
852 uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
854 if (block_size == 2) {
855 if (bytestream2_get_bytes_left(&ctx->gb) < 8)
856 return AVERROR_INVALIDDATA;
858 dst[0] = bytestream2_get_le16u(&ctx->gb);
859 dst[1] = bytestream2_get_le16u(&ctx->gb);
860 dst[pitch] = bytestream2_get_le16u(&ctx->gb);
861 dst[pitch + 1] = bytestream2_get_le16u(&ctx->gb);
863 uint16_t fgcolor, bgcolor;
866 if (bytestream2_get_bytes_left(&ctx->gb) < 5)
867 return AVERROR_INVALIDDATA;
869 glyph = bytestream2_get_byteu(&ctx->gb);
870 bgcolor = bytestream2_get_le16u(&ctx->gb);
871 fgcolor = bytestream2_get_le16u(&ctx->gb);
873 draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
878 static int good_mvec(SANMVideoContext *ctx, int cx, int cy, int mx, int my,
881 int start_pos = cx + mx + (cy + my) * ctx->pitch;
882 int end_pos = start_pos + (block_size - 1) * (ctx->pitch + 1);
884 int good = start_pos >= 0 && end_pos < (ctx->buf_size >> 1);
887 av_log(ctx->avctx, AV_LOG_ERROR, "ignoring invalid motion vector (%i, %i)->(%u, %u), block size = %u\n",
888 cx + mx, cy + my, cx, cy, block_size);
894 static int codec2subblock(SANMVideoContext *ctx, int cx, int cy, int blk_size)
896 int16_t mx, my, index;
899 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
900 return AVERROR_INVALIDDATA;
902 opcode = bytestream2_get_byteu(&ctx->gb);
904 av_dlog(ctx->avctx, "opcode 0x%0X cx %d cy %d blk %d\n", opcode, cx, cy, blk_size);
907 mx = motion_vectors[opcode][0];
908 my = motion_vectors[opcode][1];
910 if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
911 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
912 ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
913 blk_size, ctx->pitch);
917 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
918 return AVERROR_INVALIDDATA;
919 index = bytestream2_get_le16u(&ctx->gb);
921 mx = index % ctx->width;
922 my = index / ctx->width;
924 if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
925 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
926 ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
927 blk_size, ctx->pitch);
931 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
932 ctx->frm1 + cx + ctx->pitch * cy,
933 blk_size, ctx->pitch);
936 opcode_0xf7(ctx, cx, cy, blk_size, ctx->pitch);
940 opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
946 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
947 ctx->small_codebook[opcode - 0xf9], blk_size, ctx->pitch);
950 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
951 return AVERROR_INVALIDDATA;
952 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
953 ctx->codebook[bytestream2_get_byteu(&ctx->gb)], blk_size, ctx->pitch);
956 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
957 return AVERROR_INVALIDDATA;
958 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
959 bytestream2_get_le16u(&ctx->gb), blk_size, ctx->pitch);
963 opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
966 if (codec2subblock(ctx, cx , cy , blk_size))
967 return AVERROR_INVALIDDATA;
968 if (codec2subblock(ctx, cx + blk_size, cy , blk_size))
969 return AVERROR_INVALIDDATA;
970 if (codec2subblock(ctx, cx , cy + blk_size, blk_size))
971 return AVERROR_INVALIDDATA;
972 if (codec2subblock(ctx, cx + blk_size, cy + blk_size, blk_size))
973 return AVERROR_INVALIDDATA;
980 static int decode_2(SANMVideoContext *ctx)
984 for (cy = 0; cy < ctx->aligned_height; cy += 8) {
985 for (cx = 0; cx < ctx->aligned_width; cx += 8) {
986 if (ret = codec2subblock(ctx, cx, cy, 8))
994 static int decode_3(SANMVideoContext *ctx)
996 memcpy(ctx->frm0, ctx->frm2, ctx->frm2_size);
1000 static int decode_4(SANMVideoContext *ctx)
1002 memcpy(ctx->frm0, ctx->frm1, ctx->frm1_size);
1006 static int decode_5(SANMVideoContext *ctx)
1012 uint8_t *dst = (uint8_t*)ctx->frm0;
1014 if (rle_decode(ctx, dst, ctx->buf_size))
1015 return AVERROR_INVALIDDATA;
1018 npixels = ctx->npixels;
1021 *frm++ = av_bswap16(*frm);
1027 static int decode_6(SANMVideoContext *ctx)
1029 int npixels = ctx->npixels;
1030 uint16_t *frm = ctx->frm0;
1032 if (bytestream2_get_bytes_left(&ctx->gb) < npixels) {
1033 av_log(ctx->avctx, AV_LOG_ERROR, "insufficient data for frame\n");
1034 return AVERROR_INVALIDDATA;
1037 *frm++ = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
1042 static int decode_8(SANMVideoContext *ctx)
1044 uint16_t *pdest = ctx->frm0;
1046 long npixels = ctx->npixels;
1048 av_fast_malloc(&ctx->rle_buf, &ctx->rle_buf_size, npixels);
1049 if (!ctx->rle_buf) {
1050 av_log(ctx->avctx, AV_LOG_ERROR, "RLE buffer allocation failed\n");
1051 return AVERROR(ENOMEM);
1053 rsrc = ctx->rle_buf;
1055 if (rle_decode(ctx, rsrc, npixels))
1056 return AVERROR_INVALIDDATA;
1059 *pdest++ = ctx->codebook[*rsrc++];
1064 typedef int (*frm_decoder)(SANMVideoContext *ctx);
1066 static const frm_decoder v1_decoders[] = {
1067 decode_0, decode_nop, decode_2, decode_3, decode_4, decode_5,
1068 decode_6, decode_nop, decode_8
1071 static int read_frame_header(SANMVideoContext *ctx, SANMFrameHeader *hdr)
1075 if ((ret = bytestream2_get_bytes_left(&ctx->gb)) < 560) {
1076 av_log(ctx->avctx, AV_LOG_ERROR, "too short input frame (%d bytes)\n",
1078 return AVERROR_INVALIDDATA;
1080 bytestream2_skip(&ctx->gb, 8); // skip pad
1082 hdr->width = bytestream2_get_le32u(&ctx->gb);
1083 hdr->height = bytestream2_get_le32u(&ctx->gb);
1085 if (hdr->width != ctx->width || hdr->height != ctx->height) {
1086 av_log(ctx->avctx, AV_LOG_ERROR, "variable size frames are not implemented\n");
1087 return AVERROR_PATCHWELCOME;
1090 hdr->seq_num = bytestream2_get_le16u(&ctx->gb);
1091 hdr->codec = bytestream2_get_byteu(&ctx->gb);
1092 hdr->rotate_code = bytestream2_get_byteu(&ctx->gb);
1094 bytestream2_skip(&ctx->gb, 4); // skip pad
1096 for (i = 0; i < 4; i++)
1097 ctx->small_codebook[i] = bytestream2_get_le16u(&ctx->gb);
1098 hdr->bg_color = bytestream2_get_le16u(&ctx->gb);
1100 bytestream2_skip(&ctx->gb, 2); // skip pad
1102 hdr->rle_output_size = bytestream2_get_le32u(&ctx->gb);
1103 for (i = 0; i < 256; i++)
1104 ctx->codebook[i] = bytestream2_get_le16u(&ctx->gb);
1106 bytestream2_skip(&ctx->gb, 8); // skip pad
1108 av_dlog(ctx->avctx, "subcodec %d\n", hdr->codec);
1112 static void fill_frame(uint16_t *pbuf, int buf_size, uint16_t color)
1118 static int copy_output(SANMVideoContext *ctx, SANMFrameHeader *hdr)
1121 const uint8_t *src = (uint8_t*) ctx->frm0;
1122 int ret, dstpitch, height = ctx->height;
1123 int srcpitch = ctx->pitch * (hdr ? sizeof(ctx->frm0[0]) : 1);
1125 if ((ret = ctx->avctx->get_buffer(ctx->avctx, ctx->output)) < 0) {
1126 av_log(ctx->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1130 dst = ctx->output->data[0];
1131 dstpitch = ctx->output->linesize[0];
1134 memcpy(dst, src, srcpitch);
1142 static int decode_frame(AVCodecContext *avctx, void *data,
1143 int *got_frame_ptr, AVPacket *pkt)
1145 SANMVideoContext *ctx = avctx->priv_data;
1148 bytestream2_init(&ctx->gb, pkt->data, pkt->size);
1149 if (ctx->output->data[0])
1150 avctx->release_buffer(avctx, ctx->output);
1152 if (!ctx->version) {
1155 while (bytestream2_get_bytes_left(&ctx->gb) >= 8) {
1159 sig = bytestream2_get_be32u(&ctx->gb);
1160 size = bytestream2_get_be32u(&ctx->gb);
1161 pos = bytestream2_tell(&ctx->gb);
1163 if (bytestream2_get_bytes_left(&ctx->gb) < size) {
1164 av_log(avctx, AV_LOG_ERROR, "incorrect chunk size %d\n", size);
1168 case MKBETAG('N', 'P', 'A', 'L'):
1169 if (size != 256 * 3) {
1170 av_log(avctx, AV_LOG_ERROR, "incorrect palette block size %d\n",
1172 return AVERROR_INVALIDDATA;
1174 for (i = 0; i < 256; i++)
1175 ctx->pal[i] = 0xFF << 24 | bytestream2_get_be24u(&ctx->gb);
1177 case MKBETAG('F', 'O', 'B', 'J'):
1179 return AVERROR_INVALIDDATA;
1180 if (ret = process_frame_obj(ctx))
1183 case MKBETAG('X', 'P', 'A', 'L'):
1184 if (size == 6 || size == 4) {
1188 for (i = 0; i < 256; i++) {
1189 for (j = 0; j < 3; j++) {
1190 int t = (ctx->pal[i] >> (16 - j * 8)) & 0xFF;
1191 tmp[j] = av_clip_uint8((t * 129 + ctx->delta_pal[i * 3 + j]) >> 7);
1193 ctx->pal[i] = 0xFF << 24 | AV_RB24(tmp);
1196 if (size < 768 * 2 + 4) {
1197 av_log(avctx, AV_LOG_ERROR, "incorrect palette change block size %d\n",
1199 return AVERROR_INVALIDDATA;
1201 bytestream2_skipu(&ctx->gb, 4);
1202 for (i = 0; i < 768; i++)
1203 ctx->delta_pal[i] = bytestream2_get_le16u(&ctx->gb);
1204 if (size >= 768 * 5 + 4) {
1205 for (i = 0; i < 256; i++)
1206 ctx->pal[i] = 0xFF << 24 | bytestream2_get_be24u(&ctx->gb);
1208 memset(ctx->pal, 0, sizeof(ctx->pal));
1212 case MKBETAG('S', 'T', 'O', 'R'):
1215 case MKBETAG('F', 'T', 'C', 'H'):
1216 memcpy(ctx->frm0, ctx->stored_frame, ctx->buf_size);
1219 bytestream2_skip(&ctx->gb, size);
1220 av_log(avctx, AV_LOG_DEBUG, "unknown/unsupported chunk %x\n", sig);
1224 bytestream2_seek(&ctx->gb, pos + size, SEEK_SET);
1226 bytestream2_skip(&ctx->gb, 1);
1229 memcpy(ctx->stored_frame, ctx->frm0, ctx->buf_size);
1230 if ((ret = copy_output(ctx, NULL)))
1232 memcpy(ctx->output->data[1], ctx->pal, 1024);
1234 SANMFrameHeader header;
1236 if ((ret = read_frame_header(ctx, &header)))
1239 ctx->rotate_code = header.rotate_code;
1240 if ((ctx->output->key_frame = !header.seq_num)) {
1241 ctx->output->pict_type = AV_PICTURE_TYPE_I;
1242 fill_frame(ctx->frm1, ctx->npixels, header.bg_color);
1243 fill_frame(ctx->frm2, ctx->npixels, header.bg_color);
1245 ctx->output->pict_type = AV_PICTURE_TYPE_P;
1248 if (header.codec < FF_ARRAY_ELEMS(v1_decoders)) {
1249 if ((ret = v1_decoders[header.codec](ctx))) {
1250 av_log(avctx, AV_LOG_ERROR,
1251 "subcodec %d: error decoding frame\n", header.codec);
1255 av_log_ask_for_sample(avctx, "subcodec %d is not implemented\n",
1257 return AVERROR_PATCHWELCOME;
1260 if ((ret = copy_output(ctx, &header)))
1263 if (ctx->rotate_code)
1264 rotate_bufs(ctx, ctx->rotate_code);
1267 *(AVFrame*)data = *ctx->output;
1272 AVCodec ff_sanm_decoder = {
1274 .type = AVMEDIA_TYPE_VIDEO,
1275 .id = AV_CODEC_ID_SANM,
1276 .priv_data_size = sizeof(SANMVideoContext),
1277 .init = decode_init,
1278 .close = decode_end,
1279 .decode = decode_frame,
1280 .capabilities = CODEC_CAP_DR1,
1281 .long_name = NULL_IF_CONFIG_SMALL("LucasArts SMUSH video"),