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.

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