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.

589 lines
16KB

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