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.

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