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.

552 lines
15KB

  1. /*
  2. * Sierra VMD Audio & Video Decoders
  3. * Copyright (C) 2004 the ffmpeg project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file vmdvideo.c
  22. * Sierra VMD audio & video decoders
  23. * by Vladimir "VAG" Gneushev (vagsoft at mail.ru)
  24. *
  25. * The video decoder outputs PAL8 colorspace data. The decoder expects
  26. * a 0x330-byte VMD file header to be transmitted via extradata during
  27. * codec initialization. Each encoded frame that is sent to this decoder
  28. * is expected to be prepended with the appropriate 16-byte frame
  29. * information record from the VMD file.
  30. *
  31. * The audio decoder, like the video decoder, expects each encoded data
  32. * chunk to be prepended with the approriate 16-byte frame information
  33. * record from the VMD file. It does not require the 0x330-byte VMD file
  34. * header, but it does need the audio setup parameters passed in through
  35. * normal libavcodec API means.
  36. */
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #include "common.h"
  42. #include "avcodec.h"
  43. #include "dsputil.h"
  44. #define printf(...) {} //(f)printf() usage is forbidden in libavcodec, use av_log
  45. #define fprintf(...) {}
  46. #define VMD_HEADER_SIZE 0x330
  47. #define PALETTE_COUNT 256
  48. #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
  49. #define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
  50. (((uint8_t*)(x))[2] << 16) | \
  51. (((uint8_t*)(x))[1] << 8) | \
  52. ((uint8_t*)(x))[0])
  53. /*
  54. * Video Decoder
  55. */
  56. typedef struct VmdVideoContext {
  57. AVCodecContext *avctx;
  58. DSPContext dsp;
  59. AVFrame frame;
  60. AVFrame prev_frame;
  61. unsigned char *buf;
  62. int size;
  63. unsigned char palette[PALETTE_COUNT * 4];
  64. unsigned char *unpack_buffer;
  65. } VmdVideoContext;
  66. #define QUEUE_SIZE 0x1000
  67. #define QUEUE_MASK 0x0FFF
  68. static void lz_unpack(unsigned char *src, unsigned char *dest)
  69. {
  70. unsigned char *s;
  71. unsigned char *d;
  72. unsigned char queue[QUEUE_SIZE];
  73. unsigned int qpos;
  74. unsigned int dataleft;
  75. unsigned int chainofs;
  76. unsigned int chainlen;
  77. unsigned int speclen;
  78. unsigned char tag;
  79. unsigned int i, j;
  80. s = src;
  81. d = dest;
  82. dataleft = LE_32(s);
  83. s += 4;
  84. memset(queue, QUEUE_SIZE, 0x20);
  85. if (LE_32(s) == 0x56781234) {
  86. s += 4;
  87. qpos = 0x111;
  88. speclen = 0xF + 3;
  89. } else {
  90. qpos = 0xFEE;
  91. speclen = 100; /* no speclen */
  92. }
  93. while (dataleft > 0) {
  94. tag = *s++;
  95. if ((tag == 0xFF) && (dataleft > 8)) {
  96. for (i = 0; i < 8; i++) {
  97. queue[qpos++] = *d++ = *s++;
  98. qpos &= QUEUE_MASK;
  99. }
  100. dataleft -= 8;
  101. } else {
  102. for (i = 0; i < 8; i++) {
  103. if (dataleft == 0)
  104. break;
  105. if (tag & 0x01) {
  106. queue[qpos++] = *d++ = *s++;
  107. qpos &= QUEUE_MASK;
  108. dataleft--;
  109. } else {
  110. chainofs = *s++;
  111. chainofs |= ((*s & 0xF0) << 4);
  112. chainlen = (*s++ & 0x0F) + 3;
  113. if (chainlen == speclen)
  114. chainlen = *s++ + 0xF + 3;
  115. for (j = 0; j < chainlen; j++) {
  116. *d = queue[chainofs++ & QUEUE_MASK];
  117. queue[qpos++] = *d++;
  118. qpos &= QUEUE_MASK;
  119. }
  120. dataleft -= chainlen;
  121. }
  122. tag >>= 1;
  123. }
  124. }
  125. }
  126. }
  127. static int rle_unpack(unsigned char *src, unsigned char *dest, int len)
  128. {
  129. unsigned char *ps;
  130. unsigned char *pd;
  131. int i, l;
  132. ps = src;
  133. pd = dest;
  134. if (len & 1)
  135. *pd++ = *ps++;
  136. len >>= 1;
  137. i = 0;
  138. do {
  139. l = *ps++;
  140. if (l & 0x80) {
  141. l = (l & 0x7F) * 2;
  142. memcpy(pd, ps, l);
  143. ps += l;
  144. pd += l;
  145. } else {
  146. for (i = 0; i < l; i++) {
  147. *pd++ = ps[0];
  148. *pd++ = ps[1];
  149. }
  150. ps += 2;
  151. }
  152. i += l;
  153. } while (i < len);
  154. return (ps - src);
  155. }
  156. static void vmd_decode(VmdVideoContext *s)
  157. {
  158. int i;
  159. unsigned int *palette32;
  160. unsigned char r, g, b;
  161. /* point to the start of the encoded data */
  162. unsigned char *p = s->buf + 16;
  163. unsigned char *pb;
  164. unsigned char meth;
  165. unsigned char *dp; /* pointer to current frame */
  166. unsigned char *pp; /* pointer to previous frame */
  167. unsigned char len;
  168. int ofs;
  169. int frame_x, frame_y;
  170. int frame_width, frame_height;
  171. frame_x = LE_16(&s->buf[6]);
  172. frame_y = LE_16(&s->buf[8]);
  173. frame_width = LE_16(&s->buf[10]) - frame_x + 1;
  174. frame_height = LE_16(&s->buf[12]) - frame_y + 1;
  175. /* if only a certain region will be updated, copy the entire previous
  176. * frame before the decode */
  177. if (frame_x || frame_y || (frame_width != s->avctx->width) ||
  178. (frame_height != s->avctx->height)) {
  179. memcpy(s->frame.data[0], s->prev_frame.data[0],
  180. s->avctx->height * s->frame.linesize[0]);
  181. }
  182. /* check if there is a new palette */
  183. if (s->buf[15] & 0x02) {
  184. p += 2;
  185. palette32 = (unsigned int *)s->palette;
  186. for (i = 0; i < PALETTE_COUNT; i++) {
  187. r = *p++ * 4;
  188. g = *p++ * 4;
  189. b = *p++ * 4;
  190. palette32[i] = (r << 16) | (g << 8) | (b);
  191. }
  192. s->size -= (256 * 3 + 2);
  193. }
  194. if (s->size >= 0) {
  195. /* originally UnpackFrame in VAG's code */
  196. pb = p;
  197. meth = *pb++;
  198. if (meth & 0x80) {
  199. lz_unpack(pb, s->unpack_buffer);
  200. meth &= 0x7F;
  201. pb = s->unpack_buffer;
  202. }
  203. dp = &s->frame.data[0][frame_y * s->frame.linesize[0] + frame_x];
  204. pp = &s->prev_frame.data[0][frame_y * s->prev_frame.linesize[0] + frame_x];
  205. switch (meth) {
  206. case 1:
  207. for (i = 0; i < frame_height; i++) {
  208. ofs = 0;
  209. do {
  210. len = *pb++;
  211. if (len & 0x80) {
  212. len = (len & 0x7F) + 1;
  213. memcpy(&dp[ofs], pb, len);
  214. pb += len;
  215. ofs += len;
  216. } else {
  217. /* interframe pixel copy */
  218. memcpy(&dp[ofs], &pp[ofs], len + 1);
  219. ofs += len + 1;
  220. }
  221. } while (ofs < frame_width);
  222. if (ofs > frame_width) {
  223. printf (" VMD video: offset > width (%d > %d)\n",
  224. ofs, frame_width);
  225. break;
  226. }
  227. dp += s->frame.linesize[0];
  228. pp += s->prev_frame.linesize[0];
  229. }
  230. break;
  231. case 2:
  232. for (i = 0; i < frame_height; i++) {
  233. memcpy(dp, pb, frame_width);
  234. pb += frame_width;
  235. dp += s->frame.linesize[0];
  236. pp += s->prev_frame.linesize[0];
  237. }
  238. break;
  239. case 3:
  240. for (i = 0; i < frame_height; i++) {
  241. ofs = 0;
  242. do {
  243. len = *pb++;
  244. if (len & 0x80) {
  245. len = (len & 0x7F) + 1;
  246. if (*pb++ == 0xFF)
  247. len = rle_unpack(pb, dp, len);
  248. else
  249. memcpy(&dp[ofs], pb, len);
  250. pb += len;
  251. ofs += len;
  252. } else {
  253. /* interframe pixel copy */
  254. memcpy(&dp[ofs], &pp[ofs], len + 1);
  255. ofs += len + 1;
  256. }
  257. } while (ofs < frame_width);
  258. if (ofs > frame_width) {
  259. printf (" VMD video: offset > width (%d > %d)\n",
  260. ofs, frame_width);
  261. }
  262. dp += s->frame.linesize[0];
  263. pp += s->prev_frame.linesize[0];
  264. }
  265. break;
  266. }
  267. }
  268. }
  269. static int vmdvideo_decode_init(AVCodecContext *avctx)
  270. {
  271. VmdVideoContext *s = (VmdVideoContext *)avctx->priv_data;
  272. int i;
  273. unsigned int *palette32;
  274. int palette_index = 0;
  275. unsigned char r, g, b;
  276. unsigned char *vmd_header;
  277. unsigned char *raw_palette;
  278. s->avctx = avctx;
  279. avctx->pix_fmt = PIX_FMT_PAL8;
  280. avctx->has_b_frames = 0;
  281. dsputil_init(&s->dsp, avctx);
  282. /* make sure the VMD header made it */
  283. if (s->avctx->extradata_size != VMD_HEADER_SIZE) {
  284. printf(" VMD video: expected extradata size of %d\n",
  285. VMD_HEADER_SIZE);
  286. return -1;
  287. }
  288. vmd_header = (unsigned char *)avctx->extradata;
  289. s->unpack_buffer = av_malloc(LE_32(&vmd_header[800]));
  290. if (!s->unpack_buffer)
  291. return -1;
  292. /* load up the initial palette */
  293. raw_palette = &vmd_header[28];
  294. palette32 = (unsigned int *)s->palette;
  295. for (i = 0; i < PALETTE_COUNT; i++) {
  296. r = raw_palette[palette_index++] * 4;
  297. g = raw_palette[palette_index++] * 4;
  298. b = raw_palette[palette_index++] * 4;
  299. palette32[i] = (r << 16) | (g << 8) | (b);
  300. }
  301. s->frame.data[0] = s->prev_frame.data[0] = NULL;
  302. return 0;
  303. }
  304. static int vmdvideo_decode_frame(AVCodecContext *avctx,
  305. void *data, int *data_size,
  306. uint8_t *buf, int buf_size)
  307. {
  308. VmdVideoContext *s = (VmdVideoContext *)avctx->priv_data;
  309. s->buf = buf;
  310. s->size = buf_size;
  311. s->frame.reference = 1;
  312. if (avctx->get_buffer(avctx, &s->frame)) {
  313. printf (" VMD Video: get_buffer() failed\n");
  314. return -1;
  315. }
  316. vmd_decode(s);
  317. /* make the palette available on the way out */
  318. memcpy(s->frame.data[1], s->palette, PALETTE_COUNT * 4);
  319. if (s->prev_frame.data[0])
  320. avctx->release_buffer(avctx, &s->prev_frame);
  321. /* shuffle frames */
  322. s->prev_frame = s->frame;
  323. *data_size = sizeof(AVFrame);
  324. *(AVFrame*)data = s->frame;
  325. /* report that the buffer was completely consumed */
  326. return buf_size;
  327. }
  328. static int vmdvideo_decode_end(AVCodecContext *avctx)
  329. {
  330. VmdVideoContext *s = (VmdVideoContext *)avctx->priv_data;
  331. if (s->prev_frame.data[0])
  332. avctx->release_buffer(avctx, &s->prev_frame);
  333. av_free(s->unpack_buffer);
  334. return 0;
  335. }
  336. /*
  337. * Audio Decoder
  338. */
  339. typedef struct VmdAudioContext {
  340. int channels;
  341. int bits;
  342. int block_align;
  343. unsigned char steps8[16];
  344. unsigned short steps16[16];
  345. unsigned short steps128[256];
  346. short predictors[2];
  347. } VmdAudioContext;
  348. static int vmdaudio_decode_init(AVCodecContext *avctx)
  349. {
  350. VmdAudioContext *s = (VmdAudioContext *)avctx->priv_data;
  351. int i;
  352. s->channels = avctx->channels;
  353. s->bits = avctx->bits_per_sample;
  354. s->block_align = avctx->block_align;
  355. printf (" %d channels, %d bits/sample, block align = %d\n",
  356. s->channels, s->bits, s->block_align);
  357. /* set up the steps8 and steps16 tables */
  358. for (i = 0; i < 8; i++) {
  359. if (i < 4)
  360. s->steps8[i] = i;
  361. else
  362. s->steps8[i] = s->steps8[i - 1] + i - 1;
  363. if (i == 0)
  364. s->steps16[i] = 0;
  365. else if (i == 1)
  366. s->steps16[i] = 4;
  367. else if (i == 2)
  368. s->steps16[i] = 16;
  369. else
  370. s->steps16[i] = 1 << (i + 4);
  371. }
  372. /* set up the step128 table */
  373. s->steps128[0] = 0;
  374. s->steps128[1] = 8;
  375. for (i = 0x02; i <= 0x20; i++)
  376. s->steps128[i] = (i - 1) << 4;
  377. for (i = 0x21; i <= 0x60; i++)
  378. s->steps128[i] = (i + 0x1F) << 3;
  379. for (i = 0x61; i <= 0x70; i++)
  380. s->steps128[i] = (i - 0x51) << 6;
  381. for (i = 0x71; i <= 0x78; i++)
  382. s->steps128[i] = (i - 0x69) << 8;
  383. for (i = 0x79; i <= 0x7D; i++)
  384. s->steps128[i] = (i - 0x75) << 10;
  385. s->steps128[0x7E] = 0x3000;
  386. s->steps128[0x7F] = 0x4000;
  387. /* set up the negative half of each table */
  388. for (i = 0; i < 8; i++) {
  389. s->steps8[i + 8] = -s->steps8[i];
  390. s->steps16[i + 8] = -s->steps16[i];
  391. }
  392. for (i = 0; i < 128; i++)
  393. s->steps128[i + 128] = -s->steps128[i];
  394. return 0;
  395. }
  396. static void vmdaudio_decode_audio(VmdAudioContext *s, unsigned char *data,
  397. uint8_t *buf, int ratio) {
  398. }
  399. static void vmdaudio_loadsound(VmdAudioContext *s, unsigned char *data,
  400. uint8_t *buf, int silence)
  401. {
  402. if (s->channels == 2) {
  403. if ((s->block_align & 0x01) == 0) {
  404. if (silence)
  405. memset(data, 0, s->block_align * 2);
  406. else
  407. vmdaudio_decode_audio(s, data, buf, 1);
  408. } else {
  409. if (silence)
  410. memset(data, 0, s->block_align * 2);
  411. // else
  412. // vmdaudio_decode_audio(s, data, buf, 1);
  413. }
  414. } else {
  415. }
  416. }
  417. static int vmdaudio_decode_frame(AVCodecContext *avctx,
  418. void *data, int *data_size,
  419. uint8_t *buf, int buf_size)
  420. {
  421. VmdAudioContext *s = (VmdAudioContext *)avctx->priv_data;
  422. unsigned int sound_flags;
  423. unsigned char *output_samples = (unsigned char *)data;
  424. /* point to the start of the encoded data */
  425. unsigned char *p = buf + 16;
  426. unsigned char *p_end = buf + buf_size;
  427. if (buf[6] == 1) {
  428. /* the chunk contains audio */
  429. vmdaudio_loadsound(s, output_samples, p, 0);
  430. } else if (buf[6] == 2) {
  431. /* the chunk contains audio and silence mixed together */
  432. sound_flags = LE_32(p);
  433. p += 4;
  434. /* do something with extrabufs here? */
  435. while (p < p_end) {
  436. if (sound_flags & 0x01)
  437. /* audio */
  438. vmdaudio_loadsound(s, output_samples, p, 1);
  439. else
  440. /* silence */
  441. vmdaudio_loadsound(s, output_samples, p, 0);
  442. p += s->block_align;
  443. output_samples += (s->block_align * s->bits / 8);
  444. sound_flags >>= 1;
  445. }
  446. } else if (buf[6] == 3) {
  447. /* silent chunk */
  448. vmdaudio_loadsound(s, output_samples, p, 1);
  449. }
  450. // *datasize = ;
  451. return buf_size;
  452. }
  453. /*
  454. * Public Data Structures
  455. */
  456. AVCodec vmdvideo_decoder = {
  457. "vmdvideo",
  458. CODEC_TYPE_VIDEO,
  459. CODEC_ID_VMDVIDEO,
  460. sizeof(VmdVideoContext),
  461. vmdvideo_decode_init,
  462. NULL,
  463. vmdvideo_decode_end,
  464. vmdvideo_decode_frame,
  465. CODEC_CAP_DR1,
  466. };
  467. AVCodec vmdaudio_decoder = {
  468. "vmdaudio",
  469. CODEC_TYPE_AUDIO,
  470. CODEC_ID_VMDAUDIO,
  471. sizeof(VmdAudioContext),
  472. vmdaudio_decode_init,
  473. NULL,
  474. NULL,
  475. vmdaudio_decode_frame,
  476. };