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.

685 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/common.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "avcodec.h"
  30. #include <zlib.h>
  31. #define ZMBV_KEYFRAME 1
  32. #define ZMBV_DELTAPAL 2
  33. enum ZmbvFormat {
  34. ZMBV_FMT_NONE = 0,
  35. ZMBV_FMT_1BPP = 1,
  36. ZMBV_FMT_2BPP = 2,
  37. ZMBV_FMT_4BPP = 3,
  38. ZMBV_FMT_8BPP = 4,
  39. ZMBV_FMT_15BPP = 5,
  40. ZMBV_FMT_16BPP = 6,
  41. ZMBV_FMT_24BPP = 7,
  42. ZMBV_FMT_32BPP = 8
  43. };
  44. /*
  45. * Decoder context
  46. */
  47. typedef struct ZmbvContext {
  48. AVCodecContext *avctx;
  49. AVFrame pic;
  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 *data_size, AVPacket *avpkt)
  352. {
  353. const uint8_t *buf = avpkt->data;
  354. int buf_size = avpkt->size;
  355. ZmbvContext * const c = avctx->priv_data;
  356. int zret = Z_OK; // Zlib return code
  357. int len = buf_size;
  358. int hi_ver, lo_ver, ret;
  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 = (uint8_t*)buf;
  452. c->zstream.avail_in = len;
  453. c->zstream.next_out = c->decomp_buf;
  454. c->zstream.avail_out = c->decomp_size;
  455. zret = inflate(&c->zstream, Z_SYNC_FLUSH);
  456. if (zret != Z_OK && zret != Z_STREAM_END) {
  457. av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
  458. return AVERROR_INVALIDDATA;
  459. }
  460. c->decomp_len = c->zstream.total_out;
  461. }
  462. if (c->flags & ZMBV_KEYFRAME) {
  463. c->pic.key_frame = 1;
  464. c->pic.pict_type = AV_PICTURE_TYPE_I;
  465. c->decode_intra(c);
  466. } else {
  467. c->pic.key_frame = 0;
  468. c->pic.pict_type = AV_PICTURE_TYPE_P;
  469. if (c->decomp_len)
  470. c->decode_xor(c);
  471. }
  472. /* update frames */
  473. {
  474. uint8_t *out, *src;
  475. int i, j;
  476. out = c->pic.data[0];
  477. src = c->cur;
  478. switch (c->fmt) {
  479. case ZMBV_FMT_8BPP:
  480. for (j = 0; j < c->height; j++) {
  481. for (i = 0; i < c->width; i++) {
  482. out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
  483. out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
  484. out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
  485. src++;
  486. }
  487. out += c->pic.linesize[0];
  488. }
  489. break;
  490. case ZMBV_FMT_15BPP:
  491. for (j = 0; j < c->height; j++) {
  492. for (i = 0; i < c->width; i++) {
  493. uint16_t tmp = AV_RL16(src);
  494. src += 2;
  495. out[i * 3 + 0] = (tmp & 0x7C00) >> 7;
  496. out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
  497. out[i * 3 + 2] = (tmp & 0x001F) << 3;
  498. }
  499. out += c->pic.linesize[0];
  500. }
  501. break;
  502. case ZMBV_FMT_16BPP:
  503. for (j = 0; j < c->height; j++) {
  504. for (i = 0; i < c->width; i++) {
  505. uint16_t tmp = AV_RL16(src);
  506. src += 2;
  507. out[i * 3 + 0] = (tmp & 0xF800) >> 8;
  508. out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
  509. out[i * 3 + 2] = (tmp & 0x001F) << 3;
  510. }
  511. out += c->pic.linesize[0];
  512. }
  513. break;
  514. #ifdef ZMBV_ENABLE_24BPP
  515. case ZMBV_FMT_24BPP:
  516. for (j = 0; j < c->height; j++) {
  517. memcpy(out, src, c->width * 3);
  518. src += c->width * 3;
  519. out += c->pic.linesize[0];
  520. }
  521. break;
  522. #endif //ZMBV_ENABLE_24BPP
  523. case ZMBV_FMT_32BPP:
  524. for (j = 0; j < c->height; j++) {
  525. for (i = 0; i < c->width; i++) {
  526. uint32_t tmp = AV_RL32(src);
  527. src += 4;
  528. AV_WB24(out+(i*3), tmp);
  529. }
  530. out += c->pic.linesize[0];
  531. }
  532. break;
  533. default:
  534. av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
  535. }
  536. FFSWAP(uint8_t *, c->cur, c->prev);
  537. }
  538. *data_size = sizeof(AVFrame);
  539. *(AVFrame*)data = c->pic;
  540. /* always report that the buffer was completely consumed */
  541. return buf_size;
  542. }
  543. /*
  544. *
  545. * Init zmbv decoder
  546. *
  547. */
  548. static av_cold int decode_init(AVCodecContext *avctx)
  549. {
  550. ZmbvContext * const c = avctx->priv_data;
  551. int zret; // Zlib return code
  552. c->avctx = avctx;
  553. c->width = avctx->width;
  554. c->height = avctx->height;
  555. avcodec_get_frame_defaults(&c->pic);
  556. c->bpp = avctx->bits_per_coded_sample;
  557. // Needed if zlib unused or init aborted before inflateInit
  558. memset(&c->zstream, 0, sizeof(z_stream));
  559. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  560. c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
  561. /* Allocate decompression buffer */
  562. if (c->decomp_size) {
  563. if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
  564. av_log(avctx, AV_LOG_ERROR,
  565. "Can't allocate decompression buffer.\n");
  566. return AVERROR(ENOMEM);
  567. }
  568. }
  569. c->zstream.zalloc = Z_NULL;
  570. c->zstream.zfree = Z_NULL;
  571. c->zstream.opaque = Z_NULL;
  572. zret = inflateInit(&c->zstream);
  573. if (zret != Z_OK) {
  574. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  575. return -1;
  576. }
  577. return 0;
  578. }
  579. /*
  580. *
  581. * Uninit zmbv decoder
  582. *
  583. */
  584. static av_cold int decode_end(AVCodecContext *avctx)
  585. {
  586. ZmbvContext * const c = avctx->priv_data;
  587. av_freep(&c->decomp_buf);
  588. if (c->pic.data[0])
  589. avctx->release_buffer(avctx, &c->pic);
  590. inflateEnd(&c->zstream);
  591. av_freep(&c->cur);
  592. av_freep(&c->prev);
  593. return 0;
  594. }
  595. AVCodec ff_zmbv_decoder = {
  596. .name = "zmbv",
  597. .type = AVMEDIA_TYPE_VIDEO,
  598. .id = AV_CODEC_ID_ZMBV,
  599. .priv_data_size = sizeof(ZmbvContext),
  600. .init = decode_init,
  601. .close = decode_end,
  602. .decode = decode_frame,
  603. .capabilities = CODEC_CAP_DR1,
  604. .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
  605. };