comparison vqavideo.c @ 1496:b78a9ba6a568 libavcodec

first pass at a VQA video decoder
author tmmm
date Thu, 02 Oct 2003 05:20:07 +0000
parents
children dc6f46cd4a7b
comparison
equal deleted inserted replaced
1495:07029b2cd44f 1496:b78a9ba6a568
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 /**
22 * @file vqavideo.c
23 * VQA Video Decoder by Mike Melanson (melanson@pcisys.net)
24 * For more information about the RPZA format, visit:
25 * http://www.pcisys.net/~melanson/codecs/
26 *
27 * The VQA video decoder outputs PAL8 colorspace data.
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-50 VQHD chunk data
36 *
37 * Bytes 8-50 are what this decoder expects to see.
38 *
39 * Briefly, VQA is a vector quantized animation format that operates in a
40 * 6-bit 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, the 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
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <unistd.h>
68
69 #include "common.h"
70 #include "avcodec.h"
71 #include "dsputil.h"
72
73 #define PALETTE_COUNT 256
74 #define VQA_HEADER_SIZE 0x2A
75 #define CHUNK_PREAMBLE_SIZE 8
76
77 /* v1, v2 files: each vector codebook entry is 4x2=8 pixels = 8 bytes */
78 #define V1_2_VECTOR_SIZE 8
79 #define V1_2_MAX_VECTORS 0xF00
80 /* v3 files: each vector codebook entry is 4x4=16 pixels = 16 bytes */
81 #define V3_VECTOR_SIZE 16
82 #define V3_MAX_VECTORS 0xFF00
83
84 #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
85 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
86 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
87 (((uint8_t*)(x))[1] << 16) | \
88 (((uint8_t*)(x))[2] << 8) | \
89 ((uint8_t*)(x))[3])
90
91 #define FOURCC_TAG( ch0, ch1, ch2, ch3 ) \
92 ( (long)(unsigned char)(ch3) | \
93 ( (long)(unsigned char)(ch2) << 8 ) | \
94 ( (long)(unsigned char)(ch1) << 16 ) | \
95 ( (long)(unsigned char)(ch0) << 24 ) )
96
97 #define CBF0_TAG FOURCC_TAG('C', 'B', 'F', '0')
98 #define CBFZ_TAG FOURCC_TAG('C', 'B', 'F', 'Z')
99 #define CBP0_TAG FOURCC_TAG('C', 'B', 'P', '0')
100 #define CBPZ_TAG FOURCC_TAG('C', 'B', 'P', 'Z')
101 #define CPL0_TAG FOURCC_TAG('C', 'P', 'L', '0')
102 #define CPLZ_TAG FOURCC_TAG('C', 'P', 'L', 'Z')
103 #define VPTZ_TAG FOURCC_TAG('V', 'P', 'T', 'Z')
104
105 #define VQA_DEBUG 0
106
107 #if VQA_DEBUG
108 #define vqa_debug printf
109 #else
110 static inline void vqa_debug(const char *format, ...) { }
111 #endif
112
113 typedef struct VqaContext {
114
115 AVCodecContext *avctx;
116 DSPContext dsp;
117 AVFrame frame;
118
119 unsigned char *buf;
120 int size;
121
122 unsigned char palette[PALETTE_COUNT * 4];
123
124 int width; /* width of a frame */
125 int height; /* height of a frame */
126 int vector_width; /* width of individual vector */
127 int vector_height; /* height of individual vector */
128 int vqa_version; /* this should be either 1, 2 or 3 */
129
130 unsigned char *codebook; /* the current codebook */
131 unsigned char *next_codebook_buffer; /* accumulator for next codebook */
132 int next_codebook_buffer_index;
133
134 unsigned char *decode_buffer;
135 int decode_buffer_size;
136
137 /* number of frames to go before replacing codebook */
138 int partial_countdown;
139 int partial_count;
140
141 } VqaContext;
142
143 static int vqa_decode_init(AVCodecContext *avctx)
144 {
145 VqaContext *s = (VqaContext *)avctx->priv_data;
146 unsigned char *vqa_header;
147
148 s->avctx = avctx;
149 avctx->pix_fmt = PIX_FMT_PAL8;
150 avctx->has_b_frames = 0;
151 dsputil_init(&s->dsp, avctx);
152
153 /* make sure the extradata made it */
154 if (s->avctx->extradata_size != VQA_HEADER_SIZE) {
155 printf(" VQA video: expected extradata size of %d\n", VQA_HEADER_SIZE);
156 return -1;
157 }
158
159 /* load up the VQA parameters from the header */
160 vqa_header = (unsigned char *)s->avctx->extradata;
161 s->vqa_version = vqa_header[0];
162 s->width = LE_16(&vqa_header[6]);
163 s->height = LE_16(&vqa_header[8]);
164 s->vector_width = vqa_header[10];
165 s->vector_height = vqa_header[11];
166 s->partial_count = s->partial_countdown = vqa_header[13];
167
168 /* the vector dimensions have to meet very stringent requirements */
169 if ((s->vector_width != 4) ||
170 ((s->vector_height != 2) && (s->vector_height != 4))) {
171 /* return without further initialization */
172 return -1;
173 }
174
175 /* allocate codebooks */
176 if (s->vqa_version == 3) {
177 s->codebook = av_malloc(V3_VECTOR_SIZE * V3_MAX_VECTORS);
178 s->next_codebook_buffer = av_malloc(V3_VECTOR_SIZE * V3_MAX_VECTORS);
179 } else {
180 s->codebook = av_malloc(V1_2_VECTOR_SIZE * V1_2_MAX_VECTORS);
181 s->next_codebook_buffer = av_malloc(V1_2_VECTOR_SIZE * V1_2_MAX_VECTORS);
182 }
183 s->next_codebook_buffer_index = 0;
184
185 /* allocate decode buffer */
186 s->decode_buffer_size = (s->width / s->vector_width) *
187 (s->height / s->vector_height) * 2;
188 s->decode_buffer = av_malloc(s->decode_buffer_size);
189
190 s->frame.data[0] = NULL;
191
192 return 0;
193 }
194
195 #define CHECK_COUNT() \
196 if (dest_index + count > dest_size) { \
197 printf ("vqavideo: decode_format80 problem: next op would overflow dest_index\n"); \
198 printf ("vqavideo: current dest_index = %d, count = %d, dest_size = %d\n", \
199 dest_index, count, dest_size); \
200 return; \
201 }
202
203 static void decode_format80(unsigned char *src, int src_size,
204 unsigned char *dest, int dest_size) {
205
206 int src_index = 0;
207 int dest_index = 0;
208 int count;
209 int src_pos;
210 unsigned char color;
211 int i;
212
213 while (src_index < src_size) {
214
215 vqa_debug(" opcode %02X: ", src[src_index]);
216
217 /* 0x80 means that frame is finished */
218 if (src[src_index] == 0x80)
219 return;
220
221 if (dest_index >= dest_size) {
222 printf ("vqavideo: decode_format80 problem: dest_index (%d) exceeded dest_size (%d)\n",
223 dest_index, dest_size);
224 return;
225 }
226
227 if (src[src_index] == 0xFF) {
228
229 src_index++;
230 count = LE_16(&src[src_index]);
231 src_index += 2;
232 src_pos = LE_16(&src[src_index]);
233 src_index += 2;
234 vqa_debug("(1) copy %X bytes from absolute pos %X\n", count, src_pos);
235 CHECK_COUNT();
236 for (i = 0; i < count; i++)
237 dest[dest_index + i] = dest[src_pos + i];
238 dest_index += count;
239
240 } else if (src[src_index] == 0xFE) {
241
242 src_index++;
243 count = LE_16(&src[src_index]);
244 src_index += 2;
245 color = src[src_index++];
246 vqa_debug("(2) set %X bytes to %02X\n", count, color);
247 CHECK_COUNT();
248 memset(&dest[dest_index], color, count);
249 dest_index += count;
250
251 } else if ((src[src_index] & 0xC0) == 0xC0) {
252
253 count = (src[src_index++] & 0x3F) + 3;
254 src_pos = LE_16(&src[src_index]);
255 src_index += 2;
256 vqa_debug("(3) copy %X bytes from absolute pos %X\n", count, src_pos);
257 CHECK_COUNT();
258 for (i = 0; i < count; i++)
259 dest[dest_index + i] = dest[src_pos + i];
260 dest_index += count;
261
262 } else if (src[src_index] > 0x80) {
263
264 count = src[src_index++] & 0x3F;
265 vqa_debug("(4) copy %X bytes from source to dest\n", count);
266 CHECK_COUNT();
267 memcpy(&dest[dest_index], &src[src_index], count);
268 src_index += count;
269 dest_index += count;
270
271 } else {
272
273 count = ((src[src_index] & 0x70) >> 4) + 3;
274 src_pos = BE_16(&src[src_index]) & 0x0FFF;
275 src_index += 2;
276 vqa_debug("(5) copy %X bytes from relpos %X\n", count, src_pos);
277 CHECK_COUNT();
278 for (i = 0; i < count; i++)
279 dest[dest_index + i] = dest[dest_index - src_pos + i];
280 dest_index += count;
281 }
282 }
283
284 if (dest_index < dest_size)
285 printf ("vqavideo: decode_format80 problem: decode finished with dest_index (%d) < dest_size (%d)\n",
286 dest_index, dest_size);
287 }
288
289 static void vqa_decode_chunk(VqaContext *s)
290 {
291
292 //static int frame = 0;
293 unsigned int chunk_type;
294 unsigned int chunk_size;
295 int byte_skip;
296 unsigned int index = 0;
297 int i;
298 unsigned char r, g, b;
299 unsigned int *palette32;
300
301 int cbf0_chunk = -1;
302 int cbfz_chunk = -1;
303 int cbp0_chunk = -1;
304 int cbpz_chunk = -1;
305 int cpl0_chunk = -1;
306 int cplz_chunk = -1;
307 int vptz_chunk = -1;
308
309 int x, y;
310 int lines = 0;
311 int pixel_ptr;
312 int vector_index = 0;
313 int lobyte = 0;
314 int hibyte = 0;
315 int lobytes = 0;
316 int hibytes = s->decode_buffer_size / 2;
317
318 //printf (" **** decoding frame #%d, stride = %d\n", frame++, s->frame.linesize[0]);
319 /* first, traverse through the frame and find the subchunks */
320 while (index < s->size) {
321
322 chunk_type = BE_32(&s->buf[index]);
323 chunk_size = BE_32(&s->buf[index + 4]);
324
325 switch (chunk_type) {
326
327 case CBF0_TAG:
328 cbf0_chunk = index;
329 break;
330
331 case CBFZ_TAG:
332 cbfz_chunk = index;
333 break;
334
335 case CBP0_TAG:
336 cbp0_chunk = index;
337 break;
338
339 case CBPZ_TAG:
340 cbpz_chunk = index;
341 break;
342
343 case CPL0_TAG:
344 cpl0_chunk = index;
345 break;
346
347 case CPLZ_TAG:
348 cplz_chunk = index;
349 break;
350
351 case VPTZ_TAG:
352 vptz_chunk = index;
353 break;
354
355 default:
356 printf (" VQA video: Found unknown chunk type: %c%c%c%c (%08X)\n",
357 (chunk_type >> 24) & 0xFF,
358 (chunk_type >> 16) & 0xFF,
359 (chunk_type >> 8) & 0xFF,
360 (chunk_type >> 0) & 0xFF,
361 chunk_type);
362 break;
363 }
364
365 byte_skip = chunk_size & 0x01;
366 index += (CHUNK_PREAMBLE_SIZE + chunk_size + byte_skip);
367 }
368
369 /* next, deal with the palette */
370 if ((cpl0_chunk != -1) && (cplz_chunk != -1)) {
371
372 /* a chunk should not have both chunk types */
373 printf (" VQA video: problem: found both CPL0 and CPLZ chunks\n");
374 return;
375 }
376
377 /* decompress the palette chunk */
378 if (cplz_chunk != -1) {
379
380 /* yet to be handled */
381
382 }
383
384 /* convert the RGB palette into the machine's endian format */
385 if (cpl0_chunk != -1) {
386
387 chunk_size = BE_32(&s->buf[cpl0_chunk + 4]);
388 /* sanity check the palette size */
389 if (chunk_size / 3 > 256) {
390 printf ("vqavideo: problem: found a palette chunk with %d colors\n",
391 chunk_size / 3);
392 return;
393 }
394 cpl0_chunk += CHUNK_PREAMBLE_SIZE;
395 palette32 = (unsigned int *)s->palette;
396 for (i = 0; i < chunk_size / 3; i++) {
397 /* scale by 4 to transform 6-bit palette -> 8-bit */
398 r = s->buf[cpl0_chunk++] * 4;
399 g = s->buf[cpl0_chunk++] * 4;
400 b = s->buf[cpl0_chunk++] * 4;
401 palette32[i] = (r << 16) | (g << 8) | (b);
402 }
403 }
404
405 /* next, look for a full codebook */
406 if ((cbf0_chunk != -1) && (cbfz_chunk != -1)) {
407
408 /* a chunk should not have both chunk types */
409 printf (" VQA video: problem: found both CBF0 and CBFZ chunks\n");
410 return;
411 }
412
413 /* decompress the full codebook chunk into the codebook accumulation
414 * buffer; this is safe since only the first frame is supposed to have
415 * a full codebook */
416 if (cbfz_chunk != -1) {
417
418 /* yet to be handled */
419
420 }
421
422 /* copy a full codebook */
423 if (cbf0_chunk != -1) {
424
425 index = cbf0_chunk;
426
427 chunk_size = BE_32(&s->buf[cbf0_chunk + 4]);
428 /* sanity check the full codebook size */
429 if (s->vqa_version == 3) {
430 if (chunk_size / (V3_VECTOR_SIZE) > V3_MAX_VECTORS) {
431 printf (" VQA video: problem: CBF0 chunk too large (0x%X bytes)\n",
432 chunk_size);
433 return;
434 }
435 } else {
436 if (chunk_size / (V1_2_VECTOR_SIZE) > V1_2_MAX_VECTORS) {
437 printf (" VQA video: problem: CBF0 chunk too large (0x%X bytes)\n",
438 chunk_size);
439 return;
440 }
441 }
442 cbf0_chunk += CHUNK_PREAMBLE_SIZE;
443
444 memcpy(s->codebook, &s->buf[cbf0_chunk], chunk_size);
445 }
446
447 /* decode the frame */
448 if (vptz_chunk == -1) {
449
450 /* something is wrong if there is no VPTZ chunk */
451 printf (" VQA video: problem: no VPTZ chunk found\n");
452 return;
453 }
454
455 /* decode frame */
456 chunk_size = BE_32(&s->buf[vptz_chunk + 4]);
457 vptz_chunk += CHUNK_PREAMBLE_SIZE;
458 decode_format80(&s->buf[vptz_chunk], chunk_size,
459 s->decode_buffer, s->decode_buffer_size);
460
461
462 if (0)
463 {
464 int count = 0;
465 printf (" finished decoding frame...");
466 for (index = 8000; index < 16000; index++)
467 if (s->decode_buffer[index] > 0x0F)
468 count++;
469 printf (" %d/8000 hibytes exceeded 0x0F\n", count);
470 }
471
472
473 /* render the final PAL8 frame */
474 for (y = 0; y < s->frame.linesize[0] * s->height;
475 y += s->frame.linesize[0] * s->vector_height) {
476
477 for (x = y; x < y + s->width; x += 4, lobytes++, hibytes++) {
478 pixel_ptr = x;
479
480 /* get the vector index, the method for which varies according to
481 * VQA file version */
482 switch (s->vqa_version) {
483
484 case 1:
485 /* still need sample media for this case (only one game, "Legend of
486 * Kyrandia III : Malcolm's Revenge", is known to use this version) */
487 lines = 0;
488 break;
489
490 case 2:
491 lobyte = s->decode_buffer[lobytes];
492 hibyte = s->decode_buffer[hibytes];
493 if (hibyte > 0x0F) {
494 printf (" VQA video: problem: vector #%d/%d high byte out of range (0x%X >= 0x0F)\n",
495 lobytes, s->decode_buffer_size / 2, hibyte);
496 hibyte = lobyte = 0;
497 } else if (hibyte == 0x0F)
498 hibyte = 0;
499 vector_index = (hibyte << 8) | lobyte;
500 vector_index *= V1_2_VECTOR_SIZE;
501 lines = 2;
502 break;
503
504 case 3:
505 /* not implemented yet */
506 lines = 0;
507 break;
508 }
509
510 while (lines--) {
511 s->frame.data[0][pixel_ptr + 0] = s->codebook[vector_index++];
512 s->frame.data[0][pixel_ptr + 1] = s->codebook[vector_index++];
513 s->frame.data[0][pixel_ptr + 2] = s->codebook[vector_index++];
514 s->frame.data[0][pixel_ptr + 3] = s->codebook[vector_index++];
515 pixel_ptr += s->frame.linesize[0];
516 }
517 }
518 }
519
520 /* handle partial codebook */
521 if ((cbp0_chunk != -1) && (cbpz_chunk != -1)) {
522 /* a chunk should not have both chunk types */
523 printf (" VQA video: problem: found both CBP0 and CBPZ chunks\n");
524 return;
525 }
526
527 if (cbp0_chunk != -1) {
528
529 chunk_size = BE_32(&s->buf[cbp0_chunk + 4]);
530 cbp0_chunk += CHUNK_PREAMBLE_SIZE;
531
532 /* accumulate partial codebook */
533 memcpy(&s->next_codebook_buffer[s->next_codebook_buffer_index],
534 &s->buf[cbp0_chunk], chunk_size);
535 s->next_codebook_buffer_index += chunk_size;
536
537 s->partial_countdown--;
538 if (s->partial_countdown == 0) {
539
540 /* time to replace codebook */
541 memcpy(s->codebook, s->next_codebook_buffer,
542 s->next_codebook_buffer_index);
543
544 /* reset accounting */
545 s->next_codebook_buffer_index = 0;
546 s->partial_countdown = s->partial_count;
547 }
548 }
549
550 if (cbpz_chunk != -1) {
551
552 /* more partial codebook handling ... */
553
554 }
555 }
556
557 static int vqa_decode_frame(AVCodecContext *avctx,
558 void *data, int *data_size,
559 uint8_t *buf, int buf_size)
560 {
561 VqaContext *s = (VqaContext *)avctx->priv_data;
562
563 s->buf = buf;
564 s->size = buf_size;
565
566 if (s->frame.data[0])
567 avctx->release_buffer(avctx, &s->frame);
568
569 if (avctx->get_buffer(avctx, &s->frame)) {
570 printf (" VQA Video: get_buffer() failed\n");
571 return -1;
572 }
573
574 vqa_decode_chunk(s);
575
576 /* make the palette available on the way out */
577 memcpy(s->frame.data[1], s->palette, PALETTE_COUNT * 4);
578
579 *data_size = sizeof(AVFrame);
580 *(AVFrame*)data = s->frame;
581
582 /* report that the buffer was completely consumed */
583 return buf_size;
584 }
585
586 static int vqa_decode_end(AVCodecContext *avctx)
587 {
588 VqaContext *s = (VqaContext *)avctx->priv_data;
589
590 av_free(s->codebook);
591 av_free(s->next_codebook_buffer);
592 av_free(s->decode_buffer);
593
594 if (s->frame.data[0])
595 avctx->release_buffer(avctx, &s->frame);
596
597 return 0;
598 }
599
600 AVCodec vqa_decoder = {
601 "vqavideo",
602 CODEC_TYPE_VIDEO,
603 CODEC_ID_WS_VQA,
604 sizeof(VqaContext),
605 vqa_decode_init,
606 NULL,
607 vqa_decode_end,
608 vqa_decode_frame,
609 CODEC_CAP_DR1,
610 };