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.

471 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. zret = inflateReset(&c->zstream);
  266. if (zret != Z_OK) {
  267. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
  268. return -1;
  269. }
  270. if(c->fmt == ZMBV_FMT_8BPP) {
  271. c->bpp = 8;
  272. c->decode_intra = zmbv_decode_intra;
  273. c->decode_xor = zmbv_decode_xor_8;
  274. } else {
  275. c->bpp = 24;
  276. c->decode_intra = zmbv_decode_intra;
  277. c->decode_xor = zmbv_decode_xor_24;
  278. }
  279. c->cur = av_realloc(c->cur, avctx->width * avctx->height * (c->bpp / 8));
  280. c->prev = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8));
  281. c->bx = (c->width + c->bw - 1) / c->bw;
  282. c->by = (c->height+ c->bh - 1) / c->bh;
  283. }
  284. if(c->fmt == 0) {
  285. av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
  286. return -1;
  287. }
  288. if(c->comp == 0) { //Uncompressed data
  289. memcpy(c->decomp_buf, buf, len);
  290. c->decomp_size = 1;
  291. } else { // ZLIB-compressed data
  292. #ifdef CONFIG_ZLIB
  293. c->zstream.total_in = c->zstream.total_out = 0;
  294. c->zstream.next_in = buf;
  295. c->zstream.avail_in = len;
  296. c->zstream.next_out = c->decomp_buf;
  297. c->zstream.avail_out = c->decomp_size;
  298. inflate(&c->zstream, Z_FINISH);
  299. c->decomp_len = c->zstream.total_out;
  300. #else
  301. av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
  302. return -1;
  303. #endif
  304. }
  305. if(c->flags & ZMBV_KEYFRAME) {
  306. c->pic.key_frame = 1;
  307. c->pic.pict_type = FF_I_TYPE;
  308. c->decode_intra(c);
  309. } else {
  310. c->pic.key_frame = 0;
  311. c->pic.pict_type = FF_P_TYPE;
  312. c->decode_xor(c);
  313. }
  314. /* update frames */
  315. {
  316. uint8_t *out, *src;
  317. int i, j;
  318. out = c->pic.data[0];
  319. src = c->cur;
  320. for(j = 0; j < c->height; j++) {
  321. for(i = 0; i < c->width; i++) {
  322. out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
  323. out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
  324. out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
  325. *src++;
  326. }
  327. out += c->pic.linesize[0];
  328. }
  329. memcpy(c->prev, c->cur, c->width * c->height);
  330. }
  331. *data_size = sizeof(AVFrame);
  332. *(AVFrame*)data = c->pic;
  333. /* always report that the buffer was completely consumed */
  334. return buf_size;
  335. }
  336. /*
  337. *
  338. * Init zmbv decoder
  339. *
  340. */
  341. static int decode_init(AVCodecContext *avctx)
  342. {
  343. ZmbvContext * const c = (ZmbvContext *)avctx->priv_data;
  344. int zret; // Zlib return code
  345. c->avctx = avctx;
  346. avctx->has_b_frames = 0;
  347. c->pic.data[0] = NULL;
  348. c->width = avctx->width;
  349. c->height = avctx->height;
  350. if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
  351. return 1;
  352. }
  353. c->bpp = avctx->bits_per_sample;
  354. #ifdef CONFIG_ZLIB
  355. // Needed if zlib unused or init aborted before inflateInit
  356. memset(&(c->zstream), 0, sizeof(z_stream));
  357. #else
  358. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
  359. return 1;
  360. #endif
  361. avctx->pix_fmt = PIX_FMT_RGB24;
  362. c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
  363. /* Allocate decompression buffer */
  364. if (c->decomp_size) {
  365. if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
  366. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  367. return 1;
  368. }
  369. }
  370. #ifdef CONFIG_ZLIB
  371. c->zstream.zalloc = Z_NULL;
  372. c->zstream.zfree = Z_NULL;
  373. c->zstream.opaque = Z_NULL;
  374. zret = inflateInit(&(c->zstream));
  375. if (zret != Z_OK) {
  376. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  377. return 1;
  378. }
  379. #endif
  380. return 0;
  381. }
  382. /*
  383. *
  384. * Uninit zmbv decoder
  385. *
  386. */
  387. static int decode_end(AVCodecContext *avctx)
  388. {
  389. ZmbvContext * const c = (ZmbvContext *)avctx->priv_data;
  390. av_freep(&c->decomp_buf);
  391. if (c->pic.data[0])
  392. avctx->release_buffer(avctx, &c->pic);
  393. #ifdef CONFIG_ZLIB
  394. inflateEnd(&(c->zstream));
  395. #endif
  396. if(c->cur)
  397. av_freep(&c->cur);
  398. if(c->prev)
  399. av_freep(&c->prev);
  400. return 0;
  401. }
  402. AVCodec zmbv_decoder = {
  403. "zmbv",
  404. CODEC_TYPE_VIDEO,
  405. CODEC_ID_ZMBV,
  406. sizeof(ZmbvContext),
  407. decode_init,
  408. NULL,
  409. decode_end,
  410. decode_frame
  411. };