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