kmvc.c 14 KB

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