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.

358 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. #include "internal.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. 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 AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  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 (len <= 0 || FFABS(dst_end - dst) < len)
  128. return AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  155. memcpy(dst, src, len);
  156. dst += len;
  157. src += len;
  158. } else {
  159. if (src - source < len)
  160. return AVERROR_INVALIDDATA;
  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 *got_frame,
  185. AVPacket *pkt)
  186. {
  187. BMVDecContext * const c = avctx->priv_data;
  188. AVFrame *frame = 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] = 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 ((ret = ff_get_buffer(avctx, frame, 0)) < 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(frame->data[1], c->pal, AVPALETTE_SIZE);
  238. frame->palette_has_changed = type & BMV_PALETTE;
  239. outptr = frame->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 += frame->linesize[0];
  245. }
  246. *got_frame = 1;
  247. /* always report that the buffer was completely consumed */
  248. return pkt->size;
  249. }
  250. static av_cold int decode_init(AVCodecContext *avctx)
  251. {
  252. BMVDecContext * const c = avctx->priv_data;
  253. c->avctx = avctx;
  254. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  255. c->frame = c->frame_base + 640;
  256. return 0;
  257. }
  258. static const int bmv_aud_mults[16] = {
  259. 16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32
  260. };
  261. static av_cold int bmv_aud_decode_init(AVCodecContext *avctx)
  262. {
  263. avctx->channels = 2;
  264. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  265. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  266. return 0;
  267. }
  268. static int bmv_aud_decode_frame(AVCodecContext *avctx, void *data,
  269. int *got_frame_ptr, AVPacket *avpkt)
  270. {
  271. AVFrame *frame = data;
  272. const uint8_t *buf = avpkt->data;
  273. int buf_size = avpkt->size;
  274. int blocks = 0, total_blocks, i;
  275. int ret;
  276. int16_t *output_samples;
  277. int scale[2];
  278. total_blocks = *buf++;
  279. if (buf_size < total_blocks * 65 + 1) {
  280. av_log(avctx, AV_LOG_ERROR, "expected %d bytes, got %d\n",
  281. total_blocks * 65 + 1, buf_size);
  282. return AVERROR_INVALIDDATA;
  283. }
  284. /* get output buffer */
  285. frame->nb_samples = total_blocks * 32;
  286. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  287. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  288. return ret;
  289. }
  290. output_samples = (int16_t *)frame->data[0];
  291. for (blocks = 0; blocks < total_blocks; blocks++) {
  292. uint8_t code = *buf++;
  293. code = (code >> 1) | (code << 7);
  294. scale[0] = bmv_aud_mults[code & 0xF];
  295. scale[1] = bmv_aud_mults[code >> 4];
  296. for (i = 0; i < 32; i++) {
  297. *output_samples++ = av_clip_int16((scale[0] * (int8_t)*buf++) >> 5);
  298. *output_samples++ = av_clip_int16((scale[1] * (int8_t)*buf++) >> 5);
  299. }
  300. }
  301. *got_frame_ptr = 1;
  302. return buf_size;
  303. }
  304. AVCodec ff_bmv_video_decoder = {
  305. .name = "bmv_video",
  306. .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV video"),
  307. .type = AVMEDIA_TYPE_VIDEO,
  308. .id = AV_CODEC_ID_BMV_VIDEO,
  309. .priv_data_size = sizeof(BMVDecContext),
  310. .init = decode_init,
  311. .decode = decode_frame,
  312. .capabilities = CODEC_CAP_DR1,
  313. };
  314. AVCodec ff_bmv_audio_decoder = {
  315. .name = "bmv_audio",
  316. .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV audio"),
  317. .type = AVMEDIA_TYPE_AUDIO,
  318. .id = AV_CODEC_ID_BMV_AUDIO,
  319. .init = bmv_aud_decode_init,
  320. .decode = bmv_aud_decode_frame,
  321. .capabilities = CODEC_CAP_DR1,
  322. };