FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/agm.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 0 779 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 if (y * 8 + mv_y < 0 || y * 8 + mv_y + 8 > h ||
413 x * 8 + mv_x < 0 || x * 8 + mv_x + 8 > w)
414 return AVERROR_INVALIDDATA;
415
416 copy_block8(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
417 prev->data[plane] + ((s->blocks_h - 1 - y) * 8 - mv_y) * prev->linesize[plane] + (x * 8 + mv_x),
418 frame->linesize[plane], prev->linesize[plane], 8);
419 if (map) {
420 s->idsp.idct(s->wblocks + x * 64);
421 for (int i = 0; i < 64; i++)
422 s->wblocks[i + x * 64] = (s->wblocks[i + x * 64] + 1) & 0xFFFC;
423 s->idsp.add_pixels_clamped(&s->wblocks[x*64], frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
424 frame->linesize[plane]);
425 }
426 } else if (map) {
427 s->idsp.idct_put(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
428 frame->linesize[plane], s->wblocks + x * 64);
429 }
430 }
431 }
432 } else if (s->flags & 2) {
433 for (int y = 0; y < s->blocks_h; y++) {
434 for (int x = 0; x < s->blocks_w; x++) {
435 int shift = plane == 0;
436 int mvpos = (y >> shift) * (s->blocks_w >> shift) + (x >> shift);
437 int orig_mv_x = s->mvectors[mvpos].x;
438 int mv_x = s->mvectors[mvpos].x / (1 + !shift);
439 int mv_y = s->mvectors[mvpos].y / (1 + !shift);
440 int h = s->avctx->coded_height >> !shift;
441 int w = s->avctx->coded_width >> !shift;
442 int map = 0;
443
444 ret = decode_inter_block(s, gb, quant_matrix, &skip, &map);
445 if (ret < 0)
446 return ret;
447
448 if (orig_mv_x >= -32) {
449 if (y * 8 + mv_y < 0 || y * 8 + mv_y + 8 > h ||
450 x * 8 + mv_x < 0 || x * 8 + mv_x + 8 > w)
451 return AVERROR_INVALIDDATA;
452
453 copy_block8(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
454 prev->data[plane] + ((s->blocks_h - 1 - y) * 8 - mv_y) * prev->linesize[plane] + (x * 8 + mv_x),
455 frame->linesize[plane], prev->linesize[plane], 8);
456 if (map) {
457 s->idsp.idct(s->block);
458 for (int i = 0; i < 64; i++)
459 s->block[i] = (s->block[i] + 1) & 0xFFFC;
460 s->idsp.add_pixels_clamped(s->block, frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
461 frame->linesize[plane]);
462 }
463 } else if (map) {
464 s->idsp.idct_put(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
465 frame->linesize[plane], s->block);
466 }
467 }
468 }
469 } else if (s->flags & 1) {
470 av_fast_padded_malloc(&s->wblocks, &s->wblocks_size,
471 64 * s->blocks_w * sizeof(*s->wblocks));
472 if (!s->wblocks)
473 return AVERROR(ENOMEM);
474
475 av_fast_padded_malloc(&s->map, &s->map_size,
476 s->blocks_w * sizeof(*s->map));
477 if (!s->map)
478 return AVERROR(ENOMEM);
479
480 for (int y = 0; y < s->blocks_h; y++) {
481 ret = decode_inter_blocks(s, gb, quant_matrix, &skip, s->map);
482 if (ret < 0)
483 return ret;
484
485 for (int x = 0; x < s->blocks_w; x++) {
486 if (!s->map[x])
487 continue;
488 s->idsp.idct_add(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
489 frame->linesize[plane], s->wblocks + 64 * x);
490 }
491 }
492 } else {
493 for (int y = 0; y < s->blocks_h; y++) {
494 for (int x = 0; x < s->blocks_w; x++) {
495 int map = 0;
496
497 ret = decode_inter_block(s, gb, quant_matrix, &skip, &map);
498 if (ret < 0)
499 return ret;
500
501 if (!map)
502 continue;
503 s->idsp.idct_add(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
504 frame->linesize[plane], s->block);
505 }
506 }
507 }
508
509 align_get_bits(gb);
510 if (get_bits_left(gb) < 0)
511 av_log(s->avctx, AV_LOG_WARNING, "overread\n");
512 if (get_bits_left(gb) > 0)
513 av_log(s->avctx, AV_LOG_WARNING, "underread: %d\n", get_bits_left(gb));
514
515 return 0;
516 }
517
518 static void compute_quant_matrix(AGMContext *s, double qscale)
519 {
520 int luma[64], chroma[64];
521 double f = 1.0 - fabs(qscale);
522
523 if (!s->key_frame && (s->flags & 2)) {
524 if (qscale >= 0.0) {
525 for (int i = 0; i < 64; i++) {
526 luma[i] = FFMAX(1, 16 * f);
527 chroma[i] = FFMAX(1, 16 * f);
528 }
529 } else {
530 for (int i = 0; i < 64; i++) {
531 luma[i] = FFMAX(1, 16 - qscale * 32);
532 chroma[i] = FFMAX(1, 16 - qscale * 32);
533 }
534 }
535 } else {
536 if (qscale >= 0.0) {
537 for (int i = 0; i < 64; i++) {
538 luma[i] = FFMAX(1, ff_mjpeg_std_luminance_quant_tbl [(i & 7) * 8 + (i >> 3)] * f);
539 chroma[i] = FFMAX(1, ff_mjpeg_std_chrominance_quant_tbl[(i & 7) * 8 + (i >> 3)] * f);
540 }
541 } else {
542 for (int i = 0; i < 64; i++) {
543 luma[i] = FFMAX(1, 255.0 - (255 - ff_mjpeg_std_luminance_quant_tbl [(i & 7) * 8 + (i >> 3)]) * f);
544 chroma[i] = FFMAX(1, 255.0 - (255 - ff_mjpeg_std_chrominance_quant_tbl[(i & 7) * 8 + (i >> 3)]) * f);
545 }
546 }
547 }
548
549 for (int i = 0; i < 64; i++) {
550 int pos = ff_zigzag_direct[i];
551
552 s->luma_quant_matrix[i] = luma[pos] * ((pos / 8) & 1 ? -1 : 1);
553 s->chroma_quant_matrix[i] = chroma[pos] * ((pos / 8) & 1 ? -1 : 1);
554 }
555 }
556
557 static int decode_raw_intra_rgb(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
558 {
559 uint8_t *dst = frame->data[0] + (avctx->height - 1) * frame->linesize[0];
560 uint8_t r = 0, g = 0, b = 0;
561
562 if (bytestream2_get_bytes_left(gbyte) < 3 * avctx->width * avctx->height)
563 return AVERROR_INVALIDDATA;
564
565 for (int y = 0; y < avctx->height; y++) {
566 for (int x = 0; x < avctx->width; x++) {
567 dst[x*3+0] = bytestream2_get_byteu(gbyte) + r;
568 r = dst[x*3+0];
569 dst[x*3+1] = bytestream2_get_byteu(gbyte) + g;
570 g = dst[x*3+1];
571 dst[x*3+2] = bytestream2_get_byteu(gbyte) + b;
572 b = dst[x*3+2];
573 }
574 dst -= frame->linesize[0];
575 }
576
577 return 0;
578 }
579
580 av_always_inline static int fill_pixels(uint8_t **y0, uint8_t **y1,
581 uint8_t **u, uint8_t **v,
582 int ylinesize, int ulinesize, int vlinesize,
583 uint8_t *fill,
584 int *nx, int *ny, int *np, int w, int h)
585 {
586 uint8_t *y0dst = *y0;
587 uint8_t *y1dst = *y1;
588 uint8_t *udst = *u;
589 uint8_t *vdst = *v;
590 int x = *nx, y = *ny, pos = *np;
591
592 if (pos == 0) {
593 y0dst[2*x+0] += fill[0];
594 y0dst[2*x+1] += fill[1];
595 y1dst[2*x+0] += fill[2];
596 y1dst[2*x+1] += fill[3];
597 pos++;
598 } else if (pos == 1) {
599 udst[x] += fill[0];
600 vdst[x] += fill[1];
601 x++;
602 if (x >= w) {
603 x = 0;
604 y++;
605 if (y >= h)
606 return 1;
607 y0dst -= 2*ylinesize;
608 y1dst -= 2*ylinesize;
609 udst -= ulinesize;
610 vdst -= vlinesize;
611 }
612 y0dst[2*x+0] += fill[2];
613 y0dst[2*x+1] += fill[3];
614 pos++;
615 } else if (pos == 2) {
616 y1dst[2*x+0] += fill[0];
617 y1dst[2*x+1] += fill[1];
618 udst[x] += fill[2];
619 vdst[x] += fill[3];
620 x++;
621 if (x >= w) {
622 x = 0;
623 y++;
624 if (y >= h)
625 return 1;
626 y0dst -= 2*ylinesize;
627 y1dst -= 2*ylinesize;
628 udst -= ulinesize;
629 vdst -= vlinesize;
630 }
631 pos = 0;
632 }
633
634 *y0 = y0dst;
635 *y1 = y1dst;
636 *u = udst;
637 *v = vdst;
638 *np = pos;
639 *nx = x;
640 *ny = y;
641
642 return 0;
643 }
644
645 static int decode_runlen_rgb(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
646 {
647 uint8_t *dst = frame->data[0] + (avctx->height - 1) * frame->linesize[0];
648 int runlen, y = 0, x = 0;
649 uint8_t fill[4];
650 unsigned code;
651
652 while (bytestream2_get_bytes_left(gbyte) > 0) {
653 code = bytestream2_peek_le32(gbyte);
654 runlen = code & 0xFFFFFF;
655
656 if (code >> 24 == 0x77) {
657 bytestream2_skip(gbyte, 4);
658
659 for (int i = 0; i < 4; i++)
660 fill[i] = bytestream2_get_byte(gbyte);
661
662 while (runlen > 0) {
663 runlen--;
664
665 for (int i = 0; i < 4; i++) {
666 dst[x] += fill[i];
667 x++;
668 if (x >= frame->width * 3) {
669 x = 0;
670 y++;
671 dst -= frame->linesize[0];
672 if (y >= frame->height)
673 return 0;
674 }
675 }
676 }
677 } else {
678 for (int i = 0; i < 4; i++)
679 fill[i] = bytestream2_get_byte(gbyte);
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 }
694
695 return 0;
696 }
697
698 static int decode_runlen(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
699 {
700 uint8_t *y0dst = frame->data[0] + (avctx->height - 1) * frame->linesize[0];
701 uint8_t *y1dst = y0dst - frame->linesize[0];
702 uint8_t *udst = frame->data[1] + ((avctx->height >> 1) - 1) * frame->linesize[1];
703 uint8_t *vdst = frame->data[2] + ((avctx->height >> 1) - 1) * frame->linesize[2];
704 int runlen, y = 0, x = 0, pos = 0;
705 uint8_t fill[4];
706 unsigned code;
707
708 while (bytestream2_get_bytes_left(gbyte) > 0) {
709 code = bytestream2_peek_le32(gbyte);
710 runlen = code & 0xFFFFFF;
711
712 if (code >> 24 == 0x77) {
713 bytestream2_skip(gbyte, 4);
714
715 for (int i = 0; i < 4; i++)
716 fill[i] = bytestream2_get_byte(gbyte);
717
718 while (runlen > 0) {
719 runlen--;
720
721 if (fill_pixels(&y0dst, &y1dst, &udst, &vdst,
722 frame->linesize[0],
723 frame->linesize[1],
724 frame->linesize[2],
725 fill, &x, &y, &pos,
726 avctx->width / 2,
727 avctx->height / 2))
728 return 0;
729 }
730 } else {
731 for (int i = 0; i < 4; i++)
732 fill[i] = bytestream2_get_byte(gbyte);
733
734 if (fill_pixels(&y0dst, &y1dst, &udst, &vdst,
735 frame->linesize[0],
736 frame->linesize[1],
737 frame->linesize[2],
738 fill, &x, &y, &pos,
739 avctx->width / 2,
740 avctx->height / 2))
741 return 0;
742 }
743 }
744
745 return 0;
746 }
747
748 static int decode_raw_intra(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
749 {
750 uint8_t *y0dst = frame->data[0] + (avctx->height - 1) * frame->linesize[0];
751 uint8_t *y1dst = y0dst - frame->linesize[0];
752 uint8_t *udst = frame->data[1] + ((avctx->height >> 1) - 1) * frame->linesize[1];
753 uint8_t *vdst = frame->data[2] + ((avctx->height >> 1) - 1) * frame->linesize[2];
754 uint8_t ly0 = 0, ly1 = 0, ly2 = 0, ly3 = 0, lu = 0, lv = 0;
755
756 for (int y = 0; y < avctx->height / 2; y++) {
757 for (int x = 0; x < avctx->width / 2; x++) {
758 y0dst[x*2+0] = bytestream2_get_byte(gbyte) + ly0;
759 ly0 = y0dst[x*2+0];
760 y0dst[x*2+1] = bytestream2_get_byte(gbyte) + ly1;
761 ly1 = y0dst[x*2+1];
762 y1dst[x*2+0] = bytestream2_get_byte(gbyte) + ly2;
763 ly2 = y1dst[x*2+0];
764 y1dst[x*2+1] = bytestream2_get_byte(gbyte) + ly3;
765 ly3 = y1dst[x*2+1];
766 udst[x] = bytestream2_get_byte(gbyte) + lu;
767 lu = udst[x];
768 vdst[x] = bytestream2_get_byte(gbyte) + lv;
769 lv = vdst[x];
770 }
771
772 y0dst -= 2*frame->linesize[0];
773 y1dst -= 2*frame->linesize[0];
774 udst -= frame->linesize[1];
775 vdst -= frame->linesize[2];
776 }
777
778 return 0;
779 }
780
781 static int decode_intra(AVCodecContext *avctx, GetBitContext *gb, AVFrame *frame)
782 {
783 AGMContext *s = avctx->priv_data;
784 int ret;
785
786 compute_quant_matrix(s, (2 * s->compression - 100) / 100.0);
787
788 s->blocks_w = avctx->coded_width >> 3;
789 s->blocks_h = avctx->coded_height >> 3;
790
791 ret = decode_intra_plane(s, gb, s->size[0], s->luma_quant_matrix, frame, 0);
792 if (ret < 0)
793 return ret;
794
795 bytestream2_skip(&s->gbyte, s->size[0]);
796
797 s->blocks_w = avctx->coded_width >> 4;
798 s->blocks_h = avctx->coded_height >> 4;
799
800 ret = decode_intra_plane(s, gb, s->size[1], s->chroma_quant_matrix, frame, 2);
801 if (ret < 0)
802 return ret;
803
804 bytestream2_skip(&s->gbyte, s->size[1]);
805
806 s->blocks_w = avctx->coded_width >> 4;
807 s->blocks_h = avctx->coded_height >> 4;
808
809 ret = decode_intra_plane(s, gb, s->size[2], s->chroma_quant_matrix, frame, 1);
810 if (ret < 0)
811 return ret;
812
813 return 0;
814 }
815
816 static int decode_motion_vectors(AVCodecContext *avctx, GetBitContext *gb)
817 {
818 AGMContext *s = avctx->priv_data;
819 int nb_mvs = ((avctx->coded_height + 15) >> 4) * ((avctx->coded_width + 15) >> 4);
820 int ret, skip = 0, value, map;
821
822 av_fast_padded_malloc(&s->mvectors, &s->mvectors_size,
823 nb_mvs * sizeof(*s->mvectors));
824 if (!s->mvectors)
825 return AVERROR(ENOMEM);
826
827 if ((ret = init_get_bits8(gb, s->gbyte.buffer, bytestream2_get_bytes_left(&s->gbyte) -
828 (s->size[0] + s->size[1] + s->size[2]))) < 0)
829 return ret;
830
831 memset(s->mvectors, 0, sizeof(*s->mvectors) * nb_mvs);
832
833 for (int i = 0; i < nb_mvs; i++) {
834 ret = read_code(gb, &skip, &value, &map, 1);
835 if (ret < 0)
836 return ret;
837 s->mvectors[i].x = value;
838 i += skip;
839 }
840
841 for (int i = 0; i < nb_mvs; i++) {
842 ret = read_code(gb, &skip, &value, &map, 1);
843 if (ret < 0)
844 return ret;
845 s->mvectors[i].y = value;
846 i += skip;
847 }
848
849 if (get_bits_left(gb) <= 0)
850 return AVERROR_INVALIDDATA;
851 skip = (get_bits_count(gb) >> 3) + 1;
852 bytestream2_skip(&s->gbyte, skip);
853
854 return 0;
855 }
856
857 static int decode_inter(AVCodecContext *avctx, GetBitContext *gb,
858 AVFrame *frame, AVFrame *prev)
859 {
860 AGMContext *s = avctx->priv_data;
861 int ret;
862
863 compute_quant_matrix(s, (2 * s->compression - 100) / 100.0);
864
865 if (s->flags & 2) {
866 ret = decode_motion_vectors(avctx, gb);
867 if (ret < 0)
868 return ret;
869 }
870
871 s->blocks_w = avctx->coded_width >> 3;
872 s->blocks_h = avctx->coded_height >> 3;
873
874 ret = decode_inter_plane(s, gb, s->size[0], s->luma_quant_matrix, frame, prev, 0);
875 if (ret < 0)
876 return ret;
877
878 bytestream2_skip(&s->gbyte, s->size[0]);
879
880 s->blocks_w = avctx->coded_width >> 4;
881 s->blocks_h = avctx->coded_height >> 4;
882
883 ret = decode_inter_plane(s, gb, s->size[1], s->chroma_quant_matrix, frame, prev, 2);
884 if (ret < 0)
885 return ret;
886
887 bytestream2_skip(&s->gbyte, s->size[1]);
888
889 s->blocks_w = avctx->coded_width >> 4;
890 s->blocks_h = avctx->coded_height >> 4;
891
892 ret = decode_inter_plane(s, gb, s->size[2], s->chroma_quant_matrix, frame, prev, 1);
893 if (ret < 0)
894 return ret;
895
896 return 0;
897 }
898
899 typedef struct Node {
900 int parent;
901 int child[2];
902 } Node;
903
904 static void get_tree_codes(uint32_t *codes, Node *nodes, int idx, uint32_t pfx, int bitpos)
905 {
906 if (idx < 256 && idx >= 0) {
907 codes[idx] = pfx;
908 } else if (idx >= 0) {
909 get_tree_codes(codes, nodes, nodes[idx].child[0], pfx + (0 << bitpos), bitpos + 1);
910 get_tree_codes(codes, nodes, nodes[idx].child[1], pfx + (1U << bitpos), bitpos + 1);
911 }
912 }
913
914 static int make_new_tree(const uint8_t *bitlens, uint32_t *codes)
915 {
916 int zlcount = 0, curlen, idx, nindex, last, llast;
917 int blcounts[32] = { 0 };
918 int syms[8192];
919 Node nodes[512];
920 int node_idx[1024];
921 int old_idx[512];
922
923 for (int i = 0; i < 256; i++) {
924 int bitlen = bitlens[i];
925 int blcount = blcounts[bitlen];
926
927 zlcount += bitlen < 1;
928 syms[(bitlen << 8) + blcount] = i;
929 blcounts[bitlen]++;
930 }
931
932 for (int i = 0; i < 512; i++) {
933 nodes[i].child[0] = -1;
934 nodes[i].child[1] = -1;
935 }
936
937 for (int i = 0; i < 256; i++) {
938 node_idx[i] = 257 + i;
939 }
940
941 curlen = 1;
942 node_idx[512] = 256;
943 last = 255;
944 nindex = 1;
945
946 for (curlen = 1; curlen < 32; curlen++) {
947 if (blcounts[curlen] > 0) {
948 int max_zlcount = zlcount + blcounts[curlen];
949
950 for (int i = 0; zlcount < 256 && zlcount < max_zlcount; zlcount++, i++) {
951 int p = node_idx[nindex - 1 + 512];
952 int ch = syms[256 * curlen + i];
953
954 if (nindex <= 0)
955 return AVERROR_INVALIDDATA;
956
957 if (nodes[p].child[0] == -1) {
958 nodes[p].child[0] = ch;
959 } else {
960 nodes[p].child[1] = ch;
961 nindex--;
962 }
963 nodes[ch].parent = p;
964 }
965 }
966 llast = last - 1;
967 idx = 0;
968 while (nindex > 0) {
969 int p, ch;
970
971 last = llast - idx;
972 p = node_idx[nindex - 1 + 512];
973 ch = node_idx[last];
974 if (nodes[p].child[0] == -1) {
975 nodes[p].child[0] = ch;
976 } else {
977 nodes[p].child[1] = ch;
978 nindex--;
979 }
980 old_idx[idx] = ch;
981 nodes[ch].parent = p;
982 if (idx == llast)
983 goto next;
984 idx++;
985 if (nindex <= 0) {
986 for (int i = 0; i < idx; i++)
987 node_idx[512 + i] = old_idx[i];
988 }
989 }
990 nindex = idx;
991 }
992
993 next:
994
995 get_tree_codes(codes, nodes, 256, 0, 0);
996 return 0;
997 }
998
999 static int build_huff(const uint8_t *bitlen, VLC *vlc)
1000 {
1001 uint32_t new_codes[256];
1002 uint8_t bits[256];
1003 uint8_t symbols[256];
1004 uint32_t codes[256];
1005 int nb_codes = 0;
1006
1007 int ret = make_new_tree(bitlen, new_codes);
1008 if (ret < 0)
1009 return ret;
1010
1011 for (int i = 0; i < 256; i++) {
1012 if (bitlen[i]) {
1013 bits[nb_codes] = bitlen[i];
1014 codes[nb_codes] = new_codes[i];
1015 symbols[nb_codes] = i;
1016 nb_codes++;
1017 }
1018 }
1019
1020 ff_vlc_free(vlc);
1021 return ff_vlc_init_sparse(vlc, 13, nb_codes,
1022 bits, 1, 1,
1023 codes, 4, 4,
1024 symbols, 1, 1,
1025 VLC_INIT_LE);
1026 }
1027
1028 static int decode_huffman2(AVCodecContext *avctx, int header, int size)
1029 {
1030 AGMContext *s = avctx->priv_data;
1031 GetBitContext *gb = &s->gb;
1032 uint8_t lens[256];
1033 int ret, x, len;
1034
1035 if ((ret = init_get_bits8(gb, s->gbyte.buffer,
1036 bytestream2_get_bytes_left(&s->gbyte))) < 0)
1037 return ret;
1038
1039 s->output_size = get_bits_long(gb, 32);
1040
1041 if (s->output_size > avctx->width * avctx->height * 9LL + 10000)
1042 return AVERROR_INVALIDDATA;
1043
1044 av_fast_padded_malloc(&s->output, &s->padded_output_size, s->output_size);
1045 if (!s->output)
1046 return AVERROR(ENOMEM);
1047
1048 x = get_bits(gb, 1);
1049 len = 4 + get_bits(gb, 1);
1050 if (x) {
1051 int cb[8] = { 0 };
1052 int count = get_bits(gb, 3) + 1;
1053
1054 for (int i = 0; i < count; i++)
1055 cb[i] = get_bits(gb, len);
1056
1057 for (int i = 0; i < 256; i++) {
1058 int idx = get_bits(gb, 3);
1059 lens[i] = cb[idx];
1060 }
1061 } else {
1062 for (int i = 0; i < 256; i++)
1063 lens[i] = get_bits(gb, len);
1064 }
1065
1066 if ((ret = build_huff(lens, &s->vlc)) < 0)
1067 return ret;
1068
1069 x = 0;
1070 while (get_bits_left(gb) > 0 && x < s->output_size) {
1071 int val = get_vlc2(gb, s->vlc.table, s->vlc.bits, 3);
1072 if (val < 0)
1073 return AVERROR_INVALIDDATA;
1074 s->output[x++] = val;
1075 }
1076
1077 return 0;
1078 }
1079
1080 static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
1081 int *got_frame, AVPacket *avpkt)
1082 {
1083 AGMContext *s = avctx->priv_data;
1084 GetBitContext *gb = &s->gb;
1085 GetByteContext *gbyte = &s->gbyte;
1086 int w, h, width, height, header;
1087 unsigned compressed_size;
1088 long skip;
1089 int ret;
1090
1091 if (!avpkt->size)
1092 return 0;
1093
1094 bytestream2_init(gbyte, avpkt->data, avpkt->size);
1095
1096 header = bytestream2_get_le32(gbyte);
1097 s->fflags = bytestream2_get_le32(gbyte);
1098 s->bitstream_size = s->fflags & 0x1FFFFFFF;
1099 s->fflags >>= 29;
1100 av_log(avctx, AV_LOG_DEBUG, "fflags: %X\n", s->fflags);
1101 if (avpkt->size < s->bitstream_size + 8)
1102 return AVERROR_INVALIDDATA;
1103
1104 s->key_frame = (avpkt->flags & AV_PKT_FLAG_KEY);
1105 if (s->key_frame)
1106 frame->flags |= AV_FRAME_FLAG_KEY;
1107 else
1108 frame->flags &= ~AV_FRAME_FLAG_KEY;
1109 frame->pict_type = s->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
1110
1111 if (!s->key_frame) {
1112 if (!s->prev_frame->data[0]) {
1113 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
1114 return AVERROR_INVALIDDATA;
1115 }
1116 }
1117
1118 if (header) {
1119 if (avctx->codec_tag == MKTAG('A', 'G', 'M', '0') ||
1120 avctx->codec_tag == MKTAG('A', 'G', 'M', '1'))
1121 return AVERROR_PATCHWELCOME;
1122 else
1123 ret = decode_huffman2(avctx, header, (avpkt->size - s->bitstream_size) - 8);
1124 if (ret < 0)
1125 return ret;
1126 bytestream2_init(gbyte, s->output, s->output_size);
1127 } else if (!s->dct) {
1128 bytestream2_skip(gbyte, 4);
1129 }
1130
1131 if (s->dct) {
1132 s->flags = 0;
1133 w = bytestream2_get_le32(gbyte);
1134 h = bytestream2_get_le32(gbyte);
1135 if (w == INT32_MIN || h == INT32_MIN)
1136 return AVERROR_INVALIDDATA;
1137 if (w < 0) {
1138 w = -w;
1139 s->flags |= 2;
1140 }
1141 if (h < 0) {
1142 h = -h;
1143 s->flags |= 1;
1144 }
1145
1146 width = avctx->width;
1147 height = avctx->height;
1148 if (w < width || h < height || w & 7 || h & 7)
1149 return AVERROR_INVALIDDATA;
1150
1151 ret = ff_set_dimensions(avctx, w, h);
1152 if (ret < 0)
1153 return ret;
1154 avctx->width = width;
1155 avctx->height = height;
1156
1157 s->compression = bytestream2_get_le32(gbyte);
1158 if (s->compression < 0 || s->compression > 100)
1159 return AVERROR_INVALIDDATA;
1160
1161 for (int i = 0; i < 3; i++)
1162 s->size[i] = bytestream2_get_le32(gbyte);
1163 if (header) {
1164 compressed_size = s->output_size;
1165 skip = 8LL;
1166 } else {
1167 compressed_size = avpkt->size;
1168 skip = 32LL;
1169 }
1170 if (s->size[0] < 0 || s->size[1] < 0 || s->size[2] < 0 ||
1171 skip + s->size[0] + s->size[1] + s->size[2] > compressed_size) {
1172 return AVERROR_INVALIDDATA;
1173 }
1174 }
1175
1176 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1177 return ret;
1178
1179 if (frame->flags & AV_FRAME_FLAG_KEY) {
1180 if (!s->dct && !s->rgb)
1181 ret = decode_raw_intra(avctx, gbyte, frame);
1182 else if (!s->dct && s->rgb)
1183 ret = decode_raw_intra_rgb(avctx, gbyte, frame);
1184 else
1185 ret = decode_intra(avctx, gb, frame);
1186 } else {
1187 if (s->prev_frame-> width != frame->width ||
1188 s->prev_frame->height != frame->height)
1189 return AVERROR_INVALIDDATA;
1190
1191 if (!(s->flags & 2)) {
1192 ret = av_frame_copy(frame, s->prev_frame);
1193 if (ret < 0)
1194 return ret;
1195 }
1196
1197 if (s->dct) {
1198 ret = decode_inter(avctx, gb, frame, s->prev_frame);
1199 } else if (!s->dct && !s->rgb) {
1200 ret = decode_runlen(avctx, gbyte, frame);
1201 } else {
1202 ret = decode_runlen_rgb(avctx, gbyte, frame);
1203 }
1204 }
1205 if (ret < 0)
1206 return ret;
1207
1208 if ((ret = av_frame_replace(s->prev_frame, frame)) < 0)
1209 return ret;
1210
1211 frame->crop_top = avctx->coded_height - avctx->height;
1212 frame->crop_left = avctx->coded_width - avctx->width;
1213
1214 *got_frame = 1;
1215
1216 return avpkt->size;
1217 }
1218
1219 static av_cold int decode_init(AVCodecContext *avctx)
1220 {
1221 AGMContext *s = avctx->priv_data;
1222
1223 s->rgb = avctx->codec_tag == MKTAG('A', 'G', 'M', '4');
1224 avctx->pix_fmt = s->rgb ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUV420P;
1225 s->avctx = avctx;
1226 s->plus = avctx->codec_tag == MKTAG('A', 'G', 'M', '3') ||
1227 avctx->codec_tag == MKTAG('A', 'G', 'M', '7');
1228
1229 s->dct = avctx->codec_tag != MKTAG('A', 'G', 'M', '4') &&
1230 avctx->codec_tag != MKTAG('A', 'G', 'M', '5');
1231
1232 if (!s->rgb && !s->dct) {
1233 if ((avctx->width & 1) || (avctx->height & 1))
1234 return AVERROR_INVALIDDATA;
1235 }
1236
1237 avctx->idct_algo = FF_IDCT_SIMPLE;
1238 ff_idctdsp_init(&s->idsp, avctx);
1239 ff_permute_scantable(s->permutated_scantable, ff_zigzag_direct,
1240 s->idsp.idct_permutation);
1241
1242 s->prev_frame = av_frame_alloc();
1243 if (!s->prev_frame)
1244 return AVERROR(ENOMEM);
1245
1246 return 0;
1247 }
1248
1249 static av_cold void decode_flush(AVCodecContext *avctx)
1250 {
1251 AGMContext *s = avctx->priv_data;
1252
1253 av_frame_unref(s->prev_frame);
1254 }
1255
1256 static av_cold int decode_close(AVCodecContext *avctx)
1257 {
1258 AGMContext *s = avctx->priv_data;
1259
1260 ff_vlc_free(&s->vlc);
1261 av_frame_free(&s->prev_frame);
1262 av_freep(&s->mvectors);
1263 s->mvectors_size = 0;
1264 av_freep(&s->wblocks);
1265 s->wblocks_size = 0;
1266 av_freep(&s->output);
1267 s->padded_output_size = 0;
1268 av_freep(&s->map);
1269 s->map_size = 0;
1270
1271 return 0;
1272 }
1273
1274 const FFCodec ff_agm_decoder = {
1275 .p.name = "agm",
1276 CODEC_LONG_NAME("Amuse Graphics Movie"),
1277 .p.type = AVMEDIA_TYPE_VIDEO,
1278 .p.id = AV_CODEC_ID_AGM,
1279 .p.capabilities = AV_CODEC_CAP_DR1,
1280 .priv_data_size = sizeof(AGMContext),
1281 .init = decode_init,
1282 .close = decode_close,
1283 FF_CODEC_DECODE_CB(decode_frame),
1284 .flush = decode_flush,
1285 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
1286 FF_CODEC_CAP_EXPORTS_CROPPING,
1287 };
1288