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.

361 lines
11KB

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