]> git.sesse.net Git - ffmpeg/blob - libavcodec/agm.c
avcodec: Constify AVCodecs
[ffmpeg] / libavcodec / agm.c
1 /*
2  * Amuse Graphics Movie decoder
3  *
4  * Copyright (c) 2018 Paul B Mahol
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 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #define BITSTREAM_READER_LE
28
29 #include "libavutil/mem_internal.h"
30
31 #include "avcodec.h"
32 #include "bytestream.h"
33 #include "copy_block.h"
34 #include "get_bits.h"
35 #include "idctdsp.h"
36 #include "internal.h"
37
38 static const uint8_t unscaled_luma[64] = {
39     16, 11, 10, 16, 24, 40, 51, 61, 12, 12, 14, 19,
40     26, 58, 60, 55, 14, 13, 16, 24, 40, 57, 69, 56,
41     14, 17, 22, 29, 51, 87, 80, 62, 18, 22, 37, 56,
42     68,109,103, 77, 24, 35, 55, 64, 81,104,113, 92,
43     49, 64, 78, 87,103,121,120,101, 72, 92, 95, 98,
44     112,100,103,99
45 };
46
47 static const uint8_t unscaled_chroma[64] = {
48     17, 18, 24, 47, 99, 99, 99, 99, 18, 21, 26, 66,
49     99, 99, 99, 99, 24, 26, 56, 99, 99, 99, 99, 99,
50     47, 66, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
51     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
52     99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
53     99, 99, 99, 99
54 };
55
56 typedef struct MotionVector {
57     int16_t x, y;
58 } MotionVector;
59
60 typedef struct AGMContext {
61     const AVClass  *class;
62     AVCodecContext *avctx;
63     GetBitContext   gb;
64     GetByteContext  gbyte;
65
66     int key_frame;
67     int bitstream_size;
68     int compression;
69     int blocks_w;
70     int blocks_h;
71     int size[3];
72     int plus;
73     int dct;
74     int rgb;
75     unsigned flags;
76     unsigned fflags;
77
78     uint8_t *output;
79     unsigned padded_output_size;
80     unsigned output_size;
81
82     MotionVector *mvectors;
83     unsigned      mvectors_size;
84
85     VLC vlc;
86
87     AVFrame *prev_frame;
88
89     int luma_quant_matrix[64];
90     int chroma_quant_matrix[64];
91
92     ScanTable scantable;
93     DECLARE_ALIGNED(32, int16_t, block)[64];
94
95     int16_t *wblocks;
96     unsigned wblocks_size;
97
98     int      *map;
99     unsigned  map_size;
100
101     IDCTDSPContext idsp;
102 } AGMContext;
103
104 static int read_code(GetBitContext *gb, int *oskip, int *level, int *map, int mode)
105 {
106     int len = 0, skip = 0, max;
107
108     if (get_bits_left(gb) < 2)
109         return AVERROR_INVALIDDATA;
110
111     if (show_bits(gb, 2)) {
112         switch (show_bits(gb, 4)) {
113         case 1:
114         case 9:
115             len = 1;
116             skip = 3;
117             break;
118         case 2:
119             len = 3;
120             skip = 4;
121             break;
122         case 3:
123             len = 7;
124             skip = 4;
125             break;
126         case 5:
127         case 13:
128             len = 2;
129             skip = 3;
130             break;
131         case 6:
132             len = 4;
133             skip = 4;
134             break;
135         case 7:
136             len = 8;
137             skip = 4;
138             break;
139         case 10:
140             len = 5;
141             skip = 4;
142             break;
143         case 11:
144             len = 9;
145             skip = 4;
146             break;
147         case 14:
148             len = 6;
149             skip = 4;
150             break;
151         case 15:
152             len = ((show_bits(gb, 5) & 0x10) | 0xA0) >> 4;
153             skip = 5;
154             break;
155         default:
156             return AVERROR_INVALIDDATA;
157         }
158
159         skip_bits(gb, skip);
160         *level = get_bits(gb, len);
161         *map = 1;
162         *oskip = 0;
163         max = 1 << (len - 1);
164         if (*level < max)
165             *level = -(max + *level);
166     } else if (show_bits(gb, 3) & 4) {
167         skip_bits(gb, 3);
168         if (mode == 1) {
169             if (show_bits(gb, 4)) {
170                 if (show_bits(gb, 4) == 1) {
171                     skip_bits(gb, 4);
172                     *oskip = get_bits(gb, 16);
173                 } else {
174                     *oskip = get_bits(gb, 4);
175                 }
176             } else {
177                 skip_bits(gb, 4);
178                 *oskip = get_bits(gb, 10);
179             }
180         } else if (mode == 0) {
181             *oskip = get_bits(gb, 10);
182         }
183         *level = 0;
184     } else {
185         skip_bits(gb, 3);
186         if (mode == 0)
187             *oskip = get_bits(gb, 4);
188         else if (mode == 1)
189             *oskip = 0;
190         *level = 0;
191     }
192
193     return 0;
194 }
195
196 static int decode_intra_blocks(AGMContext *s, GetBitContext *gb,
197                                const int *quant_matrix, int *skip, int *dc_level)
198 {
199     const uint8_t *scantable = s->scantable.permutated;
200     int level, ret, map = 0;
201
202     memset(s->wblocks, 0, s->wblocks_size);
203
204     for (int i = 0; i < 64; i++) {
205         int16_t *block = s->wblocks + scantable[i];
206
207         for (int j = 0; j < s->blocks_w;) {
208             if (*skip > 0) {
209                 int rskip;
210
211                 rskip = FFMIN(*skip, s->blocks_w - j);
212                 j += rskip;
213                 if (i == 0) {
214                     for (int k = 0; k < rskip; k++)
215                         block[64 * k] = *dc_level * quant_matrix[0];
216                 }
217                 block += rskip * 64;
218                 *skip -= rskip;
219             } else {
220                 ret = read_code(gb, skip, &level, &map, s->flags & 1);
221                 if (ret < 0)
222                     return ret;
223
224                 if (i == 0)
225                     *dc_level += level;
226
227                 block[0] = (i == 0 ? *dc_level : level) * quant_matrix[i];
228                 block += 64;
229                 j++;
230             }
231         }
232     }
233
234     return 0;
235 }
236
237 static int decode_inter_blocks(AGMContext *s, GetBitContext *gb,
238                                const int *quant_matrix, int *skip,
239                                int *map)
240 {
241     const uint8_t *scantable = s->scantable.permutated;
242     int level, ret;
243
244     memset(s->wblocks, 0, s->wblocks_size);
245     memset(s->map, 0, s->map_size);
246
247     for (int i = 0; i < 64; i++) {
248         int16_t *block = s->wblocks + scantable[i];
249
250         for (int j = 0; j < s->blocks_w;) {
251             if (*skip > 0) {
252                 int rskip;
253
254                 rskip = FFMIN(*skip, s->blocks_w - j);
255                 j += rskip;
256                 block += rskip * 64;
257                 *skip -= rskip;
258             } else {
259                 ret = read_code(gb, skip, &level, &map[j], s->flags & 1);
260                 if (ret < 0)
261                     return ret;
262
263                 block[0] = level * quant_matrix[i];
264                 block += 64;
265                 j++;
266             }
267         }
268     }
269
270     return 0;
271 }
272
273 static int decode_intra_block(AGMContext *s, GetBitContext *gb,
274                               const int *quant_matrix, int *skip, int *dc_level)
275 {
276     const uint8_t *scantable = s->scantable.permutated;
277     const int offset = s->plus ? 0 : 1024;
278     int16_t *block = s->block;
279     int level, ret, map = 0;
280
281     memset(block, 0, sizeof(s->block));
282
283     if (*skip > 0) {
284         (*skip)--;
285     } else {
286         ret = read_code(gb, skip, &level, &map, s->flags & 1);
287         if (ret < 0)
288             return ret;
289         *dc_level += level;
290     }
291     block[scantable[0]] = offset + *dc_level * quant_matrix[0];
292
293     for (int i = 1; i < 64;) {
294         if (*skip > 0) {
295             int rskip;
296
297             rskip = FFMIN(*skip, 64 - i);
298             i += rskip;
299             *skip -= rskip;
300         } else {
301             ret = read_code(gb, skip, &level, &map, s->flags & 1);
302             if (ret < 0)
303                 return ret;
304
305             block[scantable[i]] = level * quant_matrix[i];
306             i++;
307         }
308     }
309
310     return 0;
311 }
312
313 static int decode_intra_plane(AGMContext *s, GetBitContext *gb, int size,
314                               const int *quant_matrix, AVFrame *frame,
315                               int plane)
316 {
317     int ret, skip = 0, dc_level = 0;
318     const int offset = s->plus ? 0 : 1024;
319
320     if ((ret = init_get_bits8(gb, s->gbyte.buffer, size)) < 0)
321         return ret;
322
323     if (s->flags & 1) {
324         av_fast_padded_malloc(&s->wblocks, &s->wblocks_size,
325                               64 * s->blocks_w * sizeof(*s->wblocks));
326         if (!s->wblocks)
327             return AVERROR(ENOMEM);
328
329         for (int y = 0; y < s->blocks_h; y++) {
330             ret = decode_intra_blocks(s, gb, quant_matrix, &skip, &dc_level);
331             if (ret < 0)
332                 return ret;
333
334             for (int x = 0; x < s->blocks_w; x++) {
335                 s->wblocks[64 * x] += offset;
336                 s->idsp.idct_put(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
337                                  frame->linesize[plane], s->wblocks + 64 * x);
338             }
339         }
340     } else {
341         for (int y = 0; y < s->blocks_h; y++) {
342             for (int x = 0; x < s->blocks_w; x++) {
343                 ret = decode_intra_block(s, gb, quant_matrix, &skip, &dc_level);
344                 if (ret < 0)
345                     return ret;
346
347                 s->idsp.idct_put(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
348                                  frame->linesize[plane], s->block);
349             }
350         }
351     }
352
353     align_get_bits(gb);
354     if (get_bits_left(gb) < 0)
355         av_log(s->avctx, AV_LOG_WARNING, "overread\n");
356     if (get_bits_left(gb) > 0)
357         av_log(s->avctx, AV_LOG_WARNING, "underread: %d\n", get_bits_left(gb));
358
359     return 0;
360 }
361
362 static int decode_inter_block(AGMContext *s, GetBitContext *gb,
363                               const int *quant_matrix, int *skip,
364                               int *map)
365 {
366     const uint8_t *scantable = s->scantable.permutated;
367     int16_t *block = s->block;
368     int level, ret;
369
370     memset(block, 0, sizeof(s->block));
371
372     for (int i = 0; i < 64;) {
373         if (*skip > 0) {
374             int rskip;
375
376             rskip = FFMIN(*skip, 64 - i);
377             i += rskip;
378             *skip -= rskip;
379         } else {
380             ret = read_code(gb, skip, &level, map, s->flags & 1);
381             if (ret < 0)
382                 return ret;
383
384             block[scantable[i]] = level * quant_matrix[i];
385             i++;
386         }
387     }
388
389     return 0;
390 }
391
392 static int decode_inter_plane(AGMContext *s, GetBitContext *gb, int size,
393                               const int *quant_matrix, AVFrame *frame,
394                               AVFrame *prev, int plane)
395 {
396     int ret, skip = 0;
397
398     if ((ret = init_get_bits8(gb, s->gbyte.buffer, size)) < 0)
399         return ret;
400
401     if (s->flags == 3) {
402         av_fast_padded_malloc(&s->wblocks, &s->wblocks_size,
403                               64 * s->blocks_w * sizeof(*s->wblocks));
404         if (!s->wblocks)
405             return AVERROR(ENOMEM);
406
407         av_fast_padded_malloc(&s->map, &s->map_size,
408                               s->blocks_w * sizeof(*s->map));
409         if (!s->map)
410             return AVERROR(ENOMEM);
411
412         for (int y = 0; y < s->blocks_h; y++) {
413             ret = decode_inter_blocks(s, gb, quant_matrix, &skip, s->map);
414             if (ret < 0)
415                 return ret;
416
417             for (int x = 0; x < s->blocks_w; x++) {
418                 int shift = plane == 0;
419                 int mvpos = (y >> shift) * (s->blocks_w >> shift) + (x >> shift);
420                 int orig_mv_x = s->mvectors[mvpos].x;
421                 int mv_x = s->mvectors[mvpos].x / (1 + !shift);
422                 int mv_y = s->mvectors[mvpos].y / (1 + !shift);
423                 int h = s->avctx->coded_height >> !shift;
424                 int w = s->avctx->coded_width  >> !shift;
425                 int map = s->map[x];
426
427                 if (orig_mv_x >= -32) {
428                     if (y * 8 + mv_y < 0 || y * 8 + mv_y + 8 > h ||
429                         x * 8 + mv_x < 0 || x * 8 + mv_x + 8 > w)
430                         return AVERROR_INVALIDDATA;
431
432                     copy_block8(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
433                                 prev->data[plane] + ((s->blocks_h - 1 - y) * 8 - mv_y) * prev->linesize[plane] + (x * 8 + mv_x),
434                                 frame->linesize[plane], prev->linesize[plane], 8);
435                     if (map) {
436                         s->idsp.idct(s->wblocks + x * 64);
437                         for (int i = 0; i < 64; i++)
438                             s->wblocks[i + x * 64] = (s->wblocks[i + x * 64] + 1) & 0xFFFC;
439                         s->idsp.add_pixels_clamped(&s->wblocks[x*64], frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
440                                                    frame->linesize[plane]);
441                     }
442                 } else if (map) {
443                     s->idsp.idct_put(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
444                                      frame->linesize[plane], s->wblocks + x * 64);
445                 }
446             }
447         }
448     } else if (s->flags & 2) {
449         for (int y = 0; y < s->blocks_h; y++) {
450             for (int x = 0; x < s->blocks_w; x++) {
451                 int shift = plane == 0;
452                 int mvpos = (y >> shift) * (s->blocks_w >> shift) + (x >> shift);
453                 int orig_mv_x = s->mvectors[mvpos].x;
454                 int mv_x = s->mvectors[mvpos].x / (1 + !shift);
455                 int mv_y = s->mvectors[mvpos].y / (1 + !shift);
456                 int h = s->avctx->coded_height >> !shift;
457                 int w = s->avctx->coded_width  >> !shift;
458                 int map = 0;
459
460                 ret = decode_inter_block(s, gb, quant_matrix, &skip, &map);
461                 if (ret < 0)
462                     return ret;
463
464                 if (orig_mv_x >= -32) {
465                     if (y * 8 + mv_y < 0 || y * 8 + mv_y + 8 > h ||
466                         x * 8 + mv_x < 0 || x * 8 + mv_x + 8 > w)
467                         return AVERROR_INVALIDDATA;
468
469                     copy_block8(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
470                                 prev->data[plane] + ((s->blocks_h - 1 - y) * 8 - mv_y) * prev->linesize[plane] + (x * 8 + mv_x),
471                                 frame->linesize[plane], prev->linesize[plane], 8);
472                     if (map) {
473                         s->idsp.idct(s->block);
474                         for (int i = 0; i < 64; i++)
475                             s->block[i] = (s->block[i] + 1) & 0xFFFC;
476                         s->idsp.add_pixels_clamped(s->block, frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
477                                                    frame->linesize[plane]);
478                     }
479                 } else if (map) {
480                     s->idsp.idct_put(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
481                                      frame->linesize[plane], s->block);
482                 }
483             }
484         }
485     } else if (s->flags & 1) {
486         av_fast_padded_malloc(&s->wblocks, &s->wblocks_size,
487                               64 * s->blocks_w * sizeof(*s->wblocks));
488         if (!s->wblocks)
489             return AVERROR(ENOMEM);
490
491         av_fast_padded_malloc(&s->map, &s->map_size,
492                               s->blocks_w * sizeof(*s->map));
493         if (!s->map)
494             return AVERROR(ENOMEM);
495
496         for (int y = 0; y < s->blocks_h; y++) {
497             ret = decode_inter_blocks(s, gb, quant_matrix, &skip, s->map);
498             if (ret < 0)
499                 return ret;
500
501             for (int x = 0; x < s->blocks_w; x++) {
502                 if (!s->map[x])
503                     continue;
504                 s->idsp.idct_add(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
505                                  frame->linesize[plane], s->wblocks + 64 * x);
506             }
507         }
508     } else {
509         for (int y = 0; y < s->blocks_h; y++) {
510             for (int x = 0; x < s->blocks_w; x++) {
511                 int map = 0;
512
513                 ret = decode_inter_block(s, gb, quant_matrix, &skip, &map);
514                 if (ret < 0)
515                     return ret;
516
517                 if (!map)
518                     continue;
519                 s->idsp.idct_add(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
520                                  frame->linesize[plane], s->block);
521             }
522         }
523     }
524
525     align_get_bits(gb);
526     if (get_bits_left(gb) < 0)
527         av_log(s->avctx, AV_LOG_WARNING, "overread\n");
528     if (get_bits_left(gb) > 0)
529         av_log(s->avctx, AV_LOG_WARNING, "underread: %d\n", get_bits_left(gb));
530
531     return 0;
532 }
533
534 static void compute_quant_matrix(AGMContext *s, double qscale)
535 {
536     int luma[64], chroma[64];
537     double f = 1.0 - fabs(qscale);
538
539     if (!s->key_frame && (s->flags & 2)) {
540         if (qscale >= 0.0) {
541             for (int i = 0; i < 64; i++) {
542                 luma[i]   = FFMAX(1, 16 * f);
543                 chroma[i] = FFMAX(1, 16 * f);
544             }
545         } else {
546             for (int i = 0; i < 64; i++) {
547                 luma[i]   = FFMAX(1, 16 - qscale * 32);
548                 chroma[i] = FFMAX(1, 16 - qscale * 32);
549             }
550         }
551     } else {
552         if (qscale >= 0.0) {
553             for (int i = 0; i < 64; i++) {
554                 luma[i]   = FFMAX(1, unscaled_luma  [(i & 7) * 8 + (i >> 3)] * f);
555                 chroma[i] = FFMAX(1, unscaled_chroma[(i & 7) * 8 + (i >> 3)] * f);
556             }
557         } else {
558             for (int i = 0; i < 64; i++) {
559                 luma[i]   = FFMAX(1, 255.0 - (255 - unscaled_luma  [(i & 7) * 8 + (i >> 3)]) * f);
560                 chroma[i] = FFMAX(1, 255.0 - (255 - unscaled_chroma[(i & 7) * 8 + (i >> 3)]) * f);
561             }
562         }
563     }
564
565     for (int i = 0; i < 64; i++) {
566         int pos = ff_zigzag_direct[i];
567
568         s->luma_quant_matrix[i]   = luma[pos]   * ((pos / 8) & 1 ? -1 : 1);
569         s->chroma_quant_matrix[i] = chroma[pos] * ((pos / 8) & 1 ? -1 : 1);
570     }
571 }
572
573 static int decode_raw_intra_rgb(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
574 {
575     uint8_t *dst = frame->data[0] + (avctx->height - 1) * frame->linesize[0];
576     uint8_t r = 0, g = 0, b = 0;
577
578     if (bytestream2_get_bytes_left(gbyte) < 3 * avctx->width * avctx->height)
579         return AVERROR_INVALIDDATA;
580
581     for (int y = 0; y < avctx->height; y++) {
582         for (int x = 0; x < avctx->width; x++) {
583             dst[x*3+0] = bytestream2_get_byteu(gbyte) + r;
584             r = dst[x*3+0];
585             dst[x*3+1] = bytestream2_get_byteu(gbyte) + g;
586             g = dst[x*3+1];
587             dst[x*3+2] = bytestream2_get_byteu(gbyte) + b;
588             b = dst[x*3+2];
589         }
590         dst -= frame->linesize[0];
591     }
592
593     return 0;
594 }
595
596 av_always_inline static int fill_pixels(uint8_t **y0, uint8_t **y1,
597                        uint8_t **u, uint8_t **v,
598                        int ylinesize, int ulinesize, int vlinesize,
599                        uint8_t *fill,
600                        int *nx, int *ny, int *np, int w, int h)
601 {
602     uint8_t *y0dst = *y0;
603     uint8_t *y1dst = *y1;
604     uint8_t *udst = *u;
605     uint8_t *vdst = *v;
606     int x = *nx, y = *ny, pos = *np;
607
608     if (pos == 0) {
609         y0dst[2*x+0] += fill[0];
610         y0dst[2*x+1] += fill[1];
611         y1dst[2*x+0] += fill[2];
612         y1dst[2*x+1] += fill[3];
613         pos++;
614     } else if (pos == 1) {
615         udst[x] += fill[0];
616         vdst[x] += fill[1];
617         x++;
618         if (x >= w) {
619             x = 0;
620             y++;
621             if (y >= h)
622                 return 1;
623             y0dst -= 2*ylinesize;
624             y1dst -= 2*ylinesize;
625             udst  -=   ulinesize;
626             vdst  -=   vlinesize;
627         }
628         y0dst[2*x+0] += fill[2];
629         y0dst[2*x+1] += fill[3];
630         pos++;
631     } else if (pos == 2) {
632         y1dst[2*x+0] += fill[0];
633         y1dst[2*x+1] += fill[1];
634         udst[x]      += fill[2];
635         vdst[x]      += fill[3];
636         x++;
637         if (x >= w) {
638             x = 0;
639             y++;
640             if (y >= h)
641                 return 1;
642             y0dst -= 2*ylinesize;
643             y1dst -= 2*ylinesize;
644             udst  -=   ulinesize;
645             vdst  -=   vlinesize;
646         }
647         pos = 0;
648     }
649
650     *y0 = y0dst;
651     *y1 = y1dst;
652     *u = udst;
653     *v = vdst;
654     *np = pos;
655     *nx = x;
656     *ny = y;
657
658     return 0;
659 }
660
661 static int decode_runlen_rgb(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
662 {
663     uint8_t *dst = frame->data[0] + (avctx->height - 1) * frame->linesize[0];
664     int runlen, y = 0, x = 0;
665     uint8_t fill[4];
666     unsigned code;
667
668     while (bytestream2_get_bytes_left(gbyte) > 0) {
669         code = bytestream2_peek_le32(gbyte);
670         runlen = code & 0xFFFFFF;
671
672         if (code >> 24 == 0x77) {
673             bytestream2_skip(gbyte, 4);
674
675             for (int i = 0; i < 4; i++)
676                 fill[i] = bytestream2_get_byte(gbyte);
677
678             while (runlen > 0) {
679                 runlen--;
680
681                 for (int i = 0; i < 4; i++) {
682                     dst[x] += fill[i];
683                     x++;
684                     if (x >= frame->width * 3) {
685                         x = 0;
686                         y++;
687                         dst -= frame->linesize[0];
688                         if (y >= frame->height)
689                             return 0;
690                     }
691                 }
692             }
693         } else {
694             for (int i = 0; i < 4; i++)
695                 fill[i] = bytestream2_get_byte(gbyte);
696
697             for (int i = 0; i < 4; i++) {
698                 dst[x] += fill[i];
699                 x++;
700                 if (x >= frame->width * 3) {
701                     x = 0;
702                     y++;
703                     dst -= frame->linesize[0];
704                     if (y >= frame->height)
705                         return 0;
706                 }
707             }
708         }
709     }
710
711     return 0;
712 }
713
714 static int decode_runlen(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
715 {
716     uint8_t *y0dst = frame->data[0] + (avctx->height - 1) * frame->linesize[0];
717     uint8_t *y1dst = y0dst - frame->linesize[0];
718     uint8_t *udst = frame->data[1] + ((avctx->height >> 1) - 1) * frame->linesize[1];
719     uint8_t *vdst = frame->data[2] + ((avctx->height >> 1) - 1) * frame->linesize[2];
720     int runlen, y = 0, x = 0, pos = 0;
721     uint8_t fill[4];
722     unsigned code;
723
724     while (bytestream2_get_bytes_left(gbyte) > 0) {
725         code = bytestream2_peek_le32(gbyte);
726         runlen = code & 0xFFFFFF;
727
728         if (code >> 24 == 0x77) {
729             bytestream2_skip(gbyte, 4);
730
731             for (int i = 0; i < 4; i++)
732                 fill[i] = bytestream2_get_byte(gbyte);
733
734             while (runlen > 0) {
735                 runlen--;
736
737                 if (fill_pixels(&y0dst, &y1dst, &udst, &vdst,
738                                 frame->linesize[0],
739                                 frame->linesize[1],
740                                 frame->linesize[2],
741                                 fill, &x, &y, &pos,
742                                 avctx->width / 2,
743                                 avctx->height / 2))
744                     return 0;
745             }
746         } else {
747             for (int i = 0; i < 4; i++)
748                 fill[i] = bytestream2_get_byte(gbyte);
749
750             if (fill_pixels(&y0dst, &y1dst, &udst, &vdst,
751                             frame->linesize[0],
752                             frame->linesize[1],
753                             frame->linesize[2],
754                             fill, &x, &y, &pos,
755                             avctx->width / 2,
756                             avctx->height / 2))
757                 return 0;
758         }
759     }
760
761     return 0;
762 }
763
764 static int decode_raw_intra(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
765 {
766     uint8_t *y0dst = frame->data[0] + (avctx->height - 1) * frame->linesize[0];
767     uint8_t *y1dst = y0dst - frame->linesize[0];
768     uint8_t *udst = frame->data[1] + ((avctx->height >> 1) - 1) * frame->linesize[1];
769     uint8_t *vdst = frame->data[2] + ((avctx->height >> 1) - 1) * frame->linesize[2];
770     uint8_t ly0 = 0, ly1 = 0, ly2 = 0, ly3 = 0, lu = 0, lv = 0;
771
772     for (int y = 0; y < avctx->height / 2; y++) {
773         for (int x = 0; x < avctx->width / 2; x++) {
774             y0dst[x*2+0] = bytestream2_get_byte(gbyte) + ly0;
775             ly0 = y0dst[x*2+0];
776             y0dst[x*2+1] = bytestream2_get_byte(gbyte) + ly1;
777             ly1 = y0dst[x*2+1];
778             y1dst[x*2+0] = bytestream2_get_byte(gbyte) + ly2;
779             ly2 = y1dst[x*2+0];
780             y1dst[x*2+1] = bytestream2_get_byte(gbyte) + ly3;
781             ly3 = y1dst[x*2+1];
782             udst[x] = bytestream2_get_byte(gbyte) + lu;
783             lu = udst[x];
784             vdst[x] = bytestream2_get_byte(gbyte) + lv;
785             lv = vdst[x];
786         }
787
788         y0dst -= 2*frame->linesize[0];
789         y1dst -= 2*frame->linesize[0];
790         udst  -= frame->linesize[1];
791         vdst  -= frame->linesize[2];
792     }
793
794     return 0;
795 }
796
797 static int decode_intra(AVCodecContext *avctx, GetBitContext *gb, AVFrame *frame)
798 {
799     AGMContext *s = avctx->priv_data;
800     int ret;
801
802     compute_quant_matrix(s, (2 * s->compression - 100) / 100.0);
803
804     s->blocks_w = avctx->coded_width  >> 3;
805     s->blocks_h = avctx->coded_height >> 3;
806
807     ret = decode_intra_plane(s, gb, s->size[0], s->luma_quant_matrix, frame, 0);
808     if (ret < 0)
809         return ret;
810
811     bytestream2_skip(&s->gbyte, s->size[0]);
812
813     s->blocks_w = avctx->coded_width  >> 4;
814     s->blocks_h = avctx->coded_height >> 4;
815
816     ret = decode_intra_plane(s, gb, s->size[1], s->chroma_quant_matrix, frame, 2);
817     if (ret < 0)
818         return ret;
819
820     bytestream2_skip(&s->gbyte, s->size[1]);
821
822     s->blocks_w = avctx->coded_width  >> 4;
823     s->blocks_h = avctx->coded_height >> 4;
824
825     ret = decode_intra_plane(s, gb, s->size[2], s->chroma_quant_matrix, frame, 1);
826     if (ret < 0)
827         return ret;
828
829     return 0;
830 }
831
832 static int decode_motion_vectors(AVCodecContext *avctx, GetBitContext *gb)
833 {
834     AGMContext *s = avctx->priv_data;
835     int nb_mvs = ((avctx->coded_height + 15) >> 4) * ((avctx->coded_width + 15) >> 4);
836     int ret, skip = 0, value, map;
837
838     av_fast_padded_malloc(&s->mvectors, &s->mvectors_size,
839                           nb_mvs * sizeof(*s->mvectors));
840     if (!s->mvectors)
841         return AVERROR(ENOMEM);
842
843     if ((ret = init_get_bits8(gb, s->gbyte.buffer, bytestream2_get_bytes_left(&s->gbyte) -
844                                                    (s->size[0] + s->size[1] + s->size[2]))) < 0)
845         return ret;
846
847     memset(s->mvectors, 0, sizeof(*s->mvectors) * nb_mvs);
848
849     for (int i = 0; i < nb_mvs; i++) {
850         ret = read_code(gb, &skip, &value, &map, 1);
851         if (ret < 0)
852             return ret;
853         s->mvectors[i].x = value;
854         i += skip;
855     }
856
857     for (int i = 0; i < nb_mvs; i++) {
858         ret = read_code(gb, &skip, &value, &map, 1);
859         if (ret < 0)
860             return ret;
861         s->mvectors[i].y = value;
862         i += skip;
863     }
864
865     if (get_bits_left(gb) <= 0)
866         return AVERROR_INVALIDDATA;
867     skip = (get_bits_count(gb) >> 3) + 1;
868     bytestream2_skip(&s->gbyte, skip);
869
870     return 0;
871 }
872
873 static int decode_inter(AVCodecContext *avctx, GetBitContext *gb,
874                         AVFrame *frame, AVFrame *prev)
875 {
876     AGMContext *s = avctx->priv_data;
877     int ret;
878
879     compute_quant_matrix(s, (2 * s->compression - 100) / 100.0);
880
881     if (s->flags & 2) {
882         ret = decode_motion_vectors(avctx, gb);
883         if (ret < 0)
884             return ret;
885     }
886
887     s->blocks_w = avctx->coded_width  >> 3;
888     s->blocks_h = avctx->coded_height >> 3;
889
890     ret = decode_inter_plane(s, gb, s->size[0], s->luma_quant_matrix, frame, prev, 0);
891     if (ret < 0)
892         return ret;
893
894     bytestream2_skip(&s->gbyte, s->size[0]);
895
896     s->blocks_w = avctx->coded_width  >> 4;
897     s->blocks_h = avctx->coded_height >> 4;
898
899     ret = decode_inter_plane(s, gb, s->size[1], s->chroma_quant_matrix, frame, prev, 2);
900     if (ret < 0)
901         return ret;
902
903     bytestream2_skip(&s->gbyte, s->size[1]);
904
905     s->blocks_w = avctx->coded_width  >> 4;
906     s->blocks_h = avctx->coded_height >> 4;
907
908     ret = decode_inter_plane(s, gb, s->size[2], s->chroma_quant_matrix, frame, prev, 1);
909     if (ret < 0)
910         return ret;
911
912     return 0;
913 }
914
915 typedef struct Node {
916     int parent;
917     int child[2];
918 } Node;
919
920 static void get_tree_codes(uint32_t *codes, Node *nodes, int idx, uint32_t pfx, int bitpos)
921 {
922     if (idx < 256 && idx >= 0) {
923         codes[idx] = pfx;
924     } else if (idx >= 0) {
925         get_tree_codes(codes, nodes, nodes[idx].child[0], pfx + (0 << bitpos), bitpos + 1);
926         get_tree_codes(codes, nodes, nodes[idx].child[1], pfx + (1U << bitpos), bitpos + 1);
927     }
928 }
929
930 static int make_new_tree(const uint8_t *bitlens, uint32_t *codes)
931 {
932     int zlcount = 0, curlen, idx, nindex, last, llast;
933     int blcounts[32] = { 0 };
934     int syms[8192];
935     Node nodes[512];
936     int node_idx[1024];
937     int old_idx[512];
938
939     for (int i = 0; i < 256; i++) {
940         int bitlen = bitlens[i];
941         int blcount = blcounts[bitlen];
942
943         zlcount += bitlen < 1;
944         syms[(bitlen << 8) + blcount] = i;
945         blcounts[bitlen]++;
946     }
947
948     for (int i = 0; i < 512; i++) {
949         nodes[i].child[0] = -1;
950         nodes[i].child[1] = -1;
951     }
952
953     for (int i = 0; i < 256; i++) {
954         node_idx[i] = 257 + i;
955     }
956
957     curlen = 1;
958     node_idx[512] = 256;
959     last = 255;
960     nindex = 1;
961
962     for (curlen = 1; curlen < 32; curlen++) {
963         if (blcounts[curlen] > 0) {
964             int max_zlcount = zlcount + blcounts[curlen];
965
966             for (int i = 0; zlcount < 256 && zlcount < max_zlcount; zlcount++, i++) {
967                 int p = node_idx[nindex - 1 + 512];
968                 int ch = syms[256 * curlen + i];
969
970                 if (nindex <= 0)
971                     return AVERROR_INVALIDDATA;
972
973                 if (nodes[p].child[0] == -1) {
974                     nodes[p].child[0] = ch;
975                 } else {
976                     nodes[p].child[1] = ch;
977                     nindex--;
978                 }
979                 nodes[ch].parent = p;
980             }
981         }
982         llast = last - 1;
983         idx = 0;
984         while (nindex > 0) {
985             int p, ch;
986
987             last = llast - idx;
988             p = node_idx[nindex - 1 + 512];
989             ch = node_idx[last];
990             if (nodes[p].child[0] == -1) {
991                 nodes[p].child[0] = ch;
992             } else {
993                 nodes[p].child[1] = ch;
994                 nindex--;
995             }
996             old_idx[idx] = ch;
997             nodes[ch].parent = p;
998             if (idx == llast)
999                 goto next;
1000             idx++;
1001             if (nindex <= 0) {
1002                 for (int i = 0; i < idx; i++)
1003                     node_idx[512 + i] = old_idx[i];
1004             }
1005         }
1006         nindex = idx;
1007     }
1008
1009 next:
1010
1011     get_tree_codes(codes, nodes, 256, 0, 0);
1012     return 0;
1013 }
1014
1015 static int build_huff(const uint8_t *bitlen, VLC *vlc)
1016 {
1017     uint32_t new_codes[256];
1018     uint8_t bits[256];
1019     uint8_t symbols[256];
1020     uint32_t codes[256];
1021     int nb_codes = 0;
1022
1023     int ret = make_new_tree(bitlen, new_codes);
1024     if (ret < 0)
1025         return ret;
1026
1027     for (int i = 0; i < 256; i++) {
1028         if (bitlen[i]) {
1029             bits[nb_codes] = bitlen[i];
1030             codes[nb_codes] = new_codes[i];
1031             symbols[nb_codes] = i;
1032             nb_codes++;
1033         }
1034     }
1035
1036     ff_free_vlc(vlc);
1037     return ff_init_vlc_sparse(vlc, 13, nb_codes,
1038                               bits, 1, 1,
1039                               codes, 4, 4,
1040                               symbols, 1, 1,
1041                               INIT_VLC_LE);
1042 }
1043
1044 static int decode_huffman2(AVCodecContext *avctx, int header, int size)
1045 {
1046     AGMContext *s = avctx->priv_data;
1047     GetBitContext *gb = &s->gb;
1048     uint8_t lens[256];
1049     int ret, x, len;
1050
1051     if ((ret = init_get_bits8(gb, s->gbyte.buffer,
1052                               bytestream2_get_bytes_left(&s->gbyte))) < 0)
1053         return ret;
1054
1055     s->output_size = get_bits_long(gb, 32);
1056
1057     if (s->output_size > avctx->width * avctx->height * 9LL + 10000)
1058         return AVERROR_INVALIDDATA;
1059
1060     av_fast_padded_malloc(&s->output, &s->padded_output_size, s->output_size);
1061     if (!s->output)
1062         return AVERROR(ENOMEM);
1063
1064     x = get_bits(gb, 1);
1065     len = 4 + get_bits(gb, 1);
1066     if (x) {
1067         int cb[8] = { 0 };
1068         int count = get_bits(gb, 3) + 1;
1069
1070         for (int i = 0; i < count; i++)
1071             cb[i] = get_bits(gb, len);
1072
1073         for (int i = 0; i < 256; i++) {
1074             int idx = get_bits(gb, 3);
1075             lens[i] = cb[idx];
1076         }
1077     } else {
1078         for (int i = 0; i < 256; i++)
1079             lens[i] = get_bits(gb, len);
1080     }
1081
1082     if ((ret = build_huff(lens, &s->vlc)) < 0)
1083         return ret;
1084
1085     x = 0;
1086     while (get_bits_left(gb) > 0 && x < s->output_size) {
1087         int val = get_vlc2(gb, s->vlc.table, s->vlc.bits, 3);
1088         if (val < 0)
1089             return AVERROR_INVALIDDATA;
1090         s->output[x++] = val;
1091     }
1092
1093     return 0;
1094 }
1095
1096 static int decode_frame(AVCodecContext *avctx, void *data,
1097                         int *got_frame, AVPacket *avpkt)
1098 {
1099     AGMContext *s = avctx->priv_data;
1100     GetBitContext *gb = &s->gb;
1101     GetByteContext *gbyte = &s->gbyte;
1102     AVFrame *frame = data;
1103     int w, h, width, height, header;
1104     unsigned compressed_size;
1105     long skip;
1106     int ret;
1107
1108     if (!avpkt->size)
1109         return 0;
1110
1111     bytestream2_init(gbyte, avpkt->data, avpkt->size);
1112
1113     header = bytestream2_get_le32(gbyte);
1114     s->fflags = bytestream2_get_le32(gbyte);
1115     s->bitstream_size = s->fflags & 0x1FFFFFFF;
1116     s->fflags >>= 29;
1117     av_log(avctx, AV_LOG_DEBUG, "fflags: %X\n", s->fflags);
1118     if (avpkt->size < s->bitstream_size + 8)
1119         return AVERROR_INVALIDDATA;
1120
1121     s->key_frame = (avpkt->flags & AV_PKT_FLAG_KEY);
1122     frame->key_frame = s->key_frame;
1123     frame->pict_type = s->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
1124
1125     if (!s->key_frame) {
1126         if (!s->prev_frame->data[0]) {
1127             av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
1128             return AVERROR_INVALIDDATA;
1129         }
1130     }
1131
1132     if (header) {
1133         if (avctx->codec_tag == MKTAG('A', 'G', 'M', '0') ||
1134             avctx->codec_tag == MKTAG('A', 'G', 'M', '1'))
1135             return AVERROR_PATCHWELCOME;
1136         else
1137             ret = decode_huffman2(avctx, header, (avpkt->size - s->bitstream_size) - 8);
1138         if (ret < 0)
1139             return ret;
1140         bytestream2_init(gbyte, s->output, s->output_size);
1141     } else if (!s->dct) {
1142         bytestream2_skip(gbyte, 4);
1143     }
1144
1145     if (s->dct) {
1146         s->flags = 0;
1147         w = bytestream2_get_le32(gbyte);
1148         h = bytestream2_get_le32(gbyte);
1149         if (w == INT32_MIN || h == INT32_MIN)
1150             return AVERROR_INVALIDDATA;
1151         if (w < 0) {
1152             w = -w;
1153             s->flags |= 2;
1154         }
1155         if (h < 0) {
1156             h = -h;
1157             s->flags |= 1;
1158         }
1159
1160         width  = avctx->width;
1161         height = avctx->height;
1162         if (w < width || h < height || w & 7 || h & 7)
1163             return AVERROR_INVALIDDATA;
1164
1165         ret = ff_set_dimensions(avctx, w, h);
1166         if (ret < 0)
1167             return ret;
1168         avctx->width = width;
1169         avctx->height = height;
1170
1171         s->compression = bytestream2_get_le32(gbyte);
1172         if (s->compression < 0 || s->compression > 100)
1173             return AVERROR_INVALIDDATA;
1174
1175         for (int i = 0; i < 3; i++)
1176             s->size[i] = bytestream2_get_le32(gbyte);
1177         if (header) {
1178             compressed_size = s->output_size;
1179             skip = 8LL;
1180         } else {
1181             compressed_size = avpkt->size;
1182             skip = 32LL;
1183         }
1184         if (s->size[0] < 0 || s->size[1] < 0 || s->size[2] < 0 ||
1185             skip + s->size[0] + s->size[1] + s->size[2] > compressed_size) {
1186             return AVERROR_INVALIDDATA;
1187         }
1188     }
1189
1190     if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1191         return ret;
1192
1193     if (frame->key_frame) {
1194         if (!s->dct && !s->rgb)
1195             ret = decode_raw_intra(avctx, gbyte, frame);
1196         else if (!s->dct && s->rgb)
1197             ret = decode_raw_intra_rgb(avctx, gbyte, frame);
1198         else
1199             ret = decode_intra(avctx, gb, frame);
1200     } else {
1201         if (s->prev_frame-> width != frame->width ||
1202             s->prev_frame->height != frame->height)
1203             return AVERROR_INVALIDDATA;
1204
1205         if (!(s->flags & 2)) {
1206             ret = av_frame_copy(frame, s->prev_frame);
1207             if (ret < 0)
1208                 return ret;
1209         }
1210
1211         if (s->dct) {
1212             ret = decode_inter(avctx, gb, frame, s->prev_frame);
1213         } else if (!s->dct && !s->rgb) {
1214             ret = decode_runlen(avctx, gbyte, frame);
1215         } else {
1216             ret = decode_runlen_rgb(avctx, gbyte, frame);
1217         }
1218     }
1219     if (ret < 0)
1220         return ret;
1221
1222     av_frame_unref(s->prev_frame);
1223     if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
1224         return ret;
1225
1226     frame->crop_top  = avctx->coded_height - avctx->height;
1227     frame->crop_left = avctx->coded_width  - avctx->width;
1228
1229     *got_frame = 1;
1230
1231     return avpkt->size;
1232 }
1233
1234 static av_cold int decode_init(AVCodecContext *avctx)
1235 {
1236     AGMContext *s = avctx->priv_data;
1237
1238     s->rgb = avctx->codec_tag == MKTAG('A', 'G', 'M', '4');
1239     avctx->pix_fmt = s->rgb ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUV420P;
1240     s->avctx = avctx;
1241     s->plus = avctx->codec_tag == MKTAG('A', 'G', 'M', '3') ||
1242               avctx->codec_tag == MKTAG('A', 'G', 'M', '7');
1243
1244     s->dct = avctx->codec_tag != MKTAG('A', 'G', 'M', '4') &&
1245              avctx->codec_tag != MKTAG('A', 'G', 'M', '5');
1246
1247     if (!s->rgb && !s->dct) {
1248         if ((avctx->width & 1) || (avctx->height & 1))
1249             return AVERROR_INVALIDDATA;
1250     }
1251
1252     avctx->idct_algo = FF_IDCT_SIMPLE;
1253     ff_idctdsp_init(&s->idsp, avctx);
1254     ff_init_scantable(s->idsp.idct_permutation, &s->scantable, ff_zigzag_direct);
1255
1256     s->prev_frame = av_frame_alloc();
1257     if (!s->prev_frame)
1258         return AVERROR(ENOMEM);
1259
1260     return 0;
1261 }
1262
1263 static void decode_flush(AVCodecContext *avctx)
1264 {
1265     AGMContext *s = avctx->priv_data;
1266
1267     av_frame_unref(s->prev_frame);
1268 }
1269
1270 static av_cold int decode_close(AVCodecContext *avctx)
1271 {
1272     AGMContext *s = avctx->priv_data;
1273
1274     ff_free_vlc(&s->vlc);
1275     av_frame_free(&s->prev_frame);
1276     av_freep(&s->mvectors);
1277     s->mvectors_size = 0;
1278     av_freep(&s->wblocks);
1279     s->wblocks_size = 0;
1280     av_freep(&s->output);
1281     s->padded_output_size = 0;
1282     av_freep(&s->map);
1283     s->map_size = 0;
1284
1285     return 0;
1286 }
1287
1288 const AVCodec ff_agm_decoder = {
1289     .name             = "agm",
1290     .long_name        = NULL_IF_CONFIG_SMALL("Amuse Graphics Movie"),
1291     .type             = AVMEDIA_TYPE_VIDEO,
1292     .id               = AV_CODEC_ID_AGM,
1293     .priv_data_size   = sizeof(AGMContext),
1294     .init             = decode_init,
1295     .close            = decode_close,
1296     .decode           = decode_frame,
1297     .flush            = decode_flush,
1298     .capabilities     = AV_CODEC_CAP_DR1,
1299     .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE |
1300                         FF_CODEC_CAP_INIT_CLEANUP |
1301                         FF_CODEC_CAP_EXPORTS_CROPPING,
1302 };