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.

547 lines
18KB

  1. /*
  2. * Infinity IMM4 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. #include "libavutil/thread.h"
  26. #include "avcodec.h"
  27. #include "bswapdsp.h"
  28. #include "copy_block.h"
  29. #include "get_bits.h"
  30. #include "idctdsp.h"
  31. #include "internal.h"
  32. #define CBPLO_VLC_BITS 6
  33. #define BLKTYPE_VLC_BITS 9
  34. #define BLOCK_VLC_BITS 12
  35. typedef struct IMM4Context {
  36. BswapDSPContext bdsp;
  37. GetBitContext gb;
  38. AVFrame *prev_frame;
  39. uint8_t *bitstream;
  40. int bitstream_size;
  41. int factor;
  42. unsigned lo;
  43. unsigned hi;
  44. ScanTable intra_scantable;
  45. DECLARE_ALIGNED(32, int16_t, block)[6][64];
  46. IDCTDSPContext idsp;
  47. } IMM4Context;
  48. static const uint8_t intra_cb[] = {
  49. 24, 18, 12
  50. };
  51. static const uint8_t inter_cb[] = {
  52. 30, 20, 15
  53. };
  54. static const uint8_t cbplo[][2] = {
  55. { 0,-6 }, { 0x01, 6 }, { 0x02, 6 }, { 0x03, 6 }, { 0x00, 4 },
  56. { 0x01, 3 }, { 0x02, 3 }, { 0x03, 3 }, { 0x00, 1 },
  57. };
  58. static const uint8_t cbphi_bits[] = {
  59. 4, 5, 5, 4, 5, 4, 6, 4, 5, 6, 4, 4, 4, 4, 4, 2
  60. };
  61. static const uint8_t cbphi_codes[] = {
  62. 3, 5, 4, 9, 3, 7, 2, 11, 2, 3, 5, 10, 4, 8, 6, 3
  63. };
  64. static const uint8_t blktype[][2] = {
  65. { 0,-8 }, { 0x34, 9 }, { 0,-9 }, { 0x14, 9 }, { 0,-9 },
  66. { 0x23, 8 }, { 0x13, 8 }, { 0x32, 8 }, { 0x33, 7 }, { 0x22, 7 },
  67. { 0x12, 7 }, { 0x21, 7 }, { 0x11, 7 }, { 0x04, 6 }, { 0x30, 6 },
  68. { 0x03, 5 }, { 0x20, 4 }, { 0x10, 4 }, { 0x02, 3 }, { 0x01, 3 },
  69. { 0x00, 1 },
  70. };
  71. static const uint16_t block_symbols[] = {
  72. 0, 0x4082, 0x4003, 0x000B, 0x000A, 0x4E01, 0x4D81, 0x4D01, 0x4C81,
  73. 0x0482, 0x0402, 0x0382, 0x0302, 0x0282, 0x0183, 0x0103, 0x0084, 0x000C,
  74. 0x0085, 0x0B81, 0x0C01, 0x4E81, 0x4F01, 0x4F81, 0x5001, 0x0086, 0x0104,
  75. 0x0203, 0x0283, 0x0303, 0x0502, 0x0C81, 0x0D01, 0x5081, 0x5101, 0x5181,
  76. 0x5201, 0x5281, 0x5301, 0x5381, 0x5401, 0x0000, 0x0009, 0x0008, 0x4C01,
  77. 0x4B81, 0x4B01, 0x4A81, 0x4A01, 0x4981, 0x4901, 0x4881, 0x4002, 0x0B01,
  78. 0x0A81, 0x0A01, 0x0981, 0x0901, 0x0881, 0x0801, 0x0781, 0x0202, 0x0182,
  79. 0x0007, 0x0006, 0x4801, 0x4781, 0x4701, 0x4681, 0x4601, 0x4581, 0x4501,
  80. 0x4481, 0x0701, 0x0681, 0x0102, 0x0083, 0x0005, 0x4401, 0x4381, 0x4301,
  81. 0x4281, 0x0601, 0x0581, 0x0501, 0x0004, 0x4201, 0x4181, 0x4101, 0x4081,
  82. 0x0481, 0x0401, 0x0381, 0x0301, 0x0082, 0x0003, 0x0281, 0x0201, 0x0181,
  83. 0x4001, 0x0001, 0x0081, 0x0101, 0x0002,
  84. };
  85. static const uint8_t block_bits[] = {
  86. -9, 11, 11, 11, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11,
  87. 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  88. 12, 12, 12, 7, 10, 10, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  89. 9, 9, 9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  90. 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  91. 6, 5, 5, 5, 4, 2, 3, 4, 4,
  92. };
  93. static VLC cbplo_tab;
  94. static VLC cbphi_tab;
  95. static VLC blktype_tab;
  96. static VLC block_tab;
  97. static int get_cbphi(GetBitContext *gb, int x)
  98. {
  99. int value;
  100. value = get_vlc2(gb, cbphi_tab.table, cbphi_tab.bits, 1);
  101. if (value < 0)
  102. return AVERROR_INVALIDDATA;
  103. return x ? value : 15 - value;
  104. }
  105. static int decode_block(AVCodecContext *avctx, GetBitContext *gb,
  106. int block, int factor, int flag, int offset, int flag2)
  107. {
  108. IMM4Context *s = avctx->priv_data;
  109. const uint8_t *scantable = s->intra_scantable.permutated;
  110. int i, last, len, factor2;
  111. for (i = !flag; i < 64; i++) {
  112. int value;
  113. value = get_vlc2(gb, block_tab.table, block_tab.bits, 1);
  114. if (value < 0)
  115. return AVERROR_INVALIDDATA;
  116. if (value == 0) {
  117. last = get_bits1(gb);
  118. len = get_bits(gb, 6);
  119. factor2 = get_sbits(gb, 8);
  120. } else {
  121. factor2 = value & 0x7F;
  122. last = (value >> 14) & 1;
  123. len = (value >> 7) & 0x3F;
  124. if (get_bits1(gb))
  125. factor2 = -factor2;
  126. }
  127. i += len;
  128. if (i >= 64)
  129. break;
  130. s->block[block][scantable[i]] = offset * (factor2 < 0 ? -1 : 1) + factor * factor2;
  131. if (last)
  132. break;
  133. }
  134. if (s->hi == 2 && flag2 && block < 4) {
  135. if (flag)
  136. s->block[block][scantable[0]] *= 2;
  137. s->block[block][scantable[1]] *= 2;
  138. s->block[block][scantable[8]] *= 2;
  139. s->block[block][scantable[16]] *= 2;
  140. }
  141. return 0;
  142. }
  143. static int decode_blocks(AVCodecContext *avctx, GetBitContext *gb,
  144. unsigned cbp, int flag, int offset, unsigned flag2)
  145. {
  146. IMM4Context *s = avctx->priv_data;
  147. const uint8_t *scantable = s->intra_scantable.permutated;
  148. int ret, i;
  149. memset(s->block, 0, sizeof(s->block));
  150. for (i = 0; i < 6; i++) {
  151. if (!flag) {
  152. int x = get_bits(gb, 8);
  153. if (x == 255)
  154. x = 128;
  155. x *= 8;
  156. s->block[i][scantable[0]] = x;
  157. }
  158. if (cbp & (1 << (5 - i))) {
  159. ret = decode_block(avctx, gb, i, s->factor, flag, offset, flag2);
  160. if (ret < 0)
  161. return ret;
  162. }
  163. }
  164. return 0;
  165. }
  166. static int decode_intra(AVCodecContext *avctx, GetBitContext *gb, AVFrame *frame)
  167. {
  168. IMM4Context *s = avctx->priv_data;
  169. int ret, x, y, offset = 0;
  170. if (s->hi == 0) {
  171. if (s->lo > 2)
  172. return AVERROR_INVALIDDATA;
  173. s->factor = intra_cb[s->lo];
  174. } else {
  175. s->factor = s->lo * 2;
  176. }
  177. if (s->hi) {
  178. offset = s->factor;
  179. offset >>= 1;
  180. if (!(offset & 1))
  181. offset--;
  182. }
  183. for (y = 0; y < avctx->height; y += 16) {
  184. for (x = 0; x < avctx->width; x += 16) {
  185. unsigned flag, cbphi, cbplo;
  186. cbplo = get_vlc2(gb, cbplo_tab.table, CBPLO_VLC_BITS, 1);
  187. flag = get_bits1(gb);
  188. cbphi = get_cbphi(gb, 1);
  189. ret = decode_blocks(avctx, gb, cbplo | (cbphi << 2), 0, offset, flag);
  190. if (ret < 0)
  191. return ret;
  192. s->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x,
  193. frame->linesize[0], s->block[0]);
  194. s->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x + 8,
  195. frame->linesize[0], s->block[1]);
  196. s->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x,
  197. frame->linesize[0], s->block[2]);
  198. s->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x + 8,
  199. frame->linesize[0], s->block[3]);
  200. s->idsp.idct_put(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1),
  201. frame->linesize[1], s->block[4]);
  202. s->idsp.idct_put(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1),
  203. frame->linesize[2], s->block[5]);
  204. }
  205. }
  206. return 0;
  207. }
  208. static int decode_inter(AVCodecContext *avctx, GetBitContext *gb,
  209. AVFrame *frame, AVFrame *prev)
  210. {
  211. IMM4Context *s = avctx->priv_data;
  212. int ret, x, y, offset = 0;
  213. if (s->hi == 0) {
  214. if (s->lo > 2)
  215. return AVERROR_INVALIDDATA;
  216. s->factor = inter_cb[s->lo];
  217. } else {
  218. s->factor = s->lo * 2;
  219. }
  220. if (s->hi) {
  221. offset = s->factor;
  222. offset >>= 1;
  223. if (!(offset & 1))
  224. offset--;
  225. }
  226. for (y = 0; y < avctx->height; y += 16) {
  227. for (x = 0; x < avctx->width; x += 16) {
  228. int reverse, intra_block, value;
  229. unsigned cbphi, cbplo, flag2 = 0;
  230. if (get_bits1(gb)) {
  231. copy_block16(frame->data[0] + y * frame->linesize[0] + x,
  232. prev->data[0] + y * prev->linesize[0] + x,
  233. frame->linesize[0], prev->linesize[0], 16);
  234. copy_block8(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1),
  235. prev->data[1] + (y >> 1) * prev->linesize[1] + (x >> 1),
  236. frame->linesize[1], prev->linesize[1], 8);
  237. copy_block8(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1),
  238. prev->data[2] + (y >> 1) * prev->linesize[2] + (x >> 1),
  239. frame->linesize[2], prev->linesize[2], 8);
  240. continue;
  241. }
  242. value = get_vlc2(gb, blktype_tab.table, blktype_tab.bits, 1);
  243. if (value < 0)
  244. return AVERROR_INVALIDDATA;
  245. intra_block = value & 0x07;
  246. reverse = intra_block == 3;
  247. if (reverse)
  248. flag2 = get_bits1(gb);
  249. cbplo = value >> 4;
  250. cbphi = get_cbphi(gb, reverse);
  251. if (intra_block) {
  252. ret = decode_blocks(avctx, gb, cbplo | (cbphi << 2), 0, offset, flag2);
  253. if (ret < 0)
  254. return ret;
  255. s->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x,
  256. frame->linesize[0], s->block[0]);
  257. s->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x + 8,
  258. frame->linesize[0], s->block[1]);
  259. s->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x,
  260. frame->linesize[0], s->block[2]);
  261. s->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x + 8,
  262. frame->linesize[0], s->block[3]);
  263. s->idsp.idct_put(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1),
  264. frame->linesize[1], s->block[4]);
  265. s->idsp.idct_put(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1),
  266. frame->linesize[2], s->block[5]);
  267. } else {
  268. flag2 = get_bits1(gb);
  269. skip_bits1(gb);
  270. ret = decode_blocks(avctx, gb, cbplo | (cbphi << 2), 1, offset, flag2);
  271. if (ret < 0)
  272. return ret;
  273. copy_block16(frame->data[0] + y * frame->linesize[0] + x,
  274. prev->data[0] + y * prev->linesize[0] + x,
  275. frame->linesize[0], prev->linesize[0], 16);
  276. copy_block8(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1),
  277. prev->data[1] + (y >> 1) * prev->linesize[1] + (x >> 1),
  278. frame->linesize[1], prev->linesize[1], 8);
  279. copy_block8(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1),
  280. prev->data[2] + (y >> 1) * prev->linesize[2] + (x >> 1),
  281. frame->linesize[2], prev->linesize[2], 8);
  282. s->idsp.idct_add(frame->data[0] + y * frame->linesize[0] + x,
  283. frame->linesize[0], s->block[0]);
  284. s->idsp.idct_add(frame->data[0] + y * frame->linesize[0] + x + 8,
  285. frame->linesize[0], s->block[1]);
  286. s->idsp.idct_add(frame->data[0] + (y + 8) * frame->linesize[0] + x,
  287. frame->linesize[0], s->block[2]);
  288. s->idsp.idct_add(frame->data[0] + (y + 8) * frame->linesize[0] + x + 8,
  289. frame->linesize[0], s->block[3]);
  290. s->idsp.idct_add(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1),
  291. frame->linesize[1], s->block[4]);
  292. s->idsp.idct_add(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1),
  293. frame->linesize[2], s->block[5]);
  294. }
  295. }
  296. }
  297. return 0;
  298. }
  299. static int decode_frame(AVCodecContext *avctx, void *data,
  300. int *got_frame, AVPacket *avpkt)
  301. {
  302. IMM4Context *s = avctx->priv_data;
  303. GetBitContext *gb = &s->gb;
  304. AVFrame *frame = data;
  305. int width, height;
  306. unsigned type;
  307. int ret, scaled;
  308. if (avpkt->size <= 32)
  309. return AVERROR_INVALIDDATA;
  310. av_fast_padded_malloc(&s->bitstream, &s->bitstream_size,
  311. FFALIGN(avpkt->size, 4));
  312. if (!s->bitstream)
  313. return AVERROR(ENOMEM);
  314. s->bdsp.bswap_buf((uint32_t *)s->bitstream,
  315. (uint32_t *)avpkt->data,
  316. (avpkt->size + 3) >> 2);
  317. if ((ret = init_get_bits8(gb, s->bitstream, FFALIGN(avpkt->size, 4))) < 0)
  318. return ret;
  319. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  320. avctx->color_range = AVCOL_RANGE_JPEG;
  321. width = avctx->width;
  322. height = avctx->height;
  323. scaled = avpkt->data[8];
  324. if (scaled < 2) {
  325. int mode = avpkt->data[10];
  326. switch (mode) {
  327. case 1:
  328. width = 352;
  329. height = 240;
  330. break;
  331. case 2:
  332. width = 704;
  333. height = 240;
  334. break;
  335. case 4:
  336. width = 480;
  337. height = 704;
  338. break;
  339. case 17:
  340. width = 352;
  341. height = 288;
  342. break;
  343. case 18:
  344. width = 704;
  345. height = 288;
  346. break;
  347. default:
  348. width = 704;
  349. height = 576;
  350. break;
  351. }
  352. }
  353. skip_bits_long(gb, 24 * 8);
  354. type = get_bits_long(gb, 32);
  355. s->hi = get_bits(gb, 16);
  356. s->lo = get_bits(gb, 16);
  357. switch (type) {
  358. case 0x19781977:
  359. frame->key_frame = 1;
  360. frame->pict_type = AV_PICTURE_TYPE_I;
  361. break;
  362. case 0x12250926:
  363. frame->key_frame = 0;
  364. frame->pict_type = AV_PICTURE_TYPE_P;
  365. break;
  366. default:
  367. avpriv_request_sample(avctx, "type %X", type);
  368. return AVERROR_PATCHWELCOME;
  369. }
  370. if (avctx->width != width ||
  371. avctx->height != height) {
  372. if (!frame->key_frame) {
  373. av_log(avctx, AV_LOG_ERROR, "Frame size change is unsupported.\n");
  374. return AVERROR_INVALIDDATA;
  375. }
  376. av_frame_unref(s->prev_frame);
  377. }
  378. ret = ff_set_dimensions(avctx, width, height);
  379. if (ret < 0)
  380. return ret;
  381. if ((ret = ff_get_buffer(avctx, frame, frame->key_frame ? AV_GET_BUFFER_FLAG_REF : 0)) < 0)
  382. return ret;
  383. if (frame->key_frame) {
  384. ret = decode_intra(avctx, gb, frame);
  385. if (ret < 0)
  386. return ret;
  387. av_frame_unref(s->prev_frame);
  388. if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
  389. return ret;
  390. } else {
  391. if (!s->prev_frame->data[0]) {
  392. av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
  393. return AVERROR_INVALIDDATA;
  394. }
  395. ret = decode_inter(avctx, gb, frame, s->prev_frame);
  396. if (ret < 0)
  397. return ret;
  398. }
  399. *got_frame = 1;
  400. return avpkt->size;
  401. }
  402. static av_cold void imm4_init_static_data(void)
  403. {
  404. INIT_VLC_STATIC_FROM_LENGTHS(&cbplo_tab, CBPLO_VLC_BITS, FF_ARRAY_ELEMS(cbplo),
  405. &cbplo[0][1], 2, &cbplo[0][0], 2, 1,
  406. 0, 0, 1 << CBPLO_VLC_BITS);
  407. INIT_VLC_SPARSE_STATIC(&cbphi_tab, 6, FF_ARRAY_ELEMS(cbphi_bits),
  408. cbphi_bits, 1, 1, cbphi_codes, 1, 1, NULL, 0, 0, 64);
  409. INIT_VLC_STATIC_FROM_LENGTHS(&blktype_tab, BLKTYPE_VLC_BITS, FF_ARRAY_ELEMS(blktype),
  410. &blktype[0][1], 2, &blktype[0][0], 2, 1,
  411. 0, 0, 1 << BLKTYPE_VLC_BITS);
  412. INIT_VLC_STATIC_FROM_LENGTHS(&block_tab, BLOCK_VLC_BITS, FF_ARRAY_ELEMS(block_bits),
  413. block_bits, 1, block_symbols, 2, 2,
  414. 0, 0, 1 << BLOCK_VLC_BITS);
  415. }
  416. static av_cold int decode_init(AVCodecContext *avctx)
  417. {
  418. static AVOnce init_static_once = AV_ONCE_INIT;
  419. IMM4Context *s = avctx->priv_data;
  420. uint8_t table[64];
  421. for (int i = 0; i < 64; i++)
  422. table[i] = i;
  423. ff_bswapdsp_init(&s->bdsp);
  424. ff_idctdsp_init(&s->idsp, avctx);
  425. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, table);
  426. s->prev_frame = av_frame_alloc();
  427. if (!s->prev_frame)
  428. return AVERROR(ENOMEM);
  429. ff_thread_once(&init_static_once, imm4_init_static_data);
  430. return 0;
  431. }
  432. static void decode_flush(AVCodecContext *avctx)
  433. {
  434. IMM4Context *s = avctx->priv_data;
  435. av_frame_unref(s->prev_frame);
  436. }
  437. static av_cold int decode_close(AVCodecContext *avctx)
  438. {
  439. IMM4Context *s = avctx->priv_data;
  440. av_frame_free(&s->prev_frame);
  441. av_freep(&s->bitstream);
  442. s->bitstream_size = 0;
  443. return 0;
  444. }
  445. AVCodec ff_imm4_decoder = {
  446. .name = "imm4",
  447. .long_name = NULL_IF_CONFIG_SMALL("Infinity IMM4"),
  448. .type = AVMEDIA_TYPE_VIDEO,
  449. .id = AV_CODEC_ID_IMM4,
  450. .priv_data_size = sizeof(IMM4Context),
  451. .init = decode_init,
  452. .close = decode_close,
  453. .decode = decode_frame,
  454. .flush = decode_flush,
  455. .capabilities = AV_CODEC_CAP_DR1,
  456. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  457. FF_CODEC_CAP_INIT_CLEANUP,
  458. };