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.

385 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. 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 (!read_two_nibbles) {
  93. if (src < source || src >= source_end)
  94. return -1;
  95. shift += 2;
  96. val |= *src << shift;
  97. if (*src & 0xC)
  98. break;
  99. }
  100. // two upper bits of the nibble is zero,
  101. // so shift top nibble value down into their place
  102. read_two_nibbles = 0;
  103. shift += 2;
  104. mask = (1 << shift) - 1;
  105. val = ((val >> 2) & ~mask) | (val & mask);
  106. NEXT_BYTE(src);
  107. if ((val & (0xC << shift))) {
  108. flag = 1;
  109. break;
  110. }
  111. }
  112. } else if (mode) {
  113. flag = tmplen != 4;
  114. }
  115. if (flag) {
  116. tmplen = 4;
  117. } else {
  118. saved_val = val >> (4 + shift);
  119. tmplen = 0;
  120. val &= (1 << (shift + 4)) - 1;
  121. NEXT_BYTE(src);
  122. }
  123. advance_mode = val & 1;
  124. len = (val >> 1) - 1;
  125. mode += 1 + advance_mode;
  126. if (mode >= 4)
  127. mode -= 3;
  128. if (FFABS(dst_end - dst) < len)
  129. return -1;
  130. switch (mode) {
  131. case 1:
  132. if (forward) {
  133. if (dst - frame + SCREEN_WIDE < frame_off ||
  134. dst - frame + SCREEN_WIDE + frame_off < 0 ||
  135. frame_end - dst < frame_off + len ||
  136. frame_end - dst < len)
  137. return -1;
  138. for (i = 0; i < len; i++)
  139. dst[i] = dst[frame_off + i];
  140. dst += len;
  141. } else {
  142. dst -= len;
  143. if (dst - frame + SCREEN_WIDE < frame_off ||
  144. dst - frame + SCREEN_WIDE + frame_off < 0 ||
  145. frame_end - dst < frame_off + len ||
  146. frame_end - dst < len)
  147. return -1;
  148. for (i = len - 1; i >= 0; i--)
  149. dst[i] = dst[frame_off + i];
  150. }
  151. break;
  152. case 2:
  153. if (forward) {
  154. if (source + src_len - src < len)
  155. return -1;
  156. memcpy(dst, src, len);
  157. dst += len;
  158. src += len;
  159. } else {
  160. if (src - source < len)
  161. return -1;
  162. dst -= len;
  163. src -= len;
  164. memcpy(dst, src, len);
  165. }
  166. break;
  167. case 3:
  168. val = forward ? dst[-1] : dst[1];
  169. if (forward) {
  170. memset(dst, val, len);
  171. dst += len;
  172. } else {
  173. dst -= len;
  174. memset(dst, val, len);
  175. }
  176. break;
  177. default:
  178. break;
  179. }
  180. if (dst == dst_end)
  181. return 0;
  182. }
  183. return 0;
  184. }
  185. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  186. 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] = 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 = ff_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. *got_frame = 1;
  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. c->frame = c->frame_base + 640;
  260. return 0;
  261. }
  262. static av_cold int decode_end(AVCodecContext *avctx)
  263. {
  264. BMVDecContext *c = avctx->priv_data;
  265. if (c->pic.data[0])
  266. avctx->release_buffer(avctx, &c->pic);
  267. return 0;
  268. }
  269. typedef struct BMVAudioDecContext {
  270. AVFrame frame;
  271. } BMVAudioDecContext;
  272. static const int bmv_aud_mults[16] = {
  273. 16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32
  274. };
  275. static av_cold int bmv_aud_decode_init(AVCodecContext *avctx)
  276. {
  277. BMVAudioDecContext *c = avctx->priv_data;
  278. avctx->channels = 2;
  279. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  280. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  281. avcodec_get_frame_defaults(&c->frame);
  282. avctx->coded_frame = &c->frame;
  283. return 0;
  284. }
  285. static int bmv_aud_decode_frame(AVCodecContext *avctx, void *data,
  286. int *got_frame_ptr, AVPacket *avpkt)
  287. {
  288. BMVAudioDecContext *c = avctx->priv_data;
  289. const uint8_t *buf = avpkt->data;
  290. int buf_size = avpkt->size;
  291. int blocks = 0, total_blocks, i;
  292. int ret;
  293. int16_t *output_samples;
  294. int scale[2];
  295. total_blocks = *buf++;
  296. if (buf_size < total_blocks * 65 + 1) {
  297. av_log(avctx, AV_LOG_ERROR, "expected %d bytes, got %d\n",
  298. total_blocks * 65 + 1, buf_size);
  299. return AVERROR_INVALIDDATA;
  300. }
  301. /* get output buffer */
  302. c->frame.nb_samples = total_blocks * 32;
  303. if ((ret = ff_get_buffer(avctx, &c->frame)) < 0) {
  304. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  305. return ret;
  306. }
  307. output_samples = (int16_t *)c->frame.data[0];
  308. for (blocks = 0; blocks < total_blocks; blocks++) {
  309. uint8_t code = *buf++;
  310. code = (code >> 1) | (code << 7);
  311. scale[0] = bmv_aud_mults[code & 0xF];
  312. scale[1] = bmv_aud_mults[code >> 4];
  313. for (i = 0; i < 32; i++) {
  314. *output_samples++ = av_clip_int16((scale[0] * (int8_t)*buf++) >> 5);
  315. *output_samples++ = av_clip_int16((scale[1] * (int8_t)*buf++) >> 5);
  316. }
  317. }
  318. *got_frame_ptr = 1;
  319. *(AVFrame *)data = c->frame;
  320. return buf_size;
  321. }
  322. AVCodec ff_bmv_video_decoder = {
  323. .name = "bmv_video",
  324. .type = AVMEDIA_TYPE_VIDEO,
  325. .id = AV_CODEC_ID_BMV_VIDEO,
  326. .priv_data_size = sizeof(BMVDecContext),
  327. .init = decode_init,
  328. .close = decode_end,
  329. .decode = decode_frame,
  330. .capabilities = CODEC_CAP_DR1,
  331. .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV video"),
  332. };
  333. AVCodec ff_bmv_audio_decoder = {
  334. .name = "bmv_audio",
  335. .type = AVMEDIA_TYPE_AUDIO,
  336. .id = AV_CODEC_ID_BMV_AUDIO,
  337. .priv_data_size = sizeof(BMVAudioDecContext),
  338. .init = bmv_aud_decode_init,
  339. .decode = bmv_aud_decode_frame,
  340. .capabilities = CODEC_CAP_DR1,
  341. .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV audio"),
  342. };