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.

411 lines
15KB

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