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