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.

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