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.

640 lines
18KB

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