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.

418 lines
14KB

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