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.

395 lines
13KB

  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[0];
  226. buf++;
  227. if (header & KMVC_KEYFRAME) {
  228. ctx->pic.key_frame = 1;
  229. ctx->pic.pict_type = FF_I_TYPE;
  230. } else {
  231. ctx->pic.key_frame = 0;
  232. ctx->pic.pict_type = FF_P_TYPE;
  233. }
  234. if (header & KMVC_PALETTE) {
  235. ctx->pic.palette_has_changed = 1;
  236. // palette starts from index 1 and has 127 entries
  237. for (i = 1; i <= ctx->palsize; i++) {
  238. ctx->pal[i] = (buf[0] << 16) | (buf[1] << 8) | buf[2];
  239. buf += 3;
  240. }
  241. }
  242. if (ctx->setpal) {
  243. ctx->setpal = 0;
  244. ctx->pic.palette_has_changed = 1;
  245. }
  246. /* make the palette available on the way out */
  247. memcpy(ctx->pic.data[1], ctx->pal, 1024);
  248. blocksize = *buf++;
  249. if (blocksize != 8) {
  250. av_log(avctx, AV_LOG_ERROR, "Block size = %i\n", blocksize);
  251. return -1;
  252. }
  253. memset(ctx->cur, 0, 320 * 200);
  254. switch (header & KMVC_METHOD) {
  255. case 3:
  256. kmvc_decode_intra_8x8(ctx, buf, avctx->width, avctx->height);
  257. break;
  258. case 4:
  259. kmvc_decode_inter_8x8(ctx, buf, avctx->width, avctx->height);
  260. break;
  261. default:
  262. av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD);
  263. return -1;
  264. }
  265. out = ctx->pic.data[0];
  266. src = ctx->cur;
  267. for (i = 0; i < avctx->height; i++) {
  268. memcpy(out, src, avctx->width);
  269. src += 320;
  270. out += ctx->pic.linesize[0];
  271. }
  272. /* flip buffers */
  273. if (ctx->cur == ctx->frm0) {
  274. ctx->cur = ctx->frm1;
  275. ctx->prev = ctx->frm0;
  276. } else {
  277. ctx->cur = ctx->frm0;
  278. ctx->prev = ctx->frm1;
  279. }
  280. *data_size = sizeof(AVFrame);
  281. *(AVFrame *) data = ctx->pic;
  282. /* always report that the buffer was completely consumed */
  283. return buf_size;
  284. }
  285. /*
  286. * Init kmvc decoder
  287. */
  288. static int decode_init(AVCodecContext * avctx)
  289. {
  290. KmvcContext *const c = (KmvcContext *) avctx->priv_data;
  291. int i;
  292. c->avctx = avctx;
  293. avctx->has_b_frames = 0;
  294. c->pic.data[0] = NULL;
  295. if (avctx->width > 320 || avctx->height > 200) {
  296. av_log(avctx, AV_LOG_ERROR, "KMVC supports frames <= 320x200\n");
  297. return -1;
  298. }
  299. c->frm0 = av_mallocz(320 * 200);
  300. c->frm1 = av_mallocz(320 * 200);
  301. c->cur = c->frm0;
  302. c->prev = c->frm1;
  303. for (i = 0; i < 256; i++) {
  304. c->pal[i] = i * 0x10101;
  305. }
  306. if (avctx->extradata_size < 12) {
  307. av_log(NULL, 0, "Extradata missing, decoding may not work properly...\n");
  308. c->palsize = 127;
  309. } else {
  310. c->palsize = LE_16(avctx->extradata + 10);
  311. }
  312. if (avctx->extradata_size == 1036) { // palette in extradata
  313. uint8_t *src = avctx->extradata + 12;
  314. for (i = 0; i < c->palsize; i++) {
  315. c->pal[i] = LE_32(src);
  316. src += 4;
  317. }
  318. c->setpal = 1;
  319. }
  320. avctx->pix_fmt = PIX_FMT_PAL8;
  321. return 0;
  322. }
  323. /*
  324. * Uninit kmvc decoder
  325. */
  326. static int decode_end(AVCodecContext * avctx)
  327. {
  328. KmvcContext *const c = (KmvcContext *) avctx->priv_data;
  329. if (c->frm0)
  330. av_free(c->frm0);
  331. if (c->frm1)
  332. av_free(c->frm1);
  333. if (c->pic.data[0])
  334. avctx->release_buffer(avctx, &c->pic);
  335. return 0;
  336. }
  337. AVCodec kmvc_decoder = {
  338. "kmvc",
  339. CODEC_TYPE_VIDEO,
  340. CODEC_ID_KMVC,
  341. sizeof(KmvcContext),
  342. decode_init,
  343. NULL,
  344. decode_end,
  345. decode_frame
  346. };