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.

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