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