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.

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