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.

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