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.

383 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 "libavutil/channel_layout.h"
  22. #include "avcodec.h"
  23. #include "bytestream.h"
  24. enum BMVFlags{
  25. BMV_NOP = 0,
  26. BMV_END,
  27. BMV_DELTA,
  28. BMV_INTRA,
  29. BMV_SCROLL = 0x04,
  30. BMV_PALETTE = 0x08,
  31. BMV_COMMAND = 0x10,
  32. BMV_AUDIO = 0x20,
  33. BMV_EXT = 0x40,
  34. BMV_PRINT = 0x80
  35. };
  36. #define SCREEN_WIDE 640
  37. #define SCREEN_HIGH 429
  38. typedef struct BMVDecContext {
  39. AVCodecContext *avctx;
  40. AVFrame pic;
  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 -1;
  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 -1;
  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 -1;
  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 (FFABS(dst_end - dst) < len)
  128. return -1;
  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 -1;
  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 -1;
  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 -1;
  155. memcpy(dst, src, len);
  156. dst += len;
  157. src += len;
  158. } else {
  159. if (src - source < len)
  160. return -1;
  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. return 0;
  183. }
  184. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
  185. {
  186. BMVDecContext * const c = avctx->priv_data;
  187. int type, scr_off;
  188. int i, ret;
  189. uint8_t *srcptr, *outptr;
  190. c->stream = pkt->data;
  191. type = bytestream_get_byte(&c->stream);
  192. if (type & BMV_AUDIO) {
  193. int blobs = bytestream_get_byte(&c->stream);
  194. if (pkt->size < blobs * 65 + 2) {
  195. av_log(avctx, AV_LOG_ERROR, "Audio data doesn't fit in frame\n");
  196. return AVERROR_INVALIDDATA;
  197. }
  198. c->stream += blobs * 65;
  199. }
  200. if (type & BMV_COMMAND) {
  201. int command_size = (type & BMV_PRINT) ? 8 : 10;
  202. if (c->stream - pkt->data + command_size > pkt->size) {
  203. av_log(avctx, AV_LOG_ERROR, "Command data doesn't fit in frame\n");
  204. return AVERROR_INVALIDDATA;
  205. }
  206. c->stream += command_size;
  207. }
  208. if (type & BMV_PALETTE) {
  209. if (c->stream - pkt->data > pkt->size - 768) {
  210. av_log(avctx, AV_LOG_ERROR, "Palette data doesn't fit in frame\n");
  211. return AVERROR_INVALIDDATA;
  212. }
  213. for (i = 0; i < 256; i++)
  214. c->pal[i] = bytestream_get_be24(&c->stream);
  215. }
  216. if (type & BMV_SCROLL) {
  217. if (c->stream - pkt->data > pkt->size - 2) {
  218. av_log(avctx, AV_LOG_ERROR, "Screen offset data doesn't fit in frame\n");
  219. return AVERROR_INVALIDDATA;
  220. }
  221. scr_off = (int16_t)bytestream_get_le16(&c->stream);
  222. } else if ((type & BMV_INTRA) == BMV_INTRA) {
  223. scr_off = -640;
  224. } else {
  225. scr_off = 0;
  226. }
  227. if (c->pic.data[0])
  228. avctx->release_buffer(avctx, &c->pic);
  229. c->pic.reference = 3;
  230. if ((ret = avctx->get_buffer(avctx, &c->pic)) < 0) {
  231. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  232. return ret;
  233. }
  234. if (decode_bmv_frame(c->stream, pkt->size - (c->stream - pkt->data), c->frame, scr_off)) {
  235. av_log(avctx, AV_LOG_ERROR, "Error decoding frame data\n");
  236. return AVERROR_INVALIDDATA;
  237. }
  238. memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
  239. c->pic.palette_has_changed = type & BMV_PALETTE;
  240. outptr = c->pic.data[0];
  241. srcptr = c->frame;
  242. for (i = 0; i < avctx->height; i++) {
  243. memcpy(outptr, srcptr, avctx->width);
  244. srcptr += avctx->width;
  245. outptr += c->pic.linesize[0];
  246. }
  247. *data_size = sizeof(AVFrame);
  248. *(AVFrame*)data = c->pic;
  249. /* always report that the buffer was completely consumed */
  250. return pkt->size;
  251. }
  252. static av_cold int decode_init(AVCodecContext *avctx)
  253. {
  254. BMVDecContext * const c = avctx->priv_data;
  255. c->avctx = avctx;
  256. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  257. c->frame = c->frame_base + 640;
  258. return 0;
  259. }
  260. static av_cold int decode_end(AVCodecContext *avctx)
  261. {
  262. BMVDecContext *c = avctx->priv_data;
  263. if (c->pic.data[0])
  264. avctx->release_buffer(avctx, &c->pic);
  265. return 0;
  266. }
  267. typedef struct BMVAudioDecContext {
  268. AVFrame frame;
  269. } BMVAudioDecContext;
  270. static const int bmv_aud_mults[16] = {
  271. 16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32
  272. };
  273. static av_cold int bmv_aud_decode_init(AVCodecContext *avctx)
  274. {
  275. BMVAudioDecContext *c = avctx->priv_data;
  276. avctx->channels = 2;
  277. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  278. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  279. avcodec_get_frame_defaults(&c->frame);
  280. avctx->coded_frame = &c->frame;
  281. return 0;
  282. }
  283. static int bmv_aud_decode_frame(AVCodecContext *avctx, void *data,
  284. int *got_frame_ptr, AVPacket *avpkt)
  285. {
  286. BMVAudioDecContext *c = avctx->priv_data;
  287. const uint8_t *buf = avpkt->data;
  288. int buf_size = avpkt->size;
  289. int blocks = 0, total_blocks, i;
  290. int ret;
  291. int16_t *output_samples;
  292. int scale[2];
  293. total_blocks = *buf++;
  294. if (buf_size < total_blocks * 65 + 1) {
  295. av_log(avctx, AV_LOG_ERROR, "expected %d bytes, got %d\n",
  296. total_blocks * 65 + 1, buf_size);
  297. return AVERROR_INVALIDDATA;
  298. }
  299. /* get output buffer */
  300. c->frame.nb_samples = total_blocks * 32;
  301. if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
  302. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  303. return ret;
  304. }
  305. output_samples = (int16_t *)c->frame.data[0];
  306. for (blocks = 0; blocks < total_blocks; blocks++) {
  307. uint8_t code = *buf++;
  308. code = (code >> 1) | (code << 7);
  309. scale[0] = bmv_aud_mults[code & 0xF];
  310. scale[1] = bmv_aud_mults[code >> 4];
  311. for (i = 0; i < 32; i++) {
  312. *output_samples++ = av_clip_int16((scale[0] * (int8_t)*buf++) >> 5);
  313. *output_samples++ = av_clip_int16((scale[1] * (int8_t)*buf++) >> 5);
  314. }
  315. }
  316. *got_frame_ptr = 1;
  317. *(AVFrame *)data = c->frame;
  318. return buf_size;
  319. }
  320. AVCodec ff_bmv_video_decoder = {
  321. .name = "bmv_video",
  322. .type = AVMEDIA_TYPE_VIDEO,
  323. .id = AV_CODEC_ID_BMV_VIDEO,
  324. .priv_data_size = sizeof(BMVDecContext),
  325. .init = decode_init,
  326. .close = decode_end,
  327. .decode = decode_frame,
  328. .capabilities = CODEC_CAP_DR1,
  329. .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV video"),
  330. };
  331. AVCodec ff_bmv_audio_decoder = {
  332. .name = "bmv_audio",
  333. .type = AVMEDIA_TYPE_AUDIO,
  334. .id = AV_CODEC_ID_BMV_AUDIO,
  335. .priv_data_size = sizeof(BMVAudioDecContext),
  336. .init = bmv_aud_decode_init,
  337. .decode = bmv_aud_decode_frame,
  338. .capabilities = CODEC_CAP_DR1,
  339. .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV audio"),
  340. };