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