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