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