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.

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