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.

684 lines
19KB

  1. /*
  2. * Zip Motion Blocks Video (ZMBV) decoder
  3. * Copyright (c) 2006 Konstantin Shishkov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. if (c->pic.data[0])
  359. avctx->release_buffer(avctx, &c->pic);
  360. c->pic.reference = 3;
  361. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  362. if ((ret = avctx->get_buffer(avctx, &c->pic)) < 0) {
  363. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  364. return ret;
  365. }
  366. /* parse header */
  367. c->flags = buf[0];
  368. buf++; len--;
  369. if (c->flags & ZMBV_KEYFRAME) {
  370. void *decode_intra = NULL;
  371. c->decode_intra= NULL;
  372. hi_ver = buf[0];
  373. lo_ver = buf[1];
  374. c->comp = buf[2];
  375. c->fmt = buf[3];
  376. c->bw = buf[4];
  377. c->bh = buf[5];
  378. buf += 6;
  379. len -= 6;
  380. av_log(avctx, AV_LOG_DEBUG,
  381. "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",
  382. c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
  383. if (hi_ver != 0 || lo_ver != 1) {
  384. av_log_ask_for_sample(avctx, "Unsupported version %i.%i\n",
  385. hi_ver, lo_ver);
  386. return AVERROR_PATCHWELCOME;
  387. }
  388. if (c->bw == 0 || c->bh == 0) {
  389. av_log_ask_for_sample(avctx, "Unsupported block size %ix%i\n",
  390. c->bw, c->bh);
  391. return AVERROR_PATCHWELCOME;
  392. }
  393. if (c->comp != 0 && c->comp != 1) {
  394. av_log_ask_for_sample(avctx, "Unsupported compression type %i\n",
  395. c->comp);
  396. return AVERROR_PATCHWELCOME;
  397. }
  398. switch (c->fmt) {
  399. case ZMBV_FMT_8BPP:
  400. c->bpp = 8;
  401. decode_intra = zmbv_decode_intra;
  402. c->decode_xor = zmbv_decode_xor_8;
  403. break;
  404. case ZMBV_FMT_15BPP:
  405. case ZMBV_FMT_16BPP:
  406. c->bpp = 16;
  407. decode_intra = zmbv_decode_intra;
  408. c->decode_xor = zmbv_decode_xor_16;
  409. break;
  410. #ifdef ZMBV_ENABLE_24BPP
  411. case ZMBV_FMT_24BPP:
  412. c->bpp = 24;
  413. decode_intra = zmbv_decode_intra;
  414. c->decode_xor = zmbv_decode_xor_24;
  415. break;
  416. #endif //ZMBV_ENABLE_24BPP
  417. case ZMBV_FMT_32BPP:
  418. c->bpp = 32;
  419. decode_intra = zmbv_decode_intra;
  420. c->decode_xor = zmbv_decode_xor_32;
  421. break;
  422. default:
  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. c->cur = av_realloc_f(c->cur, avctx->width * avctx->height, (c->bpp / 8));
  434. c->prev = av_realloc_f(c->prev, avctx->width * avctx->height, (c->bpp / 8));
  435. c->bx = (c->width + c->bw - 1) / c->bw;
  436. c->by = (c->height+ c->bh - 1) / c->bh;
  437. if (!c->cur || !c->prev)
  438. return -1;
  439. c->decode_intra= decode_intra;
  440. }
  441. if (c->decode_intra == NULL) {
  442. av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
  443. return AVERROR_INVALIDDATA;
  444. }
  445. if (c->comp == 0) { //Uncompressed data
  446. memcpy(c->decomp_buf, buf, len);
  447. c->decomp_size = 1;
  448. } else { // ZLIB-compressed data
  449. c->zstream.total_in = c->zstream.total_out = 0;
  450. c->zstream.next_in = buf;
  451. c->zstream.avail_in = len;
  452. c->zstream.next_out = c->decomp_buf;
  453. c->zstream.avail_out = c->decomp_size;
  454. zret = inflate(&c->zstream, Z_SYNC_FLUSH);
  455. if (zret != Z_OK && zret != Z_STREAM_END) {
  456. av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
  457. return AVERROR_INVALIDDATA;
  458. }
  459. c->decomp_len = c->zstream.total_out;
  460. }
  461. if (c->flags & ZMBV_KEYFRAME) {
  462. c->pic.key_frame = 1;
  463. c->pic.pict_type = AV_PICTURE_TYPE_I;
  464. c->decode_intra(c);
  465. } else {
  466. c->pic.key_frame = 0;
  467. c->pic.pict_type = AV_PICTURE_TYPE_P;
  468. if (c->decomp_len)
  469. c->decode_xor(c);
  470. }
  471. /* update frames */
  472. {
  473. uint8_t *out, *src;
  474. int i, j;
  475. out = c->pic.data[0];
  476. src = c->cur;
  477. switch (c->fmt) {
  478. case ZMBV_FMT_8BPP:
  479. for (j = 0; j < c->height; j++) {
  480. for (i = 0; i < c->width; i++) {
  481. out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
  482. out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
  483. out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
  484. src++;
  485. }
  486. out += c->pic.linesize[0];
  487. }
  488. break;
  489. case ZMBV_FMT_15BPP:
  490. for (j = 0; j < c->height; j++) {
  491. for (i = 0; i < c->width; i++) {
  492. uint16_t tmp = AV_RL16(src);
  493. src += 2;
  494. out[i * 3 + 0] = (tmp & 0x7C00) >> 7;
  495. out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
  496. out[i * 3 + 2] = (tmp & 0x001F) << 3;
  497. }
  498. out += c->pic.linesize[0];
  499. }
  500. break;
  501. case ZMBV_FMT_16BPP:
  502. for (j = 0; j < c->height; j++) {
  503. for (i = 0; i < c->width; i++) {
  504. uint16_t tmp = AV_RL16(src);
  505. src += 2;
  506. out[i * 3 + 0] = (tmp & 0xF800) >> 8;
  507. out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
  508. out[i * 3 + 2] = (tmp & 0x001F) << 3;
  509. }
  510. out += c->pic.linesize[0];
  511. }
  512. break;
  513. #ifdef ZMBV_ENABLE_24BPP
  514. case ZMBV_FMT_24BPP:
  515. for (j = 0; j < c->height; j++) {
  516. memcpy(out, src, c->width * 3);
  517. src += c->width * 3;
  518. out += c->pic.linesize[0];
  519. }
  520. break;
  521. #endif //ZMBV_ENABLE_24BPP
  522. case ZMBV_FMT_32BPP:
  523. for (j = 0; j < c->height; j++) {
  524. for (i = 0; i < c->width; i++) {
  525. uint32_t tmp = AV_RL32(src);
  526. src += 4;
  527. AV_WB24(out+(i*3), tmp);
  528. }
  529. out += c->pic.linesize[0];
  530. }
  531. break;
  532. default:
  533. av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
  534. }
  535. FFSWAP(uint8_t *, c->cur, c->prev);
  536. }
  537. *data_size = sizeof(AVFrame);
  538. *(AVFrame*)data = c->pic;
  539. /* always report that the buffer was completely consumed */
  540. return buf_size;
  541. }
  542. /*
  543. *
  544. * Init zmbv decoder
  545. *
  546. */
  547. static av_cold int decode_init(AVCodecContext *avctx)
  548. {
  549. ZmbvContext * const c = avctx->priv_data;
  550. int zret; // Zlib return code
  551. c->avctx = avctx;
  552. c->width = avctx->width;
  553. c->height = avctx->height;
  554. avcodec_get_frame_defaults(&c->pic);
  555. c->bpp = avctx->bits_per_coded_sample;
  556. // Needed if zlib unused or init aborted before inflateInit
  557. memset(&c->zstream, 0, sizeof(z_stream));
  558. avctx->pix_fmt = PIX_FMT_RGB24;
  559. c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
  560. /* Allocate decompression buffer */
  561. if (c->decomp_size) {
  562. if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
  563. av_log(avctx, AV_LOG_ERROR,
  564. "Can't allocate decompression buffer.\n");
  565. return AVERROR(ENOMEM);
  566. }
  567. }
  568. c->zstream.zalloc = Z_NULL;
  569. c->zstream.zfree = Z_NULL;
  570. c->zstream.opaque = Z_NULL;
  571. zret = inflateInit(&c->zstream);
  572. if (zret != Z_OK) {
  573. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  574. return -1;
  575. }
  576. return 0;
  577. }
  578. /*
  579. *
  580. * Uninit zmbv decoder
  581. *
  582. */
  583. static av_cold int decode_end(AVCodecContext *avctx)
  584. {
  585. ZmbvContext * const c = avctx->priv_data;
  586. av_freep(&c->decomp_buf);
  587. if (c->pic.data[0])
  588. avctx->release_buffer(avctx, &c->pic);
  589. inflateEnd(&c->zstream);
  590. av_freep(&c->cur);
  591. av_freep(&c->prev);
  592. return 0;
  593. }
  594. AVCodec ff_zmbv_decoder = {
  595. .name = "zmbv",
  596. .type = AVMEDIA_TYPE_VIDEO,
  597. .id = CODEC_ID_ZMBV,
  598. .priv_data_size = sizeof(ZmbvContext),
  599. .init = decode_init,
  600. .close = decode_end,
  601. .decode = decode_frame,
  602. .capabilities = CODEC_CAP_DR1,
  603. .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
  604. };