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.

294 lines
8.9KB

  1. /*
  2. * Discworld II BMV video decoder
  3. * Copyright (c) 2011 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. #include "libavutil/common.h"
  22. #include "avcodec.h"
  23. #include "bytestream.h"
  24. #include "internal.h"
  25. enum BMVFlags{
  26. BMV_NOP = 0,
  27. BMV_END,
  28. BMV_DELTA,
  29. BMV_INTRA,
  30. BMV_SCROLL = 0x04,
  31. BMV_PALETTE = 0x08,
  32. BMV_COMMAND = 0x10,
  33. BMV_AUDIO = 0x20,
  34. BMV_EXT = 0x40,
  35. BMV_PRINT = 0x80
  36. };
  37. #define SCREEN_WIDE 640
  38. #define SCREEN_HIGH 429
  39. typedef struct BMVDecContext {
  40. AVCodecContext *avctx;
  41. uint8_t *frame, frame_base[SCREEN_WIDE * (SCREEN_HIGH + 1)];
  42. uint32_t pal[256];
  43. const uint8_t *stream;
  44. } BMVDecContext;
  45. #define NEXT_BYTE(v) v = forward ? v + 1 : v - 1;
  46. static int decode_bmv_frame(const uint8_t *source, int src_len, uint8_t *frame, int frame_off)
  47. {
  48. unsigned val, saved_val = 0;
  49. int tmplen = src_len;
  50. const uint8_t *src, *source_end = source + src_len;
  51. uint8_t *frame_end = frame + SCREEN_WIDE * SCREEN_HIGH;
  52. uint8_t *dst, *dst_end;
  53. int len, mask;
  54. int forward = (frame_off <= -SCREEN_WIDE) || (frame_off >= 0);
  55. int read_two_nibbles, flag;
  56. int advance_mode;
  57. int mode = 0;
  58. int i;
  59. if (src_len <= 0)
  60. return AVERROR_INVALIDDATA;
  61. if (forward) {
  62. src = source;
  63. dst = frame;
  64. dst_end = frame_end;
  65. } else {
  66. src = source + src_len - 1;
  67. dst = frame_end - 1;
  68. dst_end = frame - 1;
  69. }
  70. for (;;) {
  71. int shift = 0;
  72. flag = 0;
  73. /* The mode/len decoding is a bit strange:
  74. * values are coded as variable-length codes with nibble units,
  75. * code end is signalled by two top bits in the nibble being nonzero.
  76. * And since data is bytepacked and we read two nibbles at a time,
  77. * we may get a nibble belonging to the next code.
  78. * Hence this convoluted loop.
  79. */
  80. if (!mode || (tmplen == 4)) {
  81. if (src < source || src >= source_end)
  82. return AVERROR_INVALIDDATA;
  83. val = *src;
  84. read_two_nibbles = 1;
  85. } else {
  86. val = saved_val;
  87. read_two_nibbles = 0;
  88. }
  89. if (!(val & 0xC)) {
  90. for (;;) {
  91. if (!read_two_nibbles) {
  92. if (src < source || src >= source_end)
  93. return AVERROR_INVALIDDATA;
  94. shift += 2;
  95. val |= *src << shift;
  96. if (*src & 0xC)
  97. break;
  98. }
  99. // two upper bits of the nibble is zero,
  100. // so shift top nibble value down into their place
  101. read_two_nibbles = 0;
  102. shift += 2;
  103. mask = (1 << shift) - 1;
  104. val = ((val >> 2) & ~mask) | (val & mask);
  105. NEXT_BYTE(src);
  106. if ((val & (0xC << shift))) {
  107. flag = 1;
  108. break;
  109. }
  110. }
  111. } else if (mode) {
  112. flag = tmplen != 4;
  113. }
  114. if (flag) {
  115. tmplen = 4;
  116. } else {
  117. saved_val = val >> (4 + shift);
  118. tmplen = 0;
  119. val &= (1 << (shift + 4)) - 1;
  120. NEXT_BYTE(src);
  121. }
  122. advance_mode = val & 1;
  123. len = (val >> 1) - 1;
  124. mode += 1 + advance_mode;
  125. if (mode >= 4)
  126. mode -= 3;
  127. if (len <= 0 || FFABS(dst_end - dst) < len)
  128. return AVERROR_INVALIDDATA;
  129. switch (mode) {
  130. case 1:
  131. if (forward) {
  132. if (dst - frame + SCREEN_WIDE < frame_off ||
  133. dst - frame + SCREEN_WIDE + frame_off < 0 ||
  134. frame_end - dst < frame_off + len ||
  135. frame_end - dst < len)
  136. return AVERROR_INVALIDDATA;
  137. for (i = 0; i < len; i++)
  138. dst[i] = dst[frame_off + i];
  139. dst += len;
  140. } else {
  141. dst -= len;
  142. if (dst - frame + SCREEN_WIDE < frame_off ||
  143. dst - frame + SCREEN_WIDE + frame_off < 0 ||
  144. frame_end - dst < frame_off + len ||
  145. frame_end - dst < len)
  146. return AVERROR_INVALIDDATA;
  147. for (i = len - 1; i >= 0; i--)
  148. dst[i] = dst[frame_off + i];
  149. }
  150. break;
  151. case 2:
  152. if (forward) {
  153. if (source + src_len - src < len)
  154. return AVERROR_INVALIDDATA;
  155. memcpy(dst, src, len);
  156. dst += len;
  157. src += len;
  158. } else {
  159. if (src - source < len)
  160. return AVERROR_INVALIDDATA;
  161. dst -= len;
  162. src -= len;
  163. memcpy(dst, src, len);
  164. }
  165. break;
  166. case 3:
  167. val = forward ? dst[-1] : dst[1];
  168. if (forward) {
  169. memset(dst, val, len);
  170. dst += len;
  171. } else {
  172. dst -= len;
  173. memset(dst, val, len);
  174. }
  175. break;
  176. default:
  177. break;
  178. }
  179. if (dst == dst_end)
  180. return 0;
  181. }
  182. }
  183. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  184. AVPacket *pkt)
  185. {
  186. BMVDecContext * const c = avctx->priv_data;
  187. AVFrame *frame = data;
  188. int type, scr_off;
  189. int i, ret;
  190. uint8_t *srcptr, *outptr;
  191. c->stream = pkt->data;
  192. type = bytestream_get_byte(&c->stream);
  193. if (type & BMV_AUDIO) {
  194. int blobs = bytestream_get_byte(&c->stream);
  195. if (pkt->size < blobs * 65 + 2) {
  196. av_log(avctx, AV_LOG_ERROR, "Audio data doesn't fit in frame\n");
  197. return AVERROR_INVALIDDATA;
  198. }
  199. c->stream += blobs * 65;
  200. }
  201. if (type & BMV_COMMAND) {
  202. int command_size = (type & BMV_PRINT) ? 8 : 10;
  203. if (c->stream - pkt->data + command_size > pkt->size) {
  204. av_log(avctx, AV_LOG_ERROR, "Command data doesn't fit in frame\n");
  205. return AVERROR_INVALIDDATA;
  206. }
  207. c->stream += command_size;
  208. }
  209. if (type & BMV_PALETTE) {
  210. if (c->stream - pkt->data > pkt->size - 768) {
  211. av_log(avctx, AV_LOG_ERROR, "Palette data doesn't fit in frame\n");
  212. return AVERROR_INVALIDDATA;
  213. }
  214. for (i = 0; i < 256; i++)
  215. c->pal[i] = bytestream_get_be24(&c->stream);
  216. }
  217. if (type & BMV_SCROLL) {
  218. if (c->stream - pkt->data > pkt->size - 2) {
  219. av_log(avctx, AV_LOG_ERROR, "Screen offset data doesn't fit in frame\n");
  220. return AVERROR_INVALIDDATA;
  221. }
  222. scr_off = (int16_t)bytestream_get_le16(&c->stream);
  223. } else if ((type & BMV_INTRA) == BMV_INTRA) {
  224. scr_off = -640;
  225. } else {
  226. scr_off = 0;
  227. }
  228. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  229. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  230. return ret;
  231. }
  232. if (decode_bmv_frame(c->stream, pkt->size - (c->stream - pkt->data), c->frame, scr_off)) {
  233. av_log(avctx, AV_LOG_ERROR, "Error decoding frame data\n");
  234. return AVERROR_INVALIDDATA;
  235. }
  236. memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
  237. frame->palette_has_changed = type & BMV_PALETTE;
  238. outptr = frame->data[0];
  239. srcptr = c->frame;
  240. for (i = 0; i < avctx->height; i++) {
  241. memcpy(outptr, srcptr, avctx->width);
  242. srcptr += avctx->width;
  243. outptr += frame->linesize[0];
  244. }
  245. *got_frame = 1;
  246. /* always report that the buffer was completely consumed */
  247. return pkt->size;
  248. }
  249. static av_cold int decode_init(AVCodecContext *avctx)
  250. {
  251. BMVDecContext * const c = avctx->priv_data;
  252. c->avctx = avctx;
  253. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  254. c->frame = c->frame_base + 640;
  255. return 0;
  256. }
  257. AVCodec ff_bmv_video_decoder = {
  258. .name = "bmv_video",
  259. .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV video"),
  260. .type = AVMEDIA_TYPE_VIDEO,
  261. .id = AV_CODEC_ID_BMV_VIDEO,
  262. .priv_data_size = sizeof(BMVDecContext),
  263. .init = decode_init,
  264. .decode = decode_frame,
  265. .capabilities = AV_CODEC_CAP_DR1,
  266. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  267. };