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