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.

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