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.

605 lines
19KB

  1. /*
  2. * Westwood Studios VQA Video Decoder
  3. * Copyright (C) 2003 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 vqavideo.c
  22. * VQA Video Decoder by Mike Melanson (melanson@pcisys.net)
  23. * For more information about the RPZA format, visit:
  24. * http://www.pcisys.net/~melanson/codecs/
  25. *
  26. * The VQA video decoder outputs PAL8 or RGB555 colorspace data, depending
  27. * on the type of data in the file.
  28. *
  29. * This decoder needs the 42-byte VQHD header from the beginning
  30. * of the VQA file passed through the extradata field. The VQHD header
  31. * is laid out as:
  32. *
  33. * bytes 0-3 chunk fourcc: 'VQHD'
  34. * bytes 4-7 chunk size in big-endian format, should be 0x0000002A
  35. * bytes 8-49 VQHD chunk data
  36. *
  37. * Bytes 8-49 are what this decoder expects to see.
  38. *
  39. * Briefly, VQA is a vector quantized animation format that operates in a
  40. * VGA palettized colorspace. It operates on pixel vectors (blocks)
  41. * of either 4x2 or 4x4 in size. Compressed VQA chunks can contain vector
  42. * codebooks, palette information, and code maps for rendering vectors onto
  43. * frames. Any of these components can also be compressed with a run-length
  44. * encoding (RLE) algorithm commonly referred to as "format80".
  45. *
  46. * VQA takes a novel approach to rate control. Each group of n frames
  47. * (usually, n = 8) relies on a different vector codebook. Rather than
  48. * transporting an entire codebook every 8th frame, the new codebook is
  49. * broken up into 8 pieces and sent along with the compressed video chunks
  50. * for each of the 8 frames preceding the 8 frames which require the
  51. * codebook. A full codebook is also sent on the very first frame of a
  52. * file. This is an interesting technique, although it makes random file
  53. * seeking difficult despite the fact that the frames are all intracoded.
  54. *
  55. * V1,2 VQA uses 12-bit codebook indices. If the 12-bit indices were
  56. * packed into bytes and then RLE compressed, bytewise, the results would
  57. * be poor. That is why the coding method divides each index into 2 parts,
  58. * the top 4 bits and the bottom 8 bits, then RL encodes the 4-bit pieces
  59. * together and the 8-bit pieces together. If most of the vectors are
  60. * clustered into one group of 256 vectors, most of the 4-bit index pieces
  61. * should be the same.
  62. */
  63. #include <stdio.h>
  64. #include <stdlib.h>
  65. #include <string.h>
  66. #include <unistd.h>
  67. #include "common.h"
  68. #include "avcodec.h"
  69. #include "dsputil.h"
  70. #define PALETTE_COUNT 256
  71. #define VQA_HEADER_SIZE 0x2A
  72. #define CHUNK_PREAMBLE_SIZE 8
  73. /* allocate the maximum vector space, regardless of the file version:
  74. * (0xFF00 codebook vectors + 0x100 solid pixel vectors) * (4x4 pixels/block) */
  75. #define MAX_CODEBOOK_VECTORS 0xFF00
  76. #define SOLID_PIXEL_VECTORS 0x100
  77. #define MAX_VECTORS (MAX_CODEBOOK_VECTORS + SOLID_PIXEL_VECTORS)
  78. #define MAX_CODEBOOK_SIZE (MAX_VECTORS * 4 * 4)
  79. #define CBF0_TAG MKBETAG('C', 'B', 'F', '0')
  80. #define CBFZ_TAG MKBETAG('C', 'B', 'F', 'Z')
  81. #define CBP0_TAG MKBETAG('C', 'B', 'P', '0')
  82. #define CBPZ_TAG MKBETAG('C', 'B', 'P', 'Z')
  83. #define CPL0_TAG MKBETAG('C', 'P', 'L', '0')
  84. #define CPLZ_TAG MKBETAG('C', 'P', 'L', 'Z')
  85. #define VPTZ_TAG MKBETAG('V', 'P', 'T', 'Z')
  86. #define VQA_DEBUG 0
  87. #if VQA_DEBUG
  88. #define vqa_debug printf
  89. #else
  90. static inline void vqa_debug(const char *format, ...) { }
  91. #endif
  92. typedef struct VqaContext {
  93. AVCodecContext *avctx;
  94. DSPContext dsp;
  95. AVFrame frame;
  96. unsigned char *buf;
  97. int size;
  98. unsigned int palette[PALETTE_COUNT];
  99. int width; /* width of a frame */
  100. int height; /* height of a frame */
  101. int vector_width; /* width of individual vector */
  102. int vector_height; /* height of individual vector */
  103. int vqa_version; /* this should be either 1, 2 or 3 */
  104. unsigned char *codebook; /* the current codebook */
  105. int codebook_size;
  106. unsigned char *next_codebook_buffer; /* accumulator for next codebook */
  107. int next_codebook_buffer_index;
  108. unsigned char *decode_buffer;
  109. int decode_buffer_size;
  110. /* number of frames to go before replacing codebook */
  111. int partial_countdown;
  112. int partial_count;
  113. } VqaContext;
  114. static int vqa_decode_init(AVCodecContext *avctx)
  115. {
  116. VqaContext *s = (VqaContext *)avctx->priv_data;
  117. unsigned char *vqa_header;
  118. int i, j, codebook_index;;
  119. s->avctx = avctx;
  120. avctx->pix_fmt = PIX_FMT_PAL8;
  121. avctx->has_b_frames = 0;
  122. dsputil_init(&s->dsp, avctx);
  123. /* make sure the extradata made it */
  124. if (s->avctx->extradata_size != VQA_HEADER_SIZE) {
  125. av_log(s->avctx, AV_LOG_ERROR, " VQA video: expected extradata size of %d\n", VQA_HEADER_SIZE);
  126. return -1;
  127. }
  128. /* load up the VQA parameters from the header */
  129. vqa_header = (unsigned char *)s->avctx->extradata;
  130. s->vqa_version = vqa_header[0];
  131. s->width = LE_16(&vqa_header[6]);
  132. s->height = LE_16(&vqa_header[8]);
  133. s->vector_width = vqa_header[10];
  134. s->vector_height = vqa_header[11];
  135. s->partial_count = s->partial_countdown = vqa_header[13];
  136. /* the vector dimensions have to meet very stringent requirements */
  137. if ((s->vector_width != 4) ||
  138. ((s->vector_height != 2) && (s->vector_height != 4))) {
  139. /* return without further initialization */
  140. return -1;
  141. }
  142. /* allocate codebooks */
  143. s->codebook_size = MAX_CODEBOOK_SIZE;
  144. s->codebook = av_malloc(s->codebook_size);
  145. s->next_codebook_buffer = av_malloc(s->codebook_size);
  146. /* initialize the solid-color vectors */
  147. if (s->vector_height == 4) {
  148. codebook_index = 0xFF00 * 16;
  149. for (i = 0; i < 256; i++)
  150. for (j = 0; j < 16; j++)
  151. s->codebook[codebook_index++] = i;
  152. } else {
  153. codebook_index = 0xF00 * 8;
  154. for (i = 0; i < 256; i++)
  155. for (j = 0; j < 8; j++)
  156. s->codebook[codebook_index++] = i;
  157. }
  158. s->next_codebook_buffer_index = 0;
  159. /* allocate decode buffer */
  160. s->decode_buffer_size = (s->width / s->vector_width) *
  161. (s->height / s->vector_height) * 2;
  162. s->decode_buffer = av_malloc(s->decode_buffer_size);
  163. s->frame.data[0] = NULL;
  164. return 0;
  165. }
  166. #define CHECK_COUNT() \
  167. if (dest_index + count > dest_size) { \
  168. av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: next op would overflow dest_index\n"); \
  169. av_log(NULL, AV_LOG_ERROR, " VQA video: current dest_index = %d, count = %d, dest_size = %d\n", \
  170. dest_index, count, dest_size); \
  171. return; \
  172. }
  173. static void decode_format80(unsigned char *src, int src_size,
  174. unsigned char *dest, int dest_size, int check_size) {
  175. int src_index = 0;
  176. int dest_index = 0;
  177. int count;
  178. int src_pos;
  179. unsigned char color;
  180. int i;
  181. while (src_index < src_size) {
  182. vqa_debug(" opcode %02X: ", src[src_index]);
  183. /* 0x80 means that frame is finished */
  184. if (src[src_index] == 0x80)
  185. return;
  186. if (dest_index >= dest_size) {
  187. av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: dest_index (%d) exceeded dest_size (%d)\n",
  188. dest_index, dest_size);
  189. return;
  190. }
  191. if (src[src_index] == 0xFF) {
  192. src_index++;
  193. count = LE_16(&src[src_index]);
  194. src_index += 2;
  195. src_pos = LE_16(&src[src_index]);
  196. src_index += 2;
  197. vqa_debug("(1) copy %X bytes from absolute pos %X\n", count, src_pos);
  198. CHECK_COUNT();
  199. for (i = 0; i < count; i++)
  200. dest[dest_index + i] = dest[src_pos + i];
  201. dest_index += count;
  202. } else if (src[src_index] == 0xFE) {
  203. src_index++;
  204. count = LE_16(&src[src_index]);
  205. src_index += 2;
  206. color = src[src_index++];
  207. vqa_debug("(2) set %X bytes to %02X\n", count, color);
  208. CHECK_COUNT();
  209. memset(&dest[dest_index], color, count);
  210. dest_index += count;
  211. } else if ((src[src_index] & 0xC0) == 0xC0) {
  212. count = (src[src_index++] & 0x3F) + 3;
  213. src_pos = LE_16(&src[src_index]);
  214. src_index += 2;
  215. vqa_debug("(3) copy %X bytes from absolute pos %X\n", count, src_pos);
  216. CHECK_COUNT();
  217. for (i = 0; i < count; i++)
  218. dest[dest_index + i] = dest[src_pos + i];
  219. dest_index += count;
  220. } else if (src[src_index] > 0x80) {
  221. count = src[src_index++] & 0x3F;
  222. vqa_debug("(4) copy %X bytes from source to dest\n", count);
  223. CHECK_COUNT();
  224. memcpy(&dest[dest_index], &src[src_index], count);
  225. src_index += count;
  226. dest_index += count;
  227. } else {
  228. count = ((src[src_index] & 0x70) >> 4) + 3;
  229. src_pos = BE_16(&src[src_index]) & 0x0FFF;
  230. src_index += 2;
  231. vqa_debug("(5) copy %X bytes from relpos %X\n", count, src_pos);
  232. CHECK_COUNT();
  233. for (i = 0; i < count; i++)
  234. dest[dest_index + i] = dest[dest_index - src_pos + i];
  235. dest_index += count;
  236. }
  237. }
  238. /* validate that the entire destination buffer was filled; this is
  239. * important for decoding frame maps since each vector needs to have a
  240. * codebook entry; it is not important for compressed codebooks because
  241. * not every entry needs to be filled */
  242. if (check_size)
  243. if (dest_index < dest_size)
  244. av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: decode finished with dest_index (%d) < dest_size (%d)\n",
  245. dest_index, dest_size);
  246. }
  247. static void vqa_decode_chunk(VqaContext *s)
  248. {
  249. unsigned int chunk_type;
  250. unsigned int chunk_size;
  251. int byte_skip;
  252. unsigned int index = 0;
  253. int i;
  254. unsigned char r, g, b;
  255. int index_shift;
  256. int cbf0_chunk = -1;
  257. int cbfz_chunk = -1;
  258. int cbp0_chunk = -1;
  259. int cbpz_chunk = -1;
  260. int cpl0_chunk = -1;
  261. int cplz_chunk = -1;
  262. int vptz_chunk = -1;
  263. int x, y;
  264. int lines = 0;
  265. int pixel_ptr;
  266. int vector_index = 0;
  267. int lobyte = 0;
  268. int hibyte = 0;
  269. int lobytes = 0;
  270. int hibytes = s->decode_buffer_size / 2;
  271. /* first, traverse through the frame and find the subchunks */
  272. while (index < s->size) {
  273. chunk_type = BE_32(&s->buf[index]);
  274. chunk_size = BE_32(&s->buf[index + 4]);
  275. switch (chunk_type) {
  276. case CBF0_TAG:
  277. cbf0_chunk = index;
  278. break;
  279. case CBFZ_TAG:
  280. cbfz_chunk = index;
  281. break;
  282. case CBP0_TAG:
  283. cbp0_chunk = index;
  284. break;
  285. case CBPZ_TAG:
  286. cbpz_chunk = index;
  287. break;
  288. case CPL0_TAG:
  289. cpl0_chunk = index;
  290. break;
  291. case CPLZ_TAG:
  292. cplz_chunk = index;
  293. break;
  294. case VPTZ_TAG:
  295. vptz_chunk = index;
  296. break;
  297. default:
  298. av_log(s->avctx, AV_LOG_ERROR, " VQA video: Found unknown chunk type: %c%c%c%c (%08X)\n",
  299. (chunk_type >> 24) & 0xFF,
  300. (chunk_type >> 16) & 0xFF,
  301. (chunk_type >> 8) & 0xFF,
  302. (chunk_type >> 0) & 0xFF,
  303. chunk_type);
  304. break;
  305. }
  306. byte_skip = chunk_size & 0x01;
  307. index += (CHUNK_PREAMBLE_SIZE + chunk_size + byte_skip);
  308. }
  309. /* next, deal with the palette */
  310. if ((cpl0_chunk != -1) && (cplz_chunk != -1)) {
  311. /* a chunk should not have both chunk types */
  312. av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CPL0 and CPLZ chunks\n");
  313. return;
  314. }
  315. /* decompress the palette chunk */
  316. if (cplz_chunk != -1) {
  317. /* yet to be handled */
  318. }
  319. /* convert the RGB palette into the machine's endian format */
  320. if (cpl0_chunk != -1) {
  321. chunk_size = BE_32(&s->buf[cpl0_chunk + 4]);
  322. /* sanity check the palette size */
  323. if (chunk_size / 3 > 256) {
  324. av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found a palette chunk with %d colors\n",
  325. chunk_size / 3);
  326. return;
  327. }
  328. cpl0_chunk += CHUNK_PREAMBLE_SIZE;
  329. for (i = 0; i < chunk_size / 3; i++) {
  330. /* scale by 4 to transform 6-bit palette -> 8-bit */
  331. r = s->buf[cpl0_chunk++] * 4;
  332. g = s->buf[cpl0_chunk++] * 4;
  333. b = s->buf[cpl0_chunk++] * 4;
  334. s->palette[i] = (r << 16) | (g << 8) | (b);
  335. }
  336. }
  337. /* next, look for a full codebook */
  338. if ((cbf0_chunk != -1) && (cbfz_chunk != -1)) {
  339. /* a chunk should not have both chunk types */
  340. av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CBF0 and CBFZ chunks\n");
  341. return;
  342. }
  343. /* decompress the full codebook chunk */
  344. if (cbfz_chunk != -1) {
  345. chunk_size = BE_32(&s->buf[cbfz_chunk + 4]);
  346. cbfz_chunk += CHUNK_PREAMBLE_SIZE;
  347. decode_format80(&s->buf[cbfz_chunk], chunk_size,
  348. s->codebook, s->codebook_size, 0);
  349. }
  350. /* copy a full codebook */
  351. if (cbf0_chunk != -1) {
  352. chunk_size = BE_32(&s->buf[cbf0_chunk + 4]);
  353. /* sanity check the full codebook size */
  354. if (chunk_size > MAX_CODEBOOK_SIZE) {
  355. av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: CBF0 chunk too large (0x%X bytes)\n",
  356. chunk_size);
  357. return;
  358. }
  359. cbf0_chunk += CHUNK_PREAMBLE_SIZE;
  360. memcpy(s->codebook, &s->buf[cbf0_chunk], chunk_size);
  361. }
  362. /* decode the frame */
  363. if (vptz_chunk == -1) {
  364. /* something is wrong if there is no VPTZ chunk */
  365. av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: no VPTZ chunk found\n");
  366. return;
  367. }
  368. chunk_size = BE_32(&s->buf[vptz_chunk + 4]);
  369. vptz_chunk += CHUNK_PREAMBLE_SIZE;
  370. decode_format80(&s->buf[vptz_chunk], chunk_size,
  371. s->decode_buffer, s->decode_buffer_size, 1);
  372. /* render the final PAL8 frame */
  373. if (s->vector_height == 4)
  374. index_shift = 4;
  375. else
  376. index_shift = 3;
  377. for (y = 0; y < s->frame.linesize[0] * s->height;
  378. y += s->frame.linesize[0] * s->vector_height) {
  379. for (x = y; x < y + s->width; x += 4, lobytes++, hibytes++) {
  380. pixel_ptr = x;
  381. /* get the vector index, the method for which varies according to
  382. * VQA file version */
  383. switch (s->vqa_version) {
  384. case 1:
  385. /* still need sample media for this case (only one game, "Legend of
  386. * Kyrandia III : Malcolm's Revenge", is known to use this version) */
  387. lines = 0;
  388. break;
  389. case 2:
  390. lobyte = s->decode_buffer[lobytes];
  391. hibyte = s->decode_buffer[hibytes];
  392. vector_index = (hibyte << 8) | lobyte;
  393. vector_index <<= index_shift;
  394. lines = s->vector_height;
  395. break;
  396. case 3:
  397. /* not implemented yet */
  398. lines = 0;
  399. break;
  400. }
  401. while (lines--) {
  402. s->frame.data[0][pixel_ptr + 0] = s->codebook[vector_index++];
  403. s->frame.data[0][pixel_ptr + 1] = s->codebook[vector_index++];
  404. s->frame.data[0][pixel_ptr + 2] = s->codebook[vector_index++];
  405. s->frame.data[0][pixel_ptr + 3] = s->codebook[vector_index++];
  406. pixel_ptr += s->frame.linesize[0];
  407. }
  408. }
  409. }
  410. /* handle partial codebook */
  411. if ((cbp0_chunk != -1) && (cbpz_chunk != -1)) {
  412. /* a chunk should not have both chunk types */
  413. av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CBP0 and CBPZ chunks\n");
  414. return;
  415. }
  416. if (cbp0_chunk != -1) {
  417. chunk_size = BE_32(&s->buf[cbp0_chunk + 4]);
  418. cbp0_chunk += CHUNK_PREAMBLE_SIZE;
  419. /* accumulate partial codebook */
  420. memcpy(&s->next_codebook_buffer[s->next_codebook_buffer_index],
  421. &s->buf[cbp0_chunk], chunk_size);
  422. s->next_codebook_buffer_index += chunk_size;
  423. s->partial_countdown--;
  424. if (s->partial_countdown == 0) {
  425. /* time to replace codebook */
  426. memcpy(s->codebook, s->next_codebook_buffer,
  427. s->next_codebook_buffer_index);
  428. /* reset accounting */
  429. s->next_codebook_buffer_index = 0;
  430. s->partial_countdown = s->partial_count;
  431. }
  432. }
  433. if (cbpz_chunk != -1) {
  434. chunk_size = BE_32(&s->buf[cbpz_chunk + 4]);
  435. cbpz_chunk += CHUNK_PREAMBLE_SIZE;
  436. /* accumulate partial codebook */
  437. memcpy(&s->next_codebook_buffer[s->next_codebook_buffer_index],
  438. &s->buf[cbpz_chunk], chunk_size);
  439. s->next_codebook_buffer_index += chunk_size;
  440. s->partial_countdown--;
  441. if (s->partial_countdown == 0) {
  442. /* decompress codebook */
  443. decode_format80(s->next_codebook_buffer,
  444. s->next_codebook_buffer_index,
  445. s->codebook, s->codebook_size, 0);
  446. /* reset accounting */
  447. s->next_codebook_buffer_index = 0;
  448. s->partial_countdown = s->partial_count;
  449. }
  450. }
  451. }
  452. static int vqa_decode_frame(AVCodecContext *avctx,
  453. void *data, int *data_size,
  454. uint8_t *buf, int buf_size)
  455. {
  456. VqaContext *s = (VqaContext *)avctx->priv_data;
  457. s->buf = buf;
  458. s->size = buf_size;
  459. if (s->frame.data[0])
  460. avctx->release_buffer(avctx, &s->frame);
  461. if (avctx->get_buffer(avctx, &s->frame)) {
  462. av_log(s->avctx, AV_LOG_ERROR, " VQA Video: get_buffer() failed\n");
  463. return -1;
  464. }
  465. vqa_decode_chunk(s);
  466. /* make the palette available on the way out */
  467. memcpy(s->frame.data[1], s->palette, PALETTE_COUNT * 4);
  468. s->frame.palette_has_changed = 1;
  469. *data_size = sizeof(AVFrame);
  470. *(AVFrame*)data = s->frame;
  471. /* report that the buffer was completely consumed */
  472. return buf_size;
  473. }
  474. static int vqa_decode_end(AVCodecContext *avctx)
  475. {
  476. VqaContext *s = (VqaContext *)avctx->priv_data;
  477. av_free(s->codebook);
  478. av_free(s->next_codebook_buffer);
  479. av_free(s->decode_buffer);
  480. if (s->frame.data[0])
  481. avctx->release_buffer(avctx, &s->frame);
  482. return 0;
  483. }
  484. AVCodec vqa_decoder = {
  485. "vqavideo",
  486. CODEC_TYPE_VIDEO,
  487. CODEC_ID_WS_VQA,
  488. sizeof(VqaContext),
  489. vqa_decode_init,
  490. NULL,
  491. vqa_decode_end,
  492. vqa_decode_frame,
  493. CODEC_CAP_DR1,
  494. };