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.

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