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.

467 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. if (ctx->pic.data[0])
  268. avctx->release_buffer(avctx, &ctx->pic);
  269. ctx->pic.reference = 1;
  270. ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  271. if (avctx->get_buffer(avctx, &ctx->pic) < 0) {
  272. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  273. return -1;
  274. }
  275. header = *buf++;
  276. /* blocksize 127 is really palette change event */
  277. if (buf[0] == 127) {
  278. buf += 3;
  279. for (i = 0; i < 127; i++) {
  280. ctx->pal[i + (header & 0x81)] = AV_RB24(buf);
  281. buf += 4;
  282. }
  283. buf -= 127 * 4 + 3;
  284. }
  285. if (header & KMVC_KEYFRAME) {
  286. ctx->pic.key_frame = 1;
  287. ctx->pic.pict_type = AV_PICTURE_TYPE_I;
  288. } else {
  289. ctx->pic.key_frame = 0;
  290. ctx->pic.pict_type = AV_PICTURE_TYPE_P;
  291. }
  292. /* if palette has been changed, copy it from palctrl */
  293. if (ctx->avctx->palctrl && ctx->avctx->palctrl->palette_changed) {
  294. memcpy(ctx->pal, ctx->avctx->palctrl->palette, AVPALETTE_SIZE);
  295. ctx->setpal = 1;
  296. ctx->avctx->palctrl->palette_changed = 0;
  297. }
  298. if (header & KMVC_PALETTE) {
  299. ctx->pic.palette_has_changed = 1;
  300. // palette starts from index 1 and has 127 entries
  301. for (i = 1; i <= ctx->palsize; i++) {
  302. ctx->pal[i] = bytestream_get_be24(&buf);
  303. }
  304. }
  305. if (ctx->setpal) {
  306. ctx->setpal = 0;
  307. ctx->pic.palette_has_changed = 1;
  308. }
  309. /* make the palette available on the way out */
  310. memcpy(ctx->pic.data[1], ctx->pal, 1024);
  311. blocksize = *buf++;
  312. if (blocksize != 8 && blocksize != 127) {
  313. av_log(avctx, AV_LOG_ERROR, "Block size = %i\n", blocksize);
  314. return -1;
  315. }
  316. memset(ctx->cur, 0, 320 * 200);
  317. switch (header & KMVC_METHOD) {
  318. case 0:
  319. case 1: // used in palette changed event
  320. memcpy(ctx->cur, ctx->prev, 320 * 200);
  321. break;
  322. case 3:
  323. kmvc_decode_intra_8x8(ctx, buf, buf_size, avctx->width, avctx->height);
  324. break;
  325. case 4:
  326. kmvc_decode_inter_8x8(ctx, buf, buf_size, avctx->width, avctx->height);
  327. break;
  328. default:
  329. av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD);
  330. return -1;
  331. }
  332. out = ctx->pic.data[0];
  333. src = ctx->cur;
  334. for (i = 0; i < avctx->height; i++) {
  335. memcpy(out, src, avctx->width);
  336. src += 320;
  337. out += ctx->pic.linesize[0];
  338. }
  339. /* flip buffers */
  340. if (ctx->cur == ctx->frm0) {
  341. ctx->cur = ctx->frm1;
  342. ctx->prev = ctx->frm0;
  343. } else {
  344. ctx->cur = ctx->frm0;
  345. ctx->prev = ctx->frm1;
  346. }
  347. *data_size = sizeof(AVFrame);
  348. *(AVFrame *) data = ctx->pic;
  349. /* always report that the buffer was completely consumed */
  350. return buf_size;
  351. }
  352. /*
  353. * Init kmvc decoder
  354. */
  355. static av_cold int decode_init(AVCodecContext * avctx)
  356. {
  357. KmvcContext *const c = avctx->priv_data;
  358. int i;
  359. c->avctx = avctx;
  360. if (avctx->width > 320 || avctx->height > 200) {
  361. av_log(avctx, AV_LOG_ERROR, "KMVC supports frames <= 320x200\n");
  362. return -1;
  363. }
  364. c->frm0 = av_mallocz(320 * 200);
  365. c->frm1 = av_mallocz(320 * 200);
  366. c->cur = c->frm0;
  367. c->prev = c->frm1;
  368. for (i = 0; i < 256; i++) {
  369. c->pal[i] = i * 0x10101;
  370. }
  371. if (avctx->extradata_size < 12) {
  372. av_log(NULL, 0, "Extradata missing, decoding may not work properly...\n");
  373. c->palsize = 127;
  374. } else {
  375. c->palsize = AV_RL16(avctx->extradata + 10);
  376. }
  377. if (avctx->extradata_size == 1036) { // palette in extradata
  378. uint8_t *src = avctx->extradata + 12;
  379. for (i = 0; i < 256; i++) {
  380. c->pal[i] = AV_RL32(src);
  381. src += 4;
  382. }
  383. c->setpal = 1;
  384. if (c->avctx->palctrl) {
  385. c->avctx->palctrl->palette_changed = 0;
  386. }
  387. }
  388. avcodec_get_frame_defaults(&c->pic);
  389. avctx->pix_fmt = PIX_FMT_PAL8;
  390. return 0;
  391. }
  392. /*
  393. * Uninit kmvc decoder
  394. */
  395. static av_cold int decode_end(AVCodecContext * avctx)
  396. {
  397. KmvcContext *const c = avctx->priv_data;
  398. av_freep(&c->frm0);
  399. av_freep(&c->frm1);
  400. if (c->pic.data[0])
  401. avctx->release_buffer(avctx, &c->pic);
  402. return 0;
  403. }
  404. AVCodec ff_kmvc_decoder = {
  405. "kmvc",
  406. AVMEDIA_TYPE_VIDEO,
  407. CODEC_ID_KMVC,
  408. sizeof(KmvcContext),
  409. decode_init,
  410. NULL,
  411. decode_end,
  412. decode_frame,
  413. CODEC_CAP_DR1,
  414. .long_name = NULL_IF_CONFIG_SMALL("Karl Morton's video codec"),
  415. };