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.

618 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 LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
  80. #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
  81. #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
  82. (((uint8_t*)(x))[1] << 16) | \
  83. (((uint8_t*)(x))[2] << 8) | \
  84. ((uint8_t*)(x))[3])
  85. #define FOURCC_TAG( ch0, ch1, ch2, ch3 ) \
  86. ( (long)(unsigned char)(ch3) | \
  87. ( (long)(unsigned char)(ch2) << 8 ) | \
  88. ( (long)(unsigned char)(ch1) << 16 ) | \
  89. ( (long)(unsigned char)(ch0) << 24 ) )
  90. #define CBF0_TAG FOURCC_TAG('C', 'B', 'F', '0')
  91. #define CBFZ_TAG FOURCC_TAG('C', 'B', 'F', 'Z')
  92. #define CBP0_TAG FOURCC_TAG('C', 'B', 'P', '0')
  93. #define CBPZ_TAG FOURCC_TAG('C', 'B', 'P', 'Z')
  94. #define CPL0_TAG FOURCC_TAG('C', 'P', 'L', '0')
  95. #define CPLZ_TAG FOURCC_TAG('C', 'P', 'L', 'Z')
  96. #define VPTZ_TAG FOURCC_TAG('V', 'P', 'T', 'Z')
  97. #define VQA_DEBUG 0
  98. #if VQA_DEBUG
  99. #define vqa_debug printf
  100. #else
  101. static inline void vqa_debug(const char *format, ...) { }
  102. #endif
  103. typedef struct VqaContext {
  104. AVCodecContext *avctx;
  105. DSPContext dsp;
  106. AVFrame frame;
  107. unsigned char *buf;
  108. int size;
  109. unsigned int palette[PALETTE_COUNT];
  110. int width; /* width of a frame */
  111. int height; /* height of a frame */
  112. int vector_width; /* width of individual vector */
  113. int vector_height; /* height of individual vector */
  114. int vqa_version; /* this should be either 1, 2 or 3 */
  115. unsigned char *codebook; /* the current codebook */
  116. int codebook_size;
  117. unsigned char *next_codebook_buffer; /* accumulator for next codebook */
  118. int next_codebook_buffer_index;
  119. unsigned char *decode_buffer;
  120. int decode_buffer_size;
  121. /* number of frames to go before replacing codebook */
  122. int partial_countdown;
  123. int partial_count;
  124. } VqaContext;
  125. static int vqa_decode_init(AVCodecContext *avctx)
  126. {
  127. VqaContext *s = (VqaContext *)avctx->priv_data;
  128. unsigned char *vqa_header;
  129. int i, j, codebook_index;;
  130. s->avctx = avctx;
  131. avctx->pix_fmt = PIX_FMT_PAL8;
  132. avctx->has_b_frames = 0;
  133. dsputil_init(&s->dsp, avctx);
  134. /* make sure the extradata made it */
  135. if (s->avctx->extradata_size != VQA_HEADER_SIZE) {
  136. av_log(s->avctx, AV_LOG_ERROR, " VQA video: expected extradata size of %d\n", VQA_HEADER_SIZE);
  137. return -1;
  138. }
  139. /* load up the VQA parameters from the header */
  140. vqa_header = (unsigned char *)s->avctx->extradata;
  141. s->vqa_version = vqa_header[0];
  142. s->width = LE_16(&vqa_header[6]);
  143. s->height = LE_16(&vqa_header[8]);
  144. s->vector_width = vqa_header[10];
  145. s->vector_height = vqa_header[11];
  146. s->partial_count = s->partial_countdown = vqa_header[13];
  147. /* the vector dimensions have to meet very stringent requirements */
  148. if ((s->vector_width != 4) ||
  149. ((s->vector_height != 2) && (s->vector_height != 4))) {
  150. /* return without further initialization */
  151. return -1;
  152. }
  153. /* allocate codebooks */
  154. s->codebook_size = MAX_CODEBOOK_SIZE;
  155. s->codebook = av_malloc(s->codebook_size);
  156. s->next_codebook_buffer = av_malloc(s->codebook_size);
  157. /* initialize the solid-color vectors */
  158. if (s->vector_height == 4) {
  159. codebook_index = 0xFF00 * 16;
  160. for (i = 0; i < 256; i++)
  161. for (j = 0; j < 16; j++)
  162. s->codebook[codebook_index++] = i;
  163. } else {
  164. codebook_index = 0xF00 * 8;
  165. for (i = 0; i < 256; i++)
  166. for (j = 0; j < 8; j++)
  167. s->codebook[codebook_index++] = i;
  168. }
  169. s->next_codebook_buffer_index = 0;
  170. /* allocate decode buffer */
  171. s->decode_buffer_size = (s->width / s->vector_width) *
  172. (s->height / s->vector_height) * 2;
  173. s->decode_buffer = av_malloc(s->decode_buffer_size);
  174. s->frame.data[0] = NULL;
  175. return 0;
  176. }
  177. #define CHECK_COUNT() \
  178. if (dest_index + count > dest_size) { \
  179. av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: next op would overflow dest_index\n"); \
  180. av_log(NULL, AV_LOG_ERROR, " VQA video: current dest_index = %d, count = %d, dest_size = %d\n", \
  181. dest_index, count, dest_size); \
  182. return; \
  183. }
  184. static void decode_format80(unsigned char *src, int src_size,
  185. unsigned char *dest, int dest_size, int check_size) {
  186. int src_index = 0;
  187. int dest_index = 0;
  188. int count;
  189. int src_pos;
  190. unsigned char color;
  191. int i;
  192. while (src_index < src_size) {
  193. vqa_debug(" opcode %02X: ", src[src_index]);
  194. /* 0x80 means that frame is finished */
  195. if (src[src_index] == 0x80)
  196. return;
  197. if (dest_index >= dest_size) {
  198. av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: dest_index (%d) exceeded dest_size (%d)\n",
  199. dest_index, dest_size);
  200. return;
  201. }
  202. if (src[src_index] == 0xFF) {
  203. src_index++;
  204. count = LE_16(&src[src_index]);
  205. src_index += 2;
  206. src_pos = LE_16(&src[src_index]);
  207. src_index += 2;
  208. vqa_debug("(1) copy %X bytes from absolute pos %X\n", count, src_pos);
  209. CHECK_COUNT();
  210. for (i = 0; i < count; i++)
  211. dest[dest_index + i] = dest[src_pos + i];
  212. dest_index += count;
  213. } else if (src[src_index] == 0xFE) {
  214. src_index++;
  215. count = LE_16(&src[src_index]);
  216. src_index += 2;
  217. color = src[src_index++];
  218. vqa_debug("(2) set %X bytes to %02X\n", count, color);
  219. CHECK_COUNT();
  220. memset(&dest[dest_index], color, count);
  221. dest_index += count;
  222. } else if ((src[src_index] & 0xC0) == 0xC0) {
  223. count = (src[src_index++] & 0x3F) + 3;
  224. src_pos = LE_16(&src[src_index]);
  225. src_index += 2;
  226. vqa_debug("(3) copy %X bytes from absolute pos %X\n", count, src_pos);
  227. CHECK_COUNT();
  228. for (i = 0; i < count; i++)
  229. dest[dest_index + i] = dest[src_pos + i];
  230. dest_index += count;
  231. } else if (src[src_index] > 0x80) {
  232. count = src[src_index++] & 0x3F;
  233. vqa_debug("(4) copy %X bytes from source to dest\n", count);
  234. CHECK_COUNT();
  235. memcpy(&dest[dest_index], &src[src_index], count);
  236. src_index += count;
  237. dest_index += count;
  238. } else {
  239. count = ((src[src_index] & 0x70) >> 4) + 3;
  240. src_pos = BE_16(&src[src_index]) & 0x0FFF;
  241. src_index += 2;
  242. vqa_debug("(5) copy %X bytes from relpos %X\n", count, src_pos);
  243. CHECK_COUNT();
  244. for (i = 0; i < count; i++)
  245. dest[dest_index + i] = dest[dest_index - src_pos + i];
  246. dest_index += count;
  247. }
  248. }
  249. /* validate that the entire destination buffer was filled; this is
  250. * important for decoding frame maps since each vector needs to have a
  251. * codebook entry; it is not important for compressed codebooks because
  252. * not every entry needs to be filled */
  253. if (check_size)
  254. if (dest_index < dest_size)
  255. av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: decode finished with dest_index (%d) < dest_size (%d)\n",
  256. dest_index, dest_size);
  257. }
  258. static void vqa_decode_chunk(VqaContext *s)
  259. {
  260. unsigned int chunk_type;
  261. unsigned int chunk_size;
  262. int byte_skip;
  263. unsigned int index = 0;
  264. int i;
  265. unsigned char r, g, b;
  266. int index_shift;
  267. int cbf0_chunk = -1;
  268. int cbfz_chunk = -1;
  269. int cbp0_chunk = -1;
  270. int cbpz_chunk = -1;
  271. int cpl0_chunk = -1;
  272. int cplz_chunk = -1;
  273. int vptz_chunk = -1;
  274. int x, y;
  275. int lines = 0;
  276. int pixel_ptr;
  277. int vector_index = 0;
  278. int lobyte = 0;
  279. int hibyte = 0;
  280. int lobytes = 0;
  281. int hibytes = s->decode_buffer_size / 2;
  282. /* first, traverse through the frame and find the subchunks */
  283. while (index < s->size) {
  284. chunk_type = BE_32(&s->buf[index]);
  285. chunk_size = BE_32(&s->buf[index + 4]);
  286. switch (chunk_type) {
  287. case CBF0_TAG:
  288. cbf0_chunk = index;
  289. break;
  290. case CBFZ_TAG:
  291. cbfz_chunk = index;
  292. break;
  293. case CBP0_TAG:
  294. cbp0_chunk = index;
  295. break;
  296. case CBPZ_TAG:
  297. cbpz_chunk = index;
  298. break;
  299. case CPL0_TAG:
  300. cpl0_chunk = index;
  301. break;
  302. case CPLZ_TAG:
  303. cplz_chunk = index;
  304. break;
  305. case VPTZ_TAG:
  306. vptz_chunk = index;
  307. break;
  308. default:
  309. av_log(s->avctx, AV_LOG_ERROR, " VQA video: Found unknown chunk type: %c%c%c%c (%08X)\n",
  310. (chunk_type >> 24) & 0xFF,
  311. (chunk_type >> 16) & 0xFF,
  312. (chunk_type >> 8) & 0xFF,
  313. (chunk_type >> 0) & 0xFF,
  314. chunk_type);
  315. break;
  316. }
  317. byte_skip = chunk_size & 0x01;
  318. index += (CHUNK_PREAMBLE_SIZE + chunk_size + byte_skip);
  319. }
  320. /* next, deal with the palette */
  321. if ((cpl0_chunk != -1) && (cplz_chunk != -1)) {
  322. /* a chunk should not have both chunk types */
  323. av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CPL0 and CPLZ chunks\n");
  324. return;
  325. }
  326. /* decompress the palette chunk */
  327. if (cplz_chunk != -1) {
  328. /* yet to be handled */
  329. }
  330. /* convert the RGB palette into the machine's endian format */
  331. if (cpl0_chunk != -1) {
  332. chunk_size = BE_32(&s->buf[cpl0_chunk + 4]);
  333. /* sanity check the palette size */
  334. if (chunk_size / 3 > 256) {
  335. av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found a palette chunk with %d colors\n",
  336. chunk_size / 3);
  337. return;
  338. }
  339. cpl0_chunk += CHUNK_PREAMBLE_SIZE;
  340. for (i = 0; i < chunk_size / 3; i++) {
  341. /* scale by 4 to transform 6-bit palette -> 8-bit */
  342. r = s->buf[cpl0_chunk++] * 4;
  343. g = s->buf[cpl0_chunk++] * 4;
  344. b = s->buf[cpl0_chunk++] * 4;
  345. s->palette[i] = (r << 16) | (g << 8) | (b);
  346. }
  347. }
  348. /* next, look for a full codebook */
  349. if ((cbf0_chunk != -1) && (cbfz_chunk != -1)) {
  350. /* a chunk should not have both chunk types */
  351. av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CBF0 and CBFZ chunks\n");
  352. return;
  353. }
  354. /* decompress the full codebook chunk */
  355. if (cbfz_chunk != -1) {
  356. chunk_size = BE_32(&s->buf[cbfz_chunk + 4]);
  357. cbfz_chunk += CHUNK_PREAMBLE_SIZE;
  358. decode_format80(&s->buf[cbfz_chunk], chunk_size,
  359. s->codebook, s->codebook_size, 0);
  360. }
  361. /* copy a full codebook */
  362. if (cbf0_chunk != -1) {
  363. chunk_size = BE_32(&s->buf[cbf0_chunk + 4]);
  364. /* sanity check the full codebook size */
  365. if (chunk_size > MAX_CODEBOOK_SIZE) {
  366. av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: CBF0 chunk too large (0x%X bytes)\n",
  367. chunk_size);
  368. return;
  369. }
  370. cbf0_chunk += CHUNK_PREAMBLE_SIZE;
  371. memcpy(s->codebook, &s->buf[cbf0_chunk], chunk_size);
  372. }
  373. /* decode the frame */
  374. if (vptz_chunk == -1) {
  375. /* something is wrong if there is no VPTZ chunk */
  376. av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: no VPTZ chunk found\n");
  377. return;
  378. }
  379. chunk_size = BE_32(&s->buf[vptz_chunk + 4]);
  380. vptz_chunk += CHUNK_PREAMBLE_SIZE;
  381. decode_format80(&s->buf[vptz_chunk], chunk_size,
  382. s->decode_buffer, s->decode_buffer_size, 1);
  383. /* render the final PAL8 frame */
  384. if (s->vector_height == 4)
  385. index_shift = 4;
  386. else
  387. index_shift = 3;
  388. for (y = 0; y < s->frame.linesize[0] * s->height;
  389. y += s->frame.linesize[0] * s->vector_height) {
  390. for (x = y; x < y + s->width; x += 4, lobytes++, hibytes++) {
  391. pixel_ptr = x;
  392. /* get the vector index, the method for which varies according to
  393. * VQA file version */
  394. switch (s->vqa_version) {
  395. case 1:
  396. /* still need sample media for this case (only one game, "Legend of
  397. * Kyrandia III : Malcolm's Revenge", is known to use this version) */
  398. lines = 0;
  399. break;
  400. case 2:
  401. lobyte = s->decode_buffer[lobytes];
  402. hibyte = s->decode_buffer[hibytes];
  403. vector_index = (hibyte << 8) | lobyte;
  404. vector_index <<= index_shift;
  405. lines = s->vector_height;
  406. break;
  407. case 3:
  408. /* not implemented yet */
  409. lines = 0;
  410. break;
  411. }
  412. while (lines--) {
  413. s->frame.data[0][pixel_ptr + 0] = s->codebook[vector_index++];
  414. s->frame.data[0][pixel_ptr + 1] = s->codebook[vector_index++];
  415. s->frame.data[0][pixel_ptr + 2] = s->codebook[vector_index++];
  416. s->frame.data[0][pixel_ptr + 3] = s->codebook[vector_index++];
  417. pixel_ptr += s->frame.linesize[0];
  418. }
  419. }
  420. }
  421. /* handle partial codebook */
  422. if ((cbp0_chunk != -1) && (cbpz_chunk != -1)) {
  423. /* a chunk should not have both chunk types */
  424. av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CBP0 and CBPZ chunks\n");
  425. return;
  426. }
  427. if (cbp0_chunk != -1) {
  428. chunk_size = BE_32(&s->buf[cbp0_chunk + 4]);
  429. cbp0_chunk += CHUNK_PREAMBLE_SIZE;
  430. /* accumulate partial codebook */
  431. memcpy(&s->next_codebook_buffer[s->next_codebook_buffer_index],
  432. &s->buf[cbp0_chunk], chunk_size);
  433. s->next_codebook_buffer_index += chunk_size;
  434. s->partial_countdown--;
  435. if (s->partial_countdown == 0) {
  436. /* time to replace codebook */
  437. memcpy(s->codebook, s->next_codebook_buffer,
  438. s->next_codebook_buffer_index);
  439. /* reset accounting */
  440. s->next_codebook_buffer_index = 0;
  441. s->partial_countdown = s->partial_count;
  442. }
  443. }
  444. if (cbpz_chunk != -1) {
  445. chunk_size = BE_32(&s->buf[cbpz_chunk + 4]);
  446. cbpz_chunk += CHUNK_PREAMBLE_SIZE;
  447. /* accumulate partial codebook */
  448. memcpy(&s->next_codebook_buffer[s->next_codebook_buffer_index],
  449. &s->buf[cbpz_chunk], chunk_size);
  450. s->next_codebook_buffer_index += chunk_size;
  451. s->partial_countdown--;
  452. if (s->partial_countdown == 0) {
  453. /* decompress codebook */
  454. decode_format80(s->next_codebook_buffer,
  455. s->next_codebook_buffer_index,
  456. s->codebook, s->codebook_size, 0);
  457. /* reset accounting */
  458. s->next_codebook_buffer_index = 0;
  459. s->partial_countdown = s->partial_count;
  460. }
  461. }
  462. }
  463. static int vqa_decode_frame(AVCodecContext *avctx,
  464. void *data, int *data_size,
  465. uint8_t *buf, int buf_size)
  466. {
  467. VqaContext *s = (VqaContext *)avctx->priv_data;
  468. s->buf = buf;
  469. s->size = buf_size;
  470. if (s->frame.data[0])
  471. avctx->release_buffer(avctx, &s->frame);
  472. if (avctx->get_buffer(avctx, &s->frame)) {
  473. av_log(s->avctx, AV_LOG_ERROR, " VQA Video: get_buffer() failed\n");
  474. return -1;
  475. }
  476. vqa_decode_chunk(s);
  477. /* make the palette available on the way out */
  478. memcpy(s->frame.data[1], s->palette, PALETTE_COUNT * 4);
  479. s->frame.palette_has_changed = 1;
  480. *data_size = sizeof(AVFrame);
  481. *(AVFrame*)data = s->frame;
  482. /* report that the buffer was completely consumed */
  483. return buf_size;
  484. }
  485. static int vqa_decode_end(AVCodecContext *avctx)
  486. {
  487. VqaContext *s = (VqaContext *)avctx->priv_data;
  488. av_free(s->codebook);
  489. av_free(s->next_codebook_buffer);
  490. av_free(s->decode_buffer);
  491. if (s->frame.data[0])
  492. avctx->release_buffer(avctx, &s->frame);
  493. return 0;
  494. }
  495. AVCodec vqa_decoder = {
  496. "vqavideo",
  497. CODEC_TYPE_VIDEO,
  498. CODEC_ID_WS_VQA,
  499. sizeof(VqaContext),
  500. vqa_decode_init,
  501. NULL,
  502. vqa_decode_end,
  503. vqa_decode_frame,
  504. CODEC_CAP_DR1,
  505. };