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.

426 lines
16KB

  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
  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. if ((l0x-mx) + 320*(l0y-my) < 0 || (l0x-mx) + 320*(l0y-my) > 320*197 - 4) {
  98. av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
  99. return AVERROR_INVALIDDATA;
  100. }
  101. for (j = 0; j < 16; j++)
  102. BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
  103. BLK(ctx->cur, l0x + (j & 3) - mx, l0y + (j >> 2) - my);
  104. }
  105. } else { // descend to 2x2 sub-sub-blocks
  106. for (j = 0; j < 4; j++) {
  107. l1x = l0x + (j & 1) * 2;
  108. l1y = l0y + (j & 2);
  109. kmvc_getbit(bb, &ctx->g, res);
  110. if (!res) {
  111. kmvc_getbit(bb, &ctx->g, res);
  112. if (!res) { // fill whole 2x2 block
  113. val = bytestream2_get_byte(&ctx->g);
  114. BLK(ctx->cur, l1x, l1y) = val;
  115. BLK(ctx->cur, l1x + 1, l1y) = val;
  116. BLK(ctx->cur, l1x, l1y + 1) = val;
  117. BLK(ctx->cur, l1x + 1, l1y + 1) = val;
  118. } else { // copy block from already decoded place
  119. val = bytestream2_get_byte(&ctx->g);
  120. mx = val & 0xF;
  121. my = val >> 4;
  122. if ((l1x-mx) + 320*(l1y-my) < 0 || (l1x-mx) + 320*(l1y-my) > 320*199 - 2) {
  123. av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
  124. return AVERROR_INVALIDDATA;
  125. }
  126. BLK(ctx->cur, l1x, l1y) = BLK(ctx->cur, l1x - mx, l1y - my);
  127. BLK(ctx->cur, l1x + 1, l1y) =
  128. BLK(ctx->cur, l1x + 1 - mx, l1y - my);
  129. BLK(ctx->cur, l1x, l1y + 1) =
  130. BLK(ctx->cur, l1x - mx, l1y + 1 - my);
  131. BLK(ctx->cur, l1x + 1, l1y + 1) =
  132. BLK(ctx->cur, l1x + 1 - mx, l1y + 1 - my);
  133. }
  134. } else { // read values for block
  135. BLK(ctx->cur, l1x, l1y) = bytestream2_get_byte(&ctx->g);
  136. BLK(ctx->cur, l1x + 1, l1y) = bytestream2_get_byte(&ctx->g);
  137. BLK(ctx->cur, l1x, l1y + 1) = bytestream2_get_byte(&ctx->g);
  138. BLK(ctx->cur, l1x + 1, l1y + 1) = bytestream2_get_byte(&ctx->g);
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
  145. return 0;
  146. }
  147. static int kmvc_decode_inter_8x8(KmvcContext * ctx, int w, int h)
  148. {
  149. BitBuf bb;
  150. int res, val;
  151. int i, j;
  152. int bx, by;
  153. int l0x, l1x, l0y, l1y;
  154. int mx, my;
  155. kmvc_init_getbits(bb, &ctx->g);
  156. for (by = 0; by < h; by += 8)
  157. for (bx = 0; bx < w; bx += 8) {
  158. kmvc_getbit(bb, &ctx->g, res);
  159. if (!res) {
  160. kmvc_getbit(bb, &ctx->g, res);
  161. if (!res) { // fill whole 8x8 block
  162. if (!bytestream2_get_bytes_left(&ctx->g)) {
  163. av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
  164. return AVERROR_INVALIDDATA;
  165. }
  166. val = bytestream2_get_byte(&ctx->g);
  167. for (i = 0; i < 64; i++)
  168. BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
  169. } else { // copy block from previous frame
  170. for (i = 0; i < 64; i++)
  171. BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) =
  172. BLK(ctx->prev, bx + (i & 0x7), by + (i >> 3));
  173. }
  174. } else { // handle four 4x4 subblocks
  175. if (!bytestream2_get_bytes_left(&ctx->g)) {
  176. av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
  177. return AVERROR_INVALIDDATA;
  178. }
  179. for (i = 0; i < 4; i++) {
  180. l0x = bx + (i & 1) * 4;
  181. l0y = by + (i & 2) * 2;
  182. kmvc_getbit(bb, &ctx->g, res);
  183. if (!res) {
  184. kmvc_getbit(bb, &ctx->g, res);
  185. if (!res) { // fill whole 4x4 block
  186. val = bytestream2_get_byte(&ctx->g);
  187. for (j = 0; j < 16; j++)
  188. BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
  189. } else { // copy block
  190. val = bytestream2_get_byte(&ctx->g);
  191. mx = (val & 0xF) - 8;
  192. my = (val >> 4) - 8;
  193. if ((l0x+mx) + 320*(l0y+my) < 0 || (l0x+mx) + 320*(l0y+my) > 320*197 - 4) {
  194. av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
  195. return AVERROR_INVALIDDATA;
  196. }
  197. for (j = 0; j < 16; j++)
  198. BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
  199. BLK(ctx->prev, l0x + (j & 3) + mx, l0y + (j >> 2) + my);
  200. }
  201. } else { // descend to 2x2 sub-sub-blocks
  202. for (j = 0; j < 4; j++) {
  203. l1x = l0x + (j & 1) * 2;
  204. l1y = l0y + (j & 2);
  205. kmvc_getbit(bb, &ctx->g, res);
  206. if (!res) {
  207. kmvc_getbit(bb, &ctx->g, res);
  208. if (!res) { // fill whole 2x2 block
  209. val = bytestream2_get_byte(&ctx->g);
  210. BLK(ctx->cur, l1x, l1y) = val;
  211. BLK(ctx->cur, l1x + 1, l1y) = val;
  212. BLK(ctx->cur, l1x, l1y + 1) = val;
  213. BLK(ctx->cur, l1x + 1, l1y + 1) = val;
  214. } else { // copy block
  215. val = bytestream2_get_byte(&ctx->g);
  216. mx = (val & 0xF) - 8;
  217. my = (val >> 4) - 8;
  218. if ((l1x+mx) + 320*(l1y+my) < 0 || (l1x+mx) + 320*(l1y+my) > 320*199 - 2) {
  219. av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
  220. return AVERROR_INVALIDDATA;
  221. }
  222. BLK(ctx->cur, l1x, l1y) = BLK(ctx->prev, l1x + mx, l1y + my);
  223. BLK(ctx->cur, l1x + 1, l1y) =
  224. BLK(ctx->prev, l1x + 1 + mx, l1y + my);
  225. BLK(ctx->cur, l1x, l1y + 1) =
  226. BLK(ctx->prev, l1x + mx, l1y + 1 + my);
  227. BLK(ctx->cur, l1x + 1, l1y + 1) =
  228. BLK(ctx->prev, l1x + 1 + mx, l1y + 1 + my);
  229. }
  230. } else { // read values for block
  231. BLK(ctx->cur, l1x, l1y) = bytestream2_get_byte(&ctx->g);
  232. BLK(ctx->cur, l1x + 1, l1y) = bytestream2_get_byte(&ctx->g);
  233. BLK(ctx->cur, l1x, l1y + 1) = bytestream2_get_byte(&ctx->g);
  234. BLK(ctx->cur, l1x + 1, l1y + 1) = bytestream2_get_byte(&ctx->g);
  235. }
  236. }
  237. }
  238. }
  239. }
  240. }
  241. return 0;
  242. }
  243. static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame,
  244. AVPacket *avpkt)
  245. {
  246. KmvcContext *const ctx = avctx->priv_data;
  247. AVFrame *frame = data;
  248. uint8_t *out, *src;
  249. int i, ret;
  250. int header;
  251. int blocksize;
  252. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  253. bytestream2_init(&ctx->g, avpkt->data, avpkt->size);
  254. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  255. return ret;
  256. header = bytestream2_get_byte(&ctx->g);
  257. /* blocksize 127 is really palette change event */
  258. if (bytestream2_peek_byte(&ctx->g) == 127) {
  259. bytestream2_skip(&ctx->g, 3);
  260. for (i = 0; i < 127; i++) {
  261. ctx->pal[i + (header & 0x81)] = 0xFFU << 24 | bytestream2_get_be24(&ctx->g);
  262. bytestream2_skip(&ctx->g, 1);
  263. }
  264. bytestream2_seek(&ctx->g, -127 * 4 - 3, SEEK_CUR);
  265. }
  266. if (header & KMVC_KEYFRAME) {
  267. frame->key_frame = 1;
  268. frame->pict_type = AV_PICTURE_TYPE_I;
  269. } else {
  270. frame->key_frame = 0;
  271. frame->pict_type = AV_PICTURE_TYPE_P;
  272. }
  273. if (header & KMVC_PALETTE) {
  274. frame->palette_has_changed = 1;
  275. // palette starts from index 1 and has 127 entries
  276. for (i = 1; i <= ctx->palsize; i++) {
  277. ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24(&ctx->g);
  278. }
  279. }
  280. if (pal) {
  281. frame->palette_has_changed = 1;
  282. memcpy(ctx->pal, pal, AVPALETTE_SIZE);
  283. }
  284. if (ctx->setpal) {
  285. ctx->setpal = 0;
  286. frame->palette_has_changed = 1;
  287. }
  288. /* make the palette available on the way out */
  289. memcpy(frame->data[1], ctx->pal, 1024);
  290. blocksize = bytestream2_get_byte(&ctx->g);
  291. if (blocksize != 8 && blocksize != 127) {
  292. av_log(avctx, AV_LOG_ERROR, "Block size = %i\n", blocksize);
  293. return AVERROR_INVALIDDATA;
  294. }
  295. memset(ctx->cur, 0, 320 * 200);
  296. switch (header & KMVC_METHOD) {
  297. case 0:
  298. case 1: // used in palette changed event
  299. memcpy(ctx->cur, ctx->prev, 320 * 200);
  300. break;
  301. case 3:
  302. kmvc_decode_intra_8x8(ctx, avctx->width, avctx->height);
  303. break;
  304. case 4:
  305. kmvc_decode_inter_8x8(ctx, avctx->width, avctx->height);
  306. break;
  307. default:
  308. av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD);
  309. return AVERROR_INVALIDDATA;
  310. }
  311. out = frame->data[0];
  312. src = ctx->cur;
  313. for (i = 0; i < avctx->height; i++) {
  314. memcpy(out, src, avctx->width);
  315. src += 320;
  316. out += frame->linesize[0];
  317. }
  318. /* flip buffers */
  319. if (ctx->cur == ctx->frm0) {
  320. ctx->cur = ctx->frm1;
  321. ctx->prev = ctx->frm0;
  322. } else {
  323. ctx->cur = ctx->frm0;
  324. ctx->prev = ctx->frm1;
  325. }
  326. *got_frame = 1;
  327. /* always report that the buffer was completely consumed */
  328. return avpkt->size;
  329. }
  330. /*
  331. * Init kmvc decoder
  332. */
  333. static av_cold int decode_init(AVCodecContext * avctx)
  334. {
  335. KmvcContext *const c = avctx->priv_data;
  336. int i;
  337. c->avctx = avctx;
  338. if (avctx->width > 320 || avctx->height > 200) {
  339. av_log(avctx, AV_LOG_ERROR, "KMVC supports frames <= 320x200\n");
  340. return AVERROR(EINVAL);
  341. }
  342. c->cur = c->frm0;
  343. c->prev = c->frm1;
  344. for (i = 0; i < 256; i++) {
  345. c->pal[i] = 0xFFU << 24 | i * 0x10101;
  346. }
  347. if (avctx->extradata_size < 12) {
  348. av_log(avctx, AV_LOG_WARNING,
  349. "Extradata missing, decoding may not work properly...\n");
  350. c->palsize = 127;
  351. } else {
  352. c->palsize = AV_RL16(avctx->extradata + 10);
  353. if (c->palsize >= (unsigned)MAX_PALSIZE) {
  354. c->palsize = 127;
  355. av_log(avctx, AV_LOG_ERROR, "KMVC palette too large\n");
  356. return AVERROR_INVALIDDATA;
  357. }
  358. }
  359. if (avctx->extradata_size == 1036) { // palette in extradata
  360. uint8_t *src = avctx->extradata + 12;
  361. for (i = 0; i < 256; i++) {
  362. c->pal[i] = AV_RL32(src);
  363. src += 4;
  364. }
  365. c->setpal = 1;
  366. }
  367. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  368. return 0;
  369. }
  370. AVCodec ff_kmvc_decoder = {
  371. .name = "kmvc",
  372. .long_name = NULL_IF_CONFIG_SMALL("Karl Morton's video codec"),
  373. .type = AVMEDIA_TYPE_VIDEO,
  374. .id = AV_CODEC_ID_KMVC,
  375. .priv_data_size = sizeof(KmvcContext),
  376. .init = decode_init,
  377. .decode = decode_frame,
  378. .capabilities = AV_CODEC_CAP_DR1,
  379. };