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.

390 lines
12KB

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