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.

433 lines
15KB

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