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.

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