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.

385 lines
11KB

  1. /*
  2. * Discworld II BMV video and audio 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 "avcodec.h"
  22. #include "bytestream.h"
  23. enum BMVFlags{
  24. BMV_NOP = 0,
  25. BMV_END,
  26. BMV_DELTA,
  27. BMV_INTRA,
  28. BMV_SCROLL = 0x04,
  29. BMV_PALETTE = 0x08,
  30. BMV_COMMAND = 0x10,
  31. BMV_AUDIO = 0x20,
  32. BMV_EXT = 0x40,
  33. BMV_PRINT = 0x80
  34. };
  35. #define SCREEN_WIDE 640
  36. #define SCREEN_HIGH 429
  37. typedef struct BMVDecContext {
  38. AVCodecContext *avctx;
  39. AVFrame pic;
  40. uint8_t *frame, frame_base[SCREEN_WIDE * (SCREEN_HIGH + 1)];
  41. uint32_t pal[256];
  42. const uint8_t *stream;
  43. } BMVDecContext;
  44. #define NEXT_BYTE(v) v = forward ? v + 1 : v - 1;
  45. static int decode_bmv_frame(const uint8_t *source, int src_len, uint8_t *frame, int frame_off)
  46. {
  47. unsigned val, saved_val = 0;
  48. int tmplen = src_len;
  49. const uint8_t *src, *source_end = source + src_len;
  50. uint8_t *frame_end = frame + SCREEN_WIDE * SCREEN_HIGH;
  51. uint8_t *dst, *dst_end;
  52. int len, mask;
  53. int forward = (frame_off <= -SCREEN_WIDE) || (frame_off >= 0);
  54. int read_two_nibbles, flag;
  55. int advance_mode;
  56. int mode = 0;
  57. int i;
  58. if (src_len <= 0)
  59. return -1;
  60. if (forward) {
  61. src = source;
  62. dst = frame;
  63. dst_end = frame_end;
  64. } else {
  65. src = source + src_len - 1;
  66. dst = frame_end - 1;
  67. dst_end = frame - 1;
  68. }
  69. for (;;) {
  70. int shift = 0;
  71. flag = 0;
  72. /* The mode/len decoding is a bit strange:
  73. * values are coded as variable-length codes with nibble units,
  74. * code end is signalled by two top bits in the nibble being nonzero.
  75. * And since data is bytepacked and we read two nibbles at a time,
  76. * we may get a nibble belonging to the next code.
  77. * Hence this convoluted loop.
  78. */
  79. if (!mode || (tmplen == 4)) {
  80. if (src < source || src >= source_end)
  81. return -1;
  82. val = *src;
  83. read_two_nibbles = 1;
  84. } else {
  85. val = saved_val;
  86. read_two_nibbles = 0;
  87. }
  88. if (!(val & 0xC)) {
  89. for (;;) {
  90. if (!read_two_nibbles) {
  91. if (src < source || src >= source_end)
  92. return -1;
  93. shift += 2;
  94. val |= *src << shift;
  95. if (*src & 0xC)
  96. break;
  97. }
  98. // two upper bits of the nibble is zero,
  99. // so shift top nibble value down into their place
  100. read_two_nibbles = 0;
  101. shift += 2;
  102. mask = (1 << shift) - 1;
  103. val = ((val >> 2) & ~mask) | (val & mask);
  104. NEXT_BYTE(src);
  105. if ((val & (0xC << shift))) {
  106. flag = 1;
  107. break;
  108. }
  109. }
  110. } else if (mode) {
  111. flag = tmplen != 4;
  112. }
  113. if (flag) {
  114. tmplen = 4;
  115. } else {
  116. saved_val = val >> (4 + shift);
  117. tmplen = 0;
  118. val &= (1 << (shift + 4)) - 1;
  119. NEXT_BYTE(src);
  120. }
  121. advance_mode = val & 1;
  122. len = (val >> 1) - 1;
  123. mode += 1 + advance_mode;
  124. if (mode >= 4)
  125. mode -= 3;
  126. if (FFABS(dst_end - dst) < len)
  127. return -1;
  128. switch (mode) {
  129. case 1:
  130. if (forward) {
  131. if (dst - frame + SCREEN_WIDE < frame_off ||
  132. dst - frame + SCREEN_WIDE + frame_off < 0 ||
  133. frame_end - dst < frame_off + len ||
  134. frame_end - dst < len)
  135. return -1;
  136. for (i = 0; i < len; i++)
  137. dst[i] = dst[frame_off + i];
  138. dst += len;
  139. } else {
  140. dst -= len;
  141. if (dst - frame + SCREEN_WIDE < frame_off ||
  142. dst - frame + SCREEN_WIDE + frame_off < 0 ||
  143. frame_end - dst < frame_off + len ||
  144. frame_end - dst < len)
  145. return -1;
  146. for (i = len - 1; i >= 0; i--)
  147. dst[i] = dst[frame_off + i];
  148. }
  149. break;
  150. case 2:
  151. if (forward) {
  152. if (source + src_len - src < len)
  153. return -1;
  154. memcpy(dst, src, len);
  155. dst += len;
  156. src += len;
  157. } else {
  158. if (src - source < len)
  159. return -1;
  160. dst -= len;
  161. src -= len;
  162. memcpy(dst, src, len);
  163. }
  164. break;
  165. case 3:
  166. val = forward ? dst[-1] : dst[1];
  167. if (forward) {
  168. memset(dst, val, len);
  169. dst += len;
  170. } else {
  171. dst -= len;
  172. memset(dst, val, len);
  173. }
  174. break;
  175. default:
  176. break;
  177. }
  178. if (dst == dst_end)
  179. return 0;
  180. }
  181. return 0;
  182. }
  183. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
  184. {
  185. BMVDecContext * const c = avctx->priv_data;
  186. int type, scr_off;
  187. int i, ret;
  188. uint8_t *srcptr, *outptr;
  189. c->stream = pkt->data;
  190. type = bytestream_get_byte(&c->stream);
  191. if (type & BMV_AUDIO) {
  192. int blobs = bytestream_get_byte(&c->stream);
  193. if (pkt->size < blobs * 65 + 2) {
  194. av_log(avctx, AV_LOG_ERROR, "Audio data doesn't fit in frame\n");
  195. return AVERROR_INVALIDDATA;
  196. }
  197. c->stream += blobs * 65;
  198. }
  199. if (type & BMV_COMMAND) {
  200. int command_size = (type & BMV_PRINT) ? 8 : 10;
  201. if (c->stream - pkt->data + command_size > pkt->size) {
  202. av_log(avctx, AV_LOG_ERROR, "Command data doesn't fit in frame\n");
  203. return AVERROR_INVALIDDATA;
  204. }
  205. c->stream += command_size;
  206. }
  207. if (type & BMV_PALETTE) {
  208. if (c->stream - pkt->data > pkt->size - 768) {
  209. av_log(avctx, AV_LOG_ERROR, "Palette data doesn't fit in frame\n");
  210. return AVERROR_INVALIDDATA;
  211. }
  212. for (i = 0; i < 256; i++)
  213. c->pal[i] = bytestream_get_be24(&c->stream);
  214. }
  215. if (type & BMV_SCROLL) {
  216. if (c->stream - pkt->data > pkt->size - 2) {
  217. av_log(avctx, AV_LOG_ERROR, "Screen offset data doesn't fit in frame\n");
  218. return AVERROR_INVALIDDATA;
  219. }
  220. scr_off = (int16_t)bytestream_get_le16(&c->stream);
  221. } else if ((type & BMV_INTRA) == BMV_INTRA) {
  222. scr_off = -640;
  223. } else {
  224. scr_off = 0;
  225. }
  226. if (c->pic.data[0])
  227. avctx->release_buffer(avctx, &c->pic);
  228. c->pic.reference = 3;
  229. if ((ret = avctx->get_buffer(avctx, &c->pic)) < 0) {
  230. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  231. return ret;
  232. }
  233. if (decode_bmv_frame(c->stream, pkt->size - (c->stream - pkt->data), c->frame, scr_off)) {
  234. av_log(avctx, AV_LOG_ERROR, "Error decoding frame data\n");
  235. return AVERROR_INVALIDDATA;
  236. }
  237. memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
  238. c->pic.palette_has_changed = type & BMV_PALETTE;
  239. outptr = c->pic.data[0];
  240. srcptr = c->frame;
  241. for (i = 0; i < avctx->height; i++) {
  242. memcpy(outptr, srcptr, avctx->width);
  243. srcptr += avctx->width;
  244. outptr += c->pic.linesize[0];
  245. }
  246. *data_size = sizeof(AVFrame);
  247. *(AVFrame*)data = c->pic;
  248. /* always report that the buffer was completely consumed */
  249. return pkt->size;
  250. }
  251. static av_cold int decode_init(AVCodecContext *avctx)
  252. {
  253. BMVDecContext * const c = avctx->priv_data;
  254. c->avctx = avctx;
  255. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  256. c->frame = c->frame_base + 640;
  257. return 0;
  258. }
  259. static av_cold int decode_end(AVCodecContext *avctx)
  260. {
  261. BMVDecContext *c = avctx->priv_data;
  262. if (c->pic.data[0])
  263. avctx->release_buffer(avctx, &c->pic);
  264. return 0;
  265. }
  266. typedef struct BMVAudioDecContext {
  267. AVFrame frame;
  268. } BMVAudioDecContext;
  269. static const int bmv_aud_mults[16] = {
  270. 16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32
  271. };
  272. static av_cold int bmv_aud_decode_init(AVCodecContext *avctx)
  273. {
  274. BMVAudioDecContext *c = avctx->priv_data;
  275. if (avctx->channels != 2) {
  276. av_log(avctx, AV_LOG_INFO, "invalid number of channels\n");
  277. return AVERROR(EINVAL);
  278. }
  279. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  280. avcodec_get_frame_defaults(&c->frame);
  281. avctx->coded_frame = &c->frame;
  282. return 0;
  283. }
  284. static int bmv_aud_decode_frame(AVCodecContext *avctx, void *data,
  285. int *got_frame_ptr, AVPacket *avpkt)
  286. {
  287. BMVAudioDecContext *c = avctx->priv_data;
  288. const uint8_t *buf = avpkt->data;
  289. int buf_size = avpkt->size;
  290. int blocks = 0, total_blocks, i;
  291. int ret;
  292. int16_t *output_samples;
  293. int scale[2];
  294. total_blocks = *buf++;
  295. if (buf_size < total_blocks * 65 + 1) {
  296. av_log(avctx, AV_LOG_ERROR, "expected %d bytes, got %d\n",
  297. total_blocks * 65 + 1, buf_size);
  298. return AVERROR_INVALIDDATA;
  299. }
  300. /* get output buffer */
  301. c->frame.nb_samples = total_blocks * 32;
  302. if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
  303. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  304. return ret;
  305. }
  306. output_samples = (int16_t *)c->frame.data[0];
  307. for (blocks = 0; blocks < total_blocks; blocks++) {
  308. uint8_t code = *buf++;
  309. code = (code >> 1) | (code << 7);
  310. scale[0] = bmv_aud_mults[code & 0xF];
  311. scale[1] = bmv_aud_mults[code >> 4];
  312. for (i = 0; i < 32; i++) {
  313. *output_samples++ = av_clip_int16((scale[0] * (int8_t)*buf++) >> 5);
  314. *output_samples++ = av_clip_int16((scale[1] * (int8_t)*buf++) >> 5);
  315. }
  316. }
  317. *got_frame_ptr = 1;
  318. *(AVFrame *)data = c->frame;
  319. return buf_size;
  320. }
  321. AVCodec ff_bmv_video_decoder = {
  322. .name = "bmv_video",
  323. .type = AVMEDIA_TYPE_VIDEO,
  324. .id = AV_CODEC_ID_BMV_VIDEO,
  325. .priv_data_size = sizeof(BMVDecContext),
  326. .init = decode_init,
  327. .close = decode_end,
  328. .decode = decode_frame,
  329. .capabilities = CODEC_CAP_DR1,
  330. .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV video"),
  331. };
  332. AVCodec ff_bmv_audio_decoder = {
  333. .name = "bmv_audio",
  334. .type = AVMEDIA_TYPE_AUDIO,
  335. .id = AV_CODEC_ID_BMV_AUDIO,
  336. .priv_data_size = sizeof(BMVAudioDecContext),
  337. .init = bmv_aud_decode_init,
  338. .decode = bmv_aud_decode_frame,
  339. .capabilities = CODEC_CAP_DR1,
  340. .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV audio"),
  341. };