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.

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