00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00066 #include <stdio.h>
00067 #include <stdlib.h>
00068 #include <string.h>
00069
00070 #include "libavutil/intreadwrite.h"
00071 #include "libavutil/imgutils.h"
00072 #include "avcodec.h"
00073
00074 #define PALETTE_COUNT 256
00075 #define VQA_HEADER_SIZE 0x2A
00076 #define CHUNK_PREAMBLE_SIZE 8
00077
00078
00079
00080 #define MAX_CODEBOOK_VECTORS 0xFF00
00081 #define SOLID_PIXEL_VECTORS 0x100
00082 #define MAX_VECTORS (MAX_CODEBOOK_VECTORS + SOLID_PIXEL_VECTORS)
00083 #define MAX_CODEBOOK_SIZE (MAX_VECTORS * 4 * 4)
00084
00085 #define CBF0_TAG MKBETAG('C', 'B', 'F', '0')
00086 #define CBFZ_TAG MKBETAG('C', 'B', 'F', 'Z')
00087 #define CBP0_TAG MKBETAG('C', 'B', 'P', '0')
00088 #define CBPZ_TAG MKBETAG('C', 'B', 'P', 'Z')
00089 #define CPL0_TAG MKBETAG('C', 'P', 'L', '0')
00090 #define CPLZ_TAG MKBETAG('C', 'P', 'L', 'Z')
00091 #define VPTZ_TAG MKBETAG('V', 'P', 'T', 'Z')
00092
00093 #define VQA_DEBUG 0
00094
00095 #if VQA_DEBUG
00096 #define vqa_debug printf
00097 #else
00098 static inline void vqa_debug(const char *format, ...) { }
00099 #endif
00100
00101 typedef struct VqaContext {
00102
00103 AVCodecContext *avctx;
00104 AVFrame frame;
00105
00106 const unsigned char *buf;
00107 int size;
00108
00109 uint32_t palette[PALETTE_COUNT];
00110
00111 int width;
00112 int height;
00113 int vector_width;
00114 int vector_height;
00115 int vqa_version;
00116
00117 unsigned char *codebook;
00118 int codebook_size;
00119 unsigned char *next_codebook_buffer;
00120 int next_codebook_buffer_index;
00121
00122 unsigned char *decode_buffer;
00123 int decode_buffer_size;
00124
00125
00126 int partial_countdown;
00127 int partial_count;
00128
00129 } VqaContext;
00130
00131 static av_cold int vqa_decode_init(AVCodecContext *avctx)
00132 {
00133 VqaContext *s = avctx->priv_data;
00134 unsigned char *vqa_header;
00135 int i, j, codebook_index;
00136
00137 s->avctx = avctx;
00138 avctx->pix_fmt = PIX_FMT_PAL8;
00139
00140
00141 if (s->avctx->extradata_size != VQA_HEADER_SIZE) {
00142 av_log(s->avctx, AV_LOG_ERROR, " VQA video: expected extradata size of %d\n", VQA_HEADER_SIZE);
00143 return -1;
00144 }
00145
00146
00147 vqa_header = (unsigned char *)s->avctx->extradata;
00148 s->vqa_version = vqa_header[0];
00149 s->width = AV_RL16(&vqa_header[6]);
00150 s->height = AV_RL16(&vqa_header[8]);
00151 if(av_image_check_size(s->width, s->height, 0, avctx)){
00152 s->width= s->height= 0;
00153 return -1;
00154 }
00155 s->vector_width = vqa_header[10];
00156 s->vector_height = vqa_header[11];
00157 s->partial_count = s->partial_countdown = vqa_header[13];
00158
00159
00160 if ((s->vector_width != 4) ||
00161 ((s->vector_height != 2) && (s->vector_height != 4))) {
00162
00163 return -1;
00164 }
00165
00166
00167 s->codebook_size = MAX_CODEBOOK_SIZE;
00168 s->codebook = av_malloc(s->codebook_size);
00169 s->next_codebook_buffer = av_malloc(s->codebook_size);
00170
00171
00172 if (s->vector_height == 4) {
00173 codebook_index = 0xFF00 * 16;
00174 for (i = 0; i < 256; i++)
00175 for (j = 0; j < 16; j++)
00176 s->codebook[codebook_index++] = i;
00177 } else {
00178 codebook_index = 0xF00 * 8;
00179 for (i = 0; i < 256; i++)
00180 for (j = 0; j < 8; j++)
00181 s->codebook[codebook_index++] = i;
00182 }
00183 s->next_codebook_buffer_index = 0;
00184
00185
00186 s->decode_buffer_size = (s->width / s->vector_width) *
00187 (s->height / s->vector_height) * 2;
00188 s->decode_buffer = av_malloc(s->decode_buffer_size);
00189
00190 s->frame.data[0] = NULL;
00191
00192 return 0;
00193 }
00194
00195 #define CHECK_COUNT() \
00196 if (dest_index + count > dest_size) { \
00197 av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: next op would overflow dest_index\n"); \
00198 av_log(NULL, AV_LOG_ERROR, " VQA video: current dest_index = %d, count = %d, dest_size = %d\n", \
00199 dest_index, count, dest_size); \
00200 return; \
00201 }
00202
00203 static void decode_format80(const unsigned char *src, int src_size,
00204 unsigned char *dest, int dest_size, int check_size) {
00205
00206 int src_index = 0;
00207 int dest_index = 0;
00208 int count;
00209 int src_pos;
00210 unsigned char color;
00211 int i;
00212
00213 while (src_index < src_size) {
00214
00215 vqa_debug(" opcode %02X: ", src[src_index]);
00216
00217
00218 if (src[src_index] == 0x80)
00219 return;
00220
00221 if (dest_index >= dest_size) {
00222 av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: dest_index (%d) exceeded dest_size (%d)\n",
00223 dest_index, dest_size);
00224 return;
00225 }
00226
00227 if (src[src_index] == 0xFF) {
00228
00229 src_index++;
00230 count = AV_RL16(&src[src_index]);
00231 src_index += 2;
00232 src_pos = AV_RL16(&src[src_index]);
00233 src_index += 2;
00234 vqa_debug("(1) copy %X bytes from absolute pos %X\n", count, src_pos);
00235 CHECK_COUNT();
00236 for (i = 0; i < count; i++)
00237 dest[dest_index + i] = dest[src_pos + i];
00238 dest_index += count;
00239
00240 } else if (src[src_index] == 0xFE) {
00241
00242 src_index++;
00243 count = AV_RL16(&src[src_index]);
00244 src_index += 2;
00245 color = src[src_index++];
00246 vqa_debug("(2) set %X bytes to %02X\n", count, color);
00247 CHECK_COUNT();
00248 memset(&dest[dest_index], color, count);
00249 dest_index += count;
00250
00251 } else if ((src[src_index] & 0xC0) == 0xC0) {
00252
00253 count = (src[src_index++] & 0x3F) + 3;
00254 src_pos = AV_RL16(&src[src_index]);
00255 src_index += 2;
00256 vqa_debug("(3) copy %X bytes from absolute pos %X\n", count, src_pos);
00257 CHECK_COUNT();
00258 for (i = 0; i < count; i++)
00259 dest[dest_index + i] = dest[src_pos + i];
00260 dest_index += count;
00261
00262 } else if (src[src_index] > 0x80) {
00263
00264 count = src[src_index++] & 0x3F;
00265 vqa_debug("(4) copy %X bytes from source to dest\n", count);
00266 CHECK_COUNT();
00267 memcpy(&dest[dest_index], &src[src_index], count);
00268 src_index += count;
00269 dest_index += count;
00270
00271 } else {
00272
00273 count = ((src[src_index] & 0x70) >> 4) + 3;
00274 src_pos = AV_RB16(&src[src_index]) & 0x0FFF;
00275 src_index += 2;
00276 vqa_debug("(5) copy %X bytes from relpos %X\n", count, src_pos);
00277 CHECK_COUNT();
00278 for (i = 0; i < count; i++)
00279 dest[dest_index + i] = dest[dest_index - src_pos + i];
00280 dest_index += count;
00281 }
00282 }
00283
00284
00285
00286
00287
00288 if (check_size)
00289 if (dest_index < dest_size)
00290 av_log(NULL, AV_LOG_ERROR, " VQA video: decode_format80 problem: decode finished with dest_index (%d) < dest_size (%d)\n",
00291 dest_index, dest_size);
00292 }
00293
00294 static void vqa_decode_chunk(VqaContext *s)
00295 {
00296 unsigned int chunk_type;
00297 unsigned int chunk_size;
00298 int byte_skip;
00299 unsigned int index = 0;
00300 int i;
00301 unsigned char r, g, b;
00302 int index_shift;
00303
00304 int cbf0_chunk = -1;
00305 int cbfz_chunk = -1;
00306 int cbp0_chunk = -1;
00307 int cbpz_chunk = -1;
00308 int cpl0_chunk = -1;
00309 int cplz_chunk = -1;
00310 int vptz_chunk = -1;
00311
00312 int x, y;
00313 int lines = 0;
00314 int pixel_ptr;
00315 int vector_index = 0;
00316 int lobyte = 0;
00317 int hibyte = 0;
00318 int lobytes = 0;
00319 int hibytes = s->decode_buffer_size / 2;
00320
00321
00322 while (index < s->size) {
00323
00324 chunk_type = AV_RB32(&s->buf[index]);
00325 chunk_size = AV_RB32(&s->buf[index + 4]);
00326
00327 switch (chunk_type) {
00328
00329 case CBF0_TAG:
00330 cbf0_chunk = index;
00331 break;
00332
00333 case CBFZ_TAG:
00334 cbfz_chunk = index;
00335 break;
00336
00337 case CBP0_TAG:
00338 cbp0_chunk = index;
00339 break;
00340
00341 case CBPZ_TAG:
00342 cbpz_chunk = index;
00343 break;
00344
00345 case CPL0_TAG:
00346 cpl0_chunk = index;
00347 break;
00348
00349 case CPLZ_TAG:
00350 cplz_chunk = index;
00351 break;
00352
00353 case VPTZ_TAG:
00354 vptz_chunk = index;
00355 break;
00356
00357 default:
00358 av_log(s->avctx, AV_LOG_ERROR, " VQA video: Found unknown chunk type: %c%c%c%c (%08X)\n",
00359 (chunk_type >> 24) & 0xFF,
00360 (chunk_type >> 16) & 0xFF,
00361 (chunk_type >> 8) & 0xFF,
00362 (chunk_type >> 0) & 0xFF,
00363 chunk_type);
00364 break;
00365 }
00366
00367 byte_skip = chunk_size & 0x01;
00368 index += (CHUNK_PREAMBLE_SIZE + chunk_size + byte_skip);
00369 }
00370
00371
00372 if ((cpl0_chunk != -1) && (cplz_chunk != -1)) {
00373
00374
00375 av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CPL0 and CPLZ chunks\n");
00376 return;
00377 }
00378
00379
00380 if (cplz_chunk != -1) {
00381
00382
00383
00384 }
00385
00386
00387 if (cpl0_chunk != -1) {
00388
00389 chunk_size = AV_RB32(&s->buf[cpl0_chunk + 4]);
00390
00391 if (chunk_size / 3 > 256) {
00392 av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found a palette chunk with %d colors\n",
00393 chunk_size / 3);
00394 return;
00395 }
00396 cpl0_chunk += CHUNK_PREAMBLE_SIZE;
00397 for (i = 0; i < chunk_size / 3; i++) {
00398
00399 r = s->buf[cpl0_chunk++] * 4;
00400 g = s->buf[cpl0_chunk++] * 4;
00401 b = s->buf[cpl0_chunk++] * 4;
00402 s->palette[i] = (r << 16) | (g << 8) | (b);
00403 }
00404 }
00405
00406
00407 if ((cbf0_chunk != -1) && (cbfz_chunk != -1)) {
00408
00409
00410 av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CBF0 and CBFZ chunks\n");
00411 return;
00412 }
00413
00414
00415 if (cbfz_chunk != -1) {
00416
00417 chunk_size = AV_RB32(&s->buf[cbfz_chunk + 4]);
00418 cbfz_chunk += CHUNK_PREAMBLE_SIZE;
00419 decode_format80(&s->buf[cbfz_chunk], chunk_size,
00420 s->codebook, s->codebook_size, 0);
00421 }
00422
00423
00424 if (cbf0_chunk != -1) {
00425
00426 chunk_size = AV_RB32(&s->buf[cbf0_chunk + 4]);
00427
00428 if (chunk_size > MAX_CODEBOOK_SIZE) {
00429 av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: CBF0 chunk too large (0x%X bytes)\n",
00430 chunk_size);
00431 return;
00432 }
00433 cbf0_chunk += CHUNK_PREAMBLE_SIZE;
00434
00435 memcpy(s->codebook, &s->buf[cbf0_chunk], chunk_size);
00436 }
00437
00438
00439 if (vptz_chunk == -1) {
00440
00441
00442 av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: no VPTZ chunk found\n");
00443 return;
00444 }
00445
00446 chunk_size = AV_RB32(&s->buf[vptz_chunk + 4]);
00447 vptz_chunk += CHUNK_PREAMBLE_SIZE;
00448 decode_format80(&s->buf[vptz_chunk], chunk_size,
00449 s->decode_buffer, s->decode_buffer_size, 1);
00450
00451
00452 if (s->vector_height == 4)
00453 index_shift = 4;
00454 else
00455 index_shift = 3;
00456 for (y = 0; y < s->frame.linesize[0] * s->height;
00457 y += s->frame.linesize[0] * s->vector_height) {
00458
00459 for (x = y; x < y + s->width; x += 4, lobytes++, hibytes++) {
00460 pixel_ptr = x;
00461
00462
00463
00464 switch (s->vqa_version) {
00465
00466 case 1:
00467
00468
00469 lobyte = s->decode_buffer[lobytes * 2];
00470 hibyte = s->decode_buffer[(lobytes * 2) + 1];
00471 vector_index = ((hibyte << 8) | lobyte) >> 3;
00472 vector_index <<= index_shift;
00473 lines = s->vector_height;
00474
00475 if (hibyte == 0xFF) {
00476 while (lines--) {
00477 s->frame.data[0][pixel_ptr + 0] = 255 - lobyte;
00478 s->frame.data[0][pixel_ptr + 1] = 255 - lobyte;
00479 s->frame.data[0][pixel_ptr + 2] = 255 - lobyte;
00480 s->frame.data[0][pixel_ptr + 3] = 255 - lobyte;
00481 pixel_ptr += s->frame.linesize[0];
00482 }
00483 lines=0;
00484 }
00485 break;
00486
00487 case 2:
00488 lobyte = s->decode_buffer[lobytes];
00489 hibyte = s->decode_buffer[hibytes];
00490 vector_index = (hibyte << 8) | lobyte;
00491 vector_index <<= index_shift;
00492 lines = s->vector_height;
00493 break;
00494
00495 case 3:
00496
00497 lines = 0;
00498 break;
00499 }
00500
00501 while (lines--) {
00502 s->frame.data[0][pixel_ptr + 0] = s->codebook[vector_index++];
00503 s->frame.data[0][pixel_ptr + 1] = s->codebook[vector_index++];
00504 s->frame.data[0][pixel_ptr + 2] = s->codebook[vector_index++];
00505 s->frame.data[0][pixel_ptr + 3] = s->codebook[vector_index++];
00506 pixel_ptr += s->frame.linesize[0];
00507 }
00508 }
00509 }
00510
00511
00512 if ((cbp0_chunk != -1) && (cbpz_chunk != -1)) {
00513
00514 av_log(s->avctx, AV_LOG_ERROR, " VQA video: problem: found both CBP0 and CBPZ chunks\n");
00515 return;
00516 }
00517
00518 if (cbp0_chunk != -1) {
00519
00520 chunk_size = AV_RB32(&s->buf[cbp0_chunk + 4]);
00521 cbp0_chunk += CHUNK_PREAMBLE_SIZE;
00522
00523
00524 memcpy(&s->next_codebook_buffer[s->next_codebook_buffer_index],
00525 &s->buf[cbp0_chunk], chunk_size);
00526 s->next_codebook_buffer_index += chunk_size;
00527
00528 s->partial_countdown--;
00529 if (s->partial_countdown == 0) {
00530
00531
00532 memcpy(s->codebook, s->next_codebook_buffer,
00533 s->next_codebook_buffer_index);
00534
00535
00536 s->next_codebook_buffer_index = 0;
00537 s->partial_countdown = s->partial_count;
00538 }
00539 }
00540
00541 if (cbpz_chunk != -1) {
00542
00543 chunk_size = AV_RB32(&s->buf[cbpz_chunk + 4]);
00544 cbpz_chunk += CHUNK_PREAMBLE_SIZE;
00545
00546
00547 memcpy(&s->next_codebook_buffer[s->next_codebook_buffer_index],
00548 &s->buf[cbpz_chunk], chunk_size);
00549 s->next_codebook_buffer_index += chunk_size;
00550
00551 s->partial_countdown--;
00552 if (s->partial_countdown == 0) {
00553
00554
00555 decode_format80(s->next_codebook_buffer,
00556 s->next_codebook_buffer_index,
00557 s->codebook, s->codebook_size, 0);
00558
00559
00560 s->next_codebook_buffer_index = 0;
00561 s->partial_countdown = s->partial_count;
00562 }
00563 }
00564 }
00565
00566 static int vqa_decode_frame(AVCodecContext *avctx,
00567 void *data, int *data_size,
00568 AVPacket *avpkt)
00569 {
00570 const uint8_t *buf = avpkt->data;
00571 int buf_size = avpkt->size;
00572 VqaContext *s = avctx->priv_data;
00573
00574 s->buf = buf;
00575 s->size = buf_size;
00576
00577 if (s->frame.data[0])
00578 avctx->release_buffer(avctx, &s->frame);
00579
00580 if (avctx->get_buffer(avctx, &s->frame)) {
00581 av_log(s->avctx, AV_LOG_ERROR, " VQA Video: get_buffer() failed\n");
00582 return -1;
00583 }
00584
00585 vqa_decode_chunk(s);
00586
00587
00588 memcpy(s->frame.data[1], s->palette, PALETTE_COUNT * 4);
00589 s->frame.palette_has_changed = 1;
00590
00591 *data_size = sizeof(AVFrame);
00592 *(AVFrame*)data = s->frame;
00593
00594
00595 return buf_size;
00596 }
00597
00598 static av_cold int vqa_decode_end(AVCodecContext *avctx)
00599 {
00600 VqaContext *s = avctx->priv_data;
00601
00602 av_free(s->codebook);
00603 av_free(s->next_codebook_buffer);
00604 av_free(s->decode_buffer);
00605
00606 if (s->frame.data[0])
00607 avctx->release_buffer(avctx, &s->frame);
00608
00609 return 0;
00610 }
00611
00612 AVCodec ff_vqa_decoder = {
00613 "vqavideo",
00614 AVMEDIA_TYPE_VIDEO,
00615 CODEC_ID_WS_VQA,
00616 sizeof(VqaContext),
00617 vqa_decode_init,
00618 NULL,
00619 vqa_decode_end,
00620 vqa_decode_frame,
00621 CODEC_CAP_DR1,
00622 .long_name = NULL_IF_CONFIG_SMALL("Westwood Studios VQA (Vector Quantized Animation) video"),
00623 };