FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/agm.c
Date: 2026-09-11 02:21:26
Exec Total Coverage
Lines: 0 783 0.0%
Functions: 0 24 0.0%
Branches: 0 483 0.0%

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