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.

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