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.

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