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.

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