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.

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