You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1290 lines
38KB

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