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.

377 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. int 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. frame_end - dst < frame_off + len)
  133. return -1;
  134. for (i = 0; i < len; i++)
  135. dst[i] = dst[frame_off + i];
  136. dst += len;
  137. } else {
  138. dst -= len;
  139. if (dst - frame + SCREEN_WIDE < frame_off ||
  140. frame_end - dst < frame_off + len)
  141. return -1;
  142. for (i = len - 1; i >= 0; i--)
  143. dst[i] = dst[frame_off + i];
  144. }
  145. break;
  146. case 2:
  147. if (forward) {
  148. if (source + src_len - src < len)
  149. return -1;
  150. memcpy(dst, src, len);
  151. dst += len;
  152. src += len;
  153. } else {
  154. if (src - source < len)
  155. return -1;
  156. dst -= len;
  157. src -= len;
  158. memcpy(dst, src, len);
  159. }
  160. break;
  161. case 3:
  162. val = forward ? dst[-1] : dst[1];
  163. if (forward) {
  164. memset(dst, val, len);
  165. dst += len;
  166. } else {
  167. dst -= len;
  168. memset(dst, val, len);
  169. }
  170. break;
  171. default:
  172. break;
  173. }
  174. if (dst == dst_end)
  175. return 0;
  176. }
  177. return 0;
  178. }
  179. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
  180. {
  181. BMVDecContext * const c = avctx->priv_data;
  182. int type, scr_off;
  183. int i;
  184. uint8_t *srcptr, *outptr;
  185. c->stream = pkt->data;
  186. type = bytestream_get_byte(&c->stream);
  187. if (type & BMV_AUDIO) {
  188. int blobs = bytestream_get_byte(&c->stream);
  189. if (pkt->size < blobs * 65 + 2) {
  190. av_log(avctx, AV_LOG_ERROR, "Audio data doesn't fit in frame\n");
  191. return AVERROR_INVALIDDATA;
  192. }
  193. c->stream += blobs * 65;
  194. }
  195. if (type & BMV_COMMAND) {
  196. int command_size = (type & BMV_PRINT) ? 8 : 10;
  197. if (c->stream - pkt->data + command_size > pkt->size) {
  198. av_log(avctx, AV_LOG_ERROR, "Command data doesn't fit in frame\n");
  199. return AVERROR_INVALIDDATA;
  200. }
  201. c->stream += command_size;
  202. }
  203. if (type & BMV_PALETTE) {
  204. if (c->stream - pkt->data > pkt->size - 768) {
  205. av_log(avctx, AV_LOG_ERROR, "Palette data doesn't fit in frame\n");
  206. return AVERROR_INVALIDDATA;
  207. }
  208. for (i = 0; i < 256; i++)
  209. c->pal[i] = 0xFF << 24 | bytestream_get_be24(&c->stream);
  210. }
  211. if (type & BMV_SCROLL) {
  212. if (c->stream - pkt->data > pkt->size - 2) {
  213. av_log(avctx, AV_LOG_ERROR, "Screen offset data doesn't fit in frame\n");
  214. return AVERROR_INVALIDDATA;
  215. }
  216. scr_off = (int16_t)bytestream_get_le16(&c->stream);
  217. } else if ((type & BMV_INTRA) == BMV_INTRA) {
  218. scr_off = -640;
  219. } else {
  220. scr_off = 0;
  221. }
  222. if (decode_bmv_frame(c->stream, pkt->size - (c->stream - pkt->data), c->frame, scr_off)) {
  223. av_log(avctx, AV_LOG_ERROR, "Error decoding frame data\n");
  224. return AVERROR_INVALIDDATA;
  225. }
  226. memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
  227. c->pic.palette_has_changed = type & BMV_PALETTE;
  228. outptr = c->pic.data[0];
  229. srcptr = c->frame;
  230. for (i = 0; i < avctx->height; i++) {
  231. memcpy(outptr, srcptr, avctx->width);
  232. srcptr += avctx->width;
  233. outptr += c->pic.linesize[0];
  234. }
  235. *data_size = sizeof(AVFrame);
  236. *(AVFrame*)data = c->pic;
  237. /* always report that the buffer was completely consumed */
  238. return pkt->size;
  239. }
  240. static av_cold int decode_init(AVCodecContext *avctx)
  241. {
  242. BMVDecContext * const c = avctx->priv_data;
  243. c->avctx = avctx;
  244. avctx->pix_fmt = PIX_FMT_PAL8;
  245. c->pic.reference = 1;
  246. if (avctx->get_buffer(avctx, &c->pic) < 0) {
  247. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  248. return -1;
  249. }
  250. c->frame = c->frame_base + 640;
  251. return 0;
  252. }
  253. static av_cold int decode_end(AVCodecContext *avctx)
  254. {
  255. BMVDecContext *c = avctx->priv_data;
  256. if (c->pic.data[0])
  257. avctx->release_buffer(avctx, &c->pic);
  258. return 0;
  259. }
  260. typedef struct BMVAudioDecContext {
  261. AVFrame frame;
  262. } BMVAudioDecContext;
  263. static const int bmv_aud_mults[16] = {
  264. 16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32
  265. };
  266. static av_cold int bmv_aud_decode_init(AVCodecContext *avctx)
  267. {
  268. BMVAudioDecContext *c = avctx->priv_data;
  269. if (avctx->channels != 2) {
  270. av_log(avctx, AV_LOG_INFO, "invalid number of channels\n");
  271. return AVERROR(EINVAL);
  272. }
  273. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  274. avcodec_get_frame_defaults(&c->frame);
  275. avctx->coded_frame = &c->frame;
  276. return 0;
  277. }
  278. static int bmv_aud_decode_frame(AVCodecContext *avctx, void *data,
  279. int *got_frame_ptr, AVPacket *avpkt)
  280. {
  281. BMVAudioDecContext *c = avctx->priv_data;
  282. const uint8_t *buf = avpkt->data;
  283. int buf_size = avpkt->size;
  284. int blocks = 0, total_blocks, i;
  285. int ret;
  286. int16_t *output_samples;
  287. int scale[2];
  288. total_blocks = *buf++;
  289. if (buf_size < total_blocks * 65 + 1) {
  290. av_log(avctx, AV_LOG_ERROR, "expected %d bytes, got %d\n",
  291. total_blocks * 65 + 1, buf_size);
  292. return AVERROR_INVALIDDATA;
  293. }
  294. /* get output buffer */
  295. c->frame.nb_samples = total_blocks * 32;
  296. if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
  297. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  298. return ret;
  299. }
  300. output_samples = (int16_t *)c->frame.data[0];
  301. for (blocks = 0; blocks < total_blocks; blocks++) {
  302. uint8_t code = *buf++;
  303. code = (code >> 1) | (code << 7);
  304. scale[0] = bmv_aud_mults[code & 0xF];
  305. scale[1] = bmv_aud_mults[code >> 4];
  306. for (i = 0; i < 32; i++) {
  307. *output_samples++ = av_clip_int16((scale[0] * (int8_t)*buf++) >> 5);
  308. *output_samples++ = av_clip_int16((scale[1] * (int8_t)*buf++) >> 5);
  309. }
  310. }
  311. *got_frame_ptr = 1;
  312. *(AVFrame *)data = c->frame;
  313. return buf_size;
  314. }
  315. AVCodec ff_bmv_video_decoder = {
  316. .name = "bmv_video",
  317. .type = AVMEDIA_TYPE_VIDEO,
  318. .id = CODEC_ID_BMV_VIDEO,
  319. .priv_data_size = sizeof(BMVDecContext),
  320. .init = decode_init,
  321. .close = decode_end,
  322. .decode = decode_frame,
  323. .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV video"),
  324. };
  325. AVCodec ff_bmv_audio_decoder = {
  326. .name = "bmv_audio",
  327. .type = AVMEDIA_TYPE_AUDIO,
  328. .id = CODEC_ID_BMV_AUDIO,
  329. .priv_data_size = sizeof(BMVAudioDecContext),
  330. .init = bmv_aud_decode_init,
  331. .decode = bmv_aud_decode_frame,
  332. .capabilities = CODEC_CAP_DR1,
  333. .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV audio"),
  334. };