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.

636 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. avpriv_request_sample(avctx, "Version %i.%i", hi_ver, lo_ver);
  382. return AVERROR_PATCHWELCOME;
  383. }
  384. if (c->bw == 0 || c->bh == 0) {
  385. avpriv_request_sample(avctx, "Block size %ix%i", c->bw, c->bh);
  386. return AVERROR_PATCHWELCOME;
  387. }
  388. if (c->comp != 0 && c->comp != 1) {
  389. avpriv_request_sample(avctx, "Compression type %i", c->comp);
  390. return AVERROR_PATCHWELCOME;
  391. }
  392. switch (c->fmt) {
  393. case ZMBV_FMT_8BPP:
  394. c->bpp = 8;
  395. decode_intra = zmbv_decode_intra;
  396. c->decode_xor = zmbv_decode_xor_8;
  397. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  398. c->stride = c->width;
  399. break;
  400. case ZMBV_FMT_15BPP:
  401. case ZMBV_FMT_16BPP:
  402. c->bpp = 16;
  403. decode_intra = zmbv_decode_intra;
  404. c->decode_xor = zmbv_decode_xor_16;
  405. if (c->fmt == ZMBV_FMT_15BPP)
  406. avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
  407. else
  408. avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
  409. c->stride = c->width * 2;
  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. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  417. c->stride = c->width * 3;
  418. break;
  419. #endif //ZMBV_ENABLE_24BPP
  420. case ZMBV_FMT_32BPP:
  421. c->bpp = 32;
  422. decode_intra = zmbv_decode_intra;
  423. c->decode_xor = zmbv_decode_xor_32;
  424. avctx->pix_fmt = AV_PIX_FMT_BGR0;
  425. c->stride = c->width * 4;
  426. break;
  427. default:
  428. c->decode_xor = NULL;
  429. avpriv_request_sample(avctx, "Format %i", c->fmt);
  430. return AVERROR_PATCHWELCOME;
  431. }
  432. zret = inflateReset(&c->zstream);
  433. if (zret != Z_OK) {
  434. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
  435. return AVERROR_UNKNOWN;
  436. }
  437. c->cur = av_realloc_f(c->cur, avctx->width * avctx->height, (c->bpp / 8));
  438. c->prev = av_realloc_f(c->prev, avctx->width * avctx->height, (c->bpp / 8));
  439. c->bx = (c->width + c->bw - 1) / c->bw;
  440. c->by = (c->height+ c->bh - 1) / c->bh;
  441. if (!c->cur || !c->prev)
  442. return -1;
  443. memset(c->cur, 0, avctx->width * avctx->height * (c->bpp / 8));
  444. memset(c->prev, 0, avctx->width * avctx->height * (c->bpp / 8));
  445. c->decode_intra= decode_intra;
  446. }
  447. if (c->decode_intra == NULL) {
  448. av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
  449. return AVERROR_INVALIDDATA;
  450. }
  451. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  452. return ret;
  453. if (c->comp == 0) { //Uncompressed data
  454. if (c->decomp_size < len) {
  455. av_log(avctx, AV_LOG_ERROR, "decomp buffer too small\n");
  456. return AVERROR_INVALIDDATA;
  457. }
  458. memcpy(c->decomp_buf, buf, len);
  459. } else { // ZLIB-compressed data
  460. c->zstream.total_in = c->zstream.total_out = 0;
  461. c->zstream.next_in = (uint8_t*)buf;
  462. c->zstream.avail_in = len;
  463. c->zstream.next_out = c->decomp_buf;
  464. c->zstream.avail_out = c->decomp_size;
  465. zret = inflate(&c->zstream, Z_SYNC_FLUSH);
  466. if (zret != Z_OK && zret != Z_STREAM_END) {
  467. av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
  468. return AVERROR_INVALIDDATA;
  469. }
  470. c->decomp_len = c->zstream.total_out;
  471. }
  472. if (c->flags & ZMBV_KEYFRAME) {
  473. frame->key_frame = 1;
  474. frame->pict_type = AV_PICTURE_TYPE_I;
  475. c->decode_intra(c);
  476. } else {
  477. frame->key_frame = 0;
  478. frame->pict_type = AV_PICTURE_TYPE_P;
  479. if (c->decomp_len)
  480. c->decode_xor(c);
  481. }
  482. /* update frames */
  483. {
  484. uint8_t *out, *src;
  485. int j;
  486. out = frame->data[0];
  487. src = c->cur;
  488. switch (c->fmt) {
  489. case ZMBV_FMT_8BPP:
  490. for (j = 0; j < 256; j++)
  491. AV_WN32(&frame->data[1][j * 4], 0xFFU << 24 | AV_RB24(&c->pal[j * 3]));
  492. case ZMBV_FMT_15BPP:
  493. case ZMBV_FMT_16BPP:
  494. #ifdef ZMBV_ENABLE_24BPP
  495. case ZMBV_FMT_24BPP:
  496. #endif
  497. case ZMBV_FMT_32BPP:
  498. for (j = 0; j < c->height; j++) {
  499. memcpy(out, src, c->stride);
  500. src += c->stride;
  501. out += frame->linesize[0];
  502. }
  503. break;
  504. default:
  505. av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
  506. }
  507. FFSWAP(uint8_t *, c->cur, c->prev);
  508. }
  509. *got_frame = 1;
  510. /* always report that the buffer was completely consumed */
  511. return buf_size;
  512. }
  513. static av_cold int decode_init(AVCodecContext *avctx)
  514. {
  515. ZmbvContext * const c = avctx->priv_data;
  516. int zret; // Zlib return code
  517. c->avctx = avctx;
  518. c->width = avctx->width;
  519. c->height = avctx->height;
  520. c->bpp = avctx->bits_per_coded_sample;
  521. // Needed if zlib unused or init aborted before inflateInit
  522. memset(&c->zstream, 0, sizeof(z_stream));
  523. c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
  524. /* Allocate decompression buffer */
  525. if (c->decomp_size) {
  526. if ((c->decomp_buf = av_mallocz(c->decomp_size)) == NULL) {
  527. av_log(avctx, AV_LOG_ERROR,
  528. "Can't allocate decompression buffer.\n");
  529. return AVERROR(ENOMEM);
  530. }
  531. }
  532. c->zstream.zalloc = Z_NULL;
  533. c->zstream.zfree = Z_NULL;
  534. c->zstream.opaque = Z_NULL;
  535. zret = inflateInit(&c->zstream);
  536. if (zret != Z_OK) {
  537. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  538. return AVERROR_UNKNOWN;
  539. }
  540. return 0;
  541. }
  542. static av_cold int decode_end(AVCodecContext *avctx)
  543. {
  544. ZmbvContext * const c = avctx->priv_data;
  545. av_freep(&c->decomp_buf);
  546. inflateEnd(&c->zstream);
  547. av_freep(&c->cur);
  548. av_freep(&c->prev);
  549. return 0;
  550. }
  551. AVCodec ff_zmbv_decoder = {
  552. .name = "zmbv",
  553. .type = AVMEDIA_TYPE_VIDEO,
  554. .id = AV_CODEC_ID_ZMBV,
  555. .priv_data_size = sizeof(ZmbvContext),
  556. .init = decode_init,
  557. .close = decode_end,
  558. .decode = decode_frame,
  559. .capabilities = CODEC_CAP_DR1,
  560. .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
  561. };