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.

428 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. #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, *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 *got_frame,
  227. AVPacket *avpkt)
  228. {
  229. KmvcContext *const ctx = avctx->priv_data;
  230. AVFrame *frame = data;
  231. uint8_t *out, *src;
  232. int i, ret;
  233. int header;
  234. int blocksize;
  235. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  236. bytestream2_init(&ctx->g, avpkt->data, avpkt->size);
  237. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  238. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  239. return ret;
  240. }
  241. header = bytestream2_get_byte(&ctx->g);
  242. /* blocksize 127 is really palette change event */
  243. if (bytestream2_peek_byte(&ctx->g) == 127) {
  244. bytestream2_skip(&ctx->g, 3);
  245. for (i = 0; i < 127; i++) {
  246. ctx->pal[i + (header & 0x81)] = bytestream2_get_be24(&ctx->g);
  247. bytestream2_skip(&ctx->g, 1);
  248. }
  249. bytestream2_seek(&ctx->g, -127 * 4 - 3, SEEK_CUR);
  250. }
  251. if (header & KMVC_KEYFRAME) {
  252. frame->key_frame = 1;
  253. frame->pict_type = AV_PICTURE_TYPE_I;
  254. } else {
  255. frame->key_frame = 0;
  256. frame->pict_type = AV_PICTURE_TYPE_P;
  257. }
  258. if (header & KMVC_PALETTE) {
  259. frame->palette_has_changed = 1;
  260. // palette starts from index 1 and has 127 entries
  261. for (i = 1; i <= ctx->palsize; i++) {
  262. ctx->pal[i] = bytestream2_get_be24(&ctx->g);
  263. }
  264. }
  265. if (pal) {
  266. frame->palette_has_changed = 1;
  267. memcpy(ctx->pal, pal, AVPALETTE_SIZE);
  268. }
  269. if (ctx->setpal) {
  270. ctx->setpal = 0;
  271. frame->palette_has_changed = 1;
  272. }
  273. /* make the palette available on the way out */
  274. memcpy(frame->data[1], ctx->pal, 1024);
  275. blocksize = bytestream2_get_byte(&ctx->g);
  276. if (blocksize != 8 && blocksize != 127) {
  277. av_log(avctx, AV_LOG_ERROR, "Block size = %i\n", blocksize);
  278. return AVERROR_INVALIDDATA;
  279. }
  280. memset(ctx->cur, 0, 320 * 200);
  281. switch (header & KMVC_METHOD) {
  282. case 0:
  283. case 1: // used in palette changed event
  284. memcpy(ctx->cur, ctx->prev, 320 * 200);
  285. break;
  286. case 3:
  287. kmvc_decode_intra_8x8(ctx, avctx->width, avctx->height);
  288. break;
  289. case 4:
  290. kmvc_decode_inter_8x8(ctx, avctx->width, avctx->height);
  291. break;
  292. default:
  293. av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD);
  294. return AVERROR_INVALIDDATA;
  295. }
  296. out = frame->data[0];
  297. src = ctx->cur;
  298. for (i = 0; i < avctx->height; i++) {
  299. memcpy(out, src, avctx->width);
  300. src += 320;
  301. out += frame->linesize[0];
  302. }
  303. /* flip buffers */
  304. if (ctx->cur == ctx->frm0) {
  305. ctx->cur = ctx->frm1;
  306. ctx->prev = ctx->frm0;
  307. } else {
  308. ctx->cur = ctx->frm0;
  309. ctx->prev = ctx->frm1;
  310. }
  311. *got_frame = 1;
  312. /* always report that the buffer was completely consumed */
  313. return avpkt->size;
  314. }
  315. /*
  316. * Init kmvc decoder
  317. */
  318. static av_cold int decode_init(AVCodecContext * avctx)
  319. {
  320. KmvcContext *const c = avctx->priv_data;
  321. int i;
  322. c->avctx = avctx;
  323. if (avctx->width > 320 || avctx->height > 200) {
  324. av_log(avctx, AV_LOG_ERROR, "KMVC supports frames <= 320x200\n");
  325. return AVERROR(EINVAL);
  326. }
  327. c->frm0 = av_mallocz(320 * 200);
  328. c->frm1 = av_mallocz(320 * 200);
  329. c->cur = c->frm0;
  330. c->prev = c->frm1;
  331. for (i = 0; i < 256; i++) {
  332. c->pal[i] = i * 0x10101;
  333. }
  334. if (avctx->extradata_size < 12) {
  335. av_log(avctx, AV_LOG_WARNING,
  336. "Extradata missing, decoding may not work properly...\n");
  337. c->palsize = 127;
  338. } else {
  339. c->palsize = AV_RL16(avctx->extradata + 10);
  340. if (c->palsize >= MAX_PALSIZE) {
  341. av_log(avctx, AV_LOG_ERROR, "KMVC palette too large\n");
  342. return AVERROR_INVALIDDATA;
  343. }
  344. }
  345. if (avctx->extradata_size == 1036) { // palette in extradata
  346. uint8_t *src = avctx->extradata + 12;
  347. for (i = 0; i < 256; i++) {
  348. c->pal[i] = AV_RL32(src);
  349. src += 4;
  350. }
  351. c->setpal = 1;
  352. }
  353. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  354. return 0;
  355. }
  356. /*
  357. * Uninit kmvc decoder
  358. */
  359. static av_cold int decode_end(AVCodecContext * avctx)
  360. {
  361. KmvcContext *const c = avctx->priv_data;
  362. av_freep(&c->frm0);
  363. av_freep(&c->frm1);
  364. return 0;
  365. }
  366. AVCodec ff_kmvc_decoder = {
  367. .name = "kmvc",
  368. .type = AVMEDIA_TYPE_VIDEO,
  369. .id = AV_CODEC_ID_KMVC,
  370. .priv_data_size = sizeof(KmvcContext),
  371. .init = decode_init,
  372. .close = decode_end,
  373. .decode = decode_frame,
  374. .capabilities = CODEC_CAP_DR1,
  375. .long_name = NULL_IF_CONFIG_SMALL("Karl Morton's video codec"),
  376. };