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.

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