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.

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