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 "libavutil/imgutils.h"
30 #include "libavcodec/dsputil.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);
236 static av_cold int init_buffers(SANMVideoContext *ctx)
238 av_fast_padded_malloc(&ctx->frm0, &ctx->frm0_size, ctx->buf_size);
239 av_fast_padded_malloc(&ctx->frm1, &ctx->frm1_size, ctx->buf_size);
240 av_fast_padded_malloc(&ctx->frm2, &ctx->frm2_size, ctx->buf_size);
242 av_fast_padded_malloc(&ctx->stored_frame, &ctx->stored_frame_size, ctx->buf_size);
244 if (!ctx->frm0 || !ctx->frm1 || !ctx->frm2 || (!ctx->stored_frame && !ctx->version)) {
245 destroy_buffers(ctx);
246 return AVERROR(ENOMEM);
252 static void rotate_bufs(SANMVideoContext *ctx, int rotate_code)
254 av_dlog(ctx->avctx, "rotate %d\n", rotate_code);
255 if (rotate_code == 2)
256 FFSWAP(uint16_t*, ctx->frm1, ctx->frm2);
257 FFSWAP(uint16_t*, ctx->frm2, ctx->frm0);
260 static av_cold int decode_init(AVCodecContext *avctx)
262 SANMVideoContext *ctx = avctx->priv_data;
265 ctx->version = !avctx->extradata_size;
267 avctx->pix_fmt = ctx->version ? AV_PIX_FMT_RGB565 : AV_PIX_FMT_PAL8;
269 init_sizes(ctx, avctx->width, avctx->height);
270 if (init_buffers(ctx)) {
271 av_log(avctx, AV_LOG_ERROR, "error allocating buffers\n");
272 return AVERROR(ENOMEM);
274 ctx->output = &ctx->frame;
275 ctx->output->data[0] = 0;
277 make_glyphs(ctx->p4x4glyphs[0], glyph4_x, glyph4_y, 4);
278 make_glyphs(ctx->p8x8glyphs[0], glyph8_x, glyph8_y, 8);
283 if (avctx->extradata_size < 1026) {
284 av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");
285 return AVERROR_INVALIDDATA;
288 ctx->subversion = AV_RL16(avctx->extradata);
289 for (i = 0; i < 256; i++)
290 ctx->pal[i] = 0xFFU << 24 | AV_RL32(avctx->extradata + 2 + i * 4);
296 static av_cold int decode_end(AVCodecContext *avctx)
298 SANMVideoContext *ctx = avctx->priv_data;
300 destroy_buffers(ctx);
302 if (ctx->frame.data[0]) {
303 avctx->release_buffer(avctx, &ctx->frame);
304 ctx->frame.data[0] = 0;
310 static int rle_decode(SANMVideoContext *ctx, uint8_t *dst, const int out_size)
312 int opcode, color, run_len, left = out_size;
315 opcode = bytestream2_get_byte(&ctx->gb);
316 run_len = (opcode >> 1) + 1;
317 if (run_len > left || bytestream2_get_bytes_left(&ctx->gb) <= 0)
318 return AVERROR_INVALIDDATA;
321 color = bytestream2_get_byte(&ctx->gb);
322 memset(dst, color, run_len);
324 if (bytestream2_get_bytes_left(&ctx->gb) < run_len)
325 return AVERROR_INVALIDDATA;
326 bytestream2_get_bufferu(&ctx->gb, dst, run_len);
336 static int old_codec1(SANMVideoContext *ctx, int top,
337 int left, int width, int height)
339 uint8_t *dst = ((uint8_t*)ctx->frm0) + left + top * ctx->pitch;
340 int i, j, len, flag, code, val, pos, end;
342 for (i = 0; i < height; i++) {
345 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
346 return AVERROR_INVALIDDATA;
348 len = bytestream2_get_le16u(&ctx->gb);
349 end = bytestream2_tell(&ctx->gb) + len;
351 while (bytestream2_tell(&ctx->gb) < end) {
352 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
353 return AVERROR_INVALIDDATA;
355 code = bytestream2_get_byteu(&ctx->gb);
357 code = (code >> 1) + 1;
358 if (pos + code > width)
359 return AVERROR_INVALIDDATA;
361 val = bytestream2_get_byteu(&ctx->gb);
363 memset(dst + pos, val, code);
366 if (bytestream2_get_bytes_left(&ctx->gb) < code)
367 return AVERROR_INVALIDDATA;
368 for (j = 0; j < code; j++) {
369 val = bytestream2_get_byteu(&ctx->gb);
378 ctx->rotate_code = 0;
383 static inline void codec37_mv(uint8_t *dst, const uint8_t *src,
384 int height, int stride, int x, int y)
388 pos = x + y * stride;
389 for (j = 0; j < 4; j++) {
390 for (i = 0; i < 4; i++) {
391 if ((pos + i) < 0 || (pos + i) >= height * stride)
402 static int old_codec37(SANMVideoContext *ctx, int top,
403 int left, int width, int height)
405 int stride = ctx->pitch;
408 int compr, mvoff, seq, flags;
409 uint32_t decoded_size;
412 compr = bytestream2_get_byte(&ctx->gb);
413 mvoff = bytestream2_get_byte(&ctx->gb);
414 seq = bytestream2_get_le16(&ctx->gb);
415 decoded_size = bytestream2_get_le32(&ctx->gb);
416 bytestream2_skip(&ctx->gb, 4);
417 flags = bytestream2_get_byte(&ctx->gb);
418 bytestream2_skip(&ctx->gb, 3);
420 ctx->rotate_code = 0;
422 if (((seq & 1) || !(flags & 1)) && (compr && compr != 2))
425 dst = ((uint8_t*)ctx->frm0) + left + top * stride;
426 prev = ((uint8_t*)ctx->frm2) + left + top * stride;
429 av_log(ctx->avctx, AV_LOG_ERROR, "invalid motion base value %d\n", mvoff);
430 return AVERROR_INVALIDDATA;
432 av_dlog(ctx->avctx, "compression %d\n", compr);
435 for (i = 0; i < height; i++) {
436 bytestream2_get_buffer(&ctx->gb, dst, width);
439 memset(ctx->frm1, 0, ctx->height * stride);
440 memset(ctx->frm2, 0, ctx->height * stride);
443 if (rle_decode(ctx, dst, decoded_size))
444 return AVERROR_INVALIDDATA;
445 memset(ctx->frm1, 0, ctx->frm1_size);
446 memset(ctx->frm2, 0, ctx->frm2_size);
451 for (j = 0; j < height; j += 4) {
452 for (i = 0; i < width; i += 4) {
456 copy_block4(dst + i, prev + i, stride, stride, 4);
459 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
460 return AVERROR_INVALIDDATA;
461 code = bytestream2_get_byteu(&ctx->gb);
464 if (bytestream2_get_bytes_left(&ctx->gb) < 16)
465 return AVERROR_INVALIDDATA;
466 for (k = 0; k < 4; k++)
467 bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
470 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
471 return AVERROR_INVALIDDATA;
472 for (k = 0; k < 4; k++)
473 memset(dst + i + k * stride, bytestream2_get_byteu(&ctx->gb), 4);
476 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
477 return AVERROR_INVALIDDATA;
478 t = bytestream2_get_byteu(&ctx->gb);
479 for (k = 0; k < 4; k++)
480 memset(dst + i + k * stride, t, 4);
483 if (compr == 4 && !code) {
484 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
485 return AVERROR_INVALIDDATA;
486 skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
491 mx = c37_mv[(mvoff * 255 + code) * 2 ];
492 my = c37_mv[(mvoff * 255 + code) * 2 + 1];
493 codec37_mv(dst + i, prev + i + mx + my * stride,
494 ctx->height, stride, i + mx, j + my);
502 for (j = 0; j < height; j += 4) {
503 for (i = 0; i < width; i += 4) {
507 copy_block4(dst + i, prev + i, stride, stride, 4);
510 code = bytestream2_get_byte(&ctx->gb);
512 if (bytestream2_get_bytes_left(&ctx->gb) < 16)
513 return AVERROR_INVALIDDATA;
514 for (k = 0; k < 4; k++)
515 bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
516 } else if (compr == 4 && !code) {
517 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
518 return AVERROR_INVALIDDATA;
519 skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
524 mx = c37_mv[(mvoff * 255 + code) * 2];
525 my = c37_mv[(mvoff * 255 + code) * 2 + 1];
526 codec37_mv(dst + i, prev + i + mx + my * stride,
527 ctx->height, stride, i + mx, j + my);
536 av_log(ctx->avctx, AV_LOG_ERROR,
537 "subcodec 37 compression %d not implemented\n", compr);
538 return AVERROR_PATCHWELCOME;
544 static int process_block(SANMVideoContext *ctx, uint8_t *dst, uint8_t *prev1,
545 uint8_t *prev2, int stride, int tbl, int size)
551 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
552 return AVERROR_INVALIDDATA;
554 code = bytestream2_get_byteu(&ctx->gb);
559 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
560 return AVERROR_INVALIDDATA;
561 dst[0] = bytestream2_get_byteu(&ctx->gb);
562 dst[1] = bytestream2_get_byteu(&ctx->gb);
563 dst[0+stride] = bytestream2_get_byteu(&ctx->gb);
564 dst[1+stride] = bytestream2_get_byteu(&ctx->gb);
567 if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
568 return AVERROR_INVALIDDATA;
569 if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
571 return AVERROR_INVALIDDATA;
572 dst += size * stride;
573 prev1 += size * stride;
574 prev2 += size * stride;
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;
583 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
584 return AVERROR_INVALIDDATA;
586 t = bytestream2_get_byteu(&ctx->gb);
587 for (k = 0; k < size; k++)
588 memset(dst + k * stride, t, size);
591 if (bytestream2_get_bytes_left(&ctx->gb) < 3)
592 return AVERROR_INVALIDDATA;
594 code = bytestream2_get_byteu(&ctx->gb);
595 pglyph = (size == 8) ? ctx->p8x8glyphs[code] : ctx->p4x4glyphs[code];
596 bytestream2_get_bufferu(&ctx->gb, colors, 2);
598 for (k = 0; k < size; k++)
599 for (t = 0; t < size; t++)
600 dst[t + k * stride] = colors[!*pglyph++];
603 for (k = 0; k < size; k++)
604 memcpy(dst + k * stride, prev1 + k * stride, size);
607 k = bytestream2_tell(&ctx->gb);
608 bytestream2_seek(&ctx->gb, tbl + (code & 7), SEEK_SET);
609 t = bytestream2_get_byte(&ctx->gb);
610 bytestream2_seek(&ctx->gb, k, SEEK_SET);
611 for (k = 0; k < size; k++)
612 memset(dst + k * stride, t, size);
615 int mx = motion_vectors[code][0];
616 int my = motion_vectors[code][1];
617 int index = prev2 - (const uint8_t*)ctx->frm2;
619 av_assert2(index >= 0 && index < (ctx->buf_size>>1));
621 if (index < - mx - my*stride ||
622 (ctx->buf_size>>1) - index < mx + size + (my + size - 1)*stride) {
623 av_log(ctx->avctx, AV_LOG_ERROR, "MV is invalid \n");
624 return AVERROR_INVALIDDATA;
627 for (k = 0; k < size; k++)
628 memcpy(dst + k * stride, prev2 + mx + (my + k) * stride, size);
634 static int old_codec47(SANMVideoContext *ctx, int top,
635 int left, int width, int height)
637 int i, j, seq, compr, new_rot, tbl_pos, skip;
638 int stride = ctx->pitch;
639 uint8_t *dst = ((uint8_t*)ctx->frm0) + left + top * stride;
640 uint8_t *prev1 = (uint8_t*)ctx->frm1;
641 uint8_t *prev2 = (uint8_t*)ctx->frm2;
642 uint32_t decoded_size;
644 tbl_pos = bytestream2_tell(&ctx->gb);
645 seq = bytestream2_get_le16(&ctx->gb);
646 compr = bytestream2_get_byte(&ctx->gb);
647 new_rot = bytestream2_get_byte(&ctx->gb);
648 skip = bytestream2_get_byte(&ctx->gb);
649 bytestream2_skip(&ctx->gb, 9);
650 decoded_size = bytestream2_get_le32(&ctx->gb);
651 bytestream2_skip(&ctx->gb, 8);
653 if (decoded_size > height * stride - left - top * stride) {
654 decoded_size = height * stride - left - top * stride;
655 av_log(ctx->avctx, AV_LOG_WARNING, "decoded size is too large\n");
659 bytestream2_skip(&ctx->gb, 0x8080);
662 memset(prev1, 0, ctx->height * stride);
663 memset(prev2, 0, ctx->height * stride);
665 av_dlog(ctx->avctx, "compression %d\n", compr);
668 if (bytestream2_get_bytes_left(&ctx->gb) < width * height)
669 return AVERROR_INVALIDDATA;
670 for (j = 0; j < height; j++) {
671 bytestream2_get_bufferu(&ctx->gb, dst, width);
676 if (bytestream2_get_bytes_left(&ctx->gb) < ((width + 1) >> 1) * ((height + 1) >> 1))
677 return AVERROR_INVALIDDATA;
678 for (j = 0; j < height; j += 2) {
679 for (i = 0; i < width; i += 2) {
680 dst[i] = dst[i + 1] =
681 dst[stride + i] = dst[stride + i + 1] = bytestream2_get_byteu(&ctx->gb);
687 if (seq == ctx->prev_seq + 1) {
688 for (j = 0; j < height; j += 8) {
689 for (i = 0; i < width; i += 8) {
690 if (process_block(ctx, dst + i, prev1 + i, prev2 + i, stride,
692 return AVERROR_INVALIDDATA;
701 memcpy(ctx->frm0, ctx->frm2, ctx->pitch * ctx->height);
704 memcpy(ctx->frm0, ctx->frm1, ctx->pitch * ctx->height);
707 if (rle_decode(ctx, dst, decoded_size))
708 return AVERROR_INVALIDDATA;
711 av_log(ctx->avctx, AV_LOG_ERROR,
712 "subcodec 47 compression %d not implemented\n", compr);
713 return AVERROR_PATCHWELCOME;
715 if (seq == ctx->prev_seq + 1)
716 ctx->rotate_code = new_rot;
718 ctx->rotate_code = 0;
724 static int process_frame_obj(SANMVideoContext *ctx)
726 uint16_t codec, top, left, w, h;
728 codec = bytestream2_get_le16u(&ctx->gb);
729 left = bytestream2_get_le16u(&ctx->gb);
730 top = bytestream2_get_le16u(&ctx->gb);
731 w = bytestream2_get_le16u(&ctx->gb);
732 h = bytestream2_get_le16u(&ctx->gb);
734 if (ctx->width < left + w || ctx->height < top + h) {
735 if (av_image_check_size(FFMAX(left + w, ctx->width),
736 FFMAX(top + h, ctx->height), 0, ctx->avctx) < 0)
737 return AVERROR_INVALIDDATA;
738 avcodec_set_dimensions(ctx->avctx, FFMAX(left + w, ctx->width),
739 FFMAX(top + h, ctx->height));
740 init_sizes(ctx, left + w, top + h);
741 if (init_buffers(ctx)) {
742 av_log(ctx->avctx, AV_LOG_ERROR, "error resizing buffers\n");
743 return AVERROR(ENOMEM);
746 bytestream2_skip(&ctx->gb, 4);
748 av_dlog(ctx->avctx, "subcodec %d\n", codec);
752 return old_codec1(ctx, top, left, w, h);
755 return old_codec37(ctx, top, left, w, h);
758 return old_codec47(ctx, top, left, w, h);
761 av_log_ask_for_sample(ctx->avctx, "unknown subcodec %d\n", codec);
762 return AVERROR_PATCHWELCOME;
766 static int decode_0(SANMVideoContext *ctx)
768 uint16_t *frm = ctx->frm0;
771 if (bytestream2_get_bytes_left(&ctx->gb) < ctx->width * ctx->height * 2) {
772 av_log(ctx->avctx, AV_LOG_ERROR, "insufficient data for raw frame\n");
773 return AVERROR_INVALIDDATA;
775 for (y = 0; y < ctx->height; y++) {
776 for (x = 0; x < ctx->width; x++)
777 frm[x] = bytestream2_get_le16u(&ctx->gb);
783 static int decode_nop(SANMVideoContext *ctx)
785 av_log_ask_for_sample(ctx->avctx, "unknown/unsupported compression type\n");
786 return AVERROR_PATCHWELCOME;
789 static void copy_block(uint16_t *pdest, uint16_t *psrc, int block_size, int pitch)
791 uint8_t *dst = (uint8_t *)pdest;
792 uint8_t *src = (uint8_t *)psrc;
793 int stride = pitch * 2;
795 switch (block_size) {
797 copy_block4(dst, src, stride, stride, 2);
800 copy_block8(dst, src, stride, stride, 4);
803 copy_block16(dst, src, stride, stride, 8);
808 static void fill_block(uint16_t *pdest, uint16_t color, int block_size, int pitch)
813 for (y = 0; y < block_size; y++, pdest += pitch)
814 for (x = 0; x < block_size; x++)
818 static int draw_glyph(SANMVideoContext *ctx, uint16_t *dst, int index, uint16_t fg_color,
819 uint16_t bg_color, int block_size, int pitch)
822 uint16_t colors[2] = { fg_color, bg_color };
825 if (index >= NGLYPHS) {
826 av_log(ctx->avctx, AV_LOG_ERROR, "ignoring nonexistent glyph #%u\n", index);
827 return AVERROR_INVALIDDATA;
830 pglyph = block_size == 8 ? ctx->p8x8glyphs[index] : ctx->p4x4glyphs[index];
833 for (y = 0; y < block_size; y++, dst += pitch)
834 for (x = 0; x < block_size; x++)
835 *dst++ = colors[*pglyph++];
839 static int opcode_0xf7(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
841 uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
843 if (block_size == 2) {
846 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
847 return AVERROR_INVALIDDATA;
849 indices = bytestream2_get_le32u(&ctx->gb);
850 dst[0] = ctx->codebook[indices & 0xFF]; indices >>= 8;
851 dst[1] = ctx->codebook[indices & 0xFF]; indices >>= 8;
852 dst[pitch] = ctx->codebook[indices & 0xFF]; indices >>= 8;
853 dst[pitch + 1] = ctx->codebook[indices & 0xFF];
855 uint16_t fgcolor, bgcolor;
858 if (bytestream2_get_bytes_left(&ctx->gb) < 3)
859 return AVERROR_INVALIDDATA;
861 glyph = bytestream2_get_byteu(&ctx->gb);
862 bgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
863 fgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
865 draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
870 static int opcode_0xf8(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
872 uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
874 if (block_size == 2) {
875 if (bytestream2_get_bytes_left(&ctx->gb) < 8)
876 return AVERROR_INVALIDDATA;
878 dst[0] = bytestream2_get_le16u(&ctx->gb);
879 dst[1] = bytestream2_get_le16u(&ctx->gb);
880 dst[pitch] = bytestream2_get_le16u(&ctx->gb);
881 dst[pitch + 1] = bytestream2_get_le16u(&ctx->gb);
883 uint16_t fgcolor, bgcolor;
886 if (bytestream2_get_bytes_left(&ctx->gb) < 5)
887 return AVERROR_INVALIDDATA;
889 glyph = bytestream2_get_byteu(&ctx->gb);
890 bgcolor = bytestream2_get_le16u(&ctx->gb);
891 fgcolor = bytestream2_get_le16u(&ctx->gb);
893 draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
898 static int good_mvec(SANMVideoContext *ctx, int cx, int cy, int mx, int my,
901 int start_pos = cx + mx + (cy + my) * ctx->pitch;
902 int end_pos = start_pos + (block_size - 1) * (ctx->pitch + 1);
904 int good = start_pos >= 0 && end_pos < (ctx->buf_size >> 1);
907 av_log(ctx->avctx, AV_LOG_ERROR, "ignoring invalid motion vector (%i, %i)->(%u, %u), block size = %u\n",
908 cx + mx, cy + my, cx, cy, block_size);
914 static int codec2subblock(SANMVideoContext *ctx, int cx, int cy, int blk_size)
916 int16_t mx, my, index;
919 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
920 return AVERROR_INVALIDDATA;
922 opcode = bytestream2_get_byteu(&ctx->gb);
924 av_dlog(ctx->avctx, "opcode 0x%0X cx %d cy %d blk %d\n", opcode, cx, cy, blk_size);
927 mx = motion_vectors[opcode][0];
928 my = motion_vectors[opcode][1];
930 if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
931 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
932 ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
933 blk_size, ctx->pitch);
937 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
938 return AVERROR_INVALIDDATA;
939 index = bytestream2_get_le16u(&ctx->gb);
941 mx = index % ctx->width;
942 my = index / ctx->width;
944 if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
945 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
946 ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
947 blk_size, ctx->pitch);
951 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
952 ctx->frm1 + cx + ctx->pitch * cy,
953 blk_size, ctx->pitch);
956 opcode_0xf7(ctx, cx, cy, blk_size, ctx->pitch);
960 opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
966 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
967 ctx->small_codebook[opcode - 0xf9], blk_size, ctx->pitch);
970 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
971 return AVERROR_INVALIDDATA;
972 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
973 ctx->codebook[bytestream2_get_byteu(&ctx->gb)], blk_size, ctx->pitch);
976 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
977 return AVERROR_INVALIDDATA;
978 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
979 bytestream2_get_le16u(&ctx->gb), blk_size, ctx->pitch);
983 opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
986 if (codec2subblock(ctx, cx , cy , blk_size))
987 return AVERROR_INVALIDDATA;
988 if (codec2subblock(ctx, cx + blk_size, cy , blk_size))
989 return AVERROR_INVALIDDATA;
990 if (codec2subblock(ctx, cx , cy + blk_size, blk_size))
991 return AVERROR_INVALIDDATA;
992 if (codec2subblock(ctx, cx + blk_size, cy + blk_size, blk_size))
993 return AVERROR_INVALIDDATA;
1000 static int decode_2(SANMVideoContext *ctx)
1004 for (cy = 0; cy < ctx->aligned_height; cy += 8) {
1005 for (cx = 0; cx < ctx->aligned_width; cx += 8) {
1006 if (ret = codec2subblock(ctx, cx, cy, 8))
1014 static int decode_3(SANMVideoContext *ctx)
1016 memcpy(ctx->frm0, ctx->frm2, ctx->frm2_size);
1020 static int decode_4(SANMVideoContext *ctx)
1022 memcpy(ctx->frm0, ctx->frm1, ctx->frm1_size);
1026 static int decode_5(SANMVideoContext *ctx)
1032 uint8_t *dst = (uint8_t*)ctx->frm0;
1034 if (rle_decode(ctx, dst, ctx->buf_size))
1035 return AVERROR_INVALIDDATA;
1038 npixels = ctx->npixels;
1041 *frm++ = av_bswap16(*frm);
1047 static int decode_6(SANMVideoContext *ctx)
1049 int npixels = ctx->npixels;
1050 uint16_t *frm = ctx->frm0;
1052 if (bytestream2_get_bytes_left(&ctx->gb) < npixels) {
1053 av_log(ctx->avctx, AV_LOG_ERROR, "insufficient data for frame\n");
1054 return AVERROR_INVALIDDATA;
1057 *frm++ = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
1062 static int decode_8(SANMVideoContext *ctx)
1064 uint16_t *pdest = ctx->frm0;
1066 long npixels = ctx->npixels;
1068 av_fast_malloc(&ctx->rle_buf, &ctx->rle_buf_size, npixels);
1069 if (!ctx->rle_buf) {
1070 av_log(ctx->avctx, AV_LOG_ERROR, "RLE buffer allocation failed\n");
1071 return AVERROR(ENOMEM);
1073 rsrc = ctx->rle_buf;
1075 if (rle_decode(ctx, rsrc, npixels))
1076 return AVERROR_INVALIDDATA;
1079 *pdest++ = ctx->codebook[*rsrc++];
1084 typedef int (*frm_decoder)(SANMVideoContext *ctx);
1086 static const frm_decoder v1_decoders[] = {
1087 decode_0, decode_nop, decode_2, decode_3, decode_4, decode_5,
1088 decode_6, decode_nop, decode_8
1091 static int read_frame_header(SANMVideoContext *ctx, SANMFrameHeader *hdr)
1095 if ((ret = bytestream2_get_bytes_left(&ctx->gb)) < 560) {
1096 av_log(ctx->avctx, AV_LOG_ERROR, "too short input frame (%d bytes)\n",
1098 return AVERROR_INVALIDDATA;
1100 bytestream2_skip(&ctx->gb, 8); // skip pad
1102 hdr->width = bytestream2_get_le32u(&ctx->gb);
1103 hdr->height = bytestream2_get_le32u(&ctx->gb);
1105 if (hdr->width != ctx->width || hdr->height != ctx->height) {
1106 av_log(ctx->avctx, AV_LOG_ERROR, "variable size frames are not implemented\n");
1107 return AVERROR_PATCHWELCOME;
1110 hdr->seq_num = bytestream2_get_le16u(&ctx->gb);
1111 hdr->codec = bytestream2_get_byteu(&ctx->gb);
1112 hdr->rotate_code = bytestream2_get_byteu(&ctx->gb);
1114 bytestream2_skip(&ctx->gb, 4); // skip pad
1116 for (i = 0; i < 4; i++)
1117 ctx->small_codebook[i] = bytestream2_get_le16u(&ctx->gb);
1118 hdr->bg_color = bytestream2_get_le16u(&ctx->gb);
1120 bytestream2_skip(&ctx->gb, 2); // skip pad
1122 hdr->rle_output_size = bytestream2_get_le32u(&ctx->gb);
1123 for (i = 0; i < 256; i++)
1124 ctx->codebook[i] = bytestream2_get_le16u(&ctx->gb);
1126 bytestream2_skip(&ctx->gb, 8); // skip pad
1128 av_dlog(ctx->avctx, "subcodec %d\n", hdr->codec);
1132 static void fill_frame(uint16_t *pbuf, int buf_size, uint16_t color)
1138 static int copy_output(SANMVideoContext *ctx, SANMFrameHeader *hdr)
1141 const uint8_t *src = (uint8_t*) ctx->frm0;
1142 int ret, dstpitch, height = ctx->height;
1143 int srcpitch = ctx->pitch * (hdr ? sizeof(ctx->frm0[0]) : 1);
1145 if ((ret = ff_get_buffer(ctx->avctx, ctx->output)) < 0) {
1146 av_log(ctx->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1150 dst = ctx->output->data[0];
1151 dstpitch = ctx->output->linesize[0];
1154 memcpy(dst, src, srcpitch);
1162 static int decode_frame(AVCodecContext *avctx, void *data,
1163 int *got_frame_ptr, AVPacket *pkt)
1165 SANMVideoContext *ctx = avctx->priv_data;
1168 bytestream2_init(&ctx->gb, pkt->data, pkt->size);
1169 if (ctx->output->data[0])
1170 avctx->release_buffer(avctx, ctx->output);
1172 if (!ctx->version) {
1175 while (bytestream2_get_bytes_left(&ctx->gb) >= 8) {
1179 sig = bytestream2_get_be32u(&ctx->gb);
1180 size = bytestream2_get_be32u(&ctx->gb);
1181 pos = bytestream2_tell(&ctx->gb);
1183 if (bytestream2_get_bytes_left(&ctx->gb) < size) {
1184 av_log(avctx, AV_LOG_ERROR, "incorrect chunk size %d\n", size);
1188 case MKBETAG('N', 'P', 'A', 'L'):
1189 if (size != 256 * 3) {
1190 av_log(avctx, AV_LOG_ERROR, "incorrect palette block size %d\n",
1192 return AVERROR_INVALIDDATA;
1194 for (i = 0; i < 256; i++)
1195 ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
1197 case MKBETAG('F', 'O', 'B', 'J'):
1199 return AVERROR_INVALIDDATA;
1200 if (ret = process_frame_obj(ctx))
1203 case MKBETAG('X', 'P', 'A', 'L'):
1204 if (size == 6 || size == 4) {
1208 for (i = 0; i < 256; i++) {
1209 for (j = 0; j < 3; j++) {
1210 int t = (ctx->pal[i] >> (16 - j * 8)) & 0xFF;
1211 tmp[j] = av_clip_uint8((t * 129 + ctx->delta_pal[i * 3 + j]) >> 7);
1213 ctx->pal[i] = 0xFFU << 24 | AV_RB24(tmp);
1216 if (size < 768 * 2 + 4) {
1217 av_log(avctx, AV_LOG_ERROR, "incorrect palette change block size %d\n",
1219 return AVERROR_INVALIDDATA;
1221 bytestream2_skipu(&ctx->gb, 4);
1222 for (i = 0; i < 768; i++)
1223 ctx->delta_pal[i] = bytestream2_get_le16u(&ctx->gb);
1224 if (size >= 768 * 5 + 4) {
1225 for (i = 0; i < 256; i++)
1226 ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
1228 memset(ctx->pal, 0, sizeof(ctx->pal));
1232 case MKBETAG('S', 'T', 'O', 'R'):
1235 case MKBETAG('F', 'T', 'C', 'H'):
1236 memcpy(ctx->frm0, ctx->stored_frame, ctx->buf_size);
1239 bytestream2_skip(&ctx->gb, size);
1240 av_log(avctx, AV_LOG_DEBUG, "unknown/unsupported chunk %x\n", sig);
1244 bytestream2_seek(&ctx->gb, pos + size, SEEK_SET);
1246 bytestream2_skip(&ctx->gb, 1);
1249 memcpy(ctx->stored_frame, ctx->frm0, ctx->buf_size);
1250 if ((ret = copy_output(ctx, NULL)))
1252 memcpy(ctx->output->data[1], ctx->pal, 1024);
1254 SANMFrameHeader header;
1256 if ((ret = read_frame_header(ctx, &header)))
1259 ctx->rotate_code = header.rotate_code;
1260 if ((ctx->output->key_frame = !header.seq_num)) {
1261 ctx->output->pict_type = AV_PICTURE_TYPE_I;
1262 fill_frame(ctx->frm1, ctx->npixels, header.bg_color);
1263 fill_frame(ctx->frm2, ctx->npixels, header.bg_color);
1265 ctx->output->pict_type = AV_PICTURE_TYPE_P;
1268 if (header.codec < FF_ARRAY_ELEMS(v1_decoders)) {
1269 if ((ret = v1_decoders[header.codec](ctx))) {
1270 av_log(avctx, AV_LOG_ERROR,
1271 "subcodec %d: error decoding frame\n", header.codec);
1275 av_log_ask_for_sample(avctx, "subcodec %d is not implemented\n",
1277 return AVERROR_PATCHWELCOME;
1280 if ((ret = copy_output(ctx, &header)))
1283 if (ctx->rotate_code)
1284 rotate_bufs(ctx, ctx->rotate_code);
1287 *(AVFrame*)data = *ctx->output;
1292 AVCodec ff_sanm_decoder = {
1294 .type = AVMEDIA_TYPE_VIDEO,
1295 .id = AV_CODEC_ID_SANM,
1296 .priv_data_size = sizeof(SANMVideoContext),
1297 .init = decode_init,
1298 .close = decode_end,
1299 .decode = decode_frame,
1300 .capabilities = CODEC_CAP_DR1,
1301 .long_name = NULL_IF_CONFIG_SMALL("LucasArts SMUSH video"),