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.

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