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;
49 AVFrame frame, *output;
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);
277 ctx->output = &ctx->frame;
278 ctx->output->data[0] = 0;
280 make_glyphs(ctx->p4x4glyphs[0], glyph4_x, glyph4_y, 4);
281 make_glyphs(ctx->p8x8glyphs[0], glyph8_x, glyph8_y, 8);
286 if (avctx->extradata_size < 1026) {
287 av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");
288 return AVERROR_INVALIDDATA;
291 ctx->subversion = AV_RL16(avctx->extradata);
292 for (i = 0; i < 256; i++)
293 ctx->pal[i] = 0xFFU << 24 | AV_RL32(avctx->extradata + 2 + i * 4);
299 static av_cold int decode_end(AVCodecContext *avctx)
301 SANMVideoContext *ctx = avctx->priv_data;
303 destroy_buffers(ctx);
305 if (ctx->frame.data[0]) {
306 avctx->release_buffer(avctx, &ctx->frame);
307 ctx->frame.data[0] = 0;
313 static int rle_decode(SANMVideoContext *ctx, uint8_t *dst, const int out_size)
315 int opcode, color, run_len, left = out_size;
318 opcode = bytestream2_get_byte(&ctx->gb);
319 run_len = (opcode >> 1) + 1;
320 if (run_len > left || bytestream2_get_bytes_left(&ctx->gb) <= 0)
321 return AVERROR_INVALIDDATA;
324 color = bytestream2_get_byte(&ctx->gb);
325 memset(dst, color, run_len);
327 if (bytestream2_get_bytes_left(&ctx->gb) < run_len)
328 return AVERROR_INVALIDDATA;
329 bytestream2_get_bufferu(&ctx->gb, dst, run_len);
339 static int old_codec1(SANMVideoContext *ctx, int top,
340 int left, int width, int height)
342 uint8_t *dst = ((uint8_t*)ctx->frm0) + left + top * ctx->pitch;
343 int i, j, len, flag, code, val, pos, end;
345 for (i = 0; i < height; i++) {
348 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
349 return AVERROR_INVALIDDATA;
351 len = bytestream2_get_le16u(&ctx->gb);
352 end = bytestream2_tell(&ctx->gb) + len;
354 while (bytestream2_tell(&ctx->gb) < end) {
355 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
356 return AVERROR_INVALIDDATA;
358 code = bytestream2_get_byteu(&ctx->gb);
360 code = (code >> 1) + 1;
361 if (pos + code > width)
362 return AVERROR_INVALIDDATA;
364 val = bytestream2_get_byteu(&ctx->gb);
366 memset(dst + pos, val, code);
369 if (bytestream2_get_bytes_left(&ctx->gb) < code)
370 return AVERROR_INVALIDDATA;
371 for (j = 0; j < code; j++) {
372 val = bytestream2_get_byteu(&ctx->gb);
381 ctx->rotate_code = 0;
386 static inline void codec37_mv(uint8_t *dst, const uint8_t *src,
387 int height, int stride, int x, int y)
391 pos = x + y * stride;
392 for (j = 0; j < 4; j++) {
393 for (i = 0; i < 4; i++) {
394 if ((pos + i) < 0 || (pos + i) >= height * stride)
405 static int old_codec37(SANMVideoContext *ctx, int top,
406 int left, int width, int height)
408 int stride = ctx->pitch;
411 int compr, mvoff, seq, flags;
412 uint32_t decoded_size;
415 compr = bytestream2_get_byte(&ctx->gb);
416 mvoff = bytestream2_get_byte(&ctx->gb);
417 seq = bytestream2_get_le16(&ctx->gb);
418 decoded_size = bytestream2_get_le32(&ctx->gb);
419 bytestream2_skip(&ctx->gb, 4);
420 flags = bytestream2_get_byte(&ctx->gb);
421 bytestream2_skip(&ctx->gb, 3);
423 if (decoded_size > ctx->height * stride - left - top * stride) {
424 decoded_size = ctx->height * stride - left - top * stride;
425 av_log(ctx->avctx, AV_LOG_WARNING, "decoded size is too large\n");
428 ctx->rotate_code = 0;
430 if (((seq & 1) || !(flags & 1)) && (compr && compr != 2))
433 dst = ((uint8_t*)ctx->frm0) + left + top * stride;
434 prev = ((uint8_t*)ctx->frm2) + left + top * stride;
437 av_log(ctx->avctx, AV_LOG_ERROR, "invalid motion base value %d\n", mvoff);
438 return AVERROR_INVALIDDATA;
440 av_dlog(ctx->avctx, "compression %d\n", compr);
443 for (i = 0; i < height; i++) {
444 bytestream2_get_buffer(&ctx->gb, dst, width);
447 memset(ctx->frm1, 0, ctx->height * stride);
448 memset(ctx->frm2, 0, ctx->height * stride);
451 if (rle_decode(ctx, dst, decoded_size))
452 return AVERROR_INVALIDDATA;
453 memset(ctx->frm1, 0, ctx->frm1_size);
454 memset(ctx->frm2, 0, ctx->frm2_size);
459 for (j = 0; j < height; j += 4) {
460 for (i = 0; i < width; i += 4) {
464 copy_block4(dst + i, prev + i, stride, stride, 4);
467 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
468 return AVERROR_INVALIDDATA;
469 code = bytestream2_get_byteu(&ctx->gb);
472 if (bytestream2_get_bytes_left(&ctx->gb) < 16)
473 return AVERROR_INVALIDDATA;
474 for (k = 0; k < 4; k++)
475 bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
478 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
479 return AVERROR_INVALIDDATA;
480 for (k = 0; k < 4; k++)
481 memset(dst + i + k * stride, bytestream2_get_byteu(&ctx->gb), 4);
484 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
485 return AVERROR_INVALIDDATA;
486 t = bytestream2_get_byteu(&ctx->gb);
487 for (k = 0; k < 4; k++)
488 memset(dst + i + k * stride, t, 4);
491 if (compr == 4 && !code) {
492 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
493 return AVERROR_INVALIDDATA;
494 skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
499 mx = c37_mv[(mvoff * 255 + code) * 2 ];
500 my = c37_mv[(mvoff * 255 + code) * 2 + 1];
501 codec37_mv(dst + i, prev + i + mx + my * stride,
502 ctx->height, stride, i + mx, j + my);
510 for (j = 0; j < height; j += 4) {
511 for (i = 0; i < width; i += 4) {
515 copy_block4(dst + i, prev + i, stride, stride, 4);
518 code = bytestream2_get_byte(&ctx->gb);
520 if (bytestream2_get_bytes_left(&ctx->gb) < 16)
521 return AVERROR_INVALIDDATA;
522 for (k = 0; k < 4; k++)
523 bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
524 } else if (compr == 4 && !code) {
525 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
526 return AVERROR_INVALIDDATA;
527 skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
532 mx = c37_mv[(mvoff * 255 + code) * 2];
533 my = c37_mv[(mvoff * 255 + code) * 2 + 1];
534 codec37_mv(dst + i, prev + i + mx + my * stride,
535 ctx->height, stride, i + mx, j + my);
544 av_log(ctx->avctx, AV_LOG_ERROR,
545 "subcodec 37 compression %d not implemented\n", compr);
546 return AVERROR_PATCHWELCOME;
552 static int process_block(SANMVideoContext *ctx, uint8_t *dst, uint8_t *prev1,
553 uint8_t *prev2, int stride, int tbl, int size)
559 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
560 return AVERROR_INVALIDDATA;
562 code = bytestream2_get_byteu(&ctx->gb);
567 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
568 return AVERROR_INVALIDDATA;
569 dst[0] = bytestream2_get_byteu(&ctx->gb);
570 dst[1] = bytestream2_get_byteu(&ctx->gb);
571 dst[0+stride] = bytestream2_get_byteu(&ctx->gb);
572 dst[1+stride] = bytestream2_get_byteu(&ctx->gb);
575 if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
576 return AVERROR_INVALIDDATA;
577 if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
579 return AVERROR_INVALIDDATA;
580 dst += size * stride;
581 prev1 += size * stride;
582 prev2 += size * stride;
583 if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
584 return AVERROR_INVALIDDATA;
585 if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
587 return AVERROR_INVALIDDATA;
591 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
592 return AVERROR_INVALIDDATA;
594 t = bytestream2_get_byteu(&ctx->gb);
595 for (k = 0; k < size; k++)
596 memset(dst + k * stride, t, size);
599 if (bytestream2_get_bytes_left(&ctx->gb) < 3)
600 return AVERROR_INVALIDDATA;
602 code = bytestream2_get_byteu(&ctx->gb);
603 pglyph = (size == 8) ? ctx->p8x8glyphs[code] : ctx->p4x4glyphs[code];
604 bytestream2_get_bufferu(&ctx->gb, colors, 2);
606 for (k = 0; k < size; k++)
607 for (t = 0; t < size; t++)
608 dst[t + k * stride] = colors[!*pglyph++];
611 for (k = 0; k < size; k++)
612 memcpy(dst + k * stride, prev1 + k * stride, size);
615 k = bytestream2_tell(&ctx->gb);
616 bytestream2_seek(&ctx->gb, tbl + (code & 7), SEEK_SET);
617 t = bytestream2_get_byte(&ctx->gb);
618 bytestream2_seek(&ctx->gb, k, SEEK_SET);
619 for (k = 0; k < size; k++)
620 memset(dst + k * stride, t, size);
623 int mx = motion_vectors[code][0];
624 int my = motion_vectors[code][1];
625 int index = prev2 - (const uint8_t*)ctx->frm2;
627 av_assert2(index >= 0 && index < (ctx->buf_size>>1));
629 if (index < - mx - my*stride ||
630 (ctx->buf_size>>1) - index < mx + size + (my + size - 1)*stride) {
631 av_log(ctx->avctx, AV_LOG_ERROR, "MV is invalid \n");
632 return AVERROR_INVALIDDATA;
635 for (k = 0; k < size; k++)
636 memcpy(dst + k * stride, prev2 + mx + (my + k) * stride, size);
642 static int old_codec47(SANMVideoContext *ctx, int top,
643 int left, int width, int height)
645 int i, j, seq, compr, new_rot, tbl_pos, skip;
646 int stride = ctx->pitch;
647 uint8_t *dst = ((uint8_t*)ctx->frm0) + left + top * stride;
648 uint8_t *prev1 = (uint8_t*)ctx->frm1;
649 uint8_t *prev2 = (uint8_t*)ctx->frm2;
650 uint32_t decoded_size;
652 tbl_pos = bytestream2_tell(&ctx->gb);
653 seq = bytestream2_get_le16(&ctx->gb);
654 compr = bytestream2_get_byte(&ctx->gb);
655 new_rot = bytestream2_get_byte(&ctx->gb);
656 skip = bytestream2_get_byte(&ctx->gb);
657 bytestream2_skip(&ctx->gb, 9);
658 decoded_size = bytestream2_get_le32(&ctx->gb);
659 bytestream2_skip(&ctx->gb, 8);
661 if (decoded_size > ctx->height * stride - left - top * stride) {
662 decoded_size = ctx->height * stride - left - top * stride;
663 av_log(ctx->avctx, AV_LOG_WARNING, "decoded size is too large\n");
667 bytestream2_skip(&ctx->gb, 0x8080);
670 memset(prev1, 0, ctx->height * stride);
671 memset(prev2, 0, ctx->height * stride);
673 av_dlog(ctx->avctx, "compression %d\n", compr);
676 if (bytestream2_get_bytes_left(&ctx->gb) < width * height)
677 return AVERROR_INVALIDDATA;
678 for (j = 0; j < height; j++) {
679 bytestream2_get_bufferu(&ctx->gb, dst, width);
684 if (bytestream2_get_bytes_left(&ctx->gb) < ((width + 1) >> 1) * ((height + 1) >> 1))
685 return AVERROR_INVALIDDATA;
686 for (j = 0; j < height; j += 2) {
687 for (i = 0; i < width; i += 2) {
688 dst[i] = dst[i + 1] =
689 dst[stride + i] = dst[stride + i + 1] = bytestream2_get_byteu(&ctx->gb);
695 if (seq == ctx->prev_seq + 1) {
696 for (j = 0; j < height; j += 8) {
697 for (i = 0; i < width; i += 8) {
698 if (process_block(ctx, dst + i, prev1 + i, prev2 + i, stride,
700 return AVERROR_INVALIDDATA;
709 memcpy(ctx->frm0, ctx->frm2, ctx->pitch * ctx->height);
712 memcpy(ctx->frm0, ctx->frm1, ctx->pitch * ctx->height);
715 if (rle_decode(ctx, dst, decoded_size))
716 return AVERROR_INVALIDDATA;
719 av_log(ctx->avctx, AV_LOG_ERROR,
720 "subcodec 47 compression %d not implemented\n", compr);
721 return AVERROR_PATCHWELCOME;
723 if (seq == ctx->prev_seq + 1)
724 ctx->rotate_code = new_rot;
726 ctx->rotate_code = 0;
732 static int process_frame_obj(SANMVideoContext *ctx)
734 uint16_t codec, top, left, w, h;
736 codec = bytestream2_get_le16u(&ctx->gb);
737 left = bytestream2_get_le16u(&ctx->gb);
738 top = bytestream2_get_le16u(&ctx->gb);
739 w = bytestream2_get_le16u(&ctx->gb);
740 h = bytestream2_get_le16u(&ctx->gb);
742 if (ctx->width < left + w || ctx->height < top + h) {
743 if (av_image_check_size(FFMAX(left + w, ctx->width),
744 FFMAX(top + h, ctx->height), 0, ctx->avctx) < 0)
745 return AVERROR_INVALIDDATA;
746 avcodec_set_dimensions(ctx->avctx, FFMAX(left + w, ctx->width),
747 FFMAX(top + h, ctx->height));
748 init_sizes(ctx, FFMAX(left + w, ctx->width),
749 FFMAX(top + h, ctx->height));
750 if (init_buffers(ctx)) {
751 av_log(ctx->avctx, AV_LOG_ERROR, "error resizing buffers\n");
752 return AVERROR(ENOMEM);
755 bytestream2_skip(&ctx->gb, 4);
757 av_dlog(ctx->avctx, "subcodec %d\n", codec);
761 return old_codec1(ctx, top, left, w, h);
764 return old_codec37(ctx, top, left, w, h);
767 return old_codec47(ctx, top, left, w, h);
770 av_log_ask_for_sample(ctx->avctx, "unknown subcodec %d\n", codec);
771 return AVERROR_PATCHWELCOME;
775 static int decode_0(SANMVideoContext *ctx)
777 uint16_t *frm = ctx->frm0;
780 if (bytestream2_get_bytes_left(&ctx->gb) < ctx->width * ctx->height * 2) {
781 av_log(ctx->avctx, AV_LOG_ERROR, "insufficient data for raw frame\n");
782 return AVERROR_INVALIDDATA;
784 for (y = 0; y < ctx->height; y++) {
785 for (x = 0; x < ctx->width; x++)
786 frm[x] = bytestream2_get_le16u(&ctx->gb);
792 static int decode_nop(SANMVideoContext *ctx)
794 av_log_ask_for_sample(ctx->avctx, "unknown/unsupported compression type\n");
795 return AVERROR_PATCHWELCOME;
798 static void copy_block(uint16_t *pdest, uint16_t *psrc, int block_size, int pitch)
800 uint8_t *dst = (uint8_t *)pdest;
801 uint8_t *src = (uint8_t *)psrc;
802 int stride = pitch * 2;
804 switch (block_size) {
806 copy_block4(dst, src, stride, stride, 2);
809 copy_block8(dst, src, stride, stride, 4);
812 copy_block16(dst, src, stride, stride, 8);
817 static void fill_block(uint16_t *pdest, uint16_t color, int block_size, int pitch)
822 for (y = 0; y < block_size; y++, pdest += pitch)
823 for (x = 0; x < block_size; x++)
827 static int draw_glyph(SANMVideoContext *ctx, uint16_t *dst, int index, uint16_t fg_color,
828 uint16_t bg_color, int block_size, int pitch)
831 uint16_t colors[2] = { fg_color, bg_color };
834 if (index >= NGLYPHS) {
835 av_log(ctx->avctx, AV_LOG_ERROR, "ignoring nonexistent glyph #%u\n", index);
836 return AVERROR_INVALIDDATA;
839 pglyph = block_size == 8 ? ctx->p8x8glyphs[index] : ctx->p4x4glyphs[index];
842 for (y = 0; y < block_size; y++, dst += pitch)
843 for (x = 0; x < block_size; x++)
844 *dst++ = colors[*pglyph++];
848 static int opcode_0xf7(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
850 uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
852 if (block_size == 2) {
855 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
856 return AVERROR_INVALIDDATA;
858 indices = bytestream2_get_le32u(&ctx->gb);
859 dst[0] = ctx->codebook[indices & 0xFF]; indices >>= 8;
860 dst[1] = ctx->codebook[indices & 0xFF]; indices >>= 8;
861 dst[pitch] = ctx->codebook[indices & 0xFF]; indices >>= 8;
862 dst[pitch + 1] = ctx->codebook[indices & 0xFF];
864 uint16_t fgcolor, bgcolor;
867 if (bytestream2_get_bytes_left(&ctx->gb) < 3)
868 return AVERROR_INVALIDDATA;
870 glyph = bytestream2_get_byteu(&ctx->gb);
871 bgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
872 fgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
874 draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
879 static int opcode_0xf8(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
881 uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
883 if (block_size == 2) {
884 if (bytestream2_get_bytes_left(&ctx->gb) < 8)
885 return AVERROR_INVALIDDATA;
887 dst[0] = bytestream2_get_le16u(&ctx->gb);
888 dst[1] = bytestream2_get_le16u(&ctx->gb);
889 dst[pitch] = bytestream2_get_le16u(&ctx->gb);
890 dst[pitch + 1] = bytestream2_get_le16u(&ctx->gb);
892 uint16_t fgcolor, bgcolor;
895 if (bytestream2_get_bytes_left(&ctx->gb) < 5)
896 return AVERROR_INVALIDDATA;
898 glyph = bytestream2_get_byteu(&ctx->gb);
899 bgcolor = bytestream2_get_le16u(&ctx->gb);
900 fgcolor = bytestream2_get_le16u(&ctx->gb);
902 draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
907 static int good_mvec(SANMVideoContext *ctx, int cx, int cy, int mx, int my,
910 int start_pos = cx + mx + (cy + my) * ctx->pitch;
911 int end_pos = start_pos + (block_size - 1) * (ctx->pitch + 1);
913 int good = start_pos >= 0 && end_pos < (ctx->buf_size >> 1);
916 av_log(ctx->avctx, AV_LOG_ERROR, "ignoring invalid motion vector (%i, %i)->(%u, %u), block size = %u\n",
917 cx + mx, cy + my, cx, cy, block_size);
923 static int codec2subblock(SANMVideoContext *ctx, int cx, int cy, int blk_size)
925 int16_t mx, my, index;
928 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
929 return AVERROR_INVALIDDATA;
931 opcode = bytestream2_get_byteu(&ctx->gb);
933 av_dlog(ctx->avctx, "opcode 0x%0X cx %d cy %d blk %d\n", opcode, cx, cy, blk_size);
936 mx = motion_vectors[opcode][0];
937 my = motion_vectors[opcode][1];
939 if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
940 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
941 ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
942 blk_size, ctx->pitch);
946 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
947 return AVERROR_INVALIDDATA;
948 index = bytestream2_get_le16u(&ctx->gb);
950 mx = index % ctx->width;
951 my = index / ctx->width;
953 if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
954 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
955 ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
956 blk_size, ctx->pitch);
960 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
961 ctx->frm1 + cx + ctx->pitch * cy,
962 blk_size, ctx->pitch);
965 opcode_0xf7(ctx, cx, cy, blk_size, ctx->pitch);
969 opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
975 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
976 ctx->small_codebook[opcode - 0xf9], blk_size, ctx->pitch);
979 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
980 return AVERROR_INVALIDDATA;
981 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
982 ctx->codebook[bytestream2_get_byteu(&ctx->gb)], blk_size, ctx->pitch);
985 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
986 return AVERROR_INVALIDDATA;
987 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
988 bytestream2_get_le16u(&ctx->gb), blk_size, ctx->pitch);
992 opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
995 if (codec2subblock(ctx, cx , cy , blk_size))
996 return AVERROR_INVALIDDATA;
997 if (codec2subblock(ctx, cx + blk_size, cy , blk_size))
998 return AVERROR_INVALIDDATA;
999 if (codec2subblock(ctx, cx , cy + blk_size, blk_size))
1000 return AVERROR_INVALIDDATA;
1001 if (codec2subblock(ctx, cx + blk_size, cy + blk_size, blk_size))
1002 return AVERROR_INVALIDDATA;
1009 static int decode_2(SANMVideoContext *ctx)
1013 for (cy = 0; cy < ctx->aligned_height; cy += 8) {
1014 for (cx = 0; cx < ctx->aligned_width; cx += 8) {
1015 if (ret = codec2subblock(ctx, cx, cy, 8))
1023 static int decode_3(SANMVideoContext *ctx)
1025 memcpy(ctx->frm0, ctx->frm2, ctx->frm2_size);
1029 static int decode_4(SANMVideoContext *ctx)
1031 memcpy(ctx->frm0, ctx->frm1, ctx->frm1_size);
1035 static int decode_5(SANMVideoContext *ctx)
1041 uint8_t *dst = (uint8_t*)ctx->frm0;
1043 if (rle_decode(ctx, dst, ctx->buf_size))
1044 return AVERROR_INVALIDDATA;
1047 npixels = ctx->npixels;
1050 *frm++ = av_bswap16(*frm);
1056 static int decode_6(SANMVideoContext *ctx)
1058 int npixels = ctx->npixels;
1059 uint16_t *frm = ctx->frm0;
1061 if (bytestream2_get_bytes_left(&ctx->gb) < npixels) {
1062 av_log(ctx->avctx, AV_LOG_ERROR, "insufficient data for frame\n");
1063 return AVERROR_INVALIDDATA;
1066 *frm++ = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
1071 static int decode_8(SANMVideoContext *ctx)
1073 uint16_t *pdest = ctx->frm0;
1075 long npixels = ctx->npixels;
1077 av_fast_malloc(&ctx->rle_buf, &ctx->rle_buf_size, npixels);
1078 if (!ctx->rle_buf) {
1079 av_log(ctx->avctx, AV_LOG_ERROR, "RLE buffer allocation failed\n");
1080 return AVERROR(ENOMEM);
1082 rsrc = ctx->rle_buf;
1084 if (rle_decode(ctx, rsrc, npixels))
1085 return AVERROR_INVALIDDATA;
1088 *pdest++ = ctx->codebook[*rsrc++];
1093 typedef int (*frm_decoder)(SANMVideoContext *ctx);
1095 static const frm_decoder v1_decoders[] = {
1096 decode_0, decode_nop, decode_2, decode_3, decode_4, decode_5,
1097 decode_6, decode_nop, decode_8
1100 static int read_frame_header(SANMVideoContext *ctx, SANMFrameHeader *hdr)
1104 if ((ret = bytestream2_get_bytes_left(&ctx->gb)) < 560) {
1105 av_log(ctx->avctx, AV_LOG_ERROR, "too short input frame (%d bytes)\n",
1107 return AVERROR_INVALIDDATA;
1109 bytestream2_skip(&ctx->gb, 8); // skip pad
1111 hdr->width = bytestream2_get_le32u(&ctx->gb);
1112 hdr->height = bytestream2_get_le32u(&ctx->gb);
1114 if (hdr->width != ctx->width || hdr->height != ctx->height) {
1115 av_log(ctx->avctx, AV_LOG_ERROR, "variable size frames are not implemented\n");
1116 return AVERROR_PATCHWELCOME;
1119 hdr->seq_num = bytestream2_get_le16u(&ctx->gb);
1120 hdr->codec = bytestream2_get_byteu(&ctx->gb);
1121 hdr->rotate_code = bytestream2_get_byteu(&ctx->gb);
1123 bytestream2_skip(&ctx->gb, 4); // skip pad
1125 for (i = 0; i < 4; i++)
1126 ctx->small_codebook[i] = bytestream2_get_le16u(&ctx->gb);
1127 hdr->bg_color = bytestream2_get_le16u(&ctx->gb);
1129 bytestream2_skip(&ctx->gb, 2); // skip pad
1131 hdr->rle_output_size = bytestream2_get_le32u(&ctx->gb);
1132 for (i = 0; i < 256; i++)
1133 ctx->codebook[i] = bytestream2_get_le16u(&ctx->gb);
1135 bytestream2_skip(&ctx->gb, 8); // skip pad
1137 av_dlog(ctx->avctx, "subcodec %d\n", hdr->codec);
1141 static void fill_frame(uint16_t *pbuf, int buf_size, uint16_t color)
1147 static int copy_output(SANMVideoContext *ctx, SANMFrameHeader *hdr)
1150 const uint8_t *src = (uint8_t*) ctx->frm0;
1151 int ret, dstpitch, height = ctx->height;
1152 int srcpitch = ctx->pitch * (hdr ? sizeof(ctx->frm0[0]) : 1);
1154 if ((ret = ff_get_buffer(ctx->avctx, ctx->output)) < 0) {
1155 av_log(ctx->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1159 dst = ctx->output->data[0];
1160 dstpitch = ctx->output->linesize[0];
1163 memcpy(dst, src, srcpitch);
1171 static int decode_frame(AVCodecContext *avctx, void *data,
1172 int *got_frame_ptr, AVPacket *pkt)
1174 SANMVideoContext *ctx = avctx->priv_data;
1177 bytestream2_init(&ctx->gb, pkt->data, pkt->size);
1178 if (ctx->output->data[0])
1179 avctx->release_buffer(avctx, ctx->output);
1181 if (!ctx->version) {
1184 while (bytestream2_get_bytes_left(&ctx->gb) >= 8) {
1188 sig = bytestream2_get_be32u(&ctx->gb);
1189 size = bytestream2_get_be32u(&ctx->gb);
1190 pos = bytestream2_tell(&ctx->gb);
1192 if (bytestream2_get_bytes_left(&ctx->gb) < size) {
1193 av_log(avctx, AV_LOG_ERROR, "incorrect chunk size %d\n", size);
1197 case MKBETAG('N', 'P', 'A', 'L'):
1198 if (size != 256 * 3) {
1199 av_log(avctx, AV_LOG_ERROR, "incorrect palette block size %d\n",
1201 return AVERROR_INVALIDDATA;
1203 for (i = 0; i < 256; i++)
1204 ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
1206 case MKBETAG('F', 'O', 'B', 'J'):
1208 return AVERROR_INVALIDDATA;
1209 if (ret = process_frame_obj(ctx))
1212 case MKBETAG('X', 'P', 'A', 'L'):
1213 if (size == 6 || size == 4) {
1217 for (i = 0; i < 256; i++) {
1218 for (j = 0; j < 3; j++) {
1219 int t = (ctx->pal[i] >> (16 - j * 8)) & 0xFF;
1220 tmp[j] = av_clip_uint8((t * 129 + ctx->delta_pal[i * 3 + j]) >> 7);
1222 ctx->pal[i] = 0xFFU << 24 | AV_RB24(tmp);
1225 if (size < 768 * 2 + 4) {
1226 av_log(avctx, AV_LOG_ERROR, "incorrect palette change block size %d\n",
1228 return AVERROR_INVALIDDATA;
1230 bytestream2_skipu(&ctx->gb, 4);
1231 for (i = 0; i < 768; i++)
1232 ctx->delta_pal[i] = bytestream2_get_le16u(&ctx->gb);
1233 if (size >= 768 * 5 + 4) {
1234 for (i = 0; i < 256; i++)
1235 ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
1237 memset(ctx->pal, 0, sizeof(ctx->pal));
1241 case MKBETAG('S', 'T', 'O', 'R'):
1244 case MKBETAG('F', 'T', 'C', 'H'):
1245 memcpy(ctx->frm0, ctx->stored_frame, ctx->buf_size);
1248 bytestream2_skip(&ctx->gb, size);
1249 av_log(avctx, AV_LOG_DEBUG, "unknown/unsupported chunk %x\n", sig);
1253 bytestream2_seek(&ctx->gb, pos + size, SEEK_SET);
1255 bytestream2_skip(&ctx->gb, 1);
1258 memcpy(ctx->stored_frame, ctx->frm0, ctx->buf_size);
1259 if ((ret = copy_output(ctx, NULL)))
1261 memcpy(ctx->output->data[1], ctx->pal, 1024);
1263 SANMFrameHeader header;
1265 if ((ret = read_frame_header(ctx, &header)))
1268 ctx->rotate_code = header.rotate_code;
1269 if ((ctx->output->key_frame = !header.seq_num)) {
1270 ctx->output->pict_type = AV_PICTURE_TYPE_I;
1271 fill_frame(ctx->frm1, ctx->npixels, header.bg_color);
1272 fill_frame(ctx->frm2, ctx->npixels, header.bg_color);
1274 ctx->output->pict_type = AV_PICTURE_TYPE_P;
1277 if (header.codec < FF_ARRAY_ELEMS(v1_decoders)) {
1278 if ((ret = v1_decoders[header.codec](ctx))) {
1279 av_log(avctx, AV_LOG_ERROR,
1280 "subcodec %d: error decoding frame\n", header.codec);
1284 av_log_ask_for_sample(avctx, "subcodec %d is not implemented\n",
1286 return AVERROR_PATCHWELCOME;
1289 if ((ret = copy_output(ctx, &header)))
1292 if (ctx->rotate_code)
1293 rotate_bufs(ctx, ctx->rotate_code);
1296 *(AVFrame*)data = *ctx->output;
1301 AVCodec ff_sanm_decoder = {
1303 .type = AVMEDIA_TYPE_VIDEO,
1304 .id = AV_CODEC_ID_SANM,
1305 .priv_data_size = sizeof(SANMVideoContext),
1306 .init = decode_init,
1307 .close = decode_end,
1308 .decode = decode_frame,
1309 .capabilities = CODEC_CAP_DR1,
1310 .long_name = NULL_IF_CONFIG_SMALL("LucasArts SMUSH video"),