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.

686 lines
19KB

  1. /*
  2. * Zip Motion Blocks Video (ZMBV) decoder
  3. * Copyright (c) 2006 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Zip Motion Blocks Video decoder
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include "libavutil/intreadwrite.h"
  28. #include "avcodec.h"
  29. #include <zlib.h>
  30. #define ZMBV_KEYFRAME 1
  31. #define ZMBV_DELTAPAL 2
  32. enum ZmbvFormat {
  33. ZMBV_FMT_NONE = 0,
  34. ZMBV_FMT_1BPP = 1,
  35. ZMBV_FMT_2BPP = 2,
  36. ZMBV_FMT_4BPP = 3,
  37. ZMBV_FMT_8BPP = 4,
  38. ZMBV_FMT_15BPP = 5,
  39. ZMBV_FMT_16BPP = 6,
  40. ZMBV_FMT_24BPP = 7,
  41. ZMBV_FMT_32BPP = 8
  42. };
  43. /*
  44. * Decoder context
  45. */
  46. typedef struct ZmbvContext {
  47. AVCodecContext *avctx;
  48. AVFrame pic;
  49. int bpp;
  50. unsigned int decomp_size;
  51. uint8_t* decomp_buf;
  52. uint8_t pal[768];
  53. uint8_t *prev, *cur;
  54. int width, height;
  55. int fmt;
  56. int comp;
  57. int flags;
  58. int bw, bh, bx, by;
  59. int decomp_len;
  60. z_stream zstream;
  61. int (*decode_intra)(struct ZmbvContext *c);
  62. int (*decode_xor)(struct ZmbvContext *c);
  63. } ZmbvContext;
  64. /**
  65. * Decode XOR'ed frame - 8bpp version
  66. */
  67. static int zmbv_decode_xor_8(ZmbvContext *c)
  68. {
  69. uint8_t *src = c->decomp_buf;
  70. uint8_t *output, *prev;
  71. int8_t *mvec;
  72. int x, y;
  73. int d, dx, dy, bw2, bh2;
  74. int block;
  75. int i, j;
  76. int mx, my;
  77. output = c->cur;
  78. prev = c->prev;
  79. if (c->flags & ZMBV_DELTAPAL) {
  80. for (i = 0; i < 768; i++)
  81. c->pal[i] ^= *src++;
  82. }
  83. mvec = (int8_t*)src;
  84. src += ((c->bx * c->by * 2 + 3) & ~3);
  85. block = 0;
  86. for (y = 0; y < c->height; y += c->bh) {
  87. bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
  88. for (x = 0; x < c->width; x += c->bw) {
  89. uint8_t *out, *tprev;
  90. d = mvec[block] & 1;
  91. dx = mvec[block] >> 1;
  92. dy = mvec[block + 1] >> 1;
  93. block += 2;
  94. bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
  95. /* copy block - motion vectors out of bounds are used to zero blocks */
  96. out = output + x;
  97. tprev = prev + x + dx + dy * c->width;
  98. mx = x + dx;
  99. my = y + dy;
  100. for (j = 0; j < bh2; j++) {
  101. if (my + j < 0 || my + j >= c->height) {
  102. memset(out, 0, bw2);
  103. } else {
  104. for (i = 0; i < bw2; i++) {
  105. if (mx + i < 0 || mx + i >= c->width)
  106. out[i] = 0;
  107. else
  108. out[i] = tprev[i];
  109. }
  110. }
  111. out += c->width;
  112. tprev += c->width;
  113. }
  114. if (d) { /* apply XOR'ed difference */
  115. out = output + x;
  116. for (j = 0; j < bh2; j++) {
  117. for (i = 0; i < bw2; i++)
  118. out[i] ^= *src++;
  119. out += c->width;
  120. }
  121. }
  122. }
  123. output += c->width * c->bh;
  124. prev += c->width * c->bh;
  125. }
  126. if (src - c->decomp_buf != c->decomp_len)
  127. av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
  128. src-c->decomp_buf, c->decomp_len);
  129. return 0;
  130. }
  131. /**
  132. * Decode XOR'ed frame - 15bpp and 16bpp version
  133. */
  134. static int zmbv_decode_xor_16(ZmbvContext *c)
  135. {
  136. uint8_t *src = c->decomp_buf;
  137. uint16_t *output, *prev;
  138. int8_t *mvec;
  139. int x, y;
  140. int d, dx, dy, bw2, bh2;
  141. int block;
  142. int i, j;
  143. int mx, my;
  144. output = (uint16_t*)c->cur;
  145. prev = (uint16_t*)c->prev;
  146. mvec = (int8_t*)src;
  147. src += ((c->bx * c->by * 2 + 3) & ~3);
  148. block = 0;
  149. for (y = 0; y < c->height; y += c->bh) {
  150. bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
  151. for (x = 0; x < c->width; x += c->bw) {
  152. uint16_t *out, *tprev;
  153. d = mvec[block] & 1;
  154. dx = mvec[block] >> 1;
  155. dy = mvec[block + 1] >> 1;
  156. block += 2;
  157. bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
  158. /* copy block - motion vectors out of bounds are used to zero blocks */
  159. out = output + x;
  160. tprev = prev + x + dx + dy * c->width;
  161. mx = x + dx;
  162. my = y + dy;
  163. for (j = 0; j < bh2; j++) {
  164. if (my + j < 0 || my + j >= c->height) {
  165. memset(out, 0, bw2 * 2);
  166. } else {
  167. for (i = 0; i < bw2; i++) {
  168. if (mx + i < 0 || mx + i >= c->width)
  169. out[i] = 0;
  170. else
  171. out[i] = tprev[i];
  172. }
  173. }
  174. out += c->width;
  175. tprev += c->width;
  176. }
  177. if (d) { /* apply XOR'ed difference */
  178. out = output + x;
  179. for (j = 0; j < bh2; j++){
  180. for (i = 0; i < bw2; i++) {
  181. out[i] ^= *((uint16_t*)src);
  182. src += 2;
  183. }
  184. out += c->width;
  185. }
  186. }
  187. }
  188. output += c->width * c->bh;
  189. prev += c->width * c->bh;
  190. }
  191. if (src - c->decomp_buf != c->decomp_len)
  192. av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
  193. src-c->decomp_buf, c->decomp_len);
  194. return 0;
  195. }
  196. #ifdef ZMBV_ENABLE_24BPP
  197. /**
  198. * Decode XOR'ed frame - 24bpp version
  199. */
  200. static int zmbv_decode_xor_24(ZmbvContext *c)
  201. {
  202. uint8_t *src = c->decomp_buf;
  203. uint8_t *output, *prev;
  204. int8_t *mvec;
  205. int x, y;
  206. int d, dx, dy, bw2, bh2;
  207. int block;
  208. int i, j;
  209. int mx, my;
  210. int stride;
  211. output = c->cur;
  212. prev = c->prev;
  213. stride = c->width * 3;
  214. mvec = (int8_t*)src;
  215. src += ((c->bx * c->by * 2 + 3) & ~3);
  216. block = 0;
  217. for (y = 0; y < c->height; y += c->bh) {
  218. bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
  219. for (x = 0; x < c->width; x += c->bw) {
  220. uint8_t *out, *tprev;
  221. d = mvec[block] & 1;
  222. dx = mvec[block] >> 1;
  223. dy = mvec[block + 1] >> 1;
  224. block += 2;
  225. bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
  226. /* copy block - motion vectors out of bounds are used to zero blocks */
  227. out = output + x * 3;
  228. tprev = prev + (x + dx) * 3 + dy * stride;
  229. mx = x + dx;
  230. my = y + dy;
  231. for (j = 0; j < bh2; j++) {
  232. if (my + j < 0 || my + j >= c->height) {
  233. memset(out, 0, bw2 * 3);
  234. } else {
  235. for (i = 0; i < bw2; i++){
  236. if (mx + i < 0 || mx + i >= c->width) {
  237. out[i * 3 + 0] = 0;
  238. out[i * 3 + 1] = 0;
  239. out[i * 3 + 2] = 0;
  240. } else {
  241. out[i * 3 + 0] = tprev[i * 3 + 0];
  242. out[i * 3 + 1] = tprev[i * 3 + 1];
  243. out[i * 3 + 2] = tprev[i * 3 + 2];
  244. }
  245. }
  246. }
  247. out += stride;
  248. tprev += stride;
  249. }
  250. if (d) { /* apply XOR'ed difference */
  251. out = output + x * 3;
  252. for (j = 0; j < bh2; j++) {
  253. for (i = 0; i < bw2; i++) {
  254. out[i * 3 + 0] ^= *src++;
  255. out[i * 3 + 1] ^= *src++;
  256. out[i * 3 + 2] ^= *src++;
  257. }
  258. out += stride;
  259. }
  260. }
  261. }
  262. output += stride * c->bh;
  263. prev += stride * c->bh;
  264. }
  265. if (src - c->decomp_buf != c->decomp_len)
  266. av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n",
  267. src-c->decomp_buf, c->decomp_len);
  268. return 0;
  269. }
  270. #endif //ZMBV_ENABLE_24BPP
  271. /**
  272. * Decode XOR'ed frame - 32bpp version
  273. */
  274. static int zmbv_decode_xor_32(ZmbvContext *c)
  275. {
  276. uint8_t *src = c->decomp_buf;
  277. uint32_t *output, *prev;
  278. int8_t *mvec;
  279. int x, y;
  280. int d, dx, dy, bw2, bh2;
  281. int block;
  282. int i, j;
  283. int mx, my;
  284. output = (uint32_t*)c->cur;
  285. prev = (uint32_t*)c->prev;
  286. mvec = (int8_t*)src;
  287. src += ((c->bx * c->by * 2 + 3) & ~3);
  288. block = 0;
  289. for (y = 0; y < c->height; y += c->bh) {
  290. bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
  291. for (x = 0; x < c->width; x += c->bw) {
  292. uint32_t *out, *tprev;
  293. d = mvec[block] & 1;
  294. dx = mvec[block] >> 1;
  295. dy = mvec[block + 1] >> 1;
  296. block += 2;
  297. bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
  298. /* copy block - motion vectors out of bounds are used to zero blocks */
  299. out = output + x;
  300. tprev = prev + x + dx + dy * c->width;
  301. mx = x + dx;
  302. my = y + dy;
  303. for (j = 0; j < bh2; j++) {
  304. if (my + j < 0 || my + j >= c->height) {
  305. memset(out, 0, bw2 * 4);
  306. } else {
  307. for (i = 0; i < bw2; i++){
  308. if (mx + i < 0 || mx + i >= c->width)
  309. out[i] = 0;
  310. else
  311. out[i] = tprev[i];
  312. }
  313. }
  314. out += c->width;
  315. tprev += c->width;
  316. }
  317. if (d) { /* apply XOR'ed difference */
  318. out = output + x;
  319. for (j = 0; j < bh2; j++){
  320. for (i = 0; i < bw2; i++) {
  321. out[i] ^= *((uint32_t *) src);
  322. src += 4;
  323. }
  324. out += c->width;
  325. }
  326. }
  327. }
  328. output += c->width * c->bh;
  329. prev += c->width * c->bh;
  330. }
  331. if (src - c->decomp_buf != c->decomp_len)
  332. av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
  333. src-c->decomp_buf, c->decomp_len);
  334. return 0;
  335. }
  336. /**
  337. * Decode intraframe
  338. */
  339. static int zmbv_decode_intra(ZmbvContext *c)
  340. {
  341. uint8_t *src = c->decomp_buf;
  342. /* make the palette available on the way out */
  343. if (c->fmt == ZMBV_FMT_8BPP) {
  344. memcpy(c->pal, src, 768);
  345. src += 768;
  346. }
  347. memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
  348. return 0;
  349. }
  350. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
  351. {
  352. const uint8_t *buf = avpkt->data;
  353. int buf_size = avpkt->size;
  354. ZmbvContext * const c = avctx->priv_data;
  355. int zret = Z_OK; // Zlib return code
  356. int len = buf_size;
  357. int hi_ver, lo_ver, ret;
  358. uint8_t *tmp;
  359. if (c->pic.data[0])
  360. avctx->release_buffer(avctx, &c->pic);
  361. c->pic.reference = 1;
  362. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  363. if ((ret = avctx->get_buffer(avctx, &c->pic)) < 0) {
  364. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  365. return ret;
  366. }
  367. /* parse header */
  368. c->flags = buf[0];
  369. buf++; len--;
  370. if (c->flags & ZMBV_KEYFRAME) {
  371. hi_ver = buf[0];
  372. lo_ver = buf[1];
  373. c->comp = buf[2];
  374. c->fmt = buf[3];
  375. c->bw = buf[4];
  376. c->bh = buf[5];
  377. buf += 6;
  378. len -= 6;
  379. av_log(avctx, AV_LOG_DEBUG,
  380. "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",
  381. c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
  382. if (hi_ver != 0 || lo_ver != 1) {
  383. av_log_ask_for_sample(avctx, "Unsupported version %i.%i\n",
  384. hi_ver, lo_ver);
  385. return AVERROR_PATCHWELCOME;
  386. }
  387. if (c->bw == 0 || c->bh == 0) {
  388. av_log_ask_for_sample(avctx, "Unsupported block size %ix%i\n",
  389. c->bw, c->bh);
  390. return AVERROR_PATCHWELCOME;
  391. }
  392. if (c->comp != 0 && c->comp != 1) {
  393. av_log_ask_for_sample(avctx, "Unsupported compression type %i\n",
  394. c->comp);
  395. return AVERROR_PATCHWELCOME;
  396. }
  397. switch (c->fmt) {
  398. case ZMBV_FMT_8BPP:
  399. c->bpp = 8;
  400. c->decode_intra = zmbv_decode_intra;
  401. c->decode_xor = zmbv_decode_xor_8;
  402. break;
  403. case ZMBV_FMT_15BPP:
  404. case ZMBV_FMT_16BPP:
  405. c->bpp = 16;
  406. c->decode_intra = zmbv_decode_intra;
  407. c->decode_xor = zmbv_decode_xor_16;
  408. break;
  409. #ifdef ZMBV_ENABLE_24BPP
  410. case ZMBV_FMT_24BPP:
  411. c->bpp = 24;
  412. c->decode_intra = zmbv_decode_intra;
  413. c->decode_xor = zmbv_decode_xor_24;
  414. break;
  415. #endif //ZMBV_ENABLE_24BPP
  416. case ZMBV_FMT_32BPP:
  417. c->bpp = 32;
  418. c->decode_intra = zmbv_decode_intra;
  419. c->decode_xor = zmbv_decode_xor_32;
  420. break;
  421. default:
  422. c->decode_intra = NULL;
  423. c->decode_xor = NULL;
  424. av_log_ask_for_sample(avctx, "Unsupported (for now) format %i\n",
  425. c->fmt);
  426. return AVERROR_PATCHWELCOME;
  427. }
  428. zret = inflateReset(&c->zstream);
  429. if (zret != Z_OK) {
  430. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
  431. return -1;
  432. }
  433. tmp = av_realloc(c->cur, avctx->width * avctx->height * (c->bpp / 8));
  434. if (!tmp)
  435. return AVERROR(ENOMEM);
  436. c->cur = tmp;
  437. tmp = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8));
  438. if (!tmp)
  439. return AVERROR(ENOMEM);
  440. c->prev = tmp;
  441. c->bx = (c->width + c->bw - 1) / c->bw;
  442. c->by = (c->height + c->bh - 1) / c->bh;
  443. }
  444. if (c->decode_intra == NULL) {
  445. av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
  446. return AVERROR_INVALIDDATA;
  447. }
  448. if (c->comp == 0) { //Uncompressed data
  449. memcpy(c->decomp_buf, buf, len);
  450. c->decomp_size = 1;
  451. } else { // ZLIB-compressed data
  452. c->zstream.total_in = c->zstream.total_out = 0;
  453. c->zstream.next_in = buf;
  454. c->zstream.avail_in = len;
  455. c->zstream.next_out = c->decomp_buf;
  456. c->zstream.avail_out = c->decomp_size;
  457. zret = inflate(&c->zstream, Z_SYNC_FLUSH);
  458. if (zret != Z_OK && zret != Z_STREAM_END) {
  459. av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
  460. return AVERROR_INVALIDDATA;
  461. }
  462. c->decomp_len = c->zstream.total_out;
  463. }
  464. if (c->flags & ZMBV_KEYFRAME) {
  465. c->pic.key_frame = 1;
  466. c->pic.pict_type = AV_PICTURE_TYPE_I;
  467. c->decode_intra(c);
  468. } else {
  469. c->pic.key_frame = 0;
  470. c->pic.pict_type = AV_PICTURE_TYPE_P;
  471. if (c->decomp_len)
  472. c->decode_xor(c);
  473. }
  474. /* update frames */
  475. {
  476. uint8_t *out, *src;
  477. int i, j;
  478. out = c->pic.data[0];
  479. src = c->cur;
  480. switch (c->fmt) {
  481. case ZMBV_FMT_8BPP:
  482. for (j = 0; j < c->height; j++) {
  483. for (i = 0; i < c->width; i++) {
  484. out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
  485. out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
  486. out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
  487. src++;
  488. }
  489. out += c->pic.linesize[0];
  490. }
  491. break;
  492. case ZMBV_FMT_15BPP:
  493. for (j = 0; j < c->height; j++) {
  494. for (i = 0; i < c->width; i++) {
  495. uint16_t tmp = AV_RL16(src);
  496. src += 2;
  497. out[i * 3 + 0] = (tmp & 0x7C00) >> 7;
  498. out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
  499. out[i * 3 + 2] = (tmp & 0x001F) << 3;
  500. }
  501. out += c->pic.linesize[0];
  502. }
  503. break;
  504. case ZMBV_FMT_16BPP:
  505. for (j = 0; j < c->height; j++) {
  506. for (i = 0; i < c->width; i++) {
  507. uint16_t tmp = AV_RL16(src);
  508. src += 2;
  509. out[i * 3 + 0] = (tmp & 0xF800) >> 8;
  510. out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
  511. out[i * 3 + 2] = (tmp & 0x001F) << 3;
  512. }
  513. out += c->pic.linesize[0];
  514. }
  515. break;
  516. #ifdef ZMBV_ENABLE_24BPP
  517. case ZMBV_FMT_24BPP:
  518. for (j = 0; j < c->height; j++) {
  519. memcpy(out, src, c->width * 3);
  520. src += c->width * 3;
  521. out += c->pic.linesize[0];
  522. }
  523. break;
  524. #endif //ZMBV_ENABLE_24BPP
  525. case ZMBV_FMT_32BPP:
  526. for (j = 0; j < c->height; j++) {
  527. for (i = 0; i < c->width; i++) {
  528. uint32_t tmp = AV_RL32(src);
  529. src += 4;
  530. AV_WB24(out+(i*3), tmp);
  531. }
  532. out += c->pic.linesize[0];
  533. }
  534. break;
  535. default:
  536. av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
  537. }
  538. FFSWAP(uint8_t *, c->cur, c->prev);
  539. }
  540. *data_size = sizeof(AVFrame);
  541. *(AVFrame*)data = c->pic;
  542. /* always report that the buffer was completely consumed */
  543. return buf_size;
  544. }
  545. /*
  546. *
  547. * Init zmbv decoder
  548. *
  549. */
  550. static av_cold int decode_init(AVCodecContext *avctx)
  551. {
  552. ZmbvContext * const c = avctx->priv_data;
  553. int zret; // Zlib return code
  554. c->avctx = avctx;
  555. c->width = avctx->width;
  556. c->height = avctx->height;
  557. c->bpp = avctx->bits_per_coded_sample;
  558. // Needed if zlib unused or init aborted before inflateInit
  559. memset(&c->zstream, 0, sizeof(z_stream));
  560. avctx->pix_fmt = PIX_FMT_RGB24;
  561. c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
  562. /* Allocate decompression buffer */
  563. if (c->decomp_size) {
  564. if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
  565. av_log(avctx, AV_LOG_ERROR,
  566. "Can't allocate decompression buffer.\n");
  567. return AVERROR(ENOMEM);
  568. }
  569. }
  570. c->zstream.zalloc = Z_NULL;
  571. c->zstream.zfree = Z_NULL;
  572. c->zstream.opaque = Z_NULL;
  573. zret = inflateInit(&c->zstream);
  574. if (zret != Z_OK) {
  575. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  576. return -1;
  577. }
  578. return 0;
  579. }
  580. /*
  581. *
  582. * Uninit zmbv decoder
  583. *
  584. */
  585. static av_cold int decode_end(AVCodecContext *avctx)
  586. {
  587. ZmbvContext * const c = avctx->priv_data;
  588. av_freep(&c->decomp_buf);
  589. if (c->pic.data[0])
  590. avctx->release_buffer(avctx, &c->pic);
  591. inflateEnd(&c->zstream);
  592. av_freep(&c->cur);
  593. av_freep(&c->prev);
  594. return 0;
  595. }
  596. AVCodec ff_zmbv_decoder = {
  597. .name = "zmbv",
  598. .type = AVMEDIA_TYPE_VIDEO,
  599. .id = CODEC_ID_ZMBV,
  600. .priv_data_size = sizeof(ZmbvContext),
  601. .init = decode_init,
  602. .close = decode_end,
  603. .decode = decode_frame,
  604. .capabilities = CODEC_CAP_DR1,
  605. .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
  606. };