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.

381 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;
  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 (decode_bmv_frame(c->stream, pkt->size - (c->stream - pkt->data), c->frame, scr_off)) {
  227. av_log(avctx, AV_LOG_ERROR, "Error decoding frame data\n");
  228. return AVERROR_INVALIDDATA;
  229. }
  230. memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
  231. c->pic.palette_has_changed = type & BMV_PALETTE;
  232. outptr = c->pic.data[0];
  233. srcptr = c->frame;
  234. for (i = 0; i < avctx->height; i++) {
  235. memcpy(outptr, srcptr, avctx->width);
  236. srcptr += avctx->width;
  237. outptr += c->pic.linesize[0];
  238. }
  239. *data_size = sizeof(AVFrame);
  240. *(AVFrame*)data = c->pic;
  241. /* always report that the buffer was completely consumed */
  242. return pkt->size;
  243. }
  244. static av_cold int decode_init(AVCodecContext *avctx)
  245. {
  246. BMVDecContext * const c = avctx->priv_data;
  247. c->avctx = avctx;
  248. avctx->pix_fmt = PIX_FMT_PAL8;
  249. c->pic.reference = 1;
  250. if (avctx->get_buffer(avctx, &c->pic) < 0) {
  251. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  252. return -1;
  253. }
  254. c->frame = c->frame_base + 640;
  255. return 0;
  256. }
  257. static av_cold int decode_end(AVCodecContext *avctx)
  258. {
  259. BMVDecContext *c = avctx->priv_data;
  260. if (c->pic.data[0])
  261. avctx->release_buffer(avctx, &c->pic);
  262. return 0;
  263. }
  264. typedef struct BMVAudioDecContext {
  265. AVFrame frame;
  266. } BMVAudioDecContext;
  267. static const int bmv_aud_mults[16] = {
  268. 16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32
  269. };
  270. static av_cold int bmv_aud_decode_init(AVCodecContext *avctx)
  271. {
  272. BMVAudioDecContext *c = avctx->priv_data;
  273. if (avctx->channels != 2) {
  274. av_log(avctx, AV_LOG_INFO, "invalid number of channels\n");
  275. return AVERROR(EINVAL);
  276. }
  277. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  278. avcodec_get_frame_defaults(&c->frame);
  279. avctx->coded_frame = &c->frame;
  280. return 0;
  281. }
  282. static int bmv_aud_decode_frame(AVCodecContext *avctx, void *data,
  283. int *got_frame_ptr, AVPacket *avpkt)
  284. {
  285. BMVAudioDecContext *c = avctx->priv_data;
  286. const uint8_t *buf = avpkt->data;
  287. int buf_size = avpkt->size;
  288. int blocks = 0, total_blocks, i;
  289. int ret;
  290. int16_t *output_samples;
  291. int scale[2];
  292. total_blocks = *buf++;
  293. if (buf_size < total_blocks * 65 + 1) {
  294. av_log(avctx, AV_LOG_ERROR, "expected %d bytes, got %d\n",
  295. total_blocks * 65 + 1, buf_size);
  296. return AVERROR_INVALIDDATA;
  297. }
  298. /* get output buffer */
  299. c->frame.nb_samples = total_blocks * 32;
  300. if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
  301. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  302. return ret;
  303. }
  304. output_samples = (int16_t *)c->frame.data[0];
  305. for (blocks = 0; blocks < total_blocks; blocks++) {
  306. uint8_t code = *buf++;
  307. code = (code >> 1) | (code << 7);
  308. scale[0] = bmv_aud_mults[code & 0xF];
  309. scale[1] = bmv_aud_mults[code >> 4];
  310. for (i = 0; i < 32; i++) {
  311. *output_samples++ = av_clip_int16((scale[0] * (int8_t)*buf++) >> 5);
  312. *output_samples++ = av_clip_int16((scale[1] * (int8_t)*buf++) >> 5);
  313. }
  314. }
  315. *got_frame_ptr = 1;
  316. *(AVFrame *)data = c->frame;
  317. return buf_size;
  318. }
  319. AVCodec ff_bmv_video_decoder = {
  320. .name = "bmv_video",
  321. .type = AVMEDIA_TYPE_VIDEO,
  322. .id = CODEC_ID_BMV_VIDEO,
  323. .priv_data_size = sizeof(BMVDecContext),
  324. .init = decode_init,
  325. .close = decode_end,
  326. .decode = decode_frame,
  327. .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV video"),
  328. };
  329. AVCodec ff_bmv_audio_decoder = {
  330. .name = "bmv_audio",
  331. .type = AVMEDIA_TYPE_AUDIO,
  332. .id = CODEC_ID_BMV_AUDIO,
  333. .priv_data_size = sizeof(BMVAudioDecContext),
  334. .init = bmv_aud_decode_init,
  335. .decode = bmv_aud_decode_frame,
  336. .capabilities = CODEC_CAP_DR1,
  337. .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV audio"),
  338. };