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