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.

669 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. uint8_t *outptr;
  352. int zret = Z_OK; // Zlib return code
  353. int len = buf_size;
  354. int hi_ver, lo_ver;
  355. if(c->pic.data[0])
  356. avctx->release_buffer(avctx, &c->pic);
  357. c->pic.reference = 1;
  358. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  359. if(avctx->get_buffer(avctx, &c->pic) < 0){
  360. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  361. return -1;
  362. }
  363. outptr = c->pic.data[0]; // Output image pointer
  364. /* parse header */
  365. c->flags = buf[0];
  366. buf++; len--;
  367. if(c->flags & ZMBV_KEYFRAME) {
  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. buf += 6;
  375. len -= 6;
  376. 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);
  377. if(hi_ver != 0 || lo_ver != 1) {
  378. av_log(avctx, AV_LOG_ERROR, "Unsupported version %i.%i\n", hi_ver, lo_ver);
  379. return -1;
  380. }
  381. if(c->bw == 0 || c->bh == 0) {
  382. av_log(avctx, AV_LOG_ERROR, "Unsupported block size %ix%i\n", c->bw, c->bh);
  383. return -1;
  384. }
  385. if(c->comp != 0 && c->comp != 1) {
  386. av_log(avctx, AV_LOG_ERROR, "Unsupported compression type %i\n", c->comp);
  387. return -1;
  388. }
  389. switch(c->fmt) {
  390. case ZMBV_FMT_8BPP:
  391. c->bpp = 8;
  392. c->decode_intra = zmbv_decode_intra;
  393. c->decode_xor = zmbv_decode_xor_8;
  394. break;
  395. case ZMBV_FMT_15BPP:
  396. case ZMBV_FMT_16BPP:
  397. c->bpp = 16;
  398. c->decode_intra = zmbv_decode_intra;
  399. c->decode_xor = zmbv_decode_xor_16;
  400. break;
  401. #ifdef ZMBV_ENABLE_24BPP
  402. case ZMBV_FMT_24BPP:
  403. c->bpp = 24;
  404. c->decode_intra = zmbv_decode_intra;
  405. c->decode_xor = zmbv_decode_xor_24;
  406. break;
  407. #endif //ZMBV_ENABLE_24BPP
  408. case ZMBV_FMT_32BPP:
  409. c->bpp = 32;
  410. c->decode_intra = zmbv_decode_intra;
  411. c->decode_xor = zmbv_decode_xor_32;
  412. break;
  413. default:
  414. c->decode_intra = NULL;
  415. c->decode_xor = NULL;
  416. av_log(avctx, AV_LOG_ERROR, "Unsupported (for now) format %i\n", c->fmt);
  417. return -1;
  418. }
  419. zret = inflateReset(&c->zstream);
  420. if (zret != Z_OK) {
  421. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
  422. return -1;
  423. }
  424. c->cur = av_realloc(c->cur, avctx->width * avctx->height * (c->bpp / 8));
  425. c->prev = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8));
  426. c->bx = (c->width + c->bw - 1) / c->bw;
  427. c->by = (c->height+ c->bh - 1) / c->bh;
  428. }
  429. if(c->decode_intra == NULL) {
  430. av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
  431. return -1;
  432. }
  433. if(c->comp == 0) { //Uncompressed data
  434. memcpy(c->decomp_buf, buf, len);
  435. c->decomp_size = 1;
  436. } else { // ZLIB-compressed data
  437. c->zstream.total_in = c->zstream.total_out = 0;
  438. c->zstream.next_in = buf;
  439. c->zstream.avail_in = len;
  440. c->zstream.next_out = c->decomp_buf;
  441. c->zstream.avail_out = c->decomp_size;
  442. inflate(&c->zstream, Z_FINISH);
  443. c->decomp_len = c->zstream.total_out;
  444. }
  445. if(c->flags & ZMBV_KEYFRAME) {
  446. c->pic.key_frame = 1;
  447. c->pic.pict_type = FF_I_TYPE;
  448. c->decode_intra(c);
  449. } else {
  450. c->pic.key_frame = 0;
  451. c->pic.pict_type = FF_P_TYPE;
  452. if(c->decomp_len)
  453. c->decode_xor(c);
  454. }
  455. /* update frames */
  456. {
  457. uint8_t *out, *src;
  458. int i, j;
  459. out = c->pic.data[0];
  460. src = c->cur;
  461. switch(c->fmt) {
  462. case ZMBV_FMT_8BPP:
  463. for(j = 0; j < c->height; j++) {
  464. for(i = 0; i < c->width; i++) {
  465. out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
  466. out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
  467. out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
  468. src++;
  469. }
  470. out += c->pic.linesize[0];
  471. }
  472. break;
  473. case ZMBV_FMT_15BPP:
  474. for(j = 0; j < c->height; j++) {
  475. for(i = 0; i < c->width; i++) {
  476. uint16_t tmp = AV_RL16(src);
  477. src += 2;
  478. out[i * 3 + 0] = (tmp & 0x7C00) >> 7;
  479. out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
  480. out[i * 3 + 2] = (tmp & 0x001F) << 3;
  481. }
  482. out += c->pic.linesize[0];
  483. }
  484. break;
  485. case ZMBV_FMT_16BPP:
  486. for(j = 0; j < c->height; j++) {
  487. for(i = 0; i < c->width; i++) {
  488. uint16_t tmp = AV_RL16(src);
  489. src += 2;
  490. out[i * 3 + 0] = (tmp & 0xF800) >> 8;
  491. out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
  492. out[i * 3 + 2] = (tmp & 0x001F) << 3;
  493. }
  494. out += c->pic.linesize[0];
  495. }
  496. break;
  497. #ifdef ZMBV_ENABLE_24BPP
  498. case ZMBV_FMT_24BPP:
  499. for(j = 0; j < c->height; j++) {
  500. memcpy(out, src, c->width * 3);
  501. src += c->width * 3;
  502. out += c->pic.linesize[0];
  503. }
  504. break;
  505. #endif //ZMBV_ENABLE_24BPP
  506. case ZMBV_FMT_32BPP:
  507. for(j = 0; j < c->height; j++) {
  508. for(i = 0; i < c->width; i++) {
  509. uint32_t tmp = AV_RL32(src);
  510. src += 4;
  511. AV_WB24(out+(i*3), tmp);
  512. }
  513. out += c->pic.linesize[0];
  514. }
  515. break;
  516. default:
  517. av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
  518. }
  519. memcpy(c->prev, c->cur, c->width * c->height * (c->bpp / 8));
  520. }
  521. *data_size = sizeof(AVFrame);
  522. *(AVFrame*)data = c->pic;
  523. /* always report that the buffer was completely consumed */
  524. return buf_size;
  525. }
  526. /*
  527. *
  528. * Init zmbv decoder
  529. *
  530. */
  531. static av_cold int decode_init(AVCodecContext *avctx)
  532. {
  533. ZmbvContext * const c = avctx->priv_data;
  534. int zret; // Zlib return code
  535. c->avctx = avctx;
  536. c->width = avctx->width;
  537. c->height = avctx->height;
  538. c->bpp = avctx->bits_per_coded_sample;
  539. // Needed if zlib unused or init aborted before inflateInit
  540. memset(&(c->zstream), 0, sizeof(z_stream));
  541. avctx->pix_fmt = PIX_FMT_RGB24;
  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_malloc(c->decomp_size)) == NULL) {
  546. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  547. return 1;
  548. }
  549. }
  550. c->zstream.zalloc = Z_NULL;
  551. c->zstream.zfree = Z_NULL;
  552. c->zstream.opaque = Z_NULL;
  553. zret = inflateInit(&(c->zstream));
  554. if (zret != Z_OK) {
  555. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  556. return 1;
  557. }
  558. return 0;
  559. }
  560. /*
  561. *
  562. * Uninit zmbv decoder
  563. *
  564. */
  565. static av_cold int decode_end(AVCodecContext *avctx)
  566. {
  567. ZmbvContext * const c = avctx->priv_data;
  568. av_freep(&c->decomp_buf);
  569. if (c->pic.data[0])
  570. avctx->release_buffer(avctx, &c->pic);
  571. inflateEnd(&(c->zstream));
  572. av_freep(&c->cur);
  573. av_freep(&c->prev);
  574. return 0;
  575. }
  576. AVCodec zmbv_decoder = {
  577. "zmbv",
  578. AVMEDIA_TYPE_VIDEO,
  579. CODEC_ID_ZMBV,
  580. sizeof(ZmbvContext),
  581. decode_init,
  582. NULL,
  583. decode_end,
  584. decode_frame,
  585. CODEC_CAP_DR1,
  586. .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
  587. };