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.

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