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.

476 lines
13KB

  1. /*
  2. * Zip Motion Blocks Video (ZMBV) decoder
  3. * Copyright (c) 2006 Konstantin Shishkov
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. */
  20. /**
  21. * @file zmbv.c
  22. * Zip Motion Blocks Video decoder
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include "common.h"
  27. #include "avcodec.h"
  28. #ifdef CONFIG_ZLIB
  29. #include <zlib.h>
  30. #endif
  31. #define ZMBV_KEYFRAME 1
  32. #define ZMBV_DELTAPAL 2
  33. enum ZmbvFormat {
  34. ZMBV_FMT_NONE = 0,
  35. ZMBV_FMT_1BPP = 1,
  36. ZMBV_FMT_2BPP = 2,
  37. ZMBV_FMT_4BPP = 3,
  38. ZMBV_FMT_8BPP = 4,
  39. ZMBV_FMT_15BPP = 5,
  40. ZMBV_FMT_16BPP = 6,
  41. ZMBV_FMT_24BPP = 7,
  42. ZMBV_FMT_32BPP = 8
  43. };
  44. /*
  45. * Decoder context
  46. */
  47. typedef struct ZmbvContext {
  48. AVCodecContext *avctx;
  49. AVFrame pic;
  50. int bpp;
  51. unsigned int decomp_size;
  52. uint8_t* decomp_buf;
  53. uint8_t pal[768];
  54. uint8_t *prev, *cur;
  55. int width, height;
  56. int fmt;
  57. int comp;
  58. int flags;
  59. int bw, bh, bx, by;
  60. int decomp_len;
  61. #ifdef CONFIG_ZLIB
  62. z_stream zstream;
  63. #endif
  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 %i of %i bytes\n", src-c->decomp_buf, c->decomp_len);
  131. return 0;
  132. }
  133. /**
  134. * Decode XOR'ed frame - 24bpp version
  135. */
  136. static int zmbv_decode_xor_24(ZmbvContext *c)
  137. {
  138. uint8_t *src = c->decomp_buf;
  139. uint8_t *output, *prev;
  140. int8_t *mvec;
  141. int x, y;
  142. int d, dx, dy, bw2, bh2;
  143. int block;
  144. int i, j;
  145. int mx, my;
  146. int stride;
  147. output = c->cur;
  148. prev = c->prev;
  149. stride = c->width * 3;
  150. mvec = (int8_t*)src;
  151. src += ((c->bx * c->by * 2 + 3) & ~3);
  152. block = 0;
  153. for(y = 0; y < c->height; y += c->bh) {
  154. bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
  155. for(x = 0; x < c->width; x += c->bw) {
  156. uint8_t *out, *tprev;
  157. d = mvec[block] & 1;
  158. dx = mvec[block] >> 1;
  159. dy = mvec[block + 1] >> 1;
  160. block += 2;
  161. bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
  162. /* copy block - motion vectors out of bounds are used to zero blocks */
  163. out = output + x * 3;
  164. tprev = prev + (x + dx) * 3 + dy * stride;
  165. mx = x + dx;
  166. my = y + dy;
  167. for(j = 0; j < bh2; j++){
  168. if((my + j < 0) || (my + j >= c->height)) {
  169. memset(out, 0, bw2 * 3);
  170. } else {
  171. for(i = 0; i < bw2; i++){
  172. if((mx + i < 0) || (mx + i >= c->width)) {
  173. out[i * 3 + 0] = 0;
  174. out[i * 3 + 1] = 0;
  175. out[i * 3 + 2] = 0;
  176. } else {
  177. out[i * 3 + 0] = tprev[i * 3 + 0];
  178. out[i * 3 + 1] = tprev[i * 3 + 1];
  179. out[i * 3 + 2] = tprev[i * 3 + 2];
  180. }
  181. }
  182. }
  183. out += stride;
  184. tprev += stride;
  185. }
  186. if(d) { /* apply XOR'ed difference */
  187. out = output + x * 3;
  188. for(j = 0; j < bh2; j++){
  189. for(i = 0; i < bw2; i++) {
  190. out[i * 3 + 0] ^= *src++;
  191. out[i * 3 + 1] ^= *src++;
  192. out[i * 3 + 2] ^= *src++;
  193. }
  194. out += stride;
  195. }
  196. }
  197. }
  198. output += stride * c->bh;
  199. prev += stride * c->bh;
  200. }
  201. if(src - c->decomp_buf != c->decomp_len)
  202. av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n", src-c->decomp_buf, c->decomp_len);
  203. return 0;
  204. }
  205. /**
  206. * Decode intraframe
  207. */
  208. static int zmbv_decode_intra(ZmbvContext *c)
  209. {
  210. uint8_t *src = c->decomp_buf;
  211. /* make the palette available on the way out */
  212. if (c->fmt == ZMBV_FMT_8BPP) {
  213. memcpy(c->pal, src, 768);
  214. src += 768;
  215. }
  216. memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
  217. return 0;
  218. }
  219. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
  220. {
  221. ZmbvContext * const c = (ZmbvContext *)avctx->priv_data;
  222. uint8_t *outptr;
  223. #ifdef CONFIG_ZLIB
  224. int zret = Z_OK; // Zlib return code
  225. #endif
  226. int len = buf_size;
  227. int hi_ver, lo_ver;
  228. if(c->pic.data[0])
  229. avctx->release_buffer(avctx, &c->pic);
  230. c->pic.reference = 1;
  231. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  232. if(avctx->get_buffer(avctx, &c->pic) < 0){
  233. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  234. return -1;
  235. }
  236. outptr = c->pic.data[0]; // Output image pointer
  237. /* parse header */
  238. c->flags = buf[0];
  239. buf++; len--;
  240. if(c->flags & ZMBV_KEYFRAME) {
  241. hi_ver = buf[0];
  242. lo_ver = buf[1];
  243. c->comp = buf[2];
  244. c->fmt = buf[3];
  245. c->bw = buf[4];
  246. c->bh = buf[5];
  247. buf += 6;
  248. len -= 6;
  249. 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);
  250. if(hi_ver != 0 || lo_ver != 1) {
  251. av_log(avctx, AV_LOG_ERROR, "Unsupported version %i.%i\n", hi_ver, lo_ver);
  252. return -1;
  253. }
  254. if(c->bw == 0 || c->bh == 0) {
  255. av_log(avctx, AV_LOG_ERROR, "Unsupported block size %ix%i\n", c->bw, c->bh);
  256. }
  257. if(c->comp != 0 && c->comp != 1) {
  258. av_log(avctx, AV_LOG_ERROR, "Unsupported compression type %i\n", c->comp);
  259. return -1;
  260. }
  261. if(c->fmt != ZMBV_FMT_8BPP && c->fmt != ZMBV_FMT_24BPP) {
  262. av_log(avctx, AV_LOG_ERROR, "Unsupported (for now) format %i\n", c->fmt);
  263. return -1;
  264. }
  265. #ifdef CONFIG_ZLIB
  266. zret = inflateReset(&c->zstream);
  267. if (zret != Z_OK) {
  268. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
  269. return -1;
  270. }
  271. #else
  272. av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
  273. return -1;
  274. #endif /* CONFIG_ZLIB */
  275. if(c->fmt == ZMBV_FMT_8BPP) {
  276. c->bpp = 8;
  277. c->decode_intra = zmbv_decode_intra;
  278. c->decode_xor = zmbv_decode_xor_8;
  279. } else {
  280. c->bpp = 24;
  281. c->decode_intra = zmbv_decode_intra;
  282. c->decode_xor = zmbv_decode_xor_24;
  283. }
  284. c->cur = av_realloc(c->cur, avctx->width * avctx->height * (c->bpp / 8));
  285. c->prev = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8));
  286. c->bx = (c->width + c->bw - 1) / c->bw;
  287. c->by = (c->height+ c->bh - 1) / c->bh;
  288. }
  289. if(c->fmt == 0) {
  290. av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
  291. return -1;
  292. }
  293. if(c->comp == 0) { //Uncompressed data
  294. memcpy(c->decomp_buf, buf, len);
  295. c->decomp_size = 1;
  296. } else { // ZLIB-compressed data
  297. #ifdef CONFIG_ZLIB
  298. c->zstream.total_in = c->zstream.total_out = 0;
  299. c->zstream.next_in = buf;
  300. c->zstream.avail_in = len;
  301. c->zstream.next_out = c->decomp_buf;
  302. c->zstream.avail_out = c->decomp_size;
  303. inflate(&c->zstream, Z_FINISH);
  304. c->decomp_len = c->zstream.total_out;
  305. #else
  306. av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
  307. return -1;
  308. #endif
  309. }
  310. if(c->flags & ZMBV_KEYFRAME) {
  311. c->pic.key_frame = 1;
  312. c->pic.pict_type = FF_I_TYPE;
  313. c->decode_intra(c);
  314. } else {
  315. c->pic.key_frame = 0;
  316. c->pic.pict_type = FF_P_TYPE;
  317. c->decode_xor(c);
  318. }
  319. /* update frames */
  320. {
  321. uint8_t *out, *src;
  322. int i, j;
  323. out = c->pic.data[0];
  324. src = c->cur;
  325. for(j = 0; j < c->height; j++) {
  326. for(i = 0; i < c->width; i++) {
  327. out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
  328. out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
  329. out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
  330. *src++;
  331. }
  332. out += c->pic.linesize[0];
  333. }
  334. memcpy(c->prev, c->cur, c->width * c->height);
  335. }
  336. *data_size = sizeof(AVFrame);
  337. *(AVFrame*)data = c->pic;
  338. /* always report that the buffer was completely consumed */
  339. return buf_size;
  340. }
  341. /*
  342. *
  343. * Init zmbv decoder
  344. *
  345. */
  346. static int decode_init(AVCodecContext *avctx)
  347. {
  348. ZmbvContext * const c = (ZmbvContext *)avctx->priv_data;
  349. int zret; // Zlib return code
  350. c->avctx = avctx;
  351. avctx->has_b_frames = 0;
  352. c->pic.data[0] = NULL;
  353. c->width = avctx->width;
  354. c->height = avctx->height;
  355. if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
  356. return 1;
  357. }
  358. c->bpp = avctx->bits_per_sample;
  359. #ifdef CONFIG_ZLIB
  360. // Needed if zlib unused or init aborted before inflateInit
  361. memset(&(c->zstream), 0, sizeof(z_stream));
  362. #else
  363. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
  364. return 1;
  365. #endif
  366. avctx->pix_fmt = PIX_FMT_RGB24;
  367. c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
  368. /* Allocate decompression buffer */
  369. if (c->decomp_size) {
  370. if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
  371. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  372. return 1;
  373. }
  374. }
  375. #ifdef CONFIG_ZLIB
  376. c->zstream.zalloc = Z_NULL;
  377. c->zstream.zfree = Z_NULL;
  378. c->zstream.opaque = Z_NULL;
  379. zret = inflateInit(&(c->zstream));
  380. if (zret != Z_OK) {
  381. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  382. return 1;
  383. }
  384. #endif
  385. return 0;
  386. }
  387. /*
  388. *
  389. * Uninit zmbv decoder
  390. *
  391. */
  392. static int decode_end(AVCodecContext *avctx)
  393. {
  394. ZmbvContext * const c = (ZmbvContext *)avctx->priv_data;
  395. av_freep(&c->decomp_buf);
  396. if (c->pic.data[0])
  397. avctx->release_buffer(avctx, &c->pic);
  398. #ifdef CONFIG_ZLIB
  399. inflateEnd(&(c->zstream));
  400. #endif
  401. if(c->cur)
  402. av_freep(&c->cur);
  403. if(c->prev)
  404. av_freep(&c->prev);
  405. return 0;
  406. }
  407. AVCodec zmbv_decoder = {
  408. "zmbv",
  409. CODEC_TYPE_VIDEO,
  410. CODEC_ID_ZMBV,
  411. sizeof(ZmbvContext),
  412. decode_init,
  413. NULL,
  414. decode_end,
  415. decode_frame
  416. };