zmbv.c 19 KB

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