]> git.sesse.net Git - ffmpeg/blob - libavcodec/sanm.c
Merge commit '5ff998a233d759d0de83ea6f95c383d03d25d88e'
[ffmpeg] / libavcodec / sanm.c
1 /*
2  * LucasArts Smush video decoder
3  * Copyright (c) 2006 Cyril Zorin
4  * Copyright (c) 2011 Konstantin Shishkov
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 // #define DEBUG 1
24
25 #include "avcodec.h"
26 #include "bytestream.h"
27 #include "libavutil/bswap.h"
28 #include "libavcodec/dsputil.h"
29 #include "sanm_data.h"
30
31 #define NGLYPHS 256
32
33 typedef struct {
34     AVCodecContext *avctx;
35     GetByteContext gb;
36
37     int version, subversion;
38     uint32_t pal[256];
39     int16_t delta_pal[768];
40
41     int pitch;
42     int width, height;
43     int aligned_width, aligned_height;
44     int prev_seq;
45
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;
51
52     uint8_t *rle_buf;
53     unsigned int rle_buf_size;
54
55     int rotate_code;
56
57     long npixels, buf_size;
58
59     uint16_t codebook[256];
60     uint16_t small_codebook[4];
61
62     int8_t p4x4glyphs[NGLYPHS][16];
63     int8_t p8x8glyphs[NGLYPHS][64];
64 } SANMVideoContext;
65
66 typedef struct {
67     int seq_num, codec, rotate_code, rle_output_size;
68
69     uint16_t bg_color;
70     uint32_t width, height;
71 } SANMFrameHeader;
72
73 enum GlyphEdge {
74     LEFT_EDGE,
75     TOP_EDGE,
76     RIGHT_EDGE,
77     BOTTOM_EDGE,
78     NO_EDGE
79 };
80
81 enum GlyphDir {
82     DIR_LEFT,
83     DIR_UP,
84     DIR_RIGHT,
85     DIR_DOWN,
86     NO_DIR
87 };
88
89 /**
90  * Return enum GlyphEdge of box where point (x, y) lies.
91  *
92  * @param x x point coordinate
93  * @param y y point coordinate
94  * @param edge_size box width/height.
95  */
96 static enum GlyphEdge which_edge(int x, int y, int edge_size)
97 {
98     const int edge_max = edge_size - 1;
99
100     if (!y) {
101         return BOTTOM_EDGE;
102     } else if (y == edge_max) {
103         return TOP_EDGE;
104     } else if (!x) {
105         return LEFT_EDGE;
106     } else if (x == edge_max) {
107         return RIGHT_EDGE;
108     } else {
109         return NO_EDGE;
110     }
111 }
112
113 static enum GlyphDir which_direction(enum GlyphEdge edge0, enum GlyphEdge edge1)
114 {
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)) {
119         return DIR_UP;
120     } else if ((edge0 == TOP_EDGE && edge1 != BOTTOM_EDGE) ||
121                (edge1 == TOP_EDGE && edge0 != BOTTOM_EDGE)) {
122         return DIR_DOWN;
123     } else if ((edge0 == LEFT_EDGE && edge1 != RIGHT_EDGE) ||
124                (edge1 == LEFT_EDGE && edge0 != RIGHT_EDGE)) {
125         return DIR_LEFT;
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)) {
130         return DIR_RIGHT;
131     }
132
133     return NO_DIR;
134 }
135
136 /**
137  * Interpolate two points.
138  */
139 static void interp_point(int8_t *points, int x0, int y0, int x1, int y1,
140                          int pos, int npoints)
141 {
142     if (npoints) {
143         points[0] = (x0 * pos + x1 * (npoints - pos) + (npoints >> 1)) / npoints;
144         points[1] = (y0 * pos + y1 * (npoints - pos) + (npoints >> 1)) / npoints;
145     } else {
146         points[0] = x0;
147         points[1] = y0;
148     }
149 }
150
151 /**
152  * Construct glyphs by iterating through vectors coordinates.
153  *
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.
158  */
159 static void make_glyphs(int8_t *pglyphs, const int8_t *xvec, const int8_t *yvec,
160                         const int side_length)
161 {
162     const int glyph_size = side_length * side_length;
163     int8_t *pglyph = pglyphs;
164
165     int i, j;
166     for (i = 0; i < GLYPH_COORD_VECT_SIZE; i++) {
167         int x0    = xvec[i];
168         int y0    = yvec[i];
169         enum GlyphEdge edge0 = which_edge(x0, y0, side_length);
170
171         for (j = 0; j < GLYPH_COORD_VECT_SIZE; j++, pglyph += glyph_size) {
172             int x1      = xvec[j];
173             int y1      = yvec[j];
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));
177             int ipoint;
178
179             for (ipoint = 0; ipoint <= npoints; ipoint++) {
180                 int8_t point[2];
181                 int irow, icol;
182
183                 interp_point(point, x0, y0, x1, y1, ipoint, npoints);
184
185                 switch (dir) {
186                 case DIR_UP:
187                     for (irow = point[1]; irow >= 0; irow--)
188                         pglyph[point[0] + irow * side_length] = 1;
189                     break;
190
191                 case DIR_DOWN:
192                     for (irow = point[1]; irow < side_length; irow++)
193                         pglyph[point[0] + irow * side_length] = 1;
194                     break;
195
196                 case DIR_LEFT:
197                     for (icol = point[0]; icol >= 0; icol--)
198                         pglyph[icol + point[1] * side_length] = 1;
199                     break;
200
201                 case DIR_RIGHT:
202                     for (icol = point[0]; icol < side_length; icol++)
203                         pglyph[icol + point[1] * side_length] = 1;
204                     break;
205                 }
206             }
207         }
208     }
209 }
210
211 static void init_sizes(SANMVideoContext *ctx, int width, int height)
212 {
213     ctx->width   = width;
214     ctx->height  = height;
215     ctx->npixels = width * height;
216
217     ctx->aligned_width  = FFALIGN(width,  8);
218     ctx->aligned_height = FFALIGN(height, 8);
219
220     ctx->buf_size = ctx->aligned_width * ctx->aligned_height * sizeof(ctx->frm0[0]);
221     ctx->pitch    = width;
222 }
223
224 static void destroy_buffers(SANMVideoContext *ctx)
225 {
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);
231 }
232
233 static av_cold int init_buffers(SANMVideoContext *ctx)
234 {
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);
238     if (!ctx->version)
239         av_fast_padded_malloc(&ctx->stored_frame, &ctx->stored_frame_size, ctx->buf_size);
240
241     if (!ctx->frm0 || !ctx->frm1 || !ctx->frm2 || (!ctx->stored_frame && !ctx->version)) {
242         destroy_buffers(ctx);
243         return AVERROR(ENOMEM);
244     }
245
246     return 0;
247 }
248
249 static void rotate_bufs(SANMVideoContext *ctx, int rotate_code)
250 {
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);
255 }
256
257 static av_cold int decode_init(AVCodecContext *avctx)
258 {
259     SANMVideoContext *ctx = avctx->priv_data;
260
261     ctx->avctx     = avctx;
262     ctx->version   = !avctx->extradata_size;
263
264     avctx->pix_fmt = ctx->version ? AV_PIX_FMT_RGB565 : AV_PIX_FMT_PAL8;
265
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);
270     }
271     ctx->output          = &ctx->frame;
272     ctx->output->data[0] = 0;
273
274     make_glyphs(ctx->p4x4glyphs[0], glyph4_x, glyph4_y, 4);
275     make_glyphs(ctx->p8x8glyphs[0], glyph8_x, glyph8_y, 8);
276
277     if (!ctx->version) {
278         int i;
279
280         if (avctx->extradata_size < 1026) {
281             av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");
282             return AVERROR_INVALIDDATA;
283         }
284
285         ctx->subversion = AV_RL16(avctx->extradata);
286         for (i = 0; i < 256; i++)
287             ctx->pal[i] = 0xFFU << 24 | AV_RL32(avctx->extradata + 2 + i * 4);
288     }
289
290     return 0;
291 }
292
293 static av_cold int decode_end(AVCodecContext *avctx)
294 {
295     SANMVideoContext *ctx = avctx->priv_data;
296
297     destroy_buffers(ctx);
298
299     if (ctx->frame.data[0]) {
300         avctx->release_buffer(avctx, &ctx->frame);
301         ctx->frame.data[0] = 0;
302     }
303
304     return 0;
305 }
306
307 static int rle_decode(SANMVideoContext *ctx, uint8_t *dst, const int out_size)
308 {
309     int opcode, color, run_len, left = out_size;
310
311     while (left > 0) {
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;
316
317         if (opcode & 1) {
318             color = bytestream2_get_byte(&ctx->gb);
319             memset(dst, color, run_len);
320         } else {
321             if (bytestream2_get_bytes_left(&ctx->gb) < run_len)
322                 return AVERROR_INVALIDDATA;
323             bytestream2_get_bufferu(&ctx->gb, dst, run_len);
324         }
325
326         dst  += run_len;
327         left -= run_len;
328     }
329
330     return 0;
331 }
332
333 static int old_codec1(SANMVideoContext *ctx, int top,
334                       int left, int width, int height)
335 {
336     uint8_t *dst = ((uint8_t*)ctx->frm0) + left + top * ctx->pitch;
337     int i, j, len, flag, code, val, pos, end;
338
339     for (i = 0; i < height; i++) {
340         pos = 0;
341
342         if (bytestream2_get_bytes_left(&ctx->gb) < 2)
343             return AVERROR_INVALIDDATA;
344
345         len = bytestream2_get_le16u(&ctx->gb);
346         end = bytestream2_tell(&ctx->gb) + len;
347
348         while (bytestream2_tell(&ctx->gb) < end) {
349             if (bytestream2_get_bytes_left(&ctx->gb) < 2)
350                 return AVERROR_INVALIDDATA;
351
352             code = bytestream2_get_byteu(&ctx->gb);
353             flag = code & 1;
354             code = (code >> 1) + 1;
355             if (pos + code > width)
356                 return AVERROR_INVALIDDATA;
357             if (flag) {
358                 val = bytestream2_get_byteu(&ctx->gb);
359                 if (val)
360                     memset(dst + pos, val, code);
361                 pos += code;
362             } else {
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);
367                     if (val)
368                         dst[pos] = val;
369                     pos++;
370                 }
371             }
372         }
373         dst += ctx->pitch;
374     }
375     ctx->rotate_code = 0;
376
377     return 0;
378 }
379
380 static inline void codec37_mv(uint8_t *dst, const uint8_t *src,
381                               int height, int stride, int x, int y)
382 {
383     int pos, i, j;
384
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)
389                 dst[i] = 0;
390             else
391                 dst[i] = src[i];
392         }
393         dst += stride;
394         src += stride;
395         pos += stride;
396     }
397 }
398
399 static int old_codec37(SANMVideoContext *ctx, int top,
400                        int left, int width, int height)
401 {
402     int stride = ctx->pitch;
403     int i, j, k, t;
404     int skip_run = 0;
405     int compr, mvoff, seq, flags;
406     uint32_t decoded_size;
407     uint8_t *dst, *prev;
408
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);
416
417     ctx->rotate_code = 0;
418
419     if (((seq & 1) || !(flags & 1)) && (compr && compr != 2))
420         rotate_bufs(ctx, 1);
421
422     dst  = ((uint8_t*)ctx->frm0) + left + top * stride;
423     prev = ((uint8_t*)ctx->frm2) + left + top * stride;
424
425     if (mvoff > 2) {
426         av_log(ctx->avctx, AV_LOG_ERROR, "invalid motion base value %d\n", mvoff);
427         return AVERROR_INVALIDDATA;
428     }
429     av_dlog(ctx->avctx, "compression %d\n", compr);
430     switch (compr) {
431     case 0:
432         for (i = 0; i < height; i++) {
433             bytestream2_get_buffer(&ctx->gb, dst, width);
434             dst += stride;
435         }
436         memset(ctx->frm1, 0, ctx->height * stride);
437         memset(ctx->frm2, 0, ctx->height * stride);
438         break;
439     case 2:
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);
444         break;
445     case 3:
446     case 4:
447         if (flags & 4) {
448             for (j = 0; j < height; j += 4) {
449                 for (i = 0; i < width; i += 4) {
450                     int code;
451                     if (skip_run) {
452                         skip_run--;
453                         copy_block4(dst + i, prev + i, stride, stride, 4);
454                         continue;
455                     }
456                     if (bytestream2_get_bytes_left(&ctx->gb) < 1)
457                         return AVERROR_INVALIDDATA;
458                     code = bytestream2_get_byteu(&ctx->gb);
459                     switch (code) {
460                     case 0xFF:
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);
465                         break;
466                     case 0xFE:
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);
471                         break;
472                     case 0xFD:
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);
478                         break;
479                     default:
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;
484                             i -= 4;
485                         } else {
486                             int mx, my;
487
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);
492                         }
493                     }
494                 }
495                 dst  += stride * 4;
496                 prev += stride * 4;
497             }
498         } else {
499             for (j = 0; j < height; j += 4) {
500                 for (i = 0; i < width; i += 4) {
501                     int code;
502                     if (skip_run) {
503                         skip_run--;
504                         copy_block4(dst + i, prev + i, stride, stride, 4);
505                         continue;
506                     }
507                     code = bytestream2_get_byte(&ctx->gb);
508                     if (code == 0xFF) {
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;
517                         i -= 4;
518                     } else {
519                         int mx, my;
520
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);
525                     }
526                 }
527                 dst  += stride * 4;
528                 prev += stride * 4;
529             }
530         }
531         break;
532     default:
533         av_log(ctx->avctx, AV_LOG_ERROR,
534                "subcodec 37 compression %d not implemented\n", compr);
535         return AVERROR_PATCHWELCOME;
536     }
537
538     return 0;
539 }
540
541 static int process_block(SANMVideoContext *ctx, uint8_t *dst, uint8_t *prev1,
542                          uint8_t *prev2, int stride, int tbl, int size)
543 {
544     int code, k, t;
545     uint8_t colors[2];
546     int8_t *pglyph;
547
548     if (bytestream2_get_bytes_left(&ctx->gb) < 1)
549         return AVERROR_INVALIDDATA;
550
551     code = bytestream2_get_byteu(&ctx->gb);
552     if (code >= 0xF8) {
553         switch (code) {
554         case 0xFF:
555             if (size == 2) {
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);
562             } else {
563                 size >>= 1;
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,
567                                   stride, tbl, 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,
575                                   stride, tbl, size))
576                     return AVERROR_INVALIDDATA;
577             }
578             break;
579         case 0xFE:
580             if (bytestream2_get_bytes_left(&ctx->gb) < 1)
581                 return AVERROR_INVALIDDATA;
582
583             t = bytestream2_get_byteu(&ctx->gb);
584             for (k = 0; k < size; k++)
585                 memset(dst + k * stride, t, size);
586             break;
587         case 0xFD:
588             if (bytestream2_get_bytes_left(&ctx->gb) < 3)
589                 return AVERROR_INVALIDDATA;
590
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);
594
595             for (k = 0; k < size; k++)
596                 for (t = 0; t < size; t++)
597                     dst[t + k * stride] = colors[!*pglyph++];
598             break;
599         case 0xFC:
600             for (k = 0; k < size; k++)
601                 memcpy(dst + k * stride, prev1 + k * stride, size);
602             break;
603         default:
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);
610         }
611     } else {
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);
616     }
617
618     return 0;
619 }
620
621 static int old_codec47(SANMVideoContext *ctx, int top,
622                        int left, int width, int height)
623 {
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;
630
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);
639
640     if (skip & 1)
641         bytestream2_skip(&ctx->gb, 0x8080);
642     if (!seq) {
643         ctx->prev_seq = -1;
644         memset(prev1, 0, ctx->height * stride);
645         memset(prev2, 0, ctx->height * stride);
646     }
647     av_dlog(ctx->avctx, "compression %d\n", compr);
648     switch (compr) {
649     case 0:
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);
655             dst += stride;
656         }
657         break;
658     case 1:
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);
665             }
666             dst += stride * 2;
667         }
668         break;
669     case 2:
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,
674                                       tbl_pos + 8, 8))
675                         return AVERROR_INVALIDDATA;
676                 }
677                 dst   += stride * 8;
678                 prev1 += stride * 8;
679                 prev2 += stride * 8;
680             }
681         }
682         break;
683     case 3:
684         memcpy(ctx->frm0, ctx->frm2, ctx->pitch * ctx->height);
685         break;
686     case 4:
687         memcpy(ctx->frm0, ctx->frm1, ctx->pitch * ctx->height);
688         break;
689     case 5:
690         if (rle_decode(ctx, dst, decoded_size))
691             return AVERROR_INVALIDDATA;
692         break;
693     default:
694         av_log(ctx->avctx, AV_LOG_ERROR,
695                "subcodec 47 compression %d not implemented\n", compr);
696         return AVERROR_PATCHWELCOME;
697     }
698     if (seq == ctx->prev_seq + 1)
699         ctx->rotate_code = new_rot;
700     else
701         ctx->rotate_code = 0;
702     ctx->prev_seq = seq;
703
704     return 0;
705 }
706
707 static int process_frame_obj(SANMVideoContext *ctx)
708 {
709     uint16_t codec, top, left, w, h;
710
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);
716
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);
724         }
725     }
726     bytestream2_skip(&ctx->gb, 4);
727
728     av_dlog(ctx->avctx, "subcodec %d\n", codec);
729     switch (codec) {
730     case 1:
731     case 3:
732         return old_codec1(ctx, top, left, w, h);
733         break;
734     case 37:
735         return old_codec37(ctx, top, left, w, h);
736         break;
737     case 47:
738         return old_codec47(ctx, top, left, w, h);
739         break;
740     default:
741         av_log_ask_for_sample(ctx->avctx, "unknown subcodec %d\n", codec);
742         return AVERROR_PATCHWELCOME;
743     }
744 }
745
746 static int decode_0(SANMVideoContext *ctx)
747 {
748     uint16_t *frm = ctx->frm0;
749     int x, y;
750
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;
754     }
755     for (y = 0; y < ctx->height; y++) {
756         for (x = 0; x < ctx->width; x++)
757             frm[x] = bytestream2_get_le16u(&ctx->gb);
758         frm += ctx->pitch;
759     }
760     return 0;
761 }
762
763 static int decode_nop(SANMVideoContext *ctx)
764 {
765     av_log_ask_for_sample(ctx->avctx, "unknown/unsupported compression type\n");
766     return AVERROR_PATCHWELCOME;
767 }
768
769 static void copy_block(uint16_t *pdest, uint16_t *psrc, int block_size, int pitch)
770 {
771     uint8_t *dst = (uint8_t *)pdest;
772     uint8_t *src = (uint8_t *)psrc;
773     int stride = pitch * 2;
774
775     switch (block_size) {
776     case 2:
777         copy_block4(dst, src, stride, stride, 2);
778         break;
779     case 4:
780         copy_block8(dst, src, stride, stride, 4);
781         break;
782     case 8:
783         copy_block16(dst, src, stride, stride, 8);
784         break;
785     }
786 }
787
788 static void fill_block(uint16_t *pdest, uint16_t color, int block_size, int pitch)
789 {
790     int x, y;
791
792     pitch -= block_size;
793     for (y = 0; y < block_size; y++, pdest += pitch)
794         for (x = 0; x < block_size; x++)
795             *pdest++ = color;
796 }
797
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)
800 {
801     int8_t *pglyph;
802     uint16_t colors[2] = { fg_color, bg_color };
803     int x, y;
804
805     if (index >= NGLYPHS) {
806         av_log(ctx->avctx, AV_LOG_ERROR, "ignoring nonexistent glyph #%u\n", index);
807         return AVERROR_INVALIDDATA;
808     }
809
810     pglyph = block_size == 8 ? ctx->p8x8glyphs[index] : ctx->p4x4glyphs[index];
811     pitch -= block_size;
812
813     for (y = 0; y < block_size; y++, dst += pitch)
814         for (x = 0; x < block_size; x++)
815             *dst++ = colors[*pglyph++];
816     return 0;
817 }
818
819 static int opcode_0xf7(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
820 {
821     uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
822
823     if (block_size == 2) {
824         uint32_t indices;
825
826         if (bytestream2_get_bytes_left(&ctx->gb) < 4)
827             return AVERROR_INVALIDDATA;
828
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];
834     } else {
835         uint16_t fgcolor, bgcolor;
836         int glyph;
837
838         if (bytestream2_get_bytes_left(&ctx->gb) < 3)
839             return AVERROR_INVALIDDATA;
840
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)];
844
845         draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
846     }
847     return 0;
848 }
849
850 static int opcode_0xf8(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
851 {
852     uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
853
854     if (block_size == 2) {
855         if (bytestream2_get_bytes_left(&ctx->gb) < 8)
856             return AVERROR_INVALIDDATA;
857
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);
862     } else {
863         uint16_t fgcolor, bgcolor;
864         int glyph;
865
866         if (bytestream2_get_bytes_left(&ctx->gb) < 5)
867             return AVERROR_INVALIDDATA;
868
869         glyph   = bytestream2_get_byteu(&ctx->gb);
870         bgcolor = bytestream2_get_le16u(&ctx->gb);
871         fgcolor = bytestream2_get_le16u(&ctx->gb);
872
873         draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
874     }
875     return 0;
876 }
877
878 static int good_mvec(SANMVideoContext *ctx, int cx, int cy, int mx, int my,
879                      int block_size)
880 {
881     int start_pos = cx + mx + (cy + my) * ctx->pitch;
882     int end_pos = start_pos + (block_size - 1) * (ctx->pitch + 1);
883
884     int good = start_pos >= 0 && end_pos < (ctx->buf_size >> 1);
885
886     if (!good) {
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);
889     }
890
891     return good;
892 }
893
894 static int codec2subblock(SANMVideoContext *ctx, int cx, int cy, int blk_size)
895 {
896     int16_t mx, my, index;
897     int opcode;
898
899     if (bytestream2_get_bytes_left(&ctx->gb) < 1)
900         return AVERROR_INVALIDDATA;
901
902     opcode = bytestream2_get_byteu(&ctx->gb);
903
904     av_dlog(ctx->avctx, "opcode 0x%0X cx %d cy %d blk %d\n", opcode, cx, cy, blk_size);
905     switch (opcode) {
906     default:
907         mx = motion_vectors[opcode][0];
908         my = motion_vectors[opcode][1];
909
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);
914         }
915         break;
916     case 0xF5:
917         if (bytestream2_get_bytes_left(&ctx->gb) < 2)
918             return AVERROR_INVALIDDATA;
919         index = bytestream2_get_le16u(&ctx->gb);
920
921         mx = index % ctx->width;
922         my = index / ctx->width;
923
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);
928         }
929         break;
930     case 0xF6:
931         copy_block(ctx->frm0 + cx + ctx->pitch * cy,
932                    ctx->frm1 + cx + ctx->pitch * cy,
933                    blk_size, ctx->pitch);
934         break;
935     case 0xF7:
936         opcode_0xf7(ctx, cx, cy, blk_size, ctx->pitch);
937         break;
938
939     case 0xF8:
940         opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
941         break;
942     case 0xF9:
943     case 0xFA:
944     case 0xFB:
945     case 0xFC:
946         fill_block(ctx->frm0 + cx + cy * ctx->pitch,
947                    ctx->small_codebook[opcode - 0xf9], blk_size, ctx->pitch);
948         break;
949     case 0xFD:
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);
954         break;
955     case 0xFE:
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);
960         break;
961     case 0xFF:
962         if (blk_size == 2) {
963             opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
964         } else {
965             blk_size >>= 1;
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;
974         }
975         break;
976     }
977     return 0;
978 }
979
980 static int decode_2(SANMVideoContext *ctx)
981 {
982     int cx, cy, ret;
983
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))
987                 return ret;
988         }
989     }
990
991     return 0;
992 }
993
994 static int decode_3(SANMVideoContext *ctx)
995 {
996     memcpy(ctx->frm0, ctx->frm2, ctx->frm2_size);
997     return 0;
998 }
999
1000 static int decode_4(SANMVideoContext *ctx)
1001 {
1002     memcpy(ctx->frm0, ctx->frm1, ctx->frm1_size);
1003     return 0;
1004 }
1005
1006 static int decode_5(SANMVideoContext *ctx)
1007 {
1008 #if HAVE_BIGENDIAN
1009     uint16_t *frm;
1010     int npixels;
1011 #endif
1012     uint8_t *dst = (uint8_t*)ctx->frm0;
1013
1014     if (rle_decode(ctx, dst, ctx->buf_size))
1015         return AVERROR_INVALIDDATA;
1016
1017 #if HAVE_BIGENDIAN
1018     npixels = ctx->npixels;
1019     frm = ctx->frm0;
1020     while (npixels--)
1021         *frm++ = av_bswap16(*frm);
1022 #endif
1023
1024     return 0;
1025 }
1026
1027 static int decode_6(SANMVideoContext *ctx)
1028 {
1029     int npixels = ctx->npixels;
1030     uint16_t *frm = ctx->frm0;
1031
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;
1035     }
1036     while (npixels--)
1037         *frm++ = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
1038
1039     return 0;
1040 }
1041
1042 static int decode_8(SANMVideoContext *ctx)
1043 {
1044     uint16_t *pdest = ctx->frm0;
1045     uint8_t *rsrc;
1046     long npixels = ctx->npixels;
1047
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);
1052     }
1053     rsrc = ctx->rle_buf;
1054
1055     if (rle_decode(ctx, rsrc, npixels))
1056         return AVERROR_INVALIDDATA;
1057
1058     while (npixels--)
1059         *pdest++ = ctx->codebook[*rsrc++];
1060
1061     return 0;
1062 }
1063
1064 typedef int (*frm_decoder)(SANMVideoContext *ctx);
1065
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
1069 };
1070
1071 static int read_frame_header(SANMVideoContext *ctx, SANMFrameHeader *hdr)
1072 {
1073     int i, ret;
1074
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",
1077                ret);
1078         return AVERROR_INVALIDDATA;
1079     }
1080     bytestream2_skip(&ctx->gb, 8); // skip pad
1081
1082     hdr->width  = bytestream2_get_le32u(&ctx->gb);
1083     hdr->height = bytestream2_get_le32u(&ctx->gb);
1084
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;
1088     }
1089
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);
1093
1094     bytestream2_skip(&ctx->gb, 4); // skip pad
1095
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);
1099
1100     bytestream2_skip(&ctx->gb, 2); // skip pad
1101
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);
1105
1106     bytestream2_skip(&ctx->gb, 8); // skip pad
1107
1108     av_dlog(ctx->avctx, "subcodec %d\n", hdr->codec);
1109     return 0;
1110 }
1111
1112 static void fill_frame(uint16_t *pbuf, int buf_size, uint16_t color)
1113 {
1114     while (buf_size--)
1115         *pbuf++ = color;
1116 }
1117
1118 static int copy_output(SANMVideoContext *ctx, SANMFrameHeader *hdr)
1119 {
1120     uint8_t *dst;
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);
1124
1125     if ((ret = ctx->avctx->get_buffer(ctx->avctx, ctx->output)) < 0) {
1126         av_log(ctx->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1127         return ret;
1128     }
1129
1130     dst      = ctx->output->data[0];
1131     dstpitch = ctx->output->linesize[0];
1132
1133     while (height--) {
1134         memcpy(dst, src, srcpitch);
1135         src += srcpitch;
1136         dst += dstpitch;
1137     }
1138
1139     return 0;
1140 }
1141
1142 static int decode_frame(AVCodecContext *avctx, void *data,
1143                         int *got_frame_ptr, AVPacket *pkt)
1144 {
1145     SANMVideoContext *ctx = avctx->priv_data;
1146     int i, ret;
1147
1148     bytestream2_init(&ctx->gb, pkt->data, pkt->size);
1149     if (ctx->output->data[0])
1150         avctx->release_buffer(avctx, ctx->output);
1151
1152     if (!ctx->version) {
1153         int to_store = 0;
1154
1155         while (bytestream2_get_bytes_left(&ctx->gb) >= 8) {
1156             uint32_t sig, size;
1157             int pos;
1158
1159             sig  = bytestream2_get_be32u(&ctx->gb);
1160             size = bytestream2_get_be32u(&ctx->gb);
1161             pos  = bytestream2_tell(&ctx->gb);
1162
1163             if (bytestream2_get_bytes_left(&ctx->gb) < size) {
1164                 av_log(avctx, AV_LOG_ERROR, "incorrect chunk size %d\n", size);
1165                 break;
1166             }
1167             switch (sig) {
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",
1171                            size);
1172                     return AVERROR_INVALIDDATA;
1173                 }
1174                 for (i = 0; i < 256; i++)
1175                     ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
1176                 break;
1177             case MKBETAG('F', 'O', 'B', 'J'):
1178                 if (size < 16)
1179                     return AVERROR_INVALIDDATA;
1180                 if (ret = process_frame_obj(ctx))
1181                     return ret;
1182                 break;
1183             case MKBETAG('X', 'P', 'A', 'L'):
1184                 if (size == 6 || size == 4) {
1185                     uint8_t tmp[3];
1186                     int j;
1187
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);
1192                         }
1193                         ctx->pal[i] = 0xFFU << 24 | AV_RB24(tmp);
1194                     }
1195                 } else {
1196                     if (size < 768 * 2 + 4) {
1197                         av_log(avctx, AV_LOG_ERROR, "incorrect palette change block size %d\n",
1198                                size);
1199                         return AVERROR_INVALIDDATA;
1200                     }
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] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
1207                     } else {
1208                         memset(ctx->pal, 0, sizeof(ctx->pal));
1209                     }
1210                 }
1211                 break;
1212             case MKBETAG('S', 'T', 'O', 'R'):
1213                 to_store = 1;
1214                 break;
1215             case MKBETAG('F', 'T', 'C', 'H'):
1216                 memcpy(ctx->frm0, ctx->stored_frame, ctx->buf_size);
1217                 break;
1218             default:
1219                 bytestream2_skip(&ctx->gb, size);
1220                 av_log(avctx, AV_LOG_DEBUG, "unknown/unsupported chunk %x\n", sig);
1221                 break;
1222             }
1223
1224             bytestream2_seek(&ctx->gb, pos + size, SEEK_SET);
1225             if (size & 1)
1226                 bytestream2_skip(&ctx->gb, 1);
1227         }
1228         if (to_store)
1229             memcpy(ctx->stored_frame, ctx->frm0, ctx->buf_size);
1230         if ((ret = copy_output(ctx, NULL)))
1231             return ret;
1232         memcpy(ctx->output->data[1], ctx->pal, 1024);
1233     } else {
1234         SANMFrameHeader header;
1235
1236         if ((ret = read_frame_header(ctx, &header)))
1237             return ret;
1238
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);
1244         } else {
1245             ctx->output->pict_type = AV_PICTURE_TYPE_P;
1246         }
1247
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);
1252                 return ret;
1253             }
1254         } else {
1255             av_log_ask_for_sample(avctx, "subcodec %d is not implemented\n",
1256                    header.codec);
1257             return AVERROR_PATCHWELCOME;
1258         }
1259
1260         if ((ret = copy_output(ctx, &header)))
1261             return ret;
1262     }
1263     if (ctx->rotate_code)
1264         rotate_bufs(ctx, ctx->rotate_code);
1265
1266     *got_frame_ptr  = 1;
1267     *(AVFrame*)data = *ctx->output;
1268
1269     return pkt->size;
1270 }
1271
1272 AVCodec ff_sanm_decoder = {
1273     .name           = "sanm",
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"),
1282 };