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.

543 lines
17KB

  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. typedef struct IMM4Context {
  33. BswapDSPContext bdsp;
  34. GetBitContext gb;
  35. AVFrame *prev_frame;
  36. uint8_t *bitstream;
  37. int bitstream_size;
  38. int changed_size;
  39. int factor;
  40. unsigned lo;
  41. unsigned hi;
  42. ScanTable intra_scantable;
  43. DECLARE_ALIGNED(32, int16_t, block)[6][64];
  44. IDCTDSPContext idsp;
  45. } IMM4Context;
  46. static const uint8_t intra_cb[] = {
  47. 24, 18, 12
  48. };
  49. static const uint8_t inter_cb[] = {
  50. 30, 20, 15
  51. };
  52. static const uint8_t cbplo_symbols[] = {
  53. 3, 4, 19, 20, 35, 36, 51, 52
  54. };
  55. static const uint8_t cbplo_bits[] = {
  56. 1, 4, 3, 6, 3, 6, 3, 6
  57. };
  58. static const uint8_t cbplo_codes[] = {
  59. 1, 1, 1, 1, 2, 2, 3, 3
  60. };
  61. static const uint8_t cbphi_bits[] = {
  62. 4, 5, 5, 4, 5, 4, 6, 4, 5, 6, 4, 4, 4, 4, 4, 2
  63. };
  64. static const uint8_t cbphi_codes[] = {
  65. 3, 5, 4, 9, 3, 7, 2, 11, 2, 3, 5, 10, 4, 8, 6, 3
  66. };
  67. static const uint8_t blktype_symbols[] = {
  68. 0, 1, 2, 3, 4, 16, 17, 18, 19, 20, 32, 33, 34, 35, 48, 50, 51, 52
  69. };
  70. static const uint8_t blktype_bits[] = {
  71. 1, 3, 3, 5, 6, 4, 7, 7, 8, 9, 4, 7, 7, 8, 6, 8, 7, 9
  72. };
  73. static const uint8_t blktype_codes[] = {
  74. 1, 3, 2, 3, 4, 3, 7, 5, 4, 4, 2, 6, 4, 3, 5, 5, 3, 2
  75. };
  76. static const uint16_t block_symbols[] = {
  77. 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0x81, 0x82, 0x83,
  78. 0x84, 0x85, 0x86, 0x101, 0x102, 0x103, 0x104, 0x181, 0x182, 0x183, 0x201, 0x202,
  79. 0x203, 0x281, 0x282, 0x283, 0x301, 0x302, 0x303, 0x381, 0x382, 0x401, 0x402,
  80. 0x481, 0x482, 0x501, 0x502, 0x581, 0x601, 0x681, 0x701, 0x781, 0x801, 0x881,
  81. 0x901, 0x981, 0xA01, 0xA81, 0xB01, 0xB81, 0xC01, 0xC81, 0xD01, 0x4001, 0x4002,
  82. 0x4003, 0x4081, 0x4082, 0x4101, 0x4181, 0x4201, 0x4281, 0x4301, 0x4381, 0x4401,
  83. 0x4481, 0x4501, 0x4581, 0x4601, 0x4681, 0x4701, 0x4781, 0x4801, 0x4881, 0x4901,
  84. 0x4981, 0x4A01, 0x4A81, 0x4B01, 0x4B81, 0x4C01, 0x4C81, 0x4D01, 0x4D81, 0x4E01,
  85. 0x4E81, 0x4F01, 0x4F81, 0x5001, 0x5081, 0x5101, 0x5181, 0x5201, 0x5281, 0x5301,
  86. 0x5381, 0x5401
  87. };
  88. static const uint8_t block_bits[] = {
  89. 7, 2, 4, 6, 7, 8, 9, 9, 10, 10, 11, 11, 11, 3, 6, 8, 10, 11, 12, 4, 8,
  90. 10, 12, 5, 9, 10, 5, 9, 12, 5, 10, 12, 6, 10, 12, 6, 10, 6, 10, 6,
  91. 10, 7, 12, 7, 7, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 11, 11, 12, 12, 4, 9,
  92. 11, 6, 11, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9,
  93. 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12,
  94. 12, 12
  95. };
  96. static const uint8_t block_codes[] = {
  97. 3, 2, 15, 21, 23, 31, 37, 36, 33, 32, 7, 6, 32, 6, 20, 30, 15, 33, 80,
  98. 14, 29, 14, 81, 13, 35, 13, 12, 34, 82, 11, 12, 83, 19, 11, 84, 18,
  99. 10, 17, 9, 16, 8, 22, 85, 21, 20, 28, 27, 33, 32, 31, 30, 29, 28,
  100. 27, 26, 34, 35, 86, 87, 7, 25, 5, 15, 4, 14, 13, 12, 19, 18, 17, 16,
  101. 26, 25, 24, 23, 22, 21, 20, 19, 24, 23, 22, 21, 20, 19, 18, 17, 7,
  102. 6, 5, 4, 36, 37, 38, 39, 88, 89, 90, 91, 92, 93, 94, 95
  103. };
  104. static VLC cbplo_tab;
  105. static VLC cbphi_tab;
  106. static VLC blktype_tab;
  107. static VLC block_tab;
  108. static int get_cbphi(GetBitContext *gb, int x)
  109. {
  110. int value;
  111. value = get_vlc2(gb, cbphi_tab.table, cbphi_tab.bits, 1);
  112. if (value < 0)
  113. return AVERROR_INVALIDDATA;
  114. return x ? value : 15 - value;
  115. }
  116. static int decode_block(AVCodecContext *avctx, GetBitContext *gb,
  117. int block, int factor, int flag, int offset)
  118. {
  119. IMM4Context *s = avctx->priv_data;
  120. const uint8_t *scantable = s->intra_scantable.permutated;
  121. int i, last, len, factor2;
  122. for (i = !flag; i < 64; i++) {
  123. int value;
  124. value = get_vlc2(gb, block_tab.table, block_tab.bits, 1);
  125. if (value < 0)
  126. return AVERROR_INVALIDDATA;
  127. if (value == 0) {
  128. last = get_bits1(gb);
  129. len = get_bits(gb, 6);
  130. factor2 = get_sbits(gb, 8);
  131. } else {
  132. factor2 = value & 0x7F;
  133. last = (value >> 14) & 1;
  134. len = (value >> 7) & 0x3F;
  135. if (get_bits1(gb))
  136. factor2 = -factor2;
  137. }
  138. i += len;
  139. if (i >= 64)
  140. break;
  141. s->block[block][scantable[i]] = offset * (factor2 < 0 ? -1 : 1) + factor * factor2;
  142. if (last)
  143. break;
  144. }
  145. return 0;
  146. }
  147. static int decode_blocks(AVCodecContext *avctx, GetBitContext *gb,
  148. unsigned cbp, int flag, int offset)
  149. {
  150. IMM4Context *s = avctx->priv_data;
  151. const uint8_t *scantable = s->intra_scantable.permutated;
  152. int ret, i;
  153. memset(s->block, 0, sizeof(s->block));
  154. for (i = 0; i < 6; i++) {
  155. if (!flag) {
  156. int x = get_bits(gb, 8);
  157. if (x == 255)
  158. x = 128;
  159. x *= 8;
  160. s->block[i][scantable[0]] = x;
  161. }
  162. if (cbp & (1 << (5 - i))) {
  163. ret = decode_block(avctx, gb, i, s->factor, flag, offset);
  164. if (ret < 0)
  165. return ret;
  166. }
  167. }
  168. return 0;
  169. }
  170. static int decode_intra(AVCodecContext *avctx, GetBitContext *gb, AVFrame *frame)
  171. {
  172. IMM4Context *s = avctx->priv_data;
  173. int ret, x, y, offset = 0;
  174. if (s->hi == 0) {
  175. if (s->lo > 2)
  176. return AVERROR_INVALIDDATA;
  177. s->factor = intra_cb[s->lo];
  178. } else {
  179. if (s->hi == 1) {
  180. s->factor = s->lo * 2;
  181. } else {
  182. s->factor = s->lo * 2;
  183. }
  184. }
  185. if (s->hi) {
  186. offset = s->factor;
  187. offset >>= 1;
  188. if (!(offset & 1))
  189. offset--;
  190. }
  191. for (y = 0; y < avctx->height; y += 16) {
  192. for (x = 0; x < avctx->width; x += 16) {
  193. unsigned cbphi, cbplo;
  194. cbplo = get_vlc2(gb, cbplo_tab.table, cbplo_tab.bits, 1) >> 4;
  195. skip_bits1(gb);
  196. cbphi = get_cbphi(gb, 1);
  197. ret = decode_blocks(avctx, gb, cbplo | (cbphi << 2), 0, offset);
  198. if (ret < 0)
  199. return ret;
  200. s->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x,
  201. frame->linesize[0], s->block[0]);
  202. s->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x + 8,
  203. frame->linesize[0], s->block[1]);
  204. s->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x,
  205. frame->linesize[0], s->block[2]);
  206. s->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x + 8,
  207. frame->linesize[0], s->block[3]);
  208. s->idsp.idct_put(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1),
  209. frame->linesize[1], s->block[4]);
  210. s->idsp.idct_put(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1),
  211. frame->linesize[2], s->block[5]);
  212. }
  213. }
  214. return 0;
  215. }
  216. static int decode_inter(AVCodecContext *avctx, GetBitContext *gb,
  217. AVFrame *frame, AVFrame *prev)
  218. {
  219. IMM4Context *s = avctx->priv_data;
  220. int ret, x, y, offset = 0;
  221. if (s->hi == 0) {
  222. if (s->lo > 2)
  223. return AVERROR_INVALIDDATA;
  224. s->factor = inter_cb[s->lo];
  225. } else {
  226. if (s->hi == 1) {
  227. s->factor = s->lo * 2;
  228. } else {
  229. s->factor = s->lo * 2;
  230. }
  231. }
  232. if (s->hi) {
  233. offset = s->factor;
  234. offset >>= 1;
  235. if (!(offset & 1))
  236. offset--;
  237. }
  238. for (y = 0; y < avctx->height; y += 16) {
  239. for (x = 0; x < avctx->width; x += 16) {
  240. int reverse, intra_block, value;
  241. unsigned cbphi, cbplo;
  242. if (get_bits1(gb)) {
  243. copy_block16(frame->data[0] + y * frame->linesize[0] + x,
  244. prev->data[0] + y * prev->linesize[0] + x,
  245. frame->linesize[0], prev->linesize[0], 16);
  246. copy_block8(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1),
  247. prev->data[1] + (y >> 1) * prev->linesize[1] + (x >> 1),
  248. frame->linesize[1], prev->linesize[1], 8);
  249. copy_block8(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1),
  250. prev->data[2] + (y >> 1) * prev->linesize[2] + (x >> 1),
  251. frame->linesize[2], prev->linesize[2], 8);
  252. continue;
  253. }
  254. value = get_vlc2(gb, blktype_tab.table, blktype_tab.bits, 1);
  255. if (value < 0)
  256. return AVERROR_INVALIDDATA;
  257. intra_block = value & 0x07;
  258. reverse = intra_block == 3;
  259. if (reverse)
  260. skip_bits1(gb);
  261. cbplo = value >> 4;
  262. cbphi = get_cbphi(gb, reverse);
  263. if (intra_block) {
  264. ret = decode_blocks(avctx, gb, cbplo | (cbphi << 2), 0, offset);
  265. if (ret < 0)
  266. return ret;
  267. s->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x,
  268. frame->linesize[0], s->block[0]);
  269. s->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x + 8,
  270. frame->linesize[0], s->block[1]);
  271. s->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x,
  272. frame->linesize[0], s->block[2]);
  273. s->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x + 8,
  274. frame->linesize[0], s->block[3]);
  275. s->idsp.idct_put(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1),
  276. frame->linesize[1], s->block[4]);
  277. s->idsp.idct_put(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1),
  278. frame->linesize[2], s->block[5]);
  279. } else {
  280. skip_bits(gb, 2);
  281. ret = decode_blocks(avctx, gb, cbplo | (cbphi << 2), 1, offset);
  282. if (ret < 0)
  283. return ret;
  284. copy_block16(frame->data[0] + y * frame->linesize[0] + x,
  285. prev->data[0] + y * prev->linesize[0] + x,
  286. frame->linesize[0], prev->linesize[0], 16);
  287. copy_block8(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1),
  288. prev->data[1] + (y >> 1) * prev->linesize[1] + (x >> 1),
  289. frame->linesize[1], prev->linesize[1], 8);
  290. copy_block8(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1),
  291. prev->data[2] + (y >> 1) * prev->linesize[2] + (x >> 1),
  292. frame->linesize[2], prev->linesize[2], 8);
  293. s->idsp.idct_add(frame->data[0] + y * frame->linesize[0] + x,
  294. frame->linesize[0], s->block[0]);
  295. s->idsp.idct_add(frame->data[0] + y * frame->linesize[0] + x + 8,
  296. frame->linesize[0], s->block[1]);
  297. s->idsp.idct_add(frame->data[0] + (y + 8) * frame->linesize[0] + x,
  298. frame->linesize[0], s->block[2]);
  299. s->idsp.idct_add(frame->data[0] + (y + 8) * frame->linesize[0] + x + 8,
  300. frame->linesize[0], s->block[3]);
  301. s->idsp.idct_add(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1),
  302. frame->linesize[1], s->block[4]);
  303. s->idsp.idct_add(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1),
  304. frame->linesize[2], s->block[5]);
  305. }
  306. }
  307. }
  308. return 0;
  309. }
  310. static int decode_frame(AVCodecContext *avctx, void *data,
  311. int *got_frame, AVPacket *avpkt)
  312. {
  313. IMM4Context *s = avctx->priv_data;
  314. GetBitContext *gb = &s->gb;
  315. AVFrame *frame = data;
  316. unsigned type;
  317. int ret, scaled;
  318. if (avpkt->size <= 32)
  319. return AVERROR_INVALIDDATA;
  320. av_fast_padded_malloc(&s->bitstream, &s->bitstream_size,
  321. FFALIGN(avpkt->size, 4));
  322. if (!s->bitstream)
  323. return AVERROR(ENOMEM);
  324. s->bdsp.bswap_buf((uint32_t *)s->bitstream,
  325. (uint32_t *)avpkt->data,
  326. (avpkt->size + 3) >> 2);
  327. if ((ret = init_get_bits8(gb, s->bitstream, FFALIGN(avpkt->size, 4))) < 0)
  328. return ret;
  329. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  330. avctx->color_range = AVCOL_RANGE_JPEG;
  331. scaled = avpkt->data[8];
  332. if (scaled < 2) {
  333. int width, height;
  334. int mode = avpkt->data[10];
  335. switch (mode) {
  336. case 1:
  337. width = 352;
  338. height = 240;
  339. break;
  340. case 2:
  341. width = 704;
  342. height = 240;
  343. break;
  344. case 4:
  345. width = 480;
  346. height = 704;
  347. break;
  348. case 17:
  349. width = 352;
  350. height = 288;
  351. break;
  352. case 18:
  353. width = 704;
  354. height = 288;
  355. break;
  356. default:
  357. width = 704;
  358. height = 576;
  359. break;
  360. }
  361. if (s->changed_size == 1 &&
  362. (avctx->width != width || avctx->height != height)) {
  363. av_log(avctx, AV_LOG_ERROR, "Frame size change is unsupported.\n");
  364. return AVERROR_INVALIDDATA;
  365. }
  366. avctx->width = width;
  367. avctx->height = height;
  368. }
  369. s->changed_size = 1;
  370. skip_bits_long(gb, 24 * 8);
  371. type = get_bits_long(gb, 32);
  372. s->hi = get_bits(gb, 16);
  373. s->lo = get_bits(gb, 16);
  374. switch (type) {
  375. case 0x19781977:
  376. frame->key_frame = 1;
  377. frame->pict_type = AV_PICTURE_TYPE_I;
  378. break;
  379. case 0x12250926:
  380. frame->key_frame = 0;
  381. frame->pict_type = AV_PICTURE_TYPE_P;
  382. break;
  383. default:
  384. avpriv_request_sample(avctx, "type %X", type);
  385. return AVERROR_PATCHWELCOME;
  386. }
  387. if ((ret = ff_get_buffer(avctx, frame, frame->key_frame ? AV_GET_BUFFER_FLAG_REF : 0)) < 0)
  388. return ret;
  389. if (frame->key_frame) {
  390. ret = decode_intra(avctx, gb, frame);
  391. if (ret < 0)
  392. return ret;
  393. av_frame_unref(s->prev_frame);
  394. if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
  395. return ret;
  396. } else {
  397. if (!s->prev_frame->data[0]) {
  398. av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
  399. return AVERROR_INVALIDDATA;
  400. }
  401. ret = decode_inter(avctx, gb, frame, s->prev_frame);
  402. if (ret < 0)
  403. return ret;
  404. }
  405. *got_frame = 1;
  406. return avpkt->size;
  407. }
  408. static av_cold void imm4_init_static_data(void)
  409. {
  410. INIT_VLC_SPARSE_STATIC(&cbplo_tab, 9, FF_ARRAY_ELEMS(cbplo_bits),
  411. cbplo_bits, 1, 1, cbplo_codes, 1, 1, cbplo_symbols, 1, 1, 512);
  412. INIT_VLC_SPARSE_STATIC(&cbphi_tab, 6, FF_ARRAY_ELEMS(cbphi_bits),
  413. cbphi_bits, 1, 1, cbphi_codes, 1, 1, NULL, 0, 0, 64);
  414. INIT_VLC_SPARSE_STATIC(&blktype_tab, 9, FF_ARRAY_ELEMS(blktype_bits),
  415. blktype_bits, 1, 1, blktype_codes, 1, 1, blktype_symbols, 1, 1, 512);
  416. INIT_VLC_SPARSE_STATIC(&block_tab, 12, FF_ARRAY_ELEMS(block_bits),
  417. block_bits, 1, 1, block_codes, 1, 1, block_symbols, 2, 2, 4096);
  418. }
  419. static av_cold int decode_init(AVCodecContext *avctx)
  420. {
  421. static AVOnce init_static_once = AV_ONCE_INIT;
  422. IMM4Context *s = avctx->priv_data;
  423. uint8_t table[64];
  424. for (int i = 0; i < 64; i++)
  425. table[i] = i;
  426. ff_bswapdsp_init(&s->bdsp);
  427. ff_idctdsp_init(&s->idsp, avctx);
  428. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, table);
  429. s->prev_frame = av_frame_alloc();
  430. if (!s->prev_frame)
  431. return AVERROR(ENOMEM);
  432. ff_thread_once(&init_static_once, imm4_init_static_data);
  433. return 0;
  434. }
  435. static av_cold int decode_close(AVCodecContext *avctx)
  436. {
  437. IMM4Context *s = avctx->priv_data;
  438. av_frame_free(&s->prev_frame);
  439. av_freep(&s->bitstream);
  440. s->bitstream_size = 0;
  441. return 0;
  442. }
  443. AVCodec ff_imm4_decoder = {
  444. .name = "imm4",
  445. .long_name = NULL_IF_CONFIG_SMALL("Infinity IMM4"),
  446. .type = AVMEDIA_TYPE_VIDEO,
  447. .id = AV_CODEC_ID_IMM4,
  448. .priv_data_size = sizeof(IMM4Context),
  449. .init = decode_init,
  450. .close = decode_close,
  451. .decode = decode_frame,
  452. .capabilities = AV_CODEC_CAP_DR1,
  453. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  454. FF_CODEC_CAP_INIT_CLEANUP,
  455. };