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.

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