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.

529 lines
15KB

  1. /*
  2. * Gremlin Digital Video (GDV) decoder
  3. * Copyright (c) 2017 Konstantin Shishkov
  4. * Copyright (c) 2017 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 "libavutil/common.h"
  23. #include "avcodec.h"
  24. #include "bytestream.h"
  25. #include "internal.h"
  26. typedef struct GDVContext {
  27. AVCodecContext *avctx;
  28. GetByteContext gb;
  29. GetByteContext g2;
  30. PutByteContext pb;
  31. uint32_t pal[256];
  32. uint8_t *frame;
  33. unsigned frame_size;
  34. unsigned scale_h, scale_v;
  35. } GDVContext;
  36. typedef struct Bits8 {
  37. uint8_t queue;
  38. uint8_t fill;
  39. } Bits8;
  40. typedef struct Bits32 {
  41. uint32_t queue;
  42. uint8_t fill;
  43. } Bits32;
  44. #define PREAMBLE_SIZE 4096
  45. static av_cold int gdv_decode_init(AVCodecContext *avctx)
  46. {
  47. GDVContext *gdv = avctx->priv_data;
  48. int i, j, k;
  49. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  50. gdv->frame_size = avctx->width * avctx->height + PREAMBLE_SIZE;
  51. gdv->frame = av_calloc(gdv->frame_size, 1);
  52. if (!gdv->frame)
  53. return AVERROR(ENOMEM);
  54. for (i = 0; i < 2; i++) {
  55. for (j = 0; j < 256; j++) {
  56. for (k = 0; k < 8; k++) {
  57. gdv->frame[i * 2048 + j * 8 + k] = j;
  58. }
  59. }
  60. }
  61. return 0;
  62. }
  63. static void rescale(GDVContext *gdv, uint8_t *dst, int w, int h, int scale_v, int scale_h)
  64. {
  65. int i, j, y, x;
  66. if ((gdv->scale_v == scale_v) && (gdv->scale_h == scale_h)) {
  67. return;
  68. }
  69. if (gdv->scale_v) {
  70. for (j = 0; j < h; j++) {
  71. int y = h - j - 1;
  72. uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
  73. uint8_t *src1 = dst + PREAMBLE_SIZE + (y>>!!gdv->scale_h) * (w>>1);
  74. for (x = w - 1; x >= 0 && !(x&1); x--) {
  75. dst1[x] = src1[(x>>1)];
  76. }
  77. for (x--; x >= 0; x-=2) {
  78. dst1[x ] =
  79. dst1[x+1] = src1[(x>>1)];
  80. }
  81. }
  82. } else if (gdv->scale_h) {
  83. for (j = 0; j < h; j++) {
  84. int y = h - j - 1;
  85. uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
  86. uint8_t *src1 = dst + PREAMBLE_SIZE + (y>>1) * w;
  87. memcpy(dst1, src1, w);
  88. }
  89. }
  90. if (scale_h && scale_v) {
  91. for (y = 0; y < (h>>1); y++) {
  92. uint8_t *dst1 = dst + PREAMBLE_SIZE + y * (w>>1);
  93. uint8_t *src1 = dst + PREAMBLE_SIZE + y*2 * w;
  94. for (x = 0; x < (w>>1); x++) {
  95. dst1[x] = src1[x*2];
  96. }
  97. }
  98. } else if (scale_h) {
  99. for (y = 0; y < (h>>1); y++) {
  100. uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
  101. uint8_t *src1 = dst + PREAMBLE_SIZE + y*2 * w;
  102. memcpy(dst1, src1, w);
  103. }
  104. } else if (scale_v) {
  105. for (y = 0; y < h; y++) {
  106. uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
  107. for (x = 0; x < (w>>1); x++) {
  108. dst1[x] = dst1[x*2];
  109. }
  110. }
  111. }
  112. gdv->scale_v = scale_v;
  113. gdv->scale_h = scale_h;
  114. }
  115. static int read_bits2(Bits8 *bits, GetByteContext *gb)
  116. {
  117. int res;
  118. if (bits->fill == 0) {
  119. bits->queue |= bytestream2_get_byte(gb);
  120. bits->fill = 8;
  121. }
  122. res = bits->queue >> 6;
  123. bits->queue <<= 2;
  124. bits->fill -= 2;
  125. return res;
  126. }
  127. static void fill_bits32(Bits32 *bits, GetByteContext *gb)
  128. {
  129. bits->queue = bytestream2_get_le32(gb);
  130. bits->fill = 32;
  131. }
  132. static int read_bits32(Bits32 *bits, GetByteContext *gb, int nbits)
  133. {
  134. int res = bits->queue & ((1 << nbits) - 1);
  135. bits->queue >>= nbits;
  136. bits->fill -= nbits;
  137. if (bits->fill <= 16) {
  138. bits->queue |= bytestream2_get_le16(gb) << bits->fill;
  139. bits->fill += 16;
  140. }
  141. return res;
  142. }
  143. static void lz_copy(PutByteContext *pb, GetByteContext *g2, int offset, unsigned len)
  144. {
  145. int i;
  146. if (offset == -1) {
  147. int c;
  148. bytestream2_seek(g2, bytestream2_tell_p(pb) - 1, SEEK_SET);
  149. c = bytestream2_get_byte(g2);
  150. for (i = 0; i < len; i++) {
  151. bytestream2_put_byte(pb, c);
  152. }
  153. } else if (offset < 0) {
  154. int start = bytestream2_tell_p(pb) - (-offset);
  155. bytestream2_seek(g2, start, SEEK_SET);
  156. for (i = 0; i < len; i++) {
  157. bytestream2_put_byte(pb, bytestream2_get_byte(g2));
  158. }
  159. } else {
  160. int start = bytestream2_tell_p(pb) + offset;
  161. bytestream2_seek(g2, start, SEEK_SET);
  162. for (i = 0; i < len; i++) {
  163. bytestream2_put_byte(pb, bytestream2_get_byte(g2));
  164. }
  165. }
  166. }
  167. static int decompress_2(AVCodecContext *avctx)
  168. {
  169. GDVContext *gdv = avctx->priv_data;
  170. GetByteContext *gb = &gdv->gb;
  171. GetByteContext *g2 = &gdv->g2;
  172. PutByteContext *pb = &gdv->pb;
  173. Bits8 bits = { 0 };
  174. int c, i;
  175. bytestream2_init(g2, gdv->frame, gdv->frame_size);
  176. bytestream2_skip_p(pb, PREAMBLE_SIZE);
  177. for (c = 0; c < 256; c++) {
  178. for (i = 0; i < 16; i++) {
  179. gdv->frame[c * 16 + i] = c;
  180. }
  181. }
  182. while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
  183. int tag = read_bits2(&bits, gb);
  184. if (tag == 0) {
  185. bytestream2_put_byte(pb, bytestream2_get_byte(gb));
  186. } else if (tag == 1) {
  187. int b = bytestream2_get_byte(gb);
  188. int len = (b & 0xF) + 3;
  189. int top = (b >> 4) & 0xF;
  190. int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
  191. lz_copy(pb, g2, off, len);
  192. } else if (tag == 2) {
  193. int len = (bytestream2_get_byte(gb)) + 2;
  194. bytestream2_skip_p(pb, len);
  195. } else {
  196. break;
  197. }
  198. }
  199. if (bytestream2_get_bytes_left_p(pb) > 0)
  200. return AVERROR_INVALIDDATA;
  201. return 0;
  202. }
  203. static int decompress_5(AVCodecContext *avctx, unsigned skip)
  204. {
  205. GDVContext *gdv = avctx->priv_data;
  206. GetByteContext *gb = &gdv->gb;
  207. GetByteContext *g2 = &gdv->g2;
  208. PutByteContext *pb = &gdv->pb;
  209. Bits8 bits = { 0 };
  210. bytestream2_init(g2, gdv->frame, gdv->frame_size);
  211. bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
  212. while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
  213. int tag = read_bits2(&bits, gb);
  214. if (tag == 0) {
  215. bytestream2_put_byte(pb, bytestream2_get_byte(gb));
  216. } else if (tag == 1) {
  217. int b = bytestream2_get_byte(gb);
  218. int len = (b & 0xF) + 3;
  219. int top = b >> 4;
  220. int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
  221. lz_copy(pb, g2, off, len);
  222. } else if (tag == 2) {
  223. int len;
  224. int b = bytestream2_get_byte(gb);
  225. if (b == 0) {
  226. break;
  227. }
  228. if (b != 0xFF) {
  229. len = b;
  230. } else {
  231. len = bytestream2_get_le16(gb);
  232. }
  233. bytestream2_skip_p(pb, len + 1);
  234. } else {
  235. int b = bytestream2_get_byte(gb);
  236. int len = (b & 0x3) + 2;
  237. int off = -(b >> 2) - 1;
  238. lz_copy(pb, g2, off, len);
  239. }
  240. }
  241. return 0;
  242. }
  243. static int decompress_68(AVCodecContext *avctx, unsigned skip, unsigned use8)
  244. {
  245. GDVContext *gdv = avctx->priv_data;
  246. GetByteContext *gb = &gdv->gb;
  247. GetByteContext *g2 = &gdv->g2;
  248. PutByteContext *pb = &gdv->pb;
  249. Bits32 bits;
  250. bytestream2_init(g2, gdv->frame, gdv->frame_size);
  251. bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
  252. fill_bits32(&bits, gb);
  253. while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
  254. int tag = read_bits32(&bits, gb, 2);
  255. if (tag == 0) {
  256. int b = read_bits32(&bits, gb, 1);
  257. if (b == 0) {
  258. bytestream2_put_byte(pb, bytestream2_get_byte(gb));
  259. } else {
  260. int i, len = 2;
  261. int lbits = 0;
  262. while (1) {
  263. int val;
  264. lbits += 1;
  265. val = read_bits32(&bits, gb, lbits);
  266. len += val;
  267. if (val != ((1 << lbits) - 1)) {
  268. break;
  269. }
  270. assert(lbits < 16);
  271. }
  272. for (i = 0; i < len; i++) {
  273. bytestream2_put_byte(pb, bytestream2_get_byte(gb));
  274. }
  275. }
  276. } else if (tag == 1) {
  277. int b = read_bits32(&bits, gb, 1);
  278. int len;
  279. if (b == 0) {
  280. len = (read_bits32(&bits, gb, 4)) + 2;
  281. } else {
  282. int bb = bytestream2_get_byte(gb);
  283. if ((bb & 0x80) == 0) {
  284. len = bb + 18;
  285. } else {
  286. int top = (bb & 0x7F) << 8;
  287. len = top + bytestream2_get_byte(gb) + 146;
  288. }
  289. }
  290. bytestream2_skip_p(pb, len);
  291. } else if (tag == 2) {
  292. int i, subtag = read_bits32(&bits, gb, 2);
  293. if (subtag != 3) {
  294. int top = (read_bits32(&bits, gb, 4)) << 8;
  295. int offs = top + bytestream2_get_byte(gb);
  296. if ((subtag != 0) || (offs <= 0xF80)) {
  297. int len = (subtag) + 3;
  298. lz_copy(pb, g2, (offs) - 4096, len);
  299. } else {
  300. int real_off, len, c1, c2;
  301. if (offs == 0xFFF) {
  302. return 0;
  303. }
  304. real_off = ((offs >> 4) & 0x7) + 1;
  305. len = ((offs & 0xF) + 2) * 2;
  306. c1 = gdv->frame[bytestream2_tell_p(pb) - real_off];
  307. c2 = gdv->frame[bytestream2_tell_p(pb) - real_off + 1];
  308. for (i = 0; i < len/2; i++) {
  309. bytestream2_put_byte(pb, c1);
  310. bytestream2_put_byte(pb, c2);
  311. }
  312. }
  313. } else {
  314. int b = bytestream2_get_byte(gb);
  315. int off = ((b & 0x7F)) + 1;
  316. int len = ((b & 0x80) == 0) ? 2 : 3;
  317. lz_copy(pb, g2, -off, len);
  318. }
  319. } else {
  320. int len;
  321. int off;
  322. if (use8) {
  323. int q, b = bytestream2_get_byte(gb);
  324. if ((b & 0xC0) == 0xC0) {
  325. len = ((b & 0x3F)) + 8;
  326. q = read_bits32(&bits, gb, 4);
  327. off = (q << 8) + (bytestream2_get_byte(gb)) + 1;
  328. } else {
  329. int ofs1;
  330. if ((b & 0x80) == 0) {
  331. len = ((b >> 4)) + 6;
  332. ofs1 = (b & 0xF);
  333. } else {
  334. len = ((b & 0x3F)) + 14;
  335. ofs1 = read_bits32(&bits, gb, 4);
  336. }
  337. off = (ofs1 << 8) + (bytestream2_get_byte(gb)) - 4096;
  338. }
  339. } else {
  340. int ofs1, b = bytestream2_get_byte(gb);
  341. if ((b >> 4) == 0xF) {
  342. len = bytestream2_get_byte(gb) + 21;
  343. } else {
  344. len = (b >> 4) + 6;
  345. }
  346. ofs1 = (b & 0xF);
  347. off = (ofs1 << 8) + bytestream2_get_byte(gb) - 4096;
  348. }
  349. lz_copy(pb, g2, off, len);
  350. }
  351. }
  352. return 0;
  353. }
  354. static int gdv_decode_frame(AVCodecContext *avctx, void *data,
  355. int *got_frame, AVPacket *avpkt)
  356. {
  357. GDVContext *gdv = avctx->priv_data;
  358. GetByteContext *gb = &gdv->gb;
  359. PutByteContext *pb = &gdv->pb;
  360. AVFrame *frame = data;
  361. int ret, i, pal_size;
  362. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
  363. int compression;
  364. unsigned flags;
  365. uint8_t *dst;
  366. bytestream2_init(gb, avpkt->data, avpkt->size);
  367. bytestream2_init_writer(pb, gdv->frame, gdv->frame_size);
  368. flags = bytestream2_get_le32(gb);
  369. compression = flags & 0xF;
  370. if (compression == 4 || compression == 7 || compression > 8)
  371. return AVERROR_INVALIDDATA;
  372. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  373. return ret;
  374. if (pal && pal_size == AVPALETTE_SIZE)
  375. memcpy(gdv->pal, pal, AVPALETTE_SIZE);
  376. rescale(gdv, gdv->frame, avctx->width, avctx->height,
  377. !!(flags & 0x10), !!(flags & 0x20));
  378. switch (compression) {
  379. case 1:
  380. memset(gdv->frame + PREAMBLE_SIZE, 0, gdv->frame_size - PREAMBLE_SIZE);
  381. case 0:
  382. if (bytestream2_get_bytes_left(gb) < 256*3)
  383. return AVERROR_INVALIDDATA;
  384. for (i = 0; i < 256; i++) {
  385. unsigned r = bytestream2_get_byte(gb);
  386. unsigned g = bytestream2_get_byte(gb);
  387. unsigned b = bytestream2_get_byte(gb);
  388. gdv->pal[i] = 0xFFU << 24 | r << 18 | g << 10 | b << 2;
  389. }
  390. break;
  391. case 2:
  392. ret = decompress_2(avctx);
  393. break;
  394. case 3:
  395. break;
  396. case 5:
  397. ret = decompress_5(avctx, flags >> 8);
  398. break;
  399. case 6:
  400. ret = decompress_68(avctx, flags >> 8, 0);
  401. break;
  402. case 8:
  403. ret = decompress_68(avctx, flags >> 8, 1);
  404. break;
  405. default:
  406. av_assert0(0);
  407. }
  408. if (ret < 0)
  409. return ret;
  410. memcpy(frame->data[1], gdv->pal, AVPALETTE_SIZE);
  411. dst = frame->data[0];
  412. if (!gdv->scale_v && !gdv->scale_h) {
  413. int sidx = PREAMBLE_SIZE, didx = 0;
  414. int y, x;
  415. for (y = 0; y < avctx->height; y++) {
  416. for (x = 0; x < avctx->width; x++) {
  417. dst[x+didx] = gdv->frame[x+sidx];
  418. }
  419. sidx += avctx->width;
  420. didx += frame->linesize[0];
  421. }
  422. } else {
  423. int sidx = PREAMBLE_SIZE, didx = 0;
  424. int y, x;
  425. for (y = 0; y < avctx->height; y++) {
  426. if (!gdv->scale_v) {
  427. memcpy(dst + didx, gdv->frame + sidx, avctx->width);
  428. } else {
  429. for (x = 0; x < avctx->width - 1; x+=2) {
  430. dst[didx + x ] =
  431. dst[didx + x + 1] = gdv->frame[sidx + (x>>1)];
  432. }
  433. for (; x < avctx->width; x++) {
  434. dst[didx + x] = gdv->frame[sidx + (x>>1)];
  435. }
  436. }
  437. if (!gdv->scale_h || ((y & 1) == 1)) {
  438. sidx += !gdv->scale_v ? avctx->width : avctx->width/2;
  439. }
  440. didx += frame->linesize[0];
  441. }
  442. }
  443. *got_frame = 1;
  444. return ret < 0 ? ret : avpkt->size;
  445. }
  446. static av_cold int gdv_decode_close(AVCodecContext *avctx)
  447. {
  448. GDVContext *gdv = avctx->priv_data;
  449. av_freep(&gdv->frame);
  450. return 0;
  451. }
  452. AVCodec ff_gdv_decoder = {
  453. .name = "gdv",
  454. .long_name = NULL_IF_CONFIG_SMALL("Gremlin Digital Video"),
  455. .type = AVMEDIA_TYPE_VIDEO,
  456. .id = AV_CODEC_ID_GDV,
  457. .priv_data_size = sizeof(GDVContext),
  458. .init = gdv_decode_init,
  459. .close = gdv_decode_close,
  460. .decode = gdv_decode_frame,
  461. .capabilities = AV_CODEC_CAP_DR1,
  462. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  463. };