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.

416 lines
14KB

  1. /*
  2. * KMVC 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 kmvc.c
  22. * Karl Morton's Video Codec decoder
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include "common.h"
  27. #include "avcodec.h"
  28. #define KMVC_KEYFRAME 0x80
  29. #define KMVC_PALETTE 0x40
  30. #define KMVC_METHOD 0x0F
  31. /*
  32. * Decoder context
  33. */
  34. typedef struct KmvcContext {
  35. AVCodecContext *avctx;
  36. AVFrame pic;
  37. int setpal;
  38. int palsize;
  39. uint32_t pal[256];
  40. uint8_t *cur, *prev;
  41. uint8_t *frm0, *frm1;
  42. } KmvcContext;
  43. typedef struct BitBuf {
  44. int bits;
  45. int bitbuf;
  46. } BitBuf;
  47. #define BLK(data, x, y) data[(x) + (y) * 320]
  48. #define kmvc_init_getbits(bb, src) bb.bits = 7; bb.bitbuf = *src++;
  49. #define kmvc_getbit(bb, src, res) {\
  50. res = 0; \
  51. if (bb.bitbuf & (1 << bb.bits)) res = 1; \
  52. bb.bits--; \
  53. if(bb.bits == -1) { \
  54. bb.bitbuf = *src++; \
  55. bb.bits = 7; \
  56. } \
  57. }
  58. static void kmvc_decode_intra_8x8(KmvcContext * ctx, uint8_t * src, int w, int h)
  59. {
  60. BitBuf bb;
  61. int res, val;
  62. int i, j;
  63. int bx, by;
  64. int l0x, l1x, l0y, l1y;
  65. int mx, my;
  66. kmvc_init_getbits(bb, src);
  67. for (by = 0; by < h; by += 8)
  68. for (bx = 0; bx < w; bx += 8) {
  69. kmvc_getbit(bb, src, res);
  70. if (!res) { // fill whole 8x8 block
  71. val = *src++;
  72. for (i = 0; i < 64; i++)
  73. BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
  74. } else { // handle four 4x4 subblocks
  75. for (i = 0; i < 4; i++) {
  76. l0x = bx + (i & 1) * 4;
  77. l0y = by + (i & 2) * 2;
  78. kmvc_getbit(bb, src, res);
  79. if (!res) {
  80. kmvc_getbit(bb, src, res);
  81. if (!res) { // fill whole 4x4 block
  82. val = *src++;
  83. for (j = 0; j < 16; j++)
  84. BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
  85. } else { // copy block from already decoded place
  86. val = *src++;
  87. mx = val & 0xF;
  88. my = val >> 4;
  89. for (j = 0; j < 16; j++)
  90. BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
  91. BLK(ctx->cur, l0x + (j & 3) - mx, l0y + (j >> 2) - my);
  92. }
  93. } else { // descend to 2x2 sub-sub-blocks
  94. for (j = 0; j < 4; j++) {
  95. l1x = l0x + (j & 1) * 2;
  96. l1y = l0y + (j & 2);
  97. kmvc_getbit(bb, src, res);
  98. if (!res) {
  99. kmvc_getbit(bb, src, res);
  100. if (!res) { // fill whole 2x2 block
  101. val = *src++;
  102. BLK(ctx->cur, l1x, l1y) = val;
  103. BLK(ctx->cur, l1x + 1, l1y) = val;
  104. BLK(ctx->cur, l1x, l1y + 1) = val;
  105. BLK(ctx->cur, l1x + 1, l1y + 1) = val;
  106. } else { // copy block from already decoded place
  107. val = *src++;
  108. mx = val & 0xF;
  109. my = val >> 4;
  110. BLK(ctx->cur, l1x, l1y) = BLK(ctx->cur, l1x - mx, l1y - my);
  111. BLK(ctx->cur, l1x + 1, l1y) =
  112. BLK(ctx->cur, l1x + 1 - mx, l1y - my);
  113. BLK(ctx->cur, l1x, l1y + 1) =
  114. BLK(ctx->cur, l1x - mx, l1y + 1 - my);
  115. BLK(ctx->cur, l1x + 1, l1y + 1) =
  116. BLK(ctx->cur, l1x + 1 - mx, l1y + 1 - my);
  117. }
  118. } else { // read values for block
  119. BLK(ctx->cur, l1x, l1y) = *src++;
  120. BLK(ctx->cur, l1x + 1, l1y) = *src++;
  121. BLK(ctx->cur, l1x, l1y + 1) = *src++;
  122. BLK(ctx->cur, l1x + 1, l1y + 1) = *src++;
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }
  130. static void kmvc_decode_inter_8x8(KmvcContext * ctx, uint8_t * src, int w, int h)
  131. {
  132. BitBuf bb;
  133. int res, val;
  134. int i, j;
  135. int bx, by;
  136. int l0x, l1x, l0y, l1y;
  137. int mx, my;
  138. kmvc_init_getbits(bb, src);
  139. for (by = 0; by < h; by += 8)
  140. for (bx = 0; bx < w; bx += 8) {
  141. kmvc_getbit(bb, src, res);
  142. if (!res) {
  143. kmvc_getbit(bb, src, res);
  144. if (!res) { // fill whole 8x8 block
  145. val = *src++;
  146. for (i = 0; i < 64; i++)
  147. BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
  148. } else { // copy block from previous frame
  149. for (i = 0; i < 64; i++)
  150. BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) =
  151. BLK(ctx->prev, bx + (i & 0x7), by + (i >> 3));
  152. }
  153. } else { // handle four 4x4 subblocks
  154. for (i = 0; i < 4; i++) {
  155. l0x = bx + (i & 1) * 4;
  156. l0y = by + (i & 2) * 2;
  157. kmvc_getbit(bb, src, res);
  158. if (!res) {
  159. kmvc_getbit(bb, src, res);
  160. if (!res) { // fill whole 4x4 block
  161. val = *src++;
  162. for (j = 0; j < 16; j++)
  163. BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
  164. } else { // copy block
  165. val = *src++;
  166. mx = (val & 0xF) - 8;
  167. my = (val >> 4) - 8;
  168. for (j = 0; j < 16; j++)
  169. BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
  170. BLK(ctx->prev, l0x + (j & 3) + mx, l0y + (j >> 2) + my);
  171. }
  172. } else { // descend to 2x2 sub-sub-blocks
  173. for (j = 0; j < 4; j++) {
  174. l1x = l0x + (j & 1) * 2;
  175. l1y = l0y + (j & 2);
  176. kmvc_getbit(bb, src, res);
  177. if (!res) {
  178. kmvc_getbit(bb, src, res);
  179. if (!res) { // fill whole 2x2 block
  180. val = *src++;
  181. BLK(ctx->cur, l1x, l1y) = val;
  182. BLK(ctx->cur, l1x + 1, l1y) = val;
  183. BLK(ctx->cur, l1x, l1y + 1) = val;
  184. BLK(ctx->cur, l1x + 1, l1y + 1) = val;
  185. } else { // copy block
  186. val = *src++;
  187. mx = (val & 0xF) - 8;
  188. my = (val >> 4) - 8;
  189. BLK(ctx->cur, l1x, l1y) = BLK(ctx->prev, l1x + mx, l1y + my);
  190. BLK(ctx->cur, l1x + 1, l1y) =
  191. BLK(ctx->prev, l1x + 1 + mx, l1y + my);
  192. BLK(ctx->cur, l1x, l1y + 1) =
  193. BLK(ctx->prev, l1x + mx, l1y + 1 + my);
  194. BLK(ctx->cur, l1x + 1, l1y + 1) =
  195. BLK(ctx->prev, l1x + 1 + mx, l1y + 1 + my);
  196. }
  197. } else { // read values for block
  198. BLK(ctx->cur, l1x, l1y) = *src++;
  199. BLK(ctx->cur, l1x + 1, l1y) = *src++;
  200. BLK(ctx->cur, l1x, l1y + 1) = *src++;
  201. BLK(ctx->cur, l1x + 1, l1y + 1) = *src++;
  202. }
  203. }
  204. }
  205. }
  206. }
  207. }
  208. }
  209. static int decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t * buf,
  210. int buf_size)
  211. {
  212. KmvcContext *const ctx = (KmvcContext *) avctx->priv_data;
  213. uint8_t *out, *src;
  214. int i;
  215. int header;
  216. int blocksize;
  217. if (ctx->pic.data[0])
  218. avctx->release_buffer(avctx, &ctx->pic);
  219. ctx->pic.reference = 1;
  220. ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  221. if (avctx->get_buffer(avctx, &ctx->pic) < 0) {
  222. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  223. return -1;
  224. }
  225. header = *buf++;
  226. /* blocksize 127 is really palette change event */
  227. if (buf[0] == 127) {
  228. buf += 3;
  229. for (i = 0; i < 127; i++) {
  230. ctx->pal[i + (header & 0x81)] = (buf[0] << 16) | (buf[1] << 8) | buf[2];
  231. buf += 4;
  232. }
  233. buf -= 127 * 4 + 3;
  234. }
  235. if (header & KMVC_KEYFRAME) {
  236. ctx->pic.key_frame = 1;
  237. ctx->pic.pict_type = FF_I_TYPE;
  238. } else {
  239. ctx->pic.key_frame = 0;
  240. ctx->pic.pict_type = FF_P_TYPE;
  241. }
  242. /* if palette has been changed, copy it from palctrl */
  243. if (ctx->avctx->palctrl && ctx->avctx->palctrl->palette_changed) {
  244. memcpy(ctx->pal, ctx->avctx->palctrl->palette, AVPALETTE_SIZE);
  245. ctx->setpal = 1;
  246. ctx->avctx->palctrl->palette_changed = 0;
  247. }
  248. if (header & KMVC_PALETTE) {
  249. ctx->pic.palette_has_changed = 1;
  250. // palette starts from index 1 and has 127 entries
  251. for (i = 1; i <= ctx->palsize; i++) {
  252. ctx->pal[i] = (buf[0] << 16) | (buf[1] << 8) | buf[2];
  253. buf += 3;
  254. }
  255. }
  256. if (ctx->setpal) {
  257. ctx->setpal = 0;
  258. ctx->pic.palette_has_changed = 1;
  259. }
  260. /* make the palette available on the way out */
  261. memcpy(ctx->pic.data[1], ctx->pal, 1024);
  262. blocksize = *buf++;
  263. if (blocksize != 8 && blocksize != 127) {
  264. av_log(avctx, AV_LOG_ERROR, "Block size = %i\n", blocksize);
  265. return -1;
  266. }
  267. memset(ctx->cur, 0, 320 * 200);
  268. switch (header & KMVC_METHOD) {
  269. case 0:
  270. case 1: // used in palette changed event
  271. memcpy(ctx->cur, ctx->prev, 320 * 200);
  272. break;
  273. case 3:
  274. kmvc_decode_intra_8x8(ctx, buf, avctx->width, avctx->height);
  275. break;
  276. case 4:
  277. kmvc_decode_inter_8x8(ctx, buf, avctx->width, avctx->height);
  278. break;
  279. default:
  280. av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD);
  281. return -1;
  282. }
  283. out = ctx->pic.data[0];
  284. src = ctx->cur;
  285. for (i = 0; i < avctx->height; i++) {
  286. memcpy(out, src, avctx->width);
  287. src += 320;
  288. out += ctx->pic.linesize[0];
  289. }
  290. /* flip buffers */
  291. if (ctx->cur == ctx->frm0) {
  292. ctx->cur = ctx->frm1;
  293. ctx->prev = ctx->frm0;
  294. } else {
  295. ctx->cur = ctx->frm0;
  296. ctx->prev = ctx->frm1;
  297. }
  298. *data_size = sizeof(AVFrame);
  299. *(AVFrame *) data = ctx->pic;
  300. /* always report that the buffer was completely consumed */
  301. return buf_size;
  302. }
  303. /*
  304. * Init kmvc decoder
  305. */
  306. static int decode_init(AVCodecContext * avctx)
  307. {
  308. KmvcContext *const c = (KmvcContext *) avctx->priv_data;
  309. int i;
  310. c->avctx = avctx;
  311. avctx->has_b_frames = 0;
  312. c->pic.data[0] = NULL;
  313. if (avctx->width > 320 || avctx->height > 200) {
  314. av_log(avctx, AV_LOG_ERROR, "KMVC supports frames <= 320x200\n");
  315. return -1;
  316. }
  317. c->frm0 = av_mallocz(320 * 200);
  318. c->frm1 = av_mallocz(320 * 200);
  319. c->cur = c->frm0;
  320. c->prev = c->frm1;
  321. for (i = 0; i < 256; i++) {
  322. c->pal[i] = i * 0x10101;
  323. }
  324. if (avctx->extradata_size < 12) {
  325. av_log(NULL, 0, "Extradata missing, decoding may not work properly...\n");
  326. c->palsize = 127;
  327. } else {
  328. c->palsize = LE_16(avctx->extradata + 10);
  329. }
  330. if (avctx->extradata_size == 1036) { // palette in extradata
  331. uint8_t *src = avctx->extradata + 12;
  332. for (i = 0; i < 256; i++) {
  333. c->pal[i] = LE_32(src);
  334. src += 4;
  335. }
  336. c->setpal = 1;
  337. if (c->avctx->palctrl) {
  338. c->avctx->palctrl->palette_changed = 0;
  339. }
  340. }
  341. avctx->pix_fmt = PIX_FMT_PAL8;
  342. return 0;
  343. }
  344. /*
  345. * Uninit kmvc decoder
  346. */
  347. static int decode_end(AVCodecContext * avctx)
  348. {
  349. KmvcContext *const c = (KmvcContext *) avctx->priv_data;
  350. av_freep(&c->frm0);
  351. av_freep(&c->frm1);
  352. if (c->pic.data[0])
  353. avctx->release_buffer(avctx, &c->pic);
  354. return 0;
  355. }
  356. AVCodec kmvc_decoder = {
  357. "kmvc",
  358. CODEC_TYPE_VIDEO,
  359. CODEC_ID_KMVC,
  360. sizeof(KmvcContext),
  361. decode_init,
  362. NULL,
  363. decode_end,
  364. decode_frame
  365. };