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.

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